Hex Artifact Content
Not logged in

Artifact d909879b1700d553fb6926ee828aa3ebfb4e4104:

File src/sqlite3.c part of check-in [017d281f45] - Update SQLite to the first release candidate for 3.6.21. by drh on 2009-12-04 15:23:41.

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 32 2d 30        "2009-12-0
5750: 34 20 31 34 3a 32 35 3a 31 39 20 30 38 32 62 38  4 14:25:19 082b8
5760: 64 61 30 30 35 31 32 38 66 34 37 66 36 33 65 39  da005128f47f63e9
5770: 35 62 36 62 37 30 32 62 66 34 35 31 37 32 32 31  5b6b702bf4517221
5780: 62 32 61 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  b2a"../*.** 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 54 68  d back..**.** Th
13360 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
13370 63 63 65 73 73 69 62 6c 65 20 74 6f 20 53 51 4c  ccessible to SQL
13380 20 73 74 61 74 65 6d 65 6e 74 73 20 76 69 61 20   statements via 
13390 74 68 65 0a 2a 2a 20 5b 6c 61 73 74 5f 69 6e 73  the.** [last_ins
133a0 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20  ert_rowid() SQL 
133b0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  function]..**.**
133c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
133d0 2a 20 5b 48 31 32 32 32 31 5d 20 5b 48 31 32 32  * [H12221] [H122
133e0 32 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73  23].**.** If a s
133f0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 70  eparate thread p
13400 65 72 66 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49  erforms a new [I
13410 4e 53 45 52 54 5d 20 6f 6e 20 74 68 65 20 73 61  NSERT] on the sa
13420 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  me.** database c
13430 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
13440 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73  the [sqlite3_las
13450 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
13460 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  ].** function is
13470 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75   running and thu
13480 73 20 63 68 61 6e 67 65 73 20 74 68 65 20 6c 61  s changes the la
13490 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64  st insert [rowid
134a0 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76  ],.** then the v
134b0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
134c0 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69   [sqlite3_last_i
134d0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69  nsert_rowid()] i
134e0 73 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74 61 62  s.** unpredictab
134f0 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74  le and might not
13500 20 65 71 75 61 6c 20 65 69 74 68 65 72 20 74 68   equal either th
13510 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e 65 77  e old or the new
13520 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72 74 20  .** last insert 
13530 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49  [rowid]..*/.SQLI
13540 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
13550 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73  nt64 sqlite3_las
13560 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73  t_insert_rowid(s
13570 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
13580 20 43 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74   CAPI3REF: Count
13590 20 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52   The Number Of R
135a0 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31  ows Modified {H1
135b0 32 32 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a  2240} <S10600>.*
135c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
135d0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  on returns the n
135e0 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73  umber of databas
135f0 65 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65  e rows that were
13600 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69   changed.** or i
13610 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74  nserted or delet
13620 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72  ed by the most r
13630 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65  ecently complete
13640 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a  d SQL statement.
13650 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62  ** on the [datab
13660 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
13670 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
13680 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
13690 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65  ..** Only change
136a0 73 20 74 68 61 74 20 61 72 65 20 64 69 72 65 63  s that are direc
136b0 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79  tly specified by
136c0 20 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b   the [INSERT], [
136d0 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b  UPDATE],.** or [
136e0 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e  DELETE] statemen
136f0 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20  t are counted.  
13700 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65  Auxiliary change
13710 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74  s caused by.** t
13720 72 69 67 67 65 72 73 20 6f 72 20 5b 66 6f 72 65  riggers or [fore
13730 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d  ign key actions]
13740 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64   are not counted
13750 2e 20 55 73 65 20 74 68 65 0a 2a 2a 20 5b 73 71  . Use the.** [sq
13760 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
13770 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20  ges()] function 
13780 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61  to find the tota
13790 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  l number of chan
137a0 67 65 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67  ges.** including
137b0 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
137c0 62 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  by triggers and 
137d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
137e0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ons..**.** Chang
137f0 65 73 20 74 6f 20 61 20 76 69 65 77 20 74 68 61  es to a view tha
13800 74 20 61 72 65 20 73 69 6d 75 6c 61 74 65 64 20  t are simulated 
13810 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f  by an [INSTEAD O
13820 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a 20 61 72  F trigger].** ar
13830 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20  e not counted.  
13840 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 6c 65 20  Only real table 
13850 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e  changes are coun
13860 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f  ted..**.** A "ro
13870 77 20 63 68 61 6e 67 65 22 20 69 73 20 61 20 63  w change" is a c
13880 68 61 6e 67 65 20 74 6f 20 61 20 73 69 6e 67 6c  hange to a singl
13890 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c  e row of a singl
138a0 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65  e table.** cause
138b0 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20  d by an INSERT, 
138c0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54  DELETE, or UPDAT
138d0 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f  E statement.  Ro
138e0 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63  ws that.** are c
138f0 68 61 6e 67 65 64 20 61 73 20 73 69 64 65 20 65  hanged as side e
13900 66 66 65 63 74 73 20 6f 66 20 5b 52 45 50 4c 41  ffects of [REPLA
13910 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 20 72  CE] constraint r
13920 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f  esolution,.** ro
13930 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20 70 72  llback, ABORT pr
13940 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20  ocessing, [DROP 
13950 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e  TABLE], or by an
13960 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61  y other.** mecha
13970 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75  nisms do not cou
13980 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 6f 77  nt as direct row
13990 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20   changes..**.** 
139a0 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e 74 65  A "trigger conte
139b0 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 20 6f  xt" is a scope o
139c0 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 61 74  f execution that
139d0 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65   begins and.** e
139e0 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 63 72  nds with the scr
139f0 69 70 74 20 6f 66 20 61 20 5b 43 52 45 41 54 45  ipt of a [CREATE
13a00 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67   TRIGGER | trigg
13a10 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 20 53 51  er]. .** Most SQ
13a20 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  L statements are
13a30 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f 75  .** evaluated ou
13a40 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 69  tside of any tri
13a50 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 74  gger.  This is t
13a60 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a  he "top level".*
13a70 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78  * trigger contex
13a80 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 72  t.  If a trigger
13a90 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 20   fires from the 
13aa0 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20  top level, a.** 
13ab0 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
13ac0 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 66  ext is entered f
13ad0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
13ae0 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74  of that one.** t
13af0 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 67  rigger.  Subtrig
13b00 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 63  gers create subc
13b10 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 69  ontexts for thei
13b20 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  r duration..**.*
13b30 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  * Calling [sqlit
13b40 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73  e3_exec()] or [s
13b50 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72  qlite3_step()] r
13b60 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 0a  ecursively does.
13b70 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20  ** not create a 
13b80 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
13b90 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ext..**.** This 
13ba0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
13bb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
13bc0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
13bd0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74  s in the.** most
13be0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20   recent INSERT, 
13bf0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
13c00 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  E statement with
13c10 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  in the same.** t
13c20 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a  rigger context..
13c30 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e  **.** Thus, when
13c40 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
13c50 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73   top level, this
13c60 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
13c70 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
13c80 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  of changes in th
13c90 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e  e most recent IN
13ca0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
13cb0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20   DELETE.** that 
13cc0 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74  also occurred at
13cd0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20   the top level. 
13ce0 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79   Within the body
13cf0 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a 2a   of a trigger,.*
13d00 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68  * the sqlite3_ch
13d10 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63  anges() interfac
13d20 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20  e can be called 
13d30 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62  to find the numb
13d40 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73  er of.** changes
13d50 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   in the most rec
13d60 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20  ently completed 
13d70 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
13d80 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61  or DELETE.** sta
13d90 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68  tement within th
13da0 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 61  e body of the sa
13db0 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48  me trigger..** H
13dc0 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d 62  owever, the numb
13dd0 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73  er returned does
13de0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61   not include cha
13df0 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 62  nges.** caused b
13e00 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 69  y subtriggers si
13e10 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 74  nce those have t
13e20 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74  heir own context
13e30 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
13e40 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f   the [sqlite3_to
13e50 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69  tal_changes()] i
13e60 6e 74 65 72 66 61 63 65 2c 20 74 68 65 0a 2a 2a  nterface, the.**
13e70 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20   [count_changes 
13e80 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 74 68 65  pragma], and the
13e90 20 5b 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20   [changes() SQL 
13ea0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  function]..**.**
13eb0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
13ec0 2a 20 5b 48 31 32 32 34 31 5d 20 5b 48 31 32 32  * [H12241] [H122
13ed0 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73  43].**.** If a s
13ee0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d  eparate thread m
13ef0 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20  akes changes on 
13f00 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
13f10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
13f20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63  while [sqlite3_c
13f30 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e  hanges()] is run
13f40 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61  ning then the va
13f50 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  lue returned.** 
13f60 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65  is unpredictable
13f70 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67   and not meaning
13f80 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ful..*/.SQLITE_A
13f90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
13fa0 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
13fb0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
13fc0 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20  F: Total Number 
13fd0 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64  Of Rows Modified
13fe0 20 7b 48 31 32 32 36 30 7d 20 3c 53 31 30 36 30   {H12260} <S1060
13ff0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  0>.**.** This fu
14000 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
14010 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
14020 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
14030 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20  by [INSERT],.** 
14040 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b 44 45 4c  [UPDATE] or [DEL
14050 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 73 20  ETE] statements 
14060 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62  since the [datab
14070 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14080 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54  was opened..** T
14090 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65  he count include
140a0 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72  s all changes fr
140b0 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54  om all [CREATE T
140c0 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72  RIGGER | trigger
140d0 5d 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 20 61  ] .** contexts a
140e0 6e 64 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  nd changes made 
140f0 62 79 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20  by [foreign key 
14100 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65  actions]. Howeve
14110 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20  r,.** the count 
14120 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
14130 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f   changes used to
14140 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c   implement [REPL
14150 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73  ACE] constraints
14160 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b  ,.** do rollback
14170 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65  s or ABORT proce
14180 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20  ssing, or [DROP 
14190 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e  TABLE] processin
141a0 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74  g.  The.** count
141b0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
141c0 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77 73 20  e rows of views 
141d0 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b 49 4e  that fire an [IN
141e0 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72  STEAD OF trigger
141f0 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 20  ],.** though if 
14200 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74  the INSTEAD OF t
14210 72 69 67 67 65 72 20 6d 61 6b 65 73 20 63 68 61  rigger makes cha
14220 6e 67 65 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c  nges of its own,
14230 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 0a   those changes .
14240 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a  ** are counted..
14250 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61  ** The changes a
14260 72 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f  re counted as so
14270 6f 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d  on as the statem
14280 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74  ent that makes t
14290 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65  hem is.** comple
142a0 74 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74  ted (when the st
142b0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69  atement handle i
142c0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c  s passed to [sql
142d0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
142e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  .** [sqlite3_fin
142f0 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a  alize()])..**.**
14300 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73   See also the [s
14310 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
14320 5d 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  ] interface, the
14330 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67  .** [count_chang
14340 65 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20  es pragma], and 
14350 74 68 65 20 5b 74 6f 74 61 6c 5f 63 68 61 6e 67  the [total_chang
14360 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  es() SQL functio
14370 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  n]..**.** Requir
14380 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
14390 36 31 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a  61] [H12263].**.
143a0 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65  ** If a separate
143b0 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68   thread makes ch
143c0 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d  anges on the sam
143d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
143e0 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b  ction.** while [
143f0 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
14400 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e  anges()] is runn
14410 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ing then the val
14420 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69  ue.** returned i
14430 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20  s unpredictable 
14440 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66  and not meaningf
14450 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ul..*/.SQLITE_AP
14460 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f  I int sqlite3_to
14470 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  tal_changes(sqli
14480 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
14490 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70  PI3REF: Interrup
144a0 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67  t A Long-Running
144b0 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20   Query {H12270} 
144c0 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30500>.**.** T
144d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75  his function cau
144e0 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20  ses any pending 
144f0 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69  database operati
14500 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a  on to abort and.
14510 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73  ** return at its
14520 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74   earliest opport
14530 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74  unity. This rout
14540 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79  ine is typically
14550 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65  .** called in re
14560 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72  sponse to a user
14570 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20   action such as 
14580 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c  pressing "Cancel
14590 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77  ".** or Ctrl-C w
145a0 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61  here the user wa
145b0 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79  nts a long query
145c0 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61   operation to ha
145d0 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  lt.** immediatel
145e0 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73  y..**.** It is s
145f0 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  afe to call this
14600 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20   routine from a 
14610 74 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  thread different
14620 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72   from the.** thr
14630 65 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72  ead that is curr
14640 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68  ently running th
14650 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61  e database opera
14660 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a  tion.  But it.**
14670 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20   is not safe to 
14680 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
14690 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61  e with a [databa
146a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
146b0 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64  hat.** is closed
146c0 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20   or might close 
146d0 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69  before sqlite3_i
146e0 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72  nterrupt() retur
146f0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ns..**.** If an 
14700 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  SQL operation is
14710 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e   very nearly fin
14720 69 73 68 65 64 20 61 74 20 74 68 65 20 74 69 6d  ished at the tim
14730 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65  e when.** sqlite
14740 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 73  3_interrupt() is
14750 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74   called, then it
14760 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20   might not have 
14770 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a  an opportunity.*
14780 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70  * to be interrup
14790 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f  ted and might co
147a0 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65  ntinue to comple
147b0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53  tion..**.** An S
147c0 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  QL operation tha
147d0 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
147e0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
147f0 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e  LITE_INTERRUPT].
14800 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72  .** If the inter
14810 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61  rupted SQL opera
14820 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52  tion is an INSER
14830 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
14840 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20  LETE.** that is 
14850 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63  inside an explic
14860 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  it transaction, 
14870 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20  then the entire 
14880 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77  transaction.** w
14890 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ill be rolled ba
148a0 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ck automatically
148b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
148c0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29  te3_interrupt(D)
148d0 20 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65   call is in effe
148e0 63 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72  ct until all cur
148f0 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a  rently running.*
14900 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
14910 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
14920 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70  nnection] D comp
14930 6c 65 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53  lete.  Any new S
14940 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
14950 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65   that are starte
14960 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  d after the sqli
14970 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
14980 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20  call and before 
14990 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  the .** running 
149a0 73 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68  statements reach
149b0 65 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65  es zero are inte
149c0 72 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68  rrupted as if th
149d0 65 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72  ey had been.** r
149e0 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20  unning prior to 
149f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
14a00 72 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e  rrupt() call.  N
14a10 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ew SQL statement
14a20 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74  s.** that are st
14a30 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20  arted after the 
14a40 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e  running statemen
14a50 74 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  t count reaches 
14a60 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20  zero are.** not 
14a70 65 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20  effected by the 
14a80 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14a90 74 28 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  t()..** A call t
14aa0 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  o sqlite3_interr
14ab0 75 70 74 28 44 29 20 74 68 61 74 20 6f 63 63 75  upt(D) that occu
14ac0 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  rs when there ar
14ad0 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20  e no running.** 
14ae0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
14af0 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61  s a no-op and ha
14b00 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53  s no effect on S
14b10 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
14b20 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65   that are starte
14b30 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  d after the sqli
14b40 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
14b50 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  call returns..**
14b60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
14b70 3a 0a 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48  :.** [H12271] [H
14b80 31 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  12272].**.** If 
14b90 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
14ba0 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77  nection closes w
14bb0 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e  hile [sqlite3_in
14bc0 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73  terrupt()].** is
14bd0 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61   running then ba
14be0 64 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69  d things will li
14bf0 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a  kely happen..*/.
14c00 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
14c10 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14c20 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  t(sqlite3*);../*
14c30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
14c40 74 65 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51  termine If An SQ
14c50 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43  L Statement Is C
14c60 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d  omplete {H10510}
14c70 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70200>.**.** 
14c80 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
14c90 72 65 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67  re useful during
14ca0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e   command-line in
14cb0 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  put to determine
14cc0 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   if the.** curre
14cd0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78  ntly entered tex
14ce0 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20  t seems to form 
14cf0 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73  a complete SQL s
14d00 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69  tatement or.** i
14d10 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70  f additional inp
14d20 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ut is needed bef
14d30 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20  ore sending the 
14d40 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c  text into.** SQL
14d50 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e  ite for parsing.
14d60 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
14d70 20 72 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   return 1 if the
14d80 20 69 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a   input string.**
14d90 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 61   appears to be a
14da0 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74   complete SQL st
14db0 61 74 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74  atement.  A stat
14dc0 65 6d 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20  ement is judged 
14dd0 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74  to be.** complet
14de0 65 20 69 66 20 69 74 20 65 6e 64 73 20 77 69 74  e if it ends wit
14df0 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f  h a semicolon to
14e00 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61  ken and is not a
14e10 20 70 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20   prefix of a.** 
14e20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41  well-formed CREA
14e30 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
14e40 6d 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e  ment.  Semicolon
14e50 73 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64  s that are embed
14e60 64 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74  ded within.** st
14e70 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72  ring literals or
14e80 20 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69   quoted identifi
14e90 65 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d  er names or comm
14ea0 65 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  ents are not.** 
14eb0 69 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65  independent toke
14ec0 6e 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72  ns (they are par
14ed0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69  t of the token i
14ee0 6e 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65  n which they are
14ef0 0a 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e  .** embedded) an
14f00 64 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f  d thus do not co
14f10 75 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65  unt as a stateme
14f20 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20  nt terminator.  
14f30 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e  Whitespace.** an
14f40 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20  d comments that 
14f50 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c  follow the final
14f60 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69   semicolon are i
14f70 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  gnored..**.** Th
14f80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74  ese routines ret
14f90 75 72 6e 20 30 20 69 66 20 74 68 65 20 73 74 61  urn 0 if the sta
14fa0 74 65 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70  tement is incomp
14fb0 6c 65 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d  lete.  If a.** m
14fc0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
14fd0 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c   fails, then SQL
14fe0 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
14ff0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
15000 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e  se routines do n
15010 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51 4c  ot parse the SQL
15020 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73   statements thus
15030 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74  .** will not det
15040 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c  ect syntacticall
15050 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e  y incorrect SQL.
15060 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65  .**.** If SQLite
15070 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e   has not been in
15080 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20  itialized using 
15090 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
150a0 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a  ize()] prior .**
150b0 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
150c0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
150d0 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69  ) then sqlite3_i
150e0 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
150f0 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61  nvoked.** automa
15100 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  tically by sqlit
15110 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e  e3_complete16().
15120 20 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61    If that initia
15130 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a  lization fails,.
15140 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  ** then the retu
15150 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  rn value from sq
15160 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
15170 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a  () will be non-z
15180 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  ero.** regardles
15190 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
151a0 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51  not the input SQ
151b0 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a  L is complete..*
151c0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
151d0 73 3a 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30  s: [H10511] [H10
151e0 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  512].**.** The i
151f0 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  nput to [sqlite3
15200 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73  _complete()] mus
15210 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d  t be a zero-term
15220 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20  inated.** UTF-8 
15230 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  string..**.** Th
15240 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69  e input to [sqli
15250 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29  te3_complete16()
15260 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f  ] must be a zero
15270 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55  -terminated.** U
15280 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20  TF-16 string in 
15290 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
152a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
152b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d   int sqlite3_com
152c0 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72  plete(const char
152d0 20 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   *sql);.SQLITE_A
152e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
152f0 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20  omplete16(const 
15300 76 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a  void *sql);../*.
15310 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67  ** CAPI3REF: Reg
15320 69 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b  ister A Callback
15330 20 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54   To Handle SQLIT
15340 45 5f 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48  E_BUSY Errors {H
15350 31 32 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a  12310} <S40400>.
15360 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15370 6e 65 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61  ne sets a callba
15380 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ck function that
15390 20 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65   might be invoke
153a0 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e  d whenever.** an
153b0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
153c0 20 74 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62   to open a datab
153d0 61 73 65 20 74 61 62 6c 65 20 74 68 61 74 20 61  ase table that a
153e0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
153f0 20 6f 72 20 70 72 6f 63 65 73 73 20 68 61 73 20   or process has 
15400 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  locked..**.** If
15410 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
15420 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  ck is NULL, then
15430 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
15440 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
15450 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72  BLOCKED].** is r
15460 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
15470 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74  ely upon encount
15480 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20  ering the lock. 
15490 49 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  If the busy call
154a0 62 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e  back.** is not N
154b0 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61  ULL, then the ca
154c0 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69  llback will be i
154d0 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20  nvoked with two 
154e0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  arguments..**.**
154f0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
15500 65 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c  ent to the handl
15510 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  er is a copy of 
15520 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65  the void* pointe
15530 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68  r which.** is th
15540 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
15550 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79   to sqlite3_busy
15560 5f 68 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65  _handler().  The
15570 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
15580 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c   to.** the handl
15590 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74  er callback is t
155a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  he number of tim
155b0 65 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79  es that the busy
155c0 20 68 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20   handler has.** 
155d0 62 65 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72  been invoked for
155e0 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76   this locking ev
155f0 65 6e 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ent.  If the.** 
15600 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  busy callback re
15610 74 75 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f  turns 0, then no
15620 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65   additional atte
15630 6d 70 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f  mpts are made to
15640 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 64  .** access the d
15650 61 74 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c  atabase and [SQL
15660 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51  ITE_BUSY] or [SQ
15670 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
15680 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ED] is returned.
15690 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62  .** If the callb
156a0 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
156b0 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68  zero, then anoth
156c0 65 72 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73  er attempt.** is
156d0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68   made to open th
156e0 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 72  e database for r
156f0 65 61 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63  eading and the c
15700 79 63 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a  ycle repeats..**
15710 0a 2a 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65  .** The presence
15720 20 6f 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c   of a busy handl
15730 65 72 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72  er does not guar
15740 61 6e 74 65 65 20 74 68 61 74 20 69 74 20 77 69  antee that it wi
15750 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ll be invoked.**
15760 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c   when there is l
15770 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20  ock contention. 
15780 49 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d  If SQLite determ
15790 69 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69  ines that invoki
157a0 6e 67 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68  ng the busy.** h
157b0 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73  andler could res
157c0 75 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63  ult in a deadloc
157d0 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68  k, it will go ah
157e0 65 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b  ead and return [
157f0 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20  SQLITE_BUSY].** 
15800 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
15810 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61  _BLOCKED] instea
15820 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68  d of invoking th
15830 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
15840 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63  ** Consider a sc
15850 65 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65  enario where one
15860 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
15870 69 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  ing a read lock 
15880 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72  that.** it is tr
15890 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20  ying to promote 
158a0 74 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  to a reserved lo
158b0 63 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f  ck and.** a seco
158c0 6e 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f  nd process is ho
158d0 6c 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64  lding a reserved
158e0 20 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73   lock that it is
158f0 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72   trying.** to pr
15900 6f 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c  omote to an excl
15910 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65  usive lock.  The
15920 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63   first process c
15930 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a  annot proceed.**
15940 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62   because it is b
15950 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65  locked by the se
15960 63 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63  cond and the sec
15970 6f 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e  ond process cann
15980 6f 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65  ot.** proceed be
15990 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63  cause it is bloc
159a0 6b 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ked by the first
159b0 2e 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65  .  If both proce
159c0 73 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  sses.** invoke t
159d0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73  he busy handlers
159e0 2c 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d  , neither will m
159f0 61 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73  ake any progress
15a00 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a  .  Therefore,.**
15a10 20 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20   SQLite returns 
15a20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f  [SQLITE_BUSY] fo
15a30 72 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63  r the first proc
15a40 65 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74  ess, hoping that
15a50 20 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e   this.** will in
15a60 64 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70  duce the first p
15a70 72 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73  rocess to releas
15a80 65 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20  e its read lock 
15a90 61 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65  and allow.** the
15aa0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20   second process 
15ab0 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a  to proceed..**.*
15ac0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75  * The default bu
15ad0 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e  sy callback is N
15ae0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  ULL..**.** The [
15af0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72  SQLITE_BUSY] err
15b00 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  or is converted 
15b10 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  to [SQLITE_IOERR
15b20 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65  _BLOCKED].** whe
15b30 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74  n SQLite is in t
15b40 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c  he middle of a l
15b50 61 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  arge transaction
15b60 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a   where all the.*
15b70 2a 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e  * changes will n
15b80 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20  ot fit into the 
15b90 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e  in-memory cache.
15ba0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a    SQLite will.**
15bb0 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20   already hold a 
15bc0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
15bd0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15be0 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73  le, but it needs
15bf0 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
15c00 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c  his lock to EXCL
15c10 55 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74  USIVE so that it
15c20 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65   can spill cache
15c30 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74  .** pages into t
15c40 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15c50 20 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f   without harm to
15c60 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72   concurrent.** r
15c70 65 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69  eaders.  If it i
15c80 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d  s unable to prom
15c90 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68  ote the lock, th
15ca0 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  en the in-memory
15cb0 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62  .** cache will b
15cc0 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63  e left in an inc
15cd0 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20  onsistent state 
15ce0 61 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72  and so the error
15cf0 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d  .** code is prom
15d00 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65  oted from the re
15d10 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20  latively benign 
15d20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f  [SQLITE_BUSY] to
15d30 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76  .** the more sev
15d40 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  ere [SQLITE_IOER
15d50 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69  R_BLOCKED].  Thi
15d60 73 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f  s error code pro
15d70 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73  motion.** forces
15d80 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
15d90 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68  llback of the ch
15da0 61 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a  anges.  See the.
15db0 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73  ** <a href="/cvs
15dc0 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72  trac/wiki?p=Corr
15dd0 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42  uptionFollowingB
15de0 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f  usyError">.** Co
15df0 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e  rruptionFollowin
15e00 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77  gBusyError</a> w
15e10 69 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64  iki page for a d
15e20 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79  iscussion of why
15e30 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f  .** this is impo
15e40 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rtant..**.** The
15e50 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61  re can only be a
15e60 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e   single busy han
15e70 64 6c 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72  dler defined for
15e80 20 65 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61   each.** [databa
15e90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20  se connection]. 
15ea0 20 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62   Setting a new b
15eb0 75 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61  usy handler clea
15ec0 72 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f  rs any.** previo
15ed0 75 73 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72  usly set handler
15ee0 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c  .  Note that cal
15ef0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75  ling [sqlite3_bu
15f00 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a  sy_timeout()].**
15f10 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f   will also set o
15f20 72 20 63 6c 65 61 72 20 74 68 65 20 62 75 73 79  r clear the busy
15f30 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20   handler..**.** 
15f40 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  The busy callbac
15f50 6b 20 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b  k should not tak
15f60 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68  e any actions wh
15f70 69 63 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a  ich modify the.*
15f80 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
15f90 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
15fa0 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ed the busy hand
15fb0 6c 65 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61  ler.  Any such a
15fc0 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74  ctions.** result
15fd0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65   in undefined be
15fe0 68 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52  havior..** .** R
15ff0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
16000 5b 48 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32  [H12311] [H12312
16010 5d 20 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33  ] [H12314] [H123
16020 31 36 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a  16] [H12318].**.
16030 2a 2a 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65  ** A busy handle
16040 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65  r must not close
16050 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
16060 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b  nnection.** or [
16070 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
16080 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  nt] that invoked
16090 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
160a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
160b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73   int sqlite3_bus
160c0 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65  y_handler(sqlite
160d0 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a  3*, int(*)(void*
160e0 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  ,int), void*);..
160f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
16100 53 65 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f  Set A Busy Timeo
16110 75 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30  ut {H12340} <S40
16120 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  410>.**.** This 
16130 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b  routine sets a [
16140 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
16150 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64  dler | busy hand
16160 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73  ler] that sleeps
16170 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66  .** for a specif
16180 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  ied amount of ti
16190 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20  me when a table 
161a0 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20  is locked.  The 
161b0 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20  handler.** will 
161c0 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74  sleep multiple t
161d0 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65  imes until at le
161e0 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65  ast "ms" millise
161f0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e  conds of sleepin
16200 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75  g.** have accumu
16210 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20  lated. {H12343} 
16220 41 66 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69  After "ms" milli
16230 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
16240 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64  ing,.** the hand
16250 6c 65 72 20 72 65 74 75 72 6e 73 20 30 20 77 68  ler returns 0 wh
16260 69 63 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69  ich causes [sqli
16270 74 65 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72  te3_step()] to r
16280 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45  eturn.** [SQLITE
16290 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54  _BUSY] or [SQLIT
162a0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
162b0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20  ..**.** Calling 
162c0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
162d0 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65  h an argument le
162e0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
162f0 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e   to zero.** turn
16300 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68  s off all busy h
16310 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  andlers..**.** T
16320 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  here can only be
16330 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68   a single busy h
16340 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72  andler for a par
16350 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61  ticular.** [data
16360 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
16370 20 61 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d   any any given m
16380 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68  oment.  If anoth
16390 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a  er busy handler.
163a0 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 64 20 20  ** was defined  
163b0 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  (using [sqlite3_
163c0 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29  busy_handler()])
163d0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
163e0 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  g.** this routin
163f0 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20 62 75  e, that other bu
16400 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c  sy handler is cl
16410 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  eared..**.** Req
16420 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
16430 31 32 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20  12341] [H12343] 
16440 5b 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49  [H12344].*/.SQLI
16450 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
16460 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
16470 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73  sqlite3*, int ms
16480 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
16490 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20  EF: Convenience 
164a0 52 6f 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e  Routines For Run
164b0 6e 69 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31  ning Queries {H1
164c0 32 33 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a  2370} <S10000>.*
164d0 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a  *.** Definition:
164e0 20 41 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62   A <b>result tab
164f0 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79  le</b> is memory
16500 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
16510 63 72 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a  created by the.*
16520 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  * [sqlite3_get_t
16530 61 62 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63  able()] interfac
16540 65 2e 20 20 41 20 72 65 73 75 6c 74 20 74 61 62  e.  A result tab
16550 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a  le records the.*
16560 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79  * complete query
16570 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e   results from on
16580 65 20 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65  e or more querie
16590 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  s..**.** The tab
165a0 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20  le conceptually 
165b0 68 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  has a number of 
165c0 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73  rows and columns
165d0 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20  .  But.** these 
165e0 6e 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20  numbers are not 
165f0 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75  part of the resu
16600 6c 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e  lt table itself.
16610 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65    These.** numbe
16620 72 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20  rs are obtained 
16630 73 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74  separately.  Let
16640 20 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72   N be the number
16650 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20   of rows.** and 
16660 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20  M be the number 
16670 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a  of columns..**.*
16680 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65  * A result table
16690 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
166a0 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f  pointers to zero
166b0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
166c0 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68  8 strings..** Th
166d0 65 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20  ere are (N+1)*M 
166e0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
166f0 61 72 72 61 79 2e 20 20 54 68 65 20 66 69 72 73  array.  The firs
16700 74 20 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69  t M pointers poi
16710 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65  nt.** to zero-te
16720 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73  rminated strings
16730 20 74 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74   that  contain t
16740 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
16750 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20  columns..** The 
16760 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65  remaining entrie
16770 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71  s all point to q
16780 75 65 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e  uery results.  N
16790 55 4c 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c  ULL values resul
167a0 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69  t.** in NULL poi
167b0 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65  nters.  All othe
167c0 72 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20  r values are in 
167d0 74 68 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f  their UTF-8 zero
167e0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73  -terminated.** s
167f0 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
16800 74 69 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64  tion as returned
16810 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c   by [sqlite3_col
16820 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a  umn_text()]..**.
16830 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c  ** A result tabl
16840 65 20 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20  e might consist 
16850 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d  of one or more m
16860 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
16870 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  s..** It is not 
16880 73 61 66 65 20 74 6f 20 70 61 73 73 20 61 20 72  safe to pass a r
16890 65 73 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65  esult table dire
168a0 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33  ctly to [sqlite3
168b0 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72  _free()]..** A r
168c0 65 73 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75  esult table shou
168d0 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65  ld be deallocate
168e0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
168f0 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a  _free_table()]..
16900 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d  **.** As an exam
16910 70 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ple of the resul
16920 74 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20  t table format, 
16930 73 75 70 70 6f 73 65 20 61 20 71 75 65 72 79 20  suppose a query 
16940 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20  result.** is as 
16950 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
16960 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
16970 0a 2a 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20  .**        Name 
16980 20 20 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20         | Age.** 
16990 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d         ---------
169a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
169b0 2a 20 20 20 20 20 20 20 20 41 6c 69 63 65 20 20  *        Alice  
169c0 20 20 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20       | 43.**    
169d0 20 20 20 20 42 6f 62 20 20 20 20 20 20 20 20 20      Bob         
169e0 7c 20 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43  | 28.**        C
169f0 69 6e 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a  indy       | 21.
16a00 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
16a10 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  quote>.**.** The
16a20 72 65 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d  re are two colum
16a30 6e 20 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72  n (M==2) and thr
16a40 65 65 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20  ee rows (N==3). 
16a50 20 54 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73   Thus the.** res
16a60 75 6c 74 20 74 61 62 6c 65 20 68 61 73 20 38 20  ult table has 8 
16a70 65 6e 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73  entries.  Suppos
16a80 65 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  e the result tab
16a90 6c 65 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20  le is stored.** 
16aa0 69 6e 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65  in an array name
16ab0 73 20 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65  s azResult.  The
16ac0 6e 20 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73  n azResult holds
16ad0 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a   this content:.*
16ae0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
16af0 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20  ><pre>.**       
16b00 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d   azResult&#91;0]
16b10 20 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20   = "Name";.**   
16b20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
16b30 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a  1;1] = "Age";.**
16b40 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
16b50 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65  &#91;2] = "Alice
16b60 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
16b70 65 73 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22  esult&#91;3] = "
16b80 34 33 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  43";.**        a
16b90 7a 52 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d  zResult&#91;4] =
16ba0 20 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20   "Bob";.**      
16bb0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35    azResult&#91;5
16bc0 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20  ] = "28";.**    
16bd0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
16be0 3b 36 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a  ;6] = "Cindy";.*
16bf0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
16c00 74 26 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b  t&#91;7] = "21";
16c10 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
16c20 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68  kquote>.**.** Th
16c30 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
16c40 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65  ble() function e
16c50 76 61 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20  valuates one or 
16c60 6d 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f  more.** semicolo
16c70 6e 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20  n-separated SQL 
16c80 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
16c90 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  e zero-terminate
16ca0 64 20 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e  d UTF-8.** strin
16cb0 67 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72  g of its 2nd par
16cc0 61 6d 65 74 65 72 2e 20 20 49 74 20 72 65 74 75  ameter.  It retu
16cd0 72 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62  rns a result tab
16ce0 6c 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69  le to the.** poi
16cf0 6e 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74  nter given in it
16d00 73 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e  s 3rd parameter.
16d10 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  .**.** After the
16d20 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
16d30 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75  n has finished u
16d40 73 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c  sing the result,
16d50 20 69 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61   it should.** pa
16d60 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ss the pointer t
16d70 6f 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  o the result tab
16d80 6c 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72  le to sqlite3_fr
16d90 65 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72  ee_table() in or
16da0 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73  der to.** releas
16db0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61  e the memory tha
16dc0 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20  t was malloced. 
16dd0 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
16de0 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  way the.** [sqli
16df0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61  te3_malloc()] ha
16e00 70 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c  ppens within sql
16e10 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
16e20 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  , the calling.**
16e30 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e   function must n
16e40 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b  ot try to call [
16e50 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
16e60 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a  directly.  Only.
16e70 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ** [sqlite3_free
16e80 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c  _table()] is abl
16e90 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65  e to release the
16ea0 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79   memory properly
16eb0 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a   and safely..**.
16ec0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  ** The sqlite3_g
16ed0 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72  et_table() inter
16ee0 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e  face is implemen
16ef0 74 65 64 20 61 73 20 61 20 77 72 61 70 70 65 72  ted as a wrapper
16f00 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69   around.** [sqli
16f10 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68  te3_exec()].  Th
16f20 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
16f30 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f  ble() routine do
16f40 65 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65  es not have acce
16f50 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74  ss.** to any int
16f60 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63  ernal data struc
16f70 74 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e  tures of SQLite.
16f80 20 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74    It uses only t
16f90 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74  he public.** int
16fa0 65 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 68  erface defined h
16fb0 65 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65  ere.  As a conse
16fc0 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74  quence, errors t
16fd0 68 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65  hat occur in the
16fe0 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65  .** wrapper laye
16ff0 72 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  r outside of the
17000 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74   internal [sqlit
17010 65 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20  e3_exec()] call 
17020 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65  are not.** refle
17030 63 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  cted in subseque
17040 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  nt calls to [sql
17050 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20  ite3_errcode()] 
17060 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  or [sqlite3_errm
17070 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  sg()]..**.** Req
17080 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
17090 31 32 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20  12371] [H12373] 
170a0 5b 48 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36  [H12374] [H12376
170b0 5d 20 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33  ] [H12379] [H123
170c0 38 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  82].*/.SQLITE_AP
170d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65  I int sqlite3_ge
170e0 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74  t_table(.  sqlit
170f0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
17100 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61   /* An open data
17110 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  base */.  const 
17120 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
17130 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61  /* SQL to be eva
17140 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72  luated */.  char
17150 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20   ***pazResult,  
17160 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20    /* Results of 
17170 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69  the query */.  i
17180 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20  nt *pnRow,      
17190 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
171a0 66 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72  f result rows wr
171b0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
171c0 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20  int *pnColumn,  
171d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
171e0 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
171f0 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  s written here *
17200 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
17210 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72  msg       /* Err
17220 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
17230 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  ere */.);.SQLITE
17240 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
17250 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61  3_free_table(cha
17260 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a  r **result);../*
17270 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f  .** CAPI3REF: Fo
17280 72 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50  rmatted String P
17290 72 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e  rinting Function
172a0 73 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30  s {H17400} <S700
172b0 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a  00><S20000>.**.*
172c0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
172d0 20 61 72 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73   are work-alikes
172e0 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74 66 28   of the "printf(
172f0 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e  )" family of fun
17300 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74  ctions.** from t
17310 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
17320 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  brary..**.** The
17330 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
17340 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76  () and sqlite3_v
17350 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e  mprintf() routin
17360 65 73 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a  es write their.*
17370 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d  * results into m
17380 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
17390 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
173a0 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  loc()]..** The s
173b0 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20  trings returned 
173c0 62 79 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75  by these two rou
173d0 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a  tines should be.
173e0 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b  ** released by [
173f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
17400 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20    Both routines 
17410 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c  return a.** NULL
17420 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c   pointer if [sql
17430 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69  ite3_malloc()] i
17440 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  s unable to allo
17450 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d  cate enough.** m
17460 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
17470 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69  e resulting stri
17480 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c  ng..**.** In sql
17490 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
174a0 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c  routine is simil
174b0 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28  ar to "snprintf(
174c0 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73  )" from.** the s
174d0 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
174e0 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  y.  The result i
174f0 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  s written into t
17500 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70  he.** buffer sup
17510 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 65 63  plied as the sec
17520 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 77 68  ond parameter wh
17530 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 76 65  ose size is give
17540 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73  n by.** the firs
17550 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74  t parameter. Not
17560 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72  e that the order
17570 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74   of the.** first
17580 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20   two parameters 
17590 69 73 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d  is reversed from
175a0 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68   snprintf().  Th
175b0 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74  is is an.** hist
175c0 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20  orical accident 
175d0 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66  that cannot be f
175e0 69 78 65 64 20 77 69 74 68 6f 75 74 20 62 72 65  ixed without bre
175f0 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72  aking.** backwar
17600 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
17610 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61  .  Note also tha
17620 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e  t sqlite3_snprin
17630 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  tf().** returns 
17640 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73  a pointer to its
17650 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20   buffer instead 
17660 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
17670 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 61  .** characters a
17680 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ctually written 
17690 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e  into the buffer.
176a0 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a    We admit that.
176b0 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
176c0 20 63 68 61 72 61 63 74 65 72 73 20 77 72 69 74   characters writ
176d0 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d  ten would be a m
176e0 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74 75 72  ore useful retur
176f0 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77  n.** value but w
17700 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20  e cannot change 
17710 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
17720 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e  on of sqlite3_sn
17730 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20  printf().** now 
17740 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67  without breaking
17750 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
17760 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73  **.** As long as
17770 20 74 68 65 20 62 75 66 66 65 72 20 73 69 7a 65   the buffer size
17780 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
17790 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73   zero, sqlite3_s
177a0 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61  nprintf().** gua
177b0 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65  rantees that the
177c0 20 62 75 66 66 65 72 20 69 73 20 61 6c 77 61 79   buffer is alway
177d0 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  s zero-terminate
177e0 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a  d.  The first.**
177f0 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69   parameter "n" i
17800 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65  s the total size
17810 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 2c 20   of the buffer, 
17820 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20  including space 
17830 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20  for.** the zero 
17840 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20  terminator.  So 
17850 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69  the longest stri
17860 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ng that can be c
17870 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69  ompletely.** wri
17880 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31  tten will be n-1
17890 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
178a0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
178b0 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20  s all implement 
178c0 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  some additional 
178d0 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70  formatting.** op
178e0 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75  tions that are u
178f0 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72  seful for constr
17900 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65  ucting SQL state
17910 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66  ments..** All of
17920 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74   the usual print
17930 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f  f() formatting o
17940 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49  ptions apply.  I
17950 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72  n addition, ther
17960 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22  e.** is are "%q"
17970 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22  , "%Q", and "%z"
17980 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20   options..**.** 
17990 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f  The %q option wo
179a0 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74  rks like %s in t
179b0 68 61 74 20 69 74 20 73 75 62 73 74 69 74 75 74  hat it substitut
179c0 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e  es a null-termin
179d0 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66  ated.** string f
179e0 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74  rom the argument
179f0 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71 20 61   list.  But %q a
17a00 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72  lso doubles ever
17a10 79 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72  y '\'' character
17a20 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67  ..** %q is desig
17a30 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ned for use insi
17a40 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  de a string lite
17a50 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e  ral.  By doublin
17a60 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63  g each '\''.** c
17a70 68 61 72 61 63 74 65 72 20 69 74 20 65 73 63 61  haracter it esca
17a80 70 65 73 20 74 68 61 74 20 63 68 61 72 61 63 74  pes that charact
17a90 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74  er and allows it
17aa0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
17ab0 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69  into.** the stri
17ac0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ng..**.** For ex
17ad0 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68  ample, assume th
17ae0 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c  e string variabl
17af0 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73  e zText contains
17b00 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73   text as follows
17b10 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
17b20 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
17b30 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27  ar *zText = "It'
17b40 73 20 61 20 68 61 70 70 79 20 64 61 79 21 22 3b  s a happy day!";
17b50 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
17b60 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e  kquote>.**.** On
17b70 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 20 74  e can use this t
17b80 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74  ext in an SQL st
17b90 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f  atement as follo
17ba0 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
17bb0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
17bc0 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c  char *zSQL = sql
17bd0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e  ite3_mprintf("IN
17be0 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20  SERT INTO table 
17bf0 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a  VALUES('%q')", z
17c00 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
17c10 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
17c20 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
17c30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
17c40 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
17c50 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
17c60 20 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20   Because the %q 
17c70 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73  format string is
17c80 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20   used, the '\'' 
17c90 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65  character in zTe
17ca0 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64  xt.** is escaped
17cb0 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e   and the SQL gen
17cc0 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c  erated is as fol
17cd0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
17ce0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
17cf0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
17d00 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27  ble1 VALUES('It'
17d10 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27  's a happy day!'
17d20 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ).** </pre></blo
17d30 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
17d40 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20  his is correct. 
17d50 20 48 61 64 20 77 65 20 75 73 65 64 20 25 73 20   Had we used %s 
17d60 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74  instead of %q, t
17d70 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c  he generated SQL
17d80 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c  .** would have l
17d90 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a  ooked like this:
17da0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
17db0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53  te><pre>.**  INS
17dc0 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20  ERT INTO table1 
17dd0 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 20 68  VALUES('It's a h
17de0 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20  appy day!');.** 
17df0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
17e00 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  te>.**.** This s
17e10 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73  econd example is
17e20 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65   an SQL syntax e
17e30 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65  rror.  As a gene
17e40 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f  ral rule you sho
17e50 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73  uld.** always us
17e60 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20  e %q instead of 
17e70 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  %s when insertin
17e80 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74  g text into a st
17e90 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a  ring literal..**
17ea0 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f  .** The %Q optio
17eb0 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20  n works like %q 
17ec0 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61  except it also a
17ed0 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65  dds single quote
17ee0 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20  s around.** the 
17ef0 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 74  outside of the t
17f00 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64  otal string.  Ad
17f10 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74  ditionally, if t
17f20 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20  he parameter in 
17f30 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  the.** argument 
17f40 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70  list is a NULL p
17f50 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74  ointer, %Q subst
17f60 69 74 75 74 65 73 20 74 68 65 20 74 65 78 74 20  itutes the text 
17f70 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a  "NULL" (without.
17f80 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73  ** single quotes
17f90 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  ) in place of th
17fa0 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f  e %Q option.  So
17fb0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f  , for example, o
17fc0 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a  ne could say:.**
17fd0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
17fe0 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a  <pre>.**  char *
17ff0 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSQL = sqlite3_m
18000 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49  printf("INSERT I
18010 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53  NTO table VALUES
18020 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a  (%Q)", zText);.*
18030 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  *  sqlite3_exec(
18040 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20  db, zSQL, 0, 0, 
18050 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  0);.**  sqlite3_
18060 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c  free(zSQL);.** <
18070 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
18080 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  e>.**.** The cod
18090 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e  e above will ren
180a0 64 65 72 20 61 20 63 6f 72 72 65 63 74 20 53 51  der a correct SQ
180b0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74  L statement in t
180c0 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61  he zSQL.** varia
180d0 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20  ble even if the 
180e0 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65 20 69  zText variable i
180f0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
18100 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22  ..**.** The "%z"
18110 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69   formatting opti
18120 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79  on works exactly
18130 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74 68 20   like "%s" with 
18140 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20  the.** addition 
18150 74 68 61 74 20 61 66 74 65 72 20 74 68 65 20 73  that after the s
18160 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72  tring has been r
18170 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69  ead and copied i
18180 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  nto.** the resul
18190 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  t, [sqlite3_free
181a0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ()] is called on
181b0 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
181c0 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52  g. {END}.**.** R
181d0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
181e0 5b 48 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36  [H17403] [H17406
181f0 5d 20 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51  ] [H17407].*/.SQ
18200 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
18210 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63  qlite3_mprintf(c
18220 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
18230 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
18240 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e   *sqlite3_vmprin
18250 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  tf(const char*, 
18260 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45  va_list);.SQLITE
18270 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
18280 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c  e3_snprintf(int,
18290 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
182a0 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *, ...);../*.** 
182b0 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79  CAPI3REF: Memory
182c0 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73   Allocation Subs
182d0 79 73 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c  ystem {H17300} <
182e0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20000>.**.** Th
182f0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75  e SQLite core  u
18300 73 65 73 20 74 68 65 73 65 20 74 68 72 65 65 20  ses these three 
18310 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c  routines for all
18320 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69   of its own.** i
18330 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61  nternal memory a
18340 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e  llocation needs.
18350 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70   "Core" in the p
18360 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
18370 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  .** does not inc
18380 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73  lude operating-s
18390 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 20 56  ystem specific V
183a0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
183b0 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f  n.  The.** Windo
183c0 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 74 69  ws VFS uses nati
183d0 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  ve malloc() and 
183e0 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20  free() for some 
183f0 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  operations..**.*
18400 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61  * The sqlite3_ma
18410 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72  lloc() routine r
18420 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
18430 20 74 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f   to a block.** o
18440 66 20 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73  f memory at leas
18450 74 20 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e  t N bytes in len
18460 67 74 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20  gth, where N is 
18470 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  the parameter..*
18480 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c  * If sqlite3_mal
18490 6c 6f 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20  loc() is unable 
184a0 74 6f 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63  to obtain suffic
184b0 69 65 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d  ient free.** mem
184c0 6f 72 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ory, it returns 
184d0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
184e0 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
184f0 72 20 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  r N to.** sqlite
18500 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65  3_malloc() is ze
18510 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74  ro or negative t
18520 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  hen sqlite3_mall
18530 6f 63 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20  oc() returns.** 
18540 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
18550 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71  **.** Calling sq
18560 6c 69 74 65 33 5f 66 72 65 65 28 29 20 77 69 74  lite3_free() wit
18570 68 20 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76  h a pointer prev
18580 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a  iously returned.
18590 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61  ** by sqlite3_ma
185a0 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65  lloc() or sqlite
185b0 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65  3_realloc() rele
185c0 61 73 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79  ases that memory
185d0 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d   so.** that it m
185e0 69 67 68 74 20 62 65 20 72 65 75 73 65 64 2e 20  ight be reused. 
185f0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65   The sqlite3_fre
18600 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a  e() routine is.*
18610 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20  * a no-op if is 
18620 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55  called with a NU
18630 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73  LL pointer.  Pas
18640 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  sing a NULL poin
18650 74 65 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ter.** to sqlite
18660 33 5f 66 72 65 65 28 29 20 69 73 20 68 61 72 6d  3_free() is harm
18670 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62 65 69  less.  After bei
18680 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79  ng freed, memory
18690 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68  .** should neith
186a0 65 72 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77  er be read nor w
186b0 72 69 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65  ritten.  Even re
186c0 61 64 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79  ading previously
186d0 20 66 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79   freed.** memory
186e0 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
186f0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20   a segmentation 
18700 66 61 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73  fault or other s
18710 65 76 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20  evere error..** 
18720 4d 65 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f  Memory corruptio
18730 6e 2c 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f  n, a segmentatio
18740 6e 20 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65  n fault, or othe
18750 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a  r severe error.*
18760 2a 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69  * might result i
18770 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  f sqlite3_free()
18780 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20   is called with 
18790 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
187a0 65 72 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e  er that.** was n
187b0 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ot obtained from
187c0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
187d0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61  ) or sqlite3_rea
187e0 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  lloc()..**.** Th
187f0 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  e sqlite3_reallo
18800 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74  c() interface at
18810 74 65 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65  tempts to resize
18820 20 61 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f   a.** prior memo
18830 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
18840 20 62 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62   be at least N b
18850 79 74 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73  ytes, where N is
18860 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   the.** second p
18870 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d  arameter.  The m
18880 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
18890 20 74 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69   to be resized i
188a0 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70  s the first.** p
188b0 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68  arameter.  If th
188c0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
188d0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
188e0 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e  lloc().** is a N
188f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
18900 20 69 74 73 20 62 65 68 61 76 69 6f 72 20 69 73   its behavior is
18910 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61   identical to ca
18920 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
18930 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65  _malloc(N) where
18940 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64   N is the second
18950 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
18960 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
18970 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e  .** If the secon
18980 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  d parameter to s
18990 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
189a0 20 69 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e   is zero or.** n
189b0 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 68 65  egative then the
189c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 65 78 61   behavior is exa
189d0 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73  ctly the same as
189e0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69   calling.** sqli
189f0 74 65 33 5f 66 72 65 65 28 50 29 20 77 68 65 72  te3_free(P) wher
18a00 65 20 50 20 69 73 20 74 68 65 20 66 69 72 73 74  e P is the first
18a10 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
18a20 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
18a30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  .** sqlite3_real
18a40 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20 61 20  loc() returns a 
18a50 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d  pointer to a mem
18a60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ory allocation.*
18a70 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20  * of at least N 
18a80 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72  bytes in size or
18a90 20 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69   NULL if suffici
18aa0 65 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e  ent memory is un
18ab0 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66  available..** If
18ac0 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f   M is the size o
18ad0 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
18ae0 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e  cation, then min
18af0 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f  (N,M) bytes.** o
18b00 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
18b10 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65  cation are copie
18b20 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e  d into the begin
18b30 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72  ning of buffer r
18b40 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
18b50 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20  lite3_realloc() 
18b60 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c  and the prior al
18b70 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
18b80 64 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33  d..** If sqlite3
18b90 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  _realloc() retur
18ba0 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  ns NULL, then th
18bb0 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  e prior allocati
18bc0 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65  on.** is not fre
18bd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  ed..**.** The me
18be0 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20 62 79  mory returned by
18bf0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
18c00 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
18c10 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c  alloc().** is al
18c20 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20  ways aligned to 
18c30 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79  at least an 8 by
18c40 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e  te boundary. {EN
18c50 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  D}.**.** The def
18c60 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ault implementat
18c70 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
18c80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
18c90 73 79 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74  system uses.** t
18ca0 68 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61  he malloc(), rea
18cb0 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28  lloc() and free(
18cc0 29 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68  ) provided by th
18cd0 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62  e standard C lib
18ce0 72 61 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32  rary..** {H17382
18cf0 7d 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51  } However, if SQ
18d00 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
18d10 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c   with the.** SQL
18d20 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d  ITE_MEMORY_SIZE=
18d30 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65  <i>NNN</i> C pre
18d40 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20  processor macro 
18d50 28 77 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69  (where <i>NNN</i
18d60 3e 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67  >.** is an integ
18d70 65 72 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  er), then SQLite
18d80 20 63 72 65 61 74 65 20 61 20 73 74 61 74 69 63   create a static
18d90 20 61 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61   array of at lea
18da0 73 74 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e  st.** <i>NNN</i>
18db0 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61   bytes in size a
18dc0 6e 64 20 75 73 65 73 20 74 68 61 74 20 61 72 72  nd uses that arr
18dd0 61 79 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  ay for all of it
18de0 73 20 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d  s dynamic.** mem
18df0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
18e00 65 65 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64  eeds. {END}  Add
18e10 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61  itional memory a
18e20 6c 6c 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73  llocator options
18e30 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64  .** may be added
18e40 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
18e50 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51  ses..**.** In SQ
18e60 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35  Lite version 3.5
18e70 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74  .0 and 3.5.1, it
18e80 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f   was possible to
18e90 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53   define.** the S
18ea0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
18eb0 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69  Y_ALLOCATION whi
18ec0 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74  ch would cause t
18ed0 68 65 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69  he built-in.** i
18ee0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
18ef0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
18f00 74 6f 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20  to be omitted.  
18f10 54 68 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a  That capability.
18f20 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  ** is no longer 
18f30 70 72 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20  provided.  Only 
18f40 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20  built-in memory 
18f50 61 6c 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62  allocators can b
18f60 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  e used..**.** Th
18f70 65 20 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74  e Windows OS int
18f80 65 72 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c  erface layer cal
18f90 6c 73 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d  ls.** the system
18fa0 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72   malloc() and fr
18fb0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 77 68  ee() directly wh
18fc0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a  en converting.**
18fd0 20 66 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65   filenames betwe
18fe0 65 6e 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63  en the UTF-8 enc
18ff0 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 53 51  oding used by SQ
19000 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74  Lite.** and what
19010 65 76 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e  ever filename en
19020 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 20 62  coding is used b
19030 79 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  y the particular
19040 20 57 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74   Windows.** inst
19050 61 6c 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72  allation.  Memor
19060 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
19070 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64  ors are detected
19080 2c 20 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72  , but.** they ar
19090 65 20 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20  e reported back 
190a0 61 73 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f  as [SQLITE_CANTO
190b0 50 45 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  PEN] or.** [SQLI
190c0 54 45 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72  TE_IOERR] rather
190d0 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f   than [SQLITE_NO
190e0 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  MEM]..**.** Requ
190f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
19100 37 33 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b  7303] [H17304] [
19110 48 31 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d  H17305] [H17306]
19120 20 5b 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31   [H17310] [H1731
19130 32 5d 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37  2] [H17315] [H17
19140 33 31 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d  318].** [H17321]
19150 20 5b 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32   [H17322] [H1732
19160 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  3].**.** The poi
19170 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74  nter arguments t
19180 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
19190 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
191a0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75  realloc()].** mu
191b0 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c  st be either NUL
191c0 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65  L or else pointe
191d0 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  rs obtained from
191e0 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f   a prior.** invo
191f0 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
19200 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
19210 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
19220 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a  ()] that have.**
19230 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65   not yet been re
19240 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  leased..**.** Th
19250 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
19260 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77  st not read or w
19270 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66  rite any part of
19280 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  .** a block of m
19290 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 20 68  emory after it h
192a0 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  as been released
192b0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74   using.** [sqlit
192c0 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73  e3_free()] or [s
192d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
192e0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
192f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d   void *sqlite3_m
19300 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
19310 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
19320 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69  ite3_realloc(voi
19330 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  d*, int);.SQLITE
19340 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
19350 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a  3_free(void*);..
19360 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
19370 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72  Memory Allocator
19380 20 53 74 61 74 69 73 74 69 63 73 20 7b 48 31 37   Statistics {H17
19390 33 37 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a  370} <S30210>.**
193a0 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69  .** SQLite provi
193b0 64 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e  des these two in
193c0 74 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70  terfaces for rep
193d0 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74  orting on the st
193e0 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  atus.** of the [
193f0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
19400 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ], [sqlite3_free
19410 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
19420 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20  3_realloc()].** 
19430 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20  routines, which 
19440 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69  form the built-i
19450 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  n memory allocat
19460 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
19470 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
19480 73 3a 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b  s:.** [H17371] [
19490 48 31 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d  H17373] [H17374]
194a0 20 5b 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c   [H17375].*/.SQL
194b0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
194c0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
194d0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b  mory_used(void);
194e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
194f0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
19500 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
19510 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67  er(int resetFlag
19520 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
19530 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f  EF: Pseudo-Rando
19540 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74  m Number Generat
19550 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30  or {H17390} <S20
19560 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  000>.**.** SQLit
19570 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67  e contains a hig
19580 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f  h-quality pseudo
19590 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67  -random number g
195a0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20  enerator (PRNG) 
195b0 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63  used to.** selec
195c0 74 20 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20  t random [ROWID 
195d0 7c 20 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69  | ROWIDs] when i
195e0 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63  nserting new rec
195f0 6f 72 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c  ords into a tabl
19600 65 20 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64  e that.** alread
19610 79 20 75 73 65 73 20 74 68 65 20 6c 61 72 67 65  y uses the large
19620 73 74 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57  st possible [ROW
19630 49 44 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69  ID].  The PRNG i
19640 73 20 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a  s also used for.
19650 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
19660 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e  random() and ran
19670 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75  domblob() SQL fu
19680 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69  nctions.  This i
19690 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a  nterface allows.
196a0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** applications 
196b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 61  to access the sa
196c0 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65  me PRNG for othe
196d0 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a  r purposes..**.*
196e0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  * A call to this
196f0 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20   routine stores 
19700 4e 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  N bytes of rando
19710 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65  mness into buffe
19720 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  r P..**.** The f
19730 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
19740 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65  outine is invoke
19750 64 20 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e  d (either intern
19760 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68  ally or by.** th
19770 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74  e application) t
19780 68 65 20 50 52 4e 47 20 69 73 20 73 65 65 64 65  he PRNG is seede
19790 64 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65  d using randomne
197a0 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  ss obtained.** f
197b0 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e  rom the xRandomn
197c0 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  ess method of th
197d0 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74  e default [sqlit
197e0 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a  e3_vfs] object..
197f0 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71  ** On all subseq
19800 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73  uent invocations
19810 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e  , the pseudo-ran
19820 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72  domness is gener
19830 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c  ated.** internal
19840 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72  ly and without r
19850 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b  ecourse to the [
19860 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61  sqlite3_vfs] xRa
19870 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68  ndomness.** meth
19880 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  od..**.** Requir
19890 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33  ements:.** [H173
198a0 39 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  92].*/.SQLITE_AP
198b0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
198c0 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c  andomness(int N,
198d0 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a   void *P);../*.*
198e0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70  * CAPI3REF: Comp
198f0 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69  ile-Time Authori
19900 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73  zation Callbacks
19910 20 7b 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30   {H12500} <S7010
19920 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
19930 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20  utine registers 
19940 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  a authorizer cal
19950 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72  lback with a par
19960 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61  ticular.** [data
19970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
19980 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68  , supplied in th
19990 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
199a0 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69  ..** The authori
199b0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
199c0 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73  invoked as SQL s
199d0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65  tatements are be
199e0 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20  ing compiled.** 
199f0 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  by [sqlite3_prep
19a00 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61  are()] or its va
19a10 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f  riants [sqlite3_
19a20 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a  prepare_v2()],.*
19a30 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
19a40 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c  re16()] and [sql
19a50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
19a60 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75  2()].  At variou
19a70 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69  s.** points duri
19a80 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69  ng the compilati
19a90 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c  on process, as l
19aa0 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20 63 72  ogic is being cr
19ab0 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66  eated.** to perf
19ac0 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69  orm various acti
19ad0 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69  ons, the authori
19ae0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
19af0 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65  invoked to.** se
19b00 65 20 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f  e if those actio
19b10 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20  ns are allowed. 
19b20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
19b30 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a  callback should.
19b40 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ** return [SQLIT
19b50 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74  E_OK] to allow t
19b60 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49  he action, [SQLI
19b70 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69  TE_IGNORE] to di
19b80 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70  sallow the.** sp
19b90 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75  ecific action bu
19ba0 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20  t allow the SQL 
19bb0 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e  statement to con
19bc0 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63  tinue to be.** c
19bd0 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c  ompiled, or [SQL
19be0 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75  ITE_DENY] to cau
19bf0 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 53 51  se the entire SQ
19c00 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  L statement to b
19c10 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69  e.** rejected wi
19c20 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66  th an error.  If
19c30 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
19c40 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
19c50 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74  .** any value ot
19c60 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45  her than [SQLITE
19c70 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54  _IGNORE], [SQLIT
19c80 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  E_OK], or [SQLIT
19c90 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20  E_DENY].** then 
19ca0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  the [sqlite3_pre
19cb0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
19cc0 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
19cd0 61 74 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20  at triggered.** 
19ce0 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77  the authorizer w
19cf0 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
19d00 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
19d10 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
19d20 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
19d30 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61  [SQLITE_OK], tha
19d40 74 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72  t means the oper
19d50 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74  ation.** request
19d60 65 64 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20  ed is ok.  When 
19d70 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
19d80 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e  urns [SQLITE_DEN
19d90 59 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  Y], the.** [sqli
19da0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
19db0 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20  ] or equivalent 
19dc0 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65  call that trigge
19dd0 72 65 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f  red the.** autho
19de0 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20  rizer will fail 
19df0 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
19e00 73 73 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67  ssage explaining
19e10 20 74 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20   that.** access 
19e20 69 73 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a  is denied. .**.*
19e30 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
19e40 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74  meter to the aut
19e50 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
19e60 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
19e70 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d  e third.** param
19e80 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
19e90 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
19ea0 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  er() interface. 
19eb0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
19ec0 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63  eter.** to the c
19ed0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e  allback is an in
19ee0 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f  teger [SQLITE_CO
19ef0 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65  PY | action code
19f00 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73  ] that specifies
19f10 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c  .** the particul
19f20 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20  ar action to be 
19f30 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20  authorized. The 
19f40 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 69  third through si
19f50 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  xth parameters.*
19f60 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
19f70 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69  k are zero-termi
19f80 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68  nated strings th
19f90 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74  at contain addit
19fa0 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73  ional.** details
19fb0 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f   about the actio
19fc0 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a  n to be authoriz
19fd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
19fe0 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20   action code is 
19ff0 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a  [SQLITE_READ].**
1a000 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63   and the callbac
1a010 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
1a020 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74  E_IGNORE] then t
1a030 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
1a040 73 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65  statement] state
1a050 6d 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63  ment is construc
1a060 74 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74  ted to substitut
1a070 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75  e.** a NULL valu
1a080 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  e in place of th
1a090 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
1a0a0 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a  hat would have.*
1a0b0 2a 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b  * been read if [
1a0c0 53 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62  SQLITE_OK] had b
1a0d0 65 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54  een returned.  T
1a0e0 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  he [SQLITE_IGNOR
1a0f0 45 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e  E].** return can
1a100 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79   be used to deny
1a110 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73   an untrusted us
1a120 65 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64  er access to ind
1a130 69 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d  ividual.** colum
1a140 6e 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a  ns of a table..*
1a150 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20  * If the action 
1a160 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f  code is [SQLITE_
1a170 44 45 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20  DELETE] and the 
1a180 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1a190 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  .** [SQLITE_IGNO
1a1a0 52 45 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45  RE] then the [DE
1a1b0 4c 45 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20  LETE] operation 
1a1c0 70 72 6f 63 65 65 64 73 20 62 75 74 20 74 68 65  proceeds but the
1a1d0 0a 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70  .** [truncate op
1a1e0 74 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64  timization] is d
1a1f0 69 73 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20  isabled and all 
1a200 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64  rows are deleted
1a210 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a   individually..*
1a220 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a  *.** An authoriz
1a230 65 72 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  er is used when 
1a240 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1a250 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a   | preparing].**
1a260 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
1a270 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65  from an untruste
1a280 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73  d source, to ens
1a290 75 72 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ure that the SQL
1a2a0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64   statements.** d
1a2b0 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63  o not try to acc
1a2c0 65 73 73 20 64 61 74 61 20 74 68 65 79 20 61 72  ess data they ar
1a2d0 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  e not allowed to
1a2e0 20 73 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68   see, or that th
1a2f0 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79  ey do not.** try
1a300 20 74 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69   to execute mali
1a310 63 69 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73  cious statements
1a320 20 74 68 61 74 20 64 61 6d 61 67 65 20 74 68 65   that damage the
1a330 20 64 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a   database.  For.
1a340 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61  ** example, an a
1a350 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61  pplication may a
1a360 6c 6c 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65  llow a user to e
1a370 6e 74 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a  nter arbitrary.*
1a380 2a 20 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f  * SQL queries fo
1a390 72 20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20  r evaluation by 
1a3a0 61 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74  a database.  But
1a3b0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
1a3c0 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e   does.** not wan
1a3d0 74 20 74 68 65 20 75 73 65 72 20 74 6f 20 62 65  t the user to be
1a3e0 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72   able to make ar
1a3f0 62 69 74 72 61 72 79 20 63 68 61 6e 67 65 73 20  bitrary changes 
1a400 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
1a410 73 65 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a  se.  An authoriz
1a420 65 72 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65  er could then be
1a430 20 70 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68   put in place wh
1a440 69 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d  ile the.** user-
1a450 65 6e 74 65 72 65 64 20 53 51 4c 20 69 73 20 62  entered SQL is b
1a460 65 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  eing [sqlite3_pr
1a470 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 65 64  epare | prepared
1a480 5d 20 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c  ] that.** disall
1a490 6f 77 73 20 65 76 65 72 79 74 68 69 6e 67 20 65  ows everything e
1a4a0 78 63 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73  xcept [SELECT] s
1a4b0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
1a4c0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
1a4d0 61 74 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65  at need to proce
1a4e0 73 73 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72  ss SQL from untr
1a4f0 75 73 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a  usted sources.**
1a500 20 6d 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73   might also cons
1a510 69 64 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65  ider lowering re
1a520 73 6f 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73  source limits us
1a530 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  ing [sqlite3_lim
1a540 69 74 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d  it()].** and lim
1a550 69 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 73  iting database s
1a560 69 7a 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d  ize using the [m
1a570 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b  ax_page_count] [
1a580 50 52 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64  PRAGMA].** in ad
1a590 64 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20  dition to using 
1a5a0 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a  an authorizer..*
1a5b0 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67  *.** Only a sing
1a5c0 6c 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  le authorizer ca
1a5d0 6e 20 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e  n be in place on
1a5e0 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
1a5f0 65 63 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74  ection.** at a t
1a600 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20  ime.  Each call 
1a610 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  to sqlite3_set_a
1a620 75 74 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69  uthorizer overri
1a630 64 65 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69  des the.** previ
1a640 6f 75 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62  ous call.  Disab
1a650 6c 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  le the authorize
1a660 72 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20  r by installing 
1a670 61 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e  a NULL callback.
1a680 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a  .** The authoriz
1a690 65 72 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  er is disabled b
1a6a0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a  y default..**.**
1a6b0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
1a6c0 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f  callback must no
1a6d0 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68  t do anything th
1a6e0 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a  at will modify.*
1a6f0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
1a700 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69  onnection that i
1a710 6e 76 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f  nvoked the autho
1a720 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a  rizer callback..
1a730 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
1a740 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a750 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
1a760 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
1a770 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
1a780 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1a790 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
1a7a0 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
1a7b0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
1a7c0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71  ..**.** When [sq
1a7d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a7e0 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70  ()] is used to p
1a7f0 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65  repare a stateme
1a800 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65  nt, the.** state
1a810 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65  ment might be re
1a820 2d 70 72 65 70 61 72 65 64 20 64 75 72 69 6e 67  -prepared during
1a830 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a840 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73  ] due to a .** s
1a850 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48  chema change.  H
1a860 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63  ence, the applic
1a870 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73  ation should ens
1a880 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ure that the.** 
1a890 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a  correct authoriz
1a8a0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61  er callback rema
1a8b0 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72  ins in place dur
1a8c0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ing the [sqlite3
1a8d0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _step()]..**.** 
1a8e0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 75  Note that the au
1a8f0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1a900 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c  k is invoked onl
1a910 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c  y during.** [sql
1a920 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
1a930 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e  or its variants.
1a940 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20    Authorization 
1a950 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72  is not.** perfor
1a960 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65  med during state
1a970 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20  ment evaluation 
1a980 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  in [sqlite3_step
1a990 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61  ()], unless.** a
1a9a0 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65 20  s stated in the 
1a9b0 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61  previous paragra
1a9c0 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ph, sqlite3_step
1a9d0 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71  () invokes.** sq
1a9e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a9f0 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65 20  () to reprepare 
1aa00 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65  a statement afte
1aa10 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  r a schema chang
1aa20 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e..**.** Require
1aa30 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30  ments:.** [H1250
1aa40 31 5d 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 32  1] [H12502] [H12
1aa50 35 30 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48  503] [H12504] [H
1aa60 31 32 35 30 35 5d 20 5b 48 31 32 35 30 36 5d 20  12505] [H12506] 
1aa70 5b 48 31 32 35 30 37 5d 20 5b 48 31 32 35 31 30  [H12507] [H12510
1aa80 5d 0a 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48  ].** [H12511] [H
1aa90 31 32 35 31 32 5d 20 5b 48 31 32 35 32 30 5d 20  12512] [H12520] 
1aaa0 5b 48 31 32 35 32 31 5d 20 5b 48 31 32 35 32 32  [H12521] [H12522
1aab0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
1aac0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  int sqlite3_set_
1aad0 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71  authorizer(.  sq
1aae0 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a  lite3*,.  int (*
1aaf0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
1ab00 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1ab10 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
1ab20 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1ab30 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72  ),.  void *pUser
1ab40 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Data.);../*.** C
1ab50 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
1ab60 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73  zer Return Codes
1ab70 20 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30   {H12590} <H1250
1ab80 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  0>.**.** The [sq
1ab90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1aba0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65  izer | authorize
1abb0 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
1abc0 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74  ion] must.** ret
1abd0 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49  urn either [SQLI
1abe0 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66  TE_OK] or one of
1abf0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74   these two const
1ac00 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  ants in order.**
1ac10 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74   to signal SQLit
1ac20 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
1ac30 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70   the action is p
1ac40 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74  ermitted.  See t
1ac50 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  he.** [sqlite3_s
1ac60 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20  et_authorizer | 
1ac70 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d  authorizer docum
1ac80 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64  entation] for ad
1ac90 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f  ditional.** info
1aca0 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66  rmation..*/.#def
1acb0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20  ine SQLITE_DENY 
1acc0 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74    1   /* Abort t
1acd0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1ace0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a   with an error *
1acf0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1ad00 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44  _IGNORE 2   /* D
1ad10 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73  on't allow acces
1ad20 73 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e  s, but don't gen
1ad30 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a  erate an error *
1ad40 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
1ad50 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63  F: Authorizer Ac
1ad60 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35  tion Codes {H125
1ad70 35 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a  50} <H12500>.**.
1ad80 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
1ad90 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29  set_authorizer()
1ada0 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ] interface regi
1adb0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b  sters a callback
1adc0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61   function.** tha
1add0 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20  t is invoked to 
1ade0 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69  authorize certai
1adf0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1ae00 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a  actions.  The.**
1ae10 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1ae20 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  r to the callbac
1ae30 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  k is an integer 
1ae40 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66  code that specif
1ae50 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69  ies.** what acti
1ae60 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68  on is being auth
1ae70 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61  orized.  These a
1ae80 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61  re the integer a
1ae90 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74  ction codes that
1aea0 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a  .** the authoriz
1aeb0 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  er callback may 
1aec0 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a  be passed..**.**
1aed0 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f   These action co
1aee0 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66  de values signif
1aef0 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f  y what kind of o
1af00 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62  peration is to b
1af10 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e  e.** authorized.
1af20 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74    The 3rd and 4t
1af30 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
1af40 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
1af50 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75  n.** callback fu
1af60 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70  nction will be p
1af70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c  arameters or NUL
1af80 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  L depending on w
1af90 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a  hich of these.**
1afa0 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61   codes is used a
1afb0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  s the second par
1afc0 61 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68  ameter.  The 5th
1afd0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1afe0 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20  e.** authorizer 
1aff0 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20  callback is the 
1b000 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
1b010 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74  base ("main", "t
1b020 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69  emp",.** etc.) i
1b030 66 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54  f applicable.  T
1b040 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72  he 6th parameter
1b050 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a   to the authoriz
1b060 65 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69  er callback.** i
1b070 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1b080 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69  e inner-most tri
1b090 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61  gger or view tha
1b0a0 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  t is responsible
1b0b0 20 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65   for.** the acce
1b0c0 73 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55  ss attempt or NU
1b0d0 4c 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73  LL if this acces
1b0e0 73 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72  s attempt is dir
1b0f0 65 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f  ectly from.** to
1b100 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65  p-level SQL code
1b110 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
1b120 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31  ents:.** [H12551
1b130 5d 20 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 35  ] [H12552] [H125
1b140 35 33 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a  53] [H12554].*/.
1b150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1b160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64  ************ 3rd
1b180 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74   ************ 4t
1b190 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  h ***********/.#
1b1a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1b1b0 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20 20  EATE_INDEX      
1b1c0 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 78      1   /* Index
1b1d0 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65   Name      Table
1b1e0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
1b1f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
1b200 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20 20 20  ATE_TABLE       
1b210 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 20     2   /* Table 
1b220 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b230 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b240 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
1b250 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20  TE_TEMP_INDEX   
1b260 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e    3   /* Index N
1b270 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1b280 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b290 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
1b2a0 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20  E_TEMP_TABLE    
1b2b0 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61   4   /* Table Na
1b2c0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b2d0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b2e0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1b2f0 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20  _TEMP_TRIGGER   
1b300 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e  5   /* Trigger N
1b310 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  ame    Table Nam
1b320 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b330 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
1b340 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20 36  TEMP_VIEW      6
1b350 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20     /* View Name 
1b360 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b370 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b380 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
1b390 52 49 47 47 45 52 20 20 20 20 20 20 20 20 37 20  RIGGER        7 
1b3a0 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1b3b0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1b3c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b3d0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49  SQLITE_CREATE_VI
1b3e0 45 57 20 20 20 20 20 20 20 20 20 20 20 38 20 20  EW           8  
1b3f0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1b400 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b410 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b420 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20 20 20  QLITE_DELETE    
1b430 20 20 20 20 20 20 20 20 20 20 20 20 39 20 20 20              9   
1b440 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1b450 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b460 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b470 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20  LITE_DROP_INDEX 
1b480 20 20 20 20 20 20 20 20 20 20 31 30 20 20 20 2f            10   /
1b490 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1b4a0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b4b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b4c0 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20  ITE_DROP_TABLE  
1b4d0 20 20 20 20 20 20 20 20 20 31 31 20 20 20 2f 2a           11   /*
1b4e0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b4f0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b500 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b510 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
1b520 45 58 20 20 20 20 20 20 31 32 20 20 20 2f 2a 20  EX      12   /* 
1b530 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1b540 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b550 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b560 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c  E_DROP_TEMP_TABL
1b570 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 54  E      13   /* T
1b580 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b590 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b5a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b5b0 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47  _DROP_TEMP_TRIGG
1b5c0 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72  ER    14   /* Tr
1b5d0 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
1b5e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b5f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b600 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20  DROP_TEMP_VIEW  
1b610 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65       15   /* Vie
1b620 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
1b630 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b640 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1b650 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20 20  ROP_TRIGGER     
1b660 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67      16   /* Trig
1b670 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1b680 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1b690 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
1b6a0 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  OP_VIEW         
1b6b0 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77 20     17   /* View 
1b6c0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1b6d0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b6e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53  efine SQLITE_INS
1b6f0 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ERT             
1b700 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 20    18   /* Table 
1b710 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b720 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b730 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 47  fine SQLITE_PRAG
1b740 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
1b750 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 20   19   /* Pragma 
1b760 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61 72 67  Name     1st arg
1b770 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   or NULL */.#def
1b780 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 20  ine SQLITE_READ 
1b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b7a0 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  20   /* Table Na
1b7b0 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e  me      Column N
1b7c0 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  ame     */.#defi
1b7d0 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54  ne SQLITE_SELECT
1b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
1b7f0 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20  1   /* NULL     
1b800 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20         NULL     
1b810 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1b820 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43  e SQLITE_TRANSAC
1b830 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 32 32  TION          22
1b840 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20     /* Operation 
1b850 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b860 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b870 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 20   SQLITE_UPDATE  
1b880 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 20               23 
1b890 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1b8a0 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65       Column Name
1b8b0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b8c0 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 20 20  SQLITE_ATTACH   
1b8d0 20 20 20 20 20 20 20 20 20 20 20 20 32 34 20 20              24  
1b8e0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 20   /* Filename    
1b8f0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b900 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b910 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20 20 20  QLITE_DETACH    
1b920 20 20 20 20 20 20 20 20 20 20 20 32 35 20 20 20             25   
1b930 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65  /* Database Name
1b940 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b950 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b960 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
1b970 20 20 20 20 20 20 20 20 20 20 32 36 20 20 20 2f            26   /
1b980 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20  * Database Name 
1b990 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b9a0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b9b0 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20 20 20  ITE_REINDEX     
1b9c0 20 20 20 20 20 20 20 20 20 32 37 20 20 20 2f 2a           27   /*
1b9d0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1b9e0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b9f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1ba00 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20  TE_ANALYZE      
1ba10 20 20 20 20 20 20 20 20 32 38 20 20 20 2f 2a 20          28   /* 
1ba20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1ba30 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1ba40 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1ba50 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 20  E_CREATE_VTABLE 
1ba60 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a 20 54         29   /* T
1ba70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d  able Name      M
1ba80 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a  odule Name     *
1ba90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1baa0 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20  _DROP_VTABLE    
1bab0 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61        30   /* Ta
1bac0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f  ble Name      Mo
1bad0 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f  dule Name     */
1bae0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1baf0 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20  FUNCTION        
1bb00 20 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c       31   /* NUL
1bb10 4c 20 20 20 20 20 20 20 20 20 20 20 20 46 75 6e  L            Fun
1bb20 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a  ction Name   */.
1bb30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
1bb40 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20  AVEPOINT        
1bb50 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72      32   /* Oper
1bb60 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65  ation       Save
1bb70 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23  point Name  */.#
1bb80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
1bb90 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  PY              
1bba0 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f      0   /* No lo
1bbb0 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a  nger used */../*
1bbc0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72  .** CAPI3REF: Tr
1bbd0 61 63 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c  acing And Profil
1bbe0 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48  ing Functions {H
1bbf0 31 32 32 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a  12280} <S60400>.
1bc00 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
1bc10 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
1bc20 69 6e 65 73 20 72 65 67 69 73 74 65 72 20 63 61  ines register ca
1bc30 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73  llback functions
1bc40 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
1bc50 64 20 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67  d for.** tracing
1bc60 20 61 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 74   and profiling t
1bc70 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
1bc80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  SQL statements..
1bc90 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61  **.** The callba
1bca0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  ck function regi
1bcb0 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 65  stered by sqlite
1bcc0 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e 76  3_trace() is inv
1bcd0 6f 6b 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f  oked at.** vario
1bce0 75 73 20 74 69 6d 65 73 20 77 68 65 6e 20 61 6e  us times when an
1bcf0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
1bd00 73 20 62 65 69 6e 67 20 72 75 6e 20 62 79 20 5b  s being run by [
1bd10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e  sqlite3_step()].
1bd20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
1bd30 20 72 65 74 75 72 6e 73 20 61 20 55 54 46 2d 38   returns a UTF-8
1bd40 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68   rendering of th
1bd50 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1bd60 74 65 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 73  text.** as the s
1bd70 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 62  tatement first b
1bd80 65 67 69 6e 73 20 65 78 65 63 75 74 69 6e 67 2e  egins executing.
1bd90 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c    Additional cal
1bda0 6c 62 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20  lbacks occur.** 
1bdb0 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72 65  as each triggere
1bdc0 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20  d subprogram is 
1bdd0 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63 61  entered.  The ca
1bde0 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67  llbacks for trig
1bdf0 67 65 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20  gers.** contain 
1be00 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d  a UTF-8 SQL comm
1be10 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69 66  ent that identif
1be20 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72 2e  ies the trigger.
1be30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  .**.** The callb
1be40 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67  ack function reg
1be50 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74  istered by sqlit
1be60 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 20  e3_profile() is 
1be70 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61  invoked.** as ea
1be80 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ch SQL statement
1be90 20 66 69 6e 69 73 68 65 73 2e 20 20 54 68 65 20   finishes.  The 
1bea0 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  profile callback
1beb0 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
1bec0 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d   original statem
1bed0 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 6e 20  ent text and an 
1bee0 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c  estimate of wall
1bef0 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f  -clock time.** o
1bf00 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20  f how long that 
1bf10 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74  statement took t
1bf20 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  o run..**.** Req
1bf30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
1bf40 31 32 32 38 31 5d 20 5b 48 31 32 32 38 32 5d 20  12281] [H12282] 
1bf50 5b 48 31 32 32 38 33 5d 20 5b 48 31 32 32 38 34  [H12283] [H12284
1bf60 5d 20 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 32  ] [H12285] [H122
1bf70 38 37 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 31  87] [H12288] [H1
1bf80 32 32 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30  2289].** [H12290
1bf90 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
1bfa0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
1bfb0 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  TAL void *sqlite
1bfc0 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 2a  3_trace(sqlite3*
1bfd0 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28  , void(*xTrace)(
1bfe0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
1bff0 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  *), void*);.SQLI
1c000 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
1c010 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20  PERIMENTAL void 
1c020 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  *sqlite3_profile
1c030 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f  (sqlite3*,.   vo
1c040 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f  id(*xProfile)(vo
1c050 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
1c060 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 2c  sqlite3_uint64),
1c070 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
1c080 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79 20  CAPI3REF: Query 
1c090 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63  Progress Callbac
1c0a0 6b 73 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 30  ks {H12910} <S60
1c0b0 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  400>.**.** This 
1c0c0 72 6f 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72  routine configur
1c0d0 65 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  es a callback fu
1c0e0 6e 63 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20  nction - the.** 
1c0f0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
1c100 6b 20 2d 20 74 68 61 74 20 69 73 20 69 6e 76 6f  k - that is invo
1c110 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79  ked periodically
1c120 20 64 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20   during long.** 
1c130 72 75 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f  running calls to
1c140 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
1c150 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
1c160 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  ()] and.** [sqli
1c170 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
1c180 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 73  .  An example us
1c190 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e  e for this.** in
1c1a0 74 65 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65  terface is to ke
1c1b0 65 70 20 61 20 47 55 49 20 75 70 64 61 74 65 64  ep a GUI updated
1c1c0 20 64 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20   during a large 
1c1d0 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  query..**.** If 
1c1e0 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c1f0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c200 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72  n-zero, the oper
1c210 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65  ation is.** inte
1c220 72 72 75 70 74 65 64 2e 20 20 54 68 69 73 20 66  rrupted.  This f
1c230 65 61 74 75 72 65 20 63 61 6e 20 62 65 20 75 73  eature can be us
1c240 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1c250 61 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75  a.** "Cancel" bu
1c260 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72  tton on a GUI pr
1c270 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f  ogress dialog bo
1c280 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  x..**.** The pro
1c290 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 75  gress handler mu
1c2a0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
1c2b0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
1c2c0 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
1c2d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
1c2e0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
1c2f0 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72  progress handler
1c300 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
1c310 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1c320 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
1c330 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
1c340 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
1c350 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c360 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
1c370 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
1c380 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
1c390 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ph..**.** Requir
1c3a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39  ements:.** [H129
1c3b0 31 31 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 31  11] [H12912] [H1
1c3c0 32 39 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b  2913] [H12914] [
1c3d0 48 31 32 39 31 35 5d 20 5b 48 31 32 39 31 36 5d  H12915] [H12916]
1c3e0 20 5b 48 31 32 39 31 37 5d 20 5b 48 31 32 39 31   [H12917] [H1291
1c3f0 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  8].**.*/.SQLITE_
1c400 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1c410 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
1c420 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  r(sqlite3*, int,
1c430 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20   int(*)(void*), 
1c440 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
1c450 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67  API3REF: Opening
1c460 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 20   A New Database 
1c470 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37  Connection {H127
1c480 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a  00} <S40200>.**.
1c490 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
1c4a0 73 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65  s open an SQLite
1c4b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
1c4c0 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 67 69 76  hose name is giv
1c4d0 65 6e 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c  en by the.** fil
1c4e0 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20  ename argument. 
1c4f0 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  The filename arg
1c500 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72  ument is interpr
1c510 65 74 65 64 20 61 73 20 55 54 46 2d 38 20 66 6f  eted as UTF-8 fo
1c520 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  r.** sqlite3_ope
1c530 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
1c540 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 73  open_v2() and as
1c550 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e   UTF-16 in the n
1c560 61 74 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72  ative byte.** or
1c570 64 65 72 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  der for sqlite3_
1c580 6f 70 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 74  open16(). A [dat
1c590 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1c5a0 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75 61  ] handle is usua
1c5b0 6c 6c 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  lly.** returned 
1c5c0 69 6e 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 69  in *ppDb, even i
1c5d0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1c5e0 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 63  s.  The only exc
1c5f0 65 70 74 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a  eption is that.*
1c600 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 75  * if SQLite is u
1c610 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
1c620 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64  e memory to hold
1c630 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
1c640 62 6a 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c  bject,.** a NULL
1c650 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e   will be written
1c660 20 69 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 74   into *ppDb inst
1c670 65 61 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 72  ead of a pointer
1c680 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
1c690 5d 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20  ].** object. If 
1c6a0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
1c6b0 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63  opened (and/or c
1c6c0 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 66  reated) successf
1c6d0 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53  ully, then.** [S
1c6e0 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74  QLITE_OK] is ret
1c6f0 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73  urned.  Otherwis
1c700 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  e an [error code
1c710 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  ] is returned.  
1c720 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  The.** [sqlite3_
1c730 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71  errmsg()] or [sq
1c740 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
1c750 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62  ] routines can b
1c760 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  e used to obtain
1c770 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c  .** an English l
1c780 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70 74  anguage descript
1c790 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ion of the error
1c7a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
1c7b0 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ult encoding for
1c7c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69   the database wi
1c7d0 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a  ll be UTF-8 if.*
1c7e0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
1c7f0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   or sqlite3_open
1c800 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20  _v2() is called 
1c810 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e  and.** UTF-16 in
1c820 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
1c830 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 65   order if sqlite
1c840 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73  3_open16() is us
1c850 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ed..**.** Whethe
1c860 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
1c870 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74  r occurs when it
1c880 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f   is opened, reso
1c890 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61  urces.** associa
1c8a0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
1c8b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c8c0 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64  n] handle should
1c8d0 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a   be released by.
1c8e0 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  ** passing it to
1c8f0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
1c900 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f  )] when it is no
1c910 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64   longer required
1c920 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
1c930 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e  te3_open_v2() in
1c940 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69  terface works li
1c950 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ke sqlite3_open(
1c960 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  ).** except that
1c970 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f 20   it accepts two 
1c980 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d  additional param
1c990 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69  eters for additi
1c9a0 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20  onal control.** 
1c9b0 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74  over the new dat
1c9c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1c9d0 2e 20 20 54 68 65 20 66 6c 61 67 73 20 70 61 72  .  The flags par
1c9e0 61 6d 65 74 65 72 20 63 61 6e 20 74 61 6b 65 20  ameter can take 
1c9f0 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  one of.** the fo
1ca00 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 76 61  llowing three va
1ca10 6c 75 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79  lues, optionally
1ca20 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 74   combined with t
1ca30 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  he .** [SQLITE_O
1ca40 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53  PEN_NOMUTEX], [S
1ca50 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
1ca60 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f  UTEX], [SQLITE_O
1ca70 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d  PEN_SHAREDCACHE]
1ca80 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 53 51 4c  ,.** and/or [SQL
1ca90 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45  ITE_OPEN_PRIVATE
1caa0 43 41 43 48 45 5d 20 66 6c 61 67 73 3a 0a 2a 2a  CACHE] flags:.**
1cab0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
1cac0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
1cad0 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c  DONLY]</dt>.** <
1cae0 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20  dd>The database 
1caf0 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61  is opened in rea
1cb00 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66  d-only mode.  If
1cb10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 6f   the database do
1cb20 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  es not.** alread
1cb30 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f  y exist, an erro
1cb40 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f  r is returned.</
1cb50 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53  dd>.**.** <dt>[S
1cb60 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
1cb70 52 49 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RITE]</dt>.** <d
1cb80 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69  d>The database i
1cb90 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
1cba0 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
1cbb0 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72   if possible, or
1cbc0 20 72 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79   reading.** only
1cbd0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
1cbe0 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20  write protected 
1cbf0 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  by the operating
1cc00 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74   system.  In eit
1cc10 68 65 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  her.** case the 
1cc20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c  database must al
1cc30 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68  ready exist, oth
1cc40 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20  erwise an error 
1cc50 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64  is returned.</dd
1cc60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c  >.**.** <dt>[SQL
1cc70 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1cc80 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50  TE] | [SQLITE_OP
1cc90 45 4e 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a  EN_CREATE]</dt>.
1cca0 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62  ** <dd>The datab
1ccb0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
1ccc0 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  r reading and wr
1ccd0 69 74 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72  iting, and is cr
1cce0 65 61 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69  eates it if.** i
1ccf0 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61  t does not alrea
1cd00 64 79 20 65 78 69 73 74 2e 20 54 68 69 73 20 69  dy exist. This i
1cd10 73 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 74  s the behavior t
1cd20 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 75 73  hat is always us
1cd30 65 64 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  ed for.** sqlite
1cd40 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
1cd50 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f  ite3_open16().</
1cd60 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a  dd>.** </dl>.**.
1cd70 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61  ** If the 3rd pa
1cd80 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1cd90 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
1cda0 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a  not one of the.*
1cdb0 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73  * combinations s
1cdc0 68 6f 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e  hown above or on
1cdd0 65 20 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61  e of the combina
1cde0 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76  tions shown abov
1cdf0 65 20 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69  e combined.** wi
1ce00 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  th the [SQLITE_O
1ce10 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53  PEN_NOMUTEX], [S
1ce20 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
1ce30 55 54 45 58 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  UTEX],.** [SQLIT
1ce40 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
1ce50 48 45 5d 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49  HE] and/or [SQLI
1ce60 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
1ce70 43 48 45 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 74  CHE] flags,.** t
1ce80 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
1ce90 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
1cea0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 4c  *.** If the [SQL
1ceb0 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
1cec0 5d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  ] flag is set, t
1ced0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1cee0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f   connection.** o
1cef0 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74  pens in the mult
1cf00 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64  i-thread [thread
1cf10 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e  ing mode] as lon
1cf20 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d  g as the single-
1cf30 74 68 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 68  thread.** mode h
1cf40 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 20  as not been set 
1cf50 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  at compile-time 
1cf60 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20  or start-time.  
1cf70 49 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  If the.** [SQLIT
1cf80 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
1cf90 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68  ] flag is set th
1cfa0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
1cfb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 73  connection opens
1cfc0 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 72 69 61  .** in the seria
1cfd0 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67  lized [threading
1cfe0 20 6d 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 69   mode] unless si
1cff0 6e 67 6c 65 2d 74 68 72 65 61 64 20 77 61 73 0a  ngle-thread was.
1d000 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  ** previously se
1d010 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c  lected at compil
1d020 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d  e-time or start-
1d030 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 5b 53 51  time..** The [SQ
1d040 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44  LITE_OPEN_SHARED
1d050 43 41 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73  CACHE] flag caus
1d060 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
1d070 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65  connection to be
1d080 0a 2a 2a 20 65 6c 69 67 69 62 6c 65 20 74 6f 20  .** eligible to 
1d090 75 73 65 20 5b 73 68 61 72 65 64 20 63 61 63 68  use [shared cach
1d0a0 65 20 6d 6f 64 65 5d 2c 20 72 65 67 61 72 64 6c  e mode], regardl
1d0b0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
1d0c0 72 20 6e 6f 74 20 73 68 61 72 65 64 0a 2a 2a 20  r not shared.** 
1d0d0 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
1d0e0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1d0f0 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61  enable_shared_ca
1d100 63 68 65 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20  che()].  The.** 
1d110 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49  [SQLITE_OPEN_PRI
1d120 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 67 20  VATECACHE] flag 
1d130 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 62  causes the datab
1d140 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
1d150 6f 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 69 63 69  o not.** partici
1d160 70 61 74 65 20 69 6e 20 5b 73 68 61 72 65 64 20  pate in [shared 
1d170 63 61 63 68 65 20 6d 6f 64 65 5d 20 65 76 65 6e  cache mode] even
1d180 20 69 66 20 69 74 20 69 73 20 65 6e 61 62 6c 65   if it is enable
1d190 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1d1a0 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  filename is ":me
1d1b0 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70  mory:", then a p
1d1c0 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72  rivate, temporar
1d1d0 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  y in-memory data
1d1e0 62 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74  base.** is creat
1d1f0 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65  ed for the conne
1d200 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d  ction.  This in-
1d210 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
1d220 77 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e  will vanish when
1d230 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
1d240 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
1d250 6c 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 76  losed.  Future v
1d260 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
1d270 65 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20  e might.** make 
1d280 75 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61  use of additiona
1d290 6c 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61  l special filena
1d2a0 6d 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77  mes that begin w
1d2b0 69 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 72  ith the ":" char
1d2c0 61 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20  acter..** It is 
1d2d0 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74  recommended that
1d2e0 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
1d2f0 20 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c   filename actual
1d300 6c 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69  ly does begin wi
1d310 74 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72  th.** a ":" char
1d320 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64  acter you should
1d330 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65   prefix the file
1d340 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68  name with a path
1d350 6e 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20  name such as.** 
1d360 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d  "./" to avoid am
1d370 62 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49  biguity..**.** I
1d380 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69  f the filename i
1d390 73 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  s an empty strin
1d3a0 67 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74  g, then a privat
1d3b0 65 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20  e, temporary.** 
1d3c0 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65  on-disk database
1d3d0 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64   will be created
1d3e0 2e 20 20 54 68 69 73 20 70 72 69 76 61 74 65 20  .  This private 
1d3f0 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
1d400 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
1d410 79 20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f  y deleted as soo
1d420 6e 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73  n as the databas
1d430 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
1d440 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  closed..**.** Th
1d450 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
1d460 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70  er to sqlite3_op
1d470 65 6e 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e  en_v2() is the n
1d480 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73  ame of the.** [s
1d490 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1d4a0 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ct that defines 
1d4b0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
1d4c0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74  stem interface t
1d4d0 68 61 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64  hat.** the new d
1d4e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d4f0 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20  on should use.  
1d500 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  If the fourth pa
1d510 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20  rameter is.** a 
1d520 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65  NULL pointer the
1d530 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  n the default [s
1d540 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1d550 63 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a  ct is used..**.*
1d560 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e  * <b>Note to Win
1d570 64 6f 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20  dows users:</b> 
1d580 20 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73   The encoding us
1d590 65 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e  ed for the filen
1d5a0 61 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ame argument.** 
1d5b0 6f 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  of sqlite3_open(
1d5c0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
1d5d0 65 6e 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20  en_v2() must be 
1d5e0 55 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65  UTF-8, not whate
1d5f0 76 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20  ver.** codepage 
1d600 69 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66  is currently def
1d610 69 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73  ined.  Filenames
1d620 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65   containing inte
1d630 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61  rnational.** cha
1d640 72 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20  racters must be 
1d650 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46  converted to UTF
1d660 2d 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73  -8 prior to pass
1d670 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a  ing them into.**
1d680 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
1d690 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
1d6a0 76 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  v2()..**.** Requ
1d6b0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1d6c0 32 37 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b  2701] [H12702] [
1d6d0 48 31 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d  H12703] [H12704]
1d6e0 20 5b 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30   [H12706] [H1270
1d6f0 37 5d 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32  7] [H12709] [H12
1d700 37 31 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d  711].** [H12712]
1d710 20 5b 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31   [H12713] [H1271
1d720 34 5d 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32  4] [H12717] [H12
1d730 37 31 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48  719] [H12721] [H
1d740 31 32 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12723].*/.SQLITE
1d750 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1d760 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  _open(.  const c
1d770 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
1d780 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
1d790 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
1d7a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
1d7b0 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b          /* OU
1d7c0 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
1d7d0 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  dle */.);.SQLITE
1d7e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1d7f0 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
1d800 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
1d810 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
1d820 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
1d830 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a   */.  sqlite3 **
1d840 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a  ppDb          /*
1d850 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20   OUT: SQLite db 
1d860 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c  handle */.);.SQL
1d870 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1d880 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
1d890 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
1d8a0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
1d8b0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
1d8c0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
1d8d0 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
1d8e0 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
1d8f0 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
1d900 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
1d910 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1d920 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1d930 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
1d940 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
1d950 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a  le to use */.);.
1d960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1d970 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64   Error Codes And
1d980 20 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30   Messages {H1280
1d990 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a  0} <S60200>.**.*
1d9a0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72  * The sqlite3_er
1d9b0 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63  rcode() interfac
1d9c0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  e returns the nu
1d9d0 6d 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f  meric [result co
1d9e0 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e  de] or.** [exten
1d9f0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d  ded result code]
1da00 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65   for the most re
1da10 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69  cent failed sqli
1da20 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a  te3_* API call.*
1da30 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1da40 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  h a [database co
1da50 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20  nnection]. If a 
1da60 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66  prior API call f
1da70 61 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65  ailed.** but the
1da80 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49   most recent API
1da90 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c   call succeeded,
1daa0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
1dab0 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  e from.** sqlite
1dac0 33 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75  3_errcode() is u
1dad0 6e 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73  ndefined.  The s
1dae0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
1daf0 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74  errcode().** int
1db00 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61  erface is the sa
1db10 6d 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69  me except that i
1db20 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
1db30 20 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64   the .** [extend
1db40 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
1db50 65 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64  even when extend
1db60 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
1db70 61 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e  are.** disabled.
1db80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1db90 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20  e3_errmsg() and 
1dba0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1dbb0 28 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73  () return Englis
1dbc0 68 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65  h-language.** te
1dbd0 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65  xt that describe
1dbe0 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20  s the error, as 
1dbf0 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20  either UTF-8 or 
1dc00 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76  UTF-16 respectiv
1dc10 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ely..** Memory t
1dc20 6f 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72  o hold the error
1dc30 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20   message string 
1dc40 69 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72  is managed inter
1dc50 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70  nally..** The ap
1dc60 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e  plication does n
1dc70 6f 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79  ot need to worry
1dc80 20 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74   about freeing t
1dc90 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f  he result..** Ho
1dca0 77 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72  wever, the error
1dcb0 20 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65   string might be
1dcc0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20   overwritten or 
1dcd0 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  deallocated by.*
1dce0 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  * subsequent cal
1dcf0 6c 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69  ls to other SQLi
1dd00 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e  te interface fun
1dd10 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68  ctions..**.** Wh
1dd20 65 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65  en the serialize
1dd30 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  d [threading mod
1dd40 65 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74  e] is in use, it
1dd50 20 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a   might be the.**
1dd60 20 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63   case that a sec
1dd70 6f 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ond error occurs
1dd80 20 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74   on a separate t
1dd90 68 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e  hread in between
1dda0 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20  .** the time of 
1ddb0 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
1ddc0 61 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  and the call to 
1ddd0 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73  these interfaces
1dde0 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68  ..** When that h
1ddf0 61 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f  appens, the seco
1de00 6e 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  nd error will be
1de10 20 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20   reported since 
1de20 74 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61  these.** interfa
1de30 63 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72  ces always repor
1de40 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  t the most recen
1de50 74 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76  t result.  To av
1de60 6f 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63  oid.** this, eac
1de70 68 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74  h thread can obt
1de80 61 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73  ain exclusive us
1de90 65 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61  e of the [databa
1dea0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
1deb0 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20  .** by invoking 
1dec0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
1ded0 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64  nter]([sqlite3_d
1dee0 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66  b_mutex](D)) bef
1def0 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  ore beginning.**
1df00 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e   to use D and in
1df10 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  voking [sqlite3_
1df20 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71  mutex_leave]([sq
1df30 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28  lite3_db_mutex](
1df40 44 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c  D)) after.** all
1df50 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e   calls to the in
1df60 74 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20  terfaces listed 
1df70 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74  here are complet
1df80 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
1df90 69 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20  interface fails 
1dfa0 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55  with SQLITE_MISU
1dfb0 53 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  SE, that means t
1dfc0 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  he interface.** 
1dfd0 77 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f  was invoked inco
1dfe0 72 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61  rrectly by the a
1dff0 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20  pplication.  In 
1e000 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a  that case, the.*
1e010 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  * error code and
1e020 20 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20   message may or 
1e030 6d 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a  may not be set..
1e040 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
1e050 74 73 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20  ts:.** [H12801] 
1e060 5b 48 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33  [H12802] [H12803
1e070 5d 20 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38  ] [H12807] [H128
1e080 30 38 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a  08] [H12809].*/.
1e090 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1e0a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73  qlite3_errcode(s
1e0b0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
1e0c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1e0d0 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72  te3_extended_err
1e0e0 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
1e0f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
1e100 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1e110 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33  3_errmsg(sqlite3
1e120 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
1e130 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1e140 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69  e3_errmsg16(sqli
1e150 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
1e160 50 49 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74  PI3REF: SQL Stat
1e170 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31  ement Object {H1
1e180 33 30 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a  3000} <H13010>.*
1e190 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65  * KEYWORDS: {pre
1e1a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d  pared statement}
1e1b0 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   {prepared state
1e1c0 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20  ments}.**.** An 
1e1d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
1e1e0 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
1e1f0 74 73 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20  ts a single SQL 
1e200 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68  statement..** Th
1e210 69 73 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72  is object is var
1e220 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20  iously known as 
1e230 61 20 22 70 72 65 70 61 72 65 64 20 73 74 61 74  a "prepared stat
1e240 65 6d 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22  ement" or a.** "
1e250 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
1e260 74 65 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c  tement" or simpl
1e270 79 20 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e  y as a "statemen
1e280 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69  t"..**.** The li
1e290 66 65 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e  fe of a statemen
1e2a0 74 20 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f  t object goes so
1e2b0 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69  mething like thi
1e2c0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a  s:.**.** <ol>.**
1e2d0 20 3c 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65   <li> Create the
1e2e0 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73   object using [s
1e2f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1e300 32 28 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65  2()] or a relate
1e310 64 0a 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69  d.**      functi
1e320 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64  on..** <li> Bind
1e330 20 76 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74   values to [host
1e340 20 70 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69   parameters] usi
1e350 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ng the sqlite3_b
1e360 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20  ind_*().**      
1e370 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c  interfaces..** <
1e380 6c 69 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20  li> Run the SQL 
1e390 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
1e3a0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20  te3_step()] one 
1e3b0 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a  or more times..*
1e3c0 2a 20 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65  * <li> Reset the
1e3d0 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67   statement using
1e3e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
1e3f0 29 5d 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a  )] then go back.
1e400 2a 2a 20 20 20 20 20 20 74 6f 20 73 74 65 70 20  **      to step 
1e410 32 2e 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f  2.  Do this zero
1e420 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a   or more times..
1e430 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20  ** <li> Destroy 
1e440 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67  the object using
1e450 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
1e460 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a  ze()]..** </ol>.
1e470 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64  **.** Refer to d
1e480 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20  ocumentation on 
1e490 69 6e 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f  individual metho
1e4a0 64 73 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64  ds above for add
1e4b0 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
1e4c0 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  mation..*/.typed
1e4d0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
1e4e0 33 5f 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73  3_stmt sqlite3_s
1e4f0 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  tmt;../*.** CAPI
1e500 33 52 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c  3REF: Run-time L
1e510 69 6d 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c  imits {H12760} <
1e520 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20600>.**.** Th
1e530 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c  is interface all
1e540 6f 77 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ows the size of 
1e550 76 61 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63  various construc
1e560 74 73 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64  ts to be limited
1e570 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74  .** on a connect
1e580 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f  ion by connectio
1e590 6e 20 62 61 73 69 73 2e 20 20 54 68 65 20 66 69  n basis.  The fi
1e5a0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
1e5b0 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73   the.** [databas
1e5c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68  e connection] wh
1e5d0 6f 73 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20  ose limit is to 
1e5e0 62 65 20 73 65 74 20 6f 72 20 71 75 65 72 69 65  be set or querie
1e5f0 64 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e  d.  The.** secon
1e600 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f  d parameter is o
1e610 6e 65 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74  ne of the [limit
1e620 20 63 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61   categories] tha
1e630 74 20 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c  t define a.** cl
1e640 61 73 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74  ass of construct
1e650 73 20 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d  s to be size lim
1e660 69 74 65 64 2e 20 20 54 68 65 20 74 68 69 72 64  ited.  The third
1e670 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
1e680 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66  e.** new limit f
1e690 6f 72 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63  or that construc
1e6a0 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  t.  The function
1e6b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64   returns the old
1e6c0 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66   limit..**.** If
1e6d0 20 74 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69   the new limit i
1e6e0 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  s a negative num
1e6f0 62 65 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69  ber, the limit i
1e700 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20  s unchanged..** 
1e710 46 6f 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61  For the limit ca
1e720 74 65 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45  tegory of SQLITE
1e730 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65  _LIMIT_XYZ there
1e740 20 69 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74   is a .** [limit
1e750 73 20 7c 20 68 61 72 64 20 75 70 70 65 72 20 62  s | hard upper b
1e760 6f 75 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20  ound].** set by 
1e770 61 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43  a compile-time C
1e780 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   preprocessor ma
1e790 63 72 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c  cro named .** [l
1e7a0 69 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d  imits | SQLITE_M
1e7b0 41 58 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65  AX_XYZ]..** (The
1e7c0 20 22 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68   "_LIMIT_" in th
1e7d0 65 20 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65  e name is change
1e7e0 64 20 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a  d to "_MAX_".).*
1e7f0 2a 20 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e  * Attempts to in
1e800 63 72 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61  crease a limit a
1e810 62 6f 76 65 20 69 74 73 20 68 61 72 64 20 75 70  bove its hard up
1e820 70 65 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a  per bound are.**
1e830 20 73 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61   silently trunca
1e840 74 65 64 20 74 6f 20 74 68 65 20 68 61 72 64 20  ted to the hard 
1e850 75 70 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a  upper limit..**.
1e860 2a 2a 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69  ** Run time limi
1e870 74 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  ts are intended 
1e880 66 6f 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69  for use in appli
1e890 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e  cations that man
1e8a0 61 67 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69  age.** both thei
1e8b0 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64  r own internal d
1e8c0 61 74 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f  atabase and also
1e8d0 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
1e8e0 61 72 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a  are controlled.*
1e8f0 2a 20 62 79 20 75 6e 74 72 75 73 74 65 64 20 65  * by untrusted e
1e900 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e  xternal sources.
1e910 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70    An example app
1e920 6c 69 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62  lication might b
1e930 65 20 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73  e a.** web brows
1e940 65 72 20 74 68 61 74 20 68 61 73 20 69 74 73 20  er that has its 
1e950 6f 77 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f  own databases fo
1e960 72 20 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72  r storing histor
1e970 79 20 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74  y and.** separat
1e980 65 20 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74  e databases cont
1e990 72 6f 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63  rolled by JavaSc
1e9a0 72 69 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e  ript application
1e9b0 73 20 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20  s downloaded.** 
1e9c0 6f 66 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74  off the Internet
1e9d0 2e 20 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20  .  The internal 
1e9e0 64 61 74 61 62 61 73 65 73 20 63 61 6e 20 62 65  databases can be
1e9f0 20 67 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61   given the.** la
1ea00 72 67 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d  rge, default lim
1ea10 69 74 73 2e 20 20 44 61 74 61 62 61 73 65 73 20  its.  Databases 
1ea20 6d 61 6e 61 67 65 64 20 62 79 20 65 78 74 65 72  managed by exter
1ea30 6e 61 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a  nal sources can.
1ea40 2a 2a 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68  ** be given much
1ea50 20 73 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20   smaller limits 
1ea60 64 65 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76  designed to prev
1ea70 65 6e 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20  ent a denial of 
1ea80 73 65 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63  service.** attac
1ea90 6b 2e 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d  k.  Developers m
1eaa0 69 67 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74  ight also want t
1eab0 6f 20 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74  o use the [sqlit
1eac0 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
1ead0 72 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  r()].** interfac
1eae0 65 20 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e  e to further con
1eaf0 74 72 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53  trol untrusted S
1eb00 51 4c 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66  QL.  The size of
1eb10 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1eb20 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 75   created by an u
1eb30 6e 74 72 75 73 74 65 64 20 73 63 72 69 70 74 20  ntrusted script 
1eb40 63 61 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64  can be contained
1eb50 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d   using the.** [m
1eb60 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b  ax_page_count] [
1eb70 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e  PRAGMA]..**.** N
1eb80 65 77 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69  ew run-time limi
1eb90 74 20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79  t categories may
1eba0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
1ebb0 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  ure releases..**
1ebc0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1ebd0 3a 0a 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48  :.** [H12762] [H
1ebe0 31 32 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a  12766] [H12769].
1ebf0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1ec00 74 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28  t sqlite3_limit(
1ec10 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64  sqlite3*, int id
1ec20 2c 20 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a  , int newVal);..
1ec30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1ec40 52 75 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43  Run-Time Limit C
1ec50 61 74 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39  ategories {H1279
1ec60 30 7d 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b  0} <H12760>.** K
1ec70 45 59 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20  EYWORDS: {limit 
1ec80 63 61 74 65 67 6f 72 79 7d 20 7b 2a 6c 69 6d 69  category} {*limi
1ec90 74 20 63 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a  t categories}.**
1eca0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
1ecb0 6e 74 73 20 64 65 66 69 6e 65 20 76 61 72 69 6f  nts define vario
1ecc0 75 73 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c  us performance l
1ecd0 69 6d 69 74 73 0a 2a 2a 20 74 68 61 74 20 63 61  imits.** that ca
1ece0 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 61 74 20  n be lowered at 
1ecf0 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b  run-time using [
1ed00 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d  sqlite3_limit()]
1ed10 2e 0a 2a 2a 20 54 68 65 20 73 79 6e 6f 70 73 69  ..** The synopsi
1ed20 73 20 6f 66 20 74 68 65 20 6d 65 61 6e 69 6e 67  s of the meaning
1ed30 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  s of the various
1ed40 20 6c 69 6d 69 74 73 20 69 73 20 73 68 6f 77 6e   limits is shown
1ed50 20 62 65 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74   below..** Addit
1ed60 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1ed70 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 61  n is available a
1ed80 74 20 5b 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69  t [limits | Limi
1ed90 74 73 20 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a  ts in SQLite]..*
1eda0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
1edb0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  >SQLITE_LIMIT_LE
1edc0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1edd0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a  >The maximum siz
1ede0 65 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20  e of any string 
1edf0 6f 72 20 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65  or BLOB or table
1ee00 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20   row.<dd>.**.** 
1ee10 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1ee20 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  _SQL_LENGTH</dt>
1ee30 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1ee40 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e  mum length of an
1ee50 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c   SQL statement.<
1ee60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1ee70 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55  QLITE_LIMIT_COLU
1ee80 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  MN</dt>.** <dd>T
1ee90 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1eea0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1eeb0 61 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69  a table definiti
1eec0 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20  on or in the.** 
1eed0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
1eee0 5b 53 45 4c 45 43 54 5d 20 6f 72 20 74 68 65 20  [SELECT] or the 
1eef0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1ef00 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20  f columns in an 
1ef10 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61  index.** or in a
1ef20 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
1ef30 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f  OUP BY clause.</
1ef40 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
1ef50 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f  LITE_LIMIT_EXPR_
1ef60 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  DEPTH</dt>.** <d
1ef70 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  d>The maximum de
1ef80 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65  pth of the parse
1ef90 20 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70   tree on any exp
1efa0 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  ression.</dd>.**
1efb0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1efc0 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  IMIT_COMPOUND_SE
1efd0 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  LECT</dt>.** <dd
1efe0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d  >The maximum num
1eff0 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
1f000 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
1f010 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64  T statement.</dd
1f020 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f030 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50  TE_LIMIT_VDBE_OP
1f040 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1f050 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1f060 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
1f070 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63  in a virtual mac
1f080 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  hine program.** 
1f090 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1f0a0 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  t an SQL stateme
1f0b0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  nt.</dd>.**.** <
1f0c0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1f0d0 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74  FUNCTION_ARG</dt
1f0e0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f0f0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
1f100 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75  rguments on a fu
1f110 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a  nction.</dd>.**.
1f120 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1f130 4d 49 54 5f 41 54 54 41 43 48 45 44 3c 2f 64 74  MIT_ATTACHED</dt
1f140 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f150 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b  imum number of [
1f160 41 54 54 41 43 48 20 7c 20 61 74 74 61 63 68 65  ATTACH | attache
1f170 64 20 64 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64  d databases].</d
1f180 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1f190 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
1f1a0 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64  ATTERN_LENGTH</d
1f1b0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1f1c0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
1f1d0 74 68 65 20 70 61 74 74 65 72 6e 20 61 72 67 75  the pattern argu
1f1e0 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b  ment to the [LIK
1f1f0 45 5d 20 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20  E] or.** [GLOB] 
1f200 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a  operators.</dd>.
1f210 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1f220 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
1f230 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c  NUMBER</dt>.** <
1f240 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  dd>The maximum n
1f250 75 6d 62 65 72 20 6f 66 20 76 61 72 69 61 62 6c  umber of variabl
1f260 65 73 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61  es in an SQL sta
1f270 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61 6e 0a  tement that can.
1f280 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64  ** be bound.</dd
1f290 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f2a0 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
1f2b0 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c  _DEPTH</dt>.** <
1f2c0 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64  dd>The maximum d
1f2d0 65 70 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f  epth of recursio
1f2e0 6e 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 3c  n for triggers.<
1f2f0 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  /dd>.** </dl>.*/
1f300 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f310 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20  LIMIT_LENGTH    
1f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f330 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
1f340 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
1f350 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1f360 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
1f370 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20  E_LIMIT_COLUMN  
1f380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f390 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
1f3a0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45  TE_LIMIT_EXPR_DE
1f3b0 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20  PTH             
1f3c0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
1f3d0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
1f3e0 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20  ND_SELECT       
1f3f0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
1f400 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f  LITE_LIMIT_VDBE_
1f410 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OP              
1f420 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53       5.#define S
1f430 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43  QLITE_LIMIT_FUNC
1f440 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20  TION_ARG        
1f450 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
1f460 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
1f470 41 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20  ACHED           
1f480 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65         7.#define
1f490 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49   SQLITE_LIMIT_LI
1f4a0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1f4b0 48 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e  H       8.#defin
1f4c0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  e SQLITE_LIMIT_V
1f4d0 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20  ARIABLE_NUMBER  
1f4e0 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
1f4f0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f500 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 20 20  TRIGGER_DEPTH   
1f510 20 20 20 20 20 20 20 20 20 31 30 0a 0a 2f 2a 0a           10../*.
1f520 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
1f530 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74  piling An SQL St
1f540 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d  atement {H13010}
1f550 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59   <S10000>.** KEY
1f560 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74  WORDS: {SQL stat
1f570 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a  ement compiler}.
1f580 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65  **.** To execute
1f590 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69   an SQL query, i
1f5a0 74 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20  t must first be 
1f5b0 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20  compiled into a 
1f5c0 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f  byte-code.** pro
1f5d0 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f  gram using one o
1f5e0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
1f5f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
1f600 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 62 22  t argument, "db"
1f610 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65  , is a [database
1f620 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74   connection] obt
1f630 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20  ained from a.** 
1f640 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c  prior successful
1f650 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
1f660 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
1f670 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f  te3_open_v2()] o
1f680 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70  r.** [sqlite3_op
1f690 65 6e 31 36 28 29 5d 2e 20 20 54 68 65 20 64 61  en16()].  The da
1f6a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1f6b0 6e 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20  n must not have 
1f6c0 62 65 65 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  been closed..**.
1f6d0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
1f6e0 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20  gument, "zSql", 
1f6f0 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  is the statement
1f700 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c   to be compiled,
1f710 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65   encoded.** as e
1f720 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
1f730 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69  TF-16.  The sqli
1f740 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e  te3_prepare() an
1f750 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  d sqlite3_prepar
1f760 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66  e_v2().** interf
1f770 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20  aces use UTF-8, 
1f780 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
1f790 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69  are16() and sqli
1f7a0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
1f7b0 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36  ().** use UTF-16
1f7c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
1f7d0 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73  Byte argument is
1f7e0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
1f7f0 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65   then zSql is re
1f800 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20  ad up to the.** 
1f810 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
1f820 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20  nator. If nByte 
1f830 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c  is non-negative,
1f840 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
1f850 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65  maximum.** numbe
1f860 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64  r of  bytes read
1f870 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65   from zSql.  Whe
1f880 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e  n nByte is non-n
1f890 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20  egative, the.** 
1f8a0 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73  zSql string ends
1f8b0 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66   at either the f
1f8c0 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27  irst '\000' or '
1f8d0 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65  \u0000' characte
1f8e0 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74  r or.** the nByt
1f8f0 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68  e-th byte, which
1f900 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
1f910 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  . If the caller 
1f920 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68  knows.** that th
1f930 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e  e supplied strin
1f940 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  g is nul-termina
1f950 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ted, then there 
1f960 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65  is a small.** pe
1f970 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74  rformance advant
1f980 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64  age to be gained
1f990 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e   by passing an n
1f9a0 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74  Byte parameter t
1f9b0 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20  hat.** is equal 
1f9c0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
1f9d0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e   bytes in the in
1f9e0 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e  put string <i>in
1f9f0 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74  cluding</i>.** t
1fa00 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
1fa10 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  r bytes..**.** I
1fa20 66 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20  f pzTail is not 
1fa30 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69  NULL then *pzTai
1fa40 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  l is made to poi
1fa50 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
1fa60 62 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65  byte.** past the
1fa70 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73   end of the firs
1fa80 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  t SQL statement 
1fa90 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20  in zSql.  These 
1faa0 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a  routines only.**
1fab0 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72   compile the fir
1fac0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  st statement in 
1fad0 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c  zSql, so *pzTail
1fae0 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1faf0 67 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d  g to.** what rem
1fb00 61 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e  ains uncompiled.
1fb10 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69  .**.** *ppStmt i
1fb20 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1fb30 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70  to a compiled [p
1fb40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1fb50 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a  t] that can be.*
1fb60 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67  * executed using
1fb70 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1fb80 5d 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  ].  If there is 
1fb90 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d  an error, *ppStm
1fba0 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e  t is set.** to N
1fbb0 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ULL.  If the inp
1fbc0 75 74 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73  ut text contains
1fbd0 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20   no SQL (if the 
1fbe0 69 6e 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74  input is an empt
1fbf0 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61  y.** string or a
1fc00 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a   comment) then *
1fc10 70 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f  ppStmt is set to
1fc20 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61   NULL..** The ca
1fc30 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
1fc40 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
1fc50 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  or deleting the 
1fc60 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20  compiled.** SQL 
1fc70 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20  statement using 
1fc80 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
1fc90 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61  e()] after it ha
1fca0 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
1fcb0 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61  it..** ppStmt ma
1fcc0 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a  y not be NULL..*
1fcd0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
1fce0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20   [SQLITE_OK] is 
1fcf0 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 65 72 77  returned, otherw
1fd00 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f  ise an [error co
1fd10 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  de] is returned.
1fd20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1fd30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20  e3_prepare_v2() 
1fd40 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
1fd50 61 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72  are16_v2() inter
1fd60 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65 63  faces are.** rec
1fd70 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c  ommended for all
1fd80 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54   new programs. T
1fd90 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74  he two older int
1fda0 65 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61  erfaces are reta
1fdb0 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b  ined.** for back
1fdc0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
1fdd0 69 74 79 2c 20 62 75 74 20 74 68 65 69 72 20 75  ity, but their u
1fde0 73 65 20 69 73 20 64 69 73 63 6f 75 72 61 67 65  se is discourage
1fdf0 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76 32  d..** In the "v2
1fe00 22 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68  " interfaces, th
1fe10 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
1fe20 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73 20  ment.** that is 
1fe30 72 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b 73  returned (the [s
1fe40 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a  qlite3_stmt] obj
1fe50 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61 20  ect) contains a 
1fe60 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f  copy of the.** o
1fe70 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74  riginal SQL text
1fe80 2e 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68  . This causes th
1fe90 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
1fea0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f 0a  )] interface to.
1feb0 2a 2a 20 62 65 68 61 76 65 20 64 69 66 66 65 72  ** behave differ
1fec0 65 6e 74 6c 79 20 69 6e 20 74 68 72 65 65 20 77  ently in three w
1fed0 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a  ays:.**.** <ol>.
1fee0 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68  ** <li>.** If th
1fef0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1ff00 61 20 63 68 61 6e 67 65 73 2c 20 69 6e 73 74 65  a changes, inste
1ff10 61 64 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20  ad of returning 
1ff20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20  [SQLITE_SCHEMA] 
1ff30 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20  as it.** always 
1ff40 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c  used to do, [sql
1ff50 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
1ff60 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
1ff70 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 20 53 51  recompile the SQ
1ff80 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61  L.** statement a
1ff90 6e 64 20 74 72 79 20 74 6f 20 72 75 6e 20 69 74  nd try to run it
1ffa0 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20   again.  If the 
1ffb0 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67  schema has chang
1ffc0 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74  ed in.** a way t
1ffd0 68 61 74 20 6d 61 6b 65 73 20 74 68 65 20 73 74  hat makes the st
1ffe0 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65  atement no longe
1fff0 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65  r valid, [sqlite
20000 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 73  3_step()] will s
20010 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b  till.** return [
20020 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20  SQLITE_SCHEMA]. 
20030 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 68 65 20   But unlike the 
20040 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 2c  legacy behavior,
20050 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
20060 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74   is.** now a fat
20070 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69  al error.  Calli
20080 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
20090 61 72 65 5f 76 32 28 29 5d 20 61 67 61 69 6e 20  are_v2()] again 
200a0 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68  will not make th
200b0 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20 61 77  e.** error go aw
200c0 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b  ay.  Note: use [
200d0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
200e0 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 65  ] to find the te
200f0 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72  xt.** of the par
20100 73 69 6e 67 20 65 72 72 6f 72 20 74 68 61 74 20  sing error that 
20110 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53  results in an [S
20120 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 72 65  QLITE_SCHEMA] re
20130 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a  turn..** </li>.*
20140 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65  *.** <li>.** Whe
20150 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  n an error occur
20160 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  s, [sqlite3_step
20170 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ()] will return 
20180 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69  one of the detai
20190 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f  led.** [error co
201a0 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  des] or [extende
201b0 64 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20  d error codes]. 
201c0 20 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61   The legacy beha
201d0 76 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a  vior was that.**
201e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
201f0 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74  ] would only ret
20200 75 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53  urn a generic [S
20210 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73  QLITE_ERROR] res
20220 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  ult code.** and 
20230 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74  you would have t
20240 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20  o make a second 
20250 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
20260 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 6f 72 64  _reset()] in ord
20270 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68  er.** to find th
20280 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75  e underlying cau
20290 73 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65  se of the proble
202a0 6d 2e 20 57 69 74 68 20 74 68 65 20 22 76 32 22  m. With the "v2"
202b0 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65   prepare.** inte
202c0 72 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65  rfaces, the unde
202d0 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f  rlying reason fo
202e0 72 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72  r the error is r
202f0 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
20300 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a  ely..** </li>.**
20310 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 5e 49 66 20  .** <li>.** ^If 
20320 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 5b  the value of a [
20330 70 61 72 61 6d 65 74 65 72 20 7c 20 68 6f 73 74  parameter | host
20340 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 74   parameter] in t
20350 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
20360 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 20  might.** change 
20370 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66  the query plan f
20380 6f 72 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20  or a statement, 
20390 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  then the stateme
203a0 6e 74 20 6d 61 79 20 62 65 0a 2a 2a 20 61 75 74  nt may be.** aut
203b0 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d  omatically recom
203c0 70 69 6c 65 64 20 28 61 73 20 69 66 20 74 68 65  piled (as if the
203d0 72 65 20 68 61 64 20 62 65 65 6e 20 61 20 73 63  re had been a sc
203e0 68 65 6d 61 20 63 68 61 6e 67 65 29 20 6f 6e 20  hema change) on 
203f0 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 5b 73  the first .** [s
20400 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63  qlite3_step()] c
20410 61 6c 6c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e  all following an
20420 79 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20  y change to the 
20430 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
20440 64 5f 74 65 78 74 20 7c 20 62 69 6e 64 69 6e 67  d_text | binding
20450 73 5d 20 6f 66 20 74 68 65 20 5b 70 61 72 61 6d  s] of the [param
20460 65 74 65 72 5d 2e 20 0a 2a 2a 20 3c 2f 6c 69 3e  eter]. .** </li>
20470 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
20480 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
20490 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 30 31   [H13011] [H1301
204a0 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 31 33  2] [H13013] [H13
204b0 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 5b 48  014] [H13015] [H
204c0 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 5d 20  13016] [H13019] 
204d0 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53  [H13021].**.*/.S
204e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
204f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20  lite3_prepare(. 
20500 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
20510 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
20520 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
20530 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
20540 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  l,       /* SQL 
20550 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38  statement, UTF-8
20560 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e   encoded */.  in
20570 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
20580 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
20590 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   length of zSql 
205a0 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
205b0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
205c0 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53  Stmt,  /* OUT: S
205d0 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20  tatement handle 
205e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
205f0 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20  **pzTail     /* 
20600 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
20610 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f  unused portion o
20620 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c  f zSql */.);.SQL
20630 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20640 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a  te3_prepare_v2(.
20650 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
20660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
20670 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
20680 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
20690 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
206a0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
206b0 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69  8 encoded */.  i
206c0 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20  nt nByte,       
206d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
206e0 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  m length of zSql
206f0 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
20700 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
20710 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  pStmt,  /* OUT: 
20720 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  Statement handle
20730 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20740 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a   **pzTail     /*
20750 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f   OUT: Pointer to
20760 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20   unused portion 
20770 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51  of zSql */.);.SQ
20780 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20790 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a  ite3_prepare16(.
207a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
207b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
207c0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
207d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
207e0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
207f0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
20800 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  16 encoded */.  
20810 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
20820 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
20830 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
20840 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
20850 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
20860 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
20870 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
20880 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
20890 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  d **pzTail     /
208a0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
208b0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
208c0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53   of zSql */.);.S
208d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
208e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
208f0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
20900 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
20910 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
20920 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
20930 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
20940 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
20950 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
20960 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
20970 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
20980 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
20990 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
209a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
209b0 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
209c0 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
209d0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
209e0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
209f0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
20a00 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
20a10 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
20a20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
20a30 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53  EF: Retrieving S
20a40 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 48 31  tatement SQL {H1
20a50 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e 0a 2a  3100} <H13000>.*
20a60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
20a70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ace can be used 
20a80 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 73 61  to retrieve a sa
20a90 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20  ved copy of the 
20aa0 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20  original.** SQL 
20ab0 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 72 65  text used to cre
20ac0 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20  ate a [prepared 
20ad0 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 74 68  statement] if th
20ae0 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  at statement was
20af0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 73 69  .** compiled usi
20b00 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
20b10 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
20b20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
20b30 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a  pare16_v2()]..**
20b40 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
20b50 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 5b 48  :.** [H13101] [H
20b60 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 5d 0a  13102] [H13103].
20b70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
20b80 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
20b90 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74  3_sql(sqlite3_st
20ba0 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
20bb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 79 6e  ** CAPI3REF: Dyn
20bc0 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56  amically Typed V
20bd0 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 31 35  alue Object {H15
20be0 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  000} <S20200>.**
20bf0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74   KEYWORDS: {prot
20c00 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
20c10 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65  lue} {unprotecte
20c20 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
20c30 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73  .**.** SQLite us
20c40 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  es the sqlite3_v
20c50 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 72  alue object to r
20c60 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c  epresent all val
20c70 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  ues.** that can 
20c80 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 64  be stored in a d
20c90 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 53  atabase table. S
20ca0 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d  QLite uses dynam
20cb0 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72  ic typing.** for
20cc0 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73   the values it s
20cd0 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 73 74  tores. Values st
20ce0 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f  ored in sqlite3_
20cf0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a  value objects.**
20d00 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 72 73   can be integers
20d10 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  , floating point
20d20 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73   values, strings
20d30 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c  , BLOBs, or NULL
20d40 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74  ..**.** An sqlit
20d50 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
20d60 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70  may be either "p
20d70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e  rotected" or "un
20d80 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53  protected"..** S
20d90 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72  ome interfaces r
20da0 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74  equire a protect
20db0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
20dc0 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61  .  Other interfa
20dd0 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65  ces.** will acce
20de0 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f 74  pt either a prot
20df0 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70 72  ected or an unpr
20e00 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
20e10 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20  value..** Every 
20e20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 61  interface that a
20e30 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 5f 76  ccepts sqlite3_v
20e40 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 20 73  alue arguments s
20e50 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 65 74  pecifies.** whet
20e60 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 72 65  her or not it re
20e70 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 63 74  quires a protect
20e80 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
20e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d  ..**.** The term
20ea0 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 61 6e  s "protected" an
20eb0 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 20  d "unprotected" 
20ec0 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 65 72  refer to whether
20ed0 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74   or not.** a mut
20ee0 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 20 69  ex is held.  A i
20ef0 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 73  nternal mutex is
20f00 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 6f 74   held for a prot
20f10 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
20f20 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 62 75  _value object bu
20f30 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 68 65  t no mutex is he
20f40 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74  ld for an unprot
20f50 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
20f60 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20  _value object.  
20f70 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
20f80 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 6e 67  piled to be sing
20f90 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 28  le-threaded.** (
20fa0 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 48 52  with [SQLITE_THR
20fb0 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 20 77  EADSAFE=0] and w
20fc0 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 68 72  ith [sqlite3_thr
20fd0 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 75 72  eadsafe()] retur
20fe0 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66  ning 0).** or if
20ff0 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e 20 69   SQLite is run i
21000 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 65 64  n one of reduced
21010 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a   mutex modes .**
21020 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
21030 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 6f 72  SINGLETHREAD] or
21040 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
21050 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20  MULTITHREAD].** 
21060 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
21070 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74   distinction bet
21080 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61  ween protected a
21090 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a  nd unprotected.*
210a0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
210b0 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79  objects and they
210c0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74   can be used int
210d0 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48  erchangeably.  H
210e0 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d  owever,.** for m
210f0 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74  aximum code port
21100 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65  ability it is re
21110 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61  commended that a
21120 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
21130 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69  till make the di
21140 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
21150 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63  n between protec
21160 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63  ted and unprotec
21170 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ted.** sqlite3_v
21180 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65  alue objects eve
21190 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 69 63  n when not stric
211a0 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a  tly required..**
211b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
211c0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 74 68  value objects th
211d0 61 74 20 61 72 65 20 70 61 73 73 65 64 20 61 73  at are passed as
211e0 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 74 6f   parameters into
211f0 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
21200 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69  tation of [appli
21210 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
21220 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72  QL functions] ar
21230 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  e protected..** 
21240 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
21250 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  e object returne
21260 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  d by.** [sqlite3
21270 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
21280 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e   is unprotected.
21290 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20  .** Unprotected 
212a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
212b0 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62  jects may only b
212c0 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b  e used with.** [
212d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
212e0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  alue()] and [sql
212f0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
21300 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  )]..** The [sqli
21310 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c  te3_value_blob |
21320 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
21330 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66  ype()] family of
21340 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72  .** interfaces r
21350 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 65 64  equire protected
21360 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
21370 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64  bjects..*/.typed
21380 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71  ef struct Mem sq
21390 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a  lite3_value;../*
213a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
213b0 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65  L Function Conte
213c0 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 30 30  xt Object {H1600
213d0 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a  1} <S20200>.**.*
213e0 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e  * The context in
213f0 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75   which an SQL fu
21400 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20  nction executes 
21410 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a  is stored in an.
21420 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ** sqlite3_conte
21430 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f  xt object.  A po
21440 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
21450 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65  te3_context obje
21460 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  ct.** is always 
21470 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
21480 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d  to [application-
21490 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63  defined SQL func
214a0 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61  tions]..** The a
214b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
214c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ed SQL function 
214d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
214e0 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a 2a 2a  ill pass this.**
214f0 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68   pointer through
21500 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b   into calls to [
21510 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
21520 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73  nt | sqlite3_res
21530 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  ult()],.** [sqli
21540 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
21550 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74  ntext()], [sqlit
21560 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c  e3_user_data()],
21570 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
21580 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29  text_db_handle()
21590 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  ], [sqlite3_get_
215a0 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61  auxdata()],.** a
215b0 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73  nd/or [sqlite3_s
215c0 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a  et_auxdata()]..*
215d0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
215e0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
215f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
21600 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
21610 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65  F: Binding Value
21620 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53 74  s To Prepared St
21630 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 30 30  atements {H13500
21640 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45  } <S70300>.** KE
21650 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 70 61  YWORDS: {host pa
21660 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 20 70  rameter} {host p
21670 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74  arameters} {host
21680 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d   parameter name}
21690 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53  .** KEYWORDS: {S
216a0 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 53  QL parameter} {S
216b0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b  QL parameters} {
216c0 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e  parameter bindin
216d0 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  g}.**.** In the 
216e0 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e 70 75  SQL strings inpu
216f0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72  t to [sqlite3_pr
21700 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
21710 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a  its variants,.**
21720 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 62 65   literals may be
21730 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 5b   replaced by a [
21740 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61 74 20  parameter] that 
21750 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66 20 66  matches one of f
21760 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 65 6d 70  ollowing.** temp
21770 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  lates:.**.** <ul
21780 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a 2a 20  >.** <li>  ?.** 
21790 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c  <li>  ?NNN.** <l
217a0 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c 69 3e  i>  :VVV.** <li>
217b0 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20    @VVV.** <li>  
217c0 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  $VVV.** </ul>.**
217d0 0a 2a 2a 20 49 6e 20 74 68 65 20 74 65 6d 70 6c  .** In the templ
217e0 61 74 65 73 20 61 62 6f 76 65 2c 20 4e 4e 4e 20  ates above, NNN 
217f0 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e  represents an in
21800 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a  teger literal,.*
21810 2a 20 61 6e 64 20 56 56 56 20 72 65 70 72 65 73  * and VVV repres
21820 65 6e 74 73 20 61 6e 20 61 6c 70 68 61 6e 75 6d  ents an alphanum
21830 65 72 69 63 20 69 64 65 6e 74 69 66 65 72 2e 20  eric identifer. 
21840 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   The values of t
21850 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  hese.** paramete
21860 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20  rs (also called 
21870 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20  "host parameter 
21880 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c 20 70  names" or "SQL p
21890 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a 20 63  arameters").** c
218a0 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e 67 20  an be set using 
218b0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  the sqlite3_bind
218c0 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 64 65  _*() routines de
218d0 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a 0a 2a  fined here..**.*
218e0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
218f0 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69  ment to the sqli
21900 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
21910 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79 73 0a  tines is always.
21920 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
21930 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
21940 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e  t] object return
21950 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69  ed from.** [sqli
21960 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
21970 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74  ] or its variant
21980 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  s..**.** The sec
21990 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
219a0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
219b0 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 74   SQL parameter t
219c0 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54 68 65  o be set..** The
219d0 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61   leftmost SQL pa
219e0 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e 20 69  rameter has an i
219f0 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68 65 6e  ndex of 1.  When
21a00 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 64 0a   the same named.
21a10 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  ** SQL parameter
21a20 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 74 68   is used more th
21a30 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e 64 20  an once, second 
21a40 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 0a 2a  and subsequent.*
21a50 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 68 61  * occurrences ha
21a60 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65  ve the same inde
21a70 78 20 61 73 20 74 68 65 20 66 69 72 73 74 20 6f  x as the first o
21a80 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20 54 68  ccurrence..** Th
21a90 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61 6d 65  e index for name
21aa0 64 20 70 61 72 61 6d 65 74 65 72 73 20 63 61 6e  d parameters can
21ab0 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73   be looked up us
21ac0 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  ing the.** [sqli
21ad0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
21ae0 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50 49 20  er_index()] API 
21af0 69 66 20 64 65 73 69 72 65 64 2e 20 20 54 68 65  if desired.  The
21b00 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 22 3f   index.** for "?
21b10 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72 73 20  NNN" parameters 
21b20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
21b30 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e 4e 20  NNN..** The NNN 
21b40 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 62 65  value must be be
21b50 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68 65 20  tween 1 and the 
21b60 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29  [sqlite3_limit()
21b70 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 5b  ].** parameter [
21b80 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
21b90 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 28 64  IABLE_NUMBER] (d
21ba0 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39  efault value: 99
21bb0 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  9)..**.** The th
21bc0 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
21bd0 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e  the value to bin
21be0 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74  d to the paramet
21bf0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 6f  er..**.** In tho
21c00 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  se routines that
21c10 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 20 61   have a fourth a
21c20 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 61 6c  rgument, its val
21c30 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d  ue is the.** num
21c40 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
21c50 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 20 20  the parameter.  
21c60 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 68 65  To be clear: the
21c70 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a   value is the.**
21c80 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e 62 79   number of <u>by
21c90 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 20 76  tes</u> in the v
21ca0 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 6e 75  alue, not the nu
21cb0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
21cc0 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  rs..** If the fo
21cd0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  urth parameter i
21ce0 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  s negative, the 
21cf0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  length of the st
21d00 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65 20 6e  ring is.** the n
21d10 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 75  umber of bytes u
21d20 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a  p to the first z
21d30 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ero terminator..
21d40 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20  **.** The fifth 
21d50 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
21d60 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 2c  te3_bind_blob(),
21d70 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
21d80 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 71 6c  xt(), and.** sql
21d90 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
21da0 28 29 20 69 73 20 61 20 64 65 73 74 72 75 63 74  () is a destruct
21db0 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 70 6f  or used to dispo
21dc0 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 6f  se of the BLOB o
21dd0 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 74 65  r.** string afte
21de0 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 69 6e  r SQLite has fin
21df0 69 73 68 65 64 20 77 69 74 68 20 69 74 2e 20 49  ished with it. I
21e00 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75  f the fifth argu
21e10 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65 20 73  ment is.** the s
21e20 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b 53 51  pecial value [SQ
21e30 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 74 68  LITE_STATIC], th
21e40 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65  en SQLite assume
21e50 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69 6e  s that the.** in
21e60 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 6e 20  formation is in 
21e70 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 67 65  static, unmanage
21e80 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73  d space and does
21e90 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
21ea0 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  freed..** If the
21eb0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20   fifth argument 
21ec0 68 61 73 20 74 68 65 20 76 61 6c 75 65 20 5b 53  has the value [S
21ed0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 5d  QLITE_TRANSIENT]
21ee0 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  , then.** SQLite
21ef0 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e 20 70   makes its own p
21f00 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74  rivate copy of t
21f10 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69 61 74  he data immediat
21f20 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a 20 74  ely, before.** t
21f30 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
21f40 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  *() routine retu
21f50 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  rns..**.** The s
21f60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
21f70 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65 20 62  blob() routine b
21f80 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66 20 6c  inds a BLOB of l
21f90 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a 2a 20  ength N that.** 
21fa0 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 7a  is filled with z
21fb0 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f 62 6c  eroes.  A zerobl
21fc0 6f 62 20 75 73 65 73 20 61 20 66 69 78 65 64 20  ob uses a fixed 
21fd0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
21fe0 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69 6e 74  .** (just an int
21ff0 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69 74 73  eger to hold its
22000 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69 74 20   size) while it 
22010 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 73 73  is being process
22020 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f 62 73  ed..** Zeroblobs
22030 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
22040 20 73 65 72 76 65 20 61 73 20 70 6c 61 63 65 68   serve as placeh
22050 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f 42 73  olders for BLOBs
22060 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e   whose.** conten
22070 74 20 69 73 20 6c 61 74 65 72 20 77 72 69 74 74  t is later writt
22080 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c  en using.** [sql
22090 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c  ite3_blob_open |
220a0 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f   incremental BLO
220b0 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65 73 2e  B I/O] routines.
220c0 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 76  .** A negative v
220d0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a 65 72  alue for the zer
220e0 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 69 6e  oblob results in
220f0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42   a zero-length B
22100 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  LOB..**.** The s
22110 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20  qlite3_bind_*() 
22120 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
22130 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a 2a 2a   called after.**
22140 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
22150 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69 74 73  e_v2()] (and its
22160 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20 5b 73   variants) or [s
22170 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
22180 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20 5b 73  and.** before [s
22190 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a  qlite3_step()]..
221a0 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72 65 20  ** Bindings are 
221b0 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 20 74  not cleared by t
221c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
221d0 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  t()] routine..**
221e0 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65 74   Unbound paramet
221f0 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 72 65  ers are interpre
22200 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ted as NULL..**.
22210 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
22220 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  s return [SQLITE
22230 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20  _OK] on success 
22240 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
22250 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   if.** anything 
22260 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b 53 51  goes wrong.  [SQ
22270 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73 20 72  LITE_RANGE] is r
22280 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 70  eturned if the p
22290 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 64 65  arameter.** inde
222a0 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  x is out of rang
222b0 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  e.  [SQLITE_NOME
222c0 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  M] is returned i
222d0 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  f malloc() fails
222e0 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53  ..** [SQLITE_MIS
222f0 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20 72 65  USE] might be re
22300 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 65 20  turned if these 
22310 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c  routines are cal
22320 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74  led on a.** virt
22330 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74  ual machine that
22340 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 73 74   is the wrong st
22350 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 61 73  ate or which has
22360 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69   already been fi
22370 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65 74 65  nalized..** Dete
22380 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73 65 20  ction of misuse 
22390 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e 20 20  is unreliable.  
223a0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f  Applications sho
223b0 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64 0a 2a  uld not depend.*
223c0 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  * on SQLITE_MISU
223d0 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53 51 4c  SE returns.  SQL
223e0 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20 69 6e  ITE_MISUSE is in
223f0 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69 63 61  tended to indica
22400 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69 63 20  te a.** a logic 
22410 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61 70 70  error in the app
22420 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74 75 72  lication.  Futur
22430 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
22440 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 70 61  Lite might.** pa
22450 6e 69 63 20 72 61 74 68 65 72 20 74 68 61 6e 20  nic rather than 
22460 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
22470 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  SUSE..**.** See 
22480 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
22490 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
224a0 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  unt()],.** [sqli
224b0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
224c0 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 20  er_name()], and 
224d0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
224e0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
224f0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
22500 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35 30 36  ents:.** [H13506
22510 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31 33 35  ] [H13509] [H135
22520 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b 48 31  12] [H13515] [H1
22530 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d 20 5b  3518] [H13521] [
22540 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32 37 5d  H13524] [H13527]
22550 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b 48 31  .** [H13530] [H1
22560 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d 20 5b  3533] [H13536] [
22570 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34 32 5d  H13539] [H13542]
22580 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33 35 34   [H13545] [H1354
22590 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a 0a 2a  8] [H13551].**.*
225a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
225b0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
225c0 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ob(sqlite3_stmt*
225d0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  , int, const voi
225e0 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28  d*, int n, void(
225f0 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
22600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22610 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73  e3_bind_double(s
22620 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
22630 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49  t, double);.SQLI
22640 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22650 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69  e3_bind_int(sqli
22660 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
22680 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22690 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  d_int64(sqlite3_
226a0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 6c 69  stmt*, int, sqli
226b0 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49  te3_int64);.SQLI
226c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
226d0 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c  e3_bind_null(sql
226e0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29  ite3_stmt*, int)
226f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
22700 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
22710 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  xt(sqlite3_stmt*
22720 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
22730 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28  r*, int n, void(
22740 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.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 74 65 78 74 31 36 28 73  e3_bind_text16(s
22770 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
22780 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
22790 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
227a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
227b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
227c0 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
227d0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  stmt*, int, cons
227e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t sqlite3_value*
227f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
22800 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  t sqlite3_bind_z
22810 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  eroblob(sqlite3_
22820 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 20  stmt*, int, int 
22830 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  n);../*.** CAPI3
22840 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20 53  REF: Number Of S
22850 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48  QL Parameters {H
22860 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a  13600} <S70300>.
22870 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
22880 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ne can be used t
22890 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65  o find the numbe
228a0 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61 6d 65  r of [SQL parame
228b0 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20 5b 70  ters].** in a [p
228c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
228d0 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d 65 74  t].  SQL paramet
228e0 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73 20 6f  ers are tokens o
228f0 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 22 3f  f the.** form "?
22900 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41 41 41  ", "?NNN", ":AAA
22910 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20 22 40  ", "$AAA", or "@
22920 41 41 41 22 20 74 68 61 74 20 73 65 72 76 65 20  AAA" that serve 
22930 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c 64 65  as.** placeholde
22940 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20 74 68  rs for values th
22950 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65 33 5f  at are [sqlite3_
22960 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f 75 6e  bind_blob | boun
22970 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 72  d].** to the par
22980 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c 61 74  ameters at a lat
22990 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  er time..**.** T
229a0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 75  his routine actu
229b0 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 68 65  ally returns the
229c0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6c 61   index of the la
229d0 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f 73 74  rgest (rightmost
229e0 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20  ).** parameter. 
229f0 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 65 78  For all forms ex
22a00 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 73 20  cept ?NNN, this 
22a10 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20  will correspond 
22a20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  to the.** number
22a30 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 61 6d   of unique param
22a40 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 61 6d  eters.  If param
22a50 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f 4e 4e  eters of the ?NN
22a60 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 74  N are used,.** t
22a70 68 65 72 65 20 6d 61 79 20 62 65 20 67 61 70 73  here may be gaps
22a80 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a   in the list..**
22a90 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
22aa0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
22ab0 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
22ac0 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
22ad0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
22ae0 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  e()], and.** [sq
22af0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22b00 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
22b10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
22b20 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d 0a 2a  s:.** [H13601].*
22b30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22b40 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
22b50 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71  rameter_count(sq
22b60 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
22b70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
22b80 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20 50 61  ame Of A Host Pa
22b90 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32 30 7d  rameter {H13620}
22ba0 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70300>.**.** 
22bb0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
22bc0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
22bd0 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  o the name of th
22be0 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c 20 70  e n-th.** [SQL p
22bf0 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61 20 5b  arameter] in a [
22c00 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22c10 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 72 61  nt]..** SQL para
22c20 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f  meters of the fo
22c30 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41  rm "?NNN" or ":A
22c40 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72  AA" or "@AAA" or
22c50 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 65 20   "$AAA".** have 
22c60 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 73 20  a name which is 
22c70 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e 4e 4e  the string "?NNN
22c80 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22  " or ":AAA" or "
22c90 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a  @AAA" or "$AAA".
22ca0 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  ** respectively.
22cb0 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
22cc0 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ds, the initial 
22cd0 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 22 40  ":" or "$" or "@
22ce0 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 20 69  " or "?".** is i
22cf0 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72 74 20  ncluded as part 
22d00 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20  of the name..** 
22d10 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  Parameters of th
22d20 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 68 6f  e form "?" witho
22d30 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 20 69  ut a following i
22d40 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f 20 6e  nteger have no n
22d50 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65 20 61  ame.** and are a
22d60 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74 6f 20  lso referred to 
22d70 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 70 61  as "anonymous pa
22d80 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a 2a 2a  rameters"..**.**
22d90 20 54 68 65 20 66 69 72 73 74 20 68 6f 73 74 20   The first host 
22da0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e  parameter has an
22db0 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e 6f 74   index of 1, not
22dc0 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65   0..**.** If the
22dd0 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75 74 20   value n is out 
22de0 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74  of range or if t
22df0 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65 74 65  he n-th paramete
22e00 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65 73 73  r is.** nameless
22e10 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72  , then NULL is r
22e20 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 72 65  eturned.  The re
22e30 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
22e40 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20 55 54  .** always in UT
22e50 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65 76 65  F-8 encoding eve
22e60 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 70  n if the named p
22e70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a 2a 20  arameter was.** 
22e80 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65 63 69  originally speci
22e90 66 69 65 64 20 61 73 20 55 54 46 2d 31 36 20 69  fied as UTF-16 i
22ea0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
22eb0 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73  re16()] or.** [s
22ec0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
22ed0 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65  _v2()]..**.** Se
22ee0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
22ef0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74  _bind_blob|sqlit
22f00 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b  e3_bind()],.** [
22f10 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
22f20 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c  ameter_count()],
22f30 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
22f40 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22f50 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  index()]..**.** 
22f60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
22f70 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53 51 4c   [H13621].*/.SQL
22f80 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
22f90 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64  ar *sqlite3_bind
22fa0 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28  _parameter_name(
22fb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
22fc0 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
22fd0 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 20 41  3REF: Index Of A
22fe0 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 68 20   Parameter With 
22ff0 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b 48 31  A Given Name {H1
23000 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3640} <S70300>.*
23010 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
23020 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20  index of an SQL 
23030 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 6e 20  parameter given 
23040 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 0a 2a  its name.  The.*
23050 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 72 65  * index value re
23060 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 61 62  turned is suitab
23070 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 74 68  le for use as th
23080 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61  e second.** para
23090 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
230a0 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69  3_bind_blob|sqli
230b0 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 41 20  te3_bind()].  A 
230c0 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 75 72  zero.** is retur
230d0 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 68 69  ned if no matchi
230e0 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ng parameter is 
230f0 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 72 61  found.  The para
23100 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 6d 75  meter.** name mu
23110 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e 20 55  st be given in U
23120 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 68 65  TF-8 even if the
23130 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d   original statem
23140 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 70 61  ent.** was prepa
23150 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 36 20  red from UTF-16 
23160 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  text using [sqli
23170 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
23180 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ()]..**.** See a
23190 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
231a0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
231b0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
231c0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
231d0 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e  ter_count()], an
231e0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  d.** [sqlite3_bi
231f0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
23200 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ex()]..**.** Req
23210 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
23220 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13641].*/.SQLITE
23230 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
23240 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
23250 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74  index(sqlite3_st
23260 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  mt*, const char 
23270 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *zName);../*.** 
23280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
23290 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20  All Bindings On 
232a0 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
232b0 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20 3c 53  ment {H13660} <S
232c0 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e  70300>.**.** Con
232d0 74 72 61 72 79 20 74 6f 20 74 68 65 20 69 6e 74  trary to the int
232e0 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20  uition of many, 
232f0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
23300 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 65 74  ] does not reset
23310 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
23320 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 69 6e  _bind_blob | bin
23330 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70 72 65  dings] on a [pre
23340 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
23350 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 72 6f  ..** Use this ro
23360 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74 20 61  utine to reset a
23370 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  ll host paramete
23380 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  rs to NULL..**.*
23390 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
233a0 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f 0a 53  ** [H13661].*/.S
233b0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
233c0 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
233d0 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
233e0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
233f0 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20  3REF: Number Of 
23400 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65 73  Columns In A Res
23410 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30 7d  ult Set {H13710}
23420 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
23430 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
23440 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
23450 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72  the result set r
23460 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a  eturned by the.*
23470 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  * [prepared stat
23480 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f 75  ement]. This rou
23490 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 69  tine returns 0 i
234a0 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53 51  f pStmt is an SQ
234b0 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74  L.** statement t
234c0 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 74  hat does not ret
234d0 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65 78  urn data (for ex
234e0 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54 45  ample an [UPDATE
234f0 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ])..**.** Requir
23500 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37  ements:.** [H137
23510 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  11].*/.SQLITE_AP
23520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
23530 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74  lumn_count(sqlit
23540 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
23550 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
23560 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49  : Column Names I
23570 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b  n A Result Set {
23580 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30 30 3e  H13720} <S10700>
23590 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
235a0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65  tines return the
235b0 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   name assigned t
235c0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63  o a particular c
235d0 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20  olumn.** in the 
235e0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
235f0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23600 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  nt.  The sqlite3
23610 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 0a 2a  _column_name().*
23620 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  * interface retu
23630 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
23640 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
23650 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a  ed UTF-8 string.
23660 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  ** and sqlite3_c
23670 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 72  olumn_name16() r
23680 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
23690 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   to a zero-termi
236a0 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20  nated.** UTF-16 
236b0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 69 72  string.  The fir
236c0 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  st parameter is 
236d0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
236e0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 61 74  atement].** that
236f0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
23700 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23710 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70  nt. The second p
23720 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a  arameter is the.
23730 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ** column number
23740 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20  .  The leftmost 
23750 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65 72  column is number
23760 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65   0..**.** The re
23770 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 70 6f  turned string po
23780 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20 75  inter is valid u
23790 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 65 20  ntil either the 
237a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
237b0 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 74 72  ent].** is destr
237c0 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  oyed by [sqlite3
237d0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
237e0 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63  until the next c
237f0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
23800 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20  3_column_name() 
23810 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
23820 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20 74 68  n_name16() on th
23830 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  e same column..*
23840 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
23850 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64  malloc() fails d
23860 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63 65 73  uring the proces
23870 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72 20 72  sing of either r
23880 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72 20 65  outine.** (for e
23890 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20 61 20  xample during a 
238a0 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20  conversion from 
238b0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 29  UTF-8 to UTF-16)
238c0 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20   then a.** NULL 
238d0 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72  pointer is retur
238e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ned..**.** The n
238f0 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c 74 20  ame of a result 
23900 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 76 61  column is the va
23910 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 22 20  lue of the "AS" 
23920 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68  clause for.** th
23930 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 74 68  at column, if th
23940 65 72 65 20 69 73 20 61 6e 20 41 53 20 63 6c 61  ere is an AS cla
23950 75 73 65 2e 20 20 49 66 20 74 68 65 72 65 20 69  use.  If there i
23960 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65 0a 2a  s no AS clause.*
23970 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20  * then the name 
23980 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  of the column is
23990 20 75 6e 73 70 65 63 69 66 69 65 64 20 61 6e 64   unspecified and
239a0 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d   may change from
239b0 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73 65 20  .** one release 
239c0 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65  of SQLite to the
239d0 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   next..**.** Req
239e0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
239f0 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33 5d 20  13721] [H13723] 
23a00 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37 32 35  [H13724] [H13725
23a10 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31 33 37  ] [H13726] [H137
23a20 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  27].*/.SQLITE_AP
23a30 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
23a40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
23a50 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
23a60 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
23a70 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
23a80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
23a90 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ame16(sqlite3_st
23aa0 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a  mt*, int N);../*
23ab0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 6f  .** CAPI3REF: So
23ac0 75 72 63 65 20 4f 66 20 44 61 74 61 20 49 6e 20  urce Of Data In 
23ad0 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b  A Query Result {
23ae0 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30 30 3e  H13740} <S10700>
23af0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
23b00 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 61 20  tines provide a 
23b10 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 6d 69  means to determi
23b20 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e 20 6f  ne what column o
23b30 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c 65 20  f what.** table 
23b40 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 61 73  in which databas
23b50 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20  e a result of a 
23b60 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23b70 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e 0a 2a  nt comes from..*
23b80 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
23b90 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74 61  e database or ta
23ba0 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 63 61  ble or column ca
23bb0 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73  n be returned as
23bc0 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55 54 46  .** either a UTF
23bd0 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73 74 72  -8 or UTF-16 str
23be0 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74 61 62  ing.  The _datab
23bf0 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20 72 65  ase_ routines re
23c00 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  turn.** the data
23c10 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65 20 5f  base name, the _
23c20 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65 73 20  table_ routines 
23c30 72 65 74 75 72 6e 20 74 68 65 20 74 61 62 6c 65  return the table
23c40 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20 74 68   name, and.** th
23c50 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74 69 6e  e origin_ routin
23c60 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f  es return the co
23c70 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68  lumn name..** Th
23c80 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
23c90 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c  g is valid until
23ca0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
23cb0 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73  tatement] is des
23cc0 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e 67 20  troyed.** using 
23cd0 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
23ce0 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68  e()] or until th
23cf0 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
23d00 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 0a  on is requested.
23d10 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 64 69  ** again in a di
23d20 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
23d30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ..**.** The name
23d40 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 74  s returned are t
23d50 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e 2d 61  he original un-a
23d60 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f 66 20  liased names of 
23d70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2c  the.** database,
23d80 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f 6c 75   table, and colu
23d90 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  mn..**.** The fi
23da0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
23db0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61  the following ca
23dc0 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70 61 72  lls is a [prepar
23dd0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a  ed statement]..*
23de0 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  * These function
23df0 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61  s return informa
23e00 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4e  tion about the N
23e10 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e  th column return
23e20 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 74 61  ed by.** the sta
23e30 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 4e 20  tement, where N 
23e40 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75  is the second fu
23e50 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e  nction argument.
23e60 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4e 74  .**.** If the Nt
23e70 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e 65  h column returne
23e80 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
23e90 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  nt is an express
23ea0 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71 75 65  ion or.** subque
23eb0 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20  ry and is not a 
23ec0 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68  column value, th
23ed0 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20  en all of these 
23ee0 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e  functions return
23ef0 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65 73 65  .** NULL.  These
23f00 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61   routine might a
23f10 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20  lso return NULL 
23f20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
23f30 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a 2a 20  cation error.** 
23f40 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72 77 69  occurs.  Otherwi
23f50 73 65 2c 20 74 68 65 79 20 72 65 74 75 72 6e 20  se, they return 
23f60 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
23f70 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
23f80 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20  e, table.** and 
23f90 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 65 72  column that quer
23fa0 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  y result column 
23fb0 77 61 73 20 65 78 74 72 61 63 74 65 64 20 66 72  was extracted fr
23fc0 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 69 74  om..**.** As wit
23fd0 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51 4c 69  h all other SQLi
23fe0 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65 20 70  te APIs, those p
23ff0 6f 73 74 66 69 78 65 64 20 77 69 74 68 20 22 31  ostfixed with "1
24000 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55 54 46  6" return.** UTF
24010 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74 72 69  -16 encoded stri
24020 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72 20 66  ngs, the other f
24030 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
24040 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  UTF-8. {END}.**.
24050 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20 61 72  ** These APIs ar
24060 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  e only available
24070 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
24080 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
24090 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45  h the.** [SQLITE
240a0 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
240b0 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72  ETADATA] C-prepr
240c0 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64  ocessor symbol d
240d0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41  efined..**.** {A
240e0 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74 77 6f  13751}.** If two
240f0 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73   or more threads
24100 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72   call one or mor
24110 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  e of these routi
24120 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68 65 20  nes against the 
24130 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72 65 64  same.** prepared
24140 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63   statement and c
24150 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73 61 6d  olumn at the sam
24160 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65 20  e time then the 
24170 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a 20 75  results are.** u
24180 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
24190 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
241a0 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33 37 34   [H13741] [H1374
241b0 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48 31 33  2] [H13743] [H13
241c0 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20 5b 48  744] [H13745] [H
241d0 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38 5d 0a  13746] [H13748].
241e0 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20  **.** If two or 
241f0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c  more threads cal
24200 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  l one or more.**
24210 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
24220 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 7c  _database_name |
24230 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 74 61   column metadata
24240 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a 2a 20   interfaces].** 
24250 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b 70 72  for the same [pr
24260 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
24270 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 6f 6c  ] and result col
24280 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  umn.** at the sa
24290 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65  me time then the
242a0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
242b0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
242c0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
242d0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
242e0 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73  _database_name(s
242f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24300 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
24310 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
24320 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
24330 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  e_name16(sqlite3
24340 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
24350 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
24360 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
24370 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71  mn_table_name(sq
24380 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
24390 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
243a0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
243b0 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
243c0 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
243d0 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
243e0 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
243f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
24400 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  rigin_name(sqlit
24410 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
24420 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
24430 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
24440 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
24450 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
24460 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ,int);../*.** CA
24470 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 64  PI3REF: Declared
24480 20 44 61 74 61 74 79 70 65 20 4f 66 20 41 20 51   Datatype Of A Q
24490 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48 31 33  uery Result {H13
244a0 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  760} <S10700>.**
244b0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
244c0 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b 70 72  rameter is a [pr
244d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
244e0 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 73 74  ]..** If this st
244f0 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 53 45  atement is a [SE
24500 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20  LECT] statement 
24510 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  and the Nth colu
24520 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 74  mn of the.** ret
24530 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 65 74  urned result set
24540 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 43 54   of that [SELECT
24550 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 6f 6c  ] is a table col
24560 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a 20 65  umn (not an.** e
24570 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75 62  xpression or sub
24580 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20  query) then the 
24590 64 65 63 6c 61 72 65 64 20 74 79 70 65 20 6f 66  declared type of
245a0 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 6f   the table.** co
245b0 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64  lumn is returned
245c0 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f  .  If the Nth co
245d0 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75  lumn of the resu
245e0 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a 2a 20  lt set is an.** 
245f0 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75  expression or su
24600 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61 20 4e  bquery, then a N
24610 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72  ULL pointer is r
24620 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20  eturned..** The 
24630 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
24640 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 20  is always UTF-8 
24650 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d 0a 2a  encoded. {END}.*
24660 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
24670 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61 74 61  , given the data
24680 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a  base schema:.**.
24690 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
246a0 74 31 28 63 31 20 56 41 52 49 41 4e 54 29 3b 0a  t1(c1 VARIANT);.
246b0 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 6f  **.** and the fo
246c0 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
246d0 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64  t to be compiled
246e0 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 20 63  :.**.** SELECT c
246f0 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d 20 74  1 + 1, c1 FROM t
24700 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 72 6f  1;.**.** this ro
24710 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 74 75  utine would retu
24720 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20 22 56  rn the string "V
24730 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68 65 20  ARIANT" for the 
24740 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a 2a 2a  second result.**
24750 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 2c 20   column (i==1), 
24760 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  and a NULL point
24770 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  er for the first
24780 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 28   result column (
24790 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  i==0)..**.** SQL
247a0 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63  ite uses dynamic
247b0 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69 6e 67   run-time typing
247c0 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63 61 75  .  So just becau
247d0 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69  se a column.** i
247e0 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20 63 6f  s declared to co
247f0 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63 75 6c  ntain a particul
24800 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e 6f 74  ar type does not
24810 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 0a 2a   mean that the.*
24820 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e  * data stored in
24830 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20   that column is 
24840 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  of the declared 
24850 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20 69 73  type.  SQLite is
24860 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74 79 70  .** strongly typ
24870 65 64 2c 20 62 75 74 20 74 68 65 20 74 79 70 69  ed, but the typi
24880 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20 6e 6f  ng is dynamic no
24890 74 20 73 74 61 74 69 63 2e 20 20 54 79 70 65 0a  t static.  Type.
248a0 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  ** is associated
248b0 20 77 69 74 68 20 69 6e 64 69 76 69 64 75 61 6c   with individual
248c0 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77 69 74   values, not wit
248d0 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 73  h the containers
248e0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  .** used to hold
248f0 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e 0a 2a   those values..*
24900 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
24910 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d 20 5b  s:.** [H13761] [
24920 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36 33 5d  H13762] [H13763]
24930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
24940 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
24950 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
24960 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  pe(sqlite3_stmt*
24970 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,int);.SQLITE_AP
24980 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
24990 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
249a0 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f  ltype16(sqlite3_
249b0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a  stmt*,int);../*.
249c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61  ** CAPI3REF: Eva
249d0 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61  luate An SQL Sta
249e0 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20  tement {H13200} 
249f0 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41  <S10000>.**.** A
24a00 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64  fter a [prepared
24a10 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20   statement] has 
24a20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73  been prepared us
24a30 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73  ing either.** [s
24a40 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
24a50 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  2()] or [sqlite3
24a60 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
24a70 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   or one of the l
24a80 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61  egacy.** interfa
24a90 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  ces [sqlite3_pre
24aa0 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69  pare()] or [sqli
24ab0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
24ac0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  , this function.
24ad0 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ** must be calle
24ae0 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69  d one or more ti
24af0 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20  mes to evaluate 
24b00 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  the statement..*
24b10 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73  *.** The details
24b20 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72   of the behavior
24b30 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
24b40 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 63 65  step() interface
24b50 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68   depend.** on wh
24b60 65 74 68 65 72 20 74 68 65 20 73 74 61 74 65 6d  ether the statem
24b70 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 64  ent was prepared
24b80 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72   using the newer
24b90 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a   "v2" interface.
24ba0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
24bb0 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73  are_v2()] and [s
24bc0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
24bd0 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c  _v2()] or the ol
24be0 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e  der legacy.** in
24bf0 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33  terface [sqlite3
24c00 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20  _prepare()] and 
24c10 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
24c20 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20  16()].  The use 
24c30 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76  of the.** new "v
24c40 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  2" interface is 
24c50 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
24c60 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  new applications
24c70 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a   but the legacy.
24c80 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  ** interface wil
24c90 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65  l continue to be
24ca0 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a   supported..**.*
24cb0 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20  * In the legacy 
24cc0 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 72  interface, the r
24cd0 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c  eturn value will
24ce0 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49   be either [SQLI
24cf0 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51  TE_BUSY],.** [SQ
24d00 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c  LITE_DONE], [SQL
24d10 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54  ITE_ROW], [SQLIT
24d20 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51  E_ERROR], or [SQ
24d30 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a  LITE_MISUSE]..**
24d40 20 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69   With the "v2" i
24d50 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66  nterface, any of
24d60 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75   the other [resu
24d70 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20  lt codes] or.** 
24d80 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74  [extended result
24d90 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65   codes] might be
24da0 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c   returned as wel
24db0 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  l..**.** [SQLITE
24dc0 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61  _BUSY] means tha
24dd0 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65  t the database e
24de0 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65  ngine was unable
24df0 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a   to acquire the.
24e00 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b  ** database lock
24e10 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f  s it needs to do
24e20 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68   its job.  If th
24e30 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  e statement is a
24e40 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20   [COMMIT].** or 
24e50 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f  occurs outside o
24e60 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72  f an explicit tr
24e70 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
24e80 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74 68  you can retry th
24e90 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  e.** statement. 
24ea0 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   If the statemen
24eb0 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d  t is not a [COMM
24ec0 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77  IT] and occurs w
24ed0 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69  ithin a.** expli
24ee0 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  cit transaction 
24ef0 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20  then you should 
24f00 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
24f10 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a  nsaction before.
24f20 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a  ** continuing..*
24f30 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  *.** [SQLITE_DON
24f40 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  E] means that th
24f50 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  e statement has 
24f60 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69  finished executi
24f70 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  ng.** successful
24f80 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65  ly.  sqlite3_ste
24f90 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  p() should not b
24fa0 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f  e called again o
24fb0 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a  n this virtual.*
24fc0 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75  * machine withou
24fd0 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20  t first calling 
24fe0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
24ff0 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76  ] to reset the v
25000 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
25010 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e  e back to its in
25020 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a  itial state..**.
25030 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74  ** If the SQL st
25040 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78  atement being ex
25050 65 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61  ecuted returns a
25060 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53  ny data, then [S
25070 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73  QLITE_ROW].** is
25080 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74   returned each t
25090 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66  ime a new row of
250a0 20 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66   data is ready f
250b0 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79  or processing by
250c0 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20   the.** caller. 
250d0 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62  The values may b
250e0 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67  e accessed using
250f0 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63   the [column acc
25100 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a  ess functions]..
25110 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ** sqlite3_step(
25120 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69  ) is called agai
25130 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
25140 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61  e next row of da
25150 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54  ta..**.** [SQLIT
25160 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74  E_ERROR] means t
25170 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65  hat a run-time e
25180 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20  rror (such as a 
25190 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69  constraint.** vi
251a0 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63  olation) has occ
251b0 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f  urred.  sqlite3_
251c0 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f  step() should no
251d0 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69  t be called agai
251e0 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20  n on.** the VM. 
251f0 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  More information
25200 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79   may be found by
25210 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
25220 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20  3_errmsg()]..** 
25230 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20  With the legacy 
25240 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72  interface, a mor
25250 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72  e specific error
25260 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70   code (for examp
25270 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49  le,.** [SQLITE_I
25280 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49  NTERRUPT], [SQLI
25290 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c  TE_SCHEMA], [SQL
252a0 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e  ITE_CORRUPT], an
252b0 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63  d so forth).** c
252c0 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62  an be obtained b
252d0 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  y calling [sqlit
252e0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74  e3_reset()] on t
252f0 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
25300 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20  statement].  In 
25310 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
25320 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20  ce,.** the more 
25330 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63  specific error c
25340 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
25350 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69  directly by sqli
25360 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a  te3_step()..**.*
25370 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  * [SQLITE_MISUSE
25380 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
25390 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
253a0 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f  s called inappro
253b0 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72  priately..** Per
253c0 68 61 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c  haps it was call
253d0 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65  ed on a [prepare
253e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61  d statement] tha
253f0 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  t has.** already
25400 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66   been [sqlite3_f
25410 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69  inalize | finali
25420 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74  zed] or on one t
25430 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69  hat had.** previ
25440 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b  ously returned [
25450 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72  SQLITE_ERROR] or
25460 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20   [SQLITE_DONE]. 
25470 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20   Or it could.** 
25480 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
25490 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
254a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
254b0 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74   being used by t
254c0 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68  wo or.** more th
254d0 72 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d  reads at the sam
254e0 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65  e moment in time
254f0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79  ..**.** <b>Goofy
25500 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74   Interface Alert
25510 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67  :</b> In the leg
25520 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74  acy interface, t
25530 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  he sqlite3_step(
25540 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20  ).** API always 
25550 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69  returns a generi
25560 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53  c error code, [S
25570 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f  QLITE_ERROR], fo
25580 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65  llowing any.** e
25590 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20  rror other than 
255a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e  [SQLITE_BUSY] an
255b0 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  d [SQLITE_MISUSE
255c0 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c  ].  You must cal
255d0 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  l.** [sqlite3_re
255e0 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  set()] or [sqlit
255f0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69  e3_finalize()] i
25600 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20  n order to find 
25610 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70  one of the.** sp
25620 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f  ecific [error co
25630 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 65 72  des] that better
25640 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65   describes the e
25650 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69  rror..** We admi
25660 74 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61  t that this is a
25670 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20   goofy design.  
25680 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20  The problem has 
25690 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69  been fixed.** wi
256a0 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 65  th the "v2" inte
256b0 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70  rface.  If you p
256c0 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f  repare all of yo
256d0 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ur SQL statement
256e0 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65  s.** using eithe
256f0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
25700 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c  re_v2()] or [sql
25710 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
25720 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20  2()] instead.** 
25730 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73  of the legacy [s
25740 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
25750 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70  ] and [sqlite3_p
25760 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65  repare16()] inte
25770 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20  rfaces,.** then 
25780 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69  the more specifi
25790 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20  c [error codes] 
257a0 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 69 72  are returned dir
257b0 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69  ectly.** by sqli
257c0 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65  te3_step().  The
257d0 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 32 22   use of the "v2"
257e0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65   interface is re
257f0 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a  commended..**.**
25800 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
25810 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31 35 33  * [H13202] [H153
25820 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b 48 31  04] [H15306] [H1
25830 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d 0a 2a  5308] [H15310].*
25840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
25850 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
25860 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
25870 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
25880 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
25890 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73 65 74   in a result set
258a0 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30 37 30   {H13770} <S1070
258b0 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73  0>.**.** Returns
258c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76   the number of v
258d0 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 75 72  alues in the cur
258e0 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
258f0 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a  result set..**.*
25900 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
25910 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48 31 33  ** [H13771] [H13
25920 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  772].*/.SQLITE_A
25930 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
25940 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ata_count(sqlite
25950 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a  3_stmt *pStmt);.
25960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
25970 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74   Fundamental Dat
25980 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20  atypes {H10265} 
25990 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e  <S10110><S10120>
259a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51  .** KEYWORDS: SQ
259b0 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20  LITE_TEXT.**.** 
259c0 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76  {H10266} Every v
259d0 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68  alue in SQLite h
259e0 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66  as one of five f
259f0 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74  undamental datat
25a00 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ypes:.**.** <ul>
25a10 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
25a20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
25a30 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45  * <li> 64-bit IE
25a40 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  EE floating poin
25a50 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e  t number.** <li>
25a60 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20   string.** <li> 
25a70 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c  BLOB.** <li> NUL
25a80 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d  L.** </ul> {END}
25a90 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
25aa0 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73  stants are codes
25ab0 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f   for each of tho
25ac0 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  se types..**.** 
25ad0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51  Note that the SQ
25ae0 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61  LITE_TEXT consta
25af0 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64  nt was also used
25b00 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   in SQLite versi
25b10 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f  on 2.** for a co
25b20 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65  mpletely differe
25b30 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66  nt meaning.  Sof
25b40 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73  tware that links
25b50 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a   against both.**
25b60 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
25b70 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72  2 and SQLite ver
25b80 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73  sion 3 should us
25b90 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20  e SQLITE3_TEXT, 
25ba0 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45  not.** SQLITE_TE
25bb0 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  XT..*/.#define S
25bc0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31  QLITE_INTEGER  1
25bd0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
25be0 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69  FLOAT    2.#defi
25bf0 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20  ne SQLITE_BLOB  
25c00 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
25c10 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23  ITE_NULL     5.#
25c20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58  ifdef SQLITE_TEX
25c30 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  T.# undef SQLITE
25c40 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65  _TEXT.#else.# de
25c50 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54  fine SQLITE_TEXT
25c60 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64       3.#endif.#d
25c70 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45  efine SQLITE3_TE
25c80 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20  XT     3../*.** 
25c90 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74  CAPI3REF: Result
25ca0 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51   Values From A Q
25cb0 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53  uery {H13800} <S
25cc0 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10700>.** KEYWOR
25cd0 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65  DS: {column acce
25ce0 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a  ss functions}.**
25cf0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
25d00 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73  es form the "res
25d10 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69  ult set query" i
25d20 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
25d30 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
25d40 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  eturn informatio
25d50 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65  n about a single
25d60 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63   column of the c
25d70 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74  urrent.** result
25d80 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e   row of a query.
25d90 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20    In every case 
25da0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
25db0 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a  nt is a pointer.
25dc0 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61  ** to the [prepa
25dd0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
25de0 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61  hat is being eva
25df0 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c  luated (the [sql
25e00 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74  ite3_stmt*].** t
25e10 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
25e20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70   from [sqlite3_p
25e30 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
25e40 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61  one of its varia
25e50 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nts).** and the 
25e60 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
25e70 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
25e80 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77  the column for w
25e90 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hich information
25ea0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65  .** should be re
25eb0 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66  turned.  The lef
25ec0 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  tmost column of 
25ed0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68  the result set h
25ee0 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a  as the index 0..
25ef0 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
25f00 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
25f10 72 65 73 75 6c 74 20 63 61 6e 20 62 65 20 64 65  result can be de
25f20 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 0a 2a  termined using.*
25f30 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
25f40 6e 5f 63 6f 75 6e 74 28 29 5d 2e 0a 2a 2a 0a 2a  n_count()]..**.*
25f50 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61  * If the SQL sta
25f60 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20  tement does not 
25f70 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20  currently point 
25f80 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20  to a valid row, 
25f90 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c  or if the.** col
25fa0 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74  umn index is out
25fb0 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72   of range, the r
25fc0 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e  esult is undefin
25fd0 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  ed..** These rou
25fe0 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62  tines may only b
25ff0 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
26000 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61  e most recent ca
26010 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
26020 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65  3_step()] has re
26030 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52  turned [SQLITE_R
26040 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a  OW] and neither.
26050 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ** [sqlite3_rese
26060 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65  t()] nor [sqlite
26070 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61  3_finalize()] ha
26080 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73  ve been called s
26090 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20  ubsequently..** 
260a0 49 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  If any of these 
260b0 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c  routines are cal
260c0 6c 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74  led after [sqlit
260d0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
260e0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
260f0 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20  ize()] or after 
26100 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
26110 20 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a   has returned.**
26120 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72   something other
26130 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f   than [SQLITE_RO
26140 57 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20  W], the results 
26150 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
26160 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74  * If [sqlite3_st
26170 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  ep()] or [sqlite
26180 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73  3_reset()] or [s
26190 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
261a0 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64  )].** are called
261b0 20 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e   from a differen
261c0 74 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61  t thread while a
261d0 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ny of these rout
261e0 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64  ines.** are pend
261f0 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ing, then the re
26200 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69  sults are undefi
26210 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ned..**.** The s
26220 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
26230 70 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74  pe() routine ret
26240 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  urns the.** [SQL
26250 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61  ITE_INTEGER | da
26260 74 61 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72  tatype code] for
26270 20 74 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74   the initial dat
26280 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65  a type.** of the
26290 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20   result column. 
262a0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61   The returned va
262b0 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53  lue is one of [S
262c0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a  QLITE_INTEGER],.
262d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ** [SQLITE_FLOAT
262e0 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d  ], [SQLITE_TEXT]
262f0 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c  , [SQLITE_BLOB],
26300 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c   or [SQLITE_NULL
26310 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a  ].  The value.**
26320 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
26330 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
26340 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69  () is only meani
26350 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65  ngful if no type
26360 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  .** conversions 
26370 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 61 73  have occurred as
26380 20 64 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77   described below
26390 2e 20 20 41 66 74 65 72 20 61 20 74 79 70 65 20  .  After a type 
263a0 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74  conversion,.** t
263b0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
263c0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
263d0 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e  umn_type() is un
263e0 64 65 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65  defined.  Future
263f0 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  .** versions of 
26400 53 51 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67  SQLite may chang
26410 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  e the behavior o
26420 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  f sqlite3_column
26430 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f  _type().** follo
26440 77 69 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76  wing a type conv
26450 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  ersion..**.** If
26460 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
26470 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73   BLOB or UTF-8 s
26480 74 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73  tring then the s
26490 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
264a0 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  tes().** routine
264b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
264c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
264d0 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72  that BLOB or str
264e0 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72  ing..** If the r
264f0 65 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31  esult is a UTF-1
26500 36 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73  6 string, then s
26510 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
26520 74 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a  tes() converts.*
26530 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20  * the string to 
26540 55 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72  UTF-8 and then r
26550 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
26560 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49  r of bytes..** I
26570 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
26580 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
26590 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
265a0 75 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73  umn_bytes() uses
265b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70  .** [sqlite3_snp
265c0 72 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76  rintf()] to conv
265d0 65 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74  ert that value t
265e0 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  o a UTF-8 string
265f0 20 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20   and returns.** 
26600 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
26610 74 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69  tes in that stri
26620 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  ng..** The value
26630 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e   returned does n
26640 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a  ot include the z
26650 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61  ero terminator a
26660 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  t the end.** of 
26670 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  the string.  For
26680 20 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61   clarity: the va
26690 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
266a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
266b0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74   bytes in the st
266c0 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75  ring, not the nu
266d0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
266e0 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67  rs..**.** String
266f0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  s returned by sq
26700 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
26710 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
26720 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c  column_text16(),
26730 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73  .** even empty s
26740 74 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61  trings, are alwa
26750 79 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  ys zero terminat
26760 65 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a  ed.  The return.
26770 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  ** value from sq
26780 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
26790 62 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c  b() for a zero-l
267a0 65 6e 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e  ength BLOB is an
267b0 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f   arbitrary.** po
267c0 69 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20  inter, possibly 
267d0 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  even a NULL poin
267e0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ter..**.** The s
267f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
26800 74 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20  tes16() routine 
26810 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71  is similar to sq
26820 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
26830 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76  es().** but leav
26840 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  es the result in
26850 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76   UTF-16 in nativ
26860 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73  e byte order ins
26870 74 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a  tead of UTF-8..*
26880 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69  * The zero termi
26890 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63  nator is not inc
268a0 6c 75 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f  luded in this co
268b0 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  unt..**.** The o
268c0 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62  bject returned b
268d0 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
268e0 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e  n_value()] is an
268f0 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  .** [unprotected
26900 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
26910 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72  object.  An unpr
26920 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
26930 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20  value object.** 
26940 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
26950 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62   with [sqlite3_b
26960 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64  ind_value()] and
26970 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
26980 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66  _value()]..** If
26990 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65   the [unprotecte
269a0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
269b0 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64   object returned
269c0 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   by.** [sqlite3_
269d0 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20  column_value()] 
269e0 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f  is used in any o
269f0 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64  ther way, includ
26a00 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20  ing calls.** to 
26a10 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73  routines like [s
26a20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
26a30 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
26a40 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20  lue_text()],.** 
26a50 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  or [sqlite3_valu
26a60 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e  e_bytes()], then
26a70 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
26a80 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
26a90 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
26aa0 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76   attempt to conv
26ab0 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68  ert the value wh
26ac0 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e  ere appropriate.
26ad0 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
26ae0 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61  , if the interna
26af0 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
26b00 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20   is FLOAT and a 
26b10 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69  text result.** i
26b20 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71  s requested, [sq
26b30 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
26b40 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e  ] is used intern
26b50 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20  ally to perform 
26b60 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  the.** conversio
26b70 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  n automatically.
26b80 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
26b90 74 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68  table details th
26ba0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a  e conversions.**
26bb0 20 74 68 61 74 20 61 72 65 20 61 70 70 6c 69 65   that are applie
26bc0 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  d:.**.** <blockq
26bd0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20  uote>.** <table 
26be0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c  border="1">.** <
26bf0 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c  tr><th> Internal
26c00 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65  <br>Type <th> Re
26c10 71 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20  quested<br>Type 
26c20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e  <th>  Conversion
26c30 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  .**.** <tr><td> 
26c40 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e   NULL    <td> IN
26c50 54 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73  TEGER   <td> Res
26c60 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e  ult is 0.** <tr>
26c70 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74  <td>  NULL    <t
26c80 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64  d>  FLOAT    <td
26c90 3e 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a  > Result is 0.0.
26ca0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
26cb0 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  L    <td>   TEXT
26cc0 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26cd0 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
26ce0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
26cf0 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  L    <td>   BLOB
26d00 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26d10 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
26d20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45  ** <tr><td> INTE
26d30 47 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  GER  <td>  FLOAT
26d40 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74      <td> Convert
26d50 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f   from integer to
26d60 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74   float.** <tr><t
26d70 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26d80 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20     TEXT    <td> 
26d90 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20  ASCII rendering 
26da0 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
26db0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
26dc0 45 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20  ER  <td>   BLOB 
26dd0 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20     <td> Same as 
26de0 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a  INTEGER->TEXT.**
26df0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
26e00 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
26e10 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66    <td> Convert f
26e20 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74  rom float to int
26e30 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  eger.** <tr><td>
26e40 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20    FLOAT   <td>  
26e50 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53   TEXT    <td> AS
26e60 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66  CII rendering of
26e70 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74   the float.** <t
26e80 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
26e90 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
26ea0 74 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41  td> Same as FLOA
26eb0 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c  T->TEXT.** <tr><
26ec0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64  td>  TEXT    <td
26ed0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e  > INTEGER   <td>
26ee0 20 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   Use atoi().** <
26ef0 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20  tr><td>  TEXT   
26f00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
26f10 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a  <td> Use atof().
26f20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
26f30 54 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  T    <td>   BLOB
26f40 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e      <td> No chan
26f50 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ge.** <tr><td>  
26f60 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54  BLOB    <td> INT
26f70 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76  EGER   <td> Conv
26f80 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e  ert to TEXT then
26f90 20 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   use atoi().** <
26fa0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
26fb0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
26fc0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20  <td> Convert to 
26fd0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74  TEXT then use at
26fe0 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  of().** <tr><td>
26ff0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20    BLOB    <td>  
27000 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64   TEXT    <td> Ad
27010 64 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  d a zero termina
27020 74 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a  tor if needed.**
27030 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62   </table>.** </b
27040 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
27050 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65   The table above
27060 20 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65   makes reference
27070 20 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c   to standard C l
27080 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
27090 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61   atoi().** and a
270a0 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64  tof().  SQLite d
270b0 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75  oes not really u
270c0 73 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  se these functio
270d0 6e 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a  ns.  It has its.
270e0 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e  ** own equivalen
270f0 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69  t internal routi
27100 6e 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29  nes.  The atoi()
27110 20 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65   and atof() name
27120 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e  s are.** used in
27130 20 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62   the table for b
27140 72 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75  revity and becau
27150 73 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69  se they are fami
27160 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20  liar to most.** 
27170 43 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a  C programmers..*
27180 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77  *.** Note that w
27190 68 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73  hen type convers
271a0 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e  ions occur, poin
271b0 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79  ters returned by
271c0 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20   prior.** calls 
271d0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
271e0 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65  n_blob(), sqlite
271f0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c  3_column_text(),
27200 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74   and/or.** sqlit
27210 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27220 28 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69  () may be invali
27230 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63  dated..** Type c
27240 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70  onversions and p
27250 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74  ointer invalidat
27260 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72  ions might occur
27270 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  .** in the follo
27280 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a  wing cases:.**.*
27290 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54  * <ul>.** <li> T
272a0 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
272b0 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64  nt is a BLOB and
272c0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
272d0 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20  text() or.**    
272e0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
272f0 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c  _text16() is cal
27300 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72  led.  A zero-ter
27310 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a  minator might.**
27320 20 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65        need to be
27330 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 74   added to the st
27340 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c  ring.</li>.** <l
27350 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63  i> The initial c
27360 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20  ontent is UTF-8 
27370 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33  text and sqlite3
27380 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
27390 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c  ) or.**      sql
273a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
273b0 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  16() is called. 
273c0 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73   The content mus
273d0 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a  t be converted.*
273e0 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36  *      to UTF-16
273f0 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54  .</li>.** <li> T
27400 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
27410 6e 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78  nt is UTF-16 tex
27420 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f  t and sqlite3_co
27430 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a  lumn_bytes() or.
27440 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  **      sqlite3_
27450 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73  column_text() is
27460 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f   called.  The co
27470 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f  ntent must be co
27480 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20  nverted.**      
27490 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a  to UTF-8.</li>.*
274a0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f  * </ul>.**.** Co
274b0 6e 76 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65  nversions betwee
274c0 6e 20 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55  n UTF-16be and U
274d0 54 46 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61  TF-16le are alwa
274e0 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65  ys done in place
274f0 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69   and do.** not i
27500 6e 76 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f  nvalidate a prio
27510 72 20 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67  r pointer, thoug
27520 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20  h of course the 
27530 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62  content of the b
27540 75 66 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68  uffer.** that th
27550 65 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20  e prior pointer 
27560 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68  points to will h
27570 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
27580 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a  d.  Other kinds.
27590 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e  ** of conversion
275a0 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61   are done in pla
275b0 63 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f  ce when it is po
275c0 73 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65  ssible, but some
275d0 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72  times they.** ar
275e0 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61  e not possible a
275f0 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65  nd in those case
27600 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73  s prior pointers
27610 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64   are invalidated
27620 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65  ..**.** The safe
27630 73 74 20 61 6e 64 20 65 61 73 69 65 73 74 20 74  st and easiest t
27640 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63  o remember polic
27650 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74  y is to invoke t
27660 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hese routines.**
27670 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   in one of the f
27680 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a  ollowing ways:.*
27690 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c  *.** <ul>.**  <l
276a0 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  i>sqlite3_column
276b0 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64  _text() followed
276c0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
276d0 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a  mn_bytes()</li>.
276e0 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
276f0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f  column_blob() fo
27700 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
27710 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
27720 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71  </li>.**  <li>sq
27730 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
27740 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  t16() followed b
27750 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
27760 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a  _bytes16()</li>.
27770 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49  ** </ul>.**.** I
27780 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79  n other words, y
27790 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73  ou should call s
277a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
277b0 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  xt(),.** sqlite3
277c0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20  _column_blob(), 
277d0 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
277e0 6e 5f 74 65 78 74 31 36 28 29 20 66 69 72 73 74  n_text16() first
277f0 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65   to force the re
27800 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  sult.** into the
27810 20 64 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c   desired format,
27820 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c   then invoke sql
27830 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
27840 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  s() or.** sqlite
27850 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
27860 28 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73  () to find the s
27870 69 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ize of the resul
27880 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63  t.  Do not mix c
27890 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  alls.** to sqlit
278a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
278b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   or sqlite3_colu
278c0 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63  mn_blob() with c
278d0 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  alls to.** sqlit
278e0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
278f0 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20  6(), and do not 
27900 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  mix calls to sql
27910 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27920 31 36 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c  16().** with cal
27930 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
27940 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a  lumn_bytes()..**
27950 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73  .** The pointers
27960 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 76 61   returned are va
27970 6c 69 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65  lid until a type
27980 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75   conversion occu
27990 72 73 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62  rs as.** describ
279a0 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74  ed above, or unt
279b0 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  il [sqlite3_step
279c0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
279d0 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b  reset()] or.** [
279e0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
279f0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  ()] is called.  
27a00 54 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65  The memory space
27a10 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74   used to hold st
27a20 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f  rings.** and BLO
27a30 42 73 20 69 73 20 66 72 65 65 64 20 61 75 74 6f  Bs is freed auto
27a40 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c  matically.  Do <
27a50 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74  b>not</b> pass t
27a60 68 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  he pointers retu
27a70 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rned.** [sqlite3
27a80 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c  _column_blob()],
27a90 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
27aa0 5f 74 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69  _text()], etc. i
27ab0 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  nto.** [sqlite3_
27ac0 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  free()]..**.** I
27ad0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
27ae0 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75  ation error occu
27af0 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76  rs during the ev
27b00 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a  aluation of any.
27b10 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ** of these rout
27b20 69 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20  ines, a default 
27b30 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
27b40 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  d.  The default 
27b50 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68  value.** is eith
27b60 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 20 30  er the integer 0
27b70 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70  , the floating p
27b80 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c  oint number 0.0,
27b90 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f   or a NULL.** po
27ba0 69 6e 74 65 72 2e 20 20 53 75 62 73 65 71 75 65  inter.  Subseque
27bb0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  nt calls to [sql
27bc0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20  ite3_errcode()] 
27bd0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b  will return.** [
27be0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a  SQLITE_NOMEM]..*
27bf0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
27c00 73 3a 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b  s:.** [H13803] [
27c10 48 31 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d  H13806] [H13809]
27c20 20 5b 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31   [H13812] [H1381
27c30 35 5d 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33  5] [H13818] [H13
27c40 38 32 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a  821] [H13824].**
27c50 20 5b 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33   [H13827] [H1383
27c60 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0].*/.SQLITE_API
27c70 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
27c80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
27c90 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27ca0 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27cb0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27cc0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
27cd0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27ce0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27cf0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27d00 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73  column_bytes16(s
27d10 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27d20 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27d30 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
27d40 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65  e3_column_double
27d50 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27d60 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27d70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27d80 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c  3_column_int(sql
27d90 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27da0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27db0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
27dc0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
27dd0 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d  nt64(sqlite3_stm
27de0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27df0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
27e00 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
27e10 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
27e20 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  xt(sqlite3_stmt*
27e30 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
27e40 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
27e50 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
27e60 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  mn_text16(sqlite
27e70 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
27e80 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
27e90 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
27ea0 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  n_type(sqlite3_s
27eb0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27ec0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
27ed0 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74  te3_value *sqlit
27ee0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
27ef0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
27f00 6e 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a  nt iCol);../*.**
27f10 20 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72   CAPI3REF: Destr
27f20 6f 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74  oy A Prepared St
27f30 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b  atement Object {
27f40 48 31 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e  H13300} <S70300>
27f50 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30100>.**.** T
27f60 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  he sqlite3_final
27f70 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ize() function i
27f80 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65  s called to dele
27f90 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  te a [prepared s
27fa0 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66  tatement]..** If
27fb0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77   the statement w
27fc0 61 73 20 65 78 65 63 75 74 65 64 20 73 75 63 63  as executed succ
27fd0 65 73 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20  essfully or not 
27fe0 65 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c  executed at all,
27ff0 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f   then.** SQLITE_
28000 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
28010 49 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  If execution of 
28020 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61  the statement fa
28030 69 6c 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20  iled then an.** 
28040 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20  [error code] or 
28050 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
28060 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
28070 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
28080 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c  utine can be cal
28090 6c 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74  led at any point
280a0 20 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63   during the exec
280b0 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ution of the.** 
280c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
280d0 65 6e 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69  ent].  If the vi
280e0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
280f0 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74  s not.** complet
28100 65 64 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65  ed execution whe
28110 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
28120 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69  s called, that i
28130 73 20 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e  s like.** encoun
28140 74 65 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20  tering an error 
28150 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69  or an [sqlite3_i
28160 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72  nterrupt | inter
28170 72 75 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70  rupt]..** Incomp
28180 6c 65 74 65 20 75 70 64 61 74 65 73 20 6d 61 79  lete updates may
28190 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
281a0 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  and transactions
281b0 20 63 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65   canceled,.** de
281c0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63  pending on the c
281d0 69 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e  ircumstances, an
281e0 64 20 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20  d the.** [error 
281f0 63 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77  code] returned w
28200 69 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  ill be [SQLITE_A
28210 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  BORT]..**.** Req
28220 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
28230 31 31 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a  11302] [H11304].
28240 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28250 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  t sqlite3_finali
28260 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ze(sqlite3_stmt 
28270 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
28280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
28290 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
282a0 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33  ment Object {H13
282b0 33 33 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a  330} <S70300>.**
282c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
282d0 72 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e  reset() function
282e0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
282f0 73 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20  set a [prepared 
28300 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62  statement].** ob
28310 6a 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73  ject back to its
28320 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20   initial state, 
28330 72 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65  ready to be re-e
28340 78 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20  xecuted..** Any 
28350 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61  SQL statement va
28360 72 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64  riables that had
28370 20 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f   values bound to
28380 20 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74   them using.** t
28390 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  he [sqlite3_bind
283a0 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f  _blob | sqlite3_
283b0 62 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65  bind_*() API] re
283c0 74 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65  tain their value
283d0 73 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74  s..** Use [sqlit
283e0 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  e3_clear_binding
283f0 73 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68  s()] to reset th
28400 65 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a  e bindings..**.*
28410 2a 20 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b  * {H11332} The [
28420 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29  sqlite3_reset(S)
28430 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65  ] interface rese
28440 74 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  ts the [prepared
28450 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a   statement] S.**
28460 20 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74            back t
28470 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
28480 6f 66 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a  of its program..
28490 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49  **.** {H11334} I
284a0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
284b0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
284c0 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20  e3_step(S)] for 
284d0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
284e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
284f0 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20  ent] S returned 
28500 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20  [SQLITE_ROW] or 
28510 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a  [SQLITE_DONE],.*
28520 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 69 66  *          or if
28530 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
28540 29 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65 66  )] has never bef
28550 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ore been called 
28560 6f 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20  on S,.**        
28570 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f    then [sqlite3_
28580 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e  reset(S)] return
28590 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  s [SQLITE_OK]..*
285a0 2a 0a 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66  *.** {H11336} If
285b0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
285c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
285d0 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74  3_step(S)] for t
285e0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
285f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28600 6e 74 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20  nt] S indicated 
28610 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a  an error, then.*
28620 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
28630 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65  te3_reset(S)] re
28640 74 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72  turns an appropr
28650 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65  iate [error code
28660 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38  ]..**.** {H11338
28670 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
28680 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61  eset(S)] interfa
28690 63 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e  ce does not chan
286a0 67 65 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a  ge the values.**
286b0 20 20 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79            of any
286c0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
286d0 6c 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e  lob|bindings] on
286e0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
286f0 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a  tatement] S..*/.
28700 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28710 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c  qlite3_reset(sql
28720 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
28730 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
28740 45 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65  EF: Create Or Re
28750 64 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74  define SQL Funct
28760 69 6f 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53  ions {H16100} <S
28770 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20200>.** KEYWOR
28780 44 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72  DS: {function cr
28790 65 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d  eation routines}
287a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61  .** KEYWORDS: {a
287b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
287c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d  ed SQL function}
287d0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61  .** KEYWORDS: {a
287e0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
287f0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  ed SQL functions
28800 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77  }.**.** These tw
28810 6f 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c  o functions (col
28820 6c 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20  lectively known 
28830 61 73 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65  as "function cre
28840 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29  ation routines")
28850 0a 2a 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20  .** are used to 
28860 61 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  add SQL function
28870 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 20  s or aggregates 
28880 6f 72 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74  or to redefine t
28890 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f  he behavior.** o
288a0 66 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66  f existing SQL f
288b0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72  unctions or aggr
288c0 65 67 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c  egates.  The onl
288d0 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  y difference bet
288e0 77 65 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20  ween the.** two 
288f0 69 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f  is that the seco
28900 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68  nd parameter, th
28910 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73  e name of the (s
28920 63 61 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20  calar) function 
28930 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c  or.** aggregate,
28940 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55   is encoded in U
28950 54 46 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33  TF-8 for sqlite3
28960 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
28970 28 29 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a  () and UTF-16.**
28980 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65   for sqlite3_cre
28990 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
289a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
289b0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
289c0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
289d0 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63  nection] to whic
289e0 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e  h the SQL.** fun
289f0 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61  ction is to be a
28a00 64 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67  dded.  If a sing
28a10 6c 65 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20  le program uses 
28a20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61  more than one da
28a30 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
28a40 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c  tion internally,
28a50 20 74 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69   then SQL functi
28a60 6f 6e 73 20 6d 75 73 74 20 62 65 20 61 64 64 65  ons must be adde
28a70 64 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74  d individually t
28a80 6f 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61  o.** each databa
28a90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
28aa0 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
28ab0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
28ac0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c   name of the SQL
28ad0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
28ae0 63 72 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65  created or.** re
28af0 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65  defined.  The le
28b00 6e 67 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65  ngth of the name
28b10 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32   is limited to 2
28b20 35 35 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73  55 bytes, exclus
28b30 69 76 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65  ive of.** the ze
28b40 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20  ro-terminator.  
28b50 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61  Note that the na
28b60 6d 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20  me length limit 
28b70 69 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74  is in bytes, not
28b80 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20  .** characters. 
28b90 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
28ba0 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f  create a functio
28bb0 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20  n with a longer 
28bc0 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73  name.** will res
28bd0 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45  ult in [SQLITE_E
28be0 52 52 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75  RROR] being retu
28bf0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
28c00 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
28c10 28 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65  (nArg).** is the
28c20 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
28c30 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 53 51  ents that the SQ
28c40 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a  L function or.**
28c50 20 61 67 67 72 65 67 61 74 65 20 74 61 6b 65 73   aggregate takes
28c60 2e 20 49 66 20 74 68 69 73 20 70 61 72 61 6d 65  . If this parame
28c70 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20  ter is -1, then 
28c80 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
28c90 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
28ca0 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75   may take any nu
28cb0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
28cc0 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  s between 0 and 
28cd0 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 73 65 74  the limit.** set
28ce0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d   by [sqlite3_lim
28cf0 69 74 5d 28 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  it]([SQLITE_LIMI
28d00 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 29  T_FUNCTION_ARG])
28d10 2e 20 20 49 66 20 74 68 65 20 74 68 69 72 64 0a  .  If the third.
28d20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ** parameter is 
28d30 6c 65 73 73 20 74 68 61 6e 20 2d 31 20 6f 72 20  less than -1 or 
28d40 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 32 37  greater than 127
28d50 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
28d60 6f 72 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e  or is.** undefin
28d70 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ed..**.** The fo
28d80 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20  urth parameter, 
28d90 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66  eTextRep, specif
28da0 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c  ies what.** [SQL
28db0 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20  ITE_UTF8 | text 
28dc0 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53  encoding] this S
28dd0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66  QL function pref
28de0 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70  ers for.** its p
28df0 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20  arameters.  Any 
28e00 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
28e10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75  lementation shou
28e20 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f  ld be able to wo
28e30 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20  rk.** work with 
28e40 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c  UTF-8, UTF-16le,
28e50 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42   or UTF-16be.  B
28e60 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  ut some implemen
28e70 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a  tations may be.*
28e80 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74  * more efficient
28e90 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69   with one encodi
28ea0 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e  ng than another.
28eb0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
28ec0 20 6d 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73   may.** invoke s
28ed0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
28ee0 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69  nction() or sqli
28ef0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28f00 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65  ion16() multiple
28f10 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74  .** times with t
28f20 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
28f30 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72   but with differ
28f40 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54  ent values of eT
28f50 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20  extRep..** When 
28f60 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65  multiple impleme
28f70 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
28f80 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  same function ar
28f90 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c  e available, SQL
28fa0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b  ite.** will pick
28fb0 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e   the one that in
28fc0 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74  volves the least
28fd0 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20   amount of data 
28fe0 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49  conversion..** I
28ff0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  f there is only 
29000 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65  a single impleme
29010 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f  ntation which do
29020 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74  es not care what
29030 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e   text.** encodin
29040 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  g is used, then 
29050 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
29060 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53  ent should be [S
29070 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a  QLITE_ANY]..**.*
29080 2a 20 54 68 65 20 66 69 66 74 68 20 70 61 72 61  * The fifth para
29090 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69  meter is an arbi
290a0 74 72 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20  trary pointer.  
290b0 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
290c0 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e  on of the.** fun
290d0 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61  ction can gain a
290e0 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f  ccess to this po
290f0 69 6e 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c  inter using [sql
29100 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29  ite3_user_data()
29110 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76  ]..**.** The sev
29120 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64  enth, eighth and
29130 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72   ninth parameter
29140 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20  s, xFunc, xStep 
29150 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a  and xFinal, are.
29160 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43  ** pointers to C
29170 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69  -language functi
29180 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
29190 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  nt the SQL funct
291a0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67  ion or.** aggreg
291b0 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51  ate. A scalar SQ
291c0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  L function requi
291d0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  res an implement
291e0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75  ation of the xFu
291f0 6e 63 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f  nc.** callback o
29200 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  nly, NULL pointe
29210 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  rs should be pas
29220 73 65 64 20 61 73 20 74 68 65 20 78 53 74 65 70  sed as the xStep
29230 20 61 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70   and xFinal.** p
29240 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67  arameters. An ag
29250 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63  gregate SQL func
29260 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e  tion requires an
29270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
29280 6f 66 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20  of xStep.** and 
29290 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20  xFinal and NULL 
292a0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
292b0 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64   for xFunc. To d
292c0 65 6c 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e  elete an existin
292d0 67 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  g.** SQL functio
292e0 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20  n or aggregate, 
292f0 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c  pass NULL for al
29300 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e  l three function
29310 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a   callbacks..**.*
29320 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65  * It is permitte
29330 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75  d to register mu
29340 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
29350 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
29360 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  me.** functions 
29370 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61  with the same na
29380 6d 65 20 62 75 74 20 77 69 74 68 20 65 69 74 68  me but with eith
29390 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d  er differing num
293a0 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d  bers of.** argum
293b0 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e  ents or differin
293c0 67 20 70 72 65 66 65 72 72 65 64 20 74 65 78 74  g preferred text
293d0 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c   encodings.  SQL
293e0 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20  ite will use.** 
293f0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
29400 6f 6e 20 74 68 61 74 20 6d 6f 73 74 20 63 6c 6f  on that most clo
29410 73 65 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65  sely matches the
29420 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 74 68   way in which th
29430 65 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  e.** SQL functio
29440 6e 20 69 73 20 75 73 65 64 2e 20 20 41 20 66 75  n is used.  A fu
29450 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
29460 61 74 69 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e  ation with a non
29470 2d 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72  -negative.** nAr
29480 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  g parameter is a
29490 20 62 65 74 74 65 72 20 6d 61 74 63 68 20 74 68   better match th
294a0 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d  an a function im
294b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74  plementation wit
294c0 68 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20  h.** a negative 
294d0 6e 41 72 67 2e 20 20 41 20 66 75 6e 63 74 69 6f  nArg.  A functio
294e0 6e 20 77 68 65 72 65 20 74 68 65 20 70 72 65 66  n where the pref
294f0 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64  erred text encod
29500 69 6e 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74  ing.** matches t
29510 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f  he database enco
29520 64 69 6e 67 20 69 73 20 61 20 62 65 74 74 65 72  ding is a better
29530 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61  .** match than a
29540 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
29550 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  the encoding is 
29560 64 69 66 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20  different.  .** 
29570 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  A function where
29580 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69   the encoding di
29590 66 66 65 72 65 6e 63 65 20 69 73 20 62 65 74 77  fference is betw
295a0 65 65 6e 20 55 54 46 31 36 6c 65 20 61 6e 64 20  een UTF16le and 
295b0 55 54 46 31 36 62 65 0a 2a 2a 20 69 73 20 61 20  UTF16be.** is a 
295c0 63 6c 6f 73 65 72 20 6d 61 74 63 68 20 74 68 61  closer match tha
295d0 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 65  n a function whe
295e0 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  re the encoding 
295f0 64 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a  difference is.**
29600 20 62 65 74 77 65 65 6e 20 55 54 46 38 20 61 6e   between UTF8 an
29610 64 20 55 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42  d UTF16..**.** B
29620 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
29630 73 20 6d 61 79 20 62 65 20 6f 76 65 72 6c 6f 61  s may be overloa
29640 64 65 64 20 62 79 20 6e 65 77 20 61 70 70 6c 69  ded by new appli
29650 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
29660 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  unctions..** The
29670 20 66 69 72 73 74 20 61 70 70 6c 69 63 61 74 69   first applicati
29680 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
29690 69 6f 6e 20 77 69 74 68 20 61 20 67 69 76 65 6e  ion with a given
296a0 20 6e 61 6d 65 20 6f 76 65 72 72 69 64 65 73 20   name overrides 
296b0 61 6c 6c 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20  all.** built-in 
296c0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
296d0 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20   same [database 
296e0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68  connection] with
296f0 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a   the same name..
29700 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 61 70  ** Subsequent ap
29710 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
29720 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74  d functions of t
29730 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c  he same name onl
29740 79 20 6f 76 65 72 72 69 64 65 20 0a 2a 2a 20 70  y override .** p
29750 72 69 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e  rior application
29760 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
29770 6e 73 20 74 68 61 74 20 61 72 65 20 61 6e 20 65  ns that are an e
29780 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74  xact match for t
29790 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
297a0 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 70  parameters and p
297b0 72 65 66 65 72 72 65 64 20 65 6e 63 6f 64 69 6e  referred encodin
297c0 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c  g..**.** An appl
297d0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
297e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 65 72 6d  function is perm
297f0 69 74 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74  itted to call ot
29800 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e  her.** SQLite in
29810 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76  terfaces.  Howev
29820 65 72 2c 20 73 75 63 68 20 63 61 6c 6c 73 20 6d  er, such calls m
29830 75 73 74 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65  ust not.** close
29840 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
29850 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e  nnection nor fin
29860 61 6c 69 7a 65 20 6f 72 20 72 65 73 65 74 20 74  alize or reset t
29870 68 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73  he prepared.** s
29880 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
29890 68 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  h the function i
298a0 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s running..**.**
298b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
298c0 2a 20 5b 48 31 36 31 30 33 5d 20 5b 48 31 36 31  * [H16103] [H161
298d0 30 36 5d 20 5b 48 31 36 31 30 39 5d 20 5b 48 31  06] [H16109] [H1
298e0 36 31 31 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b  6112] [H16118] [
298f0 48 31 36 31 32 31 5d 20 5b 48 31 36 31 32 37 5d  H16121] [H16127]
29900 0a 2a 2a 20 5b 48 31 36 31 33 30 5d 20 5b 48 31  .** [H16130] [H1
29910 36 31 33 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b  6133] [H16136] [
29920 48 31 36 31 33 39 5d 20 5b 48 31 36 31 34 32 5d  H16139] [H16142]
29930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
29940 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
29950 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  e_function(.  sq
29960 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
29970 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69  st char *zFuncti
29980 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
29990 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52  rg,.  int eTextR
299a0 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70  ep,.  void *pApp
299b0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
299c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
299d0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
299e0 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  alue**),.  void 
299f0 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
29a00 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
29a10 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
29a20 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29    void (*xFinal)
29a30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
29a40 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
29a50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
29a60 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a  ate_function16(.
29a70 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
29a80 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75   const void *zFu
29a90 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e  nctionName,.  in
29aa0 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54  t nArg,.  int eT
29ab0 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a  extRep,.  void *
29ac0 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  pApp,.  void (*x
29ad0 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
29ae0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
29af0 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76  e3_value**),.  v
29b00 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
29b10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
29b20 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
29b30 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69  *),.  void (*xFi
29b40 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
29b50 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  text*).);../*.**
29b60 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 20   CAPI3REF: Text 
29b70 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 36  Encodings {H1026
29b80 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 36  7} <S50200> <H16
29b90 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  100>.**.** These
29ba0 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65   constant define
29bb0 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74   integer codes t
29bc0 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 68  hat represent th
29bd0 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78  e various.** tex
29be0 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70  t encodings supp
29bf0 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  orted by SQLite.
29c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
29c10 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 20  TE_UTF8         
29c20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
29c30 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20  TE_UTF16LE      
29c40 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
29c50 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 20  TE_UTF16BE      
29c60 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
29c70 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 20  TE_UTF16        
29c80 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61    4    /* Use na
29c90 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
29ca0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
29cb0 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 20  E_ANY           
29cc0 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33   5    /* sqlite3
29cd0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
29ce0 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
29cf0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
29d00 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73  IGNED  8    /* s
29d10 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
29d20 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a  llation only */.
29d30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
29d40 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e 63   Deprecated Func
29d50 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41  tions.** DEPRECA
29d60 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TED.**.** These 
29d70 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b 64  functions are [d
29d80 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e 20  eprecated].  In 
29d90 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69  order to maintai
29da0 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63  n.** backwards c
29db0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74  ompatibility wit
29dc0 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 68  h older code, th
29dd0 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f  ese functions co
29de0 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65  ntinue .** to be
29df0 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f 77   supported.  How
29e00 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 63  ever, new applic
29e10 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76  ations should av
29e20 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f  oid.** the use o
29e30 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  f these function
29e40 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f  s.  To help enco
29e50 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f 20  urage people to 
29e60 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74  avoid.** using t
29e70 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20  hese functions, 
29e80 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67  we are not going
29e90 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 61   to tell you wha
29ea0 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69  t they do..*/.#i
29eb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29ec0 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51 4c  T_DEPRECATED.SQL
29ed0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44  ITE_API SQLITE_D
29ee0 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71  EPRECATED int sq
29ef0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
29f00 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f  count(sqlite3_co
29f10 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
29f20 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
29f30 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65  CATED int sqlite
29f40 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 65  3_expired(sqlite
29f50 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45  3_stmt*);.SQLITE
29f60 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
29f70 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
29f80 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
29f90 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
29fa0 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  t*, sqlite3_stmt
29fb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  *);.SQLITE_API S
29fc0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
29fd0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f   int sqlite3_glo
29fe0 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64  bal_recover(void
29ff0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
2a000 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
2a010 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72  void sqlite3_thr
2a020 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64  ead_cleanup(void
2a030 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
2a040 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
2a050 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  int sqlite3_memo
2a060 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29  ry_alarm(void(*)
2a070 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  (void*,sqlite3_i
2a080 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c  nt64,int),void*,
2a090 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
2a0a0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41  #endif../*.** CA
2a0b0 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e  PI3REF: Obtainin
2a0c0 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50  g SQL Function P
2a0d0 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73 20  arameter Values 
2a0e0 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30 30  {H15100} <S20200
2a0f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61  >.**.** The C-la
2a100 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74  nguage implement
2a110 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e  ation of SQL fun
2a120 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65  ctions and aggre
2a130 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68  gates uses.** th
2a140 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66  is set of interf
2a150 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  ace routines to 
2a160 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 6d  access the param
2a170 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a  eter values on.*
2a180 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f  * the function o
2a190 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a  r aggregate..**.
2a1a0 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f  ** The xFunc (fo
2a1b0 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  r scalar functio
2a1c0 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f  ns) or xStep (fo
2a1d0 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 61  r aggregates) pa
2a1e0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b  rameters.** to [
2a1f0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2a200 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b  unction()] and [
2a210 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2a220 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20  unction16()].** 
2a230 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73  define callbacks
2a240 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
2a250 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
2a260 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73  s and aggregates
2a270 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72  ..** The 4th par
2a280 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 20  ameter to these 
2a290 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20  callbacks is an 
2a2a0 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
2a2b0 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74  s to.** [protect
2a2c0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2a2d0 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72  ] objects.  Ther
2a2e0 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65  e is one [sqlite
2a2f0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
2a300 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61  for.** each para
2a310 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c  meter to the SQL
2a320 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73   function.  Thes
2a330 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
2a340 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63  sed to.** extrac
2a350 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
2a360 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2a370 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a  ] objects..**.**
2a380 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2a390 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b  work only with [
2a3a0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a3b0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
2a3c0 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74  ..** Any attempt
2a3d0 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f   to use these ro
2a3e0 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e  utines on an [un
2a3f0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a400 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65  3_value].** obje
2a410 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  ct results in un
2a420 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72  defined behavior
2a430 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2a440 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74  utines work just
2a450 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 73   like the corres
2a460 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20  ponding [column 
2a470 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73  access functions
2a480 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  ].** except that
2a490 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73    these routines
2a4a0 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b   take a single [
2a4b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a4c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a  3_value] object.
2a4d0 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  ** pointer inste
2a4e0 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 33  ad of a [sqlite3
2a4f0 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20  _stmt*] pointer 
2a500 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 63  and an integer c
2a510 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  olumn number..**
2a520 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2a530 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20 69  value_text16() i
2a540 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63 74  nterface extract
2a550 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  s a UTF-16 strin
2a560 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 69  g.** in the nati
2a570 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f 66  ve byte-order of
2a580 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69 6e   the host machin
2a590 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  e.  The.** sqlit
2a5a0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
2a5b0 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
2a5c0 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29  value_text16le()
2a5d0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 65   interfaces.** e
2a5e0 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 74  xtract UTF-16 st
2a5f0 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64  rings as big-end
2a600 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65  ian and little-e
2a610 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 65  ndian respective
2a620 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ly..**.** The sq
2a630 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
2a640 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65 72  ric_type() inter
2a650 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f  face attempts to
2a660 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69   apply.** numeri
2a670 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74 68  c affinity to th
2a680 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 6d  e value.  This m
2a690 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74 74  eans that an att
2a6a0 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20  empt is.** made 
2a6b0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76  to convert the v
2a6c0 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67  alue to an integ
2a6d0 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70  er or floating p
2a6e0 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63  oint.  If.** suc
2a6f0 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  h a conversion i
2a700 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f  s possible witho
2a710 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72  ut loss of infor
2a720 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72  mation (in other
2a730 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 68  .** words, if th
2a740 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  e value is a str
2a750 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c  ing that looks l
2a760 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a  ike a number).**
2a770 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 72   then the conver
2a780 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  sion is performe
2a790 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e 6f  d.  Otherwise no
2a7a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75   conversion occu
2a7b0 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49  rs..** The [SQLI
2a7c0 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74  TE_INTEGER | dat
2a7d0 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e  atype] after con
2a7e0 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72  version is retur
2a7f0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73  ned..**.** Pleas
2a800 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 72  e pay particular
2a810 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68   attention to th
2a820 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20  e fact that the 
2a830 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
2a840 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  .** from [sqlite
2a850 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c  3_value_blob()],
2a860 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a870 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  text()], or.** [
2a880 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a890 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69  xt16()] can be i
2a8a0 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 20  nvalidated by a 
2a8b0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20  subsequent call 
2a8c0 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  to.** [sqlite3_v
2a8d0 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b  alue_bytes()], [
2a8e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2a8f0 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74  tes16()], [sqlit
2a900 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d  e3_value_text()]
2a910 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ,.** or [sqlite3
2a920 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d  _value_text16()]
2a930 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2a940 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63  utines must be c
2a950 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
2a960 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a 2a  ame thread as.**
2a970 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2a980 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64 20  n that supplied 
2a990 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  the [sqlite3_val
2a9a0 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 2e  ue*] parameters.
2a9b0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2a9c0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d  nts:.** [H15103]
2a9d0 20 5b 48 31 35 31 30 36 5d 20 5b 48 31 35 31 30   [H15106] [H1510
2a9e0 39 5d 20 5b 48 31 35 31 31 32 5d 20 5b 48 31 35  9] [H15112] [H15
2a9f0 31 31 35 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48  115] [H15118] [H
2aa00 31 35 31 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a  15121] [H15124].
2aa10 2a 2a 20 5b 48 31 35 31 32 37 5d 20 5b 48 31 35  ** [H15127] [H15
2aa20 31 33 30 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48  130] [H15133] [H
2aa30 31 35 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45  15136].*/.SQLITE
2aa40 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2aa50 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  *sqlite3_value_b
2aa60 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  lob(sqlite3_valu
2aa70 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2aa80 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
2aa90 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  e_bytes(sqlite3_
2aaa0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2aab0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2aac0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71  value_bytes16(sq
2aad0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2aae0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
2aaf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
2ab00 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  ouble(sqlite3_va
2ab10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ab20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
2ab30 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  lue_int(sqlite3_
2ab40 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2ab50 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
2ab60 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  4 sqlite3_value_
2ab70 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61  int64(sqlite3_va
2ab80 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ab90 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
2aba0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
2abb0 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65  alue_text(sqlite
2abc0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
2abd0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
2abe0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
2abf0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76  text16(sqlite3_v
2ac00 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
2ac10 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
2ac20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2ac30 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  t16le(sqlite3_va
2ac40 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ac50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
2ac60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2ac70 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  16be(sqlite3_val
2ac80 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
2ac90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
2aca0 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ue_type(sqlite3_
2acb0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2acc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2acd0 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
2ace0 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
2acf0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
2ad00 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72  REF: Obtain Aggr
2ad10 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43  egate Function C
2ad20 6f 6e 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20  ontext {H16210} 
2ad30 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2ad40 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
2ad50 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53  n of aggregate S
2ad60 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  QL functions use
2ad70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
2ad80 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73   allocate.** a s
2ad90 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f  tructure for sto
2ada0 72 69 6e 67 20 74 68 65 69 72 20 73 74 61 74 65  ring their state
2adb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
2adc0 74 20 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74  t time the sqlit
2add0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2ade0 74 65 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69  text() routine i
2adf0 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a  s called for a.*
2ae00 2a 20 70 61 72 74 69 63 75 6c 61 72 20 61 67 67  * particular agg
2ae10 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61  regate, SQLite a
2ae20 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20  llocates nBytes 
2ae30 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65  of memory, zeroe
2ae40 73 20 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65  s out that.** me
2ae50 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e  mory, and return
2ae60 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
2ae70 74 2e 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64  t. On second and
2ae80 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
2ae90 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  s to.** sqlite3_
2aea0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
2aeb0 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65  t() for the same
2aec0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2aed0 69 6f 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68  ion index,.** th
2aee0 65 20 73 61 6d 65 20 62 75 66 66 65 72 20 69 73  e same buffer is
2aef0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69   returned. The i
2af00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2af10 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63   the aggregate c
2af20 61 6e 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65  an use.** the re
2af30 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f  turned buffer to
2af40 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61   accumulate data
2af50 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61  ..**.** SQLite a
2af60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65  utomatically fre
2af70 65 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  es the allocated
2af80 20 62 75 66 66 65 72 20 77 68 65 6e 20 74 68 65   buffer when the
2af90 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75   aggregate.** qu
2afa0 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a  ery concludes..*
2afb0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
2afc0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
2afd0 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  be a copy of the
2afe0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
2aff0 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74  text | SQL funct
2b000 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61  ion context] tha
2b010 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  t is the first p
2b020 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74  arameter.** to t
2b030 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  he callback rout
2b040 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ine that impleme
2b050 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74  nts the aggregat
2b060 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  e function..**.*
2b070 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
2b080 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
2b090 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
2b0a0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74  ad in which.** t
2b0b0 68 65 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  he aggregate SQL
2b0c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
2b0d0 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ning..**.** Requ
2b0e0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2b0f0 36 32 31 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b  6211] [H16213] [
2b100 48 31 36 32 31 35 5d 20 5b 48 31 36 32 31 37 5d  H16215] [H16217]
2b110 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2b120 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67  oid *sqlite3_agg
2b130 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73  regate_context(s
2b140 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2b150 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f   int nBytes);../
2b160 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55  *.** CAPI3REF: U
2b170 73 65 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e  ser Data For Fun
2b180 63 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20  ctions {H16240} 
2b190 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2b1a0 68 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  he sqlite3_user_
2b1b0 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65  data() interface
2b1c0 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20   returns a copy 
2b1d0 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  of.** the pointe
2b1e0 72 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70  r that was the p
2b1f0 55 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74  UserData paramet
2b200 65 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61  er (the 5th para
2b210 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65  meter).** of the
2b220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2b230 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20  _function()].** 
2b240 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
2b250 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
2b260 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  ] routines that 
2b270 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65  originally.** re
2b280 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70  gistered the app
2b290 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2b2a0 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d   function. {END}
2b2b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2b2c0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
2b2d0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ed from the same
2b2e0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
2b2f0 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
2b300 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2b310 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e  tion is running.
2b320 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2b330 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d  nts:.** [H16243]
2b340 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2b350 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65  oid *sqlite3_use
2b360 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  r_data(sqlite3_c
2b370 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  ontext*);../*.**
2b380 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
2b390 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46  ase Connection F
2b3a0 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  or Functions {H1
2b3b0 36 32 35 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53  6250} <S60600><S
2b3c0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2b3d0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2b3e0 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74  _db_handle() int
2b3f0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2b400 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
2b410 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
2b420 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2b430 69 6f 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61  ion] (the 1st pa
2b440 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74  rameter).** of t
2b450 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
2b460 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a  te_function()].*
2b470 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  * and [sqlite3_c
2b480 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2b490 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ()] routines tha
2b4a0 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20  t originally.** 
2b4b0 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20 61  registered the a
2b4c0 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e  pplication defin
2b4d0 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  ed function..**.
2b4e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2b4f0 0a 2a 2a 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a  .** [H16253].*/.
2b500 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
2b510 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e3 *sqlite3_cont
2b520 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71  ext_db_handle(sq
2b530 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
2b540 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2b550 3a 20 46 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c  : Function Auxil
2b560 69 61 72 79 20 44 61 74 61 20 7b 48 31 36 32 37  iary Data {H1627
2b570 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a  0} <S20200>.**.*
2b580 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2b590 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61  two functions ma
2b5a0 79 20 62 65 20 75 73 65 64 20 62 79 20 73 63 61  y be used by sca
2b5b0 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  lar SQL function
2b5c0 73 20 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74  s to.** associat
2b5d0 65 20 6d 65 74 61 64 61 74 61 20 77 69 74 68 20  e metadata with 
2b5e0 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e  argument values.
2b5f0 20 49 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c   If the same val
2b600 75 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a  ue is passed to.
2b610 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f  ** multiple invo
2b620 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73  cations of the s
2b630 61 6d 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ame SQL function
2b640 20 64 75 72 69 6e 67 20 71 75 65 72 79 20 65 78   during query ex
2b650 65 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a  ecution, under.*
2b660 2a 20 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61  * some circumsta
2b670 6e 63 65 73 20 74 68 65 20 61 73 73 6f 63 69 61  nces the associa
2b680 74 65 64 20 6d 65 74 61 64 61 74 61 20 6d 61 79  ted metadata may
2b690 20 62 65 20 70 72 65 73 65 72 76 65 64 2e 20 54   be preserved. T
2b6a0 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73  his may.** be us
2b6b0 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
2b6c0 20 74 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61   to add a regula
2b6d0 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74  r-expression mat
2b6e0 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20  ching scalar.** 
2b6f0 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f  function. The co
2b700 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f  mpiled version o
2b710 66 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  f the regular ex
2b720 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72  pression is stor
2b730 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74  ed as.** metadat
2b740 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
2b750 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75 65 20  h the SQL value 
2b760 70 61 73 73 65 64 20 61 73 20 74 68 65 20 72 65  passed as the re
2b770 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
2b780 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68  .** pattern.  Th
2b790 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c  e compiled regul
2b7a0 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61  ar expression ca
2b7b0 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d  n be reused on m
2b7c0 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63  ultiple.** invoc
2b7d0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
2b7e0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74  me function so t
2b7f0 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
2b800 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0a   pattern string.
2b810 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ** does not need
2b820 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65   to be recompile
2b830 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  d on each invoca
2b840 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
2b850 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
2b860 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
2b870 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2b880 72 20 74 6f 20 74 68 65 20 6d 65 74 61 64 61 74  r to the metadat
2b890 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  a.** associated 
2b8a0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
2b8b0 65 74 5f 61 75 78 64 61 74 61 28 29 20 66 75 6e  et_auxdata() fun
2b8c0 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 4e  ction with the N
2b8d0 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76  th argument.** v
2b8e0 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70 70 6c  alue to the appl
2b8f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2b900 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  function. If no 
2b910 6d 65 74 61 64 61 74 61 20 68 61 73 20 62 65 65  metadata has bee
2b920 6e 20 65 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73  n ever.** been s
2b930 65 74 20 66 6f 72 20 74 68 65 20 4e 74 68 20 61  et for the Nth a
2b940 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66  rgument of the f
2b950 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74  unction, or if t
2b960 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2b970 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 61 72  .** function par
2b980 61 6d 65 74 65 72 20 68 61 73 20 63 68 61 6e 67  ameter has chang
2b990 65 64 20 73 69 6e 63 65 20 74 68 65 20 6d 65 74  ed since the met
2b9a0 61 2d 64 61 74 61 20 77 61 73 20 73 65 74 2c 0a  a-data was set,.
2b9b0 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ** then sqlite3_
2b9c0 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 72 65  get_auxdata() re
2b9d0 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69  turns a NULL poi
2b9e0 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nter..**.** The 
2b9f0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
2ba00 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
2ba10 73 61 76 65 73 20 74 68 65 20 6d 65 74 61 64 61  saves the metada
2ba20 74 61 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f  ta.** pointed to
2ba30 20 62 79 20 69 74 73 20 33 72 64 20 70 61 72 61   by its 3rd para
2ba40 6d 65 74 65 72 20 61 73 20 74 68 65 20 6d 65 74  meter as the met
2ba50 61 64 61 74 61 20 66 6f 72 20 74 68 65 20 4e 2d  adata for the N-
2ba60 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f  th.** argument o
2ba70 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
2ba80 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2ba90 6f 6e 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0a  on.  Subsequent.
2baa0 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
2bab0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
2bac0 29 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74  ) might return t
2bad0 68 69 73 20 64 61 74 61 2c 20 69 66 20 69 74 20  his data, if it 
2bae0 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20  has.** not been 
2baf0 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66  destroyed..** If
2bb00 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
2bb10 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76   SQLite will inv
2bb20 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75 63 74  oke the destruct
2bb30 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67  or.** function g
2bb40 69 76 65 6e 20 62 79 20 74 68 65 20 34 74 68 20  iven by the 4th 
2bb50 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
2bb60 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2bb70 28 29 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74  () on.** the met
2bb80 61 64 61 74 61 20 77 68 65 6e 20 74 68 65 20 63  adata when the c
2bb90 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e  orresponding fun
2bba0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
2bbb0 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68  changes.** or wh
2bbc0 65 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  en the SQL state
2bbd0 6d 65 6e 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20  ment completes, 
2bbe0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
2bbf0 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  first..**.** SQL
2bc00 69 74 65 20 69 73 20 66 72 65 65 20 74 6f 20 63  ite is free to c
2bc10 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
2bc20 6f 72 20 61 6e 64 20 64 72 6f 70 20 6d 65 74 61  or and drop meta
2bc30 64 61 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70  data on any.** p
2bc40 61 72 61 6d 65 74 65 72 20 6f 66 20 61 6e 79 20  arameter of any 
2bc50 66 75 6e 63 74 69 6f 6e 20 61 74 20 61 6e 79 20  function at any 
2bc60 74 69 6d 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20  time.  The only 
2bc70 67 75 61 72 61 6e 74 65 65 20 69 73 20 74 68 61  guarantee is tha
2bc80 74 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63  t.** the destruc
2bc90 74 6f 72 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  tor will be call
2bca0 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 6d 65  ed before the me
2bcb0 74 61 64 61 74 61 20 69 73 20 64 72 6f 70 70 65  tadata is droppe
2bcc0 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63  d..**.** In prac
2bcd0 74 69 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69  tice, metadata i
2bce0 73 20 70 72 65 73 65 72 76 65 64 20 62 65 74 77  s preserved betw
2bcf0 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  een function cal
2bd00 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73  ls for.** expres
2bd10 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63  sions that are c
2bd20 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69  onstant at compi
2bd30 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e  le time. This in
2bd40 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a  cludes literal.*
2bd50 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c  * values and SQL
2bd60 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   variables..**.*
2bd70 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
2bd80 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
2bd90 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68  from the same th
2bda0 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a  read in which.**
2bdb0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2bdc0 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
2bdd0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2bde0 3a 0a 2a 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48  :.** [H16272] [H
2bdf0 31 36 32 37 34 5d 20 5b 48 31 36 32 37 36 5d 20  16274] [H16276] 
2be00 5b 48 31 36 32 37 37 5d 20 5b 48 31 36 32 37 38  [H16277] [H16278
2be10 5d 20 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51  ] [H16279].*/.SQ
2be20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
2be30 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
2be40 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
2be50 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c  xt*, int N);.SQL
2be60 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2be70 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2be80 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2be90 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c  *, int N, void*,
2bea0 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29   void (*)(void*)
2beb0 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  );.../*.** CAPI3
2bec0 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44  REF: Constants D
2bed0 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20  efining Special 
2bee0 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61 76  Destructor Behav
2bef0 69 6f 72 20 7b 48 31 30 32 38 30 7d 20 3c 53 33  ior {H10280} <S3
2bf00 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0100>.**.** Thes
2bf10 65 20 61 72 65 20 73 70 65 63 69 61 6c 20 76 61  e are special va
2bf20 6c 75 65 73 20 66 6f 72 20 74 68 65 20 64 65 73  lues for the des
2bf30 74 72 75 63 74 6f 72 20 74 68 61 74 20 69 73 20  tructor that is 
2bf40 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65  passed in as the
2bf50 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65  .** final argume
2bf60 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c  nt to routines l
2bf70 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  ike [sqlite3_res
2bf80 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66  ult_blob()].  If
2bf90 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a   the destructor.
2bfa0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  ** argument is S
2bfb0 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74  QLITE_STATIC, it
2bfc0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2bfd0 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20  content pointer 
2bfe0 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61  is constant.** a
2bff0 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68  nd will never ch
2c000 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e  ange.  It does n
2c010 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65  ot need to be de
2c020 73 74 72 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a  stroyed.  The.**
2c030 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2c040 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 68  T value means th
2c050 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77  at the content w
2c060 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67  ill likely chang
2c070 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72  e in.** the near
2c080 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 61 74   future and that
2c090 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d   SQLite should m
2c0a0 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76  ake its own priv
2c0b0 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  ate copy of.** t
2c0c0 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72  he content befor
2c0d0 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  e returning..**.
2c0e0 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 20 69  ** The typedef i
2c0f0 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77  s necessary to w
2c100 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c  ork around probl
2c110 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a  ems in certain.*
2c120 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e  * C++ compilers.
2c130 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 31    See ticket #21
2c140 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76  91..*/.typedef v
2c150 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 65  oid (*sqlite3_de
2c160 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 28 76  structor_type)(v
2c170 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53  oid*);.#define S
2c180 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 20 20  QLITE_STATIC    
2c190 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74    ((sqlite3_dest
2c1a0 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 0a 23  ructor_type)0).#
2c1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52  define SQLITE_TR
2c1c0 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 6c 69  ANSIENT   ((sqli
2c1d0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2c1e0 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43  ype)-1)../*.** C
2c1f0 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67  API3REF: Setting
2c200 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41   The Result Of A
2c210 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b  n SQL Function {
2c220 48 31 36 34 30 30 7d 20 3c 53 32 30 32 30 30 3e  H16400} <S20200>
2c230 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
2c240 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62  tines are used b
2c250 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78  y the xFunc or x
2c260 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20  Final callbacks 
2c270 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  that.** implemen
2c280 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  t SQL functions 
2c290 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 20  and aggregates. 
2c2a0 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   See.** [sqlite3
2c2b0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2c2c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2c2d0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2c2e0 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64  16()].** for add
2c2f0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2c300 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ion..**.** These
2c310 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20   functions work 
2c320 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74  very much like t
2c330 68 65 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69  he [parameter bi
2c340 6e 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66  nding] family of
2c350 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73  .** functions us
2c360 65 64 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65  ed to bind value
2c370 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65  s to host parame
2c380 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 65 64  ters in prepared
2c390 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20   statements..** 
2c3a0 52 65 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51  Refer to the [SQ
2c3b0 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63  L parameter] doc
2c3c0 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61  umentation for a
2c3d0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2c3e0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
2c3f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c400 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65  blob() interface
2c410 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74   sets the result
2c420 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c   from.** an appl
2c430 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2c440 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74  function to be t
2c450 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f  he BLOB whose co
2c460 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64  ntent is pointed
2c470 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65  .** to by the se
2c480 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61  cond parameter a
2c490 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 62 79  nd which is N by
2c4a0 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e  tes long where N
2c4b0 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64   is the.** third
2c4c0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
2c4d0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2c4e0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20  sult_zeroblob() 
2c4f0 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20 74  interfaces set t
2c500 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20  he result of.** 
2c510 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2c520 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2c530 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 6f   to be a BLOB co
2c540 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72  ntaining all zer
2c550 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 4e  o.** bytes and N
2c560 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
2c570 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 76  where N is the v
2c580 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e 64 20  alue of the 2nd 
2c590 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
2c5a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2c5b0 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74  ult_double() int
2c5c0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2c5d0 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61  result from.** a
2c5e0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
2c5f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2c600 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20  o be a floating 
2c610 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70 65 63  point value spec
2c620 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74 73 20  ified.** by its 
2c630 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  2nd argument..**
2c640 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2c650 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61  result_error() a
2c660 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  nd sqlite3_resul
2c670 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75 6e 63  t_error16() func
2c680 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74  tions.** cause t
2c690 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53  he implemented S
2c6a0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74  QL function to t
2c6b0 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f  hrow an exceptio
2c6c0 6e 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  n..** SQLite use
2c6d0 73 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69  s the string poi
2c6e0 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 0a 2a  nted to by the.*
2c6f0 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  * 2nd parameter 
2c700 6f 66 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  of sqlite3_resul
2c710 74 5f 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c  t_error() or sql
2c720 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c730 72 31 36 28 29 0a 2a 2a 20 61 73 20 74 68 65 20  r16().** as the 
2c740 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  text of an error
2c750 20 6d 65 73 73 61 67 65 2e 20 20 53 51 4c 69 74   message.  SQLit
2c760 65 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65  e interprets the
2c770 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67   error.** messag
2c780 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71  e string from sq
2c790 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2c7a0 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e 20 53  or() as UTF-8. S
2c7b0 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72  QLite.** interpr
2c7c0 65 74 73 20 74 68 65 20 73 74 72 69 6e 67 20 66  ets the string f
2c7d0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75  rom sqlite3_resu
2c7e0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 61 73 20  lt_error16() as 
2c7f0 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65  UTF-16 in native
2c800 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20  .** byte order. 
2c810 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61   If the third pa
2c820 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2c830 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c840 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ).** or sqlite3_
2c850 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
2c860 20 69 73 20 6e 65 67 61 74 69 76 65 20 74 68 65   is negative the
2c870 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 61  n SQLite takes a
2c880 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  s the error.** m
2c890 65 73 73 61 67 65 20 61 6c 6c 20 74 65 78 74 20  essage all text 
2c8a0 75 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 66  up through the f
2c8b0 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63  irst zero charac
2c8c0 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74  ter..** If the t
2c8d0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
2c8e0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  o sqlite3_result
2c8f0 5f 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73  _error() or.** s
2c900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2c910 72 6f 72 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e  ror16() is non-n
2c920 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c  egative then SQL
2c930 69 74 65 20 74 61 6b 65 73 20 74 68 61 74 20 6d  ite takes that m
2c940 61 6e 79 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f  any.** bytes (no
2c950 74 20 63 68 61 72 61 63 74 65 72 73 29 20 66 72  t characters) fr
2c960 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  om the 2nd param
2c970 65 74 65 72 20 61 73 20 74 68 65 20 65 72 72 6f  eter as the erro
2c980 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68  r message..** Th
2c990 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2c9a0 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c  _error() and sql
2c9b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c9c0 72 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  r16().** routine
2c9d0 73 20 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65  s make a private
2c9e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72   copy of the err
2c9f0 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
2ca00 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72  before.** they r
2ca10 65 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74  eturn.  Hence, t
2ca20 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2ca30 69 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61  ion can dealloca
2ca40 74 65 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20  te or.** modify 
2ca50 74 68 65 20 74 65 78 74 20 61 66 74 65 72 20 74  the text after t
2ca60 68 65 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f  hey return witho
2ca70 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20  ut harm..** The 
2ca80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2ca90 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63  rror_code() func
2caa0 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65  tion changes the
2cab0 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72   error code.** r
2cac0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74  eturned by SQLit
2cad0 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  e as a result of
2cae0 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66   an error in a f
2caf0 75 6e 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66  unction.  By def
2cb00 61 75 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72  ault,.** the err
2cb10 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54  or code is SQLIT
2cb20 45 5f 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73  E_ERROR.  A subs
2cb30 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  equent call to s
2cb40 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2cb50 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69  ror().** or sqli
2cb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2cb70 31 36 28 29 20 72 65 73 65 74 73 20 74 68 65 20  16() resets the 
2cb80 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51  error code to SQ
2cb90 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
2cba0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2cbb0 73 75 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e  sult_toobig() in
2cbc0 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53  terface causes S
2cbd0 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61  QLite to throw a
2cbe0 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63  n error.** indic
2cbf0 61 74 69 6e 67 20 74 68 61 74 20 61 20 73 74 72  ating that a str
2cc00 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74  ing or BLOB is t
2cc10 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 70 72 65 73  o long to repres
2cc20 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ent..**.** The s
2cc30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f  qlite3_result_no
2cc40 6d 65 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20  mem() interface 
2cc50 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f  causes SQLite to
2cc60 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a   throw an error.
2cc70 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ** indicating th
2cc80 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  at a memory allo
2cc90 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a  cation failed..*
2cca0 2a 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 28 29 20 69 6e  _result_int() in
2ccc0 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
2ccd0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2cce0 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
2ccf0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2cd00 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 33  tion to be the 3
2cd10 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
2cd20 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69  eger.** value gi
2cd30 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61  ven in the 2nd a
2cd40 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20  rgument..** The 
2cd50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2cd60 6e 74 36 34 28 29 20 69 6e 74 65 72 66 61 63 65  nt64() interface
2cd70 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e   sets the return
2cd80 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65   value.** of the
2cd90 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
2cda0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
2cdb0 20 62 65 20 74 68 65 20 36 34 2d 62 69 74 20 73   be the 64-bit s
2cdc0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
2cdd0 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20   value given in 
2cde0 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74  the 2nd argument
2cdf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2ce00 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
2ce10 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73  ) interface sets
2ce20 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
2ce30 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c  e.** of the appl
2ce40 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2ce50 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e  function to be N
2ce60 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ULL..**.** The s
2ce70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2ce80 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65  xt(), sqlite3_re
2ce90 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a  sult_text16(),.*
2cea0 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  * sqlite3_result
2ceb0 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64  _text16le(), and
2cec0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ced0 74 65 78 74 31 36 62 65 28 29 20 69 6e 74 65 72  text16be() inter
2cee0 66 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65  faces.** set the
2cef0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
2cf00 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2cf10 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2cf20 6e 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78  n to be.** a tex
2cf30 74 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  t string which i
2cf40 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73  s represented as
2cf50 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e   UTF-8, UTF-16 n
2cf60 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2cf70 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74  ,.** UTF-16 litt
2cf80 6c 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54  le endian, or UT
2cf90 46 2d 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c  F-16 big endian,
2cfa0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a   respectively..*
2cfb0 2a 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74  * SQLite takes t
2cfc0 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 20 66  he text result f
2cfd0 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74  rom the applicat
2cfe0 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ion from.** the 
2cff0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66  2nd parameter of
2d000 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d010 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d020 61 63 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  aces..** If the 
2d030 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
2d040 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d050 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d060 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74  aces.** is negat
2d070 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  ive, then SQLite
2d080 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 74 65   takes result te
2d090 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20  xt from the 2nd 
2d0a0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72  parameter.** thr
2d0b0 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a  ough the first z
2d0c0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  ero character..*
2d0d0 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72  * If the 3rd par
2d0e0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2d0f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2d100 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  t* interfaces.**
2d110 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
2d120 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62  , then as many b
2d130 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63  ytes (not charac
2d140 74 65 72 73 29 20 6f 66 20 74 68 65 20 74 65 78  ters) of the tex
2d150 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20  t.** pointed to 
2d160 62 79 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  by the 2nd param
2d170 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61  eter are taken a
2d180 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  s the applicatio
2d190 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e  n-defined.** fun
2d1a0 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a  ction result..**
2d1b0 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61   If the 4th para
2d1c0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2d1d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d1e0 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2d1f0 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2d200 74 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d  t_blob is a non-
2d210 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68  NULL pointer, th
2d220 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  en SQLite calls 
2d230 74 68 61 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  that.** function
2d240 20 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74   as the destruct
2d250 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f  or on the text o
2d260 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68  r BLOB result wh
2d270 65 6e 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e  en it has.** fin
2d280 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74  ished using that
2d290 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74   result..** If t
2d2a0 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
2d2b0 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f   to the sqlite3_
2d2c0 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74  result_text* int
2d2d0 65 72 66 61 63 65 73 20 6f 72 20 74 6f 0a 2a 2a  erfaces or to.**
2d2e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d2f0 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 63  blob is the spec
2d300 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c  ial constant SQL
2d310 49 54 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e  ITE_STATIC, then
2d320 20 53 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d   SQLite.** assum
2d330 65 73 20 74 68 61 74 20 74 68 65 20 74 65 78 74  es that the text
2d340 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20   or BLOB result 
2d350 69 73 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73  is in constant s
2d360 70 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f  pace and does no
2d370 74 0a 2a 2a 20 63 6f 70 79 20 74 68 65 20 63 6f  t.** copy the co
2d380 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72  ntent of the par
2d390 61 6d 65 74 65 72 20 6e 6f 72 20 63 61 6c 6c 20  ameter nor call 
2d3a0 61 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20  a destructor on 
2d3b0 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 77  the content.** w
2d3c0 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73  hen it has finis
2d3d0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
2d3e0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2d3f0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2d400 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2d410 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2d420 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
2d430 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
2d440 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
2d450 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54  onstant SQLITE_T
2d460 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e  RANSIENT.** then
2d470 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20   SQLite makes a 
2d480 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
2d490 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  lt into space ob
2d4a0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66  tained from.** f
2d4b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
2d4c0 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74  loc()] before it
2d4d0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
2d4e0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2d4f0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
2d500 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2d510 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
2d520 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2d530 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2d540 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20  e a copy the.** 
2d550 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
2d560 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2d570 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ct specified by 
2d580 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2d590 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  r.  The.** sqlit
2d5a0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2d5b0 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65  ) interface make
2d5c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2d5d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a  [sqlite3_value].
2d5e0 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b  ** so that the [
2d5f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73  sqlite3_value] s
2d600 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2d610 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68  parameter may ch
2d620 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65  ange or.** be de
2d630 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20  allocated after 
2d640 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
2d650 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77  alue() returns w
2d660 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20  ithout harm..** 
2d670 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  A [protected sql
2d680 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2d690 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65  ct may always be
2d6a0 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a   used where an.*
2d6b0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  * [unprotected s
2d6c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
2d6d0 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64  ject is required
2d6e0 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b  , so either.** k
2d6f0 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ind of [sqlite3_
2d700 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61  value] object ca
2d710 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74  n be used with t
2d720 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  his interface..*
2d730 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f  *.** If these ro
2d740 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
2d750 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  d from within th
2d760 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  e different thre
2d770 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f  ad.** than the o
2d780 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ne containing th
2d790 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2d7a0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2d7b0 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20  hat received.** 
2d7c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
2d7d0 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74  text] pointer, t
2d7e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
2d7f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
2d800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
2d810 20 5b 48 31 36 34 30 33 5d 20 5b 48 31 36 34 30   [H16403] [H1640
2d820 36 5d 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 36  6] [H16409] [H16
2d830 34 31 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 48  412] [H16415] [H
2d840 31 36 34 31 38 5d 20 5b 48 31 36 34 32 31 5d 20  16418] [H16421] 
2d850 5b 48 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 36  [H16424].** [H16
2d860 34 32 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 48  427] [H16430] [H
2d870 31 36 34 33 33 5d 20 5b 48 31 36 34 33 36 5d 20  16433] [H16436] 
2d880 5b 48 31 36 34 33 39 5d 20 5b 48 31 36 34 34 32  [H16439] [H16442
2d890 5d 20 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 34  ] [H16445] [H164
2d8a0 34 38 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 20  48].** [H16451] 
2d8b0 5b 48 31 36 34 35 34 5d 20 5b 48 31 36 34 35 37  [H16454] [H16457
2d8c0 5d 20 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 34  ] [H16460] [H164
2d8d0 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  63].*/.SQLITE_AP
2d8e0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d8f0 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74  esult_blob(sqlit
2d900 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2d910 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76  st void*, int, v
2d920 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2d930 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d940 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2d950 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  ouble(sqlite3_co
2d960 6e 74 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b  ntext*, double);
2d970 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2d980 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d990 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f  error(sqlite3_co
2d9a0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68  ntext*, const ch
2d9b0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
2d9c0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2d9d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2d9e0 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  6(sqlite3_contex
2d9f0 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
2da00 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
2da10 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2da20 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
2da30 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ig(sqlite3_conte
2da40 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  xt*);.SQLITE_API
2da50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2da60 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
2da70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2da80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
2da90 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2daa0 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71  lt_error_code(sq
2dab0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2dac0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
2dad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2dae0 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33  sult_int(sqlite3
2daf0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b  _context*, int);
2db00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2db10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2db20 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f  int64(sqlite3_co
2db30 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
2db40 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41  int64);.SQLITE_A
2db50 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2db60 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69  result_null(sqli
2db70 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53  te3_context*);.S
2db80 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2db90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2dba0 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  xt(sqlite3_conte
2dbb0 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  xt*, const char*
2dbc0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
2dbd0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
2dbe0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2dbf0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71  result_text16(sq
2dc00 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2dc10 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
2dc20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
2dc30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2dc40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2dc50 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74  t_text16le(sqlit
2dc60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2dc70 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f  st void*, int,vo
2dc80 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
2dc90 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2dca0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2dcb0 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 63  xt16be(sqlite3_c
2dcc0 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76  ontext*, const v
2dcd0 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a  oid*, int,void(*
2dce0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
2dcf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2dd00 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2dd10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2dd20 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  , sqlite3_value*
2dd30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2dd40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2dd50 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74  t_zeroblob(sqlit
2dd60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
2dd70 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   n);../*.** CAPI
2dd80 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 77  3REF: Define New
2dd90 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65   Collating Seque
2dda0 6e 63 65 73 20 7b 48 31 36 36 30 30 7d 20 3c 53  nces {H16600} <S
2ddb0 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20300>.**.** The
2ddc0 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  se functions are
2ddd0 20 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77   used to add new
2dde0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2ddf0 6e 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b  nces to the.** [
2de00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2de10 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61  ion] specified a
2de20 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
2de30 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
2de40 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20  name of the new 
2de50 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2de60 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20  ce is specified 
2de70 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e  as a UTF-8 strin
2de80 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33  g.** for sqlite3
2de90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2dea0 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
2deb0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2dec0 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 55  _v2().** and a U
2ded0 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f 72  TF-16 string for
2dee0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2def0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 49  collation16(). I
2df00 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74  n all cases.** t
2df10 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65  he name is passe
2df20 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
2df30 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
2df40 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  t..**.** The thi
2df50 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20  rd argument may 
2df60 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f  be one of the co
2df70 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f  nstants [SQLITE_
2df80 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  UTF8],.** [SQLIT
2df90 45 5f 55 54 46 31 36 4c 45 5d 2c 20 6f 72 20 5b  E_UTF16LE], or [
2dfa0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
2dfb0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2dfc0 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   the user-suppli
2dfd0 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78  ed.** routine ex
2dfe0 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73  pects to be pass
2dff0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  ed pointers to s
2e000 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75  trings encoded u
2e010 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55  sing UTF-8,.** U
2e020 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64  TF-16 little-end
2e030 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62  ian, or UTF-16 b
2e040 69 67 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65  ig-endian, respe
2e050 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
2e060 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d  third argument m
2e070 69 67 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51  ight also be [SQ
2e080 4c 49 54 45 5f 55 54 46 31 36 5d 20 74 6f 20 69  LITE_UTF16] to i
2e090 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
2e0a0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 78 70 65   routine.** expe
2e0b0 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  cts pointers to 
2e0c0 62 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  be UTF-16 string
2e0d0 73 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  s in the native 
2e0e0 62 79 74 65 20 6f 72 64 65 72 2c 20 6f 72 20 74  byte order, or t
2e0f0 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 63  he.** argument c
2e100 61 6e 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54  an be [SQLITE_UT
2e110 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 69 66 20  F16_ALIGNED] if 
2e120 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69  the.** the routi
2e130 6e 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74  ne expects point
2e140 65 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f  ers to 16-bit wo
2e150 72 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e  rd aligned strin
2e160 67 73 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 20  gs.** of UTF-16 
2e170 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
2e180 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  te order..**.** 
2e190 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2e1a0 20 75 73 65 72 20 73 75 70 70 6c 69 65 64 20 72   user supplied r
2e1b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 70  outine must be p
2e1c0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 66  assed as the fif
2e1d0 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20  th.** argument. 
2e1e0 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20   If it is NULL, 
2e1f0 74 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65  this is the same
2e200 20 61 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65   as deleting the
2e210 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
2e220 71 75 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20  quence (so that 
2e230 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61  SQLite cannot ca
2e240 6c 6c 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 0a  ll it anymore)..
2e250 2a 2a 20 45 61 63 68 20 74 69 6d 65 20 74 68 65  ** Each time the
2e260 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 75 70   application sup
2e270 70 6c 69 65 64 20 66 75 6e 63 74 69 6f 6e 20 69  plied function i
2e280 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20 69 73  s invoked, it is
2e290 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 69 74   passed.** as it
2e2a0 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  s first paramete
2e2b0 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  r a copy of the 
2e2c0 76 6f 69 64 2a 20 70 61 73 73 65 64 20 61 73 20  void* passed as 
2e2d0 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
2e2e0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ent.** to sqlite
2e2f0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2e300 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  on() or sqlite3_
2e310 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2e320 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  16()..**.** The 
2e330 72 65 6d 61 69 6e 69 6e 67 20 61 72 67 75 6d 65  remaining argume
2e340 6e 74 73 20 74 6f 20 74 68 65 20 61 70 70 6c 69  nts to the appli
2e350 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 20  cation-supplied 
2e360 72 6f 75 74 69 6e 65 20 61 72 65 20 74 77 6f 20  routine are two 
2e370 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 65 61 63 68  strings,.** each
2e380 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
2e390 61 20 28 6c 65 6e 67 74 68 2c 20 64 61 74 61 29  a (length, data)
2e3a0 20 70 61 69 72 20 61 6e 64 20 65 6e 63 6f 64 65   pair and encode
2e3b0 64 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e  d in the encodin
2e3c0 67 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 70 61  g.** that was pa
2e3d0 73 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72  ssed as the thir
2e3e0 64 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e 20  d argument when 
2e3f0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
2e400 71 75 65 6e 63 65 20 77 61 73 0a 2a 2a 20 72 65  quence was.** re
2e410 67 69 73 74 65 72 65 64 2e 20 7b 45 4e 44 7d 20  gistered. {END} 
2e420 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
2e430 20 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69   defined collati
2e440 6f 6e 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  on routine shoul
2e450 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 65 67 61  d.** return nega
2e460 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
2e470 73 69 74 69 76 65 20 69 66 20 74 68 65 20 66 69  sitive if the fi
2e480 72 73 74 20 73 74 72 69 6e 67 20 69 73 20 6c 65  rst string is le
2e490 73 73 20 74 68 61 6e 2c 0a 2a 2a 20 65 71 75 61  ss than,.** equa
2e4a0 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72  l to, or greater
2e4b0 20 74 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64   than the second
2e4c0 20 73 74 72 69 6e 67 2e 20 69 2e 65 2e 20 28 53   string. i.e. (S
2e4d0 54 52 49 4e 47 31 20 2d 20 53 54 52 49 4e 47 32  TRING1 - STRING2
2e4e0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  )..**.** The sql
2e4f0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2e500 61 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73  ation_v2() works
2e510 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72   like sqlite3_cr
2e520 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
2e530 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20  .** except that 
2e540 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72  it takes an extr
2e550 61 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68  a argument which
2e560 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72   is a destructor
2e570 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c   for.** the coll
2e580 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 73 74  ation.  The dest
2e590 72 75 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64  ructor is called
2e5a0 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
2e5b0 69 6f 6e 20 69 73 0a 2a 2a 20 64 65 73 74 72 6f  ion is.** destro
2e5c0 79 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  yed and is passe
2e5d0 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  d a copy of the 
2e5e0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
2e5f0 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 0a 2a   void* pointer.*
2e600 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
2e610 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2e620 6e 5f 76 32 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61  n_v2()..** Colla
2e630 74 69 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f  tions are destro
2e640 79 65 64 20 77 68 65 6e 20 74 68 65 79 20 61 72  yed when they ar
2e650 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20  e overridden by 
2e660 6c 61 74 65 72 20 63 61 6c 6c 73 20 74 6f 20 74  later calls to t
2e670 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
2e680 63 72 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  creation functio
2e690 6e 73 20 6f 72 20 77 68 65 6e 20 74 68 65 20 5b  ns or when the [
2e6a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e6b0 69 6f 6e 5d 20 69 73 20 63 6c 6f 73 65 64 0a 2a  ion] is closed.*
2e6c0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
2e6d0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _close()]..**.**
2e6e0 20 53 65 65 20 61 6c 73 6f 3a 20 20 5b 73 71 6c   See also:  [sql
2e6f0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2e700 65 65 64 65 64 28 29 5d 20 61 6e 64 20 5b 73 71  eeded()] and [sq
2e710 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
2e720 6e 65 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a  needed16()]..**.
2e730 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2e740 0a 2a 2a 20 5b 48 31 36 36 30 33 5d 20 5b 48 31  .** [H16603] [H1
2e750 36 36 30 34 5d 20 5b 48 31 36 36 30 36 5d 20 5b  6604] [H16606] [
2e760 48 31 36 36 30 39 5d 20 5b 48 31 36 36 31 32 5d  H16609] [H16612]
2e770 20 5b 48 31 36 36 31 35 5d 20 5b 48 31 36 36 31   [H16615] [H1661
2e780 38 5d 20 5b 48 31 36 36 32 31 5d 0a 2a 2a 20 5b  8] [H16621].** [
2e790 48 31 36 36 32 34 5d 20 5b 48 31 36 36 32 37 5d  H16624] [H16627]
2e7a0 20 5b 48 31 36 36 33 30 5d 0a 2a 2f 0a 53 51 4c   [H16630].*/.SQL
2e7b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2e7c0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2e7d0 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  tion(.  sqlite3*
2e7e0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
2e7f0 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
2e800 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
2e810 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
2e820 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2e830 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2e840 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 53 51  nst void*).);.SQ
2e850 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2e860 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2e870 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69  ation_v2(.  sqli
2e880 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63  te3*, .  const c
2e890 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69  har *zName, .  i
2e8a0 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20  nt eTextRep, .  
2e8b0 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43  void*,.  int(*xC
2e8c0 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
2e8d0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
2e8e0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a  t,const void*),.
2e8f0 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79    void(*xDestroy
2e900 29 28 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49  )(void*).);.SQLI
2e910 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2e920 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2e930 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ion16(.  sqlite3
2e940 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *, .  const void
2e950 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65   *zName,.  int e
2e960 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
2e970 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
2e980 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2e990 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2e9a0 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f  nst void*).);../
2e9b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
2e9c0 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65 64 65 64 20  ollation Needed 
2e9d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 36 37 30  Callbacks {H1670
2e9e0 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S20300>.**.*
2e9f0 2a 20 54 6f 20 61 76 6f 69 64 20 68 61 76 69 6e  * To avoid havin
2ea00 67 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 6c  g to register al
2ea10 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  l collation sequ
2ea20 65 6e 63 65 73 20 62 65 66 6f 72 65 20 61 20 64  ences before a d
2ea30 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62  atabase.** can b
2ea40 65 20 75 73 65 64 2c 20 61 20 73 69 6e 67 6c 65  e used, a single
2ea50 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
2ea60 6f 6e 20 6d 61 79 20 62 65 20 72 65 67 69 73 74  on may be regist
2ea70 65 72 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  ered with the.**
2ea80 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2ea90 63 74 69 6f 6e 5d 20 74 6f 20 62 65 20 63 61 6c  ction] to be cal
2eaa0 6c 65 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20  led whenever an 
2eab0 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  undefined collat
2eac0 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
2ead0 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  is required..**.
2eae0 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  ** If the functi
2eaf0 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64  on is registered
2eb00 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
2eb10 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2eb20 64 65 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68  ded() API,.** th
2eb30 65 6e 20 69 74 20 69 73 20 70 61 73 73 65 64 20  en it is passed 
2eb40 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64  the names of und
2eb50 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
2eb60 20 73 65 71 75 65 6e 63 65 73 20 61 73 20 73 74   sequences as st
2eb70 72 69 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64  rings.** encoded
2eb80 20 69 6e 20 55 54 46 2d 38 2e 20 7b 48 31 36 37   in UTF-8. {H167
2eb90 30 33 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63  03} If sqlite3_c
2eba0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2ebb0 36 28 29 20 69 73 20 75 73 65 64 2c 0a 2a 2a 20  6() is used,.** 
2ebc0 74 68 65 20 6e 61 6d 65 73 20 61 72 65 20 70 61  the names are pa
2ebd0 73 73 65 64 20 61 73 20 55 54 46 2d 31 36 20 69  ssed as UTF-16 i
2ebe0 6e 20 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65  n machine native
2ebf0 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 20   byte order..** 
2ec00 41 20 63 61 6c 6c 20 74 6f 20 65 69 74 68 65 72  A call to either
2ec10 20 66 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63   function replac
2ec20 65 73 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  es any existing 
2ec30 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  callback..**.** 
2ec40 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  When the callbac
2ec50 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68  k is invoked, th
2ec60 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
2ec70 20 70 61 73 73 65 64 20 69 73 20 61 20 63 6f 70   passed is a cop
2ec80 79 0a 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f  y.** of the seco
2ec90 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
2eca0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2ecb0 5f 6e 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20  _needed() or.** 
2ecc0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2ecd0 6e 5f 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54  n_needed16().  T
2ece0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
2ecf0 6e 74 20 69 73 20 74 68 65 20 64 61 74 61 62 61  nt is the databa
2ed00 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
2ed10 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67  .  The third arg
2ed20 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20  ument is one of 
2ed30 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b  [SQLITE_UTF8], [
2ed40 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
2ed50 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55  .** or [SQLITE_U
2ed60 54 46 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74  TF16LE], indicat
2ed70 69 6e 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73  ing the most des
2ed80 69 72 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74  irable form of t
2ed90 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20  he collation.** 
2eda0 73 65 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f  sequence functio
2edb0 6e 20 72 65 71 75 69 72 65 64 2e 20 20 54 68 65  n required.  The
2edc0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
2edd0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
2ede0 20 74 68 65 0a 2a 2a 20 72 65 71 75 69 72 65 64   the.** required
2edf0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2ee00 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  nce..**.** The c
2ee10 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
2ee20 20 73 68 6f 75 6c 64 20 72 65 67 69 73 74 65 72   should register
2ee30 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c   the desired col
2ee40 6c 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20  lation using.** 
2ee50 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2ee60 63 6f 6c 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73  collation()], [s
2ee70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2ee80 6c 6c 61 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72  llation16()], or
2ee90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  .** [sqlite3_cre
2eea0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2eeb0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
2eec0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2eed0 37 30 32 5d 20 5b 48 31 36 37 30 34 5d 20 5b 48  702] [H16704] [H
2eee0 31 36 37 30 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16706].*/.SQLITE
2eef0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ef00 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2ef10 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  d(.  sqlite3*, .
2ef20 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64    void*, .  void
2ef30 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
2ef40 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
2ef50 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a  const char*).);.
2ef60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2ef70 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2ef80 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
2ef90 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
2efa0 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  .  void(*)(void*
2efb0 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
2efc0 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
2efd0 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70  d*).);../*.** Sp
2efe0 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f  ecify the key fo
2eff0 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64  r an encrypted d
2f000 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
2f010 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
2f020 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74  .** called right
2f030 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f   after sqlite3_o
2f040 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pen()..**.** The
2f050 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
2f060 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e  nt this API is n
2f070 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
2f080 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61  the public relea
2f090 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e  se.** of SQLite.
2f0a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2f0b0 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a  nt sqlite3_key(.
2f0c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2f0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0e0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
2f0f0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
2f100 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2f110 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
2f120 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a  * The key */.);.
2f130 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
2f140 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e  e key on an open
2f150 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74   database.  If t
2f160 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
2f170 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e  ase is not.** en
2f180 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f  crypted, this ro
2f190 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79  utine will encry
2f1a0 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d  pt it.  If pNew=
2f1b0 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74  =0 or nNew==0, t
2f1c0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  he.** database i
2f1d0 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a  s decrypted..**.
2f1e0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
2f1f0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
2f200 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
2f210 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
2f220 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
2f230 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
2f240 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f250 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  _rekey(.  sqlite
2f260 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2f270 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2f280 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
2f290 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
2f2a0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
2f2b0 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  ey     /* The ne
2f2c0 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  w key */.);../*.
2f2d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73  ** CAPI3REF: Sus
2f2e0 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46  pend Execution F
2f2f0 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20  or A Short Time 
2f300 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30  {H10530} <S40410
2f310 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
2f320 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63  te3_sleep() func
2f330 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
2f340 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74  current thread t
2f350 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74  o suspend execut
2f360 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65  ion.** for at le
2f370 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  ast a number of 
2f380 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65  milliseconds spe
2f390 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61  cified in its pa
2f3a0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
2f3b0 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
2f3c0 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20  system does not 
2f3d0 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65  support sleep re
2f3e0 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d  quests with.** m
2f3f0 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20  illisecond time 
2f400 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e  resolution, then
2f410 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62   the time will b
2f420 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a  e rounded up to.
2f430 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73  ** the nearest s
2f440 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65  econd. The numbe
2f450 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  r of millisecond
2f460 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
2f470 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
2f480 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74   from the operat
2f490 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65  ing system is re
2f4a0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  turned..**.** SQ
2f4b0 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  Lite implements 
2f4c0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62  this interface b
2f4d0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53  y calling the xS
2f4e0 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  leep().** method
2f4f0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
2f500 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
2f510 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ject..**.** Requ
2f520 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 33  irements: [H1053
2f530 33 5d 20 5b 48 31 30 35 33 36 5d 0a 2a 2f 0a 53  3] [H10536].*/.S
2f540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f550 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29  lite3_sleep(int)
2f560 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2f570 46 3a 20 4e 61 6d 65 20 4f 66 20 54 68 65 20 46  F: Name Of The F
2f580 6f 6c 64 65 72 20 48 6f 6c 64 69 6e 67 20 54 65  older Holding Te
2f590 6d 70 6f 72 61 72 79 20 46 69 6c 65 73 20 7b 48  mporary Files {H
2f5a0 31 30 33 31 30 7d 20 3c 53 32 30 30 30 30 3e 0a  10310} <S20000>.
2f5b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 67 6c  **.** If this gl
2f5c0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73  obal variable is
2f5d0 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
2f5e0 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68  o a string which
2f5f0 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20   is.** the name 
2f600 6f 66 20 61 20 66 6f 6c 64 65 72 20 28 61 2e 6b  of a folder (a.k
2f610 2e 61 2e 20 64 69 72 65 63 74 6f 72 79 29 2c 20  .a. directory), 
2f620 74 68 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72 61  then all tempora
2f630 72 79 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61  ry files.** crea
2f640 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69  ted by SQLite wi
2f650 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20  ll be placed in 
2f660 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e 20  that directory. 
2f670 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c   If this variabl
2f680 65 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70  e.** is a NULL p
2f690 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c  ointer, then SQL
2f6a0 69 74 65 20 70 65 72 66 6f 72 6d 73 20 61 20 73  ite performs a s
2f6b0 65 61 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70  earch for an app
2f6c0 72 6f 70 72 69 61 74 65 0a 2a 2a 20 74 65 6d 70  ropriate.** temp
2f6d0 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63  orary file direc
2f6e0 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  tory..**.** It i
2f6f0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65  s not safe to re
2f700 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69  ad or modify thi
2f710 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 6d 6f  s variable in mo
2f720 72 65 20 74 68 61 6e 20 6f 6e 65 0a 2a 2a 20 74  re than one.** t
2f730 68 72 65 61 64 20 61 74 20 61 20 74 69 6d 65 2e  hread at a time.
2f740 20 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65    It is not safe
2f750 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69   to read or modi
2f760 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  fy this variable
2f770 0a 2a 2a 20 69 66 20 61 20 5b 64 61 74 61 62 61  .** if a [databa
2f780 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69  se connection] i
2f790 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 74 20  s being used at 
2f7a0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 69 6e  the same time in
2f7b0 20 61 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 74   a separate.** t
2f7c0 68 72 65 61 64 2e 0a 2a 2a 20 49 74 20 69 73 20  hread..** It is 
2f7d0 69 6e 74 65 6e 64 65 64 20 74 68 61 74 20 74 68  intended that th
2f7e0 69 73 20 76 61 72 69 61 62 6c 65 20 62 65 20 73  is variable be s
2f7f0 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61  et once.** as pa
2f800 72 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e  rt of process in
2f810 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64  itialization and
2f820 20 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69   before any SQLi
2f830 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  te interface.** 
2f840 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65  routines have be
2f850 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68  en called and th
2f860 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  at this variable
2f870 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65   remain unchange
2f880 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e  d.** thereafter.
2f890 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 74 65 6d 70  .**.** The [temp
2f8a0 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
2f8b0 20 70 72 61 67 6d 61 5d 20 6d 61 79 20 6d 6f 64   pragma] may mod
2f8c0 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c  ify this variabl
2f8d0 65 20 61 6e 64 20 63 61 75 73 65 0a 2a 2a 20 69  e and cause.** i
2f8e0 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 6d 65  t to point to me
2f8f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
2f900 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
2f910 6f 63 5d 2e 20 20 46 75 72 74 68 65 72 6d 6f 72  oc].  Furthermor
2f920 65 2c 0a 2a 2a 20 74 68 65 20 5b 74 65 6d 70 5f  e,.** the [temp_
2f930 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
2f940 70 72 61 67 6d 61 5d 20 61 6c 77 61 79 73 20 61  pragma] always a
2f950 73 73 75 6d 65 73 20 74 68 61 74 20 61 6e 79 20  ssumes that any 
2f960 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74  string.** that t
2f970 68 69 73 20 76 61 72 69 61 62 6c 65 20 70 6f 69  his variable poi
2f980 6e 74 73 20 74 6f 20 69 73 20 68 65 6c 64 20 69  nts to is held i
2f990 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
2f9a0 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69  d from .** [sqli
2f9b0 74 65 33 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20  te3_malloc] and 
2f9c0 74 68 65 20 70 72 61 67 6d 61 20 6d 61 79 20 61  the pragma may a
2f9d0 74 74 65 6d 70 74 20 74 6f 20 66 72 65 65 20 74  ttempt to free t
2f9e0 68 61 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73  hat memory.** us
2f9f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  ing [sqlite3_fre
2fa00 65 5d 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66  e]..** Hence, if
2fa10 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   this variable i
2fa20 73 20 6d 6f 64 69 66 69 65 64 20 64 69 72 65 63  s modified direc
2fa30 74 6c 79 2c 20 65 69 74 68 65 72 20 69 74 20 73  tly, either it s
2fa40 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65  hould be.** made
2fa50 20 4e 55 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f   NULL or made to
2fa60 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79   point to memory
2fa70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
2fa80 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a  sqlite3_malloc].
2fa90 2a 2a 20 6f 72 20 65 6c 73 65 20 74 68 65 20 75  ** or else the u
2faa0 73 65 20 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f  se of the [temp_
2fab0 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
2fac0 70 72 61 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62  pragma] should b
2fad0 65 20 61 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51  e avoided..*/.SQ
2fae0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
2faf0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
2fb00 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ctory;../*.** CA
2fb10 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f 72  PI3REF: Test For
2fb20 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64   Auto-Commit Mod
2fb30 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 32  e {H12930} <S602
2fb40 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
2fb50 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64   {autocommit mod
2fb60 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  e}.**.** The sql
2fb70 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
2fb80 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 20  mit() interface 
2fb90 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
2fba0 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74   or.** zero if t
2fbb0 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73  he given databas
2fbc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
2fbd0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74  or is not in aut
2fbe0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a  ocommit mode,.**
2fbf0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20   respectively.  
2fc00 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20  Autocommit mode 
2fc10 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74  is on by default
2fc20 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20  ..** Autocommit 
2fc30 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 64  mode is disabled
2fc40 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74   by a [BEGIN] st
2fc50 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f  atement..** Auto
2fc60 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72  commit mode is r
2fc70 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b  e-enabled by a [
2fc80 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c  COMMIT] or [ROLL
2fc90 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  BACK]..**.** If 
2fca0 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66  certain kinds of
2fcb0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e   errors occur on
2fcc0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74   a statement wit
2fcd0 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74  hin a multi-stat
2fce0 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
2fcf0 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63  tion (errors inc
2fd00 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46  luding [SQLITE_F
2fd10 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f  ULL], [SQLITE_IO
2fd20 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  ERR],.** [SQLITE
2fd30 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45  _NOMEM], [SQLITE
2fd40 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c  _BUSY], and [SQL
2fd50 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20  ITE_INTERRUPT]) 
2fd60 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e  then the.** tran
2fd70 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65  saction might be
2fd80 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74   rolled back aut
2fd90 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65  omatically.  The
2fda0 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20   only way to.** 
2fdb0 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72  find out whether
2fdc0 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69   SQLite automati
2fdd0 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
2fde0 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
2fdf0 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72  n after.** an er
2fe00 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 68  ror is to use th
2fe10 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  is function..**.
2fe20 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  ** If another th
2fe30 72 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65  read changes the
2fe40 20 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74   autocommit stat
2fe50 75 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  us of the databa
2fe60 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
2fe70 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74   while this rout
2fe80 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20  ine is running, 
2fe90 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20  then the return 
2fea0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65  value.** is unde
2feb0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  fined..**.** Req
2fec0 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39  uirements: [H129
2fed0 33 31 5d 20 5b 48 31 32 39 33 32 5d 20 5b 48 31  31] [H12932] [H1
2fee0 32 39 33 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a  2933] [H12934].*
2fef0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ff00 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74   sqlite3_get_aut
2ff10 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a  ocommit(sqlite3*
2ff20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2ff30 45 46 3a 20 46 69 6e 64 20 54 68 65 20 44 61 74  EF: Find The Dat
2ff40 61 62 61 73 65 20 48 61 6e 64 6c 65 20 4f 66 20  abase Handle Of 
2ff50 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
2ff60 6d 65 6e 74 20 7b 48 31 33 31 32 30 7d 20 3c 53  ment {H13120} <S
2ff70 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  60600>.**.** The
2ff80 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64   sqlite3_db_hand
2ff90 6c 65 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  le interface ret
2ffa0 75 72 6e 73 20 74 68 65 20 5b 64 61 74 61 62 61  urns the [databa
2ffb0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68  se connection] h
2ffc0 61 6e 64 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63  andle.** to whic
2ffd0 68 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  h a [prepared st
2ffe0 61 74 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73  atement] belongs
2fff0 2e 20 20 54 68 65 20 5b 64 61 74 61 62 61 73 65  .  The [database
30000 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20   connection].** 
30010 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
30020 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73  te3_db_handle is
30030 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62   the same [datab
30040 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
30050 74 68 61 74 20 77 61 73 20 74 68 65 20 66 69 72  that was the fir
30060 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  st argument.** t
30070 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70  o the [sqlite3_p
30080 72 65 70 61 72 65 5f 76 32 28 29 5d 20 63 61 6c  repare_v2()] cal
30090 6c 20 28 6f 72 20 69 74 73 20 76 61 72 69 61 6e  l (or its varian
300a0 74 73 29 20 74 68 61 74 20 77 61 73 20 75 73 65  ts) that was use
300b0 64 20 74 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74  d to.** create t
300c0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  he statement in 
300d0 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e  the first place.
300e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
300f0 6e 74 73 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f  nts: [H13123].*/
30100 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
30110 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f  te3 *sqlite3_db_
30120 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73  handle(sqlite3_s
30130 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
30140 50 49 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65  PI3REF: Find the
30150 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73   next prepared s
30160 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31 34 30  tatement {H13140
30170 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60600>.**.**
30180 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
30190 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
301a0 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70  r to the next [p
301b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
301c0 74 5d 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d  t] after.** pStm
301d0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  t associated wit
301e0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  h the [database 
301f0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e  connection] pDb.
30200 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55    If pStmt is NU
30210 4c 4c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20  LL.** then this 
30220 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
30230 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
30240 68 65 20 66 69 72 73 74 20 70 72 65 70 61 72 65  he first prepare
30250 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
30260 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
30270 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
30280 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20  ection pDb.  If 
30290 6e 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74  no prepared stat
302a0 65 6d 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69  ement.** satisfi
302b0 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  es the condition
302c0 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  s of this routin
302d0 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  e, it returns NU
302e0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64  LL..**.** The [d
302f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30300 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e  on] pointer D in
30310 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73   a call to.** [s
30320 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
30330 28 44 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65  (D,S)] must refe
30340 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74  r to an open dat
30350 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
30360 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69  ion and in parti
30370 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62  cular must not b
30380 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
30390 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
303a0 65 6e 74 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b  ents: [H13143] [
303b0 48 31 33 31 34 36 5d 20 5b 48 31 33 31 34 39 5d  H13146] [H13149]
303c0 20 5b 48 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c   [H13152].*/.SQL
303d0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
303e0 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65  stmt *sqlite3_ne
303f0 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20  xt_stmt(sqlite3 
30400 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74  *pDb, sqlite3_st
30410 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
30420 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
30430 6d 69 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b  mit And Rollback
30440 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61   Notification Ca
30450 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 35 30 7d  llbacks {H12950}
30460 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60400>.**.** 
30470 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  The sqlite3_comm
30480 69 74 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66  it_hook() interf
30490 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20  ace registers a 
304a0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63  callback.** func
304b0 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b  tion to be invok
304c0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74 72  ed whenever a tr
304d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b 43 4f  ansaction is [CO
304e0 4d 4d 49 54 20 7c 20 63 6f 6d 6d 69 74 74 65 64  MMIT | committed
304f0 5d 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  ]..** Any callba
30500 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
30510 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
30520 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
30530 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
30540 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
30550 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
30560 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  dden..** The sql
30570 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
30580 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
30590 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
305a0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
305b0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
305c0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
305d0 74 69 6f 6e 20 69 73 20 5b 52 4f 4c 4c 42 41 43  tion is [ROLLBAC
305e0 4b 20 7c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 5d  K | rolled back]
305f0 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63  ..** Any callbac
30600 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69  k set by a previ
30610 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ous call to sqli
30620 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
30630 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d  ).** for the sam
30640 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
30650 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ction is overrid
30660 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67  den..** The pArg
30670 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73   argument is pas
30680 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74  sed through to t
30690 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20  he callback..** 
306a0 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  If the callback 
306b0 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b  on a commit hook
306c0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
306d0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74  s non-zero,.** t
306e0 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69  hen the commit i
306f0 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
30700 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a   a rollback..**.
30710 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75  ** If another fu
30720 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69  nction was previ
30730 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
30740 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61  , its.** pArg va
30750 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
30760 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
30770 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
30780 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
30790 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
307a0 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
307b0 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
307c0 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
307d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
307e0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
307f0 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79  e callback.  Any
30800 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d   actions.** to m
30810 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
30820 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  se connection mu
30830 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75  st be deferred u
30840 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a  ntil after the.*
30850 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20  * completion of 
30860 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
30870 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74  p()] call that t
30880 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d  riggered the com
30890 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61  mit.** or rollba
308a0 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66  ck hook in the f
308b0 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e  irst place..** N
308c0 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65  ote that [sqlite
308d0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
308e0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  and [sqlite3_ste
308f0 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79  p()] both modify
30900 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61   their.** databa
30910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66  se connections f
30920 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  or the meaning o
30930 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68  f "modify" in th
30940 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a  is paragraph..**
30950 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20  .** Registering 
30960 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20  a NULL function 
30970 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c  disables the cal
30980 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  lback..**.** Whe
30990 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f  n the commit hoo
309a0 6b 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  k callback routi
309b0 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c  ne returns zero,
309c0 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a   the [COMMIT].**
309d0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6c   operation is al
309e0 6c 6f 77 65 64 20 74 6f 20 63 6f 6e 74 69 6e 75  lowed to continu
309f0 65 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 49 66 20  e normally.  If 
30a00 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0a  the commit hook.
30a10 2a 2a 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ** returns non-z
30a20 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 5b 43  ero, then the [C
30a30 4f 4d 4d 49 54 5d 20 69 73 20 63 6f 6e 76 65 72  OMMIT] is conver
30a40 74 65 64 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c  ted into a [ROLL
30a50 42 41 43 4b 5d 2e 0a 2a 2a 20 54 68 65 20 72 6f  BACK]..** The ro
30a60 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69  llback hook is i
30a70 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c  nvoked on a roll
30a80 62 61 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74  back that result
30a90 73 20 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0a  s from a commit.
30aa0 2a 2a 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e  ** hook returnin
30ab0 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74  g non-zero, just
30ac0 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
30ad0 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72  with any other r
30ae0 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  ollback..**.** F
30af0 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
30b00 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74  of this API, a t
30b10 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61  ransaction is sa
30b20 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a  id to have been.
30b30 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69  ** rolled back i
30b40 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52  f an explicit "R
30b50 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65  OLLBACK" stateme
30b60 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  nt is executed, 
30b70 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  or.** an error o
30b80 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75  r constraint cau
30b90 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20  ses an implicit 
30ba0 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75  rollback to occu
30bb0 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61  r..** The rollba
30bc0 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ck callback is n
30bd0 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20  ot invoked if a 
30be0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
30bf0 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
30c00 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61  rolled back beca
30c10 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  use the database
30c20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
30c30 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f  losed..** The ro
30c40 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20  llback callback 
30c50 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69  is not invoked i
30c60 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
30c70 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  is.** rolled bac
30c80 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d  k because a comm
30c90 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  it callback retu
30ca0 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  rned non-zero..*
30cb0 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f  * <todo> Check o
30cc0 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a  n this </todo>.*
30cd0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
30ce0 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74  e [sqlite3_updat
30cf0 65 5f 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66  e_hook()] interf
30d00 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ace..**.** Requi
30d10 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
30d20 39 35 31 5d 20 5b 48 31 32 39 35 32 5d 20 5b 48  951] [H12952] [H
30d30 31 32 39 35 33 5d 20 5b 48 31 32 39 35 34 5d 20  12953] [H12954] 
30d40 5b 48 31 32 39 35 35 5d 0a 2a 2a 20 5b 48 31 32  [H12955].** [H12
30d50 39 36 31 5d 20 5b 48 31 32 39 36 32 5d 20 5b 48  961] [H12962] [H
30d60 31 32 39 36 33 5d 20 5b 48 31 32 39 36 34 5d 0a  12963] [H12964].
30d70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
30d80 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  id *sqlite3_comm
30d90 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a  it_hook(sqlite3*
30da0 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c  , int(*)(void*),
30db0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
30dc0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
30dd0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
30de0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a  sqlite3*, void(*
30df0 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a  )(void *), void*
30e00 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
30e10 45 46 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20  EF: Data Change 
30e20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c  Notification Cal
30e30 6c 62 61 63 6b 73 20 7b 48 31 32 39 37 30 7d 20  lbacks {H12970} 
30e40 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S60400>.**.** T
30e50 68 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  he sqlite3_updat
30e60 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61  e_hook() interfa
30e70 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63  ce registers a c
30e80 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
30e90 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61  .** with the [da
30ea0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
30eb0 6e 5d 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  n] identified by
30ec0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
30ed0 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76  ent.** to be inv
30ee0 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20  oked whenever a 
30ef0 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20  row is updated, 
30f00 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65  inserted or dele
30f10 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c  ted..** Any call
30f20 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72  back set by a pr
30f30 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 74  evious call to t
30f40 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
30f50 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74  for the same dat
30f60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
30f70 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a   is overridden..
30f80 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
30f90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
30fa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
30fb0 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
30fc0 20 77 68 65 6e 20 61 0a 2a 2a 20 72 6f 77 20 69   when a.** row i
30fd0 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72  s updated, inser
30fe0 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ted or deleted..
30ff0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
31000 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c  ument to the cal
31010 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20  lback is a copy 
31020 6f 66 20 74 68 65 20 74 68 69 72 64 20 61 72 67  of the third arg
31030 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69  ument.** to sqli
31040 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
31050 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  )..** The second
31060 20 63 61 6c 6c 62 61 63 6b 20 61 72 67 75 6d 65   callback argume
31070 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51  nt is one of [SQ
31080 4c 49 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53  LITE_INSERT], [S
31090 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a  QLITE_DELETE],.*
310a0 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44  * or [SQLITE_UPD
310b0 41 54 45 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20  ATE], depending 
310c0 6f 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  on the operation
310d0 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
310e0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20   callback.** to 
310f0 62 65 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54  be invoked..** T
31100 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  he third and fou
31110 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  rth arguments to
31120 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f   the callback co
31130 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74  ntain pointers t
31140 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
31150 65 20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d 65  e and table name
31160 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
31170 61 66 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a  affected row..**
31180 20 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62   The final callb
31190 61 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73  ack parameter is
311a0 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
311b0 74 68 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20 74  the row..** In t
311c0 68 65 20 63 61 73 65 20 6f 66 20 61 6e 20 75 70  he case of an up
311d0 64 61 74 65 2c 20 74 68 69 73 20 69 73 20 74 68  date, this is th
311e0 65 20 5b 72 6f 77 69 64 5d 20 61 66 74 65 72 20  e [rowid] after 
311f0 74 68 65 20 75 70 64 61 74 65 20 74 61 6b 65 73  the update takes
31200 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   place..**.** Th
31210 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 73  e update hook is
31220 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77 68 65   not invoked whe
31230 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73 74 65  n internal syste
31240 6d 20 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20  m tables are.** 
31250 6d 6f 64 69 66 69 65 64 20 28 69 2e 65 2e 20 73  modified (i.e. s
31260 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64  qlite_master and
31270 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
31280 29 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  )..**.** In the 
31290 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
312a0 74 61 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61  tation, the upda
312b0 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f  te hook.** is no
312c0 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64  t invoked when d
312d0 75 70 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20  uplication rows 
312e0 61 72 65 20 64 65 6c 65 74 65 64 20 62 65 63 61  are deleted beca
312f0 75 73 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e  use of an.** [ON
31300 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43   CONFLICT | ON C
31310 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d  ONFLICT REPLACE]
31320 20 63 6c 61 75 73 65 2e 20 20 4e 6f 72 20 69 73   clause.  Nor is
31330 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31340 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e  .** invoked when
31350 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65   rows are delete
31360 64 20 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75  d using the [tru
31370 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  ncate optimizati
31380 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65  on]..** The exce
31390 70 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69  ptions defined i
313a0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
313b0 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e   might change in
313c0 20 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c   a future.** rel
313d0 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ease of SQLite..
313e0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
313f0 20 68 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   hook implementa
31400 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f  tion must not do
31410 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77   anything that w
31420 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68  ill modify.** th
31430 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
31440 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
31450 65 64 20 74 68 65 20 75 70 64 61 74 65 20 68 6f  ed the update ho
31460 6f 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73  ok.  Any actions
31470 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  .** to modify th
31480 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
31490 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65  ction must be de
314a0 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74  ferred until aft
314b0 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  er the.** comple
314c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
314d0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c  ite3_step()] cal
314e0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  l that triggered
314f0 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31500 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
31510 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
31520 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
31530 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
31540 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
31550 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
31560 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
31570 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
31580 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
31590 70 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  ph..**.** If ano
315a0 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61  ther function wa
315b0 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  s previously reg
315c0 69 73 74 65 72 65 64 2c 20 69 74 73 20 70 41 72  istered, its pAr
315d0 67 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65  g value.** is re
315e0 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69  turned.  Otherwi
315f0 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  se NULL is retur
31600 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ned..**.** See a
31610 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
31620 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20  _commit_hook()] 
31630 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c  and [sqlite3_rol
31640 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0a 2a 2a  lback_hook()].**
31650 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 0a   interfaces..**.
31660 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
31670 0a 2a 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31  .** [H12971] [H1
31680 32 39 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b  2973] [H12975] [
31690 48 31 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d  H12977] [H12979]
316a0 20 5b 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38   [H12981] [H1298
316b0 33 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53  3] [H12986].*/.S
316c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
316d0 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
316e0 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c  ook(.  sqlite3*,
316f0 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   .  void(*)(void
31700 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e   *,int ,char con
31710 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20  st *,char const 
31720 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *,sqlite3_int64)
31730 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a  ,.  void*.);../*
31740 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
31750 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
31760 53 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63  Shared Pager Cac
31770 68 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30  he {H10330} <S30
31780 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  900>.** KEYWORDS
31790 3a 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d  : {shared cache}
317a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
317b0 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64  ine enables or d
317c0 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72  isables the shar
317d0 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  ing of the datab
317e0 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64  ase cache.** and
317f0 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72   schema data str
31800 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20  uctures between 
31810 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
31820 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f  tion | connectio
31830 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61  ns].** to the sa
31840 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61  me database. Sha
31850 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  ring is enabled 
31860 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
31870 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64  is true.** and d
31880 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61  isabled if the a
31890 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
318a0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68  ..**.** Cache sh
318b0 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
318c0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f   and disabled fo
318d0 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63  r an entire proc
318e0 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ess..** This is 
318f0 61 20 63 68 61 6e 67 65 20 61 73 20 6f 66 20 53  a change as of S
31900 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
31910 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 20 76 65  5.0. In prior ve
31920 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
31930 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 77 61 73  ,.** sharing was
31940 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61   enabled or disa
31950 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20 74 68  bled for each th
31960 72 65 61 64 20 73 65 70 61 72 61 74 65 6c 79 2e  read separately.
31970 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65  .**.** The cache
31980 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 20 73 65   sharing mode se
31990 74 20 62 79 20 74 68 69 73 20 69 6e 74 65 72 66  t by this interf
319a0 61 63 65 20 65 66 66 65 63 74 73 20 61 6c 6c 20  ace effects all 
319b0 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61  subsequent.** ca
319c0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
319d0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
319e0 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e  3_open_v2()], an
319f0 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  d [sqlite3_open1
31a00 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69 6e  6()]..** Existin
31a10 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
31a20 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20  ctions continue 
31a30 75 73 65 20 74 68 65 20 73 68 61 72 69 6e 67 20  use the sharing 
31a40 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 20 77 61 73  mode.** that was
31a50 20 69 6e 20 65 66 66 65 63 74 20 61 74 20 74 68   in effect at th
31a60 65 20 74 69 6d 65 20 74 68 65 79 20 77 65 72 65  e time they were
31a70 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56   opened..**.** V
31a80 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61  irtual tables ca
31a90 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74  nnot be used wit
31aa0 68 20 61 20 73 68 61 72 65 64 20 63 61 63 68 65  h a shared cache
31ab0 2e 20 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a  .  When shared.*
31ac0 2a 20 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c  * cache is enabl
31ad0 65 64 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ed, the [sqlite3
31ae0 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29  _create_module()
31af0 5d 20 41 50 49 20 75 73 65 64 20 74 6f 20 72 65  ] API used to re
31b00 67 69 73 74 65 72 0a 2a 2a 20 76 69 72 74 75 61  gister.** virtua
31b10 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 61 6c  l tables will al
31b20 77 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 65  ways return an e
31b30 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rror..**.** This
31b40 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
31b50 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20   [SQLITE_OK] if 
31b60 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61 73  shared cache was
31b70 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61   enabled or disa
31b80 62 6c 65 64 0a 2a 2a 20 73 75 63 63 65 73 73 66  bled.** successf
31b90 75 6c 6c 79 2e 20 20 41 6e 20 5b 65 72 72 6f 72  ully.  An [error
31ba0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
31bb0 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ed otherwise..**
31bc0 0a 2a 2a 20 53 68 61 72 65 64 20 63 61 63 68 65  .** Shared cache
31bd0 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20   is disabled by 
31be0 64 65 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69  default. But thi
31bf0 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  s might change i
31c00 6e 0a 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65  n.** future rele
31c10 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20  ases of SQLite. 
31c20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
31c30 61 74 20 63 61 72 65 20 61 62 6f 75 74 20 73 68  at care about sh
31c40 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65  ared.** cache se
31c50 74 74 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74  tting should set
31c60 20 69 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a   it explicitly..
31c70 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20  **.** See Also: 
31c80 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d   [SQLite Shared-
31c90 43 61 63 68 65 20 4d 6f 64 65 5d 0a 2a 2a 0a 2a  Cache Mode].**.*
31ca0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
31cb0 5b 48 31 30 33 33 31 5d 20 5b 48 31 30 33 33 36  [H10331] [H10336
31cc0 5d 20 5b 48 31 30 33 33 37 5d 20 5b 48 31 30 33  ] [H10337] [H103
31cd0 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  39].*/.SQLITE_AP
31ce0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
31cf0 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
31d00 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e(int);../*.** C
31d10 41 50 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74  API3REF: Attempt
31d20 20 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65   To Free Heap Me
31d30 6d 6f 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53  mory {H17340} <S
31d40 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30220>.**.** The
31d50 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
31d60 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66  _memory() interf
31d70 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ace attempts to 
31d80 66 72 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20  free N bytes.** 
31d90 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62  of heap memory b
31da0 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e  y deallocating n
31db0 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
31dc0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a  ory allocations.
31dd0 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64  ** held by the d
31de0 61 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e  atabase library.
31df0 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75   {END}  Memory u
31e00 73 65 64 20 74 6f 20 63 61 63 68 65 20 64 61 74  sed to cache dat
31e10 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74  abase.** pages t
31e20 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72  o improve perfor
31e30 6d 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d  mance is an exam
31e40 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e  ple of non-essen
31e50 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20  tial memory..** 
31e60 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
31e70 6d 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73  memory() returns
31e80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
31e90 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72  ytes actually fr
31ea0 65 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69  eed,.** which mi
31eb0 67 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c  ght be more or l
31ec0 65 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f  ess than the amo
31ed0 75 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a  unt requested..*
31ee0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
31ef0 73 3a 20 5b 48 31 37 33 34 31 5d 20 5b 48 31 37  s: [H17341] [H17
31f00 33 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  342].*/.SQLITE_A
31f10 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
31f20 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e  elease_memory(in
31f30 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
31f40 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69  REF: Impose A Li
31f50 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69 7a 65  mit On Heap Size
31f60 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 30 32 32   {H17350} <S3022
31f70 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
31f80 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
31f90 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  imit() interface
31fa0 20 70 6c 61 63 65 73 20 61 20 22 73 6f 66 74 22   places a "soft"
31fb0 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65   limit.** on the
31fc0 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20   amount of heap 
31fd0 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20  memory that may 
31fe0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
31ff0 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e  SQLite..** If an
32000 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61   internal alloca
32010 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65  tion is requeste
32020 64 20 74 68 61 74 20 77 6f 75 6c 64 20 65 78 63  d that would exc
32030 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20  eed the.** soft 
32040 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c  heap limit, [sql
32050 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
32060 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65  ory()] is invoke
32070 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65  d one or.** more
32080 20 74 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75   times to free u
32090 70 20 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66  p some space bef
320a0 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  ore the allocati
320b0 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e  on is performed.
320c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74  .**.** The limit
320d0 20 69 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74   is called "soft
320e0 22 2c 20 62 65 63 61 75 73 65 20 69 66 20 5b 73  ", because if [s
320f0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
32100 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e  emory()].** cann
32110 6f 74 20 66 72 65 65 20 73 75 66 66 69 63 69 65  ot free sufficie
32120 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65  nt memory to pre
32130 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66  vent the limit f
32140 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64  rom being exceed
32150 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72  ed,.** the memor
32160 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  y is allocated a
32170 6e 79 77 61 79 20 61 6e 64 20 74 68 65 20 63 75  nyway and the cu
32180 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrent operation 
32190 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20  proceeds..**.** 
321a0 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65  A negative or ze
321b0 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d  ro value for N m
321c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20  eans that there 
321d0 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 61 70 20  is no soft heap 
321e0 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71  limit and.** [sq
321f0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
32200 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c  mory()] will onl
32210 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
32220 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 68 61 75   memory is exhau
32230 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66  sted..** The def
32240 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
32250 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
32260 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  it is zero..**.*
32270 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61  * SQLite makes a
32280 20 62 65 73 74 20 65 66 66 6f 72 74 20 74 6f 20   best effort to 
32290 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74 20 68  honor the soft h
322a0 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75  eap limit..** Bu
322b0 74 20 69 66 20 74 68 65 20 73 6f 66 74 20 68 65  t if the soft he
322c0 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20  ap limit cannot 
322d0 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63  be honored, exec
322e0 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f  ution will.** co
322f0 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74 20 65  ntinue without e
32300 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63 61  rror or notifica
32310 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 77  tion.  This is w
32320 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69 73 0a  hy the limit is.
32330 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73 6f 66  ** called a "sof
32340 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20 69 73  t" limit.  It is
32350 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a   advisory only..
32360 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53  **.** Prior to S
32370 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
32380 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  5.0, this routin
32390 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e  e only constrain
323a0 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a  ed the memory.**
323b0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20   allocated by a 
323c0 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 2d 20  single thread - 
323d0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
323e0 69 6e 20 77 68 69 63 68 20 74 68 69 73 20 72 6f  in which this ro
323f0 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20  utine.** runs.  
32400 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  Beginning with S
32410 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
32420 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 20 68 65  5.0, the soft he
32430 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61  ap limit is.** a
32440 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c 20 74 68  pplied to all th
32450 72 65 61 64 73 2e 20 54 68 65 20 76 61 6c 75 65  reads. The value
32460 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 20 74   specified for t
32470 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
32480 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 70 70 65  it.** is an uppe
32490 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74  r bound on the t
324a0 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  otal memory allo
324b0 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74  cation for all t
324c0 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65  hreads. In.** ve
324d0 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72  rsion 3.5.0 ther
324e0 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73  e is no mechanis
324f0 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74  m for limiting t
32500 68 65 20 68 65 61 70 20 75 73 61 67 65 20 66 6f  he heap usage fo
32510 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  r.** individual 
32520 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 52  threads..**.** R
32530 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
32540 5b 48 31 36 33 35 31 5d 20 5b 48 31 36 33 35 32  [H16351] [H16352
32550 5d 20 5b 48 31 36 33 35 33 5d 20 5b 48 31 36 33  ] [H16353] [H163
32560 35 34 5d 20 5b 48 31 36 33 35 35 5d 20 5b 48 31  54] [H16355] [H1
32570 36 33 35 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  6358].*/.SQLITE_
32580 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
32590 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
325a0 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  (int);../*.** CA
325b0 50 49 33 52 45 46 3a 20 45 78 74 72 61 63 74 20  PI3REF: Extract 
325c0 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74 20 41  Metadata About A
325d0 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62   Column Of A Tab
325e0 6c 65 20 7b 48 31 32 38 35 30 7d 20 3c 53 36 30  le {H12850} <S60
325f0 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  300>.**.** This 
32600 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
32610 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 20 61  metadata about a
32620 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
32630 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 0a 2a   of a specific.*
32640 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  * database table
32650 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e   accessible usin
32660 67 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  g the [database 
32670 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64  connection] hand
32680 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20  le.** passed as 
32690 74 68 65 20 66 69 72 73 74 20 66 75 6e 63 74 69  the first functi
326a0 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  on argument..**.
326b0 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73  ** The column is
326c0 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
326d0 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64  he second, third
326e0 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
326f0 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69  meters to.** thi
32700 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  s function. The 
32710 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
32720 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 6e   is either the n
32730 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
32740 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d 61  ase.** (i.e. "ma
32750 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 61  in", "temp" or a
32760 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  n attached datab
32770 61 73 65 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ase) containing 
32780 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
32790 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20   table or NULL. 
327a0 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74  If it is NULL, t
327b0 68 65 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64  hen all attached
327c0 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73   databases are s
327d0 65 61 72 63 68 65 64 0a 2a 2a 20 66 6f 72 20 74  earched.** for t
327e0 68 65 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74  he table using t
327f0 68 65 20 73 61 6d 65 20 61 6c 67 6f 72 69 74 68  he same algorith
32800 6d 20 75 73 65 64 20 62 79 20 74 68 65 20 64 61  m used by the da
32810 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 74 6f  tabase engine to
32820 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 75 6e 71 75  .** resolve unqu
32830 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 72 65  alified table re
32840 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  ferences..**.** 
32850 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f  The third and fo
32860 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20  urth parameters 
32870 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
32880 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 20 61   are the table a
32890 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d  nd column.** nam
328a0 65 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64  e of the desired
328b0 20 63 6f 6c 75 6d 6e 2c 20 72 65 73 70 65 63 74   column, respect
328c0 69 76 65 6c 79 2e 20 4e 65 69 74 68 65 72 20 6f  ively. Neither o
328d0 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65  f these paramete
328e0 72 73 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  rs.** may be NUL
328f0 4c 2e 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 64 61 74  L..**.** Metadat
32900 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  a is returned by
32910 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
32920 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
32930 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 35   passed as the 5
32940 74 68 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71  th.** and subseq
32950 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20  uent parameters 
32960 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
32970 2e 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61  . Any of these a
32980 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0a  rguments may be.
32990 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69 63  ** NULL, in whic
329a0 68 20 63 61 73 65 20 74 68 65 20 63 6f 72 72 65  h case the corre
329b0 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e 74  sponding element
329c0 20 6f 66 20 6d 65 74 61 64 61 74 61 20 69 73 20   of metadata is 
329d0 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  omitted..**.** <
329e0 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c  blockquote>.** <
329f0 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22  table border="1"
32a00 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 50 61  >.** <tr><th> Pa
32a10 72 61 6d 65 74 65 72 20 3c 74 68 3e 20 4f 75 74  rameter <th> Out
32a20 70 75 74 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  put<br>Type <th>
32a30 20 20 44 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a    Description.**
32a40 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 35 74 68  .** <tr><td> 5th
32a50 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72   <td> const char
32a60 2a 20 3c 74 64 3e 20 44 61 74 61 20 74 79 70 65  * <td> Data type
32a70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 36 74 68  .** <tr><td> 6th
32a80 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72   <td> const char
32a90 2a 20 3c 74 64 3e 20 4e 61 6d 65 20 6f 66 20 64  * <td> Name of d
32aa0 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
32ab0 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 3c 74 72   sequence.** <tr
32ac0 3e 3c 74 64 3e 20 37 74 68 20 3c 74 64 3e 20 69  ><td> 7th <td> i
32ad0 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20  nt         <td> 
32ae0 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 68  True if column h
32af0 61 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  as a NOT NULL co
32b00 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e  nstraint.** <tr>
32b10 3c 74 64 3e 20 38 74 68 20 3c 74 64 3e 20 69 6e  <td> 8th <td> in
32b20 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54  t         <td> T
32b30 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
32b40 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 49   part of the PRI
32b50 4d 41 52 59 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e  MARY KEY.** <tr>
32b60 3c 74 64 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e  <td> 9th <td> in
32b70 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54  t         <td> T
32b80 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
32b90 20 5b 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d   [AUTOINCREMENT]
32ba0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20  .** </table>.** 
32bb0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
32bc0 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 70  .** The memory p
32bd0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
32be0 20 63 68 61 72 61 63 74 65 72 20 70 6f 69 6e 74   character point
32bf0 65 72 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72  ers returned for
32c00 20 74 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74   the.** declarat
32c10 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c  ion type and col
32c20 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
32c30 69 73 20 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e  is valid only un
32c40 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  til the next.** 
32c50 63 61 6c 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69  call to any SQLi
32c60 74 65 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  te API function.
32c70 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70  .**.** If the sp
32c80 65 63 69 66 69 65 64 20 74 61 62 6c 65 20 69 73  ecified table is
32c90 20 61 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77   actually a view
32ca0 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
32cb0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ] is returned..*
32cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63  *.** If the spec
32cd0 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20  ified column is 
32ce0 22 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f  "rowid", "oid" o
32cf0 72 20 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20  r "_rowid_" and 
32d00 61 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50  an.** [INTEGER P
32d10 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75  RIMARY KEY] colu
32d20 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c  mn has been expl
32d30 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c  icitly declared,
32d40 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74   then the output
32d50 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61  .** parameters a
32d60 72 65 20 73 65 74 20 66 6f 72 20 74 68 65 20 65  re set for the e
32d70 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
32d80 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68  ed column. If th
32d90 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 70  ere is no.** exp
32da0 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
32db0 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   [INTEGER PRIMAR
32dc0 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74  Y KEY] column, t
32dd0 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  hen the output.*
32de0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
32df0 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a   set as follows:
32e00 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20  .**.** <pre>.** 
32e10 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 22      data type: "
32e20 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20  INTEGER".**     
32e30 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32e40 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20  ce: "BINARY".** 
32e50 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a      not null: 0.
32e60 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b  **     primary k
32e70 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74  ey: 1.**     aut
32e80 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a  o increment: 0.*
32e90 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54  * </pre>.**.** T
32ea0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
32eb0 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   load one or mor
32ec0 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64  e schemas from d
32ed0 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49  atabase files. I
32ee0 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  f an.** error oc
32ef0 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 73  curs during this
32f00 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20   process, or if 
32f10 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 61  the requested ta
32f20 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a  ble or column.**
32f30 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
32f40 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
32f50 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
32f60 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
32f70 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68  ge left.** in th
32f80 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
32f90 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72  ection] (to be r
32fa0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 73  etrieved using s
32fb0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29  qlite3_errmsg())
32fc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ..**.** This API
32fd0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
32fe0 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  le if the librar
32ff0 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  y was compiled w
33000 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ith the.** [SQLI
33010 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
33020 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65  _METADATA] C-pre
33030 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c  processor symbol
33040 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c   defined..*/.SQL
33050 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33060 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
33070 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c  _metadata(.  sql
33080 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33090 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
330a0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f  ection handle */
330b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
330c0 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f  DbName,        /
330d0 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * Database name 
330e0 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
330f0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e  st char *zTableN
33100 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  ame,     /* Tabl
33110 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  e name */.  cons
33120 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e  t char *zColumnN
33130 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  ame,    /* Colum
33140 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72  n name */.  char
33150 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54   const **pzDataT
33160 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55  ype,    /* OUTPU
33170 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61  T: Declared data
33180 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20   type */.  char 
33190 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65  const **pzCollSe
331a0 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54  q,     /* OUTPUT
331b0 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  : Collation sequ
331c0 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  ence name */.  i
331d0 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20  nt *pNotNull,   
331e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
331f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f  TPUT: True if NO
33200 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
33210 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e  t exists */.  in
33220 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20  t *pPrimaryKey, 
33230 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
33240 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
33250 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a  umn part of PK *
33260 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e  /.  int *pAutoin
33270 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
33280 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
33290 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74  if column is aut
332a0 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29  o-increment */.)
332b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
332c0 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e  F: Load An Exten
332d0 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53  sion {H12600} <S
332e0 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  20500>.**.** Thi
332f0 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64  s interface load
33300 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65  s an SQLite exte
33310 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72  nsion library fr
33320 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  om the named fil
33330 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31  e..**.** {H12601
33340 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
33350 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
33360 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74  nterface attempt
33370 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20  s to load an.** 
33380 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20           SQLite 
33390 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72  extension librar
333a0 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  y contained in t
333b0 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a  he file zFile..*
333c0 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68  *.** {H12602} Th
333d0 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73  e entry point is
333e0 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zProc..**.** {H
333f0 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79  12603} zProc may
33400 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20   be 0, in which 
33410 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66  case the name of
33420 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   the entry point
33430 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 66  .**          def
33440 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65  aults to "sqlite
33450 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
33460 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34  "..**.** {H12604
33470 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
33480 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
33490 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72  nterface shall r
334a0 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20  eturn.**        
334b0 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e    [SQLITE_OK] on
334c0 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51   success and [SQ
334d0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73  LITE_ERROR] if s
334e0 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
334f0 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ong..**.** {H126
33500 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20  05} If an error 
33510 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
33520 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
33530 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
33540 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64     [sqlite3_load
33550 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e  _extension()] in
33560 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74  terface shall at
33570 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20  tempt to.**     
33580 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72       fill *pzErr
33590 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d  Msg with error m
335a0 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f 72  essage text stor
335b0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ed in memory.** 
335c0 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65           obtaine
335d0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
335e0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d  malloc()]. {END}
335f0 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
33600 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  nction.**       
33610 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74     should free t
33620 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61  his memory by ca
33630 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  lling [sqlite3_f
33640 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ree()]..**.** {H
33650 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e  12606} Extension
33660 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65   loading must be
33670 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a   enabled using.*
33680 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
33690 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
336a0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69  extension()] pri
336b0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
336c0 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20  is API,.**      
336d0 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e      otherwise an
336e0 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
336f0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  eturned..**.** S
33700 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 6c 6f 61  ee also the [loa
33710 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 53 51  d_extension() SQ
33720 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2f 0a  L function]..*/.
33730 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33740 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
33750 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  nsion(.  sqlite3
33760 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
33770 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
33780 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
33790 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
337a0 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
337b0 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a  ar *zFile,    /*
337c0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61   Name of the sha
337d0 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  red library cont
337e0 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e  aining extension
337f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
33800 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45   *zProc,    /* E
33810 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72  ntry point.  Der
33820 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20  ived from zFile 
33830 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a  if 0 */.  char *
33840 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
33850 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73  /* Put error mes
33860 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74  sage here if not
33870 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   0 */.);../*.** 
33880 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65  CAPI3REF: Enable
33890 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65   Or Disable Exte
338a0 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48  nsion Loading {H
338b0 31 32 36 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12620} <S20500>.
338c0 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20  **.** So as not 
338d0 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79  to open security
338e0 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20   holes in older 
338f0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
33900 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61  t are.** unprepa
33910 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68  red to deal with
33920 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
33930 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61  ng, and as a mea
33940 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a  ns of disabling.
33950 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ** extension loa
33960 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75  ding while evalu
33970 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72  ating user-enter
33980 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c  ed SQL, the foll
33990 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20  owing API.** is 
339a0 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e  provided to turn
339b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f   the [sqlite3_lo
339c0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  ad_extension()] 
339d0 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64  mechanism on and
339e0 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65   off..**.** Exte
339f0 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
33a00 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e   off by default.
33a10 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36   See ticket #186
33a20 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31  3..**.** {H12621
33a30 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74  } Call the sqlit
33a40 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
33a50 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69  xtension() routi
33a60 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31  ne with onoff==1
33a70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20  .**          to 
33a80 74 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c  turn extension l
33a90 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61  oading on and ca
33aa0 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66  ll it with onoff
33ab0 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20  ==0 to turn.**  
33ac0 20 20 20 20 20 20 20 20 69 74 20 62 61 63 6b 20          it back 
33ad0 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a  off again..**.**
33ae0 20 7b 48 31 32 36 32 32 7d 20 45 78 74 65 6e 73   {H12622} Extens
33af0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f  ion loading is o
33b00 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a  ff by default..*
33b10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33b20 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
33b30 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73  load_extension(s
33b40 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
33b50 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  onoff);../*.** C
33b60 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74  API3REF: Automat
33b70 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45  ically Load An E
33b80 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34  xtensions {H1264
33b90 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
33ba0 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62  * This API can b
33bb0 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f  e invoked at pro
33bc0 67 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20  gram startup in 
33bd0 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65  order to registe
33be0 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  r.** one or more
33bf0 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
33c00 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68  ed extensions th
33c10 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c  at will be avail
33c20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e  able.** to all n
33c30 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ew [database con
33c40 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d  nections]. {END}
33c50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
33c60 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69  ine stores a poi
33c70 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65  nter to the exte
33c80 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61  nsion in an arra
33c90 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74  y that is.** obt
33ca0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
33cb0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20  te3_malloc()].  
33cc0 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d  If you run a mem
33cd0 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 6b 65 72  ory leak checker
33ce0 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67  .** on your prog
33cf0 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f 72  ram and it repor
33d00 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75 73  ts a leak becaus
33d10 65 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 2c  e of this array,
33d20 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69   invoke.** [sqli
33d30 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
33d40 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f  xtension()] prio
33d50 72 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f  r to shutdown to
33d60 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79   free the memory
33d70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d  ..**.** {H12641}
33d80 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
33d90 65 67 69 73 74 65 72 73 20 61 6e 20 65 78 74 65  egisters an exte
33da0 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e  nsion entry poin
33db0 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 20 20  t that is.**    
33dc0 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61        automatica
33dd0 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e  lly invoked when
33de0 65 76 65 72 20 61 20 6e 65 77 20 5b 64 61 74 61  ever a new [data
33df0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
33e00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
33e10 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b 73 71  opened using [sq
33e20 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b  lite3_open()], [
33e30 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
33e40 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ],.**          o
33e50 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
33e60 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  v2()]..**.** {H1
33e70 32 36 34 32 7d 20 44 75 70 6c 69 63 61 74 65 20  2642} Duplicate 
33e80 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 20 64  extensions are d
33e90 65 74 65 63 74 65 64 20 73 6f 20 63 61 6c 6c 69  etected so calli
33ea0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  ng this routine.
33eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74  **          mult
33ec0 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 20  iple times with 
33ed0 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69  the same extensi
33ee0 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a  on is harmless..
33ef0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54  **.** {H12643} T
33f00 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72  his routine stor
33f10 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
33f20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e  the extension in
33f30 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 20 20   an array.**    
33f40 20 20 20 20 20 20 74 68 61 74 20 69 73 20 6f 62        that is ob
33f50 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
33f60 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a  ite3_malloc()]..
33f70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41  **.** {H12644} A
33f80 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
33f90 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 6f 73 73  ons apply across
33fa0 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f   all threads..*/
33fb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33fc0 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
33fd0 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 45  ension(void (*xE
33fe0 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 64 29  ntryPoint)(void)
33ff0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
34000 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61  EF: Reset Automa
34010 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f  tic Extension Lo
34020 61 64 69 6e 67 20 7b 48 31 32 36 36 30 7d 20 3c  ading {H12660} <
34030 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20500>.**.** Th
34040 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61  is function disa
34050 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75  bles all previou
34060 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 61  sly registered a
34070 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 74 65  utomatic.** exte
34080 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 49  nsions. {END}  I
34090 74 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66  t undoes the eff
340a0 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72  ect of all prior
340b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 75 74  .** [sqlite3_aut
340c0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 63  o_extension()] c
340d0 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  alls..**.** {H12
340e0 36 36 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69  661} This functi
340f0 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20  on disables all 
34100 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
34110 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  tered.**        
34120 20 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65    automatic exte
34130 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nsions..**.** {H
34140 31 32 36 36 32 7d 20 54 68 69 73 20 66 75 6e 63  12662} This func
34150 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 75  tion disables au
34160 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
34170 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 61 64  ns in all thread
34180 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
34190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
341a0 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  set_auto_extensi
341b0 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  on(void);../*.**
341c0 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  **** EXPERIMENTA
341d0 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63  L - subject to c
341e0 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f  hange without no
341f0 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tice ***********
34200 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ***.**.** The in
34210 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76  terface to the v
34220 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
34230 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e  hanism is curren
34240 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  tly considered.*
34250 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65  * to be experime
34260 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72  ntal.  The inter
34270 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67  face might chang
34280 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  e in incompatibl
34290 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68  e ways..** If th
342a0 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20  is is a problem 
342b0 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20  for you, do not 
342c0 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63  use the interfac
342d0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  e at this time..
342e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76  **.** When the v
342f0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
34300 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65  hanism stabilize
34310 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61  s, we will decla
34320 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66  re the.** interf
34330 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f  ace fixed, suppo
34340 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65  rt it indefinite
34350 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  ly, and remove t
34360 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a  his comment..*/.
34370 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
34380 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69  s used by the vi
34390 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65  rtual table inte
343a0 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66  rface.*/.typedef
343b0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
343c0 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61  vtab sqlite3_vta
343d0 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  b;.typedef struc
343e0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
343f0 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64  info sqlite3_ind
34400 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66  ex_info;.typedef
34410 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
34420 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69  vtab_cursor sqli
34430 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b  te3_vtab_cursor;
34440 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34450 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73  sqlite3_module s
34460 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a  qlite3_module;..
34470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
34480 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62  Virtual Table Ob
34490 6a 65 63 74 20 7b 48 31 38 30 30 30 7d 20 3c 53  ject {H18000} <S
344a0 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20400>.** KEYWOR
344b0 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  DS: sqlite3_modu
344c0 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c  le {virtual tabl
344d0 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 58 50  e module}.** EXP
344e0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
344f0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20  This structure, 
34500 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64  sometimes called
34510 20 61 20 61 20 22 76 69 72 74 75 61 6c 20 74 61   a a "virtual ta
34520 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a  ble module", .**
34530 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6d 70   defines the imp
34540 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
34550 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73   [virtual tables
34560 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 74 72  ].  .** This str
34570 75 63 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20  ucture consists 
34580 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64  mostly of method
34590 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
345a0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61  ..**.** A virtua
345b0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69  l table module i
345c0 73 20 63 72 65 61 74 65 64 20 62 79 20 66 69 6c  s created by fil
345d0 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69 73  ling in a persis
345e0 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  tent.** instance
345f0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
34600 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67 20 61  re and passing a
34610 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
34620 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20   instance.** to 
34630 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
34640 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71  module()] or [sq
34650 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
34660 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 54 68  ule_v2()]..** Th
34670 65 20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72  e registration r
34680 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74  emains valid unt
34690 69 6c 20 69 74 20 69 73 20 72 65 70 6c 61 63 65  il it is replace
346a0 64 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74  d by a different
346b0 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e  .** module or un
346c0 74 69 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73  til the [databas
346d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c  e connection] cl
346e0 6f 73 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65  oses.  The conte
346f0 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  nt.** of this st
34700 72 75 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74  ructure must not
34710 20 63 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74   change while it
34720 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77   is registered w
34730 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62  ith.** any datab
34740 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
34750 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
34760 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74  3_module {.  int
34770 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74   iVersion;.  int
34780 20 28 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69   (*xCreate)(sqli
34790 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78  te3*, void *pAux
347a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
347b0 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
347c0 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
347d0 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
347e0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
347f0 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29  *ppVTab, char**)
34800 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65  ;.  int (*xConne
34810 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f  ct)(sqlite3*, vo
34820 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20  id *pAux,.      
34830 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67           int arg
34840 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
34850 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20  onst*argv,.     
34860 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
34870 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c  3_vtab **ppVTab,
34880 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20   char**);.  int 
34890 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28 73 71  (*xBestIndex)(sq
348a0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
348b0 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  b, sqlite3_index
348c0 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28  _info*);.  int (
348d0 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71  *xDisconnect)(sq
348e0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
348f0 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73  b);.  int (*xDes
34900 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74  troy)(sqlite3_vt
34910 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
34920 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
34930 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
34940 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34950 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b  sor **ppCursor);
34960 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29  .  int (*xClose)
34970 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
34980 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
34990 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33  xFilter)(sqlite3
349a0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69  _vtab_cursor*, i
349b0 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74  nt idxNum, const
349c0 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20   char *idxStr,. 
349d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
349e0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
349f0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a  _value **argv);.
34a00 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73    int (*xNext)(s
34a10 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
34a20 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45  or*);.  int (*xE
34a30 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  of)(sqlite3_vtab
34a40 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
34a50 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69   (*xColumn)(sqli
34a60 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
34a70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
34a80 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20  t*, int);.  int 
34a90 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65  (*xRowid)(sqlite
34aa0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20  3_vtab_cursor*, 
34ab0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
34ac0 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a  Rowid);.  int (*
34ad0 78 55 70 64 61 74 65 29 28 73 71 6c 69 74 65 33  xUpdate)(sqlite3
34ae0 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71  _vtab *, int, sq
34af0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20  lite3_value **, 
34b00 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29  sqlite3_int64 *)
34b10 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e  ;.  int (*xBegin
34b20 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34b30 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34b40 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76  xSync)(sqlite3_v
34b50 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69  tab *pVTab);.  i
34b60 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71  nt (*xCommit)(sq
34b70 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
34b80 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c  b);.  int (*xRol
34b90 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76  lback)(sqlite3_v
34ba0 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69  tab *pVTab);.  i
34bb0 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69  nt (*xFindFuncti
34bc0 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  on)(sqlite3_vtab
34bd0 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72   *pVtab, int nAr
34be0 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  g, const char *z
34bf0 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
34c00 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
34c10 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c  d (**pxFunc)(sql
34c20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
34c30 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
34c40 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  *),.            
34c50 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20             void 
34c60 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20  **ppArg);.  int 
34c70 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74  (*xRename)(sqlit
34c80 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
34c90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77  const char *zNew
34ca0 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  );.};../*.** CAP
34cb0 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
34cc0 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 49 6e  able Indexing In
34cd0 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 31 30  formation {H1810
34ce0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
34cf0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
34d00 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 45  _index_info.** E
34d10 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
34d20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * The sqlite3_in
34d30 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
34d40 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 74  re and its subst
34d50 72 75 63 74 75 72 65 73 20 69 73 20 75 73 65 64  ructures is used
34d60 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f   to.** pass info
34d70 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64  rmation into and
34d80 20 72 65 63 65 69 76 65 20 74 68 65 20 72 65 70   receive the rep
34d90 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 42 65  ly from the [xBe
34da0 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68  stIndex].** meth
34db0 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c  od of a [virtual
34dc0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20   table module]. 
34dd0 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64 65   The fields unde
34de0 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65  r **Inputs** are
34df0 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74   the.** inputs t
34e00 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e 64  o xBestIndex and
34e10 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20   are read-only. 
34e20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73 65   xBestIndex inse
34e30 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c  rts its.** resul
34e40 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75  ts into the **Ou
34e50 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a  tputs** fields..
34e60 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  **.** The aConst
34e70 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65  raint[] array re
34e80 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75  cords WHERE clau
34e90 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  se constraints o
34ea0 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  f the form:.**.*
34eb0 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50  * <pre>column OP
34ec0 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a   expr</pre>.**.*
34ed0 2a 20 77 68 65 72 65 20 4f 50 20 69 73 20 3d 2c  * where OP is =,
34ee0 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67   &lt;, &lt;=, &g
34ef0 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54  t;, or &gt;=.  T
34f00 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70  he particular op
34f10 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f  erator is.** sto
34f20 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69  red in aConstrai
34f30 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e  nt[].op.  The in
34f40 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
34f50 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a  n is stored in.*
34f60 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  * aConstraint[].
34f70 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74  iColumn.  aConst
34f80 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69  raint[].usable i
34f90 73 20 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a  s TRUE if the.**
34fa0 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67   expr on the rig
34fb0 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e  ht-hand side can
34fc0 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 28 61   be evaluated (a
34fd0 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73  nd thus the cons
34fe0 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61  traint.** is usa
34ff0 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69  ble) and false i
35000 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a  f it cannot..**.
35010 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72  ** The optimizer
35020 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69   automatically i
35030 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20  nverts terms of 
35040 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f  the form "expr O
35050 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64  P column".** and
35060 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d   makes other sim
35070 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20  plifications to 
35080 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
35090 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74   in an attempt t
350a0 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79  o.** get as many
350b0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
350c0 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72  rms into the for
350d0 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73  m shown above as
350e0 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68   possible..** Th
350f0 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20  e aConstraint[] 
35100 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72  array only repor
35110 74 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ts WHERE clause 
35120 74 65 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72  terms in the cor
35130 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61  rect.** form tha
35140 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70  t refer to the p
35150 61 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61  articular virtua
35160 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75  l table being qu
35170 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66  eried..**.** Inf
35180 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
35190 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
351a0 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  se is stored in 
351b0 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45  aOrderBy[]..** E
351c0 61 63 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64  ach term of aOrd
351d0 65 72 42 79 20 72 65 63 6f 72 64 73 20 61 20 63  erBy records a c
351e0 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44  olumn of the ORD
351f0 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a  ER BY clause..**
35200 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 49 6e  .** The [xBestIn
35210 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 73 74  dex] method must
35220 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 69 6e   fill aConstrain
35230 74 55 73 61 67 65 5b 5d 20 77 69 74 68 20 69 6e  tUsage[] with in
35240 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
35250 75 74 20 77 68 61 74 20 70 61 72 61 6d 65 74 65  ut what paramete
35260 72 73 20 74 6f 20 70 61 73 73 20 74 6f 20 78 46  rs to pass to xF
35270 69 6c 74 65 72 2e 20 20 49 66 20 61 72 67 76 49  ilter.  If argvI
35280 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a 20 74  ndex>0 then.** t
35290 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
352a0 64 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  de of the corres
352b0 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 72 61  ponding aConstra
352c0 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 61 74  int[] is evaluat
352d0 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f 6d 65  ed.** and become
352e0 73 20 74 68 65 20 61 72 67 76 49 6e 64 65 78 2d  s the argvIndex-
352f0 74 68 20 65 6e 74 72 79 20 69 6e 20 61 72 67 76  th entry in argv
35300 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 61 69 6e  .  If aConstrain
35310 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a  tUsage[].omit.**
35320 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
35330 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  he constraint is
35340 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 66   assumed to be f
35350 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 79 20  ully handled by 
35360 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
35370 61 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20  able and is not 
35380 63 68 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79  checked again by
35390 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   SQLite..**.** T
353a0 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 69 64  he idxNum and id
353b0 78 50 74 72 20 76 61 6c 75 65 73 20 61 72 65 20  xPtr values are 
353c0 72 65 63 6f 72 64 65 64 20 61 6e 64 20 70 61 73  recorded and pas
353d0 73 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  sed into the.** 
353e0 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64  [xFilter] method
353f0 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72  ..** [sqlite3_fr
35400 65 65 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f  ee()] is used to
35410 20 66 72 65 65 20 69 64 78 50 74 72 20 69 66 20   free idxPtr if 
35420 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a 2a 20  and only iff.** 
35430 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50 74 72  needToFreeIdxPtr
35440 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
35450 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75  The orderByConsu
35460 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f  med means that o
35470 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c  utput from [xFil
35480 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c  ter]/[xNext] wil
35490 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68  l occur in.** th
354a0 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20  e correct order 
354b0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  to satisfy the O
354c0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
354d0 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61  o that no separa
354e0 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74  te.** sorting st
354f0 65 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ep is required..
35500 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
35510 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73  tedCost value is
35520 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20   an estimate of 
35530 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e  the cost of doin
35540 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75  g the.** particu
35550 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66  lar lookup.  A f
35560 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61  ull scan of a ta
35570 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69  ble with N entri
35580 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a  es should have.*
35590 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20  * a cost of N.  
355a0 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20  A binary search 
355b0 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20  of a table of N 
355c0 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68  entries should h
355d0 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66  ave a.** cost of
355e0 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c   approximately l
355f0 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74  og(N)..*/.struct
35600 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
35610 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74  nfo {.  /* Input
35620 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73  s */.  int nCons
35630 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20  traint;         
35640 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
35650 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74  ntries in aConst
35660 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63  raint */.  struc
35670 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
35680 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20  constraint {.   
35690 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
356a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
356b0 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61  olumn on left-ha
356c0 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74  nd side of const
356d0 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e  raint */.     un
356e0 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20  signed char op; 
356f0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
35700 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a  raint operator *
35710 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  /.     unsigned 
35720 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20  char usable;    
35730 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
35740 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75   constraint is u
35750 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e  sable */.     in
35760 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20  t iTermOffset;  
35770 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
35780 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65  internally - xBe
35790 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69  stIndex should i
357a0 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43  gnore */.  } *aC
357b0 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
357c0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f        /* Table o
357d0 66 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63  f WHERE clause c
357e0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
357f0 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20  int nOrderBy;   
35800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
35810 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
35820 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
35830 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
35840 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
35850 72 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e  rderby {.     in
35860 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
35870 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
35880 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  n number */.    
35890 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64   unsigned char d
358a0 65 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  esc;       /* Tr
358b0 75 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61  ue for DESC.  Fa
358c0 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a  lse for ASC. */.
358d0 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20    } *aOrderBy;  
358e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
358f0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
35900 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70  use */.  /* Outp
35910 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  uts */.  struct 
35920 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
35930 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b  nstraint_usage {
35940 0a 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64  .    int argvInd
35950 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ex;           /*
35960 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69   if >0, constrai
35970 6e 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72  nt is part of ar
35980 67 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f  gv to xFilter */
35990 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
359a0 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a  ar omit;      /*
359b0 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74   Do not code a t
359c0 65 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e  est for this con
359d0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a  straint */.  } *
359e0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
359f0 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20  ;.  int idxNum; 
35a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35a10 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f  * Number used to
35a20 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e   identify the in
35a30 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69  dex */.  char *i
35a40 64 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20  dxStr;          
35a50 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70      /* String, p
35a60 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64  ossibly obtained
35a70 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
35a80 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65  lloc */.  int ne
35a90 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20  edToFreeIdxStr; 
35aa0 20 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78       /* Free idx
35ab0 53 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65  Str using sqlite
35ac0 33 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65  3_free() if true
35ad0 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42   */.  int orderB
35ae0 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20  yConsumed;      
35af0 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70   /* True if outp
35b00 75 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72  ut is already or
35b10 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c  dered */.  doubl
35b20 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b  e estimatedCost;
35b30 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74        /* Estimat
35b40 65 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67  ed cost of using
35b50 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d   this index */.}
35b60 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ;.#define SQLITE
35b70 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
35b80 54 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e  T_EQ    2.#defin
35b90 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
35ba0 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20  ONSTRAINT_GT    
35bb0 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  4.#define SQLITE
35bc0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
35bd0 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e  T_LE    8.#defin
35be0 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
35bf0 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20  ONSTRAINT_LT    
35c00 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  16.#define SQLIT
35c10 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
35c20 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66  NT_GE    32.#def
35c30 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
35c40 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
35c50 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  H 64../*.** CAPI
35c60 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41  3REF: Register A
35c70 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49   Virtual Table I
35c80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48  mplementation {H
35c90 31 38 32 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18200} <S20400>.
35ca0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
35cb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
35cc0 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ne is used to re
35cd0 67 69 73 74 65 72 20 61 20 6e 65 77 20 5b 76 69  gister a new [vi
35ce0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
35cf0 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d 6f 64  le] name..** Mod
35d00 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62  ule names must b
35d10 65 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66  e registered bef
35d20 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20  ore.** creating 
35d30 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 74  a new [virtual t
35d40 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 65 20  able] using the 
35d50 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72  module, or befor
35d60 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 72 65  e using a.** pre
35d70 65 78 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61  existing [virtua
35d80 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65  l table] for the
35d90 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54   module..**.** T
35da0 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69  he module name i
35db0 73 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e 20  s registered on 
35dc0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
35dd0 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66  nnection] specif
35de0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 69  ied.** by the fi
35df0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20  rst parameter.  
35e00 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
35e10 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 65 6e 20  module is given 
35e20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e  by the .** secon
35e30 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  d parameter.  Th
35e40 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
35e50 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
35e60 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  o.** the impleme
35e70 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b  ntation of the [
35e80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35e90 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 6f 75  dule].   The fou
35ea0 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rth.** parameter
35eb0 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
35ec0 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69   client data poi
35ed0 6e 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73  nter that is pas
35ee0 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69  sed through.** i
35ef0 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65  nto the [xCreate
35f00 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d  ] and [xConnect]
35f10 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20   methods of the 
35f20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35f30 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e  dule.** when a n
35f40 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ew virtual table
35f50 20 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65   is be being cre
35f60 61 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61  ated or reinitia
35f70 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lized..**.** Thi
35f80 73 20 69 6e 74 65 72 66 61 63 65 20 68 61 73 20  s interface has 
35f90 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65  exactly the same
35fa0 20 65 66 66 65 63 74 20 61 73 20 63 61 6c 6c 69   effect as calli
35fb0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
35fc0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
35fd0 29 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63  )] with a NULL c
35fe0 6c 69 65 6e 74 20 64 61 74 61 20 64 65 73 74 72  lient data destr
35ff0 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  uctor..*/.SQLITE
36000 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
36010 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
36020 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
36030 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
36040 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
36050 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
36060 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
36070 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
36080 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36090 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
360a0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   Name of the mod
360b0 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
360c0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
360d0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66  ,   /* Methods f
360e0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  or the module */
360f0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74  .  void *pClient
36100 44 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a  Data          /*
36110 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
36120 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
36130 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  t */.);../*.** C
36140 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
36150 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c  r A Virtual Tabl
36160 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e Implementation
36170 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 34 30   {H18210} <S2040
36180 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
36190 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  AL.**.** This ro
361a0 75 74 69 6e 65 20 69 73 20 69 64 65 6e 74 69 63  utine is identic
361b0 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  al to the [sqlit
361c0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
361d0 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 65  ()] method,.** e
361e0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 68 61  xcept that it ha
361f0 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d  s an extra param
36200 65 74 65 72 20 74 6f 20 73 70 65 63 69 66 79 20  eter to specify 
36210 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72  .** a destructor
36220 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
36230 65 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f  e client data po
36240 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 20 77  inter.  SQLite w
36250 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  ill.** invoke th
36260 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
36270 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20  ction (if it is 
36280 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53  not NULL) when S
36290 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67  QLite.** no long
362a0 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 6c  er needs the pCl
362b0 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 72  ientData pointer
362c0 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  .  .*/.SQLITE_AP
362d0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
362e0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
362f0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
36300 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
36310 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
36320 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
36330 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
36340 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
36350 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36360 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
36370 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   Name of the mod
36380 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
36390 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
363a0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66  ,   /* Methods f
363b0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  or the module */
363c0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74  .  void *pClient
363d0 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 2f 2a  Data,         /*
363e0 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
363f0 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
36400 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65  t */.  void(*xDe
36410 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20  stroy)(void*)   
36420 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
36430 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
36440 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
36450 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
36460 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62  able Instance Ob
36470 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53  ject {H18010} <S
36480 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20400>.** KEYWOR
36490 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62  DS: sqlite3_vtab
364a0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
364b0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69  .**.** Every [vi
364c0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
364d0 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  le] implementati
364e0 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61  on uses a subcla
364f0 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ss.** of the fol
36500 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
36510 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70   to describe a p
36520 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e  articular instan
36530 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69  ce.** of the [vi
36540 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45  rtual table].  E
36550 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c  ach subclass wil
36560 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64  l.** be tailored
36570 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63   to the specific
36580 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f   needs of the mo
36590 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
365a0 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70  ion..** The purp
365b0 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65  ose of this supe
365c0 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66  rclass is to def
365d0 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c  ine certain fiel
365e0 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  ds that are.** c
365f0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64  ommon to all mod
36600 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
36610 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75  ons..**.** Virtu
36620 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64  al tables method
36630 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72  s can set an err
36640 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73  or message by as
36650 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72  signing a.** str
36660 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
36670 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  m [sqlite3_mprin
36680 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67  tf()] to zErrMsg
36690 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68  .  The method sh
366a0 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72  ould.** take car
366b0 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72  e that any prior
366c0 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64   string is freed
366d0 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73   by a call to [s
366e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a  qlite3_free()].*
366f0 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67  * prior to assig
36700 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e  ning a new strin
36710 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41  g to zErrMsg.  A
36720 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d  fter the error m
36730 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c  essage.** is del
36740 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65  ivered up to the
36750 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74   client applicat
36760 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20  ion, the string 
36770 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
36780 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62  cally.** freed b
36790 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  y sqlite3_free()
367a0 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67   and the zErrMsg
367b0 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a   field will be z
367c0 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  eroed..*/.struct
367d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a   sqlite3_vtab {.
367e0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
367f0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
36800 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20    /* The module 
36810 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c  for this virtual
36820 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
36830 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
36840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
36850 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 20 2a 2f  O LONGER USED */
36860 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
36870 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36880 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
36890 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  age from sqlite3
368a0 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20  _mprintf() */.  
368b0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
368c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
368d0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
368e0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
368f0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
36900 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
36910 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 73 6f  tual Table Curso
36920 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 30 32  r Object  {H1802
36930 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
36940 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
36950 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 76 69  _vtab_cursor {vi
36960 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
36970 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  or}.** EXPERIMEN
36980 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
36990 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
369a0 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74  odule] implement
369b0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
369c0 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
369d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
369e0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20  ure to describe 
369f0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69  cursors that poi
36a00 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b  nt into the.** [
36a10 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 61  virtual table] a
36a20 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74  nd are used.** t
36a30 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
36a40 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
36a50 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63  .  Cursors are c
36a60 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65  reated using the
36a70 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  .** [sqlite3_mod
36a80 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f 70 65  ule.xOpen | xOpe
36a90 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  n] method of the
36aa0 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 65 20   module and are 
36ab0 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 79 20  destroyed.** by 
36ac0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  the [sqlite3_mod
36ad0 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 43 6c  ule.xClose | xCl
36ae0 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 43 75  ose] method.  Cu
36af0 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 0a 2a  ssors are used.*
36b00 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c 74 65  * by the [xFilte
36b10 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b 78 45  r], [xNext], [xE
36b20 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d 2c 20  of], [xColumn], 
36b30 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d 65 74  and [xRowid] met
36b40 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 6d  hods.** of the m
36b50 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64  odule.  Each mod
36b60 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
36b70 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a  on will define.*
36b80 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
36b90 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74   a cursor struct
36ba0 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20  ure to suit its 
36bb0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  own needs..**.**
36bc0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73   This superclass
36bd0 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72   exists in order
36be0 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64   to define field
36bf0 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20  s of the cursor 
36c00 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d  that.** are comm
36c10 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d  on to all implem
36c20 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
36c30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
36c40 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  b_cursor {.  sql
36c50 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
36c60 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61  ;      /* Virtua
36c70 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20  l table of this 
36c80 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56  cursor */.  /* V
36c90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
36ca0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
36cb0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
36cc0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
36cd0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  s */.};../*.** C
36ce0 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65  API3REF: Declare
36cf0 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41   The Schema Of A
36d00 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
36d10 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e  H18280} <S20400>
36d20 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
36d30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 72 65  .**.** The [xCre
36d40 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65  ate] and [xConne
36d50 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 61  ct] methods of a
36d60 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62  .** [virtual tab
36d70 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c 6c 20  le module] call 
36d80 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a  this interface.*
36d90 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65  * to declare the
36da0 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e 61 6d   format (the nam
36db0 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 65 73  es and datatypes
36dc0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 29   of the columns)
36dd0 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75   of.** the virtu
36de0 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 20 69  al tables they i
36df0 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  mplement..*/.SQL
36e00 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
36e10 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
36e20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
36e30 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63  vtab(sqlite3*, c
36e40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29  onst char *zSQL)
36e50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
36e60 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75  F: Overload A Fu
36e70 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72  nction For A Vir
36e80 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33  tual Table {H183
36e90 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20  00} <S20400>.** 
36ea0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
36eb0 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  ** Virtual table
36ec0 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c  s can provide al
36ed0 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d  ternative implem
36ee0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e  entations of fun
36ef0 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20  ctions.** using 
36f00 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69  the [xFindFuncti
36f10 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  on] method of th
36f20 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
36f30 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42   module].  .** B
36f40 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f  ut global versio
36f50 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63  ns of those func
36f60 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78  tions.** must ex
36f70 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ist in order to 
36f80 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a 2a  be overloaded..*
36f90 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61  *.** This API ma
36fa0 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f 62 61  kes sure a globa
36fb0 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 20 66  l version of a f
36fc0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 70  unction with a p
36fd0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e 61 6d  articular.** nam
36fe0 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20  e and number of 
36ff0 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 73 74  parameters exist
37000 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 66  s.  If no such f
37010 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 0a 2a  unction exists.*
37020 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 41 50  * before this AP
37030 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 20 6e  I is called, a n
37040 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ew function is c
37050 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 6d 70  reated.  The imp
37060 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  lementation.** o
37070 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69  f the new functi
37080 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 65 73  on always causes
37090 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f   an exception to
370a0 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0a   be thrown.  So.
370b0 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74  ** the new funct
370c0 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20  ion is not good 
370d0 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20  for anything by 
370e0 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c  itself.  Its onl
370f0 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20  y.** purpose is 
37100 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c  to be a placehol
37110 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  der function tha
37120 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61  t can be overloa
37130 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 69 72  ded.** by a [vir
37140 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a  tual table]..*/.
37150 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
37160 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
37170 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  nt sqlite3_overl
37180 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c  oad_function(sql
37190 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
371a0 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e  r *zFuncName, in
371b0 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t nArg);../*.** 
371c0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
371d0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
371e0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66  le mechanism def
371f0 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b  ined above (back
37200 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d   up.** to a comm
37210 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73  ent remarkably s
37220 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
37230 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ne) is currently
37240 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74   considered.** t
37250 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61  o be experimenta
37260 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  l.  The interfac
37270 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
37280 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77  n incompatible w
37290 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ays..** If this 
372a0 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72  is a problem for
372b0 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65   you, do not use
372c0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61   the interface a
372d0 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a  t this time..**.
372e0 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74  ** When the virt
372f0 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e  ual-table mechan
37300 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20  ism stabilizes, 
37310 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20  we will declare 
37320 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  the.** interface
37330 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20   fixed, support 
37340 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c  it indefinitely,
37350 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73   and remove this
37360 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a   comment..**.***
37370 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  *** EXPERIMENTAL
37380 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68   - subject to ch
37390 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
373a0 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ice ************
373b0 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  **.*/../*.** CAP
373c0 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20  I3REF: A Handle 
373d0 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20  To An Open BLOB 
373e0 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30  {H17800} <S30230
373f0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
37400 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c  BLOB handle} {BL
37410 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a  OB handles}.**.*
37420 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
37430 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
37440 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
37450 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  BLOB on which.**
37460 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
37470 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
37480 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20  l BLOB I/O] can 
37490 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  be performed..**
374a0 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73   Objects of this
374b0 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65   type are create
374c0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c  d by [sqlite3_bl
374d0 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e  ob_open()].** an
374e0 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  d destroyed by [
374f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
37500 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73  se()]..** The [s
37510 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
37520 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
37530 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69  _blob_write()] i
37540 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e  nterfaces.** can
37550 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64   be used to read
37560 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20   or write small 
37570 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74  subsections of t
37580 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20  he BLOB..** The 
37590 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
375a0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
375b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   returns the siz
375c0 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e  e of the BLOB in
375d0 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64   bytes..*/.typed
375e0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
375f0 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62  3_blob sqlite3_b
37600 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lob;../*.** CAPI
37610 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f  3REF: Open A BLO
37620 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61  B For Incrementa
37630 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c  l I/O {H17810} <
37640 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30230>.**.** Th
37650 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70  is interfaces op
37660 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64  ens a [BLOB hand
37670 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20  le | handle] to 
37680 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64  the BLOB located
37690 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c  .** in row iRow,
376a0 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c   column zColumn,
376b0 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e   table zTable in
376c0 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a   database zDb;.*
376d0 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
376e0 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20  , the same BLOB 
376f0 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65  that would be se
37700 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a  lected by:.**.**
37710 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45   <pre>.**     SE
37720 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f  LECT zColumn FRO
37730 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45  M zDb.zTable WHE
37740 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f  RE [rowid] = iRo
37750 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e  w;.** </pre> {EN
37760 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  D}.**.** If the 
37770 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
37780 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
37790 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70  n the BLOB is op
377a0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a  ened for read.**
377b0 20 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73   and write acces
377c0 73 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f  s. If it is zero
377d0 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70  , the BLOB is op
377e0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63  ened for read ac
377f0 63 65 73 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e  cess..** It is n
37800 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f  ot possible to o
37810 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61  pen a column tha
37820 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20  t is part of an 
37830 69 6e 64 65 78 20 6f 72 20 70 72 69 6d 61 72 79  index or primary
37840 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 20 77 72 69   .** key for wri
37850 74 69 6e 67 2e 20 5e 49 66 20 5b 66 6f 72 65 69  ting. ^If [forei
37860 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
37870 74 73 5d 20 61 72 65 20 65 6e 61 62 6c 65 64 2c  ts] are enabled,
37880 20 69 74 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 70   it is .** not p
37890 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20  ossible to open 
378a0 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  a column that is
378b0 20 70 61 72 74 20 6f 66 20 61 20 5b 63 68 69 6c   part of a [chil
378c0 64 20 6b 65 79 5d 20 66 6f 72 20 77 72 69 74 69  d key] for writi
378d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ng..**.** Note t
378e0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
378f0 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65   name is not the
37900 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 63   filename that c
37910 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 64  ontains.** the d
37920 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74 68  atabase but rath
37930 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20  er the symbolic 
37940 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
37950 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 20  base that.** is 
37960 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 68  assigned when th
37970 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f  e database is co
37980 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b 41  nnected using [A
37990 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74  TTACH]..** For t
379a0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
379b0 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 62   file, the datab
379c0 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 69  ase name is "mai
379d0 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 20  n"..** For TEMP 
379e0 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 61  tables, the data
379f0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 65  base name is "te
37a00 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  mp"..**.** On su
37a10 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f  ccess, [SQLITE_O
37a20 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  K] is returned a
37a30 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42  nd the new [BLOB
37a40 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74   handle] is writ
37a50 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f  ten.** to *ppBlo
37a60 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20  b. Otherwise an 
37a70 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
37a80 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70  returned and *pp
37a90 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a 20 74  Blob is set.** t
37aa0 6f 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e  o be a null poin
37ab0 74 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ter..** This fun
37ac0 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 5b  ction sets the [
37ad0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
37ae0 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 65 20  ion] error code 
37af0 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61  and message.** a
37b00 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73  ccessible via [s
37b10 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
37b20 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  ] and [sqlite3_e
37b30 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 72 65 6c  rrmsg()] and rel
37b40 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ated.** function
37b50 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  s.  Note that th
37b60 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 69 61 62  e *ppBlob variab
37b70 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69  le is always ini
37b80 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 0a 2a 2a  tialized in a.**
37b90 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20   way that makes 
37ba0 69 74 20 73 61 66 65 20 74 6f 20 69 6e 76 6f 6b  it safe to invok
37bb0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
37bc0 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a 70 70 42  close()] on *ppB
37bd0 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  lob.** regardles
37be0 73 20 6f 66 20 74 68 65 20 73 75 63 63 65 73 73  s of the success
37bf0 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66 20 74   or failure of t
37c00 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
37c10 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 20 74 68  ** If the row th
37c20 61 74 20 61 20 42 4c 4f 42 20 68 61 6e 64 6c 65  at a BLOB handle
37c30 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 6d 6f   points to is mo
37c40 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20  dified by an.** 
37c50 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 4c 45 54  [UPDATE], [DELET
37c60 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e 20 43 4f  E], or by [ON CO
37c70 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d 65 66 66  NFLICT] side-eff
37c80 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ects.** then the
37c90 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73 20   BLOB handle is 
37ca0 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 69 72  marked as "expir
37cb0 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ed"..** This is 
37cc0 74 72 75 65 20 69 66 20 61 6e 79 20 63 6f 6c 75  true if any colu
37cd0 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 20 69 73  mn of the row is
37ce0 20 63 68 61 6e 67 65 64 2c 20 65 76 65 6e 20 61   changed, even a
37cf0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 68 65 72   column.** other
37d00 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 74 68   than the one th
37d10 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73  e BLOB handle is
37d20 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 43 61 6c   open on..** Cal
37d30 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ls to [sqlite3_b
37d40 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64 20  lob_read()] and 
37d50 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72  [sqlite3_blob_wr
37d60 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 61 20  ite()] for.** a 
37d70 65 78 70 69 72 65 64 20 42 4c 4f 42 20 68 61 6e  expired BLOB han
37d80 64 6c 65 20 66 61 69 6c 20 77 69 74 68 20 61 6e  dle fail with an
37d90 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20   return code of 
37da0 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
37db0 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 69 74 74  ** Changes writt
37dc0 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20 70  en into a BLOB p
37dd0 72 69 6f 72 20 74 6f 20 74 68 65 20 42 4c 4f 42  rior to the BLOB
37de0 20 65 78 70 69 72 69 6e 67 20 61 72 65 20 6e 6f   expiring are no
37df0 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62 79  t.** rollback by
37e00 20 74 68 65 20 65 78 70 69 72 61 74 69 6f 6e 20   the expiration 
37e10 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 20 53 75  of the BLOB.  Su
37e20 63 68 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20  ch changes will 
37e30 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f  eventually.** co
37e40 6d 6d 69 74 20 69 66 20 74 68 65 20 74 72 61 6e  mmit if the tran
37e50 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 6e 75 65  saction continue
37e60 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e  s to completion.
37e70 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 5b  .**.** Use the [
37e80 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
37e90 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  es()] interface 
37ea0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
37eb0 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20   size of.** the 
37ec0 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20 54 68  opened blob.  Th
37ed0 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62  e size of a blob
37ee0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
37ef0 67 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 69  ged by this.** i
37f00 6e 74 65 72 66 61 63 65 2e 20 20 55 73 65 20 74  nterface.  Use t
37f10 68 65 20 5b 55 50 44 41 54 45 5d 20 53 51 4c 20  he [UPDATE] SQL 
37f20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 61 6e 67  command to chang
37f30 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a  e the size of a.
37f40 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54  ** blob..**.** T
37f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  he [sqlite3_bind
37f60 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 61 6e 64  _zeroblob()] and
37f70 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
37f80 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 69 6e 74  _zeroblob()] int
37f90 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e 64 20 74  erfaces.** and t
37fa0 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b 7a 65 72  he built-in [zer
37fb0 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 6e 63 74  oblob] SQL funct
37fc0 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c  ion can be used,
37fd0 20 69 66 20 64 65 73 69 72 65 64 2c 0a 2a 2a 20   if desired,.** 
37fe0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 6d 70  to create an emp
37ff0 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  ty, zero-filled 
38000 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 20 74 6f  blob in which to
38010 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 75   read or write u
38020 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 6e 74  sing.** this int
38030 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f  erface..**.** To
38040 20 61 76 6f 69 64 20 61 20 72 65 73 6f 75 72 63   avoid a resourc
38050 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 20 6f 70  e leak, every op
38060 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
38070 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c   should eventual
38080 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73 65  ly.** be release
38090 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b  d by a call to [
380a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
380b0 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  se()]..**.** Req
380c0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
380d0 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 5d 20  17813] [H17814] 
380e0 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 31 39  [H17816] [H17819
380f0 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 37 38  ] [H17821] [H178
38100 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  24].*/.SQLITE_AP
38110 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
38120 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74  ob_open(.  sqlit
38130 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  e3*,.  const cha
38140 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20  r *zDb,.  const 
38150 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20  char *zTable,.  
38160 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
38170 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69  umn,.  sqlite3_i
38180 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74  nt64 iRow,.  int
38190 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65   flags,.  sqlite
381a0 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a  3_blob **ppBlob.
381b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
381c0 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42  EF: Close A BLOB
381d0 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d   Handle {H17830}
381e0 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
381f0 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b  Closes an open [
38200 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a  BLOB handle]..**
38210 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c  .** Closing a BL
38220 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74  OB shall cause t
38230 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
38240 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74  action to commit
38250 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65  .** if there are
38260 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c   no other BLOBs,
38270 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70   no pending prep
38280 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c  ared statements,
38290 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61   and the.** data
382a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
382b0 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69  is in [autocommi
382c0 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61  t mode]..** If a
382d0 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d  ny writes were m
382e0 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c  ade to the BLOB,
382f0 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68   they might be h
38300 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20  eld in cache.** 
38310 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20  until the close 
38320 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65  operation if the
38330 79 20 77 69 6c 6c 20 66 69 74 2e 0a 2a 2a 0a 2a  y will fit..**.*
38340 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65 20 42 4c  * Closing the BL
38350 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63 65 73 20  OB often forces 
38360 74 68 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 6f  the changes.** o
38370 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 73  ut to disk and s
38380 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20 65 72 72  o if any I/O err
38390 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 79 20  ors occur, they 
383a0 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f 63 63 75  will likely occu
383b0 72 0a 2a 2a 20 61 74 20 74 68 65 20 74 69 6d 65  r.** at the time
383c0 20 77 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69   when the BLOB i
383d0 73 20 63 6c 6f 73 65 64 2e 20 20 41 6e 79 20 65  s closed.  Any e
383e0 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72  rrors that occur
383f0 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69   during.** closi
38400 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20  ng are reported 
38410 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65  as a non-zero re
38420 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  turn value..**.*
38430 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c  * The BLOB is cl
38440 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e  osed uncondition
38450 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74  ally.  Even if t
38460 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
38470 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20  rns.** an error 
38480 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69  code, the BLOB i
38490 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a  s still closed..
384a0 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68  **.** Calling th
384b0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
384c0 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 28  a null pointer (
384d0 77 68 69 63 68 20 61 73 20 77 6f 75 6c 64 20 62  which as would b
384e0 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79  e returned.** by
384f0 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 74 6f 20   failed call to 
38500 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
38510 65 6e 28 29 5d 29 20 69 73 20 61 20 68 61 72 6d  en()]) is a harm
38520 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  less no-op..**.*
38530 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
38540 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b 48 31 37  ** [H17833] [H17
38550 38 33 36 5d 20 5b 48 31 37 38 33 39 5d 0a 2a 2f  836] [H17839].*/
38560 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
38570 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
38580 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  se(sqlite3_blob 
38590 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
385a0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20  REF: Return The 
385b0 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20  Size Of An Open 
385c0 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c 53  BLOB {H17840} <S
385d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74  30230>.**.** Ret
385e0 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e  urns the size in
385f0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c   bytes of the BL
38600 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  OB accessible vi
38610 61 20 74 68 65 20 0a 2a 2a 20 73 75 63 63 65 73  a the .** succes
38620 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 5b 42  sfully opened [B
38630 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 69  LOB handle] in i
38640 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74  ts only argument
38650 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 63 72 65 6d  .  The.** increm
38660 65 6e 74 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72  ental blob I/O r
38670 6f 75 74 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79  outines can only
38680 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69   read or overwri
38690 74 69 6e 67 20 65 78 69 73 74 69 6e 67 0a 2a 2a  ting existing.**
386a0 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74   blob content; t
386b0 68 65 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67  hey cannot chang
386c0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20  e the size of a 
386d0 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  blob..**.** This
386e0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f   routine only wo
386f0 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68  rks on a [BLOB h
38700 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73  andle] which has
38710 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a   been created.**
38720 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63   by a prior succ
38730 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
38740 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
38750 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68  n()] and which h
38760 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63  as not.** been c
38770 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65  losed by [sqlite
38780 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e  3_blob_close()].
38790 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74    Passing any ot
387a0 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a  her pointer in.*
387b0 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  * to this routin
387c0 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  e results in und
387d0 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61  efined and proba
387e0 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20  bly undesirable 
387f0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
38800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
38810 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c   [H17843].*/.SQL
38820 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
38830 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73  te3_blob_bytes(s
38840 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a  qlite3_blob *);.
38850 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
38860 20 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20   Read Data From 
38870 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74  A BLOB Increment
38880 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53  ally {H17850} <S
38890 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  30230>.**.** Thi
388a0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
388b0 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20  ed to read data 
388c0 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c  from an open [BL
388d0 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20  OB handle] into 
388e0 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70  a.** caller-supp
388f0 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62  lied buffer. N b
38900 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
38910 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66   copied into buf
38920 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68  fer Z.** from th
38930 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61  e open BLOB, sta
38940 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  rting at offset 
38950 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  iOffset..**.** I
38960 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74  f offset iOffset
38970 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20   is less than N 
38980 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65  bytes from the e
38990 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a  nd of the BLOB,.
389a0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ** [SQLITE_ERROR
389b0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
389c0 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61  d no data is rea
389d0 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66  d.  If N or iOff
389e0 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74  set is.** less t
389f0 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54  han zero, [SQLIT
38a00 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
38a10 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
38a20 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20 54 68 65   is read..** The
38a30 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f   size of the blo
38a40 62 20 28 61 6e 64 20 68 65 6e 63 65 20 74 68 65  b (and hence the
38a50 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f   maximum value o
38a60 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a 2a 2a 20  f N+iOffset).** 
38a70 63 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65  can be determine
38a80 64 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c  d using the [sql
38a90 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28  ite3_blob_bytes(
38aa0 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
38ab0 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74  .** An attempt t
38ac0 6f 20 72 65 61 64 20 66 72 6f 6d 20 61 6e 20 65  o read from an e
38ad0 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e  xpired [BLOB han
38ae0 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20  dle] fails with 
38af0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  an.** error code
38b00 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
38b10 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  T]..**.** On suc
38b20 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  cess, SQLITE_OK 
38b30 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
38b40 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65  Otherwise, an [e
38b50 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e  rror code] or an
38b60 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72   [extended error
38b70 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
38b80 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
38b90 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
38ba0 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 6e  s on a [BLOB han
38bb0 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 20 62  dle] which has b
38bc0 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 62  een created.** b
38bd0 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 65 73  y a prior succes
38be0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
38bf0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
38c00 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 61 73  )] and which has
38c10 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f   not.** been clo
38c20 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
38c30 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 20  blob_close()].  
38c40 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 65  Passing any othe
38c50 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a 20  r pointer in.** 
38c60 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
38c70 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
38c80 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c  ined and probabl
38c90 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62 65  y undesirable be
38ca0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 65  havior..**.** Se
38cb0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
38cc0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 2e 0a  _blob_write()]..
38cd0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
38ce0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 5d 20  ts:.** [H17853] 
38cf0 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 38 35 39  [H17856] [H17859
38d00 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 31 37 38  ] [H17862] [H178
38d10 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 5b 48 31  63] [H17865] [H1
38d20 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  7868].*/.SQLITE_
38d30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
38d40 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65  blob_read(sqlite
38d50 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a  3_blob *, void *
38d60 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f  Z, int N, int iO
38d70 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ffset);../*.** C
38d80 41 50 49 33 52 45 46 3a 20 57 72 69 74 65 20 44  API3REF: Write D
38d90 61 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20  ata Into A BLOB 
38da0 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48  Incrementally {H
38db0 31 37 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17870} <S30230>.
38dc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
38dd0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77  ion is used to w
38de0 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61  rite data into a
38df0 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  n open [BLOB han
38e00 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63  dle] from a.** c
38e10 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62  aller-supplied b
38e20 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f  uffer. N bytes o
38e30 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
38e40 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  d from the buffe
38e50 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  r Z.** into the 
38e60 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74  open BLOB, start
38e70 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f  ing at offset iO
38e80 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ffset..**.** If 
38e90 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  the [BLOB handle
38ea0 5d 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  ] passed as the 
38eb0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77  first argument w
38ec0 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f  as not opened fo
38ed0 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68  r.** writing (th
38ee0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
38ef0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  r to [sqlite3_bl
38f00 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a  ob_open()] was z
38f10 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75  ero),.** this fu
38f20 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b  nction returns [
38f30 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
38f40 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
38f50 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d  ction may only m
38f60 6f 64 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e  odify the conten
38f70 74 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20  ts of the BLOB; 
38f80 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73  it is.** not pos
38f90 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73  sible to increas
38fa0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20  e the size of a 
38fb0 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20  BLOB using this 
38fc0 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65  API..** If offse
38fd0 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73  t iOffset is les
38fe0 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66  s than N bytes f
38ff0 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74  rom the end of t
39000 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c  he BLOB,.** [SQL
39010 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65  ITE_ERROR] is re
39020 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61  turned and no da
39030 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20  ta is written.  
39040 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20  If N is.** less 
39050 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54  than zero [SQLIT
39060 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
39070 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
39080 20 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20   is written..** 
39090 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
390a0 42 4c 4f 42 20 28 61 6e 64 20 68 65 6e 63 65 20  BLOB (and hence 
390b0 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
390c0 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a  e of N+iOffset).
390d0 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d  ** can be determ
390e0 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b  ined using the [
390f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
39100 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  es()] interface.
39110 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  .**.** An attemp
39120 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e  t to write to an
39130 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68   expired [BLOB h
39140 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74  andle] fails wit
39150 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  h an.** error co
39160 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42  de of [SQLITE_AB
39170 4f 52 54 5d 2e 20 20 57 72 69 74 65 73 20 74 6f  ORT].  Writes to
39180 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 20 6f   the BLOB that o
39190 63 63 75 72 72 65 64 0a 2a 2a 20 62 65 66 6f 72  ccurred.** befor
391a0 65 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64  e the [BLOB hand
391b0 6c 65 5d 20 65 78 70 69 72 65 64 20 61 72 65 20  le] expired are 
391c0 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  not rolled back 
391d0 62 79 20 74 68 65 0a 2a 2a 20 65 78 70 69 72 61  by the.** expira
391e0 74 69 6f 6e 20 6f 66 20 74 68 65 20 68 61 6e 64  tion of the hand
391f0 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f  le, though of co
39200 75 72 73 65 20 74 68 6f 73 65 20 63 68 61 6e 67  urse those chang
39210 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65  es might.** have
39220 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65   been overwritte
39230 6e 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  n by the stateme
39240 6e 74 20 74 68 61 74 20 65 78 70 69 72 65 64 20  nt that expired 
39250 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 0a  the BLOB handle.
39260 2a 2a 20 6f 72 20 62 79 20 6f 74 68 65 72 20 69  ** or by other i
39270 6e 64 65 70 65 6e 64 65 6e 74 20 73 74 61 74 65  ndependent state
39280 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  ments..**.** On 
39290 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f  success, SQLITE_
392a0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
392b0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  ** Otherwise, an
392c0 20 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f    [error code] o
392d0 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65  r an [extended e
392e0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
392f0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
39300 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
39310 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42  works on a [BLOB
39320 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68   handle] which h
39330 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a  as been created.
39340 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75  ** by a prior su
39350 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
39360 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
39370 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68  pen()] and which
39380 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e   has not.** been
39390 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69   closed by [sqli
393a0 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
393b0 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20  ].  Passing any 
393c0 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e  other pointer in
393d0 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74  .** to this rout
393e0 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ine results in u
393f0 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f  ndefined and pro
39400 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c  bably undesirabl
39410 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  e behavior..**.*
39420 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
39430 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
39440 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
39450 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37  ments:.** [H1787
39460 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b 48 31 37  3] [H17874] [H17
39470 38 37 35 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48  875] [H17876] [H
39480 31 37 38 37 37 5d 20 5b 48 31 37 38 37 39 5d 20  17877] [H17879] 
39490 5b 48 31 37 38 38 32 5d 20 5b 48 31 37 38 38 35  [H17882] [H17885
394a0 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f  ].** [H17888].*/
394b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
394c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
394d0 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  te(sqlite3_blob 
394e0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  *, const void *z
394f0 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66  , int n, int iOf
39500 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  fset);../*.** CA
39510 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20  PI3REF: Virtual 
39520 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65  File System Obje
39530 63 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32  cts {H11200} <S2
39540 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69  0100>.**.** A vi
39550 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d  rtual filesystem
39560 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71   (VFS) is an [sq
39570 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
39580 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65  t.** that SQLite
39590 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63   uses to interac
395a0 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e  t.** with the un
395b0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
395c0 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74  ng system.  Most
395d0 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63   SQLite builds c
395e0 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69  ome with a.** si
395f0 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53  ngle default VFS
39600 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
39610 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73  iate for the hos
39620 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e  t computer..** N
39630 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20  ew VFSes can be 
39640 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65  registered and e
39650 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61  xisting VFSes ca
39660 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65  n be unregistere
39670 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  d..** The follow
39680 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61  ing interfaces a
39690 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a  re provided..**.
396a0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76  ** The sqlite3_v
396b0 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66  fs_find() interf
396c0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
396d0 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67  inter to a VFS g
396e0 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a  iven its name..*
396f0 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65  * Names are case
39700 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e   sensitive..** N
39710 61 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65  ames are zero-te
39720 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
39730 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68  trings..** If th
39740 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c  ere is no match,
39750 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
39760 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
39770 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e  If zVfsName is N
39780 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66  ULL then the def
39790 61 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75  ault VFS is retu
397a0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  rned..**.** New 
397b0 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74  VFSes are regist
397c0 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65  ered with sqlite
397d0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29  3_vfs_register()
397e0 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46  ..** Each new VF
397f0 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65  S becomes the de
39800 66 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65  fault VFS if the
39810 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69   makeDflt flag i
39820 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61  s set..** The sa
39830 6d 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65  me VFS can be re
39840 67 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c  gistered multipl
39850 65 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20  e times without 
39860 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61  injury..** To ma
39870 6b 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56  ke an existing V
39880 46 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61  FS into the defa
39890 75 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65  ult VFS, registe
398a0 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69  r it again.** wi
398b0 74 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20  th the makeDflt 
398c0 66 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77  flag set.  If tw
398d0 6f 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65  o different VFSe
398e0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61  s with the.** sa
398f0 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69  me name are regi
39900 73 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61  stered, the beha
39910 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
39920 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20  d.  If a.** VFS 
39930 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69  is registered wi
39940 74 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69  th a name that i
39950 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70  s NULL or an emp
39960 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68  ty string,.** th
39970 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
39980 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  is undefined..**
39990 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61  .** Unregister a
399a0 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71   VFS with the sq
399b0 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
399c0 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  ster() interface
399d0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61  ..** If the defa
399e0 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67  ult VFS is unreg
399f0 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72  istered, another
39a00 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61   VFS is chosen a
39a10 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
39a20 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f  .  The choice fo
39a30 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73  r the new VFS is
39a40 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a   arbitrary..**.*
39a50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
39a60 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b 48 31 31  ** [H11203] [H11
39a70 32 30 36 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48  206] [H11209] [H
39a80 31 31 32 31 32 5d 20 5b 48 31 31 32 31 35 5d 20  11212] [H11215] 
39a90 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49  [H11218].*/.SQLI
39aa0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76  TE_API sqlite3_v
39ab0 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f  fs *sqlite3_vfs_
39ac0 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20  find(const char 
39ad0 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49  *zVfsName);.SQLI
39ae0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
39af0 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
39b00 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
39b10 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c  t makeDflt);.SQL
39b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
39b30 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
39b40 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29  er(sqlite3_vfs*)
39b50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
39b60 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 37 30  F: Mutexes {H170
39b70 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a  00} <S20000>.**.
39b80 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  ** The SQLite co
39b90 72 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f  re uses these ro
39ba0 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61  utines for threa
39bb0 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61  d.** synchroniza
39bc0 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65  tion. Though the
39bd0 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66  y are intended f
39be0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
39bf0 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f  se by SQLite, co
39c00 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  de that links ag
39c10 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a  ainst SQLite is.
39c20 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20  ** permitted to 
39c30 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  use any of these
39c40 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
39c50 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72   The SQLite sour
39c60 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73  ce code contains
39c70 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
39c80 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  entations.** of 
39c90 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74  these mutex rout
39ca0 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70  ines.  An approp
39cb0 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61  riate implementa
39cc0 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63  tion.** is selec
39cd0 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
39ce0 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  y at compile-tim
39cf0 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  e.  The followin
39d00 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  g.** implementat
39d10 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62  ions are availab
39d20 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65  le in the SQLite
39d30 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c   core:.**.** <ul
39d40 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49  >.** <li>   SQLI
39d50 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20  TE_MUTEX_OS2.** 
39d60 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55  <li>   SQLITE_MU
39d70 54 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c  TEX_PTHREAD.** <
39d80 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
39d90 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20  EX_W32.** <li>  
39da0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
39db0 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  OP.** </ul>.**.*
39dc0 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  * The SQLITE_MUT
39dd0 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e  EX_NOOP implemen
39de0 74 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20  tation is a set 
39df0 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74  of routines.** t
39e00 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c  hat does no real
39e10 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20   locking and is 
39e20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
39e30 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67  use in.** a sing
39e40 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c  le-threaded appl
39e50 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51  ication.  The SQ
39e60 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a  LITE_MUTEX_OS2,.
39e70 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
39e80 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c  PTHREAD, and SQL
39e90 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d  ITE_MUTEX_W32 im
39ea0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
39eb0 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65   are appropriate
39ec0 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32   for use on OS/2
39ed0 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64  , Unix, and Wind
39ee0 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51  ows..**.** If SQ
39ef0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
39f00 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
39f10 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72  _MUTEX_APPDEF pr
39f20 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61  eprocessor.** ma
39f30 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74  cro defined (wit
39f40 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45  h "-DSQLITE_MUTE
39f50 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68  X_APPDEF=1"), th
39f60 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69  en no mutex.** i
39f70 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
39f80 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74   included with t
39f90 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20 74  he library. In t
39fa0 68 69 73 20 63 61 73 65 20 74 68 65 0a 2a 2a 20  his case the.** 
39fb0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
39fc0 20 73 75 70 70 6c 79 20 61 20 63 75 73 74 6f 6d   supply a custom
39fd0 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
39fe0 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a  ation using the.
39ff0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3a000 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20  G_MUTEX] option 
3a010 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  of the sqlite3_c
3a020 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 69 6f 6e  onfig() function
3a030 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  .** before calli
3a040 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ng sqlite3_initi
3a050 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f  alize() or any o
3a060 74 68 65 72 20 70 75 62 6c 69 63 20 73 71 6c 69  ther public sqli
3a070 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  te3_.** function
3a080 20 74 68 61 74 20 63 61 6c 6c 73 20 73 71 6c 69   that calls sqli
3a090 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
3a0a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d  ..**.** {H17011}
3a0b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3a0c0 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69  ex_alloc() routi
3a0d0 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e  ne allocates a n
3a0e0 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20  ew.** mutex and 
3a0f0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
3a100 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 31 32  r to it. {H17012
3a110 7d 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20  } If it returns 
3a120 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
3a130 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
3a140 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
3a150 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 31 33 7d  ocated. {H17013}
3a160 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20   SQLite.** will 
3a170 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b  unwind its stack
3a180 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
3a190 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d 20 54  rror. {H17014} T
3a1a0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  he argument.** t
3a1b0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
3a1c0 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f  alloc() is one o
3a1d0 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
3a1e0 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a  constants:.**.**
3a1f0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53   <ul>.** <li>  S
3a200 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
3a210 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
3a220 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
3a230 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
3a240 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
3a250 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  STER.** <li>  SQ
3a260 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3a270 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53  C_MEM.** <li>  S
3a280 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
3a290 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20  IC_MEM2.** <li> 
3a2a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
3a2b0 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69  ATIC_PRNG.** <li
3a2c0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3a2d0 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c  STATIC_LRU.** <l
3a2e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3a2f0 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20  _STATIC_LRU2.** 
3a300 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  </ul>.**.** {H17
3a310 30 31 35 7d 20 54 68 65 20 66 69 72 73 74 20 74  015} The first t
3a320 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75  wo constants cau
3a330 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  se sqlite3_mutex
3a340 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61  _alloc() to crea
3a350 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65  te.** a new mute
3a360 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65  x.  The new mute
3a370 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77  x is recursive w
3a380 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
3a390 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73  _RECURSIVE.** is
3a3a0 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65   used but not ne
3a3b0 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65  cessarily so whe
3a3c0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  n SQLITE_MUTEX_F
3a3d0 41 53 54 20 69 73 20 75 73 65 64 2e 20 7b 45 4e  AST is used. {EN
3a3e0 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  D}.** The mutex 
3a3f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
3a400 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
3a410 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69  make a distincti
3a420 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51  on.** between SQ
3a430 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3a440 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f  SIVE and SQLITE_
3a450 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74  MUTEX_FAST if it
3a460 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e   does.** not wan
3a470 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20  t to.  {H17016} 
3a480 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  But SQLite will 
3a490 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72  only request a r
3a4a0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69  ecursive mutex i
3a4b0 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65  n.** cases where
3a4c0 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73   it really needs
3a4d0 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20   one.  {END} If 
3a4e0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
3a4f0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
3a500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
3a510 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
3a520 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
3a530 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
3a540 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
3a550 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
3a560 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
3a570 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
3a580 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  AST..**.** {H170
3a590 31 37 7d 20 54 68 65 20 6f 74 68 65 72 20 61 6c  17} The other al
3a5a0 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
3a5b0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
3a5c0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
3a5d0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
3a5e0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
3a5f0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
3a600 2e 20 7b 45 4e 44 7d 20 20 53 69 78 20 73 74 61  . {END}  Six sta
3a610 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
3a620 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
3a630 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
3a640 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
3a650 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
3a660 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
3a670 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
3a680 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
3a690 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
3a6a0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
3a6b0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
3a6c0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
3a6d0 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
3a6e0 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
3a6f0 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
3a700 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
3a710 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
3a720 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
3a730 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
3a740 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
3a750 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20  * {H17018} Note 
3a760 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74  that if one of t
3a770 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
3a780 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c   parameters (SQL
3a790 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
3a7a0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45  * or SQLITE_MUTE
3a7b0 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20  X_RECURSIVE) is 
3a7c0 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65  used then sqlite
3a7d0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a  3_mutex_alloc().
3a7e0 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66  ** returns a dif
3a7f0 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20  ferent mutex on 
3a800 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31  every call.  {H1
3a810 37 30 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68  7034} But for th
3a820 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  e static.** mute
3a830 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
3a840 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
3a850 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
3a860 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
3a870 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
3a880 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31  er..**.** {H1701
3a890 39 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  9} The sqlite3_m
3a8a0 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74  utex_free() rout
3a8b0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
3a8c0 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  a previously.** 
3a8d0 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69  allocated dynami
3a8e0 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30  c mutex. {H17020
3a8f0 7d 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  } SQLite is care
3a900 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
3a910 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d  e every.** dynam
3a920 69 63 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ic mutex that it
3a930 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37   allocates. {A17
3a940 30 32 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63  021} The dynamic
3a950 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f   mutexes must no
3a960 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77  t be in.** use w
3a970 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61  hen they are dea
3a980 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32  llocated. {A1702
3a990 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f  2} Attempting to
3a9a0 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74   deallocate a st
3a9b0 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65  atic.** mutex re
3a9c0 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e  sults in undefin
3a9d0 65 64 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31  ed behavior. {H1
3a9e0 37 30 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76  7023} SQLite nev
3a9f0 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a  er deallocates.*
3aa00 2a 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  * a static mutex
3aa10 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
3aa20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
3aa30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
3aa40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
3aa50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
3aa60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
3aa70 75 74 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49  utex. {H17024} I
3aa80 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
3aa90 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68   is already with
3aaa0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a  in the mutex,.**
3aab0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3aac0 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63  nter() will bloc
3aad0 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  k and sqlite3_mu
3aae0 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72  tex_try() will r
3aaf0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
3ab00 42 55 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20  BUSY. {H17025}  
3ab10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
3ab20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
3ab30 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
3ab40 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75  E_OK].** upon su
3ab50 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
3ab60 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65   {H17026} Mutexe
3ab70 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a  s created using.
3ab80 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
3ab90 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62 65  RECURSIVE can be
3aba0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
3abb0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
3abc0 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b  ame thread..** {
3abd0 48 31 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20  H17027} In such 
3abe0 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75  cases the,.** mu
3abf0 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74  tex must be exit
3ac00 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62  ed an equal numb
3ac10 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f  er of times befo
3ac20 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  re another threa
3ac30 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20  d.** can enter. 
3ac40 20 7b 41 31 37 30 32 38 7d 20 49 66 20 74 68 65   {A17028} If the
3ac50 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
3ac60 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
3ac70 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66  other.** kind of
3ac80 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e   mutex more than
3ac90 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
3aca0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
3acb0 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51  ..** {H17029} SQ
3acc0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
3acd0 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20  exhibit.** such 
3ace0 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20  behavior in its 
3acf0 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78  own use of mutex
3ad00 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73  es..**.** Some s
3ad10 79 73 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d  ystems (for exam
3ad20 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29  ple, Windows 95)
3ad30 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
3ad40 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
3ad50 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
3ad60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3ad70 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73  y().  On those s
3ad80 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f  ystems, sqlite3_
3ad90 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77  mutex_try().** w
3ada0 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72  ill always retur
3adb0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
3adc0 7b 48 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c  {H17030} The SQL
3add0 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76  ite core only ev
3ade0 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74  er uses.** sqlit
3adf0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61  e3_mutex_try() a
3ae00 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
3ae10 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63  n so this is acc
3ae20 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72  eptable behavior
3ae30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d  ..**.** {H17031}
3ae40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3ae50 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
3ae60 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
3ae70 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
3ae80 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
3ae90 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
3aea0 61 64 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68  ad.  {A17032} Th
3aeb0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
3aec0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
3aed0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
3aee0 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
3aef0 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69   by the.** calli
3af00 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20  ng thread or is 
3af10 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
3af20 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33  located.  {H1703
3af30 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a  3} SQLite will.*
3af40 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  * never do eithe
3af50 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49  r. {END}.**.** I
3af60 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  f the argument t
3af70 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
3af80 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33  enter(), sqlite3
3af90 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72  _mutex_try(), or
3afa0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
3afb0 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e  x_leave() is a N
3afc0 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65  ULL pointer, the
3afd0 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74  n all three rout
3afe0 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61  ines.** behave a
3aff0 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  s no-ops..**.** 
3b000 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
3b010 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d  e3_mutex_held()]
3b020 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75   and [sqlite3_mu
3b030 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a  tex_notheld()]..
3b040 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
3b050 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
3b060 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
3b070 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  (int);.SQLITE_AP
3b080 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
3b090 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65  utex_free(sqlite
3b0a0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
3b0b0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3b0c0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
3b0d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3b0e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3b0f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3b100 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
3b110 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
3b120 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
3b130 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75  leave(sqlite3_mu
3b140 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tex*);../*.** CA
3b150 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65  PI3REF: Mutex Me
3b160 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31  thods Object {H1
3b170 37 31 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a  7120} <S20130>.*
3b180 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3b190 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
3b1a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3b1b0 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c  re defines the l
3b1c0 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  ow-level routine
3b1d0 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c  s.** used to all
3b1e0 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75  ocate and use mu
3b1f0 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75  texes..**.** Usu
3b200 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c  ally, the defaul
3b210 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  t mutex implemen
3b220 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64  tations provided
3b230 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a   by SQLite are.*
3b240 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f  * sufficient, ho
3b250 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20 68  wever the user h
3b260 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66  as the option of
3b270 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20   substituting a 
3b280 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d  custom.** implem
3b290 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65  entation for spe
3b2a0 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d  cialized deploym
3b2b0 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20  ents or systems 
3b2c0 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 74 65  for which SQLite
3b2d0 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f  .** does not pro
3b2e0 76 69 64 65 20 61 20 73 75 69 74 61 62 6c 65 20  vide a suitable 
3b2f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
3b300 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  In this case, th
3b310 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65  e user.** create
3b320 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20  s and populates 
3b330 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3b340 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f  his structure to
3b350 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69   pass.** to sqli
3b360 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f  te3_config() alo
3b370 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ng with the [SQL
3b380 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
3b390 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64  ] option..** Add
3b3a0 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e  itionally, an in
3b3b0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
3b3c0 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
3b3d0 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75  used as an.** ou
3b3e0 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 77 68  tput variable wh
3b3f0 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 65 20  en querying the 
3b400 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63  system for the c
3b410 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20  urrent mutex.** 
3b420 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
3b430 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54  using the [SQLIT
3b440 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
3b450 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  X] option..**.**
3b460 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20   The xMutexInit 
3b470 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62  method defined b
3b480 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
3b490 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a   is invoked as.*
3b4a0 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d  * part of system
3b4b0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
3b4c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
3b4d0 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63  nitialize() func
3b4e0 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31  tion..** {H17001
3b4f0 7d 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74  } The xMutexInit
3b500 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62   routine shall b
3b510 65 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69  e called by SQLi
3b520 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  te once for each
3b530 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 20 63 61  .** effective ca
3b540 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69  ll to [sqlite3_i
3b550 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  nitialize()]..**
3b560 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e  .** The xMutexEn
3b570 64 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64  d method defined
3b580 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75   by this structu
3b590 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73  re is invoked as
3b5a0 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74  .** part of syst
3b5b0 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74  em shutdown by t
3b5c0 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  he sqlite3_shutd
3b5d0 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  own() function. 
3b5e0 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  The.** implement
3b5f0 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65  ation of this me
3b600 74 68 6f 64 20 69 73 20 65 78 70 65 63 74 65 64  thod is expected
3b610 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20   to release all 
3b620 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72  outstanding.** r
3b630 65 73 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65  esources obtaine
3b640 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d  d by the mutex m
3b650 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74  ethods implement
3b660 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c  ation, especiall
3b670 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69  y.** those obtai
3b680 6e 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65  ned by the xMute
3b690 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48  xInit method. {H
3b6a0 31 37 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65  17003} The xMute
3b6b0 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66  xEnd().** interf
3b6c0 61 63 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76  ace shall be inv
3b6d0 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
3b6e0 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ch call to [sqli
3b6f0 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e  te3_shutdown()].
3b700 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  .**.** The remai
3b710 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f  ning seven metho
3b720 64 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  ds defined by th
3b730 69 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d  is structure (xM
3b740 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d  utexAlloc,.** xM
3b750 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78  utexFree, xMutex
3b760 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79  Enter, xMutexTry
3b770 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78  , xMutexLeave, x
3b780 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a  MutexHeld and.**
3b790 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20   xMutexNotheld) 
3b7a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f  implement the fo
3b7b0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63  llowing interfac
3b7c0 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c 79  es (respectively
3b7d0 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  ):.**.** <ul>.**
3b7e0 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
3b7f0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d  3_mutex_alloc()]
3b800 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e   </li>.**   <li>
3b810 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78    [sqlite3_mutex
3b820 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  _free()] </li>.*
3b830 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
3b840 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
3b850 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
3b860 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
3b870 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  x_try()] </li>.*
3b880 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
3b890 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
3b8a0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
3b8b0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
3b8c0 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a  x_held()] </li>.
3b8d0 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69  **   <li>  [sqli
3b8e0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
3b8f0 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f  d()] </li>.** </
3b900 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e  ul>.**.** The on
3b910 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  ly difference is
3b920 20 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63   that the public
3b930 20 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e   sqlite3_XXX fun
3b940 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65  ctions enumerate
3b950 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e  d.** above silen
3b960 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69  tly ignore any i
3b970 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20  nvocations that 
3b980 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  pass a NULL poin
3b990 74 65 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  ter instead.** o
3b9a0 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
3b9b0 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c  handle. The impl
3b9c0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
3b9d0 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e  he methods defin
3b9e0 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74  ed.** by this st
3b9f0 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20  ructure are not 
3ba00 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64  required to hand
3ba10 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  le this case, th
3ba20 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20  e results.** of 
3ba30 70 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70  passing a NULL p
3ba40 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f  ointer instead o
3ba50 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
3ba60 68 61 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66  handle are undef
3ba70 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74  ined.** (i.e. it
3ba80 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 74   is acceptable t
3ba90 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70  o provide an imp
3baa0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
3bab0 20 73 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a   segfaults if.**
3bac0 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20   it is passed a 
3bad0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a  NULL pointer)..*
3bae0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
3baf0 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 6d 75 73  nit() method mus
3bb00 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e  t be threadsafe.
3bb10 20 20 49 74 20 6d 75 73 74 20 62 65 20 68 61 72    It must be har
3bb20 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 69 6e 76 6f  mless to.** invo
3bb30 6b 65 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20  ke xMutexInit() 
3bb40 6d 75 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69  mutiple times wi
3bb50 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72  thin the same pr
3bb60 6f 63 65 73 73 20 61 6e 64 20 77 69 74 68 6f 75  ocess and withou
3bb70 74 0a 2a 2a 20 69 6e 74 65 72 76 65 6e 69 6e 67  t.** intervening
3bb80 20 63 61 6c 6c 73 20 74 6f 20 78 4d 75 74 65 78   calls to xMutex
3bb90 45 6e 64 28 29 2e 20 20 53 65 63 6f 6e 64 20 61  End().  Second a
3bba0 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  nd subsequent ca
3bbb0 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 74 65 78  lls to.** xMutex
3bbc0 49 6e 69 74 28 29 20 6d 75 73 74 20 62 65 20 6e  Init() must be n
3bbd0 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 78 4d 75  o-ops..**.** xMu
3bbe0 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e  texInit() must n
3bbf0 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65  ot use SQLite me
3bc00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
3bc10 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ([sqlite3_malloc
3bc20 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61  ()].** and its a
3bc30 73 73 6f 63 69 61 74 65 73 29 2e 20 20 53 69 6d  ssociates).  Sim
3bc40 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41 6c  ilarly, xMutexAl
3bc50 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20 75  loc() must not u
3bc60 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 79  se SQLite memory
3bc70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  .** allocation f
3bc80 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75 74 65  or a static mute
3bc90 78 2e 20 20 48 6f 77 65 76 65 72 20 78 4d 75 74  x.  However xMut
3bca0 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 20 75 73  exAlloc() may us
3bcb0 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 65 6d 6f  e SQLite.** memo
3bcc0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f  ry allocation fo
3bcd0 72 20 61 20 66 61 73 74 20 6f 72 20 72 65 63 75  r a fast or recu
3bce0 72 73 69 76 65 20 6d 75 74 65 78 2e 0a 2a 2a 0a  rsive mutex..**.
3bcf0 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69  ** SQLite will i
3bd00 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75 74 65 78  nvoke the xMutex
3bd10 45 6e 64 28 29 20 6d 65 74 68 6f 64 20 77 68 65  End() method whe
3bd20 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  n [sqlite3_shutd
3bd30 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20 63 61 6c  own()] is.** cal
3bd40 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79 20 69 66  led, but only if
3bd50 20 74 68 65 20 70 72 69 6f 72 20 63 61 6c 6c 20   the prior call 
3bd60 74 6f 20 78 4d 75 74 65 78 49 6e 69 74 20 72 65  to xMutexInit re
3bd70 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b  turned SQLITE_OK
3bd80 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65 78 49 6e  ..** If xMutexIn
3bd90 69 74 20 66 61 69 6c 73 20 69 6e 20 61 6e 79 20  it fails in any 
3bda0 77 61 79 2c 20 69 74 20 69 73 20 65 78 70 65 63  way, it is expec
3bdb0 74 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  ted to clean up 
3bdc0 61 66 74 65 72 20 69 74 73 65 6c 66 0a 2a 2a 20  after itself.** 
3bdd0 70 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e 69  prior to returni
3bde0 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ng..*/.typedef s
3bdf0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
3be00 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69  tex_methods sqli
3be10 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
3be20 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  s;.struct sqlite
3be30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
3be40 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  {.  int (*xMutex
3be50 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69  Init)(void);.  i
3be60 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28  nt (*xMutexEnd)(
3be70 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  void);.  sqlite3
3be80 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78  _mutex *(*xMutex
3be90 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76  Alloc)(int);.  v
3bea0 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65  oid (*xMutexFree
3beb0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
3bec0 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75  *);.  void (*xMu
3bed0 74 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65  texEnter)(sqlite
3bee0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e  3_mutex *);.  in
3bef0 74 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73  t (*xMutexTry)(s
3bf00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b  qlite3_mutex *);
3bf10 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78  .  void (*xMutex
3bf20 4c 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d  Leave)(sqlite3_m
3bf30 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28  utex *);.  int (
3bf40 2a 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c  *xMutexHeld)(sql
3bf50 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
3bf60 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74   int (*xMutexNot
3bf70 68 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75  held)(sqlite3_mu
3bf80 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  tex *);.};../*.*
3bf90 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
3bfa0 78 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52  x Verification R
3bfb0 6f 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d  outines {H17080}
3bfc0 20 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30   <S20130> <S3080
3bfd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
3bfe0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
3bff0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
3c000 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
3c010 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e  utines.** are in
3c020 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
3c030 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
3c040 74 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30  tatements. {H170
3c050 38 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63  81} The SQLite c
3c060 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65  ore.** never use
3c070 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  s these routines
3c080 20 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61   except inside a
3c090 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61  n assert() and a
3c0a0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61  pplications.** a
3c0b0 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f  re advised to fo
3c0c0 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66  llow the lead of
3c0d0 20 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37   the core.  {H17
3c0e0 30 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e  082} The core on
3c0f0 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69  ly.** provides i
3c100 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
3c110 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  or these routine
3c120 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d  s when it is com
3c130 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  piled.** with th
3c140 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66  e SQLITE_DEBUG f
3c150 6c 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45  lag.  {A17087} E
3c160 78 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d  xternal mutex im
3c170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
3c180 20 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72   are only requir
3c190 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ed to provide th
3c1a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20  ese routines if 
3c1b0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a  SQLITE_DEBUG is.
3c1c0 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69  ** defined and i
3c1d0 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20  f NDEBUG is not 
3c1e0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  defined..**.** {
3c1f0 48 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f  H17083} These ro
3c200 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65  utines should re
3c210 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
3c220 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20   mutex in their 
3c230 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68  argument.** is h
3c240 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c  eld or not held,
3c250 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62   respectively, b
3c260 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68  y the calling th
3c270 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37  read..**.** {X17
3c280 30 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65  084} The impleme
3c290 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72  ntation is not r
3c2a0 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69  equired to provi
3c2b0 64 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ded versions of 
3c2c0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  these.** routine
3c2d0 73 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20  s that actually 
3c2e0 77 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70  work. If the imp
3c2f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
3c300 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72   not provide wor
3c310 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  king.** versions
3c320 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
3c330 65 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74  es, it should at
3c340 20 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73   least provide s
3c350 74 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73  tubs that always
3c360 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20  .** return true 
3c370 73 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73  so that one does
3c380 20 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75   not get spuriou
3c390 73 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c  s assertion fail
3c3a0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ures..**.** {H17
3c3b0 30 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75  085} If the argu
3c3c0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
3c3d0 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20  mutex_held() is 
3c3e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74  a NULL pointer t
3c3f0 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69  hen.** the routi
3c400 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  ne should return
3c410 20 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20   1.  {END} This 
3c420 73 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e  seems counter-in
3c430 74 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a  tuitive since.**
3c440 20 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74   clearly the mut
3c450 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c  ex cannot be hel
3c460 64 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  d if it does not
3c470 20 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65   exist.  But the
3c480 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74  .** the reason t
3c490 68 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f  he mutex does no
3c4a0 74 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75  t exist is becau
3c4b0 73 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20  se the build is 
3c4c0 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74  not.** using mut
3c4d0 65 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f  exes.  And we do
3c4e0 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73   not want the as
3c4f0 73 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e  sert() containin
3c500 67 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f  g the.** call to
3c510 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3c520 65 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73  eld() to fail, s
3c530 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74  o a non-zero ret
3c540 75 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70  urn is.** the ap
3c550 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20  propriate thing 
3c560 74 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d  to do.  {H17086}
3c570 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3c580 65 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20  ex_notheld().** 
3c590 69 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64  interface should
3c5a0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77   also return 1 w
3c5b0 68 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c  hen given a NULL
3c5c0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
3c5d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3c5e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
3c5f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3c600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c610 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
3c620 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
3c630 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ex*);../*.** CAP
3c640 49 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70  I3REF: Mutex Typ
3c650 65 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37  es {H17001} <H17
3c660 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  000>.**.** The [
3c670 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
3c680 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65  loc()] interface
3c690 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
3c6a0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63  argument.** whic
3c6b0 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  h is one of thes
3c6c0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
3c6d0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nts..**.** The s
3c6e0 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74  et of static mut
3c6f0 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20  exes may change 
3c700 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20  from one SQLite 
3c710 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a  release to the.*
3c720 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61  * next.  Applica
3c730 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72  tions that overr
3c740 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  ide the built-in
3c750 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73   mutex logic mus
3c760 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64  t be.** prepared
3c770 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20   to accommodate 
3c780 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
3c790 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64  c mutexes..*/.#d
3c7a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c7b0 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
3c7c0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
3c7d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3c7e0 53 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64  SIVE        1.#d
3c7f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c800 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
3c810 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
3c820 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c830 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f  C_MEM       3  /
3c840 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
3c850 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  () */.#define SQ
3c860 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c870 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f  C_MEM2      4  /
3c880 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64  * NOT USED */.#d
3c890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c8a0 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 20 20  EX_STATIC_OPEN  
3c8b0 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65      4  /* sqlite
3c8c0 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f 0a  3BtreeOpen() */.
3c8d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c8e0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
3c8f0 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69        5  /* sqli
3c900 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a  te3_random() */.
3c910 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c920 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20  UTEX_STATIC_LRU 
3c930 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 75 20        6  /* lru 
3c940 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65  page list */.#de
3c950 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
3c960 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 20 20  X_STATIC_LRU2   
3c970 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 61 67     7  /* lru pag
3c980 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  e list */../*.**
3c990 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
3c9a0 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f  eve the mutex fo
3c9b0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
3c9c0 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d  nection {H17002}
3c9d0 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H17000>.**.** 
3c9e0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72  This interface r
3c9f0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
3ca00 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   the [sqlite3_mu
3ca10 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74  tex] object that
3ca20 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20   .** serializes 
3ca30 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64  access to the [d
3ca40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3ca50 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65  on] given in the
3ca60 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65   argument.** whe
3ca70 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67  n the [threading
3ca80 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c   mode] is Serial
3ca90 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ized..** If the 
3caa0 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
3cab0 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61   is Single-threa
3cac0 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61  d or Multi-threa
3cad0 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72  d then this.** r
3cae0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
3caf0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
3cb00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
3cb10 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
3cb20 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c  te3_db_mutex(sql
3cb30 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
3cb40 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76  API3REF: Low-Lev
3cb50 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61  el Control Of Da
3cb60 74 61 62 61 73 65 20 46 69 6c 65 73 20 7b 48 31  tabase Files {H1
3cb70 31 33 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a  1300} <S30800>.*
3cb80 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 54 68  *.** {H11301} Th
3cb90 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  e [sqlite3_file_
3cba0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72  control()] inter
3cbb0 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72  face makes a dir
3cbc0 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a  ect call to the.
3cbd0 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ** xFileControl 
3cbe0 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b  method for the [
3cbf0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
3cc00 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63  ds] object assoc
3cc10 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20  iated.** with a 
3cc20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
3cc30 61 73 65 20 69 64 65 6e 74 69 66 69 65 64 20 62  ase identified b
3cc40 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  y the second arg
3cc50 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20  ument. {H11302} 
3cc60 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74  The.** name of t
3cc70 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 74  he database is t
3cc80 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64  he name assigned
3cc90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
3cca0 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72   by the.** <a hr
3ccb0 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e  ef="lang_attach.
3ccc0 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e  html">ATTACH</a>
3ccd0 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61   SQL command tha
3cce0 74 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20  t opened the.** 
3ccf0 64 61 74 61 62 61 73 65 2e 20 7b 48 31 31 33 30  database. {H1130
3cd00 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68  3} To control th
3cd10 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
3cd20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61  file, use the na
3cd30 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20  me "main".** or 
3cd40 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
3cd50 7b 48 31 31 33 30 34 7d 20 54 68 65 20 74 68 69  {H11304} The thi
3cd60 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
3cd70 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
3cd80 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20   routine.** are 
3cd90 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20  passed directly 
3cda0 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
3cdb0 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
3cdc0 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a  parameters of.**
3cdd0 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
3cde0 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 31 33  l method.  {H113
3cdf0 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76  05} The return v
3ce00 61 6c 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c  alue of the xFil
3ce10 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68  eControl.** meth
3ce20 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72  od becomes the r
3ce30 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
3ce40 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
3ce50 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 20 74  ** {H11306} If t
3ce60 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
3ce70 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f  ter (zDbName) do
3ce80 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
3ce90 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20   name of any.** 
3cea0 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
3ceb0 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f  le, then SQLITE_
3cec0 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
3ced0 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 69 73  d. {H11307} This
3cee0 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69   error.** code i
3cef0 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64  s not remembered
3cf00 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65   and will not be
3cf10 20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71   recalled by [sq
3cf20 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
3cf30 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  .** or [sqlite3_
3cf40 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33  errmsg()]. {A113
3cf50 30 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69  08} The underlyi
3cf60 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ng xFileControl 
3cf70 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20  method might.** 
3cf80 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49  also return SQLI
3cf90 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 31 33  TE_ERROR.  {A113
3cfa0 30 39 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20  09} There is no 
3cfb0 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69  way to distingui
3cfc0 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e  sh between.** an
3cfd0 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61   incorrect zDbNa
3cfe0 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45  me and an SQLITE
3cff0 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72  _ERROR return fr
3d000 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  om the underlyin
3d010 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f  g.** xFileContro
3d020 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a  l method. {END}.
3d030 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
3d040 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f  [SQLITE_FCNTL_LO
3d050 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49  CKSTATE].*/.SQLI
3d060 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3d070 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
3d080 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
3d090 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69  char *zDbName, i
3d0a0 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a  nt op, void*);..
3d0b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3d0c0 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63  Testing Interfac
3d0d0 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 30 38  e {H11400} <S308
3d0e0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
3d0f0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3d100 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  ol() interface i
3d110 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f  s used to read o
3d120 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73  ut internal.** s
3d130 74 61 74 65 20 6f 66 20 53 51 4c 69 74 65 20 61  tate of SQLite a
3d140 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 61 75  nd to inject fau
3d150 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 65 20  lts into SQLite 
3d160 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70  for testing.** p
3d170 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 66 69  urposes.  The fi
3d180 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
3d190 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f   an operation co
3d1a0 64 65 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  de that determin
3d1b0 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  es.** the number
3d1c0 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f  , meaning, and o
3d1d0 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  peration of all 
3d1e0 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d  subsequent param
3d1f0 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  eters..**.** Thi
3d200 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e  s interface is n
3d210 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20 61 70  ot for use by ap
3d220 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74 20  plications.  It 
3d230 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a  exists solely.**
3d240 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20 74   for verifying t
3d250 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61  he correct opera
3d260 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69  tion of the SQLi
3d270 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65 70  te library.  Dep
3d280 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77  ending.** on how
3d290 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
3d2a0 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c  ary is compiled,
3d2b0 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
3d2c0 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 2e  might not exist.
3d2d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69  .**.** The detai
3d2e0 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ls of the operat
3d2f0 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72  ion codes, their
3d300 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70   meanings, the p
3d310 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65  arameters.** the
3d320 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74  y take, and what
3d330 20 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c   they do are all
3d340 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
3d350 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63  ge without notic
3d360 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73  e..** Unlike mos
3d370 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  t of the SQLite 
3d380 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69  API, this functi
3d390 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e  on is not guaran
3d3a0 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61  teed to.** opera
3d3b0 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20  te consistently 
3d3c0 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65  from one release
3d3d0 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f   to the next..*/
3d3e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3d3f0 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
3d400 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  trol(int op, ...
3d410 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3d420 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65  EF: Testing Inte
3d430 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e 20  rface Operation 
3d440 43 6f 64 65 73 20 7b 48 31 31 34 31 30 7d 20 3c  Codes {H11410} <
3d450 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H11400>.**.** Th
3d460 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
3d470 65 20 74 68 65 20 76 61 6c 69 64 20 6f 70 65 72  e the valid oper
3d480 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d  ation code param
3d490 65 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 61 73  eters used.** as
3d4a0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
3d4b0 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ent to [sqlite3_
3d4c0 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e  test_control()].
3d4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 61 72  .**.** These par
3d4e0 61 6d 65 74 65 72 73 20 61 6e 64 20 74 68 65 69  ameters and thei
3d4f0 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 20 73  r meanings are s
3d500 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
3d510 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f 74 69  .** without noti
3d520 63 65 2e 20 20 54 68 65 73 65 20 76 61 6c 75 65  ce.  These value
3d530 73 20 61 72 65 20 66 6f 72 20 74 65 73 74 69 6e  s are for testin
3d540 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e  g purposes only.
3d550 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
3d560 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20   should not use 
3d570 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70 61 72  any of these par
3d580 61 6d 65 74 65 72 73 20 6f 72 20 74 68 65 0a 2a  ameters or the.*
3d590 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  * [sqlite3_test_
3d5a0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72  control()] inter
3d5b0 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  face..*/.#define
3d5c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d5d0 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20 20 20  _PRNG_SAVE      
3d5e0 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66            5.#def
3d5f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d600 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45  TRL_PRNG_RESTORE
3d610 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23               6.#
3d620 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d630 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45  STCTRL_PRNG_RESE
3d640 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
3d650 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  7.#define SQLITE
3d660 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43  _TESTCTRL_BITVEC
3d670 5f 54 45 53 54 20 20 20 20 20 20 20 20 20 20 20  _TEST           
3d680 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c     8.#define SQL
3d690 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55  ITE_TESTCTRL_FAU
3d6a0 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 20 20  LT_INSTALL      
3d6b0 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20        9.#define 
3d6c0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d6d0 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f  BENIGN_MALLOC_HO
3d6e0 4f 4b 53 20 20 20 20 20 31 30 0a 23 64 65 66 69  OKS     10.#defi
3d6f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3d700 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  RL_PENDING_BYTE 
3d710 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64             11.#d
3d720 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d730 54 43 54 52 4c 5f 41 53 53 45 52 54 20 20 20 20  TCTRL_ASSERT    
3d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
3d750 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3d760 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 20  TESTCTRL_ALWAYS 
3d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d780 20 31 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   13.#define SQLI
3d790 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45  TE_TESTCTRL_RESE
3d7a0 52 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20  RVE             
3d7b0 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 2a 20 43 41      14../*.** CA
3d7c0 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52  PI3REF: SQLite R
3d7d0 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48  untime Status {H
3d7e0 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17200} <S60200>.
3d7f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3d800 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3d810 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
3d820 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65  retrieve runtime
3d830 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
3d840 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65  ion.** about the
3d850 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20   preformance of 
3d860 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69  SQLite, and opti
3d870 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20  onally to reset 
3d880 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77  various.** highw
3d890 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65  ater marks.  The
3d8a0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
3d8b0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
3d8c0 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  de for.** the sp
3d8d0 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72  ecific parameter
3d8e0 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65   to measure.  Re
3d8f0 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72  cognized integer
3d900 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66   codes.** are of
3d910 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54   the form [SQLIT
3d920 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
3d930 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54  USED | SQLITE_ST
3d940 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68  ATUS_...]..** Th
3d950 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
3d960 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
3d970 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74   is returned int
3d980 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20  o *pCurrent..** 
3d990 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  The highest reco
3d9a0 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  rded value is re
3d9b0 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68  turned in *pHigh
3d9c0 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a  water.  If the.*
3d9d0 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74  * resetFlag is t
3d9e0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69  rue, then the hi
3d9f0 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c  ghest record val
3da00 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65  ue is reset afte
3da10 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  r.** *pHighwater
3da20 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d   is written. Som
3da30 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20  e parameters do 
3da40 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68  not record the h
3da50 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e  ighest.** value.
3da60 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61    For those para
3da70 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e  meters.** nothin
3da80 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3da90 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e  o *pHighwater an
3daa0 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20  d the resetFlag 
3dab0 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f  is ignored..** O
3dac0 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  ther parameters 
3dad0 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20  record only the 
3dae0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
3daf0 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65  nd not the curre
3db00 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f  nt.** value.  Fo
3db10 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70  r these latter p
3db20 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e  arameters nothin
3db30 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3db40 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a  o *pCurrent..**.
3db50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3db60 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
3db70 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  K on success and
3db80 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b   a non-zero.** [
3db90 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
3dba0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ailure..**.** Th
3dbb0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
3dbc0 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20  readsafe but is 
3dbd0 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69  not atomic.  Thi
3dbe0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
3dbf0 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65  .** called while
3dc00 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61   other threads a
3dc10 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73  re running the s
3dc20 61 6d 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74  ame or different
3dc30 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72   SQLite.** inter
3dc40 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20  faces.  However 
3dc50 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
3dc60 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74  ned in *pCurrent
3dc70 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61   and.** *pHighwa
3dc80 74 65 72 20 72 65 66 6c 65 63 74 20 74 68 65 20  ter reflect the 
3dc90 73 74 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65  status of SQLite
3dca0 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f   at different po
3dcb0 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20  ints in time.** 
3dcc0 61 6e 64 20 69 74 20 69 73 20 70 6f 73 73 69 62  and it is possib
3dcd0 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  le that another 
3dce0 74 68 72 65 61 64 20 6d 69 67 68 74 20 63 68 61  thread might cha
3dcf0 6e 67 65 20 74 68 65 20 70 61 72 61 6d 65 74 65  nge the paramete
3dd00 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20  r.** in between 
3dd10 74 68 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a  the times when *
3dd20 70 43 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48  pCurrent and *pH
3dd30 69 67 68 77 61 74 65 72 20 61 72 65 20 77 72 69  ighwater are wri
3dd40 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  tten..**.** See 
3dd50 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64  also: [sqlite3_d
3dd60 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53  b_status()].*/.S
3dd70 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
3dd80 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
3dd90 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  t sqlite3_status
3dda0 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43  (int op, int *pC
3ddb0 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69  urrent, int *pHi
3ddc0 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73  ghwater, int res
3ddd0 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a  etFlag);.../*.**
3dde0 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75   CAPI3REF: Statu
3ddf0 73 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31  s Parameters {H1
3de00 37 32 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a  7250} <H17200>.*
3de10 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3de20 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67  *.** These integ
3de30 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73  er constants des
3de40 69 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72  ignate various r
3de50 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70  un-time status p
3de60 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61  arameters.** tha
3de70 74 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  t can be returne
3de80 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74  d by [sqlite3_st
3de90 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c  atus()]..**.** <
3dea0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  dl>.** <dt>SQLIT
3deb0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
3dec0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3ded0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3dee0 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61  is the current a
3def0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
3df00 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75  checked out.** u
3df10 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61  sing [sqlite3_ma
3df20 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20  lloc()], either 
3df30 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69  directly or indi
3df40 72 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20  rectly.  The.** 
3df50 66 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20  figure includes 
3df60 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73  calls made to [s
3df70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
3df80 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
3df90 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72  ion.** and inter
3dfa0 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  nal memory usage
3dfb0 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c   by the SQLite l
3dfc0 69 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68  ibrary.  Scratch
3dfd0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72   memory.** contr
3dfe0 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45  olled by [SQLITE
3dff0 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3e000 20 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70   and auxiliary p
3e010 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d  age-cache.** mem
3e020 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62  ory controlled b
3e030 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  y [SQLITE_CONFIG
3e040 5f 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e  _PAGECACHE] is n
3e050 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a  ot included in.*
3e060 2a 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72  * this parameter
3e070 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65  .  The amount re
3e080 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75  turned is the su
3e090 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  m of the allocat
3e0a0 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20  ion.** sizes as 
3e0b0 72 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20  reported by the 
3e0c0 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20  xSize method in 
3e0d0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74  [sqlite3_mem_met
3e0e0 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  hods].</dd>.**.*
3e0f0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3e100 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c  TUS_MALLOC_SIZE<
3e110 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3e120 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72   parameter recor
3e130 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d  ds the largest m
3e140 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3e150 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64   request.** hand
3e160 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ed to [sqlite3_m
3e170 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c  alloc()] or [sql
3e180 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20  ite3_realloc()] 
3e190 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74  (or their.** int
3e1a0 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74  ernal equivalent
3e1b0 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61  s).  Only the va
3e1c0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20  lue returned in 
3e1d0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  the.** *pHighwat
3e1e0 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  er parameter to 
3e1f0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
3e200 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73  )] is of interes
3e210 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75  t.  .** The valu
3e220 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
3e230 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72  he *pCurrent par
3e240 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69  ameter is undefi
3e250 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
3e260 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
3e270 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44  S_PAGECACHE_USED
3e280 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3e290 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
3e2a0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
3e2b0 66 20 70 61 67 65 73 20 75 73 65 64 20 6f 75 74  f pages used out
3e2c0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65   of the.** [page
3e2d0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  cache memory all
3e2e0 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73  ocator] that was
3e2f0 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e   configured usin
3e300 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  g .** [SQLITE_CO
3e310 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e  NFIG_PAGECACHE].
3e320 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72    The.** value r
3e330 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61  eturned is in pa
3e340 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65  ges, not in byte
3e350 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  s.</dd>.**.** <d
3e360 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t>SQLITE_STATUS_
3e370 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c  PAGECACHE_OVERFL
3e380 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  OW</dt>.** <dd>T
3e390 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3e3a0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
3e3b0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 67   of bytes of pag
3e3c0 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63  e cache.** alloc
3e3d0 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c  ation which coul
3e3e0 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66  d not be statisf
3e3f0 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49  ied by the [SQLI
3e400 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
3e410 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61  CHE].** buffer a
3e420 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20  nd where forced 
3e430 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b  to overflow to [
3e440 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
3e450 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72  ].  The.** retur
3e460 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64  ned value includ
3e470 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  es allocations t
3e480 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62  hat overflowed b
3e490 65 63 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77  ecause they.** w
3e4a0 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28  here too large (
3e4b0 74 68 65 79 20 77 65 72 65 20 6c 61 72 67 65 72  they were larger
3e4c0 20 74 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70   than the "sz" p
3e4d0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b  arameter to.** [
3e4e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
3e4f0 47 45 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c  GECACHE]) and al
3e500 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f  locations that o
3e510 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73  verflowed becaus
3e520 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61  e.** no space wa
3e530 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61  s left in the pa
3e540 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a  ge cache.</dd>.*
3e550 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3e560 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
3e570 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SIZE</dt>.** <d
3e580 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3e590 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72   records the lar
3e5a0 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  gest memory allo
3e5b0 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a  cation request.*
3e5c0 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67  * handed to [pag
3e5d0 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ecache memory al
3e5e0 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20  locator].  Only 
3e5f0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
3e600 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48  ed in the.** *pH
3e610 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74  ighwater paramet
3e620 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  er to [sqlite3_s
3e630 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69  tatus()] is of i
3e640 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68  nterest.  .** Th
3e650 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  e value written 
3e660 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65  into the *pCurre
3e670 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
3e680 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
3e690 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3e6a0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3e6b0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3e6c0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3e6d0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
3e6e0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
3e6f0 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68  s used out of th
3e700 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65  e.** [scratch me
3e710 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
3e720 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
3e730 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
3e740 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68  IG_SCRATCH].  Th
3e750 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
3e760 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f   is in allocatio
3e770 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79  ns, not.** in by
3e780 74 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69  tes.  Since a si
3e790 6e 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20  ngle thread may 
3e7a0 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63  only have one sc
3e7b0 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
3e7c0 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  .** outstanding 
3e7d0 61 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61  at time, this pa
3e7e0 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70  rameter also rep
3e7f0 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20  orts the number 
3e800 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73  of threads.** us
3e810 69 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f  ing scratch memo
3e820 72 79 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ry at the same t
3e830 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ime.</dd>.**.** 
3e840 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
3e850 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
3e860 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  OW</dt>.** <dd>T
3e870 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3e880 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
3e890 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 63 72   of bytes of scr
3e8a0 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  atch memory.** a
3e8b0 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20  llocation which 
3e8c0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61  could not be sta
3e8d0 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b  tisfied by the [
3e8e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
3e8f0 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72  RATCH].** buffer
3e900 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65   and where force
3e910 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f  d to overflow to
3e920 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3e930 28 29 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73  ()].  The values
3e940 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63  .** returned inc
3e950 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62  lude overflows b
3e960 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65  ecause the reque
3e970 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  sted allocation 
3e980 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65  was too.** large
3e990 72 20 28 74 68 61 74 20 69 73 2c 20 62 65 63 61  r (that is, beca
3e9a0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
3e9b0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  d allocation was
3e9c0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
3e9d0 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74  .** "sz" paramet
3e9e0 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f  er to [SQLITE_CO
3e9f0 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61  NFIG_SCRATCH]) a
3ea00 6e 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63  nd because no sc
3ea10 72 61 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20  ratch buffer.** 
3ea20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c  slots were avail
3ea30 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a  able..** </dd>.*
3ea40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3ea50 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53  STATUS_SCRATCH_S
3ea60 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IZE</dt>.** <dd>
3ea70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3ea80 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65  ecords the large
3ea90 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  st memory alloca
3eaa0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20  tion request.** 
3eab0 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74  handed to [scrat
3eac0 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ch memory alloca
3ead0 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20  tor].  Only the 
3eae0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
3eaf0 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77  n the.** *pHighw
3eb00 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74  ater parameter t
3eb10 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  o [sqlite3_statu
3eb20 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72  s()] is of inter
3eb30 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61  est.  .** The va
3eb40 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  lue written into
3eb50 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70   the *pCurrent p
3eb60 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65  arameter is unde
3eb70 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  fined.</dd>.**.*
3eb80 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3eb90 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b  TUS_PARSER_STACK
3eba0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3ebb0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
3ebc0 72 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20  rds the deepest 
3ebd0 70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49  parser stack.  I
3ebe0 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61  t is only.** mea
3ebf0 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74  ningful if SQLit
3ec00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
3ec10 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54  th [YYTRACKMAXST
3ec20 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a  ACKDEPTH].</dd>.
3ec30 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e  ** </dl>.**.** N
3ec40 65 77 20 73 74 61 74 75 73 20 70 61 72 61 6d 65  ew status parame
3ec50 74 65 72 73 20 6d 61 79 20 62 65 20 61 64 64 65  ters may be adde
3ec60 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74  d from time to t
3ec70 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ime..*/.#define 
3ec80 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
3ec90 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 20 20 20  MORY_USED       
3eca0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
3ecb0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3ecc0 41 43 48 45 5f 55 53 45 44 20 20 20 20 20 20 20  ACHE_USED       
3ecd0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3ece0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
3ecf0 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23  E_OVERFLOW   2.#
3ed00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3ed10 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45  ATUS_SCRATCH_USE
3ed20 44 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66  D         3.#def
3ed30 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3ed40 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
3ed50 4f 57 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65  OW     4.#define
3ed60 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d   SQLITE_STATUS_M
3ed70 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20  ALLOC_SIZE      
3ed80 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51      5.#define SQ
3ed90 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53  LITE_STATUS_PARS
3eda0 45 52 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20  ER_STACK        
3edb0 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   6.#define SQLIT
3edc0 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
3edd0 48 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a  HE_SIZE       7.
3ede0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3edf0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
3ee00 5a 45 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a  ZE         8../*
3ee10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
3ee20 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
3ee30 6e 20 53 74 61 74 75 73 20 7b 48 31 37 35 30 30  n Status {H17500
3ee40 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58  } <S60200>.** EX
3ee50 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3ee60 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
3ee70 69 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  is used to retri
3ee80 65 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74  eve runtime stat
3ee90 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a  us information .
3eea0 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c  ** about a singl
3eeb0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3eec0 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69  ection].  The fi
3eed0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
3eee0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
3eef0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63  connection objec
3ef00 74 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67  t to be interrog
3ef10 61 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e  ated.  The secon
3ef20 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73  d argument.** is
3ef30 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74   the parameter t
3ef40 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20  o interrogate.  
3ef50 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f  Currently, the o
3ef60 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  nly allowed valu
3ef70 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63  e.** for the sec
3ef80 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
3ef90 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55   [SQLITE_DBSTATU
3efa0 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44  S_LOOKASIDE_USED
3efb0 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c  ]..** Additional
3efc0 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69   options will li
3efd0 6b 65 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66  kely appear in f
3efe0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
3eff0 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  f SQLite..**.** 
3f000 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  The current valu
3f010 65 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  e of the request
3f020 65 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ed parameter is 
3f030 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43  written into *pC
3f040 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69  ur.** and the hi
3f050 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65  ghest instantane
3f060 6f 75 73 20 76 61 6c 75 65 20 69 73 20 77 72 69  ous value is wri
3f070 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74  tten into *pHiwt
3f080 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65  r.  If.** the re
3f090 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20  setFlg is true, 
3f0a0 74 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74  then the highest
3f0b0 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76   instantaneous v
3f0c0 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74  alue is.** reset
3f0d0 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68   back down to th
3f0e0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e  e current value.
3f0f0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
3f100 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
3f110 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3f120 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e  _stmt_status()].
3f130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
3f140 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
3f150 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  AL int sqlite3_d
3f160 62 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  b_status(sqlite3
3f170 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a  *, int op, int *
3f180 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74  pCur, int *pHiwt
3f190 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29  r, int resetFlg)
3f1a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3f1b0 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65  F: Status Parame
3f1c0 74 65 72 73 20 66 6f 72 20 64 61 74 61 62 61 73  ters for databas
3f1d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48  e connections {H
3f1e0 31 37 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a  17520} <H17500>.
3f1f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3f200 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
3f210 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76  tants are the av
3f220 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20  ailable integer 
3f230 22 76 65 72 62 73 22 20 74 68 61 74 20 63 61 6e  "verbs" that can
3f240 20 62 65 20 70 61 73 73 65 64 20 61 73 0a 2a 2a   be passed as.**
3f250 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3f260 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c  ment to the [sql
3f270 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29  ite3_db_status()
3f280 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  ] interface..**.
3f290 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 6d 61 79  ** New verbs may
3f2a0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
3f2b0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
3f2c0 53 51 4c 69 74 65 2e 20 45 78 69 73 74 69 6e 67  SQLite. Existing
3f2d0 20 76 65 72 62 73 0a 2a 2a 20 6d 69 67 68 74 20   verbs.** might 
3f2e0 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e  be discontinued.
3f2f0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   Applications sh
3f300 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72  ould check the r
3f310 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 0a  eturn code from.
3f320 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  ** [sqlite3_db_s
3f330 74 61 74 75 73 28 29 5d 20 74 6f 20 6d 61 6b 65  tatus()] to make
3f340 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63   sure that the c
3f350 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a 2a 2a 20 54  all worked..** T
3f360 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  he [sqlite3_db_s
3f370 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61  tatus()] interfa
3f380 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61  ce will return a
3f390 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72 6f 72 20   non-zero error 
3f3a0 63 6f 64 65 0a 2a 2a 20 69 66 20 61 20 64 69 73  code.** if a dis
3f3b0 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73  continued or uns
3f3c0 75 70 70 6f 72 74 65 64 20 76 65 72 62 20 69 73  upported verb is
3f3d0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20   invoked..**.** 
3f3e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  <dl>.** <dt>SQLI
3f3f0 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
3f400 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e 0a  ASIDE_USED</dt>.
3f410 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3f420 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
3f430 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b  e number of look
3f440 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f  aside memory slo
3f450 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  ts currently.** 
3f460 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64  checked out.</dd
3f470 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  >.** </dl>.*/.#d
3f480 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53  efine SQLITE_DBS
3f490 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
3f4a0 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a 0a  USED     0.../*.
3f4b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72 65  ** CAPI3REF: Pre
3f4c0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
3f4d0 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d 20  Status {H17550} 
3f4e0 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45  <S60200>.** EXPE
3f4f0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45  RIMENTAL.**.** E
3f500 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74 61  ach prepared sta
3f510 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e 73  tement maintains
3f520 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51 4c   various.** [SQL
3f530 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
3f540 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d 20  ORT | counters] 
3f550 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 65  that measure the
3f560 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 69   number.** of ti
3f570 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 6f  mes it has perfo
3f580 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f 70  rmed specific op
3f590 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73 65  erations.  These
3f5a0 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a 2a   counters can.**
3f5b0 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e 69   be used to moni
3f5c0 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d 61  tor the performa
3f5d0 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  nce characterist
3f5e0 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70 61  ics of the prepa
3f5f0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
3f600 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  s.  For example,
3f610 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   if the number o
3f620 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67 72  f table steps gr
3f630 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a 2a  eatly exceeds.**
3f640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
3f650 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f 72  able searches or
3f660 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74 68   result rows, th
3f670 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74 6f  at would tend to
3f680 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 61   indicate.** tha
3f690 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  t the prepared s
3f6a0 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69 6e  tatement is usin
3f6b0 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  g a full table s
3f6c0 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a  can rather than.
3f6d0 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a 2a  ** an index.  .*
3f6e0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
3f6f0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
3f700 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73 65  etrieve and rese
3f710 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73  t counter values
3f720 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65 70   from.** a [prep
3f730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
3f740 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
3f750 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 70  ment is the prep
3f760 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
3f770 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69  * object to be i
3f780 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68  nterrogated.  Th
3f790 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3f7a0 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67  t.** is an integ
3f7b0 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73 70  er code for a sp
3f7c0 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f 53  ecific [SQLITE_S
3f7d0 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 7c  TMTSTATUS_SORT |
3f7e0 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f 20   counter].** to 
3f7f0 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e  be interrogated.
3f800 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74   .** The current
3f810 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65   value of the re
3f820 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20  quested counter 
3f830 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
3f840 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 20  If the resetFlg 
3f850 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
3f860 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 73  e counter is res
3f870 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72  et to zero after
3f880 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61   this.** interfa
3f890 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  ce call returns.
3f8a0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
3f8b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
3f8c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3f8d0 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a  _db_status()]..*
3f8e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
3f8f0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
3f900 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d   int sqlite3_stm
3f910 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  t_status(sqlite3
3f920 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 69  _stmt*, int op,i
3f930 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f  nt resetFlg);../
3f940 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
3f950 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73  tatus Parameters
3f960 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73 74   for prepared st
3f970 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37 30  atements {H17570
3f980 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45 58  } <H17550>.** EX
3f990 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3f9a0 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 73   These preproces
3f9b0 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  sor macros defin
3f9c0 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20  e integer codes 
3f9d0 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 65  that name counte
3f9e0 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 6f  r.** values asso
3f9f0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
3fa00 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74  [sqlite3_stmt_st
3fa10 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63  atus()] interfac
3fa20 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e  e..** The meanin
3fa30 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75  gs of the variou
3fa40 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 61  s counters are a
3fa50 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
3fa60 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
3fa70 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46  ITE_STMTSTATUS_F
3fa80 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64 74  ULLSCAN_STEP</dt
3fa90 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73  >.** <dd>This is
3faa0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
3fab0 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74 65  imes that SQLite
3fac0 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f 72   has stepped for
3fad0 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61 62  ward in.** a tab
3fae0 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61 20  le as part of a 
3faf0 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e  full table scan.
3fb00 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73 20    Large numbers 
3fb10 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65 72  for this counter
3fb20 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74 65  .** may indicate
3fb30 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20 66   opportunities f
3fb40 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69  or performance i
3fb50 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f 75  mprovement throu
3fb60 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20 75  gh .** careful u
3fb70 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f  se of indices.</
3fb80 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3fb90 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
3fba0 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SORT</dt>.** <dd
3fbb0 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d  >This is the num
3fbc0 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65 72  ber of sort oper
3fbd0 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  ations that have
3fbe0 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41 20   occurred..** A 
3fbf0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69  non-zero value i
3fc00 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6d  n this counter m
3fc10 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20 6f  ay indicate an o
3fc20 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a 2a  pportunity to.**
3fc30 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65 72   improvement per
3fc40 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67 68  formance through
3fc50 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20   careful use of 
3fc60 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a  indices.</dd>.**
3fc70 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65  .** </dl>.*/.#de
3fc80 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54  fine SQLITE_STMT
3fc90 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f  STATUS_FULLSCAN_
3fca0 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66 69  STEP     1.#defi
3fcb0 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  ne SQLITE_STMTST
3fcc0 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20 20  ATUS_SORT       
3fcd0 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20         2../*.** 
3fce0 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f 6d  CAPI3REF: Custom
3fcf0 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a 65   Page Cache Obje
3fd00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ct.** EXPERIMENT
3fd10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  AL.**.** The sql
3fd20 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70 65  ite3_pcache type
3fd30 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74 20   is opaque.  It 
3fd40 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
3fd50 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 61 62  y.** the pluggab
3fd60 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20  le module.  The 
3fd70 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 73 20  SQLite core has 
3fd80 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 0a  no knowledge of.
3fd90 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 20 69  ** its size or i
3fda0 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72  nternal structur
3fdb0 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 61 6c  e and never deal
3fdc0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 71  s with the.** sq
3fdd0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f 62 6a  lite3_pcache obj
3fde0 65 63 74 20 65 78 63 65 70 74 20 62 79 20 68 6f  ect except by ho
3fdf0 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 69 6e  lding and passin
3fe00 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 74 6f  g pointers.** to
3fe10 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a   the object..**.
3fe20 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 33 5f  ** See [sqlite3_
3fe30 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20  pcache_methods] 
3fe40 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
3fe50 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
3fe60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
3fe70 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 71 6c  lite3_pcache sql
3fe80 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a 2f 2a  ite3_pcache;../*
3fe90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 70  .** CAPI3REF: Ap
3fea0 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e 65  plication Define
3feb0 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a 2a 2a  d Page Cache..**
3fec0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 61 67 65   KEYWORDS: {page
3fed0 20 63 61 63 68 65 7d 0a 2a 2a 20 45 58 50 45 52   cache}.** EXPER
3fee0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
3fef0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  e [sqlite3_confi
3ff00 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  g]([SQLITE_CONFI
3ff10 47 5f 50 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20  G_PCACHE], ...) 
3ff20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a  interface can.**
3ff30 20 72 65 67 69 73 74 65 72 20 61 6e 20 61 6c 74   register an alt
3ff40 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61  ernative page ca
3ff50 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
3ff60 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 20 69 6e  on by passing in
3ff70 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65   an .** instance
3ff80 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
3ff90 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73  pcache_methods s
3ffa0 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 6d 61  tructure. The ma
3ffb0 6a 6f 72 69 74 79 20 6f 66 20 74 68 65 20 0a 2a  jority of the .*
3ffc0 2a 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75 73  * heap memory us
3ffd0 65 64 20 62 79 20 53 51 4c 69 74 65 20 69 73 20  ed by SQLite is 
3ffe0 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65  used by the page
3fff0 20 63 61 63 68 65 20 74 6f 20 63 61 63 68 65 20   cache to cache 
40000 64 61 74 61 20 72 65 61 64 20 0a 2a 2a 20 66 72  data read .** fr
40010 6f 6d 2c 20 6f 72 20 72 65 61 64 79 20 74 6f 20  om, or ready to 
40020 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 74  be written to, t
40030 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
40040 2e 20 42 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  . By implementin
40050 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70  g a .** custom p
40060 61 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 20  age cache using 
40070 74 68 69 73 20 41 50 49 2c 20 61 6e 20 61 70 70  this API, an app
40080 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e  lication can con
40090 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72  trol more .** pr
400a0 65 63 69 73 65 6c 79 20 74 68 65 20 61 6d 6f 75  ecisely the amou
400b0 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e  nt of memory con
400c0 73 75 6d 65 64 20 62 79 20 53 51 4c 69 74 65 2c  sumed by SQLite,
400d0 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 63   the way in whic
400e0 68 20 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72  h .** that memor
400f0 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  y is allocated a
40100 6e 64 20 72 65 6c 65 61 73 65 64 2c 20 61 6e 64  nd released, and
40110 20 74 68 65 20 70 6f 6c 69 63 69 65 73 20 75 73   the policies us
40120 65 64 20 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d  ed to .** determ
40130 69 6e 65 20 65 78 61 63 74 6c 79 20 77 68 69 63  ine exactly whic
40140 68 20 70 61 72 74 73 20 6f 66 20 61 20 64 61 74  h parts of a dat
40150 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 63  abase file are c
40160 61 63 68 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a  ached and for .*
40170 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a  * how long..**.*
40180 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  * The contents o
40190 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
401a0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72  ache_methods str
401b0 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 65  ucture are copie
401c0 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 74 65 72  d to an.** inter
401d0 6e 61 6c 20 62 75 66 66 65 72 20 62 79 20 53 51  nal buffer by SQ
401e0 4c 69 74 65 20 77 69 74 68 69 6e 20 74 68 65 20  Lite within the 
401f0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
40200 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 6e 63 65  _config].  Hence
40210 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
40220 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 72 64 20  ion may discard 
40230 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 61 66  the parameter af
40240 74 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a  ter the call to.
40250 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ** [sqlite3_conf
40260 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e 0a 2a  ig()] returns..*
40270 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28 29  *.** The xInit()
40280 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c 65   method is calle
40290 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  d once for each 
402a0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
402b0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a 2a  _initialize()].*
402c0 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20  * (usually only 
402d0 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65 20  once during the 
402e0 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20  lifetime of the 
402f0 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73 20  process). It is 
40300 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70 79  passed.** a copy
40310 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
40320 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e 70  pcache_methods.p
40330 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63 61  Arg value. It ca
40340 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74  n be used to set
40350 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73 74  .** up global st
40360 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75 74  ructures and mut
40370 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62 79  exes required by
40380 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67 65   the custom page
40390 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c 65   cache .** imple
403a0 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a  mentation. .**.*
403b0 2a 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 28  * The xShutdown(
403c0 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c  ) method is call
403d0 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 5b  ed from within [
403e0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
403f0 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20  ()], .** if the 
40400 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f  application invo
40410 6b 65 73 20 74 68 69 73 20 41 50 49 2e 20 49 74  kes this API. It
40420 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
40430 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 61 6e 79  clean up .** any
40440 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 73   outstanding res
40450 6f 75 72 63 65 73 20 62 65 66 6f 72 65 20 70 72  ources before pr
40460 6f 63 65 73 73 20 73 68 75 74 64 6f 77 6e 2c 20  ocess shutdown, 
40470 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  if required..**.
40480 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64 73 20  ** SQLite holds 
40490 61 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  a [SQLITE_MUTEX_
404a0 52 45 43 55 52 53 49 56 45 5d 20 6d 75 74 65 78  RECURSIVE] mutex
404b0 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73   when it invokes
404c0 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65  .** the xInit me
404d0 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e  thod, so the xIn
404e0 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e  it method need n
404f0 6f 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65  ot be threadsafe
40500 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64  .  The.** xShutd
40510 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e  own method is on
40520 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b  ly called from [
40530 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
40540 28 29 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a  ()] so it does.*
40550 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  * not need to be
40560 20 74 68 72 65 61 64 73 61 66 65 20 65 69 74 68   threadsafe eith
40570 65 72 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 6d  er.  All other m
40580 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74  ethods must be t
40590 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 69 6e 20  hreadsafe.** in 
405a0 6d 75 6c 74 69 74 68 72 65 61 64 65 64 20 61 70  multithreaded ap
405b0 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  plications..**.*
405c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  * SQLite will ne
405d0 76 65 72 20 69 6e 76 6f 6b 65 20 78 49 6e 69 74  ver invoke xInit
405e0 28 29 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  () more than onc
405f0 65 20 77 69 74 68 6f 75 74 20 61 6e 20 69 6e 74  e without an int
40600 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 61 6c 6c  ervening.** call
40610 20 74 6f 20 78 53 68 75 74 64 6f 77 6e 28 29 2e   to xShutdown().
40620 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61  .**.** The xCrea
40630 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  te() method is u
40640 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74  sed to construct
40650 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73   a new cache ins
40660 74 61 6e 63 65 2e 20 20 53 51 4c 69 74 65 0a 2a  tance.  SQLite.*
40670 2a 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  * will typically
40680 20 63 72 65 61 74 65 20 6f 6e 65 20 63 61 63 68   create one cach
40690 65 20 69 6e 73 74 61 6e 63 65 20 66 6f 72 20 65  e instance for e
406a0 61 63 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73  ach open databas
406b0 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 68 6f 75 67  e file,.** thoug
406c0 68 20 74 68 69 73 20 69 73 20 6e 6f 74 20 67 75  h this is not gu
406d0 61 72 61 6e 74 65 65 64 2e 20 54 68 65 0a 2a 2a  aranteed. The.**
406e0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
406f0 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74 68 65  , szPage, is the
40700 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
40710 66 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74  f the pages that
40720 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f   must.** be allo
40730 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61 63  cated by the cac
40740 68 65 2e 20 20 73 7a 50 61 67 65 20 77 69 6c 6c  he.  szPage will
40750 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72 20   not be a power 
40760 6f 66 20 74 77 6f 2e 20 20 73 7a 50 61 67 65 0a  of two.  szPage.
40770 2a 2a 20 77 69 6c 6c 20 74 68 65 20 70 61 67 65  ** will the page
40780 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
40790 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20  abase file that 
407a0 69 73 20 74 6f 20 62 65 20 63 61 63 68 65 64 20  is to be cached 
407b0 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 6e 63 72 65  plus an.** incre
407c0 6d 65 6e 74 20 28 68 65 72 65 20 63 61 6c 6c 65  ment (here calle
407d0 64 20 22 52 22 29 20 6f 66 20 61 62 6f 75 74 20  d "R") of about 
407e0 31 30 30 20 6f 72 20 32 30 30 2e 20 20 53 51 4c  100 or 200.  SQL
407f0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
40800 0a 2a 2a 20 65 78 74 72 61 20 52 20 62 79 74 65  .** extra R byte
40810 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 74  s on each page t
40820 6f 20 73 74 6f 72 65 20 6d 65 74 61 64 61 74 61  o store metadata
40830 20 61 62 6f 75 74 20 74 68 65 20 75 6e 64 65 72   about the under
40840 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73  lying.** databas
40850 65 20 70 61 67 65 20 6f 6e 20 64 69 73 6b 2e 20  e page on disk. 
40860 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 52 20   The value of R 
40870 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68  depends.** on th
40880 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  e SQLite version
40890 2c 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61  , the target pla
408a0 74 66 6f 72 6d 2c 20 61 6e 64 20 68 6f 77 20 53  tform, and how S
408b0 51 4c 69 74 65 20 77 61 73 20 63 6f 6d 70 69 6c  QLite was compil
408c0 65 64 2e 0a 2a 2a 20 52 20 69 73 20 63 6f 6e 73  ed..** R is cons
408d0 74 61 6e 74 20 66 6f 72 20 61 20 70 61 72 74 69  tant for a parti
408e0 63 75 6c 61 72 20 62 75 69 6c 64 20 6f 66 20 53  cular build of S
408f0 51 4c 69 74 65 2e 20 20 54 68 65 20 73 65 63 6f  QLite.  The seco
40900 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a  nd argument to.*
40910 2a 20 78 43 72 65 61 74 65 28 29 2c 20 62 50 75  * xCreate(), bPu
40920 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65  rgeable, is true
40930 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65   if the cache be
40940 69 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c  ing created will
40950 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63  .** be used to c
40960 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61  ache database pa
40970 67 65 73 20 6f 66 20 61 20 66 69 6c 65 20 73 74  ges of a file st
40980 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c 20 6f 72  ored on disk, or
40990 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20  .** false if it 
409a0 69 73 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69  is used for an i
409b0 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
409c0 65 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70  e. The cache imp
409d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 64  lementation.** d
409e0 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20  oes not have to 
409f0 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63  do anything spec
40a00 69 61 6c 20 62 61 73 65 64 20 77 69 74 68 20 74  ial based with t
40a10 68 65 20 76 61 6c 75 65 20 6f 66 20 62 50 75 72  he value of bPur
40a20 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 20 69 73  geable;.** it is
40a30 20 70 75 72 65 6c 79 20 61 64 76 69 73 6f 72 79   purely advisory
40a40 2e 20 20 4f 6e 20 61 20 63 61 63 68 65 20 77 68  .  On a cache wh
40a50 65 72 65 20 62 50 75 72 67 65 61 62 6c 65 20 69  ere bPurgeable i
40a60 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 74 65 20  s false, SQLite 
40a70 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 69 6e  will.** never in
40a80 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 20 65 78  voke xUnpin() ex
40a90 63 65 70 74 20 74 6f 20 64 65 6c 69 62 65 72 61  cept to delibera
40aa0 74 65 6c 79 20 64 65 6c 65 74 65 20 61 20 70 61  tely delete a pa
40ab0 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  ge..** In other 
40ac0 77 6f 72 64 73 2c 20 61 20 63 61 63 68 65 20 63  words, a cache c
40ad0 72 65 61 74 65 64 20 77 69 74 68 20 62 50 75 72  reated with bPur
40ae0 67 65 61 62 6c 65 20 73 65 74 20 74 6f 20 66 61  geable set to fa
40af0 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65  lse will.** neve
40b00 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79 20 75 6e  r contain any un
40b10 70 69 6e 6e 65 64 20 70 61 67 65 73 2e 0a 2a 2a  pinned pages..**
40b20 0a 2a 2a 20 54 68 65 20 78 43 61 63 68 65 73 69  .** The xCachesi
40b30 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20  ze() method may 
40b40 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79  be called at any
40b50 20 74 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20   time by SQLite 
40b60 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75  to set the.** su
40b70 67 67 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20  ggested maximum 
40b80 63 61 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62  cache-size (numb
40b90 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72  er of pages stor
40ba0 65 64 20 62 79 29 20 74 68 65 20 63 61 63 68 65  ed by) the cache
40bb0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73  .** instance pas
40bc0 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
40bd0 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20   argument. This 
40be0 69 73 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e  is the value con
40bf0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a  figured using.**
40c00 20 74 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52   the SQLite "[PR
40c10 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d  AGMA cache_size]
40c20 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69  " command. As wi
40c30 74 68 20 74 68 65 20 62 50 75 72 67 65 61 62 6c  th the bPurgeabl
40c40 65 20 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20  e parameter,.** 
40c50 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
40c60 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  on is not requir
40c70 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ed to do anythin
40c80 67 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 76  g with this.** v
40c90 61 6c 75 65 3b 20 69 74 20 69 73 20 61 64 76 69  alue; it is advi
40ca0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  sory only..**.**
40cb0 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 28   The xPagecount(
40cc0 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  ) method should 
40cd0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
40ce0 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65  r of pages curre
40cf0 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69  ntly.** stored i
40d00 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 20  n the cache..** 
40d10 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 68 28 29  .** The xFetch()
40d20 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20   method is used 
40d30 74 6f 20 66 65 74 63 68 20 61 20 70 61 67 65 20  to fetch a page 
40d40 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
40d50 6e 74 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20  nter to it. .** 
40d60 41 20 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69  A 'page', in thi
40d70 73 20 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20  s context, is a 
40d80 62 75 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65  buffer of szPage
40d90 20 62 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61   bytes aligned a
40da0 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62  t an.** 8-byte b
40db0 6f 75 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67  oundary. The pag
40dc0 65 20 74 6f 20 62 65 20 66 65 74 63 68 65 64 20  e to be fetched 
40dd0 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
40de0 20 74 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a   the key. The.**
40df0 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c   mimimum key val
40e00 75 65 20 69 73 20 31 2e 20 41 66 74 65 72 20 69  ue is 1. After i
40e10 74 20 68 61 73 20 62 65 65 6e 20 72 65 74 72 69  t has been retri
40e20 65 76 65 64 20 75 73 69 6e 67 20 78 46 65 74 63  eved using xFetc
40e30 68 2c 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20  h, the page .** 
40e40 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  is considered to
40e50 20 62 65 20 22 70 69 6e 6e 65 64 22 2e 0a 2a 2a   be "pinned"..**
40e60 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 65  .** If the reque
40e70 73 74 65 64 20 70 61 67 65 20 69 73 20 61 6c 72  sted page is alr
40e80 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65  eady in the page
40e90 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65   cache, then the
40ea0 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 69   page cache.** i
40eb0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
40ec0 73 74 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  st return a poin
40ed0 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
40ee0 62 75 66 66 65 72 20 77 69 74 68 20 69 74 73 20  buffer with its 
40ef0 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e 74 61 63  content.** intac
40f00 74 2e 20 20 49 66 20 74 68 65 20 72 65 71 75 65  t.  If the reque
40f10 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74  sted page is not
40f20 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
40f30 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 0a  cache, then the.
40f40 2a 2a 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74  ** behavior of t
40f50 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  he cache impleme
40f60 6e 74 61 74 69 6f 6e 20 69 73 20 64 65 74 65 72  ntation is deter
40f70 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76 61 6c  mined by the val
40f80 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 72 65  ue of the.** cre
40f90 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
40fa0 72 20 70 61 73 73 65 64 20 74 6f 20 78 46 65 74  r passed to xFet
40fb0 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ch, according to
40fc0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
40fd0 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 74 61 62  able:.**.** <tab
40fe0 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 69 64 74  le border=1 widt
40ff0 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 65 6e 74  h=85% align=cent
41000 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20  er>.** <tr><th> 
41010 63 72 65 61 74 65 46 6c 61 67 20 3c 74 68 3e 20  createFlag <th> 
41020 42 65 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70  Behaviour when p
41030 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
41040 64 79 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 3c  dy in cache.** <
41050 74 72 3e 3c 74 64 3e 20 30 20 3c 74 64 3e 20 44  tr><td> 0 <td> D
41060 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61  o not allocate a
41070 20 6e 65 77 20 70 61 67 65 2e 20 20 52 65 74 75   new page.  Retu
41080 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e  rn NULL..** <tr>
41090 3c 74 64 3e 20 31 20 3c 74 64 3e 20 41 6c 6c 6f  <td> 1 <td> Allo
410a0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
410b0 69 66 20 69 74 20 65 61 73 79 20 61 6e 64 20 63  if it easy and c
410c0 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20  onvenient to do 
410d0 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  so..**          
410e0 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65         Otherwise
410f0 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a   return NULL..**
41100 20 3c 74 72 3e 3c 74 64 3e 20 32 20 3c 74 64 3e   <tr><td> 2 <td>
41110 20 4d 61 6b 65 20 65 76 65 72 79 20 65 66 66 6f   Make every effo
41120 72 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  rt to allocate a
41130 20 6e 65 77 20 70 61 67 65 2e 20 20 4f 6e 6c 79   new page.  Only
41140 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
41150 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20             NULL 
41160 69 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20  if allocating a 
41170 6e 65 77 20 70 61 67 65 20 69 73 20 65 66 66 65  new page is effe
41180 63 74 69 76 65 6c 79 20 69 6d 70 6f 73 73 69 62  ctively impossib
41190 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a  le..** </table>.
411a0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
411b0 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 76 6f 6b  l normally invok
411c0 65 20 78 46 65 74 63 68 28 29 20 77 69 74 68 20  e xFetch() with 
411d0 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 20  a createFlag of 
411e0 30 20 6f 72 20 31 2e 20 20 49 66 0a 2a 2a 20 61  0 or 1.  If.** a
411f0 20 63 61 6c 6c 20 74 6f 20 78 46 65 74 63 68 28   call to xFetch(
41200 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61  ) with createFla
41210 67 3d 3d 31 20 72 65 74 75 72 6e 73 20 4e 55 4c  g==1 returns NUL
41220 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 77  L, then SQLite w
41230 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74  ill.** attempt t
41240 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f 72 20 6d  o unpin one or m
41250 6f 72 65 20 63 61 63 68 65 20 70 61 67 65 73 20  ore cache pages 
41260 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 68 65 20  by spilling the 
41270 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 70 69  content of.** pi
41280 6e 6e 65 64 20 70 61 67 65 73 20 74 6f 20 64 69  nned pages to di
41290 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 6e 67 20  sk and synching 
412a0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
412b0 73 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 2e  stem disk cache.
412c0 20 41 66 74 65 72 0a 2a 2a 20 61 74 74 65 6d 70   After.** attemp
412d0 74 69 6e 67 20 74 6f 20 75 6e 70 69 6e 20 70 61  ting to unpin pa
412e0 67 65 73 2c 20 74 68 65 20 78 46 65 74 63 68 28  ges, the xFetch(
412f0 29 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 62 65  ) method will be
41300 20 69 6e 76 6f 6b 65 64 20 61 67 61 69 6e 20 77   invoked again w
41310 69 74 68 0a 2a 2a 20 61 20 63 72 65 61 74 65 46  ith.** a createF
41320 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  lag of 2..**.** 
41330 78 55 6e 70 69 6e 28 29 20 69 73 20 63 61 6c 6c  xUnpin() is call
41340 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 74  ed by SQLite wit
41350 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  h a pointer to a
41360 20 63 75 72 72 65 6e 74 6c 79 20 70 69 6e 6e 65   currently pinne
41370 64 20 70 61 67 65 0a 2a 2a 20 61 73 20 69 74 73  d page.** as its
41380 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
41390 2e 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70  . If the third p
413a0 61 72 61 6d 65 74 65 72 2c 20 64 69 73 63 61 72  arameter, discar
413b0 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a  d, is non-zero,.
413c0 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  ** then the page
413d0 20 73 68 6f 75 6c 64 20 62 65 20 65 76 69 63 74   should be evict
413e0 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ed from the cach
413f0 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
41400 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73 73 75 6d  SQLite .** assum
41410 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  es that the next
41420 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20 69   time the page i
41430 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f 6d  s retrieved from
41440 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e 67   the cache using
41450 0a 2a 2a 20 74 68 65 20 78 46 65 74 63 68 28 29  .** the xFetch()
41460 20 6d 65 74 68 6f 64 2c 20 69 74 20 77 69 6c 6c   method, it will
41470 20 62 65 20 7a 65 72 6f 65 64 2e 20 49 66 20 74   be zeroed. If t
41480 68 65 20 64 69 73 63 61 72 64 20 70 61 72 61 6d  he discard param
41490 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 2c  eter is.** zero,
414a0 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 69   then the page i
414b0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  s considered to 
414c0 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65  be unpinned. The
414d0 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
414e0 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 63 68 6f  ation.** may cho
414f0 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75 6e 70  ose to evict unp
41500 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74 20 61  inned pages at a
41510 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  ny time..**.** T
41520 68 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20  he cache is not 
41530 72 65 71 75 69 72 65 64 20 74 6f 20 70 65 72 66  required to perf
41540 6f 72 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63  orm any referenc
41550 65 20 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69  e counting. A si
41560 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f  ngle .** call to
41570 20 78 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73   xUnpin() unpins
41580 20 74 68 65 20 70 61 67 65 20 72 65 67 61 72 64   the page regard
41590 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62  less of the numb
415a0 65 72 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c  er of prior call
415b0 73 20 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28  s .** to xFetch(
415c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65  )..**.** The xRe
415d0 6b 65 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20  key() method is 
415e0 75 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74  used to change t
415f0 68 65 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73  he key value ass
41600 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
41610 0a 2a 2a 20 70 61 67 65 20 70 61 73 73 65 64 20  .** page passed 
41620 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
41630 67 75 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b  gument from oldK
41640 65 79 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66  ey to newKey. If
41650 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72   the cache.** pr
41660 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e  eviously contain
41670 73 20 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63  s an entry assoc
41680 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65  iated with newKe
41690 79 2c 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a  y, it should be.
416a0 2a 2a 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e  ** discarded. An
416b0 79 20 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e  y prior cache en
416c0 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  try associated w
416d0 69 74 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75  ith newKey is gu
416e0 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20  aranteed not.** 
416f0 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a  to be pinned..**
41700 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20  .** When SQLite 
41710 63 61 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63  calls the xTrunc
41720 61 74 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68  ate() method, th
41730 65 20 63 61 63 68 65 20 6d 75 73 74 20 64 69 73  e cache must dis
41740 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73  card all.** exis
41750 74 69 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69  ting cache entri
41760 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  es with page num
41770 62 65 72 73 20 28 6b 65 79 73 29 20 67 72 65 61  bers (keys) grea
41780 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
41790 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75  l.** to the valu
417a0 65 20 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20  e of the iLimit 
417b0 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64  parameter passed
417c0 20 74 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e   to xTruncate().
417d0 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68   If any.** of th
417e0 65 73 65 20 70 61 67 65 73 20 61 72 65 20 70 69  ese pages are pi
417f0 6e 6e 65 64 2c 20 74 68 65 79 20 61 72 65 20 69  nned, they are i
41800 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e  mplicitly unpinn
41810 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ed, meaning that
41820 0a 2a 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20  .** they can be 
41830 73 61 66 65 6c 79 20 64 69 73 63 61 72 64 65 64  safely discarded
41840 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73  ..**.** The xDes
41850 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73  troy() method is
41860 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20   used to delete 
41870 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65  a cache allocate
41880 64 20 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a  d by xCreate()..
41890 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73  ** All resources
418a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
418b0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
418c0 61 63 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66  ache should be f
418d0 72 65 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63  reed. After.** c
418e0 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74  alling the xDest
418f0 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51  roy() method, SQ
41900 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74  Lite considers t
41910 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63  he [sqlite3_pcac
41920 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69  he*].** handle i
41930 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c  nvalid, and will
41940 20 6e 6f 74 20 75 73 65 20 69 74 20 77 69 74 68   not use it with
41950 20 61 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74   any other sqlit
41960 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
41970 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  s.** functions..
41980 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
41990 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
419a0 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
419b0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b  _pcache_methods;
419c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
419d0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b  pcache_methods {
419e0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
419f0 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f   int (*xInit)(vo
41a00 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  id*);.  void (*x
41a10 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29  Shutdown)(void*)
41a20 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  ;.  sqlite3_pcac
41a30 68 65 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69  he *(*xCreate)(i
41a40 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62  nt szPage, int b
41a50 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f  Purgeable);.  vo
41a60 69 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29  id (*xCachesize)
41a70 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
41a80 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65  , int nCachesize
41a90 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65  );.  int (*xPage
41aa0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70  count)(sqlite3_p
41ab0 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20  cache*);.  void 
41ac0 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74  *(*xFetch)(sqlit
41ad0 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69  e3_pcache*, unsi
41ae0 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72  gned key, int cr
41af0 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69  eateFlag);.  voi
41b00 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69  d (*xUnpin)(sqli
41b10 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69  te3_pcache*, voi
41b20 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29  d*, int discard)
41b30 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65  ;.  void (*xReke
41b40 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  y)(sqlite3_pcach
41b50 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67  e*, void*, unsig
41b60 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69  ned oldKey, unsi
41b70 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20  gned newKey);.  
41b80 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65  void (*xTruncate
41b90 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
41ba0 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d  *, unsigned iLim
41bb0 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44  it);.  void (*xD
41bc0 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
41bd0 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a  pcache*);.};../*
41be0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e  .** CAPI3REF: On
41bf0 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a 65  line Backup Obje
41c00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ct.** EXPERIMENT
41c10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  AL.**.** The sql
41c20 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
41c30 63 74 20 72 65 63 6f 72 64 73 20 73 74 61 74 65  ct records state
41c40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
41c50 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a 2a  ut an ongoing.**
41c60 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f   online backup o
41c70 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 73  peration.  The s
41c80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62  qlite3_backup ob
41c90 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 20  ject is created 
41ca0 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20  by.** a call to 
41cb0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  [sqlite3_backup_
41cc0 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64  init()] and is d
41cd0 65 73 74 72 6f 79 65 64 20 62 79 20 61 20 63 61  estroyed by a ca
41ce0 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
41cf0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
41d00 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c  )]..**.** See Al
41d10 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53  so: [Using the S
41d20 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63  QLite Online Bac
41d30 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 70 65  kup API].*/.type
41d40 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
41d50 65 33 5f 62 61 63 6b 75 70 20 73 71 6c 69 74 65  e3_backup sqlite
41d60 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a 2a 2a  3_backup;../*.**
41d70 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e   CAPI3REF: Onlin
41d80 65 20 42 61 63 6b 75 70 20 41 50 49 2e 0a 2a 2a  e Backup API..**
41d90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
41da0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20  .** This API is 
41db0 75 73 65 64 20 74 6f 20 6f 76 65 72 77 72 69 74  used to overwrit
41dc0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
41dd0 66 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 77  f one database w
41de0 69 74 68 20 74 68 61 74 0a 2a 2a 20 6f 66 20 61  ith that.** of a
41df0 6e 6f 74 68 65 72 2e 20 49 74 20 69 73 20 75 73  nother. It is us
41e00 65 66 75 6c 20 65 69 74 68 65 72 20 66 6f 72 20  eful either for 
41e10 63 72 65 61 74 69 6e 67 20 62 61 63 6b 75 70 73  creating backups
41e20 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 6f 72   of databases or
41e30 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 20  .** for copying 
41e40 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
41e50 73 65 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 70  ses to or from p
41e60 65 72 73 69 73 74 65 6e 74 20 66 69 6c 65 73 2e  ersistent files.
41e70 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f   .**.** See Also
41e80 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c  : [Using the SQL
41e90 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75  ite Online Backu
41ea0 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 45 78 63  p API].**.** Exc
41eb0 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 69 73  lusive access is
41ec0 20 72 65 71 75 69 72 65 64 20 74 6f 20 74 68 65   required to the
41ed0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
41ee0 61 62 61 73 65 20 66 6f 72 20 74 68 65 20 0a 2a  abase for the .*
41ef0 2a 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68  * duration of th
41f00 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 48 6f 77  e operation. How
41f10 65 76 65 72 20 74 68 65 20 73 6f 75 72 63 65 20  ever the source 
41f20 64 61 74 61 62 61 73 65 20 69 73 20 6f 6e 6c 79  database is only
41f30 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20  .** read-locked 
41f40 77 68 69 6c 65 20 69 74 20 69 73 20 61 63 74 75  while it is actu
41f50 61 6c 6c 79 20 62 65 69 6e 67 20 72 65 61 64 2c  ally being read,
41f60 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65   it is not locke
41f70 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 6f 75 73 6c  d.** continuousl
41f80 79 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65  y for the entire
41f90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 75 73   operation. Thus
41fa0 2c 20 74 68 65 20 62 61 63 6b 75 70 20 6d 61 79  , the backup may
41fb0 20 62 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64   be.** performed
41fc0 20 6f 6e 20 61 20 6c 69 76 65 20 64 61 74 61 62   on a live datab
41fd0 61 73 65 20 77 69 74 68 6f 75 74 20 70 72 65 76  ase without prev
41fe0 65 6e 74 69 6e 67 20 6f 74 68 65 72 20 75 73 65  enting other use
41ff0 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 72 69 74 69  rs from.** writi
42000 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
42010 73 65 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64  se for an extend
42020 65 64 20 70 65 72 69 6f 64 20 6f 66 20 74 69 6d  ed period of tim
42030 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 70 65 72  e..** .** To per
42040 66 6f 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70  form a backup op
42050 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a 20 20 20 3c  eration: .**   <
42060 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c  ol>.**     <li><
42070 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
42080 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20 63  _init()</b> is c
42090 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69 6e  alled once to in
420a0 69 74 69 61 6c 69 7a 65 20 74 68 65 0a 2a 2a 20  itialize the.** 
420b0 20 20 20 20 20 20 20 20 62 61 63 6b 75 70 2c 20          backup, 
420c0 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73  .**     <li><b>s
420d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
420e0 65 70 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c  ep()</b> is call
420f0 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  ed one or more t
42100 69 6d 65 73 20 74 6f 20 74 72 61 6e 73 66 65 72  imes to transfer
42110 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65   .**         the
42120 20 64 61 74 61 20 62 65 74 77 65 65 6e 20 74 68   data between th
42130 65 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 2c  e two databases,
42140 20 61 6e 64 20 66 69 6e 61 6c 6c 79 0a 2a 2a 20   and finally.** 
42150 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74      <li><b>sqlit
42160 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
42170 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64  ()</b> is called
42180 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20   to release all 
42190 72 65 73 6f 75 72 63 65 73 20 0a 2a 2a 20 20 20  resources .**   
421a0 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64        associated
421b0 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70   with the backup
421c0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 0a 2a 2a 20   operation. .** 
421d0 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 65 72 65    </ol>.** There
421e0 20 73 68 6f 75 6c 64 20 62 65 20 65 78 61 63 74   should be exact
421f0 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73  ly one call to s
42200 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42210 6e 69 73 68 28 29 20 66 6f 72 20 65 61 63 68 0a  nish() for each.
42220 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ** successful ca
42230 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
42240 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a  ckup_init()..**.
42250 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  ** <b>sqlite3_ba
42260 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 0a  ckup_init()</b>.
42270 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
42280 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 70 61  two arguments pa
42290 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33  ssed to [sqlite3
422a0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20  _backup_init()] 
422b0 61 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  are the database
422c0 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63  .** handle assoc
422d0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64  iated with the d
422e0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
422f0 61 73 65 20 61 6e 64 20 74 68 65 20 64 61 74 61  ase and the data
42300 62 61 73 65 20 6e 61 6d 65 20 0a 2a 2a 20 75 73  base name .** us
42310 65 64 20 74 6f 20 61 74 74 61 63 68 20 74 68 65  ed to attach the
42320 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
42330 61 62 61 73 65 20 74 6f 20 74 68 65 20 68 61 6e  abase to the han
42340 64 6c 65 2e 20 54 68 65 20 64 61 74 61 62 61 73  dle. The databas
42350 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 22 6d 61  e name.** is "ma
42360 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  in" for the main
42370 20 64 61 74 61 62 61 73 65 2c 20 22 74 65 6d 70   database, "temp
42380 22 20 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72  " for the tempor
42390 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72  ary database, or
423a0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 73 70 65  .** the name spe
423b0 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
423c0 66 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 73  f the [ATTACH] s
423d0 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20  tatement if the 
423e0 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 0a 2a  destination is.*
423f0 2a 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  * an attached da
42400 74 61 62 61 73 65 2e 20 54 68 65 20 74 68 69 72  tabase. The thir
42410 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67  d and fourth arg
42420 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f  uments passed to
42430 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63   .** sqlite3_bac
42440 6b 75 70 5f 69 6e 69 74 28 29 20 69 64 65 6e 74  kup_init() ident
42450 69 66 79 20 74 68 65 20 5b 64 61 74 61 62 61 73  ify the [databas
42460 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a  e connection].**
42470 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61   and database na
42480 6d 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 61 63  me used.** to ac
42490 63 65 73 73 20 74 68 65 20 73 6f 75 72 63 65 20  cess the source 
424a0 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 76 61  database. The va
424b0 6c 75 65 73 20 70 61 73 73 65 64 20 66 6f 72 20  lues passed for 
424c0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 0a  the source and .
424d0 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b  ** destination [
424e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
424f0 69 6f 6e 5d 20 70 61 72 61 6d 65 74 65 72 73 20  ion] parameters 
42500 6d 75 73 74 20 6e 6f 74 20 62 65 20 74 68 65 20  must not be the 
42510 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  same..**.** If a
42520 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
42530 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 62 61  ithin sqlite3_ba
42540 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 74 68 65  ckup_init(), the
42550 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  n NULL is return
42560 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  ed.** and an err
42570 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f  or code and erro
42580 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65  r message writte
42590 6e 20 69 6e 74 6f 20 74 68 65 20 5b 64 61 74 61  n into the [data
425a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
425b0 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74   .** passed as t
425c0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
425d0 74 2e 20 54 68 65 79 20 6d 61 79 20 62 65 20 72  t. They may be r
425e0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 74  etrieved using t
425f0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  he.** [sqlite3_e
42600 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69  rrcode()], [sqli
42610 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61  te3_errmsg()], a
42620 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  nd [sqlite3_errm
42630 73 67 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e  sg16()] function
42640 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  s..** Otherwise,
42650 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   if successful, 
42660 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
42670 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
42680 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 72 65   object is.** re
42690 74 75 72 6e 65 64 2e 20 54 68 69 73 20 70 6f 69  turned. This poi
426a0 6e 74 65 72 20 6d 61 79 20 62 65 20 75 73 65 64  nter may be used
426b0 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65   with the sqlite
426c0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
426d0 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  and.** sqlite3_b
426e0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66  ackup_finish() f
426f0 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70 65 72 66  unctions to perf
42700 6f 72 6d 20 74 68 65 20 73 70 65 63 69 66 69 65  orm the specifie
42710 64 20 62 61 63 6b 75 70 20 0a 2a 2a 20 6f 70 65  d backup .** ope
42720 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 3c 62  ration..**.** <b
42730 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  >sqlite3_backup_
42740 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  step()</b>.**.**
42750 20 46 75 6e 63 74 69 6f 6e 20 5b 73 71 6c 69 74   Function [sqlit
42760 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
42770 5d 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70  ] is used to cop
42780 79 20 75 70 20 74 6f 20 6e 50 61 67 65 20 70 61  y up to nPage pa
42790 67 65 73 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20  ges between .** 
427a0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64  the source and d
427b0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
427c0 61 73 65 73 2c 20 77 68 65 72 65 20 6e 50 61 67  ases, where nPag
427d0 65 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f  e is the value o
427e0 66 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64  f the .** second
427f0 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65   parameter passe
42800 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  d to sqlite3_bac
42810 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 6e  kup_step(). If n
42820 50 61 67 65 20 69 73 20 61 20 6e 65 67 61 74 69  Page is a negati
42830 76 65 0a 2a 2a 20 76 61 6c 75 65 2c 20 61 6c 6c  ve.** value, all
42840 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 63   remaining sourc
42850 65 20 70 61 67 65 73 20 61 72 65 20 63 6f 70 69  e pages are copi
42860 65 64 2e 20 49 66 20 74 68 65 20 72 65 71 75 69  ed. If the requi
42870 72 65 64 20 70 61 67 65 73 20 61 72 65 20 0a 2a  red pages are .*
42880 2a 20 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f  * succesfully co
42890 70 69 65 64 2c 20 62 75 74 20 74 68 65 72 65 20  pied, but there 
428a0 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70  are still more p
428b0 61 67 65 73 20 74 6f 20 63 6f 70 79 20 62 65 66  ages to copy bef
428c0 6f 72 65 20 74 68 65 20 0a 2a 2a 20 62 61 63 6b  ore the .** back
428d0 75 70 20 69 73 20 63 6f 6d 70 6c 65 74 65 2c 20  up is complete, 
428e0 69 74 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  it returns [SQLI
428f0 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e 6f 20 65 72  TE_OK]. If no er
42900 72 6f 72 20 6f 63 63 75 72 65 64 20 61 6e 64 20  ror occured and 
42910 74 68 65 72 65 20 0a 2a 2a 20 61 72 65 20 6e 6f  there .** are no
42920 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63   more pages to c
42930 6f 70 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  opy, then [SQLIT
42940 45 5f 44 4f 4e 45 5d 20 69 73 20 72 65 74 75 72  E_DONE] is retur
42950 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
42960 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 74 68 65   .** occurs, the
42970 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  n an SQLite erro
42980 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
42990 65 64 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 5b  ed. As well as [
429a0 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a  SQLITE_OK] and.*
429b0 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c  * [SQLITE_DONE],
429c0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
429d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
429e0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c   may return [SQL
429f0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a  ITE_READONLY],.*
42a00 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
42a10 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  , [SQLITE_BUSY],
42a20 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d   [SQLITE_LOCKED]
42a30 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  , or an.** [SQLI
42a40 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
42a50 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
42a60 58 58 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72  XX] extended err
42a70 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41  or code..**.** A
42a80 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 63 61  s well as the ca
42a90 73 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73  se where the des
42aa0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
42ab0 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65  e file was opene
42ac0 64 20 66 6f 72 0a 2a 2a 20 72 65 61 64 2d 6f 6e  d for.** read-on
42ad0 6c 79 20 61 63 63 65 73 73 2c 20 73 71 6c 69 74  ly access, sqlit
42ae0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
42af0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c   may return [SQL
42b00 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 66  ITE_READONLY] if
42b10 0a 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74  .** the destinat
42b20 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d  ion is an in-mem
42b30 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 74  ory database wit
42b40 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 61  h a different pa
42b50 67 65 20 73 69 7a 65 0a 2a 2a 20 66 72 6f 6d 20  ge size.** from 
42b60 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
42b70 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71  ase..**.** If sq
42b80 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42b90 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69  p() cannot obtai
42ba0 6e 20 61 20 72 65 71 75 69 72 65 64 20 66 69 6c  n a required fil
42bb0 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b 2c 20 74  e-system lock, t
42bc0 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  hen.** the [sqli
42bd0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
42be0 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20   | busy-handler 
42bf0 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20  function].** is 
42c00 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f 6e 65 20  invoked (if one 
42c10 69 73 20 73 70 65 63 69 66 69 65 64 29 2e 20 49  is specified). I
42c20 66 20 74 68 65 20 0a 2a 2a 20 62 75 73 79 2d 68  f the .** busy-h
42c30 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 6e  andler returns n
42c40 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72 65 20 74  on-zero before t
42c50 68 65 20 6c 6f 63 6b 20 69 73 20 61 76 61 69 6c  he lock is avail
42c60 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b  able, then .** [
42c70 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 69 73 20  SQLITE_BUSY] is 
42c80 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
42c90 63 61 6c 6c 65 72 2e 20 49 6e 20 74 68 69 73 20  caller. In this 
42ca0 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  case the call to
42cb0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
42cc0 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65  up_step() can be
42cd0 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20   retried later. 
42ce0 49 66 20 74 68 65 20 73 6f 75 72 63 65 0a 2a 2a  If the source.**
42cf0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
42d00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 62 65 69  ction].** is bei
42d10 6e 67 20 75 73 65 64 20 74 6f 20 77 72 69 74 65  ng used to write
42d20 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
42d30 61 74 61 62 61 73 65 20 77 68 65 6e 20 73 71 6c  atabase when sql
42d40 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42d50 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c  ().** is called,
42d60 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f   then [SQLITE_LO
42d70 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65  CKED] is returne
42d80 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 41  d immediately. A
42d90 67 61 69 6e 2c 20 69 6e 20 74 68 69 73 0a 2a 2a  gain, in this.**
42da0 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74   case the call t
42db0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
42dc0 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72  _step() can be r
42dd0 65 74 72 69 65 64 20 6c 61 74 65 72 20 6f 6e 2e  etried later on.
42de0 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49   If.** [SQLITE_I
42df0 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51  OERR_ACCESS | SQ
42e00 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 2c  LITE_IOERR_XXX],
42e10 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c   [SQLITE_NOMEM],
42e20 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52   or.** [SQLITE_R
42e30 45 41 44 4f 4e 4c 59 5d 20 69 73 20 72 65 74 75  EADONLY] is retu
42e40 72 6e 65 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 74  rned, then .** t
42e50 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74  here is no point
42e60 20 69 6e 20 72 65 74 72 79 69 6e 67 20 74 68 65   in retrying the
42e70 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
42e80 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
42e90 54 68 65 73 65 20 0a 2a 2a 20 65 72 72 6f 72 73  These .** errors
42ea0 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
42eb0 66 61 74 61 6c 2e 20 41 74 20 74 68 69 73 20 70  fatal. At this p
42ec0 6f 69 6e 74 20 74 68 65 20 61 70 70 6c 69 63 61  oint the applica
42ed0 74 69 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74  tion must accept
42ee0 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61   .** that the ba
42ef0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68  ckup operation h
42f00 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61  as failed and pa
42f10 73 73 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  ss the backup op
42f20 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a  eration handle .
42f30 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ** to the sqlite
42f40 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
42f50 29 20 74 6f 20 72 65 6c 65 61 73 65 20 61 73 73  ) to release ass
42f60 6f 63 69 61 74 65 64 20 72 65 73 6f 75 72 63 65  ociated resource
42f70 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c 6f 77 69  s..**.** Followi
42f80 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  ng the first cal
42f90 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
42fa0 6b 75 70 5f 73 74 65 70 28 29 2c 20 61 6e 20 65  kup_step(), an e
42fb0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73  xclusive lock is
42fc0 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  .** obtained on 
42fd0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
42fe0 66 69 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20  file. It is not 
42ff0 72 65 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65  released until e
43000 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65  ither .** sqlite
43010 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
43020 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74  ) is called or t
43030 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
43040 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  ion is complete 
43050 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  .** and sqlite3_
43060 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65  backup_step() re
43070 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f  turns [SQLITE_DO
43080 4e 45 5d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c  NE]. Additionall
43090 79 2c 20 65 61 63 68 20 74 69 6d 65 20 0a 2a 2a  y, each time .**
430a0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
430b0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
430c0 20 69 73 20 6d 61 64 65 20 61 20 5b 73 68 61 72   is made a [shar
430d0 65 64 20 6c 6f 63 6b 5d 20 69 73 20 6f 62 74 61  ed lock] is obta
430e0 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 73  ined on.** the s
430f0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66  ource database f
43100 69 6c 65 2e 20 54 68 69 73 20 6c 6f 63 6b 20 69  ile. This lock i
43110 73 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72  s released befor
43120 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  e the.** sqlite3
43130 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63  _backup_step() c
43140 61 6c 6c 20 72 65 74 75 72 6e 73 2e 20 42 65 63  all returns. Bec
43150 61 75 73 65 20 74 68 65 20 73 6f 75 72 63 65 20  ause the source 
43160 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a  database is not.
43170 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65  ** locked betwee
43180 6e 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  n calls to sqlit
43190 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
431a0 2c 20 69 74 20 6d 61 79 20 62 65 20 6d 6f 64 69  , it may be modi
431b0 66 69 65 64 20 6d 69 64 2d 77 61 79 0a 2a 2a 20  fied mid-way.** 
431c0 74 68 72 6f 75 67 68 20 74 68 65 20 62 61 63 6b  through the back
431d0 75 70 20 70 72 6f 63 65 64 75 72 65 2e 20 49 66  up procedure. If
431e0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
431f0 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64  base is modified
43200 20 62 79 20 61 6e 0a 2a 2a 20 65 78 74 65 72 6e   by an.** extern
43210 61 6c 20 70 72 6f 63 65 73 73 20 6f 72 20 76 69  al process or vi
43220 61 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  a a database con
43230 6e 65 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68  nection other th
43240 61 6e 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67  an the one being
43250 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20  .** used by the 
43260 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
43270 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75  , then the backu
43280 70 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 70  p will be transp
43290 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 65 73 74 61  arently.** resta
432a0 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 78 74  rted by the next
432b0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
432c0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
432d0 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 0a 2a  If the source .*
432e0 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f  * database is mo
432f0 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 73  dified by the us
43300 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
43310 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
43320 20 61 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 62   as is used.** b
43330 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  y the backup ope
43340 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ration, then the
43350 20 62 61 63 6b 75 70 20 64 61 74 61 62 61 73 65   backup database
43360 20 69 73 20 74 72 61 6e 73 70 61 72 65 6e 74 6c   is transparentl
43370 79 20 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 74  y .** updated at
43380 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
43390 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33  **.** <b>sqlite3
433a0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
433b0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  </b>.**.** Once 
433c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
433d0 74 65 70 28 29 20 68 61 73 20 72 65 74 75 72 6e  tep() has return
433e0 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  ed [SQLITE_DONE]
433f0 2c 20 6f 72 20 77 68 65 6e 20 74 68 65 20 0a 2a  , or when the .*
43400 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69  * application wi
43410 73 68 65 73 20 74 6f 20 61 62 61 6e 64 6f 6e 20  shes to abandon 
43420 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
43430 74 69 6f 6e 2c 20 74 68 65 20 5b 73 71 6c 69 74  tion, the [sqlit
43440 65 33 5f 62 61 63 6b 75 70 5d 0a 2a 2a 20 6f 62  e3_backup].** ob
43450 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 70  ject should be p
43460 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
43470 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
43480 2e 20 54 68 69 73 20 72 65 6c 65 61 73 65 73 20  . This releases 
43490 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73  all.** resources
434a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
434b0 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
434c0 61 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65  ation. If sqlite
434d0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a  3_backup_step().
434e0 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65 74 20 72  ** has not yet r
434f0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
43500 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 61 6e 79 20  DONE], then any 
43510 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 72 61  active write-tra
43520 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 0a  nsaction on the.
43530 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  ** destination d
43540 61 74 61 62 61 73 65 20 69 73 20 72 6f 6c 6c 65  atabase is rolle
43550 64 20 62 61 63 6b 2e 20 54 68 65 20 5b 73 71 6c  d back. The [sql
43560 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a  ite3_backup] obj
43570 65 63 74 20 69 73 20 69 6e 76 61 6c 69 64 0a 2a  ect is invalid.*
43580 2a 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65  * and may not be
43590 20 75 73 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20   used following 
435a0 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
435b0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
435c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  )..**.** The val
435d0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
435e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
435f0 6e 69 73 68 20 69 73 20 5b 53 51 4c 49 54 45 5f  nish is [SQLITE_
43600 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 72 6f 72 0a  OK] if no error.
43610 2a 2a 20 6f 63 63 75 72 72 65 64 2c 20 72 65 67  ** occurred, reg
43620 61 72 64 6c 65 73 73 20 6f 72 20 77 68 65 74 68  ardless or wheth
43630 65 72 20 6f 72 20 6e 6f 74 20 73 71 6c 69 74 65  er or not sqlite
43640 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43650 77 61 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20  was called.** a 
43660 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65  sufficient numbe
43670 72 20 6f 66 20 74 69 6d 65 73 20 74 6f 20 63 6f  r of times to co
43680 6d 70 6c 65 74 65 20 74 68 65 20 62 61 63 6b 75  mplete the backu
43690 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4f 72 2c  p operation. Or,
436a0 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66   if.** an out-of
436b0 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f  -memory conditio
436c0 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63  n or IO error oc
436d0 63 75 72 65 64 20 64 75 72 69 6e 67 20 61 20 63  cured during a c
436e0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
436f0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43700 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  then [SQLITE_NOM
43710 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51  EM] or an.** [SQ
43720 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
43730 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  S | SQLITE_IOERR
43740 5f 58 58 58 5d 20 65 72 72 6f 72 20 63 6f 64 65  _XXX] error code
43750 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
43760 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
43770 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  e error code and
43780 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
43790 65 20 61 72 65 0a 2a 2a 20 77 72 69 74 74 65 6e  e are.** written
437a0 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74   to the destinat
437b0 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ion [database co
437c0 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  nnection]..**.**
437d0 20 41 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51   A return of [SQ
437e0 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53  LITE_BUSY] or [S
437f0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 66 72  QLITE_LOCKED] fr
43800 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  om sqlite3_backu
43810 70 5f 73 74 65 70 28 29 20 69 73 0a 2a 2a 20 6e  p_step() is.** n
43820 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 65  ot a permanent e
43830 72 72 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f  rror and does no
43840 74 20 61 66 66 65 63 74 20 74 68 65 20 72 65 74  t affect the ret
43850 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20  urn value of.** 
43860 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
43870 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c  inish()..**.** <
43880 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
43890 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 73 71  _remaining(), sq
438a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67  lite3_backup_pag
438b0 65 63 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a  ecount()</b>.**.
438c0 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  ** Each call to 
438d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
438e0 74 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76  tep() sets two v
438f0 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 74  alues stored int
43900 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 61 6e  ernally.** by an
43910 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   [sqlite3_backup
43920 5d 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 75  ] object. The nu
43930 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74  mber of pages st
43940 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64  ill to be backed
43950 0a 2a 2a 20 75 70 2c 20 77 68 69 63 68 20 6d 61  .** up, which ma
43960 79 20 62 65 20 71 75 65 72 69 65 64 20 62 79 20  y be queried by 
43970 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72  sqlite3_backup_r
43980 65 6d 61 69 6e 69 6e 67 28 29 2c 20 61 6e 64 20  emaining(), and 
43990 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 6e 75 6d  the total.** num
439a0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
439b0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
439c0 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20  ase file, which 
439d0 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 20 62  may be queried b
439e0 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  y.** sqlite3_bac
439f0 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e  kup_pagecount().
43a00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
43a10 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  s returned by th
43a20 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  ese functions ar
43a30 65 20 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 62  e only updated b
43a40 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  y.** sqlite3_bac
43a50 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74  kup_step(). If t
43a60 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
43a70 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 64  se is modified d
43a80 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a  uring a backup.*
43a90 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65  * operation, the
43aa0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65  n the values are
43ab0 20 6e 6f 74 20 75 70 64 61 74 65 64 20 74 6f 20   not updated to 
43ac0 61 63 63 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20  account for any 
43ad0 65 78 74 72 61 0a 2a 2a 20 70 61 67 65 73 20 74  extra.** pages t
43ae0 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 75  hat need to be u
43af0 70 64 61 74 65 64 20 6f 72 20 74 68 65 20 73 69  pdated or the si
43b00 7a 65 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  ze of the source
43b10 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
43b20 2a 20 63 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a  * changing..**.*
43b30 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20  * <b>Concurrent 
43b40 55 73 61 67 65 20 6f 66 20 44 61 74 61 62 61 73  Usage of Databas
43b50 65 20 48 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a  e Handles</b>.**
43b60 0a 2a 2a 20 54 68 65 20 73 6f 75 72 63 65 20 5b  .** The source [
43b70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
43b80 69 6f 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64  ion] may be used
43b90 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
43ba0 69 6f 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a  ion for other.**
43bb0 20 70 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20   purposes while 
43bc0 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  a backup operati
43bd0 6f 6e 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f  on is underway o
43be0 72 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69  r being initiali
43bf0 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 69 74  zed..** If SQLit
43c00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e  e is compiled an
43c10 64 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  d configured to 
43c20 73 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61  support threadsa
43c30 66 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  fe database.** c
43c40 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e  onnections, then
43c50 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
43c60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
43c70 6d 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63  may be used conc
43c80 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d  urrently.** from
43c90 20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68   within other th
43ca0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77  reads..**.** How
43cb0 65 76 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63  ever, the applic
43cc0 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61  ation must guara
43cd0 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 64 65  ntee that the de
43ce0 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
43cf0 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
43d00 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 70   handle is not p
43d10 61 73 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68  assed to any oth
43d20 65 72 20 41 50 49 20 28 62 79 20 61 6e 79 20 74  er API (by any t
43d30 68 72 65 61 64 29 20 61 66 74 65 72 20 0a 2a 2a  hread) after .**
43d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
43d50 69 6e 69 74 28 29 20 69 73 20 63 61 6c 6c 65 64  init() is called
43d60 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20   and before the 
43d70 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 61  corresponding ca
43d80 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
43d90 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
43da0 2e 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79 20  . Unfortunately 
43db0 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20  SQLite does not 
43dc0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 0a  currently check.
43dd0 2a 2a 20 66 6f 72 20 74 68 69 73 2c 20 69 66 20  ** for this, if 
43de0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
43df0 64 6f 65 73 20 75 73 65 20 74 68 65 20 64 65 73  does use the des
43e00 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61  tination [databa
43e10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a  se connection].*
43e20 2a 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  * for some other
43e30 20 70 75 72 70 6f 73 65 20 64 75 72 69 6e 67 20   purpose during 
43e40 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  a backup operati
43e50 6f 6e 2c 20 74 68 69 6e 67 73 20 6d 61 79 20 61  on, things may a
43e60 70 70 65 61 72 20 74 6f 0a 2a 2a 20 77 6f 72 6b  ppear to.** work
43e70 20 63 6f 72 72 65 63 74 6c 79 20 62 75 74 20 69   correctly but i
43e80 6e 20 66 61 63 74 20 62 65 20 73 75 62 74 6c 79  n fact be subtly
43e90 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 69 6e 67 2e   malfunctioning.
43ea0 20 20 55 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20    Use of the.** 
43eb0 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
43ec0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
43ed0 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69  while a backup i
43ee0 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69  s in progress mi
43ef0 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 63 61 75 73  ght.** also caus
43f00 65 20 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f  e a mutex deadlo
43f10 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65  ck..**.** Furthe
43f20 72 6d 6f 72 65 2c 20 69 66 20 72 75 6e 6e 69 6e  rmore, if runnin
43f30 67 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63  g in [shared cac
43f40 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70  he mode], the ap
43f50 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 0a 2a  plication must.*
43f60 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  * guarantee that
43f70 20 74 68 65 20 73 68 61 72 65 64 20 63 61 63 68   the shared cach
43f80 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64 65  e used by the de
43f90 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
43fa0 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 63 63  se.** is not acc
43fb0 65 73 73 65 64 20 77 68 69 6c 65 20 74 68 65 20  essed while the 
43fc0 62 61 63 6b 75 70 20 69 73 20 72 75 6e 6e 69 6e  backup is runnin
43fd0 67 2e 20 49 6e 20 70 72 61 63 74 69 63 65 20 74  g. In practice t
43fe0 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61  his means.** tha
43ff0 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  t the applicatio
44000 6e 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65  n must guarantee
44010 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73   that the file-s
44020 79 73 74 65 6d 20 66 69 6c 65 20 62 65 69 6e 67  ystem file being
44030 20 0a 2a 2a 20 62 61 63 6b 65 64 20 75 70 20 74   .** backed up t
44040 6f 20 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65  o is not accesse
44050 64 20 62 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74  d by any connect
44060 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20 70  ion within the p
44070 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a  rocess,.** not j
44080 75 73 74 20 74 68 65 20 73 70 65 63 69 66 69 63  ust the specific
44090 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
440a0 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 73   was passed to s
440b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
440c0 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  it()..**.** The 
440d0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
440e0 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69   object itself i
440f0 73 20 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65  s partially thre
44100 61 64 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65  adsafe. Multiple
44110 20 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 61 79   .** threads may
44120 20 73 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75 6c   safely make mul
44130 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e 74  tiple concurrent
44140 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
44150 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
44160 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
44170 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
44180 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20  remaining() and 
44190 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70  sqlite3_backup_p
441a0 61 67 65 63 6f 75 6e 74 28 29 0a 2a 2a 20 41 50  agecount().** AP
441b0 49 73 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63  Is are not stric
441c0 74 6c 79 20 73 70 65 61 6b 69 6e 67 20 74 68 72  tly speaking thr
441d0 65 61 64 73 61 66 65 2e 20 49 66 20 74 68 65 79  eadsafe. If they
441e0 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 61 74 20   are invoked at 
441f0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65  the.** same time
44200 20 61 73 20 61 6e 6f 74 68 65 72 20 74 68 72 65   as another thre
44210 61 64 20 69 73 20 69 6e 76 6f 6b 69 6e 67 20 73  ad is invoking s
44220 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
44230 65 70 28 29 20 69 74 20 69 73 0a 2a 2a 20 70 6f  ep() it is.** po
44240 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 79  ssible that they
44250 20 72 65 74 75 72 6e 20 69 6e 76 61 6c 69 64 20   return invalid 
44260 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  values..*/.SQLIT
44270 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61  E_API sqlite3_ba
44280 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61  ckup *sqlite3_ba
44290 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c  ckup_init(.  sql
442a0 69 74 65 33 20 2a 70 44 65 73 74 2c 20 20 20 20  ite3 *pDest,    
442b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442c0 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
442d0 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  on database hand
442e0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
442f0 61 72 20 2a 7a 44 65 73 74 4e 61 6d 65 2c 20 20  ar *zDestName,  
44300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44310 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  * Destination da
44320 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
44330 20 73 71 6c 69 74 65 33 20 2a 70 53 6f 75 72 63   sqlite3 *pSourc
44340 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
44350 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
44360 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
44370 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
44380 72 20 2a 7a 53 6f 75 72 63 65 4e 61 6d 65 20 20  r *zSourceName  
44390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
443a0 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   Source database
443b0 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49   name */.);.SQLI
443c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
443d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73  e3_backup_step(s
443e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
443f0 2c 20 69 6e 74 20 6e 50 61 67 65 29 3b 0a 53 51  , int nPage);.SQ
44400 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
44410 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
44420 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  sh(sqlite3_backu
44430 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50  p *p);.SQLITE_AP
44440 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
44450 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73  ckup_remaining(s
44460 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
44470 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
44480 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
44490 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74  _pagecount(sqlit
444a0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 0a  e3_backup *p);..
444b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
444c0 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74  Unlock Notificat
444d0 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ion.** EXPERIMEN
444e0 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 72  TAL.**.** When r
444f0 75 6e 6e 69 6e 67 20 69 6e 20 73 68 61 72 65 64  unning in shared
44500 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 64  -cache mode, a d
44510 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f  atabase operatio
44520 6e 20 6d 61 79 20 66 61 69 6c 20 77 69 74 68 0a  n may fail with.
44530 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f  ** an [SQLITE_LO
44540 43 4b 45 44 5d 20 65 72 72 6f 72 20 69 66 20 74  CKED] error if t
44550 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b  he required lock
44560 73 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 2d  s on the shared-
44570 63 61 63 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69  cache or.** indi
44580 76 69 64 75 61 6c 20 74 61 62 6c 65 73 20 77 69  vidual tables wi
44590 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 2d  thin the shared-
445a0 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 20  cache cannot be 
445b0 6f 62 74 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a  obtained. See.**
445c0 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d   [SQLite Shared-
445d0 43 61 63 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20  Cache Mode] for 
445e0 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
445f0 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f   shared-cache lo
44600 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 68 69 73 20  cking. .** This 
44610 41 50 49 20 6d 61 79 20 62 65 20 75 73 65 64 20  API may be used 
44620 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 63 61  to register a ca
44630 6c 6c 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69  llback that SQLi
44640 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a  te will invoke .
44650 2a 2a 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e  ** when the conn
44660 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  ection currently
44670 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71   holding the req
44680 75 69 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e  uired lock relin
44690 71 75 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 54  quishes it..** T
446a0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20  his API is only 
446b0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
446c0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
446d0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
446e0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
446f0 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20  _UNLOCK_NOTIFY] 
44700 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73  C-preprocessor s
44710 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a  ymbol defined..*
44720 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b  *.** See Also: [
44730 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65  Using the SQLite
44740 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61   Unlock Notifica
44750 74 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a  tion Feature]..*
44760 2a 0a 2a 2a 20 53 68 61 72 65 64 2d 63 61 63 68  *.** Shared-cach
44770 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65  e locks are rele
44780 61 73 65 64 20 77 68 65 6e 20 61 20 64 61 74 61  ased when a data
44790 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
447a0 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73  concludes.** its
447b0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
447c0 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20  tion, either by 
447d0 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72  committing it or
447e0 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b   rolling it back
447f0 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  . .**.** When a 
44800 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f 77  connection (know
44810 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 64  n as the blocked
44820 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 69   connection) fai
44830 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a  ls to obtain a.*
44840 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c  * shared-cache l
44850 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c  ock and SQLITE_L
44860 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65  OCKED is returne
44870 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2c  d to the caller,
44880 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 79   the.** identity
44890 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
448a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 65   connection (the
448b0 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
448c0 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 61  tion) that.** ha
448d0 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 71  s locked the req
448e0 75 69 72 65 64 20 72 65 73 6f 75 72 63 65 20 69  uired resource i
448f0 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61  s stored interna
44900 6c 6c 79 2e 20 41 66 74 65 72 20 61 6e 20 0a 2a  lly. After an .*
44910 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 72 65  * application re
44920 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45  ceives an SQLITE
44930 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 2c 20 69  _LOCKED error, i
44940 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 65 0a 2a  t may call the.*
44950 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  * sqlite3_unlock
44960 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64  _notify() method
44970 20 77 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65   with the blocke
44980 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  d connection han
44990 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 65 20 66  dle as .** the f
449a0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
449b0 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 61 20   register for a 
449c0 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69  callback that wi
449d0 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ll be invoked.**
449e0 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69   when the blocki
449f0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  ng connections c
44a00 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
44a10 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e  on is concluded.
44a20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b   The.** callback
44a30 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d   is invoked from
44a40 20 77 69 74 68 69 6e 20 74 68 65 20 5b 73 71 6c   within the [sql
44a50 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 20 5b 73  ite3_step] or [s
44a60 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d 0a 2a 2a  qlite3_close].**
44a70 20 63 61 6c 6c 20 74 68 61 74 20 63 6f 6e 63 6c   call that concl
44a80 75 64 65 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e  udes the blockin
44a90 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72  g connections tr
44aa0 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
44ab0 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f   If sqlite3_unlo
44ac0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63  ck_notify() is c
44ad0 61 6c 6c 65 64 20 69 6e 20 61 20 6d 75 6c 74 69  alled in a multi
44ae0 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
44af0 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 72 65 20  ation,.** there 
44b00 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74  is a chance that
44b10 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f   the blocking co
44b20 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61  nnection will ha
44b30 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 6f  ve already.** co
44b40 6e 63 6c 75 64 65 64 20 69 74 73 20 74 72 61 6e  ncluded its tran
44b50 73 61 63 74 69 6f 6e 20 62 79 20 74 68 65 20 74  saction by the t
44b60 69 6d 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  ime sqlite3_unlo
44b70 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 69  ck_notify() is i
44b80 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68  nvoked..** If th
44b90 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e  is happens, then
44ba0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
44bb0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
44bc0 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a  ed immediately,.
44bd0 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  ** from within t
44be0 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
44bf0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
44c00 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ()..**.** If the
44c10 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
44c20 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e  ion is attemptin
44c30 67 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72  g to obtain a wr
44c40 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0a 2a 2a  ite-lock on a.**
44c50 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74 61   shared-cache ta
44c60 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74 68  ble, and more th
44c70 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f 6e  an one other con
44c80 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  nection currentl
44c90 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 72 65 61  y holds.** a rea
44ca0 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 61  d-lock on the sa
44cb0 6d 65 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 53  me table, then S
44cc0 51 4c 69 74 65 20 61 72 62 69 74 72 61 72 69 6c  QLite arbitraril
44cd0 79 20 73 65 6c 65 63 74 73 20 6f 6e 65 20 6f 66  y selects one of
44ce0 20 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 63   .** the other c
44cf0 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 75 73  onnections to us
44d00 65 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e  e as the blockin
44d10 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  g connection..**
44d20 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 62 65  .** There may be
44d30 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 75 6e 6c   at most one unl
44d40 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44d50 61 63 6b 20 72 65 67 69 73 74 65 72 65 64 20 62  ack registered b
44d60 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20  y a .** blocked 
44d70 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 73  connection. If s
44d80 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44d90 74 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64  tify() is called
44da0 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 62 6c 6f   when the.** blo
44db0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  cked connection 
44dc0 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65  already has a re
44dd0 67 69 73 74 65 72 65 64 20 75 6e 6c 6f 63 6b 2d  gistered unlock-
44de0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c  notify callback,
44df0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 65 77  .** then the new
44e00 20 63 61 6c 6c 62 61 63 6b 20 72 65 70 6c 61 63   callback replac
44e10 65 73 20 74 68 65 20 6f 6c 64 2e 20 49 66 20 73  es the old. If s
44e20 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44e30 74 69 66 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c  tify() is.** cal
44e40 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
44e50 70 6f 69 6e 74 65 72 20 61 73 20 69 74 73 20 73  pointer as its s
44e60 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20  econd argument, 
44e70 74 68 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e  then any existin
44e80 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  g.** unlock-noti
44e90 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63  fy callback is c
44ea0 61 6e 63 65 6c 6c 65 64 2e 20 54 68 65 20 62 6c  ancelled. The bl
44eb0 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
44ec0 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  s .** unlock-not
44ed0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  ify callback may
44ee0 20 61 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65   also be cancele
44ef0 64 20 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65  d by closing the
44f00 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e   blocked.** conn
44f10 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71  ection using [sq
44f20 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a  lite3_close()]..
44f30 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b  **.** The unlock
44f40 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
44f50 20 69 73 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e   is not reentran
44f60 74 2e 20 49 66 20 61 6e 20 61 70 70 6c 69 63 61  t. If an applica
44f70 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20  tion invokes.** 
44f80 61 6e 79 20 73 71 6c 69 74 65 33 5f 78 78 78 20  any sqlite3_xxx 
44f90 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 66 72  API functions fr
44fa0 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c  om within an unl
44fb0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44fc0 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20  ack, a.** crash 
44fd0 6f 72 20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20  or deadlock may 
44fe0 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  be the result..*
44ff0 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 64 65 61 64  *.** Unless dead
45000 6c 6f 63 6b 20 69 73 20 64 65 74 65 63 74 65 64  lock is detected
45010 20 28 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71   (see below), sq
45020 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45030 69 66 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20  ify() always.** 
45040 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
45050 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c  K..**.** <b>Call
45060 62 61 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20  back Invocation 
45070 44 65 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a  Details</b>.**.*
45080 2a 20 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b  * When an unlock
45090 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
450a0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c 20   is registered, 
450b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
450c0 70 72 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73  provides a .** s
450d0 69 6e 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e  ingle void* poin
450e0 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73  ter that is pass
450f0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ed to the callba
45100 63 6b 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e  ck when it is in
45110 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65  voked..** Howeve
45120 72 2c 20 74 68 65 20 73 69 67 6e 61 74 75 72 65  r, the signature
45130 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   of the callback
45140 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73   function allows
45150 20 53 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a   SQLite to pass.
45160 2a 2a 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f  ** it an array o
45170 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20  f void* context 
45180 70 6f 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69  pointers. The fi
45190 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73  rst argument pas
451a0 73 65 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c  sed to.** an unl
451b0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
451c0 61 63 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ack is a pointer
451d0 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
451e0 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a  void* pointers,.
451f0 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ** and the secon
45200 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d is the number 
45210 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
45220 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57  e array..**.** W
45230 68 65 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63  hen a blocking c
45240 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73  onnections trans
45250 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75  action is conclu
45260 64 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62  ded, there may b
45270 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  e.** more than o
45280 6e 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  ne blocked conne
45290 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72  ction that has r
452a0 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e  egistered for an
452b0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a   unlock-notify.*
452c0 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 49 66 20 74  * callback. If t
452d0 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20  wo or more such 
452e0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
452f0 6f 6e 73 20 68 61 76 65 20 73 70 65 63 69 66 69  ons have specifi
45300 65 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63  ed the.** same c
45310 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
45320 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f  , then instead o
45330 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63  f invoking the c
45340 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
45350 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  .** multiple tim
45360 65 73 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65  es, it is invoke
45370 64 20 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20  d once with the 
45380 73 65 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e  set of void* con
45390 74 65 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a  text pointers.**
453a0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
453b0 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
453c0 74 69 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f  tions bundled to
453d0 67 65 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61  gether into an a
453e0 72 72 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69  rray..** This gi
453f0 76 65 73 20 74 68 65 20 61 70 70 6c 69 63 61 74  ves the applicat
45400 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ion an opportuni
45410 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65  ty to prioritize
45420 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a   any actions .**
45430 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20   related to the 
45440 73 65 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64  set of unblocked
45450 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
45460 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  tions..**.** <b>
45470 44 65 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69  Deadlock Detecti
45480 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73  on</b>.**.** Ass
45490 75 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72  uming that after
454a0 20 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72   registering for
454b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
454c0 79 20 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a  y callback a .**
454d0 20 64 61 74 61 62 61 73 65 20 77 61 69 74 73 20   database waits 
454e0 66 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  for the callback
454f0 20 74 6f 20 62 65 20 69 73 73 75 65 64 20 62 65   to be issued be
45500 66 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20  fore taking any 
45510 66 75 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f  further.** actio
45520 6e 20 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20  n (a reasonable 
45530 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65  assumption), the
45540 6e 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49  n using this API
45550 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a   may cause the.*
45560 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f  * application to
45570 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65   deadlock. For e
45580 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65  xample, if conne
45590 63 74 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69  ction X is waiti
455a0 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63  ng for.** connec
455b0 74 69 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63  tion Y's transac
455c0 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c  tion to be concl
455d0 75 64 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61  uded, and simila
455e0 72 6c 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  rly connection.*
455f0 2a 20 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f  * Y is waiting o
45600 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73  n connection X's
45610 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
45620 65 6e 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65  en neither conne
45630 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72  ction.** will pr
45640 6f 63 65 65 64 20 61 6e 64 20 74 68 65 20 73 79  oceed and the sy
45650 73 74 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20  stem may remain 
45660 64 65 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66  deadlocked indef
45670 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  initely..**.** T
45680 6f 20 61 76 6f 69 64 20 74 68 69 73 20 73 63 65  o avoid this sce
45690 6e 61 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74  nario, the sqlit
456a0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
456b0 28 29 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64  () performs dead
456c0 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f  lock.** detectio
456d0 6e 2e 20 49 66 20 61 20 67 69 76 65 6e 20 63 61  n. If a given ca
456e0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e  ll to sqlite3_un
456f0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f  lock_notify() wo
45700 75 6c 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73  uld put the.** s
45710 79 73 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c  ystem in a deadl
45720 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65  ocked state, the
45730 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
45740 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
45750 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  no.** unlock-not
45760 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ify callback is 
45770 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20  registered. The 
45780 73 79 73 74 65 6d 20 69 73 20 73 61 69 64 20 74  system is said t
45790 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61  o be in.** a dea
457a0 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66  dlocked state if
457b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61   connection A ha
457c0 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  s registered for
457d0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
457e0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  y.** callback on
457f0 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
45800 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27  of connection B'
45810 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  s transaction, a
45820 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  nd connection.**
45830 20 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65   B has itself re
45840 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20  gistered for an 
45850 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45860 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e  llback when conn
45870 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72  ection.** A's tr
45880 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e  ansaction is con
45890 63 6c 75 64 65 64 2e 20 49 6e 64 69 72 65 63 74  cluded. Indirect
458a0 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c 73   deadlock is als
458b0 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0a 2a  o detected, so.*
458c0 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  * the system is 
458d0 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20  also considered 
458e0 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 64  to be deadlocked
458f0 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42   if connection B
45900 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72   has.** register
45910 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ed for an unlock
45920 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45930 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   on the conclusi
45940 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  on of connection
45950 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 61 63 74  .** C's transact
45960 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e 65  ion, where conne
45970 63 74 69 6f 6e 20 43 20 69 73 20 77 61 69 74 69  ction C is waiti
45980 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ng on connection
45990 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e 75 6d 62 65   A. Any.** numbe
459a0 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 69  r of levels of i
459b0 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 65 20 61  ndirection are a
459c0 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62  llowed..**.** <b
459d0 3e 54 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45  >The "DROP TABLE
459e0 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f 62 3e 0a  " Exception</b>.
459f0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 6c  **.** When a cal
45a00 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
45a10 65 70 28 29 5d 20 72 65 74 75 72 6e 73 20 53 51  ep()] returns SQ
45a20 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 74 20  LITE_LOCKED, it 
45a30 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 61 6c  is almost .** al
45a40 77 61 79 73 20 61 70 70 72 6f 70 72 69 61 74 65  ways appropriate
45a50 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   to call sqlite3
45a60 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
45a70 2e 20 54 68 65 72 65 20 69 73 20 68 6f 77 65 76  . There is howev
45a80 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 65 70  er,.** one excep
45a90 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 65 63 75  tion. When execu
45aa0 74 69 6e 67 20 61 20 22 44 52 4f 50 20 54 41 42  ting a "DROP TAB
45ab0 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 49 4e 44  LE" or "DROP IND
45ac0 45 58 22 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a  EX" statement,.*
45ad0 2a 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20  * SQLite checks 
45ae0 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
45af0 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
45b00 74 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  ting SELECT stat
45b10 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 62  ements.** that b
45b20 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d  elong to the sam
45b30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66  e connection. If
45b40 20 74 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49   there are, SQLI
45b50 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a 2a 20  TE_LOCKED is.** 
45b60 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69  returned. In thi
45b70 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
45b80 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  no "blocking con
45b90 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76  nection", so inv
45ba0 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  oking.** sqlite3
45bb0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
45bc0 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
45bd0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45be0 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a 2a 2a 20  llback being.** 
45bf0 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74  invoked immediat
45c00 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70 70 6c  ely. If the appl
45c10 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72 65 2d  ication then re-
45c20 61 74 74 65 6d 70 74 73 20 74 68 65 20 22 44 52  attempts the "DR
45c30 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 6f 72 20  OP TABLE".** or 
45c40 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71 75 65  "DROP INDEX" que
45c50 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74 65 20  ry, an infinite 
45c60 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20 74 68  loop might be th
45c70 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  e result..**.** 
45c80 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e 64 20 74  One way around t
45c90 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74  his problem is t
45ca0 6f 20 63 68 65 63 6b 20 74 68 65 20 65 78 74 65  o check the exte
45cb0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20  nded error code 
45cc0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 61  returned.** by a
45cd0 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  n sqlite3_step()
45ce0 20 63 61 6c 6c 2e 20 49 66 20 74 68 65 72 65 20   call. If there 
45cf0 69 73 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f  is a blocking co
45d00 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74  nnection, then t
45d10 68 65 0a 2a 2a 20 65 78 74 65 6e 64 65 64 20 65  he.** extended e
45d20 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74  rror code is set
45d30 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   to SQLITE_LOCKE
45d40 44 5f 53 48 41 52 45 44 43 41 43 48 45 2e 20 4f  D_SHAREDCACHE. O
45d50 74 68 65 72 77 69 73 65 2c 20 69 6e 0a 2a 2a 20  therwise, in.** 
45d60 74 68 65 20 73 70 65 63 69 61 6c 20 22 44 52 4f  the special "DRO
45d70 50 20 54 41 42 4c 45 2f 49 4e 44 45 58 22 20 63  P TABLE/INDEX" c
45d80 61 73 65 2c 20 74 68 65 20 65 78 74 65 6e 64 65  ase, the extende
45d90 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  d error code is 
45da0 6a 75 73 74 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  just .** SQLITE_
45db0 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54  LOCKED..*/.SQLIT
45dc0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
45dd0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
45de0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f  .  sqlite3 *pBlo
45df0 63 6b 65 64 2c 20 20 20 20 20 20 20 20 20 20 20  cked,           
45e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45e10 2a 20 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63  * Waiting connec
45e20 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
45e30 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  *xNotify)(void *
45e40 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67  *apArg, int nArg
45e50 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63  ),    /* Callbac
45e60 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  k function to in
45e70 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  voke */.  void *
45e80 70 4e 6f 74 69 66 79 41 72 67 20 20 20 20 20 20  pNotifyArg      
45e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ea0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
45eb0 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 4e 6f  t to pass to xNo
45ec0 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a 0a 2f 2a 0a  tify */.);.../*.
45ed0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 72  ** CAPI3REF: Str
45ee0 69 6e 67 20 43 6f 6d 70 61 72 69 73 6f 6e 0a 2a  ing Comparison.*
45ef0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
45f00 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
45f10 33 5f 73 74 72 6e 69 63 6d 70 28 29 5d 20 41 50  3_strnicmp()] AP
45f20 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61  I allows applica
45f30 74 69 6f 6e 73 20 61 6e 64 20 65 78 74 65 6e 73  tions and extens
45f40 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 6f 6d 70 61  ions to.** compa
45f50 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  re the contents 
45f60 6f 66 20 74 77 6f 20 62 75 66 66 65 72 73 20 63  of two buffers c
45f70 6f 6e 74 61 69 6e 69 6e 67 20 55 54 46 2d 38 20  ontaining UTF-8 
45f80 73 74 72 69 6e 67 73 20 69 6e 20 61 0a 2a 2a 20  strings in a.** 
45f90 63 61 73 65 2d 69 6e 64 65 6e 64 65 6e 74 20 66  case-indendent f
45fa0 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68  ashion, using th
45fb0 65 20 73 61 6d 65 20 64 65 66 69 6e 69 74 69 6f  e same definitio
45fc0 6e 20 6f 66 20 63 61 73 65 20 69 6e 64 65 70 65  n of case indepe
45fd0 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 68 61 74 20  ndence .** that 
45fe0 53 51 4c 69 74 65 20 75 73 65 73 20 69 6e 74 65  SQLite uses inte
45ff0 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 6f 6d 70  rnally when comp
46000 61 72 69 6e 67 20 69 64 65 6e 74 69 66 69 65 72  aring identifier
46010 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
46020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72   int sqlite3_str
46030 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  nicmp(const char
46040 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
46050 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55  , int);../*.** U
46060 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61  ndo the hack tha
46070 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74  t converts float
46080 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20  ing point types 
46090 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a  to integer for.*
460a0 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63  * builds on proc
460b0 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66  essors without f
460c0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
460d0 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66  pport..*/.#ifdef
460e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
460f0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e  ATING_POINT.# un
46100 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69  def double.#endi
46110 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45  f..#if 0.}  /* E
46120 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 65 72  nd of the 'exter
46130 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a  n "C"' block */.
46140 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 0a  #endif.#endif...
46150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46160 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 68  End of sqlite3.h
46170 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
461a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
461b0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
461c0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
461d0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
461e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
461f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46200 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 69  Include hash.h i
46210 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
46220 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46240 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46250 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e  Begin file hash.
46260 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
46270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46290 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
462a0 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
462b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
462c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
462d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
462e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
462f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
46300 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
46310 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
46320 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
46330 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
46340 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
46350 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
46360 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
46370 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
46380 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
46390 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
463a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
463b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
463c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46400 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
46410 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
46420 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69  e for the generi
46430 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70  c hash-table imp
46440 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73  lemenation.** us
46450 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f  ed in SQLite..*/
46460 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
46470 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65  _HASH_H_.#define
46480 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
46490 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
464a0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72  larations of str
464b0 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65  uctures. */.type
464c0 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 20  def struct Hash 
464d0 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74  Hash;.typedef st
464e0 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 61  ruct HashElem Ha
464f0 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f  shElem;../* A co
46500 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c  mplete hash tabl
46510 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
46520 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
46530 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  g structure..** 
46540 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  The internals of
46550 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
46560 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
46570 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69  be opaque -- cli
46580 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75  ent.** code shou
46590 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ld not attempt t
465a0 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69  o access or modi
465b0 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66  fy the fields of
465c0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
465d0 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68  ** directly.  Ch
465e0 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74  ange this struct
465f0 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e  ure only by usin
46600 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62  g the routines b
46610 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  elow..** However
46620 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22 70  , some of the "p
46630 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22  rocedures" and "
46640 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d  functions" for m
46650 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20  odifying and.** 
46660 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
46670 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
46680 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
46690 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
466a0 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  ake.** this stru
466b0 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2a  cture opaque..**
466c0 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  .** All elements
466d0 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62   of the hash tab
466e0 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 69 6e 67  le are on a sing
466f0 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64  le doubly-linked
46700 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 68 2e 66   list..** Hash.f
46710 69 72 73 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  irst points to t
46720 68 65 20 68 65 61 64 20 6f 66 20 74 68 69 73 20  he head of this 
46730 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  list..**.** Ther
46740 65 20 61 72 65 20 48 61 73 68 2e 68 74 73 69 7a  e are Hash.htsiz
46750 65 20 62 75 63 6b 65 74 73 2e 20 20 45 61 63 68  e buckets.  Each
46760 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 73 20 74   bucket points t
46770 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 74  o a spot in.** t
46780 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 62 6c 79  he global doubly
46790 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 54  -linked list.  T
467a0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
467b0 68 65 20 62 75 63 6b 65 74 20 61 72 65 20 74 68  he bucket are th
467c0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 70 6f 69  e.** element poi
467d0 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 74 68 65  nted to plus the
467e0 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 6e 74 2d   next _ht.count-
467f0 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  1 elements in th
46800 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 61  e list..**.** Ha
46810 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 20 48 61  sh.htsize and Ha
46820 73 68 2e 68 74 20 6d 61 79 20 62 65 20 7a 65 72  sh.ht may be zer
46830 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65  o.  In that case
46840 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f 6e 65 0a   lookup is done.
46850 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 72 20 73  ** by a linear s
46860 65 61 72 63 68 20 6f 66 20 74 68 65 20 67 6c 6f  earch of the glo
46870 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f 72 20 73  bal list.  For s
46880 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 74 68 65  mall tables, the
46890 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 74 61 62   .** Hash.ht tab
468a0 6c 65 20 69 73 20 6e 65 76 65 72 20 61 6c 6c 6f  le is never allo
468b0 63 61 74 65 64 20 62 65 63 61 75 73 65 20 69 66  cated because if
468c0 20 74 68 65 72 65 20 61 72 65 20 66 65 77 20 65   there are few e
468d0 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68  lements.** in th
468e0 65 20 74 61 62 6c 65 2c 20 69 74 20 69 73 20 66  e table, it is f
468f0 61 73 74 65 72 20 74 6f 20 64 6f 20 61 20 6c 69  aster to do a li
46900 6e 65 61 72 20 73 65 61 72 63 68 20 74 68 61 6e  near search than
46910 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 68   to manage.** th
46920 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
46930 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a 20  .struct Hash {. 
46940 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 74   unsigned int ht
46950 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75  size;      /* Nu
46960 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20  mber of buckets 
46970 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
46980 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
46990 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
469a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
469b0 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74 61  tries in this ta
469c0 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ble */.  HashEle
469d0 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 20 20 20  m *first;       
469e0 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
469f0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
46a00 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  rray */.  struct
46a10 20 5f 68 74 20 7b 20 20 20 20 20 20 20 20 20 20   _ht {          
46a20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
46a30 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
46a40 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
46a50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
46a60 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74  r of entries wit
46a70 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
46a80 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 61     HashElem *cha
46a90 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  in;           /*
46aa0 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73   Pointer to firs
46ab0 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69  t entry with thi
46ac0 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68  s hash */.  } *h
46ad0 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65  t;.};../* Each e
46ae0 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61  lement in the ha
46af0 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69  sh table is an i
46b00 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
46b10 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72  ollowing .** str
46b20 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65  ucture.  All ele
46b30 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64  ments are stored
46b40 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75   on a single dou
46b50 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
46b60 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68  .**.** Again, th
46b70 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
46b80 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
46b90 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61  paque, but it ca
46ba0 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65  n't really.** be
46bb0 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20   opaque because 
46bc0 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61  it is used by ma
46bd0 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  cros..*/.struct 
46be0 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73  HashElem {.  Has
46bf0 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72  hElem *next, *pr
46c00 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ev;       /* Nex
46c10 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65  t and previous e
46c20 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74  lements in the t
46c30 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
46c40 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
46c50 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61         /* Data a
46c60 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
46c70 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  his element */. 
46c80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
46c90 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 2f 2a  y; int nKey;  /*
46ca0 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20   Key associated 
46cb0 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e  with this elemen
46cc0 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
46cd0 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20  ccess routines. 
46ce0 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65   To delete, inse
46cf0 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  rt a NULL pointe
46d00 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
46d10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
46d20 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a 29  3HashInit(Hash*)
46d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
46d40 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61   void *sqlite3Ha
46d50 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20  shInsert(Hash*, 
46d60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79  const char *pKey
46d70 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64  , int nKey, void
46d80 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45   *pData);.SQLITE
46d90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
46da0 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63  qlite3HashFind(c
46db0 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73  onst Hash*, cons
46dc0 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e  t char *pKey, in
46dd0 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f  t nKey);.SQLITE_
46de0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
46df0 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 61  ite3HashClear(Ha
46e00 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  sh*);../*.** Mac
46e10 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
46e20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
46e30 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
46e40 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
46e50 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  .** like this:.*
46e60 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a  *.**   Hash h;.*
46e70 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b  *   HashElem *p;
46e80 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66  .**   ....**   f
46e90 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46  or(p=sqliteHashF
46ea0 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 73  irst(&h); p; p=s
46eb0 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 29  qliteHashNext(p)
46ec0 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74  ){.**     SomeSt
46ed0 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d  ructure *pData =
46ee0 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
46ef0 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f  p);.**     // do
46f00 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20   something with 
46f10 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a  pData.**   }.*/.
46f20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61  #define sqliteHa
46f30 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29  shFirst(H)  ((H)
46f40 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65  ->first).#define
46f50 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28   sqliteHashNext(
46f60 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29  E)   ((E)->next)
46f70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48  .#define sqliteH
46f80 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 45  ashData(E)   ((E
46f90 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 64 65 66  )->data)./* #def
46fa0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65  ine sqliteHashKe
46fb0 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b  y(E)    ((E)->pK
46fc0 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20  ey) // NOT USED 
46fd0 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71  */./* #define sq
46fe0 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28  liteHashKeysize(
46ff0 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20  E) ((E)->nKey)  
47000 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a  // NOT USED */..
47010 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
47020 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73  entries in a has
47030 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 64  h table.*/./* #d
47040 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
47050 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e  Count(H)  ((H)->
47060 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 53  count) // NOT US
47070 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a  ED */..#endif /*
47080 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
47090 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
470a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68  **** End of hash
470b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
470c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470e0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
470f0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
47100 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
47110 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
47120 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
47130 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
47140 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72  **** Include par
47150 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  se.h in the midd
47160 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
47170 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
47180 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
47190 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
471a0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  parse.h ********
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
471c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
471d0 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f  ***/.#define TK_
471e0 53 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20  SEMI            
471f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47200 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50  1.#define TK_EXP
47210 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  LAIN            
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23               2.#
47230 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20  define TK_QUERY 
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66            3.#def
47260 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20  ine TK_PLAN     
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47280 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65         4.#define
47290 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20   TK_BEGIN       
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472b0 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b      5.#define TK
472c0 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472e0 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45   6.#define TK_DE
472f0 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20  FERRED          
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a                7.
47310 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44  #define TK_IMMED
47320 49 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  IATE            
47330 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65             8.#de
47340 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56  fine TK_EXCLUSIV
47350 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47360 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
47370 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20  e TK_COMMIT     
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47390 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54      10.#define T
473a0 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20  K_END           
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473c0 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   11.#define TK_R
473d0 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20  OLLBACK         
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
473f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 45  .#define TK_SAVE
47400 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  POINT           
47410 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64             13.#d
47420 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45  efine TK_RELEASE
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47440 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69          14.#defi
47450 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 20  ne TK_TO        
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47470 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20       15.#define 
47480 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  TK_TABLE        
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474a0 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    16.#define TK_
474b0 43 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20  CREATE          
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
474d0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 20  7.#define TK_IF 
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474f0 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23              18.#
47500 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20  define TK_NOT   
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47520 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66           19.#def
47530 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20  ine TK_EXISTS   
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47550 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65        20.#define
47560 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20   TK_TEMP        
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47580 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b     21.#define TK
47590 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20  _LP             
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475b0 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 50  22.#define TK_RP
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a               23.
475e0 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 20  #define TK_AS   
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47600 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65            24.#de
47610 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20  fine TK_COMMA   
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47630 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e         25.#defin
47640 65 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 20  e TK_ID         
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47660 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54      26.#define T
47670 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 20  K_INDEXED       
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47690 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   27.#define TK_A
476a0 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20  BORT            
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38                28
476c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 43 54 49  .#define TK_ACTI
476d0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
476e0 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64             29.#d
476f0 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20  efine TK_AFTER  
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47710 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69          30.#defi
47720 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20  ne TK_ANALYZE   
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47740 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20       31.#define 
47750 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 20  TK_ASC          
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47770 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    32.#define TK_
47780 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20  ATTACH          
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
477a0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46  3.#define TK_BEF
477b0 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
477c0 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23              34.#
477d0 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20  define TK_BY    
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f0 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66           35.#def
47800 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20  ine TK_CASCADE  
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47820 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65        36.#define
47830 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20   TK_CAST        
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47850 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b     37.#define TK
47860 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20  _COLUMNKW       
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47880 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  38.#define TK_CO
47890 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20  NFLICT          
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a               39.
478b0 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42  #define TK_DATAB
478c0 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ASE             
478d0 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65            40.#de
478e0 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20  fine TK_DESC    
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47900 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e         41.#defin
47910 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20  e TK_DETACH     
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47930 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54      42.#define T
47940 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20  K_EACH          
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47960 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46   43.#define TK_F
47970 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  AIL             
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34                44
47990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20  .#define TK_FOR 
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479b0 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64             45.#d
479c0 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20  efine TK_IGNORE 
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479e0 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69          46.#defi
479f0 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20  ne TK_INITIALLY 
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a10 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20       47.#define 
47a20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20  TK_INSTEAD      
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a40 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    48.#define TK_
47a50 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20  LIKE_KW         
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47a70 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54  9.#define TK_MAT
47a80 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
47a90 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23              50.#
47aa0 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 20 20 20 20  define TK_NO    
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ac0 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66           51.#def
47ad0 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20  ine TK_KEY      
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47af0 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65        52.#define
47b00 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20   TK_OF          
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b     53.#define TK
47b30 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 20  _OFFSET         
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b50 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52  54.#define TK_PR
47b60 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  AGMA            
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a               55.
47b80 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 45  #define TK_RAISE
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ba0 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65            56.#de
47bb0 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 20  fine TK_REPLACE 
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bd0 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e         57.#defin
47be0 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20 20  e TK_RESTRICT   
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c00 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54      58.#define T
47c10 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20 20  K_ROW           
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c30 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   59.#define TK_T
47c40 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 20  RIGGER          
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30                60
47c60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 55  .#define TK_VACU
47c70 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  UM              
47c80 20 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64             61.#d
47c90 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 20  efine TK_VIEW   
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cb0 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69          62.#defi
47cc0 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 20  ne TK_VIRTUAL   
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ce0 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20       63.#define 
47cf0 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20  TK_REINDEX      
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d10 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    64.#define TK_
47d20 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20  RENAME          
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
47d40 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 49  5.#define TK_CTI
47d50 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20  ME_KW           
47d60 20 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23              66.#
47d70 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20  define TK_ANY   
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d90 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66           67.#def
47da0 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20  ine TK_OR       
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dc0 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65        68.#define
47dd0 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20   TK_AND         
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df0 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b     69.#define TK
47e00 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 20  _IS             
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45  70.#define TK_BE
47e30 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20 20  TWEEN           
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a               71.
47e50 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20  #define TK_IN   
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e70 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65            72.#de
47e80 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20  fine TK_ISNULL  
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 37 33 0a 23 64 65 66 69 6e         73.#defin
47eb0 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20  e TK_NOTNULL    
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ed0 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54      74.#define T
47ee0 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20  K_NE            
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f00 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45   75.#define TK_E
47f10 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Q               
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36                76
47f30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 20  .#define TK_GT  
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f50 20 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64             77.#d
47f60 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20  efine TK_LE     
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f80 20 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69          78.#defi
47f90 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20  ne TK_LT        
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fb0 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20       79.#define 
47fc0 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20  TK_GE           
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fe0 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    80.#define TK_
47ff0 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20 20  ESCAPE          
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
48010 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54  1.#define TK_BIT
48020 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20  AND             
48030 20 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23              82.#
48040 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20  define TK_BITOR 
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48060 20 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66           83.#def
48070 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20  ine TK_LSHIFT   
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48090 20 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65        84.#define
480a0 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20   TK_RSHIFT      
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480c0 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b     85.#define TK
480d0 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20  _PLUS           
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480f0 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49  86.#define TK_MI
48100 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20  NUS             
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a               87.
48120 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 20  #define TK_STAR 
48130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48140 20 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65            88.#de
48150 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 20  fine TK_SLASH   
48160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48170 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e         89.#defin
48180 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 20  e TK_REM        
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481a0 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54      90.#define T
481b0 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20 20  K_CONCAT        
481c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481d0 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   91.#define TK_C
481e0 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20 20  OLLATE          
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32                92
48200 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e  .#define TK_BITN
48210 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
48220 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64             93.#d
48230 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20  efine TK_STRING 
48240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48250 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69          94.#defi
48260 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20  ne TK_JOIN_KW   
48270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48280 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20       95.#define 
48290 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20  TK_CONSTRAINT   
482a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482b0 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    96.#define TK_
482c0 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  DEFAULT         
482d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
482e0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c  7.#define TK_NUL
482f0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
48300 20 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23              98.#
48310 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52  define TK_PRIMAR
48320 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
48330 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66           99.#def
48340 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20  ine TK_UNIQUE   
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48360 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e        100.#defin
48370 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20  e TK_CHECK      
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48390 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20      101.#define 
483a0 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20  TK_REFERENCES   
483b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483c0 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b    102.#define TK
483d0 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20 20  _AUTOINCR       
483e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483f0 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f  103.#define TK_O
48400 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
48410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
48420 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c  4.#define TK_DEL
48430 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ETE             
48440 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a              105.
48450 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 44 41 54  #define TK_UPDAT
48460 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48470 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64            106.#d
48480 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20 20  efine TK_SET    
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484a0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66          107.#def
484b0 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42 4c  ine TK_DEFERRABL
484c0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
484d0 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e        108.#defin
484e0 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20 20  e TK_FOREIGN    
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48500 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20      109.#define 
48510 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20 20  TK_DROP         
48520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48530 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b    110.#define TK
48540 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20  _UNION          
48550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48560 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  111.#define TK_A
48570 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
48580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
48590 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43  2.#define TK_EXC
485a0 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20 20  EPT             
485b0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a              113.
485c0 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 52  #define TK_INTER
485d0 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20  SECT            
485e0 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64            114.#d
485f0 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54 20  efine TK_SELECT 
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48610 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66          115.#def
48620 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54 20  ine TK_DISTINCT 
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48640 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e        116.#defin
48650 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 20  e TK_DOT        
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48670 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20      117.#define 
48680 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 20  TK_FROM         
48690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486a0 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b    118.#define TK
486b0 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 20  _JOIN           
486c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486d0 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55  119.#define TK_U
486e0 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  SING            
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
48700 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44  0.#define TK_ORD
48710 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ER              
48720 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a              121.
48730 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50  #define TK_GROUP
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48750 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64            122.#d
48760 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20  efine TK_HAVING 
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48780 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66          123.#def
48790 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20  ine TK_LIMIT    
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487b0 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e        124.#defin
487c0 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20  e TK_WHERE      
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487e0 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20      125.#define 
487f0 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20  TK_INTO         
48800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48810 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b    126.#define TK
48820 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20  _VALUES         
48830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48840 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  127.#define TK_I
48850 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
48860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
48870 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54  8.#define TK_INT
48880 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20  EGER            
48890 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a              129.
488a0 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 54  #define TK_FLOAT
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488c0 20 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64            130.#d
488d0 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20  efine TK_BLOB   
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488f0 20 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66          131.#def
48900 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20  ine TK_REGISTER 
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48920 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e        132.#defin
48930 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 20  e TK_VARIABLE   
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48950 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20      133.#define 
48960 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 20  TK_CASE         
48970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48980 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b    134.#define TK
48990 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20 20  _WHEN           
489a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489b0 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  135.#define TK_T
489c0 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  HEN             
489d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
489e0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c 53  6.#define TK_ELS
489f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48a00 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a              137.
48a10 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58  #define TK_INDEX
48a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a30 20 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64            138.#d
48a40 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20 20  efine TK_ALTER  
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a60 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66          139.#def
48a70 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 20  ine TK_ADD      
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a90 20 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e        140.#defin
48aa0 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20  e TK_TO_TEXT    
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ac0 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20      141.#define 
48ad0 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20  TK_TO_BLOB      
48ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48af0 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b    142.#define TK
48b00 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20  _TO_NUMERIC     
48b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  143.#define TK_T
48b30 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  O_INT           
48b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
48b50 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f  4.#define TK_TO_
48b60 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 20  REAL            
48b70 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a              145.
48b80 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 4f 54  #define TK_ISNOT
48b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ba0 20 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64            146.#d
48bb0 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f  efine TK_END_OF_
48bc0 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  FILE            
48bd0 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66          147.#def
48be0 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20  ine TK_ILLEGAL  
48bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c00 20 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e        148.#defin
48c10 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20  e TK_SPACE      
48c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c30 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20      149.#define 
48c40 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49  TK_UNCLOSED_STRI
48c50 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
48c60 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b    150.#define TK
48c70 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
48c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c90 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  151.#define TK_C
48ca0 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
48cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
48cc0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47  2.#define TK_AGG
48cd0 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
48ce0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 0a              153.
48cf0 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43  #define TK_AGG_C
48d00 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
48d10 20 20 20 20 20 20 20 20 20 20 31 35 34 0a 23 64            154.#d
48d20 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46  efine TK_CONST_F
48d30 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  UNC             
48d40 20 20 20 20 20 20 20 20 31 35 35 0a 23 64 65 66          155.#def
48d50 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20  ine TK_UMINUS   
48d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d70 20 20 20 20 20 20 31 35 36 0a 23 64 65 66 69 6e        156.#defin
48d80 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 20  e TK_UPLUS      
48d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48da0 20 20 20 20 31 35 37 0a 0a 2f 2a 2a 2a 2a 2a 2a      157../******
48db0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
48dc0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  parse.h ********
48dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48df0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
48e00 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
48e10 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
48e20 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
48e30 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
48e40 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64  *******/.#includ
48e50 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63  e <stdio.h>.#inc
48e60 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a  lude <stdlib.h>.
48e70 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
48e80 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73  .h>.#include <as
48e90 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  sert.h>.#include
48ea0 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a   <stddef.h>../*.
48eb0 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20  ** If compiling 
48ec0 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20  for a processor 
48ed0 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74  that lacks float
48ee0 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72  ing point suppor
48ef0 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65  t,.** substitute
48f00 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f   integer for flo
48f10 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23  ating-point.*/.#
48f20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
48f30 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
48f40 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65  .# define double
48f50 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20   sqlite_int64.# 
48f60 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
48f70 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e  E_TYPE sqlite_in
48f80 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 4c  t64.# ifndef SQL
48f90 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20  ITE_BIG_DBL.#   
48fa0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49  define SQLITE_BI
48fb0 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 33  G_DBL (((sqlite3
48fc0 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 23  _int64)1)<<50).#
48fd0 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20   endif.# define 
48fe0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
48ff0 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 64  TIME_FUNCS 1.# d
49000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49  efine SQLITE_OMI
49010 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 65  T_TRACE 1.# unde
49020 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
49030 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
49040 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  T.# undef SQLITE
49050 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e 64  _HAVE_ISNAN.#end
49060 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
49070 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69  E_BIG_DBL.# defi
49080 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  ne SQLITE_BIG_DB
49090 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a  L (1e99).#endif.
490a0 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50  ./*.** OMIT_TEMP
490b0 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69  DB is set to 1 i
490c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
490d0 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c  MPDB is defined,
490e0 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61   or 0.** afterwa
490f0 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20  rd. Having this 
49100 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20  macro allows us 
49110 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63  to cause the C c
49120 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f  ompiler .** to o
49130 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79  mit code used by
49140 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74   TEMP tables wit
49150 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64  hout messy #ifnd
49160 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ef statements..*
49170 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
49180 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66  OMIT_TEMPDB.#def
49190 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ine OMIT_TEMPDB 
491a0 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  1.#else.#define 
491b0 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65  OMIT_TEMPDB 0.#e
491c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
491d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
491e0 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20  ro is set to 1, 
491f0 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73  then NULL values
49200 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a   are considered.
49210 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e  ** distinct when
49220 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65   determining whe
49230 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20  ther or not two 
49240 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20  entries are the 
49250 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49  same.** in a UNI
49260 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73  QUE index.  This
49270 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74   is the way Post
49280 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20  greSQL, Oracle, 
49290 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f  DB2, MySQL,.** O
492a0 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62  CELOT, and Fireb
492b0 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54  ird all work.  T
492c0 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78  he SQL92 spec ex
492d0 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68  plicitly says th
492e0 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79  is.** is the way
492f0 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70   things are supp
49300 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a  ose to work..**.
49310 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
49320 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74  ing macro is set
49330 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73   to 0, the NULLs
49340 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20   are indistinct 
49350 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20  for.** a UNIQUE 
49360 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20  index.  In this 
49370 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e  mode, you can on
49380 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65  ly have a single
49390 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66   NULL entry.** f
493a0 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  or a column decl
493b0 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68  ared UNIQUE.  Th
493c0 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e  is is the way In
493d0 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53  formix and SQL S
493e0 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a  erver.** work..*
493f0 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44  /.#define NULL_D
49400 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51  ISTINCT_FOR_UNIQ
49410 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  UE 1../*.** The 
49420 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75  "file format" nu
49430 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67  mber is an integ
49440 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65  er that is incre
49450 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a  mented whenever.
49460 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65  ** the VDBE-leve
49470 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68  l file format ch
49480 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c  anges.  The foll
49490 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66  owing macros def
494a0 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64  ine the.** the d
494b0 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d  efault file form
494c0 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62  at for new datab
494d0 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78  ases and the max
494e0 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74  imum file format
494f0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62  .** that the lib
49500 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a  rary can read..*
49510 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
49520 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
49530 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   4.#ifndef SQLIT
49540 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46  E_DEFAULT_FILE_F
49550 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53  ORMAT.# define S
49560 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
49570 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64  LE_FORMAT 1.#end
49580 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
49590 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52  TE_DEFAULT_RECUR
495a0 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23 20  SIVE_TRIGGERS.# 
495b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
495c0 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f  FAULT_RECURSIVE_
495d0 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64 69  TRIGGERS 0.#endi
495e0 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65  f../*.** Provide
495f0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
49600 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50   for SQLITE_TEMP
49610 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69  _STORE in case i
49620 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69  t is not specifi
49630 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ed.** on the com
49640 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66  mand-line.*/.#if
49650 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
49660 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20  _STORE.# define 
49670 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
49680 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  E 1.#endif../*.*
49690 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64  * GCC does not d
496a0 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74  efine the offset
496b0 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65  of() macro so we
496c0 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69  'll have to do i
496d0 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a  t.** ourselves..
496e0 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65  */.#ifndef offse
496f0 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73  tof.#define offs
49700 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46  etof(STRUCTURE,F
49710 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68  IELD) ((int)((ch
49720 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45  ar*)&((STRUCTURE
49730 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65  *)0)->FIELD)).#e
49740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
49750 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73  k to see if this
49760 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42   machine uses EB
49770 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c  CDIC.  (Yes, bel
49780 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f  ieve it or.** no
49790 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69  t, there are sti
497a0 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20  ll machines out 
497b0 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45  there that use E
497c0 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27  BCDIC.).*/.#if '
497d0 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64  A' == '\301'.# d
497e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43  efine SQLITE_EBC
497f0 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  DIC 1.#else.# de
49800 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49  fine SQLITE_ASCI
49810 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  I 1.#endif../*.*
49820 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e  * Integers of kn
49830 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73  own sizes.  Thes
49840 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74  e typedefs might
49850 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68   change for arch
49860 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65  itectures.** whe
49870 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72  re the sizes ver
49880 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72  y.  Preprocessor
49890 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69   macros are avai
498a0 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68  lable so that th
498b0 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62  e.** types can b
498c0 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72  e conveniently r
498d0 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70  edefined at comp
498e0 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20  ile-type.  Like 
498f0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
49900 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54      cc '-DUINTPT
49910 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67  R_TYPE=long long
49920 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66   int' ....*/.#if
49930 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  ndef UINT32_TYPE
49940 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49  .# ifdef HAVE_UI
49950 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65  NT32_T.#  define
49960 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e   UINT32_TYPE uin
49970 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t32_t.# else.#  
49980 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59  define UINT32_TY
49990 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a  PE unsigned int.
499a0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
499b0 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59  ifndef UINT16_TY
499c0 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f  PE.# ifdef HAVE_
499d0 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  UINT16_T.#  defi
499e0 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
499f0 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23  int16_t.# else.#
49a00 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f    define UINT16_
49a10 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68  TYPE unsigned sh
49a20 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a  ort int.# endif.
49a30 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49  #endif.#ifndef I
49a40 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65  NT16_TYPE.# ifde
49a50 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23  f HAVE_INT16_T.#
49a60 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54    define INT16_T
49a70 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c  YPE int16_t.# el
49a80 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54  se.#  define INT
49a90 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e  16_TYPE short in
49aa0 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
49ab0 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54  .#ifndef UINT8_T
49ac0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
49ad0 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69  _UINT8_T.#  defi
49ae0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69  ne UINT8_TYPE ui
49af0 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  nt8_t.# else.#  
49b00 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50  define UINT8_TYP
49b10 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a  E unsigned char.
49b20 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
49b30 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ifndef INT8_TYPE
49b40 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e  .# ifdef HAVE_IN
49b50 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49  T8_T.#  define I
49b60 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a  NT8_TYPE int8_t.
49b70 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
49b80 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65   INT8_TYPE signe
49b90 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23  d char.# endif.#
49ba0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f  endif.#ifndef LO
49bb0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20  NGDOUBLE_TYPE.# 
49bc0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
49bd0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62  E_TYPE long doub
49be0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  le.#endif.typede
49bf0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  f sqlite_int64 i
49c00 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  64;          /* 
49c10 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  8-byte signed in
49c20 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
49c30 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75   sqlite_uint64 u
49c40 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38  64;         /* 8
49c50 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
49c60 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
49c70 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33  f UINT32_TYPE u3
49c80 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
49c90 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  4-byte unsigned 
49ca0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49cb0 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ef UINT16_TYPE u
49cc0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  16;           /*
49cd0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
49ce0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49cf0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69  def INT16_TYPE i
49d00 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
49d10 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
49d20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49d30 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38  ef UINT8_TYPE u8
49d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
49d50 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   1-byte unsigned
49d60 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49d70 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38  def INT8_TYPE i8
49d80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
49d90 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
49da0 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  integer */../*.*
49db0 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32  * SQLITE_MAX_U32
49dc0 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74 61   is a u64 consta
49dd0 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20 6d  nt that is the m
49de0 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75 65  aximum u64 value
49df0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
49e00 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32 20  stored in a u32 
49e10 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20  without loss of 
49e20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75 65  data.  The value
49e30 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30 30  .** is 0x0000000
49e40 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74 20  0ffffffff.  But 
49e50 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72 6b  because of quirk
49e60 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69 6c  s of some compil
49e70 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65 20  ers, we.** have 
49e80 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20 76  to specify the v
49e90 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73 73  alue in the less
49ea0 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e 65   intuitive manne
49eb0 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65 66  r shown:.*/.#def
49ec0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55  ine SQLITE_MAX_U
49ed0 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c 3c  32  ((((u64)1)<<
49ee0 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61  32)-1)../*.** Ma
49ef0 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  cros to determin
49f00 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61  e whether the ma
49f10 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20  chine is big or 
49f20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a  little endian,.*
49f30 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72  * evaluated at r
49f40 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  untime..*/.#ifde
49f50 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
49f60 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  ATION.SQLITE_PRI
49f70 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
49f80 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23  qlite3one = 1;.#
49f90 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56  else.SQLITE_PRIV
49fa0 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71  ATE const int sq
49fb0 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66  lite3one;.#endif
49fc0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38  .#if defined(i38
49fd0 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
49fe0 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
49ff0 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 20  ed(_M_IX86)\.   
4a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a010 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66            || def
4a020 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c  ined(__x86_64) |
4a030 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f  | defined(__x86_
4a040 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53  64__).# define S
4a050 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
4a060 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51     0.# define SQ
4a070 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41  LITE_LITTLEENDIA
4a080 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  N 1.# define SQL
4a090 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
4a0a0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a   SQLITE_UTF16LE.
4a0b0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
4a0c0 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
4a0d0 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73     (*(char *)(&s
4a0e0 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23  qlite3one)==0).#
4a0f0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
4a100 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63  ITTLEENDIAN (*(c
4a110 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f  har *)(&sqlite3o
4a120 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65  ne)==1).# define
4a130 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
4a140 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45  IVE (SQLITE_BIGE
4a150 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46  NDIAN?SQLITE_UTF
4a160 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31  16BE:SQLITE_UTF1
4a170 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  6LE).#endif../*.
4a180 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ** Constants for
4a190 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64   the largest and
4a1a0 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   smallest possib
4a1b0 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  le 64-bit signed
4a1c0 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68   integers..** Th
4a1d0 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64  ese macros are d
4a1e0 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
4a1f0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74  correctly on bot
4a200 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d  h 32-bit and 64-
4a210 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73  bit.** compilers
4a220 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52  ..*/.#define LAR
4a230 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66  GEST_INT64  (0xf
4a240 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30  fffffff|(((i64)0
4a250 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29  x7fffffff)<<32))
4a260 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53  .#define SMALLES
4a270 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d  T_INT64 (((i64)-
4a280 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54  1) - LARGEST_INT
4a290 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 6e  64)../* .** Roun
4a2a0 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f  d up a number to
4a2b0 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
4a2c0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
4a2d0 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
4a2e0 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65   to force 8-byte
4a2f0 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34   alignment on 64
4a300 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72  -bit architectur
4a310 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  es..*/.#define R
4a320 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 28  OUND8(x)     (((
4a330 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a  x)+7)&~7)../*.**
4a340 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74   Round down to t
4a350 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 69  he nearest multi
4a360 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 66  ple of 8.*/.#def
4a370 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 78  ine ROUNDDOWN8(x
4a380 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  ) ((x)&~7)../*.*
4a390 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
4a3a0 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 61  e pointer X is a
4a3b0 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62  ligned to an 8-b
4a3c0 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f  yte boundary..*/
4a3d0 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42  .#define EIGHT_B
4a3e0 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29  YTE_ALIGNMENT(X)
4a3f0 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29     ((((char*)(X)
4a400 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d   - (char*)0)&7)=
4a410 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  =0).../*.** An i
4a420 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
4a430 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
4a440 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  re is used to st
4a450 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 6e  ore the busy-han
4a460 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  dler.** callback
4a470 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 6c   for a given sql
4a480 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a  ite handle. .**.
4a490 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 75  ** The sqlite.bu
4a4a0 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 72  syHandler member
4a4b0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 73   of the sqlite s
4a4c0 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 74  truct contains t
4a4d0 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c 62  he busy.** callb
4a4e0 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 61  ack for the data
4a4f0 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 63  base handle. Eac
4a500 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 76  h pager opened v
4a510 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a  ia the sqlite.**
4a520 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65   handle is passe
4a530 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 73  d a pointer to s
4a540 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65  qlite.busyHandle
4a550 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64  r. The busy-hand
4a560 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ler.** callback 
4a570 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 76  is currently inv
4a580 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77  oked only from w
4a590 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a 2a  ithin pager.c..*
4a5a0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
4a5b0 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 73   BusyHandler Bus
4a5c0 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 74  yHandler;.struct
4a5d0 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a 20   BusyHandler {. 
4a5e0 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f   int (*xFunc)(vo
4a5f0 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  id *,int);  /* T
4a600 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
4a610 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67   */.  void *pArg
4a620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4a630 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f   /* First arg to
4a640 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a   busy callback *
4a650 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20  /.  int nBusy;  
4a660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4a670 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69  * Incremented wi
4a680 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c  th each busy cal
4a690 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e  l */.};../*.** N
4a6a0 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65  ame of the maste
4a6b0 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  r database table
4a6c0 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 61  .  The master da
4a6d0 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a 20  tabase table.** 
4a6e0 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 62  is a special tab
4a6f0 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  le that holds th
4a700 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 72  e names and attr
4a710 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a  ibutes of all.**
4a720 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e 64   user tables and
4a730 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 65   indices..*/.#de
4a740 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d 45  fine MASTER_NAME
4a750 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f 6d         "sqlite_m
4a760 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 54  aster".#define T
4a770 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 20  EMP_MASTER_NAME 
4a780 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61   "sqlite_temp_ma
4a790 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ster"../*.** The
4a7a0 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68   root-page of th
4a7b0 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73  e master databas
4a7c0 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66  e table..*/.#def
4a7d0 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20  ine MASTER_ROOT 
4a7e0 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54        1../*.** T
4a7f0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73  he name of the s
4a800 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a  chema table..*/.
4a810 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f 54  #define SCHEMA_T
4a820 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 54  ABLE(x)  ((!OMIT
4a830 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 29  _TEMPDB)&&(x==1)
4a840 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d  ?TEMP_MASTER_NAM
4a850 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a 0a  E:MASTER_NAME)..
4a860 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 65  /*.** A convenie
4a870 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 72  nce macro that r
4a880 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
4a890 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
4a8a0 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2f  .** an array..*/
4a8b0 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 69  .#define ArraySi
4a8c0 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 28  ze(X)    ((int)(
4a8d0 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f 66  sizeof(X)/sizeof
4a8e0 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20  (X[0])))../*.** 
4a8f0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
4a900 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75 63  lue as a destruc
4a910 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 65  tor means to use
4a920 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29   sqlite3DbFree()
4a930 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  ..** This is an 
4a940 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 69  internal extensi
4a950 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 41  on to SQLITE_STA
4a960 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f 54  TIC and SQLITE_T
4a970 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 65  RANSIENT..*/.#de
4a980 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e 41  fine SQLITE_DYNA
4a990 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 5f  MIC   ((sqlite3_
4a9a0 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29  destructor_type)
4a9b0 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a 0a  sqlite3DbFree)..
4a9c0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54  /*.** When SQLIT
4a9d0 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 65  E_OMIT_WSD is de
4a9e0 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20  fined, it means 
4a9f0 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74 20  that the target 
4aa00 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a  platform does.**
4aa10 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 69   not support Wri
4aa20 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 74  table Static Dat
4aa30 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73 20  a (WSD) such as 
4aa40 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 69  global and stati
4aa50 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20  c variables..** 
4aa60 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 75  All variables mu
4aa70 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e 20  st either be on 
4aa80 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 6e  the stack or dyn
4aa90 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
4aaa0 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68  ed from.** the h
4aab0 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 69  eap.  When WSD i
4aac0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 74  s unsupported, t
4aad0 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c  he variable decl
4aae0 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 72  arations scatter
4aaf0 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74  ed.** throughout
4ab00 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65   the SQLite code
4ab10 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e   must become con
4ab20 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e 20  stants instead. 
4ab30 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 0a   The SQLITE_WSD.
4ab40 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  ** macro is used
4ab50 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73   for this purpos
4ab60 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 20  e.  And instead 
4ab70 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 74  of referencing t
4ab80 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64  he variable.** d
4ab90 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 20  irectly, we use 
4aba0 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 20  its constant as 
4abb0 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20  a key to lookup 
4abc0 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c  the run-time all
4abd0 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 72  ocated.** buffer
4abe0 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 6c   that holds real
4abf0 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 20   variable.  The 
4ac00 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 6f  constant is also
4ac10 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72   the initializer
4ac20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e 2d  .** for the run-
4ac30 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  time allocated b
4ac40 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  uffer..**.** In 
4ac50 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 77  the usual case w
4ac60 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70 70  here WSD is supp
4ac70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 54  orted, the SQLIT
4ac80 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c  E_WSD and GLOBAL
4ac90 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d  .** macros becom
4aca0 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76  e no-ops and hav
4acb0 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e  e zero performan
4acc0 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69  ce impact..*/.#i
4acd0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
4ace0 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 53  _WSD.  #define S
4acf0 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a  QLITE_WSD const.
4ad00 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c    #define GLOBAL
4ad10 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c 69  (t,v) (*(t*)sqli
4ad20 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f  te3_wsd_find((vo
4ad30 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f 66  id*)&(v), sizeof
4ad40 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 20  (v))).  #define 
4ad50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
4ad60 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 63  fig GLOBAL(struc
4ad70 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c  t Sqlite3Config,
4ad80 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a   sqlite3Config).
4ad90 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e 74  SQLITE_API   int
4ada0 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69   sqlite3_wsd_ini
4adb0 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b  t(int N, int J);
4adc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 6f  .SQLITE_API   vo
4add0 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f  id *sqlite3_wsd_
4ade0 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e  find(void *K, in
4adf0 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  t L);.#else.  #d
4ae00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44  efine SQLITE_WSD
4ae10 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42   .  #define GLOB
4ae20 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 66  AL(t,v) v.  #def
4ae30 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ine sqlite3Globa
4ae40 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43  lConfig sqlite3C
4ae50 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a  onfig.#endif../*
4ae60 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
4ae70 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65  g macros are use
4ae80 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 6f  d to suppress co
4ae90 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20  mpiler warnings 
4aea0 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69  and to.** make i
4aeb0 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e  t clear to human
4aec0 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 20   readers when a 
4aed0 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74  function paramet
4aee0 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 65  er is deliberate
4aef0 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73  ly .** left unus
4af00 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f  ed within the bo
4af10 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  dy of a function
4af20 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 68  . This usually h
4af30 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61  appens when.** a
4af40 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
4af50 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 69  led via a functi
4af60 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20  on pointer. For 
4af70 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20  example the .** 
4af80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
4af90 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 61  f an SQL aggrega
4afa0 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 6b  te step callback
4afb0 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 65   may not use the
4afc0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e  .** parameter in
4afd0 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 6d  dicating the num
4afe0 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
4aff0 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 61   passed to the a
4b000 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20  ggregate,.** if 
4b010 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68  it knows that th
4b020 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 65  is is enforced e
4b030 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20  lsewhere..**.** 
4b040 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20  When a function 
4b050 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74  parameter is not
4b060 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 74   used at all wit
4b070 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20  hin the body of 
4b080 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69  a function,.** i
4b090 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e  t is generally n
4b0a0 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 6f  amed "NotUsed" o
4b0b0 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f 20  r "NotUsed2" to 
4b0c0 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 6e  make things even
4b0d0 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77   clearer..** How
4b0e0 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 72  ever, these macr
4b0f0 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 75  os may also be u
4b100 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20  sed to suppress 
4b110 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 64  warnings related
4b120 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72   to.** parameter
4b130 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d 61  s that may or ma
4b140 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 65  y not be used de
4b150 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69  pending on compi
4b160 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a  lation options..
4b170 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74  ** For example t
4b180 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 20  hose parameters 
4b190 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 73  only used in ass
4b1a0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
4b1b0 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61  . In these.** ca
4b1c0 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 65  ses the paramete
4b1d0 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 20  rs are named as 
4b1e0 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 6f  per the usual co
4b1f0 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64  nventions..*/.#d
4b200 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52  efine UNUSED_PAR
4b210 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 29  AMETER(x) (void)
4b220 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 53  (x).#define UNUS
4b230 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 2c  ED_PARAMETER2(x,
4b240 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  y) UNUSED_PARAME
4b250 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 41  TER(x),UNUSED_PA
4b260 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a  RAMETER(y)../*.*
4b270 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
4b280 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 72  nces to structur
4b290 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  es.*/.typedef st
4b2a0 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 67  ruct AggInfo Agg
4b2b0 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74  Info;.typedef st
4b2c0 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74  ruct AuthContext
4b2d0 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79   AuthContext;.ty
4b2e0 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75 74  pedef struct Aut
4b2f0 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e 63  oincInfo Autoinc
4b300 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74  Info;.typedef st
4b310 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74 76  ruct Bitvec Bitv
4b320 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ec;.typedef stru
4b330 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65 74  ct RowSet RowSet
4b340 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b350 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71   CollSeq CollSeq
4b360 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b370 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a   Column Column;.
4b380 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
4b390 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74  b Db;.typedef st
4b3a0 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65  ruct Schema Sche
4b3b0 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ma;.typedef stru
4b3c0 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79  ct Expr Expr;.ty
4b3d0 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70  pedef struct Exp
4b3e0 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a  rList ExprList;.
4b3f0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45  typedef struct E
4b400 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 6e  xprSpan ExprSpan
4b410 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b420 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65   FKey FKey;.type
4b430 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44  def struct FuncD
4b440 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65  ef FuncDef;.type
4b450 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44  def struct FuncD
4b460 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61  efHash FuncDefHa
4b470 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  sh;.typedef stru
4b480 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74  ct IdList IdList
4b490 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b4a0 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79   Index Index;.ty
4b4b0 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 64  pedef struct Ind
4b4c0 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53 61  exSample IndexSa
4b4d0 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74  mple;.typedef st
4b4e0 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b 65  ruct KeyClass Ke
4b4f0 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 20  yClass;.typedef 
4b500 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b  struct KeyInfo K
4b510 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  eyInfo;.typedef 
4b520 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65  struct Lookaside
4b530 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65   Lookaside;.type
4b540 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61  def struct Looka
4b550 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69  sideSlot Lookasi
4b560 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20  deSlot;.typedef 
4b570 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f  struct Module Mo
4b580 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74  dule;.typedef st
4b590 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ruct NameContext
4b5a0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79   NameContext;.ty
4b5b0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 72  pedef struct Par
4b5c0 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64 65  se Parse;.typede
4b5d0 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f 69  f struct Savepoi
4b5e0 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74 79  nt Savepoint;.ty
4b5f0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 6c  pedef struct Sel
4b600 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70 65  ect Select;.type
4b610 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c 69  def struct SrcLi
4b620 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 65  st SrcList;.type
4b630 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 63  def struct StrAc
4b640 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 79  cum StrAccum;.ty
4b650 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62  pedef struct Tab
4b660 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 65  le Table;.typede
4b670 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f  f struct TableLo
4b680 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 79  ck TableLock;.ty
4b690 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f 6b  pedef struct Tok
4b6a0 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 65  en Token;.typede
4b6b0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
4b6c0 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b 0a  Prg TriggerPrg;.
4b6d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
4b6e0 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 67  riggerStep Trigg
4b6f0 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 20  erStep;.typedef 
4b700 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 54  struct Trigger T
4b710 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 20  rigger;.typedef 
4b720 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52  struct UnpackedR
4b730 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 65  ecord UnpackedRe
4b740 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 74  cord;.typedef st
4b750 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61 62  ruct VTable VTab
4b760 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  le;.typedef stru
4b770 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72  ct Walker Walker
4b780 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b790 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65   WherePlan Where
4b7a0 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74  Plan;.typedef st
4b7b0 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57  ruct WhereInfo W
4b7c0 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65  hereInfo;.typede
4b7d0 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 65  f struct WhereLe
4b7e0 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a  vel WhereLevel;.
4b7f0 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75  ./*.** Defer sou
4b800 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64  rcing vdbe.h and
4b810 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61   btree.h until a
4b820 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e  fter the "u8" an
4b830 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c  d .** "BusyHandl
4b840 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 64  er" typedefs. vd
4b850 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72  be.h also requir
4b860 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20  es a few of the 
4b870 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65  opaque.** pointe
4b880 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75  r types (i.e. Fu
4b890 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 61  ncDef) defined a
4b8a0 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  bove..*/./******
4b8b0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
4b8c0 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20   btree.h in the 
4b8d0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
4b8e0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
4b8f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
4b900 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
4b910 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a  ile btree.h ****
4b920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b940 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
4b950 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
4b960 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
4b970 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
4b980 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
4b990 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
4b9a0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
4b9b0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
4b9c0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
4b9d0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
4b9e0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
4b9f0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
4ba00 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
4ba10 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
4ba20 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
4ba30 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
4ba40 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
4ba50 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
4ba60 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
4ba70 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
4ba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
4bac0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
4bad0 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
4bae0 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
4baf0 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65  he sqlite B-Tree
4bb00 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74   file.** subsyst
4bb10 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74  em.  See comment
4bb20 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  s in the source 
4bb30 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 69  code for a detai
4bb40 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a  led description.
4bb50 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 20  ** of what each 
4bb60 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  interface routin
4bb70 65 20 64 6f 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64  e does..*/.#ifnd
4bb80 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65  ef _BTREE_H_.#de
4bb90 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a  fine _BTREE_H_..
4bba0 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65  /* TODO: This de
4bbb0 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74  finition is just
4bbc0 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68   included so oth
4bbd0 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69  er modules compi
4bbe0 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20  le. It.** needs 
4bbf0 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e  to be revisited.
4bc00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
4bc10 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20  TE_N_BTREE_META 
4bc20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66  10../*.** If def
4bc30 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f  ined as non-zero
4bc40 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73  , auto-vacuum is
4bc50 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61   enabled by defa
4bc60 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a  ult. Otherwise.*
4bc70 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72  * it must be tur
4bc80 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20  ned on for each 
4bc90 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22  database using "
4bca0 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75  PRAGMA auto_vacu
4bcb0 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e  um = 1"..*/.#ifn
4bcc0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
4bcd0 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  LT_AUTOVACUUM.  
4bce0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
4bcf0 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55  EFAULT_AUTOVACUU
4bd00 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66  M 0.#endif..#def
4bd10 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41  ine BTREE_AUTOVA
4bd20 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20  CUUM_NONE 0     
4bd30 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20     /* Do not do 
4bd40 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23  auto-vacuum */.#
4bd50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54  define BTREE_AUT
4bd60 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20  OVACUUM_FULL 1  
4bd70 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c        /* Do full
4bd80 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a   auto-vacuum */.
4bd90 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55  #define BTREE_AU
4bda0 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20  TOVACUUM_INCR 2 
4bdb0 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d         /* Increm
4bdc0 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a  ental vacuum */.
4bdd0 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64  ./*.** Forward d
4bde0 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73  eclarations of s
4bdf0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65  tructure.*/.type
4be00 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65  def struct Btree
4be10 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20   Btree;.typedef 
4be20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20  struct BtCursor 
4be30 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65  BtCursor;.typede
4be40 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65  f struct BtShare
4be50 64 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65  d BtShared;.type
4be60 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65  def struct Btree
4be70 4d 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65  MutexArray Btree
4be80 4d 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a  MutexArray;../*.
4be90 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
4bea0 65 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66  e records all of
4beb0 20 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74   the Btrees that
4bec0 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a   need to hold.**
4bed0 20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20   a mutex before 
4bee0 77 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33  we enter sqlite3
4bef0 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65  VdbeExec().  The
4bf00 20 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61   Btrees are.** a
4bf10 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74  re placed in aBt
4bf20 72 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f  ree[] in order o
4bf30 66 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e  f aBtree[]->pBt.
4bf40 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77    That way,.** w
4bf50 65 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63  e can always loc
4bf60 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65  k and unlock the
4bf70 6d 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a  m all quickly..*
4bf80 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75  /.struct BtreeMu
4bf90 74 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74  texArray {.  int
4bfa0 20 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65   nMutex;.  Btree
4bfb0 20 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f   *aBtree[SQLITE_
4bfc0 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b  MAX_ATTACHED+1];
4bfd0 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49  .};...SQLITE_PRI
4bfe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4bff0 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e  BtreeOpen(.  con
4c000 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
4c010 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me,   /* Name of
4c020 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
4c030 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69  o open */.  sqli
4c040 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
4c050 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74       /* Associat
4c060 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ed database conn
4c070 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65  ection */.  Btre
4c080 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20  e **ppBtree,    
4c090 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f       /* Return o
4c0a0 70 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20  pen Btree* here 
4c0b0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4c0d0 20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20   Flags */.  int 
4c0e0 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20  vfsFlags        
4c0f0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
4c100 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20  ssed through to 
4c110 56 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a  VFS open */.);..
4c120 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72  /* The flags par
4c130 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
4c140 33 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62  3BtreeOpen can b
4c150 65 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72  e the bitwise or
4c160 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   of the.** follo
4c170 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a  wing values..**.
4c180 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20  ** NOTE:  These 
4c190 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63  values must matc
4c1a0 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  h the correspond
4c1b0 69 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65  ing PAGER_ value
4c1c0 73 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e  s in.** pager.h.
4c1d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45  .*/.#define BTRE
4c1e0 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20  E_OMIT_JOURNAL  
4c1f0 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65  1  /* Do not use
4c200 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72   journal.  No ar
4c210 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  gument */.#defin
4c220 65 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c  e BTREE_NO_READL
4c230 4f 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74  OCK   2  /* Omit
4c240 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65   readlocks on re
4c250 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a  adonly files */.
4c260 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45  #define BTREE_ME
4c270 4d 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f  MORY        4  /
4c280 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20  * In-memory DB. 
4c290 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a   No argument */.
4c2a0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45  #define BTREE_RE
4c2b0 41 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f  ADONLY      8  /
4c2c0 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62  * Open the datab
4c2d0 61 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79  ase in read-only
4c2e0 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65   mode */.#define
4c2f0 20 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45   BTREE_READWRITE
4c300 20 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20      16  /* Open 
4c310 66 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67  for both reading
4c320 20 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a   and writing */.
4c330 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52  #define BTREE_CR
4c340 45 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f  EATE       32  /
4c350 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74  * Create the dat
4c360 61 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73  abase if it does
4c370 20 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53   not exist */..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 43 6c  t sqlite3BtreeCl
4c3a0 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ose(Btree*);.SQL
4c3b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c3c0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
4c3d0 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c  acheSize(Btree*,
4c3e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4c3f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c400 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65  BtreeSetSafetyLe
4c410 76 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69  vel(Btree*,int,i
4c420 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4c430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c440 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64  treeSyncDisabled
4c450 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c470 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65  ite3BtreeSetPage
4c480 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69  Size(Btree *p, i
4c490 6e 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e  nt nPagesize, in
4c4a0 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20  t nReserve, int 
4c4b0 65 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52  eFix);.SQLITE_PR
4c4c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c4d0 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
4c4e0 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
4c4f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c500 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67  lite3BtreeMaxPag
4c510 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e  eCount(Btree*,in
4c520 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4c530 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c540 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74  reeGetReserve(Bt
4c550 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4c560 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c570 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63  3BtreeSetAutoVac
4c580 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74  uum(Btree *, int
4c590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c5a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c5b0 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeGetAutoVacuum(
4c5c0 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
4c5d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c5e0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
4c5f0 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b  ans(Btree*,int);
4c600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c610 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c620 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42  CommitPhaseOne(B
4c630 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  tree*, const cha
4c640 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c  r *zMaster);.SQL
4c650 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c660 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
4c670 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65  itPhaseTwo(Btree
4c680 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.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 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a  reeCommit(Btree*
4c6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c6d0 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65  eeRollback(Btree
4c6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4c6f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c700 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72  reeBeginStmt(Btr
4c710 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
4c720 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c730 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
4c740 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74  able(Btree*, int
4c750 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53  *, int flags);.S
4c760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c770 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
4c780 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b  InTrans(Btree*);
4c790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c7a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c7b0 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74  IsInReadTrans(Bt
4c7c0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4c7d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c7e0 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70  3BtreeIsInBackup
4c7f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c800 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
4c810 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
4c820 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20  a(Btree *, int, 
4c830 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29  void(*)(void *))
4c840 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c850 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c860 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74  eSchemaLocked(Bt
4c870 72 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51  ree *pBtree);.SQ
4c880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c890 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63   sqlite3BtreeLoc
4c8a0 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42  kTable(Btree *pB
4c8b0 74 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20  tree, int iTab, 
4c8c0 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b  u8 isWriteLock);
4c8d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c8e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c8f0 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20  Savepoint(Btree 
4c900 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53  *, int, int);..S
4c910 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
4c920 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
4c930 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d  3BtreeGetFilenam
4c940 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49  e(Btree *);.SQLI
4c950 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
4c960 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
4c970 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d  reeGetJournalnam
4c980 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49  e(Btree *);.SQLI
4c990 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c9a0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
4c9b0 69 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72  ile(Btree *, Btr
4c9c0 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ee *);..SQLITE_P
4c9d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c9e0 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
4c9f0 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20  m(Btree *);../* 
4ca00 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  The flags parame
4ca10 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74  ter to sqlite3Bt
4ca20 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63  reeCreateTable c
4ca30 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73  an be the bitwis
4ca40 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66  e OR.** of the f
4ca50 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a  ollowing flags:.
4ca60 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
4ca70 5f 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20  _INTKEY     1   
4ca80 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e   /* Table has on
4ca90 6c 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ly 64-bit signed
4caa0 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f   integer keys */
4cab0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a  .#define BTREE_Z
4cac0 45 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f  ERODATA   2    /
4cad0 2a 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73  * Table has keys
4cae0 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20   only - no data 
4caf0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
4cb00 5f 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20  _LEAFDATA   4   
4cb10 20 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20   /* Data stored 
4cb20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20  in leaves only. 
4cb30 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20   Implies INTKEY 
4cb40 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
4cb50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4cb60 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72  reeDropTable(Btr
4cb70 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b  ee*, int, int*);
4cb80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4cb90 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4cba0 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65  ClearTable(Btree
4cbb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53  *, int, int*);.S
4cbc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4cbd0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54  id sqlite3BtreeT
4cbe0 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  ripAllCursors(Bt
4cbf0 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c  ree*, int);..SQL
4cc00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4cc10 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
4cc20 4d 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72  Meta(Btree *pBtr
4cc30 65 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  ee, int idx, u32
4cc40 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54   *pValue);.SQLIT
4cc50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4cc60 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
4cc70 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74  Meta(Btree*, int
4cc80 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29   idx, u32 value)
4cc90 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63  ;../*.** The sec
4cca0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ond parameter to
4ccb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
4ccc0 4d 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42  Meta or sqlite3B
4ccd0 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a  treeUpdateMeta.*
4cce0 2a 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20  * should be one 
4ccf0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
4cd00 20 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74   values. The int
4cd10 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20  eger values are 
4cd20 61 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20  assigned .** to 
4cd30 63 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61  constants so tha
4cd40 74 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20  t the offset of 
4cd50 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
4cd60 67 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a  g field in an.**
4cd70 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
4cd80 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66   header may be f
4cd90 6f 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66  ound using the f
4cda0 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61  ollowing formula
4cdb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74  :.**.**   offset
4cdc0 20 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34   = 36 + (idx * 4
4cdd0 29 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ).**.** For exam
4cde0 70 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61  ple, the free-pa
4cdf0 67 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69  ge-count field i
4ce00 73 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74  s located at byt
4ce10 65 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a  e offset 36 of.*
4ce20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
4ce30 69 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20  ile header. The 
4ce40 69 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67  incr-vacuum-flag
4ce50 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65   field is locate
4ce60 64 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66  d at.** byte off
4ce70 73 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a  set 64 (== 36+4*
4ce80 37 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42  7)..*/.#define B
4ce90 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43  TREE_FREE_PAGE_C
4cea0 4f 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69  OUNT     0.#defi
4ceb0 6e 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  ne BTREE_SCHEMA_
4cec0 56 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23  VERSION      1.#
4ced0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c  define BTREE_FIL
4cee0 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20  E_FORMAT        
4cef0 20 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45   2.#define BTREE
4cf00 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
4cf10 49 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42  IZE  3.#define B
4cf20 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f  TREE_LARGEST_ROO
4cf30 54 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69  T_PAGE   4.#defi
4cf40 6e 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e  ne BTREE_TEXT_EN
4cf50 43 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23  CODING       5.#
4cf60 64 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45  define BTREE_USE
4cf70 52 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20  R_VERSION       
4cf80 20 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45   6.#define BTREE
4cf90 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20  _INCR_VACUUM    
4cfa0 20 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50       7..SQLITE_P
4cfb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4cfc0 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20  e3BtreeCursor(. 
4cfd0 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20   Btree*,        
4cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cff0 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63        /* BTree c
4d000 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20  ontaining table 
4d010 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74  to open */.  int
4d020 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d040 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f    /* Index of ro
4d050 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ot page */.  int
4d060 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20   wrFlag,        
4d070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d080 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69    /* 1 for writi
4d090 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d  ng.  0 for read-
4d0a0 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  only */.  struct
4d0b0 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20   KeyInfo*,      
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4d0d0 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
4d0e0 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63   to compare func
4d0f0 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73  tion */.  BtCurs
4d100 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20  or *pCursor     
4d110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4d120 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
4d130 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72   cursor structur
4d140 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  e */.);.SQLITE_P
4d150 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d160 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
4d170 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  e(void);.SQLITE_
4d180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4d190 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a  ite3BtreeCursorZ
4d1a0 65 72 6f 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  ero(BtCursor*);.
4d1b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d1c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d1d0 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75  CloseCursor(BtCu
4d1e0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
4d1f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d200 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
4d210 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f  acked(.  BtCurso
4d220 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  r*,.  UnpackedRe
4d230 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20  cord *pUnKey,.  
4d240 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e  i64 intKey,.  in
4d250 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70  t bias,.  int *p
4d260 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Res.);.SQLITE_PR
4d270 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d280 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d  3BtreeCursorHasM
4d290 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20  oved(BtCursor*, 
4d2a0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
4d2b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d2c0 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43  3BtreeDelete(BtC
4d2d0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
4d2e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d2f0 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42  te3BtreeInsert(B
4d300 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20  tCursor*, const 
4d310 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20  void *pKey, i64 
4d320 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  nKey,.          
4d330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d340 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f          const vo
4d350 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  id *pData, int n
4d360 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d380 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72          int nZer
4d390 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69 6e 74  o, int bias, int
4d3a0 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 53 51   seekResult);.SQ
4d3b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d3c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
4d3d0 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e  st(BtCursor*, in
4d3e0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45  t *pRes);.SQLITE
4d3f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d400 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74  ite3BtreeLast(Bt
4d410 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52  Cursor*, int *pR
4d420 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  es);.SQLITE_PRIV
4d430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d440 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f  treeNext(BtCurso
4d450 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a  r*, int *pRes);.
4d460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d470 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  nt sqlite3BtreeE
4d480 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  of(BtCursor*);.S
4d490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d4a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  t sqlite3BtreePr
4d4b0 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a  evious(BtCursor*
4d4c0 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51  , int *pRes);.SQ
4d4d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d4e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
4d4f0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20  Size(BtCursor*, 
4d500 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c  i64 *pSize);.SQL
4d510 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d520 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
4d530 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f  BtCursor*, u32 o
4d540 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
4d550 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  void*);.SQLITE_P
4d560 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
4d570 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b  d *sqlite3BtreeK
4d580 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  eyFetch(BtCursor
4d590 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53  *, int *pAmt);.S
4d5a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
4d5b0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
4d5c0 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28  3BtreeDataFetch(
4d5d0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
4d5e0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pAmt);.SQLITE_PR
4d5f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d600 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42  3BtreeDataSize(B
4d610 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70  tCursor*, u32 *p
4d620 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Size);.SQLITE_PR
4d630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d640 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72  3BtreeData(BtCur
4d650 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74  sor*, u32 offset
4d660 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a  , u32 amt, void*
4d670 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d680 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
4d690 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69  reeSetCachedRowi
4d6a0 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c  d(BtCursor*, sql
4d6b0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c  ite3_int64);.SQL
4d6c0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
4d6d0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
4d6e0 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52  3BtreeGetCachedR
4d6f0 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29 3b  owid(BtCursor*);
4d700 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
4d710 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
4d720 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
4d730 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61  k(Btree*, int *a
4d740 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c  Root, int nRoot,
4d750 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c   int, int*);.SQL
4d760 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 75  ITE_PRIVATE stru
4d770 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65  ct Pager *sqlite
4d780 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65  3BtreePager(Btre
4d790 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  e*);..SQLITE_PRI
4d7a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d7b0 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43  BtreePutData(BtC
4d7c0 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
4d7d0 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
4d7e0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
4d7f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4d800 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c  BtreeCacheOverfl
4d810 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a  ow(BtCursor *);.
4d820 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4d830 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4d840 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75  ClearCursor(BtCu
4d850 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65  rsor *);..#ifnde
4d860 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  f NDEBUG.SQLITE_
4d870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d880 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
4d890 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 2a 29  Valid(BtCursor*)
4d8a0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ;.#endif..#ifnde
4d8b0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  f SQLITE_OMIT_BT
4d8c0 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f  REECOUNT.SQLITE_
4d8d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d8e0 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74  te3BtreeCount(Bt
4d8f0 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29  Cursor *, i64 *)
4d900 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
4d910 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
4d920 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d930 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
4d940 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a  orInfo(BtCursor*
4d950 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  , int*, int);.SQ
4d960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4d970 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  d sqlite3BtreeCu
4d980 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a 29  rsorList(Btree*)
4d990 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
4d9a0 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 73  If we are not us
4d9b0 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 65  ing shared cache
4d9c0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
4d9d0 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73  no need to.** us
4d9e0 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 63  e mutexes to acc
4d9f0 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 64  ess the BtShared
4da00 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 6f   structures.  So
4da10 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74   make the.** Ent
4da20 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 6f  er and Leave pro
4da30 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a  cedures no-ops..
4da40 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef 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 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CHE.SQLITE_PRIVA
4da70 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
4da80 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65  3BtreeEnter(Btre
4da90 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4daa0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4dab0 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
4dac0 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65  sqlite3*);.#else
4dad0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4dae0 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 20 0a  3BtreeEnter(X) .
4daf0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4db00 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29  BtreeEnterAll(X)
4db10 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
4db20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
4db30 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20  T_SHARED_CACHE) 
4db40 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
4db50 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 49 56  SAFE.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 4c 65 61 76 65 28 42 74 72  e3BtreeLeave(Btr
4db80 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4db90 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4dba0 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72  te3BtreeEnterCur
4dbb0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  sor(BtCursor*);.
4dbc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4dbd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4dbe0 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74  eeLeaveCursor(Bt
4dbf0 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
4dc00 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4dc10 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
4dc20 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a  eAll(sqlite3*);.
4dc30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4dc40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4dc50 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
4dc60 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  r(BtreeMutexArra
4dc70 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  y*);.SQLITE_PRIV
4dc80 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
4dc90 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
4dca0 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74 65  yLeave(BtreeMute
4dcb0 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45  xArray*);.SQLITE
4dcc0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4dcd0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4dce0 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72  xArrayInsert(Btr
4dcf0 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c 20 42  eeMutexArray*, B
4dd00 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20  tree*);.#ifndef 
4dd10 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 73  NDEBUG.  /* Thes
4dd20 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
4dd30 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72  sed inside asser
4dd40 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  t() statements o
4dd50 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly. */.SQLITE_P
4dd60 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
4dd70 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
4dd80 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  tex(Btree*);.SQL
4dd90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
4dda0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  t sqlite3BtreeHo
4ddb0 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71  ldsAllMutexes(sq
4ddc0 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69 66 0a  lite3*);.#endif.
4ddd0 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
4dde0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
4ddf0 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  e(X).# define sq
4de00 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43  lite3BtreeEnterC
4de10 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69 6e  ursor(X).# defin
4de20 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  e sqlite3BtreeLe
4de30 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20 64  aveCursor(X).# d
4de40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
4de50 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a 23 20  eeLeaveAll(X).# 
4de60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4de70 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
4de80 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  er(X).# define s
4de90 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4dea0 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 20  ArrayLeave(X).# 
4deb0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4dec0 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
4ded0 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65 66 69  ert(X,Y)..# defi
4dee0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  ne sqlite3BtreeH
4def0 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a 23  oldsMutex(X) 1.#
4df00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4df10 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
4df20 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a  xes(X) 1.#endif.
4df30 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52  ..#endif /* _BTR
4df40 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  EE_H_ */../*****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
4df60 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   btree.h *******
4df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4df80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
4dfb0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
4dfc0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
4dfd0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
4e000 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 20  e vdbe.h in the 
4e010 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
4e020 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4e040 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
4e050 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  file vdbe.h ****
4e060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e080 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
4e090 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
4e0a0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
4e0b0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
4e0c0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
4e0d0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
4e0e0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
4e0f0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
4e100 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
4e110 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
4e120 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
4e130 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
4e140 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
4e150 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
4e160 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
4e170 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
4e180 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
4e190 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
4e1a0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
4e1b0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
4e1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e200 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 20  .** Header file 
4e210 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c 20  for the Virtual 
4e220 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 20  DataBase Engine 
4e230 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69  (VDBE).**.** Thi
4e240 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 73  s header defines
4e250 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
4e260 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 61  o the virtual da
4e270 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a  tabase engine.**
4e280 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 56   or VDBE.  The V
4e290 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  DBE implements a
4e2a0 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 69  n abstract machi
4e2b0 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a 2a  ne that runs a.*
4e2c0 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d  * simple program
4e2d0 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 6d   to access and m
4e2e0 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 6c  odify the underl
4e2f0 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a  ying database..*
4e300 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
4e310 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 6e  E_VDBE_H_.#defin
4e320 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48  e _SQLITE_VDBE_H
4e330 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c  _../*.** A singl
4e340 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61  e VDBE is an opa
4e350 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  que structure na
4e360 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c  med "Vdbe".  Onl
4e370 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e  y routines.** in
4e380 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65   the source file
4e390 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 72   sqliteVdbe.c ar
4e3a0 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65  e allowed to see
4e3b0 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a 20   the insides.** 
4e3c0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
4e3d0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
4e3e0 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b 0a  ruct Vdbe Vdbe;.
4e3f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 73  ./*.** The names
4e400 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
4e410 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 64  g types declared
4e420 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 72   in vdbeInt.h ar
4e430 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 6f  e required.** fo
4e440 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 66  r the VdbeOp def
4e450 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65  inition..*/.type
4e460 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 46  def struct VdbeF
4e470 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 79  unc VdbeFunc;.ty
4e480 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
4e490 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20 73 74   Mem;.typedef st
4e4a0 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20  ruct SubProgram 
4e4b0 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f 2a 0a  SubProgram;../*.
4e4c0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 74  ** A single inst
4e4d0 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ruction of the v
4e4e0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68  irtual machine h
4e4f0 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20  as an opcode.** 
4e500 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 74  and as many as t
4e510 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 20  hree operands.  
4e520 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  The instruction 
4e530 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 61  is recorded.** a
4e540 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
4e550 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
4e560 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 72  tructure:.*/.str
4e570 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 75  uct VdbeOp {.  u
4e580 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20  8 opcode;       
4e590 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61     /* What opera
4e5a0 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20  tion to perform 
4e5b0 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72  */.  signed char
4e5c0 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 20   p4type; /* One 
4e5d0 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 6f  of the P4_xxx co
4e5e0 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 2a  nstants for p4 *
4e5f0 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b 20  /.  u8 opflags; 
4e600 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
4e610 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 2a 20 66  of the OPFLG_* f
4e620 6c 61 67 73 20 69 6e 20 6f 70 63 6f 64 65 73 2e  lags in opcodes.
4e630 68 20 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20  h */.  u8 p5;   
4e640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
4e650 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  fth parameter is
4e660 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61   an unsigned cha
4e670 72 61 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  racter */.  int 
4e680 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p1;             
4e690 2f 2a 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64  /* First operand
4e6a0 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20   */.  int p2;   
4e6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
4e6c0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f  ond parameter (o
4e6d0 66 74 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65  ften the jump de
4e6e0 73 74 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20  stination) */.  
4e6f0 69 6e 74 20 70 33 3b 20 20 20 20 20 20 20 20 20  int p3;         
4e700 20 20 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64      /* The third
4e710 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20   parameter */.  
4e720 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
4e730 20 20 20 20 2f 2a 20 66 6f 75 72 74 68 20 70 61      /* fourth pa
4e740 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 20 20 69  rameter */.    i
4e750 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
4e760 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
4e770 20 76 61 6c 75 65 20 69 66 20 70 34 74 79 70 65   value if p4type
4e780 3d 3d 50 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20  ==P4_INT32 */.  
4e790 20 20 76 6f 69 64 20 2a 70 3b 20 20 20 20 20 20    void *p;      
4e7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65           /* Gene
4e7b0 72 69 63 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ric pointer */. 
4e7c0 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20     char *z;     
4e7d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
4e7e0 6e 74 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72  nter to data for
4e7f0 20 73 74 72 69 6e 67 20 28 63 68 61 72 20 61 72   string (char ar
4e800 72 61 79 29 20 74 79 70 65 73 20 2a 2f 0a 20 20  ray) types */.  
4e810 20 20 69 36 34 20 2a 70 49 36 34 3b 20 20 20 20    i64 *pI64;    
4e820 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
4e830 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e840 50 34 5f 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20  P4_INT64 */.    
4e850 64 6f 75 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20  double *pReal;  
4e860 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
4e870 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
4e880 5f 52 45 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e  _REAL */.    Fun
4e890 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
4e8a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4e8b0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 46 55   p4type is P4_FU
4e8c0 4e 43 44 45 46 20 2a 2f 0a 20 20 20 20 56 64 62  NCDEF */.    Vdb
4e8d0 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63  eFunc *pVdbeFunc
4e8e0 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e  ;   /* Used when
4e8f0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56 44   p4type is P4_VD
4e900 42 45 46 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f  BEFUNC */.    Co
4e910 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
4e920 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4e930 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 43  n p4type is P4_C
4e940 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65  OLLSEQ */.    Me
4e950 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
4e960 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4e970 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4d  n p4type is P4_M
4e980 45 4d 20 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65  EM */.    VTable
4e990 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 20 20   *pVtab;        
4e9a0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
4e9b0 74 79 70 65 20 69 73 20 50 34 5f 56 54 41 42 20  type is P4_VTAB 
4e9c0 2a 2f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  */.    KeyInfo *
4e9d0 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a  pKeyInfo;     /*
4e9e0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4e9f0 65 20 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20  e is P4_KEYINFO 
4ea00 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69 3b 20  */.    int *ai; 
4ea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4ea20 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
4ea30 65 20 69 73 20 50 34 5f 49 4e 54 41 52 52 41 59  e is P4_INTARRAY
4ea40 20 2a 2f 0a 20 20 20 20 53 75 62 50 72 6f 67 72   */.    SubProgr
4ea50 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f  am *pProgram;  /
4ea60 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4ea70 70 65 20 69 73 20 50 34 5f 53 55 42 50 52 4f 47  pe is P4_SUBPROG
4ea80 52 41 4d 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23  RAM */.  } p4;.#
4ea90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
4eaa0 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d  UG.  char *zComm
4eab0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ent;          /*
4eac0 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72   Comment to impr
4ead0 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74 79 20  ove readability 
4eae0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
4eaf0 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20   VDBE_PROFILE.  
4eb00 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20  int cnt;        
4eb10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
4eb20 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 69 73  er of times this
4eb30 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73   instruction was
4eb40 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 75   executed */.  u
4eb50 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20 20 20  64 cycles;      
4eb60 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
4eb70 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78 65 63   time spent exec
4eb80 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72  uting this instr
4eb90 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  uction */.#endif
4eba0 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
4ebb0 63 74 20 56 64 62 65 4f 70 20 56 64 62 65 4f 70  ct VdbeOp VdbeOp
4ebc0 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d  ;.../*.** A sub-
4ebd0 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20  routine used to 
4ebe0 69 6d 70 6c 65 6d 65 6e 74 20 61 20 74 72 69 67  implement a trig
4ebf0 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  ger program..*/.
4ec00 73 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61  struct SubProgra
4ec10 6d 20 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f  m {.  VdbeOp *aO
4ec20 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
4ec30 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
4ec40 6f 70 63 6f 64 65 73 20 66 6f 72 20 73 75 62 2d  opcodes for sub-
4ec50 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
4ec60 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20   nOp;           
4ec70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6c             /* El
4ec80 65 6d 65 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20  ements in aOp[] 
4ec90 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20  */.  int nMem;  
4eca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ecb0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
4ecc0 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 72 65 71  memory cells req
4ecd0 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  uired */.  int n
4ece0 43 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Csr;            
4ecf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
4ed00 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65  er of cursors re
4ed10 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  quired */.  int 
4ed20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
4ed30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
4ed40 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ber of pointers 
4ed50 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
4ed60 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b  e */.  void *tok
4ed70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
4ed80 20 20 20 20 20 2f 2a 20 69 64 20 74 68 61 74 20       /* id that 
4ed90 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 72  may be used to r
4eda0 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72  ecursive trigger
4edb0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  s */.};../*.** A
4edc0 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e   smaller version
4edd0 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64 20   of VdbeOp used 
4ede0 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64 4f  for the VdbeAddO
4edf0 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f 6e  pList() function
4ee00 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 20 74   because.** it t
4ee10 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73 70 61  akes up less spa
4ee20 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  ce..*/.struct Vd
4ee30 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38 20  beOpList {.  u8 
4ee40 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20 20  opcode;         
4ee50 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74 69   /* What operati
4ee60 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f  on to perform */
4ee70 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70  .  signed char p
4ee80 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20  1;     /* First 
4ee90 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67  operand */.  sig
4eea0 6e 65 64 20 63 68 61 72 20 70 32 3b 20 20 20 20  ned char p2;    
4eeb0 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d   /* Second param
4eec0 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20  eter (often the 
4eed0 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e  jump destination
4eee0 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  ) */.  signed ch
4eef0 61 72 20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68  ar p3;     /* Th
4ef00 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  ird parameter */
4ef10 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
4ef20 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 56 64  ct VdbeOpList Vd
4ef30 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a  beOpList;../*.**
4ef40 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
4ef50 6f 66 20 56 64 62 65 4f 70 2e 70 34 74 79 70 65  of VdbeOp.p4type
4ef60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e  .*/.#define P4_N
4ef70 4f 54 55 53 45 44 20 20 20 20 30 20 20 20 2f 2a  OTUSED    0   /*
4ef80 20 54 68 65 20 50 34 20 70 61 72 61 6d 65 74 65   The P4 paramete
4ef90 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f  r is not used */
4efa0 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 59 4e 41  .#define P4_DYNA
4efb0 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f  MIC  (-1)  /* Po
4efc0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e  inter to a strin
4efd0 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  g obtained from 
4efe0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a  sqliteMalloc() *
4eff0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 54 41  /.#define P4_STA
4f000 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20 50  TIC   (-2)  /* P
4f010 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
4f020 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65  ic string */.#de
4f030 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20  fine P4_COLLSEQ 
4f040 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 69 73 20   (-4)  /* P4 is 
4f050 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43  a pointer to a C
4f060 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 65  ollSeq structure
4f070 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 46   */.#define P4_F
4f080 55 4e 43 44 45 46 20 20 28 2d 35 29 20 20 2f 2a  UNCDEF  (-5)  /*
4f090 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
4f0a0 20 74 6f 20 61 20 46 75 6e 63 44 65 66 20 73 74   to a FuncDef st
4f0b0 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69  ructure */.#defi
4f0c0 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28  ne P4_KEYINFO  (
4f0d0 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20  -6)  /* P4 is a 
4f0e0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79  pointer to a Key
4f0f0 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a  Info structure *
4f100 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 44 42  /.#define P4_VDB
4f110 45 46 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50  EFUNC (-7)  /* P
4f120 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
4f130 6f 20 61 20 56 64 62 65 46 75 6e 63 20 73 74 72  o a VdbeFunc str
4f140 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
4f150 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d  e P4_MEM      (-
4f160 38 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  8)  /* P4 is a p
4f170 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a  ointer to a Mem*
4f180 20 20 20 20 73 74 72 75 63 74 75 72 65 20 2a 2f      structure */
4f190 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 52 41 4e  .#define P4_TRAN
4f1a0 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34  SIENT (-9) /* P4
4f1b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4f1c0 20 61 20 74 72 61 6e 73 69 65 6e 74 20 73 74 72   a transient str
4f1d0 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ing */.#define P
4f1e0 34 5f 56 54 41 42 20 20 20 20 20 28 2d 31 30 29  4_VTAB     (-10)
4f1f0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4f200 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
4f210 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
4f220 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d   */.#define P4_M
4f230 50 52 49 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a  PRINTF  (-11) /*
4f240 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20   P4 is a string 
4f250 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
4f260 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20  lite3_mprintf() 
4f270 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 52 45  */.#define P4_RE
4f280 41 4c 20 20 20 20 20 28 2d 31 32 29 20 2f 2a 20  AL     (-12) /* 
4f290 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 66  P4 is a 64-bit f
4f2a0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
4f2b0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  lue */.#define P
4f2c0 34 5f 49 4e 54 36 34 20 20 20 20 28 2d 31 33 29  4_INT64    (-13)
4f2d0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62   /* P4 is a 64-b
4f2e0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
4f2f0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  r */.#define P4_
4f300 49 4e 54 33 32 20 20 20 20 28 2d 31 34 29 20 2f  INT32    (-14) /
4f310 2a 20 50 34 20 69 73 20 61 20 33 32 2d 62 69 74  * P4 is a 32-bit
4f320 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
4f330 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e  */.#define P4_IN
4f340 54 41 52 52 41 59 20 28 2d 31 35 29 20 2f 2a 20  TARRAY (-15) /* 
4f350 50 34 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f  P4 is a vector o
4f360 66 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  f 32-bit integer
4f370 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  s */.#define P4_
4f380 53 55 42 50 52 4f 47 52 41 4d 20 20 28 2d 31 38  SUBPROGRAM  (-18
4f390 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69  ) /* P4 is a poi
4f3a0 6e 74 65 72 20 74 6f 20 61 20 53 75 62 50 72 6f  nter to a SubPro
4f3b0 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 20 2a  gram structure *
4f3c0 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e  /../* When addin
4f3d0 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20  g a P4 argument 
4f3e0 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f  using P4_KEYINFO
4f3f0 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  , a copy of the 
4f400 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
4f410 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54  e.** is made.  T
4f420 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65  hat copy is free
4f430 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
4f440 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42  is finalized.  B
4f450 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67  ut if the.** arg
4f460 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49  ument is P4_KEYI
4f470 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65  NFO_HANDOFF, the
4f480 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74   passed in point
4f490 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20  er is used.  It 
4f4a0 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72  still.** gets fr
4f4b0 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62  eed when the Vdb
4f4c0 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73  e is finalized s
4f4d0 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c  o it still shoul
4f4e0 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  d be obtained.**
4f4f0 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73   from a single s
4f500 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
4f510 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d  But no copy is m
4f520 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c  ade and the call
4f530 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ing.** function 
4f540 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79  should *not* try
4f550 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79   to free the Key
4f560 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  Info..*/.#define
4f570 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
4f580 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e  OFF (-16).#defin
4f590 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41  e P4_KEYINFO_STA
4f5a0 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a  TIC  (-17)../*.*
4f5b0 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e  * The Vdbe.aColN
4f5c0 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69  ame array contai
4f5d0 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74  ns 5n Mem struct
4f5e0 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73  ures, where n is
4f5f0 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20   the .** number 
4f600 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61  of columns of da
4f610 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ta returned by t
4f620 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  he statement..*/
4f630 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4f640 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66  _NAME     0.#def
4f650 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  ine COLNAME_DECL
4f660 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43  TYPE 1.#define C
4f670 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20  OLNAME_DATABASE 
4f680 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  2.#define COLNAM
4f690 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65  E_TABLE    3.#de
4f6a0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c  fine COLNAME_COL
4f6b0 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53  UMN   4.#ifdef S
4f6c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
4f6d0 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64  UMN_METADATA.# d
4f6e0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4f6f0 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a         5      /*
4f700 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41   Number of COLNA
4f710 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a  ME_xxx symbols *
4f720 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20  /.#else.# ifdef 
4f730 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
4f740 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20  TYPE.#   define 
4f750 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31  COLNAME_N      1
4f760 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f        /* Store o
4f770 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a  nly the name */.
4f780 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e  # else.#   defin
4f790 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20  e COLNAME_N     
4f7a0 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65   2      /* Store
4f7b0 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65   the name and de
4f7c0 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69  cltype */.# endi
4f7d0 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
4f7e0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
4f7f0 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72  cro converts a r
4f800 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20  elative address 
4f810 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a  in the p2 field.
4f820 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73  ** of a VdbeOp s
4f830 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20  tructure into a 
4f840 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
4f850 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69  so that .** sqli
4f860 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
4f870 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68  () knows that th
4f880 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c  e address is rel
4f890 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a  ative.  Calling.
4f8a0 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61  ** the macro aga
4f8b0 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20  in restores the 
4f8c0 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66  address..*/.#def
4f8d0 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31  ine ADDR(X)  (-1
4f8e0 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  -(X))../*.** The
4f8f0 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20   makefile scans 
4f900 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63  the vdbe.c sourc
4f910 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74  e file and creat
4f920 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e  es the "opcodes.
4f930 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c  h".** header fil
4f940 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61  e that defines a
4f950 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68   number for each
4f960 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20   opcode used by 
4f970 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a  the VDBE..*/./**
4f980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
4f990 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69  lude opcodes.h i
4f9a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4f9b0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.h *********
4f9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
4f9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
4f9e0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e  in file opcodes.
4f9f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4fa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
4fa20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  Automatically ge
4fa30 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  nerated.  Do not
4fa40 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20   edit */./* See 
4fa50 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
4fa60 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
4fa70 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ails */.#define 
4fa80 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20  OP_Goto         
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faa0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
4fab0 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20  e OP_Gosub      
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fad0 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
4fae0 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20  ine OP_Return   
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb00 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64              3.#d
4fb10 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20  efine OP_Yield  
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
4fb40 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49  #define OP_HaltI
4fb50 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  fNull           
4fb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb70 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c  5.#define OP_Hal
4fb80 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fba0 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49    6.#define OP_I
4fbb0 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20 20  nteger          
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbd0 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50      7.#define OP
4fbe0 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _Int64          
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc00 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
4fc10 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20  OP_Real         
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc30 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73        130   /* s
4fc40 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20  ame as TK_FLOAT 
4fc50 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4fc60 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 20 20  _String8        
4fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc80 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 61 6d       94   /* sam
4fc90 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 20  e as TK_STRING  
4fca0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4fcb0 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20 20  tring           
4fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcd0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50      9.#define OP
4fce0 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  _Null           
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd00 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20       10.#define 
4fd10 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20  OP_Blob         
4fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd30 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e         11.#defin
4fd40 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20  e OP_Variable   
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd60 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66           12.#def
4fd70 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20  ine OP_Move     
4fd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd90 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64             13.#d
4fda0 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20  efine OP_Copy   
4fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a               14.
4fdd0 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79  #define OP_SCopy
4fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4fe00 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73  5.#define OP_Res
4fe10 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20  ultRow          
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe30 20 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   16.#define OP_C
4fe40 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20  oncat           
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe60 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65 20     91   /* same 
4fe70 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a  as TK_CONCAT   *
4fe80 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64  /.#define OP_Add
4fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4feb0 20 38 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   86   /* same as
4fec0 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a   TK_PLUS     */.
4fed0 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74 72  #define OP_Subtr
4fee0 61 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20  act             
4fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
4ff00 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
4ff10 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64  K_MINUS    */.#d
4ff20 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c  efine OP_Multipl
4ff30 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20               88 
4ff50 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4ff60 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66  STAR     */.#def
4ff70 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20  ine OP_Divide   
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff90 20 20 20 20 20 20 20 20 20 20 20 38 39 20 20 20             89   
4ffa0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
4ffb0 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  ASH    */.#defin
4ffc0 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 20  e OP_Remainder  
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffe0 20 20 20 20 20 20 20 20 20 39 30 20 20 20 2f 2a           90   /*
4fff0 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20   same as TK_REM 
50000 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
50010 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20  OP_CollSeq      
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50030 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e         17.#defin
50040 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20  e OP_Function   
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50060 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66           18.#def
50070 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20  ine OP_BitAnd   
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50090 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20 20             82   
500a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
500b0 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e  TAND   */.#defin
500c0 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 20  e OP_BitOr      
500d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500e0 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f 2a           83   /*
500f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f   same as TK_BITO
50100 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  R    */.#define 
50110 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20  OP_ShiftLeft    
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50130 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73         84   /* s
50140 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54  ame as TK_LSHIFT
50150 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
50160 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20 20  _ShiftRight     
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50180 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 61 6d       85   /* sam
50190 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20 20  e as TK_RSHIFT  
501a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41   */.#define OP_A
501b0 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20  ddImm           
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501d0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50     20.#define OP
501e0 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 20 20 20  _MustBeInt      
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50200 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20       21.#define 
50210 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20  OP_RealAffinity 
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50230 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e         22.#defin
50240 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20  e OP_ToText     
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50260 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a          141   /*
50270 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54   same as TK_TO_T
50280 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  EXT  */.#define 
50290 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20  OP_ToBlob       
502a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502b0 20 20 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73        142   /* s
502c0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f  ame as TK_TO_BLO
502d0 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  B  */.#define OP
502e0 5f 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20  _ToNumeric      
502f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50300 20 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d      143   /* sam
50310 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52  e as TK_TO_NUMER
50320 49 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f  IC*/.#define OP_
50330 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20  ToInt           
50340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50350 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d 65     144   /* same
50360 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20   as TK_TO_INT   
50370 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  */.#define OP_To
50380 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20  Real            
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503a0 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 20 61   145   /* same a
503b0 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a 2f  s TK_TO_REAL  */
503c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 20  .#define OP_Eq  
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503f0 37 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  76   /* same as 
50400 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a 23  TK_EQ       */.#
50410 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20  define OP_Ne    
50420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35                75
50440 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50450 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _NE       */.#de
50460 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20  fine OP_Lt      
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50480 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20 20              79  
50490 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
504a0 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  T       */.#defi
504b0 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20  ne OP_Le        
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504d0 20 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f            78   /
504e0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20  * same as TK_LE 
504f0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50500 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 20   OP_Gt          
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50520 20 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20          77   /* 
50530 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 20  same as TK_GT   
50540 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
50550 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 20  P_Ge            
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50570 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61        80   /* sa
50580 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 20  me as TK_GE     
50590 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
505a0 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 20 20  Permutation     
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505c0 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f      23.#define O
505d0 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20  P_Compare       
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505f0 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65        24.#define
50600 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20   OP_Jump        
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50620 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69          25.#defi
50630 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20  ne OP_And       
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50650 20 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f            69   /
50660 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44  * same as TK_AND
50670 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50680 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 20   OP_Or          
50690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506a0 20 20 20 20 20 20 20 20 36 38 20 20 20 2f 2a 20          68   /* 
506b0 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 20  same as TK_OR   
506c0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
506d0 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20  P_Not           
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506f0 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73 61        19   /* sa
50700 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20  me as TK_NOT    
50710 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
50720 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20  BitNot          
50730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50740 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65      93   /* same
50750 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20   as TK_BITNOT   
50760 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  */.#define OP_If
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50790 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    26.#define OP_
507a0 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20  IfNot           
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507c0 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f      27.#define O
507d0 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20 20 20  P_IsNull        
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507f0 20 20 20 20 20 20 37 33 20 20 20 2f 2a 20 73 61        73   /* sa
50800 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20  me as TK_ISNULL 
50810 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
50820 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20  NotNull         
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50840 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d 65      74   /* same
50850 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20   as TK_NOTNULL  
50860 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  */.#define OP_Co
50870 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20  lumn            
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50890 20 20 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f    28.#define OP_
508a0 41 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20  Affinity        
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508c0 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 4f      29.#define O
508d0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20  P_MakeRecord    
508e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508f0 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65        30.#define
50900 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20 20 20   OP_Count       
50910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50920 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69          31.#defi
50930 6e 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20  ne OP_Savepoint 
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50950 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65            32.#de
50960 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d  fine OP_AutoComm
50970 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  it              
50980 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 23              33.#
50990 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61  define OP_Transa
509a0 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ction           
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34                34
509c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 64  .#define OP_Read
509d0 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20  Cookie          
509e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509f0 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  35.#define OP_Se
50a00 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20  tCookie         
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    36.#define OP_
50a30 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20  VerifyCookie    
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a50 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 4f      37.#define O
50a60 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20  P_OpenRead      
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a80 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65        38.#define
50a90 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20   OP_OpenWrite   
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ab0 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69          39.#defi
50ac0 6e 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  ne OP_OpenEpheme
50ad0 72 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ral             
50ae0 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65            40.#de
50af0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  fine OP_OpenPseu
50b00 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  do              
50b10 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23              41.#
50b20 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20  define OP_Close 
50b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32                42
50b50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b  .#define OP_Seek
50b60 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Lt              
50b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b80 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  43.#define OP_Se
50b90 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  ekLe            
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bb0 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f    44.#define OP_
50bc0 53 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20  SeekGe          
50bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50be0 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20 4f      45.#define O
50bf0 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20  P_SeekGt        
50c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c10 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65        46.#define
50c20 20 4f 50 5f 53 65 65 6b 20 20 20 20 20 20 20 20   OP_Seek        
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c40 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69          47.#defi
50c50 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20  ne OP_NotFound  
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c70 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65            48.#de
50c80 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20  fine OP_Found   
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ca0 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 23              49.#
50cb0 64 65 66 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71  define OP_IsUniq
50cc0 75 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ue              
50cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30                50
50ce0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45  .#define OP_NotE
50cf0 78 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20  xists           
50d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d10 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  51.#define OP_Se
50d20 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20 20  quence          
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d40 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    52.#define OP_
50d50 4e 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20  NewRowid        
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d70 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f      53.#define O
50d80 50 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20  P_Insert        
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50da0 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65        54.#define
50db0 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 20 20 20   OP_InsertInt   
50dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50dd0 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69          55.#defi
50de0 6e 65 20 4f 50 5f 44 65 6c 65 74 65 20 20 20 20  ne OP_Delete    
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e00 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65            56.#de
50e10 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75  fine OP_ResetCou
50e20 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
50e30 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23              57.#
50e40 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79  define OP_RowKey
50e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38                58
50e70 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44  .#define OP_RowD
50e80 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 20  ata             
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ea0 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  59.#define OP_Ro
50eb0 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20  wid             
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ed0 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    60.#define OP_
50ee0 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20  NullRow         
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f00 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20 4f      61.#define O
50f10 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20  P_Last          
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f30 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65        62.#define
50f40 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 20 20 20   OP_Sort        
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f60 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 69          63.#defi
50f70 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20  ne OP_Rewind    
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f90 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65            64.#de
50fa0 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20  fine OP_Prev    
50fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fc0 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 23              65.#
50fd0 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20  define OP_Next  
50fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36                66
51000 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49  .#define OP_IdxI
51010 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20  nsert           
51020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51030 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  67.#define OP_Id
51040 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20  xDelete         
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51060 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    70.#define OP_
51070 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20 20  IdxRowid        
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51090 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 4f      71.#define O
510a0 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 20  P_IdxLT         
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510c0 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65        72.#define
510d0 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 20   OP_IdxGE       
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510f0 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66 69          81.#defi
51100 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20  ne OP_Destroy   
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51120 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65            92.#de
51130 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20  fine OP_Clear   
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51150 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23              95.#
51160 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65  define OP_Create
51170 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  Index           
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36                96
51190 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61  .#define OP_Crea
511a0 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 20 20  teTable         
511b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511c0 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61  97.#define OP_Pa
511d0 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 20  rseSchema       
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511f0 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f    98.#define OP_
51200 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 20 20  LoadAnalysis    
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51220 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f      99.#define O
51230 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20  P_DropTable     
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51250 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65       100.#define
51260 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20   OP_DropIndex   
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51280 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69         101.#defi
51290 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65  ne OP_DropTrigge
512a0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
512b0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65           102.#de
512c0 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74  fine OP_Integrit
512d0 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  yCk             
512e0 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23             103.#
512f0 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74  define OP_RowSet
51300 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20 20  Add             
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34               104
51320 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53  .#define OP_RowS
51330 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20  etRead          
51340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51350 30 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  05.#define OP_Ro
51360 77 53 65 74 54 65 73 74 20 20 20 20 20 20 20 20  wSetTest        
51370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51380 20 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   106.#define OP_
51390 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20 20  Program         
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513b0 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 4f     107.#define O
513c0 50 5f 50 61 72 61 6d 20 20 20 20 20 20 20 20 20  P_Param         
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513e0 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65       108.#define
513f0 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20 20   OP_FkCounter   
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51410 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69         109.#defi
51420 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 20  ne OP_FkIfZero  
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51440 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65           110.#de
51450 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20  fine OP_MemMax  
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51470 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23             111.#
51480 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f 73 20  define OP_IfPos 
51490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32               112
514b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e 65  .#define OP_IfNe
514c0 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
514d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
514e0 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  13.#define OP_If
514f0 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20  Zero            
51500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51510 20 31 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f   114.#define OP_
51520 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20 20  AggStep         
51530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51540 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 4f     115.#define O
51550 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20  P_AggFinal      
51560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51570 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65       116.#define
51580 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20   OP_Vacuum      
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515a0 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 69         117.#defi
515b0 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d  ne OP_IncrVacuum
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515d0 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65           118.#de
515e0 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20 20  fine OP_Expire  
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51600 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23             119.#
51610 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c  define OP_TableL
51620 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ock             
51630 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30               120
51640 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 67  .#define OP_VBeg
51650 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  in              
51660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51670 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43  21.#define OP_VC
51680 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
51690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516a0 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f   122.#define OP_
516b0 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20  VDestroy        
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516d0 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 4f     123.#define O
516e0 50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20  P_VOpen         
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51700 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65       124.#define
51710 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20   OP_VFilter     
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51730 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69         125.#defi
51740 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20  ne OP_VColumn   
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51760 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65           126.#de
51770 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20  fine OP_VNext   
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51790 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23             127.#
517a0 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d  define OP_VRenam
517b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
517c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38               128
517d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64  .#define OP_VUpd
517e0 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
517f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51800 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61  29.#define OP_Pa
51810 67 65 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20  gecount         
51820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51830 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   131.#define OP_
51840 54 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20  Trace           
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51860 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f     132.#define O
51870 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20  P_Noop          
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51890 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65       133.#define
518a0 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20   OP_Explain     
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518c0 20 20 20 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54         134../* T
518d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63  he following opc
518e0 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e  ode values are n
518f0 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65  ever used */.#de
51900 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
51910 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20  135             
51920 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23             135.#
51930 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
51940 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20  d_136           
51950 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36               136
51960 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
51970 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20  sed_137         
51980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51990 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  37.#define OP_No
519a0 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20  tUsed_138       
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
519c0 20 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   138.#define OP_
519d0 4e 6f 74 55 73 65 64 5f 31 33 39 20 20 20 20 20  NotUsed_139     
519e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
519f0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f     139.#define O
51a00 50 5f 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20  P_NotUsed_140   
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a20 20 20 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72       140.../* Pr
51a30 6f 70 65 72 74 69 65 73 20 73 75 63 68 20 61 73  operties such as
51a40 20 22 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70   "out2" or "jump
51a50 22 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69  " that are speci
51a60 66 69 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65  fied in.** comme
51a70 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  nts following th
51a80 65 20 22 63 61 73 65 22 20 66 6f 72 20 65 61 63  e "case" for eac
51a90 68 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20  h opcode in the 
51aa0 76 64 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e  vdbe.c.** are en
51ab0 63 6f 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65  coded into bitve
51ac0 63 74 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ctors as follows
51ad0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  :.*/.#define OPF
51ae0 4c 47 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20  LG_JUMP         
51af0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75     0x0001  /* ju
51b00 6d 70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d  mp:  P2 holds jm
51b10 70 20 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66  p target */.#def
51b20 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50  ine OPFLG_OUT2_P
51b30 52 45 52 45 4c 45 41 53 45 20 30 78 30 30 30 32  RERELEASE 0x0002
51b40 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
51b50 65 61 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65  ease: */.#define
51b60 20 4f 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20   OPFLG_IN1      
51b70 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f         0x0004  /
51b80 2a 20 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61  * in1:   P1 is a
51b90 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69  n input */.#defi
51ba0 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20  ne OPFLG_IN2    
51bb0 20 20 20 20 20 20 20 20 20 30 78 30 30 30 38 20           0x0008 
51bc0 20 2f 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73   /* in2:   P2 is
51bd0 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65   an input */.#de
51be0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20  fine OPFLG_IN3  
51bf0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 31             0x001
51c00 30 20 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20  0  /* in3:   P3 
51c10 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23  is an input */.#
51c20 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54  define OPFLG_OUT
51c30 32 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30  2            0x0
51c40 30 32 30 20 20 2f 2a 20 6f 75 74 32 3a 20 20 50  020  /* out2:  P
51c50 32 20 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a  2 is an output *
51c60 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
51c70 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20 20  OUT3            
51c80 30 78 30 30 34 30 20 20 2f 2a 20 6f 75 74 33 3a  0x0040  /* out3:
51c90 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70 75    P3 is an outpu
51ca0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  t */.#define OPF
51cb0 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  LG_INITIALIZER {
51cc0 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30 30  \./*   0 */ 0x00
51cd0 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30 78  , 0x01, 0x05, 0x
51ce0 30 34 2c 20 30 78 30 34 2c 20 30 78 31 30 2c 20  04, 0x04, 0x10, 
51cf0 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20  0x00, 0x02,\./* 
51d00 20 20 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30    8 */ 0x02, 0x0
51d10 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
51d20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c  x00, 0x00, 0x24,
51d30 20 30 78 32 34 2c 5c 0a 2f 2a 20 20 31 36 20 2a   0x24,\./*  16 *
51d40 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  / 0x00, 0x00, 0x
51d50 30 30 2c 20 30 78 32 34 2c 20 30 78 30 34 2c 20  00, 0x24, 0x04, 
51d60 30 78 30 35 2c 20 30 78 30 34 2c 20 30 78 30 30  0x05, 0x04, 0x00
51d70 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78 30  ,\./*  24 */ 0x0
51d80 30 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30  0, 0x01, 0x05, 0
51d90 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x05, 0x00, 0x00,
51da0 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a   0x00, 0x02,\./*
51db0 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30 78    32 */ 0x00, 0x
51dc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20  00, 0x00, 0x02, 
51dd0 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x10, 0x00, 0x00
51de0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 34 30 20  , 0x00,\./*  40 
51df0 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  */ 0x00, 0x00, 0
51e00 78 30 30 2c 20 30 78 31 31 2c 20 30 78 31 31 2c  x00, 0x11, 0x11,
51e10 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30   0x11, 0x11, 0x0
51e20 38 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 78  8,\./*  48 */ 0x
51e30 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20  11, 0x11, 0x11, 
51e40 30 78 31 31 2c 20 30 78 30 32 2c 20 30 78 30 32  0x11, 0x02, 0x02
51e50 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x00, 0x00,\./
51e60 2a 20 20 35 36 20 2a 2f 20 30 78 30 30 2c 20 30  *  56 */ 0x00, 0
51e70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51e80 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
51e90 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 20 36 34  1, 0x01,\./*  64
51ea0 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 31 2c 20   */ 0x01, 0x01, 
51eb0 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 34 63  0x01, 0x08, 0x4c
51ec0 2c 20 30 78 34 63 2c 20 30 78 30 30 2c 20 30 78  , 0x4c, 0x00, 0x
51ed0 30 32 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 30  02,\./*  72 */ 0
51ee0 78 30 31 2c 20 30 78 30 35 2c 20 30 78 30 35 2c  x01, 0x05, 0x05,
51ef0 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31   0x15, 0x15, 0x1
51f00 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a  5, 0x15, 0x15,\.
51f10 2f 2a 20 20 38 30 20 2a 2f 20 30 78 31 35 2c 20  /*  80 */ 0x15, 
51f20 30 78 30 31 2c 20 30 78 34 63 2c 20 30 78 34 63  0x01, 0x4c, 0x4c
51f30 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78  , 0x4c, 0x4c, 0x
51f40 34 63 2c 20 30 78 34 63 2c 5c 0a 2f 2a 20 20 38  4c, 0x4c,\./*  8
51f50 38 20 2a 2f 20 30 78 34 63 2c 20 30 78 34 63 2c  8 */ 0x4c, 0x4c,
51f60 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 30   0x4c, 0x4c, 0x0
51f70 32 2c 20 30 78 32 34 2c 20 30 78 30 32 2c 20 30  2, 0x24, 0x02, 0
51f80 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 20  x00,\./*  96 */ 
51f90 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30  0x02, 0x02, 0x00
51fa0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51fb0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c  00, 0x00, 0x00,\
51fc0 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 63 2c  ./* 104 */ 0x0c,
51fd0 20 30 78 34 35 2c 20 30 78 31 35 2c 20 30 78 30   0x45, 0x15, 0x0
51fe0 31 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  1, 0x02, 0x00, 0
51ff0 78 30 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 31  x01, 0x08,\./* 1
52000 31 32 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 35  12 */ 0x05, 0x05
52010 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78  , 0x05, 0x00, 0x
52020 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20  00, 0x00, 0x01, 
52030 30 78 30 30 2c 5c 0a 2f 2a 20 31 32 30 20 2a 2f  0x00,\./* 120 */
52040 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
52050 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
52060 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 31 2c  x01, 0x00, 0x01,
52070 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 30 30  \./* 128 */ 0x00
52080 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  , 0x00, 0x02, 0x
52090 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  02, 0x00, 0x00, 
520a0 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x00, 0x00,\./* 
520b0 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30  136 */ 0x00, 0x0
520c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
520d0 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x00, 0x04, 0x04,
520e0 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 2a   0x04,\./* 144 *
520f0 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a 0a  / 0x04, 0x04,}..
52100 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
52110 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68  End of opcodes.h
52120 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
52130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
52150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
52160 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
52170 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
52180 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.h *********
52190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
521a0 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65  ./*.** Prototype
521b0 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20 69  s for the VDBE i
521c0 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20 63  nterface.  See c
521d0 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 69  omments on the i
521e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
521f0 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69   for a descripti
52200 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 20  on of what each 
52210 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
52220 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  s does..*/.SQLIT
52230 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a  E_PRIVATE Vdbe *
52240 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74  sqlite3VdbeCreat
52250 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  e(sqlite3*);.SQL
52260 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52270 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
52280 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51  0(Vdbe*,int);.SQ
52290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
522a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
522b0 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  p1(Vdbe*,int,int
522c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
522d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
522e0 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69 6e  eAddOp2(Vdbe*,in
522f0 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
52300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52310 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
52320 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69  (Vdbe*,int,int,i
52330 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
52340 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
52350 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56 64  te3VdbeAddOp4(Vd
52360 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
52370 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  int,const char *
52380 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  zP4,int);.SQLITE
52390 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
523a0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e  ite3VdbeAddOp4In
523b0 74 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  t(Vdbe*,int,int,
523c0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  int,int,int);.SQ
523d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
523e0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
523f0 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74  pList(Vdbe*, int
52400 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74   nOp, VdbeOpList
52410 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51   const *aOp);.SQ
52420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52430 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
52440 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74  ngeP1(Vdbe*, int
52450 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a   addr, int P1);.
52460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52470 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
52480 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69  hangeP2(Vdbe*, i
52490 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29  nt addr, int P2)
524a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
524b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
524c0 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c  eChangeP3(Vdbe*,
524d0 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50   int addr, int P
524e0 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  3);.SQLITE_PRIVA
524f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52500 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65  dbeChangeP5(Vdbe
52510 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54  *, u8 P5);.SQLIT
52520 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52530 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
52540 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  re(Vdbe*, int ad
52550 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  dr);.SQLITE_PRIV
52560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52570 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70  VdbeChangeToNoop
52580 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
52590 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45  , int N);.SQLITE
525a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
525b0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
525c0 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  4(Vdbe*, int add
525d0 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
525e0 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49  P4, int N);.SQLI
525f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52600 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
52610 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29  tree(Vdbe*, int)
52620 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52630 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
52640 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c  VdbeGetOp(Vdbe*,
52650 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
52660 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52670 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56  3VdbeMakeLabel(V
52680 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
52690 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
526a0 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62  e3VdbeDelete(Vdb
526b0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
526c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
526d0 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64  VdbeMakeReady(Vd
526e0 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
526f0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  int,int,int);.SQ
52700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52710 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
52720 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c  lize(Vdbe*);.SQL
52730 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
52740 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
52750 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20  lveLabel(Vdbe*, 
52760 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
52770 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
52780 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
52790 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53  Vdbe*);.#ifdef S
527a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
527b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
527c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65   sqlite3VdbeAsse
527d0 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20  rtMayAbort(Vdbe 
527e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
527f0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
52800 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28  qlite3VdbeTrace(
52810 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65  Vdbe*,FILE*);.#e
52820 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
52830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52840 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73  VdbeResetStepRes
52850 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ult(Vdbe*);.SQLI
52860 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52870 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
52880 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
52890 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
528a0 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
528b0 73 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51  s(Vdbe*,int);.SQ
528c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
528d0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
528e0 6f 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e  olName(Vdbe*, in
528f0 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  t, int, const ch
52900 61 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f  ar *, void(*)(vo
52910 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
52920 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52930 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67  e3VdbeCountChang
52940 65 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  es(Vdbe*);.SQLIT
52950 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
52960 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62  3 *sqlite3VdbeDb
52970 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
52980 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
52990 69 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56  ite3VdbeSetSql(V
529a0 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  dbe*, const char
529b0 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29   *z, int n, int)
529c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
529d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
529e0 65 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65  eSwap(Vdbe*,Vdbe
529f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
52a00 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
52a10 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61  e3VdbeTakeOpArra
52a20 79 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69  y(Vdbe*, int*, i
52a30 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
52a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
52a50 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65  3VdbeProgramDele
52a60 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 75  te(sqlite3 *, Su
52a70 62 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29  bProgram *, int)
52a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52a90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
52aa0 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 56 61  sqlite3VdbeGetVa
52ab0 6c 75 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20  lue(Vdbe*, int, 
52ac0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  u8);.SQLITE_PRIV
52ad0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52ae0 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28 56  VdbeSetVarmask(V
52af0 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e  dbe*, int);.#ifn
52b00 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
52b10 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50 52 49  TRACE.SQLITE_PRI
52b20 56 41 54 45 20 20 20 63 68 61 72 20 2a 73 71 6c  VATE   char *sql
52b30 69 74 65 33 56 64 62 65 45 78 70 61 6e 64 53 71  ite3VdbeExpandSq
52b40 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63  l(Vdbe*, const c
52b50 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53  har*);.#endif..S
52b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e  QLITE_PRIVATE Un
52b70 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71  packedRecord *sq
52b80 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
52b90 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69  npack(KeyInfo*,i
52ba0 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 63  nt,const void*,c
52bb0 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  har*,int);.SQLIT
52bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52bd0 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
52be0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55  UnpackedRecord(U
52bf0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b  npackedRecord*);
52c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52c10 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
52c20 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74  ecordCompare(int
52c30 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70  ,const void*,Unp
52c40 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a  ackedRecord*);..
52c50 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
52c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
52c70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52c80 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20  eComment(Vdbe*, 
52c90 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
52ca0 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65  );.# define Vdbe
52cb0 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69  Comment(X)  sqli
52cc0 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58  te3VdbeComment X
52cd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52ce0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
52cf0 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64  beNoopComment(Vd
52d00 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
52d10 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65  , ...);.# define
52d20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
52d30 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65  (X)  sqlite3Vdbe
52d40 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65  NoopComment X.#e
52d50 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62  lse.# define Vdb
52d60 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65  eComment(X).# de
52d70 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  fine VdbeNoopCom
52d80 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ment(X).#endif..
52d90 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
52da0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
52db0 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.h **********
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
52df0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
52e00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
52e10 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
52e20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
52e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
52e40 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
52e50 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d  pager.h in the m
52e60 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
52e70 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
52e80 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
52e90 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
52ea0 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  le pager.h *****
52eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ed0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
52ee0 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
52ef0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
52f00 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
52f10 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
52f20 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
52f30 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
52f40 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
52f50 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
52f60 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
52f70 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
52f80 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
52f90 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
52fa0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
52fb0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
52fc0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
52fd0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
52fe0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
52ff0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
53000 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
53010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
53050 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
53060 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
53070 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
53080 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61  e sqlite page ca
53090 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  che.** subsystem
530a0 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68  .  The page cach
530b0 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64  e subsystem read
530c0 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66  s and writes a f
530d0 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74  ile a page.** at
530e0 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76   a time and prov
530f0 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66  ides a journal f
53100 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a  or rollback..*/.
53110 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f  .#ifndef _PAGER_
53120 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45  H_.#define _PAGE
53130 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61  R_H_../*.** Defa
53140 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  ult maximum size
53150 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20   for persistent 
53160 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41  journal files. A
53170 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61   negative .** va
53180 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d  lue means no lim
53190 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d  it. This value m
531a0 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ay be overridden
531b0 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73   using the .** s
531c0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
531d0 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50  alSizeLimit() AP
531e0 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41  I. See also "PRA
531f0 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  GMA journal_size
53200 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e  _limit"..*/.#ifn
53210 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
53220 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f  LT_JOURNAL_SIZE_
53230 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20  LIMIT.  #define 
53240 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a  SQLITE_DEFAULT_J
53250 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49  OURNAL_SIZE_LIMI
53260 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  T -1.#endif../*.
53270 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64  ** The type used
53280 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20   to represent a 
53290 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68  page number.  Th
532a0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20  e first page in 
532b0 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c  a file.** is cal
532c0 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69  led page 1.  0 i
532d0 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  s used to repres
532e0 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22  ent "not a page"
532f0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32  ..*/.typedef u32
53300 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   Pgno;../*.** Ea
53310 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20  ch open file is 
53320 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70  managed by a sep
53330 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f  arate instance o
53340 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74  f the "Pager" st
53350 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
53360 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72  def struct Pager
53370 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48   Pager;../*.** H
53380 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70  andle type for p
53390 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ages..*/.typedef
533a0 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62   struct PgHdr Db
533b0 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67  Page;../*.** Pag
533c0 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d  e number PAGER_M
533d0 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20  J_PGNO is never 
533e0 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74  used in an SQLit
533f0 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69  e database (it i
53400 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f  s.** reserved fo
53410 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64  r working around
53420 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78   a windows/posix
53430 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79   incompatibility
53440 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  ). It is.** used
53450 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
53460 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20  to signify that 
53470 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66  the remainder of
53480 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
53490 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64  e .** is devoted
534a0 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61   to storing a ma
534b0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
534c0 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f  e - there are no
534d0 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a   more pages to.*
534e0 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65  * roll back. See
534f0 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75   comments for fu
53500 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74  nction writeMast
53510 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70  erJournal() in p
53520 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64  ager.c .** for d
53530 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69  etails..*/.#defi
53540 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
53550 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e  (x) ((Pgno)((PEN
53560 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e  DING_BYTE/((x)->
53570 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a  pageSize))+1))..
53580 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
53590 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61  lues for the fla
535a0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
535b0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
535c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ()..**.** NOTE: 
535d0 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73  These values mus
535e0 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72  t match the corr
535f0 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f  esponding BTREE_
53600 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65   values in btree
53610 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  .h..*/.#define P
53620 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
53630 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20  L  0x0001    /* 
53640 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c  Do not use a rol
53650 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f  lback journal */
53660 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e  .#define PAGER_N
53670 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30  O_READLOCK   0x0
53680 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72  002    /* Omit r
53690 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64  eadlocks on read
536a0 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f  only files */../
536b0 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65  *.** Valid value
536c0 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  s for the second
536d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
536e0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
536f0 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Mode()..*/.#defi
53700 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ne PAGER_LOCKING
53710 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20  MODE_QUERY      
53720 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  -1.#define PAGER
53730 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
53740 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69  MAL      0.#defi
53750 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ne PAGER_LOCKING
53760 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20  MODE_EXCLUSIVE  
53770 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20   1../*.** Valid 
53780 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
53790 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
537a0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  o sqlite3PagerJo
537b0 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a  urnalMode()..*/.
537c0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f  #define PAGER_JO
537d0 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20  URNALMODE_QUERY 
537e0 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20       -1.#define 
537f0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
53800 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20  E_DELETE      0 
53810 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64    /* Commit by d
53820 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  eleting journal 
53830 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  file */.#define 
53840 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
53850 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20  E_PERSIST     1 
53860 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a    /* Commit by z
53870 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68  eroing journal h
53880 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  eader */.#define
53890 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
538a0 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32  DE_OFF         2
538b0 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d     /* Journal om
538c0 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69  itted.  */.#defi
538d0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
538e0 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20  MODE_TRUNCATE   
538f0 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   3   /* Commit b
53900 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75  y truncating jou
53910 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rnal */.#define 
53920 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
53930 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20  E_MEMORY      4 
53940 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a    /* In-memory j
53950 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a  ournal file */..
53960 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e  /*.** The remain
53970 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65  der of this file
53980 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65   contains the de
53990 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68  clarations of th
539a0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74  e functions.** t
539b0 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20  hat make up the 
539c0 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d  Pager sub-system
539d0 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65   API. See source
539e0 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66   code comments f
539f0 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65  or .** a detaile
53a00 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  d description of
53a10 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a   each routine..*
53a20 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63  /../* Open and c
53a30 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e  lose a Pager con
53a40 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c  nection. */ .SQL
53a50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53a60 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
53a70 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a  (.  sqlite3_vfs*
53a80 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61  ,.  Pager **ppPa
53a90 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  ger,.  const cha
53aa0 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74  r*,.  int,.  int
53ab0 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28  ,.  int,.  void(
53ac0 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53  *)(DbPage*).);.S
53ad0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53ae0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c  t sqlite3PagerCl
53af0 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ose(Pager *pPage
53b00 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
53b10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53b20 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65  gerReadFileheade
53b30 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75  r(Pager*, int, u
53b40 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a  nsigned char*);.
53b50 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73  ./* Functions us
53b60 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20  ed to configure 
53b70 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20  a Pager object. 
53b80 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
53b90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
53ba0 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65  gerSetBusyhandle
53bb0 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29  r(Pager*, int(*)
53bc0 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a  (void *), void *
53bd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53be0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53bf0 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61  erSetPagesize(Pa
53c00 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74 29  ger*, u16*, int)
53c10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53c20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53c30 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61  rMaxPageCount(Pa
53c40 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ger*, int);.SQLI
53c50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
53c60 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
53c70 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c  achesize(Pager*,
53c80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
53c90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
53ca0 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79  e3PagerSetSafety
53cb0 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74  Level(Pager*,int
53cc0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
53cd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53ce0 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
53cf0 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b  e(Pager *, int);
53d00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53d10 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
53d20 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65  JournalMode(Page
53d30 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
53d40 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71  E_PRIVATE i64 sq
53d50 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
53d60 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72  lSizeLimit(Pager
53d70 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45   *, i64);.SQLITE
53d80 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
53d90 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65  _backup **sqlite
53da0 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28  3PagerBackupPtr(
53db0 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e  Pager*);../* Fun
53dc0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f  ctions used to o
53dd0 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73  btain and releas
53de0 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65  e page reference
53df0 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  s. */ .SQLITE_PR
53e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53e10 33 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61  3PagerAcquire(Pa
53e20 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
53e30 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a  o pgno, DbPage *
53e40 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72  *ppPage, int clr
53e50 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73  Flag);.#define s
53e60 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41  qlite3PagerGet(A
53e70 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67  ,B,C) sqlite3Pag
53e80 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c  erAcquire(A,B,C,
53e90 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  0).SQLITE_PRIVAT
53ea0 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65  E DbPage *sqlite
53eb0 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67  3PagerLookup(Pag
53ec0 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
53ed0 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50   pgno);.SQLITE_P
53ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
53ef0 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61  te3PagerRef(DbPa
53f00 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ge*);.SQLITE_PRI
53f10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
53f20 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61  3PagerUnref(DbPa
53f30 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74  ge*);../* Operat
53f40 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66  ions on page ref
53f50 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49  erences. */.SQLI
53f60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
53f70 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
53f80 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54  (DbPage*);.SQLIT
53f90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
53fa0 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57  qlite3PagerDontW
53fb0 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53  rite(DbPage*);.S
53fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53fd0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  t sqlite3PagerMo
53fe0 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62  vepage(Pager*,Db
53ff0 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b  Page*,Pgno,int);
54000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54010 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
54020 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50  PageRefcount(DbP
54030 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  age*);.SQLITE_PR
54040 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
54050 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
54060 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49  DbPage *); .SQLI
54070 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
54080 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
54090 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b  Extra(DbPage *);
540a0 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20   ../* Functions 
540b0 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70  used to manage p
540c0 61 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e  ager transaction
540d0 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73  s and savepoints
540e0 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  . */.SQLITE_PRIV
540f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
54100 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61  agerPagecount(Pa
54110 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  ger*, int*);.SQL
54120 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54130 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69  sqlite3PagerBegi
54140 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78  n(Pager*, int ex
54150 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Flag, int);.SQLI
54160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
54170 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
54180 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a  tPhaseOne(Pager*
54190 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61  ,const char *zMa
541a0 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ster, int);.SQLI
541b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
541c0 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
541d0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a  Pager *pPager);.
541e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
541f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
54200 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61  ommitPhaseTwo(Pa
54210 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
54220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
54230 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50  3PagerRollback(P
54240 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
54250 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
54260 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70  e3PagerOpenSavep
54270 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  oint(Pager *pPag
54280 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49  er, int n);.SQLI
54290 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
542a0 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70  qlite3PagerSavep
542b0 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  oint(Pager *pPag
542c0 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  er, int op, int 
542d0 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51 4c  iSavepoint);.SQL
542e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
542f0 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72  sqlite3PagerShar
54300 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  edLock(Pager *pP
54310 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74  ager);../* Funct
54320 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75 65  ions used to que
54330 72 79 20 70 61 67 65 72 20 73 74 61 74 65 20 61  ry pager state a
54340 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  nd configuration
54350 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  . */.SQLITE_PRIV
54360 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61  ATE u8 sqlite3Pa
54370 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61  gerIsreadonly(Pa
54380 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
54390 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
543a0 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50  3PagerRefcount(P
543b0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
543c0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
543d0 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46  r *sqlite3PagerF
543e0 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b  ilename(Pager*);
543f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54400 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66  const sqlite3_vf
54410 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56  s *sqlite3PagerV
54420 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  fs(Pager*);.SQLI
54430 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
54440 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33  e3_file *sqlite3
54450 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a  PagerFile(Pager*
54460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
54470 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
54480 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
54490 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53  lname(Pager*);.S
544a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
544b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f  t sqlite3PagerNo
544c0 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 51  sync(Pager*);.SQ
544d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
544e0 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54  d *sqlite3PagerT
544f0 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a 29  empSpace(Pager*)
54500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54510 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54520 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a 29  rIsMemdb(Pager*)
54530 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20  ;../* Functions 
54540 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 65  used to truncate
54550 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
54560 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le. */.SQLITE_PR
54570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
54580 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
54590 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f  mage(Pager*,Pgno
545a0 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  );../* Functions
545b0 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73 74   to support test
545c0 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
545d0 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  g. */.#if !defin
545e0 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65  ed(NDEBUG) || de
545f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
54600 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  T).SQLITE_PRIVAT
54610 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33  E   Pgno sqlite3
54620 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28  PagerPagenumber(
54630 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
54640 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
54650 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
54660 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29 3b  teable(DbPage*);
54670 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
54680 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
54690 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
546a0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61  *sqlite3PagerSta
546b0 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  ts(Pager*);.SQLI
546c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
546d0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  d sqlite3PagerRe
546e0 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a 20  fdump(Pager*);. 
546f0 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69   void disable_si
54700 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
54710 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20  s(void);.  void 
54720 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  enable_simulated
54730 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29  _io_errors(void)
54740 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
54750 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
54760 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23  ed_io_errors().#
54770 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73   define enable_s
54780 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
54790 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  rs().#endif..#en
547a0 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f  dif /* _PAGER_H_
547b0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
547c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65  **** End of page
547d0 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.h ************
547e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54800 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
54810 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
54820 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
54830 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
54840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
54850 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
54860 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 61  **** Include pca
54870 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  che.h in the mid
54880 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
54890 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
548a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
548b0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
548c0 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  pcache.h *******
548d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548f0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
54900 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20  August 05.**.** 
54910 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
54920 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
54930 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
54940 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
54950 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
54960 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
54970 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
54980 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
54990 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
549a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
549b0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
549c0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
549d0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
549e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
549f0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
54a00 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
54a10 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
54a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a60 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
54a70 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
54a80 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
54a90 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ce that the sqli
54aa0 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a  te page cache.**
54ab0 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2f 0a   subsystem. .*/.
54ac0 0a 23 69 66 6e 64 65 66 20 5f 50 43 41 43 48 45  .#ifndef _PCACHE
54ad0 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72  _H_..typedef str
54ae0 75 63 74 20 50 67 48 64 72 20 50 67 48 64 72 3b  uct PgHdr PgHdr;
54af0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
54b00 50 43 61 63 68 65 20 50 43 61 63 68 65 3b 0a 0a  PCache PCache;..
54b10 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70 61 67 65  /*.** Every page
54b20 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 69 73   in the cache is
54b30 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61   controlled by a
54b40 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
54b50 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73  e following.** s
54b60 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
54b70 75 63 74 20 50 67 48 64 72 20 7b 0a 20 20 76 6f  uct PgHdr {.  vo
54b80 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20  id *pData;      
54b90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54ba0 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20  Content of this 
54bb0 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  page */.  void *
54bc0 70 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  pExtra;         
54bd0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
54be0 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50  a content */.  P
54bf0 67 48 64 72 20 2a 70 44 69 72 74 79 3b 20 20 20  gHdr *pDirty;   
54c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54c10 20 54 72 61 6e 73 69 65 6e 74 20 6c 69 73 74 20   Transient list 
54c20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 2a  of dirty pages *
54c30 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20  /.  Pgno pgno;  
54c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54c50 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
54c60 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20  r for this page 
54c70 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  */.  Pager *pPag
54c80 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
54c90 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72      /* The pager
54ca0 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61   this page is pa
54cb0 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20  rt of */.#ifdef 
54cc0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
54cd0 45 53 0a 20 20 75 33 32 20 70 61 67 65 48 61 73  ES.  u32 pageHas
54ce0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
54cf0 20 20 20 20 2f 2a 20 48 61 73 68 20 6f 66 20 70      /* Hash of p
54d00 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23  age content */.#
54d10 65 6e 64 69 66 0a 20 20 75 31 36 20 66 6c 61 67  endif.  u16 flag
54d20 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
54d30 20 20 20 20 20 20 20 2f 2a 20 50 47 48 44 52 20         /* PGHDR 
54d40 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 62 65  flags defined be
54d50 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a  low */..  /*****
54d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54da0 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20  *.  ** Elements 
54db0 61 62 6f 76 65 20 61 72 65 20 70 75 62 6c 69 63  above are public
54dc0 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c  .  All that foll
54dd0 6f 77 73 20 69 73 20 70 72 69 76 61 74 65 20 74  ows is private t
54de0 6f 20 70 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20  o pcache.c.  ** 
54df0 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
54e00 65 20 61 63 63 65 73 73 65 64 20 62 79 20 6f 74  e accessed by ot
54e10 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a  her modules..  *
54e20 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b 20 20 20  /.  i16 nRef;   
54e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54e40 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
54e50 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 70 61  users of this pa
54e60 67 65 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ge */.  PCache *
54e70 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20  pCache;         
54e80 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20         /* Cache 
54e90 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20 70  that owns this p
54ea0 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20  age */..  PgHdr 
54eb0 2a 70 44 69 72 74 79 4e 65 78 74 3b 20 20 20 20  *pDirtyNext;    
54ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
54ed0 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74   element in list
54ee0 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20   of dirty pages 
54ef0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72  */.  PgHdr *pDir
54f00 74 79 50 72 65 76 3b 20 20 20 20 20 20 20 20 20  tyPrev;         
54f10 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
54f20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20  element in list 
54f30 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 2a  of dirty pages *
54f40 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c  /.};../* Bit val
54f50 75 65 73 20 66 6f 72 20 50 67 48 64 72 2e 66 6c  ues for PgHdr.fl
54f60 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ags */.#define P
54f70 47 48 44 52 5f 44 49 52 54 59 20 20 20 20 20 20  GHDR_DIRTY      
54f80 20 20 20 20 20 20 20 30 78 30 30 32 20 20 2f 2a         0x002  /*
54f90 20 50 61 67 65 20 68 61 73 20 63 68 61 6e 67 65   Page has change
54fa0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48  d */.#define PGH
54fb0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 20 20 20  DR_NEED_SYNC    
54fc0 20 20 20 20 20 30 78 30 30 34 20 20 2f 2a 20 46       0x004  /* F
54fd0 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c 62 61 63  sync the rollbac
54fe0 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65  k journal before
54ff0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
55000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55010 20 20 20 20 20 20 20 20 2a 2a 20 77 72 69 74 69          ** writi
55020 6e 67 20 74 68 69 73 20 70 61 67 65 20 74 6f 20  ng this page to 
55030 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
55040 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45  #define PGHDR_NE
55050 45 44 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  ED_READ         
55060 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e  0x008  /* Conten
55070 74 20 69 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23  t is unread */.#
55080 64 65 66 69 6e 65 20 50 47 48 44 52 5f 52 45 55  define PGHDR_REU
55090 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30  SE_UNLIKELY    0
550a0 78 30 31 30 20 20 2f 2a 20 41 20 68 69 6e 74 20  x010  /* A hint 
550b0 74 68 61 74 20 72 65 75 73 65 20 69 73 20 75 6e  that reuse is un
550c0 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  likely */.#defin
550d0 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  e PGHDR_DONT_WRI
550e0 54 45 20 20 20 20 20 20 20 20 30 78 30 32 30 20  TE        0x020 
550f0 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65   /* Do not write
55100 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b   content to disk
55110 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69   */../* Initiali
55120 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20  ze and shutdown 
55130 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 73  the page cache s
55140 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49  ubsystem */.SQLI
55150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
55160 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e 69 74  qlite3PcacheInit
55170 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51  ialize(void);.SQ
55180 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55190 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
551a0 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a  hutdown(void);..
551b0 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 62 75  /* Page cache bu
551c0 66 66 65 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a  ffer management:
551d0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
551e0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c  es implement SQL
551f0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
55200 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ACHE..*/.SQLITE_
55210 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55220 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72  ite3PCacheBuffer
55230 53 65 74 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e  Setup(void *, in
55240 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f  t sz, int n);../
55250 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 70  * Create a new p
55260 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55  ager cache..** U
55270 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73 74 72 65  nder memory stre
55280 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53 74 72 65  ss, invoke xStre
55290 73 73 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b  ss to try to mak
552a0 65 20 70 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a  e pages clean..*
552b0 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64  * Only clean and
552c0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
552d0 63 61 6e 20 62 65 20 72 65 63 6c 61 69 6d 65 64  can be reclaimed
552e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
552f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55300 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e  PcacheOpen(.  in
55310 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20  t szPage,       
55320 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
55330 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61  Size of every pa
55340 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78  ge */.  int szEx
55350 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20  tra,            
55360 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
55370 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64  space associated
55380 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20   with each page 
55390 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61  */.  int bPurgea
553a0 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
553b0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
553c0 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b  ages are on back
553d0 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69  ing store */.  i
553e0 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f  nt (*xStress)(vo
553f0 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a  id*, PgHdr*), /*
55400 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20   Call to try to 
55410 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e  make pages clean
55420 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 72   */.  void *pStr
55430 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ess,            
55440 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
55450 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20   to xStress */. 
55460 20 50 43 61 63 68 65 20 2a 70 54 6f 49 6e 69 74   PCache *pToInit
55470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55480 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20  /* Preallocated 
55490 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 50 43  space for the PC
554a0 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d  ache */.);../* M
554b0 6f 64 69 66 79 20 74 68 65 20 70 61 67 65 2d 73  odify the page-s
554c0 69 7a 65 20 61 66 74 65 72 20 74 68 65 20 63 61  ize after the ca
554d0 63 68 65 20 68 61 73 20 62 65 65 6e 20 63 72 65  che has been cre
554e0 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  ated. */.SQLITE_
554f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55500 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67  ite3PcacheSetPag
55510 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20  eSize(PCache *, 
55520 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e  int);../* Return
55530 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74   the size in byt
55540 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f  es of a PCache o
55550 62 6a 65 63 74 2e 20 20 55 73 65 64 20 74 6f 20  bject.  Used to 
55560 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73  preallocate.** s
55570 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f  torage space..*/
55580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55590 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
555a0 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a  eSize(void);../*
555b0 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 70 65 72   One release per
555c0 20 73 75 63 63 65 73 73 66 75 6c 20 66 65 74 63   successful fetc
555d0 68 2e 20 20 50 61 67 65 20 69 73 20 70 69 6e 6e  h.  Page is pinn
555e0 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 61 73 65  ed until release
555f0 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 20  d..** Reference 
55600 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c  counted. .*/.SQL
55610 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
55620 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
55630 63 68 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f  ch(PCache*, Pgno
55640 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67  , int createFlag
55650 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49  , PgHdr**);.SQLI
55660 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
55670 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
55680 65 61 73 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53  ease(PgHdr*);..S
55690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
556a0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
556b0 44 72 6f 70 28 50 67 48 64 72 2a 29 3b 20 20 20  Drop(PgHdr*);   
556c0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
556d0 70 61 67 65 20 66 72 6f 6d 20 63 61 63 68 65 20  page from cache 
556e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
556f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55700 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67  acheMakeDirty(Pg
55710 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b  Hdr*);    /* Mak
55720 65 20 73 75 72 65 20 70 61 67 65 20 69 73 20 6d  e sure page is m
55730 61 72 6b 65 64 20 64 69 72 74 79 20 2a 2f 0a 53  arked dirty */.S
55740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
55750 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
55760 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 2a  MakeClean(PgHdr*
55770 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20  );    /* Mark a 
55780 73 69 6e 67 6c 65 20 70 61 67 65 20 61 73 20 63  single page as c
55790 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  lean */.SQLITE_P
557a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
557b0 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c  te3PcacheCleanAl
557c0 6c 28 50 43 61 63 68 65 2a 29 3b 20 20 20 20 2f  l(PCache*);    /
557d0 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69 72 74 79  * Mark all dirty
557e0 20 6c 69 73 74 20 70 61 67 65 73 20 61 73 20 63   list pages as c
557f0 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e  lean */../* Chan
55800 67 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  ge a page number
55810 2e 20 20 55 73 65 64 20 62 79 20 69 6e 63 72 2d  .  Used by incr-
55820 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54  vacuum. */.SQLIT
55830 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55840 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
55850 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a  (PgHdr*, Pgno);.
55860 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70  ./* Remove all p
55870 61 67 65 73 20 77 69 74 68 20 70 67 6e 6f 3e 78  ages with pgno>x
55880 2e 20 20 52 65 73 65 74 20 74 68 65 20 63 61 63  .  Reset the cac
55890 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51  he if x==0 */.SQ
558a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
558b0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54  d sqlite3PcacheT
558c0 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 2a 2c  runcate(PCache*,
558d0 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65   Pgno x);../* Ge
558e0 74 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  t a list of all 
558f0 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74  dirty pages in t
55900 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64  he cache, sorted
55910 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20   by page number 
55920 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55930 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33  E PgHdr *sqlite3
55940 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
55950 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65  PCache*);../* Re
55960 73 65 74 20 61 6e 64 20 63 6c 6f 73 65 20 74 68  set and close th
55970 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a  e cache object *
55980 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55990 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
559a0 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65 2a  cheClose(PCache*
559b0 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61  );../* Clear fla
559c0 67 73 20 66 72 6f 6d 20 70 61 67 65 73 20 6f 66  gs from pages of
559d0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
559e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
559f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55a00 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61  acheClearSyncFla
55a10 67 73 28 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f  gs(PCache *);../
55a20 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f  * Discard the co
55a30 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61  ntents of the ca
55a40 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  che */.SQLITE_PR
55a50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55a60 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43  e3PcacheClear(PC
55a70 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75  ache*);../* Retu
55a80 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
55a90 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69  ber of outstandi
55aa0 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ng page referenc
55ab0 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  es */.SQLITE_PRI
55ac0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
55ad0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50  PcacheRefCount(P
55ae0 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63  Cache*);../* Inc
55af0 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
55b00 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e  ence count of an
55b10 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 2a   existing page *
55b20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55b30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55b40 63 68 65 52 65 66 28 50 67 48 64 72 2a 29 3b 0a  cheRef(PgHdr*);.
55b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55b60 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
55b70 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 50 67  ePageRefcount(Pg
55b80 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72  Hdr*);../* Retur
55b90 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
55ba0 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72  er of pages stor
55bb0 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  ed in the cache 
55bc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
55be0 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61  chePagecount(PCa
55bf0 63 68 65 2a 29 3b 0a 0a 23 69 66 20 64 65 66 69  che*);..#if defi
55c00 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 43 4b  ned(SQLITE_CHECK
55c10 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 69 6e  _PAGES) || defin
55c20 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
55c30 0a 2f 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f  ./* Iterate thro
55c40 75 67 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61  ugh all dirty pa
55c50 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74  ges currently st
55c60 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
55c70 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72  e. This.** inter
55c80 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61  face is only ava
55c90 69 6c 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45  ilable if SQLITE
55ca0 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20  _CHECK_PAGES is 
55cb0 64 65 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65  defined when the
55cc0 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20   .** library is 
55cd0 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  built..*/.SQLITE
55ce0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55cf0 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61  lite3PcacheItera
55d00 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a  teDirty(PCache *
55d10 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78  pCache, void (*x
55d20 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b  Iter)(PgHdr *));
55d30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20  .#endif../* Set 
55d40 61 6e 64 20 67 65 74 20 74 68 65 20 73 75 67 67  and get the sugg
55d50 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65  ested cache-size
55d60 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
55d70 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a  ed pager-cache..
55d80 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62  **.** If no glob
55d90 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f  al maximum is co
55da0 6e 66 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74  nfigured, then t
55db0 68 65 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70  he system attemp
55dc0 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74  ts to limit.** t
55dd0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
55de0 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20  of pages cached 
55df0 62 79 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  by purgeable pag
55e00 65 72 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65  er-caches to the
55e10 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73   sum.** of the s
55e20 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73  uggested cache-s
55e30 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  izes..*/.SQLITE_
55e40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55e50 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
55e60 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c  hesize(PCache *,
55e70 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51   int);.#ifdef SQ
55e80 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
55e90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
55ea0 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63  ite3PcacheGetCac
55eb0 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29  hesize(PCache *)
55ec0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
55ed0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
55ee0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
55ef0 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72  ./* Try to retur
55f00 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  n memory used by
55f10 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75   the pcache modu
55f20 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d  le to the main m
55f30 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51  emory heap */.SQ
55f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
55f50 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
55f60 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29  leaseMemory(int)
55f70 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
55f80 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
55f90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
55fa0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 74   sqlite3PcacheSt
55fb0 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  ats(int*,int*,in
55fc0 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66  t*,int*);.#endif
55fd0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
55fe0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
55ff0 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f  cheSetDefault(vo
56000 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  id);..#endif /* 
56010 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f  _PCACHE_H_ */../
56020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
56030 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a  nd of pcache.h *
56040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
56070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
56080 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
56090 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
560a0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
560b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
560c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
560d0 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20  Include os.h in 
560e0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
560f0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
56100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
56110 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
56120 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20  Begin file os.h 
56130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
56160 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
56170 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68  mber 16.**.** Th
56180 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
56190 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
561a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
561b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
561c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
561d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
561e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
561f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
56200 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
56210 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
56220 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
56230 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
56240 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
56250 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
56260 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
56270 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
56280 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
56290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
562e0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
562f0 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 69 74  le (together wit
56300 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43  h is companion C
56310 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c   source-code fil
56320 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74  e.** "os.c") att
56330 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63 74  empt to abstract
56340 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
56350 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
56360 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20   so that.** the 
56370 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77  SQLite library w
56380 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68  ill work on both
56390 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f   POSIX and windo
563a0 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a  ws systems..**.*
563b0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
563c0 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65  le is #include-e
563d0 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68  d by sqliteInt.h
563e0 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75   and thus ends u
563f0 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75  p.** being inclu
56400 64 65 64 20 62 79 20 65 76 65 72 79 20 73 6f 75  ded by every sou
56410 72 63 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  rce file..*/.#if
56420 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f  ndef _SQLITE_OS_
56430 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  H_.#define _SQLI
56440 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  TE_OS_H_../*.** 
56450 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65  Figure out if we
56460 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
56470 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c  h Unix, Windows,
56480 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   or some other.*
56490 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
564a0 65 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66  em.  After the f
564b0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f  ollowing block o
564c0 66 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63  f preprocess mac
564d0 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53  ros,.** all of S
564e0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53  QLITE_OS_UNIX, S
564f0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51  QLITE_OS_WIN, SQ
56500 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64  LITE_OS_OS2, and
56510 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52   SQLITE_OS_OTHER
56520 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65   .** will define
56530 64 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72  d to either 1 or
56540 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20   0.  One of the 
56550 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20  four will be 1. 
56560 20 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74   The other .** t
56570 68 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a  hree will be 0..
56580 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
56590 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a  QLITE_OS_OTHER).
565a0 23 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  # if SQLITE_OS_O
565b0 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65  THER==1.#   unde
565c0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
565d0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
565e0 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20  TE_OS_UNIX 0.#  
565f0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53   undef SQLITE_OS
56600 5f 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20  _WIN.#   define 
56610 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a  SQLITE_OS_WIN 0.
56620 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
56630 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69  _OS_OS2.#   defi
56640 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  ne SQLITE_OS_OS2
56650 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e   0.# else.#   un
56660 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  def SQLITE_OS_OT
56670 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  HER.# endif.#end
56680 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  if.#if !defined(
56690 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20  SQLITE_OS_UNIX) 
566a0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
566b0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64  TE_OS_OTHER).# d
566c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
566d0 4f 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66  OTHER 0.# ifndef
566e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23   SQLITE_OS_WIN.#
566f0 20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57     if defined(_W
56700 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  IN32) || defined
56710 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e  (WIN32) || defin
56720 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c  ed(__CYGWIN__) |
56730 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47  | defined(__MING
56740 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  W32__) || define
56750 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
56760 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
56770 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20  ITE_OS_WIN 1.#  
56780 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
56790 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20  _OS_UNIX 0.#    
567a0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
567b0 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66  S_OS2 0.#   elif
567c0 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f   defined(__EMX__
567d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
567e0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53  2) || defined(OS
567f0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f  2) || defined(_O
56800 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  S2_) || defined(
56810 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64  __OS2__).#     d
56820 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56830 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69  WIN 0.#     defi
56840 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ne SQLITE_OS_UNI
56850 58 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65  X 0.#     define
56860 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31   SQLITE_OS_OS2 1
56870 0a 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20  .#   else.#     
56880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
56890 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66  _WIN 0.#     def
568a0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ine SQLITE_OS_UN
568b0 49 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e  IX 1.#     defin
568c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
568d0 30 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73  0.#  endif.# els
568e0 65 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  e.#  define SQLI
568f0 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20  TE_OS_UNIX 0.#  
56900 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
56910 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23  _OS2 0.# endif.#
56920 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51  else.# ifndef SQ
56930 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64  LITE_OS_WIN.#  d
56940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56950 57 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65  WIN 0.# endif.#e
56960 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  ndif../*.** Dete
56970 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20  rmine if we are 
56980 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e  dealing with Win
56990 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68  dowsCE - which h
569a0 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64  as a much.** red
569b0 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  uced API..*/.#if
569c0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f   defined(_WIN32_
569d0 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51  WCE).# define SQ
569e0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a  LITE_OS_WINCE 1.
569f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
56a00 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30  QLITE_OS_WINCE 0
56a10 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
56a20 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d  Define the maxim
56a30 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d  um size of a tem
56a40 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a  porary filename.
56a50 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
56a60 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c  _WIN.# include <
56a70 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66  windows.h>.# def
56a80 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e  ine SQLITE_TEMPN
56a90 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41  AME_SIZE (MAX_PA
56aa0 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c  TH+50).#elif SQL
56ab0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20  ITE_OS_OS2.# if 
56ac0 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c  (__GNUC__ > 3 ||
56ad0 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26   __GNUC__ == 3 &
56ae0 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f  & __GNUC_MINOR__
56af0 20 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65   >= 3) && define
56b00 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52  d(OS2_HIGH_MEMOR
56b10 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f  Y).#  include <o
56b20 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73  s2safe.h> /* has
56b30 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20   to be included 
56b40 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72  before os2.h for
56b50 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b   linking to work
56b60 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65   */.# endif.# de
56b70 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54  fine INCL_DOSDAT
56b80 45 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49  ETIME.# define I
56b90 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23  NCL_DOSFILEMGR.#
56ba0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
56bb0 45 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20  ERRORS.# define 
56bc0 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64  INCL_DOSMISC.# d
56bd0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52  efine INCL_DOSPR
56be0 4f 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49  OCESS.# define I
56bf0 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52  NCL_DOSMODULEMGR
56c00 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
56c10 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69  OSSEMAPHORES.# i
56c20 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23  nclude <os2.h>.#
56c30 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e   include <uconv.
56c40 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  h>.# define SQLI
56c50 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45  TE_TEMPNAME_SIZE
56c60 20 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50   (CCHMAXPATHCOMP
56c70 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
56c80 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45   SQLITE_TEMPNAME
56c90 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66  _SIZE 200.#endif
56ca0 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f  ../* If the SET_
56cb0 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69  FULLSYNC macro i
56cc0 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62  s not defined ab
56cd0 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69  ove, then make i
56ce0 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a  t.** a no-op.*/.
56cf0 23 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c  #ifndef SET_FULL
56d00 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45  SYNC.# define SE
56d10 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a  T_FULLSYNC(x,y).
56d20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
56d30 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f  e default size o
56d40 66 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a  f a disk sector.
56d50 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
56d60 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
56d70 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
56d80 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
56d90 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65  CTOR_SIZE 512.#e
56da0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70  ndif../*.** Temp
56db0 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20  orary files are 
56dc0 6e 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77  named starting w
56dd0 69 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20  ith this prefix 
56de0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72  followed by 16 r
56df0 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75  andom.** alphanu
56e00 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73  meric characters
56e10 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78  , and no file ex
56e20 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72  tension. They ar
56e30 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a  e stored in the.
56e40 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64  ** OS's standard
56e50 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
56e60 64 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61  directory, and a
56e70 72 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72  re deleted prior
56e80 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20   to exit..** If 
56e90 73 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20  sqlite is being 
56ea0 65 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74  embedded in anot
56eb0 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75  her program, you
56ec0 20 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61   may wish to cha
56ed0 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69  nge the.** prefi
56ee0 78 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75  x to reflect you
56ef0 72 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65  r program's name
56f00 2c 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75  , so that if you
56f10 72 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a  r program exits.
56f20 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20  ** prematurely, 
56f30 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69  old temporary fi
56f40 6c 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c  les can be easil
56f50 79 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68  y identified. Th
56f60 69 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a  is can be done.*
56f70 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45  * using -DSQLITE
56f80 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
56f90 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74  X=myprefix_ on t
56fa0 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d  he compiler comm
56fb0 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20  and line..**.** 
56fc0 32 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65  2006-10-31:  The
56fd0 20 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20   default prefix 
56fe0 75 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69  used to be "sqli
56ff0 74 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a  te_".  But then.
57000 2a 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65  ** Mcafee starte
57010 64 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69  d using SQLite i
57020 6e 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72  n their anti-vir
57030 75 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69  us product and i
57040 74 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74  t.** started put
57050 74 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20  ting files with 
57060 74 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d  the "sqlite" nam
57070 65 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70  e in the c:/temp
57080 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73   folder..** This
57090 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69   annoyed many wi
570a0 6e 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68  ndows users.  Th
570b0 6f 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20  ose users would 
570c0 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f  then do a .** Go
570d0 6f 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20  ogle search for 
570e0 22 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74  "sqlite", find t
570f0 68 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d  he telephone num
57100 62 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64  bers of the.** d
57110 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61  evelopers and ca
57120 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20  ll to wake them 
57130 75 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20  up at night and 
57140 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72  complain..** For
57150 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68   this reason, th
57160 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70  e default name p
57170 72 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64  refix is changed
57180 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20   to be "sqlite" 
57190 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b  .** spelled back
571a0 77 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74  wards.  So the t
571b0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74  emp files are st
571c0 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20  ill identified, 
571d0 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73  but.** anybody s
571e0 6d 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66  mart enough to f
571f0 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f  igure out the co
57200 64 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c  de is also likel
57210 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67  y smart.** enoug
57220 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63  h to know that c
57230 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c  alling the devel
57240 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65  oper will not he
57250 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66  lp get rid.** of
57260 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69   the file..*/.#i
57270 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d  fndef SQLITE_TEM
57280 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20  P_FILE_PREFIX.# 
57290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
572a0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22  MP_FILE_PREFIX "
572b0 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a  etilqs_".#endif.
572c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
572d0 77 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20  wing values may 
572e0 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
572f0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
57300 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73   to.** sqlite3Os
57310 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69  Lock(). The vari
57320 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69  ous locks exhibi
57330 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
57340 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a  semantics:.**.**
57350 20 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20   SHARED:    Any 
57360 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73  number of proces
57370 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53  ses may hold a S
57380 48 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c  HARED lock simul
57390 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45  taneously..** RE
573a0 53 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c  SERVED:  A singl
573b0 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f  e process may ho
573c0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ld a RESERVED lo
573d0 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a  ck on a file at.
573e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e  **            an
573f0 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72  y time. Other pr
57400 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64  ocesses may hold
57410 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20   and obtain new 
57420 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a  SHARED locks..**
57430 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69   PENDING:   A si
57440 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79  ngle process may
57450 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20   hold a PENDING 
57460 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61  lock on a file a
57470 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
57480 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78  any one time. Ex
57490 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f  isting SHARED lo
574a0 63 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c  cks may persist,
574b0 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20   but no new.**  
574c0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
574d0 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62   locks may be ob
574e0 74 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20  tained by other 
574f0 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58  processes..** EX
57500 43 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c  CLUSIVE: An EXCL
57510 55 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c  USIVE lock precl
57520 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c  udes all other l
57530 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44  ocks..**.** PEND
57540 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74  ING_LOCK may not
57550 20 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63   be passed direc
57560 74 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73  tly to sqlite3Os
57570 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c  Lock(). Instead,
57580 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68   a.** process th
57590 61 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45  at requests an E
575a0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61  XCLUSIVE lock ma
575b0 79 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69  y actually obtai
575c0 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c  n a PENDING.** l
575d0 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65  ock. This can be
575e0 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20   upgraded to an 
575f0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62  EXCLUSIVE lock b
57600 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63  y a subsequent c
57610 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
57620 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64  3OsLock()..*/.#d
57630 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20  efine NO_LOCK   
57640 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
57650 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
57660 31 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56  1.#define RESERV
57670 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66  ED_LOCK   2.#def
57680 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ine PENDING_LOCK
57690 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58      3.#define EX
576a0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a  CLUSIVE_LOCK  4.
576b0 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b  ./*.** File Lock
576c0 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73  ing Notes:  (Mos
576d0 74 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77  tly about window
576e0 73 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20  s but also some 
576f0 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a  info for Unix).*
57700 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75  *.** We cannot u
57710 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20  se LockFileEx() 
57720 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28  or UnlockFileEx(
57730 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45  ) on Win95/98/ME
57740 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73   because.** thos
57750 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
57760 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20  not available.  
57770 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c  So we use only L
57780 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a  ockFile() and.**
57790 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a   UnlockFile()..*
577a0 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20  *.** LockFile() 
577b0 70 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73  prevents not jus
577c0 74 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c  t writing but al
577d0 73 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74  so reading by ot
577e0 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a  her processes..*
577f0 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20  * A SHARED_LOCK 
57800 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
57810 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  ocking a single 
57820 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20  randomly-chosen 
57830 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20  .** byte out of 
57840 61 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65  a specific range
57850 20 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c   of bytes. The l
57860 6f 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61  ock byte is obta
57870 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64  ined at .** rand
57880 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61  om so two separa
57890 74 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70  te readers can p
578a0 72 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74  robably access t
578b0 68 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a  he file at the .
578c0 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e  ** same time, un
578d0 6c 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e  less they are un
578e0 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65  lucky and choose
578f0 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62   the same lock b
57900 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55  yte..** An EXCLU
57910 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  SIVE_LOCK is obt
57920 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
57930 20 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68   all bytes in th
57940 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72  e range..** Ther
57950 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e  e can only be on
57960 65 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53  e writer.  A RES
57970 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62  ERVED_LOCK is ob
57980 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e  tained by lockin
57990 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79  g.** a single by
579a0 74 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74  te of the file t
579b0 68 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65  hat is designate
579c0 64 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65  d as the reserve
579d0 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20  d lock byte..** 
579e0 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69  A PENDING_LOCK i
579f0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
57a00 63 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74  cking a designat
57a10 65 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e  ed byte differen
57a20 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45  t from.** the RE
57a30 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65  SERVED_LOCK byte
57a40 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54  ..**.** On WinNT
57a50 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20  /2K/XP systems, 
57a60 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64  LockFileEx() and
57a70 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20   UnlockFileEx() 
57a80 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a  are available,.*
57a90 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65  * which means we
57aa0 20 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f   can use reader/
57ab0 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57  writer locks.  W
57ac0 68 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65  hen reader/write
57ad0 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75  r locks.** are u
57ae0 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73  sed, the lock is
57af0 20 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73   placed on the s
57b00 61 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74  ame range of byt
57b10 65 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a  es that is used.
57b20 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69  ** for probabili
57b30 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20  stic locking in 
57b40 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65  Win95/98/ME.  He
57b50 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67  nce, the locking
57b60 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20   scheme.** will 
57b70 73 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d  support two or m
57b80 6f 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72  ore Win95 reader
57b90 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65  s or two or more
57ba0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a   WinNT readers..
57bb0 2a 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20  ** But a single 
57bc0 57 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c  Win95 reader wil
57bd0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57  l lock out all W
57be0 69 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64  inNT readers and
57bf0 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e   a single.** Win
57c00 4e 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c  NT reader will l
57c10 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65  ock out all othe
57c20 72 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e  r Win95 readers.
57c30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
57c40 77 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70  wing #defines sp
57c50 65 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20  ecify the range 
57c60 6f 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f  of bytes used fo
57c70 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48  r locking..** SH
57c80 41 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65  ARED_SIZE is the
57c90 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
57ca0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
57cb0 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63  e pool from whic
57cc0 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79  h.** a random by
57cd0 74 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66  te is selected f
57ce0 6f 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  or a shared lock
57cf0 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62  .  The pool of b
57d00 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72  ytes for.** shar
57d10 65 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20  ed locks begins 
57d20 61 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e  at SHARED_FIRST.
57d30 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65   .**.** The same
57d40 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
57d50 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61  y and.** byte ra
57d60 6e 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f  nges are used fo
57d70 72 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65  r Unix.  This le
57d80 61 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f  aves open the po
57d90 73 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69  ssiblity of havi
57da0 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e  ng.** clients on
57db0 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61   win95, winNT, a
57dc0 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b  nd unix all talk
57dd0 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
57de0 73 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61  shared file.** a
57df0 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63  nd all locking c
57e00 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f  orrectly.  To do
57e10 20 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72   so would requir
57e20 65 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72  e that samba (or
57e30 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f   whatever.** too
57e40 6c 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  l is being used 
57e50 66 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67  for file sharing
57e60 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63  ) implements loc
57e70 6b 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74  ks correctly bet
57e80 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20  ween.** windows 
57e90 61 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67  and unix.  I'm g
57ea0 75 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e  uessing that isn
57eb0 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70  't likely to hap
57ec0 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75  pen, but by.** u
57ed0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f  sing the same lo
57ee0 63 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61  cking range we a
57ef0 72 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e  re at least open
57f00 20 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c   to the possibil
57f10 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69  ity..**.** Locki
57f20 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73  ng in windows is
57f30 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72   manditory.  For
57f40 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65   this reason, we
57f50 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a   cannot store.**
57f60 20 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20   actual data in 
57f70 74 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66  the bytes used f
57f80 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65  or locking.  The
57f90 20 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c   pager never all
57fa0 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61  ocates.** the pa
57fb0 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20  ges involved in 
57fc0 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72  locking therefor
57fd0 65 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20  e.  SHARED_SIZE 
57fe0 69 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a  is selected so.*
57ff0 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73  * that all locks
58000 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73   will fit on a s
58010 69 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20  ingle page even 
58020 61 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70  at the minimum p
58030 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e  age size..** PEN
58040 44 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65  DING_BYTE define
58050 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  s the beginning 
58060 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42  of the locks.  B
58070 79 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e  y default PENDIN
58080 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74  G_BYTE.** is set
58090 20 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65   high so that we
580a0 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61   don't have to a
580b0 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65  llocate an unuse
580c0 64 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a  d page except.**
580d0 20 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20   for very large 
580e0 64 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20  databases.  But 
580f0 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20  one should test 
58100 74 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e  the page skippin
58110 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73  g logic .** by s
58120 65 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42  etting PENDING_B
58130 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e  YTE low and runn
58140 69 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72  ing the entire r
58150 65 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e  egression suite.
58160 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  .**.** Changing 
58170 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e  the value of PEN
58180 44 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74  DING_BYTE result
58190 73 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e  s in a subtly in
581a0 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69  compatible.** fi
581b0 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65  le format.  Depe
581c0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20  nding on how it 
581d0 69 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20  is changed, you 
581e0 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65  might not notice
581f0 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74  .** the incompat
58200 69 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77  ibility right aw
58210 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67  ay, even running
58220 20 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69   a full regressi
58230 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20  on test..** The 
58240 64 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e  default location
58250 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   of PENDING_BYTE
58260 20 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79   is the first by
58270 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31  te past the.** 1
58280 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a  GB boundary..**.
58290 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49  */.#define PENDI
582a0 4e 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c  NG_BYTE      sql
582b0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a  ite3PendingByte.
582c0 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44  #define RESERVED
582d0 5f 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49  _BYTE     (PENDI
582e0 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69  NG_BYTE+1).#defi
582f0 6e 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  ne SHARED_FIRST 
58300 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59       (PENDING_BY
58310 54 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48  TE+2).#define SH
58320 41 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20  ARED_SIZE       
58330 35 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70  510../*.** Wrapp
58340 65 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65  er around OS spe
58350 63 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73  cific sqlite3_os
58360 5f 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e  _init() function
58370 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
58380 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58390 73 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a  sInit(void);../*
583a0 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66   .** Functions f
583b0 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c  or accessing sql
583c0 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64  ite3_file method
583d0 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s .*/.SQLITE_PRI
583e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
583f0 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  OsClose(sqlite3_
58400 66 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  file*);.SQLITE_P
58410 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58420 65 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33  e3OsRead(sqlite3
58430 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69  _file*, void*, i
58440 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
58450 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  et);.SQLITE_PRIV
58460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58470 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66  sWrite(sqlite3_f
58480 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ile*, const void
58490 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20  *, int amt, i64 
584a0 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f  offset);.SQLITE_
584b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
584c0 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71  te3OsTruncate(sq
584d0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34  lite3_file*, i64
584e0 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50   size);.SQLITE_P
584f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58500 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33  e3OsSync(sqlite3
58510 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  _file*, int);.SQ
58520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
58530 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
58540 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ze(sqlite3_file*
58550 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53  , i64 *pSize);.S
58560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58570 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  t sqlite3OsLock(
58580 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
58590 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
585a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
585b0 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  sUnlock(sqlite3_
585c0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  file*, int);.SQL
585d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
585e0 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65  sqlite3OsCheckRe
585f0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
58600 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
58610 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49   *pResOut);.SQLI
58620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58630 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74  qlite3OsFileCont
58640 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
58650 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64  *,int,void*);.#d
58660 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e  efine SQLITE_FCN
58670 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20  TL_DB_UNCHANGED 
58680 30 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54  0xca093fa0.SQLIT
58690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
586a0 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a  lite3OsSectorSiz
586b0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
586c0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
586d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
586e0 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
586f0 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66  istics(sqlite3_f
58700 69 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a  ile *id);../* .*
58710 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  * Functions for 
58720 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65  accessing sqlite
58730 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a  3_vfs methods .*
58740 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
58750 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70   int sqlite3OsOp
58760 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
58770 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
58780 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
58790 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49  nt, int *);.SQLI
587a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
587b0 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73  qlite3OsDelete(s
587c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
587d0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29  nst char *, int)
587e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
587f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63   int sqlite3OsAc
58800 63 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  cess(sqlite3_vfs
58810 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
58820 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73  , int, int *pRes
58830 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Out);.SQLITE_PRI
58840 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58850 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73  OsFullPathname(s
58860 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
58870 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
58880 20 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65   char *);.#ifnde
58890 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
588a0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c  AD_EXTENSION.SQL
588b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
588c0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65   *sqlite3OsDlOpe
588d0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
588e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
588f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
58900 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45  oid sqlite3OsDlE
58910 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
58920 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29   *, int, char *)
58930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58940 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f   void (*sqlite3O
58950 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  sDlSym(sqlite3_v
58960 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f  fs *, void *, co
58970 6e 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69  nst char *))(voi
58980 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
58990 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
589a0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  sDlClose(sqlite3
589b0 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b  _vfs *, void *);
589c0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
589d0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
589e0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f  NSION */.SQLITE_
589f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
58a00 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28  te3OsRandomness(
58a10 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
58a20 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nt, char *);.SQL
58a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58a40 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73  sqlite3OsSleep(s
58a50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
58a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
58a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
58a80 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69  CurrentTime(sqli
58a90 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c  te3_vfs *, doubl
58aa0 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  e*);../*.** Conv
58ab0 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e  enience function
58ac0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e  s for opening an
58ad0 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20  d closing files 
58ae0 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  using .** sqlite
58af0 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62  3_malloc() to ob
58b00 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74  tain space for t
58b10 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73  he file-handle s
58b20 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
58b30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58b40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c  sqlite3OsOpenMal
58b50 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20  loc(sqlite3_vfs 
58b60 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
58b70 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a   sqlite3_file **
58b80 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c  , int,int*);.SQL
58b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58ba0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72  sqlite3OsCloseFr
58bb0 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ee(sqlite3_file 
58bc0 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  *);..#endif /* _
58bd0 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a  SQLITE_OS_H_ */.
58be0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
58bf0 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a   End of os.h ***
58c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
58c30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
58c40 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
58c50 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
58c60 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
58c80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
58c90 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68   Include mutex.h
58ca0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
58cb0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
58cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
58cd0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
58ce0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
58cf0 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.h ************
58d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
58d20 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
58d30 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 28.**.** The 
58d40 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
58d50 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
58d60 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
58d70 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
58d80 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
58d90 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
58da0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
58db0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
58dc0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
58dd0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
58de0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
58df0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
58e00 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
58e10 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
58e20 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
58e30 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
58e40 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
58e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58e90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
58ea0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
58eb0 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20  e common header 
58ec0 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d  for all mutex im
58ed0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
58ee0 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e  * The sqliteInt.
58ef0 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64  h header #includ
58f00 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20  es this file so 
58f10 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c  that it is avail
58f20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73  able.** to all s
58f30 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65  ource files.  We
58f40 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e   break it out in
58f50 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65   an effort to ke
58f60 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62  ep the code.** b
58f70 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e  etter organized.
58f80 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f  .**.** NOTE:  so
58f90 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c  urce files shoul
58fa0 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65  d *not* #include
58fb0 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   this header fil
58fc0 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53  e directly..** S
58fd0 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75  ource files shou
58fe0 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20  ld #include the 
58ff0 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65  sqliteInt.h file
59000 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69   and let that fi
59010 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68  le.** include th
59020 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c  is one indirectl
59030 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69  y..*/.../*.** Fi
59040 67 75 72 65 20 6f 75 74 20 77 68 61 74 20 76 65  gure out what ve
59050 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64  rsion of the cod
59060 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63  e to use.  The c
59070 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a  hoices are.**.**
59080 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
59090 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20  OMIT         No 
590a0 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f  mutex logic.  No
590b0 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54  t even stubs.  T
590c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
590d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
590e0 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d    mutexes implem
590f0 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65  ention cannot be
59100 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20   overridden.**  
59110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59120 20 20 20 20 20 20 20 20 20 20 20 61 74 20 73 74             at st
59130 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  art-time..**.** 
59140 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e    SQLITE_MUTEX_N
59150 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f 72 20  OOP         For 
59160 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
59170 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e  applications.  N
59180 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
59190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591a0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
591b0 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20  n is provided.  
591c0 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20  But this.**     
591d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591e0 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e          implemen
591f0 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76  tation can be ov
59200 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20  erridden at.**  
59210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59220 20 20 20 20 20 20 20 20 20 20 20 73 74 61 72 74             start
59230 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53  -time..**.**   S
59240 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
59250 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c  EADS     For mul
59260 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c  ti-threaded appl
59270 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78  ications on Unix
59280 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45  ..**.**   SQLITE
59290 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 20 20  _MUTEX_W32      
592a0 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68      For multi-th
592b0 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
592c0 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a  ons on Win32..**
592d0 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  .**   SQLITE_MUT
592e0 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20  EX_OS2          
592f0 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64  For multi-thread
59300 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ed applications 
59310 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20  on OS/2..*/.#if 
59320 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  !SQLITE_THREADSA
59330 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  FE.# define SQLI
59340 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65  TE_MUTEX_OMIT.#e
59350 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
59360 54 48 52 45 41 44 53 41 46 45 20 26 26 20 21 64  THREADSAFE && !d
59370 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55  efined(SQLITE_MU
59380 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20  TEX_NOOP).#  if 
59390 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23  SQLITE_OS_UNIX.#
593a0 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
593b0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53  E_MUTEX_PTHREADS
593c0 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f  .#  elif SQLITE_
593d0 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69  OS_WIN.#    defi
593e0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
593f0 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49  W32.#  elif SQLI
59400 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64  TE_OS_OS2.#    d
59410 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
59420 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23  EX_OS2.#  else.#
59430 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
59440 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20  E_MUTEX_NOOP.#  
59450 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69  endif.#endif..#i
59460 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
59470 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20  X_OMIT./*.** If 
59480 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20  this is a no-op 
59490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
594a0 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74  implement everyt
594b0 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a  hing as macros..
594c0 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  */.#define sqlit
594d0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58  e3_mutex_alloc(X
594e0 29 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d  )    ((sqlite3_m
594f0 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65  utex*)8).#define
59500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
59510 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73  ree(X).#define s
59520 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
59530 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71  er(X).#define sq
59540 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
59550 58 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  X)      SQLITE_O
59560 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  K.#define sqlite
59570 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29  3_mutex_leave(X)
59580 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
59590 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20  _mutex_held(X)  
595a0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c     1.#define sql
595b0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
595c0 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65  ld(X)  1.#define
595d0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
595e0 6f 63 28 58 29 20 20 20 20 20 20 28 28 73 71 6c  oc(X)      ((sql
595f0 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23  ite3_mutex*)8).#
59600 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75  define sqlite3Mu
59610 74 65 78 49 6e 69 74 28 29 20 20 20 20 20 20 20  texInit()       
59620 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69   SQLITE_OK.#defi
59630 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45  ne sqlite3MutexE
59640 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64  nd().#endif /* d
59650 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55  efined(SQLITE_MU
59660 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a  TEX_OMIT) */../*
59670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
59680 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a  d of mutex.h ***
59690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
596a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
596b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
596c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
596d0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
596e0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
596f0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
59700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a  ************/...
59710 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62  /*.** Each datab
59720 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61  ase file to be a
59730 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 73  ccessed by the s
59740 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74  ystem is an inst
59750 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ance.** of the f
59760 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
59770 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e  re.  There are n
59780 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74  ormally two of t
59790 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 0a  hese structures.
597a0 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ** in the sqlite
597b0 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61  .aDb[] array.  a
597c0 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69  Db[0] is the mai
597d0 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
597e0 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73  and.** aDb[1] is
597f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
59800 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  le used to hold 
59810 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
59820 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  .  Additional.**
59830 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 20 62   databases may b
59840 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73  e attached..*/.s
59850 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61  truct Db {.  cha
59860 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
59870 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69    /* Name of thi
59880 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
59890 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20  Btree *pBt;     
598a0 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72       /* The B*Tr
598b0 65 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  ee structure for
598c0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66   this database f
598d0 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72  ile */.  u8 inTr
598e0 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ans;          /*
598f0 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65   0: not writable
59900 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f  .  1: Transactio
59910 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e  n.  2: Checkpoin
59920 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79  t */.  u8 safety
59930 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48  _level;     /* H
59940 6f 77 20 61 67 67 72 65 73 73 69 76 65 20 61 74  ow aggressive at
59950 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20 74 6f   syncing data to
59960 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63 68 65 6d   disk */.  Schem
59970 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
59980 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
59990 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28 70  tabase schema (p
599a0 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64 29 20  ossibly shared) 
599b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
599c0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
599d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
599e0 75 72 65 20 73 74 6f 72 65 73 20 61 20 64 61 74  ure stores a dat
599f0 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a  abase schema..**
59a00 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
59a10 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c   no virtual tabl
59a20 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e  es configured in
59a30 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68   this schema, th
59a40 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76  e.** Schema.db v
59a50 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
59a60 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68  o NULL. After th
59a70 65 20 66 69 72 73 74 20 76 69 72 74 75 61 6c 20  e first virtual 
59a80 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65  table.** has bee
59a90 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73 20 73  n added, it is s
59aa0 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
59ab0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
59ac0 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20  ection .** used 
59ad0 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 63 6f  to create the co
59ae0 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61  nnection. Once a
59af0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   virtual table h
59b00 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64  as been.** added
59b10 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73   to the Schema s
59b20 74 72 75 63 74 75 72 65 20 61 6e 64 20 74 68 65  tructure and the
59b30 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61   Schema.db varia
59b40 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a  ble populated, .
59b50 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74  ** only that dat
59b60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
59b70 20 6d 61 79 20 75 73 65 20 74 68 65 20 53 63 68   may use the Sch
59b80 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a  ema to prepare .
59b90 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ** statements..*
59ba0 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d 61 20  /.struct Schema 
59bb0 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63  {.  int schema_c
59bc0 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61  ookie;   /* Data
59bd0 62 61 73 65 20 73 63 68 65 6d 61 20 76 65 72 73  base schema vers
59be0 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ion number for t
59bf0 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61  his file */.  Ha
59c00 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20  sh tblHash;     
59c10 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73     /* All tables
59c20 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65   indexed by name
59c30 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61   */.  Hash idxHa
59c40 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c  sh;        /* Al
59c50 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65  l (named) indice
59c60 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
59c70 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67  e */.  Hash trig
59c80 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41  Hash;       /* A
59c90 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e 64 65  ll triggers inde
59ca0 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20  xed by name */. 
59cb0 20 48 61 73 68 20 66 6b 65 79 48 61 73 68 3b 20   Hash fkeyHash; 
59cc0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 6f 72        /* All for
59cd0 65 69 67 6e 20 6b 65 79 73 20 62 79 20 72 65 66  eign keys by ref
59ce0 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 6e 61  erenced table na
59cf0 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  me */.  Table *p
59d00 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20  SeqTab;      /* 
59d10 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  The sqlite_seque
59d20 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62  nce table used b
59d30 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  y AUTOINCREMENT 
59d40 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72  */.  u8 file_for
59d50 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68  mat;      /* Sch
59d60 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69  ema format versi
59d70 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  on for this file
59d80 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
59d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
59da0 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  xt encoding used
59db0 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
59dc0 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  e */.  u16 flags
59dd0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
59de0 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20  lags associated 
59df0 77 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61  with this schema
59e00 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f   */.  int cache_
59e10 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75  size;      /* Nu
59e20 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f  mber of pages to
59e30 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68   use in the cach
59e40 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  e */.#ifndef SQL
59e50 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
59e60 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20  TABLE.  sqlite3 
59e70 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
59e80 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69  "Owner" connecti
59e90 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20  on. See comment 
59ea0 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a  above */.#endif.
59eb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20  };../*.** These 
59ec0 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
59ed0 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
59ee0 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
59ef0 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61  n the .** Db.fla
59f00 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65  gs field..*/.#de
59f10 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72  fine DbHasProper
59f20 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28  ty(D,I,P)     ((
59f30 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
59f40 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d  ema->flags&(P))=
59f50 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62  =(P)).#define Db
59f60 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44  HasAnyProperty(D
59f70 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44  ,I,P)  (((D)->aD
59f80 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c  b[I].pSchema->fl
59f90 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65  ags&(P))!=0).#de
59fa0 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72  fine DbSetProper
59fb0 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44  ty(D,I,P)     (D
59fc0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
59fd0 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64  a->flags|=(P).#d
59fe0 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f  efine DbClearPro
59ff0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28  perty(D,I,P)   (
5a000 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65  D)->aDb[I].pSche
5a010 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a  ma->flags&=~(P).
5a020 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
5a030 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42  alues for the DB
5a040 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a  .flags field..**
5a050 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d  .** The DB_Schem
5a060 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20  aLoaded flag is 
5a070 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61  set after the da
5a080 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
5a090 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69  s been.** read i
5a0a0 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  nto internal has
5a0b0 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  h tables..**.** 
5a0c0 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20  DB_UnresetViews 
5a0d0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f  means that one o
5a0e0 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76  r more views hav
5a0f0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74  e column names t
5a100 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  hat.** have been
5a110 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66   filled out.  If
5a120 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e   the schema chan
5a130 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d  ges, these colum
5a140 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a  n names might.**
5a150 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20   changes and so 
5a160 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65  the view will ne
5a170 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a  ed to be reset..
5a180 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63  */.#define DB_Sc
5a190 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78  hemaLoaded    0x
5a1a0 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68  0001  /* The sch
5a1b0 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  ema has been loa
5a1c0 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  ded */.#define D
5a1d0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20  B_UnresetViews  
5a1e0 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d    0x0002  /* Som
5a1f0 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66  e views have def
5a200 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  ined column name
5a210 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  s */.#define DB_
5a220 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20  Empty           
5a230 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66  0x0004  /* The f
5a240 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65  ile is empty (le
5a250 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f  ngth 0 bytes) */
5a260 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62  ../*.** The numb
5a270 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20  er of different 
5a280 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20  kinds of things 
5a290 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69  that can be limi
5a2a0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ted.** using the
5a2b0 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29   sqlite3_limit()
5a2c0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
5a2d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f  define SQLITE_N_
5a2e0 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49  LIMIT (SQLITE_LI
5a2f0 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
5a300 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b  H+1)../*.** Look
5a310 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20  aside malloc is 
5a320 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73  a set of fixed-s
5a330 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74  ize buffers that
5a340 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20   can be used.** 
5a350 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c  to satisfy small
5a360 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72   transient memor
5a370 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
5a380 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74  uests for object
5a390 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  s.** associated 
5a3a0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
5a3b0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
5a3c0 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20  ction.  The use 
5a3d0 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  of.** lookaside 
5a3e0 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20  malloc provides 
5a3f0 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65  a significant pe
5a400 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63  rformance enhanc
5a410 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78  ement.** (approx
5a420 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e   10%) by avoidin
5a430 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f  g numerous mallo
5a440 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20  c/free requests 
5a450 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a  while parsing.**
5a460 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
5a470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61  .**.** The Looka
5a480 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68  side structure h
5a490 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69  olds configurati
5a4a0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  on information a
5a4b0 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b  bout the.** look
5a4c0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62  aside malloc sub
5a4d0 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76  system.  Each av
5a4e0 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61  ailable memory a
5a4f0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20  llocation in.** 
5a500 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75  the lookaside su
5a510 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65  bsystem is store
5a520 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d on a linked li
5a530 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53  st of LookasideS
5a540 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a  lot.** objects..
5a550 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20  **.** Lookaside 
5a560 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20  allocations are 
5a570 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72  only allowed for
5a580 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72   objects that ar
5a590 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  e associated.** 
5a5a0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
5a5b0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
5a5c0 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73  ction.  Hence, s
5a5d0 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
5a5e0 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73  n cannot.** be s
5a5f0 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69  tored in lookasi
5a600 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68  de because in sh
5a610 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20  ared cache mode 
5a620 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  the schema infor
5a630 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61  mation.** is sha
5a640 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20  red by multiple 
5a650 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5a660 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65  ions.  Therefore
5a670 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a  , while parsing.
5a680 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d  ** schema inform
5a690 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61  ation, the Looka
5a6a0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c  side.bEnabled fl
5a6b0 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f  ag is cleared so
5a6c0 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69   that.** lookasi
5a6d0 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61  de allocations a
5a6e0 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63  re not used to c
5a6f0 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68  onstruct the sch
5a700 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a  ema objects..*/.
5a710 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65  struct Lookaside
5a720 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20   {.  u16 sz;    
5a730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5a740 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66  Size of each buf
5a750 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
5a760 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20    u8 bEnabled;  
5a770 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
5a780 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65  se to disable ne
5a790 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f  w lookaside allo
5a7a0 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20  cations */.  u8 
5a7b0 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20  bMalloced;      
5a7c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
5a7d0 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20  pStart obtained 
5a7e0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
5a7f0 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  loc() */.  int n
5a800 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
5a810 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5a820 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c  buffers currentl
5a830 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  y checked out */
5a840 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20  .  int mxOut;   
5a850 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69             /* Hi
5a860 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72  ghwater mark for
5a870 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61   nOut */.  Looka
5a880 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b  sideSlot *pFree;
5a890 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76     /* List of av
5a8a0 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20  ailable buffers 
5a8b0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72  */.  void *pStar
5a8c0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
5a8d0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76  First byte of av
5a8e0 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73  ailable memory s
5a8f0 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  pace */.  void *
5a900 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20  pEnd;           
5a910 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
5a920 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69  past end of avai
5a930 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d  lable space */.}
5a940 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69  ;.struct Lookasi
5a950 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61  deSlot {.  Looka
5a960 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b  sideSlot *pNext;
5a970 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66      /* Next buff
5a980 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f  er in the list o
5a990 66 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a  f free buffers *
5a9a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61  /.};../*.** A ha
5a9b0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e  sh table for fun
5a9c0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
5a9d0 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61  s..**.** Hash ea
5a9e0 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ch FuncDef struc
5a9f0 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66  ture into one of
5aa00 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68   the FuncDefHash
5aa10 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43  .a[] slots..** C
5aa20 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e  ollisions are on
5aa30 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61   the FuncDef.pHa
5aa40 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72  sh chain..*/.str
5aa50 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20  uct FuncDefHash 
5aa60 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32  {.  FuncDef *a[2
5aa70 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73  3];       /* Has
5aa80 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63  h table for func
5aa90 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  tions */.};../*.
5aaa0 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65  ** Each database
5aab0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
5aac0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
5aad0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
5aae0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73  * The sqlite.las
5aaf0 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74  tRowid records t
5ab00 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72  he last insert r
5ab10 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62  owid generated b
5ab20 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73  y an.** insert s
5ab30 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72  tatement.  Inser
5ab40 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e  ts on views do n
5ab50 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61  ot affect its va
5ab60 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72  lue.  Each.** tr
5ab70 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77  igger has its ow
5ab80 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68  n context, so th
5ab90 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e  at lastRowid can
5aba0 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69   be updated insi
5abb0 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61  de.** triggers a
5abc0 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72  s usual.  The pr
5abd0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c  evious value wil
5abe0 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e  l be restored on
5abf0 63 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a  ce the trigger.*
5ac00 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65  * exits.  Upon e
5ac10 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65  ntering a before
5ac20 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74   or instead of t
5ac30 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69  rigger, lastRowi
5ac40 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65  d is no.** longe
5ac50 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76  r (since after v
5ac60 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72  ersion 2.8.12) r
5ac70 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a  eset to -1..**.*
5ac80 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68  * The sqlite.nCh
5ac90 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ange does not co
5aca0 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68  unt changes with
5acb0 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  in triggers and 
5acc0 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74  keeps no.** cont
5acd0 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65  ext.  It is rese
5ace0 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71  t at start of sq
5acf0 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54  lite3_exec..** T
5ad00 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e  he sqlite.lsChan
5ad10 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  ge represents th
5ad20 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  e number of chan
5ad30 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  ges made by the 
5ad40 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20  last.** insert, 
5ad50 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74  update, or delet
5ad60 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74  e statement.  It
5ad70 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e   remains constan
5ad80 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65  t throughout the
5ad90 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  .** length of a 
5ada0 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73  statement and is
5adb0 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79   then updated by
5adc0 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20   OP_SetCounts.  
5add0 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f  It keeps a.** co
5ade0 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74  ntext stack just
5adf0 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20   like lastRowid 
5ae00 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e  so that the coun
5ae10 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20  t of changes.** 
5ae20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72  within a trigger
5ae30 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74   is not seen out
5ae40 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72  side the trigger
5ae50 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69  .  Changes to vi
5ae60 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66  ews do not.** af
5ae70 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f  fect the value o
5ae80 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54  f lsChange..** T
5ae90 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e  he sqlite.csChan
5aea0 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f  ge keeps track o
5aeb0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
5aec0 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20  current changes 
5aed0 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61  (since.** the la
5aee0 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e  st statement) an
5aef0 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64  d is used to upd
5af00 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61  ate sqlite_lsCha
5af10 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  nge..**.** The m
5af20 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20  ember variables 
5af30 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20  sqlite.errCode, 
5af40 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61  sqlite.zErrMsg a
5af50 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73  nd sqlite.zErrMs
5af60 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65  g16.** store the
5af70 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
5af80 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20  or code and, if 
5af90 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69  applicable, stri
5afa0 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72  ng. The.** inter
5afb0 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  nal function sql
5afc0 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75  ite3Error() is u
5afd0 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65  sed to set these
5afe0 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f   variables.** co
5aff0 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73  nsistently..*/.s
5b000 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a  truct sqlite3 {.
5b010 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
5b020 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Vfs;            
5b030 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20  /* OS Interface 
5b040 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20  */.  int nDb;   
5b050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b060 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5b070 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74  backends current
5b080 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44  ly in use */.  D
5b090 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20  b *aDb;         
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b0b0 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a  All backends */.
5b0c0 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0e0 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73  /* Miscellaneous
5b0f0 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f   flags. See belo
5b100 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  w */.  int openF
5b110 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
5b120 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
5b130 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f  ssed to sqlite3_
5b140 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
5b150 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20   int errCode;   
5b160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b170 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
5b180 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45  ror code (SQLITE
5b190 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72  _*) */.  int err
5b1a0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Mask;           
5b1b0 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75         /* & resu
5b1c0 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68  lt codes with th
5b1d0 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  is before return
5b1e0 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f  ing */.  u8 auto
5b1f0 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20  Commit;         
5b200 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75         /* The au
5b210 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20  to-commit flag. 
5b220 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f  */.  u8 temp_sto
5b230 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  re;             
5b240 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a     /* 1: file 2:
5b250 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75   memory 0: defau
5b260 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f  lt */.  u8 mallo
5b270 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20  cFailed;        
5b280 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
5b290 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20   we have seen a 
5b2a0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a  malloc failure *
5b2b0 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d  /.  u8 dfltLockM
5b2c0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
5b2d0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63    /* Default loc
5b2e0 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74  king-mode for at
5b2f0 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20  tached dbs */.  
5b300 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  u8 dfltJournalMo
5b310 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  de;           /*
5b320 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c   Default journal
5b330 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68   mode for attach
5b340 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e  ed dbs */.  sign
5b350 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f  ed char nextAuto
5b360 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74  vac;      /* Aut
5b370 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74  ovac setting aft
5b380 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30  er VACUUM if >=0
5b390 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61   */.  int nextPa
5b3a0 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  gesize;         
5b3b0 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20      /* Pagesize 
5b3c0 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20  after VACUUM if 
5b3d0 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62  >0 */.  int nTab
5b3e0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
5b3f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5b400 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
5b410 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43   database */.  C
5b420 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c  ollSeq *pDfltCol
5b430 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
5b440 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c  The default coll
5b450 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28  ating sequence (
5b460 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34  BINARY) */.  i64
5b470 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
5b480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f             /* RO
5b490 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65  WID of most rece
5b4a0 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61  nt insert (see a
5b4b0 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d  bove) */.  u32 m
5b4c0 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20  agic;           
5b4d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
5b4e0 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74  c number for det
5b4f0 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75  ect library misu
5b500 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61  se */.  int nCha
5b510 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  nge;            
5b520 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
5b530 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
5b540 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  e3_changes() */.
5b550 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e    int nTotalChan
5b560 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
5b570 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65  /* Value returne
5b580 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74  d by sqlite3_tot
5b590 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a  al_changes() */.
5b5a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
5b5b0 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  *mutex;         
5b5c0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  /* Connection mu
5b5d0 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69  tex */.  int aLi
5b5e0 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  mit[SQLITE_N_LIM
5b5f0 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73  IT];   /* Limits
5b600 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c   */.  struct sql
5b610 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20  ite3InitInfo {  
5b620 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
5b630 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69  on used during i
5b640 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f  nitialization */
5b650 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20  .    int iDb;   
5b660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b670 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73   /* When back is
5b680 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
5b690 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65  ed */.    int ne
5b6a0 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20  wTnum;          
5b6b0 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67        /* Rootpag
5b6c0 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67  e of table being
5b6d0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
5b6e0 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20      u8 busy;    
5b6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b700 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65  /* TRUE if curre
5b710 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ntly initializin
5b720 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68  g */.    u8 orph
5b730 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  anTrigger;      
5b740 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61       /* Last sta
5b750 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e  tement is orphan
5b760 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20  ed TEMP trigger 
5b770 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69  */.  } init;.  i
5b780 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20  nt nExtension;  
5b790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b7a0 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64  Number of loaded
5b7b0 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20   extensions */. 
5b7c0 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69   void **aExtensi
5b7d0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  on;            /
5b7e0 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65  * Array of share
5b7f0 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65  d library handle
5b800 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64  s */.  struct Vd
5b810 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20  be *pVdbe;      
5b820 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
5b830 61 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d  active virtual m
5b840 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74  achines */.  int
5b850 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20   activeVdbeCnt; 
5b860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5b870 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75  mber of VDBEs cu
5b880 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
5b890 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65  g */.  int write
5b8a0 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20  VdbeCnt;        
5b8b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5b8c0 66 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74  f active VDBEs t
5b8d0 68 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20  hat are writing 
5b8e0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61  */.  void (*xTra
5b8f0 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
5b900 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f  char*);        /
5b910 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  * Trace function
5b920 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61   */.  void *pTra
5b930 63 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  ceArg;          
5b940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b950 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
5b960 68 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f  he trace functio
5b970 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50  n */.  void (*xP
5b980 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f  rofile)(void*,co
5b990 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20  nst char*,u64); 
5b9a0 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75   /* Profiling fu
5b9b0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
5b9c0 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20   *pProfileArg;  
5b9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b9e0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
5b9f0 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e  t to profile fun
5ba00 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
5ba10 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20  *pCommitArg;    
5ba20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5ba30 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d  Argument to xCom
5ba40 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f  mitCallback() */
5ba50 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d     .  int (*xCom
5ba60 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  mitCallback)(voi
5ba70 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  d*);    /* Invok
5ba80 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d  ed at every comm
5ba90 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  it. */.  void *p
5baa0 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20  RollbackArg;    
5bab0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
5bac0 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62  gument to xRollb
5bad0 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f  ackCallback() */
5bae0 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f     .  void (*xRo
5baf0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28  llbackCallback)(
5bb00 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b  void*); /* Invok
5bb10 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d  ed at every comm
5bb20 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  it. */.  void *p
5bb30 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69  UpdateArg;.  voi
5bb40 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62  d (*xUpdateCallb
5bb50 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20  ack)(void*,int, 
5bb60 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
5bb70 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69  t char*,sqlite_i
5bb80 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78  nt64);.  void(*x
5bb90 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64  CollNeeded)(void
5bba0 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
5bbb0 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
5bbc0 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43  ar*);.  void(*xC
5bbd0 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69  ollNeeded16)(voi
5bbe0 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
5bbf0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76  eTextRep,const v
5bc00 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70  oid*);.  void *p
5bc10 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20  CollNeededArg;. 
5bc20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
5bc30 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f  pErr;          /
5bc40 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
5bc50 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  ror message */. 
5bc60 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
5bc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5bc80 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
5bc90 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46  ror message (UTF
5bca0 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20  -8 encoded) */. 
5bcb0 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36   char *zErrMsg16
5bcc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5bcd0 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
5bce0 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46  ror message (UTF
5bcf0 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a  -16 encoded) */.
5bd00 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f    union {.    vo
5bd10 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74  latile int isInt
5bd20 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75  errupted; /* Tru
5bd30 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74  e if sqlite3_int
5bd40 65 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20  errupt has been 
5bd50 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f  called */.    do
5bd60 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20  uble notUsed1;  
5bd70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
5bd80 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20  cer */.  } u1;. 
5bd90 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61   Lookaside looka
5bda0 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  side;          /
5bdb0 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * Lookaside mall
5bdc0 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  oc configuration
5bdd0 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
5bde0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
5bdf0 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41  ATION.  int (*xA
5be00 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
5be10 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
5be20 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
5be30 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  r*,const char*);
5be40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5be50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5be60 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f   /* Access autho
5be70 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
5be80 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  n */.  void *pAu
5be90 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  thArg;          
5bea0 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75       /* 1st argu
5beb0 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65  ment to the acce
5bec0 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  ss auth function
5bed0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64   */.#endif.#ifnd
5bee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
5bef0 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
5bf00 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65  .  int (*xProgre
5bf10 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20  ss)(void *);    
5bf20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73   /* The progress
5bf30 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76   callback */.  v
5bf40 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72  oid *pProgressAr
5bf50 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
5bf60 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
5bf70 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
5bf80 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67  k */.  int nProg
5bf90 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20  ressOps;        
5bfa0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5bfb0 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72  f opcodes for pr
5bfc0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
5bfd0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
5bfe0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
5bff0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73  RTUALTABLE.  Has
5c000 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20  h aModule;      
5c010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f             /* po
5c020 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74  pulated by sqlit
5c030 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
5c040 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  () */.  Table *p
5c050 56 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  VTab;           
5c060 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69        /* vtab wi
5c070 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63  th active Connec
5c080 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20  t/Create method 
5c090 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56  */.  VTable **aV
5c0a0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20  Trans;          
5c0b0 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
5c0c0 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74  bles with open t
5c0d0 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20  ransactions */. 
5c0e0 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20   int nVTrans;   
5c0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5c100 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  * Allocated size
5c110 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20   of aVTrans */. 
5c120 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e   VTable *pDiscon
5c130 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 73 63  nect;    /* Disc
5c140 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69 6e 20  onnect these in 
5c150 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70 72 65  next sqlite3_pre
5c160 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66  pare() */.#endif
5c170 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61  .  FuncDefHash a
5c180 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Func;           
5c190 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f   /* Hash table o
5c1a0 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e  f connection fun
5c1b0 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68  ctions */.  Hash
5c1c0 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20   aCollSeq;      
5c1d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
5c1e0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5c1f0 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61  nces */.  BusyHa
5c200 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65  ndler busyHandle
5c210 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20  r;      /* Busy 
5c220 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
5c230 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20  t busyTimeout;  
5c240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
5c250 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65  usy handler time
5c260 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a  out, in msec */.
5c270 20 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32    Db aDbStatic[2
5c280 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
5c290 2f 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20  /* Static space 
5c2a0 66 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c  for the 2 defaul
5c2b0 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20  t backends */.  
5c2c0 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
5c2d0 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  point;        /*
5c2e0 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20   List of active 
5c2f0 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20  savepoints */.  
5c300 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20  int nSavepoint; 
5c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5c320 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74   Number of non-t
5c330 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70  ransaction savep
5c340 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  oints */.  int n
5c350 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20  Statement;      
5c360 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5c370 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74 61  er of nested sta
5c380 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69  tement-transacti
5c390 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73 54  ons  */.  u8 isT
5c3a0 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f  ransactionSavepo
5c3b0 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  int;    /* True 
5c3c0 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74  if the outermost
5c3d0 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 20   savepoint is a 
5c3e0 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e 44 65 66  TS */.  i64 nDef
5c3f0 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 20 20 20  erredCons;      
5c400 20 20 20 20 20 20 2f 2a 20 4e 65 74 20 64 65 66        /* Net def
5c410 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  erred constraint
5c420 73 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  s this transacti
5c430 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  on. */..#ifdef S
5c440 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c  QLITE_ENABLE_UNL
5c450 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20  OCK_NOTIFY.  /* 
5c460 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
5c470 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20  riables are all 
5c480 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
5c490 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a   STATIC_MASTER .
5c4a0 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20    ** mutex, not 
5c4b0 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78  by sqlite3.mutex
5c4c0 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20  . They are used 
5c4d0 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66  by code in notif
5c4e0 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  y.c. .  **.  ** 
5c4f0 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f  When X.pUnlockCo
5c500 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61  nnection==Y, tha
5c510 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69  t means that X i
5c520 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20  s waiting for Y 
5c530 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73  to.  ** unlock s
5c540 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 70 72  o that it can pr
5c550 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  oceed..  **.  **
5c560 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e   When X.pBlockin
5c570 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20  gConnection==Y, 
5c580 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
5c590 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58  something that X
5c5a0 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65   tried.  ** trie
5c5b0 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79  d to do recently
5c5c0 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20   failed with an 
5c5d0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72  SQLITE_LOCKED er
5c5e0 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73  ror due to locks
5c5f0 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e  .  ** held by Y.
5c600 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  .  */.  sqlite3 
5c610 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  *pBlockingConnec
5c620 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74  tion; /* Connect
5c630 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 20  ion that caused 
5c640 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f  SQLITE_LOCKED */
5c650 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c  .  sqlite3 *pUnl
5c660 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20  ockConnection;  
5c670 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
5c680 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20  ection to watch 
5c690 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20  for unlock */.  
5c6a0 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67  void *pUnlockArg
5c6b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5c6c0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
5c6d0 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69  t to xUnlockNoti
5c6e0 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  fy */.  void (*x
5c6f0 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f  UnlockNotify)(vo
5c700 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a  id **, int);  /*
5c710 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63   Unlock notify c
5c720 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c  allback */.  sql
5c730 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b  ite3 *pNextBlock
5c740 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  ed;        /* Ne
5c750 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c  xt in list of al
5c760 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  l blocked connec
5c770 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  tions */.#endif.
5c780 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72  };../*.** A macr
5c790 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68  o to discover th
5c7a0 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  e encoding of a 
5c7b0 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65  database..*/.#de
5c7c0 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64  fine ENC(db) ((d
5c7d0 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65  b)->aDb[0].pSche
5c7e0 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20  ma->enc)../*.** 
5c7f0 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
5c800 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66  for the sqlite.f
5c810 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66  lags and or Db.f
5c820 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a  lags fields..**.
5c830 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61  ** On sqlite.fla
5c840 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49  gs, the SQLITE_I
5c850 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61  nTrans value mea
5c860 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 65 0a  ns that we have.
5c870 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 42 45  ** executed a BE
5c880 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67  GIN.  On Db.flag
5c890 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e  s, SQLITE_InTran
5c8a0 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d  s means a statem
5c8b0 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ent.** transacti
5c8c0 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20  on is active on 
5c8d0 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 72 20  that particular 
5c8e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
5c8f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5c900 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 20  _VdbeTrace      
5c910 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54  0x00000001  /* T
5c920 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 44 42  rue to trace VDB
5c930 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23  E execution */.#
5c940 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e  define SQLITE_In
5c950 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 78 30  Trans        0x0
5c960 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65  0000008  /* True
5c970 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63   if in a transac
5c980 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
5c990 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
5c9a0 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 31 30  nges  0x00000010
5c9b0 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64    /* Uncommitted
5c9c0 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e   Hash table chan
5c9d0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ges */.#define S
5c9e0 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
5c9f0 65 73 20 20 20 30 78 30 30 30 30 30 30 32 30 20  es   0x00000020 
5ca00 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f   /* Show full co
5ca10 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45  lumn names on SE
5ca20 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LECT */.#define 
5ca30 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
5ca40 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 34 30  ames  0x00000040
5ca50 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20    /* Show short 
5ca60 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f  columns names */
5ca70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5ca80 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30  CountRows      0
5ca90 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f  x00000080  /* Co
5caa0 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64  unt rows changed
5cab0 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20   by INSERT, */. 
5cac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cae0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45           /*   DE
5caf0 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20  LETE, or UPDATE 
5cb00 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20  and return */.  
5cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb30 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65          /*   the
5cb40 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63   count using a c
5cb50 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66  allback. */.#def
5cb60 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43  ine SQLITE_NullC
5cb70 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30  allback   0x0000
5cb80 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  0100  /* Invoke 
5cb90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63  the callback onc
5cba0 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20  e if the */.    
5cbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cbd0 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c        /*   resul
5cbe0 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a  t set is empty *
5cbf0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5cc00 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 20  _SqlTrace       
5cc10 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44  0x00000200  /* D
5cc20 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61  ebug print SQL a
5cc30 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f  s it executes */
5cc40 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cc50 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30  VdbeListing    0
5cc60 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65  x00000400  /* De
5cc70 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20  bug listings of 
5cc80 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f  VDBE programs */
5cc90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cca0 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20 30  WriteSchema    0
5ccb0 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b  x00000800  /* OK
5ccc0 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54   to update SQLIT
5ccd0 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66  E_MASTER */.#def
5cce0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61  ine SQLITE_NoRea
5ccf0 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30  dlock     0x0000
5cd00 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63  1000  /* Readloc
5cd10 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77  ks are omitted w
5cd20 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20  hen .           
5cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
5cd50 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 61 64  * accessing read
5cd60 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20  -only databases 
5cd70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5cd80 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20  E_IgnoreChecks  
5cd90 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20   0x00002000  /* 
5cda0 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63  Do not enforce c
5cdb0 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
5cdc0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5cdd0 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
5cde0 65 64 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a  ed 0x00004000 /*
5cdf0 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68   For shared-cach
5ce00 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e  e mode */.#defin
5ce10 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46  e SQLITE_LegacyF
5ce20 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 38 30  ileFmt  0x000080
5ce30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65  00  /* Create ne
5ce40 77 20 64 61 74 61 62 61 73 65 73 20 69 6e 20 66  w databases in f
5ce50 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69  ormat 1 */.#defi
5ce60 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53  ne SQLITE_FullFS
5ce70 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 31 30  ync      0x00010
5ce80 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c  000  /* Use full
5ce90 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61   fsync on the ba
5cea0 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ckend */.#define
5ceb0 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65   SQLITE_LoadExte
5cec0 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30  nsion  0x0002000
5ced0 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61  0  /* Enable loa
5cee0 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a  d_extension */..
5cef0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
5cf00 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78  ecoveryMode   0x
5cf10 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e  00040000  /* Ign
5cf20 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72  ore schema error
5cf30 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
5cf40 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72  ITE_ReverseOrder
5cf50 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f     0x00100000  /
5cf60 2a 20 52 65 76 65 72 73 65 20 75 6e 6f 72 64 65  * Reverse unorde
5cf70 72 65 64 20 53 45 4c 45 43 54 73 20 2a 2f 0a 23  red SELECTs */.#
5cf80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65  define SQLITE_Re
5cf90 63 54 72 69 67 67 65 72 73 20 20 20 20 30 78 30  cTriggers    0x0
5cfa0 30 32 30 30 30 30 30 20 20 2f 2a 20 45 6e 61 62  0200000  /* Enab
5cfb0 6c 65 20 72 65 63 75 72 73 69 76 65 20 74 72 69  le recursive tri
5cfc0 67 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  ggers */.#define
5cfd0 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b   SQLITE_ForeignK
5cfe0 65 79 73 20 20 20 20 30 78 30 30 34 30 30 30 30  eys    0x0040000
5cff0 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65 20 66 6f  0  /* Enforce fo
5d000 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
5d010 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  aints  */../*.**
5d020 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   Possible values
5d030 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e   for the sqlite.
5d040 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20  magic field..** 
5d050 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20  The numbers are 
5d060 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64  obtained at rand
5d070 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73  om and have no s
5d080 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20  pecial meaning, 
5d090 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65  other.** than be
5d0a0 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f  ing distinct fro
5d0b0 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a  m one another..*
5d0c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5d0d0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20  _MAGIC_OPEN     
5d0e0 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44  0xa029a697  /* D
5d0f0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20  atabase is open 
5d100 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5d110 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20  E_MAGIC_CLOSED  
5d120 20 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20   0x9f3c2d33  /* 
5d130 44 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73  Database is clos
5d140 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
5d150 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20  LITE_MAGIC_SICK 
5d160 20 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20      0x4b771290  
5d170 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61  /* Error and awa
5d180 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23  iting close */.#
5d190 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
5d1a0 47 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66  GIC_BUSY     0xf
5d1b0 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61  03b7906  /* Data
5d1c0 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69  base currently i
5d1d0 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  n use */.#define
5d1e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
5d1f0 52 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33  ROR    0xb535793
5d200 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  0  /* An SQLITE_
5d210 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63  MISUSE error occ
5d220 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  urred */../*.** 
5d230 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f  Each SQL functio
5d240 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  n is defined by 
5d250 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
5d260 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
5d270 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f  structure.  A po
5d280 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
5d290 72 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65  ructure is store
5d2a0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e  d in the sqlite.
5d2b0 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61  aFunc.** hash ta
5d2c0 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69  ble.  When multi
5d2d0 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61  ple functions ha
5d2e0 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  ve the same name
5d2f0 2c 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  , the hash table
5d300 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  .** points to a 
5d310 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74  linked list of t
5d320 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e  hese structures.
5d330 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44  .*/.struct FuncD
5d340 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b  ef {.  i16 nArg;
5d350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5d360 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
5d370 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e  ts.  -1 means un
5d380 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20  limited */.  u8 
5d390 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20  iPrefEnc;       
5d3a0 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74    /* Preferred t
5d3b0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51  ext encoding (SQ
5d3c0 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c  LITE_UTF8, 16LE,
5d3d0 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66   16BE) */.  u8 f
5d3e0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
5d3f0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61   /* Some combina
5d400 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46  tion of SQLITE_F
5d410 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20  UNC_* */.  void 
5d420 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20  *pUserData;     
5d430 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72  /* User data par
5d440 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63  ameter */.  Func
5d450 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  Def *pNext;     
5d460 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f   /* Next functio
5d470 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65  n with same name
5d480 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75   */.  void (*xFu
5d490 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
5d4a0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
5d4b0 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65  _value**); /* Re
5d4c0 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a  gular function *
5d4d0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70  /.  void (*xStep
5d4e0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
5d4f0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
5d500 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72  alue**); /* Aggr
5d510 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20  egate step */.  
5d520 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65  void (*xFinalize
5d530 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
5d540 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  t*);            
5d550 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65      /* Aggregate
5d560 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20   finalizer */.  
5d570 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5d580 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65       /* SQL name
5d590 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
5d5a0 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  . */.  FuncDef *
5d5b0 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e  pHash;      /* N
5d5c0 65 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65  ext with a diffe
5d5d0 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68  rent name but th
5d5e0 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d  e same hash */.}
5d5f0 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c  ;../*.** Possibl
5d600 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e  e values for Fun
5d610 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64  cDef.flags.*/.#d
5d620 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
5d630 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20  C_LIKE     0x01 
5d640 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72  /* Candidate for
5d650 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69   the LIKE optimi
5d660 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  zation */.#defin
5d670 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41  e SQLITE_FUNC_CA
5d680 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43  SE     0x02 /* C
5d690 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49  ase-sensitive LI
5d6a0 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e  KE-type function
5d6b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5d6c0 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20  TE_FUNC_EPHEM   
5d6d0 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72   0x04 /* Ephemer
5d6e0 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68  al.  Delete with
5d6f0 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65   VDBE */.#define
5d700 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
5d710 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71  DCOLL 0x08 /* sq
5d720 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c  lite3GetFuncColl
5d730 53 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63  Seq() might be c
5d740 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  alled */.#define
5d750 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49   SQLITE_FUNC_PRI
5d760 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c  VATE  0x10 /* Al
5d770 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e  lowed for intern
5d780 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23  al use only */.#
5d790 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d7a0 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30  NC_COUNT    0x20
5d7b0 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75   /* Built-in cou
5d7c0 6e 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20  nt(*) aggregate 
5d7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5d7e0 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 20  E_FUNC_COALESCE 
5d7f0 30 78 34 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e  0x40 /* Built-in
5d800 20 63 6f 61 6c 65 73 63 65 28 29 20 6f 72 20 69   coalesce() or i
5d810 66 6e 75 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e  fnull() function
5d820 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66   */../*.** The f
5d830 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d  ollowing three m
5d840 61 63 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28  acros, FUNCTION(
5d850 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e  ), LIKEFUNC() an
5d860 64 20 41 47 47 52 45 47 41 54 45 28 29 20 61 72  d AGGREGATE() ar
5d870 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65  e.** used to cre
5d880 61 74 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69  ate the initiali
5d890 7a 65 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e  zers for the Fun
5d8a0 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73 2e  cDef structures.
5d8b0 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f  .**.**   FUNCTIO
5d8c0 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69  N(zName, nArg, i
5d8d0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29  Arg, bNC, xFunc)
5d8e0 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20  .**     Used to 
5d8f0 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 20  create a scalar 
5d900 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
5d910 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ion of a functio
5d920 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20  n zName .**     
5d930 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43  implemented by C
5d940 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20   function xFunc 
5d950 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 72  that accepts nAr
5d960 67 20 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65  g arguments. The
5d970 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61  .**     value pa
5d980 73 73 65 64 20 61 73 20 69 41 72 67 20 69 73 20  ssed as iArg is 
5d990 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a  cast to a (void*
5d9a0 29 20 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c  ) and made avail
5d9b0 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74  able.**     as t
5d9c0 68 65 20 75 73 65 72 2d 64 61 74 61 20 28 73 71  he user-data (sq
5d9d0 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
5d9e0 29 29 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74  )) for the funct
5d9f0 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20  ion. If .**     
5da00 61 72 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20  argument bNC is 
5da10 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 53  true, then the S
5da20 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
5da30 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73 65 74 2e  OLL flag is set.
5da40 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 45 47 41  .**.**   AGGREGA
5da50 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  TE(zName, nArg, 
5da60 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 74 65 70  iArg, bNC, xStep
5da70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20  , xFinal).**    
5da80 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   Used to create 
5da90 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  an aggregate fun
5daa0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
5dab0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a   implemented by.
5dac0 2a 2a 20 20 20 20 20 74 68 65 20 43 20 66 75 6e  **     the C fun
5dad0 63 74 69 6f 6e 73 20 78 53 74 65 70 20 61 6e 64  ctions xStep and
5dae0 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 66 69 72   xFinal. The fir
5daf0 73 74 20 66 6f 75 72 20 70 61 72 61 6d 65 74 65  st four paramete
5db00 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 20 69 6e  rs.**     are in
5db10 74 65 72 70 72 65 74 65 64 20 69 6e 20 74 68 65  terpreted in the
5db20 20 73 61 6d 65 20 77 61 79 20 61 73 20 74 68 65   same way as the
5db30 20 66 69 72 73 74 20 34 20 70 61 72 61 6d 65 74   first 4 paramet
5db40 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55  ers to.**     FU
5db50 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20  NCTION()..**.** 
5db60 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65    LIKEFUNC(zName
5db70 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 66 6c  , nArg, pArg, fl
5db80 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 73 65 64  ags).**     Used
5db90 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61   to create a sca
5dba0 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  lar function def
5dbb0 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e  inition of a fun
5dbc0 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20  ction zName .** 
5dbd0 20 20 20 20 74 68 61 74 20 61 63 63 65 70 74 73      that accepts
5dbe0 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 20   nArg arguments 
5dbf0 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  and is implement
5dc00 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
5dc10 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e 63 74 69  C .**     functi
5dc20 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67  on likeFunc. Arg
5dc30 75 6d 65 6e 74 20 70 41 72 67 20 69 73 20 63 61  ument pArg is ca
5dc40 73 74 20 74 6f 20 61 20 28 76 6f 69 64 20 2a 29  st to a (void *)
5dc50 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20   and made.**    
5dc60 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 74 68   available as th
5dc70 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d  e function user-
5dc80 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f 75 73  data (sqlite3_us
5dc90 65 72 5f 64 61 74 61 28 29 29 2e 20 54 68 65 0a  er_data()). The.
5dca0 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 66 2e 66  **     FuncDef.f
5dcb0 6c 61 67 73 20 76 61 72 69 61 62 6c 65 20 69 73  lags variable is
5dcc0 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75   set to the valu
5dcd0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
5dce0 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 70 61 72  flags.**     par
5dcf0 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  ameter..*/.#defi
5dd00 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d  ne FUNCTION(zNam
5dd10 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
5dd20 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b  NC, xFunc) \.  {
5dd30 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
5dd40 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55  8, bNC*SQLITE_FU
5dd50 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20  NC_NEEDCOLL, \. 
5dd60 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f    SQLITE_INT_TO_
5dd70 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 46  PTR(iArg), 0, xF
5dd80 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d  unc, 0, 0, #zNam
5dd90 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53 54  e, 0}.#define ST
5dda0 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65  R_FUNCTION(zName
5ddb0 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62 4e  , nArg, pArg, bN
5ddc0 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e  C, xFunc) \.  {n
5ddd0 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
5dde0 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55 4e  , bNC*SQLITE_FUN
5ddf0 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20  C_NEEDCOLL, \.  
5de00 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c   pArg, 0, xFunc,
5de10 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30   0, 0, #zName, 0
5de20 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b 45 46 55  }.#define LIKEFU
5de30 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  NC(zName, nArg, 
5de40 61 72 67 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20  arg, flags) \.  
5de50 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54  {nArg, SQLITE_UT
5de60 46 38 2c 20 66 6c 61 67 73 2c 20 28 76 6f 69 64  F8, flags, (void
5de70 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46   *)arg, 0, likeF
5de80 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d  unc, 0, 0, #zNam
5de90 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 41 47  e, 0}.#define AG
5dea0 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e  GREGATE(zName, n
5deb0 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53  Arg, arg, nc, xS
5dec0 74 65 70 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20  tep, xFinal) \. 
5ded0 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55   {nArg, SQLITE_U
5dee0 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54 45 5f 46  TF8, nc*SQLITE_F
5def0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a  UNC_NEEDCOLL, \.
5df00 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f     SQLITE_INT_TO
5df10 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30 2c  _PTR(arg), 0, 0,
5df20 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23 7a   xStep,xFinal,#z
5df30 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 41  Name,0}../*.** A
5df40 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65 70  ll current savep
5df50 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64  oints are stored
5df60 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   in a linked lis
5df70 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a 2a  t starting at.**
5df80 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f   sqlite3.pSavepo
5df90 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20 65  int. The first e
5dfa0 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c 69  lement in the li
5dfb0 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20 72  st is the most r
5dfc0 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e 65  ecently.** opene
5dfd0 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61 76  d savepoint. Sav
5dfe0 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64 65  epoints are adde
5dff0 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62 79  d to the list by
5e000 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50 5f   the vdbe.** OP_
5e010 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75  Savepoint instru
5e020 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ction..*/.struct
5e030 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 63   Savepoint {.  c
5e040 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
5e050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e060 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74 20     /* Savepoint 
5e070 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69 6e  name (nul-termin
5e080 61 74 65 64 29 20 2a 2f 0a 20 20 69 36 34 20 6e  ated) */.  i64 n
5e090 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20  DeferredCons;   
5e0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5e0b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 65  * Number of defe
5e0c0 72 72 65 64 20 66 6b 20 76 69 6f 6c 61 74 69 6f  rred fk violatio
5e0d0 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e  ns */.  Savepoin
5e0e0 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  t *pNext;       
5e0f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
5e100 61 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 20  arent savepoint 
5e110 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a  (if any) */.};..
5e120 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
5e130 69 6e 67 20 61 72 65 20 75 73 65 64 20 61 73 20  ing are used as 
5e140 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
5e150 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 53  eter to sqlite3S
5e160 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61  avepoint(),.** a
5e170 6e 64 20 61 73 20 74 68 65 20 50 31 20 61 72 67  nd as the P1 arg
5e180 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f  ument to the OP_
5e190 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75  Savepoint instru
5e1a0 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ction..*/.#defin
5e1b0 65 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49  e SAVEPOINT_BEGI
5e1c0 4e 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  N      0.#define
5e1d0 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
5e1e0 53 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  SE    1.#define 
5e1f0 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
5e200 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45  CK   2.../*.** E
5e210 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c  ach SQLite modul
5e220 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e (virtual table
5e230 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 20   definition) is 
5e240 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a  defined by an.**
5e250 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
5e260 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
5e270 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e 20  ture, stored in 
5e280 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64  the sqlite3.aMod
5e290 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c  ule.** hash tabl
5e2a0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64  e..*/.struct Mod
5e2b0 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ule {.  const sq
5e2c0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
5e2d0 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  odule;       /* 
5e2e0 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72  Callback pointer
5e2f0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
5e300 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
5e310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5e320 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 72  ame passed to cr
5e330 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f  eate_module() */
5e340 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20  .  void *pAux;  
5e350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e360 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 20          /* pAux 
5e370 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 65  passed to create
5e380 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76  _module() */.  v
5e390 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
5e3a0 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 20  void *);        
5e3b0 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65      /* Module de
5e3c0 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
5e3d0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69  n */.};../*.** i
5e3e0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
5e3f0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   each column of 
5e400 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20  an SQL table is 
5e410 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 61  held in an insta
5e420 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73  nce.** of this s
5e430 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
5e440 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63  uct Column {.  c
5e450 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
5e460 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
5e470 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72  column */.  Expr
5e480 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20   *pDflt;     /* 
5e490 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  Default value of
5e4a0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
5e4b0 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20    char *zDflt;  
5e4c0 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74     /* Original t
5e4d0 65 78 74 20 6f 66 20 74 68 65 20 64 65 66 61 75  ext of the defau
5e4e0 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68  lt value */.  ch
5e4f0 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f  ar *zType;     /
5e500 2a 20 44 61 74 61 20 74 79 70 65 20 66 6f 72 20  * Data type for 
5e510 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  this column */. 
5e520 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20   char *zColl;   
5e530 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
5e540 65 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c  equence.  If NUL
5e550 4c 2c 20 75 73 65 20 74 68 65 20 64 65 66 61 75  L, use the defau
5e560 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75  lt */.  u8 notNu
5e570 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ll;      /* True
5e580 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 4e   if there is a N
5e590 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
5e5a0 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 69  nt */.  u8 isPri
5e5b0 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75 65  mKey;    /* True
5e5c0 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   if this column 
5e5d0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 50  is part of the P
5e5e0 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20  RIMARY KEY */.  
5e5f0 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20  char affinity;  
5e600 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53   /* One of the S
5e610 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61  QLITE_AFF_... va
5e620 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20  lues */.#ifndef 
5e630 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
5e640 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 73  UALTABLE.  u8 is
5e650 48 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54  Hidden;     /* T
5e660 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75  rue if this colu
5e670 6d 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20 2a  mn is 'hidden' *
5e680 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
5e690 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20  ** A "Collating 
5e6a0 53 65 71 75 65 6e 63 65 22 20 69 73 20 64 65 66  Sequence" is def
5e6b0 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61  ined by an insta
5e6c0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
5e6d0 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
5e6e0 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c  e. Conceptually,
5e6f0 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71   a collating seq
5e700 75 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 6f  uence consists o
5e710 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20  f a name and.** 
5e720 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75  a comparison rou
5e730 74 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e 65  tine that define
5e740 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74  s the order of t
5e750 68 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a  hat sequence..**
5e760 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74 77  .** There may tw
5e770 6f 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c 65  o separate imple
5e780 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
5e790 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63  e collation func
5e7a0 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61  tion, one.** tha
5e7b0 74 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74  t processes text
5e7c0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69   in UTF-8 encodi
5e7d0 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  ng (CollSeq.xCmp
5e7e0 29 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74 68  ) and another th
5e7f0 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73 20  at.** processes 
5e800 74 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e 20  text encoded in 
5e810 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e  UTF-16 (CollSeq.
5e820 78 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20 74  xCmp16), using t
5e830 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61  he machine.** na
5e840 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
5e850 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f   When a collatio
5e860 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e  n sequence is in
5e870 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73 65  voked, SQLite se
5e880 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65 72  lects.** the ver
5e890 73 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 72  sion that will r
5e8a0 65 71 75 69 72 65 20 74 68 65 20 6c 65 61 73 74  equire the least
5e8b0 20 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f 64   expensive encod
5e8c0 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69  ing.** translati
5e8d0 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a  ons, if any..**.
5e8e0 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70  ** The CollSeq.p
5e8f0 55 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72 69  User member vari
5e900 61 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72 61  able is an extra
5e910 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 20   parameter that 
5e920 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20  passed in.** as 
5e930 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
5e940 6e 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38 20  nt to the UTF-8 
5e950 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
5e960 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f  ion, xCmp..** Co
5e970 6c 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 73  llSeq.pUser16 is
5e980 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   the equivalent 
5e990 66 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 63  for the UTF-16 c
5e9a0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
5e9b0 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a  on,.** xCmp16..*
5e9c0 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c  *.** If both Col
5e9d0 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f  lSeq.xCmp and Co
5e9e0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72 65  llSeq.xCmp16 are
5e9f0 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20   NULL, it means 
5ea00 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  that the.** coll
5ea10 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  ating sequence i
5ea20 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e  s undefined.  In
5ea30 64 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20 61  dices built on a
5ea40 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63  n undefined.** c
5ea50 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5ea60 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 61  e may not be rea
5ea70 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f  d or written..*/
5ea80 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20  .struct CollSeq 
5ea90 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
5eaa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
5eab0 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  e of the collati
5eac0 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54 46  ng sequence, UTF
5ead0 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  -8 encoded */.  
5eae0 75 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20  u8 enc;         
5eaf0 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e        /* Text en
5eb00 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20 62  coding handled b
5eb10 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38  y xCmp() */.  u8
5eb20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20   type;          
5eb30 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
5eb40 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e  e SQLITE_COLL_..
5eb50 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a  . values below *
5eb60 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b  /.  void *pUser;
5eb70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
5eb80 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  st argument to x
5eb90 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28  Cmp() */.  int (
5eba0 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74  *xCmp)(void*,int
5ebb0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
5ebc0 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29  nt, const void*)
5ebd0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  ;.  void (*xDel)
5ebe0 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73  (void*);  /* Des
5ebf0 74 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65  tructor for pUse
5ec00 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  r */.};../*.** A
5ec10 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66  llowed values of
5ec20 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a   CollSeq.type:.*
5ec30 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5ec40 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20  _COLL_BINARY  1 
5ec50 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   /* The default 
5ec60 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69  memcmp() collati
5ec70 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23  ng sequence */.#
5ec80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
5ec90 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a  LL_NOCASE  2  /*
5eca0 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f   The built-in NO
5ecb0 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73  CASE collating s
5ecc0 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69  equence */.#defi
5ecd0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52  ne SQLITE_COLL_R
5ece0 45 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68 65  EVERSE 3  /* The
5ecf0 20 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52 53   built-in REVERS
5ed00 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  E collating sequ
5ed10 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ence */.#define 
5ed20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52  SQLITE_COLL_USER
5ed30 20 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74      0  /* Any ot
5ed40 68 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65 64  her user-defined
5ed50 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5ed60 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20  nce */../*.** A 
5ed70 73 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62  sort order can b
5ed80 65 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20  e either ASC or 
5ed90 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  DESC..*/.#define
5eda0 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20   SQLITE_SO_ASC  
5edb0 20 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20       0  /* Sort 
5edc0 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
5edd0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  er */.#define SQ
5ede0 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20  LITE_SO_DESC    
5edf0 20 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20    1  /* Sort in 
5ee00 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20  ascending order 
5ee10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e  */../*.** Column
5ee20 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e   affinity types.
5ee30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65  .**.** These use
5ee40 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e  d to have mnemon
5ee50 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27  ic name like 'i'
5ee60 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f   for SQLITE_AFF_
5ee70 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27  INTEGER and.** '
5ee80 74 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46  t' for SQLITE_AF
5ee90 46 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20  F_TEXT.  But we 
5eea0 63 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c  can save a littl
5eeb0 65 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72  e space and impr
5eec0 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64  ove.** the speed
5eed0 20 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d   a little by num
5eee0 62 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65  bering the value
5eef0 73 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e  s consecutively.
5ef00 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74    .**.** But rat
5ef10 68 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77  her than start w
5ef20 69 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62  ith 0 or 1, we b
5ef30 65 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20  egin with 'a'.  
5ef40 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65  That way,.** whe
5ef50 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e  n multiple affin
5ef60 69 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f  ity types are co
5ef70 6e 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20  ncatenated into 
5ef80 61 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20  a string and.** 
5ef90 75 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f  used as the P4 o
5efa0 70 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c  perand, they wil
5efb0 6c 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62  l be more readab
5efc0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61  le..**.** Note a
5efd0 6c 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d  lso that the num
5efe0 65 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67  eric types are g
5eff0 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20  rouped together 
5f000 73 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a  so that testing.
5f010 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63  ** for a numeric
5f020 20 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c   type is a singl
5f030 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f  e comparison..*/
5f040 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f050 41 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27  AFF_TEXT     'a'
5f060 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f070 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27  AFF_NONE     'b'
5f080 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f090 41 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27  AFF_NUMERIC  'c'
5f0a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f0b0 41 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27  AFF_INTEGER  'd'
5f0c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f0d0 41 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27  AFF_REAL     'e'
5f0e0 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ..#define sqlite
5f0f0 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  3IsNumericAffini
5f100 74 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c  ty(X)  ((X)>=SQL
5f110 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29  ITE_AFF_NUMERIC)
5f120 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
5f130 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75  TE_AFF_MASK valu
5f140 65 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65  es masks off the
5f150 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74   significant bit
5f160 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e  s of an.** affin
5f170 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23  ity value. .*/.#
5f180 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46  define SQLITE_AF
5f190 46 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a  F_MASK     0x67.
5f1a0 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61  ./*.** Additiona
5f1b0 6c 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61  l bit values tha
5f1c0 74 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69  t can be ORed wi
5f1d0 74 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77  th an affinity w
5f1e0 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69  ithout.** changi
5f1f0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e  ng the affinity.
5f200 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5f210 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20  TE_JUMPIFNULL   
5f220 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69  0x08  /* jumps i
5f230 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
5f240 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   is NULL */.#def
5f250 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  ine SQLITE_STORE
5f260 50 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a  P2      0x10  /*
5f270 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e   Store result in
5f280 20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20   reg[P2] rather 
5f290 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64 65  than jump */.#de
5f2a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  fine SQLITE_NULL
5f2b0 45 51 20 20 20 20 20 20 20 30 78 38 30 20 20 2f  EQ       0x80  /
5f2c0 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a 0a  * NULL=NULL */..
5f2d0 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20  /*.** An object 
5f2e0 6f 66 20 74 68 69 73 20 74 79 70 65 20 69 73 20  of this type is 
5f2f0 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68  created for each
5f300 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
5f310 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65  resent in.** the
5f320 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
5f330 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
5f340 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
5f350 69 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e 20  is shared, then 
5f360 74 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e 73  there is one ins
5f370 74 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a  tance of this.**
5f380 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65   structure for e
5f390 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ach database con
5f3a0 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 33  nection (sqlite3
5f3b0 2a 29 20 74 68 61 74 20 75 73 65 73 20 74 68 65  *) that uses the
5f3c0 20 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d   shared.** schem
5f3d0 61 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  a. This is becau
5f3e0 73 65 20 65 61 63 68 20 64 61 74 61 62 61 73 65  se each database
5f3f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75   connection requ
5f400 69 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e 69  ires its own uni
5f410 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  que.** instance 
5f420 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  of the sqlite3_v
5f430 74 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65 64  tab* handle used
5f440 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76   to access the v
5f450 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a  irtual table .**
5f460 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
5f470 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68   sqlite3_vtab* h
5f480 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62  andles can not b
5f490 65 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e  e shared between
5f4a0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f   .** database co
5f4b0 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20  nnections, even 
5f4c0 77 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f 66  when the rest of
5f4d0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64   the in-memory d
5f4e0 61 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68 65  atabase .** sche
5f4f0 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 61 73  ma is shared, as
5f500 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
5f510 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65 73  ion often stores
5f520 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
5f530 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64   connection hand
5f540 6c 65 20 70 61 73 73 65 64 20 74 6f 20 69 74 20  le passed to it 
5f550 76 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 74  via the xConnect
5f560 28 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 20  () or xCreate() 
5f570 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67  method.** during
5f580 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
5f590 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73  internally. This
5f5a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
5f5b0 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a  tion handle may.
5f5c0 2a 2a 20 74 68 65 6e 20 75 73 65 64 20 62 79 20  ** then used by 
5f5d0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
5f5e0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
5f5f0 20 74 6f 20 61 63 63 65 73 73 20 72 65 61 6c 20   to access real 
5f600 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74 68 69  tables .** withi
5f610 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
5f620 53 6f 20 74 68 61 74 20 74 68 65 79 20 61 70 70  So that they app
5f630 65 61 72 20 61 73 20 70 61 72 74 20 6f 66 20 74  ear as part of t
5f640 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a 20 74  he callers .** t
5f650 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 73  ransaction, thes
5f660 65 20 61 63 63 65 73 73 65 73 20 6e 65 65 64 20  e accesses need 
5f670 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61 20 74  to be made via t
5f680 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
5f690 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   .** connection 
5f6a0 61 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20  as that used to 
5f6b0 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70 65 72  execute SQL oper
5f6c0 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 76 69  ations on the vi
5f6d0 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
5f6e0 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20 6f 62  ** All VTable ob
5f6f0 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72 72 65  jects that corre
5f700 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e 67 6c  spond to a singl
5f710 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73 68 61  e table in a sha
5f720 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  red.** database 
5f730 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69 74 69  schema are initi
5f740 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61  ally stored in a
5f750 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69   linked-list poi
5f760 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 74 68  nted to by.** th
5f770 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65 20  e Table.pVTable 
5f780 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20  member variable 
5f790 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
5f7a0 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a 65 63  ding Table objec
5f7b0 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 73 71  t..** When an sq
5f7c0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20  lite3_prepare() 
5f7d0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
5f7e0 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20  uired to access 
5f7f0 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  the virtual.** t
5f800 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63 68 65  able, it searche
5f810 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 74  s the list for t
5f820 68 65 20 56 54 61 62 6c 65 20 74 68 61 74 20 63  he VTable that c
5f830 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
5f840 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
5f850 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67 20 74  nnection doing t
5f860 68 65 20 70 72 65 70 61 72 69 6e 67 20 73 6f 20  he preparing so 
5f870 61 73 20 74 6f 20 75 73 65 20 74 68 65 20 63 6f  as to use the co
5f880 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74 65 33  rrect.** sqlite3
5f890 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 69 6e  _vtab* handle in
5f8a0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75   the compiled qu
5f8b0 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ery..**.** When 
5f8c0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 62  an in-memory Tab
5f8d0 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 65 6c  le object is del
5f8e0 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eted (for exampl
5f8f0 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 73 63  e when the.** sc
5f900 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 72 65  hema is being re
5f910 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d 65 20  loaded for some 
5f920 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 54 61  reason), the VTa
5f930 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20  ble objects are 
5f940 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65 64 20  not .** deleted 
5f950 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f  and the sqlite3_
5f960 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 61 72  vtab* handles ar
5f970 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e 65 63  e not xDisconnec
5f980 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65 64 69  t()ed .** immedi
5f990 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64 2c 20  ately. Instead, 
5f9a0 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64 20 66  they are moved f
5f9b0 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e 70 56  rom the Table.pV
5f9c0 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a 2a 2a  Table list to.**
5f9d0 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65 64 20   another linked 
5f9e0 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 74  list headed by t
5f9f0 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63  he sqlite3.pDisc
5fa00 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20 6f 66  onnect member of
5fa10 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f   the.** correspo
5fa20 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20 73 74  nding sqlite3 st
5fa30 72 75 63 74 75 72 65 2e 20 54 68 65 79 20 61 72  ructure. They ar
5fa40 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64 2f 78  e then deleted/x
5fa50 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a 2a 2a  Disconnected .**
5fa60 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 74 61   next time a sta
5fa70 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70 61 72  tement is prepar
5fa80 65 64 20 75 73 69 6e 67 20 73 61 69 64 20 73 71  ed using said sq
5fa90 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69 73 20  lite3*. This is 
5faa0 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64  done.** to avoid
5fab0 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75 65 73   deadlock issues
5fac0 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 69   involving multi
5fad0 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75 74 65  ple sqlite3.mute
5fae0 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20 52 65  x mutexes..** Re
5faf0 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20  fer to comments 
5fb00 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 73  above function s
5fb10 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
5fb20 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a 2a 2a  List() for an.**
5fb30 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61 73 20   explanation as 
5fb40 74 6f 20 77 68 79 20 69 74 20 69 73 20 73 61 66  to why it is saf
5fb50 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e 74 72  e to add an entr
5fb60 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 2e  y to an sqlite3.
5fb70 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 6c  pDisconnect.** l
5fb80 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f 6c 64  ist without hold
5fb90 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ing the correspo
5fba0 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75  nding sqlite3.mu
5fbb0 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a  tex mutex..**.**
5fbc0 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20   The memory for 
5fbd0 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20  objects of this 
5fbe0 74 79 70 65 20 69 73 20 61 6c 77 61 79 73 20 61  type is always a
5fbf0 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20  llocated by .** 
5fc00 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
5fc10 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6e  ), using the con
5fc20 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 73  nection handle s
5fc30 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c 65 2e  tored in VTable.
5fc40 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 69  db as .** the fi
5fc50 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  rst argument..*/
5fc60 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 7b  .struct VTable {
5fc70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
5fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5fc90 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
5fca0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
5fcb0 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ith this table *
5fcc0 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  /.  Module *pMod
5fcd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
5fce0 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f 64 75   Pointer to modu
5fcf0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
5fd00 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  n */.  sqlite3_v
5fd10 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20  tab *pVtab;     
5fd20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
5fd30 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0a  tab instance */.
5fd40 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
5fd50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5fd60 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72  umber of pointer
5fd70 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  s to this struct
5fd80 75 72 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20  ure */.  VTable 
5fd90 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
5fda0 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69     /* Next in li
5fdb0 6e 6b 65 64 20 6c 69 73 74 20 28 73 65 65 20 61  nked list (see a
5fdc0 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  bove) */.};../*.
5fdd0 2a 2a 20 45 61 63 68 20 53 51 4c 20 74 61 62 6c  ** Each SQL tabl
5fde0 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  e is represented
5fdf0 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e   in memory by an
5fe00 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
5fe10 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
5fe20 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
5fe30 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68  able.zName is th
5fe40 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
5fe50 62 6c 65 2e 20 20 54 68 65 20 63 61 73 65 20 6f  ble.  The case o
5fe60 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a  f the original.*
5fe70 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  * CREATE TABLE s
5fe80 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74 6f 72  tatement is stor
5fe90 65 64 2c 20 62 75 74 20 63 61 73 65 20 69 73 20  ed, but case is 
5fea0 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  not significant 
5feb0 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f  for.** compariso
5fec0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e  ns..**.** Table.
5fed0 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62  nCol is the numb
5fee0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
5fef0 20 74 68 69 73 20 74 61 62 6c 65 2e 20 20 54 61   this table.  Ta
5ff00 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a  ble.aCol is a.**
5ff10 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61   pointer to an a
5ff20 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73  rray of Column s
5ff30 74 72 75 63 74 75 72 65 73 2c 20 6f 6e 65 20 66  tructures, one f
5ff40 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a  or each column..
5ff50 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62  **.** If the tab
5ff60 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45  le has an INTEGE
5ff70 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74  R PRIMARY KEY, t
5ff80 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b 65 79 20  hen Table.iPKey 
5ff90 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a  is the index of.
5ffa0 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68  ** the column th
5ffb0 61 74 20 69 73 20 74 68 61 74 20 6b 65 79 2e 20  at is that key. 
5ffc0 20 20 4f 74 68 65 72 77 69 73 65 20 54 61 62 6c    Otherwise Tabl
5ffd0 65 2e 69 50 4b 65 79 20 69 73 20 6e 65 67 61 74  e.iPKey is negat
5ffe0 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68  ive.  Note.** th
5fff0 61 74 20 74 68 65 20 64 61 74 61 74 79 70 65 20  at the datatype 
60000 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  of the PRIMARY K
60010 45 59 20 6d 75 73 74 20 62 65 20 49 4e 54 45 47  EY must be INTEG
60020 45 52 20 66 6f 72 20 74 68 69 73 20 66 69 65 6c  ER for this fiel
60030 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20  d to.** be set. 
60040 20 41 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   An INTEGER PRIM
60050 41 52 59 20 4b 45 59 20 69 73 20 75 73 65 64 20  ARY KEY is used 
60060 61 73 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72  as the rowid for
60070 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20   each row of.** 
60080 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 61  the table.  If a
60090 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e   table has no IN
600a0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
600b0 59 2c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d  Y, then a random
600c0 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e   rowid.** is gen
600d0 65 72 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  erated for each 
600e0 72 6f 77 20 6f 66 20 74 68 65 20 74 61 62 6c 65  row of the table
600f0 2e 20 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79  .  TF_HasPrimary
60100 4b 65 79 20 69 73 20 73 65 74 20 69 66 0a 2a 2a  Key is set if.**
60110 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
60120 6e 79 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ny PRIMARY KEY, 
60130 49 4e 54 45 47 45 52 20 6f 72 20 6f 74 68 65 72  INTEGER or other
60140 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c  wise..**.** Tabl
60150 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 20 70 61  e.tnum is the pa
60160 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
60170 65 20 72 6f 6f 74 20 42 54 72 65 65 20 70 61 67  e root BTree pag
60180 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
60190 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  n the.** databas
601a0 65 20 66 69 6c 65 2e 20 20 49 66 20 54 61 62 6c  e file.  If Tabl
601b0 65 2e 69 44 62 20 69 73 20 74 68 65 20 69 6e 64  e.iDb is the ind
601c0 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
601d0 73 65 20 74 61 62 6c 65 20 62 61 63 6b 65 6e 64  se table backend
601e0 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 2e 61 44  .** in sqlite.aD
601f0 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f 72 20 74  b[].  0 is for t
60200 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
60210 20 61 6e 64 20 31 20 69 73 20 66 6f 72 20 74 68   and 1 is for th
60220 65 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68  e file that.** h
60230 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72 79 20 74  olds temporary t
60240 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
60250 73 2e 20 20 49 66 20 54 46 5f 45 70 68 65 6d 65  s.  If TF_Epheme
60260 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a 20 74 68  ral is set.** th
60270 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  en the table is 
60280 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c 65  stored in a file
60290 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74   that is automat
602a0 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a  ically deleted.*
602b0 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45 20  * when the VDBE 
602c0 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74 61  cursor to the ta
602d0 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  ble is closed.  
602e0 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61 62  In this case Tab
602f0 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65  le.tnum .** refe
60300 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  rs VDBE cursor n
60310 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64 73  umber that holds
60320 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c   the table open,
60330 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74   not to the root
60340 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  .** page number.
60350 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62 6c    Transient tabl
60360 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 68  es are used to h
60370 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73 20  old the results 
60380 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72  of a.** sub-quer
60390 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69  y that appears i
603a0 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61 6c  nstead of a real
603b0 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74   table name in t
603c0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a  he FROM clause .
603d0 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  ** of a SELECT s
603e0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72  tatement..*/.str
603f0 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 73 71  uct Table {.  sq
60400 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20  lite3 *dbMem;   
60410 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74     /* DB connect
60420 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f  ion used for loo
60430 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f  kaside allocatio
60440 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ns. */.  char *z
60450 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Name;         /*
60460 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
60470 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20  le or view */.  
60480 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20 20 20  int iPKey;      
60490 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e       /* If not n
604a0 65 67 61 74 69 76 65 2c 20 75 73 65 20 61 43 6f  egative, use aCo
604b0 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68 65 20  l[iPKey] as the 
604c0 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20  primary key */. 
604d0 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
604e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
604f0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
60500 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f  is table */.  Co
60510 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20  lumn *aCol;     
60520 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
60530 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c  n about each col
60540 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  umn */.  Index *
60550 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 2f 2a  pIndex;       /*
60560 20 4c 69 73 74 20 6f 66 20 53 51 4c 20 69 6e 64   List of SQL ind
60570 65 78 65 73 20 6f 6e 20 74 68 69 73 20 74 61 62  exes on this tab
60580 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75  le. */.  int tnu
60590 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  m;            /*
605a0 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f 64 65   Root BTree node
605b0 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20   for this table 
605c0 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 65 29  (see note above)
605d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
605e0 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55  elect;     /* NU
605f0 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20  LL for tables.  
60600 50 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e 69  Points to defini
60610 74 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e 20  tion if a view. 
60620 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20 20  */.  u16 nRef;  
60630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
60640 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ber of pointers 
60650 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a 2f  to this Table */
60660 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b 20  .  u8 tabFlags; 
60670 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
60680 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a  of TF_* values *
60690 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20  /.  u8 keyConf; 
606a0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74           /* What
606b0 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f   to do in case o
606c0 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e  f uniqueness con
606d0 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a  flict on iPKey *
606e0 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b  /.  FKey *pFKey;
606f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b           /* Link
60700 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66  ed list of all f
60710 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 74  oreign keys in t
60720 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  his table */.  c
60730 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20  har *zColAff;   
60740 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65      /* String de
60750 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e  fining the affin
60760 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75  ity of each colu
60770 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  mn */.#ifndef SQ
60780 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
60790 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 3b 20    Expr *pCheck; 
607a0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 41 4e         /* The AN
607b0 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b 20 63  D of all CHECK c
607c0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65  onstraints */.#e
607d0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
607e0 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
607f0 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c  BLE.  int addCol
60800 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66  Offset;    /* Of
60810 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20 54  fset in CREATE T
60820 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 61 64 64  ABLE stmt to add
60830 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f   a new column */
60840 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
60850 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
60860 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62 6c  UALTABLE.  VTabl
60870 65 20 2a 70 56 54 61 62 6c 65 3b 20 20 20 20 20  e *pVTable;     
60880 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54 61 62 6c  /* List of VTabl
60890 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f 0a 20 20  e objects. */.  
608a0 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20  int nModuleArg; 
608b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
608c0 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
608d0 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  he module */.  c
608e0 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72  har **azModuleAr
608f0 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 61  g;  /* Text of a
60900 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20  ll module args. 
60910 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61  [0] is module na
60920 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54  me */.#endif.  T
60930 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
60940 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  ;   /* List of t
60950 72 69 67 67 65 72 73 20 73 74 6f 72 65 64 20 69  riggers stored i
60960 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53  n pSchema */.  S
60970 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
60980 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 74 68      /* Schema th
60990 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73  at contains this
609a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c   table */.  Tabl
609b0 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20  e *pNextZombie; 
609c0 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65 20   /* Next on the 
609d0 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62  Parse.pZombieTab
609e0 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   list */.};../*.
609f0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
60a00 73 20 66 6f 72 20 54 61 62 65 2e 74 61 62 46 6c  s for Tabe.tabFl
60a10 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ags..*/.#define 
60a20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 20  TF_Readonly     
60a30 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 52 65     0x01    /* Re
60a40 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74  ad-only system t
60a50 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
60a60 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 20 20 20  TF_Ephemeral    
60a70 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 41 6e     0x02    /* An
60a80 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
60a90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 48   */.#define TF_H
60aa0 61 73 50 72 69 6d 61 72 79 4b 65 79 20 20 20 30  asPrimaryKey   0
60ab0 78 30 34 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  x04    /* Table 
60ac0 68 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65  has a primary ke
60ad0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  y */.#define TF_
60ae0 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20  Autoincrement   
60af0 30 78 30 38 20 20 20 20 2f 2a 20 49 6e 74 65 67  0x08    /* Integ
60b00 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  er primary key i
60b10 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  s autoincrement 
60b20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 56 69  */.#define TF_Vi
60b30 72 74 75 61 6c 20 20 20 20 20 20 20 20 20 30 78  rtual         0x
60b40 31 30 20 20 20 20 2f 2a 20 49 73 20 61 20 76 69  10    /* Is a vi
60b50 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23  rtual table */.#
60b60 64 65 66 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65  define TF_NeedMe
60b70 74 61 64 61 74 61 20 20 20 20 30 78 32 30 20 20  tadata    0x20  
60b80 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70    /* aCol[].zTyp
60b90 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f  e and aCol[].pCo
60ba0 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a  ll missing */...
60bb0 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73  ./*.** Test to s
60bc0 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
60bd0 74 20 61 20 74 61 62 6c 65 20 69 73 20 61 20 76  t a table is a v
60be0 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
60bf0 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61  his is.** done a
60c00 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61  s a macro so tha
60c10 74 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 74  t it will be opt
60c20 69 6d 69 7a 65 64 20 6f 75 74 20 77 68 65 6e 20  imized out when 
60c30 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
60c40 20 73 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 74   support is omit
60c50 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69  ted from the bui
60c60 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ld..*/.#ifndef S
60c70 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
60c80 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e  ALTABLE.#  defin
60c90 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 20  e IsVirtual(X)  
60ca0 20 20 20 20 28 28 28 58 29 2d 3e 74 61 62 46 6c      (((X)->tabFl
60cb0 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
60cc0 29 21 3d 30 29 0a 23 20 20 64 65 66 69 6e 65 20  )!=0).#  define 
60cd0 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58  IsHiddenColumn(X
60ce0 29 20 28 28 58 29 2d 3e 69 73 48 69 64 64 65 6e  ) ((X)->isHidden
60cf0 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  ).#else.#  defin
60d00 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 20  e IsVirtual(X)  
60d10 20 20 20 20 30 0a 23 20 20 64 65 66 69 6e 65 20      0.#  define 
60d20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58  IsHiddenColumn(X
60d30 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ) 0.#endif../*.*
60d40 2a 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20 6b  * Each foreign k
60d50 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  ey constraint is
60d60 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
60d70 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
60d80 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41  ructure..**.** A
60d90 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
60da0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
60db0 74 77 6f 20 74 61 62 6c 65 73 2e 20 20 54 68 65  two tables.  The
60dc0 20 22 66 72 6f 6d 22 20 74 61 62 6c 65 20 69 73   "from" table is
60dd0 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 68  .** the table th
60de0 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
60df0 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
60e00 65 20 74 68 61 74 20 63 72 65 61 74 65 73 20 74  e that creates t
60e10 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65  he foreign.** ke
60e20 79 2e 20 20 54 68 65 20 22 74 6f 22 20 74 61 62  y.  The "to" tab
60e30 6c 65 20 69 73 20 74 68 65 20 74 61 62 6c 65 20  le is the table 
60e40 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e  that is named in
60e50 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20   the REFERENCES 
60e60 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69  clause..** Consi
60e70 64 65 72 20 74 68 69 73 20 65 78 61 6d 70 6c 65  der this example
60e80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41  :.**.**     CREA
60e90 54 45 20 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a  TE TABLE ex1(.**
60ea0 20 20 20 20 20 20 20 61 20 49 4e 54 45 47 45 52         a INTEGER
60eb0 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a   PRIMARY KEY,.**
60ec0 20 20 20 20 20 20 20 62 20 49 4e 54 45 47 45 52         b INTEGER
60ed0 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 31 20   CONSTRAINT fk1 
60ee0 52 45 46 45 52 45 4e 43 45 53 20 65 78 32 28 78  REFERENCES ex2(x
60ef0 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a  ).**     );.**.*
60f00 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65  * For foreign ke
60f10 79 20 22 66 6b 31 22 2c 20 74 68 65 20 66 72 6f  y "fk1", the fro
60f20 6d 2d 74 61 62 6c 65 20 69 73 20 22 65 78 31 22  m-table is "ex1"
60f30 20 61 6e 64 20 74 68 65 20 74 6f 2d 74 61 62 6c   and the to-tabl
60f40 65 20 69 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a  e is "ex2"..**.*
60f50 2a 20 45 61 63 68 20 52 45 46 45 52 45 4e 43 45  * Each REFERENCE
60f60 53 20 63 6c 61 75 73 65 20 67 65 6e 65 72 61 74  S clause generat
60f70 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  es an instance o
60f80 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
60f90 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 68 69  structure.** whi
60fa0 63 68 20 69 73 20 61 74 74 61 63 68 65 64 20 74  ch is attached t
60fb0 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65  o the from-table
60fc0 2e 20 20 54 68 65 20 74 6f 2d 74 61 62 6c 65 20  .  The to-table 
60fd0 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 20 77  need not exist w
60fe0 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d  hen.** the from-
60ff0 74 61 62 6c 65 20 69 73 20 63 72 65 61 74 65 64  table is created
61000 2e 20 20 54 68 65 20 65 78 69 73 74 65 6e 63 65  .  The existence
61010 20 6f 66 20 74 68 65 20 74 6f 2d 74 61 62 6c 65   of the to-table
61020 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e   is not checked.
61030 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b 65 79 20  .*/.struct FKey 
61040 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d  {.  Table *pFrom
61050 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63  ;     /* Table c
61060 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45  ontaining the RE
61070 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20  FERENCES clause 
61080 28 61 6b 61 3a 20 43 68 69 6c 64 29 20 2a 2f 0a  (aka: Child) */.
61090 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 6f    FKey *pNextFro
610a0 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65  m;  /* Next fore
610b0 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f 6d  ign key in pFrom
610c0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 3b   */.  char *zTo;
610d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
610e0 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  of table that th
610f0 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20  e key points to 
61100 28 61 6b 61 3a 20 50 61 72 65 6e 74 29 20 2a 2f  (aka: Parent) */
61110 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f  .  FKey *pNextTo
61120 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72  ;    /* Next for
61130 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 62 6c  eign key on tabl
61140 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f 0a 20  e named zTo */. 
61150 20 46 4b 65 79 20 2a 70 50 72 65 76 54 6f 3b 20   FKey *pPrevTo; 
61160 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 66     /* Previous f
61170 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61  oreign key on ta
61180 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f  ble named zTo */
61190 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20  .  int nCol;    
611a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
611b0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69  f columns in thi
611c0 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a 20 45 56  s key */.  /* EV
611d0 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
611e0 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72  */.  u8 isDeferr
611f0 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ed;    /* True i
61200 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  f constraint che
61210 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65  cking is deferre
61220 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f  d till COMMIT */
61230 0a 20 20 75 38 20 61 41 63 74 69 6f 6e 5b 32 5d  .  u8 aAction[2]
61240 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e  ;          /* ON
61250 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55   DELETE and ON U
61260 50 44 41 54 45 20 61 63 74 69 6f 6e 73 2c 20 72  PDATE actions, r
61270 65 73 70 65 63 74 69 76 65 6c 79 20 2a 2f 0a 20  espectively */. 
61280 20 54 72 69 67 67 65 72 20 2a 61 70 54 72 69 67   Trigger *apTrig
61290 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54 72 69 67  ger[2];  /* Trig
612a0 67 65 72 73 20 66 6f 72 20 61 41 63 74 69 6f 6e  gers for aAction
612b0 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  [] actions */.  
612c0 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b  struct sColMap {
612d0 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20    /* Mapping of 
612e0 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d  columns in pFrom
612f0 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a   to columns in z
61300 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46  To */.    int iF
61310 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  rom;         /* 
61320 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
61330 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20  in pFrom */.    
61340 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20  char *zCol;     
61350 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f     /* Name of co
61360 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66  lumn in zTo.  If
61370 20 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b   0 use PRIMARY K
61380 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 5b 31  EY */.  } aCol[1
61390 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65  ];        /* One
613a0 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20   entry for each 
613b0 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73  of nCol column s
613c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 51   */.};../*.** SQ
613d0 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 6d 61  Lite supports ma
613e0 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  ny different way
613f0 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 63  s to resolve a c
61400 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
61410 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 70 72  or.  ROLLBACK pr
61420 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74  ocessing means t
61430 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 6e 74  hat a constraint
61440 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63 61   violation.** ca
61450 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 74 69  uses the operati
61460 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 74 6f  on in process to
61470 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 74 68   fail and for th
61480 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
61490 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72  ction.** to be r
614a0 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 42 4f  olled back.  ABO
614b0 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65  RT processing me
614c0 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ans the operatio
614d0 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a 2a 20  n in process.** 
614e0 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 70 72  fails and any pr
614f0 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 6f 6d  ior changes from
61500 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 61 74   that one operat
61510 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 20 6f  ion are backed o
61520 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74  ut,.** but the t
61530 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f  ransaction is no
61540 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20  t rolled back.  
61550 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e 67 20  FAIL processing 
61560 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68  means that.** th
61570 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70  e operation in p
61580 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 61 6e  rogress stops an
61590 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  d returns an err
615a0 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 70 72  or code.  But pr
615b0 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 20 64  ior.** changes d
615c0 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6f  ue to the same o
615d0 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e 6f 74  peration are not
615e0 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e 64 20   backed out and 
615f0 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f  no rollback.** o
61600 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 20 6d  ccurs.  IGNORE m
61610 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61  eans that the pa
61620 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 68 61  rticular row tha
61630 74 20 63 61 75 73 65 64 20 74 68 65 20 63 6f 6e  t caused the con
61640 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72  straint.** error
61650 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 65 64   is not inserted
61660 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 50 72   or updated.  Pr
61670 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75  ocessing continu
61680 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 0a  es and no error.
61690 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ** is returned. 
616a0 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 20 74   REPLACE means t
616b0 68 61 74 20 70 72 65 65 78 69 73 74 69 6e 67 20  hat preexisting 
616c0 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68  database rows th
616d0 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 20 55  at caused.** a U
616e0 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
616f0 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 20 72   violation are r
61700 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 20 74  emoved so that t
61710 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 6f 72  he new insert or
61720 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e 20 70  .** update can p
61730 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 73 73  roceed.  Process
61740 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e  ing continues an
61750 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65  d no error is re
61760 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 45  ported..**.** RE
61770 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c 4c 2c  STRICT, SETNULL,
61780 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 63 74   and CASCADE act
61790 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c 79 20  ions apply only 
617a0 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e  to foreign keys.
617b0 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 73 20  .** RESTRICT is 
617c0 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52  the same as ABOR
617d0 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 20  T for IMMEDIATE 
617e0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 6e 64  foreign keys and
617f0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20   the.** same as 
61800 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 45 46  ROLLBACK for DEF
61810 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 45 54  ERRED keys.  SET
61820 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20  NULL means that 
61830 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b  the foreign.** k
61840 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  ey is set to NUL
61850 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 61 6e  L.  CASCADE mean
61860 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 45 20  s that a DELETE 
61870 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 68 65  or UPDATE of the
61880 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 74  .** referenced t
61890 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 6f 70  able row is prop
618a0 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 65 20  agated into the 
618b0 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 20 74  row that holds t
618c0 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65  he.** foreign ke
618d0 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 6f  y..** .** The fo
618e0 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69 63  llowing symbolic
618f0 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 65 64   values are used
61900 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68   to record which
61910 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 74 69   type.** of acti
61920 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a 23  on to take..*/.#
61930 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20 20  define OE_None  
61940 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 65 20     0   /* There 
61950 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 6e 74  is no constraint
61960 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 64 65   to check */.#de
61970 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b  fine OE_Rollback
61980 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 68 65   1   /* Fail the
61990 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72   operation and r
619a0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
619b0 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  saction */.#defi
619c0 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 20 32  ne OE_Abort    2
619d0 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 20 63     /* Back out c
619e0 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 6e 6f  hanges but do no
619f0 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61   rollback transa
61a00 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
61a10 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 20 20   OE_Fail     3  
61a20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f 70 65   /* Stop the ope
61a30 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 76 65  ration but leave
61a40 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 6e 67   all prior chang
61a50 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  es */.#define OE
61a60 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 2f 2a  _Ignore   4   /*
61a70 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f   Ignore the erro
61a80 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 65  r. Do not do the
61a90 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
61aa0 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  E */.#define OE_
61ab0 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f 2a 20  Replace  5   /* 
61ac0 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e 67 20  Delete existing 
61ad0 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 6f 20  record, then do 
61ae0 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45  INSERT or UPDATE
61af0 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f   */..#define OE_
61b00 52 65 73 74 72 69 63 74 20 36 20 20 20 2f 2a 20  Restrict 6   /* 
61b10 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 4d 4d  OE_Abort for IMM
61b20 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c 62  EDIATE, OE_Rollb
61b30 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 45 44  ack for DEFERRED
61b40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53   */.#define OE_S
61b50 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a 20 53  etNull  7   /* S
61b60 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  et the foreign k
61b70 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 4c 4c  ey value to NULL
61b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53   */.#define OE_S
61b90 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a 20 53  etDflt  8   /* S
61ba0 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  et the foreign k
61bb0 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20  ey value to its 
61bc0 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 66 69  default */.#defi
61bd0 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 20 39  ne OE_Cascade  9
61be0 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 74 68     /* Cascade th
61bf0 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a 23 64  e changes */..#d
61c00 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c 74  efine OE_Default
61c10 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 61 74    99  /* Do what
61c20 65 76 65 72 20 74 68 65 20 64 65 66 61 75 6c 74  ever the default
61c30 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a 0a   action is */...
61c40 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
61c50 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
61c60 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
61c70 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
61c80 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
61c90 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4b 65  to sqlite3VdbeKe
61ca0 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 73 20  yCompare and is 
61cb0 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  used to control 
61cc0 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73  the .** comparis
61cd0 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 69 6e  on of the two in
61ce0 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 74 72  dex keys..*/.str
61cf0 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a 20 20  uct KeyInfo {.  
61d00 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
61d10 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
61d20 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
61d30 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20  /.  u8 enc;     
61d40 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
61d50 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 20 6f  encoding - one o
61d60 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 2a 20  f the TEXT_Utf* 
61d70 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 36 20  values */.  u16 
61d80 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20  nField;         
61d90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
61da0 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b 5d 20  ries in aColl[] 
61db0 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72  */.  u8 *aSortOr
61dc0 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 20 64  der;     /* If d
61dd0 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 74 4f  efined an aSortO
61de0 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 65 2c  rder[i] is true,
61df0 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a 20 20   sort DESC */.  
61e00 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b 31  CollSeq *aColl[1
61e10 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67  ];  /* Collating
61e20 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61   sequence for ea
61e30 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6b  ch term of the k
61e40 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ey */.};../*.** 
61e50 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
61e60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
61e70 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
61e80 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
61e90 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65 78  .** single index
61ea0 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73   record that has
61eb0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 61   already been pa
61ec0 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69 6e  rsed out into in
61ed0 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c 75  dividual.** valu
61ee0 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63 6f  es..**.** A reco
61ef0 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20  rd is an object 
61f00 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  that contains on
61f10 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 73  e or more fields
61f20 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65 63   of data..** Rec
61f30 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74 6f  ords are used to
61f40 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65   store the conte
61f50 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72 6f  nt of a table ro
61f60 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a 2a  w and to store.*
61f70 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e 20  * the key of an 
61f80 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20 65  index.  A blob e
61f90 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63  ncoding of a rec
61fa0 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20 62  ord is created b
61fb0 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b 65  y.** the OP_Make
61fc0 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66  Record opcode of
61fd0 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69 73   the VDBE and is
61fe0 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62 79   disassembled by
61ff0 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d   the.** OP_Colum
62000 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  n opcode..**.** 
62010 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68  This structure h
62020 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74 68  olds a record th
62030 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
62040 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65 64  een disassembled
62050 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f 6e  .** into its con
62060 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73 2e  stituent fields.
62070 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61 63  .*/.struct Unpac
62080 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b 65  kedRecord {.  Ke
62090 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
620a0 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 61    /* Collation a
620b0 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69 6e  nd sort-order in
620c0 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  formation */.  u
620d0 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  16 nField;      
620e0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
620f0 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65 6d  entries in apMem
62100 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67  [] */.  u16 flag
62110 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  s;          /* B
62120 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73 2e  oolean settings.
62130 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20 62    UNPACKED_... b
62140 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72 6f  elow */.  i64 ro
62150 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  wid;          /*
62160 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b 45   Used by UNPACKE
62170 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  D_PREFIX_SEARCH 
62180 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20  */.  Mem *aMem; 
62190 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
621a0 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
621b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f  Allowed values o
621c0 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  f UnpackedRecord
621d0 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e  .flags.*/.#defin
621e0 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  e UNPACKED_NEED_
621f0 46 52 45 45 20 20 20 20 20 30 78 30 30 30 31 20  FREE     0x0001 
62200 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 66 72   /* Memory is fr
62210 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  om sqlite3Malloc
62220 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e  () */.#define UN
62230 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54  PACKED_NEED_DEST
62240 52 4f 59 20 20 30 78 30 30 30 32 20 20 2f 2a 20  ROY  0x0002  /* 
62250 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75 6c 64 20  apMem[]s should 
62260 61 6c 6c 20 62 65 20 64 65 73 74 72 6f 79 65 64  all be destroyed
62270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41   */.#define UNPA
62280 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49  CKED_IGNORE_ROWI
62290 44 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 67  D  0x0004  /* Ig
622a0 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67 20 72 6f  nore trailing ro
622b0 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a 2f 0a 23  wid on key1 */.#
622c0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
622d0 49 4e 43 52 4b 45 59 20 20 20 20 20 20 20 30 78  INCRKEY       0x
622e0 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65 20 74 68  0008  /* Make th
622f0 69 73 20 6b 65 79 20 61 6e 20 65 70 73 69 6c 6f  is key an epsilo
62300 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23 64 65 66  n larger */.#def
62310 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45  ine UNPACKED_PRE
62320 46 49 58 5f 4d 41 54 43 48 20 20 30 78 30 30 31  FIX_MATCH  0x001
62330 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d  0  /* A prefix m
62340 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72  atch is consider
62350 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66 69 6e 65  ed OK */.#define
62360 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58   UNPACKED_PREFIX
62370 5f 53 45 41 52 43 48 20 30 78 30 30 32 30 20 20  _SEARCH 0x0020  
62380 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 61 74 63  /* A prefix matc
62390 68 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  h is considered 
623a0 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  OK */../*.** Eac
623b0 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 72  h SQL index is r
623c0 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65  epresented in me
623d0 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  mory by an.** in
623e0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
623f0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
62400 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  e..**.** The col
62410 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
62420 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65  e that are to be
62430 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 73   indexed are des
62440 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 65  cribed.** by the
62450 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c   aiColumn[] fiel
62460 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  d of this struct
62470 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ure.  For exampl
62480 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65  e, suppose.** we
62490 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77   have the follow
624a0 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  ing table and in
624b0 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43  dex:.**.**     C
624c0 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28  REATE TABLE Ex1(
624d0 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20  c1 int, c2 int, 
624e0 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20  c3 text);.**    
624f0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 78   CREATE INDEX Ex
62500 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 3b  2 ON Ex1(c3,c1);
62510 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61  .**.** In the Ta
62520 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 65  ble structure de
62530 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e 43  scribing Ex1, nC
62540 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 68  ol==3 because th
62550 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 65  ere are.** three
62560 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
62570 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 49  table.  In the I
62580 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 64  ndex structure d
62590 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32  escribing.** Ex2
625a0 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e  , nColumn==2 sin
625b0 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 6f  ce 2 of the 3 co
625c0 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 65  lumns of Ex1 are
625d0 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65   indexed..** The
625e0 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75   value of aiColu
625f0 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61  mn is {2, 0}.  a
62600 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65  iColumn[0]==2 be
62610 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 69  cause the .** fi
62620 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65  rst column to be
62630 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 61   indexed (c3) ha
62640 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 20  s an index of 2 
62650 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a  in Ex1.aCol[]..*
62660 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c  * The second col
62670 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65  umn to be indexe
62680 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 6e  d (c1) has an in
62690 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45  dex of 0 in.** E
626a0 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65  x1.aCol[], hence
626b0 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d   Ex2.aiColumn[1]
626c0 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  ==0..**.** The I
626d0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65  ndex.onError fie
626e0 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  ld determines wh
626f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
62700 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73   indexed columns
62710 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 71  .** must be uniq
62720 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 64  ue and what to d
62730 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f  o if they are no
62740 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f  t.  When Index.o
62750 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a  nError=OE_None,.
62760 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  ** it means this
62770 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 65   is not a unique
62780 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69   index.  Otherwi
62790 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 75  se it is a uniqu
627a0 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74  e index.** and t
627b0 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65  he value of Inde
627c0 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61  x.onError indica
627d0 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e  te the which con
627e0 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
627f0 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74   .** algorithm t
62800 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65  o employ wheneve
62810 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  r an attempt is 
62820 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61  made to insert a
62830 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65   non-unique.** e
62840 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63  lement..*/.struc
62850 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 72  t Index {.  char
62860 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20   *zName;     /* 
62870 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e 64  Name of this ind
62880 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  ex */.  int nCol
62890 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  umn;     /* Numb
628a0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
628b0 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 20   the table used 
628c0 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f  by this index */
628d0 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e  .  int *aiColumn
628e0 3b 20 20 20 2f 2a 20 57 68 69 63 68 20 63 6f 6c  ;   /* Which col
628f0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 62 79  umns are used by
62900 20 74 68 69 73 20 69 6e 64 65 78 2e 20 20 31 73   this index.  1s
62910 74 20 69 73 20 30 20 2a 2f 0a 20 20 75 6e 73 69  t is 0 */.  unsi
62920 67 6e 65 64 20 2a 61 69 52 6f 77 45 73 74 3b 20  gned *aiRowEst; 
62930 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 41 4e 41  /* Result of ANA
62940 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f 77 73 20  LYZE: Est. rows 
62950 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63 68  selected by each
62960 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 62   column */.  Tab
62970 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 2f 2a  le *pTable;   /*
62980 20 54 68 65 20 53 51 4c 20 74 61 62 6c 65 20 62   The SQL table b
62990 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a  eing indexed */.
629a0 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20    int tnum;     
629b0 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61     /* Page conta
629c0 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 20 74 68  ining root of th
629d0 69 73 20 69 6e 64 65 78 20 69 6e 20 64 61 74 61  is index in data
629e0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75  base file */.  u
629f0 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20 20  8 onError;      
62a00 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  /* OE_Abort, OE_
62a10 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61  Ignore, OE_Repla
62a20 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a  ce, or OE_None *
62a30 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e 64 65 78  /.  u8 autoIndex
62a40 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
62a50 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
62a60 20 63 72 65 61 74 65 64 20 28 65 78 3a 20 62 79   created (ex: by
62a70 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 20 63 68   UNIQUE) */.  ch
62a80 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f  ar *zColAff;   /
62a90 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e  * String definin
62aa0 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  g the affinity o
62ab0 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f  f each column */
62ac0 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b  .  Index *pNext;
62ad0 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
62ae0 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64  index associated
62af0 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 74   with the same t
62b00 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61  able */.  Schema
62b10 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63   *pSchema; /* Sc
62b20 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20  hema containing 
62b30 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20  this index */.  
62b40 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20  u8 *aSortOrder; 
62b50 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 7a   /* Array of siz
62b60 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e  e Index.nColumn.
62b70 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c   True==DESC, Fal
62b80 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 63 68 61  se==ASC */.  cha
62b90 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a  r **azColl;   /*
62ba0 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61 74   Array of collat
62bb0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
62bc0 65 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a  es for index */.
62bd0 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61    IndexSample *a
62be0 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 41 72  Sample;    /* Ar
62bf0 72 61 79 20 6f 66 20 53 51 4c 49 54 45 5f 49 4e  ray of SQLITE_IN
62c00 44 45 58 5f 53 41 4d 50 4c 45 53 20 73 61 6d 70  DEX_SAMPLES samp
62c10 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  les */.};../*.**
62c20 20 45 61 63 68 20 73 61 6d 70 6c 65 20 73 74 6f   Each sample sto
62c30 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  red in the sqlit
62c40 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73  e_stat2 table is
62c50 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20   represented in 
62c60 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 69 6e 67  memory .** using
62c70 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 20   a structure of 
62c80 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f 0a 73 74  this type..*/.st
62c90 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70 6c 65  ruct IndexSample
62ca0 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20   {.  union {.   
62cb0 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
62cc0 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 79   /* Value if eTy
62cd0 70 65 20 69 73 20 53 51 4c 49 54 45 5f 54 45 58  pe is SQLITE_TEX
62ce0 54 20 6f 72 20 53 51 4c 49 54 45 5f 42 4c 4f 42  T or SQLITE_BLOB
62cf0 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 72   */.    double r
62d00 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65  ;       /* Value
62d10 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c   if eType is SQL
62d20 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20 53 51 4c  ITE_FLOAT or SQL
62d30 49 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a 20  ITE_INTEGER */. 
62d40 20 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70 65   } u;.  u8 eType
62d50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ;         /* SQL
62d60 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45  ITE_NULL, SQLITE
62d70 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74 63  _INTEGER ... etc
62d80 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79 74 65 3b  . */.  u8 nByte;
62d90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
62da0 20 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78 74   in byte of text
62db0 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 7d 3b 0a   or blob. */.};.
62dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65  ./*.** Each toke
62dd0 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20  n coming out of 
62de0 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20  the lexer is an 
62df0 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74  instance of.** t
62e00 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
62e10 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20  Tokens are also 
62e20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
62e30 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  an expression..*
62e40 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b  *.** Note if Tok
62e50 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b  en.z==0 then Tok
62e60 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e  en.dyn and Token
62e70 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  .n are undefined
62e80 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74   and.** may cont
62e90 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65  ain random value
62ea0 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20  s.  Do not make 
62eb0 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20  any assumptions 
62ec0 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a  about Token.dyn.
62ed0 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77  ** and Token.n w
62ee0 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a  hen Token.z==0..
62ef0 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20  */.struct Token 
62f00 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
62f10 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f  z;     /* Text o
62f20 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f  f the token.  No
62f30 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65  t NULL-terminate
62f40 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  d! */.  unsigned
62f50 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a 20 4e 75   int n;    /* Nu
62f60 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
62f70 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65 6e  rs in this token
62f80 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
62f90 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
62fa0 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  s structure cont
62fb0 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ains information
62fc0 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72   needed to gener
62fd0 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20  ate.** code for 
62fe0 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63 6f  a SELECT that co
62ff0 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65  ntains aggregate
63000 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
63010 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54 4b  * If Expr.op==TK
63020 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20 54  _AGG_COLUMN or T
63030 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 74  K_AGG_FUNCTION t
63040 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e 66  hen Expr.pAggInf
63050 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  o is a.** pointe
63060 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
63070 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e 69  ure.  The Expr.i
63080 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73 20  Column field is 
63090 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a 20  the index in.** 
630a0 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 6f  AggInfo.aCol[] o
630b0 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b  r AggInfo.aFunc[
630c0 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ] of information
630d0 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72   needed to gener
630e0 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20  ate.** code for 
630f0 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a  that node..**.**
63100 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42   AggInfo.pGroupB
63110 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61 46  y and AggInfo.aF
63120 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74 20  unc.pExpr point 
63130 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69 6e  to fields within
63140 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
63150 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72   Select structur
63160 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
63170 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
63180 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a 2a  ement.  These.**
63190 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20 6e   fields do not n
631a0 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 20  eed to be freed 
631b0 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69 6e  when deallocatin
631c0 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73 74  g the AggInfo st
631d0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
631e0 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20 75  ct AggInfo {.  u
631f0 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20 20  8 directMode;   
63200 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74         /* Direct
63210 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65 20   rendering mode 
63220 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61 20  means take data 
63230 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20 20  directly.       
63240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63250 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 63     ** from sourc
63260 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72 20  e tables rather 
63270 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d 75  than from accumu
63280 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20 75  lators */.  u8 u
63290 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20 20  seSortingIdx;   
632a0 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63 74      /* In direct
632b0 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63 65   mode, reference
632c0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
632d0 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20 20  ex rather.      
632e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632f0 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20      ** than the 
63300 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a  source table */.
63310 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 78    int sortingIdx
63320 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
63330 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
63340 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
63350 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
63360 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20  GroupBy;     /* 
63370 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c 61  The group by cla
63380 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6f  use */.  int nSo
63390 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20 20  rtingColumn;    
633a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
633b0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 72  lumns in the sor
633c0 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20  ting index */.  
633d0 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
633e0 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20 65  ol {    /* For e
633f0 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64 20  ach column used 
63400 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73  in source tables
63410 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   */.    Table *p
63420 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
63430 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c 65   /* Source table
63440 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61 62   */.    int iTab
63450 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
63460 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
63470 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20  r of the source 
63480 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
63490 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
634a0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
634b0 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68  number within th
634c0 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a  e source table *
634d0 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 65  /.    int iSorte
634e0 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f  rColumn;       /
634f0 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * Column number 
63500 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  in the sorting i
63510 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ndex */.    int 
63520 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  iMem;           
63530 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c       /* Memory l
63540 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63 74  ocation that act
63550 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f 72  s as accumulator
63560 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45   */.    Expr *pE
63570 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
63580 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   /* The original
63590 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
635a0 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74 20   } *aCol;.  int 
635b0 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
635c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
635d0 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69 6e   used entries in
635e0 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74   aCol[] */.  int
635f0 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20 20   nColumnAlloc;  
63600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
63610 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65  f slots allocate
63620 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f 0a  d for aCol[] */.
63630 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61 74    int nAccumulat
63640 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  or;       /* Num
63650 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74  ber of columns t
63660 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67 68  hat show through
63670 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 0a   to the output..
63680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63690 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64 64            ** Add
636a0 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20  itional columns 
636b0 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61 73  are used only as
636c0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 20   parameters to. 
636d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
636e0 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67 72           ** aggr
636f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
63700 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  */.  struct AggI
63710 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a 20  nfo_func {   /* 
63720 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67 61  For each aggrega
63730 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  te function */. 
63740 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
63750 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
63760 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64 69  xpression encodi
63770 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ng the function 
63780 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  */.    FuncDef *
63790 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  pFunc;          
637a0 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 65  /* The aggregate
637b0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
637c0 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
637d0 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20  int iMem;       
637e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
637f0 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74  ry location that
63800 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c   acts as accumul
63810 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ator */.    int 
63820 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20  iDistinct;      
63830 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 61       /* Ephemera
63840 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20  l table used to 
63850 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43 54  enforce DISTINCT
63860 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b 0a   */.  } *aFunc;.
63870 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20 20    int nFunc;    
63880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
63890 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
638a0 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69  n aFunc[] */.  i
638b0 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20 20  nt nFuncAlloc;  
638c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
638d0 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61   of slots alloca
638e0 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d 20  ted for aFunc[] 
638f0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
63900 20 64 61 74 61 74 79 70 65 20 79 6e 56 61 72 20   datatype ynVar 
63910 69 73 20 61 20 73 69 67 6e 65 64 20 69 6e 74 65  is a signed inte
63920 67 65 72 2c 20 65 69 74 68 65 72 20 31 36 2d 62  ger, either 16-b
63930 69 74 20 6f 72 20 33 32 2d 62 69 74 2e 0a 2a 2a  it or 32-bit..**
63940 20 55 73 75 61 6c 6c 79 20 69 74 20 69 73 20 31   Usually it is 1
63950 36 2d 62 69 74 73 2e 20 20 42 75 74 20 69 66 20  6-bits.  But if 
63960 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
63970 42 4c 45 5f 4e 55 4d 42 45 52 20 69 73 20 67 72  BLE_NUMBER is gr
63980 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 33 32  eater.** than 32
63990 37 36 37 20 77 65 20 68 61 76 65 20 74 6f 20 6d  767 we have to m
639a0 61 6b 65 20 69 74 20 33 32 2d 62 69 74 2e 20 20  ake it 32-bit.  
639b0 31 36 2d 62 69 74 20 69 73 20 70 72 65 66 65 72  16-bit is prefer
639c0 72 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 69  red because.** i
639d0 74 20 75 73 65 73 20 6c 65 73 73 20 6d 65 6d 6f  t uses less memo
639e0 72 79 20 69 6e 20 74 68 65 20 45 78 70 72 20 6f  ry in the Expr o
639f0 62 6a 65 63 74 2c 20 77 68 69 63 68 20 69 73 20  bject, which is 
63a00 61 20 62 69 67 20 6d 65 6d 6f 72 79 20 75 73 65  a big memory use
63a10 72 0a 2a 2a 20 69 6e 20 73 79 73 74 65 6d 73 20  r.** in systems 
63a20 77 69 74 68 20 6c 6f 74 73 20 6f 66 20 70 72 65  with lots of pre
63a30 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
63a40 2e 20 20 41 6e 64 20 66 65 77 20 61 70 70 6c 69  .  And few appli
63a50 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65 65 64 20  cations.** need 
63a60 6d 6f 72 65 20 74 68 61 6e 20 61 62 6f 75 74 20  more than about 
63a70 31 30 20 6f 72 20 32 30 20 76 61 72 69 61 62 6c  10 or 20 variabl
63a80 65 73 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 78  es.  But some ex
63a90 74 72 65 6d 65 20 75 73 65 72 73 20 77 61 6e 74  treme users want
63aa0 0a 2a 2a 20 74 6f 20 68 61 76 65 20 70 72 65 70  .** to have prep
63ab0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
63ac0 77 69 74 68 20 6f 76 65 72 20 33 32 37 36 37 20  with over 32767 
63ad0 76 61 72 69 61 62 6c 65 73 2c 20 61 6e 64 20 66  variables, and f
63ae0 6f 72 20 74 68 65 6d 0a 2a 2a 20 74 68 65 20 6f  or them.** the o
63af0 70 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62  ption is availab
63b00 6c 65 20 28 61 74 20 63 6f 6d 70 69 6c 65 2d 74  le (at compile-t
63b10 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ime)..*/.#if SQL
63b20 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45  ITE_MAX_VARIABLE
63b30 5f 4e 55 4d 42 45 52 3c 3d 33 32 37 36 37 0a 74  _NUMBER<=32767.t
63b40 79 70 65 64 65 66 20 69 31 36 20 79 6e 56 61 72  ypedef i16 ynVar
63b50 3b 0a 23 65 6c 73 65 0a 74 79 70 65 64 65 66 20  ;.#else.typedef 
63b60 69 6e 74 20 79 6e 56 61 72 3b 0a 23 65 6e 64 69  int ynVar;.#endi
63b70 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f  f../*.** Each no
63b80 64 65 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  de of an express
63b90 69 6f 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65  ion in the parse
63ba0 20 74 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74   tree is an inst
63bb0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ance.** of this 
63bc0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
63bd0 20 45 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20   Expr.op is the 
63be0 6f 70 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65  opcode. The inte
63bf0 67 65 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e  ger parser token
63c00 20 63 6f 64 65 73 20 61 72 65 20 72 65 75 73 65   codes are reuse
63c10 64 0a 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20  d.** as opcodes 
63c20 68 65 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c  here. For exampl
63c30 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 64 65  e, the parser de
63c40 66 69 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62  fines TK_GE to b
63c50 65 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  e an integer.** 
63c60 63 6f 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e  code representin
63c70 67 20 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61  g the ">=" opera
63c80 74 6f 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69  tor. This same i
63c90 6e 74 65 67 65 72 20 63 6f 64 65 20 69 73 20 72  nteger code is r
63ca0 65 75 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72  eused.** to repr
63cb0 65 73 65 6e 74 20 74 68 65 20 67 72 65 61 74 65  esent the greate
63cc0 72 2d 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d  r-than-or-equal-
63cd0 74 6f 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74  to operator in t
63ce0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
63cf0 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   tree..**.** If 
63d00 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
63d10 73 20 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c  s an SQL literal
63d20 20 28 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b   (TK_INTEGER, TK
63d30 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c  _FLOAT, TK_BLOB,
63d40 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e   .** or TK_STRIN
63d50 47 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f  G), then Expr.to
63d60 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ken contains the
63d70 20 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c   text of the SQL
63d80 20 6c 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20   literal. If.** 
63d90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
63da0 73 20 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b  s a variable (TK
63db0 5f 56 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e  _VARIABLE), then
63dc0 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74   Expr.token cont
63dd0 61 69 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72  ains the .** var
63de0 69 61 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61  iable name. Fina
63df0 6c 6c 79 2c 20 69 66 20 74 68 65 20 65 78 70 72  lly, if the expr
63e00 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c  ession is an SQL
63e10 20 66 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55   function (TK_FU
63e20 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e  NCTION),.** then
63e30 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74   Expr.token cont
63e40 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ains the name of
63e50 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   the function..*
63e60 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74  *.** Expr.pRight
63e70 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20   and Expr.pLeft 
63e80 61 72 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64  are the left and
63e90 20 72 69 67 68 74 20 73 75 62 65 78 70 72 65 73   right subexpres
63ea0 73 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69  sions of a.** bi
63eb0 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45  nary operator. E
63ec0 69 74 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61  ither or both ma
63ed0 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  y be NULL..**.**
63ee0 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73   Expr.x.pList is
63ef0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d   a list of argum
63f00 65 6e 74 73 20 69 66 20 74 68 65 20 65 78 70 72  ents if the expr
63f10 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c  ession is an SQL
63f20 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20   function,.** a 
63f30 43 41 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20  CASE expression 
63f40 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73  or an IN express
63f50 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
63f60 22 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20  "<lhs> IN (<y>, 
63f70 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70  <z>...)"..** Exp
63f80 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75  r.x.pSelect is u
63f90 73 65 64 20 69 66 20 74 68 65 20 65 78 70 72 65  sed if the expre
63fa0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73  ssion is a sub-s
63fb0 65 6c 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72  elect or an expr
63fc0 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65  ession of.** the
63fd0 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20   form "<lhs> IN 
63fe0 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49  (SELECT ...)". I
63ff0 66 20 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65  f the EP_xIsSele
64000 63 74 20 62 69 74 20 69 73 20 73 65 74 20 69 6e  ct bit is set in
64010 20 74 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61   the.** Expr.fla
64020 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78  gs mask, then Ex
64030 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20  pr.x.pSelect is 
64040 76 61 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65  valid. Otherwise
64050 2c 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69  , Expr.x.pList i
64060 73 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a  s .** valid..**.
64070 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e  ** An expression
64080 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20   of the form ID 
64090 6f 72 20 49 44 2e 49 44 20 72 65 66 65 72 73 20  or ID.ID refers 
640a0 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  to a column in a
640b0 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73   table..** For s
640c0 75 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  uch expressions,
640d0 20 45 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20   Expr.op is set 
640e0 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64  to TK_COLUMN and
640f0 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a   Expr.iTable is.
64100 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63  ** the integer c
64110 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
64120 61 20 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f  a VDBE cursor po
64130 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74  inting to that t
64140 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72  able and.** Expr
64150 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20  .iColumn is the 
64160 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f  column number fo
64170 72 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63  r the specific c
64180 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a  olumn.  If the.*
64190 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  * expression is 
641a0 75 73 65 64 20 61 73 20 61 20 72 65 73 75 6c 74  used as a result
641b0 20 69 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65   in an aggregate
641c0 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68   SELECT, then th
641d0 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c  e.** value is al
641e0 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  so stored in the
641f0 20 45 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d   Expr.iAgg colum
64200 6e 20 69 6e 20 74 68 65 20 61 67 67 72 65 67 61  n in the aggrega
64210 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74  te so that.** it
64220 20 63 61 6e 20 62 65 20 61 63 63 65 73 73 65 64   can be accessed
64230 20 61 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65   after all aggre
64240 67 61 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74  gates are comput
64250 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
64260 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
64270 6e 20 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62  n unbound variab
64280 6c 65 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65  le marker (a que
64290 73 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63  stion mark .** c
642a0 68 61 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20  haracter '?' in 
642b0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
642c0 29 20 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e  ) then the Expr.
642d0 69 54 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65  iTable holds the
642e0 20 69 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65   index .** numbe
642f0 72 20 66 6f 72 20 74 68 61 74 20 76 61 72 69 61  r for that varia
64300 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ble..**.** If th
64310 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
64320 61 20 73 75 62 71 75 65 72 79 20 74 68 65 6e 20  a subquery then 
64330 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c  Expr.iColumn hol
64340 64 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  ds an integer.**
64350 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
64360 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
64370 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75  result of the su
64380 62 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a  bquery.  If the.
64390 2a 2a 20 73 75 62 71 75 65 72 79 20 67 69 76 65  ** subquery give
643a0 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73  s a constant res
643b0 75 6c 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65  ult, then iTable
643c0 20 69 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20   is -1.  If the 
643d0 73 75 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65  subquery.** give
643e0 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e  s a different an
643f0 73 77 65 72 20 61 74 20 64 69 66 66 65 72 65 6e  swer at differen
64400 74 20 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73  t times during s
64410 74 61 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73  tatement process
64420 69 6e 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62  ing.** then iTab
64430 6c 65 20 69 73 20 74 68 65 20 61 64 64 72 65 73  le is the addres
64440 73 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e  s of a subroutin
64450 65 20 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20  e that computes 
64460 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a  the subquery..**
64470 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 20  .** If the Expr 
64480 69 73 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f  is of type OP_Co
64490 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61  lumn, and the ta
644a0 62 6c 65 20 69 74 20 69 73 20 73 65 6c 65 63 74  ble it is select
644b0 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61  ing from.** is a
644c0 20 64 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74   disk table or t
644d0 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64  he "old.*" pseud
644e0 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54  o-table, then pT
644f0 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ab points to the
64500 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
64510 67 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69  g table definiti
64520 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41  on..**.** ALLOCA
64530 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a  TION NOTES:.**.*
64540 2a 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63  * Expr objects c
64550 61 6e 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20  an use a lot of 
64560 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20  memory space in 
64570 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
64580 20 20 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64    To.** help red
64590 75 63 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69  uce memory requi
645a0 72 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d  rements, sometim
645b0 65 73 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63  es an Expr objec
645c0 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75  t will be.** tru
645d0 6e 63 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20  ncated.  And to 
645e0 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65  reduce the numbe
645f0 72 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  r of memory allo
64600 63 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d  cations, sometim
64610 65 73 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72  es.** two or mor
64620 65 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77  e Expr objects w
64630 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e  ill be stored in
64640 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79   a single memory
64650 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20   allocation,.** 
64660 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 45 78  together with Ex
64670 70 72 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67  pr.zToken string
64680 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
64690 45 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45  EP_Reduced and E
646a0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67  P_TokenOnly flag
646b0 73 20 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a  s are set when.*
646c0 2a 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74  * an Expr object
646d0 20 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20   is truncated.  
646e0 57 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20  When EP_Reduced 
646f0 69 73 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c  is set, then all
64700 0a 2a 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78  .** the child Ex
64710 70 72 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68  pr objects in th
64720 65 20 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64  e Expr.pLeft and
64730 20 45 78 70 72 2e 70 52 69 67 68 74 20 73 75 62   Expr.pRight sub
64740 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e  trees.** are con
64750 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68  tained within th
64760 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c  e same memory al
64770 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c  location.  Note,
64780 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a   however, that.*
64790 2a 20 74 68 65 20 73 75 62 74 72 65 65 73 20 69  * the subtrees i
647a0 6e 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f  n Expr.x.pList o
647b0 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  r Expr.x.pSelect
647c0 20 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61   are always sepa
647d0 72 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61  rately.** alloca
647e0 74 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20  ted, regardless 
647f0 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
64800 74 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20  t EP_Reduced is 
64810 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45  set..*/.struct E
64820 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20  xpr {.  u8 op;  
64830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
64840 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66  * Operation perf
64850 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f  ormed by this no
64860 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66  de */.  char aff
64870 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f  inity;         /
64880 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f  * The affinity o
64890 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
648a0 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d  0 if not a colum
648b0 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  n */.  u16 flags
648c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
648d0 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20   Various flags. 
648e0 20 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20   EP_* See below 
648f0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
64900 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20   char *zToken;  
64910 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
64920 20 76 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72   value. Zero ter
64930 6d 69 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75  minated and dequ
64940 6f 74 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  oted */.    int 
64950 69 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20  iValue;         
64960 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
64970 6c 75 65 20 69 66 20 45 50 5f 49 6e 74 56 61 6c  lue if EP_IntVal
64980 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20  ue */.  } u;..  
64990 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b  /* If the EP_Tok
649a0 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73  enOnly flag is s
649b0 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  et in the Expr.f
649c0 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20  lags mask, then 
649d0 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73  no.  ** space is
649e0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
649f0 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20  he fields below 
64a00 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61  this point. An a
64a10 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61  ttempt to.  ** a
64a20 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20  ccess them will 
64a30 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66  result in a segf
64a40 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74  ault or malfunct
64a50 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a  ion. .  ********
64a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
64aa0 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20    Expr *pLeft;  
64ab0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74           /* Left
64ac0 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45 78   subnode */.  Ex
64ad0 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20  pr *pRight;     
64ae0 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 73 75       /* Right su
64af0 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e  bnode */.  union
64b00 20 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20   {.    ExprList 
64b10 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 46  *pList;     /* F
64b20 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
64b30 73 20 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e 20  s or in "<expr> 
64b40 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 74 29 22  IN (<expr-list)"
64b50 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a   */.    Select *
64b60 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20  pSelect;     /* 
64b70 55 73 65 64 20 66 6f 72 20 73 75 62 2d 73 65 6c  Used for sub-sel
64b80 65 63 74 73 20 61 6e 64 20 22 3c 65 78 70 72 3e  ects and "<expr>
64b90 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22 20   IN (<select>)" 
64ba0 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c  */.  } x;.  Coll
64bb0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
64bc0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74     /* The collat
64bd0 69 6f 6e 20 74 79 70 65 20 6f 66 20 74 68 65 20  ion type of the 
64be0 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a  column or 0 */..
64bf0 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 52    /* If the EP_R
64c00 65 64 75 63 65 64 20 66 6c 61 67 20 69 73 20 73  educed flag is s
64c10 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  et in the Expr.f
64c20 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20  lags mask, then 
64c30 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73  no.  ** space is
64c40 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
64c50 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20  he fields below 
64c60 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61  this point. An a
64c70 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61  ttempt to.  ** a
64c80 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20  ccess them will 
64c90 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66  result in a segf
64ca0 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74  ault or malfunct
64cb0 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion..  *********
64cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20  ************/.. 
64d00 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20   int iTable;    
64d10 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f          /* TK_CO
64d20 4c 55 4d 4e 3a 20 63 75 72 73 6f 72 20 6e 75 6d  LUMN: cursor num
64d30 62 65 72 20 6f 66 20 74 61 62 6c 65 20 68 6f 6c  ber of table hol
64d40 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 20 20  ding column.    
64d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64d60 20 20 20 20 20 2a 2a 20 54 4b 5f 52 45 47 49 53       ** TK_REGIS
64d70 54 45 52 3a 20 72 65 67 69 73 74 65 72 20 6e 75  TER: register nu
64d80 6d 62 65 72 0a 20 20 20 20 20 20 20 20 20 20 20  mber.           
64d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
64da0 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 31 20 2d   TK_TRIGGER: 1 -
64db0 3e 20 6e 65 77 2c 20 30 20 2d 3e 20 6f 6c 64 20  > new, 0 -> old 
64dc0 2a 2f 0a 20 20 79 6e 56 61 72 20 69 43 6f 6c 75  */.  ynVar iColu
64dd0 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  mn;         /* T
64de0 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e  K_COLUMN: column
64df0 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20   index.  -1 for 
64e00 72 6f 77 69 64 2e 0a 20 20 20 20 20 20 20 20 20  rowid..         
64e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64e20 2a 2a 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20  ** TK_VARIABLE: 
64e30 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
64e40 28 61 6c 77 61 79 73 20 3e 3d 20 31 29 2e 20 2a  (always >= 1). *
64e50 2f 0a 20 20 69 31 36 20 69 41 67 67 3b 20 20 20  /.  i16 iAgg;   
64e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68             /* Wh
64e70 69 63 68 20 65 6e 74 72 79 20 69 6e 20 70 41 67  ich entry in pAg
64e80 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72  gInfo->aCol[] or
64e90 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20   ->aFunc[] */.  
64ea0 69 31 36 20 69 52 69 67 68 74 4a 6f 69 6e 54 61  i16 iRightJoinTa
64eb0 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f  ble;   /* If EP_
64ec0 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69  FromJoin, the ri
64ed0 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  ght table of the
64ee0 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38 20 66 6c   join */.  u8 fl
64ef0 61 67 73 32 3b 20 20 20 20 20 20 20 20 20 20 20  ags2;           
64f00 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 65 74 20    /* Second set 
64f10 6f 66 20 66 6c 61 67 73 2e 20 20 45 50 32 5f 2e  of flags.  EP2_.
64f20 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70 32 3b 20  .. */.  u8 op2; 
64f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
64f40 2a 20 49 66 20 61 20 54 4b 5f 52 45 47 49 53 54  * If a TK_REGIST
64f50 45 52 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ER, the original
64f60 20 76 61 6c 75 65 20 6f 66 20 45 78 70 72 2e 6f   value of Expr.o
64f70 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a  p */.  AggInfo *
64f80 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 20 2f 2a  pAggInfo;     /*
64f90 20 55 73 65 64 20 62 79 20 54 4b 5f 41 47 47 5f   Used by TK_AGG_
64fa0 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b 5f 41 47  COLUMN and TK_AG
64fb0 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20  G_FUNCTION */.  
64fc0 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
64fd0 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
64fe0 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78  for TK_COLUMN ex
64ff0 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69  pressions. */.#i
65000 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
65010 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20  R_DEPTH>0.  int 
65020 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
65030 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
65040 74 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20  the tree headed 
65050 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a  by this node */.
65060 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
65070 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
65080 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20  re the meanings 
65090 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45  of bits in the E
650a0 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e  xpr.flags field.
650b0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46  .*/.#define EP_F
650c0 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 30 30 31  romJoin   0x0001
650d0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 65 64 20    /* Originated 
650e0 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  in ON or USING c
650f0 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
65100 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 67  */.#define EP_Ag
65110 67 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20  g        0x0002 
65120 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65   /* Contains one
65130 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 65 67 61   or more aggrega
65140 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  te functions */.
65150 23 64 65 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c  #define EP_Resol
65160 76 65 64 20 20 20 30 78 30 30 30 34 20 20 2f 2a  ved   0x0004  /*
65170 20 49 44 73 20 68 61 76 65 20 62 65 65 6e 20 72   IDs have been r
65180 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d  esolved to COLUM
65190 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  Ns */.#define EP
651a0 5f 45 72 72 6f 72 20 20 20 20 20 20 30 78 30 30  _Error      0x00
651b0 30 38 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f  08  /* Expressio
651c0 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f  n contains one o
651d0 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f  r more errors */
651e0 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 69 73 74  .#define EP_Dist
651f0 69 6e 63 74 20 20 20 30 78 30 30 31 30 20 20 2f  inct   0x0010  /
65200 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63  * Aggregate func
65210 74 69 6f 6e 20 77 69 74 68 20 44 49 53 54 49 4e  tion with DISTIN
65220 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64  CT keyword */.#d
65230 65 66 69 6e 65 20 45 50 5f 56 61 72 53 65 6c 65  efine EP_VarSele
65240 63 74 20 20 30 78 30 30 32 30 20 20 2f 2a 20 70  ct  0x0020  /* p
65250 53 65 6c 65 63 74 20 69 73 20 63 6f 72 72 65 6c  Select is correl
65260 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61  ated, not consta
65270 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  nt */.#define EP
65280 5f 44 62 6c 51 75 6f 74 65 64 20 20 30 78 30 30  _DblQuoted  0x00
65290 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77  40  /* token.z w
652a0 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e  as originally in
652b0 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 66 69 6e   "..." */.#defin
652c0 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20  e EP_InfixFunc  
652d0 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75 65 20  0x0080  /* True 
652e0 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e  for an infix fun
652f0 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f  ction: LIKE, GLO
65300 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e  B, etc */.#defin
65310 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  e EP_ExpCollate 
65320 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61  0x0100  /* Colla
65330 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70  ting sequence sp
65340 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74  ecified explicit
65350 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ly */.#define EP
65360 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78 30 32  _AnyAff     0x02
65370 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20  00  /* Can take 
65380 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20  a cached column 
65390 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20  of any affinity 
653a0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 69  */.#define EP_Fi
653b0 78 65 64 44 65 73 74 20 20 30 78 30 34 30 30 20  xedDest  0x0400 
653c0 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 64 65   /* Result neede
653d0 64 20 69 6e 20 61 20 73 70 65 63 69 66 69 63 20  d in a specific 
653e0 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 65 66  register */.#def
653f0 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  ine EP_IntValue 
65400 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49 6e 74    0x0800  /* Int
65410 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e 74 61  eger value conta
65420 69 6e 65 64 20 69 6e 20 75 2e 69 56 61 6c 75 65  ined in u.iValue
65430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 78   */.#define EP_x
65440 49 73 53 65 6c 65 63 74 20 20 30 78 31 30 30 30  IsSelect  0x1000
65450 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63 74 20 69    /* x.pSelect i
65460 73 20 76 61 6c 69 64 20 28 6f 74 68 65 72 77 69  s valid (otherwi
65470 73 65 20 78 2e 70 4c 69 73 74 20 69 73 29 20 2a  se x.pList is) *
65480 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65  /..#define EP_Re
65490 64 75 63 65 64 20 20 20 20 30 78 32 30 30 30 20  duced    0x2000 
654a0 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74 20   /* Expr struct 
654b0 69 73 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  is EXPR_REDUCEDS
654c0 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a  IZE bytes only *
654d0 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 54 6f 6b  /.#define EP_Tok
654e0 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 30 20 20  enOnly  0x4000  
654f0 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74 20 69  /* Expr struct i
65500 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  s EXPR_TOKENONLY
65510 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79 20  SIZE bytes only 
65520 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 53 74  */.#define EP_St
65530 61 74 69 63 20 20 20 20 20 30 78 38 30 30 30 20  atic     0x8000 
65540 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 65 6d 6f   /* Held in memo
65550 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20  ry not obtained 
65560 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f  from malloc() */
65570 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
65580 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65  owing are the me
65590 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69  anings of bits i
655a0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73  n the Expr.flags
655b0 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66  2 field..*/.#def
655c0 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64  ine EP2_Malloced
655d0 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 20 20 2f  Token  0x0001  /
655e0 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c 69 74 65  * Need to sqlite
655f0 33 44 62 46 72 65 65 28 29 20 45 78 70 72 2e 7a  3DbFree() Expr.z
65600 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  Token */.#define
65610 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65   EP2_Irreducible
65620 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43      0x0002  /* C
65630 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 5f 52 45  annot EXPRDUP_RE
65640 44 55 43 45 20 74 68 69 73 20 45 78 70 72 20 2a  DUCE this Expr *
65650 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 73 65  /../*.** The pse
65660 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 71 6c 69  udo-routine sqli
65670 74 65 33 45 78 70 72 53 65 74 49 72 72 65 64 75  te3ExprSetIrredu
65680 63 69 62 6c 65 20 73 65 74 73 20 74 68 65 20 45  cible sets the E
65690 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 0a 2a  P2_Irreducible.*
656a0 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 65 78 70  * flag on an exp
656b0 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72  ression structur
656c0 65 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73  e.  This flag is
656d0 20 75 73 65 64 20 66 6f 72 20 56 56 26 41 20 6f   used for VV&A o
656e0 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 72 6f 75  nly.  The.** rou
656f0 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e  tine is implemen
65700 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 20 74  ted as a macro t
65710 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 77  hat only works w
65720 68 65 6e 20 69 6e 20 64 65 62 75 67 67 69 6e 67  hen in debugging
65730 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 61 73 20   mode,.** so as 
65740 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e 20 70 72  not to burden pr
65750 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 2e 0a 2a  oduction code..*
65760 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
65770 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 45  DEBUG.# define E
65780 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
65790 65 28 58 29 20 20 28 58 29 2d 3e 66 6c 61 67 73  e(X)  (X)->flags
657a0 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 64 75 63  2 |= EP2_Irreduc
657b0 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 64 65 66  ible.#else.# def
657c0 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64  ine ExprSetIrred
657d0 75 63 69 62 6c 65 28 58 29 0a 23 65 6e 64 69 66  ucible(X).#endif
657e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61  ../*.** These ma
657f0 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64  cros can be used
65800 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f   to test, set, o
65810 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20  r clear bits in 
65820 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 6c 61  the .** Expr.fla
65830 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65  gs field..*/.#de
65840 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 6f 70  fine ExprHasProp
65850 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 28  erty(E,P)     ((
65860 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d  (E)->flags&(P))=
65870 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 45 78  =(P)).#define Ex
65880 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
65890 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e 66 6c  (E,P)  (((E)->fl
658a0 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65  ags&(P))!=0).#de
658b0 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 6f 70  fine ExprSetProp
658c0 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 45  erty(E,P)     (E
658d0 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64  )->flags|=(P).#d
658e0 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 72 50  efine ExprClearP
658f0 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 28  roperty(E,P)   (
65900 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a  E)->flags&=~(P).
65910 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f  ./*.** Macros to
65920 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e   determine the n
65930 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
65940 65 71 75 69 72 65 64 20 62 79 20 61 20 6e 6f 72  equired by a nor
65950 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73 74 72  mal Expr .** str
65960 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73 74 72  uct, an Expr str
65970 75 63 74 20 77 69 74 68 20 74 68 65 20 45 50 5f  uct with the EP_
65980 52 65 64 75 63 65 64 20 66 6c 61 67 20 73 65 74  Reduced flag set
65990 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 20 0a   in Expr.flags .
659a0 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72 20 73  ** and an Expr s
659b0 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45  truct with the E
659c0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67  P_TokenOnly flag
659d0 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65   set..*/.#define
659e0 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 20 20   EXPR_FULLSIZE  
659f0 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
65a00 45 78 70 72 29 20 20 20 20 20 20 20 20 20 20 20  Expr)           
65a10 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a 2f 0a  /* Full size */.
65a20 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45 44  #define EXPR_RED
65a30 55 43 45 44 53 49 5a 45 20 20 20 20 20 20 20 20  UCEDSIZE        
65a40 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69 54  offsetof(Expr,iT
65a50 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d 6f 6e  able)  /* Common
65a60 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64 65   features */.#de
65a70 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f  fine EXPR_TOKENO
65a80 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f 66 66  NLYSIZE      off
65a90 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65 66 74  setof(Expr,pLeft
65aa0 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66 65 61  )   /* Fewer fea
65ab0 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  tures */../*.** 
65ac0 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20  Flags passed to 
65ad0 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 72 44  the sqlite3ExprD
65ae0 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 53  up() function. S
65af0 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f  ee the header co
65b00 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76 65 20  mment .** above 
65b10 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29  sqlite3ExprDup()
65b20 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f   for details..*/
65b30 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55 50  .#define EXPRDUP
65b40 5f 52 45 44 55 43 45 20 20 20 20 20 20 20 20 20  _REDUCE         
65b50 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65 64 20  0x0001  /* Used 
65b60 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45 78 70  reduced-size Exp
65b70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a  r nodes */../*.*
65b80 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72  * A list of expr
65b90 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 68 20 65  essions.  Each e
65ba0 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20 6f 70  xpression may op
65bb0 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20 61 0a  tionally have a.
65bc0 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65 78 70  ** name.  An exp
65bd0 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61 74 69  r/name combinati
65be0 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  on can be used i
65bf0 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2c 20  n several ways, 
65c00 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 65 20 6c  such.** as the l
65c10 69 73 74 20 6f 66 20 22 65 78 70 72 20 41 53 20  ist of "expr AS 
65c20 49 44 22 20 66 69 65 6c 64 73 20 66 6f 6c 6c 6f  ID" fields follo
65c30 77 69 6e 67 20 61 20 22 53 45 4c 45 43 54 22 20  wing a "SELECT" 
65c40 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 69 73  or in the.** lis
65c50 74 20 6f 66 20 22 49 44 20 3d 20 65 78 70 72 22  t of "ID = expr"
65c60 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 55 50 44   items in an UPD
65c70 41 54 45 2e 20 20 41 20 6c 69 73 74 20 6f 66 20  ATE.  A list of 
65c80 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 0a  expressions can.
65c90 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  ** also be used 
65ca0 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
65cb0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 69  to a function, i
65cc0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
65cd0 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c   a.zName.** fiel
65ce0 64 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a  d is not used..*
65cf0 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4c 69 73  /.struct ExprLis
65d00 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b  t {.  int nExpr;
65d10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65d20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 72 65 73  Number of expres
65d30 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73  sions on the lis
65d40 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f  t */.  int nAllo
65d50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
65d60 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
65d70 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c  es allocated bel
65d80 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 45 43 75  ow */.  int iECu
65d90 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f  rsor;          /
65da0 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 61 73  * VDBE Cursor as
65db0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
65dc0 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20  is ExprList */. 
65dd0 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
65de0 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45 78 70 72  _item {.    Expr
65df0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
65e00 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f     /* The list o
65e10 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  f expressions */
65e20 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  .    char *zName
65e30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
65e40 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74 65 64 20  oken associated 
65e50 77 69 74 68 20 74 68 69 73 20 65 78 70 72 65 73  with this expres
65e60 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72  sion */.    char
65e70 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20 20 20 20   *zSpan;        
65e80 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74     /* Original t
65e90 65 78 74 20 6f 66 20 74 68 65 20 65 78 70 72 65  ext of the expre
65ea0 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 38 20  ssion */.    u8 
65eb0 73 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 20  sortOrder;      
65ec0 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53      /* 1 for DES
65ed0 43 20 6f 72 20 30 20 66 6f 72 20 41 53 43 20 2a  C or 0 for ASC *
65ee0 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65 3b 20 20  /.    u8 done;  
65ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65f00 41 20 66 6c 61 67 20 74 6f 20 69 6e 64 69 63 61  A flag to indica
65f10 74 65 20 77 68 65 6e 20 70 72 6f 63 65 73 73 69  te when processi
65f20 6e 67 20 69 73 20 66 69 6e 69 73 68 65 64 20 2a  ng is finished *
65f30 2f 0a 20 20 20 20 75 31 36 20 69 43 6f 6c 3b 20  /.    u16 iCol; 
65f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
65f50 46 6f 72 20 4f 52 44 45 52 20 42 59 2c 20 63 6f  For ORDER BY, co
65f60 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 72  lumn number in r
65f70 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20  esult set */.   
65f80 20 75 31 36 20 69 41 6c 69 61 73 3b 20 20 20 20   u16 iAlias;    
65f90 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
65fa0 20 69 6e 74 6f 20 50 61 72 73 65 2e 61 41 6c 69   into Parse.aAli
65fb0 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d 65 20 2a  as[] for zName *
65fc0 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20 20 20 20  /.  } *a;       
65fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
65fe0 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
65ff0 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d   expression */.}
66000 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
66010 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
66020 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 62  ucture is used b
66030 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  y the parser to 
66040 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a 2a 20 74  record both.** t
66050 68 65 20 70 61 72 73 65 20 74 72 65 65 20 66 6f  he parse tree fo
66060 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
66070 61 6e 64 20 74 68 65 20 73 70 61 6e 20 6f 66 20  and the span of 
66080 69 6e 70 75 74 20 74 65 78 74 20 66 6f 72 20 61  input text for a
66090 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e  n.** expression.
660a0 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 53  .*/.struct ExprS
660b0 70 61 6e 20 7b 0a 20 20 45 78 70 72 20 2a 70 45  pan {.  Expr *pE
660c0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  xpr;          /*
660d0 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
660e0 70 61 72 73 65 20 74 72 65 65 20 2a 2f 0a 20 20  parse tree */.  
660f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
66100 72 74 3b 20 20 20 2f 2a 20 46 69 72 73 74 20 63  rt;   /* First c
66110 68 61 72 61 63 74 65 72 20 6f 66 20 69 6e 70 75  haracter of inpu
66120 74 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  t text */.  cons
66130 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b 20 20 20  t char *zEnd;   
66140 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74    /* One charact
66150 65 72 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  er past the end 
66160 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f  of input text */
66170 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
66180 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
66190 74 72 75 63 74 75 72 65 20 63 61 6e 20 68 6f 6c  tructure can hol
661a0 64 20 61 20 73 69 6d 70 6c 65 20 6c 69 73 74 20  d a simple list 
661b0 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 2c 0a  of identifiers,.
661c0 2a 2a 20 73 75 63 68 20 61 73 20 74 68 65 20 6c  ** such as the l
661d0 69 73 74 20 22 61 2c 62 2c 63 22 20 69 6e 20 74  ist "a,b,c" in t
661e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61  he following sta
661f0 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  tements:.**.**  
66200 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
66210 74 28 61 2c 62 2c 63 29 20 56 41 4c 55 45 53 20  t(a,b,c) VALUES 
66220 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45  ...;.**      CRE
66230 41 54 45 20 49 4e 44 45 58 20 69 64 78 20 4f 4e  ATE INDEX idx ON
66240 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a 20 20 20   t(a,b,c);.**   
66250 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45     CREATE TRIGGE
66260 52 20 74 72 69 67 20 42 45 46 4f 52 45 20 55 50  R trig BEFORE UP
66270 44 41 54 45 20 4f 4e 20 74 28 61 2c 62 2c 63 29  DATE ON t(a,b,c)
66280 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20   ...;.**.** The 
66290 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 66 69 65  IdList.a.idx fie
662a0 6c 64 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  ld is used when 
662b0 74 68 65 20 49 64 4c 69 73 74 20 72 65 70 72 65  the IdList repre
662c0 73 65 6e 74 73 20 74 68 65 20 6c 69 73 74 20 6f  sents the list o
662d0 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  f.** column name
662e0 73 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 20  s after a table 
662f0 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e 53 45 52  name in an INSER
66300 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e  T statement.  In
66310 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   the statement.*
66320 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20  *.**     INSERT 
66330 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 2e 2e  INTO t(a,b,c) ..
66340 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61 22 20 69  ..**.** If "a" i
66350 73 20 74 68 65 20 6b 2d 74 68 20 63 6f 6c 75 6d  s the k-th colum
66360 6e 20 6f 66 20 74 61 62 6c 65 20 22 74 22 2c 20  n of table "t", 
66370 74 68 65 6e 20 49 64 4c 69 73 74 2e 61 5b 30 5d  then IdList.a[0]
66380 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73 74 72 75  .idx==k..*/.stru
66390 63 74 20 49 64 4c 69 73 74 20 7b 0a 20 20 73 74  ct IdList {.  st
663a0 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d  ruct IdList_item
663b0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61   {.    char *zNa
663c0 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  me;      /* Name
663d0 20 6f 66 20 74 68 65 20 69 64 65 6e 74 69 66 69   of the identifi
663e0 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 64  er */.    int id
663f0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  x;          /* I
66400 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20 54 61 62  ndex in some Tab
66410 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20 61 20 63  le.aCol[] of a c
66420 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a 4e 61 6d  olumn named zNam
66430 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a 20 20 69  e */.  } *a;.  i
66440 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20  nt nId;         
66450 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 64 65  /* Number of ide
66460 6e 74 69 66 69 65 72 73 20 6f 6e 20 74 68 65 20  ntifiers on the 
66470 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  list */.  int nA
66480 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75  lloc;      /* Nu
66490 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
664a0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 5b  allocated for a[
664b0 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f  ] below */.};../
664c0 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d 61 73 6b  *.** The bitmask
664d0 20 64 61 74 61 74 79 70 65 20 64 65 66 69 6e 65   datatype define
664e0 64 20 62 65 6c 6f 77 20 69 73 20 75 73 65 64 20  d below is used 
664f0 66 6f 72 20 76 61 72 69 6f 75 73 20 6f 70 74 69  for various opti
66500 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  mizations..**.**
66510 20 43 68 61 6e 67 69 6e 67 20 74 68 69 73 20 66   Changing this f
66520 72 6f 6d 20 61 20 36 34 2d 62 69 74 20 74 6f 20  rom a 64-bit to 
66530 61 20 33 32 2d 62 69 74 20 74 79 70 65 20 6c 69  a 32-bit type li
66540 6d 69 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20  mits the number 
66550 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20  of.** tables in 
66560 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20 69 6e 73  a join to 32 ins
66570 74 65 61 64 20 6f 66 20 36 34 2e 20 20 42 75 74  tead of 64.  But
66580 20 69 74 20 61 6c 73 6f 20 72 65 64 75 63 65 73   it also reduces
66590 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 6f 66 20   the size.** of 
665a0 74 68 65 20 6c 69 62 72 61 72 79 20 62 79 20 37  the library by 7
665b0 33 38 20 62 79 74 65 73 20 6f 6e 20 69 78 38 36  38 bytes on ix86
665c0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 36 34  ..*/.typedef u64
665d0 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a 0a 2a 2a   Bitmask;../*.**
665e0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   The number of b
665f0 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b  its in a Bitmask
66600 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e 73 20 22  .  "BMS" means "
66610 42 69 74 4d 61 73 6b 20 53 69 7a 65 22 2e 0a 2a  BitMask Size"..*
66620 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53 20 20 28  /.#define BMS  (
66630 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 42 69 74  (int)(sizeof(Bit
66640 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a 0a 2a 2a  mask)*8))../*.**
66650 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
66660 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62  tructure describ
66670 65 73 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  es the FROM clau
66680 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
66690 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 45 61 63  tatement..** Eac
666a0 68 20 74 61 62 6c 65 20 6f 72 20 73 75 62 71 75  h table or subqu
666b0 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ery in the FROM 
666c0 63 6c 61 75 73 65 20 69 73 20 61 20 73 65 70 61  clause is a sepa
666d0 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 6f 66 0a  rate element of.
666e0 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61  ** the SrcList.a
666f0 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  [] array..**.** 
66700 57 69 74 68 20 74 68 65 20 61 64 64 69 74 69 6f  With the additio
66710 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65 20 64 61  n of multiple da
66720 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 2c 20  tabase support, 
66730 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
66740 72 75 63 74 75 72 65 0a 2a 2a 20 63 61 6e 20 61  ructure.** can a
66750 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f 20 64  lso be used to d
66760 65 73 63 72 69 62 65 20 61 20 70 61 72 74 69 63  escribe a partic
66770 75 6c 61 72 20 74 61 62 6c 65 20 73 75 63 68 20  ular table such 
66780 61 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  as the table tha
66790 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66 69 65 64  t.** is modified
667a0 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44   by an INSERT, D
667b0 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45  ELETE, or UPDATE
667c0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20   statement.  In 
667d0 73 74 61 6e 64 61 72 64 20 53 51 4c 2c 0a 2a 2a  standard SQL,.**
667e0 20 73 75 63 68 20 61 20 74 61 62 6c 65 20 6d 75   such a table mu
667f0 73 74 20 62 65 20 61 20 73 69 6d 70 6c 65 20 6e  st be a simple n
66800 61 6d 65 3a 20 49 44 2e 20 20 42 75 74 20 69 6e  ame: ID.  But in
66810 20 53 51 4c 69 74 65 2c 20 74 68 65 20 74 61 62   SQLite, the tab
66820 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77 20 62 65  le can.** now be
66830 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 61   identified by a
66840 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20   database name, 
66850 61 20 64 6f 74 2c 20 74 68 65 6e 20 74 68 65 20  a dot, then the 
66860 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 49  table name: ID.I
66870 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 69  D..**.** The joi
66880 6e 74 79 70 65 20 73 74 61 72 74 73 20 6f 75 74  ntype starts out
66890 20 73 68 6f 77 69 6e 67 20 74 68 65 20 6a 6f 69   showing the joi
668a0 6e 20 74 79 70 65 20 62 65 74 77 65 65 6e 20 74  n type between t
668b0 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
668c0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 65 78 74  .** and the next
668d0 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 69   table on the li
668e0 73 74 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  st.  The parser 
668f0 62 75 69 6c 64 73 20 74 68 65 20 6c 69 73 74 20  builds the list 
66900 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20 42 75 74  this way..** But
66910 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53   sqlite3SrcListS
66920 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 29 20 6c  hiftJoinType() l
66930 61 74 65 72 20 73 68 69 66 74 73 20 74 68 65 20  ater shifts the 
66940 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20 74 68 61  jointypes so tha
66950 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69 6e 74 79  t each.** jointy
66960 70 65 20 65 78 70 72 65 73 73 65 73 20 74 68 65  pe expresses the
66970 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68   join between th
66980 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20  e table and the 
66990 70 72 65 76 69 6f 75 73 20 74 61 62 6c 65 2e 0a  previous table..
669a0 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 4c 69 73  */.struct SrcLis
669b0 74 20 7b 0a 20 20 69 31 36 20 6e 53 72 63 3b 20  t {.  i16 nSrc; 
669c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
669d0 20 6f 66 20 74 61 62 6c 65 73 20 6f 72 20 73 75   of tables or su
669e0 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 20  bqueries in the 
669f0 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
66a00 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20 20 20 20   i16 nAlloc;    
66a10 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
66a20 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64  ntries allocated
66a30 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f   in a[] below */
66a40 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
66a50 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61  t_item {.    cha
66a60 72 20 2a 7a 44 61 74 61 62 61 73 65 3b 20 20 2f  r *zDatabase;  /
66a70 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
66a80 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  se holding this 
66a90 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61  table */.    cha
66aa0 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f  r *zName;      /
66ab0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
66ac0 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  ble */.    char 
66ad0 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20 2f 2a 20  *zAlias;     /* 
66ae0 54 68 65 20 22 42 22 20 70 61 72 74 20 6f 66 20  The "B" part of 
66af0 61 20 22 41 20 41 53 20 42 22 20 70 68 72 61 73  a "A AS B" phras
66b00 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20 74 68 65  e.  zName is the
66b10 20 22 41 22 20 2a 2f 0a 20 20 20 20 54 61 62 6c   "A" */.    Tabl
66b20 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 2f 2a  e *pTab;      /*
66b30 20 41 6e 20 53 51 4c 20 74 61 62 6c 65 20 63 6f   An SQL table co
66b40 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a  rresponding to z
66b50 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53 65 6c 65  Name */.    Sele
66b60 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 2f 2a  ct *pSelect;  /*
66b70 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   A SELECT statem
66b80 65 6e 74 20 75 73 65 64 20 69 6e 20 70 6c 61 63  ent used in plac
66b90 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d  e of a table nam
66ba0 65 20 2a 2f 0a 20 20 20 20 75 38 20 69 73 50 6f  e */.    u8 isPo
66bb0 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a 20 54 65  pulated;   /* Te
66bc0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 61 73  mporary table as
66bd0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 45  sociated with SE
66be0 4c 45 43 54 20 69 73 20 70 6f 70 75 6c 61 74 65  LECT is populate
66bf0 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a 6f 69 6e  d */.    u8 join
66c00 74 79 70 65 3b 20 20 20 20 20 20 2f 2a 20 54 79  type;      /* Ty
66c10 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65 74 77 65  pe of join betwe
66c20 65 6e 20 74 68 69 73 20 61 62 6c 65 20 61 6e 64  en this able and
66c30 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 2a 2f   the previous */
66c40 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e 64 65 78  .    u8 notIndex
66c50 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ed;    /* True i
66c60 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54  f there is a NOT
66c70 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20   INDEXED clause 
66c80 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 72 73  */.    int iCurs
66c90 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or;      /* The 
66ca0 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
66cb0 65 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  er used to acces
66cc0 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  s this table */.
66cd0 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b 20 20      Expr *pOn;  
66ce0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20        /* The ON 
66cf0 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
66d00 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74 20 2a   */.    IdList *
66d10 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54 68 65  pUsing;   /* The
66d20 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
66d30 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 42   a join */.    B
66d40 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64 3b 20  itmask colUsed; 
66d50 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c 4e 29   /* Bit N (1<<N)
66d60 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e 20 4e   set if column N
66d70 20 6f 66 20 70 54 61 62 20 69 73 20 75 73 65 64   of pTab is used
66d80 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 49   */.    char *zI
66d90 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 49 64 65  ndex;     /* Ide
66da0 6e 74 69 66 69 65 72 20 66 72 6f 6d 20 22 49 4e  ntifier from "IN
66db0 44 45 58 45 44 20 42 59 20 3c 7a 49 6e 64 65 78  DEXED BY <zIndex
66dc0 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  >" clause */.   
66dd0 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20   Index *pIndex; 
66de0 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75     /* Index stru
66df0 63 74 75 72 65 20 63 6f 72 72 65 73 70 6f 6e 64  cture correspond
66e00 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78 2c 20 69  ing to zIndex, i
66e10 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20 61 5b 31  f any */.  } a[1
66e20 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ];             /
66e30 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
66e40 65 61 63 68 20 69 64 65 6e 74 69 66 69 65 72 20  each identifier 
66e50 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 7d  on the list */.}
66e60 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d 69 74 74  ;../*.** Permitt
66e70 65 64 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  ed values of the
66e80 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74   SrcList.a.joint
66e90 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a 23 64 65  ype field.*/.#de
66ea0 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 20 20 20  fine JT_INNER   
66eb0 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 41    0x0001    /* A
66ec0 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e 6e 65 72  ny kind of inner
66ed0 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 6e 20 2a   or cross join *
66ee0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 43 52 4f  /.#define JT_CRO
66ef0 53 53 20 20 20 20 20 30 78 30 30 30 32 20 20 20  SS     0x0002   
66f00 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 75 73 65   /* Explicit use
66f10 20 6f 66 20 74 68 65 20 43 52 4f 53 53 20 6b 65   of the CROSS ke
66f20 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65  yword */.#define
66f30 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20 30 78   JT_NATURAL   0x
66f40 30 30 30 34 20 20 20 20 2f 2a 20 54 72 75 65 20  0004    /* True 
66f50 66 6f 72 20 61 20 22 6e 61 74 75 72 61 6c 22 20  for a "natural" 
66f60 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  join */.#define 
66f70 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 30 78 30  JT_LEFT      0x0
66f80 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f  008    /* Left o
66f90 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65  uter join */.#de
66fa0 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 20 20 20  fine JT_RIGHT   
66fb0 20 20 30 78 30 30 31 30 20 20 20 20 2f 2a 20 52    0x0010    /* R
66fc0 69 67 68 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20  ight outer join 
66fd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4f 55  */.#define JT_OU
66fe0 54 45 52 20 20 20 20 20 30 78 30 30 32 30 20 20  TER     0x0020  
66ff0 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 45 52 22    /* The "OUTER"
67000 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
67010 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  ent */.#define J
67020 54 5f 45 52 52 4f 52 20 20 20 20 20 30 78 30 30  T_ERROR     0x00
67030 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e 6f 77 6e  40    /* unknown
67040 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
67050 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f  join type */.../
67060 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50 6c 61 6e  *.** A WherePlan
67070 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 69 6e   object holds in
67080 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64  formation that d
67090 65 73 63 72 69 62 65 73 20 61 20 6c 6f 6f 6b 75  escribes a looku
670a0 70 0a 2a 2a 20 73 74 72 61 74 65 67 79 2e 0a 2a  p.** strategy..*
670b0 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74  *.** This object
670c0 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
670d0 62 65 20 6f 70 61 71 75 65 20 6f 75 74 73 69 64  be opaque outsid
670e0 65 20 6f 66 20 74 68 65 20 77 68 65 72 65 2e 63  e of the where.c
670f0 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49 74 20 69   module..** It i
67100 73 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20  s included here 
67110 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20 74 68 61  only so that tha
67120 74 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20  t compiler will 
67130 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 69 74 0a  know how big it.
67140 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 6f 66 20  ** is.  None of 
67150 74 68 65 20 66 69 65 6c 64 73 20 69 6e 20 74 68  the fields in th
67160 69 73 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64  is object should
67170 20 62 65 20 75 73 65 64 20 6f 75 74 73 69 64 65   be used outside
67180 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68 65 72 65   of.** the where
67190 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a  .c module..**.**
671a0 20 57 69 74 68 69 6e 20 74 68 65 20 75 6e 69 6f   Within the unio
671b0 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e 6c 79 20  n, pIdx is only 
671c0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67  used when wsFlag
671d0 73 26 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  s&WHERE_INDEXED 
671e0 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70 54 65 72  is true..** pTer
671f0 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  m is only used w
67200 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52  hen wsFlags&WHER
67210 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73 20 74 72  E_MULTI_OR is tr
67220 75 65 2e 20 20 41 6e 64 20 70 56 74 61 62 49 64  ue.  And pVtabId
67230 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65  x.** is only use
67240 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57  d when wsFlags&W
67250 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c  HERE_VIRTUALTABL
67260 45 20 69 73 20 74 72 75 65 2e 20 20 49 74 20 69  E is true.  It i
67270 73 20 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 63  s never the.** c
67280 61 73 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68  ase that more th
67290 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  an one of these 
672a0 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 74 72  conditions is tr
672b0 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68  ue..*/.struct Wh
672c0 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75 33 32 20  erePlan {.  u32 
672d0 77 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  wsFlags;        
672e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48             /* WH
672f0 45 52 45 5f 2a 20 66 6c 61 67 73 20 74 68 61 74  ERE_* flags that
67300 20 64 65 73 63 72 69 62 65 20 74 68 65 20 73 74   describe the st
67310 72 61 74 65 67 79 20 2a 2f 0a 20 20 75 33 32 20  rategy */.  u32 
67320 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  nEq;            
67330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
67340 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f 6e 73 74  mber of == const
67350 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75 6e 69 6f  raints */.  unio
67360 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  n {.    Index *p
67370 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
67380 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
67390 77 68 65 6e 20 57 48 45 52 45 5f 49 4e 44 45 58  when WHERE_INDEX
673a0 45 44 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20  ED is true */.  
673b0 20 20 73 74 72 75 63 74 20 57 68 65 72 65 54 65    struct WhereTe
673c0 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
673d0 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   /* WHERE clause
673e0 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d 73 65 61   term for OR-sea
673f0 72 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  rch */.    sqlit
67400 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
67410 56 74 61 62 49 64 78 3b 20 20 2f 2a 20 56 69 72  VtabIdx;  /* Vir
67420 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78  tual table index
67430 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 7d 20 75   to use */.  } u
67440 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ;.};../*.** For 
67450 65 61 63 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70  each nested loop
67460 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75   in a WHERE clau
67470 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  se implementatio
67480 6e 2c 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f  n, the WhereInfo
67490 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f  .** structure co
674a0 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20  ntains a single 
674b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
674c0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69   structure.  Thi
674d0 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69  s structure.** i
674e0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
674f0 20 70 72 69 76 61 74 65 20 74 68 65 20 74 68 65   private the the
67500 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 20   where.c module 
67510 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
67520 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f 72 20 6d  e.** access or m
67530 6f 64 69 66 69 65 64 20 62 79 20 6f 74 68 65 72  odified by other
67540 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20   modules..**.** 
67550 54 68 65 20 70 49 64 78 49 6e 66 6f 20 66 69 65  The pIdxInfo fie
67560 6c 64 20 69 73 20 75 73 65 64 20 74 6f 20 68 65  ld is used to he
67570 6c 70 20 70 69 63 6b 20 74 68 65 20 62 65 73 74  lp pick the best
67580 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a 2a 20 76   index on a.** v
67590 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
675a0 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e  he pIdxInfo poin
675b0 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 69 6e 64  ter contains ind
675c0 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61  exing.** informa
675d0 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74  tion for the i-t
675e0 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  h table in the F
675f0 52 4f 4d 20 63 6c 61 75 73 65 20 62 65 66 6f 72  ROM clause befor
67600 65 20 72 65 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a  e reordering..**
67610 20 41 6c 6c 20 74 68 65 20 70 49 64 78 49 6e 66   All the pIdxInf
67620 6f 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 66  o pointers are f
67630 72 65 65 64 20 62 79 20 77 68 65 72 65 49 6e 66  reed by whereInf
67640 6f 46 72 65 65 28 29 20 69 6e 20 77 68 65 72 65  oFree() in where
67650 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  .c..** All other
67660 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
67670 74 68 65 20 69 2d 74 68 20 57 68 65 72 65 4c 65  the i-th WhereLe
67680 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  vel object for t
67690 68 65 20 69 2d 74 68 20 74 61 62 6c 65 0a 2a 2a  he i-th table.**
676a0 20 61 66 74 65 72 20 46 52 4f 4d 20 63 6c 61 75   after FROM clau
676b0 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a  se ordering..*/.
676c0 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 65  struct WhereLeve
676d0 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20  l {.  WherePlan 
676e0 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f 2a 20 71  plan;       /* q
676f0 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 74 68  uery plan for th
67700 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  is element of th
67710 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
67720 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e  .  int iLeftJoin
67730 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f  ;        /* Memo
67740 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20  ry cell used to 
67750 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f  implement LEFT O
67760 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69  UTER JOIN */.  i
67770 6e 74 20 69 54 61 62 43 75 72 3b 20 20 20 20 20  nt iTabCur;     
67780 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
67790 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20   cursor used to 
677a0 61 63 63 65 73 73 20 74 68 65 20 74 61 62 6c 65  access the table
677b0 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75   */.  int iIdxCu
677c0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r;          /* T
677d0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75  he VDBE cursor u
677e0 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 70 49  sed to access pI
677f0 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  dx */.  int addr
67800 42 72 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Brk;          /*
67810 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72   Jump here to br
67820 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  eak out of the l
67830 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  oop */.  int add
67840 72 4e 78 74 3b 20 20 20 20 20 20 20 20 20 20 2f  rNxt;          /
67850 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73  * Jump here to s
67860 74 61 72 74 20 74 68 65 20 6e 65 78 74 20 49 4e  tart the next IN
67870 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a   combination */.
67880 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20    int addrCont; 
67890 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
678a0 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  here to continue
678b0 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 6c   with the next l
678c0 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69  oop cycle */.  i
678d0 6e 74 20 61 64 64 72 46 69 72 73 74 3b 20 20 20  nt addrFirst;   
678e0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 69 6e       /* First in
678f0 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 69 6e 74  struction of int
67900 65 72 69 6f 72 20 6f 66 20 74 68 65 20 6c 6f 6f  erior of the loo
67910 70 20 2a 2f 0a 20 20 75 38 20 69 46 72 6f 6d 3b  p */.  u8 iFrom;
67920 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67930 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 74  Which entry in t
67940 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
67950 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35 3b 20 20  /.  u8 op, p5;  
67960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
67970 6f 64 65 20 61 6e 64 20 50 35 20 6f 66 20 74 68  ode and P5 of th
67980 65 20 6f 70 63 6f 64 65 20 74 68 61 74 20 65 6e  e opcode that en
67990 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  ds the loop */. 
679a0 20 69 6e 74 20 70 31 2c 20 70 32 3b 20 20 20 20   int p1, p2;    
679b0 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 6e         /* Operan
679c0 64 73 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ds of the opcode
679d0 20 75 73 65 64 20 74 6f 20 65 6e 64 73 20 74 68   used to ends th
679e0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f  e loop */.  unio
679f0 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  n {             
67a00 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
67a10 20 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e   that depends on
67a20 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 2a 2f   plan.wsFlags */
67a30 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
67a40 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20      int nIn;    
67a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
67a60 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
67a70 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20  n aInLoop[] */. 
67a80 20 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f       struct InLo
67a90 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  op {.        int
67aa0 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20   iCur;          
67ab0 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
67ac0 63 75 72 73 6f 72 20 75 73 65 64 20 62 79 20 74  cursor used by t
67ad0 68 69 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  his IN operator 
67ae0 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61  */.        int a
67af0 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 20 20 20  ddrInTop;       
67b00 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20    /* Top of the 
67b10 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20  IN loop */.     
67b20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20 20 20 20   } *aInLoop;    
67b30 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d         /* Inform
67b40 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68  ation about each
67b50 20 6e 65 73 74 65 64 20 49 4e 20 6f 70 65 72 61   nested IN opera
67b60 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b  tor */.    } in;
67b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67b80 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 6c   /* Used when pl
67b90 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45  an.wsFlags&WHERE
67ba0 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 20 7d 20  _IN_ABLE */.  } 
67bb0 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  u;..  /* The fol
67bc0 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73 20  lowing field is 
67bd0 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74 20  really not part 
67be0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  of the current l
67bf0 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a 2a 20  evel.  But.  ** 
67c00 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65 20  we need a place 
67c10 74 6f 20 63 61 63 68 65 20 76 69 72 74 75 61 6c  to cache virtual
67c20 20 74 61 62 6c 65 20 69 6e 64 65 78 20 69 6e 66   table index inf
67c30 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61 63  ormation for eac
67c40 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74  h.  ** virtual t
67c50 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
67c60 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20   clause and the 
67c70 57 68 65 72 65 4c 65 76 65 6c 20 73 74 72 75 63  WhereLevel struc
67c80 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 61 20 63  ture is.  ** a c
67c90 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 20  onvenient place 
67ca0 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6f  since there is o
67cb0 6e 65 20 57 68 65 72 65 4c 65 76 65 6c 20 66 6f  ne WhereLevel fo
67cc0 72 20 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 75  r each FROM clau
67cd0 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e  se.  ** element.
67ce0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
67cf0 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78  index_info *pIdx
67d00 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 65 78 20  Info;  /* Index 
67d10 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 20 73 6f  info for n-th so
67d20 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b  urce table */.};
67d30 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 61 70  ../*.** Flags ap
67d40 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
67d50 65 20 77 63 74 72 6c 46 6c 61 67 73 20 70 61 72  e wctrlFlags par
67d60 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65  ameter of sqlite
67d70 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a  3WhereBegin().**
67d80 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 49 6e   and the WhereIn
67d90 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 20 6d 65  fo.wctrlFlags me
67da0 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mber..*/.#define
67db0 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
67dc0 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 30 20 2f  ORMAL   0x0000 /
67dd0 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 65 66 69  * No-op */.#defi
67de0 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  ne WHERE_ORDERBY
67df0 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 30 30 31  _MIN      0x0001
67e00 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 72 6f   /* ORDER BY pro
67e10 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 69 6e 28  cessing for min(
67e20 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 69 6e  ) func */.#defin
67e30 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  e WHERE_ORDERBY_
67e40 4d 41 58 20 20 20 20 20 20 30 78 30 30 30 32 20  MAX      0x0002 
67e50 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 72 6f 63  /* ORDER BY proc
67e60 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 78 28 29  essing for max()
67e70 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 69 6e 65   func */.#define
67e80 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44   WHERE_ONEPASS_D
67e90 45 53 49 52 45 44 20 20 30 78 30 30 30 34 20 2f  ESIRED  0x0004 /
67ea0 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 6f 6e 65  * Want to do one
67eb0 2d 70 61 73 73 20 55 50 44 41 54 45 2f 44 45 4c  -pass UPDATE/DEL
67ec0 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ETE */.#define W
67ed0 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
67ee0 4f 4b 20 20 20 20 30 78 30 30 30 38 20 2f 2a 20  OK    0x0008 /* 
67ef0 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 61 20 72  Ok to return a r
67f00 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ow more than onc
67f10 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  e */.#define WHE
67f20 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 20 20 20  RE_OMIT_OPEN    
67f30 20 20 20 20 30 78 30 30 31 30 20 2f 2a 20 54 61      0x0010 /* Ta
67f40 62 6c 65 20 63 75 72 73 6f 72 20 61 72 65 20 61  ble cursor are a
67f50 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a 2f 0a 23  lready open */.#
67f60 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49  define WHERE_OMI
67f70 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 30 78  T_CLOSE       0x
67f80 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20 63 6c 6f  0020 /* Omit clo
67f90 73 65 20 6f 66 20 74 61 62 6c 65 20 26 20 69 6e  se of table & in
67fa0 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 23  dex cursors */.#
67fb0 64 65 66 69 6e 65 20 57 48 45 52 45 5f 46 4f 52  define WHERE_FOR
67fc0 43 45 5f 54 41 42 4c 45 20 20 20 20 20 20 30 78  CE_TABLE      0x
67fd0 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f 74 20 75  0040 /* Do not u
67fe0 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f 6e 6c 79  se an index-only
67ff0 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a   search */../*.*
68000 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
68010 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f  se processing ro
68020 75 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61  utine has two ha
68030 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69  lves.  The.** fi
68040 72 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68  rst part does th
68050 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57  e start of the W
68060 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68  HERE loop and th
68070 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66  e second.** half
68080 20 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f   does the tail o
68090 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
680a0 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  .  An instance o
680b0 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  f.** this struct
680c0 75 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ure is returned 
680d0 62 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c  by the first hal
680e0 66 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20  f and passed.** 
680f0 69 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20  into the second 
68100 68 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d  half to give som
68110 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f  e continuity..*/
68120 0a 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66  .struct WhereInf
68130 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  o {.  Parse *pPa
68140 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 50 61  rse;       /* Pa
68150 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
68160 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
68170 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c  t */.  u16 wctrl
68180 46 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 46  Flags;      /* F
68190 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20  lags originally 
681a0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
681b0 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f  3WhereBegin() */
681c0 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61 73 73 3b  .  u8 okOnePass;
681d0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b 20 74 6f          /* Ok to
681e0 20 75 73 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c   use one-pass al
681f0 67 6f 72 69 74 68 6d 20 66 6f 72 20 55 50 44 41  gorithm for UPDA
68200 54 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a  TE or DELETE */.
68210 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
68220 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
68230 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
68240 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a  es in the join *
68250 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20  /.  int iTop;   
68260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68270 20 20 20 2f 2a 20 54 68 65 20 76 65 72 79 20 62     /* The very b
68280 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
68290 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20  WHERE loop */.  
682a0 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 20 20  int iContinue;  
682b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
682c0 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
682d0 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78  ontinue with nex
682e0 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  t record */.  in
682f0 74 20 69 42 72 65 61 6b 3b 20 20 20 20 20 20 20  t iBreak;       
68300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68310 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
68320 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f  ak out of the lo
68330 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76  op */.  int nLev
68340 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  el;             
68350 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
68360 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20   of nested loop 
68370 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 68 65 72  */.  struct Wher
68380 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20  eClause *pWC;   
68390 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69      /* Decomposi
683a0 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52  tion of the WHER
683b0 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  E clause */.  Wh
683c0 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20  ereLevel a[1];  
683d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
683e0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
683f0 74 20 65 61 63 68 20 6e 65 73 74 20 6c 6f 6f 70  t each nest loop
68400 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a   in WHERE */.};.
68410 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e  ./*.** A NameCon
68420 74 65 78 74 20 64 65 66 69 6e 65 73 20 61 20 63  text defines a c
68430 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20  ontext in which 
68440 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65  to resolve table
68450 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e   and column.** n
68460 61 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65  ames.  The conte
68470 78 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  xt consists of a
68480 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20   list of tables 
68490 28 74 68 65 20 70 53 72 63 4c 69 73 74 29 20 66  (the pSrcList) f
684a0 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 20 6c 69  ield and.** a li
684b0 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72  st of named expr
684c0 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 74 29 2e  ession (pEList).
684d0 20 20 54 68 65 20 6e 61 6d 65 64 20 65 78 70 72    The named expr
684e0 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61 79 0a  ession list may.
684f0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 54 68 65  ** be NULL.  The
68500 20 70 53 72 63 20 63 6f 72 72 65 73 70 6f 6e 64   pSrc correspond
68510 73 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c  s to the FROM cl
68520 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
68530 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61   or.** to the ta
68540 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 72 61 74  ble being operat
68550 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 52 54 2c  ed on by INSERT,
68560 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
68570 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 45 4c 69  TE.  The.** pELi
68580 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  st corresponds t
68590 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  o the result set
685a0 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64   of a SELECT and
685b0 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a 2a 2a 20   is NULL for.** 
685c0 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73  other statements
685d0 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74  ..**.** NameCont
685e0 65 78 74 73 20 63 61 6e 20 62 65 20 6e 65 73 74  exts can be nest
685f0 65 64 2e 20 20 57 68 65 6e 20 72 65 73 6f 6c 76  ed.  When resolv
68600 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 65 20 69  ing names, the i
68610 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a 20 63 6f  nner-most .** co
68620 6e 74 65 78 74 20 69 73 20 73 65 61 72 63 68 65  ntext is searche
68630 64 20 66 69 72 73 74 2e 20 20 49 66 20 6e 6f 20  d first.  If no 
68640 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20  match is found, 
68650 74 68 65 20 6e 65 78 74 20 6f 75 74 65 72 0a 2a  the next outer.*
68660 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 63 68 65  * context is che
68670 63 6b 65 64 2e 20 20 49 66 20 74 68 65 72 65 20  cked.  If there 
68680 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d 61 74 63  is still no matc
68690 68 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 74  h, the next cont
686a0 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 63 6b 65  ext.** is checke
686b0 64 2e 20 20 54 68 69 73 20 70 72 6f 63 65 73 73  d.  This process
686c0 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e 74 69 6c   continues until
686d0 20 65 69 74 68 65 72 20 61 20 6d 61 74 63 68 20   either a match 
686e0 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f 72 20 61  is found.** or a
686f0 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 72 65 20  ll contexts are 
68700 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 61 20 6d  check.  When a m
68710 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 74  atch is found, t
68720 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 72 20 6f  he nRef member o
68730 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 78 74  f.** the context
68740 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
68750 6d 61 74 63 68 20 69 73 20 69 6e 63 72 65 6d 65  match is increme
68760 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63  nted. .**.** Eac
68770 68 20 73 75 62 71 75 65 72 79 20 67 65 74 73 20  h subquery gets 
68780 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74 65 78  a new NameContex
68790 74 2e 20 20 54 68 65 20 70 4e 65 78 74 20 66 69  t.  The pNext fi
687a0 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  eld points to th
687b0 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74  e.** NameContext
687c0 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 71   in the parent q
687d0 75 65 72 79 2e 20 20 54 68 75 73 20 74 68 65 20  uery.  Thus the 
687e0 70 72 6f 63 65 73 73 20 6f 66 20 73 63 61 6e 6e  process of scann
687f0 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43  ing the.** NameC
68800 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 6f 72 72  ontext list corr
68810 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 61 72 63  esponds to searc
68820 68 69 6e 67 20 74 68 72 6f 75 67 68 20 73 75 63  hing through suc
68830 63 65 73 73 69 76 65 6c 79 20 6f 75 74 65 72 0a  cessively outer.
68840 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 6c 6f  ** subqueries lo
68850 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61 74 63  oking for a matc
68860 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4e 61 6d  h..*/.struct Nam
68870 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 50 61 72  eContext {.  Par
68880 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
68890 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
688a0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
688b0 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 4f 6e 65  rcList;   /* One
688c0 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c 65 73 20   or more tables 
688d0 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20  used to resolve 
688e0 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  names */.  ExprL
688f0 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
68900 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69 73 74  /* Optional list
68910 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 65 73   of named expres
68920 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  sions */.  int n
68930 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
68940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 61 6d  /* Number of nam
68950 65 73 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74  es resolved by t
68960 68 69 73 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  his context */. 
68970 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20   int nErr;      
68980 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
68990 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  of errors encoun
689a0 74 65 72 65 64 20 77 68 69 6c 65 20 72 65 73 6f  tered while reso
689b0 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a 2f 0a 20  lving names */. 
689c0 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b 20 20 20   u8 allowAgg;   
689d0 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61        /* Aggrega
689e0 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c  te functions all
689f0 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 75  owed here */.  u
68a00 38 20 68 61 73 41 67 67 3b 20 20 20 20 20 20 20  8 hasAgg;       
68a10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
68a20 67 67 72 65 67 61 74 65 73 20 61 72 65 20 73 65  ggregates are se
68a30 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 43 68 65  en */.  u8 isChe
68a40 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ck;          /* 
68a50 54 72 75 65 20 69 66 20 72 65 73 6f 6c 76 69 6e  True if resolvin
68a60 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 48 45  g names in a CHE
68a70 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  CK constraint */
68a80 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 3b 20 20  .  int nDepth;  
68a90 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68          /* Depth
68aa0 20 6f 66 20 73 75 62 71 75 65 72 79 20 72 65 63   of subquery rec
68ab0 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f  ursion. 1 for no
68ac0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 20 20   recursion */.  
68ad0 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
68ae0 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74  o;   /* Informat
68af0 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 72 65 67  ion about aggreg
68b00 61 74 65 73 20 61 74 20 74 68 69 73 20 6c 65 76  ates at this lev
68b10 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  el */.  NameCont
68b20 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20  ext *pNext;  /* 
68b30 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 6d 65 20  Next outer name 
68b40 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c 20 66  context.  NULL f
68b50 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a 2f 0a  or outermost */.
68b60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
68b70 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
68b80 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
68b90 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 69 6e   contains all in
68ba0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6e 65 65  formation.** nee
68bb0 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  ded to generate 
68bc0 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c  code for a singl
68bd0 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
68be0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 6d 69 74  nt..**.** nLimit
68bf0 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66   is set to -1 if
68c00 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4c 49 4d   there is no LIM
68c10 49 54 20 63 6c 61 75 73 65 2e 20 20 6e 4f 66 66  IT clause.  nOff
68c20 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 30 2e  set is set to 0.
68c30 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
68c40 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20  a LIMIT clause, 
68c50 74 68 65 20 70 61 72 73 65 72 20 73 65 74 73 20  the parser sets 
68c60 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 20 76 61  nLimit to the va
68c70 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69  lue of the.** li
68c80 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20  mit and nOffset 
68c90 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
68ca0 74 68 65 20 6f 66 66 73 65 74 20 28 6f 72 20 30  the offset (or 0
68cb0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   if there is not
68cc0 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 20 42 75  .** offset).  Bu
68cd0 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e 4c 69 6d  t later on, nLim
68ce0 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20 62  it and nOffset b
68cf0 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d 6f 72 79  ecome the memory
68d00 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 69 6e   locations.** in
68d10 20 74 68 65 20 56 44 42 45 20 74 68 61 74 20 72   the VDBE that r
68d20 65 63 6f 72 64 20 74 68 65 20 6c 69 6d 69 74 20  ecord the limit 
68d30 61 6e 64 20 6f 66 66 73 65 74 20 63 6f 75 6e 74  and offset count
68d40 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 64 72 4f  ers..**.** addrO
68d50 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 72 69 65  penEphm[] entrie
68d60 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 61 64  s contain the ad
68d70 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e  dress of OP_Open
68d80 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65  Ephemeral opcode
68d90 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 64 64 72  s..** These addr
68da0 65 73 73 65 73 20 6d 75 73 74 20 62 65 20 73 74  esses must be st
68db0 6f 72 65 64 20 73 6f 20 74 68 61 74 20 77 65 20  ored so that we 
68dc0 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 6e 64 20  can go back and 
68dd0 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 65 20 50  fill in.** the P
68de0 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 20 50 32  4_KEYINFO and P2
68df0 20 70 61 72 61 6d 65 74 65 72 73 20 6c 61 74 65   parameters late
68e00 72 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20  r.  Neither the 
68e10 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a 2a 20 74  KeyInfo nor.** t
68e20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
68e30 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e 20 62  umns in P2 can b
68e40 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20 74 68  e computed at th
68e50 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a 2a 20 61  e same time.** a
68e60 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68  s the OP_OpenEph
68e70 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  m instruction is
68e80 20 63 6f 64 65 64 20 62 65 63 61 75 73 65 20 6e   coded because n
68e90 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 69 6e 66  ot.** enough inf
68ea0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
68eb0 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
68ec0 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 74 68  y is known at th
68ed0 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65  at point..** The
68ee0 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64   KeyInfo for add
68ef0 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61 6e 64  rOpenTran[0] and
68f00 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f   [1] contains co
68f10 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
68f20 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 65 73  s.** for the res
68f30 75 6c 74 20 73 65 74 2e 20 20 54 68 65 20 4b 65  ult set.  The Ke
68f40 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70  yInfo for addrOp
68f50 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e 74 61 69  enTran[2] contai
68f60 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  ns collating.** 
68f70 73 65 71 75 65 6e 63 65 73 20 66 6f 72 20 74 68  sequences for th
68f80 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
68f90 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 6c  e..*/.struct Sel
68fa0 65 63 74 20 7b 0a 20 20 45 78 70 72 4c 69 73 74  ect {.  ExprList
68fb0 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f   *pEList;      /
68fc0 2a 20 54 68 65 20 66 69 65 6c 64 73 20 6f 66 20  * The fields of 
68fd0 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  the result */.  
68fe0 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  u8 op;          
68ff0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
69000 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b 5f 41 4c  : TK_UNION TK_AL
69010 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 54  L TK_INTERSECT T
69020 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20 63 68  K_EXCEPT */.  ch
69030 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20  ar affinity;    
69040 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f       /* MakeReco
69050 72 64 20 77 69 74 68 20 74 68 69 73 20 61 66 66  rd with this aff
69060 69 6e 69 74 79 20 66 6f 72 20 53 52 54 5f 53 65  inity for SRT_Se
69070 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c 46 6c  t */.  u16 selFl
69080 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ags;          /*
69090 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20 76 61   Various SF_* va
690a0 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73  lues */.  SrcLis
690b0 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20  t *pSrc;        
690c0 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
690d0 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  use */.  Expr *p
690e0 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20  Where;          
690f0 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
69100 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
69110 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
69120 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20  /* The GROUP BY 
69130 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
69140 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 20 20   *pHaving;      
69150 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49 4e 47     /* The HAVING
69160 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
69170 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
69180 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
69190 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
691a0 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20  Select *pPrior; 
691b0 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f 72 20         /* Prior 
691c0 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70  select in a comp
691d0 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74  ound select stat
691e0 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ement */.  Selec
691f0 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  t *pNext;       
69200 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c 65 63 74    /* Next select
69210 20 74 6f 20 74 68 65 20 6c 65 66 74 20 69 6e 20   to the left in 
69220 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20  a compound */.  
69230 53 65 6c 65 63 74 20 2a 70 52 69 67 68 74 6d 6f  Select *pRightmo
69240 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 2d  st;    /* Right-
69250 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 61  most select in a
69260 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
69270 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
69280 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20 20  Expr *pLimit;   
69290 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20         /* LIMIT 
692a0 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c  expression. NULL
692b0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e   means not used.
692c0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66   */.  Expr *pOff
692d0 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  set;         /* 
692e0 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 69 6f  OFFSET expressio
692f0 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f  n. NULL means no
69300 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 69 6e 74  t used. */.  int
69310 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74   iLimit, iOffset
69320 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65  ;   /* Memory re
69330 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20  gisters holding 
69340 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 54 20 63  LIMIT & OFFSET c
69350 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
69360 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 33 5d   addrOpenEphm[3]
69370 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70  ;   /* OP_OpenEp
69380 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 65 6c 61  hem opcodes rela
69390 74 65 64 20 74 6f 20 74 68 69 73 20 73 65 6c 65  ted to this sele
693a0 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ct */.};../*.** 
693b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
693c0 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46 6c 61  or Select.selFla
693d0 67 73 2e 20 20 54 68 65 20 22 53 46 22 20 70 72  gs.  The "SF" pr
693e0 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f 72 0a  efix stands for.
693f0 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61 67 22  ** "Select Flag"
69400 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f  ..*/.#define SF_
69410 44 69 73 74 69 6e 63 74 20 20 20 20 20 20 20 20  Distinct        
69420 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74 70 75  0x0001  /* Outpu
69430 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49 53 54  t should be DIST
69440 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20  INCT */.#define 
69450 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20 20 20  SF_Resolved     
69460 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 49 64     0x0002  /* Id
69470 65 6e 74 69 66 69 65 72 73 20 68 61 76 65 20 62  entifiers have b
69480 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a 2f 0a  een resolved */.
69490 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67 72 65  #define SF_Aggre
694a0 67 61 74 65 20 20 20 20 20 20 20 30 78 30 30 30  gate       0x000
694b0 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 61  4  /* Contains a
694c0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
694d0 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  ns */.#define SF
694e0 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20 20  _UsesEphemeral  
694f0 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73 65 73   0x0008  /* Uses
69500 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 72   the OpenEphemer
69510 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 64 65  al opcode */.#de
69520 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64 65 64  fine SF_Expanded
69530 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20          0x0010  
69540 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  /* sqlite3Select
69550 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65 64 20  Expand() called 
69560 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65 66 69  on this */.#defi
69570 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66  ne SF_HasTypeInf
69580 6f 20 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a  o     0x0020  /*
69590 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69 65 73   FROM subqueries
695a0 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65 74 61   have Table meta
695b0 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  data */.../*.** 
695c0 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61  The results of a
695d0 20 73 65 6c 65 63 74 20 63 61 6e 20 62 65 20 64   select can be d
695e0 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 73 65  istributed in se
695f0 76 65 72 61 6c 20 77 61 79 73 2e 20 20 54 68 65  veral ways.  The
69600 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 66 69 78  .** "SRT" prefix
69610 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 54 20 52   means "SELECT R
69620 65 73 75 6c 74 20 54 79 70 65 22 2e 0a 2a 2f 0a  esult Type"..*/.
69630 23 64 65 66 69 6e 65 20 53 52 54 5f 55 6e 69 6f  #define SRT_Unio
69640 6e 20 20 20 20 20 20 20 20 31 20 20 2f 2a 20 53  n        1  /* S
69650 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 20 6b  tore result as k
69660 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20  eys in an index 
69670 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45  */.#define SRT_E
69680 78 63 65 70 74 20 20 20 20 20 20 20 32 20 20 2f  xcept       2  /
69690 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 20  * Remove result 
696a0 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 69 6e 64  from a UNION ind
696b0 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
696c0 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 20 33  T_Exists       3
696d0 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 69 66 20    /* Store 1 if 
696e0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f  the result is no
696f0 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69  t empty */.#defi
69700 6e 65 20 53 52 54 5f 44 69 73 63 61 72 64 20 20  ne SRT_Discard  
69710 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 6e 6f 74      4  /* Do not
69720 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74   save the result
69730 73 20 61 6e 79 77 68 65 72 65 20 2a 2f 0a 0a 2f  s anywhere */../
69740 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
69750 6c 61 75 73 65 20 69 73 20 69 67 6e 6f 72 65 64  lause is ignored
69760 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20   for all of the 
69770 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 69 6e 65  above */.#define
69780 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62   IgnorableOrderb
69790 79 28 58 29 20 28 28 58 2d 3e 65 44 65 73 74 29  y(X) ((X->eDest)
697a0 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 29 0a 0a  <=SRT_Discard)..
697b0 23 64 65 66 69 6e 65 20 53 52 54 5f 4f 75 74 70  #define SRT_Outp
697c0 75 74 20 20 20 20 20 20 20 35 20 20 2f 2a 20 4f  ut       5  /* O
697d0 75 74 70 75 74 20 65 61 63 68 20 72 6f 77 20 6f  utput each row o
697e0 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 64 65 66  f result */.#def
697f0 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 20 20 20  ine SRT_Mem     
69800 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6f 72 65       6  /* Store
69810 20 72 65 73 75 6c 74 20 69 6e 20 61 20 6d 65 6d   result in a mem
69820 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 64 65 66  ory cell */.#def
69830 69 6e 65 20 53 52 54 5f 53 65 74 20 20 20 20 20  ine SRT_Set     
69840 20 20 20 20 20 37 20 20 2f 2a 20 53 74 6f 72 65       7  /* Store
69850 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65 79 73   results as keys
69860 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a   in an index */.
69870 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61 62 6c  #define SRT_Tabl
69880 65 20 20 20 20 20 20 20 20 38 20 20 2f 2a 20 53  e        8  /* S
69890 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 20 64  tore result as d
698a0 61 74 61 20 77 69 74 68 20 61 6e 20 61 75 74 6f  ata with an auto
698b0 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f 0a 23  matic rowid */.#
698c0 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68 65 6d  define SRT_Ephem
698d0 54 61 62 20 20 20 20 20 39 20 20 2f 2a 20 43 72  Tab     9  /* Cr
698e0 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74 20 74  eate transient t
698f0 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c 69 6b  ab and store lik
69900 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f 0a 23  e SRT_Table */.#
69910 64 65 66 69 6e 65 20 53 52 54 5f 43 6f 72 6f 75  define SRT_Corou
69920 74 69 6e 65 20 20 20 31 30 20 20 2f 2a 20 47 65  tine   10  /* Ge
69930 6e 65 72 61 74 65 20 61 20 73 69 6e 67 6c 65 20  nerate a single 
69940 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f  row of result */
69950 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74  ../*.** A struct
69960 75 72 65 20 75 73 65 64 20 74 6f 20 63 75 73 74  ure used to cust
69970 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 61 76 69  omize the behavi
69980 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 53 65 6c  or of sqlite3Sel
69990 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a 20 63 6f  ect(). See.** co
699a0 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 73 71 6c  mments above sql
699b0 69 74 65 33 53 65 6c 65 63 74 28 29 20 66 6f 72  ite3Select() for
699c0 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 74 79 70   details..*/.typ
699d0 65 64 65 66 20 73 74 72 75 63 74 20 53 65 6c 65  edef struct Sele
699e0 63 74 44 65 73 74 20 53 65 6c 65 63 74 44 65 73  ctDest SelectDes
699f0 74 3b 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74  t;.struct Select
69a00 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44 65 73  Dest {.  u8 eDes
69a10 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  t;         /* Ho
69a20 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20  w to dispose of 
69a30 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20  the results */. 
69a40 20 75 38 20 61 66 66 69 6e 69 74 79 3b 20 20 20   u8 affinity;   
69a50 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 75     /* Affinity u
69a60 73 65 64 20 77 68 65 6e 20 65 44 65 73 74 3d 3d  sed when eDest==
69a70 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 69 6e 74  SRT_Set */.  int
69a80 20 69 50 61 72 6d 3b 20 20 20 20 20 20 20 20 2f   iParm;        /
69a90 2a 20 41 20 70 61 72 61 6d 65 74 65 72 20 75 73  * A parameter us
69aa0 65 64 20 62 79 20 74 68 65 20 65 44 65 73 74 20  ed by the eDest 
69ab0 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20  disposal method 
69ac0 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20  */.  int iMem;  
69ad0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
69ae0 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 65  egister where re
69af0 73 75 6c 74 73 20 61 72 65 20 77 72 69 74 74 65  sults are writte
69b00 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b  n */.  int nMem;
69b10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
69b20 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
69b30 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 7d 3b 0a  allocated */.};.
69b40 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 63 6f  ./*.** During co
69b50 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66  de generation of
69b60 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74   statements that
69b70 20 64 6f 20 69 6e 73 65 72 74 73 20 69 6e 74 6f   do inserts into
69b80 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 0a   AUTOINCREMENT .
69b90 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68 65 20 66  ** tables, the f
69ba0 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61  ollowing informa
69bb0 74 69 6f 6e 20 69 73 20 61 74 74 61 63 68 65 64  tion is attached
69bc0 20 74 6f 20 74 68 65 20 54 61 62 6c 65 2e 75 2e   to the Table.u.
69bd0 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20 70 6f 69  autoInc.p.** poi
69be0 6e 74 65 72 20 6f 66 20 65 61 63 68 20 61 75 74  nter of each aut
69bf0 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65  oincrement table
69c00 20 74 6f 20 72 65 63 6f 72 64 20 73 6f 6d 65 20   to record some 
69c10 73 69 64 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  side information
69c20 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 6f 64   that.** the cod
69c30 65 20 67 65 6e 65 72 61 74 6f 72 20 6e 65 65 64  e generator need
69c40 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 6b  s.  We have to k
69c50 65 65 70 20 70 65 72 2d 74 61 62 6c 65 20 61 75  eep per-table au
69c60 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 69  toincrement.** i
69c70 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 63 61  nformation in ca
69c80 73 65 20 69 6e 73 65 72 74 73 20 61 72 65 20 64  se inserts are d
69c90 6f 77 6e 20 77 69 74 68 69 6e 20 74 72 69 67 67  own within trigg
69ca0 65 72 73 2e 20 20 54 72 69 67 67 65 72 73 20 64  ers.  Triggers d
69cb0 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c  o not.** normall
69cc0 79 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 68 65  y coordinate the
69cd0 69 72 20 61 63 74 69 76 69 74 69 65 73 2c 20 62  ir activities, b
69ce0 75 74 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f  ut we do need to
69cf0 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 68 65 0a   coordinate the.
69d00 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e 64 20 73  ** loading and s
69d10 61 76 69 6e 67 20 6f 66 20 61 75 74 6f 69 6e 63  aving of autoinc
69d20 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d 61 74 69  rement informati
69d30 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75  on..*/.struct Au
69d40 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20 20 41 75  toincInfo {.  Au
69d50 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e 65 78 74  toincInfo *pNext
69d60 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 66 6f  ;   /* Next info
69d70 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c 69 73 74   block in a list
69d80 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a   of them all */.
69d90 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
69da0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
69db0 20 74 68 69 73 20 69 6e 66 6f 20 62 6c 6f 63 6b   this info block
69dc0 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 20 20   refers to */.  
69dd0 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
69de0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
69df0 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20  n sqlite3.aDb[] 
69e00 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64  of database hold
69e10 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e  ing pTab */.  in
69e20 74 20 72 65 67 43 74 72 3b 20 20 20 20 20 20 20  t regCtr;       
69e30 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65      /* Memory re
69e40 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74  gister holding t
69e50 68 65 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72  he rowid counter
69e60 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69   */.};../*.** Si
69e70 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ze of the column
69e80 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 6e 64 65   cache.*/.#ifnde
69e90 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  f SQLITE_N_COLCA
69ea0 43 48 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  CHE.# define SQL
69eb0 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 20 31  ITE_N_COLCACHE 1
69ec0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
69ed0 41 74 20 6c 65 61 73 74 20 6f 6e 65 20 69 6e 73  At least one ins
69ee0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
69ef0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
69f00 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
69f10 65 61 63 68 20 0a 2a 2a 20 74 72 69 67 67 65 72  each .** trigger
69f20 20 74 68 61 74 20 6d 61 79 20 62 65 20 66 69 72   that may be fir
69f30 65 64 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67  ed while parsing
69f40 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41   an INSERT, UPDA
69f50 54 45 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  TE or DELETE.** 
69f60 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c 6c 20 73  statement. All s
69f70 75 63 68 20 6f 62 6a 65 63 74 73 20 61 72 65 20  uch objects are 
69f80 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6c 69  stored in the li
69f90 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64  nked list headed
69fa0 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e 70 54 72   at.** Parse.pTr
69fb0 69 67 67 65 72 50 72 67 20 61 6e 64 20 64 65 6c  iggerPrg and del
69fc0 65 74 65 64 20 6f 6e 63 65 20 73 74 61 74 65 6d  eted once statem
69fd0 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ent compilation 
69fe0 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63 6f 6d 70  has been.** comp
69ff0 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 56  leted..**.** A V
6a000 64 62 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  dbe sub-program 
6a010 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
6a020 74 68 65 20 62 6f 64 79 20 61 6e 64 20 57 48 45  the body and WHE
6a030 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 72 69 67  N clause of trig
6a040 67 65 72 0a 2a 2a 20 54 72 69 67 67 65 72 50 72  ger.** TriggerPr
6a050 67 2e 70 54 72 69 67 67 65 72 2c 20 61 73 73 75  g.pTrigger, assu
6a060 6d 69 6e 67 20 61 20 64 65 66 61 75 6c 74 20 4f  ming a default O
6a070 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73  N CONFLICT claus
6a080 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67 65 72 50  e of.** TriggerP
6a090 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73 20 73 74  rg.orconf, is st
6a0a0 6f 72 65 64 20 69 6e 20 74 68 65 20 54 72 69 67  ored in the Trig
6a0b0 67 65 72 50 72 67 2e 70 50 72 6f 67 72 61 6d 20  gerPrg.pProgram 
6a0c0 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20 54 68 65  variable..** The
6a0d0 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50   Parse.pTriggerP
6a0e0 72 67 20 6c 69 73 74 20 6e 65 76 65 72 20 63 6f  rg list never co
6a0f0 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69  ntains two entri
6a100 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65  es with the same
6a110 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f 72 20 62  .** values for b
6a120 6f 74 68 20 70 54 72 69 67 67 65 72 20 61 6e 64  oth pTrigger and
6a130 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a 2a 20 54   orconf..**.** T
6a140 68 65 20 54 72 69 67 67 65 72 50 72 67 2e 61 43  he TriggerPrg.aC
6a150 6f 6c 6d 61 73 6b 5b 30 5d 20 76 61 72 69 61 62  olmask[0] variab
6a160 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d  le is set to a m
6a170 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c  ask of old.* col
6a180 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64  umns.** accessed
6a190 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f   (or set to 0 fo
6a1a0 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64  r triggers fired
6a1b0 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
6a1c0 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65  INSERT .** state
6a1d0 6d 65 6e 74 73 29 2e 20 53 69 6d 69 6c 61 72 6c  ments). Similarl
6a1e0 79 2c 20 74 68 65 20 54 72 69 67 67 65 72 50 72  y, the TriggerPr
6a1f0 67 2e 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20 76 61  g.aColmask[1] va
6a200 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
6a210 0a 2a 2a 20 61 20 6d 61 73 6b 20 6f 66 20 6e 65  .** a mask of ne
6a220 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64  w.* columns used
6a230 20 62 79 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   by the program.
6a240 0a 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67  .*/.struct Trigg
6a250 65 72 50 72 67 20 7b 0a 20 20 54 72 69 67 67 65  erPrg {.  Trigge
6a260 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20  r *pTrigger;    
6a270 20 20 2f 2a 20 54 72 69 67 67 65 72 20 74 68 69    /* Trigger thi
6a280 73 20 70 72 6f 67 72 61 6d 20 77 61 73 20 63 6f  s program was co
6a290 64 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  ded from */.  in
6a2a0 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20  t orconf;       
6a2b0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
6a2c0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
6a2d0 69 63 79 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67  icy */.  SubProg
6a2e0 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20  ram *pProgram;  
6a2f0 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c   /* Program impl
6a300 65 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65  ementing pTrigge
6a310 72 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 75 33  r/orconf */.  u3
6a320 32 20 61 43 6f 6c 6d 61 73 6b 5b 32 5d 3b 20 20  2 aColmask[2];  
6a330 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 73 20 6f        /* Masks o
6a340 66 20 6f 6c 64 2e 2a 2c 20 6e 65 77 2e 2a 20 63  f old.*, new.* c
6a350 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20  olumns accessed 
6a360 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  */.  TriggerPrg 
6a370 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20  *pNext;      /* 
6a380 4e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61  Next entry in Pa
6a390 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20  rse.pTriggerPrg 
6a3a0 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  list */.};../*.*
6a3b0 2a 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20  * An SQL parser 
6a3c0 63 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79  context.  A copy
6a3d0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
6a3e0 72 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72  re is passed thr
6a3f0 6f 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73  ough.** the pars
6a400 65 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f  er and down into
6a410 20 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20   all the parser 
6a420 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69  action routine i
6a430 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61  n order to.** ca
6a440 72 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72  rry around infor
6a450 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67  mation that is g
6a460 6c 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74  lobal to the ent
6a470 69 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a  ire parse..**.**
6a480 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69   The structure i
6a490 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74  s divided into t
6a4a0 77 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20  wo parts.  When 
6a4b0 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63  the parser and c
6a4c0 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20  ode.** generate 
6a4d0 63 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20  call themselves 
6a4e0 72 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65  recursively, the
6a4f0 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74   first part of t
6a500 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  he structure.** 
6a510 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20  is constant but 
6a520 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20  the second part 
6a530 69 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20  is reset at the 
6a540 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e  beginning and en
6a550 64 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63  d of.** each rec
6a560 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ursion..**.** Th
6a570 65 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64  e nTableLock and
6a580 20 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69   aTableLock vari
6a590 61 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75  ables are only u
6a5a0 73 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65  sed if the share
6a5b0 64 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74  d-cache .** feat
6a5c0 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28  ure is enabled (
6a5d0 69 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d  if sqlite3Tsd()-
6a5e0 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69  >useSharedData i
6a5f0 73 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72  s true). They ar
6a600 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f  e.** used to sto
6a610 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61  re the set of ta
6a620 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72  ble-locks requir
6a630 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
6a640 65 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d  ent being.** com
6a650 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20  piled. Function 
6a660 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
6a670 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64  () is used to ad
6a680 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  d entries to the
6a690 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72  .** list..*/.str
6a6a0 75 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71  uct Parse {.  sq
6a6b0 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
6a6c0 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64     /* The main d
6a6d0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
6a6e0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
6a6f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
6a700 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
6a710 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63  execution */.  c
6a720 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20  har *zErrMsg;   
6a730 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20      /* An error 
6a740 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62  message */.  Vdb
6a750 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
6a760 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66    /* An engine f
6a770 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74  or executing dat
6a780 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a  abase bytecode *
6a790 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53  /.  u8 colNamesS
6a7a0 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45  et;      /* TRUE
6a7b0 20 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e   after OP_Column
6a7c0 4e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73  Name has been is
6a7d0 73 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f  sued to pVdbe */
6a7e0 0a 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b  .  u8 nameClash;
6a7f0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72          /* A per
6a800 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d  manent table nam
6a810 65 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74  e clashes with t
6a820 65 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a  emp table name *
6a830 2f 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65  /.  u8 checkSche
6a840 6d 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73  ma;      /* Caus
6a850 65 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  es schema cookie
6a860 20 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20   check after an 
6a870 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65  error */.  u8 ne
6a880 73 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  sted;           
6a890 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
6a8a0 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65  ted calls to the
6a8b0 20 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e   parser/code gen
6a8c0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70  erator */.  u8 p
6a8d0 61 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20  arseError;      
6a8e0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61   /* True after a
6a8f0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20   parsing error. 
6a900 20 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f   Ticket #1794 */
6a910 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20  .  u8 nTempReg; 
6a920 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6a930 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72  r of temporary r
6a940 65 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d  egisters in aTem
6a950 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e  pReg[] */.  u8 n
6a960 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20  TempInUse;      
6a970 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54   /* Number of aT
6a980 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74  empReg[] current
6a990 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a  ly checked out *
6a9a0 2f 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67  /.  int aTempReg
6a9b0 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64  [8];     /* Hold
6a9c0 69 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d  ing area for tem
6a9d0 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73  porary registers
6a9e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65   */.  int nRange
6a9f0 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69  Reg;       /* Si
6aa00 7a 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72  ze of the tempor
6aa10 61 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f  ary register blo
6aa20 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e  ck */.  int iRan
6aa30 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20  geReg;       /* 
6aa40 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  First register i
6aa50 6e 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  n temporary regi
6aa60 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20  ster block */.  
6aa70 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20  int nErr;       
6aa80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6aa90 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f  f errors seen */
6aaa0 0a 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20  .  int nTab;    
6aab0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6aac0 72 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20  r of previously 
6aad0 61 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63  allocated VDBE c
6aae0 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  ursors */.  int 
6aaf0 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  nMem;           
6ab00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
6ab10 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20  mory cells used 
6ab20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
6ab30 6e 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  nSet;           
6ab40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65   /* Number of se
6ab50 74 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a  ts used so far *
6ab60 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20  /.  int ckBase; 
6ab70 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
6ab80 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74   register of dat
6ab90 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63  a during check c
6aba0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
6abb0 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b  int iCacheLevel;
6abc0 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65       /* ColCache
6abd0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c   valid when aCol
6abe0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d  Cache[].iLevel<=
6abf0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20  iCacheLevel */. 
6ac00 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20   int iCacheCnt; 
6ac10 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
6ac20 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
6ac30 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72  e aColCache[].lr
6ac40 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38  u values */.  u8
6ac50 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20   nColCache;     
6ac60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6ac70 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63  entries in the c
6ac80 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20  olumn cache */. 
6ac90 20 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20   u8 iColCache;  
6aca0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e        /* Next en
6acb0 74 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65  try of the cache
6acc0 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20   to replace */. 
6acd0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
6ace0 65 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62  e {.    int iTab
6acf0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  le;           /*
6ad00 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75   Table cursor nu
6ad10 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  mber */.    int 
6ad20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
6ad30 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d    /* Table colum
6ad40 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  n number */.    
6ad50 75 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20  u8 affChange;   
6ad60 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6ad70 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68   this register h
6ad80 61 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69  as had an affini
6ad90 74 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20  ty change */.   
6ada0 20 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20   u8 tempReg;    
6adb0 20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69         /* iReg i
6adc0 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65  s a temp registe
6add0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  r that needs to 
6ade0 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
6adf0 69 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20  int iLevel;     
6ae00 20 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67        /* Nesting
6ae10 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e   level */.    in
6ae20 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20  t iReg;         
6ae30 20 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20      /* Reg with 
6ae40 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f  value of this co
6ae50 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f  lumn. 0 means no
6ae60 6e 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  ne. */.    int l
6ae70 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ru;             
6ae80 20 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74   /* Least recent
6ae90 6c 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61  ly used entry ha
6aea0 73 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76  s the smallest v
6aeb0 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c  alue */.  } aCol
6aec0 43 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43  Cache[SQLITE_N_C
6aed0 4f 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e  OLCACHE];  /* On
6aee0 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
6aef0 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f  n cache entry */
6af00 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b  .  u32 writeMask
6af10 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  ;       /* Start
6af20 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63   a write transac
6af30 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61  tion on these da
6af40 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32  tabases */.  u32
6af50 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20   cookieMask;    
6af60 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20    /* Bitmask of 
6af70 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20  schema verified 
6af80 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75  databases */.  u
6af90 38 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20  8 isMultiWrite; 
6afa0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73      /* True if s
6afb0 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66  tatement may aff
6afc0 65 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69  ect/insert multi
6afd0 70 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38  ple rows */.  u8
6afe0 20 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20   mayAbort;      
6aff0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74     /* True if st
6b000 61 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f  atement may thro
6b010 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70  w an ABORT excep
6b020 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  tion */.  int co
6b030 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f  okieGoto;      /
6b040 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  * Address of OP_
6b050 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76  Goto to cookie v
6b060 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69  erifier subrouti
6b070 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  ne */.  int cook
6b080 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d  ieValue[SQLITE_M
6b090 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20  AX_ATTACHED+2]; 
6b0a0 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f   /* Values of co
6b0b0 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20  okies to verify 
6b0c0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
6b0d0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
6b0e0 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65  CHE.  int nTable
6b0f0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
6b100 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  Number of locks 
6b110 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f  in aTableLock */
6b120 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54  .  TableLock *aT
6b130 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71  ableLock; /* Req
6b140 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b  uired table lock
6b150 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63  s for shared-cac
6b160 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69  he mode */.#endi
6b170 66 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  f.  int regRowid
6b180 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
6b190 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77  ster holding row
6b1a0 69 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42  id of CREATE TAB
6b1b0 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e  LE entry */.  in
6b1c0 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20  t regRoot;      
6b1d0 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
6b1e0 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65  olding root page
6b1f0 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20   number for new 
6b200 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74  objects */.  Aut
6b210 6f 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b  oincInfo *pAinc;
6b220 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
6b230 20 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45   about AUTOINCRE
6b240 4d 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f  MENT counters */
6b250 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20  .  int nMaxArg; 
6b260 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61          /* Max a
6b270 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73  rgs passed to us
6b280 65 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73  er function by s
6b290 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20  ub-program */.. 
6b2a0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
6b2b0 75 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e  used while codin
6b2c0 67 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  g trigger progra
6b2d0 6d 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  ms. */.  Parse *
6b2e0 70 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a  pToplevel;    /*
6b2f0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65   Parse structure
6b300 20 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61   for main progra
6b310 6d 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20  m (or NULL) */. 
6b320 20 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72   Table *pTrigger
6b330 54 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74  Tab;  /* Table t
6b340 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e  riggers are bein
6b350 67 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20  g coded for */. 
6b360 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20   u32 oldmask;   
6b370 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
6b380 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72   old.* columns r
6b390 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75  eferenced */.  u
6b3a0 33 32 20 6e 65 77 6d 61 73 6b 3b 20 20 20 20 20  32 newmask;     
6b3b0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6e      /* Mask of n
6b3c0 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66  ew.* columns ref
6b3d0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20  erenced */.  u8 
6b3e0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20  eTriggerOp;     
6b3f0 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20    /* TK_UPDATE, 
6b400 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f  TK_INSERT or TK_
6b410 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65  DELETE */.  u8 e
6b420 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20  Orconf;         
6b430 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43   /* Default ON C
6b440 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66  ONFLICT policy f
6b450 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73  or trigger steps
6b460 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c 65   */.  u8 disable
6b470 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 72  Triggers;  /* Tr
6b480 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72  ue to disable tr
6b490 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20  iggers */..  /* 
6b4a0 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e  Above is constan
6b4b0 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73  t between recurs
6b4c0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20  ions.  Below is 
6b4d0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64  reset before and
6b4e0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68   after.  ** each
6b4f0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20   recursion */.. 
6b500 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20   int nVar;      
6b510 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6b520 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73  of '?' variables
6b530 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c   seen in the SQL
6b540 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74   so far */.  int
6b550 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20   nVarExpr;      
6b560 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
6b570 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56  sed slots in apV
6b580 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e  arExpr[] */.  in
6b590 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b  t nVarExprAlloc;
6b5a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6b5b0 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20  allocated slots 
6b5c0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a  in apVarExpr[] *
6b5d0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72  /.  Expr **apVar
6b5e0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e  Expr;    /* Poin
6b5f0 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64  ters to :aaa and
6b600 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20   $aaaa wildcard 
6b610 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20  expressions */. 
6b620 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61 72   Vdbe *pReprepar
6b630 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e  e;    /* VM bein
6b640 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73 71  g reprepared (sq
6b650 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29  lite3Reprepare()
6b660 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61  ) */.  int nAlia
6b670 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  s;          /* N
6b680 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64  umber of aliased
6b690 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75   result set colu
6b6a0 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  mns */.  int nAl
6b6b0 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a  iasAlloc;     /*
6b6c0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63   Number of alloc
6b6d0 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61  ated slots for a
6b6e0 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Alias[] */.  int
6b6f0 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20   *aAlias;       
6b700 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73    /* Register us
6b710 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73  ed to hold alias
6b720 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75  ed result */.  u
6b730 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20  8 explain;      
6b740 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
6b750 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20  he EXPLAIN flag 
6b760 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20  is found on the 
6b770 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e  query */.  Token
6b780 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20   sNameToken;    
6b790 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e  /* Token with un
6b7a0 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d 61  qualified schema
6b7b0 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a   object name */.
6b7c0 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b    Token sLastTok
6b7d0 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 61  en;    /* The la
6b7e0 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 20  st token parsed 
6b7f0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
6b800 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c  *zTail;   /* All
6b810 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74   SQL text past t
6b820 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f  he last semicolo
6b830 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61  n parsed */.  Ta
6b840 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20  ble *pNewTable; 
6b850 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65     /* A table be
6b860 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
6b870 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  by CREATE TABLE 
6b880 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e  */.  Trigger *pN
6b890 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f  ewTrigger;     /
6b8a0 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20  * Trigger under 
6b8b0 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43  construct by a C
6b8c0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f  REATE TRIGGER */
6b8d0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
6b8e0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20  AuthContext; /* 
6b8f0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65  The 6th paramete
6b900 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63  r to db->xAuth c
6b910 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e  allbacks */.#ifn
6b920 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6b930 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54  VIRTUALTABLE.  T
6b940 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20  oken sArg;      
6b950 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
6b960 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20  plete text of a 
6b970 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20  module argument 
6b980 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56  */.  u8 declareV
6b990 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
6b9a0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64  /* True if insid
6b9b0 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
6b9c0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e  e_vtab() */.  in
6b9d0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20  t nVtabLock;    
6b9e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6b9f0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  er of virtual ta
6ba00 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  bles to lock */.
6ba10 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62    Table **apVtab
6ba20 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
6ba30 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75  Pointer to virtu
6ba40 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e  al tables needin
6ba50 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e  g locking */.#en
6ba60 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  dif.  int nHeigh
6ba70 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
6ba80 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   Expression tree
6ba90 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65   height of curre
6baa0 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f  nt sub-select */
6bab0 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69  .  Table *pZombi
6bac0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69  eTab;      /* Li
6bad0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65  st of Table obje
6bae0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66  cts to delete af
6baf0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a  ter code gen */.
6bb00 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 54    TriggerPrg *pT
6bb10 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f 2a  riggerPrg;    /*
6bb20 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
6bb30 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20 2a  coded triggers *
6bb40 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  /.};..#ifdef SQL
6bb50 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
6bb60 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20  TABLE.  #define 
6bb70 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
6bb80 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  0.#else.  #defin
6bb90 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  e IN_DECLARE_VTA
6bba0 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61  B (pParse->decla
6bbb0 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a  reVtab).#endif..
6bbc0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
6bbd0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
6bbe0 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  ng structure can
6bbf0 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20   be declared on 
6bc00 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64  a stack and used
6bc10 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20  .** to save the 
6bc20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65  Parse.zAuthConte
6bc30 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74  xt value so that
6bc40 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f   it can be resto
6bc50 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74  red later..*/.st
6bc60 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74  ruct AuthContext
6bc70 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
6bc80 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20  *zAuthContext;  
6bc90 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 61   /* Put saved Pa
6bca0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74  rse.zAuthContext
6bcb0 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65   here */.  Parse
6bcc0 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
6bcd0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61         /* The Pa
6bce0 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  rse structure */
6bcf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69  .};../*.** Bitfi
6bd00 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 35  eld flags for P5
6bd10 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73   value in OP_Ins
6bd20 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74  ert and OP_Delet
6bd30 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  e.*/.#define OPF
6bd40 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20 20  LAG_NCHANGE     
6bd50 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65 74    0x01    /* Set
6bd60 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6e   to update db->n
6bd70 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e  Change */.#defin
6bd80 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  e OPFLAG_LASTROW
6bd90 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20 2f  ID     0x02    /
6bda0 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20  * Set to update 
6bdb0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f  db->lastRowid */
6bdc0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
6bdd0 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30 78  ISUPDATE      0x
6bde0 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50  04    /* This OP
6bdf0 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71  _Insert is an sq
6be00 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66  l UPDATE */.#def
6be10 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  ine OPFLAG_APPEN
6be20 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20 20  D        0x08   
6be30 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65   /* This is like
6be40 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
6be50 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  nd */.#define OP
6be60 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55  FLAG_USESEEKRESU
6be70 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 72  LT 0x10    /* Tr
6be80 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 65  y to avoid a see
6be90 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 74  k in BtreeInsert
6bea0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  () */.#define OP
6beb0 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 20  FLAG_CLEARCACHE 
6bec0 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43 6c     0x20    /* Cl
6bed0 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  ear pseudo-table
6bee0 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f 6c   cache in OP_Col
6bef0 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61  umn */../*. * Ea
6bf00 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 65  ch trigger prese
6bf10 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
6bf20 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f  se schema is sto
6bf30 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e  red as an instan
6bf40 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20  ce of. * struct 
6bf50 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20  Trigger. . *. * 
6bf60 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74  Pointers to inst
6bf70 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20  ances of struct 
6bf80 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f 72  Trigger are stor
6bf90 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a  ed in two ways..
6bfa0 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72   * 1. In the "tr
6bfb0 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 62  igHash" hash tab
6bfc0 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  le (part of the 
6bfd0 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65  sqlite3* that re
6bfe0 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a  presents the . *
6bff0 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 54      database). T
6c000 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67  his allows Trigg
6c010 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 6f  er structures to
6c020 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 79   be retrieved by
6c030 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c   name.. * 2. All
6c040 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
6c050 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67  ated with a sing
6c060 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20  le table form a 
6c070 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69  linked list, usi
6c080 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65  ng the. *    pNe
6c090 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72  xt member of str
6c0a0 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 70  uct Trigger. A p
6c0b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69  ointer to the fi
6c0c0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
6c0d0 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20  he. *    linked 
6c0e0 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 61  list is stored a
6c0f0 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 22  s the "pTrigger"
6c100 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61   member of the a
6c110 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20  ssociated. *    
6c120 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a  struct Table.. *
6c130 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69  . * The "step_li
6c140 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74  st" member point
6c150 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  s to the first e
6c160 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b  lement of a link
6c170 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61  ed list. * conta
6c180 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74  ining the SQL st
6c190 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69  atements specifi
6c1a0 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65  ed as the trigge
6c1b0 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73  r program.. */.s
6c1c0 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a  truct Trigger {.
6c1d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
6c1e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6c1f0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
6c200 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20  gger            
6c210 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
6c220 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20   char *table;   
6c230 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6c240 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f  table or view to
6c250 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 67   which the trigg
6c260 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20  er applies */.  
6c270 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  u8 op;          
6c280 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
6c290 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f  f TK_DELETE, TK_
6c2a0 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
6c2b0 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75  T         */.  u
6c2c0 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20  8 tr_tm;        
6c2d0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
6c2e0 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c   TRIGGER_BEFORE,
6c2f0 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a   TRIGGER_AFTER *
6c300 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b  /.  Expr *pWhen;
6c310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6c320 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f  he WHEN clause o
6c330 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
6c340 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a   (may be NULL) *
6c350 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
6c360 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49  umns;       /* I
6c370 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44  f this is an UPD
6c380 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c  ATE OF <column-l
6c390 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20  ist> trigger,.  
6c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c3b0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c             the <
6c3c0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20  column-list> is 
6c3d0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
6c3e0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
6c3f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65  ;        /* Sche
6c400 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ma containing th
6c410 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53  e trigger */.  S
6c420 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d  chema *pTabSchem
6c430 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  a;     /* Schema
6c440 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
6c450 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67  table */.  Trigg
6c460 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73  erStep *step_lis
6c470 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20  t; /* Link list 
6c480 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
6c490 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20  am steps        
6c4a0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65       */.  Trigge
6c4b0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  r *pNext;       
6c4c0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65    /* Next trigge
6c4d0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
6c4e0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  h the table */.}
6c4f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67  ;../*.** A trigg
6c500 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42  er is either a B
6c510 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45  EFORE or an AFTE
6c520 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20  R trigger.  The 
6c530 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61  following consta
6c540 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  nts.** determine
6c550 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49   which. .**.** I
6c560 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74  f there are mult
6c570 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79  iple triggers, y
6c580 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65  ou might of some
6c590 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65   BEFORE and some
6c5a0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68   AFTER..** In th
6c5b0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f  at cases, the co
6c5c0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61  nstants below ca
6c5d0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68  n be ORed togeth
6c5e0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  er..*/.#define T
6c5f0 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31  RIGGER_BEFORE  1
6c600 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52  .#define TRIGGER
6c610 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20  _AFTER   2../*. 
6c620 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6c630 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53   struct TriggerS
6c640 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73  tep is used to s
6c650 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51  tore a single SQ
6c660 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74  L statement. * t
6c670 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66  hat is a part of
6c680 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72   a trigger-progr
6c690 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61  am. . *. * Insta
6c6a0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54  nces of struct T
6c6b0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73  riggerStep are s
6c6c0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c  tored in a singl
6c6d0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c  y linked list (l
6c6e0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74  inked. * using t
6c6f0 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65  he "pNext" membe
6c700 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  r) referenced by
6c710 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22   the "step_list"
6c720 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a   member of the .
6c730 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74   * associated st
6c740 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73  ruct Trigger ins
6c750 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74  tance. The first
6c760 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
6c770 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20  linked list is. 
6c780 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  * the first step
6c790 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d   of the trigger-
6c7a0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20  program.. * . * 
6c7b0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20  The "op" member 
6c7c0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65  indicates whethe
6c7d0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c  r this is a "DEL
6c7e0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20  ETE", "INSERT", 
6c7f0 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22  "UPDATE" or. * "
6c800 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e  SELECT" statemen
6c810 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  t. The meanings 
6c820 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d  of the other mem
6c830 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e  bers is determin
6c840 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61  ed by the . * va
6c850 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66  lue of "op" as f
6c860 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f  ollows:. *. * (o
6c870 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a  p == TK_INSERT).
6c880 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20   * orconf    -> 
6c890 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f  stores the ON CO
6c8a0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d  NFLICT algorithm
6c8b0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e  . * pSelect   ->
6c8c0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
6c8d0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53  NSERT INTO ... S
6c8e0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d  ELECT ... statem
6c8f0 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20  ent, then. *    
6c900 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73            this s
6c910 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
6c920 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  to the SELECT st
6c930 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69  atement. Otherwi
6c940 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67  se NULL.. * targ
6c950 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e  et    -> A token
6c960 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f   holding the quo
6c970 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ted name of the 
6c980 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20  table to insert 
6c990 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69  into.. * pExprLi
6c9a0 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73  st -> If this is
6c9b0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20   an INSERT INTO 
6c9c0 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73  ... VALUES ... s
6c9d0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20  tatement, then. 
6c9e0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  *              t
6c9f0 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65  his stores value
6ca00 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
6ca10 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c  . Otherwise NULL
6ca20 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d  .. * pIdList   -
6ca30 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  > If this is an 
6ca40 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20  INSERT INTO ... 
6ca50 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29  (<column-names>)
6ca60 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20   VALUES ... . * 
6ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
6ca80 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69  tement, then thi
6ca90 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c  s stores the col
6caa0 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a  umn-names to be.
6cab0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6cac0 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20  inserted into.. 
6cad0 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44  *. * (op == TK_D
6cae0 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74  ELETE). * target
6caf0 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68      -> A token h
6cb00 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65  olding the quote
6cb10 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  d name of the ta
6cb20 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ble to delete fr
6cb30 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20  om.. * pWhere   
6cb40 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c   -> The WHERE cl
6cb50 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45  ause of the DELE
6cb60 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  TE statement if 
6cb70 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
6cb80 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  .. *            
6cb90 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
6cba0 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20  .. * . * (op == 
6cbb0 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61  TK_UPDATE). * ta
6cbc0 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b  rget    -> A tok
6cbd0 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71  en holding the q
6cbe0 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68  uoted name of th
6cbf0 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74  e table to updat
6cc00 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57  e rows of.. * pW
6cc10 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57  here    -> The W
6cc20 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74  HERE clause of t
6cc30 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
6cc40 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70  ent if one is sp
6cc50 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20  ecified.. *     
6cc60 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69           Otherwi
6cc70 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70  se NULL.. * pExp
6cc80 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20  rList -> A list 
6cc90 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74  of the columns t
6cca0 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65  o update and the
6ccb0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20   expressions to 
6ccc0 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20  update. *       
6ccd0 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20         them to. 
6cce0 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74  See sqlite3Updat
6ccf0 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  e() documentatio
6cd00 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a  n of "pChanges".
6cd10 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6cd20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a  argument.. * . *
6cd30 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
6cd40 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b 20  Step {.  u8 op; 
6cd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6cd60 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
6cd70 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
6cd80 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45  _INSERT, TK_SELE
6cd90 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e  CT */.  u8 orcon
6cda0 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
6cdb0 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e  OE_Rollback etc.
6cdc0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
6cdd0 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68  Trig;      /* Th
6cde0 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74  e trigger that t
6cdf0 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61  his step is a pa
6ce00 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65 63  rt of */.  Selec
6ce10 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20  t *pSelect;     
6ce20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d 65  /* SELECT statme
6ce30 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e 53  nt or RHS of INS
6ce40 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c 45  ERT INTO .. SELE
6ce50 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b 65  CT ... */.  Toke
6ce60 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20  n target;       
6ce70 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c 65   /* Target table
6ce80 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44   for DELETE, UPD
6ce90 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a 20  ATE, INSERT */. 
6cea0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
6ceb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
6cec0 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44 45  RE clause for DE
6ced0 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73  LETE or UPDATE s
6cee0 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  teps */.  ExprLi
6cef0 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f  st *pExprList; /
6cf00 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f 72  * SET clause for
6cf10 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45 53   UPDATE.  VALUES
6cf20 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53 45   clause for INSE
6cf30 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  RT */.  IdList *
6cf40 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20  pIdList;     /* 
6cf50 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  Column names for
6cf60 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72 69   INSERT */.  Tri
6cf70 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b  ggerStep *pNext;
6cf80 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65    /* Next in the
6cf90 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20   link-list */.  
6cfa0 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61  TriggerStep *pLa
6cfb0 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65  st;  /* Last ele
6cfc0 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73  ment in link-lis
6cfd0 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74  t. Valid for 1st
6cfe0 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b   elem only */.};
6cff0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
6d000 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
6d010 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
6d020 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
6d030 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a   sqliteFix....**
6d040 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65   routines as the
6d050 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65  y walk the parse
6d060 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61   tree to make da
6d070 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
6d080 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  s.** explicit.  
6d090 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
6d0a0 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78  ct DbFixer DbFix
6d0b0 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78  er;.struct DbFix
6d0c0 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
6d0d0 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  arse;      /* Th
6d0e0 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
6d0f0 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67  t.  Error messag
6d100 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  es written here 
6d110 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
6d120 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65  *zDb;    /* Make
6d130 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74   sure all object
6d140 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  s are contained 
6d150 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  in this database
6d160 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
6d170 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70   *zType;  /* Typ
6d180 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
6d190 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
6d1a0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
6d1b0 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
6d1c0 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
6d1d0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
6d1e0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
6d1f0 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a  messages */.};..
6d200 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65  /*.** An objecte
6d210 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75  d used to accumu
6d220 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  late the text of
6d230 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20   a string where 
6d240 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63  we.** do not nec
6d250 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f  essarily know ho
6d260 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67  w big the string
6d270 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20   will be in the 
6d280 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  end..*/.struct S
6d290 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69  trAccum {.  sqli
6d2a0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
6d2b0 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74   /* Optional dat
6d2c0 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73  abase for lookas
6d2d0 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c  ide.  Can be NUL
6d2e0 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61  L */.  char *zBa
6d2f0 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  se;         /* A
6d300 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   base allocation
6d310 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c  .  Not from mall
6d320 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  oc. */.  char *z
6d330 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Text;         /*
6d340 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c   The string coll
6d350 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  ected so far */.
6d360 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20    int  nChar;   
6d370 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
6d380 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73   of the string s
6d390 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20  o far */.  int  
6d3a0 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
6d3b0 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61  /* Amount of spa
6d3c0 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  ce allocated in 
6d3d0 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20  zText */.  int  
6d3e0 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  mxAlloc;        
6d3f0 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77  /* Maximum allow
6d400 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68  ed string length
6d410 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f   */.  u8   mallo
6d420 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65  cFailed;   /* Be
6d430 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e  comes true if an
6d440 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  y memory allocat
6d450 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75  ion fails */.  u
6d460 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20  8   useMalloc;  
6d470 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a      /* True if z
6d480 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61  Text is enlargea
6d490 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f  ble using reallo
6d4a0 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42  c */.  u8   tooB
6d4b0 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ig;         /* B
6d4c0 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73  ecomes true if s
6d4d0 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65  tring size excee
6d4e0 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a  ds limits */.};.
6d4f0 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  ./*.** A pointer
6d500 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
6d510 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  re is used to co
6d520 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d  mmunicate inform
6d530 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71  ation.** from sq
6d540 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50  lite3Init and OP
6d550 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74  _ParseSchema int
6d560 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69  o the sqlite3Ini
6d570 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79  tCallback..*/.ty
6d580 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20  pedef struct {. 
6d590 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
6d5a0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
6d5b0 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69  base being initi
6d5c0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
6d5d0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
6d5e0 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61  /* 0 for main da
6d5f0 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54  tabase.  1 for T
6d600 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54  EMP, 2.. for ATT
6d610 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  ACHed */.  char 
6d620 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f  **pzErrMsg;    /
6d630 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * Error message 
6d640 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
6d650 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
6d660 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63       /* Result c
6d670 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20  ode stored here 
6d680 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a  */.} InitData;..
6d690 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20  /*.** Structure 
6d6a0 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61  containing globa
6d6b0 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
6d6c0 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c  data for the SQL
6d6d0 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a  ite library..**.
6d6e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
6d6f0 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  e also contains 
6d700 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72  some state infor
6d710 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  mation..*/.struc
6d720 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
6d730 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74  {.  int bMemstat
6d740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d750 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
6d760 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73   enable memory s
6d770 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62  tatus */.  int b
6d780 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20  CoreMutex;      
6d790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d7a0 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63  True to enable c
6d7b0 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  ore mutexing */.
6d7c0 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78    int bFullMutex
6d7d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d7e0 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65      /* True to e
6d7f0 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78  nable full mutex
6d800 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53  ing */.  int mxS
6d810 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  trlen;          
6d820 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
6d830 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e  ximum string len
6d840 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c  gth */.  int szL
6d850 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20  ookaside;       
6d860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
6d870 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20  fault lookaside 
6d880 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20  buffer size */. 
6d890 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b   int nLookaside;
6d8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d8b0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f     /* Default lo
6d8c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63  okaside buffer c
6d8d0 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ount */.  sqlite
6d8e0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b  3_mem_methods m;
6d8f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
6d900 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
6d910 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72  allocation inter
6d920 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  face */.  sqlite
6d930 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
6d940 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c  mutex;      /* L
6d950 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69  ow-level mutex i
6d960 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71  nterface */.  sq
6d970 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
6d980 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20  hods pcache;    
6d990 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67  /* Low-level pag
6d9a0 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63  e-cache interfac
6d9b0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65  e */.  void *pHe
6d9c0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ap;             
6d9d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70           /* Heap
6d9e0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a   storage space *
6d9f0 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20  /.  int nHeap;  
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da10 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
6da20 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e   pHeap[] */.  in
6da30 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20  t mnReq, mxReq; 
6da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da50 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68  /* Min and max h
6da60 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a  eap requests siz
6da70 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  es */.  void *pS
6da80 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20  cratch;         
6da90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72            /* Scr
6daa0 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  atch memory */. 
6dab0 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20   int szScratch; 
6dac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dad0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61     /* Size of ea
6dae0 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65  ch scratch buffe
6daf0 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61  r */.  int nScra
6db00 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  tch;            
6db10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6db20 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75  er of scratch bu
6db30 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20  ffers */.  void 
6db40 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
6db50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6db60 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  Page cache memor
6db70 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67  y */.  int szPag
6db80 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
6db90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6dba0 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e   of each page in
6dbb0 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e   pPage[] */.  in
6dbc0 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  t nPage;        
6dbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dbe0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
6dbf0 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f  es in pPage[] */
6dc00 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53  .  int mxParserS
6dc10 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  tack;           
6dc20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20       /* maximum 
6dc30 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72  depth of the par
6dc40 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69  ser stack */.  i
6dc50 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e  nt sharedCacheEn
6dc60 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20  abled;          
6dc70 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72   /* true if shar
6dc80 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e  ed-cache mode en
6dc90 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68  abled */.  /* Th
6dca0 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65  e above might be
6dcb0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
6dcc0 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66  non-zero.  The f
6dcd0 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f  ollowing need to
6dce0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69   always.  ** ini
6dcf0 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20  tially be zero, 
6dd00 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e  however. */.  in
6dd10 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20  t isInit;       
6dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd30 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 6e  /* True after in
6dd40 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73  itialization has
6dd50 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69   finished */.  i
6dd60 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20  nt inProgress;  
6dd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd80 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69   /* True while i
6dd90 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e  nitialization in
6dda0 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69   progress */.  i
6ddb0 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b 20  nt isMutexInit; 
6ddc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ddd0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d   /* True after m
6dde0 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 69  utexes are initi
6ddf0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
6de00 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20  isMallocInit;   
6de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6de20 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c   True after mall
6de30 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  oc is initialize
6de40 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 61  d */.  int isPCa
6de50 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 20  cheInit;        
6de60 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6de70 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73   after malloc is
6de80 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6de90 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
6dea0 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20  *pInitMutex;    
6deb0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65      /* Mutex use
6dec0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  d by sqlite3_ini
6ded0 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69  tialize() */.  i
6dee0 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  nt nRefInitMutex
6def0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6df00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
6df10 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65  ers of pInitMute
6df20 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  x */.};../*.** C
6df30 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70  ontext pointer p
6df40 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75  assed down throu
6df50 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b  gh the tree-walk
6df60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b  ..*/.struct Walk
6df70 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78  er {.  int (*xEx
6df80 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b  prCallback)(Walk
6df90 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20  er*, Expr*);    
6dfa0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72   /* Callback for
6dfb0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
6dfc0 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43    int (*xSelectC
6dfd0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a  allback)(Walker*
6dfe0 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43  ,Select*);  /* C
6dff0 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45  allback for SELE
6e000 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  CTs */.  Parse *
6e010 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
6e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e030 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
6e040 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f  text.  */.  unio
6e050 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  n {             
6e060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e070 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64        /* Extra d
6e080 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b  ata for callback
6e090 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74   */.    NameCont
6e0a0 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20  ext *pNC;       
6e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e0c0 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e     /* Naming con
6e0d0 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  text */.    int 
6e0e0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
6e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e100 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
6e110 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75  r value */.  } u
6e120 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  ;.};../* Forward
6e130 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   declarations */
6e140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e150 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
6e160 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70  xpr(Walker*, Exp
6e170 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6e180 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6e190 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b  alkExprList(Walk
6e1a0 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  er*, ExprList*);
6e1b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e1c0 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
6e1d0 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53  elect(Walker*, S
6e1e0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
6e1f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e200 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
6e210 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  r(Walker*, Selec
6e220 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6e230 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6e240 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61  alkSelectFrom(Wa
6e250 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  lker*, Select*);
6e260 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63  ../*.** Return c
6e270 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ode from the par
6e280 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20  se-tree walking 
6e290 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74  primitives and t
6e2a0 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  heir.** callback
6e2b0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  s..*/.#define WR
6e2c0 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20  C_Continue    0 
6e2d0 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f    /* Continue do
6e2e0 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e  wn into children
6e2f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f   */.#define WRC_
6e300 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20  Prune       1   
6e310 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e  /* Omit children
6e320 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61   but continue wa
6e330 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a  lking siblings *
6e340 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62  /.#define WRC_Ab
6e350 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a  ort       2   /*
6e360 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65   Abandon the tre
6e370 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  e walk */../*.**
6e380 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f   Assuming zIn po
6e390 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
6e3a0 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d  t byte of a UTF-
6e3b0 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20  8 character,.** 
6e3c0 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70  advance zIn to p
6e3d0 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73  oint to the firs
6e3e0 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65  t byte of the ne
6e3f0 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  xt UTF-8 charact
6e400 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  er..*/.#define S
6e410 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
6e420 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20  zIn) {          
6e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
6e440 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29    if( (*(zIn++))
6e450 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20  >=0xc0 ){       
6e460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e470 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69         \.    whi
6e480 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30  le( (*zIn & 0xc0
6e490 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b  )==0x80 ){ zIn++
6e4a0 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  ; }             
6e4b0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
6e4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e4e0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a           \.}../*
6e4f0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43  .** The SQLITE_C
6e500 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72  ORRUPT_BKPT macr
6e510 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  o can be either 
6e520 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20  a constant (for 
6e530 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75  production.** bu
6e540 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74  ilds) or a funct
6e550 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65  ion call (for de
6e560 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74  bugging).  If it
6e570 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63   is a function c
6e580 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77  all,.** it allow
6e590 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74  s the operator t
6e5a0 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69  o set a breakpoi
6e5b0 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77  nt at the spot w
6e5c0 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  here database.**
6e5d0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66   corruption is f
6e5e0 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a  irst detected..*
6e5f0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6e600 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
6e610 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6e620 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b  e3Corrupt(void);
6e630 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
6e640 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71  _CORRUPT_BKPT sq
6e650 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23  lite3Corrupt().#
6e660 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
6e670 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6e680 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  T SQLITE_CORRUPT
6e690 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
6e6a0 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64 65  he ctype.h heade
6e6b0 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 20  r is needed for 
6e6c0 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65 6d  non-ASCII system
6e6d0 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a 2a  s.  It is also.*
6e6e0 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53 33  * needed by FTS3
6e6f0 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69 6e   when FTS3 is in
6e700 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61 6d  cluded in the am
6e710 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  algamation..*/.#
6e720 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
6e730 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a 20  TE_ASCII) || \. 
6e740 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c 49     (defined(SQLI
6e750 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
6e760 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
6e770 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29 29  E_AMALGAMATION))
6e780 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70  .# include <ctyp
6e790 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e.h>.#endif../*.
6e7a0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
6e7b0 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68   macros mimic th
6e7c0 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61  e standard libra
6e7d0 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75  ry functions tou
6e7e0 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61  pper(),.** isspa
6e7f0 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c  ce(), isalnum(),
6e800 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69   isdigit() and i
6e810 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65  sxdigit(), respe
6e820 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
6e830 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20  sqlite versions 
6e840 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53  only work for AS
6e850 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20  CII characters, 
6e860 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f  regardless of lo
6e870 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cale..*/.#ifdef 
6e880 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64  SQLITE_ASCII.# d
6e890 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75  efine sqlite3Tou
6e8a0 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28  pper(x)  ((x)&~(
6e8b0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
6e8c0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
6e8d0 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66  x)]&0x20)).# def
6e8e0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61  ine sqlite3Isspa
6e8f0 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ce(x)   (sqlite3
6e900 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6e910 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6e920 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  1).# define sqli
6e930 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20  te3Isalnum(x)   
6e940 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
6e950 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
6e960 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66  (x)]&0x06).# def
6e970 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
6e980 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33  ha(x)   (sqlite3
6e990 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6e9a0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6e9b0 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  2).# define sqli
6e9c0 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20  te3Isdigit(x)   
6e9d0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70  (sqlite3CtypeMap
6e9e0 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
6e9f0 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66  (x)]&0x04).# def
6ea00 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69  ine sqlite3Isxdi
6ea10 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33  git(x)  (sqlite3
6ea20 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
6ea30 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30  ed char)(x)]&0x0
6ea40 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  8).# define sqli
6ea50 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20  te3Tolower(x)   
6ea60 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  (sqlite3UpperToL
6ea70 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63  ower[(unsigned c
6ea80 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a  har)(x)]).#else.
6ea90 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6eaa0 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75  Toupper(x)   tou
6eab0 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63  pper((unsigned c
6eac0 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e  har)(x)).# defin
6ead0 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  e sqlite3Isspace
6eae0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75  (x)   isspace((u
6eaf0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29  nsigned char)(x)
6eb00 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
6eb10 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69  e3Isalnum(x)   i
6eb20 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64  salnum((unsigned
6eb30 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
6eb40 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
6eb50 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28  ha(x)   isalpha(
6eb60 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
6eb70 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  x)).# define sql
6eb80 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20  ite3Isdigit(x)  
6eb90 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e   isdigit((unsign
6eba0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6ebb0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78  efine sqlite3Isx
6ebc0 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67  digit(x)  isxdig
6ebd0 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  it((unsigned cha
6ebe0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20  r)(x)).# define 
6ebf0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78  sqlite3Tolower(x
6ec00 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73  )   tolower((uns
6ec10 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a  igned char)(x)).
6ec20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
6ec30 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
6ec40 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51  prototypes.*/.SQ
6ec50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ec60 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
6ec70 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f  const char *, co
6ec80 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
6ec90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6eca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
6ecb0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
6ecc0 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50  *, u8);.SQLITE_P
6ecd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6ece0 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74  e3Strlen30(const
6ecf0 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e 65   char*);.#define
6ed00 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
6ed10 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d   sqlite3_strnicm
6ed20 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  p..SQLITE_PRIVAT
6ed30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c  E int sqlite3Mal
6ed40 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53  locInit(void);.S
6ed50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ed60 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  id sqlite3Malloc
6ed70 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  End(void);.SQLIT
6ed80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
6ed90 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e  sqlite3Malloc(in
6eda0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6edb0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6edc0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b  MallocZero(int);
6edd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ede0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d  void *sqlite3DbM
6edf0 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65  allocZero(sqlite
6ee00 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  3*, int);.SQLITE
6ee10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6ee20 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
6ee30 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29  w(sqlite3*, int)
6ee40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ee50 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
6ee60 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  StrDup(sqlite3*,
6ee70 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6ee90 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e  r *sqlite3DbStrN
6eea0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  Dup(sqlite3*,con
6eeb0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  st char*, int);.
6eec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6eed0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c  oid *sqlite3Real
6eee0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  loc(void*, int);
6eef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ef00 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
6ef10 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c  eallocOrFree(sql
6ef20 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20  ite3 *, void *, 
6ef30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6ef40 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6ef50 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69  e3DbRealloc(sqli
6ef60 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
6ef70 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ef80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6ef90 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c  DbFree(sqlite3*,
6efa0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
6efb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6efc0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f  te3MallocSize(vo
6efd0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  id*);.SQLITE_PRI
6efe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6eff0 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c  DbMallocSize(sql
6f000 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
6f010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6f020 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74  id *sqlite3Scrat
6f030 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  chMalloc(int);.S
6f040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6f050 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  id sqlite3Scratc
6f060 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51  hFree(void*);.SQ
6f070 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f080 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61  d *sqlite3PageMa
6f090 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
6f0a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f0b0 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76  qlite3PageFree(v
6f0c0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
6f0d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f0e0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
6f0f0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
6f100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f110 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f  e3BenignMallocHo
6f120 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69  oks(void (*)(voi
6f130 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69  d), void (*)(voi
6f140 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d));.SQLITE_PRIV
6f150 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
6f160 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20  emoryAlarm(void 
6f170 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74  (*)(void*, sqlit
6f180 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20  e3_int64, int), 
6f190 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
6f1a0 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 6e  nt64);../*.** On
6f1b0 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 6d   systems with am
6f1c0 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 20  ple stack space 
6f1d0 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 74  and that support
6f1e0 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 0a   alloca(), make.
6f1f0 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63 61  ** use of alloca
6f200 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61  () to obtain spa
6f210 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75 74  ce for large aut
6f220 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e 20  omatic objects. 
6f230 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20   By default,.** 
6f240 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f  obtain space fro
6f250 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  m malloc()..**.*
6f260 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20 72  * The alloca() r
6f270 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65 74  outine never ret
6f280 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69 73  urns NULL.  This
6f290 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64 65   will cause code
6f2a0 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20 64   paths.** that d
6f2b0 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65 33  eal with sqlite3
6f2c0 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61 69  StackAlloc() fai
6f2d0 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72 65  lures to be unre
6f2e0 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 64  achable..*/.#ifd
6f2f0 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41 4c  ef SQLITE_USE_AL
6f300 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73 71  LOCA.# define sq
6f310 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52  lite3StackAllocR
6f320 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63 61  aw(D,N)   alloca
6f330 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (N).# define sql
6f340 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
6f350 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74 28  ro(D,N)  memset(
6f360 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e 29  alloca(N), 0, N)
6f370 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6f380 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29 20  3StackFree(D,P) 
6f390 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20 64        .#else.# d
6f3a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61  efine sqlite3Sta
6f3b0 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20  ckAllocRaw(D,N) 
6f3c0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f    sqlite3DbMallo
6f3d0 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66 69  cRaw(D,N).# defi
6f3e0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41  ne sqlite3StackA
6f3f0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 73  llocZero(D,N)  s
6f400 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
6f410 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e 65  ro(D,N).# define
6f420 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
6f430 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71 6c  e(D,P)       sql
6f440 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29 0a  ite3DbFree(D,P).
6f450 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
6f460 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
6f470 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS3.SQLITE_PRIV
6f480 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
6f490 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
6f4a0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
6f4b0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys3(void);.#endi
6f4c0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
6f4d0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53  ENABLE_MEMSYS5.S
6f4e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
6f4f0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
6f500 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
6f510 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f  MemGetMemsys5(vo
6f520 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  id);.#endif...#i
6f530 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  fndef SQLITE_MUT
6f540 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50  EX_OMIT.SQLITE_P
6f550 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33  RIVATE   sqlite3
6f560 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
6f570 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
6f580 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  tex(void);.SQLIT
6f590 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
6f5a0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74  te3_mutex *sqlit
6f5b0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  e3MutexAlloc(int
6f5c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f5d0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
6f5e0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a  utexInit(void);.
6f5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6f600 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65   int sqlite3Mute
6f610 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64  xEnd(void);.#end
6f620 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
6f630 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
6f640 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a  atusValue(int);.
6f650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f660 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
6f670 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sAdd(int, int);.
6f680 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f690 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
6f6a0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a  sSet(int, int);.
6f6b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f6c0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  int sqlite3IsNaN
6f6d0 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54  (double);..SQLIT
6f6e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f6f0 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53  qlite3VXPrintf(S
6f700 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63  trAccum*, int, c
6f710 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c  onst char*, va_l
6f720 69 73 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ist);.#ifndef SQ
6f730 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
6f740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f750 6f 69 64 20 73 71 6c 69 74 65 33 58 50 72 69 6e  oid sqlite3XPrin
6f760 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 6f  tf(StrAccum*, co
6f770 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
6f780 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
6f790 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6f7a0 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69  ite3MPrintf(sqli
6f7b0 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
6f7c0 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50  , ...);.SQLITE_P
6f7d0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6f7e0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c  ite3VMPrintf(sql
6f7f0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
6f800 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c  *, va_list);.SQL
6f810 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
6f820 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64   *sqlite3MAppend
6f830 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  f(sqlite3*,char*
6f840 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  ,const char*,...
6f850 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
6f860 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64  QLITE_TEST) || d
6f870 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
6f880 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG).SQLITE_PRIV
6f890 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
6f8a0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f  e3DebugPrintf(co
6f8b0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
6f8c0 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69  .#endif.#if defi
6f8d0 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
6f8e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f8f0 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54    void *sqlite3T
6f900 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e  estTextToPtr(con
6f910 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69  st char*);.#endi
6f920 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
6f930 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74   void sqlite3Set
6f940 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20  String(char **, 
6f950 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
6f960 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
6f970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f980 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
6f990 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63  (Parse*, const c
6f9a0 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
6f9b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f9c0 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61  sqlite3ErrorClea
6f9d0 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  r(Parse*);.SQLIT
6f9e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6f9f0 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61  lite3Dequote(cha
6fa00 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6fa10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b  ATE int sqlite3K
6fa20 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74  eywordCode(const
6fa30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c   unsigned char*,
6fa40 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6fa50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6fa60 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 65  3RunParser(Parse
6fa70 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
6fa80 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  char **);.SQLITE
6fa90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6faa0 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
6fab0 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  g(Parse*);.SQLIT
6fac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6fad0 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
6fae0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6faf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fb00 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
6fb10 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a  eg(Parse*,int);.
6fb20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6fb30 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
6fb40 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e  pRange(Parse*,in
6fb50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6fb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
6fb70 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
6fb80 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b  Parse*,int,int);
6fb90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fba0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
6fbb0 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c  rAlloc(sqlite3*,
6fbc0 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a  int,const Token*
6fbd0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6fbe0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
6fbf0 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a  te3Expr(sqlite3*
6fc00 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
6fc10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fc20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
6fc30 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73  prAttachSubtrees
6fc40 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c  (sqlite3*,Expr*,
6fc50 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51  Expr*,Expr*);.SQ
6fc60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
6fc70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28  r *sqlite3PExpr(
6fc80 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70  Parse*, int, Exp
6fc90 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74  r*, Expr*, const
6fca0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
6fcb0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
6fcc0 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71  qlite3ExprAnd(sq
6fcd0 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78  lite3*,Expr*, Ex
6fce0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
6fcf0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
6fd00 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50  e3ExprFunction(P
6fd10 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  arse*,ExprList*,
6fd20 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
6fd30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fd40 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56  lite3ExprAssignV
6fd50 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c  arNumber(Parse*,
6fd60 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6fd70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fd80 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73  ite3ExprDelete(s
6fd90 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b  qlite3*, Expr*);
6fda0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fdb0 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65  ExprList *sqlite
6fdc0 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
6fdd0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
6fde0 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ,Expr*);.SQLITE_
6fdf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fe00 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
6fe10 61 6d 65 28 50 61 72 73 65 2a 2c 45 78 70 72 4c  ame(Parse*,ExprL
6fe20 69 73 74 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29  ist*,Token*,int)
6fe30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fe40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6fe50 72 4c 69 73 74 53 65 74 53 70 61 6e 28 50 61 72  rListSetSpan(Par
6fe60 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78  se*,ExprList*,Ex
6fe70 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45  prSpan*);.SQLITE
6fe80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fe90 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
6fea0 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ete(sqlite3*, Ex
6feb0 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
6fec0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6fed0 69 74 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33  ite3Init(sqlite3
6fee0 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49  *, char**);.SQLI
6fef0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ff00 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
6ff10 63 6b 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63  ck(void*, int, c
6ff20 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a  har**, char**);.
6ff30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ff40 6f 69 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d  oid sqlite3Pragm
6ff50 61 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  a(Parse*,Token*,
6ff60 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e  Token*,Token*,in
6ff70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6ff80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
6ff90 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
6ffa0 6d 61 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ma(sqlite3*, int
6ffb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ffc0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
6ffd0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
6ffe0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6fff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70000 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43  3CommitInternalC
70010 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
70020 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70030 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52   Table *sqlite3R
70040 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
70050 28 50 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29  (Parse*,Select*)
70060 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
70080 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72  nMasterTable(Par
70090 73 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  se *, int);.SQLI
700a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
700b0 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
700c0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  e(Parse*,Token*,
700d0 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69  Token*,int,int,i
700e0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
700f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70100 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61  ite3AddColumn(Pa
70110 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
70120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70130 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e  d sqlite3AddNotN
70140 75 6c 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ull(Parse*, int)
70150 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70160 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
70170 50 72 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65  PrimaryKey(Parse
70180 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
70190 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
701a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
701b0 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63  d sqlite3AddChec
701c0 6b 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73  kConstraint(Pars
701d0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  e*, Expr*);.SQLI
701e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
701f0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
70200 54 79 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65  Type(Parse*,Toke
70210 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
70220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70230 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
70240 50 61 72 73 65 2a 2c 45 78 70 72 53 70 61 6e 2a  Parse*,ExprSpan*
70250 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70260 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
70270 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72  dCollateType(Par
70280 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  se*, Token*);.SQ
70290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
702a0 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  d sqlite3EndTabl
702b0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  e(Parse*,Token*,
702c0 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b  Token*,Select*);
702d0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
702e0 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33   Bitvec *sqlite3
702f0 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33 32  BitvecCreate(u32
70300 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70310 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74  E int sqlite3Bit
70320 76 65 63 54 65 73 74 28 42 69 74 76 65 63 2a 2c  vecTest(Bitvec*,
70330 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u32);.SQLITE_PR
70340 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70350 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65  3BitvecSet(Bitve
70360 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45  c*, u32);.SQLITE
70370 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70380 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
70390 28 42 69 74 76 65 63 2a 2c 20 75 33 32 2c 20 76  (Bitvec*, u32, v
703a0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
703b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
703c0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
703d0 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45  Bitvec*);.SQLITE
703e0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
703f0 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42  ite3BitvecSize(B
70400 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f  itvec*);.SQLITE_
70410 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
70420 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e  te3BitvecBuiltin
70430 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a  Test(int,int*);.
70440 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70450 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52  RowSet *sqlite3R
70460 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65  owSetInit(sqlite
70470 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67  3*, void*, unsig
70480 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ned int);.SQLITE
70490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
704a0 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72  lite3RowSetClear
704b0 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54  (RowSet*);.SQLIT
704c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
704d0 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
704e0 72 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29  rt(RowSet*, i64)
704f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70500 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53   int sqlite3RowS
70510 65 74 54 65 73 74 28 52 6f 77 53 65 74 2a 2c 20  etTest(RowSet*, 
70520 75 38 20 69 42 61 74 63 68 2c 20 69 36 34 29 3b  u8 iBatch, i64);
70530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70540 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65  int sqlite3RowSe
70550 74 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69  tNext(RowSet*, i
70560 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  64*);..SQLITE_PR
70570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70580 65 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72  e3CreateView(Par
70590 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
705a0 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a  *,Token*,Select*
705b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20  ,int,int);..#if 
705c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
705d0 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
705e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
705f0 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
70600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70610 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65    int sqlite3Vie
70620 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
70630 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a  Parse*,Table*);.
70640 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
70650 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
70660 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a  umnNames(A,B) 0.
70670 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
70680 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70690 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72  te3DropTable(Par
706a0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69  se*, SrcList*, i
706b0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
706c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
706d0 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
706e0 28 54 61 62 6c 65 2a 29 3b 0a 23 69 66 6e 64 65  (Table*);.#ifnde
706f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
70700 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 53 51 4c 49  TOINCREMENT.SQLI
70710 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
70720 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63  d sqlite3Autoinc
70730 72 65 6d 65 6e 74 42 65 67 69 6e 28 50 61 72 73  rementBegin(Pars
70740 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49  e *pParse);.SQLI
70750 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
70760 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63  d sqlite3Autoinc
70770 72 65 6d 65 6e 74 45 6e 64 28 50 61 72 73 65 20  rementEnd(Parse 
70780 2a 70 50 61 72 73 65 29 3b 0a 23 65 6c 73 65 0a  *pParse);.#else.
70790 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
707a0 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67  AutoincrementBeg
707b0 69 6e 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  in(X).# define s
707c0 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
707d0 65 6e 74 45 6e 64 28 58 29 0a 23 65 6e 64 69 66  entEnd(X).#endif
707e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
707f0 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65  void sqlite3Inse
70800 72 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rt(Parse*, SrcLi
70810 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  st*, ExprList*, 
70820 53 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74 2a  Select*, IdList*
70830 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70840 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
70850 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
70860 65 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a  e(sqlite3*,void*
70870 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e  ,int,int,int*,in
70880 74 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t*,int*);.SQLITE
70890 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20  _PRIVATE IdList 
708a0 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70  *sqlite3IdListAp
708b0 70 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 49  pend(sqlite3*, I
708c0 64 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  dList*, Token*);
708d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
708e0 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73  int sqlite3IdLis
708f0 74 49 6e 64 65 78 28 49 64 4c 69 73 74 2a 2c 63  tIndex(IdList*,c
70900 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
70910 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
70920 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
70930 69 73 74 45 6e 6c 61 72 67 65 28 73 71 6c 69 74  istEnlarge(sqlit
70940 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69  e3*, SrcList*, i
70950 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70960 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
70970 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
70980 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c  Append(sqlite3*,
70990 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
709a0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
709b0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
709c0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
709d0 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d  stAppendFromTerm
709e0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
709f0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
70a00 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
70a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70a20 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c           Token*,
70a30 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 2a 2c   Select*, Expr*,
70a40 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54   IdList*);.SQLIT
70a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70a60 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64  qlite3SrcListInd
70a70 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 2c 20  exedBy(Parse *, 
70a80 53 72 63 4c 69 73 74 20 2a 2c 20 54 6f 6b 65 6e  SrcList *, Token
70a90 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
70aa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
70ab0 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 50  ndexedByLookup(P
70ac0 61 72 73 65 20 2a 2c 20 73 74 72 75 63 74 20 53  arse *, struct S
70ad0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 29 3b 0a  rcList_item *);.
70ae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70af0 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
70b00 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
70b10 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
70b20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70b30 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
70b40 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65  ignCursors(Parse
70b50 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
70b60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70b70 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  d sqlite3IdListD
70b80 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20  elete(sqlite3*, 
70b90 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  IdList*);.SQLITE
70ba0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70bb0 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
70bc0 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  te(sqlite3*, Src
70bd0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
70be0 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71  RIVATE Index *sq
70bf0 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
70c00 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54  (Parse*,Token*,T
70c10 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  oken*,SrcList*,E
70c20 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b  xprList*,int,Tok
70c30 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en*,.           
70c40 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b               Tok
70c50 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  en*, int, int);.
70c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70c70 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49  oid sqlite3DropI
70c80 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63  ndex(Parse*, Src
70c90 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
70ca0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
70cb0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61  sqlite3Select(Pa
70cc0 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 53  rse*, Select*, S
70cd0 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53 51 4c  electDest*);.SQL
70ce0 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
70cf0 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
70d00 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 70 72  tNew(Parse*,Expr
70d10 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 45  List*,SrcList*,E
70d20 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 0a  xpr*,ExprList*,.
70d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70d40 20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 45           Expr*,E
70d50 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70  xprList*,int,Exp
70d60 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  r*,Expr*);.SQLIT
70d70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70d80 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
70d90 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c  te(sqlite3*, Sel
70da0 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ect*);.SQLITE_PR
70db0 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
70dc0 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
70dd0 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  p(Parse*, SrcLis
70de0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
70df0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
70e00 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 2a  sReadOnly(Parse*
70e10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a  , Table*, int);.
70e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70e30 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  oid sqlite3OpenT
70e40 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  able(Parse*, int
70e50 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c 20   iCur, int iDb, 
70e60 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a 23 69  Table*, int);.#i
70e70 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
70e80 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44  _ENABLE_UPDATE_D
70e90 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20  ELETE_LIMIT) && 
70ea0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
70eb0 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53  OMIT_SUBQUERY).S
70ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
70ed0 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74  pr *sqlite3Limit
70ee0 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c 20 53  Where(Parse *, S
70ef0 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a  rcList *, Expr *
70f00 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20 45 78  , ExprList *, Ex
70f10 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20 63 68  pr *, Expr *, ch
70f20 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51  ar *);.#endif.SQ
70f30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70f40 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46  d sqlite3DeleteF
70f50 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  rom(Parse*, SrcL
70f60 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  ist*, Expr*);.SQ
70f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70f80 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28  d sqlite3Update(
70f90 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
70fa0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70  , ExprList*, Exp
70fb0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
70fc0 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e  _PRIVATE WhereIn
70fd0 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65  fo *sqlite3Where
70fe0 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 53 72  Begin(Parse*, Sr
70ff0 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 45  cList*, Expr*, E
71000 78 70 72 4c 69 73 74 2a 2a 2c 20 75 31 36 29 3b  xprList**, u16);
71010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71020 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72  void sqlite3Wher
71030 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a 29  eEnd(WhereInfo*)
71040 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71050 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
71060 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61  CodeGetColumn(Pa
71070 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
71080 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  t, int, int, int
71090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
710a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
710b0 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65  prCodeMove(Parse
710c0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
710d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
710e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
710f0 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65  prCodeCopy(Parse
71100 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
71110 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71120 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
71130 70 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72  prCacheStore(Par
71140 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  se*, int, int, i
71150 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71160 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71170 45 78 70 72 43 61 63 68 65 50 75 73 68 28 50 61  ExprCachePush(Pa
71180 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
71190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
711a0 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 50  e3ExprCachePop(P
711b0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
711c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
711d0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
711e0 65 52 65 6d 6f 76 65 28 50 61 72 73 65 2a 2c 20  eRemove(Parse*, 
711f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71200 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71210 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
71220 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
71230 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71240 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
71250 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73  inityChange(Pars
71260 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  e*, int, int);.S
71270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71280 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61  id sqlite3ExprHa
71290 72 64 43 6f 70 79 28 50 61 72 73 65 2a 2c 69 6e  rdCopy(Parse*,in
712a0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t,int);.SQLITE_P
712b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
712c0 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65  e3ExprCode(Parse
712d0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
712e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
712f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
71300 64 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45  deTemp(Parse*, E
71310 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  xpr*, int*);.SQL
71320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71330 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
71340 61 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78  arget(Parse*, Ex
71350 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  pr*, int);.SQLIT
71360 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
71370 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64  lite3ExprCodeAnd
71380 43 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78  Cache(Parse*, Ex
71390 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  pr*, int);.SQLIT
713a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
713b0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
713c0 6e 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20  nstants(Parse*, 
713d0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
713e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
713f0 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
71400 73 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c  st(Parse*, ExprL
71410 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  ist*, int, int);
71420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71430 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
71440 49 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45  IfTrue(Parse*, E
71450 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  xpr*, int, int);
71460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71470 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
71480 49 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20  IfFalse(Parse*, 
71490 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  Expr*, int, int)
714a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
714b0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46   Table *sqlite3F
714c0 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33  indTable(sqlite3
714d0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  *,const char*, c
714e0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
714f0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
71500 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  e *sqlite3Locate
71510 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74  Table(Parse*,int
71520 20 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68   isView,const ch
71530 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ar*, const char*
71540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71550 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33  E Index *sqlite3
71560 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65  FindIndex(sqlite
71570 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
71580 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
71590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
715a0 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
715b0 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71  ndDeleteTable(sq
715c0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
715d0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
715e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
715f0 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
71600 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33  eteIndex(sqlite3
71610 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
71620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
71640 61 63 75 75 6d 28 50 61 72 73 65 2a 29 3b 0a 53  acuum(Parse*);.S
71650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
71660 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  t sqlite3RunVacu
71670 75 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c 69 74  um(char**, sqlit
71680 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
71690 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
716a0 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
716b0 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a  sqlite3*, Token*
716c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
716d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
716e0 72 43 6f 6d 70 61 72 65 28 45 78 70 72 2a 2c 20  rCompare(Expr*, 
716f0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
71700 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71710 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
71720 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e  gregates(NameCon
71730 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  text*, Expr*);.S
71740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71750 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
71760 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d  alyzeAggList(Nam
71770 65 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72 4c 69  eContext*,ExprLi
71780 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
71790 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74  VATE Vdbe *sqlit
717a0 65 33 47 65 74 56 64 62 65 28 50 61 72 73 65 2a  e3GetVdbe(Parse*
717b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
717c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
717d0 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64  ngSaveState(void
717e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
717f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
71800 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76  ngRestoreState(v
71810 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
71820 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71830 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28  3PrngResetState(
71840 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
71850 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71860 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71  e3RollbackAll(sq
71870 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
71880 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71890 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
718a0 68 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74  hema(Parse*, int
718b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
718c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
718d0 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50  ginTransaction(P
718e0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
718f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71900 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72   sqlite3CommitTr
71910 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a  ansaction(Parse*
71920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
71940 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f  llbackTransactio
71950 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  n(Parse*);.SQLIT
71960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71970 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28  qlite3Savepoint(
71980 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 6f 6b  Parse*, int, Tok
71990 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
719a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
719b0 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
719c0 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c  (sqlite3 *);.SQL
719d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
719e0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
719f0 73 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a 53 51  stant(Expr*);.SQ
71a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71a10 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
71a20 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78  nstantNotJoin(Ex
71a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
71a40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71a50 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72  ExprIsConstantOr
71a60 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a 29 3b  Function(Expr*);
71a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71a80 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49  int sqlite3ExprI
71a90 73 49 6e 74 65 67 65 72 28 45 78 70 72 2a 2c 20  sInteger(Expr*, 
71aa0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
71ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71ac0 33 45 78 70 72 43 61 6e 42 65 4e 75 6c 6c 28 63  3ExprCanBeNull(c
71ad0 6f 6e 73 74 20 45 78 70 72 2a 29 3b 0a 53 51 4c  onst Expr*);.SQL
71ae0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71af0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
71b00 49 73 4e 75 6c 6c 4a 75 6d 70 28 56 64 62 65 2a  IsNullJump(Vdbe*
71b10 2c 20 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20 69  , const Expr*, i
71b20 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
71b30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
71b40 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41  ite3ExprNeedsNoA
71b50 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 63 6f  ffinityChange(co
71b60 6e 73 74 20 45 78 70 72 2a 2c 20 63 68 61 72 29  nst Expr*, char)
71b70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71b80 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f   int sqlite3IsRo
71b90 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  wid(const char*)
71ba0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71bb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
71bc0 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50  erateRowDelete(P
71bd0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
71be0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72  nt, int, int, Tr
71bf0 69 67 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53  igger *, int);.S
71c00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71c10 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
71c20 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
71c30 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
71c40 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c   int, int*);.SQL
71c50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71c60 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
71c70 6e 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20  ndexKey(Parse*, 
71c80 49 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74  Index*, int, int
71c90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
71ca0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71cb0 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
71cc0 72 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73  raintChecks(Pars
71cd0 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e  e*,Table*,int,in
71ce0 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
71cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71d00 20 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74          int*,int
71d10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
71d20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71d30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
71d40 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e  ompleteInsertion
71d50 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
71d60 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c   int, int, int*,
71d70 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b   int, int, int);
71d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71d90 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
71da0 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50  ableAndIndices(P
71db0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
71dc0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
71dd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71de0 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
71df0 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c  peration(Parse*,
71e00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
71e10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71e20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74  sqlite3MultiWrit
71e30 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  e(Parse*);.SQLIT
71e40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71e50 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50  qlite3MayAbort(P
71e60 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
71e70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71e80 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
71e90 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  t(Parse*, int, c
71ea0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  har*, int);.SQLI
71eb0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
71ec0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
71ed0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69  sqlite3*,Expr*,i
71ee0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71ef0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71  ATE ExprList *sq
71f00 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
71f10 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69  (sqlite3*,ExprLi
71f20 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  st*,int);.SQLITE
71f30 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
71f40 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
71f50 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63  Dup(sqlite3*,Src
71f60 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  List*,int);.SQLI
71f70 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73  TE_PRIVATE IdLis
71f80 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  t *sqlite3IdList
71f90 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c  Dup(sqlite3*,IdL
71fa0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
71fb0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
71fc0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73  lite3SelectDup(s
71fd0 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c  qlite3*,Select*,
71fe0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71ff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
72000 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46  3FuncDefInsert(F
72010 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e  uncDefHash*, Fun
72020 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  cDef*);.SQLITE_P
72030 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
72040 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
72050 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ion(sqlite3*,con
72060 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74  st char*,int,int
72070 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ,u8,int);.SQLITE
72080 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72090 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69  lite3RegisterBui
720a0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71  ltinFunctions(sq
720b0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
720c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
720d0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
720e0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f  TimeFunctions(vo
720f0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
72100 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
72110 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
72120 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23  nctions(void);.#
72130 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
72140 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
72150 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
72160 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a  afetyOn(sqlite3*
72170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72180 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
72190 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
721a0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
721b0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
721c0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65  On(A) 0.# define
721d0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
721e0 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51  f(A) 0.#endif.SQ
721f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72200 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
72210 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b  eckOk(sqlite3*);
72220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72230 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
72240 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
72250 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
72260 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
72270 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
72280 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
72290 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
722a0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
722b0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
722c0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
722d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
722e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65  void sqlite3Mate
722f0 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73  rializeView(Pars
72300 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
72310 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  *, int);.#endif.
72320 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
72330 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c  OMIT_TRIGGER.SQL
72340 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
72350 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
72360 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54  rigger(Parse*, T
72370 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
72380 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63  ,int,IdList*,Src
72390 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
723a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
723b0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74    Expr*,int, int
723c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
723d0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
723e0 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61  FinishTrigger(Pa
723f0 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65  rse*, TriggerSte
72400 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  p*, Token*);.SQL
72410 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
72420 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
72430 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72  igger(Parse*, Sr
72440 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  cList*, int);.SQ
72450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
72460 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
72470 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a  riggerPtr(Parse*
72480 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c  , Trigger*);.SQL
72490 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
724a0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
724b0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73  iggersExist(Pars
724c0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  e *, Table*, int
724d0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
724e0 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45   *pMask);.SQLITE
724f0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
72500 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  er *sqlite3Trigg
72510 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20  erList(Parse *, 
72520 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
72530 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
72540 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
72550 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72  igger(Parse*, Tr
72560 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78  igger *, int, Ex
72570 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61  prList*, int, Ta
72580 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20  ble *,.         
72590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
725a0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74     int, int, int
725b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
725c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
725d0 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69  CodeRowTriggerDi
725e0 72 65 63 74 28 50 61 72 73 65 20 2a 2c 20 54 72  rect(Parse *, Tr
725f0 69 67 67 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a  igger *, Table *
72600 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
72610 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74 65 56  ;.  void sqliteV
72620 69 65 77 54 72 69 67 67 65 72 73 28 50 61 72 73  iewTriggers(Pars
72630 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
72640 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
72650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72660 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72670 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
72680 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69  ep(sqlite3*, Tri
72690 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49  ggerStep*);.SQLI
726a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
726b0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
726c0 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74  3TriggerSelectSt
726d0 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65  ep(sqlite3*,Sele
726e0 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
726f0 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74  VATE   TriggerSt
72700 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
72710 65 72 49 6e 73 65 72 74 53 74 65 70 28 73 71 6c  erInsertStep(sql
72720 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64  ite3*,Token*, Id
72730 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
72740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
72760 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a  xprList*,Select*
72770 2c 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ,u8);.SQLITE_PRI
72780 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74  VATE   TriggerSt
72790 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
727a0 65 72 55 70 64 61 74 65 53 74 65 70 28 73 71 6c  erUpdateStep(sql
727b0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70  ite3*,Token*,Exp
727c0 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75  rList*, Expr*, u
727d0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  8);.SQLITE_PRIVA
727e0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70  TE   TriggerStep
727f0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
72800 44 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74  DeleteStep(sqlit
72810 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72  e3*,Token*, Expr
72820 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72830 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72840 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  3DeleteTrigger(s
72850 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72  qlite3*, Trigger
72860 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72870 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72880 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
72890 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a  Trigger(sqlite3*
728a0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
728b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
728c0 45 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 54  E   u32 sqlite3T
728d0 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 50 61  riggerColmask(Pa
728e0 72 73 65 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78  rse*,Trigger*,Ex
728f0 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 69 6e 74 2c  prList*,int,int,
72900 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64  Table*,int);.# d
72910 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72  efine sqlite3Par
72920 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 28 28  seToplevel(p) ((
72930 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20  p)->pToplevel ? 
72940 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a  (p)->pToplevel :
72950 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20 64 65   (p)).#else.# de
72960 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
72970 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44 2c  gersExist(B,C,D,
72980 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65 20  E,F) 0.# define 
72990 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
729a0 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66 69  gger(A,B).# defi
729b0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  ne sqlite3DropTr
729c0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 20  iggerPtr(A,B).# 
729d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 6e  define sqlite3Un
729e0 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69  linkAndDeleteTri
729f0 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 65  gger(A,B,C).# de
72a00 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65  fine sqlite3Code
72a10 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c 43  RowTrigger(A,B,C
72a20 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29 0a 23 20  ,D,E,F,G,H,I).# 
72a30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
72a40 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65  deRowTriggerDire
72a50 63 74 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 0a  ct(A,B,C,D,E,F).
72a60 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72a70 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c 20 59  TriggerList(X, Y
72a80 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ) 0.# define sql
72a90 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
72aa0 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e 65 20  l(p) p.# define 
72ab0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f  sqlite3TriggerCo
72ac0 6c 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 2c  lmask(A,B,C,D,E,
72ad0 46 2c 47 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53  F,G) 0.#endif..S
72ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72af0 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70  t sqlite3JoinTyp
72b00 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
72b10 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
72b20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72b30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
72b40 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50  eateForeignKey(P
72b50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a  arse*, ExprList*
72b60 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69  , Token*, ExprLi
72b70 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  st*, int);.SQLIT
72b80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
72b90 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
72ba0 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e  gnKey(Parse*, in
72bb0 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
72bc0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
72bd0 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  ATION.SQLITE_PRI
72be0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
72bf0 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73  te3AuthRead(Pars
72c00 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a  e*,Expr*,Schema*
72c10 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  ,SrcList*);.SQLI
72c20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
72c30 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
72c40 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f  k(Parse*,int, co
72c50 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
72c60 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
72c70 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
72c80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
72c90 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
72ca0 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43  sh(Parse*, AuthC
72cb0 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63  ontext*, const c
72cc0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
72cd0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
72ce0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
72cf0 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29  op(AuthContext*)
72d00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72d10 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75     int sqlite3Au
72d20 74 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65 2a  thReadCol(Parse*
72d30 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
72d40 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
72d50 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  t);.#else.# defi
72d60 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65  ne sqlite3AuthRe
72d70 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65  ad(a,b,c,d).# de
72d80 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68  fine sqlite3Auth
72d90 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29  Check(a,b,c,d,e)
72da0 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20      SQLITE_OK.# 
72db0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
72dc0 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c  thContextPush(a,
72dd0 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71  b,c).# define sq
72de0 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
72df0 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28  Pop(a)  ((void)(
72e00 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  a)).#endif.SQLIT
72e10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
72e20 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72  qlite3Attach(Par
72e30 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72  se*, Expr*, Expr
72e40 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
72e50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
72e60 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72  qlite3Detach(Par
72e70 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  se*, Expr*);.SQL
72e80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72e90 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
72ea0 6f 72 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ory(sqlite3 *db,
72eb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
72ec0 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  lename,.        
72ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
72ee0 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20  nt omitJournal, 
72ef0 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e 74 20  int nCache, int 
72f00 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a 2a 70  flags, Btree **p
72f10 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f  pBtree);.SQLITE_
72f20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72f30 74 65 33 46 69 78 49 6e 69 74 28 44 62 46 69 78  te3FixInit(DbFix
72f40 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74  er*, Parse*, int
72f50 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  , const char*, c
72f60 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  onst Token*);.SQ
72f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72f80 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
72f90 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53 72 63  st(DbFixer*, Src
72fa0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
72fb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
72fc0 65 33 46 69 78 53 65 6c 65 63 74 28 44 62 46 69  e3FixSelect(DbFi
72fd0 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a  xer*, Select*);.
72fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
72ff0 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  nt sqlite3FixExp
73000 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72  r(DbFixer*, Expr
73010 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
73030 78 45 78 70 72 4c 69 73 74 28 44 62 46 69 78 65  xExprList(DbFixe
73040 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a  r*, ExprList*);.
73050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73060 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69  nt sqlite3FixTri
73070 67 67 65 72 53 74 65 70 28 44 62 46 69 78 65 72  ggerStep(DbFixer
73080 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29  *, TriggerStep*)
73090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
730a0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46   int sqlite3AtoF
730b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
730c0 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45  double*);.SQLITE
730d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
730e0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e  ite3GetInt32(con
730f0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29  st char *, int*)
73100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73110 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73   int sqlite3Fits
73120 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63  In64Bits(const c
73130 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  har *, int);.SQL
73140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73150 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65  sqlite3Utf16Byte
73160 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  Len(const void *
73170 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 61 72  pData, int nChar
73180 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73190 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
731a0 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63  8CharLen(const c
731b0 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
731c0 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50  nByte);.SQLITE_P
731d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
731e0 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e 73 74  e3Utf8Read(const
731f0 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a   u8*, const u8**
73200 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  );../*.** Routin
73210 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20 77  es to read and w
73220 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c 65  rite variable-le
73230 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20  ngth integers.  
73240 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a  These used to.**
73250 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 61   be defined loca
73260 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 20  lly, but now we 
73270 75 73 65 20 74 68 65 20 76 61 72 69 6e 74 20 72  use the varint r
73280 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 75  outines in the u
73290 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20  til.c.** file.  
732a0 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20  Code should use 
732b0 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20  the MACRO forms 
732c0 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56 61  below, as the Va
732d0 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73 0a  rint32 versions.
732e0 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f 20  ** are coded to 
732f0 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67 6c  assume the singl
73300 65 20 62 79 74 65 20 63 61 73 65 20 69 73 20 61  e byte case is a
73310 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20 28  lready handled (
73320 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41  which .** the MA
73330 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a  CRO form does)..
73340 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
73350 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74  E int sqlite3Put
73360 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20  Varint(unsigned 
73370 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c  char*, u64);.SQL
73380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73390 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
733a0 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  32(unsigned char
733b0 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  *, u32);.SQLITE_
733c0 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74  PRIVATE u8 sqlit
733d0 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73  e3GetVarint(cons
733e0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
733f0 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54  *, u64 *);.SQLIT
73400 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
73410 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
73420 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
73430 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53  har *, u32 *);.S
73440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73450 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  t sqlite3VarintL
73460 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a  en(u64 v);../*.*
73470 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20  * The header of 
73480 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74  a record consist
73490 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20  s of a sequence 
734a0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
734b0 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65  integers..** The
734c0 73 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20  se integers are 
734d0 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d  almost always sm
734e0 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f  all and are enco
734f0 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20  ded as a single 
73500 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c  byte..** The fol
73510 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61  lowing macros ta
73520 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69  ke advantage thi
73530 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64  s fact to provid
73540 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a  e a fast encode.
73550 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66  ** and decode of
73560 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e   the integers in
73570 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72   a record header
73580 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20  .  It is faster 
73590 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a  for the common.*
735a0 2a 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65  * case where the
735b0 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69   integer is a si
735c0 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69  ngle byte.  It i
735d0 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65  s a little slowe
735e0 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e  r when the.** in
735f0 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20  teger is two or 
73600 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74  more bytes.  But
73610 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66   overall it is f
73620 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  aster..**.** The
73630 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65   following expre
73640 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76  ssions are equiv
73650 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  alent:.**.**    
73660 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   x = sqlite3GetV
73670 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29  arint32( A, &B )
73680 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c  ;.**     x = sql
73690 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28  ite3PutVarint32(
736a0 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20   A, B );.**.**  
736b0 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74     x = getVarint
736c0 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20  32( A, B );.**  
736d0 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74     x = putVarint
736e0 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a  32( A, B );.**.*
736f0 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72  /.#define getVar
73700 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29  int32(A,B)  (u8)
73710 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30 29  ((*(A)<(u8)0x80)
73720 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29 2a   ? ((B) = (u32)*
73730 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33  (A)),1 : sqlite3
73740 47 65 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c  GetVarint32((A),
73750 20 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a 23   (u32 *)&(B))).#
73760 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74  define putVarint
73770 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 28  32(A,B)  (u8)(((
73780 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 78 38  u32)(B)<(u32)0x8
73790 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e  0) ? (*(A) = (un
737a0 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 29 29  signed char)(B))
737b0 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74 56  ,1 : sqlite3PutV
737c0 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42 29  arint32((A), (B)
737d0 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61  )).#define getVa
737e0 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 47  rint    sqlite3G
737f0 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e 65  etVarint.#define
73800 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 73 71   putVarint    sq
73810 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a 0a  lite3PutVarint..
73820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73830 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
73840 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
73850 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64 65  Str(Vdbe *, Inde
73860 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  x *);.SQLITE_PRI
73870 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
73880 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74  3TableAffinitySt
73890 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20  r(Vdbe *, Table 
738a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
738b0 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43  TE char sqlite3C
738c0 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45  ompareAffinity(E
738d0 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72  xpr *pExpr, char
738e0 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f 50   aff2);.SQLITE_P
738f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73900 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
73910 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63  k(Expr *pExpr, c
73920 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79  har idx_affinity
73930 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73940 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78  E char sqlite3Ex
73950 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20  prAffinity(Expr 
73960 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f  *pExpr);.SQLITE_
73970 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73980 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20  te3Atoi64(const 
73990 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53 51  char*, i64*);.SQ
739a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
739b0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73  d sqlite3Error(s
739c0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f  qlite3*, int, co
739d0 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a  nst char*,...);.
739e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
739f0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54  oid *sqlite3HexT
73a00 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c 20  oBlob(sqlite3*, 
73a10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
73a20 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt n);.SQLITE_PR
73a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73a40 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61 72  3TwoPartName(Par
73a50 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54  se *, Token *, T
73a60 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a  oken *, Token **
73a70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73a80 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
73a90 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 29  lite3ErrStr(int)
73aa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73ab0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64   int sqlite3Read
73ac0 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50  Schema(Parse *pP
73ad0 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  arse);.SQLITE_PR
73ae0 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
73af0 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
73b00 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65 6e  q(sqlite3*,u8 en
73b10 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  c, const char*,i
73b20 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
73b30 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
73b40 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
73b50 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
73b60 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d   const char*zNam
73b70 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
73b80 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
73b90 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50  te3ExprCollSeq(P
73ba0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
73bb0 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49  pr *pExpr);.SQLI
73bc0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
73bd0 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43  *sqlite3ExprSetC
73be0 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73  oll(Parse *pPars
73bf0 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b 65 6e  e, Expr *, Token
73c00 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
73c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
73c20 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73  heckCollSeq(Pars
73c30 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 29 3b  e *, CollSeq *);
73c40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73c50 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b  int sqlite3Check
73c60 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65  ObjectName(Parse
73c70 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
73c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
73ca0 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c  beSetChanges(sql
73cb0 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53  ite3 *, int);..S
73cc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
73cd0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
73ce0 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74  3ValueText(sqlit
73cf0 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a  e3_value*, u8);.
73d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73d10 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42  nt sqlite3ValueB
73d20 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
73d30 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  ue*, u8);.SQLITE
73d40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
73d50 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
73d60 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c  (sqlite3_value*,
73d70 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64   int, const void
73d80 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20   *,u8, .        
73d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73da0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
73db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73dc0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
73dd0 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61  eFree(sqlite3_va
73de0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  lue*);.SQLITE_PR
73df0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61  IVATE sqlite3_va
73e00 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75  lue *sqlite3Valu
73e10 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b  eNew(sqlite3 *);
73e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73e30 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66  char *sqlite3Utf
73e40 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c  16to8(sqlite3 *,
73e50 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
73e60 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t);.#ifdef SQLIT
73e70 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53  E_ENABLE_STAT2.S
73e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
73e90 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74  ar *sqlite3Utf8t
73ea0 6f 31 36 28 73 71 6c 69 74 65 33 20 2a 2c 20 75  o16(sqlite3 *, u
73eb0 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  8, char *, int, 
73ec0 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53  int *);.#endif.S
73ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73ee0 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  t sqlite3ValueFr
73ef0 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a  omExpr(sqlite3 *
73f00 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38  , Expr *, u8, u8
73f10 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
73f20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
73f30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73f40 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
73f50 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ty(sqlite3_value
73f60 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66   *, u8, u8);.#if
73f70 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  ndef SQLITE_AMAL
73f80 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f  GAMATION.SQLITE_
73f90 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e  PRIVATE const un
73fa0 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
73fb0 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74  te3OpcodePropert
73fc0 79 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  y[];.SQLITE_PRIV
73fd0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
73fe0 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55  ed char sqlite3U
73ff0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53  pperToLower[];.S
74000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
74010 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
74020 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61  r sqlite3CtypeMa
74030 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  p[];.SQLITE_PRIV
74040 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73  ATE SQLITE_WSD s
74050 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e  truct Sqlite3Con
74060 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69  fig sqlite3Confi
74070 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  g;.SQLITE_PRIVAT
74080 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  E SQLITE_WSD Fun
74090 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33  cDefHash sqlite3
740a0 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b  GlobalFunctions;
740b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
740c0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69  int sqlite3Pendi
740d0 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53  ngByte;.#endif.S
740e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
740f0 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
74100 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74  geMoved(Db*, int
74110 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74120 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74130 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65  te3Reindex(Parse
74140 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
74150 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74160 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
74170 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71  lterFunctions(sq
74180 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
74190 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
741a0 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54  ite3AlterRenameT
741b0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63  able(Parse*, Src
741c0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  List*, Token*);.
741d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
741e0 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b  nt sqlite3GetTok
741f0 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  en(const unsigne
74200 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29  d char *, int *)
74210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74220 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73   void sqlite3Nes
74230 74 65 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c  tedParse(Parse*,
74240 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e   const char*, ..
74250 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
74260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
74270 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61  xpirePreparedSta
74280 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a  tements(sqlite3*
74290 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
742a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64  E int sqlite3Cod
742b0 65 53 75 62 73 65 6c 65 63 74 28 50 61 72 73 65  eSubselect(Parse
742c0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c   *, Expr *, int,
742d0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
742e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
742f0 65 33 53 65 6c 65 63 74 50 72 65 70 28 50 61 72  e3SelectPrep(Par
74300 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61  se*, Select*, Na
74310 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  meContext*);.SQL
74320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74330 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
74340 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74  prNames(NameCont
74350 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  ext*, Expr*);.SQ
74360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74370 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  d sqlite3Resolve
74380 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73  SelectNames(Pars
74390 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d  e*, Select*, Nam
743a0 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  eContext*);.SQLI
743b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
743c0 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
743d0 65 72 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a  erGroupBy(Parse*
743e0 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c  , Select*, ExprL
743f0 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ist*, const char
74400 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74410 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
74420 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62  olumnDefault(Vdb
74430 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e  e *, Table *, in
74440 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
74450 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
74460 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41  ite3AlterFinishA
74470 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a  ddColumn(Parse *
74480 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49  , Token *);.SQLI
74490 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
744a0 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
744b0 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65  nAddColumn(Parse
744c0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a   *, SrcList *);.
744d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
744e0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47  ollSeq *sqlite3G
744f0 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65  etCollSeq(sqlite
74500 33 2a 2c 20 75 38 2c 20 43 6f 6c 6c 53 65 71 20  3*, u8, CollSeq 
74510 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
74520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74530 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69  char sqlite3Affi
74540 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63  nityType(const c
74550 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
74560 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74570 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a  e3Analyze(Parse*
74580 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
74590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
745a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76  E int sqlite3Inv
745b0 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42  okeBusyHandler(B
745c0 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51  usyHandler*);.SQ
745d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
745e0 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73   sqlite3FindDb(s
745f0 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29  qlite3*, Token*)
74600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74610 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64   int sqlite3Find
74620 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a  DbName(sqlite3 *
74630 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
74640 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74650 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  int sqlite3Analy
74660 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a  sisLoad(sqlite3*
74670 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54  ,int iDB);.SQLIT
74680 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74690 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65  qlite3DeleteInde
746a0 78 53 61 6d 70 6c 65 73 28 49 6e 64 65 78 2a 29  xSamples(Index*)
746b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
746c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66   void sqlite3Def
746d0 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78  aultRowEst(Index
746e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
746f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
74700 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74  egisterLikeFunct
74710 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69  ions(sqlite3*, i
74720 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
74730 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
74740 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71  sLikeFunction(sq
74750 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74  lite3*,Expr*,int
74760 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  *,char*);.SQLITE
74770 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74780 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65  lite3MinimumFile
74790 46 6f 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69  Format(Parse*, i
747a0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
747b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
747c0 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28  lite3SchemaFree(
747d0 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  void *);.SQLITE_
747e0 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a  PRIVATE Schema *
747f0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
74800 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65  (sqlite3 *, Btre
74810 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
74820 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74830 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71  SchemaToIndex(sq
74840 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d  lite3 *db, Schem
74850 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  a *);.SQLITE_PRI
74860 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71  VATE KeyInfo *sq
74870 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
74880 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78  o(Parse *, Index
74890 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
748a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
748b0 72 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65  reateFunc(sqlite
748c0 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  3 *, const char 
748d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69  *, int, int, voi
748e0 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29  d *, .  void (*)
748f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
74900 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
74910 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  lue **),.  void 
74920 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (*)(sqlite3_cont
74930 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
74940 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64  _value **), void
74950 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e   (*)(sqlite3_con
74960 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  text*));.SQLITE_
74970 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74980 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74  te3ApiExit(sqlit
74990 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51  e3 *db, int);.SQ
749a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
749b0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70   sqlite3OpenTemp
749c0 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a  Database(Parse *
749d0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
749e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
749f0 74 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41  trAccumInit(StrA
74a00 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e  ccum*, char*, in
74a10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
74a20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
74a30 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
74a40 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e  nd(StrAccum*,con
74a50 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53  st char*,int);.S
74a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
74a70 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63  ar *sqlite3StrAc
74a80 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63  cumFinish(StrAcc
74a90 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  um*);.SQLITE_PRI
74aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74ab0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53  3StrAccumReset(S
74ac0 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54  trAccum*);.SQLIT
74ad0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
74ae0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
74af0 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 2a  Init(SelectDest*
74b00 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
74b10 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
74b20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c  sqlite3CreateCol
74b30 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20  umnExpr(sqlite3 
74b40 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 69 6e  *, SrcList *, in
74b50 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45  t, int);..SQLITE
74b60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74b70 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61  lite3BackupResta
74b80 72 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  rt(sqlite3_backu
74b90 70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  p *);.SQLITE_PRI
74ba0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74bb0 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 73 71  3BackupUpdate(sq
74bc0 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20  lite3_backup *, 
74bd0 50 67 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a  Pgno, const u8 *
74be0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e  );../*.** The in
74bf0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c  terface to the L
74c00 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70  EMON-generated p
74c10 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arser.*/.SQLITE_
74c20 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
74c30 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
74c40 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74  (void*(*)(size_t
74c50 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
74c60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
74c70 61 72 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c  arserFree(void*,
74c80 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29   void(*)(void*))
74c90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74ca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
74cb0 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  ser(void*, int, 
74cc0 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a  Token, Parse*);.
74cd0 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
74ce0 58 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49  XSTACKDEPTH.SQLI
74cf0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
74d00 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74   sqlite3ParserSt
74d10 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a  ackPeak(void*);.
74d20 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
74d30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74d40 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e  te3AutoLoadExten
74d50 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b  sions(sqlite3*);
74d60 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
74d70 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
74d80 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
74d90 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
74da0 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73  3CloseExtensions
74db0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73  (sqlite3*);.#els
74dc0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
74dd0 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e  e3CloseExtension
74de0 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  s(X).#endif..#if
74df0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
74e00 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51  _SHARED_CACHE.SQ
74e10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
74e20 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
74e30 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e  Lock(Parse *, in
74e40 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73  t, int, u8, cons
74e50 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65  t char *);.#else
74e60 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
74e70 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c  e3TableLock(v,w,
74e80 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23  x,y,z).#endif..#
74e90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
74ea0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
74eb0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74     int sqlite3Ut
74ec0 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63  f8To8(unsigned c
74ed0 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  har*);.#endif..#
74ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
74ef0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23  T_VIRTUALTABLE.#
74f00 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74f10 56 74 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20  VtabClear(Y).#  
74f20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
74f30 61 62 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49  abSync(X,Y) SQLI
74f40 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20  TE_OK.#  define 
74f50 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
74f60 61 63 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65  ack(X).#  define
74f70 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d   sqlite3VtabComm
74f80 69 74 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20  it(X).#  define 
74f90 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e  sqlite3VtabInSyn
74fa0 63 28 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e  c(db) 0.#  defin
74fb0 65 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63  e sqlite3VtabLoc
74fc0 6b 28 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20  k(X) .#  define 
74fd0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
74fe0 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  k(X).#  define s
74ff0 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
75000 4c 69 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51  List(X).#else.SQ
75010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20  LITE_PRIVATE    
75020 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
75030 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53  Clear(Table*);.S
75040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
75050 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
75060 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62  Sync(sqlite3 *db
75070 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49  , char **);.SQLI
75080 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e  TE_PRIVATE    in
75090 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  t sqlite3VtabRol
750a0 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64  lback(sqlite3 *d
750b0 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  b);.SQLITE_PRIVA
750c0 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65  TE    int sqlite
750d0 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69  3VtabCommit(sqli
750e0 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45  te3 *db);.SQLITE
750f0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64  _PRIVATE    void
75100 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
75110 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49  (VTable *);.SQLI
75120 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f  TE_PRIVATE    vo
75130 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  id sqlite3VtabUn
75140 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a  lock(VTable *);.
75150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75160 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74    void sqlite3Vt
75170 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c  abUnlockList(sql
75180 69 74 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e  ite3*);.#  defin
75190 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53  e sqlite3VtabInS
751a0 79 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e  ync(db) ((db)->n
751b0 56 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29  VTrans>0 && (db)
751c0 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65  ->aVTrans==0).#e
751d0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
751e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
751f0 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65  VtabMakeWritable
75200 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b  (Parse*,Table*);
75210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75220 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
75230 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65  BeginParse(Parse
75240 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
75250 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
75260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75270 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
75280 68 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54  hParse(Parse*, T
75290 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
752a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
752b0 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50  te3VtabArgInit(P
752c0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
752d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
752e0 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
752f0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29  (Parse*, Token*)
75300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75310 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
75320 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74  CallCreate(sqlit
75330 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  e3*, int, const 
75340 63 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29  char *, char **)
75350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75360 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
75370 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73  CallConnect(Pars
75380 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  e*, Table*);.SQL
75390 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
753a0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44  sqlite3VtabCallD
753b0 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c  estroy(sqlite3*,
753c0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72   int, const char
753d0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
753e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
753f0 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33  tabBegin(sqlite3
75400 20 2a 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53   *, VTable *);.S
75410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75  QLITE_PRIVATE Fu
75420 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74  ncDef *sqlite3Vt
75430 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69  abOverloadFuncti
75440 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e  on(sqlite3 *,Fun
75450 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c  cDef*, int nArg,
75460 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
75470 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
75480 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74  ite3InvalidFunct
75490 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ion(sqlite3_cont
754a0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
754b0 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54  _value**);.SQLIT
754c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
754d0 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74  lite3VdbeParamet
754e0 65 72 49 6e 64 65 78 28 56 64 62 65 2a 2c 20 63  erIndex(Vdbe*, c
754f0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
75500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75510 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e   int sqlite3Tran
75520 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c  sferBindings(sql
75530 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c  ite3_stmt *, sql
75540 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51  ite3_stmt *);.SQ
75550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75560 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
75570 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
75580 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75590 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
755a0 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c  ckLength(Parse*,
755b0 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73   ExprList*, cons
755c0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
755d0 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
755e0 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43   *sqlite3BinaryC
755f0 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61  ompareCollSeq(Pa
75600 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45  rse *, Expr *, E
75610 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  xpr *);.SQLITE_P
75620 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
75630 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 63  e3TempInMemory(c
75640 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29 3b 0a  onst sqlite3*);.
75650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
75660 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 47 65  Table *sqlite3Ge
75670 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a  tVTable(sqlite3*
75680 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 2f 2a 20 44  , Table*);../* D
75690 65 63 6c 61 72 61 74 69 6f 6e 73 20 66 6f 72 20  eclarations for 
756a0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 66 6b 65  functions in fke
756b0 79 2e 63 2e 20 41 6c 6c 20 6f 66 20 74 68 65 73  y.c. All of thes
756c0 65 20 61 72 65 20 72 65 70 6c 61 63 65 64 20 62  e are replaced b
756d0 79 0a 2a 2a 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f  y.** no-op macro
756e0 73 20 69 66 20 4f 4d 49 54 5f 46 4f 52 45 49 47  s if OMIT_FOREIG
756f0 4e 5f 4b 45 59 20 69 73 20 64 65 66 69 6e 65 64  N_KEY is defined
75700 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6e  . In this case n
75710 6f 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79  o foreign.** key
75720 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69   functionality i
75730 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 66 20  s available. If 
75740 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 69 73 20  OMIT_TRIGGER is 
75750 64 65 66 69 6e 65 64 20 62 75 74 0a 2a 2a 20 4f  defined but.** O
75760 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20  MIT_FOREIGN_KEY 
75770 69 73 20 6e 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d  is not, only som
75780 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
75790 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 65 64 2e 20  ns are no-oped. 
757a0 49 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73 65 20  In.** this case 
757b0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 72 65  foreign keys are
757c0 20 70 61 72 73 65 64 2c 20 62 75 74 20 6e 6f 20   parsed, but no 
757d0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 61 6c  other functional
757e0 69 74 79 20 69 73 20 0a 2a 2a 20 70 72 6f 76 69  ity is .** provi
757f0 64 65 64 20 28 65 6e 66 6f 72 63 65 6d 65 6e 74  ded (enforcement
75800 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e   of FK constrain
75810 74 73 20 72 65 71 75 69 72 65 73 20 74 68 65 20  ts requires the 
75820 74 72 69 67 67 65 72 73 20 73 75 62 2d 73 79 73  triggers sub-sys
75830 74 65 6d 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  tem)..*/.#if !de
75840 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
75850 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26  T_FOREIGN_KEY) &
75860 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
75870 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
75880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75890 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 43   void sqlite3FkC
758a0 68 65 63 6b 28 50 61 72 73 65 2a 2c 20 54 61 62  heck(Parse*, Tab
758b0 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  le*, int, int);.
758c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
758d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44   void sqlite3FkD
758e0 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  ropTable(Parse*,
758f0 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 61 62 6c   SrcList *, Tabl
75900 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
75910 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75920 65 33 46 6b 41 63 74 69 6f 6e 73 28 50 61 72 73  e3FkActions(Pars
75930 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
75940 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  List*, int);.SQL
75950 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
75960 74 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69  t sqlite3FkRequi
75970 72 65 64 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  red(Parse*, Tabl
75980 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a  e*, int*, int);.
75990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
759a0 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b 4f 6c   u32 sqlite3FkOl
759b0 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c 20 54 61  dmask(Parse*, Ta
759c0 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
759d0 49 56 41 54 45 20 20 20 46 4b 65 79 20 2a 73 71  IVATE   FKey *sq
759e0 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65  lite3FkReference
759f0 73 28 54 61 62 6c 65 20 2a 29 3b 0a 23 65 6c 73  s(Table *);.#els
75a00 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
75a10 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 61 2c 62  te3FkActions(a,b
75a20 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20  ,c,d).  #define 
75a30 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 61  sqlite3FkCheck(a
75a40 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e  ,b,c,d).  #defin
75a50 65 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54  e sqlite3FkDropT
75a60 61 62 6c 65 28 61 2c 62 2c 63 29 0a 20 20 23 64  able(a,b,c).  #d
75a70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 4f  efine sqlite3FkO
75a80 6c 64 6d 61 73 6b 28 61 2c 62 29 20 20 20 20 20  ldmask(a,b)     
75a90 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c   0.  #define sql
75aa0 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 61  ite3FkRequired(a
75ab0 2c 62 2c 63 2c 64 29 20 30 0a 23 65 6e 64 69 66  ,b,c,d) 0.#endif
75ac0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
75ad0 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
75ae0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75af0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b    void sqlite3Fk
75b00 44 65 6c 65 74 65 28 54 61 62 6c 65 2a 29 3b 0a  Delete(Table*);.
75b10 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
75b20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28  sqlite3FkDelete(
75b30 61 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  a).#endif.../*.*
75b40 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 6c  * Available faul
75b50 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68  t injectors.  Sh
75b60 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 64  ould be numbered
75b70 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
75b80 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  0..*/.#define SQ
75b90 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54  LITE_FAULTINJECT
75ba0 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a  OR_MALLOC     0.
75bb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
75bc0 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55  AULTINJECTOR_COU
75bd0 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a  NT      1../*.**
75be0 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   The interface t
75bf0 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 61  o the code in fa
75c00 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 69  ult.c used for i
75c10 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e 69  dentifying "beni
75c20 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  gn".** malloc fa
75c30 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 20  ilures. This is 
75c40 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 20  only present if 
75c50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
75c60 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e  TIN_TEST.** is n
75c70 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  ot defined..*/.#
75c80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
75c90 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
75ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75cb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
75cc0 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76  inBenignMalloc(v
75cd0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
75ce0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
75cf0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
75d00 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a  oc(void);.#else.
75d10 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
75d20 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
75d30 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 73  oc().  #define s
75d40 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
75d50 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a  alloc().#endif..
75d60 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58  #define IN_INDEX
75d70 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 20  _ROWID          
75d80 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e   1.#define IN_IN
75d90 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 20  DEX_EPH         
75da0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 4e      2.#define IN
75db0 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 20  _INDEX_INDEX    
75dc0 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 5f         3.SQLITE_
75dd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
75de0 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50  te3FindInIndex(P
75df0 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20  arse *, Expr *, 
75e00 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53  int*);..#ifdef S
75e10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
75e20 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 45  MIC_WRITE.SQLITE
75e30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
75e40 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
75e50 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
75e60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
75e70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69  qlite3_file *, i
75e80 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
75e90 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
75ea0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
75eb0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29  e(sqlite3_vfs *)
75ec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75ed0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f     int sqlite3Jo
75ee0 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69  urnalCreate(sqli
75ef0 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c  te3_file *);.#el
75f00 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
75f10 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
75f20 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e 73  pVfs) ((pVfs)->s
75f30 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a  zOsFile).#endif.
75f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75f50 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a  void sqlite3MemJ
75f60 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74  ournalOpen(sqlit
75f70 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49  e3_file *);.SQLI
75f80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
75f90 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
75fa0 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  Size(void);.SQLI
75fb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
75fc0 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
75fd0 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al(sqlite3_file 
75fe0 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  *);..#if SQLITE_
75ff0 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30  MAX_EXPR_DEPTH>0
76000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76010 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78    void sqlite3Ex
76020 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73  prSetHeight(Pars
76030 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
76040 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *p);.SQLITE_PRIV
76050 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
76060 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68  3SelectExprHeigh
76070 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 4c  t(Select *);.SQL
76080 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
76090 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65  t sqlite3ExprChe
760a0 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a 2c  ckHeight(Parse*,
760b0 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 23   int);.#else.  #
760c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78  define sqlite3Ex
760d0 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 29  prSetHeight(x,y)
760e0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
760f0 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
76100 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 6e  ht(x) 0.  #defin
76110 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65  e sqlite3ExprChe
76120 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 65  ckHeight(x,y).#e
76130 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
76140 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
76150 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75  Get4byte(const u
76160 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  8*);.SQLITE_PRIV
76170 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
76180 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 33  Put4byte(u8*, u3
76190 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  2);..#ifdef SQLI
761a0 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b  TE_ENABLE_UNLOCK
761b0 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f 50  _NOTIFY.SQLITE_P
761c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
761d0 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
761e0 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
761f0 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51  , sqlite3 *);.SQ
76200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
76210 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
76220 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71  ctionUnlocked(sq
76230 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49  lite3 *db);.SQLI
76240 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
76250 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
76260 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 65  ionClosed(sqlite
76270 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20 20  3 *db);.#else.  
76280 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43  #define sqlite3C
76290 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
762a0 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20  (x,y).  #define 
762b0 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
762c0 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20 23  nUnlocked(x).  #
762d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
762e0 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 78  nnectionClosed(x
762f0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ).#endif..#ifdef
76300 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
76310 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
76320 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
76330 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68  rTrace(FILE*, ch
76340 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ar *);.#endif../
76350 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
76360 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43  TE_ENABLE IOTRAC
76370 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68  E exists then th
76380 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  e global variabl
76390 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72  e.** sqlite3IoTr
763a0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
763b0 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b   to a printf-lik
763c0 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74  e routine used t
763d0 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74  o.** print I/O t
763e0 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e  racing messages.
763f0 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49   .*/.#ifdef SQLI
76400 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
76410 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41  E.# define IOTRA
76420 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74  CE(A)  if( sqlit
76430 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c  e3IoTrace ){ sql
76440 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d  ite3IoTrace A; }
76450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76460 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
76470 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62  beIOTraceSql(Vdb
76480 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
76490 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74  ATE void (*sqlit
764a0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74  e3IoTrace)(const
764b0 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c   char*,...);.#el
764c0 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52  se.# define IOTR
764d0 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20  ACE(A).# define 
764e0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
764f0 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a  ceSql(X).#endif.
76500 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
76510 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
76520 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
76530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76550 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
76560 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
76570 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a  ile global.c ***
76580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
765a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
765b0 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a  008 June 13.**.*
765c0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
765d0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
765e0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
765f0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
76600 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
76610 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
76620 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
76630 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
76640 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
76650 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
76660 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
76670 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
76680 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
76690 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
766a0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
766b0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
766c0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
766d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
766e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
766f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
76720 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
76730 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20  ins definitions 
76740 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
76750 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73  les and contants
76760 2e 0a 2a 2f 0a 0a 2f 2a 20 41 6e 20 61 72 72 61  ..*/../* An arra
76770 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70  y to map all upp
76780 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65  er-case characte
76790 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20 63 6f  rs into their co
767a0 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c  rresponding.** l
767b0 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72 61 63  ower-case charac
767c0 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ter. .**.** SQLi
767d0 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72  te only consider
767e0 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72 20 45  s US-ASCII (or E
767f0 42 43 44 49 43 29 20 63 68 61 72 61 63 74 65 72  BCDIC) character
76800 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a  s.  We do not.**
76810 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e   handle case con
76820 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74 68 65  versions for the
76830 20 55 54 46 20 63 68 61 72 61 63 74 65 72 20 73   UTF character s
76840 65 74 20 73 69 6e 63 65 20 74 68 65 20 74 61 62  et since the tab
76850 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20  les.** involved 
76860 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20 62 69  are nearly as bi
76870 67 20 6f 72 20 62 69 67 67 65 72 20 74 68 61 6e  g or bigger than
76880 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a   SQLite itself..
76890 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
768a0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
768b0 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70   char sqlite3Upp
768c0 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a  erToLower[] = {.
768d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
768e0 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 31 2c  CII.      0,  1,
768f0 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c    2,  3,  4,  5,
76900 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c    6,  7,  8,  9,
76910 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c   10, 11, 12, 13,
76920 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c   14, 15, 16, 17,
76930 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 32 30  .     18, 19, 20
76940 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34  , 21, 22, 23, 24
76950 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38  , 25, 26, 27, 28
76960 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 33 32  , 29, 30, 31, 32
76970 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20  , 33, 34, 35,.  
76980 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33     36, 37, 38, 3
76990 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34  9, 40, 41, 42, 4
769a0 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34  3, 44, 45, 46, 4
769b0 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35  7, 48, 49, 50, 5
769c0 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 20 20  1, 52, 53,.     
769d0 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20  54, 55, 56, 57, 
769e0 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20  58, 59, 60, 61, 
769f0 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c 20  62, 63, 64, 97, 
76a00 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31  98, 99,100,101,1
76a10 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 34 2c  02,103,.    104,
76a20 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c  105,106,107,108,
76a30 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c  109,110,111,112,
76a40 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c  113,114,115,116,
76a50 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c  117,118,119,120,
76a60 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 39 31  121,.    122, 91
76a70 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35  , 92, 93, 94, 95
76a80 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 39 39  , 96, 97, 98, 99
76a90 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33  ,100,101,102,103
76aa0 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37  ,104,105,106,107
76ab0 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 31 31  ,.    108,109,11
76ac0 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31  0,111,112,113,11
76ad0 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31  4,115,116,117,11
76ae0 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32  8,119,120,121,12
76af0 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20  2,123,124,125,. 
76b00 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 2c 31     126,127,128,1
76b10 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31  29,130,131,132,1
76b20 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31  33,134,135,136,1
76b30 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31  37,138,139,140,1
76b40 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20 20  41,142,143,.    
76b50 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c  144,145,146,147,
76b60 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c  148,149,150,151,
76b70 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c  152,153,154,155,
76b80 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c  156,157,158,159,
76b90 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 36 32  160,161,.    162
76ba0 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36  ,163,164,165,166
76bb0 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30  ,167,168,169,170
76bc0 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 34  ,171,172,173,174
76bd0 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 38  ,175,176,177,178
76be0 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 31 38  ,179,.    180,18
76bf0 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38  1,182,183,184,18
76c00 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38  5,186,187,188,18
76c10 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 39  9,190,191,192,19
76c20 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 39  3,194,195,196,19
76c30 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 2c 32  7,.    198,199,2
76c40 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c 32  00,201,202,203,2
76c50 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 32  04,205,206,207,2
76c60 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c 32  08,209,210,211,2
76c70 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a  12,213,214,215,.
76c80 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 38 2c      216,217,218,
76c90 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c  219,220,221,222,
76ca0 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c  223,224,225,226,
76cb0 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c  227,228,229,230,
76cc0 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20  231,232,233,.   
76cd0 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 33 37   234,235,236,237
76ce0 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 34 31  ,238,239,240,241
76cf0 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35  ,242,243,244,245
76d00 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39  ,246,247,248,249
76d10 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 32 35  ,250,251,.    25
76d20 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 23 65  2,253,254,255.#e
76d30 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
76d40 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20 20  TE_EBCDIC.      
76d50 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20  0,  1,  2,  3,  
76d60 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20  4,  5,  6,  7,  
76d70 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31  8,  9, 10, 11, 1
76d80 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f  2, 13, 14, 15, /
76d90 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c  * 0x */.     16,
76da0 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c   17, 18, 19, 20,
76db0 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c   21, 22, 23, 24,
76dc0 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c   25, 26, 27, 28,
76dd0 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20   29, 30, 31, /* 
76de0 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33  1x */.     32, 3
76df0 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 20 33  3, 34, 35, 36, 3
76e00 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34  7, 38, 39, 40, 4
76e10 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34  1, 42, 43, 44, 4
76e20 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78  5, 46, 47, /* 2x
76e30 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 39 2c   */.     48, 49,
76e40 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c   50, 51, 52, 53,
76e50 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c   54, 55, 56, 57,
76e60 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c   58, 59, 60, 61,
76e70 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a   62, 63, /* 3x *
76e80 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c 20 36  /.     64, 65, 6
76e90 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37  6, 67, 68, 69, 7
76ea0 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 20 37  0, 71, 72, 73, 7
76eb0 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 20 37  4, 75, 76, 77, 7
76ec0 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a  8, 79, /* 4x */.
76ed0 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 32 2c       80, 81, 82,
76ee0 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c   83, 84, 85, 86,
76ef0 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c   87, 88, 89, 90,
76f00 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c   91, 92, 93, 94,
76f10 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20   95, /* 5x */.  
76f20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 20 36     96, 97, 66, 6
76f30 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37  7, 68, 69, 70, 7
76f40 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 31 30  1, 72, 73,106,10
76f50 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
76f60 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20  1, /* 6x */.    
76f70 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c  112, 81, 82, 83,
76f80 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c   84, 85, 86, 87,
76f90 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c   88, 89,122,123,
76fa0 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c  124,125,126,127,
76fb0 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31 32   /* 7x */.    12
76fc0 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33  8,129,130,131,13
76fd0 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33  2,133,134,135,13
76fe0 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34  6,137,138,139,14
76ff0 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f  0,141,142,143, /
77000 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c  * 8x */.    144,
77010 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c  145,146,147,148,
77020 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c  149,150,151,152,
77030 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c  153,154,155,156,
77040 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20  157,156,159, /* 
77050 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36  9x */.    160,16
77060 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36  1,162,163,164,16
77070 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36  5,166,167,168,16
77080 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31 34  9,170,171,140,14
77090 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78  1,142,175, /* Ax
770a0 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 37 2c   */.    176,177,
770b0 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c  178,179,180,181,
770c0 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c  182,183,184,185,
770d0 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c  186,187,188,189,
770e0 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a  190,191, /* Bx *
770f0 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c 31 33  /.    192,129,13
77100 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33  0,131,132,133,13
77110 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 32 30  4,135,136,137,20
77120 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30  2,203,204,205,20
77130 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a  6,207, /* Cx */.
77140 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 36 2c      208,145,146,
77150 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c  147,148,149,150,
77160 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c  151,152,153,218,
77170 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c  219,220,221,222,
77180 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20  223, /* Dx */.  
77190 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 31 36    224,225,162,16
771a0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36  3,164,165,166,16
771b0 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 32 30  7,168,169,232,20
771c0 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30  3,204,205,206,20
771d0 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20  7, /* Ex */.    
771e0 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c  239,240,241,242,
771f0 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c  243,244,245,246,
77200 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c  247,248,249,219,
77210 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c  220,221,222,255,
77220 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66   /* Fx */.#endif
77230 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
77240 6f 6c 6c 6f 77 69 6e 67 20 32 35 36 20 62 79 74  ollowing 256 byt
77250 65 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69  e lookup table i
77260 73 20 75 73 65 64 20 74 6f 20 73 75 70 70 6f 72  s used to suppor
77270 74 20 53 51 4c 69 74 65 73 20 62 75 69 6c 74 2d  t SQLites built-
77280 69 6e 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  in.** equivalent
77290 73 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s to the followi
772a0 6e 67 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72  ng standard libr
772b0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a  ary functions:.*
772c0 2a 0a 2a 2a 20 20 20 69 73 73 70 61 63 65 28 29  *.**   isspace()
772d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
772e0 20 20 20 20 20 20 20 20 30 78 30 31 0a 2a 2a 20          0x01.** 
772f0 20 20 69 73 61 6c 70 68 61 28 29 20 20 20 20 20    isalpha()     
77300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77310 20 20 20 30 78 30 32 0a 2a 2a 20 20 20 69 73 64     0x02.**   isd
77320 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20  igit()          
77330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
77340 30 34 0a 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28  04.**   isalnum(
77350 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
77360 20 20 20 20 20 20 20 20 20 30 78 30 36 0a 2a 2a           0x06.**
77370 20 20 20 69 73 78 64 69 67 69 74 28 29 20 20 20     isxdigit()   
77380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77390 20 20 20 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f      0x08.**   to
773a0 75 70 70 65 72 28 29 20 20 20 20 20 20 20 20 20  upper()         
773b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
773c0 78 32 30 0a 2a 2a 20 20 20 53 51 4c 69 74 65 20  x20.**   SQLite 
773d0 69 64 65 6e 74 69 66 69 65 72 20 63 68 61 72 61  identifier chara
773e0 63 74 65 72 20 20 20 20 20 20 30 78 34 30 0a 2a  cter      0x40.*
773f0 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73  *.** Bit 0x20 is
77400 20 73 65 74 20 69 66 20 74 68 65 20 6d 61 70 70   set if the mapp
77410 65 64 20 63 68 61 72 61 63 74 65 72 20 72 65 71  ed character req
77420 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f  uires translatio
77430 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61  n to upper.** ca
77440 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20  se. i.e. if the 
77450 63 68 61 72 61 63 74 65 72 20 69 73 20 61 20 6c  character is a l
77460 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20  ower-case ASCII 
77470 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66  character..** If
77480 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61   x is a lower-ca
77490 73 65 20 41 53 43 49 49 20 63 68 61 72 61 63 74  se ASCII charact
774a0 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70  er, then its upp
774b0 65 72 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65  er-case equivale
774c0 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78  nt.** is (x - 0x
774d0 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74  20). Therefore t
774e0 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62 65 20  oupper() can be 
774f0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a  implemented as:.
77500 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d  **.**   (x & ~(m
77510 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a  ap[x]&0x20)).**.
77520 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63  ** Standard func
77530 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69  tion tolower() i
77540 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73  s implemented us
77550 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 55  ing the sqlite3U
77560 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a  pperToLower[].**
77570 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28   array. tolower(
77580 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f  ) is used more o
77590 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65  ften than touppe
775a0 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  r() by SQLite..*
775b0 2a 0a 2a 2a 20 42 69 74 20 30 78 34 30 20 69 73  *.** Bit 0x40 is
775c0 20 73 65 74 20 69 66 20 74 68 65 20 63 68 61 72   set if the char
775d0 61 63 74 65 72 20 6e 6f 6e 2d 61 6c 70 68 61 6e  acter non-alphan
775e0 75 6d 65 72 69 63 20 61 6e 64 20 63 61 6e 20 62  umeric and can b
775f0 65 20 75 73 65 64 20 69 6e 20 61 6e 20 0a 2a 2a  e used in an .**
77600 20 53 51 4c 69 74 65 20 69 64 65 6e 74 69 66 69   SQLite identifi
77610 65 72 2e 20 20 49 64 65 6e 74 69 66 69 65 72 73  er.  Identifiers
77620 20 61 72 65 20 61 6c 70 68 61 6e 75 6d 65 72 69   are alphanumeri
77630 63 73 2c 20 22 5f 22 2c 20 22 24 22 2c 20 61 6e  cs, "_", "$", an
77640 64 20 61 6e 79 0a 2a 2a 20 6e 6f 6e 2d 41 53 43  d any.** non-ASC
77650 49 49 20 55 54 46 20 63 68 61 72 61 63 74 65 72  II UTF character
77660 2e 20 48 65 6e 63 65 20 74 68 65 20 74 65 73 74  . Hence the test
77670 20 66 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20   for whether or 
77680 6e 6f 74 20 61 20 63 68 61 72 61 63 74 65 72 20  not a character 
77690 69 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e  is.** part of an
776a0 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 30   identifier is 0
776b0 78 34 36 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  x46..**.** SQLit
776c0 65 27 73 20 76 65 72 73 69 6f 6e 73 20 61 72 65  e's versions are
776d0 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68   identical to th
776e0 65 20 73 74 61 6e 64 61 72 64 20 76 65 72 73 69  e standard versi
776f0 6f 6e 73 20 61 73 73 75 6d 69 6e 67 20 61 0a 2a  ons assuming a.*
77700 2a 20 6c 6f 63 61 6c 65 20 6f 66 20 22 43 22 2e  * locale of "C".
77710 20 54 68 65 79 20 61 72 65 20 69 6d 70 6c 65 6d   They are implem
77720 65 6e 74 65 64 20 61 73 20 6d 61 63 72 6f 73 20  ented as macros 
77730 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a  in sqliteInt.h..
77740 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
77750 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52  _ASCII.SQLITE_PR
77760 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69  IVATE const unsi
77770 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65  gned char sqlite
77780 33 43 74 79 70 65 4d 61 70 5b 32 35 36 5d 20 3d  3CtypeMap[256] =
77790 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c   {.  0x00, 0x00,
777a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
777b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
777c0 78 30 30 2c 20 20 2f 2a 20 30 30 2e 2e 30 37 20  x00,  /* 00..07 
777d0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
777e0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
777f0 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30  1, 0x01, 0x01, 0
77800 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x01, 0x00, 0x00,
77810 20 20 2f 2a 20 30 38 2e 2e 30 66 20 20 20 20 2e    /* 08..0f    .
77820 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
77830 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77840 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77850 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
77860 20 31 30 2e 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e   10..17    .....
77870 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
77880 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77890 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
778a0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 38 2e  0, 0x00,  /* 18.
778b0 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .1f    ........ 
778c0 2a 2f 0a 20 20 30 78 30 31 2c 20 30 78 30 30 2c  */.  0x01, 0x00,
778d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 34   0x00, 0x00, 0x4
778e0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
778f0 78 30 30 2c 20 20 2f 2a 20 32 30 2e 2e 32 37 20  x00,  /* 20..27 
77900 20 20 20 20 21 22 23 24 25 26 27 20 2a 2f 0a 20      !"#$%&' */. 
77910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77920 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77930 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77940 20 20 2f 2a 20 32 38 2e 2e 32 66 20 20 20 20 28    /* 28..2f    (
77950 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30  )*+,-./ */.  0x0
77960 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  c, 0x0c, 0x0c, 0
77970 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
77980 20 30 78 30 63 2c 20 30 78 30 63 2c 20 20 2f 2a   0x0c, 0x0c,  /*
77990 20 33 30 2e 2e 33 37 20 20 20 20 30 31 32 33 34   30..37    01234
779a0 35 36 37 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30  567 */.  0x0c, 0
779b0 78 30 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x0c, 0x00, 0x00,
779c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
779d0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 33 38 2e  0, 0x00,  /* 38.
779e0 2e 33 66 20 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20  .3f    89:;<=>? 
779f0 2a 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 61  */..  0x00, 0x0a
77a00 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78  , 0x0a, 0x0a, 0x
77a10 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  0a, 0x0a, 0x0a, 
77a20 30 78 30 32 2c 20 20 2f 2a 20 34 30 2e 2e 34 37  0x02,  /* 40..47
77a30 20 20 20 20 40 41 42 43 44 45 46 47 20 2a 2f 0a      @ABCDEFG */.
77a40 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78    0x02, 0x02, 0x
77a50 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
77a60 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77a70 2c 20 20 2f 2a 20 34 38 2e 2e 34 66 20 20 20 20  ,  /* 48..4f    
77a80 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78  HIJKLMNO */.  0x
77a90 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
77aa0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77ab0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f  , 0x02, 0x02,  /
77ac0 2a 20 35 30 2e 2e 35 37 20 20 20 20 50 51 52 53  * 50..57    PQRS
77ad0 54 55 56 57 20 2a 2f 0a 20 20 30 78 30 32 2c 20  TUVW */.  0x02, 
77ae0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30  0x02, 0x02, 0x00
77af0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77b00 30 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 35 38  00, 0x40,  /* 58
77b10 2e 2e 35 66 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f  ..5f    XYZ[\]^_
77b20 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 32 61   */.  0x00, 0x2a
77b30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78  , 0x2a, 0x2a, 0x
77b40 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  2a, 0x2a, 0x2a, 
77b50 30 78 32 32 2c 20 20 2f 2a 20 36 30 2e 2e 36 37  0x22,  /* 60..67
77b60 20 20 20 20 60 61 62 63 64 65 66 67 20 2a 2f 0a      `abcdefg */.
77b70 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78    0x22, 0x22, 0x
77b80 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
77b90 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77ba0 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 20 20 20 20  ,  /* 68..6f    
77bb0 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78  hijklmno */.  0x
77bc0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
77bd0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77be0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f  , 0x22, 0x22,  /
77bf0 2a 20 37 30 2e 2e 37 37 20 20 20 20 70 71 72 73  * 70..77    pqrs
77c00 74 75 76 77 20 2a 2f 0a 20 20 30 78 32 32 2c 20  tuvw */.  0x22, 
77c10 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 30 30  0x22, 0x22, 0x00
77c20 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77c30 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 37 38  00, 0x00,  /* 78
77c40 2e 2e 37 66 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e  ..7f    xyz{|}~.
77c50 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30 78 34   */..  0x40, 0x4
77c60 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77c70 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77c80 20 30 78 34 30 2c 20 20 2f 2a 20 38 30 2e 2e 38   0x40,  /* 80..8
77c90 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
77ca0 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  .  0x40, 0x40, 0
77cb0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77cc0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77cd0 30 2c 20 20 2f 2a 20 38 38 2e 2e 38 66 20 20 20  0,  /* 88..8f   
77ce0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77cf0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77d00 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77d10 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
77d20 2f 2a 20 39 30 2e 2e 39 37 20 20 20 20 2e 2e 2e  /* 90..97    ...
77d30 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c  ..... */.  0x40,
77d40 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77d50 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77d60 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 39  x40, 0x40,  /* 9
77d70 38 2e 2e 39 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..9f    .......
77d80 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34  . */.  0x40, 0x4
77d90 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77da0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77db0 20 30 78 34 30 2c 20 20 2f 2a 20 61 30 2e 2e 61   0x40,  /* a0..a
77dc0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
77dd0 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  .  0x40, 0x40, 0
77de0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77df0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77e00 30 2c 20 20 2f 2a 20 61 38 2e 2e 61 66 20 20 20  0,  /* a8..af   
77e10 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77e20 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77e30 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77e40 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
77e50 2f 2a 20 62 30 2e 2e 62 37 20 20 20 20 2e 2e 2e  /* b0..b7    ...
77e60 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c  ..... */.  0x40,
77e70 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77e80 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77e90 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 62  x40, 0x40,  /* b
77ea0 38 2e 2e 62 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..bf    .......
77eb0 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30 78  . */..  0x40, 0x
77ec0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77ed0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77ee0 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 30 2e 2e  , 0x40,  /* c0..
77ef0 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  c7    ........ *
77f00 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20  /.  0x40, 0x40, 
77f10 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77f20 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77f30 34 30 2c 20 20 2f 2a 20 63 38 2e 2e 63 66 20 20  40,  /* c8..cf  
77f40 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
77f50 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
77f60 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77f70 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77f80 20 2f 2a 20 64 30 2e 2e 64 37 20 20 20 20 2e 2e   /* d0..d7    ..
77f90 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30  ...... */.  0x40
77fa0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
77fb0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
77fc0 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20  0x40, 0x40,  /* 
77fd0 64 38 2e 2e 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e  d8..df    ......
77fe0 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78  .. */.  0x40, 0x
77ff0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78000 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
78010 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 30 2e 2e  , 0x40,  /* e0..
78020 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  e7    ........ *
78030 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20  /.  0x40, 0x40, 
78040 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
78050 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
78060 34 30 2c 20 20 2f 2a 20 65 38 2e 2e 65 66 20 20  40,  /* e8..ef  
78070 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
78080 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
78090 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
780a0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
780b0 20 2f 2a 20 66 30 2e 2e 66 37 20 20 20 20 2e 2e   /* f0..f7    ..
780c0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30  ...... */.  0x40
780d0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
780e0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
780f0 30 78 34 30 2c 20 30 78 34 30 20 20 20 2f 2a 20  0x40, 0x40   /* 
78100 66 38 2e 2e 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e  f8..ff    ......
78110 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a  .. */.};.#endif.
78120 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  .../*.** The fol
78130 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e  lowing singleton
78140 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 67 6c   contains the gl
78150 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  obal configurati
78160 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51  on for.** the SQ
78170 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  Lite library..*/
78180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78190 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
781a0 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
781b0 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 3d 20  sqlite3Config = 
781c0 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41  {.   SQLITE_DEFA
781d0 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20  ULT_MEMSTATUS,  
781e0 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20  /* bMemstat */. 
781f0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
78200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
78210 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20  bCoreMutex */.  
78220 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
78230 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62  FE==1,      /* b
78240 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20  FullMutex */.   
78250 30 78 37 66 66 66 66 66 66 65 2c 20 20 20 20 20  0x7ffffffe,     
78260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78             /* mx
78270 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31 30 30  Strlen */.   100
78280 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
78290 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f          /* szLoo
782a0 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35 30 30  kaside */.   500
782b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
782c0 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b          /* nLook
782d0 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30 2c 30  aside */.   {0,0
782e0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20  ,0,0,0,0,0,0},  
782f0 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20         /* m */. 
78300 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30    {0,0,0,0,0,0,0
78310 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20  ,0,0},       /* 
78320 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30 2c 30  mutex */.   {0,0
78330 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  ,0,0,0,0,0,0,0,0
78340 2c 30 7d 2c 20 20 20 2f 2a 20 70 63 61 63 68 65  ,0},   /* pcache
78350 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c   */.   (void*)0,
78360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78370 20 20 2f 2a 20 70 48 65 61 70 20 2a 2f 0a 20 20    /* pHeap */.  
78380 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
78390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
783a0 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 30 2c  Heap */.   0, 0,
783b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
783c0 20 20 20 20 20 20 2f 2a 20 6d 6e 48 65 61 70 2c        /* mnHeap,
783d0 20 6d 78 48 65 61 70 20 2a 2f 0a 20 20 20 28 76   mxHeap */.   (v
783e0 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20  oid*)0,         
783f0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 53 63 72           /* pScr
78400 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20  atch */.   0,   
78410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78420 20 20 20 20 20 20 2f 2a 20 73 7a 53 63 72 61 74        /* szScrat
78430 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ch */.   0,     
78440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78450 20 20 20 20 2f 2a 20 6e 53 63 72 61 74 63 68 20      /* nScratch 
78460 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20  */.   (void*)0, 
78470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78480 20 2f 2a 20 70 50 61 67 65 20 2a 2f 0a 20 20 20   /* pPage */.   
78490 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
784a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
784b0 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Page */.   0,   
784c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
784d0 20 20 20 20 20 20 2f 2a 20 6e 50 61 67 65 20 2a        /* nPage *
784e0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
784f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78500 2f 2a 20 6d 78 50 61 72 73 65 72 53 74 61 63 6b  /* mxParserStack
78510 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78530 20 20 2f 2a 20 73 68 61 72 65 64 43 61 63 68 65    /* sharedCache
78540 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a  Enabled */.   /*
78550 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20 73 68   All the rest sh
78560 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 69  ould always be i
78570 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65  nitialized to ze
78580 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ro */.   0,     
78590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
785a0 20 20 20 20 2f 2a 20 69 73 49 6e 69 74 20 2a 2f      /* isInit */
785b0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
785c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
785d0 2a 20 69 6e 50 72 6f 67 72 65 73 73 20 2a 2f 0a  * inProgress */.
785e0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
785f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78600 20 69 73 4d 75 74 65 78 49 6e 69 74 20 2a 2f 0a   isMutexInit */.
78610 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
78620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78630 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f   isMallocInit */
78640 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
78650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
78660 2a 20 69 73 50 43 61 63 68 65 49 6e 69 74 20 2a  * isPCacheInit *
78670 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
78680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78690 2f 2a 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f  /* pInitMutex */
786a0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
786b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
786c0 2a 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 20  * nRefInitMutex 
786d0 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61  */.};.../*.** Ha
786e0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 67 6c 6f  sh table for glo
786f0 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 2d 20  bal functions - 
78700 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e  functions common
78710 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74 61 62   to all.** datab
78720 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
78730 20 20 41 66 74 65 72 20 69 6e 69 74 69 61 6c 69    After initiali
78740 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20 74 61 62  zation, this tab
78750 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e  le is.** read-on
78760 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
78770 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44  IVATE SQLITE_WSD
78780 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c   FuncDefHash sql
78790 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
787a0 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ons;../*.** The 
787b0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 70 65  value of the "pe
787c0 6e 64 69 6e 67 22 20 62 79 74 65 20 6d 75 73 74  nding" byte must
787d0 20 62 65 20 30 78 34 30 30 30 30 30 30 30 20 28   be 0x40000000 (
787e0 31 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a  1 byte past the.
787f0 2a 2a 20 31 2d 67 69 62 61 62 79 74 65 20 62 6f  ** 1-gibabyte bo
78800 75 6e 64 61 72 79 29 20 69 6e 20 61 20 63 6f 6d  undary) in a com
78810 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65  patible database
78820 2e 20 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20  .  SQLite never 
78830 75 73 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  uses.** the data
78840 62 61 73 65 20 70 61 67 65 20 74 68 61 74 20 63  base page that c
78850 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 65 6e 64  ontains the pend
78860 69 6e 67 20 62 79 74 65 2e 20 20 49 74 20 6e 65  ing byte.  It ne
78870 76 65 72 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20  ver attempts.** 
78880 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  to read or write
78890 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 65   that page.  The
788a0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61   pending byte pa
788b0 67 65 20 69 73 20 73 65 74 20 61 73 73 69 67 6e  ge is set assign
788c0 0a 2a 2a 20 66 6f 72 20 75 73 65 20 62 79 20 74  .** for use by t
788d0 68 65 20 56 46 53 20 6c 61 79 65 72 73 20 61 73  he VFS layers as
788e0 20 73 70 61 63 65 20 66 6f 72 20 6d 61 6e 61 67   space for manag
788f0 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a  ing file locks..
78900 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 65 73  **.** During tes
78910 74 69 6e 67 2c 20 69 74 20 69 73 20 6f 66 74 65  ting, it is ofte
78920 6e 20 64 65 73 69 72 61 62 6c 65 20 74 6f 20 6d  n desirable to m
78930 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ove the pending 
78940 62 79 74 65 20 74 6f 0a 2a 2a 20 61 20 64 69 66  byte to.** a dif
78950 66 65 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20  ferent position 
78960 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68  in the file.  Th
78970 69 73 20 61 6c 6c 6f 77 73 20 63 6f 64 65 20 74  is allows code t
78980 68 61 74 20 68 61 73 20 74 6f 0a 2a 2a 20 64 65  hat has to.** de
78990 61 6c 20 77 69 74 68 20 74 68 65 20 70 65 6e 64  al with the pend
789a0 69 6e 67 20 62 79 74 65 20 74 6f 20 72 75 6e 20  ing byte to run 
789b0 6f 6e 20 66 69 6c 65 73 20 74 68 61 74 20 61 72  on files that ar
789c0 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 0a 2a  e much smaller.*
789d0 2a 20 74 68 61 6e 20 31 20 47 69 42 2e 20 20 54  * than 1 GiB.  T
789e0 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  he sqlite3_test_
789f0 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66  control() interf
78a00 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ace can be used 
78a10 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 20 70  to.** move the p
78a20 65 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a  ending byte..**.
78a30 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43  ** IMPORTANT:  C
78a40 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64  hanging the pend
78a50 69 6e 67 20 62 79 74 65 20 74 6f 20 61 6e 79 20  ing byte to any 
78a60 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e  value other than
78a70 0a 2a 2a 20 30 78 34 30 30 30 30 30 30 30 20 72  .** 0x40000000 r
78a80 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e 63  esults in an inc
78a90 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61  ompatible databa
78aa0 73 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 21 0a  se file format!.
78ab0 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20  ** Changing the 
78ac0 70 65 6e 64 69 6e 67 20 62 79 74 65 20 64 75 72  pending byte dur
78ad0 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 72 65  ing operating re
78ae0 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e  sults in undefin
78af0 65 64 0a 2a 2a 20 61 6e 64 20 64 69 6c 65 74 65  ed.** and dilete
78b00 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72 2e 0a  rious behavior..
78b10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
78b20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e  E int sqlite3Pen
78b30 64 69 6e 67 42 79 74 65 20 3d 20 30 78 34 30 30  dingByte = 0x400
78b40 30 30 30 30 30 3b 0a 0a 2f 2a 0a 2a 2a 20 50 72  00000;../*.** Pr
78b50 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f  operties of opco
78b60 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f  des.  The OPFLG_
78b70 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72  INITIALIZER macr
78b80 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20  o is.** created 
78b90 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b  by mkopcodeh.awk
78ba0 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74   during compilat
78bb0 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62  ion.  Data is ob
78bc0 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74  tained.** from t
78bd0 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c  he comments foll
78be0 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20  owing the "case 
78bf0 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d  OP_xxxx:" statem
78c00 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 76  ents in.** the v
78c10 64 62 65 2e 63 20 66 69 6c 65 2e 20 20 0a 2a 2f  dbe.c file.  .*/
78c20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78c30 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
78c40 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 6f 64  har sqlite3Opcod
78c50 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50  eProperty[] = OP
78c60 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b  FLG_INITIALIZER;
78c70 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
78c80 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e  * End of global.
78c90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
78ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78cc0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
78cd0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61  * Begin file sta
78ce0 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tus.c **********
78cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78d10 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e  /./*.** 2008 Jun
78d20 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  e 18.**.** The a
78d30 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
78d40 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
78d50 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
78d60 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
78d70 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
78d80 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
78d90 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
78da0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
78db0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
78dc0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
78dd0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
78de0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
78df0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
78e00 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
78e10 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
78e20 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
78e30 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
78e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e80 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
78e90 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  odule implements
78ea0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61   the sqlite3_sta
78eb0 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20  tus() interface 
78ec0 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66  and related.** f
78ed0 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2f  unctionality..*/
78ee0 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65  ../*.** Variable
78ef0 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65  s in which to re
78f00 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f  cord status info
78f10 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65  rmation..*/.type
78f20 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
78f30 65 33 53 74 61 74 54 79 70 65 20 73 71 6c 69 74  e3StatType sqlit
78f40 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74  e3StatType;.stat
78f50 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
78f60 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74  ruct sqlite3Stat
78f70 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77  Type {.  int now
78f80 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20  Value[9];       
78f90 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c    /* Current val
78fa0 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61  ue */.  int mxVa
78fb0 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20  lue[9];         
78fc0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75   /* Maximum valu
78fd0 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74  e */.} sqlite3St
78fe0 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c  at = { {0,}, {0,
78ff0 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77  } };.../* The "w
79000 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69  sdStat" macro wi
79010 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
79020 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  e status informa
79030 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65  tion.** state ve
79040 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62  ctor.  If writab
79050 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
79060 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
79070 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20   the target,.** 
79080 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
79090 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
790a0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
790b0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
790c0 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
790d0 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
790e0 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
790f0 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e  ted, wsdStat can
79100 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a   refer directly.
79110 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74  ** to the "sqlit
79120 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65  e3Stat" state ve
79130 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62  ctor declared ab
79140 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ove..*/.#ifdef S
79150 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23  QLITE_OMIT_WSD.#
79160 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49   define wsdStatI
79170 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74  nit  sqlite3Stat
79180 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41  Type *x = &GLOBA
79190 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70  L(sqlite3StatTyp
791a0 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23  e,sqlite3Stat).#
791b0 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20   define wsdStat 
791c0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
791d0 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a  ine wsdStatInit.
791e0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
791f0 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e   sqlite3Stat.#en
79200 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
79210 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  n the current va
79220 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20  lue of a status 
79230 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51  parameter..*/.SQ
79240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
79250 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61   sqlite3StatusVa
79260 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77  lue(int op){.  w
79270 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
79280 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
79290 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
792a0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
792b0 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61  .  return wsdSta
792c0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a  t.nowValue[op];.
792d0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74  }../*.** Add N t
792e0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  o the value of a
792f0 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20   status record. 
79300 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
79310 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65  hat the.** calle
79320 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69  r holds appropri
79330 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51  ate locks..*/.SQ
79340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
79350 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  d sqlite3StatusA
79360 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e  dd(int op, int N
79370 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74  ){.  wsdStatInit
79380 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d  ;.  assert( op>=
79390 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a  0 && op<ArraySiz
793a0 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
793b0 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74  ue) );.  wsdStat
793c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d  .nowValue[op] +=
793d0 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61   N;.  if( wsdSta
793e0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77  t.nowValue[op]>w
793f0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
79400 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61  p] ){.    wsdSta
79410 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20  t.mxValue[op] = 
79420 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
79430 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  [op];.  }.}../*.
79440 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ** Set the value
79450 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20   of a status to 
79460 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  X..*/.SQLITE_PRI
79470 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
79480 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f  3StatusSet(int o
79490 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64  p, int X){.  wsd
794a0 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65  StatInit;.  asse
794b0 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c  rt( op>=0 && op<
794c0 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61  ArraySize(wsdSta
794d0 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20  t.nowValue) );. 
794e0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
794f0 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28  e[op] = X;.  if(
79500 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
79510 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78  e[op]>wsdStat.mx
79520 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20  Value[op] ){.   
79530 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65   wsdStat.mxValue
79540 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e  [op] = wsdStat.n
79550 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d  owValue[op];.  }
79560 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
79570 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
79580 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  on..**.** This i
79590 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73  mplementation as
795a0 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69  sumes that readi
795b0 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e  ng or writing an
795c0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62   aligned.** 32-b
795d0 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e  it integer is an
795e0 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f   atomic operatio
795f0 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75  n.  If that assu
79600 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72  mption is not tr
79610 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73  ue,.** then this
79620 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20   routine is not 
79630 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53  threadsafe..*/.S
79640 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
79650 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74  lite3_status(int
79660 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65   op, int *pCurre
79670 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61  nt, int *pHighwa
79680 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c  ter, int resetFl
79690 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e  ag){.  wsdStatIn
796a0 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c  it;.  if( op<0 |
796b0 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28  | op>=ArraySize(
796c0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
796d0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
796e0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
796f0 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 3d   }.  *pCurrent =
79700 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
79710 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68 77  e[op];.  *pHighw
79720 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e 6d  ater = wsdStat.m
79730 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69 66  xValue[op];.  if
79740 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20  ( resetFlag ){. 
79750 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c     wsdStat.mxVal
79760 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74  ue[op] = wsdStat
79770 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20  .nowValue[op];. 
79780 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
79790 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
797a0 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66  Query status inf
797b0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 73  ormation for a s
797c0 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 63  ingle database c
797d0 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  onnection.*/.SQL
797e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
797f0 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a 20  te3_db_status(. 
79800 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
79810 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
79820 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
79830 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 20 69  n whose status i
79840 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20 69  s desired */.  i
79850 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  nt op,          
79860 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 76       /* Status v
79870 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 43  erb */.  int *pC
79880 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f  urrent,        /
79890 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e 74 20  * Write current 
798a0 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20 20  value here */.  
798b0 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c  int *pHighwater,
798c0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68        /* Write h
798d0 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 68  igh-water mark h
798e0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73  ere */.  int res
798f0 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 2f  etFlag         /
79900 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 74  * Reset high-wat
79910 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 20  er mark if true 
79920 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20  */.){.  switch( 
79930 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  op ){.    case S
79940 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
79950 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 7b  OOKASIDE_USED: {
79960 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e 74  .      *pCurrent
79970 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
79980 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 48  .nOut;.      *pH
79990 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e 6c  ighwater = db->l
799a0 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b 0a  ookaside.mxOut;.
799b0 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74 46        if( resetF
799c0 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 64  lag ){.        d
799d0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f  b->lookaside.mxO
799e0 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  ut = db->lookasi
799f0 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d  de.nOut;.      }
79a00 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
79a10 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
79a20 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
79a30 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
79a40 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
79a50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
79a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
79a70 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a  nd of status.c *
79a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
79ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
79ac0 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e 63  egin file date.c
79ad0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
79ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
79b00 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62 65  *.** 2003 Octobe
79b10 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 31.**.** The a
79b20 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
79b30 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
79b40 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
79b50 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
79b60 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
79b70 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
79b80 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
79b90 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
79ba0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
79bb0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
79bc0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
79bd0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
79be0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
79bf0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
79c00 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
79c10 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
79c20 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
79c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c70 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
79c80 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
79c90 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
79ca0 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61 6e  mplement date an
79cb0 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74 69  d time.** functi
79cc0 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 20  ons for SQLite. 
79cd0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73   .**.** There is
79ce0 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74   only one export
79cf0 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69  ed symbol in thi
79d00 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e  s file - the fun
79d10 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  ction.** sqlite3
79d20 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65  RegisterDateTime
79d30 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e  Functions() foun
79d40 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20  d at the bottom 
79d50 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20  of the file..** 
79d60 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68  All other code h
79d70 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a  as file scope..*
79d80 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63  *.** SQLite proc
79d90 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20  esses all times 
79da0 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c  and dates as Jul
79db0 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e  ian Day numbers.
79dc0 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61    The.** dates a
79dd0 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f  nd times are sto
79de0 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65  red as the numbe
79df0 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20  r of days since 
79e00 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e  noon.** in Green
79e10 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72  wich on November
79e20 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61   24, 4714 B.C. a
79e30 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
79e40 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c  Gregorian.** cal
79e50 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a  endar system. .*
79e60 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20  *.** 1970-01-01 
79e70 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32  00:00:00 is JD 2
79e80 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30  440587.5.** 2000
79e90 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20  -01-01 00:00:00 
79ea0 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a  is JD 2451544.5.
79eb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
79ec0 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73  mention requires
79ed0 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 70   years to be exp
79ee0 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 69  ressed as a 4-di
79ef0 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68  git number.** wh
79f00 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f  ich means that o
79f10 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 65  nly dates betwee
79f20 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64  n 0000-01-01 and
79f30 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a   9999-12-31 can.
79f40 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 65  ** be represente
79f50 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a  d, even though j
79f60 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
79f70 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77  s allow a much w
79f80 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66  ider.** range of
79f90 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   dates..**.** Th
79fa0 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65  e Gregorian cale
79fb0 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 75  ndar system is u
79fc0 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65  sed for all date
79fd0 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20  s and times,.** 
79fe0 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20  even those that 
79ff0 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65 67  predate the Greg
7a000 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20  orian calendar. 
7a010 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61   Historians usua
7a020 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a  lly.** use the J
7a030 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66  ulian calendar f
7a040 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 74  or dates prior t
7a050 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64  o 1582-10-15 and
7a060 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74   for some.** dat
7a070 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 64  es afterwards, d
7a080 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61  epending on loca
7a090 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74  le.  Beware of t
7a0a0 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a  his difference..
7a0b0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72  **.** The conver
7a0c0 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20  sion algorithms 
7a0d0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  are implemented 
7a0e0 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70  based on descrip
7a0f0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tions.** in the 
7a100 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a  following text:.
7a110 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20  **.**      Jean 
7a120 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73  Meeus.**      As
7a130 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72  tronomical Algor
7a140 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69  ithms, 2nd Editi
7a150 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20  on, 1998.**     
7a160 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36   ISBM 0-943396-6
7a170 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c  1-1.**      Will
7a180 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a  mann-Bell, Inc.*
7a190 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c  *      Richmond,
7a1a0 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a   Virginia (USA).
7a1b0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d  */.#include <tim
7a1c0 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51  e.h>..#ifndef SQ
7a1d0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
7a1e0 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20  ME_FUNCS../*.** 
7a1f0 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f 77  On recent Window
7a200 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65  s platforms, the
7a210 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 66   localtime_s() f
7a220 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  unction is avail
7a230 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 20  able.** as part 
7a240 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 43  of the "Secure C
7a250 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 6e  RT". It is essen
7a260 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e  tially equivalen
7a270 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69  t to .** localti
7a280 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c 65  me_r() available
7a290 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 49   under most POSI
7a2a0 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63  X platforms, exc
7a2b0 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a 2a  ept that the .**
7a2c0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 61   order of the pa
7a2d0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65  rameters is reve
7a2e0 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  rsed..**.** See 
7a2f0 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72  http://msdn.micr
7a300 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f  osoft.com/en-us/
7a310 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 65  library/a442x3ye
7a320 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a  (VS.80).aspx..**
7a330 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 20  .** If the user 
7a340 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 65  has not indicate
7a350 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69  d to use localti
7a360 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74  me_r() or localt
7a370 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61  ime_s().** alrea
7a380 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e  dy, check for an
7a390 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 69   MSVC build envi
7a3a0 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 6f  ronment that pro
7a3b0 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74  vides .** localt
7a3c0 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20  ime_s()..*/.#if 
7a3d0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f  !defined(HAVE_LO
7a3e0 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 64  CALTIME_R) && !d
7a3f0 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41  efined(HAVE_LOCA
7a400 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20  LTIME_S) && \.  
7a410 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f     defined(_MSC_
7a420 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 28  VER) && defined(
7a430 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 45  _CRT_INSECURE_DE
7a440 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e 65  PRECATE).#define
7a450 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f   HAVE_LOCALTIME_
7a460 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  S 1.#endif../*.*
7a470 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 6f  * A structure fo
7a480 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67  r holding a sing
7a490 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  le date and time
7a4a0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
7a4b0 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 74  uct DateTime Dat
7a4c0 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61  eTime;.struct Da
7a4d0 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74  teTime {.  sqlit
7a4e0 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a  e3_int64 iJD; /*
7a4f0 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   The julian day 
7a500 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34  number times 864
7a510 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59  00000 */.  int Y
7a520 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a  , M, D;       /*
7a530 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e   Year, month, an
7a540 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68  d day */.  int h
7a550 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  , m;          /*
7a560 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65   Hour and minute
7a570 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20  s */.  int tz;  
7a580 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d            /* Tim
7a590 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20  ezone offset in 
7a5a0 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75  minutes */.  dou
7a5b0 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 20  ble s;          
7a5c0 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20  /* Seconds */.  
7a5d0 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20  char validYMD;  
7a5e0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
7a5f0 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69  f Y,M,D are vali
7a600 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69  d */.  char vali
7a610 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75  dHMS;     /* Tru
7a620 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61  e (1) if h,m,s a
7a630 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68  re valid */.  ch
7a640 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20  ar validJD;     
7a650 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20   /* True (1) if 
7a660 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a  iJD is valid */.
7a670 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20    char validTZ; 
7a680 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29       /* True (1)
7a690 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20   if tz is valid 
7a6a0 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f  */.};.../*.** Co
7a6b0 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f  nvert zDate into
7a6c0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74   one or more int
7a6d0 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e  egers.  Addition
7a6e0 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  al arguments.** 
7a6f0 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f  come in groups o
7a700 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  f 5 as follows:.
7a710 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20  **.**       N   
7a720 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69      number of di
7a730 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65  gits in the inte
7a740 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e  ger.**       min
7a750 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c       minimum all
7a760 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68  owed value of th
7a770 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  e integer.**    
7a780 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d     max     maxim
7a790 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65  um allowed value
7a7a0 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
7a7b0 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 20  **       nextC  
7a7c0 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
7a7d0 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 67   after the integ
7a7e0 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c  er.**       pVal
7a7f0 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 69      where to wri
7a800 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 20  te the integers 
7a810 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e  value..**.** Con
7a820 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75  versions continu
7a830 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68  e until one with
7a840 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63   nextC==0 is enc
7a850 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65  ountered..** The
7a860 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7a870 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
7a880 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65  successful conve
7a890 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  rsions..*/.stati
7a8a0 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 28  c int getDigits(
7a8b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
7a8c0 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  e, ...){.  va_li
7a8d0 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c  st ap;.  int val
7a8e0 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74  ;.  int N;.  int
7a8f0 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b   min;.  int max;
7a900 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20  .  int nextC;.  
7a910 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74  int *pVal;.  int
7a920 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73   cnt = 0;.  va_s
7a930 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b  tart(ap, zDate);
7a940 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76  .  do{.    N = v
7a950 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
7a960 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67      min = va_arg
7a970 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d  (ap, int);.    m
7a980 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ax = va_arg(ap, 
7a990 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20  int);.    nextC 
7a9a0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
7a9b0 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61  );.    pVal = va
7a9c0 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a  _arg(ap, int*);.
7a9d0 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20      val = 0;.   
7a9e0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20   while( N-- ){. 
7a9f0 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
7aa00 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29  3Isdigit(*zDate)
7aa10 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
7aa20 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a   end_getDigits;.
7aa30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61        }.      va
7aa40 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44  l = val*10 + *zD
7aa50 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  ate - '0';.     
7aa60 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a   zDate++;.    }.
7aa70 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20      if( val<min 
7aa80 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e  || val>max || (n
7aa90 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43  extC!=0 && nextC
7aaa0 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20  !=*zDate) ){.   
7aab0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44     goto end_getD
7aac0 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20  igits;.    }.   
7aad0 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20   *pVal = val;.  
7aae0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63    zDate++;.    c
7aaf0 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20  nt++;.  }while( 
7ab00 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74  nextC );.end_get
7ab10 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64  Digits:.  va_end
7ab20 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63  (ap);.  return c
7ab30 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  nt;.}../*.** Rea
7ab40 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20  d text from z[] 
7ab50 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f  and convert into
7ab60 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
7ab70 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72  t number.  Retur
7ab80 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  n.** the number 
7ab90 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72  of digits conver
7aba0 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ted..*/.#define 
7abb0 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33  getValue sqlite3
7abc0 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73  AtoF../*.** Pars
7abd0 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74  e a timezone ext
7abe0 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e  ension on the en
7abf0 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65  d of a date-time
7ac00 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69  ..** The extensi
7ac10 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  on is of the for
7ac20 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  m:.**.**        
7ac30 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a  (+/-)HH:MM.**.**
7ac40 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e   Or the "zulu" n
7ac50 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20  otation:.**.**  
7ac60 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66        Z.**.** If
7ac70 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75   the parse is su
7ac80 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20  ccessful, write 
7ac90 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
7aca0 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e  nutes.** of chan
7acb0 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20  ge in p->tz and 
7acc0 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20  return 0.  If a 
7acd0 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63  parser error occ
7ace0 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e  urs,.** return n
7acf0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41  on-zero..**.** A
7ad00 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69   missing specifi
7ad10 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64  er is not consid
7ad20 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  ered an error..*
7ad30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
7ad40 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74  seTimezone(const
7ad50 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
7ad60 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
7ad70 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74  t sgn = 0;.  int
7ad80 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74   nHr, nMn;.  int
7ad90 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c   c;.  while( sql
7ada0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61  ite3Isspace(*zDa
7adb0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  te) ){ zDate++; 
7adc0 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20  }.  p->tz = 0;. 
7add0 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69   c = *zDate;.  i
7ade0 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  f( c=='-' ){.   
7adf0 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c   sgn = -1;.  }el
7ae00 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b  se if( c=='+' ){
7ae10 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20  .    sgn = +1;. 
7ae20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a   }else if( c=='Z
7ae30 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20  ' || c=='z' ){. 
7ae40 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7ae50 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a  goto zulu_time;.
7ae60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
7ae70 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20  urn c!=0;.  }.  
7ae80 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67  zDate++;.  if( g
7ae90 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20  etDigits(zDate, 
7aea0 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26  2, 0, 14, ':', &
7aeb0 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30  nHr, 2, 0, 59, 0
7aec0 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20  , &nMn)!=2 ){.  
7aed0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
7aee0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20    zDate += 5;.  
7aef0 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e  p->tz = sgn*(nMn
7af00 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75   + nHr*60);.zulu
7af10 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20  _time:.  while( 
7af20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a  sqlite3Isspace(*
7af30 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b  zDate) ){ zDate+
7af40 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
7af50 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Date!=0;.}../*.*
7af60 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66  * Parse times of
7af70 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20   the form HH:MM 
7af80 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48  or HH:MM:SS or H
7af90 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a  H:MM:SS.FFFF..**
7afa0 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64   The HH, MM, and
7afb0 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65   SS must each be
7afc0 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74   exactly 2 digit
7afd0 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74  s.  The.** fract
7afe0 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46  ional seconds FF
7aff0 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72  FF can be one or
7b000 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a   more digits..**
7b010 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20  .** Return 1 if 
7b020 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69  there is a parsi
7b030 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f  ng error and 0 o
7b040 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  n success..*/.st
7b050 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68  atic int parseHh
7b060 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20  MmSs(const char 
7b070 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
7b080 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d   *p){.  int h, m
7b090 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73  , s;.  double ms
7b0a0 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65   = 0.0;.  if( ge
7b0b0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32  tDigits(zDate, 2
7b0c0 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68  , 0, 24, ':', &h
7b0d0 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26  , 2, 0, 59, 0, &
7b0e0 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  m)!=2 ){.    ret
7b0f0 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61  urn 1;.  }.  zDa
7b100 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a  te += 5;.  if( *
7b110 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20  zDate==':' ){.  
7b120 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69    zDate++;.    i
7b130 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61  f( getDigits(zDa
7b140 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  te, 2, 0, 59, 0,
7b150 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20   &s)!=1 ){.     
7b160 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
7b170 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b  .    zDate += 2;
7b180 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d  .    if( *zDate=
7b190 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49  ='.' && sqlite3I
7b1a0 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29  sdigit(zDate[1])
7b1b0 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
7b1c0 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20   rScale = 1.0;. 
7b1d0 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20       zDate++;.  
7b1e0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
7b1f0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65  e3Isdigit(*zDate
7b200 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20  ) ){.        ms 
7b210 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61  = ms*10.0 + *zDa
7b220 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20  te - '0';.      
7b230 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30    rScale *= 10.0
7b240 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b  ;.        zDate+
7b250 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
7b260 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20   ms /= rScale;. 
7b270 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
7b280 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70    s = 0;.  }.  p
7b290 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
7b2a0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
7b2b0 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20  ;.  p->h = h;.  
7b2c0 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73  p->m = m;.  p->s
7b2d0 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28   = s + ms;.  if(
7b2e0 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a   parseTimezone(z
7b2f0 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72  Date, p) ) retur
7b300 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54  n 1;.  p->validT
7b310 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31  Z = (p->tz!=0)?1
7b320 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  :0;.  return 0;.
7b330 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
7b340 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44   from YYYY-MM-DD
7b350 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c   HH:MM:SS to jul
7b360 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77  ian day.  We alw
7b370 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68  ays assume.** th
7b380 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44  at the YYYY-MM-D
7b390 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74  D is according t
7b3a0 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20  o the Gregorian 
7b3b0 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20  calendar..**.** 
7b3c0 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75  Reference:  Meeu
7b3d0 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61  s page 61.*/.sta
7b3e0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
7b3f0 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  JD(DateTime *p){
7b400 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20  .  int Y, M, D, 
7b410 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20  A, B, X1, X2;.. 
7b420 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20   if( p->validJD 
7b430 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
7b440 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20  p->validYMD ){. 
7b450 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20     Y = p->Y;.   
7b460 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44   M = p->M;.    D
7b470 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65   = p->D;.  }else
7b480 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20  {.    Y = 2000; 
7b490 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70   /* If no YMD sp
7b4a0 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20  ecified, assume 
7b4b0 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20  2000-Jan-01 */. 
7b4c0 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20     M = 1;.    D 
7b4d0 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d  = 1;.  }.  if( M
7b4e0 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a  <=2 ){.    Y--;.
7b4f0 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d      M += 12;.  }
7b500 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20  .  A = Y/100;.  
7b510 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34  B = 2 - A + (A/4
7b520 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a  );.  X1 = 36525*
7b530 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20  (Y+4716)/100;.  
7b540 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31  X2 = 306001*(M+1
7b550 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a  )/10000;.  p->iJ
7b560 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  D = (sqlite3_int
7b570 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44  64)((X1 + X2 + D
7b580 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20   + B - 1524.5 ) 
7b590 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70  * 86400000);.  p
7b5a0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
7b5b0 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53   if( p->validHMS
7b5c0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b   ){.    p->iJD +
7b5d0 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b  = p->h*3600000 +
7b5e0 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73   p->m*60000 + (s
7b5f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d  qlite3_int64)(p-
7b600 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66  >s*1000);.    if
7b610 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a  ( p->validTZ ){.
7b620 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20        p->iJD -= 
7b630 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20  p->tz*60000;.   
7b640 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d     p->validYMD =
7b650 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c   0;.      p->val
7b660 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20  idHMS = 0;.     
7b670 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b   p->validTZ = 0;
7b680 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
7b690 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20  .** Parse dates 
7b6a0 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a  of the form.**.*
7b6b0 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44  *     YYYY-MM-DD
7b6c0 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a   HH:MM:SS.FFF.**
7b6d0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20       YYYY-MM-DD 
7b6e0 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20  HH:MM:SS.**     
7b6f0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
7b700 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
7b710 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74  DD.**.** Write t
7b720 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74  he result into t
7b730 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75  he DateTime stru
7b740 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e  cture and return
7b750 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73   0.** on success
7b760 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e   and 1 if the in
7b770 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f  put string is no
7b780 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a  t a well-formed.
7b790 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  ** date..*/.stat
7b7a0 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79  ic int parseYyyy
7b7b0 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20  MmDd(const char 
7b7c0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
7b7d0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d   *p){.  int Y, M
7b7e0 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28  , D, neg;..  if(
7b7f0 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29   zDate[0]=='-' )
7b800 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20  {.    zDate++;. 
7b810 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65     neg = 1;.  }e
7b820 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30  lse{.    neg = 0
7b830 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44  ;.  }.  if( getD
7b840 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c  igits(zDate,4,0,
7b850 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c  9999,'-',&Y,2,1,
7b860 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31  12,'-',&M,2,1,31
7b870 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20  ,0,&D)!=3 ){.   
7b880 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
7b890 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20   zDate += 10;.  
7b8a0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
7b8b0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c  space(*zDate) ||
7b8c0 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74   'T'==*(u8*)zDat
7b8d0 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a  e ){ zDate++; }.
7b8e0 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53    if( parseHhMmS
7b8f0 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29  s(zDate, p)==0 )
7b900 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20  {.    /* We got 
7b910 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65  the time */.  }e
7b920 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d  lse if( *zDate==
7b930 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69  0 ){.    p->vali
7b940 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  dHMS = 0;.  }els
7b950 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  e{.    return 1;
7b960 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a  .  }.  p->validJ
7b970 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69  D = 0;.  p->vali
7b980 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59  dYMD = 1;.  p->Y
7b990 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b   = neg ? -Y : Y;
7b9a0 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70  .  p->M = M;.  p
7b9b0 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70  ->D = D;.  if( p
7b9c0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20  ->validTZ ){.   
7b9d0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7b9e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
7b9f0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
7ba00 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72  time to the curr
7ba10 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65  ent time reporte
7ba20 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a  d by the VFS.*/.
7ba30 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44  static void setD
7ba40 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
7ba50 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
7ba60 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54   *context, DateT
7ba70 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c  ime *p){.  doubl
7ba80 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e r;.  sqlite3 *
7ba90 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
7baa0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
7bab0 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
7bac0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
7bad0 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20  db->pVfs, &r);. 
7bae0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
7baf0 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30  e3_int64)(r*8640
7bb00 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20  0000.0 + 0.5);. 
7bb10 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
7bb20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
7bb30 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67  t to parse the g
7bb40 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f  iven string into
7bb50 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75   a Julian Day Nu
7bb60 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  mber.  Return.**
7bb70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
7bb80 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rrors..**.** The
7bb90 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61   following are a
7bba0 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20  cceptable forms 
7bbb0 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74  for the input st
7bbc0 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ring:.**.**     
7bbd0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
7bbe0 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a  M:SS.FFF  +/-HH:
7bbf0 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e  MM.**      DDDD.
7bc00 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a  DD .**      now.
7bc10 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72  **.** In the fir
7bc20 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d  st form, the +/-
7bc30 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20  HH:MM is always 
7bc40 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66  optional.  The f
7bc50 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63  ractional.** sec
7bc60 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28  onds extension (
7bc70 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f  the ".FFF") is o
7bc80 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65  ptional.  The se
7bc90 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a  conds portion.**
7bca0 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20   (":SS.FFF") is 
7bcb0 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61  option.  The yea
7bcc0 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62  r and date can b
7bcd0 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e  e omitted as lon
7bce0 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73  g.** as there is
7bcf0 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20   a time string. 
7bd00 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67   The time string
7bd10 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20   can be omitted 
7bd20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68  as long.** as th
7bd30 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e  ere is a year an
7bd40 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  d date..*/.stati
7bd50 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f  c int parseDateO
7bd60 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  rTime(.  sqlite3
7bd70 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7bd80 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t, .  const char
7bd90 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65   *zDate, .  Date
7bda0 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  Time *p.){.  int
7bdb0 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f   isRealNum;    /
7bdc0 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71  * Return from sq
7bdd0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 29 2e  lite3IsNumber().
7bde0 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20    Not used */.  
7bdf0 69 66 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44  if( parseYyyyMmD
7be00 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b  d(zDate,p)==0 ){
7be10 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
7be20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 73 65   }else if( parse
7be30 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29  HhMmSs(zDate, p)
7be40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
7be50 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
7be60 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
7be70 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29  zDate,"now")==0)
7be80 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d  {.    setDateTim
7be90 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65  eToCurrent(conte
7bea0 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75  xt, p);.    retu
7beb0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 0;.  }else if
7bec0 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  ( sqlite3IsNumbe
7bed0 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 61 6c  r(zDate, &isReal
7bee0 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Num, SQLITE_UTF8
7bef0 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20  ) ){.    double 
7bf00 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28  r;.    getValue(
7bf10 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20  zDate, &r);.    
7bf20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65  p->iJD = (sqlite
7bf30 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30  3_int64)(r*86400
7bf40 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20  000.0 + 0.5);.  
7bf50 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31    p->validJD = 1
7bf60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
7bf70 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
7bf80 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
7bf90 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68   the Year, Month
7bfa0 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74  , and Day from t
7bfb0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
7bfc0 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mber..*/.static 
7bfd0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28  void computeYMD(
7bfe0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7bff0 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20  int Z, A, B, C, 
7c000 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20  D, E, X1;.  if( 
7c010 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65  p->validYMD ) re
7c020 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e  turn;.  if( !p->
7c030 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70  validJD ){.    p
7c040 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20  ->Y = 2000;.    
7c050 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d  p->M = 1;.    p-
7c060 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  >D = 1;.  }else{
7c070 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 28 28  .    Z = (int)((
7c080 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30  p->iJD + 4320000
7c090 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20  0)/86400000);.  
7c0a0 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d    A = (int)((Z -
7c0b0 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 36 35   1867216.25)/365
7c0c0 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 3d 20  24.25);.    A = 
7c0d0 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f 34  Z + 1 + A - (A/4
7c0e0 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20 31  );.    B = A + 1
7c0f0 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e  524;.    C = (in
7c100 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 2f 33  t)((B - 122.1)/3
7c110 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 3d 20  65.25);.    D = 
7c120 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20  (36525*C)/100;. 
7c130 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 42 2d     E = (int)((B-
7c140 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 20 20  D)/30.6001);.   
7c150 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 2e 36   X1 = (int)(30.6
7c160 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44  001*E);.    p->D
7c170 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20   = B - D - X1;. 
7c180 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f     p->M = E<14 ?
7c190 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20   E-1 : E-13;.   
7c1a0 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f   p->Y = p->M>2 ?
7c1b0 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 2d 20   C - 4716 : C - 
7c1c0 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76  4715;.  }.  p->v
7c1d0 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a  alidYMD = 1;.}..
7c1e0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
7c1f0 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20  e Hour, Minute, 
7c200 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d  and Seconds from
7c210 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   the julian day 
7c220 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
7c230 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d  c void computeHM
7c240 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  S(DateTime *p){.
7c250 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 20 70    int s;.  if( p
7c260 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 65 74  ->validHMS ) ret
7c270 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44  urn;.  computeJD
7c280 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29  (p);.  s = (int)
7c290 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30  ((p->iJD + 43200
7c2a0 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30 29  000) % 86400000)
7c2b0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30  ;.  p->s = s/100
7c2c0 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29  0.0;.  s = (int)
7c2d0 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20  p->s;.  p->s -= 
7c2e0 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36  s;.  p->h = s/36
7c2f0 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a  00;.  s -= p->h*
7c300 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73  3600;.  p->m = s
7c310 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73  /60;.  p->s += s
7c320 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d   - p->m*60;.  p-
7c330 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d  >validHMS = 1;.}
7c340 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
7c350 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d 53  both YMD and HMS
7c360 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7c370 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 44  computeYMD_HMS(D
7c380 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63  ateTime *p){.  c
7c390 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20  omputeYMD(p);.  
7c3a0 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d  computeHMS(p);.}
7c3b0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
7c3c0 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e  e YMD and HMS an
7c3d0 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74  d the TZ.*/.stat
7c3e0 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d 44  ic void clearYMD
7c3f0 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d 65  _HMS_TZ(DateTime
7c400 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64   *p){.  p->valid
7c410 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  YMD = 0;.  p->va
7c420 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d  lidHMS = 0;.  p-
7c430 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a  >validTZ = 0;.}.
7c440 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
7c450 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f  OMIT_LOCALTIME./
7c460 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
7c470 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20   difference (in 
7c480 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a  milliseconds).**
7c490 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69   between localti
7c4a0 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e  me and UTC (a.k.
7c4b0 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74  a. GMT).** for t
7c4c0 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 20  he time value p 
7c4d0 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 54  where p is in UT
7c4e0 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  C..*/.static sql
7c4f0 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c  ite3_int64 local
7c500 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 54  timeOffset(DateT
7c510 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 54  ime *p){.  DateT
7c520 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65  ime x, y;.  time
7c530 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a  _t t;.  x = *p;.
7c540 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
7c550 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c  (&x);.  if( x.Y<
7c560 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33  1971 || x.Y>=203
7c570 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32  8 ){.    x.Y = 2
7c580 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31  000;.    x.M = 1
7c590 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20  ;.    x.D = 1;. 
7c5a0 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20     x.h = 0;.    
7c5b0 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73  x.m = 0;.    x.s
7c5c0 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65   = 0.0;.  } else
7c5d0 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 28   {.    int s = (
7c5e0 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b  int)(x.s + 0.5);
7c5f0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20  .    x.s = s;.  
7c600 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20  }.  x.tz = 0;.  
7c610 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  x.validJD = 0;. 
7c620 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a   computeJD(&x);.
7c630 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29 28 78    t = (time_t)(x
7c640 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38  .iJD/1000 - 2108
7c650 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29  6676*(i64)10000)
7c660 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 4c 4f  ;.#ifdef HAVE_LO
7c670 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20  CALTIME_R.  {.  
7c680 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63    struct tm sLoc
7c690 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d  al;.    localtim
7c6a0 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29  e_r(&t, &sLocal)
7c6b0 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63  ;.    y.Y = sLoc
7c6c0 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30  al.tm_year + 190
7c6d0 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f  0;.    y.M = sLo
7c6e0 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a  cal.tm_mon + 1;.
7c6f0 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c      y.D = sLocal
7c700 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e  .tm_mday;.    y.
7c710 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f  h = sLocal.tm_ho
7c720 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c  ur;.    y.m = sL
7c730 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20  ocal.tm_min;.   
7c740 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.s = sLocal.tm
7c750 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20  _sec;.  }.#elif 
7c760 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43  defined(HAVE_LOC
7c770 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48 41 56  ALTIME_S) && HAV
7c780 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20  E_LOCALTIME_S.  
7c790 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20  {.    struct tm 
7c7a0 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61  sLocal;.    loca
7c7b0 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c  ltime_s(&sLocal,
7c7c0 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20   &t);.    y.Y = 
7c7d0 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b  sLocal.tm_year +
7c7e0 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d   1900;.    y.M =
7c7f0 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b   sLocal.tm_mon +
7c800 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c   1;.    y.D = sL
7c810 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20  ocal.tm_mday;.  
7c820 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.h = sLocal.t
7c830 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20  m_hour;.    y.m 
7c840 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b  = sLocal.tm_min;
7c850 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61  .    y.s = sLoca
7c860 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65  l.tm_sec;.  }.#e
7c870 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  lse.  {.    stru
7c880 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20  ct tm *pTm;.    
7c890 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
7c8a0 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
7c8b0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
7c8c0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
7c8d0 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f  ));.    pTm = lo
7c8e0 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20  caltime(&t);.   
7c8f0 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79   y.Y = pTm->tm_y
7c900 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20  ear + 1900;.    
7c910 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f  y.M = pTm->tm_mo
7c920 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d  n + 1;.    y.D =
7c930 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20   pTm->tm_mday;. 
7c940 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d     y.h = pTm->tm
7c950 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d  _hour;.    y.m =
7c960 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20   pTm->tm_min;.  
7c970 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.s = pTm->tm_
7c980 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sec;.    sqlite3
7c990 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
7c9a0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
7c9b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
7c9c0 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d  IC_MASTER));.  }
7c9d0 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69  .#endif.  y.vali
7c9e0 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61  dYMD = 1;.  y.va
7c9f0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e  lidHMS = 1;.  y.
7ca00 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79  validJD = 0;.  y
7ca10 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  .validTZ = 0;.  
7ca20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20  computeJD(&y);. 
7ca30 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20   return y.iJD - 
7ca40 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20  x.iJD;.}.#endif 
7ca50 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
7ca60 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a  OCALTIME */../*.
7ca70 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64  ** Process a mod
7ca80 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d  ifier to a date-
7ca90 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65  time stamp.  The
7caa0 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a   modifiers are.*
7cab0 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  * as follows:.**
7cac0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73  .**     NNN days
7cad0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72  .**     NNN hour
7cae0 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e  s.**     NNN min
7caf0 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e  utes.**     NNN.
7cb00 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20  NNNN seconds.** 
7cb10 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a      NNN months.*
7cb20 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a  *     NNN years.
7cb30 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
7cb40 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61  month.**     sta
7cb50 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20  rt of year.**   
7cb60 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a    start of week.
7cb70 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
7cb80 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64  day.**     weekd
7cb90 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78  ay N.**     unix
7cba0 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63  epoch.**     loc
7cbb0 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74  altime.**     ut
7cbc0 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30  c.**.** Return 0
7cbd0 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
7cbe0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  1 if there is an
7cbf0 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e  y kind of error.
7cc00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
7cc10 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e  arseModifier(con
7cc20 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44  st char *zMod, D
7cc30 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
7cc40 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74  nt rc = 1;.  int
7cc50 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a   n;.  double r;.
7cc60 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b    char *z, zBuf[
7cc70 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b  30];.  z = zBuf;
7cc80 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72  .  for(n=0; n<Ar
7cc90 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20  raySize(zBuf)-1 
7cca0 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29  && zMod[n]; n++)
7ccb0 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68  {.    z[n] = (ch
7ccc0 61 72 29 73 71 6c 69 74 65 33 55 70 70 65 72 54  ar)sqlite3UpperT
7ccd0 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b  oLower[(u8)zMod[
7cce0 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20  n]];.  }.  z[n] 
7ccf0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 7a  = 0;.  switch( z
7cd00 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  [0] ){.#ifndef S
7cd10 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c  QLITE_OMIT_LOCAL
7cd20 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 27 6c  TIME.    case 'l
7cd30 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20  ': {.      /*   
7cd40 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20 20   localtime.     
7cd50 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 73   **.      ** Ass
7cd60 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  uming the curren
7cd70 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 73 20  t time value is 
7cd80 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29  UTC (a.k.a. GMT)
7cd90 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a 20 20  , shift it to.  
7cda0 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61      ** show loca
7cdb0 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f  l time..      */
7cdc0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d  .      if( strcm
7cdd0 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22  p(z, "localtime"
7cde0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7cdf0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7ce00 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
7ce10 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28  localtimeOffset(
7ce20 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61  p);.        clea
7ce30 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a  rYMD_HMS_TZ(p);.
7ce40 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
7ce50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
7ce60 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
7ce70 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a 20  f.    case 'u': 
7ce80 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20  {.      /*.     
7ce90 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68   **    unixepoch
7cea0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
7ceb0 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 75 72  ** Treat the cur
7cec0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d  rent value of p-
7ced0 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62  >iJD as the numb
7cee0 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73  er of.      ** s
7cef0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
7cf00 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61  0.  Convert to a
7cf10 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79   real julian day
7cf20 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a   number..      *
7cf30 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63  /.      if( strc
7cf40 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68  mp(z, "unixepoch
7cf50 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69  ")==0 && p->vali
7cf60 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70  dJD ){.        p
7cf70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20  ->iJD = (p->iJD 
7cf80 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b  + 43200)/86400 +
7cf90 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 31   21086676*(i64)1
7cfa0 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  0000000;.       
7cfb0 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
7cfc0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
7cfd0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  = 0;.      }.#if
7cfe0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
7cff0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20  _LOCALTIME.     
7d000 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70   else if( strcmp
7d010 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b  (z, "utc")==0 ){
7d020 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
7d030 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20  _int64 c1;.     
7d040 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
7d050 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f  .        c1 = lo
7d060 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29  caltimeOffset(p)
7d070 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  ;.        p->iJD
7d080 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 20 20   -= c1;.        
7d090 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
7d0a0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
7d0b0 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c  JD += c1 - local
7d0c0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
7d0d0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7d0e0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
7d0f0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
7d100 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b  .    case 'w': {
7d110 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20  .      /*.      
7d120 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a  **    weekday N.
7d130 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
7d140 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20  * Move the date 
7d150 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  to the same time
7d160 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f 63 63   on the next occ
7d170 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 20 20  urrence of.     
7d180 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 77 68   ** weekday N wh
7d190 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31  ere 0==Sunday, 1
7d1a0 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f  ==Monday, and so
7d1b0 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a   forth.  If the.
7d1c0 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 69 73        ** date is
7d1d0 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 20   already on the 
7d1e0 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 65 6b  appropriate week
7d1f0 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e  day, this is a n
7d200 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  o-op..      */. 
7d210 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70       if( strncmp
7d220 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 2c 20  (z, "weekday ", 
7d230 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61 6c 75  8)==0 && getValu
7d240 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20  e(&z[8],&r)>0.  
7d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
7d260 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20  & (n=(int)r)==r 
7d270 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29  && n>=0 && r<7 )
7d280 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
7d290 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20  3_int64 Z;.     
7d2a0 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d     computeYMD_HM
7d2b0 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  S(p);.        p-
7d2c0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
7d2d0 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44        p->validJD
7d2e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f   = 0;.        co
7d2f0 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20  mputeJD(p);.    
7d300 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44      Z = ((p->iJD
7d310 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f 38 36   + 129600000)/86
7d320 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 20 20  400000) % 7;.   
7d330 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a       if( Z>n ) Z
7d340 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 70   -= 7;.        p
7d350 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29  ->iJD += (n - Z)
7d360 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20  *86400000;.     
7d370 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
7d380 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  TZ(p);.        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 73 27 3a 20  }.    case 's': 
7d3c0 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20  {.      /*.     
7d3d0 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f 66 20   **    start of 
7d3e0 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20  TTTTT.      **. 
7d3f0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65       ** Move the
7d400 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 73 20   date backwards 
7d410 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  to the beginning
7d420 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
7d430 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72  day,.      ** or
7d440 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a   month or year..
7d450 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
7d460 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73  f( strncmp(z, "s
7d470 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 3d 30  tart of ", 9)!=0
7d480 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
7d490 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f  z += 9;.      co
7d4a0 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20  mputeYMD(p);.   
7d4b0 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d     p->validHMS =
7d4c0 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d   1;.      p->h =
7d4d0 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20   p->m = 0;.     
7d4e0 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20   p->s = 0.0;.   
7d4f0 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20     p->validTZ = 
7d500 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
7d510 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  dJD = 0;.      i
7d520 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e  f( strcmp(z,"mon
7d530 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  th")==0 ){.     
7d540 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20     p->D = 1;.   
7d550 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7d560 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72     }else if( str
7d570 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30  cmp(z,"year")==0
7d580 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   ){.        comp
7d590 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20  uteYMD(p);.     
7d5a0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20     p->M = 1;.   
7d5b0 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20       p->D = 1;. 
7d5c0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7d5d0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
7d5e0 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d  trcmp(z,"day")==
7d5f0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
7d600 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
7d610 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
7d620 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 20      case '+':.  
7d630 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 20 20    case '-':.    
7d640 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 63 61  case '0':.    ca
7d650 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 73 65  se '1':.    case
7d660 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '2':.    case '
7d670 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 34 27  3':.    case '4'
7d680 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a 0a  :.    case '5':.
7d690 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a 20 20      case '6':.  
7d6a0 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 20 20    case '7':.    
7d6b0 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 63 61  case '8':.    ca
7d6c0 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20  se '9': {.      
7d6d0 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b  double rRounder;
7d6e0 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 56 61  .      n = getVa
7d6f0 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20  lue(z, &r);.    
7d700 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 29    assert( n>=1 )
7d710 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d  ;.      if( z[n]
7d720 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20  ==':' ){.       
7d730 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f   /* A modifier o
7d740 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29  f the form (+|-)
7d750 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64  HH:MM:SS.FFF add
7d760 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73 29  s (or subtracts)
7d770 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
7d780 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72  specified number
7d790 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74   of hours, minut
7d7a0 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64  es, seconds, and
7d7b0 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f   fractional seco
7d7c0 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  nds.        ** t
7d7d0 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65  o the time.  The
7d7e0 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f   ".FFF" may be o
7d7f0 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53  mitted.  The ":S
7d800 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20  S.FFF" may be.  
7d810 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64        ** omitted
7d820 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
7d830 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
7d840 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20  *z2 = z;.       
7d850 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20   DateTime tx;.  
7d860 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
7d870 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20  t64 day;.       
7d880 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64   if( !sqlite3Isd
7d890 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b  igit(*z2) ) z2++
7d8a0 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
7d8b0 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (&tx, 0, sizeof(
7d8c0 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66  tx));.        if
7d8d0 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32  ( parseHhMmSs(z2
7d8e0 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a  , &tx) ) break;.
7d8f0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
7d900 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20  D(&tx);.        
7d910 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30  tx.iJD -= 432000
7d920 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20  00;.        day 
7d930 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30  = tx.iJD/8640000
7d940 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a  0;.        tx.iJ
7d950 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30  D -= day*8640000
7d960 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  0;.        if( z
7d970 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a  [0]=='-' ) tx.iJ
7d980 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20  D = -tx.iJD;.   
7d990 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
7d9a0 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72  );.        clear
7d9b0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20  YMD_HMS_TZ(p);. 
7d9c0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d         p->iJD +=
7d9d0 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20   tx.iJD;.       
7d9e0 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20   rc = 0;.       
7d9f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
7da00 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20        z += n;.  
7da10 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
7da20 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20  e3Isspace(*z) ) 
7da30 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73  z++;.      n = s
7da40 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
7da50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31  );.      if( n>1
7da60 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b  0 || n<3 ) break
7da70 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d  ;.      if( z[n-
7da80 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31  1]=='s' ){ z[n-1
7da90 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20  ] = 0; n--; }.  
7daa0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7dab0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a  ;.      rc = 0;.
7dac0 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d        rRounder =
7dad0 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30   r<0 ? -0.5 : +0
7dae0 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d  .5;.      if( n=
7daf0 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =3 && strcmp(z,"
7db00 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  day")==0 ){.    
7db10 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7db20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
7db30 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f  86400000.0 + rRo
7db40 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65  under);.      }e
7db50 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20  lse if( n==4 && 
7db60 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29  strcmp(z,"hour")
7db70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
7db80 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65  ->iJD += (sqlite
7db90 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30  3_int64)(r*(8640
7dba0 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72  0000.0/24.0) + r
7dbb0 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20  Rounder);.      
7dbc0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26  }else if( n==6 &
7dbd0 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75  & strcmp(z,"minu
7dbe0 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  te")==0 ){.     
7dbf0 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71     p->iJD += (sq
7dc00 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28  lite3_int64)(r*(
7dc10 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30  86400000.0/(24.0
7dc20 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64  *60.0)) + rRound
7dc30 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
7dc40 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72   if( n==6 && str
7dc50 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d  cmp(z,"second")=
7dc60 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  =0 ){.        p-
7dc70 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33  >iJD += (sqlite3
7dc80 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30  _int64)(r*(86400
7dc90 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30  000.0/(24.0*60.0
7dca0 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64  *60.0)) + rRound
7dcb0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
7dcc0 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72   if( n==5 && str
7dcd0 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d  cmp(z,"month")==
7dce0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  0 ){.        int
7dcf0 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63   x, y;.        c
7dd00 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29  omputeYMD_HMS(p)
7dd10 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b  ;.        p->M +
7dd20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20  = (int)r;.      
7dd30 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28    x = p->M>0 ? (
7dd40 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d  p->M-1)/12 : (p-
7dd50 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20  >M-12)/12;.     
7dd60 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20     p->Y += x;.  
7dd70 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a        p->M -= x*
7dd80 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76  12;.        p->v
7dd90 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
7dda0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7ddb0 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 28 69  ;.        y = (i
7ddc0 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66  nt)r;.        if
7ddd0 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20  ( y!=r ){.      
7dde0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7ddf0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72  qlite3_int64)((r
7de00 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30   - y)*30.0*86400
7de10 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72  000.0 + rRounder
7de20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
7de30 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d     }else if( n==
7de40 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79  4 && strcmp(z,"y
7de50 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ear")==0 ){.    
7de60 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74      int y = (int
7de70 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70  )r;.        comp
7de80 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
7de90 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79         p->Y += y
7dea0 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ;.        p->val
7deb0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
7dec0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7ded0 20 20 20 20 20 20 20 20 69 66 28 20 79 21 3d 72          if( y!=r
7dee0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
7def0 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33  >iJD += (sqlite3
7df00 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a  _int64)((r - y)*
7df10 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30  365.0*86400000.0
7df20 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20   + rRounder);.  
7df30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
7df40 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20  lse{.        rc 
7df50 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
7df60 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
7df70 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65  TZ(p);.      bre
7df80 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
7df90 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62  fault: {.      b
7dfa0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
7dfb0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
7dfc0 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69  /*.** Process ti
7dfd0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  me function argu
7dfe0 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20  ments.  argv[0] 
7dff0 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73  is a date-time s
7e000 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d  tamp..** argv[1]
7e010 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   and following a
7e020 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50  re modifiers.  P
7e030 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e  arse them all an
7e040 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72  d write.** the r
7e050 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e  esulting time in
7e060 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20  to the DateTime 
7e070 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 52 65  structure p.  Re
7e080 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63  turn 0.** on suc
7e090 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68  cess and 1 if th
7e0a0 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f  ere are any erro
7e0b0 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  rs..**.** If the
7e0c0 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61  re are zero para
7e0d0 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 6e 20  meters (if even 
7e0e0 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66  argv[0] is undef
7e0f0 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73  ined).** then as
7e100 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76  sume a default v
7e110 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f  alue of "now" fo
7e120 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74  r argv[0]..*/.st
7e130 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 65 28  atic int isDate(
7e140 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7e150 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
7e160 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c  int argc, .  sql
7e170 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
7e180 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a  v, .  DateTime *
7e190 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  p.){.  int i;.  
7e1a0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
7e1b0 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54  har *z;.  int eT
7e1c0 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c  ype;.  memset(p,
7e1d0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b   0, sizeof(*p));
7e1e0 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29  .  if( argc==0 )
7e1f0 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d  {.    setDateTim
7e200 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65  eToCurrent(conte
7e210 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20  xt, p);.  }else 
7e220 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 71 6c  if( (eType = sql
7e230 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
7e240 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54  argv[0]))==SQLIT
7e250 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 20 20  E_FLOAT.        
7e260 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 54             || eT
7e270 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45  ype==SQLITE_INTE
7e280 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a  GER ){.    p->iJ
7e290 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  D = (sqlite3_int
7e2a0 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  64)(sqlite3_valu
7e2b0 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d  e_double(argv[0]
7e2c0 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30  )*86400000.0 + 0
7e2d0 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69  .5);.    p->vali
7e2e0 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  dJD = 1;.  }else
7e2f0 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65  {.    z = sqlite
7e300 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
7e310 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21  v[0]);.    if( !
7e320 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72  z || parseDateOr
7e330 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63  Time(context, (c
7e340 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20  har*)z, p) ){.  
7e350 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7e360 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
7e370 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  1; i<argc; i++){
7e380 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71  .    if( (z = sq
7e390 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
7e3a0 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c  (argv[i]))==0 ||
7e3b0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28   parseModifier((
7e3c0 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20  char*)z, p) ){. 
7e3d0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
7e3e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
7e3f0 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n 0;.}.../*.** T
7e400 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
7e410 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20  tines implement 
7e420 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 74 65  the various date
7e430 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69   and time functi
7e440 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65  ons.** of SQLite
7e450 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a  ..*/../*.**    j
7e460 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54  ulianday( TIMEST
7e470 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
7e480 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
7e490 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  n the julian day
7e4a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64   number of the d
7e4b0 61 74 65 20 73 70 65 63 69 66 69 65 64 20 69 6e  ate specified in
7e4c0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a   the arguments.*
7e4d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75  /.static void ju
7e4e0 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73  liandayFunc(.  s
7e4f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7e500 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
7e510 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
7e520 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
7e530 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
7e540 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
7e550 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
7e560 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d  x)==0 ){.    com
7e570 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20  puteJD(&x);.    
7e580 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
7e590 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78  ouble(context, x
7e5a0 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29  .iJD/86400000.0)
7e5b0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
7e5c0 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45    datetime( TIME
7e5d0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
7e5e0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
7e5f0 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48  urn YYYY-MM-DD H
7e600 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69  H:MM:SS.*/.stati
7e610 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46  c void datetimeF
7e620 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
7e630 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
7e640 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
7e650 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
7e660 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d  rgv.){.  DateTim
7e670 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74  e x;.  if( isDat
7e680 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c  e(context, argc,
7e690 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b   argv, &x)==0 ){
7e6a0 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31  .    char zBuf[1
7e6b0 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65  00];.    compute
7e6c0 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20  YMD_HMS(&x);.   
7e6d0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7e6e0 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  f(sizeof(zBuf), 
7e6f0 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64  zBuf, "%04d-%02d
7e700 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a  -%02d %02d:%02d:
7e710 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20  %02d",.         
7e720 20 20 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c              x.Y,
7e730 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20   x.M, x.D, x.h, 
7e740 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29  x.m, (int)(x.s))
7e750 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
7e760 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
7e770 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c  t, zBuf, -1, SQL
7e780 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
7e790 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20    }.}../*.**    
7e7a0 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47  time( TIMESTRING
7e7b0 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29  , MOD, MOD, ...)
7e7c0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48  .**.** Return HH
7e7d0 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63  :MM:SS.*/.static
7e7e0 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a   void timeFunc(.
7e7f0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
7e800 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
7e810 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
7e820 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
7e830 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a  {.  DateTime x;.
7e840 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e    if( isDate(con
7e850 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
7e860 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  , &x)==0 ){.    
7e870 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a  char zBuf[100];.
7e880 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26      computeHMS(&
7e890 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
7e8a0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
7e8b0 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30  zBuf), zBuf, "%0
7e8c0 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78  2d:%02d:%02d", x
7e8d0 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e  .h, x.m, (int)x.
7e8e0 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  s);.    sqlite3_
7e8f0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
7e900 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
7e910 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
7e920 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
7e930 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 52 49    date( TIMESTRI
7e940 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
7e950 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
7e960 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74  YYYY-MM-DD.*/.st
7e970 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 46 75  atic void dateFu
7e980 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
7e990 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
7e9a0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
7e9b0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
7e9c0 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65  gv.){.  DateTime
7e9d0 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65   x;.  if( isDate
7e9e0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
7e9f0 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a  argv, &x)==0 ){.
7ea00 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
7ea10 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59  0];.    computeY
7ea20 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69  MD(&x);.    sqli
7ea30 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
7ea40 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c  eof(zBuf), zBuf,
7ea50 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64   "%04d-%02d-%02d
7ea60 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44  ", x.Y, x.M, x.D
7ea70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
7ea80 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
7ea90 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51  xt, zBuf, -1, SQ
7eaa0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
7eab0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20  .  }.}../*.**   
7eac0 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41   strftime( FORMA
7ead0 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  T, TIMESTRING, M
7eae0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
7eaf0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72  .** Return a str
7eb00 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 62 79  ing described by
7eb10 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72   FORMAT.  Conver
7eb20 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73  sions as follows
7eb30 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61  :.**.**   %d  da
7eb40 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20  y of month.**   
7eb50 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61  %f  ** fractiona
7eb60 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53  l seconds  SS.SS
7eb70 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20  S.**   %H  hour 
7eb80 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64  00-24.**   %j  d
7eb90 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33  ay of year 000-3
7eba0 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a  66.**   %J  ** J
7ebb0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
7ebc0 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20  .**   %m  month 
7ebd0 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d  01-12.**   %M  m
7ebe0 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20  inute 00-59.**  
7ebf0 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e   %s  seconds sin
7ec00 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a  ce 1970-01-01.**
7ec10 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30     %S  seconds 0
7ec20 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61  0-59.**   %w  da
7ec30 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73  y of week 0-6  s
7ec40 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57  unday==0.**   %W
7ec50 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30    week of year 0
7ec60 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65  0-53.**   %Y  ye
7ec70 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20  ar 0000-9999.** 
7ec80 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69    %%  %.*/.stati
7ec90 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46  c void strftimeF
7eca0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
7ecb0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
7ecc0 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
7ecd0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
7ece0 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d  rgv.){.  DateTim
7ecf0 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20  e x;.  u64 n;.  
7ed00 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68  size_t i,j;.  ch
7ed10 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33  ar *z;.  sqlite3
7ed20 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68   *db;.  const ch
7ed30 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73  ar *zFmt = (cons
7ed40 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  t char*)sqlite3_
7ed50 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
7ed60 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66  0]);.  char zBuf
7ed70 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d  [100];.  if( zFm
7ed80 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63  t==0 || isDate(c
7ed90 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20  ontext, argc-1, 
7eda0 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65  argv+1, &x) ) re
7edb0 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c  turn;.  db = sql
7edc0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
7edd0 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
7ede0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b  .  for(i=0, n=1;
7edf0 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e   zFmt[i]; i++, n
7ee00 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d  ++){.    if( zFm
7ee10 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20  t[i]=='%' ){.   
7ee20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b     switch( zFmt[
7ee30 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  i+1] ){.        
7ee40 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20  case 'd':.      
7ee50 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20    case 'H':.    
7ee60 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20      case 'm':.  
7ee70 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a        case 'M':.
7ee80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27          case 'S'
7ee90 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
7eea0 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b  W':.          n+
7eeb0 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  +;.          /* 
7eec0 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20  fall thru */.   
7eed0 20 20 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20       case 'w':. 
7eee0 20 20 20 20 20 20 20 63 61 73 65 20 27 25 27 3a         case '%':
7eef0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
7ef00 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7ef10 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20  f':.          n 
7ef20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20  += 8;.          
7ef30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7ef40 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20  ase 'j':.       
7ef50 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20     n += 3;.     
7ef60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7ef70 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20      case 'Y':.  
7ef80 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a          n += 8;.
7ef90 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7efa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 73  .        case 's
7efb0 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ':.        case 
7efc0 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e  'J':.          n
7efd0 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20   += 50;.        
7efe0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7eff0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
7f000 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20      return;  /* 
7f010 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61  ERROR.  return a
7f020 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d   NULL */.      }
7f030 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20  .      i++;.    
7f040 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65  }.  }.  testcase
7f050 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66  ( n==sizeof(zBuf
7f060 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73  )-1 );.  testcas
7f070 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75  e( n==sizeof(zBu
7f080 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  f) );.  testcase
7f090 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c  ( n==(u64)db->aL
7f0a0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
7f0b0 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20  T_LENGTH]+1 );. 
7f0c0 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 28 75   testcase( n==(u
7f0d0 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  64)db->aLimit[SQ
7f0e0 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
7f0f0 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69  H] );.  if( n<si
7f100 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20  zeof(zBuf) ){.  
7f110 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65    z = zBuf;.  }e
7f120 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64  lse if( n>(u64)d
7f130 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
7f140 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
7f150 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
7f160 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
7f170 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  g(context);.    
7f180 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b  return;.  }else{
7f190 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
7f1a0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
7f1b0 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69 66 28  (int)n);.    if(
7f1c0 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73   z==0 ){.      s
7f1d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
7f1e0 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
7f1f0 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  t);.      return
7f200 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f  ;.    }.  }.  co
7f210 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 63  mputeJD(&x);.  c
7f220 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
7f230 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20  );.  for(i=j=0; 
7f240 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  zFmt[i]; i++){. 
7f250 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d     if( zFmt[i]!=
7f260 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a  '%' ){.      z[j
7f270 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20  ++] = zFmt[i];. 
7f280 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
7f290 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 74 63  i++;.      switc
7f2a0 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20  h( zFmt[i] ){.  
7f2b0 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 20        case 'd': 
7f2c0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7f2d0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
7f2e0 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.D); j+=2; br
7f2f0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
7f300 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'f': {.       
7f310 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e     double s = x.
7f320 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  s;.          if(
7f330 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 3d 20   s>59.999 ) s = 
7f340 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 20 20  59.999;.        
7f350 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7f360 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36  tf(7, &z[j],"%06
7f370 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 20 20  .3f", s);.      
7f380 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33      j += sqlite3
7f390 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b  Strlen30(&z[j]);
7f3a0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
7f3b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
7f3c0 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 20 73      case 'H':  s
7f3d0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7f3e0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
7f3f0 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.h); j+=2; brea
7f400 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
7f410 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72  'W': /* Fall thr
7f420 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73  u */.        cas
7f430 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'j': {.       
7f440 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20     int nDay;    
7f450 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
7f460 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65  er of days since
7f470 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 61 72   1st day of year
7f480 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61   */.          Da
7f490 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20  teTime y = x;.  
7f4a0 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a          y.validJ
7f4b0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  D = 0;.         
7f4c0 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20   y.M = 1;.      
7f4d0 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20      y.D = 1;.   
7f4e0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
7f4f0 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (&y);.          
7f500 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28 78 2e  nDay = (int)((x.
7f510 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30  iJD-y.iJD+432000
7f520 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20  00)/86400000);. 
7f530 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d           if( zFm
7f540 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20  t[i]=='W' ){.   
7f550 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 64 3b           int wd;
7f560 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20     /* 0=Monday, 
7f570 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36  1=Tuesday, ... 6
7f580 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20  =Sunday */.     
7f590 20 20 20 20 20 20 20 77 64 20 3d 20 28 69 6e 74         wd = (int
7f5a0 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30 30 30  )(((x.iJD+432000
7f5b0 30 30 29 2f 38 36 34 30 30 30 30 30 29 25 37 29  00)/86400000)%7)
7f5c0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
7f5d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7f5e0 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28  , &z[j],"%02d",(
7f5f0 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20  nDay+7-wd)/7);. 
7f600 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20             j += 
7f610 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  2;.          }el
7f620 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
7f630 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7f640 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22  (4, &z[j],"%03d"
7f650 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20 20 20  ,nDay+1);.      
7f660 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20        j += 3;.  
7f670 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7f680 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
7f690 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
7f6a0 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'J': {.       
7f6b0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
7f6c0 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25  ntf(20, &z[j],"%
7f6d0 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30  .16g",x.iJD/8640
7f6e0 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20 20 20  0000.0);.       
7f6f0 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72     j+=sqlite3Str
7f700 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20  len30(&z[j]);.  
7f710 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7f720 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
7f730 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69   case 'm':  sqli
7f740 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
7f750 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d  &z[j],"%02d",x.M
7f760 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
7f770 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27          case 'M'
7f780 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
7f790 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
7f7a0 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20  2d",x.m); j+=2; 
7f7b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7f7c0 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20  ase 's': {.     
7f7d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
7f7e0 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22  rintf(30,&z[j],"
7f7f0 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20 20 20  %lld",.         
7f800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f810 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f 31 30    (i64)(x.iJD/10
7f820 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69  00 - 21086676*(i
7f830 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20 20 20  64)10000));.    
7f840 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74        j += sqlit
7f850 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d  e3Strlen30(&z[j]
7f860 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
7f870 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
7f880 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 20        case 'S': 
7f890 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
7f8a0 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  f(3,&z[j],"%02d"
7f8b0 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32  ,(int)x.s); j+=2
7f8c0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
7f8d0 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20   case 'w': {.   
7f8e0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
7f8f0 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44 2b 31  (char)(((x.iJD+1
7f900 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30  29600000)/864000
7f910 30 30 29 20 25 20 37 29 20 2b 20 27 30 27 3b 0a  00) % 7) + '0';.
7f920 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7f930 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7f940 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b 0a 20     case 'Y': {. 
7f950 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
7f960 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a  _snprintf(5,&z[j
7f970 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a  ],"%04d",x.Y); j
7f980 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  +=sqlite3Strlen3
7f990 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20  0(&z[j]);.      
7f9a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
7f9b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 65 66     }.        def
7f9c0 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d  ault:   z[j++] =
7f9d0 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20   '%'; break;.   
7f9e0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
7f9f0 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c   z[j] = 0;.  sql
7fa00 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
7fa10 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c  (context, z, -1,
7fa20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
7fa30 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f         z==zBuf ?
7fa40 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
7fa50 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  T : SQLITE_DYNAM
7fa60 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75  IC);.}../*.** cu
7fa70 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a  rrent_time().**.
7fa80 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
7fa90 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
7faa0 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28  e value as time(
7fab0 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69  'now')..*/.stati
7fac0 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63  c void ctimeFunc
7fad0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7fae0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7faf0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
7fb00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
7fb10 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
7fb20 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
7fb30 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
7fb40 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f  );.  timeFunc(co
7fb50 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a  ntext, 0, 0);.}.
7fb60 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64  ./*.** current_d
7fb70 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ate().**.** This
7fb80 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
7fb90 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
7fba0 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e   as date('now').
7fbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7fbc0 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c  cdateFunc(.  sql
7fbd0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7fbe0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
7fbf0 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
7fc00 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
7fc10 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  .){.  UNUSED_PAR
7fc20 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
7fc30 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61   NotUsed2);.  da
7fc40 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20  teFunc(context, 
7fc50 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0, 0);.}../*.** 
7fc60 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d  current_timestam
7fc70 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p().**.** This f
7fc80 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
7fc90 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61  the same value a
7fca0 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27  s datetime('now'
7fcb0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
7fcc0 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63  d ctimestampFunc
7fcd0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7fce0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7fcf0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
7fd00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
7fd10 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
7fd20 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
7fd30 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
7fd40 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e  );.  datetimeFun
7fd50 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29  c(context, 0, 0)
7fd60 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
7fd70 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
7fd80 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43  IT_DATETIME_FUNC
7fd90 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  S) */..#ifdef SQ
7fda0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
7fdb0 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49  ME_FUNCS./*.** I
7fdc0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73  f the library is
7fdd0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69   compiled to omi
7fde0 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65  t the full-scale
7fdf0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a   date and time.*
7fe00 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67  * handling (to g
7fe10 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e  et a smaller bin
7fe20 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  ary), the follow
7fe30 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73  ing minimal vers
7fe40 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75  ion.** of the fu
7fe50 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f  nctions current_
7fe60 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f  time(), current_
7fe70 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 72 65  date() and curre
7fe80 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a  nt_timestamp().*
7fe90 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69  * are included i
7fea0 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 73 20  nstead. This is 
7feb0 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d  to support colum
7fec0 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74  n declarations t
7fed0 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22  hat.** include "
7fee0 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f  DEFAULT CURRENT_
7fef0 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a  TIME" etc..**.**
7ff00 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75   This function u
7ff10 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72  ses the C-librar
7ff20 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65  y functions time
7ff30 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20  (), gmtime().** 
7ff40 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20  and strftime(). 
7ff50 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  The format strin
7ff60 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72  g to pass to str
7ff70 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c  ftime() is suppl
7ff80 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73  ied.** as the us
7ff90 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20  er-data for the 
7ffa0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
7ffb0 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74  tic void current
7ffc0 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  TimeFunc(.  sqli
7ffd0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7ffe0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
7fff0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
80000 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69  e **argv.){.  ti
80010 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a  me_t t;.  char *
80020 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20  zFormat = (char 
80030 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
80040 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ata(context);.  
80050 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64  sqlite3 *db;.  d
80060 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72  ouble rT;.  char
80070 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e   zBuf[20];..  UN
80080 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
80090 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  rgc);.  UNUSED_P
800a0 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a  ARAMETER(argv);.
800b0 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  .  db = sqlite3_
800c0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
800d0 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  e(context);.  sq
800e0 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
800f0 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54  me(db->pVfs, &rT
80100 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
80110 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
80120 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36 34 30  POINT.  t = 8640
80130 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30 35 38  0.0*(rT - 244058
80140 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73  7.5) + 0.5;.#els
80150 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74 20 66  e.  /* without f
80160 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
80170 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c 20 68  pport, rT will h
80180 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79  ave.  ** already
80190 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e 61 6c   lost fractional
801a0 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a   day precision..
801b0 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34 30 30    */.  t = 86400
801c0 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35 38 37   * (rT - 2440587
801d0 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e 64 69  ) - 43200;.#endi
801e0 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 47 4d  f.#ifdef HAVE_GM
801f0 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73  TIME_R.  {.    s
80200 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20  truct tm sNow;. 
80210 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20     gmtime_r(&t, 
80220 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 72 66  &sNow);.    strf
80230 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a  time(zBuf, 20, z
80240 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a  Format, &sNow);.
80250 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20    }.#else.  {.  
80260 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d    struct tm *pTm
80270 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
80280 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
80290 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
802a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
802b0 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54  MASTER));.    pT
802c0 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a  m = gmtime(&t);.
802d0 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75      strftime(zBu
802e0 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  f, 20, zFormat, 
802f0 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pTm);.    sqlite
80300 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
80310 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
80320 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
80330 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20  TIC_MASTER));.  
80340 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69  }.#endif..  sqli
80350 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
80360 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
80370 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
80380 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ENT);.}.#endif..
80390 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
803a0 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 61  ion registered a
803b0 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20  ll of the above 
803c0 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53  C functions as S
803d0 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e  QL.** functions.
803e0 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65    This should be
803f0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e   the only routin
80400 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77  e in this file w
80410 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20  ith.** external 
80420 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  linkage..*/.SQLI
80430 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
80440 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
80450 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
80460 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
80470 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
80480 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 75 6e  Def aDateTimeFun
80490 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66  cs[] = {.#ifndef
804a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
804b0 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20  ETIME_FUNCS.    
804c0 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64  FUNCTION(juliand
804d0 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c 20 30  ay,        -1, 0
804e0 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75  , 0, juliandayFu
804f0 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  nc ),.    FUNCTI
80500 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 20 20  ON(date,        
80510 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64       -1, 0, 0, d
80520 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a  ateFunc      ),.
80530 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d      FUNCTION(tim
80540 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d  e,             -
80550 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e  1, 0, 0, timeFun
80560 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  c      ),.    FU
80570 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c  NCTION(datetime,
80580 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
80590 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20  0, datetimeFunc 
805a0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
805b0 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 20 20  (strftime,      
805c0 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72     -1, 0, 0, str
805d0 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20  ftimeFunc  ),.  
805e0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65    FUNCTION(curre
805f0 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c  nt_time,      0,
80600 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e 63   0, 0, ctimeFunc
80610 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
80620 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d  TION(current_tim
80630 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c  estamp, 0, 0, 0,
80640 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 29   ctimestampFunc)
80650 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63  ,.    FUNCTION(c
80660 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20  urrent_date,    
80670 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 74 65    0, 0, 0, cdate
80680 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c 73  Func     ),.#els
80690 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49  e.    STR_FUNCTI
806a0 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c  ON(current_time,
806b0 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a        0, "%H:%M:
806c0 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c  %S",          0,
806d0 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63   currentTimeFunc
806e0 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54  ),.    STR_FUNCT
806f0 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65  ION(current_time
80700 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d  stamp, 0, "%Y-%m
80710 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 20 30  -%d",          0
80720 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e  , currentTimeFun
80730 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  c),.    STR_FUNC
80740 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74  TION(current_dat
80750 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25  e,      0, "%Y-%
80760 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20  m-%d %H:%M:%S", 
80770 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
80780 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  nc),.#endif.  };
80790 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63  .  int i;.  Func
807a0 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d  DefHash *pHash =
807b0 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66   &GLOBAL(FuncDef
807c0 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f  Hash, sqlite3Glo
807d0 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20  balFunctions);. 
807e0 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20   FuncDef *aFunc 
807f0 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f  = (FuncDef*)&GLO
80800 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 44 61  BAL(FuncDef, aDa
80810 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20  teTimeFuncs);.. 
80820 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61   for(i=0; i<Arra
80830 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d 65 46  ySize(aDateTimeF
80840 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  uncs); i++){.   
80850 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49   sqlite3FuncDefI
80860 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61 46  nsert(pHash, &aF
80870 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a  unc[i]);.  }.}..
80880 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
80890 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a  End of date.c **
808a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
808b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
808c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
808d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
808e0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20  Begin file os.c 
808f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
80920 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d  /*.** 2005 Novem
80930 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 29.**.** The
80940 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
80950 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
80960 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
80970 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
80980 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
80990 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
809a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
809b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
809c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
809d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
809e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
809f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
80a00 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
80a10 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
80a20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
80a30 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
80a40 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
80a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
80aa0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
80ab0 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65  ins OS interface
80ac0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
80ad0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61  mmon to all.** a
80ae0 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f  rchitectures..*/
80af0 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
80b00 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20  _OS_C_ 1.#undef 
80b10 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f  _SQLITE_OS_C_../
80b20 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
80b30 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f   SQLite sqlite3_
80b40 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  vfs implementati
80b50 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63  ons do not alloc
80b60 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61  ate.** memory (a
80b70 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78  ctually, os_unix
80b80 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  .c allocates a s
80b90 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d  mall amount of m
80ba0 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69  emory.** from wi
80bb0 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20  thin OsOpen()), 
80bc0 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70  but some third-p
80bd0 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74  arty implementat
80be0 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20  ions may..** So 
80bf0 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66 65  we test the effe
80c00 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28  cts of a malloc(
80c10 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68  ) failing and th
80c20 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29  e sqlite3OsXXX()
80c30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
80c40 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f  urning SQLITE_IO
80c50 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20  ERR_NOMEM using 
80c60 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  the DO_OS_MALLOC
80c70 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a  _TEST macro..**.
80c80 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
80c90 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69   functions are i
80ca0 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20  nstrumented for 
80cb0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
80cc0 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a   .** testing:.**
80cd0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
80ce0 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73  sOpen().**     s
80cf0 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a  qlite3OsRead().*
80d00 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57  *     sqlite3OsW
80d10 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71  rite().**     sq
80d20 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a  lite3OsSync().**
80d30 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f       sqlite3OsLo
80d40 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64  ck().**.*/.#if d
80d50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
80d60 53 54 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f  ST) && (SQLITE_O
80d70 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66  S_WIN==0).  #def
80d80 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  ine DO_OS_MALLOC
80d90 5f 54 45 53 54 28 78 29 20 69 66 20 28 21 78 20  _TEST(x) if (!x 
80da0 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d  || !sqlite3IsMem
80db0 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20 20 20  Journal(x)) {   
80dc0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54    \.    void *pT
80dd0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  stAlloc = sqlite
80de0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20  3Malloc(10);    
80df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80e00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
80e10 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72  f (!pTstAlloc) r
80e20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
80e30 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20  RR_NOMEM;       
80e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80e50 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  \.    sqlite3_fr
80e60 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20  ee(pTstAlloc);  
80e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80e90 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c         \.  }.#el
80ea0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f  se.  #define DO_
80eb0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78  OS_MALLOC_TEST(x
80ec0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
80ed0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
80ee0 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65  utines are conve
80ef0 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20  nience wrappers 
80f00 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a  around methods.*
80f10 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
80f20 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54  _file object.  T
80f30 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75  his is mostly ju
80f40 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67  st syntactic sug
80f50 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68  ar. All.** of th
80f60 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70  is would be comp
80f70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63  letely automatic
80f80 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20   if SQLite were 
80f90 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43  coded using.** C
80fa0 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c  ++ instead of pl
80fb0 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51  ain old C..*/.SQ
80fc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
80fd0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
80fe0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49  sqlite3_file *pI
80ff0 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
81000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
81010 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b  pId->pMethods ){
81020 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70  .    rc = pId->p
81030 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28  Methods->xClose(
81040 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70  pId);.    pId->p
81050 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d  Methods = 0;.  }
81060 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
81070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81080 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64  nt sqlite3OsRead
81090 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
810a0 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  d, void *pBuf, i
810b0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
810c0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  et){.  DO_OS_MAL
810d0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
810e0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
810f0 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70  ods->xRead(id, p
81100 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74  Buf, amt, offset
81110 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
81120 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
81130 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66  sWrite(sqlite3_f
81140 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76  ile *id, const v
81150 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61  oid *pBuf, int a
81160 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b  mt, i64 offset){
81170 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
81180 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
81190 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
811a0 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 75 66  >xWrite(id, pBuf
811b0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , amt, offset);.
811c0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
811d0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72   int sqlite3OsTr
811e0 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66  uncate(sqlite3_f
811f0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a  ile *id, i64 siz
81200 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  e){.  return id-
81210 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e  >pMethods->xTrun
81220 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a  cate(id, size);.
81230 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
81240 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79   int sqlite3OsSy
81250 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
81260 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  *id, int flags){
81270 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
81280 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
81290 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
812a0 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73  >xSync(id, flags
812b0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
812c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
812d0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  sFileSize(sqlite
812e0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
812f0 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53  *pSize){.  DO_OS
81300 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
81310 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
81320 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69  Methods->xFileSi
81330 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d  ze(id, pSize);.}
81340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81350 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  int sqlite3OsLoc
81360 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
81370 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65  id, int lockType
81380 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
81390 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
813a0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
813b0 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63  s->xLock(id, loc
813c0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45  kType);.}.SQLITE
813d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
813e0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c  ite3OsUnlock(sql
813f0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
81400 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20  nt lockType){.  
81410 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
81420 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c  ods->xUnlock(id,
81430 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51   lockType);.}.SQ
81440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
81450 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
81460 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
81470 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
81480 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44  t *pResOut){.  D
81490 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
814a0 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  (id);.  return i
814b0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68  d->pMethods->xCh
814c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
814d0 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a  id, pResOut);.}.
814e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
814f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  nt sqlite3OsFile
81500 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f  Control(sqlite3_
81510 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70  file *id, int op
81520 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
81530 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
81540 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72  hods->xFileContr
81550 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 29  ol(id, op, pArg)
81560 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
81570 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
81580 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
81590 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
815a0 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a  int (*xSectorSiz
815b0 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  e)(sqlite3_file*
815c0 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  ) = id->pMethods
815d0 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20  ->xSectorSize;. 
815e0 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 6f 72   return (xSector
815f0 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53 69  Size ? xSectorSi
81600 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45 5f  ze(id) : SQLITE_
81610 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53  DEFAULT_SECTOR_S
81620 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  IZE);.}.SQLITE_P
81630 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
81640 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
81650 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
81660 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72  3_file *id){.  r
81670 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
81680 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61  ds->xDeviceChara
81690 63 74 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a  cteristics(id);.
816a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78  }../*.** The nex
816b0 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  t group of routi
816c0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65  nes are convenie
816d0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f  nce wrappers aro
816e0 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 20 6d  und the.** VFS m
816f0 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ethods..*/.SQLIT
81700 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81710 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 20 73  lite3OsOpen(.  s
81720 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
81730 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
81740 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74  *zPath, .  sqlit
81750 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
81760 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20  .  int flags, . 
81770 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 74 0a   int *pFlagsOut.
81780 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44  ){.  int rc;.  D
81790 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
817a0 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66 31 66  (0);.  /* 0x7f1f
817b0 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20 53 51   is a mask of SQ
817c0 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73  LITE_OPEN_ flags
817d0 20 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20   that are valid 
817e0 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20 20 2a  to be passed.  *
817f0 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 20  * down into the 
81800 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f 6d 65  VFS layer.  Some
81810 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c   SQLITE_OPEN_ fl
81820 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ags (for example
81830 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50  ,.  ** SQLITE_OP
81840 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f 72 20  EN_FULLMUTEX or 
81850 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
81860 45 44 43 41 43 48 45 29 20 61 72 65 20 62 6c 6f  EDCACHE) are blo
81870 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a  cked before.  **
81880 20 72 65 61 63 68 69 6e 67 20 74 68 65 20 56 46   reaching the VF
81890 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70 56 66  S. */.  rc = pVf
818a0 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a  s->xOpen(pVfs, z
818b0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61  Path, pFile, fla
818c0 67 73 20 26 20 30 78 37 66 31 66 2c 20 70 46 6c  gs & 0x7f1f, pFl
818d0 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72  agsOut);.  asser
818e0 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
818f0 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68   || pFile->pMeth
81900 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75  ods==0 );.  retu
81910 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
81920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
81930 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69  te3OsDelete(sqli
81940 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
81950 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
81960 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a  , int dirSync){.
81970 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
81980 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61  Delete(pVfs, zPa
81990 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a  th, dirSync);.}.
819a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
819b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65  nt sqlite3OsAcce
819c0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  ss(.  sqlite3_vf
819d0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
819e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a  t char *zPath, .
819f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20    int flags, .  
81a00 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a  int *pResOut.){.
81a10 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
81a20 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 72 6e  EST(0);.  return
81a30 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70   pVfs->xAccess(p
81a40 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67  Vfs, zPath, flag
81a50 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53  s, pResOut);.}.S
81a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81a70 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  t sqlite3OsFullP
81a80 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74  athname(.  sqlit
81a90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
81aa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
81ab0 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68  th, .  int nPath
81ac0 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50  Out, .  char *zP
81ad0 61 74 68 4f 75 74 0a 29 7b 0a 20 20 7a 50 61 74  athOut.){.  zPat
81ae0 68 4f 75 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72  hOut[0] = 0;.  r
81af0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c  eturn pVfs->xFul
81b00 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20  lPathname(pVfs, 
81b10 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c  zPath, nPathOut,
81b20 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69   zPathOut);.}.#i
81b30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
81b40 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
81b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81b60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44  void *sqlite3OsD
81b70 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
81b80 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63  s *pVfs, const c
81b90 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72  har *zPath){.  r
81ba0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f  eturn pVfs->xDlO
81bb0 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 29  pen(pVfs, zPath)
81bc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
81bd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
81be0 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33  sDlError(sqlite3
81bf0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
81c00 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75  nByte, char *zBu
81c10 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78  fOut){.  pVfs->x
81c20 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 42  DlError(pVfs, nB
81c30 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d  yte, zBufOut);.}
81c40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81c50 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73  void (*sqlite3Os
81c60 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
81c70 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
81c80 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  Hdle, const char
81c90 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a   *zSym))(void){.
81ca0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
81cb0 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64 6c  DlSym(pVfs, pHdl
81cc0 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49  e, zSym);.}.SQLI
81cd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
81ce0 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65  sqlite3OsDlClose
81cf0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
81d00 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
81d10 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43  e){.  pVfs->xDlC
81d20 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64  lose(pVfs, pHand
81d30 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  le);.}.#endif /*
81d40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
81d50 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53  D_EXTENSION */.S
81d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81d70 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f  t sqlite3OsRando
81d80 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
81d90 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79  s *pVfs, int nBy
81da0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  te, char *zBufOu
81db0 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  t){.  return pVf
81dc0 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70  s->xRandomness(p
81dd0 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66  Vfs, nByte, zBuf
81de0 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Out);.}.SQLITE_P
81df0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
81e00 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65  e3OsSleep(sqlite
81e10 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
81e20 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75   nMicro){.  retu
81e30 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28  rn pVfs->xSleep(
81e40 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d  pVfs, nMicro);.}
81e50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
81e60 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72  int sqlite3OsCur
81e70 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
81e80 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62  _vfs *pVfs, doub
81e90 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20  le *pTimeOut){. 
81ea0 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43   return pVfs->xC
81eb0 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c  urrentTime(pVfs,
81ec0 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53   pTimeOut);.}..S
81ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81ee0 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d  t sqlite3OsOpenM
81ef0 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33  alloc(.  sqlite3
81f00 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63  _vfs *pVfs, .  c
81f10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
81f20 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  , .  sqlite3_fil
81f30 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69  e **ppFile, .  i
81f40 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20  nt flags,.  int 
81f50 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20  *pOutFlags.){.  
81f60 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
81f70 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33  NOMEM;.  sqlite3
81f80 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20  _file *pFile;.  
81f90 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33  pFile = (sqlite3
81fa0 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d  _file *)sqlite3M
81fb0 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73  alloc(pVfs->szOs
81fc0 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69  File);.  if( pFi
81fd0 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  le ){.    rc = s
81fe0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
81ff0 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c  s, zFile, pFile,
82000 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67   flags, pOutFlag
82010 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  s);.    if( rc!=
82020 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
82030 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
82040 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73  pFile);.    }els
82050 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65  e{.      *ppFile
82060 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a   = pFile;.    }.
82070 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
82080 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
82090 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
820a0 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33  loseFree(sqlite3
820b0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  _file *pFile){. 
820c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
820d0 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  _OK;.  assert( p
820e0 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73  File );.  rc = s
820f0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46  qlite3OsClose(pF
82100 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ile);.  sqlite3_
82110 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72  free(pFile);.  r
82120 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
82130 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
82140 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
82150 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 65 63  ound the OS spec
82160 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ific implementat
82170 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65  ion of.** sqlite
82180 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 68 65  3_os_init(). The
82190 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20   purpose of the 
821a0 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 70 72  wrapper is to pr
821b0 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 62 69  ovide the.** abi
821c0 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65  lity to simulate
821d0 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
821e0 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 68  e, so that the h
821f0 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a  andling of an.**
82200 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 74 65   error in sqlite
82210 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 20 74  3_os_init() by t
82220 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 73 20  he upper layers 
82230 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e 0a 2a  can be tested..*
82240 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
82250 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e   int sqlite3OsIn
82260 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f 69 64  it(void){.  void
82270 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61   *p = sqlite3_ma
82280 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 28 20  lloc(10);.  if( 
82290 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  p==0 ) return SQ
822a0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71  LITE_NOMEM;.  sq
822b0 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
822c0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
822d0 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f 2a  os_init();.}../*
822e0 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20  .** The list of 
822f0 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 56  all registered V
82300 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
82310 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  ns..*/.static sq
82320 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c 49  lite3_vfs * SQLI
82330 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 3d  TE_WSD vfsList =
82340 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 4c   0;.#define vfsL
82350 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74  ist GLOBAL(sqlit
82360 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 73  e3_vfs *, vfsLis
82370 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  t)../*.** Locate
82380 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e 20   a VFS by name. 
82390 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67   If no name is g
823a0 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74  iven, simply ret
823b0 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  urn the.** first
823c0 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74   VFS on the list
823d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
823e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c  sqlite3_vfs *sql
823f0 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f  ite3_vfs_find(co
82400 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b  nst char *zVfs){
82410 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
82420 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 51  pVfs = 0;.#if SQ
82430 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
82440 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
82450 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23  *mutex;.#endif.#
82460 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
82470 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e  IT_AUTOINIT.  in
82480 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69  t rc = sqlite3_i
82490 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
824a0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 30  f( rc ) return 0
824b0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ;.#endif.#if SQL
824c0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
824d0 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33   mutex = sqlite3
824e0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
824f0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
82500 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20  ASTER);.#endif. 
82510 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
82520 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66  nter(mutex);.  f
82530 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 73  or(pVfs = vfsLis
82540 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 56  t; pVfs; pVfs=pV
82550 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  fs->pNext){.    
82560 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 72  if( zVfs==0 ) br
82570 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 72  eak;.    if( str
82580 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e  cmp(zVfs, pVfs->
82590 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
825a0 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  k;.  }.  sqlite3
825b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
825c0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56  ex);.  return pV
825d0 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c  fs;.}../*.** Unl
825e0 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 74  ink a VFS from t
825f0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a  he linked list.*
82600 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 66  /.static void vf
82610 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f  sUnlink(sqlite3_
82620 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 73  vfs *pVfs){.  as
82630 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
82640 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
82650 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
82660 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
82670 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 28  ASTER)) );.  if(
82680 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20   pVfs==0 ){.    
82690 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65  /* No-op */.  }e
826a0 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 3d  lse if( vfsList=
826b0 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 73  =pVfs ){.    vfs
826c0 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65  List = pVfs->pNe
826d0 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  xt;.  }else if( 
826e0 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 73  vfsList ){.    s
826f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20  qlite3_vfs *p = 
82700 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 69  vfsList;.    whi
82710 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20  le( p->pNext && 
82720 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29  p->pNext!=pVfs )
82730 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  {.      p = p->p
82740 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
82750 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56  if( p->pNext==pV
82760 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  fs ){.      p->p
82770 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65  Next = pVfs->pNe
82780 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  xt;.    }.  }.}.
82790 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
827a0 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73  a VFS with the s
827b0 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 61  ystem.  It is ha
827c0 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 74  rmless to regist
827d0 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56  er the same.** V
827e0 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  FS multiple time
827f0 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 20  s.  The new VFS 
82800 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61  becomes the defa
82810 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 20  ult if makeDflt 
82820 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53  is.** true..*/.S
82830 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
82840 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
82850 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  er(sqlite3_vfs *
82860 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66  pVfs, int makeDf
82870 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  lt){.  sqlite3_m
82880 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b  utex *mutex = 0;
82890 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
828a0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
828b0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
828c0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
828d0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
828e0 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75   rc;.#endif.  mu
828f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
82900 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
82910 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
82920 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ER);.  sqlite3_m
82930 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
82940 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70  );.  vfsUnlink(p
82950 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65  Vfs);.  if( make
82960 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d  Dflt || vfsList=
82970 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e  =0 ){.    pVfs->
82980 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b  pNext = vfsList;
82990 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70  .    vfsList = p
829a0 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Vfs;.  }else{.  
829b0 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20    pVfs->pNext = 
829c0 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  vfsList->pNext;.
829d0 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65      vfsList->pNe
829e0 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20  xt = pVfs;.  }. 
829f0 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29   assert(vfsList)
82a00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
82a10 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
82a20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
82a30 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72  OK;.}../*.** Unr
82a40 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f  egister a VFS so
82a50 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c   that it is no l
82a60 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65  onger accessible
82a70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
82a80 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  int sqlite3_vfs_
82a90 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74  unregister(sqlit
82aa0 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23  e3_vfs *pVfs){.#
82ab0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
82ac0 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d  SAFE.  sqlite3_m
82ad0 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71  utex *mutex = sq
82ae0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
82af0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
82b00 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e  TIC_MASTER);.#en
82b10 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  dif.  sqlite3_mu
82b20 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
82b30 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56  ;.  vfsUnlink(pV
82b40 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  fs);.  sqlite3_m
82b50 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
82b60 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
82b70 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  TE_OK;.}../*****
82b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
82b90 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.c **********
82ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82bc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
82bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
82be0 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a  file fault.c ***
82bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82c10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
82c20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a  2008 Jan 22.**.*
82c30 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
82c40 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
82c50 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
82c60 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
82c70 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
82c80 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
82c90 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
82ca0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
82cb0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
82cc0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
82cd0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
82ce0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
82cf0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
82d00 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
82d10 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
82d20 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
82d30 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
82d40 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
82d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
82d90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
82da0 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70  ins code to supp
82db0 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20  ort the concept 
82dc0 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20  of "benign" .** 
82dd0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
82de0 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f  (when the xMallo
82df0 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28  c() or xRealloc(
82e00 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a  ) method of the.
82e10 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  ** sqlite3_mem_m
82e20 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65  ethods structure
82e30 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61   fails to alloca
82e40 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  te a block of me
82e50 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75  mory.** and retu
82e60 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d  rns 0). .**.** M
82e70 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  ost malloc failu
82e80 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69  res are non-beni
82e90 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f  gn. After they o
82ea0 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  ccur, SQLite.** 
82eb0 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72  abandons the cur
82ec0 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61  rent operation a
82ed0 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  nd returns an er
82ee0 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c  ror code (usuall
82ef0 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  y.** SQLITE_NOME
82f00 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  M) to the user. 
82f10 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d  However, sometim
82f20 65 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f  es a fault is no
82f30 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a  t necessarily.**
82f40 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d   fatal. For exam
82f50 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63  ple, if a malloc
82f60 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73   fails while res
82f70 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61 62  izing a hash tab
82f80 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20  le, this .** is 
82f90 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76  completely recov
82fa0 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79  erable simply by
82fb0 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75   not carrying ou
82fc0 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68  t the resize. Th
82fd0 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  e .** hash table
82fe0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
82ff0 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61  o function norma
83000 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f  lly.  So a mallo
83010 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75  c failure .** du
83020 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c  ring a hash tabl
83030 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 62 65  e resize is a be
83040 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a  nign fault..*/..
83050 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
83060 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
83070 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20  T../*.** Global 
83080 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79  variables..*/.ty
83090 70 65 64 65 66 20 73 74 72 75 63 74 20 42 65 6e  pedef struct Ben
830a0 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42  ignMallocHooks B
830b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
830c0 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  ;.static SQLITE_
830d0 57 53 44 20 73 74 72 75 63 74 20 42 65 6e 69 67  WSD struct Benig
830e0 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20  nMallocHooks {. 
830f0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42   void (*xBenignB
83100 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76  egin)(void);.  v
83110 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64  oid (*xBenignEnd
83120 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74  )(void);.} sqlit
83130 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30  e3Hooks = { 0, 0
83140 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64   };../* The "wsd
83150 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c  Hooks" macro wil
83160 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
83170 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e   appropriate Ben
83180 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a  ignMallocHooks.*
83190 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66  * structure.  If
831a0 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63   writable static
831b0 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f   data is unsuppo
831c0 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67  rted on the targ
831d0 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74  et,.** we have t
831e0 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61  o locate the sta
831f0 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e  te vector at run
83200 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d  -time.  In the m
83210 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61  ore common.** ca
83220 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
83230 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
83240 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48   supported, wsdH
83250 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64  ooks can refer d
83260 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68  irectly.** to th
83270 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22  e "sqlite3Hooks"
83280 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65   state vector de
83290 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f  clared above..*/
832a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
832b0 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65  MIT_WSD.# define
832c0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a   wsdHooksInit \.
832d0 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f    BenignMallocHo
832e0 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c  oks *x = &GLOBAL
832f0 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f  (BenignMallocHoo
83300 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29  ks,sqlite3Hooks)
83310 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
83320 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20  ks x[0].#else.# 
83330 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49  define wsdHooksI
83340 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64  nit.# define wsd
83350 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f  Hooks sqlite3Hoo
83360 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  ks.#endif.../*.*
83370 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73  * Register hooks
83380 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71   to call when sq
83390 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
833a0 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20  Malloc() and.** 
833b0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
833c0 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c  Malloc() are cal
833d0 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c  led, respectivel
833e0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
833f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
83400 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f  3BenignMallocHoo
83410 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65  ks(.  void (*xBe
83420 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29  nignBegin)(void)
83430 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69  ,.  void (*xBeni
83440 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a  gnEnd)(void).){.
83450 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a    wsdHooksInit;.
83460 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69    wsdHooks.xBeni
83470 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67  gnBegin = xBenig
83480 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f  nBegin;.  wsdHoo
83490 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20  ks.xBenignEnd = 
834a0 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f  xBenignEnd;.}../
834b0 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74  *.** This (sqlit
834c0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
834d0 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62  c()) is called b
834e0 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f  y SQLite code to
834f0 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a   indicate that.*
83500 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  * subsequent mal
83510 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65  loc failures are
83520 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20   benign. A call 
83530 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  to sqlite3EndBen
83540 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69  ignMalloc().** i
83550 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 75  ndicates that su
83560 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20  bsequent malloc 
83570 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e  failures are non
83580 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49  -benign..*/.SQLI
83590 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
835a0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
835b0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a  gnMalloc(void){.
835c0 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a    wsdHooksInit;.
835d0 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78    if( wsdHooks.x
835e0 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20  BenignBegin ){. 
835f0 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e     wsdHooks.xBen
83600 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a  ignBegin();.  }.
83610 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
83620 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64   void sqlite3End
83630 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69  BenignMalloc(voi
83640 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e  d){.  wsdHooksIn
83650 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f  it;.  if( wsdHoo
83660 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b  ks.xBenignEnd ){
83670 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42  .    wsdHooks.xB
83680 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a  enignEnd();.  }.
83690 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23  }..#endif   /* #
836a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
836b0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20  IT_BUILTIN_TEST 
836c0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
836d0 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74  *** End of fault
836e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
836f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83710 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
83720 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
83730 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em0.c **********
83740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83760 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f  **/./*.** 2008 O
83770 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20  ctober 28.**.** 
83780 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
83790 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
837a0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
837b0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
837c0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
837d0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
837e0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
837f0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
83800 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
83810 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
83820 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
83830 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
83840 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
83850 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
83860 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
83870 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
83880 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
83890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838d0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
838e0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
838f0 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79  s a no-op memory
83900 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76   allocation driv
83910 65 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e  ers for use when
83920 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f  .** SQLITE_ZERO_
83930 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65  MALLOC is define
83940 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  d.  The allocati
83950 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65  on drivers imple
83960 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61  mented.** here a
83970 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c  lways fail.  SQL
83980 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65  ite will not ope
83990 72 61 74 65 20 77 69 74 68 20 74 68 65 73 65 20  rate with these 
839a0 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a  drivers.  These.
839b0 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c  ** are merely pl
839c0 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61  aceholders.  Rea
839d0 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74 20 62  l drivers must b
839e0 65 20 73 75 62 73 74 69 74 75 74 65 64 20 75 73  e substituted us
839f0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63  ing.** sqlite3_c
83a00 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20 53  onfig() before S
83a10 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61  QLite will opera
83a20 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  te..*/../*.** Th
83a30 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
83a40 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
83a50 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  or is the defaul
83a60 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65  t.  It is.** use
83a70 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20  d when no other 
83a80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
83a90 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73   is specified us
83aa0 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ing compile-time
83ab0 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23  .** macros..*/.#
83ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52  ifdef SQLITE_ZER
83ad0 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20  O_MALLOC../*.** 
83ae0 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f  No-op versions o
83af0 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  f all memory all
83b00 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
83b10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
83b20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f  *sqlite3MemMallo
83b30 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65  c(int nByte){ re
83b40 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63  turn 0; }.static
83b50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
83b60 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
83b70 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74  r){ return; }.st
83b80 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
83b90 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69  e3MemRealloc(voi
83ba0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
83bb0 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b  Byte){ return 0;
83bc0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71   }.static int sq
83bd0 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69  lite3MemSize(voi
83be0 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75  d *pPrior){ retu
83bf0 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69  rn 0; }.static i
83c00 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75  nt sqlite3MemRou
83c10 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74  ndup(int n){ ret
83c20 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20  urn n; }.static 
83c30 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e  int sqlite3MemIn
83c40 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
83c50 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
83c60 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f  _OK; }.static vo
83c70 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
83c80 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
83c90 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a  sed){ return; }.
83ca0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
83cb0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
83cc0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
83cd0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e  file with extern
83ce0 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a  al linkage..**.*
83cf0 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
83d00 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
83d10 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
83d20 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
83d30 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
83d40 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
83d50 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
83d60 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
83d70 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
83d80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
83d90 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
83da0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
83db0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
83dc0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75  em_methods defau
83dd0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20  ltMethods = {.  
83de0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
83df0 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
83e00 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73  3MemFree,.     s
83e10 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
83e20 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
83e30 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69  mSize,.     sqli
83e40 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20  te3MemRoundup,. 
83e50 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e      sqlite3MemIn
83e60 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  it,.     sqlite3
83e70 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20  MemShutdown,.   
83e80 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74    0.  };.  sqlit
83e90 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
83ea0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20  _CONFIG_MALLOC, 
83eb0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
83ec0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
83ed0 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f  QLITE_ZERO_MALLO
83ee0 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  C */../*********
83ef0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
83f00 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  0.c ************
83f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83f30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
83f40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
83f50 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem1.c ********
83f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83f80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
83f90 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a   August 14.**.**
83fa0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
83fb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
83fc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
83fd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
83fe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
83ff0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
84000 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
84010 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
84020 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
84030 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
84040 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
84050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
84060 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
84070 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
84080 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
84090 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
840a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
840b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
84100 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
84110 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  ns low-level mem
84120 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64  ory allocation d
84130 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a  rivers for when.
84140 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
84150 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  se the standard 
84160 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63  C-library malloc
84170 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e  /realloc/free in
84180 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62  terface.** to ob
84190 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20  tain the memory 
841a0 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20  it needs..**.** 
841b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
841c0 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ns implementatio
841d0 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65  ns of the low-le
841e0 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
841f0 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65  ation.** routine
84200 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  s specified in t
84210 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  he sqlite3_mem_m
84220 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a  ethods object..*
84230 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  /../*.** This ve
84240 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
84250 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
84260 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49   the default.  I
84270 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65  t is.** used whe
84280 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72  n no other memor
84290 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73  y allocator is s
842a0 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63  pecified using c
842b0 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d  ompile-time.** m
842c0 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  acros..*/.#ifdef
842d0 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d   SQLITE_SYSTEM_M
842e0 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b  ALLOC../*.** Lik
842f0 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20  e malloc(), but 
84300 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a  remember the siz
84310 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
84320 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77  ion.** so that w
84330 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61  e can find it la
84340 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ter using sqlite
84350 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a  3MemSize()..**.*
84360 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c  * For this low-l
84370 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65  evel routine, we
84380 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20   are guaranteed 
84390 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63  that nByte>0 bec
843a0 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66  ause.** cases of
843b0 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62   nByte<=0 will b
843c0 65 20 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e  e intercepted an
843d0 64 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20  d dealt with by 
843e0 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20  higher level.** 
843f0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
84400 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
84410 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  3MemMalloc(int n
84420 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  Byte){.  sqlite3
84430 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73  _int64 *p;.  ass
84440 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a  ert( nByte>0 );.
84450 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38    nByte = ROUND8
84460 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 6d  (nByte);.  p = m
84470 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38 20 29  alloc( nByte+8 )
84480 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
84490 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20   p[0] = nByte;. 
844a0 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65     p++;.  }.  re
844b0 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a  turn (void *)p;.
844c0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72  }../*.** Like fr
844d0 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73 20 66  ee() but works f
844e0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f  or allocations o
844f0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
84500 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a  ite3MemMalloc().
84510 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d  ** or sqlite3Mem
84520 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  Realloc()..**.**
84530 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65   For this low-le
84540 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20  vel routine, we 
84550 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61  already know tha
84560 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69 6e 63  t pPrior!=0 sinc
84570 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65  e.** cases where
84580 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20   pPrior==0 will 
84590 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 63 65  have been intece
845a0 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77  pted and dealt w
845b0 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 65 72  ith.** by higher
845c0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e  -level routines.
845d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
845e0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76  sqlite3MemFree(v
845f0 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20  oid *pPrior){.  
84600 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
84610 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
84620 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 73  4*)pPrior;.  ass
84630 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29  ert( pPrior!=0 )
84640 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28  ;.  p--;.  free(
84650 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b  p);.}../*.** Lik
84660 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65  e realloc().  Re
84670 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69  size an allocati
84680 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62  on previously ob
84690 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  tained from.** s
846a0 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
846b0 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69  )..**.** For thi
846c0 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65  s low-level inte
846d0 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74  rface, we know t
846e0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e 20 20  hat pPrior!=0.  
846f0 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a 20 70  Cases where.** p
84700 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68  Prior==0 while h
84710 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65  ave been interce
84720 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c  pted by higher-l
84730 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64  evel routine and
84740 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 20 74  .** redirected t
84750 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69  o xMalloc.  Simi
84760 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74  larly, we know t
84770 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61  hat nByte>0 beca
84780 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20 77 68  uses.** cases wh
84790 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c  ere nByte<=0 wil
847a0 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65  l have been inte
847b0 72 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65  rcepted by highe
847c0 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69  r-level.** routi
847d0 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65 63 74  nes and redirect
847e0 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a  ed to xFree..*/.
847f0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
84800 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76  ite3MemRealloc(v
84810 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74  oid *pPrior, int
84820 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74   nByte){.  sqlit
84830 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73  e3_int64 *p = (s
84840 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50  qlite3_int64*)pP
84850 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20  rior;.  assert( 
84860 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 79  pPrior!=0 && nBy
84870 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20  te>0 );.  nByte 
84880 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b  = ROUND8(nByte);
84890 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f  .  p = (sqlite3_
848a0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20  int64*)pPrior;. 
848b0 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 61 6c   p--;.  p = real
848c0 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38 20 29  loc(p, nByte+8 )
848d0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
848e0 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20   p[0] = nByte;. 
848f0 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65     p++;.  }.  re
84900 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d  turn (void*)p;.}
84910 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74  ../*.** Report t
84920 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a  he allocated siz
84930 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72 65 74  e of a prior ret
84940 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63  urn from xMalloc
84950 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c 6c 6f  ().** or xReallo
84960 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  c()..*/.static i
84970 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a  nt sqlite3MemSiz
84980 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b  e(void *pPrior){
84990 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
849a0 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f   *p;.  if( pPrio
849b0 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  r==0 ) return 0;
849c0 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f  .  p = (sqlite3_
849d0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20  int64*)pPrior;. 
849e0 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 28   p--;.  return (
849f0 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a  int)p[0];.}../*.
84a00 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
84a10 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
84a20 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
84a30 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f  ocation size..*/
84a40 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
84a50 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e  te3MemRoundup(in
84a60 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52  t n){.  return R
84a70 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  OUND8(n);.}../*.
84a80 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
84a90 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74  is module..*/.st
84aa0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
84ab0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  MemInit(void *No
84ac0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
84ad0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
84ae0 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ed);.  return SQ
84af0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
84b00 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  * Deinitialize t
84b10 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73  his module..*/.s
84b20 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
84b30 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f  e3MemShutdown(vo
84b40 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
84b50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
84b60 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
84b70 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  urn;.}../*.** Th
84b80 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
84b90 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
84ba0 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
84bb0 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67   external linkag
84bc0 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74  e..**.** Populat
84bd0 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  e the low-level 
84be0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
84bf0 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  n function point
84c00 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65  ers in.** sqlite
84c10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
84c20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
84c30 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   the routines in
84c40 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53   this file..*/.S
84c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
84c60 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74  id sqlite3MemSet
84c70 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20  Default(void){. 
84c80 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
84c90 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
84ca0 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73  s defaultMethods
84cb0 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65   = {.     sqlite
84cc0 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20  3MemMalloc,.    
84cd0 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c   sqlite3MemFree,
84ce0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
84cf0 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71  Realloc,.     sq
84d00 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20  lite3MemSize,.  
84d10 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75     sqlite3MemRou
84d20 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74  ndup,.     sqlit
84d30 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20  e3MemInit,.     
84d40 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f  sqlite3MemShutdo
84d50 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
84d60 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67    sqlite3_config
84d70 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  (SQLITE_CONFIG_M
84d80 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d  ALLOC, &defaultM
84d90 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64  ethods);.}..#end
84da0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53  if /* SQLITE_SYS
84db0 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f  TEM_MALLOC */../
84dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
84dd0 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a  nd of mem1.c ***
84de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
84e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
84e20 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63  egin file mem2.c
84e30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
84e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
84e60 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
84e70 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
84e80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
84e90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
84ea0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
84eb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
84ec0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
84ed0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
84ee0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
84ef0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
84f00 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
84f10 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
84f20 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
84f30 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
84f40 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
84f50 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
84f60 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
84f70 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
84f80 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
84f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fd0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
84fe0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d  le contains low-
84ff0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
85000 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20  ocation drivers 
85010 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69  for when.** SQLi
85020 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20  te will use the 
85030 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61  standard C-libra
85040 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f  ry malloc/reallo
85050 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 65  c/free interface
85060 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68  .** to obtain th
85070 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64  e memory it need
85080 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c  s while adding l
85090 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61  ots of additiona
850a0 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69  l debugging.** i
850b0 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61  nformation to ea
850c0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  ch allocation in
850d0 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64   order to help d
850e0 65 74 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65  etect and fix me
850f0 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e  mory.** leaks an
85100 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65  d memory usage e
85110 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rrors..**.** Thi
85120 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
85130 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
85140 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  of the low-level
85150 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
85160 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73  on.** routines s
85170 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
85180 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
85190 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a  ods object..*/..
851a0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
851b0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
851c0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73   allocator is us
851d0 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a  ed only if the.*
851e0 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55  * SQLITE_MEMDEBU
851f0 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  G macro is defin
85200 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ed.*/.#ifdef SQL
85210 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a  ITE_MEMDEBUG../*
85220 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63  .** The backtrac
85230 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20  e functionality 
85240 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
85250 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a  e with GLIBC.*/.
85260 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f  #ifdef __GLIBC__
85270 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61  .  extern int ba
85280 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69  cktrace(void**,i
85290 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f  nt);.  extern vo
852a0 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d  id backtrace_sym
852b0 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e  bols_fd(void*con
852c0 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65  st*,int,int);.#e
852d0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63  lse.# define bac
852e0 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20  ktrace(A,B) 1.# 
852f0 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65  define backtrace
85300 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c  _symbols_fd(A,B,
85310 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  C).#endif../*.**
85320 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c   Each memory all
85330 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69  ocation looks li
85340 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
85350 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85360 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85370 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85390 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54  --------.**  | T
853a0 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63  itle |  backtrac
853b0 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65  e pointers |  Me
853c0 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c  mBlockHdr |  all
853d0 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75  ocation |  EndGu
853e0 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  ard |.**  ------
853f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85400 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85410 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85420 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85430 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70  --.**.** The app
85440 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65  lication code se
85450 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65  es only a pointe
85460 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74  r to the allocat
85470 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a  ion.  We have.**
85480 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d   to back up from
85490 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
854a0 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20  pointer to find 
854b0 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e  the MemBlockHdr.
854c0 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63    The.** MemBloc
854d0 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68  kHdr tells us th
854e0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c  e size of the al
854f0 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65  location and the
85500 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61   number of.** ba
85510 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73  cktrace pointers
85520 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f  .  There is also
85530 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 74   a guard word at
85540 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
85550 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a  ** MemBlockHdr..
85560 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  */.struct MemBlo
85570 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53  ckHdr {.  i64 iS
85580 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
85590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
855a0 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c   Size of this al
855b0 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  location */.  st
855c0 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
855d0 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b   *pNext, *pPrev;
855e0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
855f0 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
85600 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72  memory */.  char
85610 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20   nBacktrace;    
85620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85630 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63  /* Number of bac
85640 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20  ktraces on this 
85650 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20  alloc */.  char 
85660 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
85670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
85680 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b  * Available back
85690 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20  trace slots */. 
856a0 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20   short nTitle;  
856b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
856c0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
856d0 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73   title; includes
856e0 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69   '\0' */.  int i
856f0 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 20  ForeGuard;      
85700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
85710 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72  * Guard word for
85720 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f   sanity */.};../
85730 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73  *.** Guard words
85740 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45  .*/.#define FORE
85750 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33  GUARD 0x80F5E153
85760 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41  .#define REARGUA
85770 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f  RD 0xE4676B53../
85780 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d  *.** Number of m
85790 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65  alloc size incre
857a0 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a  ments to track..
857b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a  */.#define NCSIZ
857c0 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41  E  1000../*.** A
857d0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
857e0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
857f0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
85800 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
85810 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
85820 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
85830 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f  em".  This is to
85840 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61   keep the.** sta
85850 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72  tic variables or
85860 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72  ganized and to r
85870 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20  educe namespace 
85880 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65  pollution.** whe
85890 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  n this module is
858a0 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f   combined with o
858b0 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c  ther in the amal
858c0 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  gamation..*/.sta
858d0 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a  tic struct {.  .
858e0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20    /*.  ** Mutex 
858f0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
85900 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
85910 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
85920 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  stem..  */.  sql
85930 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
85940 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65  x;..  /*.  ** He
85950 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61  ad and tail of a
85960 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
85970 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  all outstanding 
85980 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f  allocations.  */
85990 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
859a0 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20  ckHdr *pFirst;. 
859b0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
859c0 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20  Hdr *pLast;.  . 
859d0 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d   /*.  ** The num
859e0 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66  ber of levels of
859f0 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61   backtrace to sa
85a00 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61  ve in new alloca
85a10 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e  tions..  */.  in
85a20 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20  t nBacktrace;.  
85a30 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63  void (*xBacktrac
85a40 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69  e)(int, int, voi
85a50 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  d **);..  /*.  *
85a60 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20  * Title text to 
85a70 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20  insert in front 
85a80 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20  of each block.  
85a90 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b  */.  int nTitle;
85aa0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
85ab0 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61   of zTitle to sa
85ac0 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c  ve.  Includes '\
85ad0 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a  0' and padding *
85ae0 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b  /.  char zTitle[
85af0 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69  100];  /* The ti
85b00 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f  tle text */..  /
85b10 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d  * .  ** sqlite3M
85b20 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20  allocDisallow() 
85b30 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66  increments the f
85b40 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72  ollowing counter
85b50 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61  ..  ** sqlite3Ma
85b60 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72  llocAllow() decr
85b70 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a  ements it..  */.
85b80 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20    int disallow; 
85b90 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  /* Do not allow 
85ba0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
85bb0 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  n */..  /*.  ** 
85bc0 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69 63  Gather statistic
85bd0 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f  s on the sizes o
85be0 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  f memory allocat
85bf0 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f  ions..  ** nAllo
85c00 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62  c[i] is the numb
85c10 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
85c20 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38   attempts of i*8
85c30 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d  .  ** bytes.  i=
85c40 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e  =NCSIZE is the n
85c50 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
85c60 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72  ion attempts for
85c70 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65  .  ** sizes more
85c80 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62   than NCSIZE*8 b
85c90 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  ytes..  */.  int
85ca0 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b   nAlloc[NCSIZE];
85cb0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
85cc0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
85cd0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  ions */.  int nC
85ce0 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20  urrent[NCSIZE]; 
85cf0 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
85d00 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
85d10 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43  ons */.  int mxC
85d20 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20  urrent[NCSIZE]; 
85d30 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d    /* Highwater m
85d40 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74  ark for nCurrent
85d50 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a   */..} mem;.../*
85d60 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72  .** Adjust memor
85d70 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74 69  y usage statisti
85d80 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  cs.*/.static voi
85d90 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e  d adjustStats(in
85da0 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63  t iSize, int inc
85db0 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69  rement){.  int i
85dc0 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65 29   = ROUND8(iSize)
85dd0 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49  /8;.  if( i>NCSI
85de0 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20  ZE-1 ){.    i = 
85df0 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a  NCSIZE - 1;.  }.
85e00 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e    if( increment>
85e10 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c  0 ){.    mem.nAl
85e20 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65  loc[i]++;.    me
85e30 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b  m.nCurrent[i]++;
85e40 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75  .    if( mem.nCu
85e50 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43  rrent[i]>mem.mxC
85e60 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20  urrent[i] ){.   
85e70 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74     mem.mxCurrent
85e80 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65  [i] = mem.nCurre
85e90 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  nt[i];.    }.  }
85ea0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43  else{.    mem.nC
85eb0 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20  urrent[i]--;.   
85ec0 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75   assert( mem.nCu
85ed0 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20  rrent[i]>=0 );. 
85ee0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
85ef0 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  n an allocation,
85f00 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f   find the MemBlo
85f10 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61  ckHdr for that a
85f20 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
85f30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
85f40 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20  ecks the guards 
85f50 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66  at either end of
85f60 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
85f70 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61  and.** if they a
85f80 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20  re incorrect it 
85f90 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74  asserts..*/.stat
85fa0 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  ic struct MemBlo
85fb0 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65  ckHdr *sqlite3Me
85fc0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f  msysGetHeader(vo
85fd0 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29  id *pAllocation)
85fe0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
85ff0 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74  ockHdr *p;.  int
86000 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55   *pInt;.  u8 *pU
86010 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76  8;.  int nReserv
86020 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63  e;..  p = (struc
86030 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70  t MemBlockHdr*)p
86040 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d  Allocation;.  p-
86050 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  -;.  assert( p->
86060 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e 74  iForeGuard==(int
86070 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20  )FOREGUARD );.  
86080 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e 44  nReserve = ROUND
86090 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 70  8(p->iSize);.  p
860a0 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c  Int = (int*)pAll
860b0 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d  ocation;.  pU8 =
860c0 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f   (u8*)pAllocatio
860d0 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  n;.  assert( pIn
860e0 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f  t[nReserve/sizeo
860f0 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45  f(int)]==(int)RE
86100 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a 20  ARGUARD );.  /* 
86110 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79 20  This checks any 
86120 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20 62  of the "extra" b
86130 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 64  ytes allocated d
86140 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e 64  ue.  ** to round
86150 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20 62  ing up to an 8 b
86160 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f 20  yte boundary to 
86170 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68 65  ensure .  ** the
86180 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20 6f  y haven't been o
86190 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f  verwritten..  */
861a0 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65 72  .  while( nReser
861b0 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 20  ve-- > p->iSize 
861c0 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e 52  ) assert( pU8[nR
861d0 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29 3b  eserve]==0x65 );
861e0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
861f0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
86200 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
86210 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
86220 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73 20  ated at address 
86230 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
86240 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28   sqlite3MemSize(
86250 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 75  void *p){.  stru
86260 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
86270 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 29  pHdr;.  if( !p )
86280 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
86290 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c    }.  pHdr = sql
862a0 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61  ite3MemsysGetHea
862b0 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  der(p);.  return
862c0 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a   pHdr->iSize;.}.
862d0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
862e0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
862f0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
86300 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
86310 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28   sqlite3MemInit(
86320 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
86330 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
86340 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61  ER(NotUsed);.  a
86350 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 73  ssert( (sizeof(s
86360 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
86370 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20 20  r)&7) == 0 );.  
86380 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
86390 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
863a0 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d  t ){.    /* If m
863b0 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73 20  emory status is 
863c0 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  enabled, then th
863d0 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70  e malloc.c wrapp
863e0 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a  er will already.
863f0 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20      ** hold the 
86400 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78  STATIC_MEM mutex
86410 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69 6e   when the routin
86420 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76 6f  es here are invo
86430 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e  ked. */.    mem.
86440 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
86450 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
86460 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
86470 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  M);.  }.  return
86480 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
86490 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a  *.** Deinitializ
864a0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
864b0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
864c0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
864d0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74  d sqlite3MemShut
864e0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
864f0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
86500 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
86510 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20  ;.  mem.mutex = 
86520 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e  0;.}../*.** Roun
86530 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73  d up a request s
86540 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ize to the next 
86550 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  valid allocation
86560 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   size..*/.static
86570 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52   int sqlite3MemR
86580 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
86590 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e   return ROUND8(n
865a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  );.}../*.** Allo
865b0 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73  cate nByte bytes
865c0 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73   of memory..*/.s
865d0 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
865e0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74  te3MemMalloc(int
865f0 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63   nByte){.  struc
86600 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
86610 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42  Hdr;.  void **pB
86620 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  t;.  char *z;.  
86630 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69  int *pInt;.  voi
86640 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20  d *p = 0;.  int 
86650 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74  totalSize;.  int
86660 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c   nReserve;.  sql
86670 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
86680 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61  (mem.mutex);.  a
86690 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c  ssert( mem.disal
866a0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73  low==0 );.  nRes
866b0 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42  erve = ROUND8(nB
866c0 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a  yte);.  totalSiz
866d0 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73  e = nReserve + s
866e0 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73  izeof(*pHdr) + s
866f0 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20  izeof(int) +.   
86700 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e              mem.
86710 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f  nBacktrace*sizeo
86720 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e  f(void*) + mem.n
86730 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c  Title;.  p = mal
86740 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a  loc(totalSize);.
86750 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a    if( p ){.    z
86760 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20   = p;.    pBt = 
86770 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e  (void**)&z[mem.n
86780 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72  Title];.    pHdr
86790 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c   = (struct MemBl
867a0 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d  ockHdr*)&pBt[mem
867b0 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20  .nBacktrace];.  
867c0 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20    pHdr->pNext = 
867d0 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72  0;.    pHdr->pPr
867e0 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a  ev = mem.pLast;.
867f0 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73      if( mem.pLas
86800 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70  t ){.      mem.p
86810 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48  Last->pNext = pH
86820 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  dr;.    }else{. 
86830 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20       mem.pFirst 
86840 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20  = pHdr;.    }.  
86850 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48    mem.pLast = pH
86860 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46  dr;.    pHdr->iF
86870 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47  oreGuard = FOREG
86880 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e  UARD;.    pHdr->
86890 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20  nBacktraceSlots 
868a0 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65  = mem.nBacktrace
868b0 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74  ;.    pHdr->nTit
868c0 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b  le = mem.nTitle;
868d0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61  .    if( mem.nBa
868e0 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  cktrace ){.     
868f0 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d   void *aAddr[40]
86900 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42  ;.      pHdr->nB
86910 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74  acktrace = backt
86920 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e  race(aAddr, mem.
86930 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b  nBacktrace+1)-1;
86940 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42  .      memcpy(pB
86950 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48  t, &aAddr[1], pH
86960 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73  dr->nBacktrace*s
86970 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20  izeof(void*));. 
86980 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74 5b       assert(pBt[
86990 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  0]);.      if( m
869a0 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29 7b  em.xBacktrace ){
869b0 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42 61  .        mem.xBa
869c0 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20 70  cktrace(nByte, p
869d0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d  Hdr->nBacktrace-
869e0 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a 20  1, &aAddr[1]);. 
869f0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
86a00 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42  {.      pHdr->nB
86a10 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20 20  acktrace = 0;.  
86a20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e    }.    if( mem.
86a30 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20 20  nTitle ){.      
86a40 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a 54  memcpy(z, mem.zT
86a50 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c 65  itle, mem.nTitle
86a60 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48 64  );.    }.    pHd
86a70 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74 65  r->iSize = nByte
86a80 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61 74  ;.    adjustStat
86a90 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20 20  s(nByte, +1);.  
86aa0 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 26    pInt = (int*)&
86ab0 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49 6e  pHdr[1];.    pIn
86ac0 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f  t[nReserve/sizeo
86ad0 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47 55  f(int)] = REARGU
86ae0 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  ARD;.    memset(
86af0 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65 73  pInt, 0x65, nRes
86b00 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20 28  erve);.    p = (
86b10 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a  void*)pInt;.  }.
86b20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
86b30 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
86b40 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d  ;.  return p; .}
86b50 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
86b60 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ory..*/.static v
86b70 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72  oid sqlite3MemFr
86b80 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ee(void *pPrior)
86b90 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
86ba0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
86bb0 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68  void **pBt;.  ch
86bc0 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28  ar *z;.  assert(
86bd0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
86be0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c  nfig.bMemstat ||
86bf0 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b   mem.mutex!=0 );
86c00 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65  .  pHdr = sqlite
86c10 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72  3MemsysGetHeader
86c20 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74 20  (pPrior);.  pBt 
86c30 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a  = (void**)pHdr;.
86c40 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e    pBt -= pHdr->n
86c50 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a  BacktraceSlots;.
86c60 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
86c70 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29  enter(mem.mutex)
86c80 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 50  ;.  if( pHdr->pP
86c90 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65 72  rev ){.    asser
86ca0 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e  t( pHdr->pPrev->
86cb0 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20  pNext==pHdr );. 
86cc0 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e     pHdr->pPrev->
86cd0 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e  pNext = pHdr->pN
86ce0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ext;.  }else{.  
86cf0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 46    assert( mem.pF
86d00 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20  irst==pHdr );.  
86d10 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70    mem.pFirst = p
86d20 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  Hdr->pNext;.  }.
86d30 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65 78    if( pHdr->pNex
86d40 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  t ){.    assert(
86d50 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50   pHdr->pNext->pP
86d60 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20  rev==pHdr );.   
86d70 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50   pHdr->pNext->pP
86d80 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72 65  rev = pHdr->pPre
86d90 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  v;.  }else{.    
86da0 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73  assert( mem.pLas
86db0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d  t==pHdr );.    m
86dc0 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 2d  em.pLast = pHdr-
86dd0 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20  >pPrev;.  }.  z 
86de0 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20 20  = (char*)pBt;.  
86df0 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c  z -= pHdr->nTitl
86e00 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74 73  e;.  adjustStats
86e10 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31  (pHdr->iSize, -1
86e20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20 30  );.  memset(z, 0
86e30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69 64  x2b, sizeof(void
86e40 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72  *)*pHdr->nBacktr
86e50 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65 6f  aceSlots + sizeo
86e60 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20 20  f(*pHdr) +.     
86e70 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48 64               pHd
86e80 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65 6f  r->iSize + sizeo
86e90 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e 6e  f(int) + pHdr->n
86ea0 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28 7a  Title);.  free(z
86eb0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
86ec0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74  ex_leave(mem.mut
86ed0 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ex);  .}../*.** 
86ee0 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20  Change the size 
86ef0 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d  of an existing m
86f00 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
86f10 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ..**.** For this
86f20 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c 65   debugging imple
86f30 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a 61  mentation, we *a
86f40 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63 6f  lways* make a co
86f50 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 6c  py of the.** all
86f60 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20 6e  ocation into a n
86f70 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d 6f  ew place in memo
86f80 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79  ry.  In this way
86f90 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69 67  , if the .** hig
86fa0 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20 69  her level code i
86fb0 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 20  s using pointer 
86fc0 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63  to the old alloc
86fd0 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a 2a  ation, it is .**
86fe0 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c   much more likel
86ff0 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20 77  y to break and w
87000 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65 20  e are much more 
87010 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a 2a  liking to find.*
87020 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a  * the error..*/.
87030 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
87040 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76  ite3MemRealloc(v
87050 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74  oid *pPrior, int
87060 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63   nByte){.  struc
87070 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
87080 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a  OldHdr;.  void *
87090 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28 20  pNew;.  assert( 
870a0 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20  mem.disallow==0 
870b0 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 73  );.  pOldHdr = s
870c0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
870d0 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20  eader(pPrior);. 
870e0 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d   pNew = sqlite3M
870f0 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  emMalloc(nByte);
87100 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20  .  if( pNew ){. 
87110 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20     memcpy(pNew, 
87120 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 4f  pPrior, nByte<pO
87130 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 6e  ldHdr->iSize ? n
87140 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d 3e  Byte : pOldHdr->
87150 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20  iSize);.    if( 
87160 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e 69  nByte>pOldHdr->i
87170 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d 65  Size ){.      me
87180 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 4e  mset(&((char*)pN
87190 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 69  ew)[pOldHdr->iSi
871a0 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74 65  ze], 0x2b, nByte
871b0 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a   - pOldHdr->iSiz
871c0 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
871d0 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 72  lite3MemFree(pPr
871e0 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ior);.  }.  retu
871f0 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
87200 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
87210 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
87220 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
87230 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
87240 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
87250 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
87260 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
87270 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
87280 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
87290 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
872a0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
872b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
872c0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
872d0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75  em_methods defau
872e0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20  ltMethods = {.  
872f0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
87300 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
87310 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73  3MemFree,.     s
87320 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
87330 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
87340 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69  mSize,.     sqli
87350 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20  te3MemRoundup,. 
87360 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e      sqlite3MemIn
87370 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  it,.     sqlite3
87380 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20  MemShutdown,.   
87390 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74    0.  };.  sqlit
873a0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
873b0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20  _CONFIG_MALLOC, 
873c0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
873d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
873e0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61 63  he number of bac
873f0 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b 65  ktrace levels ke
87400 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c 6f  pt for each allo
87410 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 6c  cation..** A val
87420 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73  ue of zero turns
87430 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e 67   off backtracing
87440 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69 73  .  The number is
87450 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64 0a   always rounded.
87460 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74 69  ** up to a multi
87470 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 4c  ple of 2..*/.SQL
87480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87490 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
874a0 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64 65  Backtrace(int de
874b0 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70 74  pth){.  if( dept
874c0 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20 30  h<0 ){ depth = 0
874d0 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68 3e  ; }.  if( depth>
874e0 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32 30  20 ){ depth = 20
874f0 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28 64  ; }.  depth = (d
87500 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20 20  epth+1)&0xfe;.  
87510 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 3d  mem.nBacktrace =
87520 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 54   depth;.}..SQLIT
87530 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
87540 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 61  qlite3MemdebugBa
87550 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b 28  cktraceCallback(
87560 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63  void (*xBacktrac
87570 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69  e)(int, int, voi
87580 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 42  d **)){.  mem.xB
87590 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63 6b  acktrace = xBack
875a0 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  trace;.}../*.** 
875b0 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73 74  Set the title st
875c0 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71 75  ring for subsequ
875d0 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ent allocations.
875e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
875f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
87600 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65 28  emdebugSettitle(
87610 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69 74  const char *zTit
87620 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  le){.  unsigned 
87630 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
87640 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29 20  trlen30(zTitle) 
87650 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  + 1;.  sqlite3_m
87660 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
87670 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e 3d  utex);.  if( n>=
87680 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c  sizeof(mem.zTitl
87690 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66 28  e) ) n = sizeof(
876a0 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a 20  mem.zTitle)-1;. 
876b0 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 74   memcpy(mem.zTit
876c0 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b 0a  le, zTitle, n);.
876d0 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d 20    mem.zTitle[n] 
876e0 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 6c  = 0;.  mem.nTitl
876f0 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 20  e = ROUND8(n);. 
87700 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
87710 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b  eave(mem.mutex);
87720 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
87730 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
87740 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a 20  emdebugSync(){. 
87750 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
87760 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f 72  Hdr *pHdr;.  for
87770 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74  (pHdr=mem.pFirst
87780 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64  ; pHdr; pHdr=pHd
87790 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 76  r->pNext){.    v
877a0 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f 69  oid **pBt = (voi
877b0 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70 42  d**)pHdr;.    pB
877c0 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b  t -= pHdr->nBack
877d0 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20  traceSlots;.    
877e0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 70  mem.xBacktrace(p
877f0 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64 72  Hdr->iSize, pHdr
87800 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c 20  ->nBacktrace-1, 
87810 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d 0a  &pBt[1]);.  }.}.
87820 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
87830 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61  file indicated a
87840 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f  nd write a log o
87850 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65  f all unfreed me
87860 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74  mory .** allocat
87870 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c  ions into that l
87880 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  og..*/.SQLITE_PR
87890 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
878a0 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28 63  e3MemdebugDump(c
878b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
878c0 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f  name){.  FILE *o
878d0 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d  ut;.  struct Mem
878e0 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a  BlockHdr *pHdr;.
878f0 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20    void **pBt;.  
87900 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20 66  int i;.  out = f
87910 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  open(zFilename, 
87920 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d  "w");.  if( out=
87930 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  =0 ){.    fprint
87940 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e  f(stderr, "** Un
87950 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d  able to output m
87960 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70  emory debug outp
87970 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22  ut log: %s **\n"
87980 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
87990 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29        zFilename)
879a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
879b0 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d  }.  for(pHdr=mem
879c0 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70  .pFirst; pHdr; p
879d0 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29  Hdr=pHdr->pNext)
879e0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20  {.    char *z = 
879f0 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20 20  (char*)pHdr;.   
87a00 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63   z -= pHdr->nBac
87a10 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a 65  ktraceSlots*size
87a20 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64 72  of(void*) + pHdr
87a30 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66 70  ->nTitle;.    fp
87a40 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a 2a  rintf(out, "****
87a50 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20 25   %lld bytes at %
87a60 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c 6e  p from %s ****\n
87a70 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ", .            
87a80 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70 48  pHdr->iSize, &pH
87a90 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 69  dr[1], pHdr->nTi
87aa0 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 29  tle ? z : "???")
87ab0 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d 3e  ;.    if( pHdr->
87ac0 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20  nBacktrace ){.  
87ad0 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29 3b      fflush(out);
87ae0 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76 6f  .      pBt = (vo
87af0 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 20  id**)pHdr;.     
87b00 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42   pBt -= pHdr->nB
87b10 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20  acktraceSlots;. 
87b20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f 73       backtrace_s
87b30 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20 70  ymbols_fd(pBt, p
87b40 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2c  Hdr->nBacktrace,
87b50 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a 20   fileno(out));. 
87b60 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
87b70 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20  , "\n");.    }. 
87b80 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74   }.  fprintf(out
87b90 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b 0a  , "COUNTS:\n");.
87ba0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53    for(i=0; i<NCS
87bb0 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20  IZE-1; i++){.   
87bc0 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b   if( mem.nAlloc[
87bd0 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  i] ){.      fpri
87be0 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64  ntf(out, "   %5d
87bf0 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64  : %10d %10d %10d
87c00 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  \n", .          
87c10 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f    i*8, mem.nAllo
87c20 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 65  c[i], mem.nCurre
87c30 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72  nt[i], mem.mxCur
87c40 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  rent[i]);.    }.
87c50 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e 41    }.  if( mem.nA
87c60 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 29  lloc[NCSIZE-1] )
87c70 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
87c80 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 64  t, "   %5d: %10d
87c90 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a 20   %10d %10d\n",. 
87ca0 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53 49              NCSI
87cb0 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c 6c  ZE*8-8, mem.nAll
87cc0 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 20  oc[NCSIZE-1],.  
87cd0 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e             mem.n
87ce0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31  Current[NCSIZE-1
87cf0 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74  ], mem.mxCurrent
87d00 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 7d  [NCSIZE-1]);.  }
87d10 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a  .  fclose(out);.
87d20 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
87d30 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
87d40 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61  mes sqlite3MemMa
87d50 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e 20  lloc() has been 
87d60 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
87d70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
87d80 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61 6c  lite3MemdebugMal
87d90 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69 6e  locCount(){.  in
87da0 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 61  t i;.  int nTota
87db0 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  l = 0;.  for(i=0
87dc0 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 29  ; i<NCSIZE; i++)
87dd0 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20  {.    nTotal += 
87de0 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a 20  mem.nAlloc[i];. 
87df0 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f 74   }.  return nTot
87e00 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f  al;.}...#endif /
87e10 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55  * SQLITE_MEMDEBU
87e20 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  G */../*********
87e30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
87e40 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
87e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87e70 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
87e80 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
87e90 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem3.c ********
87ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ec0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
87ed0 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a   October 14.**.*
87ee0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
87ef0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
87f00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
87f10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
87f20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
87f30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
87f40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
87f50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
87f60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
87f70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
87f80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
87f90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
87fa0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
87fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
87fc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
87fd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
87fe0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
87ff0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
88000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
88040 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
88050 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
88060 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
88070 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  a memory.** allo
88080 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
88090 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69   for use by SQLi
880a0 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  te. .**.** This 
880b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
880c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
880d0 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 73   subsystem omits
880e0 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d   all.** use of m
880f0 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51 4c  alloc(). The SQL
88100 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69 65  ite user supplie
88110 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  s a block of mem
88120 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ory.** before ca
88130 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
88140 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20  itialize() from 
88150 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  which allocation
88160 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e  s.** are made an
88170 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  d returned by th
88180 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e xMalloc() and 
88190 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69  xRealloc() .** i
881a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20  mplementations. 
881b0 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  Once sqlite3_ini
881c0 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65  tialize() has be
881d0 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68  en called,.** th
881e0 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
881f0 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  ry available to 
88200 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20  SQLite is fixed 
88210 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  and cannot.** be
88220 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
88230 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
88240 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
88250 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
88260 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  is included.** i
88270 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79  n the build only
88280 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
88290 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 66  E_MEMSYS3 is def
882a0 69 6e 65 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ined..*/../*.** 
882b0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
882c0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
882d0 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69  ator is only bui
882e0 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72  lt into the libr
882f0 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e  ary.** SQLITE_EN
88300 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20  ABLE_MEMSYS3 is 
88310 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e  defined. Definin
88320 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f  g this symbol do
88330 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74  es not.** mean t
88340 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  hat the library 
88350 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72  will use a memor
88360 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c  y-pool by defaul
88370 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20  t, just that.** 
88380 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  it is available.
88390 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c   The mempool all
883a0 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61  ocator is activa
883b0 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a  ted by calling.*
883c0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  * sqlite3_config
883d0 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ()..*/.#ifdef SQ
883e0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
883f0 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  YS3../*.** Maxim
88400 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33  um size (in Mem3
88410 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d  Blocks) of a "sm
88420 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23  all" chunk..*/.#
88430 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20  define MX_SMALL 
88440 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65  10.../*.** Numbe
88450 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61  r of freelist ha
88460 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66  sh slots.*/.#def
88470 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a  ine N_HASH  61..
88480 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61  /*.** A memory a
88490 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20  llocation (also 
884a0 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22  called a "chunk"
884b0 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  ) consists of tw
884c0 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c  o or .** more bl
884d0 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20  ocks where each 
884e0 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73  block is 8 bytes
884f0 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62  .  The first 8 b
88500 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68  ytes are .** a h
88510 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f  eader that is no
88520 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  t returned to th
88530 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  e user..**.** A 
88540 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20  chunk is two or 
88550 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74  more blocks that
88560 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b   is either check
88570 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65  ed out or.** fre
88580 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c  e.  The first bl
88590 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75  ock has format u
885a0 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a  .hdr.  u.hdr.siz
885b0 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74  e4x is 4 times t
885c0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  he.** size of th
885d0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  e allocation in 
885e0 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c  blocks if the al
885f0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
88600 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73  ..** The u.hdr.s
88610 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74  ize4x&1 bit is t
88620 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b  rue if the chunk
88630 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20   is checked out 
88640 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
88650 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20  the chunk is on 
88660 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54  the freelist.  T
88670 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  he u.hdr.size4x&
88680 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65  2 bit.** is true
88690 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
886a0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
886b0 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20  d out and false 
886c0 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f  if the.** previo
886d0 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65  us chunk is free
886e0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65  .  The u.hdr.pre
886f0 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74  vSize field is t
88700 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68  he size of.** th
88710 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
88720 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68   in blocks if th
88730 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
88740 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72   is on the.** fr
88750 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70  eelist. If the p
88760 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73  revious chunk is
88770 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68   checked out, th
88780 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76  en.** u.hdr.prev
88790 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74  Size can be part
887a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72   of the data for
887b0 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20   that chunk and 
887c0 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65  should.** not be
887d0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
887e0 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e  ..**.** We often
887f0 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e   identify a chun
88800 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69  k by its index i
88810 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20  n mem3.aPool[]. 
88820 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73   When.** this is
88830 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b   done, the chunk
88840 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f   index refers to
88850 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63   the second bloc
88860 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e  k of.** the chun
88870 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  k.  In this way,
88880 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b   the first chunk
88890 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
888a0 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69   1..** A chunk i
888b0 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20  ndex of 0 means 
888c0 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20  "no such chunk" 
888d0 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76  and is the equiv
888e0 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55  alent.** of a NU
888f0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
88900 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f  * The second blo
88910 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b  ck of free chunk
88920 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  s is of the form
88930 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a   u.list.  The.**
88940 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d   two fields form
88950 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64   a double-linked
88960 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20   list of chunks 
88970 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73  of related sizes
88980 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f  ..** Pointers to
88990 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
889a0 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64   list are stored
889b0 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c   in mem3.aiSmall
889c0 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c  [] .** for small
889d0 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65  er chunks and me
889e0 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20  m3.aiHash[] for 
889f0 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a  larger chunks..*
88a00 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
88a10 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b  block of a chunk
88a20 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66   is user data if
88a30 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68   the chunk is ch
88a40 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20  ecked .** out.  
88a50 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68  If a chunk is ch
88a60 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75  ecked out, the u
88a70 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74  ser data may ext
88a80 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  end into.** the 
88a90 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76  u.hdr.prevSize v
88aa0 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  alue of the foll
88ab0 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  owing chunk..*/.
88ac0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
88ad0 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f  em3Block Mem3Blo
88ae0 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42  ck;.struct Mem3B
88af0 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b  lock {.  union {
88b00 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
88b10 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65      u32 prevSize
88b20 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70  ;   /* Size of p
88b30 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e  revious chunk in
88b40 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65   Mem3Block eleme
88b50 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  nts */.      u32
88b60 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20   size4x;     /* 
88b70 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63  4x the size of c
88b80 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20  urrent chunk in 
88b90 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e  Mem3Block elemen
88ba0 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b  ts */.    } hdr;
88bb0 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
88bc0 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20      u32 next;   
88bd0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
88be0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20  mem3.aPool[] of 
88bf0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
88c00 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65  */.      u32 pre
88c10 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  v;       /* Inde
88c20 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  x in mem3.aPool[
88c30 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72  ] of previous fr
88c40 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20  ee chunk */.    
88c50 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d  } list;.  } u;.}
88c60 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  ;../*.** All of 
88c70 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61  the static varia
88c80 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69  bles used by thi
88c90 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c  s module are col
88ca0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61  lected.** into a
88cb0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72   single structur
88cc0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20  e named "mem3". 
88cd0 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70   This is to keep
88ce0 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76   the.** static v
88cf0 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a  ariables organiz
88d00 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65  ed and to reduce
88d10 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75   namespace pollu
88d20 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  tion.** when thi
88d30 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62  s module is comb
88d40 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20  ined with other 
88d50 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  in the amalgamat
88d60 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  ion..*/.static S
88d70 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
88d80 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20   Mem3Global {.  
88d90 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61  /*.  ** Memory a
88da0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
88db0 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69  ocation. nPool i
88dc0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
88dd0 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e  e array.  ** (in
88de0 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69   Mem3Blocks) poi
88df0 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c  nted to by aPool
88e00 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20   less 2..  */.  
88e10 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d  u32 nPool;.  Mem
88e20 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a  3Block *aPool;..
88e30 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69    /*.  ** True i
88e40 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74  f we are evaluat
88e50 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65  ing an out-of-me
88e60 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20  mory callback.. 
88e70 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42   */.  int alarmB
88e80 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  usy;.  .  /*.  *
88e90 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
88ea0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
88eb0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
88ec0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
88ed0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
88ee0 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20  ex *mutex;.  .  
88ef0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69  /*.  ** The mini
88f00 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72  mum amount of fr
88f10 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65  ee space that we
88f20 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f   have seen..  */
88f30 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b  .  u32 mnMaster;
88f40 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73  ..  /*.  ** iMas
88f50 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78  ter is the index
88f60 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
88f70 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20  hunk.  Most new 
88f80 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a  allocations.  **
88f90 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68   occur off of th
88fa0 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73  is chunk.  szMas
88fb0 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20  ter is the size 
88fc0 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a  (in Mem3Blocks).
88fd0 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72    ** of the curr
88fe0 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61  ent master.  iMa
88ff0 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65  ster is 0 if the
89000 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72  re is not master
89010 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65   chunk..  ** The
89020 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73   master chunk is
89030 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74   not in either t
89040 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61  he aiHash[] or a
89050 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20  iSmall[]..  */. 
89060 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20   u32 iMaster;.  
89070 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20  u32 szMaster;.. 
89080 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f   /*.  ** Array o
89090 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20  f lists of free 
890a0 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67  blocks according
890b0 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69   to the block si
890c0 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61  ze .  ** for sma
890d0 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20  ller chunks, or 
890e0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c  a hash on the bl
890f0 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72  ock size for lar
89100 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e  ger.  ** chunks.
89110 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d  .  */.  u32 aiSm
89120 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b  all[MX_SMALL-1];
89130 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20     /* For sizes 
89140 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41  2 through MX_SMA
89150 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f  LL, inclusive */
89160 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f  .  u32 aiHash[N_
89170 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a  HASH];        /*
89180 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d   For sizes MX_SM
89190 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72  ALL+1 and larger
891a0 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39   */.} mem3 = { 9
891b0 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66  7535575 };..#def
891c0 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28  ine mem3 GLOBAL(
891d0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
891e0 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20  l, mem3)../*.** 
891f0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
89200 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   at mem3.aPool[i
89210 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
89220 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
89230 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68  n.  *pRoot is th
89240 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73  e list that i is
89250 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f   a member of..*/
89260 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
89270 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
89280 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
89290 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78  Root){.  u32 nex
892a0 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t = mem3.aPool[i
892b0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20  ].u.list.next;. 
892c0 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33   u32 prev = mem3
892d0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
892e0 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28  .prev;.  assert(
892f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89300 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
89310 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30  );.  if( prev==0
89320 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d   ){.    *pRoot =
89330 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a   next;.  }else{.
89340 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70      mem3.aPool[p
89350 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  rev].u.list.next
89360 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
89370 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d  f( next ){.    m
89380 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e  em3.aPool[next].
89390 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72  u.list.prev = pr
893a0 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61  ev;.  }.  mem3.a
893b0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
893c0 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e  ext = 0;.  mem3.
893d0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
893e0 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  prev = 0;.}../*.
893f0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68  ** Unlink the ch
89400 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66  unk at index i f
89410 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72  rom .** whatever
89420 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74   list is current
89430 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a  ly a member of..
89440 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
89450 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32  emsys3Unlink(u32
89460 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c   i){.  u32 size,
89470 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28   hash;.  assert(
89480 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89490 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
894a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
894b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
894c0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d  hdr.size4x & 1)=
894d0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
894e0 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d  i>=1 );.  size =
894f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
89500 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
89510 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d  .  assert( size=
89520 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69  =mem3.aPool[i+si
89530 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
89540 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
89550 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69  ( size>=2 );.  i
89560 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d  f( size <= MX_SM
89570 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79  ALL ){.    memsy
89580 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
89590 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c  (i, &mem3.aiSmal
895a0 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65  l[size-2]);.  }e
895b0 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20  lse{.    hash = 
895c0 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20  size % N_HASH;. 
895d0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
895e0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d  FromList(i, &mem
895f0 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
89600 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  .  }.}../*.** Li
89610 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
89620 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f  mem3.aPool[i] so
89630 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20   that is on the 
89640 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61  list rooted.** a
89650 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61  t *pRoot..*/.sta
89660 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
89670 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32  LinkIntoList(u32
89680 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b   i, u32 *pRoot){
89690 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
896a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
896b0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d  m3.mutex) );.  m
896c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
896d0 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f  ist.next = *pRoo
896e0 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  t;.  mem3.aPool[
896f0 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d  i].u.list.prev =
89700 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74   0;.  if( *pRoot
89710 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f   ){.    mem3.aPo
89720 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73  ol[*pRoot].u.lis
89730 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a  t.prev = i;.  }.
89740 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a    *pRoot = i;.}.
89750 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20  ./*.** Link the 
89760 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69  chunk at index i
89770 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65   into either the
89780 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
89790 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74  small chunk list
897a0 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61  , or into the la
897b0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
897c0 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
897d0 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  void memsys3Link
897e0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73  (u32 i){.  u32 s
897f0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73  ize, hash;.  ass
89800 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
89810 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
89820 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
89830 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72   i>=1 );.  asser
89840 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
89850 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89860 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69   & 1)==0 );.  si
89870 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
89880 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
89890 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73  x/4;.  assert( s
898a0 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ize==mem3.aPool[
898b0 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
898c0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73  prevSize );.  as
898d0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b  sert( size>=2 );
898e0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d  .  if( size <= M
898f0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d  X_SMALL ){.    m
89900 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
89910 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d  st(i, &mem3.aiSm
89920 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20  all[size-2]);.  
89930 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20  }else{.    hash 
89940 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b  = size % N_HASH;
89950 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
89960 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
89970 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
89980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
89990 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
899a0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72  mutex is not alr
899b0 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69  eady held, obtai
899c0 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75  n it now. The mu
899d0 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65  tex.** will alre
899e0 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74  ady be held (obt
899f0 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e  ained by code in
89a00 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a   malloc.c) if.**
89a10 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
89a20 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73  nfig.bMemStat is
89a30 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
89a40 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74   void memsys3Ent
89a50 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  er(void){.  if( 
89a60 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
89a70 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
89a80 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30  && mem3.mutex==0
89a90 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74   ){.    mem3.mut
89aa0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
89ab0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
89ac0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
89ad0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
89ae0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e  utex_enter(mem3.
89af0 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63  mutex);.}.static
89b00 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61   void memsys3Lea
89b10 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ve(void){.  sqli
89b20 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
89b30 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  mem3.mutex);.}..
89b40 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65  /*.** Called whe
89b50 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  n we are unable 
89b60 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c  to satisfy an al
89b70 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74  location of nByt
89b80 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
89b90 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d  id memsys3OutOfM
89ba0 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29  emory(int nByte)
89bb0 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c  {.  if( !mem3.al
89bc0 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d  armBusy ){.    m
89bd0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  em3.alarmBusy = 
89be0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  1;.    assert( s
89bf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
89c00 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
89c10 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
89c20 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75  ex_leave(mem3.mu
89c30 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
89c40 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
89c50 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c  (nByte);.    sql
89c60 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
89c70 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem3.mutex);.  
89c80 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79    mem3.alarmBusy
89c90 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
89ca0 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61  .** Chunk i is a
89cb0 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74   free chunk that
89cc0 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b   has been unlink
89cd0 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20  ed.  Adjust its 
89ce0 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74  .** size paramet
89cf0 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75  ers for check-ou
89d00 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
89d10 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a  ointer to the .*
89d20 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f  * user portion o
89d30 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  f the chunk..*/.
89d40 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
89d50 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32  sys3Checkout(u32
89d60 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b   i, u32 nBlock){
89d70 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65  .  u32 x;.  asse
89d80 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
89d90 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
89da0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
89db0 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  i>=1 );.  assert
89dc0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  ( mem3.aPool[i-1
89dd0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
89de0 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73  ==nBlock );.  as
89df0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c  sert( mem3.aPool
89e00 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
89e10 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c  dr.prevSize==nBl
89e20 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d  ock );.  x = mem
89e30 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
89e40 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d  dr.size4x;.  mem
89e50 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
89e60 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f  dr.size4x = nBlo
89e70 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29  ck*4 | 1 | (x&2)
89e80 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
89e90 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72  +nBlock-1].u.hdr
89ea0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f  .prevSize = nBlo
89eb0 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ck;.  mem3.aPool
89ec0 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
89ed0 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a  dr.size4x |= 2;.
89ee0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61    return &mem3.a
89ef0 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Pool[i];.}../*.*
89f00 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20  * Carve a piece 
89f10 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f  off of the end o
89f20 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74  f the mem3.iMast
89f30 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a  er free chunk..*
89f40 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
89f50 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c  er to the new al
89f60 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69  location.  Or, i
89f70 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
89f80 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72  nk.** is not lar
89f90 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72  ge enough, retur
89fa0 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  n 0..*/.static v
89fb0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d  oid *memsys3From
89fc0 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63  Master(u32 nBloc
89fd0 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  k){.  assert( sq
89fe0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
89ff0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
8a000 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73    assert( mem3.s
8a010 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
8a020 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e  );.  if( nBlock>
8a030 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  =mem3.szMaster-1
8a040 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74   ){.    /* Use t
8a050 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
8a060 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20   */.    void *p 
8a070 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75  = memsys3Checkou
8a080 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20  t(mem3.iMaster, 
8a090 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a  mem3.szMaster);.
8a0a0 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72      mem3.iMaster
8a0b0 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73   = 0;.    mem3.s
8a0c0 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  zMaster = 0;.   
8a0d0 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d   mem3.mnMaster =
8a0e0 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   0;.    return p
8a0f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
8a100 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74  * Split the mast
8a110 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72  er block.  Retur
8a120 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20  n the tail. */. 
8a130 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a     u32 newi, x;.
8a140 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e      newi = mem3.
8a150 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73  iMaster + mem3.s
8a160 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b  zMaster - nBlock
8a170 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65  ;.    assert( ne
8a180 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65  wi > mem3.iMaste
8a190 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e  r+1 );.    mem3.
8a1a0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8a1b0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
8a1c0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
8a1d0 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20  ze = nBlock;.   
8a1e0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8a1f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8a200 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8a210 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20  size4x |= 2;.   
8a220 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69   mem3.aPool[newi
8a230 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8a240 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b   = nBlock*4 + 1;
8a250 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  .    mem3.szMast
8a260 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20  er -= nBlock;.  
8a270 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77    mem3.aPool[new
8a280 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  i-1].u.hdr.prevS
8a290 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ize = mem3.szMas
8a2a0 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d  ter;.    x = mem
8a2b0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8a2c0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
8a2d0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65  ze4x & 2;.    me
8a2e0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8a2f0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8a300 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
8a310 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
8a320 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
8a330 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  er < mem3.mnMast
8a340 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33  er ){.      mem3
8a350 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .mnMaster = mem3
8a360 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d  .szMaster;.    }
8a370 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69  .    return (voi
8a380 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e  d*)&mem3.aPool[n
8a390 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ewi];.  }.}../*.
8a3a0 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65  ** *pRoot is the
8a3b0 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20   head of a list 
8a3c0 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f  of free chunks o
8a3d0 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a  f the same size.
8a3e0 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20  ** or same size 
8a3f0 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20  hash.  In other 
8a400 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73  words, *pRoot is
8a410 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74   an entry in eit
8a420 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d  her.** mem3.aiSm
8a430 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69  all[] or mem3.ai
8a440 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20  Hash[].  .**.** 
8a450 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61  This routine exa
8a460 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65  mines all entrie
8a470 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c  s on the given l
8a480 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a  ist and tries.**
8a490 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63   to coalesce eac
8a4a0 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61  h entries with a
8a4b0 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75  djacent free chu
8a4c0 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20  nks.  .**.** If 
8a4d0 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20  it sees a chunk 
8a4e0 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
8a4f0 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  han mem3.iMaster
8a500 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a  , it replaces .*
8a510 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65  * the current me
8a520 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20  m3.iMaster with 
8a530 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63  the new larger c
8a540 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20  hunk.  In order 
8a550 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33  for.** this mem3
8a560 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65  .iMaster replace
8a570 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68  ment to work, th
8a580 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d  e master chunk m
8a590 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64  ust be.** linked
8a5a0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
8a5b0 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20  ables.  That is 
8a5c0 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73  not the normal s
8a5d0 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69  tate of.** affai
8a5e0 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20  rs, of course.  
8a5f0 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  The calling rout
8a600 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68  ine must link th
8a610 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e  e master.** chun
8a620 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e  k before invokin
8a630 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  g this routine, 
8a640 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b  then must unlink
8a650 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a   the (possibly.*
8a660 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65  * changed) maste
8a670 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69  r chunk once thi
8a680 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69  s routine has fi
8a690 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  nished..*/.stati
8a6a0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65  c void memsys3Me
8a6b0 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b  rge(u32 *pRoot){
8a6c0 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72  .  u32 iNext, pr
8a6d0 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a  ev, size, i, x;.
8a6e0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
8a6f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
8a700 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  m3.mutex) );.  f
8a710 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30  or(i=*pRoot; i>0
8a720 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20  ; i=iNext){.    
8a730 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f  iNext = mem3.aPo
8a740 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
8a750 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65  t;.    size = me
8a760 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8a770 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20  hdr.size4x;.    
8a780 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29  assert( (size&1)
8a790 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  ==0 );.    if( (
8a7a0 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20  size&2)==0 ){.  
8a7b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8a7c0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f  kFromList(i, pRo
8a7d0 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ot);.      asser
8a7e0 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f  t( i > mem3.aPoo
8a7f0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  l[i-1].u.hdr.pre
8a800 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70  vSize );.      p
8a810 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61  rev = i - mem3.a
8a820 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8a830 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20  prevSize;.      
8a840 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20  if( prev==iNext 
8a850 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74  ){.        iNext
8a860 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72   = mem3.aPool[pr
8a870 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b  ev].u.list.next;
8a880 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
8a890 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65  emsys3Unlink(pre
8a8a0 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d  v);.      size =
8a8b0 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72   i + size/4 - pr
8a8c0 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65  ev;.      x = me
8a8d0 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d  m3.aPool[prev-1]
8a8e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20  .u.hdr.size4x & 
8a8f0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  2;.      mem3.aP
8a900 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64  ool[prev-1].u.hd
8a910 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a  r.size4x = size*
8a920 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
8a930 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a  3.aPool[prev+siz
8a940 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  e-1].u.hdr.prevS
8a950 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
8a960 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72    memsys3Link(pr
8a970 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70  ev);.      i = p
8a980 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rev;.    }else{.
8a990 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b        size /= 4;
8a9a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
8a9b0 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ize>mem3.szMaste
8a9c0 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  r ){.      mem3.
8a9d0 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20  iMaster = i;.   
8a9e0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
8a9f0 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20   = size;.    }. 
8aa00 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
8aa10 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  rn a block of me
8aa20 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74  mory of at least
8aa30 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e   nBytes in size.
8aa40 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  .** Return NULL 
8aa50 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  if unable..**.**
8aa60 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
8aa70 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
8aa80 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
8aa90 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
8aaa0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
8aab0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
8aac0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
8aad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8aae0 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys3MallocUnsaf
8aaf0 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
8ab00 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c  u32 i;.  u32 nBl
8ab10 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65  ock;.  u32 toFre
8ab20 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
8ab30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
8ab40 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
8ab50 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
8ab60 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29  (Mem3Block)==8 )
8ab70 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31  ;.  if( nByte<=1
8ab80 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  2 ){.    nBlock 
8ab90 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 2;.  }else{.  
8aba0 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74    nBlock = (nByt
8abb0 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20  e + 11)/8;.  }. 
8abc0 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e   assert( nBlock>
8abd0 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50  =2 );..  /* STEP
8abe0 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f   1:.  ** Look fo
8abf0 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  r an entry of th
8ac00 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69  e correct size i
8ac10 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61  n either the sma
8ac20 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61  ll.  ** chunk ta
8ac30 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61  ble or in the la
8ac40 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
8ac50 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20  able.  This is. 
8ac60 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d   ** successful m
8ac70 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20  ost of the time 
8ac80 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f  (about 9 times o
8ac90 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a  ut of 10)..  */.
8aca0 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20    if( nBlock <= 
8acb0 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20  MX_SMALL ){.    
8acc0 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  i = mem3.aiSmall
8acd0 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20  [nBlock-2];.    
8ace0 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20  if( i>0 ){.     
8acf0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
8ad00 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
8ad10 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32  aiSmall[nBlock-2
8ad20 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ]);.      return
8ad30 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74   memsys3Checkout
8ad40 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  (i, nBlock);.   
8ad50 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
8ad60 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63  int hash = nBloc
8ad70 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20  k % N_HASH;.    
8ad80 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73  for(i=mem3.aiHas
8ad90 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d  h[hash]; i>0; i=
8ada0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
8adb0 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20  list.next){.    
8adc0 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c    if( mem3.aPool
8add0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
8ade0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a  4x/4==nBlock ){.
8adf0 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55          memsys3U
8ae00 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
8ae10 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61   &mem3.aiHash[ha
8ae20 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65  sh]);.        re
8ae30 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
8ae40 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
8ae50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
8ae60 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a   }..  /* STEP 2:
8ae70 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74  .  ** Try to sat
8ae80 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74  isfy the allocat
8ae90 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61  ion by carving a
8aea0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68   piece off of th
8aeb0 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68  e end.  ** of th
8aec0 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20  e master chunk. 
8aed0 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c   This step usual
8aee0 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70  ly works if step
8aef0 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20   1 fails..  */. 
8af00 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
8af10 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20  er>=nBlock ){.  
8af20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
8af30 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63  FromMaster(nBloc
8af40 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53  k);.  }...  /* S
8af50 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f  TEP 3:  .  ** Lo
8af60 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
8af70 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f  ntire memory poo
8af80 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a  l.  Coalesce adj
8af90 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20  acent free.  ** 
8afa0 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75  chunks.  Recompu
8afb0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68  te the master ch
8afc0 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65  unk as the large
8afd0 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20  st free chunk.. 
8afe0 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61   ** Then try aga
8aff0 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  in to satisfy th
8b000 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20  e allocation by 
8b010 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20  carving a piece 
8b020 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  off.  ** of the 
8b030 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65  end of the maste
8b040 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73  r chunk.  This s
8b050 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79  tep happens very
8b060 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65  .  ** rarely (we
8b070 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66   hope!).  */.  f
8b080 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b  or(toFree=nBlock
8b090 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d  *16; toFree<(mem
8b0a0 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46  3.nPool*16); toF
8b0b0 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d  ree *= 2){.    m
8b0c0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72  emsys3OutOfMemor
8b0d0 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69  y(toFree);.    i
8b0e0 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  f( mem3.iMaster 
8b0f0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
8b100 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65  Link(mem3.iMaste
8b110 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69  r);.      mem3.i
8b120 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
8b130 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
8b140 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = 0;.    }.    f
8b150 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48  or(i=0; i<N_HASH
8b160 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65  ; i++){.      me
8b170 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33  msys3Merge(&mem3
8b180 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20  .aiHash[i]);.   
8b190 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
8b1a0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b  i<MX_SMALL-1; i+
8b1b0 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  +){.      memsys
8b1c0 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53  3Merge(&mem3.aiS
8b1d0 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  mall[i]);.    }.
8b1e0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d      if( mem3.szM
8b1f0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
8b200 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d  emsys3Unlink(mem
8b210 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  3.iMaster);.    
8b220 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73    if( mem3.szMas
8b230 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20  ter>=nBlock ){. 
8b240 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65         return me
8b250 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
8b260 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  nBlock);.      }
8b270 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
8b280 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
8b290 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68  above worked, th
8b2a0 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20  en we fail. */. 
8b2b0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
8b2c0 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73  .** Free an outs
8b2d0 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61  tanding memory a
8b2e0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
8b2f0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
8b300 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
8b310 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
8b320 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
8b330 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
8b340 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
8b350 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
8b360 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65  .void memsys3Fre
8b370 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f  eUnsafe(void *pO
8b380 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b  ld){.  Mem3Block
8b390 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b   *p = (Mem3Block
8b3a0 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b  *)pOld;.  int i;
8b3b0 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a  .  u32 size, x;.
8b3c0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8b3d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8b3e0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
8b3f0 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f  sert( p>mem3.aPo
8b400 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50  ol && p<&mem3.aP
8b410 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20  ool[mem3.nPool] 
8b420 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d  );.  i = p - mem
8b430 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72  3.aPool;.  asser
8b440 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
8b450 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8b460 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65  &1)==1 );.  size
8b470 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
8b480 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
8b490 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73  4;.  assert( i+s
8b4a0 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b  ize<=mem3.nPool+
8b4b0 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  1 );.  mem3.aPoo
8b4c0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8b4d0 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d  e4x &= ~1;.  mem
8b4e0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
8b4f0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
8b500 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e   = size;.  mem3.
8b510 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
8b520 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20  u.hdr.size4x &= 
8b530 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e  ~2;.  memsys3Lin
8b540 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20  k(i);..  /* Try 
8b550 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61  to expand the ma
8b560 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e  ster using the n
8b570 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b  ewly freed chunk
8b580 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69   */.  if( mem3.i
8b590 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68  Master ){.    wh
8b5a0 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ile( (mem3.aPool
8b5b0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
8b5c0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29  .u.hdr.size4x&2)
8b5d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a  ==0 ){.      siz
8b5e0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  e = mem3.aPool[m
8b5f0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8b600 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20  .hdr.prevSize;. 
8b610 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65       mem3.iMaste
8b620 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20  r -= size;.     
8b630 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b   mem3.szMaster +
8b640 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  = size;.      me
8b650 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33  msys3Unlink(mem3
8b660 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  .iMaster);.     
8b670 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
8b680 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
8b690 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32  u.hdr.size4x & 2
8b6a0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
8b6b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
8b6c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8b6d0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a  = mem3.szMaster*
8b6e0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
8b6f0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8b700 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
8b710 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
8b720 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61  Size = mem3.szMa
8b730 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ster;.    }.    
8b740 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
8b750 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8b760 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
8b770 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
8b780 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8b790 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
8b7a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8b7b0 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20  4x&1)==0 ){.    
8b7c0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28    memsys3Unlink(
8b7d0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8b7e0 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  3.szMaster);.   
8b7f0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
8b800 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d   += mem3.aPool[m
8b810 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8b820 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8b830 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20  dr.size4x/4;.   
8b840 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
8b850 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
8b860 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d  hdr.size4x = mem
8b870 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78  3.szMaster*4 | x
8b880 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
8b890 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
8b8a0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
8b8b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
8b8c0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
8b8d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
8b8e0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
8b8f0 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61  ize of an outsta
8b900 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e  nding allocation
8b910 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  , in bytes.  The
8b920 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65  .** size returne
8b930 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79  d omits the 8-by
8b940 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65  te header overhe
8b950 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a  ad.  This only.*
8b960 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e  * works for chun
8b970 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72  ks that are curr
8b980 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75  ently checked ou
8b990 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
8b9a0 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69   memsys3Size(voi
8b9b0 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f  d *p){.  Mem3Blo
8b9c0 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66  ck *pBlock;.  if
8b9d0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
8b9e0 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d  0;.  pBlock = (M
8b9f0 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61  em3Block*)p;.  a
8ba00 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d  ssert( (pBlock[-
8ba10 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
8ba20 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  1)!=0 );.  retur
8ba30 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e  n (pBlock[-1].u.
8ba40 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32  hdr.size4x&~3)*2
8ba50 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   - 4;.}../*.** R
8ba60 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73  ound up a reques
8ba70 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65  t size to the ne
8ba80 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74  xt valid allocat
8ba90 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61  ion size..*/.sta
8baa0 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52  tic int memsys3R
8bab0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
8bac0 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20   if( n<=12 ){.  
8bad0 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d    return 12;.  }
8bae0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
8baf0 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34   ((n+11)&~7) - 4
8bb00 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
8bb10 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f  llocate nBytes o
8bb20 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  f memory..*/.sta
8bb30 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
8bb40 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  3Malloc(int nByt
8bb50 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  es){.  sqlite3_i
8bb60 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72  nt64 *p;.  asser
8bb70 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20  t( nBytes>0 );  
8bb80 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f          /* mallo
8bb90 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20  c.c filters out 
8bba0 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20  0 byte requests 
8bbb0 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65  */.  memsys3Ente
8bbc0 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79  r();.  p = memsy
8bbd0 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e  s3MallocUnsafe(n
8bbe0 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73  Bytes);.  memsys
8bbf0 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75  3Leave();.  retu
8bc00 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a  rn (void*)p; .}.
8bc10 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
8bc20 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  ry..*/.void mems
8bc30 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50  ys3Free(void *pP
8bc40 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28  rior){.  assert(
8bc50 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d   pPrior );.  mem
8bc60 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys3Enter();.  m
8bc70 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65  emsys3FreeUnsafe
8bc80 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
8bc90 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f  ys3Leave();.}../
8bca0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
8bcb0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
8bcc0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
8bcd0 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d  ation.*/.void *m
8bce0 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f  emsys3Realloc(vo
8bcf0 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20  id *pPrior, int 
8bd00 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e  nBytes){.  int n
8bd10 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  Old;.  void *p;.
8bd20 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20    if( pPrior==0 
8bd30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
8bd40 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
8bd50 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  tes);.  }.  if( 
8bd60 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20  nBytes<=0 ){.   
8bd70 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
8bd80 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72  rior);.    retur
8bd90 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20  n 0;.  }.  nOld 
8bda0 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50  = memsys3Size(pP
8bdb0 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  rior);.  if( nBy
8bdc0 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79  tes<=nOld && nBy
8bdd0 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b  tes>=nOld-128 ){
8bde0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69  .    return pPri
8bdf0 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  or;.  }.  memsys
8be00 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20  3Enter();.  p = 
8be10 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73  memsys3MallocUns
8be20 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69  afe(nBytes);.  i
8be30 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  f( p ){.    if( 
8be40 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20  nOld<nBytes ){. 
8be50 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
8be60 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
8be70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
8be80 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c  emcpy(p, pPrior,
8be90 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a   nBytes);.    }.
8bea0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55      memsys3FreeU
8beb0 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20  nsafe(pPrior);. 
8bec0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76   }.  memsys3Leav
8bed0 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  e();.  return p;
8bee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
8bef0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
8bf00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8bf10 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64  memsys3Init(void
8bf20 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
8bf30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
8bf40 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21  otUsed);.  if( !
8bf50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8bf60 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20  fig.pHeap ){.   
8bf70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
8bf80 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
8bf90 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20  Store a pointer 
8bfa0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c  to the memory bl
8bfb0 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74  ock in global st
8bfc0 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f  ructure mem3. */
8bfd0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
8bfe0 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20  f(Mem3Block)==8 
8bff0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20  );.  mem3.aPool 
8c000 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73  = (Mem3Block *)s
8c010 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
8c020 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33  ig.pHeap;.  mem3
8c030 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65  .nPool = (sqlite
8c040 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
8c050 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d  eap / sizeof(Mem
8c060 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20  3Block)) - 2;.. 
8c070 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
8c080 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e  he master block.
8c090 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73   */.  mem3.szMas
8c0a0 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c  ter = mem3.nPool
8c0b0 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65  ;.  mem3.mnMaste
8c0c0 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  r = mem3.szMaste
8c0d0 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65  r;.  mem3.iMaste
8c0e0 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50  r = 1;.  mem3.aP
8c0f0 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a  ool[0].u.hdr.siz
8c100 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61  e4x = (mem3.szMa
8c110 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20  ster<<2) + 2;.  
8c120 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8c130 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65  nPool].u.hdr.pre
8c140 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f  vSize = mem3.nPo
8c150 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ol;.  mem3.aPool
8c160 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
8c170 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a  dr.size4x = 1;..
8c180 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
8c190 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
8c1a0 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d  nitialize this m
8c1b0 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  odule..*/.static
8c1c0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75   void memsys3Shu
8c1d0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
8c1e0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
8c1f0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
8c200 29 3b 0a 20 20 6d 65 6d 33 2e 6d 75 74 65 78 20  );.  mem3.mutex 
8c210 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  = 0;.  return;.}
8c220 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  ..../*.** Open t
8c230 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65  he file indicate
8c240 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f  d and write a lo
8c250 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64  g of all unfreed
8c260 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f   memory .** allo
8c270 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61  cations into tha
8c280 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t log..*/.SQLITE
8c290 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
8c2a0 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70  lite3Memsys3Dump
8c2b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
8c2c0 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20  lename){.#ifdef 
8c2d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46  SQLITE_DEBUG.  F
8c2e0 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20  ILE *out;.  u32 
8c2f0 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65  i, j;.  u32 size
8c300 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  ;.  if( zFilenam
8c310 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d  e==0 || zFilenam
8c320 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f  e[0]==0 ){.    o
8c330 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d  ut = stdout;.  }
8c340 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20  else{.    out = 
8c350 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  fopen(zFilename,
8c360 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f   "w");.    if( o
8c370 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  ut==0 ){.      f
8c380 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
8c390 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74  ** Unable to out
8c3a0 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67  put memory debug
8c3b0 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20   output log: %s 
8c3c0 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  **\n",.         
8c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69               zFi
8c3e0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  lename);.      r
8c3f0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
8c400 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28  .  memsys3Enter(
8c410 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
8c420 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a  , "CHUNKS:\n");.
8c430 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65    for(i=1; i<=me
8c440 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a  m3.nPool; i+=siz
8c450 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d  e/4){.    size =
8c460 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
8c470 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20  .u.hdr.size4x;. 
8c480 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31     if( size/4<=1
8c490 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
8c4a0 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20  f(out, "%p size 
8c4b0 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e  error\n", &mem3.
8c4c0 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20  aPool[i]);.     
8c4d0 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20   assert( 0 );.  
8c4e0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
8c4f0 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 26 31  .    if( (size&1
8c500 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f  )==0 && mem3.aPo
8c510 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75  ol[i+size/4-1].u
8c520 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73  .hdr.prevSize!=s
8c530 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66  ize/4 ){.      f
8c540 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20  printf(out, "%p 
8c550 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e  tail size does n
8c560 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65  ot match\n", &me
8c570 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20  m3.aPool[i]);.  
8c580 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
8c590 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
8c5a0 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65    }.    if( ((me
8c5b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f  m3.aPool[i+size/
8c5c0 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  4-1].u.hdr.size4
8c5d0 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26  x&2)>>1)!=(size&
8c5e0 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69  1) ){.      fpri
8c5f0 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69  ntf(out, "%p tai
8c600 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69  l checkout bit i
8c610 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20  s incorrect\n", 
8c620 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b  &mem3.aPool[i]);
8c630 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
8c640 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
8c650 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
8c660 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66  ize&1 ){.      f
8c670 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20  printf(out, "%p 
8c680 25 36 64 20 62 79 74 65 73 20 63 68 65 63 6b 65  %6d bytes checke
8c690 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e  d out\n", &mem3.
8c6a0 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f  aPool[i], (size/
8c6b0 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c  4)*8-8);.    }el
8c6c0 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  se{.      fprint
8c6d0 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62  f(out, "%p %6d b
8c6e0 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20  ytes free%s\n", 
8c6f0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20  &mem3.aPool[i], 
8c700 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20  (size/4)*8-8,.  
8c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c720 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  i==mem3.iMaster 
8c730 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20  ? " **master**" 
8c740 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  : "");.    }.  }
8c750 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58  .  for(i=0; i<MX
8c760 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a  _SMALL-1; i++){.
8c770 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53      if( mem3.aiS
8c780 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e  mall[i]==0 ) con
8c790 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e  tinue;.    fprin
8c7a0 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25  tf(out, "small(%
8c7b0 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66  2d):", i);.    f
8c7c0 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d  or(j = mem3.aiSm
8c7d0 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d  all[i]; j>0; j=m
8c7e0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c  em3.aPool[j].u.l
8c7f0 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20  ist.next){.     
8c800 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
8c810 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61  %p(%d)", &mem3.a
8c820 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20  Pool[j],.       
8c830 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f         (mem3.aPo
8c840 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[j-1].u.hdr.si
8c850 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20  ze4x/4)*8-8);.  
8c860 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28    }.    fprintf(
8c870 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d  out, "\n"); .  }
8c880 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f  .  for(i=0; i<N_
8c890 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  HASH; i++){.    
8c8a0 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  if( mem3.aiHash[
8c8b0 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  i]==0 ) continue
8c8c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
8c8d0 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c  t, "hash(%2d):",
8c8e0 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d   i);.    for(j =
8c8f0 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b   mem3.aiHash[i];
8c900 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f   j>0; j=mem3.aPo
8c910 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[j].u.list.nex
8c920 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  t){.      fprint
8c930 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22  f(out, " %p(%d)"
8c940 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d  , &mem3.aPool[j]
8c950 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
8c960 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d  (mem3.aPool[j-1]
8c970 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29  .u.hdr.size4x/4)
8c980 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20  *8-8);.    }.   
8c990 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
8c9a0 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69  n"); .  }.  fpri
8c9b0 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72  ntf(out, "master
8c9c0 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61  =%d\n", mem3.iMa
8c9d0 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66  ster);.  fprintf
8c9e0 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25  (out, "nowUsed=%
8c9f0 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c  d\n", mem3.nPool
8ca00 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  *8 - mem3.szMast
8ca10 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66  er*8);.  fprintf
8ca20 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64  (out, "mxUsed=%d
8ca30 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a  \n", mem3.nPool*
8ca40 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65  8 - mem3.mnMaste
8ca50 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  r*8);.  sqlite3_
8ca60 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33  mutex_leave(mem3
8ca70 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f  .mutex);.  if( o
8ca80 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20  ut==stdout ){.  
8ca90 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29    fflush(stdout)
8caa0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
8cab0 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a  close(out);.  }.
8cac0 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
8cad0 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61  ARAMETER(zFilena
8cae0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  me);.#endif.}../
8caf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
8cb00 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f  e is the only ro
8cb10 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69  utine in this fi
8cb20 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c  le with external
8cb30 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a   .** linkage..**
8cb40 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65  .** Populate the
8cb50 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
8cb60 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
8cb70 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69  ction pointers i
8cb80 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  n.** sqlite3Glob
8cb90 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20  alConfig.m with 
8cba0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
8cbb0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
8cbc0 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72   file. The.** ar
8cbd0 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20  guments specify 
8cbe0 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  the block of mem
8cbf0 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a  ory to manage..*
8cc00 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
8cc10 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  e is only called
8cc20 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66   by sqlite3_conf
8cc30 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66  ig(), and theref
8cc40 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65  ore.** is not re
8cc50 71 75 69 72 65 64 20 74 6f 20 62 65 20 74 68 72  quired to be thr
8cc60 65 61 64 73 61 66 65 20 28 69 74 20 69 73 20 6e  eadsafe (it is n
8cc70 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ot)..*/.SQLITE_P
8cc80 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
8cc90 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
8cca0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d   *sqlite3MemGetM
8ccb0 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20  emsys3(void){.  
8ccc0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
8ccd0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
8cce0 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20   mempoolMethods 
8ccf0 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  = {.     memsys3
8cd00 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d  Malloc,.     mem
8cd10 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d  sys3Free,.     m
8cd20 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20  emsys3Realloc,. 
8cd30 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c      memsys3Size,
8cd40 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75  .     memsys3Rou
8cd50 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79  ndup,.     memsy
8cd60 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d  s3Init,.     mem
8cd70 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20  sys3Shutdown,.  
8cd80 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75     0.  };.  retu
8cd90 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f  rn &mempoolMetho
8cda0 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  ds;.}..#endif /*
8cdb0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
8cdc0 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EMSYS3 */../****
8cdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
8cde0 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f mem3.c *******
8cdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ce00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ce10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
8ce20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
8ce30 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a   file mem5.c ***
8ce40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ce50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ce60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
8ce70 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34   2007 October 14
8ce80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
8ce90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
8cea0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
8ceb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
8cec0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
8ced0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
8cee0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
8cef0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
8cf00 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
8cf10 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
8cf20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
8cf30 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
8cf40 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
8cf50 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
8cf60 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
8cf70 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
8cf80 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
8cf90 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
8cfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
8cfe0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
8cff0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
8d000 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
8d010 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a  ment a memory.**
8d020 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
8d030 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79  ystem for use by
8d040 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20   SQLite. .**.** 
8d050 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
8d060 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
8d070 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
8d080 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65  omits all.** use
8d090 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68   of malloc(). Th
8d0a0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 67 69  e application gi
8d0b0 76 65 73 20 53 51 4c 69 74 65 20 61 20 62 6c 6f  ves SQLite a blo
8d0c0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ck of memory.** 
8d0d0 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  before calling s
8d0e0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
8d0f0 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61  e() from which a
8d100 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  llocations.** ar
8d110 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72  e made and retur
8d120 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c  ned by the xMall
8d130 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f  oc() and xReallo
8d140 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  c() .** implemen
8d150 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71  tations. Once sq
8d160 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
8d170 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  () has been call
8d180 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e  ed,.** the amoun
8d190 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  t of memory avai
8d1a0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20  lable to SQLite 
8d1b0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e  is fixed and can
8d1c0 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65  not.** be change
8d1d0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65  d..**.** This ve
8d1e0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d  rsion of the mem
8d1f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
8d200 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c  ubsystem is incl
8d210 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62  uded.** in the b
8d220 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c  uild only if SQL
8d230 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
8d240 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  S5 is defined..*
8d250 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 6d 6f 72 79  *.** This memory
8d260 20 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65 73 20   allocator uses 
8d270 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c  the following al
8d280 67 6f 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20  gorithm:.**.**  
8d290 20 31 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 79 20   1.  All memory 
8d2a0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 73 69 7a 65  allocations size
8d2b0 73 20 61 72 65 20 72 6f 75 6e 64 65 64 20 75 70  s are rounded up
8d2c0 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32   to a power of 2
8d2d0 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 49 66  ..**.**   2.  If
8d2e0 20 74 77 6f 20 61 64 6a 61 63 65 6e 74 20 66 72   two adjacent fr
8d2f0 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20 74 68  ee blocks are th
8d300 65 20 68 61 6c 76 65 73 20 6f 66 20 61 20 6c 61  e halves of a la
8d310 72 67 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20  rger block,.**  
8d320 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 74 77       then the tw
8d330 6f 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61  o blocks are coa
8d340 6c 65 73 65 64 20 69 6e 74 6f 20 74 68 65 20 73  lesed into the s
8d350 69 6e 67 6c 65 20 6c 61 72 67 65 72 20 62 6c 6f  ingle larger blo
8d360 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20  ck..**.**   3.  
8d370 4e 65 77 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c  New memory is al
8d380 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65  located from the
8d390 20 66 69 72 73 74 20 61 76 61 69 6c 61 62 6c 65   first available
8d3a0 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a   free block..**.
8d3b0 2a 2a 20 54 68 69 73 20 61 6c 67 6f 72 69 74 68  ** This algorith
8d3c0 6d 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69  m is described i
8d3d0 6e 3a 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e  n: J. M. Robson.
8d3e0 20 22 42 6f 75 6e 64 73 20 66 6f 72 20 53 6f 6d   "Bounds for Som
8d3f0 65 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43  e Functions.** C
8d400 6f 6e 63 65 72 6e 69 6e 67 20 44 79 6e 61 6d 69  oncerning Dynami
8d410 63 20 53 74 6f 72 61 67 65 20 41 6c 6c 6f 63 61  c Storage Alloca
8d420 74 69 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f  tion". Journal o
8d430 66 20 74 68 65 20 41 73 73 6f 63 69 61 74 69 6f  f the Associatio
8d440 6e 20 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75 74 69  n for.** Computi
8d450 6e 67 20 4d 61 63 68 69 6e 65 72 79 2c 20 56 6f  ng Machinery, Vo
8d460 6c 75 6d 65 20 32 31 2c 20 4e 75 6d 62 65 72 20  lume 21, Number 
8d470 38 2c 20 4a 75 6c 79 20 31 39 37 34 2c 20 70 61  8, July 1974, pa
8d480 67 65 73 20 34 39 31 2d 34 39 39 2e 0a 2a 2a 20  ges 491-499..** 
8d490 0a 2a 2a 20 4c 65 74 20 6e 20 62 65 20 74 68 65  .** Let n be the
8d4a0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
8d4b0 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  gest allocation 
8d4c0 64 69 76 69 64 65 64 20 62 79 20 74 68 65 20 6d  divided by the m
8d4d0 69 6e 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61  inimum.** alloca
8d4e0 74 69 6f 6e 20 73 69 7a 65 20 28 61 66 74 65 72  tion size (after
8d4f0 20 72 6f 75 6e 64 69 6e 67 20 61 6c 6c 20 73 69   rounding all si
8d500 7a 65 73 20 75 70 20 74 6f 20 61 20 70 6f 77 65  zes up to a powe
8d510 72 20 6f 66 20 32 2e 29 20 20 4c 65 74 20 4d 0a  r of 2.)  Let M.
8d520 2a 2a 20 62 65 20 74 68 65 20 6d 61 78 69 6d 75  ** be the maximu
8d530 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  m amount of memo
8d540 72 79 20 65 76 65 72 20 6f 75 74 73 74 61 6e 64  ry ever outstand
8d550 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e  ing at one time.
8d560 20 20 4c 65 74 0a 2a 2a 20 4e 20 62 65 20 74 68    Let.** N be th
8d570 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f  e total amount o
8d580 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62  f memory availab
8d590 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f  le for allocatio
8d5a0 6e 2e 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72  n.  Robson.** pr
8d5b0 6f 76 65 64 20 74 68 61 74 20 74 68 69 73 20 6d  oved that this m
8d5c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
8d5d0 77 69 6c 6c 20 6e 65 76 65 72 20 62 72 65 61 6b  will never break
8d5e0 64 6f 77 6e 20 64 75 65 20 74 6f 20 0a 2a 2a 20  down due to .** 
8d5f0 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 73  fragmentation as
8d600 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 66 6f 6c   long as the fol
8d610 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e  lowing constrain
8d620 74 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20  t holds:.**.**  
8d630 20 20 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b      N >=  M*(1 +
8d640 20 6c 6f 67 32 28 6e 29 2f 32 29 20 2d 20 6e 20   log2(n)/2) - n 
8d650 2b 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  + 1.**.** The sq
8d660 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 6c  lite3_status() l
8d670 6f 67 69 63 20 74 72 61 63 6b 73 20 74 68 65 20  ogic tracks the 
8d680 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 73 20 6f  maximum values o
8d690 66 20 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20  f n and M so.** 
8d6a0 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74  that an applicat
8d6b0 69 6f 6e 20 63 61 6e 2c 20 61 74 20 61 6e 79 20  ion can, at any 
8d6c0 74 69 6d 65 2c 20 76 65 72 69 66 79 20 74 68 69  time, verify thi
8d6d0 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  s constraint..*/
8d6e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
8d6f0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
8d700 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
8d710 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a  used only when .
8d720 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
8d730 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69  _MEMSYS5 is defi
8d740 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ned..*/.#ifdef S
8d750 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8d760 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69  SYS5../*.** A mi
8d770 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e  nimum allocation
8d780 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
8d790 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
8d7a0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c   structure..** L
8d7b0 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  arger allocation
8d7c0 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f  s are an array o
8d7d0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
8d7e0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20  es where the.** 
8d7f0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61  size of the arra
8d800 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20  y is a power of 
8d810 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a  2..**.** The siz
8d820 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
8d830 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
8d840 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 66   of two.  That f
8d850 61 63 74 20 69 73 0a 2a 2a 20 76 65 72 69 66 69  act is.** verifi
8d860 65 64 20 69 6e 20 6d 65 6d 73 79 73 35 49 6e 69  ed in memsys5Ini
8d870 74 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  t()..*/.typedef 
8d880 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20  struct Mem5Link 
8d890 4d 65 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74  Mem5Link;.struct
8d8a0 20 4d 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e   Mem5Link {.  in
8d8b0 74 20 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a  t next;       /*
8d8c0 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66   Index of next f
8d8d0 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69  ree chunk */.  i
8d8e0 6e 74 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  nt prev;       /
8d8f0 2a 20 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69  * Index of previ
8d900 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ous free chunk *
8d910 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  /.};../*.** Maxi
8d920 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20  mum size of any 
8d930 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28  allocation is ((
8d940 31 3c 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e  1<<LOGMAX)*mem5.
8d950 73 7a 41 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a  szAtom). Since.*
8d960 2a 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 69 73  * mem5.szAtom is
8d970 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74   always at least
8d980 20 38 20 61 6e 64 20 33 32 2d 62 69 74 20 69 6e   8 and 32-bit in
8d990 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 2c  tegers are used,
8d9a0 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 61 63  .** it is not ac
8d9b0 74 75 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20  tually possible 
8d9c0 74 6f 20 72 65 61 63 68 20 74 68 69 73 20 6c 69  to reach this li
8d9d0 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  mit..*/.#define 
8d9e0 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a  LOGMAX 30../*.**
8d9f0 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20   Masks used for 
8da00 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65  mem5.aCtrl[] ele
8da10 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ments..*/.#defin
8da20 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20  e CTRL_LOGSIZE  
8da30 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20  0x1f    /* Log2 
8da40 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f  Size of this blo
8da50 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54  ck */.#define CT
8da60 52 4c 5f 46 52 45 45 20 20 20 20 20 30 78 32 30  RL_FREE     0x20
8da70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e      /* True if n
8da80 6f 74 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a  ot checked out *
8da90 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  /../*.** All of 
8daa0 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61  the static varia
8dab0 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69  bles used by thi
8dac0 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c  s module are col
8dad0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61  lected.** into a
8dae0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72   single structur
8daf0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20  e named "mem5". 
8db00 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70   This is to keep
8db10 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76   the.** static v
8db20 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a  ariables organiz
8db30 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65  ed and to reduce
8db40 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75   namespace pollu
8db50 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  tion.** when thi
8db60 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62  s module is comb
8db70 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20  ined with other 
8db80 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  in the amalgamat
8db90 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  ion..*/.static S
8dba0 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
8dbb0 20 4d 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20   Mem5Global {.  
8dbc0 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61  /*.  ** Memory a
8dbd0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
8dbe0 6f 63 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69  ocation.  */.  i
8dbf0 6e 74 20 73 7a 41 74 6f 6d 3b 20 20 20 20 20 20  nt szAtom;      
8dc00 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73  /* Smallest poss
8dc10 69 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ible allocation 
8dc20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e  in bytes */.  in
8dc30 74 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f  t nBlock;      /
8dc40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 7a 41 74  * Number of szAt
8dc50 6f 6d 20 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20  om sized blocks 
8dc60 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38  in zPool */.  u8
8dc70 20 2a 7a 50 6f 6f 6c 3b 20 20 20 20 20 20 20 2f   *zPool;       /
8dc80 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62  * Memory availab
8dc90 6c 65 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74  le to be allocat
8dca0 65 64 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20  ed */.  .  /*.  
8dcb0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
8dcc0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
8dcd0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
8dce0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
8dcf0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
8dd00 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f  tex *mutex;..  /
8dd10 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e  *.  ** Performan
8dd20 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20  ce statistics.  
8dd30 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b  */.  u64 nAlloc;
8dd40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
8dd50 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c  l number of call
8dd60 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20  s to malloc */. 
8dd70 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b   u64 totalAlloc;
8dd80 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66       /* Total of
8dd90 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c   all malloc call
8dda0 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74  s - includes int
8ddb0 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20  ernal frag */.  
8ddc0 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b  u64 totalExcess;
8ddd0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74      /* Total int
8dde0 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74  ernal fragmentat
8ddf0 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72  ion */.  u32 cur
8de00 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20  rentOut;     /* 
8de10 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74  Current checkout
8de20 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65  , including inte
8de30 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69  rnal fragmentati
8de40 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72  on */.  u32 curr
8de50 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43  entCount;   /* C
8de60 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
8de70 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f   distinct checko
8de80 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78  uts */.  u32 max
8de90 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Out;         /* 
8dea0 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61  Maximum instanta
8deb0 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74  neous currentOut
8dec0 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75   */.  u32 maxCou
8ded0 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  nt;       /* Max
8dee0 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f  imum instantaneo
8def0 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20  us currentCount 
8df00 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75  */.  u32 maxRequ
8df10 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67  est;     /* Larg
8df20 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28  est allocation (
8df30 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74  exclusive of int
8df40 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20  ernal frag) */. 
8df50 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74   .  /*.  ** List
8df60 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  s of free blocks
8df70 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b 30 5d  .  aiFreelist[0]
8df80 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 66 72   is a list of fr
8df90 65 65 20 62 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a  ee blocks of.  *
8dfa0 2a 20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a 41 74  * size mem5.szAt
8dfb0 6f 6d 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b  om.  aiFreelist[
8dfc0 31 5d 20 68 6f 6c 64 73 20 62 6c 6f 63 6b 73 20  1] holds blocks 
8dfd0 6f 66 20 73 69 7a 65 20 73 7a 41 74 6f 6d 2a 32  of size szAtom*2
8dfe0 2e 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 6f  ..  ** and so fo
8dff0 72 74 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  rth..  */.  int 
8e000 61 69 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41  aiFreelist[LOGMA
8e010 58 2b 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  X+1];..  /*.  **
8e020 20 53 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b   Space for track
8e030 69 6e 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73  ing which blocks
8e040 20 61 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74   are checked out
8e050 20 61 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20   and the size.  
8e060 2a 2a 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b  ** of each block
8e070 2e 20 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20  .  One byte per 
8e080 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38  block..  */.  u8
8e090 20 2a 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35   *aCtrl;..} mem5
8e0a0 20 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a   = { 0 };../*.**
8e0b0 20 41 63 63 65 73 73 20 74 68 65 20 73 74 61 74   Access the stat
8e0c0 69 63 20 76 61 72 69 61 62 6c 65 20 74 68 72 6f  ic variable thro
8e0d0 75 67 68 20 61 20 6d 61 63 72 6f 20 66 6f 72 20  ugh a macro for 
8e0e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
8e0f0 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65 6d 35 20  */.#define mem5 
8e100 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65  GLOBAL(struct Me
8e110 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a  m5Global, mem5).
8e120 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  ./*.** Assuming 
8e130 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20 64 69  mem5.zPool is di
8e140 76 69 64 65 64 20 75 70 20 69 6e 74 6f 20 61 6e  vided up into an
8e150 20 61 72 72 61 79 20 6f 66 20 4d 65 6d 35 4c 69   array of Mem5Li
8e160 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  nk.** structures
8e170 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
8e180 65 72 20 74 6f 20 74 68 65 20 69 64 78 2d 74 68  er to the idx-th
8e190 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64   such lik..*/.#d
8e1a0 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69  efine MEM5LINK(i
8e1b0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a  dx) ((Mem5Link *
8e1c0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69  )(&mem5.zPool[(i
8e1d0 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d  dx)*mem5.szAtom]
8e1e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  ))../*.** Unlink
8e1f0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65   the chunk at me
8e200 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d  m5.aPool[i] from
8e210 20 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72   list it is curr
8e220 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74  ently.** on.  It
8e230 20 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64   should be found
8e240 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c   on mem5.aiFreel
8e250 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a  ist[iLogsize]..*
8e260 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8e270 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20  msys5Unlink(int 
8e280 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29  i, int iLogsize)
8e290 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72  {.  int next, pr
8e2a0 65 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  ev;.  assert( i>
8e2b0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c  =0 && i<mem5.nBl
8e2c0 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ock );.  assert(
8e2d0 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20   iLogsize>=0 && 
8e2e0 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58  iLogsize<=LOGMAX
8e2f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d   );.  assert( (m
8e300 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43  em5.aCtrl[i] & C
8e310 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c  TRL_LOGSIZE)==iL
8e320 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78  ogsize );..  nex
8e330 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d  t = MEM5LINK(i)-
8e340 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20  >next;.  prev = 
8e350 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65  MEM5LINK(i)->pre
8e360 76 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20  v;.  if( prev<0 
8e370 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72  ){.    mem5.aiFr
8e380 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d  eelist[iLogsize]
8e390 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65   = next;.  }else
8e3a0 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70  {.    MEM5LINK(p
8e3b0 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78  rev)->next = nex
8e3c0 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78  t;.  }.  if( nex
8e3d0 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35  t>=0 ){.    MEM5
8e3e0 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76  LINK(next)->prev
8e3f0 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a   = prev;.  }.}..
8e400 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63  /*.** Link the c
8e410 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f  hunk at mem5.aPo
8e420 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73  ol[i] so that is
8e430 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65   on the iLogsize
8e440 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a  .** free list..*
8e450 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8e460 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c  msys5Link(int i,
8e470 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a   int iLogsize){.
8e480 20 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72    int x;.  asser
8e490 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
8e4a0 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78  _held(mem5.mutex
8e4b0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ) );.  assert( i
8e4c0 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42  >=0 && i<mem5.nB
8e4d0 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74  lock );.  assert
8e4e0 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26  ( iLogsize>=0 &&
8e4f0 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41   iLogsize<=LOGMA
8e500 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  X );.  assert( (
8e510 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20  mem5.aCtrl[i] & 
8e520 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69  CTRL_LOGSIZE)==i
8e530 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20  Logsize );..  x 
8e540 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e  = MEM5LINK(i)->n
8e550 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65  ext = mem5.aiFre
8e560 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b  elist[iLogsize];
8e570 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e  .  MEM5LINK(i)->
8e580 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28  prev = -1;.  if(
8e590 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73   x>=0 ){.    ass
8e5a0 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f  ert( x<mem5.nBlo
8e5b0 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49  ck );.    MEM5LI
8e5c0 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b  NK(x)->prev = i;
8e5d0 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72  .  }.  mem5.aiFr
8e5e0 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d  eelist[iLogsize]
8e5f0 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49   = i;.}../*.** I
8e600 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d  f the STATIC_MEM
8e610 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c   mutex is not al
8e620 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61  ready held, obta
8e630 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d  in it now. The m
8e640 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72  utex.** will alr
8e650 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62  eady be held (ob
8e660 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69  tained by code i
8e670 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a  n malloc.c) if.*
8e680 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
8e690 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69  onfig.bMemStat i
8e6a0 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69  s true..*/.stati
8e6b0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e  c void memsys5En
8e6c0 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  ter(void){.  sql
8e6d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
8e6e0 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem5.mutex);.}.
8e6f0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8e700 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a  ys5Leave(void){.
8e710 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
8e720 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65 78  leave(mem5.mutex
8e730 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
8e740 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  rn the size of a
8e750 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c  n outstanding al
8e760 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74  location, in byt
8e770 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65  es.  The.** size
8e780 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20   returned omits 
8e790 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65  the 8-byte heade
8e7a0 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69  r overhead.  Thi
8e7b0 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20  s only.** works 
8e7c0 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20  for chunks that 
8e7d0 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68  are currently ch
8e7e0 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74  ecked out..*/.st
8e7f0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
8e800 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Size(void *p){. 
8e810 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b 0a   int iSize = 0;.
8e820 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
8e830 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29 70 2d  nt i = ((u8 *)p-
8e840 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35  mem5.zPool)/mem5
8e850 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73  .szAtom;.    ass
8e860 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8e870 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8e880 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 73    iSize = mem5.s
8e890 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d  zAtom * (1 << (m
8e8a0 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52  em5.aCtrl[i]&CTR
8e8b0 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d  L_LOGSIZE));.  }
8e8c0 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b  .  return iSize;
8e8d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  .}../*.** Find t
8e8e0 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f  he first entry o
8e8f0 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69  n the freelist i
8e900 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b  Logsize.  Unlink
8e910 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61   that.** entry a
8e920 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e  nd return its in
8e930 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  dex. .*/.static 
8e940 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  int memsys5Unlin
8e950 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73  kFirst(int iLogs
8e960 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ize){.  int i;. 
8e970 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20   int iFirst;..  
8e980 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8e990 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8e9a0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d  =LOGMAX );.  i =
8e9b0 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61   iFirst = mem5.a
8e9c0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
8e9d0 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ze];.  assert( i
8e9e0 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68  First>=0 );.  wh
8e9f0 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20  ile( i>0 ){.    
8ea00 69 66 28 20 69 3c 69 46 69 72 73 74 20 29 20 69  if( i<iFirst ) i
8ea10 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69  First = i;.    i
8ea20 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e   = MEM5LINK(i)->
8ea30 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  next;.  }.  mems
8ea40 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74  ys5Unlink(iFirst
8ea50 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72  , iLogsize);.  r
8ea60 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a  eturn iFirst;.}.
8ea70 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
8ea80 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
8ea90 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74  of at least nByt
8eaa0 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52  es in size..** R
8eab0 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e  eturn NULL if un
8eac0 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55  able.  Return NU
8ead0 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d 3d 30 2e  LL if nBytes==0.
8eae0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  .**.** The calle
8eaf0 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  r guarantees tha
8eb00 74 20 6e 42 79 74 65 20 70 6f 73 69 74 69 76 65  t nByte positive
8eb10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
8eb20 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20  er has obtained 
8eb30 61 20 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f  a mutex prior to
8eb40 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 0a 2a   invoking this.*
8eb50 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 65  * routine so the
8eb60 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e 79 20  re is never any 
8eb70 63 68 61 6e 63 65 20 74 68 61 74 20 74 77 6f 20  chance that two 
8eb80 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68 72 65 61  or more.** threa
8eb90 64 73 20 63 61 6e 20 62 65 20 69 6e 20 74 68 69  ds can be in thi
8eba0 73 20 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65  s routine at the
8ebb0 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73   same time..*/.s
8ebc0 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
8ebd0 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28  ys5MallocUnsafe(
8ebe0 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e  int nByte){.  in
8ebf0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
8ec00 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d  * Index of a mem
8ec10 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a  5.aPool[] slot *
8ec20 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20  /.  int iBin;   
8ec30 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
8ec40 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69  to mem5.aiFreeli
8ec50 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46  st[] */.  int iF
8ec60 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69  ullSz;     /* Si
8ec70 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze of allocation
8ec80 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70   rounded up to p
8ec90 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69  ower of 2 */.  i
8eca0 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20  nt iLogsize;    
8ecb0 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c  /* Log2 of iFull
8ecc0 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a  Sz/POW2_MIN */..
8ecd0 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 73 74 20    /* nByte must 
8ece0 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 2a 2f  be a positive */
8ecf0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
8ed00 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70  >0 );..  /* Keep
8ed10 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6d 61   track of the ma
8ed20 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e  ximum allocation
8ed30 20 72 65 71 75 65 73 74 2e 20 20 45 76 65 6e 20   request.  Even 
8ed40 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a  unfulfilled.  **
8ed50 20 72 65 71 75 65 73 74 73 20 61 72 65 20 63 6f   requests are co
8ed60 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 28  unted */.  if( (
8ed70 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d  u32)nByte>mem5.m
8ed80 61 78 52 65 71 75 65 73 74 20 29 7b 0a 20 20 20  axRequest ){.   
8ed90 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74   mem5.maxRequest
8eda0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20   = nByte;.  }.. 
8edb0 20 2f 2a 20 41 62 6f 72 74 20 69 66 20 74 68 65   /* Abort if the
8edc0 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63   requested alloc
8edd0 61 74 69 6f 6e 20 73 69 7a 65 20 69 73 20 6c 61  ation size is la
8ede0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61  rger than the la
8edf0 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f 77 65 72  rgest.  ** power
8ee00 20 6f 66 20 74 77 6f 20 74 68 61 74 20 77 65 20   of two that we 
8ee10 63 61 6e 20 72 65 70 72 65 73 65 6e 74 20 75 73  can represent us
8ee20 69 6e 67 20 33 32 2d 62 69 74 20 73 69 67 6e 65  ing 32-bit signe
8ee30 64 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f  d integers..  */
8ee40 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3e 20 30  .  if( nByte > 0
8ee50 78 34 30 30 30 30 30 30 30 20 29 7b 0a 20 20 20  x40000000 ){.   
8ee60 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
8ee70 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65    /* Round nByte
8ee80 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20   up to the next 
8ee90 76 61 6c 69 64 20 70 6f 77 65 72 20 6f 66 20 74  valid power of t
8eea0 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 46 75 6c  wo */.  for(iFul
8eeb0 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c  lSz=mem5.szAtom,
8eec0 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 46 75   iLogsize=0; iFu
8eed0 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 46 75 6c  llSz<nByte; iFul
8eee0 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 73 69  lSz *= 2, iLogsi
8eef0 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61  ze++){}..  /* Ma
8ef00 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e 61 69 46  ke sure mem5.aiF
8ef10 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65  reelist[iLogsize
8ef20 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65  ] contains at le
8ef30 61 73 74 20 6f 6e 65 20 66 72 65 65 0a 20 20 2a  ast one free.  *
8ef40 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 6e 6f 74  * block.  If not
8ef50 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 61 20 62  , then split a b
8ef60 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e 65 78 74  lock of the next
8ef70 20 6c 61 72 67 65 72 20 70 6f 77 65 72 20 6f 66   larger power of
8ef80 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 6f 72 64  .  ** two in ord
8ef90 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  er to create a n
8efa0 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 66  ew free block of
8efb0 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a 65 2e 0a   size iLogsize..
8efc0 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 69 6e 3d    */.  for(iBin=
8efd0 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d 35 2e 61  iLogsize; mem5.a
8efe0 69 46 72 65 65 6c 69 73 74 5b 69 42 69 6e 5d 3c  iFreelist[iBin]<
8eff0 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41  0 && iBin<=LOGMA
8f000 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69  X; iBin++){}.  i
8f010 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 58 20 29  f( iBin>LOGMAX )
8f020 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 20 3d   return 0;.  i =
8f030 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69   memsys5UnlinkFi
8f040 72 73 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69  rst(iBin);.  whi
8f050 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a  le( iBin>iLogsiz
8f060 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77  e ){.    int new
8f070 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d  Size;..    iBin-
8f080 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d  -;.    newSize =
8f090 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20   1 << iBin;.    
8f0a0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77  mem5.aCtrl[i+new
8f0b0 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45  Size] = CTRL_FRE
8f0c0 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65  E | iBin;.    me
8f0d0 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53  msys5Link(i+newS
8f0e0 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a  ize, iBin);.  }.
8f0f0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20    mem5.aCtrl[i] 
8f100 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f  = iLogsize;..  /
8f110 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74  * Update allocat
8f120 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73  or performance s
8f130 74 61 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20  tatistics. */.  
8f140 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20  mem5.nAlloc++;. 
8f150 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63   mem5.totalAlloc
8f160 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d   += iFullSz;.  m
8f170 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20  em5.totalExcess 
8f180 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79  += iFullSz - nBy
8f190 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65  te;.  mem5.curre
8f1a0 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d  ntCount++;.  mem
8f1b0 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20  5.currentOut += 
8f1c0 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d  iFullSz;.  if( m
8f1d0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d  em5.maxCount<mem
8f1e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29  5.currentCount )
8f1f0 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d   mem5.maxCount =
8f200 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75   mem5.currentCou
8f210 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d  nt;.  if( mem5.m
8f220 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65  axOut<mem5.curre
8f230 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78  ntOut ) mem5.max
8f240 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65  Out = mem5.curre
8f250 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74  ntOut;..  /* Ret
8f260 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
8f270 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d   the allocated m
8f280 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75  emory. */.  retu
8f290 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e  rn (void*)&mem5.
8f2a0 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41  zPool[i*mem5.szA
8f2b0 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  tom];.}../*.** F
8f2c0 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69  ree an outstandi
8f2d0 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ng memory alloca
8f2e0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
8f2f0 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65  void memsys5Free
8f300 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c  Unsafe(void *pOl
8f310 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20  d){.  u32 size, 
8f320 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20  iLogsize;.  int 
8f330 69 42 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65  iBlock;..  /* Se
8f340 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20  t iBlock to the 
8f350 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f  index of the blo
8f360 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ck pointed to by
8f370 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74   pOld in .  ** t
8f380 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35  he array of mem5
8f390 2e 73 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f  .szAtom byte blo
8f3a0 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  cks pointed to b
8f3b0 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20  y mem5.zPool..  
8f3c0 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28  */.  iBlock = ((
8f3d0 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a  u8 *)pOld-mem5.z
8f3e0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f  Pool)/mem5.szAto
8f3f0 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  m;..  /* Check t
8f400 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20  hat the pointer 
8f410 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61  pOld points to a
8f420 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65   valid, non-free
8f430 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73   block. */.  ass
8f440 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26  ert( iBlock>=0 &
8f450 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42  & iBlock<mem5.nB
8f460 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74  lock );.  assert
8f470 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65  ( ((u8 *)pOld-me
8f480 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73  m5.zPool)%mem5.s
8f490 7a 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73  zAtom==0 );.  as
8f4a0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72  sert( (mem5.aCtr
8f4b0 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c  l[iBlock] & CTRL
8f4c0 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20  _FREE)==0 );..  
8f4d0 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e  iLogsize = mem5.
8f4e0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20  aCtrl[iBlock] & 
8f4f0 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20  CTRL_LOGSIZE;.  
8f500 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69  size = 1<<iLogsi
8f510 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42  ze;.  assert( iB
8f520 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32  lock+size-1<(u32
8f530 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  )mem5.nBlock );.
8f540 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  .  mem5.aCtrl[iB
8f550 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  lock] |= CTRL_FR
8f560 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  EE;.  mem5.aCtrl
8f570 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20  [iBlock+size-1] 
8f580 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20  |= CTRL_FREE;.  
8f590 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
8f5a0 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20  rentCount>0 );. 
8f5b0 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75   assert( mem5.cu
8f5c0 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a  rrentOut>=(size*
8f5d0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a  mem5.szAtom) );.
8f5e0 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f    mem5.currentCo
8f5f0 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75  unt--;.  mem5.cu
8f600 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65  rrentOut -= size
8f610 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20  *mem5.szAtom;.  
8f620 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72  assert( mem5.cur
8f630 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d  rentOut>0 || mem
8f640 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d  5.currentCount==
8f650 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  0 );.  assert( m
8f660 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8f670 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65  >0 || mem5.curre
8f680 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d  ntOut==0 );..  m
8f690 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b  em5.aCtrl[iBlock
8f6a0 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20  ] = CTRL_FREE | 
8f6b0 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c  iLogsize;.  whil
8f6c0 65 28 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69  e( ALWAYS(iLogsi
8f6d0 7a 65 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20  ze<LOGMAX) ){.  
8f6e0 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20    int iBuddy;.  
8f6f0 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69    if( (iBlock>>i
8f700 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a  Logsize) & 1 ){.
8f710 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69        iBuddy = i
8f720 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20  Block - size;.  
8f730 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
8f740 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b  Buddy = iBlock +
8f750 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20   size;.    }.   
8f760 20 61 73 73 65 72 74 28 20 69 42 75 64 64 79 3e   assert( iBuddy>
8f770 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69  =0 );.    if( (i
8f780 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69  Buddy+(1<<iLogsi
8f790 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  ze))>mem5.nBlock
8f7a0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66   ) break;.    if
8f7b0 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75  ( mem5.aCtrl[iBu
8f7c0 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45  ddy]!=(CTRL_FREE
8f7d0 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62   | iLogsize) ) b
8f7e0 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73  reak;.    memsys
8f7f0 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20  5Unlink(iBuddy, 
8f800 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69  iLogsize);.    i
8f810 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69  Logsize++;.    i
8f820 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b  f( iBuddy<iBlock
8f830 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61   ){.      mem5.a
8f840 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43  Ctrl[iBuddy] = C
8f850 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
8f860 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e  ize;.      mem5.
8f870 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20  aCtrl[iBlock] = 
8f880 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20  0;.      iBlock 
8f890 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65  = iBuddy;.    }e
8f8a0 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e  lse{.      mem5.
8f8b0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20  aCtrl[iBlock] = 
8f8c0 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67  CTRL_FREE | iLog
8f8d0 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35  size;.      mem5
8f8e0 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d  .aCtrl[iBuddy] =
8f8f0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69   0;.    }.    si
8f900 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d  ze *= 2;.  }.  m
8f910 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63  emsys5Link(iBloc
8f920 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a  k, iLogsize);.}.
8f930 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
8f940 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79  nBytes of memory
8f950 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8f960 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69  *memsys5Malloc(i
8f970 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71  nt nBytes){.  sq
8f980 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d  lite3_int64 *p =
8f990 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73   0;.  if( nBytes
8f9a0 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73  >0 ){.    memsys
8f9b0 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20  5Enter();.    p 
8f9c0 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55  = memsys5MallocU
8f9d0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20  nsafe(nBytes);. 
8f9e0 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28     memsys5Leave(
8f9f0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
8fa00 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a  (void*)p; .}../*
8fa10 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e  .** Free memory.
8fa20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72  .**.** The outer
8fa30 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c   layer memory al
8fa40 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73  locator prevents
8fa50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72   this routine fr
8fa60 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c  om.** being call
8fa70 65 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d  ed with pPrior==
8fa80 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  0..*/.static voi
8fa90 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f  d memsys5Free(vo
8faa0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61  id *pPrior){.  a
8fab0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30  ssert( pPrior!=0
8fac0 20 29 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74   );.  memsys5Ent
8fad0 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46  er();.  memsys5F
8fae0 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72  reeUnsafe(pPrior
8faf0 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76  );.  memsys5Leav
8fb00 65 28 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e();  .}../*.** 
8fb10 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20  Change the size 
8fb20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d  of an existing m
8fb30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8fb40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65  ..**.** The oute
8fb50 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61  r layer memory a
8fb60 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74  llocator prevent
8fb70 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  s this routine f
8fb80 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c  rom.** being cal
8fb90 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d  led with pPrior=
8fba0 3d 30 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74  =0.  .**.** nByt
8fbb0 65 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 76  es is always a v
8fbc0 61 6c 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72  alue obtained fr
8fbd0 6f 6d 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  om a prior call 
8fbe0 74 6f 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75  to.** memsys5Rou
8fbf0 6e 64 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79  nd().  Hence nBy
8fc00 74 65 73 20 69 73 20 61 6c 77 61 79 73 20 61 20  tes is always a 
8fc10 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77  non-negative pow
8fc20 65 72 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49  er.** of two.  I
8fc30 66 20 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74  f nBytes==0 that
8fc40 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f   means that an o
8fc50 76 65 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69  versize allocati
8fc60 6f 6e 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61  on.** (an alloca
8fc70 74 69 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e  tion larger than
8fc80 20 30 78 34 30 30 30 30 30 30 30 29 20 77 61 73   0x40000000) was
8fc90 20 72 65 71 75 65 73 74 65 64 20 61 6e 64 20 74   requested and t
8fca0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73  his.** routine s
8fcb0 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77  hould return 0 w
8fcc0 69 74 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70  ithout freeing p
8fcd0 50 72 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  Prior..*/.static
8fce0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65   void *memsys5Re
8fcf0 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69  alloc(void *pPri
8fd00 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b  or, int nBytes){
8fd10 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76  .  int nOld;.  v
8fd20 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  oid *p;.  assert
8fd30 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20  ( pPrior!=0 );. 
8fd40 20 61 73 73 65 72 74 28 20 28 6e 42 79 74 65 73   assert( (nBytes
8fd50 26 28 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20  &(nBytes-1))==0 
8fd60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79  );.  assert( nBy
8fd70 74 65 73 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  tes>=0 );.  if( 
8fd80 6e 42 79 74 65 73 3d 3d 30 20 29 7b 0a 20 20 20  nBytes==0 ){.   
8fd90 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
8fda0 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 53   nOld = memsys5S
8fdb0 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69  ize(pPrior);.  i
8fdc0 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20  f( nBytes<=nOld 
8fdd0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
8fde0 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  rior;.  }.  mems
8fdf0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 70 20  ys5Enter();.  p 
8fe00 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55  = memsys5MallocU
8fe10 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20  nsafe(nBytes);. 
8fe20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65   if( p ){.    me
8fe30 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20  mcpy(p, pPrior, 
8fe40 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73 79  nOld);.    memsy
8fe50 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 50 72  s5FreeUnsafe(pPr
8fe60 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  ior);.  }.  mems
8fe70 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 72 65  ys5Leave();.  re
8fe80 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
8fe90 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75   Round up a requ
8fea0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20  est size to the 
8feb0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63  next valid alloc
8fec0 61 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a  ation size.  If.
8fed0 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  ** the allocatio
8fee0 6e 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74  n is too large t
8fef0 6f 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20  o be handled by 
8ff00 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  this allocation 
8ff10 73 79 73 74 65 6d 2c 0a 2a 2a 20 72 65 74 75 72  system,.** retur
8ff20 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61  n 0..**.** All a
8ff30 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 75 73 74 20  llocations must 
8ff40 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77  be a power of tw
8ff50 6f 20 61 6e 64 20 6d 75 73 74 20 62 65 20 65 78  o and must be ex
8ff60 70 72 65 73 73 65 64 20 62 79 20 61 0a 2a 2a 20  pressed by a.** 
8ff70 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
8ff80 74 65 67 65 72 2e 20 20 48 65 6e 63 65 20 74 68  teger.  Hence th
8ff90 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61  e largest alloca
8ffa0 74 69 6f 6e 20 69 73 20 30 78 34 30 30 30 30 30  tion is 0x400000
8ffb0 30 30 0a 2a 2a 20 6f 72 20 31 30 37 33 37 34 31  00.** or 1073741
8ffc0 38 32 34 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  824 bytes..*/.st
8ffd0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
8ffe0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a  Roundup(int n){.
8fff0 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 0a 20    int iFullSz;. 
90000 20 69 66 28 20 6e 20 3e 20 30 78 34 30 30 30 30   if( n > 0x40000
90010 30 30 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  000 ) return 0;.
90020 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65    for(iFullSz=me
90030 6d 35 2e 73 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c  m5.szAtom; iFull
90040 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d  Sz<n; iFullSz *=
90050 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46   2);.  return iF
90060 75 6c 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ullSz;.}../*.** 
90070 52 65 74 75 72 6e 20 74 68 65 20 63 65 69 6c 69  Return the ceili
90080 6e 67 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69  ng of the logari
90090 74 68 6d 20 62 61 73 65 20 32 20 6f 66 20 69 56  thm base 2 of iV
900a0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  alue..**.** Exam
900b0 70 6c 65 73 3a 20 20 20 6d 65 6d 73 79 73 35 4c  ples:   memsys5L
900c0 6f 67 28 31 29 20 2d 3e 20 30 0a 2a 2a 20 20 20  og(1) -> 0.**   
900d0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73            memsys
900e0 35 4c 6f 67 28 32 29 20 2d 3e 20 31 0a 2a 2a 20  5Log(2) -> 1.** 
900f0 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73              mems
90100 79 73 35 4c 6f 67 28 34 29 20 2d 3e 20 32 0a 2a  ys5Log(4) -> 2.*
90110 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  *             me
90120 6d 73 79 73 35 4c 6f 67 28 35 29 20 2d 3e 20 33  msys5Log(5) -> 3
90130 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
90140 6d 65 6d 73 79 73 35 4c 6f 67 28 38 29 20 2d 3e  memsys5Log(8) ->
90150 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   3.**           
90160 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 39 29 20    memsys5Log(9) 
90170 2d 3e 20 34 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -> 4.*/.static i
90180 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28 69 6e  nt memsys5Log(in
90190 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74  t iValue){.  int
901a0 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 4c 6f   iLog;.  for(iLo
901b0 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 3c 69  g=0; (1<<iLog)<i
901c0 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a  Value; iLog++);.
901d0 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d    return iLog;.}
901e0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
901f0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
90200 6c 6f 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  locator..**.** T
90210 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
90220 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  ot threadsafe.  
90230 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
90240 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74  be holding a mut
90250 65 78 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74  ex.** to prevent
90260 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64   multiple thread
90270 73 20 66 72 6f 6d 20 65 6e 74 65 72 69 6e 67 20  s from entering 
90280 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
90290 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
902a0 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64  memsys5Init(void
902b0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e   *NotUsed){.  in
902c0 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
902d0 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
902e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
902f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
90300 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d  er of bytes of m
90310 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
90320 74 6f 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f  to this allocato
90330 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 79 74 65  r */.  u8 *zByte
90340 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  ;         /* Mem
90350 6f 72 79 20 75 73 61 62 6c 65 20 62 79 20 74 68  ory usable by th
90360 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a  is allocator */.
90370 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20    int nMinLog;  
90380 20 20 20 20 20 2f 2a 20 4c 6f 67 20 62 61 73 65       /* Log base
90390 20 32 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61 6c   2 of minimum al
903a0 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 6e  location size in
903b0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
903c0 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f  iOffset;       /
903d0 2a 20 41 6e 20 6f 66 66 73 65 74 20 69 6e 74 6f  * An offset into
903e0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f   mem5.aCtrl[] */
903f0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
90400 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
90410 20 20 2f 2a 20 46 6f 72 20 74 68 65 20 70 75 72    /* For the pur
90420 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f  poses of this ro
90430 75 74 69 6e 65 2c 20 64 69 73 61 62 6c 65 20 74  utine, disable t
90440 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 6d 65  he mutex */.  me
90450 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 0a 20  m5.mutex = 0;.. 
90460 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20   /* The size of 
90470 61 20 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63  a Mem5Link objec
90480 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  t must be a powe
90490 72 20 6f 66 20 74 77 6f 2e 20 20 56 65 72 69 66  r of two.  Verif
904a0 79 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 69 73  y that.  ** this
904b0 20 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20   is case..  */. 
904c0 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66   assert( (sizeof
904d0 28 4d 65 6d 35 4c 69 6e 6b 29 26 28 73 69 7a 65  (Mem5Link)&(size
904e0 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31 29 29  of(Mem5Link)-1))
904f0 3d 3d 30 20 29 3b 0a 0a 20 20 6e 42 79 74 65 20  ==0 );..  nByte 
90500 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
90510 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20 20 7a  onfig.nHeap;.  z
90520 42 79 74 65 20 3d 20 28 75 38 2a 29 73 71 6c 69  Byte = (u8*)sqli
90530 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90540 70 48 65 61 70 3b 0a 20 20 61 73 73 65 72 74 28  pHeap;.  assert(
90550 20 7a 42 79 74 65 21 3d 30 20 29 3b 20 20 2f 2a   zByte!=0 );  /*
90560 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
90570 29 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77  ) does not allow
90580 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 0a 20   otherwise */.. 
90590 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79   nMinLog = memsy
905a0 73 35 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f  s5Log(sqlite3Glo
905b0 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29  balConfig.mnReq)
905c0 3b 0a 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20  ;.  mem5.szAtom 
905d0 3d 20 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a  = (1<<nMinLog);.
905e0 20 20 77 68 69 6c 65 28 20 28 69 6e 74 29 73 69    while( (int)si
905f0 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d  zeof(Mem5Link)>m
90600 65 6d 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a 20 20  em5.szAtom ){.  
90610 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20    mem5.szAtom = 
90620 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31  mem5.szAtom << 1
90630 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42  ;.  }..  mem5.nB
90640 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20 2f 20  lock = (nByte / 
90650 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a  (mem5.szAtom+siz
90660 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d 65 6d  eof(u8)));.  mem
90670 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b  5.zPool = zByte;
90680 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20 3d 20  .  mem5.aCtrl = 
90690 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f  (u8 *)&mem5.zPoo
906a0 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65  l[mem5.nBlock*me
906b0 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66  m5.szAtom];..  f
906c0 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47  or(ii=0; ii<=LOG
906d0 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  MAX; ii++){.    
906e0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
906f0 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20  ii] = -1;.  }.. 
90700 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20   iOffset = 0;.  
90710 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 69  for(ii=LOGMAX; i
90720 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20 20  i>=0; ii--){.   
90730 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 31   int nAlloc = (1
90740 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 20 28  <<ii);.    if( (
90750 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c  iOffset+nAlloc)<
90760 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a  =mem5.nBlock ){.
90770 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c        mem5.aCtrl
90780 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 7c  [iOffset] = ii |
90790 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 20   CTRL_FREE;.    
907a0 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 4f    memsys5Link(iO
907b0 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 20 20  ffset, ii);.    
907c0 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41 6c    iOffset += nAl
907d0 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  loc;.    }.    a
907e0 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 2b 6e  ssert((iOffset+n
907f0 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f  Alloc)>mem5.nBlo
90800 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ck);.  }..  /* I
90810 66 20 61 20 6d 75 74 65 78 20 69 73 20 72 65 71  f a mutex is req
90820 75 69 72 65 64 20 66 6f 72 20 6e 6f 72 6d 61 6c  uired for normal
90830 20 6f 70 65 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f   operation, allo
90840 63 61 74 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66  cate one */.  if
90850 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
90860 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d  onfig.bMemstat==
90870 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75  0 ){.    mem5.mu
90880 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
90890 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
908a0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29  UTEX_STATIC_MEM)
908b0 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
908c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
908d0 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
908e0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
908f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
90900 73 79 73 35 53 68 75 74 64 6f 77 6e 28 76 6f 69  sys5Shutdown(voi
90910 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
90920 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
90930 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 35  NotUsed);.  mem5
90940 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65  .mutex = 0;.  re
90950 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  turn;.}..#ifdef 
90960 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
90970 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
90980 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72  indicated and wr
90990 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c  ite a log of all
909a0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
909b0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  .** allocations 
909c0 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a  into that log..*
909d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
909e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
909f0 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63  sys5Dump(const c
90a00 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
90a10 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20  .  FILE *out;.  
90a20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69  int i, j, n;.  i
90a30 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69  nt nMinLog;..  i
90a40 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
90a50 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d  || zFilename[0]=
90a60 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20  =0 ){.    out = 
90a70 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b  stdout;.  }else{
90a80 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e  .    out = fopen
90a90 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29  (zFilename, "w")
90aa0 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30  ;.    if( out==0
90ab0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
90ac0 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e  f(stderr, "** Un
90ad0 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d  able to output m
90ae0 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70  emory debug outp
90af0 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22  ut log: %s **\n"
90b00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
90b10 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d          zFilenam
90b20 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
90b30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65  ;.    }.  }.  me
90b40 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20  msys5Enter();.  
90b50 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73  nMinLog = memsys
90b60 35 4c 6f 67 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d  5Log(mem5.szAtom
90b70 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
90b80 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e 4d 69  =LOGMAX && i+nMi
90b90 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20  nLog<32; i++){. 
90ba0 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65     for(n=0, j=me
90bb0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 5d  m5.aiFreelist[i]
90bc0 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35  ; j>=0; j = MEM5
90bd0 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e  LINK(j)->next, n
90be0 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69 6e 74  ++){}.    fprint
90bf0 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69 73 74  f(out, "freelist
90c00 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65 20 25   items of size %
90c10 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73  d: %d\n", mem5.s
90c20 7a 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a  zAtom << i, n);.
90c30 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75    }.  fprintf(ou
90c40 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20  t, "mem5.nAlloc 
90c50 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c        = %llu\n",
90c60 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20   mem5.nAlloc);. 
90c70 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
90c80 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 20  em5.totalAlloc  
90c90 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35   = %llu\n", mem5
90ca0 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20  .totalAlloc);.  
90cb0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65  fprintf(out, "me
90cc0 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 20  m5.totalExcess  
90cd0 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e  = %llu\n", mem5.
90ce0 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 20 20  totalExcess);.  
90cf0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65  fprintf(out, "me
90d00 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 20 20  m5.currentOut   
90d10 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75  = %u\n", mem5.cu
90d20 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70 72  rrentOut);.  fpr
90d30 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
90d40 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20 25  currentCount = %
90d50 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65  u\n", mem5.curre
90d60 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69  ntCount);.  fpri
90d70 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
90d80 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 25 75  axOut       = %u
90d90 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75 74  \n", mem5.maxOut
90da0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  );.  fprintf(out
90db0 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74  , "mem5.maxCount
90dc0 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65       = %u\n", me
90dd0 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20 20  m5.maxCount);.  
90de0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65  fprintf(out, "me
90df0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 20 20  m5.maxRequest   
90e00 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61  = %u\n", mem5.ma
90e10 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d 65 6d  xRequest);.  mem
90e20 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 69  sys5Leave();.  i
90e30 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29  f( out==stdout )
90e40 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64  {.    fflush(std
90e50 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  out);.  }else{. 
90e60 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a     fclose(out);.
90e70 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
90e80 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
90e90 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   is the only rou
90ea0 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c  tine in this fil
90eb0 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20  e with external 
90ec0 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 20 49 74 20  .** linkage. It 
90ed0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
90ee0 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 71  r to a static sq
90ef0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
90f00 73 0a 2a 2a 20 73 74 72 75 63 74 20 70 6f 70 75  s.** struct popu
90f10 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6d  lated with the m
90f20 65 6d 73 79 73 35 20 6d 65 74 68 6f 64 73 2e 0a  emsys5 methods..
90f30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
90f40 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
90f50 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  mem_methods *sql
90f60 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
90f70 35 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  5(void){.  stati
90f80 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
90f90 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 73  mem_methods mems
90fa0 79 73 35 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ys5Methods = {. 
90fb0 20 20 20 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f      memsys5Mallo
90fc0 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 46  c,.     memsys5F
90fd0 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  ree,.     memsys
90fe0 35 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  5Realloc,.     m
90ff0 65 6d 73 79 73 35 53 69 7a 65 2c 0a 20 20 20 20  emsys5Size,.    
91000 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 2c   memsys5Roundup,
91010 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 49 6e 69  .     memsys5Ini
91020 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53  t,.     memsys5S
91030 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a  hutdown,.     0.
91040 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d    };.  return &m
91050 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 3b 0a 7d  emsys5Methods;.}
91060 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
91070 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
91080 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  5 */../*********
91090 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
910a0 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  5.c ************
910b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
910e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
910f0 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a   mutex.c *******
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91120 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
91130 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a   August 14.**.**
91140 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
91150 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
91160 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
91170 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
91180 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
91190 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
911a0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
911b0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
911c0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
911d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
911e0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
911f0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
91200 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
91210 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
91220 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
91230 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
91240 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
91250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91290 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
912a0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
912b0 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
912c0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d  that implement m
912d0 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  utexes..**.** Th
912e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
912f0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
91300 6d 6d 6f 6e 20 61 63 72 6f 73 73 20 61 6c 6c 20  mmon across all 
91310 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
91320 74 69 6f 6e 73 2e 0a 2a 2f 0a 0a 23 69 66 20 64  tions..*/..#if d
91330 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
91340 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
91350 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d  (SQLITE_MUTEX_OM
91360 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65  IT)./*.** For de
91370 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
91380 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68  , record when th
91390 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
913a0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
913b0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61  .** and uninitia
913c0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65  lized so that we
913d0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66   can assert() if
913e0 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74   there is an att
913f0 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63  empt to.** alloc
91400 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c  ate a mutex whil
91410 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  e the system is 
91420 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  uninitialized..*
91430 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
91440 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49  WSD int mutexIsI
91450 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20  nit = 0;.#endif 
91460 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
91470 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
91480 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f  ITE_MUTEX_OMIT./
91490 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
914a0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d  the mutex system
914b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
914c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
914d0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
914e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
914f0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c  TE_OK;.  if( sql
91500 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
91510 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20  .bCoreMutex ){. 
91520 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47     if( !sqlite3G
91530 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
91540 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b  x.xMutexAlloc ){
91550 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
91560 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74   xMutexAlloc met
91570 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  hod has not been
91580 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75   set, then the u
91590 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20  ser did not.    
915a0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d    ** install a m
915b0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
915c0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f  ion via sqlite3_
915d0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74  config() prior t
915e0 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  o .      ** sqli
915f0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
91600 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54   being called. T
91610 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73  his block copies
91620 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20   pointers to.   
91630 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c     ** the defaul
91640 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
91650 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
91660 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74  3GlobalConfig st
91670 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a  ructure..      *
91680 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  /.      sqlite3_
91690 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70  mutex_methods *p
916a0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65  From = sqlite3De
916b0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20  faultMutex();.  
916c0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
916d0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d  x_methods *pTo =
916e0 20 26 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 3b 0a 0a 20 20  onfig.mutex;..  
91700 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20      memcpy(pTo, 
91710 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28  pFrom, offsetof(
91720 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
91730 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c  thods, xMutexAll
91740 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  oc));.      memc
91750 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46  py(&pTo->xMutexF
91760 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75  ree, &pFrom->xMu
91770 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20  texFree,.       
91780 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54        sizeof(*pT
91790 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71  o) - offsetof(sq
917a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
917b0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29  ods, xMutexFree)
917c0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d  );.      pTo->xM
917d0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f  utexAlloc = pFro
917e0 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a  m->xMutexAlloc;.
917f0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
91800 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91810 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49  ig.mutex.xMutexI
91820 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  nit();.  }..#ifd
91830 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91840 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
91850 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a  texIsInit) = 1;.
91860 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
91870 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68   rc;.}../*.** Sh
91880 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78  utdown the mutex
91890 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61   system. This ca
918a0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63  ll frees resourc
918b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
918c0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  ** sqlite3MutexI
918d0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nit()..*/.SQLITE
918e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
918f0 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69  ite3MutexEnd(voi
91900 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
91910 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
91920 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91930 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91940 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  End ){.    rc = 
91950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91960 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91970 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  End();.  }..#ifd
91980 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91990 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
919a0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a  texIsInit) = 0;.
919b0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
919c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
919d0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72  trieve a pointer
919e0 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74   to a static mut
919f0 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61  ex or allocate a
91a00 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65   new dynamic one
91a10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
91a20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
91a30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
91a40 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e  oc(int id){.#ifn
91a50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
91a60 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
91a70 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
91a80 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  e() ) return 0;.
91a90 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
91aa0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91ab0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91ac0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51  Alloc(id);.}..SQ
91ad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
91ae0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
91af0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  te3MutexAlloc(in
91b00 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71  t id){.  if( !sq
91b10 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
91b20 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
91b30 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
91b40 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42  }.  assert( GLOB
91b50 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49  AL(int, mutexIsI
91b60 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  nit) );.  return
91b70 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
91b80 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
91b90 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f  xAlloc(id);.}../
91ba0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61  *.** Free a dyna
91bb0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  mic mutex..*/.SQ
91bc0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
91bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
91be0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
91bf0 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
91c00 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
91c10 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91c20 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a  texFree(p);.  }.
91c30 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
91c40 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20  the mutex p. If 
91c50 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
91c60 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68  d already has th
91c70 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a  e mutex, block.*
91c80 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62  * until it can b
91c90 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53  e obtained..*/.S
91ca0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
91cb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
91cc0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
91cd0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
91ce0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
91cf0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91d00 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20  MutexEnter(p);. 
91d10 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61   }.}../*.** Obta
91d20 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20  in the mutex p. 
91d30 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
91d40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
91d50 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
91d60 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64  nother.** thread
91d70 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78   holds the mutex
91d80 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62   and it cannot b
91d90 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75  e obtained, retu
91da0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
91db0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
91dc0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
91dd0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
91de0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  x *p){.  int rc 
91df0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
91e00 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75  f( p ){.    retu
91e10 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
91e20 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91e30 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20  texTry(p);.  }. 
91e40 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
91e50 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
91e60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
91e70 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
91e80 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72  utex that was pr
91e90 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65  eviously.** ente
91ea0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
91eb0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68  thread.  The beh
91ec0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
91ed0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
91ee0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
91ef0 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66  ntly entered. If
91f00 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
91f10 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  is passed as an 
91f20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73  argument.** this
91f30 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
91f40 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
91f50 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
91f60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
91f70 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91f80 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
91f90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91fa0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c  ig.mutex.xMutexL
91fb0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  eave(p);.  }.}..
91fc0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
91fd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
91fe0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
91ff0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
92000 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
92010 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
92020 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
92030 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
92040 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
92050 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
92060 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
92070 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
92080 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73  return p==0 || s
92090 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
920a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48  ig.mutex.xMutexH
920b0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45  eld(p);.}.SQLITE
920c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
920d0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73  _mutex_notheld(s
920e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
920f0 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  {.  return p==0 
92100 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  || sqlite3Global
92110 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
92120 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d  texNotheld(p);.}
92130 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
92140 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
92150 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  OMIT */../******
92160 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
92170 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
92180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
921a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
921b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
921c0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63  ile mutex_noop.c
921d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
921e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
921f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
92200 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a  008 October 07.*
92210 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
92220 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
92230 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
92240 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
92250 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
92260 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
92270 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
92280 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
92290 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
922a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
922b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
922c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
922d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
922e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
922f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
92300 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
92310 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
92320 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
92330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
92370 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
92380 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
92390 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
923a0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a  nt mutexes..**.*
923b0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
923c0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69  ation in this fi
923d0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76  le does not prov
923e0 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a  ide any mutual.*
923f0 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20  * exclusion and 
92400 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65  is thus suitable
92410 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e   for use only in
92420 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
92430 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
92440 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72   in a single thr
92450 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ead.  The routin
92460 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65  es defined.** he
92470 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c  re are place-hol
92480 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69  ders.  Applicati
92490 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75  ons can substitu
924a0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75  te working.** mu
924b0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20  tex routines at 
924c0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67  start-time using
924d0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73   the.**.**     s
924e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
924f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
92500 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74  X,...).**.** int
92510 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  erface..**.** If
92520 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53   compiled with S
92530 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65  QLITE_DEBUG, the
92540 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67  n additional log
92550 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a  ic is inserted.*
92560 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f  * that does erro
92570 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75  r checking on mu
92580 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75  texes to make su
92590 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e  re they are bein
925a0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72  g.** called corr
925b0 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 23 69 66 20  ectly..*/...#if 
925c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
925d0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64  UTEX_NOOP) && !d
925e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
925f0 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20  BUG)./*.** Stub 
92600 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c  routines for all
92610 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 2e 0a   mutex methods..
92620 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
92630 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 6d  nes provide no m
92640 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20  utual exclusion 
92650 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  or error checkin
92660 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
92670 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28 73   noopMutexHeld(s
92680 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
92690 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74  { return 1; }.st
926a0 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74  atic int noopMut
926b0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  exNotheld(sqlite
926c0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74  3_mutex *p){ ret
926d0 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20  urn 1; }.static 
926e0 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69  int noopMutexIni
926f0 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  t(void){ return 
92700 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61  SQLITE_OK; }.sta
92710 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
92720 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
92730 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
92740 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
92750 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41  utex *noopMutexA
92760 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 72 65  lloc(int id){ re
92770 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75  turn (sqlite3_mu
92780 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69 63  tex*)8; }.static
92790 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 46   void noopMutexF
927a0 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ree(sqlite3_mute
927b0 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d  x *p){ return; }
927c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f  .static void noo
927d0 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69  pMutexEnter(sqli
927e0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72  te3_mutex *p){ r
927f0 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20  eturn; }.static 
92800 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79  int noopMutexTry
92810 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
92820 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  p){ return SQLIT
92830 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76  E_OK; }.static v
92840 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61  oid noopMutexLea
92850 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
92860 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a   *p){ return; }.
92870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
92880 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
92890 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
928a0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
928b0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
928c0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
928d0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
928e0 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20  noopMutexInit,. 
928f0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c     noopMutexEnd,
92900 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c  .    noopMutexAl
92910 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  loc,.    noopMut
92920 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70  exFree,.    noop
92930 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
92940 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20 20  noopMutexTry,.  
92950 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65    noopMutexLeave
92960 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  ,..    noopMutex
92970 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75  Held,.    noopMu
92980 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a  texNotheld.  };.
92990 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65  .  return &sMute
929a0 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64  x;.}.#endif /* d
929b0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55  efined(SQLITE_MU
929c0 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65  TEX_NOOP) && !de
929d0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
929e0 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69  UG) */..#if defi
929f0 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  ned(SQLITE_MUTEX
92a00 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65  _NOOP) && define
92a10 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
92a20 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d  /*.** In this im
92a30 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 72  plementation, er
92a40 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20  ror checking is 
92a50 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 65 73  provided for tes
92a60 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 75  ting.** and debu
92a70 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 20  gging purposes. 
92a80 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 74 69   The mutexes sti
92a90 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64  ll do not provid
92aa0 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c 20  e any.** mutual 
92ab0 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f  exclusion..*/../
92ac0 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f  *.** The mutex o
92ad0 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74 20  bject.*/.struct 
92ae0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
92af0 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 2f 2a    int id;     /*
92b00 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 65 20   The mutex type 
92b10 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
92b20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
92b30 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 61 20  tries without a 
92b40 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 20 2a  matching leave *
92b50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
92b60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
92b70 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
92b80 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
92b90 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20   routine are.** 
92ba0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
92bb0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
92bc0 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
92bd0 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67  static int debug
92be0 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65  MutexHeld(sqlite
92bf0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
92c00 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d  eturn p==0 || p-
92c10 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63  >cnt>0;.}.static
92c20 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e   int debugMutexN
92c30 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
92c40 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
92c50 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e  rn p==0 || p->cn
92c60 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  t==0;.}../*.** I
92c70 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65  nitialize and de
92c80 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
92c90 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a  utex subsystem..
92ca0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
92cb0 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  bugMutexInit(voi
92cc0 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
92cd0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69  E_OK; }.static i
92ce0 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64  nt debugMutexEnd
92cf0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
92d00 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a  QLITE_OK; }../*.
92d10 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
92d20 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75  utex_alloc() rou
92d30 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61  tine allocates a
92d40 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e   new.** mutex an
92d50 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
92d60 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69  ter to it.  If i
92d70 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a  t returns NULL.*
92d80 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  * that means tha
92d90 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20  t a mutex could 
92da0 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  not be allocated
92db0 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  . .*/.static sql
92dc0 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 62 75  ite3_mutex *debu
92dd0 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20  gMutexAlloc(int 
92de0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
92df0 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 74 61  lite3_mutex aSta
92e00 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74 65  tic[6];.  sqlite
92e10 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d 20  3_mutex *pNew = 
92e20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 20  0;.  switch( id 
92e30 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
92e40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20  TE_MUTEX_FAST:. 
92e50 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
92e60 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20  UTEX_RECURSIVE: 
92e70 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  {.      pNew = s
92e80 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a  qlite3Malloc(siz
92e90 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20  eof(*pNew));.   
92ea0 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
92eb0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20         pNew->id 
92ec0 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 70 4e  = id;.        pN
92ed0 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  ew->cnt = 0;.   
92ee0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
92ef0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
92f00 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
92f10 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 20 29  ert( id-2 >= 0 )
92f20 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
92f30 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 69 7a  id-2 < (int)(siz
92f40 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a  eof(aStatic)/siz
92f50 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d 29 29  eof(aStatic[0]))
92f60 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d   );.      pNew =
92f70 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b   &aStatic[id-2];
92f80 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20  .      pNew->id 
92f90 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 65 61  = id;.      brea
92fa0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
92fb0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
92fc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
92fd0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
92fe0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
92ff0 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73  ated mutex..*/.s
93000 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67  tatic void debug
93010 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65  MutexFree(sqlite
93020 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
93030 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30  ssert( p->cnt==0
93040 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
93050 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
93060 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d  X_FAST || p->id=
93070 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
93080 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 71 6c  CURSIVE );.  sql
93090 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
930a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
930b0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
930c0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
930d0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
930e0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
930f0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20  enter a mutex.  
93100 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
93110 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74  d is already wit
93120 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a  hin the mutex,.*
93130 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
93140 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f  enter() will blo
93150 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  ck and sqlite3_m
93160 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20  utex_try() will 
93170 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
93180 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69  _BUSY.  The sqli
93190 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
931a0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
931b0 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75  s SQLITE_OK.** u
931c0 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
931d0 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63  ntry.  Mutexes c
931e0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c  reated using SQL
931f0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
93200 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e  IVE can.** be en
93210 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
93220 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65  imes by the same
93230 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63   thread.  In suc
93240 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
93250 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
93260 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
93270 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
93280 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
93290 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
932a0 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
932b0 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
932c0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
932d0 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20  ind of mutex.** 
932e0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
932f0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
93300 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  undefined..*/.st
93310 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d  atic void debugM
93320 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65  utexEnter(sqlite
93330 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
93340 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
93350 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
93360 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74  SIVE || debugMut
93370 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a  exNotheld(p) );.
93380 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74    p->cnt++;.}.st
93390 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
933a0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
933b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
933c0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
933d0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
933e0 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e  E || debugMutexN
933f0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70  otheld(p) );.  p
93400 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72  ->cnt++;.  retur
93410 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
93420 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
93430 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
93440 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
93450 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
93460 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
93470 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
93480 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65   thread.  The be
93490 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
934a0 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
934b0 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
934c0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a  ntly entered or.
934d0 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
934e0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
934f0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
93500 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a  r do either..*/.
93510 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75  static void debu
93520 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  gMutexLeave(sqli
93530 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
93540 20 61 73 73 65 72 74 28 20 64 65 62 75 67 4d 75   assert( debugMu
93550 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20  texHeld(p) );.  
93560 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65  p->cnt--;.  asse
93570 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
93580 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
93590 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e  E || debugMutexN
935a0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a  otheld(p) );.}..
935b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
935c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
935d0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66  hods *sqlite3Def
935e0 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b  aultMutex(void){
935f0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
93600 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
93610 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 64  sMutex = {.    d
93620 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20  ebugMutexInit,. 
93630 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64     debugMutexEnd
93640 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
93650 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75 67  Alloc,.    debug
93660 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 64  MutexFree,.    d
93670 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a  ebugMutexEnter,.
93680 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 54 72      debugMutexTr
93690 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  y,.    debugMute
936a0 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 65 62  xLeave,..    deb
936b0 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20  ugMutexHeld,.   
936c0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
936d0 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72  ld.  };..  retur
936e0 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e  n &sMutex;.}.#en
936f0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
93700 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
93710 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
93720 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f  ITE_DEBUG) */../
93730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
93740 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70  nd of mutex_noop
93750 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
93760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
93780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
93790 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
937a0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
937b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
937c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
937d0 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74  *.** 2007 August
937e0 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   28.**.** The au
937f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
93800 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
93810 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
93820 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
93830 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
93840 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
93850 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
93860 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
93870 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
93880 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
93890 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
938a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
938b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
938c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
938d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
938e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
938f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
93900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93940 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
93950 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
93960 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
93970 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
93980 66 6f 72 20 4f 53 2f 32 0a 2a 2f 0a 0a 2f 2a 0a  for OS/2.*/../*.
93990 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
939a0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
939b0 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f   used if SQLITE_
939c0 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66  MUTEX_OS2 is def
939d0 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65  ined..** See the
939e0 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f   mutex.h file fo
939f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
93a00 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
93a10 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  X_OS2../********
93a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f  ************** O
93a30 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d  S/2 Mutex Implem
93a40 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
93a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
93a60 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
93a70 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74  mentation of mut
93a80 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73  exes is built us
93a90 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49  ing the OS/2 API
93aa0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
93ab0 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20  mutex object.** 
93ac0 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
93ad0 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
93ae0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
93af0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
93b00 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
93b10 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58  3_mutex {.  HMTX
93b20 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a   mutex;       /*
93b30 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
93b40 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  ng the lock */. 
93b50 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20   int  id;       
93b60 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
93b70 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b   */.  int  nRef;
93b80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
93b90 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
93ba0 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b  */.  TID  owner;
93bb0 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64         /* Thread
93bc0 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75   holding this mu
93bd0 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  tex */.};..#defi
93be0 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49  ne OS2_MUTEX_INI
93bf0 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30  TIALIZER   0,0,0
93c00 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  ,0../*.** Initia
93c10 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
93c20 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
93c30 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
93c40 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
93c50 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
93c60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
93c70 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
93c80 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
93c90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93ca0 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71   }../*.** The sq
93cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
93cc0 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
93cd0 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d  cates a new.** m
93ce0 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73  utex and returns
93cf0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
93d00 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  .  If it returns
93d10 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65   NULL.** that me
93d20 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
93d30 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
93d40 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c  located. .** SQL
93d50 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20  ite will unwind 
93d60 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65  its stack and re
93d70 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20  turn an error.  
93d80 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
93d90 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
93da0 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20  _alloc() is one 
93db0 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
93dc0 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
93dd0 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
93de0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
93df0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
93e00 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  0.** <li>  SQLIT
93e10 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
93e20 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20  E          1.** 
93e30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
93e40 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
93e50 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20        2.** <li> 
93e60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
93e70 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20  ATIC_MEM        
93e80 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   3.** <li>  SQLI
93e90 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
93ea0 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a  PRNG        4.**
93eb0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
93ec0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
93ed0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
93ee0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
93ef0 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
93f00 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
93f10 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
93f20 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
93f30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
93f40 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
93f50 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
93f60 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
93f70 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
93f80 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
93f90 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
93fa0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
93fb0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
93fc0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
93fd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
93fe0 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
93ff0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
94000 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
94010 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
94020 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
94030 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
94040 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
94050 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
94060 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
94070 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
94080 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
94090 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
940a0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
940b0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
940c0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
940d0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
940e0 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
940f0 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
94100 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
94110 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
94120 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
94130 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
94140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
94150 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
94160 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
94170 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
94180 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72  ting mutex.  Thr
94190 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ee static mutexe
941a0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
941b0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
941c0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
941d0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
941e0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
941f0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
94200 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
94210 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
94220 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
94230 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
94240 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
94250 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
94260 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
94270 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
94280 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
94290 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
942a0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
942b0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
942c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
942d0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
942e0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
942f0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
94300 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
94310 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
94320 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
94330 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
94340 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
94350 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
94360 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
94370 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
94380 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
94390 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
943a0 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
943b0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
943c0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
943d0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
943e0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
943f0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
94400 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
94410 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69  *os2MutexAlloc(i
94420 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c  nt iType){.  sql
94430 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20  ite3_mutex *p = 
94440 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20  NULL;.  switch( 
94450 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
94460 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  e SQLITE_MUTEX_F
94470 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51  AST:.    case SQ
94480 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
94490 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20  SIVE: {.      p 
944a0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
944b0 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
944c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  );.      if( p )
944d0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20  {.        p->id 
944e0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20  = iType;.       
944f0 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75   if( DosCreateMu
94500 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d  texSem( 0, &p->m
94510 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
94520 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
94530 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
94540 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20  3_free( p );.   
94550 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b         p = NULL;
94560 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
94570 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
94580 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
94590 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69  t: {.      stati
945a0 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69  c volatile int i
945b0 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  sInit = 0;.     
945c0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
945d0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
945e0 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  xes[] = {.      
945f0 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
94600 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
94610 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
94620 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
94630 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f  ,.        { OS2_
94640 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
94650 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20  R, },.        { 
94660 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
94670 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20  LIZER, },.      
94680 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
94690 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
946a0 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
946b0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
946c0 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  ,.      };.     
946d0 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b   if ( !isInit ){
946e0 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20  .        APIRET 
946f0 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42  rc;.        PTIB
94700 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50   ptib;.        P
94710 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20  PIB ppib;.      
94720 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20    HMTX mutex;.  
94730 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b        char name[
94740 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73  32];.        Dos
94750 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26  GetInfoBlocks( &
94760 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20  ptib, &ppib );. 
94770 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
94780 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28  nprintf( sizeof(
94790 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c  name), name, "\\
947a0 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34  SEM32\\SQLITE%04
947b0 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x",.            
947c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
947d0 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b  ib->pib_ulpid );
947e0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
947f0 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20  !isInit ){.     
94800 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a       mutex = 0;.
94810 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44            rc = D
94820 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d  osCreateMutexSem
94830 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20  ( name, &mutex, 
94840 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20  0, FALSE);.     
94850 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e       if( rc == N
94860 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
94870 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
94880 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20  int i;.         
94890 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29     if( !isInit )
948a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
948b0 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
948c0 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
948d0 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
948e0 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20  ticMutexes[0]); 
948f0 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i++ ){.         
94900 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65         DosCreate
94910 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74  MutexSem( 0, &st
94920 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d  aticMutexes[i].m
94930 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
94940 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
94950 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
94960 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20  isInit = 1;.    
94970 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
94980 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75        DosCloseMu
94990 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b  texSem( mutex );
949a0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
949b0 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52   if( rc == ERROR
949c0 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20  _DUPLICATE_NAME 
949d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44  ){.            D
949e0 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20  osSleep( 1 );.  
949f0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
94a00 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
94a10 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n p;.          }
94a20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
94a30 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
94a40 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
94a50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
94a60 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28  Type-2 < sizeof(
94a70 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73  staticMutexes)/s
94a80 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
94a90 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20  xes[0]) );.     
94aa0 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65   p = &staticMute
94ab0 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20  xes[iType-2];.  
94ac0 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
94ad0 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  e;.      break;.
94ae0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
94af0 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn p;.}.../*.** 
94b00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61  This routine dea
94b10 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69  llocates a previ
94b20 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
94b30 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65  mutex..** SQLite
94b40 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
94b50 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20  eallocate every 
94b60 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c  mutex that it al
94b70 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  locates..*/.stat
94b80 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78  ic void os2Mutex
94b90 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
94ba0 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ex *p){.  if( p=
94bb0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
94bc0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
94bd0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
94be0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
94bf0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
94c00 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
94c10 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f  ECURSIVE );.  Do
94c20 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20  sCloseMutexSem( 
94c30 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71  p->mutex );.  sq
94c40 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b  lite3_free( p );
94c50 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
94c60 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68  E_DEBUG./*.** Th
94c70 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
94c80 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74  held() and sqlit
94c90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
94ca0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a  () routine are.*
94cb0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  * intended for u
94cc0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
94cd0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
94ce0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
94cf0 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65  MutexHeld(sqlite
94d00 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54  3_mutex *p){.  T
94d10 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69  ID tid;.  PID pi
94d20 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75  d;.  ULONG ulCou
94d30 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b  nt;.  PTIB ptib;
94d40 0a 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a  .  if( p!=0 ) {.
94d50 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65      DosQueryMute
94d60 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
94d70 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f  pid, &tid, &ulCo
94d80 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  unt);.  } else {
94d90 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42  .    DosGetInfoB
94da0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c  locks(&ptib, NUL
94db0 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74  L);.    tid = pt
94dc0 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74  ib->tib_ptib2->t
94dd0 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20  ib2_ultid;.  }. 
94de0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20   return p==0 || 
94df0 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70  (p->nRef!=0 && p
94e00 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d  ->owner==tid);.}
94e10 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
94e20 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
94e30 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
94e40 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20   TID tid;.  PID 
94e50 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43  pid;.  ULONG ulC
94e60 6f 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69  ount;.  PTIB pti
94e70 62 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29  b;.  if( p!= 0 )
94e80 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d   {.    DosQueryM
94e90 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
94ea0 2c 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75  , &pid, &tid, &u
94eb0 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73  lCount);.  } els
94ec0 65 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e  e {.    DosGetIn
94ed0 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20  foBlocks(&ptib, 
94ee0 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d  NULL);.    tid =
94ef0 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32   ptib->tib_ptib2
94f00 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20  ->tib2_ultid;.  
94f10 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  }.  return p==0 
94f20 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  || p->nRef==0 ||
94f30 20 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a   p->owner!=tid;.
94f40 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
94f50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
94f60 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
94f70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
94f80 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
94f90 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
94fa0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
94fb0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
94fc0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
94fd0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
94fe0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
94ff0 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
95000 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
95010 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
95020 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
95030 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
95040 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
95050 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
95060 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
95070 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
95080 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
95090 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
950a0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
950b0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
950c0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
950d0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
950e0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
950f0 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
95100 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
95110 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
95120 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
95130 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
95140 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
95150 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
95160 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
95170 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
95180 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
95190 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
951a0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
951b0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
951c0 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72  id os2MutexEnter
951d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
951e0 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
951f0 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20   PID holder1;.  
95200 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20  ULONG holder2;. 
95210 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
95220 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
95230 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
95240 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f  X_RECURSIVE || o
95250 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  s2MutexNotheld(p
95260 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73  ) );.  DosReques
95270 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  tMutexSem(p->mut
95280 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49  ex, SEM_INDEFINI
95290 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51  TE_WAIT);.  DosQ
952a0 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e  ueryMutexSem(p->
952b0 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c  mutex, &holder1,
952c0 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29   &tid, &holder2)
952d0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74  ;.  p->owner = t
952e0 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  id;.  p->nRef++;
952f0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  .}.static int os
95300 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  2MutexTry(sqlite
95310 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
95320 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64  nt rc;.  TID tid
95330 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b  ;.  PID holder1;
95340 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32  .  ULONG holder2
95350 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
95360 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
95370 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
95380 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
95390 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d  ECURSIVE || os2M
953a0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
953b0 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65  ;.  if( DosReque
953c0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  stMutexSem(p->mu
953d0 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41  tex, SEM_IMMEDIA
953e0 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f  TE_RETURN) == NO
953f0 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f  _ERROR) {.    Do
95400 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
95410 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
95420 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
95430 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72  2);.    p->owner
95440 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e   = tid;.    p->n
95450 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
95460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65  SQLITE_OK;.  } e
95470 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53  lse {.    rc = S
95480 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
95490 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
954a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
954b0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
954c0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
954d0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
954e0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
954f0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
95500 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
95510 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
95520 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
95530 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
95540 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
95550 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
95560 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
95570 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
95580 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
95590 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
955a0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74  MutexLeave(sqlit
955b0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
955c0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
955d0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
955e0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
955f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
95600 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
95610 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d  0 );.  DosQueryM
95620 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78  utexSem(p->mutex
95630 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64  , &holder1, &tid
95640 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61  , &holder2);.  a
95650 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d  ssert( p->owner=
95660 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  =tid );.  p->nRe
95670 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
95680 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
95690 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
956a0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
956b0 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53  DosReleaseMutexS
956c0 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  em(p->mutex);.}.
956d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
956e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
956f0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
95700 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
95710 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
95720 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
95730 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
95740 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20  os2MutexInit,.  
95750 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20    os2MutexEnd,. 
95760 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63     os2MutexAlloc
95770 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72  ,.    os2MutexFr
95780 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  ee,.    os2Mutex
95790 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75  Enter,.    os2Mu
957a0 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d  texTry,.    os2M
957b0 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65  utexLeave,.#ifde
957c0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
957d0 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c     os2MutexHeld,
957e0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74  .    os2MutexNot
957f0 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  held.#endif.  };
95800 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74  ..  return &sMut
95810 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ex;.}.#endif /* 
95820 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
95830 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
95840 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
95850 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
95860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95880 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
95890 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
958a0 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a  mutex_unix.c ***
958b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
958c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
958d0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
958e0 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20  August 28.**.** 
958f0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
95900 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
95910 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
95920 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
95930 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
95940 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
95950 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
95960 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
95970 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
95980 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
95990 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
959a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
959b0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
959c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
959d0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
959e0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
959f0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
95a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a40 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
95a50 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
95a60 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
95a70 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  hat implement mu
95a80 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61  texes for pthrea
95a90 64 73 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ds.*/../*.** The
95aa0 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
95ab0 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  le is only used 
95ac0 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c  if we are compil
95ad0 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 0a 2a  ing threadsafe.*
95ae0 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77 69 74  * under unix wit
95af0 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a  h pthreads..**.*
95b00 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73  * Note that this
95b10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
95b20 72 65 71 75 69 72 65 73 20 61 20 76 65 72 73 69  requires a versi
95b30 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73 20 74  on of pthreads t
95b40 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20  hat.** supports 
95b50 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
95b60 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
95b70 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
95b80 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74  DS..#include <pt
95b90 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a  hread.h>.../*.**
95ba0 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20   Each recursive 
95bb0 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74  mutex is an inst
95bc0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
95bd0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
95be0 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
95bf0 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 74 68  e3_mutex {.  pth
95c00 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74  read_mutex_t mut
95c10 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 65 78  ex;     /* Mutex
95c20 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65   controlling the
95c30 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69   lock */.  int i
95c40 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
95c50 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74        /* Mutex t
95c60 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ype */.  int nRe
95c70 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
95c80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
95c90 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20   entrances */.  
95ca0 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b  pthread_t owner;
95cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
95cc0 72 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74  read that is wit
95cd0 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a  hin this mutex *
95ce0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
95cf0 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72 61 63  DEBUG.  int trac
95d00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
95d10 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72     /* True to tr
95d20 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23  ace changes */.#
95d30 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20  endif.};.#ifdef 
95d40 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65  SQLITE_DEBUG.#de
95d50 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
95d60 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
95d70 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
95d80 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30  NITIALIZER, 0, 0
95d90 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20  , (pthread_t)0, 
95da0 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  0 }.#else.#defin
95db0 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
95dc0 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54  INITIALIZER { PT
95dd0 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54  HREAD_MUTEX_INIT
95de0 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28  IALIZER, 0, 0, (
95df0 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a 23 65  pthread_t)0 }.#e
95e00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
95e10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
95e20 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
95e30 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
95e40 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20   routine are.** 
95e50 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
95e60 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73   only inside ass
95e70 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
95e80 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66  .  On some platf
95e90 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d  orms,.** there m
95ea0 69 67 68 74 20 62 65 20 72 61 63 65 20 63 6f 6e  ight be race con
95eb0 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  ditions that can
95ec0 20 63 61 75 73 65 20 74 68 65 73 65 20 72 6f 75   cause these rou
95ed0 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69  tines to.** deli
95ee0 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20 72 65  ver incorrect re
95ef0 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 74 69  sults.  In parti
95f00 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72 65 61  cular, if pthrea
95f10 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20  d_equal() is.** 
95f20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70  not an atomic op
95f30 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  eration, then th
95f40 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 69 67  ese routines mig
95f50 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69  ht delivery.** i
95f60 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73  ncorrect results
95f70 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66  .  On most platf
95f80 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f 65 71  orms, pthread_eq
95f90 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a 20 63  ual() is a .** c
95fa0 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f  omparison of two
95fb0 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 69 73   integers and is
95fc0 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f 6d 69   therefore atomi
95fd0 63 2e 20 20 42 75 74 20 77 65 20 61 72 65 0a 2a  c.  But we are.*
95fe0 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50 55 58  * told that HPUX
95ff0 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61 20 70   is not such a p
96000 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c  latform.  If so,
96010 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74   then these rout
96020 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  ines.** will not
96030 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 6f 72   always work cor
96040 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a  rectly on HPUX..
96050 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70  **.** On those p
96060 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65 20 70  latforms where p
96070 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69  thread_equal() i
96080 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51  s not atomic, SQ
96090 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  Lite.** should b
960a0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f  e compiled witho
960b0 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55  ut -DSQLITE_DEBU
960c0 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e 44 45  G and with -DNDE
960d0 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73  BUG to.** make s
960e0 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28 29 20  ure no assert() 
960f0 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65  statements are e
96100 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68 65 6e  valuated and hen
96110 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74  ce these.** rout
96120 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72 20 63  ines are never c
96130 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64  alled..*/.#if !d
96140 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
96150 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
96160 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69  _DEBUG).static i
96170 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 48  nt pthreadMutexH
96180 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
96190 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  x *p){.  return 
961a0 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70  (p->nRef!=0 && p
961b0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e  thread_equal(p->
961c0 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73  owner, pthread_s
961d0 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69  elf()));.}.stati
961e0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74  c int pthreadMut
961f0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  exNotheld(sqlite
96200 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
96210 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30  eturn p->nRef==0
96220 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 75 61   || pthread_equa
96230 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72  l(p->owner, pthr
96240 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a  ead_self())==0;.
96250 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
96260 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
96270 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
96280 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
96290 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
962a0 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 28  threadMutexInit(
962b0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
962c0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
962d0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74  c int pthreadMut
962e0 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74  exEnd(void){ ret
962f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
96300 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
96310 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
96320 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61  ) routine alloca
96330 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74  tes a new.** mut
96340 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  ex and returns a
96350 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
96360 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e   If it returns N
96370 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e  ULL.** that mean
96380 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63  s that a mutex c
96390 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f  ould not be allo
963a0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a  cated.  SQLite.*
963b0 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74  * will unwind it
963c0 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75  s stack and retu
963d0 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68  rn an error.  Th
963e0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  e argument.** to
963f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
96400 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66  lloc() is one of
96410 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63   these integer c
96420 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  onstants:.**.** 
96430 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  <ul>.** <li>  SQ
96440 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a  LITE_MUTEX_FAST.
96450 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
96460 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
96470 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
96480 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
96490 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  TER.** <li>  SQL
964a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
964b0 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  _MEM.** <li>  SQ
964c0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
964d0 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20  C_MEM2.** <li>  
964e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
964f0 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e  TIC_PRNG.** <li>
96500 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
96510 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69  TATIC_LRU.** <li
96520 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
96530 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c  STATIC_LRU2.** <
96540 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  /ul>.**.** The f
96550 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e  irst two constan
96560 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  ts cause sqlite3
96570 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74  _mutex_alloc() t
96580 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65  o create.** a ne
96590 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65  w mutex.  The ne
965a0 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72  w mutex is recur
965b0 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45  sive when SQLITE
965c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
965d0 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20  .** is used but 
965e0 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
965f0 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  so when SQLITE_M
96600 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65  UTEX_FAST is use
96610 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  d..** The mutex 
96620 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
96630 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
96640 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69  make a distincti
96650 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51  on.** between SQ
96660 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
96670 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f  SIVE and SQLITE_
96680 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74  MUTEX_FAST if it
96690 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e   does.** not wan
966a0 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74  t to.  But SQLit
966b0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75  e will only requ
966c0 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20  est a recursive 
966d0 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65  mutex in.** case
966e0 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c  s where it reall
966f0 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66  y needs one.  If
96700 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65   a faster non-re
96710 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a  cursive mutex.**
96720 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
96730 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  is available on 
96740 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72  the host platfor
96750 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  m, the mutex sub
96760 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20  system.** might 
96770 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75  return such a mu
96780 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20  tex in response 
96790 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  to SQLITE_MUTEX_
967a0 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  FAST..**.** The 
967b0 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61  other allowed pa
967c0 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69  rameters to sqli
967d0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
967e0 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a  ) each return.**
967f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
96800 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69  static preexisti
96810 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73  ng mutex.  Six s
96820 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72  tatic mutexes ar
96830 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65  e.** used by the
96840 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e   current version
96850 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74   of SQLite.  Fut
96860 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ure versions of 
96870 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64  SQLite.** may ad
96880 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61  d additional sta
96890 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74  tic mutexes.  St
968a0 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65  atic mutexes are
968b0 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   for internal.**
968c0 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f   use by SQLite o
968d0 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  nly.  Applicatio
968e0 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69  ns that use SQLi
968f0 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c  te mutexes shoul
96900 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68  d.** use only th
96910 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65  e dynamic mutexe
96920 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  s returned by SQ
96930 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
96940 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  or.** SQLITE_MUT
96950 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a  EX_RECURSIVE..**
96960 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66  .** Note that if
96970 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61   one of the dyna
96980 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65  mic mutex parame
96990 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54  ters (SQLITE_MUT
969a0 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51  EX_FAST.** or SQ
969b0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
969c0 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68  SIVE) is used th
969d0 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  en sqlite3_mutex
969e0 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75  _alloc().** retu
969f0 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20  rns a different 
96a00 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63  mutex on every c
96a10 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68  all.  But for th
96a20 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74  e static .** mut
96a30 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61  ex types, the sa
96a40 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75  me mutex is retu
96a50 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61  rned on every ca
96a60 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74  ll that has.** t
96a70 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d  he same type num
96a80 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ber..*/.static s
96a90 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 74  qlite3_mutex *pt
96aa0 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28  hreadMutexAlloc(
96ab0 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 74  int iType){.  st
96ac0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
96ad0 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex staticMutexes
96ae0 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54  [] = {.    SQLIT
96af0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
96b00 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
96b10 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
96b20 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
96b30 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
96b40 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  ER,.    SQLITE3_
96b50 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
96b60 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d  R,.    SQLITE3_M
96b70 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
96b80 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
96b90 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a  TEX_INITIALIZER.
96ba0 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d    };.  sqlite3_m
96bb0 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 74 63  utex *p;.  switc
96bc0 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20  h( iType ){.    
96bd0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  case SQLITE_MUTE
96be0 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20  X_RECURSIVE: {. 
96bf0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33       p = sqlite3
96c00 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
96c10 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20  of(*p) );.      
96c20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65 66 20  if( p ){.#ifdef 
96c30 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e  SQLITE_HOMEGROWN
96c40 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58  _RECURSIVE_MUTEX
96c50 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 72  .        /* If r
96c60 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
96c70 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
96c80 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 76 65  le, we will have
96c90 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62   to.        ** b
96ca0 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53  uild our own.  S
96cb0 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20  ee below. */.   
96cc0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
96cd0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65  ex_init(&p->mute
96ce0 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20  x, 0);.#else.   
96cf0 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 65       /* Use a re
96d00 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 66  cursive mutex if
96d10 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65   it is available
96d20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72   */.        pthr
96d30 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 74 20  ead_mutexattr_t 
96d40 72 65 63 75 72 73 69 76 65 41 74 74 72 3b 0a 20  recursiveAttr;. 
96d50 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96d60 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28 26 72  utexattr_init(&r
96d70 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20  ecursiveAttr);. 
96d80 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
96d90 75 74 65 78 61 74 74 72 5f 73 65 74 74 79 70 65  utexattr_settype
96da0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 2c  (&recursiveAttr,
96db0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 52   PTHREAD_MUTEX_R
96dc0 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20  ECURSIVE);.     
96dd0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
96de0 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c  _init(&p->mutex,
96df0 20 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29   &recursiveAttr)
96e00 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61  ;.        pthrea
96e10 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 73 74  d_mutexattr_dest
96e20 72 6f 79 28 26 72 65 63 75 72 73 69 76 65 41 74  roy(&recursiveAt
96e30 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  tr);.#endif.    
96e40 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
96e50 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
96e60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
96e70 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
96e80 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20  TEX_FAST: {.    
96e90 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
96ea0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
96eb0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
96ec0 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d   p ){.        p-
96ed0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
96ee0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
96ef0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65  ex_init(&p->mute
96f00 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  x, 0);.      }. 
96f10 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
96f20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
96f30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
96f40 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20  Type-2 >= 0 );. 
96f50 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79       assert( iTy
96f60 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65  pe-2 < ArraySize
96f70 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 20  (staticMutexes) 
96f80 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74  );.      p = &st
96f90 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70  aticMutexes[iTyp
96fa0 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69  e-2];.      p->i
96fb0 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20  d = iType;.     
96fc0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
96fd0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
96fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
96ff0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
97000 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a   a previously.**
97010 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78   allocated mutex
97020 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72  .  SQLite is car
97030 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61  eful to dealloca
97040 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65  te every.** mute
97050 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61  x that it alloca
97060 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
97070 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78  oid pthreadMutex
97080 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
97090 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
970a0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
970b0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
970c0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  =SQLITE_MUTEX_FA
970d0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  ST || p->id==SQL
970e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
970f0 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65 61 64  IVE );.  pthread
97100 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 28 26  _mutex_destroy(&
97110 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  p->mutex);.  sql
97120 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a  ite3_free(p);.}.
97130 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
97140 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
97150 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
97160 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
97170 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20  s attempt.** to 
97180 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20  enter a mutex.  
97190 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  If another threa
971a0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74  d is already wit
971b0 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a  hin the mutex,.*
971c0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
971d0 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f  enter() will blo
971e0 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  ck and sqlite3_m
971f0 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20  utex_try() will 
97200 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
97210 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69  _BUSY.  The sqli
97220 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
97230 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
97240 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75  s SQLITE_OK.** u
97250 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
97260 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63  ntry.  Mutexes c
97270 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c  reated using SQL
97280 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
97290 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e  IVE can.** be en
972a0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  tered multiple t
972b0 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65  imes by the same
972c0 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63   thread.  In suc
972d0 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20  h cases the,.** 
972e0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78  mutex must be ex
972f0 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75  ited an equal nu
97300 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65  mber of times be
97310 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72  fore another thr
97320 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72  ead.** can enter
97330 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74  .  If the same t
97340 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65  hread tries to e
97350 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b  nter any other k
97360 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20  ind of mutex.** 
97370 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
97380 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
97390 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  undefined..*/.st
973a0 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61  atic void pthrea
973b0 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69  dMutexEnter(sqli
973c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
973d0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
973e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
973f0 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61  URSIVE || pthrea
97400 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  dMutexNotheld(p)
97410 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
97420 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43  TE_HOMEGROWN_REC
97430 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f  URSIVE_MUTEX.  /
97440 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d  * If recursive m
97450 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61  utexes are not a
97460 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77  vailable, then w
97470 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20  e have to grow. 
97480 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68   ** our own.  Th
97490 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
974a0 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 70  n assumes that p
974b0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20  thread_equal(). 
974c0 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20   ** is atomic - 
974d0 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
974e0 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20  e deceived into 
974f0 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20  thinking self.  
97500 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20  ** and p->owner 
97510 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e  are equal if p->
97520 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65  owner changes be
97530 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73  tween two values
97540 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e  .  ** that are n
97550 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66  ot equal to self
97560 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61   while the compa
97570 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20  rison is taking 
97580 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73  place..  ** This
97590 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
975a0 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63  also assumes a c
975b0 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20  oherent cache - 
975c0 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72  that .  ** separ
975d0 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 63 61  ate processes ca
975e0 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 65 72  nnot read differ
975f0 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20  ent values from 
97600 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64  the same.  ** ad
97610 64 72 65 73 73 20 61 74 20 74 68 65 20 73 61 6d  dress at the sam
97620 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68  e time.  If eith
97630 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20  er of these two 
97640 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20  conditions.  ** 
97650 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65  are not met, the
97660 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 77 69  n the mutexes wi
97670 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62  ll fail and prob
97680 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74  lems will result
97690 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70  ..  */.  {.    p
976a0 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20  thread_t self = 
976b0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a  pthread_self();.
976c0 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e      if( p->nRef>
976d0 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75  0 && pthread_equ
976e0 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c  al(p->owner, sel
976f0 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  f) ){.      p->n
97700 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65  Ref++;.    }else
97710 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61 64 5f  {.      pthread_
97720 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d  mutex_lock(&p->m
97730 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73  utex);.      ass
97740 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
97750 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65  );.      p->owne
97760 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20  r = self;.      
97770 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  p->nRef = 1;.   
97780 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f   }.  }.#else.  /
97790 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d  * Use the built-
977a0 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  in recursive mut
977b0 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65  exes if they are
977c0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f   available..  */
977d0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
977e0 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  _lock(&p->mutex)
977f0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70  ;.  p->owner = p
97800 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20  thread_self();. 
97810 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64   p->nRef++;.#end
97820 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
97830 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d  E_DEBUG.  if( p-
97840 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72  >trace ){.    pr
97850 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65  intf("enter mute
97860 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e  x %p (%d) with n
97870 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d  Ref=%d\n", p, p-
97880 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29  >trace, p->nRef)
97890 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73  ;.  }.#endif.}.s
978a0 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
978b0 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  dMutexTry(sqlite
978c0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
978d0 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
978e0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
978f0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
97900 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f  | pthreadMutexNo
97910 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66  theld(p) );..#if
97920 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47  def SQLITE_HOMEG
97930 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d  ROWN_RECURSIVE_M
97940 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63  UTEX.  /* If rec
97950 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61  ursive mutexes a
97960 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  re not available
97970 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 20 74  , then we have t
97980 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20  o grow.  ** our 
97990 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65  own.  This imple
979a0 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65  mentation assume
979b0 73 20 74 68 61 74 20 70 74 68 72 65 61 64 5f 65  s that pthread_e
979c0 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61  qual().  ** is a
979d0 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20  tomic - that it 
979e0 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76  cannot be deceiv
979f0 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67  ed into thinking
97a00 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70   self.  ** and p
97a10 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61  ->owner are equa
97a20 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68  l if p->owner ch
97a30 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 74 77  anges between tw
97a40 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68  o values.  ** th
97a50 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c  at are not equal
97a60 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74   to self while t
97a70 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
97a80 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20   taking place.. 
97a90 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65   ** This impleme
97aa0 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73  ntation also ass
97ab0 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20  umes a coherent 
97ac0 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20  cache - that .  
97ad0 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63  ** separate proc
97ae0 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61  esses cannot rea
97af0 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75  d different valu
97b00 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  es from the same
97b10 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74  .  ** address at
97b20 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20   the same time. 
97b30 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 68   If either of th
97b40 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f  ese two conditio
97b50 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20  ns.  ** are not 
97b60 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75  met, then the mu
97b70 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20  texes will fail 
97b80 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c  and problems wil
97b90 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20  l result..  */. 
97ba0 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74   {.    pthread_t
97bb0 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f   self = pthread_
97bc0 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20  self();.    if( 
97bd0 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68  p->nRef>0 && pth
97be0 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77  read_equal(p->ow
97bf0 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20  ner, self) ){.  
97c00 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20      p->nRef++;. 
97c10 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
97c20 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  _OK;.    }else i
97c30 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  f( pthread_mutex
97c40 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  _trylock(&p->mut
97c50 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ex)==0 ){.      
97c60 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
97c70 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  =0 );.      p->o
97c80 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20  wner = self;.   
97c90 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a     p->nRef = 1;.
97ca0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
97cb0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  E_OK;.    }else{
97cc0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
97cd0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
97ce0 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73   }.#else.  /* Us
97cf0 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72  e the built-in r
97d00 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
97d10 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61   if they are ava
97d20 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  ilable..  */.  i
97d30 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  f( pthread_mutex
97d40 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  _trylock(&p->mut
97d50 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d  ex)==0 ){.    p-
97d60 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64  >owner = pthread
97d70 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e  _self();.    p->
97d80 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d  nRef++;.    rc =
97d90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
97da0 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  lse{.    rc = SQ
97db0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23  LITE_BUSY;.  }.#
97dc0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
97dd0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28  LITE_DEBUG.  if(
97de0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
97df0 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20  & p->trace ){.  
97e00 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20    printf("enter 
97e10 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69  mutex %p (%d) wi
97e20 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70  th nRef=%d\n", p
97e30 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e  , p->trace, p->n
97e40 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Ref);.  }.#endif
97e50 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
97e60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
97e70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
97e80 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
97e90 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
97ea0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
97eb0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
97ec0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
97ed0 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e  ehavior.** is un
97ee0 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d  defined if the m
97ef0 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72  utex is not curr
97f00 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72  ently entered or
97f10 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
97f20 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ntly allocated. 
97f30 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
97f40 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f  er do either..*/
97f50 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68  .static void pth
97f60 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 28 73  readMutexLeave(s
97f70 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
97f80 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74 68 72  {.  assert( pthr
97f90 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70 29 20  eadMutexHeld(p) 
97fa0 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a  );.  p->nRef--;.
97fb0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
97fc0 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  f==0 || p->id==S
97fd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
97fe0 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65 66  RSIVE );..#ifdef
97ff0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57   SQLITE_HOMEGROW
98000 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45  N_RECURSIVE_MUTE
98010 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d  X.  if( p->nRef=
98020 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61  =0 ){.    pthrea
98030 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
98040 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23  p->mutex);.  }.#
98050 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f 6d  else.  pthread_m
98060 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e  utex_unlock(&p->
98070 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a  mutex);.#endif..
98080 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
98090 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61  BUG.  if( p->tra
980a0 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66  ce ){.    printf
980b0 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25 70  ("leave mutex %p
980c0 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d   (%d) with nRef=
980d0 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61  %d\n", p, p->tra
980e0 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20  ce, p->nRef);.  
980f0 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49  }.#endif.}..SQLI
98100 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
98110 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
98120 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74   *sqlite3Default
98130 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
98140 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
98150 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74  tex_methods sMut
98160 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65  ex = {.    pthre
98170 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20  adMutexInit,.   
98180 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64   pthreadMutexEnd
98190 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74  ,.    pthreadMut
981a0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68  exAlloc,.    pth
981b0 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c 0a 20  readMutexFree,. 
981c0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45     pthreadMutexE
981d0 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72 65 61  nter,.    pthrea
981e0 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 70  dMutexTry,.    p
981f0 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65  threadMutexLeave
98200 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ,.#ifdef SQLITE_
98210 44 45 42 55 47 0a 20 20 20 20 70 74 68 72 65 61  DEBUG.    pthrea
98220 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20  dMutexHeld,.    
98230 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68  pthreadMutexNoth
98240 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c  eld.#else.    0,
98250 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20  .    0.#endif.  
98260 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
98270 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  utex;.}..#endif 
98280 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
98290 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a  PTHREAD */../***
982a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
982b0 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20  of mutex_unix.c 
982c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
982d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
982e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
982f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
98300 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32  n file mutex_w32
98310 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
98320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
98340 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34  * 2007 August 14
98350 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
98360 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
98370 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
98380 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
98390 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
983a0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
983b0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
983c0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
983d0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
983e0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
983f0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
98400 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
98410 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
98420 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
98430 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
98440 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
98450 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
98460 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
98470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
984a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
984b0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
984c0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
984d0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
984e0 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72  ment mutexes for
984f0 20 77 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   win32.*/../*.**
98500 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
98510 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75  s file is only u
98520 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f  sed if we are co
98530 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72  mpiling multithr
98540 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69  eaded.** on a wi
98550 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23  n32 system..*/.#
98560 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  ifdef SQLITE_MUT
98570 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61  EX_W32../*.** Ea
98580 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ch recursive mut
98590 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ex is an instanc
985a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
985b0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
985c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
985d0 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43  mutex {.  CRITIC
985e0 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78  AL_SECTION mutex
985f0 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f  ;    /* Mutex co
98600 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
98610 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20  ck */.  int id; 
98620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98630 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
98640 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
98650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98660 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
98670 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57  terances */.  DW
98680 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 20  ORD owner;      
98690 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65           /* Thre
986a0 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  ad holding this 
986b0 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  mutex */.};../*.
986c0 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 28  ** Return true (
986d0 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20  non-zero) if we 
986e0 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65  are running unde
986f0 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20  r WinNT, Win2K, 
98700 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e  WinXP,.** or Win
98710 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73  CE.  Return fals
98720 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e  e (zero) for Win
98730 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69  95, Win98, or Wi
98740 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20  nME..**.** Here 
98750 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e  is an interestin
98760 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20  g observation:  
98770 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e  Win95, Win98, an
98780 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20  d WinME lack.** 
98790 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
987a0 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 61   API.  But we ca
987b0 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c  n still statical
987c0 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  ly link against 
987d0 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c  that.** API as l
987e0 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20  ong as we don't 
987f0 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e  call it win runn
98800 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e  ing Win95/98/ME.
98810 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74    A call to.** t
98820 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
98830 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
98840 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20   if the host is 
98850 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a  Win95/98/ME or.*
98860 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f  * WinNT/2K/XP so
98870 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e   that we will kn
98880 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ow whether or no
98890 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20  t we can safely 
988a0 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b  call.** the Lock
988b0 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a  FileEx() API..**
988c0 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 20  .** mutexIsNT() 
988d0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  is only used for
988e0 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 69   the TryEnterCri
988f0 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 41  ticalSection() A
98900 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63  PI call,.** whic
98910 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  h is only availa
98920 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 6c  ble if your appl
98930 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70  ication was comp
98940 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57  iled with .** _W
98950 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e  IN32_WINNT defin
98960 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d  ed to a value >=
98970 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 6e   0x0400.  Curren
98980 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a  tly, the only.**
98990 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65   call to TryEnte
989a0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  rCriticalSection
989b0 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 20  () is #ifdef'ed 
989c0 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 0a  out, so #ifdef .
989d0 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 77  ** this out as w
989e0 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69  ell..*/.#if 0.#i
989f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
98a00 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78  E.# define mutex
98a10 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73  IsNT()  (1).#els
98a20 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d  e.  static int m
98a30 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a  utexIsNT(void){.
98a40 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f      static int o
98a50 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69  sType = 0;.    i
98a60 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a  f( osType==0 ){.
98a70 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49        OSVERSIONI
98a80 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20  NFO sInfo;.     
98a90 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69   sInfo.dwOSVersi
98aa0 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a  onInfoSize = siz
98ab0 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20  eof(sInfo);.    
98ac0 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26    GetVersionEx(&
98ad0 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73  sInfo);.      os
98ae0 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50  Type = sInfo.dwP
98af0 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50  latformId==VER_P
98b00 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54  LATFORM_WIN32_NT
98b10 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a   ? 2 : 1;.    }.
98b20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70      return osTyp
98b30 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  e==2;.  }.#endif
98b40 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   /* SQLITE_OS_WI
98b50 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23  NCE */.#endif..#
98b60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
98b70 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  UG./*.** The sql
98b80 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
98b90 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
98ba0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
98bb0 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74  utine are.** int
98bc0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  ended for use on
98bd0 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ly inside assert
98be0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
98bf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
98c00 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65  MutexHeld(sqlite
98c10 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
98c20 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30  eturn p->nRef!=0
98c30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65   && p->owner==Ge
98c40 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
98c50 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ();.}.static int
98c60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
98c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
98c80 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
98c90 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77  nRef==0 || p->ow
98ca0 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 54  ner!=GetCurrentT
98cb0 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e  hreadId();.}.#en
98cc0 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  dif.../*.** Init
98cd0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
98ce0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65  tialize the mute
98cf0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  x subsystem..*/.
98d00 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
98d10 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 74  utex winMutex_st
98d20 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a  aticMutexes[6];.
98d30 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
98d40 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a  tex_isInit = 0;.
98d50 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49 6e  /* As winMutexIn
98d60 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74 65  it() and winMute
98d70 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c 65  xEnd() are calle
98d80 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20  d as part.** of 
98d90 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  the sqlite3_init
98da0 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69 74  ialize and sqlit
98db0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a  e3_shutdown().**
98dc0 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 65   processing, the
98dd0 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20 6d   "interlocked" m
98de0 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c 79  agic is probably
98df0 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c 79   not.** strictly
98e00 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73   necessary..*/.s
98e10 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d 75  tatic long winMu
98e20 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a 73  tex_lock = 0;..s
98e30 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74  tatic int winMut
98e40 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20  exInit(void){ . 
98e50 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 6f   /* The first to
98e60 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31 20   increment to 1 
98e70 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69 74  does actual init
98e80 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20  ialization */.  
98e90 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43  if( InterlockedC
98ea0 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26  ompareExchange(&
98eb0 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31  winMutex_lock, 1
98ec0 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  , 0)==0 ){.    i
98ed0 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
98ee0 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 77  0; i<ArraySize(w
98ef0 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75  inMutex_staticMu
98f00 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20  texes); i++){.  
98f10 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72      InitializeCr
98f20 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77  iticalSection(&w
98f30 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75  inMutex_staticMu
98f40 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b  texes[i].mutex);
98f50 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d 75  .    }.    winMu
98f60 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b 0a  tex_isInit = 1;.
98f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
98f80 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73 20  Someone else is 
98f90 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  in the process o
98fa0 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73 74  f initing the st
98fb0 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f 0a  atic mutexes */.
98fc0 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e 4d      while( !winM
98fd0 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a 20  utex_isInit ){. 
98fe0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20       Sleep(1);. 
98ff0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
99000 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
99010 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
99020 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 0a  utexEnd(void){ .
99030 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
99040 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20 30  o decrement to 0
99050 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68 75   does actual shu
99060 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68 69  tdown .  ** (whi
99070 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ch should be the
99080 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f 77   last to shutdow
99090 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74  n.) */.  if( Int
990a0 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45  erlockedCompareE
990b0 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74 65  xchange(&winMute
990c0 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d 31  x_lock, 0, 1)==1
990d0 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e 4d   ){.    if( winM
990e0 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29  utex_isInit==1 )
990f0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
99100 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
99110 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74  ArraySize(winMut
99120 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
99130 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ); i++){.       
99140 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53   DeleteCriticalS
99150 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78  ection(&winMutex
99160 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69  _staticMutexes[i
99170 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ].mutex);.      
99180 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65 78  }.      winMutex
99190 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  _isInit = 0;.   
991a0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
991b0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f  SQLITE_OK; .}../
991c0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
991d0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
991e0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
991f0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
99200 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
99210 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
99220 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
99230 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
99240 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
99250 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
99260 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ed.  SQLite.** w
99270 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
99280 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
99290 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61  an error.  The a
992a0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71  rgument.** to sq
992b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
992c0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  c() is one of th
992d0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
992e0 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  tants:.**.** <ul
992f0 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  >.** <li>  SQLIT
99300 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
99310 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
99320 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
99330 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
99340 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
99350 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
99360 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
99370 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  M.** <li>  SQLIT
99380 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
99390 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM2.** <li>  SQL
993a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
993b0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _PRNG.** <li>  S
993c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
993d0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_LRU.** <li>  
993e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
993f0 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c  TIC_LRU2.** </ul
99400 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
99410 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
99420 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
99430 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
99440 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
99450 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
99460 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
99470 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
99480 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
99490 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
994a0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
994b0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
994c0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a  X_FAST is used..
994d0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
994e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
994f0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
99500 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
99510 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
99520 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
99530 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
99540 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
99550 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
99560 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77  o.  But SQLite w
99570 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
99580 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
99590 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
995a0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
995b0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
995c0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
995d0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  sive mutex.** im
995e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
995f0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
99600 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20   host platform, 
99610 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
99620 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  tem.** might ret
99630 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
99640 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
99650 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
99660 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68  T..**.** The oth
99670 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
99680 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
99690 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
996a0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
996b0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
996c0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
996d0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74  mutex.  Six stat
996e0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a  ic mutexes are.*
996f0 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
99700 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
99710 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
99720 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
99730 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61  ite.** may add a
99740 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
99750 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69   mutexes.  Stati
99760 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f  c mutexes are fo
99770 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
99780 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79  e by SQLite only
99790 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
997a0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20  that use SQLite 
997b0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a  mutexes should.*
997c0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64  * use only the d
997d0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72  ynamic mutexes r
997e0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54  eturned by SQLIT
997f0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a  E_MUTEX_FAST or.
99800 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
99810 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a  RECURSIVE..**.**
99820 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
99830 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
99840 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
99850 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
99860 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
99870 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
99880 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
99890 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
998a0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
998b0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
998c0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
998d0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73  .  But for the s
998e0 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20  tatic .** mutex 
998f0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
99900 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
99910 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
99920 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
99930 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
99940 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
99950 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75  te3_mutex *winMu
99960 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79  texAlloc(int iTy
99970 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  pe){.  sqlite3_m
99980 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74  utex *p;..  swit
99990 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20  ch( iType ){.   
999a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
999b0 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73  EX_FAST:.    cas
999c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
999d0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
999e0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
999f0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
99a00 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
99a10 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20 20   p ){  .        
99a20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
99a30 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a         Initializ
99a40 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
99a50 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (&p->mutex);.   
99a60 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
99a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
99a80 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
99a90 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69 73  ert( winMutex_is
99aa0 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20  Init==1 );.     
99ab0 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
99ac0 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61   >= 0 );.      a
99ad0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c  ssert( iType-2 <
99ae0 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75   ArraySize(winMu
99af0 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65  tex_staticMutexe
99b00 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  s) );.      p = 
99b10 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  &winMutex_static
99b20 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d  Mutexes[iType-2]
99b30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ;.      p->id = 
99b40 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65  iType;.      bre
99b50 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
99b60 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
99b70 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
99b80 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70   deallocates a p
99b90 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c  reviously.** all
99ba0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53  ocated mutex.  S
99bb0 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c  QLite is careful
99bc0 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65   to deallocate e
99bd0 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68  very.** mutex th
99be0 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
99bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
99c00 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c  winMutexFree(sql
99c10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
99c20 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
99c30 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
99c40 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
99c50 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
99c60 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
99c70 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
99c80 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
99c90 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65  DeleteCriticalSe
99ca0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
99cb0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
99cc0 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
99cd0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
99ce0 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
99cf0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
99d00 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
99d10 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
99d20 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65  utex.  If anothe
99d30 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65  r thread is alre
99d40 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d  ady within the m
99d50 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  utex,.** sqlite3
99d60 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77  _mutex_enter() w
99d70 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71  ill block and sq
99d80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
99d90 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a  ) will return.**
99da0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
99db0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
99dc0 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
99dd0 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
99de0 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65  OK.** upon succe
99df0 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75  ssful entry.  Mu
99e00 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
99e10 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ing SQLITE_MUTEX
99e20 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a  _RECURSIVE can.*
99e30 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  * be entered mul
99e40 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
99e50 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
99e60 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
99e70 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
99e80 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
99e90 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
99ea0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
99eb0 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
99ec0 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
99ed0 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
99ee0 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
99ef0 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
99f00 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  tex.** more than
99f10 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
99f20 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
99f30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
99f40 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73   winMutexEnter(s
99f50 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
99f60 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
99f70 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
99f80 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e  RECURSIVE || win
99f90 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
99fa0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63  );.  EnterCritic
99fb0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
99fc0 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72  tex);.  p->owner
99fd0 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72   = GetCurrentThr
99fe0 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e  eadId(); .  p->n
99ff0 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  Ref++;.}.static 
9a000 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28  int winMutexTry(
9a010 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
9a020 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
9a030 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73  LITE_BUSY;.  ass
9a040 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
9a050 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
9a060 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f  VE || winMutexNo
9a070 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a  theld(p) );.  /*
9a080 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  .  ** The sqlite
9a090 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
9a0a0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61  utine is very ra
9a0b0 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77  rely used, and w
9a0c0 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75  hen it.  ** is u
9a0d0 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79  sed it is merely
9a0e0 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
9a0f0 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66  .  So it is OK f
9a100 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a  or it to always.
9a110 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a    ** fail.  .  *
9a120 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e  *.  ** The TryEn
9a130 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
9a140 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  on() interface i
9a150 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
9a160 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20   on WinNT..  ** 
9a170 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73  And some windows
9a180 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
9a190 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74  ain if you try t
9a1a0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74  o use it without
9a1b0 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e  .  ** first doin
9a1c0 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20  g some #defines 
9a1d0 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c  that prevent SQL
9a1e0 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e  ite from buildin
9a1f0 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a  g on Win98..  **
9a200 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e   For that reason
9a210 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74  , we will omit t
9a220 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
9a230 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20   for now.  See. 
9a240 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35   ** ticket #2685
9a250 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69  ..  */.#if 0.  i
9a260 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26  f( mutexIsNT() &
9a270 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63  & TryEnterCritic
9a280 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
9a290 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f  tex) ){.    p->o
9a2a0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e  wner = GetCurren
9a2b0 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20  tThreadId();.   
9a2c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
9a2d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
9a2e0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53    }.#else.  UNUS
9a2f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b  ED_PARAMETER(p);
9a300 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
9a310 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
9a320 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
9a330 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20  leave() routine 
9a340 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68  exits a mutex th
9a350 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f  at was.** previo
9a360 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20  usly entered by 
9a370 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
9a380 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a    The behavior.*
9a390 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
9a3a0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
9a3b0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
9a3c0 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f  ered or.** is no
9a3d0 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  t currently allo
9a3e0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77  cated.  SQLite w
9a3f0 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74  ill never do eit
9a400 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
9a410 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76  oid winMutexLeav
9a420 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
9a430 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
9a440 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73  ->nRef>0 );.  as
9a450 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d  sert( p->owner==
9a460 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
9a470 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65  Id() );.  p->nRe
9a480 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  f--;.  assert( p
9a490 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
9a4a0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
9a4b0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
9a4c0 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 63  LeaveCriticalSec
9a4d0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
9a4e0 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
9a4f0 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
9a500 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
9a510 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f  3DefaultMutex(vo
9a520 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
9a530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
9a540 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20  ods sMutex = {. 
9a550 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c     winMutexInit,
9a560 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64  .    winMutexEnd
9a570 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c  ,.    winMutexAl
9a580 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  loc,.    winMute
9a590 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75  xFree,.    winMu
9a5a0 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69  texEnter,.    wi
9a5b0 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77  nMutexTry,.    w
9a5c0 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69  inMutexLeave,.#i
9a5d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
9a5e0 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65  G.    winMutexHe
9a5f0 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  ld,.    winMutex
9a600 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20  Notheld.#else.  
9a610 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69    0,.    0.#endi
9a620 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
9a630 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
9a640 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
9a650 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_W32 */../****
9a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
9a670 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a  f mutex_w32.c **
9a680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
9a6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
9a6c0 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a   file malloc.c *
9a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
9a700 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
9a710 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
9a720 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
9a730 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
9a740 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
9a750 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
9a760 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
9a770 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
9a780 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
9a790 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
9a7a0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
9a7b0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
9a7c0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
9a7d0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
9a7e0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
9a7f0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
9a800 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
9a810 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
9a820 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
9a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a870 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61  *.**.** Memory a
9a880 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
9a890 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68  ons used through
9a8a0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2f 0a 0a  out sqlite..*/..
9a8b0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
9a8c0 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65  ne runs when the
9a8d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
9a8e0 72 20 73 65 65 73 20 74 68 61 74 20 74 68 65 0a  r sees that the.
9a8f0 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20  ** total memory 
9a900 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62  allocation is ab
9a910 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 74 68  out to exceed th
9a920 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c  e soft heap.** l
9a930 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  imit..*/.static 
9a940 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c 69 6d  void softHeapLim
9a950 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f  itEnforcer(.  vo
9a960 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20  id *NotUsed, .  
9a970 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f  sqlite3_int64 No
9a980 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c  tUsed2,.  int al
9a990 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55  locSize.){.  UNU
9a9a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
9a9b0 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
9a9c0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c  );.  sqlite3_rel
9a9d0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f  ease_memory(allo
9a9e0 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  cSize);.}../*.**
9a9f0 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68 65   Set the soft he
9aa00 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f  ap-size limit fo
9aa10 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50  r the library. P
9aa20 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72  assing a zero or
9aa30 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61   .** negative va
9aa40 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f  lue indicates no
9aa50 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54   limit..*/.SQLIT
9aa60 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
9aa70 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
9aa80 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c  it(int n){.  sql
9aa90 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d  ite3_uint64 iLim
9aaa0 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67  it;.  int overag
9aab0 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a  e;.  if( n<0 ){.
9aac0 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a      iLimit = 0;.
9aad0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69    }else{.    iLi
9aae0 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 23 69 66  mit = n;.  }.#if
9aaf0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9ab00 5f 41 55 54 4f 49 4e 49 54 0a 20 20 73 71 6c 69  _AUTOINIT.  sqli
9ab10 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
9ab20 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69  ;.#endif.  if( i
9ab30 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73  Limit>0 ){.    s
9ab40 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9ab50 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45  m(softHeapLimitE
9ab60 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d  nforcer, 0, iLim
9ab70 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  it);.  }else{.  
9ab80 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41    sqlite3MemoryA
9ab90 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20  larm(0, 0, 0);. 
9aba0 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28   }.  overage = (
9abb0 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d  int)(sqlite3_mem
9abc0 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36  ory_used() - (i6
9abd0 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72  4)n);.  if( over
9abe0 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  age>0 ){.    sql
9abf0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
9ac00 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20  ory(overage);.  
9ac10 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  }.}../*.** Attem
9ac20 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70  pt to release up
9ac30 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e   to n bytes of n
9ac40 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
9ac50 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  ory currently.**
9ac60 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e   held by SQLite.
9ac70 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e   An example of n
9ac80 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
9ac90 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73  ory is memory us
9aca0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64  ed to.** cache d
9acb0 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68  atabase pages th
9acc0 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65  at are not curre
9acd0 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a  ntly in use..*/.
9ace0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
9acf0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
9ad00 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69  emory(int n){.#i
9ad10 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
9ad20 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
9ad30 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20  MENT.  int nRet 
9ad40 3d 20 30 3b 0a 20 20 6e 52 65 74 20 2b 3d 20 73  = 0;.  nRet += s
9ad50 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
9ad60 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74  aseMemory(n-nRet
9ad70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74  );.  return nRet
9ad80 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  ;.#else.  UNUSED
9ad90 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20  _PARAMETER(n);. 
9ada0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
9adb0 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  K;.#endif.}../*.
9adc0 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61  ** State informa
9add0 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68  tion local to th
9ade0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9adf0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
9ae00 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
9ae10 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47  WSD struct Mem0G
9ae20 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d  lobal {.  /* Num
9ae30 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65  ber of free page
9ae40 73 20 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e  s for scratch an
9ae50 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d  d page-cache mem
9ae60 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63  ory */.  u32 nSc
9ae70 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32  ratchFree;.  u32
9ae80 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73   nPageFree;..  s
9ae90 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
9aea0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  tex;         /* 
9aeb0 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69  Mutex to seriali
9aec0 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20  ze access */..  
9aed0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72  /*.  ** The alar
9aee0 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69  m callback and i
9aef0 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  ts arguments.  T
9af00 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f  he mem0.mutex lo
9af10 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  ck will.  ** be 
9af20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63  held while the c
9af30 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69  allback is runni
9af40 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63  ng.  Recursive c
9af50 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74  alls into.  ** t
9af60 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73  he memory subsys
9af70 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c  tem are allowed,
9af80 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c   but no new call
9af90 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20  backs will be.  
9afa0 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a  ** issued..  */.
9afb0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
9afc0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a  alarmThreshold;.
9afd0 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61    void (*alarmCa
9afe0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73  llback)(void*, s
9aff0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
9b000 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d  );.  void *alarm
9b010 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  Arg;..  /*.  ** 
9b020 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  Pointers to the 
9b030 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c  end of sqlite3Gl
9b040 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
9b050 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c  tch and.  ** sql
9b060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b070 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63  .pPage to a bloc
9b080 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74  k of memory that
9b090 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68   records.  ** wh
9b0a0 69 63 68 20 70 61 67 65 73 20 61 72 65 20 61 76  ich pages are av
9b0b0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ailable..  */.  
9b0c0 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 72 65  u32 *aScratchFre
9b0d0 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46  e;.  u32 *aPageF
9b0e0 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20  ree;.} mem0 = { 
9b0f0 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
9b100 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69  , 0, 0 };..#defi
9b110 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73  ne mem0 GLOBAL(s
9b120 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c  truct Mem0Global
9b130 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49  , mem0)../*.** I
9b140 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
9b150 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9b160 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51  subsystem..*/.SQ
9b170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9b180 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e   sqlite3MallocIn
9b190 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  it(void){.  if( 
9b1a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b1b0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30  fig.m.xMalloc==0
9b1c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
9b1d0 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a  emSetDefault();.
9b1e0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65    }.  memset(&me
9b1f0 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65  m0, 0, sizeof(me
9b200 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  m0));.  if( sqli
9b210 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b220 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20  bCoreMutex ){.  
9b230 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73    mem0.mutex = s
9b240 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
9b250 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
9b260 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20  ATIC_MEM);.  }. 
9b270 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9b280 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9b290 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62  h && sqlite3Glob
9b2a0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
9b2b0 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26  ch>=100.      &&
9b2c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9b2d0 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30  nfig.nScratch>=0
9b2e0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
9b2f0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9b300 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9b310 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71   = ROUNDDOWN8(sq
9b320 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b330 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a  g.szScratch-4);.
9b340 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63      mem0.aScratc
9b350 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28  hFree = (u32*)&(
9b360 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c  (char*)sqlite3Gl
9b370 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
9b380 74 63 68 29 0a 20 20 20 20 20 20 20 20 20 20 20  tch).           
9b390 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47         [sqlite3G
9b3a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
9b3b0 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f  ratch*sqlite3Glo
9b3c0 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74  balConfig.nScrat
9b3d0 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ch];.    for(i=0
9b3e0 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  ; i<sqlite3Globa
9b3f0 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
9b400 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63  ; i++){ mem0.aSc
9b410 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69  ratchFree[i] = i
9b420 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63  ; }.    mem0.nSc
9b430 72 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69  ratchFree = sqli
9b440 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b450 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73  nScratch;.  }els
9b460 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  e{.    sqlite3Gl
9b470 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
9b480 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  tch = 0;.    sql
9b490 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b4a0 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a  .szScratch = 0;.
9b4b0 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
9b4c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50  3GlobalConfig.pP
9b4d0 61 67 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c  age && sqlite3Gl
9b4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9b4f0 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20  e>=512.      && 
9b500 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b510 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a  fig.nPage>=1 ){.
9b520 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
9b530 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20  nt overhead;.   
9b540 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44   int sz = ROUNDD
9b550 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62  OWN8(sqlite3Glob
9b560 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29  alConfig.szPage)
9b570 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
9b580 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b590 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65  g.nPage;.    ove
9b5a0 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73  rhead = (4*n + s
9b5b0 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73  z - 1)/sz;.    s
9b5c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b5d0 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72  ig.nPage -= over
9b5e0 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61  head;.    mem0.a
9b5f0 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a  PageFree = (u32*
9b600 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65  )&((char*)sqlite
9b610 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50  3GlobalConfig.pP
9b620 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20  age).           
9b630 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47         [sqlite3G
9b640 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
9b650 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ge*sqlite3Global
9b660 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20  Config.nPage];. 
9b670 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71     for(i=0; i<sq
9b680 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b690 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d  g.nPage; i++){ m
9b6a0 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d  em0.aPageFree[i]
9b6b0 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30   = i; }.    mem0
9b6c0 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c  .nPageFree = sql
9b6d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b6e0 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b  .nPage;.  }else{
9b6f0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9b700 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d  alConfig.pPage =
9b710 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   0;.    sqlite3G
9b720 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
9b730 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ge = 0;.  }.  re
9b740 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
9b750 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74  alConfig.m.xInit
9b760 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
9b770 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29  nfig.m.pAppData)
9b780 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
9b790 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
9b7a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
9b7b0 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49  bsystem..*/.SQLI
9b7c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9b7d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64  sqlite3MallocEnd
9b7e0 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71  (void){.  if( sq
9b7f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b800 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b  g.m.xShutdown ){
9b810 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9b820 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74  alConfig.m.xShut
9b830 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62  down(sqlite3Glob
9b840 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44  alConfig.m.pAppD
9b850 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  ata);.  }.  mems
9b860 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a  et(&mem0, 0, siz
9b870 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f  eof(mem0));.}../
9b880 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
9b890 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9b8a0 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b   currently check
9b8b0 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ed out..*/.SQLIT
9b8c0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
9b8d0 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
9b8e0 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20  ry_used(void){. 
9b8f0 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71   int n, mx;.  sq
9b900 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b  lite3_int64 res;
9b910 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  .  sqlite3_statu
9b920 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  s(SQLITE_STATUS_
9b930 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c  MEMORY_USED, &n,
9b940 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20   &mx, 0);.  res 
9b950 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
9b960 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  )n;  /* Work aro
9b970 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9b980 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9b990 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9b9a0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  es;.}../*.** Ret
9b9b0 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  urn the maximum 
9b9c0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9b9d0 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62   that has ever b
9b9e0 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f  een.** checked o
9b9f0 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20  ut since either 
9ba00 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
9ba10 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a   this process.**
9ba20 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f   or since the mo
9ba30 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e  st recent reset.
9ba40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
9ba50 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
9ba60 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
9ba70 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46  water(int resetF
9ba80 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d  lag){.  int n, m
9ba90 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  x;.  sqlite3_int
9baa0 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65  64 res;.  sqlite
9bab0 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f  3_status(SQLITE_
9bac0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9bad0 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73  ED, &n, &mx, res
9bae0 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d  etFlag);.  res =
9baf0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
9bb00 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  mx;  /* Work aro
9bb10 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9bb20 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9bb30 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9bb40 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  es;.}../*.** Cha
9bb50 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61  nge the alarm ca
9bb60 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45  llback.*/.SQLITE
9bb70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
9bb80 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
9bb90 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61  .  void(*xCallba
9bba0 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20  ck)(void *pArg, 
9bbb0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73  sqlite3_int64 us
9bbc0 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69  ed,int N),.  voi
9bbd0 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74  d *pArg,.  sqlit
9bbe0 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68  e3_int64 iThresh
9bbf0 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  old.){.  sqlite3
9bc00 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9bc10 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30  0.mutex);.  mem0
9bc20 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d  .alarmCallback =
9bc30 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65   xCallback;.  me
9bc40 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41  m0.alarmArg = pA
9bc50 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  rg;.  mem0.alarm
9bc60 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72  Threshold = iThr
9bc70 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65  eshold;.  sqlite
9bc80 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9bc90 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  m0.mutex);.  ret
9bca0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
9bcb0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
9bcc0 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
9bcd0 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65  ./*.** Deprecate
9bce0 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72  d external inter
9bcf0 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f  face.  Internal/
9bd00 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65  core SQLite code
9bd10 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20  .** should call 
9bd20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61  sqlite3MemoryAla
9bd30 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rm..*/.SQLITE_AP
9bd40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  I int sqlite3_me
9bd50 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f  mory_alarm(.  vo
9bd60 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  id(*xCallback)(v
9bd70 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74  oid *pArg, sqlit
9bd80 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e  e3_int64 used,in
9bd90 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t N),.  void *pA
9bda0 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  rg,.  sqlite3_in
9bdb0 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29  t64 iThreshold.)
9bdc0 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
9bdd0 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43  e3MemoryAlarm(xC
9bde0 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69  allback, pArg, i
9bdf0 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65  Threshold);.}.#e
9be00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67  ndif../*.** Trig
9be10 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a  ger the alarm .*
9be20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
9be30 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
9be40 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76  (int nByte){.  v
9be50 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29  oid (*xCallback)
9be60 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  (void*,sqlite3_i
9be70 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c  nt64,int);.  sql
9be80 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73  ite3_int64 nowUs
9be90 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ed;.  void *pArg
9bea0 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61  ;.  if( mem0.ala
9beb0 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20  rmCallback==0 ) 
9bec0 72 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62  return;.  xCallb
9bed0 61 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d  ack = mem0.alarm
9bee0 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55  Callback;.  nowU
9bef0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61  sed = sqlite3Sta
9bf00 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f  tusValue(SQLITE_
9bf10 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9bf20 45 44 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65  ED);.  pArg = me
9bf30 6d 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d  m0.alarmArg;.  m
9bf40 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9bf50 6b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  k = 0;.  sqlite3
9bf60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
9bf70 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c  0.mutex);.  xCal
9bf80 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55  lback(pArg, nowU
9bf90 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73  sed, nByte);.  s
9bfa0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9bfb0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9bfc0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c    mem0.alarmCall
9bfd0 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
9bfe0 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72  ;.  mem0.alarmAr
9bff0 67 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a  g = pArg;.}../*.
9c000 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61  ** Do a memory a
9c010 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73  llocation with s
9c020 74 61 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c  tatistics and al
9c030 61 72 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68  arms.  Assume th
9c040 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72  e.** lock is alr
9c050 65 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74  eady held..*/.st
9c060 61 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57  atic int mallocW
9c070 69 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20  ithAlarm(int n, 
9c080 76 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e  void **pp){.  in
9c090 74 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20  t nFull;.  void 
9c0a0 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  *p;.  assert( sq
9c0b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
9c0c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem0.mutex) );.
9c0d0 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65    nFull = sqlite
9c0e0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9c0f0 78 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73  xRoundup(n);.  s
9c100 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
9c110 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41  SQLITE_STATUS_MA
9c120 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20  LLOC_SIZE, n);. 
9c130 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43   if( mem0.alarmC
9c140 61 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20  allback!=0 ){.  
9c150 20 20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71    int nUsed = sq
9c160 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65  lite3StatusValue
9c170 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9c180 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20  EMORY_USED);.   
9c190 20 69 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c   if( nUsed+nFull
9c1a0 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68   >= mem0.alarmTh
9c1b0 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20  reshold ){.     
9c1c0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
9c1d0 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20  arm(nFull);.    
9c1e0 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69  }.  }.  p = sqli
9c1f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9c200 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29  m.xMalloc(nFull)
9c210 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20  ;.  if( p==0 && 
9c220 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9c230 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ck ){.    sqlite
9c240 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75  3MallocAlarm(nFu
9c250 6c 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c  ll);.    p = sql
9c260 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9c270 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c  .m.xMalloc(nFull
9c280 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29  );.  }.  if( p )
9c290 7b 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71  {.    nFull = sq
9c2a0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9c2b0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  p);.    sqlite3S
9c2c0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9c2d0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9c2e0 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a  ED, nFull);.  }.
9c2f0 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74    *pp = p;.  ret
9c300 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a  urn nFull;.}../*
9c310 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d  .** Allocate mem
9c320 6f 72 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ory.  This routi
9c330 6e 65 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74  ne is like sqlit
9c340 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65  e3_malloc() exce
9c350 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73  pt that it.** as
9c360 73 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79  sumes the memory
9c370 20 73 75 62 73 79 73 74 65 6d 20 68 61 73 20 61   subsystem has a
9c380 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74  lready been init
9c390 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ialized..*/.SQLI
9c3a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9c3b0 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69  *sqlite3Malloc(i
9c3c0 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
9c3d0 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20  ;.  if( n<=0 || 
9c3e0 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b  n>=0x7fffff00 ){
9c3f0 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79  .    /* A memory
9c400 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61   allocation of a
9c410 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
9c420 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74   which is near t
9c430 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a  he maximum.    *
9c440 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  * signed integer
9c450 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75   value might cau
9c460 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76  se an integer ov
9c470 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66  erflow inside of
9c480 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c   the.    ** xMal
9c490 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65  loc().  Hence we
9c4a0 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d   limit the maxim
9c4b0 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66  um size to 0x7ff
9c4c0 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20  fff00, giving.  
9c4d0 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f    ** 255 bytes o
9c4e0 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c  f overhead.  SQL
9c4f0 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20  ite itself will 
9c500 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69  never use anythi
9c510 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74  ng near.    ** t
9c520 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65  his amount.  The
9c530 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61   only way to rea
9c540 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20  ch the limit is 
9c550 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c  with sqlite3_mal
9c560 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d  loc() */.    p =
9c570 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
9c580 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c590 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
9c5a0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9c5b0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
9c5c0 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57  ex);.    mallocW
9c5d0 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b  ithAlarm(n, &p);
9c5e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
9c5f0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9c600 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tex);.  }else{. 
9c610 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c     p = sqlite3Gl
9c620 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
9c630 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72  lloc(n);.  }.  r
9c640 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
9c650 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
9c660 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
9c670 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75  ocation is for u
9c680 73 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63  se by the applic
9c690 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20  ation..** First 
9c6a0 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65  make sure the me
9c6b0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69  mory subsystem i
9c6c0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74  s initialized, t
9c6d0 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c  hen do the.** al
9c6e0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  location..*/.SQL
9c6f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
9c700 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74  lite3_malloc(int
9c710 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c   n){.#ifndef SQL
9c720 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
9c730 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
9c740 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72  initialize() ) r
9c750 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
9c760 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9c770 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a  Malloc(n);.}../*
9c780 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20  .** Each thread 
9c790 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20  may only have a 
9c7a0 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69  single outstandi
9c7b0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72  ng allocation fr
9c7c0 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61  om.** xScratchMa
9c7d0 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69  lloc().  We veri
9c7e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  fy this constrai
9c7f0 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65  nt in the single
9c800 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73  -threaded.** cas
9c810 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72  e by setting scr
9c820 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20  atchAllocOut to 
9c830 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61  1 when an alloca
9c840 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74  tion.** is outst
9c850 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20  anding clearing 
9c860 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f  it when the allo
9c870 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
9c880 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
9c890 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20  HREADSAFE==0 && 
9c8a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
9c8b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61  .static int scra
9c8c0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b  tchAllocOut = 0;
9c8d0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
9c8e0 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
9c8f0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73  that is to be us
9c900 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20  ed and released 
9c910 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54  right away..** T
9c920 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73  his routine is s
9c930 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61  imilar to alloca
9c940 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73  () in that it is
9c950 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a   not intended.**
9c960 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20   for situations 
9c970 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79  where the memory
9c980 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c   might be held l
9c990 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a  ong-term.  This.
9c9a0 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e  ** routine is in
9c9b0 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65  tended to get me
9c9c0 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67  mory to old larg
9c9d0 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61  e transient data
9c9e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74  .** structures t
9c9f0 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f  hat would not no
9ca00 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68  rmally fit on th
9ca10 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a  e stack of an.**
9ca20 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73   embedded proces
9ca30 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sor..*/.SQLITE_P
9ca40 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
9ca50 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f  ite3ScratchMallo
9ca60 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  c(int n){.  void
9ca70 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e   *p;.  assert( n
9ca80 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54  >0 );..#if SQLIT
9ca90 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20  E_THREADSAFE==0 
9caa0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  && !defined(NDEB
9cab0 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  UG).  /* Verify 
9cac0 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61  that no more tha
9cad0 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c  n one scratch al
9cae0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72  location per thr
9caf0 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73  ead.  ** is outs
9cb00 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74  tanding at one t
9cb10 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f  ime.  (This is o
9cb20 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74  nly checked in t
9cb30 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74  he.  ** single-t
9cb40 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e  hreaded case sin
9cb50 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74  ce checking in t
9cb60 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  he multi-threade
9cb70 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c  d case.  ** woul
9cb80 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63  d be much more c
9cb90 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a  omplicated.) */.
9cba0 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63    assert( scratc
9cbb0 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a  hAllocOut==0 );.
9cbc0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
9cbd0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9cbe0 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b  g.szScratch<n ){
9cbf0 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63  .    goto scratc
9cc00 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65  h_overflow;.  }e
9cc10 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74  lse{  .    sqlit
9cc20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9cc30 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9cc40 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63  if( mem0.nScratc
9cc50 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20  hFree==0 ){.    
9cc60 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9cc70 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
9cc80 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63  );.      goto sc
9cc90 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a  ratch_overflow;.
9cca0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9ccb0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20   int i;.      i 
9ccc0 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46  = mem0.aScratchF
9ccd0 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61  ree[--mem0.nScra
9cce0 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20  tchFree];.      
9ccf0 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  i *= sqlite3Glob
9cd00 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
9cd10 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ch;.      sqlite
9cd20 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9cd30 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
9cd40 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20  _USED, 1);.     
9cd50 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
9cd60 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
9cd70 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29  SCRATCH_SIZE, n)
9cd80 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
9cd90 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9cda0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  .mutex);.      p
9cdb0 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61   = (void*)&((cha
9cdc0 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  r*)sqlite3Global
9cdd0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29  Config.pScratch)
9cde0 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  [i];.      asser
9cdf0 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28  t(  (((u8*)p - (
9ce00 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29  u8*)0) & 7)==0 )
9ce10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20  ;.    }.  }.#if 
9ce20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9ce30 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
9ce40 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74  (NDEBUG).  scrat
9ce50 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d  chAllocOut = p!=
9ce60 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74  0;.#endif..  ret
9ce70 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f  urn p;..scratch_
9ce80 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20  overflow:.  if( 
9ce90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9cea0 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
9ceb0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9cec0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
9ced0 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ex);.    sqlite3
9cee0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
9cef0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
9cf00 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20  SIZE, n);.    n 
9cf10 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72  = mallocWithAlar
9cf20 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66  m(n, &p);.    if
9cf30 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61  ( p ) sqlite3Sta
9cf40 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9cf50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45  ATUS_SCRATCH_OVE
9cf60 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73  RFLOW, n);.    s
9cf70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
9cf80 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
9cf90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d    }else{.    p =
9cfa0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9cfb0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e  nfig.m.xMalloc(n
9cfc0 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54  );.  }.#if SQLIT
9cfd0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20  E_THREADSAFE==0 
9cfe0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  && !defined(NDEB
9cff0 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c  UG).  scratchAll
9d000 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65  ocOut = p!=0;.#e
9d010 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b  ndif.  return p;
9d020 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52      .}.SQLITE_PR
9d030 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
9d040 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f  e3ScratchFree(vo
9d050 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  id *p){.  if( p 
9d060 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  ){..#if SQLITE_T
9d070 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20  HREADSAFE==0 && 
9d080 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
9d090 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74  .    /* Verify t
9d0a0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  hat no more than
9d0b0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c   one scratch all
9d0c0 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65  ocation per thre
9d0d0 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74  ad.    ** is out
9d0e0 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20  standing at one 
9d0f0 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20  time.  (This is 
9d100 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20  only checked in 
9d110 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c  the.    ** singl
9d120 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20  e-threaded case 
9d130 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69  since checking i
9d140 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65  n the multi-thre
9d150 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a  aded case.    **
9d160 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d   would be much m
9d170 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e  ore complicated.
9d180 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  ) */.    assert(
9d190 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
9d1a0 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74  ==1 );.    scrat
9d1b0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a  chAllocOut = 0;.
9d1c0 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
9d1d0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d1e0 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a  fig.pScratch==0.
9d1f0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c             || p<
9d200 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d210 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20  fig.pScratch.   
9d220 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76          || p>=(v
9d230 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74  oid*)mem0.aScrat
9d240 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20  chFree ){.      
9d250 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9d260 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9d270 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
9d280 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d  iSize = sqlite3M
9d290 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20  allocSize(p);.  
9d2a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
9d2b0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9d2c0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73  utex);.        s
9d2d0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
9d2e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9d2f0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20  RATCH_OVERFLOW, 
9d300 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20  -iSize);.       
9d310 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9d320 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9d330 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53  MEMORY_USED, -iS
9d340 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ize);.        sq
9d350 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d360 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
9d370 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
9d380 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9d390 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c  utex);.      }el
9d3a0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
9d3b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9d3c0 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20  m.xFree(p);.    
9d3d0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
9d3e0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
9d3f0 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a    i = (int)((u8*
9d400 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65  )p - (u8*)sqlite
9d410 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
9d420 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69  cratch);.      i
9d430 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   /= sqlite3Globa
9d440 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9d450 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  h;.      assert(
9d460 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74   i>=0 && i<sqlit
9d470 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9d480 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
9d490 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9d4a0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9d4b0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
9d4c0 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
9d4d0 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c  e<(u32)sqlite3Gl
9d4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
9d4f0 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  tch );.      mem
9d500 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d  0.aScratchFree[m
9d510 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
9d520 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73  ++] = i;.      s
9d530 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
9d540 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9d550 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b  RATCH_USED, -1);
9d560 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
9d570 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9d580 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20  mutex);.    }.  
9d590 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20  }.}../*.** TRUE 
9d5a0 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73  if p is a lookas
9d5b0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
9d5c0 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f  ation from db.*/
9d5d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
9d5e0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73  OMIT_LOOKASIDE.s
9d5f0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b  tatic int isLook
9d600 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64  aside(sqlite3 *d
9d610 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72  b, void *p){.  r
9d620 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26  eturn db && p &&
9d630 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   p>=db->lookasid
9d640 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62  e.pStart && p<db
9d650 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
9d660 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ;.}.#else.#defin
9d670 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c  e isLookaside(A,
9d680 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  B) 0.#endif../*.
9d690 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  ** Return the si
9d6a0 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61  ze of a memory a
9d6b0 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f  llocation previo
9d6c0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72  usly obtained fr
9d6d0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c  om.** sqlite3Mal
9d6e0 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33  loc() or sqlite3
9d6f0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51  _malloc()..*/.SQ
9d700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9d710 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
9d720 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72  ze(void *p){.  r
9d730 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
9d740 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a  balConfig.m.xSiz
9d750 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  e(p);.}.SQLITE_P
9d760 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
9d770 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73  e3DbMallocSize(s
9d780 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
9d790 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
9d7a0 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
9d7b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
9d7c0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
9d7d0 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20  isLookaside(db, 
9d7e0 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  p) ){.    return
9d7f0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73   db->lookaside.s
9d800 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  z;.  }else{.    
9d810 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
9d820 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69  obalConfig.m.xSi
9d830 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ze(p);.  }.}../*
9d840 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20  .** Free memory 
9d850 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69  previously obtai
9d860 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
9d870 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c  Malloc()..*/.SQL
9d880 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
9d890 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a  ite3_free(void *
9d8a0 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  p){.  if( p==0 )
9d8b0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
9d8c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d8d0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
9d8e0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9d8f0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9d900 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  x);.    sqlite3S
9d910 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9d920 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9d930 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c  ED, -sqlite3Mall
9d940 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20  ocSize(p));.    
9d950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d960 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
9d970 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9d980 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9d990 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
9d9a0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9d9b0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
9d9c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
9d9d0 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  ree memory that 
9d9e0 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61  might be associa
9d9f0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69  ted with a parti
9da00 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a  cular database.*
9da10 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  * connection..*/
9da20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9da30 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72  void sqlite3DbFr
9da40 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ee(sqlite3 *db, 
9da50 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
9da60 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  rt( db==0 || sql
9da70 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
9da80 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
9da90 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  if( isLookaside(
9daa0 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f  db, p) ){.    Lo
9dab0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75  okasideSlot *pBu
9dac0 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c  f = (LookasideSl
9dad0 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d  ot*)p;.    pBuf-
9dae0 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f  >pNext = db->loo
9daf0 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20  kaside.pFree;.  
9db00 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
9db10 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20  pFree = pBuf;.  
9db20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
9db30 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b  nOut--;.  }else{
9db40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
9db50 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  e(p);.  }.}../*.
9db60 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69  ** Change the si
9db70 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e  ze of an existin
9db80 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  g memory allocat
9db90 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
9dba0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9dbb0 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20  te3Realloc(void 
9dbc0 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65  *pOld, int nByte
9dbd0 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20  s){.  int nOld, 
9dbe0 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e  nNew;.  void *pN
9dbf0 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d  ew;.  if( pOld==
9dc00 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
9dc10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42  sqlite3Malloc(nB
9dc20 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ytes);.  }.  if(
9dc30 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20   nBytes<=0 ){.  
9dc40 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
9dc50 4f 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Old);.    return
9dc60 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42   0;.  }.  if( nB
9dc70 79 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30  ytes>=0x7fffff00
9dc80 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30   ){.    /* The 0
9dc90 78 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74  x7ffff00 limit t
9dca0 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64  erm is explained
9dcb0 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20   in comments on 
9dcc0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20  sqlite3Malloc() 
9dcd0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
9dce0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71  .  }.  nOld = sq
9dcf0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9dd00 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20  pOld);.  nNew = 
9dd10 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9dd20 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e  fig.m.xRoundup(n
9dd30 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f  Bytes);.  if( nO
9dd40 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20  ld==nNew ){.    
9dd50 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d  pNew = pOld;.  }
9dd60 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
9dd70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9dd80 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
9dd90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9dda0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9ddb0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
9ddc0 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
9ddd0 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42  _MALLOC_SIZE, nB
9dde0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73  ytes);.    if( s
9ddf0 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75  qlite3StatusValu
9de00 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e(SQLITE_STATUS_
9de10 4d 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65  MEMORY_USED)+nNe
9de20 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20  w-nOld >= .     
9de30 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54       mem0.alarmT
9de40 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20  hreshold ){.    
9de50 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41    sqlite3MallocA
9de60 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b  larm(nNew-nOld);
9de70 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20  .    }.    pNew 
9de80 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9de90 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63  onfig.m.xRealloc
9dea0 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20  (pOld, nNew);.  
9deb0 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26    if( pNew==0 &&
9dec0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
9ded0 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ack ){.      sql
9dee0 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28  ite3MallocAlarm(
9def0 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70  nBytes);.      p
9df00 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  New = sqlite3Glo
9df10 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61  balConfig.m.xRea
9df20 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29  lloc(pOld, nNew)
9df30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
9df40 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e  pNew ){.      nN
9df50 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ew = sqlite3Mall
9df60 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20  ocSize(pNew);.  
9df70 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
9df80 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
9df90 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20  US_MEMORY_USED, 
9dfa0 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20  nNew-nOld);.    
9dfb0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
9dfc0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9dfd0 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  utex);.  }else{.
9dfe0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
9dff0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9e000 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20  .xRealloc(pOld, 
9e010 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74  nNew);.  }.  ret
9e020 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
9e030 2a 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e  ** The public in
9e040 74 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74  terface to sqlit
9e050 65 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65  e3Realloc.  Make
9e060 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d   sure that the m
9e070 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74  emory.** subsyst
9e080 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  em is initialize
9e090 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
9e0a0 69 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f  ing sqliteReallo
9e0b0 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  c..*/.SQLITE_API
9e0c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72   void *sqlite3_r
9e0d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c  ealloc(void *pOl
9e0e0 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64  d, int n){.#ifnd
9e0f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
9e100 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71  UTOINIT.  if( sq
9e110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
9e120 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  () ) return 0;.#
9e130 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73  endif.  return s
9e140 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f  qlite3Realloc(pO
9e150 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ld, n);.}.../*.*
9e160 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a  * Allocate and z
9e170 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a  ero memory..*/ .
9e180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9e190 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c  oid *sqlite3Mall
9e1a0 6f 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20  ocZero(int n){. 
9e1b0 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74   void *p = sqlit
9e1c0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  e3Malloc(n);.  i
9e1d0 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73  f( p ){.    mems
9e1e0 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  et(p, 0, n);.  }
9e1f0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
9e200 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
9e210 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20  nd zero memory. 
9e220 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   If the allocati
9e230 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a  on fails, make.*
9e240 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c  * the mallocFail
9e250 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63  ed flag in the c
9e260 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65  onnection pointe
9e270 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
9e280 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9e290 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  e3DbMallocZero(s
9e2a0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
9e2b0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  n){.  void *p = 
9e2c0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
9e2d0 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28  aw(db, n);.  if(
9e2e0 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   p ){.    memset
9e2f0 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20  (p, 0, n);.  }. 
9e300 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
9e310 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
9e320 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49   zero memory.  I
9e330 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
9e340 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20   fails, make.** 
9e350 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
9e360 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e   flag in the con
9e370 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e  nection pointer.
9e380 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20  .**.** If db!=0 
9e390 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  and db->mallocFa
9e3a0 69 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e  iled is true (in
9e3b0 64 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72  dicating a prior
9e3c0 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75   malloc.** failu
9e3d0 72 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64  re on the same d
9e3e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
9e3f0 6f 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20  on) then always 
9e400 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e  return 0..** Hen
9e410 63 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ce for a particu
9e420 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
9e430 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61  nection, once ma
9e440 6c 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66  lloc starts.** f
9e450 61 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73  ailing, it fails
9e460 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e   consistently un
9e470 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  til mallocFailed
9e480 20 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68   is reset..** Th
9e490 69 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61  is is an importa
9e4a0 6e 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20  nt assumption.  
9e4b0 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70  There are many p
9e4c0 6c 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20  laces in the.** 
9e4d0 63 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69  code that do thi
9e4e0 6e 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ngs like this:.*
9e4f0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74  *.**         int
9e500 20 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69   *a = (int*)sqli
9e510 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
9e520 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20  b, 100);.**     
9e530 20 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e      int *b = (in
9e540 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  t*)sqlite3DbMall
9e550 6f 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a  ocRaw(db, 200);.
9e560 2a 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62  **         if( b
9e570 20 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a   ) a[10] = 9;.**
9e580 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
9e590 64 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75  ds, if a subsequ
9e5a0 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20  ent malloc (ex: 
9e5b0 22 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20  "b") worked, it 
9e5c0 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68  is assumed.** th
9e5d0 61 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c  at all prior mal
9e5e0 6c 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77  locs (ex: "a") w
9e5f0 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51  orked too..*/.SQ
9e600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9e610 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  d *sqlite3DbMall
9e620 6f 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64  ocRaw(sqlite3 *d
9e630 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  b, int n){.  voi
9e640 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
9e650 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
9e660 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
9e670 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65  mutex) );.#ifnde
9e680 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
9e690 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62  OKASIDE.  if( db
9e6a0 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64   ){.    Lookasid
9e6b0 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20  eSlot *pBuf;.   
9e6c0 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
9e6d0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72  ailed ){.      r
9e6e0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
9e6f0 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61     if( db->looka
9e700 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26  side.bEnabled &&
9e710 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   n<=db->lookasid
9e720 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26  e.sz.         &&
9e730 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f   (pBuf = db->loo
9e740 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30  kaside.pFree)!=0
9e750 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f   ){.      db->lo
9e760 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
9e770 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pBuf->pNext;.   
9e780 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
9e790 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69  .nOut++;.      i
9e7a0 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  f( db->lookaside
9e7b0 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73  .nOut>db->lookas
9e7c0 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20  ide.mxOut ){.   
9e7d0 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
9e7e0 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c  de.mxOut = db->l
9e7f0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20  ookaside.nOut;. 
9e800 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
9e810 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b  urn (void*)pBuf;
9e820 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
9e830 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d  .  if( db && db-
9e840 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
9e850 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
9e860 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20   }.#endif.  p = 
9e870 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29  sqlite3Malloc(n)
9e880 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62  ;.  if( !p && db
9e890 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
9e8a0 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
9e8b0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
9e8c0 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68  ./*.** Resize th
9e8d0 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  e block of memor
9e8e0 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  y pointed to by 
9e8f0 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66  p to n bytes. If
9e900 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66   the.** resize f
9e910 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61  ails, set the ma
9e920 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
9e930 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  in the connectio
9e940 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c  n object..*/.SQL
9e950 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9e960 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c   *sqlite3DbReall
9e970 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  oc(sqlite3 *db, 
9e980 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b  void *p, int n){
9e990 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20  .  void *pNew = 
9e9a0 30 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  0;.  assert( db!
9e9b0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
9e9c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
9e9d0 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
9e9e0 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
9e9f0 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20  cFailed==0 ){.  
9ea00 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20    if( p==0 ){.  
9ea10 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
9ea20 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
9ea30 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , n);.    }.    
9ea40 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  if( isLookaside(
9ea50 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20  db, p) ){.      
9ea60 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61  if( n<=db->looka
9ea70 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20  side.sz ){.     
9ea80 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20     return p;.   
9ea90 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20     }.      pNew 
9eaa0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
9eab0 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20  cRaw(db, n);.   
9eac0 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
9ead0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e         memcpy(pN
9eae0 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61  ew, p, db->looka
9eaf0 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20  side.sz);.      
9eb00 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
9eb10 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a  db, p);.      }.
9eb20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9eb30 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f   pNew = sqlite3_
9eb40 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20  realloc(p, n);. 
9eb50 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29       if( !pNew )
9eb60 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61  {.        db->ma
9eb70 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
9eb80 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
9eb90 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
9eba0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
9ebb0 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20  t to reallocate 
9ebc0 70 2e 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c  p.  If the reall
9ebd0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74  ocation fails, t
9ebe0 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e  hen free p.** an
9ebf0 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63  d set the malloc
9ec00 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74  Failed flag in t
9ec10 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
9ec20 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
9ec30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9ec40 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
9ec50 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a  OrFree(sqlite3 *
9ec60 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74  db, void *p, int
9ec70 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65   n){.  void *pNe
9ec80 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  w;.  pNew = sqli
9ec90 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
9eca0 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70   p, n);.  if( !p
9ecb0 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  New ){.    sqlit
9ecc0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
9ecd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e  .  }.  return pN
9ece0 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  ew;.}../*.** Mak
9ecf0 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74  e a copy of a st
9ed00 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  ring in memory o
9ed10 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
9ed20 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65  iteMalloc(). The
9ed30 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  se .** functions
9ed40 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c   call sqlite3Mal
9ed50 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63 74 6c  locRaw() directl
9ed60 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c  y instead of sql
9ed70 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69  iteMalloc(). Thi
9ed80 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20  s.** is because 
9ed90 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75  when memory debu
9eda0 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  gging is turned 
9edb0 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75  on, these two fu
9edc0 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20  nctions are .** 
9edd0 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f  called via macro
9ede0 73 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68  s that record th
9edf0 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61  e current file a
9ee00 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69  nd line number i
9ee10 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44  n the.** ThreadD
9ee20 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ata structure..*
9ee30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9ee40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
9ee50 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a  StrDup(sqlite3 *
9ee60 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
9ee70 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77  z){.  char *zNew
9ee80 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20  ;.  size_t n;.  
9ee90 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
9eea0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
9eeb0 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
9eec0 6e 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73  n30(z) + 1;.  as
9eed0 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66  sert( (n&0x7ffff
9eee0 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65  fff)==n );.  zNe
9eef0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
9ef00 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29  locRaw(db, (int)
9ef10 6e 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29  n);.  if( zNew )
9ef20 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65  {.    memcpy(zNe
9ef30 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  w, z, n);.  }.  
9ef40 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53  return zNew;.}.S
9ef50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
9ef60 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72  ar *sqlite3DbStr
9ef70 4e 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  NDup(sqlite3 *db
9ef80 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
9ef90 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20   int n){.  char 
9efa0 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d  *zNew;.  if( z==
9efb0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
9efc0 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
9efd0 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d   (n&0x7fffffff)=
9efe0 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73  =n );.  zNew = s
9eff0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
9f000 77 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66  w(db, n+1);.  if
9f010 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65  ( zNew ){.    me
9f020 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29  mcpy(zNew, z, n)
9f030 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20  ;.    zNew[n] = 
9f040 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
9f050 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  zNew;.}../*.** C
9f060 72 65 61 74 65 20 61 20 73 74 72 69 6e 67 20 66  reate a string f
9f070 72 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20  rom the zFromat 
9f080 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65  argument and the
9f090 20 76 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f   va_list that fo
9f0a0 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20  llows..** Store 
9f0b0 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65  the string in me
9f0c0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
9f0d0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
9f0e0 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a  ) and make *pz.*
9f0f0 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  * point to that 
9f100 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  string..*/.SQLIT
9f110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9f120 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
9f130 63 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74  char **pz, sqlit
9f140 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
9f150 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
9f160 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
9f170 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76  .  char *z;..  v
9f180 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
9f190 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69  mat);.  z = sqli
9f1a0 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20  te3VMPrintf(db, 
9f1b0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
9f1c0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71  va_end(ap);.  sq
9f1d0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
9f1e0 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b  *pz);.  *pz = z;
9f1f0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
9f200 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  function must be
9f210 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65   called before e
9f220 78 69 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66  xiting any API f
9f230 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a  unction (i.e. .*
9f240 2a 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74  * returning cont
9f250 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29  rol to the user)
9f260 20 74 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64   that has called
9f270 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20   sqlite3_malloc 
9f280 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  or.** sqlite3_re
9f290 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  alloc..**.** The
9f2a0 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20   returned value 
9f2b0 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f  is normally a co
9f2c0 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  py of the second
9f2d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
9f2e0 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48  s.** function. H
9f2f0 6f 77 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c  owever, if a mal
9f300 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 68 61  loc() failure ha
9f310 73 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65  s occurred since
9f320 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a   the previous.**
9f330 20 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49   invocation SQLI
9f340 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
9f350 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a  rned instead. .*
9f360 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73  *.** If the firs
9f370 74 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20  t argument, db, 
9f380 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20  is not NULL and 
9f390 61 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72  a malloc() error
9f3a0 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a   has occurred,.*
9f3b0 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65  * then the conne
9f3c0 63 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65  ction error-code
9f3d0 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75   (the value retu
9f3e0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
9f3f0 65 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73  errcode()).** is
9f400 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
9f410 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  OMEM..*/.SQLITE_
9f420 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
9f430 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74  te3ApiExit(sqlit
9f440 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b  e3* db, int rc){
9f450 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20  .  /* If the db 
9f460 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55  handle is not NU
9f470 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74  LL, then we must
9f480 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63   hold the connec
9f490 74 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a  tion handle.  **
9f4a0 20 6d 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68   mutex here. Oth
9f4b0 65 72 77 69 73 65 20 74 68 65 20 72 65 61 64 20  erwise the read 
9f4c0 28 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72  (and possible wr
9f4d0 69 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c  ite) of db->mall
9f4e0 6f 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69  ocFailed .  ** i
9f4f0 73 20 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20  s unsafe, as is 
9f500 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
9f510 74 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f  te3Error()..  */
9f520 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c  .  assert( !db |
9f530 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
9f540 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
9f550 29 3b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28  );.  if( db && (
9f560 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9f570 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
9f580 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20  OERR_NOMEM) ){. 
9f590 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
9f5a0 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  db, SQLITE_NOMEM
9f5b0 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61  , 0);.    db->ma
9f5c0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
9f5d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
9f5e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74  NOMEM;.  }.  ret
9f5f0 75 72 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64  urn rc & (db ? d
9f600 62 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66  b->errMask : 0xf
9f610 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  f);.}../********
9f620 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61  ****** End of ma
9f630 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lloc.c *********
9f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f660 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
9f670 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
9f680 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a  e printf.c *****
9f690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f6b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65  *****/./*.** The
9f6c0 20 22 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74   "printf" code t
9f6d0 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65  hat follows date
9f6e0 73 20 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27  s from the 1980'
9f6f0 73 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20  s.  It is in.** 
9f700 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69  the public domai
9f710 6e 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  n.  The original
9f720 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e   comments are in
9f730 63 6c 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a  cluded here for.
9f740 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e  ** completeness.
9f750 20 20 54 68 65 79 20 61 72 65 20 76 65 72 79 20    They are very 
9f760 6f 75 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20  out-of-date but 
9f770 6d 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20  might be useful 
9f780 61 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69  as.** an histori
9f790 63 61 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20  cal reference.  
9f7a0 4d 6f 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68  Most of the "enh
9f7b0 61 6e 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20  ancements" have 
9f7c0 62 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f  been backed.** o
9f7d0 75 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 66  ut so that the f
9f7e0 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20  unctionality is 
9f7f0 6e 6f 77 20 74 68 65 20 73 61 6d 65 20 61 73 20  now the same as 
9f800 73 74 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28  standard printf(
9f810 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  )..**.**********
9f820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
9f870 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20  wing modules is 
9f880 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c  an enhanced repl
9f890 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20  acement for the 
9f8a0 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74  "printf" subrout
9f8b0 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e  ines.** found in
9f8c0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
9f8d0 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f  library.  The fo
9f8e0 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d  llowing enhancem
9f8f0 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70  ents are.** supp
9f900 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  orted:.**.**    
9f910 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20    +  Additional 
9f920 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20  functions.  The 
9f930 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20  standard set of 
9f940 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f  "printf" functio
9f950 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e  ns.**         in
9f960 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66  cludes printf, f
9f970 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c  printf, sprintf,
9f980 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e   vprintf, vfprin
9f990 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  tf, and.**      
9f9a0 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68     vsprintf.  Th
9f9b0 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74  is module adds t
9f9c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
9f9d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
9f9e0 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72   snprintf -- Wor
9f9f0 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c  ks like sprintf,
9fa00 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72   but has an extr
9fa10 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20  a argument.**   
9fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fa30 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20         which is 
9fa40 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
9fa50 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74  buffer written t
9fa60 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  o..**.**        
9fa70 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d     *  mprintf --
9fa80 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72    Similar to spr
9fa90 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75  intf.  Writes ou
9faa0 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a  tput to memory.*
9fab0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
9fac0 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69             obtai
9fad0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  ned from malloc.
9fae0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9faf0 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20   *  xprintf --  
9fb00 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e  Calls a function
9fb10 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f   to dispose of o
9fb20 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  utput..**.**    
9fb30 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74         *  nprint
9fb40 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c  f --  No output,
9fb50 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65   but returns the
9fb60 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
9fb70 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  cters.**        
9fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fb90 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76    that would hav
9fba0 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79  e been output by
9fbb0 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20   printf..**.**  
9fbc0 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d           *  A v-
9fbd0 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73   version (ex: vs
9fbe0 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72  nprintf) of ever
9fbf0 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c  y function is al
9fc00 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  so.**           
9fc10 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a     supplied..**.
9fc20 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77  **      +  A few
9fc30 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74   extensions to t
9fc40 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f  he formatting no
9fc50 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f  tation are suppo
9fc60 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rted:.**.**     
9fc70 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22        *  The "="
9fc80 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74   flag (similar t
9fc90 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68  o "-") causes th
9fca0 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a  e output to be.*
9fcb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  *              b
9fcc0 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68  e centered in th
9fcd0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20  e appropriately 
9fce0 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a  sized field..**.
9fcf0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
9fd00 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74  The %b field out
9fd10 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20  puts an integer 
9fd20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69  in binary notati
9fd30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  on..**.**       
9fd40 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69      *  The %c fi
9fd50 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20  eld now accepts 
9fd60 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68  a precision.  Th
9fd70 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70  e character outp
9fd80 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
9fd90 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62     is repeated b
9fda0 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
9fdb0 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73  times the precis
9fdc0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a  ion specifies..*
9fdd0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9fde0 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77    The %' field w
9fdf0 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75  orks like %c, bu
9fe00 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63  t takes as its c
9fe10 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20  haracter the.** 
9fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78               nex
9fe30 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74  t character of t
9fe40 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
9fe50 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  , instead of the
9fe60 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20   next.**        
9fe70 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20        argument. 
9fe80 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70   For example,  p
9fe90 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20  rintf("%.78'-") 
9fea0 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73   prints 78 minus
9feb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fec0 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65   signs, the same
9fed0 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37   as  printf("%.7
9fee0 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20  8c",'-')..**.** 
9fef0 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d       +  When com
9ff00 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20  piled using GCC 
9ff10 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73  on a SPARC, this
9ff20 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e   version of prin
9ff30 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  tf is.**        
9ff40 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65   faster than the
9ff50 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20   library printf 
9ff60 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a  for SUN OS 4.1..
9ff70 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c  **.**      +  Al
9ff80 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  l functions are 
9ff90 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e  fully reentrant.
9ffa0 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .**.*/../*.** Co
9ffb0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66  nversion types f
9ffc0 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73  all into various
9ffd0 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64   categories as d
9ffe0 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  efined by the.**
9fff0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65   following enume
a0000 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  ration..*/.#defi
a0010 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20  ne etRADIX      
a0020 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79   1 /* Integer ty
a0030 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f  pes.  %d, %x, %o
a0040 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a  , and so forth *
a0050 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41  /.#define etFLOA
a0060 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f  T       2 /* Flo
a0070 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66  ating point.  %f
a0080 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58   */.#define etEX
a0090 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45  P         3 /* E
a00a0 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74  xponentional not
a00b0 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45  ation. %e and %E
a00c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45   */.#define etGE
a00d0 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46  NERIC     4 /* F
a00e0 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e  loating or expon
a00f0 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e  ential, dependin
a0100 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25  g on exponent. %
a0110 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  g */.#define etS
a0120 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20  IZE        5 /* 
a0130 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66  Return number of
a0140 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63   characters proc
a0150 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e  essed so far. %n
a0160 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54   */.#define etST
a0170 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53  RING      6 /* S
a0180 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64  trings. %s */.#d
a0190 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e  efine etDYNSTRIN
a01a0 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63  G   7 /* Dynamic
a01b0 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
a01c0 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64  trings. %z */.#d
a01d0 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20  efine etPERCENT 
a01e0 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74      8 /* Percent
a01f0 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23   symbol. %% */.#
a0200 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20  define etCHARX  
a0210 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63       9 /* Charac
a0220 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54  ters. %c */./* T
a0230 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65  he rest are exte
a0240 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d  nsions, not norm
a0250 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72  ally found in pr
a0260 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e  intf() */.#defin
a0270 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31  e etSQLESCAPE  1
a0280 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  0 /* Strings wit
a0290 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20  h '\'' doubled. 
a02a0 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %q */.#define e
a02b0 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f  tSQLESCAPE2 11 /
a02c0 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27  * Strings with '
a02d0 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20  \'' doubled and 
a02e0 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a  enclosed in '',.
a02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0300 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70            NULL p
a0310 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64  ointers replaced
a0320 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25   by SQL NULL.  %
a0330 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54  Q */.#define etT
a0340 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20  OKEN      12 /* 
a0350 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
a0360 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a  oken structure *
a0370 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c  /.#define etSRCL
a0380 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70  IST    13 /* a p
a0390 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c  ointer to a SrcL
a03a0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ist */.#define e
a03b0 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f  tPOINTER    14 /
a03c0 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73  * The %p convers
a03d0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  ion */.#define e
a03e0 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f  tSQLESCAPE3 15 /
a03f0 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20  * %w -> Strings 
a0400 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65  with '\"' double
a0410 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f  d */.#define etO
a0420 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20  RDINAL    16 /* 
a0430 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20  %r -> 1st, 2nd, 
a0440 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20  3rd, 4th, etc.  
a0450 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a  English only */.
a0460 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c  .#define etINVAL
a0470 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20  ID     0 /* Any 
a0480 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e  unrecognized con
a0490 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a  version type */.
a04a0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79  ../*.** An "etBy
a04b0 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20  te" is an 8-bit 
a04c0 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a  unsigned value..
a04d0 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67  */.typedef unsig
a04e0 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b  ned char etByte;
a04f0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69  ../*.** Each bui
a0500 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ltin conversion 
a0510 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74  character (ex: t
a0520 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20  he 'd' in "%d") 
a0530 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  is described.** 
a0540 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
a0550 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
a0560 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70  structure.*/.typ
a0570 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69  edef struct et_i
a0580 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72  nfo {   /* Infor
a0590 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63  mation about eac
a05a0 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a  h format field *
a05b0 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65  /.  char fmttype
a05c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a05d0 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64  The format field
a05e0 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a   code letter */.
a05f0 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20    etByte base;  
a0600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
a0610 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78  e base for radix
a0620 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20   conversion */. 
a0630 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20   etByte flags;  
a0640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
a0650 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47   or more of FLAG
a0660 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f  _ constants belo
a0670 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79  w */.  etByte ty
a0680 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pe;             
a0690 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61  /* Conversion pa
a06a0 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79  radigm */.  etBy
a06b0 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20  te charset;     
a06c0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
a06d0 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66  nto aDigits[] of
a06e0 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69   the digits stri
a06f0 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70  ng */.  etByte p
a0700 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
a0710 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
a0720 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65  aPrefix[] of the
a0730 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a   prefix string *
a0740 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a  /.} et_info;../*
a0750 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
a0760 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66  es for et_info.f
a0770 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lags.*/.#define 
a0780 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20  FLAG_SIGNED  1  
a0790 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
a07a0 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65  e value to conve
a07b0 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a  rt is signed */.
a07c0 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54  #define FLAG_INT
a07d0 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72  ERN  2     /* Tr
a07e0 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e  ue if for intern
a07f0 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23  al use only */.#
a0800 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49  define FLAG_STRI
a0810 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c  NG  4     /* All
a0820 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63  ow infinity prec
a0830 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ision */.../*.**
a0840 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
a0850 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64  able is searched
a0860 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74   linearly, so it
a0870 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20   is good to put 
a0880 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71  the.** most freq
a0890 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76  uently used conv
a08a0 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72  ersion types fir
a08b0 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  st..*/.static co
a08c0 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73  nst char aDigits
a08d0 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39  [] = "0123456789
a08e0 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39  ABCDEF0123456789
a08f0 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20  abcdef";.static 
a0900 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66  const char aPref
a0910 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58  ix[] = "-x0\000X
a0920 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  0";.static const
a0930 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f   et_info fmtinfo
a0940 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c  [] = {.  {  'd',
a0950 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c   10, 1, etRADIX,
a0960 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
a0970 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20   {  's',  0, 4, 
a0980 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c  etSTRING,     0,
a0990 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c    0 },.  {  'g',
a09a0 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49    0, 1, etGENERI
a09b0 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20  C,    30, 0 },. 
a09c0 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20   {  'z',  0, 4, 
a09d0 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c  etDYNSTRING,  0,
a09e0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c    0 },.  {  'q',
a09f0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
a0a00 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20  APE,  0,  0 },. 
a0a10 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20   {  'Q',  0, 4, 
a0a20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c  etSQLESCAPE2, 0,
a0a30 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c    0 },.  {  'w',
a0a40 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43    0, 4, etSQLESC
a0a50 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20  APE3, 0,  0 },. 
a0a60 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20   {  'c',  0, 0, 
a0a70 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c  etCHARX,      0,
a0a80 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c    0 },.  {  'o',
a0a90 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c    8, 0, etRADIX,
a0aa0 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20        0,  2 },. 
a0ab0 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20   {  'u', 10, 0, 
a0ac0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a0ad0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c    0 },.  {  'x',
a0ae0 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c   16, 0, etRADIX,
a0af0 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20        16, 1 },. 
a0b00 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20   {  'X', 16, 0, 
a0b10 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a0b20 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53    4 },.#ifndef S
a0b30 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a0b40 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27  ING_POINT.  {  '
a0b50 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f  f',  0, 1, etFLO
a0b60 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  AT,      0,  0 }
a0b70 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20  ,.  {  'e',  0, 
a0b80 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20  1, etEXP,       
a0b90 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27   30, 0 },.  {  '
a0ba0 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50  E',  0, 1, etEXP
a0bb0 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d  ,        14, 0 }
a0bc0 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20  ,.  {  'G',  0, 
a0bd0 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20  1, etGENERIC,   
a0be0 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66   14, 0 },.#endif
a0bf0 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31  .  {  'i', 10, 1
a0c00 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a0c10 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e  0,  0 },.  {  'n
a0c20 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45  ',  0, 0, etSIZE
a0c30 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  ,       0,  0 },
a0c40 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30  .  {  '%',  0, 0
a0c50 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20  , etPERCENT,    
a0c60 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70  0,  0 },.  {  'p
a0c70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e  ', 16, 0, etPOIN
a0c80 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c  TER,    0,  1 },
a0c90 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73  ../* All the res
a0ca0 74 20 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f  t have the FLAG_
a0cb0 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74 20 61  INTERN bit set a
a0cc0 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f 72 20  nd are thus for 
a0cd0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20  internal.** use 
a0ce0 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27  only */.  {  'T'
a0cf0 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e  ,  0, 2, etTOKEN
a0d00 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ,      0,  0 },.
a0d10 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c    {  'S',  0, 2,
a0d20 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30   etSRCLIST,    0
a0d30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27  ,  0 },.  {  'r'
a0d40 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e  , 10, 3, etORDIN
a0d50 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  AL,    0,  0 },.
a0d60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c  };../*.** If SQL
a0d70 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a0d80 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e  G_POINT is defin
a0d90 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  ed, then none of
a0da0 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f   the floating po
a0db0 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  int.** conversio
a0dc0 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f  ns will work..*/
a0dd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a0de0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
a0df0 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22  INT./*.** "*val"
a0e00 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63   is a double suc
a0e10 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76  h that 0.1 <= *v
a0e20 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74  al < 10.0.** Ret
a0e30 75 72 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f  urn the ascii co
a0e40 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69  de for the leadi
a0e50 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c  ng digit of *val
a0e60 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70  , then.** multip
a0e70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e  ly "*val" by 10.
a0e80 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65  0 to renormalize
a0e90 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a  ..**.** Example:
a0ea0 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20  .**     input:  
a0eb0 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35     *val = 3.1415
a0ec0 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a  9.**     output:
a0ed0 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35      *val = 1.415
a0ee0 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65  9    function re
a0ef0 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a  turn = '3'.**.**
a0f00 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e   The counter *cn
a0f10 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
a0f20 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74   each time.  Aft
a0f30 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65  er counter excee
a0f40 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75  ds.** 16 (the nu
a0f50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63  mber of signific
a0f60 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20 61 20  ant digits in a 
a0f70 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30  64-bit float) '0
a0f80 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72  ' is.** always r
a0f90 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
a0fa0 69 63 20 63 68 61 72 20 65 74 5f 67 65 74 64 69  ic char et_getdi
a0fb0 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  git(LONGDOUBLE_T
a0fc0 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63  YPE *val, int *c
a0fd0 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74  nt){.  int digit
a0fe0 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54  ;.  LONGDOUBLE_T
a0ff0 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63  YPE d;.  if( (*c
a1000 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65  nt)++ >= 16 ) re
a1010 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69  turn '0';.  digi
a1020 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20  t = (int)*val;. 
a1030 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69   d = digit;.  di
a1040 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76  git += '0';.  *v
a1050 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a  al = (*val - d)*
a1060 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28  10.0;.  return (
a1070 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65  char)digit;.}.#e
a1080 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
a1090 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
a10a0 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70  NT */../*.** App
a10b0 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68 61 72  end N space char
a10c0 61 63 74 65 72 73 20 74 6f 20 74 68 65 20 67 69  acters to the gi
a10d0 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65  ven string buffe
a10e0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
a10f0 64 20 61 70 70 65 6e 64 53 70 61 63 65 28 53 74  d appendSpace(St
a1100 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20  rAccum *pAccum, 
a1110 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63  int N){.  static
a1120 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61   const char zSpa
a1130 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20  ces[] = "       
a1140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1150 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65        ";.  while
a1160 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66  ( N>=(int)sizeof
a1170 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20  (zSpaces)-1 ){. 
a1180 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a1190 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a11a0 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66   zSpaces, sizeof
a11b0 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20  (zSpaces)-1);.  
a11c0 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53    N -= sizeof(zS
a11d0 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20  paces)-1;.  }.  
a11e0 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73  if( N>0 ){.    s
a11f0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a1200 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70  pend(pAccum, zSp
a1210 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a  aces, N);.  }.}.
a1220 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e  ./*.** On machin
a1230 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20  es with a small 
a1240 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20  stack size, you 
a1250 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65  can redefine the
a1260 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  .** SQLITE_PRINT
a1270 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20  _BUF_SIZE to be 
a1280 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e 0a 2a  less than 350..*
a1290 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
a12a0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a  _PRINT_BUF_SIZE.
a12b0 23 20 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  # if defined(SQL
a12c0 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29  ITE_SMALL_STACK)
a12d0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
a12e0 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
a12f0 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20 20 20  E 50.# else.#   
a1300 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52  define SQLITE_PR
a1310 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 30  INT_BUF_SIZE 350
a1320 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
a1330 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a  #define etBUFSIZ
a1340 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  E SQLITE_PRINT_B
a1350 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65  UF_SIZE  /* Size
a1360 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   of the output b
a1370 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  uffer */../*.** 
a1380 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d  The root program
a1390 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e  .  All variation
a13a0 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65  s call this core
a13b0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a  ..**.** INPUTS:.
a13c0 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69 73  **   func   This
a13d0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
a13e0 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69   a function taki
a13f0 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  ng three argumen
a1400 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ts.**           
a1410 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f   1. A pointer to
a1420 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65   anything.  Same
a1430 20 61 73 20 74 68 65 20 22 61 72 67 22 20 70 61   as the "arg" pa
a1440 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20  rameter..**     
a1450 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e         2. A poin
a1460 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ter to the list 
a1470 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f  of characters to
a1480 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20   be output.**   
a1490 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74              (Not
a14a0 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73 20  e, this list is 
a14b0 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61  NOT null termina
a14c0 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20  ted.).**        
a14d0 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65      3. An intege
a14e0 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  r number of char
a14f0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74  acters to be out
a1500 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  put..**         
a1510 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69        (Note: Thi
a1520 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62  s number might b
a1530 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20  e zero.).**.**  
a1540 20 61 72 67 20 20 20 20 54 68 69 73 20 69 73 20   arg    This is 
a1550 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  the pointer to a
a1560 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 77 69  nything which wi
a1570 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 73 20  ll be passed as 
a1580 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
a1590 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
a15a0 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69  o "func".  Use i
a15b0 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 20 79  t for whatever y
a15c0 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20  ou like..**.**  
a15d0 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 73 20   fmt    This is 
a15e0 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  the format strin
a15f0 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73 75  g, as in the usu
a1600 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20  al print..**.** 
a1610 20 20 61 70 20 20 20 20 20 54 68 69 73 20 69 73    ap     This is
a1620 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
a1630 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74  list of argument
a1640 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a  s.  Same as in.*
a1650 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 72 69  *          vfpri
a1660 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54  nt..**.** OUTPUT
a1670 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54  S:.**          T
a1680 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
a1690 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
a16a0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
a16b0 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20  s sent to.**    
a16c0 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 74 69        the functi
a16d0 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75  on "func".  Retu
a16e0 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f  rns -1 on a erro
a16f0 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  r..**.** Note th
a1700 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20  at the order in 
a1710 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20  which automatic 
a1720 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 64 65  variables are de
a1730 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20  clared below.** 
a1740 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20  seems to make a 
a1750 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 20 69  big difference i
a1760 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f  n determining ho
a1770 77 20 66 61 73 74 20 74 68 69 73 20 62 65 61 73  w fast this beas
a1780 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a  t.** will run..*
a1790 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a17a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50   void sqlite3VXP
a17b0 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75  rintf(.  StrAccu
a17c0 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20  m *pAccum,      
a17d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
a17e0 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74  ccumulate result
a17f0 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  s here */.  int 
a1800 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20  useExtended,    
a1810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a1820 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64  * Allow extended
a1830 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a   %-conversions *
a1840 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
a1850 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  fmt,            
a1860 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74         /* Format
a1870 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f   string */.  va_
a1880 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 20 20  list ap         
a1890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a18a0 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  /* arguments */.
a18b0 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20  ){.  int c;     
a18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a18d0 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74 65  /* Next characte
a18e0 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20  r in the format 
a18f0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  string */.  char
a1900 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20 20   *bufpt;        
a1910 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
a1920 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73  r to the convers
a1930 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  ion buffer */.  
a1940 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20  int precision;  
a1950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
a1960 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63  ecision of the c
a1970 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a  urrent field */.
a1980 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20    int length;   
a1990 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a19a0 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69  Length of the fi
a19b0 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  eld */.  int idx
a19c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a19d0 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c      /* A general
a19e0 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f   purpose loop co
a19f0 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77  unter */.  int w
a1a00 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  idth;           
a1a10 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f        /* Width o
a1a20 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69  f the current fi
a1a30 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  eld */.  etByte 
a1a40 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a1a50 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22  ;   /* True if "
a1a60 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  -" flag is prese
a1a70 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
a1a80 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20  lag_plussign;   
a1a90 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b     /* True if "+
a1aa0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a1ab0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a1ac0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20  ag_blanksign;   
a1ad0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22    /* True if " "
a1ae0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1af0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1b00 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b  g_alternateform;
a1b10 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22 20   /* True if "#" 
a1b20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
a1b30 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1b40 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20  _altform2;      
a1b50 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 20 66  /* True if "!" f
a1b60 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a  lag is present *
a1b70 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a1b80 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f  zeropad;       /
a1b90 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c 64 20  * True if field 
a1ba0 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73  width constant s
a1bb0 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20  tarts with zero 
a1bc0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1bd0 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20  _long;          
a1be0 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20 66  /* True if "l" f
a1bf0 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a  lag is present *
a1c00 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a1c10 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f  longlong;      /
a1c20 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 22 6c  * True if the "l
a1c30 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  l" flag is prese
a1c40 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64  nt */.  etByte d
a1c50 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  one;            
a1c60 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69     /* Loop termi
a1c70 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20  nation flag */. 
a1c80 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c   sqlite_uint64 l
a1c90 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56  ongvalue;   /* V
a1ca0 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72  alue for integer
a1cb0 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47   types */.  LONG
a1cc0 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c  DOUBLE_TYPE real
a1cd0 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20  value; /* Value 
a1ce0 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a  for real types *
a1cf0 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66  /.  const et_inf
a1d00 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f  o *infop;      /
a1d10 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
a1d20 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66   appropriate inf
a1d30 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  o structure */. 
a1d40 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 46 53   char buf[etBUFS
a1d50 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43  IZE];       /* C
a1d60 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72  onversion buffer
a1d70 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69   */.  char prefi
a1d80 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
a1d90 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72 61   /* Prefix chara
a1da0 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d  cter.  "+" or "-
a1db0 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27  " or " " or '\0'
a1dc0 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74  . */.  etByte xt
a1dd0 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ype = 0;        
a1de0 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20    /* Conversion 
a1df0 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68  paradigm */.  ch
a1e00 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20  ar *zExtra;     
a1e10 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
a1e20 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f  a memory used fo
a1e30 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f  r etTCLESCAPE co
a1e40 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66  nversions */.#if
a1e50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a1e60 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
a1e70 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20    int  exp, e2; 
a1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1e90 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c  exponent of real
a1ea0 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f   numbers */.  do
a1eb0 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20  uble rounder;   
a1ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
a1ed0 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c   for rounding fl
a1ee0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
a1ef0 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ues */.  etByte 
a1f00 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20  flag_dp;        
a1f10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64      /* True if d
a1f20 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f  ecimal point sho
a1f30 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a  uld be shown */.
a1f40 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74    etByte flag_rt
a1f50 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  z;           /* 
a1f60 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67  True if trailing
a1f70 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65   zeros should be
a1f80 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74   removed */.  et
a1f90 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20  Byte flag_exp;  
a1fa0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a1fb0 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61   to force displa
a1fc0 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e  y of the exponen
a1fd0 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20  t */.  int nsd; 
a1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1ff0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
a2000 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
a2010 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65  s returned */.#e
a2020 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d  ndif..  length =
a2030 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b   0;.  bufpt = 0;
a2040 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d  .  for(; (c=(*fm
a2050 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a  t))!=0; ++fmt){.
a2060 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 20 29      if( c!='%' )
a2070 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b  {.      int amt;
a2080 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 28  .      bufpt = (
a2090 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20  char *)fmt;.    
a20a0 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20    amt = 1;.     
a20b0 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66   while( (c=(*++f
a20c0 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d  mt))!='%' && c!=
a20d0 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20  0 ) amt++;.     
a20e0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a20f0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62  Append(pAccum, b
a2100 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20  ufpt, amt);.    
a2110 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65    if( c==0 ) bre
a2120 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
a2130 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d  ( (c=(*++fmt))==
a2140 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
a2150 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
a2160 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29  (pAccum, "%", 1)
a2170 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
a2180 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64     }.    /* Find
a2190 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 20   out what flags 
a21a0 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20  are present */. 
a21b0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a21c0 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73  ify = flag_pluss
a21d0 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b  ign = flag_blank
a21e0 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61  sign = .     fla
a21f0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
a2200 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20  = flag_altform2 
a2210 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d  = flag_zeropad =
a2220 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30   0;.    done = 0
a2230 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  ;.    do{.      
a2240 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20  switch( c ){.   
a2250 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20       case '-':  
a2260 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66   flag_leftjustif
a2270 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b  y = 1;     break
a2280 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
a2290 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73  +':   flag_pluss
a22a0 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20  ign = 1;        
a22b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
a22c0 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f  ase ' ':   flag_
a22d0 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20  blanksign = 1;  
a22e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a22f0 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 20 20      case '#':   
a2300 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a2310 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b  rm = 1;   break;
a2320 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 21  .        case '!
a2330 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72  ':   flag_altfor
a2340 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62  m2 = 1;        b
a2350 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
a2360 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a  se '0':   flag_z
a2370 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20  eropad = 1;     
a2380 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a2390 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64     default:    d
a23a0 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20  one = 1;        
a23b0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
a23c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69        }.    }whi
a23d0 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d  le( !done && (c=
a23e0 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a  (*++fmt))!=0 );.
a23f0 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66      /* Get the f
a2400 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20  ield width */.  
a2410 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20    width = 0;.   
a2420 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20   if( c=='*' ){. 
a2430 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f       width = va_
a2440 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20  arg(ap,int);.   
a2450 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 20 29     if( width<0 )
a2460 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  {.        flag_l
a2470 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a  eftjustify = 1;.
a2480 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20          width = 
a2490 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a  -width;.      }.
a24a0 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a24b0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
a24c0 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27     while( c>='0'
a24d0 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20   && c<='9' ){.  
a24e0 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 77 69        width = wi
a24f0 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27  dth*10 + c - '0'
a2500 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
a2510 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  +fmt;.      }.  
a2520 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74    }.    if( widt
a2530 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30  h > etBUFSIZE-10
a2540 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20   ){.      width 
a2550 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a  = etBUFSIZE-10;.
a2560 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74      }.    /* Get
a2570 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a   the precision *
a2580 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27  /.    if( c=='.'
a2590 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73   ){.      precis
a25a0 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63  ion = 0;.      c
a25b0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a25c0 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20   if( c=='*' ){. 
a25d0 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e         precision
a25e0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
a25f0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
a2600 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65  recision<0 ) pre
a2610 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73  cision = -precis
a2620 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d  ion;.        c =
a2630 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d   *++fmt;.      }
a2640 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68  else{.        wh
a2650 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63  ile( c>='0' && c
a2660 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20  <='9' ){.       
a2670 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70     precision = p
a2680 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20  recision*10 + c 
a2690 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20  - '0';.         
a26a0 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
a26b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
a26c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a26d0 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a  precision = -1;.
a26e0 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74      }.    /* Get
a26f0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
a2700 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f  type modifier */
a2710 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20  .    if( c=='l' 
a2720 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  ){.      flag_lo
a2730 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20  ng = 1;.      c 
a2740 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20  = *++fmt;.      
a2750 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20  if( c=='l' ){.  
a2760 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c        flag_longl
a2770 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ong = 1;.       
a2780 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
a2790 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a27a0 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20    flag_longlong 
a27b0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
a27c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c   }else{.      fl
a27d0 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c  ag_long = flag_l
a27e0 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20  onglong = 0;.   
a27f0 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20   }.    /* Fetch 
a2800 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66  the info entry f
a2810 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a  or the field */.
a2820 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74      infop = &fmt
a2830 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79  info[0];.    xty
a2840 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a  pe = etINVALID;.
a2850 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69      for(idx=0; i
a2860 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74  dx<ArraySize(fmt
a2870 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20  info); idx++){. 
a2880 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69       if( c==fmti
a2890 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65  nfo[idx].fmttype
a28a0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f   ){.        info
a28b0 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78  p = &fmtinfo[idx
a28c0 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75  ];.        if( u
a28d0 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69  seExtended || (i
a28e0 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c  nfop->flags & FL
a28f0 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b  AG_INTERN)==0 ){
a2900 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65  .          xtype
a2910 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a   = infop->type;.
a2920 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a2930 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b           return;
a2940 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a2950 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a2960 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74  }.    }.    zExt
a2970 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a  ra = 0;...    /*
a2980 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69   Limit the preci
a2990 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20  sion to prevent 
a29a0 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b  overflowing buf[
a29b0 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73  ] during convers
a29c0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ion */.    if( p
a29d0 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49  recision>etBUFSI
a29e0 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d  ZE-40 && (infop-
a29f0 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54  >flags & FLAG_ST
a2a00 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20  RING)==0 ){.    
a2a10 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74    precision = et
a2a20 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20  BUFSIZE-40;.    
a2a30 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
a2a40 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
a2a50 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e  variables are in
a2a60 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c  itialized as fol
a2a70 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  lows:.    **.   
a2a80 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72   **   flag_alter
a2a90 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20  nateform        
a2aa0 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20    TRUE if a '#' 
a2ab0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
a2ac0 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72  **   flag_altfor
a2ad0 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m2              
a2ae0 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69   TRUE if a '!' i
a2af0 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
a2b00 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  *   flag_plussig
a2b10 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
a2b20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73  TRUE if a '+' is
a2b30 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
a2b40 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a2b50 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54  ify            T
a2b60 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20  RUE if a '-' is 
a2b70 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68  present or if th
a2b80 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  e.    **        
a2b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2ba0 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64         field wid
a2bb0 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e  th was negative.
a2bc0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a  .    **   flag_z
a2bd0 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20  eropad          
a2be0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68        TRUE if th
a2bf0 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69  e width began wi
a2c00 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66  th 0..    **   f
a2c10 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20  lag_long        
a2c20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
a2c30 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c  if the letter 'l
a2c40 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64  ' (ell) prefixed
a2c50 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
a2c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2c70 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72        the conver
a2c80 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a  sion character..
a2c90 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f      **   flag_lo
a2ca0 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20  nglong          
a2cb0 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
a2cc0 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c   letter 'll' (el
a2cd0 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a  l ell) prefixed.
a2ce0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
a2cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d00 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
a2d10 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
a2d20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61     **   flag_bla
a2d30 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20  nksign          
a2d40 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20      TRUE if a ' 
a2d50 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
a2d60 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20    **   width    
a2d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d80 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64     The specified
a2d90 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54   field width.  T
a2da0 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20  his is.    **   
a2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2dc0 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61              alwa
a2dd0 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  ys non-negative.
a2de0 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65    Zero is the de
a2df0 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20  fault..    **   
a2e00 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20  precision       
a2e10 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
a2e20 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73  specified precis
a2e30 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c  ion.  The defaul
a2e40 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
a2e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e60 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20         is -1..  
a2e70 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20    **   xtype    
a2e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e90 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20     The class of 
a2ea0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a  the conversion..
a2eb0 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20      **   infop  
a2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2ed0 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20       Pointer to 
a2ee0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
a2ef0 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20  info struct..   
a2f00 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20   */.    switch( 
a2f10 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63  xtype ){.      c
a2f20 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20  ase etPOINTER:. 
a2f30 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
a2f40 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
a2f50 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34  ar*)==sizeof(i64
a2f60 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  );.        flag_
a2f70 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68  long = sizeof(ch
a2f80 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e  ar*)==sizeof(lon
a2f90 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
a2fa0 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
a2fb0 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
a2fc0 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  se */.      case
a2fd0 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20   etORDINAL:.    
a2fe0 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a    case etRADIX:.
a2ff0 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
a3000 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
a3010 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20  SIGNED ){.      
a3020 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20      i64 v;.     
a3030 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
a3040 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  nglong ){.      
a3050 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67        v = va_arg
a3060 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20  (ap,i64);.      
a3070 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c      }else if( fl
a3080 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20  ag_long ){.     
a3090 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72         v = va_ar
a30a0 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a  g(ap,long int);.
a30b0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
a30c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d  .            v =
a30d0 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b   va_arg(ap,int);
a30e0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a30f0 20 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29         if( v<0 )
a3100 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f  {.            lo
a3110 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20  ngvalue = -v;.  
a3120 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78            prefix
a3130 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20   = '-';.        
a3140 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a3150 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
a3160 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   v;.            
a3170 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  if( flag_plussig
a3180 6e 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69  n )        prefi
a3190 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20  x = '+';.       
a31a0 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
a31b0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20  ag_blanksign )  
a31c0 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20  prefix = ' ';.  
a31d0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20            else  
a31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a31f0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b       prefix = 0;
a3200 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a3210 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a3220 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c        if( flag_l
a3230 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20  onglong ){.     
a3240 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a3250 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34   = va_arg(ap,u64
a3260 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
a3270 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67  se if( flag_long
a3280 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a3290 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61  longvalue = va_a
a32a0 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c  rg(ap,unsigned l
a32b0 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
a32c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a32d0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a32e0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73   = va_arg(ap,uns
a32f0 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20  igned int);.    
a3300 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3310 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
a3320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3330 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30  if( longvalue==0
a3340 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74   ) flag_alternat
a3350 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20  eform = 0;.     
a3360 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f     if( flag_zero
a3370 70 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e  pad && precision
a3380 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d  <width-(prefix!=
a3390 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  0) ){.          
a33a0 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74  precision = widt
a33b0 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20  h-(prefix!=0);. 
a33c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a33d0 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74   bufpt = &buf[et
a33e0 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20  BUFSIZE-1];.    
a33f0 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65      if( xtype==e
a3400 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20  tORDINAL ){.    
a3410 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
a3420 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d  st char zOrd[] =
a3430 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20   "thstndrd";.   
a3440 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28         int x = (
a3450 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25  int)(longvalue %
a3460 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20   10);.          
a3470 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e  if( x>=4 || (lon
a3480 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31  gvalue/10)%10==1
a3490 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a34a0 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  x = 0;.         
a34b0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 75 66   }.          buf
a34c0 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20  [etBUFSIZE-3] = 
a34d0 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20  zOrd[x*2];.     
a34e0 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49       buf[etBUFSI
a34f0 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32  ZE-2] = zOrd[x*2
a3500 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62  +1];.          b
a3510 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20  ufpt -= 2;.     
a3520 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20     }.        {. 
a3530 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65           registe
a3540 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73  r const char *cs
a3550 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20  et;      /* Use 
a3560 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 70  registers for sp
a3570 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  eed */.         
a3580 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 62 61   register int ba
a3590 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73  se;.          cs
a35a0 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e  et = &aDigits[in
a35b0 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20  fop->charset];. 
a35c0 20 20 20 20 20 20 20 20 20 62 61 73 65 20 3d 20           base = 
a35d0 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20  infop->base;.   
a35e0 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20         do{      
a35f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3610 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20       /* Convert 
a3620 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20  to ascii */.    
a3630 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70          *(--bufp
a3640 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61  t) = cset[longva
a3650 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20  lue%base];.     
a3660 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a3670 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73   = longvalue/bas
a3680 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68  e;.          }wh
a3690 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30  ile( longvalue>0
a36a0 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   );.        }.  
a36b0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28        length = (
a36c0 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53  int)(&buf[etBUFS
a36d0 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20  IZE-1]-bufpt);. 
a36e0 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70         for(idx=p
a36f0 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b  recision-length;
a3700 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a   idx>0; idx--){.
a3710 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
a3720 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20  fpt) = '0';     
a3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3740 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20          /* Zero 
a3750 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d  pad */.        }
a3760 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
a3770 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29  fix ) *(--bufpt)
a3780 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20 20 20   = prefix;      
a3790 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20           /* Add 
a37a0 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sign */.        
a37b0 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61  if( flag_alterna
a37c0 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d  teform && infop-
a37d0 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20 20 20  >prefix ){      
a37e0 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20 22 30  /* Add "0" or "0
a37f0 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  x" */.          
a3800 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b  const char *pre;
a3810 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
a3820 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65  x;.          pre
a3830 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f   = &aPrefix[info
a3840 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20  p->prefix];.    
a3850 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28        for(; (x=(
a3860 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b  *pre))!=0; pre++
a3870 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78  ) *(--bufpt) = x
a3880 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a3890 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
a38a0 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a  t)(&buf[etBUFSIZ
a38b0 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20  E-1]-bufpt);.   
a38c0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a38d0 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a    case etFLOAT:.
a38e0 20 20 20 20 20 20 63 61 73 65 20 65 74 45 58 50        case etEXP
a38f0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 47  :.      case etG
a3900 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20  ENERIC:.        
a3910 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61  realvalue = va_a
a3920 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23  rg(ap,double);.#
a3930 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a3940 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
a3950 54 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  T.        if( pr
a3960 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63  ecision<0 ) prec
a3970 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20  ision = 6;      
a3980 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c     /* Set defaul
a3990 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20  t precision */. 
a39a0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69         if( preci
a39b0 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32  sion>etBUFSIZE/2
a39c0 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20  -10 ) precision 
a39d0 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30  = etBUFSIZE/2-10
a39e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65  ;.        if( re
a39f0 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20  alvalue<0.0 ){. 
a3a00 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c           realval
a3a10 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b  ue = -realvalue;
a3a20 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69  .          prefi
a3a30 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20  x = '-';.       
a3a40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a3a50 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73    if( flag_pluss
a3a60 69 67 6e 20 29 20 20 20 20 20 20 20 20 20 20 70  ign )          p
a3a70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20  refix = '+';.   
a3a80 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20         else if( 
a3a90 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29  flag_blanksign )
a3aa0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27      prefix = ' '
a3ab0 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65  ;.          else
a3ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3ad0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
a3ae0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
a3af0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
a3b00 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26 20 70  ==etGENERIC && p
a3b10 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65  recision>0 ) pre
a3b20 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a  cision--;.#if 0.
a3b30 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64          /* Round
a3b40 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42  ing works like B
a3b50 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73  SD when the cons
a3b60 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75  tant 0.4999 is u
a3b70 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a  sed.  Wierd! */.
a3b80 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d          for(idx=
a3b90 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64  precision, round
a3ba0 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30  er=0.4999; idx>0
a3bb0 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72  ; idx--, rounder
a3bc0 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20  *=0.1);.#else.  
a3bd0 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65        /* It make
a3be0 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20  s more sense to 
a3bf0 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20  use 0.5 */.     
a3c00 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69     for(idx=preci
a3c10 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e  sion, rounder=0.
a3c20 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c  5; idx>0; idx--,
a3c30 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d   rounder*=0.1){}
a3c40 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
a3c50 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f  if( xtype==etFLO
a3c60 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b  AT ) realvalue +
a3c70 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20  = rounder;.     
a3c80 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20     /* Normalize 
a3c90 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74  realvalue to wit
a3ca0 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76  hin 10.0 > realv
a3cb0 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20  alue >= 1.0 */. 
a3cc0 20 20 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a         exp = 0;.
a3cd0 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
a3ce0 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65  te3IsNaN((double
a3cf0 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20  )realvalue) ){. 
a3d00 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
a3d10 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20   "NaN";.        
a3d20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20    length = 3;.  
a3d30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a3d40 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3d50 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30   if( realvalue>0
a3d60 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .0 ){.          
a3d70 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65  while( realvalue
a3d80 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33  >=1e32 && exp<=3
a3d90 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  50 ){ realvalue 
a3da0 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33  *= 1e-32; exp+=3
a3db0 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77  2; }.          w
a3dc0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e  hile( realvalue>
a3dd0 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30  =1e8 && exp<=350
a3de0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a3df0 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d   1e-8; exp+=8; }
a3e00 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a3e10 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e  ( realvalue>=10.
a3e20 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b  0 && exp<=350 ){
a3e30 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e   realvalue *= 0.
a3e40 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20  1; exp++; }.    
a3e50 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
a3e60 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72  lvalue<1e-8 ){ r
a3e70 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b  ealvalue *= 1e8;
a3e80 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20   exp-=8; }.     
a3e90 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c       while( real
a3ea0 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61  value<1.0 ){ rea
a3eb0 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20  lvalue *= 10.0; 
a3ec0 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20  exp--; }.       
a3ed0 20 20 20 69 66 28 20 65 78 70 3e 33 35 30 20 29     if( exp>350 )
a3ee0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
a3ef0 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b  ( prefix=='-' ){
a3f00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
a3f10 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20  ufpt = "-Inf";. 
a3f20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
a3f30 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27   if( prefix=='+'
a3f40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a3f50 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22    bufpt = "+Inf"
a3f60 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
a3f70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
a3f80 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22     bufpt = "Inf"
a3f90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
a3fa0 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67              leng
a3fb0 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  th = sqlite3Strl
a3fc0 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20  en30(bufpt);.   
a3fd0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
a3fe0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a3ff0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75      }.        bu
a4000 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
a4010 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a     /*.        **
a4020 20 49 66 20 74 68 65 20 66 69 65 6c 64 20 74 79   If the field ty
a4030 70 65 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c  pe is etGENERIC,
a4040 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f   then convert to
a4050 20 65 69 74 68 65 72 20 65 74 45 58 50 0a 20 20   either etEXP.  
a4060 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c        ** or etFL
a4070 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69  OAT, as appropri
a4080 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ate..        */.
a4090 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70          flag_exp
a40a0 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b   = xtype==etEXP;
a40b0 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
a40c0 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20  pe!=etFLOAT ){. 
a40d0 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c           realval
a40e0 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20  ue += rounder;. 
a40f0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61           if( rea
a4100 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20  lvalue>=10.0 ){ 
a4110 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31  realvalue *= 0.1
a4120 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20  ; exp++; }.     
a4130 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
a4140 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49   xtype==etGENERI
a4150 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  C ){.          f
a4160 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f  lag_rtz = !flag_
a4170 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20  alternateform;. 
a4180 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70           if( exp
a4190 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69  <-4 || exp>preci
a41a0 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  sion ){.        
a41b0 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 45 58      xtype = etEX
a41c0 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  P;.          }el
a41d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a41e0 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63  precision = prec
a41f0 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20  ision - exp;.   
a4200 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d           xtype =
a4210 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20   etFLOAT;.      
a4220 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
a4230 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66  lse{.          f
a4240 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20  lag_rtz = 0;.   
a4250 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a4260 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20  f( xtype==etEXP 
a4270 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20  ){.          e2 
a4280 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 0;.        }el
a4290 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32  se{.          e2
a42a0 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20   = exp;.        
a42b0 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20  }.        nsd = 
a42c0 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  0;.        flag_
a42d0 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e  dp = (precision>
a42e0 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61  0 ?1:0) | flag_a
a42f0 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66  lternateform | f
a4300 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20  lag_altform2;.  
a4310 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67        /* The sig
a4320 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68  n in front of th
a4330 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
a4340 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29      if( prefix )
a4350 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a4360 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b  fpt++) = prefix;
a4370 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a4380 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69     /* Digits pri
a4390 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61  or to the decima
a43a0 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20  l point */.     
a43b0 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20     if( e2<0 ){. 
a43c0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a43d0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20  ++) = '0';.     
a43e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a43f0 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b      for(; e2>=0;
a4400 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20   e2--){.        
a4410 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a4420 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65   et_getdigit(&re
a4430 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20  alvalue,&nsd);. 
a4440 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a4450 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
a4460 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  The decimal poin
a4470 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  t */.        if(
a4480 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20   flag_dp ){.    
a4490 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a44a0 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20   = '.';.        
a44b0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22  }.        /* "0"
a44c0 20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68   digits after th
a44d0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a44e0 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66  but before the f
a44f0 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  irst.        ** 
a4500 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
a4510 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  t of the number 
a4520 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65  */.        for(e
a4530 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69  2++; e2<0; preci
a4540 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20  sion--, e2++){. 
a4550 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
a4560 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a   precision>0 );.
a4570 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a4580 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20  t++) = '0';.    
a4590 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
a45a0 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   Significant dig
a45b0 69 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65  its after the de
a45c0 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20  cimal point */. 
a45d0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70         while( (p
a45e0 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b  recision--)>0 ){
a45f0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a4600 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69  pt++) = et_getdi
a4610 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26  git(&realvalue,&
a4620 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nsd);.        }.
a4630 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76          /* Remov
a4640 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73  e trailing zeros
a4650 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20   and the "." if 
a4660 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77  no digits follow
a4670 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20   the "." */.    
a4680 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a      if( flag_rtz
a4690 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20   && flag_dp ){. 
a46a0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a46b0 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29  bufpt[-1]=='0' )
a46c0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b   *(--bufpt) = 0;
a46d0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
a46e0 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a  t( bufpt>buf );.
a46f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75            if( bu
a4700 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a  fpt[-1]=='.' ){.
a4710 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a4720 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b  flag_altform2 ){
a4730 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
a4740 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b  (bufpt++) = '0';
a4750 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
a4760 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a4770 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30    *(--bufpt) = 0
a4780 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
a4790 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a47a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
a47b0 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20   Add the "eNNN" 
a47c0 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20  suffix */.      
a47d0 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c    if( flag_exp |
a47e0 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29  | xtype==etEXP )
a47f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a4800 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73  fpt++) = aDigits
a4810 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
a4820 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a4830 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  exp<0 ){.       
a4840 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a4850 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78  = '-'; exp = -ex
a4860 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  p;.          }el
a4870 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a4880 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27  *(bufpt++) = '+'
a4890 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a48a0 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e          if( exp>
a48b0 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20  =100 ){.        
a48c0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a48d0 20 28 63 68 61 72 29 28 28 65 78 70 2f 31 30 30   (char)((exp/100
a48e0 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f  )+'0');        /
a48f0 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f  * 100's digit */
a4900 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70  .            exp
a4910 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20   %= 100;.       
a4920 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a     }.          *
a4930 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61  (bufpt++) = (cha
a4940 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20  r)(exp/10+'0'); 
a4950 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
a4960 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20  0's digit */.   
a4970 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a4980 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 25 31  ) = (char)(exp%1
a4990 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20  0+'0');         
a49a0 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74      /* 1's digit
a49b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
a49c0 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30        *bufpt = 0
a49d0 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;..        /* Th
a49e0 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62  e converted numb
a49f0 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61  er is in buf[] a
a4a00 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  nd zero terminat
a4a10 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20  ed. Output it.. 
a4a20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74         ** Note t
a4a30 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69  hat the number i
a4a40 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f  s in the usual o
a4a50 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73  rder, not revers
a4a60 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20  ed as with.     
a4a70 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f     ** integer co
a4a80 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20  nversions. */.  
a4a90 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28        length = (
a4aa0 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b  int)(bufpt-buf);
a4ab0 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d  .        bufpt =
a4ac0 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f   buf;..        /
a4ad0 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
a4ae0 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72   Add leading zer
a4af0 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a  os if the flag_z
a4b00 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20  eropad flag is. 
a4b10 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e         ** set an
a4b20 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66  d we are not lef
a4b30 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20  t justified */. 
a4b40 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
a4b50 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67  zeropad && !flag
a4b60 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20  _leftjustify && 
a4b70 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b  length < width){
a4b80 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
a4b90 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
a4ba0 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c  nPad = width - l
a4bb0 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20  ength;.         
a4bc0 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e   for(i=width; i>
a4bd0 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20  =nPad; i--){.   
a4be0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69           bufpt[i
a4bf0 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64  ] = bufpt[i-nPad
a4c00 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  ];.          }. 
a4c10 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65           i = pre
a4c20 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20  fix!=0;.        
a4c30 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20    while( nPad-- 
a4c40 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27  ) bufpt[i++] = '
a4c50 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  0';.          le
a4c60 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20  ngth = width;.  
a4c70 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
a4c80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4c90 20 20 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a      case etSIZE:
a4ca0 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72  .        *(va_ar
a4cb0 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41  g(ap,int*)) = pA
a4cc0 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20  ccum->nChar;.   
a4cd0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69       length = wi
a4ce0 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dth = 0;.       
a4cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
a4d00 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20  se etPERCENT:.  
a4d10 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27        buf[0] = '
a4d20 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70  %';.        bufp
a4d30 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
a4d40 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20   length = 1;.   
a4d50 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a4d60 20 20 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a    case etCHARX:.
a4d70 20 20 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61          c = va_a
a4d80 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
a4d90 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68      buf[0] = (ch
a4da0 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66  ar)c;.        if
a4db0 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29  ( precision>=0 )
a4dc0 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  {.          for(
a4dd0 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69  idx=1; idx<preci
a4de0 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66  sion; idx++) buf
a4df0 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b  [idx] = (char)c;
a4e00 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
a4e10 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20  h = precision;. 
a4e20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a4e30 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4e40 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
a4e50 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
a4e60 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a4e70 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 54  .      case etST
a4e80 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65  RING:.      case
a4e90 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20   etDYNSTRING:.  
a4ea0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 76 61        bufpt = va
a4eb0 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a  _arg(ap,char*);.
a4ec0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
a4ed0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
a4ee0 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20    bufpt = "";.  
a4ef0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
a4f00 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49  xtype==etDYNSTRI
a4f10 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NG ){.          
a4f20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a  zExtra = bufpt;.
a4f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4f40 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e    if( precision>
a4f50 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
a4f60 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65  for(length=0; le
a4f70 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26  ngth<precision &
a4f80 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b  & bufpt[length];
a4f90 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20   length++){}.   
a4fa0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a4fb0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73        length = s
a4fc0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62  qlite3Strlen30(b
a4fd0 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  ufpt);.        }
a4fe0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a4ff0 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c        case etSQL
a5000 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61  ESCAPE:.      ca
a5010 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a  se etSQLESCAPE2:
a5020 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51  .      case etSQ
a5030 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20  LESCAPE3: {.    
a5040 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 2c      int i, j, k,
a5050 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20   n, isnull;.    
a5060 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74      int needQuot
a5070 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  e;.        char 
a5080 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ch;.        char
a5090 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74   q = ((xtype==et
a50a0 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a  SQLESCAPE3)?'"':
a50b0 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74  '\'');   /* Quot
a50c0 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
a50d0 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63         char *esc
a50e0 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  arg = va_arg(ap,
a50f0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20  char*);.        
a5100 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d  isnull = escarg=
a5110 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  =0;.        if( 
a5120 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20  isnull ) escarg 
a5130 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45  = (xtype==etSQLE
a5140 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20  SCAPE2 ? "NULL" 
a5150 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20  : "(NULL)");.   
a5160 20 20 20 20 20 6b 20 3d 20 70 72 65 63 69 73 69       k = precisi
a5170 6f 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  on;.        for(
a5180 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72  i=n=0; (ch=escar
a5190 67 5b 69 5d 29 21 3d 30 20 26 26 20 6b 21 3d 30  g[i])!=0 && k!=0
a51a0 3b 20 69 2b 2b 2c 20 6b 2d 2d 29 7b 0a 20 20 20  ; i++, k--){.   
a51b0 20 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71         if( ch==q
a51c0 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20   )  n++;.       
a51d0 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65 64 51   }.        needQ
a51e0 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c 20 26  uote = !isnull &
a51f0 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  & xtype==etSQLES
a5200 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20 20 6e  CAPE2;.        n
a5210 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65 65 64   += i + 1 + need
a5220 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20 20 20  Quote*2;.       
a5230 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49 5a 45   if( n>etBUFSIZE
a5240 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75   ){.          bu
a5250 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73  fpt = zExtra = s
a5260 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20  qlite3Malloc( n 
a5270 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
a5280 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20   bufpt==0 ){.   
a5290 20 20 20 20 20 20 20 20 20 70 41 63 63 75 6d 2d           pAccum-
a52a0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
a52b0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  1;.            r
a52c0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 20  eturn;.         
a52d0 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
a52e0 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70  {.          bufp
a52f0 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
a5300 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 30   }.        j = 0
a5310 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65  ;.        if( ne
a5320 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b  edQuote ) bufpt[
a5330 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20  j++] = q;.      
a5340 20 20 6b 20 3d 20 69 3b 0a 20 20 20 20 20 20 20    k = i;.       
a5350 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69   for(i=0; i<k; i
a5360 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  ++){.          b
a5370 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 20 3d  ufpt[j++] = ch =
a5380 20 65 73 63 61 72 67 5b 69 5d 3b 0a 20 20 20 20   escarg[i];.    
a5390 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20        if( ch==q 
a53a0 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63  ) bufpt[j++] = c
a53b0 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  h;.        }.   
a53c0 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f       if( needQuo
a53d0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20  te ) bufpt[j++] 
a53e0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66  = q;.        buf
a53f0 70 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20  pt[j] = 0;.     
a5400 20 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20     length = j;. 
a5410 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72         /* The pr
a5420 65 63 69 73 69 6f 6e 20 69 6e 20 25 71 20 61 6e  ecision in %q an
a5430 64 20 25 51 20 6d 65 61 6e 73 20 68 6f 77 20 6d  d %Q means how m
a5440 61 6e 79 20 69 6e 70 75 74 20 63 68 61 72 61 63  any input charac
a5450 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20 20  ters to.        
a5460 2a 2a 20 63 6f 6e 73 75 6d 65 2c 20 6e 6f 74 20  ** consume, not 
a5470 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
a5480 65 20 6f 75 74 70 75 74 2e 2e 2e 0a 20 20 20 20  e output....    
a5490 20 20 20 20 2a 2a 20 69 66 28 20 70 72 65 63 69      ** if( preci
a54a0 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69  sion>=0 && preci
a54b0 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65  sion<length ) le
a54c0 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e  ngth = precision
a54d0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65  ; */.        bre
a54e0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
a54f0 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20    case etTOKEN: 
a5500 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20  {.        Token 
a5510 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67  *pToken = va_arg
a5520 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20  (ap, Token*);.  
a5530 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e        if( pToken
a5540 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
a5550 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a5560 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e  end(pAccum, (con
a5570 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d  st char*)pToken-
a5580 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a  >z, pToken->n);.
a5590 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a55a0 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68    length = width
a55b0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
a55c0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
a55d0 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 53     case etSRCLIS
a55e0 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63  T: {.        Src
a55f0 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f  List *pSrc = va_
a5600 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a  arg(ap, SrcList*
a5610 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b  );.        int k
a5620 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
a5630 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75  t);.        stru
a5640 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
a5650 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e  *pItem = &pSrc->
a5660 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73  a[k];.        as
a5670 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c  sert( k>=0 && k<
a5680 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  pSrc->nSrc );.  
a5690 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
a56a0 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20  >zDatabase ){.  
a56b0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
a56c0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a56d0 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  ccum, pItem->zDa
a56e0 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20  tabase, -1);.   
a56f0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
a5700 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
a5710 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20  cum, ".", 1);.  
a5720 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a5730 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
a5740 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49  ppend(pAccum, pI
a5750 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b  tem->zName, -1);
a5760 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a5770 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20  = width = 0;.   
a5780 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a5790 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
a57a0 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73  t: {.        ass
a57b0 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74 49 4e  ert( xtype==etIN
a57c0 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 20  VALID );.       
a57d0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
a57e0 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77 69  .    }/* End swi
a57f0 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f 72  tch over the for
a5800 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20 20  mat type */.    
a5810 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 65  /*.    ** The te
a5820 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 72  xt of the conver
a5830 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64 20  sion is pointed 
a5840 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61 6e  to by "bufpt" an
a5850 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65 6e  d is.    ** "len
a5860 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73 20  gth" characters 
a5870 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c 64  long.  The field
a5880 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74 68   width is "width
a5890 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74 68  ".  Do.    ** th
a58a0 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a 2f  e output..    */
a58b0 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f 6c  .    if( !flag_l
a58c0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20  eftjustify ){.  
a58d0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
a58e0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e   nspace;.      n
a58f0 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65  space = width-le
a5900 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20  ngth;.      if( 
a5910 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20  nspace>0 ){.    
a5920 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28      appendSpace(
a5930 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b  pAccum, nspace);
a5940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
a5950 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30 20     if( length>0 
a5960 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
a5970 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a5980 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c 65  Accum, bufpt, le
a5990 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ngth);.    }.   
a59a0 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a 75   if( flag_leftju
a59b0 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72  stify ){.      r
a59c0 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61  egister int nspa
a59d0 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65  ce;.      nspace
a59e0 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b   = width-length;
a59f0 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63  .      if( nspac
a5a00 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  e>0 ){.        a
a5a10 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 75  ppendSpace(pAccu
a5a20 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 20  m, nspace);.    
a5a30 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
a5a40 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20 20  ( zExtra ){.    
a5a50 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
a5a60 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20  Extra);.    }.  
a5a70 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f 70  }/* End for loop
a5a80 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74   over the format
a5a90 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20   string */.} /* 
a5aa0 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  End of function 
a5ab0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  */../*.** Append
a5ac0 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 74   N bytes of text
a5ad0 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 53   from z to the S
a5ae0 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e 0a  trAccum object..
a5af0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a5b00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
a5b10 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72  rAccumAppend(Str
a5b20 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20  Accum *p, const 
a5b30 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b  char *z, int N){
a5b40 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20  .  assert( z!=0 
a5b50 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66 28  || N==0 );.  if(
a5b60 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d 3e   p->tooBig | p->
a5b70 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
a5b80 20 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e      testcase(p->
a5b90 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65 73  tooBig);.    tes
a5ba0 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46  tcase(p->mallocF
a5bb0 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74 75  ailed);.    retu
a5bc0 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3c  rn;.  }.  if( N<
a5bd0 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 71 6c  0 ){.    N = sql
a5be0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b  ite3Strlen30(z);
a5bf0 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 30 20  .  }.  if( N==0 
a5c00 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29 20 29  || NEVER(z==0) )
a5c10 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
a5c20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 61 72  }.  if( p->nChar
a5c30 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20  +N >= p->nAlloc 
a5c40 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65  ){.    char *zNe
a5c50 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 75  w;.    if( !p->u
a5c60 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  seMalloc ){.    
a5c70 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b    p->tooBig = 1;
a5c80 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 6e 41  .      N = p->nA
a5c90 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 72 20  lloc - p->nChar 
a5ca0 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 4e  - 1;.      if( N
a5cb0 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  <=0 ){.        r
a5cc0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
a5cd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a5ce0 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e  i64 szNew = p->n
a5cf0 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e 65  Char;.      szNe
a5d00 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20 20  w += N + 1;.    
a5d10 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70 2d    if( szNew > p-
a5d20 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >mxAlloc ){.    
a5d30 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a5d40 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20  cumReset(p);.   
a5d50 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d       p->tooBig =
a5d60 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   1;.        retu
a5d70 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  rn;.      }else{
a5d80 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c  .        p->nAll
a5d90 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 3b  oc = (int)szNew;
a5da0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
a5db0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
a5dc0 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20  allocRaw(p->db, 
a5dd0 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20  p->nAlloc );.   
a5de0 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20     if( zNew ){. 
a5df0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e         memcpy(zN
a5e00 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 70 2d  ew, p->zText, p-
a5e10 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 20 20  >nChar);.       
a5e20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a5e30 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  Reset(p);.      
a5e40 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a 4e 65    p->zText = zNe
a5e50 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  w;.      }else{.
a5e60 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f          p->mallo
a5e70 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
a5e80 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a5e90 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20  ccumReset(p);.  
a5ea0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a5eb0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
a5ec0 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 54 65    memcpy(&p->zTe
a5ed0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 7a 2c  xt[p->nChar], z,
a5ee0 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 20   N);.  p->nChar 
a5ef0 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  += N;.}../*.** F
a5f00 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 72 69  inish off a stri
a5f10 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 75 72  ng by making sur
a5f20 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 65 72  e it is zero-ter
a5f30 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 74 75  minated..** Retu
a5f40 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
a5f50 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74  the resulting st
a5f60 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 20  ring.  Return a 
a5f70 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  NULL.** pointer 
a5f80 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65  if any kind of e
a5f90 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 6e 74  rror was encount
a5fa0 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ered..*/.SQLITE_
a5fb0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
a5fc0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
a5fd0 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a 70 29  ish(StrAccum *p)
a5fe0 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74  {.  if( p->zText
a5ff0 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 78 74   ){.    p->zText
a6000 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b 0a  [p->nChar] = 0;.
a6010 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 4d 61      if( p->useMa
a6020 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 78 74  lloc && p->zText
a6030 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20  ==p->zBase ){.  
a6040 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 73      p->zText = s
a6050 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
a6060 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 68 61  w(p->db, p->nCha
a6070 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28  r+1 );.      if(
a6080 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20   p->zText ){.   
a6090 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a       memcpy(p->z
a60a0 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 2c 20  Text, p->zBase, 
a60b0 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20  p->nChar+1);.   
a60c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a60d0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65    p->mallocFaile
a60e0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  d = 1;.      }. 
a60f0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
a6100 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f  n p->zText;.}../
a6110 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 53 74  *.** Reset an St
a6120 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e 20 20  rAccum string.  
a6130 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 6c 6c  Reclaim all mall
a6140 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  oced memory..*/.
a6150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a6160 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63  oid sqlite3StrAc
a6170 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75  cumReset(StrAccu
a6180 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  m *p){.  if( p->
a6190 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 65 20  zText!=p->zBase 
a61a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
a61b0 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a  Free(p->db, p->z
a61c0 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e  Text);.  }.  p->
a61d0 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  zText = 0;.}../*
a61e0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
a61f0 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 6c 61   string accumula
a6200 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  tor.*/.SQLITE_PR
a6210 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a6220 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53  e3StrAccumInit(S
a6230 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 61 72  trAccum *p, char
a6240 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e 2c 20   *zBase, int n, 
a6250 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e 7a 54  int mx){.  p->zT
a6260 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 20 3d  ext = p->zBase =
a6270 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 62 20   zBase;.  p->db 
a6280 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 20  = 0;.  p->nChar 
a6290 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63  = 0;.  p->nAlloc
a62a0 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 6c 6c   = n;.  p->mxAll
a62b0 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e 75 73  oc = mx;.  p->us
a62c0 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 70  eMalloc = 1;.  p
a62d0 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a 20 20  ->tooBig = 0;.  
a62e0 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  p->mallocFailed 
a62f0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  = 0;.}../*.** Pr
a6300 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a6310 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a6320 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55  liteMalloc().  U
a6330 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  se the internal.
a6340 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
a6350 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
a6360 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a6370 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72 69  ar *sqlite3VMPri
a6380 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ntf(sqlite3 *db,
a6390 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
a63a0 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70  rmat, va_list ap
a63b0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  ){.  char *z;.  
a63c0 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54  char zBase[SQLIT
a63d0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
a63e0 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63  ];.  StrAccum ac
a63f0 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  c;.  assert( db!
a6400 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  =0 );.  sqlite3S
a6410 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63  trAccumInit(&acc
a6420 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28  , zBase, sizeof(
a6430 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 20  zBase),.        
a6440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
a6450 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
a6460 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 3b 0a  LIMIT_LENGTH]);.
a6470 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a 20    acc.db = db;. 
a6480 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66   sqlite3VXPrintf
a6490 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d 61  (&acc, 1, zForma
a64a0 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71  t, ap);.  z = sq
a64b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
a64c0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66 28  ish(&acc);.  if(
a64d0 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65   acc.mallocFaile
a64e0 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  d ){.    db->mal
a64f0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
a6500 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d   }.  return z;.}
a6510 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e  ../*.** Print in
a6520 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
a6530 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
a6540 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65  lloc().  Use the
a6550 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63   internal.** %-c
a6560 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73  onversion extens
a6570 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
a6580 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
a6590 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c  lite3MPrintf(sql
a65a0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
a65b0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a65c0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
a65d0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  p;.  char *z;.  
a65e0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
a65f0 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  rmat);.  z = sql
a6600 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c  ite3VMPrintf(db,
a6610 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
a6620 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
a6630 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn z;.}../*.*
a6640 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50  * Like sqlite3MP
a6650 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c  rintf(), but cal
a6660 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  l sqlite3DbFree(
a6670 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20  ) on zStr after 
a6680 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68  formatting.** th
a6690 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66  e string and bef
a66a0 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20  ore returnning. 
a66b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
a66c0 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
a66d0 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66  used.** to modif
a66e0 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74  y an existing st
a66f0 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70  ring.  For examp
a6700 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  le:.**.**       
a6710 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  x = sqlite3MPrin
a6720 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69  tf(db, x, "prefi
a6730 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78 29  x %s suffix", x)
a6740 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ;.**.*/.SQLITE_P
a6750 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
a6760 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c  ite3MAppendf(sql
a6770 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
a6780 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72  zStr, const char
a6790 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
a67a0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
a67b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73   char *z;.  va_s
a67c0 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
a67d0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a67e0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
a67f0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
a6800 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74  end(ap);.  sqlit
a6810 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74  e3DbFree(db, zSt
a6820 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  r);.  return z;.
a6830 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a6840 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a6850 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
a6860 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74  _malloc().  Omit
a6870 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   the internal.**
a6880 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78   %-conversion ex
a6890 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
a68a0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
a68b0 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63  lite3_vmprintf(c
a68c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a68d0 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b  at, va_list ap){
a68e0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68  .  char *z;.  ch
a68f0 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f  ar zBase[SQLITE_
a6900 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b  PRINT_BUF_SIZE];
a6910 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b  .  StrAccum acc;
a6920 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a6930 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
a6940 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
a6950 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72  ialize() ) retur
a6960 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  n 0;.#endif.  sq
a6970 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a6980 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
a6990 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51  izeof(zBase), SQ
a69a0 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29  LITE_MAX_LENGTH)
a69b0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
a69c0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
a69d0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d  rmat, ap);.  z =
a69e0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a69f0 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20  Finish(&acc);.  
a6a00 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
a6a10 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65  ** Print into me
a6a20 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
a6a30 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
a6a40 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65  c()().  Omit the
a6a50 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63   internal.** %-c
a6a60 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73  onversion extens
a6a70 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
a6a80 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
a6a90 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  3_mprintf(const 
a6aa0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a6ab0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
a6ac0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69  p;.  char *z;.#i
a6ad0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a6ae0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
a6af0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
a6b00 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30  ize() ) return 0
a6b10 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74  ;.#endif.  va_st
a6b20 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
a6b30 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  z = sqlite3_
a6b40 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74  vmprintf(zFormat
a6b50 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
a6b60 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  ap);.  return z;
a6b70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65  .}../*.** sqlite
a6b80 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72  3_snprintf() wor
a6b90 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66  ks like snprintf
a6ba0 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69  () except that i
a6bb0 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a  t ignores the.**
a6bc0 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20   current locale 
a6bd0 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20  settings.  This 
a6be0 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72  is important for
a6bf0 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65 20   SQLite because 
a6c00 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62  we.** are not ab
a6c10 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20  le to use a "," 
a6c20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  as the decimal p
a6c30 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66  oint in place of
a6c40 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69   "." as.** speci
a6c50 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63  fied by some loc
a6c60 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ales..*/.SQLITE_
a6c70 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
a6c80 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e  3_snprintf(int n
a6c90 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f  , char *zBuf, co
a6ca0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
a6cb0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20  t, ...){.  char 
a6cc0 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70  *z;.  va_list ap
a6cd0 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63  ;.  StrAccum acc
a6ce0 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b  ;..  if( n<=0 ){
a6cf0 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66  .    return zBuf
a6d00 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
a6d10 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63  trAccumInit(&acc
a6d20 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20  , zBuf, n, 0);. 
a6d30 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d   acc.useMalloc =
a6d40 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
a6d50 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71  p,zFormat);.  sq
a6d60 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61  lite3VXPrintf(&a
a6d70 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  cc, 0, zFormat, 
a6d80 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a6d90 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a6da0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
a6db0 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  acc);.  return z
a6dc0 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  ;.}..#if defined
a6dd0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
a6de0 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f  *.** A version o
a6df0 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74 20  f printf() that 
a6e00 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64  understands %lld
a6e10 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 75  .  Used for debu
a6e20 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72  gging..** The pr
a6e30 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74  intf() built int
a6e40 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  o some versions 
a6e50 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20  of windows does 
a6e60 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25  not understand %
a6e70 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61  lld.** and segfa
a6e80 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76 65  ults if you give
a6e90 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20   it a long long 
a6ea0 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
a6eb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a6ec0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63  te3DebugPrintf(c
a6ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a6ee0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a6ef0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63  ist ap;.  StrAcc
a6f00 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a  um acc;.  char z
a6f10 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69  Buf[500];.  sqli
a6f20 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
a6f30 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65  &acc, zBuf, size
a6f40 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20  of(zBuf), 0);.  
a6f50 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20  acc.useMalloc = 
a6f60 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  0;.  va_start(ap
a6f70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c  ,zFormat);.  sql
a6f80 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63  ite3VXPrintf(&ac
a6f90 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  c, 0, zFormat, a
a6fa0 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
a6fb0 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  ;.  sqlite3StrAc
a6fc0 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
a6fd0 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75  .  fprintf(stdou
a6fe0 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20  t,"%s", zBuf);. 
a6ff0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b   fflush(stdout);
a7000 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
a7010 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
a7020 52 41 43 45 0a 2f 2a 0a 2a 2a 20 76 61 72 69 61  RACE./*.** varia
a7030 62 6c 65 2d 61 72 67 75 6d 65 6e 74 20 77 72 61  ble-argument wra
a7040 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69  pper around sqli
a7050 74 65 33 56 58 50 72 69 6e 74 66 28 29 2e 0a 2a  te3VXPrintf()..*
a7060 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a7070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 58 50 72   void sqlite3XPr
a7080 69 6e 74 66 28 53 74 72 41 63 63 75 6d 20 2a 70  intf(StrAccum *p
a7090 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
a70a0 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
a70b0 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f  a_list ap;.  va_
a70c0 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74  start(ap,zFormat
a70d0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72  );.  sqlite3VXPr
a70e0 69 6e 74 66 28 70 2c 20 31 2c 20 7a 46 6f 72 6d  intf(p, 1, zForm
a70f0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
a7100 64 28 61 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  d(ap);.}.#endif.
a7110 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a7120 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63   End of printf.c
a7130 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a7140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a7160 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a7170 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64   Begin file rand
a7180 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  om.c ***********
a7190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a71a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a71b0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
a71c0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
a71d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
a71e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
a71f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
a7200 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
a7210 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
a7220 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
a7230 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
a7240 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
a7250 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
a7260 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
a7270 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
a7280 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
a7290 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
a72a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
a72b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
a72c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
a72d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
a72e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a72f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7320 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
a7330 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
a7340 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
a7350 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  a pseudo-random 
a7360 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61  number.** genera
a7370 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53  tor (PRNG) for S
a7380 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e  QLite..**.** Ran
a7390 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20  dom numbers are 
a73a0 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20  used by some of 
a73b0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63  the database bac
a73c0 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a  kends in order.*
a73d0 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61  * to generate ra
a73e0 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79  ndom integer key
a73f0 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20  s for tables or 
a7400 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73  random filenames
a7410 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68  ..*/.../* All th
a7420 72 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69  reads share a si
a7430 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  ngle random numb
a7440 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a  er generator..**
a7450 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
a7460 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  is the current s
a7470 74 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65  tate of the gene
a7480 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
a7490 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
a74a0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a74b0 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  pe {.  unsigned 
a74c0 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20  char isInit;    
a74d0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a74e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
a74f0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
a7500 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
a7510 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62   /* State variab
a7520 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  les */.  unsigne
a7530 64 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20  d char s[256];  
a7540 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65          /* State
a7550 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20   variables */.} 
a7560 73 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a  sqlite3Prng;../*
a7570 0a 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65  .** Get a single
a7580 20 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61   8-bit random va
a7590 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34  lue from the RC4
a75a0 20 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65   PRNG.  The Mute
a75b0 78 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c  x.** must be hel
a75c0 64 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e  d while executin
a75d0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  g this routine..
a75e0 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75  **.** Why not ju
a75f0 73 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79  st use a library
a7600 20 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f   random generato
a7610 72 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29  r like lrand48()
a7620 20 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65   for this?.** Be
a7630 63 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77  cause the OP_New
a7640 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20  Rowid opcode in 
a7650 74 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73  the VDBE depends
a7660 20 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72   on having a ver
a7670 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65  y.** good source
a7680 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65   of random numbe
a7690 72 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38  rs.  The lrand48
a76a0 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74  () library funct
a76b0 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20  ion may.** well 
a76c0 62 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20  be good enough. 
a76d0 20 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20   But maybe not. 
a76e0 20 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34   Or maybe lrand4
a76f0 38 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20  8() has some.** 
a7700 73 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20  subtle problems 
a7710 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20  on some systems 
a7720 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65  that could cause
a7730 20 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69   problems.  It i
a7740 73 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f  s hard.** to kno
a7750 77 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20  w.  To minimize 
a7760 74 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62  the risk of prob
a7770 6c 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20  lems due to bad 
a7780 6c 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70  lrand48().** imp
a7790 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51  lementations, SQ
a77a0 4c 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72  Lite uses this r
a77b0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
a77c0 65 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20  erator based.** 
a77d0 6f 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65  on RC4, which we
a77e0 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79   know works very
a77f0 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61   well..**.** (La
a7800 74 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c  ter):  Actually,
a7810 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65   OP_NewRowid doe
a7820 73 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20  s not depend on 
a7830 61 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66  a good source of
a7840 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61  .** randomness a
a7850 6e 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65  ny more.  But we
a7860 20 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73   will leave this
a7870 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65   code in all the
a7880 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   same..*/.static
a7890 20 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76   u8 randomByte(v
a78a0 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  oid){.  unsigned
a78b0 20 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20   char t;...  /* 
a78c0 54 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61  The "wsdPrng" ma
a78d0 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65  cro will resolve
a78e0 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72   to the pseudo-r
a78f0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
a7900 65 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74  erator.  ** stat
a7910 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72  e vector.  If wr
a7920 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
a7930 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  ta is unsupporte
a7940 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c  d on the target,
a7950 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  .  ** we have to
a7960 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74   locate the stat
a7970 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d  e vector at run-
a7980 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f  time.  In the mo
a7990 72 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63  re common.  ** c
a79a0 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62  ase where writab
a79b0 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
a79c0 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64  s supported, wsd
a79d0 50 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64  Prng can refer d
a79e0 69 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20  irectly.  ** to 
a79f0 74 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67  the "sqlite3Prng
a7a00 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64  " state vector d
a7a10 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20  eclared above.. 
a7a20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
a7a30 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72  E_OMIT_WSD.  str
a7a40 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
a7a50 79 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c  ype *p = &GLOBAL
a7a60 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50  (struct sqlite3P
a7a70 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33  rngType, sqlite3
a7a80 50 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20  Prng);.# define 
a7a90 77 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c  wsdPrng p[0].#el
a7aa0 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50  se.# define wsdP
a7ab0 72 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a  rng sqlite3Prng.
a7ac0 23 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e  #endif...  /* In
a7ad0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61  itialize the sta
a7ae0 74 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d  te of the random
a7af0 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f   number generato
a7b00 72 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65  r once,.  ** the
a7b10 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
a7b20 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
a7b30 65 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61  ed.  The seed va
a7b40 6c 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f  lue does.  ** no
a7b50 74 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69  t need to contai
a7b60 6e 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f  n a lot of rando
a7b70 6d 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61  mness since we a
a7b80 72 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69  re not.  ** tryi
a7b90 6e 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20  ng to do secure 
a7ba0 65 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e  encryption or an
a7bb0 79 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74  ything like that
a7bc0 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  ....  **.  ** No
a7bd0 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69  thing in this fi
a7be0 6c 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65  le or anywhere e
a7bf0 6c 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f  lse in SQLite do
a7c00 65 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20  es any kind of. 
a7c10 20 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20   ** encryption. 
a7c20 20 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74   The RC4 algorit
a7c30 68 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64  hm is being used
a7c40 20 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75   as a PRNG (pseu
a7c50 64 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e  do-random.  ** n
a7c60 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29  umber generator)
a7c70 20 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79   not as an encry
a7c80 70 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20  ption device..  
a7c90 2a 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e  */.  if( !wsdPrn
a7ca0 67 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  g.isInit ){.    
a7cb0 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20  int i;.    char 
a7cc0 6b 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50  k[256];.    wsdP
a7cd0 72 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77  rng.j = 0;.    w
a7ce0 73 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20  sdPrng.i = 0;.  
a7cf0 20 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f    sqlite3OsRando
a7d00 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
a7d10 73 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20  s_find(0), 256, 
a7d20 6b 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  k);.    for(i=0;
a7d30 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20   i<256; i++){.  
a7d40 20 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d      wsdPrng.s[i]
a7d50 20 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a   = (u8)i;.    }.
a7d60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
a7d70 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  56; i++){.      
a7d80 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64  wsdPrng.j += wsd
a7d90 50 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d  Prng.s[i] + k[i]
a7da0 3b 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50  ;.      t = wsdP
a7db0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d  rng.s[wsdPrng.j]
a7dc0 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e  ;.      wsdPrng.
a7dd0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77  s[wsdPrng.j] = w
a7de0 73 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20  sdPrng.s[i];.   
a7df0 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20     wsdPrng.s[i] 
a7e00 3d 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77  = t;.    }.    w
a7e10 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20  sdPrng.isInit = 
a7e20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  1;.  }..  /* Gen
a7e30 65 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  erate and return
a7e40 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62   single random b
a7e50 79 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72  yte.  */.  wsdPr
a7e60 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73  ng.i++;.  t = ws
a7e70 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a7e80 69 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20  i];.  wsdPrng.j 
a7e90 2b 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e  += t;.  wsdPrng.
a7ea0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77  s[wsdPrng.i] = w
a7eb0 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67  sdPrng.s[wsdPrng
a7ec0 2e 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73  .j];.  wsdPrng.s
a7ed0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b  [wsdPrng.j] = t;
a7ee0 0a 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e  .  t += wsdPrng.
a7ef0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20  s[wsdPrng.i];.  
a7f00 72 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73  return wsdPrng.s
a7f10 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  [t];.}../*.** Re
a7f20 74 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79  turn N random by
a7f30 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tes..*/.SQLITE_A
a7f40 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
a7f50 72 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e  randomness(int N
a7f60 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20  , void *pBuf){. 
a7f70 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
a7f80 7a 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66  zBuf = pBuf;.#if
a7f90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
a7fa0 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  FE.  sqlite3_mut
a7fb0 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
a7fc0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
a7fd0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
a7fe0 43 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a  C_PRNG);.#endif.
a7ff0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
a8000 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
a8010 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20  while( N-- ){.  
a8020 20 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61    *(zBuf++) = ra
a8030 6e 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a  ndomByte();.  }.
a8040 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
a8050 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a  leave(mutex);.}.
a8060 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a8070 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
a8080 54 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74  T./*.** For test
a8090 69 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65  ing purposes, we
a80a0 20 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20   sometimes want 
a80b0 74 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20  to preserve the 
a80c0 73 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47  state of.** PRNG
a80d0 20 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65   and restore the
a80e0 20 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76   PRNG to its sav
a80f0 65 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61  ed state at a la
a8100 74 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20  ter time, or.** 
a8110 74 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e  to reset the PRN
a8120 47 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c  G to its initial
a8130 20 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72   state.  These r
a8140 6f 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69  outines accompli
a8150 73 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b  sh.** those task
a8160 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  s..**.** The sql
a8170 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
a8180 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  l() interface ca
a8190 6c 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e  lls these routin
a81a0 65 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c  es to.** control
a81b0 20 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74   the PRNG..*/.st
a81c0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
a81d0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
a81e0 6e 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61  ngType sqlite3Sa
a81f0 76 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f  vedPrng;.SQLITE_
a8200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a8210 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
a8220 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70  e(void){.  memcp
a8230 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  y(.    &GLOBAL(s
a8240 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a8250 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61  gType, sqlite3Sa
a8260 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47  vedPrng),.    &G
a8270 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
a8280 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
a8290 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20  lite3Prng),.    
a82a0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72  sizeof(sqlite3Pr
a82b0 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54  ng).  );.}.SQLIT
a82c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a82d0 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72  qlite3PrngRestor
a82e0 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20  eState(void){.  
a82f0 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f  memcpy(.    &GLO
a8300 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
a8310 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
a8320 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47  te3Prng),.    &G
a8330 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
a8340 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
a8350 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c  lite3SavedPrng),
a8360 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69  .    sizeof(sqli
a8370 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a  te3Prng).  );.}.
a8380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a8390 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52  oid sqlite3PrngR
a83a0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b  esetState(void){
a83b0 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  .  GLOBAL(struct
a83c0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a83d0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69  , sqlite3Prng).i
a83e0 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e  sInit = 0;.}.#en
a83f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
a8400 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20  IT_BUILTIN_TEST 
a8410 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
a8420 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f  *** End of rando
a8430 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
a8440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8460 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
a8470 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75  *** Begin file u
a8480 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
a8490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a84a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a84b0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41  **/./*.** 2004 A
a84c0 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68  pril 13.**.** Th
a84d0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
a84e0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
a84f0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
a8500 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
a8510 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
a8520 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
a8530 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
a8540 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
a8550 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
a8560 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
a8570 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
a8580 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
a8590 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
a85a0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
a85b0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
a85c0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
a85d0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
a85e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a85f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a  ****************
a8620 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
a8630 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
a8640 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72  tines used to tr
a8650 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20  anslate between 
a8660 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31  UTF-8, .** UTF-1
a8670 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64  6, UTF-16BE, and
a8680 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a   UTF-16LE..**.**
a8690 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a   Notes on UTF-8:
a86a0 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20  .**.**   Byte-0 
a86b0 20 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74     Byte-1    Byt
a86c0 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20  e-2    Byte-3   
a86d0 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78   Value.**  0xxxx
a86e0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20  xxx             
a86f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8700 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30      00000000 000
a8710 30 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a  00000 0xxxxxxx.*
a8720 2a 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78  *  110yyyyy  10x
a8730 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20  xxxxx           
a8740 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30              0000
a8750 30 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79  0000 00000yyy yy
a8760 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a  xxxxxx.**  1110z
a8770 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31  zzz  10yyyyyy  1
a8780 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20  0xxxxxx         
a8790 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a      00000000 zzz
a87a0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  zyyyy yyxxxxxx.*
a87b0 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75  *  11110uuu  10u
a87c0 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20  uzzzz  10yyyyyy 
a87d0 20 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75   10xxxxxx   000u
a87e0 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79  uuuu zzzzyyyy yy
a87f0 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  xxxxxx.**.**.** 
a8800 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a  Notes on UTF-16:
a8810 20 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d    (with wwww+1==
a8820 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20  uuuuu).**.**    
a8830 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20    Word-0        
a8840 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20         Word-1   
a8850 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20         Value.** 
a8860 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a   110110ww wwzzzz
a8870 79 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79  yy   110111yy yy
a8880 78 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75  xxxxxx    000uuu
a8890 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  uu zzzzyyyy yyxx
a88a0 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79  xxxx.**  zzzzyyy
a88b0 79 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20  y yyxxxxxx      
a88c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a88d0 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79    00000000 zzzzy
a88e0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a  yyy yyxxxxxx.**.
a88f0 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74  **.** BOM or Byt
a8900 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a  e Order Mark:.**
a8910 20 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20       0xff 0xfe  
a8920 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75   little-endian u
a8930 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a  tf-16 follows.**
a8940 20 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20       0xfe 0xff  
a8950 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d   big-endian utf-
a8960 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f  16 follows.**.*/
a8970 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a8980 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74   Include vdbeInt
a8990 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
a89a0 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a   of utf.c ******
a89b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a89c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a89d0 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
a89e0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
a89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a8a10 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74  ./*.** 2003 Sept
a8a20 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68  ember 6.**.** Th
a8a30 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
a8a40 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
a8a50 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
a8a60 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
a8a70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
a8a80 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
a8a90 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
a8aa0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
a8ab0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
a8ac0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
a8ad0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
a8ae0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
a8af0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
a8b00 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
a8b10 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
a8b20 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
a8b30 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
a8b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8b80 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
a8b90 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
a8ba0 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  e for informatio
a8bb0 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74  n that is privat
a8bc0 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45  e to the.** VDBE
a8bd0 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  .  This informat
a8be0 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20  ion used to all 
a8bf0 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  be at the top of
a8c00 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73   the single.** s
a8c10 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20  ource code file 
a8c20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20  "vdbe.c".  When 
a8c30 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65  that file became
a8c40 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a   too big (over.*
a8c50 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e  * 6000 lines lon
a8c60 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20  g) it was split 
a8c70 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20  up into several 
a8c80 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e  smaller files an
a8c90 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72  d.** this header
a8ca0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73   information was
a8cb0 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a   factored out..*
a8cc0 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49  /.#ifndef _VDBEI
a8cd0 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56  NT_H_.#define _V
a8ce0 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  DBEINT_H_../*.**
a8cf0 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74   SQL is translat
a8d00 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e  ed into a sequen
a8d10 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  ce of instructio
a8d20 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63  ns to be.** exec
a8d30 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61  uted by a virtua
a8d40 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68  l machine.  Each
a8d50 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
a8d60 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f  an instance.** o
a8d70 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
a8d80 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
a8d90 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
a8da0 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42  eOp Op;../*.** B
a8db0 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f  oolean values.*/
a8dc0 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
a8dd0 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a  d char Bool;../*
a8de0 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
a8df0 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61  a pointer into a
a8e00 20 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69   single BTree wi
a8e10 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
a8e20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72  file..** The cur
a8e30 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20  sor can seek to 
a8e40 61 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69  a BTree entry wi
a8e50 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  th a particular 
a8e60 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20  key, or.** loop 
a8e70 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73  over all entries
a8e80 20 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20   of the Btree.  
a8e90 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73  You can also ins
a8ea0 65 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a  ert new BTree.**
a8eb0 20 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72   entries or retr
a8ec0 69 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20  ieve the key or 
a8ed0 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e  data from the en
a8ee0 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72  try that the cur
a8ef0 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e  sor.** is curren
a8f00 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e  tly pointing to.
a8f10 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75  .** .** Every cu
a8f20 72 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69  rsor that the vi
a8f30 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
a8f40 73 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73  s open is repres
a8f50 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69  ented by an.** i
a8f60 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
a8f70 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
a8f80 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  re..**.** If the
a8f90 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72   VdbeCursor.isTr
a8fa0 69 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73  iggerRow flag is
a8fb0 20 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68   set it means th
a8fc0 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69  at this cursor i
a8fd0 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69  s.** really a si
a8fe0 6e 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65  ngle row that re
a8ff0 70 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57  presents the NEW
a9000 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74   or OLD pseudo-t
a9010 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77  able of.** a row
a9020 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64   trigger.  The d
a9030 61 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20  ata for the row 
a9040 69 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62  is stored in Vdb
a9050 65 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e  eCursor.pData an
a9060 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69  d.** the rowid i
a9070 73 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e  s in VdbeCursor.
a9080 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  iKey..*/.struct 
a9090 56 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42  VdbeCursor {.  B
a90a0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
a90b0 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  ;    /* The curs
a90c0 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  or structure of 
a90d0 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20  the backend */. 
a90e0 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
a90f0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
a9100 6f 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61  of cursor databa
a9110 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  se in db->aDb[] 
a9120 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34  (or -1) */.  i64
a9130 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
a9140 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64     /* Last rowid
a9150 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20   from a Next or 
a9160 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f  NextIdx operatio
a9170 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f  n */.  Bool zero
a9180 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ed;          /* 
a9190 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f  True if zeroed o
a91a0 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72  ut and ready for
a91b0 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c   reuse */.  Bool
a91c0 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20   rowidIsValid;  
a91d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73    /* True if las
a91e0 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20  tRowid is valid 
a91f0 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73  */.  Bool atFirs
a9200 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
a9210 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74  ue if pointing t
a9220 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  o first entry */
a9230 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f  .  Bool useRando
a9240 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65  mRowid;  /* Gene
a9250 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20  rate new record 
a9260 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e  numbers semi-ran
a9270 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20  domly */.  Bool 
a9280 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20  nullRow;        
a9290 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e   /* True if poin
a92a0 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69  ting to a row wi
a92b0 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20  th no data */.  
a92c0 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76  Bool deferredMov
a92d0 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20  eto;  /* A call 
a92e0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  to sqlite3BtreeM
a92f0 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65  oveto() is neede
a9300 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61  d */.  Bool isTa
a9310 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ble;         /* 
a9320 54 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20  True if a table 
a9330 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65  requiring intege
a9340 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c  r keys */.  Bool
a9350 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20   isIndex;       
a9360 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20    /* True if an 
a9370 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67  index containing
a9380 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20   keys only - no 
a9390 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f  data */.  i64 mo
a93a0 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20  vetoTarget;     
a93b0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
a93c0 68 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69  he deferred sqli
a93d0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
a93e0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
a93f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
a9400 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c  eparate file hol
a9410 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74  ding temporary t
a9420 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73  able */.  int ps
a9430 65 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20  eudoTableReg;   
a9440 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
a9450 69 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20  ing pseudotable 
a9460 63 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65  content. */.  Ke
a9470 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
a9480 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75      /* Info abou
a9490 74 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65  t index keys nee
a94a0 64 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72  ded by index cur
a94b0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  sors */.  int nF
a94c0 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  ield;           
a94d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
a94e0 6c 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65  lds in the heade
a94f0 72 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f  r */.  i64 seqCo
a9500 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  unt;         /* 
a9510 53 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72  Sequence counter
a9520 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
a9530 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
a9540 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20  Cursor;  /* The 
a9550 63 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72  cursor for a vir
a9560 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
a9570 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
a9580 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
a9590 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72     /* Module for
a95a0 20 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72   cursor pVtabCur
a95b0 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73  sor */..  /* Res
a95c0 75 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69  ult of last sqli
a95d0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
a95e0 20 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e   done by an OP_N
a95f0 6f 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a  otExists or .  *
a9600 2a 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70  * OP_IsUnique op
a9610 63 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72  code on this cur
a9620 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65  sor. */.  int se
a9630 65 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20  ekResult;..  /* 
a9640 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69  Cached informati
a9650 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61  on about the hea
a9660 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  der for the data
a9670 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65   record that the
a9680 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20  .  ** cursor is 
a9690 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
a96a0 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c  ng to.  Only val
a96b0 69 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75  id if cacheStatu
a96c0 73 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56  s matches.  ** V
a96d0 64 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56  dbe.cacheCtr.  V
a96e0 64 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c  dbe.cacheCtr wil
a96f0 6c 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20  l never take on 
a9700 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a  the value of.  *
a9710 2a 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e  * CACHE_STALE an
a9720 64 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63  d so setting cac
a9730 68 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53  heStatus=CACHE_S
a9740 54 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20  TALE guarantees 
a9750 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61  that.  ** the ca
a9760 63 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61  che is out of da
a9770 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52  te..  **.  ** aR
a9780 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74  ow might point t
a9790 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61  o (ephemeral) da
a97a0 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ta for the curre
a97b0 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69  nt row, or it mi
a97c0 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c  ght.  ** be NULL
a97d0 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63  ..  */.  u32 cac
a97e0 68 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f  heStatus;      /
a97f0 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64  * Cache is valid
a9800 20 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73   if this matches
a9810 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a   Vdbe.cacheCtr *
a9820 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53  /.  int payloadS
a9830 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74  ize;      /* Tot
a9840 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
a9850 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
a9860 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65   */.  u32 *aType
a9870 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
a9880 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61  ype values for a
a9890 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ll entries in th
a98a0 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33  e record */.  u3
a98b0 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
a98c0 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66      /* Cached of
a98d0 66 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61  fsets to the sta
a98e0 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  rt of each colum
a98f0 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20  ns data */.  u8 
a9900 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20  *aRow;          
a9910 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74     /* Data for t
a9920 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20  he current row, 
a9930 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61  if all on one pa
a9940 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  ge */.};.typedef
a9950 20 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73   struct VdbeCurs
a9960 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a  or VdbeCursor;..
a9970 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62  /*.** When a sub
a9980 2d 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63  -program is exec
a9990 75 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d  uted (OP_Program
a99a0 29 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f  ), a structure o
a99b0 66 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69  f this type.** i
a99c0 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73  s allocated to s
a99d0 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  tore the current
a99e0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72   value of the pr
a99f0 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61  ogram counter, a
a9a00 73 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65  s.** well as the
a9a10 20 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20   current memory 
a9a20 63 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76  cell array and v
a9a30 61 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61  arious other fra
a9a40 6d 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76  me specific.** v
a9a50 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20  alues stored in 
a9a60 74 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e  the Vdbe struct.
a9a70 20 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72   When the sub-pr
a9a80 6f 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65  ogram is finishe
a9a90 64 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c  d, .** these val
a9aa0 75 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62  ues are copied b
a9ab0 61 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20  ack to the Vdbe 
a9ac0 66 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61  from the VdbeFra
a9ad0 6d 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a  me structure,.**
a9ae0 20 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73   restoring the s
a9af0 74 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74  tate of the VM t
a9b00 6f 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f  o as it was befo
a9b10 72 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  re the sub-progr
a9b20 61 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63  am.** began exec
a9b30 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61  uting..**.** Fra
a9b40 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  mes are stored i
a9b50 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
a9b60 68 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70  headed at Vdbe.p
a9b70 50 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61  Parent. Vdbe.pPa
a9b80 72 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70  rent.** is the p
a9b90 61 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72  arent of the cur
a9ba0 72 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a  rent frame, or z
a9bb0 65 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65  ero if the curre
a9bc0 6e 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74  nt frame.** is t
a9bd0 68 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f  he main Vdbe pro
a9be0 67 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  gram..*/.typedef
a9bf0 20 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d   struct VdbeFram
a9c00 65 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72  e VdbeFrame;.str
a9c10 75 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a  uct VdbeFrame {.
a9c20 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
a9c30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20            /* VM 
a9c40 74 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e  this frame belon
a9c50 67 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70  gs to */.  int p
a9c60 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
a9c70 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f     /* Program Co
a9c80 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61  unter */.  Op *a
a9c90 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
a9ca0 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e     /* Program in
a9cb0 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  structions */.  
a9cc0 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20  int nOp;        
a9cd0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
a9ce0 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a  of aOp array */.
a9cf0 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20    Mem *aMem;    
a9d00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
a9d10 61 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  ay of memory cel
a9d20 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  ls */.  int nMem
a9d30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a9d40 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
a9d50 72 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a  ries in aMem */.
a9d60 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61    VdbeCursor **a
a9d70 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65  pCsr;     /* Ele
a9d80 6d 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72  ment of Vdbe cur
a9d90 73 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43  sors */.  u16 nC
a9da0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
a9db0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
a9dc0 6e 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20  ntries in apCsr 
a9dd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e  */.  void *token
a9de0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a9df0 43 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72  Copy of SubProgr
a9e00 61 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  am.token */.  in
a9e10 74 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20  t nChildMem;    
a9e20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a9e30 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
a9e40 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20  for child frame 
a9e50 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43  */.  int nChildC
a9e60 73 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sr;          /* 
a9e70 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
a9e80 73 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d  s for child fram
a9e90 65 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  e */.  i64 lastR
a9ea0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f  owid;          /
a9eb0 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  * Last insert ro
a9ec0 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73  wid (sqlite3.las
a9ed0 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74  tRowid) */.  int
a9ee0 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20   nChange;       
a9ef0 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e       /* Statemen
a9f00 74 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e  t changes (Vdbe.
a9f10 6e 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f  nChanges)     */
a9f20 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50  .  VdbeFrame *pP
a9f30 61 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61  arent;     /* Pa
a9f40 72 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61  rent of this fra
a9f50 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  me */.};..#defin
a9f60 65 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70  e VdbeFrameMem(p
a9f70 29 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20  ) ((Mem *)&((u8 
a9f80 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65  *)p)[ROUND8(size
a9f90 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29  of(VdbeFrame))])
a9fa0 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ../*.** A value 
a9fb0 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63  for VdbeCursor.c
a9fc0 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d  acheValid that m
a9fd0 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69  eans the cache i
a9fe0 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64  s always invalid
a9ff0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43  ..*/.#define CAC
aa000 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a  HE_STALE 0../*.*
aa010 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68  * Internally, th
aa020 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74  e vdbe manipulat
aa030 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51  es nearly all SQ
aa040 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a  L values as Mem.
aa050 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45  ** structures. E
aa060 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d  ach Mem struct m
aa070 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c  ay cache multipl
aa080 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  e representation
aa090 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e  s (string,.** in
aa0a0 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74  teger etc.) of t
aa0b0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20  he same value.  
aa0c0 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65  A value (and the
aa0d0 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63  refore Mem struc
aa0e0 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65  ture).** has the
aa0f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65   following prope
aa100 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63  rties:.**.** Eac
aa110 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61  h value has a ma
aa120 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65  nifest type. The
aa130 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f   manifest type o
aa140 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  f the value stor
aa150 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73  ed.** in a Mem s
aa160 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65  truct is returne
aa170 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65  d by the MemType
aa180 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68  (Mem*) macro. Th
aa190 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65  e type is.** one
aa1a0 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c   of SQLITE_NULL,
aa1b0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c   SQLITE_INTEGER,
aa1c0 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51   SQLITE_REAL, SQ
aa1d0 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20  LITE_TEXT or.** 
aa1e0 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a  SQLITE_BLOB..*/.
aa1f0 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75  struct Mem {.  u
aa200 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69  nion {.    i64 i
aa210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
aa220 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e  * Integer value.
aa230 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72   */.    int nZer
aa240 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  o;          /* U
aa250 73 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d  sed when bit MEM
aa260 5f 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20  _Zero is set in 
aa270 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e  flags */.    Fun
aa280 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20  cDef *pDef;     
aa290 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68   /* Used only wh
aa2a0 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67  en flags==MEM_Ag
aa2b0 67 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20  g */.    RowSet 
aa2c0 2a 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20  *pRowSet;    /* 
aa2d0 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66  Used only when f
aa2e0 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74  lags==MEM_RowSet
aa2f0 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d   */.    VdbeFram
aa300 65 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55  e *pFrame;  /* U
aa310 73 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d  sed when flags==
aa320 4d 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d  MEM_Frame */.  }
aa330 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20   u;.  double r; 
aa340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61            /* Rea
aa350 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c  l value */.  sql
aa360 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
aa370 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74   /* The associat
aa380 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ed database conn
aa390 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72  ection */.  char
aa3a0 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
aa3b0 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f  /* String or BLO
aa3c0 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74  B value */.  int
aa3d0 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
aa3e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
aa3f0 61 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69  aracters in stri
aa400 6e 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64  ng value, exclud
aa410 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31  ing '\0' */.  u1
aa420 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
aa430 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e    /* Some combin
aa440 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c  ation of MEM_Nul
aa450 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f  l, MEM_Str, MEM_
aa460 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75  Dyn, etc. */.  u
aa470 38 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20  8  type;        
aa480 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c     /* One of SQL
aa490 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45  ITE_NULL, SQLITE
aa4a0 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e  _TEXT, SQLITE_IN
aa4b0 54 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20  TEGER, etc */.  
aa4c0 75 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20  u8  enc;        
aa4d0 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54      /* SQLITE_UT
aa4e0 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  F8, SQLITE_UTF16
aa4f0 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  BE, SQLITE_UTF16
aa500 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  LE */.  void (*x
aa510 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f  Del)(void *);  /
aa520 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63  * If not null, c
aa530 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
aa540 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e  n to delete Mem.
aa550 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61  z */.  char *zMa
aa560 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79  lloc;      /* Dy
aa570 6e 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c  namic buffer all
aa580 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
aa590 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b  3_malloc() */.};
aa5a0 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65  ../* One or more
aa5b0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
aa5c0 67 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20  g flags are set 
aa5d0 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20  to indicate the 
aa5e0 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65  validOK.** repre
aa5f0 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  sentations of th
aa600 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
aa610 6e 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74  n the Mem struct
aa620 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d  ..**.** If the M
aa630 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20  EM_Null flag is 
aa640 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61  set, then the va
aa650 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  lue is an SQL NU
aa660 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20  LL value..** No 
aa670 6f 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20  other flags may 
aa680 62 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63  be set in this c
aa690 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ase..**.** If th
aa6a0 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69  e MEM_Str flag i
aa6b0 73 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a  s set then Mem.z
aa6c0 20 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72   points at a str
aa6d0 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
aa6e0 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74  on..** Usually t
aa6f0 68 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69  his is encoded i
aa700 6e 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f  n the same unico
aa710 64 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74  de encoding as t
aa720 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62  he main.** datab
aa730 61 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66  ase (see below f
aa740 6f 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20  or exceptions). 
aa750 49 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20  If the MEM_Term 
aa760 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20  flag is also.** 
aa770 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74  set, then the st
aa780 72 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d  ring is nul term
aa790 69 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f  inated. The MEM_
aa7a0 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
aa7b0 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63   .** flags may c
aa7c0 6f 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20  oexist with the 
aa7d0 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a  MEM_Str flag..**
aa7e0 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20  .** Multiple of 
aa7f0 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e  these values can
aa800 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66   appear in Mem.f
aa810 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20  lags.  But only 
aa820 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65  one.** at a time
aa830 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d   can appear in M
aa840 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66  em.type..*/.#def
aa850 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20  ine MEM_Null    
aa860 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61    0x0001   /* Va
aa870 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23  lue is NULL */.#
aa880 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20  define MEM_Str  
aa890 20 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a       0x0002   /*
aa8a0 20 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69   Value is a stri
aa8b0 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ng */.#define ME
aa8c0 4d 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30  M_Int       0x00
aa8d0 30 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  04   /* Value is
aa8e0 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23   an integer */.#
aa8f0 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20  define MEM_Real 
aa900 20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a       0x0008   /*
aa910 20 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c   Value is a real
aa920 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69   number */.#defi
aa930 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20  ne MEM_Blob     
aa940 20 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c   0x0010   /* Val
aa950 75 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a  ue is a BLOB */.
aa960 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53  #define MEM_RowS
aa970 65 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f  et    0x0020   /
aa980 2a 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77  * Value is a Row
aa990 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  Set object */.#d
aa9a0 65 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20  efine MEM_Frame 
aa9b0 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20      0x0040   /* 
aa9c0 56 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46  Value is a VdbeF
aa9d0 72 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23  rame object */.#
aa9e0 64 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d  define MEM_TypeM
aa9f0 61 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a  ask  0x00ff   /*
aaa00 20 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69   Mask of type bi
aaa10 74 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76  ts */../* Whenev
aaa20 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20  er Mem contains 
aaa30 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f  a valid string o
aaa40 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74  r blob represent
aaa50 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a  ation, one of.**
aaa60 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
aaa70 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74  lags must be set
aaa80 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
aaa90 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
aaaa0 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f  ent.** policy fo
aaab0 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45  r Mem.z.  The ME
aaac0 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c  M_Term flag tell
aaad0 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20  s us whether or 
aaae0 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e  not the.** strin
aaaf0 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30  g is \000 or \u0
aab00 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a  000 terminated.*
aab10 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65  /.#define MEM_Te
aab20 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20  rm      0x0200  
aab30 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69   /* String rep i
aab40 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
aab50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aab60 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30  Dyn       0x0400
aab70 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61     /* Need to ca
aab80 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20  ll sqliteFree() 
aab90 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66  on Mem.z */.#def
aaba0 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20  ine MEM_Static  
aabb0 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65    0x0800   /* Me
aabc0 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  m.z points to a 
aabd0 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
aabe0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68  .#define MEM_Eph
aabf0 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20  em     0x1000   
aac00 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20  /* Mem.z points 
aac10 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  to an ephemeral 
aac20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
aac30 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20  e MEM_Agg       
aac40 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x2000   /* Mem.
aac50 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  z points to an a
aac60 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  gg function cont
aac70 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ext */.#define M
aac80 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34  EM_Zero      0x4
aac90 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63  000   /* Mem.i c
aaca0 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66  ontains count of
aacb0 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   0s appended to 
aacc0 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20  blob */..#ifdef 
aacd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
aace0 42 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45  BLOB.  #undef ME
aacf0 4d 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65  M_Zero.  #define
aad00 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30   MEM_Zero 0x0000
aad10 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
aad20 43 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69  Clear any existi
aad30 6e 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72  ng type flags fr
aad40 6f 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70  om a Mem and rep
aad50 6c 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66  lace them with f
aad60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53  .*/.#define MemS
aad70 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29  etTypeFlag(p, f)
aad80 20 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67   \.   ((p)->flag
aad90 73 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26  s = ((p)->flags&
aada0 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d  ~(MEM_TypeMask|M
aadb0 45 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f  EM_Zero))|f).../
aadc0 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20  * A VdbeFunc is 
aadd0 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28  just a FuncDef (
aade0 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74  defined in sqlit
aadf0 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e  eInt.h) that con
aae00 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f  tains.** additio
aae10 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
aae20 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20  about auxiliary 
aae30 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e  information boun
aae40 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a  d to arguments.*
aae50 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * of the functio
aae60 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  n.  This is used
aae70 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
aae80 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
aae90 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73  xdata().** and s
aaea0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
aaeb0 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20  ta() APIs.  The 
aaec0 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d  "auxdata" is som
aaed0 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
aaee0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
aaef0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
aaf00 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  a constant argum
aaf10 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ent to a functio
aaf20 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f  n.  This.** allo
aaf30 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63  ws functions suc
aaf40 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f  h as "regexp" to
aaf50 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63   compile their c
aaf60 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a  onstant regular.
aaf70 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72  ** expression ar
aaf80 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20  gument once and 
aaf90 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69  reused the compi
aafa0 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c  led code for mul
aafb0 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74  tiple.** invocat
aafc0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ions..*/.struct 
aafd0 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e  VdbeFunc {.  Fun
aafe0 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
aaff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
ab000 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
ab010 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
ab020 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20    int nAux;     
ab030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab040 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
ab050 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ries allocated f
ab060 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20  or apAux[] */.  
ab070 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b  struct AuxData {
ab080 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b  .    void *pAux;
ab090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab0a0 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66     /* Aux data f
ab0b0 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75  or the i-th argu
ab0c0 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64  ment */.    void
ab0d0 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64   (*xDelete)(void
ab0e0 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73   *);      /* Des
ab0f0 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20  tructor for the 
ab100 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20  aux data */.  } 
ab110 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20  apAux[1];       
ab120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
ab130 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68  ne slot for each
ab140 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
ab150 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
ab160 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72  The "context" ar
ab170 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73  gument for a ins
ab180 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f  tallable functio
ab190 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  n.  A pointer to
ab1a0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
ab1b0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
ab1c0 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61  e is the first a
ab1d0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72  rgument to the r
ab1e0 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20  outines used.** 
ab1f0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51  implement the SQ
ab200 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  L functions..**.
ab210 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79  ** There is a ty
ab220 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73  pedef for this s
ab230 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69  tructure in sqli
ab240 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f  te.h.  So all ro
ab250 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20  utines,.** even 
ab260 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  the public inter
ab270 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20  face to SQLite, 
ab280 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65  can use a pointe
ab290 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
ab2a0 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73  ure..** But this
ab2b0 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c   file is the onl
ab2c0 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68  y place where th
ab2d0 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69  e internal detai
ab2e0 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74  ls of this.** st
ab2f0 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77  ructure are know
ab300 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  n..**.** This st
ab310 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e  ructure is defin
ab320 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62  ed inside of vdb
ab330 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69  eInt.h because i
ab340 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74  t uses substruct
ab350 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68  ures.** (Mem) wh
ab360 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66  ich are only def
ab370 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73  ined there..*/.s
ab380 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f  truct sqlite3_co
ab390 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65  ntext {.  FuncDe
ab3a0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
ab3b0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75  /* Pointer to fu
ab3c0 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  nction informati
ab3d0 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52  on.  MUST BE FIR
ab3e0 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63  ST */.  VdbeFunc
ab3f0 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a   *pVdbeFunc;  /*
ab400 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20   Auxilary data, 
ab410 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20  if created. */. 
ab420 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20   Mem s;         
ab430 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65         /* The re
ab440 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74  turn value is st
ab450 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d  ored here */.  M
ab460 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
ab470 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
ab480 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ell used to stor
ab490 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74  e aggregate cont
ab4a0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45  ext */.  int isE
ab4b0 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f  rror;          /
ab4c0 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74  * Error code ret
ab4d0 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e  urned by the fun
ab4e0 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c  ction. */.  Coll
ab4f0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
ab500 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
ab510 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f  equence */.};../
ab520 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63  *.** A Set struc
ab530 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72  ture is used for
ab540 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74   quick testing t
ab550 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65  o see if a value
ab560 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61  .** is part of a
ab570 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74   small set.  Set
ab580 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
ab590 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b  plement code lik
ab5a0 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20  e.** this:.**   
ab5b0 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20           x.y IN 
ab5c0 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d  ('hi','hoo','hum
ab5d0 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  ').*/.typedef st
ab5e0 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74  ruct Set Set;.st
ab5f0 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73  ruct Set {.  Has
ab600 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20  h hash;         
ab610 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20      /* A set is 
ab620 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c  just a hash tabl
ab630 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
ab640 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a  *prev;        /*
ab650 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65   Previously acce
ab660 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e  ssed hash elemen
ab670 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
ab680 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
ab690 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
ab6a0 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
ab6b0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63  e contains the c
ab6c0 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65  omplete.** state
ab6d0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
ab6e0 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  machine..**.** T
ab6f0 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  he "sqlite3_stmt
ab700 22 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  " structure poin
ab710 74 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75  ter that is retu
ab720 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
ab730 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20  compile().** is 
ab740 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72  really a pointer
ab750 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
ab760 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
ab770 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62  e..**.** The Vdb
ab780 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76  e.inVtabMethod v
ab790 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
ab7a0 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74  o non-zero for t
ab7b0 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a  he duration of.*
ab7c0 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61  * any virtual ta
ab7d0 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63  ble method invoc
ab7e0 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74  ations made by t
ab7f0 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e  he vdbe program.
ab800 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f   It is.** set to
ab810 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20   2 for xDestroy 
ab820 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64  method calls and
ab830 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72   1 for all other
ab840 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a   methods. This.*
ab850 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73  * variable is us
ab860 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f  ed for two purpo
ab870 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44  ses: to allow xD
ab880 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74  estroy methods t
ab890 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52  o execute.** "DR
ab8a0 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d  OP TABLE" statem
ab8b0 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76  ents and to prev
ab8c0 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73  ent some nasty s
ab8d0 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a  ide effects of.*
ab8e0 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
ab8f0 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
ab900 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76  invoked recursiv
ab910 65 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c  ely by a virtual
ab920 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f   table .** metho
ab930 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  d function..*/.s
ab940 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73  truct Vdbe {.  s
ab950 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
ab960 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
ab970 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
ab980 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73  n that owns this
ab990 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
ab9a0 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65  Vdbe *pPrev,*pNe
ab9b0 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65  xt;     /* Linke
ab9c0 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20  d list of VDBEs 
ab9d0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64  with the same Vd
ab9e0 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e  be.db */.  int n
ab9f0 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
aba00 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
aba10 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20  instructions in 
aba20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  the program */. 
aba30 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20   int nOpAlloc;  
aba40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
aba50 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
aba60 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20  cated for aOp[] 
aba70 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20  */.  Op *aOp;   
aba80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aba90 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
abaa0 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
abab0 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  e's program */. 
abac0 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20   int nLabel;    
abad0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
abae0 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65  er of labels use
abaf0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65  d */.  int nLabe
abb00 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f  lAlloc;        /
abb10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
abb20 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61  s allocated in a
abb30 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Label[] */.  int
abb40 20 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20   *aLabel;       
abb50 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
abb60 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73   hold the labels
abb70 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72   */.  Mem **apAr
abb80 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  g;            /*
abb90 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75   Arguments to cu
abba0 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
abbb0 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20  g user function 
abbc0 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61  */.  Mem *aColNa
abbd0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  me;          /* 
abbe0 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20  Column names to 
abbf0 72 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20  return */.  Mem 
abc00 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20  *pResultSet;    
abc10 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
abc20 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  o an array of re
abc30 73 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e  sults */.  u16 n
abc40 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  ResColumn;      
abc50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
abc60 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72  columns in one r
abc70 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
abc80 20 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43   set */.  u16 nC
abc90 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
abca0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
abcb0 6c 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20  lots in apCsr[] 
abcc0 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
abcd0 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20  **apCsr;     /* 
abce0 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  One element of t
abcf0 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61  his array for ea
abd00 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a  ch open cursor *
abd10 2f 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69  /.  u8 errorActi
abd20 6f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  on;         /* R
abd30 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74  ecovery action t
abd40 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20  o do in case of 
abd50 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38  an error */.  u8
abd60 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20   okVar;         
abd70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
abd80 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65   azVar[] has bee
abd90 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  n initialized */
abda0 0a 20 20 79 6e 56 61 72 20 6e 56 61 72 3b 20 20  .  ynVar nVar;  
abdb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
abdc0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
abdd0 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d  in aVar[] */.  M
abde0 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20  em *aVar;       
abdf0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
abe00 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69   for the OP_Vari
abe10 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a  able opcode. */.
abe20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20    char **azVar; 
abe30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
abe40 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a  e of variables *
abe50 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20  /.  u32 magic;  
abe60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
abe70 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20  agic number for 
abe80 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20  sanity checking 
abe90 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20  */.  int nMem;  
abea0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
abeb0 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  Number of memory
abec0 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65   locations curre
abed0 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a  ntly allocated *
abee0 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20  /.  Mem *aMem;  
abef0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
abf00 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
abf10 6f 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63  ons */.  u32 cac
abf20 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20  heCtr;          
abf30 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72   /* VdbeCursor r
abf40 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74  ow cache generat
abf50 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ion counter */. 
abf60 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20   int pc;        
abf70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
abf80 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20  program counter 
abf90 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
abfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
abfb0 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
abfc0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  */.  char *zErrM
abfd0 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sg;          /* 
abfe0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  Error message wr
abff0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
ac000 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20  u8 explain;     
ac010 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
ac020 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65  if EXPLAIN prese
ac030 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e  nt on SQL comman
ac040 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65  d */.  u8 change
ac050 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f  CntOn;         /
ac060 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65  * True to update
ac070 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
ac080 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69  ter */.  u8 expi
ac090 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  red;            
ac0a0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
ac0b0 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  VM needs to be r
ac0c0 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75  ecompiled */.  u
ac0d0 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  8 minWriteFileFo
ac0e0 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75  rmat;  /* Minimu
ac0f0 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f  m file format fo
ac100 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62  r writable datab
ac110 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75  ase files */.  u
ac120 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20  8 inVtabMethod; 
ac130 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f         /* See co
ac140 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a  mments above */.
ac150 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75    u8 usesStmtJou
ac160 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75  rnal;     /* Tru
ac170 65 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74  e if uses a stat
ac180 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ement journal */
ac190 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
ac1a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
ac1b0 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79  ue for read-only
ac1c0 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20   statements */. 
ac1d0 20 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b   u8 isPrepareV2;
ac1e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
ac1f0 20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74   if prepared wit
ac200 68 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a  h prepare_v2() *
ac210 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b  /.  int nChange;
ac220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ac230 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e  umber of db chan
ac240 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c  ges made since l
ac250 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69  ast reset */.  i
ac260 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20  nt btreeMask;   
ac270 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73         /* Bitmas
ac280 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65  k of db->aDb[] e
ac290 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65  ntries reference
ac2a0 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74  d */.  i64 start
ac2b0 54 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Time;          /
ac2c0 2a 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72  * Time when quer
ac2d0 79 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64  y started - used
ac2e0 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a   for profiling *
ac2f0 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72  /.  BtreeMutexAr
ac300 72 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41  ray aMutex; /* A
ac310 6e 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65  n array of Btree
ac320 20 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e   used here and n
ac330 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a  eeding locks */.
ac340 20 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32    int aCounter[2
ac350 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  ];        /* Cou
ac360 6e 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71  nters used by sq
ac370 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75  lite3_stmt_statu
ac380 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  s() */.  char *z
ac390 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sql;            
ac3a0 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
ac3b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
ac3c0 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69  at generated thi
ac3d0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72  s */.  void *pFr
ac3e0 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ee;            /
ac3f0 2a 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e  * Free this when
ac400 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64   deleting the vd
ac410 62 65 20 2a 2f 0a 20 20 69 36 34 20 6e 46 6b 43  be */.  i64 nFkC
ac420 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
ac430 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6d 6d  /* Number of imm
ac440 2e 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  . FK constraints
ac450 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 36   this VM */.  i6
ac460 34 20 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 20  4 nStmtDefCons; 
ac470 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ac480 6f 66 20 64 65 66 2e 20 63 6f 6e 73 74 72 61 69  of def. constrai
ac490 6e 74 73 20 77 68 65 6e 20 73 74 6d 74 20 73 74  nts when stmt st
ac4a0 61 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  arted */.  int i
ac4b0 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20  Statement;      
ac4c0 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20     /* Statement 
ac4d0 6e 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20  number (or 0 if 
ac4e0 68 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73  has not opened s
ac4f0 74 6d 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53  tmt) */.#ifdef S
ac500 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49  QLITE_DEBUG.  FI
ac510 4c 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20  LE *trace;      
ac520 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61        /* Write a
ac530 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63  n execution trac
ac540 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e  e here, if not N
ac550 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ULL */.#endif.  
ac560 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d  VdbeFrame *pFram
ac570 65 3b 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e  e;      /* Paren
ac580 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  t frame */.  int
ac590 20 6e 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20   nFrame;        
ac5a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
ac5b0 66 20 66 72 61 6d 65 73 20 69 6e 20 70 46 72 61  f frames in pFra
ac5c0 6d 65 20 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32  me list */.  u32
ac5d0 20 65 78 70 6d 61 73 6b 3b 20 20 20 20 20 20 20   expmask;       
ac5e0 20 20 20 20 20 2f 2a 20 42 69 6e 64 69 6e 67 20       /* Binding 
ac5f0 74 6f 20 74 68 65 73 65 20 76 61 72 73 20 69 6e  to these vars in
ac600 76 61 6c 69 64 61 74 65 73 20 56 4d 20 2a 2f 0a  validates VM */.
ac610 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
ac620 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f  llowing are allo
ac630 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56  wed values for V
ac640 64 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65  dbe.magic.*/.#de
ac650 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f  fine VDBE_MAGIC_
ac660 49 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65  INIT     0x26bce
ac670 61 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69  aa5    /* Buildi
ac680 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  ng a VDBE progra
ac690 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  m */.#define VDB
ac6a0 45 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20  E_MAGIC_RUN     
ac6b0 20 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f   0xbdf20da3    /
ac6c0 2a 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20  * VDBE is ready 
ac6d0 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64  to execute */.#d
ac6e0 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43  efine VDBE_MAGIC
ac6f0 5f 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63  _HALT     0x519c
ac700 32 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20  2973    /* VDBE 
ac710 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78  has completed ex
ac720 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  ecution */.#defi
ac730 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45  ne VDBE_MAGIC_DE
ac740 41 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63  AD     0xb606c3c
ac750 38 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45  8    /* The VDBE
ac760 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f   has been deallo
ac770 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  cated */../*.** 
ac780 46 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79  Function prototy
ac790 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pes.*/.SQLITE_PR
ac7a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac7b0 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
ac7c0 28 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72  (Vdbe *, VdbeCur
ac7d0 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69  sor*);.void sqli
ac7e0 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56  teVdbePopStack(V
ac7f0 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  dbe*,int);.SQLIT
ac800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ac810 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
ac820 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72  oveto(VdbeCursor
ac830 2a 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  *);.#if defined(
ac840 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
ac850 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52   defined(VDBE_PR
ac860 4f 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52  OFILE).SQLITE_PR
ac870 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac880 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49  e3VdbePrintOp(FI
ac890 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a  LE*, int, Op*);.
ac8a0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
ac8b0 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
ac8c0 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
ac8d0 65 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  en(u32);.SQLITE_
ac8e0 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
ac8f0 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
ac900 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51  e(Mem*, int);.SQ
ac910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
ac920 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
ac930 61 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63  alPut(unsigned c
ac940 68 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c  har*, int, Mem*,
ac950 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
ac960 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
ac970 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63  3VdbeSerialGet(c
ac980 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
ac990 61 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b  ar*, u32, Mem*);
ac9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac9b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac9c0 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64  DeleteAuxData(Vd
ac9d0 62 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a  beFunc*, int);..
ac9e0 69 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65  int sqlite2Btree
ac9f0 4b 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72  KeyCompare(BtCur
aca00 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  sor *, const voi
aca10 64 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  d *, int, int, i
aca20 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt *);.SQLITE_PR
aca30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
aca40 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61  3VdbeIdxKeyCompa
aca50 72 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55  re(VdbeCursor*,U
aca60 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69  npackedRecord*,i
aca70 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
aca80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
aca90 56 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c  VdbeIdxRowid(sql
acaa0 69 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20  ite3*, BtCursor 
acab0 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54  *, i64 *);.SQLIT
acac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
acad0 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
acae0 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73  const Mem*, cons
acaf0 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f  t Mem*, const Co
acb00 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f  llSeq*);.SQLITE_
acb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acb20 74 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65  te3VdbeExec(Vdbe
acb30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acb40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acb50 62 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53  beList(Vdbe*);.S
acb60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acb70 74 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c  t sqlite3VdbeHal
acb80 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  t(Vdbe*);.SQLITE
acb90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acba0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
acbb0 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e  coding(Mem *, in
acbc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
acbd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acbe0 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a  beMemTooBig(Mem*
acbf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
acc00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
acc10 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63  eMemCopy(Mem*, c
acc20 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49  onst Mem*);.SQLI
acc30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
acc40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68  sqlite3VdbeMemSh
acc50 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20  allowCopy(Mem*, 
acc60 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29  const Mem*, int)
acc70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
acc80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
acc90 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d  eMemMove(Mem*, M
acca0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
accb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
accc0 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
accd0 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ate(Mem*);.SQLIT
acce0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
accf0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
acd00 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63  tr(Mem*, const c
acd10 68 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76  har*, int, u8, v
acd20 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
acd30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
acd40 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
acd50 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c  emSetInt64(Mem*,
acd60 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   i64);.SQLITE_PR
acd70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
acd80 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62  e3VdbeMemSetDoub
acd90 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29  le(Mem*, double)
acda0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
acdb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
acdc0 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a  eMemSetNull(Mem*
acdd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
acde0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
acdf0 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62  beMemSetZeroBlob
ace00 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  (Mem*,int);.SQLI
ace10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ace20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
ace30 74 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b 0a 53  tRowSet(Mem*);.S
ace40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ace50 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
ace60 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65  MakeWriteable(Me
ace70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
ace80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ace90 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
acea0 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
aceb0 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73  TE_PRIVATE i64 s
acec0 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
aced0 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ue(Mem*);.SQLITE
acee0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acef0 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
acf00 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  erify(Mem*);.SQL
acf10 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62  ITE_PRIVATE doub
acf20 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  le sqlite3VdbeRe
acf30 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53  alValue(Mem*);.S
acf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
acf50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  id sqlite3VdbeIn
acf60 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65  tegerAffinity(Me
acf70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
acf80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acf90 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65  dbeMemRealify(Me
acfa0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
acfb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acfc0 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d  dbeMemNumerify(M
acfd0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
acfe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
acff0 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
ad000 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c 69  (BtCursor*,int,i
ad010 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 51  nt,int,Mem*);.SQ
ad020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
ad030 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
ad040 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 3b  Release(Mem *p);
ad050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ad060 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ad070 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
ad080 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49  al(Mem *p);.SQLI
ad090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ad0a0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e  qlite3VdbeMemFin
ad0b0 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63  alize(Mem*, Func
ad0c0 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Def*);.SQLITE_PR
ad0d0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
ad0e0 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e   *sqlite3OpcodeN
ad0f0 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ame(int);.SQLITE
ad100 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ad110 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
ad120 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e  Mem *pMem, int n
ad130 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b  , int preserve);
ad140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ad150 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
ad160 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64  loseStatement(Vd
ad170 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  be *, int);.SQLI
ad180 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ad190 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65  sqlite3VdbeFrame
ad1a0 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65  Delete(VdbeFrame
ad1b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
ad1c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
ad1d0 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56  beFrameRestore(V
ad1e0 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c  dbeFrame *);.SQL
ad1f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
ad200 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
ad210 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d  toreType(Mem *pM
ad220 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  em);..#ifndef SQ
ad230 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
ad240 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49  N_KEY.SQLITE_PRI
ad250 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ad260 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65  VdbeCheckFk(Vdbe
ad270 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a   *, int);.#else.
ad280 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
ad290 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29  VdbeCheckFk(p,i)
ad2a0 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64   0.#endif..#ifnd
ad2b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
ad2c0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
ad2d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ad2e0 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
ad2f0 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20  ArrayEnter(Vdbe 
ad300 2a 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  *p);.#else.# def
ad310 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ine sqlite3VdbeM
ad320 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70  utexArrayEnter(p
ad330 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ).#endif..SQLITE
ad340 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ad350 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73  ite3VdbeMemTrans
ad360 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a  late(Mem*, u8);.
ad370 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
ad380 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
ad390 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
ad3a0 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64  3VdbePrintSql(Vd
ad3b0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
ad3c0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
ad3d0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
ad3e0 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c  Print(Mem *pMem,
ad3f0 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65   char *zBuf);.#e
ad400 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
ad410 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ad420 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28  dbeMemHandleBom(
ad430 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66  Mem *pMem);..#if
ad440 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
ad450 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45  _INCRBLOB.SQLITE
ad460 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
ad470 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
ad480 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a  andBlob(Mem *);.
ad490 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
ad4a0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
ad4b0 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49  pandBlob(x) SQLI
ad4c0 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65  TE_OK.#endif..#e
ad4d0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
ad4e0 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f  (_VDBEINT_H_) */
ad4f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ad500 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74  * End of vdbeInt
ad510 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
ad520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad540 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
ad550 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
ad560 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
ad570 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  n utf.c ********
ad580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad590 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
ad5a0 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f  E_AMALGAMATION./
ad5b0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
ad5c0 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75  ng constant valu
ad5d0 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  e is used by the
ad5e0 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41   SQLITE_BIGENDIA
ad5f0 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  N and.** SQLITE_
ad600 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63  LITTLEENDIAN mac
ad610 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ros..*/.SQLITE_P
ad620 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74  RIVATE const int
ad630 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b   sqlite3one = 1;
ad640 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
ad650 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a  E_AMALGAMATION *
ad660 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f  /../*.** This lo
ad670 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73  okup table is us
ad680 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64  ed to help decod
ad690 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
ad6a0 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62   of.** a multi-b
ad6b0 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74  yte UTF8 charact
ad6c0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  er..*/.static co
ad6d0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
ad6e0 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61  r sqlite3Utf8Tra
ad6f0 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30  ns1[] = {.  0x00
ad700 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
ad710 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20  03, 0x04, 0x05, 
ad720 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78  0x06, 0x07,.  0x
ad730 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20  08, 0x09, 0x0a, 
ad740 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64  0x0b, 0x0c, 0x0d
ad750 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20  , 0x0e, 0x0f,.  
ad760 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32  0x10, 0x11, 0x12
ad770 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78  , 0x13, 0x14, 0x
ad780 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a  15, 0x16, 0x17,.
ad790 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78    0x18, 0x19, 0x
ad7a0 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20  1a, 0x1b, 0x1c, 
ad7b0 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66  0x1d, 0x1e, 0x1f
ad7c0 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
ad7d0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
ad7e0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
ad7f0 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39  07,.  0x08, 0x09
ad800 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78  , 0x0a, 0x0b, 0x
ad810 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20  0c, 0x0d, 0x0e, 
ad820 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78  0x0f,.  0x00, 0x
ad830 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
ad840 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36  0x04, 0x05, 0x06
ad850 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20  , 0x07,.  0x00, 
ad860 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
ad870 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  , 0x00, 0x01, 0x
ad880 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23  00, 0x00,.};...#
ad890 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46  define WRITE_UTF
ad8a0 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20  8(zOut, c) {    
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8c0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c        \.  if( c<
ad8d0 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20  0x00080 ){      
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad900 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ad910 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20  u8)(c&0xFF);    
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad930 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad950 20 20 20 20 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 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c   \.  else if( c<
ad980 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20  0x00800 ){      
ad990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ad9b0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b  *zOut++ = 0xC0 +
ad9c0 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31   (u8)((c>>6)&0x1
ad9d0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ad9e0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad9f0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63   = 0x80 + (u8)(c
ada00 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
ada10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ada20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
ada30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada50 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66       \.  else if
ada60 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20  ( c<0x10000 ){  
ada70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ada90 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
adaa0 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32  E0 + (u8)((c>>12
adab0 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20  )&0x0F);        
adac0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
adad0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75  ut++ = 0x80 + (u
adae0 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46  8)((c>>6) & 0x3F
adaf0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adb00 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
adb10 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20  0x80 + (u8)(c & 
adb20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x3F);          
adb30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
adb40 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
adb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb70 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
adb80 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63  = 0xF0 + (u8)((c
adb90 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20  >>18) & 0x07);  
adba0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
adbb0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20   *zOut++ = 0x80 
adbc0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26  + (u8)((c>>12) &
adbd0 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
adbe0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
adbf0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28  + = 0x80 + (u8)(
adc00 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20  (c>>6) & 0x3F); 
adc10 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
adc20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
adc30 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33  0 + (u8)(c & 0x3
adc40 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
adc50 20 20 20 20 20 20 5c 0a 20 20 7d 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 20 20 20 20 20 20 20 20                  
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adc90 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54  .}..#define WRIT
adca0 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20  E_UTF16LE(zOut, 
adcb0 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  c) {            
adcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adcd0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
adce0 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20  c<=0xFFFF ){    
adcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
add20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
add30 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
add40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add60 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
add70 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26  + = (u8)((c>>8)&
add80 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adda0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65            \.  }e
addb0 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addf0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ade00 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78  (u8)(((c>>10)&0x
ade10 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31  003F) + (((c-0x1
ade20 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43  0000)>>10)&0x00C
ade30 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75  0));  \.    *zOu
ade40 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44  t++ = (u8)(0x00D
ade50 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30  8 + (((c-0x10000
ade60 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20  )>>18)&0x03));  
ade70 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ade80 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
ade90 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adec0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
aded0 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20  = (u8)(0x00DC + 
adee0 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20  ((c>>8)&0x03)); 
adef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf00 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 20                  
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
adf50 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45  }..#define WRITE
adf60 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63  _UTF16BE(zOut, c
adf70 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) {             
adf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf90 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
adfa0 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20  <=0xFFFF ){     
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 20                  
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
adfe0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
adff0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29  )((c>>8)&0x00FF)
ae000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae020 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ae030 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46   = (u8)(c&0x00FF
ae040 29 3b 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 20                  
ae060 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
ae070 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
ae080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 20 20 20 20 20 5c                 \
ae0b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ae0c0 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28  u8)(0x00D8 + (((
ae0d0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26  c-0x10000)>>18)&
ae0e0 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
ae0f0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ae100 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31  ++ = (u8)(((c>>1
ae110 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28  0)&0x003F) + (((
ae120 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26  c-0x10000)>>10)&
ae130 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20  0x00C0));  \.   
ae140 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
ae150 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29  0x00DC + ((c>>8)
ae160 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae180 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ae190 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b   (u8)(c&0x00FF);
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 5c 0a 20 20 7d 20 20 20 20         \.  }    
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1f0 20 20 20 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 5c 0a 7d               \.}
ae210 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
ae220 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d  TF16LE(zIn, TERM
ae230 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20  , c){           
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae250 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
ae260 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
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 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b  \.  c += ((*zIn+
ae2b0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
ae2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae2e0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
ae2f0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
ae300 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b  xE000 && TERM ){
ae310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae330 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28  \.    int c2 = (
ae340 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20  *zIn++);        
ae350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae370 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32          \.    c2
ae380 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38   += ((*zIn++)<<8
ae390 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ae3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3c0 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78  \.    c = (c2&0x
ae3d0 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30  03FF) + ((c&0x00
ae3e0 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26  3F)<<10) + (((c&
ae3f0 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c  0x03C0)+0x0040)<
ae400 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20  <10);   \.  }   
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae450 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41  \.}..#define REA
ae460 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54  D_UTF16BE(zIn, T
ae470 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20  ERM, c){        
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae490 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63             \.  c
ae4a0 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29   = ((*zIn++)<<8)
ae4b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae4e0 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49     \.  c += (*zI
ae4f0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
ae500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae520 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
ae530 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20  f( c>=0xD800 && 
ae540 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d  c<0xE000 && TERM
ae550 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ae560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae570 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20     \.    int c2 
ae580 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b  = ((*zIn++)<<8);
ae590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae5c0 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b   c2 += (*zIn++);
ae5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae600 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32     \.    c = (c2
ae610 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30  &0x03FF) + ((c&0
ae620 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28  x003F)<<10) + ((
ae630 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34  (c&0x03C0)+0x004
ae640 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d  0)<<10);   \.  }
ae650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae690 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72     \.}../*.** Tr
ae6a0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
ae6b0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
ae6c0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e  .  Return the un
ae6d0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a  icode value..**.
ae6e0 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c  ** During transl
ae6f0 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68  ation, assume th
ae700 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74  at the byte that
ae710 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a   zTerm points.**
ae720 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a   is a 0x00..**.*
ae730 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
ae740 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e  r to the next un
ae750 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69  read byte back i
ae760 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a  nto *pzNext..**.
ae770 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61  ** Notes On Inva
ae780 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a  lid UTF-8:.**.**
ae790 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e    *  This routin
ae7a0 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61  e never allows a
ae7b0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72   7-bit character
ae7c0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30   (0x00 through 0
ae7d0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62  x7f) to.**     b
ae7e0 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d  e encoded as a m
ae7f0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63  ulti-byte charac
ae800 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d  ter.  Any multi-
ae810 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
ae820 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d  hat.**     attem
ae830 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20  pts to encode a 
ae840 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78  value between 0x
ae850 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72  00 and 0x7f is r
ae860 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66  endered as 0xfff
ae870 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69  d..**.**  *  Thi
ae880 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20  s routine never 
ae890 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73  allows a UTF16 s
ae8a0 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74  urrogate value t
ae8b0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a  o be encoded..**
ae8c0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d       If a multi-
ae8d0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61  byte character a
ae8e0 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64  ttempts to encod
ae8f0 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65  e a value betwee
ae900 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20  n.**     0xd800 
ae910 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20  and 0xe000 then 
ae920 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61  it is rendered a
ae930 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20  s 0xfffd..**.** 
ae940 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65   *  Bytes in the
ae950 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74   range of 0x80 t
ae960 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63  hrough 0xbf whic
ae970 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66  h occur as the f
ae980 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65  irst.**     byte
ae990 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
ae9a0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20  are interpreted 
ae9b0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  as single-byte c
ae9c0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20  haracters.**    
ae9d0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73   and rendered as
ae9e0 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e   themselves even
ae9f0 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65   though they are
aea00 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20   technically.** 
aea10 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72      invalid char
aea20 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a  acters..**.**  *
aea30 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
aea40 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69  ccepts an infini
aea50 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66  te number of dif
aea60 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f  ferent UTF8 enco
aea70 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72  dings.**     for
aea80 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20   unicode values 
aea90 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72  0x80 and greater
aeaa0 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61  .  It do not cha
aeab0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a  nge over-length.
aeac0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73  **     encodings
aead0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f   to 0xfffd as so
aeae0 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d  me systems recom
aeaf0 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mend..*/.#define
aeb00 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20   READ_UTF8(zIn, 
aeb10 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20  zTerm, c)       
aeb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb30 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49      \.  c = *(zI
aeb40 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
aeb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb70 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30   \.  if( c>=0xc0
aeb80 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
aeb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
aebb0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
aebc0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30  tf8Trans1[c-0xc0
aebd0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
aebe0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
aebf0 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65   while( zIn!=zTe
aec00 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78  rm && (*zIn & 0x
aec10 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20  c0)==0x80 ){    
aec20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
aec30 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78  c = (c<<6) + (0x
aec40 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20  3f & *(zIn++)); 
aec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aec60 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20       \.    }    
aec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aec80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeca0 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78    \.    if( c<0x
aecb0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  80              
aecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
aece0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
aecf0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38  xFFFFF800)==0xD8
aed00 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
aed10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
aed20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46        || (c&0xFF
aed30 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
aed40 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20  ){  c = 0xFFFD; 
aed50 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53  }        \.  }.S
aed60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
aed70 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  t sqlite3Utf8Rea
aed80 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  d(.  const unsig
aed90 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20  ned char *zIn,  
aeda0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
aedb0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72  te of UTF-8 char
aedc0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  acter */.  const
aedd0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
aede0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72  *pzNext    /* Wr
aedf0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70  ite first byte p
aee00 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68  ast UTF-8 char h
aee10 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
aee20 63 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73  c;..  /* Same as
aee30 20 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f   READ_UTF8() abo
aee40 76 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74  ve but without t
aee50 68 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74  he zTerm paramet
aee60 65 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69  er..  ** For thi
aee70 73 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73  s routine, we as
aee80 73 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74  sume the UTF8 st
aee90 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a  ring is always z
aeea0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a  ero-terminated..
aeeb0 20 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e    */.  c = *(zIn
aeec0 2b 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78  ++);.  if( c>=0x
aeed0 63 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  c0 ){.    c = sq
aeee0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b  lite3Utf8Trans1[
aeef0 63 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69  c-0xc0];.    whi
aef00 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30  le( (*zIn & 0xc0
aef10 29 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20  )==0x80 ){.     
aef20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30   c = (c<<6) + (0
aef30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b  x3f & *(zIn++));
aef40 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
aef50 3c 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c  <0x80.        ||
aef60 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d   (c&0xFFFFF800)=
aef70 3d 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20  =0xD800.        
aef80 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45  || (c&0xFFFFFFFE
aef90 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20  )==0xFFFE ){  c 
aefa0 3d 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a  = 0xFFFD; }.  }.
aefb0 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b    *pzNext = zIn;
aefc0 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
aefd0 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  .../*.** If the 
aefe0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20  TRANSLATE_TRACE 
aeff0 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
af000 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65  , the value of e
af010 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72  ach Mem is.** pr
af020 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20  inted on stderr 
af030 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20  on the way into 
af040 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74  and out of sqlit
af050 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
af060 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65  te()..*/ ./* #de
af070 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54  fine TRANSLATE_T
af080 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64  RACE 1 */..#ifnd
af090 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
af0a0 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  TF16./*.** This 
af0b0 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72  routine transfor
af0c0 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ms the internal 
af0d0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  text encoding us
af0e0 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a  ed by pMem to.**
af0f0 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20   desiredEnc. It 
af100 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74  is an error if t
af110 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72  he string is alr
af120 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69  eady of the desi
af130 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c  red.** encoding,
af140 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65   or if *pMem doe
af150 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  s not contain a 
af160 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f  string value..*/
af170 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
af180 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
af190 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20  emTranslate(Mem 
af1a0 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65  *pMem, u8 desire
af1b0 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e  dEnc){.  int len
af1c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
af1d0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
af1e0 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74  length of output
af1f0 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73   string in bytes
af200 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
af210 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20  har *zOut;      
af220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
af230 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
af240 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
af250 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20  *zIn;           
af260 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74          /* Input
af270 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75   iterator */.  u
af280 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54  nsigned char *zT
af290 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
af2a0 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69       /* End of i
af2b0 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  nput */.  unsign
af2c0 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  ed char *z;     
af2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
af2e0 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74  /* Output iterat
af2f0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  or */.  unsigned
af300 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72   int c;..  asser
af310 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
af320 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
af330 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
af340 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
af350 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  t( pMem->flags&M
af360 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65  EM_Str );.  asse
af370 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64  rt( pMem->enc!=d
af380 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61  esiredEnc );.  a
af390 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63  ssert( pMem->enc
af3a0 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
af3b0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a   pMem->n>=0 );..
af3c0 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e  #if defined(TRAN
af3d0 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20  SLATE_TRACE) && 
af3e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
af3f0 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68  EBUG).  {.    ch
af400 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20  ar zBuf[100];.  
af410 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
af420 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d  PrettyPrint(pMem
af430 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72  , zBuf);.    fpr
af440 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e  intf(stderr, "IN
af450 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75  PUT:  %s\n", zBu
af460 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  f);.  }.#endif..
af470 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e    /* If the tran
af480 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65  slation is betwe
af490 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65  en UTF-16 little
af4a0 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c   and big endian,
af4b0 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20   then .  ** all 
af4c0 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64  that is required
af4d0 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20   is to swap the 
af4e0 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73  byte order. This
af4f0 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64   case is handled
af500 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c  .  ** differentl
af510 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  y from the other
af520 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d  s..  */.  if( pM
af530 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f  em->enc!=SQLITE_
af540 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45  UTF8 && desiredE
af550 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
af560 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a  ){.    u8 temp;.
af570 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
af580 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
af590 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
af5a0 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  (pMem);.    if( 
af5b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
af5c0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
af5d0 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
af5e0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
af5f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
af600 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75    }.    zIn = (u
af610 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20  8*)pMem->z;.    
af620 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65  zTerm = &zIn[pMe
af630 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68  m->n&~1];.    wh
af640 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
af650 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a  {.      temp = *
af660 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20  zIn;.      *zIn 
af670 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20  = *(zIn+1);.    
af680 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a    zIn++;.      *
af690 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20  zIn++ = temp;.  
af6a0 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e    }.    pMem->en
af6b0 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a  c = desiredEnc;.
af6c0 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61      goto transla
af6d0 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  te_out;.  }..  /
af6e0 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65  * Set len to the
af6f0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
af700 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65  of bytes require
af710 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
af720 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28  buffer. */.  if(
af730 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
af740 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
af750 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
af760 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20  ng from UTF-16, 
af770 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77  the maximum grow
af780 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a  th results from.
af790 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69      ** translati
af7a0 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72  ng a 2-byte char
af7b0 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74  acter to a 4-byt
af7c0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
af7d0 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67  r..    ** A sing
af7e0 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69  le byte is requi
af7f0 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70  red for the outp
af800 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a  ut string.    **
af810 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e   nul-terminator.
af820 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d  .    */.    pMem
af830 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c  ->n &= ~1;.    l
af840 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32  en = pMem->n * 2
af850 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   + 1;.  }else{. 
af860 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65     /* When conve
af870 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38  rting from UTF-8
af880 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d   to UTF-16 the m
af890 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73  aximum growth is
af8a0 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77   caused.    ** w
af8b0 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46  hen a 1-byte UTF
af8c0 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20  -8 character is 
af8d0 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20  translated into 
af8e0 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a  a 2-byte UTF-16.
af8f0 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72      ** character
af900 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20  . Two bytes are 
af910 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20  required in the 
af920 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f  output buffer fo
af930 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c  r the.    ** nul
af940 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20  -terminator..   
af950 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d   */.    len = pM
af960 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20  em->n * 2 + 2;. 
af970 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e   }..  /* Set zIn
af980 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
af990 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e   start of the in
af9a0 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a  put buffer and z
af9b0 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a  Term to point 1.
af9c0 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74    ** byte past t
af9d0 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a  he end..  **.  *
af9e0 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20  * Variable zOut 
af9f0 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
afa00 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  at the output bu
afa10 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61  ffer, space obta
afa20 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73  ined.  ** from s
afa30 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
afa40 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75  .  */.  zIn = (u
afa50 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54  8*)pMem->z;.  zT
afa60 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d  erm = &zIn[pMem-
afa70 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71  >n];.  zOut = sq
afa80 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
afa90 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b  (pMem->db, len);
afaa0 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a  .  if( !zOut ){.
afab0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
afac0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a  E_NOMEM;.  }.  z
afad0 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20   = zOut;..  if( 
afae0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  pMem->enc==SQLIT
afaf0 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66  E_UTF8 ){.    if
afb00 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  ( desiredEnc==SQ
afb10 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a  LITE_UTF16LE ){.
afb20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d        /* UTF-8 -
afb30 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d  > UTF-16 Little-
afb40 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20  endian */.      
afb50 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d  while( zIn<zTerm
afb60 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63   ){.        /* c
afb70 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
afb80 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28  ad(zIn, zTerm, (
afb90 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29  const u8**)&zIn)
afba0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41  ; */.        REA
afbb0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
afbc0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57  m, c);.        W
afbd0 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20  RITE_UTF16LE(z, 
afbe0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
afbf0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
afc00 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d  ert( desiredEnc=
afc10 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
afc20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  );.      /* UTF-
afc30 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d  8 -> UTF-16 Big-
afc40 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20  endian */.      
afc50 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d  while( zIn<zTerm
afc60 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63   ){.        /* c
afc70 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
afc80 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28  ad(zIn, zTerm, (
afc90 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29  const u8**)&zIn)
afca0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41  ; */.        REA
afcb0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
afcc0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57  m, c);.        W
afcd0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20  RITE_UTF16BE(z, 
afce0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
afcf0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20  }.    pMem->n = 
afd00 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b  (int)(z - zOut);
afd10 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20  .    *z++ = 0;. 
afd20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
afd30 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d  rt( desiredEnc==
afd40 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20  SQLITE_UTF8 );. 
afd50 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63     if( pMem->enc
afd60 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
afd70 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46   ){.      /* UTF
afd80 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 Little-endia
afd90 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20  n -> UTF-8 */.  
afda0 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a      while( zIn<z
afdb0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
afdc0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e  READ_UTF16LE(zIn
afdd0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b  , zIn<zTerm, c);
afde0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
afdf0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
afe00 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
afe10 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42       /* UTF-16 B
afe20 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46  ig-endian -> UTF
afe30 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  -8 */.      whil
afe40 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
afe50 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
afe60 31 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54  16BE(zIn, zIn<zT
afe70 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20  erm, c); .      
afe80 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20    WRITE_UTF8(z, 
afe90 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
afea0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20  }.    pMem->n = 
afeb0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b  (int)(z - zOut);
afec0 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20  .  }.  *z = 0;. 
afed0 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
afee0 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  n+(desiredEnc==S
afef0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29  QLITE_UTF8?1:2))
aff00 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69  <=len );..  sqli
aff10 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
aff20 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d  e(pMem);.  pMem-
aff30 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
aff40 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d  Static|MEM_Dyn|M
aff50 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65  EM_Ephem);.  pMe
aff60 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64  m->enc = desired
aff70 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  Enc;.  pMem->fla
aff80 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c  gs |= (MEM_Term|
aff90 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d  MEM_Dyn);.  pMem
affa0 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75  ->z = (char*)zOu
affb0 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  t;.  pMem->zMall
affc0 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74  oc = pMem->z;..t
affd0 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69  ranslate_out:.#i
affe0 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c  f defined(TRANSL
afff0 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65  ATE_TRACE) && de
b0000 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
b0010 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72  UG).  {.    char
b0020 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
b0030 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
b0040 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20  ettyPrint(pMem, 
b0050 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e  zBuf);.    fprin
b0060 74 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50  tf(stderr, "OUTP
b0070 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29  UT: %s\n", zBuf)
b0080 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
b0090 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
b00a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
b00b0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f  outine checks fo
b00c0 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d  r a byte-order m
b00d0 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e  ark at the begin
b00e0 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20  ning of the .** 
b00f0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74  UTF-16 string st
b0100 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49  ored in *pMem. I
b0110 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74  f one is present
b0120 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20  , it is removed 
b0130 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64  and.** the encod
b0140 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61  ing of the Mem a
b0150 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f  djusted. This ro
b0160 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64  utine does not d
b0170 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77  o any.** byte-sw
b0180 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20  apping, it just 
b0190 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70  sets Mem.enc app
b01a0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  ropriately..**.*
b01b0 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  * The allocation
b01c0 20 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69   (static, dynami
b01d0 63 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f  c etc.) and enco
b01e0 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20  ding of the Mem 
b01f0 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65  may be.** change
b0200 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
b0210 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
b0220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0230 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f  3VdbeMemHandleBo
b0240 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  m(Mem *pMem){.  
b0250 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
b0260 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30  OK;.  u8 bom = 0
b0270 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
b0280 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28  m->n>=0 );.  if(
b0290 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20   pMem->n>1 ){.  
b02a0 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a    u8 b1 = *(u8 *
b02b0 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38  )pMem->z;.    u8
b02c0 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70   b2 = *(((u8 *)p
b02d0 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20  Mem->z) + 1);.  
b02e0 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26    if( b1==0xFE &
b02f0 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20  & b2==0xFF ){.  
b0300 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45      bom = SQLITE
b0310 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a  _UTF16BE;.    }.
b0320 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46      if( b1==0xFF
b0330 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a   && b2==0xFE ){.
b0340 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49        bom = SQLI
b0350 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20  TE_UTF16LE;.    
b0360 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62  }.  }.  .  if( b
b0370 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  om ){.    rc = s
b0380 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
b0390 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29  eWriteable(pMem)
b03a0 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
b03b0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
b03c0 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20   pMem->n -= 2;. 
b03d0 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65       memmove(pMe
b03e0 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32  m->z, &pMem->z[2
b03f0 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20  ], pMem->n);.   
b0400 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d     pMem->z[pMem-
b0410 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  >n] = '\0';.    
b0420 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
b0430 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  n+1] = '\0';.   
b0440 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
b0450 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  = MEM_Term;.    
b0460 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f    pMem->enc = bo
b0470 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  m;.    }.  }.  r
b0480 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
b0490 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
b04a0 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
b04b0 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20  * pZ is a UTF-8 
b04c0 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20  encoded unicode 
b04d0 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65  string. If nByte
b04e0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
b04f0 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68  ro,.** return th
b0500 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63  e number of unic
b0510 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69  ode characters i
b0520 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20  n pZ up to (but 
b0530 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a  not including).*
b0540 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30  * the first 0x00
b0550 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20   byte. If nByte 
b0560 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e  is not less than
b0570 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68   zero, return th
b0580 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75  e.** number of u
b0590 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72  nicode character
b05a0 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e  s in the first n
b05b0 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75  Byte of pZ (or u
b05c0 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72  p to .** the fir
b05d0 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76  st 0x00, whichev
b05e0 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  er comes first).
b05f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b0600 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
b0610 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20  f8CharLen(const 
b0620 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e  char *zIn, int n
b0630 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d  Byte){.  int r =
b0640 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a   0;.  const u8 *
b0650 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a  z = (const u8*)z
b0660 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  In;.  const u8 *
b0670 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79  zTerm;.  if( nBy
b0680 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65  te>=0 ){.    zTe
b0690 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a  rm = &z[nByte];.
b06a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65    }else{.    zTe
b06b0 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29  rm = (const u8*)
b06c0 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  (-1);.  }.  asse
b06d0 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a  rt( z<=zTerm );.
b06e0 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26    while( *z!=0 &
b06f0 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  & z<zTerm ){.   
b0700 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
b0710 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20  8(z);.    r++;. 
b0720 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
b0730 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66  ../* This test f
b0740 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63  unction is not c
b0750 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79  urrently used by
b0760 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74   the automated t
b0770 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48  est-suite. .** H
b0780 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20  ence it is only 
b0790 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62  available in deb
b07a0 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69  ug builds..*/.#i
b07b0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
b07c0 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65  _TEST) && define
b07d0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
b07e0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
b07f0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a  UTF-8 to UTF-8..
b0800 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74  **.** This has t
b0810 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b  he effect of mak
b0820 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68  ing sure that th
b0830 65 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c  e string is well
b0840 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38  -formed.** UTF-8
b0850 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72  .  Miscoded char
b0860 61 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76  acters are remov
b0870 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72  ed..**.** The tr
b0880 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e  anslation is don
b0890 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63  e in-place (sinc
b08a0 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62  e it is impossib
b08b0 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f  le for the.** co
b08c0 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f  rrect UTF-8 enco
b08d0 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65  ding to be longe
b08e0 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d  r than a malform
b08f0 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f  ed encoding)..*/
b0900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0910 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54  int sqlite3Utf8T
b0920 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  o8(unsigned char
b0930 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e   *zIn){.  unsign
b0940 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20  ed char *zOut = 
b0950 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  zIn;.  unsigned 
b0960 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a  char *zStart = z
b0970 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20  In;.  u32 c;..  
b0980 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b  while( zIn[0] ){
b0990 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
b09a0 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63  Utf8Read(zIn, (c
b09b0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
b09c0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66  .    if( c!=0xff
b09d0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54  fd ){.      WRIT
b09e0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b  E_UTF8(zOut, c);
b09f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f  .    }.  }.  *zO
b0a00 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ut = 0;.  return
b0a10 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53   (int)(zOut - zS
b0a20 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  tart);.}.#endif.
b0a30 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b0a40 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
b0a50 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31   Convert a UTF-1
b0a60 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  6 string in the 
b0a70 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20  native encoding 
b0a80 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72  into a UTF-8 str
b0a90 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ing..** Memory t
b0aa0 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38  o hold the UTF-8
b0ab0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
b0ac0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
b0ad0 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74  _malloc and must
b0ae0 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20  .** be freed by 
b0af0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
b0b00 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c  tion..**.** NULL
b0b10 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
b0b20 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f  there is an allo
b0b30 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f  cation error..*/
b0b40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0b50 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66  char *sqlite3Utf
b0b60 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64  16to8(sqlite3 *d
b0b70 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  b, const void *z
b0b80 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
b0b90 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28  Mem m;.  memset(
b0ba0 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
b0bb0 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a  );.  m.db = db;.
b0bc0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
b0bd0 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42  SetStr(&m, z, nB
b0be0 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  yte, SQLITE_UTF1
b0bf0 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
b0c00 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74  STATIC);.  sqlit
b0c10 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
b0c20 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f  ding(&m, SQLITE_
b0c30 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d  UTF8);.  if( db-
b0c40 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
b0c50 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
b0c60 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
b0c70 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d      m.z = 0;.  }
b0c80 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c  .  assert( (m.fl
b0c90 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21  ags & MEM_Term)!
b0ca0 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
b0cb0 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65  Failed );.  asse
b0cc0 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d  rt( (m.flags & M
b0cd0 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62  EM_Str)!=0 || db
b0ce0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
b0cf0 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c  ;.  return (m.fl
b0d00 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d  ags & MEM_Dyn)!=
b0d10 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65  0 ? m.z : sqlite
b0d20 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e  3DbStrDup(db, m.
b0d30 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  z);.}../*.** Con
b0d40 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72  vert a UTF-8 str
b0d50 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31  ing to the UTF-1
b0d60 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69  6 encoding speci
b0d70 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
b0d80 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e  r.** enc. A poin
b0d90 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73  ter to the new s
b0da0 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
b0db0 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  d, and the value
b0dc0 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73   of *pnOut.** is
b0dd0 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67   set to the leng
b0de0 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e  th of the return
b0df0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74  ed string in byt
b0e00 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f  es. The call sho
b0e10 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74  uld.** arrange t
b0e20 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  o call sqlite3Db
b0e30 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65  Free() on the re
b0e40 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77  turned pointer w
b0e50 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20  hen it is.** no 
b0e60 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e  longer required.
b0e70 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c  .** .** If a mal
b0e80 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
b0e90 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
b0ea0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e  rned and the db.
b0eb0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20  mallocFailed.** 
b0ec0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66  flag set..*/.#if
b0ed0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
b0ee0 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50  E_STAT2.SQLITE_P
b0ef0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
b0f00 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c  ite3Utf8to16(sql
b0f10 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63  ite3 *db, u8 enc
b0f20 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  , char *z, int n
b0f30 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20  , int *pnOut){. 
b0f40 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74   Mem m;.  memset
b0f50 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d  (&m, 0, sizeof(m
b0f60 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b  ));.  m.db = db;
b0f70 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
b0f80 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e  mSetStr(&m, z, n
b0f90 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
b0fa0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
b0fb0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
b0fc0 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c  MemTranslate(&m,
b0fd0 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73   enc) ){.    ass
b0fe0 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
b0ff0 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74  ailed );.    ret
b1000 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
b1010 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c  ert( m.z==m.zMal
b1020 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20  loc );.  *pnOut 
b1030 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20  = m.n;.  return 
b1040 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  m.z;.}.#endif../
b1050 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54  *.** zIn is a UT
b1060 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69  F-16 encoded uni
b1070 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c  code string at l
b1080 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61  east nChar chara
b1090 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52  cters long..** R
b10a0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
b10b0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
b10c0 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69   first nChar uni
b10d0 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a  code characters.
b10e0 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72  ** in pZ.  nChar
b10f0 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67   must be non-neg
b1100 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ative..*/.SQLITE
b1110 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b1120 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
b1130 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e  (const void *zIn
b1140 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20  , int nChar){.  
b1150 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65  int c;.  unsigne
b1160 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20  d char const *z 
b1170 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d  = zIn;.  int n =
b1180 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c   0;.  .  if( SQL
b1190 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
b11a0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
b11b0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c  ){.    while( n<
b11c0 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52  nChar ){.      R
b11d0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31  EAD_UTF16BE(z, 1
b11e0 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b  , c);.      n++;
b11f0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
b1200 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68      while( n<nCh
b1210 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44  ar ){.      READ
b1220 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63  _UTF16LE(z, 1, c
b1230 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20  );.      n++;.  
b1240 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
b1250 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e   (int)(z-(unsign
b1260 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29  ed char const *)
b1270 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  zIn);.}..#if def
b1280 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
b1290 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
b12a0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
b12b0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74  rom the TCL test
b12c0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73   function "trans
b12d0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a  late_selftest"..
b12e0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61  ** It checks tha
b12f0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73  t the primitives
b1300 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67   for serializing
b1310 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69   and deserializi
b1320 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ng.** characters
b1330 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e   in each encodin
b1340 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f  g are inverses o
b1350 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f  f each other..*/
b1360 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b1370 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53  void sqlite3UtfS
b1380 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20  elfTest(void){. 
b1390 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
b13a0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   t;.  unsigned c
b13b0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20  har zBuf[20];.  
b13c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b13d0 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
b13e0 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
b13f0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b1400 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b1410 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b1420 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
b1430 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a  .    n = (int)(z
b1440 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65  -zBuf);.    asse
b1450 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20  rt( n>0 && n<=4 
b1460 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b  );.    z[0] = 0;
b1470 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b1480 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
b1490 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74  f8Read(z, (const
b14a0 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74   u8**)&z);.    t
b14b0 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e   = i;.    if( i>
b14c0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78  =0xD800 && i<=0x
b14d0 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46  DFFF ) t = 0xFFF
b14e0 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78  D;.    if( (i&0x
b14f0 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46  FFFFFFFE)==0xFFF
b1500 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a  E ) t = 0xFFFD;.
b1510 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74      assert( c==t
b1520 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
b1530 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20  (z-zBuf)==n );. 
b1540 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
b1550 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29  0x00110000; i++)
b1560 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44  {.    if( i>=0xD
b1570 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20  800 && i<0xE000 
b1580 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
b1590 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52  z = zBuf;.    WR
b15a0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69  ITE_UTF16LE(z, i
b15b0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  );.    n = (int)
b15c0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73  (z-zBuf);.    as
b15d0 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d  sert( n>0 && n<=
b15e0 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20  4 );.    z[0] = 
b15f0 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  0;.    z = zBuf;
b1600 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c  .    READ_UTF16L
b1610 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20  E(z, 1, c);.    
b1620 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a  assert( c==i );.
b1630 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a      assert( (z-z
b1640 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20  Buf)==n );.  }. 
b1650 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30   for(i=0; i<0x00
b1660 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20  110000; i++){.  
b1670 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20    if( i>=0xD800 
b1680 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f  && i<0xE000 ) co
b1690 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20  ntinue;.    z = 
b16a0 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
b16b0 55 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20  UTF16BE(z, i);. 
b16c0 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a     n = (int)(z-z
b16d0 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Buf);.    assert
b16e0 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b  ( n>0 && n<=4 );
b16f0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20  .    z[0] = 0;. 
b1700 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20     z = zBuf;.   
b1710 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c   READ_UTF16BE(z,
b1720 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65   1, c);.    asse
b1730 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20  rt( c==i );.    
b1740 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29  assert( (z-zBuf)
b1750 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ==n );.  }.}.#en
b1760 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45  dif /* SQLITE_TE
b1770 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ST */.#endif /* 
b1780 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
b1790 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  6 */../*********
b17a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66  ***** End of utf
b17b0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b17e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
b17f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b1800 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   util.c ********
b1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1830 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
b1840 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
b1850 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
b1860 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
b1870 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
b1880 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
b1890 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
b18a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
b18b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
b18c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
b18d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
b18e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
b18f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
b1900 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
b1910 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
b1920 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
b1930 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
b1940 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
b1950 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
b1960 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
b1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b19a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
b19b0 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  Utility function
b19c0 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75  s used throughou
b19d0 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  t sqlite..**.** 
b19e0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
b19f0 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ns functions for
b1a00 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f   allocating memo
b1a10 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a  ry, comparing.**
b1a20 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74   strings, and st
b1a30 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a  uff like that..*
b1a40 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  *.*/.#ifdef SQLI
b1a50 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20  TE_HAVE_ISNAN.# 
b1a60 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e  include <math.h>
b1a70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
b1a80 6f 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f  outine needed to
b1a90 20 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73   support the tes
b1aa0 74 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a  tcase() macro..*
b1ab0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b1ac0 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51  COVERAGE_TEST.SQ
b1ad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b1ae0 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  d sqlite3Coverag
b1af0 65 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74  e(int x){.  stat
b1b00 69 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30  ic int dummy = 0
b1b10 3b 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a  ;.  dummy += x;.
b1b20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
b1b30 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
b1b40 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
b1b50 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61  t value is Not a
b1b60 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a   Number (NaN)..*
b1b70 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74  *.** Use the mat
b1b80 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28  h library isnan(
b1b90 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f  ) function if co
b1ba0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
b1bb0 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a  TE_HAVE_ISNAN..*
b1bc0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20  * Otherwise, we 
b1bd0 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70  have our own imp
b1be0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
b1bf0 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73   works on most s
b1c00 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ystems..*/.SQLIT
b1c10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b1c20 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c  lite3IsNaN(doubl
b1c30 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  e x){.  int rc; 
b1c40 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72    /* The value r
b1c50 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65  eturn */.#if !de
b1c60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56  fined(SQLITE_HAV
b1c70 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20  E_ISNAN).  /*.  
b1c80 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20  ** Systems that 
b1c90 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61  support the isna
b1ca0 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63  n() library func
b1cb0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62  tion should prob
b1cc0 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75  ably.  ** make u
b1cd0 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70  se of it by comp
b1ce0 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c  iling with -DSQL
b1cf0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20  ITE_HAVE_ISNAN. 
b1d00 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a   But we have.  *
b1d10 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e  * found that man
b1d20 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74  y systems do not
b1d30 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20   have a working 
b1d40 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e  isnan() function
b1d50 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d   so.  ** this im
b1d60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
b1d70 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61  provided as an a
b1d80 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a  lternative..  **
b1d90 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74  .  ** This NaN t
b1da0 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61  est sometimes fa
b1db0 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ils if compiled 
b1dc0 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61  on GCC with -ffa
b1dd0 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e  st-math..  ** On
b1de0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
b1df0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61   the use of -ffa
b1e00 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69  st-math comes wi
b1e10 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
b1e20 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20  .  ** warning:. 
b1e30 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68   **.  **      Th
b1e40 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73  is option [-ffas
b1e50 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e  t-math] should n
b1e60 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f  ever be turned o
b1e70 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20  n by any.  **   
b1e80 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e     -O option sin
b1e90 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74  ce it can result
b1ea0 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75   in incorrect ou
b1eb0 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d  tput for program
b1ec0 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63  s.  **      whic
b1ed0 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65  h depend on an e
b1ee0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  xact implementat
b1ef0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49  ion of IEEE or I
b1f00 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75  SO .  **      ru
b1f10 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f  les/specificatio
b1f20 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63  ns for math func
b1f30 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
b1f40 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69   Under MSVC, thi
b1f50 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66  s NaN test may f
b1f60 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ail if compiled 
b1f70 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d  with a floating-
b1f80 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63  .  ** point prec
b1f90 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72  ision mode other
b1fa0 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73   than /fp:precis
b1fb0 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44  e.  From the MSD
b1fc0 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  N .  ** document
b1fd0 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ation:.  **.  **
b1fe0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c        The compil
b1ff0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65  er [with /fp:pre
b2000 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65  cise] will prope
b2010 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61  rly handle compa
b2020 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20  risons .  **    
b2030 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e    involving NaN.
b2040 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20   For example, x 
b2050 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74  != x evaluates t
b2060 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e  o true if x is N
b2070 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e  aN .  **      ..
b2080 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  ..  */.#ifdef __
b2090 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72  FAST_MATH__.# er
b20a0 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ror SQLite will 
b20b0 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  not work correct
b20c0 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61  ly with the -ffa
b20d0 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f  st-math option o
b20e0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20  f GCC..#endif.  
b20f0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20  volatile double 
b2100 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c  y = x;.  volatil
b2110 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a  e double z = y;.
b2120 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23    rc = (y!=z);.#
b2130 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69  else  /* if defi
b2140 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f  ned(SQLITE_HAVE_
b2150 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d  ISNAN) */.  rc =
b2160 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69   isnan(x);.#endi
b2170 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45  f /* SQLITE_HAVE
b2180 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74  _ISNAN */.  test
b2190 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65  case( rc );.  re
b21a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
b21b0 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69  * Compute a stri
b21c0 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69  ng length that i
b21d0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61  s limited to wha
b21e0 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20  t can be stored 
b21f0 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62  in.** lower 30 b
b2200 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20  its of a 32-bit 
b2210 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a  signed integer..
b2220 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
b2230 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65  returned will ne
b2240 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e  ver be negative.
b2250 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76    Nor will it ev
b2260 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a  er be greater.**
b2270 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c   than the actual
b2280 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73   length of the s
b2290 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79  tring.  For very
b22a0 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67   long strings (g
b22b0 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31  reater.** than 1
b22c0 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72  GiB) the value r
b22d0 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65  eturned might be
b22e0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74   less than the t
b22f0 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74  rue string lengt
b2300 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
b2310 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b2320 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63  Strlen30(const c
b2330 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74  har *z){.  const
b2340 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
b2350 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
b2360 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a  rn 0;.  while( *
b2370 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20  z2 ){ z2++; }.  
b2380 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66  return 0x3ffffff
b2390 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a  f & (int)(z2 - z
b23a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
b23b0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
b23c0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
b23d0 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20  rror string for 
b23e0 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61  the sqlite.** ha
b23f0 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65  ndle "db". The e
b2400 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74  rror code is set
b2410 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a   to "err_code"..
b2420 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  **.** If it is n
b2430 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20  ot NULL, string 
b2440 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65  zFormat specifie
b2450 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  s the format of 
b2460 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72  the.** error str
b2470 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65  ing in the style
b2480 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66   of the printf f
b2490 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f  unctions: The fo
b24a0 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61  llowing.** forma
b24b0 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  t characters are
b24c0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
b24d0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73       %s      Ins
b24e0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  ert a string.** 
b24f0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73       %z      A s
b2500 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c  tring that shoul
b2510 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  d be freed after
b2520 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20   use.**      %d 
b2530 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69       Insert an i
b2540 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25  nteger.**      %
b2550 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  T      Insert a 
b2560 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53  token.**      %S
b2570 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65        Insert the
b2580 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
b2590 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a  f a SrcList.**.*
b25a0 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e  * zFormat and an
b25b0 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20  y string tokens 
b25c0 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61  that follow it a
b25d0 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  re assumed to be
b25e0 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55  .** encoded in U
b25f0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63  TF-8..**.** To c
b2600 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65  lear the most re
b2610 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73  cent error for s
b2620 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62  qlite handle "db
b2630 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a  ", sqlite3Error.
b2640 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  ** should be cal
b2650 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64  led with err_cod
b2660 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  e set to SQLITE_
b2670 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73  OK and zFormat s
b2680 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  et.** to NULL..*
b2690 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b26a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
b26b0 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  or(sqlite3 *db, 
b26c0 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f  int err_code, co
b26d0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
b26e0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64  t, ...){.  if( d
b26f0 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c  b && (db->pErr |
b2700 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71  | (db->pErr = sq
b2710 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62  lite3ValueNew(db
b2720 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62  ))!=0) ){.    db
b2730 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f  ->errCode = err_
b2740 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46  code;.    if( zF
b2750 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63  ormat ){.      c
b2760 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61  har *z;.      va
b2770 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20  _list ap;.      
b2780 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
b2790 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d  rmat);.      z =
b27a0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
b27b0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
b27c0 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28  );.      va_end(
b27d0 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ap);.      sqlit
b27e0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
b27f0 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53  ->pErr, -1, z, S
b2800 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
b2810 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  TE_DYNAMIC);.   
b2820 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
b2830 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
b2840 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c  (db->pErr, 0, 0,
b2850 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
b2860 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
b2870 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
b2880 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   Add an error me
b2890 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d  ssage to pParse-
b28a0 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63  >zErrMsg and inc
b28b0 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e  rement pParse->n
b28c0 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  Err..** The foll
b28d0 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67  owing formatting
b28e0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
b28f0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  allowed:.**.**  
b2900 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65      %s      Inse
b2910 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20  rt a string.**  
b2920 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74      %z      A st
b2930 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64  ring that should
b2940 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20   be freed after 
b2950 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20  use.**      %d  
b2960 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e      Insert an in
b2970 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54  teger.**      %T
b2980 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74        Insert a t
b2990 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20  oken.**      %S 
b29a0 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20       Insert the 
b29b0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
b29c0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a   a SrcList.**.**
b29d0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
b29e0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f  hould be used to
b29f0 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f   report any erro
b2a00 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68  r that occurs wh
b2a10 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e  ilst.** compilin
b2a20 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  g an SQL stateme
b2a30 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20  nt (i.e. within 
b2a40 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
b2a50 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20  )). The.** last 
b2a60 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  thing the sqlite
b2a70 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63  3_prepare() func
b2a80 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70  tion does is cop
b2a90 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73  y the error.** s
b2aa0 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75  tored by this fu
b2ab0 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  nction into the 
b2ac0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
b2ad0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72  using sqlite3Err
b2ae0 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f  or()..** Functio
b2af0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  n sqlite3Error()
b2b00 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
b2b10 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  during statement
b2b20 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73   execution.** (s
b2b30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74  qlite3_step() et
b2b40 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  c.)..*/.SQLITE_P
b2b50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b2b60 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73  te3ErrorMsg(Pars
b2b70 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
b2b80 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
b2b90 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
b2ba0 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ap;.  sqlite3 *d
b2bb0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
b2bc0 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
b2bd0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
b2be0 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45  e(db, pParse->zE
b2bf0 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61  rrMsg);.  va_sta
b2c00 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
b2c10 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  .  pParse->zErrM
b2c20 73 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72  sg = sqlite3VMPr
b2c30 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
b2c40 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
b2c50 61 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72  ap);.  pParse->r
b2c60 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
b2c70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
b2c80 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
b2c90 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66  ge in pParse, if
b2ca0 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   any.*/.SQLITE_P
b2cb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b2cc0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61  te3ErrorClear(Pa
b2cd0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
b2ce0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
b2cf0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65  arse->db, pParse
b2d00 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50  ->zErrMsg);.  pP
b2d10 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
b2d20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  0;.  pParse->nEr
b2d30 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r = 0;.}../*.** 
b2d40 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73  Convert an SQL-s
b2d50 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69  tyle quoted stri
b2d60 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c  ng into a normal
b2d70 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76   string by remov
b2d80 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65  ing.** the quote
b2d90 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
b2da0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
b2db0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  done in-place.  
b2dc0 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  If the.** input 
b2dd0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
b2de0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72  ith a quote char
b2df0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73  acter, then this
b2e00 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
b2e10 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   no-op..**.** Th
b2e20 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d  e input string m
b2e30 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d  ust be zero-term
b2e40 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a  inated.  A new z
b2e50 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a  ero-terminator.*
b2e60 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
b2e70 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e  e dequoted strin
b2e80 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  g..**.** The ret
b2e90 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20  urn value is -1 
b2ea0 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20  if no dequoting 
b2eb0 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65  occurs or the le
b2ec0 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ngth of the.** d
b2ed0 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20  equoted string, 
b2ee0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
b2ef0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
b2f00 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64  , if dequoting d
b2f10 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a  oes.** occur..**
b2f20 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a  .** 2002-Feb-14:
b2f30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
b2f40 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d   extended to rem
b2f50 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74  ove MS-Access st
b2f60 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20  yle.** brackets 
b2f70 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e  from around iden
b2f80 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61  tifers.  For exa
b2f90 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22  mple:  "[a-b-c]"
b2fa0 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62   becomes.** "a-b
b2fb0 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  -c"..*/.SQLITE_P
b2fc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b2fd0 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a  e3Dequote(char *
b2fe0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65  z){.  char quote
b2ff0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
b3000 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
b3010 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20  n -1;.  quote = 
b3020 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20  z[0];.  switch( 
b3030 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73  quote ){.    cas
b3040 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a  e '\'':  break;.
b3050 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20      case '"':   
b3060 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
b3070 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20  '`':   break;   
b3080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b3090 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74  For MySQL compat
b30a0 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63  ibility */.    c
b30b0 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65  ase '[':   quote
b30c0 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20   = ']';  break; 
b30d0 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65   /* For MS SqlSe
b30e0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69  rver compatibili
b30f0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c  ty */.    defaul
b3100 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  t:    return -1;
b3110 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20  .  }.  for(i=1, 
b3120 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d  j=0; ALWAYS(z[i]
b3130 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ); i++){.    if(
b3140 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a   z[i]==quote ){.
b3150 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
b3160 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20  ==quote ){.     
b3170 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74     z[j++] = quot
b3180 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  e;.        i++;.
b3190 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
b31a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
b31b0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
b31c0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
b31d0 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
b31e0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75  z[j] = 0;.  retu
b31f0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76  rn j;.}../* Conv
b3200 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e  enient short-han
b3210 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70  d */.#define Upp
b3220 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65  erToLower sqlite
b3230 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f  3UpperToLower../
b3240 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d  *.** Some system
b3250 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29  s have stricmp()
b3260 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73  .  Others have s
b3270 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65  trcasecmp().  Be
b3280 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69  cause.** there i
b3290 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79  s no consistency
b32a0 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65  , we will define
b32b0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c   our own..*/.SQL
b32c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b32d0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
b32e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
b32f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
b3300 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
b3310 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
b3320 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
b3330 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
b3340 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
b3350 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
b3360 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
b3370 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
b3380 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
b3390 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
b33a0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
b33b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b33c0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
b33d0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  [*b];.}.SQLITE_A
b33e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
b33f0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68  trnicmp(const ch
b3400 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74  ar *zLeft, const
b3410 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69   char *zRight, i
b3420 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65  nt N){.  registe
b3430 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
b3440 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
b3450 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
b3460 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
b3470 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
b3480 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d  ght;.  while( N-
b3490 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26  - > 0 && *a!=0 &
b34a0 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
b34b0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
b34c0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
b34d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20   }.  return N<0 
b34e0 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77  ? 0 : UpperToLow
b34f0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f  er[*a] - UpperTo
b3500 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a  Lower[*b];.}../*
b3510 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
b3520 69 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e  if z is a pure n
b3530 75 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20  umeric string.  
b3540 52 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64  Return FALSE and
b3550 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e   leave.** *realn
b3560 75 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20  um unchanged if 
b3570 74 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  the string conta
b3580 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65  ins any characte
b3590 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a  r which is not.*
b35a0 2a 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62  * part of a numb
b35b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
b35c0 20 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20   string is pure 
b35d0 6e 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65  numeric, set *re
b35e0 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66  alnum to TRUE if
b35f0 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63   the string.** c
b3600 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20  ontains the '.' 
b3610 63 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20  character or an 
b3620 22 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78  "E+000" style ex
b3630 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66  ponentiation suf
b3640 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  fix..** Otherwis
b3650 65 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74  e set *realnum t
b3660 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74  o FALSE.  Note t
b3670 68 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20  hat just becaue 
b3680 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66  *realnum is.** f
b3690 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65  alse does not me
b36a0 61 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62  an that the numb
b36b0 65 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73  er can be succes
b36c0 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64  sfully converted
b36d0 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65   into.** an inte
b36e0 67 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62  ger - it might b
b36f0 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a  e too big..**.**
b3700 20 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   An empty string
b3710 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e   is considered n
b3720 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53  on-numeric..*/.S
b3730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b3740 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  t sqlite3IsNumbe
b3750 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  r(const char *z,
b3760 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75   int *realnum, u
b3770 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e  8 enc){.  int in
b3780 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54  cr = (enc==SQLIT
b3790 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69  E_UTF8?1:2);.  i
b37a0 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
b37b0 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20  TF16BE ) z++;.  
b37c0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a  if( *z=='-' || *
b37d0 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e  z=='+' ) z += in
b37e0 63 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  cr;.  if( !sqlit
b37f0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
b3800 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b3810 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a   }.  z += incr;.
b3820 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a    *realnum = 0;.
b3830 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
b3840 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a  Isdigit(*z) ){ z
b3850 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66   += incr; }.  if
b3860 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  ( *z=='.' ){.   
b3870 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20   z += incr;.    
b3880 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69  if( !sqlite3Isdi
b3890 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e  git(*z) ) return
b38a0 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   0;.    while( s
b38b0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b38c0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20  ) ){ z += incr; 
b38d0 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d  }.    *realnum =
b38e0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a   1;.  }.  if( *z
b38f0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27  =='e' || *z=='E'
b3900 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63   ){.    z += inc
b3910 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  r;.    if( *z=='
b3920 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20  +' || *z=='-' ) 
b3930 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69  z += incr;.    i
b3940 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
b3950 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20  it(*z) ) return 
b3960 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71  0;.    while( sq
b3970 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
b3980 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d   ){ z += incr; }
b3990 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20  .    *realnum = 
b39a0 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
b39b0 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  *z==0;.}../*.** 
b39c0 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69  The string z[] i
b39d0 73 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65  s an ASCII repre
b39e0 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72  sentation of a r
b39f0 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43  eal number..** C
b3a00 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69  onvert this stri
b3a10 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a  ng to a double..
b3a20 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
b3a30 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ne assumes that 
b3a40 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20  z[] really is a 
b3a50 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49  valid number.  I
b3a60 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20  f it.** is not, 
b3a70 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e  the result is un
b3a80 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  defined..**.** T
b3a90 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
b3aa0 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74  sed instead of t
b3ab0 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28  he library atof(
b3ac0 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75  ) function becau
b3ad0 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72  se.** the librar
b3ae0 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77  y atof() might w
b3af0 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61  ant to use "," a
b3b00 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  s the decimal po
b3b10 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  int instead.** o
b3b20 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20  f "." depending 
b3b30 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73  on how locale is
b3b40 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20   set.  But that 
b3b50 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62  would cause prob
b3b60 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e  lems.** for SQL.
b3b70 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e    So this routin
b3b80 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e  e always uses ".
b3b90 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  " regardless of 
b3ba0 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  locale..*/.SQLIT
b3bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b3bc0 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
b3bd0 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20  char *z, double 
b3be0 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64  *pResult){.#ifnd
b3bf0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
b3c00 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
b3c10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67  const char *zBeg
b3c20 69 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67  in = z;.  /* sig
b3c30 6e 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20  n * significand 
b3c40 2a 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a  * (10 ^ (esign *
b3c50 20 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20   exponent)) */. 
b3c60 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20   int sign = 1;  
b3c70 20 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e   /* sign of sign
b3c80 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34  ificand */.  i64
b3c90 20 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20   s = 0;      /* 
b3ca0 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20  significand */. 
b3cb0 20 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20   int d = 0;     
b3cc0 20 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e   /* adjust expon
b3cd0 65 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67  ent for shifting
b3ce0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a   decimal point *
b3cf0 2f 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20  /.  int esign = 
b3d00 31 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65  1;  /* sign of e
b3d10 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  xponent */.  int
b3d20 20 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20   e = 0;      /* 
b3d30 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f  exponent */.  do
b3d40 75 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69  uble result;.  i
b3d50 6e 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a  nt nDigits = 0;.
b3d60 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69  .  /* skip leadi
b3d70 6e 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77  ng spaces */.  w
b3d80 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
b3d90 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a  pace(*z) ) z++;.
b3da0 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66    /* get sign of
b3db0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a   significand */.
b3dc0 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b    if( *z=='-' ){
b3dd0 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a  .    sign = -1;.
b3de0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65      z++;.  }else
b3df0 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a   if( *z=='+' ){.
b3e00 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f      z++;.  }.  /
b3e10 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a  * skip leading z
b3e20 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65  eroes */.  while
b3e30 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b  ( z[0]=='0' ) z+
b3e40 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20  +, nDigits++;.. 
b3e50 20 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67   /* copy max sig
b3e60 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20  nificant digits 
b3e70 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a  to significand *
b3e80 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  /.  while( sqlit
b3e90 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26  e3Isdigit(*z) &&
b3ea0 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54   s<((LARGEST_INT
b3eb0 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20  64-9)/10) ){.   
b3ec0 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20   s = s*10 + (*z 
b3ed0 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c  - '0');.    z++,
b3ee0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a   nDigits++;.  }.
b3ef0 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69    /* skip non-si
b3f00 67 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66  gnificant signif
b3f10 69 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a  icand digits.  *
b3f20 2a 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f  * (increase expo
b3f30 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69  nent by d to shi
b3f40 66 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29  ft decimal left)
b3f50 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c   */.  while( sql
b3f60 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
b3f70 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b  ) z++, nDigits++
b3f80 2c 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20  , d++;..  /* if 
b3f90 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73  decimal point is
b3fa0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66   present */.  if
b3fb0 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  ( *z=='.' ){.   
b3fc0 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70   z++;.    /* cop
b3fd0 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66  y digits from af
b3fe0 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73  ter decimal to s
b3ff0 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a  ignificand.    *
b4000 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f  * (decrease expo
b4010 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69  nent by d to shi
b4020 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74  ft decimal right
b4030 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  ) */.    while( 
b4040 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b4050 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53  z) && s<((LARGES
b4060 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29  T_INT64-9)/10) )
b4070 7b 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30  {.      s = s*10
b4080 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20   + (*z - '0');. 
b4090 20 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74       z++, nDigit
b40a0 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a  s++, d--;.    }.
b40b0 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d      /* skip non-
b40c0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
b40d0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  ts */.    while(
b40e0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
b40f0 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69  *z) ) z++, nDigi
b4100 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ts++;.  }..  /* 
b4110 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70  if exponent is p
b4120 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20  resent */.  if( 
b4130 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='e' || *z=='
b4140 45 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  E' ){.    z++;. 
b4150 20 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f     /* get sign o
b4160 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20  f exponent */.  
b4170 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b    if( *z=='-' ){
b4180 0a 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d  .      esign = -
b4190 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  1;.      z++;.  
b41a0 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d    }else if( *z==
b41b0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b  '+' ){.      z++
b41c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63  ;.    }.    /* c
b41d0 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78  opy digits to ex
b41e0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68  ponent */.    wh
b41f0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b4200 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20  git(*z) ){.     
b4210 20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20   e = e*10 + (*z 
b4220 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b  - '0');.      z+
b4230 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
b4240 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65  /* adjust expone
b4250 6e 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64  nt by d, and upd
b4260 61 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20  ate sign */.  e 
b4270 3d 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b  = (e*esign) + d;
b4280 0a 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20  .  if( e<0 ) {. 
b4290 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20     esign = -1;. 
b42a0 20 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20     e *= -1;.  } 
b42b0 65 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e  else {.    esign
b42c0 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
b42d0 69 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64  if 0 significand
b42e0 20 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b   */.  if( !s ) {
b42f0 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49  .    /* In the I
b4300 45 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64  EEE 754 standard
b4310 2c 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64  , zero is signed
b4320 2e 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65  ..    ** Add the
b4330 20 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73   sign if we've s
b4340 65 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  een at least one
b4350 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65   digit */.    re
b4360 73 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26  sult = (sign<0 &
b4370 26 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64  & nDigits) ? -(d
b4380 6f 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c  ouble)0 : (doubl
b4390 65 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  e)0;.  } else {.
b43a0 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74      /* attempt t
b43b0 6f 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e  o reduce exponen
b43c0 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69  t */.    if( esi
b43d0 67 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68  gn>0 ){.      wh
b43e0 69 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f  ile( s<(LARGEST_
b43f0 49 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30  INT64/10) && e>0
b4400 20 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20   ) e--,s*=10;.  
b4410 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77    }else{.      w
b4420 68 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26  hile( !(s%10) &&
b4430 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30   e>0 ) e--,s/=10
b4440 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
b4450 61 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20  adjust the sign 
b4460 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a  of significand *
b4470 2f 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30  /.    s = sign<0
b4480 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20   ? -s : s;..    
b4490 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20  /* if exponent, 
b44a0 73 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e  scale significan
b44b0 64 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  d as appropriate
b44c0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72  .    ** and stor
b44d0 65 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a  e in result. */.
b44e0 20 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20      if( e ){.   
b44f0 20 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20     double scale 
b4500 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20  = 1.0;.      /* 
b4510 61 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c  attempt to handl
b4520 65 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c  e extremely smal
b4530 6c 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20  l/large numbers 
b4540 62 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  better */.      
b4550 69 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33  if( e>307 && e<3
b4560 34 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68  42 ){.        wh
b4570 69 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73  ile( e%308 ) { s
b4580 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20  cale *= 1.0e+1; 
b4590 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20  e -= 1; }.      
b45a0 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b    if( esign<0 ){
b45b0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c  .          resul
b45c0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20  t = s / scale;. 
b45d0 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20           result 
b45e0 2f 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20  /= 1.0e+308;.   
b45f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
b4600 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73        result = s
b4610 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20   * scale;.      
b4620 20 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e      result *= 1.
b4630 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20  0e+308;.        
b4640 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
b4650 20 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32         /* 1.0e+2
b4660 32 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74  2 is the largest
b4670 20 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61   power of 10 tha
b4680 6e 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20  n can be .      
b4690 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64    ** represented
b46a0 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20   exactly. */.   
b46b0 20 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32       while( e%22
b46c0 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e   ) { scale *= 1.
b46d0 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a  0e+1; e -= 1; }.
b46e0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65          while( e
b46f0 3e 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20  >0 ) { scale *= 
b4700 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32  1.0e+22; e -= 22
b4710 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
b4720 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20  esign<0 ){.     
b4730 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20       result = s 
b4740 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20  / scale;.       
b4750 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
b4760 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73    result = s * s
b4770 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  cale;.        }.
b4780 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
b4790 73 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c  se {.      resul
b47a0 74 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20  t = (double)s;. 
b47b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73     }.  }..  /* s
b47c0 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
b47d0 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20  */.  *pResult = 
b47e0 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65  result;..  /* re
b47f0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63  turn number of c
b4800 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a  haracters used *
b4810 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  /.  return (int)
b4820 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65  (z - zBegin);.#e
b4830 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  lse.  return sql
b4840 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52  ite3Atoi64(z, pR
b4850 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f  esult);.#endif /
b4860 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  * SQLITE_OMIT_FL
b4870 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a  OATING_POINT */.
b4880 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
b4890 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65   the 19-characte
b48a0 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67  r string zNum ag
b48b0 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72  ainst the text r
b48c0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a  epresentation.**
b48d0 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32   value 2^63:  92
b48e0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
b48f0 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74  8.  Return negat
b4900 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f  ive, zero, or po
b4910 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75  sitive.** if zNu
b4920 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20  m is less than, 
b4930 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65  equal to, or gre
b4940 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74  ater than the st
b4950 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69  ring..**.** Unli
b4960 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73  ke memcmp() this
b4970 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72   routine is guar
b4980 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e  anteed to return
b4990 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a   the difference.
b49a0 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73  ** in the values
b49b0 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67   of the last dig
b49c0 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64  it if the only d
b49d0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20  ifference is in 
b49e0 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69  the.** last digi
b49f0 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d  t.  So, for exam
b4a00 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ple,.**.**      
b4a10 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39  compare2pow63("9
b4a20 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
b4a30 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20  00").**.** will 
b4a40 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74  return -8..*/.st
b4a50 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65  atic int compare
b4a60 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61  2pow63(const cha
b4a70 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20  r *zNum){.  int 
b4a80 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28  c;.  c = memcmp(
b4a90 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36  zNum,"9223372036
b4aa0 38 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30  85477580",18)*10
b4ab0 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a  ;.  if( c==0 ){.
b4ac0 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d      c = zNum[18]
b4ad0 20 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65   - '8';.  }.  re
b4ae0 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn c;.}.../*.*
b4af0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
b4b00 20 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69   zNum is a 64-bi
b4b10 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
b4b20 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68   and write.** th
b4b30 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
b4b40 6e 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75  nteger into *pNu
b4b50 6d 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e  m.  If zNum is n
b4b60 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  ot an integer.**
b4b70 20 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65   or is an intege
b4b80 72 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61  r that is too la
b4b90 72 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73  rge to be expres
b4ba0 73 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73  sed with 64 bits
b4bb0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e  ,.** then return
b4bc0 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68   false..**.** Wh
b4bd0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
b4be0 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77  was originally w
b4bf0 72 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20  ritten it dealt 
b4c00 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d  with only.** 32-
b4c10 62 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74  bit numbers.  At
b4c20 20 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77   that time, it w
b4c30 61 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74  as much faster t
b4c40 68 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28  han the.** atoi(
b4c50 29 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e  ) library routin
b4c60 65 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e  e in RedHat 7.2.
b4c70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b4c80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
b4c90 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20  oi64(const char 
b4ca0 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d  *zNum, i64 *pNum
b4cb0 29 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a  ){.  i64 v = 0;.
b4cc0 20 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74    int neg;.  int
b4cd0 20 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63   i, c;.  const c
b4ce0 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77  har *zStart;.  w
b4cf0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
b4d00 70 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e  pace(*zNum) ) zN
b4d10 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75  um++;.  if( *zNu
b4d20 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65  m=='-' ){.    ne
b4d30 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b  g = 1;.    zNum+
b4d40 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a  +;.  }else if( *
b4d50 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  zNum=='+' ){.   
b4d60 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e   neg = 0;.    zN
b4d70 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  um++;.  }else{. 
b4d80 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a     neg = 0;.  }.
b4d90 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b    zStart = zNum;
b4da0 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30  .  while( zNum[0
b4db0 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b  ]=='0' ){ zNum++
b4dc0 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72  ; } /* Skip over
b4dd0 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20   leading zeros. 
b4de0 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a  Ticket #2454 */.
b4df0 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e    for(i=0; (c=zN
b4e00 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63  um[i])>='0' && c
b4e10 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20  <='9'; i++){.   
b4e20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20   v = v*10 + c - 
b4e30 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d  '0';.  }.  *pNum
b4e40 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b   = neg ? -v : v;
b4e50 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28  .  if( c!=0 || (
b4e60 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d  i==0 && zStart==
b4e70 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b  zNum) || i>19 ){
b4e80 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20  .    /* zNum is 
b4e90 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e  empty or contain
b4ea0 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65  s non-numeric te
b4eb0 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a  xt or is longer.
b4ec0 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64      ** than 19 d
b4ed0 69 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72  igits (thus guar
b4ee0 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69  anting that it i
b4ef0 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a  s too large) */.
b4f00 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b4f10 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29  }else if( i<19 )
b4f20 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68  {.    /* Less th
b4f30 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f  an 19 digits, so
b4f40 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74   we know that it
b4f50 20 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73   fits in 64 bits
b4f60 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
b4f70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
b4f80 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65  * 19-digit numbe
b4f90 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61  rs must be no la
b4fa0 72 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37  rger than 922337
b4fb0 32 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66  2036854775807 if
b4fc0 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a   positive.    **
b4fd0 20 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35   or 922337203685
b4fe0 34 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74  4775808 if negat
b4ff0 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ive.  Note that 
b5000 39 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35  9223372036854665
b5010 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e  808.    ** is 2^
b5020 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  63. */.    retur
b5030 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  n compare2pow63(
b5040 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d  zNum)<neg;.  }.}
b5050 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69  ../*.** The stri
b5060 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e  ng zNum represen
b5070 74 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69  ts an unsigned i
b5080 6e 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75  nteger.  The zNu
b5090 6d 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73  m string.** cons
b50a0 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d  ists of one or m
b50b0 6f 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63  ore digit charac
b50c0 74 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d  ters and is term
b50d0 69 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a  inated by.** a z
b50e0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20  ero character.  
b50f0 41 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63  Any stray charac
b5100 74 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73  ters in zNum res
b5110 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  ult in undefined
b5120 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  .** behavior..**
b5130 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67  .** If the unsig
b5140 6e 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74  ned integer that
b5150 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73   zNum represents
b5160 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a   will fit in a.*
b5170 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  * 64-bit signed 
b5180 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
b5190 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65  TRUE.  Otherwise
b51a0 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
b51b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46  *.** If the negF
b51c0 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  lag parameter is
b51d0 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e   true, that mean
b51e0 73 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c  s that zNum real
b51f0 6c 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a  ly represents.**
b5200 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
b5210 65 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e  er.  (The leadin
b5220 67 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64  g "-" is omitted
b5230 20 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68   from zNum.)  Th
b5240 69 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  is.** parameter 
b5250 69 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74  is needed to det
b5260 65 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72  ermine a boundar
b5270 79 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e  y case.  A strin
b5280 67 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33  g.** of "9223373
b5290 30 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65  036854775808" re
b52a0 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e  turns false if n
b52b0 65 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20  egFlag is false 
b52c0 6f 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65  or true.** if ne
b52d0 67 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a  gFlag is true..*
b52e0 2a 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72  *.** Leading zer
b52f0 6f 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  os are ignored..
b5300 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b5310 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74  E int sqlite3Fit
b5320 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20  sIn64Bits(const 
b5330 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
b5340 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20  negFlag){.  int 
b5350 69 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30  i;.  int neg = 0
b5360 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75  ;..  assert( zNu
b5370 6d 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75  m[0]>='0' && zNu
b5380 6d 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20  m[0]<='9' ); /* 
b5390 7a 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67  zNum is an unsig
b53a0 6e 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20  ned number */.. 
b53b0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e   if( negFlag ) n
b53c0 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68  eg = 1-neg;.  wh
b53d0 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20  ile( *zNum=='0' 
b53e0 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20  ){.    zNum++;  
b53f0 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67   /* Skip leading
b5400 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20   zeros.  Ticket 
b5410 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66  #2454 */.  }.  f
b5420 6f 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b  or(i=0; zNum[i];
b5430 20 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a   i++){ assert( z
b5440 4e 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a  Num[i]>='0' && z
b5450 4e 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d  Num[i]<='9' ); }
b5460 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20  .  if( i<19 ){. 
b5470 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64     /* Guaranteed
b5480 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20   to fit if less 
b5490 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a  than 19 digits *
b54a0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
b54b0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39    }else if( i>19
b54c0 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61   ){.    /* Guara
b54d0 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20  nteed to be too 
b54e0 62 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74  big if greater t
b54f0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f  han 19 digits */
b5500 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b5510 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43   }else{.    /* C
b5520 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32  ompare against 2
b5530 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ^63. */.    retu
b5540 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33  rn compare2pow63
b5550 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a  (zNum)<neg;.  }.
b5560 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d  }../*.** If zNum
b5570 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69   represents an i
b5580 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c  nteger that will
b5590 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c   fit in 32-bits,
b55a0 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56   then set.** *pV
b55b0 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74  alue to that int
b55c0 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eger and return 
b55d0 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65  true.  Otherwise
b55e0 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a   return false..*
b55f0 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d  *.** Any non-num
b5600 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20  eric characters 
b5610 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a  that following z
b5620 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  Num are ignored.
b5630 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66  .** This is diff
b5640 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74  erent from sqlit
b5650 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68  e3Atoi64() which
b5660 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a   requires the.**
b5670 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f   input number to
b5680 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
b5690 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
b56a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b56b0 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74  e3GetInt32(const
b56c0 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74   char *zNum, int
b56d0 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c   *pValue){.  sql
b56e0 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b  ite_int64 v = 0;
b56f0 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69  .  int i, c;.  i
b5700 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66  nt neg = 0;.  if
b5710 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29  ( zNum[0]=='-' )
b5720 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20  {.    neg = 1;. 
b5730 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c     zNum++;.  }el
b5740 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  se if( zNum[0]==
b5750 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b  '+' ){.    zNum+
b5760 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  +;.  }.  while( 
b5770 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a  zNum[0]=='0' ) z
b5780 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30  Num++;.  for(i=0
b5790 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a  ; i<11 && (c = z
b57a0 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30  Num[i] - '0')>=0
b57b0 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a   && c<=9; i++){.
b57c0 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63      v = v*10 + c
b57d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
b57e0 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20  longest decimal 
b57f0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
b5800 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67  f a 32 bit integ
b5810 65 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a  er is 10 digits:
b5820 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
b5830 20 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39         123456789
b5840 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20  0.  **     2^31 
b5850 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20  -> 2147483648.  
b5860 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b  */.  if( i>10 ){
b5870 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b5880 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32   }.  if( v-neg>2
b5890 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20  147483647 ){.   
b58a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
b58b0 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20   if( neg ){.    
b58c0 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70  v = -v;.  }.  *p
b58d0 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a  Value = (int)v;.
b58e0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
b58f0 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c  *.** The variabl
b5900 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b5910 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20   encoding is as 
b5920 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b  follows:.**.** K
b5930 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41  EY:.**         A
b5940 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37   = 0xxxxxxx    7
b5950 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
b5960 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a  d one flag bit.*
b5970 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78  *         B = 1x
b5980 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73  xxxxxx    7 bits
b5990 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65   of data and one
b59a0 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20   flag bit.**    
b59b0 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78       C = xxxxxxx
b59c0 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64  x    8 bits of d
b59d0 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74  ata.**.**  7 bit
b59e0 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73  s - A.** 14 bits
b59f0 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73   - BA.** 21 bits
b5a00 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74   - BBA.** 28 bit
b5a10 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62  s - BBBA.** 35 b
b5a20 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34  its - BBBBA.** 4
b5a30 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a  2 bits - BBBBBA.
b5a40 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42  ** 49 bits - BBB
b5a50 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20  BBBA.** 56 bits 
b5a60 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34  - BBBBBBBA.** 64
b5a70 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42   bits - BBBBBBBB
b5a80 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  C.*/../*.** Writ
b5a90 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61  e a 64-bit varia
b5aa0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
b5ab0 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61  er to memory sta
b5ac0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
b5ad0 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  * The length of 
b5ae0 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20  data write will 
b5af0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
b5b00 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e   9 bytes.  The n
b5b10 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65  umber.** of byte
b5b20 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74  s written is ret
b5b30 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76  urned..**.** A v
b5b40 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
b5b50 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20  nteger consists 
b5b60 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62  of the lower 7 b
b5b70 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65  its of each byte
b5b80 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65  .** for all byte
b5b90 73 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20  s that have the 
b5ba0 38 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20  8th bit set and 
b5bb0 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68  one byte with th
b5bc0 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65  e 8th.** bit cle
b5bd0 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20  ar.  Except, if 
b5be0 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74  we get to the 9t
b5bf0 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65  h byte, it store
b5c00 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20  s the full.** 8 
b5c10 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20  bits and is the 
b5c20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51  last byte..*/.SQ
b5c30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b5c40 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
b5c50 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t(unsigned char 
b5c60 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e  *p, u64 v){.  in
b5c70 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20  t i, j, n;.  u8 
b5c80 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76  buf[10];.  if( v
b5c90 20 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30   & (((u64)0xff00
b5ca0 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20  0000)<<32) ){.  
b5cb0 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a    p[8] = (u8)v;.
b5cc0 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20      v >>= 8;.   
b5cd0 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20   for(i=7; i>=0; 
b5ce0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d  i--){.      p[i]
b5cf0 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37   = (u8)((v & 0x7
b5d00 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20  f) | 0x80);.    
b5d10 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d    v >>= 7;.    }
b5d20 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20  .    return 9;. 
b5d30 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a   }    .  n = 0;.
b5d40 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b    do{.    buf[n+
b5d50 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30  +] = (u8)((v & 0
b5d60 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  x7f) | 0x80);.  
b5d70 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68    v >>= 7;.  }wh
b5d80 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62  ile( v!=0 );.  b
b5d90 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20  uf[0] &= 0x7f;. 
b5da0 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b   assert( n<=9 );
b5db0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d  .  for(i=0, j=n-
b5dc0 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b  1; j>=0; j--, i+
b5dd0 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62  +){.    p[i] = b
b5de0 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  uf[j];.  }.  ret
b5df0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn n;.}../*.** 
b5e00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
b5e10 61 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e  a faster version
b5e20 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61   of sqlite3PutVa
b5e30 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79  rint() that only
b5e40 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32  .** works for 32
b5e50 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e  -bit positive in
b5e60 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68  tegers and which
b5e70 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f   is optimized fo
b5e80 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  r.** the common 
b5e90 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e  case of small in
b5ea0 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f  tegers.  A MACRO
b5eb0 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72   version, putVar
b5ec0 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f  int32,.** is pro
b5ed0 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69  vided which inli
b5ee0 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  nes the single-b
b5ef0 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63  yte case.  All c
b5f00 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a  ode should use.*
b5f10 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73  * the MACRO vers
b5f20 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63  ion as this func
b5f30 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65  tion assumes the
b5f40 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73   single-byte cas
b5f50 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  e has.** already
b5f60 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a   been handled..*
b5f70 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b5f80 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56   int sqlite3PutV
b5f90 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64  arint32(unsigned
b5fa0 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29   char *p, u32 v)
b5fb0 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72  {.#ifndef putVar
b5fc0 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26  int32.  if( (v &
b5fd0 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20   ~0x7f)==0 ){.  
b5fe0 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20    p[0] = v;.    
b5ff0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
b6000 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20  ndif.  if( (v & 
b6010 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20  ~0x3fff)==0 ){. 
b6020 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28     p[0] = (u8)((
b6030 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20  v>>7) | 0x80);. 
b6040 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76     p[1] = (u8)(v
b6050 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65   & 0x7f);.    re
b6060 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65  turn 2;.  }.  re
b6070 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56  turn sqlite3PutV
b6080 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a  arint(p, v);.}..
b6090 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d  /*.** Read a 64-
b60a0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
b60b0 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
b60c0 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
b60d0 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74   at p[0]..** Ret
b60e0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
b60f0 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54  f bytes read.  T
b6100 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
b6110 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c  ed in *v..*/.SQL
b6120 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
b6130 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
b6140 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
b6150 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b  har *p, u64 *v){
b6160 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20  .  u32 a,b,s;.. 
b6170 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a   a = *p;.  /* a:
b6180 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p0 (unmasked) *
b6190 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30  /.  if (!(a&0x80
b61a0 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20  )).  {.    *v = 
b61b0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  a;.    return 1;
b61c0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62  .  }..  p++;.  b
b61d0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70   = *p;.  /* b: p
b61e0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  1 (unmasked) */.
b61f0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
b6200 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78  .  {.    a &= 0x
b6210 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  7f;.    a = a<<7
b6220 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6230 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b6240 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70  turn 2;.  }..  p
b6250 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
b6260 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b6270 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20   a: p0<<14 | p2 
b6280 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b6290 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b62a0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37   {.    a &= (0x7
b62b0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b62c0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20     b &= 0x7f;.  
b62d0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b62e0 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d  a |= b;.    *v =
b62f0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33   a;.    return 3
b6300 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31  ;.  }..  /* CSE1
b6310 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20   from below */. 
b6320 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b6330 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a  |(0x7f);.  p++;.
b6340 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62    b = b<<14;.  b
b6350 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20   |= *p;.  /* b: 
b6360 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d  p1<<14 | p3 (unm
b6370 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b6380 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b6390 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31     b &= (0x7f<<1
b63a0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f  4)|(0x7f);.    /
b63b0 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20  * moved CSE1 up 
b63c0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28  */.    /* a &= (
b63d0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b63e0 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c  ; */.    a = a<<
b63f0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b6400 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72     *v = a;.    r
b6410 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20  eturn 4;.  }..  
b6420 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* a: p0<<14 | p
b6430 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  2 (masked) */.  
b6440 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
b6450 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
b6460 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20    /* 1:save off 
b6470 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20  p0<<21 | p1<<14 
b6480 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61  | p2<<7 | p3 (ma
b6490 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f  sked) */.  /* mo
b64a0 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20  ved CSE1 up */. 
b64b0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
b64c0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20  14)|(0x7f); */. 
b64d0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
b64e0 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61  |(0x7f);.  s = a
b64f0 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34  ;.  /* s: p0<<14
b6500 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a   | p2 (masked) *
b6510 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  /..  p++;.  a = 
b6520 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
b6530 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38  ;.  /* a: p0<<28
b6540 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28   | p2<<14 | p4 (
b6550 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b6560 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b6570 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20  {.    /* we can 
b6580 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65  skip these cause
b6590 20 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65   they were (effe
b65a0 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62  ctively) done ab
b65b0 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20  ove in calc'ing 
b65c0 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  s */.    /* a &=
b65d0 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37   (0x7f<<28)|(0x7
b65e0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a  f<<14)|(0x7f); *
b65f0 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30  /.    /* b &= (0
b6600 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6610 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37   */.    b = b<<7
b6620 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6630 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20    s = s>>18;.   
b6640 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
b6650 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75  32 | a;.    retu
b6660 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 5;.  }..  /* 
b6670 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32  2:save off p0<<2
b6680 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c  1 | p1<<14 | p2<
b6690 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29  <7 | p3 (masked)
b66a0 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a   */.  s = s<<7;.
b66b0 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73    s |= b;.  /* s
b66c0 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  : p0<<21 | p1<<1
b66d0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
b66e0 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b  masked) */..  p+
b66f0 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a  +;.  b = b<<14;.
b6700 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    b |= *p;.  /* 
b6710 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c  b: p1<<28 | p3<<
b6720 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65  14 | p5 (unmaske
b6730 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b6740 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f  0x80)).  {.    /
b6750 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68  * we can skip th
b6760 69 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20  is cause it was 
b6770 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f  (effectively) do
b6780 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63  ne above in calc
b6790 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a  'ing s */.    /*
b67a0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29   b &= (0x7f<<28)
b67b0 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37  |(0x7f<<14)|(0x7
b67c0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20  f); */.    a &= 
b67d0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b67e0 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  );.    a = a<<7;
b67f0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b6800 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20   s = s>>18;.    
b6810 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b6820 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b6830 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 6;.  }..  p++;
b6840 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20  .  a = a<<14;.  
b6850 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b6860 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34   p2<<28 | p4<<14
b6870 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29   | p6 (unmasked)
b6880 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b6890 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
b68a0 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78  = (0x1f<<28)|(0x
b68b0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b68c0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c      b &= (0x7f<<
b68d0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b68e0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20  b = b<<7;.    a 
b68f0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e  |= b;.    s = s>
b6900 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28  >11;.    *v = ((
b6910 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a  u64)s)<<32 | a;.
b6920 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20      return 7;.  
b6930 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f  }..  /* CSE2 fro
b6940 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26  m below */.  a &
b6950 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b6960 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20  7f);.  p++;.  b 
b6970 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20  = b<<14;.  b |= 
b6980 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c  *p;.  /* b: p3<<
b6990 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37  28 | p5<<14 | p7
b69a0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b69b0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
b69c0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78    {.    b &= (0x
b69d0 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  1f<<28)|(0x7f<<1
b69e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f  4)|(0x7f);.    /
b69f0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20  * moved CSE2 up 
b6a00 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28  */.    /* a &= (
b6a10 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b6a20 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c  ; */.    a = a<<
b6a30 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b6a40 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20     s = s>>4;.   
b6a50 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
b6a60 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75  32 | a;.    retu
b6a70 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 8;.  }..  p++
b6a80 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20  ;.  a = a<<15;. 
b6a90 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61   a |= *p;.  /* a
b6aa0 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31  : p4<<29 | p6<<1
b6ab0 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64  5 | p8 (unmasked
b6ac0 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64  ) */..  /* moved
b6ad0 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a   CSE2 up */.  /*
b6ae0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29   a &= (0x7f<<29)
b6af0 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66  |(0x7f<<15)|(0xf
b6b00 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30  f); */.  b &= (0
b6b10 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6b20 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61  .  b = b<<8;.  a
b6b30 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c   |= b;..  s = s<
b6b40 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b  <4;.  b = p[-4];
b6b50 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20  .  b &= 0x7f;.  
b6b60 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d  b = b>>3;.  s |=
b6b70 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36   b;..  *v = ((u6
b6b80 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20  4)s)<<32 | a;.. 
b6b90 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a   return 9;.}../*
b6ba0 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69  .** Read a 32-bi
b6bb0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
b6bc0 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
b6bd0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
b6be0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72  t p[0]..** Retur
b6bf0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
b6c00 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65  bytes read.  The
b6c10 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64   value is stored
b6c20 20 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66   in *v..**.** If
b6c30 20 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72   the varint stor
b6c40 65 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61  ed in p[0] is la
b6c50 72 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69  rger than can fi
b6c60 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e  t in a 32-bit un
b6c70 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
b6c80 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74  r, then set *v t
b6c90 6f 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a  o 0xffffffff..**
b6ca0 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73  .** A MACRO vers
b6cb0 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32  ion, getVarint32
b6cc0 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68  , is provided wh
b6cd0 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20  ich inlines the 
b6ce0 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  .** single-byte 
b6cf0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20  case.  All code 
b6d00 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d  should use the M
b6d10 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20  ACRO version as 
b6d20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
b6d30 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69  n assumes the si
b6d40 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68  ngle-byte case h
b6d50 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
b6d60 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49  handled..*/.SQLI
b6d70 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71  TE_PRIVATE u8 sq
b6d80 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
b6d90 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
b6da0 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29  char *p, u32 *v)
b6db0 7b 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20  {.  u32 a,b;..  
b6dc0 2f 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61  /* The 1-byte ca
b6dd0 73 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e  se.  Overwhelmin
b6de0 67 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d  gly the most com
b6df0 6d 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e  mon.  Handled in
b6e00 6c 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65  line.  ** by the
b6e10 20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61   getVarin32() ma
b6e20 63 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b  cro */.  a = *p;
b6e30 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d  .  /* a: p0 (unm
b6e40 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65  asked) */.#ifnde
b6e50 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20  f getVarint32.  
b6e60 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b6e70 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b6e80 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31   between 0 and 1
b6e90 32 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61  27 */.    *v = a
b6ea0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
b6eb0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
b6ec0 20 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65   The 2-byte case
b6ed0 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d   */.  p++;.  b =
b6ee0 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20   *p;.  /* b: p1 
b6ef0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b6f00 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
b6f10 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b6f20 20 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64   between 128 and
b6f30 20 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20   16383 */.    a 
b6f40 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d  &= 0x7f;.    a =
b6f50 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20   a<<7;.    *v = 
b6f60 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72  a | b;.    retur
b6f70 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  n 2;.  }..  /* T
b6f80 68 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a  he 3-byte case *
b6f90 2f 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  /.  p++;.  a = a
b6fa0 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<14;.  a |= *p;
b6fb0 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20  .  /* a: p0<<14 
b6fc0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p2 (unmasked) 
b6fd0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
b6fe0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56  0)).  {.    /* V
b6ff0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36  alues between 16
b7000 33 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20  384 and 2097151 
b7010 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37  */.    a &= (0x7
b7020 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b7030 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20     b &= 0x7f;.  
b7040 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b7050 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
b7060 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20  return 3;.  }.. 
b7070 20 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72   /* A 32-bit var
b7080 69 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73  int is used to s
b7090 74 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d  tore size inform
b70a0 61 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e  ation in btrees.
b70b0 0a 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72  .  ** Objects ar
b70c0 65 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20  e rarely larger 
b70d0 74 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20  than 2MiB limit 
b70e0 6f 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69  of a 3-byte vari
b70f0 6e 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74  nt..  ** A 3-byt
b7100 65 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66  e varint is suff
b7110 69 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d  icient, for exam
b7120 70 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74  ple, to record t
b7130 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20  he size.  ** of 
b7140 61 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42  a 1048569-byte B
b7150 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20  LOB or string.. 
b7160 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79   **.  ** We only
b7170 20 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73   unroll the firs
b7180 74 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d  t 1-, 2-, and 3-
b7190 20 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68   byte cases.  Th
b71a0 65 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  e very.  ** rare
b71b0 20 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61   larger cases ca
b71c0 6e 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20  n be handled by 
b71d0 74 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69  the slower 64-bi
b71e0 74 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f  t varint.  ** ro
b71f0 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20  utine..  */.#if 
b7200 31 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36  1.  {.    u64 v6
b7210 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20  4;.    u8 n;..  
b7220 20 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20    p -= 2;.    n 
b7230 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  = sqlite3GetVari
b7240 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20  nt(p, &v64);.   
b7250 20 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20   assert( n>3 && 
b7260 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20  n<=9 );.    if( 
b7270 28 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41  (v64 & SQLITE_MA
b7280 58 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20  X_U32)!=v64 ){. 
b7290 20 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66       *v = 0xffff
b72a0 66 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ffff;.    }else{
b72b0 0a 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32  .      *v = (u32
b72c0 29 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20  )v64;.    }.    
b72d0 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23  return n;.  }..#
b72e0 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f  else.  /* For fo
b72f0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65  llowing code (ke
b7300 70 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  pt for historica
b7310 6c 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73  l record only) s
b7320 68 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72  hows an.  ** unr
b7330 6f 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33  olling for the 3
b7340 2d 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72  - and 4-byte var
b7350 69 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73  int cases.  This
b7360 20 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c   code is.  ** sl
b7370 69 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62  ightly faster, b
b7380 75 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61  ut it is also la
b7390 72 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61  rger and much ha
b73a0 72 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20  rder to test..  
b73b0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  */.  p++;.  b = 
b73c0 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70  b<<14;.  b |= *p
b73d0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34  ;.  /* b: p1<<14
b73e0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29   | p3 (unmasked)
b73f0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b7400 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b7410 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32  Values between 2
b7420 30 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33  097152 and 26843
b7430 35 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d  5455 */.    b &=
b7440 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b7450 66 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78  f);.    a &= (0x
b7460 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b7470 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
b7480 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20    *v = a | b;.  
b7490 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a    return 4;.  }.
b74a0 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b74b0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <14;.  a |= *p;.
b74c0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c    /* a: p0<<28 |
b74d0 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e   p2<<14 | p4 (un
b74e0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b74f0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
b7500 20 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62      /* Walues  b
b7510 65 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36  etween 268435456
b7520 20 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37   and 34359738367
b7530 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78   */.    a &= (0x
b7540 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  1f<<28)|(0x7f<<1
b7550 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b7560 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28   &= (0x1f<<28)|(
b7570 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b7580 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a  ;.    b = b<<7;.
b7590 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a      *v = a | b;.
b75a0 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20      return 5;.  
b75b0 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f  }..  /* We can o
b75c0 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70  nly reach this p
b75d0 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e  oint when readin
b75e0 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  g a corrupt data
b75f0 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20  base.  ** file. 
b7600 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65   In that case we
b7610 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20   are not in any 
b7620 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20  hurry.  Use the 
b7630 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a  (relatively.  **
b7640 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70   slow) general-p
b7650 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65  urpose sqlite3Ge
b7660 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e  tVarint() routin
b7670 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65  e to extract the
b7680 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a  .  ** value. */.
b7690 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b    {.    u64 v64;
b76a0 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20  .    u8 n;..    
b76b0 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20  p -= 4;.    n = 
b76c0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
b76d0 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61  (p, &v64);.    a
b76e0 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c  ssert( n>5 && n<
b76f0 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28  =9 );.    *v = (
b7700 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74  u32)v64;.    ret
b7710 75 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn n;.  }.#endi
b7720 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  f.}../*.** Retur
b7730 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
b7740 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20  bytes that will 
b7750 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f  be needed to sto
b7760 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  re the given.** 
b7770 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a  64-bit integer..
b7780 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b7790 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72  E int sqlite3Var
b77a0 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20  intLen(u64 v){. 
b77b0 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f   int i = 0;.  do
b77c0 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76  {.    i++;.    v
b77d0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
b77e0 28 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53  ( v!=0 && ALWAYS
b77f0 28 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72  (i<9) );.  retur
b7800 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  n i;.}.../*.** R
b7810 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66  ead or write a f
b7820 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64  our-byte big-end
b7830 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ian integer valu
b7840 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
b7850 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
b7860 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75  Get4byte(const u
b7870 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  8 *p){.  return 
b7880 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b  (p[0]<<24) | (p[
b7890 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c  1]<<16) | (p[2]<
b78a0 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51  <8) | p[3];.}.SQ
b78b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b78c0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  d sqlite3Put4byt
b78d0 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  e(unsigned char 
b78e0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b  *p, u32 v){.  p[
b78f0 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29  0] = (u8)(v>>24)
b7900 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28  ;.  p[1] = (u8)(
b7910 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d  v>>16);.  p[2] =
b7920 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70   (u8)(v>>8);.  p
b7930 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a  [3] = (u8)v;.}..
b7940 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
b7950 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
b7960 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69  LITERAL) || defi
b7970 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43  ned(SQLITE_HAS_C
b7980 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  ODEC)./*.** Tran
b7990 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62  slate a single b
b79a0 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20  yte of Hex into 
b79b0 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54  an integer..** T
b79c0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  his routine only
b79d0 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c   works if h real
b79e0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65  ly is a valid he
b79f0 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61  xadecimal.** cha
b7a00 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e  racter:  0..9a..
b7a10 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20  fA..F.*/.static 
b7a20 75 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20  u8 hexToInt(int 
b7a30 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68  h){.  assert( (h
b7a40 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29  >='0' && h<='9')
b7a50 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20   ||  (h>='a' && 
b7a60 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d  h<='f') ||  (h>=
b7a70 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29  'A' && h<='F') )
b7a80 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
b7a90 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28  ASCII.  h += 9*(
b7aa0 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69  1&(h>>6));.#endi
b7ab0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
b7ac0 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a  EBCDIC.  h += 9*
b7ad0 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e  (1&~(h>>4));.#en
b7ae0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38  dif.  return (u8
b7af0 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65  )(h & 0xf);.}.#e
b7b00 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
b7b10 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
b7b20 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f  L || SQLITE_HAS_
b7b30 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64  CODEC */..#if !d
b7b40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
b7b50 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29  IT_BLOB_LITERAL)
b7b60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
b7b70 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a  TE_HAS_CODEC)./*
b7b80 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c  .** Convert a BL
b7b90 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68  OB literal of th
b7ba0 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68  e form "x'hhhhhh
b7bb0 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61  '" into its bina
b7bc0 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65  ry.** value.  Re
b7bd0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
b7be0 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c  o its binary val
b7bf0 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f  ue.  Space to ho
b7c00 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79  ld the.** binary
b7c10 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20   value has been 
b7c20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
b7c30 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65  lloc and must be
b7c40 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65   freed by.** the
b7c50 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65   calling routine
b7c60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b7c70 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
b7c80 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74  3HexToBlob(sqlit
b7c90 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
b7ca0 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
b7cb0 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20   char *zBlob;.  
b7cc0 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20  int i;..  zBlob 
b7cd0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
b7ce0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
b7cf0 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d   n/2 + 1);.  n--
b7d00 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b  ;.  if( zBlob ){
b7d10 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
b7d20 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  n; i+=2){.      
b7d30 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65  zBlob[i/2] = (he
b7d40 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29  xToInt(z[i])<<4)
b7d50 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b   | hexToInt(z[i+
b7d60 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  1]);.    }.    z
b7d70 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20  Blob[i/2] = 0;. 
b7d80 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f   }.  return zBlo
b7d90 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  b;.}.#endif /* !
b7da0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
b7db0 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49  _LITERAL || SQLI
b7dc0 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a  TE_HAS_CODEC */.
b7dd0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
b7de0 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20  he sqlite.magic 
b7df0 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49  from SQLITE_MAGI
b7e00 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45  C_OPEN to SQLITE
b7e10 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20  _MAGIC_BUSY..** 
b7e20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
b7e30 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68  (non-zero) if th
b7e40 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20  e magic was not 
b7e50 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b7e60 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  N.** when this r
b7e70 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b7e80 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
b7e90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
b7ea0 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20  hen entering an 
b7eb0 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65  SQLite API.  The
b7ec0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b7ed0 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69  EN.** value indi
b7ee0 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64  cates that the d
b7ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
b7f00 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74  on passed into t
b7f10 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65  he API is.** ope
b7f20 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69  n and is not bei
b7f30 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68  ng used by anoth
b7f40 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63  er thread.  By c
b7f50 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75  hanging the valu
b7f60 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d  e.** to SQLITE_M
b7f70 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64  AGIC_BUSY we ind
b7f80 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63  icate that the c
b7f90 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
b7fa0 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53  use..** sqlite3S
b7fb0 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77  afetyOff() below
b7fc0 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65   will change the
b7fd0 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53   value back to S
b7fe0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b7ff0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49  .** when the API
b8000 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54   exits. .**.** T
b8010 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
b8020 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65   attempt to dete
b8030 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64  ct if two thread
b8040 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d  s use the.** sam
b8050 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65  e sqlite* pointe
b8060 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  r at the same ti
b8070 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20  me.  There is a 
b8080 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69  race .** conditi
b8090 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73  on so it is poss
b80a0 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72  ible that the er
b80b0 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63  ror is not detec
b80c0 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61  ted..** But usua
b80d0 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  lly the problem 
b80e0 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54  will be seen.  T
b80f0 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62  he result will b
b8100 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68  e an.** error wh
b8110 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
b8120 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70  to debug the app
b8130 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  lication that is
b8140 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65  .** using SQLite
b8150 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a   incorrectly..**
b8160 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a  .** Ticket #202:
b8170 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69    If db->magic i
b8180 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70  s not a valid op
b8190 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63  en value, take c
b81a0 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f  are not.** to mo
b81b0 64 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75  dify the db stru
b81c0 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49  cture at all.  I
b81d0 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20  t could be that 
b81e0 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a  db is a stale.**
b81f0 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74   pointer.  In ot
b8200 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f  her words, it co
b8210 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72  uld be that ther
b8220 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69  e has been a pri
b8230 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  or.** call to sq
b8240 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20  lite3_close(db) 
b8250 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20  and db has been 
b8260 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e  deallocated.  An
b8270 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77  d we do.** not w
b8280 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ant to write int
b8290 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65  o deallocated me
b82a0 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  mory..*/.#ifdef 
b82b0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
b82c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b82d0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
b82e0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b82f0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
b8300 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b8310 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69   ){.    db->magi
b8320 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
b8330 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72  _BUSY;.    asser
b8340 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
b8350 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
b8360 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   );.    return 0
b8370 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62  ;.  }else if( db
b8380 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
b8390 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20  MAGIC_BUSY ){.  
b83a0 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
b83b0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52  LITE_MAGIC_ERROR
b83c0 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49  ;.    db->u1.isI
b83d0 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a  nterrupted = 1;.
b83e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
b83f0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
b8400 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63  Change the magic
b8410 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47   from SQLITE_MAG
b8420 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54  IC_BUSY to SQLIT
b8430 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a  E_MAGIC_OPEN..**
b8440 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
b8450 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74   (non-zero) if t
b8460 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74  he magic was not
b8470 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b8480 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  SY.** when this 
b8490 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
b84a0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
b84b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
b84c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b84d0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71  ite3SafetyOff(sq
b84e0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66  lite3 *db){.  if
b84f0 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c  ( db->magic==SQL
b8500 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29  ITE_MAGIC_BUSY )
b8510 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20  {.    db->magic 
b8520 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  = SQLITE_MAGIC_O
b8530 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  PEN;.    assert(
b8540 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
b8550 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
b8560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
b8570 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d    }else{.    db-
b8580 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
b8590 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20  MAGIC_ERROR;.   
b85a0 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
b85b0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72  upted = 1;.    r
b85c0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23  eturn 1;.  }.}.#
b85d0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  endif../*.** Che
b85e0 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
b85f0 77 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20  we have a valid 
b8600 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69  db pointer.  Thi
b8610 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a  s test is not.**
b8620 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69   foolproof but i
b8630 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73  t does provide s
b8640 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70  ome measure of p
b8650 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73  rotection agains
b8660 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74  t.** misuse of t
b8670 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63  he interface suc
b8680 68 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20  h as passing in 
b8690 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74  db pointers that
b86a0 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20   are.** NULL or 
b86b0 77 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20  which have been 
b86c0 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65  previously close
b86d0 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74  d.  If this rout
b86e0 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31  ine returns.** 1
b86f0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
b8700 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73  he db pointer is
b8710 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20   valid and 0 if 
b8720 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  it should not be
b8730 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64  .** dereferenced
b8740 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e   for any reason.
b8750 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
b8760 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e  nction should in
b8770 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  voke.** SQLITE_M
b8780 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c  ISUSE immediatel
b8790 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  y..**.** sqlite3
b87a0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20  SafetyCheckOk() 
b87b0 72 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68  requires that th
b87c0 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20  e db pointer be 
b87d0 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65  valid for.** use
b87e0 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  .  sqlite3Safety
b87f0 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20  CheckSickOrOk() 
b8800 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e  allows a db poin
b8810 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20  ter that failed 
b8820 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65  to.** open prope
b8830 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66  rly and is not f
b8840 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75  it for general u
b8850 73 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e  se but which can
b8860 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61   be.** used as a
b8870 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  n argument to sq
b8880 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f  lite3_errmsg() o
b8890 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28  r sqlite3_close(
b88a0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
b88b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b88c0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71  SafetyCheckOk(sq
b88d0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33  lite3 *db){.  u3
b88e0 32 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64  2 magic;.  if( d
b88f0 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  b==0 ) return 0;
b8900 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d  .  magic = db->m
b8910 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69  agic;.  if( magi
b8920 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
b8930 4f 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c  OPEN .#ifdef SQL
b8940 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26  ITE_DEBUG.     &
b8950 26 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f  & magic!=SQLITE_
b8960 4d 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69  MAGIC_BUSY.#endi
b8970 66 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  f.  ){.    retur
b8980 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 0;.  }else{.  
b8990 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
b89a0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
b89b0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
b89c0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
b89d0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b89e0 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67  u32 magic;.  mag
b89f0 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a  ic = db->magic;.
b8a00 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c    if( magic!=SQL
b8a10 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26  ITE_MAGIC_SICK &
b8a20 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53  &.      magic!=S
b8a30 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b8a40 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21   &&.      magic!
b8a50 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55  =SQLITE_MAGIC_BU
b8a60 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  SY ) return 0;. 
b8a70 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
b8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b8a90 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a  d of util.c ****
b8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b8ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b8ae0 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20  gin file hash.c 
b8af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b8b20 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
b8b30 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 22.**.** The 
b8b40 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
b8b50 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
b8b60 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
b8b70 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
b8b80 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
b8b90 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
b8ba0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
b8bb0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
b8bc0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
b8bd0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
b8be0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
b8bf0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
b8c00 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
b8c10 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
b8c20 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
b8c30 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
b8c40 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
b8c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c90 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
b8ca0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
b8cb0 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61  on of generic ha
b8cc0 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65  sh-tables.** use
b8cd0 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  d in SQLite..*/.
b8ce0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65  ./* Turn bulk me
b8cf0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68  mory into a hash
b8d00 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79   table object by
b8d10 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
b8d20 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74  e.** fields of t
b8d30 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72  he Hash structur
b8d40 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20  e..**.** "pNew" 
b8d50 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
b8d60 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
b8d70 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69  hat is to be ini
b8d80 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c  tialized..*/.SQL
b8d90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b8da0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
b8db0 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20  (Hash *pNew){.  
b8dc0 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20  assert( pNew!=0 
b8dd0 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74  );.  pNew->first
b8de0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f   = 0;.  pNew->co
b8df0 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  unt = 0;.  pNew-
b8e00 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70  >htsize = 0;.  p
b8e10 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a  New->ht = 0;.}..
b8e20 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e  /* Remove all en
b8e30 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73  tries from a has
b8e40 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69  h table.  Reclai
b8e50 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  m all memory..**
b8e60 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
b8e70 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68  ne to delete a h
b8e80 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20  ash table or to 
b8e90 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62  reset a hash tab
b8ea0 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  le.** to the emp
b8eb0 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  ty state..*/.SQL
b8ec0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b8ed0 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
b8ee0 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48  r(Hash *pH){.  H
b8ef0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
b8f00 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
b8f10 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65  oping over all e
b8f20 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74  lements of the t
b8f30 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  able */..  asser
b8f40 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c  t( pH!=0 );.  el
b8f50 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a  em = pH->first;.
b8f60 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b    pH->first = 0;
b8f70 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
b8f80 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68  pH->ht);.  pH->h
b8f90 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73  t = 0;.  pH->hts
b8fa0 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  ize = 0;.  while
b8fb0 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61  ( elem ){.    Ha
b8fc0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65  shElem *next_ele
b8fd0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
b8fe0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
b8ff0 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d  (elem);.    elem
b9000 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20   = next_elem;.  
b9010 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  }.  pH->count = 
b9020 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
b9030 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e  hashing function
b9040 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69  ..*/.static unsi
b9050 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68  gned int strHash
b9060 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
b9070 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74  int nKey){.  int
b9080 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74   h = 0;.  assert
b9090 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77  ( nKey>=0 );.  w
b90a0 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20  hile( nKey > 0  
b90b0 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33  ){.    h = (h<<3
b90c0 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55  ) ^ h ^ sqlite3U
b90d0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
b90e0 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d  igned char)*z++]
b90f0 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20  ;.    nKey--;.  
b9100 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a  }.  return h;.}.
b9110 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65  ../* Link pNew e
b9120 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
b9130 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
b9140 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65  If pEntry!=0 the
b9150 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74  n also.** insert
b9160 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70   pNew into the p
b9170 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65  Entry hash bucke
b9180 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
b9190 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28  d insertElement(
b91a0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20  .  Hash *pH,    
b91b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
b91c0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
b91d0 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  able */.  struct
b91e0 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20   _ht *pEntry,   
b91f0 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e   /* The entry in
b9200 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73  to which pNew is
b9210 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48   inserted */.  H
b9220 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20  ashElem *pNew   
b9230 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65        /* The ele
b9240 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72  ment to be inser
b9250 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68  ted */.){.  Hash
b9260 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20  Elem *pHead;    
b9270 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d     /* First elem
b9280 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70  ent already in p
b9290 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70  Entry */.  if( p
b92a0 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65  Entry ){.    pHe
b92b0 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75  ad = pEntry->cou
b92c0 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61  nt ? pEntry->cha
b92d0 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74  in : 0;.    pEnt
b92e0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20  ry->count++;.   
b92f0 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
b9300 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pNew;.  }else{.
b9310 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20      pHead = 0;. 
b9320 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29   }.  if( pHead )
b9330 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74  {.    pNew->next
b9340 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e   = pHead;.    pN
b9350 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64  ew->prev = pHead
b9360 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20  ->prev;.    if( 
b9370 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70  pHead->prev ){ p
b9380 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  Head->prev->next
b9390 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65   = pNew; }.    e
b93a0 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
b93b0 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e  { pH->first = pN
b93c0 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d  ew; }.    pHead-
b93d0 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20  >prev = pNew;.  
b93e0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
b93f0 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73  >next = pH->firs
b9400 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66  t;.    if( pH->f
b9410 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73  irst ){ pH->firs
b9420 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20  t->prev = pNew; 
b9430 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76  }.    pNew->prev
b9440 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69   = 0;.    pH->fi
b9450 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  rst = pNew;.  }.
b9460 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
b9470 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
b9480 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
b9490 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
b94a0 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68  ets..**.** The h
b94b0 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ash table might 
b94c0 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69  fail to resize i
b94d0 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
b94e0 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69  () fails or.** i
b94f0 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69  f the new size i
b9500 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
b9510 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a  e prior size..**
b9520 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
b9530 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72  the resize occur
b9540 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e  s and false if n
b9550 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ot..*/.static in
b9560 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70  t rehash(Hash *p
b9570 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  H, unsigned int 
b9580 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72  new_size){.  str
b9590 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b  uct _ht *new_ht;
b95a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
b95b0 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c  he new hash tabl
b95c0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
b95d0 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65  *elem, *next_ele
b95e0 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  m;    /* For loo
b95f0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
b9600 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a  ng elements */..
b9610 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  #if SQLITE_MALLO
b9620 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20  C_SOFT_LIMIT>0. 
b9630 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69   if( new_size*si
b9640 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b9650 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53  >SQLITE_MALLOC_S
b9660 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20  OFT_LIMIT ){.   
b9670 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49   new_size = SQLI
b9680 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c  TE_MALLOC_SOFT_L
b9690 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75  IMIT/sizeof(stru
b96a0 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69  ct _ht);.  }.  i
b96b0 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d  f( new_size==pH-
b96c0 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e  >htsize ) return
b96d0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a   0;.#endif..  /*
b96e0 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74   The inability t
b96f0 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63  o allocates spac
b9700 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68  e for a larger h
b9710 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a  ash table is.  *
b9720 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  * a performance 
b9730 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f  hit but it is no
b9740 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e  t a fatal error.
b9750 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20    So mark the.  
b9760 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73  ** allocation as
b9770 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a   a benign..  */.
b9780 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
b9790 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
b97a0 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74  new_ht = (struct
b97b0 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61   _ht *)sqlite3Ma
b97c0 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73  lloc( new_size*s
b97d0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
b97e0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e  ) );.  sqlite3En
b97f0 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
b9800 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d  ..  if( new_ht==
b9810 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
b9820 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d  sqlite3_free(pH-
b9830 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
b9840 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68   new_ht;.  pH->h
b9850 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65  tsize = new_size
b9860 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
b9870 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a  Size(new_ht)/siz
b9880 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b  eof(struct _ht);
b9890 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74  .  memset(new_ht
b98a0 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69  , 0, new_size*si
b98b0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b98c0 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48  );.  for(elem=pH
b98d0 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72  ->first, pH->fir
b98e0 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d  st=0; elem; elem
b98f0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20   = next_elem){. 
b9900 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
b9910 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d  h = strHash(elem
b9920 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b  ->pKey, elem->nK
b9930 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a  ey) % new_size;.
b9940 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20      next_elem = 
b9950 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
b9960 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48  insertElement(pH
b9970 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c  , &new_ht[h], el
b9980 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  em);.  }.  retur
b9990 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  n 1;.}../* This 
b99a0 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e  function (for in
b99b0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29  ternal use only)
b99c0 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d   locates an elem
b99d0 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73  ent in an.** has
b99e0 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  h table that mat
b99f0 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b  ches the given k
b9a00 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f  ey.  The hash fo
b9a10 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a  r this key has.*
b9a20 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  * already been c
b9a30 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70  omputed and is p
b9a40 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68  assed as the 4th
b9a50 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73   parameter..*/.s
b9a60 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a  tatic HashElem *
b9a70 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e  findElementGiven
b9a80 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61  Hash(.  const Ha
b9a90 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54  sh *pH,     /* T
b9aa0 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72  he pH to be sear
b9ab0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ched */.  const 
b9ac0 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  char *pKey,   /*
b9ad0 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20   The key we are 
b9ae0 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
b9af0 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
b9b00 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
b9b10 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e  in key (not coun
b9b20 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e  ting zero termin
b9b30 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67  ator) */.  unsig
b9b40 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f  ned int h      /
b9b50 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
b9b60 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
b9b70 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b9b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9b90 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
b9ba0 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
b9bb0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
b9bc0 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
b9bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
b9be0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
b9bf0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
b9c00 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b  .  if( pH->ht ){
b9c10 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20  .    struct _ht 
b9c20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68  *pEntry = &pH->h
b9c30 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d  t[h];.    elem =
b9c40 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a   pEntry->chain;.
b9c50 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74      count = pEnt
b9c60 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c  ry->count;.  }el
b9c70 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70  se{.    elem = p
b9c80 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f  H->first;.    co
b9c90 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b  unt = pH->count;
b9ca0 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f  .  }.  while( co
b9cb0 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28  unt-- && ALWAYS(
b9cc0 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28  elem) ){.    if(
b9cd0 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65   elem->nKey==nKe
b9ce0 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  y && sqlite3StrN
b9cf0 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c  ICmp(elem->pKey,
b9d00 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b  pKey,nKey)==0 ){
b9d10 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65   .      return e
b9d20 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  lem;.    }.    e
b9d30 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  lem = elem->next
b9d40 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
b9d50 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
b9d60 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72   single entry fr
b9d70 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
b9d80 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65  e given a pointe
b9d90 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65  r to that.** ele
b9da0 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20  ment and a hash 
b9db0 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73  on the element's
b9dc0 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   key..*/.static 
b9dd0 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65  void removeEleme
b9de0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48  ntGivenHash(.  H
b9df0 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
b9e00 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61   /* The pH conta
b9e10 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a  ining "elem" */.
b9e20 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d    HashElem* elem
b9e30 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65  ,   /* The eleme
b9e40 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64  nt to be removed
b9e50 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a   from the pH */.
b9e60 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
b9e70 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75      /* Hash valu
b9e80 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e  e for the elemen
b9e90 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  t */.){.  struct
b9ea0 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20   _ht *pEntry;.  
b9eb0 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
b9ec0 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76  {.    elem->prev
b9ed0 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e  ->next = elem->n
b9ee0 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext; .  }else{. 
b9ef0 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65     pH->first = e
b9f00 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
b9f10 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20   if( elem->next 
b9f20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78  ){.    elem->nex
b9f30 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e  t->prev = elem->
b9f40 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20  prev;.  }.  if( 
b9f50 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45  pH->ht ){.    pE
b9f60 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
b9f70 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72  ];.    if( pEntr
b9f80 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29  y->chain==elem )
b9f90 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e  {.      pEntry->
b9fa0 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65  chain = elem->ne
b9fb0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  xt;.    }.    pE
b9fc0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20  ntry->count--;. 
b9fd0 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72     assert( pEntr
b9fe0 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20  y->count>=0 );. 
b9ff0 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
ba000 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d  e( elem );.  pH-
ba010 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20  >count--;.  if( 
ba020 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a  pH->count<=0 ){.
ba030 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
ba040 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  first==0 );.    
ba050 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e  assert( pH->coun
ba060 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  t==0 );.    sqli
ba070 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29  te3HashClear(pH)
ba080 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65  ;.  }.}../* Atte
ba090 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e  mpt to locate an
ba0a0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
ba0b0 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69  hash table pH wi
ba0c0 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74  th a key.** that
ba0d0 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b   matches pKey,nK
ba0e0 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ey.  Return the 
ba0f0 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c  data for this el
ba100 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a  ement if it is.*
ba110 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c  * found, or NULL
ba120 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
ba130 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  match..*/.SQLITE
ba140 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
ba150 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63  qlite3HashFind(c
ba160 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63  onst Hash *pH, c
ba170 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c  onst char *pKey,
ba180 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61   int nKey){.  Ha
ba190 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
ba1a0 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20   /* The element 
ba1b0 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79  that matches key
ba1c0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
ba1d0 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61  nt h;    /* A ha
ba1e0 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20  sh on key */..  
ba1f0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
ba200 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21  .  assert( pKey!
ba210 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
ba220 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28  nKey>=0 );.  if(
ba230 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68   pH->ht ){.    h
ba240 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c   = strHash(pKey,
ba250 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73   nKey) % pH->hts
ba260 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
ba270 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65    h = 0;.  }.  e
ba280 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e  lem = findElemen
ba290 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70  tGivenHash(pH, p
ba2a0 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20  Key, nKey, h);. 
ba2b0 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65   return elem ? e
ba2c0 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d  lem->data : 0;.}
ba2d0 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65  ../* Insert an e
ba2e0 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
ba2f0 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
ba300 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c  The key is pKey,
ba310 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nKey.** and the 
ba320 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a  data is "data"..
ba330 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  **.** If no elem
ba340 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
ba350 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
ba360 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c  then a new.** el
ba370 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64  ement is created
ba380 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74   and NULL is ret
ba390 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
ba3a0 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20  another element 
ba3b0 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77  already exists w
ba3c0 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79  ith the same key
ba3d0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65  , then the.** ne
ba3e0 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20  w data replaces 
ba3f0 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64  the old data and
ba400 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73   the old data is
ba410 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68   returned..** Th
ba420 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70  e key is not cop
ba430 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74  ied in this inst
ba440 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c  ance.  If a mall
ba450 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a  oc fails, then.*
ba460 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69  * the new data i
ba470 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
ba480 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  he hash table is
ba490 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
ba4a0 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20  * If the "data" 
ba4b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69  parameter to thi
ba4c0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  s function is NU
ba4d0 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  LL, then the.** 
ba4e0 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f  element correspo
ba4f0 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69  nding to "key" i
ba500 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
ba510 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  he hash table..*
ba520 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ba530 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61   void *sqlite3Ha
ba540 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70  shInsert(Hash *p
ba550 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  H, const char *p
ba560 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76  Key, int nKey, v
ba570 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e  oid *data){.  un
ba580 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20  signed int h;   
ba590 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
ba5a0 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c  of the key modul
ba5b0 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a  o hash table siz
ba5c0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
ba5d0 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20  *elem;       /* 
ba5e0 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
ba5f0 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69  u the element li
ba600 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  st */.  HashElem
ba610 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a   *new_elem;   /*
ba620 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64   New element add
ba630 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a  ed to the pH */.
ba640 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30  .  assert( pH!=0
ba650 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b   );.  assert( pK
ba660 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ey!=0 );.  asser
ba670 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  t( nKey>=0 );.  
ba680 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29  if( pH->htsize )
ba690 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73  {.    h = strHas
ba6a0 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20  h(pKey, nKey) % 
ba6b0 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65  pH->htsize;.  }e
ba6c0 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a  lse{.    h = 0;.
ba6d0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e    }.  elem = fin
ba6e0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  dElementGivenHas
ba6f0 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68  h(pH,pKey,nKey,h
ba700 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b  );.  if( elem ){
ba710 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64  .    void *old_d
ba720 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61  ata = elem->data
ba730 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d  ;.    if( data==
ba740 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76  0 ){.      remov
ba750 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  eElementGivenHas
ba760 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20  h(pH,elem,h);.  
ba770 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65    }else{.      e
ba780 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61  lem->data = data
ba790 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b  ;.      elem->pK
ba7a0 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20  ey = pKey;.     
ba7b0 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c   assert(nKey==el
ba7c0 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d  em->nKey);.    }
ba7d0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f  .    return old_
ba7e0 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20  data;.  }.  if( 
ba7f0 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  data==0 ) return
ba800 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d   0;.  new_elem =
ba810 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69   (HashElem*)sqli
ba820 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  te3Malloc( sizeo
ba830 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20  f(HashElem) );. 
ba840 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30   if( new_elem==0
ba850 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a   ) return data;.
ba860 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79    new_elem->pKey
ba870 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65   = pKey;.  new_e
ba880 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79  lem->nKey = nKey
ba890 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61  ;.  new_elem->da
ba8a0 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d  ta = data;.  pH-
ba8b0 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20  >count++;.  if( 
ba8c0 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26  pH->count>=10 &&
ba8d0 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70   pH->count > 2*p
ba8e0 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20  H->htsize ){.   
ba8f0 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20   if( rehash(pH, 
ba900 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a  pH->count*2) ){.
ba910 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48        assert( pH
ba920 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20  ->htsize>0 );.  
ba930 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28      h = strHash(
ba940 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48  pKey, nKey) % pH
ba950 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a  ->htsize;.    }.
ba960 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74    }.  if( pH->ht
ba970 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c   ){.    insertEl
ba980 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68  ement(pH, &pH->h
ba990 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b  t[h], new_elem);
ba9a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
ba9b0 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
ba9c0 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  0, new_elem);.  
ba9d0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
ba9e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
ba9f0 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a   End of hash.c *
baa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
baa30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
baa40 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f   Begin file opco
baa50 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.c **********
baa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
baa80 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c  ./* Automaticall
baa90 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f  y generated.  Do
baaa0 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20   not edit */./* 
baab0 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  See the mkopcode
baac0 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  c.awk script for
baad0 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66   details. */.#if
baae0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
baaf0 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c  _OMIT_EXPLAIN) |
bab00 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  | !defined(NDEBU
bab10 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44  G) || defined(VD
bab20 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64  BE_PROFILE) || d
bab30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
bab40 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG).SQLITE_PRIV
bab50 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
bab60 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
bab70 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69  e(int i){. stati
bab80 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
bab90 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b  nst azName[] = {
baba0 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20   "?",.     /*   
babb0 31 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20  1 */ "Goto",.   
babc0 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73    /*   2 */ "Gos
babd0 75 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33  ub",.     /*   3
babe0 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20   */ "Return",.  
babf0 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69     /*   4 */ "Yi
bac00 65 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  eld",.     /*   
bac10 35 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c  5 */ "HaltIfNull
bac20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a  ",.     /*   6 *
bac30 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f  / "Halt",.     /
bac40 2a 20 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65  *   7 */ "Intege
bac50 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20  r",.     /*   8 
bac60 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20  */ "Int64",.    
bac70 20 2f 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69   /*   9 */ "Stri
bac80 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30  ng",.     /*  10
bac90 20 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20   */ "Null",.    
baca0 20 2f 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62   /*  11 */ "Blob
bacb0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a  ",.     /*  12 *
bacc0 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20  / "Variable",.  
bacd0 20 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f     /*  13 */ "Mo
bace0 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34  ve",.     /*  14
bacf0 20 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20   */ "Copy",.    
bad00 20 2f 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70   /*  15 */ "SCop
bad10 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20  y",.     /*  16 
bad20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a  */ "ResultRow",.
bad30 20 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22       /*  17 */ "
bad40 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f  CollSeq",.     /
bad50 2a 20 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69  *  18 */ "Functi
bad60 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39  on",.     /*  19
bad70 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20   */ "Not",.     
bad80 2f 2a 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d  /*  20 */ "AddIm
bad90 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20  m",.     /*  21 
bada0 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a  */ "MustBeInt",.
badb0 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22       /*  22 */ "
badc0 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20  RealAffinity",. 
badd0 20 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50      /*  23 */ "P
bade0 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20  ermutation",.   
badf0 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d    /*  24 */ "Com
bae00 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  pare",.     /*  
bae10 32 35 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20  25 */ "Jump",.  
bae20 20 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 49 66     /*  26 */ "If
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a  ",.     /*  27 *
bae40 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20  / "IfNot",.     
bae50 2f 2a 20 20 32 38 20 2a 2f 20 22 43 6f 6c 75 6d  /*  28 */ "Colum
bae60 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20  n",.     /*  29 
bae70 2a 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20  */ "Affinity",. 
bae80 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 4d      /*  30 */ "M
bae90 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20 20  akeRecord",.    
baea0 20 2f 2a 20 20 33 31 20 2a 2f 20 22 43 6f 75 6e   /*  31 */ "Coun
baeb0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20  t",.     /*  32 
baec0 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a  */ "Savepoint",.
baed0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22       /*  33 */ "
baee0 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20  AutoCommit",.   
baef0 20 20 2f 2a 20 20 33 34 20 2a 2f 20 22 54 72 61    /*  34 */ "Tra
baf00 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20  nsaction",.     
baf10 2f 2a 20 20 33 35 20 2a 2f 20 22 52 65 61 64 43  /*  35 */ "ReadC
baf20 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20  ookie",.     /* 
baf30 20 33 36 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69   36 */ "SetCooki
baf40 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 37 20  e",.     /*  37 
baf50 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65  */ "VerifyCookie
baf60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a  ",.     /*  38 *
baf70 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20  / "OpenRead",.  
baf80 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4f 70     /*  39 */ "Op
baf90 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f  enWrite",.     /
bafa0 2a 20 20 34 30 20 2a 2f 20 22 4f 70 65 6e 45 70  *  40 */ "OpenEp
bafb0 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f  hemeral",.     /
bafc0 2a 20 20 34 31 20 2a 2f 20 22 4f 70 65 6e 50 73  *  41 */ "OpenPs
bafd0 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20  eudo",.     /*  
bafe0 34 32 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20  42 */ "Close",. 
baff0 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53      /*  43 */ "S
bb000 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20  eekLt",.     /* 
bb010 20 34 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22 2c   44 */ "SeekLe",
bb020 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20  .     /*  45 */ 
bb030 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f  "SeekGe",.     /
bb040 2a 20 20 34 36 20 2a 2f 20 22 53 65 65 6b 47 74  *  46 */ "SeekGt
bb050 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a  ",.     /*  47 *
bb060 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f  / "Seek",.     /
bb070 2a 20 20 34 38 20 2a 2f 20 22 4e 6f 74 46 6f 75  *  48 */ "NotFou
bb080 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39  nd",.     /*  49
bb090 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20   */ "Found",.   
bb0a0 20 20 2f 2a 20 20 35 30 20 2a 2f 20 22 49 73 55    /*  50 */ "IsU
bb0b0 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20  nique",.     /* 
bb0c0 20 35 31 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74   51 */ "NotExist
bb0d0 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20  s",.     /*  52 
bb0e0 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20  */ "Sequence",. 
bb0f0 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 4e      /*  53 */ "N
bb100 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f  ewRowid",.     /
bb110 2a 20 20 35 34 20 2a 2f 20 22 49 6e 73 65 72 74  *  54 */ "Insert
bb120 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a  ",.     /*  55 *
bb130 2f 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20  / "InsertInt",. 
bb140 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20 22 44      /*  56 */ "D
bb150 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20  elete",.     /* 
bb160 20 35 37 20 2a 2f 20 22 52 65 73 65 74 43 6f 75   57 */ "ResetCou
bb170 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38  nt",.     /*  58
bb180 20 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20   */ "RowKey",.  
bb190 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 52 6f     /*  59 */ "Ro
bb1a0 77 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20  wData",.     /* 
bb1b0 20 36 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a   60 */ "Rowid",.
bb1c0 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22       /*  61 */ "
bb1d0 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f  NullRow",.     /
bb1e0 2a 20 20 36 32 20 2a 2f 20 22 4c 61 73 74 22 2c  *  62 */ "Last",
bb1f0 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20  .     /*  63 */ 
bb200 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20  "Sort",.     /* 
bb210 20 36 34 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c   64 */ "Rewind",
bb220 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20  .     /*  65 */ 
bb230 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20  "Prev",.     /* 
bb240 20 36 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20   66 */ "Next",. 
bb250 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49      /*  67 */ "I
bb260 64 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20  dxInsert",.     
bb270 2f 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a  /*  68 */ "Or",.
bb280 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22       /*  69 */ "
bb290 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  And",.     /*  7
bb2a0 30 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22  0 */ "IdxDelete"
bb2b0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f  ,.     /*  71 */
bb2c0 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20   "IdxRowid",.   
bb2d0 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 49 64 78    /*  72 */ "Idx
bb2e0 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33  LT",.     /*  73
bb2f0 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20   */ "IsNull",.  
bb300 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f     /*  74 */ "No
bb310 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  tNull",.     /* 
bb320 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20   75 */ "Ne",.   
bb330 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22    /*  76 */ "Eq"
bb340 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f  ,.     /*  77 */
bb350 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "Gt",.     /*  
bb360 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20  78 */ "Le",.    
bb370 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c   /*  79 */ "Lt",
bb380 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20  .     /*  80 */ 
bb390 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  "Ge",.     /*  8
bb3a0 31 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20  1 */ "IdxGE",.  
bb3b0 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 42 69     /*  82 */ "Bi
bb3c0 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tAnd",.     /*  
bb3d0 38 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20  83 */ "BitOr",. 
bb3e0 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 53      /*  84 */ "S
bb3f0 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20  hiftLeft",.     
bb400 2f 2a 20 20 38 35 20 2a 2f 20 22 53 68 69 66 74  /*  85 */ "Shift
bb410 52 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20  Right",.     /* 
bb420 20 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20   86 */ "Add",.  
bb430 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 53 75     /*  87 */ "Su
bb440 62 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a  btract",.     /*
bb450 20 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 70 6c    88 */ "Multipl
bb460 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20  y",.     /*  89 
bb470 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20  */ "Divide",.   
bb480 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 6d    /*  90 */ "Rem
bb490 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a  ainder",.     /*
bb4a0 20 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61 74 22    91 */ "Concat"
bb4b0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f  ,.     /*  92 */
bb4c0 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20   "Destroy",.    
bb4d0 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e   /*  93 */ "BitN
bb4e0 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34  ot",.     /*  94
bb4f0 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20   */ "String8",. 
bb500 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43      /*  95 */ "C
bb510 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lear",.     /*  
bb520 39 36 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64  96 */ "CreateInd
bb530 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37  ex",.     /*  97
bb540 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65   */ "CreateTable
bb550 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a  ",.     /*  98 *
bb560 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c  / "ParseSchema",
bb570 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20  .     /*  99 */ 
bb580 22 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a  "LoadAnalysis",.
bb590 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22       /* 100 */ "
bb5a0 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20  DropTable",.    
bb5b0 20 2f 2a 20 31 30 31 20 2a 2f 20 22 44 72 6f 70   /* 101 */ "Drop
bb5c0 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20  Index",.     /* 
bb5d0 31 30 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67  102 */ "DropTrig
bb5e0 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  ger",.     /* 10
bb5f0 33 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43  3 */ "IntegrityC
bb600 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20  k",.     /* 104 
bb610 2a 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c 0a  */ "RowSetAdd",.
bb620 20 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22       /* 105 */ "
bb630 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20 20  RowSetRead",.   
bb640 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 52 6f 77    /* 106 */ "Row
bb650 53 65 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f  SetTest",.     /
bb660 2a 20 31 30 37 20 2a 2f 20 22 50 72 6f 67 72 61  * 107 */ "Progra
bb670 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20  m",.     /* 108 
bb680 2a 2f 20 22 50 61 72 61 6d 22 2c 0a 20 20 20 20  */ "Param",.    
bb690 20 2f 2a 20 31 30 39 20 2a 2f 20 22 46 6b 43 6f   /* 109 */ "FkCo
bb6a0 75 6e 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  unter",.     /* 
bb6b0 31 31 30 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f  110 */ "FkIfZero
bb6c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a  ",.     /* 111 *
bb6d0 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20  / "MemMax",.    
bb6e0 20 2f 2a 20 31 31 32 20 2a 2f 20 22 49 66 50 6f   /* 112 */ "IfPo
bb6f0 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20  s",.     /* 113 
bb700 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20 20  */ "IfNeg",.    
bb710 20 2f 2a 20 31 31 34 20 2a 2f 20 22 49 66 5a 65   /* 114 */ "IfZe
bb720 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35  ro",.     /* 115
bb730 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 20   */ "AggStep",. 
bb740 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 41      /* 116 */ "A
bb750 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f  ggFinal",.     /
bb760 2a 20 31 31 37 20 2a 2f 20 22 56 61 63 75 75 6d  * 117 */ "Vacuum
bb770 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a  ",.     /* 118 *
bb780 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a  / "IncrVacuum",.
bb790 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22       /* 119 */ "
bb7a0 45 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a  Expire",.     /*
bb7b0 20 31 32 30 20 2a 2f 20 22 54 61 62 6c 65 4c 6f   120 */ "TableLo
bb7c0 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31  ck",.     /* 121
bb7d0 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 20   */ "VBegin",.  
bb7e0 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56 43     /* 122 */ "VC
bb7f0 72 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20  reate",.     /* 
bb800 31 32 33 20 2a 2f 20 22 56 44 65 73 74 72 6f 79  123 */ "VDestroy
bb810 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a  ",.     /* 124 *
bb820 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20  / "VOpen",.     
bb830 2f 2a 20 31 32 35 20 2a 2f 20 22 56 46 69 6c 74  /* 125 */ "VFilt
bb840 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36  er",.     /* 126
bb850 20 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20   */ "VColumn",. 
bb860 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56      /* 127 */ "V
bb870 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Next",.     /* 1
bb880 32 38 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c  28 */ "VRename",
bb890 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20  .     /* 129 */ 
bb8a0 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20  "VUpdate",.     
bb8b0 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c 22  /* 130 */ "Real"
bb8c0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f  ,.     /* 131 */
bb8d0 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20   "Pagecount",.  
bb8e0 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 54 72     /* 132 */ "Tr
bb8f0 61 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  ace",.     /* 13
bb900 33 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20  3 */ "Noop",.   
bb910 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 45 78 70    /* 134 */ "Exp
bb920 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  lain",.     /* 1
bb930 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  35 */ "NotUsed_1
bb940 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36  35",.     /* 136
bb950 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36   */ "NotUsed_136
bb960 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a  ",.     /* 137 *
bb970 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c  / "NotUsed_137",
bb980 0a 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20  .     /* 138 */ 
bb990 22 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20  "NotUsed_138",. 
bb9a0 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e      /* 139 */ "N
bb9b0 6f 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20  otUsed_139",.   
bb9c0 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74    /* 140 */ "Not
bb9d0 55 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20  Used_140",.     
bb9e0 2f 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78  /* 141 */ "ToTex
bb9f0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20  t",.     /* 142 
bba00 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20  */ "ToBlob",.   
bba10 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e    /* 143 */ "ToN
bba20 75 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a  umeric",.     /*
bba30 20 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c   144 */ "ToInt",
bba40 0a 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20  .     /* 145 */ 
bba50 22 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20  "ToReal",.  };. 
bba60 20 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69   return azName[i
bba70 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  ];.}.#endif../**
bba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
bba90 20 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a   of opcodes.c **
bbaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bbad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
bbae0 69 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63  in file os_os2.c
bbaf0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bbb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
bbb20 2a 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a  ** 2006 Feb 14.*
bbb30 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
bbb40 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
bbb50 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
bbb60 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
bbb70 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
bbb80 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
bbb90 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
bbba0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
bbbb0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
bbbc0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
bbbd0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
bbbe0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
bbbf0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
bbc00 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
bbc10 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
bbc20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
bbc30 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
bbc40 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
bbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc90 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
bbca0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
bbcb0 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69   that is specifi
bbcc0 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a  c to OS/2..*/...
bbcd0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  #if SQLITE_OS_OS
bbce0 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20  2../*.** A Note 
bbcf0 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c  About Memory All
bbd00 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  ocation:.**.** T
bbd10 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20  his driver uses 
bbd20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20  malloc()/free() 
bbd30 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20  directly rather 
bbd40 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75  than going throu
bbd50 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  gh.** the SQLite
bbd60 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65  -wrappers sqlite
bbd70 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74  3_malloc()/sqlit
bbd80 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  e3_free().  Thos
bbd90 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72  e wrappers.** ar
bbda0 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  e designed for u
bbdb0 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  se on embedded s
bbdc0 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d  ystems where mem
bbdd0 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e  ory is scarce an
bbde0 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  d.** malloc fail
bbdf0 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71  ures happen freq
bbe00 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f  uently.  OS/2 do
bbe10 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79  es not typically
bbe20 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64   run on.** embed
bbe30 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64  ded systems, and
bbe40 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68   when it does th
bbe50 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72  e developers nor
bbe60 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65  mally have bigge
bbe70 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f  r.** problems to
bbe80 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61   worry about tha
bbe90 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66  n running out of
bbea0 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65   memory.  So the
bbeb0 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63  re is not.** a c
bbec0 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74  ompelling need t
bbed0 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  o use the wrappe
bbee0 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68  rs..**.** But th
bbef0 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65  ere is a good re
bbf00 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20  ason to not use 
bbf10 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49  the wrappers.  I
bbf20 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20  f we use the.** 
bbf30 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65  wrappers then we
bbf40 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61   will get simula
bbf50 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  ted malloc() fai
bbf60 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69  lures within thi
bbf70 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e  s.** driver.  An
bbf80 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c  d that causes al
bbf90 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c  l kinds of probl
bbfa0 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74  ems for our test
bbfb0 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20  s.  We.** could 
bbfc0 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74  enhance SQLite t
bbfd0 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75  o deal with simu
bbfe0 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69  lated malloc fai
bbff0 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20  lures within.** 
bc000 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62  the OS driver, b
bc010 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64  ut the code to d
bc020 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66  eal with those f
bc030 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74  ailure would not
bc040 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64  .** be exercised
bc050 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68   on Linux (which
bc060 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
bc070 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68  o malloc() in th
bc080 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64  e driver).** and
bc090 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76   so we would hav
bc0a0 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69  e difficulty wri
bc0b0 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65  ting coverage te
bc0c0 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20  sts for that.** 
bc0d0 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f  code.  Better to
bc0e0 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20   leave the code 
bc0f0 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a  out, we think..*
bc100 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f  *.** The point o
bc110 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f  f this discussio
bc120 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  n is as follows:
bc130 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20    When creating 
bc140 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65  a new.** OS laye
bc150 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65  r for an embedde
bc160 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75  d system, if you
bc170 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61   use this file a
bc180 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a  s an example,.**
bc190 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f   avoid the use o
bc1a0 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28  f malloc()/free(
bc1b0 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e  ).  Those routin
bc1c0 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53  es work ok on OS
bc1d0 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  /2.** desktops b
bc1e0 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
bc1f0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
bc200 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ms..*/../*.** Ma
bc210 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74  cros used to det
bc220 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
bc230 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72  r not to use thr
bc240 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  eads..*/.#if def
bc250 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45  ined(SQLITE_THRE
bc260 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54  ADSAFE) && SQLIT
bc270 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64  E_THREADSAFE.# d
bc280 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32  efine SQLITE_OS2
bc290 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69  _THREADS 1.#endi
bc2a0 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  f../*.** Include
bc2b0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
bc2c0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a  mmon to all os_*
bc2d0 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a  .c files.*/./***
bc2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
bc2f0 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ude os_common.h 
bc300 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
bc310 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
bc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
bc330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
bc340 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
bc350 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
bc380 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
bc390 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
bc3a0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
bc3b0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
bc3c0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
bc3d0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
bc3e0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
bc3f0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
bc400 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
bc410 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
bc420 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
bc430 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
bc440 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
bc450 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
bc460 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
bc470 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
bc480 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
bc490 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
bc4a0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
bc4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc4f0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
bc500 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f  e contains macro
bc510 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62  s and a little b
bc520 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  it of code that 
bc530 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20  is common to.** 
bc540 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66  all of the platf
bc550 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c  orm-specific fil
bc560 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20  es (os_*.c) and 
bc570 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74  is #included int
bc580 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73  o those.** files
bc590 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
bc5a0 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63  e should be #inc
bc5b0 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f  luded by the os_
bc5c0 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20  *.c files only. 
bc5d0 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20   It is not a.** 
bc5e0 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20  general purpose 
bc5f0 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a  header file..*/.
bc600 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d  #ifndef _OS_COMM
bc610 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f  ON_H_.#define _O
bc620 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a  S_COMMON_H_../*.
bc630 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20  ** At least two 
bc640 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65  bugs have slippe
bc650 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20  d in because we 
bc660 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f  changed the MEMO
bc670 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72  RY_DEBUG.** macr
bc680 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55  o to SQLITE_DEBU
bc690 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72  G and some older
bc6a0 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20   makefiles have 
bc6b0 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65  not yet made the
bc6c0 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65  .** switch.  The
bc6d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
bc6e0 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69  should catch thi
bc6f0 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d  s problem at com
bc700 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69  pile-time..*/.#i
bc710 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55  fdef MEMORY_DEBU
bc720 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d  G.# error "The M
bc730 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72  EMORY_DEBUG macr
bc740 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20  o is obsolete.  
bc750 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47  Use SQLITE_DEBUG
bc760 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69   instead.".#endi
bc770 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
bc780 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
bc790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
bc7a0 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64  3OSTrace = 0;.#d
bc7b0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58  efine OSTRACE1(X
bc7c0 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71  )         if( sq
bc7d0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
bc7e0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bc7f0 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  f(X).#define OST
bc800 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20  RACE2(X,Y)      
bc810 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
bc820 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
bc830 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64  ugPrintf(X,Y).#d
bc840 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58  efine OSTRACE3(X
bc850 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71  ,Y,Z)     if( sq
bc860 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
bc870 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
bc880 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  f(X,Y,Z).#define
bc890 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
bc8a0 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  A)   if( sqlite3
bc8b0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
bc8c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
bc8d0 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
bc8e0 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
bc8f0 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54  ) if( sqlite3OST
bc900 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
bc910 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
bc920 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  A,B).#define OST
bc930 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE6(X,Y,Z,A,B,
bc940 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69  C) \.    if(sqli
bc950 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69  te3OSTrace) sqli
bc960 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
bc970 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66  ,Y,Z,A,B,C).#def
bc980 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59  ine OSTRACE7(X,Y
bc990 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20  ,Z,A,B,C,D) \.  
bc9a0 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72    if(sqlite3OSTr
bc9b0 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
bc9c0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
bc9d0 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65  B,C,D).#else.#de
bc9e0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
bc9f0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bca00 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f  2(X,Y).#define O
bca10 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23  STRACE3(X,Y,Z).#
bca20 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
bca30 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65  X,Y,Z,A).#define
bca40 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c   OSTRACE5(X,Y,Z,
bca50 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  A,B).#define OST
bca60 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE6(X,Y,Z,A,B,
bca70 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  C).#define OSTRA
bca80 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c  CE7(X,Y,Z,A,B,C,
bca90 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  D).#endif../*.**
bcaa0 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66   Macros for perf
bcab0 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e  ormance tracing.
bcac0 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65    Normally turne
bcad0 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72  d off.  Only wor
bcae0 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61  ks.** on i486 ha
bcaf0 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65  rdware..*/.#ifde
bcb00 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d  f SQLITE_PERFORM
bcb10 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a  ANCE_TRACE../* .
bcb20 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74  ** hwtime.h cont
bcb30 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65  ains inline asse
bcb40 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69  mbler code for i
bcb50 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20  mplementing .** 
bcb60 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65  high-performance
bcb70 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73   timing routines
bcb80 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..*/./**********
bcb90 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74  **** Include hwt
bcba0 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ime.h in the mid
bcbb0 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e  dle of os_common
bcbc0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bcbd0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
bcbe0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
bcbf0 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
bcc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcc20 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
bcc30 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65  May 27.**.** The
bcc40 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
bcc50 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
bcc60 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
bcc70 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
bcc80 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
bcc90 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
bcca0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
bccb0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
bccc0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
bccd0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
bcce0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
bccf0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
bcd00 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
bcd10 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
bcd20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
bcd30 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
bcd40 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
bcd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
bcda0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
bcdb0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63  ins inline asm c
bcdc0 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69  ode for retrievi
bcdd0 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d  ng "high-perform
bcde0 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72  ance".** counter
bcdf0 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20  s for x86 class 
bce00 43 50 55 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  CPUs..*/.#ifndef
bce10 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
bce20 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
bce30 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
bce40 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
bce50 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
bce60 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
bce70 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
bce80 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
bce90 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
bcea0 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
bceb0 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
bcec0 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
bced0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
bcee0 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
bcef0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
bcf00 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
bcf10 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
bcf20 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
bcf30 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
bcf40 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
bcf50 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
bcf60 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
bcf70 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
bcf80 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
bcf90 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
bcfa0 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
bcfb0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
bcfc0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
bcfd0 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
bcfe0 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
bcff0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
bd000 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
bd010 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
bd020 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
bd030 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
bd040 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
bd050 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
bd060 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
bd070 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
bd080 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
bd090 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
bd0a0 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
bd0b0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
bd0c0 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
bd0d0 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
bd0e0 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
bd0f0 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
bd100 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
bd110 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
bd120 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
bd130 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
bd140 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
bd150 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
bd160 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
bd170 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
bd180 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
bd190 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
bd1a0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
bd1b0 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
bd1c0 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
bd1d0 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
bd1e0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
bd1f0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
bd200 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
bd210 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
bd220 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
bd230 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
bd240 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
bd250 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
bd260 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
bd270 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
bd280 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
bd290 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
bd2a0 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
bd2b0 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
bd2c0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
bd2d0 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
bd2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
bd2f0 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
bd300 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
bd310 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
bd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd330 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
bd340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
bd350 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
bd360 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
bd370 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
bd380 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
bd390 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
bd3a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
bd3b0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
bd3c0 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
bd3d0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
bd3e0 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
bd3f0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
bd400 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
bd410 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
bd420 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
bd430 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
bd440 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
bd450 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
bd460 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
bd470 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
bd480 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
bd490 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
bd4a0 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
bd4b0 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
bd4c0 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
bd4d0 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
bd4e0 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
bd4f0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
bd500 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
bd510 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
bd520 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
bd530 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
bd540 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
bd550 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
bd560 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
bd570 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bd580 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
bd590 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bd5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bd5c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bd5d0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
bd5e0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
bd5f0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
bd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bd610 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
bd620 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a  uint64 g_start;.
bd630 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
bd640 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a  nt64 g_elapsed;.
bd650 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
bd660 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
bd670 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  t=sqlite3Hwtime(
bd680 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ).#define TIMER_
bd690 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c  END         g_el
bd6a0 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74  apsed=sqlite3Hwt
bd6b0 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64  ime()-g_start.#d
bd6c0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
bd6d0 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65  SED     g_elapse
bd6e0 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  d.#else.#define 
bd6f0 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
bd700 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
bd710 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
bd720 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65  SED     ((sqlite
bd730 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69  _uint64)0).#endi
bd740 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
bd750 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
bd760 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
bd770 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
bd780 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
bd790 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
bd7a0 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
bd7b0 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
bd7c0 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
bd7d0 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
bd7e0 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
bd7f0 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
bd800 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
bd810 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
bd820 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
bd830 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
bd840 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
bd850 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
bd860 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a   of I/O Errors *
bd870 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
bd880 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bd890 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20  r_hardhit = 0;  
bd8a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
bd8b0 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72  of non-benign er
bd8c0 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
bd8d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
bd8e0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
bd8f0 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
bd900 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72  ount down to fir
bd910 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
bd920 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
bd930 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd940 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20  persist = 0;    
bd950 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49      /* True if I
bd960 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73  /O errors persis
bd970 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t */.SQLITE_API 
bd980 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
bd990 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b  rror_benign = 0;
bd9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
bd9b0 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62   if errors are b
bd9c0 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f  enign */.SQLITE_
bd9d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bd9e0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
bd9f0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
bda00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
bda10 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
bda20 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
bda30 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69  orBenign(X) sqli
bda40 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
bda50 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20  ign=(X).#define 
bda60 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
bda70 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28  CODE)  \.  if( (
bda80 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bda90 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
bdaa0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
bdab0 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  ) \.       || sq
bdac0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
bdad0 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20  ending-- == 1 ) 
bdae0 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
bdaf0 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29   { local_ioerr()
bdb00 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63  ; CODE; }.static
bdb10 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72   void local_ioer
bdb20 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28  r(){.  IOTRACE((
bdb30 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73  "IOERR\n"));.  s
bdb40 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bdb50 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71  hit++;.  if( !sq
bdb60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
bdb70 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f  enign ) sqlite3_
bdb80 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
bdb90 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69  ++;.}.#define Si
bdba0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
bdbb0 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69  ror(CODE) \.   i
bdbc0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
bdbd0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c  ull_pending ){ \
bdbe0 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  .     if( sqlite
bdbf0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
bdc00 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20  ng == 1 ){ \.   
bdc10 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28      local_ioerr(
bdc20 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  ); \.       sqli
bdc30 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
bdc40 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
bdc50 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
bdc60 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f  = 1; \.       CO
bdc70 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65  DE; \.     }else
bdc80 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  { \.       sqlit
bdc90 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
bdca0 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20  ing--; \.     } 
bdcb0 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65  \.   }.#else.#de
bdcc0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bdcd0 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64  rrorBenign(X).#d
bdce0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
bdcf0 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65  Error(A).#define
bdd00 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
bdd10 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66  lError(A).#endif
bdd20 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73  ../*.** When tes
bdd30 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75  ting, keep a cou
bdd40 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
bdd50 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a   of open files..
bdd60 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
bdd70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
bdd80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
bdd90 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30  n_file_count = 0
bdda0 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  ;.#define OpenCo
bddb0 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65  unter(X)  sqlite
bddc0 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
bddd0 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  t+=(X).#else.#de
bdde0 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
bddf0 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  (X).#endif..#end
bde00 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
bde10 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f  OS_COMMON_H_) */
bde20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
bde30 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
bde40 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
bde50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bde80 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
bde90 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
bdea0 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  n os_os2.c *****
bdeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdec0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32  /../*.** The os2
bded0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
bdee0 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  s subclass of sq
bdef0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69  lite3_file speci
bdf00 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32  fic for the OS/2
bdf10 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20  .** protability 
bdf20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
bdf30 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65  f struct os2File
bdf40 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74   os2File;.struct
bdf50 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e   os2File {.  con
bdf60 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
bdf70 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20  thods *pMethod; 
bdf80 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66   /* Always the f
bdf90 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  irst entry */.  
bdfa0 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20  HFILE h;        
bdfb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
bdfc0 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
bdfd0 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  g the file */.  
bdfe0 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b  char* pathToDel;
bdff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
be000 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
be010 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55  ete on close, NU
be020 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75  LL if not */.  u
be030 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63  nsigned char loc
be040 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65  ktype;   /* Type
be050 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74   of lock current
be060 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20  ly held on this 
be070 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66  file */.};..#def
be080 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ine LOCK_TIMEOUT
be090 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61   10L /* the defa
be0a0 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65  ult locking time
be0b0 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  out */../*******
be0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be100 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ******.** The ne
be110 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  xt group of rout
be120 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74  ines implement t
be130 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73  he I/O methods s
be140 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74  pecified.** by t
be150 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
be160 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
be170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
be1c0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
be1d0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
be1e0 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74   os2Close( sqlit
be1f0 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20  e3_file *id ){. 
be200 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
be210 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65  ERROR;.  os2File
be220 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69   *pFile;.  if( i
be230 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f  d && (pFile = (o
be240 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30  s2File*)id) != 0
be250 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32   ){.    OSTRACE2
be260 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20  ( "CLOSE %d\n", 
be270 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20  pFile->h );.    
be280 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70  rc = DosClose( p
be290 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70  File->h );.    p
be2a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
be2b0 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66   NO_LOCK;.    if
be2c0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
be2d0 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20  el != NULL ){.  
be2e0 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63      rc = DosForc
be2f0 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46  eDelete( (PSZ)pF
be300 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
be310 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46  ;.      free( pF
be320 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
be330 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70  ;.      pFile->p
be340 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
be350 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20  .    }.    id = 
be360 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  0;.    OpenCount
be370 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20  er( -1 );.  }.. 
be380 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
be390 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
be3a0 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
be3b0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  R;.}../*.** Read
be3c0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c   data from a fil
be3d0 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e  e into a buffer.
be3e0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
be3f0 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74  OK if all.** byt
be400 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63  es were read suc
be410 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51  cessfully and SQ
be420 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e  LITE_IOERR if an
be430 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
be440 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
be450 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73  int os2Read(.  s
be460 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
be470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be480 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66  * File to read f
be490 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  rom */.  void *p
be4a0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
be4b0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
be4c0 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74  e content into t
be4d0 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  his buffer */.  
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 72 65 61 64 20 2a 2f 0a 20 20  es to read */.  
be520 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
be530 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
be540 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
be550 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
be560 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69  */.){.  ULONG fi
be570 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b  leLocation = 0L;
be580 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20  .  ULONG got;.  
be590 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
be5a0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
be5b0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
be5c0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
be5d0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
be5e0 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b  TE_IOERR_READ );
be5f0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45  .  OSTRACE3( "RE
be600 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
be610 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
be620 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
be630 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50   if( DosSetFileP
be640 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66  tr(pFile->h, off
be650 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c  set, FILE_BEGIN,
be660 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20   &fileLocation) 
be670 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
be680 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
be690 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66  _IOERR;.  }.  if
be6a0 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65  ( DosRead( pFile
be6b0 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
be6c0 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52  &got ) != NO_ERR
be6d0 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
be6e0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
be6f0 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f  AD;.  }.  if( go
be700 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20  t == (ULONG)amt 
be710 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ).    return SQL
be720 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b  ITE_OK;.  else {
be730 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
be740 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69  ortions of the i
be750 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74  nput buffer must
be760 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20   be zero-filled 
be770 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28  */.    memset(&(
be780 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74  (char*)pBuf)[got
be790 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a  ], 0, amt-got);.
be7a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
be7b0 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
be7c0 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  AD;.  }.}../*.**
be7d0 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d   Write data from
be7e0 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61   a buffer into a
be7f0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53   file.  Return S
be800 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
be810 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f  ess.** or some o
be820 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20  ther error code 
be830 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73  on failure..*/.s
be840 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69  tatic int os2Wri
be850 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  te(.  sqlite3_fi
be860 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20  le *id,         
be870 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
be880 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20   write into */. 
be890 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
be8a0 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
be8b0 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f   /* The bytes to
be8c0 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   be written */. 
be8d0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
be8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be8f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
be900 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
be910 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
be920 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20  offset          
be930 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
be940 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67   the file to beg
be950 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f  in writing at */
be960 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65  .){.  ULONG file
be970 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20  Location = 0L;. 
be980 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
be990 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77  ERROR;.  ULONG w
be9a0 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20  rote;.  os2File 
be9b0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
be9c0 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
be9d0 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
be9e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
be9f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
bea00 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75  _WRITE );.  Simu
bea10 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
bea20 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
bea30 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41  _FULL );.  OSTRA
bea40 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c  CE3( "WRITE %d l
bea50 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
bea60 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
bea70 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f  type );.  if( Do
bea80 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c  sSetFilePtr(pFil
bea90 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49  e->h, offset, FI
beaa0 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c  LE_BEGIN, &fileL
beab0 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45  ocation) != NO_E
beac0 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75  RROR ){.    retu
bead0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
beae0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
beaf0 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  mt>0 );.  while(
beb00 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20   amt > 0 &&.    
beb10 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57       ( rc = DosW
beb20 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20  rite( pFile->h, 
beb30 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74  (PVOID)pBuf, amt
beb40 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20  , &wrote ) ) == 
beb50 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20  NO_ERROR &&.    
beb60 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20       wrote > 0. 
beb70 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77   ){.    amt -= w
beb80 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d  rote;.    pBuf =
beb90 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
beba0 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72  wrote];.  }..  r
bebb0 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f  eturn ( rc != NO
bebc0 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20  _ERROR || amt > 
bebd0 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53  (int)wrote ) ? S
bebe0 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c  QLITE_FULL : SQL
bebf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
bec00 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65   Truncate an ope
bec10 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63  n file to a spec
bec20 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74  ified size.*/.st
bec30 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e  atic int os2Trun
bec40 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69  cate( sqlite3_fi
bec50 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74  le *id, i64 nByt
bec60 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63  e ){.  APIRET rc
bec70 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f   = NO_ERROR;.  o
bec80 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
bec90 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
beca0 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43  OSTRACE3( "TRUNC
becb0 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20  ATE %d %lld\n", 
becc0 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20  pFile->h, nByte 
becd0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
bece0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
becf0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
bed00 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  TE );.  rc = Dos
bed10 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69  SetFileSize( pFi
bed20 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a  le->h, nByte );.
bed30 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e    return rc == N
bed40 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
bed50 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
bed60 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a  RR_TRUNCATE;.}..
bed70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
bed80 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74  ST./*.** Count t
bed90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c  he number of ful
beda0 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61  lsyncs and norma
bedb0 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69  l syncs.  This i
bedc0 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a  s used to test.*
bedd0 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64  * that syncs and
bede0 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f   fullsyncs are o
bedf0 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72  ccuring at the r
bee00 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53  ight times..*/.S
bee10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bee20 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
bee30 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
bee40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c   int sqlite3_ful
bee50 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b  lsync_count = 0;
bee60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
bee70 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69  ake sure all wri
bee80 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75  tes to a particu
bee90 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d  lar file are com
beea0 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a  mitted to disk..
beeb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
beec0 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66  2Sync( sqlite3_f
beed0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
beee0 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20  gs ){.  os2File 
beef0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
bef00 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
bef10 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b  3( "SYNC %d lock
bef20 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
bef30 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
bef40 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e );.#ifdef SQLI
bef50 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c  TE_TEST.  if( fl
bef60 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
bef70 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c  C_FULL){.    sql
bef80 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
bef90 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  unt++;.  }.  sql
befa0 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b  ite3_sync_count+
befb0 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49  +;.#endif.  /* I
befc0 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  f we compiled wi
befd0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f  th the SQLITE_NO
befe0 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e  _SYNC flag, then
beff0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20   syncing is a.  
bf000 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69  ** no-op.  */.#i
bf010 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  fdef SQLITE_NO_S
bf020 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  YNC.  UNUSED_PAR
bf030 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20  AMETER(pFile);. 
bf040 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
bf050 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72  K;.#else.  retur
bf060 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72  n DosResetBuffer
bf070 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20  ( pFile->h ) == 
bf080 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
bf090 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
bf0a0 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  ERR;.#endif.}../
bf0b0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
bf0c0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
bf0d0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
bf0e0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
bf0f0 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71   os2FileSize( sq
bf100 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
bf110 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
bf120 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54  Size ){.  APIRET
bf130 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
bf140 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73    FILESTATUS3 fs
bf150 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d  ts3FileInfo;.  m
bf160 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65  emset(&fsts3File
bf170 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
bf180 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b  fsts3FileInfo));
bf190 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
bf1a0 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
bf1b0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
bf1c0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
bf1d0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75   );.  rc = DosQu
bf1e0 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f  eryFileInfo( ((o
bf1f0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
bf200 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66  FIL_STANDARD, &f
bf210 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69  sts3FileInfo, si
bf220 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33  zeof(FILESTATUS3
bf230 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d  ) );.  if( rc ==
bf240 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
bf250 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46   *pSize = fsts3F
bf260 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a  ileInfo.cbFile;.
bf270 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bf280 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
bf290 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bf2a0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20  _IOERR_FSTAT;.  
bf2b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  }.}../*.** Acqui
bf2c0 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
bf2d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
bf2e0 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32  getReadLock( os2
bf2f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20  File *pFile ){. 
bf300 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
bf310 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
bf320 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41   UnlockArea;.  A
bf330 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d  PIRET res;.  mem
bf340 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
bf350 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
bf360 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55  a));.  memset(&U
bf370 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  nlockArea, 0, si
bf380 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29  zeof(UnlockArea)
bf390 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  );.  LockArea.lO
bf3a0 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
bf3b0 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61  IRST;.  LockArea
bf3c0 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
bf3d0 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41  _SIZE;.  UnlockA
bf3e0 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
bf3f0 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c  ;.  UnlockArea.l
bf400 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65  Range = 0L;.  re
bf410 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
bf420 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
bf430 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
bf440 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
bf450 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54  OUT, 1L );.  OST
bf460 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c  RACE3( "GETREADL
bf470 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22  OCK %d res=%d\n"
bf480 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
bf490 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  );.  return res;
bf4a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61  .}../*.** Undo a
bf4b0 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61   readlock.*/.sta
bf4c0 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65  tic int unlockRe
bf4d0 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20  adLock( os2File 
bf4e0 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43  *id ){.  FILELOC
bf4f0 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20  K  LockArea,.   
bf500 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41           UnlockA
bf510 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65  rea;.  APIRET re
bf520 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  s;.  memset(&Loc
bf530 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
bf540 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
bf550 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
bf560 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
bf570 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63  ockArea));.  Loc
bf580 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
bf590 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  0L;.  LockArea.l
bf5a0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e  Range = 0L;.  Un
bf5b0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
bf5c0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
bf5d0 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
bf5e0 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49  ange = SHARED_SI
bf5f0 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53  ZE;.  res = DosS
bf600 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d  etFileLocks( id-
bf610 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
bf620 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
bf630 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a  _TIMEOUT, 1L );.
bf640 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c    OSTRACE3( "UNL
bf650 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c  OCK-READLOCK fil
bf660 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d  e handle=%d res=
bf670 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72  %d?\n", id->h, r
bf680 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  es );.  return r
bf690 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  es;.}../*.** Loc
bf6a0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
bf6b0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
bf6c0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
bf6d0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
bf6e0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
bf6f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
bf700 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
bf710 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
bf720 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
bf730 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
bf740 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
bf750 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
bf760 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
bf770 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
bf780 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
bf790 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
bf7a0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
bf7b0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
bf7c0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
bf7d0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
bf7e0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
bf7f0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
bf800 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
bf810 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
bf820 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
bf830 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
bf840 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
bf850 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
bf860 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
bf870 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
bf880 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
bf890 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
bf8a0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
bf8b0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
bf8c0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
bf8d0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
bf8e0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
bf8f0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
bf900 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
bf910 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
bf920 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
bf930 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
bf940 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
bf950 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
bf960 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
bf970 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55   lock.  The os2U
bf980 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a  nlock() routine.
bf990 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f  ** erases all lo
bf9a0 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20  cks at once and 
bf9b0 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64  returns us immed
bf9c0 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e  iately to lockin
bf9d0 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74  g level 0..** It
bf9e0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
bf9f0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f   to lower the lo
bfa00 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20  cking level one 
bfa10 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20  step at a time. 
bfa20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20   You.** must go 
bfa30 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b  straight to lock
bfa40 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a  ing level 0..*/.
bfa50 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f  static int os2Lo
bfa60 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
bfa70 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
bfa80 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pe ){.  int rc =
bfa90 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
bfaa0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
bfab0 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65   from subroutine
bfac0 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65  s */.  APIRET re
bfad0 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20  s = NO_ERROR;   
bfae0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e   /* Result of an
bfaf0 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20   OS/2 lock call 
bfb00 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b  */.  int newLock
bfb10 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53  type;       /* S
bfb20 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  et pFile->lockty
bfb30 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  pe to this value
bfb40 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
bfb50 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64  */.  int gotPend
bfb60 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54  ingLock = 0;/* T
bfb70 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72  rue if we acquir
bfb80 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ed a PENDING loc
bfb90 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20  k this time */. 
bfba0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
bfbb0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
bfbc0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f   UnlockArea;.  o
bfbd0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
bfbe0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
bfbf0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
bfc00 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
bfc10 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
bfc20 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
bfc30 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
bfc40 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ea));.  assert( 
bfc50 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53  pFile!=0 );.  OS
bfc60 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64  TRACE4( "LOCK %d
bfc70 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70   %d was %d\n", p
bfc80 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
bfc90 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
bfca0 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  pe );..  /* If t
bfcb0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
bfcc0 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
bfcd0 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
bfce0 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
bfcf0 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20   ** os2File, do 
bfd00 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75  nothing. Don't u
bfd10 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a  se the end_lock:
bfd20 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20   exit path, as. 
bfd30 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   ** sqlite3_mute
bfd40 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74  x_enter() hasn't
bfd50 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
bfd60 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
bfd70 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
bfd80 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
bfd90 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
bfda0 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %d ok (already 
bfdb0 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  held)\n", pFile-
bfdc0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  >h, locktype );.
bfdd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bfde0 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
bfdf0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
bfe00 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
bfe10 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20  s correct.  */. 
bfe20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
bfe30 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
bfe40 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
bfe50 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
bfe60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
bfe70 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
bfe80 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
bfe90 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
bfea0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
bfeb0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
bfec0 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  OCK );..  /* Loc
bfed0 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f  k the PENDING_LO
bfee0 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65  CK byte if we ne
bfef0 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ed to acquire a 
bff00 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a  PENDING lock or.
bff10 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f    ** a SHARED lo
bff20 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  ck.  If we are a
bff30 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
bff40 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75  D lock, the acqu
bff50 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20  isition of.  ** 
bff60 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
bff70 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61   byte is tempora
bff80 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f  ry..  */.  newLo
bff90 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e  cktype = pFile->
bffa0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20  locktype;.  if( 
bffb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
bffc0 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c  =NO_LOCK.      |
bffd0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
bffe0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
bfff0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
c0000 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20  RESERVED_LOCK). 
c0010 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   ){.    LockArea
c0020 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
c0030 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63  NG_BYTE;.    Loc
c0040 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
c0050 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
c0060 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
c0070 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c0080 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20  Range = 0L;..   
c0090 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20   /* wait longer 
c00a0 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  than LOCK_TIMEOU
c00b0 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61  T here not to ha
c00c0 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70  ve to try multip
c00d0 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20  le times */.    
c00e0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
c00f0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
c0100 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
c0110 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30  ockArea, 100L, 0
c0120 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
c0130 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0140 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67        gotPending
c0150 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20  Lock = 1;.      
c0160 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
c0170 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  %d pending lock 
c0180 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65  boolean set.  re
c0190 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
c01a0 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a  h, res );.    }.
c01b0 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
c01c0 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a  e a shared lock.
c01d0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
c01e0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
c01f0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52   && res == NO_ER
c0200 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ROR ){.    asser
c0210 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
c0220 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
c0230 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64     res = getRead
c0240 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
c0250 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45   if( res == NO_E
c0260 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65  RROR ){.      ne
c0270 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  wLocktype = SHAR
c0280 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  ED_LOCK;.    }. 
c0290 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
c02a0 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68  CK %d acquire sh
c02b0 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25  ared lock. res=%
c02c0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c02d0 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  res );.  }..  /*
c02e0 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52   Acquire a RESER
c02f0 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  VED lock.  */.  
c0300 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  if( locktype==RE
c0310 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72  SERVED_LOCK && r
c0320 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
c0330 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
c0340 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
c0350 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
c0360 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c0370 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
c0380 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
c0390 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
c03a0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c03b0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
c03c0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c03d0 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 0L;.    res =
c03e0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c03f0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c0400 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c0410 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c0420 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
c0430 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
c0440 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
c0450 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f  type = RESERVED_
c0460 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  LOCK;.    }.    
c0470 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
c0480 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72  %d acquire reser
c0490 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64  ved lock. res=%d
c04a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
c04b0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
c04c0 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  Acquire a PENDIN
c04d0 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  G lock.  */.  if
c04e0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ( locktype==EXCL
c04f0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65  USIVE_LOCK && re
c0500 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
c0510 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65  .    newLocktype
c0520 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
c0530 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c  .    gotPendingL
c0540 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54  ock = 0;.    OST
c0550 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20  RACE2( "LOCK %d 
c0560 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20  acquire pending 
c0570 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f  lock. pending lo
c0580 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74  ck boolean unset
c0590 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29  .\n", pFile->h )
c05a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
c05b0 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
c05c0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
c05d0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
c05e0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
c05f0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0600 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
c0610 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
c0620 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
c0630 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
c0640 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
c0650 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65   OSTRACE2( "unre
c0660 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20  adlock = %d\n", 
c0670 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41  res );.    LockA
c0680 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
c0690 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
c06a0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
c06b0 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
c06c0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
c06d0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
c06e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
c06f0 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20  e = 0L;.    res 
c0700 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c0710 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c0720 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c0730 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c0740 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28  T, 0L );.    if(
c0750 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
c0760 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
c0770 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56  ktype = EXCLUSIV
c0780 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73  E_LOCK;.    }els
c0790 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  e{.      OSTRACE
c07a0 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63  2( "OS/2 error-c
c07b0 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ode = %d\n", res
c07c0 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61   );.      getRea
c07d0 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
c07e0 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33    }.    OSTRACE3
c07f0 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69  ( "LOCK %d acqui
c0800 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  re exclusive loc
c0810 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  k.  res=%d\n", p
c0820 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
c0830 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
c0840 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45  are holding a PE
c0850 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20  NDING lock that 
c0860 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65  ought to be rele
c0870 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  ased, then.  ** 
c0880 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a  release it now..
c0890 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65    */.  if( gotPe
c08a0 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63  ndingLock && loc
c08b0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
c08c0 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b  CK ){.    int r;
c08d0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
c08e0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
c08f0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
c0900 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
c0910 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50  Area.lOffset = P
c0920 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
c0930 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
c0940 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d  ge = 1L;.    r =
c0950 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c0960 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c0970 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c0980 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c0990 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
c09a0 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75  ACE3( "LOCK %d u
c09b0 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67  nlocking pending
c09c0 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64  /is shared. r=%d
c09d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
c09e0 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70   );.  }..  /* Up
c09f0 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f  date the state o
c0a00 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68  f the lock has h
c0a10 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20  eld in the file 
c0a20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a  descriptor then.
c0a30 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
c0a40 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75  appropriate resu
c0a50 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  lt code..  */.  
c0a60 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
c0a70 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROR ){.    rc = 
c0a80 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
c0a90 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34  se{.    OSTRACE4
c0aa0 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  ( "LOCK FAILED %
c0ab0 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
c0ac0 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
c0ad0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
c0ae0 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c         locktype,
c0af0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a   newLocktype );.
c0b00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
c0b10 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  BUSY;.  }.  pFil
c0b20 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65  e->locktype = ne
c0b30 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54  wLocktype;.  OST
c0b40 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
c0b50 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  now %d\n", pFile
c0b60 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
c0b70 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  type );.  return
c0b80 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
c0b90 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
c0ba0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
c0bb0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
c0bc0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
c0bd0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
c0be0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
c0bf0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
c0c00 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
c0c10 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d  , return.** non-
c0c20 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20  zero, otherwise 
c0c30 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zero..*/.static 
c0c40 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65  int os2CheckRese
c0c50 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65  rvedLock( sqlite
c0c60 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
c0c70 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72  *pOut ){.  int r
c0c80 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20   = 0;.  os2File 
c0c90 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
c0ca0 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
c0cb0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69   pFile!=0 );.  i
c0cc0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
c0cd0 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe>=RESERVED_LOC
c0ce0 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a  K ){.    r = 1;.
c0cf0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
c0d00 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
c0d10 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46  d (local)\n", pF
c0d20 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
c0d30 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f  else{.    FILELO
c0d40 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20  CK  LockArea,.  
c0d50 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
c0d60 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52  ckArea;.    APIR
c0d70 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
c0d80 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f  ;.    memset(&Lo
c0d90 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
c0da0 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  f(LockArea));.  
c0db0 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
c0dc0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c0dd0 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
c0de0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c0df0 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
c0e00 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
c0e10 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
c0e20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c0e30 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
c0e40 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c0e50 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20   = 0L;.    rc = 
c0e60 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c0e70 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c0e80 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c0e90 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c0ea0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c0eb0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE3( "TEST WR-LO
c0ec0 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72  CK %d lock reser
c0ed0 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e  ved byte rc=%d\n
c0ee0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20  ", pFile->h, rc 
c0ef0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d  );.    if( rc ==
c0f00 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c0f10 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20     APIRET rcu = 
c0f20 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74  NO_ERROR; /* ret
c0f30 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c  urn code for unl
c0f40 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  ocking */.      
c0f50 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
c0f60 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63   = 0L;.      Loc
c0f70 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c0f80 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41  L;.      UnlockA
c0f90 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45  rea.lOffset = RE
c0fa0 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20  SERVED_BYTE;.   
c0fb0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
c0fc0 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20  ange = 1L;.     
c0fd0 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c   rcu = DosSetFil
c0fe0 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
c0ff0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c1000 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c1010 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
c1020 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
c1030 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75  EST WR-LOCK %d u
c1040 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62  nlock reserved b
c1050 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69  yte r=%d\n", pFi
c1060 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20  le->h, rcu );.  
c1070 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63    }.    r = !(rc
c1080 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20   == NO_ERROR);. 
c1090 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
c10a0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
c10b0 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
c10c0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
c10d0 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20  .  *pOut = r;.  
c10e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c10f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72  ;.}../*.** Lower
c1100 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
c1110 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72  el on file descr
c1120 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b  iptor id to lock
c1130 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
c1140 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
c1150 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
c1160 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
c1170 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
c1180 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
c1190 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
c11a0 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
c11b0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
c11c0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
c11d0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
c11e0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
c11f0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
c1200 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f  ible for this ro
c1210 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66  utine to fail if
c1220 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
c1230 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f  ment.** is NO_LO
c1240 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  CK.  If the seco
c1250 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  nd argument is S
c1260 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20  HARED_LOCK then 
c1270 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
c1280 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c  might return SQL
c1290 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74  ITE_IOERR;.*/.st
c12a0 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f  atic int os2Unlo
c12b0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
c12c0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
c12d0 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65  pe ){.  int type
c12e0 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
c12f0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
c1300 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  d;.  APIRET rc =
c1310 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50   SQLITE_OK;.  AP
c1320 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52  IRET res = NO_ER
c1330 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  ROR;.  FILELOCK 
c1340 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
c1350 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
c1360 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  a;.  memset(&Loc
c1370 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
c1380 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
c1390 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
c13a0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
c13b0 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73  ockArea));.  ass
c13c0 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
c13d0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
c13e0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
c13f0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20   );.  OSTRACE4( 
c1400 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64  "UNLOCK %d to %d
c1410 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c   was %d\n", pFil
c1420 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20  e->h, locktype, 
c1430 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
c1440 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c  );.  type = pFil
c1450 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69  e->locktype;.  i
c1460 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49  f( type>=EXCLUSI
c1470 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  VE_LOCK ){.    L
c1480 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c1490 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c14a0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c14b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c14c0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
c14d0 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63  FIRST;.    Unloc
c14e0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53  kArea.lRange = S
c14f0 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20  HARED_SIZE;.    
c1500 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
c1510 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
c1520 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
c1530 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
c1540 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
c1550 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f   OSTRACE3( "UNLO
c1560 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20  CK %d exclusive 
c1570 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20  lock res=%d\n", 
c1580 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c1590 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
c15a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
c15b0 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  & getReadLock(pF
c15c0 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ile) != NO_ERROR
c15d0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
c15e0 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
c15f0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
c1600 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
c1610 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
c1620 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
c1630 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53  lock */.      OS
c1640 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20  TRACE3( "UNLOCK 
c1650 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64  %d to %d getRead
c1660 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22  Lock() failed\n"
c1670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
c1680 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63  type );.      rc
c1690 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
c16a0 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
c16b0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
c16c0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
c16d0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c16e0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c16f0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c1700 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c1710 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53  ea.lOffset = RES
c1720 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
c1730 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
c1740 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20  e = 1L;.    res 
c1750 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c1760 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c1770 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c1780 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c1790 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54  T, 0L );.    OST
c17a0 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c17b0 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25  d reserved res=%
c17c0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c17d0 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  res );.  }.  if(
c17e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
c17f0 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52  CK && type>=SHAR
c1800 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
c1810 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
c1820 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
c1830 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43  OSTRACE5( "UNLOC
c1840 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20  K %d is %d want 
c1850 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  %d res=%d\n", pF
c1860 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f  ile->h, type, lo
c1870 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20  cktype, res );. 
c1880 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50   }.  if( type>=P
c1890 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20  ENDING_LOCK ){. 
c18a0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c18b0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c18c0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c18d0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c18e0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
c18f0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
c1900 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c1910 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
c1920 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c1930 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c1940 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c1950 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c1960 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
c1970 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c1980 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c   pending res=%d\
c1990 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c19a0 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  s );.  }.  pFile
c19b0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
c19c0 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45  ktype;.  OSTRACE
c19d0 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f  3( "UNLOCK %d no
c19e0 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  w %d\n", pFile->
c19f0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
c1a00 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pe );.  return r
c1a10 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74  c;.}../*.** Cont
c1a20 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66  rol and query of
c1a30 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68   the open file h
c1a40 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  andle..*/.static
c1a50 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74   int os2FileCont
c1a60 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
c1a70 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f   *id, int op, vo
c1a80 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69  id *pArg){.  swi
c1a90 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
c1aa0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
c1ab0 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20  _LOCKSTATE: {.  
c1ac0 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
c1ad0 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  = ((os2File*)id)
c1ae0 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  ->locktype;.    
c1af0 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e    OSTRACE3( "FCN
c1b00 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20  TL_LOCKSTATE %d 
c1b10 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73  lock=%d\n", ((os
c1b20 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28  2File*)id)->h, (
c1b30 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (os2File*)id)->l
c1b40 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20  ocktype );.     
c1b50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c1b60 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
c1b70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
c1b80 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
c1b90 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
c1ba0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
c1bb0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
c1bc0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
c1bd0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
c1be0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
c1bf0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
c1c00 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
c1c10 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
c1c20 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
c1c30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
c1c40 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
c1c50 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
c1c60 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
c1c70 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
c1c80 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
c1c90 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
c1ca0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
c1cb0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
c1cc0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
c1cd0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
c1ce0 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
c1cf0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
c1d00 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
c1d10 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
c1d20 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28  t os2SectorSize(
c1d30 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c1d40 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
c1d50 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
c1d60 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
c1d70 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
c1d80 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
c1d90 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
c1da0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76  tatic int os2Dev
c1db0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
c1dc0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
c1dd0 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30  *id){.  return 0
c1de0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72  ;.}.../*.** Char
c1df0 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72  acter set conver
c1e00 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65  sion objects use
c1e10 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20  d by conversion 
c1e20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
c1e30 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20  tic UconvObject 
c1e40 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f  ucUtf8 = NULL; /
c1e50 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65  * convert betwee
c1e60 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d  n UTF-8 and UCS-
c1e70 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e  2 */.static Ucon
c1e80 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20  vObject uclCp = 
c1e90 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72  NULL;  /* conver
c1ea0 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20  t between local 
c1eb0 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53  codepage and UCS
c1ec0 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  -2 */../*.** Hel
c1ed0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
c1ee0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  initialize the c
c1ef0 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74  onversion object
c1f00 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54  s from and to UT
c1f10 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  F-8..*/.static v
c1f20 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a  oid initUconvObj
c1f30 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20  ects( void ){.  
c1f40 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f  if( UniCreateUco
c1f50 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c  nvObject( UTF_8,
c1f60 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c   &ucUtf8 ) != UL
c1f70 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20  S_SUCCESS ).    
c1f80 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20  ucUtf8 = NULL;. 
c1f90 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55   if ( UniCreateU
c1fa0 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69  convObject( (Uni
c1fb0 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79  Char *)L"@path=y
c1fc0 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d  es", &uclCp ) !=
c1fd0 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
c1fe0 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b     uclCp = NULL;
c1ff0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
c2000 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65   function to fre
c2010 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  e the conversion
c2020 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e   objects from an
c2030 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73  d to UTF-8..*/.s
c2040 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55  tatic void freeU
c2050 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69  convObjects( voi
c2060 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74  d ){.  if ( ucUt
c2070 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65  f8 ).    UniFree
c2080 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55  UconvObject( ucU
c2090 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63  tf8 );.  if ( uc
c20a0 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65  lCp ).    UniFre
c20b0 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63  eUconvObject( uc
c20c0 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20  lCp );.  ucUtf8 
c20d0 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20  = NULL;.  uclCp 
c20e0 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = NULL;.}../*.**
c20f0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
c2100 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d   to convert UTF-
c2110 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c  8 filenames to l
c2120 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61  ocal OS/2 codepa
c2130 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73  ge..** The two-s
c2140 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72  tep process: fir
c2150 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69  st convert the i
c2160 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74  ncoming UTF-8 st
c2170 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53  ring.** into UCS
c2180 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d  -2 and then from
c2190 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75   UCS-2 to the cu
c21a0 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a  rrent codepage..
c21b0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
c21c0 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73  char pointer has
c21d0 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f   to be freed..*/
c21e0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f  .static char *co
c21f0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c2200 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69  p( const char *i
c2210 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74  n ){.  UniChar t
c2220 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41  empPath[CCHMAXPA
c2230 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74  TH];.  char *out
c2240 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f   = (char *)callo
c2250 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31  c( CCHMAXPATH, 1
c2260 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20   );..  if( !out 
c2270 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c  ).    return NUL
c2280 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66  L;..  if( !ucUtf
c2290 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20  8 || !uclCp ).  
c22a0 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63    initUconvObjec
c22b0 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65  ts();..  /* dete
c22c0 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72  rmine string for
c22d0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c22e0 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69  of UTF-8 which i
c22f0 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66  s CP1208 */.  if
c2300 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75  ( UniStrToUcs( u
c2310 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c  cUtf8, tempPath,
c2320 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48   (char *)in, CCH
c2330 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53  MAXPATH ) != ULS
c2340 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72  _SUCCESS ).    r
c2350 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66  eturn out; /* if
c2360 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c   conversion fail
c2370 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d  s, return the em
c2380 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20  pty string */.. 
c2390 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   /* conversion f
c23a0 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  or current codep
c23b0 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65  age which can be
c23c0 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20   used for paths 
c23d0 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55  */.  UniStrFromU
c23e0 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20  cs( uclCp, out, 
c23f0 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58  tempPath, CCHMAX
c2400 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72  PATH );..  retur
c2410 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n out;.}../*.** 
c2420 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c2430 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e  to convert filen
c2440 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20  ames from local 
c2450 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d  codepage to UTF-
c2460 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74  8..** The two-st
c2470 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73  ep process: firs
c2480 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  t convert the in
c2490 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d  coming codepage-
c24a0 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69  specific.** stri
c24b0 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e  ng into UCS-2 an
c24c0 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d  d then from UCS-
c24d0 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67  2 to the codepag
c24e0 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54  e of UTF-8..** T
c24f0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72  he returned char
c2500 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20   pointer has to 
c2510 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  be freed..**.** 
c2520 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
c2530 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62   non-static to b
c2540 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68  e able to use th
c2550 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e  is in shell.c an
c2560 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70  d.** similar app
c2570 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74  lications that t
c2580 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65  ake command line
c2590 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63   arguments..*/.c
c25a0 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61  har *convertCpPa
c25b0 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20  thToUtf8( const 
c25c0 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e  char *in ){.  Un
c25d0 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43  iChar tempPath[C
c25e0 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68  CHMAXPATH];.  ch
c25f0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20  ar *out = (char 
c2600 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58  *)calloc( CCHMAX
c2610 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66  PATH, 1 );..  if
c2620 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74  ( !out ).    ret
c2630 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28  urn NULL;..  if(
c2640 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c   !ucUtf8 || !ucl
c2650 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f  Cp ).    initUco
c2660 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20  nvObjects();..  
c2670 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f  /* conversion fo
c2680 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  r current codepa
c2690 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ge which can be 
c26a0 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a  used for paths *
c26b0 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f  /.  if( UniStrTo
c26c0 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70  Ucs( uclCp, temp
c26d0 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e  Path, (char *)in
c26e0 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21  , CCHMAXPATH ) !
c26f0 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a  = ULS_SUCCESS ).
c2700 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20      return out; 
c2710 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e  /* if conversion
c2720 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74   fails, return t
c2730 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20  he empty string 
c2740 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69  */..  /* determi
c2750 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ne string for th
c2760 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20  e conversion of 
c2770 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43  UTF-8 which is C
c2780 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74  P1208 */.  UniSt
c2790 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38  rFromUcs( ucUtf8
c27a0 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c  , out, tempPath,
c27b0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a   CCHMAXPATH );..
c27c0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a    return out;.}.
c27d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74  ./*.** This vect
c27e0 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74  or defines all t
c27f0 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  he methods that 
c2800 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61  can operate on a
c2810 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c  n.** sqlite3_fil
c2820 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74  e for os2..*/.st
c2830 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
c2840 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73  e3_io_methods os
c2850 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  2IoMethod = {.  
c2860 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
c2870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
c2880 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c  rsion */.  os2Cl
c2890 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a  ose,.  os2Read,.
c28a0 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73    os2Write,.  os
c28b0 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32  2Truncate,.  os2
c28c0 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53  Sync,.  os2FileS
c28d0 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a  ize,.  os2Lock,.
c28e0 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f    os2Unlock,.  o
c28f0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  s2CheckReservedL
c2900 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f  ock,.  os2FileCo
c2910 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74  ntrol,.  os2Sect
c2920 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76  orSize,.  os2Dev
c2930 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
c2940 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  cs.};../********
c2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2990 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73  ***.** Here ends
c29a0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
c29b0 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73   that form the s
c29c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c29d0 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  s object..**.** 
c29e0 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f  The next block o
c29f0 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74  f code implement
c2a00 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64  s the VFS method
c2a10 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..*************
c2a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c2a60 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
c2a70 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
c2a80 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
c2a90 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20  Buf must be big 
c2aa0 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c  enough to.** hol
c2ab0 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  d at pVfs->mxPat
c2ac0 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73  hname characters
c2ad0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c2ae0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
c2af0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c2b00 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   ){.  static con
c2b10 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
c2b20 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
c2b30 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
c2b40 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
c2b50 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
c2b60 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
c2b70 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
c2b80 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68    int i, j;.  ch
c2b90 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b  ar zTempPathBuf[
c2ba0 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50  3];.  PSZ zTempP
c2bb0 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d  ath = (PSZ)&zTem
c2bc0 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20  pPathBuf;.  if( 
c2bd0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
c2be0 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54  ectory ){.    zT
c2bf0 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65  empPath = sqlite
c2c00 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
c2c10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
c2c20 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28  f( DosScanEnv( (
c2c30 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65  PSZ)"TEMP", &zTe
c2c40 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
c2c50 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
c2c60 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a  ( (PSZ)"TMP", &z
c2c70 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20  TempPath ) ){.  
c2c80 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61        if( DosSca
c2c90 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44  nEnv( (PSZ)"TMPD
c2ca0 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20  IR", &zTempPath 
c2cb0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
c2cc0 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d  ULONG ulDriveNum
c2cd0 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70   = 0, ulDriveMap
c2ce0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
c2cf0 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74   DosQueryCurrent
c2d00 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75  Disk( &ulDriveNu
c2d10 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29  m, &ulDriveMap )
c2d20 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72  ;.           spr
c2d30 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65  intf( (char*)zTe
c2d40 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28  mpPath, "%c:", (
c2d50 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44  char)( 'A' + ulD
c2d60 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b  riveNum - 1 ) );
c2d70 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
c2d80 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f   }.    }.  }.  /
c2d90 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72  * Strip off a tr
c2da0 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f  ailing slashes o
c2db0 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f  r backslashes, o
c2dc0 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c  therwise we woul
c2dd0 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c  d get *.   * mul
c2de0 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73  tiple (back)slas
c2df0 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73  hes which causes
c2e00 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61   DosOpen() to fa
c2e10 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  il.             
c2e20 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67   *.   * Trailing
c2e30 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20   spaces are not 
c2e40 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e  allowed, either.
c2e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2e60 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
c2e70 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
c2e80 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b  en30(zTempPath);
c2e90 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20  .  while( j > 0 
c2ea0 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a  && ( zTempPath[j
c2eb0 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a  -1] == '\\' || z
c2ec0 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d  TempPath[j-1] ==
c2ed0 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20   '/'.           
c2ee0 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d           || zTem
c2ef0 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20  pPath[j-1] == ' 
c2f00 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a  ' ) ){.    j--;.
c2f10 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b    }.  zTempPath[
c2f20 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28  j] = '\0';.  if(
c2f30 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64   !sqlite3_temp_d
c2f40 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
c2f50 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55  char *zTempPathU
c2f60 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61  TF = convertCpPa
c2f70 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50  thToUtf8( zTempP
c2f80 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ath );.    sqlit
c2f90 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75  e3_snprintf( nBu
c2fa0 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20  f-30, zBuf,.    
c2fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2fc0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
c2fd0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
c2fe0 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b   zTempPathUTF );
c2ff0 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70  .    free( zTemp
c3000 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c  PathUTF );.  }el
c3010 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
c3020 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33  snprintf( nBuf-3
c3030 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20  0, zBuf,.       
c3040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
c3050 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50  %s\\"SQLITE_TEMP
c3060 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54  _FILE_PREFIX, zT
c3070 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20  empPath );.  }. 
c3080 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
c3090 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20  en30( zBuf );.  
c30a0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
c30b0 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d  ss( 20, &zBuf[j]
c30c0 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30   );.  for( i = 0
c30d0 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a  ; i < 20; i++, j
c30e0 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a  ++ ){.    zBuf[j
c30f0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
c3100 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
c3110 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
c3120 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
c3130 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d  .  }.  zBuf[j] =
c3140 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20   0;.  OSTRACE2( 
c3150 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20  "TEMP FILENAME: 
c3160 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20  %s\n", zBuf );. 
c3170 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c3180 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  K;.}.../*.** Tur
c3190 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
c31a0 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
c31b0 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69  l pathname.  Wri
c31c0 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70  te the full.** p
c31d0 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75  athname into zFu
c31e0 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77  ll[].  zFull[] w
c31f0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20  ill be at least 
c3200 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
c3210 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a  .** bytes in siz
c3220 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
c3230 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65   os2FullPathname
c3240 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
c3250 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
c3260 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
c3270 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  s object */.  co
c3280 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74  nst char *zRelat
c3290 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73  ive,      /* Pos
c32a0 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69  sibly relative i
c32b0 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69  nput path */.  i
c32c0 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20  nt nFull,       
c32d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
c32e0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
c32f0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
c3300 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20    char *zFull   
c3310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c3320 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   Output buffer *
c3330 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65  /.){.  char *zRe
c3340 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65  lativeCp = conve
c3350 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20  rtUtf8PathToCp( 
c3360 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63  zRelative );.  c
c3370 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d  har zFullCp[CCHM
c3380 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a  AXPATH] = "\0";.
c3390 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46    char *zFullUTF
c33a0 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c33b0 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f  DosQueryPathInfo
c33c0 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46  ( zRelativeCp, F
c33d0 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45  IL_QUERYFULLNAME
c33e0 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20  , zFullCp,.     
c33f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3400 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41             CCHMA
c3410 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28  XPATH );.  free(
c3420 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a   zRelativeCp );.
c3430 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e    zFullUTF = con
c3440 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38  vertCpPathToUtf8
c3450 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73  ( zFullCp );.  s
c3460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
c3470 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a   nFull, zFull, z
c3480 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65  FullUTF );.  fre
c3490 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20  e( zFullUTF );. 
c34a0 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
c34b0 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
c34c0 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
c34d0 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  R;.}.../*.** Ope
c34e0 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  n a file..*/.sta
c34f0 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28  tic int os2Open(
c3500 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c3510 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
c3520 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a   /* Not used */.
c3530 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
c3540 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
c3550 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
c3560 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ile */.  sqlite3
c3570 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
c3580 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
c3590 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20  the SQLite file 
c35a0 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20  handle here */. 
c35b0 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
c35c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c35d0 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67  * Open mode flag
c35e0 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74  s */.  int *pOut
c35f0 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
c3600 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72       /* Status r
c3610 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29  eturn flags */.)
c3620 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55  {.  HFILE h;.  U
c3630 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69  LONG ulFileAttri
c3640 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d  bute = FILE_NORM
c3650 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70  AL;.  ULONG ulOp
c3660 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55  enFlags = 0;.  U
c3670 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  LONG ulOpenMode 
c3680 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  = 0;.  os2File *
c3690 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
c36a0 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72  *)id;.  APIRET r
c36b0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c36c0 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a  ULONG ulAction;.
c36d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b    char *zNameCp;
c36e0 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
c36f0 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20  [CCHMAXPATH+1]; 
c3700 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20     /* Buffer to 
c3710 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d  hold name of tem
c3720 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20  p file */..  /* 
c3730 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
c3740 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
c3750 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c  unction is NULL,
c3760 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a   generate a .  *
c3770 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
c3780 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20   name to use .  
c3790 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  */.  if( !zName 
c37a0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
c37b0 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d  getTempname(CCHM
c37c0 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61  AXPATH+1, zTmpna
c37d0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
c37e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
c37f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
c3800 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d     }.    zName =
c3810 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a   zTmpname;.  }..
c3820 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65  .  memset( pFile
c3830 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69  , 0, sizeof(*pFi
c3840 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43  le) );..  OSTRAC
c3850 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25  E2( "OPEN want %
c3860 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a  d\n", flags );..
c3870 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
c3880 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c3890 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65  ITE ){.    ulOpe
c38a0 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43  nMode |= OPEN_AC
c38b0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a  CESS_READWRITE;.
c38c0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c38d0 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e  PEN read/write\n
c38e0 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
c38f0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
c3900 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44  OPEN_ACCESS_READ
c3910 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43  ONLY;.    OSTRAC
c3920 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f  E1( "OPEN read o
c3930 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20  nly\n" );.  }.. 
c3940 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c3950 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
c3960 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61  ){.    ulOpenFla
c3970 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f  gs |= OPEN_ACTIO
c3980 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53  N_OPEN_IF_EXISTS
c3990 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43   | OPEN_ACTION_C
c39a0 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20  REATE_IF_NEW;.  
c39b0 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c39c0 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74  N open new/creat
c39d0 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  e\n" );.  }else{
c39e0 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73  .    ulOpenFlags
c39f0 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f   |= OPEN_ACTION_
c3a00 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c  OPEN_IF_EXISTS |
c3a10 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49   OPEN_ACTION_FAI
c3a20 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53  L_IF_NEW;.    OS
c3a30 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70  TRACE1( "OPEN op
c3a40 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29  en existing\n" )
c3a50 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61  ;.  }..  if( fla
c3a60 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
c3a70 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20  _MAIN_DB ){.    
c3a80 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
c3a90 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e  EN_SHARE_DENYNON
c3aa0 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28  E;.    OSTRACE1(
c3ab0 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61   "OPEN share rea
c3ac0 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20  d/write\n" );.  
c3ad0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65  }else{.    ulOpe
c3ae0 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48  nMode |= OPEN_SH
c3af0 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20  ARE_DENYWRITE;. 
c3b00 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c3b10 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e  EN share read on
c3b20 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ly\n" );.  }..  
c3b30 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c3b40 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
c3b50 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61  CLOSE ){.    cha
c3b60 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41  r pathUtf8[CCHMA
c3b70 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e  XPATH];.#ifdef N
c3b80 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65  DEBUG /* when de
c3b90 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20  bugging we want 
c3ba0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
c3bb0 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  is deleted */.  
c3bc0 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74    ulFileAttribut
c3bd0 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b  e = FILE_HIDDEN;
c3be0 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46  .#endif.    os2F
c3bf0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66  ullPathname( pVf
c3c00 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58  s, zName, CCHMAX
c3c10 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29  PATH, pathUtf8 )
c3c20 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  ;.    pFile->pat
c3c30 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74  hToDel = convert
c3c40 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61  Utf8PathToCp( pa
c3c50 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53  thUtf8 );.    OS
c3c60 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69  TRACE1( "OPEN hi
c3c70 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63  dden/delete on c
c3c80 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62  lose file attrib
c3c90 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c  utes\n" );.  }el
c3ca0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70  se{.    pFile->p
c3cb0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
c3cc0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
c3cd0 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65  OPEN normal file
c3ce0 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b   attribute\n" );
c3cf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79  .  }..  /* alway
c3d00 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d  s open in random
c3d10 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72   access mode for
c3d20 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72   possibly better
c3d30 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70   speed */.  ulOp
c3d40 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46  enMode |= OPEN_F
c3d50 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75  LAGS_RANDOM;.  u
c3d60 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c3d70 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f  N_FLAGS_FAIL_ON_
c3d80 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d  ERROR;.  ulOpenM
c3d90 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47  ode |= OPEN_FLAG
c3da0 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20  S_NOINHERIT;..  
c3db0 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  zNameCp = conver
c3dc0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c3dd0 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44  Name );.  rc = D
c3de0 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61  osOpen( (PSZ)zNa
c3df0 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20  meCp,.          
c3e00 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20        &h,.      
c3e10 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74            &ulAct
c3e20 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ion,.           
c3e30 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20       0L,.       
c3e40 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41           ulFileA
c3e50 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20  ttribute,.      
c3e60 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e            ulOpen
c3e70 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
c3e80 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64         ulOpenMod
c3e90 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
c3ea0 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20     (PEAOP2)NULL 
c3eb0 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65  );.  free( zName
c3ec0 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21  Cp );.  if( rc !
c3ed0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c3ee0 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45    OSTRACE7( "OPE
c3ef0 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65  N Invalid handle
c3f00 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73   rc=%d: zName=%s
c3f10 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c  , ulAction=%#lx,
c3f20 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c   ulAttr=%#lx, ul
c3f30 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f  Flags=%#lx, ulMo
c3f40 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20  de=%#lx\n",.    
c3f50 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e            rc, zN
c3f60 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75  ame, ulAction, u
c3f70 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20  lFileAttribute, 
c3f80 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f  ulOpenFlags, ulO
c3f90 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69  penMode );.    i
c3fa0 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f  f( pFile->pathTo
c3fb0 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65  Del ).      free
c3fc0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
c3fd0 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  el );.    pFile-
c3fe0 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c  >pathToDel = NUL
c3ff0 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  L;.    if( flags
c4000 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
c4010 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
c4020 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45    OSTRACE2( "OPE
c4030 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e  N %d Invalid han
c4040 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20  dle\n", ((flags 
c4050 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  | SQLITE_OPEN_RE
c4060 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54  ADONLY) & ~SQLIT
c4070 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
c4080 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ) );.      retur
c4090 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c  n os2Open( pVfs,
c40a0 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20   zName, id,.    
c40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c40c0 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49    ((flags | SQLI
c40d0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
c40e0 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e  ) & ~SQLITE_OPEN
c40f0 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20  _READWRITE),.   
c4100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4110 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a     pOutFlags );.
c4120 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c4130 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
c4140 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20  ANTOPEN;.    }. 
c4150 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c   }..  if( pOutFl
c4160 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74  ags ){.    *pOut
c4170 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20  Flags = flags & 
c4180 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c4190 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f  WRITE ? SQLITE_O
c41a0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20  PEN_READWRITE : 
c41b0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c41c0 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69  ONLY;.  }..  pFi
c41d0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f  le->pMethod = &o
c41e0 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46  s2IoMethod;.  pF
c41f0 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70  ile->h = h;.  Op
c4200 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
c4210 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e   OSTRACE3( "OPEN
c4220 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64   %d pOutFlags=%d
c4230 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
c4240 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65  OutFlags );.  re
c4250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c4260 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
c4270 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a  the named file..
c4280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c4290 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74  2Delete(.  sqlit
c42a0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
c42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c42c0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
c42d0 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   os2 */.  const 
c42e0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
c42f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4300 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
c4310 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
c4320 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20  int syncDir     
c4330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4340 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
c4350 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a  ed on os2 */.){.
c4360 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
c4370 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a  _ERROR;.  char *
c4380 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f  zFilenameCp = co
c4390 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c43a0 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  p( zFilename );.
c43b0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
c43c0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
c43d0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b  _IOERR_DELETE );
c43e0 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74  .  rc = DosDelet
c43f0 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  e( (PSZ)zFilenam
c4400 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  eCp );.  free( z
c4410 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20  FilenameCp );.  
c4420 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54  OSTRACE2( "DELET
c4430 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
c4440 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75  lename );.  retu
c4450 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
c4460 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
c4470 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
c4480 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ETE;.}../*.** Ch
c4490 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63  eck the existanc
c44a0 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20  e and status of 
c44b0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
c44c0 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28  c int os2Access(
c44d0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c44e0 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
c44f0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
c4500 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
c4510 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f  *zFilename,    /
c4520 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
c4530 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74  o check */.  int
c4540 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
c4550 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
c4560 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f  f test to make o
c4570 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  n this file */. 
c4580 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20   int *pOut      
c4590 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
c45a0 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  ite results here
c45b0 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41   */.){.  FILESTA
c45c0 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67  TUS3 fsts3Config
c45d0 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72  Info;.  APIRET r
c45e0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c45f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
c4600 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
c4610 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61  athToCp( zFilena
c4620 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  me );..  memset(
c4630 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
c4640 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74  o, 0, sizeof(fst
c4650 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b  s3ConfigInfo) );
c4660 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79  .  rc = DosQuery
c4670 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a  PathInfo( (PSZ)z
c4680 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f  FilenameCp, FIL_
c4690 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20  STANDARD,.      
c46a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c46b0 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49     &fsts3ConfigI
c46c0 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45  nfo, sizeof(FILE
c46d0 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72  STATUS3) );.  fr
c46e0 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ee( zFilenameCp 
c46f0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
c4700 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66  ACCESS fsts3Conf
c4710 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d  igInfo.attrFile=
c4720 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25  %d flags=%d rc=%
c4730 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
c4740 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66    fsts3ConfigInf
c4750 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67  o.attrFile, flag
c4760 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63  s, rc );.  switc
c4770 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  h( flags ){.    
c4780 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
c4790 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73  SS_READ:.    cas
c47a0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
c47b0 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63  EXISTS:.      rc
c47c0 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52   = (rc == NO_ERR
c47d0 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  OR);.      OSTRA
c47e0 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20  CE3( "ACCESS %s 
c47f0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61  access of read a
c4800 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64  nd exists  rc=%d
c4810 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  \n", zFilename, 
c4820 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  rc );.      brea
c4830 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
c4840 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
c4850 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  ITE:.      rc = 
c4860 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29  (rc == NO_ERROR)
c4870 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66   && ( (fsts3Conf
c4880 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20  igInfo.attrFile 
c4890 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29  & FILE_READONLY)
c48a0 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f   == 0 );.      O
c48b0 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53  STRACE3( "ACCESS
c48c0 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65   %s access of re
c48d0 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c  ad/write  rc=%d\
c48e0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72  n", zFilename, r
c48f0 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  c );.      break
c4900 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
c4910 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49       assert( !"I
c4920 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67  nvalid flags arg
c4930 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20  ument" );.  }.  
c4940 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65  *pOut = rc;.  re
c4950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c4960 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
c4970 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
c4980 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
c4990 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
c49a0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
c49b0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
c49c0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
c49d0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
c49e0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
c49f0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
c4a00 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a  library..*/./*.*
c4a10 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72  * Interfaces for
c4a20 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65   opening a share
c4a30 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69  d library, findi
c4a40 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a  ng entry points.
c4a50 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  ** within the sh
c4a60 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e  ared library, an
c4a70 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68  d closing the sh
c4a80 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  ared library..*/
c4a90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73  .static void *os
c4aa0 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  2DlOpen(sqlite3_
c4ab0 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
c4ac0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
c4ad0 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45  ){.  UCHAR loadE
c4ae0 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55  rr[256];.  HMODU
c4af0 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45  LE hmod;.  APIRE
c4b00 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46  T rc;.  char *zF
c4b10 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
c4b20 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c4b30 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63  zFilename);.  rc
c4b40 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65   = DosLoadModule
c4b50 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73  ((PSZ)loadErr, s
c4b60 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20  izeof(loadErr), 
c4b70 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d  zFilenameCp, &hm
c4b80 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c  od);.  free(zFil
c4b90 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75  enameCp);.  retu
c4ba0 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f  rn rc != NO_ERRO
c4bb0 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68  R ? 0 : (void*)h
c4bc0 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e  mod;.}./*.** A n
c4bd0 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65  o-op since the e
c4be0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
c4bf0 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73  urned on the Dos
c4c00 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e  LoadModule call.
c4c10 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65  .** os2Dlopen re
c4c20 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f  turns zero if Do
c4c30 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e  sLoadModule is n
c4c40 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a  ot successful..*
c4c50 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
c4c60 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33  2DlError(sqlite3
c4c70 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c4c80 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c4c90 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a  Out){./* no-op *
c4ca0 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  /.}.static void 
c4cb0 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65  *os2DlSym(sqlite
c4cc0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
c4cd0 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73  d *pHandle, cons
c4ce0 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29  t char *zSymbol)
c4cf0 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41  {.  PFN pfn;.  A
c4d00 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d  PIRET rc;.  rc =
c4d10 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64   DosQueryProcAdd
c4d20 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64  r((HMODULE)pHand
c4d30 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c  le, 0L, zSymbol,
c4d40 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63   &pfn);.  if( rc
c4d50 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
c4d60 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79      /* if the sy
c4d70 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20  mbol itself was 
c4d80 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63  not found, searc
c4d90 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20  h again for the 
c4da0 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62  same.     * symb
c4db0 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61  ol with an extra
c4dc0 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61   underscore, tha
c4dd0 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65  t might be neede
c4de0 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20  d depending.    
c4df0 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e   * on the callin
c4e00 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a  g convention */.
c4e10 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f      char _zSymbo
c4e20 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20  l[256] = "_";.  
c4e30 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62    strncat(_zSymb
c4e40 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35  ol, zSymbol, 255
c4e50 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51  );.    rc = DosQ
c4e60 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
c4e70 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
c4e80 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66  L, _zSymbol, &pf
c4e90 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
c4ea0 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
c4eb0 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e  ? 0 : (void*)pfn
c4ec0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
c4ed0 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  os2DlClose(sqlit
c4ee0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
c4ef0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
c4f00 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48  DosFreeModule((H
c4f10 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b  MODULE)pHandle);
c4f20 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53  .}.#else /* if S
c4f30 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
c4f40 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66  EXTENSION is def
c4f50 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69  ined: */.  #defi
c4f60 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20  ne os2DlOpen 0. 
c4f70 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72   #define os2DlEr
c4f80 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20  ror 0.  #define 
c4f90 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65  os2DlSym 0.  #de
c4fa0 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20  fine os2DlClose 
c4fb0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
c4fc0 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75   Write up to nBu
c4fd0 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
c4fe0 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e  mness into zBuf.
c4ff0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c5000 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  s2Randomness(sql
c5010 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c5020 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
c5030 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20  zBuf ){.  int n 
c5040 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  = 0;.#if defined
c5050 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20  (SQLITE_TEST).  
c5060 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73  n = nBuf;.  mems
c5070 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66  et(zBuf, 0, nBuf
c5080 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73  );.#else.  int s
c5090 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a  izeofULong = siz
c50a0 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66  eof(ULONG);.  if
c50b0 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41  ( (int)sizeof(DA
c50c0 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20  TETIME) <= nBuf 
c50d0 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54  - n ){.    DATET
c50e0 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65  IME x;.    DosGe
c50f0 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20  tDateTime(&x);. 
c5100 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c5110 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  n], &x, sizeof(x
c5120 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
c5130 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69  eof(x);.  }..  i
c5140 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
c5150 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
c5160 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20    PPIB ppib;.   
c5170 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
c5180 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a  s(NULL, &ppib);.
c5190 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c51a0 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f  [n], &ppib->pib_
c51b0 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f  ulpid, sizeofULo
c51c0 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  ng);.    n += si
c51d0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a  zeofULong;.  }..
c51e0 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c51f0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c5200 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a  .    PTIB ptib;.
c5210 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
c5220 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
c5230 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
c5240 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74  Buf[n], &ptib->t
c5250 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75  ib_ptib2->tib2_u
c5260 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  ltid, sizeofULon
c5270 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
c5280 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20  eofULong;.  }.. 
c5290 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20   /* if we still 
c52a0 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74  haven't filled t
c52b0 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68  he buffer yet th
c52c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c  e following will
c52d0 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76   */.  /* grab ev
c52e0 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e  erything once in
c52f0 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20  stead of making 
c5300 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f  several calls fo
c5310 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20  r a single item 
c5320 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  */.  if( sizeofU
c5330 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c5340 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c   ){.    ULONG ul
c5350 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d  SysInfo[QSV_MAX]
c5360 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79  ;.    DosQuerySy
c5370 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41  sInfo(1L, QSV_MA
c5380 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69  X, ulSysInfo, si
c5390 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f  zeofULong * QSV_
c53a0 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  MAX);..    memcp
c53b0 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c53c0 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f  ysInfo[QSV_MS_CO
c53d0 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66  UNT - 1], sizeof
c53e0 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d  ULong);.    n +=
c53f0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20   sizeofULong;.. 
c5400 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f     if( sizeofULo
c5410 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c5420 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
c5430 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49  zBuf[n], &ulSysI
c5440 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e  nfo[QSV_TIMER_IN
c5450 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a  TERVAL - 1], siz
c5460 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
c5470 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
c5480 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
c5490 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
c54a0 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
c54b0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c54c0 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
c54d0 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c  V_TIME_LOW - 1],
c54e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
c54f0 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66       n += sizeof
c5500 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20  ULong;.    }.   
c5510 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c5520 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c5530 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
c5540 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
c5550 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20  o[QSV_TIME_HIGH 
c5560 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  - 1], sizeofULon
c5570 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73  g);.      n += s
c5580 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20  izeofULong;.    
c5590 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66  }.    if( sizeof
c55a0 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
c55b0 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  n ){.      memcp
c55c0 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c55d0 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56  ysInfo[QSV_TOTAV
c55e0 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a  AILMEM - 1], siz
c55f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
c5600 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
c5610 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  g;.    }.  }.#en
c5620 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b  dif..  return n;
c5630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  .}../*.** Sleep 
c5640 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
c5650 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
c5660 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
c5670 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67  lept..** The arg
c5680 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d  ument is the num
c5690 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
c56a0 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73  nds we want to s
c56b0 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74  leep..** The ret
c56c0 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
c56d0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
c56e0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
c56f0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71   actually.** req
c5700 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20  uested from the 
c5710 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
c5720 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e  ting system, a n
c5730 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d  umber which.** m
c5740 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20  ight be greater 
c5750 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
c5760 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62   the argument, b
c5770 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74  ut not less.** t
c5780 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74  han the argument
c5790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c57a0 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65  os2Sleep( sqlite
c57b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
c57c0 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44   microsec ){.  D
c57d0 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73  osSleep( (micros
c57e0 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65  ec/1000) );.  re
c57f0 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d  turn microsec;.}
c5800 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
c5810 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
c5820 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
c5830 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
c5840 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
c5850 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
c5860 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
c5870 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
c5880 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
c5890 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
c58a0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
c58b0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c58c0 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
c58d0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
c58e0 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
c58f0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
c5900 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
c5910 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
c5920 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
c5930 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
c5940 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
c5950 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
c5960 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
c5970 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
c5980 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
c5990 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
c59a0 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74  /.int os2Current
c59b0 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66  Time( sqlite3_vf
c59c0 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
c59d0 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62  *prNow ){.  doub
c59e0 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20  le now;.  SHORT 
c59f0 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73  minute; /* needs
c5a00 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63   to be able to c
c5a10 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76  ope with negativ
c5a20 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65  e timezone offse
c5a30 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65  t */.  USHORT se
c5a40 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20  cond, hour,.    
c5a50 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c       day, month,
c5a60 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d   year;.  DATETIM
c5a70 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61  E dt;.  DosGetDa
c5a80 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20  teTime( &dt );. 
c5a90 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52   second = (USHOR
c5aa0 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20  T)dt.seconds;.  
c5ab0 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29  minute = (SHORT)
c5ac0 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e  dt.minutes + dt.
c5ad0 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72  timezone;.  hour
c5ae0 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f   = (USHORT)dt.ho
c5af0 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53  urs;.  day = (US
c5b00 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d  HORT)dt.day;.  m
c5b10 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64  onth = (USHORT)d
c5b20 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20  t.month;.  year 
c5b30 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61  = (USHORT)dt.yea
c5b40 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61  r;..  /* Calcula
c5b50 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a  tions from http:
c5b60 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c  //www.astro.keel
c5b70 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74  e.ac.uk/~rno/Ast
c5b80 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a  ronomy/hjd.html.
c5b90 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e       http://www.
c5ba0 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75  astro.keele.ac.u
c5bb0 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79  k/~rno/Astronomy
c5bc0 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20  /hjd-0.1.c */.  
c5bd0 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65  /* Calculate the
c5be0 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a   Julian days */.
c5bf0 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32    now = day - 32
c5c00 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28  076 +.    1461*(
c5c10 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d  year + 4800 + (m
c5c20 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34  onth - 14)/12)/4
c5c30 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74   +.    367*(mont
c5c40 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d  h - 2 - (month -
c5c50 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d   14)/12*12)/12 -
c5c60 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20  .    3*((year + 
c5c70 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4900 + (month - 
c5c80 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a  14)/12)/100)/4;.
c5c90 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72  .  /* Add the fr
c5ca0 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20  actional hours, 
c5cb0 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73  mins and seconds
c5cc0 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f   */.  now += (ho
c5cd0 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b  ur + 12.0)/24.0;
c5ce0 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65  .  now += minute
c5cf0 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b  /1440.0;.  now +
c5d00 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30  = second/86400.0
c5d10 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77  ;.  *prNow = now
c5d20 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
c5d30 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
c5d40 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
c5d50 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
c5d60 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
c5d70 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32  time/86400.0 + 2
c5d80 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65  440587.5;.  }.#e
c5d90 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
c5da0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .}..static int o
c5db0 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  s2GetLastError(s
c5dc0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
c5dd0 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
c5de0 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72   *zBuf){.  retur
c5df0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
c5e00 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
c5e10 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
c5e20 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
c5e30 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c  nterface..*/.SQL
c5e40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c5e50 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
c5e60 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
c5e70 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d  te3_vfs os2Vfs =
c5e80 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
c5e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
c5ea0 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a  rsion */.    siz
c5eb0 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20  eof(os2File),   
c5ec0 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20  /* szOsFile */. 
c5ed0 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20     CCHMAXPATH,  
c5ee0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
c5ef0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
c5f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c5f10 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f   pNext */.    "o
c5f20 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  s2",            
c5f30 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
c5f40 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
c5f50 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
c5f60 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c  */..    os2Open,
c5f70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
c5f80 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65  pen */.    os2De
c5f90 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  lete,         /*
c5fa0 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20   xDelete */.    
c5fb0 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20  os2Access,      
c5fc0 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
c5fd0 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68  .    os2FullPath
c5fe0 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c  name,   /* xFull
c5ff0 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Pathname */.    
c6000 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20  os2DlOpen,      
c6010 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
c6020 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c  .    os2DlError,
c6030 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
c6040 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  ror */.    os2Dl
c6050 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Sym,          /*
c6060 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f   xDlSym */.    o
c6070 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  s2DlClose,      
c6080 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f    /* xDlClose */
c6090 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65  .    os2Randomne
c60a0 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ss,     /* xRand
c60b0 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73  omness */.    os
c60c0 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20  2Sleep,         
c60d0 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20   /* xSleep */.  
c60e0 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65    os2CurrentTime
c60f0 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74  ,    /* xCurrent
c6100 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47  Time */.    os2G
c6110 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f  etLastError    /
c6120 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20  * xGetLastError 
c6130 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  */.  };.  sqlite
c6140 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26  3_vfs_register(&
c6150 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e  os2Vfs, 1);.  in
c6160 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  itUconvObjects()
c6170 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c6180 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  E_OK;.}.SQLITE_A
c6190 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
c61a0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66  s_end(void){.  f
c61b0 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28  reeUconvObjects(
c61c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
c61d0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66  TE_OK;.}..#endif
c61e0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53   /* SQLITE_OS_OS
c61f0 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
c6200 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
c6210 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
c6220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6240 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
c6250 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
c6260 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a   os_unix.c *****
c6270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6290 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
c62a0 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
c62b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
c62c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
c62d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
c62e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
c62f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
c6300 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
c6310 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
c6320 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
c6330 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
c6340 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
c6350 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
c6360 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
c6370 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
c6380 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
c6390 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
c63a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
c63b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
c63c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c63d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c63e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
c6410 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
c6420 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70  ains the VFS imp
c6430 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
c6440 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74  unix-like operat
c6450 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69  ing systems.** i
c6460 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61  nclude Linux, Ma
c6470 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c  cOSX, *BSD, QNX,
c6480 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48   VxWorks, AIX, H
c6490 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e  PUX, and others.
c64a0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
c64b0 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61   actually severa
c64c0 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20  l different VFS 
c64d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
c64e0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a  in this file..**
c64f0 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73   The differences
c6500 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20   are in the way 
c6510 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  that file lockin
c6520 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20  g is done.  The 
c6530 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65  default.** imple
c6540 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50  mentation uses P
c6550 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f  osix Advisory Lo
c6560 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76  cks.  Alternativ
c6570 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
c6580 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29  s.** use flock()
c6590 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72  , dot-files, var
c65a0 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79  ious proprietary
c65b0 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73   locking schemas
c65c0 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73  , or simply.** s
c65d0 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20  kip locking all 
c65e0 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20  together..**.** 
c65f0 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  This source file
c6600 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e   is organized in
c6610 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65  to divisions whe
c6620 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72  re the logic for
c6630 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66   various.** subf
c6640 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74  unctions is cont
c6650 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  ained within the
c6660 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76   appropriate div
c6670 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a  ision.  PLEASE.*
c6680 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43  * KEEP THE STRUC
c6690 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c  TURE OF THIS FIL
c66a0 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63  E INTACT.  New c
c66b0 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c  ode should be pl
c66c0 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63  aced.** in the c
c66d0 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20  orrect division 
c66e0 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c  and should be cl
c66f0 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a  early labeled..*
c6700 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20  *.** The layout 
c6710 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20  of divisions is 
c6720 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
c6730 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70  *   *  General-p
c6740 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69  urpose declarati
c6750 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20  ons and utility 
c6760 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20  functions..**   
c6770 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49  *  Unique file I
c6780 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20  D logic used by 
c6790 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20  VxWorks..**   * 
c67a0 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67   Various locking
c67b0 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65   primitive imple
c67c0 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20  mentations (all 
c67d0 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63  except proxy loc
c67e0 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b  king):.**      +
c67f0 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73   for Posix Advis
c6800 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20  ory Locks.**    
c6810 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f    + for no-op lo
c6820 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f  cks.**      + fo
c6830 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73  r dot-file locks
c6840 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66  .**      + for f
c6850 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a  lock() locking.*
c6860 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d  *      + for nam
c6870 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
c6880 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79  ks (VxWorks only
c6890 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  ).**      + for 
c68a0 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c  AFP filesystem l
c68b0 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c  ocks (MacOSX onl
c68c0 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74  y).**   *  sqlit
c68d0 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20  e3_file methods 
c68e0 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77  not associated w
c68f0 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20  ith locking..** 
c6900 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73    *  Definitions
c6910 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   of sqlite3_io_m
c6920 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
c6930 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a  or all locking.*
c6940 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70  *      methods p
c6950 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e  lus "finder" fun
c6960 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20  ctions for each 
c6970 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a  locking method..
c6980 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f  **   *  sqlite3_
c6990 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65  vfs method imple
c69a0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20  mentations..**  
c69b0 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d   *  Locking prim
c69c0 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70  itives for the p
c69d0 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e  roxy uber-lockin
c69e0 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53  g-method. (MacOS
c69f0 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20  X only).**   *  
c6a00 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73  Definitions of s
c6a10 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
c6a20 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69  ts for all locki
c6a30 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20  ng methods.**   
c6a40 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e     plus implemen
c6a50 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  tations of sqlit
c6a60 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
c6a70 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
c6a80 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  )..*/.#if SQLITE
c6a90 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20  _OS_UNIX        
c6aa0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69        /* This fi
c6ab0 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e  le is used on un
c6ac0 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a  ix only */../*.*
c6ad0 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69  * There are vari
c6ae0 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20  ous methods for 
c6af0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65  file locking use
c6b00 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63  d for concurrenc
c6b10 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a  y.** control:.**
c6b20 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c  .**   1. POSIX l
c6b30 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61  ocking (the defa
c6b40 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f  ult),.**   2. No
c6b50 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33   locking,.**   3
c6b60 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69  . Dot-file locki
c6b70 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63  ng,.**   4. floc
c6b80 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20  k() locking,.** 
c6b90 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67    5. AFP locking
c6ba0 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20   (OSX only),.** 
c6bb0 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58    6. Named POSIX
c6bc0 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57   semaphores (VXW
c6bd0 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20  orks only),.**  
c6be0 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e   7. proxy lockin
c6bf0 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a  g. (OSX only).**
c6c00 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c  .** Styles 4, 5,
c6c10 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20   and 7 are only 
c6c20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c  available of SQL
c6c30 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c6c40 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64  NG_STYLE.** is d
c6c50 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68  efined to 1.  Th
c6c60 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
c6c70 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c  LOCKING_STYLE al
c6c80 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d  so enables autom
c6c90 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f  atic.** selectio
c6ca0 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72  n of the appropr
c6cb0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79  iate locking sty
c6cc0 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  le based on the 
c6cd0 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68  filesystem.** wh
c6ce0 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
c6cf0 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a   is located.  .*
c6d00 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
c6d10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
c6d20 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69  KING_STYLE).#  i
c6d30 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
c6d40 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65  E__).#    define
c6d50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c6d60 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23  OCKING_STYLE 1.#
c6d70 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69    else.#    defi
c6d80 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ne SQLITE_ENABLE
c6d90 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30  _LOCKING_STYLE 0
c6da0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  .#  endif.#endif
c6db0 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74  ../*.** Define t
c6dc0 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72  he OS_VXWORKS pr
c6dd0 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  e-processor macr
c6de0 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69  o to 1 if buildi
c6df0 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b  ng on .** vxwork
c6e00 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73  s, or 0 otherwis
c6e10 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53  e..*/.#ifndef OS
c6e20 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64  _VXWORKS.#  if d
c6e30 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20  efined(__RTP__) 
c6e40 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f  || defined(_WRS_
c6e50 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66  KERNEL).#    def
c6e60 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31  ine OS_VXWORKS 1
c6e70 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65  .#  else.#    de
c6e80 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20  fine OS_VXWORKS 
c6e90 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  0.#  endif.#endi
c6ea0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23  f../*.** These #
c6eb0 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65  defines should e
c6ec0 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20  nable >2GB file 
c6ed0 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78  support on Posix
c6ee0 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72   if the.** under
c6ef0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
c6f00 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20  system supports 
c6f10 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c  it.  If the OS l
c6f20 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69  acks.** large fi
c6f30 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73  le support, thes
c6f40 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f  e should be no-o
c6f50 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20  ps..**.** Large 
c6f60 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e  file support can
c6f70 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69   be disabled usi
c6f80 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f  ng the -DSQLITE_
c6f90 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74  DISABLE_LFS swit
c6fa0 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ch.** on the com
c6fb0 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69  piler command li
c6fc0 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  ne.  This is nec
c6fd0 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72  essary if you ar
c6fe0 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f  e compiling.** o
c6ff0 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69  n a recent machi
c7000 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37  ne (ex: RedHat 7
c7010 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74  .2) but you want
c7020 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f   your code to wo
c7030 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65  rk.** on an olde
c7040 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52  r machine (ex: R
c7050 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20  edHat 6.0).  If 
c7060 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52  you compile on R
c7070 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74  edHat 7.2.** wit
c7080 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e  hout this option
c7090 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e  , LFS is enable.
c70a0 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e    But LFS does n
c70b0 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20  ot exist in the 
c70c0 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64  kernel.** in Red
c70d0 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20  Hat 6.0, so the 
c70e0 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e  code won't work.
c70f0 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78    Hence, for max
c7100 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70  imum binary.** p
c7110 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73  ortability you s
c7120 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a  hould omit LFS..
c7130 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f  **.** The previo
c7140 75 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73  us paragraph was
c7150 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35   written in 2005
c7160 2e 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61  .  (This paragra
c7170 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  ph is written.**
c7180 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29   on 2008-11-28.)
c7190 20 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c   These days, all
c71a0 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73   Linux kernels s
c71b0 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c  upport large fil
c71c0 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68  es, so.** you sh
c71d0 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65  ould probably le
c71e0 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e  ave LFS enabled.
c71f0 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64    But some embed
c7200 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69  ded platforms mi
c7210 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20  ght.** lack LFS 
c7220 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
c7230 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  e SQLITE_DISABLE
c7240 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74  _LFS macro might
c7250 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c   still be useful
c7260 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
c7270 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a  ITE_DISABLE_LFS.
c7280 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f  # define _LARGE_
c7290 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69  FILE       1.# i
c72a0 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53  fndef _FILE_OFFS
c72b0 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69  ET_BITS.#   defi
c72c0 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f  ne _FILE_OFFSET_
c72d0 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a  BITS 64.# endif.
c72e0 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46  # define _LARGEF
c72f0 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e  ILE_SOURCE 1.#en
c7300 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64  dif../*.** stand
c7310 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65  ard include file
c7320 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  s..*/.#include <
c7330 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e  sys/types.h>.#in
c7340 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e  clude <sys/stat.
c7350 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e  h>.#include <fcn
c7360 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  tl.h>.#include <
c7370 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75  unistd.h>.#inclu
c7380 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a  de <sys/time.h>.
c7390 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e  #include <errno.
c73a0 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  h>..#if SQLITE_E
c73b0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
c73c0 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73  YLE.# include <s
c73d0 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66  ys/ioctl.h>.# if
c73e0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69   OS_VXWORKS.#  i
c73f0 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72  nclude <semaphor
c7400 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20  e.h>.#  include 
c7410 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73  <limits.h>.# els
c7420 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79  e.#  include <sy
c7430 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63  s/file.h>.#  inc
c7440 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e  lude <sys/param.
c7450 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73  h>.#  include <s
c7460 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e  ys/mount.h>.# en
c7470 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
c7480 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c7490 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a  ING_STYLE */../*
c74a0 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f  .** If we are to
c74b0 20 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c   be thread-safe,
c74c0 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68   include the pth
c74d0 72 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64  reads header and
c74e0 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53   define.** the S
c74f0 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
c7500 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  DS macro..*/.#if
c7510 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
c7520 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  FE.# define SQLI
c7530 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20  TE_UNIX_THREADS 
c7540 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  1.#endif../*.** 
c7550 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69  Default permissi
c7560 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e  ons when creatin
c7570 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a  g a new file.*/.
c7580 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
c7590 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d  EFAULT_FILE_PERM
c75a0 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65  ISSIONS.# define
c75b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
c75c0 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53  FILE_PERMISSIONS
c75d0 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a   0644.#endif../*
c75e0 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72  . ** Default per
c75f0 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72  missions when cr
c7600 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78  eating auto prox
c7610 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65  y dir. */.#ifnde
c7620 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
c7630 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
c7640 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53  SIONS.# define S
c7650 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52  QLITE_DEFAULT_PR
c7660 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f  OXYDIR_PERMISSIO
c7670 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a  NS 0755.#endif..
c7680 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75  /*.** Maximum su
c7690 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e  pported path-len
c76a0 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  gth..*/.#define 
c76b0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32  MAX_PATHNAME 512
c76c0 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74  ../*.** Only set
c76d0 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69   the lastErrno i
c76e0 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  f the error code
c76f0 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72   is a real error
c7700 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e   and not .** a n
c7710 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72  ormal expected r
c7720 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51  eturn code of SQ
c7730 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c  LITE_BUSY or SQL
c7740 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e  ITE_OK.*/.#defin
c7750 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  e IS_LOCK_ERROR(
c7760 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54  x)  ((x != SQLIT
c7770 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53  E_OK) && (x != S
c7780 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f  QLITE_BUSY)).../
c7790 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20  *.** Sometimes, 
c77a0 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e  after a file han
c77b0 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79  dle is closed by
c77c0 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c   SQLite, the fil
c77d0 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20  e descriptor.** 
c77e0 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64  cannot be closed
c77f0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e   immediately. In
c7800 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e   these cases, in
c7810 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66  stances of the f
c7820 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
c7830 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74  cture are used t
c7840 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65  o store the file
c7850 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c   descriptor whil
c7860 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e  e waiting for an
c7870 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20  .** opportunity 
c7880 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20  to either close 
c7890 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a  or reuse it..*/.
c78a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55  typedef struct U
c78b0 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78  nixUnusedFd Unix
c78c0 55 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74  UnusedFd;.struct
c78d0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a   UnixUnusedFd {.
c78e0 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20    int fd;       
c78f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
c7900 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
c7910 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74  o close */.  int
c7920 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
c7930 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
c7940 74 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69  this file descri
c7950 70 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20  ptor was opened 
c7960 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e  with */.  UnixUn
c7970 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20  usedFd *pNext;  
c7980 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73      /* Next unus
c7990 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
c79a0 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20  or on same file 
c79b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
c79c0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
c79d0 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20  ure is subclass 
c79e0 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  of sqlite3_file 
c79f0 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20  specific to the 
c7a00 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c  unix.** VFS impl
c7a10 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  ementations..*/.
c7a20 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75  typedef struct u
c7a30 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65  nixFile unixFile
c7a40 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c  ;.struct unixFil
c7a50 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  e {.  sqlite3_io
c7a60 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
c7a70 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77  pMethod;  /* Alw
c7a80 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e  ays the first en
c7a90 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  try */.  struct 
c7aa0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
c7ab0 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66  en;       /* Inf
c7ac0 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e  o about all open
c7ad0 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e   fd's on this in
c7ae0 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ode */.  struct 
c7af0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  unixLockInfo *pL
c7b00 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66  ock;      /* Inf
c7b10 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e  o about locks on
c7b20 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20   this inode */. 
c7b30 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
c7b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7b50 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
c7b60 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e  scriptor */.  in
c7b70 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20  t dirfd;        
c7b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c7b90 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
c7ba0 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74  r for the direct
c7bb0 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ory */.  unsigne
c7bc0 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b  d char locktype;
c7bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c7be0 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65   type of lock he
c7bf0 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f  ld on this fd */
c7c00 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f  .  int lastErrno
c7c10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c7c20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20      /* The unix 
c7c30 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c  errno from the l
c7c40 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f  ast I/O error */
c7c50 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67  .  void *locking
c7c60 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20  Context;        
c7c70 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73      /* Locking s
c7c80 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74  tyle specific st
c7c90 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  ate */.  UnixUnu
c7ca0 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20  sedFd *pUnused; 
c7cb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
c7cc0 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55  -allocated UnixU
c7cd0 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74  nusedFd */.  int
c7ce0 20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20   fileFlags;     
c7cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c7d00 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c   Miscellanous fl
c7d10 61 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ags */.#if SQLIT
c7d20 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c7d30 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65  _STYLE.  int ope
c7d40 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  nFlags;         
c7d50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c7d60 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64   flags specified
c7d70 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65   at open() */.#e
c7d80 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
c7d90 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
c7da0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
c7db0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64  .  pthread_t tid
c7dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c7dd0 20 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61      /* The threa
c7de0 64 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68  d that "owns" th
c7df0 69 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23  is unixFile */.#
c7e00 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57  endif.#if OS_VXW
c7e10 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c  ORKS.  int isDel
c7e20 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ete;            
c7e30 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74          /* Delet
c7e40 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72  e on close if tr
c7e50 75 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76  ue */.  struct v
c7e60 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
c7e70 64 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71  d;       /* Uniq
c7e80 75 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65  ue file ID */.#e
c7e90 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45  ndif.#ifndef NDE
c7ea0 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78  BUG.  /* The nex
c7eb0 74 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61  t group of varia
c7ec0 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  bles are used to
c7ed0 20 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f   track whether o
c7ee0 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74  r not the.  ** t
c7ef0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
c7f00 65 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32  er in bytes 24-2
c7f10 37 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  7 of database fi
c7f20 6c 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a  les are updated.
c7f30 20 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e    ** whenever an
c7f40 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  y part of the da
c7f50 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20  tabase changes. 
c7f60 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
c7f70 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63  ult will.  ** oc
c7f80 63 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73  cur if a file is
c7f90 20 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74   updated without
c7fa0 20 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74   also updating t
c7fb0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20  he transaction. 
c7fc0 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68   ** counter.  Th
c7fd0 69 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20  is test is made 
c7fe0 74 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f  to avoid new pro
c7ff0 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f  blems similar to
c8000 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65   the.  ** one de
c8010 73 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65  scribed by ticke
c8020 74 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20  t #3584. .  */. 
c8030 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
c8040 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20  ransCntrChng;   
c8050 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74  /* True if the t
c8060 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
c8070 65 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  er changed */.  
c8080 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62  unsigned char db
c8090 55 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f  Update;        /
c80a0 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61  * True if any pa
c80b0 72 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  rt of database f
c80c0 69 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20  ile changed */. 
c80d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
c80e0 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20  nNormalWrite;   
c80f0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20  /* True if in a 
c8100 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65  normal write ope
c8110 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  ration */.#endif
c8120 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c8130 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74  EST.  /* In test
c8140 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20   mode, increase 
c8150 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73  the size of this
c8160 20 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74   structure a bit
c8170 20 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69   so that .  ** i
c8180 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  t is larger than
c8190 20 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73   the struct Cras
c81a0 68 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e  hFile defined in
c81b0 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20   test6.c..  */. 
c81c0 20 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33   char aPadding[3
c81d0 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f  2];.#endif.};../
c81e0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
c81f0 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  ng macros define
c8200 20 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c   bits in unixFil
c8210 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23  e.fileFlags.*/.#
c8220 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48  define SQLITE_WH
c8230 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47  OLE_FILE_LOCKING
c8240 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73    0x0001   /* Us
c8250 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63  e whole-file loc
c8260 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  king */../*.** I
c8270 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74  nclude code that
c8280 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
c8290 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a  l os_*.c files.*
c82a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c82b0 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d  * Include os_com
c82c0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mon.h in the mid
c82d0 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  dle of os_unix.c
c82e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c82f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c8300 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
c8310 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8340 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
c8350 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
c8360 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c8370 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c8380 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c8390 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c83a0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c83b0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c83c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c83d0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c83e0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c83f0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c8400 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c8410 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c8420 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c8430 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c8440 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c8450 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c8460 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c8470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c84a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c84b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c84c0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c84d0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69   macros and a li
c84e0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65  ttle bit of code
c84f0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
c8500 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65  to.** all of the
c8510 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66   platform-specif
c8520 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63  ic files (os_*.c
c8530 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64  ) and is #includ
c8540 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a  ed into those.**
c8550 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   files..**.** Th
c8560 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  is file should b
c8570 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74  e #included by t
c8580 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20  he os_*.c files 
c8590 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74  only.  It is not
c85a0 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75   a.** general pu
c85b0 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c  rpose header fil
c85c0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f  e..*/.#ifndef _O
c85d0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
c85e0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
c85f0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
c8600 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
c8610 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
c8620 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
c8630 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
c8640 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
c8650 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
c8660 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
c8670 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
c8680 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
c8690 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
c86a0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
c86b0 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
c86c0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
c86d0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
c86e0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
c86f0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
c8700 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
c8710 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
c8720 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
c8730 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
c8740 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
c8750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
c8760 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
c8770 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
c8780 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
c8790 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
c87a0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
c87b0 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
c87c0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
c87d0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
c87e0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c87f0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c8800 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
c8810 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
c8820 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
c8830 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
c8840 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
c8850 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
c8860 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
c8870 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c8880 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c8890 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
c88a0 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
c88b0 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
c88c0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
c88d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c88e0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
c88f0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
c8900 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
c8910 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
c8920 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c8930 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
c8940 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8950 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
c8960 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
c8970 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
c8980 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
c8990 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
c89a0 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
c89b0 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
c89c0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
c89d0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
c89e0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
c89f0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
c8a00 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
c8a10 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
c8a20 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
c8a30 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
c8a40 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
c8a50 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
c8a60 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
c8a70 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
c8a80 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
c8a90 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
c8aa0 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
c8ab0 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
c8ac0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
c8ad0 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
c8ae0 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
c8af0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
c8b00 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
c8b10 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
c8b20 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
c8b30 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
c8b40 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
c8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
c8b60 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
c8b70 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
c8b80 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
c8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
c8bb0 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
c8bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
c8bf0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
c8c00 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
c8c10 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
c8c20 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
c8c30 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
c8c40 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
c8c50 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
c8c60 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
c8c70 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
c8c80 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
c8c90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
c8ca0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
c8cb0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
c8cc0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
c8cd0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
c8ce0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
c8cf0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
c8d00 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
c8d10 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
c8d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
c8d70 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
c8d80 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
c8d90 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
c8da0 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
c8db0 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
c8dc0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23  class CPUs..*/.#
c8dd0 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48  ifndef _HWTIME_H
c8de0 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d  _.#define _HWTIM
c8df0 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  E_H_../*.** The 
c8e00 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
c8e10 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20  e only works on 
c8e20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f  pentium-class (o
c8e30 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73  r newer) process
c8e40 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20  ors..** It uses 
c8e50 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65  the RDTSC opcode
c8e60 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63   to read the cyc
c8e70 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f  le count value o
c8e80 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f  ut of the.** pro
c8e90 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72  cessor and retur
c8ea0 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20  ns that value.  
c8eb0 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
c8ec0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a   for high-res.**
c8ed0 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23   profiling..*/.#
c8ee0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
c8ef0 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  UC__) || defined
c8f00 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c  (_MSC_VER)) && \
c8f10 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28  .      (defined(
c8f20 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64  i386) || defined
c8f30 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65  (__i386__) || de
c8f40 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a  fined(_M_IX86)).
c8f50 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f  .  #if defined(_
c8f60 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e  _GNUC__)..  __in
c8f70 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
c8f80 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
c8f90 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75  me(void){.     u
c8fa0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20  nsigned int lo, 
c8fb0 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  hi;.     __asm__
c8fc0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
c8fd0 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c  rdtsc" : "=a" (l
c8fe0 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a  o), "=d" (hi));.
c8ff0 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c       return (sql
c9000 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c  ite_uint64)hi <<
c9010 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20   32 | lo;.  }.. 
c9020 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f   #elif defined(_
c9030 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65  MSC_VER)..  __de
c9040 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f  clspec(naked) __
c9050 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69  inline sqlite_ui
c9060 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c  nt64 __cdecl sql
c9070 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c9080 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20  {.     __asm {. 
c9090 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20         rdtsc.   
c90a0 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b       ret       ;
c90b0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74   return value at
c90c0 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a   EDX:EAX.     }.
c90d0 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23    }..  #endif..#
c90e0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
c90f0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
c9100 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a  ed(__x86_64__)).
c9110 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
c9120 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
c9130 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
c9140 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
c9150 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20  long val;.      
c9160 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
c9170 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20  le__ ("rdtsc" : 
c9180 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20  "=A" (val));.   
c9190 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20     return val;. 
c91a0 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69   }. .#elif (defi
c91b0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
c91c0 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f   defined(__ppc__
c91d0 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
c91e0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
c91f0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
c9200 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
c9210 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74  ed long long ret
c9220 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67  val;.      unsig
c9230 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20  ned long junk;. 
c9240 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
c9250 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a  olatile__ ("\n\.
c9260 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20            1:    
c9270 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a    mftbu   %1\n\.
c9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9290 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c    mftb    %L0\n\
c92a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c92b0 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c     mftbu   %0\n\
c92c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c92d0 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31     cmpw    %0,%1
c92e0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
c92f0 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62        bne     1b
c9300 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
c9310 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76      : "=r" (retv
c9320 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29  al), "=r" (junk)
c9330 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
c9340 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c  retval;.  }..#el
c9350 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65  se..  #error Nee
c9360 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  d implementation
c9370 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d   of sqlite3Hwtim
c9380 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
c9390 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a  tform...  /*.  *
c93a0 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74  * To compile wit
c93b0 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  hout implementin
c93c0 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  g sqlite3Hwtime(
c93d0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
c93e0 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61  orm,.  ** you ca
c93f0 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f  n remove the abo
c9400 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73  ve #error and us
c9410 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  e the following.
c9420 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69    ** stub functi
c9430 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f  on.  You will lo
c9440 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72  se timing suppor
c9450 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20  t for many.  ** 
c9460 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67  of the debugging
c9470 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69   and testing uti
c9480 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73  lities, but it s
c9490 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65  hould at.  ** le
c94a0 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  ast compile and 
c94b0 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45  run..  */.SQLITE
c94c0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74  _PRIVATE   sqlit
c94d0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
c94e0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65  Hwtime(void){ re
c94f0 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69  turn ((sqlite_ui
c9500 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64  nt64)0); }..#end
c9510 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  if..#endif /* !d
c9520 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48  efined(_HWTIME_H
c9530 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
c9540 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77  ****** End of hw
c9550 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  time.h *********
c9560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9580 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
c9590 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
c95a0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
c95b0 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e  off in os_common
c95c0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
c95d0 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73  *****/..static s
c95e0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73  qlite_uint64 g_s
c95f0 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c  tart;.static sql
c9600 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61  ite_uint64 g_ela
c9610 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49  psed;.#define TI
c9620 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20  MER_START       
c9630 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48  g_start=sqlite3H
c9640 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20  wtime().#define 
c9650 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20  TIMER_END       
c9660 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69    g_elapsed=sqli
c9670 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74  te3Hwtime()-g_st
c9680 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  art.#define TIME
c9690 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f  R_ELAPSED     g_
c96a0 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64  elapsed.#else.#d
c96b0 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52  efine TIMER_STAR
c96c0 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  T.#define TIMER_
c96d0 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  END.#define TIME
c96e0 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28  R_ELAPSED     ((
c96f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29  sqlite_uint64)0)
c9700 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
c9710 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
c9720 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53  h the SQLITE_TES
c9730 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65  T macro set, the
c9740 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
c9750 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65  block.** of code
c9760 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68   will give us th
c9770 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d  e ability to sim
c9780 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f  ulate a disk I/O
c9790 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a   error.  This.**
c97a0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
c97b0 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63  ting the I/O rec
c97c0 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a  overy logic..*/.
c97d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c97e0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
c97f0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
c9800 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20  or_hit = 0;     
c9810 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
c9820 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72  number of I/O Er
c9830 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
c9840 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
c9850 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20  o_error_hardhit 
c9860 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  = 0;        /* N
c9870 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e  umber of non-ben
c9880 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51  ign errors */.SQ
c9890 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c98a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
c98b0 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20  nding = 0;      
c98c0 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20    /* Count down 
c98d0 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72  to first I/O err
c98e0 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  or */.SQLITE_API
c98f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
c9900 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20  error_persist = 
c9910 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
c9920 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20  e if I/O errors 
c9930 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54  persist */.SQLIT
c9940 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c9950 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
c9960 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  n = 0;         /
c9970 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73  * True if errors
c9980 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53   are benign */.S
c9990 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c99a0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
c99b0 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49  ending = 0;.SQLI
c99c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c99d0 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b  e3_diskfull = 0;
c99e0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c99f0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
c9a00 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
c9a10 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64  or_benign=(X).#d
c9a20 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
c9a30 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20  Error(CODE)  \. 
c9a40 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f   if( (sqlite3_io
c9a50 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26  _error_persist &
c9a60 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  & sqlite3_io_err
c9a70 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20  or_hit) \.      
c9a80 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65   || sqlite3_io_e
c9a90 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d  rror_pending-- =
c9aa0 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20  = 1 )  \.       
c9ab0 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69         { local_i
c9ac0 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a  oerr(); CODE; }.
c9ad0 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61  static void loca
c9ae0 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54  l_ioerr(){.  IOT
c9af0 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29  RACE(("IOERR\n")
c9b00 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  );.  sqlite3_io_
c9b10 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69  error_hit++;.  i
c9b20 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( !sqlite3_io_e
c9b30 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71  rror_benign ) sq
c9b40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c9b50 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66  ardhit++;.}.#def
c9b60 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ine SimulateDisk
c9b70 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20  fullError(CODE) 
c9b80 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  \.   if( sqlite3
c9b90 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
c9ba0 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20  g ){ \.     if( 
c9bb0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c9bc0 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b  _pending == 1 ){
c9bd0 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f   \.       local_
c9be0 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20  ioerr(); \.     
c9bf0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
c9c00 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20  ll = 1; \.      
c9c10 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9c20 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20  r_hit = 1; \.   
c9c30 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20      CODE; \.    
c9c40 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20   }else{ \.      
c9c50 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c9c60 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20  l_pending--; \. 
c9c70 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c      } \.   }.#el
c9c80 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  se.#define Simul
c9c90 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
c9ca0 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (X).#define Simu
c9cb0 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23  lateIOError(A).#
c9cc0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
c9cd0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a  iskfullError(A).
c9ce0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68  #endif../*.** Wh
c9cf0 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70  en testing, keep
c9d00 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20   a count of the 
c9d10 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66  number of open f
c9d20 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  iles..*/.#ifdef 
c9d30 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
c9d40 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c9d50 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75  e3_open_file_cou
c9d60 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  nt = 0;.#define 
c9d70 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20  OpenCounter(X)  
c9d80 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
c9d90 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c  e_count+=(X).#el
c9da0 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43  se.#define OpenC
c9db0 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66  ounter(X).#endif
c9dc0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
c9dd0 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ined(_OS_COMMON_
c9de0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
c9df0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
c9e00 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
c9e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
c9e40 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
c9e50 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
c9e60 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e   off in os_unix.
c9e70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c9e80 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44  ******/../*.** D
c9e90 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61  efine various ma
c9ea0 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69  cros that are mi
c9eb0 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20  ssing from some 
c9ec0 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e  systems..*/.#ifn
c9ed0 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a  def O_LARGEFILE.
c9ee0 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45  # define O_LARGE
c9ef0 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69  FILE 0.#endif.#i
c9f00 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  fdef SQLITE_DISA
c9f10 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20  BLE_LFS.# undef 
c9f20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65  O_LARGEFILE.# de
c9f30 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45  fine O_LARGEFILE
c9f40 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65   0.#endif.#ifnde
c9f50 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64  f O_NOFOLLOW.# d
c9f60 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57  efine O_NOFOLLOW
c9f70 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65   0.#endif.#ifnde
c9f80 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66  f O_BINARY.# def
c9f90 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23  ine O_BINARY 0.#
c9fa0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
c9fb0 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20   DJGPP compiler 
c9fc0 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b  environment look
c9fd0 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e  s mostly like Un
c9fe0 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61  ix, but it.** la
c9ff0 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20  cks the fcntl() 
ca000 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f  system call.  So
ca010 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28   redefine fcntl(
ca020 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e  ) to be somethin
ca030 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73  g.** that always
ca040 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73   succeeds.  This
ca050 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b   means that lock
ca060 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63  ing does not occ
ca070 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50  ur under.** DJGP
ca080 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f  P.  But it is DO
ca090 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75  S - what did you
ca0a0 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64   expect?.*/.#ifd
ca0b0 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64  ef __DJGPP__.# d
ca0c0 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c  efine fcntl(A,B,
ca0d0 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  C) 0.#endif../*.
ca0e0 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 20  ** The threadid 
ca0f0 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74  macro resolves t
ca100 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 20  o the thread-id 
ca110 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66  or to 0.  Used f
ca120 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e  or.** testing an
ca130 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
ca140 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
ca150 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 69  THREADSAFE.#defi
ca160 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68 72  ne threadid pthr
ca170 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65  ead_self().#else
ca180 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69  .#define threadi
ca190 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  d 0.#endif.../*.
ca1a0 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
ca1b0 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e  ons to obtain an
ca1c0 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65  d relinquish the
ca1d0 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54   global mutex. T
ca1e0 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74  he.** global mut
ca1f0 65 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 72  ex is used to pr
ca200 6f 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70  otect the unixOp
ca210 65 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49  enCnt, unixLockI
ca220 6e 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72  nfo and.** vxwor
ca230 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73  ksFileId objects
ca240 20 75 73 65 64 20 62 79 20 74 68 69 73 20 66 69   used by this fi
ca250 6c 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68  le, all of which
ca260 20 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72   may be .** shar
ca270 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74  ed by multiple t
ca280 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75  hreads..**.** Fu
ca290 6e 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78  nction unixMutex
ca2a0 48 65 6c 64 28 29 20 69 73 20 75 73 65 64 20 74  Held() is used t
ca2b0 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20  o assert() that 
ca2c0 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  the global mutex
ca2d0 20 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65   .** is held whe
ca2e0 6e 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73  n required. This
ca2f0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c   function is onl
ca300 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  y used as part o
ca310 66 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73  f assert() .** s
ca320 74 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a  tatements. e.g..
ca330 2a 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65  **.**   unixEnte
ca340 72 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20  rMutex().**     
ca350 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65  assert( unixMute
ca360 78 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20  xHeld() );.**   
ca370 75 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29  unixEnterLeave()
ca380 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
ca390 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76  unixEnterMutex(v
ca3a0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
ca3b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
ca3c0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
ca3d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
ca3e0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74  C_MASTER));.}.st
ca3f0 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65  atic void unixLe
ca400 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  aveMutex(void){.
ca410 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
ca420 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74  leave(sqlite3Mut
ca430 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
ca440 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
ca450 45 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53  ER));.}.#ifdef S
ca460 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74  QLITE_DEBUG.stat
ca470 69 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78  ic int unixMutex
ca480 48 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72  Held(void) {.  r
ca490 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75  eturn sqlite3_mu
ca4a0 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
ca4b0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
ca4c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
ca4d0 41 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69  ASTER));.}.#endi
ca4e0 66 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  f...#ifdef SQLIT
ca4f0 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65  E_DEBUG./*.** He
ca500 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  lper function fo
ca510 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74  r printing out t
ca520 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  race information
ca530 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a   from debugging.
ca540 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69  ** binaries. Thi
ca550 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74  s returns the st
ca560 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69  ring represetati
ca570 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69  on of the suppli
ca580 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f  ed.** integer lo
ca590 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74  ck-type..*/.stat
ca5a0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c  ic const char *l
ca5b0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20  ocktypeName(int 
ca5c0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69  locktype){.  swi
ca5d0 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b  tch( locktype ){
ca5e0 0a 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43  .    case NO_LOC
ca5f0 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22  K: return "NONE"
ca600 3b 0a 20 20 20 20 63 61 73 65 20 53 48 41 52 45  ;.    case SHARE
ca610 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22  D_LOCK: return "
ca620 53 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73  SHARED";.    cas
ca630 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a  e RESERVED_LOCK:
ca640 20 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45   return "RESERVE
ca650 44 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e  D";.    case PEN
ca660 44 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72  DING_LOCK: retur
ca670 6e 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20  n "PENDING";.   
ca680 20 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f   case EXCLUSIVE_
ca690 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58  LOCK: return "EX
ca6a0 43 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20  CLUSIVE";.  }.  
ca6b0 72 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a  return "ERROR";.
ca6c0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
ca6d0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41   SQLITE_LOCK_TRA
ca6e0 43 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f  CE./*.** Print o
ca6f0 75 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ut information a
ca700 62 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67  bout all locking
ca710 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a   operations..**.
ca720 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ca730 69 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75  is used for trou
ca740 62 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b  bleshooting lock
ca750 73 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64  s on multithread
ca760 65 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e  ed.** platforms.
ca770 20 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70    Enable by comp
ca780 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d  iling with the -
ca790 44 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41  DSQLITE_LOCK_TRA
ca7a0 43 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69  CE.** command-li
ca7b0 6e 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65  ne option on the
ca7c0 20 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73   compiler.  This
ca7d0 20 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c   code is normall
ca7e0 79 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e  y.** turned off.
ca7f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
ca800 6f 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c  ockTrace(int fd,
ca810 20 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20   int op, struct 
ca820 66 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61  flock *p){.  cha
ca830 72 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79  r *zOpName, *zTy
ca840 70 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69  pe;.  int s;.  i
ca850 6e 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20  nt savedErrno;. 
ca860 20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b   if( op==F_GETLK
ca870 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20   ){.    zOpName 
ca880 3d 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c  = "GETLK";.  }el
ca890 73 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54  se if( op==F_SET
ca8a0 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d  LK ){.    zOpNam
ca8b0 65 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d  e = "SETLK";.  }
ca8c0 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63  else{.    s = fc
ca8d0 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a  ntl(fd, op, p);.
ca8e0 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
ca8f0 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e  Printf("fcntl un
ca900 6b 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e  known %d %d %d\n
ca910 22 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20  ", fd, op, s);. 
ca920 20 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d     return s;.  }
ca930 0a 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65  .  if( p->l_type
ca940 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20  ==F_RDLCK ){.   
ca950 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22   zType = "RDLCK"
ca960 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
ca970 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b  >l_type==F_WRLCK
ca980 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   ){.    zType = 
ca990 22 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65  "WRLCK";.  }else
ca9a0 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d   if( p->l_type==
ca9b0 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a  F_UNLCK ){.    z
ca9c0 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a  Type = "UNLCK";.
ca9d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
ca9e0 65 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20  ert( 0 );.  }.  
ca9f0 61 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65  assert( p->l_whe
caa00 6e 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b  nce==SEEK_SET );
caa10 0a 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c  .  s = fcntl(fd,
caa20 20 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64   op, p);.  saved
caa30 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
caa40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
caa50 6e 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64  ntf("fcntl %d %d
caa60 20 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20   %s %s %d %d %d 
caa70 25 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65  %d\n",.     thre
caa80 61 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d  adid, fd, zOpNam
caa90 65 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70  e, zType, (int)p
caaa0 2d 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29  ->l_start, (int)
caab0 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28  p->l_len,.     (
caac0 69 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29  int)p->l_pid, s)
caad0 3b 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20  ;.  if( s==(-1) 
caae0 26 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26  && op==F_SETLK &
caaf0 26 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  & (p->l_type==F_
cab00 52 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79  RDLCK || p->l_ty
cab10 70 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a  pe==F_WRLCK) ){.
cab20 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b      struct flock
cab30 20 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70   l2;.    l2 = *p
cab40 3b 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20  ;.    fcntl(fd, 
cab50 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20  F_GETLK, &l2);. 
cab60 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65     if( l2.l_type
cab70 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20  ==F_RDLCK ){.   
cab80 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43     zType = "RDLC
cab90 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  K";.    }else if
caba0 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57  ( l2.l_type==F_W
cabb0 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54  RLCK ){.      zT
cabc0 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20  ype = "WRLCK";. 
cabd0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e     }else if( l2.
cabe0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20  l_type==F_UNLCK 
cabf0 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  ){.      zType =
cac00 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65   "UNLCK";.    }e
cac10 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
cac20 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  t( 0 );.    }.  
cac30 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
cac40 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c  intf("fcntl-fail
cac50 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25  ure-reason: %s %
cac60 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20  d %d %d\n",.    
cac70 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c     zType, (int)l
cac80 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29  2.l_start, (int)
cac90 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c  l2.l_len, (int)l
caca0 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20  2.l_pid);.  }.  
cacb0 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72  errno = savedErr
cacc0 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a  no;.  return s;.
cacd0 7d 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20  }.#define fcntl 
cace0 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66  lockTrace.#endif
cacf0 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f   /* SQLITE_LOCK_
cad00 54 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a  TRACE */..../*.*
cad10 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74  * This routine t
cad20 72 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e  ranslates a stan
cad30 64 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f  dard POSIX errno
cad40 20 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74   code into somet
cad50 68 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74  hing.** useful t
cad60 6f 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66  o the clients of
cad70 20 74 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e   the sqlite3 fun
cad80 63 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69  ctions.  Specifi
cad90 63 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20  cally, it is.** 
cada0 69 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e  intended to tran
cadb0 73 6c 61 74 65 20 61 20 76 61 72 69 65 74 79 20  slate a variety 
cadc0 6f 66 20 22 74 72 79 20 61 67 61 69 6e 22 20 65  of "try again" e
cadd0 72 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54  rrors into SQLIT
cade0 45 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20  E_BUSY.** and a 
cadf0 76 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61  variety of "plea
cae00 73 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c  se close the fil
cae10 65 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57  e descriptor NOW
cae20 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a  " errors into .*
cae30 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a  * SQLITE_IOERR.*
cae40 2a 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72  * .** Errors dur
cae50 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
cae60 6f 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20  on of locks, or 
cae70 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70  file system supp
cae80 6f 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a  ort for locks,.*
cae90 2a 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20  * should handle 
caea0 45 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c  ENOLCK, ENOTSUP,
caeb0 20 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61   EOPNOTSUPP sepa
caec0 72 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69  rately..*/.stati
caed0 63 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f  c int sqliteErro
caee0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
caef0 69 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20  int posixError, 
caf00 69 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29  int sqliteIOErr)
caf10 20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73   {.  switch (pos
caf20 69 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73  ixError) {.  cas
caf30 65 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e  e 0: .    return
caf40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
caf50 0a 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a  .  case EAGAIN:.
caf60 20 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54    case ETIMEDOUT
caf70 3a 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a  :.  case EBUSY:.
caf80 20 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20    case EINTR:.  
caf90 63 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20  case ENOLCK:  . 
cafa0 20 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53     /* random NFS
cafb0 20 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e   retry error, un
cafc0 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65  less during file
cafd0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20   system support 
cafe0 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65  .     * introspe
caff0 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20  ction, in which 
cb000 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e  it actually mean
cb010 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a  s what it says *
cb020 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
cb030 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20  ITE_BUSY;.    . 
cb040 20 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20   case EACCES: . 
cb050 20 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20     /* EACCES is 
cb060 6c 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69  like EAGAIN duri
cb070 6e 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61  ng locking opera
cb080 74 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61  tions, but not a
cb090 6e 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a  ny other time*/.
cb0a0 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 49      if( (sqliteI
cb0b0 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49  OErr == SQLITE_I
cb0c0 4f 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09  OERR_LOCK) || ..
cb0d0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
cb0e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
cb0f0 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74  OCK) || ..(sqlit
cb100 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45  eIOErr == SQLITE
cb110 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c  _IOERR_RDLOCK) |
cb120 7c 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20  |..(sqliteIOErr 
cb130 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  == SQLITE_IOERR_
cb140 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
cb150 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  K) ){.      retu
cb160 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
cb170 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73      }.    /* els
cb180 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a  e fall through *
cb190 2f 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20  /.  case EPERM: 
cb1a0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
cb1b0 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20  TE_PERM;.    .  
cb1c0 63 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20  case EDEADLK:.  
cb1d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cb1e0 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20  IOERR_BLOCKED;. 
cb1f0 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55     .#if EOPNOTSU
cb200 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61  PP!=ENOTSUP.  ca
cb210 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a  se EOPNOTSUPP: .
cb220 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67      /* something
cb230 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61   went terribly a
cb240 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69  wry, unless duri
cb250 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73  ng file system s
cb260 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69  upport .     * i
cb270 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e  ntrospection, in
cb280 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c   which it actual
cb290 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74  ly means what it
cb2a0 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a   says */.#endif.
cb2b0 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20  #ifdef ENOTSUP. 
cb2c0 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a   case ENOTSUP: .
cb2d0 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66      /* invalid f
cb2e0 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67  d, unless during
cb2f0 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70   file system sup
cb300 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69  port introspecti
cb310 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20  on, in which .  
cb320 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79     * it actually
cb330 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73   means what it s
cb340 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ays */.#endif.  
cb350 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65  case EIO:.  case
cb360 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45   EBADF:.  case E
cb370 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e  INVAL:.  case EN
cb380 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45  OTCONN:.  case E
cb390 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e  NODEV:.  case EN
cb3a0 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45  XIO:.  case ENOE
cb3b0 4e 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c  NT:.  case ESTAL
cb3c0 45 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53  E:.  case ENOSYS
cb3d0 3a 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73  :.    /* these s
cb3e0 68 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20  hould force the 
cb3f0 63 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20  client to close 
cb400 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63  the file and rec
cb410 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20  onnect */.    . 
cb420 20 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72   default: .    r
cb430 65 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72  eturn sqliteIOEr
cb440 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a  r;.  }.}..../***
cb450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
cb4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
cb4b0 65 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65  egin Unique File
cb4c0 20 49 44 20 55 74 69 6c 69 74 79 20 55 73 65 64   ID Utility Used
cb4d0 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a   By VxWorks ****
cb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
cb4f0 2a 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f  * On most versio
cb500 6e 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63  ns of unix, we c
cb510 61 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65 20  an get a unique 
cb520 49 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79  ID for a file by
cb530 20 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a   concatenating.*
cb540 2a 20 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d  * the device num
cb550 62 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64  ber and the inod
cb560 65 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74  e number.  But t
cb570 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  his does not wor
cb580 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a  k on VxWorks..**
cb590 20 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75   On VxWorks, a u
cb5a0 6e 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75  nique file id mu
cb5b0 73 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74  st be based on t
cb5c0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c  he canonical fil
cb5d0 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70  ename..**.** A p
cb5e0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
cb5f0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
cb600 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
cb610 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
cb620 61 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65  a.** unique file
cb630 20 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20   ID in VxWorks. 
cb640 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f   Each instance o
cb650 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
cb660 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63   contains.** a c
cb670 6f 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e  opy of the canon
cb680 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20  ical filename.  
cb690 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20  There is also a 
cb6a0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
cb6b0 20 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74    .** The struct
cb6c0 75 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64  ure is reclaimed
cb6d0 20 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72   when the number
cb6e0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
cb6f0 69 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a  it drops to.** z
cb700 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ero..**.** There
cb710 20 61 72 65 20 6e 65 76 65 72 20 76 65 72 79 20   are never very 
cb720 6d 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20  many files open 
cb730 61 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20  at one time and 
cb740 6c 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a  lookups are not.
cb750 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65  ** a performance
cb760 2d 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20  -critical path, 
cb770 73 6f 20 69 74 20 69 73 20 73 75 66 66 69 63 69  so it is suffici
cb780 65 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73 65  ent to put these
cb790 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f  .** structures o
cb7a0 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  n a linked list.
cb7b0 0a 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72  .*/.struct vxwor
cb7c0 6b 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72  ksFileId {.  str
cb7d0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cb7e0 64 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65  d *pNext;  /* Ne
cb7f0 78 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20  xt in a list of 
cb800 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e  them all */.  in
cb810 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
cb820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
cb830 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
cb840 63 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20  ces to this one 
cb850 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
cb860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb870 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
cb880 74 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  the zCanonicalNa
cb890 6d 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20  me[] string */. 
cb8a0 20 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61   char *zCanonica
cb8b0 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f  lName;         /
cb8c0 2a 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65  * Canonical file
cb8d0 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20  name */.};..#if 
cb8e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a  OS_VXWORKS./* .*
cb8f0 2a 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c  * All unique fil
cb900 65 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20  enames are held 
cb910 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  on a linked list
cb920 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 0a   headed by this.
cb930 2a 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a  ** variable:.*/.
cb940 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78  static struct vx
cb950 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77  worksFileId *vxw
cb960 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30  orksFileList = 0
cb970 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66  ;../*.** Simplif
cb980 79 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74  y a filename int
cb990 6f 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20  o its canonical 
cb9a0 66 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e  form.** by makin
cb9b0 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  g the following 
cb9c0 63 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  changes:.**.**  
cb9d0 2a 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74  * removing any t
cb9e0 72 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c  railing and dupl
cb9f0 69 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f  icate /.**  * co
cba00 6e 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a  nvert /./ into j
cba10 75 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76  ust /.**  * conv
cba20 65 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65  ert /A/../ where
cba30 20 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65   A is any simple
cba40 20 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20   name into just 
cba50 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20  /.**.** Changes 
cba60 61 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63  are made in-plac
cba70 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e  e.  Return the n
cba80 65 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a  ew name length..
cba90 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e  **.** The origin
cbaa0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69  al filename is i
cbab0 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65  n z[0..n-1].  Re
cbac0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
cbad0 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  of.** characters
cbae0 20 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69   in the simplifi
cbaf0 65 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74  ed name..*/.stat
cbb00 69 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69  ic int vxworksSi
cbb10 6d 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20  mplifyName(char 
cbb20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e  *z, int n){.  in
cbb30 74 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28  t i, j;.  while(
cbb40 20 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d   n>1 && z[n-1]==
cbb50 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20  '/' ){ n--; }.  
cbb60 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20  for(i=j=0; i<n; 
cbb70 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b  i++){.    if( z[
cbb80 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20  i]=='/' ){.     
cbb90 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27   if( z[i+1]=='/'
cbba0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
cbbb0 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27     if( z[i+1]=='
cbbc0 2e 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a  .' && i+2<n && z
cbbd0 5b 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  [i+2]=='/' ){.  
cbbe0 20 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20        i += 1;.  
cbbf0 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
cbc00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
cbc10 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26  ( z[i+1]=='.' &&
cbc20 20 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d   i+3<n && z[i+2]
cbc30 3d 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d  =='.' && z[i+3]=
cbc40 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='/' ){.        
cbc50 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b  while( j>0 && z[
cbc60 6a 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d  j-1]!='/' ){ j--
cbc70 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
cbc80 6a 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20  j>0 ){ j--; }.  
cbc90 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20        i += 2;.  
cbca0 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
cbcb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
cbcc0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b    z[j++] = z[i];
cbcd0 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b  .  }.  z[j] = 0;
cbce0 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a  .  return j;.}..
cbcf0 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69  /*.** Find a uni
cbd00 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20  que file ID for 
cbd10 74 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75  the given absolu
cbd20 74 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65  te pathname.  Re
cbd30 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
cbd40 72 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73  r to the vxworks
cbd50 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20  FileId object.  
cbd60 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20  This pointer is 
cbd70 74 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69  the unique.** fi
cbd80 6c 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  le ID..**.** The
cbd90 20 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74   nRef field of t
cbda0 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  he vxworksFileId
cbdb0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65   object is incre
cbdc0 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a  mented before.**
cbdd0 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72   the object is r
cbde0 65 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20  eturned.  A new 
cbdf0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62  vxworksFileId ob
cbe00 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 0a  ject is created.
cbe10 2a 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20  ** and added to 
cbe20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20  the global list 
cbe30 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  if necessary..**
cbe40 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
cbe50 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
cbe60 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
cbe70 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
cbe80 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cbe90 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e  leId *vxworksFin
cbea0 64 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68  dFileId(const ch
cbeb0 61 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d  ar *zAbsoluteNam
cbec0 65 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77  e){.  struct vxw
cbed0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77  orksFileId *pNew
cbee0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61  ;         /* sea
cbef0 72 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20  rch key and new 
cbf00 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72  file ID */.  str
cbf10 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cbf20 64 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20  d *pCandidate;  
cbf30 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
cbf40 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 66 69  over existing fi
cbf50 6c 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20  le IDs */.  int 
cbf60 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
cbf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cbf80 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62  /* Length of zAb
cbf90 73 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e  soluteName strin
cbfa0 67 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  g */..  assert( 
cbfb0 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d  zAbsoluteName[0]
cbfc0 3d 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28  =='/' );.  n = (
cbfd0 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f  int)strlen(zAbso
cbfe0 6c 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65  luteName);.  pNe
cbff0 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  w = sqlite3_mall
cc000 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  oc( sizeof(*pNew
cc010 29 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69  ) + (n+1) );.  i
cc020 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
cc030 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a  urn 0;.  pNew->z
cc040 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20  CanonicalName = 
cc050 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b  (char*)&pNew[1];
cc060 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e  .  memcpy(pNew->
cc070 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20  zCanonicalName, 
cc080 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e  zAbsoluteName, n
cc090 2b 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72  +1);.  n = vxwor
cc0a0 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70  ksSimplifyName(p
cc0b0 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e  New->zCanonicalN
cc0c0 61 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53  ame, n);..  /* S
cc0d0 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69  earch for an exi
cc0e0 73 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74  sting entry that
cc0f0 20 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63 61   matching the ca
cc100 6e 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20  nonical name..  
cc110 2a 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63  ** If found, inc
cc120 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
cc130 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72  ence count and r
cc140 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
cc150 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73  to.  ** the exis
cc160 74 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20  ting file ID..  
cc170 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  */.  unixEnterMu
cc180 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61  tex();.  for(pCa
cc190 6e 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46  ndidate=vxworksF
cc1a0 69 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64  ileList; pCandid
cc1b0 61 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d  ate; pCandidate=
cc1c0 70 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78  pCandidate->pNex
cc1d0 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e  t){.    if( pCan
cc1e0 64 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e  didate->nName==n
cc1f0 20 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70   .     && memcmp
cc200 28 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61  (pCandidate->zCa
cc210 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65  nonicalName, pNe
cc220 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  w->zCanonicalNam
cc230 65 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a  e, n)==0.    ){.
cc240 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
cc250 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20  ree(pNew);.     
cc260 20 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52    pCandidate->nR
cc270 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69  ef++;.       uni
cc280 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
cc290 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 61        return pCa
cc2a0 6e 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20  ndidate;.    }. 
cc2b0 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63   }..  /* No matc
cc2c0 68 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65  h was found.  We
cc2d0 20 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77   will make a new
cc2e0 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e   file ID */.  pN
cc2f0 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  ew->nRef = 1;.  
cc300 70 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b  pNew->nName = n;
cc310 0a 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d  .  pNew->pNext =
cc320 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74   vxworksFileList
cc330 3b 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c  ;.  vxworksFileL
cc340 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e  ist = pNew;.  un
cc350 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
cc360 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
cc370 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
cc380 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  t the reference 
cc390 63 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72  count on a vxwor
cc3a0 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e  ksFileId object.
cc3b0 20 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62    Free.** the ob
cc3c0 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 65  ject when the re
cc3d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65  ference count re
cc3e0 61 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  aches zero..*/.s
cc3f0 74 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72  tatic void vxwor
cc400 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28  ksReleaseFileId(
cc410 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cc420 6c 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e  leId *pId){.  un
cc430 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
cc440 20 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e    assert( pId->n
cc450 52 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e  Ref>0 );.  pId->
cc460 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49  nRef--;.  if( pI
cc470 64 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  d->nRef==0 ){.  
cc480 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
cc490 46 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20  FileId **pp;.   
cc4a0 20 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73   for(pp=&vxworks
cc4b0 46 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26  FileList; *pp &&
cc4c0 20 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20   *pp!=pId; pp = 
cc4d0 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29  &((*pp)->pNext))
cc4e0 7b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a  {}.    assert( *
cc4f0 70 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a  pp==pId );.    *
cc500 70 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b  pp = pId->pNext;
cc510 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
cc520 65 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e  e(pId);.  }.  un
cc530 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
cc540 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56  }.#endif /* OS_V
cc550 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a  XWORKS */./*****
cc560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
cc570 66 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44  f Unique File ID
cc580 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79   Utility Used By
cc590 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a   VxWorks *******
cc5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
cc5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc5f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a  ********/.../***
cc600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
cc650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc660 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64  ******* Posix Ad
cc670 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a  visory Locking *
cc680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
cc6a0 2a 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  * POSIX advisory
cc6b0 20 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65   locks are broke
cc6c0 6e 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e  n by design.  AN
cc6d0 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 31  SI STD 1003.1 (1
cc6e0 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20  996).** section 
cc6f0 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38  6.5.2.2 lines 48
cc700 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 70  3 through 490 sp
cc710 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e 20  ecify that when 
cc720 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74  a process.** set
cc730 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f  s or clears a lo
cc740 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69  ck, that operati
cc750 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79  on overrides any
cc760 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74   prior locks set
cc770 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20  .** by the same 
cc780 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65  process.  It doe
cc790 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79  s not explicitly
cc7a0 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69   say so, but thi
cc7b0 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61  s implies.** tha
cc7c0 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c  t it overrides l
cc7d0 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 20  ocks set by the 
cc7e0 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69  same process usi
cc7f0 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a  ng a different.*
cc800 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * file descripto
cc810 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69  r.  Consider thi
cc820 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a  s test case:.**.
cc830 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 31  **       int fd1
cc840 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31   = open("./file1
cc850 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41  ", O_RDWR|O_CREA
cc860 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20  T, 0644);.**    
cc870 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65     int fd2 = ope
cc880 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52  n("./file2", O_R
cc890 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34  DWR|O_CREAT, 064
cc8a0 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73  4);.**.** Suppos
cc8b0 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f  e ./file1 and ./
cc8c0 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79  file2 are really
cc8d0 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28   the same file (
cc8e0 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69  because.** one i
cc8f0 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62  s a hard or symb
cc900 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65  olic link to the
cc910 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20   other) then if 
cc920 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78  you set.** an ex
cc930 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20  clusive lock on 
cc940 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  fd1, then try to
cc950 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76   get an exclusiv
cc960 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32  e lock.** on fd2
cc970 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77  , it works.  I w
cc980 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74  ould have expect
cc990 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f  ed the second lo
cc9a0 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69  ck to.** fail si
cc9b0 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c  nce there was al
cc9c0 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20  ready a lock on 
cc9d0 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20  the file due to 
cc9e0 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20  fd1..** But not 
cc9f0 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20  so.  Since both 
cca00 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20  locks came from 
cca10 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
cca20 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20  , the.** second 
cca30 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69  overrides the fi
cca40 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  rst, even though
cca50 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69   they were on di
cca60 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20  fferent.** file 
cca70 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e  descriptors open
cca80 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20  ed on different 
cca90 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a  file names..**.*
ccaa0 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61  * This means tha
ccab0 74 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20  t we cannot use 
ccac0 50 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73  POSIX locks to s
ccad0 79 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20  ynchronize file 
ccae0 61 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20  access.** among 
ccaf0 63 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64  competing thread
ccb00 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72  s of the same pr
ccb10 6f 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f  ocess.  POSIX lo
ccb20 63 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69  cks will work fi
ccb30 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f  ne.** to synchro
ccb40 6e 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20  nize access for 
ccb50 74 68 72 65 61 64 73 20 69 6e 20 73 65 70 61 72  threads in separ
ccb60 61 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62  ate processes, b
ccb70 75 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64  ut not.** thread
ccb80 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d  s within the sam
ccb90 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  e process..**.**
ccba0 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
ccbb0 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c  the problem, SQL
ccbc0 69 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67  ite has to manag
ccbd0 65 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74  e file locks int
ccbe0 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74  ernally.** on it
ccbf0 73 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72  s own.  Whenever
ccc00 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
ccc10 69 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61  is opened, we ha
ccc20 76 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a  ve to find the.*
ccc30 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65  * specific inode
ccc40 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
ccc50 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65   file (the inode
ccc60 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
ccc70 79 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20  y the.** st_dev 
ccc80 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64  and st_ino field
ccc90 73 20 6f 66 20 74 68 65 20 73 74 61 74 20 73 74  s of the stat st
ccca0 72 75 63 74 75 72 65 20 74 68 61 74 20 66 73 74  ructure that fst
cccb0 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a  at() fills in).*
cccc0 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20  * and check for 
cccd0 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78  locks already ex
ccce0 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69  isting on that i
cccf0 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b  node.  When lock
ccd00 73 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64  s are.** created
ccd10 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20   or removed, we 
ccd20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20  have to look at 
ccd30 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c  our own internal
ccd40 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a   record of the.*
ccd50 2a 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69  * locks to see i
ccd60 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
ccd70 20 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   has previously 
ccd80 73 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  set a lock on th
ccd90 61 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65  at same.** inode
ccda0 2e 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20  ..**.** (Aside: 
ccdb0 54 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65  The use of inode
ccdc0 20 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71   numbers as uniq
ccdd0 75 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20  ue IDs does not 
ccde0 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e  work on VxWorks.
ccdf0 0a 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c  .** For VxWorks,
cce00 20 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 20   we have to use 
cce10 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20  the alternative 
cce20 75 6e 69 71 75 65 20 49 44 20 73 79 73 74 65 6d  unique ID system
cce30 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e   based on.** can
cce40 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20  onical filename 
cce50 61 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  and implemented 
cce60 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
cce70 64 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a  division.).**.**
cce80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   The sqlite3_fil
cce90 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
ccea0 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67  POSIX is no long
cceb0 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67  er just an integ
ccec0 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  er file.** descr
cced0 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f  iptor.  It is no
ccee0 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68  w a structure th
ccef0 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74  at holds the int
ccf00 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73  eger file.** des
ccf10 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f  criptor and a po
ccf20 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63  inter to a struc
ccf30 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
ccf40 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  bes the internal
ccf50 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  .** locks on the
ccf60 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69   corresponding i
ccf70 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20  node.  There is 
ccf80 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75  one locking stru
ccf90 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f  cture.** per ino
ccfa0 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61  de, so if the sa
ccfb0 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e  me inode is open
ccfc0 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75  ed twice, both u
ccfd0 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
ccfe0 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74  es.** point to t
ccff0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
cd000 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
cd010 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72  locking structur
cd020 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66  e keeps.** a ref
cd030 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f  erence count (so
cd040 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
cd050 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29  en to delete it)
cd060 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20   and a "cnt".** 
cd070 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73  field that tells
cd080 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c   us its internal
cd090 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63   lock status.  c
cd0a0 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a  nt==0 means the.
cd0b0 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63  ** file is unloc
cd0c0 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65  ked.  cnt==-1 me
cd0d0 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73  ans the file has
cd0e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
cd0f0 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61  ck..** cnt>0 mea
cd100 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74  ns there are cnt
cd110 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e   shared locks on
cd120 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
cd130 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
cd140 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61  lock or unlock a
cd150 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63   file first chec
cd160 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a  ks the locking.*
cd170 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  * structure.  Th
cd180 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d  e fcntl() system
cd190 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e   call is only in
cd1a0 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a  voked to set a .
cd1b0 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66  ** POSIX lock if
cd1c0 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f   the internal lo
cd1d0 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61  ck structure tra
cd1e0 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e  nsitions between
cd1f0 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64  .** a locked and
cd200 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61   an unlocked sta
cd210 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61  te..**.** But wa
cd220 69 74 3a 20 20 74 68 65 72 65 20 61 72 65 20 79  it:  there are y
cd230 65 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73  et more problems
cd240 20 77 69 74 68 20 50 4f 53 49 58 20 61 64 76 69   with POSIX advi
cd250 73 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  sory locks..**.*
cd260 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61  * If you close a
cd270 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
cd280 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20   that points to 
cd290 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20  a file that has 
cd2a0 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f  locks,.** all lo
cd2b0 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65  cks on that file
cd2c0 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20   that are owned 
cd2d0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  by the current p
cd2e0 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65  rocess are.** re
cd2f0 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b  leased.  To work
cd300 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f   around this pro
cd310 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46  blem, each unixF
cd320 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  ile structure co
cd330 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e  ntains.** a poin
cd340 74 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70  ter to an unixOp
cd350 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e  enCnt structure.
cd360 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75    There is one u
cd370 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
cd380 74 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e  ture.** per open
cd390 20 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65   inode, which me
cd3a0 61 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c  ans that multipl
cd3b0 65 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70  e unixFile can p
cd3c0 6f 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65  oint to a single
cd3d0 0a 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e  .** unixOpenCnt.
cd3e0 20 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70    When an attemp
cd3f0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f  t is made to clo
cd400 73 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20  se an unixFile, 
cd410 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20  if there are.** 
cd420 6f 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f  other unixFile o
cd430 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  pen on the same 
cd440 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20 68  inode that are h
cd450 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68  olding locks, th
cd460 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f  e call.** to clo
cd470 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64 65  se() the file de
cd480 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66 65  scriptor is defe
cd490 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f  rred until all o
cd4a0 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61  f the locks clea
cd4b0 72 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70  r..** The unixOp
cd4c0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20  enCnt structure 
cd4d0 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20  keeps a list of 
cd4e0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
cd4f0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a   that need to.**
cd500 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74   be closed and t
cd510 68 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b  hat list is walk
cd520 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29  ed (and cleared)
cd530 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c   when the last l
cd540 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a  ock.** clears..*
cd550 2a 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72  *.** Yet another
cd560 20 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78   problem:  Linux
cd570 54 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70  Threads do not p
cd580 6c 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f  lay well with po
cd590 73 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  six locks..**.**
cd5a0 20 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73   Many older vers
cd5b0 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73  ions of linux us
cd5c0 65 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61  e the LinuxThrea
cd5d0 64 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 68  ds library which
cd5e0 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78   is.** not posix
cd5f0 20 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64   compliant.  Und
cd600 65 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c  er LinuxThreads,
cd610 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20   a lock created 
cd620 62 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63  by thread.** A c
cd630 61 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65  annot be modifie
cd640 64 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20  d or overridden 
cd650 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74  by a different t
cd660 68 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79  hread B..** Only
cd670 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f   thread A can mo
cd680 64 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20  dify the lock.  
cd690 4c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72  Locking behavior
cd6a0 20 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69   is correct.** i
cd6b0 66 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e  f the appliation
cd6c0 20 75 73 65 73 20 74 68 65 20 6e 65 77 65 72 20   uses the newer 
cd6d0 4e 61 74 69 76 65 20 50 6f 73 69 78 20 54 68 72  Native Posix Thr
cd6e0 65 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54  ead Library (NPT
cd6f0 4c 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d  L).** on linux -
cd700 20 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63   with NPTL a loc
cd710 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72  k created by thr
cd720 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69  ead A can overri
cd730 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74  de locks.** in t
cd740 68 72 65 61 64 20 42 2e 20 20 42 75 74 20 74 68  hread B.  But th
cd750 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f  ere is no way to
cd760 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65   know at compile
cd770 2d 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74  -time which.** t
cd780 68 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79  hreading library
cd790 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20   is being used. 
cd7a0 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20   So there is no 
cd7b0 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a  way to know at.*
cd7c0 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77  * compile-time w
cd7d0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
cd7e0 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72  read A can overr
cd7f0 69 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72  ide locks on thr
cd800 65 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76  ead B..** We hav
cd810 65 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69  e to do a run-ti
cd820 6d 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63  me check to disc
cd830 6f 76 65 72 20 74 68 65 20 62 65 68 61 76 69 6f  over the behavio
cd840 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72  r of the.** curr
cd850 65 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a  ent process..**.
cd860 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68  ** On systems wh
cd870 65 72 65 20 74 68 72 65 61 64 20 41 20 69 73 20  ere thread A is 
cd880 75 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79  unable to modify
cd890 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62   locks created b
cd8a0 79 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77  y.** thread B, w
cd8b0 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74  e have to keep t
cd8c0 72 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68  rack of which th
cd8d0 72 65 61 64 20 63 72 65 61 74 65 64 20 65 61 63  read created eac
cd8e0 68 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63  h.** lock.  Henc
cd8f0 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 78  e there is an ex
cd900 74 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65  tra field in the
cd910 20 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78   key to the unix
cd920 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75  LockInfo.** stru
cd930 63 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20  cture to record 
cd940 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
cd950 2e 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20  .  And on those 
cd960 73 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73  systems it.** is
cd970 20 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69   illegal to begi
cd980 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  n a transaction 
cd990 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e  in one thread an
cd9a0 64 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69  d finish it.** i
cd9b0 6e 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20  n another.  For 
cd9c0 74 68 69 73 20 6c 61 74 74 65 72 20 72 65 73 74  this latter rest
cd9d0 72 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69  riction, there i
cd9e0 73 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64  s no work-around
cd9f0 2e 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d  ..** It is a lim
cda00 69 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78  itation of Linux
cda10 54 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  Threads..*/../*.
cda20 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20  ** Set or check 
cda30 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64  the unixFile.tid
cda40 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69   field.  This fi
cda50 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e 20  eld is set when 
cda60 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69  an unixFile.** i
cda70 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20  s first opened. 
cda80 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20   All subsequent 
cda90 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78  uses of the unix
cdaa0 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 74  File verify that
cdab0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72   the.** same thr
cdac0 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67  ead is operating
cdad0 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65   on the unixFile
cdae0 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e  .  Some operatin
cdaf0 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20  g systems do.** 
cdb00 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20  not allow locks 
cdb10 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  to be overridden
cdb20 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 64   by other thread
cdb30 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 72  s and that restr
cdb40 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20  iction.** means 
cdb50 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61  that sqlite3* da
cdb60 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63  tabase handles c
cdb70 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66  annot be moved f
cdb80 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a  rom one thread.*
cdb90 2a 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69  * to another whi
cdba0 6c 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  le locks are hel
cdbb0 64 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e  d..**.** Version
cdbc0 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d   3.3.1 (2006-01-
cdbd0 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63  15):  unixFile c
cdbe0 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d  an be moved from
cdbf0 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a   one thread to.*
cdc00 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e  * another as lon
cdc10 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e  g as we are runn
cdc20 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20  ing on a system 
cdc30 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 68  that supports th
cdc40 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64  reads.** overrid
cdc50 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 20  ing each others 
cdc60 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73 20  locks (which is 
cdc70 6e 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d  now the most com
cdc80 6d 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a  mon behavior).**
cdc90 20 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20   or if no locks 
cdca0 61 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74  are held.  But t
cdcb0 68 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63  he unixFile.pLoc
cdcc0 6b 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f  k field needs to
cdcd0 20 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65   be.** recompute
cdce0 64 20 62 65 63 61 75 73 65 20 69 74 73 20 6b 65  d because its ke
cdcf0 79 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74  y includes the t
cdd00 68 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74  hread-id.  See t
cdd10 68 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f  he .** transferO
cdd20 77 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63 74  wnership() funct
cdd30 69 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64  ion below for ad
cdd40 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
cdd50 74 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  tion.*/.#if SQLI
cdd60 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
cdd70 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
cdd80 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54  __).# define SET
cdd90 5f 54 48 52 45 41 44 49 44 28 58 29 20 20 20 28  _THREADID(X)   (
cdda0 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61  X)->tid = pthrea
cddb0 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e  d_self().# defin
cddc0 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44  e CHECK_THREADID
cddd0 28 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 72  (X) (threadsOver
cdde0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
cddf0 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20  cks==0 && \.    
cde00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cde10 20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 64          !pthread
cde20 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c  _equal((X)->tid,
cde30 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29   pthread_self())
cde40 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
cde50 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29   SET_THREADID(X)
cde60 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  .# define CHECK_
cde70 54 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 65  THREADID(X) 0.#e
cde80 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  ndif../*.** An i
cde90 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
cdea0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
cdeb0 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68 65  re serves as the
cdec0 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20   key used.** to 
cded0 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 75  locate a particu
cdee0 6c 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  lar unixOpenCnt 
cdef0 73 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20  structure given 
cdf00 69 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73  its inode.  This
cdf10 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20  .** is the same 
cdf20 61 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b  as the unixLockK
cdf30 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74  ey except that t
cdf40 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20  he thread ID is 
cdf50 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75  omitted..*/.stru
cdf60 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a  ct unixFileId {.
cdf70 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20    dev_t dev;    
cdf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
cdf90 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a   Device number *
cdfa0 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  /.#if OS_VXWORKS
cdfb0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  .  struct vxwork
cdfc0 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f  sFileId *pId;  /
cdfd0 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44  * Unique file ID
cdfe0 20 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f   for vxworks. */
cdff0 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69  .#else.  ino_t i
ce000 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
ce010 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75       /* Inode nu
ce020 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  mber */.#endif.}
ce030 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
ce040 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
ce050 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
ce060 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65  serves as the ke
ce070 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63  y used.** to loc
ce080 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72  ate a particular
ce090 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74   unixLockInfo st
ce0a0 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74  ructure given it
ce0b0 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  s inode..**.** I
ce0c0 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74  f threads cannot
ce0d0 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f   override each o
ce0e0 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e  thers locks (Lin
ce0f0 75 78 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e  uxThreads), then
ce100 20 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75   we.** set the u
ce110 6e 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66  nixLockKey.tid f
ce120 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65  ield to the thre
ce130 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 61  ad ID.  If threa
ce140 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a  ds can override.
ce150 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  ** each others l
ce160 6f 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20  ocks (Posix and 
ce170 4e 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69  NPTL) then tid i
ce180 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20  s always set to 
ce190 7a 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20  zero..** tid is 
ce1a0 6f 6d 69 74 74 65 64 20 69 66 20 77 65 20 63 6f  omitted if we co
ce1b0 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68  mpile without th
ce1c0 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20  reading support 
ce1d0 6f 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f  or on an OS.** o
ce1e0 74 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e  ther than linux.
ce1f0 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c  .*/.struct unixL
ce200 6f 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63  ockKey {.  struc
ce210 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64  t unixFileId fid
ce220 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65  ;  /* Unique ide
ce230 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 20  ntifier for the 
ce240 66 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49  file */.#if SQLI
ce250 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
ce260 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
ce270 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20  __).  pthread_t 
ce280 74 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20  tid;  /* Thread 
ce290 49 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72  ID of lock owner
ce2a0 2e 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73  . Zero if not us
ce2b0 69 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73  ing LinuxThreads
ce2c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
ce2d0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
ce2e0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
ce2f0 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
ce300 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
ce310 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e  h open.** inode.
ce320 20 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68    Or, on LinuxTh
ce330 72 65 61 64 73 2c 20 74 68 65 72 65 20 69 73 20  reads, there is 
ce340 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74 72  one of these str
ce350 75 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65  uctures for.** e
ce360 61 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64  ach inode opened
ce370 20 62 79 20 65 61 63 68 20 74 68 72 65 61 64 2e   by each thread.
ce380 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20  .**.** A single 
ce390 69 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d  inode can have m
ce3a0 75 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73  ultiple file des
ce3b0 63 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63  criptors, so eac
ce3c0 68 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74  h unixFile.** st
ce3d0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
ce3e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
ce3f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
ce400 73 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69  s object and thi
ce410 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70  s.** object keep
ce420 73 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  s a count of the
ce430 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46   number of unixF
ce440 69 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  ile pointing to 
ce450 69 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e  it..*/.struct un
ce460 69 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73  ixLockInfo {.  s
ce470 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65  truct unixLockKe
ce480 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f  y lockKey;     /
ce490 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79  * The lookup key
ce4a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
ce4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce4c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ce4d0 6f 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20  of SHARED locks 
ce4e0 68 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f  held */.  int lo
ce4f0 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20  cktype;         
ce500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
ce510 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c   of SHARED_LOCK,
ce520 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65   RESERVED_LOCK e
ce530 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  tc. */.  int nRe
ce540 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
ce550 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ce560 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  er of pointers t
ce570 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  o this structure
ce580 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
ce590 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74  xLockInfo *pNext
ce5a0 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ;     /* List of
ce5b0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   all unixLockInf
ce5c0 6f 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73  o objects */.  s
ce5d0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
ce5e0 66 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f  fo *pPrev;     /
ce5f0 2a 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79  *    .... doubly
ce600 20 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f   linked */.};../
ce610 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
ce620 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
ce630 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
ce640 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
ce650 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e  h open.** inode.
ce660 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65    This structure
ce670 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
ce680 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f  the number of lo
ce690 63 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69  cks on that.** i
ce6a0 6e 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73  node.  If a clos
ce6b0 65 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61  e is attempted a
ce6c0 67 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20  gainst an inode 
ce6d0 74 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a  that is holding.
ce6e0 2a 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c  ** locks, the cl
ce6f0 6f 73 65 20 69 73 20 64 65 66 65 72 72 65 64 20  ose is deferred 
ce700 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20  until all locks 
ce710 63 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67 20  clear by adding 
ce720 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63  the.** file desc
ce730 72 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f  riptor to be clo
ce740 73 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69  sed to the pendi
ce750 6e 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  ng list..**.** T
ce760 4f 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63  ODO:  Consider c
ce770 68 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20  hanging this so 
ce780 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e  that there is on
ce790 6c 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65  ly a single file
ce7a0 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66  .** descriptor f
ce7b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c  or each open fil
ce7c0 65 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20  e, even when it 
ce7d0 69 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70  is opened multip
ce7e0 6c 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65  le times..** The
ce7f0 20 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20   close() system 
ce800 63 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20  call would only 
ce810 6f 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c  occur when the l
ce820 61 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ast database.** 
ce830 75 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63  using the file c
ce840 6c 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  loses..*/.struct
ce850 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20   unixOpenCnt {. 
ce860 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65   struct unixFile
ce870 49 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20  Id fileId;   /* 
ce880 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a  The lookup key *
ce890 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
ce8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce8b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
ce8c0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
ce8d0 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74  ructure */.  int
ce8e0 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20   nLock;         
ce8f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ce900 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e  er of outstandin
ce910 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69  g locks */.  Uni
ce920 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73  xUnusedFd *pUnus
ce930 65 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73  ed;      /* Unus
ce940 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
ce950 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a  ors to close */.
ce960 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
ce970 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20   sem_t *pSem;   
ce980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce990 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58    /* Named POSIX
ce9a0 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20   semaphore */.  
ce9b0 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41  char aSemName[MA
ce9c0 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20  X_PATHNAME+2];  
ce9d0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74   /* Name of that
ce9e0 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65   semaphore */.#e
ce9f0 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e  ndif.  struct un
cea00 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74  ixOpenCnt *pNext
cea10 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c  , *pPrev;   /* L
cea20 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f  ist of all unixO
cea30 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a  penCnt objects *
cea40 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74  /.};../*.** List
cea50 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63  s of all unixLoc
cea60 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70  kInfo and unixOp
cea70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20  enCnt objects.  
cea80 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65  These used to be
cea90 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e   hash.** tables.
ceaa0 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72    But the number
ceab0 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72   of objects is r
ceac0 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20  arely more than 
cead0 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e  a dozen and.** n
ceae0 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66  ever exceeds a f
ceaf0 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e  ew thousand.  An
ceb00 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20  d lookup is not 
ceb10 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a  on a critical.**
ceb20 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c   path so a simpl
ceb30 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69  e linked list wi
ceb40 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73  ll suffice..*/.s
ceb50 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69  tatic struct uni
ceb60 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c  xLockInfo *lockL
ceb70 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20  ist = 0;.static 
ceb80 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
ceb90 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30  nt *openList = 0
ceba0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61  ;../*.** This va
cebb0 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73  riable remembers
cebc0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
cebd0 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72  threads can over
cebe0 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
cebf0 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  .** locks..**.**
cec00 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72      0:  No.  Thr
cec10 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72  eads cannot over
cec20 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73  ride each others
cec30 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54   locks.  (LinuxT
cec40 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a  hreads).**    1:
cec50 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20    Yes.  Threads 
cec60 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63  can override eac
cec70 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20  h others locks. 
cec80 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a   (Posix & NLPT).
cec90 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e  **   -1:  We don
ceca0 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a  't know yet..**.
cecb0 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65  ** On some syste
cecc0 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63  ms, we know at c
cecd0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74  ompile-time if t
cece0 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72  hreads can overr
cecf0 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65  ide each.** othe
ced00 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68  rs locks.  On th
ced10 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65  ose systems, the
ced20 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f   SQLITE_THREAD_O
ced30 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63  VERRIDE_LOCK mac
ced40 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65  ro.** will be se
ced50 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  t appropriately.
ced60 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65    On other syste
ced70 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63  ms, we have to c
ced80 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69  heck at.** runti
ced90 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61  me.  On these la
ceda0 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51  tter systems, SQ
cedb0 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LTIE_THREAD_OVER
cedc0 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20  RIDE_LOCK is.** 
cedd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
cede0 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e   This variable n
cedf0 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65  ormally has file
cee00 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75   scope only.  Bu
cee10 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  t during testing
cee20 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  , we make.** it 
cee30 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74  a global so that
cee40 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63   the test code c
cee50 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61  an change its va
cee60 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  lue in order to 
cee70 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74  verify.** that t
cee80 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68  he right stuff h
cee90 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72  appens in either
ceea0 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51   case..*/.#if SQ
ceeb0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
ceec0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
ceed0 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20  ux__).#  ifndef 
ceee0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
ceef0 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20  ERRIDE_LOCK.#   
cef00 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54   define SQLITE_T
cef10 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
cef20 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a  OCK -1.#  endif.
cef30 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f  #  ifdef SQLITE_
cef40 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73  TEST.int threads
cef50 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cef60 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45  rsLocks = SQLITE
cef70 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
cef80 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73  _LOCK;.#  else.s
cef90 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64  tatic int thread
cefa0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
cefb0 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54  ersLocks = SQLIT
cefc0 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
cefd0 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66  E_LOCK;.#  endif
cefe0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
ceff0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f  his structure ho
cf000 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  lds information 
cf010 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69  passed into indi
cf020 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74  vidual test.** t
cf030 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65  hreads by the te
cf040 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
cf050 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e  ehavior() routin
cf060 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72  e..*/.struct thr
cf070 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20  eadTestData {.  
cf080 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20  int fd;         
cf090 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
cf0a0 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
cf0b0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
cf0c0 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c  ck;     /* The l
cf0d0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
cf0e0 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74   */.  int result
cf0f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
cf100 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f  Result of the lo
cf110 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  cking operation 
cf120 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54  */.};..#if SQLIT
cf130 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cf140 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cf150 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  _)./*.** This fu
cf160 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61  nction is used a
cf170 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69  s the main routi
cf180 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20  ne for a thread 
cf190 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74  launched by.** t
cf1a0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
cf1b0 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74  Behavior(). It t
cf1c0 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65  ests whether the
cf1d0 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74   shared-lock obt
cf1e0 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  ained.** by the 
cf1f0 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74  main thread in t
cf200 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
cf210 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c  Behavior() confl
cf220 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68  icts with a.** h
cf230 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74  ypothetical writ
cf240 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20  e-lock obtained 
cf250 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f  by this thread o
cf260 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e  n the same file.
cf270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65  .**.** The write
cf280 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74  -lock is not act
cf290 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20  ually acquired, 
cf2a0 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70  as this is not p
cf2b0 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74  ossible if .** t
cf2c0 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20  he file is open 
cf2d0 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64  in read-only mod
cf2e0 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33  e (see ticket #3
cf2f0 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  472)..*/ .static
cf300 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63   void *threadLoc
cf310 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70  kingTest(void *p
cf320 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74  Arg){.  struct t
cf330 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70  hreadTestData *p
cf340 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74  Data = (struct t
cf350 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70  hreadTestData*)p
cf360 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65  Arg;.  pData->re
cf370 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61  sult = fcntl(pDa
cf380 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c  ta->fd, F_GETLK,
cf390 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a   &pData->lock);.
cf3a0 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d    return pArg;.}
cf3b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
cf3c0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cf3d0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cf3e0 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49  _) */...#if SQLI
cf3f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
cf400 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
cf410 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70  __)./*.** This p
cf420 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74  rocedure attempt
cf430 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  s to determine w
cf440 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
cf450 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65  reads.** can ove
cf460 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
cf470 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74  s locks then set
cf480 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64  s the .** thread
cf490 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
cf4a0 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c  ersLocks variabl
cf4b0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  e appropriately.
cf4c0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
cf4d0 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
cf4e0 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64  gBehavior(int fd
cf4f0 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64  _orig){.  int fd
cf500 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74  ;.  int rc;.  st
cf510 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44  ruct threadTestD
cf520 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20  ata d;.  struct 
cf530 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65  flock l;.  pthre
cf540 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20  ad_t t;..  fd = 
cf550 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20  dup(fd_orig);.  
cf560 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72  if( fd<0 ) retur
cf570 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20  n;.  memset(&l, 
cf580 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20  0, sizeof(l));. 
cf590 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44   l.l_type = F_RD
cf5a0 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d  LCK;.  l.l_len =
cf5b0 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20   1;.  l.l_start 
cf5c0 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63  = 0;.  l.l_whenc
cf5d0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
cf5e0 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72  rc = fcntl(fd_or
cf5f0 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29  ig, F_SETLK, &l)
cf600 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20  ;.  if( rc!=0 ) 
cf610 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74  return;.  memset
cf620 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64  (&d, 0, sizeof(d
cf630 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b  ));.  d.fd = fd;
cf640 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20  .  d.lock = l;. 
cf650 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d   d.lock.l_type =
cf660 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20   F_WRLCK;.  if( 
cf670 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26  pthread_create(&
cf680 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b  t, 0, threadLock
cf690 69 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20  ingTest, &d)==0 
cf6a0 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a  ){.    pthread_j
cf6b0 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20  oin(t, 0);.  }. 
cf6c0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66   close(fd);.  if
cf6d0 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20  ( d.result!=0 ) 
cf6e0 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64  return;.  thread
cf6f0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
cf700 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f  ersLocks = (d.lo
cf710 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c  ck.l_type==F_UNL
cf720 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  CK);.}.#endif /*
cf730 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cf740 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cf750 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a  linux__) */../*.
cf760 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69  ** Release a uni
cf770 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74  xLockInfo struct
cf780 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
cf790 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64  llocated by find
cf7a0 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a  LockInfo()..**.*
cf7b0 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65  * The mutex ente
cf7c0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e  red using the un
cf7d0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66  ixEnterMutex() f
cf7e0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
cf7f0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69  held.** when thi
cf800 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
cf810 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lled..*/.static 
cf820 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b  void releaseLock
cf830 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78  Info(struct unix
cf840 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29  LockInfo *pLock)
cf850 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78  {.  assert( unix
cf860 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20  MutexHeld() );. 
cf870 20 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20   if( pLock ){.  
cf880 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b    pLock->nRef--;
cf890 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e  .    if( pLock->
cf8a0 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nRef==0 ){.     
cf8b0 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65   if( pLock->pPre
cf8c0 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  v ){.        ass
cf8d0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65  ert( pLock->pPre
cf8e0 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20  v->pNext==pLock 
cf8f0 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b  );.        pLock
cf900 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
cf910 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
cf920 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
cf930 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b      assert( lock
cf940 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20  List==pLock );. 
cf950 20 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20         lockList 
cf960 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a  = pLock->pNext;.
cf970 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
cf980 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29  ( pLock->pNext )
cf990 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
cf9a0 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e  ( pLock->pNext->
cf9b0 70 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a  pPrev==pLock );.
cf9c0 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70          pLock->p
cf9d0 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c  Next->pPrev = pL
cf9e0 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20  ock->pPrev;.    
cf9f0 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
cfa00 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20  3_free(pLock);. 
cfa10 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
cfa20 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78  * Release a unix
cfa30 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  OpenCnt structur
cfa40 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
cfa50 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f  ocated by findLo
cfa60 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20  ckInfo()..**.** 
cfa70 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65  The mutex entere
cfa80 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78  d using the unix
cfa90 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e  EnterMutex() fun
cfaa0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65  ction must be he
cfab0 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  ld.** when this 
cfac0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
cfad0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
cfae0 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e  id releaseOpenCn
cfaf0 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65  t(struct unixOpe
cfb00 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20  nCnt *pOpen){.  
cfb10 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65  assert( unixMute
cfb20 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28  xHeld() );.  if(
cfb30 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f   pOpen ){.    pO
cfb40 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20  pen->nRef--;.   
cfb50 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66   if( pOpen->nRef
cfb60 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
cfb70 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b   pOpen->pPrev ){
cfb80 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
cfb90 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70   pOpen->pPrev->p
cfba0 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20  Next==pOpen );. 
cfbb0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50         pOpen->pP
cfbc0 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70  rev->pNext = pOp
cfbd0 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  en->pNext;.     
cfbe0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
cfbf0 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74  assert( openList
cfc00 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20  ==pOpen );.     
cfc10 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
cfc20 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pen->pNext;.    
cfc30 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f    }.      if( pO
cfc40 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  pen->pNext ){.  
cfc50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
cfc60 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  pen->pNext->pPre
cfc70 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20  v==pOpen );.    
cfc80 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74      pOpen->pNext
cfc90 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d  ->pPrev = pOpen-
cfca0 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a  >pPrev;.      }.
cfcb0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
cfcc0 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
cfcd0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20  (__linux__).    
cfce0 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e    assert( !pOpen
cfcf0 2d 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72  ->pUnused || thr
cfd00 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
cfd10 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29  OthersLocks==0 )
cfd20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  ;.#endif..      
cfd30 2f 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e  /* If pOpen->pUn
cfd40 75 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c  used is not null
cfd50 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e  , then memory an
cfd60 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  d file-descripto
cfd70 72 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20  rs.      ** are 
cfd80 6c 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a  leaked..      **
cfd90 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77  .      ** This w
cfda0 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  ill only happen 
cfdb0 69 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74  if, under Linuxt
cfdc0 68 72 65 61 64 73 2c 20 74 68 65 20 75 73 65 72  hreads, the user
cfdd0 20 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20   has opened.    
cfde0 20 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69    ** a transacti
cfdf0 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64  on in one thread
cfe00 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20  , then attempts 
cfe10 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  to close the dat
cfe20 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68  abase.      ** h
cfe30 61 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68  andle from anoth
cfe40 65 72 20 74 68 72 65 61 64 20 28 77 69 74 68 6f  er thread (witho
cfe50 75 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69  ut first unlocki
cfe60 6e 67 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e  ng the db file).
cfe70 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69  .      ** This i
cfe80 73 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a  s a misuse.  */.
cfe90 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
cfea0 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d  ee(pOpen);.    }
cfeb0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
cfec0 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72  ven a file descr
cfed0 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e  iptor, locate un
cfee0 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75  ixLockInfo and u
cfef0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
cff00 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65  tures that.** de
cff10 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69 6c  scribes that fil
cff20 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43  e descriptor.  C
cff30 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69  reate new ones i
cff40 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68  f necessary.  Th
cff50 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  e.** return valu
cff60 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e  es might be unin
cff70 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20  itialized if an 
cff80 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a  error occurs..**
cff90 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e  .** The mutex en
cffa0 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20  tered using the 
cffb0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
cffc0 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62   function must b
cffd0 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74  e held.** when t
cffe0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
cfff0 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  called..**.** Re
d0000 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69  turn an appropri
d0010 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a  ate error code..
d0020 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
d0030 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e  ndLockInfo(.  un
d0040 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20  ixFile *pFile,  
d0050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d0060 55 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20 66  Unix file with f
d0070 69 6c 65 20 64 65 73 63 20 75 73 65 64 20 69 6e  ile desc used in
d0080 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74   the key */.  st
d0090 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
d00a0 6f 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20  o **ppLock,  /* 
d00b0 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c  Return the unixL
d00c0 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ockInfo structur
d00d0 65 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75  e here */.  stru
d00e0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d00f0 2a 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65  *ppOpen    /* Re
d0100 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65  turn the unixOpe
d0110 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68  nCnt structure h
d0120 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
d0130 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
d0140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79             /* Sy
d0150 73 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e  stem call return
d0160 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66   code */.  int f
d0170 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
d0180 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
d0190 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d01a0 20 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20   for pFile */.  
d01b0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b  struct unixLockK
d01c0 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f  ey lockKey;    /
d01d0 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72  * Lookup key for
d01e0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   the unixLockInf
d01f0 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  o structure */. 
d0200 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65   struct unixFile
d0210 49 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20  Id fileId;      
d0220 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f  /* Lookup key fo
d0230 72 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e  r the unixOpenCn
d0240 74 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74  t struct */.  st
d0250 72 75 63 74 20 73 74 61 74 20 73 74 61 74 62 75  ruct stat statbu
d0260 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
d0270 4c 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69  Low-level file i
d0280 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
d0290 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49  struct unixLockI
d02a0 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f  nfo *pLock = 0;/
d02b0 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78  * Candidate unix
d02c0 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20  LockInfo object 
d02d0 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  */.  struct unix
d02e0 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20  OpenCnt *pOpen; 
d02f0 20 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65      /* Candidate
d0300 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a   unixOpenCnt obj
d0310 65 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ect */..  assert
d0320 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  ( unixMutexHeld(
d0330 29 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c  ) );..  /* Get l
d0340 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61  ow-level informa
d0350 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
d0360 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20  ile that we can 
d0370 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65  used to.  ** cre
d0380 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d  ate a unique nam
d0390 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a  e for the file..
d03a0 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c    */.  fd = pFil
d03b0 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74  e->h;.  rc = fst
d03c0 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29  at(fd, &statbuf)
d03d0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b  ;.  if( rc!=0 ){
d03e0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
d03f0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23  Errno = errno;.#
d0400 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a  ifdef EOVERFLOW.
d0410 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c      if( pFile->l
d0420 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46  astErrno==EOVERF
d0430 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  LOW ) return SQL
d0440 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69  ITE_NOLFS;.#endi
d0450 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  f.    return SQL
d0460 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a  ITE_IOERR;.  }..
d0470 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f  #ifdef __APPLE__
d0480 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e  .  /* On OS X on
d0490 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79   an msdos filesy
d04a0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20  stem, the inode 
d04b0 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74  number is report
d04c0 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63  ed.  ** incorrec
d04d0 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a  tly for zero-siz
d04e0 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69  e files.  See ti
d04f0 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20  cket #3260.  To 
d0500 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  work.  ** around
d0510 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77   this problem (w
d0520 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20  e consider it a 
d0530 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74  bug in OS X, not
d0540 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65   SQLite).  ** we
d0550 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65   always increase
d0560 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74   the file size t
d0570 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61  o 1 by writing a
d0580 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a   single byte.  *
d0590 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73  * prior to acces
d05a0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e  sing the inode n
d05b0 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20  umber.  The one 
d05c0 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a  byte written is.
d05d0 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53    ** an ASCII 'S
d05e0 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63  ' character whic
d05f0 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74  h also happens t
d0600 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62  o be the first b
d0610 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  yte.  ** in the 
d0620 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20  header of every 
d0630 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e  SQLite database.
d0640 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69    In this way, i
d0650 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20  f there.  ** is 
d0660 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
d0670 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68   such that anoth
d0680 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c  er thread has al
d0690 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a  ready populated.
d06a0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70    ** the first p
d06b0 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
d06c0 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69  ase, no damage i
d06d0 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69  s done..  */.  i
d06e0 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69  f( statbuf.st_si
d06f0 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ze==0 ){.    rc 
d0700 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c  = write(fd, "S",
d0710 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   1);.    if( rc!
d0720 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =1 ){.      retu
d0730 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
d0740 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
d0750 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62  fstat(fd, &statb
d0760 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  uf);.    if( rc!
d0770 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  =0 ){.      pFil
d0780 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
d0790 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75  rrno;.      retu
d07a0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
d07b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
d07c0 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63  f..  memset(&loc
d07d0 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28  kKey, 0, sizeof(
d07e0 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63  lockKey));.  loc
d07f0 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73  kKey.fid.dev = s
d0800 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23  tatbuf.st_dev;.#
d0810 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
d0820 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20  lockKey.fid.pId 
d0830 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65  = pFile->pId;.#e
d0840 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69  lse.  lockKey.fi
d0850 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e  d.ino = statbuf.
d0860 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23  st_ino;.#endif.#
d0870 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
d0880 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
d0890 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28  __linux__).  if(
d08a0 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65   threadsOverride
d08b0 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c  EachOthersLocks<
d08c0 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72  0 ){.    testThr
d08d0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
d08e0 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f  or(fd);.  }.  lo
d08f0 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65  ckKey.tid = thre
d0900 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
d0910 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a  thersLocks ? 0 :
d0920 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
d0930 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64  .#endif.  fileId
d0940 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a   = lockKey.fid;.
d0950 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20    if( ppLock!=0 
d0960 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c  ){.    pLock = l
d0970 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69  ockList;.    whi
d0980 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d  le( pLock && mem
d0990 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70  cmp(&lockKey, &p
d09a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73  Lock->lockKey, s
d09b0 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20  izeof(lockKey)) 
d09c0 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d  ){.      pLock =
d09d0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
d09e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f     }.    if( pLo
d09f0 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ck==0 ){.      p
d0a00 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Lock = sqlite3_m
d0a10 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70  alloc( sizeof(*p
d0a20 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69  Lock) );.      i
d0a30 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  f( pLock==0 ){. 
d0a40 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
d0a50 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
d0a60 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64    goto exit_find
d0a70 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20  lockinfo;.      
d0a80 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  }.      memcpy(&
d0a90 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26  pLock->lockKey,&
d0aa0 6c 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28 6c  lockKey,sizeof(l
d0ab0 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20 20  ockKey));.      
d0ac0 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b  pLock->nRef = 1;
d0ad0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e  .      pLock->cn
d0ae0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f  t = 0;.      pLo
d0af0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 30  ck->locktype = 0
d0b00 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70  ;.      pLock->p
d0b10 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b  Next = lockList;
d0b20 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50  .      pLock->pP
d0b30 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  rev = 0;.      i
d0b40 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f  f( lockList ) lo
d0b50 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20  ckList->pPrev = 
d0b60 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f 63  pLock;.      loc
d0b70 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20  kList = pLock;. 
d0b80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d0b90 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  pLock->nRef++;. 
d0ba0 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63 6b     }.    *ppLock
d0bb0 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20   = pLock;.  }.  
d0bc0 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b  if( ppOpen!=0 ){
d0bd0 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65  .    pOpen = ope
d0be0 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65  nList;.    while
d0bf0 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d  ( pOpen && memcm
d0c00 70 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70 65  p(&fileId, &pOpe
d0c10 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f  n->fileId, sizeo
d0c20 66 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20 20  f(fileId)) ){.  
d0c30 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70 65      pOpen = pOpe
d0c40 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  n->pNext;.    }.
d0c50 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30      if( pOpen==0
d0c60 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20   ){.      pOpen 
d0c70 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
d0c80 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29  ( sizeof(*pOpen)
d0c90 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   );.      if( pO
d0ca0 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pen==0 ){.      
d0cb0 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66    releaseLockInf
d0cc0 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  o(pLock);.      
d0cd0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
d0ce0 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  MEM;.        got
d0cf0 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  o exit_findlocki
d0d00 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nfo;.      }.   
d0d10 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e 2c     memset(pOpen,
d0d20 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65   0, sizeof(*pOpe
d0d30 6e 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e  n));.      pOpen
d0d40 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49  ->fileId = fileI
d0d50 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  d;.      pOpen->
d0d60 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  nRef = 1;.      
d0d70 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f  pOpen->pNext = o
d0d80 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 69  penList;.      i
d0d90 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f 70  f( openList ) op
d0da0 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20  enList->pPrev = 
d0db0 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 65  pOpen;.      ope
d0dc0 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a 20  nList = pOpen;. 
d0dd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d0de0 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  pOpen->nRef++;. 
d0df0 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 6e     }.    *ppOpen
d0e00 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65   = pOpen;.  }..e
d0e10 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f  xit_findlockinfo
d0e20 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  :.  return rc;.}
d0e30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
d0e40 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61  e currently in a
d0e50 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
d0e60 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61  d than the threa
d0e70 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e  d that the.** un
d0e80 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20  ixFile argument 
d0e90 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e  belongs to, then
d0ea0 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73   transfer owners
d0eb0 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46  hip of the unixF
d0ec0 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74  ile.** over to t
d0ed0 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61  he current threa
d0ee0 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46  d..**.** A unixF
d0ef0 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65  ile is only owne
d0f00 64 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e  d by a thread on
d0f10 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75 73   systems that us
d0f20 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a  e LinuxThreads..
d0f30 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 20  **.** Ownership 
d0f40 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c 79  transfer is only
d0f50 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20   allowed if the 
d0f60 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72 72  unixFile is curr
d0f70 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a  ently unlocked..
d0f80 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46 69  ** If the unixFi
d0f90 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e 64  le is locked and
d0fa0 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 73   an ownership is
d0fb0 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 74   wrong, then ret
d0fc0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49  urn.** SQLITE_MI
d0fd0 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f 4b  SUSE.  SQLITE_OK
d0fe0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
d0ff0 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73  everything works
d1000 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
d1010 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
d1020 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
d1030 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 61 6e  .static int tran
d1040 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75 6e  sferOwnership(un
d1050 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  ixFile *pFile){.
d1060 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68 72    int rc;.  pthr
d1070 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69  ead_t hSelf;.  i
d1080 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69  f( threadsOverri
d1090 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b  deEachOthersLock
d10a0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65  s ){.    /* Owne
d10b0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 73 20  rship transfers 
d10c0 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74 68  not needed on th
d10d0 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20  is system */.   
d10e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d10f0 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d  K;.  }.  hSelf =
d1100 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b   pthread_self();
d1110 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 65  .  if( pthread_e
d1120 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c  qual(pFile->tid,
d1130 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f   hSelf) ){.    /
d1140 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20 69  * We are still i
d1150 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  n the same threa
d1160 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45  d */.    OSTRACE
d1170 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20  1("No-transfer, 
d1180 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29 3b  same thread\n");
d1190 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d11a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
d11b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d11c0 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  !=NO_LOCK ){.   
d11d0 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63 68   /* We cannot ch
d11e0 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20 77  ange ownership w
d11f0 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c 64  hile we are hold
d1200 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20  ing a lock! */. 
d1210 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d1220 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f  _MISUSE;.  }.  O
d1230 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 66 65  STRACE4("Transfe
d1240 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 25  r ownership of %
d1250 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c  d from %d to %d\
d1260 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
d1270 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
d1280 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20  >tid, hSelf);.  
d1290 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65  pFile->tid = hSe
d12a0 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d  lf;.  if (pFile-
d12b0 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20  >pLock != NULL) 
d12c0 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63  {.    releaseLoc
d12d0 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f  kInfo(pFile->pLo
d12e0 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69  ck);.    rc = fi
d12f0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65  ndLockInfo(pFile
d1300 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c  , &pFile->pLock,
d1310 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45   0);.    OSTRACE
d1320 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73  5("LOCK    %d is
d1330 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e   now %s(%s,%d)\n
d1340 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  ", pFile->h,.   
d1350 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d1360 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
d1370 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20  type),.         
d1380 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70    locktypeName(p
d1390 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
d13a0 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70  ktype), pFile->p
d13b0 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20  Lock->cnt);.    
d13c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65  return rc;.  } e
d13d0 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e  lse {.    return
d13e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
d13f0 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 6e  }.#else  /* if n
d1400 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ot SQLITE_THREAD
d1410 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20  SAFE */.  /* On 
d1420 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
d1430 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69  builds, ownershi
d1440 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20  p transfer is a 
d1450 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e  no-op */.# defin
d1460 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  e transferOwners
d1470 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b  hip(X) SQLITE_OK
d1480 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
d1490 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a  E_THREADSAFE */.
d14a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
d14b0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
d14c0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
d14d0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
d14e0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
d14f0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
d1500 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
d1510 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
d1520 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20  ck is held, set 
d1530 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61  *pResOut.** to a
d1540 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
d1550 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f  otherwise *pResO
d1560 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  ut is set to zer
d1570 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  o.  The return v
d1580 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74  alue.** is set t
d1590 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65  o SQLITE_OK unle
d15a0 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  ss an I/O error 
d15b0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f  occurs during lo
d15c0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a  ck checking..*/.
d15d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
d15e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
d15f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d1600 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
d1610 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
d1620 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65  ITE_OK;.  int re
d1630 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e  served = 0;.  un
d1640 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d1650 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a  (unixFile*)id;..
d1660 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
d1670 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
d1680 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
d1690 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20  RVEDLOCK; );..  
d16a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d16b0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
d16c0 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20  x(); /* Because 
d16d0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20  pFile->pLock is 
d16e0 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68  shared across th
d16f0 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43  reads */..  /* C
d1700 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64  heck if a thread
d1710 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73   in this process
d1720 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f   holds such a lo
d1730 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  ck */.  if( pFil
d1740 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79  e->pLock->lockty
d1750 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
d1760 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  {.    reserved =
d1770 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74   1;.  }..  /* Ot
d1780 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73  herwise see if s
d1790 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
d17a0 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f  s holds it..  */
d17b0 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 50  .#ifndef __DJGPP
d17c0 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  __.  if( !reserv
d17d0 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  ed ){.    struct
d17e0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20   flock lock;.   
d17f0 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
d1800 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c   SEEK_SET;.    l
d1810 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45  ock.l_start = RE
d1820 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20  SERVED_BYTE;.   
d1830 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b   lock.l_len = 1;
d1840 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
d1850 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20   = F_WRLCK;.    
d1860 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28  if (-1 == fcntl(
d1870 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c  pFile->h, F_GETL
d1880 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20  K, &lock)) {.   
d1890 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d18a0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
d18b0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d18c0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d18d0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d18e0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
d18f0 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  CK);.      pFile
d1900 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d1910 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65  rrno;.    } else
d1920 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65   if( lock.l_type
d1930 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20  !=F_UNLCK ){.   
d1940 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
d1950 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
d1960 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65  f.  .  unixLeave
d1970 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41  Mutex();.  OSTRA
d1980 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE4("TEST WR-LOC
d1990 4b 20 25 64 20 25 64 20 25 64 20 28 75 6e 69 78  K %d %d %d (unix
d19a0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
d19b0 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a  rc, reserved);..
d19c0 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73    *pResOut = res
d19d0 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20  erved;.  return 
d19e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  rc;.}../*.** Per
d19f0 66 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b  form a file lock
d1a00 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e  ing operation on
d1a10 20 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65   a range of byte
d1a20 73 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20  s in a file..** 
d1a30 54 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74  The "op" paramet
d1a40 65 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65  er should be one
d1a50 20 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57   of F_RDLCK, F_W
d1a60 52 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b  RLCK, or F_UNLCK
d1a70 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e  ..** Return 0 on
d1a80 20 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66   success or -1 f
d1a90 6f 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20  or failure.  On 
d1aa0 66 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74  failure, write t
d1ab0 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  he error.** code
d1ac0 20 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e   into *pErrcode.
d1ad0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  .**.** If the SQ
d1ae0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
d1af0 4c 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63  LOCKING bit is c
d1b00 6c 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  lear, then only 
d1b10 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67  lock.** the rang
d1b20 65 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68  e of bytes on th
d1b30 65 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62  e locking page b
d1b40 65 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49  etween SHARED_FI
d1b50 52 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45  RST and.** SHARE
d1b60 44 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49  D_SIZE.  If SQLI
d1b70 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f  TE_WHOLE_FILE_LO
d1b80 43 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68  CKING is set, th
d1b90 65 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62  en lock all.** b
d1ba0 79 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74  ytes from 0 up t
d1bb0 6f 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64  o but not includ
d1bc0 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ing PENDING_BYTE
d1bd0 2c 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a  , and all bytes.
d1be0 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53  ** that follow S
d1bf0 48 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a  HARED_FIRST..**.
d1c00 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
d1c10 73 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f  s, of SQLITE_WHO
d1c20 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
d1c30 69 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69  if false (the hi
d1c40 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61  storical.** defa
d1c50 75 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f  ult case) then o
d1c60 6e 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c  nly lock a small
d1c70 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20   range of bytes 
d1c80 66 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53  from SHARED_FIRS
d1c90 54 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41  T.** through SHA
d1ca0 52 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44  RED_FIRST+SHARED
d1cb0 5f 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66  _SIZE-1.  But if
d1cc0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d1cd0 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a  LE_LOCKING is.**
d1ce0 20 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20   true then lock 
d1cf0 65 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68  every byte in th
d1d00 65 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f  e file except fo
d1d10 72 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61  r PENDING_BYTE a
d1d20 6e 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42  nd.** RESERVED_B
d1d30 59 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  YTE..**.** SQLIT
d1d40 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d1d50 4b 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61  KING=true overla
d1d60 70 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f  ps SQLITE_WHOLE_
d1d70 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c  FILE_LOCKING=fal
d1d80 73 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65  se.** and so the
d1d90 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73   locking schemes
d1da0 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e   are compatible.
d1db0 20 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f    One type of lo
d1dc0 63 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63  ck will.** effec
d1dd0 74 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74  tively exclude t
d1de0 68 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20  he other type.  
d1df0 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75  The reason for u
d1e00 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  sing the.** SQLI
d1e10 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f  TE_WHOLE_FILE_LO
d1e20 43 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68  CKING=true is th
d1e30 61 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67  at by indicating
d1e40 20 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a   the full range.
d1e50 2a 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62  ** of bytes to b
d1e60 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65  e read or writte
d1e70 6e 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73  n, we give hints
d1e80 20 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20   to NFS to help 
d1e90 69 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63  it.** maintain c
d1ea0 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20  ache coherency. 
d1eb0 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
d1ec0 6e 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c  nd, whole file l
d1ed0 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f  ocking.** is slo
d1ee0 77 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74  wer, so we don't
d1ef0 20 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20   want to use it 
d1f00 65 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a  except for NFS..
d1f10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61  */.static int ra
d1f20 6e 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65  ngeLock(unixFile
d1f30 20 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c   *pFile, int op,
d1f40 20 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b   int *pErrcode){
d1f50 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
d1f60 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  lock;.  int rc;.
d1f70 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
d1f80 6f 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  op;.  lock.l_sta
d1f90 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  rt = SHARED_FIRS
d1fa0 54 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e  T;.  lock.l_when
d1fb0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
d1fc0 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c   if( (pFile->fil
d1fd0 65 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  eFlags & SQLITE_
d1fe0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
d1ff0 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f  NG)==0 ){.    lo
d2000 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45  ck.l_len = SHARE
d2010 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d  D_SIZE;.    rc =
d2020 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d2030 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d2040 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20  ;.    *pErrcode 
d2050 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65  = errno;.  }else
d2060 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  {.    lock.l_len
d2070 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66   = 0;.    rc = f
d2080 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46  cntl(pFile->h, F
d2090 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a  _SETLK, &lock);.
d20a0 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20      *pErrcode = 
d20b0 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e  errno;.    if( N
d20c0 45 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b  EVER(op==F_UNLCK
d20d0 29 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b  ) || rc!=(-1) ){
d20e0 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  .      lock.l_st
d20f0 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c  art = 0;.      l
d2100 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44  ock.l_len = PEND
d2110 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20  ING_BYTE;.      
d2120 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65  rc = fcntl(pFile
d2130 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d2140 6f 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ock);.      if( 
d2150 41 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c  ALWAYS(op!=F_UNL
d2160 43 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20  CK) && rc==(-1) 
d2170 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72  ){.        *pErr
d2180 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  code = errno;.  
d2190 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70        lock.l_typ
d21a0 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
d21b0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72       lock.l_star
d21c0 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
d21d0 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  ;.        lock.l
d21e0 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  _len = 0;.      
d21f0 20 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68    fcntl(pFile->h
d2200 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d2210 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
d2220 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
d2230 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20  ;.}../*.** Lock 
d2240 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
d2250 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
d2260 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f   by parameter lo
d2270 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20  cktype - one.** 
d2280 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
d2290 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
d22a0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SHARED_LOCK.**  
d22b0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f     (2) RESERVED_
d22c0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20  LOCK.**     (3) 
d22d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20  PENDING_LOCK.** 
d22e0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56      (4) EXCLUSIV
d22f0 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d  E_LOCK.**.** Som
d2300 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
d2310 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
d2320 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
d2330 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a  l lock states.**
d2340 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
d2350 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c   between.  The l
d2360 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69  ocking might fai
d2370 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  l on one of the 
d2380 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74  later.** transit
d2390 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
d23a0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
d23b0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
d23c0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a  it started but.*
d23d0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66  * still short of
d23e0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20   its goal.  The 
d23f0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20  following chart 
d2400 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65  shows the allowe
d2410 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  d.** transitions
d2420 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65   and the inserte
d2430 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  d intermediate s
d2440 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tates:.**.**    
d2450 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52  UNLOCKED -> SHAR
d2460 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
d2470 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20  -> RESERVED.**  
d2480 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
d2490 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
d24a0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45  VE.**    RESERVE
d24b0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
d24c0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
d24d0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43    PENDING -> EXC
d24e0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69  LUSIVE.**.** Thi
d24f0 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
d2500 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
d2510 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
d2520 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
d2530 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
d2540 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
d2550 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
d2560 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69  nt unixLock(sqli
d2570 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
d2580 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f  t locktype){.  /
d2590 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
d25a0 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d  describes the im
d25b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
d25c0 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b  the various lock
d25d0 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20  s and.  ** lock 
d25e0 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74  transitions in t
d25f0 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49  erms of the POSI
d2600 58 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65  X advisory share
d2610 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a  d and exclusive.
d2620 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74    ** lock primit
d2630 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61  ives (called rea
d2640 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74  d-locks and writ
d2650 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74  e-locks below, t
d2660 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e  o avoid.  ** con
d2670 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69  fusion with SQLi
d2680 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20  te lock names). 
d2690 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61  The algorithms a
d26a0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20  re complicated. 
d26b0 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20   ** slightly in 
d26c0 6f 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70  order to be comp
d26d0 61 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64  atible with wind
d26e0 6f 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75  ows systems simu
d26f0 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20  ltaneously.  ** 
d2700 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61  accessing the sa
d2710 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  me database file
d2720 2c 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69  , in case that i
d2730 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e  s ever required.
d2740 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f  .  **.  ** Symbo
d2750 6c 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73  ls defined in os
d2760 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65  .h indentify the
d2770 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20   'pending byte' 
d2780 61 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65  and the 'reserve
d2790 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61  d.  ** byte', ea
d27a0 63 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20  ch single bytes 
d27b0 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66  at well known of
d27c0 66 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27  fsets, and the '
d27d0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d27e0 20 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65   range', a range
d27f0 20 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74   of 510 bytes at
d2800 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66   a well known of
d2810 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  fset..  **.  ** 
d2820 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52  To obtain a SHAR
d2830 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d  ED lock, a read-
d2840 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
d2850 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67   on the 'pending
d2860 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66  .  ** byte'.  If
d2870 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73   this is success
d2880 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79  ful, a random by
d2890 74 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61  te from the 'sha
d28a0 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61  red byte.  ** ra
d28b0 6e 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63  nge' is read-loc
d28c0 6b 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b  ked and the lock
d28d0 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67   on the 'pending
d28e0 20 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e   byte' released.
d28f0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f  .  **.  ** A pro
d2900 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62  cess may only ob
d2910 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20  tain a RESERVED 
d2920 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61  lock after it ha
d2930 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e  s a SHARED lock.
d2940 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44  .  ** A RESERVED
d2950 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65   lock is impleme
d2960 6e 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67  nted by grabbing
d2970 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e   a write-lock on
d2980 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72   the.  ** 'reser
d2990 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a  ved byte'. .  **
d29a0 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20  .  ** A process 
d29b0 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20  may only obtain 
d29c0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61  a PENDING lock a
d29d0 66 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61  fter it has obta
d29e0 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52  ined a.  ** SHAR
d29f0 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49  ED lock. A PENDI
d2a00 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65  NG lock is imple
d2a10 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
d2a20 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
d2a30 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65  .  ** on the 'pe
d2a40 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69  nding byte'. Thi
d2a50 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e  s ensures that n
d2a60 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63  o new SHARED loc
d2a70 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f  ks can be.  ** o
d2a80 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69  btained, but exi
d2a90 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63  sting SHARED loc
d2aa0 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  ks are allowed t
d2ab0 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f  o persist. A pro
d2ac0 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e  cess.  ** does n
d2ad0 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69  ot have to obtai
d2ae0 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  n a RESERVED loc
d2af0 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20  k on the way to 
d2b00 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a  a PENDING lock..
d2b10 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72    ** This proper
d2b20 74 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68  ty is used by th
d2b30 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20  e algorithm for 
d2b40 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a  rolling back a j
d2b50 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a  ournal file.  **
d2b60 20 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a   after a crash..
d2b70 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43    **.  ** An EXC
d2b80 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74  LUSIVE lock, obt
d2b90 61 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45  ained after a PE
d2ba0 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65  NDING lock is he
d2bb0 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c  ld, is.  ** impl
d2bc0 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69  emented by obtai
d2bd0 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ning a write-loc
d2be0 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20  k on the entire 
d2bf0 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a  'shared byte.  *
d2c00 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20  * range'. Since 
d2c10 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20  all other locks 
d2c20 72 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c  require a read-l
d2c30 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ock on one of th
d2c40 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74  e bytes.  ** wit
d2c50 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20  hin this range, 
d2c60 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  this ensures tha
d2c70 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73  t no other locks
d2c80 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65   are held on the
d2c90 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
d2ca0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72  .  **.  ** The r
d2cb0 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62  eason a single b
d2cc0 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  yte cannot be us
d2cd0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
d2ce0 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20  e 'shared byte. 
d2cf0 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68   ** range' is th
d2d00 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73  at some versions
d2d10 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e   of windows do n
d2d20 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d  ot support read-
d2d30 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c  locks. By.  ** l
d2d40 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20  ocking a random 
d2d50 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67  byte from a rang
d2d60 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48  e, concurrent SH
d2d70 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65  ARED locks may e
d2d80 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69  xist.  ** even i
d2d90 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72  f the locking pr
d2da0 69 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20  imitive used is 
d2db0 61 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c  always a write-l
d2dc0 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ock..  */.  int 
d2dd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d2de0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d2df0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d2e00 64 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  d;.  struct unix
d2e10 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20  LockInfo *pLock 
d2e20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a  = pFile->pLock;.
d2e30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
d2e40 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30  ock;.  int s = 0
d2e50 3b 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a  ;.  int tErrno;.
d2e60 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d2e70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22   );.  OSTRACE7("
d2e80 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61  LOCK    %d %s wa
d2e90 73 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d  s %s(%s,%d) pid=
d2ea0 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46  %d (unix)\n", pF
d2eb0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f  ile->h,.      lo
d2ec0 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
d2ed0 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61  ype), locktypeNa
d2ee0 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  me(pFile->lockty
d2ef0 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74  pe),.      lockt
d2f00 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c  ypeName(pLock->l
d2f10 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d  ocktype), pLock-
d2f20 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29  >cnt , getpid())
d2f30 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
d2f40 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
d2f50 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
d2f60 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
d2f70 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
d2f80 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  unixFile, do not
d2f90 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
d2fa0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
d2fb0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
d2fc0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
d2fd0 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
d2fe0 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
d2ff0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d3000 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29  type>=locktype )
d3010 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  {.    OSTRACE3("
d3020 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b  LOCK    %d %s ok
d3030 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 20   (already held) 
d3040 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65  (unix)\n", pFile
d3050 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
d3060 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d3070 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65  cktype));.    re
d3080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d3090 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
d30a0 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ure the locking 
d30b0 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72  sequence is corr
d30c0 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20 57  ect..  **  (1) W
d30d0 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 6f  e never move fro
d30e0 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 6e  m unlocked to an
d30f0 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 68  ything higher th
d3100 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 0a  an shared lock..
d3110 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 65    **  (2) SQLite
d3120 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 6c   never explicitl
d3130 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65 6e  y requests a pen
d3140 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20  dig lock..  **  
d3150 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f 63  (3) A shared loc
d3160 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64  k is always held
d3170 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 20   when a reserve 
d3180 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65  lock is requeste
d3190 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
d31a0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
d31b0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f  e!=NO_LOCK || lo
d31c0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d31d0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
d31e0 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49   locktype!=PENDI
d31f0 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  NG_LOCK );.  ass
d3200 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52  ert( locktype!=R
d3210 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20  ESERVED_LOCK || 
d3220 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d3230 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d3240 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78  .  /* This mutex
d3250 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75   is needed becau
d3260 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20  se pFile->pLock 
d3270 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
d3280 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20   threads.  */.  
d3290 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
d32a0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  ;..  /* Make sur
d32b0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  e the current th
d32c0 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46  read owns the pF
d32d0 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ile..  */.  rc =
d32e0 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
d32f0 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ip(pFile);.  if(
d3300 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
d3310 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  {.    unixLeaveM
d3320 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75  utex();.    retu
d3330 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f  rn rc;.  }.  pLo
d3340 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ck = pFile->pLoc
d3350 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65  k;..  /* If some
d3360 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
d3370 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63  is PID has a loc
d3380 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e  k via a differen
d3390 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a  t unixFile*.  **
d33a0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65   handle that pre
d33b0 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65  cludes the reque
d33c0 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72  sted lock, retur
d33d0 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  n BUSY..  */.  i
d33e0 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  f( (pFile->lockt
d33f0 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ype!=pLock->lock
d3400 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20  type && .       
d3410 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74     (pLock->lockt
d3420 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
d3430 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48  K || locktype>SH
d3440 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b  ARED_LOCK)).  ){
d3450 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d3460 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20  _BUSY;.    goto 
d3470 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20  end_lock;.  }.. 
d3480 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20   /* If a SHARED 
d3490 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65  lock is requeste
d34a0 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65  d, and some thre
d34b0 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49  ad using this PI
d34c0 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68  D already.  ** h
d34d0 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52  as a SHARED or R
d34e0 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68  ESERVED lock, th
d34f0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66  en increment ref
d3500 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e  erence counts an
d3510 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51  d.  ** return SQ
d3520 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20  LITE_OK..  */.  
d3530 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d3540 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20  ARED_LOCK && .  
d3550 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b      (pLock->lock
d3560 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d3570 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  K || pLock->lock
d3580 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
d3590 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65  OCK) ){.    asse
d35a0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  rt( locktype==SH
d35b0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ARED_LOCK );.   
d35c0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
d35d0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20  locktype==0 );. 
d35e0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
d35f0 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70  ->cnt>0 );.    p
d3600 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d3610 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d3620 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a    pLock->cnt++;.
d3630 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
d3640 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67  ->nLock++;.    g
d3650 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
d3660 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49  }...  /* A PENDI
d3670 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65  NG lock is neede
d3680 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69  d before acquiri
d3690 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ng a SHARED lock
d36a0 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a   and before.  **
d36b0 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58   acquiring an EX
d36c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46  CLUSIVE lock.  F
d36d0 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f  or the SHARED lo
d36e0 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ck, the PENDING 
d36f0 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c  will.  ** be rel
d3700 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f  eased..  */.  lo
d3710 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20  ck.l_len = 1L;. 
d3720 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
d3730 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 28   SEEK_SET;.  if(
d3740 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
d3750 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c  D_LOCK .      ||
d3760 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
d3770 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46  USIVE_LOCK && pF
d3780 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45  ile->locktype<PE
d3790 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b  NDING_LOCK).  ){
d37a0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
d37b0 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   = (locktype==SH
d37c0 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43  ARED_LOCK?F_RDLC
d37d0 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20  K:F_WRLCK);.    
d37e0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50  lock.l_start = P
d37f0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
d3800 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65   s = fcntl(pFile
d3810 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d3820 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d  ock);.    if( s=
d3830 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 74  =(-1) ){.      t
d3840 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d3850 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d3860 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d3870 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d3880 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
d3890 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
d38a0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
d38b0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d38c0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d38d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
d38e0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
d38f0 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20     }.  }...  /* 
d3900 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20  If control gets 
d3910 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  to this point, t
d3920 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20  hen actually go 
d3930 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20  ahead and make. 
d3940 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79   ** operating sy
d3950 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74  stem calls for t
d3960 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63  he specified loc
d3970 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  k..  */.  if( lo
d3980 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d3990 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OCK ){.    asser
d39a0 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30  t( pLock->cnt==0
d39b0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
d39c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
d39d0 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f  =0 );..    /* No
d39e0 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c  w get the read-l
d39f0 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20 72  ock */.    s = r
d3a00 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20  angeLock(pFile, 
d3a10 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f  F_RDLCK, &tErrno
d3a20 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20  );..    /* Drop 
d3a30 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45  the temporary PE
d3a40 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20  NDING lock */.  
d3a50 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
d3a60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20   PENDING_BYTE;. 
d3a70 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
d3a80 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74  1L;.    lock.l_t
d3a90 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20  ype = F_UNLCK;. 
d3aa0 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69     if( fcntl(pFi
d3ab0 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d3ac0 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20  &lock)!=0 ){.   
d3ad0 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29     if( s != -1 )
d3ae0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
d3af0 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77  s could happen w
d3b00 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f  ith a network mo
d3b10 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74  unt */.        t
d3b20 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a  Errno = errno; .
d3b30 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
d3b40 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d3b50 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d3b60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
d3b70 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66  CK); .        if
d3b80 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d3b90 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rc) ){.         
d3ba0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d3bb0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d3bc0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f      }.        go
d3bd0 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20  to end_lock;.   
d3be0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
d3bf0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20  f( s==(-1) ){.  
d3c00 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d3c10 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d3c20 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d3c30 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d3c40 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d3c50 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d3c60 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d3c70 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d3c80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
d3c90 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  se{.      pFile-
d3ca0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
d3cb0 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70  ED_LOCK;.      p
d3cc0 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f  File->pOpen->nLo
d3cd0 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63  ck++;.      pLoc
d3ce0 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20  k->cnt = 1;.    
d3cf0 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  }.  }else if( lo
d3d00 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
d3d10 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d  E_LOCK && pLock-
d3d20 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a  >cnt>1 ){.    /*
d3d30 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20 66   We are trying f
d3d40 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  or an exclusive 
d3d50 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72  lock but another
d3d60 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a   thread in this.
d3d70 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63      ** same proc
d3d80 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c  ess is still hol
d3d90 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f  ding a shared lo
d3da0 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ck. */.    rc = 
d3db0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
d3dc0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
d3dd0 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72   request was for
d3de0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45   a RESERVED or E
d3df0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
d3e00 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73  It is.    ** ass
d3e10 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20  umed that there 
d3e20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67  is a SHARED or g
d3e30 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74  reater lock on t
d3e40 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61  he file.    ** a
d3e50 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20  lready..    */. 
d3e60 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 46     assert( 0!=pF
d3e70 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
d3e80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
d3e90 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20   = F_WRLCK;.    
d3ea0 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65  switch( locktype
d3eb0 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52   ){.      case R
d3ec0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20  ESERVED_LOCK:.  
d3ed0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
d3ee0 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  rt = RESERVED_BY
d3ef0 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d 20  TE;.        s = 
d3f00 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d3f10 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b  F_SETLK, &lock);
d3f20 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20  .        tErrno 
d3f30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
d3f40 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
d3f50 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  se EXCLUSIVE_LOC
d3f60 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20 72  K:.        s = r
d3f70 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20  angeLock(pFile, 
d3f80 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e 6f  F_WRLCK, &tErrno
d3f90 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
d3fa0 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
d3fb0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
d3fc0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
d3fd0 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20  ( s==(-1) ){.   
d3fe0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d3ff0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d4000 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d4010 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20  _IOERR_LOCK);.  
d4020 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d4030 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
d4040 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d4050 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d4060 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
d4070 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e 44  }.  ..#ifndef ND
d4080 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75 70  EBUG.  /* Set up
d4090 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
d40a0 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 20  -counter change 
d40b0 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20 77  checking flags w
d40c0 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69 74  hen.  ** transit
d40d0 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53 48  ioning from a SH
d40e0 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52 56  ARED to a RESERV
d40f0 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63 68  ED lock.  The ch
d4100 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 53  ange.  ** from S
d4110 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 45  HARED to RESERVE
d4120 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67 69  D marks the begi
d4130 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d 61  nning of a norma
d4140 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70 65  l.  ** write ope
d4150 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68 6f  ration (not a ho
d4160 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t journal rollba
d4170 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ck)..  */.  if( 
d4180 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  rc==SQLITE_OK.  
d4190 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
d41a0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
d41b0 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  .   && locktype=
d41c0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 20  =RESERVED_LOCK. 
d41d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 74   ){.    pFile->t
d41e0 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 30  ransCntrChng = 0
d41f0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 55  ;.    pFile->dbU
d4200 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 70  pdate = 0;.    p
d4210 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72  File->inNormalWr
d4220 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e  ite = 1;.  }.#en
d4230 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d 3d  dif...  if( rc==
d4240 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
d4250 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d4260 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20   = locktype;.   
d4270 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d4280 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d   = locktype;.  }
d4290 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70  else if( locktyp
d42a0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
d42b0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d42c0 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  locktype = PENDI
d42d0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f  NG_LOCK;.    pLo
d42e0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50  ck->locktype = P
d42f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d  ENDING_LOCK;.  }
d4300 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e  ..end_lock:.  un
d4310 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
d4320 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b    OSTRACE4("LOCK
d4330 20 20 20 20 25 64 20 25 73 20 25 73 20 28 75 6e      %d %s %s (un
d4340 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ix)\n", pFile->h
d4350 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c  , locktypeName(l
d4360 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20  ocktype), .     
d4370 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
d4380 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
d4390 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
d43a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d43b0 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ll file descript
d43c0 6f 72 73 20 61 63 63 75 6d 75 61 74 65 64 20 69  ors accumuated i
d43d0 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e  n the unixOpenCn
d43e0 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e  t->pUnused list.
d43f0 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20  .** If all such 
d4400 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
d4410 20 61 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68   are closed with
d4420 6f 75 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c  out error, the l
d4430 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65  ist is.** cleare
d4440 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  d and SQLITE_OK 
d4450 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
d4460 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e  Otherwise, if an
d4470 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
d4480 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  hen successfully
d4490 20 63 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73   closed file des
d44a0 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69  criptor.** entri
d44b0 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  es are removed f
d44c0 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e  rom the list, an
d44d0 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  d SQLITE_IOERR_C
d44e0 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a  LOSE returned. .
d44f0 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61  ** not deleted a
d4500 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  nd SQLITE_IOERR_
d4510 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a  CLOSE returned..
d4520 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */ .static int c
d4530 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75  losePendingFds(u
d4540 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  nixFile *pFile){
d4550 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d4560 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20  TE_OK;.  struct 
d4570 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
d4580 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
d4590 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  n;.  UnixUnusedF
d45a0 64 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20  d *pError = 0;. 
d45b0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
d45c0 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64  ;.  UnixUnusedFd
d45d0 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70   *pNext;.  for(p
d45e0 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b  =pOpen->pUnused;
d45f0 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20   p; p=pNext){.  
d4600 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65    pNext = p->pNe
d4610 78 74 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73  xt;.    if( clos
d4620 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20  e(p->fd) ){.    
d4630 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d4640 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d4650 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
d4660 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  ERR_CLOSE;.     
d4670 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72   p->pNext = pErr
d4680 6f 72 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72  or;.      pError
d4690 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = p;.    }else{
d46a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
d46b0 72 65 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ree(p);.    }.  
d46c0 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73  }.  pOpen->pUnus
d46d0 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72  ed = pError;.  r
d46e0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d46f0 2a 2a 20 41 64 64 20 74 68 65 20 66 69 6c 65 20  ** Add the file 
d4700 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20  descriptor used 
d4710 62 79 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70  by file handle p
d4720 46 69 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72  File to the corr
d4730 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e  esponding.** pUn
d4740 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74  used list..*/.st
d4750 61 74 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e  atic void setPen
d4760 64 69 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20  dingFd(unixFile 
d4770 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63  *pFile){.  struc
d4780 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70  t unixOpenCnt *p
d4790 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f  Open = pFile->pO
d47a0 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65  pen;.  UnixUnuse
d47b0 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e  dFd *p = pFile->
d47c0 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e  pUnused;.  p->pN
d47d0 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e  ext = pOpen->pUn
d47e0 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70  used;.  pOpen->p
d47f0 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46  Unused = p;.  pF
d4800 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70  ile->h = -1;.  p
d4810 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20  File->pUnused = 
d4820 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  0;.}../*.** Lowe
d4830 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
d4840 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
d4850 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20  riptor pFile to 
d4860 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
d4870 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
d4880 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
d4890 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
d48a0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
d48b0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
d48c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
d48d0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
d48e0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
d48f0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
d4900 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
d4910 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
d4920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d4930 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  ixUnlock(sqlite3
d4940 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d4950 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78  ocktype){.  unix
d4960 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d4970 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20  nixFile*)id; /* 
d4980 54 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f  The open file */
d4990 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  .  struct unixLo
d49a0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20  ckInfo *pLock;  
d49b0 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65      /* Structure
d49c0 20 64 65 73 63 72 69 62 69 6e 67 20 63 75 72 72   describing curr
d49d0 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a  ent lock state *
d49e0 2f 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  /.  struct flock
d49f0 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20   lock;          
d4a00 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
d4a10 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20  ion passed into 
d4a20 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74  fcntl() */.  int
d4a30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d4a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d4a50 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
d4a60 6d 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  m this interface
d4a70 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
d4a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4a90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e         /* The un
d4aa0 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65  derlying file de
d4ab0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e  scriptor */.  in
d4ac0 74 20 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20  t tErrno;       
d4ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d4ae0 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f  * Error code fro
d4af0 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72  m system call er
d4b00 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  rors */..  asser
d4b10 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
d4b20 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20  TRACE7("UNLOCK  
d4b30 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c  %d %d was %d(%d,
d4b40 25 64 29 20 70 69 64 3d 25 64 20 28 75 6e 69 78  %d) pid=%d (unix
d4b50 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
d4b60 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
d4b70 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
d4b80 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c   pFile->pLock->l
d4b90 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
d4ba0 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70  pLock->cnt, getp
d4bb0 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74  id());..  assert
d4bc0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
d4bd0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28  ED_LOCK );.  if(
d4be0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d4bf0 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  <=locktype ){.  
d4c00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d4c10 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48  OK;.  }.  if( CH
d4c20 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69  ECK_THREADID(pFi
d4c30 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  le) ){.    retur
d4c40 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
d4c50 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72  .  }.  unixEnter
d4c60 4d 75 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70  Mutex();.  h = p
d4c70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b  File->h;.  pLock
d4c80 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b   = pFile->pLock;
d4c90 0a 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b  .  assert( pLock
d4ca0 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66  ->cnt!=0 );.  if
d4cb0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
d4cc0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  e>SHARED_LOCK ){
d4cd0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f  .    assert( pLo
d4ce0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46  ck->locktype==pF
d4cf0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
d4d00 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  .    SimulateIOE
d4d10 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20  rrorBenign(1);. 
d4d20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72     SimulateIOErr
d4d30 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20  or( h=(-1) ).   
d4d40 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4d50 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e  Benign(0);..#ifn
d4d60 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
d4d70 2a 20 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20  * When reducing 
d4d80 61 20 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74  a lock such that
d4d90 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
d4da0 20 63 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a   can start.    *
d4db0 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  * reading the da
d4dc0 74 61 62 61 73 65 20 66 69 6c 65 20 61 67 61 69  tabase file agai
d4dd0 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  n, make sure tha
d4de0 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61  t the.    ** tra
d4df0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
d4e00 20 77 61 73 20 75 70 64 61 74 65 64 20 69 66 20   was updated if 
d4e10 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20  any part of the 
d4e20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20  database.    ** 
d4e30 66 69 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49  file changed.  I
d4e40 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
d4e50 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74  n counter is not
d4e60 20 75 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a   updated,.    **
d4e70 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
d4e80 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66  ns to the same f
d4e90 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65  ile might not re
d4ea0 61 6c 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a  alize that.    *
d4eb0 2a 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63  * the file has c
d4ec0 68 61 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65  hanged and hence
d4ed0 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20   might not know 
d4ee0 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20  to flush their. 
d4ef0 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68     ** cache.  Th
d4f00 65 20 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65  e use of a stale
d4f10 20 63 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20   cache can lead 
d4f20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  to database corr
d4f30 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  uption..    */. 
d4f40 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
d4f50 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d  ->inNormalWrite=
d4f60 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70  =0.         || p
d4f70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d  File->dbUpdate==
d4f80 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46  0.         || pF
d4f90 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68  ile->transCntrCh
d4fa0 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69  ng==1 );.    pFi
d4fb0 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74  le->inNormalWrit
d4fc0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a  e = 0;.#endif...
d4fd0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
d4fe0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
d4ff0 0a 20 20 20 20 20 20 69 66 28 20 72 61 6e 67 65  .      if( range
d5000 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44  Lock(pFile, F_RD
d5010 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3d 3d 28  LCK, &tErrno)==(
d5020 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  -1) ){.        r
d5030 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d5040 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d5050 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d5060 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20  RR_RDLOCK);.    
d5070 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d5080 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
d5090 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d50a0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d50b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
d50c0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c      goto end_unl
d50d0 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ock;.      }.   
d50e0 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79   }.    lock.l_ty
d50f0 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20  pe = F_UNLCK;.  
d5100 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
d5110 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20  = SEEK_SET;.    
d5120 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50  lock.l_start = P
d5130 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
d5140 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c   lock.l_len = 2L
d5150 3b 20 20 61 73 73 65 72 74 28 20 50 45 4e 44 49  ;  assert( PENDI
d5160 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52  NG_BYTE+1==RESER
d5170 56 45 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20  VED_BYTE );.    
d5180 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53  if( fcntl(h, F_S
d5190 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d  ETLK, &lock)!=(-
d51a0 31 29 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63  1) ){.      pLoc
d51b0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48  k->locktype = SH
d51c0 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  ARED_LOCK;.    }
d51d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 74 45 72 72  else{.      tErr
d51e0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d51f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d5200 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d5210 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d5220 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d5230 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d5240 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d5250 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d5260 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d5270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
d5280 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a  oto end_unlock;.
d5290 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
d52a0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
d52b0 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  K ){.    struct 
d52c0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
d52d0 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72  en;..    /* Decr
d52e0 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72 65 64  ement the shared
d52f0 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20   lock counter.  
d5300 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b  Release the lock
d5310 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a   using an.    **
d5320 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68   OS call only wh
d5330 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69  en all threads i
d5340 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63  n this same proc
d5350 65 73 73 20 68 61 76 65 20 72 65 6c 65 61 73 65  ess have release
d5360 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63  d.    ** the loc
d5370 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c  k..    */.    pL
d5380 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20  ock->cnt--;.    
d5390 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d  if( pLock->cnt==
d53a0 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  0 ){.      lock.
d53b0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b  l_type = F_UNLCK
d53c0 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77  ;.      lock.l_w
d53d0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
d53e0 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73  ;.      lock.l_s
d53f0 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65  tart = lock.l_le
d5400 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69  n = 0L;.      Si
d5410 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
d5420 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69  ign(1);.      Si
d5430 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68  mulateIOError( h
d5440 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69  =(-1) ).      Si
d5450 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
d5460 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66  ign(0);.      if
d5470 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54  ( fcntl(h, F_SET
d5480 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29  LK, &lock)!=(-1)
d5490 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63   ){.        pLoc
d54a0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  k->locktype = NO
d54b0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c  _LOCK;.      }el
d54c0 73 65 7b 0a 20 20 20 20 20 20 20 20 74 45 72 72  se{.        tErr
d54d0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d54e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d54f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d5500 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d5510 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d5520 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f  .        if( IS_
d5530 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d5540 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  {.          pFil
d5550 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d5560 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d  Errno;.        }
d5570 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
d5580 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
d5590 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c  CK;.        pFil
d55a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  e->locktype = NO
d55b0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  _LOCK;.      }. 
d55c0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63     }..    /* Dec
d55d0 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74  rement the count
d55e0 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73   of locks agains
d55f0 74 20 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65  t this same file
d5600 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20  .  When the.    
d5610 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73  ** count reaches
d5620 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79   zero, close any
d5630 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63   other file desc
d5640 72 69 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c  riptors whose cl
d5650 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64  ose.    ** was d
d5660 65 66 65 72 72 65 64 20 62 65 63 61 75 73 65 20  eferred because 
d5670 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c  of outstanding l
d5680 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ocks..    */.   
d5690 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e   pOpen = pFile->
d56a0 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e  pOpen;.    pOpen
d56b0 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61  ->nLock--;.    a
d56c0 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c  ssert( pOpen->nL
d56d0 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66  ock>=0 );.    if
d56e0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d  ( pOpen->nLock==
d56f0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  0 ){.      int r
d5700 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e  c2 = closePendin
d5710 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20  gFds(pFile);.   
d5720 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
d5730 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
d5740 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20  rc = rc2;.      
d5750 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e  }.    }.  }...en
d5760 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78  d_unlock:.  unix
d5770 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
d5780 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d5790 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  K ) pFile->lockt
d57a0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d57b0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d57c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
d57d0 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65  ion performs the
d57e0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22 63   parts of the "c
d57f0 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61  lose file" opera
d5800 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20  tion .** common 
d5810 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73  to all locking s
d5820 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65  chemes. It close
d5830 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  s the directory 
d5840 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64  and file.** hand
d5850 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72 65  les, if they are
d5860 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73   valid, and sets
d5870 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74   all fields of t
d5880 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73  he unixFile.** s
d5890 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a  tructure to 0..*
d58a0 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a  *.** It is *not*
d58b0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f   necessary to ho
d58c0 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68 65  ld the mutex whe
d58d0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
d58e0 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65  s called,.** eve
d58f0 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41  n on VxWorks.  A
d5900 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61   mutex will be a
d5910 63 71 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72  cquired on VxWor
d5920 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77  ks by the.** vxw
d5930 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49  orksReleaseFileI
d5940 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  d() routine..*/.
d5950 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65  static int close
d5960 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33  UnixFile(sqlite3
d5970 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e  _file *id){.  un
d5980 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d5990 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
d59a0 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20   if( pFile ){.  
d59b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72    if( pFile->dir
d59c0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  fd>=0 ){.      i
d59d0 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70  nt err = close(p
d59e0 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20  File->dirfd);.  
d59f0 20 20 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20      if( err ){. 
d5a00 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d5a10 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
d5a20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
d5a30 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
d5a40 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65  _CLOSE;.      }e
d5a50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 69  lse{.        pFi
d5a60 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20  le->dirfd=-1;.  
d5a70 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d5a80 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20  if( pFile->h>=0 
d5a90 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72  ){.      int err
d5aa0 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e   = close(pFile->
d5ab0 68 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72  h);.      if( er
d5ac0 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  r ){.        pFi
d5ad0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d5ae0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72  errno;.        r
d5af0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d5b00 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20  RR_CLOSE;.      
d5b10 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56  }.    }.#if OS_V
d5b20 58 57 4f 52 4b 53 0a 20 20 20 20 69 66 28 20 70  XWORKS.    if( p
d5b30 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20  File->pId ){.   
d5b40 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 69 73     if( pFile->is
d5b50 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20  Delete ){.      
d5b60 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e    unlink(pFile->
d5b70 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e  pId->zCanonicalN
d5b80 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
d5b90 20 20 20 20 76 78 77 6f 72 6b 73 52 65 6c 65 61      vxworksRelea
d5ba0 73 65 46 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e  seFileId(pFile->
d5bb0 70 49 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c  pId);.      pFil
d5bc0 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20 20 20 20  e->pId = 0;.    
d5bd0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54  }.#endif.    OST
d5be0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25  RACE2("CLOSE   %
d5bf0 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  -3d\n", pFile->h
d5c00 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  );.    OpenCount
d5c10 65 72 28 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  er(-1);.    sqli
d5c20 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
d5c30 70 55 6e 75 73 65 64 29 3b 0a 20 20 20 20 6d 65  pUnused);.    me
d5c40 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
d5c50 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
d5c60 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
d5c70 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
d5c80 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
d5c90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d5ca0 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  nixClose(sqlite3
d5cb0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e  _file *id){.  in
d5cc0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d5cd0 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  ;.  if( id ){.  
d5ce0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d5cf0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29  e = (unixFile *)
d5d00 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f  id;.    unixUnlo
d5d10 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  ck(id, NO_LOCK);
d5d20 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
d5d30 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70  tex();.    if( p
d5d40 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70  File->pOpen && p
d5d50 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f  File->pOpen->nLo
d5d60 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  ck ){.      /* I
d5d70 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  f there are outs
d5d80 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64  tanding locks, d
d5d90 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63  o not actually c
d5da0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75  lose the file ju
d5db0 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20  st.      ** yet 
d5dc0 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75  because that wou
d5dd0 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c  ld clear those l
d5de0 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20  ocks.  Instead, 
d5df0 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20  add the file.   
d5e00 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72     ** descriptor
d5e10 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73   to pOpen->pUnus
d5e20 65 64 20 6c 69 73 74 2e 20 20 49 74 20 77 69 6c  ed list.  It wil
d5e30 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
d5e40 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20 20 20 20  ly closed .     
d5e50 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73   ** when the las
d5e60 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65  t lock is cleare
d5e70 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
d5e80 20 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28 70    setPendingFd(p
d5e90 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  File);.    }.   
d5ea0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f   releaseLockInfo
d5eb0 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a  (pFile->pLock);.
d5ec0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43      releaseOpenC
d5ed0 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29  nt(pFile->pOpen)
d5ee0 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65  ;.    rc = close
d5ef0 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20  UnixFile(id);.  
d5f00 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
d5f10 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ();.  }.  return
d5f20 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   rc;.}../*******
d5f30 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
d5f40 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72  he posix advisor
d5f50 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  y lock implement
d5f60 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
d5f70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d5f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5fc0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
d5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6010 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
d6020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6030 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f  ******* No-op Lo
d6040 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cking **********
d6050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6060 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f  ********.**.** O
d6070 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f  f the various lo
d6080 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61  cking implementa
d6090 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c  tions available,
d60a0 20 74 68 69 73 20 69 73 20 62 79 20 66 61 72 20   this is by far 
d60b0 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a  the.** simplest:
d60c0 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e    locking is ign
d60d0 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70  ored.  No attemp
d60e0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63  t is made to loc
d60f0 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  k the database.*
d6100 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69  * file for readi
d6110 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a  ng or writing..*
d6120 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e  *.** This lockin
d6130 67 20 6d 6f 64 65 20 69 73 20 61 70 70 72 6f 70  g mode is approp
d6140 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e  riate for use on
d6150 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62   read-only datab
d6160 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74  ases.** (ex: dat
d6170 61 62 61 73 65 73 20 74 68 61 74 20 61 72 65 20  abases that are 
d6180 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52  burned into CD-R
d6190 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e  OM, for example.
d61a0 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73  )  It can.** als
d61b0 6f 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65  o be used if the
d61c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70   application emp
d61d0 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 65 72 6e  loys some extern
d61e0 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a  al mechanism to.
d61f0 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 6d 75 6c  ** prevent simul
d6200 74 61 6e 65 6f 75 73 20 61 63 63 65 73 73 20 6f  taneous access o
d6210 66 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  f the same datab
d6220 61 73 65 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f  ase by two or mo
d6230 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  re.** database c
d6240 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74  onnections.  But
d6250 20 74 68 65 72 65 20 69 73 20 61 20 73 65 72 69   there is a seri
d6260 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 61 74 61  ous risk of data
d6270 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69  base.** corrupti
d6280 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f 63 6b 69  on if this locki
d6290 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 65 64 20  ng mode is used 
d62a0 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68  in situations wh
d62b0 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20  ere multiple.** 
d62c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
d62d0 69 6f 6e 73 20 61 72 65 20 61 63 63 65 73 73 69  ions are accessi
d62e0 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
d62f0 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65  base file at the
d6300 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e   same.** time an
d6310 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  d one or more of
d6320 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f   those connectio
d6330 6e 73 20 61 72 65 20 77 72 69 74 69 6e 67 2e 0a  ns are writing..
d6340 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  */..static int n
d6350 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76  olockCheckReserv
d6360 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
d6370 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
d6380 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55  t *pResOut){.  U
d6390 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
d63a0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65  NotUsed);.  *pRe
d63b0 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75  sOut = 0;.  retu
d63c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d63d0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
d63e0 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  kLock(sqlite3_fi
d63f0 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  le *NotUsed, int
d6400 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e   NotUsed2){.  UN
d6410 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
d6420 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
d6430 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  2);.  return SQL
d6440 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63  ITE_OK;.}.static
d6450 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63   int nolockUnloc
d6460 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d6470 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74  NotUsed, int Not
d6480 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44  Used2){.  UNUSED
d6490 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
d64a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
d64b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d64c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  OK;.}../*.** Clo
d64d0 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  se the file..*/.
d64e0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
d64f0 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  kClose(sqlite3_f
d6500 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74  ile *id) {.  ret
d6510 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  urn closeUnixFil
d6520 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  e(id);.}../*****
d6530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
d6540 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20  nd of the no-op 
d6550 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d6560 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
d6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d6580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a  ********/../****
d65d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d65f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d6620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6630 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66  **** Begin dot-f
d6640 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  ile Locking ****
d6650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
d6670 20 54 68 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63   The dotfile loc
d6680 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74  king implementat
d6690 69 6f 6e 20 75 73 65 73 20 74 68 65 20 65 78 69  ion uses the exi
d66a0 73 74 61 6e 63 65 20 6f 66 20 73 65 70 61 72 61  stance of separa
d66b0 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73  te lock.** files
d66c0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e   in order to con
d66d0 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74  trol access to t
d66e0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
d66f0 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a 75 73 74  is works on just
d6700 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65 72 79 20  .** about every 
d6710 66 69 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69  filesystem imagi
d6720 6e 61 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72  nable.  But ther
d6730 65 20 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f  e are serious do
d6740 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  wnsides:.**.**  
d6750 20 20 28 31 29 20 20 54 68 65 72 65 20 69 73 20    (1)  There is 
d6760 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79  zero concurrency
d6770 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64  .  A single read
d6780 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74  er blocks all ot
d6790 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63  her.**         c
d67a0 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20  onnections from 
d67b0 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
d67c0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
d67d0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41  .**.**    (2)  A
d67e0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72  n application cr
d67f0 61 73 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73  ash or power los
d6800 73 20 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c  s can leave stal
d6810 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20  e lock files.** 
d6820 20 20 20 20 20 20 20 20 73 69 74 74 69 6e 67 20          sitting 
d6830 61 72 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64  around that need
d6840 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d   to be cleared m
d6850 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e  anually..**.** N
d6860 65 76 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64  evertheless, a d
d6870 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70  otlock is an app
d6880 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67  ropriate locking
d6890 20 6d 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66   mode for use if
d68a0 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63   no.** other loc
d68b0 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 69 73  king strategy is
d68c0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a   available..**.*
d68d0 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e  * Dotfile lockin
d68e0 67 20 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74  g works by creat
d68f0 69 6e 67 20 61 20 66 69 6c 65 20 69 6e 20 74 68  ing a file in th
d6900 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79  e same directory
d6910 20 61 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   as the.** datab
d6920 61 73 65 20 61 6e 64 20 77 69 74 68 20 74 68 65  ase and with the
d6930 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77   same name but w
d6940 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65 78  ith a ".lock" ex
d6950 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0a 2a  tension added..*
d6960 2a 20 54 68 65 20 65 78 69 73 74 61 6e 63 65 20  * The existance 
d6970 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c 65 20 69  of a lock file i
d6980 6d 70 6c 69 65 73 20 61 6e 20 45 58 43 4c 55 53  mplies an EXCLUS
d6990 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f  IVE lock.  All o
d69a0 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70  ther lock.** typ
d69b0 65 73 20 28 53 48 41 52 45 44 2c 20 52 45 53 45  es (SHARED, RESE
d69c0 52 56 45 44 2c 20 50 45 4e 44 49 4e 47 29 20 61  RVED, PENDING) a
d69d0 72 65 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 45  re mapped into E
d69e0 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a  XCLUSIVE..*/../*
d69f0 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 73 75 66  .** The file suf
d6a00 66 69 78 20 61 64 64 65 64 20 74 6f 20 74 68 65  fix added to the
d6a10 20 64 61 74 61 20 62 61 73 65 20 66 69 6c 65 6e   data base filen
d6a20 61 6d 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ame in order to 
d6a30 63 72 65 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f  create the.** lo
d6a40 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66  ck file..*/.#def
d6a50 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46  ine DOTLOCK_SUFF
d6a60 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a  IX ".lock"../*.*
d6a70 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
d6a80 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
d6a90 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
d6aa0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
d6ab0 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
d6ac0 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
d6ad0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
d6ae0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
d6af0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f  held, set *pResO
d6b00 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a  ut.** to a non-z
d6b10 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77  ero value otherw
d6b20 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20  ise *pResOut is 
d6b30 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  set to zero.  Th
d6b40 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
d6b50 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
d6b60 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20  TE_OK unless an 
d6b70 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73  I/O error occurs
d6b80 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65   during lock che
d6b90 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  cking..**.** In 
d6ba0 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c  dotfile locking,
d6bb0 20 65 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65   either a lock e
d6bc0 78 69 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73  xists or it does
d6bd0 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69   not.  So in thi
d6be0 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f  s.** variation o
d6bf0 66 20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  f CheckReservedL
d6c00 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20  ock(), *pResOut 
d6c10 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
d6c20 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73  f any lock.** is
d6c30 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c   held on the fil
d6c40 65 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74  e and false if t
d6c50 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63  he file is unloc
d6c60 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ked..*/.static i
d6c70 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52  nt dotlockCheckR
d6c80 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
d6c90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
d6ca0 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20  t *pResOut) {.  
d6cb0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d6cc0 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76  OK;.  int reserv
d6cd0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69  ed = 0;.  unixFi
d6ce0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d6cf0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69  xFile*)id;..  Si
d6d00 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d6d10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d6d20 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
d6d30 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73  LOCK; );.  .  as
d6d40 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a  sert( pFile );..
d6d50 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
d6d60 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
d6d70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
d6d80 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66  h a lock */.  if
d6d90 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
d6da0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  e>SHARED_LOCK ){
d6db0 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74  .    /* Either t
d6dc0 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  his connection o
d6dd0 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e  r some other con
d6de0 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73  nection in the s
d6df0 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 20 20 20  ame process.    
d6e00 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20  ** holds a lock 
d6e10 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 4e 6f  on the file.  No
d6e20 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66   need to check f
d6e30 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 72  urther. */.    r
d6e40 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d  eserved = 1;.  }
d6e50 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
d6e60 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 66   lock is held if
d6e70 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65   and only if the
d6e80 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 73   lockfile exists
d6e90 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
d6ea0 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20  ar *zLockFile = 
d6eb0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 69  (const char*)pFi
d6ec0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d6ed0 78 74 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64  xt;.    reserved
d6ee0 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f 63 6b 46   = access(zLockF
d6ef0 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a  ile, 0)==0;.  }.
d6f00 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54    OSTRACE4("TEST
d6f10 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25   WR-LOCK %d %d %
d6f20 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c 20  d (dotlock)\n", 
d6f30 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65  pFile->h, rc, re
d6f40 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 65 73  served);.  *pRes
d6f50 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a  Out = reserved;.
d6f60 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d6f70 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66  /*.** Lock the f
d6f80 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63  ile with the loc
d6f90 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  k specified by p
d6fa0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70  arameter locktyp
d6fb0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68  e - one.** of th
d6fc0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
d6fd0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45  **     (1) SHARE
d6fe0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32  D_LOCK.**     (2
d6ff0 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ) RESERVED_LOCK.
d7000 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49  **     (3) PENDI
d7010 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  NG_LOCK.**     (
d7020 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  4) EXCLUSIVE_LOC
d7030 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65  K.**.** Sometime
d7040 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e  s when requestin
d7050 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65  g one lock state
d7060 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63  , additional loc
d7070 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20  k states.** are 
d7080 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77  inserted in betw
d7090 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  een.  The lockin
d70a0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20  g might fail on 
d70b0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72  one of the later
d70c0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d70d0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b  leaving the lock
d70e0 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74   state different
d70f0 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74   from what it st
d7100 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69  arted but.** sti
d7110 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20  ll short of its 
d7120 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  goal.  The follo
d7130 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73  wing chart shows
d7140 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20   the allowed.** 
d7150 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20  transitions and 
d7160 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  the inserted int
d7170 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73  ermediate states
d7180 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43  :.**.**    UNLOC
d7190 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a  KED -> SHARED.**
d71a0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45      SHARED -> RE
d71b0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41  SERVED.**    SHA
d71c0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  RED -> (PENDING)
d71d0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
d71e0 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20      RESERVED -> 
d71f0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d7200 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e  LUSIVE.**    PEN
d7210 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56  DING -> EXCLUSIV
d7220 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  E.**.** This rou
d7230 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
d7240 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
d7250 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33   Use the sqlite3
d7260 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f  OsUnlock().** ro
d7270 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61  utine to lower a
d7280 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a   locking level..
d7290 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 66 69  **.** With dotfi
d72a0 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72  le locking, we r
d72b0 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f  eally only suppo
d72c0 72 74 20 73 74 61 74 65 20 28 34 29 3a 20 45 58  rt state (4): EX
d72d0 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20  CLUSIVE..** But 
d72e0 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f 74 68  we track the oth
d72f0 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  er locking level
d7300 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f  s internally..*/
d7310 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
d7320 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  ockLock(sqlite3_
d7330 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d7340 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
d7350 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d7360 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69  nixFile*)id;.  i
d7370 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a  nt fd;.  char *z
d7380 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72  LockFile = (char
d7390 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
d73a0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20  gContext;.  int 
d73b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d73c0 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76  ..  /* If we hav
d73d0 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e  e any lock, then
d73e0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61   the lock file a
d73f0 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20  lready exists.  
d7400 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 2a 2a  All we have.  **
d7410 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 73 74   to do is adjust
d7420 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 72 65   our internal re
d7430 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b  cord of the lock
d7440 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69   level..  */.  i
d7450 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d7460 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  pe > NO_LOCK ){.
d7470 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d7480 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d7490 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a  #if !OS_VXWORKS.
d74a0 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70      /* Always up
d74b0 64 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61  date the timesta
d74c0 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69  mp on the old fi
d74d0 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73  le */.    utimes
d74e0 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c  (zLockFile, NULL
d74f0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65  );.#endif.    re
d7500 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d7510 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62    }.  .  /* grab
d7520 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
d7530 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65  ck */.  fd = ope
d7540 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44  n(zLockFile,O_RD
d7550 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45  ONLY|O_CREAT|O_E
d7560 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28  XCL,0600);.  if(
d7570 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20   fd<0 ){.    /* 
d7580 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63  failed to open/c
d7590 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20  reate the file, 
d75a0 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79  someone else may
d75b0 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65   have stolen the
d75c0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74   lock */.    int
d75d0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d75e0 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20  .    if( EEXIST 
d75f0 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20  == tErrno ){.   
d7600 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
d7610 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  USY;.    } else 
d7620 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
d7630 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d7640 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d7650 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
d7660 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f  );.      if( IS_
d7670 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d7680 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d7690 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d76a0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rno;.      }.   
d76b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
d76c0 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f  ;.  } .  if( clo
d76d0 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20 70 46  se(fd) ){.    pF
d76e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d76f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d   errno;.    rc =
d7700 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c   SQLITE_IOERR_CL
d7710 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  OSE;.  }.  .  /*
d7720 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65   got it, set the
d7730 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e   type and return
d7740 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e   ok */.  pFile->
d7750 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d7760 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ype;.  return rc
d7770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72  ;.}../*.** Lower
d7780 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
d7790 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72  el on file descr
d77a0 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c  iptor pFile to l
d77b0 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79  ocktype.  lockty
d77c0 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  pe.** must be ei
d77d0 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20  ther NO_LOCK or 
d77e0 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a  SHARED_LOCK..**.
d77f0 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e  ** If the lockin
d7800 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66  g level of the f
d7810 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
d7820 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20  s already at or 
d7830 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71  below.** the req
d7840 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c  uested locking l
d7850 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69  evel, this routi
d7860 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
d7870 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f  *.** When the lo
d7880 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 61 63  cking level reac
d7890 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c  hes NO_LOCK, del
d78a0 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c  ete the lock fil
d78b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
d78c0 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73   dotlockUnlock(s
d78d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d78e0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d78f0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d7900 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d7910 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63  id;.  char *zLoc
d7920 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  kFile = (char *)
d7930 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d7940 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74  ntext;..  assert
d7950 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
d7960 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE5("UNLOCK  %
d7970 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d  d %d was %d pid=
d7980 25 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c  %d (dotlock)\n",
d7990 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
d79a0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e  ype,..   pFile->
d79b0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
d79c0 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  ());.  assert( l
d79d0 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
d79e0 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  LOCK );.  .  /* 
d79f0 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c  no-op if possibl
d7a00 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  e */.  if( pFile
d7a10 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b  ->locktype==lock
d7a20 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
d7a30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d7a40 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67  }..  /* To downg
d7a50 72 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20  rade to shared, 
d7a60 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75  simply update ou
d7a70 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f  r internal notio
d7a80 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f  n of the.  ** lo
d7a90 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65  ck state.  No ne
d7aa0 65 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20  ed to mess with 
d7ab0 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b  the file on disk
d7ac0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  ..  */.  if( loc
d7ad0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d7ae0 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
d7af0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  >locktype = SHAR
d7b00 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74  ED_LOCK;.    ret
d7b10 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
d7b20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75   }.  .  /* To fu
d7b30 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64  lly unlock the d
d7b40 61 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20  atabase, delete 
d7b50 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f  the lock file */
d7b60 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d7b70 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a  ype==NO_LOCK );.
d7b80 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f    if( unlink(zLo
d7b90 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69  ckFile) ){.    i
d7ba0 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69  nt rc = 0;.    i
d7bb0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d7bc0 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e  o;.    if( ENOEN
d7bd0 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20  T != tErrno ){. 
d7be0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d7bf0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d7c00 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d7c10 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
d7c20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
d7c30 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d7c40 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ) ){.      pFile
d7c50 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d7c60 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rrno;.    }.    
d7c70 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a  return rc; .  }.
d7c80 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d7c90 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72  e = NO_LOCK;.  r
d7ca0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d7cb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
d7cc0 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75  a file.  Make su
d7cd0 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20  re the lock has 
d7ce0 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65  been released be
d7cf0 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f  fore closing..*/
d7d00 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
d7d10 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  ockClose(sqlite3
d7d20 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
d7d30 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20  nt rc;.  if( id 
d7d40 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
d7d50 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d7d60 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c  le*)id;.    dotl
d7d70 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f  ockUnlock(id, NO
d7d80 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69  _LOCK);.    sqli
d7d90 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
d7da0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
d7db0 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73  .  }.  rc = clos
d7dc0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20  eUnixFile(id);. 
d7dd0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a   return rc;.}./*
d7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7df0 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74  * End of the dot
d7e00 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65  -file lock imple
d7e10 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a  mentation ******
d7e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d7e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
d7e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d7ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
d7ef0 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a  flock Locking **
d7f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d7f20 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f  *.** Use the flo
d7f30 63 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c  ck() system call
d7f40 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b   to do file lock
d7f50 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b  ing..**.** flock
d7f60 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69  () locking is li
d7f70 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b  ke dot-file lock
d7f80 69 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20  ing in that the 
d7f90 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d  various.** fine-
d7fa0 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65  grain locking le
d7fb0 76 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62  vels supported b
d7fc0 79 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c  y SQLite are col
d7fd0 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61  lapsed into.** a
d7fe0 20 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76   single exclusiv
d7ff0 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65  e lock.  In othe
d8000 72 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c  r words, SHARED,
d8010 20 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a   RESERVED, and.*
d8020 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20  * PENDING locks 
d8030 61 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69  are the same thi
d8040 6e 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49  ng as an EXCLUSI
d8050 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65  VE lock.  SQLite
d8060 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20  .** still works 
d8070 77 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73  when you do this
d8080 2c 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63  , but concurrenc
d8090 79 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e  y is reduced sin
d80a0 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e  ce.** only a sin
d80b0 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20  gle process can 
d80c0 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64  be reading the d
d80d0 61 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d  atabase at a tim
d80e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68  e..**.** Omit th
d80f0 69 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51  is section if SQ
d8100 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d8110 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72  ING_STYLE is tur
d8120 6e 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a  ned off or if.**
d8130 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56   compiling for V
d8140 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53  XWORKS..*/.#if S
d8150 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
d8160 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f  KING_STYLE && !O
d8170 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a  S_VXWORKS../*.**
d8180 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
d8190 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
d81a0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d81b0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
d81c0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
d81d0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
d81e0 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
d81f0 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
d8200 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75  eld, set *pResOu
d8210 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65  t.** to a non-ze
d8220 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69  ro value otherwi
d8230 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  se *pResOut is s
d8240 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65  et to zero.  The
d8250 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
d8260 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
d8270 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49  E_OK unless an I
d8280 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  /O error occurs 
d8290 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63  during lock chec
d82a0 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
d82b0 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65  int flockCheckRe
d82c0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
d82d0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d82e0 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
d82f0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d8300 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
d8310 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
d8320 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d8330 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69  ile*)id;.  .  Si
d8340 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d8350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d8360 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
d8370 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73  LOCK; );.  .  as
d8380 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
d8390 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
d83a0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
d83b0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
d83c0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
d83d0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d83e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
d83f0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
d8400 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
d8410 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
d8420 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
d8430 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a  cess holds it. *
d8440 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  /.  if( !reserve
d8450 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65  d ){.    /* atte
d8460 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c  mpt to get the l
d8470 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  ock */.    int l
d8480 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65  rc = flock(pFile
d8490 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c  ->h, LOCK_EX | L
d84a0 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28  OCK_NB);.    if(
d84b0 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f   !lrc ){.      /
d84c0 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20  * got the lock, 
d84d0 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20  unlock it */.   
d84e0 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70     lrc = flock(p
d84f0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e  File->h, LOCK_UN
d8500 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72  );.      if ( lr
d8510 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  c ) {.        in
d8520 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
d8530 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c  ;.        /* unl
d8540 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20  ock failed with 
d8550 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  an error */.    
d8560 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65      lrc = sqlite
d8570 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d8580 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d8590 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
d85a0 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ; .        if( I
d85b0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63  S_LOCK_ERROR(lrc
d85c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
d85d0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d85e0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d85f0 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20      rc = lrc;.  
d8600 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
d8610 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
d8620 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d8630 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73  errno;.      res
d8640 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  erved = 1;.     
d8650 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65   /* someone else
d8660 20 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72   might have it r
d8670 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20  eserved */.     
d8680 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72   lrc = sqliteErr
d8690 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d86a0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d86b0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20  IOERR_LOCK); .  
d86c0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d86d0 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20  ERROR(lrc) ){.  
d86e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d86f0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d8700 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72  .        rc = lr
d8710 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
d8720 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
d8730 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
d8740 20 25 64 20 25 64 20 28 66 6c 6f 63 6b 29 5c 6e   %d %d (flock)\n
d8750 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
d8760 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66   reserved);..#if
d8770 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  def SQLITE_IGNOR
d8780 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52  E_FLOCK_LOCK_ERR
d8790 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20  ORS.  if( (rc & 
d87a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d  SQLITE_IOERR) ==
d87b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b   SQLITE_IOERR ){
d87c0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d87d0 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65  _OK;.    reserve
d87e0 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  d=1;.  }.#endif 
d87f0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d8800 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d8810 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74  RS */.  *pResOut
d8820 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72   = reserved;.  r
d8830 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d8840 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
d8850 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
d8860 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
d8870 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
d8880 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
d8890 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
d88a0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
d88b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
d88c0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
d88d0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
d88e0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
d88f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
d8900 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
d8910 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
d8920 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
d8930 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
d8940 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
d8950 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
d8960 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
d8970 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
d8980 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
d8990 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
d89a0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
d89b0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
d89c0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
d89d0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
d89e0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
d89f0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
d8a00 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
d8a10 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
d8a20 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
d8a30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
d8a40 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
d8a50 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
d8a60 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
d8a70 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
d8a80 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
d8a90 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d8aa0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d8ab0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
d8ac0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
d8ad0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
d8ae0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
d8af0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c  *.** flock() onl
d8b00 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74  y really support
d8b10 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73   EXCLUSIVE locks
d8b20 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65  .  We track inte
d8b30 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b  rmediate.** lock
d8b40 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73   states in the s
d8b50 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75  qlite3_file stru
d8b60 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c  cture, but all l
d8b70 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a  ocks SHARED or.*
d8b80 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c  * above are real
d8b90 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ly EXCLUSIVE loc
d8ba0 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61  ks and exclude a
d8bb0 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ll other process
d8bc0 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73  es from.** acces
d8bd0 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  s the file..**.*
d8be0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
d8bf0 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
d8c00 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
d8c10 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
d8c20 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
d8c30 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
d8c40 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
d8c50 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63  tic int flockLoc
d8c60 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d8c70 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d8c80 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d8c90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78  QLITE_OK;.  unix
d8ca0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d8cb0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  nixFile*)id;..  
d8cc0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d8cd0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72  ..  /* if we alr
d8ce0 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b  eady have a lock
d8cf0 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76  , it is exclusiv
d8d00 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61  e.  .  ** Just a
d8d10 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20  djust level and 
d8d20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65  punt on outta he
d8d30 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  re. */.  if (pFi
d8d40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e  le->locktype > N
d8d50 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46  O_LOCK) {.    pF
d8d60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d8d70 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65  locktype;.    re
d8d80 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d8d90 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62    }.  .  /* grab
d8da0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
d8db0 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66  ck */.  .  if (f
d8dc0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d8dd0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
d8de0 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72  )) {.    int tEr
d8df0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d8e00 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20   /* didn't get, 
d8e10 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a  must be busy */.
d8e20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d8e30 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d8e40 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d8e50 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d8e60 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d8e70 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d8e80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d8e90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d8ea0 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20   }.  } else {.  
d8eb0 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
d8ec0 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
d8ed0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70  turn ok */.    p
d8ee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d8ef0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20   locktype;.  }. 
d8f00 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20   OSTRACE4("LOCK 
d8f10 20 20 20 25 64 20 25 73 20 25 73 20 28 66 6c 6f     %d %s %s (flo
d8f20 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ck)\n", pFile->h
d8f30 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c  , locktypeName(l
d8f40 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20  ocktype), .     
d8f50 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45        rc==SQLITE
d8f60 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  _OK ? "ok" : "fa
d8f70 69 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53  iled");.#ifdef S
d8f80 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d8f90 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20  CK_LOCK_ERRORS. 
d8fa0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54   if( (rc & SQLIT
d8fb0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
d8fc0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
d8fd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
d8fe0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
d8ff0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c  SQLITE_IGNORE_FL
d9000 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20  OCK_LOCK_ERRORS 
d9010 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
d9020 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  }.../*.** Lower 
d9030 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d9040 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
d9050 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
d9060 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
d9070 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
d9080 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
d9090 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
d90a0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
d90b0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
d90c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d90d0 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
d90e0 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
d90f0 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
d9100 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
d9110 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
d9120 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63  .static int floc
d9130 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
d9140 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d9150 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
d9160 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d9170 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a  nixFile*)id;.  .
d9180 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d9190 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55  );.  OSTRACE5("U
d91a0 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
d91b0 20 25 64 20 70 69 64 3d 25 64 20 28 66 6c 6f 63   %d pid=%d (floc
d91c0 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  k)\n", pFile->h,
d91d0 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
d91e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
d91f0 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29  ktype, getpid())
d9200 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
d9210 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  type<=SHARED_LOC
d9220 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d  K );.  .  /* no-
d9230 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a  op if possible *
d9240 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d9250 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70  ocktype==locktyp
d9260 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
d9270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
d9280 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61   .  /* shared ca
d9290 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65  n just be set be
d92a0 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20  cause we always 
d92b0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
d92c0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74  e */.  if (lockt
d92d0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d92e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
d92f0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d9300 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  pe;.    return S
d9310 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d9320 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79  .  /* no, really
d9330 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69  , unlock. */.  i
d9340 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46  nt rc = flock(pF
d9350 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29  ile->h, LOCK_UN)
d9360 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20  ;.  if (rc) {.  
d9370 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20    int r, tErrno 
d9380 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d  = errno;.    r =
d9390 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d93a0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d93b0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d93c0 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  UNLOCK);.    if(
d93d0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d93e0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ) ){.      pFile
d93f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d9400 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64  rrno;.    }.#ifd
d9410 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
d9420 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d9430 52 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20  RS.    if( (r & 
d9440 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d  SQLITE_IOERR) ==
d9450 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b   SQLITE_IOERR ){
d9460 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54  .      r = SQLIT
d9470 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65  E_BUSY;.    }.#e
d9480 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49  ndif /* SQLITE_I
d9490 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
d94a0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a  _ERRORS */.    .
d94b0 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20      return r;.  
d94c0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69  } else {.    pFi
d94d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
d94e0 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75  O_LOCK;.    retu
d94f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d9500 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  }.}../*.** Close
d9510 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
d9520 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73  ic int flockClos
d9530 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
d9540 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29  id) {.  if( id )
d9550 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63  {.    flockUnloc
d9560 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
d9570 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f    }.  return clo
d9580 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
d9590 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
d95a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d95b0 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f  NG_STYLE && !OS_
d95c0 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  VXWORK */../****
d95d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d95e0 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b  End of the flock
d95f0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   lock implementa
d9600 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
d9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d9620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a  *********/../***
d9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d96a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
d96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d96d0 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64  **** Begin Named
d96e0 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69   Semaphore Locki
d96f0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
d9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
d9710 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72  * Named semaphor
d9720 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c  e locking is onl
d9730 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56  y supported on V
d9740 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  xWorks..**.** Se
d9750 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
d9760 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b  is like dot-lock
d9770 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68   and flock in th
d9780 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c  at it really onl
d9790 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58  y.** supports EX
d97a0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e  CLUSIVE locking.
d97b0 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20    Only a single 
d97c0 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64  process can read
d97d0 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65   or write.** the
d97e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
d97f0 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20  t a time.  This 
d9800 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61  reduces potentia
d9810 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62  l concurrency, b
d9820 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20  ut.** makes the 
d9830 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
d9840 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e  ion much easier.
d9850 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  .*/.#if OS_VXWOR
d9860 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  KS../*.** This r
d9870 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
d9880 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
d9890 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
d98a0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
d98b0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
d98c0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
d98d0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
d98e0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65  lock is held, se
d98f0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f  t *pResOut.** to
d9900 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   a non-zero valu
d9910 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65  e otherwise *pRe
d9920 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a  sOut is set to z
d9930 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ero.  The return
d9940 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74   value.** is set
d9950 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e   to SQLITE_OK un
d9960 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f  less an I/O erro
d9970 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  r occurs during 
d9980 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  lock checking..*
d9990 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d  /.static int sem
d99a0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d99b0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d99c0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d99d0 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d99e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d99f0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d9a00 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d9a10 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d9a20 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
d9a30 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d9a40 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d9a50 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20  SERVEDLOCK; );. 
d9a60 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d9a70 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  e );..  /* Check
d9a80 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d9a90 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d9aa0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d9ab0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d9ac0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d9ad0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
d9ae0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
d9af0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
d9b00 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
d9b10 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
d9b20 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73  t. */.  if( !res
d9b30 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d  erved ){.    sem
d9b40 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65  _t *pSem = pFile
d9b50 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20  ->pOpen->pSem;. 
d9b60 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73     struct stat s
d9b70 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28  tatBuf;..    if(
d9b80 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65   sem_trywait(pSe
d9b90 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20  m)==-1 ){.      
d9ba0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d9bb0 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41  no;.      if( EA
d9bc0 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29  GAIN != tErrno )
d9bd0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
d9be0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d9bf0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d9c00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
d9c10 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29  ECKRESERVEDLOCK)
d9c20 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ;.        pFile-
d9c30 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d9c40 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  rno;.      } els
d9c50 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73  e {.        /* s
d9c60 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20  omeone else has 
d9c70 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65  the lock when we
d9c80 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20   are in NO_LOCK 
d9c90 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72  */.        reser
d9ca0 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f  ved = (pFile->lo
d9cb0 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f  cktype < SHARED_
d9cc0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  LOCK);.      }. 
d9cd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d9ce0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65  /* we could have
d9cf0 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69   it if we want i
d9d00 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70  t */.      sem_p
d9d10 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d  ost(pSem);.    }
d9d20 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
d9d30 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
d9d40 20 25 64 20 25 64 20 28 73 65 6d 29 5c 6e 22 2c   %d %d (sem)\n",
d9d50 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
d9d60 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52  eserved);..  *pR
d9d70 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64  esOut = reserved
d9d80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d9d90 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
d9da0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
d9db0 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
d9dc0 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
d9dd0 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
d9de0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
d9df0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
d9e00 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
d9e10 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
d9e20 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
d9e30 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
d9e40 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
d9e50 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
d9e60 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
d9e70 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
d9e80 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
d9e90 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
d9ea0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
d9eb0 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
d9ec0 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
d9ed0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
d9ee0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
d9ef0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
d9f00 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
d9f10 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
d9f20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
d9f30 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
d9f40 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
d9f50 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
d9f60 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
d9f70 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
d9f80 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
d9f90 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
d9fa0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
d9fb0 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
d9fc0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d9fd0 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
d9fe0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
d9ff0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
da000 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
da010 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
da020 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
da030 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
da040 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68  IVE.**.** Semaph
da050 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72  ore locks only r
da060 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58  eally support EX
da070 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20  CLUSIVE locks.  
da080 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65  We track interme
da090 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74  diate.** lock st
da0a0 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  ates in the sqli
da0b0 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75  te3_file structu
da0c0 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b  re, but all lock
da0d0 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61  s SHARED or.** a
da0e0 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20  bove are really 
da0f0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20  EXCLUSIVE locks 
da100 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20  and exclude all 
da110 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
da120 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74  from.** access t
da130 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  he file..**.** T
da140 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
da150 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
da160 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20   lock.  Use the 
da170 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
da180 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ).** routine to 
da190 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20  lower a locking 
da1a0 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  level..*/.static
da1b0 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 6c   int semLock(sql
da1c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
da1d0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
da1e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
da1f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
da200 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 65  ;.  int fd;.  se
da210 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c  m_t *pSem = pFil
da220 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a  e->pOpen->pSem;.
da230 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
da240 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77  E_OK;..  /* if w
da250 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61  e already have a
da260 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63   lock, it is exc
da270 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a  lusive.  .  ** J
da280 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c  ust adjust level
da290 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74   and punt on out
da2a0 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66  ta here. */.  if
da2b0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
da2c0 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20  e > NO_LOCK) {. 
da2d0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
da2e0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
da2f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
da300 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f  K;.    goto sem_
da310 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20  end_lock;.  }.  
da320 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70  .  /* lock semap
da330 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69  hore now but bai
da340 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61  l out when alrea
da350 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  dy locked. */.  
da360 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28  if( sem_trywait(
da370 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20  pSem)==-1 ){.   
da380 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
da390 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f  Y;.    goto sem_
da3a0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20  end_lock;.  }.. 
da3b0 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20   /* got it, set 
da3c0 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74  the type and ret
da3d0 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c  urn ok */.  pFil
da3e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
da3f0 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e  cktype;.. sem_en
da400 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 6e  d_lock:.  return
da410 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
da420 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
da430 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
da440 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
da450 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
da460 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
da470 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
da480 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
da490 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
da4a0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
da4b0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
da4c0 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
da4d0 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
da4e0 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
da4f0 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
da500 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
da510 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
da520 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  semUnlock(sqlite
da530 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
da540 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
da550 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
da560 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
da570 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70   sem_t *pSem = p
da580 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65  File->pOpen->pSe
da590 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  m;..  assert( pF
da5a0 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ile );.  assert(
da5b0 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41   pSem );.  OSTRA
da5c0 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20  CE5("UNLOCK  %d 
da5d0 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64  %d was %d pid=%d
da5e0 20 28 73 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65   (sem)\n", pFile
da5f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09  ->h, locktype,..
da600 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
da610 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  pe, getpid());. 
da620 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
da630 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
da640 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
da650 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
da660 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
da670 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29  type==locktype )
da680 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
da690 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
da6a0 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a   /* shared can j
da6b0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75  ust be set becau
da6c0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76  se we always hav
da6d0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a  e an exclusive *
da6e0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65  /.  if (locktype
da6f0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b  ==SHARED_LOCK) {
da700 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
da710 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
da720 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
da730 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
da740 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e  /* no, really un
da750 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20  lock. */.  if ( 
da760 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d  sem_post(pSem)==
da770 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72  -1 ) {.    int r
da780 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  c, tErrno = errn
da790 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  o;.    rc = sqli
da7a0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
da7b0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
da7c0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
da7d0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c  K);.    if( IS_L
da7e0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
da7f0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
da800 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
da810 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
da820 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46  rn rc; .  }.  pF
da830 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
da840 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72  NO_LOCK;.  retur
da850 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
da860 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66  /*. ** Close a f
da870 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20  ile.. */.static 
da880 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c  int semClose(sql
da890 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
da8a0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
da8b0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
da8c0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
da8d0 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28  ;.    semUnlock(
da8e0 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  id, NO_LOCK);.  
da8f0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
da900 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  );.    unixEnter
da910 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c  Mutex();.    rel
da920 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69  easeLockInfo(pFi
da930 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  le->pLock);.    
da940 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70  releaseOpenCnt(p
da950 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20  File->pOpen);.  
da960 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
da970 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69  ();.    closeUni
da980 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20  xFile(id);.  }. 
da990 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
da9a0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
da9b0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a  OS_VXWORKS */./*
da9c0 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68  .** Named semaph
da9d0 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f  ore locking is o
da9e0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  nly available on
da9f0 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a   VxWorks..**.***
daa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
daa10 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65   of the named se
daa20 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70  maphore lock imp
daa30 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
daa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
daa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
daaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
daae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
daaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dab00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
dab10 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a   AFP Locking ***
dab20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dab30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
dab40 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20  *.** AFP is the 
dab50 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f  Apple Filing Pro
dab60 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61  tocol.  AFP is a
dab70 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73   network filesys
dab80 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20  tem found.** on 
dab90 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20  Apple Macintosh 
daba0 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68  computers - both
dabb0 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a   OS9 and OSX..**
dabc0 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20  .** Third-party 
dabd0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
dabe0 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c  of AFP are avail
dabf0 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20  able.  But this 
dac00 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c  code here.** onl
dac10 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a  y works on OSX..
dac20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
dac30 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51  __APPLE__) && SQ
dac40 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
dac50 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20  ING_STYLE./*.** 
dac60 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  The afpLockingCo
dac70 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20  ntext structure 
dac80 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70  contains all afp
dac90 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73   lock specific s
daca0 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  tate.*/.typedef 
dacb0 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e  struct afpLockin
dacc0 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b  gContext afpLock
dacd0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75  ingContext;.stru
dace0 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  ct afpLockingCon
dacf0 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65  text {.  unsigne
dad00 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72  d long long shar
dad10 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20  edByte;.  const 
dad20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20  char *dbPath;   
dad30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
dad40 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69  e of the open fi
dad50 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74  le */.};..struct
dad60 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42   ByteRangeLockPB
dad70 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c  2.{.  unsigned l
dad80 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b  ong long offset;
dad90 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65          /* offse
dada0 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  t to first byte 
dadb0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73  to lock */.  uns
dadc0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
dadd0 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f  length;        /
dade0 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74  * nbr of bytes t
dadf0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  o lock */.  unsi
dae00 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72  gned long long r
dae10 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a  etRangeStart; /*
dae20 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65   nbr of 1st byte
dae30 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65   locked if succe
dae40 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67  ssful */.  unsig
dae50 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46  ned char unLockF
dae60 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lag;         /* 
dae70 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20  1 = unlock, 0 = 
dae80 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  lock */.  unsign
dae90 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64  ed char startEnd
daea0 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31  Flag;       /* 1
daeb0 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66  =rel to end of f
daec0 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74  ork, 0=rel to st
daed0 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b  art */.  int fd;
daee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
daef0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69             /* fi
daf00 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63  le desc to assoc
daf10 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20   this lock with 
daf20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61  */.};..#define a
daf30 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63  fpfsByteRangeLoc
daf40 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f  k2FSCTL        _
daf50 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74  IOWR('z', 23, st
daf60 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
daf70 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  ckPB2)../*.** Th
daf80 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20  is is a utility 
daf90 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63  for setting or c
dafa0 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61  learing a bit-ra
dafb0 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a  nge lock on an.*
dafc0 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d  * AFP filesystem
dafd0 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ..** .** Return 
dafe0 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
daff0 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53  cess, SQLITE_BUS
db000 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  Y on failure..*/
db010 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53  .static int afpS
db020 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20  etLock(.  const 
db030 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20  char *path,     
db040 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
db050 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20   of the file to 
db060 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c  be locked or unl
db070 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46  ocked */.  unixF
db080 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20  ile *pFile,     
db090 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
db0a0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
db0b0 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75  r on path */.  u
db0c0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
db0d0 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a  g offset,     /*
db0e0 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62   First byte to b
db0f0 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e  e locked */.  un
db100 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
db110 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20   length,     /* 
db120 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
db130 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  to lock */.  int
db140 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20   setLockFlag    
db150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
db160 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e  rue to set lock.
db170 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72    False to clear
db180 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74   lock */.){.  st
db190 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
db1a0 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20  ckPB2 pb;.  int 
db1b0 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c  err;.  .  pb.unL
db1c0 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63  ockFlag = setLoc
db1d0 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20  kFlag ? 0 : 1;. 
db1e0 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67   pb.startEndFlag
db1f0 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65   = 0;.  pb.offse
db200 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62  t = offset;.  pb
db210 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68  .length = length
db220 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69  ; .  pb.fd = pFi
db230 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52  le->h;.  .  OSTR
db240 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b  ACE6("AFPSETLOCK
db250 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69   [%s] for %d%s i
db260 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c  n range %llx:%ll
db270 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c  x\n", .    (setL
db280 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46  ockFlag?"ON":"OF
db290 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28  F"), pFile->h, (
db2a0 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74  pb.fd==-1?"[test
db2b0 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20  val-1]":""),.   
db2c0 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29   offset, length)
db2d0 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28  ;.  err = fsctl(
db2e0 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52  path, afpfsByteR
db2f0 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20  angeLock2FSCTL, 
db300 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20  &pb, 0);.  if ( 
db310 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20  err==-1 ) {.    
db320 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20  int rc;.    int 
db330 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
db340 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46      OSTRACE4("AF
db350 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20  PSETLOCK failed 
db360 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20  to fsctl() '%s' 
db370 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  %d %s\n",.      
db380 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72         path, tEr
db390 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45  rno, strerror(tE
db3a0 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53  rrno));.#ifdef S
db3b0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50  QLITE_IGNORE_AFP
db3c0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20  _LOCK_ERRORS.   
db3d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
db3e0 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20  Y;.#else.    rc 
db3f0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
db400 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
db410 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  no,.            
db420 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46          setLockF
db430 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45  lag ? SQLITE_IOE
db440 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45  RR_LOCK : SQLITE
db450 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
db460 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
db470 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b  _IGNORE_AFP_LOCK
db480 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69  _ERRORS */.    i
db490 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
db4a0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46  (rc) ){.      pF
db4b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
db4c0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20   tErrno;.    }. 
db4d0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
db4e0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74  } else {.    ret
db4f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
db500 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
db510 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
db520 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
db530 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
db540 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
db550 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
db560 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
db570 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
db580 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
db590 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20  set *pResOut.** 
db5a0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  to a non-zero va
db5b0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70  lue otherwise *p
db5c0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
db5d0 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75   zero.  The retu
db5e0 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73  rn value.** is s
db5f0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  et to SQLITE_OK 
db600 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72  unless an I/O er
db610 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
db620 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e  g lock checking.
db630 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
db640 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  fpCheckReservedL
db650 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
db660 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
db670 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ut){.  int rc = 
db680 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
db690 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
db6a0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
db6b0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
db6c0 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49  ;.  .  SimulateI
db6d0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
db6e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
db6f0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
db700 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
db710 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63  File );.  afpLoc
db720 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
db730 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
db740 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
db750 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
db760 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63  xt;.  .  /* Chec
db770 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
db780 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
db790 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
db7a0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
db7b0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
db7c0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
db7d0 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rved = 1;.  }.  
db7e0 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
db7f0 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
db800 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
db810 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20  it..   */.  if( 
db820 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
db830 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53   /* lock the RES
db840 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20  ERVED byte */.  
db850 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53    int lrc = afpS
db860 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
db870 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
db880 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
db890 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51  1);  .    if( SQ
db8a0 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a  LITE_OK==lrc ){.
db8b0 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73        /* if we s
db8c0 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69  ucceeded in taki
db8d0 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20  ng the reserved 
db8e0 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20  lock, unlock it 
db8f0 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20  to restore.     
db900 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   ** the original
db910 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20   state */.      
db920 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  lrc = afpSetLock
db930 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
db940 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45  , pFile, RESERVE
db950 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  D_BYTE, 1, 0);. 
db960 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
db970 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65    /* if we faile
db980 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63  d to get the loc
db990 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65  k then someone e
db9a0 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74  lse must have it
db9b0 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76   */.      reserv
db9c0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
db9d0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
db9e0 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20  ROR(lrc) ){.    
db9f0 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a    rc=lrc;.    }.
dba00 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45    }.  .  OSTRACE
dba10 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
dba20 25 64 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e  %d %d %d (afp)\n
dba30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
dba40 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20   reserved);.  . 
dba50 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
dba60 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
dba70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  c;.}../*.** Lock
dba80 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
dba90 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
dbaa0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
dbab0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
dbac0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
dbad0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
dbae0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
dbaf0 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
dbb00 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
dbb10 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
dbb20 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
dbb30 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
dbb40 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
dbb50 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
dbb60 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
dbb70 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
dbb80 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
dbb90 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
dbba0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
dbbb0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
dbbc0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
dbbd0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
dbbe0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
dbbf0 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
dbc00 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
dbc10 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
dbc20 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
dbc30 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
dbc40 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
dbc50 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
dbc60 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
dbc70 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
dbc80 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
dbc90 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
dbca0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
dbcb0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
dbcc0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
dbcd0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
dbce0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
dbcf0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
dbd00 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
dbd10 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
dbd20 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
dbd30 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
dbd40 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
dbd50 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73  lock.  Use the s
dbd60 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
dbd70 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c  .** routine to l
dbd80 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c  ower a locking l
dbd90 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  evel..*/.static 
dbda0 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69  int afpLock(sqli
dbdb0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
dbdc0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
dbdd0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
dbde0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
dbdf0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
dbe00 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69  *)id;.  afpLocki
dbe10 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  ngContext *conte
dbe20 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67  xt = (afpLocking
dbe30 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65  Context *) pFile
dbe40 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
dbe50 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
dbe60 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
dbe70 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E5("LOCK    %d %
dbe80 73 20 77 61 73 20 25 73 20 70 69 64 3d 25 64 20  s was %s pid=%d 
dbe90 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  (afp)\n", pFile-
dbea0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63  >h,.         loc
dbeb0 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
dbec0 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  pe), locktypeNam
dbed0 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  e(pFile->locktyp
dbee0 65 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  e), getpid());..
dbef0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
dbf00 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20   already a lock 
dbf10 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20  of this type or 
dbf20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65  more restrictive
dbf30 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69   on the.  ** uni
dbf40 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e  xFile, do nothin
dbf50 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65  g. Don't use the
dbf60 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65   afp_end_lock: e
dbf70 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
dbf80 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78  * unixEnterMutex
dbf90 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63  () hasn't been c
dbfa0 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a  alled yet..  */.
dbfb0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
dbfc0 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  ktype>=locktype 
dbfd0 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  ){.    OSTRACE3(
dbfe0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f  "LOCK    %d %s o
dbff0 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29  k (already held)
dc000 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65   (afp)\n", pFile
dc010 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
dc020 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
dc030 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74  ktype));.    ret
dc040 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
dc050 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
dc060 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73  re the locking s
dc070 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65  equence is corre
dc080 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ct.  */.  assert
dc090 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
dc0a0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f  e!=NO_LOCK || lo
dc0b0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
dc0c0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
dc0d0 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49   locktype!=PENDI
dc0e0 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  NG_LOCK );.  ass
dc0f0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52  ert( locktype!=R
dc100 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20  ESERVED_LOCK || 
dc110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
dc120 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
dc130 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74    .  /* This mut
dc140 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63  ex is needed bec
dc150 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ause pFile->pLoc
dc160 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f  k is shared acro
dc170 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a  ss threads.  */.
dc180 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
dc190 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ();..  /* Make s
dc1a0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ure the current 
dc1b0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20  thread owns the 
dc1c0 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pFile..  */.  rc
dc1d0 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72   = transferOwner
dc1e0 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69  ship(pFile);.  i
dc1f0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
dc200 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76   ){.    unixLeav
dc210 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65  eMutex();.    re
dc220 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20  turn rc;.  }.   
dc230 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47   .  /* A PENDING
dc240 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20   lock is needed 
dc250 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67  before acquiring
dc260 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61   a SHARED lock a
dc270 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61  nd before.  ** a
dc280 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c  cquiring an EXCL
dc290 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72  USIVE lock.  For
dc2a0 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b   the SHARED lock
dc2b0 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69  , the PENDING wi
dc2c0 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61  ll.  ** be relea
dc2d0 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sed..  */.  if( 
dc2e0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
dc2f0 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20  _LOCK .      || 
dc300 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  (locktype==EXCLU
dc310 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
dc320 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e  le->locktype<PEN
dc330 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a  DING_LOCK).  ){.
dc340 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a      int failed;.
dc350 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70      failed = afp
dc360 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
dc370 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
dc380 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c  PENDING_BYTE, 1,
dc390 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69   1);.    if (fai
dc3a0 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20  led) {.      rc 
dc3b0 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  = failed;.      
dc3c0 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63  goto afp_end_loc
dc3d0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  k;.    }.  }.  .
dc3e0 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
dc3f0 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69  gets to this poi
dc400 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c  nt, then actuall
dc410 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d  y go ahead and m
dc420 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  ake.  ** operati
dc430 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20  ng system calls 
dc440 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
dc450 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  d lock..  */.  i
dc460 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
dc470 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
dc480 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72  int lk, lrc1, lr
dc490 63 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72 63 31  c2;.    int lrc1
dc4a0 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 0a  Errno = 0;.    .
dc4b0 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74      /* Now get t
dc4c0 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41  he read-lock SHA
dc4d0 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20  RED_LOCK */.    
dc4e0 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 65  /* note that the
dc4f0 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 20   quality of the 
dc500 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e  randomness doesn
dc510 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 6d  't matter that m
dc520 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20  uch */.    lk = 
dc530 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63  random(); .    c
dc540 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79  ontext->sharedBy
dc550 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66  te = (lk & 0x7ff
dc560 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53  fffff)%(SHARED_S
dc570 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72  IZE - 1);.    lr
dc580 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  c1 = afpSetLock(
dc590 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
dc5a0 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20   pFile, .       
dc5b0 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b     SHARED_FIRST+
dc5c0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42  context->sharedB
dc5d0 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  yte, 1, 1);.    
dc5e0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
dc5f0 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 20  R(lrc1) ){.     
dc600 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 69   lrc1Errno = pFi
dc610 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20  le->lastErrno;. 
dc620 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70     }.    /* Drop
dc630 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50   the temporary P
dc640 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20  ENDING lock */. 
dc650 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 74     lrc2 = afpSet
dc660 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
dc670 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e  Path, pFile, PEN
dc680 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29  DING_BYTE, 1, 0)
dc690 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 49  ;.    .    if( I
dc6a0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63  S_LOCK_ERROR(lrc
dc6b0 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69  1) ) {.      pFi
dc6c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
dc6d0 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20  lrc1Errno;.     
dc6e0 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20   rc = lrc1;.    
dc6f0 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c    goto afp_end_l
dc700 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  ock;.    } else 
dc710 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
dc720 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20  R(lrc2) ){.     
dc730 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20   rc = lrc2;.    
dc740 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c    goto afp_end_l
dc750 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  ock;.    } else 
dc760 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49  if( lrc1 != SQLI
dc770 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20  TE_OK ) {.      
dc780 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d  rc = lrc1;.    }
dc790 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46   else {.      pF
dc7a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
dc7b0 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20  SHARED_LOCK;.   
dc7c0 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d     pFile->pOpen-
dc7d0 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a  >nLock++;.    }.
dc7e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
dc7f0 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20  The request was 
dc800 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f  for a RESERVED o
dc810 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
dc820 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20  .  It is.    ** 
dc830 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
dc840 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f  re is a SHARED o
dc850 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f  r greater lock o
dc860 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  n the file.    *
dc870 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a  * already..    *
dc880 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64  /.    int failed
dc890 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
dc8a0 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ( 0!=pFile->lock
dc8b0 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28  type );.    if (
dc8c0 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45  locktype >= RESE
dc8d0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69  RVED_LOCK && pFi
dc8e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52  le->locktype < R
dc8f0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a  ESERVED_LOCK) {.
dc900 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69          /* Acqui
dc910 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  re a RESERVED lo
dc920 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61  ck */.        fa
dc930 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63  iled = afpSetLoc
dc940 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
dc950 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
dc960 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20  ED_BYTE, 1,1);. 
dc970 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61     }.    if (!fa
dc980 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65  iled && locktype
dc990 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f   == EXCLUSIVE_LO
dc9a0 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41  CK) {.      /* A
dc9b0 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53  cquire an EXCLUS
dc9c0 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  IVE lock */.    
dc9d0 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65      .      /* Re
dc9e0 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20  move the shared 
dc9f0 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69  lock before tryi
dca00 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77  ng the range.  w
dca10 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20  e'll need to .  
dca20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69      ** reestabli
dca30 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f  sh the shared lo
dca40 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67  ck if we can't g
dca50 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63  et the  afpUnloc
dca60 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  k.      */.     
dca70 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d 20   if( !(failed = 
dca80 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  afpSetLock(conte
dca90 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  xt->dbPath, pFil
dcaa0 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  e, SHARED_FIRST 
dcab0 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +.              
dcac0 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65             conte
dcad0 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20  xt->sharedByte, 
dcae0 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 20  1, 0)) ){.      
dcaf0 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d 20    int failed2 = 
dcb00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
dcb10 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d     /* now attemm
dcb20 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 78  pt to get the ex
dcb30 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e  clusive lock ran
dcb40 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61  ge */.        fa
dcb50 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63  iled = afpSetLoc
dcb60 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
dcb70 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44  h, pFile, SHARED
dcb80 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 20  _FIRST, .       
dcb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcba0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53          SHARED_S
dcbb0 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  IZE, 1);.       
dcbc0 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 28   if( failed && (
dcbd0 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 74  failed2 = afpSet
dcbe0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
dcbf0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20  Path, pFile, .  
dcc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcc10 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53       SHARED_FIRS
dcc20 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61  T + context->sha
dcc30 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 20  redByte, 1, 1)) 
dcc40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
dcc50 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 73  Can't reestablis
dcc60 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
dcc70 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 74  k.  Sqlite can't
dcc80 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a 20   deal, this is. 
dcc90 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 72           ** a cr
dcca0 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72  itical I/O error
dccb0 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
dccc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 66          rc = ((f
dccd0 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f 49  ailed & SQLITE_I
dcce0 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f  OERR) == SQLITE_
dccf0 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 32  IOERR) ? failed2
dcd00 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   : .            
dcd10 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f     SQLITE_IOERR_
dcd20 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 20  LOCK;.          
dcd30 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63  goto afp_end_loc
dcd40 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20  k;.        } .  
dcd50 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
dcd60 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 20     rc = failed; 
dcd70 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
dcd80 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 7b     if( failed ){
dcd90 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c  .      rc = fail
dcda0 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ed;.    }.  }.  
dcdb0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
dcdc0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c  E_OK ){.    pFil
dcdd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
dcde0 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20  cktype;.  }else 
dcdf0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58  if( locktype==EX
dce00 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a  CLUSIVE_LOCK ){.
dce10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
dce20 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
dce30 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65  CK;.  }.  .afp_e
dce40 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c  nd_lock:.  unixL
dce50 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
dce60 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20  STRACE4("LOCK   
dce70 20 25 64 20 25 73 20 25 73 20 28 61 66 70 29 5c   %d %s %s (afp)\
dce80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
dce90 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74  cktypeName(lockt
dcea0 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ype), .         
dceb0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
dcec0 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
dced0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
dcee0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
dcef0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
dcf00 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
dcf10 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
dcf20 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
dcf30 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
dcf40 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
dcf50 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
dcf60 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
dcf70 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
dcf80 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
dcf90 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
dcfa0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
dcfb0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
dcfc0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
dcfd0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
dcfe0 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c  tatic int afpUnl
dcff0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
dd000 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
dd010 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pe) {.  int rc =
dd020 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e   SQLITE_OK;.  un
dd030 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
dd040 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
dd050 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
dd060 78 74 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c  xt *pCtx = (afpL
dd070 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
dd080 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43   pFile->lockingC
dd090 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72  ontext;..  asser
dd0a0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
dd0b0 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20  TRACE5("UNLOCK  
dd0c0 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64  %d %d was %d pid
dd0d0 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20 70 46  =%d (afp)\n", pF
dd0e0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
dd0f0 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  ,.         pFile
dd100 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70  ->locktype, getp
dd110 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74  id());..  assert
dd120 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
dd130 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28  ED_LOCK );.  if(
dd140 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dd150 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  <=locktype ){.  
dd160 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
dd170 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48  OK;.  }.  if( CH
dd180 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69  ECK_THREADID(pFi
dd190 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  le) ){.    retur
dd1a0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
dd1b0 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72  .  }.  unixEnter
dd1c0 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70  Mutex();.  if( p
dd1d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  File->locktype>S
dd1e0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
dd1f0 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65    .    if( pFile
dd200 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ->locktype==EXCL
dd210 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
dd220 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c      rc = afpSetL
dd230 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68  ock(pCtx->dbPath
dd240 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  , pFile, SHARED_
dd250 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49  FIRST, SHARED_SI
dd260 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  ZE, 0);.      if
dd270 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
dd280 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  && locktype==SHA
dd290 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
dd2a0 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d 65      /* only re-e
dd2b0 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61  stablish the sha
dd2c0 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 65  red lock if nece
dd2d0 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 20  ssary */.       
dd2e0 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42   int sharedLockB
dd2f0 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52  yte = SHARED_FIR
dd300 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42  ST+pCtx->sharedB
dd310 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63 20  yte;.        rc 
dd320 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
dd330 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
dd340 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  , sharedLockByte
dd350 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  , 1, 1);.      }
dd360 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
dd370 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
dd380 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
dd390 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b  =PENDING_LOCK ){
dd3a0 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53  .      rc = afpS
dd3b0 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50  etLock(pCtx->dbP
dd3c0 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44  ath, pFile, PEND
dd3d0 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b  ING_BYTE, 1, 0);
dd3e0 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20  .    } .    if( 
dd3f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
dd400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dd410 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  >=RESERVED_LOCK 
dd420 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66  ){.      rc = af
dd430 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64  pSetLock(pCtx->d
dd440 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45  bPath, pFile, RE
dd450 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20  SERVED_BYTE, 1, 
dd460 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  0);.    }.  }els
dd470 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d  e if( locktype==
dd480 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f  NO_LOCK ){.    /
dd490 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61 72  * clear the shar
dd4a0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69  ed lock */.    i
dd4b0 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  nt sharedLockByt
dd4c0 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  e = SHARED_FIRST
dd4d0 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74  +pCtx->sharedByt
dd4e0 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70 53  e;.    rc = afpS
dd4f0 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50  etLock(pCtx->dbP
dd500 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72  ath, pFile, shar
dd510 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30  edLockByte, 1, 0
dd520 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
dd530 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
dd540 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d     if( locktype=
dd550 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
dd560 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65    struct unixOpe
dd570 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46  nCnt *pOpen = pF
dd580 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20  ile->pOpen;.    
dd590 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d    pOpen->nLock--
dd5a0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
dd5b0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20  pOpen->nLock>=0 
dd5c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  );.      if( pOp
dd5d0 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a  en->nLock==0 ){.
dd5e0 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c 6f          rc = clo
dd5f0 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69  sePendingFds(pFi
dd600 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  le);.      }.   
dd610 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61   }.  }.  unixLea
dd620 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  veMutex();.  if(
dd630 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
dd640 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
dd650 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
dd660 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
dd670 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
dd680 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e  e a file & clean
dd690 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20  up AFP specific 
dd6a0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
dd6b0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
dd6c0 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  fpClose(sqlite3_
dd6d0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
dd6e0 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
dd6f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
dd700 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
dd710 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e   afpUnlock(id, N
dd720 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69  O_LOCK);.    uni
dd730 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
dd740 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f     if( pFile->pO
dd750 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f  pen && pFile->pO
dd760 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20  pen->nLock ){.  
dd770 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
dd780 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  are outstanding 
dd790 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63  locks, do not ac
dd7a0 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65  tually close the
dd7b0 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20   file just.     
dd7c0 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20   ** yet because 
dd7d0 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72  that would clear
dd7e0 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49   those locks.  I
dd7f0 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20  nstead, add the 
dd800 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65  file.      ** de
dd810 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65  scriptor to pOpe
dd820 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74  n->aPending.  It
dd830 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74   will be automat
dd840 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68  ically closed wh
dd850 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  en.      ** the 
dd860 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65  last lock is cle
dd870 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ared..      */. 
dd880 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46       setPendingF
dd890 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  d(pFile);.    }.
dd8a0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43      releaseOpenC
dd8b0 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29  nt(pFile->pOpen)
dd8c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
dd8d0 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  ee(pFile->lockin
dd8e0 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 63  gContext);.    c
dd8f0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
dd900 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  ;.    unixLeaveM
dd910 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65  utex();.  }.  re
dd920 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
dd930 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  }..#endif /* def
dd940 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
dd950 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
dd960 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a  _LOCKING_STYLE *
dd970 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  /./*.** The code
dd980 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41 46   above is the AF
dd990 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  P lock implement
dd9a0 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65  ation.  The code
dd9b0 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a 20   is specific.** 
dd9c0 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f  to MacOSX and do
dd9d0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f  es not work on o
dd9e0 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66 6f  ther unix platfo
dd9f0 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61  rms.  No alterna
dda00 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c  tive.** is avail
dda10 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 6f  able.  If you do
dda20 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20  n't compile for 
dda30 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 20  a mac, then the 
dda40 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46  "unix-afp".** VF
dda50 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  S is not availab
dda60 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  le..**.*********
dda70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
dda80 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63 6b   of the AFP lock
dda90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
ddaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddab0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
ddac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddb00 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  *****/.../******
ddb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddb50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
ddb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f  ********* Non-lo
ddb70 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69  cking sqlite3_fi
ddb80 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  le methods *****
ddb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddba0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
ddbb0 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f 6e  he next division
ddbc0 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d   contains implem
ddbd0 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c  entations for al
ddbe0 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65  l methods of the
ddbf0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c   .** sqlite3_fil
ddc00 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 74  e object other t
ddc10 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  han the locking 
ddc20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f  methods.  The lo
ddc30 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73  cking.** methods
ddc40 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 6e   were defined in
ddc50 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 65   divisions above
ddc60 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65   (one locking me
ddc70 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 69  thod per.** divi
ddc80 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65  sion).  Those me
ddc90 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 63  thods that are c
ddca0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63  ommon to all loc
ddcb0 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72  king modes.** ar
ddcc0 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68 65  e gather togethe
ddcd0 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76 69  r into this divi
ddce0 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  sion..*/../*.** 
ddcf0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73  Seek to the offs
ddd00 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  et passed as the
ddd10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
ddd20 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20  , then read cnt 
ddd30 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70  .** bytes into p
ddd40 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20  Buf. Return the 
ddd50 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
ddd60 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a  actually read..*
ddd70 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75  *.** NB:  If you
ddd80 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41   define USE_PREA
ddd90 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34  D or USE_PREAD64
ddda0 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
dddb0 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73  also.** be neces
dddc0 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f  sary to define _
dddd0 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20  XOPEN_SOURCE to 
ddde0 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61  be 500.  This va
dddf0 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65  ries from.** one
dde00 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68   system to anoth
dde10 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74  er.  Since SQLit
dde20 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e  e does not defin
dde30 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61  e USE_PREAD.** a
dde40 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64  ny any form by d
dde50 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20  efault, we will 
dde60 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64  not attempt to d
dde70 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55  efine _XOPEN_SOU
dde80 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b  RCE..** See tick
dde90 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32  ets #2741 and #2
ddea0 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76  681..**.** To av
ddeb0 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65  oid stomping the
ddec0 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20   errno value on 
dded0 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74 68  a failed read th
ddee0 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75  e lastErrno valu
ddef0 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f  e.** is set befo
ddf00 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f  re returning..*/
ddf10 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b  .static int seek
ddf20 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c 65  AndRead(unixFile
ddf30 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e   *id, sqlite3_in
ddf40 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 64  t64 offset, void
ddf50 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29   *pBuf, int cnt)
ddf60 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69  {.  int got;.  i
ddf70 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20  64 newOffset;.  
ddf80 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66  TIMER_START;.#if
ddf90 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45   defined(USE_PRE
ddfa0 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61  AD).  got = prea
ddfb0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  d(id->h, pBuf, c
ddfc0 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53  nt, offset);.  S
ddfd0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
ddfe0 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69  got = -1 );.#eli
ddff0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52  f defined(USE_PR
de000 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70  EAD64).  got = p
de010 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 42  read64(id->h, pB
de020 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29  uf, cnt, offset)
de030 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
de040 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b  ror( got = -1 );
de050 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73  .#else.  newOffs
de060 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68  et = lseek(id->h
de070 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53  , offset, SEEK_S
de080 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  ET);.  SimulateI
de090 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65  OError( newOffse
de0a0 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 77  t-- );.  if( new
de0b0 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29  Offset!=offset )
de0c0 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66  {.    if( newOff
de0d0 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20  set == -1 ){.   
de0e0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
de0f0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
de100 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65  errno;.    }else
de110 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69  {.      ((unixFi
de120 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
de130 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d  no = 0;....    }
de140 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a  .    return -1;.
de150 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 64    }.  got = read
de160 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
de170 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d  t);.#endif.  TIM
de180 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f  ER_END;.  if( go
de190 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69  t<0 ){.    ((uni
de1a0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
de1b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
de1c0 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22 52   }.  OSTRACE5("R
de1d0 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 20  EAD    %-3d %5d 
de1e0 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69  %7lld %llu\n", i
de1f0 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65  d->h, got, offse
de200 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44  t, TIMER_ELAPSED
de210 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b  );.  return got;
de220 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  .}../*.** Read d
de230 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20  ata from a file 
de240 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20  into a buffer.  
de250 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
de260 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73   if all.** bytes
de270 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65   were read succe
de280 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49  ssfully and SQLI
de290 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74  TE_IOERR if anyt
de2a0 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f  hing goes.** wro
de2b0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
de2c0 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71  t unixRead(.  sq
de2d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
de2e0 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a  .  void *pBuf, .
de2f0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c    int amt,.  sql
de300 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
de310 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  t.){.  unixFile 
de320 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
de330 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20 67  le *)id;.  int g
de340 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  ot;.  assert( id
de350 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   );..  /* If thi
de360 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20  s is a database 
de370 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72  file (not a jour
de380 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72  nal, master-jour
de390 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a  nal or temp.  **
de3a0 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65   file), the byte
de3b0 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67  s in the locking
de3c0 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65   range should ne
de3d0 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 77  ver be read or w
de3e0 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73  ritten. */.  ass
de3f0 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75  ert( pFile->pUnu
de400 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c  sed==0.       ||
de410 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47   offset>=PENDING
de420 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20  _BYTE+512.      
de430 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d   || offset+amt<=
de440 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20  PENDING_BYTE .  
de450 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65 6b  );..  got = seek
de460 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 6f  AndRead(pFile, o
de470 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74  ffset, pBuf, amt
de480 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d  );.  if( got==am
de490 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
de4a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
de4b0 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a  se if( got<0 ){.
de4c0 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f      /* lastErrno
de4d0 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 52   set by seekAndR
de4e0 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ead */.    retur
de4f0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  n SQLITE_IOERR_R
de500 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  EAD;.  }else{.  
de510 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
de520 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20 61  no = 0; /* not a
de530 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a 2f   system error */
de540 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
de550 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66  arts of the buff
de560 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  er must be zero-
de570 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65  filled */.    me
de580 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42  mset(&((char*)pB
de590 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74  uf)[got], 0, amt
de5a0 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72  -got);.    retur
de5b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
de5c0 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d  HORT_READ;.  }.}
de5d0 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20  ../*.** Seek to 
de5e0 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64  the offset in id
de5f0 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65  ->offset then re
de600 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74  ad cnt bytes int
de610 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72  o pBuf..** Retur
de620 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
de630 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
de640 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 65  ead.  Update the
de650 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 54   offset..**.** T
de660 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67  o avoid stomping
de670 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 65   the errno value
de680 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72 69   on a failed wri
de690 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f  te the lastErrno
de6a0 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74   value.** is set
de6b0 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
de6c0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
de6d0 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75 6e   seekAndWrite(un
de6e0 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  ixFile *id, i64 
de6f0 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76 6f  offset, const vo
de700 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e  id *pBuf, int cn
de710 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20  t){.  int got;. 
de720 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a   i64 newOffset;.
de730 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23    TIMER_START;.#
de740 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
de750 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 77  READ).  got = pw
de760 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66  rite(id->h, pBuf
de770 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , cnt, offset);.
de780 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 53  #elif defined(US
de790 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f 74  E_PREAD64).  got
de7a0 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d 3e   = pwrite64(id->
de7b0 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66  h, pBuf, cnt, of
de7c0 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20 6e  fset);.#else.  n
de7d0 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b  ewOffset = lseek
de7e0 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  (id->h, offset, 
de7f0 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66 28  SEEK_SET);.  if(
de800 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73   newOffset!=offs
de810 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65  et ){.    if( ne
de820 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b  wOffset == -1 ){
de830 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
de840 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e  e*)id)->lastErrn
de850 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d  o = errno;.    }
de860 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 6e  else{.      ((un
de870 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
de880 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20  tErrno = 0;.... 
de890 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
de8a0 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20  -1;.  }.  got = 
de8b0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75  write(id->h, pBu
de8c0 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  f, cnt);.#endif.
de8d0 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69    TIMER_END;.  i
de8e0 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20  f( got<0 ){.    
de8f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
de900 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
de910 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 41  no;.  }..  OSTRA
de920 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d 33  CE5("WRITE   %-3
de930 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75  d %5d %7lld %llu
de940 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c  \n", id->h, got,
de950 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45   offset, TIMER_E
de960 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72  LAPSED);.  retur
de970 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  n got;.}.../*.**
de980 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d   Write data from
de990 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61   a buffer into a
de9a0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53   file.  Return S
de9b0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
de9c0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f  ess.** or some o
de9d0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20  ther error code 
de9e0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73  on failure..*/.s
de9f0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 72  tatic int unixWr
dea00 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66  ite(.  sqlite3_f
dea10 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e 73  ile *id, .  cons
dea20 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20  t void *pBuf, . 
dea30 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69   int amt,.  sqli
dea40 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
dea50 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20   .){.  unixFile 
dea60 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
dea70 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 72  le*)id;.  int wr
dea80 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  ote = 0;.  asser
dea90 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72  t( id );.  asser
deaa0 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 2f  t( amt>0 );..  /
deab0 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64  * If this is a d
deac0 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f  atabase file (no
dead0 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73  t a journal, mas
deae0 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74  ter-journal or t
deaf0 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20  emp.  ** file), 
deb00 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65  the bytes in the
deb10 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73   locking range s
deb20 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72  hould never be r
deb30 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20  ead or written. 
deb40 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  */.  assert( pFi
deb50 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20  le->pUnused==0. 
deb60 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e        || offset>
deb70 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31  =PENDING_BYTE+51
deb80 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73  2.       || offs
deb90 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f  et+amt<=PENDING_
deba0 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66 6e  BYTE .  );..#ifn
debb0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20  def NDEBUG.  /* 
debc0 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20  If we are doing 
debd0 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 74  a normal write t
debe0 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  o a database fil
debf0 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f  e (as opposed to
dec00 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 6f  .  ** doing a ho
dec10 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
dec20 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 6f  ck or a write to
dec30 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 72   some file other
dec40 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f 72   than a.  ** nor
dec50 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  mal database fil
dec60 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 74  e) then record t
dec70 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
dec80 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68   database.  ** h
dec90 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20  as changed.  If 
deca0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
decb0 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 66  counter is modif
decc0 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 74  ied, record that
decd0 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e 0a  .  ** fact too..
dece0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
decf0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20  ->inNormalWrite 
ded00 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62  ){.    pFile->db
ded10 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20  Update = 1;  /* 
ded20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  The database has
ded30 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a   been modified *
ded40 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74  /.    if( offset
ded50 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61  <=24 && offset+a
ded60 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20  mt>=27 ){.      
ded70 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68  int rc;.      ch
ded80 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20  ar oldCntr[4];. 
ded90 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
deda0 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20  rrorBenign(1);. 
dedb0 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e       rc = seekAn
dedc0 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c  dRead(pFile, 24,
dedd0 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20   oldCntr, 4);.  
dede0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
dedf0 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20  rorBenign(0);.  
dee00 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c      if( rc!=4 ||
dee10 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c   memcmp(oldCntr,
dee20 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
dee30 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d  24-offset], 4)!=
dee40 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  0 ){.        pFi
dee50 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e  le->transCntrChn
dee60 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74  g = 1;  /* The t
dee70 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
dee80 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  er has changed *
dee90 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  /.      }.    }.
deea0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68    }.#endif..  wh
deeb0 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77  ile( amt>0 && (w
deec0 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72  rote = seekAndWr
deed0 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 65  ite(pFile, offse
deee0 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e 30  t, pBuf, amt))>0
deef0 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77   ){.    amt -= w
def00 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65 74  rote;.    offset
def10 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70   += wrote;.    p
def20 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70  Buf = &((char*)p
def30 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d  Buf)[wrote];.  }
def40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
def50 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c  or(( wrote=(-1),
def60 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d   amt=1 ));.  Sim
def70 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
def80 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d  or(( wrote=0, am
def90 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61 6d  t=1 ));.  if( am
defa0 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 77  t>0 ){.    if( w
defb0 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 20  rote<0 ){.      
defc0 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74  /* lastErrno set
defd0 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 65   by seekAndWrite
defe0 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
deff0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
df000 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ITE;.    }else{.
df010 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
df020 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e  tErrno = 0; /* n
df030 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f  ot a system erro
df040 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  r */.      retur
df050 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
df060 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
df070 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
df080 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
df090 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74  ST./*.** Count t
df0a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c  he number of ful
df0b0 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61  lsyncs and norma
df0c0 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69  l syncs.  This i
df0d0 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a  s used to test.*
df0e0 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64  * that syncs and
df0f0 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f   fullsyncs are o
df100 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 20  ccurring at the 
df110 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a  right times..*/.
df120 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
df130 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e  qlite3_sync_coun
df140 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  t = 0;.SQLITE_AP
df150 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75  I int sqlite3_fu
df160 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  llsync_count = 0
df170 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
df180 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 20  We do not trust 
df190 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 69  systems to provi
df1a0 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 61  de a working fda
df1b0 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 20  tasync().  Some 
df1c0 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 6f  do..** Others do
df1d0 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 65   no.  To be safe
df1e0 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b 20  , we will stick 
df1f0 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65 72  with the (slower
df200 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49 66  ) fsync()..** If
df210 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20 79   you know that y
df220 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73 20  our system does 
df230 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79 6e  support fdatasyn
df240 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a 2a  c() correctly,.*
df250 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63 6f  * then simply co
df260 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64 61  mpile with -Dfda
df270 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e 63  tasync=fdatasync
df280 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
df290 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20 21  (fdatasync) && !
df2a0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
df2b0 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61 74  _).# define fdat
df2c0 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e 64  async fsync.#end
df2d0 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65  if../*.** Define
df2e0 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
df2f0 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e 64  to 0 or 1 depend
df300 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  ing on whether o
df310 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f 46  r not.** the F_F
df320 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 69  ULLFSYNC macro i
df330 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 55  s defined.  F_FU
df340 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 65  LLFSYNC is curre
df350 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 61  ntly.** only ava
df360 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f 53  ilable on Mac OS
df370 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 6f   X.  But that co
df380 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 23  uld change..*/.#
df390 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 4e  ifdef F_FULLFSYN
df3a0 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f  C.# define HAVE_
df3b0 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c 73  FULLFSYNC 1.#els
df3c0 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f  e.# define HAVE_
df3d0 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e 64  FULLFSYNC 0.#end
df3e0 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  if.../*.** The f
df3f0 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 61  sync() system ca
df400 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  ll does not work
df410 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 6f   as advertised o
df420 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 73  n many.** unix s
df430 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f 6c  ystems.  The fol
df440 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 65  lowing procedure
df450 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74   is an attempt t
df460 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f 72  o make.** it wor
df470 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  k better..**.** 
df480 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  The SQLITE_NO_SY
df490 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c 65  NC macro disable
df4a0 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e 20  s all fsync()s. 
df4b0 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 0a   This is useful.
df4c0 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 77  ** for testing w
df4d0 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 72  hen we want to r
df4e0 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 74  un through the t
df4f0 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b 6c  est suite quickl
df500 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 74  y..** You are st
df510 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 2a  rongly advised *
df520 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 77  not* to deploy w
df530 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
df540 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 68  NC.** enabled, h
df550 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 69  owever, since wi
df560 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  th SQLITE_NO_SYN
df570 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f 53  C enabled, an OS
df580 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f 77   crash.** or pow
df590 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c 20  er failure will 
df5a0 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 74  likely corrupt t
df5b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
df5c0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73  ..**.** SQLite s
df5d0 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c 79  ets the dataOnly
df5e0 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 7a   flag if the siz
df5f0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  e of the file is
df600 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 54   unchanged..** T
df610 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 64  he idea behind d
df620 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 20  ataOnly is that 
df630 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 77  it should only w
df640 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 6f  rite the file co
df650 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73 6b  ntent.** to disk
df660 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65 2e  , not the inode.
df670 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64 61    We only set da
df680 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66 69  taOnly if the fi
df690 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20 75  le size is .** u
df6a0 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74  nchanged since t
df6b0 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20  he file size is 
df6c0 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f 64  part of the inod
df6d0 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a 2a  e.  However, .**
df6e0 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73 20   Ted Ts'o tells 
df6f0 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e  us that fdatasyn
df700 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77 72  c() will also wr
df710 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69 66  ite the inode if
df720 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69 7a   the.** file siz
df730 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20  e has changed.  
df740 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64 69  The only real di
df750 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
df760 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a 20   fdatasync().** 
df770 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65 64  and fsync(), Ted
df780 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74 68   tells us, is th
df790 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 77  at fdatasync() w
df7a0 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74 68  ill not flush th
df7b0 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74 68  e.** inode if th
df7c0 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65 72  e mtime or owner
df7d0 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65 20   or other inode 
df7e0 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65 20  attributes have 
df7f0 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20 6f  changed..** We o
df800 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20 74  nly care about t
df810 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e 6f  he file size, no
df820 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c 65  t the other file
df830 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f 0a   attributes, so.
df840 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51 4c  ** as far as SQL
df850 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65 64  ite is concerned
df860 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28 29  , an fdatasync()
df870 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71 75   is always adequ
df880 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20 61  ate..** So, we a
df890 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61 73  lways use fdatas
df8a0 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20 61  ync() if it is a
df8b0 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72 64  vailable, regard
df8c0 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20 76  less of.** the v
df8d0 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61  alue of the data
df8e0 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73 74  Only flag..*/.st
df8f0 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 73  atic int full_fs
df900 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ync(int fd, int 
df910 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 61  fullSync, int da
df920 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 72  taOnly){.  int r
df930 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  c;..  /* The fol
df940 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 6c  lowing "ifdef/el
df950 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b 20  if/else/" block 
df960 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 72  has the same str
df970 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 74  ucture as.  ** t
df980 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 74  he one below. It
df990 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 68   is replicated h
df9a0 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 76  ere solely to av
df9b0 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 0a  oid cluttering .
df9c0 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 6c    ** up the real
df9d0 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 55   code with the U
df9e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
df9f0 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a 23  ) macros..  */.#
dfa00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  ifdef SQLITE_NO_
dfa10 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41  SYNC.  UNUSED_PA
dfa20 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 55  RAMETER(fd);.  U
dfa30 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
dfa40 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e 55  fullSync);.  UNU
dfa50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 61  SED_PARAMETER(da
dfa60 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 48  taOnly);.#elif H
dfa70 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 20  AVE_FULLFSYNC.  
dfa80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
dfa90 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 73  (dataOnly);.#els
dfaa0 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  e.  UNUSED_PARAM
dfab0 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a  ETER(fullSync);.
dfac0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
dfad0 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65  ER(dataOnly);.#e
dfae0 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f 72  ndif..  /* Recor
dfaf0 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
dfb00 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f  times that we do
dfb10 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28   a normal fsync(
dfb20 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c 4c  ) and .  ** FULL
dfb30 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75  SYNC.  This is u
dfb40 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69  sed during testi
dfb50 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  ng to verify tha
dfb60 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  t this procedure
dfb70 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65  .  ** gets calle
dfb80 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65  d with the corre
dfb90 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20  ct arguments..  
dfba0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
dfbb0 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c 6c  _TEST.  if( full
dfbc0 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f 66  Sync ) sqlite3_f
dfbd0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b  ullsync_count++;
dfbe0 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f  .  sqlite3_sync_
dfbf0 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
dfc00 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70  .  /* If we comp
dfc10 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51  iled with the SQ
dfc20 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61  LITE_NO_SYNC fla
dfc30 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20  g, then syncing 
dfc40 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a  is a.  ** no-op.
dfc50 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
dfc60 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 20  TE_NO_SYNC.  rc 
dfc70 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c  = SQLITE_OK;.#el
dfc80 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  if HAVE_FULLFSYN
dfc90 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e 63  C.  if( fullSync
dfca0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63 6e   ){.    rc = fcn
dfcb0 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 59  tl(fd, F_FULLFSY
dfcc0 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  NC, 0);.  }else{
dfcd0 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 7d  .    rc = 1;.  }
dfce0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55 4c  .  /* If the FUL
dfcf0 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20 66  LFSYNC failed, f
dfd00 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74 65  all back to atte
dfd10 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63 28  mpting an fsync(
dfd20 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75 6c  )..  ** It shoul
dfd30 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c 65  dn't be possible
dfd40 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20 74   for fullfsync t
dfd50 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c 6f  o fail on the lo
dfd60 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 73  cal .  ** file s
dfd70 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c 20  ystem (on OSX), 
dfd80 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69 63  so failure indic
dfd90 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46 53  ates that FULLFS
dfda0 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20 73  YNC.  ** isn't s
dfdb0 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68 69  upported for thi
dfdc0 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 53  s file system. S
dfdd0 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66 73  o, attempt an fs
dfde0 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28 66  ync .  ** and (f
dfdf0 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20 74  or now) ignore t
dfe00 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 61  he overhead of a
dfe10 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63 6e   superfluous fcn
dfe20 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a 20  tl call.  .  ** 
dfe30 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20 74  It'd be better t
dfe40 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73 79  o detect fullfsy
dfe50 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65 20  nc support once 
dfe60 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a 20  and avoid .  ** 
dfe70 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20 65  the fcntl call e
dfe80 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20 69  very time sync i
dfe90 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a 20  s called..  */. 
dfea0 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20 66   if( rc ) rc = f
dfeb0 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73 65  sync(fd);..#else
dfec0 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73 79   .  rc = fdatasy
dfed0 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f 56  nc(fd);.#if OS_V
dfee0 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 3d  XWORKS.  if( rc=
dfef0 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45 4e  =-1 && errno==EN
dff00 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63 20  OTSUP ){.    rc 
dff10 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d  = fsync(fd);.  }
dff20 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
dff30 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66 20  WORKS */.#endif 
dff40 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /* ifdef SQLITE_
dff50 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41 56  NO_SYNC elif HAV
dff60 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 0a  E_FULLFSYNC */..
dff70 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b 53    if( OS_VXWORKS
dff80 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a 20   && rc!= -1 ){. 
dff90 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a 20     rc = 0;.  }. 
dffa0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
dffb0 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61  *.** Make sure a
dffc0 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70  ll writes to a p
dffd0 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61  articular file a
dffe0 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20  re committed to 
dfff0 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64  disk..**.** If d
e0000 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e 20  ataOnly==0 then 
e0010 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69 74  both the file it
e0020 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65 74  self and its met
e0030 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20 73  adata (file.** s
e0040 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d 65  ize, access time
e0050 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63 65  , etc) are synce
e0060 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79 21  d.  If dataOnly!
e0070 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65  =0 then only the
e0080 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69 73  .** file data is
e0090 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 55   synced..**.** U
e00a0 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f 20  nder Unix, also 
e00b0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
e00c0 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74  he directory ent
e00d0 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 0a  ry for the file.
e00e0 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ** has been crea
e00f0 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e 67  ted by fsync-ing
e0100 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74   the directory t
e0110 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
e0120 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 20   file..** If we 
e0130 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 61  do not do this a
e0140 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20  nd we encounter 
e0150 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c  a power failure,
e0160 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a 2a   the directory.*
e0170 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  * entry for the 
e0180 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f  journal might no
e0190 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77 65  t exist after we
e01a0 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e 65   reboot.  The ne
e01b0 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20  xt.** SQLite to 
e01c0 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 20  access the file 
e01d0 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 68  will not know th
e01e0 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 65  at the journal e
e01f0 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a 2a  xists (because.*
e0200 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  * the directory 
e0210 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a 6f  entry for the jo
e0220 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72 20  urnal was never 
e0230 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68 65  created) and the
e0240 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
e0250 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 61  will not roll ba
e0260 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c 65  ck - possibly le
e0270 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73  ading to databas
e0280 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f  e corruption..*/
e0290 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e02a0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
e02b0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73  e *id, int flags
e02c0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  ){.  int rc;.  u
e02d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
e02e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
e02f0 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e 6c  .  int isDataOnl
e0300 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49 54  y = (flags&SQLIT
e0310 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 29  E_SYNC_DATAONLY)
e0320 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73 79  ;.  int isFullsy
e0330 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30 46  nc = (flags&0x0F
e0340 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  )==SQLITE_SYNC_F
e0350 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ULL;..  /* Check
e0360 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c   that one of SQL
e0370 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20  ITE_SYNC_NORMAL 
e0380 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 73  or FULL was pass
e0390 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 28  ed */.  assert((
e03a0 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c  flags&0x0F)==SQL
e03b0 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0a  ITE_SYNC_NORMAL.
e03c0 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73 26        || (flags&
e03d0 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59  0x0F)==SQLITE_SY
e03e0 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 20  NC_FULL.  );..  
e03f0 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c 20  /* Unix cannot, 
e0400 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d 73  but some systems
e0410 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
e0420 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65 72  TE_FULL from her
e0430 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69 6e  e. This.  ** lin
e0440 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 61  e is to test tha
e0450 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73 20  t doing so does 
e0460 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70 72  not cause any pr
e0470 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 53  oblems..  */.  S
e0480 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
e0490 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
e04a0 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 61  ITE_FULL );..  a
e04b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
e04c0 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e 43    OSTRACE2("SYNC
e04d0 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69      %-3d\n", pFi
e04e0 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20 66  le->h);.  rc = f
e04f0 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d  ull_fsync(pFile-
e0500 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c 20  >h, isFullsync, 
e0510 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 53  isDataOnly);.  S
e0520 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e0530 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63  rc=1 );.  if( rc
e0540 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
e0550 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
e0560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
e0570 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b  ITE_IOERR_FSYNC;
e0580 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65  .  }.  if( pFile
e0590 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20  ->dirfd>=0 ){.  
e05a0 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 4f    int err;.    O
e05b0 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e 43  STRACE4("DIRSYNC
e05c0 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c 6c   %-3d (have_full
e05d0 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e  fsync=%d fullsyn
e05e0 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  c=%d)\n", pFile-
e05f0 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20 20  >dirfd,.        
e0600 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 59      HAVE_FULLFSY
e0610 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 3b  NC, isFullsync);
e0620 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e0630 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a  DISABLE_DIRSYNC.
e0640 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 63      /* The direc
e0650 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e 6c  tory sync is onl
e0660 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 66  y attempted if f
e0670 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20 20  ull_fsync is.   
e0680 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20 6f   ** turned off o
e0690 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 20  r unavailable.  
e06a0 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63 20  If a full_fsync 
e06b0 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c 0a  occurred above,.
e06c0 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20      ** then the 
e06d0 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69  directory sync i
e06e0 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a 20  s superfluous.. 
e06f0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21     */.    if( (!
e0700 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 7c  HAVE_FULLFSYNC |
e0710 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20 26  | !isFullsync) &
e0720 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69  & full_fsync(pFi
e0730 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20 29  le->dirfd,0,0) )
e0740 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20  {.       /*.    
e0750 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72 65     ** We have re
e0760 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65 20  ceived multiple 
e0770 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e 63  reports of fsync
e0780 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20 20  () returning.   
e0790 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77 68      ** errors wh
e07a0 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64 69  en applied to di
e07b0 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65 72  rectories on cer
e07c0 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65 6d  tain file system
e07d0 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20 66  s..       ** A f
e07e0 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79 20  ailed directory 
e07f0 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62 69  sync is not a bi
e0800 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20 73  g deal.  So it s
e0810 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20 62  eems.       ** b
e0820 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65 20  etter to ignore 
e0830 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63 6b  the error.  Tick
e0840 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20 20  et #1657.       
e0850 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46 69  */.       /* pFi
e0860 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
e0870 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 20  errno; */.      
e0880 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54   /* return SQLIT
e0890 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 20  E_IOERR; */.    
e08a0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72 72  }.#endif.    err
e08b0 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e   = close(pFile->
e08c0 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 20  dirfd); /* Only 
e08d0 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63  need to sync onc
e08e0 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 20  e, so close the 
e08f0 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d 3d  */.    if( err==
e0900 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
e0910 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20 77    /* directory w
e0920 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 20  hen we are done 
e0930 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  */.      pFile->
e0940 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20 20  dirfd = -1;.    
e0950 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69  }else{.      pFi
e0960 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
e0970 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
e0980 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  = SQLITE_IOERR_D
e0990 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d 0a  IR_CLOSE;.    }.
e09a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
e09b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  .}../*.** Trunca
e09c0 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  te an open file 
e09d0 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73  to a specified s
e09e0 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ize.*/.static in
e09f0 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 73  t unixTruncate(s
e0a00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
e0a10 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 69   i64 nByte){.  i
e0a20 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
e0a30 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74   id );.  Simulat
e0a40 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
e0a50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52   SQLITE_IOERR_TR
e0a60 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d  UNCATE );.  rc =
e0a70 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e 69   ftruncate(((uni
e0a80 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28  xFile*)id)->h, (
e0a90 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 20  off_t)nByte);.  
e0aa0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28 28  if( rc ){.    ((
e0ab0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
e0ac0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
e0ad0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
e0ae0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
e0af0 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66  TE;.  }else{.#if
e0b00 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
e0b10 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
e0b20 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74  ng a normal writ
e0b30 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
e0b40 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64  file (as opposed
e0b50 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67   to.    ** doing
e0b60 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72   a hot-journal r
e0b70 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69  ollback or a wri
e0b80 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20  te to some file 
e0b90 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 20  other than a.   
e0ba0 20 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62   ** normal datab
e0bb0 61 73 65 20 66 69 6c 65 29 20 61 6e 64 20 77 65  ase file) and we
e0bc0 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 66 69   truncate the fi
e0bd0 6c 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74  le to zero lengt
e0be0 68 2c 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 65  h,.    ** that e
e0bf0 66 66 65 63 74 69 76 65 6c 79 20 75 70 64 61 74  ffectively updat
e0c00 65 73 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f  es the change co
e0c10 75 6e 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67  unter.  This mig
e0c20 68 74 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a  ht happen.    **
e0c30 20 77 68 65 6e 20 72 65 73 74 6f 72 69 6e 67 20   when restoring 
e0c40 61 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67  a database using
e0c50 20 74 68 65 20 62 61 63 6b 75 70 20 41 50 49 20   the backup API 
e0c60 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 6c 65 6e 67  from a zero-leng
e0c70 74 68 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65  th.    ** source
e0c80 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
e0c90 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
e0ca0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20  ->inNormalWrite 
e0cb0 26 26 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20  && nByte==0 ){. 
e0cc0 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a       ((unixFile*
e0cd0 29 69 64 29 2d 3e 74 72 61 6e 73 43 6e 74 72 43  )id)->transCntrC
e0ce0 68 6e 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23  hng = 1;.    }.#
e0cf0 65 6e 64 69 66 0a 0a 20 20 20 20 72 65 74 75 72  endif..    retur
e0d00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
e0d10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d  .}../*.** Determ
e0d20 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ine the current 
e0d30 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69  size of a file i
e0d40 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69  n bytes.*/.stati
e0d50 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69  c int unixFileSi
e0d60 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
e0d70 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29  *id, i64 *pSize)
e0d80 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74  {.  int rc;.  st
e0d90 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20  ruct stat buf;. 
e0da0 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
e0db0 20 72 63 20 3d 20 66 73 74 61 74 28 28 28 75 6e   rc = fstat(((un
e0dc0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  ixFile*)id)->h, 
e0dd0 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 74  &buf);.  Simulat
e0de0 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29  eIOError( rc=1 )
e0df0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b  ;.  if( rc!=0 ){
e0e00 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a  .    ((unixFile*
e0e10 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  )id)->lastErrno 
e0e20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74  = errno;.    ret
e0e30 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e0e40 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70  _FSTAT;.  }.  *p
e0e50 53 69 7a 65 20 3d 20 62 75 66 2e 73 74 5f 73 69  Size = buf.st_si
e0e60 7a 65 3b 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 6f  ze;..  /* When o
e0e70 70 65 6e 69 6e 67 20 61 20 7a 65 72 6f 2d 73 69  pening a zero-si
e0e80 7a 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ze database, the
e0e90 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20   findLockInfo() 
e0ea0 70 72 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 77  procedure.  ** w
e0eb0 72 69 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62  rites a single b
e0ec0 79 74 65 20 69 6e 74 6f 20 74 68 61 74 20 66 69  yte into that fi
e0ed0 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77  le in order to w
e0ee0 6f 72 6b 20 61 72 6f 75 6e 64 20 61 20 62 75 67  ork around a bug
e0ef0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d  .  ** in the OS-
e0f00 58 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74  X msdos filesyst
e0f10 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f  em.  In order to
e0f20 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20   avoid problems 
e0f30 77 69 74 68 20 75 70 70 65 72 0a 20 20 2a 2a 20  with upper.  ** 
e0f40 6c 61 79 65 72 73 2c 20 77 65 20 6e 65 65 64 20  layers, we need 
e0f50 74 6f 20 72 65 70 6f 72 74 20 74 68 69 73 20 66  to report this f
e0f60 69 6c 65 20 73 69 7a 65 20 61 73 20 7a 65 72 6f  ile size as zero
e0f70 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20   even though it 
e0f80 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31  is.  ** really 1
e0f90 2e 20 20 20 54 69 63 6b 65 74 20 23 33 32 36 30  .   Ticket #3260
e0fa0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53  ..  */.  if( *pS
e0fb0 69 7a 65 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20  ize==1 ) *pSize 
e0fc0 3d 20 30 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20  = 0;...  return 
e0fd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
e0fe0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e0ff0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
e1000 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
e1010 5f 5f 29 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65  __)./*.** Handle
e1020 72 20 66 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b  r for proxy-lock
e1030 69 6e 67 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c  ing file-control
e1040 20 76 65 72 62 73 2e 20 20 44 65 66 69 6e 65 64   verbs.  Defined
e1050 20 62 65 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a   below in the.**
e1060 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e   proxying lockin
e1070 67 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73  g division..*/.s
e1080 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46  tatic int proxyF
e1090 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
e10a0 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69  e3_file*,int,voi
e10b0 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  d*);.#endif.../*
e10c0 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** Information 
e10d0 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61  and control of a
e10e0 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64  n open file hand
e10f0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
e1100 74 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f  t unixFileContro
e1110 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
e1120 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
e1130 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63   *pArg){.  switc
e1140 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
e1150 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
e1160 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20  OCKSTATE: {.    
e1170 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20    *(int*)pArg = 
e1180 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
e1190 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20  >locktype;.     
e11a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e11b0 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  K;.    }.    cas
e11c0 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52  e SQLITE_LAST_ER
e11d0 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69  RNO: {.      *(i
e11e0 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69  nt*)pArg = ((uni
e11f0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
e1200 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74  Errno;.      ret
e1210 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
e1220 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45     }.#ifndef NDE
e1230 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 65 20 70  BUG.    /* The p
e1240 61 67 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  ager calls this 
e1250 6d 65 74 68 6f 64 20 74 6f 20 73 69 67 6e 61 6c  method to signal
e1260 20 74 68 61 74 20 69 74 20 68 61 73 20 64 6f 6e   that it has don
e1270 65 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62  e.    ** a rollb
e1280 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65  ack and that the
e1290 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65   database is the
e12a0 72 65 66 6f 72 65 20 75 6e 63 68 61 6e 67 65 64  refore unchanged
e12b0 20 61 6e 64 0a 20 20 20 20 2a 2a 20 69 74 20 68   and.    ** it h
e12c0 65 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 66 6f  ence it is OK fo
e12d0 72 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  r the transactio
e12e0 6e 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  n change counter
e12f0 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e   to be.    ** un
e1300 63 68 61 6e 67 65 64 2e 0a 20 20 20 20 2a 2f 0a  changed..    */.
e1310 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
e1320 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47  FCNTL_DB_UNCHANG
e1330 45 44 3a 20 7b 0a 20 20 20 20 20 20 28 28 75 6e  ED: {.      ((un
e1340 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 64 62 55  ixFile*)id)->dbU
e1350 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 20  pdate = 0;.     
e1360 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e1370 4b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  K;.    }.#endif.
e1380 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
e1390 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
e13a0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
e13b0 4c 45 5f 5f 29 0a 20 20 20 20 63 61 73 65 20 53  LE__).    case S
e13c0 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
e13d0 4f 58 59 46 49 4c 45 3a 0a 20 20 20 20 63 61 73  OXYFILE:.    cas
e13e0 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43  e SQLITE_GET_LOC
e13f0 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20  KPROXYFILE: {.  
e1400 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79      return proxy
e1410 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 6f  FileControl(id,o
e1420 70 2c 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 23  p,pArg);.    }.#
e1430 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
e1440 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e1450 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
e1460 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a 2f 0a 20 20  __APPLE__) */.  
e1470 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
e1480 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a  E_ERROR;.}../*.*
e1490 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63  * Return the sec
e14a0 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65  tor size in byte
e14b0 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79  s of the underly
e14c0 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65  ing block device
e14d0 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63   for.** the spec
e14e0 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73  ified file. This
e14f0 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79   is almost alway
e1500 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74  s 512 bytes, but
e1510 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65   may be.** large
e1520 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63  r for some devic
e1530 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  es..**.** SQLite
e1540 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68   code assumes th
e1550 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e  is function cann
e1560 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f  ot fail. It also
e1570 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a   assumes that.**
e1580 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72   if two files ar
e1590 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  e created in the
e15a0 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65   same file-syste
e15b0 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65  m directory (i.e
e15c0 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20  ..** a database 
e15d0 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20  and its journal 
e15e0 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73  file) that the s
e15f0 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20  ector size will 
e1600 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66  be the.** same f
e1610 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74  or both..*/.stat
e1620 69 63 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f  ic int unixSecto
e1630 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
e1640 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  le *NotUsed){.  
e1650 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e1660 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
e1670 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55  urn SQLITE_DEFAU
e1680 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a  LT_SECTOR_SIZE;.
e1690 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
e16a0 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72 61  the device chara
e16b0 63 74 65 72 69 73 74 69 63 73 20 66 6f 72 20 74  cteristics for t
e16c0 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  he file. This is
e16d0 20 61 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e   always 0 for un
e16e0 69 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ix..*/.static in
e16f0 74 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72  t unixDeviceChar
e1700 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69  acteristics(sqli
e1710 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65  te3_file *NotUse
e1720 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
e1730 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e1740 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
e1750 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20  /*.** Here ends 
e1760 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
e1770 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65  on of all sqlite
e1780 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 2e 0a  3_file methods..
e1790 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71  ********* End sq
e17b0 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 74 68 6f  lite3_file Metho
e17c0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
e17d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e17e0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e17f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1830 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 64  */../*.** This d
e1840 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  ivision contains
e1850 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20   definitions of 
e1860 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e1870 64 73 20 6f 62 6a 65 63 74 73 20 74 68 61 74 0a  ds objects that.
e1880 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72  ** implement var
e1890 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  ious file lockin
e18a0 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 49  g strategies.  I
e18b0 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  t also contains 
e18c0 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 6f  definitions.** o
e18d0 66 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  f "finder" funct
e18e0 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 65 72 2d  ions.  A finder-
e18f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
e1900 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 61   to locate the a
e1910 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 71  ppropriate.** sq
e1920 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e1930 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 20 70 61   object for a pa
e1940 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
e1950 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 41 70  e file.  The pAp
e1960 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c 64 20 6f  pData.** field o
e1970 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66  f the sqlite3_vf
e1980 73 20 56 46 53 20 6f 62 6a 65 63 74 73 20 61 72  s VFS objects ar
e1990 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  e initialized to
e19a0 20 62 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a   be pointers to.
e19b0 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 66  ** the correct f
e19c0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 66  inder-function f
e19d0 6f 72 20 74 68 61 74 20 56 46 53 2e 0a 2a 2a 0a  or that VFS..**.
e19e0 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 66  ** Most finder f
e19f0 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
e1a00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
e1a10 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f  ixed sqlite3_io_
e1a20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63  methods.** objec
e1a30 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e 74  t.  The only int
e1a40 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65 72 2d  eresting finder-
e1a50 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 6f  function is auto
e1a60 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 68  lockIoFinder, wh
e1a70 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74 20  ich.** looks at 
e1a80 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74  the filesystem t
e1a90 79 70 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f  ype and tries to
e1aa0 20 67 75 65 73 73 20 74 68 65 20 62 65 73 74 20   guess the best 
e1ab0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 61 74  locking.** strat
e1ac0 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e 0a 2a  egy from that..*
e1ad0 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 65 72 2d  *.** For finder-
e1ae0 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77 6f 20 6f  funtion F, two o
e1af0 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 61 74  bjects are creat
e1b00 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29  ed:.**.**    (1)
e1b10 20 54 68 65 20 72 65 61 6c 20 66 69 6e 64 65 72   The real finder
e1b20 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20  -function named 
e1b30 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a 2a 2a  "FImpt()"..**.**
e1b40 20 20 20 20 28 32 29 20 41 20 63 6f 6e 73 74 61      (2) A consta
e1b50 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  nt pointer to th
e1b60 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  is function name
e1b70 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a 0a 2a  d just "F"..**.*
e1b80 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74  *.** A pointer t
e1b90 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65 72 20  o the F pointer 
e1ba0 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 70  is used as the p
e1bb0 41 70 70 44 61 74 61 20 76 61 6c 75 65 20 66 6f  AppData value fo
e1bc0 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 74 73  r VFS.** objects
e1bd0 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f  .  We have to do
e1be0 20 74 68 69 73 20 69 6e 73 74 65 61 64 20 6f 66   this instead of
e1bf0 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44 61 74   letting pAppDat
e1c00 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 65 63  a point.** direc
e1c10 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e 64 65  tly at the finde
e1c20 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65  r-function since
e1c30 20 43 39 30 20 72 75 6c 65 73 20 70 72 65 76 65   C90 rules preve
e1c40 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 66 72  nt a void*.** fr
e1c50 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74 6f 20  om be cast into 
e1c60 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  a function point
e1c70 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 61 63  er..**.**.** Eac
e1c80 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  h instance of th
e1c90 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 61 74  is macro generat
e1ca0 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 3a 0a  es two objects:.
e1cb0 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63 6f 6e  **.**   *  A con
e1cc0 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  stant sqlite3_io
e1cd0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
e1ce0 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68 61 74  call METHOD that
e1cf0 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20   has locking.** 
e1d00 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43 4c 4f       methods CLO
e1d10 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b  SE, LOCK, UNLOCK
e1d20 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a  , CKRESLOCK..**.
e1d30 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f 20 6d  **   *  An I/O m
e1d40 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e  ethod finder fun
e1d50 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46 49 4e  ction called FIN
e1d60 44 45 52 20 74 68 61 74 20 72 65 74 75 72 6e 73  DER that returns
e1d70 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20   a pointer.**   
e1d80 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48 4f 44     to the METHOD
e1d90 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 70   object in the p
e1da0 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74 2e 0a  revious bullet..
e1db0 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d 45 54  */.#define IOMET
e1dc0 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d 45 54  HODS(FINDER, MET
e1dd0 48 4f 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b  HOD, CLOSE, LOCK
e1de0 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f 43 4b  , UNLOCK, CKLOCK
e1df0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
e1e00 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  \.static const s
e1e10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e1e20 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20 20 20  s METHOD = {    
e1e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e1e50 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  .   1,          
e1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e70 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20  /* iVersion */  
e1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e1ea0 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20 20 20     CLOSE,       
e1eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e1ec0 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20  * xClose */     
e1ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e1ef0 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20    unixRead,     
e1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1f10 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20   xRead */       
e1f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e1f40 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20   unixWrite,     
e1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1f60 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20  xWrite */       
e1f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1f80 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e1f90 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 20 20  unixTruncate,   
e1fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e1fb0 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 20 20  Truncate */     
e1fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1fd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e1fe0 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 20 20  nixSync,        
e1ff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
e2000 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ync */          
e2010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2020 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e           \.   un
e2030 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20  ixFileSize,     
e2040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
e2050 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20  leSize */       
e2060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2070 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c 4f 43          \.   LOC
e2080 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K,              
e2090 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
e20a0 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  k */            
e20b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e20c0 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e 4c 4f         \.   UNLO
e20d0 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  CK,             
e20e0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
e20f0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ck */           
e2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2110 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c 4f 43        \.   CKLOC
e2120 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K,              
e2130 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b         /* xCheck
e2140 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 20  ReservedLock */ 
e2150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2160 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69       \.   unixFi
e2170 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20  leControl,      
e2180 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f        /* xFileCo
e2190 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20  ntrol */        
e21a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e21b0 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 65 63      \.   unixSec
e21c0 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20  torSize,        
e21d0 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53       /* xSectorS
e21e0 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ize */          
e21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2200 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65 76 69     \.   unixDevi
e2210 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
e2220 73 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 61  s   /* xDeviceCa
e2230 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20 20 20  pabilities */   
e2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2250 20 20 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20    \.};          
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e22a0 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20   \.static const 
e22b0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e22c0 64 73 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c  ds *FINDER##Impl
e22d0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
e22e0 75 6e 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20  unixFile *p){   
e22f0 5c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  \.  UNUSED_PARAM
e2300 45 54 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f  ETER(z); UNUSED_
e2310 50 41 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20  PARAMETER(p);   
e2320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e2340 0a 20 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f  .  return &METHO
e2350 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D;              
e2360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e2390 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
e23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e23b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e23c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e23d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73               \.s
e23e0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
e23f0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
e2400 28 2a 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28  (*const FINDER)(
e2410 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78  const char*,unix
e2420 46 69 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20  File *p)    \.  
e2430 20 20 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c    = FINDER##Impl
e2440 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72  ;../*.** Here ar
e2450 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c  e all of the sql
e2460 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e2470 6f 62 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68  objects for each
e2480 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69   of the.** locki
e2490 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 20  ng strategies.  
e24a0 46 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72  Functions that r
e24b0 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74  eturn pointers t
e24c0 6f 20 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a  o these methods.
e24d0 2a 2a 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61  ** are also crea
e24e0 74 65 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44  ted..*/.IOMETHOD
e24f0 53 28 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64  S(.  posixIoFind
e2500 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  er,            /
e2510 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
e2520 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69  n name */.  posi
e2530 78 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  xIoMethods,     
e2540 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
e2550 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e2560 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 6e 69  ct name */.  uni
e2570 78 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  xClose,         
e2580 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
e2590 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69   method */.  uni
e25a0 78 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  xLock,          
e25b0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e25c0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78  method */.  unix
e25d0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e25e0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e25f0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69   method */.  uni
e2600 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e2610 63 6b 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  ck     /* xCheck
e2620 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
e2630 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f  hod */.).IOMETHO
e2640 44 53 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69  DS(.  nolockIoFi
e2650 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  nder,           
e2660 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e2670 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c  on name */.  nol
e2680 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  ockIoMethods,   
e2690 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e26a0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e26b0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f  ect name */.  no
e26c0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20  lockClose,      
e26d0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e26e0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f  e method */.  no
e26f0 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20  lockLock,       
e2700 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e2710 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c   method */.  nol
e2720 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  ockUnlock,      
e2730 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e2740 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f  k method */.  no
e2750 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65  lockCheckReserve
e2760 64 4c 6f 63 6b 20 20 20 2f 2a 20 78 43 68 65 63  dLock   /* xChec
e2770 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e2780 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48  thod */.).IOMETH
e2790 4f 44 53 28 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f  ODS(.  dotlockIo
e27a0 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20  Finder,         
e27b0 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74   /* Finder funct
e27c0 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f  ion name */.  do
e27d0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20  tlockIoMethods, 
e27e0 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
e27f0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e2800 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 64  ject name */.  d
e2810 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20  otlockClose,    
e2820 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
e2830 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64  se method */.  d
e2840 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20  otlockLock,     
e2850 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
e2860 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f  k method */.  do
e2870 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20  tlockUnlock,    
e2880 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
e2890 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64  ck method */.  d
e28a0 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72  otlockCheckReser
e28b0 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68 65  vedLock  /* xChe
e28c0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
e28d0 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66 20  ethod */.)..#if 
e28e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
e28f0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21  CKING_STYLE && !
e2900 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54  OS_VXWORKS.IOMET
e2910 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f 46  HODS(.  flockIoF
e2920 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20  inder,          
e2930 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63    /* Finder func
e2940 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 66  tion name */.  f
e2950 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20  lockIoMethods,  
e2960 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
e2970 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e2980 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20  bject name */.  
e2990 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20  flockClose,     
e29a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
e29b0 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ose method */.  
e29c0 66 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20  flockLock,      
e29d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
e29e0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66  ck method */.  f
e29f0 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20  lockUnlock,     
e2a00 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
e2a10 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
e2a20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76  flockCheckReserv
e2a30 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68  edLock    /* xCh
e2a40 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
e2a50 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64  method */.).#end
e2a60 69 66 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  if..#if OS_VXWOR
e2a70 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  KS.IOMETHODS(.  
e2a80 73 65 6d 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  semIoFinder,    
e2a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
e2aa0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
e2ab0 65 20 2a 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68  e */.  semIoMeth
e2ac0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ods,            
e2ad0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
e2ae0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
e2af0 6d 65 20 2a 2f 0a 20 20 73 65 6d 43 6c 6f 73 65  me */.  semClose
e2b00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2b10 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
e2b20 6f 64 20 2a 2f 0a 20 20 73 65 6d 4c 6f 63 6b 2c  od */.  semLock,
e2b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2b40 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
e2b50 64 20 2a 2f 0a 20 20 73 65 6d 55 6e 6c 6f 63 6b  d */.  semUnlock
e2b60 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2b70 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
e2b80 6f 64 20 2a 2f 0a 20 20 73 65 6d 43 68 65 63 6b  od */.  semCheck
e2b90 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
e2ba0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
e2bb0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
e2bc0 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  /.).#endif..#if 
e2bd0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
e2be0 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
e2bf0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2c00 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 61  E.IOMETHODS(.  a
e2c10 66 70 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20  fpIoFinder,     
e2c20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
e2c30 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
e2c40 20 2a 2f 0a 20 20 61 66 70 49 6f 4d 65 74 68 6f   */.  afpIoMetho
e2c50 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ds,             
e2c60 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
e2c70 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
e2c80 65 20 2a 2f 0a 20 20 61 66 70 43 6c 6f 73 65 2c  e */.  afpClose,
e2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2ca0 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
e2cb0 64 20 2a 2f 0a 20 20 61 66 70 4c 6f 63 6b 2c 20  d */.  afpLock, 
e2cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2cd0 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64   /* xLock method
e2ce0 20 2a 2f 0a 20 20 61 66 70 55 6e 6c 6f 63 6b 2c   */.  afpUnlock,
e2cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2d00 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
e2d10 64 20 2a 2f 0a 20 20 61 66 70 43 68 65 63 6b 52  d */.  afpCheckR
e2d20 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20  eservedLock     
e2d30 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
e2d40 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  edLock method */
e2d50 0a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .).#endif../*.**
e2d60 20 54 68 65 20 22 57 68 6f 6c 65 20 46 69 6c 65   The "Whole File
e2d70 20 4c 6f 63 6b 69 6e 67 22 20 66 69 6e 64 65 72   Locking" finder
e2d80 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
e2d90 65 20 73 65 74 20 6f 66 20 6d 65 74 68 6f 64 73  e set of methods
e2da0 20 61 73 0a 2a 2a 20 74 68 65 20 70 6f 73 69 78   as.** the posix
e2db0 20 6c 6f 63 6b 69 6e 67 20 66 69 6e 64 65 72 2e   locking finder.
e2dc0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 73 65    But it also se
e2dd0 74 73 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48  ts the SQLITE_WH
e2de0 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47  OLE_FILE_LOCKING
e2df0 0a 2a 2a 20 66 6c 61 67 20 74 6f 20 66 6f 72 63  .** flag to forc
e2e00 65 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69  e the posix advi
e2e10 73 6f 72 79 20 6c 6f 63 6b 73 20 74 6f 20 63 6f  sory locks to co
e2e20 76 65 72 20 74 68 65 20 77 68 6f 6c 65 20 66 69  ver the whole fi
e2e30 6c 65 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66  le instead.** of
e2e40 20 6a 75 73 74 20 61 20 73 6d 61 6c 6c 20 73 70   just a small sp
e2e50 61 6e 20 6f 66 20 62 79 74 65 73 20 6e 65 61 72  an of bytes near
e2e60 20 74 68 65 20 31 47 69 42 20 62 6f 75 6e 64 61   the 1GiB bounda
e2e70 72 79 2e 20 20 57 68 6f 6c 65 20 46 69 6c 65 20  ry.  Whole File 
e2e80 4c 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 75 73  Locking.** is us
e2e90 65 66 75 6c 20 6f 6e 20 4e 46 53 2d 6d 6f 75 6e  eful on NFS-moun
e2ea0 74 65 64 20 66 69 6c 65 73 20 73 69 6e 63 65 20  ted files since 
e2eb0 69 74 20 68 65 6c 70 73 20 4e 46 53 20 74 6f 20  it helps NFS to 
e2ec0 6d 61 69 6e 74 61 69 6e 20 63 61 63 68 65 0a 2a  maintain cache.*
e2ed0 2a 20 63 6f 68 65 72 65 6e 63 79 2e 20 20 42 75  * coherency.  Bu
e2ee0 74 20 69 74 20 69 73 20 61 20 64 65 74 72 69 6d  t it is a detrim
e2ef0 65 6e 74 20 74 6f 20 6f 74 68 65 72 20 66 69 6c  ent to other fil
e2f00 65 73 79 73 74 65 6d 73 20 73 69 6e 63 65 20 69  esystems since i
e2f10 74 20 72 75 6e 73 0a 2a 2a 20 73 6c 6f 77 65 72  t runs.** slower
e2f20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
e2f30 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e2f40 68 6f 64 73 20 2a 70 6f 73 69 78 57 66 6c 49 6f  hods *posixWflIo
e2f50 46 69 6e 64 65 72 49 6d 70 6c 28 63 6f 6e 73 74  FinderImpl(const
e2f60 20 63 68 61 72 2a 7a 2c 20 75 6e 69 78 46 69 6c   char*z, unixFil
e2f70 65 2a 70 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  e*p){.  UNUSED_P
e2f80 41 52 41 4d 45 54 45 52 28 7a 29 3b 0a 20 20 70  ARAMETER(z);.  p
e2f90 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20 53 51  ->fileFlags = SQ
e2fa0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
e2fb0 4c 4f 43 4b 49 4e 47 3b 0a 20 20 72 65 74 75 72  LOCKING;.  retur
e2fc0 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  n &posixIoMethod
e2fd0 73 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73  s;.}.static cons
e2fe0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e2ff0 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74  hods .  *(*const
e3000 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65   posixWflIoFinde
e3010 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  r)(const char*,u
e3020 6e 69 78 46 69 6c 65 20 2a 70 29 20 3d 20 70 6f  nixFile *p) = po
e3030 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d  sixWflIoFinderIm
e3040 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  pl;../*.** The p
e3050 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  roxy locking met
e3060 68 6f 64 20 69 73 20 61 20 22 73 75 70 65 72 2d  hod is a "super-
e3070 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73  method" in the s
e3080 65 6e 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20  ense that it.** 
e3090 6f 70 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20  opens secondary 
e30a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
e30b0 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61   for the conch a
e30c0 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e  nd lock files an
e30d0 64 0a 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f  d.** it uses pro
e30e0 78 79 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46  xy, dot-file, AF
e30f0 50 2c 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c  P, and flock() l
e3100 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f  ocking methods o
e3110 6e 20 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e  n those.** secon
e3120 64 61 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72  dary files.  For
e3130 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68   this reason, th
e3140 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20  e division that 
e3150 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72  implements.** pr
e3160 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c  oxy locking is l
e3170 6f 63 61 74 65 64 20 6d 75 63 68 20 66 75 72 74  ocated much furt
e3180 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20  her down in the 
e3190 66 69 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65  file.  But we ne
e31a0 65 64 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61  ed.** to go ahea
e31b0 64 20 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65  d and define the
e31c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e31d0 6f 64 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66  ods and finder f
e31e0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70  unction.** for p
e31f0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72  roxy locking her
e3200 65 2e 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72  e.  So we forwar
e3210 64 20 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f  d declare the I/
e3220 4f 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69  O methods..*/.#i
e3230 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
e3240 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
e3250 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e3260 59 4c 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70  YLE.static int p
e3270 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65  roxyClose(sqlite
e3280 33 5f 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63  3_file*);.static
e3290 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73   int proxyLock(s
e32a0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
e32b0 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70  t);.static int p
e32c0 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  roxyUnlock(sqlit
e32d0 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
e32e0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
e32f0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e3300 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  k(sqlite3_file*,
e3310 20 69 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44   int*);.IOMETHOD
e3320 53 28 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64  S(.  proxyIoFind
e3330 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  er,            /
e3340 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
e3350 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78  n name */.  prox
e3360 79 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  yIoMethods,     
e3370 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
e3380 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e3390 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f  ct name */.  pro
e33a0 78 79 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  xyClose,        
e33b0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
e33c0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f   method */.  pro
e33d0 78 79 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  xyLock,         
e33e0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e33f0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78  method */.  prox
e3400 79 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  yUnlock,        
e3410 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e3420 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f   method */.  pro
e3430 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  xyCheckReservedL
e3440 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  ock    /* xCheck
e3450 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
e3460 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
e3470 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
e3480 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
e3490 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e34a0 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54  G_STYLE./* .** T
e34b0 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e  his "finder" fun
e34c0 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ction attempts t
e34d0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
e34e0 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72  best locking str
e34f0 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68  ategy .** for th
e3500 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
e3510 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20  "filePath".  It 
e3520 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65  then returns the
e3530 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3540 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68  ods.** object th
e3550 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
e3560 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a  at strategy..**.
e3570 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d  ** This is for M
e3580 61 63 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73  acOSX only..*/.s
e3590 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
e35a0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
e35b0 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
e35c0 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68  Impl(.  const ch
e35d0 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20  ar *filePath,   
e35e0 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20   /* name of the 
e35f0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
e3600 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65  .  unixFile *pNe
e3610 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  w           /* o
e3620 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20  pen file object 
e3630 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
e3640 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74   file */.){.  st
e3650 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
e3660 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20  t Mapping {.    
e3670 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
e3680 65 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20  esystem;        
e3690 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73        /* Filesys
e36a0 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f  tem type name */
e36b0 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  .    const sqlit
e36c0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
e36d0 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70  Methods;   /* Ap
e36e0 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e  propriate lockin
e36f0 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20  g method */.  } 
e3700 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  aMap[] = {.    {
e3710 20 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69   "hfs",    &posi
e3720 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20  xIoMethods },.  
e3730 20 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70    { "ufs",    &p
e3740 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c  osixIoMethods },
e3750 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20  .    { "afpfs", 
e3760 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d   &afpIoMethods }
e3770 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ,.#ifdef SQLITE_
e3780 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49  ENABLE_AFP_LOCKI
e3790 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d  NG_SMB.    { "sm
e37a0 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74  bfs",  &afpIoMet
e37b0 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20  hods },.#else.  
e37c0 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66    { "smbfs",  &f
e37d0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c  lockIoMethods },
e37e0 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77  .#endif.    { "w
e37f0 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49  ebdav", &nolockI
e3800 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20  oMethods },.    
e3810 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20  { 0, 0 }.  };.  
e3820 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
e3830 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20  statfs fsInfo;. 
e3840 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
e3850 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21  ckInfo;..  if( !
e3860 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20  filePath ){.    
e3870 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d  /* If filePath==
e3880 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20  NULL that means 
e3890 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
e38a0 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20  ith a transient 
e38b0 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74  file.    ** that
e38c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
e38d0 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a  o be locked. */.
e38e0 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f      return &nolo
e38f0 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  ckIoMethods;.  }
e3900 0a 20 20 69 66 28 20 73 74 61 74 66 73 28 66 69  .  if( statfs(fi
e3910 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29  lePath, &fsInfo)
e3920 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66   != -1 ){.    if
e3930 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73  ( fsInfo.f_flags
e3940 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b   & MNT_RDONLY ){
e3950 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e  .      return &n
e3960 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a  olockIoMethods;.
e3970 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
e3980 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65  0; aMap[i].zFile
e3990 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20  system; i++){.  
e39a0 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 66      if( strcmp(f
e39b0 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61  sInfo.f_fstypena
e39c0 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c  me, aMap[i].zFil
e39d0 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20  esystem)==0 ){. 
e39e0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d         return aM
e39f0 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a  ap[i].pMethods;.
e3a00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e3a10 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20  }..  /* Default 
e3a20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61  case. Handles, a
e3a30 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22  mongst others, "
e3a40 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20  nfs"..  ** Test 
e3a50 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20  byte-range lock 
e3a60 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49  using fcntl(). I
e3a70 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65  f the call succe
e3a80 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d  eds, .  ** assum
e3a90 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d  e that the file-
e3aa0 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20  system supports 
e3ab0 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b  POSIX style lock
e3ac0 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49  s. .  */.  lockI
e3ad0 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20  nfo.l_len = 1;. 
e3ae0 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72   lockInfo.l_star
e3af0 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66  t = 0;.  lockInf
e3b00 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  o.l_whence = SEE
e3b10 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66  K_SET;.  lockInf
e3b20 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c  o.l_type = F_RDL
e3b30 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28  CK;.  if( fcntl(
e3b40 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b  pNew->h, F_GETLK
e3b50 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31  , &lockInfo)!=-1
e3b60 20 29 20 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 66   ) {.    pNew->f
e3b70 69 6c 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54  ileFlags = SQLIT
e3b80 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
e3b90 4b 49 4e 47 3b 0a 20 20 20 20 72 65 74 75 72 6e  KING;.    return
e3ba0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73   &posixIoMethods
e3bb0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
e3bc0 65 74 75 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f  eturn &dotlockIo
e3bd0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73  Methods;.  }.}.s
e3be0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
e3bf0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a  te3_io_methods .
e3c00 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c    *(*const autol
e3c10 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e  ockIoFinder)(con
e3c20 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c  st char*,unixFil
e3c30 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f  e*) = autolockIo
e3c40 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e  FinderImpl;..#en
e3c50 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f  dif /* defined(_
e3c60 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
e3c70 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e3c80 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66  NG_STYLE */..#if
e3c90 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53   OS_VXWORKS && S
e3ca0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e3cb0 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a  KING_STYLE./* .*
e3cc0 2a 20 54 68 69 73 20 22 66 69 6e 64 65 72 22 20  * This "finder" 
e3cd0 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  function attempt
e3ce0 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  s to determine t
e3cf0 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20  he best locking 
e3d00 73 74 72 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72  strategy .** for
e3d10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
e3d20 6c 65 20 22 66 69 6c 65 50 61 74 68 22 2e 20 20  le "filePath".  
e3d30 49 74 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20  It then returns 
e3d40 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
e3d50 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74  ethods.** object
e3d60 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
e3d70 20 74 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a   that strategy..
e3d80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 66 6f  **.** This is fo
e3d90 72 20 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a  r VXWorks only..
e3da0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
e3db0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e3dc0 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  ds *autolockIoFi
e3dd0 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f 6e 73  nderImpl(.  cons
e3de0 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68  t char *filePath
e3df0 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f 66 20  ,    /* name of 
e3e00 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
e3e10 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20  e */.  unixFile 
e3e20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 20 20  *pNew           
e3e30 2f 2a 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  /* the open file
e3e40 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20   object */.){.  
e3e50 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
e3e60 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66  kInfo;..  if( !f
e3e70 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f  ilePath ){.    /
e3e80 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e  * If filePath==N
e3e90 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77  ULL that means w
e3ea0 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
e3eb0 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66  th a transient f
e3ec0 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ile.    ** that 
e3ed0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
e3ee0 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20   be locked. */. 
e3ef0 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63     return &noloc
e3f00 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a  kIoMethods;.  }.
e3f10 0a 20 20 2f 2a 20 54 65 73 74 20 69 66 20 66 63  .  /* Test if fc
e3f20 6e 74 6c 28 29 20 69 73 20 73 75 70 70 6f 72 74  ntl() is support
e3f30 65 64 20 61 6e 64 20 75 73 65 20 50 4f 53 49 58  ed and use POSIX
e3f40 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20   style locks..  
e3f50 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c  ** Otherwise fal
e3f60 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 6e 61  l back to the na
e3f70 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6d 65  med semaphore me
e3f80 74 68 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63  thod..  */.  loc
e3f90 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b  kInfo.l_len = 1;
e3fa0 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74  .  lockInfo.l_st
e3fb0 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49  art = 0;.  lockI
e3fc0 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  nfo.l_whence = S
e3fd0 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49  EEK_SET;.  lockI
e3fe0 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52  nfo.l_type = F_R
e3ff0 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74  DLCK;.  if( fcnt
e4000 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54  l(pNew->h, F_GET
e4010 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d  LK, &lockInfo)!=
e4020 2d 31 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72  -1 ) {.    retur
e4030 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  n &posixIoMethod
e4040 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  s;.  }else{.    
e4050 72 65 74 75 72 6e 20 26 73 65 6d 49 6f 4d 65 74  return &semIoMet
e4060 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  hods;.  }.}.stat
e4070 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e4080 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a  _io_methods .  *
e4090 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b  (*const autolock
e40a0 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20  IoFinder)(const 
e40b0 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29  char*,unixFile*)
e40c0 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e   = autolockIoFin
e40d0 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66  derImpl;..#endif
e40e0 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26   /* OS_VXWORKS &
e40f0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
e4100 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
e4110 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72  ../*.** An abstr
e4120 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 70  act type for a p
e4130 6f 69 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d  ointer to a IO m
e4140 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e  ethod finder fun
e4150 63 74 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65  ction:.*/.typede
e4160 66 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  f const sqlite3_
e4170 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69  io_methods *(*fi
e4180 6e 64 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74  nder_type)(const
e4190 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a   char*,unixFile*
e41a0 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.../**********
e41b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e41c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e41d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e41e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e41f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e4200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
e4210 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68  sqlite3_vfs meth
e4220 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ods ************
e4230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4240 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69  .**.** This divi
e4250 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  sion contains th
e4260 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
e4270 20 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74   of methods on t
e4280 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66  he.** sqlite3_vf
e4290 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a  s object..*/../*
e42a0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
e42b0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
e42c0 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75  he unixFile stru
e42d0 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f  cture pointed to
e42e0 20 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74   by pId..*/.stat
e42f0 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69  ic int fillInUni
e4300 78 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  xFile(.  sqlite3
e4310 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
e4320 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
e4330 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69  fs object */.  i
e4340 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20  nt h,           
e4350 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66         /* Open f
e4360 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f  ile descriptor o
e4370 66 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65  f file being ope
e4380 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72  ned */.  int dir
e4390 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fd,             
e43a0 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69   /* Directory fi
e43b0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f  le descriptor */
e43c0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
e43d0 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72  *pId,      /* Wr
e43e0 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46  ite to the unixF
e43f0 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65  ile structure he
e4400 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  re */.  const ch
e4410 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
e4420 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
e4430 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64  ile being opened
e4440 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b   */.  int noLock
e4450 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e4460 20 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66   Omit locking if
e4470 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69   true */.  int i
e4480 73 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20  sDelete         
e4490 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20     /* Delete on 
e44a0 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f  close if true */
e44b0 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  .){.  const sqli
e44c0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
e44d0 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20  pLockingStyle;. 
e44e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20   unixFile *pNew 
e44f0 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49  = (unixFile *)pI
e4500 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
e4510 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
e4520 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d  rt( pNew->pLock=
e4530 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72  =NULL );.  asser
e4540 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d  t( pNew->pOpen==
e4550 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61  NULL );..  /* Pa
e4560 72 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65  rameter isDelete
e4570 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e   is only used on
e4580 20 76 78 77 6f 72 6b 73 2e 20 45 78 70 72 65 73   vxworks. Expres
e4590 73 20 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c  s this explicitl
e45a0 79 20 0a 20 20 2a 2a 20 68 65 72 65 20 74 6f 20  y .  ** here to 
e45b0 70 72 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72  prevent compiler
e45c0 20 77 61 72 6e 69 6e 67 73 20 61 62 6f 75 74 20   warnings about 
e45d0 75 6e 75 73 65 64 20 70 61 72 61 6d 65 74 65 72  unused parameter
e45e0 73 2e 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44  s..  */.  UNUSED
e45f0 5f 50 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c  _PARAMETER(isDel
e4600 65 74 65 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45  ete);..  OSTRACE
e4610 33 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20  3("OPEN    %-3d 
e4620 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e  %s\n", h, zFilen
e4630 61 6d 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77  ame);    .  pNew
e4640 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d  ->h = h;.  pNew-
e4650 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a  >dirfd = dirfd;.
e4660 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70    SET_THREADID(p
e4670 4e 65 77 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69  New);.  pNew->fi
e4680 6c 65 46 6c 61 67 73 20 3d 20 30 3b 0a 0a 23 69  leFlags = 0;..#i
e4690 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70  f OS_VXWORKS.  p
e46a0 4e 65 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72  New->pId = vxwor
e46b0 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69  ksFindFileId(zFi
e46c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  lename);.  if( p
e46d0 4e 65 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20  New->pId==0 ){. 
e46e0 20 20 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20     noLock = 1;. 
e46f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
e4700 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  OMEM;.  }.#endif
e4710 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29  ..  if( noLock )
e4720 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74  {.    pLockingSt
e4730 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d  yle = &nolockIoM
e4740 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b  ethods;.  }else{
e4750 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79  .    pLockingSty
e4760 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f  le = (**(finder_
e4770 74 79 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70  type*)pVfs->pApp
e4780 44 61 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c  Data)(zFilename,
e4790 20 70 4e 65 77 29 3b 0a 23 69 66 20 53 51 4c 49   pNew);.#if SQLI
e47a0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e47b0 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43  G_STYLE.    /* C
e47c0 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69  ache zFilename i
e47d0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  n the locking co
e47e0 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 20 64  ntext (AFP and d
e47f0 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29  otlock override)
e4800 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78   for.    ** prox
e4810 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e  yLock activation
e4820 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65   is possible (re
e4830 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 62 61  mote proxy is ba
e4840 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a  sed on db name).
e4850 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65      ** zFilename
e4860 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75   remains valid u
e4870 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f  ntil file is clo
e4880 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20  sed, to support 
e4890 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63  */.    pNew->loc
e48a0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76  kingContext = (v
e48b0 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a  oid*)zFilename;.
e48c0 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66  #endif.  }..  if
e48d0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
e48e0 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f  == &posixIoMetho
e48f0 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e  ds ){.    unixEn
e4900 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
e4910 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  rc = findLockInf
e4920 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70  o(pNew, &pNew->p
e4930 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70  Lock, &pNew->pOp
e4940 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  en);.    if( rc!
e4950 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e4960 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72      /* If an err
e4970 6f 72 20 6f 63 63 75 72 65 64 20 69 6e 20 66 69  or occured in fi
e4980 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2c 20 63 6c  ndLockInfo(), cl
e4990 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ose the file des
e49a0 63 72 69 70 74 6f 72 0a 20 20 20 20 20 20 2a 2a  criptor.      **
e49b0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65   immediately, be
e49c0 66 6f 72 65 20 72 65 6c 65 61 73 69 6e 67 20 74  fore releasing t
e49d0 68 65 20 6d 75 74 65 78 2e 20 66 69 6e 64 4c 6f  he mutex. findLo
e49e0 63 6b 49 6e 66 6f 28 29 20 6d 61 79 20 66 61 69  ckInfo() may fai
e49f0 6c 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 77  l.      ** in tw
e4a00 6f 20 73 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20  o scenarios:.   
e4a10 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
e4a20 20 28 61 29 20 41 20 63 61 6c 6c 20 74 6f 20 66   (a) A call to f
e4a30 73 74 61 74 28 29 20 66 61 69 6c 65 64 2e 0a 20  stat() failed.. 
e4a40 20 20 20 20 20 2a 2a 20 20 20 28 62 29 20 41 20       **   (b) A 
e4a50 6d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20  malloc failed.. 
e4a60 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
e4a70 20 53 63 65 6e 61 72 69 6f 20 28 62 29 20 6d 61   Scenario (b) ma
e4a80 79 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20  y only occur if 
e4a90 74 68 65 20 70 72 6f 63 65 73 73 20 69 73 20 68  the process is h
e4aa0 6f 6c 64 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a  olding no other.
e4ab0 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 64 65        ** file de
e4ac0 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f  scriptors open o
e4ad0 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e  n the same file.
e4ae0 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20 6f   If there were o
e4af0 74 68 65 72 20 66 69 6c 65 0a 20 20 20 20 20 20  ther file.      
e4b00 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f  ** descriptors o
e4b10 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65  n this file, the
e4b20 6e 20 6e 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c  n no malloc woul
e4b30 64 20 62 65 20 72 65 71 75 69 72 65 64 20 62 79  d be required by
e4b40 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e 64 4c 6f  .      ** findLo
e4b50 63 6b 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69  ckInfo(). If thi
e4b60 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 69  s is the case, i
e4b70 74 20 69 73 20 71 75 69 74 65 20 73 61 66 65 20  t is quite safe 
e4b80 74 6f 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a  to close.      *
e4b90 2a 20 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20  * handle h - as 
e4ba0 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  it is guaranteed
e4bb0 20 74 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c   that no posix l
e4bc0 6f 63 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c  ocks will be rel
e4bd0 65 61 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62  eased.      ** b
e4be0 79 20 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20  y doing so..    
e4bf0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66    **.      ** If
e4c00 20 73 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61   scenario (a) ca
e4c10 75 73 65 64 20 74 68 65 20 65 72 72 6f 72 20 74  used the error t
e4c20 68 65 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e  hen things are n
e4c30 6f 74 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a  ot so safe. The.
e4c40 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69        ** implici
e4c50 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72  t assumption her
e4c60 65 20 69 73 20 74 68 61 74 20 69 66 20 66 73 74  e is that if fst
e4c70 61 74 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e  at() fails, thin
e4c80 67 73 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20  gs are in.      
e4c90 2a 2a 20 73 75 63 68 20 62 61 64 20 73 68 61 70  ** such bad shap
e4ca0 65 20 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20  e that dropping 
e4cb0 61 20 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f  a lock or two do
e4cc0 65 73 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63  esn't matter muc
e4cd0 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
e4ce0 20 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20 20    close(h);.    
e4cf0 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a    h = -1;.    }.
e4d00 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
e4d10 65 78 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53  ex();.  }..#if S
e4d20 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e4d30 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
e4d40 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e4d50 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63  .  else if( pLoc
e4d60 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66  kingStyle == &af
e4d70 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  pIoMethods ){.  
e4d80 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67    /* AFP locking
e4d90 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70   uses the file p
e4da0 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20  ath so it needs 
e4db0 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69  to be included i
e4dc0 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70  n.    ** the afp
e4dd0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a  LockingContext..
e4de0 20 20 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f      */.    afpLo
e4df0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
e4e00 74 78 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f  tx;.    pNew->lo
e4e10 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70  ckingContext = p
e4e20 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Ctx = sqlite3_ma
e4e30 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43  lloc( sizeof(*pC
e4e40 74 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  tx) );.    if( p
e4e50 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ctx==0 ){.      
e4e60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
e4e70 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  M;.    }else{.  
e4e80 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65      /* NB: zFile
e4e90 6e 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20  name exists and 
e4ea0 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e  remains valid un
e4eb0 74 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20  til the file is 
e4ec0 63 6c 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20  closed.      ** 
e4ed0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71  according to req
e4ee0 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e  uirement F11141.
e4ef0 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e    So we do not n
e4f00 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20  eed to make a.  
e4f10 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74      ** copy of t
e4f20 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a  he filename. */.
e4f30 20 20 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61        pCtx->dbPa
e4f40 74 68 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a  th = zFilename;.
e4f50 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76        srandomdev
e4f60 28 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e  ();.      unixEn
e4f70 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
e4f80 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49    rc = findLockI
e4f90 6e 66 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20  nfo(pNew, NULL, 
e4fa0 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20  &pNew->pOpen);. 
e4fb0 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75       unixLeaveMu
e4fc0 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20  tex();        . 
e4fd0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
e4fe0 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63  .  else if( pLoc
e4ff0 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f  kingStyle == &do
e5000 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29  tlockIoMethods )
e5010 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65  {.    /* Dotfile
e5020 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68   locking uses th
e5030 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69  e file path so i
e5040 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e  t needs to be in
e5050 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a  cluded in.    **
e5060 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   the dotlockLock
e5070 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20  ingContext .    
e5080 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f  */.    char *zLo
e5090 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20  ckFile;.    int 
e50a0 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e  nFilename;.    n
e50b0 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29  Filename = (int)
e50c0 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65  strlen(zFilename
e50d0 29 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b  ) + 6;.    zLock
e50e0 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73  File = (char *)s
e50f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46  qlite3_malloc(nF
e5100 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66  ilename);.    if
e5110 28 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29  ( zLockFile==0 )
e5120 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
e5130 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
e5140 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
e5150 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69  te3_snprintf(nFi
e5160 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c  lename, zLockFil
e5170 65 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f  e, "%s" DOTLOCK_
e5180 53 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d  SUFFIX, zFilenam
e5190 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e  e);.    }.    pN
e51a0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e51b0 78 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a  xt = zLockFile;.
e51c0 20 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f    }..#if OS_VXWO
e51d0 52 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70  RKS.  else if( p
e51e0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20  LockingStyle == 
e51f0 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b  &semIoMethods ){
e5200 0a 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65  .    /* Named se
e5210 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
e5220 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
e5230 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
e5240 6f 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c  o be.    ** incl
e5250 75 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c  uded in the semL
e5260 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20  ockingContext.  
e5270 20 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74    */.    unixEnt
e5280 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72  erMutex();.    r
e5290 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  c = findLockInfo
e52a0 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c  (pNew, &pNew->pL
e52b0 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65  ock, &pNew->pOpe
e52c0 6e 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d  n);.    if( (rc=
e52d0 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28  =SQLITE_OK) && (
e52e0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65  pNew->pOpen->pSe
e52f0 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20  m==NULL) ){.    
e5300 20 20 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65    char *zSemName
e5310 20 3d 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e   = pNew->pOpen->
e5320 61 53 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20  aSemName;.      
e5330 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c  int n;.      sql
e5340 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41  ite3_snprintf(MA
e5350 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d  X_PATHNAME, zSem
e5360 4e 61 6d 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c  Name, "/%s.sem",
e5370 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
e5380 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49          pNew->pI
e5390 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  d->zCanonicalNam
e53a0 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e  e);.      for( n
e53b0 3d 31 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b  =1; zSemName[n];
e53c0 20 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69   n++ ).        i
e53d0 66 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d  f( zSemName[n]==
e53e0 27 2f 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e  '/' ) zSemName[n
e53f0 5d 20 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70  ] = '_';.      p
e5400 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d  New->pOpen->pSem
e5410 20 3d 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d   = sem_open(zSem
e5420 4e 61 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30  Name, O_CREAT, 0
e5430 36 36 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69  666, 1);.      i
e5440 66 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e  f( pNew->pOpen->
e5450 70 53 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c  pSem == SEM_FAIL
e5460 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ED ){.        rc
e5470 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e5480 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  .        pNew->p
e5490 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30  Open->aSemName[0
e54a0 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20  ] = '\0';.      
e54b0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78  }.    }.    unix
e54c0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
e54d0 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e  }.#endif.  .  pN
e54e0 65 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  ew->lastErrno = 
e54f0 30 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  0;.#if OS_VXWORK
e5500 53 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  S.  if( rc!=SQLI
e5510 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c  TE_OK ){.    unl
e5520 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ink(zFilename);.
e5530 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30      isDelete = 0
e5540 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73  ;.  }.  pNew->is
e5550 44 65 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74  Delete = isDelet
e5560 65 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  e;.#endif.  if( 
e5570 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e5580 0a 20 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d  .    if( dirfd>=
e5590 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29  0 ) close(dirfd)
e55a0 3b 20 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b  ; /* silent leak
e55b0 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64   if fail, alread
e55c0 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  y in error */.  
e55d0 20 20 69 66 28 20 68 3e 3d 30 20 29 20 63 6c 6f    if( h>=0 ) clo
e55e0 73 65 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  se(h);.  }else{.
e55f0 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f      pNew->pMetho
e5600 64 20 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c  d = pLockingStyl
e5610 65 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  e;.    OpenCount
e5620 65 72 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65  er(+1);.  }.  re
e5630 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
e5640 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65  * Open a file de
e5650 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20  scriptor to the 
e5660 64 69 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69  directory contai
e5670 6e 69 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e  ning file zFilen
e5680 61 6d 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65  ame..** If succe
e5690 73 73 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73  ssful, *pFd is s
e56a0 65 74 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64  et to the opened
e56b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e56c0 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f   and.** SQLITE_O
e56d0 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
e56e0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
e56f0 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45  s, either SQLITE
e5700 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c  _NOMEM.** or SQL
e5710 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20  ITE_CANTOPEN is 
e5720 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46  returned and *pF
e5730 64 20 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75  d is set to an u
e5740 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75  ndefined.** valu
e5750 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  e..**.** If SQLI
e5760 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
e5770 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  d, the caller is
e5780 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
e5790 20 63 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20   closing.** the 
e57a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e57b0 2a 70 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65  *pFd using close
e57c0 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
e57d0 74 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28  t openDirectory(
e57e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
e57f0 65 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29  ename, int *pFd)
e5800 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  {.  int ii;.  in
e5810 74 20 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61  t fd = -1;.  cha
e5820 72 20 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50  r zDirname[MAX_P
e5830 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73  ATHNAME+1];..  s
e5840 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
e5850 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44  MAX_PATHNAME, zD
e5860 69 72 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46  irname, "%s", zF
e5870 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28  ilename);.  for(
e5880 69 69 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a  ii=(int)strlen(z
e5890 44 69 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26  Dirname); ii>1 &
e58a0 26 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d  & zDirname[ii]!=
e58b0 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66  '/'; ii--);.  if
e58c0 28 20 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44  ( ii>0 ){.    zD
e58d0 69 72 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30  irname[ii] = '\0
e58e0 27 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e  ';.    fd = open
e58f0 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f  (zDirname, O_RDO
e5900 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29  NLY|O_BINARY, 0)
e5910 3b 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20  ;.    if( fd>=0 
e5920 29 7b 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f  ){.#ifdef FD_CLO
e5930 45 58 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c  EXEC.      fcntl
e5940 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63  (fd, F_SETFD, fc
e5950 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c  ntl(fd, F_GETFD,
e5960 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43   0) | FD_CLOEXEC
e5970 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
e5980 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49  OSTRACE3("OPENDI
e5990 52 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64  R %-3d %s\n", fd
e59a0 2c 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20  , zDirname);.   
e59b0 20 7d 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20   }.  }.  *pFd = 
e59c0 66 64 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64  fd;.  return (fd
e59d0 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51  >=0?SQLITE_OK:SQ
e59e0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a  LITE_CANTOPEN);.
e59f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
e5a00 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
e5a10 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20   name in zBuf.  
e5a20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c  zBuf must be all
e5a30 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65  ocated.** by the
e5a40 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73   calling process
e5a50 20 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67   and must be big
e5a60 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
e5a70 61 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73  at least.** pVfs
e5a80 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74  ->mxPathname byt
e5a90 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
e5aa0 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e  t getTempname(in
e5ab0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
e5ac0 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  uf){.  static co
e5ad0 6e 73 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73  nst char *azDirs
e5ae0 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20  [] = {.     0,. 
e5af0 20 20 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61      0,.     "/va
e5b00 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75  r/tmp",.     "/u
e5b10 73 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f  sr/tmp",.     "/
e5b20 74 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a  tmp",.     ".",.
e5b30 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
e5b40 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
e5b50 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20  r zChars[] =.   
e5b60 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e   "abcdefghijklmn
e5b70 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20  opqrstuvwxyz".  
e5b80 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d    "ABCDEFGHIJKLM
e5b90 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20  NOPQRSTUVWXYZ". 
e5ba0 20 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b     "0123456789";
e5bb0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
e5bc0 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73  i, j;.  struct s
e5bd0 74 61 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74  tat buf;.  const
e5be0 20 63 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e   char *zDir = ".
e5bf0 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64  ";..  /* It's od
e5c00 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e  d to simulate an
e5c10 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20   io-error here, 
e5c20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20  but really this 
e5c30 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69  is just.  ** usi
e5c40 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20  ng the io-error 
e5c50 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 74  infrastructure t
e5c60 6f 20 74 65 73 74 20 74 68 61 74 20 53 51 4c 69  o test that SQLi
e5c70 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a  te handles this.
e5c80 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61    ** function fa
e5c90 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53  iling. .  */.  S
e5ca0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e5cb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e5cc0 45 52 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73  ERR );..  azDirs
e5cd0 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65  [0] = sqlite3_te
e5ce0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20  mp_directory;.  
e5cf0 69 66 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69  if (NULL == azDi
e5d00 72 73 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44  rs[1]) {.    azD
e5d10 69 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28  irs[1] = getenv(
e5d20 22 54 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20  "TMPDIR");.  }. 
e5d30 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73   .  for(i=0; i<s
e5d40 69 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69  izeof(azDirs)/si
e5d50 7a 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b  zeof(azDirs[0]);
e5d60 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61   i++){.    if( a
e5d70 7a 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f  zDirs[i]==0 ) co
e5d80 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
e5d90 73 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20  stat(azDirs[i], 
e5da0 26 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65  &buf) ) continue
e5db0 3b 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44  ;.    if( !S_ISD
e5dc0 49 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20  IR(buf.st_mode) 
e5dd0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
e5de0 69 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72  if( access(azDir
e5df0 73 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74  s[i], 07) ) cont
e5e00 69 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d  inue;.    zDir =
e5e10 20 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20   azDirs[i];.    
e5e20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  break;.  }..  /*
e5e30 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
e5e40 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73  output buffer is
e5e50 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f   large enough fo
e5e60 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  r the temporary 
e5e70 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e  file .  ** name.
e5e80 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72   If it is not, r
e5e90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
e5ea0 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  OR..  */.  if( (
e5eb0 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73  strlen(zDir) + s
e5ec0 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d  trlen(SQLITE_TEM
e5ed0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b  P_FILE_PREFIX) +
e5ee0 20 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29   17) >= (size_t)
e5ef0 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75  nBuf ){.    retu
e5f00 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
e5f10 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20  .  }..  do{.    
e5f20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
e5f30 28 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20  (nBuf-17, zBuf, 
e5f40 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50  "%s/"SQLITE_TEMP
e5f50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44  _FILE_PREFIX, zD
e5f60 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e  ir);.    j = (in
e5f70 74 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a  t)strlen(zBuf);.
e5f80 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
e5f90 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66  omness(15, &zBuf
e5fa0 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  [j]);.    for(i=
e5fb0 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b  0; i<15; i++, j+
e5fc0 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a  +){.      zBuf[j
e5fd0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
e5fe0 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
e5ff0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
e6000 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
e6010 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b  .    }.    zBuf[
e6020 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65  j] = 0;.  }while
e6030 28 20 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29  ( access(zBuf,0)
e6040 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==0 );.  return 
e6050 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
e6060 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e6070 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
e6080 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
e6090 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  __)./*.** Routin
e60a0 65 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61  e to transform a
e60b0 20 75 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61   unixFile into a
e60c0 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75   proxy-locking u
e60d0 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c  nixFile..** Impl
e60e0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68  ementation in th
e60f0 65 20 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76  e proxy-lock div
e6100 69 73 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20  ision, but used 
e6110 62 79 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a  by unixOpen().**
e6120 20 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45   if SQLITE_PREFE
e6130 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20  R_PROXY_LOCKING 
e6140 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73  is defined..*/.s
e6150 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54  tatic int proxyT
e6160 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65  ransformUnixFile
e6170 28 75 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73  (unixFile*, cons
e6180 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66  t char*);.#endif
e6190 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66  ../*.** Search f
e61a0 6f 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c  or an unused fil
e61b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61  e descriptor tha
e61c0 74 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  t was opened on 
e61d0 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  the database .**
e61e0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75   file (not a jou
e61f0 72 6e 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a  rnal or master-j
e6200 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65  ournal file) ide
e6210 6e 74 69 66 69 65 64 20 62 79 20 70 61 74 68 6e  ntified by pathn
e6220 61 6d 65 0a 2a 2a 20 7a 50 61 74 68 20 77 69 74  ame.** zPath wit
e6230 68 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58 58  h SQLITE_OPEN_XX
e6240 58 20 66 6c 61 67 73 20 6d 61 74 63 68 69 6e 67  X flags matching
e6250 20 74 68 6f 73 65 20 70 61 73 73 65 64 20 61 73   those passed as
e6260 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61   the second.** a
e6270 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
e6280 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
e6290 53 75 63 68 20 61 20 66 69 6c 65 20 64 65 73 63  Such a file desc
e62a0 72 69 70 74 6f 72 20 6d 61 79 20 65 78 69 73 74  riptor may exist
e62b0 20 69 66 20 61 20 64 61 74 61 62 61 73 65 20 63   if a database c
e62c0 6f 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 6c  onnection was cl
e62d0 6f 73 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20  osed.** but the 
e62e0 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 20  associated file 
e62f0 64 65 73 63 72 69 70 74 6f 72 20 63 6f 75 6c 64  descriptor could
e6300 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 62   not be closed b
e6310 65 63 61 75 73 65 20 73 6f 6d 65 0a 2a 2a 20 6f  ecause some.** o
e6320 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69  ther file descri
e6330 70 74 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  ptor open on the
e6340 20 73 61 6d 65 20 66 69 6c 65 20 69 73 20 68 6f   same file is ho
e6350 6c 64 69 6e 67 20 61 20 66 69 6c 65 2d 6c 6f 63  lding a file-loc
e6360 6b 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63  k..** Refer to c
e6370 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 75  omments in the u
e6380 6e 69 78 43 6c 6f 73 65 28 29 20 66 75 6e 63 74  nixClose() funct
e6390 69 6f 6e 20 61 6e 64 20 74 68 65 20 6c 65 6e 67  ion and the leng
e63a0 74 68 79 20 63 6f 6d 6d 65 6e 74 0a 2a 2a 20 64  thy comment.** d
e63b0 65 73 63 72 69 62 69 6e 67 20 22 50 6f 73 69 78  escribing "Posix
e63c0 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e   Advisory Lockin
e63d0 67 22 20 61 74 20 74 68 65 20 73 74 61 72 74 20  g" at the start 
e63e0 6f 66 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72  of this file for
e63f0 20 0a 2a 2a 20 66 75 72 74 68 65 72 20 64 65 74   .** further det
e6400 61 69 6c 73 2e 20 41 6c 73 6f 2c 20 74 69 63 6b  ails. Also, tick
e6410 65 74 20 23 34 30 31 38 2e 0a 2a 2a 0a 2a 2a 20  et #4018..**.** 
e6420 49 66 20 61 20 73 75 69 74 61 62 6c 65 20 66 69  If a suitable fi
e6430 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
e6440 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 69 74 20   found, then it 
e6450 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
e6460 6e 6f 0a 2a 2a 20 73 75 63 68 20 66 69 6c 65 20  no.** such file 
e6470 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 6c 6f  descriptor is lo
e6480 63 61 74 65 64 2c 20 2d 31 20 69 73 20 72 65 74  cated, -1 is ret
e6490 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
e64a0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66   UnixUnusedFd *f
e64b0 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f  indReusableFd(co
e64c0 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
e64d0 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 55   int flags){.  U
e64e0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e  nixUnusedFd *pUn
e64f0 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  used = 0;..  /* 
e6500 44 6f 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f  Do not search fo
e6510 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65  r an unused file
e6520 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 76   descriptor on v
e6530 78 77 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63 61  xworks. Not beca
e6540 75 73 65 0a 20 20 2a 2a 20 76 78 77 6f 72 6b 73  use.  ** vxworks
e6550 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 6e 65 66   would not benef
e6560 69 74 20 66 72 6f 6d 20 74 68 65 20 63 68 61 6e  it from the chan
e6570 67 65 20 28 69 74 20 6d 69 67 68 74 2c 20 77 65  ge (it might, we
e6580 27 72 65 20 6e 6f 74 20 73 75 72 65 29 2c 0a 20  're not sure),. 
e6590 20 2a 2a 20 62 75 74 20 62 65 63 61 75 73 65 20   ** but because 
e65a0 6e 6f 20 77 61 79 20 74 6f 20 74 65 73 74 20 69  no way to test i
e65b0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  t is currently a
e65c0 76 61 69 6c 61 62 6c 65 2e 20 49 74 20 69 73 20  vailable. It is 
e65d0 62 65 74 74 65 72 20 0a 20 20 2a 2a 20 6e 6f 74  better .  ** not
e65e0 20 74 6f 20 72 69 73 6b 20 62 72 65 61 6b 69 6e   to risk breakin
e65f0 67 20 76 78 77 6f 72 6b 73 20 73 75 70 70 6f 72  g vxworks suppor
e6600 74 20 66 6f 72 20 74 68 65 20 73 61 6b 65 20 6f  t for the sake o
e6610 66 20 73 75 63 68 20 61 6e 20 6f 62 73 63 75 72  f such an obscur
e6620 65 20 0a 20 20 2a 2a 20 66 65 61 74 75 72 65 2e  e .  ** feature.
e6630 20 20 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 57    */.#if !OS_VXW
e6640 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 73 74  ORKS.  struct st
e6650 61 74 20 73 53 74 61 74 3b 20 20 20 20 20 20 20  at sStat;       
e6660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
e6670 65 73 75 6c 74 73 20 6f 66 20 73 74 61 74 28 29  esults of stat()
e6680 20 63 61 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 41   call */..  /* A
e6690 20 73 74 61 74 28 29 20 63 61 6c 6c 20 6d 61 79   stat() call may
e66a0 20 66 61 69 6c 20 66 6f 72 20 76 61 72 69 6f 75   fail for variou
e66b0 73 20 72 65 61 73 6f 6e 73 2e 20 49 66 20 74 68  s reasons. If th
e66c0 69 73 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69  is happens, it i
e66d0 73 0a 20 20 2a 2a 20 61 6c 6d 6f 73 74 20 63 65  s.  ** almost ce
e66e0 72 74 61 69 6e 20 74 68 61 74 20 61 6e 20 6f 70  rtain that an op
e66f0 65 6e 28 29 20 63 61 6c 6c 20 6f 6e 20 74 68 65  en() call on the
e6700 20 73 61 6d 65 20 70 61 74 68 20 77 69 6c 6c 20   same path will 
e6710 61 6c 73 6f 20 66 61 69 6c 2e 0a 20 20 2a 2a 20  also fail..  ** 
e6720 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c  For this reason,
e6730 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
e6740 75 72 73 20 69 6e 20 74 68 65 20 73 74 61 74 28  urs in the stat(
e6750 29 20 63 61 6c 6c 20 68 65 72 65 2c 20 69 74 20  ) call here, it 
e6760 69 73 0a 20 20 2a 2a 20 69 67 6e 6f 72 65 64 20  is.  ** ignored 
e6770 61 6e 64 20 2d 31 20 69 73 20 72 65 74 75 72 6e  and -1 is return
e6780 65 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  ed. The caller w
e6790 69 6c 6c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20  ill try to open 
e67a0 61 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20  a new file.  ** 
e67b0 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68  descriptor on th
e67c0 65 20 73 61 6d 65 20 70 61 74 68 2c 20 66 61 69  e same path, fai
e67d0 6c 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e  l, and return an
e67e0 20 65 72 72 6f 72 20 74 6f 20 53 51 4c 69 74 65   error to SQLite
e67f0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e  ..  **.  ** Even
e6800 20 69 66 20 61 20 73 75 62 73 65 71 75 65 6e 74   if a subsequent
e6810 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65   open() call doe
e6820 73 20 73 75 63 63 65 65 64 2c 20 74 68 65 20 63  s succeed, the c
e6830 6f 6e 73 65 71 75 65 6e 63 65 73 20 6f 66 0a 20  onsequences of. 
e6840 20 2a 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e   ** not searchin
e6850 67 20 66 6f 72 20 61 20 72 65 73 75 73 61 62 6c  g for a resusabl
e6860 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
e6870 72 20 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20  r are not dire. 
e6880 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 61   */.  if( 0==sta
e6890 74 28 7a 50 61 74 68 2c 20 26 73 53 74 61 74 29  t(zPath, &sStat)
e68a0 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75   ){.    struct u
e68b0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 3b 0a  nixOpenCnt *pO;.
e68c0 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 46      struct unixF
e68d0 69 6c 65 49 64 20 69 64 3b 0a 20 20 20 20 69 64  ileId id;.    id
e68e0 2e 64 65 76 20 3d 20 73 53 74 61 74 2e 73 74 5f  .dev = sStat.st_
e68f0 64 65 76 3b 0a 20 20 20 20 69 64 2e 69 6e 6f 20  dev;.    id.ino 
e6900 3d 20 73 53 74 61 74 2e 73 74 5f 69 6e 6f 3b 0a  = sStat.st_ino;.
e6910 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
e6920 74 65 78 28 29 3b 0a 20 20 20 20 66 6f 72 28 70  tex();.    for(p
e6930 4f 3d 6f 70 65 6e 4c 69 73 74 3b 20 70 4f 20 26  O=openList; pO &
e6940 26 20 6d 65 6d 63 6d 70 28 26 69 64 2c 20 26 70  & memcmp(&id, &p
e6950 4f 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f  O->fileId, sizeo
e6960 66 28 69 64 29 29 3b 20 70 4f 3d 70 4f 2d 3e 70  f(id)); pO=pO->p
e6970 4e 65 78 74 29 3b 0a 20 20 20 20 69 66 28 20 70  Next);.    if( p
e6980 4f 20 29 7b 0a 20 20 20 20 20 20 55 6e 69 78 55  O ){.      UnixU
e6990 6e 75 73 65 64 46 64 20 2a 2a 70 70 3b 0a 20 20  nusedFd **pp;.  
e69a0 20 20 20 20 66 6f 72 28 70 70 3d 26 70 4f 2d 3e      for(pp=&pO->
e69b0 70 55 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20  pUnused; *pp && 
e69c0 28 2a 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c  (*pp)->flags!=fl
e69d0 61 67 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d  ags; pp=&((*pp)-
e69e0 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20  >pNext));.      
e69f0 70 55 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20  pUnused = *pp;. 
e6a00 20 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64       if( pUnused
e6a10 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20   ){.        *pp 
e6a20 3d 20 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74  = pUnused->pNext
e6a30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
e6a40 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
e6a50 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ex();.  }.#endif
e6a60 20 20 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58      /* if !OS_VX
e6a70 57 4f 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72  WORKS */.  retur
e6a80 6e 20 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a  n pUnused;.}../*
e6a90 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c  .** Open the fil
e6aa0 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20  e zPath..** .** 
e6ab0 50 72 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20  Previously, the 
e6ac0 53 51 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20  SQLite OS layer 
e6ad0 75 73 65 64 20 74 68 72 65 65 20 66 75 6e 63 74  used three funct
e6ae0 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66  ions in place of
e6af0 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a   this.** one:.**
e6b00 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
e6b10 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 29  sOpenReadWrite()
e6b20 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ;.**     sqlite3
e6b30 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29  OsOpenReadOnly()
e6b40 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ;.**     sqlite3
e6b50 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  OsOpenExclusive(
e6b60 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  );.**.** These c
e6b70 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20  alls correspond 
e6b80 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
e6b90 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66   combinations of
e6ba0 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   flags:.**.**   
e6bb0 20 20 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e    ReadWrite() ->
e6bc0 20 20 20 20 20 28 52 45 41 44 57 52 49 54 45 20       (READWRITE 
e6bd0 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20  | CREATE).**    
e6be0 20 52 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20   ReadOnly()  -> 
e6bf0 20 20 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a      (READONLY) .
e6c00 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75  **     OpenExclu
e6c10 73 69 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57  sive() -> (READW
e6c20 52 49 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20  RITE | CREATE | 
e6c30 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a  EXCLUSIVE).**.**
e6c40 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63   The old OpenExc
e6c50 6c 75 73 69 76 65 28 29 20 61 63 63 65 70 74 65  lusive() accepte
e6c60 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75  d a boolean argu
e6c70 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22  ment - "delFlag"
e6c80 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68  . If.** true, th
e6c90 65 20 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69  e file was confi
e6ca0 67 75 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f  gured to be auto
e6cb0 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
e6cc0 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69  d when the.** fi
e6cd0 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64  le handle closed
e6ce0 2e 20 54 6f 20 61 63 68 69 65 76 65 20 74 68 65  . To achieve the
e6cf0 20 73 61 6d 65 20 65 66 66 65 63 74 20 75 73 69   same effect usi
e6d00 6e 67 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20  ng this new .** 
e6d10 69 6e 74 65 72 66 61 63 65 2c 20 61 64 64 20 74  interface, add t
e6d20 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  he DELETEONCLOSE
e6d30 20 66 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73   flag to those s
e6d40 70 65 63 69 66 69 65 64 20 61 62 6f 76 65 20 66  pecified above f
e6d50 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75  or .** OpenExclu
e6d60 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  sive()..*/.stati
e6d70 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a  c int unixOpen(.
e6d80 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
e6d90 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Vfs,           /
e6da0 2a 20 54 68 65 20 56 46 53 20 66 6f 72 20 77 68  * The VFS for wh
e6db0 69 63 68 20 74 68 69 73 20 69 73 20 74 68 65 20  ich this is the 
e6dc0 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a  xOpen method */.
e6dd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
e6de0 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ath,           /
e6df0 2a 20 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69  * Pathname of fi
e6e00 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20  le to be opened 
e6e10 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
e6e20 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20  e *pFile,       
e6e30 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
e6e40 73 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66  scriptor to be f
e6e50 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e  illed in */.  in
e6e60 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
e6e70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
e6e80 70 75 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e  put flags to con
e6e90 74 72 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67  trol the opening
e6ea0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46   */.  int *pOutF
e6eb0 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
e6ec0 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61     /* Output fla
e6ed0 67 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53  gs returned to S
e6ee0 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b  QLite core */.){
e6ef0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d  .  unixFile *p =
e6f00 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 46 69   (unixFile *)pFi
e6f10 6c 65 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d  le;.  int fd = -
e6f20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
e6f30 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
e6f40 63 72 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64  criptor returned
e6f50 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20   by open() */.  
e6f60 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20  int dirfd = -1; 
e6f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e6f80 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65  * Directory file
e6f90 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
e6fa0 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d   int openFlags =
e6fb0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
e6fc0 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73  /* Flags to pass
e6fd0 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20   to open() */.  
e6fe0 69 6e 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67  int eType = flag
e6ff0 73 26 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f  s&0xFFFFFF00;  /
e7000 2a 20 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74  * Type of file t
e7010 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
e7020 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20  noLock;         
e7030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
e7040 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69  ue to omit locki
e7050 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f  ng primitives */
e7060 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
e7070 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
e7080 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 52 65    /* Function Re
e7090 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20  turn Code */..  
e70a0 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20  int isExclusive 
e70b0 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
e70c0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
e70d0 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65  E);.  int isDele
e70e0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20  te     = (flags 
e70f0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  & SQLITE_OPEN_DE
e7100 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20  LETEONCLOSE);.  
e7110 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20  int isCreate    
e7120 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
e7130 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b  TE_OPEN_CREATE);
e7140 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c  .  int isReadonl
e7150 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53  y   = (flags & S
e7160 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
e7170 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65  NLY);.  int isRe
e7180 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67  adWrite  = (flag
e7190 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
e71a0 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f  READWRITE);..  /
e71b0 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20  * If creating a 
e71c0 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66  master or main-f
e71d0 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69  ile journal, thi
e71e0 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  s function will 
e71f0 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65  open.  ** a file
e7200 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74  -descriptor on t
e7210 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f  he directory too
e7220 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  . The first time
e7230 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a   unixSync().  **
e7240 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64   is called the d
e7250 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65  irectory file de
e7260 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65  scriptor will be
e7270 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63   fsync()ed and c
e7280 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20  lose()d..  */.  
e7290 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74  int isOpenDirect
e72a0 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65 20  ory = (isCreate 
e72b0 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70 65  && .      (eType
e72c0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
e72d0 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  STER_JOURNAL || 
e72e0 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e72f0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29  EN_MAIN_JOURNAL)
e7300 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  .  );..  /* If a
e7310 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73  rgument zPath is
e7320 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
e7330 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
e7340 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70  s required to op
e7350 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72  en.  ** a tempor
e7360 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68  ary file. Use th
e7370 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f  is buffer to sto
e7380 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  re the file name
e7390 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72   in..  */.  char
e73a0 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41   zTmpname[MAX_PA
e73b0 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e  THNAME+1];.  con
e73c0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
e73d0 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68   zPath;..  /* Ch
e73e0 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  eck the followin
e73f0 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  g statements are
e7400 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a   true: .  **.  *
e7410 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20  *   (a) Exactly 
e7420 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57  one of the READW
e7430 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c  RITE and READONL
e7440 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20  Y flags must be 
e7450 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20  set, and .  **  
e7460 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69   (b) if CREATE i
e7470 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44  s set, then READ
e7480 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  WRITE must also 
e7490 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a  be set, and.  **
e74a0 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53     (c) if EXCLUS
e74b0 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  IVE is set, then
e74c0 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73   CREATE must als
e74d0 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20  o be set..  **  
e74e0 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e   (d) if DELETEON
e74f0 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68  CLOSE is set, th
e7500 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61  en CREATE must a
e7510 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f  lso be set..  */
e7520 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61  .  assert((isRea
e7530 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65  donly==0 || isRe
e7540 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28  adWrite==0) && (
e7550 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69  isReadWrite || i
e7560 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61  sReadonly));.  a
e7570 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d  ssert(isCreate==
e7580 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65  0 || isReadWrite
e7590 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78  );.  assert(isEx
e75a0 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73  clusive==0 || is
e75b0 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72  Create);.  asser
e75c0 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c  t(isDelete==0 ||
e75d0 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f   isCreate);..  /
e75e0 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d  * The main DB, m
e75f0 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64  ain journal, and
e7600 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
e7610 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61  are never automa
e7620 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c  tically.  ** del
e7630 65 74 65 64 2e 20 4e 6f 72 20 61 72 65 20 74 68  eted. Nor are th
e7640 65 79 20 65 76 65 72 20 74 65 6d 70 6f 72 61 72  ey ever temporar
e7650 79 20 66 69 6c 65 73 2e 20 20 2a 2f 0a 20 20 61  y files.  */.  a
e7660 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74  ssert( (!isDelet
e7670 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65  e && zName) || e
e7680 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45  Type!=SQLITE_OPE
e7690 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 61  N_MAIN_DB );.  a
e76a0 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74  ssert( (!isDelet
e76b0 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65  e && zName) || e
e76c0 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45  Type!=SQLITE_OPE
e76d0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29  N_MAIN_JOURNAL )
e76e0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73  ;.  assert( (!is
e76f0 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29  Delete && zName)
e7700 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54   || eType!=SQLIT
e7710 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
e7720 55 52 4e 41 4c 20 29 3b 0a 0a 20 20 2f 2a 20 41  URNAL );..  /* A
e7730 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 75  ssert that the u
e7740 70 70 65 72 20 6c 61 79 65 72 20 68 61 73 20 73  pper layer has s
e7750 65 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 66  et one of the "f
e7760 69 6c 65 2d 74 79 70 65 22 20 66 6c 61 67 73 2e  ile-type" flags.
e7770 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54   */.  assert( eT
e7780 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e7790 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c  _MAIN_DB      ||
e77a0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e77b0 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20  PEN_TEMP_DB .   
e77c0 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51      || eType==SQ
e77d0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
e77e0 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d  OURNAL || eType=
e77f0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  =SQLITE_OPEN_TEM
e7800 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20  P_JOURNAL .     
e7810 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
e7820 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
e7830 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53  AL   || eType==S
e7840 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
e7850 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20  R_JOURNAL .     
e7860 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
e7870 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
e7880 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d  T_DB.  );..  mem
e7890 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
e78a0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20  (unixFile));..  
e78b0 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  if( eType==SQLIT
e78c0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29  E_OPEN_MAIN_DB )
e78d0 7b 0a 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64  {.    UnixUnused
e78e0 46 64 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20  Fd *pUnused;.   
e78f0 20 70 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52   pUnused = findR
e7900 65 75 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c  eusableFd(zName,
e7910 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28   flags);.    if(
e7920 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20   pUnused ){.    
e7930 20 20 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e    fd = pUnused->
e7940 66 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  fd;.    }else{. 
e7950 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73       pUnused = s
e7960 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
e7970 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b  zeof(*pUnused));
e7980 0a 20 20 20 20 20 20 69 66 28 20 21 70 55 6e 75  .      if( !pUnu
e7990 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72  sed ){.        r
e79a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
e79b0 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
e79c0 7d 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64  }.    p->pUnused
e79d0 20 3d 20 70 55 6e 75 73 65 64 3b 0a 20 20 7d 65   = pUnused;.  }e
e79e0 6c 73 65 20 69 66 28 20 21 7a 4e 61 6d 65 20 29  lse if( !zName )
e79f0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d  {.    /* If zNam
e7a00 65 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 75  e is NULL, the u
e7a10 70 70 65 72 20 6c 61 79 65 72 20 69 73 20 72 65  pper layer is re
e7a20 71 75 65 73 74 69 6e 67 20 61 20 74 65 6d 70 20  questing a temp 
e7a30 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73  file. */.    ass
e7a40 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26 20  ert(isDelete && 
e7a50 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79  !isOpenDirectory
e7a60 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54  );.    rc = getT
e7a70 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48  empname(MAX_PATH
e7a80 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65  NAME+1, zTmpname
e7a90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e7aa0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e7ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
e7ac0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a   }.    zName = z
e7ad0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20  Tmpname;.  }..  
e7ae0 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  /* Determine the
e7af0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c   value of the fl
e7b00 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61  ags parameter pa
e7b10 73 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75  ssed to POSIX fu
e7b20 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e  nction.  ** open
e7b30 28 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62  (). These must b
e7b40 65 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65  e calculated eve
e7b50 6e 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e  n if open() is n
e7b60 6f 74 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20  ot called, as.  
e7b70 2a 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 73  ** they may be s
e7b80 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66  tored as part of
e7b90 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65   the file handle
e7ba0 20 61 6e 64 20 75 73 65 64 20 62 79 20 74 68 65   and used by the
e7bb0 20 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69   .  ** 'conch fi
e7bc0 6c 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63  le' locking func
e7bd0 74 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20  tions later on. 
e7be0 20 2a 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64   */.  if( isRead
e7bf0 6f 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67  only )  openFlag
e7c00 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20  s |= O_RDONLY;. 
e7c10 20 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65   if( isReadWrite
e7c20 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20   ) openFlags |= 
e7c30 4f 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73  O_RDWR;.  if( is
e7c40 43 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e  Create )    open
e7c50 46 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54  Flags |= O_CREAT
e7c60 3b 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73  ;.  if( isExclus
e7c70 69 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20  ive ) openFlags 
e7c80 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46  |= (O_EXCL|O_NOF
e7c90 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c  OLLOW);.  openFl
e7ca0 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46  ags |= (O_LARGEF
e7cb0 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a  ILE|O_BINARY);..
e7cc0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
e7cd0 20 20 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64    mode_t openMod
e7ce0 65 20 3d 20 28 69 73 44 65 6c 65 74 65 3f 30 36  e = (isDelete?06
e7cf0 30 30 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  00:SQLITE_DEFAUL
e7d00 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f  T_FILE_PERMISSIO
e7d10 4e 53 29 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70  NS);.    fd = op
e7d20 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c  en(zName, openFl
e7d30 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a  ags, openMode);.
e7d40 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50      OSTRACE4("OP
e7d50 45 4e 58 20 20 20 25 2d 33 64 20 25 73 20 30 25  ENX   %-3d %s 0%
e7d60 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c  o\n", fd, zName,
e7d70 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 20   openFlags);.   
e7d80 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 72   if( fd<0 && err
e7d90 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 73  no!=EISDIR && is
e7da0 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69 73  ReadWrite && !is
e7db0 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20  Exclusive ){.   
e7dc0 20 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20     /* Failed to 
e7dd0 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f  open the file fo
e7de0 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  r read/write acc
e7df0 65 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e  ess. Try read-on
e7e00 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61  ly. */.      fla
e7e10 67 73 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f  gs &= ~(SQLITE_O
e7e20 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
e7e30 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
e7e40 29 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c 61  );.      openFla
e7e50 67 73 20 26 3d 20 7e 28 4f 5f 52 44 57 52 7c 4f  gs &= ~(O_RDWR|O
e7e60 5f 43 52 45 41 54 29 3b 0a 20 20 20 20 20 20 66  _CREAT);.      f
e7e70 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f  lags |= SQLITE_O
e7e80 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  PEN_READONLY;.  
e7e90 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d      openFlags |=
e7ea0 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 20   O_RDONLY;.     
e7eb0 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65   fd = open(zName
e7ec0 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65  , openFlags, ope
e7ed0 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  nMode);.    }.  
e7ee0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
e7ef0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e7f00 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20  CANTOPEN;.      
e7f10 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68  goto open_finish
e7f20 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ed;.    }.  }.  
e7f30 61 73 73 65 72 74 28 20 66 64 3e 3d 30 20 29 3b  assert( fd>=0 );
e7f40 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73  .  if( pOutFlags
e7f50 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61   ){.    *pOutFla
e7f60 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a  gs = flags;.  }.
e7f70 0a 20 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 65  .  if( p->pUnuse
e7f80 64 20 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e 75  d ){.    p->pUnu
e7f90 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 20  sed->fd = fd;.  
e7fa0 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c    p->pUnused->fl
e7fb0 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d  ags = flags;.  }
e7fc0 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 65  ..  if( isDelete
e7fd0 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52   ){.#if OS_VXWOR
e7fe0 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a  KS.    zPath = z
e7ff0 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20  Name;.#else.    
e8000 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23  unlink(zName);.#
e8010 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51  endif.  }.#if SQ
e8020 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
e8030 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65  ING_STYLE.  else
e8040 7b 0a 20 20 20 20 70 2d 3e 6f 70 65 6e 46 6c 61  {.    p->openFla
e8050 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a  gs = openFlags;.
e8060 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
e8070 28 20 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72  ( isOpenDirector
e8080 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70  y ){.    rc = op
e8090 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74  enDirectory(zPat
e80a0 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20  h, &dirfd);.    
e80b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
e80c0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74  K ){.      /* It
e80d0 20 69 73 20 73 61 66 65 20 74 6f 20 63 6c 6f 73   is safe to clos
e80e0 65 20 66 64 20 61 74 20 74 68 69 73 20 70 6f 69  e fd at this poi
e80f0 6e 74 2c 20 62 65 63 61 75 73 65 20 69 74 20 69  nt, because it i
e8100 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74  s guaranteed not
e8110 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  .      ** to be 
e8120 6f 70 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61  open on a databa
e8130 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 77  se file. If it w
e8140 65 72 65 20 6f 70 65 6e 20 6f 6e 20 61 20 64 61  ere open on a da
e8150 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 20 20 20  tabase file,.   
e8160 20 20 20 2a 2a 20 69 74 20 77 6f 75 6c 64 20 6e     ** it would n
e8170 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20 63 6c  ot be safe to cl
e8180 6f 73 65 20 61 73 20 74 68 69 73 20 77 6f 75 6c  ose as this woul
e8190 64 20 72 65 6c 65 61 73 65 20 61 6e 79 20 6c 6f  d release any lo
e81a0 63 6b 73 20 68 65 6c 64 0a 20 20 20 20 20 20 2a  cks held.      *
e81b0 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 62 79  * on the file by
e81c0 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 20 20   this process.  
e81d0 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
e81e0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f   eType!=SQLITE_O
e81f0 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20  PEN_MAIN_DB );. 
e8200 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20       close(fd); 
e8210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
e8220 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20  ilently leak if 
e8230 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e  fail, already in
e8240 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20   error */.      
e8250 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68  goto open_finish
e8260 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23  ed;.    }.  }..#
e8270 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43  ifdef FD_CLOEXEC
e8280 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53  .  fcntl(fd, F_S
e8290 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20  ETFD, fcntl(fd, 
e82a0 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44  F_GETFD, 0) | FD
e82b0 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69  _CLOEXEC);.#endi
e82c0 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54  f..  noLock = eT
e82d0 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype!=SQLITE_OPEN
e82e0 5f 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53  _MAIN_DB;..#if S
e82f0 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f  QLITE_PREFER_PRO
e8300 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28  XY_LOCKING.  if(
e8310 20 7a 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20   zPath!=NULL && 
e8320 21 6e 6f 4c 6f 63 6b 20 26 26 20 70 56 66 73 2d  !noLock && pVfs-
e8330 3e 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63 68  >xOpen ){.    ch
e8340 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67  ar *envforce = g
e8350 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f  etenv("SQLITE_FO
e8360 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  RCE_PROXY_LOCKIN
e8370 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65  G");.    int use
e8380 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20  Proxy = 0;..    
e8390 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f  /* SQLITE_FORCE_
e83a0 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31  PROXY_LOCKING==1
e83b0 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77   means force alw
e83c0 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20 30  ays use proxy, 0
e83d0 20 6d 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20 6e   means .    ** n
e83e0 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20  ever use proxy, 
e83f0 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70  NULL means use p
e8400 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63  roxy for non-loc
e8410 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20  al files only.  
e8420 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76 66 6f  */.    if( envfo
e8430 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  rce!=NULL ){.   
e8440 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 61 74     useProxy = at
e8450 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a  oi(envforce)>0;.
e8460 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
e8470 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66   struct statfs f
e8480 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28  sInfo;.      if(
e8490 20 73 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26   statfs(zPath, &
e84a0 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b  fsInfo) == -1 ){
e84b0 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74  .        /* In t
e84c0 68 65 6f 72 79 2c 20 74 68 65 20 63 6c 6f 73 65  heory, the close
e84d0 28 66 64 29 20 63 61 6c 6c 20 69 73 20 73 75 62  (fd) call is sub
e84e0 2d 6f 70 74 69 6d 61 6c 2e 20 49 66 20 74 68 65  -optimal. If the
e84f0 20 66 69 6c 65 20 6f 70 65 6e 65 64 0a 20 20 20   file opened.   
e8500 20 20 20 20 20 2a 2a 20 77 69 74 68 20 66 64 20       ** with fd 
e8510 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69  is a database fi
e8520 6c 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72  le, and there ar
e8530 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
e8540 6f 6e 73 20 6f 70 65 6e 0a 20 20 20 20 20 20 20  ons open.       
e8550 20 2a 2a 20 6f 6e 20 74 68 61 74 20 66 69 6c 65   ** on that file
e8560 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e   that are curren
e8570 74 6c 79 20 68 6f 6c 64 69 6e 67 20 61 64 76 69  tly holding advi
e8580 73 6f 72 79 20 6c 6f 63 6b 73 20 6f 6e 20 69 74  sory locks on it
e8590 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  ,.        ** the
e85a0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6c  n the call to cl
e85b0 6f 73 65 28 29 20 77 69 6c 6c 20 63 61 6e 63 65  ose() will cance
e85c0 6c 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 49  l those locks. I
e85d0 6e 20 70 72 61 63 74 69 63 65 2c 0a 20 20 20 20  n practice,.    
e85e0 20 20 20 20 2a 2a 20 77 65 27 72 65 20 61 73 73      ** we're ass
e85f0 75 6d 69 6e 67 20 74 68 61 74 20 73 74 61 74 66  uming that statf
e8600 73 28 29 20 64 6f 65 73 6e 27 74 20 66 61 69 6c  s() doesn't fail
e8610 20 76 65 72 79 20 6f 66 74 65 6e 2e 20 41 74 20   very often. At 
e8620 6c 65 61 73 74 0a 20 20 20 20 20 20 20 20 2a 2a  least.        **
e8630 20 6e 6f 74 20 77 68 69 6c 65 20 6f 74 68 65 72   not while other
e8640 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e8650 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20  s opened by the 
e8660 73 61 6d 65 20 70 72 6f 63 65 73 73 20 6f 6e 0a  same process on.
e8670 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73          ** the s
e8680 61 6d 65 20 66 69 6c 65 20 61 72 65 20 77 6f 72  ame file are wor
e8690 6b 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20  king.  */.      
e86a0 20 20 70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d    p->lastErrno =
e86b0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
e86c0 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 7b 0a  if( dirfd>=0 ){.
e86d0 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28            close(
e86e0 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  dirfd); /* silen
e86f0 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c  tly leak if fail
e8700 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  , in error */.  
e8710 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e8720 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
e8730 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66  lently leak if f
e8740 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f  ail, in error */
e8750 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
e8760 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
e8770 53 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  S;.        goto 
e8780 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20  open_finished;. 
e8790 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 73 65       }.      use
e87a0 50 72 6f 78 79 20 3d 20 21 28 66 73 49 6e 66 6f  Proxy = !(fsInfo
e87b0 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43  .f_flags&MNT_LOC
e87c0 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  AL);.    }.    i
e87d0 66 28 20 75 73 65 50 72 6f 78 79 20 29 7b 0a 20  f( useProxy ){. 
e87e0 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e       rc = fillIn
e87f0 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66  UnixFile(pVfs, f
e8800 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c  d, dirfd, pFile,
e8810 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20   zPath, noLock, 
e8820 69 73 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20  isDelete);.     
e8830 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
e8840 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
e8850 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72   = proxyTransfor
e8860 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 46  mUnixFile((unixF
e8870 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 75  ile*)pFile, ":au
e8880 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  to:");.      }. 
e8890 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66       goto open_f
e88a0 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20  inished;.    }. 
e88b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72   }.#endif.  .  r
e88c0 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69  c = fillInUnixFi
e88d0 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72  le(pVfs, fd, dir
e88e0 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68  fd, pFile, zPath
e88f0 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65  , noLock, isDele
e8900 74 65 29 3b 0a 6f 70 65 6e 5f 66 69 6e 69 73 68  te);.open_finish
e8910 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ed:.  if( rc!=SQ
e8920 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
e8930 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70  qlite3_free(p->p
e8940 55 6e 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72  Unused);.  }.  r
e8950 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
e8960 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66  .** Delete the f
e8970 69 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66  ile at zPath. If
e8980 20 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67   the dirSync arg
e8990 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66  ument is true, f
e89a0 73 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69  sync().** the di
e89b0 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65  rectory after de
e89c0 6c 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e  leting the file.
e89d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e89e0 6e 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c  nixDelete(.  sql
e89f0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e8a00 64 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f  d,     /* VFS co
e8a10 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73  ntaining this as
e8a20 20 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74   the xDelete met
e8a30 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
e8a40 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
e8a50 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69     /* Name of fi
e8a60 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  le to be deleted
e8a70 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e   */.  int dirSyn
e8a80 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
e8a90 2f 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e  /* If true, fsyn
e8aa0 63 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66  c() directory af
e8ab0 74 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c  ter deleting fil
e8ac0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
e8ad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
e8ae0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e8af0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d  (NotUsed);.  Sim
e8b00 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
e8b10 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e8b20 5f 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69  _DELETE);.  unli
e8b30 6e 6b 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64  nk(zPath);.#ifnd
e8b40 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
e8b50 45 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20  E_DIRSYNC.  if( 
e8b60 64 69 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69  dirSync ){.    i
e8b70 6e 74 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20  nt fd;.    rc = 
e8b80 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50  openDirectory(zP
e8b90 61 74 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69  ath, &fd);.    i
e8ba0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e8bb0 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52   ){.#if OS_VXWOR
e8bc0 4b 53 0a 20 20 20 20 20 20 69 66 28 20 66 73 79  KS.      if( fsy
e8bd0 6e 63 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c  nc(fd)==-1 ).#el
e8be0 73 65 0a 20 20 20 20 20 20 69 66 28 20 66 73 79  se.      if( fsy
e8bf0 6e 63 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a  nc(fd) ).#endif.
e8c00 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
e8c10 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e8c20 52 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20  R_DIR_FSYNC;.   
e8c30 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
e8c40 6c 6f 73 65 28 66 64 29 26 26 21 72 63 20 29 7b  lose(fd)&&!rc ){
e8c50 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
e8c60 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43  LITE_IOERR_DIR_C
e8c70 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LOSE;.      }.  
e8c80 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
e8c90 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
e8ca0 2a 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78  *.** Test the ex
e8cb0 69 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63  istance of or ac
e8cc0 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73  cess permissions
e8cd0 20 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20   of file zPath. 
e8ce0 54 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66  The.** test perf
e8cf0 6f 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e  ormed depends on
e8d00 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c   the value of fl
e8d10 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ags:.**.**     S
e8d20 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
e8d30 53 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66  STS: Return 1 if
e8d40 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73   the file exists
e8d50 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41  .**     SQLITE_A
e8d60 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a  CCESS_READWRITE:
e8d70 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
e8d80 20 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e   file is read an
e8d90 64 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20  d writable..**  
e8da0 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53     SQLITE_ACCESS
e8db0 5f 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72  _READONLY: Retur
e8dc0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
e8dd0 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a  is readable..**.
e8de0 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  ** Otherwise ret
e8df0 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  urn 0..*/.static
e8e00 20 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28   int unixAccess(
e8e10 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
e8e20 4e 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68  NotUsed,   /* Th
e8e30 65 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67  e VFS containing
e8e40 20 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65   this xAccess me
e8e50 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  thod */.  const 
e8e60 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
e8e70 20 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65    /* Path of the
e8e80 20 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65   file to examine
e8e90 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e8eb0 20 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74   What do we want
e8ec0 20 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20   to learn about 
e8ed0 74 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20  the zPath file? 
e8ee0 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75  */.  int *pResOu
e8ef0 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
e8f00 57 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f  Write result boo
e8f10 6c 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  lean here */.){.
e8f20 20 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b    int amode = 0;
e8f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e8f40 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
e8f50 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
e8f60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
e8f70 4f 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a  OERR_ACCESS; );.
e8f80 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
e8f90 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
e8fa0 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
e8fb0 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20  :.      amode = 
e8fc0 46 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61  F_OK;.      brea
e8fd0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
e8fe0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
e8ff0 49 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65  ITE:.      amode
e9000 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20   = W_OK|R_OK;.  
e9010 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
e9020 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
e9030 53 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d  S_READ:.      am
e9040 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20  ode = R_OK;.    
e9050 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65    break;..    de
e9060 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73  fault:.      ass
e9070 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c  ert(!"Invalid fl
e9080 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a  ags argument");.
e9090 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d    }.  *pResOut =
e90a0 20 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20   (access(zPath, 
e90b0 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65  amode)==0);.  re
e90c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e90d0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61  }.../*.** Turn a
e90e0 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61   relative pathna
e90f0 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70  me into a full p
e9100 61 74 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c  athname. The rel
e9110 61 74 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73  ative path.** is
e9120 20 73 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c   stored as a nul
e9130 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
e9140 6e 67 20 69 6e 20 74 68 65 20 62 75 66 66 65 72  ng in the buffer
e9150 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a   pointed to by.*
e9160 2a 20 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20  * zPath. .**.** 
e9170 7a 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  zOut points to a
e9180 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65   buffer of at le
e9190 61 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e  ast sqlite3_vfs.
e91a0 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73  mxPathname bytes
e91b0 20 0a 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61   .** (in this ca
e91c0 73 65 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45  se, MAX_PATHNAME
e91d0 20 62 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c   bytes). The ful
e91e0 6c 2d 70 61 74 68 20 69 73 20 77 72 69 74 74 65  l-path is writte
e91f0 6e 20 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66  n to.** this buf
e9200 66 65 72 20 62 65 66 6f 72 65 20 72 65 74 75 72  fer before retur
e9210 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
e9220 69 6e 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68  int unixFullPath
e9230 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  name(.  sqlite3_
e9240 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
e9250 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
e9260 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a   to vfs object *
e9270 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
e9280 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20  zPath,          
e9290 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65    /* Possibly re
e92a0 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74  lative input pat
e92b0 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c  h */.  int nOut,
e92c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e92d0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
e92e0 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e  output buffer in
e92f0 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72   bytes */.  char
e9300 20 2a 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20   *zOut          
e9310 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
e9320 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b  put buffer */.){
e9330 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20  ..  /* It's odd 
e9340 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69  to simulate an i
e9350 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75  o-error here, bu
e9360 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73  t really this is
e9370 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67   just.  ** using
e9380 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e   the io-error in
e9390 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20  frastructure to 
e93a0 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65  test that SQLite
e93b0 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20   handles this.  
e93c0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c  ** function fail
e93d0 69 6e 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ing. This functi
e93e0 6f 6e 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66  on could fail if
e93f0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  , for example, t
e9400 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20  he.  ** current 
e9410 77 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72  working director
e9420 79 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e  y has been unlin
e9430 6b 65 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75  ked..  */.  Simu
e9440 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
e9450 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
e9460 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
e9470 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d  Vfs->mxPathname=
e9480 3d 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b  =MAX_PATHNAME );
e9490 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e94a0 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f  TER(pVfs);..  zO
e94b0 75 74 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30  ut[nOut-1] = '\0
e94c0 27 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30  ';.  if( zPath[0
e94d0 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71  ]=='/' ){.    sq
e94e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
e94f0 4f 75 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c  Out, zOut, "%s",
e9500 20 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65   zPath);.  }else
e9510 7b 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a  {.    int nCwd;.
e9520 20 20 20 20 69 66 28 20 67 65 74 63 77 64 28 7a      if( getcwd(z
e9530 4f 75 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20  Out, nOut-1)==0 
e9540 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
e9550 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
e9560 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20  .    }.    nCwd 
e9570 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f  = (int)strlen(zO
e9580 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ut);.    sqlite3
e9590 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e  _snprintf(nOut-n
e95a0 43 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d  Cwd, &zOut[nCwd]
e95b0 2c 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b  , "/%s", zPath);
e95c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
e95d0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66  LITE_OK;.}...#if
e95e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
e95f0 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
e9600 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73  /*.** Interfaces
e9610 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73   for opening a s
e9620 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66  hared library, f
e9630 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69  inding entry poi
e9640 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  nts.** within th
e9650 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
e9660 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68  , and closing th
e9670 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
e9680 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64  ..*/.#include <d
e9690 6c 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76  lfcn.h>.static v
e96a0 6f 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28  oid *unixDlOpen(
e96b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
e96c0 55 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72  Used, const char
e96d0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
e96e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e96f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
e9700 75 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65  urn dlopen(zFile
e9710 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c  name, RTLD_NOW |
e9720 20 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d   RTLD_GLOBAL);.}
e9730 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ../*.** SQLite c
e9740 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69  alls this functi
e9750 6f 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  on immediately a
e9760 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75  fter a call to u
e9770 6e 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a  nixDlSym() or.**
e9780 20 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61   unixDlOpen() fa
e9790 69 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e  ils (returns a n
e97a0 75 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66  ull pointer). If
e97b0 20 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64   a more detailed
e97c0 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67   error.** messag
e97d0 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  e is available, 
e97e0 69 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  it is written to
e97f0 20 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20   zBufOut. If no 
e9800 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
e9810 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a   is available, z
e9820 42 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75  BufOut is left u
e9830 6e 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51  nmodified and SQ
e9840 4c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61  Lite uses a defa
e9850 75 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73  ult.** error mes
e9860 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sage..*/.static 
e9870 76 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72  void unixDlError
e9880 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
e9890 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c  tUsed, int nBuf,
e98a0 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b   char *zBufOut){
e98b0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20  .  char *zErr;. 
e98c0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e98d0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e  R(NotUsed);.  un
e98e0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
e98f0 20 20 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72    zErr = dlerror
e9900 28 29 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29  ();.  if( zErr )
e9910 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
e9920 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75  printf(nBuf, zBu
e9930 66 4f 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72  fOut, "%s", zErr
e9940 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61  );.  }.  unixLea
e9950 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61  veMutex();.}.sta
e9960 74 69 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44  tic void (*unixD
e9970 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
e9980 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20   *NotUsed, void 
e9990 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a  *p, const char*z
e99a0 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f  Sym))(void){.  /
e99b0 2a 20 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68  * .  ** GCC with
e99c0 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72   -pedantic-error
e99d0 73 20 73 61 79 73 20 74 68 61 74 20 43 39 30 20  s says that C90 
e99e0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61  does not allow a
e99f0 20 76 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a   void* to be.  *
e9a00 2a 20 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f  * cast into a po
e9a10 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
e9a20 69 6f 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68  ion.  And yet th
e9a30 65 20 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28  e library dlsym(
e9a40 29 20 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72  ) routine.  ** r
e9a50 65 74 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77  eturns a void* w
e9a60 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61  hich is really a
e9a70 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
e9a80 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20  nction.  So how 
e9a90 64 6f 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64  do we.  ** use d
e9aa0 6c 73 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64  lsym() with -ped
e9ab0 61 6e 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20  antic-errors?.  
e9ac0 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65  **.  ** Variable
e9ad0 20 78 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69   x below is defi
e9ae0 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e  ned to be a poin
e9af0 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
e9b00 6e 20 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61  n taking.  ** pa
e9b10 72 61 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61  rameters void* a
e9b20 6e 64 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61  nd const char* a
e9b30 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70  nd returning a p
e9b40 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
e9b50 74 69 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e  tion..  ** We in
e9b60 69 74 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73  itialize x by as
e9b70 73 69 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69  signing it a poi
e9b80 6e 74 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79  nter to the dlsy
e9b90 6d 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  m() function..  
e9ba0 2a 2a 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d  ** (That assignm
e9bb0 65 6e 74 20 72 65 71 75 69 72 65 73 20 61 20 63  ent requires a c
e9bc0 61 73 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63  ast.)  Then we c
e9bd0 61 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  all the function
e9be0 20 74 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69   that.  ** x poi
e9bf0 6e 74 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20  nts to.  .  **. 
e9c00 20 2a 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72   ** This work-ar
e9c10 6f 75 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79  ound is unlikely
e9c20 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74   to work correct
e9c30 6c 79 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d  ly on any system
e9c40 20 77 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20   where.  ** you 
e9c50 72 65 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61  really cannot ca
e9c60 73 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f  st a function po
e9c70 69 6e 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a  inter into void*
e9c80 2e 20 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20  .  But then, on 
e9c90 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68  the.  ** other h
e9ca0 61 6e 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c  and, dlsym() wil
e9cb0 6c 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75  l not work on su
e9cc0 63 68 20 61 20 73 79 73 74 65 6d 20 65 69 74 68  ch a system eith
e9cd0 65 72 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20  er, so we have. 
e9ce0 20 2a 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c   ** not really l
e9cf0 6f 73 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20  ost anything..  
e9d00 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29  */.  void (*(*x)
e9d10 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
e9d20 72 2a 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e  r*))(void);.  UN
e9d30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e9d40 6f 74 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28  otUsed);.  x = (
e9d50 76 6f 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c  void(*(*)(void*,
e9d60 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f  const char*))(vo
e9d70 69 64 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74  id))dlsym;.  ret
e9d80 75 72 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d  urn (*x)(p, zSym
e9d90 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
e9da0 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c   unixDlClose(sql
e9db0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e9dc0 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  d, void *pHandle
e9dd0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
e9de0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
e9df0 20 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c    dlclose(pHandl
e9e00 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69  e);.}.#else /* i
e9e10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
e9e20 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20  AD_EXTENSION is 
e9e30 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64  defined: */.  #d
e9e40 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e  efine unixDlOpen
e9e50 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e    0.  #define un
e9e60 69 78 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64  ixDlError 0.  #d
e9e70 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20  efine unixDlSym 
e9e80 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e    0.  #define un
e9e90 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64  ixDlClose 0.#end
e9ea0 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  if../*.** Write 
e9eb0 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61  nBuf bytes of ra
e9ec0 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65  ndom data to the
e9ed0 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72   supplied buffer
e9ee0 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63   zBuf..*/.static
e9ef0 20 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e   int unixRandomn
e9f00 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
e9f10 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42  *NotUsed, int nB
e9f20 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  uf, char *zBuf){
e9f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e9f40 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
e9f50 61 73 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e  assert((size_t)n
e9f60 42 75 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d  Buf>=(sizeof(tim
e9f70 65 5f 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29  e_t)+sizeof(int)
e9f80 29 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76  ));..  /* We hav
e9f90 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  e to initialize 
e9fa0 7a 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20  zBuf to prevent 
e9fb0 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65  valgrind from re
e9fc0 70 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72  porting.  ** err
e9fd0 6f 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74  ors.  The report
e9fe0 73 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67  s issued by valg
e9ff0 72 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65  rind are incorre
ea000 63 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20  ct - we would.  
ea010 2a 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74  ** prefer that t
ea020 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65  he randomness be
ea030 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61   increased by ma
ea040 6b 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a  king use of the.
ea050 20 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a    ** uninitializ
ea060 65 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66  ed space in zBuf
ea070 20 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20   - but valgrind 
ea080 65 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77  errors tend to w
ea090 6f 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75  orry.  ** some u
ea0a0 73 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68  sers.  Rather th
ea0b0 61 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65  an argue, it see
ea0c0 6d 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74  ms easier just t
ea0d0 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a  o initialize.  *
ea0e0 2a 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61  * the whole arra
ea0f0 79 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61  y and silence va
ea100 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20  lgrind, even if 
ea110 74 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20  that means less 
ea120 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20  randomness.  ** 
ea130 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65  in the random se
ea140 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68  ed..  **.  ** Wh
ea150 65 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74  en testing, init
ea160 69 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20  ializing zBuf[] 
ea170 74 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77  to zero is all w
ea180 65 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e  e do.  That mean
ea190 73 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61  s.  ** that we a
ea1a0 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61  lways use the sa
ea1b0 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  me random number
ea1c0 20 73 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73   sequence.  This
ea1d0 20 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20   makes the.  ** 
ea1e0 74 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65  tests repeatable
ea1f0 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
ea200 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a  zBuf, 0, nBuf);.
ea210 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
ea220 49 54 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20  ITE_TEST).  {.  
ea230 20 20 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20    int pid, fd;. 
ea240 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64     fd = open("/d
ea250 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52  ev/urandom", O_R
ea260 44 4f 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20  DONLY);.    if( 
ea270 66 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69  fd<0 ){.      ti
ea280 6d 65 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69  me_t t;.      ti
ea290 6d 65 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65  me(&t);.      me
ea2a0 6d 63 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73  mcpy(zBuf, &t, s
ea2b0 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20  izeof(t));.     
ea2c0 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b   pid = getpid();
ea2d0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  .      memcpy(&z
ea2e0 42 75 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20  Buf[sizeof(t)], 
ea2f0 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64  &pid, sizeof(pid
ea300 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ));.      assert
ea310 28 20 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65  ( sizeof(t)+size
ea320 6f 66 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74  of(pid)<=(size_t
ea330 29 6e 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e  )nBuf );.      n
ea340 42 75 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20  Buf = sizeof(t) 
ea350 2b 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20  + sizeof(pid);. 
ea360 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
ea370 6e 42 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20  nBuf = read(fd, 
ea380 7a 42 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20  zBuf, nBuf);.   
ea390 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20     close(fd);.  
ea3a0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
ea3b0 20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a   return nBuf;.}.
ea3c0 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
ea3d0 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
ea3e0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
ea3f0 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
ea400 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  pt..** The argum
ea410 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ent is the numbe
ea420 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
ea430 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65  s we want to sle
ea440 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ep..** The retur
ea450 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e  n value is the n
ea460 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65  umber of microse
ea470 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61  conds of sleep a
ea480 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65  ctually.** reque
ea490 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e  sted from the un
ea4a0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
ea4b0 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d  ng system, a num
ea4c0 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67  ber which.** mig
ea4d0 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68  ht be greater th
ea4e0 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
ea4f0 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74  he argument, but
ea500 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61   not less.** tha
ea510 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a  n the argument..
ea520 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
ea530 69 78 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f  ixSleep(sqlite3_
ea540 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  vfs *NotUsed, in
ea550 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b  t microseconds){
ea560 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
ea570 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65    struct timespe
ea580 63 20 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73  c sp;..  sp.tv_s
ea590 65 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64  ec = microsecond
ea5a0 73 20 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73  s / 1000000;.  s
ea5b0 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63  p.tv_nsec = (mic
ea5c0 72 6f 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30  roseconds % 1000
ea5d0 30 30 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e  000) * 1000;.  n
ea5e0 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55  anosleep(&sp, NU
ea5f0 4c 4c 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  LL);.  UNUSED_PA
ea600 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
ea610 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f  ;.  return micro
ea620 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64  seconds;.#elif d
ea630 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45  efined(HAVE_USLE
ea640 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45  EP) && HAVE_USLE
ea650 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72  EP.  usleep(micr
ea660 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55  oseconds);.  UNU
ea670 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
ea680 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
ea690 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23   microseconds;.#
ea6a0 65 6c 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e  else.  int secon
ea6b0 64 73 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e  ds = (microsecon
ea6c0 64 73 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30  ds+999999)/10000
ea6d0 30 30 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f  00;.  sleep(seco
ea6e0 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  nds);.  UNUSED_P
ea6f0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
ea700 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f  );.  return seco
ea710 6e 64 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e  nds*1000000;.#en
ea720 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  dif.}../*.** The
ea730 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
ea740 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61  ble, if set to a
ea750 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
ea760 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20   is interpreted 
ea770 61 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  as.** the number
ea780 20 6f 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   of seconds sinc
ea790 65 20 31 39 37 30 20 61 6e 64 20 69 73 20 75 73  e 1970 and is us
ea7a0 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 72 65  ed to set the re
ea7b0 73 75 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74  sult of.** sqlit
ea7c0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
ea7d0 29 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67  ) during testing
ea7e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
ea7f0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
ea800 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
ea810 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b  urrent_time = 0;
ea820 20 20 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d    /* Fake system
ea830 20 74 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73   time in seconds
ea840 20 73 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a   since 1970. */.
ea850 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
ea860 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  nd the current t
ea870 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61  ime (in Universa
ea880 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69  l Coordinated Ti
ea890 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a  me).  Write the.
ea8a0 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  ** current time 
ea8b0 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75  and date as a Ju
ea8c0 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20  lian Day number 
ea8d0 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a  into *prNow and.
ea8e0 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65  ** return 0.  Re
ea8f0 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69  turn 1 if the ti
ea900 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e  me and date cann
ea910 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
ea920 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
ea930 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
ea940 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
ea950 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b   double *prNow){
ea960 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
ea970 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
ea980 47 5f 50 4f 49 4e 54 29 0a 20 20 74 69 6d 65 5f  G_POINT).  time_
ea990 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b  t t;.  time(&t);
ea9a0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 28 73  .  *prNow = (((s
ea9b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 74 29 2f  qlite3_int64)t)/
ea9c0 38 36 34 30 20 2b 20 32 34 34 30 35 38 37 35 29  8640 + 24405875)
ea9d0 2f 31 30 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e  /10;.#elif defin
ea9e0 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20  ed(NO_GETTOD).  
ea9f0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65  time_t t;.  time
eaa00 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d  (&t);.  *prNow =
eaa10 20 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34   t/86400.0 + 244
eaa20 30 35 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53  0587.5;.#elif OS
eaa30 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63  _VXWORKS.  struc
eaa40 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b  t timespec sNow;
eaa50 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65  .  clock_gettime
eaa60 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c  (CLOCK_REALTIME,
eaa70 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f   &sNow);.  *prNo
eaa80 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20  w = 2440587.5 + 
eaa90 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30  sNow.tv_sec/8640
eaaa0 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73  0.0 + sNow.tv_ns
eaab0 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 30 30  ec/8640000000000
eaac0 30 2e 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72  0.0;.#else.  str
eaad0 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77  uct timeval sNow
eaae0 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79  ;.  gettimeofday
eaaf0 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70  (&sNow, 0);.  *p
eab00 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35  rNow = 2440587.5
eab10 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38   + sNow.tv_sec/8
eab20 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76  6400.0 + sNow.tv
eab30 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 30 30  _usec/8640000000
eab40 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0.0;.#endif..#if
eab50 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
eab60 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75    if( sqlite3_cu
eab70 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20  rrent_time ){.  
eab80 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74    *prNow = sqlit
eab90 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f  e3_current_time/
eaba0 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38  86400.0 + 244058
eabb0 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  7.5;.  }.#endif.
eabc0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
eabd0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
eabe0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
eabf0 2a 20 57 65 20 61 64 64 65 64 20 74 68 65 20 78  * We added the x
eac00 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d  GetLastError() m
eac10 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 69  ethod with the i
eac20 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76  ntention of prov
eac30 69 64 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20  iding.** better 
eac40 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20  low-level error 
eac50 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70  messages when op
eac60 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70  erating-system p
eac70 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a  roblems come up.
eac80 2a 2a 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65  ** during SQLite
eac90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
eaca0 20 73 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66   so far, none of
eacb0 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 69   that has been i
eacc0 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e  mplemented.** in
eacd0 20 74 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74   the core.  So t
eace0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
eacf0 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f  ever called.  Fo
ead00 72 20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72  r now, it is mer
ead10 65 6c 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68  ely.** a place-h
ead20 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  older..*/.static
ead30 20 69 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74   int unixGetLast
ead40 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
ead50 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  s *NotUsed, int 
ead60 4e 6f 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a  NotUsed2, char *
ead70 4e 6f 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55  NotUsed3){.  UNU
ead80 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
ead90 74 55 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44  tUsed);.  UNUSED
eada0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
eadb0 65 64 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ed2);.  UNUSED_P
eadc0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
eadd0 33 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  3);.  return 0;.
eade0 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../*.**********
eadf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
eae00 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66  nd of sqlite3_vf
eae10 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  s methods ******
eae20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eae30 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
eae40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eae50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eae60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eae70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eae80 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
eae90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaed0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
eaee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaef0 2a 20 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f  * Begin Proxy Lo
eaf00 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cking **********
eaf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaf20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f  ******.**.** Pro
eaf30 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20  xy locking is a 
eaf40 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65  "uber-locking-me
eaf50 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65  thod" in this se
eaf60 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 68  nse:  It uses th
eaf70 65 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69  e.** other locki
eaf80 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65  ng methods on se
eaf90 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c  condary lock fil
eafa0 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69  es.  Proxy locki
eafb0 6e 67 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d  ng is a.** meta-
eafc0 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f  layer over top o
eafd0 66 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  f the primitive 
eafe0 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  locking implemen
eaff0 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a  ted above.  For.
eb000 2a 2a 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20  ** this reason, 
eb010 74 68 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61  the division tha
eb020 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20  t implements of 
eb030 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  proxy locking is
eb040 20 64 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74   deferred.** unt
eb050 69 6c 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66  il late in the f
eb060 69 6c 65 20 28 68 65 72 65 29 20 61 66 74 65 72  ile (here) after
eb070 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65   all of the othe
eb080 72 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61  r I/O methods ha
eb090 76 65 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e  ve.** been defin
eb0a0 65 64 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65  ed - so that the
eb0b0 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69   primitive locki
eb0c0 6e 67 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61  ng methods are a
eb0d0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73  vailable.** as s
eb0e0 65 72 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20  ervices to help 
eb0f0 77 69 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65  with the impleme
eb100 6e 74 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79  ntation of proxy
eb110 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a   locking..**.***
eb120 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  *.**.** The defa
eb130 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  ult locking sche
eb140 6d 65 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73  mes in SQLite us
eb150 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  e byte-range loc
eb160 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74  ks on the.** dat
eb170 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f  abase file to co
eb180 6f 72 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63  ordinate safe, c
eb190 6f 6e 63 75 72 72 65 6e 74 20 61 63 63 65 73 73  oncurrent access
eb1a0 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61   by multiple rea
eb1b0 64 65 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74  ders.** and writ
eb1c0 65 72 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69  ers [http://sqli
eb1d0 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33  te.org/lockingv3
eb1e0 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76  .html].  The fiv
eb1f0 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a  e file locking.*
eb200 2a 20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b  * states (UNLOCK
eb210 45 44 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41  ED, PENDING, SHA
eb220 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45  RED, RESERVED, E
eb230 58 43 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d  XCLUSIVE) are im
eb240 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20  plemented.** as 
eb250 50 4f 53 49 58 20 72 65 61 64 20 26 20 77 72 69  POSIX read & wri
eb260 74 65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69  te locks over fi
eb270 78 65 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74  xed set of locat
eb280 69 6f 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29  ions (via fsctl)
eb290 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20  ,.** on AFP and 
eb2a0 53 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69  SMB only exclusi
eb2b0 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f  ve byte-range lo
eb2c0 63 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  cks are availabl
eb2d0 65 20 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77  e via fsctl.** w
eb2e0 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32  ith _IOWR('z', 2
eb2f0 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61  3, struct ByteRa
eb300 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74  ngeLockPB2) to t
eb310 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20  rack the same 5 
eb320 73 74 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69  states..** To si
eb330 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b  mulate a F_RDLCK
eb340 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 72   on the shared r
eb350 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72  ange, on AFP a r
eb360 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64  andomly selected
eb370 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74  .** address in t
eb380 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 20  he shared range 
eb390 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53  is taken for a S
eb3a0 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
eb3b0 65 6e 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64  entire.** shared
eb3c0 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20   range is taken 
eb3d0 66 6f 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45  for an EXCLUSIVE
eb3e0 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20   lock):.**.**   
eb3f0 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20     PENDING_BYTE 
eb400 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30         0x4000000
eb410 30 09 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20  0..   ..**      
eb420 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20  RESERVED_BYTE   
eb430 20 20 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a      0x40000001.*
eb440 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f 52 41  *      SHARED_RA
eb450 4e 47 45 20 20 20 20 20 20 20 20 30 78 34 30 30  NGE        0x400
eb460 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30  00002 -> 0x40000
eb470 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77  200.**.** This w
eb480 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65  orks well on the
eb490 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74   local file syst
eb4a0 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20  em, but shows a 
eb4b0 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73  nearly 100x.** s
eb4c0 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20  lowdown in read 
eb4d0 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41  performance on A
eb4e0 46 50 20 62 65 63 61 75 73 65 20 74 68 65 20 41  FP because the A
eb4f0 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c  FP client disabl
eb500 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63  es.** the read c
eb510 61 63 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72  ache when byte-r
eb520 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70  ange locks are p
eb530 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e  resent.  Enablin
eb540 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61  g the read.** ca
eb550 63 68 65 20 65 78 70 6f 73 65 73 20 61 20 63 61  che exposes a ca
eb560 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72  che coherency pr
eb570 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72  oblem that is pr
eb580 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20  esent on all OS 
eb590 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e  X.** supported n
eb5a0 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74  etwork file syst
eb5b0 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46  ems.  NFS and AF
eb5c0 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74  P both observe t
eb5d0 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f  he.** close-to-o
eb5e0 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f  pen semantics fo
eb5f0 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65  r ensuring cache
eb600 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68   coherency.** [h
eb610 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65  ttp://nfs.source
eb620 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61  forge.net/#faq_a
eb630 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e  8], which does n
eb640 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a  ot effectively.*
eb650 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 72 65  * address the re
eb660 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63  quirements for c
eb670 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 62 61  oncurrent databa
eb680 73 65 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c  se access by mul
eb690 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73  tiple.** readers
eb6a0 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20   and writers.** 
eb6b0 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62  [http://www.nabb
eb6c0 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e  le.com/SQLite-on
eb6d0 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72  -NFS-cache-coher
eb6e0 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e  ency-td15655701.
eb6f0 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  html]..**.** To 
eb700 61 64 64 72 65 73 73 20 74 68 65 20 70 65 72 66  address the perf
eb710 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68  ormance and cach
eb720 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75  e coherency issu
eb730 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c  es, proxy file l
eb740 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65  ocking.** change
eb750 73 20 74 68 65 20 77 61 79 20 64 61 74 61 62 61  s the way databa
eb760 73 65 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e  se access is con
eb770 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74  trolled by limit
eb780 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a  ing access to a.
eb790 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61  ** single host a
eb7a0 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76  t a time and mov
eb7b0 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f  ing file locks o
eb7c0 66 66 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ff of the databa
eb7d0 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f  se file.** and o
eb7e0 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65  nto a proxy file
eb7f0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69   on the local fi
eb800 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a  le system.  .**.
eb810 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78  **.** Using prox
eb820 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d  y locks.** -----
eb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
eb840 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20  ** C APIs.**.** 
eb850 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
eb860 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65  ntrol(db, dbname
eb870 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43  , SQLITE_SET_LOC
eb880 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20  KPROXYFILE,.**  
eb890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb8a0 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68       <proxy_path
eb8b0 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a  > | ":auto:");.*
eb8c0 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  *  sqlite3_file_
eb8d0 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61  control(db, dbna
eb8e0 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c  me, SQLITE_GET_L
eb8f0 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c  OCKPROXYFILE, &<
eb900 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a  proxy_path>);.**
eb910 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d  .**.** SQL pragm
eb920 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41  as.**.**  PRAGMA
eb930 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
eb940 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f  _proxy_file=<pro
eb950 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f  xy_path> | :auto
eb960 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61  :.**  PRAGMA [da
eb970 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f  tabase.]lock_pro
eb980 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70  xy_file.**.** Sp
eb990 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a  ecifying ":auto:
eb9a0 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20  " means that if 
eb9b0 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68  there is a conch
eb9c0 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74   file with a mat
eb9d0 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44  ching.** host ID
eb9e0 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78   in it, the prox
eb9f0 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f  y path in the co
eba00 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  nch file will be
eba10 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65   used, otherwise
eba20 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68  .** a proxy path
eba30 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73   based on the us
eba40 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a  er's temp dir.**
eba50 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43   (via confstr(_C
eba60 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45  S_DARWIN_USER_TE
eba70 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c  MP_DIR,...)) wil
eba80 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 74 68  l be used and th
eba90 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78  e.** actual prox
ebaa0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67  y file name is g
ebab0 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68  enerated from th
ebac0 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20  e name and path 
ebad0 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
ebae0 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78  se file.  For ex
ebaf0 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
ebb00 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20     For database 
ebb10 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f  path "/Users/me/
ebb20 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20  foo.db" .**     
ebb30 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20    The lock path 
ebb40 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72  will be "<tmpdir
ebb50 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f  >/sqliteplocks/_
ebb60 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a  Users_me_foo.db:
ebb70 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e  auto:").**.** On
ebb80 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20  ce a lock proxy 
ebb90 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  is configured fo
ebba0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
ebbb0 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20  nection, it can 
ebbc0 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65  not.** be remove
ebbd0 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61  d, however it ma
ebbe0 79 20 62 65 20 73 77 69 74 63 68 65 64 20 74 6f  y be switched to
ebbf0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f   a different pro
ebc00 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74  xy path via.** t
ebc10 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 28 61  he above APIs (a
ebc20 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63  ssuming the conc
ebc30 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65  h file is not be
ebc40 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74  ing held by anot
ebc50 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  her.** connectio
ebc60 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a  n or process). .
ebc70 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f  **.**.** How pro
ebc80 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73  xy locking works
ebc90 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
ebca0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
ebcb0 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63  * Proxy file loc
ebcc0 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d  king relies prim
ebcd0 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77  arily on two new
ebce0 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65   supporting file
ebcf0 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63  s: .**.**   *  c
ebd00 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d  onch file to lim
ebd10 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  it access to the
ebd20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
ebd30 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a  o a single host.
ebd40 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d  **      at a tim
ebd50 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f  e.**.**   *  pro
ebd60 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61  xy file to act a
ebd70 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68  s a proxy for th
ebd80 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  e advisory locks
ebd90 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20   normally.**    
ebda0 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64    taken on the d
ebdb0 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68  atabase.**.** Th
ebdc0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74  e conch file - t
ebdd0 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 66 69  o use a proxy fi
ebde0 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20  le, sqlite must 
ebdf0 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20  first "hold the 
ebe00 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b  conch".** by tak
ebe10 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74  ing an sqlite-st
ebe20 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  yle shared lock 
ebe30 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  on the conch fil
ebe40 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a  e, reading the.*
ebe50 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63  * contents and c
ebe60 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73  omparing the hos
ebe70 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20  t's unique host 
ebe80 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61  ID (see below) a
ebe90 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79  nd lock.** proxy
ebea0 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 74 68   path against th
ebeb0 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  e values stored 
ebec0 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54  in the conch.  T
ebed0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73  he conch file is
ebee0 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68  .** stored in th
ebef0 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79  e same directory
ebf00 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65   as the database
ebf10 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69   file and the fi
ebf20 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61  le name.** is pa
ebf30 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 74 68  tterned after th
ebf40 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
ebf50 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62  name as ".<datab
ebf60 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e  asename>-conch".
ebf70 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68  .** If the conch
ebf80 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65   file does not e
ebf90 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f  xist, or it's co
ebfa0 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61  ntents do not ma
ebfb0 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20  tch the.** host 
ebfc0 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20  ID and/or proxy 
ebfd0 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c  path, then the l
ebfe0 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64  ock is escalated
ebff0 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65   to an exclusive
ec000 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65  .** lock and the
ec010 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74   conch file cont
ec020 65 6e 74 73 20 69 73 20 75 70 64 61 74 65 64 20  ents is updated 
ec030 77 69 74 68 20 74 68 65 20 68 6f 73 74 20 49 44  with the host ID
ec040 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61   and proxy.** pa
ec050 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  th and the lock 
ec060 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f  is downgraded to
ec070 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61   a shared lock a
ec080 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f  gain.  If the co
ec090 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62  nch.** is held b
ec0a0 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73  y another proces
ec0b0 73 20 28 77 69 74 68 20 61 20 73 68 61 72 65 64  s (with a shared
ec0c0 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c   lock), the excl
ec0d0 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69  usive lock.** wi
ec0e0 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49  ll fail and SQLI
ec0f0 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72  TE_BUSY is retur
ec100 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ned..**.** The p
ec110 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69  roxy file - a si
ec120 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75  ngle-byte file u
ec130 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69  sed for all advi
ec140 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a  sory file locks.
ec150 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65  ** normally take
ec160 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
ec170 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61  e file.   This a
ec180 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73  llows for safe s
ec190 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65  haring.** of the
ec1a0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
ec1b0 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64  or multiple read
ec1c0 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 20  ers and writers 
ec1d0 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68  on the same.** h
ec1e0 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65  ost (the conch e
ec1f0 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 79  nsures that they
ec200 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d   all use the sam
ec210 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c  e local lock fil
ec220 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  e)..**.** There 
ec230 69 73 20 61 20 74 68 69 72 64 20 66 69 6c 65 20  is a third file 
ec240 2d 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69  - the host ID fi
ec250 6c 65 20 2d 20 75 73 65 64 20 61 73 20 61 20 70  le - used as a p
ec260 65 72 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64  ersistent record
ec270 0a 2a 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20  .** of a unique 
ec280 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74  identifier for t
ec290 68 65 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62  he host, a 128-b
ec2a0 79 74 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20  yte unique host 
ec2b0 69 64 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68  id file.** in th
ec2c0 65 20 70 61 74 68 20 64 65 66 69 6e 65 64 20 62  e path defined b
ec2d0 79 20 74 68 65 20 48 4f 53 54 49 44 50 41 54 48  y the HOSTIDPATH
ec2e0 20 6d 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20   macro (default 
ec2f0 76 61 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62  value is.** /Lib
ec300 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d  rary/Caches/.com
ec310 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e  .apple.sqliteCon
ec320 63 68 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a  chHostId)..**.**
ec330 20 52 65 71 75 65 73 74 69 6e 67 20 74 68 65 20   Requesting the 
ec340 6c 6f 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20  lock proxy does 
ec350 6e 6f 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  not immediately 
ec360 74 61 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20  take the conch, 
ec370 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61  it is.** only ta
ec380 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72  ken when the fir
ec390 73 74 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f  st request to lo
ec3a0 63 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ck database file
ec3b0 20 69 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54   is made.  .** T
ec3c0 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65 20  his matches the 
ec3d0 73 65 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65  semantics of the
ec3e0 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63   traditional loc
ec3f0 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77  king behavior, w
ec400 68 65 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20  here.** opening 
ec410 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  a connection to 
ec420 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
ec430 64 6f 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20  does not take a 
ec440 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54  lock on it..** T
ec450 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61  he shared lock a
ec460 6e 64 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  nd an open file 
ec470 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d  descriptor are m
ec480 61 69 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20  aintained until 
ec490 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  .** the connecti
ec4a0 6f 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  on to the databa
ec4b0 73 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a  se is closed. .*
ec4c0 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66  *.** The proxy f
ec4d0 69 6c 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b  ile and the lock
ec4e0 20 66 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20   file are never 
ec4f0 64 65 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20  deleted so they 
ec500 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20  only need.** to 
ec510 62 65 20 63 72 65 61 74 65 64 20 74 68 65 20 66  be created the f
ec520 69 72 73 74 20 74 69 6d 65 20 74 68 65 79 20 61  irst time they a
ec530 72 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43  re used..**.** C
ec540 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
ec550 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d  ions.** --------
ec560 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
ec570 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46  .**  SQLITE_PREF
ec580 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  ER_PROXY_LOCKING
ec590 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74  .**.**       Dat
ec5a0 61 62 61 73 65 20 66 69 6c 65 73 20 61 63 63 65  abase files acce
ec5b0 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61  ssed on non-loca
ec5c0 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61  l file systems a
ec5d0 72 65 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f  re.**       auto
ec5e0 6d 61 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67  matically config
ec5f0 75 72 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c  ured for proxy l
ec600 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c  ocking, lock fil
ec610 65 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20  es are.**       
ec620 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61  named automatica
ec630 6c 6c 79 20 75 73 69 6e 67 20 74 68 65 20 73 61  lly using the sa
ec640 6d 65 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20  me logic as.**  
ec650 20 20 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b       PRAGMA lock
ec660 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75  _proxy_file=":au
ec670 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20  to:".**    .**  
ec680 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42  SQLITE_PROXY_DEB
ec690 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45  UG.**.**       E
ec6a0 6e 61 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69  nables the loggi
ec6b0 6e 67 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73  ng of error mess
ec6c0 61 67 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74  ages during host
ec6d0 20 69 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20   id file.**     
ec6e0 20 20 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20    retrieval and 
ec6f0 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20  creation.**.**  
ec700 48 4f 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a  HOSTIDPATH.**.**
ec710 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73         Overrides
ec720 20 74 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73   the default hos
ec730 74 20 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c  t ID file path l
ec740 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c  ocation.**.**  L
ec750 4f 43 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a  OCKPROXYDIR.**.*
ec760 2a 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65  *       Override
ec770 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 64 69  s the default di
ec780 72 65 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72  rectory used for
ec790 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65   lock proxy file
ec7a0 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  s that.**       
ec7b0 61 72 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61  are named automa
ec7c0 74 69 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20  tically via the 
ec7d0 22 3a 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67  ":auto:" setting
ec7e0 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44  .**.**  SQLITE_D
ec7f0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
ec800 50 45 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a  PERMISSIONS.**.*
ec810 2a 20 20 20 20 20 20 20 50 65 72 6d 69 73 73 69  *       Permissi
ec820 6f 6e 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20  ons to use when 
ec830 63 72 65 61 74 69 6e 67 20 61 20 64 69 72 65 63  creating a direc
ec840 74 6f 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67  tory for storing
ec850 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f   the.**       lo
ec860 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20  ck proxy files, 
ec870 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c  only used when L
ec880 4f 43 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e  OCKPROXYDIR is n
ec890 6f 74 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a  ot set..**    .*
ec8a0 2a 20 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74  *    .** As ment
ec8b0 69 6f 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65  ioned above, whe
ec8c0 6e 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  n compiled with 
ec8d0 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52  SQLITE_PREFER_PR
ec8e0 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20  OXY_LOCKING,.** 
ec8f0 73 65 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69  setting the envi
ec900 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  ronment variable
ec910 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52   SQLITE_FORCE_PR
ec920 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31  OXY_LOCKING to 1
ec930 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70   will.** force p
ec940 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20  roxy locking to 
ec950 62 65 20 75 73 65 64 20 66 6f 72 20 65 76 65 72  be used for ever
ec960 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  y database file 
ec970 6f 70 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a  opened, and 0.**
ec980 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f   will force auto
ec990 6d 61 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b  matic proxy lock
ec9a0 69 6e 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c  ing to be disabl
ec9b0 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62  ed for all datab
ec9c0 61 73 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78  ase.** files (ex
ec9d0 70 6c 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20  plicity calling 
ec9e0 74 68 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c  the SQLITE_SET_L
ec9f0 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61  OCKPROXYFILE pra
eca00 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  gma or.** sqlite
eca10 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50  _file_control AP
eca20 49 20 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65  I is not affecte
eca30 64 20 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43  d by SQLITE_FORC
eca40 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29  E_PROXY_LOCKING)
eca50 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78  ..*/../*.** Prox
eca60 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c  y locking is onl
eca70 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d  y available on M
eca80 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65  acOSX .*/.#if de
eca90 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
ecaa0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
ecab0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
ecac0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
ecad0 45 53 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20  EST./* simulate 
ecae0 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62  multiple hosts b
ecaf0 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75  y creating uniqu
ecb00 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61  e hostid file pa
ecb10 74 68 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ths */.SQLITE_AP
ecb20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f  I int sqlite3_ho
ecb30 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65  stid_num = 0;.#e
ecb40 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
ecb50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ecb60 65 78 74 20 68 61 73 20 74 68 65 20 70 61 74 68  ext has the path
ecb70 20 61 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74   and file struct
ecb80 75 72 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d  ures for the rem
ecb90 6f 74 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61  ote .** and loca
ecba0 6c 20 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e  l proxy files in
ecbb0 20 69 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73   it.*/.typedef s
ecbc0 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69  truct proxyLocki
ecbd0 6e 67 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c  ngContext proxyL
ecbe0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73  ockingContext;.s
ecbf0 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69  truct proxyLocki
ecc00 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e  ngContext {.  un
ecc10 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c  ixFile *conchFil
ecc20 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70  e;         /* Op
ecc30 65 6e 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f  en conch file */
ecc40 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69  .  char *conchFi
ecc50 6c 65 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  lePath;         
ecc60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
ecc70 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75  onch file */.  u
ecc80 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f  nixFile *lockPro
ecc90 78 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  xy;         /* O
ecca0 70 65 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66  pen proxy lock f
eccb0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c  ile */.  char *l
eccc0 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20  ockProxyPath;   
eccd0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ecce0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20   the proxy lock 
eccf0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
ecd00 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  dbPath;         
ecd10 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
ecd20 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  f the open file 
ecd30 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65  */.  int conchHe
ecd40 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
ecd50 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
ecd60 20 63 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e   conch is curren
ecd70 74 6c 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f  tly held */.  vo
ecd80 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f  id *oldLockingCo
ecd90 6e 74 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72  ntext;     /* Or
ecda0 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f  iginal lockingco
ecdb0 6e 74 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65  ntext to restore
ecdc0 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73   on close */.  s
ecdd0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
ecde0 73 20 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74  s const *pOldMet
ecdf0 68 6f 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  hod;     /* Orig
ece00 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73  inal I/O methods
ece10 20 66 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b   for close */.};
ece20 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61  ../* HOSTIDLEN a
ece30 6e 64 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68  nd CONCHLEN both
ece40 20 69 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66   include space f
ece50 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a  or the string .*
ece60 2a 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75  * terminating nu
ece70 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f  l .*/.#define HO
ece80 53 54 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20  STIDLEN         
ece90 31 32 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43  128.#define CONC
ecea0 48 4c 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d  HLEN          (M
eceb0 41 58 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44  AXPATHLEN+HOSTID
ecec0 4c 45 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48  LEN+1).#ifndef H
eced0 4f 53 54 49 44 50 41 54 48 0a 23 20 64 65 66 69  OSTIDPATH.# defi
ecee0 6e 65 20 48 4f 53 54 49 44 50 41 54 48 20 20 20  ne HOSTIDPATH   
ecef0 20 20 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61      "/Library/Ca
ecf00 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e  ches/.com.apple.
ecf10 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49  sqliteConchHostI
ecf20 64 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61  d".#endif../* ba
ecf30 73 69 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f  sically a copy o
ecf40 66 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73  f unixRandomness
ecf50 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a   with different.
ecf60 2a 2a 20 74 65 73 74 20 62 65 68 61 76 69 6f 72  ** test behavior
ecf70 20 62 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61   built in */.sta
ecf80 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e  tic int proxyGen
ecf90 65 72 61 74 65 48 6f 73 74 49 44 28 63 68 61 72  erateHostID(char
ecfa0 20 2a 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e   *pHostID){.  in
ecfb0 74 20 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a  t pid, fd, len;.
ecfc0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
ecfd0 2a 6b 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64  *key = (unsigned
ecfe0 20 63 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b   char *)pHostID;
ecff0 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79  .  .  memset(key
ed000 2c 20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b  , 0, HOSTIDLEN);
ed010 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64  .  len = 0;.  fd
ed020 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72   = open("/dev/ur
ed030 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59  andom", O_RDONLY
ed040 29 3b 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29  );.  if( fd>=0 )
ed050 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64  {.    len = read
ed060 28 66 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44  (fd, key, HOSTID
ed070 4c 45 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28  LEN);.    close(
ed080 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79  fd); /* silently
ed090 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20   leak the fd if 
ed0a0 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a  it fails */.  }.
ed0b0 20 20 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54    if( len < HOST
ed0c0 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d  IDLEN ){.    tim
ed0d0 65 5f 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28  e_t t;.    time(
ed0e0 26 74 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  &t);.    memcpy(
ed0f0 6b 65 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28  key, &t, sizeof(
ed100 74 29 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67  t));.    pid = g
ed110 65 74 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d  etpid();.    mem
ed120 63 70 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28  cpy(&key[sizeof(
ed130 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f  t)], &pid, sizeo
ed140 66 28 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a  f(pid));.  }.  .
ed150 23 69 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54  #ifdef MAKE_PRET
ed160 54 59 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20  TY_HOSTID.  {.  
ed170 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20    int i;.    /* 
ed180 66 69 6c 74 65 72 20 74 68 65 20 62 79 74 65 73  filter the bytes
ed190 20 69 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20   into printable 
ed1a0 61 73 63 69 69 20 63 68 61 72 61 63 74 65 72 73  ascii characters
ed1b0 20 61 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61   and NUL termina
ed1c0 74 65 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48  te */.    key[(H
ed1d0 4f 53 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30  OSTIDLEN-1)] = 0
ed1e0 78 30 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d  x00;.    for( i=
ed1f0 30 3b 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d  0; i<(HOSTIDLEN-
ed200 31 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20  1); i++ ){.     
ed210 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70   unsigned char p
ed220 61 20 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b  a = key[i]&0x7F;
ed230 0a 20 20 20 20 20 20 69 66 28 20 70 61 3c 30 78  .      if( pa<0x
ed240 32 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65  20 ){.        ke
ed250 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30  y[i] = (key[i]&0
ed260 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70  x80 == 0x80) ? p
ed270 61 2b 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30  a+0x40 : pa+0x20
ed280 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
ed290 28 20 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20  ( pa==0x7F ){.  
ed2a0 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28        key[i] = (
ed2b0 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30  key[i]&0x80 == 0
ed2c0 78 38 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a  x80) ? pa=0x20 :
ed2d0 20 70 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20   pa+0x7E;.      
ed2e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
ed2f0 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  if.  return SQLI
ed300 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69  TE_OK;.}../* wri
ed310 74 65 73 20 74 68 65 20 68 6f 73 74 20 69 64 20  tes the host id 
ed320 70 61 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61  path to path, pa
ed330 74 68 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20  th should be an 
ed340 70 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75  pre-allocated bu
ed350 66 66 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f  ffer.** with eno
ed360 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61 20  ugh space for a 
ed370 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20  path .*/.static 
ed380 76 6f 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73  void proxyGetHos
ed390 74 49 44 50 61 74 68 28 63 68 61 72 20 2a 70 61  tIDPath(char *pa
ed3a0 74 68 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b  th, size_t len){
ed3b0 0a 20 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c  .  strlcpy(path,
ed3c0 20 48 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e   HOSTIDPATH, len
ed3d0 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
ed3e0 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
ed3f0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30  te3_hostid_num>0
ed400 20 29 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66   ){.    char suf
ed410 66 69 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20  fix[2] = "1";.  
ed420 20 20 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75    suffix[0] = su
ed430 66 66 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65  ffix[0] + sqlite
ed440 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20  3_hostid_num;.  
ed450 20 20 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20    strlcat(path, 
ed460 73 75 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20  suffix, len);.  
ed470 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41  }.#endif.  OSTRA
ed480 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 50 41  CE3("GETHOSTIDPA
ed490 54 48 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22  TH  %s pid=%d\n"
ed4a0 2c 20 70 61 74 68 2c 20 67 65 74 70 69 64 28 29  , path, getpid()
ed4b0 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65  );.}../* get the
ed4c0 20 68 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20   host ID from a 
ed4d0 73 71 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69  sqlite hostid fi
ed4e0 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  le stored in the
ed4f0 20 0a 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66   .** user-specif
ed500 69 63 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79  ic tmp directory
ed510 2c 20 63 72 65 61 74 65 20 74 68 65 20 49 44 20  , create the ID 
ed520 69 66 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72  if it's not ther
ed530 65 20 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74  e already .*/.st
ed540 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65  atic int proxyGe
ed550 74 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48  tHostID(char *pH
ed560 6f 73 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72  ostID, int *pErr
ed570 6f 72 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20  or){.  int fd;. 
ed580 20 63 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41   char path[MAXPA
ed590 54 48 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f  THLEN]; .  size_
ed5a0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d  t len;.  int rc=
ed5b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72  SQLITE_OK;..  pr
ed5c0 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68  oxyGetHostIDPath
ed5d0 28 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  (path, MAXPATHLE
ed5e0 4e 29 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20  N);.  /* try to 
ed5f0 63 72 65 61 74 65 20 74 68 65 20 68 6f 73 74 20  create the host 
ed600 49 44 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61  ID file, if it a
ed610 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 72 65  lready exists re
ed620 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ad the contents 
ed630 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70  */.  fd = open(p
ed640 61 74 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57  ath, O_CREAT|O_W
ed650 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36  RONLY|O_EXCL, 06
ed660 34 34 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20  44);.  if( fd<0 
ed670 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65  ){.    int err=e
ed680 72 72 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28  rrno;....    if(
ed690 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a   err!=EEXIST ){.
ed6a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52  #ifdef SQLITE_PR
ed6b0 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74  OXY_DEBUG /* set
ed6c0 20 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f   the sqlite erro
ed6d0 72 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61  r message instea
ed6e0 64 20 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e  d */.      fprin
ed6f0 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69  tf(stderr, "sqli
ed700 74 65 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e  te error creatin
ed710 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25  g host ID file %
ed720 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  s: %s\n",.      
ed730 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74          path, st
ed740 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65  rerror(err));.#e
ed750 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72  ndif.      retur
ed760 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20  n SQLITE_PERM;. 
ed770 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c     }.    /* coul
ed780 64 6e 27 74 20 63 72 65 61 74 65 20 74 68 65 20  dn't create the 
ed790 66 69 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e  file, read it in
ed7a0 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20  stead */.    fd 
ed7b0 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52  = open(path, O_R
ed7c0 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20  DONLY|O_EXCL);. 
ed7d0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23     if( fd<0 ){.#
ed7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f  ifdef SQLITE_PRO
ed7f0 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20  XY_DEBUG /* set 
ed800 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72  the sqlite error
ed810 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64   message instead
ed820 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72   */.      int er
ed830 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  r = errno;.     
ed840 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
ed850 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f   "sqlite error o
ed860 70 65 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66  pening host ID f
ed870 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20  ile %s: %s\n",. 
ed880 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
ed890 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29  h, strerror(err)
ed8a0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
ed8b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
ed8c0 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65  RM;.    }.    le
ed8d0 6e 20 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48  n = pread(fd, pH
ed8e0 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e  ostID, HOSTIDLEN
ed8f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65  , 0);.    if( le
ed900 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45  n<0 ){.      *pE
ed910 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rror = errno;.  
ed920 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ed930 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20  IOERR_READ;.    
ed940 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f  }else if( len<HO
ed950 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20  STIDLEN ){.     
ed960 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   *pError = 0;.  
ed970 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ed980 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
ed990 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73  ;.    }.    clos
ed9a0 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74  e(fd); /* silent
ed9b0 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69  ly leak the fd i
ed9c0 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20  f it fails */.  
ed9d0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48    OSTRACE3("GETH
ed9e0 4f 53 54 49 44 20 20 72 65 61 64 20 25 73 20 70  OSTID  read %s p
ed9f0 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49  id=%d\n", pHostI
eda00 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  D, getpid());.  
eda10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
eda20 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27  else{.    /* we'
eda30 72 65 20 63 72 65 61 74 69 6e 67 20 74 68 65 20  re creating the 
eda40 68 6f 73 74 20 49 44 20 66 69 6c 65 20 28 75 73  host ID file (us
eda50 65 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e  e a random strin
eda60 67 20 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20  g of bytes) */. 
eda70 20 20 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65     proxyGenerate
eda80 48 6f 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b  HostID(pHostID);
eda90 0a 20 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74  .    len = pwrit
edaa0 65 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48  e(fd, pHostID, H
edab0 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20  OSTIDLEN, 0);.  
edac0 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20    if( len<0 ){. 
edad0 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65       *pError = e
edae0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
edaf0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
edb00 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ITE;.    }else i
edb10 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e  f( len<HOSTIDLEN
edb20 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f   ){.      *pErro
edb30 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  r = 0;.      rc 
edb40 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  = SQLITE_IOERR_W
edb50 52 49 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RITE;.    }.    
edb60 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
edb70 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20  lently leak the 
edb80 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a  fd if it fails *
edb90 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  /.    OSTRACE3("
edba0 47 45 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65  GETHOSTID  wrote
edbb0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
edbc0 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29  HostID, getpid()
edbd0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
edbe0 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20  ;.  }.}..static 
edbf0 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b  int proxyGetLock
edc00 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20  Path(const char 
edc10 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c  *dbPath, char *l
edc20 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78  Path, size_t max
edc30 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b  Len){.  int len;
edc40 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20  .  int dbLen;.  
edc50 69 6e 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c  int i;..#ifdef L
edc60 4f 43 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65  OCKPROXYDIR.  le
edc70 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74  n = strlcpy(lPat
edc80 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c  h, LOCKPROXYDIR,
edc90 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a   maxLen);.#else.
edca0 23 20 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57  # ifdef _CS_DARW
edcb0 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52  IN_USER_TEMP_DIR
edcc0 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72  .  {.    confstr
edcd0 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52  (_CS_DARWIN_USER
edce0 5f 54 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68  _TEMP_DIR, lPath
edcf0 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c  , maxLen);.    l
edd00 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61  en = strlcat(lPa
edd10 74 68 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b  th, "sqliteplock
edd20 73 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20  s", maxLen);.   
edd30 20 69 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68   if( mkdir(lPath
edd40 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
edd50 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
edd60 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20  SIONS) ){.      
edd70 2f 2a 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c  /* if mkdir fail
edd80 73 2c 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63  s, handle as loc
edd90 6b 20 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20  k file creation 
edda0 66 61 69 6c 75 72 65 20 2a 2f 0a 23 20 20 69 66  failure */.#  if
eddb0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
eddc0 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d  .      int err =
eddd0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66   errno;.      if
edde0 28 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b  ( err!=EEXIST ){
eddf0 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
ede00 28 73 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47  (stderr, "proxyG
ede10 65 74 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69  etLockPath: mkdi
ede20 72 28 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20  r(%s,0%o) error 
ede30 25 64 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c  %d %s\n", lPath,
ede40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ede50 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
ede60 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
ede70 49 4f 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72  IONS, err, strer
ede80 72 6f 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20  ror(err));.     
ede90 20 7d 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20   }.#  endif.    
edea0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54  }else{.      OST
edeb0 52 41 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41  RACE3("GETLOCKPA
edec0 54 48 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64  TH  mkdir %s pid
eded0 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67  =%d\n", lPath, g
edee0 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a  etpid());.    }.
edef0 20 20 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a      .  }.# else.
edf00 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28    len = strlcpy(
edf10 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20  lPath, "/tmp/", 
edf20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66  maxLen);.# endif
edf30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c  .#endif..  if( l
edf40 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27  Path[len-1]!='/'
edf50 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74   ){.    len = st
edf60 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22  rlcat(lPath, "/"
edf70 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20  , maxLen);.  }. 
edf80 20 0a 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d   .  /* transform
edf90 20 74 68 65 20 64 62 20 70 61 74 68 20 74 6f 20   the db path to 
edfa0 61 20 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e  a unique cache n
edfb0 61 6d 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d  ame */.  dbLen =
edfc0 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50   (int)strlen(dbP
edfd0 61 74 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30  ath);.  for( i=0
edfe0 3b 20 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b  ; i<dbLen && (i+
edff0 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69  len+7)<maxLen; i
ee000 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20  ++){.    char c 
ee010 3d 20 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20  = dbPath[i];.   
ee020 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20   lPath[i+len] = 
ee030 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a  (c=='/')?'_':c;.
ee040 20 20 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65    }.  lPath[i+le
ee050 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63  n]='\0';.  strlc
ee060 61 74 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f  at(lPath, ":auto
ee070 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72  :", maxLen);.  r
ee080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ee090 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
ee0a0 20 61 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20   a new VFS file 
ee0b0 64 65 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72  descriptor (stor
ee0c0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ed in memory obt
ee0d0 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71  ained from.** sq
ee0e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e  lite3_malloc) an
ee0f0 64 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  d open the file 
ee100 6e 61 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20  named "path" in 
ee110 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
ee120 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  tor..**.** The c
ee130 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73  aller is respons
ee140 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f  ible not only fo
ee150 72 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69  r closing the fi
ee160 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a  le descriptor.**
ee170 20 62 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72   but also for fr
ee180 65 65 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79  eeing the memory
ee190 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
ee1a0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
ee1b0 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ptor..*/.static 
ee1c0 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 55  int proxyCreateU
ee1d0 6e 69 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68  nixFile(const ch
ee1e0 61 72 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69  ar *path, unixFi
ee1f0 6c 65 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20  le **ppFile) {. 
ee200 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b   unixFile *pNew;
ee210 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 53  .  int flags = S
ee220 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
ee230 44 42 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  DB|SQLITE_OPEN_C
ee240 52 45 41 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45  REATE|SQLITE_OPE
ee250 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 69  N_READWRITE;.  i
ee260 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
ee270 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  K;.  sqlite3_vfs
ee280 20 64 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 70 4e   dummyVfs;..  pN
ee290 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a  ew = (unixFile *
ee2a0 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
ee2b0 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
ee2c0 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29  );.  if( !pNew )
ee2d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ee2e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
ee2f0 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c   memset(pNew, 0,
ee300 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65   sizeof(unixFile
ee310 29 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 6c 20 75  ));..  /* Call u
ee320 6e 69 78 4f 70 65 6e 28 29 20 74 6f 20 6f 70 65  nixOpen() to ope
ee330 6e 20 74 68 65 20 70 72 6f 78 79 20 66 69 6c 65  n the proxy file
ee340 2e 20 54 68 65 20 66 6c 61 67 73 20 70 61 73 73  . The flags pass
ee350 65 64 20 74 6f 20 75 6e 69 78 4f 70 65 6e 28 29  ed to unixOpen()
ee360 0a 20 20 2a 2a 20 73 75 67 67 65 73 74 20 74 68  .  ** suggest th
ee370 61 74 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e  at the file bein
ee380 67 20 6f 70 65 6e 65 64 20 69 73 20 61 20 22 6d  g opened is a "m
ee390 61 69 6e 20 64 61 74 61 62 61 73 65 22 2e 20 54  ain database". T
ee3a0 68 69 73 20 69 73 0a 20 20 2a 2a 20 6e 65 63 65  his is.  ** nece
ee3b0 73 73 61 72 79 20 61 73 20 6f 74 68 65 72 20 66  ssary as other f
ee3c0 69 6c 65 20 74 79 70 65 73 20 64 6f 20 6e 6f 74  ile types do not
ee3d0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 75 70   necessarily sup
ee3e0 70 6f 72 74 20 6c 6f 63 6b 69 6e 67 2e 20 49 74  port locking. It
ee3f0 0a 20 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20  .  ** is better 
ee400 74 6f 20 75 73 65 20 75 6e 69 78 4f 70 65 6e 28  to use unixOpen(
ee410 29 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 70 65  ) instead of ope
ee420 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 69  ning the file di
ee430 72 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a  rectly with.  **
ee440 20 6f 70 65 6e 28 29 2c 20 61 73 20 75 6e 69 78   open(), as unix
ee450 4f 70 65 6e 28 29 20 73 65 74 73 20 75 70 20 74  Open() sets up t
ee460 68 65 20 76 61 72 69 6f 75 73 20 6d 65 63 68 61  he various mecha
ee470 6e 69 73 6d 73 20 72 65 71 75 69 72 65 64 20 74  nisms required t
ee480 6f 0a 20 20 2a 2a 20 6d 61 6b 65 20 73 75 72 65  o.  ** make sure
ee490 20 61 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65   a call to close
ee4a0 28 29 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73  () does not caus
ee4b0 65 20 74 68 65 20 73 79 73 74 65 6d 20 74 6f 20  e the system to 
ee4c0 64 69 73 63 61 72 64 0a 20 20 2a 2a 20 50 4f 53  discard.  ** POS
ee4d0 49 58 20 6c 6f 63 6b 73 20 70 72 65 6d 61 74 75  IX locks prematu
ee4e0 72 65 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rely..  **.  ** 
ee4f0 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  It is important 
ee500 74 68 61 74 20 74 68 65 20 78 4f 70 65 6e 20 6d  that the xOpen m
ee510 65 6d 62 65 72 20 6f 66 20 74 68 65 20 56 46 53  ember of the VFS
ee520 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 74   object passed t
ee530 6f 20 0a 20 20 2a 2a 20 75 6e 69 78 4f 70 65 6e  o .  ** unixOpen
ee540 28 29 20 69 73 20 4e 55 4c 4c 2e 20 54 68 69 73  () is NULL. This
ee550 20 74 65 6c 6c 73 20 75 6e 69 78 4f 70 65 6e 28   tells unixOpen(
ee560 29 20 6d 61 79 20 74 72 79 20 74 6f 20 6f 70 65  ) may try to ope
ee570 6e 20 61 20 70 72 6f 78 79 2d 66 69 6c 65 20 0a  n a proxy-file .
ee580 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 72 6f    ** for the pro
ee590 78 79 2d 66 69 6c 65 20 28 63 72 65 61 74 69 6e  xy-file (creatin
ee5a0 67 20 61 20 70 6f 74 65 6e 74 69 61 6c 20 69 6e  g a potential in
ee5b0 66 69 6e 69 74 65 20 6c 6f 6f 70 29 2e 0a 20 20  finite loop)..  
ee5c0 2a 2f 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41  */.  dummyVfs.pA
ee5d0 70 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29  ppData = (void*)
ee5e0 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65  &autolockIoFinde
ee5f0 72 3b 0a 20 20 64 75 6d 6d 79 56 66 73 2e 78 4f  r;.  dummyVfs.xO
ee600 70 65 6e 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  pen = 0;.  rc = 
ee610 75 6e 69 78 4f 70 65 6e 28 26 64 75 6d 6d 79 56  unixOpen(&dummyV
ee620 66 73 2c 20 70 61 74 68 2c 20 28 73 71 6c 69 74  fs, path, (sqlit
ee630 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77 2c 20  e3_file *)pNew, 
ee640 66 6c 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a  flags, &flags);.
ee650 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ee660 5f 4f 4b 20 26 26 20 28 66 6c 61 67 73 26 53 51  _OK && (flags&SQ
ee670 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
ee680 4c 59 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  LY) ){.    pNew-
ee690 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
ee6a0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
ee6b0 29 70 4e 65 77 29 3b 0a 20 20 20 20 72 63 20 3d  )pNew);.    rc =
ee6c0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
ee6d0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21  ;.  }..  if( rc!
ee6e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ee6f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
ee700 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d  New);.    pNew =
ee710 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 46 69   0;.  }..  *ppFi
ee720 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 72 65 74  le = pNew;.  ret
ee730 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61  urn rc;.}../* ta
ee740 6b 65 73 20 74 68 65 20 63 6f 6e 63 68 20 62 79  kes the conch by
ee750 20 74 61 6b 69 6e 67 20 61 20 73 68 61 72 65 64   taking a shared
ee760 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 64 20 74   lock and read t
ee770 68 65 20 63 6f 6e 74 65 6e 74 73 20 63 6f 6e 63  he contents conc
ee780 68 2c 20 69 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61  h, if .** lockPa
ee790 74 68 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20  th is non-NULL, 
ee7a0 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20  the host ID and 
ee7b0 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 6d  lock file path m
ee7c0 75 73 74 20 6d 61 74 63 68 2e 20 20 41 20 4e 55  ust match.  A NU
ee7d0 4c 4c 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20  LL .** lockPath 
ee7e0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6c  means that the l
ee7f0 6f 63 6b 50 61 74 68 20 69 6e 20 74 68 65 20 63  ockPath in the c
ee800 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62  onch file will b
ee810 65 20 75 73 65 64 20 69 66 20 74 68 65 20 0a 2a  e used if the .*
ee820 2a 20 68 6f 73 74 20 49 44 73 20 6d 61 74 63 68  * host IDs match
ee830 2c 20 6f 72 20 61 20 6e 65 77 20 6c 6f 63 6b 20  , or a new lock 
ee840 70 61 74 68 20 77 69 6c 6c 20 62 65 20 67 65 6e  path will be gen
ee850 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63  erated automatic
ee860 61 6c 6c 79 20 0a 2a 2a 20 61 6e 64 20 77 72 69  ally .** and wri
ee870 74 74 65 6e 20 74 6f 20 74 68 65 20 63 6f 6e 63  tten to the conc
ee880 68 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  h file..*/.stati
ee890 63 20 69 6e 74 20 70 72 6f 78 79 54 61 6b 65 43  c int proxyTakeC
ee8a0 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  onch(unixFile *p
ee8b0 46 69 6c 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f  File){.  proxyLo
ee8c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
ee8d0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
ee8e0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
ee8f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
ee900 74 3b 20 0a 20 20 0a 20 20 69 66 28 20 70 43 74  t; .  .  if( pCt
ee910 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e 30 20 29  x->conchHeld>0 )
ee920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ee930 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
ee940 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63  .    unixFile *c
ee950 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d  onchFile = pCtx-
ee960 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20  >conchFile;.    
ee970 63 68 61 72 20 74 65 73 74 56 61 6c 75 65 5b 43  char testValue[C
ee980 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68  ONCHLEN];.    ch
ee990 61 72 20 63 6f 6e 63 68 56 61 6c 75 65 5b 43 4f  ar conchValue[CO
ee9a0 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61  NCHLEN];.    cha
ee9b0 72 20 6c 6f 63 6b 50 61 74 68 5b 4d 41 58 50 41  r lockPath[MAXPA
ee9c0 54 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72  THLEN];.    char
ee9d0 20 2a 74 4c 6f 63 6b 50 61 74 68 20 3d 20 4e 55   *tLockPath = NU
ee9e0 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  LL;.    int rc =
ee9f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
eea00 69 6e 74 20 72 65 61 64 52 63 20 3d 20 53 51 4c  int readRc = SQL
eea10 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20  ITE_OK;.    int 
eea20 73 79 6e 63 50 65 72 6d 73 20 3d 20 30 3b 0a 0a  syncPerms = 0;..
eea30 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 54 41      OSTRACE4("TA
eea40 4b 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20  KECONCH  %d for 
eea50 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f  %s pid=%d\n", co
eea60 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  nchFile->h,.    
eea70 20 20 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e           (pCtx->
eea80 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20  lockProxyPath ? 
eea90 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
eeaa0 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c  ath : ":auto:"),
eeab0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 20   getpid());..   
eeac0 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d   rc = conchFile-
eead0 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28  >pMethod->xLock(
eeae0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
eeaf0 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44  onchFile, SHARED
eeb00 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20  _LOCK);.    if( 
eeb10 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
eeb20 0a 20 20 20 20 20 20 69 6e 74 20 70 45 72 72 6f  .      int pErro
eeb30 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d  r = 0;.      mem
eeb40 73 65 74 28 74 65 73 74 56 61 6c 75 65 2c 20 30  set(testValue, 0
eeb50 2c 20 43 4f 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20  , CONCHLEN); /* 
eeb60 63 6f 6e 63 68 20 69 73 20 66 69 78 65 64 20 73  conch is fixed s
eeb70 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  ize */.      rc 
eeb80 3d 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44  = proxyGetHostID
eeb90 28 74 65 73 74 56 61 6c 75 65 2c 20 26 70 45 72  (testValue, &pEr
eeba0 72 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ror);.      if( 
eebb0 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54  (rc&0xff)==SQLIT
eebc0 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20  E_IOERR ){.     
eebd0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
eebe0 72 6e 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20  rno = pError;.  
eebf0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
eec00 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
eec10 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ath ){.        s
eec20 74 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75  trlcpy(&testValu
eec30 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 70 43  e[HOSTIDLEN], pC
eec40 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
eec50 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a  h, MAXPATHLEN);.
eec60 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
eec70 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
eec80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
eec90 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b  o end_takeconch;
eeca0 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
eecb0 72 65 61 64 52 63 20 3d 20 75 6e 69 78 52 65 61  readRc = unixRea
eecc0 64 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  d((sqlite3_file 
eecd0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 63 6f 6e  *)conchFile, con
eece0 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45  chValue, CONCHLE
eecf0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  N, 0);.    if( r
eed00 65 61 64 52 63 21 3d 53 51 4c 49 54 45 5f 49 4f  eadRc!=SQLITE_IO
eed10 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
eed20 7b 0a 20 20 20 20 20 20 69 66 28 20 72 65 61 64  {.      if( read
eed30 52 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  Rc!=SQLITE_OK ){
eed40 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 72 63  .        if( (rc
eed50 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49  &0xff)==SQLITE_I
eed60 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20  OERR ){.        
eed70 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
eed80 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  no = conchFile->
eed90 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20  lastErrno;.     
eeda0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20     }.        rc 
eedb0 3d 20 72 65 61 64 52 63 3b 0a 20 20 20 20 20 20  = readRc;.      
eedc0 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63    goto end_takec
eedd0 6f 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20  onch;.      }.  
eede0 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 63 6f      /* if the co
eedf0 6e 63 68 20 68 61 73 20 64 61 74 61 20 63 6f 6d  nch has data com
eee00 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  pare the content
eee10 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21  s */.      if( !
eee20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
eee30 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ath ){.        /
eee40 2a 20 66 6f 72 20 61 75 74 6f 2d 6e 61 6d 65 64  * for auto-named
eee50 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65   local lock file
eee60 2c 20 6a 75 73 74 20 63 68 65 63 6b 20 74 68 65  , just check the
eee70 20 68 6f 73 74 20 49 44 20 61 6e 64 20 77 65 27   host ID and we'
eee80 6c 6c 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 75  ll.         ** u
eee90 73 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63  se the local loc
eeea0 6b 20 66 69 6c 65 20 70 61 74 68 20 74 68 61 74  k file path that
eeeb0 27 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  's already in th
eeec0 65 72 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ere */.        i
eeed0 66 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56  f( !memcmp(testV
eeee0 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65  alue, conchValue
eeef0 2c 20 48 4f 53 54 49 44 4c 45 4e 29 20 29 7b 0a  , HOSTIDLEN) ){.
eef00 20 20 20 20 20 20 20 20 20 20 74 4c 6f 63 6b 50            tLockP
eef10 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29 26 63  ath = (char *)&c
eef20 6f 6e 63 68 56 61 6c 75 65 5b 48 4f 53 54 49 44  onchValue[HOSTID
eef30 4c 45 4e 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  LEN];.          
eef40 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e  goto end_takecon
eef50 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ch;.        }.  
eef60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
eef70 20 20 20 2f 2a 20 77 65 27 76 65 20 67 6f 74 20     /* we've got 
eef80 74 68 65 20 63 6f 6e 63 68 20 69 66 20 63 6f 6e  the conch if con
eef90 63 68 56 61 6c 75 65 20 6d 61 74 63 68 65 73 20  chValue matches 
eefa0 6f 75 72 20 70 61 74 68 20 61 6e 64 20 68 6f 73  our path and hos
eefb0 74 20 49 44 20 2a 2f 0a 20 20 20 20 20 20 20 20  t ID */.        
eefc0 69 66 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74  if( !memcmp(test
eefd0 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75  Value, conchValu
eefe0 65 2c 20 43 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a  e, CONCHLEN) ){.
eeff0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
ef000 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20  nd_takeconch;.  
ef010 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
ef020 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
ef030 20 2f 2a 20 61 20 73 68 6f 72 74 20 72 65 61 64   /* a short read
ef040 20 6d 65 61 6e 73 20 77 65 27 72 65 20 22 63 72   means we're "cr
ef050 65 61 74 69 6e 67 22 20 74 68 65 20 63 6f 6e 63  eating" the conc
ef060 68 20 28 65 76 65 6e 20 74 68 6f 75 67 68 20 69  h (even though i
ef070 74 20 63 6f 75 6c 64 20 0a 20 20 20 20 20 20 2a  t could .      *
ef080 2a 20 68 61 76 65 20 62 65 65 6e 20 75 73 65 72  * have been user
ef090 2d 69 6e 74 65 72 76 65 6e 74 69 6f 6e 29 2c 20  -intervention), 
ef0a0 69 66 20 77 65 20 61 63 71 75 69 72 65 20 74 68  if we acquire th
ef0b0 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
ef0c0 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 27 6c 6c  ,.      ** we'll
ef0d0 20 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68   try to match th
ef0e0 65 20 63 75 72 72 65 6e 74 20 6f 6e 2d 64 69 73  e current on-dis
ef0f0 6b 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66  k permissions of
ef100 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
ef110 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 79 6e      */.      syn
ef120 63 50 65 72 6d 73 20 3d 20 31 3b 0a 20 20 20 20  cPerms = 1;.    
ef130 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 65 69  }.    .    /* ei
ef140 74 68 65 72 20 63 6f 6e 63 68 20 77 61 73 20 65  ther conch was e
ef150 6d 74 70 79 20 6f 72 20 64 69 64 6e 27 74 20 6d  mtpy or didn't m
ef160 61 74 63 68 20 2a 2f 0a 20 20 20 20 69 66 28 20  atch */.    if( 
ef170 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  !pCtx->lockProxy
ef180 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 70 72  Path ){.      pr
ef190 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 70  oxyGetLockPath(p
ef1a0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 6c 6f 63  Ctx->dbPath, loc
ef1b0 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  kPath, MAXPATHLE
ef1c0 4e 29 3b 0a 20 20 20 20 20 20 74 4c 6f 63 6b 50  N);.      tLockP
ef1d0 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a  ath = lockPath;.
ef1e0 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74        strlcpy(&t
ef1f0 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c  estValue[HOSTIDL
ef200 45 4e 5d 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d  EN], lockPath, M
ef210 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20  AXPATHLEN);.    
ef220 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 75 70  }.    .    /* up
ef230 64 61 74 65 20 63 6f 6e 63 68 20 77 69 74 68 20  date conch with 
ef240 68 6f 73 74 20 61 6e 64 20 70 61 74 68 20 28 74  host and path (t
ef250 68 69 73 20 77 69 6c 6c 20 66 61 69 6c 20 69 66  his will fail if
ef260 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20   other process. 
ef270 20 20 20 20 2a 2a 20 68 61 73 20 61 20 73 68 61      ** has a sha
ef280 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65 61 64 79  red lock already
ef290 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 63 6f  ) */.    rc = co
ef2a0 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
ef2b0 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33  ->xLock((sqlite3
ef2c0 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
ef2d0 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  , EXCLUSIVE_LOCK
ef2e0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
ef2f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ef300 20 20 72 63 20 3d 20 75 6e 69 78 57 72 69 74 65    rc = unixWrite
ef310 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
ef320 29 63 6f 6e 63 68 46 69 6c 65 2c 20 74 65 73 74  )conchFile, test
ef330 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c  Value, CONCHLEN,
ef340 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
ef350 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
ef360 73 79 6e 63 50 65 72 6d 73 20 29 7b 0a 20 20 20  syncPerms ){.   
ef370 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74       struct stat
ef380 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 69 6e   buf;.        in
ef390 74 20 65 72 72 20 3d 20 66 73 74 61 74 28 70 46  t err = fstat(pF
ef3a0 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20  ile->h, &buf);. 
ef3b0 20 20 20 20 20 20 20 69 66 28 20 65 72 72 3d 3d         if( err==
ef3c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  0 ){.          /
ef3d0 2a 20 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74  * try to match t
ef3e0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
ef3f0 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c 20 69 67   permissions, ig
ef400 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 2a 2f 0a  nore failure */.
ef410 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50  #ifndef SQLITE_P
ef420 52 4f 58 59 5f 44 45 42 55 47 0a 20 20 20 20 20  ROXY_DEBUG.     
ef430 20 20 20 20 20 66 63 68 6d 6f 64 28 63 6f 6e 63       fchmod(conc
ef440 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74  hFile->h, buf.st
ef450 5f 6d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20  _mode);.#else.  
ef460 20 20 20 20 20 20 20 20 69 66 28 20 66 63 68 6d          if( fchm
ef470 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  od(conchFile->h,
ef480 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 21 3d 30   buf.st_mode)!=0
ef490 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
ef4a0 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f  int code = errno
ef4b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70  ;.            fp
ef4c0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66  rintf(stderr, "f
ef4d0 63 68 6d 6f 64 20 25 6f 20 46 41 49 4c 45 44 20  chmod %o FAILED 
ef4e0 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 0a 20  with %d %s\n",. 
ef4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef500 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 2e              buf.
ef510 73 74 5f 6d 6f 64 65 2c 20 63 6f 64 65 2c 20 73  st_mode, code, s
ef520 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a  trerror(code));.
ef530 20 20 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65            } else
ef540 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66   {.            f
ef550 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
ef560 66 63 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44  fchmod %o SUCCED
ef570 45 44 5c 6e 22 2c 62 75 66 2e 73 74 5f 6d 6f 64  ED\n",buf.st_mod
ef580 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  e);.          }.
ef590 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
ef5a0 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64           int cod
ef5b0 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  e = errno;.     
ef5c0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ef5d0 65 72 72 2c 20 22 53 54 41 54 20 46 41 49 4c 45  err, "STAT FAILE
ef5e0 44 5b 25 64 5d 20 77 69 74 68 20 25 64 20 25 73  D[%d] with %d %s
ef5f0 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  \n", .          
ef600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef610 65 72 72 2c 20 63 6f 64 65 2c 20 73 74 72 65 72  err, code, strer
ef620 72 6f 72 28 63 6f 64 65 29 29 3b 0a 23 65 6e 64  ror(code));.#end
ef630 69 66 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  if.        }.   
ef640 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
ef650 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
ef660 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69  d->xUnlock((sqli
ef670 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
ef680 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  ile, SHARED_LOCK
ef690 29 3b 0a 20 20 0a 65 6e 64 5f 74 61 6b 65 63 6f  );.  .end_takeco
ef6a0 6e 63 68 3a 0a 20 20 20 20 4f 53 54 52 41 43 45  nch:.    OSTRACE
ef6b0 32 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43  2("TRANSPROXY: C
ef6c0 4c 4f 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69  LOSE  %d\n", pFi
ef6d0 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 69 66 28 20  le->h);.    if( 
ef6e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
ef6f0 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67   pFile->openFlag
ef700 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  s ){.      if( p
ef710 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69  File->h>=0 ){.#i
ef720 66 64 65 66 20 53 54 52 49 43 54 5f 43 4c 4f 53  fdef STRICT_CLOS
ef730 45 5f 45 52 52 4f 52 0a 20 20 20 20 20 20 20 20  E_ERROR.        
ef740 69 66 28 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d  if( close(pFile-
ef750 3e 68 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >h) ){.         
ef760 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
ef770 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
ef780 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
ef790 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a  TE_IOERR_CLOSE;.
ef7a0 20 20 20 20 20 20 20 20 7d 0a 23 65 6c 73 65 0a          }.#else.
ef7b0 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 70 46          close(pF
ef7c0 69 6c 65 2d 3e 68 29 3b 20 2f 2a 20 73 69 6c 65  ile->h); /* sile
ef7d0 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20  ntly leak fd if 
ef7e0 66 61 69 6c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  fail */.#endif. 
ef7f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 46 69       }.      pFi
ef800 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20  le->h = -1;.    
ef810 20 20 69 6e 74 20 66 64 20 3d 20 6f 70 65 6e 28    int fd = open(
ef820 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46  pCtx->dbPath, pF
ef830 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a  ile->openFlags,.
ef840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef850 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55      SQLITE_DEFAU
ef860 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
ef870 4f 4e 53 29 3b 0a 20 20 20 20 20 20 4f 53 54 52  ONS);.      OSTR
ef880 41 43 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59  ACE2("TRANSPROXY
ef890 3a 20 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66  : OPEN  %d\n", f
ef8a0 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64  d);.      if( fd
ef8b0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  >=0 ){.        p
ef8c0 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b 0a 20 20  File->h = fd;.  
ef8d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
ef8e0 20 20 20 72 63 3d 53 51 4c 49 54 45 5f 43 41 4e     rc=SQLITE_CAN
ef8f0 54 4f 50 45 4e 3b 20 2f 2a 20 53 51 4c 49 54 45  TOPEN; /* SQLITE
ef900 5f 42 55 53 59 3f 20 70 72 6f 78 79 54 61 6b 65  _BUSY? proxyTake
ef910 43 6f 6e 63 68 20 63 61 6c 6c 65 64 0a 20 20 20  Conch called.   
ef920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef930 20 20 20 20 20 20 20 20 20 20 20 20 64 75 72 69              duri
ef940 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20  ng locking */.  
ef950 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ef960 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
ef970 4b 20 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b  K && !pCtx->lock
ef980 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 63  Proxy ){.      c
ef990 68 61 72 20 2a 70 61 74 68 20 3d 20 74 4c 6f 63  har *path = tLoc
ef9a0 6b 50 61 74 68 20 3f 20 74 4c 6f 63 6b 50 61 74  kPath ? tLockPat
ef9b0 68 20 3a 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  h : pCtx->lockPr
ef9c0 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 2f  oxyPath;.      /
ef9d0 2a 20 41 43 53 3a 20 4e 65 65 64 20 74 6f 20 6d  * ACS: Need to m
ef9e0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 70 61  ake a copy of pa
ef9f0 74 68 20 73 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a  th sometimes */.
efa00 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
efa10 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70  CreateUnixFile(p
efa20 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 6b  ath, &pCtx->lock
efa30 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20  Proxy);.    }.  
efa40 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
efa50 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 74  _OK ){.      pCt
efa60 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 31  x->conchHeld = 1
efa70 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 74 4c 6f  ;..      if( tLo
efa80 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20  ckPath ){.      
efa90 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78    pCtx->lockProx
efaa0 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44  yPath = sqlite3D
efab0 62 53 74 72 44 75 70 28 30 2c 20 74 4c 6f 63 6b  bStrDup(0, tLock
efac0 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69  Path);.        i
efad0 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f  f( pCtx->lockPro
efae0 78 79 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26  xy->pMethod == &
efaf0 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a  afpIoMethods ){.
efb00 20 20 20 20 20 20 20 20 20 20 28 28 61 66 70 4c            ((afpL
efb10 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
efb20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d  pCtx->lockProxy-
efb30 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29  >lockingContext)
efb40 2d 3e 64 62 50 61 74 68 20 3d 0a 20 20 20 20 20  ->dbPath =.     
efb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efb60 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
efb70 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ath;.        }. 
efb80 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73       }.    } els
efb90 65 20 7b 0a 20 20 20 20 20 20 63 6f 6e 63 68 46  e {.      conchF
efba0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55  ile->pMethod->xU
efbb0 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  nlock((sqlite3_f
efbc0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  ile*)conchFile, 
efbd0 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a  NO_LOCK);.    }.
efbe0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 41      OSTRACE3("TA
efbf0 4b 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e  KECONCH  %d %s\n
efc00 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  ", conchFile->h,
efc10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22   rc==SQLITE_OK?"
efc20 6f 6b 22 3a 22 66 61 69 6c 65 64 22 29 3b 0a 20  ok":"failed");. 
efc30 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
efc40 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46  }.}../*.** If pF
efc50 69 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b  ile holds a lock
efc60 20 6f 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65   on a conch file
efc70 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74  , then release t
efc80 68 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61  hat lock..*/.sta
efc90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c  tic int proxyRel
efca0 65 61 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69  easeConch(unixFi
efcb0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
efcc0 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
efcd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62            /* Sub
efce0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63  routine return c
efcf0 6f 64 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f  ode */.  proxyLo
efd00 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
efd10 74 78 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b  tx;  /* The lock
efd20 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ing context for 
efd30 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a  the proxy lock *
efd40 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f  /.  unixFile *co
efd50 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20  nchFile;        
efd60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
efd70 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  onch file */..  
efd80 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63  pCtx = (proxyLoc
efd90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46  kingContext *)pF
efda0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
efdb0 65 78 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65  ext;.  conchFile
efdc0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
efdd0 6c 65 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22  le;.  OSTRACE4("
efde0 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64  RELEASECONCH  %d
efdf0 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e   for %s pid=%d\n
efe00 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  ", conchFile->h,
efe10 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74  .           (pCt
efe20 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
efe30 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   ? pCtx->lockPro
efe40 78 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a  xyPath : ":auto:
efe50 22 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  "), .           
efe60 67 65 74 70 69 64 28 29 29 3b 0a 20 20 70 43 74  getpid());.  pCt
efe70 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30  x->conchHeld = 0
efe80 3b 0a 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69  ;.  rc = conchFi
efe90 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e  le->pMethod->xUn
efea0 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69  lock((sqlite3_fi
efeb0 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e  le*)conchFile, N
efec0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 4f 53 54 52 41  O_LOCK);.  OSTRA
efed0 43 45 33 28 22 52 45 4c 45 41 53 45 43 4f 4e 43  CE3("RELEASECONC
efee0 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e  H  %d %s\n", con
efef0 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20  chFile->h,.     
eff00 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54        (rc==SQLIT
eff10 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
eff20 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75  ailed"));.  retu
eff30 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
eff40 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  Given the name o
eff50 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  f a database fil
eff60 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e  e, compute the n
eff70 61 6d 65 20 6f 66 20 69 74 73 20 63 6f 6e 63 68  ame of its conch
eff80 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72 65 20   file..** Store 
eff90 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61  the conch filena
effa0 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  me in memory obt
effb0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
effc0 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20  e3_malloc()..** 
effd0 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 61 74 68  Make *pConchPath
effe0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
efff0 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 20  w name.  Return 
f0000 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
f0010 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  cess.** or SQLIT
f0020 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e 61 62 6c  E_NOMEM if unabl
f0030 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d 65 6d 6f  e to obtain memo
f0040 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ry..**.** The ca
f0050 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
f0060 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67  ble for ensuring
f0070 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61   that the alloca
f0080 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70  ted memory.** sp
f0090 61 63 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c  ace is eventuall
f00a0 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a  y freed..**.** *
f00b0 70 43 6f 6e 63 68 50 61 74 68 20 69 73 20 73 65  pConchPath is se
f00c0 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20 61 20 6d  t to NULL if a m
f00d0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
f00e0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
f00f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
f0100 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74  xyCreateConchPat
f0110 68 6e 61 6d 65 28 63 68 61 72 20 2a 64 62 50 61  hname(char *dbPa
f0120 74 68 2c 20 63 68 61 72 20 2a 2a 70 43 6f 6e 63  th, char **pConc
f0130 68 50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 3b  hPath){.  int i;
f0140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f0150 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
f0160 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
f0170 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c   len = (int)strl
f0180 65 6e 28 64 62 50 61 74 68 29 3b 20 2f 2a 20 4c  en(dbPath); /* L
f0190 65 6e 67 74 68 20 6f 66 20 64 61 74 61 62 61 73  ength of databas
f01a0 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20 64 62 50  e filename - dbP
f01b0 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63  ath */.  char *c
f01c0 6f 6e 63 68 50 61 74 68 3b 20 20 20 20 20 20 20  onchPath;       
f01d0 20 20 20 20 20 20 20 2f 2a 20 62 75 66 66 65 72         /* buffer
f01e0 20 69 6e 20 77 68 69 63 68 20 74 6f 20 63 6f 6e   in which to con
f01f0 73 74 72 75 63 74 20 63 6f 6e 63 68 20 6e 61 6d  struct conch nam
f0200 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  e */..  /* Alloc
f0210 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74 68  ate space for th
f0220 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65  e conch filename
f0230 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
f0240 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a  the name to.  **
f0250 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
f0260 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
f0270 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 0a 20 20  se file. */  .  
f0280 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d 20 63 6f  *pConchPath = co
f0290 6e 63 68 50 61 74 68 20 3d 20 28 63 68 61 72 20  nchPath = (char 
f02a0 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
f02b0 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20 69 66 28  (len + 8);.  if(
f02c0 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 20 29 7b   conchPath==0 ){
f02d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f02e0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
f02f0 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50 61 74 68  memcpy(conchPath
f0300 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e 2b 31 29  , dbPath, len+1)
f0310 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e  ;.  .  /* now in
f0320 73 65 72 74 20 61 20 22 2e 22 20 62 65 66 6f 72  sert a "." befor
f0330 65 20 74 68 65 20 6c 61 73 74 20 2f 20 63 68 61  e the last / cha
f0340 72 61 63 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28  racter */.  for(
f0350 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30   i=(len-1); i>=0
f0360 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20 69 66 28  ; i-- ){.    if(
f0370 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 3d 27   conchPath[i]=='
f0380 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 2b 2b 3b  /' ){.      i++;
f0390 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
f03a0 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63 68 50    }.  }.  conchP
f03b0 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20 77 68  ath[i]='.';.  wh
f03c0 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29 7b 0a 20  ile ( i<len ){. 
f03d0 20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31     conchPath[i+1
f03e0 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20  ]=dbPath[i];.   
f03f0 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   i++;.  }..  /* 
f0400 61 70 70 65 6e 64 20 74 68 65 20 22 2d 63 6f 6e  append the "-con
f0410 63 68 22 20 73 75 66 66 69 78 20 74 6f 20 74 68  ch" suffix to th
f0420 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63  e file */.  memc
f0430 70 79 28 26 63 6f 6e 63 68 50 61 74 68 5b 69 2b  py(&conchPath[i+
f0440 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c 20 37 29  1], "-conch", 7)
f0450 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 74  ;.  assert( (int
f0460 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68 50 61 74  )strlen(conchPat
f0470 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a  h) == len+7 );..
f0480 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f0490 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73  OK;.}.../* Takes
f04a0 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66 69 67 75   a fully configu
f04b0 72 65 64 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  red proxy lockin
f04c0 67 2d 73 74 79 6c 65 20 75 6e 69 78 20 66 69 6c  g-style unix fil
f04d0 65 20 61 6e 64 20 73 77 69 74 63 68 65 73 0a 2a  e and switches.*
f04e0 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b  * the local lock
f04f0 20 66 69 6c 65 20 70 61 74 68 20 0a 2a 2f 0a 73   file path .*/.s
f0500 74 61 74 69 63 20 69 6e 74 20 73 77 69 74 63 68  tatic int switch
f0510 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 75 6e  LockProxyPath(un
f0520 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63  ixFile *pFile, c
f0530 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29  onst char *path)
f0540 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e   {.  proxyLockin
f0550 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
f0560 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
f0570 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f  ntext*)pFile->lo
f0580 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f0590 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 20 3d 20  char *oldPath = 
f05a0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f05b0 61 74 68 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ath;.  int rc = 
f05c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
f05d0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
f05e0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e!=NO_LOCK ){.  
f05f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f0600 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f  BUSY;.  }  ..  /
f0610 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  * nothing to do 
f0620 69 66 20 74 68 65 20 70 61 74 68 20 69 73 20 4e  if the path is N
f0630 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f 72 20 6d  ULL, :auto: or m
f0640 61 74 63 68 65 73 20 74 68 65 20 65 78 69 73 74  atches the exist
f0650 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20 20 69 66  ing path */.  if
f0660 28 20 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b  ( !path || path[
f0670 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72  0]=='\0' || !str
f0680 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f  cmp(path, ":auto
f0690 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f 6c 64 50  :") ||.    (oldP
f06a0 61 74 68 20 26 26 20 21 73 74 72 6e 63 6d 70 28  ath && !strncmp(
f06b0 6f 6c 64 50 61 74 68 2c 20 70 61 74 68 2c 20 4d  oldPath, path, M
f06c0 41 58 50 41 54 48 4c 45 4e 29 29 20 29 7b 0a 20  AXPATHLEN)) ){. 
f06d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f06e0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
f06f0 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b    unixFile *lock
f0700 50 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f  Proxy = pCtx->lo
f0710 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 70 43 74  ckProxy;.    pCt
f0720 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c  x->lockProxy=NUL
f0730 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e  L;.    pCtx->con
f0740 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 20 20  chHeld = 0;.    
f0750 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e  if( lockProxy!=N
f0760 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 3d  ULL ){.      rc=
f0770 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
f0780 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69  od->xClose((sqli
f0790 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f 63 6b 50  te3_file *)lockP
f07a0 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28  roxy);.      if(
f07b0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f07c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
f07d0 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a  ree(lockProxy);.
f07e0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
f07f0 33 5f 66 72 65 65 28 6f 6c 64 50 61 74 68 29 3b  3_free(oldPath);
f0800 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  .    pCtx->lockP
f0810 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74  roxyPath = sqlit
f0820 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 70 61  e3DbStrDup(0, pa
f0830 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65  th);.  }.  .  re
f0840 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f0850 2a 20 70 46 69 6c 65 20 69 73 20 61 20 66 69 6c  * pFile is a fil
f0860 65 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  e that has been 
f0870 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f  opened by a prio
f0880 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e 20 20 64  r xOpen call.  d
f0890 62 50 61 74 68 0a 2a 2a 20 69 73 20 61 20 73 74  bPath.** is a st
f08a0 72 69 6e 67 20 62 75 66 66 65 72 20 61 74 20 6c  ring buffer at l
f08b0 65 61 73 74 20 4d 41 58 50 41 54 48 4c 45 4e 2b  east MAXPATHLEN+
f08c0 31 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  1 characters in 
f08d0 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  size..**.** This
f08e0 20 72 6f 75 74 69 6e 65 20 66 69 6e 64 20 74 68   routine find th
f08f0 65 20 66 69 6c 65 6e 61 6d 65 20 61 73 73 6f 63  e filename assoc
f0900 69 61 74 65 64 20 77 69 74 68 20 70 46 69 6c 65  iated with pFile
f0910 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 0a 2a   and writes it.*
f0920 2a 20 69 6e 74 20 64 62 50 61 74 68 2e 0a 2a 2f  * int dbPath..*/
f0930 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
f0940 79 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69  yGetDbPathForUni
f0950 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a  xFile(unixFile *
f0960 70 46 69 6c 65 2c 20 63 68 61 72 20 2a 64 62 50  pFile, char *dbP
f0970 61 74 68 29 7b 0a 23 69 66 20 64 65 66 69 6e 65  ath){.#if define
f0980 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 69  d(__APPLE__).  i
f0990 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  f( pFile->pMetho
f09a0 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f  d == &afpIoMetho
f09b0 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 61 66 70  ds ){.    /* afp
f09c0 20 73 74 79 6c 65 20 6b 65 65 70 73 20 61 20 72   style keeps a r
f09d0 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
f09e0 64 62 20 70 61 74 68 20 69 6e 20 74 68 65 20 66  db path in the f
f09f0 69 6c 65 50 61 74 68 20 66 69 65 6c 64 20 0a 20  ilePath field . 
f0a00 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 74 72     ** of the str
f0a10 75 63 74 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  uct */.    asser
f0a20 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 28  t( (int)strlen((
f0a30 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  char*)pFile->loc
f0a40 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41  kingContext)<=MA
f0a50 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20  XPATHLEN );.    
f0a60 73 74 72 63 70 79 28 64 62 50 61 74 68 2c 20 28  strcpy(dbPath, (
f0a70 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
f0a80 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
f0a90 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50  ingContext)->dbP
f0aa0 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  ath);.  }else.#e
f0ab0 6e 64 69 66 0a 20 20 69 66 28 20 70 46 69 6c 65  ndif.  if( pFile
f0ac0 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64 6f  ->pMethod == &do
f0ad0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29  tlockIoMethods )
f0ae0 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f 63  {.    /* dot loc
f0af0 6b 20 73 74 79 6c 65 20 75 73 65 73 20 74 68 65  k style uses the
f0b00 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
f0b10 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64 6f   to store the do
f0b20 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66 69  t lock.    ** fi
f0b30 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 69  le path */.    i
f0b40 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28  nt len = strlen(
f0b50 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c  (char *)pFile->l
f0b60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 2d  ockingContext) -
f0b70 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f   strlen(DOTLOCK_
f0b80 53 55 46 46 49 58 29 3b 0a 20 20 20 20 6d 65 6d  SUFFIX);.    mem
f0b90 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 61  cpy(dbPath, (cha
f0ba0 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  r *)pFile->locki
f0bb0 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 2b  ngContext, len +
f0bc0 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   1);.  }else{.  
f0bd0 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20 73    /* all other s
f0be0 74 79 6c 65 73 20 75 73 65 20 74 68 65 20 6c 6f  tyles use the lo
f0bf0 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f  cking context to
f0c00 20 73 74 6f 72 65 20 74 68 65 20 64 62 20 66 69   store the db fi
f0c10 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 61  le path */.    a
f0c20 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28 28 63  ssert( strlen((c
f0c30 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  har*)pFile->lock
f0c40 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58  ingContext)<=MAX
f0c50 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73  PATHLEN );.    s
f0c60 74 72 63 70 79 28 64 62 50 61 74 68 2c 20 28 63  trcpy(dbPath, (c
f0c70 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  har *)pFile->loc
f0c80 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20  kingContext);.  
f0c90 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
f0ca0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
f0cb0 61 6b 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20  akes an already 
f0cc0 66 69 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66  filled in unix f
f0cd0 69 6c 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69  ile and alters i
f0ce0 74 20 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f  t so all file lo
f0cf0 63 6b 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62  cking .** will b
f0d00 65 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74  e performed on t
f0d10 68 65 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c  he local proxy l
f0d20 6f 63 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66  ock file.  The f
f0d30 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a  ollowing fields.
f0d40 2a 2a 20 61 72 65 20 70 72 65 73 65 72 76 65 64  ** are preserved
f0d50 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   in the locking 
f0d60 63 6f 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20  context so that 
f0d70 74 68 65 79 20 63 61 6e 20 62 65 20 72 65 73 74  they can be rest
f0d80 6f 72 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65  ored and .** the
f0d90 20 75 6e 69 78 20 73 74 72 75 63 74 75 72 65 20   unix structure 
f0da0 70 72 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64  properly cleaned
f0db0 20 75 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d   up at close tim
f0dc0 65 3a 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67  e:.**  ->locking
f0dd0 43 6f 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d  Context.**  ->pM
f0de0 65 74 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20  ethod.*/.static 
f0df0 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f  int proxyTransfo
f0e00 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46  rmUnixFile(unixF
f0e10 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73  ile *pFile, cons
f0e20 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a  t char *path) {.
f0e30 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
f0e40 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63  ntext *pCtx;.  c
f0e50 68 61 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41  har dbPath[MAXPA
f0e60 54 48 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20  THLEN+1];       
f0e70 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
f0e80 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
f0e90 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68    char *lockPath
f0ea0 3d 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20  =NULL;.  int rc 
f0eb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a  = SQLITE_OK;.  .
f0ec0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
f0ed0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype!=NO_LOCK )
f0ee0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
f0ef0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
f0f00 70 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f  proxyGetDbPathFo
f0f10 72 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c  rUnixFile(pFile,
f0f20 20 64 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20   dbPath);.  if( 
f0f30 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d  !path || path[0]
f0f40 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d  =='\0' || !strcm
f0f50 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22  p(path, ":auto:"
f0f60 29 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74  ) ){.    lockPat
f0f70 68 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b  h=NULL;.  }else{
f0f80 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63  .    lockPath=(c
f0f90 68 61 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a  har *)path;.  }.
f0fa0 20 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54    .  OSTRACE4("T
f0fb0 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f  RANSPROXY  %d fo
f0fc0 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20  r %s pid=%d\n", 
f0fd0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
f0fe0 20 20 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f       (lockPath ?
f0ff0 20 6c 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75   lockPath : ":au
f1000 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29  to:"), getpid())
f1010 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69  ;..  pCtx = sqli
f1020 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
f1030 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69  of(*pCtx) );.  i
f1040 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20  f( pCtx==0 ){.  
f1050 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f1060 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
f1070 73 65 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a  set(pCtx, 0, siz
f1080 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20  eof(*pCtx));..  
f1090 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65  rc = proxyCreate
f10a0 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62  ConchPathname(db
f10b0 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e  Path, &pCtx->con
f10c0 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69  chFilePath);.  i
f10d0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f10e0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f   ){.    rc = pro
f10f0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65  xyCreateUnixFile
f1100 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
f1110 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e  Path, &pCtx->con
f1120 63 68 46 69 6c 65 29 3b 0a 20 20 7d 20 20 0a 20  chFile);.  }  . 
f1130 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
f1140 4f 4b 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29  OK && lockPath )
f1150 7b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b  {.    pCtx->lock
f1160 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69  ProxyPath = sqli
f1170 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c  te3DbStrDup(0, l
f1180 6f 63 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20  ockPath);.  }.. 
f1190 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
f11a0 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c  OK ){.    /* all
f11b0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63   memory is alloc
f11c0 61 74 65 64 2c 20 70 72 6f 78 79 73 20 61 72 65  ated, proxys are
f11d0 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 73 73   created and ass
f11e0 69 67 6e 65 64 2c 20 0a 20 20 20 20 2a 2a 20 73  igned, .    ** s
f11f0 77 69 74 63 68 20 74 68 65 20 6c 6f 63 6b 69 6e  witch the lockin
f1200 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d  g context and pM
f1210 65 74 68 6f 64 20 74 68 65 6e 20 72 65 74 75 72  ethod then retur
f1220 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43  n..    */.    pC
f1230 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 73 71 6c  tx->dbPath = sql
f1240 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20  ite3DbStrDup(0, 
f1250 64 62 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74  dbPath);.    pCt
f1260 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e  x->oldLockingCon
f1270 74 65 78 74 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  text = pFile->lo
f1280 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f1290 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67    pFile->locking
f12a0 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 3b 0a  Context = pCtx;.
f12b0 20 20 20 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65      pCtx->pOldMe
f12c0 74 68 6f 64 20 3d 20 70 46 69 6c 65 2d 3e 70 4d  thod = pFile->pM
f12d0 65 74 68 6f 64 3b 0a 20 20 20 20 70 46 69 6c 65  ethod;.    pFile
f12e0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 70 72 6f  ->pMethod = &pro
f12f0 78 79 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  xyIoMethods;.  }
f1300 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 43  else{.    if( pC
f1310 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 20 29 7b  tx->conchFile ){
f1320 20 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 74   .      rc = pCt
f1330 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d  x->conchFile->pM
f1340 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
f1350 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 43  qlite3_file *)pC
f1360 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a  tx->conchFile);.
f1370 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
f1380 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
f1390 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f13a0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20  x->conchFile);. 
f13b0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
f13c0 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63  _free(pCtx->conc
f13d0 68 46 69 6c 65 50 61 74 68 29 3b 20 0a 20 20 20  hFilePath); .   
f13e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
f13f0 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  tx);.  }.  OSTRA
f1400 43 45 33 28 22 54 52 41 4e 53 50 52 4f 58 59 20  CE3("TRANSPROXY 
f1410 20 25 64 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %d %s\n", pFile
f1420 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
f1430 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f  (rc==SQLITE_OK ?
f1440 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
f1450 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ));.  return rc;
f1460 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
f1470 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20  routine handles 
f1480 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
f1490 74 72 6f 6c 28 29 20 63 61 6c 6c 73 20 74 68 61  trol() calls tha
f14a0 74 20 61 72 65 20 73 70 65 63 69 66 69 63 0a 2a  t are specific.*
f14b0 2a 20 74 6f 20 70 72 6f 78 79 20 6c 6f 63 6b 69  * to proxy locki
f14c0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
f14d0 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72  t proxyFileContr
f14e0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
f14f0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
f1500 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74  d *pArg){.  swit
f1510 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
f1520 73 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f  se SQLITE_GET_LO
f1530 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20  CKPROXYFILE: {. 
f1540 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70       unixFile *p
f1550 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
f1560 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 66 28 20  *)id;.      if( 
f1570 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
f1580 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  = &proxyIoMethod
f1590 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 6f  s ){.        pro
f15a0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f15b0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
f15c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70  ockingContext*)p
f15d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f15e0 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 72  text;.        pr
f15f0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f1600 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  le);.        if(
f1610 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f1620 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20  Path ){.        
f1630 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a    *(const char *
f1640 2a 29 70 41 72 67 20 3d 20 70 43 74 78 2d 3e 6c  *)pArg = pCtx->l
f1650 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20  ockProxyPath;.  
f1660 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
f1670 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63         *(const c
f1680 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 22 3a  har **)pArg = ":
f1690 61 75 74 6f 3a 20 28 6e 6f 74 20 68 65 6c 64 29  auto: (not held)
f16a0 22 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ";.        }.   
f16b0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
f16c0 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72      *(const char
f16d0 20 2a 2a 29 70 41 72 67 20 3d 20 4e 55 4c 4c 3b   **)pArg = NULL;
f16e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
f16f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f1700 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
f1710 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50  SQLITE_SET_LOCKP
f1720 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20  ROXYFILE: {.    
f1730 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f1740 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f1750 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20  d;.      int rc 
f1760 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
f1770 20 20 20 69 6e 74 20 69 73 50 72 6f 78 79 53 74     int isProxySt
f1780 79 6c 65 20 3d 20 28 70 46 69 6c 65 2d 3e 70 4d  yle = (pFile->pM
f1790 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49  ethod == &proxyI
f17a0 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 20 20 20 20  oMethods);.     
f17b0 20 69 66 28 20 70 41 72 67 3d 3d 4e 55 4c 4c 20   if( pArg==NULL 
f17c0 7c 7c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  || (const char *
f17d0 29 70 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20  )pArg==0 ){.    
f17e0 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53      if( isProxyS
f17f0 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  tyle ){.        
f1800 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72    /* turn off pr
f1810 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f  oxy locking - no
f1820 74 20 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20  t supported */. 
f1830 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
f1840 4c 49 54 45 5f 45 52 52 4f 52 20 2f 2a 53 51 4c  LITE_ERROR /*SQL
f1850 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 3f 20 53 51  ITE_PROTOCOL? SQ
f1860 4c 49 54 45 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a  LITE_MISUSE?*/;.
f1870 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
f1880 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e           /* turn
f1890 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69   off proxy locki
f18a0 6e 67 20 2d 20 61 6c 72 65 61 64 79 20 6f 66 66  ng - already off
f18b0 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20 20 20 20 20   - NOOP */.     
f18c0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
f18d0 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  _OK;.        }. 
f18e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
f18f0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
f1900 70 72 6f 78 79 50 61 74 68 20 3d 20 28 63 6f 6e  proxyPath = (con
f1910 73 74 20 63 68 61 72 20 2a 29 70 41 72 67 3b 0a  st char *)pArg;.
f1920 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72          if( isPr
f1930 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20  oxyStyle ){.    
f1940 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69        proxyLocki
f1950 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
f1960 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  = .            (
f1970 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1980 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ext*)pFile->lock
f1990 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f19a0 20 20 20 20 20 20 69 66 28 20 21 73 74 72 63 6d        if( !strcm
f19b0 70 28 70 41 72 67 2c 20 22 3a 61 75 74 6f 3a 22  p(pArg, ":auto:"
f19c0 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  ) .           ||
f19d0 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78   (pCtx->lockProx
f19e0 79 50 61 74 68 20 26 26 0a 20 20 20 20 20 20 20  yPath &&.       
f19f0 20 20 20 20 20 20 20 20 21 73 74 72 6e 63 6d 70          !strncmp
f1a00 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  (pCtx->lockProxy
f1a10 50 61 74 68 2c 20 70 72 6f 78 79 50 61 74 68 2c  Path, proxyPath,
f1a20 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 0a 20 20   MAXPATHLEN)).  
f1a30 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
f1a40 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
f1a50 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  TE_OK;.         
f1a60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f1a70 20 20 20 20 72 63 20 3d 20 73 77 69 74 63 68 4c      rc = switchL
f1a80 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 70 46 69  ockProxyPath(pFi
f1a90 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a  le, proxyPath);.
f1aa0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
f1ab0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
f1ac0 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 6e 20       /* turn on 
f1ad0 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69  proxy file locki
f1ae0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ng */.          
f1af0 72 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66  rc = proxyTransf
f1b00 6f 72 6d 55 6e 69 78 46 69 6c 65 28 70 46 69 6c  ormUnixFile(pFil
f1b10 65 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20  e, proxyPath);. 
f1b20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
f1b30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
f1b40 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
f1b50 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
f1b60 65 72 74 28 20 30 20 29 3b 20 20 2f 2a 20 54 68  ert( 0 );  /* Th
f1b70 65 20 63 61 6c 6c 20 61 73 73 75 72 65 73 20 74  e call assures t
f1b80 68 61 74 20 6f 6e 6c 79 20 76 61 6c 69 64 20 6f  hat only valid o
f1b90 70 63 6f 64 65 73 20 61 72 65 20 73 65 6e 74 20  pcodes are sent 
f1ba0 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  */.    }.  }.  /
f1bb0 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20  *NOTREACHED*/.  
f1bc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
f1bd0 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69  ROR;.}../*.** Wi
f1be0 74 68 69 6e 20 74 68 69 73 20 64 69 76 69 73 69  thin this divisi
f1bf0 6f 6e 20 28 74 68 65 20 70 72 6f 78 79 69 6e 67  on (the proxying
f1c00 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65   locking impleme
f1c10 6e 74 61 74 69 6f 6e 29 20 74 68 65 20 70 72 6f  ntation) the pro
f1c20 63 65 64 75 72 65 73 0a 2a 2a 20 61 62 6f 76 65  cedures.** above
f1c30 20 74 68 69 73 20 70 6f 69 6e 74 20 61 72 65 20   this point are 
f1c40 61 6c 6c 20 75 74 69 6c 69 74 69 65 73 2e 20 20  all utilities.  
f1c50 54 68 65 20 6c 6f 63 6b 2d 72 65 6c 61 74 65 64  The lock-related
f1c60 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 0a   methods of the.
f1c70 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67  ** proxy-locking
f1c80 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
f1c90 6f 64 20 6f 62 6a 65 63 74 20 66 6f 6c 6c 6f 77  od object follow
f1ca0 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ..*/.../*.** Thi
f1cb0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
f1cc0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
f1cd0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
f1ce0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
f1cf0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
f1d00 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
f1d10 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
f1d20 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
f1d30 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a   set *pResOut.**
f1d40 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
f1d50 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a  alue otherwise *
f1d60 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74  pResOut is set t
f1d70 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74  o zero.  The ret
f1d80 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  urn value.** is 
f1d90 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
f1da0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65   unless an I/O e
f1db0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
f1dc0 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67  ng lock checking
f1dd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f1de0 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76  proxyCheckReserv
f1df0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
f1e00 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
f1e10 65 73 4f 75 74 29 20 7b 0a 20 20 75 6e 69 78 46  esOut) {.  unixF
f1e20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
f1e30 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
f1e40 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65  t rc = proxyTake
f1e50 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20  Conch(pFile);.  
f1e60 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
f1e70 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  K ){.    proxyLo
f1e80 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
f1e90 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
f1ea0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
f1eb0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f1ec0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
f1ed0 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c  *proxy = pCtx->l
f1ee0 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 65  ockProxy;.    re
f1ef0 74 75 72 6e 20 70 72 6f 78 79 2d 3e 70 4d 65 74  turn proxy->pMet
f1f00 68 6f 64 2d 3e 78 43 68 65 63 6b 52 65 73 65 72  hod->xCheckReser
f1f10 76 65 64 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33  vedLock((sqlite3
f1f20 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 70 52  _file*)proxy, pR
f1f30 65 73 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65  esOut);.  }.  re
f1f40 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
f1f50 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
f1f60 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
f1f70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
f1f80 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20  eter locktype - 
f1f90 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  one.** of the fo
f1fa0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
f1fb0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f     (1) SHARED_LO
f1fc0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45  CK.**     (2) RE
f1fd0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SERVED_LOCK.**  
f1fe0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c     (3) PENDING_L
f1ff0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  OCK.**     (4) E
f2000 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a  XCLUSIVE_LOCK.**
f2010 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68  .** Sometimes wh
f2020 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e  en requesting on
f2030 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64  e lock state, ad
f2040 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74  ditional lock st
f2050 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  ates.** are inse
f2060 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e  rted in between.
f2070 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69    The locking mi
f2080 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20  ght fail on one 
f2090 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20  of the later.** 
f20a0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
f20b0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
f20c0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
f20d0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
f20e0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73  d but.** still s
f20f0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c  hort of its goal
f2100 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
f2110 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65   chart shows the
f2120 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e   allowed.** tran
f2130 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
f2140 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
f2150 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a  diate states:.**
f2160 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
f2170 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20  -> SHARED.**    
f2180 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
f2190 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
f21a0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
f21b0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
f21c0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
f21d0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
f21e0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47  VE.**    PENDING
f21f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
f2200 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f2210 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
f2220 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65  ase a lock.  Use
f2230 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e   the sqlite3OsUn
f2240 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  lock().** routin
f2250 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63  e to lower a loc
f2260 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73  king level..*/.s
f2270 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 4c  tatic int proxyL
f2280 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
f2290 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
f22a0 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
f22b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
f22c0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72  ile*)id;.  int r
f22d0 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e  c = proxyTakeCon
f22e0 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ch(pFile);.  if(
f22f0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
f2300 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69  {.    proxyLocki
f2310 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
f2320 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  = (proxyLockingC
f2330 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e  ontext *)pFile->
f2340 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
f2350 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72      unixFile *pr
f2360 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b  oxy = pCtx->lock
f2370 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20  Proxy;.    rc = 
f2380 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  proxy->pMethod->
f2390 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  xLock((sqlite3_f
f23a0 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b  ile*)proxy, lock
f23b0 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65  type);.    pFile
f23c0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f  ->locktype = pro
f23d0 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  xy->locktype;.  
f23e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
f23f0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74  .../*.** Lower t
f2400 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
f2410 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70   on file descrip
f2420 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63  tor pFile to loc
f2430 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65  ktype.  locktype
f2440 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
f2450 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
f2460 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
f2470 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
f2480 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
f2490 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
f24a0 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
f24b0 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
f24c0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
f24d0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
f24e0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
f24f0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
f2500 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
f2510 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
f2520 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
f2530 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
f2540 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
f2550 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65  t rc = proxyTake
f2560 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20  Conch(pFile);.  
f2570 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
f2580 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  K ){.    proxyLo
f2590 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
f25a0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
f25b0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
f25c0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f25d0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
f25e0 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c  *proxy = pCtx->l
f25f0 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63  ockProxy;.    rc
f2600 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f   = proxy->pMetho
f2610 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69  d->xUnlock((sqli
f2620 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c  te3_file*)proxy,
f2630 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20   locktype);.    
f2640 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
f2650 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70  = proxy->locktyp
f2660 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
f2670 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
f2680 73 65 20 61 20 66 69 6c 65 20 74 68 61 74 20 75  se a file that u
f2690 73 65 73 20 70 72 6f 78 79 20 6c 6f 63 6b 73 2e  ses proxy locks.
f26a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
f26b0 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65  roxyClose(sqlite
f26c0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20  3_file *id) {.  
f26d0 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e  if( id ){.    un
f26e0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
f26f0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
f2700 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43     proxyLockingC
f2710 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28  ontext *pCtx = (
f2720 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f2730 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  ext *)pFile->loc
f2740 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
f2750 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50   unixFile *lockP
f2760 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  roxy = pCtx->loc
f2770 6b 50 72 6f 78 79 3b 0a 20 20 20 20 75 6e 69 78  kProxy;.    unix
f2780 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20  File *conchFile 
f2790 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
f27a0 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  e;.    int rc = 
f27b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a  SQLITE_OK;.    .
f27c0 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78      if( lockProx
f27d0 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
f27e0 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
f27f0 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
f2800 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50  ite3_file*)lockP
f2810 72 6f 78 79 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  roxy, NO_LOCK);.
f2820 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
f2830 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
f2840 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e  rc = lockProxy->
f2850 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28  pMethod->xClose(
f2860 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c  (sqlite3_file*)l
f2870 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20  ockProxy);.     
f2880 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
f2890 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   rc;.      sqlit
f28a0 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78  e3_free(lockProx
f28b0 79 29 3b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e  y);.      pCtx->
f28c0 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 30 3b 0a 20  lockProxy = 0;. 
f28d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6f 6e     }.    if( con
f28e0 63 68 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20  chFile ){.      
f28f0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
f2900 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72  eld ){.        r
f2910 63 20 3d 20 70 72 6f 78 79 52 65 6c 65 61 73 65  c = proxyRelease
f2920 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20  Conch(pFile);.  
f2930 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
f2940 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
f2950 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e  }.      rc = con
f2960 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
f2970 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33  >xClose((sqlite3
f2980 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
f2990 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
f29a0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
f29b0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
f29c0 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20  conchFile);.    
f29d0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
f29e0 65 65 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f  ee(pCtx->lockPro
f29f0 78 79 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c  xyPath);.    sql
f2a00 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e  ite3_free(pCtx->
f2a10 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a  conchFilePath);.
f2a20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f2a30 28 70 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a  (pCtx->dbPath);.
f2a40 20 20 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74      /* restore t
f2a50 68 65 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b  he original lock
f2a60 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20  ing context and 
f2a70 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f  pMethod then clo
f2a80 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69  se it */.    pFi
f2a90 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f2aa0 78 74 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f  xt = pCtx->oldLo
f2ab0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f2ac0 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
f2ad0 20 3d 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74   = pCtx->pOldMet
f2ae0 68 6f 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  hod;.    sqlite3
f2af0 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20  _free(pCtx);.   
f2b00 20 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70   return pFile->p
f2b10 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69  Method->xClose(i
f2b20 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  d);.  }.  return
f2b30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
f2b40 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
f2b50 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
f2b60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
f2b70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a  OCKING_STYLE */.
f2b80 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20  /*.** The proxy 
f2b90 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73  locking style is
f2ba0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
f2bb0 65 20 77 69 74 68 20 41 46 50 20 66 69 6c 65 73  e with AFP files
f2bc0 79 73 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73  ystems..** And s
f2bd0 69 6e 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79  ince AFP is only
f2be0 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61   supported on Ma
f2bf0 63 4f 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20  cOSX, the proxy 
f2c00 6c 6f 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a  locking is also.
f2c10 2a 2a 20 72 65 73 74 72 69 63 74 65 64 20 74 6f  ** restricted to
f2c20 20 4d 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a   MacOSX..** .**.
f2c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2c40 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70  *** End of the p
f2c50 72 6f 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  roxy lock implem
f2c60 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
f2c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
f2c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f2cd0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
f2ce0 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  e the operating 
f2cf0 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65  system interface
f2d00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
f2d10 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61  tine registers a
f2d20 6c 6c 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  ll VFS implement
f2d30 61 74 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d  ations for unix-
f2d40 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a  like operating.*
f2d50 2a 20 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73  * systems.  This
f2d60 20 72 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68   routine, and th
f2d70 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  e sqlite3_os_end
f2d80 28 29 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  () routine that 
f2d90 66 6f 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75  follows,.** shou
f2da0 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72  ld be the only r
f2db0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
f2dc0 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 76 69  file that are vi
f2dd0 73 69 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72  sible from other
f2de0 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  .** files..**.**
f2df0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
f2e00 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72   called once dur
f2e10 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69  ing SQLite initi
f2e20 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79  alization and by
f2e30 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72   a.** single thr
f2e40 65 61 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79  ead.  The memory
f2e50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20   allocation and 
f2e60 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 73  mutex subsystems
f2e70 20 68 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63   have not.** nec
f2e80 65 73 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e  essarily been in
f2e90 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74  itialized when t
f2ea0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
f2eb0 61 6c 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68  alled, and so th
f2ec0 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74  ey.** should not
f2ed0 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c   be used..*/.SQL
f2ee0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f2ef0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
f2f00 29 7b 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54  ){ .  /* .  ** T
f2f10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
f2f20 72 6f 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e  ro defines an in
f2f30 69 74 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e  itializer for an
f2f40 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a   sqlite3_vfs obj
f2f50 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61  ect..  ** The na
f2f60 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 69 73  me of the VFS is
f2f70 20 4e 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70   NAME.  The pApp
f2f80 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65  Data is a pointe
f2f90 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20  r to a pointer. 
f2fa0 20 2a 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64   ** to the "find
f2fb0 65 72 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28  er" function.  (
f2fc0 70 41 70 70 44 61 74 61 20 69 73 20 61 20 70 6f  pAppData is a po
f2fd0 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74  inter to a point
f2fe0 65 72 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  er because.  ** 
f2ff0 73 69 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20  silly C90 rules 
f3000 70 72 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a  prohibit a void*
f3010 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74   from being cast
f3020 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70   to a function p
f3030 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20  ointer.  ** and 
f3040 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f  so we have to go
f3050 20 74 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74   through the int
f3060 65 72 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65  ermediate pointe
f3070 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c  r to avoid probl
f3080 65 6d 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f  ems.  ** when co
f3090 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65  mpiling with -pe
f30a0 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e  dantic-errors on
f30b0 20 47 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a   GCC.).  **.  **
f30c0 20 54 68 65 20 46 49 4e 44 45 52 20 70 61 72 61   The FINDER para
f30d0 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61  meter to this ma
f30e0 63 72 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20  cro is the name 
f30f0 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  of the pointer t
f3100 6f 20 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65  o the.  ** finde
f3110 72 2d 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  r-function.  The
f3120 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
f3130 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
f3140 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73  er to the.  ** s
f3150 71 6c 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73  qlite_io_methods
f3160 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70   object that imp
f3170 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69  lements the desi
f3180 72 65 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a  red locking.  **
f3190 20 62 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65   behaviors.  See
f31a0 20 74 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62   the division ab
f31b0 6f 76 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ove that contain
f31c0 73 20 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a  s the IOMETHODS.
f31d0 20 20 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61    ** macro for a
f31e0 64 64 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  ddition informat
f31f0 69 6f 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75  ion on finder-fu
f3200 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  nctions..  **.  
f3210 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20  ** Most finders 
f3220 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20  simply return a 
f3230 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78  pointer to a fix
f3240 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ed sqlite3_io_me
f3250 74 68 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63  thods.  ** objec
f3260 74 2e 20 20 42 75 74 20 74 68 65 20 22 61 75 74  t.  But the "aut
f3270 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61  olockIoFinder" a
f3280 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f  vailable on MacO
f3290 53 58 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65  SX does a little
f32a0 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  .  ** more than 
f32b0 74 68 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61  that; it looks a
f32c0 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d  t the filesystem
f32d0 20 74 79 70 65 20 74 68 61 74 20 68 6f 73 74 73   type that hosts
f32e0 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62   the .  ** datab
f32f0 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69  ase file and tri
f3300 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20  es to choose an 
f3310 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61  locking method a
f3320 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20  ppropriate for. 
f3330 20 2a 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73   ** that filesys
f3340 74 65 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20  tem time..  */. 
f3350 20 23 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53   #define UNIXVFS
f3360 28 56 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52  (VFSNAME, FINDER
f3370 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) {             
f3380 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f3390 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
f33a0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
f33b0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  on */           
f33c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73           \.    s
f33d0 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c  izeof(unixFile),
f33e0 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65       /* szOsFile
f33f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f3400 20 20 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58         \.    MAX
f3410 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20  _PATHNAME,      
f3420 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65     /* mxPathname
f3430 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f3440 20 20 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20       \.    0,   
f3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3460 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20   /* pNext */    
f3470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3480 20 20 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45     \.    VFSNAME
f3490 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
f34a0 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20  * zName */      
f34b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f34c0 20 5c 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46   \.    (void*)&F
f34d0 49 4e 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20  INDER,       /* 
f34e0 70 41 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20  pAppData */     
f34f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3500 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20  .    unixOpen,  
f3510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
f3520 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20  pen */          
f3530 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3540 20 20 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20     unixDelete,  
f3550 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
f3560 65 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ete */          
f3570 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f3580 20 75 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20   unixAccess,    
f3590 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73         /* xAcces
f35a0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
f35b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f35c0 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  nixFullPathname,
f35d0 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74       /* xFullPat
f35e0 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20  hname */        
f35f0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
f3600 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  xDlOpen,        
f3610 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
f3620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3630 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44       \.    unixD
f3640 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  lError,         
f3650 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20   /* xDlError */ 
f3660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3670 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53     \.    unixDlS
f3680 79 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ym,            /
f3690 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20  * xDlSym */     
f36a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f36b0 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f   \.    unixDlClo
f36c0 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
f36d0 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20  xDlClose */     
f36e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f36f0 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e  .    unixRandomn
f3700 65 73 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52  ess,       /* xR
f3710 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20  andomness */    
f3720 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3730 20 20 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20     unixSleep,   
f3740 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65           /* xSle
f3750 65 70 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ep */           
f3760 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f3770 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65   unixCurrentTime
f3780 2c 20 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65  ,      /* xCurre
f3790 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20  ntTime */       
f37a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f37b0 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20  nixGetLastError 
f37c0 20 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74       /* xGetLast
f37d0 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20  Error */        
f37e0 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20         \.  }..  
f37f0 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 20 64 65 66 61  /*.  ** All defa
f3800 75 6c 74 20 56 46 53 65 73 20 66 6f 72 20 75 6e  ult VFSes for un
f3810 69 78 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64  ix are contained
f3820 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
f3830 67 20 61 72 72 61 79 2e 0a 20 20 2a 2a 0a 20 20  g array..  **.  
f3840 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
f3850 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 70 4e 65   sqlite3_vfs.pNe
f3860 78 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  xt field of the 
f3870 56 46 53 20 6f 62 6a 65 63 74 20 69 73 20 6d 6f  VFS object is mo
f3880 64 69 66 69 65 64 0a 20 20 2a 2a 20 62 79 20 74  dified.  ** by t
f3890 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 77  he SQLite core w
f38a0 68 65 6e 20 74 68 65 20 56 46 53 20 69 73 20 72  hen the VFS is r
f38b0 65 67 69 73 74 65 72 65 64 2e 20 20 53 6f 20 74  egistered.  So t
f38c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
f38d0 2a 20 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62  * array cannot b
f38e0 65 20 63 6f 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20  e const..  */.  
f38f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
f3900 66 73 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 23 69  fs aVfs[] = {.#i
f3910 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
f3920 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
f3930 20 28 4f 53 5f 56 58 57 4f 52 4b 53 20 7c 7c 20   (OS_VXWORKS || 
f3940 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
f3950 5f 29 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28  _)).    UNIXVFS(
f3960 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20  "unix",         
f3970 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
f3980 72 20 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 55  r ),.#else.    U
f3990 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20  NIXVFS("unix",  
f39a0 20 20 20 20 20 20 20 20 70 6f 73 69 78 49 6f 46          posixIoF
f39b0 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a  inder ),.#endif.
f39c0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
f39d0 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 20 6e 6f 6c  x-none",     nol
f39e0 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20  ockIoFinder ),. 
f39f0 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78     UNIXVFS("unix
f3a00 2d 64 6f 74 66 69 6c 65 22 2c 20 20 64 6f 74 6c  -dotfile",  dotl
f3a10 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20  ockIoFinder ),. 
f3a20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78     UNIXVFS("unix
f3a30 2d 77 66 6c 22 2c 20 20 20 20 20 20 70 6f 73 69  -wfl",      posi
f3a40 78 57 66 6c 49 6f 46 69 6e 64 65 72 20 29 2c 0a  xWflIoFinder ),.
f3a50 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
f3a60 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78     UNIXVFS("unix
f3a70 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73 65 6d 49  -namedsem", semI
f3a80 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69  oFinder ),.#endi
f3a90 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  f.#if SQLITE_ENA
f3aa0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
f3ab0 45 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  E.    UNIXVFS("u
f3ac0 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 20 20 70  nix-posix",    p
f3ad0 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a  osixIoFinder ),.
f3ae0 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a  #if !OS_VXWORKS.
f3af0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69      UNIXVFS("uni
f3b00 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20 66 6c 6f  x-flock",    flo
f3b10 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65  ckIoFinder ),.#e
f3b20 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20  ndif.#endif.#if 
f3b30 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
f3b40 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
f3b50 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
f3b60 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  ).    UNIXVFS("u
f3b70 6e 69 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61  nix-afp",      a
f3b80 66 70 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20  fpIoFinder ),.  
f3b90 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d    UNIXVFS("unix-
f3ba0 70 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79  proxy",    proxy
f3bb0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64  IoFinder ),.#end
f3bc0 69 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e  if.  };.  unsign
f3bd0 65 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  ed int i;       
f3be0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
f3bf0 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69  er */..  /* Regi
f3c00 73 74 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64  ster all VFSes d
f3c10 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56  efined in the aV
f3c20 66 73 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20  fs[] array */.  
f3c30 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65  for(i=0; i<(size
f3c40 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28  of(aVfs)/sizeof(
f3c50 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69  sqlite3_vfs)); i
f3c60 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
f3c70 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61  _vfs_register(&a
f3c80 56 66 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20  Vfs[i], i==0);. 
f3c90 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
f3ca0 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  TE_OK; .}../*.**
f3cb0 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70   Shutdown the op
f3cc0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
f3cd0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
f3ce0 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73  Some operating s
f3cf0 79 73 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65  ystems might nee
f3d00 64 20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65  d to do some cle
f3d10 61 6e 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75  anup in this rou
f3d20 74 69 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65  tine,.** to rele
f3d30 61 73 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  ase dynamically 
f3d40 61 6c 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74  allocated object
f3d50 73 2e 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75  s.  But not on u
f3d60 6e 69 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  nix..** This rou
f3d70 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tine is a no-op 
f3d80 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c  for unix..*/.SQL
f3d90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f3da0 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29  te3_os_end(void)
f3db0 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  { .  return SQLI
f3dc0 54 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64  TE_OK; .}. .#end
f3dd0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
f3de0 55 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  UNIX */../******
f3df0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
f3e00 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  os_unix.c ******
f3e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
f3e40 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
f3e50 69 6c 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a  ile os_win.c ***
f3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
f3e90 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a  004 May 22.**.**
f3ea0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
f3eb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
f3ec0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
f3ed0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
f3ee0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
f3ef0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
f3f00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
f3f10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
f3f20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
f3f30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
f3f40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
f3f50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
f3f60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
f3f70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
f3f80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
f3f90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
f3fa0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
f3fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
f4000 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
f4010 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
f4020 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f  t is specific to
f4030 20 77 69 6e 64 6f 77 73 2e 0a 2a 2f 0a 23 69 66   windows..*/.#if
f4040 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20   SQLITE_OS_WIN  
f4050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f4060 54 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65  This file is use
f4070 64 20 66 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e  d for windows on
f4080 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  ly */.../*.** A 
f4090 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72  Note About Memor
f40a0 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a  y Allocation:.**
f40b0 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20  .** This driver 
f40c0 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72  uses malloc()/fr
f40d0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61  ee() directly ra
f40e0 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20  ther than going 
f40f0 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53  through.** the S
f4100 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73  QLite-wrappers s
f4110 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f  qlite3_malloc()/
f4120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20  sqlite3_free(). 
f4130 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a   Those wrappers.
f4140 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ** are designed 
f4150 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64  for use on embed
f4160 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72  ded systems wher
f4170 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72  e memory is scar
f4180 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63  ce and.** malloc
f4190 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e   failures happen
f41a0 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 57 69   frequently.  Wi
f41b0 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70  n32 does not typ
f41c0 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a  ically run on.**
f41d0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
f41e0 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64  s, and when it d
f41f0 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65  oes the develope
f4200 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65  rs normally have
f4210 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c   bigger.** probl
f4220 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  ems to worry abo
f4230 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20  ut than running 
f4240 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20  out of memory.  
f4250 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a  So there is not.
f4260 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20  ** a compelling 
f4270 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  need to use the 
f4280 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  wrappers..**.** 
f4290 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67  But there is a g
f42a0 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f  ood reason to no
f42b0 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  t use the wrappe
f42c0 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74  rs.  If we use t
f42d0 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74  he.** wrappers t
f42e0 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20  hen we will get 
f42f0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63  simulated malloc
f4300 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  () failures with
f4310 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65  in this.** drive
f4320 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75  r.  And that cau
f4330 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66  ses all kinds of
f4340 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75   problems for ou
f4350 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20  r tests.  We.** 
f4360 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51  could enhance SQ
f4370 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74  Lite to deal wit
f4380 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  h simulated mall
f4390 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  oc failures with
f43a0 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69  in.** the OS dri
f43b0 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64  ver, but the cod
f43c0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74  e to deal with t
f43d0 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75  hose failure wou
f43e0 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65  ld not.** be exe
f43f0 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20  rcised on Linux 
f4400 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20  (which does not 
f4410 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29  need to malloc()
f4420 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a   in the driver).
f4430 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75  ** and so we wou
f4440 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c  ld have difficul
f4450 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72  ty writing cover
f4460 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68  age tests for th
f4470 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74  at.** code.  Bet
f4480 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65  ter to leave the
f4490 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68   code out, we th
f44a0 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ink..**.** The p
f44b0 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73  oint of this dis
f44c0 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f  cussion is as fo
f44d0 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65  llows:  When cre
f44e0 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f  ating a new.** O
f44f0 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65  S layer for an e
f4500 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20  mbedded system, 
f4510 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20  if you use this 
f4520 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70  file as an examp
f4530 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65  le,.** avoid the
f4540 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
f4550 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20  /free().  Those 
f4560 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b  routines work ok
f4570 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64   on windows.** d
f4580 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20  esktops but not 
f4590 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64  so well in embed
f45a0 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  ded systems..*/.
f45b0 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61  .#include <winba
f45c0 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f  se.h>..#ifdef __
f45d0 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75  CYGWIN__.# inclu
f45e0 64 65 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68  de <sys/cygwin.h
f45f0 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  >.#endif../*.** 
f4600 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64  Macros used to d
f4610 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
f4620 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74   or not to use t
f4630 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64  hreads..*/.#if d
f4640 65 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46  efined(THREADSAF
f4650 45 29 20 26 26 20 54 48 52 45 41 44 53 41 46 45  E) && THREADSAFE
f4660 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
f4670 5f 57 33 32 5f 54 48 52 45 41 44 53 20 31 0a 23  _W32_THREADS 1.#
f4680 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  endif../*.** Inc
f4690 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69  lude code that i
f46a0 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  s common to all 
f46b0 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a  os_*.c files.*/.
f46c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f46d0 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f  Include os_commo
f46e0 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  n.h in the middl
f46f0 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a  e of os_win.c **
f4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f4710 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f4720 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f  Begin file os_co
f4730 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
f4740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f4760 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
f4770 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
f4780 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f4790 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f47a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f47b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f47c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f47d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f47e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f47f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f4800 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f4810 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f4820 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f4830 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f4840 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f4850 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f4860 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f4870 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f4880 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
f4890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f48a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f48b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f48c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f48d0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
f48e0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d   file contains m
f48f0 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74  acros and a litt
f4900 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74  le bit of code t
f4910 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
f4920 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70  .** all of the p
f4930 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63  latform-specific
f4940 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20   files (os_*.c) 
f4950 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64  and is #included
f4960 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66   into those.** f
f4970 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  iles..**.** This
f4980 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
f4990 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65  #included by the
f49a0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e   os_*.c files on
f49b0 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61  ly.  It is not a
f49c0 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70  .** general purp
f49d0 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ose header file.
f49e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f  .*/.#ifndef _OS_
f49f0 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e  COMMON_H_.#defin
f4a00 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  e _OS_COMMON_H_.
f4a10 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20  ./*.** At least 
f4a20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c  two bugs have sl
f4a30 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65  ipped in because
f4a40 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20   we changed the 
f4a50 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20  MEMORY_DEBUG.** 
f4a60 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f  macro to SQLITE_
f4a70 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f  DEBUG and some o
f4a80 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68  lder makefiles h
f4a90 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  ave not yet made
f4aa0 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20   the.** switch. 
f4ab0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
f4ac0 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68  ode should catch
f4ad0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74   this problem at
f4ae0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a   compile-time..*
f4af0 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f  /.#ifdef MEMORY_
f4b00 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54  DEBUG.# error "T
f4b10 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20  he MEMORY_DEBUG 
f4b20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74  macro is obsolet
f4b30 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44  e.  Use SQLITE_D
f4b40 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23  EBUG instead.".#
f4b50 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
f4b60 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
f4b70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f4b80 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30  lite3OSTrace = 0
f4b90 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ;.#define OSTRAC
f4ba0 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66  E1(X)         if
f4bb0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
f4bc0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
f4bd0 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65  rintf(X).#define
f4be0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20   OSTRACE2(X,Y)  
f4bf0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
f4c00 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
f4c10 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
f4c20 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f4c30 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66  E3(X,Y,Z)     if
f4c40 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
f4c50 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
f4c60 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65  rintf(X,Y,Z).#de
f4c70 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
f4c80 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c  Y,Z,A)   if( sql
f4c90 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
f4ca0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f4cb0 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
f4cc0 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
f4cd0 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65  ,A,B) if( sqlite
f4ce0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
f4cf0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f4d00 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
f4d10 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
f4d20 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28  A,B,C) \.    if(
f4d30 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
f4d40 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f4d50 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  tf(X,Y,Z,A,B,C).
f4d60 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
f4d70 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20  (X,Y,Z,A,B,C,D) 
f4d80 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
f4d90 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
f4da0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f4db0 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65  Z,A,B,C,D).#else
f4dc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f4dd0 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  1(X).#define OST
f4de0 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69  RACE2(X,Y).#defi
f4df0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
f4e00 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
f4e10 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65  CE4(X,Y,Z,A).#de
f4e20 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c  fine OSTRACE5(X,
f4e30 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
f4e40 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
f4e50 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
f4e60 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
f4e70 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f  B,C,D).#endif../
f4e80 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
f4e90 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63  performance trac
f4ea0 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74  ing.  Normally t
f4eb0 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79  urned off.  Only
f4ec0 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38   works.** on i48
f4ed0 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23  6 hardware..*/.#
f4ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52  ifdef SQLITE_PER
f4ef0 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a  FORMANCE_TRACE..
f4f00 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20  /* .** hwtime.h 
f4f10 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
f4f20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66  assembler code f
f4f30 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
f4f40 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d  .** high-perform
f4f50 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74  ance timing rout
f4f60 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ines..*/./******
f4f70 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
f4f80 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65   hwtime.h in the
f4f90 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f   middle of os_co
f4fa0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
f4fb0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
f4fc0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
f4fd0 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a  ile hwtime.h ***
f4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5000 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
f5010 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a  008 May 27.**.**
f5020 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
f5030 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
f5040 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
f5050 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
f5060 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
f5070 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
f5080 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
f5090 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
f50a0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
f50b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
f50c0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
f50d0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
f50e0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
f50f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
f5100 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
f5110 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
f5120 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
f5180 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
f5190 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
f51a0 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72  sm code for retr
f51b0 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72  ieving "high-per
f51c0 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75  formance".** cou
f51d0 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c  nters for x86 cl
f51e0 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 69 66  ass CPUs..*/.#if
f51f0 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  ndef _HWTIME_H_.
f5200 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f  #define _HWTIME_
f5210 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  H_../*.** The fo
f5220 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
f5230 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
f5240 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
f5250 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
f5260 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
f5270 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
f5280 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
f5290 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
f52a0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
f52b0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
f52c0 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
f52d0 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
f52e0 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
f52f0 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66  rofiling..*/.#if
f5300 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
f5310 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
f5320 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20  MSC_VER)) && \. 
f5330 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33       (defined(i3
f5340 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
f5350 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
f5360 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20  ned(_M_IX86)).. 
f5370 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47   #if defined(__G
f5380 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69  NUC__)..  __inli
f5390 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
f53a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
f53b0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73  (void){.     uns
f53c0 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69  igned int lo, hi
f53d0 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  ;.     __asm__ _
f53e0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
f53f0 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29  tsc" : "=a" (lo)
f5400 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20  , "=d" (hi));.  
f5410 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74     return (sqlit
f5420 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33  e_uint64)hi << 3
f5430 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23  2 | lo;.  }..  #
f5440 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
f5450 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c  C_VER)..  __decl
f5460 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e  spec(naked) __in
f5470 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74  line sqlite_uint
f5480 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74  64 __cdecl sqlit
f5490 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
f54a0 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20       __asm {.   
f54b0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20       rdtsc.     
f54c0 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72     ret       ; r
f54d0 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45  eturn value at E
f54e0 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20  DX:EAX.     }.  
f54f0 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c  }..  #endif..#el
f5500 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
f5510 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
f5520 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20  (__x86_64__)).. 
f5530 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
f5540 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
f5550 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
f5560 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
f5570 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f  ng val;.      __
f5580 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
f5590 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
f55a0 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20  A" (val));.     
f55b0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
f55c0 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  . .#elif (define
f55d0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
f55e0 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
f55f0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
f5600 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
f5610 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
f5620 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
f5630 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61   long long retva
f5640 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  l;.      unsigne
f5650 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20  d long junk;.   
f5660 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
f5670 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20  atile__ ("\n\.  
f5680 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20          1:      
f5690 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20  mftbu   %1\n\.  
f56a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f56b0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20  mftb    %L0\n\. 
f56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f56d0 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20   mftbu   %0\n\. 
f56e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f56f0 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e   cmpw    %0,%1\n
f5700 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
f5710 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a      bne     1b".
f5720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f5730 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c    : "=r" (retval
f5740 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b  ), "=r" (junk));
f5750 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
f5760 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  tval;.  }..#else
f5770 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20  ..  #error Need 
f5780 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
f5790 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  f sqlite3Hwtime(
f57a0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
f57b0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  orm...  /*.  ** 
f57c0 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  To compile witho
f57d0 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  ut implementing 
f57e0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
f57f0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
f5800 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20  m,.  ** you can 
f5810 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65  remove the above
f5820 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20   #error and use 
f5830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
f5840 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e  ** stub function
f5850 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65  .  You will lose
f5860 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20   timing support 
f5870 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66  for many.  ** of
f5880 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61   the debugging a
f5890 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69  nd testing utili
f58a0 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f  ties, but it sho
f58b0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73  uld at.  ** leas
f58c0 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75  t compile and ru
f58d0 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  n..  */.SQLITE_P
f58e0 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f  RIVATE   sqlite_
f58f0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
f5900 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75  time(void){ retu
f5910 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74  rn ((sqlite_uint
f5920 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66  64)0); }..#endif
f5930 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
f5940 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29  ined(_HWTIME_H_)
f5950 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
f5960 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
f5970 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
f5980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f59a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
f59b0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
f59c0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
f59d0 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  f in os_common.h
f59e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f59f0 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c  ***/..static sql
f5a00 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61  ite_uint64 g_sta
f5a10 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74  rt;.static sqlit
f5a20 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73  e_uint64 g_elaps
f5a30 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ed;.#define TIME
f5a40 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f  R_START       g_
f5a50 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74  start=sqlite3Hwt
f5a60 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49  ime().#define TI
f5a70 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20  MER_END         
f5a80 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65  g_elapsed=sqlite
f5a90 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72  3Hwtime()-g_star
f5aa0 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  t.#define TIMER_
f5ab0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c  ELAPSED     g_el
f5ac0 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66  apsed.#else.#def
f5ad0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a  ine TIMER_START.
f5ae0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
f5af0 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  D.#define TIMER_
f5b00 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71  ELAPSED     ((sq
f5b10 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23  lite_uint64)0).#
f5b20 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
f5b30 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  we compile with 
f5b40 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20  the SQLITE_TEST 
f5b50 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20  macro set, then 
f5b60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  the following bl
f5b70 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77  ock.** of code w
f5b80 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20  ill give us the 
f5b90 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c  ability to simul
f5ba0 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65  ate a disk I/O e
f5bb0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69  rror.  This.** i
f5bc0 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
f5bd0 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76  ng the I/O recov
f5be0 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69  ery logic..*/.#i
f5bf0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
f5c00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f5c10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f5c20 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  _hit = 0;       
f5c30 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
f5c40 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f  mber of I/O Erro
f5c50 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
f5c60 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
f5c70 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20  error_hardhit = 
f5c80 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  0;        /* Num
f5c90 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67  ber of non-benig
f5ca0 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  n errors */.SQLI
f5cb0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
f5cc0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
f5cd0 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ing = 0;        
f5ce0 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f  /* Count down to
f5cf0 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72   first I/O error
f5d00 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
f5d10 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
f5d20 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b  ror_persist = 0;
f5d30 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
f5d40 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65  if I/O errors pe
f5d50 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  rsist */.SQLITE_
f5d60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f5d70 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
f5d80 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
f5d90 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61  True if errors a
f5da0 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c  re benign */.SQL
f5db0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f5dc0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
f5dd0 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ding = 0;.SQLITE
f5de0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
f5df0 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23  _diskfull = 0;.#
f5e00 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
f5e10 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20  OErrorBenign(X) 
f5e20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f5e30 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66  _benign=(X).#def
f5e40 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
f5e50 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
f5e60 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
f5e70 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
f5e80 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f5e90 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c  _hit) \.       |
f5ea0 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  | sqlite3_io_err
f5eb0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20  or_pending-- == 
f5ec0 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20  1 )  \.         
f5ed0 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65       { local_ioe
f5ee0 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74  rr(); CODE; }.st
f5ef0 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
f5f00 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41  ioerr(){.  IOTRA
f5f10 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b  CE(("IOERR\n"));
f5f20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
f5f30 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28  ror_hit++;.  if(
f5f40 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   !sqlite3_io_err
f5f50 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69  or_benign ) sqli
f5f60 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
f5f70 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e  dhit++;.}.#defin
f5f80 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
f5f90 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a  llError(CODE) \.
f5fa0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
f5fb0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
f5fc0 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71  ){ \.     if( sq
f5fd0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
f5fe0 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c  ending == 1 ){ \
f5ff0 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f  .       local_io
f6000 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20  err(); \.       
f6010 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
f6020 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73   = 1; \.       s
f6030 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
f6040 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  hit = 1; \.     
f6050 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d    CODE; \.     }
f6060 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73  else{ \.       s
f6070 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
f6080 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20  pending--; \.   
f6090 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65    } \.   }.#else
f60a0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
f60b0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
f60c0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
f60d0 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65  teIOError(A).#de
f60e0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
f60f0 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65  kfullError(A).#e
f6100 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ndif../*.** When
f6110 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61   testing, keep a
f6120 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75   count of the nu
f6130 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c  mber of open fil
f6140 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  es..*/.#ifdef SQ
f6150 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
f6160 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
f6170 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
f6180 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70   = 0;.#define Op
f6190 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71  enCounter(X)  sq
f61a0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
f61b0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65  count+=(X).#else
f61c0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
f61d0 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a  nter(X).#endif..
f61e0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
f61f0 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ed(_OS_COMMON_H_
f6200 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
f6210 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
f6220 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
f6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6250 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
f6260 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
f6270 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
f6280 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20 2a  ff in os_win.c *
f6290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f62a0 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  ****/../*.** Som
f62b0 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70  e microsoft comp
f62c0 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20  ilers lack this 
f62d0 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23  definition..*/.#
f62e0 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 46  ifndef INVALID_F
f62f0 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 0a 23  ILE_ATTRIBUTES.#
f6300 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f   define INVALID_
f6310 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 20  FILE_ATTRIBUTES 
f6320 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23 65 6e  ((DWORD)-1) .#en
f6330 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  dif../*.** Deter
f6340 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64  mine if we are d
f6350 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64  ealing with Wind
f6360 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61  owsCE - which ha
f6370 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75  s a much.** redu
f6380 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  ced API..*/.#if 
f6390 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
f63a0 23 20 64 65 66 69 6e 65 20 41 72 65 46 69 6c 65  # define AreFile
f63b0 41 70 69 73 41 4e 53 49 28 29 20 31 0a 23 20 64  ApisANSI() 1.# d
f63c0 65 66 69 6e 65 20 46 6f 72 6d 61 74 4d 65 73 73  efine FormatMess
f63d0 61 67 65 57 28 61 2c 62 2c 63 2c 64 2c 65 2c 66  ageW(a,b,c,d,e,f
f63e0 2c 67 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,g) 0.#endif../*
f63f0 0a 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20  .** WinCE lacks 
f6400 6e 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66  native support f
f6410 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  or file locking 
f6420 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61  so we have to fa
f6430 6b 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f  ke it.** with so
f6440 6d 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f  me code of our o
f6450 77 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  wn..*/.#if SQLIT
f6460 45 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64  E_OS_WINCE.typed
f6470 65 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c  ef struct winceL
f6480 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61  ock {.  int nRea
f6490 64 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e  ders;       /* N
f64a0 75 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20  umber of reader 
f64b0 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a  locks obtained *
f64c0 2f 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e  /.  BOOL bPendin
f64d0 67 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63  g;      /* Indic
f64e0 61 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c  ates a pending l
f64f0 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74  ock has been obt
f6500 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20  ained */.  BOOL 
f6510 62 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f  bReserved;     /
f6520 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65  * Indicates a re
f6530 73 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20  served lock has 
f6540 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f  been obtained */
f6550 0a 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69  .  BOOL bExclusi
f6560 76 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61  ve;    /* Indica
f6570 74 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65  tes an exclusive
f6580 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f   lock has been o
f6590 62 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e  btained */.} win
f65a0 63 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a  ceLock;.#endif..
f65b0 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c  /*.** The winFil
f65c0 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  e structure is a
f65d0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
f65e0 69 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69  ite3_file* speci
f65f0 66 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32  fic to the win32
f6600 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20  .** portability 
f6610 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
f6620 66 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65  f struct winFile
f6630 20 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74   winFile;.struct
f6640 20 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e   winFile {.  con
f6650 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
f6660 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f  thods *pMethod;/
f6670 2a 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20  * Must be first 
f6680 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20  */.  HANDLE h;  
f6690 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f66a0 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
f66b0 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f  sing the file */
f66c0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
f66d0 20 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79   locktype; /* Ty
f66e0 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65  pe of lock curre
f66f0 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69  ntly held on thi
f6700 73 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72  s file */.  shor
f6710 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  t sharedLockByte
f6720 3b 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20  ;   /* Randomly 
f6730 63 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64  chosen byte used
f6740 20 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63   as a shared loc
f6750 6b 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c 61 73  k */.  DWORD las
f6760 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 2f  tErrno;        /
f6770 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 65 72  * The Windows er
f6780 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73  rno from the las
f6790 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20  t I/O error */. 
f67a0 20 44 57 4f 52 44 20 73 65 63 74 6f 72 53 69 7a   DWORD sectorSiz
f67b0 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74  e;       /* Sect
f67c0 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  or size of the d
f67d0 65 76 69 63 65 20 66 69 6c 65 20 69 73 20 6f 6e  evice file is on
f67e0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   */.#if SQLITE_O
f67f0 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20  S_WINCE.  WCHAR 
f6800 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b  *zDeleteOnClose;
f6810 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
f6820 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e  e to delete when
f6830 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41   closing */.  HA
f6840 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20  NDLE hMutex;    
f6850 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75        /* Mutex u
f6860 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61  sed to control a
f6870 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20  ccess to shared 
f6880 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44  lock */  .  HAND
f6890 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20  LE hShared;     
f68a0 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65      /* Shared me
f68b0 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65  mory segment use
f68c0 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f  d for locking */
f68d0 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63  .  winceLock loc
f68e0 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  al;        /* Lo
f68f0 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20  cks obtained by 
f6900 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66  this instance of
f6910 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69   winFile */.  wi
f6920 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b  nceLock *shared;
f6930 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20        /* Global 
f6940 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f  shared lock memo
f6950 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  ry for the file 
f6960 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
f6970 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 70 72 6f  *.** Forward pro
f6980 74 6f 74 79 70 65 73 2e 0a 2a 2f 0a 73 74 61 74  totypes..*/.stat
f6990 69 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72  ic int getSector
f69a0 53 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65  Size(.    sqlite
f69b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20  3_vfs *pVfs,.   
f69c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65   const char *zRe
f69d0 6c 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54  lative     /* UT
f69e0 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f  F-8 file name */
f69f0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .);../*.** The f
f6a00 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
f6a10 65 20 69 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20  e is (normally) 
f6a20 73 65 74 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76  set once and nev
f6a30 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68  er changes.** th
f6a40 65 72 65 61 66 74 65 72 2e 20 20 49 74 20 72 65  ereafter.  It re
f6a50 63 6f 72 64 73 20 77 68 65 74 68 65 72 20 74 68  cords whether th
f6a60 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
f6a70 65 6d 20 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f  em is Win95.** o
f6a80 72 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30  r WinNT..**.** 0
f6a90 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
f6aa0 73 74 65 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a  stem unknown..**
f6ab0 20 31 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20   1:   Operating 
f6ac0 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 35 2e  system is Win95.
f6ad0 0a 2a 2a 20 32 3a 20 20 20 4f 70 65 72 61 74 69  .** 2:   Operati
f6ae0 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e  ng system is Win
f6af0 4e 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64  NT..**.** In ord
f6b00 65 72 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65  er to facilitate
f6b10 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 20 57 69   testing on a Wi
f6b20 6e 4e 54 20 73 79 73 74 65 6d 2c 20 74 68 65 20  nNT system, the 
f6b30 74 65 73 74 20 66 69 78 74 75 72 65 0a 2a 2a 20  test fixture.** 
f6b40 63 61 6e 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74  can manually set
f6b50 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 31   this value to 1
f6b60 20 74 6f 20 65 6d 75 6c 61 74 65 20 57 69 6e 39   to emulate Win9
f6b70 38 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23  8 behavior..*/.#
f6b80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
f6b90 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
f6ba0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65   sqlite3_os_type
f6bb0 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74   = 0;.#else.stat
f6bc0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  ic int sqlite3_o
f6bd0 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64  s_type = 0;.#end
f6be0 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
f6bf0 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29   true (non-zero)
f6c00 20 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69   if we are runni
f6c10 6e 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20  ng under WinNT, 
f6c20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a  Win2K, WinXP,.**
f6c30 20 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75   or WinCE.  Retu
f6c40 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20  rn false (zero) 
f6c50 66 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38  for Win95, Win98
f6c60 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a  , or WinME..**.*
f6c70 2a 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74  * Here is an int
f6c80 65 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61  eresting observa
f6c90 74 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69  tion:  Win95, Wi
f6ca0 6e 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c  n98, and WinME l
f6cb0 61 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46  ack.** the LockF
f6cc0 69 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75  ileEx() API.  Bu
f6cd0 74 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73  t we can still s
f6ce0 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61  tatically link a
f6cf0 67 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41  gainst that.** A
f6d00 50 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65  PI as long as we
f6d10 20 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77   don't call it w
f6d20 68 65 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39  hen running Win9
f6d30 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c  5/98/ME.  A call
f6d40 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74   to.** this rout
f6d50 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  ine is used to d
f6d60 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20  etermine if the 
f6d70 68 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38  host is Win95/98
f6d80 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f  /ME or.** WinNT/
f6d90 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65  2K/XP so that we
f6da0 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68   will know wheth
f6db0 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e  er or not we can
f6dc0 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20   safely call.** 
f6dd0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
f6de0 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c   API..*/.#if SQL
f6df0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64  ITE_OS_WINCE.# d
f6e00 65 66 69 6e 65 20 69 73 4e 54 28 29 20 20 28 31  efine isNT()  (1
f6e10 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63  ).#else.  static
f6e20 20 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 29 7b   int isNT(void){
f6e30 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
f6e40 5f 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b 0a 20  _os_type==0 ){. 
f6e50 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e       OSVERSIONIN
f6e60 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20  FO sInfo;.      
f6e70 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f  sInfo.dwOSVersio
f6e80 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65  nInfoSize = size
f6e90 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20  of(sInfo);.     
f6ea0 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73   GetVersionEx(&s
f6eb0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c  Info);.      sql
f6ec0 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 73  ite3_os_type = s
f6ed0 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49  Info.dwPlatformI
f6ee0 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f  d==VER_PLATFORM_
f6ef0 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31  WIN32_NT ? 2 : 1
f6f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
f6f10 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79  rn sqlite3_os_ty
f6f20 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69  pe==2;.  }.#endi
f6f30 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
f6f40 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  INCE */../*.** C
f6f50 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 73  onvert a UTF-8 s
f6f60 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f  tring to microso
f6f70 66 74 20 75 6e 69 63 6f 64 65 20 28 55 54 46 2d  ft unicode (UTF-
f6f80 31 36 3f 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61  16?). .**.** Spa
f6f90 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
f6fa0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
f6fb0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
f6fc0 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69  malloc..*/.stati
f6fd0 63 20 57 43 48 41 52 20 2a 75 74 66 38 54 6f 55  c WCHAR *utf8ToU
f6fe0 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  nicode(const cha
f6ff0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
f7000 20 69 6e 74 20 6e 43 68 61 72 3b 0a 20 20 57 43   int nChar;.  WC
f7010 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61  HAR *zWideFilena
f7020 6d 65 3b 0a 0a 20 20 6e 43 68 61 72 20 3d 20 4d  me;..  nChar = M
f7030 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
f7040 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  ar(CP_UTF8, 0, z
f7050 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55  Filename, -1, NU
f7060 4c 4c 2c 20 30 29 3b 0a 20 20 7a 57 69 64 65 46  LL, 0);.  zWideF
f7070 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63  ilename = malloc
f7080 28 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a  ( nChar*sizeof(z
f7090 57 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29  WideFilename[0])
f70a0 20 29 3b 0a 20 20 69 66 28 20 7a 57 69 64 65 46   );.  if( zWideF
f70b0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
f70c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
f70d0 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
f70e0 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
f70f0 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
f7100 61 6d 65 2c 20 2d 31 2c 20 7a 57 69 64 65 46 69  ame, -1, zWideFi
f7110 6c 65 6e 61 6d 65 2c 20 6e 43 68 61 72 29 3b 0a  lename, nChar);.
f7120 20 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29    if( nChar==0 )
f7130 7b 0a 20 20 20 20 66 72 65 65 28 7a 57 69 64 65  {.    free(zWide
f7140 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
f7150 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 30  WideFilename = 0
f7160 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
f7170 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  WideFilename;.}.
f7180 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d  ./*.** Convert m
f7190 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f71a0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63   to UTF-8.  Spac
f71b0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
f71c0 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
f71d0 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  .** obtained fro
f71e0 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73  m malloc()..*/.s
f71f0 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63  tatic char *unic
f7200 6f 64 65 54 6f 55 74 66 38 28 63 6f 6e 73 74 20  odeToUtf8(const 
f7210 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65  WCHAR *zWideFile
f7220 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
f7230 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  te;.  char *zFil
f7240 65 6e 61 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20  ename;..  nByte 
f7250 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74  = WideCharToMult
f7260 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30  iByte(CP_UTF8, 0
f7270 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c  , zWideFilename,
f7280 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29   -1, 0, 0, 0, 0)
f7290 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20  ;.  zFilename = 
f72a0 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b  malloc( nByte );
f72b0 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
f72c0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f72d0 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65  n 0;.  }.  nByte
f72e0 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c   = WideCharToMul
f72f0 74 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20  tiByte(CP_UTF8, 
f7300 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65  0, zWideFilename
f7310 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  , -1, zFilename,
f7320 20 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20   nByte,.        
f7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7340 20 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69        0, 0);.  i
f7350 66 28 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b  f( nByte == 0 ){
f7360 0a 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e  .    free(zFilen
f7370 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e  ame);.    zFilen
f7380 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ame = 0;.  }.  r
f7390 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b  eturn zFilename;
f73a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
f73b0 74 20 61 6e 20 61 6e 73 69 20 73 74 72 69 6e 67  t an ansi string
f73c0 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e   to microsoft un
f73d0 69 63 6f 64 65 2c 20 62 61 73 65 64 20 6f 6e 20  icode, based on 
f73e0 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63  the.** current c
f73f0 6f 64 65 70 61 67 65 20 73 65 74 74 69 6e 67 73  odepage settings
f7400 20 66 6f 72 20 66 69 6c 65 20 61 70 69 73 2e 0a   for file apis..
f7410 2a 2a 20 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  ** .** Space to 
f7420 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
f7430 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
f7440 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c  ined.** from mal
f7450 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57  loc..*/.static W
f7460 43 48 41 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63  CHAR *mbcsToUnic
f7470 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ode(const char *
f7480 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e  zFilename){.  in
f7490 74 20 6e 42 79 74 65 3b 0a 20 20 57 43 48 41 52  t nByte;.  WCHAR
f74a0 20 2a 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b   *zMbcsFilename;
f74b0 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20  .  int codepage 
f74c0 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53  = AreFileApisANS
f74d0 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43  I() ? CP_ACP : C
f74e0 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74  P_OEMCP;..  nByt
f74f0 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  e = MultiByteToW
f7500 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65  ideChar(codepage
f7510 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f7520 2d 31 2c 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65  -1, NULL,0)*size
f7530 6f 66 28 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62  of(WCHAR);.  zMb
f7540 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c  csFilename = mal
f7550 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f  loc( nByte*sizeo
f7560 66 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b  f(zMbcsFilename[
f7570 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62  0]) );.  if( zMb
f7580 63 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b  csFilename==0 ){
f7590 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
f75a0 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c   }.  nByte = Mul
f75b0 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72  tiByteToWideChar
f75c0 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46  (codepage, 0, zF
f75d0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62  ilename, -1, zMb
f75e0 63 73 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74  csFilename, nByt
f75f0 65 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3d  e);.  if( nByte=
f7600 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a  =0 ){.    free(z
f7610 4d 62 63 73 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  MbcsFilename);. 
f7620 20 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65     zMbcsFilename
f7630 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
f7640 72 6e 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  rn zMbcsFilename
f7650 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
f7660 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69  rt microsoft uni
f7670 63 6f 64 65 20 74 6f 20 6d 75 6c 74 69 62 79 74  code to multibyt
f7680 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69  e character stri
f7690 6e 67 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ng, based on the
f76a0 0a 2a 2a 20 75 73 65 72 27 73 20 41 6e 73 69 20  .** user's Ansi 
f76b0 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  codepage..**.** 
f76c0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
f76d0 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
f76e0 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
f76f0 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a  om.** malloc()..
f7700 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
f7710 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 63 6f  unicodeToMbcs(co
f7720 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 65  nst WCHAR *zWide
f7730 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
f7740 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a   nByte;.  char *
f7750 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74  zFilename;.  int
f7760 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46   codepage = AreF
f7770 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20  ileApisANSI() ? 
f7780 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43  CP_ACP : CP_OEMC
f7790 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69  P;..  nByte = Wi
f77a0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
f77b0 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a  e(codepage, 0, z
f77c0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  WideFilename, -1
f77d0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
f77e0 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c   zFilename = mal
f77f0 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20  loc( nByte );.  
f7800 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
f7810 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
f7820 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20  ;.  }.  nByte = 
f7830 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42  WideCharToMultiB
f7840 79 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c  yte(codepage, 0,
f7850 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20   zWideFilename, 
f7860 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e  -1, zFilename, n
f7870 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Byte,.          
f7880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7890 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28      0, 0);.  if(
f78a0 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20   nByte == 0 ){. 
f78b0 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d     free(zFilenam
f78c0 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d  e);.    zFilenam
f78d0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
f78e0 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d  urn zFilename;.}
f78f0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
f7900 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63  multibyte charac
f7910 74 65 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54  ter string to UT
f7920 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68  F-8.  Space to h
f7930 6f 6c 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72  old the.** retur
f7940 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62  ned string is ob
f7950 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
f7960 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oc()..*/.SQLITE_
f7970 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
f7980 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f  3_win32_mbcs_to_
f7990 75 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20  utf8(const char 
f79a0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63  *zFilename){.  c
f79b0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 55 74  har *zFilenameUt
f79c0 66 38 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d  f8;.  WCHAR *zTm
f79d0 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69  pWide;..  zTmpWi
f79e0 64 65 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f  de = mbcsToUnico
f79f0 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  de(zFilename);. 
f7a00 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30   if( zTmpWide==0
f7a10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
f7a20 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d  ;.  }.  zFilenam
f7a30 65 55 74 66 38 20 3d 20 75 6e 69 63 6f 64 65 54  eUtf8 = unicodeT
f7a40 6f 55 74 66 38 28 7a 54 6d 70 57 69 64 65 29 3b  oUtf8(zTmpWide);
f7a50 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65  .  free(zTmpWide
f7a60 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c  );.  return zFil
f7a70 65 6e 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a  enameUtf8;.}../*
f7a80 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d  .** Convert UTF-
f7a90 38 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63  8 to multibyte c
f7aa0 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e  haracter string.
f7ab0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
f7ac0 74 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64  the .** returned
f7ad0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
f7ae0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ned from malloc(
f7af0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  )..*/.static cha
f7b00 72 20 2a 75 74 66 38 54 6f 4d 62 63 73 28 63 6f  r *utf8ToMbcs(co
f7b10 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
f7b20 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46  ame){.  char *zF
f7b30 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 20 20 57  ilenameMbcs;.  W
f7b40 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a  CHAR *zTmpWide;.
f7b50 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 75 74  .  zTmpWide = ut
f7b60 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c  f8ToUnicode(zFil
f7b70 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54  ename);.  if( zT
f7b80 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20  mpWide==0 ){.   
f7b90 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
f7ba0 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 20 3d   zFilenameMbcs =
f7bb0 20 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 7a   unicodeToMbcs(z
f7bc0 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65  TmpWide);.  free
f7bd0 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65  (zTmpWide);.  re
f7be0 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d 62  turn zFilenameMb
f7bf0 63 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54  cs;.}..#if SQLIT
f7c00 45 5f 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a  E_OS_WINCE./****
f7c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7c50 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65  *****.** This se
f7c60 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 63  ction contains c
f7c70 6f 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f 6e  ode for WinCE on
f7c80 6c 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e  ly..*/./*.** Win
f7c90 64 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f 74 20  dowsCE does not 
f7ca0 68 61 76 65 20 61 20 6c 6f 63 61 6c 74 69 6d 65  have a localtime
f7cb0 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f  () function.  So
f7cc0 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 73 75 62   create a.** sub
f7cd0 73 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74 72 75  stitute..*/.stru
f7ce0 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c  ct tm *__cdecl l
f7cf0 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20 74  ocaltime(const t
f7d00 69 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 73 74  ime_t *t).{.  st
f7d10 61 74 69 63 20 73 74 72 75 63 74 20 74 6d 20 79  atic struct tm y
f7d20 3b 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 54 6d  ;.  FILETIME uTm
f7d30 2c 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 4d 54  , lTm;.  SYSTEMT
f7d40 49 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c 69 74  IME pTm;.  sqlit
f7d50 65 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a 20 20  e3_int64 t64;.  
f7d60 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 34 20  t64 = *t;.  t64 
f7d70 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34 34 37  = (t64 + 1164447
f7d80 33 36 30 30 29 2a 31 30 30 30 30 30 30 30 3b 0a  3600)*10000000;.
f7d90 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 65 54    uTm.dwLowDateT
f7da0 69 6d 65 20 3d 20 28 44 57 4f 52 44 29 28 74 36  ime = (DWORD)(t6
f7db0 34 20 26 20 30 78 46 46 46 46 46 46 46 46 29 3b  4 & 0xFFFFFFFF);
f7dc0 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74  .  uTm.dwHighDat
f7dd0 65 54 69 6d 65 3d 20 28 44 57 4f 52 44 29 28 74  eTime= (DWORD)(t
f7de0 36 34 20 3e 3e 20 33 32 29 3b 0a 20 20 46 69 6c  64 >> 32);.  Fil
f7df0 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65  eTimeToLocalFile
f7e00 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b  Time(&uTm,&lTm);
f7e10 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73  .  FileTimeToSys
f7e20 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54  temTime(&lTm,&pT
f7e30 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20  m);.  y.tm_year 
f7e40 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39  = pTm.wYear - 19
f7e50 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d  00;.  y.tm_mon =
f7e60 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b   pTm.wMonth - 1;
f7e70 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70  .  y.tm_wday = p
f7e80 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20  Tm.wDayOfWeek;. 
f7e90 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d   y.tm_mday = pTm
f7ea0 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f  .wDay;.  y.tm_ho
f7eb0 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a  ur = pTm.wHour;.
f7ec0 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d    y.tm_min = pTm
f7ed0 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d  .wMinute;.  y.tm
f7ee0 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f  _sec = pTm.wSeco
f7ef0 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b  nd;.  return &y;
f7f00 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c  .}../* This will
f7f10 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64   never be called
f7f20 2c 20 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f  , but defined to
f7f30 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63   make the code c
f7f40 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  ompile */.#defin
f7f50 65 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 61  e GetTempPathA(a
f7f60 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63  ,b)..#define Loc
f7f70 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29  kFile(a,b,c,d,e)
f7f80 20 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b         winceLock
f7f90 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64  File(&a, b, c, d
f7fa0 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c  , e).#define Unl
f7fb0 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c  ockFile(a,b,c,d,
f7fc0 65 29 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f  e)     winceUnlo
f7fd0 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c  ckFile(&a, b, c,
f7fe0 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c   d, e).#define L
f7ff0 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c  ockFileEx(a,b,c,
f8000 64 2c 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f  d,e,f)   winceLo
f8010 63 6b 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20  ckFileEx(&a, b, 
f8020 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65  c, d, e, f)..#de
f8030 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57  fine HANDLE_TO_W
f8040 49 4e 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69  INFILE(a) (winFi
f8050 6c 65 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b  le*)&((char*)a)[
f8060 2d 28 69 6e 74 29 6f 66 66 73 65 74 6f 66 28 77  -(int)offsetof(w
f8070 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a  inFile,h)]../*.*
f8080 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b  * Acquire a lock
f8090 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68   on the handle h
f80a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
f80b0 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72  winceMutexAcquir
f80c0 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20  e(HANDLE h){.   
f80d0 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20  DWORD dwErr;.   
f80e0 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20  do {.     dwErr 
f80f0 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f  = WaitForSingleO
f8100 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54  bject(h, INFINIT
f8110 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28  E);.   } while (
f8120 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42  dwErr != WAIT_OB
f8130 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20  JECT_0 && dwErr 
f8140 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45  != WAIT_ABANDONE
f8150 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  D);.}./*.** Rele
f8160 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69  ase a lock acqui
f8170 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65  red by winceMute
f8180 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64  xAcquire().*/.#d
f8190 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78  efine winceMutex
f81a0 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61  Release(h) Relea
f81b0 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a  seMutex(h)../*.*
f81c0 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74  * Create the mut
f81d0 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65  ex and shared me
f81e0 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f  mory used for lo
f81f0 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c  cking in the fil
f8200 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20  e.** descriptor 
f8210 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20  pFile.*/.static 
f8220 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65  BOOL winceCreate
f8230 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20  Lock(const char 
f8240 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46  *zFilename, winF
f8250 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57  ile *pFile){.  W
f8260 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43  CHAR *zTok;.  WC
f8270 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66  HAR *zName = utf
f8280 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
f8290 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49  name);.  BOOL bI
f82a0 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f  nit = TRUE;..  /
f82b0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
f82c0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20   local lockdata 
f82d0 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28  */.  ZeroMemory(
f82e0 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73  &pFile->local, s
f82f0 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63  izeof(pFile->loc
f8300 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c  al));..  /* Repl
f8310 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73  ace the backslas
f8320 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c  hes from the fil
f8330 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63  ename and lowerc
f8340 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64  ase it.  ** to d
f8350 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61  erive a mutex na
f8360 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20  me. */.  zTok = 
f8370 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65  CharLowerW(zName
f8380 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b  );.  for (;*zTok
f8390 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66  ;zTok++){.    if
f83a0 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29   (*zTok == '\\')
f83b0 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20   *zTok = '_';.  
f83c0 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f  }..  /* Create/o
f83d0 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75  pen the named mu
f83e0 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e  tex */.  pFile->
f83f0 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d  hMutex = CreateM
f8400 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53  utexW(NULL, FALS
f8410 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20  E, zName);.  if 
f8420 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  (!pFile->hMutex)
f8430 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
f8440 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
f8450 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65  Error();.    fre
f8460 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  e(zName);.    re
f8470 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  turn FALSE;.  }.
f8480 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68  .  /* Acquire th
f8490 65 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63  e mutex before c
f84a0 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77  ontinuing */.  w
f84b0 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65  inceMutexAcquire
f84c0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f84d0 0a 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74  .  .  /* Since t
f84e0 68 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65  he names of name
f84f0 64 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70  d mutexes, semap
f8500 68 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70  hores, file mapp
f8510 69 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20  ings etc are .  
f8520 2a 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76  ** case-sensitiv
f8530 65 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  e, take advantag
f8540 65 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70  e of that by upp
f8550 65 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74  ercasing the mut
f8560 65 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64  ex name.  ** and
f8570 20 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74   using that as t
f8580 68 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61  he shared filema
f8590 70 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f  pping name..  */
f85a0 0a 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e  .  CharUpperW(zN
f85b0 61 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68  ame);.  pFile->h
f85c0 53 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46  Shared = CreateF
f85d0 69 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41  ileMappingW(INVA
f85e0 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
f85f0 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20  , NULL,.        
f8600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
f8620 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30  AGE_READWRITE, 0
f8630 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f  , sizeof(winceLo
f8640 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ck),.           
f8650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8660 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d              zNam
f8670 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20  e);  ..  /* Set 
f8680 61 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69  a flag that indi
f8690 63 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20  cates we're the 
f86a0 66 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20  first to create 
f86b0 74 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74  the memory so it
f86c0 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a   .  ** must be z
f86d0 65 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20  ero-initialized 
f86e0 2a 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74  */.  if (GetLast
f86f0 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52  Error() == ERROR
f8700 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29  _ALREADY_EXISTS)
f8710 7b 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41  {.    bInit = FA
f8720 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65  LSE;.  }..  free
f8730 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49  (zName);..  /* I
f8740 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69  f we succeeded i
f8750 6e 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61  n making the sha
f8760 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c  red memory handl
f8770 65 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20  e, map it. */.  
f8780 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  if (pFile->hShar
f8790 65 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ed){.    pFile->
f87a0 73 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c  shared = (winceL
f87b0 6f 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69  ock*)MapViewOfFi
f87c0 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  le(pFile->hShare
f87d0 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  d, .            
f87e0 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46   FILE_MAP_READ|F
f87f0 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30  ILE_MAP_WRITE, 0
f8800 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63  , 0, sizeof(winc
f8810 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20  eLock));.    /* 
f8820 49 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65  If mapping faile
f8830 64 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61  d, close the sha
f8840 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c  red memory handl
f8850 65 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a  e and erase it *
f8860 2f 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65  /.    if (!pFile
f8870 2d 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20  ->shared){.     
f8880 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f8890 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
f88a0 28 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48  ();.      CloseH
f88b0 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68  andle(pFile->hSh
f88c0 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69  ared);.      pFi
f88d0 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55  le->hShared = NU
f88e0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  LL;.    }.  }.. 
f88f0 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65   /* If shared me
f8900 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62  mory could not b
f8910 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20  e created, then 
f8920 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20  close the mutex 
f8930 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66  and fail */.  if
f8940 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64   (pFile->hShared
f8950 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77   == NULL){.    w
f8960 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
f8970 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f8980 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
f8990 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f89a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74  .    pFile->hMut
f89b0 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72  ex = NULL;.    r
f89c0 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d  eturn FALSE;.  }
f89d0 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  .  .  /* Initial
f89e0 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d  ize the shared m
f89f0 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73  emory if we're s
f8a00 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20  upposed to */.  
f8a10 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20  if (bInit) {.   
f8a20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c   ZeroMemory(pFil
f8a30 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f  e->shared, sizeo
f8a40 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20  f(winceLock));. 
f8a50 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   }..  winceMutex
f8a60 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
f8a70 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
f8a80 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   TRUE;.}../*.** 
f8a90 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74  Destroy the part
f8aa0 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74   of winFile that
f8ab0 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63   deals with winc
f8ac0 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69  e locks.*/.stati
f8ad0 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74  c void winceDest
f8ae0 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  royLock(winFile 
f8af0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70  *pFile){.  if (p
f8b00 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20  File->hMutex){. 
f8b10 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68     /* Acquire th
f8b20 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77  e mutex */.    w
f8b30 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65  inceMutexAcquire
f8b40 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f8b50 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  ..    /* The fol
f8b60 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68  lowing blocks sh
f8b70 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73  ould probably as
f8b80 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f  sert in debug mo
f8b90 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20  de, but they.   
f8ba0 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e      are to clean
f8bb0 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c  up in case any l
f8bc0 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70  ocks remained op
f8bd0 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46  en */.    if (pF
f8be0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
f8bf0 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ers){.      pFil
f8c00 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64  e->shared->nRead
f8c10 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20  ers --;.    }.  
f8c20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
f8c30 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20  al.bReserved){. 
f8c40 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f8c50 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20  ed->bReserved = 
f8c60 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20  FALSE;.    }.   
f8c70 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f8c80 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20  l.bPending){.   
f8c90 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
f8ca0 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c  ->bPending = FAL
f8cb0 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  SE;.    }.    if
f8cc0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
f8cd0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20  Exclusive){.    
f8ce0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8cf0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41  >bExclusive = FA
f8d00 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  LSE;.    }..    
f8d10 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20  /* De-reference 
f8d20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f  and close our co
f8d30 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  py of the shared
f8d40 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a   memory handle *
f8d50 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f  /.    UnmapViewO
f8d60 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61  fFile(pFile->sha
f8d70 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48  red);.    CloseH
f8d80 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68  andle(pFile->hSh
f8d90 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  ared);..    /* D
f8da0 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74  one with the mut
f8db0 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d  ex */.    winceM
f8dc0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
f8dd0 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a  e->hMutex);    .
f8de0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28      CloseHandle(
f8df0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f8e00 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65      pFile->hMute
f8e10 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a  x = NULL;.  }.}.
f8e20 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65  ./* .** An imple
f8e30 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
f8e40 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20   LockFile() API 
f8e50 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77  of windows for w
f8e60 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  ince.*/.static B
f8e70 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  OOL winceLockFil
f8e80 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46  e(.  HANDLE *phF
f8e90 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  ile,.  DWORD dwF
f8ea0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20  ileOffsetLow,.  
f8eb0 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73  DWORD dwFileOffs
f8ec0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20  etHigh,.  DWORD 
f8ed0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8ee0 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  LockLow,.  DWORD
f8ef0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f8f00 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77  oLockHigh.){.  w
f8f10 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
f8f20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c  HANDLE_TO_WINFIL
f8f30 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f  E(phFile);.  BOO
f8f40 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53  L bReturn = FALS
f8f50 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  E;..  UNUSED_PAR
f8f60 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 66  AMETER(dwFileOff
f8f70 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 53  setHigh);.  UNUS
f8f80 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75  ED_PARAMETER(nNu
f8f90 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
f8fa0 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 20 28 21  kHigh);..  if (!
f8fb0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72  pFile->hMutex) r
f8fc0 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69  eturn TRUE;.  wi
f8fd0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
f8fe0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f8ff0 0a 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e  .  /* Wanting an
f9000 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f   exclusive lock?
f9010 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65   */.  if (dwFile
f9020 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57  OffsetLow == (DW
f9030 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54  ORD)SHARED_FIRST
f9040 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62  .       && nNumb
f9050 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f9060 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41  ow == (DWORD)SHA
f9070 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69  RED_SIZE){.    i
f9080 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  f (pFile->shared
f9090 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20  ->nReaders == 0 
f90a0 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  && pFile->shared
f90b0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20  ->bExclusive == 
f90c0 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65  0){.       pFile
f90d0 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75  ->shared->bExclu
f90e0 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20  sive = TRUE;.   
f90f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
f9100 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52  .bExclusive = TR
f9110 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75  UE;.       bRetu
f9120 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
f9130 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20  .  }..  /* Want 
f9140 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b  a read-only lock
f9150 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  ? */.  else if (
f9160 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f9170 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f9180 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20  _FIRST &&.      
f9190 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79       nNumberOfBy
f91a0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
f91b0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c  1){.    if (pFil
f91c0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c  e->shared->bExcl
f91d0 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20  usive == 0){.   
f91e0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
f91f0 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20  nReaders ++;.   
f9200 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
f9210 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20  cal.nReaders == 
f9220 31 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c  1){.        pFil
f9230 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64  e->shared->nRead
f9240 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  ers ++;.      }.
f9250 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
f9260 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
f9270 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 65 6e  .  /* Want a pen
f9280 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  ding lock? */.  
f9290 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f  else if (dwFileO
f92a0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
f92b0 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  RD)PENDING_BYTE 
f92c0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  && nNumberOfByte
f92d0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  sToLockLow == 1)
f92e0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70  {.    /* If no p
f92f0 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20  ending lock has 
f9300 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74  been acquired, t
f9310 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a  hen acquire it *
f9320 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  /.    if (pFile-
f9330 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e  >shared->bPendin
f9340 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20  g == 0) {.      
f9350 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f9360 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a  Pending = TRUE;.
f9370 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
f9380 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52  al.bPending = TR
f9390 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72  UE;.      bRetur
f93a0 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
f93b0 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61    }..  /* Want a
f93c0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20   reserved lock? 
f93d0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77  */.  else if (dw
f93e0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d  FileOffsetLow ==
f93f0 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44   (DWORD)RESERVED
f9400 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72  _BYTE && nNumber
f9410 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77  OfBytesToLockLow
f9420 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28   == 1){.    if (
f9430 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f9440 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20 7b  Reserved == 0) {
f9450 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
f9460 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20  ared->bReserved 
f9470 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46  = TRUE;.      pF
f9480 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65  ile->local.bRese
f9490 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20  rved = TRUE;.   
f94a0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
f94b0 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
f94c0 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73  winceMutexReleas
f94d0 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
f94e0 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75  ;.  return bRetu
f94f0 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  rn;.}../*.** An 
f9500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
f9510 66 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 6c 65  f the UnlockFile
f9520 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20   API of windows 
f9530 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61  for wince.*/.sta
f9540 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e  tic BOOL winceUn
f9550 6c 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44  lockFile(.  HAND
f9560 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57  LE *phFile,.  DW
f9570 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
f9580 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  Low,.  DWORD dwF
f9590 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20  ileOffsetHigh,. 
f95a0 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
f95b0 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77  BytesToUnlockLow
f95c0 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ,.  DWORD nNumbe
f95d0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
f95e0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c  High.){.  winFil
f95f0 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c  e *pFile = HANDL
f9600 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46  E_TO_WINFILE(phF
f9610 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65  ile);.  BOOL bRe
f9620 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20  turn = FALSE;.. 
f9630 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f9640 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69  R(dwFileOffsetHi
f9650 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  gh);.  UNUSED_PA
f9660 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f  RAMETER(nNumberO
f9670 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69  fBytesToUnlockHi
f9680 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69  gh);..  if (!pFi
f9690 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75  le->hMutex) retu
f96a0 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65  rn TRUE;.  wince
f96b0 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
f96c0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20  le->hMutex);..  
f96d0 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72  /* Releasing a r
f96e0 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e  eader lock or an
f96f0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
f9700 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
f9710 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
f9720 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29  RD)SHARED_FIRST)
f9730 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20  {.    /* Did we 
f9740 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
f9750 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69  e lock? */.    i
f9760 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f9770 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20  bExclusive){.   
f9780 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65     assert(nNumbe
f9790 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
f97a0 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48  Low == (DWORD)SH
f97b0 41 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20  ARED_SIZE);.    
f97c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
f97d0 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
f97e0 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
f97f0 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
f9800 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ve = FALSE;.    
f9810 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
f9820 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
f9830 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65  Did we just have
f9840 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20   a reader lock? 
f9850 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  */.    else if (
f9860 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
f9870 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73  aders){.      as
f9880 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79  sert(nNumberOfBy
f9890 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f98a0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f98b0 53 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f  SIZE || nNumberO
f98c0 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f  fBytesToUnlockLo
f98d0 77 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70  w == 1);.      p
f98e0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
f98f0 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69  ders --;.      i
f9900 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f9910 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20  nReaders == 0). 
f9920 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
f9930 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
f9940 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20  eaders --;.     
f9950 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e   }.      bReturn
f9960 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f9970 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69   }..  /* Releasi
f9980 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ng a pending loc
f9990 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
f99a0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f99b0 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e  == (DWORD)PENDIN
f99c0 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  G_BYTE && nNumbe
f99d0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
f99e0 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69  Low == 1){.    i
f99f0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f9a00 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
f9a10 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50   pFile->local.bP
f9a20 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
f9a30 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f9a40 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
f9a50 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
f9a60 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
f9a70 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65   }.  }.  /* Rele
f9a80 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64  asing a reserved
f9a90 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20   lock */.  else 
f9aa0 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f9ab0 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45  Low == (DWORD)RE
f9ac0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e  SERVED_BYTE && n
f9ad0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
f9ae0 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a  nlockLow == 1){.
f9af0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c      if (pFile->l
f9b00 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20  ocal.bReserved) 
f9b10 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
f9b20 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d  ocal.bReserved =
f9b30 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46   FALSE;.      pF
f9b40 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65  ile->shared->bRe
f9b50 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a  served = FALSE;.
f9b60 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
f9b70 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
f9b80 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c  .  winceMutexRel
f9b90 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ease(pFile->hMut
f9ba0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52  ex);.  return bR
f9bb0 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
f9bc0 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  An implementatio
f9bd0 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c  n of the LockFil
f9be0 65 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e  eEx() API of win
f9bf0 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a  dows for wince.*
f9c00 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69  /.static BOOL wi
f9c10 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20  nceLockFileEx(. 
f9c20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c   HANDLE *phFile,
f9c30 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73  .  DWORD dwFlags
f9c40 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65  ,.  DWORD dwRese
f9c50 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  rved,.  DWORD nN
f9c60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
f9c70 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e  ckLow,.  DWORD n
f9c80 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f9c90 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45  ockHigh,.  LPOVE
f9ca0 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61  RLAPPED lpOverla
f9cb0 70 70 65 64 0a 29 7b 0a 20 20 55 4e 55 53 45 44  pped.){.  UNUSED
f9cc0 5f 50 41 52 41 4d 45 54 45 52 28 64 77 52 65 73  _PARAMETER(dwRes
f9cd0 65 72 76 65 64 29 3b 0a 20 20 55 4e 55 53 45 44  erved);.  UNUSED
f9ce0 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62  _PARAMETER(nNumb
f9cf0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48  erOfBytesToLockH
f9d00 69 67 68 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  igh);..  /* If t
f9d10 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20  he caller wants 
f9d20 61 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f  a shared read lo
f9d30 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73  ck, forward this
f9d40 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69   call.  ** to wi
f9d50 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20  nceLockFile */. 
f9d60 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65   if (lpOverlappe
f9d70 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 28 44 57  d->Offset == (DW
f9d80 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54  ORD)SHARED_FIRST
f9d90 20 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67   &&.      dwFlag
f9da0 73 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20  s == 1 &&.      
f9db0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9dc0 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52  LockLow == (DWOR
f9dd0 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a  D)SHARED_SIZE){.
f9de0 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 63 65      return wince
f9df0 4c 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c  LockFile(phFile,
f9e00 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30   SHARED_FIRST, 0
f9e10 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 1, 0);.  }.  r
f9e20 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f  eturn FALSE;.}./
f9e30 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  *.** End of the 
f9e40 73 70 65 63 69 61 6c 20 63 6f 64 65 20 66 6f 72  special code for
f9e50 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a   wince.*********
f9e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ea0 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ****/.#endif /* 
f9eb0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
f9ec0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
f9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9f10 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  **.** The next g
f9f20 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73  roup of routines
f9f30 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49   implement the I
f9f40 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69  /O methods speci
f9f50 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73  fied.** by the s
f9f60 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
f9f70 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a  s object..******
f9f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
f9fd0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
f9fe0 2a 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 6f 72  *.** It is repor
f9ff0 74 65 64 20 74 68 61 74 20 61 6e 20 61 74 74 65  ted that an atte
fa000 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 20 68  mpt to close a h
fa010 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f 6d 65  andle might some
fa020 74 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e 20 20  times.** fail.  
fa030 54 68 69 73 20 69 73 20 61 20 76 65 72 79 20 75  This is a very u
fa040 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 73 75  nreasonable resu
fa050 6c 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 73 20  lt, but windows 
fa060 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20  is notorious.** 
fa070 66 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 61 73  for being unreas
fa080 6f 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f 20 6e  onable so I do n
fa090 6f 74 20 64 6f 75 62 74 20 74 68 61 74 20 69 74  ot doubt that it
fa0a0 20 6d 69 67 68 74 20 68 61 70 70 65 6e 2e 20 20   might happen.  
fa0b0 49 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 65 20  If.** the close 
fa0c0 66 61 69 6c 73 2c 20 77 65 20 70 61 75 73 65 20  fails, we pause 
fa0d0 66 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 65 63  for 100 millisec
fa0e0 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 61 67 61  onds and try aga
fa0f0 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e 79 20  in.  As.** many 
fa100 61 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45  as MX_CLOSE_ATTE
fa110 4d 50 54 20 61 74 74 65 6d 70 74 73 20 74 6f 20  MPT attempts to 
fa120 63 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 6c 65  close the handle
fa130 20 61 72 65 20 6d 61 64 65 20 62 65 66 6f 72 65   are made before
fa140 0a 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 61 6e  .** giving up an
fa150 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65  d returning an e
fa160 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rror..*/.#define
fa170 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50   MX_CLOSE_ATTEMP
fa180 54 20 33 0a 73 74 61 74 69 63 20 69 6e 74 20 77  T 3.static int w
fa190 69 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  inClose(sqlite3_
fa1a0 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74  file *id){.  int
fa1b0 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a 20 20   rc, cnt = 0;.  
fa1c0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fa1d0 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a   (winFile*)id;..
fa1e0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fa1f0 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43  );.  OSTRACE2("C
fa200 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c  LOSE %d\n", pFil
fa210 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20  e->h);.  do{.   
fa220 20 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c   rc = CloseHandl
fa230 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d  e(pFile->h);.  }
fa240 77 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20  while( rc==0 && 
fa250 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45  ++cnt < MX_CLOSE
fa260 5f 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65  _ATTEMPT && (Sle
fa270 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23  ep(100), 1) );.#
fa280 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
fa290 43 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45  CE.#define WINCE
fa2a0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
fa2b0 54 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74  TS 3.  winceDest
fa2c0 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  royLock(pFile);.
fa2d0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65    if( pFile->zDe
fa2e0 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20  leteOnClose ){. 
fa2f0 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a     int cnt = 0;.
fa300 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20      while(.     
fa310 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65        DeleteFile
fa320 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  W(pFile->zDelete
fa330 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20  OnClose)==0.    
fa340 20 20 20 20 26 26 20 47 65 74 46 69 6c 65 41 74      && GetFileAt
fa350 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d  tributesW(pFile-
fa360 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29  >zDeleteOnClose)
fa370 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 20  !=0xffffffff .  
fa380 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c        && cnt++ <
fa390 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f   WINCE_DELETION_
fa3a0 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a  ATTEMPTS.    ){.
fa3b0 20 20 20 20 20 20 20 53 6c 65 65 70 28 31 30 30         Sleep(100
fa3c0 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69  );  /* Wait a li
fa3d0 74 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69  ttle before tryi
fa3e0 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20  ng again */.    
fa3f0 7d 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c 65  }.    free(pFile
fa400 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65  ->zDeleteOnClose
fa410 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
fa420 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b  OpenCounter(-1);
fa430 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53  .  return rc ? S
fa440 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
fa450 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a  E_IOERR;.}../*.*
fa460 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74  * Some microsoft
fa470 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20   compilers lack 
fa480 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  this definition.
fa490 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41  .*/.#ifndef INVA
fa4a0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49  LID_SET_FILE_POI
fa4b0 4e 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e  NTER.# define IN
fa4c0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
fa4d0 4f 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d  OINTER ((DWORD)-
fa4e0 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  1).#endif../*.**
fa4f0 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
fa500 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75  a file into a bu
fa510 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51  ffer.  Return SQ
fa520 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a  LITE_OK if all.*
fa530 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61  * bytes were rea
fa540 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  d successfully a
fa550 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  nd SQLITE_IOERR 
fa560 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
fa570 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74  .** wrong..*/.st
fa580 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64  atic int winRead
fa590 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
fa5a0 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f   *id,          /
fa5b0 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66  * File to read f
fa5c0 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  rom */.  void *p
fa5d0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
fa5e0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e      /* Write con
fa5f0 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62  tent into this b
fa600 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61  uffer */.  int a
fa610 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
fa620 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
fa630 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64  of bytes to read
fa640 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
fa650 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20  t64 offset      
fa660 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e   /* Begin readin
fa670 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74  g at this offset
fa680 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70   */.){.  LONG up
fa690 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29  perBits = (LONG)
fa6a0 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20  ((offset>>32) & 
fa6b0 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c  0x7fffffff);.  L
fa6c0 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20  ONG lowerBits = 
fa6d0 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20  (LONG)(offset & 
fa6e0 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44  0xffffffff);.  D
fa6f0 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69  WORD rc;.  winFi
fa700 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fa710 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fa720 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44  D error;.  DWORD
fa730 20 67 6f 74 3b 0a 0a 20 20 61 73 73 65 72 74 28   got;..  assert(
fa740 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
fa750 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
fa760 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fa770 52 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45  READ);.  OSTRACE
fa780 33 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d  3("READ %d lock=
fa790 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
fa7a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
fa7b0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c  );.  rc = SetFil
fa7c0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e  ePointer(pFile->
fa7d0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75  h, lowerBits, &u
fa7e0 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42  pperBits, FILE_B
fa7f0 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d  EGIN);.  if( rc=
fa800 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c  =INVALID_SET_FIL
fa810 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72  E_POINTER && (er
fa820 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72  ror=GetLastError
fa830 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b  ())!=NO_ERROR ){
fa840 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
fa850 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20  Errno = error;. 
fa860 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fa870 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28  _FULL;.  }.  if(
fa880 20 21 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65   !ReadFile(pFile
fa890 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
fa8a0 26 67 6f 74 2c 20 30 29 20 29 7b 0a 20 20 20 20  &got, 0) ){.    
fa8b0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
fa8c0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fa8d0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
fa8e0 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
fa8f0 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d  .  }.  if( got==
fa900 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20  (DWORD)amt ){.  
fa910 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fa920 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
fa930 20 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73   /* Unread parts
fa940 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d   of the buffer m
fa950 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c  ust be zero-fill
fa960 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74  ed */.    memset
fa970 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b  (&((char*)pBuf)[
fa980 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74  got], 0, amt-got
fa990 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
fa9a0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
fa9b0 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  _READ;.  }.}../*
fa9c0 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66  .** Write data f
fa9d0 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74  rom a buffer int
fa9e0 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72  o a file.  Retur
fa9f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
faa00 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d  uccess.** or som
faa10 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  e other error co
faa20 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a  de on failure..*
faa30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
faa40 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33  Write(.  sqlite3
faa50 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
faa60 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72     /* File to wr
faa70 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f  ite into */.  co
faa80 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  nst void *pBuf, 
faa90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
faaa0 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74  ytes to be writt
faab0 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  en */.  int amt,
faac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
faad0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
faae0 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f  ytes to write */
faaf0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
fab00 20 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a 20   offset      /* 
fab10 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  Offset into the 
fab20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72  file to begin wr
fab30 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20  iting at */.){. 
fab40 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
fab50 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74  = (LONG)((offset
fab60 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66  >>32) & 0x7fffff
fab70 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65  ff);.  LONG lowe
fab80 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f  rBits = (LONG)(o
fab90 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66  ffset & 0xffffff
faba0 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b  ff);.  DWORD rc;
fabb0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
fabc0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
fabd0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b  ;.  DWORD error;
fabe0 0a 20 20 44 57 4f 52 44 20 77 72 6f 74 65 20 3d  .  DWORD wrote =
fabf0 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69   0;..  assert( i
fac00 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
fac10 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e  teIOError(return
fac20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
fac30 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  ITE);.  Simulate
fac40 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65  DiskfullError(re
fac50 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
fac60 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57  );.  OSTRACE3("W
fac70 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  RITE %d lock=%d\
fac80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
fac90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a  ile->locktype);.
faca0 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f    rc = SetFilePo
facb0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20  inter(pFile->h, 
facc0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65  lowerBits, &uppe
facd0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49  rBits, FILE_BEGI
face0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e  N);.  if( rc==IN
facf0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
fad00 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72  OINTER && (error
fad10 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29  =GetLastError())
fad20 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  !=NO_ERROR ){.  
fad30 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
fad40 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20  no = error;.    
fad50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
fad60 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  LL;.  }.  assert
fad70 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69  ( amt>0 );.  whi
fad80 6c 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20  le(.     amt>0. 
fad90 20 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69      && (rc = Wri
fada0 74 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  teFile(pFile->h,
fadb0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f   pBuf, amt, &wro
fadc0 74 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20  te, 0))!=0.     
fadd0 26 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a  && wrote>0.  ){.
fade0 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65      amt -= wrote
fadf0 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
fae00 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
fae10 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72  e];.  }.  if( !r
fae20 63 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72  c || amt>(int)wr
fae30 6f 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  ote ){.    pFile
fae40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
fae50 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fae60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fae70 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75  FULL;.  }.  retu
fae80 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
fae90 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
faea0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20  an open file to 
faeb0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65  a specified size
faec0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
faed0 69 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  inTruncate(sqlit
faee0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c  e3_file *id, sql
faef0 69 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65  ite3_int64 nByte
faf00 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42  ){.  LONG upperB
faf10 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6e 42  its = (LONG)((nB
faf20 79 74 65 3e 3e 33 32 29 20 26 20 30 78 37 66 66  yte>>32) & 0x7ff
faf30 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c  fffff);.  LONG l
faf40 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47  owerBits = (LONG
faf50 29 28 6e 42 79 74 65 20 26 20 30 78 66 66 66 66  )(nByte & 0xffff
faf60 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72  ffff);.  DWORD r
faf70 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  c;.  winFile *pF
faf80 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
faf90 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f  id;.  DWORD erro
fafa0 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64  r;..  assert( id
fafb0 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
fafc0 33 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25  3("TRUNCATE %d %
fafd0 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  lld\n", pFile->h
fafe0 2c 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75  , nByte);.  Simu
faff0 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
fb000 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fb010 54 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20  TRUNCATE);.  rc 
fb020 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
fb030 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
fb040 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
fb050 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
fb060 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44   if( rc==INVALID
fb070 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
fb080 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c  R && (error=GetL
fb090 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f  astError())!=NO_
fb0a0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69  ERROR ){.    pFi
fb0b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
fb0c0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72  error;.    retur
fb0d0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
fb0e0 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 2f  RUNCATE;.  }.  /
fb0f0 2a 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 20 77  * SetEndOfFile w
fb100 69 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42 79 74  ill fail if nByt
fb110 65 20 69 73 20 6e 65 67 61 74 69 76 65 20 2a 2f  e is negative */
fb120 0a 20 20 69 66 28 20 21 53 65 74 45 6e 64 4f 66  .  if( !SetEndOf
fb130 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29  File(pFile->h) )
fb140 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
fb150 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
fb160 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74  Error();.    ret
fb170 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fb180 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20  _TRUNCATE;.  }. 
fb190 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fb1a0 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  K;.}..#ifdef SQL
fb1b0 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43  ITE_TEST./*.** C
fb1c0 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
fb1d0 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64  of fullsyncs and
fb1e0 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20   normal syncs.  
fb1f0 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
fb200 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e  test.** that syn
fb210 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73  cs and fullsyncs
fb220 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74   are occuring at
fb230 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73   the right times
fb240 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
fb250 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  int sqlite3_sync
fb260 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49  _count = 0;.SQLI
fb270 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
fb280 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
fb290 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  t = 0;.#endif../
fb2a0 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61  *.** Make sure a
fb2b0 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70  ll writes to a p
fb2c0 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61  articular file a
fb2d0 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20  re committed to 
fb2e0 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  disk..*/.static 
fb2f0 69 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69  int winSync(sqli
fb300 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
fb310 74 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64 65  t flags){.#ifnde
fb320 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
fb330 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
fb340 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
fb350 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  ;..  assert( id!
fb360 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  =0 );.  OSTRACE3
fb370 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25  ("SYNC %d lock=%
fb380 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
fb390 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
fb3a0 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  ;.#else.  UNUSED
fb3b0 5f 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a  _PARAMETER(id);.
fb3c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
fb3d0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55  QLITE_TEST.  UNU
fb3e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c  SED_PARAMETER(fl
fb3f0 61 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66  ags);.#else.  if
fb400 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
fb410 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20  _SYNC_FULL ){.  
fb420 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79    sqlite3_fullsy
fb430 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a  nc_count++;.  }.
fb440 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63    sqlite3_sync_c
fb450 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
fb460 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
fb470 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
fb480 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
fb490 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
fb4a0 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20   a.  ** no-op.  
fb4b0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
fb4c0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74  _NO_SYNC.    ret
fb4d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
fb4e0 65 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68  else.  if( Flush
fb4f0 46 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c  FileBuffers(pFil
fb500 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74  e->h) ){.    ret
fb510 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
fb520 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c   }else{.    pFil
fb530 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
fb540 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fb550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb560 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65 6e 64  _IOERR;.  }.#end
fb570 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  if.}../*.** Dete
fb580 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e  rmine the curren
fb590 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65  t size of a file
fb5a0 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61   in bytes.*/.sta
fb5b0 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 53  tic int winFileS
fb5c0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
fb5d0 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e   *id, sqlite3_in
fb5e0 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44  t64 *pSize){.  D
fb5f0 57 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a  WORD upperBits;.
fb600 20 20 44 57 4f 52 44 20 6c 6f 77 65 72 42 69 74    DWORD lowerBit
fb610 73 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  s;.  winFile *pF
fb620 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
fb630 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f  id;.  DWORD erro
fb640 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64  r;..  assert( id
fb650 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
fb660 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
fb670 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54  SQLITE_IOERR_FST
fb680 41 54 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73  AT);.  lowerBits
fb690 20 3d 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70   = GetFileSize(p
fb6a0 46 69 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42  File->h, &upperB
fb6b0 69 74 73 29 3b 0a 20 20 69 66 28 20 20 20 28 6c  its);.  if(   (l
fb6c0 6f 77 65 72 42 69 74 73 20 3d 3d 20 49 4e 56 41  owerBits == INVA
fb6d0 4c 49 44 5f 46 49 4c 45 5f 53 49 5a 45 29 0a 20  LID_FILE_SIZE). 
fb6e0 20 20 20 20 26 26 20 28 28 65 72 72 6f 72 20 3d      && ((error =
fb6f0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29   GetLastError())
fb700 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 29 0a   != NO_ERROR) ).
fb710 20 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c    {.    pFile->l
fb720 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72  astErrno = error
fb730 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
fb740 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
fb750 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20  .  }.  *pSize = 
fb760 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  (((sqlite3_int64
fb770 29 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29  )upperBits)<<32)
fb780 20 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20   + lowerBits;.  
fb790 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
fb7a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46  ;.}../*.** LOCKF
fb7b0 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41  ILE_FAIL_IMMEDIA
fb7c0 54 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65  TELY is undefine
fb7d0 64 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77  d on some Window
fb7e0 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69  s systems..*/.#i
fb7f0 66 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46  fndef LOCKFILE_F
fb800 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a  AIL_IMMEDIATELY.
fb810 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c  # define LOCKFIL
fb820 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45  E_FAIL_IMMEDIATE
fb830 4c 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  LY 1.#endif../*.
fb840 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61  ** Acquire a rea
fb850 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66  der lock..** Dif
fb860 66 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69  ferent API routi
fb870 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64  nes are called d
fb880 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
fb890 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a  her or not this.
fb8a0 2a 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57  ** is Win95 or W
fb8b0 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  inNT..*/.static 
fb8c0 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28  int getReadLock(
fb8d0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  winFile *pFile){
fb8e0 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66  .  int res;.  if
fb8f0 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
fb900 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b  OVERLAPPED ovlp;
fb910 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74  .    ovlp.Offset
fb920 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
fb930 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74  .    ovlp.Offset
fb940 48 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76  High = 0;.    ov
fb950 6c 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20  lp.hEvent = 0;. 
fb960 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c     res = LockFil
fb970 65 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  eEx(pFile->h, LO
fb980 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45  CKFILE_FAIL_IMME
fb990 44 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20  DIATELY,.       
fb9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
fb9b0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c   SHARED_SIZE, 0,
fb9c0 20 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54   &ovlp);./* isNT
fb9d0 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
fb9e0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
fb9f0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
fba00 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
fba10 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
fba20 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
fba30 65 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20  e{.    int lk;. 
fba40 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
fba50 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29  mness(sizeof(lk)
fba60 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c  , &lk);.    pFil
fba70 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
fba80 65 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20  e = (short)((lk 
fba90 26 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53  & 0x7fffffff)%(S
fbaa0 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29  HARED_SIZE - 1))
fbab0 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b  ;.    res = Lock
fbac0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
fbad0 48 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c  HARED_FIRST+pFil
fbae0 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
fbaf0 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e  e, 0, 1, 0);.#en
fbb00 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65  dif.  }.  if( re
fbb10 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46  s == 0 ){.    pF
fbb20 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
fbb30 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
fbb40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65  .  }.  return re
fbb50 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f  s;.}../*.** Undo
fbb60 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73   a readlock.*/.s
fbb70 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b  tatic int unlock
fbb80 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65  ReadLock(winFile
fbb90 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20   *pFile){.  int 
fbba0 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28  res;.  if( isNT(
fbbb0 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55  ) ){.    res = U
fbbc0 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d  nlockFile(pFile-
fbbd0 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  >h, SHARED_FIRST
fbbe0 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45  , 0, SHARED_SIZE
fbbf0 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20  , 0);./* isNT() 
fbc00 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
fbc10 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
fbc20 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
fbc30 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a  r executed. .*/.
fbc40 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
fbc50 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
fbc60 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b      res = Unlock
fbc70 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
fbc80 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 70 46  HARED_FIRST + pF
fbc90 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
fbca0 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23  yte, 0, 1, 0);.#
fbcb0 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20  endif.  }.  if( 
fbcc0 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  res == 0 ){.    
fbcd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
fbce0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fbcf0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
fbd00 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  res;.}../*.** Lo
fbd10 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
fbd20 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
fbd30 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
fbd40 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
fbd50 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
fbd60 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
fbd70 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
fbd80 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
fbd90 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
fbda0 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
fbdb0 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
fbdc0 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
fbdd0 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
fbde0 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
fbdf0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
fbe00 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
fbe10 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
fbe20 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
fbe30 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
fbe40 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
fbe50 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
fbe60 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
fbe70 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
fbe80 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
fbe90 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
fbea0 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
fbeb0 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
fbec0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
fbed0 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
fbee0 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
fbef0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
fbf00 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
fbf10 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
fbf20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
fbf30 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
fbf40 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
fbf50 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
fbf60 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
fbf70 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
fbf80 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
fbf90 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
fbfa0 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
fbfb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
fbfc0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
fbfd0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
fbfe0 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20 77 69 6e  a lock.  The win
fbff0 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65  Unlock() routine
fc000 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c  .** erases all l
fc010 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64  ocks at once and
fc020 20 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65   returns us imme
fc030 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69  diately to locki
fc040 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49  ng level 0..** I
fc050 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
fc060 65 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c  e to lower the l
fc070 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65  ocking level one
fc080 20 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e   step at a time.
fc090 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f    You.** must go
fc0a0 20 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63   straight to loc
fc0b0 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f  king level 0..*/
fc0c0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c  .static int winL
fc0d0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
fc0e0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
fc0f0 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  pe){.  int rc = 
fc100 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a  SQLITE_OK;    /*
fc110 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
fc120 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f  m subroutines */
fc130 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20  .  int res = 1; 
fc140 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
fc150 75 6c 74 20 6f 66 20 61 20 77 69 6e 64 6f 77 73  ult of a windows
fc160 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20   lock call */.  
fc170 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b  int newLocktype;
fc180 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46         /* Set pF
fc190 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f  ile->locktype to
fc1a0 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f   this value befo
fc1b0 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20  re exiting */.  
fc1c0 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f  int gotPendingLo
fc1d0 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69  ck = 0;/* True i
fc1e0 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20  f we acquired a 
fc1f0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69  PENDING lock thi
fc200 73 20 74 69 6d 65 20 2a 2f 0a 20 20 77 69 6e 46  s time */.  winF
fc210 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fc220 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f  nFile*)id;.  DWO
fc230 52 44 20 65 72 72 6f 72 20 3d 20 4e 4f 5f 45 52  RD error = NO_ER
fc240 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ROR;..  assert( 
fc250 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41  id!=0 );.  OSTRA
fc260 43 45 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20  CE5("LOCK %d %d 
fc270 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20  was %d(%d)\n",. 
fc280 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
fc290 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  h, locktype, pFi
fc2a0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46  le->locktype, pF
fc2b0 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
fc2c0 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  yte);..  /* If t
fc2d0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
fc2e0 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
fc2f0 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
fc300 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
fc310 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e   ** OsFile, do n
fc320 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
fc330 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20  e the end_lock: 
fc340 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20  exit path, as.  
fc350 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65  ** sqlite3OsEnte
fc360 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20  rMutex() hasn't 
fc370 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
fc380 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
fc390 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63  e->locktype>=loc
fc3a0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74  ktype ){.    ret
fc3b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
fc3c0 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
fc3d0 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73  re the locking s
fc3e0 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65  equence is corre
fc3f0 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ct.  */.  assert
fc400 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
fc410 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f  e!=NO_LOCK || lo
fc420 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc430 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
fc440 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49   locktype!=PENDI
fc450 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  NG_LOCK );.  ass
fc460 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52  ert( locktype!=R
fc470 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20  ESERVED_LOCK || 
fc480 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
fc490 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
fc4a0 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50  .  /* Lock the P
fc4b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65  ENDING_LOCK byte
fc4c0 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61   if we need to a
fc4d0 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
fc4e0 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20   lock or.  ** a 
fc4f0 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66  SHARED lock.  If
fc500 20 77 65 20 61 72 65 20 61 63 71 75 69 72 69 6e   we are acquirin
fc510 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c  g a SHARED lock,
fc520 20 74 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e   the acquisition
fc530 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e   of.  ** the PEN
fc540 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69  DING_LOCK byte i
fc550 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a  s temporary..  *
fc560 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20  /.  newLocktype 
fc570 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
fc580 65 3b 0a 20 20 69 66 28 20 20 20 28 70 46 69 6c  e;.  if(   (pFil
fc590 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
fc5a0 4c 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20 28 20  LOCK).     || ( 
fc5b0 20 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43    (locktype==EXC
fc5c0 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 20 20  LUSIVE_LOCK).   
fc5d0 20 20 20 20 20 20 26 26 20 28 70 46 69 6c 65 2d        && (pFile-
fc5e0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52  >locktype==RESER
fc5f0 56 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a  VED_LOCK)).  ){.
fc600 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b      int cnt = 3;
fc610 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d  .    while( cnt-
fc620 2d 3e 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f  ->0 && (res = Lo
fc630 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fc640 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30   PENDING_BYTE, 0
fc650 2c 20 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20  , 1, 0))==0 ){. 
fc660 20 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69       /* Try 3 ti
fc670 6d 65 73 20 74 6f 20 67 65 74 20 74 68 65 20 70  mes to get the p
fc680 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68  ending lock.  Th
fc690 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d  e pending lock m
fc6a0 69 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a  ight be.      **
fc6b0 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72   held by another
fc6c0 20 72 65 61 64 65 72 20 70 72 6f 63 65 73 73 20   reader process 
fc6d0 77 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  who will release
fc6e0 20 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e   it momentarily.
fc6f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
fc700 4f 53 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20  OSTRACE2("could 
fc710 6e 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e  not get a PENDIN
fc720 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e  G lock. cnt=%d\n
fc730 22 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53  ", cnt);.      S
fc740 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20  leep(1);.    }. 
fc750 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63     gotPendingLoc
fc760 6b 20 3d 20 72 65 73 3b 0a 20 20 20 20 69 66 28  k = res;.    if(
fc770 20 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20 65   !res ){.      e
fc780 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
fc790 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ror();.    }.  }
fc7a0 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
fc7b0 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a   shared lock.  *
fc7c0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
fc7d0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
fc7e0 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65   res ){.    asse
fc7f0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
fc800 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a  ype==NO_LOCK );.
fc810 20 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61      res = getRea
fc820 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
fc830 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
fc840 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
fc850 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
fc860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65    }else{.      e
fc870 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
fc880 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ror();.    }.  }
fc890 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
fc8a0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20   RESERVED lock. 
fc8b0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
fc8c0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
fc8d0 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
fc8e0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
fc8f0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
fc900 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
fc910 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fc920 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ->h, RESERVED_BY
fc930 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20  TE, 0, 1, 0);.  
fc940 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
fc950 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
fc960 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a   RESERVED_LOCK;.
fc970 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
fc980 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74   error = GetLast
fc990 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20  Error();.    }. 
fc9a0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65   }..  /* Acquire
fc9b0 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a   a PENDING lock.
fc9c0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
fc9d0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
fc9e0 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
fc9f0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
fca00 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
fca10 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b    gotPendingLock
fca20 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
fca30 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55  Acquire an EXCLU
fca40 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  SIVE lock.  */. 
fca50 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
fca60 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
fca70 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65   res ){.    asse
fca80 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
fca90 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype>=SHARED_LOCK
fcaa0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e   );.    res = un
fcab0 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69  lockReadLock(pFi
fcac0 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  le);.    OSTRACE
fcad0 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20  2("unreadlock = 
fcae0 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 20 20  %d\n", res);.   
fcaf0 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28   res = LockFile(
fcb00 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fcb10 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
fcb20 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20  D_SIZE, 0);.    
fcb30 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20  if( res ){.     
fcb40 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45   newLocktype = E
fcb50 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20  XCLUSIVE_LOCK;. 
fcb60 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
fcb70 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
fcb80 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 4f 53  rror();.      OS
fcb90 54 52 41 43 45 32 28 22 65 72 72 6f 72 2d 63 6f  TRACE2("error-co
fcba0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 65 72 72 6f  de = %d\n", erro
fcbb0 72 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61  r);.      getRea
fcbc0 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
fcbd0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
fcbe0 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20   we are holding 
fcbf0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74  a PENDING lock t
fcc00 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20  hat ought to be 
fcc10 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20  released, then. 
fcc20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e   ** release it n
fcc30 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67  ow..  */.  if( g
fcc40 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26  otPendingLock &&
fcc50 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
fcc60 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e  D_LOCK ){.    Un
fcc70 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
fcc80 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  h, PENDING_BYTE,
fcc90 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a   0, 1, 0);.  }..
fcca0 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
fccb0 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63  state of the loc
fccc0 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68  k has held in th
fccd0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
fcce0 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75  r then.  ** retu
fccf0 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
fcd00 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a  te result code..
fcd10 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 29    */.  if( res )
fcd20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
fcd30 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
fcd40 20 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43     OSTRACE4("LOC
fcd50 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69  K FAILED %d tryi
fcd60 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f  ng for %d but go
fcd70 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  t %d\n", pFile->
fcd80 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
fcd90 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74  cktype, newLockt
fcda0 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  ype);.    pFile-
fcdb0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
fcdc0 6f 72 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  or;.    rc = SQL
fcdd0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
fcde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
fcdf0 3d 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70  = (u8)newLocktyp
fce00 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
fce10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
fce20 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
fce30 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
fce40 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
fce50 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
fce60 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
fce70 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
fce80 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
fce90 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74  ock is held, ret
fcea0 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c  urn.** non-zero,
fceb0 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e   otherwise zero.
fcec0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fced0 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  inCheckReservedL
fcee0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
fcef0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
fcf00 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ut){.  int rc;. 
fcf10 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fcf20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fcf30 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
fcf40 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
fcf50 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52  >locktype>=RESER
fcf60 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  VED_LOCK ){.    
fcf70 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52  rc = 1;.    OSTR
fcf80 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f  ACE3("TEST WR-LO
fcf90 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29  CK %d %d (local)
fcfa0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
fcfb0 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  c);.  }else{.   
fcfc0 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70   rc = LockFile(p
fcfd0 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
fcfe0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
fcff0 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
fd000 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65        UnlockFile
fd010 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52  (pFile->h, RESER
fd020 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  VED_BYTE, 0, 1, 
fd030 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  0);.    }.    rc
fd040 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52   = !rc;.    OSTR
fd050 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f  ACE3("TEST WR-LO
fd060 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65  CK %d %d (remote
fd070 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
fd080 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73  rc);.  }.  *pRes
fd090 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75  Out = rc;.  retu
fd0a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
fd0b0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
fd0c0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
fd0d0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
fd0e0 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65  r id to locktype
fd0f0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
fd100 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
fd110 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
fd120 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
fd130 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
fd140 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
fd150 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
fd160 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
fd170 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
fd180 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
fd190 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
fd1a0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74   no-op..**.** It
fd1b0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
fd1c0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e   for this routin
fd1d0 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65  e to fail if the
fd1e0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
fd1f0 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20  .** is NO_LOCK. 
fd200 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
fd210 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45  rgument is SHARE
fd220 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73  D_LOCK then this
fd230 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68   routine.** migh
fd240 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
fd250 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63  IOERR;.*/.static
fd260 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73   int winUnlock(s
fd270 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fd280 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
fd290 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69    int type;.  wi
fd2a0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
fd2b0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69  winFile*)id;.  i
fd2c0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
fd2d0 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  K;.  assert( pFi
fd2e0 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le!=0 );.  asser
fd2f0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
fd300 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53  RED_LOCK );.  OS
fd310 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25  TRACE5("UNLOCK %
fd320 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25  d to %d was %d(%
fd330 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  d)\n", pFile->h,
fd340 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20   locktype,.     
fd350 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
fd360 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61  type, pFile->sha
fd370 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20  redLockByte);.  
fd380 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
fd390 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
fd3a0 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
fd3b0 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b  CK ){.    Unlock
fd3c0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
fd3d0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20  HARED_FIRST, 0, 
fd3e0 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
fd3f0 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
fd400 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
fd410 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70  & !getReadLock(p
fd420 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f  File) ){.      /
fd430 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65  * This should ne
fd440 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20  ver happen.  We 
fd450 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
fd460 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a   able to.      *
fd470 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20  * reacquire the 
fd480 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read lock */.   
fd490 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
fd4a0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20  OERR_UNLOCK;.   
fd4b0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70   }.  }.  if( typ
fd4c0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
fd4d0 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69   ){.    UnlockFi
fd4e0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
fd4f0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
fd500 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
fd510 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
fd520 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45  K && type>=SHARE
fd530 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e  D_LOCK ){.    un
fd540 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69  lockReadLock(pFi
fd550 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74  le);.  }.  if( t
fd560 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
fd570 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
fd580 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
fd590 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
fd5a0 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  , 0);.  }.  pFil
fd5b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75  e->locktype = (u
fd5c0 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65  8)locktype;.  re
fd5d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
fd5e0 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75  * Control and qu
fd5f0 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20  ery of the open 
fd600 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  file handle..*/.
fd610 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69  static int winFi
fd620 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
fd630 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fd640 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
fd650 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
fd660 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
fd670 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
fd680 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a  : {.      *(int*
fd690 29 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c  )pArg = ((winFil
fd6a0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
fd6b0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
fd6c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
fd6d0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
fd6e0 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20  LAST_ERRNO: {.  
fd6f0 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
fd700 3d 20 28 69 6e 74 29 28 28 77 69 6e 46 69 6c 65  = (int)((winFile
fd710 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
fd720 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
fd730 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
fd740 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
fd750 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
fd760 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
fd770 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79  ector size in by
fd780 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72  tes of the under
fd790 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69  lying block devi
fd7a0 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  ce for.** the sp
fd7b0 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68  ecified file. Th
fd7c0 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77  is is almost alw
fd7d0 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62  ays 512 bytes, b
fd7e0 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72  ut may be.** lar
fd7f0 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76  ger for some dev
fd800 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ices..**.** SQLi
fd810 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20  te code assumes 
fd820 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61  this function ca
fd830 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c  nnot fail. It al
fd840 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a  so assumes that.
fd850 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20  ** if two files 
fd860 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74  are created in t
fd870 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73  he same file-sys
fd880 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69  tem directory (i
fd890 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73  .e..** a databas
fd8a0 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61  e and its journa
fd8b0 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65  l file) that the
fd8c0 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c   sector size wil
fd8d0 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65  l be the.** same
fd8e0 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74   for both..*/.st
fd8f0 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74  atic int winSect
fd900 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  orSize(sqlite3_f
fd910 69 6c 65 20 2a 69 64 29 7b 0a 20 20 61 73 73 65  ile *id){.  asse
fd920 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 72  rt( id!=0 );.  r
fd930 65 74 75 72 6e 20 28 69 6e 74 29 28 28 28 77 69  eturn (int)(((wi
fd940 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 73 65 63 74  nFile*)id)->sect
fd950 6f 72 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  orSize);.}../*.*
fd960 2a 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f  * Return a vecto
fd970 72 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72  r of device char
fd980 61 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a  acteristics..*/.
fd990 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65  static int winDe
fd9a0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
fd9b0 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
fd9c0 20 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f   *id){.  UNUSED_
fd9d0 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20  PARAMETER(id);. 
fd9e0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
fd9f0 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20  .** This vector 
fda00 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20  defines all the 
fda10 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e  methods that can
fda20 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a   operate on an.*
fda30 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66  * sqlite3_file f
fda40 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61  or win32..*/.sta
fda50 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
fda60 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e  3_io_methods win
fda70 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31  IoMethod = {.  1
fda80 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
fda90 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
fdaa0 73 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f  sion */.  winClo
fdab0 73 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20  se,.  winRead,. 
fdac0 20 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e   winWrite,.  win
fdad0 54 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53  Truncate,.  winS
fdae0 79 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69  ync,.  winFileSi
fdaf0 7a 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20  ze,.  winLock,. 
fdb00 20 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69   winUnlock,.  wi
fdb10 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  nCheckReservedLo
fdb20 63 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e  ck,.  winFileCon
fdb30 74 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f  trol,.  winSecto
fdb40 72 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69  rSize,.  winDevi
fdb50 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
fdb60 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.};../*********
fdb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdbb0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20  **.** Here ends 
fdbc0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20  the I/O methods 
fdbd0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71  that form the sq
fdbe0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
fdbf0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54   object..**.** T
fdc00 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66  he next block of
fdc10 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73   code implements
fdc20 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73   the VFS methods
fdc30 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
fdc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
fdc80 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
fdc90 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20   UTF-8 filename 
fdca0 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f  into whatever fo
fdcb0 72 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  rm the underlyin
fdcc0 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73  g.** operating s
fdcd0 79 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65  ystem wants file
fdce0 6e 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65  names in.  Space
fdcf0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73   to hold the res
fdd00 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e  ult.** is obtain
fdd10 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61  ed from malloc a
fdd20 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64  nd must be freed
fdd30 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a   by the calling.
fdd40 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  ** function..*/.
fdd50 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e  static void *con
fdd60 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
fdd70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
fdd80 6c 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20  lename){.  void 
fdd90 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b  *zConverted = 0;
fdda0 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
fddb0 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  .    zConverted 
fddc0 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28  = utf8ToUnicode(
fddd0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69  zFilename);./* i
fdde0 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
fddf0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
fde00 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
fde10 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
fde20 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  . .*/.#if SQLITE
fde30 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
fde40 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65  else{.    zConve
fde50 72 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63  rted = utf8ToMbc
fde60 73 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65  s(zFilename);.#e
fde70 6e 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61  ndif.  }.  /* ca
fde80 6c 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65  ller will handle
fde90 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a   out of memory *
fdea0 2f 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76  /.  return zConv
fdeb0 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  erted;.}../*.** 
fdec0 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61  Create a tempora
fded0 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20  ry file name in 
fdee0 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74  zBuf.  zBuf must
fdef0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74   be big enough t
fdf00 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66  o.** hold at pVf
fdf10 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68  s->mxPathname ch
fdf20 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61  aracters..*/.sta
fdf30 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e  tic int getTempn
fdf40 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68  ame(int nBuf, ch
fdf50 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61  ar *zBuf){.  sta
fdf60 74 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b  tic char zChars[
fdf70 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67  ] =.    "abcdefg
fdf80 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77  hijklmnopqrstuvw
fdf90 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46  xyz".    "ABCDEF
fdfa0 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56  GHIJKLMNOPQRSTUV
fdfb0 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34  WXYZ".    "01234
fdfc0 35 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74  56789";.  size_t
fdfd0 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54   i, j;.  char zT
fdfe0 65 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  empPath[MAX_PATH
fdff0 2b 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  +1];.  if( sqlit
fe000 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
fe010 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
fe020 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41  _snprintf(MAX_PA
fe030 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68  TH-30, zTempPath
fe040 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f  , "%s", sqlite3_
fe050 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b  temp_directory);
fe060 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e  .  }else if( isN
fe070 54 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20  T() ){.    char 
fe080 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48  *zMulti;.    WCH
fe090 41 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58  AR zWidePath[MAX
fe0a0 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54  _PATH];.    GetT
fe0b0 65 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54  empPathW(MAX_PAT
fe0c0 48 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29  H-30, zWidePath)
fe0d0 3b 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75  ;.    zMulti = u
fe0e0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69  nicodeToUtf8(zWi
fe0f0 64 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28  dePath);.    if(
fe100 20 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20   zMulti ){.     
fe110 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
fe120 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a  f(MAX_PATH-30, z
fe130 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20  TempPath, "%s", 
fe140 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66  zMulti);.      f
fe150 72 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20  ree(zMulti);.   
fe160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
fe170 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
fe180 4d 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54  M;.    }./* isNT
fe190 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54  () is 1 if SQLIT
fe1a0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73  E_OS_WINCE==1, s
fe1b0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e  o this else is n
fe1c0 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a  ever executed. .
fe1d0 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43  ** Since the ASC
fe1e0 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  II version of th
fe1f0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20  ese Windows API 
fe200 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  do not exist for
fe210 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20   WINCE,.** it's 
fe220 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74  important to not
fe230 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20   reference them 
fe240 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73  for WINCE builds
fe250 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
fe260 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
fe270 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  lse{.    char *z
fe280 55 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a  Utf8;.    char z
fe290 4d 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54  MbcsPath[MAX_PAT
fe2a0 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50  H];.    GetTempP
fe2b0 61 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30  athA(MAX_PATH-30
fe2c0 2c 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20  , zMbcsPath);.  
fe2d0 20 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65    zUtf8 = sqlite
fe2e0 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f  3_win32_mbcs_to_
fe2f0 75 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b  utf8(zMbcsPath);
fe300 0a 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29  .    if( zUtf8 )
fe310 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
fe320 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
fe330 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c  H-30, zTempPath,
fe340 20 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20   "%s", zUtf8);. 
fe350 20 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29       free(zUtf8)
fe360 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
fe370 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fe380 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65  _NOMEM;.    }.#e
fe390 6e 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ndif.  }.  for(i
fe3a0 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  =sqlite3Strlen30
fe3b0 28 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30  (zTempPath); i>0
fe3c0 20 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d   && zTempPath[i-
fe3d0 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d  1]=='\\'; i--){}
fe3e0 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20  .  zTempPath[i] 
fe3f0 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  = 0;.  sqlite3_s
fe400 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c  nprintf(nBuf-30,
fe410 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20   zBuf,.         
fe420 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22            "%s\\"
fe430 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
fe440 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61  _PREFIX, zTempPa
fe450 74 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74  th);.  j = sqlit
fe460 65 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29  e3Strlen30(zBuf)
fe470 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64  ;.  sqlite3_rand
fe480 6f 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66  omness(20, &zBuf
fe490 5b 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  [j]);.  for(i=0;
fe4a0 20 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29   i<20; i++, j++)
fe4b0 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20  {.    zBuf[j] = 
fe4c0 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28  (char)zChars[ ((
fe4d0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42  unsigned char)zB
fe4e0 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a  uf[j])%(sizeof(z
fe4f0 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d  Chars)-1) ];.  }
fe500 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a  .  zBuf[j] = 0;.
fe510 20 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50    OSTRACE2("TEMP
fe520 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22   FILENAME: %s\n"
fe530 2c 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72  , zBuf);.  retur
fe540 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
fe550 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ./*.** The retur
fe560 6e 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61  n value of getLa
fe570 73 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73  stErrorMsg.** is
fe580 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72   zero if the err
fe590 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20  or message fits 
fe5a0 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f  in the buffer, o
fe5b0 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74  r non-zero.** ot
fe5c0 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20  herwise (if the 
fe5d0 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e  message was trun
fe5e0 63 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69  cated)..*/.stati
fe5f0 63 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72  c int getLastErr
fe600 6f 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20  orMsg(int nBuf, 
fe610 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 2f  char *zBuf){.  /
fe620 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20  * FormatMessage 
fe630 72 65 74 75 72 6e 73 20 30 20 6f 6e 20 66 61 69  returns 0 on fai
fe640 6c 75 72 65 2e 20 20 4f 74 68 65 72 77 69 73 65  lure.  Otherwise
fe650 20 69 74 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73   it.  ** returns
fe660 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 54   the number of T
fe670 43 48 41 52 73 20 77 72 69 74 74 65 6e 20 74 6f  CHARs written to
fe680 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a   the output.  **
fe690 20 62 75 66 66 65 72 2c 20 65 78 63 6c 75 64 69   buffer, excludi
fe6a0 6e 67 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69  ng the terminati
fe6b0 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20  ng null char..  
fe6c0 2a 2f 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72  */.  DWORD error
fe6d0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fe6e0 29 3b 0a 20 20 44 57 4f 52 44 20 64 77 4c 65 6e  );.  DWORD dwLen
fe6f0 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4f   = 0;.  char *zO
fe700 75 74 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69  ut = 0;..  if( i
fe710 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48  sNT() ){.    WCH
fe720 41 52 20 2a 7a 54 65 6d 70 57 69 64 65 20 3d 20  AR *zTempWide = 
fe730 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20  NULL;.    dwLen 
fe740 3d 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57  = FormatMessageW
fe750 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f  (FORMAT_MESSAGE_
fe760 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20  ALLOCATE_BUFFER 
fe770 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45  | FORMAT_MESSAGE
fe780 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46  _FROM_SYSTEM | F
fe790 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47  ORMAT_MESSAGE_IG
fe7a0 4e 4f 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20  NORE_INSERTS,.  
fe7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe7c0 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20           NULL,. 
fe7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe7e0 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c            error,
fe7f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
fe800 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20              0,. 
fe810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe820 20 20 20 20 20 20 20 20 20 20 28 4c 50 57 53 54            (LPWST
fe830 52 29 20 26 7a 54 65 6d 70 57 69 64 65 2c 0a 20  R) &zTempWide,. 
fe840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe850 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
fe860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe870 20 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20          0);.    
fe880 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b  if( dwLen > 0 ){
fe890 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61  .      /* alloca
fe8a0 74 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20  te a buffer and 
fe8b0 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20  convert to UTF8 
fe8c0 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20  */.      zOut = 
fe8d0 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54  unicodeToUtf8(zT
fe8e0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 20 20  empWide);.      
fe8f0 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79 73 74  /* free the syst
fe900 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61  em buffer alloca
fe910 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d 65 73  ted by FormatMes
fe920 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 4c 6f  sage */.      Lo
fe930 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 57 69 64  calFree(zTempWid
fe940 65 29 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e  e);.    }./* isN
fe950 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
fe960 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
fe970 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
fe980 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
fe990 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53  .** Since the AS
fe9a0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  CII version of t
fe9b0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
fe9c0 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
fe9d0 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73  r WINCE,.** it's
fe9e0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f   important to no
fe9f0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d  t reference them
fea00 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64   for WINCE build
fea10 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
fea20 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
fea30 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
fea40 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20  zTemp = NULL;.  
fea50 20 20 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74    dwLen = Format
fea60 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41 54 5f  MessageA(FORMAT_
fea70 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45  MESSAGE_ALLOCATE
fea80 5f 42 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54  _BUFFER | FORMAT
fea90 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59  _MESSAGE_FROM_SY
feaa0 53 54 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45  STEM | FORMAT_ME
feab0 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53  SSAGE_IGNORE_INS
feac0 45 52 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20  ERTS,.          
fead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feae0 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20   NULL,.         
feaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feb00 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20    error,.       
feb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feb20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20      0,.         
feb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feb40 20 20 28 4c 50 53 54 52 29 20 26 7a 54 65 6d 70    (LPSTR) &zTemp
feb50 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
feb60 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a               0,.
feb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feb80 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 20             0);. 
feb90 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30     if( dwLen > 0
feba0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c   ){.      /* all
febb0 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61  ocate a buffer a
febc0 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54  nd convert to UT
febd0 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74  F8 */.      zOut
febe0 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32   = sqlite3_win32
febf0 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54  _mbcs_to_utf8(zT
fec00 65 6d 70 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66  emp);.      /* f
fec10 72 65 65 20 74 68 65 20 73 79 73 74 65 6d 20 62  ree the system b
fec20 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
fec30 62 79 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65  by FormatMessage
fec40 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 61 6c 46   */.      LocalF
fec50 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  ree(zTemp);.    
fec60 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69  }.#endif.  }.  i
fec70 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e 20 29 7b  f( 0 == dwLen ){
fec80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
fec90 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66  rintf(nBuf, zBuf
feca0 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20  , "OsError 0x%x 
fecb0 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72  (%u)", error, er
fecc0 72 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ror);.  }else{. 
fecd0 20 20 20 2f 2a 20 63 6f 70 79 20 61 20 6d 61 78     /* copy a max
fece0 69 6d 75 6d 20 6f 66 20 6e 42 75 66 20 63 68 61  imum of nBuf cha
fecf0 72 73 20 74 6f 20 6f 75 74 70 75 74 20 62 75 66  rs to output buf
fed00 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fer */.    sqlit
fed10 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
fed20 2c 20 7a 42 75 66 2c 20 22 25 73 22 2c 20 7a 4f  , zBuf, "%s", zO
fed30 75 74 29 3b 0a 20 20 20 20 2f 2a 20 66 72 65 65  ut);.    /* free
fed40 20 74 68 65 20 55 54 46 38 20 62 75 66 66 65 72   the UTF8 buffer
fed50 20 2a 2f 0a 20 20 20 20 66 72 65 65 28 7a 4f 75   */.    free(zOu
fed60 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
fed70 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65   0;.}../*.** Ope
fed80 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  n a file..*/.sta
fed90 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28  tic int winOpen(
feda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
fedb0 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
fedc0 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f  Not used */.  co
fedd0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
fede0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
fedf0 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54 46  of the file (UTF
fee00 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
fee10 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
fee20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
fee30 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64  SQLite file hand
fee40 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  le here */.  int
fee50 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
fee60 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d         /* Open m
fee70 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ode flags */.  i
fee80 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20  nt *pOutFlags   
fee90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
feea0 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20  us return flags 
feeb0 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68  */.){.  HANDLE h
feec0 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 69  ;.  DWORD dwDesi
feed0 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f  redAccess;.  DWO
feee0 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a  RD dwShareMode;.
feef0 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74 69    DWORD dwCreati
fef00 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20  onDisposition;. 
fef10 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e   DWORD dwFlagsAn
fef20 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b  dAttributes = 0;
fef30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fef40 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d  INCE.  int isTem
fef50 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  p = 0;.#endif.  
fef60 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fef70 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
fef80 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
fef90 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
fefa0 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69     /* Filename i
fefb0 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  n OS encoding */
fefc0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
fefd0 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65  Utf8Name = zName
fefe0 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65  ;    /* Filename
feff0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69   in UTF-8 encodi
ff000 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d  ng */.  char zTm
ff010 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31  pname[MAX_PATH+1
ff020 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66  ];        /* Buf
ff030 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 61  fer used to crea
ff040 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65  te temp filename
ff050 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
ff060 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44  d!=0 );.  UNUSED
ff070 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
ff080 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ;..  /* If the s
ff090 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
ff0a0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
ff0b0 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74  is NULL, generat
ff0c0 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72  e a .  ** tempor
ff0d0 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f  ary file name to
ff0e0 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28   use .  */.  if(
ff0f0 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20   !zUtf8Name ){. 
ff100 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54     int rc = getT
ff110 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48  empname(MAX_PATH
ff120 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20  +1, zTmpname);. 
ff130 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
ff140 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
ff150 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
ff160 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a     zUtf8Name = z
ff170 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20  Tmpname;.  }..  
ff180 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66  /* Convert the f
ff190 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73  ilename to the s
ff1a0 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20  ystem encoding. 
ff1b0 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  */.  zConverted 
ff1c0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
ff1d0 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29  ename(zUtf8Name)
ff1e0 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ;.  if( zConvert
ff1f0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ed==0 ){.    ret
ff200 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ff210 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61  ;.  }..  if( fla
ff220 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ff230 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
ff240 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73    dwDesiredAcces
ff250 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44  s = GENERIC_READ
ff260 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45   | GENERIC_WRITE
ff270 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
ff280 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 3d  wDesiredAccess =
ff290 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20   GENERIC_READ;. 
ff2a0 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f   }.  /* SQLITE_O
ff2b0 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 73  PEN_EXCLUSIVE is
ff2c0 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
ff2d0 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66 69  re that a new fi
ff2e0 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 61  le is .  ** crea
ff2f0 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 73  ted. SQLite does
ff300 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69 6e  n't use it to in
ff310 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69 76  dicate "exclusiv
ff320 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a 20  e access" .  ** 
ff330 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c 79  as it is usually
ff340 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 2a   understood..  *
ff350 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c 61  /.  assert(!(fla
ff360 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ff370 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 28  _EXCLUSIVE) || (
ff380 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
ff390 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 20  PEN_CREATE));.  
ff3a0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
ff3b0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
ff3c0 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61  E ){.    /* Crea
ff3d0 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c 20  tes a new file, 
ff3e0 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73 20  only if it does 
ff3f0 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
ff400 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20  t. */.    /* If 
ff410 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2c  the file exists,
ff420 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 20   it fails. */.  
ff430 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
ff440 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 45  osition = CREATE
ff450 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 66  _NEW;.  }else if
ff460 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
ff470 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a  _OPEN_CREATE ){.
ff480 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 73      /* Open exis
ff490 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 72  ting file, or cr
ff4a0 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73 6e  eate if it doesn
ff4b0 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 20  't exist */.    
ff4c0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
ff4d0 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57  ition = OPEN_ALW
ff4e0 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  AYS;.  }else{.  
ff4f0 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 6c    /* Opens a fil
ff500 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 78  e, only if it ex
ff510 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43  ists. */.    dwC
ff520 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69  reationDispositi
ff530 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49  on = OPEN_EXISTI
ff540 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 72  NG;.  }.  dwShar
ff550 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 41  eMode = FILE_SHA
ff560 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53  RE_READ | FILE_S
ff570 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 66  HARE_WRITE;.  if
ff580 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
ff590 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
ff5a0 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 54  OSE ){.#if SQLIT
ff5b0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 64  E_OS_WINCE.    d
ff5c0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
ff5d0 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
ff5e0 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20  BUTE_HIDDEN;.   
ff5f0 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c   isTemp = 1;.#el
ff600 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e  se.    dwFlagsAn
ff610 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49  dAttributes = FI
ff620 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45 4d  LE_ATTRIBUTE_TEM
ff630 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20 20  PORARY.         
ff640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff650 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54        | FILE_ATT
ff660 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 20  RIBUTE_HIDDEN.  
ff670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff680 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46               | F
ff690 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f  ILE_FLAG_DELETE_
ff6a0 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66  ON_CLOSE;.#endif
ff6b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77  .  }else{.    dw
ff6c0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
ff6d0 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42  es = FILE_ATTRIB
ff6e0 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a  UTE_NORMAL;.  }.
ff6f0 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 6f    /* Reports fro
ff700 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20 61  m the internet a
ff710 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d 61  re that performa
ff720 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20 20  nce is always.  
ff730 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49 4c  ** better if FIL
ff740 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43  E_FLAG_RANDOM_AC
ff750 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20 54  CESS is used.  T
ff760 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a  icket #2699. */.
ff770 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
ff780 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e 64  NCE.  dwFlagsAnd
ff790 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46 49  Attributes |= FI
ff7a0 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41  LE_FLAG_RANDOM_A
ff7b0 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20  CCESS;.#endif.  
ff7c0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
ff7d0 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65    h = CreateFile
ff7e0 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
ff7f0 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44  rted,.       dwD
ff800 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20  esiredAccess,.  
ff810 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65       dwShareMode
ff820 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20  ,.       NULL,. 
ff830 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e        dwCreation
ff840 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20  Disposition,.   
ff850 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
ff860 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20  tributes,.      
ff870 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20   NULL.    );./* 
ff880 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
ff890 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
ff8a0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
ff8b0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
ff8c0 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
ff8d0 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
ff8e0 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
ff8f0 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
ff900 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
ff910 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
ff920 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
ff930 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
ff940 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
ff950 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
ff960 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d    }else{.    h =
ff970 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 68   CreateFileA((ch
ff980 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a  ar*)zConverted,.
ff990 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64         dwDesired
ff9a0 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64  Access,.       d
ff9b0 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20  wShareMode,.    
ff9c0 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20     NULL,.       
ff9d0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
ff9e0 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77  ition,.       dw
ff9f0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
ffa00 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a  es,.       NULL.
ffa10 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20      );.#endif.  
ffa20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c  }.  if( h==INVAL
ffa30 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20  ID_HANDLE_VALUE 
ffa40 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e  ){.    free(zCon
ffa50 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66 28  verted);.    if(
ffa60 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
ffa70 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
ffa80 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 77  {.      return w
ffa90 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61  inOpen(pVfs, zNa
ffaa0 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 20  me, id, .       
ffab0 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53 51        ((flags|SQ
ffac0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
ffad0 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 4e  LY)&~SQLITE_OPEN
ffae0 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f 75  _READWRITE), pOu
ffaf0 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 6c  tFlags);.    }el
ffb00 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
ffb10 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
ffb20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
ffb30 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20  ( pOutFlags ){. 
ffb40 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53     if( flags & S
ffb50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
ffb60 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a 70  RITE ){.      *p
ffb70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54  OutFlags = SQLIT
ffb80 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ffb90 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
ffba0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20     *pOutFlags = 
ffbb0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
ffbc0 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ONLY;.    }.  }.
ffbd0 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20    memset(pFile, 
ffbe0 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65  0, sizeof(*pFile
ffbf0 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65  ));.  pFile->pMe
ffc00 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74  thod = &winIoMet
ffc10 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20  hod;.  pFile->h 
ffc20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61  = h;.  pFile->la
ffc30 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52  stErrno = NO_ERR
ffc40 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 63  OR;.  pFile->sec
ffc50 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65 63  torSize = getSec
ffc60 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a 55  torSize(pVfs, zU
ffc70 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 51  tf8Name);.#if SQ
ffc80 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
ffc90 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 51  if( (flags & (SQ
ffca0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
ffcb0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
ffcc0 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20  MAIN_DB)) ==.   
ffcd0 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
ffce0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
ffcf0 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  TE|SQLITE_OPEN_M
ffd00 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26  AIN_DB).       &
ffd10 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f  & !winceCreateLo
ffd20 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29  ck(zName, pFile)
ffd30 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48  .  ){.    CloseH
ffd40 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72  andle(h);.    fr
ffd50 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
ffd60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ffd70 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
ffd80 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a    if( isTemp ){.
ffd90 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65      pFile->zDele
ffda0 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e  teOnClose = zCon
ffdb0 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a  verted;.  }else.
ffdc0 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66  #endif.  {.    f
ffdd0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
ffde0 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74  .  }.  OpenCount
ffdf0 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e  er(+1);.  return
ffe00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
ffe10 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
ffe20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  named file..**.*
ffe30 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64  * Note that wind
ffe40 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  ows does not all
ffe50 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20  ow a file to be 
ffe60 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20  deleted if some 
ffe70 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73  other.** process
ffe80 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53   has it open.  S
ffe90 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 73  ometimes a virus
ffea0 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65   scanner or inde
ffeb0 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  xing program.** 
ffec0 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72  will open a jour
ffed0 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79  nal file shortly
ffee0 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72 65   after it is cre
ffef0 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ated in order to
fff00 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20   do.** whatever 
fff10 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20  it does.  While 
fff20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65  this other proce
fff30 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68  ss is holding th
fff40 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20  e.** file open, 
fff50 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c  we will be unabl
fff60 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20  e to delete it. 
fff70 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
fff80 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c  this.** problem,
fff90 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69   we delay 100 mi
fffa0 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74  lliseconds and t
fffb0 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61  ry to delete aga
fffc0 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58  in.  Up.** to MX
fffd0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
fffe0 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65  Ts deletion atte
ffff0 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66  mpts are run bef
10000 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70  ore giving.** up
10001 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61   and returning a
10002 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66  n error..*/.#def
10003 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f  ine MX_DELETION_
10004 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69  ATTEMPTS 5.stati
10005 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28  c int winDelete(
10006 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
10007 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f  pVfs,          /
10008 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69  * Not used on wi
10009 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  n32 */.  const c
1000a 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
1000b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1000c 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a  file to delete *
1000d 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20  /.  int syncDir 
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1000f 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77  /* Not used on w
10010 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  in32 */.){.  int
10011 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52   cnt = 0;.  DWOR
10012 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72  D rc;.  DWORD er
10013 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20  ror = 0;.  void 
10014 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f  *zConverted = co
10015 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d  nvertUtf8Filenam
10016 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
10017 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10018 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44  (pVfs);.  UNUSED
10019 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44  _PARAMETER(syncD
1001a 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  ir);.  if( zConv
1001b 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
1001c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1001d 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c  MEM;.  }.  Simul
1001e 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72  ateIOError(retur
1001f 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  n SQLITE_IOERR_D
10020 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69 73  ELETE);.  if( is
10021 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b 0a  NT() ){.    do{.
10022 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65        DeleteFile
10023 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  W(zConverted);. 
10024 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20     }while(   (  
10025 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41   ((rc = GetFileA
10026 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e 76  ttributesW(zConv
10027 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c  erted)) != INVAL
10028 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
10029 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ES).            
1002a 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20     || ((error = 
1002b 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20  GetLastError()) 
1002c 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f  == ERROR_ACCESS_
1002d 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20  DENIED)).       
1002e 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20      && (++cnt < 
1002f 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  MX_DELETION_ATTE
10030 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20  MPTS).          
10031 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c   && (Sleep(100),
10032 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29   1) );./* isNT()
10033 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
10034 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
10035 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
10036 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
10037 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
10038 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
10039 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
1003a 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
1003b 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
1003c 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
1003d 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
1003e 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
1003f 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
10040 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
10041 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20  e{.    do{.     
10042 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 6f   DeleteFileA(zCo
10043 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77  nverted);.    }w
10044 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63  hile(   (   ((rc
10045 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62   = GetFileAttrib
10046 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 64  utesA(zConverted
10047 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49  )) != INVALID_FI
10048 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20  LE_ATTRIBUTES). 
10049 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
1004a 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61   ((error = GetLa
1004b 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52  stError()) == ER
1004c 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45  ROR_ACCESS_DENIE
1004d 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26  D)).           &
1004e 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45  & (++cnt < MX_DE
1004f 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29  LETION_ATTEMPTS)
10050 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28  .           && (
10051 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29  Sleep(100), 1) )
10052 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66  ;.#endif.  }.  f
10053 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
10054 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c  .  OSTRACE2("DEL
10055 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a  ETE \"%s\"\n", z
10056 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74  Filename);.  ret
10057 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 49  urn (   (rc == I
10058 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
10059 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20 20  IBUTES) .       
1005a 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d 20     && (error == 
1005b 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46  ERROR_FILE_NOT_F
1005c 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 5f  OUND)) ? SQLITE_
1005d 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
1005e 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a  R_DELETE;.}../*.
1005f 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78 69  ** Check the exi
10060 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75  stance and statu
10061 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  s of a file..*/.
10062 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41 63  static int winAc
10063 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  cess(.  sqlite3_
10064 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
10065 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
10066 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e  n win32 */.  con
10067 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
10068 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me,     /* Name 
10069 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b  of file to check
1006a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
1006b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1006c 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74   /* Type of test
1006d 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73   to make on this
1006e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   file */.  int *
1006f 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20  pResOut         
10070 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
10071 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f  sult */.){.  DWO
10072 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 72  RD attr;.  int r
10073 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a  c = 0;.  void *z
10074 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
10075 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
10076 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e  zFilename);.  UN
10077 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
10078 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  Vfs);.  if( zCon
10079 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
1007a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1007b 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
1007c 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 74  isNT() ){.    at
1007d 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72  tr = GetFileAttr
1007e 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a 29  ibutesW((WCHAR*)
1007f 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20  zConverted);./* 
10080 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
10081 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
10082 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
10083 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
10084 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
10085 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
10086 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
10087 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
10088 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
10089 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
1008a 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
1008b 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
1008c 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
1008d 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
1008e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 74    }else{.    att
1008f 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69  r = GetFileAttri
10090 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a 43  butesA((char*)zC
10091 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69  onverted);.#endi
10092 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f  f.  }.  free(zCo
10093 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69 74  nverted);.  swit
10094 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20  ch( flags ){.   
10095 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43   case SQLITE_ACC
10096 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61  ESS_READ:.    ca
10097 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
10098 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72  _EXISTS:.      r
10099 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c 49  c = attr!=INVALI
1009a 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
1009b 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  S;.      break;.
1009c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1009d 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45  ACCESS_READWRITE
1009e 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61 74  :.      rc = (at
1009f 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49 42  tr & FILE_ATTRIB
100a0 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d 30  UTE_READONLY)==0
100a1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
100a2 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
100a3 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c    assert(!"Inval
100a4 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e  id flags argumen
100a5 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73  t");.  }.  *pRes
100a6 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75  Out = rc;.  retu
100a7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
100a8 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ../*.** Turn a r
100a9 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
100aa 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
100ab 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68  hname.  Write th
100ac 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61  e full.** pathna
100ad 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20  me into zOut[]. 
100ae 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20   zOut[] will be 
100af 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d  at least pVfs->m
100b0 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74  xPathname.** byt
100b1 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  es in size..*/.s
100b2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75 6c  tatic int winFul
100b3 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c  lPathname(.  sql
100b4 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
100b5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
100b6 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a  inter to vfs obj
100b7 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ect */.  const c
100b8 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20  har *zRelative, 
100b9 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62         /* Possib
100ba 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75  ly relative inpu
100bb 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20  t path */.  int 
100bc 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  nFull,          
100bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
100be 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66  e of output buff
100bf 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  er in bytes */. 
100c0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20   char *zFull    
100c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
100c2 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20  * Output buffer 
100c3 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 66  */.){.  .#if def
100c4 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29  ined(__CYGWIN__)
100c5 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
100c6 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 79  TER(nFull);.  cy
100c7 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c  gwin_conv_to_ful
100c8 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65  l_win32_path(zRe
100c9 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a  lative, zFull);.
100ca 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
100cb 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  OK;.#endif..#if 
100cc 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
100cd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
100ce 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20  ER(nFull);.  /* 
100cf 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e  WinCE has no con
100d0 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69  cept of a relati
100d1 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20  ve pathname, or 
100d2 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f  so I am told. */
100d3 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
100d4 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68  ntf(pVfs->mxPath
100d5 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73  name, zFull, "%s
100d6 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20  ", zRelative);. 
100d7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
100d8 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  K;.#endif..#if !
100d9 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
100da 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59  && !defined(__CY
100db 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42  GWIN__).  int nB
100dc 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  yte;.  void *zCo
100dd 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20  nverted;.  char 
100de 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 5f  *zOut;.  UNUSED_
100df 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29  PARAMETER(nFull)
100e0 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d  ;.  zConverted =
100e1 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
100e2 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 3b  name(zRelative);
100e3 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
100e4 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d  .    WCHAR *zTem
100e5 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47  p;.    nByte = G
100e6 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28  etFullPathNameW(
100e7 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
100e8 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33  ed, 0, 0, 0) + 3
100e9 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61  ;.    zTemp = ma
100ea 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65  lloc( nByte*size
100eb 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a  of(zTemp[0]) );.
100ec 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30      if( zTemp==0
100ed 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a   ){.      free(z
100ee 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
100ef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
100f0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
100f1 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65   GetFullPathName
100f2 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
100f3 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65  rted, nByte, zTe
100f4 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65  mp, 0);.    free
100f5 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
100f6 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65    zOut = unicode
100f7 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20  ToUtf8(zTemp);. 
100f8 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a     free(zTemp);.
100f9 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
100fa 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
100fb 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
100fc 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
100fd 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
100fe 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
100ff 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
10100 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
10101 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
10102 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
10103 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
10104 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
10105 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
10106 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
10107 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
10108 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 20  char *zTemp;.   
10109 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c   nByte = GetFull
1010a 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a  PathNameA((char*
1010b 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20  )zConverted, 0, 
1010c 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a  0, 0) + 3;.    z
1010d 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  Temp = malloc( n
1010e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d  Byte*sizeof(zTem
1010f 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  p[0]) );.    if(
10110 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20   zTemp==0 ){.   
10111 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
10112 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ed);.      retur
10113 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
10114 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c      }.    GetFul
10115 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72  lPathNameA((char
10116 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42  *)zConverted, nB
10117 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a  yte, zTemp, 0);.
10118 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
10119 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d  ted);.    zOut =
1011a 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d   sqlite3_win32_m
1011b 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d  bcs_to_utf8(zTem
1011c 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65  p);.    free(zTe
1011d 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  mp);.#endif.  }.
1011e 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 20    if( zOut ){.  
1011f 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
10120 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  tf(pVfs->mxPathn
10121 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22  ame, zFull, "%s"
10122 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 65  , zOut);.    fre
10123 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 74  e(zOut);.    ret
10124 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
10125 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
10126 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
10127 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
10128 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65 63  *.** Get the sec
10129 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20  tor size of the 
1012a 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20 73  device used to s
1012b 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f  tore.** file..*/
1012c 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 53  .static int getS
1012d 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 73  ectorSize(.    s
1012e 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1012f 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ,.    const char
10130 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 20   *zRelative     
10131 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e 61  /* UTF-8 file na
10132 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44  me */.){.  DWORD
10133 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 20   bytesPerSector 
10134 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  = SQLITE_DEFAULT
10135 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20  _SECTOR_SIZE;.  
10136 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53 70  /* GetDiskFreeSp
10137 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70 6f  ace is not suppo
10138 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43 45  rted under WINCE
10139 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   */.#if SQLITE_O
1013a 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44  S_WINCE.  UNUSED
1013b 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
1013c 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
1013d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29 3b  ETER(zRelative);
1013e 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a 46  .#else.  char zF
1013f 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 48  ullpath[MAX_PATH
10140 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  +1];.  int rc;. 
10141 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20 30   DWORD dwRet = 0
10142 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d 6d  ;.  DWORD dwDumm
10143 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65  y;..  /*.  ** We
10144 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68 65   need to get the
10145 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20   full path name 
10146 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a  of the file.  **
10147 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69 76   to get the driv
10148 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b  e letter to look
10149 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a 20   up the sector. 
1014a 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20   ** size..  */. 
1014b 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74   rc = winFullPat
1014c 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c  hname(pVfs, zRel
1014d 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c  ative, MAX_PATH,
1014e 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69   zFullpath);.  i
1014f 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f  f( rc == SQLITE_
10150 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69  OK ).  {.    voi
10151 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
10152 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
10153 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a  ame(zFullpath);.
10154 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74      if( zConvert
10155 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ed ){.      if( 
10156 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20 20  isNT() ){.      
10157 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74    /* trim path t
10158 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66  o just drive ref
10159 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20  erence */.      
1015a 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43 6f    WCHAR *p = zCo
1015b 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 20  nverted;.       
1015c 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20   for(;*p;p++){. 
1015d 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 20           if( *p 
1015e 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20  == '\\' ){.     
1015f 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 27         *p = '\0'
10160 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
10161 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
10162 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
10163 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 69     dwRet = GetDi
10164 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57 43  skFreeSpaceW((WC
10165 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  HAR*)zConverted,
10166 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10168 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20     &dwDummy,.   
10169 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1016a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
1016b 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c 0a  bytesPerSector,.
1016c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1016d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1016e 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20    &dwDummy,.    
1016f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
10171 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 7d  wDummy);.      }
10172 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
10173 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75   trim path to ju
10174 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e  st drive referen
10175 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68  ce */.        ch
10176 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29  ar *p = (char *)
10177 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20  zConverted;.    
10178 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29      for(;*p;p++)
10179 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1017a 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20  *p == '\\' ){.  
1017b 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27            *p = '
1017c 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20  \0';.           
1017d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1017e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1017f 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65        dwRet = Ge
10180 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 28  tDiskFreeSpaceA(
10181 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
10182 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
10183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10184 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20       &dwDummy,. 
10185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10187 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72   &bytesPerSector
10188 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
10189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1018a 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20      &dwDummy,.  
1018b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1018c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1018d 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20  &dwDummy);.     
1018e 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43   }.      free(zC
1018f 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d  onverted);.    }
10190 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74 20  .    if( !dwRet 
10191 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50 65  ){.      bytesPe
10192 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45  rSector = SQLITE
10193 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
10194 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  SIZE;.    }.  }.
10195 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
10196 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 65  (int) bytesPerSe
10197 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65  ctor; .}..#ifnde
10198 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
10199 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a  AD_EXTENSION./*.
1019a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f  ** Interfaces fo
1019b 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72  r opening a shar
1019c 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64  ed library, find
1019d 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ing entry points
1019e 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73  .** within the s
1019f 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61  hared library, a
101a0 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73  nd closing the s
101a1 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a  hared library..*
101a2 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  /./*.** Interfac
101a3 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  es for opening a
101a4 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
101a5 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70   finding entry p
101a6 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  oints.** within 
101a7 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
101a8 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ry, and closing 
101a9 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
101aa 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
101ab 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71  id *winDlOpen(sq
101ac 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
101ad 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
101ae 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c  lename){.  HANDL
101af 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  E h;.  void *zCo
101b0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
101b1 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46  tUtf8Filename(zF
101b2 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53  ilename);.  UNUS
101b3 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
101b4 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65  s);.  if( zConve
101b5 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rted==0 ){.    r
101b6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
101b7 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
101b8 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79   h = LoadLibrary
101b9 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
101ba 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  rted);./* isNT()
101bb 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
101bc 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
101bd 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
101be 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
101bf 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
101c0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
101c1 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
101c2 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
101c3 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
101c4 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
101c5 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
101c6 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
101c7 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
101c8 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
101c9 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c  e{.    h = LoadL
101ca 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a  ibraryA((char*)z
101cb 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64  Converted);.#end
101cc 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
101cd 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74  onverted);.  ret
101ce 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a  urn (void*)h;.}.
101cf 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44  static void winD
101d0 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
101d1 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
101d2 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  uf, char *zBufOu
101d3 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t){.  UNUSED_PAR
101d4 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
101d5 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28  getLastErrorMsg(
101d6 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b 0a  nBuf, zBufOut);.
101d7 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79  }.void (*winDlSy
101d8 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  m(sqlite3_vfs *p
101d9 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
101da 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  le, const char *
101db 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b  zSymbol))(void){
101dc 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
101dd 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 53  TER(pVfs);.#if S
101de 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
101df 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 41   /* The GetProcA
101e0 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 6e  ddressA() routin
101e1 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  e is only availa
101e2 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f  ble on wince. */
101e3 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28  .  return (void(
101e4 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63  *)(void))GetProc
101e5 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c 45  AddressA((HANDLE
101e6 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f  )pHandle, zSymbo
101e7 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41  l);.#else.  /* A
101e8 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73  ll other windows
101e9 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 63   platforms expec
101ea 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  t GetProcAddress
101eb 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20  () to take.  ** 
101ec 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 72  an Ansi string r
101ed 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
101ee 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 6e   _UNICODE settin
101ef 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76  g */.  return (v
101f0 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74  oid(*)(void))Get
101f1 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e  ProcAddress((HAN
101f2 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79  DLE)pHandle, zSy
101f3 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  mbol);.#endif.}.
101f4 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 28  void winDlClose(
101f5 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
101f6 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
101f7 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
101f8 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 46  METER(pVfs);.  F
101f9 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44  reeLibrary((HAND
101fa 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  LE)pHandle);.}.#
101fb 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54  else /* if SQLIT
101fc 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
101fd 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64  NSION is defined
101fe 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77  : */.  #define w
101ff 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64  inDlOpen  0.  #d
10200 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72  efine winDlError
10201 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e   0.  #define win
10202 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
10203 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30  ine winDlClose 0
10204 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
10205 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
10206 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
10207 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
10208 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
10209 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  nRandomness(sqli
1020a 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
1020b 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
1020c 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  Buf){.  int n = 
1020d 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  0;.  UNUSED_PARA
1020e 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66  METER(pVfs);.#if
1020f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
10210 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66  TEST).  n = nBuf
10211 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c  ;.  memset(zBuf,
10212 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65   0, nBuf);.#else
10213 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 59  .  if( sizeof(SY
10214 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 2d  STEMTIME)<=nBuf-
10215 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d 54  n ){.    SYSTEMT
10216 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 79  IME x;.    GetSy
10217 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 20  stemTime(&x);.  
10218 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
10219 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  ], &x, sizeof(x)
1021a 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
1021b 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28  of(x);.  }.  if(
1021c 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d   sizeof(DWORD)<=
1021d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57  nBuf-n ){.    DW
1021e 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 72  ORD pid = GetCur
1021f 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 3b  rentProcessId();
10220 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
10221 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  f[n], &pid, size
10222 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e 20  of(pid));.    n 
10223 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a  += sizeof(pid);.
10224 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66    }.  if( sizeof
10225 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20  (DWORD)<=nBuf-n 
10226 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e 74  ){.    DWORD cnt
10227 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 28   = GetTickCount(
10228 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
10229 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69  Buf[n], &cnt, si
1022a 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 20  zeof(cnt));.    
1022b 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29  n += sizeof(cnt)
1022c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  ;.  }.  if( size
1022d 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52  of(LARGE_INTEGER
1022e 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20  )<=nBuf-n ){.   
1022f 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69   LARGE_INTEGER i
10230 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 6f  ;.    QueryPerfo
10231 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69  rmanceCounter(&i
10232 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
10233 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65  Buf[n], &i, size
10234 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  of(i));.    n +=
10235 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a   sizeof(i);.  }.
10236 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
10237 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  n;.}.../*.** Sle
10238 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
10239 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
1023a 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
1023b 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74  e slept..*/.stat
1023c 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28  ic int winSleep(
1023d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1023e 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29  s, int microsec)
1023f 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 6f  {.  Sleep((micro
10240 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a  sec+999)/1000);.
10241 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
10242 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75  ER(pVfs);.  retu
10243 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39  rn ((microsec+99
10244 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d  9)/1000)*1000;.}
10245 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
10246 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
10247 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
10248 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
10249 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
1024a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
1024b 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
1024c 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
1024d 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
1024e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
1024f 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
10250 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
10251 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
10252 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
10253 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
10254 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
10255 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
10256 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
10257 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
10258 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
10259 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
1025a 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
1025b 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
1025c 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
1025d 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
1025e 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
1025f 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74  /.int winCurrent
10260 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Time(sqlite3_vfs
10261 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a   *pVfs, double *
10262 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49  prNow){.  FILETI
10263 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45  ME ft;.  /* FILE
10264 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69  TIME structure i
10265 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65  s a 64-bit value
10266 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
10267 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20  e number of .   
10268 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64    100-nanosecond
10269 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65   intervals since
1026a 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31   January 1, 1601
1026b 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35   (= JD 2305813.5
1026c 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ). .  */.  sqlit
1026d 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b 20  e3_int64 timeW; 
1026e 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 20    /* Whole days 
1026f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
10270 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 46  64 timeF;   /* F
10271 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 2a  ractional Days *
10272 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  /..  /* Number o
10273 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64  f 100-nanosecond
10274 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 20   intervals in a 
10275 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 20  single day */.  
10276 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
10277 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65  ite3_int64 ntuPe
10278 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30  rDay = .      10
10279 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  000000*(sqlite3_
1027a 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 20  int64)86400;..  
1027b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30  /* Number of 100
1027c 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65  -nanosecond inte
1027d 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f 66  rvals in half of
1027e 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74   a day */.  stat
1027f 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
10280 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 6c  _int64 ntuPerHal
10281 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30  fDay = .      10
10282 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  000000*(sqlite3_
10283 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 20  int64)43200;..  
10284 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 6f  /* 2^32 - to avo
10285 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e 64  id use of LL and
10286 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 63   warnings in gcc
10287 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
10288 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  st sqlite3_int64
10289 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 3d   max32BitValue =
1028a 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 33   .      (sqlite3
1028b 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30  _int64)200000000
1028c 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  0 + (sqlite3_int
1028d 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20  64)2000000000 + 
1028e 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32  (sqlite3_int64)2
1028f 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 53  94967296;..#if S
10290 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
10291 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d 65   SYSTEMTIME time
10292 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d  ;.  GetSystemTim
10293 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69  e(&time);.  /* i
10294 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69  f SystemTimeToFi
10295 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c 20  leTime() fails, 
10296 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e  it returns zero.
10297 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 65   */.  if (!Syste
10298 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28  mTimeToFileTime(
10299 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20  &time,&ft)){.   
1029a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
1029b 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 6d  else.  GetSystem
1029c 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 20  TimeAsFileTime( 
1029d 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  &ft );.#endif.  
1029e 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1029f 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 20  (pVfs);.  timeW 
102a0 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74  = (((sqlite3_int
102a1 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 65  64)ft.dwHighDate
102a2 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 61  Time)*max32BitVa
102a3 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 5f  lue) + (sqlite3_
102a4 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 61  int64)ft.dwLowDa
102a5 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 20  teTime;.  timeF 
102a6 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 72  = timeW % ntuPer
102a7 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Day;          /*
102a8 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73   fractional days
102a9 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64   (100-nanosecond
102aa 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20  s) */.  timeW = 
102ab 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 61  timeW / ntuPerDa
102ac 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77  y;          /* w
102ad 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 74  hole days */.  t
102ae 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 32  imeW = timeW + 2
102af 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 20  305813;         
102b0 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20     /* add whole 
102b1 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 38  days (from 23058
102b2 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 46  13.5) */.  timeF
102b3 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 65   = timeF + ntuPe
102b4 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 2f  rHalfDay;      /
102b5 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 79  * add half a day
102b6 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35   (from 2305813.5
102b7 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74  ) */.  timeW = t
102b8 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e 74  imeW + (timeF/nt
102b9 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 64  uPerDay);  /* ad
102ba 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 68  d whole day if h
102bb 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e 65  alf day made one
102bc 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69   */.  timeF = ti
102bd 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 3b  meF % ntuPerDay;
102be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d            /* com
102bf 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 6f  pute new fractio
102c0 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a 70  nal days */.  *p
102c1 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 74  rNow = (double)t
102c2 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 29  imeW + ((double)
102c3 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 29  timeF / (double)
102c4 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 64  ntuPerDay);.#ifd
102c5 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
102c6 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
102c7 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20  rent_time ){.   
102c8 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 62   *prNow = ((doub
102c9 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65  le)sqlite3_curre
102ca 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 6c  nt_time + (doubl
102cb 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 62  e)43200) / (doub
102cc 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 62  le)86400 + (doub
102cd 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d 0a  le)2440587;.  }.
102ce 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
102cf 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
102d0 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69  idea is that thi
102d1 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73  s function works
102d2 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74   like a combinat
102d3 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73  ion of.** GetLas
102d4 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72  tError() and For
102d5 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20  matMessage() on 
102d6 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e  windows (or errn
102d7 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f  o and.** strerro
102d8 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20  r_r() on unix). 
102d9 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69  After an error i
102da 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e  s returned by an
102db 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c   OS.** function,
102dc 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68   SQLite calls th
102dd 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  is function with
102de 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74   zBuf pointing t
102df 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66  o.** a buffer of
102e0 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65   nBuf bytes. The
102e1 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64   OS layer should
102e2 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a   populate the.**
102e3 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e   buffer with a n
102e4 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ul-terminated UT
102e5 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f  F-8 encoded erro
102e6 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73  r message.** des
102e7 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74  cribing the last
102e8 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76   IO error to hav
102e9 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69  e occurred withi
102ea 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  n the calling.**
102eb 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49   thread..**.** I
102ec 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
102ed 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  age is too large
102ee 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65   for the supplie
102ef 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20  d buffer,.** it 
102f0 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61  should be trunca
102f1 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20  ted. The return 
102f2 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73  value of xGetLas
102f3 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72  tError.** is zer
102f4 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d  o if the error m
102f5 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74  essage fits in t
102f6 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f  he buffer, or no
102f7 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77  n-zero.** otherw
102f8 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73  ise (if the mess
102f9 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65  age was truncate
102fa 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20  d). If non-zero 
102fb 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20  is returned,.** 
102fc 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e  then it is not n
102fd 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c  ecessary to incl
102fe 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d  ude the nul-term
102ff 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72  inator character
10300 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75  .** in the outpu
10301 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  t buffer..**.** 
10302 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e  Not supplying an
10303 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
10304 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65  ill have no adve
10305 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e  rse effect.** on
10306 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66   SQLite. It is f
10307 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69  ine to have an i
10308 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68  mplementation th
10309 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75  at never.** retu
1030a 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  rns an error mes
1030b 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e  sage:.**.**   in
1030c 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28  t xGetLastError(
1030d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1030e 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
1030f 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20  r *zBuf){.**    
10310 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d   assert(zBuf[0]=
10311 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72  ='\0');.**     r
10312 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a  eturn 0;.**   }.
10313 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66  **.** However if
10314 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
10315 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69  e is supplied, i
10316 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70  t will be incorp
10317 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c  orated.** by sql
10318 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72  ite into the err
10319 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c  or message avail
1031a 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72  able to the user
1031b 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65   using.** sqlite
1031c 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73  3_errmsg(), poss
1031d 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65  ibly making IO e
1031e 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20  rrors easier to 
1031f 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  debug..*/.static
10320 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45   int winGetLastE
10321 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
10322 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
10323 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
10324 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10325 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72  R(pVfs);.  retur
10326 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73  n getLastErrorMs
10327 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d  g(nBuf, zBuf);.}
10328 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
10329 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
1032a 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ize the operatin
1032b 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
1032c 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
1032d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
1032e 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73  _init(void){.  s
1032f 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
10330 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20  s winVfs = {.   
10331 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
10332 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
10333 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69  */.    sizeof(wi
10334 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f  nFile),   /* szO
10335 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58  sFile */.    MAX
10336 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20  _PATH,          
10337 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
10338 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
10339 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
1033a 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c   */.    "win32",
1033b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
1033c 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
1033d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1033e 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20   pAppData */. . 
1033f 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20     winOpen,     
10340 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
10341 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c  /.    winDelete,
10342 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
10343 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63  ete */.    winAc
10344 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  cess,         /*
10345 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20   xAccess */.    
10346 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  winFullPathname,
10347 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
10348 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  ame */.    winDl
10349 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  Open,         /*
1034a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20   xDlOpen */.    
1034b 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20  winDlError,     
1034c 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a     /* xDlError *
1034d 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20  /.    winDlSym, 
1034e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53           /* xDlS
1034f 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43  ym */.    winDlC
10350 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  lose,        /* 
10351 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20  xDlClose */.    
10352 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20  winRandomness,  
10353 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73     /* xRandomnes
10354 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65  s */.    winSlee
10355 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  p,          /* x
10356 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e  Sleep */.    win
10357 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20  CurrentTime,    
10358 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20  /* xCurrentTime 
10359 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73  */.    winGetLas
1035a 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65  tError    /* xGe
1035b 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20  tLastError */.  
1035c 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  };..  sqlite3_vf
1035d 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56  s_register(&winV
1035e 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e  fs, 1);.  return
1035f 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53   SQLITE_OK; .}.S
10360 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
10361 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69  lite3_os_end(voi
10362 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51  d){ .  return SQ
10363 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
10364 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
10365 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  WIN */../*******
10366 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
10367 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
10368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1036a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1036b 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1036c 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a  le bitvec.c ****
1036d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1036e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1036f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
10370 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a  08 February 16.*
10371 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
10372 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
10373 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
10374 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
10375 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
10376 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
10377 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
10378 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
10379 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1037a 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1037b 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1037c 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1037d 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1037e 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1037f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
10380 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
10381 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
10382 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
10383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
10387 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
10388 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20  ments an object 
10389 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
1038a 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a  a fixed-length.*
1038b 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20  * bitmap.  Bits 
1038c 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61  are numbered sta
1038d 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a  rting with 1..**
1038e 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20  .** A bitmap is 
1038f 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
10390 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20  hich pages of a 
10391 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
10392 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e  ve been.** journ
10393 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74  alled during a t
10394 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77  ransaction, or w
10395 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20  hich pages have 
10396 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22  the "dont-write"
10397 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55  .** property.  U
10398 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65  sually only a fe
10399 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74  w pages are meet
1039a 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f   either conditio
1039b 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74  n..** So the bit
1039c 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73  map is usually s
1039d 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f  parse and has lo
1039e 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a  w cardinality..*
1039f 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20  * But sometimes 
103a0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65  (for example whe
103a1 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20  n during a DROP 
103a2 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65  of a large table
103a3 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c  ) most.** or all
103a4 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e   of the pages in
103a5 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20   a database can 
103a6 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20  get journalled. 
103a7 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c   In those cases,
103a8 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20   .** the bitmap 
103a9 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69  becomes dense wi
103aa 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c  th high cardinal
103ab 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69  ity.  The algori
103ac 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f  thm needs .** to
103ad 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73   handle both cas
103ae 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  es well..**.** T
103af 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62  he size of the b
103b0 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77  itmap is fixed w
103b1 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69  hen the object i
103b2 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  s created..**.**
103b3 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c   All bits are cl
103b4 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74  ear when the bit
103b5 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20  map is created. 
103b6 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73   Individual bits
103b7 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f  .** may be set o
103b8 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74  r cleared one at
103b9 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54   a time..**.** T
103ba 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  est operations a
103bb 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d  re about 100 tim
103bc 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74  es more common t
103bd 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f  hat set operatio
103be 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65  ns..** Clear ope
103bf 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65  rations are exce
103c0 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54  edingly rare.  T
103c1 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79  here are usually
103c2 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e   between.** 5 an
103c3 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74  d 500 set operat
103c4 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20  ions per Bitvec 
103c5 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74  object, though t
103c6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74  he number of set
103c7 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d  s can.** sometim
103c8 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e  es grow into ten
103c9 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f  s of thousands o
103ca 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73  r larger.  The s
103cb 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69  ize of the.** Bi
103cc 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74  tvec object is t
103cd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
103ce 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
103cf 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a  se file at the.*
103d0 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61  * start of a tra
103d1 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73  nsaction, and is
103d2 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65   thus usually le
103d3 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68  ss than a few th
103d4 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63  ousand,.** but c
103d5 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61  an be as large a
103d6 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20  s 2 billion for 
103d7 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74  a really big dat
103d8 61 62 61 73 65 2e 0a 2a 2f 0a 0a 2f 2a 20 53 69  abase..*/../* Si
103d9 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63  ze of the Bitvec
103da 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79   structure in by
103db 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tes. */.#define 
103dc 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20  BITVEC_SZ       
103dd 20 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a   (sizeof(void*)*
103de 31 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20  128)  /* 512 on 
103df 33 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20  32bit.  1024 on 
103e0 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75  64bit */../* Rou
103e1 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a  nd the union siz
103e2 65 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65  e down to the ne
103e3 61 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f  arest pointer bo
103e4 75 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68  undary, since th
103e5 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20  at's how .** it 
103e6 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20  will be aligned 
103e7 77 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65  within the Bitve
103e8 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65  c struct. */.#de
103e9 66 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a  fine BITVEC_USIZ
103ea 45 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f  E     (((BITVEC_
103eb 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32  SZ-(3*sizeof(u32
103ec 29 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65  )))/sizeof(Bitve
103ed 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76  c*))*sizeof(Bitv
103ee 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f  ec*))../* Type o
103ef 66 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65  f the array "ele
103f0 6d 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69  ment" for the bi
103f1 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74  tmap representat
103f2 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20  ion. .** Should 
103f3 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c  be a power of 2,
103f4 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76   and ideally, ev
103f5 65 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f  enly divide into
103f6 20 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a   BITVEC_USIZE. .
103f7 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20  ** Setting this 
103f8 74 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20  to the "natural 
103f9 77 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f  word" size of yo
103fa 75 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f  ur CPU may impro
103fb 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63  ve.** performanc
103fc 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  e. */.#define BI
103fd 54 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75  TVEC_TELEM     u
103fe 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69  8./* Size, in bi
103ff 74 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ts, of the bitma
10400 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64  p element. */.#d
10401 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45  efine BITVEC_SZE
10402 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62  LEM    8./* Numb
10403 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
10404 6e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79  n a bitmap array
10405 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
10406 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42  VEC_NELEM     (B
10407 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65  ITVEC_USIZE/size
10408 6f 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29  of(BITVEC_TELEM)
10409 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62  )./* Number of b
1040a 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61  its in the bitma
1040b 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66  p array. */.#def
1040c 69 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20  ine BITVEC_NBIT 
1040d 20 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c       (BITVEC_NEL
1040e 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  EM*BITVEC_SZELEM
1040f 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  )../* Number of 
10410 75 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61  u32 values in ha
10411 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65  sh table. */.#de
10412 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54  fine BITVEC_NINT
10413 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53        (BITVEC_US
10414 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29  IZE/sizeof(u32))
10415 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62  ./* Maximum numb
10416 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
10417 20 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f   hash table befo
10418 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64  re .** sub-divid
10419 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69  ing and re-hashi
1041a 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ng. */.#define B
1041b 49 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20  ITVEC_MXHASH    
1041c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a  (BITVEC_NINT/2).
1041d 2f 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74  /* Hashing funct
1041e 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73  ion for the aHas
1041f 68 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  h representation
10420 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74  ..** Empirical t
10421 65 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68  esting showed th
10422 61 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69  at the *37 multi
10423 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72  plier .** (an ar
10424 62 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e  bitrary prime)in
10425 20 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   the hash functi
10426 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20  on provided .** 
10427 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69  no fewer collisi
10428 6f 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d  ons than the no-
10429 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e  op *1. */.#defin
1042a 65 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29  e BITVEC_HASH(X)
1042b 20 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56     (((X)*1)%BITV
1042c 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e  EC_NINT)..#defin
1042d 65 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20  e BITVEC_NPTR   
1042e 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45     (BITVEC_USIZE
1042f 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a  /sizeof(Bitvec *
10430 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74  )).../*.** A bit
10431 6d 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  map is an instan
10432 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
10433 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
10434 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70  *.** This bitmap
10435 20 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69   records the exi
10436 73 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f  stance of zero o
10437 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77  r more bits.** w
10438 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65  ith values betwe
10439 65 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20  en 1 and iSize, 
1043a 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a  inclusive..**.**
1043b 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
1043c 20 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73   possible repres
1043d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
1043e 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69   bitmap..** If i
1043f 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49  Size<=BITVEC_NBI
10440 54 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75  T, then Bitvec.u
10441 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20  .aBitmap[] is a 
10442 73 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d  straight.** bitm
10443 61 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73  ap.  The least s
10444 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69  ignificant bit i
10445 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49  s bit 1..**.** I
10446 66 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e  f iSize>BITVEC_N
10447 42 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72  BIT and iDivisor
10448 3d 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e  ==0 then Bitvec.
10449 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20  u.aHash[] is.** 
1044a 61 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61  a hash table tha
1044b 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74  t will hold up t
1044c 6f 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20  o BITVEC_MXHASH 
1044d 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e  distinct values.
1044e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
1044f 2c 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73  , the value i is
10450 20 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f   redirected into
10451 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e   one of BITVEC_N
10452 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61  PTR.** sub-bitma
10453 70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ps pointed to by
10454 20 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b   Bitvec.u.apSub[
10455 5d 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d  ].  Each subbitm
10456 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70  ap.** handles up
10457 20 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70   to iDivisor sep
10458 61 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20  arate values of 
10459 69 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c  i.  apSub[0] hol
1045a 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74  ds.** values bet
1045b 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69  ween 1 and iDivi
1045c 73 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68  sor.  apSub[1] h
1045d 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77  olds values betw
1045e 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b  een.** iDivisor+
1045f 31 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72  1 and 2*iDivisor
10460 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64  .  apSub[N] hold
10461 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  s values between
10462 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31  .** N*iDivisor+1
10463 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69   and (N+1)*iDivi
10464 73 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69  sor.  Each subbi
10465 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a  tmap is normaliz
10466 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65  ed.** to hold de
10467 61 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62  al with values b
10468 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69  etween 1 and iDi
10469 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74  visor..*/.struct
1046a 20 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20   Bitvec {.  u32 
1046b 69 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d  iSize;      /* M
1046c 61 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78  aximum bit index
1046d 2e 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20  .  Max iSize is 
1046e 34 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a  4,294,967,296. *
1046f 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20  /.  u32 nSet;   
10470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10471 20 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73   bits that are s
10472 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20  et - only valid 
10473 66 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20  for aHash.      
10474 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65              ** e
10475 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20  lement.  Max is 
10476 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f  BITVEC_NINT.  Fo
10477 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35  r BITVEC_SZ of 5
10478 31 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  12,.            
10479 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f        ** this wo
1047a 75 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20  uld be 125. */. 
1047b 20 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20   u32 iDivisor;  
1047c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69   /* Number of bi
1047d 74 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61  ts handled by ea
1047e 63 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79  ch apSub[] entry
1047f 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  . */.           
10480 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64         /* Should
10481 20 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65   >=0 for apSub e
10482 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20  lement. */.     
10483 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10484 4d 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20  Max iDivisor is 
10485 6d 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45  max(u32) / BITVE
10486 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a  C_NPTR + 1.  */.
10487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10488 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45    /* For a BITVE
10489 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69  C_SZ of 512, thi
1048a 73 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35  s would be 34,35
1048b 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f  9,739. */.  unio
1048c 6e 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54  n {.    BITVEC_T
1048d 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54  ELEM aBitmap[BIT
1048e 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f  VEC_NELEM];    /
1048f 2a 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65  * Bitmap represe
10490 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75  ntation */.    u
10491 33 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f  32 aHash[BITVEC_
10492 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48  NINT];      /* H
10493 61 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73  ash table repres
10494 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
10495 42 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49  Bitvec *apSub[BI
10496 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20  TVEC_NPTR];  /* 
10497 52 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73  Recursive repres
10498 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20  entation */.  } 
10499 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  u;.};../*.** Cre
1049a 61 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70  ate a new bitmap
1049b 20 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20   object able to 
1049c 68 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77  handle bits betw
1049d 65 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c  een 0 and iSize,
1049e 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20  .** inclusive.  
1049f 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
104a0 20 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65   to the new obje
104a1 63 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ct.  Return NULL
104a2 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66   if .** malloc f
104a3 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ails..*/.SQLITE_
104a4 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a  PRIVATE Bitvec *
104a5 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65  sqlite3BitvecCre
104a6 61 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a  ate(u32 iSize){.
104a7 20 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61    Bitvec *p;.  a
104a8 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70  ssert( sizeof(*p
104a9 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a  )==BITVEC_SZ );.
104aa 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
104ab 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
104ac 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29  *p) );.  if( p )
104ad 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d  {.    p->iSize =
104ae 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65   iSize;.  }.  re
104af 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
104b0 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
104b1 20 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73   the i-th bit is
104b2 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72   set.  Return tr
104b3 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20  ue or false..** 
104b4 49 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66  If p is NULL (if
104b5 20 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20   the bitmap has 
104b6 6e 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64  not been created
104b7 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20  ) or if.** i is 
104b8 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
104b9 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  en return false.
104ba 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
104bb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
104bc 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20  tvecTest(Bitvec 
104bd 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66  *p, u32 i){.  if
104be 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
104bf 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53  0;.  if( i>p->iS
104c0 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65  ize || i==0 ) re
104c1 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20  turn 0;.  i--;. 
104c2 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69   while( p->iDivi
104c3 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62  sor ){.    u32 b
104c4 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73  in = i/p->iDivis
104c5 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d  or;.    i = i%p-
104c6 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70  >iDivisor;.    p
104c7 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69   = p->u.apSub[bi
104c8 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20  n];.    if (!p) 
104c9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
104ca 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
104cb 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56  ( p->iSize<=BITV
104cc 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72  EC_NBIT ){.    r
104cd 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74  eturn (p->u.aBit
104ce 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45  map[i/BITVEC_SZE
104cf 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42  LEM] & (1<<(i&(B
104d0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29  ITVEC_SZELEM-1))
104d1 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b  ))!=0;.  } else{
104d2 0a 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54  .    u32 h = BIT
104d3 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20  VEC_HASH(i++);. 
104d4 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61     while( p->u.a
104d5 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20  Hash[h] ){.     
104d6 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b   if( p->u.aHash[
104d7 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31  h]==i ) return 1
104d8 3b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31  ;.      h = (h+1
104d9 29 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b  ) % BITVEC_NINT;
104da 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
104db 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  n 0;.  }.}../*.*
104dc 2a 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62  * Set the i-th b
104dd 69 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e  it.  Return 0 on
104de 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20   success and an 
104df 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a  error code if.**
104e0 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
104e1 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rong..**.** This
104e2 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63   routine might c
104e3 61 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73  ause sub-bitmaps
104e4 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64   to be allocated
104e5 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f  .  Failing.** to
104e6 20 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20   get the memory 
104e7 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74  needed to hold t
104e8 68 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73  he sub-bitmap is
104e9 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61   the only.** tha
104ea 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77  t can go wrong w
104eb 69 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61  ith an insert, a
104ec 73 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20  ssuming p and i 
104ed 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a  are valid..**.**
104ee 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
104ef 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72  ction must ensur
104f0 65 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61  e that p is a va
104f1 6c 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63  lid Bitvec objec
104f2 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68  t.** and that th
104f3 65 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20  e value for "i" 
104f4 69 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20  is within range 
104f5 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62  of the Bitvec ob
104f6 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  ject..** Otherwi
104f7 73 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  se the behavior 
104f8 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
104f9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
104fa 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
104fb 63 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20  cSet(Bitvec *p, 
104fc 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b  u32 i){.  u32 h;
104fd 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
104fe 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
104ff 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b    assert( i>0 );
10500 0a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d  .  assert( i<=p-
10501 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b  >iSize );.  i--;
10502 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69  .  while((p->iSi
10503 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54  ze > BITVEC_NBIT
10504 29 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72  ) && p->iDivisor
10505 29 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20  ) {.    u32 bin 
10506 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i/p->iDivisor;
10507 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44  .    i = i%p->iD
10508 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20  ivisor;.    if( 
10509 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d  p->u.apSub[bin]=
1050a 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75  =0 ){.      p->u
1050b 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71  .apSub[bin] = sq
1050c 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
1050d 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29  e( p->iDivisor )
1050e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75  ;.      if( p->u
1050f 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29  .apSub[bin]==0 )
10510 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
10511 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
10512 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62  p = p->u.apSub[b
10513 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  in];.  }.  if( p
10514 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  ->iSize<=BITVEC_
10515 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75  NBIT ){.    p->u
10516 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45  .aBitmap[i/BITVE
10517 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c  C_SZELEM] |= 1 <
10518 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45  < (i&(BITVEC_SZE
10519 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74  LEM-1));.    ret
1051a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1051b 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f   }.  h = BITVEC_
1051c 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20  HASH(i++);.  /* 
1051d 69 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20  if there wasn't 
1051e 61 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e  a hash collision
1051f 2c 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e  , and this doesn
10520 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c  't */.  /* compl
10521 65 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68  etely fill the h
10522 61 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61  ash, then just a
10523 64 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f  dd it without */
10524 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62  .  /* worring ab
10525 6f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67  out sub-dividing
10526 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e   and re-hashing.
10527 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e   */.  if( !p->u.
10528 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20  aHash[h] ){.    
10529 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54  if (p->nSet<(BIT
1052a 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20  VEC_NINT-1)) {. 
1052b 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63       goto bitvec
1052c 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20  _set_end;.    } 
1052d 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74  else {.      got
1052e 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68  o bitvec_set_reh
1052f 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ash;.    }.  }. 
10530 20 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20   /* there was a 
10531 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b  collision, check
10532 20 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20   to see if it's 
10533 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20  already */.  /* 
10534 69 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c  in hash, if not,
10535 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73   try to find a s
10536 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20  pot for it */.  
10537 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  do {.    if( p->
10538 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20  u.aHash[h]==i ) 
10539 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1053a 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69  ;.    h++;.    i
1053b 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e  f( h>=BITVEC_NIN
1053c 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77  T ) h = 0;.  } w
1053d 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68  hile( p->u.aHash
1053e 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64  [h] );.  /* we d
1053f 69 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e  idn't find it in
10540 20 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f   the hash.  h po
10541 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
10542 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61  t */.  /* availa
10543 62 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63  ble free spot. c
10544 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
10545 68 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20  his is going to 
10546 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72  */.  /* make our
10547 20 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22   hash too "full"
10548 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74  .  */.bitvec_set
10549 5f 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70  _rehash:.  if( p
1054a 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d  ->nSet>=BITVEC_M
1054b 58 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73  XHASH ){.    uns
1054c 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20  igned int j;.   
1054d 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32   int rc;.    u32
1054e 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c   *aiValues = sql
1054f 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61  ite3StackAllocRa
10550 77 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75  w(0, sizeof(p->u
10551 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66  .aHash));.    if
10552 28 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b  ( aiValues==0 ){
10553 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
10554 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
10555 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
10556 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d  cpy(aiValues, p-
10557 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66  >u.aHash, sizeof
10558 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20  (p->u.aHash));. 
10559 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75       memset(p->u
1055a 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f  .apSub, 0, sizeo
1055b 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a  f(p->u.apSub));.
1055c 20 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f        p->iDiviso
1055d 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20  r = (p->iSize + 
1055e 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29  BITVEC_NPTR - 1)
1055f 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20  /BITVEC_NPTR;.  
10560 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
10561 42 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b  BitvecSet(p, i);
10562 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
10563 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a  j<BITVEC_NINT; j
10564 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
10565 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72   aiValues[j] ) r
10566 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76  c |= sqlite3Bitv
10567 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65  ecSet(p, aiValue
10568 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  s[j]);.      }. 
10569 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63       sqlite3Stac
1056a 6b 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65  kFree(0, aiValue
1056b 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  s);.      return
1056c 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62   rc;.    }.  }.b
1056d 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20  itvec_set_end:. 
1056e 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d   p->nSet++;.  p-
1056f 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b  >u.aHash[h] = i;
10570 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10571 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
10572 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74  ear the i-th bit
10573 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73  ..**.** pBuf mus
10574 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  t be a pointer t
10575 6f 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45  o at least BITVE
10576 43 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65  C_SZ bytes of te
10577 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a  mporary storage.
10578 2a 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c  ** that BitvecCl
10579 65 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72  ear can use to r
1057a 65 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20  ebuilt its hash 
1057b 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
1057c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1057d 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
1057e 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20  (Bitvec *p, u32 
1057f 69 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a  i, void *pBuf){.
10580 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
10581 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69  urn;.  assert( i
10582 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77  >0 );.  i--;.  w
10583 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f  hile( p->iDiviso
10584 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e  r ){.    u32 bin
10585 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72   = i/p->iDivisor
10586 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69  ;.    i = i%p->i
10587 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d  Divisor;.    p =
10588 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d   p->u.apSub[bin]
10589 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a  ;.    if (!p) {.
1058a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1058b 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d    }.  }.  if( p-
1058c 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e  >iSize<=BITVEC_N
1058d 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e  BIT ){.    p->u.
1058e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43  aBitmap[i/BITVEC
1058f 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20  _SZELEM] &= ~(1 
10590 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a  << (i&(BITVEC_SZ
10591 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c  ELEM-1)));.  }el
10592 73 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64  se{.    unsigned
10593 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20   int j;.    u32 
10594 2a 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66  *aiValues = pBuf
10595 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56  ;.    memcpy(aiV
10596 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73  alues, p->u.aHas
10597 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61  h, sizeof(p->u.a
10598 48 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73  Hash));.    mems
10599 65 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30  et(p->u.aHash, 0
1059a 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48  , sizeof(p->u.aH
1059b 61 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53  ash));.    p->nS
1059c 65 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  et = 0;.    for(
1059d 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49  j=0; j<BITVEC_NI
1059e 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  NT; j++){.      
1059f 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20  if( aiValues[j] 
105a0 26 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d  && aiValues[j]!=
105a1 28 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20  (i+1) ){.       
105a2 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f   u32 h = BITVEC_
105a3 48 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d  HASH(aiValues[j]
105a4 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  -1);.        p->
105a5 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nSet++;.        
105a6 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73  while( p->u.aHas
105a7 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  h[h] ){.        
105a8 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    h++;.         
105a9 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e   if( h>=BITVEC_N
105aa 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20  INT ) h = 0;.   
105ab 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
105ac 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61  ->u.aHash[h] = a
105ad 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20  iValues[j];.    
105ae 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
105af 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61  ./*.** Destroy a
105b0 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20   bitmap object. 
105b1 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d   Reclaim all mem
105b2 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c  ory used..*/.SQL
105b3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
105b4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
105b5 73 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29  stroy(Bitvec *p)
105b6 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
105b7 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e  eturn;.  if( p->
105b8 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20  iDivisor ){.    
105b9 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
105ba 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42      for(i=0; i<B
105bb 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29  ITVEC_NPTR; i++)
105bc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
105bd 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e  itvecDestroy(p->
105be 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20  u.apSub[i]);.   
105bf 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
105c0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
105c1 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
105c2 6c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65  lue of the iSize
105c3 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69   parameter speci
105c4 66 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63  fied when Bitvec
105c5 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74   *p.** was creat
105c6 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
105c7 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
105c8 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76  3BitvecSize(Bitv
105c9 65 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ec *p){.  return
105ca 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69   p->iSize;.}..#i
105cb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
105cc 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f  T_BUILTIN_TEST./
105cd 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20  *.** Let V[] be 
105ce 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69  an array of unsi
105cf 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20  gned characters 
105d0 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f  sufficient to ho
105d1 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69  ld.** up to N bi
105d2 74 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e  ts.  Let I be an
105d3 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
105d4 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c   0 and N.  0<=I<
105d5 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66  N..** Then the f
105d6 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20  ollowing macros 
105d7 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
105d8 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65  et, clear, or te
105d9 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  st.** individual
105da 20 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a   bits within V..
105db 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49  */.#define SETBI
105dc 54 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e  T(V,I)      V[I>
105dd 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29  >3] |= (1<<(I&7)
105de 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42  ).#define CLEARB
105df 49 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e  IT(V,I)    V[I>>
105e0 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29  3] &= ~(1<<(I&7)
105e1 29 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49  ).#define TESTBI
105e2 54 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e  T(V,I)     (V[I>
105e3 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21  >3]&(1<<(I&7)))!
105e4 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  =0../*.** This r
105e5 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65  outine runs an e
105e6 78 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66  xtensive test of
105e7 20 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65   the Bitvec code
105e8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
105e9 74 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  t is an array of
105ea 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61   integers that a
105eb 63 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d  cts as a program
105ec 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20  .** to test the 
105ed 42 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74  Bitvec.  The int
105ee 65 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65  egers are opcode
105ef 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79  s followed.** by
105f0 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72   0, 1, or 3 oper
105f1 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20  ands, depending 
105f2 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20  on the opcode.  
105f3 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64  Another.** opcod
105f4 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69  e follows immedi
105f5 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20  ately after the 
105f6 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a  last operand..**
105f7 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20  .** There are 6 
105f8 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64  opcodes numbered
105f9 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20   from 0 through 
105fa 35 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20  5.  0 is the.** 
105fb 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e  "halt" opcode an
105fc 64 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73  d causes the tes
105fd 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20  t to end..**.** 
105fe 20 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61     0          Ha
105ff 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  lt and return th
10600 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
10601 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58  rs.**    1 N S X
10602 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62      Set N bits b
10603 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20  eginning with S 
10604 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67  and incrementing
10605 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20   by X.**    2 N 
10606 53 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62  S X    Clear N b
10607 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  its beginning wi
10608 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65  th S and increme
10609 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20  nting by X.**   
1060a 20 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20   3 N        Set 
1060b 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65  N randomly chose
1060c 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e  n bits.**    4 N
1060d 20 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20          Clear N 
1060e 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20  randomly chosen 
1060f 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53  bits.**    5 N S
10610 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73   X    Set N bits
10611 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e   from S incremen
10612 74 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c  t X in array onl
10613 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63  y, not in bitvec
10614 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64  .**.** The opcod
10615 65 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70  es 1 through 4 p
10616 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63  erform set and c
10617 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20  lear operations 
10618 61 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a  are performed.**
10619 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65   on both a Bitve
1061a 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20  c object and on 
1061b 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f  a linear array o
1061c 66 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20  f bits obtained 
1061d 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20  from malloc..** 
1061e 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f  Opcode 5 works o
1061f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72  n the linear arr
10620 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20  ay only, not on 
10621 74 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f  the Bitvec..** O
10622 70 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20  pcode 5 is used 
10623 74 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20  to deliberately 
10624 69 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69  induce a fault i
10625 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f  n order to.** co
10626 6e 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72  nfirm that error
10627 20 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73   detection works
10628 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63  ..**.** At the c
10629 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65  onclusion of the
1062a 20 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72   test the linear
1062b 20 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72   array is compar
1062c 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68  ed.** against th
1062d 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e  e Bitvec object.
1062e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61    If there are a
1062f 6e 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a  ny differences,.
10630 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  ** an error is r
10631 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65  eturned.  If the
10632 79 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20  y are the same, 
10633 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
10634 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
10635 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
10636 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
10637 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54  urn -1..*/.SQLIT
10638 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10639 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74  lite3BitvecBuilt
1063a 69 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69  inTest(int sz, i
1063b 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76  nt *aOp){.  Bitv
1063c 65 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b  ec *pBitvec = 0;
1063d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1063e 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20   *pV = 0;.  int 
1063f 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69  rc = -1;.  int i
10640 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20  , nx, pc, op;.  
10641 76 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b  void *pTmpSpace;
10642 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
10643 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65  the Bitvec to be
10644 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69   tested and a li
10645 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20  near array of.  
10646 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61  ** bits to act a
10647 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
10648 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73  */.  pBitvec = s
10649 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
1064a 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d  te( sz );.  pV =
1064b 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1064c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b   (sz+7)/8 + 1 );
1064d 0a 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73  .  pTmpSpace = s
1064e 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49  qlite3_malloc(BI
1064f 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20  TVEC_SZ);.  if( 
10650 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56  pBitvec==0 || pV
10651 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65  ==0 || pTmpSpace
10652 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76  ==0  ) goto bitv
10653 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74  ec_end;.  memset
10654 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38  (pV, 0, (sz+7)/8
10655 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c   + 1);..  /* NUL
10656 4c 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20  L pBitvec tests 
10657 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  */.  sqlite3Bitv
10658 65 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73  ecSet(0, 1);.  s
10659 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61  qlite3BitvecClea
1065a 72 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63  r(0, 1, pTmpSpac
1065b 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68  e);..  /* Run th
1065c 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70  e program */.  p
1065d 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  c = 0;.  while( 
1065e 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d  (op = aOp[pc])!=
1065f 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28  0 ){.    switch(
10660 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73   op ){.      cas
10661 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 1:.      case 
10662 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a  2:.      case 5:
10663 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20   {.        nx = 
10664 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61  4;.        i = a
10665 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20  Op[pc+2] - 1;.  
10666 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20        aOp[pc+2] 
10667 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20  += aOp[pc+3];.  
10668 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
10669 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
1066a 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a  3:.      case 4:
1066b 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a   .      default:
1066c 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20   {.        nx = 
1066d 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  2;.        sqlit
1066e 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
1066f 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20  zeof(i), &i);.  
10670 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
10671 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
10672 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29  f( (--aOp[pc+1])
10673 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20   > 0 ) nx = 0;. 
10674 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20     pc += nx;.   
10675 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66   i = (i & 0x7fff
10676 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66  ffff)%sz;.    if
10677 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b  ( (op & 1)!=0 ){
10678 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56  .      SETBIT(pV
10679 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20  , (i+1));.      
1067a 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20  if( op!=5 ){.   
1067b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1067c 42 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65  BitvecSet(pBitve
1067d 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62  c, i+1) ) goto b
1067e 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20  itvec_end;.     
1067f 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
10680 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c      CLEARBIT(pV,
10681 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73   (i+1));.      s
10682 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61  qlite3BitvecClea
10683 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20  r(pBitvec, i+1, 
10684 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20  pTmpSpace);.    
10685 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74  }.  }..  /* Test
10686 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
10687 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65  e linear array e
10688 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74  xactly matches t
10689 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f  he.  ** Bitvec o
1068a 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69  bject.  Start wi
1068b 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f  th the assumptio
1068c 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20  n that they do. 
1068d 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30   ** match (rc==0
1068e 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f  ).  Change rc to
1068f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64   non-zero if a d
10690 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20  iscrepancy.  ** 
10691 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20  is found..  */. 
10692 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74   rc = sqlite3Bit
10693 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73  vecTest(0,0) + s
10694 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
10695 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a  (pBitvec, sz+1).
10696 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69            + sqli
10697 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42  te3BitvecTest(pB
10698 69 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20  itvec, 0).      
10699 20 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69      + (sqlite3Bi
1069a 74 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63  tvecSize(pBitvec
1069b 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69  ) - sz);.  for(i
1069c 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b  =1; i<=sz; i++){
1069d 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42  .    if(  (TESTB
1069e 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74  IT(pV,i))!=sqlit
1069f 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69  e3BitvecTest(pBi
106a0 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20  tvec,i) ){.     
106a1 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62   rc = i;.      b
106a2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
106a3 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63  .  /* Free alloc
106a4 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a  ated structure *
106a5 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20  /.bitvec_end:.  
106a6 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d  sqlite3_free(pTm
106a7 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74  pSpace);.  sqlit
106a8 65 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73  e3_free(pV);.  s
106a9 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
106aa 72 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20  roy(pBitvec);.  
106ab 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
106ac 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
106ad 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20  IT_BUILTIN_TEST 
106ae 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
106af 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65  *** End of bitve
106b0 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c.c ************
106b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106b3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
106b4 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
106b5 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  cache.c ********
106b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106b8 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41  **/./*.** 2008 A
106b9 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54  ugust 05.**.** T
106ba 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
106bb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
106bc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
106bd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
106be 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
106bf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
106c0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
106c1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
106c2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
106c3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
106c4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
106c5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
106c6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
106c7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
106c8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
106c9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
106ca 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
106cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106cf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
106d0 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
106d1 74 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e  that page cache.
106d2 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d  .*/../*.** A com
106d3 70 6c 65 74 65 20 70 61 67 65 20 63 61 63 68 65  plete page cache
106d4 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
106d5 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
106d6 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61  e..*/.struct PCa
106d7 63 68 65 20 7b 0a 20 20 50 67 48 64 72 20 2a 70  che {.  PgHdr *p
106d8 44 69 72 74 79 2c 20 2a 70 44 69 72 74 79 54 61  Dirty, *pDirtyTa
106d9 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  il;         /* L
106da 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67  ist of dirty pag
106db 65 73 20 69 6e 20 4c 52 55 20 6f 72 64 65 72 20  es in LRU order 
106dc 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 79 6e  */.  PgHdr *pSyn
106dd 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ced;            
106de 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
106df 20 73 79 6e 63 65 64 20 70 61 67 65 20 69 6e 20   synced page in 
106e0 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 74 20  dirty page list 
106e1 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
106e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
106e4 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64  er of referenced
106e5 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20   pages */.  int 
106e6 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20  nMax;           
106e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106e8 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63 61  /* Configured ca
106e9 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  che size */.  in
106ea 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
106eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106ec 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65    /* Size of eve
106ed 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20  ry page in this 
106ee 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73  cache */.  int s
106ef 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20  zExtra;         
106f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106f1 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20  * Size of extra 
106f2 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70  space for each p
106f3 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75  age */.  int bPu
106f4 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
106f5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106f6 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
106f7 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
106f8 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
106f9 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
106fa 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43  dr*);       /* C
106fb 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20  all to try make 
106fc 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a  a page clean */.
106fd 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b    void *pStress;
106fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106ff 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
10700 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a  t to xStress */.
10701 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
10702 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
10703 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62        /* Pluggab
10704 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20  le cache module 
10705 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67  */.  PgHdr *pPag
10706 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  e1;             
10707 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65           /* Refe
10708 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
10709 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  */.};../*.** Som
1070a 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28  e of the assert(
1070b 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73  ) macros in this
1070c 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78   code are too ex
1070d 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a  pensive to run.*
1070e 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f  * even during no
1070f 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20  rmal debugging. 
10710 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72   Use them only r
10711 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75  arely on long-ru
10712 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20  nning.** tests. 
10713 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65   Enable the expe
10714 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73  nsive asserts us
10715 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c  ing the.** -DSQL
10716 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
10717 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f  SIVE_ASSERT=1 co
10718 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
10719 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  n..*/.#ifdef SQL
1071a 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
1071b 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65  SIVE_ASSERT.# de
1071c 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61  fine expensive_a
1071d 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74  ssert(X)  assert
1071e 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  (X).#else.# defi
1071f 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  ne expensive_ass
10720 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  ert(X).#endif../
10721 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10722 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10723 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d  ** Linked List M
10724 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a  anagement ******
10725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10726 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
10727 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64  EBUG) && defined
10728 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45  (SQLITE_ENABLE_E
10729 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29  XPENSIVE_ASSERT)
1072a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
1072b 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53  t the pCache->pS
1072c 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69  ynced variable i
1072d 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e  s set correctly.
1072e 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74   If it.** is not
1072f 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e  , either fail an
10730 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72   assert or retur
10731 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73  n zero. Otherwis
10732 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  e, return.** non
10733 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f  -zero. This is o
10734 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75  nly used in debu
10735 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73  gging builds, as
10736 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
10737 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
10738 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
10739 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b  ynced(pCache) );
1073a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1073b 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
1073c 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
1073d 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
1073e 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
1073f 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61  irtyTail; p!=pCa
10740 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d  che->pSynced; p=
10741 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a  p->pDirtyPrev){.
10742 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
10743 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73  Ref || (p->flags
10744 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
10745 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
10746 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52  n (p==0 || p->nR
10747 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26  ef || (p->flags&
10748 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
10749 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ==0);.}.#endif /
1074a 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c  * !NDEBUG && SQL
1074b 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e  ITE_ENABLE_EXPEN
1074c 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a  SIVE_ASSERT */..
1074d 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67  /*.** Remove pag
1074e 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65  e pPage from the
1074f 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
10750 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ages..*/.static 
10751 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76  void pcacheRemov
10752 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50  eFromDirtyList(P
10753 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
10754 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67  PCache *p = pPag
10755 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73  e->pCache;..  as
10756 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
10757 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65  rtyNext || pPage
10758 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20  ==p->pDirtyTail 
10759 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1075a 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c  ge->pDirtyPrev |
1075b 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  | pPage==p->pDir
1075c 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61  ty );..  /* Upda
1075d 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70  te the PCache1.p
1075e 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20  Synced variable 
1075f 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f  if necessary. */
10760 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65  .  if( p->pSynce
10761 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  d==pPage ){.    
10762 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d  PgHdr *pSynced =
10763 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10764 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  ev;.    while( p
10765 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63  Synced && (pSync
10766 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ed->flags&PGHDR_
10767 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20  NEED_SYNC) ){.  
10768 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53      pSynced = pS
10769 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65  ynced->pDirtyPre
1076a 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  v;.    }.    p->
1076b 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65  pSynced = pSynce
1076c 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  d;.  }..  if( pP
1076d 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20  age->pDirtyNext 
1076e 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  ){.    pPage->pD
1076f 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79  irtyNext->pDirty
10770 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44  Prev = pPage->pD
10771 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73  irtyPrev;.  }els
10772 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
10773 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54  Page==p->pDirtyT
10774 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44  ail );.    p->pD
10775 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65  irtyTail = pPage
10776 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20  ->pDirtyPrev;.  
10777 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70  }.  if( pPage->p
10778 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20  DirtyPrev ){.   
10779 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
1077a 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d  ev->pDirtyNext =
1077b 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65   pPage->pDirtyNe
1077c 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  xt;.  }else{.   
1077d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d   assert( pPage==
1077e 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20  p->pDirty );.   
1077f 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61   p->pDirty = pPa
10780 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a  ge->pDirtyNext;.
10781 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69    }.  pPage->pDi
10782 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  rtyNext = 0;.  p
10783 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76  Page->pDirtyPrev
10784 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69   = 0;..  expensi
10785 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68  ve_assert( pcach
10786 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20  eCheckSynced(p) 
10787 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  );.}../*.** Add 
10788 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68  page pPage to th
10789 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69  e head of the di
1078a 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65  rty list (PCache
1078b 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20  1.pDirty is set 
1078c 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f  to.** pPage)..*/
1078d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
1078e 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73  cheAddToDirtyLis
1078f 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b  t(PgHdr *pPage){
10790 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70  .  PCache *p = p
10791 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20  Page->pCache;.. 
10792 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10793 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26  pDirtyNext==0 &&
10794 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10795 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72  ev==0 && p->pDir
10796 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20  ty!=pPage );..  
10797 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
10798 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20  t = p->pDirty;. 
10799 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72   if( pPage->pDir
1079a 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73  tyNext ){.    as
1079b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
1079c 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50  rtyNext->pDirtyP
1079d 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50  rev==0 );.    pP
1079e 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d  age->pDirtyNext-
1079f 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50  >pDirtyPrev = pP
107a0 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44  age;.  }.  p->pD
107a1 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20  irty = pPage;.  
107a2 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61  if( !p->pDirtyTa
107a3 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69  il ){.    p->pDi
107a4 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b  rtyTail = pPage;
107a5 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70  .  }.  if( !p->p
107a6 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50  Synced && 0==(pP
107a7 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  age->flags&PGHDR
107a8 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20  _NEED_SYNC) ){. 
107a9 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20     p->pSynced = 
107aa 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70  pPage;.  }.  exp
107ab 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70  ensive_assert( p
107ac 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64  cacheCheckSynced
107ad 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (p) );.}../*.** 
107ae 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74  Wrapper around t
107af 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63  he pluggable cac
107b0 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f  hes xUnpin metho
107b1 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20  d. If the cache 
107b2 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64  is.** being used
107b3 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   for an in-memor
107b4 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73  y database, this
107b5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
107b6 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
107b7 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e  void pcacheUnpin
107b8 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43  (PgHdr *p){.  PC
107b9 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70  ache *pCache = p
107ba 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20  ->pCache;.  if( 
107bb 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
107bc 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  le ){.    if( p-
107bd 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20  >pgno==1 ){.    
107be 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31    pCache->pPage1
107bf 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
107c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
107c1 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69  fig.pcache.xUnpi
107c2 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  n(pCache->pCache
107c3 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a  , p, 0);.  }.}..
107c4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
107c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107c7 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74  **** General Int
107c8 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a  erfaces ******.*
107c9 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
107ca 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65  and shutdown the
107cb 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
107cc 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f  ystem. Neither o
107cd 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63  f these .** func
107ce 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64  tions are thread
107cf 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  safe..*/.SQLITE_
107d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
107d1 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c  te3PcacheInitial
107d2 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  ize(void){.  if(
107d3 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
107d4 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69  nfig.pcache.xIni
107d5 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
107d6 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61  te3PCacheSetDefa
107d7 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ult();.  }.  ret
107d8 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
107d9 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
107da 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62  Init(sqlite3Glob
107db 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
107dc 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pArg);.}.SQLITE_
107dd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
107de 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f  ite3PcacheShutdo
107df 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  wn(void){.  if( 
107e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
107e1 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74  fig.pcache.xShut
107e2 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  down ){.    sqli
107e3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
107e4 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e  pcache.xShutdown
107e5 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
107e6 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67  nfig.pcache.pArg
107e7 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
107e8 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
107e9 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43  in bytes of a PC
107ea 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ache object..*/.
107eb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
107ec 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
107ed 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75  Size(void){ retu
107ee 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  rn sizeof(PCache
107ef 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ); }../*.** Crea
107f0 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20  te a new PCache 
107f1 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20  object. Storage 
107f2 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
107f3 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20  e object.** has 
107f4 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c  already been all
107f5 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61  ocated and is pa
107f6 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70  ssed in as the p
107f7 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68   pointer. .** Th
107f8 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65  e caller discove
107f9 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  rs how much spac
107fa 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c  e needs to be al
107fb 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63  located by .** c
107fc 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63  alling sqlite3Pc
107fd 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53  acheSize()..*/.S
107fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107ff 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10800 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61  Open(.  int szPa
10801 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ge,             
10802 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
10803 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  every page */.  
10804 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20  int szExtra,    
10805 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10806 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f  Extra space asso
10807 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
10808 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62   page */.  int b
10809 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20  Purgeable,      
1080a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1080b 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20  if pages are on 
1080c 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f  backing store */
1080d 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73  .  int (*xStress
1080e 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c  )(void*,PgHdr*),
1080f 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74  /* Call to try t
10810 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65  o make pages cle
10811 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  an */.  void *pS
10812 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20  tress,          
10813 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
10814 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20   to xStress */. 
10815 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20   PCache *p      
10816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10817 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70   Preallocated sp
10818 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63  ace for the PCac
10819 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65  he */.){.  memse
1081a 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50  t(p, 0, sizeof(P
1081b 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a  Cache));.  p->sz
1081c 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20  Page = szPage;. 
1081d 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a   p->szExtra = sz
1081e 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72  Extra;.  p->bPur
1081f 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61  geable = bPurgea
10820 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73  ble;.  p->xStres
10821 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70  s = xStress;.  p
10822 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72  ->pStress = pStr
10823 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d  ess;.  p->nMax =
10824 20 31 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   100;.}../*.** C
10825 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73  hange the page s
10826 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f  ize for PCache o
10827 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65  bject. The calle
10828 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68  r must ensure th
10829 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  at there.** are 
1082a 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  no outstanding p
1082b 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77  age references w
1082c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
1082d 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
1082e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1082f 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10830 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61  eSetPageSize(PCa
10831 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74  che *pCache, int
10832 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65   szPage){.  asse
10833 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66  rt( pCache->nRef
10834 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70  ==0 && pCache->p
10835 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66  Dirty==0 );.  if
10836 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
10837 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
10838 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
10839 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63  he.xDestroy(pCac
1083a 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20  he->pCache);.   
1083b 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
1083c 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68  = 0;.  }.  pCach
1083d 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61  e->szPage = szPa
1083e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79  ge;.}../*.** Try
1083f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67   to obtain a pag
10840 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65  e from the cache
10841 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10842 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
10843 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43  cacheFetch(.  PC
10844 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20  ache *pCache,   
10845 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68      /* Obtain th
10846 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73  e page from this
10847 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
10848 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
10849 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
1084a 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20   to obtain */.  
1084b 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20  int createFlag, 
1084c 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
1084d 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66  , create page if
1084e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69   it does not exi
1084f 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20  st already */.  
10850 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20  PgHdr **ppPage  
10851 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
10852 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a  he page here */.
10853 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67  ){.  PgHdr *pPag
10854 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72  e = 0;.  int eCr
10855 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  eate;..  assert(
10856 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20   pCache!=0 );.  
10857 61 73 73 65 72 74 28 20 63 72 65 61 74 65 46 6c  assert( createFl
10858 61 67 3d 3d 31 20 7c 7c 20 63 72 65 61 74 65 46  ag==1 || createF
10859 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  lag==0 );.  asse
1085a 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20  rt( pgno>0 );.. 
1085b 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67   /* If the plugg
1085c 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69  able cache (sqli
1085d 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73  te3_pcache*) has
1085e 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61   not been alloca
1085f 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  ted,.  ** alloca
10860 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  te it now..  */.
10861 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70    if( !pCache->p
10862 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46  Cache && createF
10863 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  lag ){.    sqlit
10864 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20  e3_pcache *p;.  
10865 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20    int nByte;.   
10866 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d   nByte = pCache-
10867 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65  >szPage + pCache
10868 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65  ->szExtra + size
10869 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70  of(PgHdr);.    p
1086a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
1086b 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43  Config.pcache.xC
1086c 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61  reate(nByte, pCa
1086d 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29  che->bPurgeable)
1086e 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
1086f 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
10870 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
10871 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
10872 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10873 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43  xCachesize(p, pC
10874 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20  ache->nMax);.   
10875 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
10876 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65  = p;.  }..  eCre
10877 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67  ate = createFlag
10878 20 2a 20 28 31 20 2b 20 28 21 70 43 61 63 68 65   * (1 + (!pCache
10879 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20  ->bPurgeable || 
1087a 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29  !pCache->pDirty)
1087b 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  );.  if( pCache-
1087c 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70  >pCache ){.    p
1087d 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  Page = sqlite3Gl
1087e 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
1087f 65 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d  e.xFetch(pCache-
10880 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65  >pCache, pgno, e
10881 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20  Create);.  }..  
10882 69 66 28 20 21 70 50 61 67 65 20 26 26 20 65 43  if( !pPage && eC
10883 72 65 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20  reate==1 ){.    
10884 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20  PgHdr *pPg;..   
10885 20 2f 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79   /* Find a dirty
10886 20 70 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f   page to write-o
10887 75 74 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20  ut and recycle. 
10888 46 69 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e  First try to fin
10889 64 20 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65  d a .    ** page
1088a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72   that does not r
1088b 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c  equire a journal
1088c 2d 73 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20  -sync (one with 
1088d 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a  PGHDR_NEED_SYNC.
1088e 20 20 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c      ** cleared),
1088f 20 62 75 74 20 69 66 20 74 68 61 74 20 69 73 20   but if that is 
10890 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74  not possible set
10891 74 6c 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  tle for any othe
10892 72 20 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65  r .    ** unrefe
10893 72 65 6e 63 65 64 20 64 69 72 74 79 20 70 61 67  renced dirty pag
10894 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78  e..    */.    ex
10895 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
10896 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
10897 64 28 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20  d(pCache) );.   
10898 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d   for(pPg=pCache-
10899 3e 70 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20  >pSynced; .     
1089a 20 20 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e     pPg && (pPg->
1089b 6e 52 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c  nRef || (pPg->fl
1089c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
1089d 59 4e 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20  YNC)); .        
1089e 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50  pPg=pPg->pDirtyP
1089f 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  rev.    );.    i
108a0 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20  f( !pPg ){.     
108a1 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d   for(pPg=pCache-
108a2 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67  >pDirtyTail; pPg
108a3 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70   && pPg->nRef; p
108a4 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72  Pg=pPg->pDirtyPr
108a5 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ev);.    }.    i
108a6 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20  f( pPg ){.      
108a7 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63  int rc;.      rc
108a8 20 3d 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65   = pCache->xStre
108a9 73 73 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65  ss(pCache->pStre
108aa 73 73 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20  ss, pPg);.      
108ab 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
108ac 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  K && rc!=SQLITE_
108ad 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20  BUSY ){.        
108ae 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
108af 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50   }.    }..    pP
108b0 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  age = sqlite3Glo
108b1 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
108b2 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e  .xFetch(pCache->
108b3 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29  pCache, pgno, 2)
108b4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  ;.  }..  if( pPa
108b5 67 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  ge ){.    if( !p
108b6 50 61 67 65 2d 3e 70 44 61 74 61 20 29 7b 0a 20  Page->pData ){. 
108b7 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67       memset(pPag
108b8 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48  e, 0, sizeof(PgH
108b9 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a  dr) + pCache->sz
108ba 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70 50  Extra);.      pP
108bb 61 67 65 2d 3e 70 45 78 74 72 61 20 3d 20 28 76  age->pExtra = (v
108bc 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a  oid*)&pPage[1];.
108bd 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 61        pPage->pDa
108be 74 61 20 3d 20 28 76 6f 69 64 20 2a 29 26 28 28  ta = (void *)&((
108bf 63 68 61 72 20 2a 29 70 50 61 67 65 29 5b 73 69  char *)pPage)[si
108c0 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43  zeof(PgHdr) + pC
108c1 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 5d 3b 0a  ache->szExtra];.
108c2 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61        pPage->pCa
108c3 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20  che = pCache;.  
108c4 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20      pPage->pgno 
108c5 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  = pgno;.    }.  
108c6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
108c7 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20  >pCache==pCache 
108c8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
108c9 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f  Page->pgno==pgno
108ca 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
108cb 70 50 61 67 65 2d 3e 70 45 78 74 72 61 3d 3d 28  pPage->pExtra==(
108cc 76 6f 69 64 20 2a 29 26 70 50 61 67 65 5b 31 5d  void *)&pPage[1]
108cd 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d   );..    if( 0==
108ce 70 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20  pPage->nRef ){. 
108cf 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65       pCache->nRe
108d0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  f++;.    }.    p
108d1 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  Page->nRef++;.  
108d2 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b    if( pgno==1 ){
108d3 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70  .      pCache->p
108d4 50 61 67 65 31 20 3d 20 70 50 61 67 65 3b 0a 20  Page1 = pPage;. 
108d5 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70 50 61     }.  }.  *ppPa
108d6 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 72 65  ge = pPage;.  re
108d7 74 75 72 6e 20 28 70 50 61 67 65 3d 3d 30 20 26  turn (pPage==0 &
108d8 26 20 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c  & eCreate) ? SQL
108d9 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49  ITE_NOMEM : SQLI
108da 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
108db 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65  Decrement the re
108dc 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e  ference count on
108dd 20 61 20 70 61 67 65 2e 20 49 66 20 74 68 65 20   a page. If the 
108de 70 61 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e  page is clean an
108df 64 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e  d the.** referen
108e0 63 65 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74  ce count drops t
108e1 6f 20 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20  o 0, then it is 
108e2 6d 61 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20  made elible for 
108e3 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51  recycling..*/.SQ
108e4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
108e5 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  d sqlite3PcacheR
108e6 65 6c 65 61 73 65 28 50 67 48 64 72 20 2a 70 29  elease(PgHdr *p)
108e7 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  {.  assert( p->n
108e8 52 65 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52  Ref>0 );.  p->nR
108e9 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  ef--;.  if( p->n
108ea 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43  Ref==0 ){.    PC
108eb 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70  ache *pCache = p
108ec 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43  ->pCache;.    pC
108ed 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  ache->nRef--;.  
108ee 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26    if( (p->flags&
108ef 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20  PGHDR_DIRTY)==0 
108f0 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55  ){.      pcacheU
108f1 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c  npin(p);.    }el
108f2 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76  se{.      /* Mov
108f3 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  e the page to th
108f4 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69  e head of the di
108f5 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20  rty list. */.   
108f6 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46     pcacheRemoveF
108f7 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b  romDirtyList(p);
108f8 0a 20 20 20 20 20 20 70 63 61 63 68 65 41 64 64  .      pcacheAdd
108f9 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a  ToDirtyList(p);.
108fa 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
108fb 2a 2a 20 49 6e 63 72 65 61 73 65 20 74 68 65 20  ** Increase the 
108fc 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
108fd 6f 66 20 61 20 73 75 70 70 6c 69 65 64 20 70 61  of a supplied pa
108fe 67 65 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49  ge by 1..*/.SQLI
108ff 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10900 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
10901 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73  (PgHdr *p){.  as
10902 73 65 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b  sert(p->nRef>0);
10903 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a  .  p->nRef++;.}.
10904 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61  ./*.** Drop a pa
10905 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ge from the cach
10906 65 2e 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  e. There must be
10907 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66   exactly one ref
10908 65 72 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a  erence to the.**
10909 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63   page. This func
1090a 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61  tion deletes tha
1090b 74 20 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20  t reference, so 
1090c 61 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73  after it returns
1090d 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69   the.** page poi
1090e 6e 74 65 64 20 74 6f 20 62 79 20 70 20 69 73 20  nted to by p is 
1090f 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49  invalid..*/.SQLI
10910 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10911 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
10912 70 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50  p(PgHdr *p){.  P
10913 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20  Cache *pCache;. 
10914 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
10915 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ==1 );.  if( p->
10916 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
10917 59 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52  Y ){.    pcacheR
10918 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69  emoveFromDirtyLi
10919 73 74 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61  st(p);.  }.  pCa
1091a 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
1091b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d  .  pCache->nRef-
1091c 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f  -;.  if( p->pgno
1091d 3d 3d 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68  ==1 ){.    pCach
1091e 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  e->pPage1 = 0;. 
1091f 20 7d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62   }.  sqlite3Glob
10920 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10921 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70  xUnpin(pCache->p
10922 43 61 63 68 65 2c 20 70 2c 20 31 29 3b 0a 7d 0a  Cache, p, 1);.}.
10923 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
10924 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72   the page is mar
10925 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 49 66  ked as dirty. If
10926 20 69 74 20 69 73 6e 27 74 20 64 69 72 74 79 20   it isn't dirty 
10927 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65  already,.** make
10928 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54   it so..*/.SQLIT
10929 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1092a 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1092b 44 69 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b  Dirty(PgHdr *p){
1092c 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  .  p->flags &= ~
1092d 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
1092e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
1092f 52 65 66 3e 30 20 29 3b 0a 20 20 69 66 28 20 30  Ref>0 );.  if( 0
10930 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 47  ==(p->flags & PG
10931 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20  HDR_DIRTY) ){.  
10932 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47    p->flags |= PG
10933 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20 20 70  HDR_DIRTY;.    p
10934 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c  cacheAddToDirtyL
10935 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  ist( p);.  }.}..
10936 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
10937 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  the page is mark
10938 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 49 66 20  ed as clean. If 
10939 69 74 20 69 73 6e 27 74 20 63 6c 65 61 6e 20 61  it isn't clean a
1093a 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20  lready,.** make 
1093b 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45  it so..*/.SQLITE
1093c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1093d 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
1093e 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a  lean(PgHdr *p){.
1093f 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20    if( (p->flags 
10940 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29  & PGHDR_DIRTY) )
10941 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f  {.    pcacheRemo
10942 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28  veFromDirtyList(
10943 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73  p);.    p->flags
10944 20 26 3d 20 7e 28 50 47 48 44 52 5f 44 49 52 54   &= ~(PGHDR_DIRT
10945 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  Y|PGHDR_NEED_SYN
10946 43 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  C);.    if( p->n
10947 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ref==0 ){.      
10948 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a  pcacheUnpin(p);.
10949 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1094a 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79 20 70 61  ** Make every pa
1094b 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  ge in the cache 
1094c 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  clean..*/.SQLITE
1094d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1094e 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
1094f 41 6c 6c 28 50 43 61 63 68 65 20 2a 70 43 61 63  All(PCache *pCac
10950 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b  he){.  PgHdr *p;
10951 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20 70  .  while( (p = p
10952 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 21 3d  Cache->pDirty)!=
10953 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
10954 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
10955 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
10956 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 52   Clear the PGHDR
10957 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
10958 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20 70  from all dirty p
10959 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ages..*/.SQLITE_
1095a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1095b 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53  ite3PcacheClearS
1095c 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20  yncFlags(PCache 
1095d 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64  *pCache){.  PgHd
1095e 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43  r *p;.  for(p=pC
1095f 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b  ache->pDirty; p;
10960 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 74   p=p->pDirtyNext
10961 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ){.    p->flags 
10962 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53  &= ~PGHDR_NEED_S
10963 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68  YNC;.  }.  pCach
10964 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 43 61  e->pSynced = pCa
10965 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b  che->pDirtyTail;
10966 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
10967 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
10968 20 6f 66 20 70 61 67 65 20 70 20 74 6f 20 6e 65   of page p to ne
10969 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51 4c 49 54  wPgno. .*/.SQLIT
1096a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1096b 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
1096c 28 50 67 48 64 72 20 2a 70 2c 20 50 67 6e 6f 20  (PgHdr *p, Pgno 
1096d 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 43 61 63  newPgno){.  PCac
1096e 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e  he *pCache = p->
1096f 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
10970 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
10971 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 6e 6f   assert( newPgno
10972 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 47  >0 );.  sqlite3G
10973 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
10974 68 65 2e 78 52 65 6b 65 79 28 70 43 61 63 68 65  he.xRekey(pCache
10975 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 70 2d 3e  ->pCache, p, p->
10976 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f 29 3b 0a  pgno, newPgno);.
10977 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50    p->pgno = newP
10978 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66  gno;.  if( (p->f
10979 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
1097a 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 26 50  ) && (p->flags&P
1097b 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20  GHDR_NEED_SYNC) 
1097c 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d  ){.    pcacheRem
1097d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74  oveFromDirtyList
1097e 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 41  (p);.    pcacheA
1097f 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29  ddToDirtyList(p)
10980 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
10981 72 6f 70 20 65 76 65 72 79 20 63 61 63 68 65 20  rop every cache 
10982 65 6e 74 72 79 20 77 68 6f 73 65 20 70 61 67 65  entry whose page
10983 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 74   number is great
10984 65 72 20 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20  er than "pgno". 
10985 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75  The.** caller mu
10986 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  st ensure that t
10987 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
10988 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
10989 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 0a  es to any pages.
1098a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 70 61  ** other than pa
1098b 67 65 20 31 20 77 69 74 68 20 61 20 70 61 67 65  ge 1 with a page
1098c 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20   number greater 
1098d 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a  than pgno..**.**
1098e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 72   If there is a r
1098f 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65  eference to page
10990 20 31 20 61 6e 64 20 74 68 65 20 70 67 6e 6f 20   1 and the pgno 
10991 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64  parameter passed
10992 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63   to this.** func
10993 74 69 6f 6e 20 69 73 20 30 2c 20 74 68 65 6e 20  tion is 0, then 
10994 74 68 65 20 64 61 74 61 20 61 72 65 61 20 61 73  the data area as
10995 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61  sociated with pa
10996 67 65 20 31 20 69 73 20 7a 65 72 6f 65 64 2c 20  ge 1 is zeroed, 
10997 62 75 74 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  but.** the page 
10998 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 64 72  object is not dr
10999 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  opped..*/.SQLITE
1099a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1099b 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
1099c 61 74 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ate(PCache *pCac
1099d 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  he, Pgno pgno){.
1099e 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43    if( pCache->pC
1099f 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67 48 64  ache ){.    PgHd
109a0 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 64 72 20  r *p;.    PgHdr 
109a1 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 6f 72 28  *pNext;.    for(
109a2 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  p=pCache->pDirty
109a3 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20  ; p; p=pNext){. 
109a4 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e       pNext = p->
109a5 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 20 20  pDirtyNext;.    
109a6 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3e 70 67    if( p->pgno>pg
109a7 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  no ){.        as
109a8 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73 26 50  sert( p->flags&P
109a9 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20  GHDR_DIRTY );.  
109aa 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61        sqlite3Pca
109ab 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b  cheMakeClean(p);
109ac 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
109ad 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 26     if( pgno==0 &
109ae 26 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31  & pCache->pPage1
109af 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
109b0 28 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 2d  (pCache->pPage1-
109b1 3e 70 44 61 74 61 2c 20 30 2c 20 70 43 61 63 68  >pData, 0, pCach
109b2 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20  e->szPage);.    
109b3 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20    pgno = 1;.    
109b4 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
109b5 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
109b6 2e 78 54 72 75 6e 63 61 74 65 28 70 43 61 63 68  .xTruncate(pCach
109b7 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2b  e->pCache, pgno+
109b8 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  1);.  }.}../*.**
109b9 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0a   Close a cache..
109ba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
109bb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
109bc 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65  acheClose(PCache
109bd 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 66 28   *pCache){.  if(
109be 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
109bf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
109c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
109c1 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68  e.xDestroy(pCach
109c2 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a  e->pCache);.  }.
109c3 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73 63 61 72  }../* .** Discar
109c4 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
109c5 66 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a  f the cache..*/.
109c6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
109c7 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
109c8 65 43 6c 65 61 72 28 50 43 61 63 68 65 20 2a 70  eClear(PCache *p
109c9 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69 74 65  Cache){.  sqlite
109ca 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28  3PcacheTruncate(
109cb 70 43 61 63 68 65 2c 20 30 29 3b 0a 7d 0a 0a 2f  pCache, 0);.}../
109cc 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c  *.** Merge two l
109cd 69 73 74 73 20 6f 66 20 70 61 67 65 73 20 63 6f  ists of pages co
109ce 6e 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74  nnected by pDirt
109cf 79 20 61 6e 64 20 69 6e 20 70 67 6e 6f 20 6f 72  y and in pgno or
109d0 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 62  der..** Do not b
109d1 6f 74 68 20 66 69 78 69 6e 67 20 74 68 65 20 70  oth fixing the p
109d2 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 65  DirtyPrev pointe
109d3 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67  rs..*/.static Pg
109d4 48 64 72 20 2a 70 63 61 63 68 65 4d 65 72 67 65  Hdr *pcacheMerge
109d5 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20  DirtyList(PgHdr 
109d6 2a 70 41 2c 20 50 67 48 64 72 20 2a 70 42 29 7b  *pA, PgHdr *pB){
109d7 0a 20 20 50 67 48 64 72 20 72 65 73 75 6c 74 2c  .  PgHdr result,
109d8 20 2a 70 54 61 69 6c 3b 0a 20 20 70 54 61 69 6c   *pTail;.  pTail
109d9 20 3d 20 26 72 65 73 75 6c 74 3b 0a 20 20 77 68   = &result;.  wh
109da 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b  ile( pA && pB ){
109db 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 70 67 6e  .    if( pA->pgn
109dc 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  o<pB->pgno ){.  
109dd 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74      pTail->pDirt
109de 79 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 54  y = pA;.      pT
109df 61 69 6c 20 3d 20 70 41 3b 0a 20 20 20 20 20 20  ail = pA;.      
109e0 70 41 20 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b  pA = pA->pDirty;
109e1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
109e2 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20    pTail->pDirty 
109e3 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 54 61 69  = pB;.      pTai
109e4 6c 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42  l = pB;.      pB
109e5 20 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0a 20   = pB->pDirty;. 
109e6 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
109e7 41 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  A ){.    pTail->
109e8 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 7d  pDirty = pA;.  }
109e9 65 6c 73 65 20 69 66 28 20 70 42 20 29 7b 0a 20  else if( pB ){. 
109ea 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79     pTail->pDirty
109eb 20 3d 20 70 42 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = pB;.  }else{.
109ec 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74      pTail->pDirt
109ed 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
109ee 75 72 6e 20 72 65 73 75 6c 74 2e 70 44 69 72 74  urn result.pDirt
109ef 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74  y;.}../*.** Sort
109f0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67   the list of pag
109f1 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20  es in accending 
109f2 6f 72 64 65 72 20 62 79 20 70 67 6e 6f 2e 20 20  order by pgno.  
109f3 50 61 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 6e  Pages are.** con
109f4 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79  nected by pDirty
109f5 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20   pointers.  The 
109f6 70 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74  pDirtyPrev point
109f7 65 72 73 20 61 72 65 0a 2a 2a 20 63 6f 72 72 75  ers are.** corru
109f8 70 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72  pted by this sor
109f9 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74  t..**.** Since t
109fa 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d  here cannot be m
109fb 6f 72 65 20 74 68 61 6e 20 32 5e 33 31 20 64 69  ore than 2^31 di
109fc 73 74 69 6e 63 74 20 70 61 67 65 73 20 69 6e 20  stinct pages in 
109fd 61 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 74  a database,.** t
109fe 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d  here cannot be m
109ff 6f 72 65 20 74 68 61 6e 20 33 31 20 62 75 63 6b  ore than 31 buck
10a00 65 74 73 20 72 65 71 75 69 72 65 64 20 62 79 20  ets required by 
10a01 74 68 65 20 6d 65 72 67 65 20 73 6f 72 74 65 72  the merge sorter
10a02 2e 0a 2a 2a 20 4f 6e 65 20 65 78 74 72 61 20 62  ..** One extra b
10a03 75 63 6b 65 74 20 69 73 20 61 64 64 65 64 20 74  ucket is added t
10a04 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77  o catch overflow
10a05 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 74 68 69   in case somethi
10a06 6e 67 0a 2a 2a 20 65 76 65 72 20 63 68 61 6e 67  ng.** ever chang
10a07 65 73 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 70  es to make the p
10a08 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
10a09 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 23   incorrect..*/.#
10a0a 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55  define N_SORT_BU
10a0b 43 4b 45 54 20 20 33 32 0a 73 74 61 74 69 63 20  CKET  32.static 
10a0c 50 67 48 64 72 20 2a 70 63 61 63 68 65 53 6f 72  PgHdr *pcacheSor
10a0d 74 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72  tDirtyList(PgHdr
10a0e 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64 72 20   *pIn){.  PgHdr 
10a0f 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  *a[N_SORT_BUCKET
10a10 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a  ], *p;.  int i;.
10a11 20 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c 20 73    memset(a, 0, s
10a12 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20 77 68 69  izeof(a));.  whi
10a13 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70  le( pIn ){.    p
10a14 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20   = pIn;.    pIn 
10a15 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20  = p->pDirty;.   
10a16 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a   p->pDirty = 0;.
10a17 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57      for(i=0; ALW
10a18 41 59 53 28 69 3c 4e 5f 53 4f 52 54 5f 42 55 43  AYS(i<N_SORT_BUC
10a19 4b 45 54 2d 31 29 3b 20 69 2b 2b 29 7b 0a 20 20  KET-1); i++){.  
10a1a 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20      if( a[i]==0 
10a1b 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20  ){.        a[i] 
10a1c 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = p;.        bre
10a1d 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ak;.      }else{
10a1e 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61  .        p = pca
10a1f 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
10a20 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20  t(a[i], p);.    
10a21 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20      a[i] = 0;.  
10a22 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
10a23 69 66 28 20 4e 45 56 45 52 28 69 3d 3d 4e 5f 53  if( NEVER(i==N_S
10a24 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 20 29 7b  ORT_BUCKET-1) ){
10a25 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 67 65 74  .      /* To get
10a26 20 68 65 72 65 2c 20 74 68 65 72 65 20 6e 65 65   here, there nee
10a27 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f 53 4f 52  d to be 2^(N_SOR
10a28 54 5f 42 55 43 4b 45 54 29 20 65 6c 65 6d 65 6e  T_BUCKET) elemen
10a29 74 73 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74  ts in.      ** t
10a2a 68 65 20 69 6e 70 75 74 20 6c 69 73 74 2e 20 20  he input list.  
10a2b 42 75 74 20 74 68 61 74 20 69 73 20 69 6d 70 6f  But that is impo
10a2c 73 73 69 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f  ssible..      */
10a2d 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63  .      a[i] = pc
10a2e 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
10a2f 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20  st(a[i], p);.   
10a30 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30   }.  }.  p = a[0
10a31 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  ];.  for(i=1; i<
10a32 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69  N_SORT_BUCKET; i
10a33 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61  ++){.    p = pca
10a34 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
10a35 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a  t(p, a[i]);.  }.
10a36 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
10a37 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69  *.** Return a li
10a38 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20  st of all dirty 
10a39 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63  pages in the cac
10a3a 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61  he, sorted by pa
10a3b 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51  ge number..*/.SQ
10a3c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48  LITE_PRIVATE PgH
10a3d 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68  dr *sqlite3Pcach
10a3e 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68  eDirtyList(PCach
10a3f 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67  e *pCache){.  Pg
10a40 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  Hdr *p;.  for(p=
10a41 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
10a42 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65  p; p=p->pDirtyNe
10a43 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72  xt){.    p->pDir
10a44 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65  ty = p->pDirtyNe
10a45 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  xt;.  }.  return
10a46 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79   pcacheSortDirty
10a47 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69  List(pCache->pDi
10a48 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  rty);.}../* .** 
10a49 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
10a4a 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   number of refer
10a4b 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64  enced pages held
10a4c 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a   by the cache..*
10a4d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10a4e 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
10a4f 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68  heRefCount(PCach
10a50 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65  e *pCache){.  re
10a51 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65  turn pCache->nRe
10a52 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  f;.}../*.** Retu
10a53 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
10a54 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
10a55 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64  he page supplied
10a56 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
10a57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10a58 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
10a59 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74  achePageRefcount
10a5a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65  (PgHdr *p){.  re
10a5b 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a  turn p->nRef;.}.
10a5c 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
10a5d 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
10a5e 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10a5f 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
10a60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10a61 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f  ite3PcachePageco
10a62 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63  unt(PCache *pCac
10a63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65  he){.  int nPage
10a64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63   = 0;.  if( pCac
10a65 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20  he->pCache ){.  
10a66 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65    nPage = sqlite
10a67 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10a68 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28  ache.xPagecount(
10a69 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
10a6a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50  .  }.  return nP
10a6b 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  age;.}..#ifdef S
10a6c 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
10a6d 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74   Get the suggest
10a6e 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61  ed cache-size va
10a6f 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  lue..*/.SQLITE_P
10a70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10a71 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
10a72 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61  size(PCache *pCa
10a73 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  che){.  return p
10a74 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23  Cache->nMax;.}.#
10a75 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  endif../*.** Set
10a76 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
10a77 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e  ache-size value.
10a78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10a79 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10a7a 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a  cacheSetCachesiz
10a7b 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  e(PCache *pCache
10a7c 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
10a7d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20   pCache->nMax = 
10a7e 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43  mxPage;.  if( pC
10a7f 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
10a80 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
10a81 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
10a82 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65  Cachesize(pCache
10a83 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65  ->pCache, mxPage
10a84 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65  );.  }.}..#if de
10a85 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45  fined(SQLITE_CHE
10a86 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66  CK_PAGES) || def
10a87 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
10a88 47 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c  G)./*.** For all
10a89 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72   dirty pages cur
10a8a 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61  rently in the ca
10a8b 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  che, invoke the 
10a8c 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c  specified.** cal
10a8d 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f  lback. This is o
10a8e 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20  nly used if the 
10a8f 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
10a90 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64  ES macro is.** d
10a91 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
10a92 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10a93 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
10a94 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20  ateDirty(PCache 
10a95 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a  *pCache, void (*
10a96 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29  xIter)(PgHdr *))
10a97 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  {.  PgHdr *pDirt
10a98 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d  y;.  for(pDirty=
10a99 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
10a9a 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70  pDirty; pDirty=p
10a9b 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78  Dirty->pDirtyNex
10a9c 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44  t){.    xIter(pD
10a9d 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  irty);.  }.}.#en
10a9e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
10a9f 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63  **** End of pcac
10aa0 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.c ***********
10aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa3 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
10aa4 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
10aa5 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a  pcache1.c ******
10aa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10aa8 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
10aa9 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a  November 05.**.*
10aaa 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
10aab 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
10aac 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
10aad 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
10aae 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
10aaf 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
10ab0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
10ab1 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
10ab2 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
10ab3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
10ab4 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
10ab5 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
10ab6 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
10ab7 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
10ab8 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
10ab9 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
10aba 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
10abb 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
10abc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10abd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
10ac0 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
10ac1 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c  ments the defaul
10ac2 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  t page cache imp
10ac3 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65  lementation (the
10ac4 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63  .** sqlite3_pcac
10ac5 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49  he interface). I
10ac6 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  t also contains 
10ac7 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c  part of the impl
10ac8 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66  ementation.** of
10ac9 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
10aca 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64  IG_PAGECACHE and
10acb 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
10acc 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72  _memory() featur
10acd 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65  es..** If the de
10ace 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
10acf 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10ad0 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68  is overriden, th
10ad1 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a  en neither of.**
10ad2 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75   these two featu
10ad3 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  res are availabl
10ad4 65 2e 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20  e..*/...typedef 
10ad5 73 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50  struct PCache1 P
10ad6 43 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20  Cache1;.typedef 
10ad7 73 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67  struct PgHdr1 Pg
10ad8 48 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74  Hdr1;.typedef st
10ad9 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20  ruct PgFreeslot 
10ada 50 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20  PgFreeslot;../* 
10adb 50 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 75  Pointers to stru
10adc 63 74 75 72 65 73 20 6f 66 20 74 68 69 73 20 74  ctures of this t
10add 79 70 65 20 61 72 65 20 63 61 73 74 20 61 6e 64  ype are cast and
10ade 20 72 65 74 75 72 6e 65 64 20 61 73 20 0a 2a 2a   returned as .**
10adf 20 6f 70 61 71 75 65 20 73 71 6c 69 74 65 33 5f   opaque sqlite3_
10ae0 70 63 61 63 68 65 2a 20 68 61 6e 64 6c 65 73 0a  pcache* handles.
10ae1 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65  */.struct PCache
10ae2 31 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65 20 63  1 {.  /* Cache c
10ae3 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72  onfiguration par
10ae4 61 6d 65 74 65 72 73 2e 20 50 61 67 65 20 73 69  ameters. Page si
10ae5 7a 65 20 28 73 7a 50 61 67 65 29 20 61 6e 64 20  ze (szPage) and 
10ae6 74 68 65 20 70 75 72 67 65 61 62 6c 65 0a 20 20  the purgeable.  
10ae7 2a 2a 20 66 6c 61 67 20 28 62 50 75 72 67 65 61  ** flag (bPurgea
10ae8 62 6c 65 29 20 61 72 65 20 73 65 74 20 77 68 65  ble) are set whe
10ae9 6e 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63  n the cache is c
10aea 72 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d 61 79  reated. nMax may
10aeb 20 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69   be .  ** modifi
10aec 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62  ed at any time b
10aed 79 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20  y a call to the 
10aee 70 63 61 63 68 65 31 43 61 63 68 65 53 69 7a 65  pcache1CacheSize
10aef 28 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2a 20  () method..  ** 
10af0 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10af1 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10af2 65 6e 20 61 63 63 65 73 73 69 6e 67 20 6e 4d 61  en accessing nMa
10af3 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  x..  */.  int sz
10af4 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
10af5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10af6 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74   Size of allocat
10af7 65 64 20 70 61 67 65 73 20 69 6e 20 62 79 74 65  ed pages in byte
10af8 73 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67  s */.  int bPurg
10af9 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  eable;          
10afa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
10afb 75 65 20 69 66 20 63 61 63 68 65 20 69 73 20 70  ue if cache is p
10afc 75 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20 75 6e  urgeable */.  un
10afd 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 3b  signed int nMin;
10afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10aff 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d    /* Minimum num
10b00 62 65 72 20 6f 66 20 70 61 67 65 73 20 72 65 73  ber of pages res
10b01 65 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  erved */.  unsig
10b02 6e 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20  ned int nMax;   
10b03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b04 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 22 63 61  * Configured "ca
10b05 63 68 65 5f 73 69 7a 65 22 20 76 61 6c 75 65 20  che_size" value 
10b06 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20 74 61  */..  /* Hash ta
10b07 62 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67 65 73  ble of all pages
10b08 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
10b09 76 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6f 6e  variables may on
10b0a 6c 79 20 62 65 20 61 63 63 65 73 73 65 64 0a 20  ly be accessed. 
10b0b 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 61 63 63   ** when the acc
10b0c 65 73 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67  essor is holding
10b0d 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   the global mute
10b0e 78 20 28 73 65 65 20 70 63 61 63 68 65 31 45 6e  x (see pcache1En
10b0f 74 65 72 4d 75 74 65 78 28 29 20 0a 20 20 2a 2a  terMutex() .  **
10b10 20 61 6e 64 20 70 63 61 63 68 65 31 4c 65 61 76   and pcache1Leav
10b11 65 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a 2f 0a  eMutex())..  */.
10b12 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
10b13 52 65 63 79 63 6c 61 62 6c 65 3b 20 20 20 20 20  Recyclable;     
10b14 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10b15 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10b16 4c 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20 75 6e  LRU list */.  un
10b17 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65  signed int nPage
10b18 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b19 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
10b1a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70  r of pages in ap
10b1b 48 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  Hash */.  unsign
10b1c 65 64 20 69 6e 74 20 6e 48 61 73 68 3b 20 20 20  ed int nHash;   
10b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b1e 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
10b1f 20 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a 2f 0a   in apHash[] */.
10b20 20 20 50 67 48 64 72 31 20 2a 2a 61 70 48 61 73    PgHdr1 **apHas
10b21 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
10b22 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61        /* Hash ta
10b23 62 6c 65 20 66 6f 72 20 66 61 73 74 20 6c 6f 6f  ble for fast loo
10b24 6b 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a 0a 20  kup by key */.. 
10b25 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4d   unsigned int iM
10b26 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  axKey;          
10b27 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20       /* Largest 
10b28 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20 78  key seen since x
10b29 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 7d 3b  Truncate() */.};
10b2a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 61 63  ../*.** Each cac
10b2b 68 65 20 65 6e 74 72 79 20 69 73 20 72 65 70 72  he entry is repr
10b2c 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20 69 6e  esented by an in
10b2d 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
10b2e 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75  llowing .** stru
10b2f 63 74 75 72 65 2e 20 41 20 62 75 66 66 65 72 20  cture. A buffer 
10b30 6f 66 20 50 67 48 64 72 31 2e 70 43 61 63 68 65  of PgHdr1.pCache
10b31 2d 3e 73 7a 50 61 67 65 20 62 79 74 65 73 20 69  ->szPage bytes i
10b32 73 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20  s allocated .** 
10b33 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20  directly before 
10b34 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
10b35 6e 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68  n memory (see th
10b36 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45  e PGHDR1_TO_PAGE
10b37 28 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c  () .** macro bel
10b38 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  ow)..*/.struct P
10b39 67 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e  gHdr1 {.  unsign
10b3a 65 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20  ed int iKey;    
10b3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20           /* Key 
10b3c 76 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62  value (page numb
10b3d 65 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20  er) */.  PgHdr1 
10b3e 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
10b3f 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
10b40 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68  in hash table ch
10b41 61 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31  ain */.  PCache1
10b42 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
10b43 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
10b44 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20   that currently 
10b45 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a  owns this page *
10b46 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10b47 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
10b48 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52     /* Next in LR
10b49 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e  U list of unpinn
10b4a 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67  ed pages */.  Pg
10b4b 48 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20  Hdr1 *pLruPrev; 
10b4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b4d 50 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20  Previous in LRU 
10b4e 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64  list of unpinned
10b4f 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pages */.};../*
10b50 0a 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69  .** Free slots i
10b51 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20  n the allocator 
10b52 75 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75  used to divide u
10b53 70 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f  p the buffer pro
10b54 76 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74  vided using.** t
10b55 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
10b56 5f 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61  _PAGECACHE mecha
10b57 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  nism..*/.struct 
10b58 50 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50  PgFreeslot {.  P
10b59 67 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74  gFreeslot *pNext
10b5a 3b 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20  ;  /* Next free 
10b5b 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  slot */.};../*.*
10b5c 2a 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73  * Global data us
10b5d 65 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65  ed by this cache
10b5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49  ..*/.static SQLI
10b5f 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43  TE_WSD struct PC
10b60 61 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73  acheGlobal {.  s
10b61 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
10b62 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
10b63 20 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74     /* static mut
10b64 65 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  ex MUTEX_STATIC_
10b65 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d  LRU */..  int nM
10b66 61 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  axPage;         
10b67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b68 20 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65   Sum of nMaxPage
10b69 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63   for purgeable c
10b6a 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  aches */.  int n
10b6b 4d 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  MinPage;        
10b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b6d 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67  * Sum of nMinPag
10b6e 65 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20  e for purgeable 
10b6f 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20  caches */.  int 
10b70 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20  nCurrentPage;   
10b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b72 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72  /* Number of pur
10b73 67 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c  geable pages all
10b74 6f 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64  ocated */.  PgHd
10b75 72 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70  r1 *pLruHead, *p
10b76 4c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20  LruTail;        
10b77 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75  /* LRU list of u
10b78 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f  npinned pages */
10b79 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73  ..  /* Variables
10b7a 20 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49   related to SQLI
10b7b 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
10b7c 43 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f  CHE settings. */
10b7d 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20  .  int szSlot;  
10b7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b7f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
10b80 66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74  f each free slot
10b81 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61   */.  void *pSta
10b82 72 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20  rt, *pEnd;      
10b83 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75            /* Bou
10b84 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65  nds of pagecache
10b85 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f   malloc range */
10b86 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70  .  PgFreeslot *p
10b87 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  Free;           
10b88 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70         /* Free p
10b89 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20  age blocks */.  
10b8a 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20  int isInit;     
10b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b8c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
10b8d 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 7d 20  nitialized */.} 
10b8e 70 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a  pcache1_g;../*.*
10b8f 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68  * All code in th
10b90 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61  is file should a
10b91 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c  ccess the global
10b92 20 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65   structure above
10b93 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61   via the.** alia
10b94 73 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69  s "pcache1". Thi
10b95 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74  s ensures that t
10b96 68 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e  he WSD emulation
10b97 20 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a   is used when.**
10b98 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73   compiling for s
10b99 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e  ystems that do n
10b9a 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20  ot support real 
10b9b 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  WSD..*/.#define 
10b9c 70 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28  pcache1 (GLOBAL(
10b9d 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f  struct PCacheGlo
10b9e 62 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29  bal, pcache1_g))
10b9f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50  ../*.** When a P
10ba0 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20  gHdr1 structure 
10ba1 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68  is allocated, th
10ba2 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61  e associated PCa
10ba3 63 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62  che1.szPage.** b
10ba4 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
10ba5 20 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c   located directl
10ba6 79 20 62 65 66 6f 72 65 20 69 74 20 69 6e 20 6d  y before it in m
10ba7 65 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20  emory (i.e. the 
10ba8 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66  total.** size of
10ba9 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
10baa 69 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31  is sizeof(PgHdr1
10bab 29 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65  )+PCache1.szPage
10bac 20 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50   byte). The.** P
10bad 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20  GHDR1_TO_PAGE() 
10bae 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f  macro takes a po
10baf 69 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72  inter to a PgHdr
10bb0 31 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a  1 structure as.*
10bb1 2a 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e  * an argument an
10bb2 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
10bb3 74 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63  ter to the assoc
10bb4 69 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73  iated block of s
10bb5 7a 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20  zPage.** bytes. 
10bb6 54 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44  The PAGE_TO_PGHD
10bb7 52 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20  R1() macro does 
10bb8 74 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74  the opposite: it
10bb9 73 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a  s argument is.**
10bba 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
10bbb 62 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20  block of szPage 
10bbc 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e  bytes of data an
10bbd 64 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  d the return val
10bbe 75 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74  ue is.** a point
10bbf 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69  er to the associ
10bc0 61 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75  ated PgHdr1 stru
10bc1 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61  cture..**.**   a
10bc2 73 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f  ssert( PGHDR1_TO
10bc3 5f 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47  _PAGE(PAGE_TO_PG
10bc4 48 44 52 31 28 70 43 61 63 68 65 2c 20 58 29 29  HDR1(pCache, X))
10bc5 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e  ==X );.*/.#defin
10bc6 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45  e PGHDR1_TO_PAGE
10bc7 28 70 29 20 20 20 20 28 76 6f 69 64 2a 29 28 28  (p)    (void*)((
10bc8 28 63 68 61 72 2a 29 70 29 20 2d 20 70 2d 3e 70  (char*)p) - p->p
10bc9 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 0a 23  Cache->szPage).#
10bca 64 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f 50  define PAGE_TO_P
10bcb 47 48 44 52 31 28 63 2c 20 70 29 20 28 50 67 48  GHDR1(c, p) (PgH
10bcc 64 72 31 2a 29 28 28 28 63 68 61 72 2a 29 70 29  dr1*)(((char*)p)
10bcd 20 2b 20 63 2d 3e 73 7a 50 61 67 65 29 0a 0a 2f   + c->szPage)../
10bce 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65  *.** Macros to e
10bcf 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74  nter and leave t
10bd0 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d 75  he global LRU mu
10bd1 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  tex..*/.#define 
10bd2 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10bd3 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x() sqlite3_mute
10bd4 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e  x_enter(pcache1.
10bd5 6d 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70  mutex).#define p
10bd6 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10bd7 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  () sqlite3_mutex
10bd8 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d  _leave(pcache1.m
10bd9 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  utex)../********
10bda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bde 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
10bdf 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f  * Page Allocatio
10be0 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  n/SQLITE_CONFIG_
10be1 50 43 41 43 48 45 20 52 65 6c 61 74 65 64 20 46  PCACHE Related F
10be2 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
10be3 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
10be4 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10be5 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69   called during i
10be6 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 66  nitialization if
10be7 20 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72   a static buffer
10be8 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65 64   is .** supplied
10be9 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20   to use for the 
10bea 70 61 67 65 2d 63 61 63 68 65 20 62 79 20 70 61  page-cache by pa
10beb 73 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45  ssing the SQLITE
10bec 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
10bed 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71 6c  E.** verb to sql
10bee 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 50  ite3_config(). P
10bef 61 72 61 6d 65 74 65 72 20 70 42 75 66 20 70 6f  arameter pBuf po
10bf0 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63  ints to an alloc
10bf1 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20 65  ation large.** e
10bf2 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69 6e  nough to contain
10bf3 20 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66 20   'n' buffers of 
10bf4 27 73 7a 27 20 62 79 74 65 73 20 65 61 63 68 2e  'sz' bytes each.
10bf5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10bf6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10bf7 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70  CacheBufferSetup
10bf8 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74  (void *pBuf, int
10bf9 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69   sz, int n){.  i
10bfa 66 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69  f( pcache1.isIni
10bfb 74 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73  t ){.    PgFrees
10bfc 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d  lot *p;.    sz =
10bfd 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b   ROUNDDOWN8(sz);
10bfe 0a 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53  .    pcache1.szS
10bff 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63  lot = sz;.    pc
10c00 61 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70  ache1.pStart = p
10c01 42 75 66 3b 0a 20 20 20 20 70 63 61 63 68 65 31  Buf;.    pcache1
10c02 2e 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  .pFree = 0;.    
10c03 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20  while( n-- ){.  
10c04 20 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73      p = (PgFrees
10c05 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 20  lot*)pBuf;.     
10c06 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63   p->pNext = pcac
10c07 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 20  he1.pFree;.     
10c08 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d   pcache1.pFree =
10c09 20 70 3b 0a 20 20 20 20 20 20 70 42 75 66 20 3d   p;.      pBuf =
10c0a 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a   (void*)&((char*
10c0b 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 20 20  )pBuf)[sz];.    
10c0c 7d 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 45  }.    pcache1.pE
10c0d 6e 64 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 7d  nd = pBuf;.  }.}
10c0e 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66  ../*.** Malloc f
10c0f 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 69 74  unction used wit
10c10 68 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 6f  hin this file to
10c11 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   allocate space 
10c12 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0a  from the buffer.
10c13 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  ** configured us
10c14 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ing sqlite3_conf
10c15 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
10c16 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69  _PAGECACHE) opti
10c17 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75  on. If no .** su
10c18 63 68 20 62 75 66 66 65 72 20 65 78 69 73 74 73  ch buffer exists
10c19 20 6f 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20   or there is no 
10c1a 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 69 74  space left in it
10c1b 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
10c1c 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74  falls .** back t
10c1d 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  o sqlite3Malloc(
10c1e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10c1f 64 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 28  d *pcache1Alloc(
10c20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f  int nByte){.  vo
10c21 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  id *p;.  assert(
10c22 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
10c23 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65  eld(pcache1.mute
10c24 78 29 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  x) );.  if( nByt
10c25 65 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f  e<=pcache1.szSlo
10c26 74 20 26 26 20 70 63 61 63 68 65 31 2e 70 46 72  t && pcache1.pFr
10c27 65 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ee ){.    assert
10c28 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74  ( pcache1.isInit
10c29 20 29 3b 0a 20 20 20 20 70 20 3d 20 28 50 67 48   );.    p = (PgH
10c2a 64 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46  dr1 *)pcache1.pF
10c2b 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31  ree;.    pcache1
10c2c 2e 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31  .pFree = pcache1
10c2d 2e 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20  .pFree->pNext;. 
10c2e 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
10c2f 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
10c30 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45  S_PAGECACHE_SIZE
10c31 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71  , nByte);.    sq
10c32 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
10c33 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
10c34 45 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b  ECACHE_USED, 1);
10c35 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f  .  }else{..    /
10c36 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
10c37 20 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71   buffer using sq
10c38 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66  lite3Malloc. Bef
10c39 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78  ore doing so, ex
10c3a 69 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c  it the.    ** gl
10c3b 6f 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65  obal pcache mute
10c3c 78 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65  x and unlock the
10c3d 20 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a   pager-cache obj
10c3e 65 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73  ect pCache. This
10c3f 20 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74   is .    ** so t
10c40 68 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d  hat if the attem
10c41 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  pt to allocate a
10c42 20 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73   new buffer caus
10c43 65 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20  es the the .    
10c44 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f  ** configured so
10c45 66 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f  ft-heap-limit to
10c46 20 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74   be breached, it
10c47 20 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c   will be possibl
10c48 65 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c  e to.    ** recl
10c49 61 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20  aim memory from 
10c4a 74 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65  this pager-cache
10c4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61  ..    */.    pca
10c4c 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10c4d 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
10c4e 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a  3Malloc(nByte);.
10c4f 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72      pcache1Enter
10c50 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28  Mutex();.    if(
10c51 20 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20   p ){.      int 
10c52 73 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  sz = sqlite3Mall
10c53 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20  ocSize(p);.     
10c54 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
10c55 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
10c56 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c  PAGECACHE_OVERFL
10c57 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20  OW, sz);.    }. 
10c58 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
10c59 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20  ../*.** Free an 
10c5a 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
10c5b 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70   obtained from p
10c5c 63 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a  cache1Alloc()..*
10c5d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10c5e 61 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a  ache1Free(void *
10c5f 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  p){.  assert( sq
10c60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
10c61 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20  (pcache1.mutex) 
10c62 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  );.  if( p==0 ) 
10c63 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e  return;.  if( p>
10c64 3d 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20  =pcache1.pStart 
10c65 26 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e  && p<pcache1.pEn
10c66 64 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73  d ){.    PgFrees
10c67 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20  lot *pSlot;.    
10c68 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
10c69 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
10c6a 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d  AGECACHE_USED, -
10c6b 31 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20  1);.    pSlot = 
10c6c 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a  (PgFreeslot*)p;.
10c6d 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74      pSlot->pNext
10c6e 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65   = pcache1.pFree
10c6f 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46  ;.    pcache1.pF
10c70 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d  ree = pSlot;.  }
10c71 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53  else{.    int iS
10c72 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ize = sqlite3Mal
10c73 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  locSize(p);.    
10c74 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
10c75 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
10c76 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f  AGECACHE_OVERFLO
10c77 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20  W, -iSize);.    
10c78 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
10c79 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
10c7a 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67  locate a new pag
10c7b 65 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c  e object initial
10c7c 6c 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ly associated wi
10c7d 74 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e  th cache pCache.
10c7e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72  .*/.static PgHdr
10c7f 31 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50  1 *pcache1AllocP
10c80 61 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61  age(PCache1 *pCa
10c81 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  che){.  int nByt
10c82 65 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72  e = sizeof(PgHdr
10c83 31 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50  1) + pCache->szP
10c84 61 67 65 3b 0a 20 20 76 6f 69 64 20 2a 70 50 67  age;.  void *pPg
10c85 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28   = pcache1Alloc(
10c86 6e 42 79 74 65 29 3b 0a 20 20 50 67 48 64 72 31  nByte);.  PgHdr1
10c87 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 67 20 29   *p;.  if( pPg )
10c88 7b 0a 20 20 20 20 70 20 3d 20 50 41 47 45 5f 54  {.    p = PAGE_T
10c89 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10c8a 20 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 70   pPg);.    if( p
10c8b 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
10c8c 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68  e ){.      pcach
10c8d 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b  e1.nCurrentPage+
10c8e 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  +;.    }.  }else
10c8f 7b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d  {.    p = 0;.  }
10c90 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
10c91 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 67  /*.** Free a pag
10c92 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 74  e object allocat
10c93 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c 6c  ed by pcache1All
10c94 6f 63 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  ocPage()..**.** 
10c95 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61  The pointer is a
10c96 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 4e 55 4c  llowed to be NUL
10c97 4c 2c 20 77 68 69 63 68 20 69 73 20 70 72 75 64  L, which is prud
10c98 65 6e 74 2e 20 20 42 75 74 20 69 74 20 74 75 72  ent.  But it tur
10c99 6e 73 20 6f 75 74 0a 2a 2a 20 74 68 61 74 20 74  ns out.** that t
10c9a 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65  he current imple
10c9b 6d 65 6e 74 61 74 69 6f 6e 20 68 61 70 70 65 6e  mentation happen
10c9c 73 20 74 6f 20 6e 65 76 65 72 20 63 61 6c 6c 20  s to never call 
10c9d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
10c9e 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  with a NULL poin
10c9f 74 65 72 2c 20 73 6f 20 77 65 20 6d 61 72 6b 20  ter, so we mark 
10ca0 74 68 65 20 4e 55 4c 4c 20 74 65 73 74 20 77 69  the NULL test wi
10ca1 74 68 20 41 4c 57 41 59 53 28 29 2e 0a 2a 2f 0a  th ALWAYS()..*/.
10ca2 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10ca3 68 65 31 46 72 65 65 50 61 67 65 28 50 67 48 64  he1FreePage(PgHd
10ca4 72 31 20 2a 70 29 7b 0a 20 20 69 66 28 20 41 4c  r1 *p){.  if( AL
10ca5 57 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69  WAYS(p) ){.    i
10ca6 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50  f( p->pCache->bP
10ca7 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20  urgeable ){.    
10ca8 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65    pcache1.nCurre
10ca9 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a  ntPage--;.    }.
10caa 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28      pcache1Free(
10cab 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70  PGHDR1_TO_PAGE(p
10cac 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
10cad 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e   Malloc function
10cae 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
10caf 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20  to obtain space 
10cb0 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
10cb1 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73  configured.** us
10cb2 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ing sqlite3_conf
10cb3 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
10cb4 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69  _PAGECACHE) opti
10cb5 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62  on. If no such b
10cb6 75 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c  uffer.** exists,
10cb7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
10cb8 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c  alls back to sql
10cb9 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  ite3Malloc()..*/
10cba 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10cbb 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
10cbc 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b  eMalloc(int sz){
10cbd 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63  .  void *p;.  pc
10cbe 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10cbf 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31  );.  p = pcache1
10cc0 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61  Alloc(sz);.  pca
10cc1 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10cc2 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
10cc3 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61  ./*.** Free an a
10cc4 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
10cc5 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
10cc6 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
10cc7 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
10cc8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
10cc9 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a  3PageFree(void *
10cca 70 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  p){.  pcache1Ent
10ccb 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
10ccc 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70  che1Free(p);.  p
10ccd 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10cce 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ();.}../********
10ccf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cd3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
10cd4 2a 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d  * General Implem
10cd5 65 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f  entation Functio
10cd6 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ns *************
10cd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cd8 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
10cd9 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10cda 20 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20   used to resize 
10cdb 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75  the hash table u
10cdc 73 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  sed by the cache
10cdd 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68   passed.** as th
10cde 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
10cdf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62  ..**.** The glob
10ce0 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  al mutex must be
10ce1 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20   held when this 
10ce2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
10ce3 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
10ce4 74 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48  t pcache1ResizeH
10ce5 61 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b  ash(PCache1 *p){
10ce6 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65  .  PgHdr1 **apNe
10ce7 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  w;.  unsigned in
10ce8 74 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e  t nNew;.  unsign
10ce9 65 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73  ed int i;..  ass
10cea 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10ceb 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10cec 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65  mutex) );..  nNe
10ced 77 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a  w = p->nHash*2;.
10cee 20 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29    if( nNew<256 )
10cef 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36  {.    nNew = 256
10cf0 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31  ;.  }..  pcache1
10cf1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10cf2 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20  if( p->nHash ){ 
10cf3 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
10cf4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
10cf5 61 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20  apNew = (PgHdr1 
10cf6 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
10cf7 63 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20  c(sizeof(PgHdr1 
10cf8 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20  *)*nNew);.  if( 
10cf9 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69  p->nHash ){ sqli
10cfa 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
10cfb 6f 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65  oc(); }.  pcache
10cfc 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10cfd 20 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20   if( apNew ){.  
10cfe 20 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20    memset(apNew, 
10cff 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31  0, sizeof(PgHdr1
10d00 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66   *)*nNew);.    f
10d01 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61  or(i=0; i<p->nHa
10d02 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  sh; i++){.      
10d03 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20  PgHdr1 *pPage;. 
10d04 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65       PgHdr1 *pNe
10d05 78 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69  xt = p->apHash[i
10d06 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ];.      while( 
10d07 28 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21  (pPage = pNext)!
10d08 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e  =0 ){.        un
10d09 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70  signed int h = p
10d0a 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65  Page->iKey % nNe
10d0b 77 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74  w;.        pNext
10d0c 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b   = pPage->pNext;
10d0d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e  .        pPage->
10d0e 70 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d  pNext = apNew[h]
10d0f 3b 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b  ;.        apNew[
10d10 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20  h] = pPage;.    
10d11 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
10d12 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70  lite3_free(p->ap
10d13 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70  Hash);.    p->ap
10d14 48 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20  Hash = apNew;.  
10d15 20 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65    p->nHash = nNe
10d16 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  w;.  }..  return
10d17 20 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51   (p->apHash ? SQ
10d18 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
10d19 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NOMEM);.}../*.*
10d1a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
10d1b 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
10d1c 6c 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65  ly to remove the
10d1d 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d   page pPage from
10d1e 20 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20   the .** global 
10d1f 4c 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20  LRU list, if is 
10d20 70 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70  part of it. If p
10d21 50 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74  Page is not part
10d22 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a   of the global.*
10d23 2a 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e  * LRU list, then
10d24 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10d25 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
10d26 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   The global mute
10d27 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
10d28 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
10d29 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
10d2a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10d2b 68 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72  he1PinPage(PgHdr
10d2c 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73  1 *pPage){.  ass
10d2d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10d2e 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10d2f 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
10d30 70 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d  pPage && (pPage-
10d31 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61  >pLruNext || pPa
10d32 67 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75  ge==pcache1.pLru
10d33 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28  Tail) ){.    if(
10d34 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76   pPage->pLruPrev
10d35 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d   ){.      pPage-
10d36 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e  >pLruPrev->pLruN
10d37 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ext = pPage->pLr
10d38 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  uNext;.    }.   
10d39 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75   if( pPage->pLru
10d3a 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50  Next ){.      pP
10d3b 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70  age->pLruNext->p
10d3c 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d  LruPrev = pPage-
10d3d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d  >pLruPrev;.    }
10d3e 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31  .    if( pcache1
10d3f 2e 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65  .pLruHead==pPage
10d40 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
10d41 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61  1.pLruHead = pPa
10d42 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20  ge->pLruNext;.  
10d43 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63    }.    if( pcac
10d44 68 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50  he1.pLruTail==pP
10d45 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61  age ){.      pca
10d46 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20  che1.pLruTail = 
10d47 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
10d48 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
10d49 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a  ->pLruNext = 0;.
10d4a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50      pPage->pLruP
10d4b 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  rev = 0;.    pPa
10d4c 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63  ge->pCache->nRec
10d4d 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d  yclable--;.  }.}
10d4e 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  .../*.** Remove 
10d4f 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65  the page supplie
10d50 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
10d51 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
10d52 61 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65  able .** (PCache
10d53 31 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75  1.apHash structu
10d54 72 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63  re) that it is c
10d55 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
10d56 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c  in..**.** The gl
10d57 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20  obal mutex must 
10d58 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69  be held when thi
10d59 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
10d5a 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lled..*/.static 
10d5b 76 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f  void pcache1Remo
10d5c 76 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72  veFromHash(PgHdr
10d5d 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73  1 *pPage){.  uns
10d5e 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50  igned int h;.  P
10d5f 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10d60 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a   pPage->pCache;.
10d61 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a    PgHdr1 **pp;..
10d62 20 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65    h = pPage->iKe
10d63 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73  y % pCache->nHas
10d64 68 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61  h;.  for(pp=&pCa
10d65 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20  che->apHash[h]; 
10d66 28 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70  (*pp)!=pPage; pp
10d67 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b  =&(*pp)->pNext);
10d68 0a 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e  .  *pp = (*pp)->
10d69 70 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65  pNext;..  pCache
10d6a 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a  ->nPage--;.}../*
10d6b 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
10d6c 20 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20   currently more 
10d6d 74 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78  than pcache.nMax
10d6e 50 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63  Page pages alloc
10d6f 61 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20  ated, try.** to 
10d70 72 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f  recycle pages to
10d71 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62   reduce the numb
10d72 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  er allocated to 
10d73 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e  pcache.nMaxPage.
10d74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10d75 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
10d76 78 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61  xPage(void){.  a
10d77 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
10d78 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
10d79 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68  1.mutex) );.  wh
10d7a 69 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75  ile( pcache1.nCu
10d7b 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65  rrentPage>pcache
10d7c 31 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63  1.nMaxPage && pc
10d7d 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29  ache1.pLruTail )
10d7e 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20  {.    PgHdr1 *p 
10d7f 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  = pcache1.pLruTa
10d80 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50  il;.    pcache1P
10d81 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70  inPage(p);.    p
10d82 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
10d83 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61  Hash(p);.    pca
10d84 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b  che1FreePage(p);
10d85 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69  .  }.}../*.** Di
10d86 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20  scard all pages 
10d87 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68  from cache pCach
10d88 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75  e with a page nu
10d89 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29  mber (key value)
10d8a 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61   .** greater tha
10d8b 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c  n or equal to iL
10d8c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64  imit. Any pinned
10d8d 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74   pages that meet
10d8e 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72   this .** criter
10d8f 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20  ia are unpinned 
10d90 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
10d91 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a  discarded..**.**
10d92 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65   The global mute
10d93 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
10d94 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
10d95 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
10d96 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10d97 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66  he1TruncateUnsaf
10d98 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  e(.  PCache1 *pC
10d99 61 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65  ache, .  unsigne
10d9a 64 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b  d int iLimit .){
10d9b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 75 6e 73  .  TESTONLY( uns
10d9c 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 20  igned int nPage 
10d9d 3d 20 30 3b 20 29 20 20 20 20 20 20 2f 2a 20 55  = 0; )      /* U
10d9e 73 65 64 20 74 6f 20 61 73 73 65 72 74 20 70 43  sed to assert pC
10d9f 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20 63  ache->nPage is c
10da0 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73 69  orrect */.  unsi
10da1 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61 73  gned int h;.  as
10da2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
10da3 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31  tex_held(pcache1
10da4 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  .mutex) );.  for
10da5 28 68 3d 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e  (h=0; h<pCache->
10da6 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20  nHash; h++){.   
10da7 20 50 67 48 64 72 31 20 2a 2a 70 70 20 3d 20 26   PgHdr1 **pp = &
10da8 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
10da9 5d 3b 20 0a 20 20 20 20 50 67 48 64 72 31 20 2a  ]; .    PgHdr1 *
10daa 70 50 61 67 65 3b 0a 20 20 20 20 77 68 69 6c 65  pPage;.    while
10dab 28 20 28 70 50 61 67 65 20 3d 20 2a 70 70 29 21  ( (pPage = *pp)!
10dac 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
10dad 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69  pPage->iKey>=iLi
10dae 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  mit ){.        p
10daf 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a  Cache->nPage--;.
10db0 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50          *pp = pP
10db1 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  age->pNext;.    
10db2 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
10db3 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
10db4 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
10db5 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
10db6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
10db7 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65  pp = &pPage->pNe
10db8 78 74 3b 0a 20 20 20 20 20 20 20 20 54 45 53 54  xt;.        TEST
10db9 4f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 3b 20 29  ONLY( nPage++; )
10dba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
10dbb 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61   }.  assert( pCa
10dbc 63 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50 61 67  che->nPage==nPag
10dbd 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  e );.}../*******
10dbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc2 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
10dc3 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ** sqlite3_pcach
10dc4 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  e Methods ******
10dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dc7 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
10dc8 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
10dc9 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
10dca 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68  cache.xInit meth
10dcb 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
10dcc 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f  t pcache1Init(vo
10dcd 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
10dce 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10dcf 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73  (NotUsed);.  ass
10dd0 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49  ert( pcache1.isI
10dd1 6e 69 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73  nit==0 );.  mems
10dd2 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20  et(&pcache1, 0, 
10dd3 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29  sizeof(pcache1))
10dd4 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
10dd5 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
10dd6 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63  eMutex ){.    pc
10dd7 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71  ache1.mutex = sq
10dd8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
10dd9 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
10dda 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a  TATIC_LRU);.  }.
10ddb 20 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74    pcache1.isInit
10ddc 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 1;.  return S
10ddd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
10dde 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10ddf 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10de0 5f 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77  _pcache.xShutdow
10de1 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74  n method..** Not
10de2 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 69  e that the stati
10de3 63 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 65  c mutex allocate
10de4 64 20 69 6e 20 78 49 6e 69 74 20 64 6f 65 73 20  d in xInit does 
10de5 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  .** not need to 
10de6 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61  be freed..*/.sta
10de7 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
10de8 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e  Shutdown(void *N
10de9 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
10dea 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
10deb 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20  sed);.  assert( 
10dec 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 21 3d  pcache1.isInit!=
10ded 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70  0 );.  memset(&p
10dee 63 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f  cache1, 0, sizeo
10def 66 28 70 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a  f(pcache1));.}..
10df0 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10df1 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10df2 74 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65 61  te3_pcache.xCrea
10df3 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a  te method..**.**
10df4 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
10df5 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cache..*/.static
10df6 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
10df7 2a 70 63 61 63 68 65 31 43 72 65 61 74 65 28 69  *pcache1Create(i
10df8 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62  nt szPage, int b
10df9 50 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50 43  Purgeable){.  PC
10dfa 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a  ache1 *pCache;..
10dfb 20 20 70 43 61 63 68 65 20 3d 20 28 50 43 61 63    pCache = (PCac
10dfc 68 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  he1 *)sqlite3_ma
10dfd 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61 63  lloc(sizeof(PCac
10dfe 68 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43 61  he1));.  if( pCa
10dff 63 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  che ){.    memse
10e00 74 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69 7a  t(pCache, 0, siz
10e01 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20  eof(PCache1));. 
10e02 20 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67     pCache->szPag
10e03 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20  e = szPage;.    
10e04 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10e05 6c 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c 65  le = (bPurgeable
10e06 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 69   ? 1 : 0);.    i
10e07 66 28 20 62 50 75 72 67 65 61 62 6c 65 20 29 7b  f( bPurgeable ){
10e08 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  .      pCache->n
10e09 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20 20  Min = 10;.      
10e0a 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10e0b 78 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68  x();.      pcach
10e0c 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70  e1.nMinPage += p
10e0d 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20  Cache->nMin;.   
10e0e 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
10e0f 75 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20  utex();.    }.  
10e10 7d 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69  }.  return (sqli
10e11 74 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43 61  te3_pcache *)pCa
10e12 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  che;.}../*.** Im
10e13 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
10e14 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
10e15 68 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d 65  he.xCachesize me
10e16 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e  thod. .**.** Con
10e17 66 69 67 75 72 65 20 74 68 65 20 63 61 63 68 65  figure the cache
10e18 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20  _size limit for 
10e19 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74  a cache..*/.stat
10e1a 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 43  ic void pcache1C
10e1b 61 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 33  achesize(sqlite3
10e1c 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 20  _pcache *p, int 
10e1d 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65 31  nMax){.  PCache1
10e1e 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10e1f 68 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20 70  he1 *)p;.  if( p
10e20 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
10e21 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31  e ){.    pcache1
10e22 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
10e23 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61    pcache1.nMaxPa
10e24 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43  ge += (nMax - pC
10e25 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20  ache->nMax);.   
10e26 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20   pCache->nMax = 
10e27 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65  nMax;.    pcache
10e28 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28  1EnforceMaxPage(
10e29 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65  );.    pcache1Le
10e2a 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
10e2b 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10e2c 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10e2d 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 50  qlite3_pcache.xP
10e2e 61 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e  agecount method.
10e2f 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
10e30 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74  pcache1Pagecount
10e31 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20  (sqlite3_pcache 
10e32 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  *p){.  int n;.  
10e33 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10e34 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61  x();.  n = ((PCa
10e35 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65  che1 *)p)->nPage
10e36 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ;.  pcache1Leave
10e37 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72  Mutex();.  retur
10e38 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  n n;.}../*.** Im
10e39 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
10e3a 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
10e3b 68 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f 64  he.xFetch method
10e3c 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61  . .**.** Fetch a
10e3d 20 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 6c   page by key val
10e3e 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ue..**.** Whethe
10e3f 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20 70  r or not a new p
10e40 61 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  age may be alloc
10e41 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ated by this fun
10e42 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e  ction depends on
10e43 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
10e44 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20   the createFlag 
10e45 61 72 67 75 6d 65 6e 74 2e 20 20 30 20 6d 65 61  argument.  0 mea
10e46 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61  ns do not alloca
10e47 74 65 20 61 20 6e 65 77 0a 2a 2a 20 70 61 67 65  te a new.** page
10e48 2e 20 20 31 20 6d 65 61 6e 73 20 61 6c 6c 6f 63  .  1 means alloc
10e49 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 69  ate a new page i
10e4a 66 20 73 70 61 63 65 20 69 73 20 65 61 73 69 6c  f space is easil
10e4b 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 32 20  y available.  2 
10e4c 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 74 72 79  .** means to try
10e4d 20 72 65 61 6c 6c 79 20 68 61 72 64 20 74 6f 20   really hard to 
10e4e 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
10e4f 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  age..**.** For a
10e50 20 6e 6f 6e 2d 70 75 72 67 65 61 62 6c 65 20 63   non-purgeable c
10e51 61 63 68 65 20 28 61 20 63 61 63 68 65 20 75 73  ache (a cache us
10e52 65 64 20 61 73 20 74 68 65 20 73 74 6f 72 61 67  ed as the storag
10e53 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
10e54 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 20  ry.** database) 
10e55 74 68 65 72 65 20 69 73 20 72 65 61 6c 6c 79 20  there is really 
10e56 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  no difference be
10e57 74 77 65 65 6e 20 63 72 65 61 74 65 46 6c 61 67  tween createFlag
10e58 20 31 20 61 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a   1 and 2.  So.**
10e59 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
10e5a 63 74 69 6f 6e 20 28 70 63 61 63 68 65 2e 63 29  ction (pcache.c)
10e5b 20 77 69 6c 6c 20 6e 65 76 65 72 20 68 61 76 65   will never have
10e5c 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66   a createFlag of
10e5d 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70   1 on.** a non-p
10e5e 75 72 67 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a  urgable cache..*
10e5f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74  *.** There are t
10e60 68 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61  hree different a
10e61 70 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74  pproaches to obt
10e62 61 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72  aining space for
10e63 20 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65   a page,.** depe
10e64 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c  nding on the val
10e65 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20  ue of parameter 
10e66 63 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63  createFlag (whic
10e67 68 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72  h may be 0, 1 or
10e68 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20   2)..**.**   1. 
10e69 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  Regardless of th
10e6a 65 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74  e value of creat
10e6b 65 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65  eFlag, the cache
10e6c 20 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72   is searched for
10e6d 20 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79   a .**      copy
10e6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
10e6f 64 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69  d page. If one i
10e70 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72  s found, it is r
10e71 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  eturned..**.**  
10e72 20 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61   2. If createFla
10e73 67 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67  g==0 and the pag
10e74 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
10e75 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e   in the cache, N
10e76 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72  ULL is.**      r
10e77 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  eturned..**.**  
10e78 20 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61   3. If createFla
10e79 67 20 69 73 20 31 2c 20 61 6e 64 20 74 68 65 20  g is 1, and the 
10e7a 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  page is not alre
10e7b 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  ady in the cache
10e7c 2c 0a 2a 2a 20 20 20 20 20 20 61 6e 64 20 69 66  ,.**      and if
10e7d 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66   either of the f
10e7e 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75  ollowing are tru
10e7f 65 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a  e, return NULL:.
10e80 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20  **.**       (a) 
10e81 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
10e82 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68  ges pinned by th
10e83 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74  e cache is great
10e84 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20  er than.**      
10e85 20 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61       PCache1.nMa
10e86 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28  x, or.**       (
10e87 62 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  b) the number of
10e88 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79   pages pinned by
10e89 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72   the cache is gr
10e8a 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20  eater than.**   
10e8b 20 20 20 20 20 20 20 20 74 68 65 20 73 75 6d 20          the sum 
10e8c 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20  of nMax for all 
10e8d 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
10e8e 2c 20 6c 65 73 73 20 74 68 65 20 73 75 6d 20 6f  , less the sum o
10e8f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  f .**           
10e90 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68  nMin for all oth
10e91 65 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63  er purgeable cac
10e92 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e  hes. .**.**   4.
10e93 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
10e94 66 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64  first three cond
10e95 69 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64  itions apply and
10e96 20 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61   the cache is ma
10e97 72 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20  rked.**      as 
10e98 70 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69  purgeable, and i
10e99 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  f one of the fol
10e9a 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a  lowing is true:.
10e9b 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20  **.**       (a) 
10e9c 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  The number of pa
10e9d 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ges allocated fo
10e9e 72 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61  r the cache is a
10e9f 6c 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20  lready .**      
10ea0 20 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61       PCache1.nMa
10ea1 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20  x, or.**.**     
10ea2 20 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72    (b) The number
10ea3 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61   of pages alloca
10ea4 74 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67  ted for all purg
10ea5 65 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a  eable caches is.
10ea6 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72  **           alr
10ea7 65 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72  eady equal to or
10ea8 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
10ea9 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f  e sum of nMax fo
10eaa 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20  r all.**        
10eab 20 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63     purgeable cac
10eac 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  hes,.**.**      
10ead 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  then attempt to 
10eae 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 66  recycle a page f
10eaf 72 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74  rom the LRU list
10eb0 2e 20 49 66 20 69 74 20 69 73 20 74 68 65 20 72  . If it is the r
10eb1 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a  ight.**      siz
10eb2 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65  e, return the re
10eb3 63 79 63 6c 65 64 20 62 75 66 66 65 72 2e 20 4f  cycled buffer. O
10eb4 74 68 65 72 77 69 73 65 2c 20 66 72 65 65 20 74  therwise, free t
10eb5 68 65 20 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a  he buffer and.**
10eb6 20 20 20 20 20 20 70 72 6f 63 65 65 64 20 74 6f        proceed to
10eb7 20 73 74 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20   step 5. .**.** 
10eb8 20 20 35 2e 20 4f 74 68 65 72 77 69 73 65 2c 20    5. Otherwise, 
10eb9 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74  allocate and ret
10eba 75 72 6e 20 61 20 6e 65 77 20 70 61 67 65 20 62  urn a new page b
10ebb 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  uffer..*/.static
10ebc 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 46 65   void *pcache1Fe
10ebd 74 63 68 28 73 71 6c 69 74 65 33 5f 70 63 61 63  tch(sqlite3_pcac
10ebe 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20  he *p, unsigned 
10ebf 69 6e 74 20 69 4b 65 79 2c 20 69 6e 74 20 63 72  int iKey, int cr
10ec0 65 61 74 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73  eateFlag){.  uns
10ec1 69 67 6e 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65  igned int nPinne
10ec2 64 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  d;.  PCache1 *pC
10ec3 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
10ec4 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70  *)p;.  PgHdr1 *p
10ec5 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73  Page = 0;..  ass
10ec6 65 72 74 28 20 70 43 61 63 68 65 2d 3e 62 50 75  ert( pCache->bPu
10ec7 72 67 65 61 62 6c 65 20 7c 7c 20 63 72 65 61 74  rgeable || creat
10ec8 65 46 6c 61 67 21 3d 31 20 29 3b 0a 20 20 70 63  eFlag!=1 );.  pc
10ec9 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10eca 29 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46  );.  if( createF
10ecb 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33  lag==1 ) sqlite3
10ecc 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
10ecd 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63  c();..  /* Searc
10ece 68 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  h the hash table
10ecf 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67   for an existing
10ed0 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 66 28   entry. */.  if(
10ed1 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3e 30   pCache->nHash>0
10ed2 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   ){.    unsigned
10ed3 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20   int h = iKey % 
10ed4 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
10ed5 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43 61     for(pPage=pCa
10ed6 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20  che->apHash[h]; 
10ed7 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e 69 4b  pPage&&pPage->iK
10ed8 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 65 3d  ey!=iKey; pPage=
10ed9 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b 0a 20  pPage->pNext);. 
10eda 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20   }..  if( pPage 
10edb 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30  || createFlag==0
10edc 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 50   ){.    pcache1P
10edd 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10ede 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75     goto fetch_ou
10edf 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65  t;.  }..  /* Ste
10ee0 70 20 33 20 6f 66 20 68 65 61 64 65 72 20 63 6f  p 3 of header co
10ee1 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e  mment. */.  nPin
10ee2 6e 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 6e 50  ned = pCache->nP
10ee3 61 67 65 20 2d 20 70 43 61 63 68 65 2d 3e 6e 52  age - pCache->nR
10ee4 65 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69 66 28  ecyclable;.  if(
10ee5 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 26   createFlag==1 &
10ee6 26 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e  & (.        nPin
10ee7 6e 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d  ned>=(pcache1.nM
10ee8 61 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e  axPage+pCache->n
10ee9 4d 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e  Min-pcache1.nMin
10eea 50 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50  Page).     || nP
10eeb 69 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e  inned>=(pCache->
10eec 6e 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20  nMax * 9 / 10). 
10eed 20 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65   )){.    goto fe
10eee 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10eef 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  if( pCache->nPag
10ef0 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68  e>=pCache->nHash
10ef1 20 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a   && pcache1Resiz
10ef2 65 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b  eHash(pCache) ){
10ef3 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f  .    goto fetch_
10ef4 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  out;.  }..  /* S
10ef5 74 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65  tep 4. Try to re
10ef6 63 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66  cycle a page buf
10ef7 66 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61  fer if appropria
10ef8 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61  te. */.  if( pCa
10ef9 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
10efa 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54  && pcache1.pLruT
10efb 61 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70  ail && (.     (p
10efc 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d  Cache->nPage+1>=
10efd 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c  pCache->nMax) ||
10efe 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10eff 74 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e  tPage>=pcache1.n
10f00 4d 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20  MaxPage.  )){.  
10f01 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65    pPage = pcache
10f02 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20  1.pLruTail;.    
10f03 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f  pcache1RemoveFro
10f04 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20  mHash(pPage);.  
10f05 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65    pcache1PinPage
10f06 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  (pPage);.    if(
10f07 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e   pPage->pCache->
10f08 73 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e  szPage!=pCache->
10f09 73 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  szPage ){.      
10f0a 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10f0b 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50  pPage);.      pP
10f0c 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  age = 0;.    }el
10f0d 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65  se{.      pcache
10f0e 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d  1.nCurrentPage -
10f0f 3d 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65  = (pPage->pCache
10f10 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70  ->bPurgeable - p
10f11 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
10f12 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
10f13 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61   /* Step 5. If a
10f14 20 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66   usable page buf
10f15 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f  fer has still no
10f16 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20  t been found, . 
10f17 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61   ** attempt to a
10f18 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e  llocate a new on
10f19 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  e. .  */.  if( !
10f1a 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61  pPage ){.    pPa
10f1b 67 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f  ge = pcache1Allo
10f1c 63 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20  cPage(pCache);. 
10f1d 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20   }..  if( pPage 
10f1e 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
10f1f 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70  int h = iKey % p
10f20 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10f21 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b    pCache->nPage+
10f22 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 4b  +;.    pPage->iK
10f23 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70  ey = iKey;.    p
10f24 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43  Page->pNext = pC
10f25 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b  ache->apHash[h];
10f26 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63  .    pPage->pCac
10f27 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 20  he = pCache;.   
10f28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76   pPage->pLruPrev
10f29 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
10f2a 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20  >pLruNext = 0;. 
10f2b 20 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47     *(void **)(PG
10f2c 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61  HDR1_TO_PAGE(pPa
10f2d 67 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43  ge)) = 0;.    pC
10f2e 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20  ache->apHash[h] 
10f2f 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65  = pPage;.  }..fe
10f30 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70  tch_out:.  if( p
10f31 50 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61  Page && iKey>pCa
10f32 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a  che->iMaxKey ){.
10f33 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78      pCache->iMax
10f34 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a  Key = iKey;.  }.
10f35 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67    if( createFlag
10f36 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64  ==1 ) sqlite3End
10f37 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
10f38 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10f39 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20  tex();.  return 
10f3a 28 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f  (pPage ? PGHDR1_
10f3b 54 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a  TO_PAGE(pPage) :
10f3c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49   0);.}.../*.** I
10f3d 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10f3e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10f3f 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f  che.xUnpin metho
10f40 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20  d..**.** Mark a 
10f41 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64  page as unpinned
10f42 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61   (eligible for a
10f43 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79  synchronous recy
10f44 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69  cling)..*/.stati
10f45 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e  c void pcache1Un
10f46 70 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63  pin(sqlite3_pcac
10f47 68 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67  he *p, void *pPg
10f48 2c 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b  , int reuseUnlik
10f49 65 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ely){.  PCache1 
10f4a 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10f4b 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10f4c 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54   *pPage = PAGE_T
10f4d 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10f4e 20 70 50 67 29 3b 0a 20 0a 20 20 61 73 73 65 72   pPg);. .  asser
10f4f 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65  t( pPage->pCache
10f50 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63  ==pCache );.  pc
10f51 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10f52 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61  );..  /* It is a
10f53 6e 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20  n error to call 
10f54 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66  this function if
10f55 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72   the page is alr
10f56 65 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20  eady .  ** part 
10f57 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52  of the global LR
10f58 55 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61  U list..  */.  a
10f59 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c  ssert( pPage->pL
10f5a 72 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61  ruPrev==0 && pPa
10f5b 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20  ge->pLruNext==0 
10f5c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61  );.  assert( pca
10f5d 63 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70  che1.pLruHead!=p
10f5e 50 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e  Page && pcache1.
10f5f 70 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20  pLruTail!=pPage 
10f60 29 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55  );..  if( reuseU
10f61 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68  nlikely || pcach
10f62 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e  e1.nCurrentPage>
10f63 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
10f64 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52   ){.    pcache1R
10f65 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50  emoveFromHash(pP
10f66 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  age);.    pcache
10f67 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29  1FreePage(pPage)
10f68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
10f69 2a 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74  * Add the page t
10f6a 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55  o the global LRU
10f6b 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c   list. Normally,
10f6c 20 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64   the page is add
10f6d 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  ed to.    ** the
10f6e 20 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73   head of the lis
10f6f 74 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20  t (last page to 
10f70 62 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f  be recycled). Ho
10f71 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20  wever, if the . 
10f72 20 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b     ** reuseUnlik
10f73 65 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20  ely flag passed 
10f74 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
10f75 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61   is true, the pa
10f76 67 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20  ge is added.    
10f77 2a 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f  ** to the tail o
10f78 66 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73  f the list (firs
10f79 74 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63  t page to be rec
10f7a 79 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20  ycled)..    */. 
10f7b 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70     if( pcache1.p
10f7c 4c 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20  LruHead ){.     
10f7d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10f7e 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50  d->pLruPrev = pP
10f7f 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65  age;.      pPage
10f80 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61  ->pLruNext = pca
10f81 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20  che1.pLruHead;. 
10f82 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72       pcache1.pLr
10f83 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20  uHead = pPage;. 
10f84 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
10f85 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10f86 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20   = pPage;.      
10f87 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10f88 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a   = pPage;.    }.
10f89 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63      pCache->nRec
10f8a 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a  yclable++;.  }..
10f8b 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10f8c 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
10f8d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
10f8e 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
10f8f 61 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68  ache.xRekey meth
10f90 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  od. .*/.static v
10f91 6f 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79  oid pcache1Rekey
10f92 28 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  (.  sqlite3_pcac
10f93 68 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70  he *p,.  void *p
10f94 50 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  Pg,.  unsigned i
10f95 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67  nt iOld,.  unsig
10f96 6e 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a  ned int iNew.){.
10f97 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10f98 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
10f99 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67  ;.  PgHdr1 *pPag
10f9a 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44  e = PAGE_TO_PGHD
10f9b 52 31 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b  R1(pCache, pPg);
10f9c 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a  .  PgHdr1 **pp;.
10f9d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10f9e 3b 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ; .  assert( pPa
10f9f 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29  ge->iKey==iOld )
10fa0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
10fa1 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68  e->pCache==pCach
10fa2 65 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45  e );..  pcache1E
10fa3 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
10fa4 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d  h = iOld%pCache-
10fa5 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26  >nHash;.  pp = &
10fa6 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
10fa7 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70  ];.  while( (*pp
10fa8 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  )!=pPage ){.    
10fa9 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
10faa 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20  xt;.  }.  *pp = 
10fab 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20  pPage->pNext;.. 
10fac 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65   h = iNew%pCache
10fad 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65  ->nHash;.  pPage
10fae 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20  ->iKey = iNew;. 
10faf 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20   pPage->pNext = 
10fb0 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
10fb1 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48  ];.  pCache->apH
10fb2 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a  ash[h] = pPage;.
10fb3 20 20 69 66 28 20 69 4e 65 77 3e 70 43 61 63 68    if( iNew>pCach
10fb4 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20  e->iMaxKey ){.  
10fb5 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65    pCache->iMaxKe
10fb6 79 20 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20  y = iNew;.  }.. 
10fb7 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10fb8 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ex();.}../*.** I
10fb9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10fba 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10fbb 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20 6d 65  che.xTruncate me
10fbc 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73  thod. .**.** Dis
10fbd 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e 6e 65  card all unpinne
10fbe 64 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63  d pages in the c
10fbf 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67 65  ache with a page
10fc0 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20 74 6f   number equal to
10fc1 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 74  .** or greater t
10fc2 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20 69 4c  han parameter iL
10fc3 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64  imit. Any pinned
10fc4 20 70 61 67 65 73 20 77 69 74 68 20 61 20 70 61   pages with a pa
10fc5 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65 71 75  ge number.** equ
10fc6 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72  al to or greater
10fc7 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61 72 65   than iLimit are
10fc8 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 69   implicitly unpi
10fc9 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nned..*/.static 
10fca 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 75 6e  void pcache1Trun
10fcb 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70 63 61  cate(sqlite3_pca
10fcc 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64  che *p, unsigned
10fcd 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a 20 20   int iLimit){.  
10fce 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
10fcf 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a  = (PCache1 *)p;.
10fd0 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10fd1 74 65 78 28 29 3b 0a 20 20 69 66 28 20 69 4c 69  tex();.  if( iLi
10fd2 6d 69 74 3c 3d 70 43 61 63 68 65 2d 3e 69 4d 61  mit<=pCache->iMa
10fd3 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 63 61 63  xKey ){.    pcac
10fd4 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66  he1TruncateUnsaf
10fd5 65 28 70 43 61 63 68 65 2c 20 69 4c 69 6d 69 74  e(pCache, iLimit
10fd6 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69  );.    pCache->i
10fd7 4d 61 78 4b 65 79 20 3d 20 69 4c 69 6d 69 74 2d  MaxKey = iLimit-
10fd8 31 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31  1;.  }.  pcache1
10fd9 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a  LeaveMutex();.}.
10fda 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10fdb 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10fdc 69 74 65 33 5f 70 63 61 63 68 65 2e 78 44 65 73  ite3_pcache.xDes
10fdd 74 72 6f 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a  troy method. .**
10fde 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 63 61  .** Destroy a ca
10fdf 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 75 73  che allocated us
10fe0 69 6e 67 20 70 63 61 63 68 65 31 43 72 65 61 74  ing pcache1Creat
10fe1 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  e()..*/.static v
10fe2 6f 69 64 20 70 63 61 63 68 65 31 44 65 73 74 72  oid pcache1Destr
10fe3 6f 79 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  oy(sqlite3_pcach
10fe4 65 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 31  e *p){.  PCache1
10fe5 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10fe6 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68  he1 *)p;.  pcach
10fe7 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  e1EnterMutex();.
10fe8 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74    pcache1Truncat
10fe9 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20  eUnsafe(pCache, 
10fea 30 29 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d  0);.  pcache1.nM
10feb 61 78 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65  axPage -= pCache
10fec 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63 61 63 68 65  ->nMax;.  pcache
10fed 31 2e 6e 4d 69 6e 50 61 67 65 20 2d 3d 20 70 43  1.nMinPage -= pC
10fee 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63  ache->nMin;.  pc
10fef 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50  ache1EnforceMaxP
10ff0 61 67 65 28 29 3b 0a 20 20 70 63 61 63 68 65 31  age();.  pcache1
10ff1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10ff2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61  sqlite3_free(pCa
10ff3 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20  che->apHash);.  
10ff4 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61  sqlite3_free(pCa
10ff5 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  che);.}../*.** T
10ff6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10ff7 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e  called during in
10ff8 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71  itialization (sq
10ff9 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
10ffa 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c  ()) to.** instal
10ffb 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 6c  l the default pl
10ffc 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f  uggable cache mo
10ffd 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74  dule, assuming t
10ffe 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 0a  he user has not.
10fff 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f 76 69  ** already provi
11000 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ded an alternati
11001 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
11002 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
11003 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75  e3PCacheSetDefau
11004 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  lt(void){.  stat
11005 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ic sqlite3_pcach
11006 65 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  e_methods defaul
11007 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
11008 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
11009 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 72            /* pAr
1100a 67 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  g */.    pcache1
1100b 49 6e 69 74 2c 20 20 20 20 20 20 20 20 20 20 20  Init,           
1100c 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f 0a 20 20    /* xInit */.  
1100d 20 20 70 63 61 63 68 65 31 53 68 75 74 64 6f 77    pcache1Shutdow
1100e 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53  n,         /* xS
1100f 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20 20 20 70  hutdown */.    p
11010 63 61 63 68 65 31 43 72 65 61 74 65 2c 20 20 20  cache1Create,   
11011 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61          /* xCrea
11012 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  te */.    pcache
11013 31 43 61 63 68 65 73 69 7a 65 2c 20 20 20 20 20  1Cachesize,     
11014 20 20 20 2f 2a 20 78 43 61 63 68 65 73 69 7a 65     /* xCachesize
11015 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 50   */.    pcache1P
11016 61 67 65 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  agecount,       
11017 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e 74 20 2a   /* xPagecount *
11018 2f 0a 20 20 20 20 70 63 61 63 68 65 31 46 65 74  /.    pcache1Fet
11019 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ch,            /
1101a 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20 20 20 20  * xFetch */.    
1101b 70 63 61 63 68 65 31 55 6e 70 69 6e 2c 20 20 20  pcache1Unpin,   
1101c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 70           /* xUnp
1101d 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  in */.    pcache
1101e 31 52 65 6b 65 79 2c 20 20 20 20 20 20 20 20 20  1Rekey,         
1101f 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 2a 2f 0a     /* xRekey */.
11020 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e 63      pcache1Trunc
11021 61 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ate,         /* 
11022 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 20  xTruncate */.   
11023 20 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 20   pcache1Destroy 
11024 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65            /* xDe
11025 73 74 72 6f 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20  stroy */.  };.  
11026 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
11027 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
11028 43 48 45 2c 20 26 64 65 66 61 75 6c 74 4d 65 74  CHE, &defaultMet
11029 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66  hods);.}..#ifdef
1102a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
1102b 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
1102c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1102d 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
1102e 6f 20 66 72 65 65 20 73 75 70 65 72 66 6c 75 6f  o free superfluo
1102f 75 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  us dynamically a
11030 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a  llocated memory.
11031 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 70  ** held by the p
11032 61 67 65 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d  ager system. Mem
11033 6f 72 79 20 69 6e 20 75 73 65 20 62 79 20 61 6e  ory in use by an
11034 79 20 53 51 4c 69 74 65 20 70 61 67 65 72 20 61  y SQLite pager a
11035 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74  llocated.** by t
11036 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61  he current threa
11037 64 20 6d 61 79 20 62 65 20 73 71 6c 69 74 65 33  d may be sqlite3
11038 5f 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a  _free()ed..**.**
11039 20 6e 52 65 71 20 69 73 20 74 68 65 20 6e 75 6d   nReq is the num
1103a 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1103b 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 2e  memory required.
1103c 20 4f 6e 63 65 20 74 68 69 73 20 6d 75 63 68 20   Once this much 
1103d 68 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65  has.** been rele
1103e 61 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69  ased, the functi
1103f 6f 6e 20 72 65 74 75 72 6e 73 2e 20 54 68 65 20  on returns. The 
11040 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
11041 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
11042 20 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 6f 66   .** of bytes of
11043 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73 65 64   memory released
11044 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11045 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
11046 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f  cacheReleaseMemo
11047 72 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20  ry(int nReq){.  
11048 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20  int nFree = 0;. 
11049 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 53 74   if( pcache1.pSt
1104a 61 72 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50 67  art==0 ){.    Pg
1104b 48 64 72 31 20 2a 70 3b 0a 20 20 20 20 70 63 61  Hdr1 *p;.    pca
1104c 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
1104d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6e 52  ;.    while( (nR
1104e 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65 3c 6e 52  eq<0 || nFree<nR
1104f 65 71 29 20 26 26 20 28 70 3d 70 63 61 63 68 65  eq) && (p=pcache
11050 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20  1.pLruTail) ){. 
11051 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71       nFree += sq
11052 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
11053 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70  PGHDR1_TO_PAGE(p
11054 29 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65  ));.      pcache
11055 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20  1PinPage(p);.   
11056 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65     pcache1Remove
11057 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20  FromHash(p);.   
11058 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
11059 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ge(p);.    }.   
1105a 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
1105b 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ex();.  }.  retu
1105c 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64  rn nFree;.}.#end
1105d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
1105e 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
1105f 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66  EMENT */..#ifdef
11060 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a   SQLITE_TEST./*.
11061 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
11062 20 69 73 20 75 73 65 64 20 62 79 20 74 65 73 74   is used by test
11063 20 70 72 6f 63 65 64 75 72 65 73 20 74 6f 20 69   procedures to i
11064 6e 73 70 65 63 74 20 74 68 65 20 69 6e 74 65 72  nspect the inter
11065 6e 61 6c 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20  nal state.** of 
11066 74 68 65 20 67 6c 6f 62 61 6c 20 63 61 63 68 65  the global cache
11067 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11068 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
11069 50 63 61 63 68 65 53 74 61 74 73 28 0a 20 20 69  PcacheStats(.  i
1106a 6e 74 20 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20  nt *pnCurrent,  
1106b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61      /* OUT: Tota
1106c 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
1106d 73 20 63 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e  s cached */.  in
1106e 74 20 2a 70 6e 4d 61 78 2c 20 20 20 20 20 20 20  t *pnMax,       
1106f 20 20 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61     /* OUT: Globa
11070 6c 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 20  l maximum cache 
11071 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  size */.  int *p
11072 6e 4d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 2f  nMin,          /
11073 2a 20 4f 55 54 3a 20 53 75 6d 20 6f 66 20 50 43  * OUT: Sum of PC
11074 61 63 68 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70  ache1.nMin for p
11075 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20  urgeable caches 
11076 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 65 63 79  */.  int *pnRecy
11077 63 6c 61 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54  clable    /* OUT
11078 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  : Total number o
11079 66 20 70 61 67 65 73 20 61 76 61 69 6c 61 62 6c  f pages availabl
1107a 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 20  e for recycling 
1107b 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 31 20 2a  */.){.  PgHdr1 *
1107c 70 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c  p;.  int nRecycl
1107d 61 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28  able = 0;.  for(
1107e 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65  p=pcache1.pLruHe
1107f 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c 72 75  ad; p; p=p->pLru
11080 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65 63 79  Next){.    nRecy
11081 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20  clable++;.  }.  
11082 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 63 61  *pnCurrent = pca
11083 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67  che1.nCurrentPag
11084 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20 70 63  e;.  *pnMax = pc
11085 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 3b 0a  ache1.nMaxPage;.
11086 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63 61 63 68    *pnMin = pcach
11087 65 31 2e 6e 4d 69 6e 50 61 67 65 3b 0a 20 20 2a  e1.nMinPage;.  *
11088 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 6e  pnRecyclable = n
11089 52 65 63 79 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65  Recyclable;.}.#e
1108a 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
1108b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61  ***** End of pca
1108c 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  che1.c *********
1108d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1108e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1108f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
11090 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
11091 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a   rowset.c ******
11092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11094 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
11095 20 44 65 63 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a   December 3.**.*
11096 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
11097 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
11098 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
11099 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1109a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1109b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1109c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1109d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1109e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1109f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
110a0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
110a1 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
110a2 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
110a3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
110a4 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
110a5 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
110a6 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
110a7 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
110a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
110ac 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70   This module imp
110ad 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63  lements an objec
110ae 74 20 77 65 20 63 61 6c 6c 20 61 20 22 52 6f 77  t we call a "Row
110af 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Set"..**.** The 
110b0 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73  RowSet object is
110b1 20 61 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66   a collection of
110b2 20 72 6f 77 69 64 73 2e 20 20 52 6f 77 69 64 73   rowids.  Rowids
110b3 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
110b4 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74   into the RowSet
110b5 20 69 6e 20 61 6e 20 61 72 62 69 74 72 61 72 79   in an arbitrary
110b6 20 6f 72 64 65 72 2e 20 20 49 6e 73 65 72 74 73   order.  Inserts
110b7 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 72  .** can be inter
110b8 6d 69 78 65 64 20 77 69 74 68 20 74 65 73 74 73  mixed with tests
110b9 20 74 6f 20 73 65 65 20 69 66 20 61 20 67 69 76   to see if a giv
110ba 65 6e 20 72 6f 77 69 64 20 68 61 73 20 62 65 65  en rowid has bee
110bb 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  n.** previously 
110bc 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
110bd 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20  e RowSet..**.** 
110be 41 66 74 65 72 20 61 6c 6c 20 69 6e 73 65 72 74  After all insert
110bf 73 20 61 72 65 20 66 69 6e 69 73 68 65 64 2c 20  s are finished, 
110c0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
110c1 6f 20 65 78 74 72 61 63 74 20 74 68 65 0a 2a 2a  o extract the.**
110c2 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
110c3 20 52 6f 77 53 65 74 20 69 6e 20 73 6f 72 74 65   RowSet in sorte
110c4 64 20 6f 72 64 65 72 2e 20 20 4f 6e 63 65 20 74  d order.  Once t
110c5 68 69 73 20 65 78 74 72 61 63 74 69 6f 6e 0a 2a  his extraction.*
110c6 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 73 74  * process has st
110c7 61 72 74 65 64 2c 20 6e 6f 20 6e 65 77 20 65 6c  arted, no new el
110c8 65 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 69 6e  ements may be in
110c9 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65  serted..**.** He
110ca 6e 63 65 2c 20 74 68 65 20 70 72 69 6d 69 74 69  nce, the primiti
110cb 76 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f  ve operations fo
110cc 72 20 61 20 52 6f 77 53 65 74 20 61 72 65 3a 0a  r a RowSet are:.
110cd 2a 2a 0a 2a 2a 20 20 20 20 43 52 45 41 54 45 0a  **.**    CREATE.
110ce 2a 2a 20 20 20 20 49 4e 53 45 52 54 0a 2a 2a 20  **    INSERT.** 
110cf 20 20 20 54 45 53 54 0a 2a 2a 20 20 20 20 53 4d     TEST.**    SM
110d0 41 4c 4c 45 53 54 0a 2a 2a 20 20 20 20 44 45 53  ALLEST.**    DES
110d1 54 52 4f 59 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  TROY.**.** The C
110d2 52 45 41 54 45 20 61 6e 64 20 44 45 53 54 52 4f  REATE and DESTRO
110d3 59 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65  Y primitives are
110d4 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 6f 72   the constructor
110d5 20 61 6e 64 20 64 65 73 74 72 75 63 74 6f 72 2c   and destructor,
110d6 0a 2a 2a 20 6f 62 76 69 6f 75 73 6c 79 2e 20 20  .** obviously.  
110d7 54 68 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69  The INSERT primi
110d8 74 69 76 65 20 61 64 64 73 20 61 20 6e 65 77 20  tive adds a new 
110d9 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 52  element to the R
110da 6f 77 53 65 74 2e 0a 2a 2a 20 54 45 53 54 20 63  owSet..** TEST c
110db 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  hecks to see if 
110dc 61 6e 20 65 6c 65 6d 65 6e 74 20 69 73 20 61 6c  an element is al
110dd 72 65 61 64 79 20 69 6e 20 74 68 65 20 52 6f 77  ready in the Row
110de 53 65 74 2e 20 20 53 4d 41 4c 4c 45 53 54 0a 2a  Set.  SMALLEST.*
110df 2a 20 65 78 74 72 61 63 74 73 20 74 68 65 20 6c  * extracts the l
110e0 65 61 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20  east value from 
110e1 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a  the RowSet..**.*
110e2 2a 20 54 68 65 20 49 4e 53 45 52 54 20 70 72 69  * The INSERT pri
110e3 6d 69 74 69 76 65 20 6d 69 67 68 74 20 61 6c 6c  mitive might all
110e4 6f 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c  ocate additional
110e5 20 6d 65 6d 6f 72 79 2e 20 20 4d 65 6d 6f 72 79   memory.  Memory
110e6 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64   is.** allocated
110e7 20 69 6e 20 63 68 75 6e 6b 73 20 73 6f 20 6d 6f   in chunks so mo
110e8 73 74 20 49 4e 53 45 52 54 73 20 64 6f 20 6e 6f  st INSERTs do no
110e9 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68   allocation.  Th
110ea 65 72 65 20 69 73 20 61 6e 20 0a 2a 2a 20 75 70  ere is an .** up
110eb 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65  per bound on the
110ec 20 73 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74   size of allocat
110ed 65 64 20 6d 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d  ed memory.  No m
110ee 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 0a 2a  emory is freed.*
110ef 2a 20 75 6e 74 69 6c 20 44 45 53 54 52 4f 59 2e  * until DESTROY.
110f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 20  .**.** The TEST 
110f1 70 72 69 6d 69 74 69 76 65 20 69 6e 63 6c 75 64  primitive includ
110f2 65 73 20 61 20 22 62 61 74 63 68 22 20 6e 75 6d  es a "batch" num
110f3 62 65 72 2e 20 20 54 68 65 20 54 45 53 54 20 70  ber.  The TEST p
110f4 72 69 6d 69 74 69 76 65 0a 2a 2a 20 77 69 6c 6c  rimitive.** will
110f5 20 6f 6e 6c 79 20 73 65 65 20 65 6c 65 6d 65 6e   only see elemen
110f6 74 73 20 74 68 61 74 20 77 65 72 65 20 69 6e 73  ts that were ins
110f7 65 72 74 65 64 20 62 65 66 6f 72 65 20 74 68 65  erted before the
110f8 20 6c 61 73 74 20 63 68 61 6e 67 65 0a 2a 2a 20   last change.** 
110f9 69 6e 20 74 68 65 20 62 61 74 63 68 20 6e 75 6d  in the batch num
110fa 62 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ber.  In other w
110fb 6f 72 64 73 2c 20 69 66 20 61 6e 20 49 4e 53 45  ords, if an INSE
110fc 52 54 20 6f 63 63 75 72 73 20 62 65 74 77 65 65  RT occurs betwee
110fd 6e 0a 2a 2a 20 74 77 6f 20 54 45 53 54 73 20 77  n.** two TESTs w
110fe 68 65 72 65 20 74 68 65 20 54 45 53 54 73 20 68  here the TESTs h
110ff 61 76 65 20 74 68 65 20 73 61 6d 65 20 62 61 74  ave the same bat
11100 63 68 20 6e 75 62 6d 65 72 2c 20 74 68 65 6e 20  ch nubmer, then 
11101 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 61 64 64  the.** value add
11102 65 64 20 62 79 20 74 68 65 20 49 4e 53 45 52 54  ed by the INSERT
11103 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 76 69 73   will not be vis
11104 69 62 6c 65 20 74 6f 20 74 68 65 20 73 65 63 6f  ible to the seco
11105 6e 64 20 54 45 53 54 2e 0a 2a 2a 20 54 68 65 20  nd TEST..** The 
11106 69 6e 69 74 69 61 6c 20 62 61 74 63 68 20 6e 75  initial batch nu
11107 6d 62 65 72 20 69 73 20 7a 65 72 6f 2c 20 73 6f  mber is zero, so
11108 20 69 66 20 74 68 65 20 76 65 72 79 20 66 69 72   if the very fir
11109 73 74 20 54 45 53 54 20 63 6f 6e 74 61 69 6e 73  st TEST contains
1110a 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62  .** a non-zero b
1110b 61 74 63 68 20 6e 75 6d 62 65 72 2c 20 69 74 20  atch number, it 
1110c 77 69 6c 6c 20 73 65 65 20 61 6c 6c 20 70 72 69  will see all pri
1110d 6f 72 20 49 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a  or INSERTs..**.*
1110e 2a 20 4e 6f 20 49 4e 53 45 52 54 73 20 6d 61 79  * No INSERTs may
1110f 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 61 20   occurs after a 
11110 53 4d 41 4c 4c 45 53 54 2e 20 20 41 6e 20 61 73  SMALLEST.  An as
11111 73 65 72 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69  sertion will fai
11112 6c 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20  l if.** that is 
11113 61 74 74 65 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a  attempted..**.**
11114 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 6e 20   The cost of an 
11115 49 4e 53 45 52 54 20 69 73 20 72 6f 75 67 68 6c  INSERT is roughl
11116 79 20 63 6f 6e 73 74 61 6e 74 2e 20 20 28 53 6f  y constant.  (So
11117 6d 65 74 69 6d 65 20 6e 65 77 20 6d 65 6d 6f 72  metime new memor
11118 79 0a 2a 2a 20 68 61 73 20 74 6f 20 62 65 20 61  y.** has to be a
11119 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20 49  llocated on an I
1111a 4e 53 45 52 54 2e 29 20 20 54 68 65 20 63 6f 73  NSERT.)  The cos
1111b 74 20 6f 66 20 61 20 54 45 53 54 20 77 69 74 68  t of a TEST with
1111c 20 61 20 6e 65 77 0a 2a 2a 20 62 61 74 63 68 20   a new.** batch 
1111d 6e 75 6d 62 65 72 20 69 73 20 4f 28 4e 6c 6f 67  number is O(Nlog
1111e 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  N) where N is th
1111f 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
11120 65 6e 74 73 20 69 6e 20 74 68 65 20 52 6f 77 53  ents in the RowS
11121 65 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20  et..** The cost 
11122 6f 66 20 61 20 54 45 53 54 20 75 73 69 6e 67 20  of a TEST using 
11123 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e  the same batch n
11124 75 6d 62 65 72 20 69 73 20 4f 28 6c 6f 67 4e 29  umber is O(logN)
11125 2e 20 20 54 68 65 20 63 6f 73 74 0a 2a 2a 20 6f  .  The cost.** o
11126 66 20 74 68 65 20 66 69 72 73 74 20 53 4d 41 4c  f the first SMAL
11127 4c 45 53 54 20 69 73 20 4f 28 4e 6c 6f 67 4e 29  LEST is O(NlogN)
11128 2e 20 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75  .  Second and su
11129 62 73 65 71 75 65 6e 74 20 53 4d 41 4c 4c 45 53  bsequent SMALLES
1112a 54 0a 2a 2a 20 70 72 69 6d 69 74 69 76 65 73 20  T.** primitives 
1112b 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 74 69 6d  are constant tim
1112c 65 2e 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20  e.  The cost of 
1112d 44 45 53 54 52 4f 59 20 69 73 20 4f 28 4e 29 2e  DESTROY is O(N).
1112e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
1112f 61 6e 20 61 64 64 65 64 20 63 6f 73 74 20 6f 66  an added cost of
11130 20 4f 28 4e 29 20 77 68 65 6e 20 73 77 69 74 63   O(N) when switc
11131 68 69 6e 67 20 62 65 74 77 65 65 6e 20 54 45 53  hing between TES
11132 54 20 61 6e 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53  T and.** SMALLES
11133 54 20 70 72 69 6d 69 74 69 76 65 73 2e 0a 2a 2f  T primitives..*/
11134 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74 20  .../*.** Target 
11135 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  size for allocat
11136 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a 23  ion chunks..*/.#
11137 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41 4c  define ROWSET_AL
11138 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31 30  LOCATION_SIZE 10
11139 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75  24../*.** The nu
1113a 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 65  mber of rowset e
1113b 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f 63  ntries per alloc
1113c 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  ation chunk..*/.
1113d 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 45  #define ROWSET_E
1113e 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 20  NTRY_PER_CHUNK  
1113f 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
11140 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 45           ((ROWSE
11141 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a  T_ALLOCATION_SIZ
11142 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 75  E-8)/sizeof(stru
11143 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29  ct RowSetEntry))
11144 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74  ../*.** Each ent
11145 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 69  ry in a RowSet i
11146 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
11147 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   the following o
11148 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  bject..*/.struct
11149 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 20   RowSetEntry {  
1114a 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36 34            .  i64
1114b 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   v;             
1114c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f             /* RO
1114d 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74 68  WID value for th
1114e 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74  is entry */.  st
1114f 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11150 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 52   *pRight;   /* R
11151 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c 61  ight subtree (la
11152 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f 72  rger entries) or
11153 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63   list */.  struc
11154 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
11155 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 74  Left;    /* Left
11156 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c 65   subtree (smalle
11157 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d 3b  r entries) */.};
11158 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 6e  ../*.** RowSetEn
11159 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65 20  try objects are 
1115a 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 72  allocated in lar
1115b 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 61  ge chunks (insta
1115c 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66  nces of the.** f
1115d 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
1115e 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d 65  re) to reduce me
1115f 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
11160 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a 2a  overhead.  The.*
11161 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 70  * chunks are kep
11162 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  t on a linked li
11163 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  st so that they 
11164 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74  can be deallocat
11165 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 52  ed.** when the R
11166 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f 79  owSet is destroy
11167 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f  ed..*/.struct Ro
11168 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 74  wSetChunk {.  st
11169 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
1116a 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 20   *pNextChunk;   
1116b 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75       /* Next chu
1116c 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 68  nk on list of th
1116d 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 75  em all */.  stru
1116e 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 61  ct RowSetEntry a
1116f 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e 54  Entry[ROWSET_ENT
11170 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f  RY_PER_CHUNK]; /
11171 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 72  * Allocated entr
11172 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ies */.};../*.**
11173 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20   A RowSet in an 
11174 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
11175 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
11176 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70  ure..**.** A typ
11177 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74 72  edef of this str
11178 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64 20  ucture if found 
11179 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a  in sqliteInt.h..
1117a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74  */.struct RowSet
1117b 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53   {.  struct RowS
1117c 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  etChunk *pChunk;
1117d 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61      /* List of a
1117e 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 74  ll chunk allocat
1117f 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ions */.  sqlite
11180 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
11181 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
11182 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
11183 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ion */.  struct 
11184 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e  RowSetEntry *pEn
11185 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20  try;    /* List 
11186 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e 67  of entries using
11187 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 72   pRight */.  str
11188 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11189 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c  *pLast;     /* L
1118a 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65  ast entry on the
1118b 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a   pEntry list */.
1118c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1118d 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 20  ntry *pFresh;   
1118e 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e 65   /* Source of ne
1118f 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 20  w entry objects 
11190 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
11191 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b 20  etEntry *pTree; 
11192 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 72      /* Binary tr
11193 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a 2f  ee of entries */
11194 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 20  .  u16 nFresh;  
11195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11196 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f    /* Number of o
11197 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 68  bjects on pFresh
11198 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 65   */.  u8 isSorte
11199 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
1119a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1119b 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65 64  pEntry is sorted
1119c 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68 3b   */.  u8 iBatch;
1119d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1119e 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
1119f 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f 0a  insert batch */.
111a0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62  };../*.** Turn b
111a1 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20  ulk memory into 
111a2 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  a RowSet object.
111a3 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d    N bytes of mem
111a4 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 6c  ory.** are avail
111a5 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e 20  able at pSpace. 
111a6 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20   The db pointer 
111a7 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 6d  is used as a mem
111a8 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 66  ory context.** f
111a9 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e  or any subsequen
111aa 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68  t allocations th
111ab 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 72  at need to occur
111ac 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  ..** Return a po
111ad 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
111ae 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e 0a   RowSet object..
111af 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 65  **.** It must be
111b0 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 4e   the case that N
111b1 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74   is sufficient t
111b2 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 2e  o make a Rowset.
111b3 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61    If not.** an a
111b4 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 6f  ssertion fault o
111b5 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  ccurs..** .** If
111b6 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 61   N is larger tha
111b7 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 75  n the minimum, u
111b8 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 61  se the surplus a
111b9 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a 20  s an initial.** 
111ba 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 6e  allocation of en
111bb 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 20  tries available 
111bc 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f  to be filled..*/
111bd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
111be 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52  RowSet *sqlite3R
111bf 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65  owSetInit(sqlite
111c0 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 70  3 *db, void *pSp
111c1 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e  ace, unsigned in
111c2 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 2a  t N){.  RowSet *
111c3 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 3e  p;.  assert( N >
111c4 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  = ROUND8(sizeof(
111c5 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70 53  *p)) );.  p = pS
111c6 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75 6e  pace;.  p->pChun
111c7 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d  k = 0;.  p->db =
111c8 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79   db;.  p->pEntry
111c9 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74   = 0;.  p->pLast
111ca 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65   = 0;.  p->pTree
111cb 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 73   = 0;.  p->pFres
111cc 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77 53  h = (struct RowS
111cd 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 38  etEntry*)(ROUND8
111ce 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 28  (sizeof(*p)) + (
111cf 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e  char*)p);.  p->n
111d0 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e  Fresh = (u16)((N
111d1 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66   - ROUND8(sizeof
111d2 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 74  (*p)))/sizeof(st
111d3 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
111d4 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65  ));.  p->isSorte
111d5 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61 74  d = 1;.  p->iBat
111d6 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ch = 0;.  return
111d7 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61   p;.}../*.** Dea
111d8 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75 6e  llocate all chun
111d9 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65 74  ks from a RowSet
111da 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61 6c  .  This frees al
111db 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a 2a  l memory that.**
111dc 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73 20   the RowSet has 
111dd 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20 69  allocated over i
111de 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54 68  ts lifetime.  Th
111df 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a  is routine is.**
111e0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
111e1 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e 0a  for the RowSet..
111e2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
111e3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
111e4 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74  wSetClear(RowSet
111e5 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20 52   *p){.  struct R
111e6 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75  owSetChunk *pChu
111e7 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b  nk, *pNextChunk;
111e8 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 2d  .  for(pChunk=p-
111e9 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 3b  >pChunk; pChunk;
111ea 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74 43   pChunk = pNextC
111eb 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78 74  hunk){.    pNext
111ec 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e  Chunk = pChunk->
111ed 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20 20  pNextChunk;.    
111ee 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
111ef 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20 20  >db, pChunk);.  
111f0 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20  }.  p->pChunk = 
111f1 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d  0;.  p->nFresh =
111f2 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20   0;.  p->pEntry 
111f3 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20  = 0;.  p->pLast 
111f4 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20  = 0;.  p->pTree 
111f5 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74  = 0;.  p->isSort
111f6 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
111f7 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 61   Insert a new va
111f8 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 65  lue into a RowSe
111f9 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c  t..**.** The mal
111fa 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6f  locFailed flag o
111fb 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
111fc 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 74  onnection is set
111fd 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20   if a.** memory 
111fe 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
111ff 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
11201 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77  RowSetInsert(Row
11202 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 69  Set *p, i64 rowi
11203 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  d){.  struct Row
11204 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79  SetEntry *pEntry
11205 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 6e  ;  /* The new en
11206 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  try */.  struct 
11207 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61  RowSetEntry *pLa
11208 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 73  st;   /* The las
11209 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a 2f  t prior entry */
1120a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
1120b 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 65  );.  if( p->nFre
1120c 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72  sh==0 ){.    str
1120d 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20  uct RowSetChunk 
1120e 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 20  *pNew;.    pNew 
1120f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
11210 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a 65  cRaw(p->db, size
11211 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20  of(*pNew));.    
11212 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
11213 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
11214 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65   }.    pNew->pNe
11215 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 68  xtChunk = p->pCh
11216 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 75  unk;.    p->pChu
11217 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70  nk = pNew;.    p
11218 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77 2d  ->pFresh = pNew-
11219 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e  >aEntry;.    p->
1121a 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54 5f  nFresh = ROWSET_
1121b 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 3b  ENTRY_PER_CHUNK;
1121c 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
1121d 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20 70  p->pFresh++;.  p
1121e 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70 45  ->nFresh--;.  pE
1121f 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64 3b  ntry->v = rowid;
11220 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68  .  pEntry->pRigh
11221 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20 3d  t = 0;.  pLast =
11222 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66 28   p->pLast;.  if(
11223 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69 66   pLast ){.    if
11224 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26 26  ( p->isSorted &&
11225 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e 76   rowid<=pLast->v
11226 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 53   ){.      p->isS
11227 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d  orted = 0;.    }
11228 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69 67  .    pLast->pRig
11229 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d  ht = pEntry;.  }
1122a 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1122b 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29  ( p->pEntry==0 )
1122c 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49 4e  ; /* Fires if IN
1122d 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c 4c  SERT after SMALL
1122e 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70 45  EST */.    p->pE
1122f 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20  ntry = pEntry;. 
11230 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20   }.  p->pLast = 
11231 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pEntry;.}../*.**
11232 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73   Merge two lists
11233 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 20   of RowSetEntry 
11234 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76 65  objects.  Remove
11235 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a   duplicates..**.
11236 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69 73  ** The input lis
11237 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 64  ts are connected
11238 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69 6e   via pRight poin
11239 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a 2a  ters and are .**
1123a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63 68   assumed to each
1123b 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20 73   already be in s
1123c 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a  orted order..*/.
1123d 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f  static struct Ro
1123e 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65  wSetEntry *rowSe
1123f 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63 74  tMerge(.  struct
11240 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 41   RowSetEntry *pA
11241 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 6f  ,    /* First so
11242 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 20  rted list to be 
11243 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72 75  merged */.  stru
11244 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
11245 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64  pB     /* Second
11246 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20   sorted list to 
11247 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b 0a  be merged */.){.
11248 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11249 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74 72  ntry head;.  str
1124a 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1124b 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69 6c  *pTail;..  pTail
1124c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69 6c   = &head;.  whil
1124d 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a 20  e( pA && pB ){. 
1124e 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70     assert( pA->p
1124f 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e  Right==0 || pA->
11250 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e 76  v<=pA->pRight->v
11251 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
11252 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c  pB->pRight==0 ||
11253 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67   pB->v<=pB->pRig
11254 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 28  ht->v );.    if(
11255 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a   pA->v<pB->v ){.
11256 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69        pTail->pRi
11257 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 20  ght = pA;.      
11258 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b  pA = pA->pRight;
11259 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70  .      pTail = p
1125a 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Tail->pRight;.  
1125b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e    }else if( pB->
1125c 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20  v<pA->v ){.     
1125d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d   pTail->pRight =
1125e 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20   pB;.      pB = 
1125f 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pB->pRight;.    
11260 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d    pTail = pTail-
11261 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c  >pRight;.    }el
11262 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20 70  se{.      pA = p
11263 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d  A->pRight;.    }
11264 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 7b  .  }.  if( pA ){
11265 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d  .    assert( pA-
11266 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41  >pRight==0 || pA
11267 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d  ->v<=pA->pRight-
11268 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d  >v );.    pTail-
11269 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 20  >pRight = pA;.  
1126a 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1126b 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d 3e  t( pB==0 || pB->
1126c 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d  pRight==0 || pB-
1126d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e  >v<=pB->pRight->
1126e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  v );.    pTail->
1126f 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 7d  pRight = pB;.  }
11270 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e 70  .  return head.p
11271 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Right;.}../*.** 
11272 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  Sort all element
11273 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20  s on the pEntry 
11274 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77 53  list of the RowS
11275 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 6e  et into ascendin
11276 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 61  g order..*/ .sta
11277 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 53  tic void rowSetS
11278 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a  ort(RowSet *p){.
11279 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
1127a 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  ;.  struct RowSe
1127b 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 0a  tEntry *pEntry;.
1127c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1127d 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34 30  ntry *aBucket[40
1127e 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  ];..  assert( p-
1127f 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b 0a  >isSorted==0 );.
11280 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65 74    memset(aBucket
11281 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75 63  , 0, sizeof(aBuc
11282 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28 20  ket));.  while( 
11283 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20  p->pEntry ){.   
11284 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e   pEntry = p->pEn
11285 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74  try;.    p->pEnt
11286 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52 69  ry = pEntry->pRi
11287 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d  ght;.    pEntry-
11288 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20  >pRight = 0;.   
11289 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b 65   for(i=0; aBucke
1128a 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t[i]; i++){.    
1128b 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65    pEntry = rowSe
1128c 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b 69  tMerge(aBucket[i
1128d 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20 20  ], pEntry);.    
1128e 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 30    aBucket[i] = 0
1128f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75 63  ;.    }.    aBuc
11290 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79 3b  ket[i] = pEntry;
11291 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
11292 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
11293 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 2f  sizeof(aBucket)/
11294 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b 30  sizeof(aBucket[0
11295 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 45  ]); i++){.    pE
11296 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72  ntry = rowSetMer
11297 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63 6b  ge(pEntry, aBuck
11298 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 2d  et[i]);.  }.  p-
11299 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79  >pEntry = pEntry
1129a 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30  ;.  p->pLast = 0
1129b 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20  ;.  p->isSorted 
1129c 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  = 1;.}.../*.** T
1129d 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20 69  he input, pIn, i
1129e 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 20  s a binary tree 
1129f 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66 20  (or subtree) of 
112a0 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65  RowSetEntry obje
112a1 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  cts..** Convert 
112a2 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20 61  this tree into a
112a3 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f 6e   linked list con
112a4 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70 52  nected by the pR
112a5 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a  ight pointers.**
112a6 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69 6e   and return poin
112a7 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72 73  ters to the firs
112a8 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d 65  t and last eleme
112a9 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20 6c  nts of the new l
112aa 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
112ab 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54 6f  oid rowSetTreeTo
112ac 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20 52  List(.  struct R
112ad 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e 2c  owSetEntry *pIn,
112ae 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
112af 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 72   of the input tr
112b0 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ee */.  struct R
112b1 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 46  owSetEntry **ppF
112b2 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69 74  irst,    /* Writ
112b3 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f 75  e head of the ou
112b4 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 2a  tput list here *
112b5 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  /.  struct RowSe
112b6 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74 20  tEntry **ppLast 
112b7 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 61       /* Write ta
112b8 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  il of the output
112b9 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b   list here */.){
112ba 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21 3d  .  assert( pIn!=
112bb 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d 3e  0 );.  if( pIn->
112bc 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74 72  pLeft ){.    str
112bd 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
112be 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54 72  *p;.    rowSetTr
112bf 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 4c  eeToList(pIn->pL
112c0 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26 70  eft, ppFirst, &p
112c1 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74  );.    p->pRight
112c2 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65 7b   = pIn;.  }else{
112c3 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d 20  .    *ppFirst = 
112c4 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  pIn;.  }.  if( p
112c5 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20  In->pRight ){.  
112c6 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69    rowSetTreeToLi
112c7 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20  st(pIn->pRight, 
112c8 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70 70  &pIn->pRight, pp
112c9 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Last);.  }else{.
112ca 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70 49      *ppLast = pI
112cb 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
112cc 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69 67   (*ppLast)->pRig
112cd 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  ht==0 );.}.../*.
112ce 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72  ** Convert a sor
112cf 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d  ted list of elem
112d0 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64 20  ents (connected 
112d1 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f 20  by pRight) into 
112d2 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65 65  a binary.** tree
112d3 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20 69   with depth of i
112d4 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68 20  Depth.  A depth 
112d5 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20 74  of 1 means the t
112d6 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73  ree contains a s
112d7 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74 61  ingle.** node ta
112d8 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65 61  ken from the hea
112d9 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20 41  d of *ppList.  A
112da 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61 6e   depth of 2 mean
112db 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a 2a  s a tree with.**
112dc 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20 41   three nodes.  A
112dd 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a  nd so forth..**.
112de 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20 65  ** Use as many e
112df 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20  ntries from the 
112e0 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72 65  input list as re
112e1 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61 74  quired and updat
112e2 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73 74  e the.** *ppList
112e3 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
112e4 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74 73   unused elements
112e5 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20 49   of the list.  I
112e6 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20 6c  f the input.** l
112e7 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6f  ist contains too
112e8 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20 74   few elements, t
112e9 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61 6e  hen construct an
112ea 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65 65   incomplete tree
112eb 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a 70  .** and leave *p
112ec 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55 4c  pList set to NUL
112ed 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  L..**.** Return 
112ee 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
112ef 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f 6e   root of the con
112f0 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79 20  structed binary 
112f1 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tree..*/.static 
112f2 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
112f3 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70 54  ry *rowSetNDeepT
112f4 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52 6f  ree(.  struct Ro
112f5 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 69  wSetEntry **ppLi
112f6 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74 68  st,.  int iDepth
112f7 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  .){.  struct Row
112f8 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 20  SetEntry *p;    
112f9 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20       /* Root of 
112fa 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f 0a  the new tree */.
112fb 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
112fc 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20  ntry *pLeft;    
112fd 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65   /* Left subtree
112fe 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69 73   */.  if( *ppLis
112ff 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  t==0 ){.    retu
11300 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
11301 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20 20  iDepth==1 ){.   
11302 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20   p = *ppList;.  
11303 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70    *ppList = p->p
11304 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c  Right;.    p->pL
11305 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20  eft = p->pRight 
11306 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
11307 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 3d  p;.  }.  pLeft =
11308 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65   rowSetNDeepTree
11309 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d  (ppList, iDepth-
1130a 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69 73  1);.  p = *ppLis
1130b 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  t;.  if( p==0 ){
1130c 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66  .    return pLef
1130d 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65 66  t;.  }.  p->pLef
1130e 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70 70  t = pLeft;.  *pp
1130f 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74  List = p->pRight
11310 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20  ;.  p->pRight = 
11311 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28  rowSetNDeepTree(
11312 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d 31  ppList, iDepth-1
11313 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
11314 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
11315 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66  a sorted list of
11316 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20 61   elements into a
11317 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d 61   binary tree. Ma
11318 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 61  ke the tree.** a
11319 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65 65  s deep as it nee
1131a 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64 65  ds to be in orde
1131b 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65  r to contain the
1131c 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a 2f   entire list..*/
1131d 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52  .static struct R
1131e 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53  owSetEntry *rowS
1131f 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74 72  etListToTree(str
11320 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11321 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
11322 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20  Depth;          
11323 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68 65   /* Depth of the
11324 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f 0a   tree so far */.
11325 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11326 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 2f  ntry *p;       /
11327 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20 72  * Current tree r
11328 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  oot */.  struct 
11329 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65  RowSetEntry *pLe
1132a 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73 75  ft;   /* Left su
1132b 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73 65  btree */..  asse
1132c 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a  rt( pList!=0 );.
1132d 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20 70    p = pList;.  p
1132e 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74  List = p->pRight
1132f 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70  ;.  p->pLeft = p
11330 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
11331 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70 4c  for(iDepth=1; pL
11332 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b 0a  ist; iDepth++){.
11333 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a 20      pLeft = p;. 
11334 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20     p = pList;.  
11335 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69    pList = p->pRi
11336 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 66  ght;.    p->pLef
11337 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20 70  t = pLeft;.    p
11338 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65  ->pRight = rowSe
11339 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69 73  tNDeepTree(&pLis
1133a 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d 0a  t, iDepth);.  }.
1133b 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
1133c 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65  *.** Convert the
1133d 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e 74   list in p->pEnt
1133e 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65 64  ry into a sorted
1133f 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20 6e   list if it is n
11340 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c 72  ot.** sorted alr
11341 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65 20  eady.  If there 
11342 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65  is a binary tree
11343 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74 68   on p->pTree, th
11344 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69 74  en.** convert it
11345 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f 6f   into a list too
11346 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69 6e   and merge it in
11347 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72 79  to the p->pEntry
11348 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   list..*/.static
11349 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c 69   void rowSetToLi
1134a 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20  st(RowSet *p){. 
1134b 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 65   if( !p->isSorte
1134c 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 53  d ){.    rowSetS
1134d 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66  ort(p);.  }.  if
1134e 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20 20  ( p->pTree ){.  
1134f 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11350 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70 54  ntry *pHead, *pT
11351 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74 54  ail;.    rowSetT
11352 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54 72  reeToList(p->pTr
11353 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 61  ee, &pHead, &pTa
11354 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72 65  il);.    p->pTre
11355 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 45  e = 0;.    p->pE
11356 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72  ntry = rowSetMer
11357 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70 48  ge(p->pEntry, pH
11358 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ead);.  }.}../*.
11359 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 73  ** Extract the s
1135a 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74 20  mallest element 
1135b 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 2e  from the RowSet.
1135c 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 6c  .** Write the el
1135d 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f 77  ement into *pRow
1135e 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f 6e  id.  Return 1 on
1135f 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75 72   success.  Retur
11360 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52 6f  n.** 0 if the Ro
11361 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79 20  wSet is already 
11362 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 74  empty..**.** Aft
11363 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
11364 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  has been called,
11365 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77 53   the sqlite3RowS
11366 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72 6f  etInsert().** ro
11367 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62 65  utine may not be
11368 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20 20   called again.  
11369 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1136a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f  TE int sqlite3Ro
1136b 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 20  wSetNext(RowSet 
1136c 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 29  *p, i64 *pRowid)
1136d 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 74  {.  rowSetToList
1136e 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45  (p);.  if( p->pE
1136f 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f  ntry ){.    *pRo
11370 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d  wid = p->pEntry-
11371 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72  >v;.    p->pEntr
11372 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70  y = p->pEntry->p
11373 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 70  Right;.    if( p
11374 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a 20  ->pEntry==0 ){. 
11375 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53       sqlite3RowS
11376 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 20  etClear(p);.    
11377 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  }.    return 1;.
11378 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
11379 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
1137a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
1137b 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f 77   if element iRow
1137c 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64 20  id was inserted 
1137d 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f 77  into the the row
1137e 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f  set as.** part o
1137f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61 74  f any insert bat
11380 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61 74  ch prior to iBat
11381 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f 72  ch.  Return 1 or
11382 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
11383 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11384 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53  3RowSetTest(RowS
11385 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 20  et *pRowSet, u8 
11386 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 5f  iBatch, sqlite3_
11387 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  int64 iRowid){. 
11388 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
11389 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69 42  try *p;.  if( iB
1138a 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e 69  atch!=pRowSet->i
1138b 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 28  Batch ){.    if(
1138c 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79   pRowSet->pEntry
1138d 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65 74   ){.      rowSet
1138e 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29 3b  ToList(pRowSet);
1138f 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e  .      pRowSet->
11390 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c 69  pTree = rowSetLi
11391 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65 74  stToTree(pRowSet
11392 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 20  ->pEntry);.     
11393 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79   pRowSet->pEntry
11394 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f 77   = 0;.      pRow
11395 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a  Set->pLast = 0;.
11396 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53 65      }.    pRowSe
11397 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61 74  t->iBatch = iBat
11398 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 52  ch;.  }.  p = pR
11399 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20 20  owSet->pTree;.  
1139a 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
1139b 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 20  if( p->v<iRowid 
1139c 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  ){.      p = p->
1139d 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73  pRight;.    }els
1139e 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77 69  e if( p->v>iRowi
1139f 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70  d ){.      p = p
113a0 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65 6c  ->pLeft;.    }el
113a1 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
113a2 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
113a3 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a  return 0;.}../**
113a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
113a5 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a   of rowset.c ***
113a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
113a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
113aa 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 20  in file pager.c 
113ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
113ae 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
113af 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
113b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
113b1 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
113b2 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
113b3 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
113b4 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
113b5 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
113b6 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
113b7 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
113b8 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
113b9 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
113ba 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
113bb 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
113bc 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
113bd 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
113be 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
113bf 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
113c0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
113c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c5 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
113c6 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
113c7 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61  n of the page ca
113c8 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f 72  che subsystem or
113c9 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a   "pager"..** .**
113ca 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 73   The pager is us
113cb 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 64  ed to access a d
113cc 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c  atabase disk fil
113cd 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74  e.  It implement
113ce 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d  s.** atomic comm
113cf 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20  it and rollback 
113d0 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 20  through the use 
113d1 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  of a journal fil
113d2 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 70  e that.** is sep
113d3 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 64  arate from the d
113d4 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
113d5 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 6d  he pager also im
113d6 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a  plements file.**
113d7 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76   locking to prev
113d8 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65  ent two processe
113d9 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74  s from writing t
113da 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
113db 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61  .** file simulta
113dc 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20  neously, or one 
113dd 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61  process from rea
113de 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ding the databas
113df 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68  e while.** anoth
113e0 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a  er is writing..*
113e1 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
113e2 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a  _OMIT_DISKIO../*
113e3 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74  .** Macros for t
113e4 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20  roubleshooting. 
113e5 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
113e6 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e   off.*/.#if 0.in
113e7 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  t sqlite3PagerTr
113e8 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20  ace=1;  /* True 
113e9 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e  to enable tracin
113ea 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  g */.#define sql
113eb 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20  ite3DebugPrintf 
113ec 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50  printf.#define P
113ed 41 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20  AGERTRACE(X)    
113ee 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65   if( sqlite3Page
113ef 72 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65  rTrace ){ sqlite
113f0 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20  3DebugPrintf X; 
113f1 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
113f2 50 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65  PAGERTRACE(X).#e
113f3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
113f4 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61  following two ma
113f5 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69  cros are used wi
113f6 74 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52  thin the PAGERTR
113f7 41 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f  ACE() macros abo
113f8 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f  ve.** to print o
113f9 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74  ut file-descript
113fa 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45  ors. .**.** PAGE
113fb 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f  RID() takes a po
113fc 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72  inter to a Pager
113fd 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61   struct as its a
113fe 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  rgument. The.** 
113ff 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d  associated file-
11400 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65  descriptor is re
11401 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44  turned. FILEHAND
11402 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20  LEID() takes an 
11403 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20  sqlite3_file.** 
11404 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72  struct as its ar
11405 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69  gument..*/.#defi
11406 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28  ne PAGERID(p) ((
11407 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65  int)(p->fd)).#de
11408 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49  fine FILEHANDLEI
11409 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a  D(fd) ((int)fd).
1140a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  ./*.** The page 
1140b 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65  cache as a whole
1140c 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e   is always in on
1140d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1140e 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a  ng.** states:.**
1140f 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f  .**   PAGER_UNLO
11410 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61  CK        The pa
11411 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20  ge cache is not 
11412 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e  currently readin
11413 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  g or .**        
11414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
11415 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  riting the datab
11416 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65  ase file.  There
11417 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20   is no.**       
11418 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11419 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d  data held in mem
1141a 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68  ory.  This is th
1141b 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20  e initial.**    
1141c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1141d 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20     state..**.** 
1141e 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20    PAGER_SHARED  
1141f 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63        The page c
11420 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20  ache is reading 
11421 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
11422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11423 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69         Writing i
11424 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e  s not permitted.
11425 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a    There can be.*
11426 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
11427 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65          multiple
11428 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69   readers accessi
11429 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
1142a 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  base.**         
1142b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
1142c 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  le at the same t
1142d 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  ime..**.**   PAG
1142e 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20  ER_RESERVED     
1142f 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61   This process ha
11430 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64  s reserved the d
11431 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74  atabase for writ
11432 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
11433 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74               but
11434 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64   has not yet mad
11435 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20  e any changes.  
11436 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73  Only one process
11437 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11438 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74            at a t
11439 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20  ime can reserve 
1143a 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
1143b 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20  he original.**  
1143c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1143d 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69       database fi
1143e 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  le has not been 
1143f 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65  modified so othe
11440 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
11441 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65             proce
11442 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  sses may still b
11443 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e  e reading the on
11444 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20  -disk.**        
11445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
11446 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
11447 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c  .**   PAGER_EXCL
11448 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61  USIVE     The pa
11449 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74  ge cache is writ
1144a 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
1144b 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
1144c 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73             Acces
1144d 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20  s is exclusive. 
1144e 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73   No other proces
1144f 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ses or.**       
11450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11451 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72  threads can be r
11452 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
11453 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20  g while one.**  
11454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11455 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20       process is 
11456 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  writing..**.**  
11457 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20   PAGER_SYNCED   
11458 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d       The pager m
11459 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61  oves to this sta
1145a 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58  te from PAGER_EX
1145b 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20  CLUSIVE.**      
1145c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1145d 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79   after all dirty
1145e 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
1145f 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a   written to the.
11460 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
11461 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
11462 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66  e file and the f
11463 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e  ile has been syn
11464 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ced to.**       
11465 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11466 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72  disk. All that r
11467 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20  emains to do is 
11468 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20  to remove or.** 
11469 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1146a 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74        truncate t
1146b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1146c 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  and the transact
1146d 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion .**         
1146e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69                wi
1146f 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e  ll be committed.
11470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  .**.** The page 
11471 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69  cache comes up i
11472 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20  n PAGER_UNLOCK. 
11473 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
11474 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65  a.** sqlite3Page
11475 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74  rGet() occurs, t
11476 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74  he state transit
11477 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48  ions to PAGER_SH
11478 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61  ARED..** After a
11479 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65  ll pages have be
1147a 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e  en released usin
1147b 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e  g sqlite_page_un
1147c 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74  ref(),.** the st
1147d 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ate transitions 
1147e 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e  back to PAGER_UN
1147f 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74  LOCK.  The first
11480 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71   time.** that sq
11481 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
11482 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  ) is called, the
11483 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
11484 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52  ns to.** PAGER_R
11485 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20  ESERVED.  (Note 
11486 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65  that sqlite3Page
11487 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c  rWrite() can onl
11488 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f  y be.** called o
11489 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  n an outstanding
1148a 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e   page which mean
1148b 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72  s that the pager
1148c 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50   must.** be in P
1148d 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f  AGER_SHARED befo
1148e 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e  re it transition
1148f 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52  s to PAGER_RESER
11490 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52  VED.).** PAGER_R
11491 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68  ESERVED means th
11492 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f  at there is an o
11493 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  pen rollback jou
11494 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61  rnal..** The tra
11495 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52  nsition to PAGER
11496 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72  _EXCLUSIVE occur
11497 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  s before any cha
11498 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65  nges.** are made
11499 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1149a 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72   file, though wr
1149b 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c  ites to the roll
1149c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  back.** journal 
1149d 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74  occurs with just
1149e 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e   PAGER_RESERVED.
1149f 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74    After an sqlit
114a0 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
114a1 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50  ).** or sqlite3P
114a2 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54  agerCommitPhaseT
114a3 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20  wo(), the state 
114a4 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50  can go back to P
114a5 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20  AGER_SHARED,.** 
114a6 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61  or it can stay a
114a7 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  t PAGER_EXCLUSIV
114a8 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65  E if we are in e
114a9 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20  xclusive access 
114aa 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mode..*/.#define
114ab 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20   PAGER_UNLOCK   
114ac 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47     0.#define PAG
114ad 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31  ER_SHARED      1
114ae 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48     /* same as SH
114af 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  ARED_LOCK */.#de
114b0 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52  fine PAGER_RESER
114b1 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61  VED    2   /* sa
114b2 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c  me as RESERVED_L
114b3 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  OCK */.#define P
114b4 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20  AGER_EXCLUSIVE  
114b5 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20   4   /* same as 
114b6 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a  EXCLUSIVE_LOCK *
114b7 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
114b8 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f  SYNCED      5../
114b9 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65  *.** A macro use
114ba 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74  d for invoking t
114bb 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72  he codec if ther
114bc 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64  e is one.*/.#ifd
114bd 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ef SQLITE_HAS_CO
114be 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44  DEC.# define COD
114bf 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c  EC1(P,D,N,X,E) \
114c0 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64  .    if( P->xCod
114c1 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28  ec && P->xCodec(
114c2 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29  P->pCodec,D,N,X)
114c3 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65  ==0 ){ E; }.# de
114c4 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c  fine CODEC2(P,D,
114c5 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69  N,X,E,O) \.    i
114c6 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20  f( P->xCodec==0 
114c7 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d  ){ O=(char*)D; }
114c8 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28  else \.    if( (
114c9 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f  O=(char*)(P->xCo
114ca 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c  dec(P->pCodec,D,
114cb 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20  N,X)))==0 ){ E; 
114cc 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
114cd 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c   CODEC1(P,D,N,X,
114ce 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f  E)   /* NO-OP */
114cf 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32  .# define CODEC2
114d0 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d  (P,D,N,X,E,O) O=
114d1 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a  (char*)D.#endif.
114d2 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
114d3 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f  um allowed secto
114d4 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49  r size. 64KiB. I
114d5 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a  f the xSectorsiz
114d6 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72  e() method .** r
114d7 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c  eturns a value l
114d8 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c  arger than this,
114d9 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52   then MAX_SECTOR
114da 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e  _SIZE is used in
114db 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63  stead..** This c
114dc 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79  ould conceivably
114dd 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f   cause corruptio
114de 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f  n following a po
114df 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a  wer failure on.*
114e0 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e  * such a system.
114e1 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74   This is current
114e2 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74  ly an undocument
114e3 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65  ed limit..*/.#de
114e4 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f  fine MAX_SECTOR_
114e5 53 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a  SIZE 0x10000../*
114e6 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
114e7 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
114e8 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
114e9 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68  located for each
114ea 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70   active.** savep
114eb 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65  oint and stateme
114ec 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
114ed 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c  n the system. Al
114ee 6c 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65  l such structure
114ef 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20  s.** are stored 
114f0 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61  in the Pager.aSa
114f1 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c  vepoint[] array,
114f2 20 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61   which is alloca
114f3 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a  ted and.** resiz
114f4 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
114f5 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  Realloc()..**.**
114f6 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e   When a savepoin
114f7 74 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68  t is created, th
114f8 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  e PagerSavepoint
114f9 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c  .iHdrOffset fiel
114fa 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30  d is.** set to 0
114fb 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68  . If a journal-h
114fc 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e  eader is written
114fd 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a   into the main j
114fe 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20  ournal while.** 
114ff 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73  the savepoint is
11500 20 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48   active, then iH
11501 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20  drOffset is set 
11502 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73  to the byte offs
11503 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  et .** immediate
11504 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ly following the
11505 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65   last journal re
11506 63 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74  cord written int
11507 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f  o the main.** jo
11508 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65  urnal before the
11509 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e   journal-header.
1150a 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65   This is require
1150b 64 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69  d during savepoi
1150c 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28  nt.** rollback (
1150d 73 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63  see pagerPlaybac
1150e 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a  kSavepoint())..*
1150f 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
11510 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
11511 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a  PagerSavepoint;.
11512 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65  struct PagerSave
11513 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f  point {.  i64 iO
11514 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
11515 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69         /* Starti
11516 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69  ng offset in mai
11517 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69  n journal */.  i
11518 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20  64 iHdrOffset;  
11519 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1151a 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69  ee above */.  Bi
1151b 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69  tvec *pInSavepoi
1151c 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65  nt;        /* Se
1151d 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  t of pages in th
1151e 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a  is savepoint */.
1151f 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20    Pgno nOrig;   
11520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11521 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65  * Original numbe
11522 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69  r of pages in fi
11523 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75  le */.  Pgno iSu
11524 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  bRec;           
11525 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
11526 20 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e   first record in
11527 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   sub-journal */.
11528 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e  };../*.** A open
11529 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61   page cache is a
1152a 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
1152b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
1152c 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72  cture..**.** err
1152d 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67  Code.**.**   Pag
1152e 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62  er.errCode may b
1152f 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  e set to SQLITE_
11530 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f  IOERR, SQLITE_CO
11531 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f  RRUPT, or.**   o
11532 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f  r SQLITE_FULL. O
11533 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  nce one of the f
11534 69 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72  irst three error
11535 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72  s occurs, it per
11536 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69  sists.**   and i
11537 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68  s returned as th
11538 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72  e result of ever
11539 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50  y major pager AP
1153a 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20  I call.  The.** 
1153b 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65    SQLITE_FULL re
1153c 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69  turn code is sli
1153d 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e  ghtly different.
1153e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c   It persists onl
1153f 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20  y until the.**  
11540 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c   next successful
11541 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72   rollback is per
11542 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61  formed on the pa
11543 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c  ger cache. Also,
11544 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c  .**   SQLITE_FUL
11545 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63  L does not affec
11546 74 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67  t the sqlite3Pag
11547 65 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69  erGet() and sqli
11548 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29  te3PagerLookup()
11549 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79  .**   APIs, they
1154a 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73   may still be us
1154b 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ed successfully.
1154c 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c  .**.** dbSizeVal
1154d 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72  id, dbSize, dbOr
1154e 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69  igSize, dbFileSi
1154f 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67  ze.**.**   Manag
11550 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ing the size of 
11551 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11552 65 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20  e in pages is a 
11553 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74  little complicat
11554 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72  ed..**   The var
11555 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69  iable Pager.dbSi
11556 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ze contains the 
11557 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
11558 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
11559 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72  e.**   image cur
1155a 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e  rently contains.
1155b 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65   As the database
1155c 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20   image grows or 
1155d 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20  shrinks this.** 
1155e 20 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70    variable is up
1155f 64 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61  dated. The varia
11560 62 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65  ble Pager.dbFile
11561 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Size contains th
11562 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66  e number.**   of
11563 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61   pages in the da
11564 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
11565 73 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65  s may be differe
11566 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62  nt from Pager.db
11567 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d  Size.**   if som
11568 65 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65  e pages have bee
11569 6e 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  n appended to th
1156a 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
1156b 20 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69   but not yet wri
1156c 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72  tten.**   out fr
1156d 6f 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20  om the cache to 
1156e 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20  the actual file 
1156f 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74  on disk. Or if t
11570 68 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65  he image has bee
11571 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64  n.**   truncated
11572 20 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74   by an increment
11573 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74  al-vacuum operat
11574 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64  ion. The Pager.d
11575 62 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62  bOrigSize variab
11576 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73  le.**   contains
11577 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
11578 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
11579 62 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20  base image when 
1157a 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20  the current.**  
1157b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
1157c 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e   opened. The con
1157d 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72  tents of all thr
1157e 65 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69  ee of these vari
1157f 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e  ables is.**   on
11580 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ly guaranteed to
11581 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74   be correct if t
11582 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72  he boolean Pager
11583 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20  .dbSizeValid is 
11584 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f  true..**.**   TO
11585 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63  DO: Under what c
11586 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53  onditions is dbS
11587 69 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c  izeValid set? Cl
11588 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61  eared?.**.** cha
11589 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a  ngeCountDone.**.
1158a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61  **   This boolea
1158b 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73  n variable is us
1158c 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
1158d 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d  that the change-
1158e 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74  counter .**   (t
1158f 68 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72  he 4-byte header
11590 20 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f   field at byte o
11591 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20  ffset 24 of the 
11592 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69  database file) i
11593 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61  s .**   not upda
11594 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74  ted more often t
11595 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a  han necessary. .
11596 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65  **.**   It is se
11597 74 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74  t to true when t
11598 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
11599 72 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74  r field is updat
1159a 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20  ed, which .**   
1159b 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
1159c 69 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  if an exclusive 
1159d 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20  lock is held on 
1159e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1159f 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c  e..**   It is cl
115a0 65 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61  eared (set to fa
115a1 6c 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e  lse) whenever an
115a2 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
115a3 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75  is .**   relinqu
115a4 69 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74  ished on the dat
115a5 61 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68  abase file. Each
115a6 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74   time a transact
115a7 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
115a8 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67  ,.**   The chang
115a9 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20  eCountDone flag 
115aa 69 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66  is inspected. If
115ab 20 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65   it is true, the
115ac 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70   work of.**   up
115ad 64 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67  dating the chang
115ae 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69  e-counter is omi
115af 74 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72  tted for the cur
115b0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
115b1 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d  ..**.**   This m
115b2 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74  echanism means t
115b3 68 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67  hat when running
115b4 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
115b5 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e  de, a connection
115b6 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79   .**   need only
115b7 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
115b8 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c  ge-counter once,
115b9 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74   for the first t
115ba 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20  ransaction.**   
115bb 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  committed..**.**
115bc 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a   dbModified.**.*
115bd 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69  *   The dbModifi
115be 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77  ed flag is set w
115bf 68 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61  henever a databa
115c0 73 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69  se page is dirti
115c1 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63  ed..**   It is c
115c2 6c 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e  leared at the en
115c3 64 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61  d of each transa
115c4 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49  ction..**.**   I
115c5 74 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63  t is used when c
115c6 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68  ommitting or oth
115c7 65 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20  erwise ending a 
115c8 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a  transaction. If.
115c9 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66  **   the dbModif
115ca 69 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61  ied flag is clea
115cb 72 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b  r then less work
115cc 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e   has to be done.
115cd 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74  .**.** journalSt
115ce 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68  arted.**.**   Th
115cf 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77  is flag is set w
115d0 68 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20  henever the the 
115d1 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20  main journal is 
115d2 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20  synced. .**.**  
115d3 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68   The point of th
115d4 69 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20  is flag is that 
115d5 69 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61  it must be set a
115d6 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66  fter the .**   f
115d7 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  irst journal hea
115d8 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c  der in a journal
115d9 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73   file has been s
115da 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a  ynced to disk..*
115db 2a 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68  *   After this h
115dc 61 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77  as happened, new
115dd 20 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20   pages appended 
115de 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
115df 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65  .**   do not nee
115e0 64 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44  d the PGHDR_NEED
115e1 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20  _SYNC flag set, 
115e2 61 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e  as they do not n
115e3 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74  eed.**   to wait
115e4 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73   for a journal s
115e5 79 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20  ync before they 
115e6 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f  can be written o
115e7 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64  ut to.**   the d
115e8 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65  atabase file (se
115e9 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72  e function pager
115ea 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20  _write())..**   
115eb 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a  .** setMaster.**
115ec 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61  .**   This varia
115ed 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65  ble is used to e
115ee 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d  nsure that the m
115ef 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
115f0 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66  le name.**   (if
115f1 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72   any) is only wr
115f2 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
115f3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65  ournal file once
115f4 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63  ..**.**   When c
115f5 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e  ommitting a tran
115f6 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73  saction, the mas
115f7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
115f8 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a   name (if any).*
115f9 2a 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74  *   may be writt
115fa 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  en into the jour
115fb 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74  nal file while t
115fc 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c  he pager is stil
115fd 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f  l in.**   PAGER_
115fe 52 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28  RESERVED state (
115ff 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f  see CommitPhaseO
11600 6e 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74  ne() for the act
11601 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68  ion). It.**   th
11602 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75  en attempts to u
11603 70 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63  pgrade to an exc
11604 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20  lusive lock. If 
11605 74 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  this attempt.** 
11606 20 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51    fails, then SQ
11607 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65  LITE_BUSY may be
11608 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
11609 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73   user and the us
1160a 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65  er.**   may atte
1160b 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68  mpt to commit th
1160c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67  e transaction ag
1160d 61 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69  ain later (calli
1160e 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68  ng.**   CommitPh
1160f 61 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e  aseOne() again).
11610 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73   This flag is us
11611 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  ed to ensure tha
11612 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74  t the .**   mast
11613 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
11614 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20  is only written 
11615 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
11616 69 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ile the first.**
11617 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68     time CommitPh
11618 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c  aseOne() is call
11619 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53  ed..**.** doNotS
1161a 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73  ync.**.**   This
1161b 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
1161c 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20   and cleared by 
1161d 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1161e 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53  e()..**.** needS
1161f 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f  ync.**.**   TODO
11620 3a 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61  : It might be ea
11621 73 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73  sier to set this
11622 20 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69   variable in wri
11623 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a  teJournalHdr().*
11624 2a 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73  *   and writeMas
11625 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c  terJournal() onl
11626 79 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65  y. Change its me
11627 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63  aning to "unsync
11628 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73  ed data.**   has
11629 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
1162a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a   the journal"..*
1162b 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72  *.** subjInMemor
1162c 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69  y.**.**   This i
1162d 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69  s a boolean vari
1162e 61 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74  able. If true, t
1162f 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64  hen any required
11630 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   sub-journal.** 
11631 20 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61    is opened as a
11632 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
11633 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c  nal file. If fal
11634 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f  se, then in-memo
11635 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72  ry.**   sub-jour
11636 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73  nals are only us
11637 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79  ed for in-memory
11638 20 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f   pager files..*/
11639 0a 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a  .struct Pager {.
1163a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
1163b 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Vfs;          /*
1163c 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f   OS functions to
1163d 20 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20   use for IO */. 
1163e 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64   u8 exclusiveMod
1163f 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e;           /* 
11640 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66  Boolean. True if
11641 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45   locking_mode==E
11642 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38  XCLUSIVE */.  u8
11643 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20   journalMode;   
11644 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20            /* On 
11645 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55  of the PAGER_JOU
11646 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65  RNALMODE_* value
11647 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75  s */.  u8 useJou
11648 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  rnal;           
11649 20 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c     /* Use a roll
1164a 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20  back journal on 
1164b 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75  this file */.  u
1164c 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20  8 noReadlock;   
1164d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
1164e 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f   not bother to o
1164f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20  btain readlocks 
11650 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20  */.  u8 noSync; 
11651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11652 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20   /* Do not sync 
11653 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74  the journal if t
11654 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c  rue */.  u8 full
11655 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
11656 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61       /* Do extra
11657 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f   syncs of the jo
11658 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74  urnal for robust
11659 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e  ness */.  u8 syn
1165a 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  c_flags;        
1165b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
1165c 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53  SYNC_NORMAL or S
1165d 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38  YNC_FULL */.  u8
1165e 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20   tempFile;      
1165f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69            /* zFi
11660 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70  lename is a temp
11661 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20  orary file */.  
11662 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20  u8 readOnly;    
11663 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11664 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f  rue for a read-o
11665 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
11666 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20    u8 memDb;     
11667 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11668 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74   True to inhibit
11669 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f   all file I/O */
1166a 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  /* The follo
1166b 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61  wing block conta
1166c 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20  ins those class 
1166d 6d 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65  members that are
1166e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a   dynamically.  *
1166f 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e  * modified durin
11670 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69  g normal operati
11671 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76  ons. The other v
11672 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73  ariables in this
11673 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20   structure.  ** 
11674 61 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74  are either const
11675 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74  ant throughout t
11676 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74  he lifetime of t
11677 68 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73  he pager, or els
11678 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73  e.  ** used to s
11679 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69  tore configurati
1167a 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68  on parameters th
1167b 61 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61  at affect the wa
1167c 79 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a  y the pager .  *
1167d 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a  * operates..  **
1167e 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65  .  ** The 'state
1167f 27 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65  ' variable is de
11680 73 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20  scribed in more 
11681 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74  detail along wit
11682 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72  h the.  ** descr
11683 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76  iptions of the v
11684 61 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b  alues it may tak
11685 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  e - PAGER_UNLOCK
11686 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68   etc. Many of th
11687 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72  e.  ** other var
11688 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62  iables in this b
11689 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62  lock are describ
1168a 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e  ed in the commen
1168b 74 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a  t directly .  **
1168c 20 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73   above this clas
1168d 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20  s definition..  
1168e 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20  */.  u8 state;  
1168f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11690 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b   /* PAGER_UNLOCK
11691 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45  , _SHARED, _RESE
11692 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20  RVED, etc. */.  
11693 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20  u8 dbModified;  
11694 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11695 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65  rue if there are
11696 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20   any changes to 
11697 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e  the Db */.  u8 n
11698 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20  eedSync;        
11699 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1169a 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73  if an fsync() is
1169b 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a   needed on the j
1169c 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a  ournal */.  u8 j
1169d 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20  ournalStarted;  
1169e 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1169f 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75  if header of jou
116a0 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a  rnal is synced *
116a1 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75  /.  u8 changeCou
116a2 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  ntDone;         
116a3 2f 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63  /* Set after inc
116a4 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68  rementing the ch
116a5 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a  ange-counter */.
116a6 20 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20    u8 setMaster; 
116a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116a8 20 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e   True if a m-j n
116a9 61 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69  ame has been wri
116aa 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a  tten to jrnl */.
116ab 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20    u8 doNotSync; 
116ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116ad 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20   Boolean. While 
116ae 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69  true, do not spi
116af 6c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  ll the cache */.
116b0 20 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64    u8 dbSizeValid
116b1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
116b2 20 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65   Set when dbSize
116b3 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20   is correct */. 
116b4 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79   u8 subjInMemory
116b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
116b6 54 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d  True to use in-m
116b7 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61  emory sub-journa
116b8 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53  ls */.  Pgno dbS
116b9 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
116ba 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
116bb 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61   pages in the da
116bc 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f  tabase */.  Pgno
116bd 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20   dbOrigSize;    
116be 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a          /* dbSiz
116bf 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72  e before the cur
116c0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
116c1 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c   */.  Pgno dbFil
116c2 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
116c3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
116c4 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
116c5 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69  base file */.  i
116c6 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20  nt errCode;     
116c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
116c8 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e  e of several kin
116c9 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a  ds of errors */.
116ca 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20    int nRec;     
116cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116cc 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65   Pages journalle
116cd 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68  d since last j-h
116ce 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f  eader written */
116cf 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74  .  u32 cksumInit
116d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
116d1 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76  * Quasi-random v
116d2 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76  alue added to ev
116d3 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a  ery checksum */.
116d4 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20    u32 nSubRec;  
116d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116d6 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72   Number of recor
116d7 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75  ds written to su
116d8 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42  b-journal */.  B
116d9 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61  itvec *pInJourna
116da 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  l;         /* On
116db 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70  e bit for each p
116dc 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
116dd 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71  ase file */.  sq
116de 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20  lite3_file *fd; 
116df 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
116e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
116e1 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73   database */.  s
116e2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64  qlite3_file *jfd
116e3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
116e4 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f  le descriptor fo
116e5 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a  r main journal *
116e6 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
116e7 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20   *sjfd;         
116e8 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
116e9 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e  or for sub-journ
116ea 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72  al */.  i64 jour
116eb 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20  nalOff;         
116ec 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77      /* Current w
116ed 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74  rite offset in t
116ee 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
116ef 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  */.  i64 journal
116f0 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Hdr;            
116f1 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20   /* Byte offset 
116f2 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72  to previous jour
116f3 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  nal header */.  
116f4 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
116f5 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41  aSavepoint; /* A
116f6 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73  rray of active s
116f7 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69  avepoints */.  i
116f8 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20  nt nSavepoint;  
116f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
116fa 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
116fb 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d   in aSavepoint[]
116fc 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c   */.  char dbFil
116fd 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20  eVers[16];      
116fe 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65    /* Changes whe
116ff 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66  never database f
11700 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20  ile changes */. 
11701 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b   u32 sectorSize;
11702 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11703 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73  Assumed sector s
11704 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  ize during rollb
11705 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45  ack */..  u16 nE
11706 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20  xtra;           
11707 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69        /* Add thi
11708 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20  s many bytes to 
11709 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  each in-memory p
1170a 61 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65  age */.  i16 nRe
1170b 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20  serve;          
1170c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1170d 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61  f unused bytes a
1170e 74 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61  t end of each pa
1170f 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46  ge */.  u32 vfsF
11710 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
11711 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72      /* Flags for
11712 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70   sqlite3_vfs.xOp
11713 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61  en() */.  int pa
11714 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  geSize;         
11715 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
11716 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61  of bytes in a pa
11717 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50  ge */.  Pgno mxP
11718 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
11719 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
1171a 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74  llowed size of t
1171b 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
1171c 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
1171d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1171e 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
1171f 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63  base file */.  c
11720 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20  har *zJournal;  
11721 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
11722 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
11723 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  l file */.  int 
11724 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28  (*xBusyHandler)(
11725 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74  void*); /* Funct
11726 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e  ion to call when
11727 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20   busy */.  void 
11728 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  *pBusyHandlerArg
11729 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78  ;      /* Contex
1172a 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78  t argument for x
1172b 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23  BusyHandler */.#
1172c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1172d 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d  T.  int nHit, nM
1172e 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  iss;            
1172f 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e  /* Cache hits an
11730 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69  d missing */.  i
11731 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65  nt nRead, nWrite
11732 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
11733 74 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61  tabase pages rea
11734 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e  d/written */.#en
11735 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65  dif.  void (*xRe
11736 69 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29  initer)(DbPage*)
11737 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  ; /* Call this r
11738 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f  outine when relo
11739 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23  ading pages */.#
1173a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
1173b 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28  _CODEC.  void *(
1173c 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76  *xCodec)(void*,v
1173d 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20  oid*,Pgno,int); 
1173e 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65  /* Routine for e
1173f 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20  n/decoding data 
11740 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64  */.  void (*xCod
11741 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64  ecSizeChng)(void
11742 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e  *,int,int); /* N
11743 6f 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69  otify of page si
11744 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20  ze changes */.  
11745 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65  void (*xCodecFre
11746 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  e)(void*);      
11747 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75         /* Destru
11748 63 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64  ctor for the cod
11749 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  ec */.  void *pC
1174a 6f 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  odec;           
1174b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
1174c 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e  ument to xCodec.
1174d 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65  .. methods */.#e
1174e 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d  ndif.  char *pTm
1174f 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20  pSpace;         
11750 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65     /* Pager.page
11751 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70  Size bytes of sp
11752 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20  ace for tmp use 
11753 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  */.  i64 journal
11754 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20  SizeLimit;      
11755 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66   /* Size limit f
11756 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f  or persistent jo
11757 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20  urnal files */. 
11758 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65   PCache *pPCache
11759 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1175a 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20  Pointer to page 
1175b 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  cache object */.
1175c 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
1175d 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a   *pBackup;    /*
1175e 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74   Pointer to list
1175f 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b   of ongoing back
11760 75 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a  up processes */.
11761 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
11762 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
11763 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f  ariables hold co
11764 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a  unters used for.
11765 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f  ** testing purpo
11766 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65  ses only.  These
11767 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f   variables do no
11768 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20  t exist in.** a 
11769 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c  non-testing buil
1176a 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62  d.  These variab
1176b 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65  les are not thre
1176c 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64  ad-safe..*/.#ifd
1176d 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1176e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1176f 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64  lite3_pager_read
11770 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20  db_count = 0;   
11771 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75   /* Number of fu
11772 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72  ll pages read fr
11773 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f  om DB */.SQLITE_
11774 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
11775 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f  pager_writedb_co
11776 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75  unt = 0;   /* Nu
11777 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67  mber of full pag
11778 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42  es written to DB
11779 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
1177a 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
1177b 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20  _writej_count = 
1177c 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  0;    /* Number 
1177d 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e  of pages written
1177e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23   to journal */.#
1177f 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e   define PAGER_IN
11780 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65  CR(v)  v++.#else
11781 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f  .# define PAGER_
11782 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a  INCR(v).#endif..
11783 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20  ../*.** Journal 
11784 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68  files begin with
11785 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
11786 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68  agic string.  Th
11787 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62  e data.** was ob
11788 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76  tained from /dev
11789 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20  /random.  It is 
1178a 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73  used only as a s
1178b 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a  anity check..**.
1178c 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e  ** Since version
1178d 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72   2.8.0, the jour
1178e 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61  nal format conta
1178f 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  ins additional s
11790 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e  anity.** checkin
11791 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  g information.  
11792 49 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69  If the power fai
11793 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75  ls while the jou
11794 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a  rnal is being.**
11795 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72   written, semi-r
11796 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61  andom garbage da
11797 74 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20  ta might appear 
11798 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a  in the journal.*
11799 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77  * file after pow
1179a 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20  er is restored. 
1179b 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69   If an attempt i
1179c 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74  s then made.** t
1179d 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e  o roll the journ
1179e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74  al back, the dat
1179f 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63  abase could be c
117a0 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61  orrupted.  The a
117a1 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e  dditional.** san
117a2 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74  ity checking dat
117a3 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  a is an attempt 
117a4 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20  to discover the 
117a5 67 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a  garbage in the.*
117a6 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67  * journal and ig
117a7 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  nore it..**.** T
117a8 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  he sanity checki
117a9 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ng information f
117aa 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e  or the new journ
117ab 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73  al format consis
117ac 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69  ts.** of a 32-bi
117ad 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61  t checksum on ea
117ae 63 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e  ch page of data.
117af 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63    The checksum c
117b0 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68  overs both.** th
117b1 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e  e page number an
117b2 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61  d the pPager->pa
117b3 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20  geSize bytes of 
117b4 64 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67  data for the pag
117b5 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d  e..** This cksum
117b6 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
117b7 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64  to a 32-bit rand
117b8 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70  om value that ap
117b9 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20  pears in the.** 
117ba 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67  journal file rig
117bb 68 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61  ht after the hea
117bc 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d  der.  The random
117bd 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20   initializer is 
117be 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65  important,.** be
117bf 63 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61  cause garbage da
117c0 74 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ta that appears 
117c1 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  at the end of a 
117c2 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c  journal is likel
117c3 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77  y.** data that w
117c4 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72  as once in other
117c5 20 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65   files that have
117c6 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65   now been delete
117c7 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61  d.  If the.** ga
117c8 72 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20  rbage data came 
117c9 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65  from an obsolete
117ca 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74   journal file, t
117cb 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67  he checksums mig
117cc 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74  ht.** be correct
117cd 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61  .  But by initia
117ce 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b  lizing the check
117cf 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61  sum to random va
117d0 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20  lue which.** is 
117d1 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76  different for ev
117d2 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20  ery journal, we 
117d3 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69  minimize that ri
117d4 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  sk..*/.static co
117d5 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
117d6 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  r aJournalMagic[
117d7 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78  ] = {.  0xd9, 0x
117d8 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20  d5, 0x05, 0xf9, 
117d9 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33  0x20, 0xa1, 0x63
117da 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a  , 0xd7,.};../*.*
117db 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68  * The size of th
117dc 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72  e of each page r
117dd 65 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75  ecord in the jou
117de 72 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79  rnal is given by
117df 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
117e0 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66  g macro..*/.#def
117e1 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  ine JOURNAL_PG_S
117e2 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61  Z(pPager)  ((pPa
117e3 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b  ger->pageSize) +
117e4 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a   8)../*.** The j
117e5 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69  ournal header si
117e6 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ze for this page
117e7 72 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c  r. This is usual
117e8 6c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20  ly the same .** 
117e9 73 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65  size as a single
117ea 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65   disk sector. Se
117eb 65 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72  e also setSector
117ec 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Size()..*/.#defi
117ed 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  ne JOURNAL_HDR_S
117ee 5a 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65  Z(pPager) (pPage
117ef 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a  r->sectorSize)..
117f0 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20  /*.** The macro 
117f1 4d 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66  MEMDB is true if
117f2 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
117f3 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  with an in-memor
117f4 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57  y database..** W
117f5 65 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d  e do this as a m
117f6 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20  acro so that if 
117f7 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  the SQLITE_OMIT_
117f8 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69  MEMORYDB macro i
117f9 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61  s set,.** the va
117fa 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c  lue of MEMDB wil
117fb 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20  l be a constant 
117fc 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  and the compiler
117fd 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a   will optimize.*
117fe 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20  * out code that 
117ff 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63  would never exec
11800 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ute..*/.#ifdef S
11801 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
11802 59 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d  YDB.# define MEM
11803 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66  DB 0.#else.# def
11804 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72  ine MEMDB pPager
11805 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a  ->memDb.#endif..
11806 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
11807 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d  m legal page num
11808 62 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31  ber is (2^31 - 1
11809 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )..*/.#define PA
1180a 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34  GER_MAX_PGNO 214
1180b 37 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66  7483647..#ifndef
1180c 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55   NDEBUG ./*.** U
1180d 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73  sage:.**.**   as
1180e 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67  sert( assert_pag
1180f 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29  er_state(pPager)
11810 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   );.*/.static in
11811 74 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73  t assert_pager_s
11812 74 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67  tate(Pager *pPag
11813 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d  er){..  /* A tem
11814 70 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73  p-file is always
11815 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53   in PAGER_EXCLUS
11816 49 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e  IVE or PAGER_SYN
11817 43 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20  CED state. */.  
11818 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
11819 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70  tempFile==0 || p
1181a 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
1181b 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b  GER_EXCLUSIVE );
1181c 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67  ..  /* The chang
1181d 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20  eCountDone flag 
1181e 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f  is always set fo
1181f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a  r temp-files */.
11820 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
11821 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c  ->tempFile==0 ||
11822 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
11823 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72  ountDone );..  r
11824 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
11825 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
11826 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65  true if it is ne
11827 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65  cessary to write
11828 20 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20   page *pPg into 
11829 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
1182a 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73  .** A page needs
1182b 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69   to be written i
1182c 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  nto the sub-jour
1182d 6e 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69  nal if there exi
1182e 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f  sts one.** or mo
1182f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e  re open savepoin
11830 74 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a  ts for which:.**
11831 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65  .**   * The page
11832 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20  -number is less 
11833 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
11834 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
11835 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20  nOrig, and.**   
11836 2a 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73  * The bit corres
11837 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70  ponding to the p
11838 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f  age-number is no
11839 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20  t set in.**     
1183a 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70  PagerSavepoint.p
1183b 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a  InSavepoint..*/.
1183c 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52  static int subjR
1183d 65 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64  equiresPage(PgHd
1183e 72 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20  r *pPg){.  Pgno 
1183f 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f  pgno = pPg->pgno
11840 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
11841 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
11842 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
11843 69 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e  i=0; i<pPager->n
11844 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b  Savepoint; i++){
11845 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f  .    PagerSavepo
11846 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72  int *p = &pPager
11847 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b  ->aSavepoint[i];
11848 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69  .    if( p->nOri
11849 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71  g>=pgno && 0==sq
1184a 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
1184b 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c  p->pInSavepoint,
1184c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20   pgno) ){.      
1184d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1184e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1184f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
11850 74 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65  true if the page
11851 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
11852 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
11853 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
11854 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48  ageInJournal(PgH
11855 64 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75  dr *pPg){.  retu
11856 72 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  rn sqlite3Bitvec
11857 54 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72  Test(pPg->pPager
11858 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50  ->pInJournal, pP
11859 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a  g->pgno);.}../*.
1185a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74  ** Read a 32-bit
1185b 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68   integer from th
1185c 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73  e given file des
1185d 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20  criptor.  Store 
1185e 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74  the integer.** t
1185f 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a  hat is read in *
11860 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51  pRes.  Return SQ
11861 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
11862 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72  thing worked, or
11863 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64   an.** error cod
11864 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67  e is something g
11865 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
11866 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20   All values are 
11867 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61  stored on disk a
11868 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f  s big-endian..*/
11869 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
1186a 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66  32bits(sqlite3_f
1186b 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66  ile *fd, i64 off
1186c 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b  set, u32 *pRes){
1186d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1186e 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63   ac[4];.  int rc
1186f 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
11870 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28  (fd, ac, sizeof(
11871 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  ac), offset);.  
11872 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11873 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d  K ){.    *pRes =
11874 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
11875 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  (ac);.  }.  retu
11876 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11877 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69  Write a 32-bit i
11878 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74  nteger into a st
11879 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62  ring buffer in b
1187a 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f  ig-endian byte o
1187b 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rder..*/.#define
1187c 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20   put32bits(A,B) 
1187d 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65   sqlite3Put4byte
1187e 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a  ((u8*)A,B)../*.*
1187f 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74  * Write a 32-bit
11880 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68   integer into th
11881 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73  e given file des
11882 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e  criptor.  Return
11883 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e   SQLITE_OK.** on
11884 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65   success or an e
11885 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d  rror code is som
11886 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ething goes wron
11887 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
11888 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c   write32bits(sql
11889 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69  ite3_file *fd, i
1188a 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76  64 offset, u32 v
1188b 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34  al){.  char ac[4
1188c 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61  ];.  put32bits(a
1188d 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72  c, val);.  retur
1188e 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  n sqlite3OsWrite
1188f 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73  (fd, ac, 4, offs
11890 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  et);.}../*.** Th
11891 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  e argument to th
11892 69 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69  is macro is a fi
11893 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74  le descriptor (t
11894 79 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ype sqlite3_file
11895 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20  *)..** Return 0 
11896 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65  if it is not ope
11897 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28  n, or non-zero (
11898 62 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74  but not 1) if it
11899 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   is..**.** This 
1189a 69 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65  is so that expre
1189b 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72  ssions can be wr
1189c 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20  itten as:.**.** 
1189d 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
1189e 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e  ger->jfd) ){ ...
1189f 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f  .**.** instead o
118a0 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50  f.**.**   if( pP
118a1 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68  ager->jfd->pMeth
118a2 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64  ods ){ ....*/.#d
118a3 65 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64  efine isOpen(pFd
118a4 29 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f  ) ((pFd)->pMetho
118a5 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69  ds)../*.** If fi
118a6 6c 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20  le pFd is open, 
118a7 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e  call sqlite3OsUn
118a8 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f  lock() on it..*/
118a9 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e  .static int osUn
118aa 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
118ab 65 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63  e *pFd, int eLoc
118ac 6b 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65  k){.  if( !isOpe
118ad 6e 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65  n(pFd) ){.    re
118ae 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
118af 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
118b0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64  ite3OsUnlock(pFd
118b1 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a  , eLock);.}../*.
118b2 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
118b3 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74   determines whet
118b4 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61  her or not the a
118b5 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
118b6 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20  mization.** can 
118b7 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69  be used with thi
118b8 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74  s pager. The opt
118b9 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65  imization can be
118ba 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20   used if:.**.** 
118bb 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72   (a) the value r
118bc 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76  eturned by OsDev
118bd 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
118be 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74  cs() indicates t
118bf 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61  hat.**      a da
118c0 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20  tabase page may 
118c1 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69  be written atomi
118c2 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28  cally, and.**  (
118c3 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  b) the value ret
118c4 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f  urned by OsSecto
118c5 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20  rSize() is less 
118c6 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a  than or equal.**
118c7 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67        to the pag
118c8 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e size..**.** Th
118c9 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
118ca 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e  s also always en
118cb 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72  abled for tempor
118cc 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73  ary files. It is
118cd 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20  .** an error to 
118ce 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
118cf 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20  on if pPager is 
118d0 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d  opened on an in-
118d1 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61  memory.** databa
118d2 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
118d3 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61   optimization ca
118d4 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20  nnot be used, 0 
118d5 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
118d6 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a  it can be used,.
118d7 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ** then the valu
118d8 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
118d9 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f  e size of the jo
118da 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20  urnal file when 
118db 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72  it.** contains r
118dc 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72  ollback data for
118dd 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67   exactly one pag
118de 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
118df 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
118e0 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69  C_WRITE.static i
118e1 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  nt jrnlBufferSiz
118e2 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
118e3 7b 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d  {.  assert( !MEM
118e4 44 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61  DB );.  if( !pPa
118e5 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
118e6 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20  .    int dc;    
118e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118e8 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65         /* Device
118e9 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
118ea 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63   */.    int nSec
118eb 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  tor;            
118ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
118ed 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  tor size */.    
118ee 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20  int szPage;     
118ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118f0 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a    /* Page size *
118f1 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  /..    assert( i
118f2 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
118f3 29 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71  ) );.    dc = sq
118f4 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
118f5 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61  racteristics(pPa
118f6 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53  ger->fd);.    nS
118f7 65 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e  ector = pPager->
118f8 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20  sectorSize;.    
118f9 73 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d  szPage = pPager-
118fa 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20  >pageSize;..    
118fb 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f  assert(SQLITE_IO
118fc 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28  CAP_ATOMIC512==(
118fd 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73  512>>8));.    as
118fe 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41  sert(SQLITE_IOCA
118ff 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35  P_ATOMIC64K==(65
11900 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66  536>>8));.    if
11901 28 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45  ( 0==(dc&(SQLITE
11902 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73  _IOCAP_ATOMIC|(s
11903 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53  zPage>>8)) || nS
11904 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b  ector>szPage) ){
11905 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
11906 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
11907 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  turn JOURNAL_HDR
11908 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f  _SZ(pPager) + JO
11909 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67  URNAL_PG_SZ(pPag
1190a 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  er);.}.#endif../
1190b 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43  *.** If SQLITE_C
1190c 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65  HECK_PAGES is de
1190d 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f  fined then we do
1190e 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65   some sanity che
1190f 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20  cking.** on the 
11910 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61  cache using a ha
11911 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  sh function.  Th
11912 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  is is used for t
11913 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65  esting.** and de
11914 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f  bugging only..*/
11915 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
11916 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a  HECK_PAGES./*.**
11917 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74   Return a 32-bit
11918 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67   hash of the pag
11919 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65  e data for pPage
1191a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20  ..*/.static u32 
1191b 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69  pager_datahash(i
1191c 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e  nt nByte, unsign
1191d 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b  ed char *pData){
1191e 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b  .  u32 hash = 0;
1191f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
11920 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b  i=0; i<nByte; i+
11921 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28  +){.    hash = (
11922 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61  hash*1039) + pDa
11923 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  ta[i];.  }.  ret
11924 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74  urn hash;.}.stat
11925 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67  ic u32 pager_pag
11926 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61  ehash(PgHdr *pPa
11927 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61  ge){.  return pa
11928 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61  ger_datahash(pPa
11929 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65  ge->pPager->page
1192a 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20  Size, (unsigned 
1192b 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44  char *)pPage->pD
1192c 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ata);.}.static v
1192d 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61  oid pager_set_pa
1192e 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50  gehash(PgHdr *pP
1192f 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70  age){.  pPage->p
11930 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f  ageHash = pager_
11931 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b  pagehash(pPage);
11932 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48  .}../*.** The CH
11933 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74  ECK_PAGE macro t
11934 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73  akes a PgHdr* as
11935 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66   an argument. If
11936 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
11937 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  GES.** is define
11938 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73  d, and NDEBUG is
11939 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e   not defined, an
1193a 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
1193b 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68  ent checks.** th
1193c 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65  at the page is e
1193d 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73  ither dirty or s
1193e 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65  till matches the
1193f 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65   calculated page
11940 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  -hash..*/.#defin
11941 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20  e CHECK_PAGE(x) 
11942 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61  checkPage(x).sta
11943 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61  tic void checkPa
11944 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ge(PgHdr *pPg){.
11945 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
11946 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
11947 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70   assert( !pPg->p
11948 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65  ageHash || pPage
11949 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20  r->errCode.     
1194a 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26   || (pPg->flags&
1194b 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20  PGHDR_DIRTY) || 
1194c 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70  pPg->pageHash==p
1194d 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
1194e 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23  g) );.}..#else.#
1194f 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74  define pager_dat
11950 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64  ahash(X,Y)  0.#d
11951 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65  efine pager_page
11952 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69  hash(X)  0.#defi
11953 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29  ne CHECK_PAGE(x)
11954 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
11955 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a  TE_CHECK_PAGES *
11956 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  /../*.** When th
11957 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65  is is called the
11958 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
11959 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d  r pager pPager m
1195a 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20  ust be open..** 
1195b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  This function at
1195c 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61  tempts to read a
1195d 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1195e 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74  file name from t
1195f 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  he .** end of th
11960 65 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73  e file and, if s
11961 75 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65  uccessful, copie
11962 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  s it into memory
11963 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79   supplied .** by
11964 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65   the caller. See
11965 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20   comments above 
11966 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e  writeMasterJourn
11967 61 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72  al() for the for
11968 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73  mat.** used to s
11969 74 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f  tore a master jo
1196a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
1196b 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  at the end of a 
1196c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a  journal file..**
1196d 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74  .** zMaster must
1196e 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
1196f 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e  er of at least n
11970 4d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c  Master bytes all
11971 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65  ocated by.** the
11972 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68   caller. This sh
11973 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f  ould be sqlite3_
11974 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  vfs.mxPathname+1
11975 20 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72   (to ensure ther
11976 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73  e is.** enough s
11977 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68  pace to write th
11978 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11979 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d   name). If the m
1197a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  aster journal.**
1197b 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75   name in the jou
1197c 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74  rnal is longer t
1197d 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65  han nMaster byte
1197e 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a  s (including a.*
1197f 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  * nul-terminator
11980 29 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20  ), then this is 
11981 68 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f  handled as if no
11982 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11983 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65  name.** were pre
11984 73 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72  sent in the jour
11985 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  nal..**.** If a 
11986 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11987 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73  ile name is pres
11988 65 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ent at the end o
11989 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  f the journal.**
1198a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69   file, then it i
1198b 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68  s copied into th
1198c 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  e buffer pointed
1198d 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20   to by zMaster. 
1198e 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  A.** nul-termina
1198f 74 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65  tor byte is appe
11990 6e 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66  nded to the buff
11991 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  er following the
11992 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e   master.** journ
11993 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a  al file name..**
11994 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74  .** If it is det
11995 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20  ermined that no 
11996 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
11997 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73  ile name is pres
11998 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b  ent .** zMaster[
11999 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61  0] is set to 0 a
1199a 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
1199b 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
1199c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1199d 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72  while reading fr
1199e 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  om the journal f
1199f 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a  ile, an SQLite.*
119a0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  * error code is 
119a1 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
119a2 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74  tic int readMast
119a3 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  erJournal(sqlite
119a4 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63  3_file *pJrnl, c
119a5 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33  har *zMaster, u3
119a6 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e  2 nMaster){.  in
119a7 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
119a8 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
119a9 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32  rn code */.  u32
119aa 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20   len;           
119ab 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
119ac 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61  h in bytes of ma
119ad 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
119ae 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20  e */.  i64 szJ; 
119af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119b0 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20    /* Total size 
119b1 69 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72  in bytes of jour
119b2 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a  nal file pJrnl *
119b3 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20  /.  u32 cksum;  
119b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
119b5 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61  * MJ checksum va
119b6 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f  lue read from jo
119b7 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75  urnal */.  u32 u
119b8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
119b9 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65        /* Unsigne
119ba 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  d loop counter *
119bb 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
119bc 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f  r aMagic[8];   /
119bd 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f  * A buffer to ho
119be 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61  ld the magic hea
119bf 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72  der */.  zMaster
119c0 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69  [0] = '\0';..  i
119c1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
119c2 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
119c3 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a  eSize(pJrnl, &sz
119c4 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36  J)).   || szJ<16
119c5 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b  .   || SQLITE_OK
119c6 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69  !=(rc = read32bi
119c7 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36  ts(pJrnl, szJ-16
119c8 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c  , &len)).   || l
119c9 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20  en>=nMaster .   
119ca 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  || SQLITE_OK!=(r
119cb 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70  c = read32bits(p
119cc 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63  Jrnl, szJ-12, &c
119cd 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c  ksum)).   || SQL
119ce 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
119cf 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e  lite3OsRead(pJrn
119d0 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a  l, aMagic, 8, sz
119d1 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63  J-8)).   || memc
119d2 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72  mp(aMagic, aJour
119d3 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20  nalMagic, 8).   
119d4 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  || SQLITE_OK!=(r
119d5 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
119d6 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72  d(pJrnl, zMaster
119d7 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65  , len, szJ-16-le
119d8 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  n)).  ){.    ret
119d9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
119da 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65  * See if the che
119db 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68  cksum matches th
119dc 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
119dd 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75   name */.  for(u
119de 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b  =0; u<len; u++){
119df 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d  .    cksum -= zM
119e0 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20  aster[u];.  }.  
119e1 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20  if( cksum ){.   
119e2 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b   /* If the check
119e3 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20  sum doesn't add 
119e4 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20  up, then one or 
119e5 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b  more of the disk
119e6 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20   sectors.    ** 
119e7 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d  containing the m
119e8 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
119e9 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70  lename is corrup
119ea 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a  ted. This means.
119eb 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c      ** definitel
119ec 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20  y roll back, so 
119ed 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49  just return SQLI
119ee 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74  TE_OK and report
119ef 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20   a (nul).    ** 
119f0 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66  master-journal f
119f1 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a  ilename..    */.
119f2 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d      len = 0;.  }
119f3 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20  .  zMaster[len] 
119f4 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65  = '\0';.   .  re
119f5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
119f6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
119f7 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  the offset of th
119f8 65 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72  e sector boundar
119f9 79 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74  y at or immediat
119fa 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  ely .** followin
119fb 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70  g the value in p
119fc 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
119fd 66 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65  f, assuming a se
119fe 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66  ctor .** size of
119ff 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
11a00 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  ize bytes..**.**
11a01 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f   i.e for a secto
11a02 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a  r size of 512:.*
11a03 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75  *.**   Pager.jou
11a04 72 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20  rnalOff         
11a05 20 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   Return value.**
11a06 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
11a07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a08 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
11a09 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
11a0a 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20            0.**  
11a0b 20 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20   512            
11a0c 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a             512.*
11a0d 2a 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20  *   100         
11a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31                51
11a0f 32 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20  2.**   2000     
11a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a11 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61   2048.** .*/.sta
11a12 74 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48  tic i64 journalH
11a13 64 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a  drOffset(Pager *
11a14 70 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f  pPager){.  i64 o
11a15 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34  ffset = 0;.  i64
11a16 20 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75   c = pPager->jou
11a17 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63  rnalOff;.  if( c
11a18 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d   ){.    offset =
11a19 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f   ((c-1)/JOURNAL_
11a1a 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b  HDR_SZ(pPager) +
11a1b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44   1) * JOURNAL_HD
11a1c 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20  R_SZ(pPager);.  
11a1d 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73  }.  assert( offs
11a1e 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  et%JOURNAL_HDR_S
11a1f 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a  Z(pPager)==0 );.
11a20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74    assert( offset
11a21 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >=c );.  assert(
11a22 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52   (offset-c)<JOUR
11a23 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
11a24 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f  r) );.  return o
11a25 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ffset;.}../*.** 
11a26 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
11a27 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68   must be open wh
11a28 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
11a29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   is called..**.*
11a2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
11a2b 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
11a2c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
11a2d 61 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74  as not been writ
11a2e 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e  ten to.** within
11a2f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
11a30 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69  nsaction (i.e. i
11a31 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  f Pager.journalO
11a32 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  ff==0)..**.** If
11a33 20 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e   doTruncate is n
11a34 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50  on-zero or the P
11a35 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ager.journalSize
11a36 4c 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69  Limit variable i
11a37 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74  s.** set to 0, t
11a38 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65  hen truncate the
11a39 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f   journal file to
11a3a 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73   zero bytes in s
11a3b 69 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  ize. Otherwise,.
11a3c 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62  ** zero the 28-b
11a3d 79 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68  yte header at th
11a3e 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a  e start of the j
11a3f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20  ournal file. In 
11a40 65 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a  either case, .**
11a41 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73   if the pager is
11a42 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20   not in no-sync 
11a43 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a  mode, sync the j
11a44 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65  ournal file imme
11a45 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65  diately .** afte
11a46 72 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75  r writing or tru
11a47 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a  ncating it..**.*
11a48 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e  * If Pager.journ
11a49 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73  alSizeLimit is s
11a4a 65 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65  et to a positive
11a4b 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  , non-zero value
11a4c 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  , and.** followi
11a4d 6e 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f  ng the truncatio
11a4e 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73  n or zeroing des
11a4f 63 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65  cribed above the
11a50 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a   size of the .**
11a51 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
11a52 20 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72   bytes is larger
11a53 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65   than this value
11a54 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
11a55 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
11a56 69 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75  ile to Pager.jou
11a57 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79  rnalSizeLimit by
11a58 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c  tes. The journal
11a59 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f   file does.** no
11a5a 74 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e  t need to be syn
11a5b 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ced following th
11a5c 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a  is operation..**
11a5d 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72  .** If an IO err
11a5e 6f 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64  or occurs, aband
11a5f 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e  on processing an
11a60 64 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20  d return the IO 
11a61 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f  error code..** O
11a62 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
11a63 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73   SQLITE_OK..*/.s
11a64 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f  tatic int zeroJo
11a65 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a  urnalHdr(Pager *
11a66 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72  pPager, int doTr
11a67 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72  uncate){.  int r
11a68 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
11a69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a6a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11a6b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
11a6c 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
11a6d 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
11a6e 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f    if( pPager->jo
11a6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20  urnalOff ){.    
11a70 63 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74  const i64 iLimit
11a71 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
11a72 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20  alSizeLimit;    
11a73 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f  /* Local cache o
11a74 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f  f jsl */..    IO
11a75 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52  TRACE(("JZEROHDR
11a76 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29   %p\n", pPager))
11a77 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63  .    if( doTrunc
11a78 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30  ate || iLimit==0
11a79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
11a7a 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
11a7b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29  (pPager->jfd, 0)
11a7c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
11a7d 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
11a7e 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d  char zeroHdr[28]
11a7f 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63   = {0};.      rc
11a80 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
11a81 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a  e(pPager->jfd, z
11a82 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a  eroHdr, sizeof(z
11a83 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20  eroHdr), 0);.   
11a84 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
11a85 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61  QLITE_OK && !pPa
11a86 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20  ger->noSync ){. 
11a87 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11a88 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e  3OsSync(pPager->
11a89 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43  jfd, SQLITE_SYNC
11a8a 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72  _DATAONLY|pPager
11a8b 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20  ->sync_flags);. 
11a8c 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20     }..    /* At 
11a8d 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74  this point the t
11a8e 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
11a8f 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20  mmitted but the 
11a90 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20  write lock .    
11a91 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64  ** is still held
11a92 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66   on the file. If
11a93 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65   there is a size
11a94 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65   limit configure
11a95 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68  d for .    ** th
11a96 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  e persistent jou
11a97 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75  rnal and the jou
11a98 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e  rnal file curren
11a99 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72  tly consumes mor
11a9a 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74  e.    ** space t
11a9b 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61  han that limit a
11a9c 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63  llows for, trunc
11a9d 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72  ate it now. Ther
11a9e 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20  e is no need.   
11a9f 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20   ** to sync the 
11aa0 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  file following t
11aa1 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20  his operation.. 
11aa2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63     */.    if( rc
11aa3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
11aa4 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20  Limit>0 ){.     
11aa5 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72   i64 sz;.      r
11aa6 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
11aa7 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66  eSize(pPager->jf
11aa8 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69  d, &sz);.      i
11aa9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11aaa 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b   && sz>iLimit ){
11aab 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11aac 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
11aad 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69  pPager->jfd, iLi
11aae 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  mit);.      }.  
11aaf 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
11ab0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
11ab1 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d  e journal file m
11ab2 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e  ust be open when
11ab3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
11ab4 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e   called. A journ
11ab5 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f  al.** header (JO
11ab6 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74  URNAL_HDR_SZ byt
11ab7 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69  es) is written i
11ab8 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
11ab9 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63  file at the.** c
11aba 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e  urrent location.
11abb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61  .**.** The forma
11abc 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61  t for the journa
11abd 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66  l header is as f
11abe 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62  ollows:.** - 8 b
11abf 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e  ytes: Magic iden
11ac0 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  tifying journal 
11ac1 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62  format..** - 4 b
11ac2 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20  ytes: Number of 
11ac3 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e  records in journ
11ac4 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e  al, or -1 no-syn
11ac5 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a  c mode is on..**
11ac6 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64   - 4 bytes: Rand
11ac7 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66  om number used f
11ac8 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a  or page hash..**
11ac9 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74   - 4 bytes: Init
11aca 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67  ial database pag
11acb 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20  e count..** - 4 
11acc 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69  bytes: Sector si
11acd 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70  ze used by the p
11ace 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74  rocess that wrot
11acf 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a  e this journal..
11ad0 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61  ** - 4 bytes: Da
11ad1 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
11ad2 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65  ..** .** Followe
11ad3 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44  d by (JOURNAL_HD
11ad4 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73  R_SZ - 28) bytes
11ad5 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65   of unused space
11ad6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11ad7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
11ad8 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11ad9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
11ada 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
11adb 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11adc 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  code */.  char *
11add 7a 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72  zHeader = pPager
11ade 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a  ->pTmpSpace;  /*
11adf 20 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65   Temporary space
11ae0 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68   used to build h
11ae1 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e  eader */.  u32 n
11ae2 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d  Header = pPager-
11ae3 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f  >pageSize;     /
11ae4 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
11ae5 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a   pointed to by z
11ae6 48 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20  Header */.  u32 
11ae7 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20  nWrite;         
11ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ae9 2f 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64  /* Bytes of head
11aea 65 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65  er sector writte
11aeb 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20  n */.  int ii;  
11aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
11aee 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20  op counter */.. 
11aef 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
11af0 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20  pPager->jfd) ); 
11af1 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20       /* Journal 
11af2 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65  file must be ope
11af3 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65  n. */..  if( nHe
11af4 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52  ader>JOURNAL_HDR
11af5 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20  _SZ(pPager) ){. 
11af6 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55     nHeader = JOU
11af7 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11af8 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  er);.  }..  /* I
11af9 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  f there are acti
11afa 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e  ve savepoints an
11afb 64 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65  d any of them we
11afc 72 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a  re created .  **
11afd 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20   since the most 
11afe 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68  recent journal h
11aff 65 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65  eader was writte
11b00 6e 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20  n, update the . 
11b01 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69   ** PagerSavepoi
11b02 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69  nt.iHdrOffset fi
11b03 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  elds now..  */. 
11b04 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50   for(ii=0; ii<pP
11b05 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
11b06 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
11b07 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f   pPager->aSavepo
11b08 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73  int[ii].iHdrOffs
11b09 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  et==0 ){.      p
11b0a 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
11b0b 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74  t[ii].iHdrOffset
11b0c 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
11b0d 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d  alOff;.    }.  }
11b0e 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ..  pPager->jour
11b0f 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d  nalHdr = pPager-
11b10 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f  >journalOff = jo
11b11 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70  urnalHdrOffset(p
11b12 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20  Pager);..  /* . 
11b13 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52   ** Write the nR
11b14 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e  ec Field - the n
11b15 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65  umber of page re
11b16 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f  cords that follo
11b17 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72  w this.  ** jour
11b18 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d  nal header. Norm
11b19 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72  ally, zero is wr
11b1a 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61  itten to this va
11b1b 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65  lue at this time
11b1c 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65  ..  ** After the
11b1d 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64   records are add
11b1e 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ed to the journa
11b1f 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e  l (and the journ
11b20 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a  al synced, .  **
11b21 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63   if in full-sync
11b22 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f   mode), the zero
11b23 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20   is overwritten 
11b24 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75  with the true nu
11b25 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63  mber.  ** of rec
11b26 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f  ords (see syncJo
11b27 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20  urnal())..  **. 
11b28 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74   ** A faster alt
11b29 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77  ernative is to w
11b2a 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20  rite 0xFFFFFFFF 
11b2b 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c  to the nRec fiel
11b2c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61  d. When.  ** rea
11b2d 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ding the journal
11b2e 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c   this value tell
11b2f 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75  s SQLite to assu
11b30 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  me that the.  **
11b31 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75   rest of the jou
11b32 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69  rnal file contai
11b33 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65  ns valid page re
11b34 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75  cords. This assu
11b35 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64  mption.  ** is d
11b36 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20  angerous, as if 
11b37 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72  a failure occurr
11b38 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e  ed whilst writin
11b39 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  g to the journal
11b3a 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61  .  ** file it ma
11b3b 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67  y contain some g
11b3c 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65  arbage data. The
11b3d 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61  re are two scena
11b3e 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20  rios.  ** where 
11b3f 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65  this risk can be
11b40 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20   ignored:.  **. 
11b41 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65   **   * When the
11b42 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d   pager is in no-
11b43 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75  sync mode. Corru
11b44 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77  ption can follow
11b45 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65   a.  **     powe
11b46 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69  r failure in thi
11b47 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20  s case anyway.. 
11b48 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65   **.  **   * Whe
11b49 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43  n the SQLITE_IOC
11b4a 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66  AP_SAFE_APPEND f
11b4b 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73  lag is set. This
11b4c 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a   guarantees.  **
11b4d 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67       that garbag
11b4e 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20  e data is never 
11b4f 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
11b50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20  journal file..  
11b51 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  */.  assert( isO
11b52 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
11b53 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  || pPager->noSyn
11b54 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67  c );.  if( (pPag
11b55 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28  er->noSync) || (
11b56 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
11b57 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
11b58 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20  ALMODE_MEMORY). 
11b59 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44    || (sqlite3OsD
11b5a 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
11b5b 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
11b5c 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
11b5d 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b  FE_APPEND) .  ){
11b5e 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61  .    memcpy(zHea
11b5f 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  der, aJournalMag
11b60 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72  ic, sizeof(aJour
11b61 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20  nalMagic));.    
11b62 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
11b63 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
11b64 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66  alMagic)], 0xfff
11b65 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b  fffff);.  }else{
11b66 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61  .    memset(zHea
11b67 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  der, 0, sizeof(a
11b68 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29  JournalMagic)+4)
11b69 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
11b6a 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73  random check-has
11b6b 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f  h initialiser */
11b6c 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64   .  sqlite3_rand
11b6d 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50  omness(sizeof(pP
11b6e 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
11b6f 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  , &pPager->cksum
11b70 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69  Init);.  put32bi
11b71 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  ts(&zHeader[size
11b72 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
11b73 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b  )+4], pPager->ck
11b74 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54  sumInit);.  /* T
11b75 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62  he initial datab
11b76 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75  ase size */.  pu
11b77 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
11b78 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
11b79 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65  Magic)+8], pPage
11b7a 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a  r->dbOrigSize);.
11b7b 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64    /* The assumed
11b7c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72   sector size for
11b7d 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f   this process */
11b7e 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48  .  put32bits(&zH
11b7f 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
11b80 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c  urnalMagic)+12],
11b81 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
11b82 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  ize);..  /* The 
11b83 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70  page size */.  p
11b84 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
11b85 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11b86 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61  lMagic)+16], pPa
11b87 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
11b88 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69  .  /* Initializi
11b89 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74  ng the tail of t
11b8a 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74  he buffer is not
11b8b 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65   necessary.  Eve
11b8c 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72  rything.  ** wor
11b8d 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66  ks find if the f
11b8e 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28  ollowing memset(
11b8f 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42  ) is omitted.  B
11b90 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a  ut initializing.
11b91 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20    ** the memory 
11b92 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e  prevents valgrin
11b93 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69  d from complaini
11b94 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69  ng, so we are wi
11b95 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61  lling to.  ** ta
11b96 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e  ke the performan
11b97 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d  ce hit..  */.  m
11b98 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73  emset(&zHeader[s
11b99 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
11b9a 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20  gic)+20], 0,.   
11b9b 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73        nHeader-(s
11b9c 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
11b9d 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a  gic)+20));..  /*
11b9e 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69   In theory, it i
11b9f 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79  s only necessary
11ba0 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38   to write the 28
11ba1 20 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20   bytes that the 
11ba2 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65  .  ** journal he
11ba3 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f  ader consumes to
11ba4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11ba5 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63  e here. Then inc
11ba6 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a  rement the .  **
11ba7 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
11ba8 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f  f variable by JO
11ba9 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20  URNAL_HDR_SZ so 
11baa 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20  that the next . 
11bab 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72   ** record is wr
11bac 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c  itten to the fol
11bad 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c  lowing sector (l
11bae 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20  eaving a gap in 
11baf 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68  the file.  ** th
11bb0 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69  at will be impli
11bb1 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20  citly filled in 
11bb2 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a  by the OS)..  **
11bb3 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74  .  ** However it
11bb4 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76   has been discov
11bb5 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d  ered that on som
11bb6 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70  e systems this p
11bb7 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a  attern can .  **
11bb8 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c   be significantl
11bb9 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f  y slower than co
11bba 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69  ntiguously writi
11bbb 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66  ng data to the f
11bbc 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69  ile,.  ** even i
11bbd 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70  f that means exp
11bbe 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20  licitly writing 
11bbf 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63  data to the bloc
11bc0 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52  k of .  ** (JOUR
11bc1 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29  NAL_HDR_SZ - 28)
11bc2 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c   bytes that will
11bc3 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f   not be used. So
11bc4 20 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20   that is what.  
11bc5 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a  ** is done. .  *
11bc6 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20  *.  ** The loop 
11bc7 69 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65  is required here
11bc8 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63   in case the sec
11bc9 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67  tor-size is larg
11bca 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a  er than the .  *
11bcb 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  * database page 
11bcc 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20  size. Since the 
11bcd 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69  zHeader buffer i
11bce 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67  s only Pager.pag
11bcf 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73  eSize.  ** bytes
11bd0 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74   in size, more t
11bd1 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20  han one call to 
11bd2 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29  sqlite3OsWrite()
11bd3 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64   may be required
11bd4 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74  .  ** to populat
11bd5 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75  e the entire jou
11bd6 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74  rnal header sect
11bd7 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28  or..  */ .  for(
11bd8 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51  nWrite=0; rc==SQ
11bd9 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c  LITE_OK&&nWrite<
11bda 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
11bdb 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d  Pager); nWrite+=
11bdc 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f  nHeader){.    IO
11bdd 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20  TRACE(("JHDR %p 
11bde 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
11bdf 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
11be0 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29  nalHdr, nHeader)
11be1 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ).    rc = sqlit
11be2 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
11be3 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20  ->jfd, zHeader, 
11be4 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d  nHeader, pPager-
11be5 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20  >journalOff);.  
11be6 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11be7 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b  lOff += nHeader;
11be8 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
11be9 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
11bea 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73  journal file mus
11beb 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74  t be open when t
11bec 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41  his is called. A
11bed 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11bee 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c  file.** (JOURNAL
11bef 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69  _HDR_SZ bytes) i
11bf0 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
11bf1 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
11bf2 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   in the journal.
11bf3 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72  ** file. The cur
11bf4 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e  rent location in
11bf5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11bf6 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a  e is given by.**
11bf7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11bf8 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74  Off. See comment
11bf9 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e  s above function
11bfa 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72   writeJournalHdr
11bfb 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63  () for.** a desc
11bfc 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a  ription of the j
11bfd 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f  ournal header fo
11bfe 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rmat..**.** If t
11bff 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61  he header is rea
11c00 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
11c01 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f  *pNRec is set to
11c02 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
11c03 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66  * page records f
11c04 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65  ollowing this he
11c05 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a  ader and *pDbSiz
11c06 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
11c07 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  size of the.** d
11c08 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74  atabase before t
11c09 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62  he transaction b
11c0a 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20  egan, in pages. 
11c0b 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b  Also, pPager->ck
11c0c 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65  sumInit.** is se
11c0d 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72  t to the value r
11c0e 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ead from the jou
11c0f 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c  rnal header. SQL
11c10 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
11c11 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61  ed.** in this ca
11c12 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
11c13 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11c14 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20  file appears to 
11c15 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51  be corrupted, SQ
11c16 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20  LITE_DONE is.** 
11c17 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e  returned and *pN
11c18 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65  Rec and *PDbSize
11c19 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20   are undefined. 
11c1a 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f   If JOURNAL_HDR_
11c1b 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e  SZ bytes.** cann
11c1c 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20  ot be read from 
11c1d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11c1e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
11c1f 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
11c20 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f  tatic int readJo
11c21 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65  urnalHdr(.  Page
11c22 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
11c23 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
11c24 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  r object */.  in
11c25 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a  t isHot,.  i64 j
11c26 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20  ournalSize,     
11c27 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
11c28 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72  of the open jour
11c29 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65  nal file in byte
11c2a 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65  s */.  u32 *pNRe
11c2b 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
11c2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75      /* OUT: Valu
11c2d 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
11c2e 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20  nRec field */.  
11c2f 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20  u32 *pDbSize    
11c30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11c31 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72  OUT: Value of or
11c32 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
11c33 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b  size field */.){
11c34 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
11c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c36 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
11c37 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
11c38 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20  r aMagic[8];    
11c39 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20   /* A buffer to 
11c3a 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68  hold the magic h
11c3b 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69  eader */.  i64 i
11c3c 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20  HdrOff;         
11c3d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
11c3e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61  t of journal hea
11c3f 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a  der being read *
11c40 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  /..  assert( isO
11c41 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
11c42 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72   );      /* Jour
11c43 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65  nal file must be
11c44 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20   open. */..  /* 
11c45 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f  Advance Pager.jo
11c46 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20  urnalOff to the 
11c47 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78  start of the nex
11c48 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65  t sector. If the
11c49 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  .  ** journal fi
11c4a 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20  le is too small 
11c4b 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20  for there to be 
11c4c 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20  a header stored 
11c4d 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69  at this.  ** poi
11c4e 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  nt, return SQLIT
11c4f 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70  E_DONE..  */.  p
11c50 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11c51 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66  f = journalHdrOf
11c52 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  fset(pPager);.  
11c53 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
11c54 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48  nalOff+JOURNAL_H
11c55 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20  DR_SZ(pPager) > 
11c56 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20  journalSize ){. 
11c57 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11c58 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64  _DONE;.  }.  iHd
11c59 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a  rOff = pPager->j
11c5a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a  ournalOff;..  /*
11c5b 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72   Read in the fir
11c5c 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68  st 8 bytes of th
11c5d 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11c5e 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74  . If they do not
11c5f 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20   match.  ** the 
11c60 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f   magic string fo
11c61 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74  und at the start
11c62 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c   of each journal
11c63 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a   header, return.
11c64 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45    ** SQLITE_DONE
11c65 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
11c66 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
11c67 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f  an error code. O
11c68 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70  therwise,.  ** p
11c69 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  roceed..  */.  i
11c6a 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72  f( isHot || iHdr
11c6b 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  Off!=pPager->jou
11c6c 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72  rnalHdr ){.    r
11c6d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
11c6e 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61  d(pPager->jfd, a
11c6f 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d  Magic, sizeof(aM
11c70 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b  agic), iHdrOff);
11c71 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
11c72 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
11c73 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
11c74 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f  mcmp(aMagic, aJo
11c75 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65  urnalMagic, size
11c76 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29  of(aMagic))!=0 )
11c77 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
11c78 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
11c79 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64  }.  }..  /* Read
11c7a 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65   the first three
11c7b 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f   32-bit fields o
11c7c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
11c7d 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20  ader: The nRec. 
11c7e 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63   ** field, the c
11c7f 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69  hecksum-initiali
11c80 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61  zer and the data
11c81 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65  base size at the
11c82 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74   start.  ** of t
11c83 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
11c84 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
11c85 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67  code if anything
11c86 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a   goes wrong..  *
11c87 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  /.  if( SQLITE_O
11c88 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
11c89 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
11c8a 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65   iHdrOff+8, pNRe
11c8b 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45  c)).   || SQLITE
11c8c 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
11c8d 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11c8e 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26  d, iHdrOff+12, &
11c8f 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
11c90 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45  t)).   || SQLITE
11c91 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
11c92 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11c93 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70  d, iHdrOff+16, p
11c94 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20  DbSize)).  ){.  
11c95 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
11c96 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
11c97 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b  journalOff==0 ){
11c98 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69  .    u32 iPageSi
11c99 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
11c9a 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66    /* Page-size f
11c9b 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  ield of journal 
11c9c 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33  header */.    u3
11c9d 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20  2 iSectorSize;  
11c9e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
11c9f 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20  ctor-size field 
11ca0 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  of journal heade
11ca1 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61  r */.    u16 iPa
11ca2 67 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20  geSize16;       
11ca3 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
11ca4 20 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36   iPageSize in 16
11ca5 2d 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f  -bit variable */
11ca6 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
11ca7 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20  e page-size and 
11ca8 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72  sector-size jour
11ca9 6e 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64  nal header field
11caa 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51  s. */.    if( SQ
11cab 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
11cac 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72  ead32bits(pPager
11cad 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32  ->jfd, iHdrOff+2
11cae 30 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29  0, &iSectorSize)
11caf 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45  ).     || SQLITE
11cb0 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
11cb1 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11cb2 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26  d, iHdrOff+24, &
11cb3 69 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20  iPageSize)).    
11cb4 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
11cb5 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
11cb6 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
11cb7 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f   values read fro
11cb8 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  m the page-size 
11cb9 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20  and sector-size 
11cba 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72  fields.    ** ar
11cbb 65 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20  e within range. 
11cbc 54 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27  To be 'in range'
11cbd 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65  , both values ne
11cbe 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72  ed to be a power
11cbf 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67  .    ** of two g
11cc0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
11cc1 71 75 61 6c 20 74 6f 20 35 31 32 20 6f 72 20 33  qual to 512 or 3
11cc2 32 2c 20 61 6e 64 20 6e 6f 74 20 67 72 65 61 74  2, and not great
11cc3 65 72 20 74 68 61 6e 20 74 68 65 69 72 20 0a 20  er than their . 
11cc4 20 20 20 2a 2a 20 72 65 73 70 65 63 74 69 76 65     ** respective
11cc5 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61   compile time ma
11cc6 78 69 6d 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20  ximum limits..  
11cc7 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 61    */.    if( iPa
11cc8 67 65 53 69 7a 65 3c 35 31 32 20 20 20 20 20 20  geSize<512      
11cc9 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 69              || i
11cca 53 65 63 74 6f 72 53 69 7a 65 3c 33 32 0a 20 20  SectorSize<32.  
11ccb 20 20 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e     || iPageSize>
11ccc 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
11ccd 53 49 5a 45 20 7c 7c 20 69 53 65 63 74 6f 72 53  SIZE || iSectorS
11cce 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53  ize>MAX_SECTOR_S
11ccf 49 5a 45 0a 20 20 20 20 20 7c 7c 20 28 28 69 50  IZE.     || ((iP
11cd0 61 67 65 53 69 7a 65 2d 31 29 26 69 50 61 67 65  ageSize-1)&iPage
11cd1 53 69 7a 65 29 21 3d 30 20 20 20 7c 7c 20 28 28  Size)!=0   || ((
11cd2 69 53 65 63 74 6f 72 53 69 7a 65 2d 31 29 26 69  iSectorSize-1)&i
11cd3 53 65 63 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a  SectorSize)!=0 .
11cd4 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20      ){.      /* 
11cd5 49 66 20 74 68 65 20 65 69 74 68 65 72 20 74 68  If the either th
11cd6 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 72 20 73  e page-size or s
11cd7 65 63 74 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68  ector-size in th
11cd8 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  e journal-header
11cd9 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e   is .      ** in
11cda 76 61 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20  valid, then the 
11cdb 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f  process that wro
11cdc 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  te the journal-h
11cdd 65 61 64 65 72 20 6d 75 73 74 20 68 61 76 65 20  eader must have 
11cde 0a 20 20 20 20 20 20 2a 2a 20 63 72 61 73 68 65  .      ** crashe
11cdf 64 20 62 65 66 6f 72 65 20 74 68 65 20 68 65 61  d before the hea
11ce0 64 65 72 20 77 61 73 20 73 79 6e 63 65 64 2e 20  der was synced. 
11ce1 49 6e 20 74 68 69 73 20 63 61 73 65 20 73 74 6f  In this case sto
11ce2 70 20 72 65 61 64 69 6e 67 20 0a 20 20 20 20 20  p reading .     
11ce3 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   ** the journal 
11ce4 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20  file here..     
11ce5 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
11ce6 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
11ce7 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61    }..    /* Upda
11ce8 74 65 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  te the page-size
11ce9 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61   to match the va
11cea 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68  lue read from th
11ceb 65 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20  e journal. .    
11cec 2a 2a 20 55 73 65 20 61 20 74 65 73 74 63 61 73  ** Use a testcas
11ced 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b  e() macro to mak
11cee 65 20 73 75 72 65 20 74 68 61 74 20 6d 61 6c 6c  e sure that mall
11cef 6f 63 20 66 61 69 6c 75 72 65 20 77 69 74 68 69  oc failure withi
11cf0 6e 20 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 53  n .    ** PagerS
11cf1 65 74 50 61 67 65 73 69 7a 65 28 29 20 69 73 20  etPagesize() is 
11cf2 74 65 73 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  tested..    */. 
11cf3 20 20 20 69 50 61 67 65 53 69 7a 65 31 36 20 3d     iPageSize16 =
11cf4 20 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 3b   (u16)iPageSize;
11cf5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11cf6 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
11cf7 65 28 70 50 61 67 65 72 2c 20 26 69 50 61 67 65  e(pPager, &iPage
11cf8 53 69 7a 65 31 36 2c 20 2d 31 29 3b 0a 20 20 20  Size16, -1);.   
11cf9 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
11cfa 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
11cfb 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
11cfc 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 69  TE_OK || iPageSi
11cfd 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61 67 65  ze16==(u16)iPage
11cfe 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  Size );..    /* 
11cff 55 70 64 61 74 65 20 74 68 65 20 61 73 73 75 6d  Update the assum
11d00 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 74  ed sector-size t
11d01 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75  o match the valu
11d02 65 20 75 73 65 64 20 62 79 20 0a 20 20 20 20 2a  e used by .    *
11d03 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  * the process th
11d04 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 20  at created this 
11d05 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73  journal. If this
11d06 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20   journal was.   
11d07 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61   ** created by a
11d08 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74   process other t
11d09 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68  han this one, th
11d0a 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
11d0b 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20      ** is being 
11d0c 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
11d0d 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  in pager_playbac
11d0e 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76  k(). The local v
11d0f 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50  alue.    ** of P
11d10 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
11d11 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 74  is restored at t
11d12 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72  he end of that r
11d13 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20  outine..    */. 
11d14 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
11d15 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53  rSize = iSectorS
11d16 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67  ize;.  }..  pPag
11d17 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b  er->journalOff +
11d18 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  = JOURNAL_HDR_SZ
11d19 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
11d1a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
11d1b 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70 6c   Write the suppl
11d1c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ied master journ
11d1d 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65  al name into the
11d1e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
11d1f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 65  r pager.** pPage
11d20 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  r at the current
11d21 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d   location. The m
11d22 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11d23 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c  me must be the l
11d24 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69  ast.** thing wri
11d25 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61  tten to a journa
11d26 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70  l file. If the p
11d27 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d  ager is in full-
11d28 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a  sync mode, the.*
11d29 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64  * journal file d
11d2a 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64 76  escriptor is adv
11d2b 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65 78  anced to the nex
11d2c 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72  t sector boundar
11d2d 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74  y before.** anyt
11d2e 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 2e  hing is written.
11d2f 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a   The format is:.
11d30 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65  **.**   + 4 byte
11d31 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  s: PAGER_MJ_PGNO
11d32 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73  ..**   + N bytes
11d33 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  : Master journal
11d34 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66   filename in utf
11d35 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74  -8..**   + 4 byt
11d36 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66  es: N (length of
11d37 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11d38 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 6e  name in bytes, n
11d39 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  o nul-terminator
11d3a 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65  )..**   + 4 byte
11d3b 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61  s: Master journa
11d3c 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e  l name checksum.
11d3d 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 73 3a  .**   + 8 bytes:
11d3e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
11d3f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74  ..**.** The mast
11d40 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er journal page 
11d41 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20  checksum is the 
11d42 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73  sum of the bytes
11d43 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a   in the master.*
11d44 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20  * journal name, 
11d45 77 68 65 72 65 20 65 61 63 68 20 62 79 74 65 20  where each byte 
11d46 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
11d47 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62 69 74  s a signed 8-bit
11d48 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20   integer..**.** 
11d49 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61 20  If zMaster is a 
11d4a 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63  NULL pointer (oc
11d4b 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67 6c  curs for a singl
11d4c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73  e database trans
11d4d 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69  action), .** thi
11d4e 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
11d4f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
11d50 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
11d51 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
11d52 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
11d53 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72  Master){.  int r
11d54 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11d55 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11d56 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
11d57 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 20  int nMaster;    
11d58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d59 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
11d5a 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a  ring zMaster */.
11d5b 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20    i64 iHdrOff;  
11d5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d5d 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20     /* Offset of 
11d5e 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61  header in journa
11d5f 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20  l file */.  i64 
11d60 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20  jrnlSize;       
11d61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11d62 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  Size of journal 
11d63 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a  file on disk */.
11d64 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b    u32 cksum = 0;
11d65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d66 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f     /* Checksum o
11d67 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72  f string zMaster
11d68 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73   */..  if( !zMas
11d69 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73  ter || pPager->s
11d6a 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 70  etMaster.   || p
11d6b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11d6c 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11d6d 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20  LMODE_MEMORY .  
11d6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
11d6f 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
11d70 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a  OURNALMODE_OFF .
11d71 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
11d72 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
11d73 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
11d74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74  er = 1;.  assert
11d75 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
11d76 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  >jfd) );..  /* C
11d77 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c 65 6e  alculate the len
11d78 67 74 68 20 69 6e 20 62 79 74 65 73 20 61 6e 64  gth in bytes and
11d79 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6f 66   the checksum of
11d7a 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66 6f   zMaster */.  fo
11d7b 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d 61  r(nMaster=0; zMa
11d7c 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e  ster[nMaster]; n
11d7d 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20 63  Master++){.    c
11d7e 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b  ksum += zMaster[
11d7f 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20  nMaster];.  }.. 
11d80 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d 73   /* If in full-s
11d81 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e 63  ync mode, advanc
11d82 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64 69  e to the next di
11d83 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f 72 65  sk sector before
11d84 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74 68   writing.  ** th
11d85 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11d86 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20 69   name. This is i
11d87 6e 20 63 61 73 65 20 74 68 65 20 70 72 65 76 69  n case the previ
11d88 6f 75 73 20 70 61 67 65 20 77 72 69 74 74 65 6e  ous page written
11d89 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75   to.  ** the jou
11d8a 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61 64 79  rnal has already
11d8b 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20 20   been synced..  
11d8c 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
11d8d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20  >fullSync ){.   
11d8e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11d8f 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72  Off = journalHdr
11d90 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a  Offset(pPager);.
11d91 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d 20    }.  iHdrOff = 
11d92 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11d93 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20  ff;..  /* Write 
11d94 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
11d95 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65 20 65  al data to the e
11d96 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  nd of the journa
11d97 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a 20  l file. If.  ** 
11d98 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
11d99 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
11d9a 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61  r code to the ca
11d9b 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ller..  */.  if(
11d9c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69   (0 != (rc = wri
11d9d 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
11d9e 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c 20 50  >jfd, iHdrOff, P
11d9f 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
11da0 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20 28 30  ger)))).   || (0
11da1 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65   != (rc = sqlite
11da2 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
11da3 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 6e  >jfd, zMaster, n
11da4 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66 66 2b  Master, iHdrOff+
11da5 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d  4))).   || (0 !=
11da6 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69   (rc = write32bi
11da7 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
11da8 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65  iHdrOff+4+nMaste
11da9 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a 20 20  r, nMaster))).  
11daa 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20   || (0 != (rc = 
11dab 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
11dac 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11dad 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63 6b  +4+nMaster+4, ck
11dae 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20  sum))).   || (0 
11daf 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  != (rc = sqlite3
11db0 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
11db1 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  jfd, aJournalMag
11db2 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b 34  ic, 8, iHdrOff+4
11db3 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a 20 20  +nMaster+8))).  
11db4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
11db5 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e  ;.  }.  pPager->
11db6 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e  journalOff += (n
11db7 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20 70 50  Master+20);.  pP
11db8 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d  ager->needSync =
11db9 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63   !pPager->noSync
11dba 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70  ;..  /* If the p
11dbb 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69 73  ager is in peris
11dbc 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64  tent-journal mod
11dbd 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79 73  e, then the phys
11dbe 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e  ical .  ** journ
11dbf 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 65  al-file may exte
11dc0 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  nd past the end 
11dc1 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f  of the master-jo
11dc2 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20  urnal name.  ** 
11dc3 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20 6d  and 8 bytes of m
11dc4 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20 77  agic data just w
11dc5 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 69  ritten to the fi
11dc6 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 2a  le. This is .  *
11dc7 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61  * dangerous beca
11dc8 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20  use the code to 
11dc9 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a  rollback a hot-j
11dca 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a  ournal file.  **
11dcb 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c   will not be abl
11dcc 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61  e to find the ma
11dcd 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster-journal nam
11dce 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0a  e to determine .
11dcf 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20    ** whether or 
11dd0 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  not the journal 
11dd1 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20  is hot. .  **.  
11dd2 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e 67  ** Easiest thing
11dd3 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 73   to do in this s
11dd4 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 72  cenario is to tr
11dd5 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e  uncate the journ
11dd6 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 6f  al .  ** file to
11dd7 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73 69   the required si
11dd8 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20  ze..  */ .  if( 
11dd9 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
11dda 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
11ddb 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ze(pPager->jfd, 
11ddc 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 26  &jrnlSize)).   &
11ddd 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 65  & jrnlSize>pPage
11dde 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20  r->journalOff.  
11ddf 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
11de0 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50  te3OsTruncate(pP
11de1 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65  ager->jfd, pPage
11de2 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a  r->journalOff);.
11de3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
11de4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61  .}../*.** Find a
11de5 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61 73   page in the has
11de6 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74  h table given it
11de7 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52  s page number. R
11de8 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
11de9 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f  er to the page o
11dea 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65  r NULL if the re
11deb 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20  quested page is 
11dec 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20  not .** already 
11ded 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  in memory..*/.st
11dee 61 74 69 63 20 50 67 48 64 72 20 2a 70 61 67 65  atic PgHdr *page
11def 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a  r_lookup(Pager *
11df0 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
11df1 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 20  o){.  PgHdr *p; 
11df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11df3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
11df4 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a  n value */..  /*
11df5 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   It is not possi
11df6 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 74  ble for a call t
11df7 6f 20 50 63 61 63 68 65 46 65 74 63 68 28 29 20  o PcacheFetch() 
11df8 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d  with createFlag=
11df9 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c  =0 to.  ** fail,
11dfa 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d 70   since no attemp
11dfb 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 79  t to allocate dy
11dfc 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 6c  namic memory wil
11dfd 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a  l be made..  */.
11dfe 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 50    (void)sqlite3P
11dff 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65  cacheFetch(pPage
11e00 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f  r->pPCache, pgno
11e01 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 75  , 0, &p);.  retu
11e02 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  rn p;.}../*.** U
11e03 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 72 20  nless the pager 
11e04 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74  is in error-stat
11e05 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69  e, discard all i
11e06 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e 20  n-memory pages. 
11e07 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20  If.** the pager 
11e08 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74  is in error-stat
11e09 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c  e, then this cal
11e0a 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  l is a no-op..**
11e0b 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63 61  .** TODO: Why ca
11e0c 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74 20 74  n we not reset t
11e0d 68 65 20 70 61 67 65 72 20 77 68 69 6c 65 20 69  he pager while i
11e0e 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f 0a 2a  n error state?.*
11e0f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
11e10 67 65 72 5f 72 65 73 65 74 28 50 61 67 65 72 20  ger_reset(Pager 
11e11 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
11e12 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67 65  SQLITE_OK==pPage
11e13 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20  r->errCode ){.  
11e14 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52    sqlite3BackupR
11e15 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70  estart(pPager->p
11e16 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73 71 6c  Backup);.    sql
11e17 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28  ite3PcacheClear(
11e18 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
11e19 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62  ;.    pPager->db
11e1a 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20  SizeValid = 0;. 
11e1b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65   }.}../*.** Free
11e1c 20 61 6c 6c 20 73 74 72 75 63 74 75 72 65 73 20   all structures 
11e1d 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61  in the Pager.aSa
11e1e 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 20  vepoint[] array 
11e1f 61 6e 64 20 73 65 74 20 62 6f 74 68 0a 2a 2a 20  and set both.** 
11e20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
11e21 20 61 6e 64 20 50 61 67 65 72 2e 6e 53 61 76 65   and Pager.nSave
11e22 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f 2e 20 43  point to zero. C
11e23 6c 6f 73 65 20 74 68 65 20 73 75 62 2d 6a 6f 75  lose the sub-jou
11e24 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73  rnal.** if it is
11e25 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61   open and the pa
11e26 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 78  ger is not in ex
11e27 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f  clusive mode..*/
11e28 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
11e29 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74  easeAllSavepoint
11e2a 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
11e2b 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20  {.  int ii;     
11e2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
11e2d 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 69 6e  rator for loopin
11e2e 67 20 74 68 72 6f 75 67 68 20 50 61 67 65 72 2e  g through Pager.
11e2f 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20  aSavepoint */.  
11e30 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
11e31 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
11e32 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69   ii++){.    sqli
11e33 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
11e34 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  (pPager->aSavepo
11e35 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70  int[ii].pInSavep
11e36 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  oint);.  }.  if(
11e37 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
11e38 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74  iveMode || sqlit
11e39 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70  e3IsMemJournal(p
11e3a 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a  Pager->sjfd) ){.
11e3b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
11e3c 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29  se(pPager->sjfd)
11e3d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
11e3e 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61  free(pPager->aSa
11e3f 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67  vepoint);.  pPag
11e40 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d  er->aSavepoint =
11e41 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53   0;.  pPager->nS
11e42 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20  avepoint = 0;.  
11e43 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20  pPager->nSubRec 
11e44 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  = 0;.}../*.** Se
11e45 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72  t the bit number
11e46 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67   pgno in the Pag
11e47 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53  erSavepoint.pInS
11e48 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74  avepoint .** bit
11e49 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e  vecs of all open
11e4a 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74   savepoints. Ret
11e4b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
11e4c 20 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f   successful.** o
11e4d 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  r SQLITE_NOMEM i
11e4e 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  f a malloc failu
11e4f 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  re occurs..*/.st
11e50 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61  atic int addToSa
11e51 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50  vepointBitvecs(P
11e52 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
11e53 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20  no pgno){.  int 
11e54 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ii;             
11e55 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
11e56 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  unter */.  int r
11e57 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
11e58 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63       /* Result c
11e59 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69  ode */..  for(ii
11e5a 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e  =0; ii<pPager->n
11e5b 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29  Savepoint; ii++)
11e5c 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70  {.    PagerSavep
11e5d 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65  oint *p = &pPage
11e5e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69  r->aSavepoint[ii
11e5f 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c  ];.    if( pgno<
11e60 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20  =p->nOrig ){.   
11e61 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33     rc |= sqlite3
11e62 42 69 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e  BitvecSet(p->pIn
11e63 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29  Savepoint, pgno)
11e64 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
11e65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
11e66 45 4d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  EM );.      asse
11e67 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
11e68 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  K || rc==SQLITE_
11e69 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20  NOMEM );.    }. 
11e6a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11e6b 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20  }../*.** Unlock 
11e6c 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11e6d 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  e. This function
11e6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
11e6f 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69  he pager.** is i
11e70 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
11e71 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
11e72 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c  ager is currentl
11e73 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65  y in error state
11e74 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 63 6f  , discard the co
11e75 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68  ntents of .** th
11e76 65 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 65  e cache and rese
11e77 74 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75  t the Pager stru
11e78 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73  cture internal s
11e79 74 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 69  tate. If there i
11e7a 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75  s.** an open jou
11e7b 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20  rnal-file, then 
11e7c 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20  the next time a 
11e7d 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f  shared-lock is o
11e7e 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68  btained.** on th
11e7f 65 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 79  e pager file (by
11e80 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
11e81 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20  er process), it 
11e82 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74  will be.** treat
11e83 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72  ed as a hot-jour
11e84 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62  nal and rolled b
11e85 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ack..*/.static v
11e86 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b  oid pager_unlock
11e87 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11e88 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
11e89 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b  exclusiveMode ){
11e8a 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20  .    int rc;    
11e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11e8c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
11e8d 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61   */..    /* Alwa
11e8e 79 73 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75  ys close the jou
11e8f 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64  rnal file when d
11e90 72 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61  ropping the data
11e91 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a  base lock..    *
11e92 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f  * Otherwise, ano
11e93 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
11e94 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  with journal_mod
11e95 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20  e=delete might. 
11e96 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65     ** delete the
11e97 20 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75   file out from u
11e98 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a  nder us..    */.
11e99 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
11e9a 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
11e9b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76  .    sqlite3Bitv
11e9c 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
11e9d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20  ->pInJournal);. 
11e9e 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f     pPager->pInJo
11e9f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72  urnal = 0;.    r
11ea0 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69  eleaseAllSavepoi
11ea1 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  nts(pPager);..  
11ea2 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
11ea3 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f   is unlocked, so
11ea4 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68  mebody else migh
11ea5 74 20 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65  t change it. The
11ea6 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73  .    ** values s
11ea7 74 6f 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64  tored in Pager.d
11ea8 62 53 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74  bSize etc. might
11ea9 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20   become invalid 
11eaa 69 66 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68  if.    ** this h
11eab 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65  appens. TODO: Re
11eac 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e  ally, this doesn
11ead 27 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c  't need to be cl
11eae 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74  eared.    ** unt
11eaf 69 6c 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  il the change-co
11eb0 75 6e 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c  unter check fail
11eb1 73 20 69 6e 20 50 61 67 65 72 53 68 61 72 65 64  s in PagerShared
11eb2 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  Lock()..    */. 
11eb3 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a     pPager->dbSiz
11eb4 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20  eValid = 0;..   
11eb5 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70   rc = osUnlock(p
11eb6 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f  Pager->fd, NO_LO
11eb7 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  CK);.    if( rc 
11eb8 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
11eb9 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20  >errCode = rc;. 
11eba 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45     }.    IOTRACE
11ebb 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c  (("UNLOCK %p\n",
11ebc 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f   pPager))..    /
11ebd 2a 20 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f  * If Pager.errCo
11ebe 64 65 20 69 73 20 73 65 74 2c 20 74 68 65 20 63  de is set, the c
11ebf 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
11ec0 61 67 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f  ager cache canno
11ec1 74 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73  t be.    ** trus
11ec2 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68  ted. Now that th
11ec3 65 20 70 61 67 65 72 20 66 69 6c 65 20 69 73 20  e pager file is 
11ec4 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f  unlocked, the co
11ec5 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20  ntents of the.  
11ec6 20 20 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62    ** cache can b
11ec7 65 20 64 69 73 63 61 72 64 65 64 20 61 6e 64 20  e discarded and 
11ec8 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73  the error code s
11ec9 61 66 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20  afely cleared.. 
11eca 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50     */.    if( pP
11ecb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
11ecc 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
11ecd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
11ece 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43      pPager->errC
11ecf 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  ode = SQLITE_OK;
11ed0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
11ed1 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
11ed2 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  r);.    }..    p
11ed3 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
11ed4 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20  ntDone = 0;.    
11ed5 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
11ed6 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20  PAGER_UNLOCK;.  
11ed7 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
11ed8 66 69 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  fied = 0;.  }.}.
11ed9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
11eda 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63  tion should be c
11edb 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f  alled when an IO
11edc 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20  ERR, CORRUPT or 
11edd 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61  FULL error.** ma
11ede 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e  y have occurred.
11edf 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
11ee0 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ent is a pointer
11ee1 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a   to the pager .*
11ee2 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  * structure, the
11ee3 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f   second the erro
11ee4 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20  r-code about to 
11ee5 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  be returned by a
11ee6 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66   pager .** API f
11ee7 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c  unction. The val
11ee8 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61  ue returned is a
11ee9 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63   copy of the sec
11eea 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a  ond argument .**
11eeb 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
11eec 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  n. .**.** If the
11eed 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
11eee 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   is SQLITE_IOERR
11eef 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  , SQLITE_CORRUPT
11ef0 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c  , or SQLITE_FULL
11ef1 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65  .** the error be
11ef2 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74  comes persistent
11ef3 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73  . Until the pers
11ef4 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63  isten error is c
11ef5 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65  leared,.** subse
11ef6 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20  quent API calls 
11ef7 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69  on this Pager wi
11ef8 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72  ll immediately r
11ef9 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a  eturn the same .
11efa 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  ** error code..*
11efb 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e  *.** A persisten
11efc 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65  t error indicate
11efd 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65  s that the conte
11efe 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72  nts of the pager
11eff 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f  -cache .** canno
11f00 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68  t be trusted. Th
11f01 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20  is state can be 
11f02 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c  cleared by compl
11f03 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67  etely discarding
11f04 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74   .** the content
11f05 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
11f06 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73  ache. If a trans
11f07 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76  action was activ
11f08 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65  e when.** the pe
11f09 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f  rsistent error o
11f0a 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68  ccurred, then th
11f0b 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
11f0c 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74  al may need.** t
11f0d 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f  o be replayed to
11f0e 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e   restore the con
11f0f 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74  tents of the dat
11f10 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 69  abase file (as i
11f11 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68  f.** it were a h
11f12 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a  ot-journal)..*/.
11f13 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
11f14 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50  _error(Pager *pP
11f15 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20  ager, int rc){. 
11f16 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20   int rc2 = rc & 
11f17 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20  0xff;.  assert( 
11f18 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
11f19 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73   !MEMDB );.  ass
11f1a 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 67  ert(.       pPag
11f1b 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
11f1c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20  ITE_FULL ||.    
11f1d 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
11f1e 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  de==SQLITE_OK ||
11f1f 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d  .       (pPager-
11f20 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 29  >errCode & 0xff)
11f21 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20  ==SQLITE_IOERR. 
11f22 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53   );.  if( rc2==S
11f23 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63  QLITE_FULL || rc
11f24 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  2==SQLITE_IOERR 
11f25 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65  ){.    pPager->e
11f26 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d  rrCode = rc;.  }
11f27 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11f28 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61  ./*.** Execute a
11f29 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74   rollback if a t
11f2a 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
11f2b 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20  tive and unlock 
11f2c 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
11f2d 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66   file. .**.** If
11f2e 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61   the pager has a
11f2f 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74  lready entered t
11f30 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
11f31 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a  do not attempt .
11f32 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
11f33 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e  at this time. In
11f34 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c  stead, pager_unl
11f35 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ock() is called.
11f36 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20   The.** call to 
11f37 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77  pager_unlock() w
11f38 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20  ill discard all 
11f39 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c  in-memory pages,
11f3a 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64   unlock.** the d
11f3b 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
11f3c 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   clear the error
11f3d 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20   state. If this 
11f3e 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68  means that.** th
11f3f 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75  ere is a hot-jou
11f40 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65  rnal left in the
11f41 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68   file-system, th
11f42 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f  e next connectio
11f43 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61  n.** to obtain a
11f44 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20   shared lock on 
11f45 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68  the pager (which
11f46 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65   may be this one
11f47 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69  ) will.** roll i
11f48 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  t back..**.** If
11f49 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e   the pager has n
11f4a 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72  ot already enter
11f4b 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ed the error sta
11f4c 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72  te, but an IO or
11f4d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72  .** malloc error
11f4e 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61   occurs during a
11f4f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20   rollback, then 
11f50 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66  this will itself
11f51 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70   cause .** the p
11f52 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68  ager to enter th
11f53 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57  e error state. W
11f54 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65  hich will be cle
11f55 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ared by the.** c
11f56 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c  all to pager_unl
11f57 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69  ock(), as descri
11f58 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74  bed above..*/.st
11f59 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55  atic void pagerU
11f5a 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b  nlockAndRollback
11f5b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11f5c 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
11f5d 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f  rrCode==SQLITE_O
11f5e 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61  K && pPager->sta
11f5f 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
11f60 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ED ){.    sqlite
11f61 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
11f62 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oc();.    sqlite
11f63 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
11f64 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69  Pager);.    sqli
11f65 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
11f66 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65  oc();.  }.  page
11f67 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29  r_unlock(pPager)
11f68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
11f69 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74  routine ends a t
11f6a 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72  ransaction. A tr
11f6b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75  ansaction is usu
11f6c 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a  ally ended by .*
11f6d 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49  * either a COMMI
11f6e 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20  T or a ROLLBACK 
11f6f 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20  operation. This 
11f70 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63  routine may be c
11f71 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20  alled .** after 
11f72 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f  rollback of a ho
11f73 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66  t-journal, or if
11f74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
11f75 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a   while opening.*
11f76 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * the journal fi
11f77 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68  le or writing th
11f78 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75  e very first jou
11f79 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61  rnal-header of a
11f7a 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61  .** database tra
11f7b 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a  nsaction..** .**
11f7c 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
11f7d 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44   in PAGER_SHARED
11f7e 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b   or PAGER_UNLOCK
11f7f 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
11f80 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63  .** routine is c
11f81 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
11f82 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51  o-op (returns SQ
11f83 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK)..**.** 
11f84 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61  Otherwise, any a
11f85 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
11f86 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a   are released..*
11f87 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72  *.** If the jour
11f88 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  nal file is open
11f89 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 69  , then it is "fi
11f8a 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61  nalized". Once a
11f8b 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c   journal .** fil
11f8c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c  e has been final
11f8d 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70  ized it is not p
11f8e 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69  ossible to use i
11f8f 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61  t to roll back a
11f90 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
11f91 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65  . Nor will it be
11f92 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
11f93 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  e a hot-journal 
11f94 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e  by this.** or an
11f95 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  y other database
11f96 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61   connection. Exa
11f97 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e  ctly how a journ
11f98 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a  al is finalized.
11f99 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68  ** depends on wh
11f9a 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
11f9b 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
11f9c 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
11f9d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63  ode and.** the c
11f9e 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d  urrent journal-m
11f9f 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e  ode (Pager.journ
11fa0 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61  alMode value), a
11fa1 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
11fa2 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d     journalMode==
11fa3 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f  MEMORY.**     Jo
11fa4 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
11fa5 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20  iptor is simply 
11fa6 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73  closed. This des
11fa7 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20  troys an .**    
11fa8 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
11fa9 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72  al..**.**   jour
11faa 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54  nalMode==TRUNCAT
11fab 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c  E.**     Journal
11fac 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74   file is truncat
11fad 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73  ed to zero bytes
11fae 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20   in size..**.** 
11faf 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50    journalMode==P
11fb0 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68  ERSIST.**     Th
11fb1 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 73  e first 28 bytes
11fb2 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11fb3 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e  file are zeroed.
11fb4 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65   This invalidate
11fb5 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72  s.**     the fir
11fb6 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  st journal heade
11fb7 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61  r in the file, a
11fb8 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74  nd hence the ent
11fb9 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  ire journal.**  
11fba 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61     file. An inva
11fbb 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  lid journal file
11fbc 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65   cannot be rolle
11fbd 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20  d back..**.**   
11fbe 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c  journalMode==DEL
11fbf 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a  ETE.**     The j
11fc0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63  ournal file is c
11fc1 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65  losed and delete
11fc2 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f  d using sqlite3O
11fc3 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a  sDelete()..**.**
11fc4 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 65       If the page
11fc5 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20  r is running in 
11fc6 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11fc7 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66  this method of f
11fc8 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20  inalizing.**    
11fc9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11fca 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e  e is never used.
11fcb 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65   Instead, if the
11fcc 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a   journalMode is.
11fcd 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e  **     DELETE an
11fce 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  d the pager is i
11fcf 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
11fd0 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73  , the method des
11fd1 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20  cribed under.** 
11fd2 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d      journalMode=
11fd3 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 64  =PERSIST is used
11fd4 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
11fd5 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61  After the journa
11fd6 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20  l is finalized, 
11fd7 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f  if running in no
11fd8 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n-exclusive mode
11fd9 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d  , the.** pager m
11fda 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48  oves to PAGER_SH
11fdb 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 20  ARED state (and 
11fdc 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c  downgrades the l
11fdd 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61  ock on the.** da
11fde 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f  tabase file acco
11fdf 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20  rdingly)..**.** 
11fe0 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
11fe1 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
11fe2 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73  sive mode and is
11fe3 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44   in PAGER_SYNCED
11fe4 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f   state,.** it mo
11fe5 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43  ves to PAGER_EXC
11fe6 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73  LUSIVE. No locks
11fe7 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20   are downgraded 
11fe8 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a  when running in.
11fe9 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  ** exclusive mod
11fea 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  e..**.** SQLITE_
11feb 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
11fec 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
11fed 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  s. If an error o
11fee 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20  ccurs during.** 
11fef 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70  any of the IO op
11ff0 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61  erations to fina
11ff1 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lize the journal
11ff2 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20   file or unlock 
11ff3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
11ff4 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f  then the IO erro
11ff5 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
11ff6 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
11ff7 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61  If the .** opera
11ff8 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65  tion to finalize
11ff9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11ffa 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68  e fails, then th
11ffb 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20  e code still.** 
11ffc 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20  tries to unlock 
11ffd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11ffe 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c  e if not in excl
11fff 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74  usive mode. If t
12000 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65  he.** unlock ope
12001 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20  ration fails as 
12002 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66  well, then the f
12003 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20  irst error code 
12004 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68  related.** to th
12005 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e  e first error en
12006 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a  countered (the j
12007 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74  ournal finalizat
12008 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72  ion one) is.** r
12009 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
1200a 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64  ic int pager_end
1200b 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67  _transaction(Pag
1200c 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
1200d 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e  hasMaster){.  in
1200e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1200f 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  ;      /* Error 
12010 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61  code from journa
12011 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f  l finalization o
12012 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  peration */.  in
12013 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f  t rc2 = SQLITE_O
12014 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  K;     /* Error 
12015 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c  code from db fil
12016 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69  e unlock operati
12017 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61  on */..  if( pPa
12018 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52  ger->state<PAGER
12019 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20  _RESERVED ){.   
1201a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1201b 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  K;.  }.  release
1201c 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50  AllSavepoints(pP
1201d 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74  ager);..  assert
1201e 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1201f 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  >jfd) || pPager-
12020 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
12021 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70  ;.  if( isOpen(p
12022 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a  Pager->jfd) ){..
12023 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20      /* Finalize 
12024 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12025 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  . */.    if( sql
12026 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c  ite3IsMemJournal
12027 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
12028 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
12029 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1202a 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
1202b 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a  LMODE_MEMORY );.
1202c 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
1202d 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
1202e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1202f 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12030 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
12031 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
12032 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
12033 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12034 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
12035 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12036 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12037 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12038 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
12039 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20  r->jfd, 0);.    
1203a 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
1203b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
1203c 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
1203d 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
1203e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
1203f 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  ( pPager->exclus
12040 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c  iveMode .     ||
12041 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12042 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
12043 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
12044 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20      ){.      rc 
12045 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72  = zeroJournalHdr
12046 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74  (pPager, hasMast
12047 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72  er);.      pager
12048 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
12049 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  c);.      pPager
1204a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
1204b 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
1204c 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
1204d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
1204e 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
1204f 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75  nch may be execu
12050 74 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a  ted with Pager.j
12051 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f  ournalMode==MEMO
12052 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61  RY if.      ** a
12053 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73   hot-journal was
12054 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63   just rolled bac
12055 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  k. In this case 
12056 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
12057 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64    ** file should
12058 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64   be closed and d
12059 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20  eleted. If this 
1205a 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65  connection write
1205b 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  s to.      ** th
1205c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1205d 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75   it will do so u
1205e 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  sing an in-memor
1205f 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20  y journal.  */. 
12060 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
12061 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
12062 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
12063 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20  ODE_DELETE .    
12064 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72         || pPager
12065 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
12066 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12067 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29  _MEMORY .      )
12068 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
12069 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
1206a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  fd);.      if( !
1206b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
1206c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1206d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1206e 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70  (pPager->pVfs, p
1206f 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
12070 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
12071 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54   }..#ifdef SQLIT
12072 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
12073 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49    sqlite3PcacheI
12074 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67  terateDirty(pPag
12075 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67  er->pPCache, pag
12076 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29  er_set_pagehash)
12077 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71  ;.#endif..    sq
12078 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
12079 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
1207a 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  che);.    sqlite
1207b 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
1207c 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1207d 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  l);.    pPager->
1207e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  pInJournal = 0;.
1207f 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63      pPager->nRec
12080 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28   = 0;.  }..  if(
12081 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
12082 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72  iveMode ){.    r
12083 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50  c2 = osUnlock(pP
12084 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44  ager->fd, SHARED
12085 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67  _LOCK);.    pPag
12086 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
12087 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50  R_SHARED;.    pP
12088 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e  ager->changeCoun
12089 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c  tDone = 0;.  }el
1208a 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  se if( pPager->s
1208b 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43  tate==PAGER_SYNC
1208c 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  ED ){.    pPager
1208d 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
1208e 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20  EXCLUSIVE;.  }. 
1208f 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
12090 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  er = 0;.  pPager
12091 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
12092 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
12093 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  fied = 0;..  /* 
12094 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70  TODO: Is this op
12095 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68  timal? Why is th
12096 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69  e db size invali
12097 64 61 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a  dated here .  **
12098 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
12099 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75  se file is not u
1209a 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50  nlocked? */.  pP
1209b 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
1209c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50   = 0;.  sqlite3P
1209d 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 50  cacheTruncate(pP
1209e 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
1209f 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a  Pager->dbSize);.
120a0 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a    if( !MEMDB ){.
120a1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
120a2 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d  zeValid = 0;.  }
120a3 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  ..  return (rc==
120a4 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63  SQLITE_OK?rc2:rc
120a5 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  );.}../*.** Para
120a6 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75 73 74  meter aData must
120a7 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
120a8 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61  er of pPager->pa
120a9 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20  geSize bytes.** 
120aa 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65  of data. Compute
120ab 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68   and return a ch
120ac 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74  ecksum based ont
120ad 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
120ae 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66   the .** page of
120af 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75   data and the cu
120b0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70  rrent value of p
120b1 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
120b2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
120b3 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b  not a real check
120b4 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c  sum. It is reall
120b5 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f  y just the sum o
120b6 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d  f the .** random
120b7 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28   initial value (
120b8 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
120b9 74 29 20 61 6e 64 20 65 76 65 72 79 20 32 30 30  t) and every 200
120ba 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68  th byte.** of th
120bb 65 20 70 61 67 65 20 64 61 74 61 2c 20 73 74 61  e page data, sta
120bc 72 74 69 6e 67 20 77 69 74 68 20 62 79 74 65 20  rting with byte 
120bd 6f 66 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e  offset (pPager->
120be 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a  pageSize%200)..*
120bf 2a 20 45 61 63 68 20 62 79 74 65 20 69 73 20 69  * Each byte is i
120c0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e  nterpreted as an
120c1 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20   8-bit unsigned 
120c2 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43  integer..**.** C
120c3 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d  hanging the form
120c4 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  ula used to comp
120c5 75 74 65 20 74 68 69 73 20 63 68 65 63 6b 73 75  ute this checksu
120c6 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a  m results in an.
120c7 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  ** incompatible 
120c8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
120c9 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f  mat..**.** If jo
120ca 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e  urnal corruption
120cb 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 61   occurs due to a
120cc 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20   power failure, 
120cd 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20  the most likely 
120ce 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20  .** scenario is 
120cf 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20  that one end or 
120d0 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65  the other of the
120d1 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20   record will be 
120d2 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20  changed. .** It 
120d3 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b  is much less lik
120d4 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f  ely that the two
120d5 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75   ends of the jou
120d6 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c  rnal record will
120d7 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61   be.** correct a
120d8 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65  nd the middle be
120d9 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c   corrupt.  Thus,
120da 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22   this "checksum"
120db 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75   scheme,.** thou
120dc 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70  gh fast and simp
120dd 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20  le, catches the 
120de 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69  mostly likely ki
120df 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e  nd of corruption
120e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20  ..*/.static u32 
120e1 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65  pager_cksum(Page
120e2 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  r *pPager, const
120e3 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75   u8 *aData){.  u
120e4 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65  32 cksum = pPage
120e5 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20  r->cksumInit;   
120e6 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75        /* Checksu
120e7 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72  m value to retur
120e8 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  n */.  int i = p
120e9 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d  Pager->pageSize-
120ea 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  200;          /*
120eb 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
120ec 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b  .  while( i>0 ){
120ed 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44  .    cksum += aD
120ee 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d  ata[i];.    i -=
120ef 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   200;.  }.  retu
120f0 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a  rn cksum;.}../*.
120f1 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65  ** Read a single
120f2 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65   page from eithe
120f3 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  r the journal fi
120f4 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e  le (if isMainJrn
120f5 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d  l==1) or.** from
120f6 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
120f7 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d   (if isMainJrnl=
120f8 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b  =0) and playback
120f9 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54   that page..** T
120fa 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 20 61  he page begins a
120fb 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65  t offset *pOffse
120fc 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e  t into the file.
120fd 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a   The *pOffset.**
120fe 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
120ff 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 72 74  sed to the start
12100 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67   of the next pag
12101 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
12102 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61  ..**.** The isMa
12103 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74  inJrnl flag is t
12104 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 74  rue if this is t
12105 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
12106 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20   journal and.** 
12107 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74  false for the st
12108 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
12109 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62    The main rollb
1210a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73  ack journal uses
1210b 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20  .** checksums - 
1210c 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
1210d 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a  urnal does not..
1210e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
1210f 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
12110 70 61 67 65 20 72 65 63 6f 72 64 20 72 65 61 64  page record read
12111 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29   from the (sub-)
12112 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20  journal file.** 
12113 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
12114 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
12115 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a  e of Pager.dbSiz
12116 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b  e, then playback
12117 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61   is.** skipped a
12118 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  nd SQLITE_OK is 
12119 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1211a 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20  If pDone is not 
1211b 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73  NULL, then it is
1211c 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67   a record of pag
1211d 65 73 20 74 68 61 74 20 68 61 76 65 20 61 6c 72  es that have alr
1211e 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61  eady.** been pla
1211f 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68  yed back.  If th
12120 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73  e page at *pOffs
12121 65 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  et has already b
12122 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a  een played back.
12123 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72 72 65  ** (if the corre
12124 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62  sponding pDone b
12125 69 74 20 69 73 20 73 65 74 29 20 74 68 65 6e 20  it is set) then 
12126 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62 61 63  skip the playbac
12127 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  k..** Make sure 
12128 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f  the pDone bit co
12129 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
1212a 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65  he *pOffset page
1212b 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72   is set.** prior
1212c 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a   to returning..*
1212d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
1212e 20 72 65 63 6f 72 64 20 69 73 20 73 75 63 63 65   record is succe
1212f 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f  ssfully read fro
12130 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72  m the (sub-)jour
12131 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20  nal file.** and 
12132 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65  played back, the
12133 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  n SQLITE_OK is r
12134 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49  eturned. If an I
12135 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a  O error occurs.*
12136 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20  * while reading 
12137 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  the record from 
12138 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
12139 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20  l file or while 
1213a 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68  writing.** to th
1213b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1213c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
1213d 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
1213e 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20  ned. If data.** 
1213f 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
12140 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73  read from the (s
12141 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ub-)journal file
12142 20 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20   but appears to 
12143 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c  be.** corrupted,
12144 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20   SQLITE_DONE is 
12145 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69  returned. Data i
12146 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72  s considered cor
12147 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f  rupted in.** two
12148 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a   circumstances:.
12149 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68  ** .**   * If th
1214a 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75  e record page-nu
1214b 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20  mber is illegal 
1214c 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50  (0 or PAGER_MJ_P
1214d 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20  GNO), or.**   * 
1214e 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73  If the record is
1214f 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61   being rolled ba
12150 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ck from the main
12151 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
12152 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 65       and the che
12153 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73  cksum field does
12154 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72   not match the r
12155 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a  ecord content..*
12156 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20  *.** Neither of 
12157 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72  these two scenar
12158 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65  ios are possible
12159 20 64 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f   during a savepo
1215a 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  int rollback..**
1215b 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61  .** If this is a
1215c 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62   savepoint rollb
1215d 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79  ack, then memory
1215e 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62 65 20   may have to be 
1215f 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61  dynamically.** a
12160 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
12161 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68   function. If th
12162 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20 61  is is the case a
12163 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  nd an allocation
12164 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54   fails,.** SQLIT
12165 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
12166 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
12167 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  nt pager_playbac
12168 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61  k_one_page(.  Pa
12169 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20  ger *pPager,    
1216a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1216b 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70  he pager being p
1216c 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  layed back */.  
1216d 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20  int isMainJrnl, 
1216e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1216f 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e   1 -> main journ
12170 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75  al. 0 -> sub-jou
12171 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69  rnal. */.  int i
12172 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20  sUnsync,        
12173 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
12174 20 69 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d   if reading from
12175 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a   unsynced main j
12176 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20  ournal */.  i64 
12177 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20  *pOffset,       
12178 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
12179 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f  set of record to
1217a 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69   playback */.  i
1217b 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20  nt isSavepnt,   
1217c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1217d 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 65 70  True for a savep
1217e 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f  oint rollback */
1217f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65  .  Bitvec *pDone
12180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12181 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61   /* Bitvec of pa
12182 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79  ges already play
12183 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20  ed back */.){.  
12184 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20  int rc;.  PgHdr 
12185 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  *pPg;           
12186 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78          /* An ex
12187 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74  isting page in t
12188 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67  he cache */.  Pg
12189 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
1218a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1218b 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
1218c 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72  f a page in jour
1218d 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73  nal */.  u32 cks
1218e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
1218f 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73         /* Checks
12190 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69  um used for sani
12191 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20  ty checking */. 
12192 20 63 68 61 72 20 2a 61 44 61 74 61 3b 20 20 20   char *aData;   
12193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12194 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72  * Temporary stor
12195 61 67 65 20 66 6f 72 20 74 68 65 20 70 61 67 65  age for the page
12196 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
12197 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20  le *jfd;        
12198 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20      /* The file 
12199 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
1219a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1219b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69  */..  assert( (i
1219c 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30  sMainJrnl&~1)==0
1219d 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61   );      /* isMa
1219e 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31  inJrnl is 0 or 1
1219f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69   */.  assert( (i
121a0 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20  sSavepnt&~1)==0 
121a1 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61  );       /* isSa
121a2 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20  vepnt is 0 or 1 
121a3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d  */.  assert( isM
121a4 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65  ainJrnl || pDone
121a5 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65   );     /* pDone
121a6 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20   always used on 
121a7 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a  sub-journals */.
121a8 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65    assert( isSave
121a9 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20  pnt || pDone==0 
121aa 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65  );   /* pDone ne
121ab 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d  ver used on non-
121ac 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20  savepoint */..  
121ad 61 44 61 74 61 20 3d 20 70 50 61 67 65 72 2d 3e  aData = pPager->
121ae 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 61 73 73  pTmpSpace;.  ass
121af 65 72 74 28 20 61 44 61 74 61 20 29 3b 20 20 20  ert( aData );   
121b0 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74        /* Temp st
121b1 6f 72 61 67 65 20 6d 75 73 74 20 68 61 76 65 20  orage must have 
121b2 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c  already been all
121b3 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20  ocated */..  /* 
121b4 52 65 61 64 20 74 68 65 20 70 61 67 65 20 6e 75  Read the page nu
121b5 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 64 61  mber and page da
121b6 74 61 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ta from the jour
121b7 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e  nal or sub-journ
121b8 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65  al.  ** file. Re
121b9 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
121ba 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  de to the caller
121bb 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   if an IO error 
121bc 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a  occurs..  */.  j
121bd 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20  fd = isMainJrnl 
121be 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20  ? pPager->jfd : 
121bf 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20  pPager->sjfd;.  
121c0 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
121c1 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26  jfd, *pOffset, &
121c2 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21  pgno);.  if( rc!
121c3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
121c4 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  urn rc;.  rc = s
121c5 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64  qlite3OsRead(jfd
121c6 2c 20 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50  , (u8*)aData, pP
121c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
121c8 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20  (*pOffset)+4);. 
121c9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
121ca 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
121cb 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50    *pOffset += pP
121cc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b  ager->pageSize +
121cd 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a   4 + isMainJrnl*
121ce 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20  4;..  /* Sanity 
121cf 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20  checking on the 
121d0 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d  page.  This is m
121d1 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68  ore important th
121d2 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a  at I originally.
121d3 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49    ** thought.  I
121d4 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
121d5 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  e occurs while t
121d6 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65  he journal is be
121d7 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a  ing written,.  *
121d8 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65  * it could cause
121d9 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f   invalid data to
121da 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
121db 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57   the journal.  W
121dc 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64  e need to.  ** d
121dd 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c  etect this inval
121de 69 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69  id data (with hi
121df 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20  gh probability) 
121e0 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20  and ignore it.. 
121e1 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d   */.  if( pgno==
121e2 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52  0 || pgno==PAGER
121e3 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
121e4 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
121e5 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20  !isSavepnt );.  
121e6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
121e7 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  DONE;.  }.  if( 
121e8 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65  pgno>(Pgno)pPage
121e9 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c  r->dbSize || sql
121ea 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
121eb 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20  Done, pgno) ){. 
121ec 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
121ed 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  _OK;.  }.  if( i
121ee 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20  sMainJrnl ){.   
121ef 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73   rc = read32bits
121f0 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29  (jfd, (*pOffset)
121f1 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20  -4, &cksum);.   
121f2 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
121f3 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73   rc;.    if( !is
121f4 53 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72  Savepnt && pager
121f5 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28  _cksum(pPager, (
121f6 75 38 2a 29 61 44 61 74 61 29 21 3d 63 6b 73 75  u8*)aData)!=cksu
121f7 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  m ){.      retur
121f8 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
121f9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
121fa 70 44 6f 6e 65 20 26 26 20 28 72 63 20 3d 20 73  pDone && (rc = s
121fb 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
121fc 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 21 3d 53  pDone, pgno))!=S
121fd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
121fe 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
121ff 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12200 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52  ->state==PAGER_R
12201 45 53 45 52 56 45 44 20 7c 7c 20 70 50 61 67 65  ESERVED || pPage
12202 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
12203 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20  EXCLUSIVE );..  
12204 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  /* If the pager 
12205 69 73 20 69 6e 20 52 45 53 45 52 56 45 44 20 73  is in RESERVED s
12206 74 61 74 65 2c 20 74 68 65 6e 20 74 68 65 72 65  tate, then there
12207 20 6d 75 73 74 20 62 65 20 61 20 63 6f 70 79 20   must be a copy 
12208 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 70 61 67  of this.  ** pag
12209 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  e in the pager c
1220a 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61  ache. In this ca
1220b 73 65 20 6a 75 73 74 20 75 70 64 61 74 65 20 74  se just update t
1220c 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2c 0a  he pager cache,.
1220d 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20 64 61 74    ** not the dat
1220e 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20  abase file. The 
1220f 70 61 67 65 20 69 73 20 6c 65 66 74 20 6d 61 72  page is left mar
12210 6b 65 64 20 64 69 72 74 79 20 69 6e 20 74 68 69  ked dirty in thi
12211 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  s case..  **.  *
12212 2a 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 74  * An exception t
12213 6f 20 74 68 65 20 61 62 6f 76 65 20 72 75 6c 65  o the above rule
12214 3a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  : If the databas
12215 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20  e is in no-sync 
12216 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20  mode.  ** and a 
12217 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20 64 75  page is moved du
12218 72 69 6e 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e  ring an incremen
12219 74 61 6c 20 76 61 63 75 75 6d 20 74 68 65 6e 20  tal vacuum then 
1221a 74 68 65 20 70 61 67 65 20 6d 61 79 0a 20 20 2a  the page may.  *
1221b 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20  * not be in the 
1221c 70 61 67 65 72 20 63 61 63 68 65 2e 20 4c 61 74  pager cache. Lat
1221d 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28  er: if a malloc(
1221e 29 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63  ) or IO error oc
1221f 63 75 72 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67  curs.  ** during
12220 20 61 20 4d 6f 76 65 70 61 67 65 28 29 20 63 61   a Movepage() ca
12221 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67  ll, then the pag
12222 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 20  e may not be in 
12223 74 68 65 20 63 61 63 68 65 0a 20 20 2a 2a 20 65  the cache.  ** e
12224 69 74 68 65 72 2e 20 53 6f 20 74 68 65 20 63 6f  ither. So the co
12225 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65  ndition describe
12226 64 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 70  d in the above p
12227 61 72 61 67 72 61 70 68 20 69 73 20 6e 6f 74 0a  aragraph is not.
12228 20 20 2a 2a 20 61 73 73 65 72 74 28 29 61 62 6c    ** assert()abl
12229 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  e..  **.  ** If 
1222a 69 6e 20 45 58 43 4c 55 53 49 56 45 20 73 74 61  in EXCLUSIVE sta
1222b 74 65 2c 20 74 68 65 6e 20 77 65 20 75 70 64 61  te, then we upda
1222c 74 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63  te the pager cac
1222d 68 65 20 69 66 20 69 74 20 65 78 69 73 74 73 0a  he if it exists.
1222e 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 69    ** and the mai
1222f 6e 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65  n file. The page
12230 20 69 73 20 74 68 65 6e 20 6d 61 72 6b 65 64 20   is then marked 
12231 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a  not dirty..  **.
12232 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 31 31 37    ** Ticket #117
12233 31 3a 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e  1:  The statemen
12234 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20  t journal might 
12235 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 63 6f 6e  contain page con
12236 74 65 6e 74 20 74 68 61 74 20 69 73 0a 20 20 2a  tent that is.  *
12237 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  * different from
12238 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e   the page conten
12239 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  t at the start o
1223a 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
1223b 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f 63 63  n..  ** This occ
1223c 75 72 73 20 77 68 65 6e 20 61 20 70 61 67 65 20  urs when a page 
1223d 69 73 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72  is changed prior
1223e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1223f 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a   a statement.  *
12240 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 64 20 61  * then changed a
12241 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 65 20  gain within the 
12242 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e  statement.  When
12243 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75   rolling back su
12244 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d  ch a.  ** statem
12245 65 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f 74 20  ent we must not 
12246 77 72 69 74 65 20 74 6f 20 74 68 65 20 6f 72 69  write to the ori
12247 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 75  ginal database u
12248 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20  nless we know.  
12249 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 6e 20 74  ** for certain t
1224a 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  hat original pag
1224b 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 73  e contents are s
1224c 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6d  ynced into the m
1224d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a  ain rollback.  *
1224e 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65  * journal.  Othe
1224f 72 77 69 73 65 2c 20 61 20 70 6f 77 65 72 20 6c  rwise, a power l
12250 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 76 65 20  oss might leave 
12251 6d 6f 64 69 66 69 65 64 20 64 61 74 61 20 69 6e  modified data in
12252 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
12253 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20  se file without 
12254 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  an entry in the 
12255 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
12256 20 74 68 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72   that can.  ** r
12257 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62  estore the datab
12258 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69  ase to its origi
12259 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63  nal form.  Two c
1225a 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74 20 62  onditions must b
1225b 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72  e.  ** met befor
1225c 65 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65  e writing to the
1225d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e   database files.
1225e 20 28 31 29 20 74 68 65 20 64 61 74 61 62 61 73   (1) the databas
1225f 65 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c  e must be.  ** l
12260 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 65 20 6b  ocked.  (2) we k
12261 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f 72 69  now that the ori
12262 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65  ginal page conte
12263 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 79 6e 63  nt is fully sync
12264 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d  ed.  ** in the m
12265 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68  ain journal eith
12266 65 72 20 62 65 63 61 75 73 65 20 74 68 65 20 70  er because the p
12267 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61  age is not in ca
12268 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a  che or else.  **
12269 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72   the page is mar
1226a 6b 65 64 20 61 73 20 6e 65 65 64 53 79 6e 63 3d  ked as needSync=
1226b 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30  =0..  **.  ** 20
1226c 30 38 2d 30 34 2d 31 34 3a 20 20 57 68 65 6e 20  08-04-14:  When 
1226d 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 76 61  attempting to va
1226e 63 75 75 6d 20 61 20 63 6f 72 72 75 70 74 20 64  cuum a corrupt d
1226f 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 74  atabase file, it
12270 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c  .  ** is possibl
12271 65 20 74 6f 20 66 61 69 6c 20 61 20 73 74 61 74  e to fail a stat
12272 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61 74 61 62  ement on a datab
12273 61 73 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ase that does no
12274 74 20 79 65 74 20 65 78 69 73 74 2e 0a 20 20 2a  t yet exist..  *
12275 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  * Do not attempt
12276 20 74 6f 20 77 72 69 74 65 20 69 66 20 64 61 74   to write if dat
12277 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e  abase file has n
12278 65 76 65 72 20 62 65 65 6e 20 6f 70 65 6e 65 64  ever been opened
12279 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 20 3d 20 70  ..  */.  pPg = p
1227a 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
1227b 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73  er, pgno);.  ass
1227c 65 72 74 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d  ert( pPg || !MEM
1227d 44 42 20 29 3b 0a 20 20 50 41 47 45 52 54 52 41  DB );.  PAGERTRA
1227e 43 45 28 28 22 50 4c 41 59 42 41 43 4b 20 25 64  CE(("PLAYBACK %d
1227f 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
12280 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20  8x) %s\n",.     
12281 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
12282 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67  ager), pgno, pag
12283 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67  er_datahash(pPag
12284 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75  er->pageSize, (u
12285 38 2a 29 61 44 61 74 61 29 2c 0a 20 20 20 20 20  8*)aData),.     
12286 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e        (isMainJrn
12287 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22  l?"main-journal"
12288 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a  :"sub-journal").
12289 20 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61    ));.  if( (pPa
1228a 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
1228b 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20  R_EXCLUSIVE).   
1228c 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d  && (pPg==0 || 0=
1228d 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  =(pPg->flags&PGH
1228e 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20  DR_NEED_SYNC)). 
1228f 20 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67    && isOpen(pPag
12290 65 72 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69  er->fd).   && !i
12291 73 55 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20  sUnsync.  ){.   
12292 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e   i64 ofst = (pgn
12293 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72  o-1)*(i64)pPager
12294 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  ->pageSize;.    
12295 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
12296 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
12297 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67  (u8*)aData, pPag
12298 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66  er->pageSize, of
12299 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e  st);.    if( pgn
1229a 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  o>pPager->dbFile
1229b 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50  Size ){.      pP
1229c 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
1229d 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20   = pgno;.    }. 
1229e 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70     if( pPager->p
1229f 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 20 20  Backup ){.      
122a0 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 61  CODEC1(pPager, a
122a1 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72  Data, pgno, 3, r
122a2 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  c=SQLITE_NOMEM);
122a3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61  .      sqlite3Ba
122a4 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65  ckupUpdate(pPage
122a5 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f  r->pBackup, pgno
122a6 2c 20 28 75 38 2a 29 61 44 61 74 61 29 3b 0a 20  , (u8*)aData);. 
122a7 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67       CODEC2(pPag
122a8 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c  er, aData, pgno,
122a9 20 37 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f   7, rc=SQLITE_NO
122aa 4d 45 4d 2c 20 61 44 61 74 61 29 3b 0a 20 20 20  MEM, aData);.   
122ab 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21   }.  }else if( !
122ac 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50  isMainJrnl && pP
122ad 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49  g==0 ){.    /* I
122ae 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c  f this is a roll
122af 62 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f  back of a savepo
122b0 69 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73  int and data was
122b1 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a   not written to.
122b2 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
122b3 61 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65  ase and the page
122b4 20 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72   is not in-memor
122b5 79 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f  y, there is a po
122b6 74 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70  tential.    ** p
122b7 72 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65  roblem. When the
122b8 20 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65   page is next fe
122b9 74 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74  tched by the b-t
122ba 72 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20  ree layer, it . 
122bb 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65     ** will be re
122bc 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
122bd 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68  base file, which
122be 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
122bf 62 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65  be .    ** curre
122c0 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  nt. .    **.    
122c1 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63  ** There are a c
122c2 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65  ouple of differe
122c3 6e 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e  nt ways this can
122c4 20 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65   happen. All are
122c5 20 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62   quite.    ** ob
122c6 73 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e  scure. When runn
122c7 69 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f  ing in synchrono
122c8 75 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61  us mode, this ca
122c9 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20  n only happen . 
122ca 20 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67     ** if the pag
122cb 65 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65  e is on the free
122cc 2d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61  -list at the sta
122cd 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
122ce 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20  ction, then.    
122cf 2a 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68  ** populated, th
122d0 65 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73  en moved using s
122d1 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
122d2 61 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  age()..    **.  
122d3 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f    ** The solutio
122d4 6e 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69  n is to add an i
122d5 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f  n-memory page to
122d6 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61   the cache conta
122d7 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65  ining.    ** the
122d8 20 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20   data just read 
122d9 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75  from the sub-jou
122da 72 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70  rnal. Mark the p
122db 61 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20  age as dirty .  
122dc 20 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20    ** and if the 
122dd 70 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61  pager requires a
122de 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74   journal-sync, t
122df 68 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67  hen mark the pag
122e0 65 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71  e as .    ** req
122e1 75 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c  uiring a journal
122e2 2d 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20  -sync before it 
122e3 69 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20  is written..    
122e4 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  */.    assert( i
122e5 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20  sSavepnt );.    
122e6 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  if( (rc = sqlite
122e7 33 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50  3PagerAcquire(pP
122e8 61 67 65 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67  ager, pgno, &pPg
122e9 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  , 1))!=SQLITE_OK
122ea 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
122eb 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   rc;.    }.    p
122ec 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47  Pg->flags &= ~PG
122ed 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20  HDR_NEED_READ;. 
122ee 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
122ef 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  MakeDirty(pPg);.
122f0 20 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b    }.  if( pPg ){
122f1 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20  .    /* No page 
122f2 73 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65  should ever be e
122f3 78 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64  xplicitly rolled
122f4 20 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e   back that is in
122f5 20 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20   use, except.   
122f6 20 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77   ** for page 1 w
122f7 68 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20  hich is held in 
122f8 75 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  use in order to 
122f9 6b 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e  keep the lock on
122fa 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61   the.    ** data
122fb 62 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77  base active. How
122fc 65 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65  ever such a page
122fd 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62   may be rolled b
122fe 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a  ack as a result.
122ff 20 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74      ** of an int
12300 65 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75  ernal error resu
12301 6c 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f  lting in an auto
12302 6d 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20  matic call to.  
12303 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65    ** sqlite3Page
12304 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20  rRollback()..   
12305 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44   */.    void *pD
12306 61 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d  ata;.    pData =
12307 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20   pPg->pData;.   
12308 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 28   memcpy(pData, (
12309 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67 65  u8*)aData, pPage
1230a 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r->pageSize);.  
1230b 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69    pPager->xReini
1230c 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 69 66  ter(pPg);.    if
1230d 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20  ( isMainJrnl && 
1230e 28 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 2a  (!isSavepnt || *
1230f 70 4f 66 66 73 65 74 3c 3d 70 50 61 67 65 72 2d  pOffset<=pPager-
12310 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 20 29 7b 0a  >journalHdr) ){.
12311 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
12312 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73  contents of this
12313 20 70 61 67 65 20 77 65 72 65 20 6a 75 73 74 20   page were just 
12314 72 65 73 74 6f 72 65 64 20 66 72 6f 6d 20 74 68  restored from th
12315 65 20 6d 61 69 6e 20 0a 20 20 20 20 20 20 2a 2a  e main .      **
12316 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74   journal file, t
12317 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 20  hen its content 
12318 6d 75 73 74 20 62 65 20 61 73 20 74 68 65 79 20  must be as they 
12319 77 65 72 65 20 77 68 65 6e 20 74 68 65 20 0a 20  were when the . 
1231a 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74       ** transact
1231b 69 6f 6e 20 77 61 73 20 66 69 72 73 74 20 6f 70  ion was first op
1231c 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  ened. In this ca
1231d 73 65 20 77 65 20 63 61 6e 20 6d 61 72 6b 20 74  se we can mark t
1231e 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  he page.      **
1231f 20 61 73 20 63 6c 65 61 6e 2c 20 73 69 6e 63 65   as clean, since
12320 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e   there will be n
12321 6f 20 6e 65 65 64 20 74 6f 20 77 72 69 74 65 20  o need to write 
12322 69 74 20 6f 75 74 20 74 6f 20 74 68 65 2e 0a 20  it out to the.. 
12323 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
12324 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65 78   There is one ex
12325 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 69 73 20  ception to this 
12326 72 75 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67  rule. If the pag
12327 65 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65  e is being rolle
12328 64 0a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20  d.      ** back 
12329 61 73 20 70 61 72 74 20 6f 66 20 61 20 73 61 76  as part of a sav
1232a 65 70 6f 69 6e 74 20 28 6f 72 20 73 74 61 74 65  epoint (or state
1232b 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 63 6b 20 66  ment) rollback f
1232c 72 6f 6d 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a  rom an .      **
1232d 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f   unsynced portio
1232e 6e 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  n of the main jo
1232f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e  urnal file, then
12330 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a   it is not safe.
12331 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 72 6b        ** to mark
12332 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65   the page as cle
12333 61 6e 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  an. This is beca
12334 75 73 65 20 6d 61 72 6b 69 6e 67 20 74 68 65 20  use marking the 
12335 70 61 67 65 20 61 73 0a 20 20 20 20 20 20 2a 2a  page as.      **
12336 20 63 6c 65 61 6e 20 77 69 6c 6c 20 63 6c 65 61   clean will clea
12337 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44  r the PGHDR_NEED
12338 5f 53 59 4e 43 20 66 6c 61 67 2e 20 53 69 6e 63  _SYNC flag. Sinc
12339 65 20 74 68 65 20 70 61 67 65 20 69 73 0a 20 20  e the page is.  
1233a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 69      ** already i
1233b 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
1233c 6c 65 20 28 72 65 63 6f 72 64 65 64 20 69 6e 20  le (recorded in 
1233d 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c  Pager.pInJournal
1233e 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 74  ) and.      ** t
1233f 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
12340 4e 43 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  NC flag is clear
12341 65 64 2c 20 69 66 20 74 68 65 20 70 61 67 65 20  ed, if the page 
12342 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20  is written to.  
12343 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74      ** again wit
12344 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63  hin this transac
12345 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65  tion, it will be
12346 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79   marked as dirty
12347 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 68   but.      ** th
12348 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
12349 43 20 66 6c 61 67 20 77 69 6c 6c 20 6e 6f 74 20  C flag will not 
1234a 62 65 20 73 65 74 2e 20 49 74 20 63 6f 75 6c 64  be set. It could
1234b 20 74 68 65 6e 20 70 6f 74 65 6e 74 69 61 6c 6c   then potentiall
1234c 79 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 77 72  y.      ** be wr
1234d 69 74 74 65 6e 20 6f 75 74 20 69 6e 74 6f 20 74  itten out into t
1234e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1234f 20 62 65 66 6f 72 65 20 69 74 73 20 6a 6f 75 72   before its jour
12350 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 2a  nal file.      *
12351 2a 20 73 65 67 6d 65 6e 74 20 69 73 20 73 79 6e  * segment is syn
12352 63 65 64 2e 20 49 66 20 61 20 63 72 61 73 68 20  ced. If a crash 
12353 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6f 72  occurs during or
12354 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 2c   following this,
12355 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
12356 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61  se corruption ma
12357 79 20 65 6e 73 75 65 2e 0a 20 20 20 20 20 20 2a  y ensue..      *
12358 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  /.      sqlite3P
12359 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70  cacheMakeClean(p
1235a 50 67 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65  Pg);.    }.#ifde
1235b 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
1235c 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61  AGES.    pPg->pa
1235d 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
1235e 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65  agehash(pPg);.#e
1235f 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20 74  ndif.    /* If t
12360 68 69 73 20 77 61 73 20 70 61 67 65 20 31 2c 20  his was page 1, 
12361 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65  then restore the
12362 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e   value of Pager.
12363 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 20  dbFileVers..    
12364 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f 72  ** Do this befor
12365 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e 20  e any decoding. 
12366 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d  */.    if( pgno=
12367 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  =1 ){.      memc
12368 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69  py(&pPager->dbFi
12369 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29 70  leVers, &((u8*)p
1236a 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f 66  Data)[24],sizeof
1236b 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56  (pPager->dbFileV
1236c 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  ers));.    }..  
1236d 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20    /* Decode the 
1236e 70 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 66  page just read f
1236f 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20  rom disk */.    
12370 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70  CODEC1(pPager, p
12371 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  Data, pPg->pgno,
12372 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f   3, rc=SQLITE_NO
12373 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  MEM);.    sqlite
12374 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 70  3PcacheRelease(p
12375 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Pg);.  }.  retur
12376 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  n rc;.}../*.** P
12377 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 72  arameter zMaster
12378 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
12379 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
1237a 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 20   file. A single 
1237b 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
1237c 74 68 61 74 20 72 65 66 65 72 72 65 64 20 74 6f  that referred to
1237d 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1237e 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 73  nal file has jus
1237f 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  t been rolled ba
12380 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ck..** This rout
12381 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69 74  ine checks if it
12382 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   is possible to 
12383 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
12384 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 0a  r journal file,.
12385 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 69  ** and does so i
12386 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41  f it is..**.** A
12387 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 20  rgument zMaster 
12388 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 67  may point to Pag
12389 65 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 6f  er.pTmpSpace. So
1238a 20 74 68 61 74 20 62 75 66 66 65 72 20 69 73 20   that buffer is 
1238b 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c  not .** availabl
1238c 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e  e for use within
1238d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
1238e 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 73  **.** When a mas
1238f 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
12390 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20   is created, it 
12391 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
12392 68 20 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a 20  h the names .** 
12393 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68  of all of its ch
12394 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e  ild journals, on
12395 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72 2c  e after another,
12396 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75 74   formatted as ut
12397 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64 20  f-8 .** encoded 
12398 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f 66  text. The end of
12399 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72   each child jour
1239a 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72 6b  nal file is mark
1239b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e 75  ed with a .** nu
1239c 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74  l-terminator byt
1239d 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e 20 74  e (0x00). i.e. t
1239e 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e  he entire conten
1239f 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  ts of a master j
123a0 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 66  ournal.** file f
123a1 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  or a transaction
123a2 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20 64   involving two d
123a3 61 74 61 62 61 73 65 73 20 6d 69 67 68 74 20 62  atabases might b
123a4 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d  e:.**.**   "/hom
123a5 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72  e/bill/a.db-jour
123a6 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69 6c  nal\x00/home/bil
123a7 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78  l/b.db-journal\x
123a8 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74  00".**.** A mast
123a9 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
123aa 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c 65  may only be dele
123ab 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66 20  ted once all of 
123ac 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f  its child .** jo
123ad 75 72 6e 61 6c 73 20 68 61 76 65 20 62 65 65 6e  urnals have been
123ae 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
123af 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
123b0 6e 20 72 65 61 64 73 20 74 68 65 20 63 6f 6e 74  n reads the cont
123b1 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74  ents of the mast
123b2 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er-journal file 
123b3 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20  into .** memory 
123b4 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  and loops throug
123b5 68 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 68  h each of the ch
123b6 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ild journal name
123b7 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20 63  s. For.** each c
123b8 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69 74  hild journal, it
123b9 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a   checks if:.**.*
123ba 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 69  *   * if the chi
123bb 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74  ld journal exist
123bc 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a 20  s, and if so.** 
123bd 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64    * if the child
123be 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e   journal contain
123bf 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
123c0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
123c1 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d 61  .**     file zMa
123c2 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ster.**.** If a 
123c3 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61  child journal ca
123c4 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74 20  n be found that 
123c5 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f 66 20  matches both of 
123c6 74 68 65 20 63 72 69 74 65 72 69 61 0a 2a 2a 20  the criteria.** 
123c7 61 62 6f 76 65 2c 20 74 68 69 73 20 66 75 6e 63  above, this func
123c8 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 74  tion returns wit
123c9 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68  hout doing anyth
123ca 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ing. Otherwise, 
123cb 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63 68  if.** no such ch
123cc 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20  ild journal can 
123cd 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20 7a  be found, file z
123ce 4d 61 73 74 65 72 20 69 73 20 64 65 6c 65 74 65  Master is delete
123cf 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66 69  d from.** the fi
123d0 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 6e 67 20  le-system using 
123d1 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
123d2 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49  )..**.** If an I
123d3 4f 20 65 72 72 6f 72 20 77 69 74 68 69 6e 20 74  O error within t
123d4 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e  his function, an
123d5 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
123d6 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a  eturned. This.**
123d7 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61   function alloca
123d8 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61  tes memory by ca
123d9 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c  lling sqlite3Mal
123da 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 61 6c 6c  loc(). If an all
123db 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73  ocation.** fails
123dc 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  , SQLITE_NOMEM i
123dd 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
123de 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f 20  rwise, if no IO 
123df 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  or malloc errors
123e0 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c 49   .** occur, SQLI
123e1 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
123e2 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54  d..**.** TODO: T
123e3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c  his function all
123e4 6f 63 61 74 65 73 20 61 20 73 69 6e 67 6c 65 20  ocates a single 
123e5 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
123e6 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20 65  to load.** the e
123e7 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f  ntire contents o
123e8 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
123e9 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
123ea 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63 6f  could be.** a co
123eb 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 65  uple of kilobyte
123ec 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e 74  s or so - potent
123ed 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61  ially larger tha
123ee 6e 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 73  n the page .** s
123ef 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
123f0 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74  nt pager_delmast
123f1 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  er(Pager *pPager
123f2 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d  , const char *zM
123f3 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  aster){.  sqlite
123f4 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50  3_vfs *pVfs = pP
123f5 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e  ager->pVfs;.  in
123f6 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
123f7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
123f8 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69  n code */.  sqli
123f9 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65  te3_file *pMaste
123fa 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27  r;    /* Malloc'
123fb 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  d master-journal
123fc 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
123fd 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
123fe 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20 20  le *pJournal;   
123ff 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69 6c  /* Malloc'd chil
12400 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64  d-journal file d
12401 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63  escriptor */.  c
12402 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72  har *zMasterJour
12403 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74  nal = 0; /* Cont
12404 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20 6a  ents of master j
12405 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
12406 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72   i64 nMasterJour
12407 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53 69  nal;       /* Si
12408 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75  ze of master jou
12409 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  rnal file */..  
1240a 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
1240b 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 70  e for both the p
1240c 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61 73  Journal and pMas
1240d 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70  ter file descrip
1240e 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 73 75  tors..  ** If su
1240f 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20 74  ccessful, open t
12410 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
12411 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69  l file for readi
12412 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74  ng..  */.  pMast
12413 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  er = (sqlite3_fi
12414 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  le *)sqlite3Mall
12415 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 7a 4f  ocZero(pVfs->szO
12416 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 70 4a  sFile * 2);.  pJ
12417 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 74 65  ournal = (sqlite
12418 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 20 2a  3_file *)(((u8 *
12419 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 66 73  )pMaster) + pVfs
1241a 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69  ->szOsFile);.  i
1241b 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b 0a 20  f( !pMaster ){. 
1241c 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
1241d 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
1241e 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61     const int fla
1241f 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45  gs = (SQLITE_OPE
12420 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54  N_READONLY|SQLIT
12421 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
12422 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d  URNAL);.    rc =
12423 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
12424 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d  Vfs, zMaster, pM
12425 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29  aster, flags, 0)
12426 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
12427 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
12428 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
12429 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1242a 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65  sFileSize(pMaste
1242b 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  r, &nMasterJourn
1242c 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  al);.  if( rc!=S
1242d 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
1242e 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a  delmaster_out;..
1242f 20 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f 75    if( nMasterJou
12430 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63 68  rnal>0 ){.    ch
12431 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20  ar *zJournal;.  
12432 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 50    char *zMasterP
12433 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  tr = 0;.    int 
12434 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56 66  nMasterPtr = pVf
12435 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b  s->mxPathname+1;
12436 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68  ..    /* Load th
12437 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20  e entire master 
12438 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74  journal file int
12439 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  o space obtained
1243a 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 6c   from.    ** sql
1243b 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e  ite3_malloc() an
1243c 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  d pointed to by 
1243d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e 20  zMasterJournal. 
1243e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 73  .    */.    zMas
1243f 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  terJournal = sql
12440 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29  ite3Malloc((int)
12441 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b  nMasterJournal +
12442 20 6e 4d 61 73 74 65 72 50 74 72 20 2b 20 31 29   nMasterPtr + 1)
12443 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74  ;.    if( !zMast
12444 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  erJournal ){.   
12445 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
12446 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
12447 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
12448 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65      }.    zMaste
12449 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a  rPtr = &zMasterJ
1244a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f  ournal[nMasterJo
1244b 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 20 20 72 63  urnal+1];.    rc
1244c 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
1244d 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65  (pMaster, zMaste
1244e 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e  rJournal, (int)n
1244f 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30  MasterJournal, 0
12450 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
12451 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
12452 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
12453 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61     zMasterJourna
12454 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l[nMasterJournal
12455 5d 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 4a 6f 75  ] = 0;..    zJou
12456 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f  rnal = zMasterJo
12457 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c 65  urnal;.    while
12458 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73  ( (zJournal-zMas
12459 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73  terJournal)<nMas
1245a 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  terJournal ){.  
1245b 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a      int exists;.
1245c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1245d 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c  e3OsAccess(pVfs,
1245e 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54   zJournal, SQLIT
1245f 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
12460 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 20   &exists);.     
12461 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12462 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
12463 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
12464 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12465 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20  if( exists ){.  
12466 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
12467 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69  the journals poi
12468 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d  nted to by the m
12469 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78  aster journal ex
1246a 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ists..        **
1246b 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65   Open it and che
1246c 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20  ck if it points 
1246d 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  at the master jo
1246e 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20  urnal. If.      
1246f 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20    ** so, return 
12470 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67  without deleting
12471 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
12472 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20  nal file..      
12473 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
12474 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   c;.        int 
12475 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f  flags = (SQLITE_
12476 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51  OPEN_READONLY|SQ
12477 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
12478 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20  OURNAL);.       
12479 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
1247a 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e  pen(pVfs, zJourn
1247b 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c  al, pJournal, fl
1247c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ags, 0);.       
1247d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1247e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1247f 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
12480 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
12481 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64         rc = read
12482 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a  MasterJournal(pJ
12483 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50  ournal, zMasterP
12484 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b  tr, nMasterPtr);
12485 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
12486 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c  OsClose(pJournal
12487 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
12488 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
12489 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64            goto d
1248a 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
1248b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
1248c 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b   c = zMasterPtr[
1248d 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28  0]!=0 && strcmp(
1248e 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73  zMasterPtr, zMas
1248f 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20  ter)==0;.       
12490 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20   if( c ){.      
12491 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61      /* We have a
12492 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64   match. Do not d
12493 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72  elete the master
12494 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a   journal file. *
12495 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  /.          goto
12496 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
12497 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12498 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c  }.      zJournal
12499 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c   += (sqlite3Strl
1249a 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31  en30(zJournal)+1
1249b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  );.    }.  }.  .
1249c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1249d 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61  Delete(pVfs, zMa
1249e 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61  ster, 0);..delma
1249f 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20  ster_out:.  if( 
124a0 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29  zMasterJournal )
124a1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
124a2 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  ee(zMasterJourna
124a3 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20  l);.  }  .  if( 
124a4 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73  pMaster ){.    s
124a5 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d  qlite3OsClose(pM
124a6 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65  aster);.    asse
124a7 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75  rt( !isOpen(pJou
124a8 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 73  rnal) );.  }.  s
124a9 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73  qlite3_free(pMas
124aa 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ter);.  return r
124ab 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c;.}.../*.** Thi
124ac 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
124ad 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
124ae 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20   actual size of 
124af 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  the database .**
124b0 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c   file in the fil
124b1 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f  e-system. This o
124b2 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e  nly happens when
124b3 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72   committing a tr
124b4 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72  ansaction,.** or
124b5 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
124b6 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63  transaction (inc
124b7 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62  luding rolling b
124b8 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  ack a hot-journa
124b9 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  l)..**.** If the
124ba 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
124bb 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c  ile is not open,
124bc 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65   or an exclusive
124bd 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a 20   lock is not.** 
124be 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74  held, this funct
124bf 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ion is a no-op. 
124c0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73  Otherwise, the s
124c1 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
124c2 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 6f  is.** changed to
124c3 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e 50   nPage pages (nP
124c4 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 65  age*pPager->page
124c5 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 20  Size bytes). If 
124c6 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64  the file.** on d
124c7 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79  isk is currently
124c8 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 61   larger than nPa
124c9 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 75  ge pages, then u
124ca 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 54  se the VFS.** xT
124cb 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64  runcate() method
124cc 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 2e   to truncate it.
124cd 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d 69  .**.** Or, it mi
124ce 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ght might be the
124cf 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66   case that the f
124d0 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73  ile on disk is s
124d1 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a 20  maller than .** 
124d2 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f 6d  nPage pages. Som
124d3 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
124d4 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  em implementatio
124d5 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75  ns can get confu
124d6 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 74  sed if .** you t
124d7 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 61  ry to truncate a
124d8 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69   file to some si
124d9 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65  ze that is large
124da 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 75  r than it .** cu
124db 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64  rrently is, so d
124dc 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20  etect this case 
124dd 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67  and write a sing
124de 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20  le zero byte to 
124df 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74  .** the end of t
124e0 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74  he new file inst
124e1 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ead..**.** If su
124e2 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
124e3 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61   SQLITE_OK. If a
124e4 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
124e5 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 6e  s while modifyin
124e6 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  g.** the databas
124e7 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 74  e file, return t
124e8 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f  he error code to
124e9 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a   the caller..*/.
124ea 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
124eb 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20  _truncate(Pager 
124ec 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50  *pPager, Pgno nP
124ed 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  age){.  int rc =
124ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
124ef 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
124f0 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45  =PAGER_EXCLUSIVE
124f1 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
124f2 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 36  r->fd) ){.    i6
124f3 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e  4 currentSize, n
124f4 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 54  ewSize;.    /* T
124f5 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20  ODO: Is it safe 
124f6 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46  to use Pager.dbF
124f7 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f  ileSize here? */
124f8 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
124f9 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67  3OsFileSize(pPag
124fa 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74  er->fd, &current
124fb 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69  Size);.    newSi
124fc 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67  ze = pPager->pag
124fd 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65  eSize*(i64)nPage
124fe 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
124ff 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65  LITE_OK && curre
12500 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20  ntSize!=newSize 
12501 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72  ){.      if( cur
12502 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65  rentSize>newSize
12503 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
12504 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
12505 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e  te(pPager->fd, n
12506 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  ewSize);.      }
12507 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
12508 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
12509 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22  e(pPager->fd, ""
1250a 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b  , 1, newSize-1);
1250b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1250c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1250d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67   ){.        pPag
1250e 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d  er->dbFileSize =
1250f 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a   nPage;.      }.
12510 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
12511 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12512 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
12513 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f   the Pager.secto
12514 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66  rSize variable f
12515 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  or the given.** 
12516 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74  pager based on t
12517 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
12518 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72  d by the xSector
12519 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  Size method.** o
1251a 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62  f the open datab
1251b 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65  ase file. The se
1251c 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62  ctor size will b
1251d 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20  e used used .** 
1251e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
1251f 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d   size and alignm
12520 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68  ent of journal h
12521 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61  eader and .** ma
12522 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster journal poi
12523 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65  nters within cre
12524 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ated journal fil
12525 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65  es..**.** For te
12526 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68  mporary files th
12527 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74  e effective sect
12528 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79  or size is alway
12529 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a  s 512 bytes..**.
1252a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
1252b 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20  r non-temporary 
1252c 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63  files, the effec
1252d 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65  tive sector size
1252e 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65   is.** the value
1252f 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
12530 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d   xSectorSize() m
12531 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70  ethod rounded up
12532 20 74 6f 20 33 32 20 69 66 0a 2a 2a 20 69 74 20   to 32 if.** it 
12533 69 73 20 6c 65 73 73 20 74 68 61 6e 20 33 32 2c  is less than 32,
12534 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e   or rounded down
12535 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53   to MAX_SECTOR_S
12536 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69 73 20  IZE if it.** is 
12537 67 72 65 61 74 65 72 20 74 68 61 6e 20 4d 41 58  greater than MAX
12538 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f  _SECTOR_SIZE..*/
12539 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
1253a 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72  SectorSize(Pager
1253b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73   *pPager){.  ass
1253c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
1253d 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65  er->fd) || pPage
1253e 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a  r->tempFile );..
1253f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74    if( !pPager->t
12540 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 2f  empFile ){.    /
12541 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 64 6f  * Sector size do
12542 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 6f 72  esn't matter for
12543 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
12544 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65  . Also, the file
12545 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20  .    ** may not 
12546 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64  have been opened
12547 20 79 65 74 2c 20 69 6e 20 77 68 69 63 68 20 63   yet, in which c
12548 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 6f 72  ase the OsSector
12549 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 63 61  Size().    ** ca
1254a 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 6c 74  ll will segfault
1254b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
1254c 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
1254d 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  = sqlite3OsSecto
1254e 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64  rSize(pPager->fd
1254f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
12550 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c  ger->sectorSize<
12551 33 32 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  32 ){.    pPager
12552 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35  ->sectorSize = 5
12553 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  12;.  }.  if( pP
12554 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
12555 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45  >MAX_SECTOR_SIZE
12556 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
12557 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e  MAX_SECTOR_SIZE>
12558 3d 35 31 32 20 29 3b 0a 20 20 20 20 70 50 61 67  =512 );.    pPag
12559 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d  er->sectorSize =
1255a 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45   MAX_SECTOR_SIZE
1255b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
1255c 6c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72  layback the jour
1255d 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73  nal and thus res
1255e 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  tore the databas
1255f 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65  e file to.** the
12560 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e   state it was in
12561 20 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74   before we start
12562 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  ed making change
12563 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a  s.  .**.** The j
12564 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d  ournal file form
12565 61 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  at is as follows
12566 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38  : .**.**  (1)  8
12567 20 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41   byte prefix.  A
12568 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61   copy of aJourna
12569 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32  lMagic[]..**  (2
1256a 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
1256b 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
1256c 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
1256d 20 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72   of valid page r
1256e 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20  ecords.**       
1256f 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  in the journal. 
12570 20 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69   If this value i
12571 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68  s 0xffffffff, th
12572 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a  en compute the.*
12573 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f  *       number o
12574 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66  f page records f
12575 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
12576 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34  size..**  (3)  4
12577 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e   byte big-endian
12578 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
12579 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61  s the initial va
1257a 6c 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  lue for the .** 
1257b 20 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65        sanity che
1257c 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20  cksum..**  (4)  
1257d 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77  4 byte integer w
1257e 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62  hich is the numb
1257f 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74  er of pages to t
12580 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20  runcate the.**  
12581 20 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f       database to
12582 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
12583 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62  ck..**  (5)  4 b
12584 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
12585 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
12586 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e  the sector size.
12587 20 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20    The header.** 
12588 20 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61        is this ma
12589 6e 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ny bytes in size
1258a 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74  ..**  (6)  4 byt
1258b 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
1258c 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
1258d 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20  e page size..** 
1258e 20 28 37 29 20 20 7a 65 72 6f 20 70 61 64 64 69   (7)  zero paddi
1258f 6e 67 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65  ng out to the ne
12590 78 74 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 0a  xt sector size..
12591 2a 2a 20 20 28 38 29 20 20 5a 65 72 6f 20 6f 72  **  (8)  Zero or
12592 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74   more pages inst
12593 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66  ances, each as f
12594 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20  ollows:.**      
12595 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65    +  4 byte page
12596 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20   number..**     
12597 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61     +  pPager->pa
12598 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20  geSize bytes of 
12599 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20  data..**        
1259a 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73  +  4 byte checks
1259b 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65  um.**.** When we
1259c 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f   speak of the jo
1259d 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65  urnal header, we
1259e 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20   mean the first 
1259f 37 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a  7 items above..*
125a0 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20  * Each entry in 
125a1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  the journal is a
125a2 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
125a3 65 20 38 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a  e 8th item..**.*
125a4 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65  * Call the value
125a5 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64   from the second
125a6 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20   bullet "nRec". 
125a7 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d   nRec is the num
125a8 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20  ber of.** valid 
125a9 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20  page entries in 
125aa 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e  the journal.  In
125ab 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75   most cases, you
125ac 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65   can compute the
125ad 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65  .** value of nRe
125ae 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20  c from the size 
125af 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
125b0 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70  ile.  But if a p
125b1 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20  ower.** failure 
125b2 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74  occurred while t
125b3 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62  he journal was b
125b4 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74  eing written, it
125b5 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a   could be the.**
125b6 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73   case that the s
125b7 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
125b8 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65  al file had alre
125b9 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73  ady been increas
125ba 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78  ed but.** the ex
125bb 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20  tra entries had 
125bc 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20  not yet made it 
125bd 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20  safely to disk. 
125be 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c   In such a case,
125bf 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
125c0 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66   nRec computed f
125c1 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a  rom the file siz
125c2 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c  e would be too l
125c3 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68  arge.  For.** th
125c4 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c  at reason, we al
125c5 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65  ways use the nRe
125c6 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68  c value in the h
125c7 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  eader..**.** If 
125c8 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69  the nRec value i
125c9 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20  s 0xffffffff it 
125ca 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20  means that nRec 
125cb 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74  should be comput
125cc 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66  ed.** from the f
125cd 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20  ile size.  This 
125ce 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68  value is used wh
125cf 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65  en the user sele
125d0 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79  cts the.** no-sy
125d1 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68  nc option for th
125d2 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f  e journal.  A po
125d3 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c  wer failure coul
125d4 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70  d lead to corrup
125d5 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20  tion.** in this 
125d6 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74  case.  But for t
125d7 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f  hings like tempo
125d8 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63  rary table (whic
125d9 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c  h will be.** del
125da 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f  eted when the po
125db 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29  wer is restored)
125dc 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20   we don't care. 
125dd 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66   .**.** If the f
125de 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68  ile opened as th
125df 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
125e0 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72  s not a well-for
125e1 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  med.** journal f
125e2 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67  ile then all pag
125e3 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72  es up to the fir
125e4 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67  st corrupted pag
125e5 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20  e are rolled.** 
125e6 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65  back (or no page
125e7 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s if the journal
125e8 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75   header is corru
125e9 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e  pted). The journ
125ea 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68  al file.** is th
125eb 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53  en deleted and S
125ec 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
125ed 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f  d, just as if no
125ee 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a   corruption had.
125ef 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65  ** been encounte
125f0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  red..**.** If an
125f1 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29   I/O or malloc()
125f2 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
125f3 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20  he journal-file 
125f4 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a  is not deleted.*
125f5 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63  * and an error c
125f6 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
125f7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74  .**.** The isHot
125f8 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63   parameter indic
125f9 61 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65  ates that we are
125fa 20 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62   trying to rollb
125fb 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ack a journal.**
125fc 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61   that might be a
125fd 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f   hot journal.  O
125fe 72 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74  r, it could be t
125ff 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
12600 69 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64  is .** preserved
12601 20 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52   because of JOUR
12602 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20  NALMODE_PERSIST 
12603 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54  or JOURNALMODE_T
12604 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74  RUNCATE..** If t
12605 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c  he journal reall
12606 79 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20  y is hot, reset 
12607 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20  the pager cache 
12608 70 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a  prior rolling.**
12609 20 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e   back any conten
1260a 74 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e  t.  If the journ
1260b 61 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72  al is merely per
1260c 73 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65  sistent, no rese
1260d 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a  t is.** needed..
1260e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
1260f 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67  ger_playback(Pag
12610 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
12611 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65  isHot){.  sqlite
12612 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50  3_vfs *pVfs = pP
12613 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36  ager->pVfs;.  i6
12614 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
12615 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
12616 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
12617 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
12618 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20   u32 nRec;      
12619 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1261a 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69  ber of Records i
1261b 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f  n the journal */
1261c 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20  .  u32 u;       
1261d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
1261e 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75  nsigned loop cou
1261f 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d  nter */.  Pgno m
12620 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  xPg = 0;        
12621 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
12622 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20  e original file 
12623 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e  in pages */.  in
12624 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
12625 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
12626 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f   code of a subro
12627 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
12628 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  es = 1;         
12629 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
1262a 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
1262b 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20  OsAccess() */.  
1262c 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20  char *zMaster = 
1262d 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  0;       /* Name
1262e 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
1262f 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a  al file if any *
12630 2f 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65  /.  int needPage
12631 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  rReset;      /* 
12632 54 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61  True to reset pa
12633 67 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73  ge prior to firs
12634 74 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20  t page rollback 
12635 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  */..  /* Figure 
12636 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63  out how many rec
12637 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20  ords are in the 
12638 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20  journal.  Abort 
12639 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68  early if.  ** th
1263a 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70  e journal is emp
1263b 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ty..  */.  asser
1263c 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
1263d 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d  ->jfd) );.  rc =
1263e 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
1263f 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ze(pPager->jfd, 
12640 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21  &szJ);.  if( rc!
12641 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a  =SQLITE_OK || sz
12642 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  J==0 ){.    goto
12643 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
12644 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68   }..  /* Read th
12645 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
12646 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a   name from the j
12647 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73  ournal, if it is
12648 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49   present..  ** I
12649 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
1264a 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20  al file name is 
1264b 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74  specified, but t
1264c 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20  he file is not. 
1264d 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64   ** present on d
1264e 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  isk, then the jo
1264f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74  urnal is not hot
12650 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65   and does not ne
12651 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c  ed to be.  ** pl
12652 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a  ayed back..  **.
12653 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e    ** TODO: Techn
12654 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f  ically the follo
12655 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72  wing is an error
12656 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75   because it assu
12657 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75  mes that.  ** bu
12658 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53  ffer Pager.pTmpS
12659 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e  pace is (mxPathn
1265a 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20  ame+1) bytes or 
1265b 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61  larger. i.e. tha
1265c 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e  t.  ** (pPager->
1265d 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67  pageSize >= pPag
1265e 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68  er->pVfs->mxPath
1265f 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f  name+1). Using o
12660 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20  s_unix.c,.  **  
12661 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31  mxPathname is 51
12662 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 20  2, which is the 
12663 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69  same as the mini
12664 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61  mum allowable va
12665 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67  lue.  ** for pag
12666 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d  eSize..  */.  zM
12667 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  aster = pPager->
12668 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20  pTmpSpace;.  rc 
12669 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
1266a 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  nal(pPager->jfd,
1266b 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
1266c 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  ->pVfs->mxPathna
1266d 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d  me+1);.  if( rc=
1266e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d  =SQLITE_OK && zM
1266f 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20  aster[0] ){.    
12670 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
12671 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74  cess(pVfs, zMast
12672 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  er, SQLITE_ACCES
12673 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b  S_EXISTS, &res);
12674 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d  .  }.  zMaster =
12675 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   0;.  if( rc!=SQ
12676 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20  LITE_OK || !res 
12677 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  ){.    goto end_
12678 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20  playback;.  }.  
12679 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
1267a 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61  ff = 0;.  needPa
1267b 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74  gerReset = isHot
1267c 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  ;..  /* This loo
1267d 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74  p terminates eit
1267e 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a  her when a readJ
1267f 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a  ournalHdr() or .
12680 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62    ** pager_playb
12681 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63  ack_one_page() c
12682 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49  all returns SQLI
12683 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f  TE_DONE or an IO
12684 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63   error .  ** occ
12685 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69  urs. .  */.  whi
12686 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74  le( 1 ){.    int
12687 20 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a   isUnsync = 0;..
12688 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
12689 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  next journal hea
1268a 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  der from the jou
1268b 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74  rnal file.  If t
1268c 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20  here are.    ** 
1268d 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73  not enough bytes
1268e 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75   left in the jou
1268f 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20  rnal file for a 
12690 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c  complete header,
12691 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73   or.    ** it is
12692 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e   corrupted, then
12693 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20   a process must 
12694 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20  of failed while 
12695 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20  writing it..    
12696 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65  ** This indicate
12697 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e  s nothing more n
12698 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65  eeds to be rolle
12699 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20  d back..    */. 
1269a 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72     rc = readJour
1269b 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69  nalHdr(pPager, i
1269c 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63  sHot, szJ, &nRec
1269d 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66  , &mxPg);.    if
1269e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1269f 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63  ){ .      if( rc
126a0 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
126a1 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
126a2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
126a3 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
126a4 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a  playback;.    }.
126a5 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20  .    /* If nRec 
126a6 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74  is 0xffffffff, t
126a7 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c  hen this journal
126a8 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20   was created by 
126a9 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a  a process.    **
126aa 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73   working in no-s
126ab 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d  ync mode. This m
126ac 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65  eans that the re
126ad 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  st of the journa
126ae 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f  l.    ** file co
126af 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c  nsists of pages,
126b0 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
126b1 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
126b2 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20  rs. Compute.    
126b3 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
126b4 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68  nRec based on th
126b5 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20  is assumption.. 
126b6 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52     */.    if( nR
126b7 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29  ec==0xffffffff )
126b8 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
126b9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
126ba 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
126bb 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  SZ(pPager) );.  
126bc 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29      nRec = (int)
126bd 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f  ((szJ - JOURNAL_
126be 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f  HDR_SZ(pPager))/
126bf 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
126c0 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20  ager));.    }.. 
126c1 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73     /* If nRec is
126c2 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c   0 and this roll
126c3 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61  back is of a tra
126c4 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64  nsaction created
126c5 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   by this.    ** 
126c6 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74  process and if t
126c7 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c  his is the final
126c8 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a   header in the j
126c9 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20  ournal, then it 
126ca 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61  means.    ** tha
126cb 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74  t this part of t
126cc 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62  he journal was b
126cd 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20  eing filled but 
126ce 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
126cf 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74  .    ** synced t
126d0 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65  o disk.  Compute
126d1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
126d2 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68  ages based on th
126d3 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20  e remaining.    
126d4 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  ** size of the f
126d5 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
126d6 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72  ** The third ter
126d7 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61  m of the test wa
126d8 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74  s added to fix t
126d9 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20  icket #2565..   
126da 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67   ** When rolling
126db 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72   back a hot jour
126dc 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77  nal, nRec==0 alw
126dd 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ays means that t
126de 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63  he next.    ** c
126df 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72  hunk of the jour
126e0 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72  nal contains zer
126e1 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f  o pages to be ro
126e2 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a  lled back.  But.
126e3 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e      ** when doin
126e4 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64  g a ROLLBACK and
126e5 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75   the nRec==0 chu
126e6 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63  nk is the last c
126e7 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74  hunk in.    ** t
126e8 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d  he journal, it m
126e9 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f  eans that the jo
126ea 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74  urnal might cont
126eb 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20  ain additional. 
126ec 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74     ** pages that
126ed 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c   need to be roll
126ee 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74  ed back and that
126ef 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
126f0 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f  ages .    ** sho
126f1 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20  uld be computed 
126f2 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75  based on the jou
126f3 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a  rnal file size..
126f4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
126f5 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74  Rec==0 && !isHot
126f6 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67   &&.        pPag
126f7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
126f8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
126f9 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
126fa 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20  ournalOff ){.   
126fb 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28     nRec = (int)(
126fc 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a  (szJ - pPager->j
126fd 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55  ournalOff) / JOU
126fe 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
126ff 72 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e 73  r));.      isUns
12700 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a  ync = 1;.    }..
12701 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
12702 73 20 74 68 65 20 66 69 72 73 74 20 68 65 61 64  s the first head
12703 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  er read from the
12704 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61   journal, trunca
12705 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  te the.    ** da
12706 74 61 62 61 73 65 20 66 69 6c 65 20 62 61 63 6b  tabase file back
12707 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
12708 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   size..    */.  
12709 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f    if( pPager->jo
1270a 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41  urnalOff==JOURNA
1270b 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
1270c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
1270d 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50  ager_truncate(pP
1270e 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20  ager, mxPg);.   
1270f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12710 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
12711 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63  goto end_playbac
12712 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
12713 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
12714 3d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20  = mxPg;.    }.. 
12715 20 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69     /* Copy origi
12716 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66  nal pages out of
12717 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64   the journal and
12718 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a   back into the .
12719 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
1271a 66 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 65  file and/or page
1271b 20 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20   cache..    */. 
1271c 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52     for(u=0; u<nR
1271d 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20  ec; u++){.      
1271e 69 66 28 20 6e 65 65 64 50 61 67 65 72 52 65 73  if( needPagerRes
1271f 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61  et ){.        pa
12720 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
12721 29 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64 50  );.        needP
12722 61 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a 20  agerReset = 0;. 
12723 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
12724 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
12725 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72  _one_page(pPager
12726 2c 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 61  ,1,isUnsync,&pPa
12727 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ger->journalOff,
12728 30 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  0,0);.      if( 
12729 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1272a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
1272b 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
1272c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1272d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1272e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1272f 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20  nalOff = szJ;.  
12730 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
12731 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
12732 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65          /* If we
12733 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72   are unable to r
12734 6f 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e  ollback, quit an
12735 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  d return the err
12736 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  or.          ** 
12737 63 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c  code.  This will
12738 20 63 61 75 73 65 20 74 68 65 20 70 61 67 65 72   cause the pager
12739 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72   to enter the er
1273a 72 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20 20  ror state.      
1273b 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e      ** so that n
1273c 6f 20 66 75 72 74 68 65 72 20 68 61 72 6d 20 77  o further harm w
1273d 69 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65  ill be done.  Pe
1273e 72 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20  rhaps the next. 
1273f 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63           ** proc
12740 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e  ess to come alon
12741 67 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74  g will be able t
12742 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64  o rollback the d
12743 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20  atabase..       
12744 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
12745 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63  goto end_playbac
12746 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
12747 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
12748 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a   /*NOTREACHED*/.
12749 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a    assert( 0 );..
1274a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20  end_playback:.  
1274b 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72  /* Following a r
1274c 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74  ollback, the dat
1274d 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c  abase file shoul
1274e 64 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73  d be back in its
1274f 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73   original.  ** s
12750 74 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68  tate prior to th
12751 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74  e start of the t
12752 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69  ransaction, so i
12753 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53  nvoke the.  ** S
12754 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55  QLITE_FCNTL_DB_U
12755 4e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f  NCHANGED file-co
12756 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20  ntrol method to 
12757 64 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a  disable the.  **
12758 20 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20   assertion that 
12759 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1275a 63 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69  counter was modi
1275b 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  fied..  */.  ass
1275c 65 72 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d  ert(.    pPager-
1275d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30  >fd->pMethods==0
1275e 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   ||.    sqlite3O
1275f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61  sFileControl(pPa
12760 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46  ger->fd,SQLITE_F
12761 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45  CNTL_DB_UNCHANGE
12762 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a  D,0)>=SQLITE_OK.
12763 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68    );..  /* If th
12764 69 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68  is playback is h
12765 61 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74  appening automat
12766 69 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75  ically as a resu
12767 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a  lt of an IO or .
12768 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f    ** malloc erro
12769 72 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20  r that occurred 
1276a 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 65  after the change
1276b 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64  -counter was upd
1276c 61 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62  ated but .  ** b
1276d 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
1276e 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74  ction was commit
1276f 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68  ted, then the ch
12770 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20  ange-counter .  
12771 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  ** modification 
12772 6d 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 65  may just have be
12773 65 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 20  en reverted. If 
12774 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20  this happens in 
12775 65 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20  exclusive .  ** 
12776 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65  mode, then subse
12777 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  quent transactio
12778 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20  ns performed by 
12779 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  the connection w
1277a 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64  ill not.  ** upd
1277b 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63  ate the change-c
1277c 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54  ounter at all. T
1277d 68 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20  his may lead to 
1277e 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65  cache inconsiste
1277f 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d  ncy.  ** problem
12780 73 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63  s for other proc
12781 65 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f  esses at some po
12782 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72  int in the futur
12783 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a  e. So, just.  **
12784 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61   in case this ha
12785 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61  s happened, clea
12786 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e  r the changeCoun
12787 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a  tDone flag now..
12788 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63    */.  pPager->c
12789 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
1278a 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
1278b 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e;..  if( rc==SQ
1278c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a  LITE_OK ){.    z
1278d 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d  Master = pPager-
1278e 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20  >pTmpSpace;.    
1278f 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a  rc = readMasterJ
12790 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a  ournal(pPager->j
12791 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61  fd, zMaster, pPa
12792 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74  ger->pVfs->mxPat
12793 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65  hname+1);.    te
12794 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49  stcase( rc!=SQLI
12795 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69  TE_OK );.  }.  i
12796 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12797 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
12798 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
12799 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  on(pPager, zMast
1279a 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20  er[0]!='\0');.  
1279b 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
1279c 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d  SQLITE_OK );.  }
1279d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1279e 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b  E_OK && zMaster[
1279f 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20  0] && res ){.   
127a0 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73   /* If there was
127a1 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
127a2 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69  l and this routi
127a3 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73  ne will return s
127a4 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73  uccess,.    ** s
127a5 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73  ee if it is poss
127a6 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74  ible to delete t
127a7 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
127a8 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  l..    */.    rc
127a9 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74   = pager_delmast
127aa 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  er(pPager, zMast
127ab 65 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  er);.    testcas
127ac 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc!=SQLITE_OK
127ad 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   );.  }..  /* Th
127ae 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69  e Pager.sectorSi
127af 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  ze variable may 
127b0 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65  have been update
127b1 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a  d while rolling.
127b2 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72    ** back a jour
127b3 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61  nal created by a
127b4 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20   process with a 
127b5 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72  different sector
127b6 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65   size.  ** value
127b7 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68  . Reset it to th
127b8 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20  e correct value 
127b9 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73  for this process
127ba 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74  ..  */.  setSect
127bb 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  orSize(pPager);.
127bc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
127bd 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73  /*.** Playback s
127be 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70 6f  avepoint pSavepo
127bf 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 76  int. Or, if pSav
127c0 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68  epoint==NULL, th
127c1 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74  en playback.** t
127c2 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
127c3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54   journal file. T
127c4 68 65 20 63 61 73 65 20 70 53 61 76 65 70 6f 69  he case pSavepoi
127c5 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20  nt==NULL occurs 
127c6 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42  when .** a ROLLB
127c7 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69  ACK TO command i
127c8 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53  s invoked on a S
127c9 41 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69 73  AVEPOINT that is
127ca 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a   a transaction .
127cb 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a  ** savepoint..**
127cc 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70 6f  .** When pSavepo
127cd 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  int is not NULL 
127ce 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74  (meaning a non-t
127cf 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70  ransaction savep
127d0 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e  oint is .** bein
127d1 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20  g rolled back), 
127d2 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  then the rollbac
127d3 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 70  k consists of up
127d4 20 74 6f 20 74 68 72 65 65 20 73 74 61 67 65 73   to three stages
127d5 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 69  ,.** performed i
127d6 6e 20 74 68 65 20 6f 72 64 65 72 20 73 70 65 63  n the order spec
127d7 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  ified:.**.**   *
127d8 20 50 61 67 65 73 20 61 72 65 20 70 6c 61 79 65   Pages are playe
127d9 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20  d back from the 
127da 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61  main journal sta
127db 72 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a  rting at byte.**
127dc 20 20 20 20 20 6f 66 66 73 65 74 20 50 61 67 65       offset Page
127dd 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73  rSavepoint.iOffs
127de 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e  et and continuin
127df 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67  g to .**     Pag
127e0 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72  erSavepoint.iHdr
127e1 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 68  Offset, or to th
127e2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69  e end of the mai
127e3 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  n journal.**    
127e4 20 66 69 6c 65 20 69 66 20 50 61 67 65 72 53 61   file if PagerSa
127e5 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73  vepoint.iHdrOffs
127e6 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  et is zero..**.*
127e7 2a 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 61  *   * If PagerSa
127e8 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73  vepoint.iHdrOffs
127e9 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  et is not zero, 
127ea 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 70  then pages are p
127eb 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 63  layed.**     bac
127ec 6b 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20  k starting from 
127ed 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
127ee 65 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66  er immediately f
127ef 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20  ollowing .**    
127f0 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
127f1 69 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74 68  iHdrOffset to th
127f2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69  e end of the mai
127f3 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  n journal file..
127f4 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20  **.**   * Pages 
127f5 61 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64 20  are then played 
127f6 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 75  back from the su
127f7 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  b-journal file, 
127f8 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20  starting.**     
127f9 77 69 74 68 20 74 68 65 20 50 61 67 65 72 53 61  with the PagerSa
127fa 76 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 20  vepoint.iSubRec 
127fb 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74  and continuing t
127fc 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20  o the end of.** 
127fd 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20      the journal 
127fe 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f  file..**.** Thro
127ff 75 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62  ughout the rollb
12800 61 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 63  ack process, eac
12801 68 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73  h time a page is
12802 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68   rolled back, th
12803 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69  e.** correspondi
12804 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e  ng bit is set in
12805 20 61 20 62 69 74 76 65 63 20 73 74 72 75 63 74   a bitvec struct
12806 75 72 65 20 28 76 61 72 69 61 62 6c 65 20 70 44  ure (variable pD
12807 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d  one in the.** im
12808 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c  plementation bel
12809 6f 77 29 2e 20 54 68 69 73 20 69 73 20 75 73 65  ow). This is use
1280a 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
1280b 20 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a   a page is only.
1280c 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74  ** rolled back t
1280d 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74  he first time it
1280e 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20   is encountered 
1280f 69 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e 61  in either journa
12810 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76  l..**.** If pSav
12811 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20  epoint is NULL, 
12812 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 6f  then pages are o
12813 6e 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20  nly played back 
12814 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  from the main.**
12815 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54   journal file. T
12816 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
12817 66 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e 20  for a bitvec in 
12818 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
12819 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c   In either case,
1281a 20 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b   before playback
1281b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50   commences the P
1281c 61 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69  ager.dbSize vari
1281d 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 74  able.** is reset
1281e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74 68   to the value th
1281f 61 74 20 69 74 20 68 65 6c 64 20 61 74 20 74 68  at it held at th
12820 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 73  e start of the s
12821 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72  avepoint .** (or
12822 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e   transaction). N
12823 6f 20 70 61 67 65 20 77 69 74 68 20 61 20 70 61  o page with a pa
12824 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 65  ge-number greate
12825 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75  r than this valu
12826 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 62  e.** is played b
12827 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 65  ack. If one is e
12828 6e 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73  ncountered it is
12829 20 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e   simply skipped.
1282a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1282b 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65  agerPlaybackSave
1282c 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61  point(Pager *pPa
1282d 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 6f  ger, PagerSavepo
1282e 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 29  int *pSavepoint)
1282f 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20  {.  i64 szJ;    
12830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12831 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f  Effective size o
12832 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
12833 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72  al */.  i64 iHdr
12834 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Off;            
12835 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 74   /* End of first
12836 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e   segment of main
12837 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73  -journal records
12838 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
12839 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f  QLITE_OK;      /
1283a 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
1283b 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65  .  Bitvec *pDone
1283c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42   = 0;       /* B
1283d 69 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20  itvec to ensure 
1283e 70 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63  pages played bac
1283f 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a  k only once */..
12840 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12841 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53  ->state>=PAGER_S
12842 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41  HARED );..  /* A
12843 6c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 63  llocate a bitvec
12844 20 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65   to use to store
12845 20 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 65   the set of page
12846 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f  s rolled back */
12847 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e  .  if( pSavepoin
12848 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d  t ){.    pDone =
12849 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
1284a 65 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74 2d  eate(pSavepoint-
1284b 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 28  >nOrig);.    if(
1284c 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20   !pDone ){.     
1284d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1284e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  OMEM;.    }.  }.
1284f 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 61  .  /* Set the da
12850 74 61 62 61 73 65 20 73 69 7a 65 20 62 61 63 6b  tabase size back
12851 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 74   to the value it
12852 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20   was before the 
12853 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20  savepoint .  ** 
12854 62 65 69 6e 67 20 72 65 76 65 72 74 65 64 20 77  being reverted w
12855 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a  as opened..  */.
12856 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
12857 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f 20   = pSavepoint ? 
12858 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69  pSavepoint->nOri
12859 67 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f 72  g : pPager->dbOr
1285a 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73  igSize;..  /* Us
1285b 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  e pPager->journa
1285c 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65  lOff as the effe
1285d 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68  ctive size of th
1285e 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a  e main rollback.
1285f 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54    ** journal.  T
12860 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d  he actual file m
12861 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74  ight be larger t
12862 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a  han this in.  **
12863 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
12864 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50  DE_TRUNCATE or P
12865 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12866 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61  _PERSIST.  But a
12867 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73  nything.  ** pas
12868 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  t pPager->journa
12869 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69  lOff is off-limi
1286a 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20  ts to us..  */. 
1286b 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a   szJ = pPager->j
1286c 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a  ournalOff;..  /*
1286d 20 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e   Begin by rollin
1286e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66  g back records f
1286f 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  rom the main jou
12870 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74  rnal starting at
12871 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70  .  ** PagerSavep
12872 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64  oint.iOffset and
12873 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74   continuing to t
12874 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20  he next journal 
12875 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65  header..  ** The
12876 72 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f  re might be reco
12877 72 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  rds in the main 
12878 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76  journal that hav
12879 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  e a page number.
1287a 20 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61    ** greater tha
1287b 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  n the current da
1287c 74 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61  tabase size (pPa
1287d 67 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74  ger->dbSize) but
1287e 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c   those.  ** will
1287f 20 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f   be skipped auto
12880 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65  matically.  Page
12881 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70  s are added to p
12882 44 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a  Done as they.  *
12883 2a 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63  * are played bac
12884 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  k..  */.  if( pS
12885 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
12886 69 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65 70  iHdrOff = pSavep
12887 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74  oint->iHdrOffset
12888 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69   ? pSavepoint->i
12889 48 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b  HdrOffset : szJ;
1288a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
1288b 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 70  rnalOff = pSavep
1288c 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20  oint->iOffset;. 
1288d 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51     while( rc==SQ
1288e 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
1288f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48  r->journalOff<iH
12890 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 72  drOff ){.      r
12891 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
12892 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67  ck_one_page(pPag
12893 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65  er, 1, 0, &pPage
12894 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31  r->journalOff, 1
12895 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a  , pDone);.    }.
12896 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
12897 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20  SQLITE_DONE );. 
12898 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
12899 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
1289a 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f   0;.  }..  /* Co
1289b 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62  ntinue rolling b
1289c 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 20  ack records out 
1289d 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
1289e 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a  nal starting at.
1289f 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6a    ** the first j
128a0 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65  ournal header se
128a1 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e  en and continuin
128a2 67 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66 65  g until the effe
128a3 63 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f  ctive end.  ** o
128a4 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
128a5 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e  al file.  Contin
128a6 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f  ue to skip out-o
128a7 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 6e  f-range pages an
128a8 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20  d.  ** continue 
128a9 61 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f 6c  adding pages rol
128aa 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e  led back to pDon
128ab 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  e..  */.  while(
128ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
128ad 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
128ae 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20  lOff<szJ ){.    
128af 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20  u32 ii;         
128b0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
128b1 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a  er */.    u32 nJ
128b2 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  Rec = 0;     /* 
128b3 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61  Number of Journa
128b4 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20  l Records */.   
128b5 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20   u32 dummy;.    
128b6 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c  rc = readJournal
128b7 48 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20 73  Hdr(pPager, 0, s
128b8 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d  zJ, &nJRec, &dum
128b9 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  my);.    assert(
128ba 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
128bb 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   );..    /*.    
128bc 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e  ** The "pPager->
128bd 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e  journalHdr+JOURN
128be 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
128bf 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  )==pPager->journ
128c0 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65  alOff".    ** te
128c1 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f  st is related to
128c2 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20   ticket #2565.  
128c3 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69  See the discussi
128c4 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  on in the.    **
128c5 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
128c6 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61  ) function for a
128c7 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
128c8 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
128c9 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a    if( nJRec==0 .
128ca 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e       && pPager->
128cb 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e  journalHdr+JOURN
128cc 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
128cd 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  )==pPager->journ
128ce 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 20  alOff.    ){.   
128cf 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 29     nJRec = (u32)
128d0 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e  ((szJ - pPager->
128d1 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52  journalOff)/JOUR
128d2 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72  NAL_PG_SZ(pPager
128d3 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  ));.    }.    fo
128d4 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  r(ii=0; rc==SQLI
128d5 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65  TE_OK && ii<nJRe
128d6 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  c && pPager->jou
128d7 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b  rnalOff<szJ; ii+
128d8 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  +){.      rc = p
128d9 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e  ager_playback_on
128da 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31  e_page(pPager, 1
128db 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f  , 0, &pPager->jo
128dc 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f  urnalOff, 1, pDo
128dd 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ne);.    }.    a
128de 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
128df 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20  E_DONE );.  }.  
128e0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
128e1 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d  TE_OK || pPager-
128e2 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a  >journalOff==szJ
128e3 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c   );..  /* Finall
128e4 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67  y,  rollback pag
128e5 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d  es from the sub-
128e6 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74  journal.  Page t
128e7 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72  hat were.  ** pr
128e8 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20  eviously rolled 
128e9 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  back out of the 
128ea 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e  main journal (an
128eb 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70  d are hence in p
128ec 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20  Done).  ** will 
128ed 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74  be skipped.  Out
128ee 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20  -of-range pages 
128ef 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64  are also skipped
128f0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61  ..  */.  if( pSa
128f1 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75  vepoint ){.    u
128f2 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  32 ii;          
128f3 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
128f4 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66  r */.    i64 off
128f5 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74  set = pSavepoint
128f6 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61  ->iSubRec*(4+pPa
128f7 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
128f8 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76 65      for(ii=pSave
128f9 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20  point->iSubRec; 
128fa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
128fb 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62   ii<pPager->nSub
128fc 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Rec; ii++){.    
128fd 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74    assert( offset
128fe 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e  ==ii*(4+pPager->
128ff 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20  pageSize) );.   
12900 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
12901 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
12902 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f  pPager, 0, 0, &o
12903 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29  ffset, 1, pDone)
12904 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
12905 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
12906 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  ONE );.  }..  sq
12907 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
12908 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28  oy(pDone);.  if(
12909 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1290a 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
1290b 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a  urnalOff = szJ;.
1290c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1290d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1290e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
1290f 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79  ber of in-memory
12910 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20   pages that are 
12911 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49  allowed..*/.SQLI
12912 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12913 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
12914 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a  achesize(Pager *
12915 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61  pPager, int mxPa
12916 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63  ge){.  sqlite3Pc
12917 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65  acheSetCachesize
12918 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12919 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a  , mxPage);.}../*
1291a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72  .** Adjust the r
1291b 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65  obustness of the
1291c 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d   database to dam
1291d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72  age due to OS cr
1291e 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65  ashes.** or powe
1291f 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 68  r failures by ch
12920 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65  anging the numbe
12921 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68  r of syncs()s wh
12922 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68  en writing.** th
12923 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
12924 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 74  al.  There are t
12925 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a  hree levels:.**.
12926 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 20  **    OFF       
12927 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20  sqlite3OsSync() 
12928 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e  is never called.
12929 20 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65    This is the de
1292a 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20  fault.**        
1292b 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72        for tempor
1292c 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e  ary and transien
1292d 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  t files..**.**  
1292e 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20    NORMAL    The 
1292f 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65  journal is synce
12930 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72  d once before wr
12931 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68  ites begin on th
12932 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
12933 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69    database.  Thi
12934 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64  s is normally ad
12935 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f  equate protectio
12936 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20  n, but.**       
12937 20 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65         it is the
12938 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69  oretically possi
12939 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79  ble, though very
1293a 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20   unlikely,.**   
1293b 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20             that 
1293c 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f  an inopertune po
1293d 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c  wer failure coul
1293e 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72  d leave the jour
1293f 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nal.**          
12940 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 77      in a state w
12941 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65  hich would cause
12942 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64   damage to the d
12943 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20  atabase.**      
12944 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 20          when it 
12945 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a  is rolled back..
12946 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20  **.**    FULL   
12947 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69     The journal i
12948 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 62  s synced twice b
12949 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67  efore writes beg
1294a 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20  in on the.**    
1294b 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61            databa
1294c 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64  se (with some ad
1294d 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1294e 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20  tion - the nRec 
1294f 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20  field.**        
12950 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75        of the jou
12951 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65  rnal header - be
12952 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62  ing written in b
12953 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a  etween the two.*
12954 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  *              s
12955 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 73  yncs).  If we as
12956 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e  sume that writin
12957 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  g a.**          
12958 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20      single disk 
12959 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63  sector is atomic
1295a 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65  , then this mode
1295b 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20   provides.**    
1295c 20 20 20 20 20 20 20 20 20 20 61 73 73 75 72 61            assura
1295d 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75  nce that the jou
1295e 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rnal will not be
1295f 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68   corrupted to th
12960 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
12961 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69    point of causi
12962 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65  ng damage to the
12963 20 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67   database during
12964 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
12965 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20   Numeric values 
12966 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
12967 74 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65  these states are
12968 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d   OFF==1, NORMAL=
12969 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33  2,.** and FULL=3
1296a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1296b 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1296c 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52  RAGMAS.SQLITE_PR
1296d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1296e 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79  e3PagerSetSafety
1296f 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61  Level(Pager *pPa
12970 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20  ger, int level, 
12971 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b  int bFullFsync){
12972 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  .  pPager->noSyn
12973 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c  c =  (level==1 |
12974 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  | pPager->tempFi
12975 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67  le) ?1:0;.  pPag
12976 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28  er->fullSync = (
12977 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61  level==3 && !pPa
12978 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f  ger->tempFile) ?
12979 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73  1:0;.  pPager->s
1297a 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75  ync_flags = (bFu
1297b 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53  llFsync?SQLITE_S
1297c 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f  YNC_FULL:SQLITE_
1297d 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20  SYNC_NORMAL);.  
1297e 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  if( pPager->noSy
1297f 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65  nc ) pPager->nee
12980 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e  dSync = 0;.}.#en
12981 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
12982 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
12983 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72  variable is incr
12984 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
12985 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20   the library.** 
12986 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e  attempts to open
12987 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
12988 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  e.  This informa
12989 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72  tion is used for
1298a 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20  .** testing and 
1298b 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20  analysis only.  
1298c 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1298d 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
1298e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70  I int sqlite3_op
1298f 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30  entemp_count = 0
12990 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
12991 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  Open a temporary
12992 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69   file..**.** Wri
12993 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  te the file desc
12994 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69  riptor into *pFi
12995 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  le. Return SQLIT
12996 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
12997 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  .** or some othe
12998 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  r error code if 
12999 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53 20  we fail. The OS 
1299a 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
1299b 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68  ly .** delete th
1299c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  e temporary file
1299d 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73   when it is clos
1299e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c  ed..**.** The fl
1299f 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 74 68  ags passed to th
129a0 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70 65  e VFS layer xOpe
129a1 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68 6f  n() call are tho
129a2 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  se specified.** 
129a3 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 66 73  by parameter vfs
129a4 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68 20  Flags ORed with 
129a5 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
129a6 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  *.**     SQLITE_
129a7 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a  OPEN_READWRITE.*
129a8 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45  *     SQLITE_OPE
129a9 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 20  N_CREATE.**     
129aa 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
129ab 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c  USIVE.**     SQL
129ac 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
129ad 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63  NCLOSE.*/.static
129ae 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 65   int pagerOpente
129af 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  mp(.  Pager *pPa
129b0 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  ger,        /* T
129b1 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20  he pager object 
129b2 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
129b3 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72  e *pFile,  /* Wr
129b4 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ite the file des
129b5 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a  criptor here */.
129b6 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20    int vfsFlags  
129b7 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
129b8 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20   passed through 
129b9 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b  to the VFS */.){
129ba 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
129bb 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
129bc 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64  rn code */..#ifd
129bd 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
129be 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d   sqlite3_opentem
129bf 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55  p_count++;  /* U
129c0 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
129c1 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  and analysis onl
129c2 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76  y */.#endif..  v
129c3 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49  fsFlags |=  SQLI
129c4 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
129c5 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  E | SQLITE_OPEN_
129c6 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20  CREATE |.       
129c7 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
129c8 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c  _EXCLUSIVE | SQL
129c9 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
129ca 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73  NCLOSE;.  rc = s
129cb 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61  qlite3OsOpen(pPa
129cc 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46  ger->pVfs, 0, pF
129cd 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30  ile, vfsFlags, 0
129ce 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
129cf 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73  =SQLITE_OK || is
129d0 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20  Open(pFile) );. 
129d1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
129d2 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73  *.** Set the bus
129d3 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y handler functi
129d4 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  on..**.** The pa
129d5 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ger invokes the 
129d6 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66 20  busy-handler if 
129d7 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20  sqlite3OsLock() 
129d8 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49  returns .** SQLI
129d9 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72 79  TE_BUSY when try
129da 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66  ing to upgrade f
129db 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61  rom no-lock to a
129dc 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a   SHARED lock,.**
129dd 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67 20   or when trying 
129de 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20  to upgrade from 
129df 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
129e0 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  to an EXCLUSIVE 
129e1 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65  .** lock. It doe
129e2 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74  s *not* invoke t
129e3 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
129e4 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66  when upgrading f
129e5 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f  rom.** SHARED to
129e6 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77 68   RESERVED, or wh
129e7 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f  en upgrading fro
129e8 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 43 4c  m SHARED to EXCL
129e9 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68 20  USIVE.** (which 
129ea 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 68 6f  occurs during ho
129eb 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
129ec 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a  ck). Summary:.**
129ed 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f 6e  .**   Transition
129ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129ef 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65          | Invoke
129f0 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a  s xBusyHandler.*
129f1 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
129f2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129f3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129f4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
129f5 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20    NO_LOCK       
129f6 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20  -> SHARED_LOCK  
129f7 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53      | Yes.**   S
129f8 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20  HARED_LOCK   -> 
129f9 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20  RESERVED_LOCK   
129fa 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45   | No.**   SHARE
129fb 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c  D_LOCK   -> EXCL
129fc 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e  USIVE_LOCK   | N
129fd 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 5f  o.**   RESERVED_
129fe 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56  LOCK -> EXCLUSIV
129ff 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a  E_LOCK   | Yes.*
12a00 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79  *.** If the busy
12a01 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63  -handler callbac
12a02 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  k returns non-ze
12a03 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ro, the lock is 
12a04 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66 20  .** retried. If 
12a05 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c  it returns zero,
12a06 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45   then the SQLITE
12a07 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a 2a  _BUSY error is.*
12a08 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  * returned to th
12a09 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65 20  e caller of the 
12a0a 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69  pager API functi
12a0b 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
12a0c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
12a0d 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
12a0e 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a  ndler(.  Pager *
12a0f 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20  pPager,         
12a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12a11 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f   Pager object */
12a12 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61  .  int (*xBusyHa
12a13 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c 20  ndler)(void *), 
12a14 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
12a15 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c  er to busy-handl
12a16 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  er function */. 
12a17 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64   void *pBusyHand
12a18 6c 65 72 41 72 67 20 20 20 20 20 20 20 20 20 20  lerArg          
12a19 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
12a1a 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42 75  t to pass to xBu
12a1b 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20  syHandler */.){ 
12a1c 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73   .  pPager->xBus
12a1d 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 79  yHandler = xBusy
12a1e 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 65  Handler;.  pPage
12a1f 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41  r->pBusyHandlerA
12a20 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c 65  rg = pBusyHandle
12a21 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rArg;.}../*.** R
12a22 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e  eport the curren
12a23 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20  t page size and 
12a24 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76  number of reserv
12a25 65 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a  ed bytes back.**
12a26 20 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a   to the codec..*
12a27 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
12a28 48 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69 63  HAS_CODEC.static
12a29 20 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f 72   void pagerRepor
12a2a 74 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61  tSize(Pager *pPa
12a2b 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67  ger){.  if( pPag
12a2c 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68  er->xCodecSizeCh
12a2d 6e 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  ng ){.    pPager
12a2e 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67  ->xCodecSizeChng
12a2f 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c  (pPager->pCodec,
12a30 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
12a31 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
12a32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
12a33 6e 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73 65  nt)pPager->nRese
12a34 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  rve);.  }.}.#els
12a35 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72  e.# define pager
12a36 52 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20 20  ReportSize(X)   
12a37 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65    /* No-op if we
12a38 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
12a39 61 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69  a codec */.#endi
12a3a 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  f../*.** Change 
12a3b 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 75 73  the page size us
12a3c 65 64 20 62 79 20 74 68 65 20 50 61 67 65 72 20  ed by the Pager 
12a3d 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77 20  object. The new 
12a3e 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73  page size .** is
12a3f 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61 67   passed in *pPag
12a40 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  eSize..**.** If 
12a41 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
12a42 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
12a43 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
12a44 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74  on is called, it
12a45 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  .** is a no-op. 
12a46 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
12a47 65 64 20 69 73 20 74 68 65 20 65 72 72 6f 72 20  ed is the error 
12a48 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65  state error code
12a49 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f   (i.e. .** one o
12a4a 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20  f SQLITE_IOERR, 
12a4b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f  SQLITE_CORRUPT o
12a4c 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a  r SQLITE_FULL)..
12a4d 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
12a4e 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66   if all of the f
12a4f 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75  ollowing are tru
12a50 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65  e:.**.**   * the
12a51 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 28   new page size (
12a52 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65 53  value of *pPageS
12a53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28 61  ize) is valid (a
12a54 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f   power .**     o
12a55 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31  f two between 51
12a56 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58  2 and SQLITE_MAX
12a57 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c  _PAGE_SIZE, incl
12a58 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a  usive), and.**.*
12a59 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 20  *   * there are 
12a5a 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  no outstanding p
12a5b 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20  age references, 
12a5c 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68  and.**.**   * th
12a5d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65 69  e database is ei
12a5e 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d  ther not an in-m
12a5f 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6f  emory database o
12a60 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 61  r it is.**     a
12a61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
12a62 62 61 73 65 20 74 68 61 74 20 63 75 72 72 65 6e  base that curren
12a63 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  tly consists of 
12a64 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a  zero pages..**.*
12a65 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 72  * then the pager
12a66 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69 7a   object page siz
12a67 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50 61  e is set to *pPa
12a68 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  geSize..**.** If
12a69 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69   the page size i
12a6a 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20  s changed, then 
12a6b 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73  this function us
12a6c 65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  es sqlite3PagerM
12a6d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f  alloc() .** to o
12a6e 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67 65  btain a new Page
12a6f 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66  r.pTmpSpace buff
12a70 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c 6f  er. If this allo
12a71 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a  cation attempt .
12a72 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45  ** fails, SQLITE
12a73 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
12a74 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20  ed and the page 
12a75 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e 63  size remains unc
12a76 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61  hanged. .** In a
12a77 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c 20  ll other cases, 
12a78 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
12a79 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
12a7a 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  the page size is
12a7b 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65 69   not changed, ei
12a7c 74 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e 65  ther because one
12a7d 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61 74   of the enumerat
12a7e 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73  ed.** conditions
12a7f 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74 72   above is not tr
12a80 75 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 61  ue, the pager wa
12a81 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65  s in error state
12a82 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75   when this.** fu
12a83 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65  nction was calle
12a84 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74 68  d, or because th
12a85 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
12a86 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69 6c  ion attempt fail
12a87 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50  ed, .** then *pP
12a88 61 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 74  ageSize is set t
12a89 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 69  o the old, retai
12a8a 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 65  ned page size be
12a8b 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
12a8c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12a8d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
12a8e 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61  erSetPagesize(Pa
12a8f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31 36  ger *pPager, u16
12a90 20 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e 74   *pPageSize, int
12a91 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69 6e   nReserve){.  in
12a92 74 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65  t rc = pPager->e
12a93 72 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72  rrCode;..  if( r
12a94 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12a95 20 20 20 20 75 31 36 20 70 61 67 65 53 69 7a 65      u16 pageSize
12a96 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20   = *pPageSize;. 
12a97 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 53     assert( pageS
12a98 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53  ize==0 || (pageS
12a99 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65  ize>=512 && page
12a9a 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58  Size<=SQLITE_MAX
12a9b 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20  _PAGE_SIZE) );. 
12a9c 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e     if( (pPager->
12a9d 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67  memDb==0 || pPag
12a9e 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20  er->dbSize==0). 
12a9f 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 63      && sqlite3Pc
12aa0 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61  acheRefCount(pPa
12aa1 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30  ger->pPCache)==0
12aa2 20 0a 20 20 20 20 20 26 26 20 70 61 67 65 53 69   .     && pageSi
12aa3 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d  ze && pageSize!=
12aa4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12aa5 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63   .    ){.      c
12aa6 68 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68 61  har *pNew = (cha
12aa7 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 4d  r *)sqlite3PageM
12aa8 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29 3b  alloc(pageSize);
12aa9 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
12aaa 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
12aab 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
12aac 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12aad 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28      pager_reset(
12aae 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20  pPager);.       
12aaf 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
12ab0 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20  e = pageSize;.  
12ab1 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
12ab2 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54  eFree(pPager->pT
12ab3 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20  mpSpace);.      
12ab4 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70    pPager->pTmpSp
12ab5 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ace = pNew;.    
12ab6 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
12ab7 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 61  eSetPageSize(pPa
12ab8 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61  ger->pPCache, pa
12ab9 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  geSize);.      }
12aba 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 67  .    }.    *pPag
12abb 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50 61  eSize = (u16)pPa
12abc 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20  ger->pageSize;. 
12abd 20 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c     if( nReserve<
12abe 30 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20 70  0 ) nReserve = p
12abf 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b  Pager->nReserve;
12ac0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 52 65  .    assert( nRe
12ac1 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73  serve>=0 && nRes
12ac2 65 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20 20  erve<1000 );.   
12ac3 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76   pPager->nReserv
12ac4 65 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72 76  e = (i16)nReserv
12ac5 65 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 6f  e;.    pagerRepo
12ac6 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  rtSize(pPager);.
12ac7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
12ac8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
12ac9 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
12aca 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61 67  e "temporary pag
12acb 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 20 69  e" buffer held i
12acc 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20  nternally.** by 
12acd 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73  the pager.  This
12ace 20 69 73 20 61 20 62 75 66 66 65 72 20 74 68 61   is a buffer tha
12acf 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20  t is big enough 
12ad0 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65  to hold the.** e
12ad1 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66  ntire content of
12ad2 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
12ad3 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 20 69  .  This buffer i
12ad4 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  s used internall
12ad5 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c  y.** during roll
12ad6 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65  back and will be
12ad7 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65   overwritten whe
12ad8 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b  never a rollback
12ad9 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75 74  .** occurs.  But
12ada 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 61   other modules a
12adb 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 20 69  re free to use i
12adc 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61  t too, as long a
12add 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b  s.** no rollback
12ade 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67 2e  s are happening.
12adf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
12ae0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
12ae1 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50  PagerTempSpace(P
12ae2 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
12ae3 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
12ae4 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a  pTmpSpace;.}../*
12ae5 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73  .** Attempt to s
12ae6 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64  et the maximum d
12ae7 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75  atabase page cou
12ae8 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 73 20  nt if mxPage is 
12ae9 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61  positive. .** Ma
12aea 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66  ke no changes if
12aeb 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f 20   mxPage is zero 
12aec 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41 6e  or negative.  An
12aed 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 20 74  d never reduce t
12aee 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61  he.** maximum pa
12aef 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74  ge count below t
12af0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
12af1 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
12af2 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73  .**.** Regardles
12af3 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74  s of mxPage, ret
12af4 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  urn the current 
12af5 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75  maximum page cou
12af6 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
12af7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12af8 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75  3PagerMaxPageCou
12af9 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
12afa 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
12afb 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b   if( mxPage>0 ){
12afc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50  .    pPager->mxP
12afd 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20  gno = mxPage;.  
12afe 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  }.  sqlite3Pager
12aff 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
12b00 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  , 0);.  return p
12b01 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d  Pager->mxPgno;.}
12b02 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
12b03 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75  owing set of rou
12b04 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74  tines are used t
12b05 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69  o disable the si
12b06 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65  mulated.** I/O e
12b07 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20  rror mechanism. 
12b08 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
12b09 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f 69  are used to avoi
12b0a 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65  d simulated.** e
12b0b 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 20  rrors in places 
12b0c 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20  where we do not 
12b0d 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f 72  care about error
12b0e 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  s..**.** Unless 
12b0f 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 20  -DSQLITE_TEST=1 
12b10 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 72  is used, these r
12b11 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c 20  outines are all 
12b12 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65  no-ops.** and ge
12b13 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a  nerate no code..
12b14 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
12b15 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
12b16 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69   extern int sqli
12b17 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
12b18 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ding;.SQLITE_API
12b19 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69   extern int sqli
12b1a 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
12b1b 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76  ;.static int sav
12b1c 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73  ed_cnt;.void dis
12b1d 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
12b1e 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a  o_errors(void){.
12b1f 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71    saved_cnt = sq
12b20 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
12b21 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65  ending;.  sqlite
12b22 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
12b23 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20  ng = -1;.}.void 
12b24 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  enable_simulated
12b25 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29  _io_errors(void)
12b26 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  {.  sqlite3_io_e
12b27 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73  rror_pending = s
12b28 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73  aved_cnt;.}.#els
12b29 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62  e.# define disab
12b2a 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
12b2b 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e  errors().# defin
12b2c 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74  e enable_simulat
12b2d 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23  ed_io_errors().#
12b2e 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  endif../*.** Rea
12b2f 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79  d the first N by
12b30 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67  tes from the beg
12b31 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69  inning of the fi
12b32 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a  le into memory.*
12b33 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69  * that pDest poi
12b34 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49  nts to. .**.** I
12b35 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20  f the pager was 
12b36 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e  opened on a tran
12b37 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c  sient file (zFil
12b38 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a  ename==""), or.*
12b39 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 69  * opened on a fi
12b3a 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62  le less than N b
12b3b 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68  ytes in size, th
12b3c 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  e output buffer 
12b3d 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64  is.** zeroed and
12b3e 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
12b3f 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e 61  ned. The rationa
12b40 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 74  le for this is t
12b41 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e  hat this .** fun
12b42 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
12b43 20 72 65 61 64 20 64 61 74 61 62 61 73 65 20 68   read database h
12b44 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65  eaders, and a ne
12b45 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a  w transient or.*
12b46 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74  * zero sized dat
12b47 61 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64  abase has a head
12b48 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73  er than consists
12b49 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72   entirely of zer
12b4a 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  oes..**.** If an
12b4b 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 74  y IO error apart
12b4c 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45   from SQLITE_IOE
12b4d 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 73  RR_SHORT_READ is
12b4e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a   encountered,.**
12b4f 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
12b50 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
12b51 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 68  he caller and th
12b52 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
12b53 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66  e.** output buff
12b54 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  er undefined..*/
12b55 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12b56 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
12b57 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50  ReadFileheader(P
12b58 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
12b59 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68  t N, unsigned ch
12b5a 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e  ar *pDest){.  in
12b5b 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
12b5c 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74  ;.  memset(pDest
12b5d 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72  , 0, N);.  asser
12b5e 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
12b5f 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  ->fd) || pPager-
12b60 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69  >tempFile );.  i
12b61 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
12b62 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f 54  ->fd) ){.    IOT
12b63 52 41 43 45 28 28 22 44 42 48 44 52 20 25 70 20  RACE(("DBHDR %p 
12b64 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  0 %d\n", pPager,
12b65 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71   N)).    rc = sq
12b66 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
12b67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e  er->fd, pDest, N
12b68 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
12b69 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  ==SQLITE_IOERR_S
12b6a 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20  HORT_READ ){.   
12b6b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
12b6c 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
12b6d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
12b6e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  ** Return the to
12b6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
12b70 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
12b71 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61  ase file associa
12b72 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50 61  ted .** with pPa
12b73 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74  ger. Normally, t
12b74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74 65  his is calculate
12b75 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20 73  d as (<db file s
12b76 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e  ize>/<page-size>
12b77 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69  )..** However, i
12b78 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 65  f the file is be
12b79 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61 67  tween 1 and <pag
12b7a 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69 6e  e-size> bytes in
12b7b 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20   size, then .** 
12b7c 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65 72  this is consider
12b7d 65 64 20 61 20 31 20 70 61 67 65 20 66 69 6c 65  ed a 1 page file
12b7e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
12b7f 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72  ager is in error
12b80 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
12b81 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
12b82 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  led, then the.**
12b83 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72   error state err
12b84 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12b85 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65 20  ned and *pnPage 
12b86 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 20  left unchanged. 
12b87 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66 69  Or,.** if the fi
12b88 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20 74 6f  le system has to
12b89 20 62 65 20 71 75 65 72 69 65 64 20 66 6f 72 20   be queried for 
12b8a 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
12b8b 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20  file and.** the 
12b8c 71 75 65 72 79 20 61 74 74 65 6d 70 74 20 72 65  query attempt re
12b8d 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f  turns an IO erro
12b8e 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  r, the IO error 
12b8f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
12b90 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20  .** and *pnPage 
12b91 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65  is left unchange
12b92 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  d..**.** Otherwi
12b93 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e  se, if everythin
12b94 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
12b95 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20   then SQLITE_OK 
12b96 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61  is returned.** a
12b97 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73 65  nd *pnPage is se
12b98 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
12b99 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
12b9a 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c  database..*/.SQL
12b9b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12b9c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
12b9d 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61  count(Pager *pPa
12b9e 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65  ger, int *pnPage
12b9f 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b  ){.  Pgno nPage;
12ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12ba1 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
12ba2 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f  n via *pnPage */
12ba3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
12ba4 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 69  ger is already i
12ba5 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
12ba6 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  e, return the er
12ba7 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69  ror code. */.  i
12ba8 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  f( pPager->errCo
12ba9 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  de ){.    return
12baa 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
12bab 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65  ;.  }..  /* Dete
12bac 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72  rmine the number
12bad 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
12bae 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68 69   file. Store thi
12baf 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20  s in nPage. */. 
12bb0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
12bb1 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  izeValid ){.    
12bb2 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e  nPage = pPager->
12bb3 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b  dbSize;.  }else{
12bb4 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20  .    int rc;    
12bb5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12bb6 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20 62  Error returned b
12bb7 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a  y OsFileSize() *
12bb8 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30 3b  /.    i64 n = 0;
12bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12bba 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62 79   File size in by
12bbb 74 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  tes returned by 
12bbc 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a  OsFileSize() */.
12bbd 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f  .    assert( isO
12bbe 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12bbf 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
12bc0 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 69  ile );.    if( i
12bc1 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
12bc2 29 20 26 26 20 28 30 20 21 3d 20 28 72 63 20 3d  ) && (0 != (rc =
12bc3 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
12bc4 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26  ze(pPager->fd, &
12bc5 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70 61  n))) ){.      pa
12bc6 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
12bc7 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 74  , rc);.      ret
12bc8 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
12bc9 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70    if( n>0 && n<p
12bca 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
12bcb 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  ){.      nPage =
12bcc 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
12bcd 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50 67       nPage = (Pg
12bce 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e  no)(n / pPager->
12bcf 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d  pageSize);.    }
12bd0 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
12bd1 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e  >state!=PAGER_UN
12bd2 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50  LOCK ){.      pP
12bd3 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e  ager->dbSize = n
12bd4 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67  Page;.      pPag
12bd5 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d  er->dbFileSize =
12bd6 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50   nPage;.      pP
12bd7 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
12bd8 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
12bd9 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75  ..  /* If the cu
12bda 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
12bdb 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c  pages in the fil
12bdc 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
12bdd 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66  n the .  ** conf
12bde 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70  igured maximum p
12bdf 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63  ager number, inc
12be0 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65  rease the allowe
12be1 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20  d limit so.  ** 
12be2 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 63 61  that the file ca
12be3 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a  n be read..  */.
12be4 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67    if( nPage>pPag
12be5 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20  er->mxPgno ){.  
12be6 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f    pPager->mxPgno
12be7 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a   = (Pgno)nPage;.
12be8 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68    }..  /* Set th
12be9 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  e output variabl
12bea 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  e and return SQL
12beb 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20  ITE_OK */.  if( 
12bec 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70  pnPage ){.    *p
12bed 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20  nPage = nPage;. 
12bee 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
12bef 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
12bf0 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61   Try to obtain a
12bf1 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f   lock of type lo
12bf2 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 61  cktype on the da
12bf3 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0a  tabase file. If.
12bf4 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 20  ** a similar or 
12bf5 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20  greater lock is 
12bf6 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68  already held, th
12bf7 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
12bf8 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72   no-op.** (retur
12bf9 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ning SQLITE_OK i
12bfa 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a  mmediately)..**.
12bfb 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 74  ** Otherwise, at
12bfc 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20  tempt to obtain 
12bfd 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73  the lock using s
12bfe 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20  qlite3OsLock(). 
12bff 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62  Invoke .** the b
12c00 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  usy callback if 
12c01 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 72  the lock is curr
12c02 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61  ently not availa
12c03 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20  ble. Repeat .** 
12c04 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63  until the busy c
12c05 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
12c06 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74  false or until t
12c07 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a  he attempt to .*
12c08 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63  * obtain the loc
12c09 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a  k succeeds..**.*
12c0a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
12c0b 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  OK on success an
12c0c 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
12c0d 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74  if we cannot obt
12c0e 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e  ain.** the lock.
12c0f 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20   If the lock is 
12c10 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 73  obtained success
12c11 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 50  fully, set the P
12c12 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76  ager.state .** v
12c13 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74  ariable to lockt
12c14 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  ype before retur
12c15 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
12c16 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f  int pager_wait_o
12c17 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  n_lock(Pager *pP
12c18 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79  ager, int lockty
12c19 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  pe){.  int rc;  
12c1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
12c1c 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
12c1d 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20   /* The OS lock 
12c1e 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74  values must be t
12c1f 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50  he same as the P
12c20 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73  ager lock values
12c21 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41   */.  assert( PA
12c22 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52  GER_SHARED==SHAR
12c23 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
12c24 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52  ert( PAGER_RESER
12c25 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  VED==RESERVED_LO
12c26 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
12c27 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d  PAGER_EXCLUSIVE=
12c28 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
12c29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
12c2a 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c  file is currentl
12c2b 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20  y unlocked then 
12c2c 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65  the size must be
12c2d 20 75 6e 6b 6e 6f 77 6e 2e 20 49 74 0a 20 20 2a   unknown. It.  *
12c2e 2a 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20  * must not have 
12c2f 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 74  been modified at
12c30 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a   this point..  *
12c31 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
12c32 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
12c33 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65  _SHARED || pPage
12c34 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d  r->dbSizeValid==
12c35 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
12c36 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
12c37 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50  GER_SHARED || pP
12c38 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
12c39 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  ==0 );..  /* Che
12c3a 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20  ck that this is 
12c3b 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28  either a no-op (
12c3c 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75  because the requ
12c3d 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20  ested lock is . 
12c3e 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64   ** already held
12c3f 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  , or one of the 
12c40 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 61  transistions tha
12c41 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  t the busy-handl
12c42 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69  er.  ** may be i
12c43 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 61  nvoked during, a
12c44 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
12c45 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20  comment above.  
12c46 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  ** sqlite3PagerS
12c47 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 2e  etBusyhandler().
12c48 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
12c49 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d  (pPager->state>=
12c4a 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 20  locktype).      
12c4b 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61   || (pPager->sta
12c4c 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te==PAGER_UNLOCK
12c4d 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41   && locktype==PA
12c4e 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20  GER_SHARED).    
12c4f 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73     || (pPager->s
12c50 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
12c51 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 65  RVED && locktype
12c52 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ==PAGER_EXCLUSIV
12c53 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 70  E).  );..  if( p
12c54 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f  Pager->state>=lo
12c55 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63  cktype ){.    rc
12c56 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
12c57 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a  }else{.    do {.
12c58 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12c59 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
12c5a 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a  >fd, locktype);.
12c5b 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d      }while( rc==
12c5c 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70  SQLITE_BUSY && p
12c5d 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
12c5e 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73  ler(pPager->pBus
12c5f 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a  yHandlerArg) );.
12c60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12c61 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
12c62 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 28  Pager->state = (
12c63 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20  u8)locktype;.   
12c64 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43     IOTRACE(("LOC
12c65 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  K %p %d\n", pPag
12c66 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20  er, locktype)). 
12c67 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
12c68 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n rc;.}../*.** F
12c69 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54 72  unction assertTr
12c6a 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  uncateConstraint
12c6b 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73 20  (pPager) checks 
12c6c 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  that one of the 
12c6d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  .** following is
12c6e 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64 69   true for all di
12c6f 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e  rty pages curren
12c70 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65 2d  tly in the page-
12c71 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  cache:.**.**   a
12c72 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  ) The page numbe
12c73 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  r is less than o
12c74 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 73  r equal to the s
12c75 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 20  ize of the .**  
12c76 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 74 61      current data
12c77 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e 20 70  base image, in p
12c78 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20  ages, OR.**.**  
12c79 20 62 29 20 69 66 20 74 68 65 20 70 61 67 65 20   b) if the page 
12c7a 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 72 69  content were wri
12c7b 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 69 6d  tten at this tim
12c7c 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a  e, it would not.
12c7d 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 65 73  **      be neces
12c7e 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 68  sary to write th
12c7f 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e  e current conten
12c80 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 75 62  t out to the sub
12c81 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20  -journal.**     
12c82 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20   (as determined 
12c83 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 62 6a  by function subj
12c84 52 65 71 75 69 72 65 73 50 61 67 65 28 29 29 2e  RequiresPage()).
12c85 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f  .**.** If the co
12c86 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 65 64  ndition asserted
12c87 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
12c88 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c  n were not true,
12c89 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 72 74   and the.** dirt
12c8a 79 20 70 61 67 65 20 77 65 72 65 20 74 6f 20 62  y page were to b
12c8b 65 20 64 69 73 63 61 72 64 65 64 20 66 72 6f 6d  e discarded from
12c8c 20 74 68 65 20 63 61 63 68 65 20 76 69 61 20 74   the cache via t
12c8d 68 65 20 70 61 67 65 72 53 74 72 65 73 73 28 29  he pagerStress()
12c8e 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 61 67  .** routine, pag
12c8f 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 6c 64  erStress() would
12c90 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 63   not write the c
12c91 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e 74  urrent page cont
12c92 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 61  ent to.** the da
12c93 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20  tabase file. If 
12c94 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 61 6e  a savepoint tran
12c95 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 6f 6c  saction were rol
12c96 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 0a 2a  led back after.*
12c97 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 64 2c  * this happened,
12c98 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 68   the correct beh
12c99 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 65 20  aviour would be 
12c9a 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63  to restore the c
12c9b 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e  urrent.** conten
12c9c 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 48  t of the page. H
12c9d 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68  owever, since th
12c9e 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  is content is no
12c9f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69 74  t present in eit
12ca0 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  her.** the datab
12ca1 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65 20  ase file or the 
12ca2 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  portion of the r
12ca3 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
12ca4 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72  and .** sub-jour
12ca5 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  nal rolled back 
12ca6 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75 6c  the content coul
12ca7 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72 65  d not be restore
12ca8 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74  d and the.** dat
12ca9 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f 75 6c  abase image woul
12caa 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74  d become corrupt
12cab 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72  . It is therefor
12cac 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 61 74  e fortunate that
12cad 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 75 6d   .** this circum
12cae 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 61 72  stance cannot ar
12caf 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ise..*/.#if defi
12cb0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
12cb1 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73  ).static void as
12cb2 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
12cb3 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 20 2a  traintCb(PgHdr *
12cb4 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20  pPg){.  assert( 
12cb5 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
12cb6 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 73 65  _DIRTY );.  asse
12cb7 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 72 65  rt( !subjRequire
12cb8 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 70 50  sPage(pPg) || pP
12cb9 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50  g->pgno<=pPg->pP
12cba 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 0a  ager->dbSize );.
12cbb 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73  }.static void as
12cbc 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
12cbd 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a 70 50  traint(Pager *pP
12cbe 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  ager){.  sqlite3
12cbf 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72  PcacheIterateDir
12cc0 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ty(pPager->pPCac
12cc1 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e 63 61  he, assertTrunca
12cc2 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 29 3b  teConstraintCb);
12cc3 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  .}.#else.# defin
12cc4 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65  e assertTruncate
12cc5 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65  Constraint(pPage
12cc6 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  r).#endif../*.**
12cc7 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e   Truncate the in
12cc8 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
12cc9 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e   file image to n
12cca 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 73  Page pages. This
12ccb 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f   .** function do
12ccc 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  es not actually 
12ccd 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
12cce 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b  ase file on disk
12ccf 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65  . It .** just se
12cd0 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ts the internal 
12cd1 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67  state of the pag
12cd2 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61  er object so tha
12cd3 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61  t the .** trunca
12cd4 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e  tion will be don
12cd5 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65  e when the curre
12cd6 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
12cd7 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a  s committed..*/.
12cd8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
12cd9 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
12cda 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61  TruncateImage(Pa
12cdb 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
12cdc 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65  o nPage){.  asse
12cdd 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
12cde 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73  zeValid );.  ass
12cdf 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
12ce0 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20  ize>=nPage );.  
12ce1 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
12ce2 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
12ce3 45 52 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65  ERVED );.  pPage
12ce4 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67  r->dbSize = nPag
12ce5 65 3b 0a 20 20 61 73 73 65 72 74 54 72 75 6e 63  e;.  assertTrunc
12ce6 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50  ateConstraint(pP
12ce7 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
12ce8 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67  Shutdown the pag
12ce9 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 20 61  e cache.  Free a
12cea 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 63 6c  ll memory and cl
12ceb 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a  ose all files..*
12cec 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e 73 61  *.** If a transa
12ced 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 72 6f  ction was in pro
12cee 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 73 20  gress when this 
12cef 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
12cf0 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 6e 73  d, that.** trans
12cf1 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64  action is rolled
12cf2 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 73   back.  All outs
12cf3 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 61 72  tanding pages ar
12cf4 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a  e invalidated.**
12cf5 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d 6f 72   and their memor
12cf6 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 6e 79  y is freed.  Any
12cf7 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
12cf8 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 74 65  a page associate
12cf9 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 20 70  d.** with this p
12cfa 61 67 65 20 63 61 63 68 65 20 61 66 74 65 72 20  age cache after 
12cfb 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
12cfc 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c  turns will likel
12cfd 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 61  y.** result in a
12cfe 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a   coredump..**.**
12cff 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
12d00 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e 20  lways succeeds. 
12d01 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  If a transaction
12d02 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 61 74   is active an at
12d03 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65  tempt.** is made
12d04 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b   to roll it back
12d05 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
12d06 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 20  curs during the 
12d07 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 20 68  rollback .** a h
12d08 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 62  ot journal may b
12d09 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69  e left in the fi
12d0a 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e 6f 20  lesystem but no 
12d0b 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
12d0c 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c  d.** to the call
12d0d 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
12d0e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12d0f 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65  3PagerClose(Page
12d10 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 64 69  r *pPager){.  di
12d11 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
12d12 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 73  io_errors();.  s
12d13 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
12d14 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 50 61  nMalloc();.  pPa
12d15 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 30  ger->errCode = 0
12d16 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c  ;.  pPager->excl
12d17 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 3b 0a 20  usiveMode = 0;. 
12d18 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
12d19 67 65 72 29 3b 0a 20 20 69 66 28 20 4d 45 4d 44  ger);.  if( MEMD
12d1a 42 20 29 7b 0a 20 20 20 20 70 61 67 65 72 5f 75  B ){.    pager_u
12d1b 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  nlock(pPager);. 
12d1c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53   }else{.    /* S
12d1d 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  et Pager.journal
12d1e 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 74 68  Hdr to -1 for th
12d1f 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74 68 65  e benefit of the
12d20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
12d21 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77  ) .    ** call w
12d22 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61 64 65  hich may be made
12d23 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67   from within pag
12d24 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
12d25 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 20 20  ack(). If it.   
12d26 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c 20 74   ** is not -1, t
12d27 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 65 64  hen the unsynced
12d28 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e 20 6f   portion of an o
12d29 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  pen journal file
12d2a 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 20 70   may.    ** be p
12d2b 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20  layed back into 
12d2c 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66  the database. If
12d2d 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
12d2e 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a 20 20   occurs while.  
12d2f 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70    ** this is hap
12d30 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 61  pening, the data
12d31 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d 65 20  base may become 
12d32 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a 2f 0a  corrupt..    */.
12d33 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
12d34 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 20 20  nalHdr = -1;.   
12d35 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52   pagerUnlockAndR
12d36 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b  ollback(pPager);
12d37 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e  .  }.  sqlite3En
12d38 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
12d39 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  .  enable_simula
12d3a 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
12d3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
12d3c 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 47  CLOSE %d\n", PAG
12d3d 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a  ERID(pPager)));.
12d3e 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53    IOTRACE(("CLOS
12d3f 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  E %p\n", pPager)
12d40 29 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  ).  sqlite3OsClo
12d41 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  se(pPager->fd);.
12d42 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65    sqlite3PageFre
12d43 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  e(pPager->pTmpSp
12d44 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  ace);.  sqlite3P
12d45 63 61 63 68 65 43 6c 6f 73 65 28 70 50 61 67 65  cacheClose(pPage
12d46 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 23 69  r->pPCache);..#i
12d47 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
12d48 43 4f 44 45 43 0a 20 20 69 66 28 20 70 50 61 67  CODEC.  if( pPag
12d49 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29  er->xCodecFree )
12d4a 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46   pPager->xCodecF
12d4b 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64  ree(pPager->pCod
12d4c 65 63 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61  ec);.#endif..  a
12d4d 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
12d4e 61 53 61 76 65 70 6f 69 6e 74 20 26 26 20 21 70  aSavepoint && !p
12d4f 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
12d50 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  l );.  assert( !
12d51 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
12d52 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70  fd) && !isOpen(p
12d53 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 3b 0a  Pager->sjfd) );.
12d54 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
12d55 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
12d56 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
12d57 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
12d58 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
12d59 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a  SQLITE_TEST)./*.
12d5a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
12d5b 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 61  ge number for pa
12d5c 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ge pPg..*/.SQLIT
12d5d 45 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73  E_PRIVATE Pgno s
12d5e 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e  qlite3PagerPagen
12d5f 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 50  umber(DbPage *pP
12d60 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67  g){.  return pPg
12d61 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66  ->pgno;.}.#endif
12d62 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  ../*.** Incremen
12d63 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  t the reference 
12d64 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 70  count for page p
12d65 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Pg..*/.SQLITE_PR
12d66 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
12d67 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67  e3PagerRef(DbPag
12d68 65 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74  e *pPg){.  sqlit
12d69 65 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29  e3PcacheRef(pPg)
12d6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20  ;.}../*.** Sync 
12d6b 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20  the journal. In 
12d6c 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b  other words, mak
12d6d 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 70  e sure all the p
12d6e 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 2a  ages that have.*
12d6f 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  * been written t
12d70 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61  o the journal ha
12d71 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 63  ve actually reac
12d72 68 65 64 20 74 68 65 20 73 75 72 66 61 63 65 20  hed the surface 
12d73 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61  of the.** disk a
12d74 6e 64 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72  nd can be restor
12d75 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20  ed in the event 
12d76 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  of a hot-journal
12d77 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
12d78 20 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e 65   If the Pager.ne
12d79 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e  edSync flag is n
12d7a 6f 74 20 73 65 74 2c 20 74 68 65 6e 20 74 68 69  ot set, then thi
12d7b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 0a  s function is a.
12d7c 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77  ** no-op. Otherw
12d7d 69 73 65 2c 20 74 68 65 20 61 63 74 69 6f 6e 73  ise, the actions
12d7e 20 72 65 71 75 69 72 65 64 20 64 65 70 65 6e 64   required depend
12d7f 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d   on the journal-
12d80 6d 6f 64 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  mode.** and the 
12d81 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
12d82 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 74 68  istics of the th
12d83 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 61  e file-system, a
12d84 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
12d85 20 20 20 2a 20 49 66 20 74 68 65 20 6a 6f 75 72     * If the jour
12d86 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6e 20 69  nal file is an i
12d87 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
12d88 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 69 6f 6e   file, no action
12d89 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 62 65 20   need.**     be 
12d8a 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  taken..**.**   *
12d8b 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74   Otherwise, if t
12d8c 68 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e  he device does n
12d8d 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53  ot support the S
12d8e 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65  AFE_APPEND prope
12d8f 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 68 65 6e  rty,.**     then
12d90 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20   the nRec field 
12d91 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
12d92 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75  ntly written jou
12d93 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 20  rnal header.**  
12d94 20 20 20 69 73 20 75 70 64 61 74 65 64 20 74 6f     is updated to
12d95 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d   contain the num
12d96 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 72  ber of journal r
12d97 65 63 6f 72 64 73 20 74 68 61 74 20 68 61 76 65  ecords that have
12d98 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 77 72 69  .**     been wri
12d99 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 69  tten following i
12d9a 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20  t. If the pager 
12d9b 69 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20  is operating in 
12d9c 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 20 20 20  full-sync.**    
12d9d 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20   mode, then the 
12d9e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
12d9f 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20 74 68  synced before th
12da0 69 73 20 66 69 65 6c 64 20 69 73 20 75 70 64 61  is field is upda
12da1 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49  ted..**.**   * I
12da2 66 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65  f the device doe
12da3 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68  s not support th
12da4 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f  e SEQUENTIAL pro
12da5 70 65 72 74 79 2c 20 74 68 65 6e 20 0a 2a 2a 20  perty, then .** 
12da6 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65      journal file
12da7 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a   is synced..**.*
12da8 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 64 6f 2d  * Or, in pseudo-
12da9 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66  code:.**.**   if
12daa 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79  ( NOT <in-memory
12dab 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20   journal> ){.** 
12dac 20 20 20 20 69 66 28 20 4e 4f 54 20 53 41 46 45      if( NOT SAFE
12dad 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a 20 20 20  _APPEND ){.**   
12dae 20 20 20 20 69 66 28 20 3c 66 75 6c 6c 2d 73 79      if( <full-sy
12daf 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 79 6e 63  nc mode> ) xSync
12db0 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29  (<journal file>)
12db1 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 70 64 61  ;.**       <upda
12db2 74 65 20 6e 52 65 63 20 66 69 65 6c 64 3e 0a 2a  te nRec field>.*
12db3 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 20 20 20  *     } .**     
12db4 69 66 28 20 4e 4f 54 20 53 45 51 55 45 4e 54 49  if( NOT SEQUENTI
12db5 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72  AL ) xSync(<jour
12db6 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20  nal file>);.**  
12db7 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61 67   }.**.** The Pag
12db8 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  er.needSync flag
12db9 20 69 73 20 6e 65 76 65 72 20 62 65 20 73 65 74   is never be set
12dba 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66   for temporary f
12dbb 69 6c 65 73 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20  iles, or any.** 
12dbc 66 69 6c 65 20 6f 70 65 72 61 74 69 6e 67 20 69  file operating i
12dbd 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 28  n no-sync mode (
12dbe 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 73 65 74  Pager.noSync set
12dbf 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a   to non-zero)..*
12dc0 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
12dc1 75 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ul, this routine
12dc2 20 63 6c 65 61 72 73 20 74 68 65 20 50 47 48 44   clears the PGHD
12dc3 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
12dc4 20 6f 66 20 65 76 65 72 79 20 0a 2a 2a 20 70 61   of every .** pa
12dc5 67 65 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c  ge currently hel
12dc6 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 65 66 6f  d in memory befo
12dc7 72 65 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c  re returning SQL
12dc8 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f  ITE_OK. If an IO
12dc9 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 65 6e 63  .** error is enc
12dca 6f 75 6e 74 65 72 65 64 2c 20 74 68 65 6e 20 74  ountered, then t
12dcb 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
12dcc 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
12dcd 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73  the caller..*/.s
12dce 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a 6f  tatic int syncJo
12dcf 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
12dd0 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67  ger){.  if( pPag
12dd1 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a  er->needSync ){.
12dd2 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
12dd3 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
12dd4 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
12dd5 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
12dd6 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
12dd7 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20  MEMORY ){.      
12dd8 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12dd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dda 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
12ddb 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  ode */.      con
12ddc 73 74 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c  st int iDc = sql
12ddd 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
12dde 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67  acteristics(pPag
12ddf 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 61  er->fd);.      a
12de0 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
12de1 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20  ager->jfd) );.. 
12de2 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63       if( 0==(iDc
12de3 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41  &SQLITE_IOCAP_SA
12de4 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 20  FE_APPEND) ){.  
12de5 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c        /* This bl
12de6 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61  ock deals with a
12de7 6e 20 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65  n obscure proble
12de8 6d 2e 20 49 66 20 74 68 65 20 6c 61 73 74 20 63  m. If the last c
12de9 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20  onnection.      
12dea 20 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20    ** that wrote 
12deb 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65  to this database
12dec 20 77 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69   was operating i
12ded 6e 20 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75  n persistent-jou
12dee 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20  rnal.        ** 
12def 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a  mode, then the j
12df0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20  ournal file may 
12df1 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63  at this point ac
12df2 74 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72  tually be larger
12df3 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e  .        ** than
12df4 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
12df5 66 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 20  f bytes. If the 
12df6 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68  next thing in th
12df7 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  e journal.      
12df8 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e    ** file happen
12df9 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61  s to be a journa
12dfa 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74 74 65  l-header (writte
12dfb 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  n as part of the
12dfc 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76  .        ** prev
12dfd 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ious connections
12dfe 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61   transaction), a
12dff 6e 64 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f  nd a crash or po
12e00 77 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20  wer-failure .   
12e01 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61       ** occurs a
12e02 66 74 65 72 20 6e 52 65 63 20 69 73 20 75 70 64  fter nRec is upd
12e03 61 74 65 64 20 62 75 74 20 62 65 66 6f 72 65 20  ated but before 
12e04 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  this connection 
12e05 77 72 69 74 65 73 20 0a 20 20 20 20 20 20 20 20  writes .        
12e06 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  ** anything else
12e07 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
12e08 66 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73  file (or commits
12e09 2f 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20  /rolls back its 
12e0a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e  .        ** tran
12e0b 73 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53  saction), then S
12e0c 51 4c 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65  QLite may become
12e0d 20 63 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64   confused when d
12e0e 6f 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20  oing the .      
12e0f 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c    ** hot-journal
12e10 20 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77   rollback follow
12e11 69 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74  ing recovery. It
12e12 20 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61   may roll back a
12e13 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  ll.        ** of
12e14 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
12e15 73 20 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f  s data, then pro
12e16 63 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20  ceed to rolling 
12e17 62 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20  back the old,.  
12e18 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d        ** out-of-
12e19 64 61 74 65 20 64 61 74 61 20 74 68 61 74 20 66  date data that f
12e1a 6f 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62  ollows it. Datab
12e1b 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
12e1c 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
12e1d 20 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72     ** To work ar
12e1e 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68  ound this, if th
12e1f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64  e journal file d
12e20 6f 65 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f  oes appear to co
12e21 6e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ntain.        **
12e22 20 61 20 76 61 6c 69 64 20 68 65 61 64 65 72 20   a valid header 
12e23 66 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e  following Pager.
12e24 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e  journalOff, then
12e25 20 77 72 69 74 65 20 61 20 30 78 30 30 0a 20 20   write a 0x00.  
12e26 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f        ** byte to
12e27 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74   the start of it
12e28 20 74 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66   to prevent it f
12e29 72 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e  rom being recogn
12e2a 69 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ized..        **
12e2b 0a 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69  .        ** Vari
12e2c 61 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66  able iNextHdrOff
12e2d 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68  set is set to th
12e2e 65 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 63  e offset at whic
12e2f 68 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a  h this.        *
12e30 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65  * problematic he
12e31 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c  ader will occur,
12e32 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 61   if it exists. a
12e33 4d 61 67 69 63 20 69 73 20 75 73 65 64 20 0a 20  Magic is used . 
12e34 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74         ** as a t
12e35 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20  emporary buffer 
12e36 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 66  to inspect the f
12e37 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62  irst couple of b
12e38 79 74 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20  ytes of.        
12e39 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c  ** the potential
12e3a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e   journal header.
12e3b 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
12e3c 20 20 20 20 69 36 34 20 69 4e 65 78 74 48 64 72      i64 iNextHdr
12e3d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20  Offset;.        
12e3e 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b 0a 09 75  u8 aMagic[8];..u
12e3f 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  8 zHeader[sizeof
12e40 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
12e41 34 5d 3b 0a 0a 09 6d 65 6d 63 70 79 28 7a 48 65  4];...memcpy(zHe
12e42 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  ader, aJournalMa
12e43 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75  gic, sizeof(aJou
12e44 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 09 70 75  rnalMagic));..pu
12e45 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
12e46 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
12e47 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d  Magic)], pPager-
12e48 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 20  >nRec);..       
12e49 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20   iNextHdrOffset 
12e4a 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
12e4b 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
12e4c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12e4d 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a  OsRead(pPager->j
12e4e 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 69  fd, aMagic, 8, i
12e4f 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a  NextHdrOffset);.
12e50 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
12e51 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d  SQLITE_OK && 0==
12e52 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61  memcmp(aMagic, a
12e53 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29  JournalMagic, 8)
12e54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74   ){.          st
12e55 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 7a 65  atic const u8 ze
12e56 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 20  robyte = 0;.    
12e57 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12e58 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
12e59 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 65  ->jfd, &zerobyte
12e5a 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 66  , 1, iNextHdrOff
12e5b 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  set);.        }.
12e5c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
12e5d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
12e5e 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
12e5f 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
12e60 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
12e61 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
12e62 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
12e63 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f   nRec value into
12e64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12e65 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a  e header. If in.
12e66 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d          ** full-
12e67 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65  synchronous mode
12e68 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  , sync the journ
12e69 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65  al first. This e
12e6a 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20  nsures that.    
12e6b 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20      ** all data 
12e6c 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74  has really hit t
12e6d 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e  he disk before n
12e6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74  Rec is updated t
12e6f 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a  o mark.        *
12e70 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64  * it as a candid
12e71 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b  ate for rollback
12e72 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
12e73 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20       ** This is 
12e74 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20  not required if 
12e75 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d  the persistent m
12e76 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68  edia supports th
12e77 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46  e.        ** SAF
12e78 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74  E_APPEND propert
12e79 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68  y. Because in th
12e7a 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f  is case it is no
12e7b 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20  t possible .    
12e7c 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61      ** for garba
12e7d 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70  ge data to be ap
12e7e 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69  pended to the fi
12e7f 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65  le, the nRec fie
12e80 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  ld.        ** is
12e81 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
12e82 30 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20  0xFFFFFFFF when 
12e83 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
12e84 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20  er is written.  
12e85 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76        ** and nev
12e86 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75  er needs to be u
12e87 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20  pdated..        
12e88 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  */.        if( p
12e89 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
12e8a 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54  && 0==(iDc&SQLIT
12e8b 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
12e8c 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  AL) ){.         
12e8d 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59   PAGERTRACE(("SY
12e8e 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64  NC journal of %d
12e8f 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
12e90 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20  ger)));.        
12e91 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e    IOTRACE(("JSYN
12e92 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  C %p\n", pPager)
12e93 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ).          rc =
12e94 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
12e95 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
12e96 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b  er->sync_flags);
12e97 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
12e98 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
12e99 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
12e9a 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52    }.        IOTR
12e9b 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c  ACE(("JHDR %p %l
12e9c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  ld\n", pPager, p
12e9d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
12e9e 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  r));.        rc 
12e9f 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
12ea0 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  (.            pP
12ea1 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64  ager->jfd, zHead
12ea2 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 64  er, sizeof(zHead
12ea3 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  er), pPager->jou
12ea4 72 6e 61 6c 48 64 72 0a 09 29 3b 0a 20 20 20 20  rnalHdr..);.    
12ea5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12ea6 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
12ea7 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
12ea8 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
12ea9 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
12eaa 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20  TIAL) ){.       
12eab 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59   PAGERTRACE(("SY
12eac 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64  NC journal of %d
12ead 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
12eae 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20  ger)));.        
12eaf 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20  IOTRACE(("JSYNC 
12eb0 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
12eb1 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12eb2 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
12eb3 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
12eb4 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20  sync_flags| .   
12eb5 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e         (pPager->
12eb6 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49  sync_flags==SQLI
12eb7 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c  TE_SYNC_FULL?SQL
12eb8 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c  ITE_SYNC_DATAONL
12eb9 59 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a  Y:0).        );.
12eba 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
12ebb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
12ebc 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
12ebd 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
12ebe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 61   journal file wa
12ebf 73 20 6a 75 73 74 20 73 75 63 63 65 73 73 66 75  s just successfu
12ec0 6c 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 74 20  lly synced. Set 
12ec1 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 0a  Pager.needSync .
12ec2 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 61      ** to zero a
12ec3 6e 64 20 63 6c 65 61 72 20 74 68 65 20 50 47 48  nd clear the PGH
12ec4 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61  DR_NEED_SYNC fla
12ec5 67 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 73 2e  g on all pagess.
12ec6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67  .    */.    pPag
12ec7 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
12ec8 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ;.    pPager->jo
12ec9 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31  urnalStarted = 1
12eca 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
12ecb 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67  cheClearSyncFlag
12ecc 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  s(pPager->pPCach
12ecd 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  e);.  }..  retur
12ece 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
12ecf 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65  /*.** The argume
12ed0 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20  nt is the first 
12ed1 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  in a linked list
12ed2 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20   of dirty pages 
12ed3 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 79 20  connected.** by 
12ed4 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79  the PgHdr.pDirty
12ed5 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 66   pointer. This f
12ed6 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 65  unction writes e
12ed7 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a  ach one of the.*
12ed8 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  * in-memory page
12ed9 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 6f  s in the list to
12eda 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12edb 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74  le. The argument
12edc 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c   may.** be NULL,
12edd 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 6e   representing an
12ede 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 6e 20   empty list. In 
12edf 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 66  this case this f
12ee0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 20  unction is.** a 
12ee1 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  no-op..**.** The
12ee2 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f 6c 64   pager must hold
12ee3 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45   at least a RESE
12ee4 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 74  RVED lock when t
12ee5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
12ee6 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f 72  is called. Befor
12ee7 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 69  e writing anythi
12ee8 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
12ee9 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c 6f  se file, this lo
12eea 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 65  ck.** is upgrade
12eeb 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56  d to an EXCLUSIV
12eec 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c  E lock. If the l
12eed 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62  ock cannot be ob
12eee 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c 49 54  tained,.** SQLIT
12eef 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e  E_BUSY is return
12ef0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69  ed and no data i
12ef1 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  s written to the
12ef2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
12ef3 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  ** .** If the pa
12ef4 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d 66 69  ger is a temp-fi
12ef5 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74 68 65  le pager and the
12ef6 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 79 73   actual file-sys
12ef7 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 20 6e  tem file.** is n
12ef8 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 74 20  ot yet open, it 
12ef9 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6f  is created and o
12efa 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61 6e 79  pened before any
12efb 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 72 69   data is .** wri
12efc 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20  tten out..**.** 
12efd 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 68 61  Once the lock ha
12efe 73 20 62 65 65 6e 20 75 70 67 72 61 64 65 64 20  s been upgraded 
12eff 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72  and, if necessar
12f00 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e  y, the file open
12f01 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 65 73  ed,.** the pages
12f02 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 75 74   are written out
12f03 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
12f04 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 6f 72   file in list or
12f05 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a 2a 20  der. Writing.** 
12f06 61 20 70 61 67 65 20 69 73 20 73 6b 69 70 70 65  a page is skippe
12f07 64 20 69 66 20 69 74 20 6d 65 65 74 73 20 65 69  d if it meets ei
12f08 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ther of the foll
12f09 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 3a 0a  owing criteria:.
12f0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61  **.**   * The pa
12f0b 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65  ge number is gre
12f0c 61 74 65 72 20 74 68 61 6e 20 50 61 67 65 72 2e  ater than Pager.
12f0d 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20  dbSize, or.**   
12f0e 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f 4e 54  * The PGHDR_DONT
12f0f 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73 20 73  _WRITE flag is s
12f10 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a  et on the page..
12f11 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e 67  **.** If writing
12f12 20 6f 75 74 20 61 20 70 61 67 65 20 63 61 75 73   out a page caus
12f13 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
12f14 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 61  file to grow, Pa
12f15 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 0a 2a  ger.dbFileSize.*
12f16 2a 20 69 73 20 75 70 64 61 74 65 64 20 61 63 63  * is updated acc
12f17 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 67  ordingly. If pag
12f18 65 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 6f  e 1 is written o
12f19 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ut, then the val
12f1a 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 6e 20  ue cached.** in 
12f1b 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
12f1c 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 6f  [] is updated to
12f1d 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 76   match the new v
12f1e 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a  alue stored in.*
12f1f 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
12f20 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76  ile..**.** If ev
12f21 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63  erything is succ
12f22 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
12f23 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
12f24 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a  f an IO error .*
12f25 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20  * occurs, an IO 
12f26 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
12f27 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 20 74  turned. Or, if t
12f28 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  he EXCLUSIVE loc
12f29 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f  k cannot.** be o
12f2a 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 45 5f  btained, SQLITE_
12f2b 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64  BUSY is returned
12f2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12f2d 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
12f2e 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c 69 73  list(PgHdr *pLis
12f2f 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  t){.  Pager *pPa
12f30 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ger;            
12f31 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
12f32 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ger object */.  
12f33 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f35 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
12f36 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 4e 45  ode */..  if( NE
12f37 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 29 20  VER(pList==0) ) 
12f38 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12f39 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69  ;.  pPager = pLi
12f3a 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f  st->pPager;..  /
12f3b 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  * At this point 
12f3c 74 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 74  there may be eit
12f3d 68 65 72 20 61 20 52 45 53 45 52 56 45 44 20 6f  her a RESERVED o
12f3e 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
12f3f 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   on the.  ** dat
12f40 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74  abase file. If t
12f41 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
12f42 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
12f43 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  k, the following
12f44 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20  .  ** call is a 
12f45 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  no-op..  **.  **
12f46 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b   Moving the lock
12f47 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74   from RESERVED t
12f48 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 75  o EXCLUSIVE actu
12f49 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f  ally involves go
12f4a 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68  ing.  ** through
12f4b 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65   an intermediate
12f4c 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20   state PENDING. 
12f4d 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b    A PENDING lock
12f4e 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20   prevents new.  
12f4f 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20  ** readers from 
12f50 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65  attaching to the
12f51 20 64 61 74 61 62 61 73 65 20 62 75 74 20 69 73   database but is
12f52 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f   unsufficient fo
12f53 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69  r us to.  ** wri
12f54 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66  te.  The idea of
12f55 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
12f56 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65  is to prevent ne
12f57 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20  w readers from. 
12f58 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68   ** coming in wh
12f59 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 20  ile we wait for 
12f5a 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 73  existing readers
12f5b 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a   to clear..  **.
12f5c 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70    ** While the p
12f5d 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 52  ager is in the R
12f5e 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74  ESERVED state, t
12f5f 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
12f60 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69  base file.  ** i
12f61 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20  s unchanged and 
12f62 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20  we can rollback 
12f63 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
12f64 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20  o playback the. 
12f65 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f   ** journal into
12f66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
12f67 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e  tabase file.  On
12f68 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e  ce we transition
12f69 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49   to.  ** EXCLUSI
12f6a 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65  VE, it means the
12f6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
12f6c 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20  as been changed 
12f6d 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b  and any rollback
12f6e 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69  .  ** will requi
12f6f 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61  re a journal pla
12f70 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73  yback..  */.  as
12f71 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
12f72 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52  ate>=PAGER_RESER
12f73 56 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61  VED );.  rc = pa
12f74 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b  ger_wait_on_lock
12f75 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49  (pPager, EXCLUSI
12f76 56 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20  VE_LOCK);..  /* 
12f77 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 61  If the file is a
12f78 20 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e   temp-file has n
12f79 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
12f7a 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e  ed, open it now.
12f7b 20 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20   It.  ** is not 
12f7c 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20  possible for rc 
12f7d 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e  to be other than
12f7e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68   SQLITE_OK if th
12f7f 69 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69  is branch.  ** i
12f80 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65  s taken, as page
12f81 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29  r_wait_on_lock()
12f82 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20   is a no-op for 
12f83 74 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f  temp-files..  */
12f84 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70  .  if( !isOpen(p
12f85 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20  Pager->fd) ){.  
12f86 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12f87 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63  ->tempFile && rc
12f88 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
12f89 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65     rc = pagerOpe
12f8a 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50  ntemp(pPager, pP
12f8b 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72  ager->fd, pPager
12f8c 2d 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d  ->vfsFlags);.  }
12f8d 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ..  while( rc==S
12f8e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73  QLITE_OK && pLis
12f8f 74 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67  t ){.    Pgno pg
12f90 6e 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f  no = pList->pgno
12f91 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
12f92 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67  re are dirty pag
12f93 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63  es in the page c
12f94 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e  ache with page n
12f95 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20  umbers greater. 
12f96 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72     ** than Pager
12f97 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65  .dbSize, this me
12f98 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72  ans sqlite3Pager
12f99 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20  TruncateImage() 
12f9a 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20  was called to.  
12f9b 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69    ** make the fi
12f9c 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73  le smaller (pres
12f9d 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76  umably by auto-v
12f9e 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20  acuum code). Do 
12f9f 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a  not write.    **
12fa0 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20   any such pages 
12fa1 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20  to the file..   
12fa2 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c   **.    ** Also,
12fa3 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75   do not write ou
12fa4 74 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20  t any page that 
12fa5 68 61 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f  has the PGHDR_DO
12fa6 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20  NT_WRITE flag.  
12fa7 20 20 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79    ** set (set by
12fa8 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
12fa9 74 57 72 69 74 65 28 29 29 2e 20 20 4e 6f 74 65  tWrite()).  Note
12faa 20 74 68 61 74 20 69 66 20 63 6f 6d 70 69 6c 65   that if compile
12fab 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 53 51  d with.    ** SQ
12fac 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
12fad 54 45 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e  TE the PGHDR_DON
12fae 54 5f 57 52 49 54 45 20 62 69 74 20 69 73 20 6e  T_WRITE bit is n
12faf 65 76 65 72 20 73 65 74 20 61 6e 64 20 73 6f 0a  ever set and so.
12fb0 20 20 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e      ** the secon
12fb1 64 20 74 65 73 74 20 69 73 20 61 6c 77 61 79 73  d test is always
12fb2 20 74 72 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   true..    */.  
12fb3 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67    if( pgno<=pPag
12fb4 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d  er->dbSize && 0=
12fb5 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50  =(pList->flags&P
12fb6 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29  GHDR_DONT_WRITE)
12fb7 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66   ){.      i64 of
12fb8 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a  fset = (pgno-1)*
12fb9 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67  (i64)pPager->pag
12fba 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73  eSize;   /* Offs
12fbb 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20  et to write */. 
12fbc 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61       char *pData
12fbd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12fbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fbf 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77      /* Data to w
12fc0 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20  rite */    ..   
12fc1 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65     /* Encode the
12fc2 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
12fc3 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72     CODEC2(pPager
12fc4 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20  , pList->pData, 
12fc5 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20  pgno, 6, return 
12fc6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44  SQLITE_NOMEM, pD
12fc7 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ata);..      /* 
12fc8 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61  Write out the pa
12fc9 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20  ge data. */.    
12fca 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12fcb 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
12fcc 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
12fcd 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
12fce 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66  t);..      /* If
12fcf 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74   page 1 was just
12fd0 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65   written, update
12fd1 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
12fd2 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20  s to match.     
12fd3 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f   ** the value no
12fd4 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  w stored in the 
12fd5 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
12fd6 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a  f writing this .
12fd7 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61        ** page ca
12fd8 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73  used the databas
12fd9 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20  e file to grow, 
12fda 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a  update dbFileSiz
12fdb 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e. .      */.   
12fdc 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29     if( pgno==1 )
12fdd 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
12fde 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  (&pPager->dbFile
12fdf 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d  Vers, &pData[24]
12fe0 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  , sizeof(pPager-
12fe1 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
12fe2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
12fe3 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62   pgno>pPager->db
12fe4 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  FileSize ){.    
12fe5 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
12fe6 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
12fe7 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
12fe8 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b   Update any back
12fe9 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69  up objects copyi
12fea 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ng the contents 
12feb 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a  of this pager. *
12fec 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  /.      sqlite3B
12fed 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67  ackupUpdate(pPag
12fee 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e  er->pBackup, pgn
12fef 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70  o, (u8*)pList->p
12ff0 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 50 41  Data);..      PA
12ff1 47 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45  GERTRACE(("STORE
12ff2 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68   %d page %d hash
12ff3 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20  (%08x)\n",.     
12ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41                PA
12ff5 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
12ff6 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68  gno, pager_pageh
12ff7 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a 20 20  ash(pList)));.  
12ff8 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47      IOTRACE(("PG
12ff9 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50  OUT %p %d\n", pP
12ffa 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20  ager, pgno));.  
12ffb 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73      PAGER_INCR(s
12ffc 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69  qlite3_pager_wri
12ffd 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20  tedb_count);.   
12ffe 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50     PAGER_INCR(pP
12fff 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20  ager->nWrite);. 
13000 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13001 50 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53  PAGERTRACE(("NOS
13002 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c  TORE %d page %d\
13003 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
13004 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20  er), pgno));.   
13005 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
13006 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
13007 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68   pList->pageHash
13008 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
13009 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66  h(pList);.#endif
1300a 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69  .    pList = pLi
1300b 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a  st->pDirty;.  }.
1300c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1300d 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
1300e 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63 75  record of the cu
1300f 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 70  rrent state of p
13010 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 73  age pPg to the s
13011 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20  ub-journal. .** 
13012 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72  It is the caller
13013 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  s responsibility
13014 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 75   to use subjRequ
13015 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 68  iresPage() to ch
13016 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74 20  eck .** that it 
13017 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72  is really requir
13018 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  ed before callin
13019 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  g this function.
1301a 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
1301b 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62 69  sful, set the bi
1301c 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
1301d 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20  to pPg->pgno in 
1301e 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20 66  the bitvecs.** f
1301f 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65  or all open save
13020 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72 65  points before re
13021 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  turning..**.** T
13022 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
13023 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69  urns SQLITE_OK i
13024 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  f everything is 
13025 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20 49  successful, an I
13026 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  O.** error code 
13027 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74  if the attempt t
13028 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73  o write to the s
13029 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73  ub-journal fails
1302a 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  , or .** SQLITE_
1302b 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f  NOMEM if a mallo
1302c 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73 65  c fails while se
1302d 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20 61  tting a bit in a
1302e 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69   savepoint.** bi
1302f 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tvec..*/.static 
13030 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  int subjournalPa
13031 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  ge(PgHdr *pPg){.
13032 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13033 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
13034 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
13035 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  ger;.  if( isOpe
13036 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  n(pPager->sjfd) 
13037 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61  ){.    void *pDa
13038 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b  ta = pPg->pData;
13039 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20  .    i64 offset 
1303a 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65  = pPager->nSubRe
1303b 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67  c*(4+pPager->pag
1303c 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68 61 72  eSize);.    char
1303d 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 43   *pData2;..    C
1303e 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44  ODEC2(pPager, pD
1303f 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ata, pPg->pgno, 
13040 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  7, return SQLITE
13041 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b  _NOMEM, pData2);
13042 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28  .    PAGERTRACE(
13043 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25  ("STMT-JOURNAL %
13044 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
13045 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
13046 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20  Pg->pgno));.  . 
13047 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49     assert( pageI
13048 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c  nJournal(pPg) ||
13049 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
1304a 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b  r->dbOrigSize );
1304b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33  .    rc = write3
1304c 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 73 6a  2bits(pPager->sj
1304d 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d  fd, offset, pPg-
1304e 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20  >pgno);.    if( 
1304f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13050 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13051 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
13052 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c  r->sjfd, pData2,
13053 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
13054 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20  e, offset+4);.  
13055 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63    }.  }.  if( rc
13056 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13057 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52     pPager->nSubR
13058 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74  ec++;.    assert
13059 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  ( pPager->nSavep
1305a 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 72 63  oint>0 );.    rc
1305b 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e   = addToSavepoin
1305c 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c  tBitvecs(pPager,
1305d 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d   pPg->pgno);.  }
1305e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1305f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
13060 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
13061 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6c 61  by the pcache la
13062 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20  yer when it has 
13063 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20  reached some.** 
13064 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69  soft memory limi
13065 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  t. The first arg
13066 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
13067 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 6f 62  er to a Pager ob
13068 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 61 73  ject.** (cast as
13069 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 20 70   a void*). The p
1306a 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 20 27  ager is always '
1306b 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 74 20  purgeable' (not 
1306c 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20  an in-memory.** 
1306d 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 20 73  database). The s
1306e 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
1306f 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
13070 20 61 20 70 61 67 65 20 74 68 61 74 20 69 73 20   a page that is 
13071 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 64 69  .** currently di
13072 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f 20 6f  rty but has no o
13073 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
13074 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 65 0a  ences. The page.
13075 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 73 73  ** is always ass
13076 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
13077 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61   Pager object pa
13078 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
13079 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  t .** argument..
1307a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66  **.** The job of
1307b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1307c 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c  s to make pPg cl
1307d 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 20 69  ean by writing i
1307e 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f  ts contents.** o
1307f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
13080 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f 73 73  se file, if poss
13081 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 69  ible. This may i
13082 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 20 74  nvolve syncing t
13083 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
13084 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  le. .**.** If su
13085 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 74 65  ccessful, sqlite
13086 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
13087 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  () is called on 
13088 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20  the page and.** 
13089 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
1308a 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  ed. If an IO err
1308b 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
1308c 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74  trying to make t
1308d 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e  he.** page clean
1308e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  , the IO error c
1308f 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
13090 20 49 66 20 74 68 65 20 70 61 67 65 20 63 61 6e   If the page can
13091 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63  not be.** made c
13092 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74  lean for some ot
13093 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 74 20  her reason, but 
13094 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  no error occurs,
13095 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a   then SQLITE_OK.
13096 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  ** is returned b
13097 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d  y sqlite3PcacheM
13098 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f  akeClean() is no
13099 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  t called..*/.sta
1309a 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 74 72  tic int pagerStr
1309b 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 67 48  ess(void *p, PgH
1309c 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
1309d 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67  r *pPager = (Pag
1309e 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63  er *)p;.  int rc
1309f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
130a0 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50   assert( pPg->pP
130a1 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a  ager==pPager );.
130a2 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66    assert( pPg->f
130a3 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
130a4 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f   );..  /* The do
130a5 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 73 20  NotSync flag is 
130a6 73 65 74 20 62 79 20 74 68 65 20 73 71 6c 69 74  set by the sqlit
130a7 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 66  e3PagerWrite() f
130a8 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 69 74  unction while it
130a9 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c  .  ** is journal
130aa 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20 74 77  ling a set of tw
130ab 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61  o or more databa
130ac 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72  se pages that ar
130ad 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e  e stored.  ** on
130ae 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b 20 73   the same disk s
130af 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74  ector. Syncing t
130b0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f  he journal is no
130b1 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a  t allowed while.
130b2 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70    ** this is hap
130b3 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69 73 20  pening as it is 
130b4 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61  important that a
130b5 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 73 75  ll members of su
130b6 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66  ch a.  ** set of
130b7 20 70 61 67 65 73 20 61 72 65 20 73 79 6e 63 65   pages are synce
130b8 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65 74 68  d to disk togeth
130b9 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65 20 70  er. So, if the p
130ba 61 67 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  age this functio
130bb 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69 6e 67  n.  ** is trying
130bc 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77   to make clean w
130bd 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f  ill require a jo
130be 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 20 74  urnal sync and t
130bf 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a  he doNotSync.  *
130c0 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 72  * flag is set, r
130c1 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f  eturn without do
130c2 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 54 68  ing anything. Th
130c3 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77  e pcache layer w
130c4 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61  ill.  ** just ha
130c5 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61  ve to go ahead a
130c6 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  nd allocate a ne
130c7 77 20 70 61 67 65 20 62 75 66 66 65 72 20 69 6e  w page buffer in
130c8 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65  stead of.  ** re
130c9 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a  using pPg..  **.
130ca 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20    ** Similarly, 
130cb 69 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73  if the pager has
130cc 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64   already entered
130cd 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
130ce 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72  , do not.  ** tr
130cf 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63  y to write the c
130d0 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67 20 74  ontents of pPg t
130d1 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69  o disk..  */.  i
130d2 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
130d3 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20  >errCode).   || 
130d4 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79  (pPager->doNotSy
130d5 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67 73  nc && pPg->flags
130d6 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
130d7 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
130d8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
130d9 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20  ..  /* Sync the 
130da 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
130db 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
130dc 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  f( pPg->flags&PG
130dd 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b  HDR_NEED_SYNC ){
130de 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f  .    rc = syncJo
130df 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
130e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
130e1 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
130e2 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20  fullSync && .   
130e3 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75     !(pPager->jou
130e4 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
130e5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
130e6 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73  RY) &&.      !(s
130e7 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
130e8 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50  aracteristics(pP
130e9 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45  ager->fd)&SQLITE
130ea 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
130eb 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ND).    ){.     
130ec 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
130ed 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72  0;.      rc = wr
130ee 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
130ef 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
130f0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
130f1 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69  ge number of thi
130f2 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72  s page is larger
130f3 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
130f4 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74  t size of.  ** t
130f5 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
130f6 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74  e, it may need t
130f7 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20  o be written to 
130f8 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
130f9 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65  .  ** This is be
130fa 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74  cause the call t
130fb 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  o pager_write_pa
130fc 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77  gelist() below w
130fd 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74  ill not.  ** act
130fe 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61  ually write data
130ff 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20   to the file in 
13100 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a  this case..  **.
13101 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68    ** Consider th
13102 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75  e following sequ
13103 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a  ence of events:.
13104 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
13105 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
13106 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a  rnal page X>.  *
13107 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61  *     <modify pa
13108 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53  ge X>.  **     S
13109 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a  AVEPOINT sp;.  *
1310a 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20  *       <shrink 
1310b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
1310c 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20   Y pages>.  **  
1310d 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73       pagerStress
1310e 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20  (page X).  **   
1310f 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70    ROLLBACK TO sp
13110 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28  ;.  **.  ** If (
13111 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20  X>Y), then when 
13112 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63  pagerStress is c
13113 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c  alled page X wil
13114 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  l not be written
13115 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65  .  ** out to the
13116 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
13117 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70  but will be drop
13118 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63  ped from the cac
13119 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66  he. Then,.  ** f
1311a 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f  ollowing the "RO
1311b 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74  LLBACK TO sp" st
1311c 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67  atement, reading
1311d 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61   page X will rea
1311e 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d  d.  ** data from
1311f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13120 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65  le. This will be
13121 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67   the copy of pag
13122 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77  e X as it.  ** w
13123 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  as when the tran
13124 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c  saction started,
13125 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77   not as it was w
13126 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73  hen "SAVEPOINT s
13127 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63  p".  ** was exec
13128 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted..  **.  ** 
13129 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
1312a 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72  to write the cur
1312b 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61  rent data for pa
1312c 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20  ge X into the . 
1312d 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   ** sub-journal 
1312e 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20  file now (if it 
1312f 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74  is not already t
13130 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69  here), so that i
13131 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72  t will.  ** be r
13132 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63  estored to its c
13133 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65  urrent value whe
13134 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  n the "ROLLBACK 
13135 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20  TO sp" is .  ** 
13136 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20  executed..  */. 
13137 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20   if( NEVER(.    
13138 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
13139 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  && pPg->pgno>pPa
1313a 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 73  ger->dbSize && s
1313b 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28  ubjRequiresPage(
1313c 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20 20 20  pPg).  ) ){.    
1313d 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50  rc = subjournalP
1313e 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20  age(pPg);.  }.. 
1313f 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
13140 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
13141 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  ge out to the da
13142 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
13143 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13144 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e  _OK ){.    pPg->
13145 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20  pDirty = 0;.    
13146 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65  rc = pager_write
13147 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a  _pagelist(pPg);.
13148 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74    }..  /* Mark t
13149 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e  he page as clean
1314a 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53  . */.  if( rc==S
1314b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1314c 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52  PAGERTRACE(("STR
1314d 45 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e  ESS %d page %d\n
1314e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
1314f 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b  r), pPg->pgno));
13150 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
13151 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29  heMakeClean(pPg)
13152 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
13153 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
13154 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  er, rc);.}.../*.
13155 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  ** Allocate and 
13156 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77  initialize a new
13157 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e   Pager object an
13158 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20  d put a pointer 
13159 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50  to it.** in *ppP
1315a 61 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20  ager. The pager 
1315b 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c  should eventuall
1315c 79 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61  y be freed by pa
1315d 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73  ssing it.** to s
1315e 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
1315f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46  ()..**.** The zF
13160 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74  ilename argument
13161 20 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20   is the path to 
13162 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13163 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66  e to open..** If
13164 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55   zFilename is NU
13165 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d  LL then a random
13166 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61  ly-named tempora
13167 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74  ry file is creat
13168 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61  ed.** and used a
13169 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  s the file to be
1316a 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61   cached. Tempora
1316b 72 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20  ry files are be 
1316c 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d  deleted.** autom
1316d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68  atically when th
1316e 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49  ey are closed. I
1316f 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22  f zFilename is "
13170 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a  :memory:" then .
13171 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  ** all informati
13172 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61  on is held in ca
13173 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72  che. It is never
13174 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b   written to disk
13175 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62  . .** This can b
13176 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
13177 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ent an in-memory
13178 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
13179 20 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61   The nExtra para
1317a 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20  meter specifies 
1317b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1317c 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c  tes of space all
1317d 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20  ocated.** along 
1317e 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72  with each page r
1317f 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73  eference. This s
13180 70 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c  pace is availabl
13181 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a  e to the user.**
13182 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33   via the sqlite3
13183 50 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20  PagerGetExtra() 
13184 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  API..**.** The f
13185 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73  lags argument is
13186 20 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79   used to specify
13187 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74   properties that
13188 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f   affect the.** o
13189 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
1318a 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64  pager. It should
1318b 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20   be passed some 
1318c 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74  bitwise combinat
1318d 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41  ion.** of the PA
1318e 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
1318f 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45   and PAGER_NO_RE
13190 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a  ADLOCK flags..**
13191 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73  .** The vfsFlags
13192 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20   parameter is a 
13193 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20  bitmask to pass 
13194 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  to the flags par
13195 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65  ameter.** of the
13196 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20   xOpen() method 
13197 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  of the supplied 
13198 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67  VFS when opening
13199 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49   files. .**.** I
1319a 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  f the pager obje
1319b 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ct is allocated 
1319c 61 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65  and the specifie
1319d 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a  d file opened .*
1319e 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  * successfully, 
1319f 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
131a0 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67  urned and *ppPag
131a1 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  er set to point 
131a2 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61  to.** the new pa
131a3 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61  ger object. If a
131a4 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
131a5 2a 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20  *ppPager is set 
131a6 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65  to NULL.** and e
131a7 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e  rror code return
131a8 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ed. This functio
131a9 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  n may return SQL
131aa 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71  ITE_NOMEM.** (sq
131ab 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73  lite3Malloc() is
131ac 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
131ad 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54  e memory), SQLIT
131ae 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a  E_CANTOPEN or .*
131af 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45  * various SQLITE
131b0 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a  _IO_XXX errors..
131b1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
131b2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
131b3 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  erOpen(.  sqlite
131b4 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
131b5 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
131b6 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f  l file system to
131b7 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20   use */.  Pager 
131b8 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20  **ppPager,      
131b9 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72     /* OUT: Retur
131ba 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75  n the Pager stru
131bb 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20  cture here */.  
131bc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
131bd 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65  ename,   /* Name
131be 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
131bf 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
131c0 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20  .  int nExtra,  
131c1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
131c2 78 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e  xtra bytes appen
131c3 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d  d to each in-mem
131c4 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ory page */.  in
131c5 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
131c6 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
131c7 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73  controlling this
131c8 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76   file */.  int v
131c9 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20  fsFlags,        
131ca 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73      /* flags pas
131cb 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
131cc 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
131cd 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  () */.  void (*x
131ce 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29  Reinit)(DbPage*)
131cf 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20   /* Function to 
131d0 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67  reinitialize pag
131d1 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70  es */.){.  u8 *p
131d2 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  Ptr;.  Pager *pP
131d3 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20  ager = 0;       
131d4 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
131d5 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  to allocate and 
131d6 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  return */.  int 
131d7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
131d8 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
131d9 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d  ode */.  int tem
131da 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20  pFile = 0;      
131db 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65    /* True for te
131dc 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20  mp files (incl. 
131dd 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29  in-memory files)
131de 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20   */.  int memDb 
131df 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
131e0 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
131e1 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66  s an in-memory f
131e2 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61  ile */.  int rea
131e3 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20  dOnly = 0;      
131e4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
131e5 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79  s is a read-only
131e6 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a   file */.  int j
131e7 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20  ournalFileSize; 
131e8 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20      /* Bytes to 
131e9 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63  allocate for eac
131ea 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a  h journal fd */.
131eb 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d    char *zPathnam
131ec 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75  e = 0;     /* Fu
131ed 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62  ll path to datab
131ee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  ase file */.  in
131ef 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b  t nPathname = 0;
131f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
131f1 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61   of bytes in zPa
131f2 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  thname */.  int 
131f3 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c  useJournal = (fl
131f4 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54  ags & PAGER_OMIT
131f5 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a  _JOURNAL)==0; /*
131f6 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a   False to omit j
131f7 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20  ournal */.  int 
131f8 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c  noReadlock = (fl
131f9 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52  ags & PAGER_NO_R
131fa 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a  EADLOCK)!=0;  /*
131fb 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65   True to omit re
131fc 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  ad-lock */.  int
131fd 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71   pcacheSize = sq
131fe 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28  lite3PcacheSize(
131ff 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65  );       /* Byte
13200 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f  s to allocate fo
13201 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31  r PCache */.  u1
13202 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53  6 szPageDflt = S
13203 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
13204 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66  GE_SIZE;  /* Def
13205 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a  ault page size *
13206 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
13207 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  ut how much spac
13208 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
13209 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66  r each journal f
1320a 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20  ile-handle.  ** 
1320b 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f  (there are two o
1320c 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e  f them, the main
1320d 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65   journal and the
1320e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54   sub-journal). T
1320f 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20  his.  ** is the 
13210 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65  maximum space re
13211 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e  quired for an in
13212 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
13213 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a  file handle .  *
13214 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20  * and a regular 
13215 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e  journal file-han
13216 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61  dle. Note that a
13217 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61   "regular journa
13218 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d  l-handle".  ** m
13219 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20  ay be a wrapper 
1321a 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69  capable of cachi
1321b 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72  ng the first por
1321c 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72  tion of the jour
1321d 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e  nal.  ** file in
1321e 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65   memory to imple
1321f 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d  ment the atomic-
13220 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
13221 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f  on (see .  ** so
13222 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61  urce file journa
13223 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  l.c)..  */.  if(
13224 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
13225 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65  ize(pVfs)>sqlite
13226 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
13227 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  ) ){.    journal
13228 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44  FileSize = ROUND
13229 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  8(sqlite3Journal
1322a 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d  Size(pVfs));.  }
1322b 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61  else{.    journa
1322c 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e  lFileSize = ROUN
1322d 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  D8(sqlite3MemJou
1322e 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d  rnalSize());.  }
1322f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f  ..  /* Set the o
13230 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74  utput variable t
13231 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61  o NULL in case a
13232 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  n error occurs. 
13233 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20  */.  *ppPager = 
13234 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65  0;..  /* Compute
13235 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66   and store the f
13236 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20  ull pathname in 
13237 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  an allocated buf
13238 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a  fer pointed.  **
13239 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65   to by zPathname
1323a 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61  , length nPathna
1323b 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20  me. Or, if this 
1323c 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  is a temporary f
1323d 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20  ile,.  ** leave 
1323e 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61  both nPathname a
1323f 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74  nd zPathname set
13240 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66   to 0..  */.  if
13241 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a  ( zFilename && z
13242 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20  Filename[0] ){. 
13243 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70     nPathname = p
13244 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
13245 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65  1;.    zPathname
13246 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
13247 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20  (nPathname*2);. 
13248 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65     if( zPathname
13249 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
1324a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1324b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1324c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
1324d 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72  RYDB.    if( str
1324e 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a  cmp(zFilename,":
1324f 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a  memory:")==0 ){.
13250 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b        memDb = 1;
13251 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65  .      zPathname
13252 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  [0] = 0;.    }el
13253 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
13254 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b        zPathname[
13255 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20  0] = 0; /* Make 
13256 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  sure initialized
13257 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74   even if FullPat
13258 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f  hname() fails */
13259 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1325a 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
1325b 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  e(pVfs, zFilenam
1325c 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50  e, nPathname, zP
1325d 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  athname);.    }.
1325e 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d  .    nPathname =
1325f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
13260 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  (zPathname);.   
13261 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13262 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b  OK && nPathname+
13263 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  8>pVfs->mxPathna
13264 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  me ){.      /* T
13265 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61  his branch is ta
13266 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75  ken when the jou
13267 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72  rnal path requir
13268 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74  ed by.      ** t
13269 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
1326a 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65  g opened will be
1326b 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d   more than pVfs-
1326c 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20  >mxPathname.    
1326d 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65    ** bytes in le
1326e 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73  ngth. This means
1326f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61   the database ca
13270 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a  nnot be opened,.
13271 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77        ** as it w
13272 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69  ill not be possi
13273 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ble to open the 
13274 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
13275 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68  even.      ** ch
13276 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f  eck for a hot-jo
13277 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61  urnal before rea
13278 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ding..      */. 
13279 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1327a 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
1327b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1327c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1327d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
1327e 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  thname);.      r
1327f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
13280 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
13281 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68  te memory for th
13282 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
13283 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74  e, PCache object
13284 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65  , the.  ** three
13285 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
13286 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  s, the database 
13287 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68  file name and th
13288 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20  e journal .  ** 
13289 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c  file name. The l
1328a 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20  ayout in memory 
1328b 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  is as follows:. 
1328c 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67   **.  **     Pag
1328d 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20  er object       
1328e 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69               (si
1328f 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65  zeof(Pager) byte
13290 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63  s).  **     PCac
13291 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20  he object       
13292 20 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c              (sql
13293 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29  ite3PcacheSize()
13294 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
13295 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   Database file h
13296 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20  andle           
13297 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65   (pVfs->szOsFile
13298 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
13299 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   Sub-journal fil
1329a 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  e handle        
1329b 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a   (journalFileSiz
1329c 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20  e bytes).  **   
1329d 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66    Main journal f
1329e 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20  ile handle      
1329f 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69    (journalFileSi
132a0 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20  ze bytes).  **  
132a1 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65     Database file
132a2 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20   name           
132a3 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20     (nPathname+1 
132a4 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
132a5 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  Journal file nam
132a6 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
132a7 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62  (nPathname+8+1 b
132a8 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74  ytes).  */.  pPt
132a9 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  r = (u8 *)sqlite
132aa 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20  3MallocZero(.   
132ab 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a   ROUND8(sizeof(*
132ac 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20  pPager)) +      
132ad 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75  /* Pager structu
132ae 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38  re */.    ROUND8
132af 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20  (pcacheSize) +  
132b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63           /* PCac
132b1 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20  he object */.   
132b2 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a   ROUND8(pVfs->sz
132b3 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20  OsFile) +       
132b4 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66  /* The main db f
132b5 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e  ile */.    journ
132b6 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b  alFileSize * 2 +
132b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
132b8 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   two journal fil
132b9 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68  es */ .    nPath
132ba 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20  name + 1 +      
132bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69            /* zFi
132bc 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50  lename */.    nP
132bd 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20  athname + 8 + 1 
132be 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
132bf 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b  zJournal */.  );
132c0 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
132c1 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
132c2 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
132c3 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a  R(journalFileSiz
132c4 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50  e)) );.  if( !pP
132c5 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  tr ){.    sqlite
132c6 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65  3_free(zPathname
132c7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
132c8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
132c9 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20    pPager =      
132ca 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29          (Pager*)
132cb 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72  (pPtr);.  pPager
132cc 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28  ->pPCache =    (
132cd 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d  PCache*)(pPtr +=
132ce 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a   ROUND8(sizeof(*
132cf 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61  pPager)));.  pPa
132d0 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c  ger->fd =   (sql
132d1 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72  ite3_file*)(pPtr
132d2 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68   += ROUND8(pcach
132d3 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65  eSize));.  pPage
132d4 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74  r->sjfd = (sqlit
132d5 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b  e3_file*)(pPtr +
132d6 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73  = ROUND8(pVfs->s
132d7 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61  zOsFile));.  pPa
132d8 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c  ger->jfd =  (sql
132d9 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72  ite3_file*)(pPtr
132da 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53   += journalFileS
132db 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ize);.  pPager->
132dc 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28  zFilename =    (
132dd 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a  char*)(pPtr += j
132de 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b  ournalFileSize);
132df 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
132e0 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
132e1 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
132e2 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
132e3 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d  e Pager.zFilenam
132e4 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75  e and Pager.zJou
132e5 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66  rnal buffers, if
132e6 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
132e7 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b  if( zPathname ){
132e8 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  .    pPager->zJo
132e9 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a  urnal =   (char*
132ea 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e  )(pPtr += nPathn
132eb 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65  ame + 1);.    me
132ec 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69  mcpy(pPager->zFi
132ed 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d  lename, zPathnam
132ee 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20  e, nPathname);. 
132ef 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72     memcpy(pPager
132f0 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74  ->zJournal, zPat
132f1 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65  hname, nPathname
132f2 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70  );.    memcpy(&p
132f3 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b  Pager->zJournal[
132f4 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f  nPathname], "-jo
132f5 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20  urnal", 8);.    
132f6 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c  if( pPager->zFil
132f7 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50  ename[0]==0 ) pP
132f8 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30  ager->zJournal[0
132f9 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
132fa 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d  e3_free(zPathnam
132fb 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72  e);.  }.  pPager
132fc 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
132fd 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67   pPager->vfsFlag
132fe 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20  s = vfsFlags;.. 
132ff 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67   /* Open the pag
13300 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  er file..  */.  
13301 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  if( zFilename &&
13302 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26   zFilename[0] &&
13303 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69   !memDb ){.    i
13304 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20  nt fout = 0;    
13305 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13306 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72 65 74  /* VFS flags ret
13307 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29  urned by xOpen()
13308 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
13309 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
1330a 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
1330b 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20  me, pPager->fd, 
1330c 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29  vfsFlags, &fout)
1330d 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d  ;.    readOnly =
1330e 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50   (fout&SQLITE_OP
1330f 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20  EN_READONLY);.. 
13310 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
13311 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  e was successful
13312 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65  ly opened for re
13313 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c  ad/write access,
13314 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61  .    ** choose a
13315 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69   default page si
13316 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 61  ze in case we ha
13317 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ve to create the
13318 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
13319 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75   file. The defau
1331a 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  lt page size is 
1331b 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a  the maximum of:.
1331c 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
1331d 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   + SQLITE_DEFAUL
1331e 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20  T_PAGE_SIZE,.   
1331f 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c   **    + The val
13320 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
13321 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69  qlite3OsSectorSi
13322 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b  ze().    **    +
13323 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61 67   The largest pag
13324 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20  e size that can 
13325 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69  be written atomi
13326 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  cally..    */.  
13327 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13328 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79  _OK && !readOnly
13329 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63   ){.      setSec
1332a 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b  torSize(pPager);
1332b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51  .      assert(SQ
1332c 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1332d 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d  E_SIZE<=SQLITE_M
1332e 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1332f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28  SIZE);.      if(
13330 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67   szPageDflt<pPag
13331 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29  er->sectorSize )
13332 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  {.        if( pP
13333 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
13334 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  >SQLITE_MAX_DEFA
13335 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b  ULT_PAGE_SIZE ){
13336 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67  .          szPag
13337 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d  eDflt = SQLITE_M
13338 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
13339 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65  SIZE;.        }e
1333a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
1333b 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36  zPageDflt = (u16
1333c 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  )pPager->sectorS
1333d 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ize;.        }. 
1333e 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51       }.#ifdef SQ
1333f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
13340 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b  IC_WRITE.      {
13341 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 63  .        int iDc
13342 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69   = sqlite3OsDevi
13343 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
13344 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  s(pPager->fd);. 
13345 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20         int ii;. 
13346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51         assert(SQ
13347 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
13348 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b  C512==(512>>8));
13349 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1334a 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
1334b 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e  MIC64K==(65536>>
1334c 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  8));.        ass
1334d 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ert(SQLITE_MAX_D
1334e 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1334f 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20  <=65536);.      
13350 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44    for(ii=szPageD
13351 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f  flt; ii<=SQLITE_
13352 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  MAX_DEFAULT_PAGE
13353 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b  _SIZE; ii=ii*2){
13354 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
13355 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  Dc&(SQLITE_IOCAP
13356 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29  _ATOMIC|(ii>>8))
13357 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
13358 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b  szPageDflt = ii;
13359 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1335a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23       }.      }.#
1335b 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65  endif.    }.  }e
1335c 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61  lse{.    /* If a
1335d 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
1335e 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74  is requested, it
1335f 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69   is not opened i
13360 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20  mmediately..    
13361 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
13362 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65  we accept the de
13363 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20  fault page size 
13364 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c  and delay actual
13365 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e  ly.    ** openin
13366 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c  g the file until
13367 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
13368 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20  to OsWrite()..  
13369 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
1336a 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20   branch is also 
1336b 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  run for an in-me
1336c 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 41  mory database. A
1336d 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20  n in-memory.    
1336e 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 74  ** database is t
1336f 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d  he same as a tem
13370 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e  p-file that is n
13371 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74  ever written out
13372 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20   to.    ** disk 
13373 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d  and uses an in-m
13374 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  emory rollback j
13375 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a  ournal..    */ .
13376 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31      tempFile = 1
13377 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
13378 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c  ate = PAGER_EXCL
13379 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61 64 4f  USIVE;.    readO
1337a 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67 73 26  nly = (vfsFlags&
1337b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
1337c 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ONLY);.  }..  /*
1337d 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
1337e 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50  all to PagerSetP
1337f 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 65 73  agesize() serves
13380 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75   to set the valu
13381 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72  e of .  ** Pager
13382 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f  .pageSize and to
13383 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61   allocate the Pa
13384 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75  ger.pTmpSpace bu
13385 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ffer..  */.  if(
13386 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13387 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
13388 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29  ager->memDb==0 )
13389 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1338a 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
1338b 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61  ze(pPager, &szPa
1338c 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20  geDflt, -1);.   
1338d 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
1338e 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a  QLITE_OK );.  }.
1338f 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f  .  /* If an erro
13390 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69  r occurred in ei
13391 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63  ther of the bloc
13392 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74  ks above, free t
13393 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73  he .  ** Pager s
13394 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f  tructure and clo
13395 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a  se the file..  *
13396 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
13397 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73  TE_OK ){.    ass
13398 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54  ert( !pPager->pT
13399 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73  mpSpace );.    s
1339a 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1339b 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73  ager->fd);.    s
1339c 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
1339d 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  er);.    return 
1339e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  rc;.  }..  /* In
1339f 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61  itialize the PCa
133a0 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20  che object. */. 
133a1 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3c   assert( nExtra<
133a2 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74 72 61  1000 );.  nExtra
133a3 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72 61   = ROUND8(nExtra
133a4 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  );.  sqlite3Pcac
133a5 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66 6c  heOpen(szPageDfl
133a6 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44  t, nExtra, !memD
133a7 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
133a8 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70 61         !memDb?pa
133a9 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76 6f  gerStress:0, (vo
133aa 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50 61  id *)pPager, pPa
133ab 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a  ger->pPCache);..
133ac 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4f    PAGERTRACE(("O
133ad 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46 49  PEN %d %s\n", FI
133ae 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67 65  LEHANDLEID(pPage
133af 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d 3e  r->fd), pPager->
133b0 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49  zFilename));.  I
133b1 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 70  OTRACE(("OPEN %p
133b2 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %s\n", pPager, 
133b3 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
133b4 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75  e))..  pPager->u
133b5 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29  seJournal = (u8)
133b6 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50  useJournal;.  pP
133b7 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b  ager->noReadlock
133b8 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26   = (noReadlock &
133b9 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30  & readOnly) ?1:0
133ba 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  ;.  /* pPager->s
133bb 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a  tmtOpen = 0; */.
133bc 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
133bd 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20  tInUse = 0; */. 
133be 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 66   /* pPager->nRef
133bf 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65   = 0; */.  pPage
133c0 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
133c1 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a   (u8)memDb;.  /*
133c2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a   pPager->stmtSiz
133c3 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  e = 0; */.  /* p
133c4 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65  Pager->stmtJSize
133c5 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
133c6 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b  ager->nPage = 0;
133c7 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78   */.  pPager->mx
133c8 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41  Pgno = SQLITE_MA
133c9 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20  X_PAGE_COUNT;.  
133ca 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  /* pPager->state
133cb 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b   = PAGER_UNLOCK;
133cc 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
133cd 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28  ager->state == (
133ce 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52  tempFile ? PAGER
133cf 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47  _EXCLUSIVE : PAG
133d0 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20  ER_UNLOCK) );.  
133d1 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61  /* pPager->errMa
133d2 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  sk = 0; */.  pPa
133d3 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20  ger->tempFile = 
133d4 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20  (u8)tempFile;.  
133d5 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65  assert( tempFile
133d6 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
133d7 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20  ODE_NORMAL .    
133d8 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c        || tempFil
133d9 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
133da 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
133db 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
133dc 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
133dd 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20  CLUSIVE==1 );.  
133de 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
133df 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70  eMode = (u8)temp
133e0 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d  File; .  pPager-
133e1 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
133e2 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46   = pPager->tempF
133e3 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d  ile;.  pPager->m
133e4 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44 62  emDb = (u8)memDb
133e5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61 64  ;.  pPager->read
133e6 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64 4f  Only = (u8)readO
133e7 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  nly;.  /* pPager
133e8 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20  ->needSync = 0; 
133e9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 73 65  */.  assert( use
133ea 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65  Journal || pPage
133eb 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20  r->tempFile );. 
133ec 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
133ed 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  = pPager->tempFi
133ee 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75  le;.  pPager->fu
133ef 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d  llSync = pPager-
133f0 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20  >noSync ?0:1;.  
133f1 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
133f2 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs = SQLITE_SYNC
133f3 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50  _NORMAL;.  /* pP
133f4 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30  ager->pFirst = 0
133f5 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
133f6 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d  ->pFirstSynced =
133f7 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
133f8 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a  er->pLast = 0; *
133f9 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74  /.  pPager->nExt
133fa 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72 61  ra = (u16)nExtra
133fb 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ;.  pPager->jour
133fc 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53  nalSizeLimit = S
133fd 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f  QLITE_DEFAULT_JO
133fe 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54  URNAL_SIZE_LIMIT
133ff 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ;.  assert( isOp
13400 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
13401 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  | tempFile );.  
13402 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50  setSectorSize(pP
13403 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 75 73  ager);.  if( !us
13404 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  eJournal ){.    
13405 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
13406 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
13407 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d  NALMODE_OFF;.  }
13408 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 20 29  else if( memDb )
13409 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
1340a 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45  urnalMode = PAGE
1340b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
1340c 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70  MORY;.  }.  /* p
1340d 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
1340e 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ler = 0; */.  /*
1340f 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61   pPager->pBusyHa
13410 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f  ndlerArg = 0; */
13411 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e  .  pPager->xRein
13412 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a  iter = xReinit;.
13413 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67    /* memset(pPag
13414 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69  er->aHash, 0, si
13415 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61  zeof(pPager->aHa
13416 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61  sh)); */.  *ppPa
13417 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20  ger = pPager;.  
13418 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
13419 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ;.}..../*.** Thi
1341a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1341b 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73  lled after trans
1341c 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41  itioning from PA
1341d 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a  GER_UNLOCK to.**
1341e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74   PAGER_SHARED st
1341f 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66  ate. It tests if
13420 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20   there is a hot 
13421 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20  journal present 
13422 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73  in.** the file-s
13423 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69  ystem for the gi
13424 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74  ven pager. A hot
13425 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20   journal is one 
13426 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74  that .** needs t
13427 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  o be played back
13428 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  . According to t
13429 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20  his function, a 
1342a 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  hot-journal.** f
1342b 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68  ile exists if th
1342c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74  e following crit
1342d 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a  eria are met:.**
1342e 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72  .**   * The jour
1342f 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
13430 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74  in the file syst
13431 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e  em, and.**   * N
13432 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  o process holds 
13433 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72  a RESERVED or gr
13434 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68  eater lock on th
13435 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
13436 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
13437 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74  database file it
13438 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20  self is greater 
13439 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20  than 0 bytes in 
1343a 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  size, and.**   *
1343b 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
1343c 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
1343d 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69  ile exists and i
1343e 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a  s not 0x00..**.*
1343f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74  * If the current
13440 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
13441 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20  abase file is 0 
13442 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  but a journal fi
13443 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68  le.** exists, th
13444 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61  at is probably a
13445 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65  n old journal le
13446 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70  ft over from a p
13447 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  rior.** database
13448 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
13449 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ame. In this cas
1344a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
1344b 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65  le is.** just de
1344c 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65  leted using OsDe
1344d 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69  lete, *pExists i
1344e 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  s set to 0 and S
1344f 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72  QLITE_OK.** is r
13450 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
13451 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
13452 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68   not check if th
13453 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20  ere is a master 
13454 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
13455 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f  .** at the end o
13456 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74  f the file. If t
13457 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61  here is, and tha
13458 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  t master journal
13459 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f   file.** does no
1345a 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68  t exist, then th
1345b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
1345c 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74  s not really hot
1345d 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73  . In this.** cas
1345e 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
1345f 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c  ill return a fal
13460 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65  se-positive. The
13461 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
13462 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c  ).** routine wil
13463 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20  l discover that 
13464 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13465 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68   is not really h
13466 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20  ot and .** will 
13467 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b  not roll it back
13468 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f  . .**.** If a ho
13469 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  t-journal file i
1346a 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74  s found to exist
1346b 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65  , *pExists is se
1346c 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53  t to 1 and .** S
1346d 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
1346e 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75  d. If no hot-jou
1346f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
13470 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69  sent, *pExists i
13471 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e  s.** set to 0 an
13472 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
13473 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
13474 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
13475 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64  e trying.** to d
13476 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
13477 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f   or not a hot-jo
13478 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74  urnal file exist
13479 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a  s, the IO error.
1347a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  ** code is retur
1347b 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75  ned and the valu
1347c 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73  e of *pExists is
1347d 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73   undefined..*/.s
1347e 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74  tatic int hasHot
1347f 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
13480 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69  Pager, int *pExi
13481 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sts){.  sqlite3_
13482 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73  vfs * const pVfs
13483 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b   = pPager->pVfs;
13484 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
13485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13486 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
13487 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b  */.  int exists;
13488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13489 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
1348a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
1348b 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73  present */..  as
1348c 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20  sert( pPager!=0 
1348d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1348e 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
1348f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
13490 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
13491 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73  );.  assert( !is
13492 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
13493 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
13494 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20  Pager->state <= 
13495 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a  PAGER_SHARED );.
13496 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b  .  *pExists = 0;
13497 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
13498 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50  sAccess(pVfs, pP
13499 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
1349a 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1349b 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a  ISTS, &exists);.
1349c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1349d 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b  _OK && exists ){
1349e 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b  .    int locked;
1349f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134a0 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65   /* True if some
134a1 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61   process holds a
134a2 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a   RESERVED lock *
134a3 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 20 63  /..    /* Race c
134a4 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a 20 20  ondition here:  
134a5 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  Another process 
134a6 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20  might have been 
134a7 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 20 20  holding the.    
134a8 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 20  ** the RESERVED 
134a9 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 61 20  lock and have a 
134aa 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20  journal open at 
134ab 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 63 63  the sqlite3OsAcc
134ac 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 63 61  ess() .    ** ca
134ad 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 74 68  ll above, but th
134ae 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f  en delete the jo
134af 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 20 74  urnal and drop t
134b0 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20  he lock before. 
134b1 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 6f 20     ** we get to 
134b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71  the following sq
134b3 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65  lite3OsCheckRese
134b4 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e  rvedLock() call.
134b5 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 2a 2a    If that.    **
134b6 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
134b7 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
134b8 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 73 20   think there is 
134b9 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68  a hot journal wh
134ba 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 61 63  en.    ** in fac
134bb 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e  t there is none.
134bc 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 20 69    This results i
134bd 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69  n a false-positi
134be 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20  ve which will.  
134bf 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69    ** be dealt wi
134c0 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 62 61  th by the playba
134c1 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 69 63  ck routine.  Tic
134c2 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 20 2a  ket #3883..    *
134c3 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
134c4 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
134c5 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  dLock(pPager->fd
134c6 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20  , &locked);.    
134c7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
134c8 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a  K && !locked ){.
134c9 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b        int nPage;
134ca 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b  ..      /* Check
134cb 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
134cc 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
134cd 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f  If it consists o
134ce 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20  f 0 pages,.     
134cf 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20   ** then delete 
134d0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
134d1 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  . See the header
134d2 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66   comment above f
134d3 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  or .      ** the
134d4 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e   reasoning here.
134d5 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62 73    Delete the obs
134d6 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69  olete journal fi
134d7 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20 2a  le under.      *
134d8 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  * a RESERVED loc
134d9 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65 20  k to avoid race 
134da 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74  conditions and t
134db 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e  o avoid violatin
134dc 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 30  g.      ** [H330
134dd 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  20]..      */.  
134de 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
134df 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
134e0 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
134e1 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
134e2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
134e3 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20     if( nPage==0 
134e4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
134e5 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
134e6 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20  alloc();.       
134e7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73     if( sqlite3Os
134e8 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
134e9 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d   RESERVED_LOCK)=
134ea 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
134eb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
134ec 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
134ed 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
134ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
134ef 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63    sqlite3OsUnloc
134f0 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48  k(pPager->fd, SH
134f1 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  ARED_LOCK);.    
134f2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
134f3 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
134f4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
134f5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
134f6 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72       /* The jour
134f7 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
134f8 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e  and no other con
134f9 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65  nection has a re
134fa 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 20 20  served.         
134fb 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c   ** or greater l
134fc 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
134fd 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68  ase file. Now ch
134fe 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69  eck that there i
134ff 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  s.          ** a
13500 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d  t least one non-
13501 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68  zero bytes at th
13502 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a  e start of the j
13503 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20  ournal file..   
13504 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65         ** If the
13505 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63  re is, then we c
13506 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75  onsider this jou
13507 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20  rnal to be hot. 
13508 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20  If not, .       
13509 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20     ** it can be 
1350a 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20  ignored..       
1350b 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
1350c 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f  int f = SQLITE_O
1350d 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c  PEN_READONLY|SQL
1350e 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
1350f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  URNAL;.         
13510 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
13511 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
13512 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67  ->zJournal, pPag
13513 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b  er->jfd, f, &f);
13514 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
13515 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13516 20 20 20 20 20 20 20 20 20 20 20 20 75 38 20 66              u8 f
13517 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  irst = 0;.      
13518 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13519 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
1351a 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66  >jfd, (void *)&f
1351b 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  irst, 1, 0);.   
1351c 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
1351d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
1351e 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
1351f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
13520 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
13521 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13522 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
13523 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
13524 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45  .            *pE
13525 78 69 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d  xists = (first!=
13526 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  0);.          }e
13527 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
13528 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20  TE_CANTOPEN ){. 
13529 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1352a 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20   we cannot open 
1352b 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
1352c 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64  rnal file in ord
1352d 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20 20 20  er to see if.   
1352e 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 20           ** its 
1352f 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61 64 65  has a zero heade
13530 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  r, that might be
13531 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65   due to an I/O e
13532 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20 20 20  rror, or.       
13533 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74       ** it might
13534 20 62 65 20 64 75 65 20 74 6f 20 74 68 65 20 72   be due to the r
13535 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65  ace condition de
13536 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 61 6e  scribed above an
13537 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20  d in.           
13538 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38 38 33   ** ticket #3883
13539 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61  .  Either way, a
1353a 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 6a  ssume that the j
1353b 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20  ournal is hot.. 
1353c 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68             ** Th
1353d 69 73 20 6d 69 67 68 74 20 62 65 20 61 20 66 61  is might be a fa
1353e 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20 20 42  lse positive.  B
1353f 75 74 20 69 66 20 69 74 20 69 73 2c 20 74 68 65  ut if it is, the
13540 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
13541 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a    ** automatic j
13542 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20  ournal playback 
13543 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d 65 63  and recovery mec
13544 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c  hanism will deal
13545 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
13546 77 69 74 68 20 69 74 20 75 6e 64 65 72 20 61 6e  with it under an
13547 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
13548 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20  where we do not 
13549 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20  need to.        
1354a 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20      ** worry so 
1354b 6d 75 63 68 20 77 69 74 68 20 72 61 63 65 20 63  much with race c
1354c 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20  onditions..     
1354d 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1354e 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d        *pExists =
1354f 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
13550 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
13551 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
13552 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
13553 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
13554 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
13555 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ead the content 
13556 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f 75 74  for page pPg out
13557 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
13558 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a   file and into .
13559 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e 20 41  ** pPg->pData. A
1355a 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20   shared lock or 
1355b 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 65 20  greater must be 
1355c 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
1355d 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66  base.** file bef
1355e 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
1355f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  n is called..**.
13560 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 73 20  ** If page 1 is 
13561 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 20 76  read, then the v
13562 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62  alue of Pager.db
13563 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 73 65  FileVers[] is se
13564 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75  t to.** the valu
13565 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
13566 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
13567 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72  *.** If an IO er
13568 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
13569 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 69 73   the IO error is
1356a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
1356b 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65   caller..** Othe
1356c 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b  rwise, SQLITE_OK
1356d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
1356e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
1356f 44 62 50 61 67 65 28 50 67 48 64 72 20 2a 70 50  DbPage(PgHdr *pP
13570 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  g){.  Pager *pPa
13571 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
13572 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65  r; /* Pager obje
13573 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ct associated wi
13574 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f 0a 20  th page pPg */. 
13575 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67   Pgno pgno = pPg
13576 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a  ->pgno;       /*
13577 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
13578 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63  read */.  int rc
13579 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1357a 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
1357b 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69   code */.  i64 i
1357c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
1357d 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
1357e 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 20 74  offset of file t
1357f 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a  o read from */..
13580 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13581 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53  ->state>=PAGER_S
13582 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 42 20  HARED && !MEMDB 
13583 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  );.  assert( isO
13584 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
13585 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  );..  if( NEVER(
13586 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
13587 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65  fd)) ){.    asse
13588 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  rt( pPager->temp
13589 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73  File );.    mems
1358a 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30  et(pPg->pData, 0
1358b 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
1358c 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ze);.    return 
1358d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1358e 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f   iOffset = (pgno
1358f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d  -1)*(i64)pPager-
13590 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20  >pageSize;.  rc 
13591 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
13592 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d  pPager->fd, pPg-
13593 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e  >pData, pPager->
13594 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65  pageSize, iOffse
13595 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
13596 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
13597 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20  _READ ){.    rc 
13598 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
13599 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29  .  if( pgno==1 )
1359a 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65  {.    u8 *dbFile
1359b 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50  Vers = &((u8*)pP
1359c 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20  g->pData)[24];. 
1359d 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
1359e 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64  r->dbFileVers, d
1359f 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
135a0 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
135a1 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f  Vers));.  }.  CO
135a2 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67  DEC1(pPager, pPg
135a3 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33  ->pData, pgno, 3
135a4 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  , rc = SQLITE_NO
135a5 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49  MEM);..  PAGER_I
135a6 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65  NCR(sqlite3_page
135a7 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b  r_readdb_count);
135a8 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50  .  PAGER_INCR(pP
135a9 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20  ager->nRead);.  
135aa 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25  IOTRACE(("PGIN %
135ab 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
135ac 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52   pgno));.  PAGER
135ad 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64  TRACE(("FETCH %d
135ae 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
135af 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  8x)\n",.        
135b0 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
135b1 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
135b2 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
135b3 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  )));..  return r
135b4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
135b5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
135b6 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  led to obtain a 
135b7 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74  shared lock on t
135b8 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
135b9 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67  ..** It is illeg
135ba 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  al to call sqlit
135bb 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 29  e3PagerAcquire()
135bc 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 69   until after thi
135bd 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61  s function.** ha
135be 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75  s been successfu
135bf 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66 20 61  lly called. If a
135c0 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20   shared-lock is 
135c1 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77 68 65  already held whe
135c2 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  n.** this functi
135c3 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74  on is called, it
135c4 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
135c5 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
135c6 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
135c7 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64 20 62  also performed b
135c8 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
135c9 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74  .**.**   1) If t
135ca 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72  he pager is curr
135cb 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 5f 55  ently in PAGER_U
135cc 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e 6f 20  NLOCK state (no 
135cd 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20  lock held.**    
135ce 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73    on the databas
135cf 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e  e file), then an
135d0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
135d1 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20   to obtain a.** 
135d2 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b       SHARED lock
135d3 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
135d4 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65   file. Immediate
135d5 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69  ly after obtaini
135d6 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 53  ng.**      the S
135d7 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
135d8 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 20 63  file-system is c
135d9 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 6f 74  hecked for a hot
135da 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20  -journal,.**    
135db 20 20 77 68 69 63 68 20 69 73 20 70 6c 61 79 65    which is playe
135dc 64 20 62 61 63 6b 20 69 66 20 70 72 65 73 65 6e  d back if presen
135dd 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79  t. Following any
135de 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a   hot-journal .**
135df 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20        rollback, 
135e0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
135e1 74 68 65 20 63 61 63 68 65 20 61 72 65 20 76 61  the cache are va
135e2 6c 69 64 61 74 65 64 20 62 79 20 63 68 65 63 6b  lidated by check
135e3 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20  ing.**      the 
135e4 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27  'change-counter'
135e5 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 64 61   field of the da
135e6 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64  tabase file head
135e7 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64  er and.**      d
135e8 69 73 63 61 72 64 65 64 20 69 66 20 74 68 65 79  iscarded if they
135e9 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20 62 65   are found to be
135ea 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20   invalid..**.** 
135eb 20 20 32 29 20 49 66 20 74 68 65 20 70 61 67 65    2) If the page
135ec 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20  r is running in 
135ed 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20  exclusive-mode, 
135ee 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 63 75  and there are cu
135ef 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20  rrently.**      
135f0 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  no outstanding r
135f1 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79  eferences to any
135f2 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73 20 69   pages, and is i
135f3 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  n the error stat
135f4 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20  e,.**      then 
135f5 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
135f6 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20  de to clear the 
135f7 65 72 72 6f 72 20 73 74 61 74 65 20 62 79 20 64  error state by d
135f8 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20  iscarding.**    
135f9 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f    the contents o
135fa 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
135fb 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63   and rolling bac
135fc 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e  k any open journ
135fd 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e  al.**      file.
135fe 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
135ff 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65  eration describe
13600 64 20 62 79 20 28 32 29 20 61 62 6f 76 65 20 69  d by (2) above i
13601 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c  s not attempted,
13602 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a 20 70   and if the.** p
13603 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72  ager is in an er
13604 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20  ror state other 
13605 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  than SQLITE_FULL
13606 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61   when this is ca
13607 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72  lled,.** the err
13608 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63  or state error c
13609 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1360a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64   It is permitted
1360b 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a 2a 20   to read the.** 
1360c 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 69 6e  database when in
1360d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72   SQLITE_FULL err
1360e 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  or state..**.** 
1360f 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76  Otherwise, if ev
13610 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63  erything is succ
13611 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
13612 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
13613 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72  f an.** IO error
13614 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c 6f   occurs while lo
13615 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  cking the databa
13616 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f 72  se, checking for
13617 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a   a hot-journal.*
13618 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e  * file or rollin
13619 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  g back a journal
1361a 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 65 72   file, the IO er
1361b 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
1361c 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
1361d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1361e 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f  te3PagerSharedLo
1361f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
13620 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
13621 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
13622 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
13623 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
13624 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 30  isErrorReset = 0
13625 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
13626 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f 76 65  * True if recove
13627 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f 72 20  ring from error 
13628 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54  state */..  /* T
13629 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f  his routine is o
1362a 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  nly called from 
1362b 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20  b-tree and only 
1362c 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e  when there are n
1362d 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69  o.  ** outstandi
1362e 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20 61 73  ng pages */.  as
1362f 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61  sert( sqlite3Pca
13630 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
13631 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20  er->pPCache)==0 
13632 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 4d  );.  if( NEVER(M
13633 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
13634 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 74 75  errCode) ){ retu
13635 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
13636 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  de; }..  /* If t
13637 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20  his database is 
13638 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74  in an error-stat
13639 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68 61 6e  e, now is a chan
1363a 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a  ce to clear.  **
1363b 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63   the error. Disc
1363c 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
1363d 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61   of the pager-ca
1363e 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b  che and rollback
1363f 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f  .  ** any hot jo
13640 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c  urnal in the fil
13641 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  e-system..  */. 
13642 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
13643 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20  Code ){.    if( 
13644 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13645 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a  fd) || pPager->z
13646 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  Journal ){.     
13647 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20   isErrorReset = 
13648 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  1;.    }.    pPa
13649 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53  ger->errCode = S
1364a 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61  QLITE_OK;.    pa
1364b 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
1364c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
1364d 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
1364e 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45  ER_UNLOCK || isE
1364f 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20  rrorReset ){.   
13650 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63   sqlite3_vfs * c
13651 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67  onst pVfs = pPag
13652 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e  er->pVfs;.    in
13653 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d  t isHotJournal =
13654 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
13655 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73  !MEMDB );.    as
13656 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61  sert( sqlite3Pca
13657 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
13658 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20  er->pPCache)==0 
13659 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
1365a 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b  r->noReadlock ){
1365b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1365c 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
1365d 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
1365e 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53  >state = PAGER_S
1365f 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65  HARED;.    }else
13660 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
13661 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
13662 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c  pPager, SHARED_L
13663 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
13664 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13665 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
13666 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
13667 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
13668 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
13669 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
1366a 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  r, rc);.      }.
1366b 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
1366c 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
1366d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
1366e 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75  .    /* If a jou
1366f 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73  rnal file exists
13670 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e  , and there is n
13671 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  o RESERVED lock 
13672 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  on the.    ** da
13673 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
13674 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64  n it either need
13675 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62  s to be played b
13676 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ack or deleted..
13677 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
13678 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a  isErrorReset ){.
13679 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1367a 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50  ager->state <= P
1367b 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20  AGER_SHARED );. 
1367c 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f 74       rc = hasHot
1367d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20  Journal(pPager, 
1367e 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a  &isHotJournal);.
1367f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13680 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13681 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
13682 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13683 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65 73    if( isErrorRes
13684 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e  et || isHotJourn
13685 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47  al ){.      /* G
13686 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  et an EXCLUSIVE 
13687 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
13688 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 68  base file. At th
13689 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a 20  is point it is. 
1368a 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e       ** importan
1368b 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 45  t that a RESERVE
1368c 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62  D lock is not ob
1368d 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 61  tained on the wa
1368e 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a  y to the.      *
1368f 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  * EXCLUSIVE lock
13690 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 6e  . If it were, an
13691 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69  other process mi
13692 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 20  ght open the.   
13693 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66     ** database f
13694 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 20  ile, detect the 
13695 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61  RESERVED lock, a
13696 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74  nd conclude that
13697 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61   the.      ** da
13698 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 74  tabase is safe t
13699 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 69  o read while thi
1369a 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69  s process is sti
1369b 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a  ll rolling the .
1369c 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75        ** hot-jou
1369d 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20  rnal back..     
1369e 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65   ** .      ** Be
1369f 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d  cause the interm
136a0 65 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20  ediate RESERVED 
136a1 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75  lock is not requ
136a2 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 20 20  ested, any.     
136a3 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 73   ** other proces
136a4 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  s attempting to 
136a5 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62  access the datab
136a6 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 67 65  ase file will ge
136a7 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74  t to .      ** t
136a8 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  his point in the
136a9 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74   code and fail t
136aa 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e  o obtain its own
136ab 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
136ac 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65  .      ** on the
136ad 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
136ae 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
136af 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
136b0 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  <EXCLUSIVE_LOCK 
136b1 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
136b2 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
136b3 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53  ager->fd, EXCLUS
136b4 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  IVE_LOCK);.     
136b5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
136b6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
136b7 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
136b8 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
136b9 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66            goto f
136ba 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d  ailed;.        }
136bb 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
136bc 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45  >state = PAGER_E
136bd 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20  XCLUSIVE;.      
136be 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65  }. .      /* Ope
136bf 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f  n the journal fo
136c0 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  r read/write acc
136c1 65 73 73 2e 20 54 68 69 73 20 69 73 20 62 65 63  ess. This is bec
136c2 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a  ause in .      *
136c3 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65  * exclusive-acce
136c4 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65  ss mode the file
136c5 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c   descriptor will
136c6 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e   be kept open an
136c7 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69  d.      ** possi
136c8 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 20 74  bly used for a t
136c9 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72  ransaction later
136ca 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73   on. On some sys
136cb 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20  tems, the.      
136cc 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20  ** OsTruncate() 
136cd 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63  call used in exc
136ce 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f  lusive-access mo
136cf 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73  de also requires
136d0 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64  .      ** a read
136d1 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64  /write file hand
136d2 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  le..      */.   
136d3 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70     if( !isOpen(p
136d4 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
136d5 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a         int res;.
136d6 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
136d7 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
136d8 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  s,pPager->zJourn
136d9 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53 53  al,SQLITE_ACCESS
136da 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a 20  _EXISTS,&res);. 
136db 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
136dc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
136dd 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b        if( res ){
136de 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
136df 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 20   fout = 0;.     
136e0 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53         int f = S
136e1 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
136e2 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
136e3 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20  _MAIN_JOURNAL;. 
136e4 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
136e5 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  t( !pPager->temp
136e6 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20  File );.        
136e7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
136e8 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61  OsOpen(pVfs, pPa
136e9 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70  ger->zJournal, p
136ea 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26  Pager->jfd, f, &
136eb 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 20  fout);.         
136ec 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
136ed 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70  QLITE_OK || isOp
136ee 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
136ef 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
136f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
136f1 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45 5f   && fout&SQLITE_
136f2 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b  OPEN_READONLY ){
136f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
136f4 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  c = SQLITE_CANTO
136f5 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20  PEN;.           
136f6 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
136f7 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
136f8 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
136f9 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
136fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
136fb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65   the journal doe
136fc 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 74 20  s not exist, it 
136fd 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68  usually means th
136fe 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 20 20  at some .       
136ff 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f       ** other co
13700 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64  nnection managed
13701 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72   to get in and r
13702 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f  oll it back befo
13703 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  re .            
13704 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  ** this connecti
13705 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65 20  on obtained the 
13706 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61  exclusive lock a
13707 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20  bove. Or, it .  
13708 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79            ** may
13709 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 70   mean that the p
1370a 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 65 20  ager was in the 
1370b 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e  error-state when
1370c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20   this.          
1370d 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61    ** function wa
1370e 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 65  s called and the
1370f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f   journal file do
13710 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a  es not exist.  *
13711 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
13712 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
13713 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
13714 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   0);.          }
13715 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13716 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21   }.      if( rc!
13717 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13718 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65        goto faile
13719 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  d;.      }..    
1371a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61    /* TODO: Why a
1371b 72 65 20 74 68 65 73 65 20 63 6c 65 61 72 65 64  re these cleared
1371c 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e 65 63   here? Is it nec
1371d 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20  essary? */.     
1371e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1371f 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
13720 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13721 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20  alOff = 0;.     
13722 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
13723 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  er = 0;.      pP
13724 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
13725 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a   = 0;. .      /*
13726 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 64 65   Playback and de
13727 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
13728 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 74 61  .  Drop the data
13729 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 20 20  base write.     
1372a 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61   ** lock and rea
1372b 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
1372c 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 65 20  lock. Purge the 
1372d 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 20 20  cache before.   
1372e 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61     ** playing ba
1372f 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e  ck the hot-journ
13730 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f  al so that we do
13731 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 68 0a  n't end up with.
13732 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f        ** an inco
13733 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 2e 0a  nsistent cache..
13734 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
13735 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  f( isOpen(pPager
13736 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
13737 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
13738 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29  yback(pPager, 1)
13739 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1373a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1373b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61           rc = pa
1373c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72  ger_error(pPager
1373d 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20  , rc);.         
1373e 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
1373f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13740 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
13741 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
13742 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20  GER_SHARED).    
13743 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65         || (pPage
13744 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
13745 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74   && pPager->stat
13746 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a  e>PAGER_SHARED).
13747 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a        );.    }..
13748 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
13749 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74  pBackup || sqlit
1374a 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e  e3PcachePagecoun
1374b 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
1374c 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  e)>0 ){.      /*
1374d 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b   The shared-lock
1374e 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61   has just been a
1374f 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64  cquired on the d
13750 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20  atabase file.   
13751 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20     ** and there 
13752 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65  are already page
13753 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28  s in the cache (
13754 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a  from a previous.
13755 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72        ** read or
13756 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
13757 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  on).  Check to s
13758 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ee if the databa
13759 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20  se.      ** has 
1375a 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20  been modified.  
1375b 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1375c 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75  has changed, flu
1375d 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  sh the.      ** 
1375e 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a  cache..      **.
1375f 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73        ** Databas
13760 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74  e changes is det
13761 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67  ected by looking
13762 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67   at 15 bytes beg
13763 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  inning.      ** 
13764 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74  at offset 24 int
13765 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  o the file.  The
13766 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73   first 4 of thes
13767 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20  e 16 bytes are. 
13768 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74       ** a 32-bit
13769 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73   counter that is
1376a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   incremented wit
1376b 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20  h each change.  
1376c 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  The.      ** oth
1376d 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20  er bytes change 
1376e 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61  randomly with ea
1376f 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77  ch file change w
13770 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63  hen.      ** a c
13771 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a  odec is in use..
13772 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20        ** .      
13773 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61  ** There is a va
13774 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20  nishingly small 
13775 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68  chance that a ch
13776 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65  ange will not be
13777 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63   .      ** detec
13778 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65  ted.  The chance
13779 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65   of an undetecte
1377a 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73  d change is so s
1377b 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20  mall that.      
1377c 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67  ** it can be neg
1377d 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f  lected..      */
1377e 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69  .      char dbFi
1377f 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50  leVers[sizeof(pP
13780 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
13781 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  )];.      sqlite
13782 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
13783 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20  pPager, 0);..   
13784 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
13785 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
13786 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
13787 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20  rrCode;.        
13788 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
13789 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
1378a 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
1378b 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20  zeValid );.     
1378c 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
1378d 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ize>0 ){.       
1378e 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52   IOTRACE(("CKVER
1378f 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  S %p %d\n", pPag
13790 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  er, sizeof(dbFil
13791 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20  eVers)));.      
13792 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13793 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
13794 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69   &dbFileVers, si
13795 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29  zeof(dbFileVers)
13796 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69  , 24);.        i
13797 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13798 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
13799 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
1379a 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1379b 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  {.        memset
1379c 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20  (dbFileVers, 0, 
1379d 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
1379e 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  s));.      }..  
1379f 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
137a0 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
137a1 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73  s, dbFileVers, s
137a2 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
137a3 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))!=0 ){.       
137a4 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
137a5 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ger);.      }.  
137a6 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
137a7 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
137a8 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d  eMode || pPager-
137a9 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48  >state==PAGER_SH
137aa 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61  ARED );.  }.. fa
137ab 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d  iled:.  if( rc!=
137ac 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
137ad 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b   /* pager_unlock
137ae 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  () is a no-op fo
137af 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  r exclusive mode
137b0 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64   and in-memory d
137b1 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20  atabases. */.   
137b2 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50   pager_unlock(pP
137b3 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ager);.  }.  ret
137b4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
137b5 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63   If the referenc
137b6 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63  e count has reac
137b7 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61  hed zero, rollba
137b8 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a  ck any active.**
137b9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
137ba 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65   unlock the page
137bb 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c  r..**.** Except,
137bc 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   in locking_mode
137bd 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65 6e 20  =EXCLUSIVE when 
137be 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
137bf 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f   to in.** the ro
137c0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
137c1 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f  the unlock is no
137c2 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e 64 20  t performed and 
137c3 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68  there is.** noth
137c4 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c  ing to rollback,
137c5 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   so this routine
137c6 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20   is a no-op..*/ 
137c7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
137c8 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64  erUnlockIfUnused
137c9 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
137ca 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 50  .  if( (sqlite3P
137cb 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
137cc 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
137cd 30 29 0a 20 20 20 26 26 20 28 21 70 50 61 67 65  0).   && (!pPage
137ce 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
137cf 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
137d0 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a  nalOff>0) .  ){.
137d1 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41      pagerUnlockA
137d2 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65  ndRollback(pPage
137d3 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
137d4 20 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72   Acquire a refer
137d5 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d  ence to page num
137d6 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65  ber pgno in page
137d7 72 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65  r pPager (a page
137d8 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61  .** reference ha
137d9 73 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e  s type DbPage*).
137da 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65   If the requeste
137db 64 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a  d reference is .
137dc 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ** successfully 
137dd 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20  obtained, it is 
137de 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67  copied to *ppPag
137df 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
137e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
137e1 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
137e2 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79   page is already
137e3 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69   in the cache, i
137e4 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a  t is returned. .
137e5 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20  ** Otherwise, a 
137e6 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20  new page object 
137e7 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
137e8 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
137e9 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f  data.** read fro
137ea 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
137eb 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73  ile. In some cas
137ec 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d  es, the pcache m
137ed 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f  odule may.** cho
137ee 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63  ose not to alloc
137ef 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f  ate a new page o
137f0 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65  bject and may re
137f1 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  use an existing.
137f2 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e  ** object with n
137f3 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  o outstanding re
137f4 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  ferences..**.** 
137f5 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61  The extra data a
137f6 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67  ppended to a pag
137f7 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74  e is always init
137f8 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73  ialized to zeros
137f9 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74   the .** first t
137fa 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f  ime a page is lo
137fb 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  aded into memory
137fc 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65  . If the page re
137fd 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61  quested is .** a
137fe 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
137ff 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75  che when this fu
13800 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
13801 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61  , then the extra
13802 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74  .** data is left
13803 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20   as it was when 
13804 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20  the page object 
13805 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a  was last used..*
13806 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  *.** If the data
13807 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d  base image is sm
13808 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72  aller than the r
13809 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72  equested page or
1380a 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65   if a .** non-ze
1380b 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73  ro value is pass
1380c 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74  ed as the noCont
1380d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e  ent parameter an
1380e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73  d the .** reques
1380f 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20  ted page is not 
13810 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69  already stored i
13811 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65  n the cache, the
13812 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20  n no .** actual 
13813 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73  disk read occurs
13814 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
13815 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20  he memory image 
13816 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20  of the .** page 
13817 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
13818 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a  o all zeros. .**
13819 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74  .** If noContent
1381a 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61   is true, it mea
1381b 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f  ns that we do no
1381c 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65  t care about the
1381d 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20   contents.** of 
1381e 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f  the page. This o
1381f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70  ccurs in two sep
13820 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a  erate scenarios:
13821 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e  .**.**   a) When
13822 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d   reading a free-
13823 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66  list leaf page f
13824 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
13825 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29  , and.**.**   b)
13826 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e   When a savepoin
13827 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65  t is being rolle
13828 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65  d back and we ne
13829 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20  ed to load.**   
1382a 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e     a new page in
1382b 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20  to the cache to 
1382c 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68  populate with th
1382d 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20  e data read.**  
1382e 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76      from the sav
1382f 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a  epoint journal..
13830 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65  **.** If noConte
13831 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  nt is true, then
13832 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e   the data return
13833 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73  ed is zeroed ins
13834 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67  tead of.** being
13835 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
13836 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f  atabase. Additio
13837 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20  nally, the bits 
13838 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
13839 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65   to pgno in Page
1383a 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69  r.pInJournal (bi
1383b 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c  tvec of pages al
1383c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f  ready written to
1383d 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
1383e 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61  file) and the Pa
1383f 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e  gerSavepoint.pIn
13840 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  Savepoint bitvec
13841 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a  s of any open.**
13842 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20   savepoints are 
13843 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  set. This means 
13844 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d  if the page is m
13845 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20  ade writable at 
13846 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20  any.** point in 
13847 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e  the future, usin
13848 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  g a call to sqli
13849 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c  te3PagerWrite(),
1384a 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   its contents.**
1384b 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75   will not be jou
1384c 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76  rnaled. This sav
1384d 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  es IO..**.** The
1384e 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67   acquisition mig
1384f 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65  ht fail for seve
13850 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e  ral reasons.  In
13851 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61   all cases,.** a
13852 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
13853 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
13854 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65  rned and *ppPage
13855 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
13856 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
13857 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b  sqlite3PagerLook
13858 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73  up().  Both this
13859 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f   routine and Loo
1385a 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a  kup() attempt.**
1385b 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20   to find a page 
1385c 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  in the in-memory
1385d 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49   cache first.  I
1385e 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
1385f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20  t already.** in 
13860 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75  memory, this rou
13861 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73  tine goes to dis
13862 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20  k to read it in 
13863 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29  whereas Lookup()
13864 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73  .** just returns
13865 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e   0.  This routin
13866 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61  e acquires a rea
13867 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74  d-lock the first
13868 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20   time it.** has 
13869 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61  to go to disk, a
1386a 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c  nd could also pl
1386b 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f  ayback an old jo
1386c 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61  urnal if necessa
1386d 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f  ry..** Since Loo
1386e 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73  kup() never goes
1386f 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76   to disk, it nev
13870 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77  er has to deal w
13871 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20  ith locks.** or 
13872 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a  journal files..*
13873 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13874 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13875 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65  rAcquire(.  Page
13876 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
13877 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65  /* The pager ope
13878 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
13879 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  e file */.  Pgno
1387a 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
1387b 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
1387c 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50  o fetch */.  DbP
1387d 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20  age **ppPage,   
1387e 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e   /* Write a poin
1387f 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
13880 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  here */.  int no
13881 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a  Content       /*
13882 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72   Do not bother r
13883 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66  eading content f
13884 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65  rom disk if true
13885 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
13886 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a  .  PgHdr *pPg;..
13887 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
13888 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
13889 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ger) );.  assert
1388a 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
1388b 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
1388c 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
1388d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1388e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1388f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
13890 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74  he pager is in t
13891 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
13892 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
13893 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20  immediately. .  
13894 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  ** Otherwise, re
13895 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66  quest the page f
13896 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c  rom the PCache l
13897 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ayer. */.  if( p
13898 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d  Pager->errCode!=
13899 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
1389a 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51  ger->errCode!=SQ
1389b 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  LITE_FULL ){.   
1389c 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
1389d 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rCode;.  }else{.
1389e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1389f 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
138a0 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
138a1 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20  o, 1, ppPage);. 
138a2 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51   }..  if( rc!=SQ
138a3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
138a4 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c  * Either the cal
138a5 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63  l to sqlite3Pcac
138a6 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e  heFetch() return
138a7 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74  ed an error or t
138a8 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20  he.    ** pager 
138a9 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74  was already in t
138aa 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77  he error-state w
138ab 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
138ac 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20  n was called..  
138ad 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20    ** Set pPg to 
138ae 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68  0 and jump to th
138af 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64  e exception hand
138b0 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67  ler.  */.    pPg
138b1 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70   = 0;.    goto p
138b2 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
138b3 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
138b4 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d  (*ppPage)->pgno=
138b5 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  =pgno );.  asser
138b6 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50  t( (*ppPage)->pP
138b7 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20  ager==pPager || 
138b8 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65  (*ppPage)->pPage
138b9 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28  r==0 );..  if( (
138ba 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72  *ppPage)->pPager
138bb 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68   ){.    /* In th
138bc 69 73 20 63 61 73 65 20 74 68 65 20 70 63 61 63  is case the pcac
138bd 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61  he already conta
138be 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a  ins an initializ
138bf 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a  ed copy of.    *
138c0 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65 74 75  * the page. Retu
138c1 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72 74 68  rn without furth
138c2 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20  er ado.  */.    
138c3 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41  assert( pgno<=PA
138c4 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20  GER_MAX_PGNO && 
138c5 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50  pgno!=PAGER_MJ_P
138c6 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20  GNO(pPager) );. 
138c7 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50     PAGER_INCR(pP
138c8 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20  ager->nHit);.   
138c9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
138ca 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  K;..  }else{.   
138cb 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61   /* The pager ca
138cc 63 68 65 20 68 61 73 20 63 72 65 61 74 65 64 20  che has created 
138cd 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20  a new page. Its 
138ce 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f  content needs to
138cf 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74   .    ** be init
138d0 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20  ialized.  */.   
138d1 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20   int nMax;..    
138d2 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
138d3 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70  r->nMiss);.    p
138d4 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20  Pg = *ppPage;.  
138d5 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d 20    pPg->pPager = 
138d6 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20  pPager;..    /* 
138d7 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  The maximum page
138d8 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e   number is 2^31.
138d9 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   Return SQLITE_C
138da 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67 65  ORRUPT if a page
138db 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67  .    ** number g
138dc 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73  reater than this
138dd 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65 64 20  , or the unused 
138de 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73  locking-page, is
138df 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20   requested. */. 
138e0 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45     if( pgno>PAGE
138e1 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67  R_MAX_PGNO || pg
138e2 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no==PAGER_MJ_PGN
138e3 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  O(pPager) ){.   
138e4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
138e5 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
138e6 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63     goto pager_ac
138e7 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d  quire_err;.    }
138e8 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
138e9 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
138ea 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b  (pPager, &nMax);
138eb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
138ec 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
138ed 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
138ee 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20  re_err;.    }.. 
138ef 20 20 20 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20     if( MEMDB || 
138f0 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c  nMax<(int)pgno |
138f1 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20  | noContent ){. 
138f2 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50       if( pgno>pP
138f3 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a  ager->mxPgno ){.
138f4 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c  .rc = SQLITE_FUL
138f5 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72 5f 61  L;..goto pager_a
138f6 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20  cquire_err;.    
138f7 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f    }.      if( no
138f8 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
138f9 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f     /* Failure to
138fa 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e   set the bits in
138fb 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62   the InJournal b
138fc 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65  it-vectors is be
138fd 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a  nign..        **
138fe 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73   It merely means
138ff 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64   that we might d
13900 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72  o some extra wor
13901 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a  k to journal a .
13902 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
13903 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
13904 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c  ed to be journal
13905 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73  ed.  Nevertheles
13906 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20  s, be sure .    
13907 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74      ** to test t
13908 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20  he case where a 
13909 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63  malloc error occ
1390a 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67  urs while trying
1390b 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20   to set .       
1390c 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62   ** a bit in a b
1390d 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20  it vector..     
1390e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71     */.        sq
1390f 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
13910 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
13911 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67    if( pgno<=pPag
13912 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
13913 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54  {.          TEST
13914 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c  ONLY( rc = ) sql
13915 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50  ite3BitvecSet(pP
13916 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13917 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
13918 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d     testcase( rc=
13919 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
1391a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1391b 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20     TESTONLY( rc 
1391c 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69  = ) addToSavepoi
1391d 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72  ntBitvecs(pPager
1391e 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  , pgno);.       
1391f 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
13920 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13921 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e         sqlite3En
13922 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
13923 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
13924 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67        memset(pPg
13925 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->pData, 0, pPag
13926 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
13927 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54       }.      IOT
13928 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25  RACE(("ZERO %p %
13929 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67  d\n", pPager, pg
1392a 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  no));.    }else{
1392b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1392c 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67  Pg->pPager==pPag
1392d 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  er );.      rc =
1392e 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29   readDbPage(pPg)
1392f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
13930 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13931 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f       goto pager_
13932 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20  acquire_err;.   
13933 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65     }.    }.#ifde
13934 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
13935 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61  AGES.    pPg->pa
13936 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
13937 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65  agehash(pPg);.#e
13938 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ndif.  }..  retu
13939 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70  rn SQLITE_OK;..p
1393a 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
1393b 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  :.  assert( rc!=
1393c 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
1393d 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 71  f( pPg ){.    sq
1393e 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28  lite3PcacheDrop(
1393f 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65  pPg);.  }.  page
13940 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28  rUnlockIfUnused(
13941 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50  pPager);..  *ppP
13942 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  age = 0;.  retur
13943 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
13944 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66  cquire a page if
13945 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69   it is already i
13946 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  n the in-memory 
13947 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f  cache.  Do.** no
13948 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20  t read the page 
13949 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75  from disk.  Retu
1394a 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1394b 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20  the page,.** or 
1394c 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  0 if the page is
1394d 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41   not in cache. A
1394e 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66  lso, return 0 if
1394f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69   the .** pager i
13950 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43  s in PAGER_UNLOC
13951 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  K state when thi
13952 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
13953 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74  lled,.** or if t
13954 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61  he pager is in a
13955 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74  n error state ot
13956 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
13957 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  FULL..**.** See 
13958 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65  also sqlite3Page
13959 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66  rGet().  The dif
1395a 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
1395b 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1395c 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  and sqlite3Pager
1395d 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67  Get() is that _g
1395e 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20  et() will go to 
1395f 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61  the disk and rea
13960 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65  d.** in the page
13961 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
13962 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63  not already in c
13963 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74  ache.  This rout
13964 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e  ine.** returns N
13965 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20  ULL if the page 
13966 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20  is not in cache 
13967 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f  or if a disk I/O
13968 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65   error .** has e
13969 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f  ver happened..*/
1396a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1396b 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50  DbPage *sqlite3P
1396c 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72  agerLookup(Pager
1396d 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
1396e 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70  gno){.  PgHdr *p
1396f 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  Pg = 0;.  assert
13970 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ( pPager!=0 );. 
13971 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30   assert( pgno!=0
13972 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
13973 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 3d 30  ager->pPCache!=0
13974 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
13975 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20 50 41  ager->state > PA
13976 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20  GER_UNLOCK );.  
13977 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
13978 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
13979 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50  he, pgno, 0, &pP
1397a 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67  g);.  return pPg
1397b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
1397c 73 65 20 61 20 70 61 67 65 20 72 65 66 65 72 65  se a page refere
1397d 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  nce..**.** If th
1397e 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
1397f 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
13980 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c  ge drop to zero,
13981 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67   then the.** pag
13982 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  e is added to th
13983 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 68 65  e LRU list.  Whe
13984 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65 73  n all references
13985 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a   to all pages.**
13986 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c 20 61   are released, a
13987 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73   rollback occurs
13988 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e   and the lock on
13989 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
1398a 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a  .** removed..*/.
1398b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1398c 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
1398d 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70 50  Unref(DbPage *pP
1398e 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29 7b  g){.  if( pPg ){
1398f 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67  .    Pager *pPag
13990 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13991 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
13992 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b  cheRelease(pPg);
13993 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b  .    pagerUnlock
13994 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29  IfUnused(pPager)
13995 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
13996 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
13997 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65  al file has alre
13998 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  ady been opened,
13999 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
1399a 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  .** sub-journal 
1399b 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f  file is open too
1399c 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  . If the main jo
1399d 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65  urnal is not ope
1399e 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  n,.** this funct
1399f 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
139a0 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
139a1 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65  is returned if e
139a2 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61  verything goes a
139a3 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e  ccording to plan
139a4 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f  . .** An SQLITE_
139a5 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20  IOERR_XXX error 
139a6 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
139a7 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a   if a call to .*
139a8 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  * sqlite3OsOpen(
139a9 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  ) fails..*/.stat
139aa 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f  ic int openSubJo
139ab 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
139ac 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
139ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
139ae 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
139af 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e  >jfd) && !isOpen
139b0 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29  (pPager->sjfd) )
139b1 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  {.    if( pPager
139b2 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
139b3 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
139b4 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65  _MEMORY || pPage
139b5 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20  r->subjInMemory 
139b6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
139b7 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70  MemJournalOpen(p
139b8 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20  Pager->sjfd);.  
139b9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
139ba 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d  c = pagerOpentem
139bb 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  p(pPager, pPager
139bc 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f  ->sjfd, SQLITE_O
139bd 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b  PEN_SUBJOURNAL);
139be 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
139bf 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
139c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
139c1 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20  s called at the 
139c2 73 74 61 72 74 20 6f 66 20 65 76 65 72 79 20 77  start of every w
139c3 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
139c4 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  ..** There must 
139c5 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 45 53  already be a RES
139c6 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49  ERVED or EXCLUSI
139c7 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  VE lock on the d
139c8 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65  atabase .** file
139c9 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
139ca 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
139cb 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75  .** Open the jou
139cc 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61  rnal file for pa
139cd 67 65 72 20 70 50 61 67 65 72 20 61 6e 64 20 77  ger pPager and w
139ce 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68  rite a journal h
139cf 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  eader.** to the 
139d0 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20  start of it. If 
139d1 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65  there are active
139d2 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65   savepoints, ope
139d3 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  n the sub-journa
139d4 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68  l.** as well. Th
139d5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
139d6 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 74 68  nly used when th
139d7 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
139d8 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e  s being .** open
139d9 65 64 20 74 6f 20 77 72 69 74 65 20 61 20 72 6f  ed to write a ro
139da 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61  llback log for a
139db 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74   transaction. It
139dc 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a   is not used .**
139dd 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20   when opening a 
139de 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot journal file
139df 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b   to roll it back
139e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ..**.** If the j
139e1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61  ournal file is a
139e2 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20  lready open (as 
139e3 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63  it may be in exc
139e4 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a  lusive mode),.**
139e5 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
139e6 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20  ion just writes 
139e7 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  a journal header
139e8 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
139e9 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   the.** already 
139ea 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a  open file. .**.*
139eb 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
139ec 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
139ed 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79 20 74  e is opened by t
139ee 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  his function, th
139ef 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f  e.** Pager.pInJo
139f0 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73 74 72  urnal bitvec str
139f1 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61  ucture is alloca
139f2 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ted..**.** Retur
139f3 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  n SQLITE_OK if e
139f4 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
139f5 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69  cessful. Otherwi
139f6 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53  se, return .** S
139f7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74  QLITE_NOMEM if t
139f8 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c  he attempt to al
139f9 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70 49 6e  locate Pager.pIn
139fa 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f  Journal fails, o
139fb 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f  r .** an IO erro
139fc 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e  r code if openin
139fd 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65  g or writing the
139fe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61   journal file fa
139ff 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
13a00 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f  nt pager_open_jo
13a01 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61  urnal(Pager *pPa
13a02 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
13a03 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
13a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13a05 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
13a06 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  e */.  sqlite3_v
13a07 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20  fs * const pVfs 
13a08 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 20  = pPager->pVfs; 
13a09 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65    /* Local cache
13a0a 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65 72 20   of vfs pointer 
13a0b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  */..  assert( pP
13a0c 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13a0d 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
13a0e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13a0f 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
13a10 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13a11 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
13a12 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13a13 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OFF );.  assert(
13a14 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13a15 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f  nal==0 );.  .  /
13a16 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20  * If already in 
13a17 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c  the error state,
13a18 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13a19 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20  s a no-op.  But 
13a1a 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65  on.  ** the othe
13a1b 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75  r hand, this rou
13a1c 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
13a1d 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65 20 61  lled if we are a
13a1e 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61  lready in.  ** a
13a1f 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a  n error state. *
13a20 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
13a21 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
13a22 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
13a23 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54  errCode;..  /* T
13a24 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c  ODO: Is it reall
13a25 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65  y possible to ge
13a26 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69  t here with dbSi
13a27 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e  zeValid==0? If n
13a28 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c  ot,.  ** the cal
13a29 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f  l to PagerPageco
13a2a 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d  unt() can be rem
13a2b 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73  oved..  */.  tes
13a2c 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64  tcase( pPager->d
13a2d 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b  bSizeValid==0 );
13a2e 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  .  sqlite3PagerP
13a2f 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
13a30 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e   0);..  pPager->
13a31 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  pInJournal = sql
13a32 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
13a33 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
13a34 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
13a35 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b  pInJournal==0 ){
13a36 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
13a37 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
13a38 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75   /* Open the jou
13a39 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20  rnal file if it 
13a3a 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
13a3b 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69  pen. */.  if( !i
13a3c 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13a3d 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  d) ){.    if( pP
13a3e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
13a3f 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
13a40 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
13a41 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a       sqlite3MemJ
13a42 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65  ournalOpen(pPage
13a43 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c  r->jfd);.    }el
13a44 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  se{.      const 
13a45 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20  int flags =     
13a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13a47 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70   VFS flags to op
13a48 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  en journal file 
13a49 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54  */.        SQLIT
13a4a 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
13a4b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  |SQLITE_OPEN_CRE
13a4c 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50  ATE|.        (pP
13a4d 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f  ager->tempFile ?
13a4e 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
13a4f 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
13a50 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50  NCLOSE|SQLITE_OP
13a51 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29  EN_TEMP_JOURNAL)
13a52 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  :.          (SQL
13a53 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
13a54 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29  URNAL).        )
13a55 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
13a56 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13a57 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73  ITE.      rc = s
13a58 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65  qlite3JournalOpe
13a59 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66  n(.          pVf
13a5a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
13a5b 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
13a5c 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66  , flags, jrnlBuf
13a5d 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a  ferSize(pPager).
13a5e 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20        );.#else. 
13a5f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13a60 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
13a61 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
13a62 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61  pPager->jfd, fla
13a63 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20  gs, 0);.#endif. 
13a64 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
13a65 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
13a66 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
13a67 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20  >jfd) );.  }... 
13a68 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69   /* Write the fi
13a69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
13a6a 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  er to the journa
13a6b 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20  l file and open 
13a6c 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f  .  ** the sub-jo
13a6d 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61  urnal if necessa
13a6e 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  ry..  */.  if( r
13a6f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13a70 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65      /* TODO: Che
13a71 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  ck if all of the
13a72 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65  se are really re
13a73 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70  quired. */.    p
13a74 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
13a75 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69  e = pPager->dbSi
13a76 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ze;.    pPager->
13a77 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
13a78 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13a79 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
13a7a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
13a7b 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13a7c 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
13a7d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d      pPager->setM
13a7e 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70  aster = 0;.    p
13a7f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
13a80 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  r = 0;.    rc = 
13a81 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
13a82 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69  pPager);.  }.  i
13a83 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13a84 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76   && pPager->nSav
13a85 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63  epoint ){.    rc
13a86 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61   = openSubJourna
13a87 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  l(pPager);.  }..
13a88 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13a89 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
13a8a 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
13a8b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
13a8c 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
13a8d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
13a8e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
13a8f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  ;.}../*.** Begin
13a90 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
13a91 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63  tion on the spec
13a92 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65  ified pager obje
13a93 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69  ct. If a .** wri
13a94 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  te-transaction h
13a95 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
13a96 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e  opened, this fun
13a97 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
13a98 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
13a99 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69  xFlag argument i
13a9a 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63  s false, then ac
13a9b 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61  quire at least a
13a9c 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63   RESERVED.** loc
13a9d 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
13a9e 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61  e file. If exFla
13a9f 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
13aa0 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  acquire at least
13aa1 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45  .** an EXCLUSIVE
13aa2 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61   lock. If such a
13aa3 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79   lock is already
13aa4 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e   held, no lockin
13aa5 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  g .** functions 
13aa6 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a  need be called..
13aa7 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73  **.** If this is
13aa8 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79   not a temporary
13aa9 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69   or in-memory fi
13aaa 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72  le and, the jour
13aab 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20  nal file is .** 
13aac 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73  opened if it has
13aad 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64   not been alread
13aae 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61  y. For a tempora
13aaf 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65  ry file, the ope
13ab0 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20  ning .** of the 
13ab1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
13ab2 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74  deferred until t
13ab3 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61  here is an actua
13ab4 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72  l need to .** wr
13ab5 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ite to the journ
13ab6 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61  al. TODO: Why ha
13ab7 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66  ndle temporary f
13ab8 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79  iles differently
13ab9 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a  ?.**.** If the j
13aba 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f  ournal file is o
13abb 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20  pened (or if it 
13abc 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29  is already open)
13abd 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72  , then a.** jour
13abe 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72  nal-header is wr
13abf 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61  itten to the sta
13ac0 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  rt of it..**.** 
13ac1 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d  If the subjInMem
13ac2 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ory argument is 
13ac3 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61  non-zero, then a
13ac4 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f  ny sub-journal o
13ac5 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  pened.** within 
13ac6 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13ac7 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20   will be opened 
13ac8 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  as an in-memory 
13ac9 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61  file. This.** ha
13aca 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74  s no effect if t
13acb 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
13acc 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64  s already opened
13acd 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77   (as it may be w
13ace 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69  hen.** running i
13acf 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
13ad0 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e  ) or if the tran
13ad1 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  saction does not
13ad2 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75   require a.** su
13ad3 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68  b-journal. If th
13ad4 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61  e subjInMemory a
13ad5 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c  rgument is zero,
13ad6 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72   then any requir
13ad7 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61  ed.** sub-journa
13ad8 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  l is implemented
13ad9 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50   in-memory if pP
13ada 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65  ager is an in-me
13adb 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a  mory database, .
13adc 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65  ** or using a te
13add 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68  mporary file oth
13ade 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
13adf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13ae0 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28  lite3PagerBegin(
13ae1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
13ae2 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73  nt exFlag, int s
13ae3 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20  ubjInMemory){.  
13ae4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13ae5 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  OK;.  assert( pP
13ae6 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
13ae7 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70  ER_UNLOCK );.  p
13ae8 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d  Pager->subjInMem
13ae9 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e  ory = (u8)subjIn
13aea 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50  Memory;.  if( pP
13aeb 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
13aec 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20  ER_SHARED ){.   
13aed 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13aee 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
13aef 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d  ;.    assert( !M
13af0 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d  EMDB && !pPager-
13af1 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20  >tempFile );..  
13af2 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45    /* Obtain a RE
13af3 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
13af4 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13af5 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20  . If the exFlag 
13af6 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a  parameter.    **
13af7 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69   is true, then i
13af8 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61  mmediately upgra
13af9 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58  de this to an EX
13afa 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68  CLUSIVE lock. Th
13afb 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61  e.    ** busy-ha
13afc 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63  ndler callback c
13afd 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20  an be used when 
13afe 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65  upgrading to the
13aff 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a   EXCLUSIVE.    *
13b00 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20  * lock, but not 
13b01 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74  when obtaining t
13b02 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b  he RESERVED lock
13b03 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
13b04 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
13b05 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45  pPager->fd, RESE
13b06 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  RVED_LOCK);.    
13b07 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13b08 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
13b09 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
13b0a 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20  _RESERVED;.     
13b0b 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20   if( exFlag ){. 
13b0c 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
13b0d 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70  r_wait_on_lock(p
13b0e 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45  Pager, EXCLUSIVE
13b0f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a  _LOCK);.      }.
13b10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
13b11 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f   the required lo
13b12 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73  cks were success
13b13 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20  fully obtained, 
13b14 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  open the journal
13b15 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64  .    ** file and
13b16 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74   write the first
13b17 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
13b18 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
13b19 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13b1a 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  _OK && pPager->j
13b1b 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
13b1c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13b1d 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  F ){.      rc = 
13b1e 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
13b1f 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
13b20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73  }.  }else if( is
13b21 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
13b22 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ) && pPager->jou
13b23 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20  rnalOff==0 ){.  
13b24 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
13b25 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  s when the pager
13b26 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76   was in exclusiv
13b27 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68  e-access mode th
13b28 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69  e last.    ** ti
13b29 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72  me a (read or wr
13b2a 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e  ite) transaction
13b2b 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c   was successfull
13b2c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20  y concluded.    
13b2d 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65  ** by this conne
13b2e 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f  ction. Instead o
13b2f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a  f deleting the j
13b30 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77  ournal file it w
13b31 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20  as .    ** kept 
13b32 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20  open and either 
13b33 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f  was truncated to
13b34 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20   0 bytes or its 
13b35 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a  header was.    *
13b36 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  * overwritten wi
13b37 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f  th zeros..    */
13b38 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13b39 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a  ger->nRec==0 );.
13b3a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
13b3b 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d  er->dbOrigSize==
13b3c 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
13b3d 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13b3e 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63  nal==0 );.    rc
13b3f 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
13b40 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
13b41 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
13b42 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25  (("TRANSACTION %
13b43 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
13b44 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72  ager)));.  asser
13b45 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  t( !isOpen(pPage
13b46 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65  r->jfd) || pPage
13b47 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20  r->journalOff>0 
13b48 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  || rc!=SQLITE_OK
13b49 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
13b4a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
13b4b 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
13b4c 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20  dbModified );.  
13b4d 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20    /* Ignore any 
13b4e 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63  IO error that oc
13b4f 63 75 72 73 20 77 69 74 68 69 6e 20 70 61 67 65  curs within page
13b50 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
13b51 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20  n(). The.    ** 
13b52 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
13b53 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73 65 74  call is to reset
13b54 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74   the internal st
13b55 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72  ate of the pager
13b56 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79 73 74  .    ** sub-syst
13b57 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74 20 6d  em. It doesn't m
13b58 61 74 74 65 72 20 69 66 20 74 68 65 20 6a 6f 75  atter if the jou
13b59 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74  rnal-file is not
13b5a 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a   properly.    **
13b5b 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20 74 68   finalized at th
13b5c 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63 65 20  is point (since 
13b5d 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69  it is not a vali
13b5e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  d journal file a
13b5f 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20  nyway)..    */. 
13b60 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61     pager_end_tra
13b61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
13b62 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
13b63 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
13b64 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 61 74  ark a single dat
13b65 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61  a page as writea
13b66 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ble. The page is
13b67 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
13b68 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e  e .** main journ
13b69 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61  al or sub-journa
13b6a 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 49  l as required. I
13b6b 66 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72  f the page is wr
13b6c 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e  itten into.** on
13b6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
13b6e 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  s, the correspon
13b6f 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20  ding bit is set 
13b70 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67 65 72  in the .** Pager
13b71 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76  .pInJournal bitv
13b72 65 63 20 61 6e 64 20 74 68 65 20 50 61 67 65 72  ec and the Pager
13b73 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76  Savepoint.pInSav
13b74 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 0a 2a  epoint bitvecs.*
13b75 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20 73 61  * of any open sa
13b76 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70 70 72  vepoints as appr
13b77 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  opriate..*/.stat
13b78 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69  ic int pager_wri
13b79 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  te(PgHdr *pPg){.
13b7a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20    void *pData = 
13b7b 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61  pPg->pData;.  Pa
13b7c 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
13b7d 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74  g->pPager;.  int
13b7e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13b7f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74  ..  /* This rout
13b80 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65  ine is not calle
13b81 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73  d unless a trans
13b82 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  action has alrea
13b83 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61  dy been.  ** sta
13b84 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  rted..  */.  ass
13b85 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
13b86 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
13b87 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  ED );..  /* If a
13b88 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e  n error has been
13b89 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 65   previously dete
13b8a 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c 64 20  cted, we should 
13b8b 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c  not be.  ** call
13b8c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
13b8d 2e 20 20 52 65 70 65 61 74 20 74 68 65 20 65 72  .  Repeat the er
13b8e 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74 6e 65  ror for robustne
13b8f 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e  ss..  */.  if( N
13b90 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72  EVER(pPager->err
13b91 43 6f 64 65 29 20 29 20 20 72 65 74 75 72 6e 20  Code) )  return 
13b92 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
13b93 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65  ..  /* Higher-le
13b94 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76  vel routines nev
13b95 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  er call this fun
13b96 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73  ction if databas
13b97 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72  e is not.  ** wr
13b98 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65  itable.  But che
13b99 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20  ck anyway, just 
13b9a 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20  for robustness. 
13b9b 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
13b9c 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29  Pager->readOnly)
13b9d 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
13b9e 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74  _PERM;..  assert
13b9f 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  ( !pPager->setMa
13ba0 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b  ster );..  CHECK
13ba1 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f  _PAGE(pPg);..  /
13ba2 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20  * Mark the page 
13ba3 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68  as dirty.  If th
13ba4 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61  e page has alrea
13ba5 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a  dy been written.
13ba6 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72    ** to the jour
13ba7 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  nal then we can 
13ba8 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61  return right awa
13ba9 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  y..  */.  sqlite
13baa 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
13bab 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67  (pPg);.  if( pag
13bac 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
13bad 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73  && !subjRequires
13bae 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20  Page(pPg) ){.   
13baf 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66   pPager->dbModif
13bb0 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  ied = 1;.  }else
13bb1 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  {..    /* If we 
13bb2 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74  get this far, it
13bb3 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
13bb4 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65  page needs to be
13bb5 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20  .    ** written 
13bb6 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  to the transacti
13bb7 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68  on journal or th
13bb8 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75  e ckeckpoint jou
13bb9 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62  rnal.    ** or b
13bba 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oth..    **.    
13bbb 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 6c 20  ** Higher level 
13bbc 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20  routines should 
13bbd 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 74 61  have already sta
13bbe 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 74 69  rted a transacti
13bbf 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  on,.    ** which
13bc0 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 76 65   means they have
13bc1 20 61 63 71 75 69 72 65 64 20 74 68 65 20 6e 65   acquired the ne
13bc2 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e  cessary locks an
13bc3 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20  d opened.    ** 
13bc4 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
13bc5 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63  al.  Double-chec
13bc6 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72 65 20  k to makes sure 
13bc7 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
13bc8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
13bc9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65  = sqlite3PagerBe
13bca 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c 20 70  gin(pPager, 0, p
13bcb 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d  Pager->subjInMem
13bcc 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20 4e 45  ory);.    if( NE
13bcd 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f  VER(rc!=SQLITE_O
13bce 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  K) ){.      retu
13bcf 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
13bd0 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61   if( !isOpen(pPa
13bd1 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61  ger->jfd) && pPa
13bd2 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
13bd3 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
13bd4 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20  ODE_OFF ){.     
13bd5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13bd6 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
13bd7 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
13bd8 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61  open_journal(pPa
13bd9 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
13bda 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
13bdb 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
13bdc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d  .    pPager->dbM
13bdd 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a  odified = 1;.  .
13bde 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73      /* The trans
13bdf 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e  action journal n
13be0 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20 77 65  ow exists and we
13be1 20 68 61 76 65 20 61 20 52 45 53 45 52 56 45 44   have a RESERVED
13be2 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58   or an.    ** EX
13be3 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
13be4 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
13be5 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74  e file.  Write t
13be6 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  he current page 
13be7 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72  to.    ** the tr
13be8 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61  ansaction journa
13be9 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74  l if it is not t
13bea 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20  here already..  
13beb 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 61    */.    if( !pa
13bec 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
13bed 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
13bee 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
13bef 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d   if( pPg->pgno<=
13bf0 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
13bf1 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33  ze ){.        u3
13bf2 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20  2 cksum;.       
13bf3 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a   char *pData2;..
13bf4 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68          /* We sh
13bf5 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65  ould never write
13bf6 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
13bf7 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 74 68  file the page th
13bf8 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  at.        ** co
13bf9 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62  ntains the datab
13bfa 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20  ase locks.  The 
13bfb 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
13bfc 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20   verifies.      
13bfd 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20    ** that we do 
13bfe 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  not. */.        
13bff 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e  assert( pPg->pgn
13c00 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o!=PAGER_MJ_PGNO
13c01 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20  (pPager) );.    
13c02 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65      CODEC2(pPage
13c03 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70  r, pData, pPg->p
13c04 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53  gno, 7, return S
13c05 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61  QLITE_NOMEM, pDa
13c06 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b  ta2);.        ck
13c07 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75  sum = pager_cksu
13c08 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70  m(pPager, (u8*)p
13c09 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20  Data2);.        
13c0a 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
13c0b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
13c0c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13c0d 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
13c0e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13c0f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13c10 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13c11 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
13c12 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50  >jfd, pData2, pP
13c13 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  ager->pageSize,.
13c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
13c16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13c17 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20   + 4);.         
13c18 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13c19 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70  Off += pPager->p
13c1a 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20  ageSize+4;.     
13c1b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
13c1c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13c1d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
13c1e 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
13c1f 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72  ger->jfd, pPager
13c20 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b  ->journalOff, ck
13c21 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sum);.          
13c22 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13c23 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20  ff += 4;.       
13c24 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41   }.        IOTRA
13c25 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20  CE(("JOUT %p %d 
13c26 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  %lld %d\n", pPag
13c27 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a  er, pPg->pgno, .
13c28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c29 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13c2a 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  Off, pPager->pag
13c2b 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20  eSize));.       
13c2c 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69   PAGER_INCR(sqli
13c2d 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a  te3_pager_writej
13c2e 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20  _count);.       
13c2f 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f   PAGERTRACE(("JO
13c30 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64  URNAL %d page %d
13c31 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73   needSync=%d has
13c32 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20  h(%08x)\n",.    
13c33 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
13c34 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
13c35 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20  gno, .          
13c36 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26     ((pPg->flags&
13c37 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
13c38 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67  ?1:0), pager_pag
13c39 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20  ehash(pPg)));.. 
13c3a 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69         /* Even i
13c3b 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66  f an IO or diskf
13c3c 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72  ull error occurr
13c3d 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c  ed while journal
13c3e 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20  ling the.       
13c3f 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20   ** page in the 
13c40 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74  block above, set
13c41 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66   the need-sync f
13c42 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65  lag for the page
13c43 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68  ..        ** Oth
13c44 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65  erwise, when the
13c45 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
13c46 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65  rolled back, the
13c47 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20   logic in.      
13c48 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e    ** playback_on
13c49 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68  e_page() will th
13c4a 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67  ink that the pag
13c4b 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  e needs to be re
13c4c 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a  stored.        *
13c4d 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
13c4e 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61  e file. And if a
13c4f 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
13c50 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
13c51 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  ,.        ** the
13c52 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79  n corruption may
13c53 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20   follow..       
13c54 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
13c55 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
13c56 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67  ){.          pPg
13c57 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
13c58 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
13c59 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65        pPager->ne
13c5a 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
13c5b 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
13c5c 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  * An error has o
13c5d 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20  ccurred writing 
13c5e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
13c5f 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20  ile. The .      
13c60 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
13c61 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20   will be rolled 
13c62 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65  back by the laye
13c63 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20  r above..       
13c64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
13c65 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13c66 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
13c67 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
13c68 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
13c69 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20  >nRec++;.       
13c6a 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13c6b 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29  >pInJournal!=0 )
13c6c 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
13c6d 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
13c6e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
13c6f 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  al, pPg->pgno);.
13c70 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
13c71 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
13c72 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  EM );.        as
13c73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
13c74 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  _OK || rc==SQLIT
13c75 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
13c76 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61     rc |= addToSa
13c77 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
13c78 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
13c79 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
13c7a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13c7b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
13c7c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
13c7d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  EM );.          
13c7e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
13c7f 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
13c80 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  {.        if( !p
13c81 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74  Pager->journalSt
13c82 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72  arted && !pPager
13c83 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
13c84 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
13c85 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
13c86 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70  YNC;.          p
13c87 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
13c88 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
13c89 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
13c8a 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61  E(("APPEND %d pa
13c8b 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25  ge %d needSync=%
13c8c 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
13c8d 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
13c8e 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
13c8f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
13c90 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47   ((pPg->flags&PG
13c91 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31  HDR_NEED_SYNC)?1
13c92 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  :0)));.      }. 
13c93 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
13c94 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
13c95 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20  journal is open 
13c96 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20  and the page is 
13c97 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a  not in it,.    *
13c98 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  * then write the
13c99 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f   current page to
13c9a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   the statement j
13c9b 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68  ournal.  Note th
13c9c 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74  at.    ** the st
13c9d 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
13c9e 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66  format differs f
13c9f 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
13ca0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a   journal format.
13ca1 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69      ** in that i
13ca2 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63  t omits the chec
13ca3 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65  ksums and the he
13ca4 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ader..    */.   
13ca5 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65   if( subjRequire
13ca6 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20  sPage(pPg) ){.  
13ca7 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72      rc = subjour
13ca8 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20  nalPage(pPg);.  
13ca9 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70    }.  }..  /* Up
13caa 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73  date the databas
13cab 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72  e size and retur
13cac 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  n..  */.  assert
13cad 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
13cae 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
13caf 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64  .  if( pPager->d
13cb0 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20  bSize<pPg->pgno 
13cb1 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  ){.    pPager->d
13cb2 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e  bSize = pPg->pgn
13cb3 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  o;.  }.  return 
13cb4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72  rc;.}../*.** Mar
13cb5 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73  k a data page as
13cb6 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73   writeable. This
13cb7 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
13cb8 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a   called before .
13cb9 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  ** making change
13cba 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65  s to a page. The
13cbb 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65   caller must che
13cbc 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ck the return va
13cbd 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20  lue .** of this 
13cbe 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20  function and be 
13cbf 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63  careful not to c
13cc0 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64  hange any page d
13cc1 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74  ata unless .** t
13cc2 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
13cc3 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  rns SQLITE_OK..*
13cc4 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
13cc5 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
13cc6 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61   function and pa
13cc7 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74  ger_write() is t
13cc8 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  hat this.** func
13cc9 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20  tion also deals 
13cca 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c  with the special
13ccb 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72   case where 2 or
13ccc 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66   more pages.** f
13ccd 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64  it on a single d
13cce 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74  isk sector. In t
13ccf 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d  his case all co-
13cd0 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a  resident pages.*
13cd1 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  * must have been
13cd2 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
13cd3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66  journal file bef
13cd4 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
13cd5 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
13cd6 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f   occurs, SQLITE_
13cd7 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65  NOMEM or an IO e
13cd8 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
13cd9 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72  urned.** as appr
13cda 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69  opriate. Otherwi
13cdb 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  se, SQLITE_OK..*
13cdc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13cdd 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13cde 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  rWrite(DbPage *p
13cdf 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72  DbPage){.  int r
13ce0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
13ce1 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70    PgHdr *pPg = p
13ce2 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20  DbPage;.  Pager 
13ce3 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
13ce4 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50  Pager;.  Pgno nP
13ce5 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28  agePerSector = (
13ce6 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
13ce7 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ze/pPager->pageS
13ce8 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61  ize);..  if( nPa
13ce9 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b  gePerSector>1 ){
13cea 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43  .    Pgno nPageC
13ceb 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f  ount;          /
13cec 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
13ced 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62  f pages in datab
13cee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
13cef 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20  Pgno pg1;       
13cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
13cf1 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
13cf2 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
13cf3 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ated on. */.    
13cf4 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20  int nPage;      
13cf5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
13cf6 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61  ber of pages sta
13cf7 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20  rting at pg1 to 
13cf8 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69  journal */.    i
13cf9 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
13cfa 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
13cfb 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
13cfc 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30  int needSync = 0
13cfd 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
13cfe 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61  e if any page ha
13cff 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s PGHDR_NEED_SYN
13d00 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74  C */..    /* Set
13d01 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66   the doNotSync f
13d02 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69  lag to 1. This i
13d03 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e  s because we can
13d04 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72  not allow a jour
13d05 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65  nal.    ** heade
13d06 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  r to be written 
13d07 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65  between the page
13d08 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74  s journaled by t
13d09 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  his function..  
13d0a 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
13d0b 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61   !MEMDB );.    a
13d0c 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
13d0d 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20  oNotSync==0 );. 
13d0e 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74     pPager->doNot
13d0f 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f  Sync = 1;..    /
13d10 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73  * This trick ass
13d11 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74  umes that both t
13d12 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64  he page-size and
13d13 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65   sector-size are
13d14 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67  .    ** an integ
13d15 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49  er power of 2. I
13d16 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
13d17 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74  pg1 to the ident
13d18 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20  ifier.    ** of 
13d19 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f  the first page o
13d1a 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67  f the sector pPg
13d1b 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a   is located on..
13d1c 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d      */.    pg1 =
13d1d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20   ((pPg->pgno-1) 
13d1e 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74  & ~(nPagePerSect
13d1f 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20  or-1)) + 1;..   
13d20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
13d21 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28  ecount(pPager, (
13d22 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e  int *)&nPageCoun
13d23 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d  t);.    if( pPg-
13d24 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74  >pgno>nPageCount
13d25 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20   ){.      nPage 
13d26 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70  = (pPg->pgno - p
13d27 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65  g1)+1;.    }else
13d28 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50   if( (pg1+nPageP
13d29 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67  erSector-1)>nPag
13d2a 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20  eCount ){.      
13d2b 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75  nPage = nPageCou
13d2c 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65  nt+1-pg1;.    }e
13d2d 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  lse{.      nPage
13d2e 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f   = nPagePerSecto
13d2f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  r;.    }.    ass
13d30 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20  ert(nPage>0);.  
13d31 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50    assert(pg1<=pP
13d32 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73  g->pgno);.    as
13d33 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29  sert((pg1+nPage)
13d34 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20  >pPg->pgno);..  
13d35 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
13d36 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49  Page && rc==SQLI
13d37 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20  TE_OK; ii++){.  
13d38 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67      Pgno pg = pg
13d39 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64  1+ii;.      PgHd
13d3a 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20  r *pPage;.      
13d3b 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e  if( pg==pPg->pgn
13d3c 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74  o || !sqlite3Bit
13d3d 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e  vecTest(pPager->
13d3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20  pInJournal, pg) 
13d3f 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
13d40 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  g!=PAGER_MJ_PGNO
13d41 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
13d42 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13d43 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65  e3PagerGet(pPage
13d44 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a  r, pg, &pPage);.
13d45 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
13d46 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13d47 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
13d48 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67  pager_write(pPag
13d49 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
13d4a 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73  if( pPage->flags
13d4b 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
13d4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
13d4d 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a    needSync = 1;.
13d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73                as
13d4f 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65  sert(pPager->nee
13d50 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  dSync);.        
13d51 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
13d52 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
13d53 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ref(pPage);.    
13d54 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13d55 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
13d56 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72  ( (pPage = pager
13d57 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
13d58 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  pg))!=0 ){.     
13d59 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c     if( pPage->fl
13d5a 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
13d5b 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  YNC ){.         
13d5c 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
13d5d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
13d5e 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
13d5f 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
13d60 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
13d61 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45   If the PGHDR_NE
13d62 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20  ED_SYNC flag is 
13d63 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74  set for any of t
13d64 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a  he nPage pages .
13d65 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
13d66 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20  at pg1, then it 
13d67 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20  needs to be set 
13d68 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e  for all of them.
13d69 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20   Because.    ** 
13d6a 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f  writing to any o
13d6b 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61  f these nPage pa
13d6c 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74  ges may damage t
13d6d 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20  he others, the. 
13d6e 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69     ** journal fi
13d6f 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20  le must contain 
13d70 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20  sync()ed copies 
13d71 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20  of all of them. 
13d72 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79     ** before any
13d73 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20   of them can be 
13d74 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74  written out to t
13d75 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13d76 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13d77 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13d78 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20  & needSync ){.  
13d79 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d      assert( !MEM
13d7a 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f  DB && pPager->no
13d7b 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Sync==0 );.     
13d7c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50   for(ii=0; ii<nP
13d7d 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  age; ii++){.    
13d7e 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65      PgHdr *pPage
13d7f 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
13d80 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b  pPager, pg1+ii);
13d81 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
13d82 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
13d83 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20  pPage->flags |= 
13d84 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
13d85 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
13d86 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
13d87 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
13d88 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
13d89 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64  ert(pPager->need
13d8a 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Sync);.    }..  
13d8b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13d8c 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29  ->doNotSync==1 )
13d8d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f  ;.    pPager->do
13d8e 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d  NotSync = 0;.  }
13d8f 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70  else{.    rc = p
13d90 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61  ager_write(pDbPa
13d91 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ge);.  }.  retur
13d92 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
13d93 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
13d94 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20  e page given in 
13d95 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73  the argument was
13d96 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73   previously pass
13d97 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ed.** to sqlite3
13d98 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49  PagerWrite().  I
13d99 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72  n other words, r
13d9a 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74  eturn TRUE if it
13d9b 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61   is ok.** to cha
13d9c 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  nge the content 
13d9d 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a  of the page..*/.
13d9e 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
13d9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13da0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
13da1 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
13da2 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e   *pPg){.  return
13da3 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
13da4 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69  R_DIRTY;.}.#endi
13da5 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
13da6 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a  E_SECURE_DELETE.
13da7 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  /*.** A call to 
13da8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c  this routine tel
13da9 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61  ls the pager tha
13daa 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65  t it is not nece
13dab 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74  ssary to.** writ
13dac 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  e the informatio
13dad 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61  n on page pPg ba
13dae 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20  ck to the disk, 
13daf 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74  even though.** t
13db0 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62  hat page might b
13db1 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
13db2 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73  y.  This happens
13db3 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
13db4 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  hen.** the page 
13db5 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61  has been added a
13db6 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20  s a leaf of the 
13db7 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20  freelist and so 
13db8 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e  its.** content n
13db9 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73  o longer matters
13dba 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72  ..**.** The over
13dbb 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c  lying software l
13dbc 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  ayer calls this 
13dbd 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c  routine when all
13dbe 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20   of the data.** 
13dbf 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67  on the given pag
13dc0 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65  e is unused. The
13dc1 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65   pager marks the
13dc2 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73   page as clean s
13dc3 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65  o.** that it doe
13dc4 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65  s not get writte
13dc5 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  n to disk..**.**
13dc6 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74   Tests show that
13dc7 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
13dc8 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65  on can quadruple
13dc9 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61   the speed of la
13dca 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f  rge .** DELETE o
13dcb 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  perations..*/.SQ
13dcc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
13dcd 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
13dce 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a 70  ntWrite(PgHdr *p
13dcf 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg){.  Pager *pP
13dd0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13dd1 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e  er;.  if( (pPg->
13dd2 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
13dd3 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53  Y) && pPager->nS
13dd4 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20  avepoint==0 ){. 
13dd5 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
13dd6 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20  DONT_WRITE page 
13dd7 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67  %d of %d\n", pPg
13dd8 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28  ->pgno, PAGERID(
13dd9 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49  pPager)));.    I
13dda 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25  OTRACE(("CLEAN %
13ddb 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
13ddc 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20   pPg->pgno)).   
13ddd 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50   pPg->flags |= P
13dde 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b  GHDR_DONT_WRITE;
13ddf 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
13de0 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
13de1 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
13de2 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
13de3 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d  g);.#endif.  }.}
13de4 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
13de5 6e 65 64 28 53 51 4c 49 54 45 5f 53 45 43 55 52  ned(SQLITE_SECUR
13de6 45 5f 44 45 4c 45 54 45 29 20 2a 2f 0a 0a 2f 2a  E_DELETE) */../*
13de7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
13de8 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e   is called to in
13de9 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75  crement the valu
13dea 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
13deb 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67  e file .** chang
13dec 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65  e-counter, store
13ded 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69  d as a 4-byte bi
13dee 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
13def 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a   starting at .**
13df0 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20   byte offset 24 
13df1 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  of the pager fil
13df2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
13df3 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61  isDirectMode fla
13df4 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  g is zero, then 
13df5 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20  this is done by 
13df6 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69  calling .** sqli
13df7 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
13df8 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20  on page 1, then 
13df9 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f  modifying the co
13dfa 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a  ntents of the.**
13dfb 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74   page data. In t
13dfc 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c  his case the fil
13dfd 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65  e will be update
13dfe 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65  d when the curre
13dff 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
13e00 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  n is committed..
13e01 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65  **.** The isDire
13e02 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20  ctMode flag may 
13e03 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f  only be non-zero
13e04 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
13e05 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20  was compiled.** 
13e06 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
13e07 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13e08 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ITE macro define
13e09 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  d. In this case,
13e0a 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20  .** if isDirect 
13e0b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
13e0c 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13e0d 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64  ile is updated d
13e0e 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72  irectly.** by wr
13e0f 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64  iting an updated
13e10 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65   version of page
13e11 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20   1 using a call 
13e12 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  to the .** sqlit
13e13 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63  e3OsWrite() func
13e14 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
13e15 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  int pager_incr_c
13e16 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67  hangecounter(Pag
13e17 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
13e18 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20  isDirectMode){. 
13e19 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13e1a 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61  _OK;..  /* Decla
13e1b 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  re and initializ
13e1c 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67  e constant integ
13e1d 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49  er 'isDirect'. I
13e1e 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69  f the.  ** atomi
13e1f 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
13e20 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20  tion is enabled 
13e21 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74  in this build, t
13e22 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a  hen isDirect.  *
13e23 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  * is initialized
13e24 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61   to the value pa
13e25 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69  ssed as the isDi
13e26 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74  rectMode paramet
13e27 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20  er.  ** to this 
13e28 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  function. Otherw
13e29 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79  ise, it is alway
13e2a 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20  s set to zero.. 
13e2b 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65   **.  ** The ide
13e2c 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65  a is that if the
13e2d 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
13e2e 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f  timization is no
13e2f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61  t.  ** enabled a
13e30 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20  t compile time, 
13e31 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e  the compiler can
13e32 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20   omit the tests 
13e33 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63  of.  ** 'isDirec
13e34 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c  t' below, as wel
13e35 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65  l as the block e
13e36 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20  nclosed in the. 
13e37 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63   ** "if( isDirec
13e38 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a  t )" condition..
13e39 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
13e3a 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
13e3b 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65  C_WRITE.# define
13e3c 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20   DIRECT_MODE 0. 
13e3d 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63   assert( isDirec
13e3e 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e  tMode==0 );.  UN
13e3f 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
13e40 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65  sDirectMode);.#e
13e41 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52  lse.# define DIR
13e42 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63  ECT_MODE isDirec
13e43 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20  tMode.#endif..  
13e44 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13e45 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
13e46 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21  ERVED );.  if( !
13e47 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
13e48 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65  untDone && pPage
13e49 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20  r->dbSize>0 ){. 
13e4a 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72     PgHdr *pPgHdr
13e4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13e4c 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f   /* Reference to
13e4d 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75   page 1 */.    u
13e4e 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65  32 change_counte
13e4f 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
13e50 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66  Initial value of
13e51 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
13e52 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73  field */..    as
13e53 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74  sert( !pPager->t
13e54 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65  empFile && isOpe
13e55 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b  n(pPager->fd) );
13e56 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61  ..    /* Open pa
13e57 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65  ge 1 of the file
13e58 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f   for writing. */
13e59 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13e5a 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
13e5b 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20  , 1, &pPgHdr);. 
13e5c 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64     assert( pPgHd
13e5d 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  r==0 || rc==SQLI
13e5e 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a  TE_OK );..    /*
13e5f 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73   If page one was
13e60 20 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73   fetched success
13e61 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20  fully, and this 
13e62 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a  function is not.
13e63 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67      ** operating
13e64 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c   in direct-mode,
13e65 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69   make page 1 wri
13e66 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74  table.  When not
13e67 20 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65   in .    ** dire
13e68 63 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20  ct mode, page 1 
13e69 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69  is always held i
13e6a 6e 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63  n cache and henc
13e6b 65 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29  e the PagerGet()
13e6c 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73  .    ** above is
13e6d 20 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66   always successf
13e6e 75 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41  ul - hence the A
13e6f 4c 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c  LWAYS on rc==SQL
13e70 49 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20  ITE_OK..    */. 
13e71 20 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d     if( !DIRECT_M
13e72 4f 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63  ODE && ALWAYS(rc
13e73 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a  ==SQLITE_OK) ){.
13e74 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
13e75 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
13e76 48 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Hdr);.    }..   
13e77 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13e78 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  OK ){.      /* I
13e79 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c  ncrement the val
13e7a 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64  ue just read and
13e7b 20 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 74   write it back t
13e7c 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20  o byte 24. */.  
13e7d 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74      change_count
13e7e 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34  er = sqlite3Get4
13e7f 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72  byte((u8*)pPager
13e80 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20  ->dbFileVers);. 
13e81 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e       change_coun
13e82 74 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74  ter++;.      put
13e83 33 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70  32bits(((char*)p
13e84 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34  PgHdr->pData)+24
13e85 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  , change_counter
13e86 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
13e87 72 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63  running in direc
13e88 74 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68  t mode, write th
13e89 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61  e contents of pa
13e8a 67 65 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65  ge 1 to the file
13e8b 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44  . */.      if( D
13e8c 49 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20  IRECT_MODE ){.  
13e8d 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64        const void
13e8e 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d   *zBuf = pPgHdr-
13e8f 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20  >pData;.        
13e90 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13e91 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a  dbFileSize>0 );.
13e92 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
13e93 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
13e94 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50  er->fd, zBuf, pP
13e95 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
13e96 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
13e97 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13e98 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
13e99 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
13e9a 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ne = 1;.        
13e9b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
13e9c 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63         pPager->c
13e9d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
13e9e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
13e9f 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73  }..    /* Releas
13ea0 65 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72  e the page refer
13ea1 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ence. */.    sql
13ea2 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
13ea3 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65  PgHdr);.  }.  re
13ea4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
13ea5 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72  * Sync the pager
13ea6 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54   file to disk. T
13ea7 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66  his is a no-op f
13ea8 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c  or in-memory fil
13ea9 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77  es.** or pages w
13eaa 69 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f  ith the Pager.no
13eab 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a  Sync flag set..*
13eac 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
13ead 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e  ul, or called on
13eae 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69   a pager for whi
13eaf 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70  ch it is a no-op
13eb0 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  , this.** functi
13eb1 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
13eb2 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
13eb3 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
13eb4 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
13eb5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13eb6 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13eb7 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61  rSync(Pager *pPa
13eb8 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  ger){.  int rc; 
13eb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13eba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13ebb 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
13ebc 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
13ebd 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
13ebe 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72  >noSync ){.    r
13ebf 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
13ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
13ec1 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
13ec2 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65  Pager->fd, pPage
13ec3 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a  r->sync_flags);.
13ec4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13ec5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
13ec6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13ec7 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70   for the pager p
13ec8 50 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70  Pager. zMaster p
13ec9 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d  oints to the nam
13eca 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72  e.** of a master
13ecb 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
13ecc 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  at should be wri
13ecd 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e  tten into the in
13ece 64 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72  dividual.** jour
13ecf 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65  nal file. zMaste
13ed0 72 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77  r may be NULL, w
13ed1 68 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65  hich is interpre
13ed2 74 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72  ted as no master
13ed3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73  .** journal (a s
13ed4 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74  ingle database t
13ed5 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a  ransaction)..**.
13ed6 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
13ed7 65 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a  ensures that:.**
13ed8 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61  .**   * The data
13ed9 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
13eda 2d 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61  -counter is upda
13edb 74 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20  ted,.**   * the 
13edc 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65  journal is synce
13edd 64 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74  d (unless the at
13ede 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
13edf 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29  ization is used)
13ee0 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72  ,.**   * all dir
13ee1 74 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69  ty pages are wri
13ee2 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
13ee3 62 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20  base file, .**  
13ee4 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20   * the database 
13ee5 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65  file is truncate
13ee6 64 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c  d (if required),
13ee7 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20   and.**   * the 
13ee8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79  database file sy
13ee9 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  nced. .**.** The
13eea 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74   only thing that
13eeb 20 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d   remains to comm
13eec 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  it the transacti
13eed 6f 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a  on is to finaliz
13eee 65 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74  e .** (delete, t
13eef 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20  runcate or zero 
13ef0 74 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f  the first part o
13ef1 66 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  f) the journal f
13ef2 69 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65  ile (or .** dele
13ef3 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
13ef4 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70  urnal file if sp
13ef5 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20  ecified)..**.** 
13ef6 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61  Note that if zMa
13ef7 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73  ster==NULL, this
13ef8 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72   does not overwr
13ef9 69 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76  ite a previous v
13efa 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74  alue.** passed t
13efb 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65  o an sqlite3Page
13efc 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
13efd 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  ) call..**.** If
13efe 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d   the final param
13eff 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20  eter - noSync - 
13f00 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
13f01 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13f02 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74  itself.** is not
13f03 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c   synced. The cal
13f04 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71  ler must call sq
13f05 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29  lite3PagerSync()
13f06 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20   directly to.** 
13f07 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73  sync the databas
13f08 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61  e file before ca
13f09 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73  lling CommitPhas
13f0a 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65  eTwo() to delete
13f0b 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
13f0c 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73  file in this cas
13f0d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
13f0e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13f0f 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
13f10 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50  One(.  Pager *pP
13f11 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  ager,           
13f12 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20         /* Pager 
13f13 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
13f14 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c  t char *zMaster,
13f15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
13f16 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20  f not NULL, the 
13f17 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
13f18 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53  ame */.  int noS
13f19 79 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20  ync             
13f1a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
13f1b 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79   to omit the xSy
13f1c 6e 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c  nc on the db fil
13f1d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
13f1e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
13f1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
13f20 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f  urn code */..  /
13f21 2a 20 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65  * The dbOrigSize
13f22 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 69 66   is never set if
13f23 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46   journal_mode=OF
13f24 46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  F */.  assert( p
13f25 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
13f26 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
13f27 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 50 61  LMODE_OFF || pPa
13f28 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d  ger->dbOrigSize=
13f29 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  =0 );..  /* If a
13f2a 20 70 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63   prior error occ
13f2b 75 72 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74  urred, this rout
13f2c 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ine should not b
13f2d 65 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42  e called.  ROLLB
13f2e 41 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 65 20  ACK.  ** is the 
13f2f 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 70  appropriate resp
13f30 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72  onse to an error
13f31 2c 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 20 47  , not COMMIT.  G
13f32 75 61 72 64 20 61 67 61 69 6e 73 74 0a 20 20 2a  uard against.  *
13f33 2a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 73 20  * coding errors 
13f34 62 79 20 72 65 70 65 61 74 69 6e 67 20 74 68 65  by repeating the
13f35 20 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 2a 2f   prior error. */
13f36 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61  .  if( NEVER(pPa
13f37 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20  ger->errCode) ) 
13f38 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65  return pPager->e
13f39 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52  rrCode;..  PAGER
13f3a 54 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45  TRACE(("DATABASE
13f3b 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a   SYNC: File=%s z
13f3c 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d  Master=%s nSize=
13f3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50  %d\n", .      pP
13f3e 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
13f3f 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
13f40 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 69  ->dbSize));..  i
13f41 66 28 20 4d 45 4d 44 42 20 26 26 20 70 50 61 67  f( MEMDB && pPag
13f42 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29  er->dbModified )
13f43 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
13f44 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79   is an in-memory
13f45 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73   db, or no pages
13f46 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74   have been writt
13f47 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20  en to, or this. 
13f48 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68     ** function h
13f49 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
13f4a 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 6d 6f  called, it is mo
13f4b 73 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 20 48  stly a no-op.  H
13f4c 6f 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 20 20  owever, any.    
13f4d 2a 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 72 6f  ** backup in pro
13f4e 67 72 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62  gress needs to b
13f4f 65 20 72 65 73 74 61 72 74 65 64 2e 0a 20 20 20  e restarted..   
13f50 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
13f51 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61  ackupRestart(pPa
13f52 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20  ger->pBackup);. 
13f53 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65   }else if( pPage
13f54 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
13f55 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 72  SYNCED && pPager
13f56 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a  ->dbModified ){.
13f57 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
13f58 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61  owing block upda
13f59 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63  tes the change-c
13f5a 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 20  ounter. Exactly 
13f5b 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 6f  how it.    ** do
13f5c 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20  es this depends 
13f5d 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
13f5e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64  t the atomic-upd
13f5f 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ate optimization
13f60 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 62  .    ** was enab
13f61 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  led at compile t
13f62 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 73  ime, and if this
13f63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 65   transaction mee
13f64 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72  ts the .    ** r
13f65 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 20  untime criteria 
13f66 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 61  to use the opera
13f67 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 20  tion: .    **.  
13f68 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69    **    * The fi
13f69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le-system suppor
13f6a 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  ts the atomic-wr
13f6b 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72  ite property for
13f6c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f  .    **      blo
13f6d 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65  cks of size page
13f6e 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20  -size, and .    
13f6f 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d  **    * This com
13f70 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20  mit is not part 
13f71 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20  of a multi-file 
13f72 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
13f73 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 61  .    **    * Exa
13f74 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61  ctly one page ha
13f75 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
13f76 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65  and store in the
13f77 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
13f78 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
13f79 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
13f7a 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64   was not enabled
13f7b 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
13f7c 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a  , then the.    *
13f7d 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  * pager_incr_cha
13f7e 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e  ngecounter() fun
13f7f 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
13f80 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68  to update the ch
13f81 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e  ange.    ** coun
13f82 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 74  ter in 'indirect
13f83 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 6f  -mode'. If the o
13f84 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63  ptimization is c
13f85 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a 20  ompiled in but. 
13f86 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70     ** is not app
13f87 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20  licable to this 
13f88 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c  transaction, cal
13f89 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  l sqlite3Journal
13f8a 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a 20  Create().    ** 
13f8b 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
13f8c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
13f8d 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20  s actually been 
13f8e 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61  created, then ca
13f8f 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f  ll.    ** pager_
13f90 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13f91 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74  er() to update t
13f92 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
13f93 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20  r in indirect.  
13f94 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 20    ** mode. .    
13f95 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77  **.    ** Otherw
13f96 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 69  ise, if the opti
13f97 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 68  mization is both
13f98 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 70   enabled and app
13f99 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20  licable,.    ** 
13f9a 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 5f  then call pager_
13f9b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13f9c 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74  er() to update t
13f9d 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
13f9e 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 72  r.    ** in 'dir
13f9f 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 68  ect' mode. In th
13fa0 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72  is case the jour
13fa1 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65  nal file will ne
13fa2 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 72  ver be.    ** cr
13fa3 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74  eated for this t
13fa4 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20  ransaction..    
13fa5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
13fa6 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57  _ENABLE_ATOMIC_W
13fa7 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 2a  RITE.    PgHdr *
13fa8 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pPg;.    assert(
13fa9 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
13faa 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e  jfd) || pPager->
13fab 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
13fac 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
13fad 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  FF );.    if( !z
13fae 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 6e  Master && isOpen
13faf 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a 20  (pPager->jfd) . 
13fb0 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a      && pPager->j
13fb1 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42  ournalOff==jrnlB
13fb2 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72  ufferSize(pPager
13fb3 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65  ) .     && pPage
13fb4 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 65  r->dbSize>=pPage
13fb5 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 20  r->dbFileSize.  
13fb6 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d     && (0==(pPg =
13fb7 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69   sqlite3PcacheDi
13fb8 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e  rtyList(pPager->
13fb9 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d  pPCache)) || 0==
13fba 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20  pPg->pDirty).   
13fbb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64   ){.      /* Upd
13fbc 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20  ate the db file 
13fbd 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 76  change counter v
13fbe 69 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 72  ia the direct-wr
13fbf 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20  ite method. The 
13fc0 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  .      ** follow
13fc1 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f  ing call will mo
13fc2 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  dify the in-memo
13fc3 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
13fc4 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20  n of page 1 .   
13fc5 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65     ** to include
13fc6 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61   the updated cha
13fc7 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20  nge counter and 
13fc8 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20  then write page 
13fc9 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 65  1 .      ** dire
13fca 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61  ctly to the data
13fcb 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 75  base file. Becau
13fcc 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63  se of the atomic
13fcd 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a 2a  -write .      **
13fce 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65   property of the
13fcf 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65   host file-syste
13fd0 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 2e  m, this is safe.
13fd1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
13fd2 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
13fd3 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
13fd4 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d 65  ager, 1);.    }e
13fd5 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
13fd6 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72  sqlite3JournalCr
13fd7 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  eate(pPager->jfd
13fd8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
13fd9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13fda 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
13fdb 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
13fdc 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ter(pPager, 0);.
13fdd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
13fde 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 67  lse.    rc = pag
13fdf 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
13fe0 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29  unter(pPager, 0)
13fe1 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
13fe2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13fe3 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61   goto commit_pha
13fe4 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20  se_one_exit;..  
13fe5 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61    /* If this tra
13fe6 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64  nsaction has mad
13fe7 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
13fe8 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c  maller, then all
13fe9 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 65   pages.    ** be
13fea 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 79  ing discarded by
13feb 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20   the truncation 
13fec 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e 20  must be written 
13fed 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  to the journal. 
13fee 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 73     ** file. This
13fef 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
13ff0 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
13ff1 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  mode..    **.   
13ff2 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69   ** Before readi
13ff3 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 69 74  ng the pages wit
13ff4 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c  h page numbers l
13ff5 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a  arger than the .
13ff6 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76      ** current v
13ff7 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62  alue of Pager.db
13ff8 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65  Size, set dbSize
13ff9 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c   back to the val
13ffa 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69  ue.    ** that i
13ffb 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 74  t took at the st
13ffc 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73  art of the trans
13ffd 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73  action. Otherwis
13ffe 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61  e, the.    ** ca
13fff 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61  lls to sqlite3Pa
14000 67 65 72 47 65 74 28 29 20 72 65 74 75 72 6e 20  gerGet() return 
14001 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e 73  zeroed pages ins
14002 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a 20  tead of .    ** 
14003 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 6f  reading data fro
14004 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
14005 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
14006 2a 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f  ** When journal_
14007 6d 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 64 62  mode==OFF the db
14008 4f 72 69 67 53 69 7a 65 20 69 73 20 61 6c 77 61  OrigSize is alwa
14009 79 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 69 73  ys zero, so this
1400a 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65  .    ** block ne
1400b 76 65 72 20 72 75 6e 73 20 69 66 20 6a 6f 75 72  ver runs if jour
1400c 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20  nal_mode=OFF..  
1400d 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
1400e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1400f 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67  UUM.    if( pPag
14010 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65  er->dbSize<pPage
14011 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20  r->dbOrigSize . 
14012 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 50      && ALWAYS(pP
14013 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
14014 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
14015 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 29 7b  MODE_OFF).    ){
14016 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20  .      Pgno i;  
14017 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14018 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14019 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
1401a 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63  iable */.      c
1401b 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20  onst Pgno iSkip 
1401c 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  = PAGER_MJ_PGNO(
1401d 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64  pPager); /* Pend
1401e 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f  ing lock page */
1401f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e  .      const Pgn
14020 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65  o dbSize = pPage
14021 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20  r->dbSize;      
14022 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61   /* Database ima
14023 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20  ge size */ .    
14024 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
14025 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69   = pPager->dbOri
14026 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72  gSize;.      for
14027 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c  ( i=dbSize+1; i<
14028 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  =pPager->dbOrigS
14029 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20  ize; i++ ){.    
1402a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
1402b 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65  BitvecTest(pPage
1402c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69  r->pInJournal, i
1402d 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b  ) && i!=iSkip ){
1402e 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72  .          PgHdr
1402f 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
14030 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20       /* Page to 
14031 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20  journal */.     
14032 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14033 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
14034 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20  , i, &pPage);.  
14035 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
14036 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
14037 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
14038 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20  e_exit;.        
14039 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1403a 67 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b  gerWrite(pPage);
1403b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1403c 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
1403d 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
1403e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1403f 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70   ) goto commit_p
14040 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20  hase_one_exit;. 
14041 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
14042 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e   .      pPager->
14043 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b  dbSize = dbSize;
14044 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
14045 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
14046 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
14047 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ame into the jou
14048 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20  rnal file. If a 
14049 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a  master .    ** j
1404a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1404b 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1404c 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
1404d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a   journal file, .
1404e 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61      ** or if zMa
1404f 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f  ster is NULL (no
14050 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29   master journal)
14051 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c  , then this call
14052 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20   is a no-op..   
14053 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69   */.    rc = wri
14054 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  teMasterJournal(
14055 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29  pPager, zMaster)
14056 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
14057 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
14058 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
14059 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79  exit;..    /* Sy
1405a 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
1405b 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d  ile. If the atom
1405c 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69  ic-update optimi
1405d 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a  zation is being.
1405e 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69      ** used, thi
1405f 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20  s call will not 
14060 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e  create the journ
14061 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f  al file or perfo
14062 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65  rm any.    ** re
14063 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20  al IO..    */.  
14064 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
14065 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
14066 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
14067 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
14068 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
14069 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c  .    /* Write al
1406a 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f  l dirty pages to
1406b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1406c 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  le. */.    rc = 
1406d 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
1406e 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63  list(sqlite3Pcac
1406f 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67  heDirtyList(pPag
14070 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20  er->pPCache));. 
14071 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
14072 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
14073 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
14074 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29  _IOERR_BLOCKED )
14075 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d  ;.      goto com
14076 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
14077 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  it;.    }.    sq
14078 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
14079 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
1407a 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  che);..    /* If
1407b 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
1407c 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d  k is not the sam
1407d 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61  e size as the da
1407e 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20  tabase image,.  
1407f 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61    ** then use pa
14080 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20  ger_truncate to 
14081 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74  grow or shrink t
14082 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20  he file here..  
14083 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
14084 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61  ger->dbSize!=pPa
14085 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
14086 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e  ){.      Pgno nN
14087 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ew = pPager->dbS
14088 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64  ize - (pPager->d
14089 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f  bSize==PAGER_MJ_
1408a 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20  PGNO(pPager));. 
1408b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1408c 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
1408d 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20  R_EXCLUSIVE );. 
1408e 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
1408f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c  truncate(pPager,
14090 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66   nNew);.      if
14091 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
14092 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
14093 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20  ase_one_exit;.  
14094 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61    }..    /* Fina
14095 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61  lly, sync the da
14096 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a  tabase file. */.
14097 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d      if( !pPager-
14098 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79  >noSync && !noSy
14099 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  nc ){.      rc =
1409a 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
1409b 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65  Pager->fd, pPage
1409c 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a  r->sync_flags);.
1409d 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43      }.    IOTRAC
1409e 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22  E(("DBSYNC %p\n"
1409f 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20  , pPager))..    
140a0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
140a1 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20  PAGER_SYNCED;.  
140a2 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  }..commit_phase_
140a3 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 74 75  one_exit:.  retu
140a4 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
140a5 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   When this funct
140a6 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ion is called, t
140a7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
140a8 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65   has been comple
140a9 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20  tely.** updated 
140aa 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20 63  to reflect the c
140ab 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
140ac 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
140ad 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79  action and.** sy
140ae 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68  nced to disk. Th
140af 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  e journal file s
140b0 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74  till exists in t
140b1 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a  he file-system .
140b2 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69  ** though, and i
140b3 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75  f a failure occu
140b4 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  rs at this point
140b5 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61   it will eventua
140b6 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61  lly.** be used a
140b7 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  s a hot-journal 
140b8 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
140b9 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c  transaction roll
140ba 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  ed back..**.** T
140bb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e  his function fin
140bc 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e  alizes the journ
140bd 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20  al file, either 
140be 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a  by deleting, .**
140bf 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70   truncating or p
140c0 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67  artially zeroing
140c1 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20   it, so that it 
140c2 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a  cannot be used .
140c3 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e  ** for hot-journ
140c4 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63  al rollback. Onc
140c5 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74  e this is done t
140c6 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
140c7 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79  s.** irrevocably
140c8 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a   committed..**.*
140c9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
140ca 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f  curs, an IO erro
140cb 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
140cc 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 72  ed and the pager
140cd 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74  .** moves into t
140ce 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20  he error state. 
140cf 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54  Otherwise, SQLIT
140d0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
140d1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
140d2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
140d3 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54  agerCommitPhaseT
140d4 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  wo(Pager *pPager
140d5 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
140d6 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
140d7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
140d8 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f  urn code */..  /
140d9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
140da 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c  hould not be cal
140db 6c 65 64 20 69 66 20 61 20 70 72 69 6f 72 20 65  led if a prior e
140dc 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
140dd 64 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28  d..  ** But if (
140de 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20  due to a coding 
140df 65 72 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20  error elsewhere 
140e0 69 6e 20 74 68 65 20 73 79 73 74 65 6d 29 20 69  in the system) i
140e1 74 20 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20  t does get.  ** 
140e2 63 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74  called, just ret
140e3 75 72 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72  urn the same err
140e4 6f 72 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20  or code without 
140e5 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20  doing anything. 
140e6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
140e7 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20  Pager->errCode) 
140e8 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d  ) return pPager-
140e9 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20  >errCode;..  /* 
140ea 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
140eb 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
140ec 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20  ed if the pager 
140ed 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 61  is not in at lea
140ee 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 45  st.  ** PAGER_RE
140ef 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 6e  SERVED state. An
140f0 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 20  d indeed SQLite 
140f1 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 2e  never does this.
140f2 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a 20   But it is.  ** 
140f3 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 69  nice to have thi
140f4 73 20 64 65 66 65 6e 73 69 76 65 20 74 65 73 74  s defensive test
140f5 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a 20 20   here anyway..  
140f6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
140f7 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47  Pager->state<PAG
140f8 45 52 5f 52 45 53 45 52 56 45 44 29 20 29 20 72  ER_RESERVED) ) r
140f9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
140fa 4f 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74  OR;..  /* An opt
140fb 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68  imization. If th
140fc 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6e  e database was n
140fd 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69  ot actually modi
140fe 66 69 65 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a  fied during.  **
140ff 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
14100 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69 73 20  n, the pager is 
14101 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
14102 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73  sive-mode and is
14103 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 72 73  .  ** using pers
14104 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c  istent journals,
14105 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
14106 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
14107 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74    **.  ** The st
14108 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  art of the journ
14109 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c  al file currentl
1410a 79 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  y contains a sin
1410b 67 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a  gle journal .  *
1410c 2a 20 68 65 61 64 65 72 20 77 69 74 68 20 74 68  * header with th
1410d 65 20 6e 52 65 63 20 66 69 65 6c 64 20 73 65 74  e nRec field set
1410e 20 74 6f 20 30 2e 20 49 66 20 73 75 63 68 20 61   to 0. If such a
1410f 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64   journal is used
14110 20 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a   as.  ** a hot-j
14111 6f 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f  ournal during ho
14112 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
14113 63 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 77 69  ck, 0 changes wi
14114 6c 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20  ll be made.  ** 
14115 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
14116 66 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 20 69  file. So there i
14117 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72  s no need to zer
14118 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20  o the journal . 
14119 20 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 6e 63   ** header. Sinc
1411a 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  e the pager is i
1411b 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
1411c 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  , there is no ne
1411d 65 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20  ed.  ** to drop 
1411e 61 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 65 72  any locks either
1411f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
14120 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d  ger->dbModified=
14121 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78  =0 && pPager->ex
14122 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20  clusiveMode .   
14123 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
14124 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
14125 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
14126 54 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65 72  T.  ){.    asser
14127 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
14128 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48  alOff==JOURNAL_H
14129 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b  DR_SZ(pPager) );
1412a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1412b 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41  TE_OK;.  }..  PA
1412c 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d 49  GERTRACE(("COMMI
1412d 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  T %d\n", PAGERID
1412e 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73  (pPager)));.  as
1412f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
14130 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45  ate==PAGER_SYNCE
14131 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70  D || MEMDB || !p
14132 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
14133 64 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65  d );.  rc = page
14134 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
14135 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  n(pPager, pPager
14136 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20  ->setMaster);.  
14137 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72  return pager_err
14138 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
14139 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63  }../*.** Rollbac
1413a 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54  k all changes. T
1413b 68 65 20 64 61 74 61 62 61 73 65 20 66 61 6c 6c  he database fall
1413c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f  s back to PAGER_
1413d 53 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a  SHARED mode..**.
1413e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1413f 20 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 74 61   performs two ta
14140 73 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  sks:.**.**   1) 
14141 49 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68  It rolls back th
14142 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  e journal file, 
14143 72 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61  restoring all da
14144 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
14145 0a 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f  .**      in-memo
14146 72 79 20 63 61 63 68 65 20 70 61 67 65 73 20 74  ry cache pages t
14147 6f 20 74 68 65 20 73 74 61 74 65 20 74 68 65 79  o the state they
14148 20 77 65 72 65 20 69 6e 20 77 68 65 6e 20 74 68   were in when th
14149 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
1414a 20 20 20 20 20 20 77 61 73 20 6f 70 65 6e 65 64        was opened
1414b 2c 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74  , and.**   2) It
1414c 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a   finalizes the j
1414d 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20  ournal file, so 
1414e 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 75  that it is not u
1414f 73 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20  sed for hot.**  
14150 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20      rollback at 
14151 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  any point in the
14152 20 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73   future..**.** s
14153 75 62 6a 65 63 74 20 74 6f 20 74 68 65 20 66 6f  ubject to the fo
14154 6c 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 69 63  llowing qualific
14155 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20  ations:.**.** * 
14156 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  If the journal f
14157 69 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 20 6f  ile is not yet o
14158 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  pen when this fu
14159 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1415a 2c 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79  ,.**   then only
1415b 20 28 32 29 20 69 73 20 70 65 72 66 6f 72 6d 65   (2) is performe
1415c 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
1415d 74 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72  there is no jour
1415e 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f  nal file.**   to
1415f 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a   roll back..**.*
14160 2a 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 72 72  * * If in an err
14161 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74  or state other t
14162 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c  han SQLITE_FULL,
14163 20 74 68 65 6e 20 74 61 73 6b 20 28 31 29 20 69   then task (1) i
14164 73 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65  s .**   performe
14165 64 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  d. If successful
14166 2c 20 74 61 73 6b 20 28 32 29 2e 20 52 65 67 61  , task (2). Rega
14167 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75  rdless of the ou
14168 74 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69  tcome.**   of ei
14169 74 68 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20  ther, the error 
1416a 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65  state error code
1416b 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
1416c 74 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20  the caller.**   
1416d 28 69 2e 65 2e 20 65 69 74 68 65 72 20 53 51 4c  (i.e. either SQL
1416e 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c  ITE_IOERR or SQL
1416f 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a  ITE_CORRUPT)..**
14170 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 61 67  .** * If the pag
14171 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 52  er is in PAGER_R
14172 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74  ESERVED state, t
14173 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e  hen attempt (1).
14174 20 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72   Whether.**   or
14175 20 6e 6f 74 20 28 31 29 20 69 73 20 73 75 63 63   not (1) is succ
14176 75 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74  ussful, also att
14177 65 6d 70 74 20 28 32 29 2e 20 49 66 20 73 75 63  empt (2). If suc
14178 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a  cessful, return.
14179 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  **   SQLITE_OK. 
1417a 4f 74 68 65 72 77 69 73 65 2c 20 65 6e 74 65 72  Otherwise, enter
1417b 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
1417c 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
1417d 66 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f  first .**   erro
1417e 72 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72  r code encounter
1417f 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20  ed. .**.**   In 
14180 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20  this case there 
14181 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61  is no chance tha
14182 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  t the database w
14183 61 73 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a  as written to. .
14184 2a 2a 20 20 20 53 6f 20 69 73 20 73 61 66 65 20  **   So is safe 
14185 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20  to finalize the 
14186 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 76 65  journal file eve
14187 6e 20 69 66 20 74 68 65 20 70 6c 61 79 62 61 63  n if the playbac
14188 6b 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 74 69  k .**   (operati
14189 6f 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 48 6f  on 1) failed. Ho
1418a 77 65 76 65 72 20 74 68 65 20 70 61 67 65 72 20  wever the pager 
1418b 6d 75 73 74 20 65 6e 74 65 72 20 74 68 65 20 65  must enter the e
1418c 72 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 20 20  rror state.**   
1418d 61 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  as the contents 
1418e 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  of the in-memory
1418f 20 63 61 63 68 65 20 61 72 65 20 6e 6f 77 20 73   cache are now s
14190 75 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20  uspect..**.** * 
14191 46 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50  Finally, if in P
14192 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 73  AGER_EXCLUSIVE s
14193 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d  tate, then attem
14194 70 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20  pt (1). Only.** 
14195 20 20 61 74 74 65 6d 70 74 20 28 32 29 20 69 66    attempt (2) if
14196 20 28 31 29 20 69 73 20 73 75 63 63 65 73 73 66   (1) is successf
14197 75 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  ul. Return SQLIT
14198 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66  E_OK if successf
14199 75 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69  ul,.**   otherwi
1419a 73 65 20 65 6e 74 65 72 20 74 68 65 20 65 72 72  se enter the err
1419b 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74  or state and ret
1419c 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
1419d 64 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20  de from the .** 
1419e 20 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 61 74    failing operat
1419f 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20  ion..**.**   In 
141a0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 64 61  this case the da
141a1 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20  tabase file may 
141a2 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
141a3 6e 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a  n to. So if the.
141a4 2a 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 6f 70  **   playback op
141a5 65 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20  eration did not 
141a6 73 75 63 63 65 65 64 20 69 74 20 77 6f 75 6c 64  succeed it would
141a7 20 6e 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20   not be safe to 
141a8 66 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68  finalize.**   th
141a9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
141aa 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6c  It needs to be l
141ab 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d  eft in the file-
141ac 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a  system so that.*
141ad 2a 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  *   some other p
141ae 72 6f 63 65 73 73 20 63 61 6e 20 75 73 65 20 69  rocess can use i
141af 74 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65  t to restore the
141b0 20 64 61 74 61 62 61 73 65 20 73 74 61 74 65 20   database state 
141b1 28 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75  (by.**   hot-jou
141b2 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a  rnal rollback)..
141b3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
141b4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
141b5 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72  erRollback(Pager
141b6 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
141b7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
141b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141b9 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
141ba 20 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 43 45   */.  PAGERTRACE
141bb 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e  (("ROLLBACK %d\n
141bc 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
141bd 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61  r)));.  if( !pPa
141be 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20  ger->dbModified 
141bf 7c 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  || !isOpen(pPage
141c0 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 72  r->jfd) ){.    r
141c1 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
141c2 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
141c3 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
141c4 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ter);.  }else if
141c5 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
141c6 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  e && pPager->err
141c7 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c  Code!=SQLITE_FUL
141c8 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  L ){.    if( pPa
141c9 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
141ca 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  R_EXCLUSIVE ){. 
141cb 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62       pager_playb
141cc 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ack(pPager, 0);.
141cd 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70      }.    rc = p
141ce 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
141cf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
141d0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
141d1 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29  PAGER_RESERVED )
141d2 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b  {.      int rc2;
141d3 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
141d4 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
141d5 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  r, 0);.      rc2
141d6 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
141d7 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
141d8 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
141d9 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  er);.      if( r
141da 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
141db 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
141dc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
141dd 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
141de 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
141df 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d  Pager, 0);.    }
141e0 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42  ..    if( !MEMDB
141e1 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
141e2 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20  ->dbSizeValid = 
141e3 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
141e4 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
141e5 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c  urs during a ROL
141e6 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f  LBACK, we can no
141e7 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68   longer trust th
141e8 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63  e pager.    ** c
141e9 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61  ache. So call pa
141ea 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74  ger_error() on t
141eb 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61  he way out to ma
141ec 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20  ke any error .  
141ed 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e    ** persistent.
141ee 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
141ef 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
141f0 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20  ger, rc);.  }.  
141f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
141f2 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
141f3 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
141f4 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72  file is opened r
141f5 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72  ead-only.  Retur
141f6 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68  n FALSE.** if th
141f7 65 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69  e database is (i
141f8 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62  n theory) writab
141f9 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
141fa 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33  IVATE u8 sqlite3
141fb 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28  PagerIsreadonly(
141fc 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
141fd 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
141fe 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a  >readOnly;.}../*
141ff 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
14200 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
14201 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72  ces to the pager
14202 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14203 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
14204 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67  agerRefcount(Pag
14205 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
14206 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61  eturn sqlite3Pca
14207 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
14208 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a  er->pPCache);.}.
14209 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1420a 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
1420b 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70  rences to the sp
1420c 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f  ecified page..*/
1420d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1420e 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
1420f 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50  PageRefcount(DbP
14210 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72  age *pPage){.  r
14211 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61  eturn sqlite3Pca
14212 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28  chePageRefcount(
14213 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65  pPage);.}..#ifde
14214 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
14215 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14216 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
14217 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69  ting and analysi
14218 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  s only..*/.SQLIT
14219 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73  E_PRIVATE int *s
1421a 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73  qlite3PagerStats
1421b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
1421c 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b  .  static int a[
1421d 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71  11];.  a[0] = sq
1421e 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
1421f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
14220 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73  che);.  a[1] = s
14221 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
14222 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  count(pPager->pP
14223 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d  Cache);.  a[2] =
14224 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65   sqlite3PcacheGe
14225 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65  tCachesize(pPage
14226 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61  r->pPCache);.  a
14227 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  [3] = pPager->db
14228 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74  SizeValid ? (int
14229 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ) pPager->dbSize
1422a 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20   : -1;.  a[4] = 
1422b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20  pPager->state;. 
1422c 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[5] = pPager->
1422d 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20  errCode;.  a[6] 
1422e 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a  = pPager->nHit;.
1422f 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d    a[7] = pPager-
14230 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d  >nMiss;.  a[8] =
14231 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20   0;  /* Used to 
14232 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c  be pPager->nOvfl
14233 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61   */.  a[9] = pPa
14234 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b  ger->nRead;.  a[
14235 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57  10] = pPager->nW
14236 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61  rite;.  return a
14237 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
14238 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
14239 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d   this is an in-m
1423a 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a  emory pager..*/.
1423b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1423c 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  nt sqlite3PagerI
1423d 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50  sMemdb(Pager *pP
1423e 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
1423f 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  MEMDB;.}../*.** 
14240 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65  Check that there
14241 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 6e 53   are at least nS
14242 61 76 65 70 6f 69 6e 74 20 73 61 76 65 70 6f 69  avepoint savepoi
14243 6e 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 68 65  nts open. If the
14244 72 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e  re are.** curren
14245 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 6e 53  tly less than nS
14246 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20  avepoints open, 
14247 74 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72  then open one or
14248 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 73   more savepoints
14249 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 20 74  .** to make up t
1424a 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e 20 49  he difference. I
1424b 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
1424c 73 61 76 65 70 6f 69 6e 74 73 20 69 73 20 61 6c  savepoints is al
1424d 72 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74  ready.** equal t
1424e 6f 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 74 68  o nSavepoint, th
1424f 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
14250 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
14251 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  ** If a memory a
14252 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
14253 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73   SQLITE_NOMEM is
14254 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
14255 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72   error .** occur
14256 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20  s while opening 
14257 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
14258 66 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f  file, then an IO
14259 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a   error code is.*
1425a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  * returned. Othe
1425b 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b  rwise, SQLITE_OK
1425c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1425d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1425e 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
1425f 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
14260 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 29   int nSavepoint)
14261 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
14262 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
14263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14264 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
14265 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d    int nCurrent =
14266 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
14267 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  int;        /* C
14268 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
14269 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a   savepoints */..
1426a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74    if( nSavepoint
1426b 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61  >nCurrent && pPa
1426c 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
1426d 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20  ){.    int ii;  
1426e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1426f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14270 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
14271 62 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72  ble */.    Pager
14272 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b  Savepoint *aNew;
14273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14274 20 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e     /* New Pager.
14275 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79  aSavepoint array
14276 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68   */..    /* Eith
14277 65 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61  er there is no a
14278 63 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72  ctive journal or
14279 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
1427a 20 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20   is open or .   
1427b 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   ** the journal 
1427c 69 73 20 61 6c 77 61 79 73 20 73 74 6f 72 65 64  is always stored
1427d 20 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20   in memory */.  
1427e 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1427f 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20  ->nSavepoint==0 
14280 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  || isOpen(pPager
14281 2d 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20  ->sjfd) ||.     
14282 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
14283 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
14284 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
14285 4d 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  MORY );..    /* 
14286 47 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61  Grow the Pager.a
14287 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20  Savepoint array 
14288 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e  using realloc().
14289 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   Return SQLITE_N
1428a 4f 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74  OMEM.    ** if t
1428b 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  he allocation fa
1428c 69 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ils. Otherwise, 
1428d 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72  zero the new por
1428e 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a  tion in case a .
1428f 20 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61      ** malloc fa
14290 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69  ilure occurs whi
14291 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74  le populating it
14292 20 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29   in the for(...)
14293 20 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20   loop below..   
14294 20 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28   */.    aNew = (
14295 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
14296 29 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28  )sqlite3Realloc(
14297 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
14298 3e 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a  >aSavepoint, siz
14299 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69  eof(PagerSavepoi
1429a 6e 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20  nt)*nSavepoint. 
1429b 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61     );.    if( !a
1429c 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74  New ){.      ret
1429d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1429e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
1429f 65 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e  et(&aNew[nCurren
142a0 74 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 6f 69  t], 0, (nSavepoi
142a1 6e 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a 20 73  nt-nCurrent) * s
142a2 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70  izeof(PagerSavep
142a3 6f 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 61 67  oint));.    pPag
142a4 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d  er->aSavepoint =
142a5 20 61 4e 65 77 3b 0a 20 20 20 20 70 50 61 67 65   aNew;.    pPage
142a6 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  r->nSavepoint = 
142a7 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20  nSavepoint;..   
142a8 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65   /* Populate the
142a9 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
142aa 73 74 72 75 63 74 75 72 65 73 20 6a 75 73 74 20  structures just 
142ab 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20  allocated. */.  
142ac 20 20 66 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e    for(ii=nCurren
142ad 74 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74  t; ii<nSavepoint
142ae 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  ; ii++){.      a
142af 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
142b0 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20  bSizeValid );.  
142b1 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72      aNew[ii].nOr
142b2 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ig = pPager->dbS
142b3 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 69  ize;.      if( i
142b4 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
142b5 64 29 20 26 26 20 41 4c 57 41 59 53 28 70 50 61  d) && ALWAYS(pPa
142b6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
142b7 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e  0) ){.        aN
142b8 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d  ew[ii].iOffset =
142b9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
142ba 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Off;.      }else
142bb 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69  {.        aNew[i
142bc 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55  i].iOffset = JOU
142bd 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
142be 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
142bf 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62     aNew[ii].iSub
142c0 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53  Rec = pPager->nS
142c1 75 62 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65  ubRec;.      aNe
142c2 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69  w[ii].pInSavepoi
142c3 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  nt = sqlite3Bitv
142c4 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d  ecCreate(pPager-
142c5 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  >dbSize);.      
142c6 69 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49  if( !aNew[ii].pI
142c7 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  nSavepoint ){.  
142c8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
142c9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
142ca 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
142cb 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f   Open the sub-jo
142cc 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20  urnal, if it is 
142cd 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
142ce 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ed. */.    rc = 
142cf 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70  openSubJournal(p
142d0 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65  Pager);.    asse
142d1 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72  rtTruncateConstr
142d2 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20  aint(pPager);.  
142d3 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
142d4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
142d5 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
142d6 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20   to rollback or 
142d7 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29  release (commit)
142d8 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a   a savepoint..**
142d9 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74   The savepoint t
142da 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c  o release or rol
142db 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62  lback need not b
142dc 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
142dd 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20  tly .** created 
142de 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a  savepoint..**.**
142df 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73   Parameter op is
142e0 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53   always either S
142e1 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
142e2 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  K or SAVEPOINT_R
142e3 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74  ELEASE..** If it
142e4 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45   is SAVEPOINT_RE
142e5 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65  LEASE, then rele
142e6 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20  ase and destroy 
142e7 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69  the savepoint wi
142e8 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76  th.** index iSav
142e9 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73  epoint. If it is
142ea 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
142eb 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61  ACK, then rollba
142ec 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a  ck all changes.*
142ed 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75  * that have occu
142ee 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73  rred since the s
142ef 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69  pecified savepoi
142f0 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a  nt was created..
142f1 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f  **.** The savepo
142f2 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  int to rollback 
142f3 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64  or release is id
142f4 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61  entified by para
142f5 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70  meter .** iSavep
142f6 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66  oint. A value of
142f7 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72   0 means to oper
142f8 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72  ate on the outer
142f9 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a  most savepoint.*
142fa 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65  * (the first cre
142fb 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f  ated). A value o
142fc 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f  f (Pager.nSavepo
142fd 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65  int-1) means ope
142fe 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d  rate.** on the m
142ff 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65  ost recently cre
14300 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  ated savepoint. 
14301 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73  If iSavepoint is
14302 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
14303 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69   (Pager.nSavepoi
14304 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73  nt-1), then this
14305 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
14306 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  o-op..**.** If a
14307 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
14308 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  is passed to thi
14309 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  s function, then
1430a 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
1430b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
1430c 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73  olled back. This
1430d 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f   is different to
1430e 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c   calling .** sql
1430f 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
14310 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73  k() because this
14311 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
14312 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20  ot terminate.** 
14313 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
14314 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  or unlock the da
14315 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20  tabase, it just 
14316 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a  restores the .**
14317 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
14318 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
14319 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e   original state.
1431a 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63   .**.** In any c
1431b 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69  ase, all savepoi
1431c 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65  nts with an inde
1431d 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69  x greater than i
1431e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72  Savepoint .** ar
1431f 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20  e destroyed. If 
14320 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73  this is a releas
14321 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d  e operation (op=
14322 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
14323 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76  SE),.** then sav
14324 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  epoint iSavepoin
14325 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f  t is also destro
14326 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  yed..**.** This 
14327 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74  function may ret
14328 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
14329 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
1432a 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a  ocation fails,.*
1432b 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72  * or an IO error
1432c 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65   code if an IO e
1432d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
1432e 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61  e rolling back a
1432f 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20   .** savepoint. 
14330 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63  If no errors occ
14331 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ur, SQLITE_OK is
14332 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53   returned..*/ .S
14333 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14334 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
14335 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
14336 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
14337 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a  nt iSavepoint){.
14338 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
14339 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28  E_OK;..  assert(
1433a 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   op==SAVEPOINT_R
1433b 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41  ELEASE || op==SA
1433c 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
1433d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53   );.  assert( iS
1433e 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f  avepoint>=0 || o
1433f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
14340 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20  LBACK );..  if( 
14341 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65  iSavepoint<pPage
14342 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b  r->nSavepoint ){
14343 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20  .    int ii;    
14344 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
14345 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
14346 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20      int nNew;   
14347 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14348 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61   of remaining sa
14349 76 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74  vepoints after t
1434a 68 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20  his op. */..    
1434b 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
1434c 77 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74  w many savepoint
1434d 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20  s will still be 
1434e 61 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69  active after thi
1434f 73 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69  s.    ** operati
14350 6f 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76  on. Store this v
14351 61 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68  alue in nNew. Th
14352 65 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65  en free resource
14353 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20  s associated .  
14354 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61    ** with any sa
14355 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72  vepoints that ar
14356 65 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74  e destroyed by t
14357 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20  his operation.. 
14358 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d     */.    nNew =
14359 20 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f   iSavepoint + (o
1435a 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
1435b 4c 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28  LBACK);.    for(
1435c 69 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67  ii=nNew; ii<pPag
1435d 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20  er->nSavepoint; 
1435e 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c  ii++){.      sql
1435f 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
14360 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70  y(pPager->aSavep
14361 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65  oint[ii].pInSave
14362 70 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  point);.    }.  
14363 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70    pPager->nSavep
14364 6f 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20  oint = nNew;..  
14365 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
14366 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61  a rollback opera
14367 74 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74  tion, playback t
14368 68 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76  he specified sav
14369 65 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49  epoint..    ** I
1436a 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70  f this is a temp
1436b 2d 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73  -file, it is pos
1436c 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a  sible that the j
1436d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a  ournal file has.
1436e 20 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62      ** not yet b
1436f 65 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74  een opened. In t
14370 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68  his case there h
14371 61 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e  ave been no chan
14372 67 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68  ges to.    ** th
14373 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
14374 20 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b   so the playback
14375 20 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62   operation can b
14376 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a  e skipped..    *
14377 2f 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41  /.    if( op==SA
14378 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
14379 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
1437a 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
1437b 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
1437c 2a 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e  *pSavepoint = (n
1437d 4e 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65  New==0)?0:&pPage
1437e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e  r->aSavepoint[nN
1437f 65 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20  ew-1];.      rc 
14380 3d 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53  = pagerPlaybackS
14381 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c  avepoint(pPager,
14382 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20   pSavepoint);.  
14383 20 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53      assert(rc!=S
14384 51 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20  QLITE_DONE);.   
14385 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
14386 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73  this is a releas
14387 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f  e of the outermo
14388 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72  st savepoint, tr
14389 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74  uncate .    ** t
1438a 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74  he sub-journal t
1438b 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20  o zero bytes in 
1438c 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28  size. */.    if(
1438d 20 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d   nNew==0 && op==
1438e 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
1438f 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67  E && isOpen(pPag
14390 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20  er->sjfd) ){.   
14391 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
14392 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
14393 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
14394 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
14395 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20  >sjfd, 0);.     
14396 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63   pPager->nSubRec
14397 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
14398 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
14399 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1439a 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
1439b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1439c 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
1439d 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
1439e 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46  r *sqlite3PagerF
1439f 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70  ilename(Pager *p
143a0 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
143a1 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
143a2 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  me;.}../*.** Ret
143a3 75 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75  urn the VFS stru
143a4 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61  cture for the pa
143a5 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
143a6 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
143a7 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65  ite3_vfs *sqlite
143a8 33 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20  3PagerVfs(Pager 
143a9 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
143aa 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b  rn pPager->pVfs;
143ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
143ac 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65   the file handle
143ad 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
143ae 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65  e file associate
143af 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61  d.** with the pa
143b0 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74  ger.  This might
143b1 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   return NULL if 
143b2 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20  the file has.** 
143b3 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65  not yet been ope
143b4 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
143b5 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66  RIVATE sqlite3_f
143b6 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65  ile *sqlite3Page
143b7 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61  rFile(Pager *pPa
143b8 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
143b9 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a  Pager->fd;.}../*
143ba 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66  .** Return the f
143bb 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20  ull pathname of 
143bc 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
143bd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
143be 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
143bf 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
143c0 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70  nalname(Pager *p
143c1 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
143c2 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
143c3 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  l;.}../*.** Retu
143c4 72 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63  rn true if fsync
143c5 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73  () calls are dis
143c6 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70  abled for this p
143c7 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41  ager.  Return FA
143c8 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28  LSE.** if fsync(
143c9 29 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20  )s are executed 
143ca 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c  normally..*/.SQL
143cb 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
143cc 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79  sqlite3PagerNosy
143cd 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
143ce 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
143cf 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23  er->noSync;.}..#
143d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
143d1 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74  _CODEC./*.** Set
143d2 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65   or retrieve the
143d3 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20   codec for this 
143d4 70 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  pager.*/.static 
143d5 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
143d6 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67  rSetCodec(.  Pag
143d7 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f  er *pPager,.  vo
143d8 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f  id *(*xCodec)(vo
143d9 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69  id*,void*,Pgno,i
143da 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43  nt),.  void (*xC
143db 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f  odecSizeChng)(vo
143dc 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20  id*,int,int),.  
143dd 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65  void (*xCodecFre
143de 65 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69  e)(void*),.  voi
143df 64 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69  d *pCodec.){.  i
143e0 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  f( pPager->xCode
143e1 63 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e  cFree ) pPager->
143e2 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65  xCodecFree(pPage
143e3 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20 70 50  r->pCodec);.  pP
143e4 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 70  ager->xCodec = p
143e5 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3f 20 30  Pager->memDb ? 0
143e6 20 3a 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61   : xCodec;.  pPa
143e7 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43  ger->xCodecSizeC
143e8 68 6e 67 20 3d 20 78 43 6f 64 65 63 53 69 7a 65  hng = xCodecSize
143e9 43 68 6e 67 3b 0a 20 20 70 50 61 67 65 72 2d 3e  Chng;.  pPager->
143ea 78 43 6f 64 65 63 46 72 65 65 20 3d 20 78 43 6f  xCodecFree = xCo
143eb 64 65 63 46 72 65 65 3b 0a 20 20 70 50 61 67 65  decFree;.  pPage
143ec 72 2d 3e 70 43 6f 64 65 63 20 3d 20 70 43 6f 64  r->pCodec = pCod
143ed 65 63 3b 0a 20 20 70 61 67 65 72 52 65 70 6f 72  ec;.  pagerRepor
143ee 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 7d  tSize(pPager);.}
143ef 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
143f0 6c 69 74 65 33 50 61 67 65 72 47 65 74 43 6f 64  lite3PagerGetCod
143f1 65 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ec(Pager *pPager
143f2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
143f3 65 72 2d 3e 70 43 6f 64 65 63 3b 0a 7d 0a 23 65  er->pCodec;.}.#e
143f4 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
143f5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
143f6 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  CUUM./*.** Move 
143f7 74 68 65 20 70 61 67 65 20 70 50 67 20 74 6f 20  the page pPg to 
143f8 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e  location pgno in
143f9 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
143fa 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e   There must be n
143fb 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  o references to 
143fc 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75  the page previou
143fd 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a  sly located at.*
143fe 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65  * pgno (which we
143ff 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68   call pPgOld) th
14400 6f 75 67 68 20 74 68 61 74 20 70 61 67 65 20 69  ough that page i
14401 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a  s allowed to be.
14402 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66  ** in cache.  If
14403 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f   the page previo
14404 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20  usly located at 
14405 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65  pgno is not alre
14406 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f  ady.** in the ro
14407 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20  llback journal, 
14408 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68  it is not put th
14409 65 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72  ere by by this r
1440a 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65  outine..**.** Re
1440b 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
1440c 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20  page pPg remain 
1440d 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20  valid. Updating 
1440e 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61  any.** meta-data
1440f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
14410 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20   pPg (i.e. data 
14411 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45  stored in the nE
14412 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c  xtra bytes.** al
14413 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69  located along wi
14414 74 68 20 74 68 65 20 70 61 67 65 29 20 69 73 20  th the page) is 
14415 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
14416 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
14417 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
14418 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63  ction must be ac
14419 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 20 72  tive when this r
1441a 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1441b 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a  . It used to be.
1441c 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 61 74  ** required that
1441d 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
1441e 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74  nsaction was not
1441f 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 68 69   active, but thi
14420 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a  s restriction.**
14421 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65   has been remove
14422 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 58 20  d (CREATE INDEX 
14423 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20  needs to move a 
14424 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 61 74  page when a stat
14425 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
14426 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e  tion is active).
14427 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  .**.** If the fo
14428 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69  urth argument, i
14429 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d  sCommit, is non-
1442a 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20  zero, then this 
1442b 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a  page is being.**
1442c 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f   moved as part o
1442d 66 20 61 20 64 61 74 61 62 61 73 65 20 72 65 6f  f a database reo
1442e 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74  rganization just
1442f 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e   before the tran
14430 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62  saction .** is b
14431 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20  eing committed. 
14432 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 69 74  In this case, it
14433 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
14434 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
14435 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65   page .** pPg re
14436 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74  fers to will not
14437 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61   be written to a
14438 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73  gain within this
14439 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
1443a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1443b 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  n may return SQL
1443c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20  ITE_NOMEM or an 
1443d 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  IO error code if
1443e 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63   an error.** occ
1443f 75 72 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  urs. Otherwise, 
14440 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
14441 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
14442 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14443 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
14444 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
14445 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e  DbPage *pPg, Pgn
14446 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f  o pgno, int isCo
14447 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a  mmit){.  PgHdr *
14448 70 50 67 4f 6c 64 3b 20 20 20 20 20 20 20 20 20  pPgOld;         
14449 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
1444a 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74  e being overwrit
1444b 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e  ten. */.  Pgno n
1444c 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b  eedSyncPgno = 0;
1444d 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61         /* Old va
1444e 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f  lue of pPg->pgno
1444f 2c 20 69 66 20 73 79 6e 63 20 69 73 20 72 65 71  , if sync is req
14450 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72  uired */.  int r
14451 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
14452 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
14453 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f  n code */.  Pgno
14454 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20 20 20 20   origPgno;      
14455 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
14456 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 6e 75  original page nu
14457 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72  mber */..  asser
14458 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29  t( pPg->nRef>0 )
14459 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6f 72 64 65 72  ;..  /* In order
1445a 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 72   to be able to r
1445b 6f 6c 6c 62 61 63 6b 2c 20 61 6e 20 69 6e 2d 6d  ollback, an in-m
1445c 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6d  emory database m
1445d 75 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a  ust journal.  **
1445e 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65   the page we are
1445f 20 6d 6f 76 69 6e 67 20 66 72 6f 6d 2e 0a 20 20   moving from..  
14460 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  */.  if( MEMDB )
14461 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
14462 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
14463 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
14464 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
14465 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
14466 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20   being moved is 
14467 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f  dirty and has no
14468 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79 20  t been saved by 
14469 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20  the latest.  ** 
1446a 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20  savepoint, then 
1446b 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  save the current
1446c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1446d 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a   page into the .
1446e 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c    ** sub-journal
1446f 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65   now. This is re
14470 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65  quired to handle
14471 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
14472 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20  cenario:.  **.  
14473 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a  **   BEGIN;.  **
14474 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61       <journal pa
14475 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66  ge X, then modif
14476 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a  y it in memory>.
14477 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49    **     SAVEPOI
14478 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20  NT one;.  **    
14479 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20     <Move page X 
1447a 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20  to location Y>. 
1447b 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b   **     ROLLBACK
1447c 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20   TO one;.  **.  
1447d 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65 72  ** If page X wer
1447e 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f  e not written to
1447f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
14480 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20   here, it would 
14481 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73  not.  ** be poss
14482 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20  ible to restore 
14483 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65  its contents whe
14484 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  n the "ROLLBACK 
14485 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61  TO one".  ** sta
14486 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70  tement were is p
14487 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20  rocessed..  **. 
14488 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61   ** subjournalPa
14489 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f  ge() may need to
1448a 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   allocate space 
1448b 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67  to store pPg->pg
1448c 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65  no into.  ** one
1448d 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69   or more savepoi
1448e 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68 69 73  nt bitvecs. This
1448f 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74   is the reason t
14490 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  his function.  *
14491 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  * may return SQL
14492 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a  ITE_NOMEM..  */.
14493 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73    if( pPg->flags
14494 26 50 47 48 44 52 5f 44 49 52 54 59 0a 20 20 20  &PGHDR_DIRTY.   
14495 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  && subjRequiresP
14496 61 67 65 28 70 50 67 29 0a 20 20 20 26 26 20 53  age(pPg).   && S
14497 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
14498 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70  subjournalPage(p
14499 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  Pg)).  ){.    re
1449a 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
1449b 50 41 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56  PAGERTRACE(("MOV
1449c 45 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65  E %d page %d (ne
1449d 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73  edSync=%d) moves
1449e 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20   to %d\n", .    
1449f 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
144a0 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70  ), pPg->pgno, (p
144a1 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
144a2 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20  NEED_SYNC)?1:0, 
144a3 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43  pgno));.  IOTRAC
144a4 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25  E(("MOVE %p %d %
144a5 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
144a6 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a  g->pgno, pgno)).
144a7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75  .  /* If the jou
144a8 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65  rnal needs to be
144a9 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65   sync()ed before
144aa 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20   page pPg->pgno 
144ab 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74  can.  ** be writ
144ac 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50  ten to, store pP
144ad 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c  g->pgno in local
144ae 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79   variable needSy
144af 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a  ncPgno..  **.  *
144b0 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69  * If the isCommi
144b1 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  t flag is set, t
144b2 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
144b3 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74  to remember that
144b4 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  .  ** the journa
144b5 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79  l needs to be sy
144b6 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61  nc()ed before da
144b7 74 61 62 61 73 65 20 70 61 67 65 20 70 50 67 2d  tabase page pPg-
144b8 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20  >pgno .  ** can 
144b9 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54  be written to. T
144ba 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c  he caller has al
144bb 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e  ready promised n
144bc 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69  ot to write to i
144bd 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  t..  */.  if( (p
144be 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
144bf 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69  NEED_SYNC) && !i
144c0 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e  sCommit ){.    n
144c1 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50  eedSyncPgno = pP
144c2 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73  g->pgno;.    ass
144c3 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e  ert( pageInJourn
144c4 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e  al(pPg) || pPg->
144c5 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f  pgno>pPager->dbO
144c6 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61  rigSize );.    a
144c7 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67  ssert( pPg->flag
144c8 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b  s&PGHDR_DIRTY );
144c9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
144ca 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b  ger->needSync );
144cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
144cc 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73  e cache contains
144cd 20 61 20 70 61 67 65 20 77 69 74 68 20 70 61 67   a page with pag
144ce 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72  e-number pgno, r
144cf 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72  emove it.  ** fr
144d0 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68 61 69  om its hash chai
144d1 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20  n. Also, if the 
144d2 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77  PgHdr.needSync w
144d3 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a  as set for .  **
144d4 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72   page pgno befor
144d5 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65  e the 'move' ope
144d6 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73  ration, it needs
144d7 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20   to be retained 
144d8 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61  .  ** for the pa
144d9 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a  ge moved there..
144da 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67    */.  pPg->flag
144db 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44  s &= ~PGHDR_NEED
144dc 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20  _SYNC;.  pPgOld 
144dd 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70  = pager_lookup(p
144de 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  Pager, pgno);.  
144df 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20  assert( !pPgOld 
144e0 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d  || pPgOld->nRef=
144e1 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f  =1 );.  if( pPgO
144e2 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66  ld ){.    pPg->f
144e3 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d  lags |= (pPgOld-
144e4 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
144e5 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28  D_SYNC);.    if(
144e6 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20   MEMDB ){.      
144e7 2f 2a 20 44 6f 20 6e 6f 74 20 64 69 73 63 61 72  /* Do not discar
144e8 64 20 70 61 67 65 73 20 66 72 6f 6d 20 61 6e 20  d pages from an 
144e9 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
144ea 73 65 20 73 69 6e 63 65 20 77 65 20 6d 69 67 68  se since we migh
144eb 74 0a 20 20 20 20 20 20 2a 2a 20 6e 65 65 64 20  t.      ** need 
144ec 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6c 61 74 65  to rollback late
144ed 72 2e 20 20 4a 75 73 74 20 6d 6f 76 65 20 74 68  r.  Just move th
144ee 65 20 70 61 67 65 20 6f 75 74 20 6f 66 20 74 68  e page out of th
144ef 65 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20  e way. */.      
144f0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
144f1 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20  dbSizeValid );. 
144f2 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
144f3 68 65 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 70  heMove(pPgOld, p
144f4 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b 31 29  Pager->dbSize+1)
144f5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
144f6 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
144f7 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a 20 20  Drop(pPgOld);.  
144f8 20 20 7d 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50    }.  }..  origP
144f9 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
144fa 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
144fb 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b  Move(pPg, pgno);
144fc 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
144fd 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  MakeDirty(pPg);.
144fe 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
144ff 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28  fied = 1;..  if(
14500 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b   needSyncPgno ){
14501 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53  .    /* If needS
14502 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a  yncPgno is non-z
14503 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ero, then the jo
14504 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73  urnal file needs
14505 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73   to be .    ** s
14506 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 61  ync()ed before a
14507 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 74  ny data is writt
14508 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66  en to database f
14509 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e  ile page needSyn
1450a 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75  cPgno..    ** Cu
1450b 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68  rrently, no such
1450c 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e 20   page exists in 
1450d 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61  the page-cache a
1450e 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22  nd the .    ** "
1450f 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69  is journaled" bi
14510 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62 65  tvec flag has be
14511 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65  en set. This nee
14512 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65  ds to be remedie
14513 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64  d by.    ** load
14514 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e 74  ing the page int
14515 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  o the pager-cach
14516 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68  e and setting th
14517 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63  e PgHdr.needSync
14518 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20   .    ** flag.. 
14519 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
1451a 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c  the attempt to l
1451b 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e 74  oad the page int
1451c 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  o the page-cache
1451d 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20   fails, (due.   
1451e 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28   ** to a malloc(
1451f 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29  ) or IO failure)
14520 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74 20  , clear the bit 
14521 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61  in the pInJourna
14522 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79  l[].    ** array
14523 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
14524 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64  the page is load
14525 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61  ed and written a
14526 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74  gain in.    ** t
14527 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  his transaction,
14528 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 74   it may be writt
14529 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
1452a 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20  se file before. 
1452b 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63     ** it is sync
1452c 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  ed into the jour
1452d 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77  nal file. This w
1452e 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75  ay, it may end u
1452f 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  p in.    ** the 
14530 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69  journal file twi
14531 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73 20  ce, but that is 
14532 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20  not a problem.. 
14533 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
14534 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
14535 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73  () call may caus
14536 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f  e the journal to
14537 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20   sync. So make. 
14538 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50     ** sure the P
14539 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c  ager.needSync fl
1453a 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20  ag is set too.. 
1453b 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20     */.    PgHdr 
1453c 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73  *pPgHdr;.    ass
1453d 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65  ert( pPager->nee
1453e 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20  dSync );.    rc 
1453f 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
14540 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79  t(pPager, needSy
14541 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29  ncPgno, &pPgHdr)
14542 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
14543 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
14544 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e   if( needSyncPgn
14545 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  o<=pPager->dbOri
14546 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  gSize ){.       
14547 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14548 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b  >pTmpSpace!=0 );
14549 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1454a 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61 67  BitvecClear(pPag
1454b 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20  er->pInJournal, 
1454c 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50  needSyncPgno, pP
1454d 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29  ager->pTmpSpace)
1454e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1454f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
14550 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  .    pPager->nee
14551 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61  dSync = 1;.    a
14552 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
14553 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d  oSync==0 && !MEM
14554 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72  DB );.    pPgHdr
14555 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
14556 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
14557 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
14558 65 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a  eDirty(pPgHdr);.
14559 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
1455a 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20  Unref(pPgHdr);. 
1455b 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f   }..  /*.  ** Fo
1455c 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
1455d 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75  atabase, make su
1455e 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
1455f 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20  page continues. 
14560 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e   ** to exist, in
14561 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61   case the transa
14562 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72  ction needs to r
14563 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65 20 70  oll back.  Use p
14564 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 74 68  PgOld.  ** as th
14565 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20  e original page 
14566 73 69 6e 63 65 20 69 74 20 68 61 73 20 61 6c 72  since it has alr
14567 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
14568 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
14569 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 73 71 6c  MEMDB ){.    sql
1456a 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70  ite3PcacheMove(p
1456b 50 67 4f 6c 64 2c 20 6f 72 69 67 50 67 6e 6f 29  PgOld, origPgno)
1456c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
1456d 65 72 55 6e 72 65 66 28 70 50 67 4f 6c 64 29 3b  erUnref(pPgOld);
1456e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
1456f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
14570 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
14571 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
14572 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 73  e data for the s
14573 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a  pecified page..*
14574 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14575 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
14576 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67  gerGetData(DbPag
14577 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72  e *pPg){.  asser
14578 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c  t( pPg->nRef>0 |
14579 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d  | pPg->pPager->m
1457a 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 72 6e  emDb );.  return
1457b 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a   pPg->pData;.}..
1457c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
1457d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61  ointer to the Pa
1457e 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 65 73  ger.nExtra bytes
1457f 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 61 63   of "extra" spac
14580 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  e .** allocated 
14581 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 73  along with the s
14582 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a  pecified page..*
14583 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14584 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
14585 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61  gerGetExtra(DbPa
14586 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75  ge *pPg){.  retu
14587 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 3b 0a  rn pPg->pExtra;.
14588 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74  }../*.** Get/set
14589 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
1458a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  e for this pager
1458b 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64  . Parameter eMod
1458c 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a 2a  e must be one.**
1458d 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e   of PAGER_LOCKIN
1458e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 47  GMODE_QUERY, PAG
1458f 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
14590 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 47  ORMAL or .** PAG
14591 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
14592 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 65  XCLUSIVE. If the
14593 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
14594 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a 2a  t _QUERY, then.*
14595 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  * the locking-mo
14596 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  de is set to the
14597 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64   value specified
14598 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
14599 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 69  rned value is ei
1459a 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ther PAGER_LOCKI
1459b 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72  NGMODE_NORMAL or
1459c 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  .** PAGER_LOCKIN
1459d 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2c  GMODE_EXCLUSIVE,
1459e 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
1459f 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c  current (possibl
145a0 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c 6f  y updated).** lo
145a1 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53  cking-mode..*/.S
145a2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
145a3 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  t sqlite3PagerLo
145a4 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20  ckingMode(Pager 
145a5 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f  *pPager, int eMo
145a6 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 65  de){.  assert( e
145a7 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
145a8 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20  INGMODE_QUERY.  
145a9 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
145aa 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
145ab 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 20  GMODE_NORMAL.   
145ac 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
145ad 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
145ae 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
145af 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
145b0 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
145b1 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 72  ERY<0 );.  asser
145b2 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
145b3 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 26  MODE_NORMAL>=0 &
145b4 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  & PAGER_LOCKINGM
145b5 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d 30  ODE_EXCLUSIVE>=0
145b6 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 3e   );.  if( eMode>
145b7 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74  =0 && !pPager->t
145b8 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 70  empFile ){.    p
145b9 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
145ba 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65  Mode = (u8)eMode
145bb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
145bc 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 6c  int)pPager->excl
145bd 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a  usiveMode;.}../*
145be 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20  .** Get/set the 
145bf 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72  journal-mode for
145c0 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72   this pager. Par
145c1 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73  ameter eMode mus
145c2 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a  t be one of:.**.
145c3 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
145c4 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a 2a  NALMODE_QUERY.**
145c5 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
145c6 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a 20  LMODE_DELETE.** 
145c7 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c     PAGER_JOURNAL
145c8 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a 2a  MODE_TRUNCATE.**
145c9 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
145ca 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a 2a  LMODE_PERSIST.**
145cb 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
145cc 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20 20  LMODE_OFF.**    
145cd 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
145ce 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20 49  E_MEMORY.**.** I
145cf 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
145d0 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74  is not _QUERY, t
145d1 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f  hen the journal_
145d2 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74  mode is set to t
145d3 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65 63  he.** value spec
145d4 69 66 69 65 64 20 69 66 20 74 68 65 20 63 68 61  ified if the cha
145d5 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20  nge is allowed. 
145d6 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 20 64   The change is d
145d7 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 6f 72  isallowed.** for
145d8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   the following r
145d9 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  easons:.**.**   
145da 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  *  An in-memory 
145db 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e 6c  database can onl
145dc 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72 6e  y have its journ
145dd 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20 5f  al_mode set to _
145de 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20 5f  OFF.**      or _
145df 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20 20  MEMORY..**.**   
145e0 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 6d  *  The journal m
145e1 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  ode may not be c
145e2 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 20 74  hanged while a t
145e3 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
145e4 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tive..**.** The 
145e5 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74  returned indicat
145e6 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70  e the current (p
145e7 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29  ossibly updated)
145e8 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a   journal-mode..*
145e9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
145ea 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
145eb 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67  rJournalMode(Pag
145ec 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
145ed 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74  eMode){.  assert
145ee 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  ( eMode==PAGER_J
145ef 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59  OURNALMODE_QUERY
145f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
145f1 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
145f2 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a  RNALMODE_DELETE.
145f3 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
145f4 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
145f5 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
145f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
145f7 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
145f8 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
145f9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
145fa 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
145fb 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20  RNALMODE_OFF .  
145fc 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
145fd 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
145fe 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a  LMODE_MEMORY );.
145ff 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f    assert( PAGER_
14600 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
14601 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f  Y<0 );.  if( eMo
14602 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 4d 45  de>=0.   && (!ME
14603 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41  MDB || eMode==PA
14604 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
14605 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20 20  MEMORY .        
14606 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
14607 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
14608 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 70 50  E_OFF).   && !pP
14609 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
1460a 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 6e 28  .   && (!isOpen(
1460b 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
1460c 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  0==pPager->journ
1460d 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 20 20  alOff).  ){.    
1460e 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
1460f 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
14610 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
14611 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
14612 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
14613 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 75  journalMode = (u
14614 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72  8)eMode;.  }.  r
14615 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65  eturn (int)pPage
14616 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a  r->journalMode;.
14617 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74  }../*.** Get/set
14618 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74 20   the size-limit 
14619 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 73 74  used for persist
1461a 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ent journal file
1461b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67  s..**.** Setting
1461c 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74 20   the size limit 
1461d 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f 20 6c  to -1 means no l
1461e 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 65 64  imit is enforced
1461f 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20  ..** An attempt 
14620 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 20 73  to set a limit s
14621 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 20 69  maller than -1 i
14622 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  s a no-op..*/.SQ
14623 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
14624 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
14625 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
14626 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36 34  ger *pPager, i64
14627 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28 20   iLimit){.  if( 
14628 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20 20  iLimit>=-1 ){.  
14629 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
1462a 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c 69  lSizeLimit = iLi
1462b 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  mit;.  }.  retur
1462c 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  n pPager->journa
1462d 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f  lSizeLimit;.}../
1462e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
1462f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 61  inter to the pPa
14630 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72  ger->pBackup var
14631 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75  iable. The backu
14632 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 62  p module.** in b
14633 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e  ackup.c maintain
14634 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  s the content of
14635 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20   this variable. 
14636 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 75  This module.** u
14637 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79 20  ses it opaquely 
14638 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  as an argument t
14639 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52  o sqlite3BackupR
1463a 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a 20  estart() and.** 
1463b 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
1463c 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  ate() only..*/.S
1463d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
1463e 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73  lite3_backup **s
1463f 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75  qlite3PagerBacku
14640 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 67  pPtr(Pager *pPag
14641 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 70  er){.  return &p
14642 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b 0a  Pager->pBackup;.
14643 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
14644 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20  ITE_OMIT_DISKIO 
14645 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
14646 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72  *** End of pager
14647 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
14648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1464a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1464b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
1464c 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
1464d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1464e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1464f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41  **/./*.** 2007 A
14650 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 54  ugust 27.**.** T
14651 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
14652 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
14653 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
14654 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
14655 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
14656 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
14657 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
14658 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
14659 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1465a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1465b 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1465c 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1465d 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1465e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1465f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
14660 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
14661 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
14662 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14664 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14665 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14666 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
14667 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
14668 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
14669 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
1466a 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73  on Btree objects
1466b 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72  ..** This code r
1466c 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e  eally belongs in
1466d 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62   btree.c.  But b
1466e 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e  tree.c is gettin
1466f 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64  g too.** big and
14670 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61   we want to brea
14671 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20  k it down some. 
14672 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73   This packaged s
14673 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20  eemed like.** a 
14674 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a  good breakout..*
14675 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
14676 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49  * Include btreeI
14677 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  nt.h in the midd
14678 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20  le of btmutex.c 
14679 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1467a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1467b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72  * Begin file btr
1467c 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  eeInt.h ********
1467d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1467e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1467f 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72  /./*.** 2004 Apr
14680 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  il 6.**.** The a
14681 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
14682 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
14683 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
14684 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
14685 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
14686 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
14687 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
14688 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
14689 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1468a 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1468b 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1468c 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1468d 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1468e 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1468f 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
14690 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
14691 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
14692 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14693 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14696 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
14697 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78   implements a ex
14698 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73  ternal (disk-bas
14699 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69  ed) database usi
1469a 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f  ng BTrees..** Fo
1469b 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 69 73  r a detailed dis
1469c 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 65  cussion of BTree
1469d 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a  s, refer to.**.*
1469e 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20  *     Donald E. 
1469f 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 20 4f  Knuth, THE ART O
146a0 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 52  F COMPUTER PROGR
146a1 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33  AMMING, Volume 3
146a2 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e  :.**     "Sortin
146a3 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22  g And Searching"
146a4 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e  , pages 473-480.
146a5 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a   Addison-Wesley.
146a6 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e  **     Publishin
146a7 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69  g Company, Readi
146a8 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74  ng, Massachusett
146a9 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73  s..**.** The bas
146aa 69 63 20 69 64 65 61 20 69 73 20 74 68 61 74 20  ic idea is that 
146ab 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65  each page of the
146ac 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e   file contains N
146ad 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74   database.** ent
146ae 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69  ries and N+1 poi
146af 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 67 65  nters to subpage
146b0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  s..**.**   -----
146b1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146b4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
146b5 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b 65 79   |  Ptr(0) | Key
146b6 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c 20 4b  (0) | Ptr(1) | K
146b7 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65  ey(1) | ... | Ke
146b8 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 20  y(N-1) | Ptr(N) 
146b9 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  |.**   ---------
146ba 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146bb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146bc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146bd 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c  -------.**.** Al
146be 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e  l of the keys on
146bf 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 50   the page that P
146c0 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 6f 20  tr(0) points to 
146c1 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 73 73  have values less
146c2 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 2e  .** than Key(0).
146c3 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79    All of the key
146c4 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 31 29  s on page Ptr(1)
146c5 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65   and its subpage
146c6 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73  s have.** values
146c7 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65   greater than Ke
146c8 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 74 68  y(0) and less th
146c9 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20  an Key(1).  All 
146ca 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f  of the keys.** o
146cb 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 74 73  n Ptr(N) and its
146cc 20 73 75 62 70 61 67 65 73 20 68 61 76 65 20 76   subpages have v
146cd 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 68  alues greater th
146ce 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e  an Key(N-1).  An
146cf 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a  d.** so forth..*
146d0 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70  *.** Finding a p
146d1 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65  articular key re
146d2 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f  quires reading O
146d3 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66  (log(M)) pages f
146d4 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b  rom the .** disk
146d5 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 65 20   where M is the 
146d6 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
146d7 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  s in the tree..*
146d8 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70  *.** In this imp
146d9 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73  lementation, a s
146da 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 68  ingle file can h
146db 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  old one or more 
146dc 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 72  separate .** BTr
146dd 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 65 65  ees.  Each BTree
146de 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
146df 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 69  y the index of i
146e0 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54  ts root page.  T
146e1 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61  he.** key and da
146e2 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 79  ta for any entry
146e3 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f   are combined to
146e4 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 6c 6f   form the "paylo
146e5 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64  ad".  A.** fixed
146e6 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f   amount of paylo
146e7 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 69 65  ad can be carrie
146e8 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 68  d directly on th
146e9 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61  e database.** pa
146ea 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 79 6c  ge.  If the payl
146eb 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 74 68  oad is larger th
146ec 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 61 6d  an the preset am
146ed 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 6c 75  ount then surplu
146ee 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 20 73  s.** bytes are s
146ef 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f  tored on overflo
146f0 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 70 61  w pages.  The pa
146f1 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e 74  yload for an ent
146f2 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72  ry.** and the pr
146f3 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 20  eceding pointer 
146f4 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20  are combined to 
146f5 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20  form a "Cell".  
146f6 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 61  Each .** page ha
146f7 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 72  s a small header
146f8 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20   which contains 
146f9 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 6e 74  the Ptr(N) point
146fa 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20  er and other.** 
146fb 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68  information such
146fc 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   as the size of 
146fd 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a  key and data..**
146fe 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 49  .** FORMAT DETAI
146ff 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c  LS.**.** The fil
14700 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e 74  e is divided int
14701 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 66 69  o pages.  The fi
14702 72 73 74 20 70 61 67 65 20 69 73 20 63 61 6c 6c  rst page is call
14703 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68  ed page 1,.** th
14704 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65  e second is page
14705 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68   2, and so forth
14706 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72  .  A page number
14707 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74   of zero indicat
14708 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70  es.** "no such p
14709 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 65 20  age".  The page 
1470a 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20  size can be any 
1470b 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65  power of 2 betwe
1470c 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38  en 512 and 32768
1470d 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63  ..** Each page c
1470e 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62  an be either a b
1470f 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65  tree page, a fre
14710 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f  elist page, an o
14711 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c  verflow.** page,
14712 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   or a pointer-ma
14713 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  p page..**.** Th
14714 65 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20  e first page is 
14715 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 20 70  always a btree p
14716 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  age.  The first 
14717 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65  100 bytes of the
14718 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63   first.** page c
14719 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c  ontain a special
1471a 20 68 65 61 64 65 72 20 28 74 68 65 20 22 66 69   header (the "fi
1471b 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 61 74  le header") that
1471c 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 66   describes the f
1471d 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d  ile..** The form
1471e 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68  at of the file h
1471f 65 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c  eader is as foll
14720 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46  ows:.**.**   OFF
14721 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 44 45  SET   SIZE    DE
14722 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20  SCRIPTION.**    
14723 20 20 30 20 20 20 20 20 20 31 36 20 20 20 20 20    0      16     
14724 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22  Header string: "
14725 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c  SQLite format 3\
14726 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20  000".**     16  
14727 20 20 20 20 20 32 20 20 20 20 20 50 61 67 65 20       2     Page 
14728 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20  size in bytes.  
14729 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 20 20  .**     18      
1472a 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d   1     File form
1472b 61 74 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e  at write version
1472c 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 20 20  .**     19      
1472d 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d   1     File form
1472e 61 74 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a  at read version.
1472f 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 20 20  **     20       
14730 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 20 75  1     Bytes of u
14731 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 20 74  nused space at t
14732 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70  he end of each p
14733 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20  age.**     21   
14734 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d      1     Max em
14735 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
14736 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
14737 32 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69  2       1     Mi
14738 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f  n embedded paylo
14739 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20  ad fraction.**  
1473a 20 20 20 32 33 20 20 20 20 20 20 20 31 20 20 20     23       1   
1473b 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f    Min leaf paylo
1473c 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20  ad fraction.**  
1473d 20 20 20 32 34 20 20 20 20 20 20 20 34 20 20 20     24       4   
1473e 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f    File change co
1473f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20  unter.**     28 
14740 20 20 20 20 20 20 34 20 20 20 20 20 52 65 73 65        4     Rese
14741 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
14742 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20  use.**     32   
14743 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20      4     First 
14744 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a  freelist page.**
14745 20 20 20 20 20 33 36 20 20 20 20 20 20 20 34 20       36       4 
14746 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72      Number of fr
14747 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20  eelist pages in 
14748 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20  the file.**     
14749 34 30 20 20 20 20 20 20 36 30 20 20 20 20 20 31  40      60     1
1474a 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61  5 4-byte meta va
1474b 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f 20 68  lues passed to h
1474c 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a  igher layers.**.
1474d 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 20  **     40       
1474e 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f  4     Schema coo
1474f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20  kie.**     44   
14750 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 66      4     File f
14751 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20  ormat of schema 
14752 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20  layer.**     48 
14753 20 20 20 20 20 20 34 20 20 20 20 20 53 69 7a 65        4     Size
14754 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a   of page cache.*
14755 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 20 34  *     52       4
14756 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f       Largest roo
14757 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63  t-page (auto/inc
14758 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20  r_vacuum).**    
14759 20 35 36 20 20 20 20 20 20 20 34 20 20 20 20 20   56       4     
1475a 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c  1=UTF-8 2=UTF16l
1475b 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20  e 3=UTF16be.**  
1475c 20 20 20 36 30 20 20 20 20 20 20 20 34 20 20 20     60       4   
1475d 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a    User version.*
1475e 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 20 34  *     64       4
1475f 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c       Incremental
14760 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20   vacuum mode.** 
14761 20 20 20 20 36 38 20 20 20 20 20 20 20 34 20 20      68       4  
14762 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20     unused.**    
14763 20 37 32 20 20 20 20 20 20 20 34 20 20 20 20 20   72       4     
14764 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36  unused.**     76
14765 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75         4     unu
14766 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  sed.**.** All of
14767 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
14768 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69  ues are big-endi
14769 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69  an (most signifi
1476a 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29  cant byte first)
1476b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ..**.** The file
1476c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1476d 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
1476e 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1476f 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54   is changed.** T
14770 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f  his counter allo
14771 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ws other process
14772 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20  es to know when 
14773 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
14774 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73  nged.** and thus
14775 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20   when they need 
14776 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63  to flush their c
14777 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
14778 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  max embedded pay
14779 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73  load fraction is
1477a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
1477b 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a  he total usable.
1477c 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61  ** space in a pa
1477d 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ge that can be c
1477e 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e  onsumed by a sin
1477f 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61  gle cell for sta
14780 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20  ndard.** B-tree 
14781 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74  (non-LEAFDATA) t
14782 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20  ables.  A value 
14783 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30  of 255 means 100
14784 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a  %.  The default.
14785 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74  ** is to limit t
14786 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
14787 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20  size so that at 
14788 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69  least 4 cells wi
14789 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65  ll fit.** on one
1478a 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65   page.  Thus the
1478b 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62   default max emb
1478c 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
1478d 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a  action is 64..**
1478e 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f  .** If the paylo
1478f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73  ad for a cell is
14790 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
14791 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68   max payload, th
14792 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c  en extra.** payl
14793 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74  oad is spilled t
14794 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
14795 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  .  Once an overf
14796 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f  low page is allo
14797 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e  cated,.** as man
14798 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69  y bytes as possi
14799 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e  ble are moved in
1479a 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  to the overflow 
1479b 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65  pages without le
1479c 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c  tting.** the cel
1479d 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f  l size drop belo
1479e 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  w the min embedd
1479f 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
147a0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ion..**.** The m
147a1 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
147a2 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65  fraction is like
147a3 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65   the min embedde
147a4 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
147a5 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  on.** except tha
147a6 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20  t it applies to 
147a7 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20  leaf nodes in a 
147a8 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20  LEAFDATA tree.  
147a9 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70  The maximum.** p
147aa 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
147ab 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74  for a LEAFDATA t
147ac 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30  ree is always 10
147ad 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20  0% (or 255) and 
147ae 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66  it.** not specif
147af 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65  ied in the heade
147b0 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74  r..**.** Each bt
147b1 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76  ree pages is div
147b2 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20  ided into three 
147b3 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68  sections:  The h
147b4 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65  eader, the.** ce
147b5 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
147b6 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  , and the cell c
147b7 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61  ontent area.  Pa
147b8 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20  ge 1 also has a 
147b9 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65  100-byte.** file
147ba 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63   header that occ
147bb 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70  urs before the p
147bc 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  age header..**.*
147bd 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
147be 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
147bf 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20    | file header 
147c0 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73     |   100 bytes
147c1 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a  .  Page 1 only..
147c2 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
147c3 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20  ---------|.**   
147c4 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72     | page header
147c5 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20      |   8 bytes 
147c6 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20  for leaves.  12 
147c7 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69  bytes for interi
147c8 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20  or nodes.**     
147c9 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
147ca 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  --|.**      | ce
147cb 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20  ll pointer   |  
147cc 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20   |  2 bytes per 
147cd 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72  cell.  Sorted or
147ce 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61  der..**      | a
147cf 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20  rray          | 
147d0 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77    |  Grows downw
147d1 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20  ard.**      |   
147d2 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
147d3 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d   v.**      |----
147d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
147d5 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61        | unalloca
147d6 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20  ted    |.**     
147d7 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20   | space        
147d8 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d    |.**      |---
147d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20  -------------|  
147da 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64   ^  Grows upward
147db 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c  s.**      | cell
147dc 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c   content   |   |
147dd 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65    Arbitrary orde
147de 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77  r interspersed w
147df 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a  ith freeblocks..
147e0 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20  **      | area  
147e1 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20           |   |  
147e2 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66  and free space f
147e3 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20  ragments..**    
147e4 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
147e5 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ---|.**.** The p
147e6 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b  age headers look
147e7 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
147e8 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49  **   OFFSET   SI
147e9 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49  ZE     DESCRIPTI
147ea 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  ON.**      0    
147eb 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e     1      Flags.
147ec 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a   1: intkey, 2: z
147ed 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66  erodata, 4: leaf
147ee 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a  data, 8: leaf.**
147ef 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20        1       2 
147f0 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74       byte offset
147f1 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72   to the first fr
147f2 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20  eeblock.**      
147f3 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e  3       2      n
147f4 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
147f5 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20  n this page.**  
147f6 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20      5       2   
147f7 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66     first byte of
147f8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
147f9 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37  t area.**      7
147fa 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75         1      nu
147fb 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74  mber of fragment
147fc 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a  ed free bytes.**
147fd 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20        8       4 
147fe 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64       Right child
147ff 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c   (the Ptr(N) val
14800 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e  ue).  Omitted on
14801 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   leaves..**.** T
14802 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20  he flags define 
14803 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  the format of th
14804 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20  is btree page.  
14805 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65  The leaf flag me
14806 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73  ans that.** this
14807 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69   page has no chi
14808 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f  ldren.  The zero
14809 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20  data flag means 
1480a 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63  that this page c
1480b 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b  arries.** only k
1480c 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e  eys and no data.
1480d 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61    The intkey fla
1480e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  g means that the
1480f 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65   key is a intege
14810 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74  r.** which is st
14811 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20  ored in the key 
14812 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68  size entry of th
14813 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61  e cell header ra
14814 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20  ther than in.** 
14815 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61  the payload area
14816 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  ..**.** The cell
14817 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62   pointer array b
14818 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72  egins on the fir
14819 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68  st byte after th
1481a 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a  e page header..*
1481b 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  * The cell point
1481c 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  er array contain
1481d 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32  s zero or more 2
1481e 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68  -byte numbers wh
1481f 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65  ich are.** offse
14820 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  ts from the begi
14821 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
14822 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  e to the cell co
14823 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c  ntent in the cel
14824 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65  l.** content are
14825 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  a.  The cell poi
14826 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73  nters occur in s
14827 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68  orted order.  Th
14828 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73  e system strives
14829 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65  .** to keep free
1482a 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65   space after the
1482b 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74   last cell point
1482c 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63  er so that new c
1482d 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65  ells can.** be e
1482e 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68  asily added with
1482f 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65  out having to de
14830 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
14831 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  e..**.** Cell co
14832 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20  ntent is stored 
14833 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20  at the very end 
14834 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
14835 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65  grows toward the
14836 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  .** beginning of
14837 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
14838 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69   Unused space wi
14839 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  thin the cell co
1483a 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f  ntent area is co
1483b 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c  llected into a l
1483c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a  inked list of.**
1483d 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61   freeblocks.  Ea
1483e 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20  ch freeblock is 
1483f 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
14840 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62   in size.  The b
14841 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f  yte offset.** to
14842 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62   the first freeb
14843 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e  lock is given in
14844 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72   the header.  Fr
14845 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69  eeblocks occur i
14846 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20  n.** increasing 
14847 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20  order.  Because 
14848 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74  a freeblock must
14849 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62   be at least 4 b
1484a 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a  ytes in size,.**
1484b 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20   any group of 3 
1484c 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20  or fewer unused 
1484d 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c  bytes in the cel
1484e 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63  l content area c
1484f 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f  annot.** exist o
14850 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20  n the freeblock 
14851 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20  chain.  A group 
14852 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72  of 3 or fewer fr
14853 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c  ee bytes is call
14854 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74  ed.** a fragment
14855 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  .  The total num
14856 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
14857 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73  all fragments is
14858 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e   recorded..** in
14859 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
1485a 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a   at offset 7..**
1485b 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
1485c 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
1485d 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66     2     Byte of
1485e 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74  fset of the next
1485f 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20   freeblock.**   
14860 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69     2     Bytes i
14861 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b  n this freeblock
14862 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65  .**.** Cells are
14863 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e   of variable len
14864 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20  gth.  Cells are 
14865 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65  stored in the ce
14866 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
14867 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
14868 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e   the page.  Poin
14869 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c  ters to the cell
1486a 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c  s are in the cel
1486b 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a  l pointer array.
1486c 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74  ** that immediat
1486d 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ely follows the 
1486e 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65  page header.  Ce
1486f 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  lls is not neces
14870 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67  sarily.** contig
14871 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72  uous or in order
14872 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74  , but cell point
14873 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  ers are contiguo
14874 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e  us and in order.
14875 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74  .**.** Cell cont
14876 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66  ent makes use of
14877 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
14878 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61   integers.  A va
14879 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68  riable.** length
1487a 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f   integer is 1 to
1487b 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74   9 bytes where t
1487c 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20  he lower 7 bits 
1487d 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65  of each .** byte
1487e 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20   are used.  The 
1487f 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73  integer consists
14880 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68   of all bytes th
14881 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65  at have bit 8 se
14882 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72  t and.** the fir
14883 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74  st byte with bit
14884 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d   8 clear.  The m
14885 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ost significant 
14886 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65  byte of the inte
14887 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66  ger.** appears f
14888 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c  irst.  A variabl
14889 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
1488a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65   may not be more
1488b 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f   than 9 bytes lo
1488c 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63  ng..** As a spec
1488d 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20  ial case, all 8 
1488e 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68  bytes of the 9th
1488f 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61   byte are used a
14890 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a  s data.  This.**
14891 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74   allows a 64-bit
14892 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65   integer to be e
14893 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65  ncoded in 9 byte
14894 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30  s..**.**    0x00
14895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14896 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
14897 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20  x00000000.**    
14898 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
14899 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
1489a 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a  s  0x0000007f.**
1489b 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20      0x81 0x00   
1489c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
1489d 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38  comes  0x0000008
1489e 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30  0.**    0x82 0x0
1489f 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
148a0 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
148a1 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30  00100.**    0x80
148a2 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20   0x7f           
148a3 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
148a4 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
148a5 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30  0x8a 0x91 0xd1 0
148a6 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65  xac 0x78  become
148a7 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a  s  0x12345678.**
148a8 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78      0x81 0x81 0x
148a9 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65  81 0x81 0x01  be
148aa 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38  comes  0x1020408
148ab 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65  1.**.** Variable
148ac 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73   length integers
148ad 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f   are used for ro
148ae 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64  wids and to hold
148af 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
148b0 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61  * bytes of key a
148b1 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72  nd data in a btr
148b2 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ee cell..**.** T
148b3 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
148b4 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  cell looks like 
148b5 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
148b6 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
148b7 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
148b8 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
148b9 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20  the left child. 
148ba 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20  Omitted if leaf 
148bb 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
148bc 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65      var    Numbe
148bd 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
148be 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74  ta. Omitted if t
148bf 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67  he zerodata flag
148c0 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
148c1 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
148c2 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f   bytes of key. O
148c3 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66  r the key itself
148c4 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20   if intkey flag 
148c5 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20  is set..**      
148c6 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a  *     Payload.**
148c7 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
148c8 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76  t page of the ov
148c9 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f  erflow chain.  O
148ca 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65  mitted if no ove
148cb 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72  rflow.**.** Over
148cc 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20  flow pages form 
148cd 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20  a linked list.  
148ce 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74  Each page except
148cf 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d   the last is com
148d0 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65  pletely.** fille
148d1 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67  d with data (pag
148d2 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29  esize - 4 bytes)
148d3 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65  .  The last page
148d4 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74   can have as lit
148d5 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65  tle.** as 1 byte
148d6 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20   of data..**.** 
148d7 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
148d8 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
148d9 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
148da 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f   of next overflo
148db 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  w page.**      *
148dc 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20       Data.**.** 
148dd 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63  Freelist pages c
148de 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79  ome in two subty
148df 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73  pes: trunk pages
148e0 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e   and leaf pages.
148e1 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65    The.** file he
148e2 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ader points to t
148e3 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69  he first in a li
148e4 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75  nked list of tru
148e5 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74  nk page.  Each t
148e6 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69  runk.** page poi
148e7 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20  nts to multiple 
148e8 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
148e9 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65   content of a le
148ea 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e  af page is.** un
148eb 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72  specified.  A tr
148ec 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c  unk page looks l
148ed 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
148ee 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
148ef 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
148f0 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
148f1 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70   of next trunk p
148f2 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  age.**      4   
148f3 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66    Number of leaf
148f4 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69   pointers on thi
148f5 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  s page.**      *
148f6 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72       zero or mor
148f7 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  e pages numbers 
148f8 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f  of leaves.*/.../
148f9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
148fa 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78  value is the max
148fb 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61  imum cell size a
148fc 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75  ssuming a maximu
148fd 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67  m page.** size g
148fe 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64  ive above..*/.#d
148ff 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49  efine MX_CELL_SI
14900 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70  ZE(pBt)  (pBt->p
14901 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54  ageSize-8)../* T
14902 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
14903 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20  r of cells on a 
14904 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74  single page of t
14905 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
14906 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20  is.** assumes a 
14907 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  minimum cell siz
14908 65 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34  e of 6 bytes  (4
14909 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63   bytes for the c
1490a 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c  ell itself.** pl
1490b 75 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74  us 2 bytes for t
1490c 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20  he index to the 
1490d 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65  cell in the page
1490e 20 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a   header).  Such.
1490f 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77  ** small cells w
14910 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74  ill be rare, but
14911 20 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62   they are possib
14912 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  le..*/.#define M
14913 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42  X_CELL(pBt) ((pB
14914 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36  t->pageSize-8)/6
14915 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  )../* Forward de
14916 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79  clarations */.ty
14917 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
14918 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79  Page MemPage;.ty
14919 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c  pedef struct BtL
1491a 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a  ock BtLock;../*.
1491b 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67  ** This is a mag
1491c 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61  ic string that a
1491d 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65  ppears at the be
1491e 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79  ginning of every
1491f 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62  .** SQLite datab
14920 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ase in order to 
14921 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c  identify the fil
14922 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61  e as a real data
14923 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20  base..**.** You 
14924 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20  can change this 
14925 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65  value at compile
14926 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79  -time by specify
14927 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54  ing a.** -DSQLIT
14928 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e  E_FILE_HEADER=".
14929 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  .." on the compi
1492a 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65  ler command-line
1492b 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72  .  The.** header
1492c 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79   must be exactly
1492d 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64   16 bytes includ
1492e 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72  ing the zero-ter
1492f 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68  minator so.** th
14930 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20  e string itself 
14931 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61  should be 15 cha
14932 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49  racters long.  I
14933 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20  f you change.** 
14934 74 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e  the header, then
14935 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62   your custom lib
14936 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rary will not be
14937 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a   able to read .*
14938 2a 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65  * databases gene
14939 72 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61  rated by the sta
1493a 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20  ndard tools and 
1493b 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f  the standard too
1493c 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  ls.** will not b
1493d 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64  e able to read d
1493e 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64  atabases created
1493f 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20   by your custom 
14940 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e  library..*/.#ifn
14941 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f  def SQLITE_FILE_
14942 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36  HEADER /* 123456
14943 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20  789 123456 */.# 
14944 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46   define SQLITE_F
14945 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69  ILE_HEADER "SQLi
14946 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e  te format 3".#en
14947 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20  dif../*.** Page 
14948 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20  type flags.  An 
14949 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  ORed combination
1494a 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20   of these flags 
1494b 61 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a  appear as the.**
1494c 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f   first byte of o
1494d 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20  n-disk image of 
1494e 65 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65  every BTree page
1494f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46  ..*/.#define PTF
14950 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a  _INTKEY    0x01.
14951 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f  #define PTF_ZERO
14952 44 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69  DATA  0x02.#defi
14953 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20  ne PTF_LEAFDATA 
14954 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54   0x04.#define PT
14955 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38  F_LEAF      0x08
14956 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20  ../*.** As each 
14957 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65  page of the file
14958 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20   is loaded into 
14959 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61  memory, an insta
1495a 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
1495b 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
1495c 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e  e is appended an
1495d 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  d initialized to
1495e 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72   zero.  This str
1495f 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a  ucture stores.**
14960 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
14961 75 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74  ut the page that
14962 20 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d   is decoded from
14963 20 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61   the raw file pa
14964 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50  ge..**.** The pP
14965 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e  arent field poin
14966 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70  ts back to the p
14967 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69  arent page.  Thi
14968 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a  s allows us to.*
14969 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54  * walk up the BT
1496a 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61  ree from any lea
1496b 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20  f to the root.  
1496c 43 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b  Care must be tak
1496d 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29  en to.** unref()
1496e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1496f 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68   pointer when th
14970 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f  is page is no lo
14971 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e  nger referenced.
14972 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74  .** The pageDest
14973 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65  ructor() routine
14974 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68   handles that ch
14975 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73  ore..**.** Acces
14976 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20  s to all fields 
14977 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
14978 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20  e is controlled 
14979 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20  by the mutex.** 
1497a 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67  stored in MemPag
1497b 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f  e.pBt->mutex..*/
1497c 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20  .struct MemPage 
1497d 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20  {.  u8 isInit;  
1497e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1497f 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69   if previously i
14980 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54  nitialized. MUST
14981 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20   BE FIRST! */.  
14982 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20  u8 nOverflow;   
14983 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14984 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  f overflow cell 
14985 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b  bodies in aCell[
14986 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79  ] */.  u8 intKey
14987 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
14988 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c  rue if intkey fl
14989 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75  ag is set */.  u
1498a 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20  8 leaf;         
1498b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c      /* True if l
1498c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20  eaf flag is set 
1498d 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b  */.  u8 hasData;
1498e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1498f 65 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73  e if this page s
14990 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20  tores data */.  
14991 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20  u8 hdrOffset;   
14992 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20       /* 100 for 
14993 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72  page 1.  0 other
14994 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69  wise */.  u8 chi
14995 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f  ldPtrSize;     /
14996 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20  * 0 if leaf==1. 
14997 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f   4 if leaf==0 */
14998 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b  .  u16 maxLocal;
14999 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
1499a 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c  of BtShared.maxL
1499b 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64  ocal or BtShared
1499c 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31  .maxLeaf */.  u1
1499d 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20  6 minLocal;     
1499e 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74     /* Copy of Bt
1499f 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20  Shared.minLocal 
149a0 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c  or BtShared.minL
149a1 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c  eaf */.  u16 cel
149a2 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  lOffset;      /*
149a3 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20   Index in aData 
149a4 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  of first cell po
149a5 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e  inter */.  u16 n
149a6 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  Free;           
149a7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65  /* Number of fre
149a8 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70  e bytes on the p
149a9 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65  age */.  u16 nCe
149aa 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ll;           /*
149ab 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
149ac 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c   on this page, l
149ad 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f  ocal and ovfl */
149ae 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b  .  u16 maskPage;
149af 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
149b0 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20  for page offset 
149b1 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66  */.  struct _Ovf
149b2 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c  lCell {   /* Cel
149b3 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74  ls that will not
149b4 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20   fit on aData[] 
149b5 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c  */.    u8 *pCell
149b6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ;          /* Po
149b7 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f  inters to the bo
149b8 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c  dy of the overfl
149b9 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75  ow cell */.    u
149ba 31 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  16 idx;         
149bb 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69     /* Insert thi
149bc 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64  s cell before id
149bd 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f  x-th non-overflo
149be 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f  w cell */.  } aO
149bf 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72  vfl[5];.  BtShar
149c0 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f  ed *pBt;       /
149c1 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53  * Pointer to BtS
149c2 68 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20  hared that this 
149c3 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20  page is part of 
149c4 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20  */.  u8 *aData; 
149c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
149c6 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61  nter to disk ima
149c7 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64  ge of the page d
149c8 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20  ata */.  DbPage 
149c9 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a  *pDbPage;     /*
149ca 20 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64   Pager page hand
149cb 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  le */.  Pgno pgn
149cc 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  o;           /* 
149cd 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  Page number for 
149ce 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a  this page */.};.
149cf 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65  ./*.** The in-me
149d0 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20  mory image of a 
149d1 64 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68  disk page has th
149d2 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f  e auxiliary info
149d3 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64  rmation appended
149d4 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20  .** to the end. 
149d5 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74   EXTRA_SIZE is t
149d6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
149d7 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64  es of space need
149d8 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68  ed to hold.** th
149d9 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61  at extra informa
149da 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
149db 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65   EXTRA_SIZE size
149dc 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a  of(MemPage)../*.
149dd 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74  ** A linked list
149de 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
149df 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73 20  g structures is 
149e0 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61 72  stored at BtShar
149e1 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63  ed.pLock..** Loc
149e2 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f 72  ks are added (or
149e3 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52   upgraded from R
149e4 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54  EAD_LOCK to WRIT
149e5 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63  E_LOCK) when a c
149e6 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65  ursor .** is ope
149e7 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ned on the table
149e8 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20   with root page 
149e9 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e  BtShared.iTable.
149ea 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76   Locks are remov
149eb 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20  ed.** from this 
149ec 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e  list when a tran
149ed 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
149ee 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62  tted or rolled b
149ef 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20  ack, or when.** 
149f0 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69  a btree handle i
149f1 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72  s closed..*/.str
149f2 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42  uct BtLock {.  B
149f3 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20  tree *pBtree;   
149f4 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61       /* Btree ha
149f5 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69  ndle holding thi
149f6 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f  s lock */.  Pgno
149f7 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
149f8 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
149f9 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20  f table */.  u8 
149fa 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  eLock;          
149fb 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20     /* READ_LOCK 
149fc 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f  or WRITE_LOCK */
149fd 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74  .  BtLock *pNext
149fe 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  ;        /* Next
149ff 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f   in BtShared.pLo
14a00 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  ck list */.};../
14a01 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75  * Candidate valu
14a02 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c  es for BtLock.eL
14a03 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ock */.#define R
14a04 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23  EAD_LOCK     1.#
14a05 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43  define WRITE_LOC
14a06 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72  K    2../* A Btr
14a07 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20  ee handle.**.** 
14a08 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  A database conne
14a09 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61  ction contains a
14a0a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
14a0b 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
14a0c 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76  is object for ev
14a0d 65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ery database fil
14a0e 65 20 74 68 61 74 20 69 74 20 68 61 73 20 6f 70  e that it has op
14a0f 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  en.  This struct
14a10 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65  ure.** is opaque
14a11 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
14a12 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
14a13 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14a14 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20  ction cannot.** 
14a15 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  see the internal
14a16 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
14a17 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61  ure and only dea
14a18 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73  ls with pointers
14a19 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75   to.** this stru
14a1a 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  cture..**.** For
14a1b 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66   some database f
14a1c 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75  iles, the same u
14a1d 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61  nderlying databa
14a1e 73 65 20 63 61 63 68 65 20 6d 69 67 68 74 20 62  se cache might b
14a1f 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74  e .** shared bet
14a20 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f  ween multiple co
14a21 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74  nnections.  In t
14a22 68 61 74 20 63 61 73 65 2c 20 65 61 63 68 20 63  hat case, each c
14a23 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73  onnection.** has
14a24 20 69 74 20 6f 77 6e 20 69 6e 73 74 61 6e 63 65   it own instance
14a25 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e   of this object.
14a26 20 20 42 75 74 20 65 61 63 68 20 69 6e 73 74 61    But each insta
14a27 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
14a28 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20  ct.** points to 
14a29 74 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65  the same BtShare
14a2a 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64  d object.  The d
14a2b 61 74 61 62 61 73 65 20 63 61 63 68 65 20 61 6e  atabase cache an
14a2c 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20  d the.** schema 
14a2d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
14a2e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
14a2f 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69  e are all contai
14a30 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68  ned within.** th
14a31 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
14a32 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65  t..**.** All fie
14a33 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lds in this stru
14a34 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73  cture are access
14a35 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33  ed under sqlite3
14a36 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70  .mutex..** The p
14a37 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c  Bt pointer itsel
14a38 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61  f may not be cha
14a39 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65 72 65  nged while there
14a3a 20 65 78 69 73 74 73 20 63 75 72 73 6f 72 73 20   exists cursors 
14a3b 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72  .** in the refer
14a3c 65 6e 63 65 64 20 42 74 53 68 61 72 65 64 20 74  enced BtShared t
14a3d 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74  hat point back t
14a3e 6f 20 74 68 69 73 20 42 74 72 65 65 20 73 69 6e  o this Btree sin
14a3f 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73  ce those.** curs
14a40 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67  ors have to do g
14a41 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73 20 42  o through this B
14a42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65  tree to find the
14a43 69 72 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a  ir BtShared and.
14a44 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f  ** they often do
14a45 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64   so without hold
14a46 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65  ing sqlite3.mute
14a47 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72  x..*/.struct Btr
14a48 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ee {.  sqlite3 *
14a49 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  db;       /* The
14a4a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14a4b 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69  tion holding thi
14a4c 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53  s btree */.  BtS
14a4d 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
14a4e 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74  /* Sharable cont
14a4f 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74 72 65  ent of this btre
14a50 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e  e */.  u8 inTran
14a51 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41  s;        /* TRA
14a52 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52  NS_NONE, TRANS_R
14a53 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49  EAD or TRANS_WRI
14a54 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61  TE */.  u8 shara
14a55 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  ble;       /* Tr
14a56 75 65 20 69 66 20 77 65 20 63 61 6e 20 73 68 61  ue if we can sha
14a57 72 65 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74  re pBt with anot
14a58 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c  her db */.  u8 l
14a59 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f  ocked;         /
14a5a 2a 20 54 72 75 65 20 69 66 20 64 62 20 63 75 72  * True if db cur
14a5b 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c  rently has pBt l
14a5c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77  ocked */.  int w
14a5d 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a  antToLock;    /*
14a5e 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
14a5f 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  d calls to sqlit
14a60 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a  e3BtreeEnter() *
14a61 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b  /.  int nBackup;
14a62 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14a63 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61   of backup opera
14a64 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68  tions reading th
14a65 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74  is btree */.  Bt
14a66 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  ree *pNext;     
14a67 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65   /* List of othe
14a68 72 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65  r sharable Btree
14a69 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  s from the same 
14a6a 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70  db */.  Btree *p
14a6b 50 72 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61  Prev;      /* Ba
14a6c 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68  ck pointer of th
14a6d 65 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23  e same list */.#
14a6e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14a6f 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14a70 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20    BtLock lock;  
14a71 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75       /* Object u
14a72 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65  sed to lock page
14a73 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a   1 */.#endif.};.
14a74 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54  ./*.** Btree.inT
14a75 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e  rans may take on
14a76 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
14a77 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  ng values..**.**
14a78 20 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64   If the shared-d
14a79 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73  ata extension is
14a7a 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20   enabled, there 
14a7b 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20  may be multiple 
14a7c 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20  users.** of the 
14a7d 42 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e  Btree structure.
14a7e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20   At most one of 
14a7f 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61  these may open a
14a80 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
14a81 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e  on,.** but any n
14a82 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61  umber may have a
14a83 63 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73  ctive read trans
14a84 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66  actions..*/.#def
14a85 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20  ine TRANS_NONE  
14a86 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f  0.#define TRANS_
14a87 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20  READ  1.#define 
14a88 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f  TRANS_WRITE 2../
14a89 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
14a8a 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
14a8b 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e  represents a sin
14a8c 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
14a8d 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67  e..** .** A sing
14a8e 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
14a8f 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61   can be in use a
14a90 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  s the same time 
14a91 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72  by two.** or mor
14a92 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14a93 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77  ctions.  When tw
14a94 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63  o or more connec
14a95 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61  tions are.** sha
14a96 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  ring the same da
14a97 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63  tabase file, eac
14a98 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73  h connection has
14a99 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61   it own.** priva
14a9a 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20  te Btree object 
14a9b 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64  for the file and
14a9c 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42   each of those B
14a9d 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20  trees points.** 
14a9e 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68  to this one BtSh
14a9f 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74  ared object.  Bt
14aa0 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74  Shared.nRef is t
14aa1 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
14aa2 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72  connections curr
14aa3 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68  ently sharing th
14aa4 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  is database file
14aa5 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69  ..**.** Fields i
14aa6 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
14aa7 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
14aa8 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  der the BtShared
14aa9 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c  .mutex.** mutex,
14aaa 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66   except for nRef
14aab 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68   and pNext which
14aac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
14aad 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61  der the.** globa
14aae 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  l SQLITE_MUTEX_S
14aaf 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
14ab0 65 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20  ex.  The pPager 
14ab1 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74  field.** may not
14ab2 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63   be modified onc
14ab3 65 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c  e it is initiall
14ab4 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73  y set as long as
14ab5 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20   nRef>0..** The 
14ab6 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61  pSchema field ma
14ab7 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e  y be set once un
14ab8 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74  der BtShared.mut
14ab9 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61  ex and.** therea
14aba 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65  fter is unchange
14abb 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65  d as long as nRe
14abc 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e  f>0..**.** isPen
14abd 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66  ding:.**.**   If
14abe 20 61 20 42 74 53 68 61 72 65 64 20 63 6c 69 65   a BtShared clie
14abf 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61  nt fails to obta
14ac0 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  in a write-lock 
14ac1 6f 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  on a database.**
14ac2 20 20 20 74 61 62 6c 65 20 28 62 65 63 61 75 73     table (becaus
14ac3 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f  e there exists o
14ac4 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d  ne or more read-
14ac5 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62  locks on the tab
14ac6 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68  le),.**   the sh
14ac7 61 72 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72  ared-cache enter
14ac8 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27  s 'pending-lock'
14ac9 20 73 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e   state and isPen
14aca 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74  ding is.**   set
14acb 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   to true..**.** 
14acc 20 20 54 68 65 20 73 68 61 72 65 64 2d 63 61 63    The shared-cac
14acd 68 65 20 6c 65 61 76 65 73 20 74 68 65 20 27 70  he leaves the 'p
14ace 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61  ending lock' sta
14acf 74 65 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f  te when either o
14ad0 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f  f.**   the follo
14ad1 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a  wing occur:.**.*
14ad2 2a 20 20 20 20 20 31 29 20 54 68 65 20 63 75 72  *     1) The cur
14ad3 72 65 6e 74 20 77 72 69 74 65 72 20 28 42 74 53  rent writer (BtS
14ad4 68 61 72 65 64 2e 70 57 72 69 74 65 72 29 20 63  hared.pWriter) c
14ad5 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 74 72 61  oncludes its tra
14ad6 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20  nsaction, OR.** 
14ad7 20 20 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65      2) The numbe
14ad8 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20  r of locks held 
14ad9 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  by other connect
14ada 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65  ions drops to ze
14adb 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c  ro..**.**   whil
14adc 65 20 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e  e in the 'pendin
14add 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e  g-lock' state, n
14ade 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79  o connection may
14adf 20 73 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20   start a new.** 
14ae0 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a    transaction..*
14ae1 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74  *.**   This feat
14ae2 75 72 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20  ure is included 
14ae3 74 6f 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20  to help prevent 
14ae4 77 72 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f  writer-starvatio
14ae5 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53  n..*/.struct BtS
14ae6 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20  hared {.  Pager 
14ae7 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20  *pPager;        
14ae8 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68  /* The page cach
14ae9 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
14aea 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db;          /* 
14aeb 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
14aec 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73  ion currently us
14aed 69 6e 67 20 74 68 69 73 20 42 74 72 65 65 20 2a  ing this Btree *
14aee 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
14aef 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c  ursor;    /* A l
14af0 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20  ist of all open 
14af1 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d  cursors */.  Mem
14af2 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20  Page *pPage1;   
14af3 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
14af4 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
14af5 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c   */.  u8 readOnl
14af6 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  y;          /* T
14af7 72 75 65 20 69 66 20 74 68 65 20 75 6e 64 65 72  rue if the under
14af8 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65  lying file is re
14af9 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70  adonly */.  u8 p
14afa 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20  ageSizeFixed;   
14afb 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
14afc 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e   page size can n
14afd 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e  o longer be chan
14afe 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ged */.#ifndef S
14aff 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
14b00 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56  ACUUM.  u8 autoV
14b01 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a  acuum;        /*
14b02 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61   True if auto-va
14b03 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20  cuum is enabled 
14b04 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75  */.  u8 incrVacu
14b05 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  um;        /* Tr
14b06 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75  ue if incr-vacuu
14b07 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a  m is enabled */.
14b08 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67  #endif.  u16 pag
14b09 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f  eSize;         /
14b0a 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
14b0b 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67  f bytes on a pag
14b0c 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c  e */.  u16 usabl
14b0d 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20  eSize;       /* 
14b0e 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  Number of usable
14b0f 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70   bytes on each p
14b10 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78  age */.  u16 max
14b11 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f  Local;         /
14b12 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Maximum local 
14b13 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c  payload in non-L
14b14 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a  EAFDATA tables *
14b15 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c  /.  u16 minLocal
14b16 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e  ;         /* Min
14b17 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f  imum local paylo
14b18 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41  ad in non-LEAFDA
14b19 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75  TA tables */.  u
14b1a 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20  16 maxLeaf;     
14b1b 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
14b1c 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
14b1d 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c   a LEAFDATA tabl
14b1e 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65  e */.  u16 minLe
14b1f 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  af;          /* 
14b20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Minimum local pa
14b21 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44  yload in a LEAFD
14b22 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75  ATA table */.  u
14b23 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b  8 inTransaction;
14b24 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74       /* Transact
14b25 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69  ion state */.  i
14b26 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b  nt nTransaction;
14b27 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14b28 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69  f open transacti
14b29 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74  ons (read + writ
14b2a 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  e) */.  void *pS
14b2b 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a  chema;        /*
14b2c 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63   Pointer to spac
14b2d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  e allocated by s
14b2e 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
14b2f 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  a() */.  void (*
14b30 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69  xFreeSchema)(voi
14b31 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63  d*);  /* Destruc
14b32 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64  tor for BtShared
14b33 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71  .pSchema */.  sq
14b34 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
14b35 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72  ex; /* Non-recur
14b36 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69  sive mutex requi
14b37 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  red to access th
14b38 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42  is struct */.  B
14b39 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65  itvec *pHasConte
14b3a 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70  nt;  /* Set of p
14b3b 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72  ages moved to fr
14b3c 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61  ee-list this tra
14b3d 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e  nsaction */.#ifn
14b3e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14b3f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
14b40 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
14b41 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14b42 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
14b43 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
14b44 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e  /.  BtShared *pN
14b45 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
14b46 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73  t on a list of s
14b47 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64  harable BtShared
14b48 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74   structs */.  Bt
14b49 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20  Lock *pLock;    
14b4a 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c      /* List of l
14b4b 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69  ocks held on thi
14b4c 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73  s shared-btree s
14b4d 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65  truct */.  Btree
14b4e 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20   *pWriter;      
14b4f 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63   /* Btree with c
14b50 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72  urrently open wr
14b51 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
14b52 2a 2f 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73  */.  u8 isExclus
14b53 69 76 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  ive;       /* Tr
14b54 75 65 20 69 66 20 70 57 72 69 74 65 72 20 68 61  ue if pWriter ha
14b55 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  s an EXCLUSIVE l
14b56 6f 63 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f  ock on the db */
14b57 0a 20 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b  .  u8 isPending;
14b58 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77           /* If w
14b59 61 69 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d  aiting for read-
14b5a 6c 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a  locks to clear *
14b5b 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70  /.#endif.  u8 *p
14b5c 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20  TmpSpace;       
14b5d 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67   /* BtShared.pag
14b5e 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73  eSize bytes of s
14b5f 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65  pace for tmp use
14b60 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
14b61 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
14b62 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
14b63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
14b64 68 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hold information
14b65 0a 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c  .** about a cell
14b66 2e 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c  .  The parseCell
14b67 50 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  Ptr() function f
14b68 69 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72  ills in this str
14b69 75 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20  ucture.** based 
14b6a 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65  on information e
14b6b 78 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20  xtract from the 
14b6c 72 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a  raw disk page..*
14b6d 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
14b6e 20 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e   CellInfo CellIn
14b6f 66 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49  fo;.struct CellI
14b70 6e 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c  nfo {.  u8 *pCel
14b71 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  l;     /* Pointe
14b72 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  r to the start o
14b73 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a  f cell content *
14b74 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20  /.  i64 nKey;   
14b75 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f     /* The key fo
14b76 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c  r INTKEY tables,
14b77 20 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79   or number of by
14b78 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20  tes in key */.  
14b79 75 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f  u32 nData;     /
14b7a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
14b7b 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75  s of data */.  u
14b7c 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a  32 nPayload;  /*
14b7d 20 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66   Total amount of
14b7e 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31   payload */.  u1
14b7f 36 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20  6 nHeader;   /* 
14b80 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  Size of the cell
14b81 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20   content header 
14b82 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31  in bytes */.  u1
14b83 36 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20  6 nLocal;    /* 
14b84 41 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61  Amount of payloa
14b85 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a  d held locally *
14b86 2f 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f  /.  u16 iOverflo
14b87 77 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20  w; /* Offset to 
14b88 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75  overflow page nu
14b89 6d 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e  mber.  Zero if n
14b8a 6f 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20  o overflow */.  
14b8b 75 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f  u16 nSize;     /
14b8c 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65  * Size of the ce
14b8d 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68  ll content on th
14b8e 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61  e main b-tree pa
14b8f 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ge */.};../*.** 
14b90 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  Maximum depth of
14b91 20 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65   an SQLite B-Tre
14b92 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79  e structure. Any
14b93 20 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74   B-Tree deeper t
14b94 68 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c  han.** this will
14b95 20 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72   be declared cor
14b96 72 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65  rupt. This value
14b97 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62   is calculated b
14b98 61 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78  ased on a.** max
14b99 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69  imum database si
14b9a 7a 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73  ze of 2^31 pages
14b9b 20 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75   a minimum fanou
14b9c 74 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20  t of 2 for a.** 
14b9d 72 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20  root-node and 3 
14b9e 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e  for all other in
14b9f 74 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a  ternal nodes..**
14ba0 0a 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68  .** If a tree th
14ba1 61 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  at appears to be
14ba2 20 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69   taller than thi
14ba3 73 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  s is encountered
14ba4 2c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d  , it is.** assum
14ba5 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ed that the data
14ba6 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e  base is corrupt.
14ba7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55  .*/.#define BTCU
14ba8 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32  RSOR_MAX_DEPTH 2
14ba9 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f  0../*.** A curso
14baa 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
14bab 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65  o a particular e
14bac 6e 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61  ntry within a pa
14bad 72 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72  rticular.** b-tr
14bae 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61  ee within a data
14baf 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
14bb0 20 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64   The entry is id
14bb1 65 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20  entified by its 
14bb2 4d 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20  MemPage and the 
14bb3 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50  index in.** MemP
14bb4 61 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74  age.aCell[] of t
14bb5 68 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20  he entry..**.** 
14bb6 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  A single databas
14bb7 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65  e file can share
14bb8 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61  d by two more da
14bb9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14bba 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f  ns,.** but curso
14bbb 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61  rs cannot be sha
14bbc 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f  red.  Each curso
14bbd 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  r is associated 
14bbe 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63  with a.** partic
14bbf 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
14bc0 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
14bc1 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
14bc2 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69  ree.db..**.** Fi
14bc3 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
14bc4 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
14bc5 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
14bc6 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
14bc7 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70  found at self->p
14bc8 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73  Bt->mutex. .*/.s
14bc9 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b  truct BtCursor {
14bca 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
14bcb 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
14bcc 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69  The Btree to whi
14bcd 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ch this cursor b
14bce 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68  elongs */.  BtSh
14bcf 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
14bd0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53        /* The BtS
14bd1 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f  hared this curso
14bd2 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  r points to */. 
14bd3 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74   BtCursor *pNext
14bd4 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f  , *pPrev;  /* Fo
14bd5 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  rms a linked lis
14bd6 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73  t of all cursors
14bd7 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
14bd8 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
14bd9 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73  /* Argument pass
14bda 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  ed to comparison
14bdb 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50   function */.  P
14bdc 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20  gno pgnoRoot;   
14bdd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
14bde 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69  root page of thi
14bdf 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69  s tree */.  sqli
14be0 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64  te3_int64 cached
14be1 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72  Rowid; /* Next r
14be2 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d  owid cache.  0 m
14be3 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a  eans not valid *
14be4 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  /.  CellInfo inf
14be5 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
14be6 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20   A parse of the 
14be7 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e  cell we are poin
14be8 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20  ting at */.  u8 
14be9 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20  wrFlag;         
14bea 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14beb 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20  f writable */.  
14bec 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20  u8 atLast;      
14bed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
14bee 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
14bef 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a  the last entry *
14bf0 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79  /.  u8 validNKey
14bf1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
14bf2 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b   True if info.nK
14bf3 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  ey is valid */. 
14bf4 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20   u8 eState;     
14bf5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
14bf6 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f  e of the CURSOR_
14bf7 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73  XXX constants (s
14bf8 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76  ee below) */.  v
14bf9 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  oid *pKey;      
14bfa 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61  /* Saved key tha
14bfb 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c  t was cursor's l
14bfc 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69  ast known positi
14bfd 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79  on */.  i64 nKey
14bfe 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ;        /* Size
14bff 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73   of pKey, or las
14c00 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f  t integer key */
14c01 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b  .  int skipNext;
14c02 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73      /* Prev() is
14c03 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76   noop if negativ
14c04 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f  e. Next() is noo
14c05 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f  p if positive */
14c06 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14c07 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
14c08 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  u8 isIncrblobHan
14c09 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dle;      /* Tru
14c0a 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72  e if this cursor
14c0b 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20   is an incr. io 
14c0c 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
14c0d 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20   *aOverflow;    
14c0e 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f        /* Cache o
14c0f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  f overflow page 
14c10 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e  locations */.#en
14c11 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b  dif.  i16 iPage;
14c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c13 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
14c14 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20  ndex of current 
14c15 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a  page in apPage *
14c16 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50  /.  MemPage *apP
14c17 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58  age[BTCURSOR_MAX
14c18 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67  _DEPTH];  /* Pag
14c19 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20  es from root to 
14c1a 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a  current page */.
14c1b 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55    u16 aiIdx[BTCU
14c1c 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b  RSOR_MAX_DEPTH];
14c1d 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
14c1e 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61  nt index in apPa
14c1f 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ge[i] */.};../*.
14c20 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c  ** Potential val
14c21 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72  ues for BtCursor
14c22 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43  .eState..**.** C
14c23 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20  URSOR_VALID:.** 
14c24 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20    Cursor points 
14c25 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79  to a valid entry
14c26 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65  . getPayload() e
14c27 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  tc. may be calle
14c28 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  d..**.** CURSOR_
14c29 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75  INVALID:.**   Cu
14c2a 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f  rsor does not po
14c2b 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65  int to a valid e
14c2c 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68  ntry. This can h
14c2d 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70  appen (for examp
14c2e 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73  le) .**   becaus
14c2f 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  e the table is e
14c30 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20  mpty or because 
14c31 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74  BtreeCursorFirst
14c32 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a  () has not been.
14c33 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  **   called..**.
14c34 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  ** CURSOR_REQUIR
14c35 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20  ESEEK:.**   The 
14c36 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20  table that this 
14c37 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
14c38 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74  d on still exist
14c39 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20  s, but has been 
14c3a 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73  .**   modified s
14c3b 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20  ince the cursor 
14c3c 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54  was last used. T
14c3d 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  he cursor positi
14c3e 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20  on is saved.**  
14c3f 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74   in variables Bt
14c40 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20  Cursor.pKey and 
14c41 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57  BtCursor.nKey. W
14c42 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20  hen a cursor is 
14c43 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74  in .**   this st
14c44 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73  ate, restoreCurs
14c45 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e  orPosition() can
14c46 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74   be called to at
14c47 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65  tempt to.**   se
14c48 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ek the cursor to
14c49 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74   the saved posit
14c4a 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  ion..**.** CURSO
14c4b 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20  R_FAULT:.**   A 
14c4c 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72  unrecoverable er
14c4d 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f  ror (an I/O erro
14c4e 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61  r or a malloc fa
14c4f 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72  ilure) has occur
14c50 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69  red.**   on a di
14c51 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69  fferent connecti
14c52 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74  on that shares t
14c53 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68  he BtShared cach
14c54 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20  e with this.**  
14c55 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72   cursor.  The er
14c56 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65  ror has left the
14c57 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63   cache in an inc
14c58 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e  onsistent state.
14c59 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67  .**   Do nothing
14c5a 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20   else with this 
14c5b 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74  cursor.  Any att
14c5c 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20  empt to use the 
14c5d 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75  cursor.**   shou
14c5e 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  ld return the er
14c5f 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20  ror code stored 
14c60 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70  in BtCursor.skip
14c61 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53  .*/.#define CURS
14c62 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20  OR_INVALID      
14c63 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43       0.#define C
14c64 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20  URSOR_VALID     
14c65 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
14c66 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  e CURSOR_REQUIRE
14c67 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65  SEEK       2.#de
14c68 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c  fine CURSOR_FAUL
14c69 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a  T             3.
14c6a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61  ./* .** The data
14c6b 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45  base page the PE
14c6c 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70  NDING_BYTE occup
14c6d 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69  ies. This page i
14c6e 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f  s never used..*/
14c6f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  .# define PENDIN
14c70 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
14c71 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70   PAGER_MJ_PGNO(p
14c72 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  Bt)../*.** These
14c73 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74   macros define t
14c74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  he location of t
14c75 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
14c76 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64  ntry for a .** d
14c77 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68  atabase page. Th
14c78 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
14c79 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20   to each is the 
14c7a 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  number of usable
14c7b 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63  .** bytes on eac
14c7c 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  h page of the da
14c7d 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30  tabase (often 10
14c7e 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  24). The second 
14c7f 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e  is the.** page n
14c80 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70  umber to look up
14c81 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20   in the pointer 
14c82 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  map..**.** PTRMA
14c83 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73  P_PAGENO returns
14c84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
14c85 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
14c86 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20   pointer-map.** 
14c87 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73  page that stores
14c88 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f   the required po
14c89 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54  inter. PTRMAP_PT
14c8a 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a  ROFFSET returns.
14c8b 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  ** the offset of
14c8c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d   the requested m
14c8d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20  ap entry..**.** 
14c8e 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75  If the pgno argu
14c8f 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50  ment passed to P
14c90 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20  TRMAP_PAGENO is 
14c91 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
14c92 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f  ge,.** then pgno
14c93 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f   is returned. So
14c94 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50   (pgno==PTRMAP_P
14c95 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f  AGENO(pgsz, pgno
14c96 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65  )) can be.** use
14c97 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e  d to test if pgn
14c98 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d  o is a pointer-m
14c99 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f  ap page. PTRMAP_
14c9a 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74  ISPAGE implement
14c9b 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a  s.** this test..
14c9c 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41  */.#define PTRMA
14c9d 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67  P_PAGENO(pBt, pg
14c9e 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f  no) ptrmapPageno
14c9f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66  (pBt, pgno).#def
14ca0 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  ine PTRMAP_PTROF
14ca1 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70  FSET(pgptrmap, p
14ca2 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67  gno) (5*(pgno-pg
14ca3 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69  ptrmap-1)).#defi
14ca4 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  ne PTRMAP_ISPAGE
14ca5 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52  (pBt, pgno) (PTR
14ca6 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29  MAP_PAGENO((pBt)
14ca7 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29  ,(pgno))==(pgno)
14ca8 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  )../*.** The poi
14ca9 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f  nter map is a lo
14caa 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20  okup table that 
14cab 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
14cac 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a  arent page for.*
14cad 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67  * each child pag
14cae 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
14caf 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72  e file.  The par
14cb0 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20  ent page is the 
14cb1 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e  page that.** con
14cb2 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
14cb3 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45  to the child.  E
14cb4 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
14cb5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
14cb6 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72  ns.** 0 or 1 par
14cb7 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20  ent pages.  (In 
14cb8 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61  this context 'da
14cb9 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66  tabase page' ref
14cba 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61  ers.** to any pa
14cbb 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  ge that is not p
14cbc 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  art of the point
14cbd 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20  er map itself.) 
14cbe 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61   Each pointer ma
14cbf 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69  p.** entry consi
14cc0 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  sts of a single 
14cc1 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20  byte 'type' and 
14cc2 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20  a 4 byte parent 
14cc3 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
14cc4 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69  The PTRMAP_XXX i
14cc5 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77  dentifiers below
14cc6 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74   are the valid t
14cc7 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ypes..**.** The 
14cc8 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70  purpose of the p
14cc9 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f  ointer map is to
14cca 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67   facility moving
14ccb 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a   pages from one.
14ccc 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  ** position in t
14ccd 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68  he file to anoth
14cce 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75  er as part of au
14ccf 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20  tovacuum.  When 
14cd0 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76  a page.** is mov
14cd1 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ed, the pointer 
14cd2 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75  in its parent mu
14cd3 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f  st be updated to
14cd4 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
14cd5 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20   new location.  
14cd6 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
14cd7 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
14cd8 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
14cd9 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a  e quickly..**.**
14cda 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
14cdb 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14cdc 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61  age is a root-pa
14cdd 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
14cde 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20  ber is not.**   
14cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
14ce0 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  sed in this case
14ce1 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46  ..**.** PTRMAP_F
14ce2 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74  REEPAGE: The dat
14ce3 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e  abase page is an
14ce4 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70   unused (free) p
14ce5 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
14ce6 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  mber .**        
14ce7 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74            is not
14ce8 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61   used in this ca
14ce9 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  se..**.** PTRMAP
14cea 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20  _OVERFLOW1: The 
14ceb 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
14cec 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
14ced 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a  in a list of .**
14cee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cef 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65     overflow page
14cf0 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62  s. The page numb
14cf1 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68  er identifies th
14cf2 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20  e page that.**  
14cf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cf4 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65   contains the ce
14cf5 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  ll with a pointe
14cf6 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c  r to this overfl
14cf7 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  ow page..**.** P
14cf8 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
14cf9 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
14cfa 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  ge is the second
14cfb 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69   or later page i
14cfc 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20  n a list of.**  
14cfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14cfe 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
14cff 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
14d00 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14d01 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20  previous.**     
14d02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
14d03 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c  ge in the overfl
14d04 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a  ow page list..**
14d05 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45  .** PTRMAP_BTREE
14d06 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14d07 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f  age is a non-roo
14d08 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68  t btree page. Th
14d09 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a  e page number.**
14d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
14d0b 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
14d0c 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65  rent page in the
14d0d 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69   btree..*/.#defi
14d0e 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ne PTRMAP_ROOTPA
14d0f 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52  GE 1.#define PTR
14d10 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23  MAP_FREEPAGE 2.#
14d11 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56  define PTRMAP_OV
14d12 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e  ERFLOW1 3.#defin
14d13 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  e PTRMAP_OVERFLO
14d14 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52  W2 4.#define PTR
14d15 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20  MAP_BTREE 5../* 
14d16 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72  A bunch of asser
14d17 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
14d18 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e  o check the tran
14d19 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61  saction state va
14d1a 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61  riables.** of ha
14d1b 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72  ndle p (type Btr
14d1c 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61  ee*) are interna
14d1d 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a  lly consistent..
14d1e 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65  */.#define btree
14d1f 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20  Integrity(p) \. 
14d20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
14d21 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
14d22 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d  TRANS_NONE || p-
14d23 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  >pBt->nTransacti
14d24 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73  on==0 ); \.  ass
14d25 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54  ert( p->pBt->inT
14d26 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69  ransaction>=p->i
14d27 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a  nTrans ); .../*.
14d28 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43  ** The ISAUTOVAC
14d29 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65  UUM macro is use
14d2a 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65  d within balance
14d2b 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65  _nonroot() to de
14d2c 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68  termine.** if th
14d2d 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
14d2e 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20  rts auto-vacuum 
14d2f 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20  or not. Because 
14d30 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69  it is used.** wi
14d31 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69  thin an expressi
14d32 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72  on that is an ar
14d33 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65  gument to anothe
14d34 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c  r macro .** (sql
14d35 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69  iteMallocRaw), i
14d36 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
14d37 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69  e to use conditi
14d38 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  onal compilation
14d39 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61  ..** So, this ma
14d3a 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69  cro is defined i
14d3b 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64  nstead..*/.#ifnd
14d3c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14d3d 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e  UTOVACUUM.#defin
14d3e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28  e ISAUTOVACUUM (
14d3f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
14d40 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49  .#else.#define I
14d41 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65  SAUTOVACUUM 0.#e
14d42 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ndif.../*.** Thi
14d43 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70  s structure is p
14d44 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72  assed around thr
14d45 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e  ough all the san
14d46 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75  ity checking rou
14d47 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65  tines.** in orde
14d48 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20  r to keep track 
14d49 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73  of some global s
14d4a 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
14d4b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
14d4c 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14d4d 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72  IntegrityCk;.str
14d4e 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14d4f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
14d50 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65  t;    /* The tre
14d51 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20  e being checked 
14d52 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a  out */.  Pager *
14d53 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68  pPager;    /* Th
14d54 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67  e associated pag
14d55 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73  er.  Also access
14d56 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61  ible by pBt->pPa
14d57 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50  ger */.  Pgno nP
14d58 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  age;       /* Nu
14d59 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
14d5a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
14d5b 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20  .  int *anRef;  
14d5c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14d5d 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67  f times each pag
14d5e 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20  e is referenced 
14d5f 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20  */.  int mxErr; 
14d60 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61         /* Stop a
14d61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f  ccumulating erro
14d62 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61  rs when this rea
14d63 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69  ches zero */.  i
14d64 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20  nt nErr;        
14d65 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
14d66 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74  ssages written t
14d67 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72  o zErrMsg so far
14d68 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63   */.  int malloc
14d69 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d  Failed; /* A mem
14d6a 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
14d6b 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
14d6c 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20  d */.  StrAccum 
14d6d 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75  errMsg;  /* Accu
14d6e 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72  mulate the error
14d6f 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65   message text he
14d70 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  re */.};../*.** 
14d71 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20  Read or write a 
14d72 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79  two- and four-by
14d73 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
14d74 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f  teger values..*/
14d75 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74  .#define get2byt
14d76 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c  e(x)   ((x)[0]<<
14d77 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66  8 | (x)[1]).#def
14d78 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76  ine put2byte(p,v
14d79 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29  ) ((p)[0] = (u8)
14d7a 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d  ((v)>>8), (p)[1]
14d7b 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66   = (u8)(v)).#def
14d7c 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c  ine get4byte sql
14d7d 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65  ite3Get4byte.#de
14d7e 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71  fine put4byte sq
14d7f 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f  lite3Put4byte../
14d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
14d81 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
14d82 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
14d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
14d85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
14d86 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
14d87 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
14d88 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
14d89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
14d8a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14d8b 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14d8c 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
14d8d 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  DSAFE../*.** Obt
14d8e 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64  ain the BtShared
14d8f 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
14d90 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61  d with B-Tree ha
14d91 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a  ndle p. Also,.**
14d92 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62   set BtShared.db
14d93 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
14d94 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74   handle associat
14d95 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68  ed with p and th
14d96 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62  e.** p->locked b
14d97 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a  oolean to true..
14d98 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
14d99 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74  ockBtreeMutex(Bt
14d9a 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
14d9b 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  t( p->locked==0 
14d9c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14d9d 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
14d9e 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
14d9f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14da1 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
14da2 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
14da3 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74  tex_enter(p->pBt
14da4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70  ->mutex);.  p->p
14da5 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
14da6 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b    p->locked = 1;
14da7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  .}../*.** Releas
14da8 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  e the BtShared m
14da9 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
14daa 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64  with B-Tree hand
14dab 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61  le p and.** clea
14dac 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20  r the p->locked 
14dad 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74  boolean..*/.stat
14dae 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74  ic void unlockBt
14daf 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a  reeMutex(Btree *
14db0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
14db1 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20  >locked==1 );.  
14db2 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14db3 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42  mutex_held(p->pB
14db4 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
14db5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
14db6 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
14db7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
14db8 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70  ert( p->db==p->p
14db9 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c  Bt->db );..  sql
14dba 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
14dbb 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
14dbc 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30  .  p->locked = 0
14dbd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  ;.}../*.** Enter
14dbe 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20   a mutex on the 
14dbf 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65  given BTree obje
14dc0 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ct..**.** If the
14dc1 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73   object is not s
14dc2 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f  harable, then no
14dc3 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72   mutex is ever r
14dc4 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74  equired.** and t
14dc5 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
14dc6 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64   no-op.  The und
14dc7 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73  erlying mutex is
14dc8 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a   non-recursive..
14dc9 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61  ** But we keep a
14dca 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
14dcb 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f   in Btree.wantTo
14dcc 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61  Lock so the beha
14dcd 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20  vior.** of this 
14dce 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63  interface is rec
14dcf 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f  ursive..**.** To
14dd0 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73   avoid deadlocks
14dd1 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65  , multiple Btree
14dd2 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20  s are locked in 
14dd3 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a  the same order.*
14dd4 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73  * by all databas
14dd5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
14dd6 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20  The p->pNext is 
14dd7 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a  a list of other.
14dd8 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67  ** Btrees belong
14dd9 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
14dda 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14ddb 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72  ion as the p Btr
14ddc 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64  ee.** which need
14ddd 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66   to be locked af
14dde 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61  ter p.  If we ca
14ddf 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20  nnot get a lock 
14de0 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69  on.** p, then fi
14de1 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f  rst unlock all o
14de2 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20  f the others on 
14de3 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77  p->pNext, then w
14de4 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c  ait.** for the l
14de5 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76  ock to become av
14de6 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68  ailable on p, th
14de7 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66  en relock all of
14de8 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65   the.** subseque
14de9 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64  nt Btrees that d
14dea 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f  esire a lock..*/
14deb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14dec 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14ded 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29  eEnter(Btree *p)
14dee 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65  {.  Btree *pLate
14def 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61  r;..  /* Some ba
14df0 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b  sic sanity check
14df1 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65  ing on the Btree
14df2 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42  .  The list of B
14df3 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65  trees.  ** conne
14df4 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e  cted by pNext an
14df5 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62  d pPrev should b
14df6 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
14df7 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e  r by.  ** Btree.
14df8 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65  pBt value. All e
14df9 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c  lements of the l
14dfa 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e  ist should belon
14dfb 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61  g to.  ** the sa
14dfc 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f  me connection. O
14dfd 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65  nly shared Btree
14dfe 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73  s are on the lis
14dff 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  t. */.  assert( 
14e00 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  p->pNext==0 || p
14e01 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e  ->pNext->pBt>p->
14e02 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pBt );.  assert(
14e03 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20   p->pPrev==0 || 
14e04 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d  p->pPrev->pBt<p-
14e05 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74  >pBt );.  assert
14e06 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ( p->pNext==0 ||
14e07 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70   p->pNext->db==p
14e08 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14e09 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c  ( p->pPrev==0 ||
14e0a 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70   p->pPrev->db==p
14e0b 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14e0c 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
14e0d 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26   (p->pNext==0 &&
14e0e 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b   p->pPrev==0) );
14e0f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
14e10 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74   locking consist
14e11 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74  ency */.  assert
14e12 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
14e13 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
14e14 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
14e15 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77  sharable || p->w
14e16 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a  antToLock==0 );.
14e17 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20  .  /* We should 
14e18 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c  already hold a l
14e19 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
14e1a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
14e1b 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
14e1c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14e1d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
14e1e 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65  .  /* Unless the
14e1f 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61   database is sha
14e20 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b  rable and unlock
14e21 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65  ed, then BtShare
14e22 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  d.db.  ** should
14e23 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20   already be set 
14e24 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20  correctly. */.  
14e25 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b  assert( (p->lock
14e26 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72  ed==0 && p->shar
14e27 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d  able) || p->pBt-
14e28 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20  >db==p->db );.. 
14e29 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
14e2a 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d  e ) return;.  p-
14e2b 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20  >wantToLock++;. 
14e2c 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29   if( p->locked )
14e2d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49   return;..  /* I
14e2e 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65  n most cases, we
14e2f 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20   should be able 
14e30 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c  to acquire the l
14e31 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74  ock we.  ** want
14e32 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
14e33 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74  to go throught t
14e34 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63  he ascending loc
14e35 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65  k.  ** procedure
14e36 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20   that follows.  
14e37 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74  Just be sure not
14e38 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a   to block..  */.
14e39 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75    if( sqlite3_mu
14e3a 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e  tex_try(p->pBt->
14e3b 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f  mutex)==SQLITE_O
14e3c 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d  K ){.    p->pBt-
14e3d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  >db = p->db;.   
14e3e 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a   p->locked = 1;.
14e3f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
14e40 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64  .  /* To avoid d
14e41 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72  eadlock, first r
14e42 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73  elease all locks
14e43 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20   with a larger. 
14e44 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64   ** BtShared add
14e45 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75  ress.  Then acqu
14e46 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54  ire our lock.  T
14e47 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20  hen reacquire.  
14e48 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53  ** the other BtS
14e49 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74  hared locks that
14e4a 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64   we used to hold
14e4b 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20   in ascending.  
14e4c 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ** order..  */. 
14e4d 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70   for(pLater=p->p
14e4e 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
14e4f 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
14e50 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  xt){.    assert(
14e51 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c   pLater->sharabl
14e52 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
14e53 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d   pLater->pNext==
14e54 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65  0 || pLater->pNe
14e55 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e  xt->pBt>pLater->
14e56 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14e57 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b  t( !pLater->lock
14e58 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61  ed || pLater->wa
14e59 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14e5a 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f    if( pLater->lo
14e5b 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e  cked ){.      un
14e5c 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70  lockBtreeMutex(p
14e5d 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Later);.    }.  
14e5e 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74  }.  lockBtreeMut
14e5f 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61  ex(p);.  for(pLa
14e60 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  ter=p->pNext; pL
14e61 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
14e62 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
14e63 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e   if( pLater->wan
14e64 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  tToLock ){.     
14e65 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28   lockBtreeMutex(
14e66 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pLater);.    }. 
14e67 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74   }.}../*.** Exit
14e68 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d   the recursive m
14e69 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e  utex on a Btree.
14e6a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14e6b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14e6c 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20  treeLeave(Btree 
14e6d 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68  *p){.  if( p->sh
14e6e 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73  arable ){.    as
14e6f 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
14e70 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e  ock>0 );.    p->
14e71 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20  wantToLock--;.  
14e72 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c    if( p->wantToL
14e73 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
14e74 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14e75 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
14e76 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
14e77 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
14e78 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
14e79 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
14e7a 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f   on the btree, o
14e7b 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72  r if the.** B-Tr
14e7c 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64  ee is not marked
14e7d 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a   as sharable..**
14e7e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14e7f 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72   is used only fr
14e80 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74  om within assert
14e81 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
14e82 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14e83 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
14e84 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65  eHoldsMutex(Btre
14e85 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
14e86 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14e87 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14e88 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
14e89 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
14e8a 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c  p->sharable==0 |
14e8b 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c  | p->locked==0 |
14e8c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d  | p->db==p->pBt-
14e8d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >db );.  assert(
14e8e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14e8f 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14e90 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
14e91 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
14e92 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
14e93 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ( p->sharable==0
14e94 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   || p->locked==0
14e95 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
14e96 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14e97 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72  tex) );..  retur
14e98 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  n (p->sharable==
14e99 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b  0 || p->locked);
14e9a 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  .}.#endif...#ifn
14e9b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14e9c 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45  INCRBLOB./*.** E
14e9d 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61  nter and leave a
14e9e 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
14e9f 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72  e given a cursor
14ea0 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a   owned by that.*
14ea1 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20  * Btree.  These 
14ea2 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65  entry points are
14ea3 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65   used by increme
14ea4 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e  ntal I/O and can
14ea5 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69   be.** omitted i
14ea6 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73  f that module is
14ea7 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   not used..*/.SQ
14ea8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14ea9 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
14eaa 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
14eab 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
14eac 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
14ead 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
14eae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14eaf 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
14eb0 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
14eb1 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
14eb2 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
14eb3 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
14eb4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
14eb5 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
14eb6 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65   */.../*.** Ente
14eb7 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
14eb8 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
14eb9 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
14eba 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
14ebb 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
14ebc 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eded (for exampl
14ebd 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73  e) prior to pars
14ebe 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  ing.** a stateme
14ebf 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c  nt since we will
14ec0 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61   be comparing ta
14ec1 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ble and column n
14ec2 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20  ames.** against 
14ec3 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20  all schemas and 
14ec4 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
14ec5 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69  hose schemas bei
14ec6 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20  ng.** reset out 
14ec7 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a  from under us..*
14ec8 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
14ec9 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65  corresponding le
14eca 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72  ave-all procedur
14ecb 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20  es..**.** Enter 
14ecc 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61  the mutexes in a
14ecd 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
14ece 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74  y BtShared point
14ecf 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f  er address.** to
14ed0 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69   avoid the possi
14ed1 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f  bility of deadlo
14ed2 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65  ck when two thre
14ed3 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20  ads with.** two 
14ed4 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69  or more btrees i
14ed5 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72  n common both tr
14ed6 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68  y to lock all th
14ed7 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74  eir btrees.** at
14ed8 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e   the same instan
14ed9 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
14eda 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14edb 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
14edc 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
14edd 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
14ede 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73  , *pLater;.  ass
14edf 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14ee0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
14ee1 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
14ee2 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
14ee3 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
14ee4 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73  b[i].pBt;.    as
14ee5 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e  sert( !p || (p->
14ee6 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e  locked==0 && p->
14ee7 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e  sharable) || p->
14ee8 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  pBt->db==p->db )
14ee9 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
14eea 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
14eeb 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14eec 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21  k++;.      if( !
14eed 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  p->locked ){.   
14eee 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
14eef 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b  wantToLock==1 );
14ef0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
14ef1 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70  p->pPrev ) p = p
14ef2 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20  ->pPrev;.       
14ef3 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41   /* Reason for A
14ef4 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75  LWAYS:  There mu
14ef5 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  st be at least o
14ef6 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65  n unlocked Btree
14ef7 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   in.        ** t
14ef8 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72  he chain.  Other
14ef9 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63  wise the !p->loc
14efa 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77  ked test above w
14efb 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64  ould have failed
14efc 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c   */.        whil
14efd 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20  e( p->locked && 
14efe 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29  ALWAYS(p->pNext)
14eff 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b   ) p = p->pNext;
14f00 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61  .        for(pLa
14f01 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20  ter = p->pNext; 
14f02 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70  pLater; pLater=p
14f03 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  Later->pNext){. 
14f04 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61           if( pLa
14f05 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20  ter->locked ){. 
14f06 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63             unloc
14f07 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74  kBtreeMutex(pLat
14f08 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  er);.          }
14f09 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
14f0a 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20     while( p ){. 
14f0b 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72           lockBtr
14f0c 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14f0d 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65        p = p->pNe
14f0e 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  xt;.        }.  
14f0f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
14f10 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14f11 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14f12 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74  eeLeaveAll(sqlit
14f13 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
14f14 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20  ;.  Btree *p;.  
14f15 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14f16 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
14f17 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
14f18 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
14f19 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d  ++){.    p = db-
14f1a 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
14f1b 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61   if( p && p->sha
14f1c 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61  rable ){.      a
14f1d 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
14f1e 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20  Lock>0 );.      
14f1f 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b  p->wantToLock--;
14f20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61  .      if( p->wa
14f21 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  ntToLock==0 ){. 
14f22 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72         unlockBtr
14f23 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14f24 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
14f25 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
14f26 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
14f27 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  e if the current
14f28 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
14f29 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14f2a 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61  ction.** mutex a
14f2b 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20  nd all required 
14f2c 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73  BtShared mutexes
14f2d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14f2e 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
14f2f 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
14f30 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f  tements only..*/
14f31 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14f32 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14f33 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
14f34 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
14f35 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
14f36 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14f37 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20  (db->mutex) ){. 
14f38 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
14f39 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
14f3a 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
14f3b 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70   Btree *p;.    p
14f3c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14f3d 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14f3e 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20  p->sharable &&. 
14f3f 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74          (p->want
14f40 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71  ToLock==0 || !sq
14f41 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14f42 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29  (p->pBt->mutex))
14f43 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14f44 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
14f45 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
14f46 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
14f47 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
14f48 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74   Btree pointer t
14f49 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72  o a BtreeMutexAr
14f4a 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20  ray. .** if the 
14f4b 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73  pointer can poss
14f4c 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77  ibly be shared w
14f4d 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64  ith.** another d
14f4e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14f4f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  on..**.** The po
14f50 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20  inters are kept 
14f51 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
14f52 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20  by pBtree->pBt. 
14f53 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65   That.** way whe
14f54 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72  n we go to enter
14f55 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73   all the mutexes
14f56 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74  , we can enter t
14f57 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  hem.** in order 
14f58 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61  without every ha
14f59 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61  ving to backup a
14f5a 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74  nd retry and wit
14f5b 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67  hout.** worrying
14f5c 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e   about deadlock.
14f5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
14f5e 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65  r of shared btre
14f5f 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  es will always b
14f60 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79  e small (usually
14f61 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61   0 or 1).** so a
14f62 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74  n insertion sort
14f63 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20   is an adequate 
14f64 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a  algorithm here..
14f65 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14f66 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14f67 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
14f68 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72  ert(BtreeMutexAr
14f69 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72  ray *pArray, Btr
14f6a 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69  ee *pBtree){.  i
14f6b 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61  nt i, j;.  BtSha
14f6c 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20  red *pBt;.  if( 
14f6d 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74  pBtree==0 || pBt
14f6e 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ree->sharable==0
14f6f 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64   ) return;.#ifnd
14f70 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
14f71 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72    for(i=0; i<pAr
14f72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b  ray->nMutex; i++
14f73 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14f74 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14f75 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20  i]!=pBtree );.  
14f76 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
14f77 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
14f78 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20  >nMutex>=0 );.  
14f79 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e  assert( pArray->
14f7a 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65  nMutex<ArraySize
14f7b 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29  (pArray->aBtree)
14f7c 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42  -1 );.  pBt = pB
14f7d 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72  tree->pBt;.  for
14f7e 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e  (i=0; i<pArray->
14f7f 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nMutex; i++){.  
14f80 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14f81 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74  ->aBtree[i]!=pBt
14f82 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ree );.    if( p
14f83 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
14f84 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20  ->pBt>pBt ){.   
14f85 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d     for(j=pArray-
14f86 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d  >nMutex; j>i; j-
14f87 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72  -){.        pArr
14f88 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20  ay->aBtree[j] = 
14f89 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
14f8a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1];.      }.   
14f8b 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65     pArray->aBtre
14f8c 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20  e[i] = pBtree;. 
14f8d 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75       pArray->nMu
14f8e 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  tex++;.      ret
14f8f 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
14f90 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14f91 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b  pArray->nMutex++
14f92 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f  ] = pBtree;.}../
14f93 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
14f94 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
14f95 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79  ree in the array
14f96 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
14f97 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20  is.** called at 
14f98 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14f99 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
14f9a 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  ().  The mutexes
14f9b 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61   are.** exited a
14f9c 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
14f9d 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a   same function..
14f9e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14f9f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14fa0 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
14fa1 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  er(BtreeMutexArr
14fa2 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
14fa3 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
14fa4 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
14fa5 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
14fa6 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
14fa7 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
14fa8 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
14fa9 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
14faa 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
14fab 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
14fac 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
14fad 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14fae 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  t( !p->locked ||
14faf 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
14fb0 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73   );..    /* We s
14fb1 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
14fb2 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
14fb3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14fb4 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  tion */.    asse
14fb5 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14fb6 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14fb7 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tex) );..    /* 
14fb8 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61  The Btree is sha
14fb9 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e  rable because on
14fba 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65  ly sharable Btre
14fbb 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20  es are entered. 
14fbc 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61     ** into the a
14fbd 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73  rray in the firs
14fbe 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20  t place. */.    
14fbf 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14fc0 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77  ble );..    p->w
14fc1 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
14fc2 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
14fc3 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72  ){.      lockBtr
14fc4 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14fc5 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  }.  }.}../*.** L
14fc6 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f  eave the mutex o
14fc7 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e  f every btree in
14fc8 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53   the group..*/.S
14fc9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14fca 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
14fcb 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42  utexArrayLeave(B
14fcc 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a  treeMutexArray *
14fcd 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69  pArray){.  int i
14fce 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
14fcf 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
14fd0 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
14fd1 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  p = pArray->aBtr
14fd2 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f  ee[i];.    /* So
14fd3 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20  me basic sanity 
14fd4 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20  checking */.    
14fd5 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20  assert( i==0 || 
14fd6 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
14fd7 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20  -1]->pBt<p->pBt 
14fd8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14fd9 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20  ->locked );.    
14fda 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
14fdb 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20  oLock>0 );..    
14fdc 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72  /* We should alr
14fdd 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b  eady hold a lock
14fde 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14fdf 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
14fe0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
14fe1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14fe2 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
14fe3 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14fe4 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  k--;.    if( p->
14fe5 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
14fe6 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
14fe7 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14fe8 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53  }.  }.}..#else.S
14fe9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14fea 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
14feb 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  nter(Btree *p){.
14fec 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
14fed 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ->db;.}.SQLITE_P
14fee 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14fef 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
14ff0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14ff1 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14ff2 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
14ff3 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
14ff4 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14ff5 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  t;.    if( p ){.
14ff6 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62        p->pBt->db
14ff7 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a   = p->db;.    }.
14ff8 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
14ff9 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
14ffa 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  SAFE */.#endif /
14ffb 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
14ffc 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14ffd 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
14ffe 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d  ***** End of btm
14fff 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
15000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15001 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15002 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
15003 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
15004 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   btree.c *******
15005 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15006 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15007 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
15008 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
15009 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1500a 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1500b 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1500c 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1500d 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1500e 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1500f 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
15010 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
15011 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
15012 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
15013 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
15014 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
15015 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
15016 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
15017 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
15018 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
15019 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1501a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501e 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
1501f 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
15020 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b  a external (disk
15021 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65  -based) database
15022 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a   using BTrees..*
15023 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  * See the header
15024 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72   comment on "btr
15025 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64  eeInt.h" for add
15026 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
15027 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e  ion..** Includin
15028 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  g a description 
15029 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61  of file format a
1502a 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f  nd an overview o
1502b 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a  f operation..*/.
1502c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65  ./*.** The heade
1502d 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70  r string that ap
1502e 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67  pears at the beg
1502f 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a  inning of every.
15030 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  ** SQLite databa
15031 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  se..*/.static co
15032 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48  nst char zMagicH
15033 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45  eader[] = SQLITE
15034 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f  _FILE_HEADER;../
15035 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c  *.** Set this gl
15036 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f  obal variable to
15037 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61   1 to enable tra
15038 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54  cing using the T
15039 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a  RACE.** macro..*
1503a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69  /.#if 0.int sqli
1503b 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 3b  te3BtreeTrace=1;
1503c 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
1503d 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23  ble tracing */.#
1503e 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29   define TRACE(X)
1503f 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65    if(sqlite3Btre
15040 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58  eTrace){printf X
15041 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b  ;fflush(stdout);
15042 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
15043 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66   TRACE(X).#endif
15044 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
15045 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
15046 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73  ACHE./*.** A lis
15047 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62  t of BtShared ob
15048 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65  jects that are e
15049 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74  ligible for part
1504a 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20  icipation.** in 
1504b 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54  shared cache.  T
1504c 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
1504d 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69   file scope duri
1504e 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73  ng normal builds
1504f 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73  ,.** but the tes
15050 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20  t harness needs 
15051 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20  to access it so 
15052 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61  we make it globa
15053 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62  l for .** test b
15054 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63  uilds..**.** Acc
15055 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69  ess to this vari
15056 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65  able is protecte
15057 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45  d by SQLITE_MUTE
15058 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e  X_STATIC_MASTER.
15059 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1505a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
1505b 49 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a  IVATE BtShared *
1505c 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74  SQLITE_WSD sqlit
1505d 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
1505e 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61  t = 0;.#else.sta
1505f 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51  tic BtShared *SQ
15060 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
15061 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20  SharedCacheList 
15062 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64  = 0;.#endif.#end
15063 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
15064 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a  T_SHARED_CACHE *
15065 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
15066 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15067 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65  CHE./*.** Enable
15068 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20   or disable the 
15069 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64  shared pager and
1506a 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73   schema features
1506b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1506c 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65  tine has no effe
1506d 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64  ct on existing d
1506e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1506f 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72  ons..** The shar
15070 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67  ed cache setting
15071 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75   effects only fu
15072 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  ture calls to.**
15073 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c   sqlite3_open(),
15074 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
15075 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  ), or sqlite3_op
15076 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49  en_v2()..*/.SQLI
15077 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
15078 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64  e3_enable_shared
15079 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c  _cache(int enabl
1507a 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f  e){.  sqlite3Glo
1507b 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64  balConfig.shared
1507c 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65  CacheEnabled = e
1507d 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20  nable;.  return 
1507e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
1507f 64 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20 53 51  dif....#ifdef SQ
15080 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
15081 5f 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a  _CACHE.  /*.  **
15082 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71   The functions q
15083 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
15084 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53  ableLock(), setS
15085 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15086 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20  ock(),.  ** and 
15087 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61  clearAllSharedCa
15088 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a  cheTableLocks().
15089 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20    ** manipulate 
1508a 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42  entries in the B
1508b 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69  tShared.pLock li
1508c 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74  nked list used t
1508d 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61  o store.  ** sha
1508e 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20  red-cache table 
1508f 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20  level locks. If 
15090 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63  the library is c
15091 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
15092 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63  .  ** shared-cac
15093 68 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62  he feature disab
15094 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  led, then there 
15095 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65  is only ever one
15096 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61   user.  ** of ea
15097 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75  ch BtShared stru
15098 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69  cture and so thi
15099 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74  s locking is not
1509a 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a   necessary. .  *
1509b 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20  * So define the 
1509c 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e  lock related fun
1509d 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73  ctions as no-ops
1509e 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ..  */.  #define
1509f 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
150a0 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63  eTableLock(a,b,c
150a1 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64  ) SQLITE_OK.  #d
150a2 65 66 69 6e 65 20 73 65 74 53 68 61 72 65 64 43  efine setSharedC
150a3 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c  acheTableLock(a,
150a4 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20  b,c) SQLITE_OK. 
150a5 20 23 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c   #define clearAl
150a6 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
150a7 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66  eLocks(a).  #def
150a8 69 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c  ine downgradeAll
150a9 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
150aa 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69  Locks(a).  #defi
150ab 6e 65 20 68 61 73 53 68 61 72 65 64 43 61 63 68  ne hasSharedCach
150ac 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63  eTableLock(a,b,c
150ad 2c 64 29 20 31 0a 20 20 23 64 65 66 69 6e 65 20  ,d) 1.  #define 
150ae 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73  hasReadConflicts
150af 28 61 2c 20 62 29 20 30 0a 23 65 6e 64 69 66 0a  (a, b) 0.#endif.
150b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
150b1 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
150b2 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  E..#ifdef SQLITE
150b3 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54  _DEBUG./*.**** T
150b4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
150b5 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 70 61 72  only used as par
150b6 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29  t of an assert()
150b7 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2a 2a 0a   statement. ***.
150b8 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  **.** Check to s
150b9 65 65 20 69 66 20 70 42 74 72 65 65 20 68 6f 6c  ee if pBtree hol
150ba 64 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ds the required 
150bb 6c 6f 63 6b 73 20 74 6f 20 72 65 61 64 20 6f 72  locks to read or
150bc 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 0a 2a   write to the .*
150bd 2a 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  * table with roo
150be 74 20 70 61 67 65 20 69 52 6f 6f 74 2e 20 20 20  t page iRoot.   
150bf 52 65 74 75 72 6e 20 31 20 69 66 20 69 74 20 64  Return 1 if it d
150c0 6f 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 74  oes and 0 if not
150c1 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ..**.** For exam
150c2 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 69 6e  ple, when writin
150c3 67 20 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74  g to a table wit
150c4 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f  h root-page iRoo
150c5 74 20 76 69 61 20 0a 2a 2a 20 42 74 72 65 65 20  t via .** Btree 
150c6 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 74 72 65  connection pBtre
150c7 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65  e:.**.**    asse
150c8 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63  rt( hasSharedCac
150c9 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 42 74 72  heTableLock(pBtr
150ca 65 65 2c 20 69 52 6f 6f 74 2c 20 30 2c 20 57 52  ee, iRoot, 0, WR
150cb 49 54 45 5f 4c 4f 43 4b 29 20 29 3b 0a 2a 2a 0a  ITE_LOCK) );.**.
150cc 2a 2a 20 57 68 65 6e 20 77 72 69 74 69 6e 67 20  ** When writing 
150cd 74 6f 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74  to an index that
150ce 20 72 65 73 69 64 65 73 20 69 6e 20 61 20 73 68   resides in a sh
150cf 61 72 61 62 6c 65 20 64 61 74 61 62 61 73 65 2c  arable database,
150d0 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20   the .** caller 
150d1 73 68 6f 75 6c 64 20 68 61 76 65 20 66 69 72 73  should have firs
150d2 74 20 6f 62 74 61 69 6e 65 64 20 61 20 6c 6f 63  t obtained a loc
150d3 6b 20 73 70 65 63 69 66 79 69 6e 67 20 74 68 65  k specifying the
150d4 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a   root page of.**
150d5 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
150d6 6e 67 20 74 61 62 6c 65 2e 20 54 68 69 73 20 6d  ng table. This m
150d7 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20 62 69  akes things a bi
150d8 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74  t more complicat
150d9 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73 20 6d  ed,.** as this m
150da 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65 61 63  odule treats eac
150db 68 20 74 61 62 6c 65 20 61 73 20 61 20 73 65 70  h table as a sep
150dc 61 72 61 74 65 20 73 74 72 75 63 74 75 72 65 2e  arate structure.
150dd 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a   To determine.**
150de 20 74 68 65 20 74 61 62 6c 65 20 63 6f 72 72 65   the table corre
150df 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
150e0 69 6e 64 65 78 20 62 65 69 6e 67 20 77 72 69 74  index being writ
150e1 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e  ten, this.** fun
150e2 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 61  ction has to sea
150e3 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65 20  rch through the 
150e4 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
150e5 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f  .**.** Instead o
150e6 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  f a lock on the 
150e7 74 61 62 6c 65 2f 69 6e 64 65 78 20 72 6f 6f 74  table/index root
150e8 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f 74  ed at page iRoot
150e9 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79  , the caller may
150ea 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69 74 65  .** hold a write
150eb 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68  -lock on the sch
150ec 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f 74 20  ema table (root 
150ed 70 61 67 65 20 31 29 2e 20 54 68 69 73 20 69 73  page 1). This is
150ee 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70 74 61   also.** accepta
150ef 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
150f0 6e 74 20 68 61 73 53 68 61 72 65 64 43 61 63 68  nt hasSharedCach
150f1 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 42 74  eTableLock(.  Bt
150f2 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
150f3 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74       /* Handle t
150f4 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f  hat must hold lo
150f5 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 52 6f  ck */.  Pgno iRo
150f6 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ot,            /
150f7 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 62  * Root page of b
150f8 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  -tree */.  int i
150f9 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20  sIndex,         
150fa 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 52 6f    /* True if iRo
150fb 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f  ot is the root o
150fc 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65  f an index b-tre
150fd 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f 63 6b  e */.  int eLock
150fe 54 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a  Type          /*
150ff 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 74   Required lock t
15100 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f  ype (READ_LOCK o
15101 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f  r WRITE_LOCK) */
15102 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  .){.  Schema *pS
15103 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20  chema = (Schema 
15104 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70  *)pBtree->pBt->p
15105 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f 20 69  Schema;.  Pgno i
15106 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63  Tab = 0;.  BtLoc
15107 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20  k *pLock;..  /* 
15108 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65  If this database
15109 20 69 73 20 6e 6f 74 20 73 68 61 72 65 61 62 6c   is not shareabl
1510a 65 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6c 69  e, or if the cli
1510b 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 0a 20  ent is reading. 
1510c 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 68 65 20   ** and has the 
1510d 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
1510e 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20   flag set, then 
1510f 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 69  no lock is requi
15110 72 65 64 2e 20 0a 20 20 2a 2a 20 52 65 74 75 72  red. .  ** Retur
15111 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61 74 65  n true immediate
15112 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ly..  */.  if( (
15113 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65  pBtree->sharable
15114 3d 3d 30 29 0a 20 20 20 7c 7c 20 28 65 4c 6f 63  ==0).   || (eLoc
15115 6b 54 79 70 65 3d 3d 52 45 41 44 5f 4c 4f 43 4b  kType==READ_LOCK
15116 20 26 26 20 28 70 42 74 72 65 65 2d 3e 64 62 2d   && (pBtree->db-
15117 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
15118 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29  ReadUncommitted)
15119 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
1511a 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  n 1;.  }..  /* I
1511b 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
1511c 72 65 61 64 69 6e 67 20 20 6f 72 20 77 72 69 74  reading  or writ
1511d 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 61 6e 64  ing an index and
1511e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 0a 20   the schema is. 
1511f 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20   ** not loaded, 
15120 74 68 65 6e 20 69 74 20 69 73 20 74 6f 6f 20 64  then it is too d
15121 69 66 66 69 63 75 6c 74 20 74 6f 20 61 63 74 75  ifficult to actu
15122 61 6c 6c 79 20 63 68 65 63 6b 20 74 6f 20 73 65  ally check to se
15123 65 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f  e if.  ** the co
15124 72 72 65 63 74 20 6c 6f 63 6b 73 20 61 72 65 20  rrect locks are 
15125 68 65 6c 64 2e 20 20 53 6f 20 64 6f 20 6e 6f 74  held.  So do not
15126 20 62 6f 74 68 65 72 20 2d 20 6a 75 73 74 20 72   bother - just r
15127 65 74 75 72 6e 20 74 72 75 65 2e 0a 20 20 2a 2a  eturn true..  **
15128 20 54 68 69 73 20 63 61 73 65 20 64 6f 65 73 20   This case does 
15129 6e 6f 74 20 63 6f 6d 65 20 75 70 20 76 65 72 79  not come up very
1512a 20 6f 66 74 65 6e 20 61 6e 79 68 6f 77 2e 0a 20   often anyhow.. 
1512b 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65   */.  if( isInde
1512c 78 20 26 26 20 28 21 70 53 63 68 65 6d 61 20 7c  x && (!pSchema |
1512d 7c 20 28 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  | (pSchema->flag
1512e 73 26 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  s&DB_SchemaLoade
1512f 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65  d)==0) ){.    re
15130 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 1;.  }..  /
15131 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
15132 20 72 6f 6f 74 2d 70 61 67 65 20 74 68 61 74 20   root-page that 
15133 74 68 65 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20  the lock should 
15134 62 65 20 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20  be held on. For 
15135 74 61 62 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65  table.  ** b-tre
15136 65 73 2c 20 74 68 69 73 20 69 73 20 6a 75 73 74  es, this is just
15137 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
15138 66 20 74 68 65 20 62 2d 74 72 65 65 20 62 65 69  f the b-tree bei
15139 6e 67 20 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20  ng read or.  ** 
1513a 77 72 69 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64  written. For ind
1513b 65 78 20 62 2d 74 72 65 65 73 2c 20 69 74 20 69  ex b-trees, it i
1513c 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
1513d 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  of the associate
1513e 64 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a  d.  ** table.  *
1513f 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65 78 20  /.  if( isIndex 
15140 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20  ){.    HashElem 
15141 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71  *p;.    for(p=sq
15142 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70  liteHashFirst(&p
15143 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29  Schema->idxHash)
15144 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73  ; p; p=sqliteHas
15145 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20  hNext(p)){.     
15146 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 28   Index *pIdx = (
15147 49 6e 64 65 78 20 2a 29 73 71 6c 69 74 65 48 61  Index *)sqliteHa
15148 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20  shData(p);.     
15149 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d   if( pIdx->tnum=
1514a 3d 28 69 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20  =(int)iRoot ){. 
1514b 20 20 20 20 20 20 20 69 54 61 62 20 3d 20 70 49         iTab = pI
1514c 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 6d  dx->pTable->tnum
1514d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1514e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 54 61    }else{.    iTa
1514f 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a  b = iRoot;.  }..
15150 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20    /* Search for 
15151 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63  the required loc
15152 6b 2e 20 45 69 74 68 65 72 20 61 20 77 72 69 74  k. Either a writ
15153 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70  e-lock on root-p
15154 61 67 65 20 69 54 61 62 2c 20 61 20 0a 20 20 2a  age iTab, a .  *
15155 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  * write-lock on 
15156 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65  the schema table
15157 2c 20 6f 72 20 28 69 66 20 74 68 65 20 63 6c 69  , or (if the cli
15158 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 29 20  ent is reading) 
15159 61 0a 20 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b  a.  ** read-lock
1515a 20 6f 6e 20 69 54 61 62 20 77 69 6c 6c 20 73 75   on iTab will su
1515b 66 66 69 63 65 2e 20 52 65 74 75 72 6e 20 31 20  ffice. Return 1 
1515c 69 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  if any of these 
1515d 61 72 65 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20  are found.  */. 
1515e 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65   for(pLock=pBtre
1515f 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70  e->pBt->pLock; p
15160 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63  Lock; pLock=pLoc
15161 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  k->pNext){.    i
15162 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65  f( pLock->pBtree
15163 3d 3d 70 42 74 72 65 65 20 0a 20 20 20 20 20 26  ==pBtree .     &
15164 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65  & (pLock->iTable
15165 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f 63 6b  ==iTab || (pLock
15166 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c  ->eLock==WRITE_L
15167 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 54  OCK && pLock->iT
15168 61 62 6c 65 3d 3d 31 29 29 0a 20 20 20 20 20 26  able==1)).     &
15169 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d  & pLock->eLock>=
1516a 65 4c 6f 63 6b 54 79 70 65 20 0a 20 20 20 20 29  eLockType .    )
1516b 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1516c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
1516d 2a 20 46 61 69 6c 65 64 20 74 6f 20 66 69 6e 64  * Failed to find
1516e 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f   the required lo
1516f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ck. */.  return 
15170 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  0;.}.#endif /* S
15171 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a  QLITE_DEBUG */..
15172 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
15173 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73  BUG./*.**** This
15174 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65   function may be
15175 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66   used as part of
15176 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
15177 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a  ents only. ****.
15178 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  **.** Return tru
15179 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20 62 65  e if it would be
1517a 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74   illegal for pBt
1517b 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ree to write int
1517c 6f 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6f  o the.** table o
1517d 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61  r index rooted a
1517e 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73 65 20  t iRoot because 
1517f 6f 74 68 65 72 20 73 68 61 72 65 64 20 63 6f 6e  other shared con
15180 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20  nections are.** 
15181 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 20 72  simultaneously r
15182 65 61 64 69 6e 67 20 74 68 61 74 20 73 61 6d 65  eading that same
15183 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
15184 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c  .**.** It is ill
15185 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 20  egal for pBtree 
15186 74 6f 20 77 72 69 74 65 20 69 66 20 73 6f 6d 65  to write if some
15187 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a   other Btree obj
15188 65 63 74 20 74 68 61 74 0a 2a 2a 20 73 68 61 72  ect that.** shar
15189 65 73 20 74 68 65 20 73 61 6d 65 20 42 74 53 68  es the same BtSh
1518a 61 72 65 64 20 6f 62 6a 65 63 74 20 69 73 20 63  ared object is c
1518b 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67  urrently reading
1518c 20 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74   or writing.** t
1518d 68 65 20 69 52 6f 6f 74 20 74 61 62 6c 65 2e 20  he iRoot table. 
1518e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20   Except, if the 
1518f 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65  other Btree obje
15190 63 74 20 68 61 73 20 74 68 65 0a 2a 2a 20 72 65  ct has the.** re
15191 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66  ad-uncommitted f
15192 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20 69 74  lag set, then it
15193 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 6f   is OK for the o
15194 74 68 65 72 20 6f 62 6a 65 63 74 20 74 6f 0a 2a  ther object to.*
15195 2a 20 68 61 76 65 20 61 20 72 65 61 64 20 63 75  * have a read cu
15196 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  rsor..**.** For 
15197 65 78 61 6d 70 6c 65 2c 20 62 65 66 6f 72 65 20  example, before 
15198 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 70  writing to any p
15199 61 72 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65  art of the table
1519a 20 6f 72 20 69 6e 64 65 78 0a 2a 2a 20 72 6f 6f   or index.** roo
1519b 74 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f  ted at page iRoo
1519c 74 2c 20 6f 6e 65 20 73 68 6f 75 6c 64 20 63 61  t, one should ca
1519d 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 73  ll:.**.**    ass
1519e 65 72 74 28 20 21 68 61 73 52 65 61 64 43 6f 6e  ert( !hasReadCon
1519f 66 6c 69 63 74 73 28 70 42 74 72 65 65 2c 20 69  flicts(pBtree, i
151a0 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a 73 74 61 74  Root) );.*/.stat
151a1 69 63 20 69 6e 74 20 68 61 73 52 65 61 64 43 6f  ic int hasReadCo
151a2 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20 2a 70  nflicts(Btree *p
151a3 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52 6f 6f  Btree, Pgno iRoo
151a4 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  t){.  BtCursor *
151a5 70 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65  p;.  for(p=pBtre
151a6 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  e->pBt->pCursor;
151a7 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
151a8 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f  .    if( p->pgno
151a9 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0a 20 20 20  Root==iRoot .   
151aa 20 20 26 26 20 70 2d 3e 70 42 74 72 65 65 21 3d    && p->pBtree!=
151ab 70 42 74 72 65 65 0a 20 20 20 20 20 26 26 20 30  pBtree.     && 0
151ac 3d 3d 28 70 2d 3e 70 42 74 72 65 65 2d 3e 64 62  ==(p->pBtree->db
151ad 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
151ae 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
151af 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
151b0 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
151b1 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
151b2 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 23 69  .#endif    /* #i
151b3 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
151b4 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72  G */../*.** Quer
151b5 79 20 74 6f 20 73 65 65 20 69 66 20 42 74 72 65  y to see if Btre
151b6 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f  e handle p may o
151b7 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20  btain a lock of 
151b8 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28  type eLock .** (
151b9 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49  READ_LOCK or WRI
151ba 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20  TE_LOCK) on the 
151bb 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
151bc 70 61 67 65 20 69 54 61 62 2e 20 52 65 74 75 72  page iTab. Retur
151bd 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  n.** SQLITE_OK i
151be 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79 20 62  f the lock may b
151bf 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79 20 63  e obtained (by c
151c0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 74 53 68 61  alling.** setSha
151c1 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
151c2 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 54 45 5f  k()), or SQLITE_
151c3 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e 0a 2a  LOCKED if not..*
151c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65  /.static int que
151c5 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
151c6 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c  leLock(Btree *p,
151c7 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65   Pgno iTab, u8 e
151c8 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
151c9 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
151ca 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72  .  BtLock *pIter
151cb 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
151cc 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
151cd 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
151ce 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f  rt( eLock==READ_
151cf 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57  LOCK || eLock==W
151d0 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  RITE_LOCK );.  a
151d1 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20  ssert( p->db!=0 
151d2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 70  );.  assert( !(p
151d3 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  ->db->flags&SQLI
151d4 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
151d5 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54  ed)||eLock==WRIT
151d6 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 3d 3d 31 20  E_LOCK||iTab==1 
151d7 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 72 65  );.  .  /* If re
151d8 71 75 65 73 74 69 6e 67 20 61 20 77 72 69 74 65  questing a write
151d9 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20  -lock, then the 
151da 42 74 72 65 65 20 6d 75 73 74 20 68 61 76 65 20  Btree must have 
151db 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 0a 20 20  an open write.  
151dc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  ** transaction o
151dd 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 41 6e 64  n this file. And
151de 2c 20 6f 62 76 69 6f 75 73 6c 79 2c 20 66 6f 72  , obviously, for
151df 20 74 68 69 73 20 74 6f 20 62 65 20 73 6f 20 74   this to be so t
151e0 68 65 72 65 20 0a 20 20 2a 2a 20 6d 75 73 74 20  here .  ** must 
151e1 62 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65  be an open write
151e2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
151e3 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66 2e  the file itself.
151e4 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
151e5 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
151e6 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70 57 72   || (p==pBt->pWr
151e7 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54 72 61  iter && p->inTra
151e8 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29  ns==TRANS_WRITE)
151e9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c   );.  assert( eL
151ea 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
151eb 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  | pBt->inTransac
151ec 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
151ed 45 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69  E );.  .  /* Thi
151ee 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
151ef 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72  o-op if the shar
151f0 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20  ed-cache is not 
151f1 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28  enabled */.  if(
151f2 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b   !p->sharable ){
151f3 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
151f4 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
151f5 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63   If some other c
151f6 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
151f7 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76  ding an exclusiv
151f8 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a  e lock, the.  **
151f9 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20   requested lock 
151fa 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69  may not be obtai
151fb 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ned..  */.  if( 
151fc 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20  pBt->pWriter!=p 
151fd 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73  && pBt->isExclus
151fe 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ive ){.    sqlit
151ff 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
15200 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
15201 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20  pWriter->db);.  
15202 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15203 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
15204 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70  HE;.  }..  for(p
15205 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
15206 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
15207 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
15208 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f   /* The conditio
15209 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21  n (pIter->eLock!
1520a 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66  =eLock) in the f
1520b 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29  ollowing if(...)
1520c 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65   .    ** stateme
1520d 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69  nt is a simplifi
1520e 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a  cation of:.    *
1520f 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63  *.    **   (eLoc
15210 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c  k==WRITE_LOCK ||
15211 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57   pIter->eLock==W
15212 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a  RITE_LOCK).    *
15213 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77  *.    ** since w
15214 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65  e know that if e
15215 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
15216 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20  , then no other 
15217 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a  connection.    *
15218 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49  * may hold a WRI
15219 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74  TE_LOCK on any t
1521a 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c  able in this fil
1521b 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63  e (since there c
1521c 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62  an.    ** only b
1521d 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65  e a single write
1521e 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  r)..    */.    a
1521f 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c  ssert( pIter->eL
15220 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
15221 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
15222 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20  WRITE_LOCK );.  
15223 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
15224 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49  =READ_LOCK || pI
15225 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c  ter->pBtree==p |
15226 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
15227 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  READ_LOCK);.    
15228 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65  if( pIter->pBtre
15229 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69  e!=p && pIter->i
1522a 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70  Table==iTab && p
1522b 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f  Iter->eLock!=eLo
1522c 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ck ){.      sqli
1522d 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
1522e 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65  cked(p->db, pIte
1522f 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a  r->pBtree->db);.
15230 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d        if( eLock=
15231 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20  =WRITE_LOCK ){. 
15232 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
15233 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29  ==pBt->pWriter )
15234 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69  ;.        pBt->i
15235 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20  sPending = 1;.  
15236 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
15237 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
15238 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
15239 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1523a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
1523b 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
1523c 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1523d 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1523e 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1523f 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64  _CACHE./*.** Add
15240 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
15241 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  able with root-p
15242 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68  age iTable to th
15243 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75  e shared-btree u
15244 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20  sed.** by Btree 
15245 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65  handle p. Parame
15246 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62  ter eLock must b
15247 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f  e either READ_LO
15248 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f  CK or .** WRITE_
15249 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  LOCK..**.** This
1524a 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
1524b 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
1524c 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65  .**.**   (a) The
1524d 20 73 70 65 63 69 66 69 65 64 20 42 74 72 65 65   specified Btree
1524e 20 6f 62 6a 65 63 74 20 70 20 69 73 20 63 6f 6e   object p is con
1524f 6e 65 63 74 65 64 20 74 6f 20 61 20 73 68 61 72  nected to a shar
15250 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 64 61  able.**       da
15251 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 68  tabase (one with
15252 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 68   the BtShared.sh
15253 61 72 61 62 6c 65 20 66 6c 61 67 20 73 65 74 29  arable flag set)
15254 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28 62  , and.**.**   (b
15255 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72 65 65  ) No other Btree
15256 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20 61 20   objects hold a 
15257 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69  lock that confli
15258 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 69 74  cts.**       wit
15259 68 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  h the requested 
1525a 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 72 79  lock (i.e. query
1525b 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1525c 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20  Lock() has.**   
1525d 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 65 6e      already been
1525e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 74 75   called and retu
1525f 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e  rned SQLITE_OK).
15260 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
15261 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
15262 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 64 65  the lock is adde
15263 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20  d successfully. 
15264 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a  SQLITE_NOMEM .**
15265 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
15266 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74  a malloc attempt
15267 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   fails..*/.stati
15268 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 64 43  c int setSharedC
15269 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74  acheTableLock(Bt
1526a 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61  ree *p, Pgno iTa
1526b 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a  ble, u8 eLock){.
1526c 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1526d 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f  = p->pBt;.  BtLo
1526e 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20  ck *pLock = 0;. 
1526f 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a   BtLock *pIter;.
15270 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15271 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
15272 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
15273 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ( eLock==READ_LO
15274 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49  CK || eLock==WRI
15275 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  TE_LOCK );.  ass
15276 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b  ert( p->db!=0 );
15277 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74  ..  /* A connect
15278 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 61  ion with the rea
15279 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c  d-uncommitted fl
1527a 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76 65  ag set will neve
1527b 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62  r try to.  ** ob
1527c 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b  tain a read-lock
1527d 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63   using this func
1527e 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72  tion. The only r
1527f 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65  ead-lock obtaine
15280 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e  d.  ** by a conn
15281 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75  ection in read-u
15282 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20  ncommitted mode 
15283 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 65  is on the sqlite
15284 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 74 61  _master .  ** ta
15285 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f  ble, and that lo
15286 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69  ck is obtained i
15287 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  n BtreeBeginTran
15288 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  s().  */.  asser
15289 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c  t( 0==(p->db->fl
1528a 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
1528b 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65  ncommitted) || e
1528c 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1528d 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66   );..  /* This f
1528e 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f  unction should o
1528f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  nly be called on
15290 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d 74 72   a sharable b-tr
15291 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 20 2a  ee after it .  *
15292 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 65 72  * has been deter
15293 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6f 74  mined that no ot
15294 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c 64 73  her b-tree holds
15295 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c   a conflicting l
15296 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ock.  */.  asser
15297 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  t( p->sharable )
15298 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49  ;.  assert( SQLI
15299 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72  TE_OK==queryShar
1529a 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1529b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63  (p, iTable, eLoc
1529c 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73  k) );..  /* Firs
1529d 74 20 73 65 61 72 63 68 20 74 68 65 20 6c 69 73  t search the lis
1529e 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e  t for an existin
1529f 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74  g lock on this t
152a0 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70  able. */.  for(p
152a1 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
152a2 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
152a3 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
152a4 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 61 62   if( pIter->iTab
152a5 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 49  le==iTable && pI
152a6 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29  ter->pBtree==p )
152a7 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20  {.      pLock = 
152a8 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 72 65  pIter;.      bre
152a9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
152aa 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f 76 65   /* If the above
152ab 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20   search did not 
152ac 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 73 74  find a BtLock st
152ad 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 6e 67  ruct associating
152ae 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 77 69   Btree p.  ** wi
152af 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c 65 2c  th table iTable,
152b0 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e   allocate one an
152b1 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74  d link it into t
152b2 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20  he list..  */.  
152b3 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20  if( !pLock ){.  
152b4 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63    pLock = (BtLoc
152b5 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  k *)sqlite3Mallo
152b6 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c  cZero(sizeof(BtL
152b7 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 20 21  ock));.    if( !
152b8 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72  pLock ){.      r
152b9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
152ba 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c  EM;.    }.    pL
152bb 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54  ock->iTable = iT
152bc 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d  able;.    pLock-
152bd 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 20  >pBtree = p;.   
152be 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20   pLock->pNext = 
152bf 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20  pBt->pLock;.    
152c0 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f  pBt->pLock = pLo
152c1 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ck;.  }..  /* Se
152c2 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f  t the BtLock.eLo
152c3 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74  ck variable to t
152c4 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68  he maximum of th
152c5 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20  e current lock. 
152c6 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 71 75   ** and the requ
152c7 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73  ested lock. This
152c8 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 69 74   means if a writ
152c9 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65 61  e-lock was alrea
152ca 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64  dy held.  ** and
152cb 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71   a read-lock req
152cc 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e 27 74  uested, we don't
152cd 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f 77   incorrectly dow
152ce 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e  ngrade the lock.
152cf 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
152d0 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f  WRITE_LOCK>READ_
152d1 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c  LOCK );.  if( eL
152d2 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b  ock>pLock->eLock
152d3 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65   ){.    pLock->e
152d4 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20  Lock = eLock;.  
152d5 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
152d6 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20  TE_OK;.}.#endif 
152d7 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
152d8 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
152d9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
152da 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
152db 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  E./*.** Release 
152dc 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f  all the table lo
152dd 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69  cks (locks obtai
152de 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74 6f  ned via calls to
152df 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 72 65  .** the setShare
152e0 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
152e1 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 65 6c  ) procedure) hel
152e2 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a 65 63  d by Btree objec
152e3 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  t p..**.** This 
152e4 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
152e5 20 74 68 61 74 20 42 74 72 65 65 20 70 20 68 61   that Btree p ha
152e6 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f  s an open read o
152e7 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e  r write .** tran
152e8 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64  saction. If it d
152e9 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68  oes not, then th
152ea 65 20 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e  e BtShared.isPen
152eb 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a  ding variable.**
152ec 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63   may be incorrec
152ed 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a  tly cleared..*/.
152ee 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61  static void clea
152ef 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  rAllSharedCacheT
152f0 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20  ableLocks(Btree 
152f1 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
152f2 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
152f3 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72   BtLock **ppIter
152f4 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a   = &pBt->pLock;.
152f5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
152f6 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
152f7 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
152f8 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
152f9 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20   0==*ppIter );. 
152fa 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
152fb 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c  ans>0 );..  whil
152fc 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20  e( *ppIter ){.  
152fd 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20    BtLock *pLock 
152fe 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61  = *ppIter;.    a
152ff 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78  ssert( pBt->isEx
15300 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42  clusive==0 || pB
15301 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63  t->pWriter==pLoc
15302 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20  k->pBtree );.   
15303 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
15304 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e  pBtree->inTrans>
15305 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b  =pLock->eLock );
15306 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e  .    if( pLock->
15307 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20  pBtree==p ){.   
15308 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f     *ppIter = pLo
15309 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ck->pNext;.     
1530a 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
1530b 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f  iTable!=1 || pLo
1530c 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a  ck==&p->lock );.
1530d 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d        if( pLock-
1530e 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20  >iTable!=1 ){.  
1530f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
15310 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20  ee(pLock);.     
15311 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
15312 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c      ppIter = &pL
15313 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
15314 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  }.  }..  assert(
15315 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d   pBt->isPending=
15316 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74  =0 || pBt->pWrit
15317 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d  er );.  if( pBt-
15318 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20  >pWriter==p ){. 
15319 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20     pBt->pWriter 
1531a 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73  = 0;.    pBt->is
1531b 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20  Exclusive = 0;. 
1531c 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e     pBt->isPendin
1531d 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  g = 0;.  }else i
1531e 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63  f( pBt->nTransac
1531f 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f  tion==2 ){.    /
15320 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
15321 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 42  is called when B
15322 74 72 65 65 20 70 20 69 73 20 63 6f 6e 63 6c 75  tree p is conclu
15323 64 69 6e 67 20 69 74 73 20 0a 20 20 20 20 2a 2a  ding its .    **
15324 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66   transaction. If
15325 20 74 68 65 72 65 20 63 75 72 72 65 6e 74 6c 79   there currently
15326 20 65 78 69 73 74 73 20 61 20 77 72 69 74 65 72   exists a writer
15327 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20  , and p is not. 
15328 20 20 20 2a 2a 20 74 68 61 74 20 77 72 69 74 65     ** that write
15329 72 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62  r, then the numb
1532a 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64  er of locks held
1532b 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20   by connections 
1532c 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  other.    ** tha
1532d 6e 20 74 68 65 20 77 72 69 74 65 72 20 6d 75 73  n the writer mus
1532e 74 20 62 65 20 61 62 6f 75 74 20 74 6f 20 64 72  t be about to dr
1532f 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74  op to zero. In t
15330 68 69 73 20 63 61 73 65 0a 20 20 20 20 2a 2a 20  his case.    ** 
15331 73 65 74 20 74 68 65 20 69 73 50 65 6e 64 69 6e  set the isPendin
15332 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20  g flag to 0..   
15333 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
15334 65 72 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ere is not curre
15335 6e 74 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74  ntly a writer, t
15336 68 65 6e 20 42 74 53 68 61 72 65 64 2e 69 73 50  hen BtShared.isP
15337 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 20 20 20  ending must.    
15338 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c 72 65 61  ** be zero alrea
15339 64 79 2e 20 53 6f 20 74 68 69 73 20 6e 65 78 74  dy. So this next
1533a 20 6c 69 6e 65 20 69 73 20 68 61 72 6d 6c 65 73   line is harmles
1533b 73 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 0a  s in that case..
1533c 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e      */.    pBt->
1533d 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  isPending = 0;. 
1533e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
1533f 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67 65   function change
15340 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f 63 6b  s all write-lock
15341 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 20  s held by Btree 
15342 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f 63 6b  p into read-lock
15343 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
15344 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68  d downgradeAllSh
15345 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
15346 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  cks(Btree *p){. 
15347 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15348 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 70   p->pBt;.  if( p
15349 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29  Bt->pWriter==p )
1534a 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c  {.    BtLock *pL
1534b 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 57  ock;.    pBt->pW
1534c 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70  riter = 0;.    p
1534d 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20  Bt->isExclusive 
1534e 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73  = 0;.    pBt->is
1534f 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20  Pending = 0;.   
15350 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 2d 3e   for(pLock=pBt->
15351 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c  pLock; pLock; pL
15352 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74  ock=pLock->pNext
15353 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
15354 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 52   pLock->eLock==R
15355 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63  EAD_LOCK || pLoc
15356 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 3b 0a  k->pBtree==p );.
15357 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f        pLock->eLo
15358 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a  ck = READ_LOCK;.
15359 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6e      }.  }.}..#en
1535a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1535b 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20  IT_SHARED_CACHE 
1535c 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */..static void 
1535d 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50  releasePage(MemP
1535e 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a  age *pPage);  /*
1535f 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
15360 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 20  ce */../*.***** 
15361 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
15362 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20 61  used inside of a
15363 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a 2a 2a  ssert() only ***
15364 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74  *.**.** Verify t
15365 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68  hat the cursor h
15366 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 6f  olds the mutex o
15367 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 0a 2a  n its BtShared.*
15368 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
15369 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74  DEBUG.static int
1536a 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1536b 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a  x(BtCursor *p){.
1536c 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1536d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70  _mutex_held(p->p
1536e 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65  Bt->mutex);.}.#e
1536f 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53  ndif...#ifndef S
15370 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
15371 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  LOB./*.** Invali
15372 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f  date the overflo
15373 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
15374 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75  e for cursor pCu
15375 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74  r, if any..*/.st
15376 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69  atic void invali
15377 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
15378 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
15379 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
1537a 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1537b 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ur) );.  sqlite3
1537c 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65  _free(pCur->aOve
1537d 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e  rflow);.  pCur->
1537e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d  aOverflow = 0;.}
1537f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61  ../*.** Invalida
15380 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  te the overflow 
15381 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20  page-list cache 
15382 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20  for all cursors 
15383 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65  opened.** on the
15384 20 73 68 61 72 65 64 20 62 74 72 65 65 20 73 74   shared btree st
15385 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a  ructure pBt..*/.
15386 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
15387 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f  lidateAllOverflo
15388 77 43 61 63 68 65 28 42 74 53 68 61 72 65 64 20  wCache(BtShared 
15389 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f  *pBt){.  BtCurso
1538a 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  r *p;.  assert( 
1538b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1538c 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
1538d 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70  ;.  for(p=pBt->p
1538e 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e  Cursor; p; p=p->
1538f 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61  pNext){.    inva
15390 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61  lidateOverflowCa
15391 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  che(p);.  }.}../
15392 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15393 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 65 66  on is called bef
15394 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 74 68  ore modifying th
15395 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
15396 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e 76 61  table.** to inva
15397 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62  lidate any incrb
15398 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68 61 74  lob cursors that
15399 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65   are open on the
1539a 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 20 6f  .** row or one o
1539b 66 20 74 68 65 20 72 6f 77 73 20 62 65 69 6e 67  f the rows being
1539c 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a   modified..**.**
1539d 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43   If argument isC
1539e 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74 72 75  learTable is tru
1539f 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69  e, then the enti
153a0 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  re contents of t
153a1 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 61  he.** table is a
153a2 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74  bout to be delet
153a3 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
153a4 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20   invalidate all 
153a5 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 72 73  incrblob.** curs
153a6 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 79 20  ors open on any 
153a7 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65 20 74  row within the t
153a8 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  able with root-p
153a9 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a  age pgnoRoot..**
153aa 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
153ab 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c 65  f argument isCle
153ac 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c 73 65  arTable is false
153ad 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 77  , then the row w
153ae 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f  ith.** rowid iRo
153af 77 20 69 73 20 62 65 69 6e 67 20 72 65 70 6c 61  w is being repla
153b0 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 20  ced or deleted. 
153b1 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76  In this case inv
153b2 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c 79 20  alidate.** only 
153b3 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 20 63  those incrblob c
153b4 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
153b5 68 61 74 20 73 70 65 63 69 66 69 63 20 72 6f 77  hat specific row
153b6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
153b7 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62   invalidateIncrb
153b8 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 42 74  lobCursors(.  Bt
153b9 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
153ba 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
153bb 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68  abase file to ch
153bc 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 52 6f  eck */.  i64 iRo
153bd 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w,              
153be 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 68   /* The rowid th
153bf 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e  at might be chan
153c0 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ging */.  int is
153c1 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20 20 20  ClearTable      
153c2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c    /* True if all
153c3 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e 67 20   rows are being 
153c4 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a 20 20  deleted */.){.  
153c5 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42  BtCursor *p;.  B
153c6 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
153c7 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 61 73  Btree->pBt;.  as
153c8 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
153c9 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74  eeHoldsMutex(pBt
153ca 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d  ree) );.  for(p=
153cb 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b  pBt->pCursor; p;
153cc 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
153cd 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62    if( p->isIncrb
153ce 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 69 73  lobHandle && (is
153cf 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 70 2d  ClearTable || p-
153d0 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77  >info.nKey==iRow
153d1 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53  ) ){.      p->eS
153d2 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
153d3 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 20 7d  VALID;.    }.  }
153d4 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 53  .}..#else.  /* S
153d5 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20 77 68  tub functions wh
153d6 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73 20 6f  en INCRBLOB is o
153d7 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64 65 66  mitted */.  #def
153d8 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76  ine invalidateOv
153d9 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20  erflowCache(x). 
153da 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64   #define invalid
153db 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61  ateAllOverflowCa
153dc 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65  che(x).  #define
153dd 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62   invalidateIncrb
153de 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 2c 7a  lobCursors(x,y,z
153df 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
153e0 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
153e1 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62   */../*.** Set b
153e2 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42  it pgno of the B
153e3 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74  tShared.pHasCont
153e4 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73  ent bitvec. This
153e5 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77   is called .** w
153e6 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20  hen a page that 
153e7 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61  previously conta
153e8 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65  ined data become
153e9 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  s a free-list le
153ea 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a  af .** page..**.
153eb 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e  ** The BtShared.
153ec 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
153ed 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72  ec exists to wor
153ee 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63  k around an obsc
153ef 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65  ure.** bug cause
153f0 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63  d by the interac
153f1 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66  tion of two usef
153f2 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69  ul IO optimizati
153f3 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a  ons surrounding.
153f4 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  ** free-list lea
153f5 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  f pages:.**.**  
153f6 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74   1) When all dat
153f7 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  a is deleted fro
153f8 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65  m a page and the
153f9 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a   page becomes.**
153fa 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73        a free-lis
153fb 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  t leaf page, the
153fc 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69   page is not wri
153fd 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
153fe 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73  base.**      (as
153ff 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
15400 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f  pages contain no
15401 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61   meaningful data
15402 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ). Sometimes.** 
15403 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65       such a page
15404 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75   is not even jou
15405 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77  rnalled (as it w
15406 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  ill not be modif
15407 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79  ied,.**      why
15408 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c   bother journall
15409 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20  ing it?)..**.** 
1540a 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65    2) When a free
1540b 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
1540c 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63  is reused, its c
1540d 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65  ontent is not re
1540e 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20  ad.**      from 
1540f 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
15410 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
15411 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79  ournal file (why
15412 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20   should it.**   
15413 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20     be, if it is 
15414 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69  not at all meani
15415 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42  ngful?)..**.** B
15416 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68  y themselves, th
15417 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ese optimization
15418 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20  s work fine and 
15419 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a  provide a handy.
1541a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ** performance b
1541b 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c  oost to bulk del
1541c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70  ete or insert op
1541d 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65  erations. Howeve
1541e 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20  r, if.** a page 
1541f 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  is moved to the 
15420 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68  free-list and th
15421 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e  en reused within
15422 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61   the same.** tra
15423 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62  nsaction, a prob
15424 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66  lem comes up. If
15425 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
15426 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e   journalled when
15427 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20  .** it is moved 
15428 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  to the free-list
15429 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20   and it is also 
1542a 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77  not journalled w
1542b 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74  hen it.** is ext
1542c 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  racted from the 
1542d 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65  free-list and re
1542e 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f  used, then the o
1542f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20  riginal data.** 
15430 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20  may be lost. In 
15431 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72  the event of a r
15432 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20  ollback, it may 
15433 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a  not be possible.
15434 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68  ** to restore th
15435 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
15436 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69  s original confi
15437 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  guration..**.** 
15438 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
15439 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
1543a 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e  sContent bitvec.
1543b 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65   Whenever a page
1543c 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f   is .** moved to
1543d 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c   become a free-l
1543e 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
1543f 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
15440 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69   bit is.** set i
15441 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68  n the bitvec. Wh
15442 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61  enever a leaf pa
15443 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20  ge is extracted 
15444 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69  from the free-li
15445 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74  st,.** optimizat
15446 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f  ion 2 above is o
15447 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 6f  mitted if the co
15448 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20  rresponding bit 
15449 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65  is already.** se
1544a 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 48  t in BtShared.pH
1544b 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 63  asContent. The c
1544c 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62  ontents of the b
1544d 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 65  itvec are cleare
1544e 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20  d.** at the end 
1544f 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 63  of every transac
15450 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
15451 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 43  int btreeSetHasC
15452 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20  ontent(BtShared 
15453 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
15454 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
15455 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70  ITE_OK;.  if( !p
15456 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20  Bt->pHasContent 
15457 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65  ){.    int nPage
15458 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c 69   = 100;.    sqli
15459 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
1545a 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  t(pBt->pPager, &
1545b 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20 49  nPage);.    /* I
1545c 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  f sqlite3PagerPa
1545d 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73 20  gecount() fails 
1545e 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72 6d  there is no harm
1545f 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20 20   because the.   
15460 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61 62   ** nPage variab
15461 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20  le is unchanged 
15462 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c 74  from its default
15463 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a 2f   value of 100 */
15464 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 6f  .    pBt->pHasCo
15465 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 42  ntent = sqlite3B
15466 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 32  itvecCreate((u32
15467 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  )nPage);.    if(
15468 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65   !pBt->pHasConte
15469 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  nt ){.      rc =
1546a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1546b 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72     }.  }.  if( r
1546c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1546d 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 74  pgno<=sqlite3Bit
1546e 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 61  vecSize(pBt->pHa
1546f 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 20  sContent) ){.   
15470 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74   rc = sqlite3Bit
15471 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61 73  vecSet(pBt->pHas
15472 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a  Content, pgno);.
15473 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
15474 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
15475 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
15476 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 2e  sContent vector.
15477 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
15478 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
15479 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20  hen a free-list 
1547a 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 6d  leaf page is rem
1547b 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  oved from the.**
1547c 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 72   free-list for r
1547d 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e 73  euse. It returns
1547e 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20   false if it is 
1547f 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76 65  safe to retrieve
15480 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 6f   the.** page fro
15481 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  m the pager laye
15482 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63  r with the 'no-c
15483 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74  ontent' flag set
15484 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73 65  . True otherwise
15485 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15486 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65  btreeGetHasConte
15487 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  nt(BtShared *pBt
15488 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
15489 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74 2d  Bitvec *p = pBt-
1548a 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 20  >pHasContent;.  
1548b 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 67  return (p && (pg
1548c 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65 63  no>sqlite3Bitvec
1548d 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 74  Size(p) || sqlit
1548e 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c 20  e3BitvecTest(p, 
1548f 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pgno)));.}../*.*
15490 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f 79  * Clear (destroy
15491 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  ) the BtShared.p
15492 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65  HasContent bitve
15493 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  c. This should b
15494 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 20  e.** invoked at 
15495 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
15496 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72 61  f each write-tra
15497 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  nsaction..*/.sta
15498 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43 6c  tic void btreeCl
15499 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 74  earHasContent(Bt
1549a 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
1549b 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
1549c 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 6f  troy(pBt->pHasCo
1549d 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e 70  ntent);.  pBt->p
1549e 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a  HasContent = 0;.
1549f 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68  }../*.** Save th
154a0 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72  e current cursor
154a1 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65   position in the
154a2 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72   variables BtCur
154a3 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64  sor.nKey .** and
154a4 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20   BtCursor.pKey. 
154a5 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 61  The cursor's sta
154a6 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 52  te is set to CUR
154a7 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e  SOR_REQUIRESEEK.
154a8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  .**.** The calle
154a9 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68  r must ensure th
154aa 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
154ab 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74 61   valid (has eSta
154ac 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
154ad 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 61  ).** prior to ca
154ae 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
154af 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ne.  .*/.static 
154b0 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f  int saveCursorPo
154b1 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20  sition(BtCursor 
154b2 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63  *pCur){.  int rc
154b3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52  ;..  assert( CUR
154b4 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d  SOR_VALID==pCur-
154b5 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73  >eState );.  ass
154b6 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b  ert( 0==pCur->pK
154b7 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
154b8 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
154b9 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20  (pCur) );..  rc 
154ba 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
154bb 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75  ySize(pCur, &pCu
154bc 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65  r->nKey);.  asse
154bd 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
154be 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a 65  K );  /* KeySize
154bf 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a  () cannot fail *
154c0 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  /..  /* If this 
154c1 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62  is an intKey tab
154c2 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 6f  le, then the abo
154c3 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65  ve call to Btree
154c4 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 73  KeySize().  ** s
154c5 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 65  tores the intege
154c6 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e 6e  r key in pCur->n
154c7 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73  Key. In this cas
154c8 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 0a  e this value is.
154c9 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73    ** all that is
154ca 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 72   required. Other
154cb 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 73  wise, if pCur is
154cc 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e 20   not open on an 
154cd 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 6c  intKey.  ** tabl
154ce 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 73  e, then malloc s
154cf 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 6f  pace for and sto
154d0 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b 65  re the pCur->nKe
154d1 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 0a  y bytes of key .
154d2 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f 0a    ** data..  */.
154d3 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e 61    if( 0==pCur->a
154d4 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
154d5 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b   ){.    void *pK
154d6 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ey = sqlite3Mall
154d7 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e  oc( (int)pCur->n
154d8 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 70  Key );.    if( p
154d9 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Key ){.      rc 
154da 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
154db 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29  y(pCur, 0, (int)
154dc 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79  pCur->nKey, pKey
154dd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
154de 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
154df 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79        pCur->pKey
154e0 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d   = pKey;.      }
154e1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
154e2 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29  lite3_free(pKey)
154e3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
154e4 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
154e5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
154e6 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
154e7 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ( !pCur->apPage[
154e8 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70  0]->intKey || !p
154e9 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20  Cur->pKey );..  
154ea 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
154eb 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  K ){.    int i;.
154ec 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d      for(i=0; i<=
154ed 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  pCur->iPage; i++
154ee 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
154ef 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
154f0 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 75  e[i]);.      pCu
154f1 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30  r->apPage[i] = 0
154f2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72  ;.    }.    pCur
154f3 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20  ->iPage = -1;.  
154f4 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
154f5 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
154f6 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 61  EEK;.  }..  inva
154f7 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61  lidateOverflowCa
154f8 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74  che(pCur);.  ret
154f9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
154fa 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69   Save the positi
154fb 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f  ons of all curso
154fc 72 73 20 28 65 78 63 65 70 74 20 70 45 78 63 65  rs (except pExce
154fd 70 74 29 20 74 68 61 74 20 61 72 65 20 6f 70 65  pt) that are ope
154fe 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 61 62 6c  n on.** the tabl
154ff 65 20 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  e  with root-pag
15500 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79  e iRoot. Usually
15501 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  , this is called
15502 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72   just before cur
15503 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69  sor.** pExcept i
15504 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79  s used to modify
15505 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65   the table (Btre
15506 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72  eDelete() or Btr
15507 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a  eeInsert())..*/.
15508 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41  static int saveA
15509 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72  llCursors(BtShar
1550a 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52  ed *pBt, Pgno iR
1550b 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70  oot, BtCursor *p
1550c 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72  Except){.  BtCur
1550d 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  sor *p;.  assert
1550e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1550f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
15510 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
15511 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63  xcept==0 || pExc
15512 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b  ept->pBt==pBt );
15513 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43  .  for(p=pBt->pC
15514 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
15515 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
15516 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d  !=pExcept && (0=
15517 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e  =iRoot || p->pgn
15518 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26  oRoot==iRoot) &&
15519 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74   .        p->eSt
1551a 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
1551b 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  D ){.      int r
1551c 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f  c = saveCursorPo
1551d 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20  sition(p);.     
1551e 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1551f 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  rc ){.        re
15520 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
15521 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
15522 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15523 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
15524 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72  e current cursor
15525 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   position..*/.SQ
15526 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
15527 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  d sqlite3BtreeCl
15528 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  earCursor(BtCurs
15529 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
1552a 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1552b 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1552c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
1552d 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75  ur->pKey);.  pCu
1552e 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70  r->pKey = 0;.  p
1552f 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
15530 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a  RSOR_INVALID;.}.
15531 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76  ./*.** In this v
15532 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d  ersion of BtreeM
15533 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61  oveto, pKey is a
15534 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65   packed index re
15535 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20  cord.** such as 
15536 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
15537 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  the OP_MakeRecor
15538 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63  d opcode.  Unpac
15539 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20  k the.** record 
1553a 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74  and then call Bt
1553b 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1553c 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  d() to do the wo
1553d 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rk..*/.static in
1553e 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20  t btreeMoveto(. 
1553f 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
15540 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
15541 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65  pen on the btree
15542 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20   to be searched 
15543 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
15544 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b  *pKey,   /* Pack
15545 65 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74  ed key if the bt
15546 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ree is an index 
15547 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20  */.  i64 nKey,  
15548 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
15549 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c  ger key for tabl
1554a 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65  es.  Size of pKe
1554b 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f  y for indices */
1554c 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20  .  int bias,    
1554d 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73         /* Bias s
1554e 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67  earch to the hig
1554f 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  h end */.  int *
15550 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f  pRes           /
15551 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72  * Write search r
15552 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29  esults here */.)
15553 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
15554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15555 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f  * Status code */
15556 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
15557 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a  d *pIdxKey;   /*
15558 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20   Unpacked index 
15559 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 61 53  key */.  char aS
1555a 70 61 63 65 5b 31 35 30 5d 3b 20 20 20 20 20 20  pace[150];      
1555b 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63      /* Temp spac
1555c 65 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20  e for pIdxKey - 
1555d 74 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f  to avoid a mallo
1555e 63 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79  c */..  if( pKey
1555f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
15560 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e 74 29  nKey==(i64)(int)
15561 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 64 78  nKey );.    pIdx
15562 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Key = sqlite3Vdb
15563 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43  eRecordUnpack(pC
15564 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69  ur->pKeyInfo, (i
15565 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20  nt)nKey, pKey,. 
15566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15568 20 20 20 20 20 61 53 70 61 63 65 2c 20 73 69 7a       aSpace, siz
15569 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a 20 20  eof(aSpace));.  
1556a 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30    if( pIdxKey==0
1556b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
1556c 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b  _NOMEM;.  }else{
1556d 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 30  .    pIdxKey = 0
1556e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
1556f 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
15570 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20 70 49  npacked(pCur, pI
15571 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 69 61  dxKey, nKey, bia
15572 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 28 20  s, pRes);.  if( 
15573 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69  pKey ){.    sqli
15574 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70  te3VdbeDeleteUnp
15575 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 78  ackedRecord(pIdx
15576 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Key);.  }.  retu
15577 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15578 52 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 73  Restore the curs
15579 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69  or to the positi
1557a 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 28 6f 72  on it was in (or
1557b 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 61 73 20   as close to as 
1557c 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 77 68 65  possible).** whe
1557d 6e 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69  n saveCursorPosi
1557e 74 69 6f 6e 28 29 20 77 61 73 20 63 61 6c 6c 65  tion() was calle
1557f 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69  d. Note that thi
15580 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 73 20 74  s call deletes t
15581 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 70 6f 73  he .** saved pos
15582 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 6f 72 65  ition info store
15583 64 20 62 79 20 73 61 76 65 43 75 72 73 6f 72 50  d by saveCursorP
15584 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f 20 74 68  osition(), so th
15585 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 61 74  ere can be.** at
15586 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 65 63 74   most one effect
15587 69 76 65 20 72 65 73 74 6f 72 65 43 75 72 73 6f  ive restoreCurso
15588 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6c 6c  rPosition() call
15589 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a 2a 20   after each .** 
1558a 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69  saveCursorPositi
1558b 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  on()..*/.static 
1558c 69 6e 74 20 62 74 72 65 65 52 65 73 74 6f 72 65  int btreeRestore
1558d 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42  CursorPosition(B
1558e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
1558f 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
15590 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
15591 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
15592 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
15593 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51  tate>=CURSOR_REQ
15594 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66  UIRESEEK );.  if
15595 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
15596 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a  CURSOR_FAULT ){.
15597 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d      return pCur-
15598 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  >skipNext;.  }. 
15599 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
1559a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
1559b 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65    rc = btreeMove
1559c 74 6f 28 70 43 75 72 2c 20 70 43 75 72 2d 3e 70  to(pCur, pCur->p
1559d 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c  Key, pCur->nKey,
1559e 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69 70 4e   0, &pCur->skipN
1559f 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ext);.  if( rc==
155a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
155a1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
155a2 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 70  ur->pKey);.    p
155a3 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20  Cur->pKey = 0;. 
155a4 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
155a5 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
155a6 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65  VALID || pCur->e
155a7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
155a8 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 20 72  VALID );.  }.  r
155a9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 64 65  eturn rc;.}..#de
155aa 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75 72 73  fine restoreCurs
155ab 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 5c 0a  orPosition(p) \.
155ac 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d 43 55    (p->eState>=CU
155ad 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
155ae 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20 62 74   ? \.         bt
155af 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72  reeRestoreCursor
155b0 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a  Position(p) : \.
155b1 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
155b2 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  OK)../*.** Deter
155b3 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
155b4 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 61 73  not a cursor has
155b5 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20   moved from the 
155b6 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77  position it.** w
155b7 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 20 61  as last placed a
155b8 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 6e 20  t.  Cursors can 
155b9 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 72 6f  move when the ro
155ba 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 6e 74  w they are point
155bb 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c  ing.** at is del
155bc 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e  eted out from un
155bd 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  der them..**.** 
155be 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
155bf 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f  urns an error co
155c0 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20  de if something 
155c1 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 68 65  goes wrong.  The
155c2 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 48 61  .** integer *pHa
155c3 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f  sMoved is set to
155c4 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 72 73   one if the curs
155c5 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 6e 64  or has moved and
155c6 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51   0 if not..*/.SQ
155c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
155c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
155c9 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75  sorHasMoved(BtCu
155ca 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
155cb 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69  *pHasMoved){.  i
155cc 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72  nt rc;..  rc = r
155cd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
155ce 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
155cf 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 48 61  ( rc ){.    *pHa
155d0 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20  sMoved = 1;.    
155d1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
155d2 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
155d3 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e!=CURSOR_VALID 
155d4 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  || pCur->skipNex
155d5 74 21 3d 30 20 29 7b 0a 20 20 20 20 2a 70 48 61  t!=0 ){.    *pHa
155d6 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65  sMoved = 1;.  }e
155d7 6c 73 65 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f  lse{.    *pHasMo
155d8 76 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ved = 0;.  }.  r
155d9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
155da 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
155db 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
155dc 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  UM./*.** Given a
155dd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
155de 61 20 72 65 67 75 6c 61 72 20 64 61 74 61 62 61  a regular databa
155df 73 65 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20  se page, return 
155e0 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62  the page.** numb
155e1 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74  er for the point
155e2 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74  er-map page that
155e3 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e   contains the en
155e4 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69  try for the.** i
155e5 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72  nput page number
155e6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f  ..*/.static Pgno
155e7 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74   ptrmapPageno(Bt
155e8 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
155e9 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e  o pgno){.  int n
155ea 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b  PagesPerMapPage;
155eb 0a 20 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c  .  Pgno iPtrMap,
155ec 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20   ret;.  assert( 
155ed 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
155ee 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
155ef 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70  ;.  nPagesPerMap
155f0 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61  Page = (pBt->usa
155f1 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20  bleSize/5)+1;.  
155f2 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d  iPtrMap = (pgno-
155f3 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50  2)/nPagesPerMapP
155f4 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50  age;.  ret = (iP
155f5 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d  trMap*nPagesPerM
155f6 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20  apPage) + 2; .  
155f7 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47  if( ret==PENDING
155f8 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
155f9 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20  ){.    ret++;.  
155fa 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  }.  return ret;.
155fb 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  }../*.** Write a
155fc 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65  n entry into the
155fd 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a   pointer map..**
155fe 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
155ff 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f 69   updates the poi
15600 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66  nter map entry f
15601 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27  or page number '
15602 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20  key'.** so that 
15603 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20  it maps to type 
15604 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72 65  'eType' and pare
15605 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27  nt page number '
15606 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pgno'..**.** If 
15607 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61 6c 6c  *pRC is initiall
15608 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f 6e 2d  y non-zero (non-
15609 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65 6e 20  SQLITE_OK) then 
1560a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a  this routine is.
1560b 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20  ** a no-op.  If 
1560c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
1560d 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
1560e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 77   error code is w
1560f 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a  ritten.** into *
15610 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pRC..*/.static v
15611 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28 42 74  oid ptrmapPut(Bt
15612 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
15613 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 65 2c  o key, u8 eType,
15614 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20 69 6e   Pgno parent, in
15615 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50 61 67  t *pRC){.  DbPag
15616 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20  e *pDbPage;  /* 
15617 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
15618 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50  page */.  u8 *pP
15619 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54  trmap;      /* T
1561a 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64  he pointer map d
1561b 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ata */.  Pgno iP
1561c 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68  trmap;     /* Th
1561d 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
1561e 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  ge number */.  i
1561f 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
15620 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f   /* Offset in po
15621 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
15622 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
15623 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
15624 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66 75 6e  code from subfun
15625 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 66 28  ctions */..  if(
15626 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a   *pRC ) return;.
15627 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15628 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
15629 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f  t->mutex) );.  /
1562a 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75  * The master-jou
1562b 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72  rnal page number
1562c 20 6d 75 73 74 20 6e 65 76 65 72 20 62 65 20 75   must never be u
1562d 73 65 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72  sed as a pointer
1562e 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61   map page */.  a
1562f 73 73 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50  ssert( 0==PTRMAP
15630 5f 49 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e  _ISPAGE(pBt, PEN
15631 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
15632 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72  Bt)) );..  asser
15633 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  t( pBt->autoVacu
15634 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d  um );.  if( key=
15635 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d  =0 ){.    *pRC =
15636 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15637 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e  BKPT;.    return
15638 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20  ;.  }.  iPtrmap 
15639 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  = PTRMAP_PAGENO(
1563a 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20  pBt, key);.  rc 
1563b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1563c 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
1563d 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
1563e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1563f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70  ITE_OK ){.    *p
15640 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74  RC = rc;.    ret
15641 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65  urn;.  }.  offse
15642 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  t = PTRMAP_PTROF
15643 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65  FSET(iPtrmap, ke
15644 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74  y);.  if( offset
15645 3c 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d  <0 ){.    *pRC =
15646 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15647 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 70  BKPT;.    goto p
15648 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 7d 0a  trmap_exit;.  }.
15649 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20    pPtrmap = (u8 
1564a 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
1564b 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a  tData(pDbPage);.
1564c 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 50  .  if( eType!=pP
1564d 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c  trmap[offset] ||
1564e 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d   get4byte(&pPtrm
1564f 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70  ap[offset+1])!=p
15650 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 41  arent ){.    TRA
15651 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 41  CE(("PTRMAP_UPDA
15652 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c  TE: %d->(%d,%d)\
15653 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c 20  n", key, eType, 
15654 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 2a 70  parent));.    *p
15655 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 74 65 33  RC= rc = sqlite3
15656 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61  PagerWrite(pDbPa
15657 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ge);.    if( rc=
15658 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15659 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73      pPtrmap[offs
1565a 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20  et] = eType;.   
1565b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74     put4byte(&pPt
1565c 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20  rmap[offset+1], 
1565d 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20  parent);.    }. 
1565e 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 69 74 3a   }..ptrmap_exit:
1565f 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55  .  sqlite3PagerU
15660 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 7d  nref(pDbPage);.}
15661 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20  ../*.** Read an 
15662 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70  entry from the p
15663 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a  ointer map..**.*
15664 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
15665 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f 69  etrieves the poi
15666 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66  nter map entry f
15667 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77  or page 'key', w
15668 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79  riting.** the ty
15669 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61  pe and parent pa
1566a 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45  ge number to *pE
1566b 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20  Type and *pPgno 
1566c 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a  respectively..**
1566d 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
1566e 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f  s returned if so
1566f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
15670 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51  ng, otherwise SQ
15671 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74  LITE_OK..*/.stat
15672 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74  ic int ptrmapGet
15673 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
15674 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45  Pgno key, u8 *pE
15675 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e  Type, Pgno *pPgn
15676 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  o){.  DbPage *pD
15677 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20  bPage;   /* The 
15678 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
15679 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61   */.  int iPtrma
1567a 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  p;       /* Poin
1567b 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64  ter map page ind
1567c 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72  ex */.  u8 *pPtr
1567d 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  map;       /* Po
1567e 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 64  inter map page d
1567f 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66  ata */.  int off
15680 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f  set;        /* O
15681 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69  ffset of entry i
15682 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f  n pointer map */
15683 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
15684 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15685 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15686 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d  tex) );..  iPtrm
15687 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45  ap = PTRMAP_PAGE
15688 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20  NO(pBt, key);.  
15689 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1568a 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72  rGet(pBt->pPager
1568b 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50  , iPtrmap, &pDbP
1568c 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
1568d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1568e 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61  rc;.  }.  pPtrma
1568f 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  p = (u8 *)sqlite
15690 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44  3PagerGetData(pD
15691 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65  bPage);..  offse
15692 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  t = PTRMAP_PTROF
15693 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65  FSET(iPtrmap, ke
15694 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  y);.  assert( pE
15695 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45  Type!=0 );.  *pE
15696 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f  Type = pPtrmap[o
15697 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 70 50  ffset];.  if( pP
15698 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67  gno ) *pPgno = g
15699 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70  et4byte(&pPtrmap
1569a 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20  [offset+1]);..  
1569b 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1569c 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66  f(pDbPage);.  if
1569d 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a  ( *pEType<1 || *
1569e 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 75 72  pEType>5 ) retur
1569f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
156a0 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 6e 20  _BKPT;.  return 
156a1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65  SQLITE_OK;.}..#e
156a2 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 6e 65  lse /* if define
156a3 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  d SQLITE_OMIT_AU
156a4 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64  TOVACUUM */.  #d
156a5 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 28  efine ptrmapPut(
156a6 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a 20 20 23 64  w,x,y,z,rc).  #d
156a7 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 74 28  efine ptrmapGet(
156a8 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f  w,x,y,z) SQLITE_
156a9 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72  OK.  #define ptr
156aa 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 78 2c  mapPutOvflPtr(x,
156ab 20 79 2c 20 72 63 29 0a 23 65 6e 64 69 66 0a 0a   y, rc).#endif..
156ac 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74  /*.** Given a bt
156ad 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 20 63  ree page and a c
156ae 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61  ell index (0 mea
156af 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 65 6c  ns the first cel
156b0 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65  l on.** the page
156b1 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65  , 1 means the se
156b2 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73  cond cell, and s
156b3 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20  o forth) return 
156b4 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
156b5 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
156b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
156b7 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20  tine works only 
156b8 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 64  for pages that d
156b9 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76  o not contain ov
156ba 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f  erflow cells..*/
156bb 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c  .#define findCel
156bc 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d  l(P,I) \.  ((P)-
156bd 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d  >aData + ((P)->m
156be 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 62 79  askPage & get2by
156bf 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61 5b 28  te(&(P)->aData[(
156c0 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32  P)->cellOffset+2
156c1 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20  *(I)])))../*.** 
156c2 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70  This a more comp
156c3 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66 20 66  lex version of f
156c4 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77  indCell() that w
156c5 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65  orks for.** page
156c6 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69  s that do contai
156c7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  n overflow cells
156c8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a  ..*/.static u8 *
156c9 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
156ca 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
156cb 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69   int iCell){.  i
156cc 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
156cd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
156ce 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
156cf 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
156d0 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f  =pPage->nOverflo
156d1 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  w-1; i>=0; i--){
156d2 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20  .    int k;.    
156d3 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c  struct _OvflCell
156d4 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76   *pOvfl;.    pOv
156d5 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76  fl = &pPage->aOv
156d6 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70  fl[i];.    k = p
156d7 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69  Ovfl->idx;.    i
156d8 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20  f( k<=iCell ){. 
156d9 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c       if( k==iCel
156da 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  l ){.        ret
156db 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c  urn pOvfl->pCell
156dc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
156dd 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20  iCell--;.    }. 
156de 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64   }.  return find
156df 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
156e0 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  l);.}../*.** Par
156e1 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  se a cell conten
156e2 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c  t block and fill
156e3 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f   in the CellInfo
156e4 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
156e5 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65  re.** are two ve
156e6 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66  rsions of this f
156e7 75 6e 63 74 69 6f 6e 2e 20 20 62 74 72 65 65 50  unction.  btreeP
156e8 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 73  arseCell() takes
156e9 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65   a .** cell inde
156ea 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  x as the second 
156eb 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 62 74 72  argument and btr
156ec 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29  eeParseCellPtr()
156ed 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69   .** takes a poi
156ee 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79  nter to the body
156ef 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20   of the cell as 
156f0 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
156f1 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69  ent..**.** Withi
156f2 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65  n this file, the
156f3 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63   parseCell() mac
156f4 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ro can be called
156f5 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62   instead of.** b
156f6 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
156f7 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 20 63  (). Using some c
156f8 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 20 77  ompilers, this w
156f9 69 6c 6c 20 62 65 20 66 61 73 74 65 72 2e 0a 2a  ill be faster..*
156fa 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74  /.static void bt
156fb 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
156fc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
156fd 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  e,         /* Pa
156fe 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ge containing th
156ff 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a  e cell */.  u8 *
15700 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  pCell,          
15701 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
15702 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e  o the cell text.
15703 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a   */.  CellInfo *
15704 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a  pInfo         /*
15705 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74   Fill in this st
15706 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20  ructure */.){.  
15707 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20 20 20  u16 n;          
15708 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15709 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 20  r bytes in cell 
1570a 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 2a  content header *
1570b 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64  /.  u32 nPayload
1570c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1570d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1570e 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a  f cell payload *
1570f 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
15710 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15711 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
15712 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e  x) );..  pInfo->
15713 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20  pCell = pCell;. 
15714 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15715 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65  leaf==0 || pPage
15716 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 6e  ->leaf==1 );.  n
15717 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50   = pPage->childP
15718 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74  trSize;.  assert
15719 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e  ( n==4-4*pPage->
1571a 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20 70 50  leaf );.  if( pP
1571b 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
1571c 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61     if( pPage->ha
1571d 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6e  sData ){.      n
1571e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
1571f 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c  &pCell[n], nPayl
15720 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  oad);.    }else{
15721 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61 64 20  .      nPayload 
15722 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  = 0;.    }.    n
15723 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28 26 70   += getVarint(&p
15724 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a 29 26  Cell[n], (u64*)&
15725 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20  pInfo->nKey);.  
15726 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d    pInfo->nData =
15727 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c   nPayload;.  }el
15728 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  se{.    pInfo->n
15729 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 6e 20  Data = 0;.    n 
1572a 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26  += getVarint32(&
1572b 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f  pCell[n], nPaylo
1572c 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  ad);.    pInfo->
1572d 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  nKey = nPayload;
1572e 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50  .  }.  pInfo->nP
1572f 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61  ayload = nPayloa
15730 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61  d;.  pInfo->nHea
15731 64 65 72 20 3d 20 6e 3b 0a 20 20 74 65 73 74 63  der = n;.  testc
15732 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70  ase( nPayload==p
15733 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29  Page->maxLocal )
15734 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50  ;.  testcase( nP
15735 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d  ayload==pPage->m
15736 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69  axLocal+1 );.  i
15737 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f  f( likely(nPaylo
15738 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ad<=pPage->maxLo
15739 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  cal) ){.    /* T
1573a 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73 79  his is the (easy
1573b 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68  ) common case wh
1573c 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 70  ere the entire p
1573d 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20  ayload fits.    
1573e 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ** on the local 
1573f 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c  page.  No overfl
15740 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ow is required..
15741 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e      */.    int n
15742 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Size;          /
15743 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20  * Total size of 
15744 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20  cell content in 
15745 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69  bytes */.    nSi
15746 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20  ze = nPayload + 
15747 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c  n;.    pInfo->nL
15748 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79  ocal = (u16)nPay
15749 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d  load;.    pInfo-
1574a 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >iOverflow = 0;.
1574b 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26      if( (nSize &
1574c 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20   ~3)==0 ){.     
1574d 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20   nSize = 4;     
1574e 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65     /* Minimum ce
1574f 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
15750 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d      }.    pInfo-
15751 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53  >nSize = (u16)nS
15752 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
15753 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c    /* If the payl
15754 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  oad will not fit
15755 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74   completely on t
15756 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77  he local page, w
15757 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f  e have.    ** to
15758 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68   decide how much
15759 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c   to store locall
1575a 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74  y and how much t
1575b 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20  o spill onto.   
1575c 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   ** overflow pag
1575d 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67  es.  The strateg
1575e 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65  y is to minimize
1575f 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75   the amount of u
15760 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61  nused.    ** spa
15761 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70  ce on overflow p
15762 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69  ages while keepi
15763 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ng the amount of
15764 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20   local storage. 
15765 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e     ** in between
15766 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61   minLocal and ma
15767 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20  xLocal..    **. 
15768 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20     ** Warning:  
15769 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79  changing the way
1576a 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61   overflow payloa
1576b 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64  d is distributed
1576c 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77   in any.    ** w
1576d 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ay will result i
1576e 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  n an incompatibl
1576f 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20  e file format.. 
15770 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69     */.    int mi
15771 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69  nLocal;  /* Mini
15772 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
15773 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
15774 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61  ly */.    int ma
15775 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69  xLocal;  /* Maxi
15776 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61  mum amount of pa
15777 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c  yload held local
15778 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75  ly */.    int su
15779 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72  rplus;   /* Over
1577a 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61  flow payload ava
1577b 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c  ilable for local
1577c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20   storage */..   
1577d 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67   minLocal = pPag
1577e 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20  e->minLocal;.   
1577f 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67   maxLocal = pPag
15780 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20  e->maxLocal;.   
15781 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f   surplus = minLo
15782 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20  cal + (nPayload 
15783 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61  - minLocal)%(pPa
15784 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
15785 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65  ize - 4);.    te
15786 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 3d  stcase( surplus=
15787 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20  =maxLocal );.   
15788 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 6c   testcase( surpl
15789 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 20 29  us==maxLocal+1 )
1578a 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75  ;.    if( surplu
1578b 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b  s <= maxLocal ){
1578c 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c  .      pInfo->nL
1578d 6f 63 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70  ocal = (u16)surp
1578e 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  lus;.    }else{.
1578f 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f        pInfo->nLo
15790 63 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f  cal = (u16)minLo
15791 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  cal;.    }.    p
15792 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
15793 3d 20 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e  = (u16)(pInfo->n
15794 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20  Local + n);.    
15795 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70  pInfo->nSize = p
15796 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
15797 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69  + 4;.  }.}.#defi
15798 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61  ne parseCell(pPa
15799 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f  ge, iCell, pInfo
1579a 29 20 5c 0a 20 20 62 74 72 65 65 50 61 72 73 65  ) \.  btreeParse
1579b 43 65 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c  CellPtr((pPage),
1579c 20 66 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65   findCell((pPage
1579d 29 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49  ), (iCell)), (pI
1579e 6e 66 6f 29 29 0a 73 74 61 74 69 63 20 76 6f 69  nfo)).static voi
1579f 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c  d btreeParseCell
157a0 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  (.  MemPage *pPa
157a1 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ge,         /* P
157a2 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  age containing t
157a3 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74  he cell */.  int
157a4 20 69 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20   iCell,         
157a5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c       /* The cell
157a6 20 69 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63   index.  First c
157a7 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65  ell is 0 */.  Ce
157a8 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20  llInfo *pInfo   
157a9 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e        /* Fill in
157aa 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
157ab 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65 43 65 6c  */.){.  parseCel
157ac 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20  l(pPage, iCell, 
157ad 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pInfo);.}../*.**
157ae 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74   Compute the tot
157af 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
157b0 65 73 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e  es that a Cell n
157b1 65 65 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c  eeds in the cell
157b2 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66  .** data area of
157b3 20 74 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e   the btree-page.
157b4 20 20 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d    The return num
157b5 62 65 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65  ber includes the
157b6 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65   cell.** data he
157b7 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63  ader and the loc
157b8 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20  al payload, but 
157b9 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  not any overflow
157ba 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20   page or.** the 
157bb 73 70 61 63 65 20 75 73 65 64 20 62 79 20 74 68  space used by th
157bc 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a  e cell pointer..
157bd 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 65  */.static u16 ce
157be 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 61 67  llSizePtr(MemPag
157bf 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43  e *pPage, u8 *pC
157c0 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49 74 65  ell){.  u8 *pIte
157c1 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50 61 67 65  r = &pCell[pPage
157c2 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 5d 3b  ->childPtrSize];
157c3 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b 0a 0a 23  .  u32 nSize;..#
157c4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
157c5 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c 75  UG.  /* The valu
157c6 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
157c7 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
157c8 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 68 65  ld always be the
157c9 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20 74 68   same as.  ** th
157ca 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 69 7a  e (CellInfo.nSiz
157cb 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64 20 62  e) value found b
157cc 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c 20 70  y doing a full p
157cd 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a  arse of the.  **
157ce 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49 54 45   cell. If SQLITE
157cf 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65  _DEBUG is define
157d0 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20 61  d, an assert() a
157d1 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 0a  t the bottom of.
157d2 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69    ** this functi
157d3 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68 61 74  on verifies that
157d4 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e 74 20   this invariant 
157d5 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65 64 2e  is not violated.
157d6 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 64   */.  CellInfo d
157d7 65 62 75 67 69 6e 66 6f 3b 0a 20 20 62 74 72 65  ebuginfo;.  btre
157d8 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
157d9 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62  age, pCell, &deb
157da 75 67 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 66 0a  uginfo);.#endif.
157db 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e  .  if( pPage->in
157dc 74 4b 65 79 20 29 7b 0a 20 20 20 20 75 38 20 2a  tKey ){.    u8 *
157dd 70 45 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 50  pEnd;.    if( pP
157de 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a  age->hasData ){.
157df 20 20 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67        pIter += g
157e0 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72  etVarint32(pIter
157e1 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 7d 65  , nSize);.    }e
157e2 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65  lse{.      nSize
157e3 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
157e4 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 6f   /* pIter now po
157e5 69 6e 74 73 20 61 74 20 74 68 65 20 36 34 2d 62  ints at the 64-b
157e6 69 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76  it integer key v
157e7 61 6c 75 65 2c 20 61 20 76 61 72 69 61 62 6c 65  alue, a variable
157e8 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a 20   length .    ** 
157e9 69 6e 74 65 67 65 72 2e 20 54 68 65 20 66 6f 6c  integer. The fol
157ea 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76  lowing block mov
157eb 65 73 20 70 49 74 65 72 20 74 6f 20 70 6f 69 6e  es pIter to poin
157ec 74 20 61 74 20 74 68 65 20 66 69 72 73 74 20 62  t at the first b
157ed 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 20  yte.    ** past 
157ee 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6b  the end of the k
157ef 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20  ey value. */.   
157f0 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39   pEnd = &pIter[9
157f1 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a  ];.    while( (*
157f2 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20 26 26  pIter++)&0x80 &&
157f3 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20   pIter<pEnd );. 
157f4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 74 65   }else{.    pIte
157f5 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  r += getVarint32
157f6 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a  (pIter, nSize);.
157f7 20 20 7d 0a 0a 20 20 74 65 73 74 63 61 73 65 28    }..  testcase(
157f8 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d   nSize==pPage->m
157f9 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73  axLocal );.  tes
157fa 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
157fb 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20  age->maxLocal+1 
157fc 29 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 70  );.  if( nSize>p
157fd 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29  Page->maxLocal )
157fe 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63  {.    int minLoc
157ff 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  al = pPage->minL
15800 6f 63 61 6c 3b 0a 20 20 20 20 6e 53 69 7a 65 20  ocal;.    nSize 
15801 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53  = minLocal + (nS
15802 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20  ize - minLocal) 
15803 25 20 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  % (pPage->pBt->u
15804 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
15805 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 53      testcase( nS
15806 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  ize==pPage->maxL
15807 6f 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74  ocal );.    test
15808 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61  case( nSize==pPa
15809 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29  ge->maxLocal+1 )
1580a 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a 65 3e  ;.    if( nSize>
1580b 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
1580c 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d  ){.      nSize =
1580d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d   minLocal;.    }
1580e 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d 20 34 3b  .    nSize += 4;
1580f 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 20 2b 3d 20  .  }.  nSize += 
15810 28 75 33 32 29 28 70 49 74 65 72 20 2d 20 70 43  (u32)(pIter - pC
15811 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  ell);..  /* The 
15812 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20  minimum size of 
15813 61 6e 79 20 63 65 6c 6c 20 69 73 20 34 20 62 79  any cell is 4 by
15814 74 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 53  tes. */.  if( nS
15815 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 6e 53 69  ize<4 ){.    nSi
15816 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a 0a 20 20 61  ze = 4;.  }..  a
15817 73 73 65 72 74 28 20 6e 53 69 7a 65 3d 3d 64 65  ssert( nSize==de
15818 62 75 67 69 6e 66 6f 2e 6e 53 69 7a 65 20 29 3b  buginfo.nSize );
15819 0a 20 20 72 65 74 75 72 6e 20 28 75 31 36 29 6e  .  return (u16)n
1581a 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  Size;.}..#ifdef 
1581b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 20  SQLITE_DEBUG./* 
1581c 54 68 69 73 20 76 61 72 69 61 74 69 6f 6e 20 6f  This variation o
1581d 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 29 20  n cellSizePtr() 
1581e 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f  is used inside o
1581f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  f assert() state
15820 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 2a  ments.** only. *
15821 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 6c  /.static u16 cel
15822 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70  lSize(MemPage *p
15823 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Page, int iCell)
15824 7b 0a 20 20 72 65 74 75 72 6e 20 63 65 6c 6c 53  {.  return cellS
15825 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 66 69  izePtr(pPage, fi
15826 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  ndCell(pPage, iC
15827 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ell));.}.#endif.
15828 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15829 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1582a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c  /*.** If the cel
1582b 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66  l pCell, part of
1582c 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74   page pPage cont
1582d 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  ains a pointer.*
1582e 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  * to an overflow
1582f 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e   page, insert an
15830 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
15831 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66  pointer-map.** f
15832 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
15833 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  page..*/.static 
15834 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 4f 76  void ptrmapPutOv
15835 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70  flPtr(MemPage *p
15836 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 2c  Page, u8 *pCell,
15837 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 43 65   int *pRC){.  Ce
15838 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 69  llInfo info;.  i
15839 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e  f( *pRC ) return
1583a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c  ;.  assert( pCel
1583b 6c 21 3d 30 20 29 3b 0a 20 20 62 74 72 65 65 50  l!=0 );.  btreeP
1583c 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
1583d 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
1583e 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66  ;.  assert( (inf
1583f 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e  o.nData+(pPage->
15840 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b  intKey?0:info.nK
15841 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c  ey))==info.nPayl
15842 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 69 6e 66  oad );.  if( inf
15843 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  o.iOverflow ){. 
15844 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67     Pgno ovfl = g
15845 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69  et4byte(&pCell[i
15846 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b  nfo.iOverflow]);
15847 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70  .    ptrmapPut(p
15848 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c  Page->pBt, ovfl,
15849 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1584a 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  1, pPage->pgno, 
1584b 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  pRC);.  }.}.#end
1584c 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61  if.../*.** Defra
1584d 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 20 67  gment the page g
1584e 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73  iven.  All Cells
1584f 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68   are moved to th
15850 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20  e.** end of the 
15851 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65  page and all fre
15852 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65  e space is colle
15853 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a  cted into one.**
15854 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61   big FreeBlk tha
15855 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77  t occurs in betw
15856 65 65 6e 20 74 68 65 20 68 65 61 64 65 72 20 61  een the header a
15857 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74  nd cell.** point
15858 65 72 20 61 72 72 61 79 20 61 6e 64 20 74 68 65  er array and the
15859 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
1585a 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ea..*/.static in
1585b 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65  t defragmentPage
1585c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
1585d 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
1585e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1585f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
15860 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20  /.  int pc;     
15861 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15862 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 69  * Address of a i
15863 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e  -th cell */.  in
15864 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20  t hdr;          
15865 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
15866 65 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68  et to the page h
15867 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73  eader */.  int s
15868 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
15869 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1586a 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74   a cell */.  int
1586b 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20   usableSize;    
1586c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1586d 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65  r of usable byte
1586e 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20  s on a page */. 
1586f 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b   int cellOffset;
15870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
15871 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c  ffset to the cel
15872 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20  l pointer array 
15873 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20  */.  int cbrk;  
15874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15875 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65  /* Offset to the
15876 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
15877 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c  ea */.  int nCel
15878 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
15879 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1587a 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67  cells on the pag
1587b 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
1587c 63 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20  char *data;     
1587d 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61    /* The page da
1587e 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ta */.  unsigned
1587f 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20   char *temp;    
15880 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20     /* Temp area 
15881 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  for cell content
15882 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46   */.  int iCellF
15883 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
15884 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61   /* First allowa
15885 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a  ble cell index *
15886 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73  /.  int iCellLas
15887 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
15888 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20  * Last possible 
15889 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a  cell index */...
1588a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1588b 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
1588c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1588d 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1588e 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
1588f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15890 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
15891 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50   <= SQLITE_MAX_P
15892 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73  AGE_SIZE );.  as
15893 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
15894 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61  erflow==0 );.  a
15895 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15896 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
15897 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
15898 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33    temp = sqlite3
15899 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 70  PagerTempSpace(p
1589a 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65  Page->pBt->pPage
1589b 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61  r);.  data = pPa
1589c 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72  ge->aData;.  hdr
1589d 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
1589e 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65  set;.  cellOffse
1589f 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  t = pPage->cellO
158a0 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d  ffset;.  nCell =
158a1 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
158a2 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d   assert( nCell==
158a3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
158a4 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62  dr+3]) );.  usab
158a5 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e  leSize = pPage->
158a6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
158a7 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 79  .  cbrk = get2by
158a8 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
158a9 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70  ;.  memcpy(&temp
158aa 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62  [cbrk], &data[cb
158ab 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20  rk], usableSize 
158ac 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20  - cbrk);.  cbrk 
158ad 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  = usableSize;.  
158ae 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c  iCellFirst = cel
158af 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c  lOffset + 2*nCel
158b0 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d  l;.  iCellLast =
158b1 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b   usableSize - 4;
158b2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43  .  for(i=0; i<nC
158b3 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75  ell; i++){.    u
158b4 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a  8 *pAddr;     /*
158b5 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70   The i-th cell p
158b6 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41  ointer */.    pA
158b7 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c  ddr = &data[cell
158b8 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20  Offset + i*2];. 
158b9 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65     pc = get2byte
158ba 28 70 41 64 64 72 29 3b 0a 20 20 20 20 74 65 73  (pAddr);.    tes
158bb 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c  tcase( pc==iCell
158bc 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73  First );.    tes
158bd 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c  tcase( pc==iCell
158be 4c 61 73 74 20 29 3b 0a 23 69 66 20 21 64 65 66  Last );.#if !def
158bf 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
158c0 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c  LE_OVERSIZE_CELL
158c1 5f 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a 20 54  _CHECK).    /* T
158c2 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hese conditions 
158c3 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
158c4 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20 62 74  n verified in bt
158c5 72 65 65 49 6e 69 74 50 61 67 65 28 29 0a 20 20  reeInitPage().  
158c6 20 20 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 45    ** if SQLITE_E
158c7 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43  NABLE_OVERSIZE_C
158c8 45 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64 65 66  ELL_CHECK is def
158c9 69 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ined .    */.   
158ca 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72   if( pc<iCellFir
158cb 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61  st || pc>iCellLa
158cc 73 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  st ){.      retu
158cd 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
158ce 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65  T_BKPT;.    }.#e
158cf 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28  ndif.    assert(
158d0 20 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20   pc>=iCellFirst 
158d1 26 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74  && pc<=iCellLast
158d2 20 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63   );.    size = c
158d3 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
158d4 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20  , &temp[pc]);.  
158d5 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a    cbrk -= size;.
158d6 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
158d7 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49  TE_ENABLE_OVERSI
158d8 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20  ZE_CELL_CHECK). 
158d9 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c     if( cbrk<iCel
158da 6c 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20  lFirst ){.      
158db 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
158dc 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
158dd 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20  }.#else.    if( 
158de 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20  cbrk<iCellFirst 
158df 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c  || pc+size>usabl
158e0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72  eSize ){.      r
158e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
158e2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
158e3 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65  .#endif.    asse
158e4 72 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75  rt( cbrk+size<=u
158e5 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72  sableSize && cbr
158e6 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b  k>=iCellFirst );
158e7 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63  .    testcase( c
158e8 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65  brk+size==usable
158e9 53 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74  Size );.    test
158ea 63 61 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75  case( pc+size==u
158eb 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20  sableSize );.   
158ec 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62   memcpy(&data[cb
158ed 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20  rk], &temp[pc], 
158ee 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62  size);.    put2b
158ef 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29  yte(pAddr, cbrk)
158f0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
158f1 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74  cbrk>=iCellFirst
158f2 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   );.  put2byte(&
158f3 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72  data[hdr+5], cbr
158f4 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31  k);.  data[hdr+1
158f5 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64  ] = 0;.  data[hd
158f6 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61  r+2] = 0;.  data
158f7 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d  [hdr+7] = 0;.  m
158f8 65 6d 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c  emset(&data[iCel
158f9 6c 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b  lFirst], 0, cbrk
158fa 2d 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20  -iCellFirst);.  
158fb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
158fc 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
158fd 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
158fe 29 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d 69 43  );.  if( cbrk-iC
158ff 65 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d  ellFirst!=pPage-
15900 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65  >nFree ){.    re
15901 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15902 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
15903 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15904 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
15905 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20  ate nByte bytes 
15906 6f 66 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69  of space from wi
15907 74 68 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20  thin the B-Tree 
15908 70 61 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61  page passed.** a
15909 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
1590a 6d 65 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f  ment. Write into
1590b 20 2a 70 49 64 78 20 74 68 65 20 69 6e 64 65 78   *pIdx the index
1590c 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61   into pPage->aDa
1590d 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ta[].** of the f
1590e 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c  irst byte of all
1590f 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 52 65  ocated space. Re
15910 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49  turn either SQLI
15911 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65  TE_OK or.** an e
15912 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c  rror code (usual
15913 6c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  ly SQLITE_CORRUP
15914 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  T)..**.** The ca
15915 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20  ller guarantees 
15916 74 68 61 74 20 74 68 65 72 65 20 69 73 20 73 75  that there is su
15917 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74  fficient space t
15918 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c  o make the.** al
15919 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20  location.  This 
1591a 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65  routine might ne
1591b 65 64 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74  ed to defragment
1591c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69   in order to bri
1591d 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70  ng.** all the sp
1591e 61 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f  ace together, ho
1591f 77 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75  wever.  This rou
15920 74 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20  tine will avoid 
15921 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72  using.** the fir
15922 73 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73  st two bytes pas
15923 74 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  t the cell point
15924 65 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72  er area since pr
15925 65 73 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a  esumably this.**
15926 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62   allocation is b
15927 65 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64  eing made in ord
15928 65 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e  er to insert a n
15929 65 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77  ew cell, so we w
1592a 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20  ill.** also end 
1592b 75 70 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77  up needing a new
1592c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a   cell pointer..*
1592d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c  /.static int all
1592e 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61  ocateSpace(MemPa
1592f 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e  ge *pPage, int n
15930 42 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29  Byte, int *pIdx)
15931 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64  {.  const int hd
15932 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
15933 66 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61  fset;    /* Loca
15934 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65  l cache of pPage
15935 2d 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20  ->hdrOffset */. 
15936 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61   u8 * const data
15937 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
15938 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63        /* Local c
15939 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61  ache of pPage->a
1593a 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  Data */.  int nF
1593b 72 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  rag;            
1593c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1593d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67  * Number of frag
1593e 6d 65 6e 74 65 64 20 62 79 74 65 73 20 6f 6e 20  mented bytes on 
1593f 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74  pPage */.  int t
15940 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
15941 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15942 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
15943 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
15944 65 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b  ea */.  int gap;
15945 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
15946 20 62 79 74 65 20 6f 66 20 67 61 70 20 62 65 74   byte of gap bet
15947 77 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65  ween cell pointe
15948 72 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74  rs and cell cont
15949 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ent */.  int rc;
1594a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
1594b 67 65 72 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ger return code 
1594c 2a 2f 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  */.  .  assert( 
1594d 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1594e 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
1594f 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
15950 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20  ert( pPage->pBt 
15951 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15952 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15953 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
15954 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
15955 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20  nByte>=0 );  /* 
15956 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  Minimum cell siz
15957 65 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65  e is 4 */.  asse
15958 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  rt( pPage->nFree
15959 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73  >=nByte );.  ass
1595a 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
1595b 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  rflow==0 );.  as
1595c 73 65 72 74 28 20 6e 42 79 74 65 3c 70 50 61 67  sert( nByte<pPag
1595d 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
1595e 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72 61 67  ze-8 );..  nFrag
1595f 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a   = data[hdr+7];.
15960 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15961 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68  >cellOffset == h
15962 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67  dr + 12 - 4*pPag
15963 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67 61 70  e->leaf );.  gap
15964 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
15965 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e  fset + 2*pPage->
15966 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d 20 67  nCell;.  top = g
15967 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
15968 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 61 70  r+5]);.  if( gap
15969 3e 74 6f 70 20 29 20 72 65 74 75 72 6e 20 53 51  >top ) return SQ
1596a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1596b 54 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67  T;.  testcase( g
1596c 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74  ap+2==top );.  t
1596d 65 73 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d  estcase( gap+1==
1596e 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73  top );.  testcas
1596f 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 0a  e( gap==top );..
15970 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20    if( nFrag>=60 
15971 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73  ){.    /* Always
15972 20 64 65 66 72 61 67 6d 65 6e 74 20 68 69 67 68   defragment high
15973 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 20 70 61  ly fragmented pa
15974 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ges */.    rc = 
15975 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70  defragmentPage(p
15976 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
15977 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
15978 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74     top = get2byt
15979 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
1597a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 70  .  }else if( gap
1597b 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 2f  +2<=top ){.    /
1597c 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72 65  * Search the fre
1597d 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f  elist looking fo
1597e 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69  r a free slot bi
1597f 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69  g enough to sati
15980 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20  sfy .    ** the 
15981 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c 6c  request. The all
15982 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 20  ocation is made 
15983 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 66  from the first f
15984 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20  ree slot in .   
15985 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68 61   ** the list tha
15986 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67  t is large enoug
15987 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65 20  h to accomadate 
15988 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  it..    */.    i
15989 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20 20  nt pc, addr;.   
1598a 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31 3b   for(addr=hdr+1;
1598b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28   (pc = get2byte(
1598c 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b  &data[addr]))>0;
1598d 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 20   addr=pc){.     
1598e 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 74 32   int size = get2
1598f 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d  byte(&data[pc+2]
15990 29 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  );     /* Size o
15991 66 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20  f free slot */. 
15992 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e       if( size>=n
15993 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Byte ){.        
15994 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e  int x = size - n
15995 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65  Byte;.        te
15996 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a  stcase( x==4 );.
15997 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
15998 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20  ( x==3 );.      
15999 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20    if( x<4 ){.   
1599a 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65         /* Remove
1599b 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74   the slot from t
1599c 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70  he free-list. Up
1599d 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  date the number 
1599e 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  of.          ** 
1599f 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73  fragmented bytes
159a0 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65   within the page
159a1 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d  . */.          m
159a2 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72  emcpy(&data[addr
159a3 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29  ], &data[pc], 2)
159a4 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  ;.          data
159a5 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e  [hdr+7] = (u8)(n
159a6 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20  Frag + x);.     
159a7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
159a8 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20      /* The slot 
159a9 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66  remains on the f
159aa 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65  ree-list. Reduce
159ab 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63   its size to acc
159ac 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a  ount.          *
159ad 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f  * for the portio
159ae 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65  n used by the ne
159af 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f  w allocation. */
159b0 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62  .          put2b
159b1 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c  yte(&data[pc+2],
159b2 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   x);.        }. 
159b3 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70         *pIdx = p
159b4 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72  c + x;.        r
159b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
159b6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
159b7 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
159b8 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72  o make sure ther
159b9 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
159ba 65 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20  e in the gap to 
159bb 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65  satisfy.  ** the
159bc 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66   allocation.  If
159bd 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74   not, defragment
159be 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73  ..  */.  testcas
159bf 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d  e( gap+2+nByte==
159c0 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70  top );.  if( gap
159c1 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a  +2+nByte>top ){.
159c2 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d      rc = defragm
159c3 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  entPage(pPage);.
159c4 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
159c5 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20  urn rc;.    top 
159c6 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
159c7 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73  [hdr+5]);.    as
159c8 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c  sert( gap+nByte<
159c9 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20  =top );.  }...  
159ca 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
159cb 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20  ry from the gap 
159cc 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  in between the c
159cd 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
159ce 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63  y.  ** and the c
159cf 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
159d0 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74  .  The btreeInit
159d1 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20  Page() call has 
159d2 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c  already.  ** val
159d3 69 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c  idated the freel
159d4 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74  ist.  Given that
159d5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73   the freelist is
159d6 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20   valid, there.  
159d7 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61  ** is no way tha
159d8 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  t the allocation
159d9 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20   can extend off 
159da 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
159db 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73  age..  ** The as
159dc 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72  sert() below ver
159dd 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f  ifies the previo
159de 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a  us sentence..  *
159df 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65  /.  top -= nByte
159e0 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
159e1 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b  ta[hdr+5], top);
159e2 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e  .  assert( top+n
159e3 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70  Byte <= pPage->p
159e4 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
159e5 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b  ;.  *pIdx = top;
159e6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
159e7 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  _OK;.}../*.** Re
159e8 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f  turn a section o
159e9 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61  f the pPage->aDa
159ea 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ta to the freeli
159eb 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  st..** The first
159ec 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77   byte of the new
159ed 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70   free block is p
159ee 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72  Page->aDisk[star
159ef 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69  t].** and the si
159f0 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20  ze of the block 
159f1 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e  is "size" bytes.
159f2 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74  .**.** Most of t
159f3 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69  he effort here i
159f4 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f  s involved in co
159f5 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74  alesing adjacent
159f6 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20  .** free blocks 
159f7 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69  into a single bi
159f8 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f  g free block..*/
159f9 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65  .static int free
159fa 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70  Space(MemPage *p
159fb 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c  Page, int start,
159fc 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e   int size){.  in
159fd 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20  t addr, pbegin, 
159fe 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74  hdr;.  int iLast
159ff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
15a01 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65  est possible fre
15a02 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f  eblock offset */
15a03 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15a04 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e   *data = pPage->
15a05 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74  aData;..  assert
15a06 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20  ( pPage->pBt!=0 
15a07 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15a08 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
15a09 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
15a0a 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
15a0b 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e  ( start>=pPage->
15a0c 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67  hdrOffset+6+pPag
15a0d 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20  e->childPtrSize 
15a0e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74  );.  assert( (st
15a0f 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61  art + size)<=pPa
15a10 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
15a11 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ize );.  assert(
15a12 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15a13 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15a14 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
15a15 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20  rt( size>=0 );  
15a16 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
15a17 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23   size is 4 */..#
15a18 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43  ifdef SQLITE_SEC
15a19 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20  URE_DELETE.  /* 
15a1a 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65  Overwrite delete
15a1b 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69  d information wi
15a1c 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68  th zeros when th
15a1d 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  e SECURE_DELETE 
15a1e 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20  .  ** option is 
15a1f 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69  enabled at compi
15a20 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d  le-time */.  mem
15a21 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d  set(&data[start]
15a22 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64  , 0, size);.#end
15a23 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  if..  /* Add the
15a24 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f   space back into
15a25 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
15a26 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20   of freeblocks. 
15a27 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20   Note that.  ** 
15a28 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  even though the 
15a29 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77  freeblock list w
15a2a 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74  as checked by bt
15a2b 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20  reeInitPage(),. 
15a2c 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67   ** btreeInitPag
15a2d 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65  e() did not dete
15a2e 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63  ct overlapping c
15a2f 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65  ells or.  ** fre
15a30 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65  eblocks that ove
15a31 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20  rlapped cells.  
15a32 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74   Nor does it det
15a33 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a  ect when the.  *
15a34 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  * cell content a
15a35 72 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20  rea exceeds the 
15a36 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67  value in the pag
15a37 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68  e header.  If th
15a38 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69  ese.  ** situati
15a39 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20  ons arise, then 
15a3a 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72  subsequent inser
15a3b 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67  t operations mig
15a3c 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20  ht corrupt.  ** 
15a3d 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53  the freelist.  S
15a3e 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20  o we do need to 
15a3f 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70  check for corrup
15a40 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e  tion while scann
15a41 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65  ing.  ** the fre
15a42 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64  elist..  */.  hd
15a43 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
15a44 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68  fset;.  addr = h
15a45 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20  dr + 1;.  iLast 
15a46 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
15a47 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20  ableSize - 4;.  
15a48 61 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69  assert( start<=i
15a49 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28  Last );.  while(
15a4a 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62   (pbegin = get2b
15a4b 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29  yte(&data[addr])
15a4c 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69  )<start && pbegi
15a4d 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  n>0 ){.    if( p
15a4e 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a  begin<addr+4 ){.
15a4f 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a50 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a51 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72  ;.    }.    addr
15a52 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20   = pbegin;.  }. 
15a53 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73   if( pbegin>iLas
15a54 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
15a55 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
15a56 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  KPT;.  }.  asser
15a57 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c  t( pbegin>addr |
15a58 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20  | pbegin==0 );. 
15a59 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15a5a 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20  addr], start);. 
15a5b 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15a5c 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b  start], pbegin);
15a5d 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
15a5e 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65  a[start+2], size
15a5f 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
15a60 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  e = pPage->nFree
15a61 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20   + (u16)size;.. 
15a62 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a   /* Coalesce adj
15a63 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b  acent free block
15a64 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64  s */.  addr = hd
15a65 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  r + 1;.  while( 
15a66 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79  (pbegin = get2by
15a67 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29  te(&data[addr]))
15a68 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e  >0 ){.    int pn
15a69 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20  ext, psize, x;. 
15a6a 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69     assert( pbegi
15a6b 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73  n>addr );.    as
15a6c 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
15a6d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
15a6e 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e  Size-4 );.    pn
15a6f 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26  ext = get2byte(&
15a70 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20  data[pbegin]);. 
15a71 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62     psize = get2b
15a72 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
15a73 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62  +2]);.    if( pb
15a74 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33  egin + psize + 3
15a75 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65   >= pnext && pne
15a76 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  xt>0 ){.      in
15a77 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d  t frag = pnext -
15a78 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b   (pbegin+psize);
15a79 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67  .      if( (frag
15a7a 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e  <0) || (frag>(in
15a7b 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29  t)data[hdr+7]) )
15a7c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
15a7d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15a7e 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BKPT;.      }.  
15a7f 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20      data[hdr+7] 
15a80 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20  -= (u8)frag;.   
15a81 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28     x = get2byte(
15a82 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20  &data[pnext]);. 
15a83 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64       put2byte(&d
15a84 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b  ata[pbegin], x);
15a85 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74  .      x = pnext
15a86 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74   + get2byte(&dat
15a87 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62  a[pnext+2]) - pb
15a88 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32  egin;.      put2
15a89 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
15a8a 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65  n+2], x);.    }e
15a8b 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20  lse{.      addr 
15a8c 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a  = pbegin;.    }.
15a8d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
15a8e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
15a8f 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61  ea begins with a
15a90 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f   freeblock, remo
15a91 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20  ve it. */.  if( 
15a92 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74  data[hdr+1]==dat
15a93 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61  a[hdr+5] && data
15a94 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64  [hdr+2]==data[hd
15a95 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  r+6] ){.    int 
15a96 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20  top;.    pbegin 
15a97 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15a98 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65  [hdr+1]);.    me
15a99 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31  mcpy(&data[hdr+1
15a9a 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d  ], &data[pbegin]
15a9b 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20  , 2);.    top = 
15a9c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
15a9d 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74  dr+5]) + get2byt
15a9e 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32  e(&data[pbegin+2
15a9f 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65  ]);.    put2byte
15aa0 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74  (&data[hdr+5], t
15aa1 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  op);.  }.  asser
15aa2 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
15aa3 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
15aa4 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
15aa5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15aa6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64  ;.}../*.** Decod
15aa7 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65  e the flags byte
15aa8 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65   (the first byte
15aa9 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20   of the header) 
15aaa 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e  for a page.** an
15aab 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65  d initialize fie
15aac 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61  lds of the MemPa
15aad 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63  ge structure acc
15aae 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ordingly..**.** 
15aaf 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  Only the followi
15ab0 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20  ng combinations 
15ab1 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20  are supported.  
15ab2 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65  Anything differe
15ab3 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20  nt.** indicates 
15ab4 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
15ab5 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  se files:.**.** 
15ab6 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f          PTF_ZERO
15ab7 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  DATA.**         
15ab8 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50  PTF_ZERODATA | P
15ab9 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20  TF_LEAF.**      
15aba 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20     PTF_LEAFDATA 
15abb 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20  | PTF_INTKEY.** 
15abc 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46          PTF_LEAF
15abd 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45  DATA | PTF_INTKE
15abe 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a  Y | PTF_LEAF.*/.
15abf 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64  static int decod
15ac0 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a  eFlags(MemPage *
15ac1 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42  pPage, int flagB
15ac2 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  yte){.  BtShared
15ac3 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20   *pBt;     /* A 
15ac4 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70  copy of pPage->p
15ac5 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  Bt */..  assert(
15ac6 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15ac7 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t==(pPage->pgno=
15ac8 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b  =1 ? 100 : 0) );
15ac9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15aca 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
15acb 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
15acc 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61   );.  pPage->lea
15acd 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74  f = (u8)(flagByt
15ace 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20  e>>3);  assert( 
15acf 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33  PTF_LEAF == 1<<3
15ad0 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26   );.  flagByte &
15ad1 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70  = ~PTF_LEAF;.  p
15ad2 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
15ad3 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e  ze = 4-4*pPage->
15ad4 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50  leaf;.  pBt = pP
15ad5 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20  age->pBt;.  if( 
15ad6 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c  flagByte==(PTF_L
15ad7 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e  EAFDATA | PTF_IN
15ad8 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61  TKEY) ){.    pPa
15ad9 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a  ge->intKey = 1;.
15ada 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61      pPage->hasDa
15adb 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  ta = pPage->leaf
15adc 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78  ;.    pPage->max
15add 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78  Local = pBt->max
15ade 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  Leaf;.    pPage-
15adf 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >minLocal = pBt-
15ae0 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73  >minLeaf;.  }els
15ae1 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d  e if( flagByte==
15ae2 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a  PTF_ZERODATA ){.
15ae3 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65      pPage->intKe
15ae4 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  y = 0;.    pPage
15ae5 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20  ->hasData = 0;. 
15ae6 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63     pPage->maxLoc
15ae7 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63  al = pBt->maxLoc
15ae8 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d  al;.    pPage->m
15ae9 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d  inLocal = pBt->m
15aea 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65  inLocal;.  }else
15aeb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
15aec 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15aed 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
15aee 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
15aef 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
15af0 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f  e auxiliary info
15af1 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69  rmation for a di
15af2 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  sk block..**.** 
15af3 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
15af4 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66   on success.  If
15af5 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65   we see that the
15af6 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f   page does.** no
15af7 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c  t contain a well
15af8 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  -formed database
15af9 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75   page, then retu
15afa 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f  rn .** SQLITE_CO
15afb 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61  RRUPT.  Note tha
15afc 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51  t a return of SQ
15afd 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74  LITE_OK does not
15afe 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68  .** guarantee th
15aff 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77  at the page is w
15b00 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20  ell-formed.  It 
15b01 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a  only shows that.
15b02 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  ** we failed to 
15b03 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75  detect any corru
15b04 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ption..*/.static
15b05 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61   int btreeInitPa
15b06 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
15b07 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  e){..  assert( p
15b08 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
15b09 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15b0a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15b0b 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15b0c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
15b0d 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65  ge->pgno==sqlite
15b0e 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
15b0f 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15b10 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
15b11 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61  age == sqlite3Pa
15b12 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
15b13 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
15b14 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15b15 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33  aData == sqlite3
15b16 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
15b17 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
15b18 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69  .  if( !pPage->i
15b19 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36  sInit ){.    u16
15b1a 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
15b1b 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20  /* Address of a 
15b1c 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e  freeblock within
15b1d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20   pPage->aData[] 
15b1e 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20  */.    u8 hdr;  
15b1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
15b20 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67  set to beginning
15b21 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20   of page header 
15b22 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b  */.    u8 *data;
15b23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75            /* Equ
15b24 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61  al to pPage->aDa
15b25 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72  ta */.    BtShar
15b26 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
15b27 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65  /* The main btre
15b28 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
15b29 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a     u16 usableSiz
15b2a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  e;    /* Amount 
15b2b 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20  of usable space 
15b2c 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
15b2d 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73      u16 cellOffs
15b2e 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
15b2f 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70   from start of p
15b30 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c  age to first cel
15b31 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  l pointer */.   
15b32 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
15b33 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
15b34 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e   unused bytes on
15b35 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
15b36 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20   u16 top;       
15b37 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
15b38 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
15b39 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20  ntent area */.  
15b3a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74    int iCellFirst
15b3b 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c  ;    /* First al
15b3c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20  lowable cell or 
15b3d 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74  freeblock offset
15b3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c   */.    int iCel
15b3f 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61  lLast;     /* La
15b40 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c  st possible cell
15b41 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66   or freeblock of
15b42 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74  fset */..    pBt
15b43 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a   = pPage->pBt;..
15b44 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d      hdr = pPage-
15b45 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20  >hdrOffset;.    
15b46 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
15b47 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63  ata;.    if( dec
15b48 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20  odeFlags(pPage, 
15b49 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74  data[hdr]) ) ret
15b4a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15b4b 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73  PT_BKPT;.    ass
15b4c 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69  ert( pBt->pageSi
15b4d 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e  ze>=512 && pBt->
15b4e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20  pageSize<=32768 
15b4f 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  );.    pPage->ma
15b50 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61  skPage = pBt->pa
15b51 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20  geSize - 1;.    
15b52 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
15b53 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65   = 0;.    usable
15b54 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Size = pBt->usab
15b55 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67  leSize;.    pPag
15b56 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20  e->cellOffset = 
15b57 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72  cellOffset = hdr
15b58 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d   + 12 - 4*pPage-
15b59 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d  >leaf;.    top =
15b5a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15b5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61  hdr+5]);.    pPa
15b5c 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32  ge->nCell = get2
15b5d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
15b5e 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  ]);.    if( pPag
15b5f 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c  e->nCell>MX_CELL
15b60 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f  (pBt) ){.      /
15b61 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20  * To many cells 
15b62 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67  for a single pag
15b63 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73  e.  The page mus
15b64 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a  t be corrupt */.
15b65 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15b66 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15b67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74  ;.    }.    test
15b68 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65  case( pPage->nCe
15b69 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29  ll==MX_CELL(pBt)
15b6a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61   );..    /* A ma
15b6b 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  lformed database
15b6c 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73   page might caus
15b6d 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73  e us to read pas
15b6e 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a  t the end.    **
15b6f 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61   of page when pa
15b70 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a  rsing a cell.  .
15b71 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
15b72 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
15b73 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73  k of code checks
15b74 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66   early to see if
15b75 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a   a cell extends.
15b76 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20      ** past the 
15b77 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f  end of a page bo
15b78 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65  undary and cause
15b79 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  s SQLITE_CORRUPT
15b7a 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72   to be .    ** r
15b7b 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f  eturned if it do
15b7c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  es..    */.    i
15b7d 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c  CellFirst = cell
15b7e 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65  Offset + 2*pPage
15b7f 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65  ->nCell;.    iCe
15b80 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53  llLast = usableS
15b81 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66  ize - 4;.#if def
15b82 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
15b83 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c  LE_OVERSIZE_CELL
15b84 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20  _CHECK).    {.  
15b85 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
15b86 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
15b87 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69  nto the cell poi
15b88 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20  nter array */.  
15b89 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20      int sz;     
15b8a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
15b8b 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20   a cell */..    
15b8c 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
15b8d 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d  af ) iCellLast--
15b8e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
15b8f 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   i<pPage->nCell;
15b90 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   i++){.        p
15b91 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  c = get2byte(&da
15b92 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a  ta[cellOffset+i*
15b93 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  2]);.        tes
15b94 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c  tcase( pc==iCell
15b95 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20  First );.       
15b96 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69   testcase( pc==i
15b97 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20  CellLast );.    
15b98 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c      if( pc<iCell
15b99 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c  First || pc>iCel
15b9a 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20  lLast ){.       
15b9b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15b9c 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15b9d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15b9e 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74   sz = cellSizePt
15b9f 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
15ba0 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  c]);.        tes
15ba1 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73  tcase( pc+sz==us
15ba2 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
15ba3 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73      if( pc+sz>us
15ba4 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  ableSize ){.    
15ba5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15ba6 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15ba7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15ba8 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70    }.      if( !p
15ba9 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65  Page->leaf ) iCe
15baa 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20  llLast++;.    } 
15bab 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a   .#endif..    /*
15bac 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74   Compute the tot
15bad 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e  al free space on
15bae 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
15baf 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26   pc = get2byte(&
15bb0 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20  data[hdr+1]);.  
15bb1 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68    nFree = data[h
15bb2 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20  dr+7] + top;.   
15bb3 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a   while( pc>0 ){.
15bb4 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20        u16 next, 
15bb5 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  size;.      if( 
15bb6 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c  pc<iCellFirst ||
15bb7 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b   pc>iCellLast ){
15bb8 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72  .        /* Star
15bb9 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20  t of free block 
15bba 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
15bbb 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
15bbc 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15bbd 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
15bbe 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
15bbf 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29  2byte(&data[pc])
15bc0 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  ;.      size = g
15bc1 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
15bc2 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
15bc3 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c  (next>0 && next<
15bc4 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70  =pc+size+3) || p
15bc5 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a  c+size>usableSiz
15bc6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e ){.        /* 
15bc7 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74  Free blocks must
15bc8 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67   be in ascending
15bc9 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20   order. And the 
15bca 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a  last byte of..**
15bcb 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20   the free-block 
15bcc 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20  must lie on the 
15bcd 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20  database page.  
15bce 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
15bcf 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15bd0 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
15bd1 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46        nFree = nF
15bd2 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20  ree + size;.    
15bd3 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20    pc = next;.   
15bd4 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   }..    /* At th
15bd5 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20  is point, nFree 
15bd6 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d  contains the sum
15bd7 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74   of the offset t
15bd8 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20  o the start.    
15bd9 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63  ** of the cell-c
15bda 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73  ontent area plus
15bdb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
15bdc 72 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e  ree bytes within
15bdd 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c  .    ** the cell
15bde 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49  -content area. I
15bdf 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65  f this is greate
15be0 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c  r than the usabl
15be1 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66  e-size.    ** of
15be2 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20   the page, then 
15be3 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65  the page must be
15be4 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73   corrupted. This
15be5 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20   check also.    
15be6 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72  ** serves to ver
15be7 69 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66  ify that the off
15be8 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74  set to the start
15be9 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e   of the cell-con
15bea 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61  tent.    ** area
15beb 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  , according to t
15bec 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20  he page header, 
15bed 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  lies within the 
15bee 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
15bef 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c   if( nFree>usabl
15bf0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72  eSize ){.      r
15bf1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15bf2 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20  RUPT_BKPT; .    
15bf3 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72  }.    pPage->nFr
15bf4 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65  ee = (u16)(nFree
15bf5 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a   - iCellFirst);.
15bf6 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69      pPage->isIni
15bf7 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  t = 1;.  }.  ret
15bf8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15bf9 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61  ../*.** Set up a
15bfa 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61   raw page so tha
15bfb 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  t it looks like 
15bfc 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  a database page 
15bfd 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e  holding.** no en
15bfe 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tries..*/.static
15bff 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d   void zeroPage(M
15c00 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
15c01 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73  nt flags){.  uns
15c02 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
15c03 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
15c04 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
15c05 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20   = pPage->pBt;. 
15c06 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d   u8 hdr = pPage-
15c07 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31  >hdrOffset;.  u1
15c08 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65  6 first;..  asse
15c09 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15c0a 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65  Pagenumber(pPage
15c0b 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67  ->pDbPage)==pPag
15c0c 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73  e->pgno );.  ass
15c0d 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
15c0e 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d  rGetExtra(pPage-
15c0f 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f  >pDbPage) == (vo
15c10 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61  id*)pPage );.  a
15c11 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15c12 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
15c13 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61  ->pDbPage) == da
15c14 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ta );.  assert( 
15c15 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
15c16 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
15c17 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
15c18 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15c19 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
15c1a 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51  ex) );.#ifdef SQ
15c1b 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
15c1c 54 45 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  TE.  memset(&dat
15c1d 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e  a[hdr], 0, pBt->
15c1e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72  usableSize - hdr
15c1f 29 3b 0a 23 65 6e 64 69 66 0a 20 20 64 61 74 61  );.#endif.  data
15c20 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c  [hdr] = (char)fl
15c21 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68  ags;.  first = h
15c22 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61  dr + 8 + 4*((fla
15c23 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20  gs&PTF_LEAF)==0 
15c24 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ?1:0);.  memset(
15c25 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c  &data[hdr+1], 0,
15c26 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b   4);.  data[hdr+
15c27 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79  7] = 0;.  put2by
15c28 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
15c29 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
15c2a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
15c2b 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
15c2c 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64  ize - first;.  d
15c2d 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65  ecodeFlags(pPage
15c2e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67  , flags);.  pPag
15c2f 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68  e->hdrOffset = h
15c30 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c  dr;.  pPage->cel
15c31 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b  lOffset = first;
15c32 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  .  pPage->nOverf
15c33 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  low = 0;.  asser
15c34 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  t( pBt->pageSize
15c35 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61  >=512 && pBt->pa
15c36 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b  geSize<=32768 );
15c37 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61  .  pPage->maskPa
15c38 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ge = pBt->pageSi
15c39 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d  ze - 1;.  pPage-
15c3a 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50  >nCell = 0;.  pP
15c3b 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b  age->isInit = 1;
15c3c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  .}.../*.** Conve
15c3d 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61  rt a DbPage obta
15c3e 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  ined from the pa
15c3f 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61  ger into a MemPa
15c40 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68  ge used by.** th
15c41 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a  e btree layer..*
15c42 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65  /.static MemPage
15c43 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44   *btreePageFromD
15c44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44  bPage(DbPage *pD
15c45 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f  bPage, Pgno pgno
15c46 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  , BtShared *pBt)
15c47 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
15c48 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73  ge = (MemPage*)s
15c49 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
15c4a 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  tra(pDbPage);.  
15c4b 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73  pPage->aData = s
15c4c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
15c4d 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ta(pDbPage);.  p
15c4e 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20  Page->pDbPage = 
15c4f 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65  pDbPage;.  pPage
15c50 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70  ->pBt = pBt;.  p
15c51 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e  Page->pgno = pgn
15c52 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f  o;.  pPage->hdrO
15c53 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70  ffset = pPage->p
15c54 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
15c55 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  ;.  return pPage
15c56 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  ; .}../*.** Get 
15c57 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
15c58 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69  pager.  Initiali
15c59 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70  ze the MemPage.p
15c5a 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67  Bt and.** MemPag
15c5b 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73  e.aData elements
15c5c 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a   if needed..**.*
15c5d 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65  * If the noConte
15c5e 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  nt flag is set, 
15c5f 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
15c60 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
15c61 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ut.** the conten
15c62 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  t of the page at
15c63 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20   this time.  So 
15c64 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65  do not go to the
15c65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63   disk.** to fetc
15c66 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20  h the content.  
15c67 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65  Just fill in the
15c68 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65   content with ze
15c69 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20  ros for now..** 
15c6a 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  If in the future
15c6b 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   we call sqlite3
15c6c 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
15c6d 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a  this page, that.
15c6e 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65  ** means we have
15c6f 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63   started to be c
15c70 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63  oncerned about c
15c71 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64  ontent and the d
15c72 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75  isk.** read shou
15c73 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74  ld occur at that
15c74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   point..*/.stati
15c75 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61  c int btreeGetPa
15c76 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
15c77 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  pBt,       /* Th
15c78 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e  e btree */.  Pgn
15c79 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
15c7a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
15c7b 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68  he page to fetch
15c7c 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
15c7d 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65  ppPage,    /* Re
15c7e 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e  turn the page in
15c7f 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20   this parameter 
15c80 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65  */.  int noConte
15c81 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  nt        /* Do 
15c82 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f  not load page co
15c83 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f  ntent if true */
15c84 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
15c85 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b  DbPage *pDbPage;
15c86 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15c87 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15c88 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15c89 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15c8a 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50  rAcquire(pBt->pP
15c8b 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50  ager, pgno, (DbP
15c8c 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20  age**)&pDbPage, 
15c8d 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66  noContent);.  if
15c8e 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
15c8f 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74  ;.  *ppPage = bt
15c90 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15c91 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
15c92 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20   pBt);.  return 
15c93 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15c94 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70  .** Retrieve a p
15c95 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
15c96 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65  er cache. If the
15c97 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
15c98 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  is not.** alread
15c99 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  y in the pager c
15c9a 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  ache return NULL
15c9b 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  . Initialize the
15c9c 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64   MemPage.pBt and
15c9d 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74  .** MemPage.aDat
15c9e 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65  a elements if ne
15c9f 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eded..*/.static 
15ca0 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61  MemPage *btreePa
15ca1 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65  geLookup(BtShare
15ca2 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
15ca3 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  o){.  DbPage *pD
15ca4 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28  bPage;.  assert(
15ca5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15ca6 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15ca7 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73  );.  pDbPage = s
15ca8 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
15ca9 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70  p(pBt->pPager, p
15caa 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50  gno);.  if( pDbP
15cab 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  age ){.    retur
15cac 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44  n btreePageFromD
15cad 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70  bPage(pDbPage, p
15cae 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20  gno, pBt);.  }. 
15caf 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
15cb0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
15cb1 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
15cb2 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65  ase file in page
15cb3 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  s. If there is a
15cb4 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72  ny kind of.** er
15cb5 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e  ror, return ((un
15cb6 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a  signed int)-1)..
15cb7 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70  */.static Pgno p
15cb8 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74  agerPagecount(Bt
15cb9 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15cba 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a  int nPage = -1;.
15cbb 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
15cbc 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20  rt( pBt->pPage1 
15cbd 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
15cbe 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
15cbf 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
15cc0 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  age);.  assert( 
15cc1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
15cc2 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20   nPage==-1 );.  
15cc3 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61  return (Pgno)nPa
15cc4 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ge;.}../*.** Get
15cc5 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
15cc6 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69   pager and initi
15cc7 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20  alize it.  This 
15cc8 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20  routine is just 
15cc9 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
15cca 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   wrapper around 
15ccb 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74  separate calls t
15ccc 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29  o btreeGetPage()
15ccd 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e   and .** btreeIn
15cce 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  itPage()..**.** 
15ccf 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
15cd0 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  rs, then the val
15cd1 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  ue *ppPage is se
15cd2 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65  t to is undefine
15cd3 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d  d. It.** may rem
15cd4 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f  ain unchanged, o
15cd5 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20  r it may be set 
15cd6 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61  to an invalid va
15cd7 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
15cd8 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
15cd9 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
15cda 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
15cdb 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
15cdc 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
15cdd 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
15cde 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
15cdf 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
15ce0 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20  mPage **ppPage  
15ce1 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
15ce2 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
15ce3 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15ce4 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67  ;.  TESTONLY( Pg
15ce5 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67  no iLastPg = pag
15ce6 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
15ce7 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71  ; ).  assert( sq
15ce8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15ce9 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
15cea 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
15ceb 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20  Page(pBt, pgno, 
15cec 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66  ppPage, 0);.  if
15ced 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15cee 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
15cef 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67  eInitPage(*ppPag
15cf0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
15cf1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15cf2 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
15cf3 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  ppPage);.    }. 
15cf4 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
15cf5 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e  requested page n
15cf6 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72  umber was either
15cf7 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   0 or greater th
15cf8 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a  an the page.  **
15cf9 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
15cfa 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ast page in the 
15cfb 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66  database, this f
15cfc 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72  unction should r
15cfd 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54  eturn.  ** SQLIT
15cfe 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d  E_CORRUPT or som
15cff 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69  e other error (i
15d00 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29  .e. SQLITE_FULL)
15d01 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69  . Check that thi
15d02 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61  s.  ** is the ca
15d03 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  se.  */.  assert
15d04 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e  ( (pgno>0 && pgn
15d05 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72  o<=iLastPg) || r
15d06 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
15d07 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f    testcase( pgno
15d08 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==0 );.  testcas
15d09 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67  e( pgno==iLastPg
15d0a 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63   );..  return rc
15d0b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
15d0c 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54  se a MemPage.  T
15d0d 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61  his should be ca
15d0e 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  lled once for ea
15d0f 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c  ch prior.** call
15d10 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65   to btreeGetPage
15d11 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15d12 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d   releasePage(Mem
15d13 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
15d14 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20  if( pPage ){.   
15d15 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15d16 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
15d17 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
15d18 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e  Refcount(pPage->
15d19 70 44 62 50 61 67 65 29 3e 31 20 29 3b 0a 20 20  pDbPage)>1 );.  
15d1a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15d1b 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73  >aData );.    as
15d1c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
15d1d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15d1e 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15d1f 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
15d20 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70  age) == (void*)p
15d21 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65  Page );.    asse
15d22 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15d23 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
15d24 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e  DbPage)==pPage->
15d25 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73  aData );.    ass
15d26 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15d27 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
15d28 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15d29 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
15d2a 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ref(pPage->pDbPa
15d2b 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ge);.  }.}../*.*
15d2c 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62  * During a rollb
15d2d 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61  ack, when the pa
15d2e 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f  ger reloads info
15d2f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  rmation into the
15d30 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61   cache.** so tha
15d31 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72  t the cache is r
15d32 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f  estored to its o
15d33 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74  riginal state at
15d34 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a   the start of.**
15d35 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
15d36 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  , for each page 
15d37 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f  restored this ro
15d38 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
15d39 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
15d3a 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73  ine needs to res
15d3b 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74  et the extra dat
15d3c 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65  a section at the
15d3d 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70   end of the.** p
15d3e 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74  age to agree wit
15d3f 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64  h the restored d
15d40 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ata..*/.static v
15d41 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44  oid pageReinit(D
15d42 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20  bPage *pData){. 
15d43 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
15d44 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50  .  pPage = (MemP
15d45 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67  age *)sqlite3Pag
15d46 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61  erGetExtra(pData
15d47 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15d48 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
15d49 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29  count(pData)>0 )
15d4a 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
15d4b 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73  sInit ){.    ass
15d4c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15d4d 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
15d4e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15d4f 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
15d50 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
15d51 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
15d52 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29  count(pData)>1 )
15d53 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65  {.      /* pPage
15d54 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20   might not be a 
15d55 62 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20  btree page;  it 
15d56 6d 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72  might be an over
15d57 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20  flow page.      
15d58 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67  ** or ptrmap pag
15d59 65 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65  e or a free page
15d5a 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65  .  In those case
15d5b 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  s, the following
15d5c 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74  .      ** call t
15d5d 6f 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  o btreeInitPage(
15d5e 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65  ) will likely re
15d5f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15d60 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75  UPT..      ** Bu
15d61 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e  t no harm is don
15d62 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20  e by this.  And 
15d63 69 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72  it is very impor
15d64 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20  tant that.      
15d65 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65  ** btreeInitPage
15d66 28 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  () be called on 
15d67 65 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65  every btree page
15d68 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20   so we make.    
15d69 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f    ** the call fo
15d6a 72 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61  r every page tha
15d6b 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72  t comes in for r
15d6c 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20  e-initing. */.  
15d6d 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67      btreeInitPag
15d6e 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  e(pPage);.    }.
15d6f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76    }.}../*.** Inv
15d70 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e  oke the busy han
15d71 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65  dler for a btree
15d72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15d73 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48  btreeInvokeBusyH
15d74 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72  andler(void *pAr
15d75 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  g){.  BtShared *
15d76 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a  pBt = (BtShared*
15d77 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28  )pArg;.  assert(
15d78 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73   pBt->db );.  as
15d79 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15d7a 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62  tex_held(pBt->db
15d7b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65  ->mutex) );.  re
15d7c 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f  turn sqlite3Invo
15d7d 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70  keBusyHandler(&p
15d7e 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64  Bt->db->busyHand
15d7f 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ler);.}../*.** O
15d80 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  pen a database f
15d81 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c  ile..** .** zFil
15d82 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  ename is the nam
15d83 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
15d84 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c  e file.  If zFil
15d85 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a  ename is NULL.**
15d86 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
15d87 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61  with a random na
15d88 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  me is created.  
15d89 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61  This randomly na
15d8a 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  med.** database 
15d8b 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c  file will be del
15d8c 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65  eted when sqlite
15d8d 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73  3BtreeClose() is
15d8e 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a   called..** If z
15d8f 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  Filename is ":me
15d90 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69  mory:" then an i
15d91 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
15d92 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
15d93 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69  that is automati
15d94 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20  cally destroyed 
15d95 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
15d96 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
15d97 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65  database is alre
15d98 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68  ady opened in th
15d99 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
15d9a 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e  connection.** an
15d9b 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72  d we are in shar
15d9c 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74  ed cache mode, t
15d9d 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c  hen the open wil
15d9e 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a  l fail with an.*
15d9f 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  * SQLITE_CONSTRA
15da0 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63  INT error.  We c
15da1 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20  annot allow two 
15da2 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64  or more BtShared
15da3 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74  .** objects in t
15da4 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
15da5 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63   connection sinc
15da6 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20  e doing so will 
15da7 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c  lead.** to probl
15da8 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67  ems with locking
15da9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15daa 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15dab 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73  treeOpen(.  cons
15dac 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
15dad 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  e,  /* Name of t
15dae 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
15daf 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
15db0 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74  abase */.  sqlit
15db1 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
15db2 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64     /* Associated
15db3 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
15db4 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70   */.  Btree **pp
15db5 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a  Btree,        /*
15db6 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
15db7 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69  Btree object wri
15db8 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
15db9 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
15dba 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
15dbb 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c  s */.  int vfsFl
15dbc 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
15dbd 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
15dbe 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65  hrough to sqlite
15dbf 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
15dc0 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  .){.  sqlite3_vf
15dc1 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
15dc2 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
15dc3 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20  to use for this 
15dc4 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61  btree */.  BtSha
15dc5 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20  red *pBt = 0;   
15dc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61            /* Sha
15dc7 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65  red part of btre
15dc8 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
15dc9 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20   Btree *p;      
15dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15dcb 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74  /* Handle to ret
15dcc 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  urn */.  sqlite3
15dcd 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65  _mutex *mutexOpe
15dce 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65  n = 0;  /* Preve
15dcf 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69  nts a race condi
15dd0 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35  tion. Ticket #35
15dd1 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  37 */.  int rc =
15dd2 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
15dd3 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
15dd4 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20   code from this 
15dd5 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  function */.  u8
15dd6 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20   nReserve;      
15dd7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15dd8 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73  Byte of unused s
15dd9 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67  pace on each pag
15dda 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
15ddb 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31  char zDbHeader[1
15ddc 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73  00];  /* Databas
15ddd 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74  e header content
15dde 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68   */..  /* Set th
15ddf 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d  e variable isMem
15de0 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61  db to true for a
15de1 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
15de2 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66  base, or .  ** f
15de3 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d  alse for a file-
15de4 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20  based database. 
15de5 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f  This symbol is o
15de6 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a  nly required if.
15de7 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74    ** either of t
15de8 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f  he shared-data o
15de9 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61  r autovacuum fea
15dea 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c  tures are compil
15deb 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  ed .  ** into th
15dec 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a  e library..  */.
15ded 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
15dee 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15def 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e  CACHE) || !defin
15df0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
15df1 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66  UTOVACUUM).  #if
15df2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15df3 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e  MEMORYDB.    con
15df4 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d  st int isMemdb =
15df5 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20   0;.  #else.    
15df6 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64  const int isMemd
15df7 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  b = zFilename &&
15df8 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61   !strcmp(zFilena
15df9 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b  me, ":memory:");
15dfa 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  .  #endif.#endif
15dfb 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d  ..  assert( db!=
15dfc 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
15dfd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15dfe 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
15dff 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56  .  pVfs = db->pV
15e00 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  fs;.  p = sqlite
15e01 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
15e02 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66  of(Btree));.  if
15e03 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
15e04 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
15e05 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e  .  }.  p->inTran
15e06 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
15e07 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69    p->db = db;.#i
15e08 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15e09 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
15e0a 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20   p->lock.pBtree 
15e0b 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69  = p;.  p->lock.i
15e0c 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69  Table = 1;.#endi
15e0d 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  f..#if !defined(
15e0e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15e0f 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
15e10 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15e11 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20  T_DISKIO).  /*. 
15e12 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65   ** If this Btre
15e13 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65  e is a candidate
15e14 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68   for shared cach
15e15 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61  e, try to find a
15e16 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20  n.  ** existing 
15e17 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20  BtShared object 
15e18 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72  that we can shar
15e19 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66  e with.  */.  if
15e1a 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20  ( isMemdb==0 && 
15e1b 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
15e1c 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20  lename[0] ){.   
15e1d 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20   if( vfsFlags & 
15e1e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
15e1f 45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20  EDCACHE ){.     
15e20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61   int nFullPathna
15e21 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74  me = pVfs->mxPat
15e22 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63  hname+1;.      c
15e23 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61  har *zFullPathna
15e24 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  me = sqlite3Mall
15e25 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  oc(nFullPathname
15e26 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
15e27 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61  _mutex *mutexSha
15e28 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68  red;.      p->sh
15e29 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  arable = 1;.    
15e2a 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68    if( !zFullPath
15e2b 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  name ){.        
15e2c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
15e2d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
15e2e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15e2f 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
15e30 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
15e31 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  e(pVfs, zFilenam
15e32 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e, nFullPathname
15e33 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  , zFullPathname)
15e34 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65  ;.      mutexOpe
15e35 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  n = sqlite3Mutex
15e36 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
15e37 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b  EX_STATIC_OPEN);
15e38 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15e39 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
15e3a 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74  Open);.      mut
15e3b 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74  exShared = sqlit
15e3c 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
15e3d 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
15e3e 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
15e3f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
15e40 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29  ter(mutexShared)
15e41 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d  ;.      for(pBt=
15e42 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15e43 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15e44 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70  cheList); pBt; p
15e45 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a  Bt=pBt->pNext){.
15e46 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
15e47 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  pBt->nRef>0 );. 
15e48 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74         if( 0==st
15e49 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61  rcmp(zFullPathna
15e4a 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72  me, sqlite3Pager
15e4b 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50  Filename(pBt->pP
15e4c 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20  ager)).         
15e4d 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74          && sqlit
15e4e 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e  e3PagerVfs(pBt->
15e4f 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b  pPager)==pVfs ){
15e50 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
15e51 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  Db;.          fo
15e52 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b  r(iDb=db->nDb-1;
15e53 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b   iDb>=0; iDb--){
15e54 0a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72  .            Btr
15e55 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20  ee *pExisting = 
15e56 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
15e57 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
15e58 28 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70  ( pExisting && p
15e59 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70  Existing->pBt==p
15e5a 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Bt ){.          
15e5b 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15e5c 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
15e5d 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  red);.          
15e5e 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15e5f 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65  x_leave(mutexOpe
15e60 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  n);.            
15e61 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
15e62 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
15e63 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
15e64 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
15e65 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
15e66 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  rn SQLITE_CONSTR
15e67 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20  AINT;.          
15e68 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
15e69 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74            p->pBt
15e6a 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20   = pBt;.        
15e6b 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20    pBt->nRef++;. 
15e6c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
15e6d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15e6e 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
15e6f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
15e70 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20  xShared);.      
15e71 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75  sqlite3_free(zFu
15e72 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  llPathname);.   
15e73 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
15e74 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b  _DEBUG.    else{
15e75 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62  .      /* In deb
15e76 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b  ug mode, we mark
15e77 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20   all persistent 
15e78 64 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61  databases as sha
15e79 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65  rable.      ** e
15e7a 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72  ven when they ar
15e7b 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65  e not.  This exe
15e7c 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69  rcises the locki
15e7d 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20  ng code and.    
15e7e 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20    ** gives more 
15e7f 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20  opportunity for 
15e80 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f  asserts(sqlite3_
15e81 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20  mutex_held()).  
15e82 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
15e83 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e  s to find lockin
15e84 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20  g problems..    
15e85 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68    */.      p->sh
15e86 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  arable = 1;.    
15e87 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e  }.#endif.  }.#en
15e88 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30  dif.  if( pBt==0
15e89 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   ){.    /*.    *
15e8a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
15e8b 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72  asserts make sur
15e8c 65 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65  e that structure
15e8d 73 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74  s used by the bt
15e8e 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74  ree are.    ** t
15e8f 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20  he right size.  
15e90 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64  This is to guard
15e91 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68   against size ch
15e92 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c  anges that resul
15e93 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f  t.    ** when co
15e94 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66  mpiling on a dif
15e95 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74  ferent architect
15e96 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ure..    */.    
15e97 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69  assert( sizeof(i
15e98 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66  64)==8 || sizeof
15e99 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (i64)==4 );.    
15e9a 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
15e9b 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66  64)==8 || sizeof
15e9c 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (u64)==4 );.    
15e9d 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
15e9e 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73  32)==4 );.    as
15e9f 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36  sert( sizeof(u16
15ea0 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65  )==2 );.    asse
15ea1 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29  rt( sizeof(Pgno)
15ea2 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42  ==4 );.  .    pB
15ea3 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
15ea4 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70  cZero( sizeof(*p
15ea5 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  Bt) );.    if( p
15ea6 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  Bt==0 ){.      r
15ea7 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
15ea8 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72  ;.      goto btr
15ea9 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ee_open_out;.   
15eaa 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
15eab 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66  te3PagerOpen(pVf
15eac 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c  s, &pBt->pPager,
15ead 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20   zFilename,.    
15eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15eaf 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45        EXTRA_SIZE
15eb0 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67  , flags, vfsFlag
15eb1 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a  s, pageReinit);.
15eb2 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
15eb3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15eb4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15eb5 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70  ReadFileheader(p
15eb6 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f  Bt->pPager,sizeo
15eb7 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62  f(zDbHeader),zDb
15eb8 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20  Header);.    }. 
15eb9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
15eba 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
15ebb 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
15ebc 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  t;.    }.    pBt
15ebd 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73  ->db = db;.    s
15ebe 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75  qlite3PagerSetBu
15ebf 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70  syhandler(pBt->p
15ec0 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f  Pager, btreeInvo
15ec1 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70  keBusyHandler, p
15ec2 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20  Bt);.    p->pBt 
15ec3 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42  = pBt;.  .    pB
15ec4 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a  t->pCursor = 0;.
15ec5 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20      pBt->pPage1 
15ec6 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65  = 0;.    pBt->re
15ec7 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33  adOnly = sqlite3
15ec8 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28  PagerIsreadonly(
15ec9 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
15eca 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
15ecb 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48  = get2byte(&zDbH
15ecc 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20  eader[16]);.    
15ecd 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  if( pBt->pageSiz
15ece 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61  e<512 || pBt->pa
15ecf 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41  geSize>SQLITE_MA
15ed0 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20  X_PAGE_SIZE.    
15ed1 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70       || ((pBt->p
15ed2 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e  ageSize-1)&pBt->
15ed3 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a  pageSize)!=0 ){.
15ed4 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53        pBt->pageS
15ed5 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  ize = 0;.#ifndef
15ed6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
15ed7 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a  OVACUUM.      /*
15ed8 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61   If the magic na
15ed9 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69  me ":memory:" wi
15eda 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d  ll create an in-
15edb 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
15edc 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c   then.      ** l
15edd 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63  eave the autoVac
15ede 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64  uum mode at 0 (d
15edf 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75  o not auto-vacuu
15ee0 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  m), even if.    
15ee1 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41    ** SQLITE_DEFA
15ee2 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69  ULT_AUTOVACUUM i
15ee3 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f  s true. On the o
15ee4 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20  ther hand, if.  
15ee5 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d      ** SQLITE_OM
15ee6 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20  IT_MEMORYDB has 
15ee7 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68  been defined, th
15ee8 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73  en ":memory:" is
15ee9 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a   just a.      **
15eea 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61   regular file-na
15eeb 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  me. In this case
15eec 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
15eed 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20   applies as per 
15eee 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f  normal..      */
15eef 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65  .      if( zFile
15ef0 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62  name && !isMemdb
15ef1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d   ){.        pBt-
15ef2 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53  >autoVacuum = (S
15ef3 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
15ef4 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30  TOVACUUM ? 1 : 0
15ef5 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  );.        pBt->
15ef6 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51  incrVacuum = (SQ
15ef7 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
15ef8 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a  OVACUUM==2 ? 1 :
15ef9 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e   0);.      }.#en
15efa 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72  dif.      nReser
15efb 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ve = 0;.    }els
15efc 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76  e{.      nReserv
15efd 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30  e = zDbHeader[20
15efe 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61  ];.      pBt->pa
15eff 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b  geSizeFixed = 1;
15f00 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15f01 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
15f02 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56        pBt->autoV
15f03 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74  acuum = (get4byt
15f04 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20  e(&zDbHeader[36 
15f05 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20  + 4*4])?1:0);.  
15f06 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63      pBt->incrVac
15f07 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
15f08 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20  &zDbHeader[36 + 
15f09 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64  7*4])?1:0);.#end
15f0a 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  if.    }.    rc 
15f0b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
15f0c 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
15f0d 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
15f0e 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29  eSize, nReserve)
15f0f 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
15f10 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f  oto btree_open_o
15f11 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61  ut;.    pBt->usa
15f12 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70  bleSize = pBt->p
15f13 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72  ageSize - nReser
15f14 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ve;.    assert( 
15f15 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26  (pBt->pageSize &
15f16 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d   7)==0 );  /* 8-
15f17 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
15f18 66 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20  f pageSize */.  
15f19 20 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53   .#if !defined(S
15f1a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15f1b 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66  D_CACHE) && !def
15f1c 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15f1d 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20  _DISKIO).    /* 
15f1e 41 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68  Add the new BtSh
15f1f 61 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74  ared object to t
15f20 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73  he linked list s
15f21 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64  harable BtShared
15f22 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
15f23 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  ( p->sharable ){
15f24 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15f25 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65  utex *mutexShare
15f26 64 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52  d;.      pBt->nR
15f27 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75  ef = 1;.      mu
15f28 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69  texShared = sqli
15f29 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
15f2a 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
15f2b 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20  C_MASTER);.     
15f2c 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45   if( SQLITE_THRE
15f2d 41 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65  ADSAFE && sqlite
15f2e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
15f2f 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
15f30 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d      pBt->mutex =
15f31 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
15f32 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
15f33 46 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69  FAST);.        i
15f34 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30  f( pBt->mutex==0
15f35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
15f36 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
15f37 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  .          db->m
15f38 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
15f39 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
15f3a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15f3b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15f3c 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
15f3d 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
15f3e 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20  xShared);.      
15f3f 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f  pBt->pNext = GLO
15f40 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
15f41 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
15f42 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f  List);.      GLO
15f43 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
15f44 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
15f45 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20  List) = pBt;.   
15f46 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15f47 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72  _leave(mutexShar
15f48 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ed);.    }.#endi
15f49 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69  f.  }..#if !defi
15f4a 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
15f4b 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26  SHARED_CACHE) &&
15f4c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15f4d 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20  _OMIT_DISKIO).  
15f4e 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74  /* If the new Bt
15f4f 72 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61  ree uses a shara
15f50 62 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74  ble pBtShared, t
15f51 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77  hen link the new
15f52 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f  .  ** Btree into
15f53 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c   the list of all
15f54 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73   sharable Btrees
15f55 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f   for the same co
15f56 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54  nnection..  ** T
15f57 68 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20  he list is kept 
15f58 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
15f59 65 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73  er by pBt addres
15f5a 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
15f5b 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15f5c 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65   int i;.    Btre
15f5d 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72  e *pSib;.    for
15f5e 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
15f5f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
15f60 20 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62   (pSib = db->aDb
15f61 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70  [i].pBt)!=0 && p
15f62 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  Sib->sharable ){
15f63 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
15f64 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70  pSib->pPrev ){ p
15f65 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65  Sib = pSib->pPre
15f66 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  v; }.        if(
15f67 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42   p->pBt<pSib->pB
15f68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  t ){.          p
15f69 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a  ->pNext = pSib;.
15f6a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72            p->pPr
15f6b 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ev = 0;.        
15f6c 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20    pSib->pPrev = 
15f6d 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  p;.        }else
15f6e 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
15f6f 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26  e( pSib->pNext &
15f70 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70  & pSib->pNext->p
15f71 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20  Bt<p->pBt ){.   
15f72 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20           pSib = 
15f73 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pSib->pNext;.   
15f74 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15f75 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53     p->pNext = pS
15f76 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ib->pNext;.     
15f77 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20       p->pPrev = 
15f78 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pSib;.          
15f79 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a  if( p->pNext ){.
15f7a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70              p->p
15f7b 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b  Next->pPrev = p;
15f7c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
15f7d 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65         pSib->pNe
15f7e 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  xt = p;.        
15f7f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
15f80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
15f81 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42   }.#endif.  *ppB
15f82 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65  tree = p;..btree
15f83 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28  _open_out:.  if(
15f84 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15f85 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26  {.    if( pBt &&
15f86 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a   pBt->pPager ){.
15f87 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
15f88 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61  erClose(pBt->pPa
15f89 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ger);.    }.    
15f8a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74  sqlite3_free(pBt
15f8b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
15f8c 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42  ree(p);.    *ppB
15f8d 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  tree = 0;.  }.  
15f8e 69 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b  if( mutexOpen ){
15f8f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
15f90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15f91 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20  mutexOpen) );.  
15f92 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15f93 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29  leave(mutexOpen)
15f94 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15f95 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72  c;.}../*.** Decr
15f96 65 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72  ement the BtShar
15f97 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e  ed.nRef counter.
15f98 20 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65    When it reache
15f99 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76  s zero,.** remov
15f9a 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73  e the BtShared s
15f9b 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68  tructure from th
15f9c 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20  e sharing list. 
15f9d 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20   Return.** true 
15f9e 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  if the BtShared.
15f9f 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61  nRef counter rea
15fa0 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65  ches zero and re
15fa1 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66  turn.** false if
15fa2 20 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73   it is still pos
15fa3 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  itive..*/.static
15fa4 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53   int removeFromS
15fa5 68 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61  haringList(BtSha
15fa6 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64  red *pBt){.#ifnd
15fa7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
15fa8 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71  HARED_CACHE.  sq
15fa9 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61  lite3_mutex *pMa
15faa 73 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64  ster;.  BtShared
15fab 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72   *pList;.  int r
15fac 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61  emoved = 0;..  a
15fad 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15fae 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74  utex_notheld(pBt
15faf 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
15fb0 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d  aster = sqlite3M
15fb1 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
15fb2 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
15fb3 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33  STER);.  sqlite3
15fb4 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61  _mutex_enter(pMa
15fb5 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52  ster);.  pBt->nR
15fb6 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d  ef--;.  if( pBt-
15fb7 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20  >nRef<=0 ){.    
15fb8 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  if( GLOBAL(BtSha
15fb9 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
15fba 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42  edCacheList)==pB
15fbb 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41  t ){.      GLOBA
15fbc 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
15fbd 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
15fbe 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74  st) = pBt->pNext
15fbf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
15fc0 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41     pList = GLOBA
15fc1 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
15fc2 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
15fc3 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  st);.      while
15fc4 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20  ( ALWAYS(pList) 
15fc5 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21  && pList->pNext!
15fc6 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20  =pBt ){.        
15fc7 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65  pList=pList->pNe
15fc8 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
15fc9 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69    if( ALWAYS(pLi
15fca 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  st) ){.        p
15fcb 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42  List->pNext = pB
15fcc 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  t->pNext;.      
15fcd 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
15fce 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
15fcf 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E ){.      sqlit
15fd0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42  e3_mutex_free(pB
15fd1 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d  t->mutex);.    }
15fd2 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31  .    removed = 1
15fd3 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
15fd4 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73  mutex_leave(pMas
15fd5 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ter);.  return r
15fd6 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20  emoved;.#else.  
15fd7 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
15fd8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
15fd9 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  ure pBt->pTmpSpa
15fda 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  ce points to an 
15fdb 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a  allocation of .*
15fdc 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70  * MX_CELL_SIZE(p
15fdd 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  Bt) bytes..*/.st
15fde 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61  atic void alloca
15fdf 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68  teTempSpace(BtSh
15fe0 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66  ared *pBt){.  if
15fe1 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  ( !pBt->pTmpSpac
15fe2 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54  e ){.    pBt->pT
15fe3 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65  mpSpace = sqlite
15fe4 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74  3PageMalloc( pBt
15fe5 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
15fe6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  }.}../*.** Free 
15fe7 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  the pBt->pTmpSpa
15fe8 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ce allocation.*/
15fe9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
15fea 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61  eTempSpace(BtSha
15feb 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c  red *pBt){.  sql
15fec 69 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42  ite3PageFree( pB
15fed 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  t->pTmpSpace);. 
15fee 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20   pBt->pTmpSpace 
15fef 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  = 0;.}../*.** Cl
15ff0 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61  ose an open data
15ff1 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64  base and invalid
15ff2 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e  ate all cursors.
15ff3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15ff4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15ff5 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a  reeClose(Btree *
15ff6 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  p){.  BtShared *
15ff7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
15ff8 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  BtCursor *pCur;.
15ff9 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  .  /* Close all 
15ffa 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76  cursors opened v
15ffb 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20  ia this handle. 
15ffc 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
15ffd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15ffe 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
15fff 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16000 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72  Enter(p);.  pCur
16001 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
16002 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29  .  while( pCur )
16003 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a  {.    BtCursor *
16004 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20  pTmp = pCur;.   
16005 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e   pCur = pCur->pN
16006 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d  ext;.    if( pTm
16007 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a  p->pBtree==p ){.
16008 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
16009 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54  eeCloseCursor(pT
1600a 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  mp);.    }.  }..
1600b 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e    /* Rollback an
1600c 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  y active transac
1600d 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68  tion and free th
1600e 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75  e handle structu
1600f 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c  re..  ** The cal
16010 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
16011 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70  eRollback() drop
16012 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b  s any table-lock
16013 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74  s held by.  ** t
16014 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f  his handle..  */
16015 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  .  sqlite3BtreeR
16016 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71  ollback(p);.  sq
16017 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
16018 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
16019 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68  re are still oth
1601a 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  er outstanding r
1601b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
1601c 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20   shared-btree.  
1601d 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65  ** structure, re
1601e 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65  turn now. The re
1601f 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20  mainder of this 
16020 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73  procedure cleans
16021 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68   .  ** up the sh
16022 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f  ared-btree..  */
16023 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61  .  assert( p->wa
16024 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70  ntToLock==0 && p
16025 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20  ->locked==0 );. 
16026 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
16027 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53  e || removeFromS
16028 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20  haringList(pBt) 
16029 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42  ){.    /* The pB
1602a 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f  t is no longer o
1602b 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69  n the sharing li
1602c 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63  st, so we can ac
1602d 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77  cess.    ** it w
1602e 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
1602f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e   hold the mutex.
16030 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
16031 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c  lean out and del
16032 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64  ete the BtShared
16033 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a   object..    */.
16034 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74      assert( !pBt
16035 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20  ->pCursor );.   
16036 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
16037 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  se(pBt->pPager);
16038 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46  .    if( pBt->xF
16039 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74  reeSchema && pBt
1603a 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
1603b 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68     pBt->xFreeSch
1603c 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ema(pBt->pSchema
1603d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
1603e 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70  ite3_free(pBt->p
1603f 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65  Schema);.    fre
16040 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
16041 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
16042 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66  e(pBt);.  }..#if
16043 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16044 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
16045 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
16046 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73  oLock==0 );.  as
16047 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d  sert( p->locked=
16048 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  =0 );.  if( p->p
16049 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d  Prev ) p->pPrev-
1604a 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78  >pNext = p->pNex
1604b 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78  t;.  if( p->pNex
1604c 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50  t ) p->pNext->pP
1604d 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  rev = p->pPrev;.
1604e 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
1604f 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74  3_free(p);.  ret
16050 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16051 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
16052 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20  he limit on the 
16053 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
16054 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63  allowed in the c
16055 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
16056 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
16057 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73  f cache pages is
16058 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f   set to the abso
16059 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  lute.** value of
1605a 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50   mxPage.  If mxP
1605b 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c  age is negative,
1605c 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a   the pager will.
1605d 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63  ** operate async
1605e 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77  hronously - it w
1605f 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20  ill not stop to 
16060 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74  do fsync()s.** t
16061 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73  o insure data is
16062 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
16063 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66  disk surface bef
16064 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e  ore.** continuin
16065 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73  g.  Transactions
16066 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73   still work if s
16067 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66  ynchronous is of
16068 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  f,.** and the da
16069 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65  tabase cannot be
1606a 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68   corrupted if th
1606b 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72  is program.** cr
1606c 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74  ashes.  But if t
1606d 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
1606e 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74  tem crashes or t
1606f 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62  here is.** an ab
16070 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75  rupt power failu
16071 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e  re when synchron
16072 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20  ous is off, the 
16073 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c  database.** coul
16074 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20  d be left in an 
16075 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64  inconsistent and
16076 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73   unrecoverable s
16077 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f  tate..** Synchro
16078 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65  nous is on by de
16079 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73  fault so databas
1607a 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  e corruption is 
1607b 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20  not.** normally 
1607c 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49  a worry..*/.SQLI
1607d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1607e 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
1607f 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  cheSize(Btree *p
16080 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
16081 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
16082 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
16083 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
16084 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
16085 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
16086 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
16087 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
16088 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70  Cachesize(pBt->p
16089 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a  Pager, mxPage);.
1608a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1608b 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1608c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1608d 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
1608e 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63  way data is sync
1608f 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72  ed to disk in or
16090 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20  der to increase 
16091 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68  or decrease.** h
16092 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61  ow well the data
16093 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d  base resists dam
16094 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72  age due to OS cr
16095 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a  ashes and power.
16096 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65  ** failures.  Le
16097 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d  vel 1 is the sam
16098 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75  e as asynchronou
16099 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63  s (no syncs() oc
1609a 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65  cur and.** there
1609b 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61   is a high proba
1609c 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
1609d 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68  )  Level 2 is th
1609e 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72  e default.  Ther
1609f 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c  e.** is a very l
160a0 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20  ow but non-zero 
160a1 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64  probability of d
160a2 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20  amage.  Level 3 
160a3 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70  reduces the.** p
160a4 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
160a5 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72  mage to near zer
160a6 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69  o but with a wri
160a7 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72  te performance r
160a8 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  eduction..*/.#if
160a9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
160aa 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53  _PAGER_PRAGMAS.S
160ab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
160ac 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
160ad 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72  tSafetyLevel(Btr
160ae 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c  ee *p, int level
160af 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b  , int fullSync){
160b0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
160b1 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73   = p->pBt;.  ass
160b2 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
160b3 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
160b4 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
160b5 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
160b6 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  .  sqlite3PagerS
160b7 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42  etSafetyLevel(pB
160b8 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c  t->pPager, level
160b9 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73  , fullSync);.  s
160ba 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
160bb 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
160bc 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
160bd 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
160be 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
160bf 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74  n btree is set t
160c0 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31  o safety level 1
160c1 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77  .  In other.** w
160c2 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55  ords, return TRU
160c3 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f  E if no sync() o
160c4 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73  ccurs on the dis
160c5 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49  k files..*/.SQLI
160c6 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
160c7 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
160c8 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70  isabled(Btree *p
160c9 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
160ca 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
160cb 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
160cc 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
160cd 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
160ce 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33  ) );  .  sqlite3
160cf 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
160d0 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20   assert( pBt && 
160d1 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20  pBt->pPager );. 
160d2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
160d3 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50  erNosync(pBt->pP
160d4 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ager);.  sqlite3
160d5 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
160d6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
160d7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
160d8 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
160d9 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e  AGMAS) || !defin
160da 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
160db 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61  ACUUM)./*.** Cha
160dc 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  nge the default 
160dd 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74  pages size and t
160de 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
160df 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20  erved bytes per 
160e0 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20  page..** Or, if 
160e1 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61  the page size ha
160e2 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66  s already been f
160e3 69 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c  ixed, return SQL
160e4 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a  ITE_READONLY .**
160e5 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e   without changin
160e6 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a  g anything..**.*
160e7 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20  * The page size 
160e8 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
160e9 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32  of 2 between 512
160ea 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20   and 65536.  If 
160eb 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65  the page.** size
160ec 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e   supplied does n
160ed 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e  ot meet this con
160ee 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65  straint then the
160ef 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
160f0 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  t.** changed..**
160f1 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61  .** Page sizes a
160f2 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74  re constrained t
160f3 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  o be a power of 
160f4 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20  two so that the 
160f5 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65  region.** of the
160f6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
160f7 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20  sed for locking 
160f8 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45  (beginning at PE
160f9 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74  NDING_BYTE,.** t
160fa 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
160fb 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64  st the 1GB bound
160fc 61 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29  ary, 0x40000000)
160fd 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a   needs to occur.
160fe 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  ** at the beginn
160ff 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a  ing of a page..*
16100 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  *.** If paramete
16101 72 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65  r nReserve is le
16102 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
16103 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
16104 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74   reserved.** byt
16105 65 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c  es per page is l
16106 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  eft unchanged..*
16107 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78  *.** If the iFix
16108 21 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67  !=0 then the pag
16109 65 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20  eSizeFixed flag 
1610a 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74  is set so that t
1610b 68 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20  he page size.** 
1610c 61 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d  and autovacuum m
1610d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65  ode can no longe
1610e 72 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f  r be changed..*/
1610f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16110 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16111 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  SetPageSize(Btre
16112 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69  e *p, int pageSi
16113 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ze, int nReserve
16114 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69  , int iFix){.  i
16115 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16116 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  K;.  BtShared *p
16117 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61  Bt = p->pBt;.  a
16118 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e  ssert( nReserve>
16119 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c  =-1 && nReserve<
1611a 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65  =255 );.  sqlite
1611b 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1611c 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53    if( pBt->pageS
1611d 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20  izeFixed ){.    
1611e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1611f 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  e(p);.    return
16120 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
16121 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73  ;.  }.  if( nRes
16122 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52  erve<0 ){.    nR
16123 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61  eserve = pBt->pa
16124 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73  geSize - pBt->us
16125 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20  ableSize;.  }.  
16126 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65  assert( nReserve
16127 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c  >=0 && nReserve<
16128 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61  =255 );.  if( pa
16129 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
1612a 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f  ageSize<=SQLITE_
1612b 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26  MAX_PAGE_SIZE &&
1612c 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53  .        ((pageS
1612d 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29  ize-1)&pageSize)
1612e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1612f 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37  t( (pageSize & 7
16130 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )==0 );.    asse
16131 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31  rt( !pBt->pPage1
16132 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f   && !pBt->pCurso
16133 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61  r );.    pBt->pa
16134 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61  geSize = (u16)pa
16135 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65  geSize;.    free
16136 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
16137 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
16138 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
16139 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
1613a 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20  &pBt->pageSize, 
1613b 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74  nReserve);.  pBt
1613c 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
1613d 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28  Bt->pageSize - (
1613e 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20  u16)nReserve;.  
1613f 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e  if( iFix ) pBt->
16140 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20  pageSizeFixed = 
16141 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  1;.  sqlite3Btre
16142 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16143 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16144 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
16145 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61  ently defined pa
16146 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54  ge size.*/.SQLIT
16147 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16148 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
16149 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b  eSize(Btree *p){
1614a 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74  .  return p->pBt
1614b 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f  ->pageSize;.}../
1614c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1614d 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1614e 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20  of space at the 
1614f 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67  end of every pag
16150 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e  e that.** are in
16151 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75  tentually left u
16152 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20  nused.  This is 
16153 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73  the "reserved" s
16154 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20  pace that is.** 
16155 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62  sometimes used b
16156 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  y extensions..*/
16157 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16158 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16159 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65  GetReserve(Btree
1615a 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *p){.  int n;. 
1615b 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1615c 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e  er(p);.  n = p->
1615d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
1615e 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  p->pBt->usableSi
1615f 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ze;.  sqlite3Btr
16160 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
16161 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
16162 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   Set the maximum
16163 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20   page count for 
16164 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78  a database if mx
16165 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65  Page is positive
16166 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20  ..** No changes 
16167 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61  are made if mxPa
16168 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74  ge is 0 or negat
16169 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65  ive..** Regardle
1616a 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  ss of the value 
1616b 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72  of mxPage, retur
1616c 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61  n the maximum pa
1616d 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c  ge count..*/.SQL
1616e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1616f 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50  sqlite3BtreeMaxP
16170 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a  ageCount(Btree *
16171 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  p, int mxPage){.
16172 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
16173 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
16174 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61  .  n = sqlite3Pa
16175 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
16176 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  p->pBt->pPager, 
16177 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74  mxPage);.  sqlit
16178 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
16179 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23  .  return n;.}.#
1617a 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
1617b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  d(SQLITE_OMIT_PA
1617c 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20  GER_PRAGMAS) || 
1617d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1617e 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a  OMIT_VACUUM) */.
1617f 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
16180 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20  e 'auto-vacuum' 
16181 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20  property of the 
16182 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65  database. If the
16183 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a   'autoVacuum'.**
16184 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
16185 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74  n-zero, then aut
16186 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73  o-vacuum mode is
16187 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72   enabled. If zer
16188 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61  o, it.** is disa
16189 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c  bled. The defaul
1618a 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
1618b 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70  auto-vacuum prop
1618c 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65  erty is .** dete
1618d 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51  rmined by the SQ
1618e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
1618f 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a  OVACUUM macro..*
16190 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16191 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16192 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42  eSetAutoVacuum(B
16193 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74  tree *p, int aut
16194 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66  oVacuum){.#ifdef
16195 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
16196 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e  OVACUUM.  return
16197 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
16198 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72  ;.#else.  BtShar
16199 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1619a 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1619b 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20  ITE_OK;.  u8 av 
1619c 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d  = (u8)autoVacuum
1619d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
1619e 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
1619f 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
161a0 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29  xed && (av ?1:0)
161a1 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  !=pBt->autoVacuu
161a2 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  m ){.    rc = SQ
161a3 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
161a4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d   }else{.    pBt-
161a5 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76  >autoVacuum = av
161a6 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e   ?1:0;.    pBt->
161a7 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d  incrVacuum = av=
161a8 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73  =2 ?1:0;.  }.  s
161a9 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
161aa 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
161ab 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
161ac 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
161ad 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d  ue of the 'auto-
161ae 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79  vacuum' property
161af 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d  . If auto-vacuum
161b0 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20   is .** enabled 
161b1 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  1 is returned. O
161b2 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53  therwise 0..*/.S
161b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
161b4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
161b5 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65  tAutoVacuum(Btre
161b6 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51  e *p){.#ifdef SQ
161b7 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
161b8 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54  CUUM.  return BT
161b9 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
161ba 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  ONE;.#else.  int
161bb 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
161bc 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
161bd 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70  c = (.    (!p->p
161be 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f  Bt->autoVacuum)?
161bf 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
161c0 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e  _NONE:.    (!p->
161c1 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29  pBt->incrVacuum)
161c2 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
161c3 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45  M_FULL:.    BTRE
161c4 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
161c5 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  R.  );.  sqlite3
161c6 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
161c7 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
161c8 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  if.}.../*.** Get
161c9 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
161ca 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61  pPage1 of the da
161cb 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
161cc 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20  is will.** also 
161cd 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f  acquire a readlo
161ce 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e  ck on that file.
161cf 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
161d0 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
161d1 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65  success.  If the
161d2 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a   file is not a.*
161d3 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61  * well-formed da
161d4 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
161d5 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
161d6 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
161d7 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
161d8 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
161d9 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
161da 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ed.  SQLITE_NOME
161db 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  M.** is returned
161dc 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f   if we run out o
161dd 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74  f memory. .*/.st
161de 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72  atic int lockBtr
161df 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ee(BtShared *pBt
161e0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
161e1 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a  emPage *pPage1;.
161e2 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20    int nPage;..  
161e3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
161e4 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
161e5 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
161e6 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d  rt( pBt->pPage1=
161e7 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =0 );.  rc = sql
161e8 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c  ite3PagerSharedL
161e9 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ock(pBt->pPager)
161ea 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
161eb 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
161ec 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47  c;.  rc = btreeG
161ed 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
161ee 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66  pPage1, 0);.  if
161ef 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
161f0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
161f1 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b  /* Do some check
161f2 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75  ing to help insu
161f3 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f  re the file we o
161f4 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a  pened really is.
161f5 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74    ** a valid dat
161f6 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a  abase file. .  *
161f7 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
161f8 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
161f9 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61  Bt->pPager, &nPa
161fa 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ge);.  if( rc!=S
161fb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
161fc 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
161fd 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20  failed;.  }else 
161fe 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20  if( nPage>0 ){. 
161ff 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b     int pageSize;
16200 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53  .    int usableS
16201 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67  ize;.    u8 *pag
16202 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61  e1 = pPage1->aDa
16203 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ta;.    rc = SQL
16204 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20  ITE_NOTADB;.    
16205 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31  if( memcmp(page1
16206 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20  , zMagicHeader, 
16207 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  16)!=0 ){.      
16208 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
16209 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
1620a 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e    if( page1[18]>
1620b 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  1 ){.      pBt->
1620c 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20  readOnly = 1;.  
1620d 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65    }.    if( page
1620e 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20  1[19]>1 ){.     
1620f 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74   goto page1_init
16210 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a  _failed;.    }..
16211 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d      /* The maxim
16212 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63  um embedded frac
16213 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61  tion must be exa
16214 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74  ctly 25%.  And t
16215 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a  he minimum.    *
16216 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74  * embedded fract
16217 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35  ion must be 12.5
16218 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d  % for both leaf-
16219 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61  data and non-lea
1621a 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54  f-data..    ** T
1621b 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69  he original desi
1621c 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65  gn allowed these
1621d 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79   amounts to vary
1621e 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20  , but as of.    
1621f 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30  ** version 3.6.0
16220 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65  , we require the
16221 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20  m to be fixed.. 
16222 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65     */.    if( me
16223 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c  mcmp(&page1[21],
16224 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c   "\100\040\040",
16225 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  3)!=0 ){.      g
16226 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
16227 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
16228 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32   pageSize = get2
16229 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29  byte(&page1[16])
1622a 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65  ;.    if( ((page
1622b 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65  Size-1)&pageSize
1622c 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65  )!=0 || pageSize
1622d 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20  <512 ||.        
1622e 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  (SQLITE_MAX_PAGE
1622f 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70  _SIZE<32768 && p
16230 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d  ageSize>SQLITE_M
16231 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20  AX_PAGE_SIZE).  
16232 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20    ){.      goto 
16233 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
16234 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  d;.    }.    ass
16235 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26  ert( (pageSize &
16236 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73   7)==0 );.    us
16237 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53  ableSize = pageS
16238 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b  ize - page1[20];
16239 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a  .    if( pageSiz
1623a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e!=pBt->pageSize
1623b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74   ){.      /* Aft
1623c 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66  er reading the f
1623d 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
1623e 20 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69   database assumi
1623f 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20  ng a page size. 
16240 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61       ** of BtSha
16241 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65  red.pageSize, we
16242 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64   have discovered
16243 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73   that the page-s
16244 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  ize is.      ** 
16245 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a  actually pageSiz
16246 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  e. Unlock the da
16247 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42  tabase, leave pB
16248 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20  t->pPage1 at.   
16249 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72     ** zero and r
1624a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
1624b 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c   The caller will
1624c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
1624d 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61  ion.      ** aga
1624e 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72  in with the corr
1624f 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20  ect page-size.. 
16250 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65       */.      re
16251 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31  leasePage(pPage1
16252 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73  );.      pBt->us
16253 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29  ableSize = (u16)
16254 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
16255 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
16256 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b  = (u16)pageSize;
16257 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53  .      freeTempS
16258 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20  pace(pBt);.     
16259 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1625a 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42  erSetPagesize(pB
1625b 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d  t->pPager, &pBt-
1625c 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20  >pageSize,.     
1625d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1625e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
1625f 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a  geSize-usableSiz
16260 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
16261 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   rc;.    }.    i
16262 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38  f( usableSize<48
16263 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
16264 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
16265 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  d;.    }.    pBt
16266 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31  ->pageSize = (u1
16267 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  6)pageSize;.    
16268 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
16269 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a  = (u16)usableSiz
1626a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
1626b 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1626c 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56  M.    pBt->autoV
1626d 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74  acuum = (get4byt
1626e 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a  e(&page1[36 + 4*
1626f 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42  4])?1:0);.    pB
16270 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
16271 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31  (get4byte(&page1
16272 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29  [36 + 7*4])?1:0)
16273 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  ;.#endif.  }..  
16274 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74  /* maxLocal is t
16275 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e  he maximum amoun
16276 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20  t of payload to 
16277 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f  store locally fo
16278 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20  r.  ** a cell.  
16279 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20  Make sure it is 
1627a 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20  small enough so 
1627b 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69  that at least mi
1627c 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c  nFanout.  ** cel
1627d 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20  ls can will fit 
1627e 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65  on one page.  We
1627f 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74   assume a 10-byt
16280 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20  e page header.. 
16281 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20   ** Besides the 
16282 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c  payload, the cel
16283 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20  l must store:.  
16284 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f  **     2-byte po
16285 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c  inter to the cel
16286 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74  l.  **     4-byt
16287 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a  e child pointer.
16288 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20    **     9-byte 
16289 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20  nKey value.  ** 
1628a 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61      4-byte nData
1628b 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20   value.  **     
1628c 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20  4-byte overflow 
1628d 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a  page pointer.  *
1628e 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73  * So a cell cons
1628f 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65  ists of a 2-byte
16290 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65   poiner, a heade
16291 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75  r which is as mu
16292 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79  ch as.  ** 17 by
16293 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e  tes long, 0 to N
16294 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61   bytes of payloa
16295 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e  d, and an option
16296 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c  al 4 byte overfl
16297 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69  ow.  ** page poi
16298 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74  nter..  */.  pBt
16299 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42  ->maxLocal = (pB
1629a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
1629b 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*64/255 - 23;. 
1629c 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d   pBt->minLocal =
1629d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a   (pBt->usableSiz
1629e 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32  e-12)*32/255 - 2
1629f 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61  3;.  pBt->maxLea
162a0 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  f = pBt->usableS
162a1 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d  ize - 35;.  pBt-
162a2 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d  >minLeaf = (pBt-
162a3 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a  >usableSize-12)*
162a4 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61  32/255 - 23;.  a
162a5 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c  ssert( pBt->maxL
162a6 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43  eaf + 23 <= MX_C
162a7 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b  ELL_SIZE(pBt) );
162a8 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d  .  pBt->pPage1 =
162a9 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72   pPage1;.  retur
162aa 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61  n SQLITE_OK;..pa
162ab 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a  ge1_init_failed:
162ac 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
162ad 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70  Page1);.  pBt->p
162ae 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74  Page1 = 0;.  ret
162af 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
162b0 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
162b1 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72   outstanding cur
162b2 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20  sors and we are 
162b3 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  not in the middl
162b4 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61  e.** of a transa
162b5 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20  ction but there 
162b6 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f  is a read lock o
162b7 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
162b8 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
162b9 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20  tine unrefs the 
162ba 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
162bb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
162bc 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68  which .** has th
162bd 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65  e effect of rele
162be 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c  asing the read l
162bf 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ock..**.** If th
162c0 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63  ere is a transac
162c1 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
162c2 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
162c3 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
162c4 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
162c5 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74  BtreeIfUnused(Bt
162c6 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
162c7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
162c8 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
162c9 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
162ca 72 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72  rt( pBt->pCursor
162cb 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72  ==0 || pBt->inTr
162cc 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f  ansaction>TRANS_
162cd 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42  NONE );.  if( pB
162ce 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
162cf 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20  ==TRANS_NONE && 
162d0 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29  pBt->pPage1!=0 )
162d1 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  {.    assert( pB
162d2 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
162d3 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
162d4 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
162d5 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
162d6 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  )==1 );.    asse
162d7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  rt( pBt->pPage1-
162d8 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65  >aData );.    re
162d9 6c 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70  leasePage(pBt->p
162da 50 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d  Page1);.    pBt-
162db 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d  >pPage1 = 0;.  }
162dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74  .}../*.** If pBt
162dd 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d   points to an em
162de 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f  pty file then co
162df 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79  nvert that empty
162e0 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20   file.** into a 
162e1 6e 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61  new empty databa
162e2 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69  se by initializi
162e3 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ng the first pag
162e4 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61  e of.** the data
162e5 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
162e6 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28  int newDatabase(
162e7 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
162e8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a    MemPage *pP1;.
162e9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
162ea 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b  *data;.  int rc;
162eb 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
162ec 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
162ed 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
162ee 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
162ef 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
162f0 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  gecount(pBt->pPa
162f1 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20  ger, &nPage);.  
162f2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
162f3 4b 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a  K || nPage>0 ){.
162f4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
162f5 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e   }.  pP1 = pBt->
162f6 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74  pPage1;.  assert
162f7 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61  ( pP1!=0 );.  da
162f8 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b  ta = pP1->aData;
162f9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
162fa 61 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70  agerWrite(pP1->p
162fb 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
162fc 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
162fd 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d   memcpy(data, zM
162fe 61 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65  agicHeader, size
162ff 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29  of(zMagicHeader)
16300 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
16301 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72  eof(zMagicHeader
16302 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62  )==16 );.  put2b
16303 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70  yte(&data[16], p
16304 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
16305 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20   data[18] = 1;. 
16306 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20   data[19] = 1;. 
16307 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73   assert( pBt->us
16308 61 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70  ableSize<=pBt->p
16309 61 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e  ageSize && pBt->
1630a 75 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d  usableSize+255>=
1630b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
1630c 20 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38    data[20] = (u8
1630d 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  )(pBt->pageSize 
1630e 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  - pBt->usableSiz
1630f 65 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d  e);.  data[21] =
16310 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20   64;.  data[22] 
16311 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d  = 32;.  data[23]
16312 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28   = 32;.  memset(
16313 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30  &data[24], 0, 10
16314 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67  0-24);.  zeroPag
16315 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45  e(pP1, PTF_INTKE
16316 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c  Y|PTF_LEAF|PTF_L
16317 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74  EAFDATA );.  pBt
16318 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
16319 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 1;.#ifndef SQL
1631a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1631b 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42  UUM.  assert( pB
1631c 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31  t->autoVacuum==1
1631d 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63   || pBt->autoVac
1631e 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  uum==0 );.  asse
1631f 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63  rt( pBt->incrVac
16320 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69  uum==1 || pBt->i
16321 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a  ncrVacuum==0 );.
16322 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61    put4byte(&data
16323 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d  [36 + 4*4], pBt-
16324 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20  >autoVacuum);.  
16325 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33  put4byte(&data[3
16326 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69  6 + 7*4], pBt->i
16327 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64  ncrVacuum);.#end
16328 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  if.  return SQLI
16329 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1632a 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74  Attempt to start
1632b 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69   a new transacti
1632c 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e  on. A write-tran
1632d 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74  saction.** is st
1632e 61 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63  arted if the sec
1632f 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
16330 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69  nonzero, otherwi
16331 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72  se a read-.** tr
16332 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74  ansaction.  If t
16333 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
16334 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20  nt is 2 or more 
16335 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a  and exclusive.**
16336 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
16337 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67  started, meaning
16338 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70   that no other p
16339 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65  rocess is allowe
1633a 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74  d.** to access t
1633b 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20  he database.  A 
1633c 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e  preexisting tran
1633d 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20  saction may not 
1633e 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74  be.** upgraded t
1633f 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63  o exclusive by c
16340 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
16341 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d  ine a second tim
16342 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75  e - the.** exclu
16343 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79  sivity flag only
16344 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77   works for a new
16345 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
16346 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e  .** A write-tran
16347 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  saction must be 
16348 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61  started before a
16349 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a  ttempting any .*
1634a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  * changes to the
1634b 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65   database.  None
1634c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1634d 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77  g routines .** w
1634e 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20  ill work unless 
1634f 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
16350 20 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a   started first:.
16351 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  **.**      sqlit
16352 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62  e3BtreeCreateTab
16353 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c  le().**      sql
16354 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49  ite3BtreeCreateI
16355 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73  ndex().**      s
16356 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
16357 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20  Table().**      
16358 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
16359 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20  Table().**      
1635a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65  sqlite3BtreeInse
1635b 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c  rt().**      sql
1635c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28  ite3BtreeDelete(
1635d 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ).**      sqlite
1635e 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
1635f 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  ().**.** If an i
16360 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74  nitial attempt t
16361 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f  o acquire the lo
16362 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65  ck fails because
16363 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74   of lock content
16364 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  ion.** and the d
16365 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76  atabase was prev
16366 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c  iously unlocked,
16367 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65   then invoke the
16368 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a   busy handler.**
16369 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65   if there is one
1636a 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20  .  But if there 
1636b 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61  was previously a
1636c 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e   read-lock, do n
1636d 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  ot.** invoke the
1636e 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20   busy handler - 
1636f 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49  just return SQLI
16370 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45  TE_BUSY.  SQLITE
16371 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74  _BUSY is .** ret
16372 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65  urned when there
16373 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65   is already a re
16374 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72  ad-lock in order
16375 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64   to avoid a dead
16376 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70  lock..**.** Supp
16377 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77  ose there are tw
16378 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e  o processes A an
16379 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65  d B.  A has a re
1637a 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61  ad lock and B ha
1637b 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20  s.** a reserved 
1637c 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74  lock.  B tries t
1637d 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63  o promote to exc
1637e 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c  lusive but is bl
1637f 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a  ocked because.**
16380 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63   of A's read loc
16381 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70  k.  A tries to p
16382 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76  romote to reserv
16383 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65  ed but is blocke
16384 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f  d by B..** One o
16385 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74  r the other of t
16386 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73  he two processes
16387 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f   must give way o
16388 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a  r there can be.*
16389 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20  * no progress.  
1638a 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c  By returning SQL
1638b 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74  ITE_BUSY and not
1638c 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75   invoking the bu
1638d 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77  sy callback.** w
1638e 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61  hen A already ha
1638f 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77  s a read lock, w
16390 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f  e encourage A to
16391 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74   give up and let
16392 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a   B.** proceed..*
16393 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16394 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16395 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65  eBeginTrans(Btre
16396 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67  e *p, int wrflag
16397 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42  ){.  sqlite3 *pB
16398 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68  lock = 0;.  BtSh
16399 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1639a 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  Bt;.  int rc = S
1639b 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c  QLITE_OK;..  sql
1639c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1639d 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  );.  btreeIntegr
1639e 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66  ity(p);..  /* If
1639f 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c   the btree is al
163a0 72 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65  ready in a write
163a1 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72  -transaction, or
163a2 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65   it.  ** is alre
163a3 61 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72  ady in a read-tr
163a4 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20  ansaction and a 
163a5 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
163a6 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74  .  ** is request
163a7 65 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f  ed, this is a no
163a8 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  -op..  */.  if( 
163a9 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
163aa 53 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69  S_WRITE || (p->i
163ab 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45  nTrans==TRANS_RE
163ac 41 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29  AD && !wrflag) )
163ad 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73  {.    goto trans
163ae 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  _begun;.  }..  /
163af 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74  * Write transact
163b0 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73  ions are not pos
163b1 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d  sible on a read-
163b2 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f  only database */
163b3 0a 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64  .  if( pBt->read
163b4 4f 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29  Only && wrflag )
163b5 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
163b6 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  E_READONLY;.    
163b7 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
163b8 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
163b9 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
163ba 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20  D_CACHE.  /* If 
163bb 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  another database
163bc 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65   handle has alre
163bd 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69  ady opened a wri
163be 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a  te transaction .
163bf 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61    ** on this sha
163c0 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74  red-btree struct
163c1 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64  ure and a second
163c2 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
163c3 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65  on is.  ** reque
163c4 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c  sted, return SQL
163c5 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f  ITE_LOCKED..  */
163c6 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26  .  if( (wrflag &
163c7 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  & pBt->inTransac
163c8 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
163c9 45 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e  E) || pBt->isPen
163ca 64 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f  ding ){.    pBlo
163cb 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65  ck = pBt->pWrite
163cc 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69  r->db;.  }else i
163cd 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20  f( wrflag>1 ){. 
163ce 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72     BtLock *pIter
163cf 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d  ;.    for(pIter=
163d0 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65  pBt->pLock; pIte
163d1 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e  r; pIter=pIter->
163d2 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  pNext){.      if
163d3 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21  ( pIter->pBtree!
163d4 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42  =p ){.        pB
163d5 6c 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42  lock = pIter->pB
163d6 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20  tree->db;.      
163d7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
163d8 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
163d9 20 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73   pBlock ){.    s
163da 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
163db 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70  Blocked(p->db, p
163dc 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d  Block);.    rc =
163dd 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
163de 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20  HAREDCACHE;.    
163df 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
163e0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
163e1 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79  /* Any read-only
163e2 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74   or read-write t
163e3 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69  ransaction impli
163e4 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  es a read-lock o
163e5 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20  n .  ** page 1. 
163e6 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  So if some other
163e7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c   shared-cache cl
163e8 69 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73  ient already has
163e9 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20   a write-lock . 
163ea 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74   ** on page 1, t
163eb 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  he transaction c
163ec 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e  annot be opened.
163ed 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79   */.  rc = query
163ee 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
163ef 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52  Lock(p, MASTER_R
163f0 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b  OOT, READ_LOCK);
163f1 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
163f2 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e  !=rc ) goto tran
163f3 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b  s_begun;..  do {
163f4 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63  .    /* Call loc
163f5 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65  kBtree() until e
163f6 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65  ither pBt->pPage
163f7 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f  1 is populated o
163f8 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72  r.    ** lockBtr
163f9 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d  ee() returns som
163fa 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
163fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63  n SQLITE_OK. loc
163fc 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20  kBtree().    ** 
163fd 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54  may return SQLIT
163fe 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70  E_OK but leave p
163ff 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74  Bt->pPage1 set t
16400 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20  o 0 if after.   
16401 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65   ** reading page
16402 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20   1 it discovers 
16403 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69  that the page-si
16404 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
16405 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20  se .    ** file 
16406 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65  is not pBt->page
16407 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61  Size. In this ca
16408 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77  se lockBtree() w
16409 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a  ill update.    *
1640a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  * pBt->pageSize 
1640b 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  to the page-size
1640c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20   of the file on 
1640d 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  disk..    */.   
1640e 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61   while( pBt->pPa
1640f 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45  ge1==0 && SQLITE
16410 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42  _OK==(rc = lockB
16411 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20  tree(pBt)) );.. 
16412 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
16413 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29  E_OK && wrflag )
16414 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  {.      if( pBt-
16415 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
16416 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
16417 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20  _READONLY;.     
16418 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
16419 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1641a 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67  rBegin(pBt->pPag
1641b 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69  er,wrflag>1,sqli
1641c 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28  te3TempInMemory(
1641d 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20  p->db));.       
1641e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1641f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
16420 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
16421 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  (pBt);.        }
16422 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
16423 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51   .    if( rc!=SQ
16424 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16425 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
16426 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d  used(pBt);.    }
16427 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53  .  }while( rc==S
16428 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42  QLITE_BUSY && pB
16429 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1642a 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a  ==TRANS_NONE &&.
1642b 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49            btreeI
1642c 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72  nvokeBusyHandler
1642d 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20  (pBt) );..  if( 
1642e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1642f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  .    if( p->inTr
16430 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20  ans==TRANS_NONE 
16431 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54  ){.      pBt->nT
16432 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69  ransaction++;.#i
16433 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16434 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
16435 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72       if( p->shar
16436 61 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28  able ){..assert(
16437 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d   p->lock.pBtree=
16438 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54  =p && p->lock.iT
16439 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20  able==1 );.     
1643a 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b     p->lock.eLock
1643b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20   = READ_LOCK;.  
1643c 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e        p->lock.pN
1643d 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b  ext = pBt->pLock
1643e 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70  ;.        pBt->p
1643f 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b  Lock = &p->lock;
16440 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
16441 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54      }.    p->inT
16442 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54  rans = (wrflag?T
16443 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53  RANS_WRITE:TRANS
16444 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20  _READ);.    if( 
16445 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e  p->inTrans>pBt->
16446 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  inTransaction ){
16447 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  .      pBt->inTr
16448 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69  ansaction = p->i
16449 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69  nTrans;.    }.#i
1644a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1644b 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
1644c 20 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b     if( wrflag ){
1644d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
1644e 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a  pBt->pWriter );.
1644f 20 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74        pBt->pWrit
16450 65 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42  er = p;.      pB
16451 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d  t->isExclusive =
16452 20 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b   (u8)(wrflag>1);
16453 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
16454 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a  }...trans_begun:
16455 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16456 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29  E_OK && wrflag )
16457 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61  {.    /* This ca
16458 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68  ll makes sure th
16459 61 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73  at the pager has
1645a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d   the correct num
1645b 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70  ber of.    ** op
1645c 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49  en savepoints. I
1645d 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  f the second par
1645e 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65  ameter is greate
1645f 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20  r than 0 and.   
16460 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   ** the sub-jour
16461 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  nal is not alrea
16462 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74  dy open, then it
16463 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20   will be opened 
16464 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
16465 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16466 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28  erOpenSavepoint(
16467 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e  pBt->pPager, p->
16468 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b  db->nSavepoint);
16469 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74  .  }..  btreeInt
1646a 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c  egrity(p);.  sql
1646b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1646c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1646d 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1646e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1646f 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  M../*.** Set the
16470 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
16471 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69  ries for all chi
16472 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50  ldren of page pP
16473 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a  age. Also, if.**
16474 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20   pPage contains 
16475 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74  cells that point
16476 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
16477 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e  es, set the poin
16478 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69  ter.** map entri
16479 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66  es for the overf
1647a 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c  low pages as wel
1647b 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
1647c 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73   setChildPtrmaps
1647d 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
1647e 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
1647f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16480 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65         /* Counte
16481 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
16482 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20  int nCell;      
16483 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16484 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
16485 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50  cells in page pP
16486 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  age */.  int rc;
16487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16488 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
16489 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1648a 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1648b 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38  pPage->pBt;.  u8
1648c 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50   isInitOrig = pP
1648d 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50  age->isInit;.  P
1648e 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65  gno pgno = pPage
1648f 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72  ->pgno;..  asser
16490 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
16491 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
16492 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63  ->mutex) );.  rc
16493 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65   = btreeInitPage
16494 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  (pPage);.  if( r
16495 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16496 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69      goto set_chi
16497 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a  ld_ptrmaps_out;.
16498 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50    }.  nCell = pP
16499 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66  age->nCell;..  f
1649a 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
1649b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70   i++){.    u8 *p
1649c 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
1649d 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20  pPage, i);..    
1649e 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
1649f 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26  (pPage, pCell, &
164a0 72 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70  rc);..    if( !p
164a1 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
164a2 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67      Pgno childPg
164a3 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43  no = get4byte(pC
164a4 65 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d  ell);.      ptrm
164a5 61 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64  apPut(pBt, child
164a6 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  Pgno, PTRMAP_BTR
164a7 45 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a  EE, pgno, &rc);.
164a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
164a9 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
164aa 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50  .    Pgno childP
164ab 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26  gno = get4byte(&
164ac 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
164ad 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
164ae 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74  );.    ptrmapPut
164af 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c  (pBt, childPgno,
164b0 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
164b1 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a  gno, &rc);.  }..
164b2 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70  set_child_ptrmap
164b3 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e  s_out:.  pPage->
164b4 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f  isInit = isInitO
164b5 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  rig;.  return rc
164b6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77  ;.}../*.** Somew
164b7 68 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73  here on pPage is
164b8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61   a pointer to pa
164b9 67 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66  ge iFrom.  Modif
164ba 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73  y this pointer s
164bb 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69  o.** that it poi
164bc 6e 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61  nts to iTo. Para
164bd 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63  meter eType desc
164be 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f  ribes the type o
164bf 66 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20  f pointer to.** 
164c0 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20  be modified, as 
164c1 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
164c2 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20  PTRMAP_BTREE:   
164c3 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72    pPage is a btr
164c4 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69  ee-page. The poi
164c5 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61  nter points at a
164c6 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20   child .**      
164c7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
164c8 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a  e of pPage..**.*
164c9 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  * PTRMAP_OVERFLO
164ca 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62  W1: pPage is a b
164cb 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70  tree-page. The p
164cc 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74  ointer points at
164cd 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20   an overflow.** 
164ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164cf 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74    page pointed t
164d0 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20  o by one of the 
164d1 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a  cells on pPage..
164d2 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45  **.** PTRMAP_OVE
164d3 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73  RFLOW2: pPage is
164d4 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67   an overflow-pag
164d5 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70  e. The pointer p
164d6 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78  oints at the nex
164d7 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
164d8 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
164d9 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74  page in the list
164da 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
164db 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65  modifyPagePointe
164dc 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  r(MemPage *pPage
164dd 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67  , Pgno iFrom, Pg
164de 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65  no iTo, u8 eType
164df 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
164e0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
164e1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
164e2 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
164e3 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
164e4 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
164e5 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28  DbPage) );.  if(
164e6 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
164e7 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20  VERFLOW2 ){.    
164e8 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  /* The pointer i
164e9 73 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72  s always the fir
164ea 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68  st 4 bytes of th
164eb 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63  e page in this c
164ec 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ase.  */.    if(
164ed 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d   get4byte(pPage-
164ee 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29  >aData)!=iFrom )
164ef 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
164f0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
164f1 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75  PT;.    }.    pu
164f2 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44  t4byte(pPage->aD
164f3 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c  ata, iTo);.  }el
164f4 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69  se{.    u8 isIni
164f5 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69  tOrig = pPage->i
164f6 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69  sInit;.    int i
164f7 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b  ;.    int nCell;
164f8 0a 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50  ..    btreeInitP
164f9 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
164fa 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e  nCell = pPage->n
164fb 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69  Cell;..    for(i
164fc 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
164fd 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65  ){.      u8 *pCe
164fe 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
164ff 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69  age, i);.      i
16500 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
16501 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20  _OVERFLOW1 ){.  
16502 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69        CellInfo i
16503 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72  nfo;.        btr
16504 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
16505 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
16506 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  fo);.        if(
16507 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20   info.iOverflow 
16508 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
16509 20 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65   iFrom==get4byte
1650a 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
1650b 65 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20  erflow]) ){.    
1650c 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
1650d 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
1650e 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20  erflow], iTo);. 
1650f 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
16510 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
16511 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
16512 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28  lse{.        if(
16513 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29   get4byte(pCell)
16514 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ==iFrom ){.     
16515 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43       put4byte(pC
16516 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20  ell, iTo);.     
16517 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
16518 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
16519 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69    }.  .    if( i
1651a 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  ==nCell ){.     
1651b 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d   if( eType!=PTRM
1651c 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20  AP_BTREE || .   
1651d 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28         get4byte(
1651e 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
1651f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
16520 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  ])!=iFrom ){.   
16521 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
16522 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
16523 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
16524 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  ut4byte(&pPage->
16525 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
16526 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b  Offset+8], iTo);
16527 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67  .    }..    pPag
16528 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e  e->isInit = isIn
16529 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65  itOrig;.  }.  re
1652a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1652b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  }.../*.** Move t
1652c 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  he open database
1652d 20 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f   page pDbPage to
1652e 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50   location iFreeP
1652f 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64  age in the .** d
16530 61 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62  atabase. The pDb
16531 50 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72  Page reference r
16532 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a  emains valid..**
16533 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74  .** The isCommit
16534 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20   flag indicates 
16535 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f  that there is no
16536 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
16537 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f  r that.** the jo
16538 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62  urnal needs to b
16539 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72  e sync()ed befor
1653a 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
1653b 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a  pDbPage->pgno .*
1653c 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  * can be written
1653d 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20   to. The caller 
1653e 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d  has already prom
1653f 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74  ised not to writ
16540 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67  e to that.** pag
16541 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
16542 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20   relocatePage(. 
16543 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
16544 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72            /* Btr
16545 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ee */.  MemPage 
16546 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20  *pDbPage,       
16547 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f   /* Open page to
16548 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54   move */.  u8 eT
16549 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
1654a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d      /* Pointer m
1654b 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20  ap 'type' entry 
1654c 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20  for pDbPage */. 
1654d 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20   Pgno iPtrPage, 
1654e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1654f 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e  nter map 'page-n
16550 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62  o' entry for pDb
16551 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  Page */.  Pgno i
16552 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20  FreePage,       
16553 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69     /* The locati
16554 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61  on to move pDbPa
16555 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69  ge to */.  int i
16556 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20  sCommit         
16557 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20      /* isCommit 
16558 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73  flag passed to s
16559 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
1655a 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50  age */.){.  MemP
1655b 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20  age *pPtrPage;  
1655c 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61   /* The page tha
1655d 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  t contains a poi
1655e 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20  nter to pDbPage 
1655f 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67  */.  Pgno iDbPag
16560 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e  e = pDbPage->pgn
16561 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  o;.  Pager *pPag
16562 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72  er = pBt->pPager
16563 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
16564 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54  ssert( eType==PT
16565 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c  RMAP_OVERFLOW2 |
16566 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
16567 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20  OVERFLOW1 || .  
16568 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41      eType==PTRMA
16569 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65  P_BTREE || eType
1656a 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  ==PTRMAP_ROOTPAG
1656b 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  E );.  assert( s
1656c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1656d 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1656e 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61  .  assert( pDbPa
1656f 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a  ge->pBt==pBt );.
16570 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20  .  /* Move page 
16571 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73  iDbPage from its
16572 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f   current locatio
16573 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72  n to page number
16574 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20   iFreePage */.  
16575 54 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55  TRACE(("AUTOVACU
16576 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f  UM: Moving %d to
16577 20 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70   free page %d (p
16578 74 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20  tr page %d type 
16579 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69  %d)\n", .      i
1657a 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67  DbPage, iFreePag
1657b 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79  e, iPtrPage, eTy
1657c 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  pe));.  rc = sql
1657d 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67  ite3PagerMovepag
1657e 65 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67  e(pPager, pDbPag
1657f 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65  e->pDbPage, iFre
16580 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29  ePage, isCommit)
16581 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
16582 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
16583 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44  urn rc;.  }.  pD
16584 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46  bPage->pgno = iF
16585 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49  reePage;..  /* I
16586 66 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20  f pDbPage was a 
16587 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e  btree-page, then
16588 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69   it may have chi
16589 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20  ld pages and/or 
1658a 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20  cells.  ** that 
1658b 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f  point to overflo
1658c 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69  w pages. The poi
1658d 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73  nter map entries
1658e 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20   for all these. 
1658f 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74   ** pages need t
16590 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20  o be changed..  
16591 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61  **.  ** If pDbPa
16592 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f  ge is an overflo
16593 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65  w page, then the
16594 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d   first 4 bytes m
16595 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20  ay store a.  ** 
16596 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62  pointer to a sub
16597 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77  sequent overflow
16598 20 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69   page. If this i
16599 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
1659a 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  .  ** the pointe
1659b 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62  r map needs to b
1659c 65 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68  e updated for th
1659d 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65  e subsequent ove
1659e 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f  rflow page..  */
1659f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54  .  if( eType==PT
165a0 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54  RMAP_BTREE || eT
165a1 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype==PTRMAP_ROOT
165a2 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d  PAGE ){.    rc =
165a3 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73   setChildPtrmaps
165a4 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
165a5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
165a6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
165a7 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   rc;.    }.  }el
165a8 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78  se{.    Pgno nex
165a9 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65  tOvfl = get4byte
165aa 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29  (pDbPage->aData)
165ab 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76  ;.    if( nextOv
165ac 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  fl!=0 ){.      p
165ad 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65  trmapPut(pBt, ne
165ae 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f  xtOvfl, PTRMAP_O
165af 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50  VERFLOW2, iFreeP
165b0 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  age, &rc);.     
165b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
165b2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
165b3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
165b4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
165b5 20 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73   Fix the databas
165b6 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67  e pointer on pag
165b7 65 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20  e iPtrPage that 
165b8 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61  pointed at iDbPa
165b9 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20  ge so.  ** that 
165ba 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72  it points at iFr
165bb 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78  eePage. Also fix
165bc 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
165bd 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20   entry for.  ** 
165be 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20  iPtrPage..  */. 
165bf 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d   if( eType!=PTRM
165c0 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20  AP_ROOTPAGE ){. 
165c1 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74     rc = btreeGet
165c2 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61  Page(pBt, iPtrPa
165c3 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30  ge, &pPtrPage, 0
165c4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
165c5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
165c6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
165c7 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
165c8 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
165c9 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  trPage->pDbPage)
165ca 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
165cb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
165cc 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74   releasePage(pPt
165cd 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65  rPage);.      re
165ce 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
165cf 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61     rc = modifyPa
165d0 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61  gePointer(pPtrPa
165d1 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72  ge, iDbPage, iFr
165d2 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a  eePage, eType);.
165d3 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
165d4 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69  pPtrPage);.    i
165d5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
165d6 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70   ){.      ptrmap
165d7 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61  Put(pBt, iFreePa
165d8 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50  ge, eType, iPtrP
165d9 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d  age, &rc);.    }
165da 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
165db 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  ;.}../* Forward 
165dc 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75  declaration requ
165dd 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75  ired by incrVacu
165de 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61  umStep(). */.sta
165df 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
165e0 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72  BtreePage(BtShar
165e1 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a  ed *, MemPage **
165e2 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20  , Pgno *, Pgno, 
165e3 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  u8);../*.** Perf
165e4 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65  orm a single ste
165e5 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e  p of an incremen
165e6 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73  tal-vacuum. If s
165e7 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65  uccessful,.** re
165e8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  turn SQLITE_OK. 
165e9 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  If there is no w
165ea 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74  ork to do (and t
165eb 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70  herefore no.** p
165ec 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20  oint in calling 
165ed 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67  this function ag
165ee 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c  ain), return SQL
165ef 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20  ITE_DONE..**.** 
165f0 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c  More specificly,
165f1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
165f2 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72  ttempts to re-or
165f3 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64  ganize the .** d
165f4 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20  atabase so that 
165f5 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66  the last page of
165f6 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e   the file curren
165f7 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73  tly in use.** is
165f8 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73   no longer in us
165f9 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
165fa 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69  nFin parameter i
165fb 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73  s non-zero, this
165fc 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
165fd 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61  s.** that the ca
165fe 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63  ller will keep c
165ff 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75  alling incrVacuu
16600 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a  mStep() until.**
16601 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49   it returns SQLI
16602 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72  TE_DONE or an er
16603 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46  ror, and that nF
16604 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d  in is the.** num
16605 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65  ber of pages the
16606 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
16607 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65  ill contain afte
16608 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65  r this .** proce
16609 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20  ss is complete. 
1660a 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f   If nFin is zero
1660b 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20  , it is assumed 
1660c 74 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75  that.** incrVacu
1660d 75 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65  umStep() will be
1660e 20 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65   called a finite
1660f 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73   amount of times
16610 0a 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72  .** which may or
16611 20 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74   may not empty t
16612 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20  he freelist.  A 
16613 66 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a  full autovacuum.
16614 2a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20  ** has nFin>0.  
16615 41 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d  A "PRAGMA increm
16616 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61  ental_vacuum" ha
16617 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74  s nFin==0..*/.st
16618 61 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63  atic int incrVac
16619 75 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64  uumStep(BtShared
1661a 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e   *pBt, Pgno nFin
1661b 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b  , Pgno iLastPg){
1661c 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73  .  Pgno nFreeLis
1661d 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
1661e 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
1661f 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65  still on the fre
16620 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73  e-list */..  ass
16621 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
16622 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
16623 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
16624 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b   iLastPg>nFin );
16625 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f  ..  if( !PTRMAP_
16626 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73  ISPAGE(pBt, iLas
16627 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21  tPg) && iLastPg!
16628 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
16629 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69  GE(pBt) ){.    i
1662a 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54  nt rc;.    u8 eT
1662b 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50  ype;.    Pgno iP
1662c 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72  trPage;..    nFr
1662d 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74  eeList = get4byt
1662e 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  e(&pBt->pPage1->
1662f 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20  aData[36]);.    
16630 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30  if( nFreeList==0
16631 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
16632 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
16633 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74    }..    rc = pt
16634 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61  rmapGet(pBt, iLa
16635 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69  stPg, &eType, &i
16636 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66  PtrPage);.    if
16637 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
16638 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16639 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rc;.    }.    if
1663a 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
1663b 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20  ROOTPAGE ){.    
1663c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1663d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1663e 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79    }..    if( eTy
1663f 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
16640 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28  AGE ){.      if(
16641 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20   nFin==0 ){.    
16642 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68      /* Remove th
16643 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  e page from the 
16644 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e  files free-list.
16645 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71   This is not req
16646 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  uired.        **
16647 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d   if nFin is non-
16648 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61  zero. In that ca
16649 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73  se, the free-lis
1664a 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20  t will be.      
1664b 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74    ** truncated t
1664c 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69  o zero after thi
1664d 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
1664e 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27  ns, so it doesn'
1664f 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61  t .        ** ma
16650 74 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c  tter if it still
16651 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67   contains some g
16652 61 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a  arbage entries..
16653 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
16654 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b     Pgno iFreePg;
16655 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
16656 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20   *pFreePg;.     
16657 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
16658 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
16659 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50  pFreePg, &iFreeP
1665a 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a  g, iLastPg, 1);.
1665b 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1665c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1665d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1665e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1665f 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65      assert( iFre
16660 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a  ePg==iLastPg );.
16661 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
16662 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20  age(pFreePg);.  
16663 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65      }.    } else
16664 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46   {.      Pgno iF
16665 72 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20  reePg;          
16666 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
16667 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  ree page to move
16668 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20   pLastPg to */. 
16669 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c       MemPage *pL
1666a 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63  astPg;..      rc
1666b 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1666c 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70  pBt, iLastPg, &p
1666d 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20  LastPg, 0);.    
1666e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1666f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
16670 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
16671 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e  }..      /* If n
16672 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69  Fin is zero, thi
16673 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63  s loop runs exac
16674 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67  tly once and pag
16675 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20  e pLastPg.      
16676 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69  ** is swapped wi
16677 74 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65  th the first fre
16678 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66  e page pulled of
16679 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e  f the free list.
1667a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1667b 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ** On the other 
1667c 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73  hand, if nFin is
1667d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
1667e 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20  ro, then keep.  
1667f 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75      ** looping u
16680 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65  ntil a free-page
16681 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20   located within 
16682 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70  the first nFin p
16683 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66  ages.      ** of
16684 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75   the file is fou
16685 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nd..      */.   
16686 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20     do {.        
16687 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67  MemPage *pFreePg
16688 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61  ;.        rc = a
16689 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
1668a 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20  (pBt, &pFreePg, 
1668b 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b  &iFreePg, 0, 0);
1668c 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1668d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1668e 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1668f 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20  age(pLastPg);.  
16690 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
16691 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
16692 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
16693 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20  (pFreePg);.     
16694 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30   }while( nFin!=0
16695 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e   && iFreePg>nFin
16696 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
16697 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50  ( iFreePg<iLastP
16698 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20  g );.      .    
16699 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1669a 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67  gerWrite(pLastPg
1669b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1669c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1669d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
1669e 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65  c = relocatePage
1669f 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65  (pBt, pLastPg, e
166a0 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
166a1 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30  iFreePg, nFin!=0
166a2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
166a3 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61   releasePage(pLa
166a4 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28  stPg);.      if(
166a5 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
166a6 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
166a7 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
166a8 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46   }.  }..  if( nF
166a9 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61  in==0 ){.    iLa
166aa 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c  stPg--;.    whil
166ab 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44  e( iLastPg==PEND
166ac 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
166ad 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47  t)||PTRMAP_ISPAG
166ae 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20  E(pBt, iLastPg) 
166af 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52  ){.      if( PTR
166b0 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
166b1 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20  iLastPg) ){.    
166b2 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67      MemPage *pPg
166b3 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63  ;.        int rc
166b4 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
166b5 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70  pBt, iLastPg, &p
166b6 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  Pg, 0);.        
166b7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
166b8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
166b9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
166ba 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d    }.        rc =
166bb 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
166bc 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29  te(pPg->pDbPage)
166bd 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
166be 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20  ePage(pPg);.    
166bf 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
166c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
166c1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
166c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
166c3 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b        iLastPg--;
166c4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
166c5 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
166c6 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72  mage(pBt->pPager
166c7 2c 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a  , iLastPg);.  }.
166c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
166c9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77  OK;.}../*.** A w
166ca 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
166cb 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
166cc 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74  before calling t
166cd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
166ce 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73   It performs a s
166cf 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f  ingle unit of wo
166d0 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e  rk towards an in
166d1 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
166d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ..**.** If the i
166d3 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
166d4 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66  m is finished af
166d5 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
166d6 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51  n has run,.** SQ
166d7 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74  LITE_DONE is ret
166d8 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20  urned. If it is 
166d9 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75  not finished, bu
166da 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  t no error occur
166db 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  red,.** SQLITE_O
166dc 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  K is returned. O
166dd 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69  therwise an SQLi
166de 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a  te error code. .
166df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
166e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
166e1 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72  eeIncrVacuum(Btr
166e2 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ee *p){.  int rc
166e3 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
166e4 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73  t = p->pBt;..  s
166e5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
166e6 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
166e7 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
166e8 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26  n==TRANS_WRITE &
166e9 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  & p->inTrans==TR
166ea 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69  ANS_WRITE );.  i
166eb 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63  f( !pBt->autoVac
166ec 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  uum ){.    rc = 
166ed 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
166ee 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69  else{.    invali
166ef 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43  dateAllOverflowC
166f0 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72  ache(pBt);.    r
166f1 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
166f2 65 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72  ep(pBt, 0, pager
166f3 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b  Pagecount(pBt));
166f4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
166f5 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
166f6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
166f7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
166f8 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20  is called prior 
166f9 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  to sqlite3PagerC
166fa 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61  ommit when a tra
166fb 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63  nsaction.** is c
166fc 6f 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61  ommited for an a
166fd 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
166fe 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51  ase..**.** If SQ
166ff 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
16700 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75  ned, then *pnTru
16701 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  nc is set to the
16702 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
16703 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
16704 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
16705 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72  truncated to dur
16706 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70  ing the commit p
16707 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e  rocess. .** i.e.
16708 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
16709 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a  s been reorganiz
1670a 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20  ed so that only 
1670b 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75  the first *pnTru
1670c 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20  nc.** pages are 
1670d 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  in use..*/.stati
1670e 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d  c int autoVacuum
1670f 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20  Commit(BtShared 
16710 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20  *pBt){.  int rc 
16711 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50  = SQLITE_OK;.  P
16712 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
16713 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56  Bt->pPager;.  VV
16714 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66  A_ONLY( int nRef
16715 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52   = sqlite3PagerR
16716 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20  efcount(pPager) 
16717 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  );..  assert( sq
16718 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
16719 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1671a 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f    invalidateAllO
1671b 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74  verflowCache(pBt
1671c 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d  );.  assert(pBt-
1671d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20  >autoVacuum);.  
1671e 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61  if( !pBt->incrVa
1671f 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f  cuum ){.    Pgno
16720 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f   nFin;         /
16721 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
16722 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66  s in database af
16723 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e  ter autovacuumin
16724 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46  g */.    Pgno nF
16725 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ree;        /* N
16726 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
16727 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69  n the freelist i
16728 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20  nitially */.    
16729 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20  Pgno nPtrmap;   
1672a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1672b 50 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20  PtrMap pages to 
1672c 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
1672d 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20  Pgno iFree;     
1672e 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70     /* The next p
1672f 61 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20  age to be freed 
16730 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72  */.    int nEntr
16731 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  y;        /* Num
16732 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f  ber of entries o
16733 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67  n one ptrmap pag
16734 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f  e */.    Pgno nO
16735 72 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  rig;        /* D
16736 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66  atabase size bef
16737 6f 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a  ore freeing */..
16738 20 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65      nOrig = page
16739 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b  rPagecount(pBt);
1673a 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f  .    if( PTRMAP_
1673b 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69  ISPAGE(pBt, nOri
1673c 67 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e  g) || nOrig==PEN
1673d 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1673e 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  Bt) ){.      /* 
1673f 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  It is not possib
16740 6c 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64  le to create a d
16741 61 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63  atabase for whic
16742 68 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65  h the final page
16743 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74  .      ** is eit
16744 68 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61  her a pointer-ma
16745 70 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65  p page or the pe
16746 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e  nding-byte page.
16747 20 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a   If one.      **
16748 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
16749 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
1674a 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20  corruption..    
1674b 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72    */.      retur
1674c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1674d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20  _BKPT;.    }..  
1674e 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79    nFree = get4by
1674f 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
16750 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20  >aData[36]);.   
16751 20 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75   nEntry = pBt->u
16752 73 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20  sableSize/5;.   
16753 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65   nPtrmap = (nFre
16754 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50  e-nOrig+PTRMAP_P
16755 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67  AGENO(pBt, nOrig
16756 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79  )+nEntry)/nEntry
16757 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72  ;.    nFin = nOr
16758 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74  ig - nFree - nPt
16759 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f  rmap;.    if( nO
1675a 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45  rig>PENDING_BYTE
1675b 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46  _PAGE(pBt) && nF
1675c 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  in<PENDING_BYTE_
1675d 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
1675e 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d     nFin--;.    }
1675f 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d  .    while( PTRM
16760 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e  AP_ISPAGE(pBt, n
16761 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45  Fin) || nFin==PE
16762 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
16763 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  pBt) ){.      nF
16764 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  in--;.    }.    
16765 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29  if( nFin>nOrig )
16766 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
16767 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20  ORRUPT_BKPT;..  
16768 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69    for(iFree=nOri
16769 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26  g; iFree>nFin &&
1676a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
1676b 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20  iFree--){.      
1676c 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53  rc = incrVacuumS
1676d 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69  tep(pBt, nFin, i
1676e 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Free);.    }.   
1676f 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45   if( (rc==SQLITE
16770 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c  _DONE || rc==SQL
16771 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65  ITE_OK) && nFree
16772 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  >0 ){.      rc =
16773 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
16774 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16775 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50  gerWrite(pBt->pP
16776 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age1->pDbPage);.
16777 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
16778 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
16779 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20  ta[32], 0);.    
1677a 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d    put4byte(&pBt-
1677b 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
1677c 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  6], 0);.      sq
1677d 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
1677e 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61  teImage(pBt->pPa
1677f 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20  ger, nFin);.    
16780 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
16781 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16782 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
16783 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  lback(pPager);. 
16784 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65     }.  }..  asse
16785 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65  rt( nRef==sqlite
16786 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
16787 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75  Pager) );.  retu
16788 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20  rn rc;.}..#else 
16789 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
1678a 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1678b 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74   */.# define set
1678c 43 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20  ChildPtrmaps(x) 
1678d 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66  SQLITE_OK.#endif
1678e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1678f 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69  tine does the fi
16790 72 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74  rst phase of a t
16791 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e  wo-phase commit.
16792 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
16793 2a 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62  * causes a rollb
16794 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62  ack journal to b
16795 65 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74  e created (if it
16796 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
16797 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20  y exist).** and 
16798 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65  populated with e
16799 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f  nough informatio
1679a 6e 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70  n so that if a p
1679b 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73  ower loss occurs
1679c 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
1679d 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64   can be restored
1679e 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
1679f 20 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e   state by playin
167a0 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f  g back.** the jo
167a1 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65  urnal.  Then the
167a2 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
167a3 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75   journal are flu
167a4 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74  shed out to.** t
167a5 68 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20  he disk.  After 
167a6 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  the journal is s
167a7 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20  afely on oxide, 
167a8 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74  the changes to t
167a9 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61  he.** database a
167aa 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  re written into 
167ab 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
167ac 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f  e and flushed to
167ad 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68   oxide..** At th
167ae 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61  e end of this ca
167af 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  ll, the rollback
167b0 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65   journal still e
167b1 78 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20  xists on the.** 
167b2 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20  disk and we are 
167b3 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c  still holding al
167b4 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20  l locks, so the 
167b5 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20  transaction has 
167b6 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64  not.** committed
167b7 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74  .  See sqlite3Bt
167b8 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
167b9 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f  o() for the seco
167ba 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a  nd phase of the.
167bb 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73  ** commit proces
167bc 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61  s..**.** This ca
167bd 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ll is a no-op if
167be 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61   no write-transa
167bf 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74  ction is current
167c0 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74  ly active on pBt
167c1 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
167c2 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61  e, sync the data
167c3 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68  base file for th
167c4 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61  e btree pBt. zMa
167c5 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a  ster points to.*
167c6 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  * the name of a 
167c7 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
167c8 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ile that should 
167c9 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
167ca 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  the.** individua
167cb 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  l journal file, 
167cc 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69  or is NULL, indi
167cd 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72  cating no master
167ce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a   journal file .*
167cf 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61  * (single databa
167d0 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e  se transaction).
167d1 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  .**.** When this
167d2 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
167d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73  master journal s
167d4 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61  hould already ha
167d5 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74  ve been.** creat
167d6 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69  ed, populated wi
167d7 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  th this journal 
167d8 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63  pointer and sync
167d9 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  ed to disk..**.*
167da 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72  * Once this is r
167db 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72  outine has retur
167dc 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68  ned, the only th
167dd 69 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20  ing required to 
167de 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72  commit.** the wr
167df 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
167e0 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73  for this databas
167e1 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c  e file is to del
167e2 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  ete the journal.
167e3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
167e4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
167e5 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
167e6 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73  e(Btree *p, cons
167e7 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
167e8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
167e9 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d  ITE_OK;.  if( p-
167ea 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
167eb 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53  WRITE ){.    BtS
167ec 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
167ed 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  pBt;.    sqlite3
167ee 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23  BtreeEnter(p);.#
167ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
167f0 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
167f1 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
167f2 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72  acuum ){.      r
167f3 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f  c = autoVacuumCo
167f4 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20  mmit(pBt);.     
167f5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
167f6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  OK ){.        sq
167f7 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
167f8 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
167f9 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
167fa 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
167fb 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
167fc 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
167fd 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61  pBt->pPager, zMa
167fe 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71  ster, 0);.    sq
167ff 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
16800 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
16801 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
16802 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
16803 61 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20  alled from both 
16804 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
16805 54 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52  Two() and BtreeR
16806 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20  ollback().** at 
16807 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
16808 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  f a transaction.
16809 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1680a 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74  btreeEndTransact
1680b 69 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ion(Btree *p){. 
1680c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1680d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
1680e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1680f 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a  oldsMutex(p) );.
16810 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73  .  btreeClearHas
16811 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20  Content(pBt);.  
16812 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54  if( p->inTrans>T
16813 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 2d 3e  RANS_NONE && p->
16814 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
16815 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  t>1 ){.    /* If
16816 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72   there are other
16817 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
16818 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74  ts that belong t
16819 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0a  o this database.
1681a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20 64      ** handle, d
1681b 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72 65  owngrade to a re
1681c 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74  ad-only transact
1681d 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20 73  ion. The other s
1681e 74 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a  tatements.    **
1681f 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65   may still be re
16820 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64  ading from the d
16821 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 20  atabase.  */.   
16822 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61   downgradeAllSha
16823 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
16824 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e  ks(p);.    p->in
16825 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45  Trans = TRANS_RE
16826 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  AD;.  }else{.   
16827 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
16828 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f  e had any kind o
16829 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  f transaction op
1682a 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68  en, decrement th
1682b 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61  e .    ** transa
1682c 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74  ction count of t
1682d 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e  he shared btree.
1682e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   If the transact
1682f 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a  ion count .    *
16830 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74  * reaches 0, set
16831 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74   the shared stat
16832 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e  e to TRANS_NONE.
16833 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65   The unlockBtree
16834 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a  IfUnused().    *
16835 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c  * call below wil
16836 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67  l unlock the pag
16837 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  er.  */.    if( 
16838 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  p->inTrans!=TRAN
16839 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  S_NONE ){.      
1683a 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61  clearAllSharedCa
1683b 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29  cheTableLocks(p)
1683c 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  ;.      pBt->nTr
1683d 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20  ansaction--;.   
1683e 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e     if( 0==pBt->n
1683f 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20  Transaction ){. 
16840 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72         pBt->inTr
16841 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
16842 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a  S_NONE;.      }.
16843 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65      }..    /* Se
16844 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  t the current tr
16845 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20  ansaction state 
16846 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e  to TRANS_NONE an
16847 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20  d unlock the .  
16848 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68    ** pager if th
16849 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74  is call closed t
1684a 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20  he only read or 
1684b 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1684c 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e  n.  */.    p->in
1684d 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
1684e 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74  NE;.    unlockBt
1684f 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
16850 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e  ;.  }..  btreeIn
16851 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f  tegrity(p);.}../
16852 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20  *.** Commit the 
16853 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72  transaction curr
16854 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73  ently in progres
16855 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
16856 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
16857 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73   the second phas
16858 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63  e of a 2-phase c
16859 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73  ommit.  The.** s
1685a 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1685b 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74  tPhaseOne() rout
1685c 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
1685d 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f  st phase and sho
1685e 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65  uld.** be invoke
1685f 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  d prior to calli
16860 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
16861 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72    The sqlite3Btr
16862 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
16863 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69  ().** routine di
16864 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f  d all the work o
16865 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d  f writing inform
16866 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73  ation out to dis
16867 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74  k and flushing t
16868 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73  he.** contents s
16869 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20  o that they are 
1686a 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65  written onto the
1686b 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20   disk platter.  
1686c 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  All this.** rout
1686d 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73  ine has to do is
1686e 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63   delete or trunc
1686f 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20  ate or zero the 
16870 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a  header in the.**
16871 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
16872 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75  urnal (which cau
16873 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74  ses the transact
16874 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61  ion to commit) a
16875 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73  nd.** drop locks
16876 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c  ..**.** This wil
16877 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72  l release the wr
16878 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ite lock on the 
16879 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
1687a 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  If there.** are 
1687b 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72  no active cursor
1687c 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61  s, it also relea
1687d 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ses the read loc
1687e 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
1687f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16880 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
16881 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  Two(Btree *p){. 
16882 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
16883 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69   p->pBt;..  sqli
16884 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
16885 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ;.  btreeIntegri
16886 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ty(p);..  /* If 
16887 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61  the handle has a
16888 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
16889 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20  on open, commit 
1688a 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
1688b 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  s .  ** transact
1688c 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20  ion and set the 
1688d 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20  shared state to 
1688e 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f  TRANS_READ..  */
1688f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
16890 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
16891 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
16892 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
16893 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
16894 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20  ANS_WRITE );.   
16895 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54   assert( pBt->nT
16896 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a  ransaction>0 );.
16897 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16898 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
16899 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29  Two(pBt->pPager)
1689a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1689b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1689c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1689d 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74  ve(p);.      ret
1689e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1689f 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
168a0 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41  tion = TRANS_REA
168a1 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45  D;.  }..  btreeE
168a2 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29  ndTransaction(p)
168a3 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
168a4 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
168a5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
168a6 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70  ./*.** Do both p
168a7 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69  hases of a commi
168a8 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
168a9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
168aa 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65  BtreeCommit(Btre
168ab 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
168ac 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
168ad 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
168ae 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
168af 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29  itPhaseOne(p, 0)
168b0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
168b1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
168b2 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
168b3 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b  mmitPhaseTwo(p);
168b4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
168b5 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
168b6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
168b7 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
168b8 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
168b9 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72  ber of write-cur
168ba 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69  sors open on thi
168bb 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69  s handle. This i
168bc 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20  s for use.** in 
168bd 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73  assert() express
168be 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f  ions, so it is o
168bf 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20  nly compiled if 
168c0 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a  NDEBUG is not.**
168c1 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
168c2 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
168c3 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
168c4 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72  , a write-cursor
168c5 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74   is any cursor t
168c6 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c  hat.** is capabl
168c7 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20  e of writing to 
168c8 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68  the databse.  Th
168c9 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72  at means the cur
168ca 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69  sor was.** origi
168cb 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72  nally opened for
168cc 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65   writing and the
168cd 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20   cursor has not 
168ce 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62  be disabled.** b
168cf 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61  y having its sta
168d0 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55  te changed to CU
168d1 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73  RSOR_FAULT..*/.s
168d2 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57  tatic int countW
168d3 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68  riteCursors(BtSh
168d4 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74  ared *pBt){.  Bt
168d5 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
168d6 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72  int r = 0;.  for
168d7 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73  (pCur=pBt->pCurs
168d8 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70  or; pCur; pCur=p
168d9 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Cur->pNext){.   
168da 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61   if( pCur->wrFla
168db 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74  g && pCur->eStat
168dc 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e!=CURSOR_FAULT 
168dd 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65  ) r++; .  }.  re
168de 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66  turn r;.}.#endif
168df 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
168e0 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74  tine sets the st
168e1 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41  ate to CURSOR_FA
168e2 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ULT and the erro
168e3 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72  r.** code to err
168e4 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63  Code for every c
168e5 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65  ursor on BtShare
168e6 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a  d that pBtree.**
168e7 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a   references..**.
168e8 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20  ** Every cursor 
168e9 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c  is tripped, incl
168ea 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68  uding cursors th
168eb 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20  at belong.** to 
168ec 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
168ed 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20  onnections that 
168ee 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61  happen to be sha
168ef 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68  ring.** the cach
168f0 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a  e with pBtree..*
168f1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
168f2 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68  e gets called wh
168f3 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63  en a rollback oc
168f4 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72  curs..** All cur
168f5 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73  sors using the s
168f6 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62  ame cache must b
168f7 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20  e tripped.** to 
168f8 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f  prevent them fro
168f9 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20  m trying to use 
168fa 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a  the btree after.
168fb 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e  ** the rollback.
168fc 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d    The rollback m
168fd 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20  ay have deleted 
168fe 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76  tables.** or mov
168ff 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73  ed root pages, s
16900 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66  o it is not suff
16901 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76  icient to.** sav
16902 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
16903 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  he cursor.  The 
16904 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a  cursor must be.*
16905 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  * invalidated..*
16906 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16907 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
16908 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
16909 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1690a 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20  int errCode){.  
1690b 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73  BtCursor *p;.  s
1690c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1690d 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28  (pBtree);.  for(
1690e 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70  p=pBtree->pBt->p
1690f 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e  Cursor; p; p=p->
16910 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
16911 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  i;.    sqlite3Bt
16912 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70  reeClearCursor(p
16913 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65  );.    p->eState
16914 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b   = CURSOR_FAULT;
16915 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74  .    p->skipNext
16916 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20   = errCode;.    
16917 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69  for(i=0; i<=p->i
16918 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Page; i++){.    
16919 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d    releasePage(p-
1691a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
1691b 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20     p->apPage[i] 
1691c 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
1691d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1691e 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f  ve(pBtree);.}../
1691f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68  *.** Rollback th
16920 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  e transaction in
16921 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20   progress.  All 
16922 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a  cursors will be.
16923 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20  ** invalided by 
16924 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20  this operation. 
16925 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
16926 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  use a cursor.** 
16927 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74  that was open at
16928 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
16929 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  f this operation
1692a 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20   will result.** 
1692b 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  in an error..**.
1692c 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c  ** This will rel
1692d 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c  ease the write l
1692e 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
1692f 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68  ase file.  If th
16930 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63  ere.** are no ac
16931 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74  tive cursors, it
16932 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74   also releases t
16933 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f  he read lock..*/
16934 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16935 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16936 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a  Rollback(Btree *
16937 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
16938 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16939 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
1693a 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71  e *pPage1;..  sq
1693b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1693c 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41  p);.  rc = saveA
1693d 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30  llCursors(pBt, 0
1693e 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  , 0);.#ifndef SQ
1693f 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
16940 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21  _CACHE.  if( rc!
16941 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16942 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68    /* This is a h
16943 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f  orrible situatio
16944 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c  n. An IO or mall
16945 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
16946 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a  red whilst.    *
16947 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65  * trying to save
16948 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e   cursor position
16949 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  s. If this is an
1694a 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62   automatic rollb
1694b 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74  ack (as.    ** t
1694c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63  he result of a c
1694d 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f  onstraint, mallo
1694e 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49  c() failure or I
1694f 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20  O error) then . 
16950 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20     ** the cache 
16951 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c  may be internall
16952 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28  y inconsistent (
16953 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69  not contain vali
16954 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20  d trees) so.    
16955 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d  ** we cannot sim
16956 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65  ply return the e
16957 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c  rror to the call
16958 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f  er. Instead, abo
16959 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71  rt .    ** all q
1695a 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20  ueries that may 
1695b 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20  be using any of 
1695c 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  the cursors that
1695d 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e   failed to save.
1695e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1695f 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
16960 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20  ursors(p, rc);. 
16961 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65   }.#endif.  btre
16962 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a  eIntegrity(p);..
16963 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
16964 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  ==TRANS_WRITE ){
16965 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20  .    int rc2;.. 
16966 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53     assert( TRANS
16967 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54  _WRITE==pBt->inT
16968 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20  ransaction );.  
16969 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50    rc2 = sqlite3P
1696a 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74  agerRollback(pBt
1696b 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
1696c 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f  f( rc2!=SQLITE_O
1696d 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
1696e 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  rc2;.    }..    
1696f 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  /* The rollback 
16970 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79  may have destroy
16971 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61  ed the pPage1->a
16972 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a  Data value.  So.
16973 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65      ** call btre
16974 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61  eGetPage() on pa
16975 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61  ge 1 again to ma
16976 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70  ke.    ** sure p
16977 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20  Page1->aData is 
16978 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a  set correctly. *
16979 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47  /.    if( btreeG
1697a 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
1697b 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49  pPage1, 0)==SQLI
1697c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1697d 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1697e 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  1);.    }.    as
1697f 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65  sert( countWrite
16980 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20  Cursors(pBt)==0 
16981 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  );.    pBt->inTr
16982 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
16983 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62  S_READ;.  }..  b
16984 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69  treeEndTransacti
16985 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  on(p);.  sqlite3
16986 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
16987 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
16988 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61  *.** Start a sta
16989 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  tement subtransa
1698a 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72  ction. The subtr
1698b 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61  ansaction can ca
1698c 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62  n be rolled.** b
1698d 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c  ack independentl
1698e 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72  y of the main tr
1698f 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d  ansaction. You m
16990 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e  ust start a tran
16991 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f  saction .** befo
16992 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75  re starting a su
16993 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68  btransaction. Th
16994 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e  e subtransaction
16995 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61   is ended automa
16996 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74  tically .** if t
16997 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74  he main transact
16998 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72  ion commits or r
16999 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  olls back..**.**
1699a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   Statement subtr
1699b 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75  ansactions are u
1699c 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76  sed around indiv
1699d 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  idual SQL statem
1699e 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ents.** that are
1699f 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
169a0 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d  n a BEGIN...COMM
169a1 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20  IT block.  If a 
169a2 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72  constraint.** er
169a3 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69  ror occurs withi
169a4 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c  n the statement,
169a5 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74   the effect of t
169a6 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e  hat one statemen
169a7 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c  t.** can be roll
169a8 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20  ed back without 
169a9 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61  having to rollba
169aa 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72  ck the entire tr
169ab 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
169ac 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62   A statement sub
169ad 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
169ae 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
169af 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65  n anonymous save
169b0 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61  point. The.** va
169b1 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68  lue passed as th
169b2 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
169b3 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  er is the total 
169b4 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f  number of savepo
169b5 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69  ints,.** includi
169b6 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79  ng the new anony
169b7 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20  mous savepoint, 
169b8 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72  open on the B-Tr
169b9 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72  ee. i.e. if ther
169ba 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69  e.** are no acti
169bb 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e  ve savepoints an
169bc 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65  d no other state
169bd 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
169be 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74  s open,.** iStat
169bf 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73  ement is 1. This
169c0 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70   anonymous savep
169c1 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65  oint can be rele
169c2 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62  ased or rolled b
169c3 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ack.** using the
169c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76   sqlite3BtreeSav
169c5 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f  epoint() functio
169c6 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
169c7 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
169c8 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42  BtreeBeginStmt(B
169c9 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74  tree *p, int iSt
169ca 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20  atement){.  int 
169cb 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  rc;.  BtShared *
169cc 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
169cd 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
169ce 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
169cf 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
169d0 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73  S_WRITE );.  ass
169d1 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e  ert( pBt->readOn
169d2 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ly==0 );.  asser
169d3 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20  t( iStatement>0 
169d4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  );.  assert( iSt
169d5 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e  atement>p->db->n
169d6 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69  Savepoint );.  i
169d7 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72  f( NEVER(p->inTr
169d8 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans!=TRANS_WRITE
169d9 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c   || pBt->readOnl
169da 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  y) ){.    rc = S
169db 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a  QLITE_INTERNAL;.
169dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
169dd 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
169de 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
169df 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41  RITE );.    /* A
169e0 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65  t the pager leve
169e1 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  l, a statement t
169e2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20  ransaction is a 
169e3 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20  savepoint with. 
169e4 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67     ** an index g
169e5 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20  reater than all 
169e6 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74  savepoints creat
169e7 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73  ed explicitly us
169e8 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73  ing.    ** SQL s
169e9 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73  tatements. It is
169ea 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e   illegal to open
169eb 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c  , release or rol
169ec 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a  lback any.    **
169ed 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73   such savepoints
169ee 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65   while the state
169ef 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
169f0 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63   savepoint is ac
169f1 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tive..    */.   
169f2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
169f3 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28  erOpenSavepoint(
169f4 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74  pBt->pPager, iSt
169f5 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20  atement);.  }.  
169f6 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
169f7 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
169f8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
169f9 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
169fa 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
169fb 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20  , op, is always 
169fc 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
169fd 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49  CK.** or SAVEPOI
169fe 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73  NT_RELEASE. This
169ff 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72   function either
16a00 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c   releases or rol
16a01 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73  ls back the.** s
16a02 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66  avepoint identif
16a03 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
16a04 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70   iSavepoint, dep
16a05 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61  ending on the va
16a06 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a  lue .** of op..*
16a07 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69  *.** Normally, i
16a08 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65  Savepoint is gre
16a09 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
16a0a 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65  al to zero. Howe
16a0b 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a  ver, if op is.**
16a0c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
16a0d 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70  ACK, then iSavep
16a0e 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65  oint may also be
16a0f 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73   -1. In this cas
16a10 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e  e the .** conten
16a11 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  ts of the entire
16a12 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65   transaction are
16a13 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68   rolled back. Th
16a14 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a  is is different.
16a15 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c  ** from a normal
16a16 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c   transaction rol
16a17 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63  lback, as no loc
16a18 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20  ks are released 
16a19 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73  and the.** trans
16a1a 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f  action remains o
16a1b 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pen..*/.SQLITE_P
16a1c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16a1d 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
16a1e 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f  (Btree *p, int o
16a1f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  p, int iSavepoin
16a20 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
16a21 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
16a22 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d  p && p->inTrans=
16a23 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
16a24 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42      BtShared *pB
16a25 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20  t = p->pBt;.    
16a26 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45  assert( op==SAVE
16a27 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c  POINT_RELEASE ||
16a28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   op==SAVEPOINT_R
16a29 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61  OLLBACK );.    a
16a2a 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e  ssert( iSavepoin
16a2b 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f  t>=0 || (iSavepo
16a2c 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53  int==-1 && op==S
16a2d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
16a2e 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  K) );.    sqlite
16a2f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
16a30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16a31 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70  PagerSavepoint(p
16a32 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20  Bt->pPager, op, 
16a33 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
16a34 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16a35 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
16a36 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74   newDatabase(pBt
16a37 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
16a38 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
16a39 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
16a3a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  rc;.}../*.** Cre
16a3b 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72  ate a new cursor
16a3c 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77   for the BTree w
16a3d 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20  hose root is on 
16a3e 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62  the page.** iTab
16a3f 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e  le. If a read-on
16a40 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71  ly cursor is req
16a41 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73  uested, it is as
16a42 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68  sumed that.** th
16a43 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79  e caller already
16a44 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20   has at least a 
16a45 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61  read-only transa
16a46 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e  ction open.** on
16a47 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c   the database al
16a48 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74  ready. If a writ
16a49 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75  e-cursor is requ
16a4a 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74  ested, then.** t
16a4b 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73  he caller is ass
16a4c 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20  umed to have an 
16a4d 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
16a4e 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  action..**.** If
16a4f 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e   wrFlag==0, then
16a50 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20   the cursor can 
16a51 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72  only be used for
16a52 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20   reading..** If 
16a53 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20  wrFlag==1, then 
16a54 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62  the cursor can b
16a55 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69  e used for readi
16a56 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69  ng or for.** wri
16a57 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f  ting if other co
16a58 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69  nditions for wri
16a59 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65  ting are also me
16a5a 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65  t.  These.** are
16a5b 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20   the conditions 
16a5c 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74  that must be met
16a5d 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72   in order for wr
16a5e 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61  iting to.** be a
16a5f 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a  llowed:.**.** 1:
16a60 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73    The cursor mus
16a61 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e  t have been open
16a62 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d  ed with wrFlag==
16a63 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65  1.**.** 2:  Othe
16a64 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
16a65 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72  ctions that shar
16a66 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72  e the same pager
16a67 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75   cache.**     bu
16a68 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20  t which are not 
16a69 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f  in the READ_UNCO
16a6a 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61  MMITTED state ma
16a6b 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20  y not have.**   
16a6c 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77    cursors open w
16a6d 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e  ith wrFlag==0 on
16a6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e   the same table.
16a6f 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20    Otherwise.**  
16a70 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d     the changes m
16a71 61 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74  ade by this writ
16a72 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62  e cursor would b
16a73 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20  e visible to.** 
16a74 20 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72      the read cur
16a75 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65  sors in the othe
16a76 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
16a77 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20  ction..**.** 3: 
16a78 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   The database mu
16a79 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28  st be writable (
16a7a 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79  not on read-only
16a7b 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a   media).**.** 4:
16a7c 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20    There must be 
16a7d 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61  an active transa
16a7e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20  ction..**.** No 
16a7f 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65  checking is done
16a80 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
16a81 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72  at page iTable r
16a82 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20  eally is the.** 
16a83 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62  root page of a b
16a84 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73  -tree.  If it is
16a85 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63   not, then the c
16a86 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a  ursor acquired.*
16a87 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20  * will not work 
16a88 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a  correctly..**.**
16a89 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
16a8a 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42  hat the sqlite3B
16a8b 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29  treeCursorZero()
16a8c 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
16a8d 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69  .** on pCur to i
16a8e 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
16a8f 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72  mory space prior
16a90 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69   to invoking thi
16a91 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74  s routine..*/.st
16a92 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75  atic int btreeCu
16a93 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70  rsor(.  Btree *p
16a94 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16a95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16a96 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20  * The btree */. 
16a97 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20   int iTable,    
16a98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a99 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
16a9a 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f  page of table to
16a9b 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77   open */.  int w
16a9c 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  rFlag,          
16a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a9e 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e    /* 1 to write.
16a9f 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a   0 read-only */.
16aa0 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f    struct KeyInfo
16aa1 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20   *pKeyInfo,     
16aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
16aa3 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69  t arg to compari
16aa4 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  son function */.
16aa5 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
16aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16aa7 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
16aa8 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72  e for new cursor
16aa9 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65   */.){.  BtShare
16aaa 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
16aab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16aac 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65  /* Shared b-tree
16aad 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73   handle */..  as
16aae 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
16aaf 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20  eeHoldsMutex(p) 
16ab0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46  );.  assert( wrF
16ab1 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67  lag==0 || wrFlag
16ab2 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  ==1 );..  /* The
16ab3 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72   following asser
16ab4 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72  t statements ver
16ab5 69 66 79 20 74 68 61 74 20 69 66 20 74 68 69 73  ify that if this
16ab6 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a   is a sharable .
16ab7 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61    ** b-tree data
16ab8 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63  base, the connec
16ab9 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20  tion is holding 
16aba 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62  the required tab
16abb 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20  le locks, .  ** 
16abc 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  and that no othe
16abd 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73  r connection has
16abe 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72   any open cursor
16abf 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20   that conflicts 
16ac0 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20  with .  ** this 
16ac1 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65  lock.  */.  asse
16ac2 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63  rt( hasSharedCac
16ac3 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  heTableLock(p, i
16ac4 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21  Table, pKeyInfo!
16ac5 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b  =0, wrFlag+1) );
16ac6 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61  .  assert( wrFla
16ac7 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64  g==0 || !hasRead
16ac8 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61  Conflicts(p, iTa
16ac9 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73  ble) );..  /* As
16aca 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61  sert that the ca
16acb 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20  ller has opened 
16acc 74 68 65 20 72 65 71 75 69 72 65 64 20 74 72 61  the required tra
16acd 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61  nsaction. */.  a
16ace 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
16acf 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a  s>TRANS_NONE );.
16ad0 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67    assert( wrFlag
16ad1 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e  ==0 || p->inTran
16ad2 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
16ad3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
16ad4 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e  >pPage1 && pBt->
16ad5 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b  pPage1->aData );
16ad6 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 72  ..  if( NEVER(wr
16ad7 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65 61  Flag && pBt->rea
16ad8 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 65  dOnly) ){.    re
16ad9 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44  turn SQLITE_READ
16ada 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ONLY;.  }.  if( 
16adb 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61 67  iTable==1 && pag
16adc 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
16add 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
16ade 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a  n SQLITE_EMPTY;.
16adf 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68    }..  /* Now th
16ae0 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f  at no other erro
16ae1 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 69  rs can occur, fi
16ae2 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20  nish filling in 
16ae3 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 2a  the BtCursor.  *
16ae4 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20  * variables and 
16ae5 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20  link the cursor 
16ae6 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72 65  into the BtShare
16ae7 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43  d list.  */.  pC
16ae8 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28  ur->pgnoRoot = (
16ae9 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70  Pgno)iTable;.  p
16aea 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b  Cur->iPage = -1;
16aeb 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  .  pCur->pKeyInf
16aec 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  o = pKeyInfo;.  
16aed 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70  pCur->pBtree = p
16aee 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20  ;.  pCur->pBt = 
16aef 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46  pBt;.  pCur->wrF
16af0 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67  lag = (u8)wrFlag
16af1 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20  ;.  pCur->pNext 
16af2 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a  = pBt->pCursor;.
16af3 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78    if( pCur->pNex
16af4 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70  t ){.    pCur->p
16af5 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43  Next->pPrev = pC
16af6 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70  ur;.  }.  pBt->p
16af7 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20  Cursor = pCur;. 
16af8 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
16af9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
16afa 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f    pCur->cachedRo
16afb 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  wid = 0;.  retur
16afc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53  n SQLITE_OK;.}.S
16afd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16afe 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
16aff 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70  rsor(.  Btree *p
16b00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b02 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
16b03 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
16b04 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b06 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
16b07 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  f table to open 
16b08 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c  */.  int wrFlag,
16b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b0b 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20   /* 1 to write. 
16b0c 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  0 read-only */. 
16b0d 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
16b0e 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
16b0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16b10 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f  First arg to xCo
16b11 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43  mpare() */.  BtC
16b12 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20  ursor *pCur     
16b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b14 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
16b15 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72  e new cursor her
16b16 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
16b17 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16b18 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d  Enter(p);.  rc =
16b19 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20   btreeCursor(p, 
16b1a 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20  iTable, wrFlag, 
16b1b 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b  pKeyInfo, pCur);
16b1c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
16b1d 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
16b1e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
16b1f 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
16b20 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a  f a BtCursor obj
16b21 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a  ect in bytes..**
16b22 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
16b23 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f  ces is needed so
16b24 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63   that users of c
16b25 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c  ursors can preal
16b26 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63  locate.** suffic
16b27 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20  ient storage to 
16b28 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20  hold a cursor.  
16b29 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a  The BtCursor obj
16b2a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a  ect is opaque.**
16b2b 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65   to users so the
16b2c 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20  y cannot do the 
16b2d 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c  sizeof() themsel
16b2e 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20  ves - they must 
16b2f 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75  call.** this rou
16b30 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tine..*/.SQLITE_
16b31 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16b32 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
16b33 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  ze(void){.  retu
16b34 72 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  rn ROUND8(sizeof
16b35 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a  (BtCursor));.}..
16b36 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
16b37 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c   memory that wil
16b38 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69  l be converted i
16b39 6e 74 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f  nto a BtCursor o
16b3a 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  bject..**.** The
16b3b 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68   simple approach
16b3c 20 68 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74   here would be t
16b3d 6f 20 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65  o memset() the e
16b3e 6e 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20  ntire object.** 
16b3f 74 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74  to zero.  But it
16b40 20 74 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20   turns out that 
16b41 74 68 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64  the apPage[] and
16b42 20 61 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a   aiIdx[] arrays.
16b43 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74  ** do not need t
16b44 6f 20 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20  o be zeroed and 
16b45 74 68 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20  they are large, 
16b46 73 6f 20 77 65 20 63 61 6e 20 73 61 76 65 20 61  so we can save a
16b47 20 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74   lot.** of run-t
16b48 69 6d 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20  ime by skipping 
16b49 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  the initializati
16b4a 6f 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d  on of those elem
16b4b 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
16b4c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16b4d 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a  ite3BtreeCursorZ
16b4e 65 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29  ero(BtCursor *p)
16b4f 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  {.  memset(p, 0,
16b50 20 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73   offsetof(BtCurs
16b51 6f 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a  or, iPage));.}..
16b52 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61  /*.** Set the ca
16b53 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65  ched rowid value
16b54 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f 72   of every cursor
16b55 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   in the same dat
16b56 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73  abase file.** as
16b57 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67   pCur and having
16b58 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70   the same root p
16b59 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43  age number as pC
16b5a 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  ur.  The value i
16b5b 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77  s.** set to iRow
16b5c 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70  id..**.** Only p
16b5d 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 61  ositive rowid va
16b5e 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65  lues are conside
16b5f 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68  red valid for th
16b60 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65  is cache..** The
16b61 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 61   cache is initia
16b62 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69  lized to zero, i
16b63 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76  ndicating an inv
16b64 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41  alid cache..** A
16b65 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b   btree will work
16b66 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20   fine with zero 
16b67 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69  or negative rowi
16b68 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e  ds.  We just can
16b69 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72  not.** cache zer
16b6a 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f  o or negative ro
16b6b 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e  wids, which mean
16b6c 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 73  s tables that us
16b6d 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67  e zero or.** neg
16b6e 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67  ative rowids mig
16b6f 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20  ht run a little 
16b70 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20  slower.  But in 
16b71 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a  practice, zero.*
16b72 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f  * or negative ro
16b73 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75 6e  wids are very un
16b74 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73  common so this s
16b75 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70  hould not be a p
16b76 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  roblem..*/.SQLIT
16b77 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
16b78 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
16b79 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73  chedRowid(BtCurs
16b7a 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65  or *pCur, sqlite
16b7b 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b  3_int64 iRowid){
16b7c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a  .  BtCursor *p;.
16b7d 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42    for(p=pCur->pB
16b7e 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
16b7f 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
16b80 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d  if( p->pgnoRoot=
16b81 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20  =pCur->pgnoRoot 
16b82 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64  ) p->cachedRowid
16b83 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20   = iRowid;.  }. 
16b84 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63   assert( pCur->c
16b85 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77  achedRowid==iRow
16b86 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  id );.}../*.** R
16b87 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 64  eturn the cached
16b88 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67   rowid for the g
16b89 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20  iven cursor.  A 
16b8a 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f  negative or zero
16b8b 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65  .** return value
16b8c 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
16b8d 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20  the rowid cache 
16b8e 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73  is invalid and s
16b8f 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f  hould be.** igno
16b90 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77  red.  If the row
16b91 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 76  id cache has nev
16b92 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73  er before been s
16b93 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65  et, then a.** ze
16b94 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ro is returned..
16b95 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16b96 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  E sqlite3_int64 
16b97 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43  sqlite3BtreeGetC
16b98 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
16b99 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65  sor *pCur){.  re
16b9a 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65  turn pCur->cache
16b9b 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  dRowid;.}../*.**
16b9c 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e   Close a cursor.
16b9d 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20    The read lock 
16b9e 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
16b9f 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64  file is released
16ba0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73  .** when the las
16ba1 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  t cursor is clos
16ba2 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
16ba3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
16ba4 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f  3BtreeCloseCurso
16ba5 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
16ba6 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72  ){.  Btree *pBtr
16ba7 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  ee = pCur->pBtre
16ba8 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20  e;.  if( pBtree 
16ba9 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
16baa 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
16bab 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20  = pCur->pBt;.   
16bac 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
16bad 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20  er(pBtree);.    
16bae 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
16baf 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20  rCursor(pCur);. 
16bb0 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72     if( pCur->pPr
16bb1 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72  ev ){.      pCur
16bb2 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
16bb3 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20   pCur->pNext;.  
16bb4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
16bb5 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43  Bt->pCursor = pC
16bb6 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ur->pNext;.    }
16bb7 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70  .    if( pCur->p
16bb8 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43  Next ){.      pC
16bb9 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  ur->pNext->pPrev
16bba 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a   = pCur->pPrev;.
16bbb 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
16bbc 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67  0; i<=pCur->iPag
16bbd 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72  e; i++){.      r
16bbe 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d  eleasePage(pCur-
16bbf 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
16bc0 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72   }.    unlockBtr
16bc1 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
16bc2 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  .    invalidateO
16bc3 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75  verflowCache(pCu
16bc4 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74  r);.    /* sqlit
16bc5 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a  e3_free(pCur); *
16bc6 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  /.    sqlite3Btr
16bc7 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b  eeLeave(pBtree);
16bc8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
16bc9 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16bca 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
16bcb 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20  BtCursor* given 
16bcc 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  in the argument 
16bcd 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42  has a valid.** B
16bce 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72  tCursor.info str
16bcf 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 69  ucture.  If it i
16bd0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61  s not already va
16bd1 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72  lid, call.** btr
16bd2 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f  eeParseCell() to
16bd3 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a   fill it in..**.
16bd4 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f  ** BtCursor.info
16bd5 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74   is a cache of t
16bd6 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  he information i
16bd7 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65  n the current ce
16bd8 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69  ll..** Using thi
16bd9 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20  s cache reduces 
16bda 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  the number of ca
16bdb 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73  lls to btreePars
16bdc 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32  eCell()..**.** 2
16bdd 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72  007-06-25:  Ther
16bde 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f  e is a bug in so
16bdf 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d  me versions of M
16be0 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 74  SVC that cause t
16be1 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74  he.** compiler t
16be2 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 74  o crash when get
16be3 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d  CellInfo() is im
16be4 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d  plemented as a m
16be5 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65  acro..** But the
16be6 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 61  re is a measurea
16be7 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e 74  ble speed advant
16be8 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65  age to using the
16be9 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a   macro on gcc.**
16bea 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70   (when less comp
16beb 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  iler optimizatio
16bec 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d  ns like -Os or -
16bed 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 20  O0 are used and 
16bee 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  the.** compiler 
16bef 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72  is not doing agr
16bf0 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e  essive inlining.
16bf1 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 72  )  So we use a r
16bf2 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  eal function.** 
16bf3 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d  for MSVC and a m
16bf4 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68  acro for everyth
16bf5 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65  ing else.  Ticke
16bf6 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e  t #2457..*/.#ifn
16bf7 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61  def NDEBUG.  sta
16bf8 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 43  tic void assertC
16bf9 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72  ellInfo(BtCursor
16bfa 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c   *pCur){.    Cel
16bfb 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20  lInfo info;.    
16bfc 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
16bfd 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d  ->iPage;.    mem
16bfe 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69  set(&info, 0, si
16bff 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20  zeof(info));.   
16c00 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28   btreeParseCell(
16c01 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61  pCur->apPage[iPa
16c02 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78  ge], pCur->aiIdx
16c03 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b  [iPage], &info);
16c04 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d  .    assert( mem
16c05 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72  cmp(&info, &pCur
16c06 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69  ->info, sizeof(i
16c07 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a  nfo))==0 );.  }.
16c08 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
16c09 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78  assertCellInfo(x
16c0a 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
16c0b 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73  _MSC_VER.  /* Us
16c0c 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f  e a real functio
16c0d 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72  n in MSVC to wor
16c0e 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e  k around bugs in
16c0f 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20   that compiler. 
16c10 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  */.  static void
16c11 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43   getCellInfo(BtC
16c12 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
16c13 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f    if( pCur->info
16c14 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20  .nSize==0 ){.   
16c15 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70     int iPage = p
16c16 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20  Cur->iPage;.    
16c17 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
16c18 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50  (pCur->apPage[iP
16c19 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78  age],pCur->aiIdx
16c1a 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69  [iPage],&pCur->i
16c1b 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72  nfo);.      pCur
16c1c 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b  ->validNKey = 1;
16c1d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
16c1e 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f    assertCellInfo
16c1f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (pCur);.    }.  
16c20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f  }.#else /* if no
16c21 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20  t _MSC_VER */.  
16c22 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69  /* Use a macro i
16c23 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70  n all other comp
16c24 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68  ilers so that th
16c25 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  e function is in
16c26 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  lined */.#define
16c27 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
16c28 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r)              
16c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c2b 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
16c2c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
16c2d 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  ==0 ){          
16c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
16c31 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d  nt iPage = pCur-
16c32 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  >iPage;         
16c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c35 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
16c36 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70  btreeParseCell(p
16c37 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67  Cur->apPage[iPag
16c38 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  e],pCur->aiIdx[i
16c39 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66  Page],&pCur->inf
16c3a 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e  o); \.    pCur->
16c3b 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20  validNKey = 1;  
16c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c3f 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
16c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c44 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65        \.    asse
16c45 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  rtCellInfo(pCur)
16c46 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16c47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c49 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e         \.  }.#en
16c4a 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20  dif /* _MSC_VER 
16c4b 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
16c4c 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20  UG  /* The next 
16c4d 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c  routine used onl
16c4e 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28  y within assert(
16c4f 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  ) statements */.
16c50 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
16c51 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 42  e if the given B
16c52 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64  tCursor is valid
16c53 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f  .  A valid curso
16c54 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74  r is one.** that
16c55 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
16c56 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20  inting to a row 
16c57 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29  in a (non-empty)
16c58 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20   table..** This 
16c59 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69 6f  is a verificatio
16c5a 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  n routine is use
16c5b 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73  d only within as
16c5c 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
16c5d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
16c5e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16c5f 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
16c60 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
16c61 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75  r){.  return pCu
16c62 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74  r && pCur->eStat
16c63 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b  e==CURSOR_VALID;
16c64 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45  .}.#endif /* NDE
16c65 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65  BUG */../*.** Se
16c66 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20  t *pSize to the 
16c67 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66  size of the buff
16c68 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c  er needed to hol
16c69 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a  d the value of.*
16c6a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68  * the key for th
16c6b 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e  e current entry.
16c6c 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20    If the cursor 
16c6d 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a  is not pointing.
16c6e 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e  ** to a valid en
16c6f 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73  try, *pSize is s
16c70 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20  et to 0. .**.** 
16c71 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 68  For a table with
16c72 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67   the INTKEY flag
16c73 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69   set, this routi
16c74 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b  ne returns the k
16c75 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f  ey.** itself, no
16c76 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
16c77 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79  bytes in the key
16c78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
16c79 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e  er must position
16c7a 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f   the cursor prio
16c7b 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68  r to invoking th
16c7c 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a  is routine..** .
16c7d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16c7e 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74  cannot fail.  It
16c7f 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
16c80 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a  SQLITE_OK.  .*/.
16c81 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16c82 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
16c83 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20  eySize(BtCursor 
16c84 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a  *pCur, i64 *pSiz
16c85 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  e){.  assert( cu
16c86 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16c87 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
16c88 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16c89 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c  CURSOR_INVALID |
16c8a 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  | pCur->eState==
16c8b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16c8c 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
16c8d 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te!=CURSOR_VALID
16c8e 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d   ){.    *pSize =
16c8f 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
16c90 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75   getCellInfo(pCu
16c91 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d  r);.    *pSize =
16c92 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
16c93 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
16c94 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
16c95 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f  ** Set *pSize to
16c96 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
16c97 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20  ytes of data in 
16c98 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a  the entry the.**
16c99 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c   cursor currentl
16c9a 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a  y points to..**.
16c9b 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  ** The caller mu
16c9c 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  st guarantee tha
16c9d 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
16c9e 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f  pointing to a no
16c9f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20  n-NULL.** valid 
16ca0 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72  entry.  In other
16ca1 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c   words, the call
16ca2 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75  ing procedure mu
16ca3 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20  st guarantee.** 
16ca4 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
16ca5 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74  has Cursor.eStat
16ca6 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e  e==CURSOR_VALID.
16ca7 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69  .**.** Failure i
16ca8 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20  s not possible. 
16ca9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
16caa 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51  lways returns SQ
16cab 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d  LITE_OK..** It m
16cac 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c  ight just as wel
16cad 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 65  l be a procedure
16cae 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64   (returning void
16caf 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75  ) but we continu
16cb0 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61  e.** to return a
16cb1 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74  n integer result
16cb2 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72   code for histor
16cb3 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f  ical reasons..*/
16cb4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16cb5 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16cb6 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f  DataSize(BtCurso
16cb7 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53  r *pCur, u32 *pS
16cb8 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ize){.  assert( 
16cb9 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16cba 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
16cbb 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16cbc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16cbd 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28  ;.  getCellInfo(
16cbe 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20  pCur);.  *pSize 
16cbf 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61  = pCur->info.nDa
16cc0 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ta;.  return SQL
16cc1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
16cc2 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 20   Given the page 
16cc3 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65  number of an ove
16cc4 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68  rflow page in th
16cc5 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72 61  e database (para
16cc6 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20  meter.** ovfl), 
16cc7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69  this function fi
16cc8 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d  nds the page num
16cc9 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ber of the next 
16cca 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20  page in the .** 
16ccb 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f  linked list of o
16ccc 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49  verflow pages. I
16ccd 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75  f possible, it u
16cce 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63  ses the auto-vac
16ccf 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d  uum.** pointer-m
16cd0 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 20  ap data instead 
16cd1 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 63  of reading the c
16cd2 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f  ontent of page o
16cd3 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a  vfl to do so. .*
16cd4 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
16cd5 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74   occurs an SQLit
16cd6 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
16cd7 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
16cd8 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ise:.**.** The p
16cd9 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
16cda 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20  e next overflow 
16cdb 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b  page in the link
16cdc 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77  ed list is .** w
16cdd 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f  ritten to *pPgno
16cde 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76  Next. If page ov
16cdf 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 70  fl is the last p
16ce0 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65  age in its linke
16ce1 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67  d .** list, *pPg
16ce2 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f  noNext is set to
16ce3 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66   zero. .**.** If
16ce4 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e   ppPage is not N
16ce5 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72  ULL, and a refer
16ce6 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50  ence to the MemP
16ce7 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65  age object corre
16ce8 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70  sponding.** to p
16ce9 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c  age number pOvfl
16cea 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74   was obtained, t
16ceb 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73  hen *ppPage is s
16cec 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
16ced 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  hat.** reference
16cee 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70  . It is the resp
16cef 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68  onsibility of th
16cf0 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c  e caller to call
16cf1 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a   releasePage().*
16cf2 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20  * on *ppPage to 
16cf3 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e  free the referen
16cf4 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65  ce. In no refere
16cf5 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64  nce was obtained
16cf6 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65   (because.** the
16cf7 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73   pointer-map was
16cf8 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20   used to obtain 
16cf9 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70  the value for *p
16cfa 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a  PgnoNext), then.
16cfb 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  ** *ppPage is se
16cfc 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74  t to zero..*/.st
16cfd 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72  atic int getOver
16cfe 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68  flowPage(.  BtSh
16cff 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
16d00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
16d01 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
16d02 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20  .  Pgno ovfl,   
16d03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16d04 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66  /* Current overf
16d05 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20  low page number 
16d06 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  */.  MemPage **p
16d07 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  pPage,          
16d08 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67    /* OUT: MemPag
16d09 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65  e handle (may be
16d0a 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f   NULL) */.  Pgno
16d0b 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20   *pPgnoNext     
16d0c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
16d0d 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70   Next overflow p
16d0e 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b  age number */.){
16d0f 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30  .  Pgno next = 0
16d10 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
16d11 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  ge = 0;.  int rc
16d12 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
16d13 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16d14 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
16d15 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
16d16 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a  ert(pPgnoNext);.
16d17 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16d18 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
16d19 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64    /* Try to find
16d1a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
16d1b 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c  n the overflow l
16d1c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20  ist using the.  
16d1d 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f  ** autovacuum po
16d1e 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e  inter-map pages.
16d1f 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20   Guess that the 
16d20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20  next page in .  
16d21 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  ** the overflow 
16d22 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d  list is page num
16d23 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66  ber (ovfl+1). If
16d24 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e   that guess turn
16d25 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62  s .  ** out to b
16d26 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61  e wrong, fall ba
16d27 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68  ck to loading th
16d28 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a  e data of page .
16d29 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c    ** number ovfl
16d2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
16d2b 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62  e next page numb
16d2c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  er..  */.  if( p
16d2d 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
16d2e 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b  {.    Pgno pgno;
16d2f 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73  .    Pgno iGuess
16d30 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75   = ovfl+1;.    u
16d31 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68  8 eType;..    wh
16d32 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
16d33 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20  GE(pBt, iGuess) 
16d34 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49  || iGuess==PENDI
16d35 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
16d36 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73  ) ){.      iGues
16d37 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  s++;.    }..    
16d38 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65  if( iGuess<=page
16d39 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
16d3a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74  ){.      rc = pt
16d3b 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75  rmapGet(pBt, iGu
16d3c 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67  ess, &eType, &pg
16d3d 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  no);.      if( r
16d3e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
16d3f 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
16d40 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d  ERFLOW2 && pgno=
16d41 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20  =ovfl ){.       
16d42 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a   next = iGuess;.
16d43 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
16d44 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20  ITE_DONE;.      
16d45 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  }.    }.  }.#end
16d46 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65  if..  assert( ne
16d47 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c  xt==0 || rc==SQL
16d48 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66  ITE_DONE );.  if
16d49 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16d4a 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
16d4b 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76  eGetPage(pBt, ov
16d4c 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a  fl, &pPage, 0);.
16d4d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
16d4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61  SQLITE_OK || pPa
16d4f 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ge==0 );.    if(
16d50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16d51 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67  {.      next = g
16d52 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61  et4byte(pPage->a
16d53 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Data);.    }.  }
16d54 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d  ..  *pPgnoNext =
16d55 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50   next;.  if( ppP
16d56 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61  age ){.    *ppPa
16d57 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65  ge = pPage;.  }e
16d58 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  lse{.    release
16d59 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d  Page(pPage);.  }
16d5a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53  .  return (rc==S
16d5b 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c  QLITE_DONE ? SQL
16d5c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a  ITE_OK : rc);.}.
16d5d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61  ./*.** Copy data
16d5e 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74   from a buffer t
16d5f 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f  o a page, or fro
16d60 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 75  m a page to a bu
16d61 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79  ffer..**.** pPay
16d62 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65  load is a pointe
16d63 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64  r to data stored
16d64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67   on database pag
16d65 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66  e pDbPage..** If
16d66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73   argument eOp is
16d67 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79   false, then nBy
16d68 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  te bytes of data
16d69 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66   are copied.** f
16d6a 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20  rom pPayload to 
16d6b 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74  the buffer point
16d6c 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 49  ed at by pBuf. I
16d6d 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a  f eOp is true,.*
16d6e 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61  * then sqlite3Pa
16d6f 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61  gerWrite() is ca
16d70 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20  lled on pDbPage 
16d71 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a  and nByte bytes.
16d72 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 63  ** of data are c
16d73 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62  opied from the b
16d74 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 50  uffer pBuf to pP
16d75 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  ayload..**.** SQ
16d76 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
16d77 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20  ned on success, 
16d78 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72  otherwise an err
16d79 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
16d7a 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f  ic int copyPaylo
16d7b 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79  ad(.  void *pPay
16d7c 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 20  load,           
16d7d 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61  /* Pointer to pa
16d7e 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69  ge data */.  voi
16d7f 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20  d *pBuf,        
16d80 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
16d81 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20  r to buffer */. 
16d82 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
16d83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
16d84 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f  mber of bytes to
16d85 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65   copy */.  int e
16d86 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Op,             
16d87 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70       /* 0 -> cop
16d88 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d  y from page, 1 -
16d89 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a  > copy to page *
16d8a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  /.  DbPage *pDbP
16d8b 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a  age           /*
16d8c 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67   Page containing
16d8d 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a   pPayload */.){.
16d8e 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20    if( eOp ){.   
16d8f 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72   /* Copy data fr
16d90 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67  om buffer to pag
16d91 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61  e (a write opera
16d92 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74  tion) */.    int
16d93 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16d94 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29  erWrite(pDbPage)
16d95 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
16d96 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16d97 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16d98 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61  }.    memcpy(pPa
16d99 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79  yload, pBuf, nBy
16d9a 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  te);.  }else{.  
16d9b 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66    /* Copy data f
16d9c 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66  rom page to buff
16d9d 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 61  er (a read opera
16d9e 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d  tion) */.    mem
16d9f 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f  cpy(pBuf, pPaylo
16da0 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a  ad, nByte);.  }.
16da1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16da2 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
16da3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
16da4 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76  ed to read or ov
16da5 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20  erwrite payload 
16da6 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66  information.** f
16da7 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  or the entry tha
16da8 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f  t the pCur curso
16da9 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
16daa 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20  . If the eOp.** 
16dab 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20  parameter is 0, 
16dac 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f  this is a read o
16dad 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63  peration (data c
16dae 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75  opied into.** bu
16daf 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 69  ffer pBuf). If i
16db0 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61  t is non-zero, a
16db1 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f 70   write (data cop
16db2 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66  ied from.** buff
16db3 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20  er pBuf)..**.** 
16db4 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22  A total of "amt"
16db5 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20   bytes are read 
16db6 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e  or written begin
16db7 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22  ning at "offset"
16db8 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61  ..** Data is rea
16db9 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65  d to or from the
16dba 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a   buffer pBuf..**
16dbb 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  .** The content 
16dbc 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72  being read or wr
16dbd 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65  itten might appe
16dbe 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70  ar on the main p
16dbf 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61  age.** or be sca
16dc0 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75  ttered out on mu
16dc1 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20  ltiple overflow 
16dc2 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pages..**.** If 
16dc3 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49  the BtCursor.isI
16dc4 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c  ncrblobHandle fl
16dc5 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74  ag is set, and t
16dc6 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75  he current.** cu
16dc7 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20  rsor entry uses 
16dc8 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72  one or more over
16dc9 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73  flow pages, this
16dca 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c   function.** all
16dcb 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f 72  ocates space for
16dcc 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c   and lazily popl
16dcd 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c  uates the overfl
16dce 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a  ow page-list .**
16dcf 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42 74   cache array (Bt
16dd0 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77  Cursor.aOverflow
16dd1 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 61  ). Subsequent ca
16dd2 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20  lls use this.** 
16dd3 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65  cache to make se
16dd4 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70  eking to the sup
16dd5 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72  plied offset mor
16dd6 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a  e efficient..**.
16dd7 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  ** Once an overf
16dd8 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
16dd9 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c  che has been all
16dda 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62  ocated, it may b
16ddb 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64  e.** invalidated
16ddc 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63   if some other c
16ddd 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20  ursor writes to 
16dde 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20  the same table, 
16ddf 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72  or if.** the cur
16de0 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20  sor is moved to 
16de1 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e  a different row.
16de2 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69   Additionally, i
16de3 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a  n auto-vacuum.**
16de4 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f   mode, the follo
16de5 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20  wing events may 
16de6 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76  invalidate an ov
16de7 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74  erflow page-list
16de8 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20   cache..**.**   
16de9 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c  * An incremental
16dea 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20   vacuum,.**   * 
16deb 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f  A commit in auto
16dec 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d  _vacuum="full" m
16ded 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61  ode,.**   * Crea
16dee 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61  ting a table (ma
16def 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67  y require moving
16df0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
16df1 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  e)..*/.static in
16df2 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  t accessPayload(
16df3 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
16df4 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  r,      /* Curso
16df5 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e  r pointing to en
16df6 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  try to read from
16df7 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74   */.  u32 offset
16df8 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65  ,          /* Be
16df9 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73  gin reading this
16dfa 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61   far into payloa
16dfb 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20  d */.  u32 amt, 
16dfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
16dfd 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79  ead this many by
16dfe 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tes */.  unsigne
16dff 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a  d char *pBuf, /*
16e00 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73   Write the bytes
16e01 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65   into this buffe
16e02 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20  r */ .  int eOp 
16e03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16e04 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f  zero to read. no
16e05 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e  n-zero to write.
16e06 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65   */.){.  unsigne
16e07 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64  d char *aPayload
16e08 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
16e09 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b  ITE_OK;.  u32 nK
16e0a 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d  ey;.  int iIdx =
16e0b 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70   0;.  MemPage *p
16e0c 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
16e0d 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16e0e 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20  ; /* Btree page 
16e0f 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79  of current entry
16e10 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
16e11 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b  pBt = pCur->pBt;
16e12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e13 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20    /* Btree this 
16e14 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74  cursor belongs t
16e15 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  o */..  assert( 
16e16 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72  pPage );.  asser
16e17 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16e18 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16e19 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16e1a 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
16e1b 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ge]<pPage->nCell
16e1c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75   );.  assert( cu
16e1d 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16e1e 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65  Cur) );..  getCe
16e1f 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20  llInfo(pCur);.  
16e20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d  aPayload = pCur-
16e21 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43  >info.pCell + pC
16e22 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72  ur->info.nHeader
16e23 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67  ;.  nKey = (pPag
16e24 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20  e->intKey ? 0 : 
16e25 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e  (int)pCur->info.
16e26 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45  nKey);..  if( NE
16e27 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e  VER(offset+amt >
16e28 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f   nKey+pCur->info
16e29 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26  .nData) .   || &
16e2a 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69  aPayload[pCur->i
16e2b 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70  nfo.nLocal] > &p
16e2c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d  Page->aData[pBt-
16e2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29  >usableSize].  )
16e2e 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20  {.    /* Trying 
16e2f 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  to read or write
16e30 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
16e31 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20   the data is an 
16e32 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74  error */.    ret
16e33 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16e34 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20  PT_BKPT;.  }..  
16e35 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61  /* Check if data
16e36 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72   must be read/wr
16e37 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68  itten to/from th
16e38 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73  e btree page its
16e39 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66  elf. */.  if( of
16e3a 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e  fset<pCur->info.
16e3b 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e  nLocal ){.    in
16e3c 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69  t a = amt;.    i
16e3d 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72  f( a+offset>pCur
16e3e 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b  ->info.nLocal ){
16e3f 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d  .      a = pCur-
16e40 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f  >info.nLocal - o
16e41 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20  ffset;.    }.   
16e42 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61   rc = copyPayloa
16e43 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73  d(&aPayload[offs
16e44 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f  et], pBuf, a, eO
16e45 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67  p, pPage->pDbPag
16e46 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d  e);.    offset =
16e47 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20   0;.    pBuf += 
16e48 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b  a;.    amt -= a;
16e49 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66  .  }else{.    of
16e4a 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e  fset -= pCur->in
16e4b 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a  fo.nLocal;.  }..
16e4c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16e4d 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a  _OK && amt>0 ){.
16e4e 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76      const u32 ov
16e4f 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  flSize = pBt->us
16e50 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f  ableSize - 4;  /
16e51 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20  * Bytes content 
16e52 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f  per ovfl page */
16e53 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61  .    Pgno nextPa
16e54 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67  ge;..    nextPag
16e55 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50  e = get4byte(&aP
16e56 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66  ayload[pCur->inf
16e57 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66  o.nLocal]);..#if
16e58 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16e59 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a  _INCRBLOB.    /*
16e5a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c   If the isIncrbl
16e5b 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73  obHandle flag is
16e5c 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43   set and the BtC
16e5d 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b  ursor.aOverflow[
16e5e 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74  ].    ** has not
16e5f 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
16e60 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77   allocate it now
16e61 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73  . The array is s
16e62 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f  ized at.    ** o
16e63 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ne entry for eac
16e64 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  h overflow page 
16e65 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
16e66 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a  chain. The.    *
16e67 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * page number of
16e68 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
16e69 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72  low page is stor
16e6a 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b  ed in aOverflow[
16e6b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20  0],.    ** etc. 
16e6c 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20  A value of 0 in 
16e6d 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20  the aOverflow[] 
16e6e 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74  array means "not
16e6f 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20   yet known".    
16e70 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73  ** (the cache is
16e71 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65   lazily populate
16e72 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d)..    */.    i
16e73 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62  f( pCur->isIncrb
16e74 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43  lobHandle && !pC
16e75 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b  ur->aOverflow ){
16e76 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c  .      int nOvfl
16e77 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   = (pCur->info.n
16e78 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e  Payload-pCur->in
16e79 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69  fo.nLocal+ovflSi
16e7a 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a  ze-1)/ovflSize;.
16e7b 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65        pCur->aOve
16e7c 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29  rflow = (Pgno *)
16e7d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
16e7e 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e  o(sizeof(Pgno)*n
16e7f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20  Ovfl);.      /* 
16e80 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20  nOvfl is always 
16e81 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 74  positive.  If it
16e82 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63   were zero, fetc
16e83 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68  hPayload would h
16e84 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65  ave.      ** bee
16e85 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f  n used instead o
16e86 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  f this routine. 
16e87 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
16e88 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70  AYS(nOvfl) && !p
16e89 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29  Cur->aOverflow )
16e8a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
16e8b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
16e8c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
16e8d 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c  /* If the overfl
16e8e 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
16e8f 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f  he has been allo
16e90 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20  cated and the.  
16e91 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74    ** entry for t
16e92 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72 65  he first require
16e93 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  d overflow page 
16e94 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20  is valid, skip. 
16e95 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74     ** directly t
16e96 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o it..    */.   
16e97 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72   if( pCur->aOver
16e98 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f  flow && pCur->aO
16e99 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f  verflow[offset/o
16e9a 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20  vflSize] ){.    
16e9b 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74    iIdx = (offset
16e9c 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20  /ovflSize);.    
16e9d 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75    nextPage = pCu
16e9e 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64  r->aOverflow[iId
16e9f 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74  x];.      offset
16ea0 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53   = (offset%ovflS
16ea1 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ize);.    }.#end
16ea2 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72  if..    for( ; r
16ea3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
16ea4 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67  amt>0 && nextPag
16ea5 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66  e; iIdx++){..#if
16ea6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16ea7 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20  _INCRBLOB.      
16ea8 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20  /* If required, 
16ea9 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65  populate the ove
16eaa 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
16eab 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20  cache. */.      
16eac 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  if( pCur->aOverf
16ead 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61  low ){.        a
16eae 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76  ssert(!pCur->aOv
16eaf 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20  erflow[iIdx] || 
16eb0 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
16eb1 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29  iIdx]==nextPage)
16eb2 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  ;.        pCur->
16eb3 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20  aOverflow[iIdx] 
16eb4 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20  = nextPage;.    
16eb5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
16eb6 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76    if( offset>=ov
16eb7 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  flSize ){.      
16eb8 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65    /* The only re
16eb9 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69  ason to read thi
16eba 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74  s page is to obt
16ebb 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20  ain the page.   
16ebc 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66       ** number f
16ebd 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  or the next page
16ebe 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
16ebf 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65   chain. The page
16ec0 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61  .        ** data
16ec1 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
16ec2 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 74  . So first try t
16ec3 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65  o lookup the ove
16ec4 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a  rflow.        **
16ec5 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
16ec6 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66  , if any, then f
16ec7 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20  all back to the 
16ec8 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28  getOverflowPage(
16ec9 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e  ).        ** fun
16eca 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a  ction..        *
16ecb 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
16ecc 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
16ecd 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d         if( pCur-
16ece 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43  >aOverflow && pC
16ecf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
16ed0 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20  dx+1] ){.       
16ed1 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43     nextPage = pC
16ed2 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
16ed3 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d  dx+1];.        }
16ed4 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20   else .#endif.  
16ed5 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74          rc = get
16ed6 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74  OverflowPage(pBt
16ed7 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26  , nextPage, 0, &
16ed8 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20  nextPage);.     
16ed9 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66     offset -= ovf
16eda 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c  lSize;.      }el
16edb 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  se{.        /* N
16edc 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 73  eed to read this
16edd 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20   page properly. 
16ede 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65  It contains some
16edf 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
16ee0 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61  ** range of data
16ee1 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 72   that is being r
16ee2 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20  ead (eOp==0) or 
16ee3 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29  written (eOp!=0)
16ee4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
16ee5 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62       DbPage *pDb
16ee6 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e  Page;.        in
16ee7 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20  t a = amt;.     
16ee8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
16ee9 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
16eea 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26  ger, nextPage, &
16eeb 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
16eec 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16eed 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
16eee 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69   aPayload = sqli
16eef 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
16ef0 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
16ef1 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67      nextPage = g
16ef2 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64  et4byte(aPayload
16ef3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
16ef4 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76   a + offset > ov
16ef5 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  flSize ){.      
16ef6 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69        a = ovflSi
16ef7 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20  ze - offset;.   
16ef8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
16ef9 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c     rc = copyPayl
16efa 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66  oad(&aPayload[of
16efb 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61  fset+4], pBuf, a
16efc 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b  , eOp, pDbPage);
16efd 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
16efe 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62  e3PagerUnref(pDb
16eff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
16f00 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20   offset = 0;.   
16f01 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b         amt -= a;
16f02 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66 20  .          pBuf 
16f03 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a  += a;.        }.
16f04 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16f05 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
16f06 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20  ITE_OK && amt>0 
16f07 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16f08 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
16f09 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  T;.  }.  return 
16f0a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  rc;.}../*.** Rea
16f0b 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65  d part of the ke
16f0c 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
16f0d 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20  h cursor pCur.  
16f0e 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22  Exactly.** "amt"
16f0f 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74   bytes will be t
16f10 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70  ransfered into p
16f11 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e  Buf[].  The tran
16f12 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61  sfer.** begins a
16f13 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a  t "offset"..**.*
16f14 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  * The caller mus
16f15 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 43  t ensure that pC
16f16 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  ur is pointing t
16f17 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a  o a valid row.**
16f18 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   in the table..*
16f19 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
16f1a 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
16f1b 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64   or an error cod
16f1c 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f  e if anything go
16f1d 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e  es.** wrong.  An
16f1e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
16f1f 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d  ed if "offset+am
16f20 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  t" is larger tha
16f21 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62  n.** the availab
16f22 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53  le payload..*/.S
16f23 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16f24 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
16f25 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  y(BtCursor *pCur
16f26 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
16f27 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75  2 amt, void *pBu
16f28 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  f){.  assert( cu
16f29 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16f2a 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
16f2b 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16f2c 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16f2d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16f2e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72  iPage>=0 && pCur
16f2f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16f30 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72  Page] );.  asser
16f31 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
16f32 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72  Cur->iPage]<pCur
16f33 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16f34 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  Page]->nCell );.
16f35 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50    return accessP
16f36 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66  ayload(pCur, off
16f37 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67  set, amt, (unsig
16f38 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20  ned char*)pBuf, 
16f39 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  0);.}../*.** Rea
16f3a 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  d part of the da
16f3b 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
16f3c 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20  th cursor pCur. 
16f3d 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74   Exactly.** "amt
16f3e 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20  " bytes will be 
16f3f 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20  transfered into 
16f40 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61  pBuf[].  The tra
16f41 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20  nsfer.** begins 
16f42 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a  at "offset"..**.
16f43 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
16f44 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  _OK on success o
16f45 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
16f46 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
16f47 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65  .** wrong.  An e
16f48 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
16f49 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22   if "offset+amt"
16f4a 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a   is larger than.
16f4b 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  ** the available
16f4c 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c   payload..*/.SQL
16f4d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16f4e 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
16f4f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
16f50 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
16f51 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66   amt, void *pBuf
16f52 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69  ){.  int rc;..#i
16f53 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16f54 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20  T_INCRBLOB.  if 
16f55 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16f56 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
16f57 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
16f58 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23  ITE_ABORT;.  }.#
16f59 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
16f5a 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16f5b 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20  x(pCur) );.  rc 
16f5c 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
16f5d 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20  osition(pCur);. 
16f5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16f5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OK ){.    assert
16f60 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16f61 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16f62 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
16f63 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43  ->iPage>=0 && pC
16f64 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16f65 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61  >iPage] );.    a
16f66 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
16f67 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c  dx[pCur->iPage]<
16f68 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16f69 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
16f6a 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63   );.    rc = acc
16f6b 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c  essPayload(pCur,
16f6c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42   offset, amt, pB
16f6d 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  uf, 0);.  }.  re
16f6e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
16f6f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
16f70 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e  er to payload in
16f71 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
16f72 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
16f73 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f  e .** pCur curso
16f74 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
16f75 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  .  The pointer i
16f76 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
16f77 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79  ng of.** the key
16f78 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61   if skipKey==0 a
16f79 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  nd it points to 
16f7a 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
16f7b 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70   data if.** skip
16f7c 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d  Key==1.  The num
16f7d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
16f7e 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61  available key/da
16f7f 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  ta is written.**
16f80 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66   into *pAmt.  If
16f81 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20   *pAmt==0, then 
16f82 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
16f83 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a  ed will not be.*
16f84 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65  * a valid pointe
16f85 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
16f86 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69  utine is an opti
16f87 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73  mization.  It is
16f88 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20   common for the 
16f89 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e  entire key.** an
16f8a 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e  d data to fit on
16f8b 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20   the local page 
16f8c 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f  and for there to
16f8d 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a   be no overflow.
16f8e 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20  ** pages.  When 
16f8f 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73  that is so, this
16f90 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
16f91 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
16f92 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61  he.** key and da
16f93 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e  ta without makin
16f94 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68  g a copy.  If th
16f95 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74  e key and/or dat
16f96 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f  a spills.** onto
16f97 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c   overflow pages,
16f98 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c   then accessPayl
16f99 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73  oad() must be us
16f9a 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65  ed to reassemble
16f9b 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61  .** the key/data
16f9c 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74   and copy it int
16f9d 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64  o a preallocated
16f9e 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   buffer..**.** T
16f9f 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72  he pointer retur
16fa0 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
16fa1 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74  ine looks direct
16fa2 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68  ly into the cach
16fa3 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68  ed.** page of th
16fa4 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
16fa5 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e   data might chan
16fa6 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e  ge or move the n
16fa7 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20  ext time.** any 
16fa8 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73  btree routine is
16fa9 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74   called..*/.stat
16faa 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
16fab 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79  d char *fetchPay
16fac 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72  load(.  BtCursor
16fad 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20   *pCur,      /* 
16fae 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
16faf 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64  to entry to read
16fb0 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a   from */.  int *
16fb1 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  pAmt,           
16fb2 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d  /* Write the num
16fb3 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  ber of available
16fb4 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20   bytes here */. 
16fb5 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20   int skipKey    
16fb6 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65        /* read be
16fb7 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20  ginning at data 
16fb8 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20  if this is true 
16fb9 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
16fba 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b   char *aPayload;
16fbb 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
16fbc 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20  e;.  u32 nKey;. 
16fbd 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20   u32 nLocal;..  
16fbe 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20  assert( pCur!=0 
16fbf 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  && pCur->iPage>=
16fc0 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67  0 && pCur->apPag
16fc1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b  e[pCur->iPage]);
16fc2 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16fc3 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16fc4 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
16fc5 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16fc6 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70  tex(pCur) );.  p
16fc7 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
16fc8 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16fc9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16fca 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16fcb 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  age]<pPage->nCel
16fcc 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  l );.  if( NEVER
16fcd 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a  (pCur->info.nSiz
16fce 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72  e==0) ){.    btr
16fcf 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72  eeParseCell(pCur
16fd0 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16fd1 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49  Page], pCur->aiI
16fd2 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c  dx[pCur->iPage],
16fd3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
16fd4 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29      &pCur->info)
16fd5 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64  ;.  }.  aPayload
16fd6 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43   = pCur->info.pC
16fd7 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20  ell;.  aPayload 
16fd8 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48  += pCur->info.nH
16fd9 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61  eader;.  if( pPa
16fda 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
16fdb 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65    nKey = 0;.  }e
16fdc 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20  lse{.    nKey = 
16fdd 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e  (int)pCur->info.
16fde 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nKey;.  }.  if( 
16fdf 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61  skipKey ){.    a
16fe0 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b  Payload += nKey;
16fe1 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43  .    nLocal = pC
16fe2 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
16fe3 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b  - nKey;.  }else{
16fe4 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43  .    nLocal = pC
16fe5 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b  ur->info.nLocal;
16fe6 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f  .    assert( nLo
16fe7 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d  cal<=nKey );.  }
16fe8 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61  .  *pAmt = nLoca
16fe9 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79  l;.  return aPay
16fea 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  load;.}.../*.** 
16feb 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68  For the entry th
16fec 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 69  at cursor pCur i
16fed 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75  s point to, retu
16fee 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79  rn as.** many by
16fef 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f  tes of the key o
16ff0 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76  r data as are av
16ff1 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c  ailable on the l
16ff2 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70  ocal.** b-tree p
16ff3 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20  age.  Write the 
16ff4 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61  number of availa
16ff5 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a  ble bytes into *
16ff6 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  pAmt..**.** The 
16ff7 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
16ff8 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20   is ephemeral.  
16ff9 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79  The key/data may
16ffa 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64   move.** or be d
16ffb 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20  estroyed on the 
16ffc 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79  next call to any
16ffd 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a   Btree routine,.
16ffe 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c  ** including cal
16fff 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68  ls from other th
17000 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 68  reads against th
17001 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a  e same cache..**
17002 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20   Hence, a mutex 
17003 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20  on the BtShared 
17004 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70  should be held p
17005 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a  rior to calling.
17006 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ** this routine.
17007 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
17008 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f  tines is used to
17009 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 73   get quick acces
1700a 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74  s to key and dat
1700b 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d  a.** in the comm
1700c 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f  on case where no
1700d 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
1700e 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c  are used..*/.SQL
1700f 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
17010 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  t void *sqlite3B
17011 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43  treeKeyFetch(BtC
17012 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
17013 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74   *pAmt){.  const
17014 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20   void *p = 0;.  
17015 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17016 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
17017 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
17018 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
17019 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1701a 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
1701b 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53   ALWAYS(pCur->eS
1701c 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1701d 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28  ID) ){.    p = (
1701e 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63  const void*)fetc
1701f 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70  hPayload(pCur, p
17020 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  Amt, 0);.  }.  r
17021 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54  eturn p;.}.SQLIT
17022 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
17023 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
17024 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75  eeDataFetch(BtCu
17025 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
17026 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20  *pAmt){.  const 
17027 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61  void *p = 0;.  a
17028 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
17029 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e  utex_held(pCur->
1702a 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65  pBtree->db->mute
1702b 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1702c 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1702d 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20  (pCur) );.  if( 
1702e 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74  ALWAYS(pCur->eSt
1702f 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
17030 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63  D) ){.    p = (c
17031 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68  onst void*)fetch
17032 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41  Payload(pCur, pA
17033 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 1);.  }.  re
17034 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
17035 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
17036 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20  r down to a new 
17037 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65  child page.  The
17038 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e   newPgno argumen
17039 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65  t is the.** page
1703a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63   number of the c
1703b 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76  hild page to mov
1703c 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  e to..**.** This
1703d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1703e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  s SQLITE_CORRUPT
1703f 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 61   if the page-hea
17040 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20  der flags field 
17041 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68  of.** the new ch
17042 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f  ild page does no
17043 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67  t match the flag
17044 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70  s field of the p
17045 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69  arent (i.e..** i
17046 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65  f an intkey page
17047 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74   appears to be t
17048 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e  he parent of a n
17049 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20  on-intkey page, 
1704a 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61  or.** vice-versa
1704b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
1704c 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43   moveToChild(BtC
1704d 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32  ursor *pCur, u32
1704e 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74   newPgno){.  int
1704f 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70   rc;.  int i = p
17050 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65  Cur->iPage;.  Me
17051 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b  mPage *pNewPage;
17052 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
17053 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20   = pCur->pBt;.. 
17054 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
17055 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
17056 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
17057 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
17058 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73  R_VALID );.  ass
17059 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65  ert( pCur->iPage
1705a 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45  <BTCURSOR_MAX_DE
1705b 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75  PTH );.  if( pCu
1705c 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52  r->iPage>=(BTCUR
1705d 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29  SOR_MAX_DEPTH-1)
1705e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1705f 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
17060 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67  PT;.  }.  rc = g
17061 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
17062 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65  t, newPgno, &pNe
17063 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  wPage);.  if( rc
17064 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
17065 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31  pCur->apPage[i+1
17066 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20  ] = pNewPage;.  
17067 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d  pCur->aiIdx[i+1]
17068 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50   = 0;.  pCur->iP
17069 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e  age++;..  pCur->
1706a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
1706b 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
1706c 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65  y = 0;.  if( pNe
1706d 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c  wPage->nCell<1 |
1706e 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b  | pNewPage->intK
1706f 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65  ey!=pCur->apPage
17070 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  [i]->intKey ){. 
17071 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17072 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
17073 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
17074 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
17075 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50  f NDEBUG./*.** P
17076 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 61  age pParent is a
17077 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d  n internal (non-
17078 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65 2e  leaf) tree page.
17079 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a   This function .
1707a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 20  ** asserts that 
1707b 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69  page number iChi
1707c 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63  ld is the left-c
1707d 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 78  hild if the iIdx
1707e 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70  'th.** cell in p
1707f 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c  age pParent. Or,
17080 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75 61   if iIdx is equa
17081 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e  l to the total n
17082 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c  umber of.** cell
17083 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68  s in pParent, th
17084 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  at page number i
17085 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 67  Child is the rig
17086 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74  ht-child of.** t
17087 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  he page..*/.stat
17088 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 61  ic void assertPa
17089 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67  rentIndex(MemPag
1708a 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20  e *pParent, int 
1708b 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c  iIdx, Pgno iChil
1708c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49  d){.  assert( iI
1708d 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65  dx<=pParent->nCe
1708e 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78  ll );.  if( iIdx
1708f 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  ==pParent->nCell
17090 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
17091 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e  get4byte(&pParen
17092 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74  t->aData[pParent
17093 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d  ->hdrOffset+8])=
17094 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c  =iChild );.  }el
17095 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
17096 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c  get4byte(findCel
17097 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29  l(pParent, iIdx)
17098 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d  )==iChild );.  }
17099 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69  .}.#else.#  defi
1709a 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 49  ne assertParentI
1709b 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e  ndex(x,y,z) .#en
1709c 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  dif../*.** Move 
1709d 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f  the cursor up to
1709e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1709f 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64  ..**.** pCur->id
170a0 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  x is set to the 
170a1 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20  cell index that 
170a2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69  contains the poi
170a3 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70  nter.** to the p
170a4 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e  age we are comin
170a5 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61  g from.  If we a
170a6 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74  re coming from t
170a7 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74  he.** right-most
170a8 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e   child page then
170a9 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65   pCur->idx is se
170aa 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68  t to one more th
170ab 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73  an.** the larges
170ac 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f  t cell index..*/
170ad 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76  .static void mov
170ae 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73  eToParent(BtCurs
170af 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
170b0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
170b1 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
170b2 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
170b3 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
170b4 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
170b5 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29   pCur->iPage>0 )
170b6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
170b7 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
170b8 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72  Page] );.  asser
170b9 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20  tParentIndex(.  
170ba 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70    pCur->apPage[p
170bb 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a  Cur->iPage-1], .
170bc 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
170bd 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20  pCur->iPage-1], 
170be 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67  .    pCur->apPag
170bf 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
170c0 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65  pgno.  );.  rele
170c1 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70  asePage(pCur->ap
170c2 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
170c3 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67  ]);.  pCur->iPag
170c4 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66  e--;.  pCur->inf
170c5 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
170c6 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
170c7 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76   0;.}../*.** Mov
170c8 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
170c9 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f  point to the roo
170ca 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d  t page of its b-
170cb 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a  tree structure..
170cc 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62  **.** If the tab
170cd 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61 6c  le has a virtual
170ce 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e   root page, then
170cf 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d   the cursor is m
170d0 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a  oved to point.**
170d1 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20   to the virtual 
170d2 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61  root page instea
170d3 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20  d of the actual 
170d4 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62  root page. A tab
170d5 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74  le has a.** virt
170d6 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68  ual root page wh
170d7 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f  en the actual ro
170d8 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73  ot page contains
170d9 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20   no cells and a 
170da 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64  .** single child
170db 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20   page. This can 
170dc 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68  only happen with
170dd 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65   the table roote
170de 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a  d at page 1..**.
170df 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65  ** If the b-tree
170e0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65 6d   structure is em
170e1 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20  pty, the cursor 
170e2 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20  state is set to 
170e3 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  .** CURSOR_INVAL
170e4 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  ID. Otherwise, t
170e5 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 74  he cursor is set
170e6 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
170e7 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c   first.** cell l
170e8 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f  ocated on the ro
170e9 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72  ot (or virtual r
170ea 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 68  oot) page and th
170eb 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a  e cursor state.*
170ec 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53  * is set to CURS
170ed 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20  OR_VALID..**.** 
170ee 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  If this function
170ef 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73   returns success
170f0 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65  fully, it may be
170f1 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
170f2 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72  e.** page-header
170f3 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20   flags indicate 
170f4 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 61  that the [virtua
170f5 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20  l] root-page is 
170f6 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a  the expected .**
170f7 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20   kind of b-tree 
170f8 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68  page (i.e. if wh
170f9 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63  en opening the c
170fa 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72  ursor the caller
170fb 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63   did not.** spec
170fc 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74  ify a KeyInfo st
170fd 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67  ructure the flag
170fe 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f  s byte is set to
170ff 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a   0x05 or 0x0D,.*
17100 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74  * indicating a t
17101 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20  able b-tree, or 
17102 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69  if the caller di
17103 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49  d specify a KeyI
17104 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  nfo .** structur
17105 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65  e the flags byte
17106 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20   is set to 0x02 
17107 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74  or 0x0A, indicat
17108 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20  ing an index.** 
17109 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74  b-tree)..*/.stat
1710a 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f  ic int moveToRoo
1710b 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
1710c 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52  ){.  MemPage *pR
1710d 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  oot;.  int rc = 
1710e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72  SQLITE_OK;.  Btr
1710f 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42  ee *p = pCur->pB
17110 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64  tree;.  BtShared
17111 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
17112 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
17113 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
17114 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  ) );.  assert( C
17115 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20  URSOR_INVALID < 
17116 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
17117 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  EK );.  assert( 
17118 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c  CURSOR_VALID   <
17119 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
1711a 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  EEK );.  assert(
1711b 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20   CURSOR_FAULT   
1711c 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  > CURSOR_REQUIRE
1711d 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43  SEEK );.  if( pC
1711e 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
1711f 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
17120 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  {.    if( pCur->
17121 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46  eState==CURSOR_F
17122 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73  AULT ){.      as
17123 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70  sert( pCur->skip
17124 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  Next!=SQLITE_OK 
17125 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
17126 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a  pCur->skipNext;.
17127 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
17128 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f  3BtreeClearCurso
17129 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20  r(pCur);.  }..  
1712a 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e  if( pCur->iPage>
1712b 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
1712c 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  .    for(i=1; i<
1712d 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b  =pCur->iPage; i+
1712e 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  +){.      releas
1712f 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
17130 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  ge[i]);.    }.  
17131 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
17132 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
17133 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50  rc = getAndInitP
17134 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  age(pBt, pCur->p
17135 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e  gnoRoot, &pCur->
17136 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20  apPage[0]);.    
17137 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17138 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  K ){.      pCur-
17139 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
1713a 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20  _INVALID;.      
1713b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1713c 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65  .    pCur->iPage
1713d 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66   = 0;..    /* If
1713e 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20   pCur->pKeyInfo 
1713f 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
17140 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61  n the caller tha
17141 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75  t opened this cu
17142 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65  rsor.    ** expe
17143 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20  cted to open it 
17144 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72  on an index b-tr
17145 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ee. Otherwise, i
17146 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20  f pKeyInfo is.  
17147 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63    ** NULL, the c
17148 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 20  aller expects a 
17149 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66  table b-tree. If
1714a 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65   this is not the
1714b 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65   case,.    ** re
1714c 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43  turn an SQLITE_C
1714d 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a  ORRUPT error.  *
1714e 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  /.    assert( pC
1714f 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
17150 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72  ntKey==1 || pCur
17151 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
17152 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  Key==0 );.    if
17153 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  ( (pCur->pKeyInf
17154 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50  o==0)!=pCur->apP
17155 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29  age[0]->intKey )
17156 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
17157 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
17158 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  PT;.    }.  }.. 
17159 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
1715a 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73  the root page is
1715b 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20   of the correct 
1715c 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20  type. This must 
1715d 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65  be the.  ** case
1715e 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   as the call to 
1715f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68  this function th
17160 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f  at loaded the ro
17161 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a  ot-page (either.
17162 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f    ** this call o
17163 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76  r a previous inv
17164 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68  ocation) would h
17165 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f 72  ave detected cor
17166 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66  ruption .  ** if
17167 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20   the assumption 
17168 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61  were not true, a
17169 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  nd it is not pos
1716a 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c  sible for the fl
1716b 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74  ags .  ** byte t
1716c 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69  o have been modi
1716d 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 20  fied while this 
1716e 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e  cursor is holdin
1716f 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20  g a reference.  
17170 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20  ** to the page. 
17171 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43   */.  pRoot = pC
17172 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20  ur->apPage[0];. 
17173 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e   assert( pRoot->
17174 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f  pgno==pCur->pgno
17175 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74  Root );.  assert
17176 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20  ( pRoot->isInit 
17177 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  && (pCur->pKeyIn
17178 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69  fo==0)==pRoot->i
17179 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72  ntKey );..  pCur
1717a 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a  ->aiIdx[0] = 0;.
1717b 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
1717c 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ze = 0;.  pCur->
1717d 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43  atLast = 0;.  pC
1717e 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
1717f 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d  0;..  if( pRoot-
17180 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52  >nCell==0 && !pR
17181 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  oot->leaf ){.   
17182 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20   Pgno subpage;. 
17183 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67     if( pRoot->pg
17184 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53  no!=1 ) return S
17185 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
17186 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20  PT;.    subpage 
17187 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f  = get4byte(&pRoo
17188 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e  t->aData[pRoot->
17189 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
1718a 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
1718b 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a  = CURSOR_VALID;.
1718c 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
1718d 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61  hild(pCur, subpa
1718e 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ge);.  }else{.  
1718f 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
17190 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e   ((pRoot->nCell>
17191 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a  0)?CURSOR_VALID:
17192 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b  CURSOR_INVALID);
17193 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
17194 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
17195 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20  the cursor down 
17196 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  to the left-most
17197 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65   leaf entry bene
17198 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79  ath the.** entry
17199 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20   to which it is 
1719a 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
1719b 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65  ng..**.** The le
1719c 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20  ft-most leaf is 
1719d 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65  the one with the
1719e 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20   smallest key - 
1719f 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20  the first.** in 
171a0 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e  ascending order.
171a1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
171a2 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74  oveToLeftmost(Bt
171a3 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
171a4 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e   Pgno pgno;.  in
171a5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
171a6 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
171a7 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  ge;..  assert( c
171a8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
171a9 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
171aa 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
171ab 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
171ac 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
171ad 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61  LITE_OK && !(pPa
171ae 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
171af 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d  e[pCur->iPage])-
171b0 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73  >leaf ){.    ass
171b1 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
171b2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50  [pCur->iPage]<pP
171b3 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
171b4 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74    pgno = get4byt
171b5 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  e(findCell(pPage
171b6 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  , pCur->aiIdx[pC
171b7 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20  ur->iPage]));.  
171b8 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
171b9 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a  ld(pCur, pgno);.
171ba 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
171bb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  .}../*.** Move t
171bc 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74  he cursor down t
171bd 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
171be 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65   leaf entry bene
171bf 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20  ath the.** page 
171c0 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63  to which it is c
171c1 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
171c2 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64  g.  Notice the d
171c3 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74  ifference.** bet
171c4 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  ween moveToLeftm
171c5 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f  ost() and moveTo
171c6 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f  Rightmost().  mo
171c7 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a  veToLeftmost().*
171c8 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74  * finds the left
171c9 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65  -most entry bene
171ca 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20  ath the *entry* 
171cb 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69  whereas moveToRi
171cc 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e  ghtmost().** fin
171cd 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  ds the right-mos
171ce 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20  t entry beneath 
171cf 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a  the *page*..**.*
171d0 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
171d1 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e   entry is the on
171d2 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65  e with the large
171d3 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73  st key - the las
171d4 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65  t.** key in asce
171d5 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a  nding order..*/.
171d6 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54  static int moveT
171d7 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72  oRightmost(BtCur
171d8 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67  sor *pCur){.  Pg
171d9 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  no pgno;.  int r
171da 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
171db 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
171dc 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
171dd 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
171de 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
171df 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
171e0 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
171e1 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
171e2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50  QLITE_OK && !(pP
171e3 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
171e4 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
171e5 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
171e6 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
171e7 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
171e8 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
171e9 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64  ;.    pCur->aiId
171ea 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
171eb 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
171ec 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
171ed 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b  ild(pCur, pgno);
171ee 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
171ef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
171f0 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
171f1 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65  ->iPage] = pPage
171f2 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70  ->nCell-1;.    p
171f3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
171f4 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76  = 0;.    pCur->v
171f5 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20  alidNKey = 0;.  
171f6 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
171f7 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ../* Move the cu
171f8 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73  rsor to the firs
171f9 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
171fa 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  able.  Return SQ
171fb 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75  LITE_OK.** on su
171fc 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65  ccess.  Set *pRe
171fd 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75  s to 0 if the cu
171fe 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f  rsor actually po
171ff 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e  ints to somethin
17200 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65  g.** or set *pRe
17201 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61  s to 1 if the ta
17202 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f  ble is empty..*/
17203 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17204 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
17205 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a  First(BtCursor *
17206 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29  pCur, int *pRes)
17207 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
17208 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
17209 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1720a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1720b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1720c 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  ur->pBtree->db->
1720d 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
1720e 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
1720f 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
17210 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
17211 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
17212 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
17213 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
17214 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
17215 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
17216 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  ==0 );.      *pR
17217 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63  es = 1;.      rc
17218 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
17219 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
1721a 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
1721b 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1721c 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20  ->nCell>0 );.   
1721d 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
1721e 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c      rc = moveToL
1721f 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  eftmost(pCur);. 
17220 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
17221 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65  n rc;.}../* Move
17222 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
17223 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
17224 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74   the table.  Ret
17225 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  urn SQLITE_OK.**
17226 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65   on success.  Se
17227 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20  t *pRes to 0 if 
17228 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61  the cursor actua
17229 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f  lly points to so
1722a 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65  mething.** or se
1722b 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20  t *pRes to 1 if 
1722c 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1722d 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ty..*/.SQLITE_PR
1722e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1722f 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72  3BtreeLast(BtCur
17230 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
17231 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
17232 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  . .  assert( cur
17233 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
17234 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
17235 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
17236 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
17237 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
17238 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72  .  /* If the cur
17239 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e  sor already poin
1723a 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65  ts to the last e
1723b 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 20  ntry, this is a 
1723c 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20  no-op. */.  if( 
1723d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43  CURSOR_VALID==pC
1723e 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43  ur->eState && pC
1723f 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69  ur->atLast ){.#i
17240 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
17241 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c  G.    /* This bl
17242 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73  ock serves to as
17243 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20  sert() that the 
17244 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f  cursor really do
17245 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a  es point .    **
17246 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
17247 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65  ry in the b-tree
17248 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b  . */.    int ii;
17249 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  .    for(ii=0; i
1724a 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69  i<pCur->iPage; i
1724b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  i++){.      asse
1724c 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
1724d 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67  ii]==pCur->apPag
1724e 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  e[ii]->nCell );.
1724f 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
17250 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
17251 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72  ur->iPage]==pCur
17252 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
17253 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29  Page]->nCell-1 )
17254 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
17255 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
17256 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b  >iPage]->leaf );
17257 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75  .#endif.    retu
17258 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
17259 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  }..  rc = moveTo
1725a 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66  Root(pCur);.  if
1725b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1725c 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f  ){.    if( CURSO
1725d 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d  R_INVALID==pCur-
1725e 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20  >eState ){.     
1725f 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
17260 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
17261 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  e]->nCell==0 );.
17262 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
17263 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
17264 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
17265 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
17266 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70  ALID );.      *p
17267 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  Res = 0;.      r
17268 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d  c = moveToRightm
17269 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20  ost(pCur);.     
1726a 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20   pCur->atLast = 
1726b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31  rc==SQLITE_OK ?1
1726c 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  :0;.    }.  }.  
1726d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1726e 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
1726f 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
17270 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e  ts to an entry n
17271 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20  ear the key .** 
17272 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64  specified by pId
17273 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20  xKey or intKey. 
17274 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65    Return a succe
17275 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46  ss code..**.** F
17276 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73  or INTKEY tables
17277 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72  , the intKey par
17278 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20  ameter is used. 
17279 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73   pIdxKey .** mus
1727a 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20  t be NULL.  For 
1727b 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49  index tables, pI
1727c 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e  dxKey is used an
1727d 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69  d intKey.** is i
1727e 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  gnored..**.** If
1727f 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20   an exact match 
17280 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68  is not found, th
17281 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  en the cursor is
17282 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20   always.** left 
17283 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65  pointing at a le
17284 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f  af page which wo
17285 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74  uld hold the ent
17286 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65  ry if it.** were
17287 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63   present.  The c
17288 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e  ursor might poin
17289 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68  t to an entry th
1728a 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f  at comes.** befo
1728b 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20  re or after the 
1728c 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  key..**.** An in
1728d 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e  teger is written
1728e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63   into *pRes whic
1728f 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20  h is the result 
17290 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20  of.** comparing 
17291 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65  the key with the
17292 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20   entry to which 
17293 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a  the cursor is .*
17294 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65  * pointing.  The
17295 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20   meaning of the 
17296 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20  integer written 
17297 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73  into.** *pRes is
17298 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
17299 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20  **     *pRes<0  
1729a 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
1729b 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1729c 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
1729d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1729e 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20       is smaller 
1729f 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78  than intKey/pIdx
172a0 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61  Key or if the ta
172a1 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20  ble is empty.** 
172a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
172a3 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20   and the cursor 
172a4 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66  is therefore lef
172a5 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69  t point to nothi
172a6 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70  ng..**.**     *p
172a7 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63  Res==0     The c
172a8 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
172a9 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74  inting at an ent
172aa 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ry that.**      
172ab 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63              exac
172ac 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b  tly matches intK
172ad 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a  ey/pIdxKey..**.*
172ae 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20  *     *pRes>0   
172af 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73     The cursor is
172b0 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
172b1 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a  t an entry that.
172b2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
172b3 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68      is larger th
172b4 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65  an intKey/pIdxKe
172b5 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  y..**.*/.SQLITE_
172b6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
172b7 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
172b8 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73  packed(.  BtCurs
172b9 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20  or *pCur,       
172ba 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
172bb 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a   to be moved */.
172bc 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
172bd 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e   *pIdxKey, /* Un
172be 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
172bf 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79   */.  i64 intKey
172c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
172c1 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20  * The table key 
172c2 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67  */.  int biasRig
172c3 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ht,           /*
172c4 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74   If true, bias t
172c5 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65  he search to the
172c6 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69   high end */.  i
172c7 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20  nt *pRes        
172c8 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
172c9 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20   search results 
172ca 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
172cb 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
172cc 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
172cd 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
172ce 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
172cf 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
172d0 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
172d1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65  );.  assert( pRe
172d2 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  s );.  assert( (
172d3 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43  pIdxKey==0)==(pC
172d4 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29  ur->pKeyInfo==0)
172d5 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
172d6 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61   cursor is alrea
172d7 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74  dy positioned at
172d8 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72   the point we ar
172d9 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f  e trying.  ** to
172da 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a   move to, then j
172db 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f  ust return witho
172dc 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72  ut doing any wor
172dd 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d  k */.  if( pCur-
172de 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
172df 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76  VALID && pCur->v
172e0 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20  alidNKey .   && 
172e1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
172e2 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20  >intKey .  ){.  
172e3 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f    if( pCur->info
172e4 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b  .nKey==intKey ){
172e5 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30  .      *pRes = 0
172e6 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
172e7 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
172e8 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74      if( pCur->at
172e9 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e  Last && pCur->in
172ea 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29  fo.nKey<intKey )
172eb 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  {.      *pRes = 
172ec 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  -1;.      return
172ed 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
172ee 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f  }.  }..  rc = mo
172ef 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a  veToRoot(pCur);.
172f0 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
172f1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
172f2 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
172f3 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
172f4 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e] );.  assert( 
172f5 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
172f6 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69  r->iPage]->isIni
172f7 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
172f8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
172f9 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e  ->iPage]->nCell>
172fa 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74  0 || pCur->eStat
172fb 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
172fc 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  D );.  if( pCur-
172fd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
172fe 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a  INVALID ){.    *
172ff 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61  pRes = -1;.    a
17300 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
17301 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17302 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
17303 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17304 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  OK;.  }.  assert
17305 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  ( pCur->apPage[0
17306 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64  ]->intKey || pId
17307 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b  xKey );.  for(;;
17308 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20  ){.    int lwr, 
17309 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68  upr;.    Pgno ch
1730a 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67  ldPg;.    MemPag
1730b 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
1730c 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1730d 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b  age];.    int c;
1730e 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e  ..    /* pPage->
1730f 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72  nCell must be gr
17310 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e  eater than zero.
17311 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
17312 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a  root-page.    **
17313 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c   the cursor woul
17314 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41  d have been INVA
17315 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68  LID above and th
17316 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a  is for(;;) loop.
17317 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20      ** not run. 
17318 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74  If this is not t
17319 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68  he root-page, th
1731a 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69  en the moveToChi
1731b 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20  ld() routine.   
1731c 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61   ** would have a
1731d 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64 20  lready detected 
1731e 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53  db corruption. S
1731f 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20  imilarly, pPage 
17320 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74  must.    ** be t
17321 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69  he right kind (i
17322 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f  ndex or table) o
17323 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f  f b-tree page. O
17324 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20  therwise.    ** 
17325 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20  a moveToChild() 
17326 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20  or moveToRoot() 
17327 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20  call would have 
17328 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74  detected corrupt
17329 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73  ion.  */.    ass
1732a 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ert( pPage->nCel
1732b 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  l>0 );.    asser
1732c 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  t( pPage->intKey
1732d 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29  ==(pIdxKey==0) )
1732e 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20  ;.    lwr = 0;. 
1732f 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e     upr = pPage->
17330 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28  nCell-1;.    if(
17331 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20   biasRight ){.  
17332 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
17333 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28  pCur->iPage] = (
17334 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c  u16)upr;.    }el
17335 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  se{.      pCur->
17336 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
17337 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b  e] = (u16)((upr+
17338 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20  lwr)/2);.    }. 
17339 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20     for(;;){.    
1733a 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72    int idx = pCur
1733b 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
1733c 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f  age]; /* Index o
1733d 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69  f current cell i
1733e 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20  n pPage */.     
1733f 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20   u8 *pCell;     
17340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17341 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
17342 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20  to current cell 
17343 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20  in pPage */..   
17344 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53     pCur->info.nS
17345 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ize = 0;.      p
17346 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
17347 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50  pPage, idx) + pP
17348 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
17349 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  e;.      if( pPa
1734a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
1734b 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b        i64 nCellK
1734c 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ey;.        if( 
1734d 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
1734e 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20  {.          u32 
1734f 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20  dummy;.         
17350 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72   pCell += getVar
17351 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d  int32(pCell, dum
17352 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  my);.        }. 
17353 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74         getVarint
17354 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e  (pCell, (u64*)&n
17355 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20  CellKey);.      
17356 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d    if( nCellKey==
17357 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
17358 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20      c = 0;.     
17359 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65     }else if( nCe
1735a 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a  llKey<intKey ){.
1735b 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31            c = -1
1735c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1735d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1735e 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b  t( nCellKey>intK
1735f 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ey );.          
17360 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20  c = +1;.        
17361 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  }.        pCur->
17362 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20  validNKey = 1;. 
17363 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66         pCur->inf
17364 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65  o.nKey = nCellKe
17365 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  y;.      }else{.
17366 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
17367 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64  aximum supported
17368 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32   page-size is 32
17369 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 20  768 bytes. This 
1736a 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20  means that.     
1736b 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75     ** the maximu
1736c 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f  m number of reco
1736d 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 20  rd bytes stored 
1736e 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72  on an index B-Tr
1736f 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61  ee.        ** pa
17370 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31  ge is at most 81
17371 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 20  98 bytes, which 
17372 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73  may be stored as
17373 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20   a 2-byte.      
17374 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69    ** varint. Thi
17375 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  s information is
17376 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74   used to attempt
17377 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e   to avoid parsin
17378 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  g .        ** th
17379 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79  e entire cell by
1737a 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68   checking for th
1737b 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68  e cases where th
1737c 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20  e record is .   
1737d 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65       ** stored e
1737e 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74  ntirely within t
1737f 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 62  he b-tree page b
17380 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65  y inspecting the
17381 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 20   first .        
17382 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 68  ** 2 bytes of th
17383 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20  e cell..        
17384 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  */.        int n
17385 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b  Cell = pCell[0];
17386 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28 6e  .        if( !(n
17387 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20  Cell & 0x80) && 
17388 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61  nCell<=pPage->ma
17389 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  xLocal ){.      
1738a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e      /* This bran
1738b 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72  ch runs if the r
1738c 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64  ecord-size field
1738d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20   of the cell is 
1738e 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73  a.          ** s
1738f 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e  ingle byte varin
17390 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64  t and the record
17391 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f   fits entirely o
17392 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20  n the main.     
17393 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70       ** b-tree p
17394 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  age.  */.       
17395 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64     c = sqlite3Vd
17396 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
17397 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70  nCell, (void*)&p
17398 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79  Cell[1], pIdxKey
17399 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
1739a 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20   if( !(pCell[1] 
1739b 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 20  & 0x80) .       
1739c 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28     && (nCell = (
1739d 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29  (nCell&0x7f)<<7)
1739e 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50   + pCell[1])<=pP
1739f 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20  age->maxLocal.  
173a0 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
173a1 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64     /* The record
173a2 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61  -size field is a
173a3 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 61   2 byte varint a
173a4 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20  nd the record . 
173a5 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73           ** fits
173a6 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65   entirely on the
173a7 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67   main b-tree pag
173a8 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  e.  */.         
173a9 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   c = sqlite3Vdbe
173aa 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43  RecordCompare(nC
173ab 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65  ell, (void*)&pCe
173ac 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b  ll[2], pIdxKey);
173ad 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
173ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
173af 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76   record flows ov
173b0 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d  er onto one or m
173b1 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ore overflow pag
173b2 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20  es. In.         
173b3 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68   ** this case th
173b4 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65  e whole cell nee
173b5 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c  ds to be parsed,
173b6 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61   a buffer alloca
173b7 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ted.          **
173b8 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f   and accessPaylo
173b9 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74  ad() used to ret
173ba 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64  rieve the record
173bb 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20   into the.      
173bc 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65      ** buffer be
173bd 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43  fore VdbeRecordC
173be 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20  ompare() can be 
173bf 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20  called. */.     
173c0 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c       void *pCell
173c1 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75  Key;.          u
173c2 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42  8 * const pCellB
173c3 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50  ody = pCell - pP
173c4 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
173c5 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72  e;.          btr
173c6 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
173c7 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c  Page, pCellBody,
173c8 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20   &pCur->info);. 
173c9 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d           nCell =
173ca 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f   (int)pCur->info
173cb 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20  .nKey;.         
173cc 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69   pCellKey = sqli
173cd 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c  te3Malloc( nCell
173ce 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
173cf 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b  ( pCellKey==0 ){
173d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
173d1 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
173d2 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
173d3 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
173d4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
173d5 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73        rc = acces
173d6 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30  sPayload(pCur, 0
173d7 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e  , nCell, (unsign
173d8 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65  ed char*)pCellKe
173d9 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  y, 0);.         
173da 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
173db 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
173dc 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20  ree(pCellKey);. 
173dd 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
173de 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
173df 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
173e0 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33       c = sqlite3
173e1 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
173e2 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65  e(nCell, pCellKe
173e3 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20  y, pIdxKey);.   
173e4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
173e5 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20  ree(pCellKey);. 
173e6 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
173e7 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
173e8 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
173e9 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20  Page->intKey && 
173ea 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
173eb 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20            lwr = 
173ec 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75  idx;.          u
173ed 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20  pr = lwr - 1;.  
173ee 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
173ef 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
173f0 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20          *pRes = 
173f1 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  0;.          rc 
173f2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
173f3 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65         goto move
173f4 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20  to_finish;.     
173f5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
173f6 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20     if( c<0 ){.  
173f7 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b        lwr = idx+
173f8 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
173f9 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64          upr = id
173fa 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  x-1;.      }.   
173fb 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29     if( lwr>upr )
173fc 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
173fd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
173fe 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
173ff 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28  >iPage] = (u16)(
17400 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20  (lwr+upr)/2);.  
17401 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
17402 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20  lwr==upr+1 );.  
17403 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17404 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69  >isInit );.    i
17405 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
17406 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d  {.      chldPg =
17407 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
17408 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43  ( lwr>=pPage->nC
17409 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c  ell ){.      chl
1740a 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26  dPg = get4byte(&
1740b 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
1740c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
1740d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1740e 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74      chldPg = get
1740f 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
17410 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20  Page, lwr));.   
17411 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50   }.    if( chldP
17412 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  g==0 ){.      as
17413 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
17414 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
17415 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
17416 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
17417 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  );.      *pRes =
17418 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   c;.      rc = S
17419 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1741a 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
1741b 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  sh;.    }.    pC
1741c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1741d 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77  iPage] = (u16)lw
1741e 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66  r;.    pCur->inf
1741f 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  o.nSize = 0;.   
17420 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
17421 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d   = 0;.    rc = m
17422 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
17423 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66   chldPg);.    if
17424 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65  ( rc ) goto move
17425 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d  to_finish;.  }.m
17426 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20  oveto_finish:.  
17427 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
17428 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
17429 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69   if the cursor i
1742a 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61  s not pointing a
1742b 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  t an entry of th
1742c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
1742d 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75  RUE will be retu
1742e 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c  rned after a cal
1742f 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
17430 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a  eNext() moves.**
17431 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65   past the last e
17432 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c  ntry in the tabl
17433 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65  e or sqlite3Btre
17434 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61  ePrev() moves pa
17435 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  st.** the first 
17436 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20  entry.  TRUE is 
17437 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66  also returned if
17438 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
17439 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pty..*/.SQLITE_P
1743a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1743b 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72  e3BtreeEof(BtCur
1743c 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a  sor *pCur){.  /*
1743d 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74   TODO: What if t
1743e 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20  he cursor is in 
1743f 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
17440 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65  EK but all table
17441 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61   entries.  ** ha
17442 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f  ve been deleted?
17443 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e   This API will n
17444 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f  eed to change to
17445 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
17446 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65   code.  ** as we
17447 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61  ll as the boolea
17448 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a  n result value..
17449 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43    */.  return (C
1744a 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75  URSOR_VALID!=pCu
1744b 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f  r->eState);.}../
1744c 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65  *.** Advance the
1744d 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e   cursor to the n
1744e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ext entry in the
1744f 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a   database.  If.*
17450 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65  * successful the
17451 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20  n set *pRes=0.  
17452 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  If the cursor.**
17453 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69   was already poi
17454 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73  nting to the las
17455 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  t entry in the d
17456 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a  atabase before.*
17457 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * this routine w
17458 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  as called, then 
17459 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a  set *pRes=1..*/.
1745a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1745b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e  nt sqlite3BtreeN
1745c 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ext(BtCursor *pC
1745d 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a  ur, int *pRes){.
1745e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
1745f 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  idx;.  MemPage *
17460 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74  pPage;..  assert
17461 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
17462 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
17463 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
17464 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
17465 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
17466 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
17467 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65  n rc;.  }.  asse
17468 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20  rt( pRes!=0 );. 
17469 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41   if( CURSOR_INVA
1746a 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74  LID==pCur->eStat
1746b 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d  e ){.    *pRes =
1746c 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   1;.    return S
1746d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1746e 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  if( pCur->skipNe
1746f 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72  xt>0 ){.    pCur
17470 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a  ->skipNext = 0;.
17471 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
17472 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17473 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d  _OK;.  }.  pCur-
17474 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a  >skipNext = 0;..
17475 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
17476 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17477 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70  ge];.  idx = ++p
17478 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
17479 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72  >iPage];.  asser
1747a 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
1747b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64   );.  assert( id
1747c 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  x<=pPage->nCell 
1747d 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f  );..  pCur->info
1747e 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43  .nSize = 0;.  pC
1747f 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
17480 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50  0;.  if( idx>=pP
17481 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  age->nCell ){.  
17482 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
17483 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  af ){.      rc =
17484 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
17485 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61  r, get4byte(&pPa
17486 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
17487 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b  >hdrOffset+8]));
17488 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
17489 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1748a 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74   rc = moveToLeft
1748b 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
1748c 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
1748d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1748e 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20    }.    do{.    
1748f 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
17490 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
17491 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20  *pRes = 1;.     
17492 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
17493 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
17494 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
17495 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
17496 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f    }.      moveTo
17497 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20  Parent(pCur);.  
17498 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72      pPage = pCur
17499 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1749a 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c  Page];.    }whil
1749b 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  e( pCur->aiIdx[p
1749c 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61  Cur->iPage]>=pPa
1749d 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  ge->nCell );.   
1749e 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
1749f 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
174a0 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
174a1 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
174a2 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20  (pCur, pRes);.  
174a3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
174a4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
174a5 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
174a6 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20  rc;.  }.  *pRes 
174a7 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65  = 0;.  if( pPage
174a8 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65  ->leaf ){.    re
174a9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
174aa 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54    }.  rc = moveT
174ab 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b  oLeftmost(pCur);
174ac 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
174ad 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65  ../*.** Step the
174ae 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62   cursor to the b
174af 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69  ack to the previ
174b0 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ous entry in the
174b1 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a   database.  If.*
174b2 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65  * successful the
174b3 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20  n set *pRes=0.  
174b4 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  If the cursor.**
174b5 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69   was already poi
174b6 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72  nting to the fir
174b7 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
174b8 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
174b9 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
174ba 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e  was called, then
174bb 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f   set *pRes=1..*/
174bc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
174bd 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
174be 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f  Previous(BtCurso
174bf 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52  r *pCur, int *pR
174c0 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  es){.  int rc;. 
174c1 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
174c2 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
174c3 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
174c4 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73  r) );.  rc = res
174c5 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
174c6 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  on(pCur);.  if( 
174c7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
174c8 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
174c9 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61    }.  pCur->atLa
174ca 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55  st = 0;.  if( CU
174cb 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43  RSOR_INVALID==pC
174cc 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20  ur->eState ){.  
174cd 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
174ce 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
174cf 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75  K;.  }.  if( pCu
174d0 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b  r->skipNext<0 ){
174d1 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e  .    pCur->skipN
174d2 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52  ext = 0;.    *pR
174d3 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  es = 0;.    retu
174d4 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
174d5 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  }.  pCur->skipNe
174d6 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65  xt = 0;..  pPage
174d7 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
174d8 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
174d9 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
174da 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21  sInit );.  if( !
174db 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
174dc 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
174dd 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
174de 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20  Page];.    rc = 
174df 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
174e0 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  , get4byte(findC
174e1 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29  ell(pPage, idx))
174e2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
174e3 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
174e4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
174e5 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
174e6 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pCur);.  }else{
174e7 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72  .    while( pCur
174e8 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
174e9 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  age]==0 ){.     
174ea 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
174eb 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
174ec 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55  Cur->eState = CU
174ed 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20  RSOR_INVALID;.  
174ee 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
174ef 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
174f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
174f1 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50   }.      moveToP
174f2 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
174f3 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66   }.    pCur->inf
174f4 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  o.nSize = 0;.   
174f5 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
174f6 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d   = 0;..    pCur-
174f7 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
174f8 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65  ge]--;.    pPage
174f9 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
174fa 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
174fb 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
174fc 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c  Key && !pPage->l
174fd 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eaf ){.      rc 
174fe 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72  = sqlite3BtreePr
174ff 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65  evious(pCur, pRe
17500 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  s);.    }else{. 
17501 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
17502 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
17503 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65   *pRes = 0;.  re
17504 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
17505 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
17506 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64   page from the d
17507 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
17508 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65  .** The new page
17509 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69   is marked as di
1750a 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20  rty.  (In other 
1750b 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61  words, sqlite3Pa
1750c 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61  gerWrite().** ha
1750d 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
1750e 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77  alled on the new
1750f 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77   page.)  The new
17510 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a   page has also.*
17511 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65  * been reference
17512 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e  d and the callin
17513 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73  g routine is res
17514 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c  ponsible for cal
17515 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50  ling.** sqlite3P
17516 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74  agerUnref() on t
17517 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e  he new page when
17518 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a   it is done..**.
17519 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
1751a 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
1751b 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20  ess.  Any other 
1751c 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64  return value ind
1751d 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72  icates.** an err
1751e 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64  or.  *ppPage and
1751f 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65   *pPgno are unde
17520 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65  fined in the eve
17521 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a  nt of an error..
17522 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65  ** Do not invoke
17523 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
17524 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20  ef() on *ppPage 
17525 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  if an error is r
17526 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
17527 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70  f the "nearby" p
17528 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20  arameter is not 
17529 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c  0, then a (feebl
1752a 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64  e) effort is mad
1752b 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20  e to .** locate 
1752c 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20  a page close to 
1752d 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
1752e 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 20  "nearby".  This 
1752f 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
17530 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20  n.** attempt to 
17531 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 67  keep related pag
17532 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68  es close to each
17533 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 61   other in the da
17534 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20  tabase file,.** 
17535 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61  which in turn ca
17536 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20  n make database 
17537 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a  access faster..*
17538 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61  *.** If the "exa
17539 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  ct" parameter is
1753a 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20   not 0, and the 
1753b 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72  page-number near
1753c 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e  by exists .** an
1753d 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72  ywhere on the fr
1753e 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74  ee-list, then it
1753f 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 74   is guarenteed t
17540 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54  o be returned. T
17541 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75  his.** is only u
17542 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75  sed by auto-vacu
17543 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 65  um databases whe
17544 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e  n allocating a n
17545 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ew table..*/.sta
17546 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
17547 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53  BtreePage(.  BtS
17548 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d  hared *pBt, .  M
17549 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
1754a 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c   .  Pgno *pPgno,
1754b 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c   .  Pgno nearby,
1754c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20  .  u8 exact.){. 
1754d 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31   MemPage *pPage1
1754e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33  ;.  int rc;.  u3
1754f 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  2 n;     /* Numb
17550 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74  er of pages on t
17551 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20  he freelist */. 
17552 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e   u32 k;     /* N
17553 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20  umber of leaves 
17554 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20  on the trunk of 
17555 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a  the freelist */.
17556 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e    MemPage *pTrun
17557 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65  k = 0;.  MemPage
17558 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30   *pPrevTrunk = 0
17559 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b  ;.  Pgno mxPage;
1755a 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69       /* Total si
1755b 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
1755c 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73  se file */..  as
1755d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1755e 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1755f 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31  tex) );.  pPage1
17560 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a   = pBt->pPage1;.
17561 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72    mxPage = pager
17562 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
17563 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26    n = get4byte(&
17564 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
17565 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  ]);.  testcase( 
17566 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20  n==mxPage-1 );. 
17567 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29   if( n>=mxPage )
17568 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
17569 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1756a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20  ;.  }.  if( n>0 
1756b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  ){.    /* There 
1756c 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65  are pages on the
1756d 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73   freelist.  Reus
1756e 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70  e one of those p
1756f 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e  ages. */.    Pgn
17570 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38  o iTrunk;.    u8
17571 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b   searchList = 0;
17572 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d   /* If the free-
17573 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61  list must be sea
17574 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62  rched for 'nearb
17575 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f  y' */.    .    /
17576 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27  * If the 'exact'
17577 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74   parameter was t
17578 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20  rue and a query 
17579 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
1757a 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20  ap.    ** shows 
1757b 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e  that the page 'n
1757c 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68  earby' is somewh
1757d 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d  ere on the free-
1757e 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a  list, then.    *
1757f 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73  * the entire-lis
17580 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68  t will be search
17581 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65  ed for that page
17582 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
17583 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
17584 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
17585 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c  exact && nearby<
17586 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20  =mxPage ){.     
17587 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20   u8 eType;.     
17588 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e   assert( nearby>
17589 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
1758a 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  t( pBt->autoVacu
1758b 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  um );.      rc =
1758c 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
1758d 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20  nearby, &eType, 
1758e 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
1758f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
17590 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
17591 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
17592 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68  {.        search
17593 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  List = 1;.      
17594 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d  }.      *pPgno =
17595 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23   nearby;.    }.#
17596 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65  endif..    /* De
17597 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65  crement the free
17598 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31  -list count by 1
17599 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20  . Set iTrunk to 
1759a 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
1759b 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72  .    ** first fr
1759c 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61  ee-list trunk pa
1759d 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69  ge. iPrevTrunk i
1759e 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20  s initially 1.. 
1759f 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
175a0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
175a1 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  (pPage1->pDbPage
175a2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
175a3 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70  return rc;.    p
175a4 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  ut4byte(&pPage1-
175a5 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29  >aData[36], n-1)
175a6 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  ;..    /* The co
175a7 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c  de within this l
175a8 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20  oop is run only 
175a9 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 61  once if the 'sea
175aa 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c  rchList' variabl
175ab 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20  e.    ** is not 
175ac 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  true. Otherwise,
175ad 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f   it runs once fo
175ae 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67  r each trunk-pag
175af 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  e on the.    ** 
175b0 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20  free-list until 
175b1 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79  the page 'nearby
175b2 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20  ' is located..  
175b3 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20    */.    do {.  
175b4 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d      pPrevTrunk =
175b5 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69   pTrunk;.      i
175b6 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b  f( pPrevTrunk ){
175b7 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20  .        iTrunk 
175b8 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72 65  = get4byte(&pPre
175b9 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  vTrunk->aData[0]
175ba 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
175bb 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d          iTrunk =
175bc 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
175bd 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20  1->aData[32]);. 
175be 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73       }.      tes
175bf 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d  tcase( iTrunk==m
175c0 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69  xPage );.      i
175c1 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65  f( iTrunk>mxPage
175c2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
175c3 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
175c4 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73  BKPT;.      }els
175c5 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
175c6 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
175c7 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e  , iTrunk, &pTrun
175c8 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  k, 0);.      }. 
175c9 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
175ca 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20         pTrunk = 
175cb 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  0;.        goto 
175cc 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
175cd 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  e;.      }..    
175ce 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26    k = get4byte(&
175cf 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d  pTrunk->aData[4]
175d0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d  );.      if( k==
175d1 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74  0 && !searchList
175d2 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
175d3 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20  he trunk has no 
175d4 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c  leaves and the l
175d5 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67  ist is not being
175d6 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20   searched. .    
175d7 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63      ** So extrac
175d8 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65  t the trunk page
175d9 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20   itself and use 
175da 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20  it as the newly 
175db 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f  .        ** allo
175dc 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20  cated page */.  
175dd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
175de 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20  revTrunk==0 );. 
175df 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
175e0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54  te3PagerWrite(pT
175e1 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
175e2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
175e3 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
175e4 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
175e5 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ge;.        }.  
175e6 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69        *pPgno = i
175e7 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d  Trunk;.        m
175e8 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61  emcpy(&pPage1->a
175e9 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e  Data[32], &pTrun
175ea 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b  k->aData[0], 4);
175eb 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65  .        *ppPage
175ec 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20   = pTrunk;.     
175ed 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20     pTrunk = 0;. 
175ee 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41         TRACE(("A
175ef 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e  LLOCATE: %d trun
175f0 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65  k - %d free page
175f1 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e  s left\n", *pPgn
175f2 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20  o, n-1));.      
175f3 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32  }else if( k>(u32
175f4 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  )(pBt->usableSiz
175f5 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20  e/4 - 2) ){.    
175f6 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20      /* Value of 
175f7 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  k is out of rang
175f8 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72  e.  Database cor
175f9 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ruption */.     
175fa 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
175fb 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
175fc 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
175fd 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66  locate_page;.#if
175fe 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
175ff 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
17600 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72    }else if( sear
17601 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79  chList && nearby
17602 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20  ==iTrunk ){.    
17603 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20      /* The list 
17604 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65  is being searche
17605 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b  d and this trunk
17606 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67   page is the pag
17607 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  e.        ** to 
17608 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64  allocate, regard
17609 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
1760a 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20  it has leaves.. 
1760b 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1760c 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
1760d 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20  ==iTrunk );.    
1760e 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54      *ppPage = pT
1760f 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65  runk;.        se
17610 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20  archList = 0;.  
17611 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17612 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
17613 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
17614 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
17615 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
17616 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
17617 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
17618 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b       if( k==0 ){
17619 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
1761a 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20  pPrevTrunk ){.  
1761b 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
1761c 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
1761d 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  32], &pTrunk->aD
1761e 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20  ata[0], 4);.    
1761f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
17620 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
17621 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61  &pPrevTrunk->aDa
17622 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  ta[0], &pTrunk->
17623 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
17624 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17625 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17626 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20     /* The trunk 
17627 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64  page is required
17628 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62   by the caller b
17629 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a  ut it contains .
1762a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69            ** poi
1762b 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69  nters to free-li
1762c 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66  st leaves. The f
1762d 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65  irst leaf become
1762e 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20  s a trunk.      
1762f 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74      ** page in t
17630 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20  his case..      
17631 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
17632 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72   MemPage *pNewTr
17633 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50  unk;.          P
17634 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20  gno iNewTrunk = 
17635 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b  get4byte(&pTrunk
17636 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20  ->aData[8]);.   
17637 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54         if( iNewT
17638 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a  runk>mxPage ){ .
17639 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
1763a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1763b 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20  BKPT;.          
1763c 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63    goto end_alloc
1763d 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
1763e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1763f 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72  testcase( iNewTr
17640 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20  unk==mxPage );. 
17641 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74           rc = bt
17642 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
17643 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77  iNewTrunk, &pNew
17644 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20  Trunk, 0);.     
17645 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
17646 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17647 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
17648 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
17649 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1764a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1764b 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54  PagerWrite(pNewT
1764c 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
1764d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
1764e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1764f 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61             relea
17650 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b  sePage(pNewTrunk
17651 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  );.            g
17652 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
17653 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
17654 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d   }.          mem
17655 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e  cpy(&pNewTrunk->
17656 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e  aData[0], &pTrun
17657 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b  k->aData[0], 4);
17658 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62  .          put4b
17659 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e  yte(&pNewTrunk->
1765a 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a  aData[4], k-1);.
1765b 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
1765c 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61  (&pNewTrunk->aDa
1765d 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  ta[8], &pTrunk->
1765e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29  aData[12], (k-1)
1765f 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  *4);.          r
17660 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54  eleasePage(pNewT
17661 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20  runk);.         
17662 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b   if( !pPrevTrunk
17663 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
17664 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
17665 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
17666 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
17667 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
17668 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
17669 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65  ->aData[32], iNe
1766a 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20  wTrunk);.       
1766b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1766c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1766d 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72  e3PagerWrite(pPr
1766e 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65  evTrunk->pDbPage
1766f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
17670 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
17671 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
17672 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
17673 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
17674 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
17675 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61  e(&pPrevTrunk->a
17676 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75  Data[0], iNewTru
17677 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nk);.          }
17678 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
17679 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20     pTrunk = 0;. 
1767a 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41         TRACE(("A
1767b 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e  LLOCATE: %d trun
1767c 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65  k - %d free page
1767d 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e  s left\n", *pPgn
1767e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66  o, n-1));.#endif
1767f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
17680 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   k>0 ){.        
17681 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61  /* Extract a lea
17682 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b  f from the trunk
17683 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20   */.        u32 
17684 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20  closest;.       
17685 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20   Pgno iPage;.   
17686 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
17687 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75  ar *aData = pTru
17688 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20  nk->aData;.     
17689 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1768a 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b  agerWrite(pTrunk
1768b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1768c 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
1768d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
1768e 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
1768f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17690 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29    if( nearby>0 )
17691 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20  {.          u32 
17692 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  i;.          int
17693 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20   dist;.         
17694 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20   closest = 0;.  
17695 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67          dist = g
17696 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38  et4byte(&aData[8
17697 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20  ]) - nearby;.   
17698 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c         if( dist<
17699 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74  0 ) dist = -dist
1769a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
1769b 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a  i=1; i<k; i++){.
1769c 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
1769d 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61  d2 = get4byte(&a
1769e 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e  Data[8+i*4]) - n
1769f 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20  earby;.         
176a0 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32     if( d2<0 ) d2
176a1 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20   = -d2;.        
176a2 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20      if( d2<dist 
176a3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
176a4 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20   closest = i;.  
176a5 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74              dist
176a6 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20   = d2;.         
176a7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
176a8 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
176a9 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
176aa 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
176ab 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20  ..        iPage 
176ac 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74  = get4byte(&aDat
176ad 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b  a[8+closest*4]);
176ae 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
176af 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65  e( iPage==mxPage
176b0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
176b1 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a  iPage>mxPage ){.
176b2 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
176b3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
176b4 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
176b5 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
176b6 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  page;.        }.
176b7 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
176b8 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20  ( iPage==mxPage 
176b9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
176ba 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50  searchList || iP
176bb 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20  age==nearby ){. 
176bc 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43           int noC
176bd 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20  ontent;.        
176be 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65    *pPgno = iPage
176bf 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43  ;.          TRAC
176c0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64  E(("ALLOCATE: %d
176c1 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20   was leaf %d of 
176c2 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a  %d on trunk %d".
176c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176c4 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65   ": %d more free
176c5 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20   pages\n",.     
176c6 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67              *pPg
176c7 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b  no, closest+1, k
176c8 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20  , pTrunk->pgno, 
176c9 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20  n-1));.         
176ca 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31   if( closest<k-1
176cb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
176cc 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b  memcpy(&aData[8+
176cd 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61  closest*4], &aDa
176ce 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20  ta[4+k*4], 4);. 
176cf 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
176d0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 61       put4byte(&a
176d1 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20  Data[4], k-1);. 
176d2 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
176d3 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
176d4 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d  riteable(pTrunk-
176d5 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
176d6 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74         noContent
176d7 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43   = !btreeGetHasC
176d8 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67  ontent(pBt, *pPg
176d9 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  no);.          r
176da 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
176db 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70  (pBt, *pPgno, pp
176dc 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29  Page, noContent)
176dd 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
176de 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
176df 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
176e0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
176e1 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70  ite((*ppPage)->p
176e2 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
176e3 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
176e4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
176e5 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
176e6 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20  age(*ppPage);.  
176e7 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
176e8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
176e9 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30    searchList = 0
176ea 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
176eb 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73    }.      releas
176ec 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b  ePage(pPrevTrunk
176ed 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72  );.      pPrevTr
176ee 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68  unk = 0;.    }wh
176ef 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20  ile( searchList 
176f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
176f1 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20  /* There are no 
176f2 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65  pages on the fre
176f3 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65  elist, so create
176f4 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74   a new page at t
176f5 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66  he.    ** end of
176f6 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20   the file */.   
176f7 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67   int nPage = pag
176f8 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
176f9 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e  ;.    *pPgno = n
176fa 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69  Page + 1;..    i
176fb 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49  f( *pPgno==PENDI
176fc 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
176fd 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67  ) ){.      (*pPg
176fe 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69  no)++;.    }..#i
176ff 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17700 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
17701 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
17702 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49  cuum && PTRMAP_I
17703 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e  SPAGE(pBt, *pPgn
17704 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  o) ){.      /* I
17705 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20  f *pPgno refers 
17706 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  to a pointer-map
17707 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20   page, allocate 
17708 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20  two new pages.  
17709 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e      ** at the en
1770a 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e  d of the file in
1770b 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68  stead of one. Th
1770c 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65  e first allocate
1770d 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  d page.      ** 
1770e 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f  becomes a new po
1770f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20  inter-map page, 
17710 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73  the second is us
17711 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
17712 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
17713 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20   MemPage *pPg = 
17714 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  0;.      TRACE((
17715 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72  "ALLOCATE: %d fr
17716 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28  om end of file (
17717 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
17718 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a  )\n", *pPgno));.
17719 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70        assert( *p
1771a 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
1771b 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
1771c 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65        rc = btree
1771d 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50  GetPage(pBt, *pP
1771e 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20  gno, &pPg, 0);. 
1771f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
17720 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17721 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17722 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44  gerWrite(pPg->pD
17723 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
17724 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29  releasePage(pPg)
17725 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17726 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
17727 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e  rc;.      (*pPgn
17728 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  o)++;.      if( 
17729 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f  *pPgno==PENDING_
1772a 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1772b 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a  { (*pPgno)++; }.
1772c 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1772d 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
1772e 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
1772f 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20  AGE(pBt) );.    
17730 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
17731 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70  e(pBt, *pPgno, p
17732 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69  pPage, 0);.    i
17733 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
17734 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  c;.    rc = sqli
17735 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a  te3PagerWrite((*
17736 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65  ppPage)->pDbPage
17737 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
17738 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17739 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70    releasePage(*p
1773a 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pPage);.    }.  
1773b 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
1773c 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20  TE: %d from end 
1773d 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67  of file\n", *pPg
1773e 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  no));.  }..  ass
1773f 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e  ert( *pPgno!=PEN
17740 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
17741 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f  Bt) );..end_allo
17742 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c  cate_page:.  rel
17743 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29  easePage(pTrunk)
17744 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  ;.  releasePage(
17745 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69  pPrevTrunk);.  i
17746 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
17747 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
17748 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
17749 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e  ount((*ppPage)->
1774a 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20  pDbPage)>1 ){.  
1774b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1774c 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  *ppPage);.      
1774d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1774e 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
1774f 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d  }.    (*ppPage)-
17750 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d  >isInit = 0;.  }
17751 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67  else{.    *ppPag
17752 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
17753 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
17754 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
17755 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61  s used to add pa
17756 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20  ge iPage to the 
17757 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72  database file fr
17758 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20  ee-list. .** It 
17759 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
1775a 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
1775b 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f  already a part o
1775c 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e  f the free-list.
1775d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
1775e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
1775f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
17760 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
17761 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20  is optional..** 
17762 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61  If the caller ha
17763 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20  ppens to have a 
17764 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d  pointer to the M
17765 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a  emPage object .*
17766 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
17767 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61  to page iPage ha
17768 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73  ndy, it may pass
17769 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e   it as the secon
1776a 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68  d value. .** Oth
1776b 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70  erwise, it may p
1776c 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  ass NULL..**.** 
1776d 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  If a pointer to 
1776e 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74  a MemPage object
1776f 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
17770 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
17771 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65  t,.** its refere
17772 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74  nce count is not
17773 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73   altered by this
17774 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
17775 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67  atic int freePag
17776 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  e2(BtShared *pBt
17777 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50  , MemPage *pMemP
17778 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29  age, Pgno iPage)
17779 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72  {.  MemPage *pTr
1777a 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20  unk = 0;        
1777b 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d          /* Free-
1777c 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20  list trunk page 
1777d 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b  */.  Pgno iTrunk
1777e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1777f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
17780 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d   number of free-
17781 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20  list trunk page 
17782 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  */ .  MemPage *p
17783 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61  Page1 = pBt->pPa
17784 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63  ge1;      /* Loc
17785 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  al reference to 
17786 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50  page 1 */.  MemP
17787 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20  age *pPage;     
17788 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17789 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72  /* Page being fr
1778a 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c  eed. May be NULL
1778b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  . */.  int rc;  
1778c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1778d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1778e 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69  turn Code */.  i
1778f 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20  nt nFree;       
17790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17791 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75     /* Initial nu
17792 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e  mber of pages on
17793 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20   free-list */.. 
17794 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17795 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
17796 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
17797 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a  ert( iPage>1 );.
17798 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50    assert( !pMemP
17799 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d  age || pMemPage-
1779a 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a  >pgno==iPage );.
1779b 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20  .  if( pMemPage 
1779c 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70  ){.    pPage = p
1779d 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c  MemPage;.    sql
1779e 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61  ite3PagerRef(pPa
1779f 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
177a0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65  }else{.    pPage
177a1 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b   = btreePageLook
177a2 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a  up(pBt, iPage);.
177a3 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d    }..  /* Increm
177a4 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67  ent the free pag
177a5 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65  e count on pPage
177a6 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  1 */.  rc = sqli
177a7 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
177a8 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a  age1->pDbPage);.
177a9 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
177aa 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
177ab 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65  nFree = get4byte
177ac 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
177ad 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65  36]);.  put4byte
177ae 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
177af 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a  36], nFree+1);..
177b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45  #ifdef SQLITE_SE
177b1 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a  CURE_DELETE.  /*
177b2 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53   If the SQLITE_S
177b3 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d  ECURE_DELETE com
177b4 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
177b5 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
177b6 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75  n.  ** always fu
177b7 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65  lly overwrite de
177b8 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  leted informatio
177b9 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20  n with zeros..  
177ba 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 65  */.  if( (!pPage
177bb 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 47   && (rc = btreeG
177bc 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67  etPage(pBt, iPag
177bd 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a  e, &pPage, 0))).
177be 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20     ||           
177bf 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61   (rc = sqlite3Pa
177c0 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
177c1 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20  pDbPage)).  ){. 
177c2 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65     goto freepage
177c3 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  _out;.  }.  mems
177c4 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c  et(pPage->aData,
177c5 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e   0, pPage->pBt->
177c6 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69  pageSize);.#endi
177c7 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  f..  /* If the d
177c8 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73  atabase supports
177c9 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72   auto-vacuum, wr
177ca 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  ite an entry in 
177cb 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
177cc 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65    ** to indicate
177cd 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69   that the page i
177ce 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69  s free..  */.  i
177cf 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
177d0 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74  ){.    ptrmapPut
177d1 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52  (pBt, iPage, PTR
177d2 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c  MAP_FREEPAGE, 0,
177d3 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72   &rc);.    if( r
177d4 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67  c ) goto freepag
177d5 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  e_out;.  }..  /*
177d6 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20   Now manipulate 
177d7 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62  the actual datab
177d8 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74  ase free-list st
177d9 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61  ructure. There a
177da 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73  re two.  ** poss
177db 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68  ibilities. If th
177dc 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63  e free-list is c
177dd 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20  urrently empty, 
177de 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a  or if the first.
177df 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20    ** trunk page 
177e0 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  in the free-list
177e1 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74   is full, then t
177e2 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65  his page will be
177e3 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20  come a.  ** new 
177e4 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20  free-list trunk 
177e5 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  page. Otherwise,
177e6 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20   it will become 
177e7 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20  a leaf of the.  
177e8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70  ** first trunk p
177e9 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65  age in the curre
177ea 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68  nt free-list. Th
177eb 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69  is block tests i
177ec 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73  f it.  ** is pos
177ed 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65  sible to add the
177ee 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66   page as a new f
177ef 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20  ree-list leaf.. 
177f0 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21   */.  if( nFree!
177f1 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c  =0 ){.    u32 nL
177f2 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  eaf;            
177f3 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e      /* Initial n
177f4 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65  umber of leaf ce
177f5 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67  lls on trunk pag
177f6 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b  e */..    iTrunk
177f7 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
177f8 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b  ge1->aData[32]);
177f9 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47  .    rc = btreeG
177fa 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75  etPage(pBt, iTru
177fb 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b  nk, &pTrunk, 0);
177fc 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
177fd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
177fe 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75  goto freepage_ou
177ff 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c  t;.    }..    nL
17800 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26  eaf = get4byte(&
17801 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d  pTrunk->aData[4]
17802 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
17803 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33  Bt->usableSize>3
17804 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65  2 );.    if( nLe
17805 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75  af > (u32)pBt->u
17806 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20  sableSize/4 - 2 
17807 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
17808 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
17809 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72  T;.      goto fr
1780a 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  eepage_out;.    
1780b 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20  }.    if( nLeaf 
1780c 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62  < (u32)pBt->usab
1780d 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a  leSize/4 - 8 ){.
1780e 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73        /* In this
1780f 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72   case there is r
17810 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b  oom on the trunk
17811 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20   page to insert 
17812 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a  the page.      *
17813 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73  * being freed as
17814 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20   a new leaf..   
17815 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e     **.      ** N
17816 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75  ote that the tru
17817 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72  nk page is not r
17818 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c  eally full until
17819 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20   it contains.   
1781a 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65     ** usableSize
1781b 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20  /4 - 2 entries, 
1781c 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34  not usableSize/4
1781d 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20   - 8 entries as 
1781e 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a  we have.      **
1781f 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65   coded.  But due
17820 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72   to a coding err
17821 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f  or in versions o
17822 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74  f SQLite prior t
17823 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30  o.      ** 3.6.0
17824 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68  , databases with
17825 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20   freelist trunk 
17826 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f  pages holding mo
17827 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a  re than.      **
17828 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
17829 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62  8 entries will b
1782a 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f  e reported as co
1782b 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72  rrupt.  In order
1782c 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69  .      ** to mai
1782d 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20  ntain backwards 
1782e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69  compatibility wi
1782f 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e  th older version
17830 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20  s of SQLite,.   
17831 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f     ** we will co
17832 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69  ntinue to restri
17833 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ct the number of
17834 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62   entries to usab
17835 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20  leSize/4 - 8.   
17836 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20     ** for now.  
17837 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e  At some point in
17838 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63   the future (onc
17839 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75  e everyone has u
1783a 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a  pgraded.      **
1783b 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74   to 3.6.0 or lat
1783c 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f  er) we should co
1783d 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68  nsider fixing th
1783e 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62  e conditional ab
1783f 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ove.      ** to 
17840 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65  read "usableSize
17841 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66  /4-2" instead of
17842 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38   "usableSize/4-8
17843 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  "..      */.    
17844 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17845 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d  gerWrite(pTrunk-
17846 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
17847 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17848 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75  OK ){.        pu
17849 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e  t4byte(&pTrunk->
1784a 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b  aData[4], nLeaf+
1784b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34  1);.        put4
1784c 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44  byte(&pTrunk->aD
1784d 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20  ata[8+nLeaf*4], 
1784e 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20  iPage);.#ifndef 
1784f 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45  SQLITE_SECURE_DE
17850 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 28  LETE.        if(
17851 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20   pPage ){.      
17852 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
17853 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d  DontWrite(pPage-
17854 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
17855 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
17856 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 65      rc = btreeSe
17857 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c  tHasContent(pBt,
17858 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d   iPage);.      }
17859 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 46  .      TRACE(("F
1785a 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61  REE-PAGE: %d lea
1785b 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20  f on trunk page 
1785c 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e  %d\n",pPage->pgn
1785d 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29  o,pTrunk->pgno))
1785e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65  ;.      goto fre
1785f 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  epage_out;.    }
17860 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f  .  }..  /* If co
17861 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74  ntrol flows to t
17862 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20  his point, then 
17863 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69  it was not possi
17864 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20  ble to add the. 
17865 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 69   ** the page bei
17866 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c 65  ng freed as a le
17867 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 66  af page of the f
17868 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68  irst trunk in th
17869 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a  e free-list..  *
1786a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75  * Possibly becau
1786b 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  se the free-list
1786c 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f   is empty, or po
1786d 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74  ssibly because t
1786e 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74  he .  ** first t
1786f 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65  runk in the free
17870 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45  -list is full. E
17871 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 70  ither way, the p
17872 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a  age being freed.
17873 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65    ** will become
17874 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 74   the new first t
17875 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65  runk page in the
17876 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f   free-list..  */
17877 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20  .  if( pPage==0 
17878 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  && SQLITE_OK!=(r
17879 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
1787a 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50  (pBt, iPage, &pP
1787b 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20  age, 0)) ){.    
1787c 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75  goto freepage_ou
1787d 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  t;.  }.  rc = sq
1787e 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1787f 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
17880 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
17881 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
17882 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
17883 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50   }.  put4byte(pP
17884 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75  age->aData, iTru
17885 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28  nk);.  put4byte(
17886 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d  &pPage->aData[4]
17887 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65  , 0);.  put4byte
17888 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
17889 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54  32], iPage);.  T
1788a 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45  RACE(("FREE-PAGE
1788b 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70  : %d new trunk p
1788c 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64  age replacing %d
1788d 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  \n", pPage->pgno
1788e 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65  , iTrunk));..fre
1788f 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28  epage_out:.  if(
17890 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50   pPage ){.    pP
17891 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
17892 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61  .  }.  releasePa
17893 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c  ge(pPage);.  rel
17894 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29  easePage(pTrunk)
17895 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
17896 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
17897 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  ePage(MemPage *p
17898 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b  Page, int *pRC){
17899 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53  .  if( (*pRC)==S
1789a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1789b 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 32  *pRC = freePage2
1789c 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61  (pPage->pBt, pPa
1789d 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  ge, pPage->pgno)
1789e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
1789f 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  ree any overflow
178a0 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65   pages associate
178a1 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  d with the given
178a2 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63   Cell..*/.static
178a3 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d   int clearCell(M
178a4 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75  emPage *pPage, u
178a5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43  nsigned char *pC
178a6 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ell){.  BtShared
178a7 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70   *pBt = pPage->p
178a8 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69  Bt;.  CellInfo i
178a9 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c  nfo;.  Pgno ovfl
178aa 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Pgno;.  int rc;.
178ab 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75    int nOvfl;.  u
178ac 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b  16 ovflPageSize;
178ad 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
178ae 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
178af 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
178b0 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73  ) );.  btreePars
178b1 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
178b2 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20  pCell, &info);. 
178b3 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66   if( info.iOverf
178b4 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  low==0 ){.    re
178b5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
178b6 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20   /* No overflow 
178b7 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69  pages. Return wi
178b8 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74  thout doing anyt
178b9 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76  hing */.  }.  ov
178ba 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74  flPgno = get4byt
178bb 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f  e(&pCell[info.iO
178bc 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73  verflow]);.  ass
178bd 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65  ert( pBt->usable
178be 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76  Size > 4 );.  ov
178bf 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74  flPageSize = pBt
178c0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  ->usableSize - 4
178c1 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66  ;.  nOvfl = (inf
178c2 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66  o.nPayload - inf
178c3 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50  o.nLocal + ovflP
178c4 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66  ageSize - 1)/ovf
178c5 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73  lPageSize;.  ass
178c6 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30  ert( ovflPgno==0
178c7 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20   || nOvfl>0 );. 
178c8 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20   while( nOvfl-- 
178c9 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78  ){.    Pgno iNex
178ca 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61  t = 0;.    MemPa
178cb 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20  ge *pOvfl = 0;. 
178cc 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c     if( ovflPgno<
178cd 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61  2 || ovflPgno>pa
178ce 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
178cf 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20  ) ){.      /* 0 
178d0 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70  is not a legal p
178d1 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70  age number and p
178d2 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20  age 1 cannot be 
178d3 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65  an .      ** ove
178d4 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72  rflow page. Ther
178d5 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e  efore if ovflPgn
178d6 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 20  o<2 or past the 
178d7 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20  end of the .    
178d8 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61    ** file the da
178d9 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63  tabase must be c
178da 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20  orrupt. */.     
178db 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
178dc 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
178dd 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c   }.    if( nOvfl
178de 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67   ){.      rc = g
178df 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70  etOverflowPage(p
178e0 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70  Bt, ovflPgno, &p
178e1 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20  Ovfl, &iNext);. 
178e2 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
178e3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
178e4 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
178e5 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76  2(pBt, pOvfl, ov
178e6 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28  flPgno);.    if(
178e7 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20   pOvfl ){.      
178e8 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
178e9 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65  f(pOvfl->pDbPage
178ea 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
178eb 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
178ec 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20  .    ovflPgno = 
178ed 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  iNext;.  }.  ret
178ee 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
178ef 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74  ../*.** Create t
178f0 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65  he byte sequence
178f1 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65   used to represe
178f2 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67  nt a cell on pag
178f3 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77  e pPage.** and w
178f4 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73  rite that byte s
178f5 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65  equence into pCe
178f6 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20  ll[].  Overflow 
178f7 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c  pages are.** all
178f8 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65  ocated and fille
178f9 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72  d in as necessar
178fa 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  y.  The calling 
178fb 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20  procedure.** is 
178fc 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
178fd 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66  making sure suff
178fe 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73  icient space has
178ff 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a   been allocated.
17900 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a  ** for pCell[]..
17901 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
17902 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e  pCell does not n
17903 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f  ecessary need to
17904 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50   point to the pP
17905 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72  age->aData.** ar
17906 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74  ea.  pCell might
17907 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74   point to some t
17908 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65  emporary storage
17909 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c  .  The cell will
1790a 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74  .** be construct
1790b 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f  ed in this tempo
1790c 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63  rary area then c
1790d 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65  opied into pPage
1790e 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72  ->aData.** later
1790f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
17910 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65  fillInCell(.  Me
17911 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
17912 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17913 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f  The page that co
17914 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20  ntains the cell 
17915 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
17916 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20  ar *pCell,      
17917 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20      /* Complete 
17918 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  text of the cell
17919 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1791a 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79   *pKey, i64 nKey
1791b 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  ,    /* The key 
1791c 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1791d 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61  *pData,int nData
1791e 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20  ,   /* The data 
1791f 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20  */.  int nZero, 
17920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17921 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72      /* Extra zer
17922 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e  o bytes to appen
17923 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20  d to pData */.  
17924 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20  int *pnSize     
17925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17926 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a  * Write cell siz
17927 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
17928 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63  nt nPayload;.  c
17929 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20  onst u8 *pSrc;. 
1792a 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63   int nSrc, n, rc
1792b 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66  ;.  int spaceLef
1792c 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  t;.  MemPage *pO
1792d 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61  vfl = 0;.  MemPa
1792e 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d  ge *pToRelease =
1792f 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   0;.  unsigned c
17930 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75  har *pPrior;.  u
17931 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50  nsigned char *pP
17932 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72  ayload;.  BtShar
17933 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
17934 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  >pBt;.  Pgno pgn
17935 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74  oOvfl = 0;.  int
17936 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c   nHeader;.  Cell
17937 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73  Info info;..  as
17938 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17939 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1793a 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
1793b 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f    /* pPage is no
1793c 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72  t necessarily wr
1793d 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43  iteable since pC
1793e 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78  ell might be aux
1793f 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66  iliary.  ** buff
17940 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73  er space that is
17941 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74   separate from t
17942 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20  he pPage buffer 
17943 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74  area */.  assert
17944 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61  ( pCell<pPage->a
17945 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26  Data || pCell>=&
17946 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
17947 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20  ->pageSize].    
17948 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
17949 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1794a 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
1794b 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c  e) );..  /* Fill
1794c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20   in the header. 
1794d 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30  */.  nHeader = 0
1794e 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ;.  if( !pPage->
1794f 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61  leaf ){.    nHea
17950 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20  der += 4;.  }.  
17951 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
17952 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65  ta ){.    nHeade
17953 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26  r += putVarint(&
17954 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20  pCell[nHeader], 
17955 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20  nData+nZero);.  
17956 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61  }else{.    nData
17957 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20   = nZero = 0;.  
17958 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70  }.  nHeader += p
17959 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
1795a 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a  nHeader], *(u64*
1795b 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65  )&nKey);.  btree
1795c 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
1795d 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f  ge, pCell, &info
1795e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66  );.  assert( inf
1795f 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64  o.nHeader==nHead
17960 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  er );.  assert( 
17961 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20  info.nKey==nKey 
17962 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66  );.  assert( inf
17963 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e  o.nData==(u32)(n
17964 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20  Data+nZero) );. 
17965 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74   .  /* Fill in t
17966 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  he payload */.  
17967 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61  nPayload = nData
17968 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20   + nZero;.  if( 
17969 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
1796a 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74  .    pSrc = pDat
1796b 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44  a;.    nSrc = nD
1796c 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d  ata;.    nData =
1796d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20   0;.  }else{ .  
1796e 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79    if( NEVER(nKey
1796f 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70  >0x7fffffff || p
17970 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20  Key==0) ){.     
17971 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
17972 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
17973 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
17974 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20  += (int)nKey;.  
17975 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20    pSrc = pKey;. 
17976 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e     nSrc = (int)n
17977 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69  Key;.  }.  *pnSi
17978 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b  ze = info.nSize;
17979 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69  .  spaceLeft = i
1797a 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50  nfo.nLocal;.  pP
1797b 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b  ayload = &pCell[
1797c 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69  nHeader];.  pPri
1797d 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f  or = &pCell[info
1797e 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20  .iOverflow];..  
1797f 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e  while( nPayload>
17980 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61  0 ){.    if( spa
17981 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66  ceLeft==0 ){.#if
17982 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17983 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
17984 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61    Pgno pgnoPtrma
17985 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a  p = pgnoOvfl; /*
17986 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70   Overflow page p
17987 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79  ointer-map entry
17988 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69   page */.      i
17989 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
1798a 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  um ){.        do
1798b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f  {.          pgno
1798c 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  Ovfl++;.        
1798d 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20  } while( .      
1798e 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47      PTRMAP_ISPAG
1798f 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29  E(pBt, pgnoOvfl)
17990 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45   || pgnoOvfl==PE
17991 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
17992 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b  pBt) .        );
17993 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
17994 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
17995 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
17996 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f  , &pOvfl, &pgnoO
17997 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30  vfl, pgnoOvfl, 0
17998 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
17999 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1799a 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
1799b 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
1799c 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c  rts auto-vacuum,
1799d 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
1799e 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20  or subsequent.  
1799f 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20      ** overflow 
179a0 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c  page is being al
179a1 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20  located, add an 
179a2 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69  entry to the poi
179a3 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a  nter-map.      *
179a4 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20  * for that page 
179a5 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20  now. .      **. 
179a6 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20       ** If this 
179a7 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65  is the first ove
179a8 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
179a9 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c   write a partial
179aa 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a   entry .      **
179ab 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   to the pointer-
179ac 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65  map. If we write
179ad 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73   nothing to this
179ae 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f   pointer-map slo
179af 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  t,.      ** then
179b0 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20   the optimistic 
179b1 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70  overflow chain p
179b2 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65  rocessing in cle
179b3 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a  arCell().      *
179b4 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72  * may misinterpr
179b5 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c  et the uninitial
179b6 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20  ised values and 
179b7 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20  delete the.     
179b8 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20   ** wrong pages 
179b9 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
179ba 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
179bb 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
179bc 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c  acuum && rc==SQL
179bd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
179be 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67    u8 eType = (pg
179bf 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f  noPtrmap?PTRMAP_
179c0 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50  OVERFLOW2:PTRMAP
179c1 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20  _OVERFLOW1);.   
179c2 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
179c3 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54  Bt, pgnoOvfl, eT
179c4 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c  ype, pgnoPtrmap,
179c5 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69   &rc);.        i
179c6 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
179c7 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
179c8 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d  Ovfl);.        }
179c9 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
179ca 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
179cb 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
179cc 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b  age(pToRelease);
179cd 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
179ce 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rc;.      }..   
179cf 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65     /* If pToRele
179d0 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20  ase is not zero 
179d1 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e  than pPrior poin
179d2 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ts into the data
179d3 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f   area.      ** o
179d4 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d  f pToRelease.  M
179d5 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65  ake sure pToRele
179d6 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69  ase is still wri
179d7 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  teable. */.     
179d8 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65   assert( pToRele
179d9 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ase==0 || sqlite
179da 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
179db 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44  e(pToRelease->pD
179dc 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20  bPage) );..     
179dd 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73   /* If pPrior is
179de 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
179df 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c  a area of pPage,
179e0 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20   then make sure 
179e1 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69  pPage.      ** i
179e2 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
179e3 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
179e4 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d  t( pPrior<pPage-
179e5 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72  >aData || pPrior
179e6 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  >=&pPage->aData[
179e7 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20  pBt->pageSize]. 
179e8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
179e9 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
179ea 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
179eb 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20  Page) );..      
179ec 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c  put4byte(pPrior,
179ed 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20   pgnoOvfl);.    
179ee 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
179ef 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20  oRelease);.     
179f0 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f   pToRelease = pO
179f1 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f  vfl;.      pPrio
179f2 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61  r = pOvfl->aData
179f3 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
179f4 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20  (pPrior, 0);.   
179f5 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70     pPayload = &p
179f6 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a  Ovfl->aData[4];.
179f7 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20        spaceLeft 
179f8 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
179f9 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  e - 4;.    }.   
179fa 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20   n = nPayload;. 
179fb 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65     if( n>spaceLe
179fc 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65  ft ) n = spaceLe
179fd 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  ft;..    /* If p
179fe 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74  ToRelease is not
179ff 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c   zero than pPayl
17a00 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20  oad points into 
17a01 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20  the data area.  
17a02 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61    ** of pToRelea
17a03 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70  se.  Make sure p
17a04 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69  ToRelease is sti
17a05 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f  ll writeable. */
17a06 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
17a07 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71  Release==0 || sq
17a08 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
17a09 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65  eable(pToRelease
17a0a 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
17a0b 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61     /* If pPayloa
17a0c 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  d is part of the
17a0d 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50   data area of pP
17a0e 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  age, then make s
17a0f 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a  ure pPage.    **
17a10 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61   is still writea
17a11 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ble */.    asser
17a12 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67  t( pPayload<pPag
17a13 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79  e->aData || pPay
17a14 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44  load>=&pPage->aD
17a15 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  ata[pBt->pageSiz
17a16 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  e].            |
17a17 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  | sqlite3PagerIs
17a18 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
17a19 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
17a1a 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a    if( nSrc>0 ){.
17a1b 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63        if( n>nSrc
17a1c 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20   ) n = nSrc;.   
17a1d 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20     assert( pSrc 
17a1e 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
17a1f 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20  pPayload, pSrc, 
17a20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  n);.    }else{. 
17a21 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79       memset(pPay
17a22 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20  load, 0, n);.   
17a23 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
17a24 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f  -= n;.    pPaylo
17a25 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72  ad += n;.    pSr
17a26 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63  c += n;.    nSrc
17a27 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65   -= n;.    space
17a28 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69  Left -= n;.    i
17a29 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( nSrc==0 ){.  
17a2a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61      nSrc = nData
17a2b 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ;.      pSrc = p
17a2c 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
17a2d 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
17a2e 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74  oRelease);.  ret
17a2f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
17a30 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
17a31 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f  he i-th cell fro
17a32 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72  m pPage.  This r
17a33 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70  outine effects p
17a34 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68  Page only..** Th
17a35 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  e cell content i
17a36 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64  s not freed or d
17a37 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20  eallocated.  It 
17a38 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
17a39 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** the cell cont
17a3a 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70  ent has been cop
17a3b 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c  ied someplace el
17a3c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
17a3d 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65  e just.** remove
17a3e 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
17a3f 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d  to the cell from
17a40 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73   pPage..**.** "s
17a41 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e  z" must be the n
17a42 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
17a43 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73  n the cell..*/.s
17a44 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43  tatic void dropC
17a45 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ell(MemPage *pPa
17a46 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74  ge, int idx, int
17a47 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a   sz, int *pRC){.
17a48 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
17a49 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
17a4a 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20  r */.  int pc;  
17a4b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
17a4c 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   to cell content
17a4d 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64   of cell being d
17a4e 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a  eleted */.  u8 *
17a4f 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70  data;       /* p
17a50 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20  Page->aData */. 
17a51 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20   u8 *ptr;       
17a52 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65   /* Used to move
17a53 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69   bytes around wi
17a54 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20  thin data[] */. 
17a55 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
17a56 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63   /* The return c
17a57 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  ode */.  int hdr
17a58 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69  ;        /* Begi
17a59 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61  nning of the hea
17a5a 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67  der.  0 most pag
17a5b 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20  es.  100 page 1 
17a5c 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29  */..  if( *pRC )
17a5d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65   return;..  asse
17a5e 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64  rt( idx>=0 && id
17a5f 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  x<pPage->nCell )
17a60 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d  ;.  assert( sz==
17a61 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20  cellSize(pPage, 
17a62 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  idx) );.  assert
17a63 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
17a64 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
17a65 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
17a66 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
17a67 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
17a68 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
17a69 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
17a6a 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26  aData;.  ptr = &
17a6b 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c  data[pPage->cell
17a6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b  Offset + 2*idx];
17a6d 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65  .  pc = get2byte
17a6e 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70  (ptr);.  hdr = p
17a6f 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
17a70 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d  .  testcase( pc=
17a71 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b  =get2byte(&data[
17a72 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73  hdr+5]) );.  tes
17a73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50  tcase( pc+sz==pP
17a74 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
17a75 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63  Size );.  if( pc
17a76 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 74   < get2byte(&dat
17a77 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b  a[hdr+5]) || pc+
17a78 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d  sz > pPage->pBt-
17a79 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20  >usableSize ){. 
17a7a 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45     *pRC = SQLITE
17a7b 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
17a7c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
17a7d 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28   rc = freeSpace(
17a7e 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a  pPage, pc, sz);.
17a7f 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
17a80 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72  *pRC = rc;.    r
17a81 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72  eturn;.  }.  for
17a82 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67  (i=idx+1; i<pPag
17a83 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70  e->nCell; i++, p
17a84 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b  tr+=2){.    ptr[
17a85 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20  0] = ptr[2];.   
17a86 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d   ptr[1] = ptr[3]
17a87 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e  ;.  }.  pPage->n
17a88 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79  Cell--;.  put2by
17a89 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c  te(&data[hdr+3],
17a8a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a   pPage->nCell);.
17a8b 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b    pPage->nFree +
17a8c 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  = 2;.}../*.** In
17a8d 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20  sert a new cell 
17a8e 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c  on pPage at cell
17a8f 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65   index "i".  pCe
17a90 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ll points to the
17a91 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  .** content of t
17a92 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49  he cell..**.** I
17a93 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
17a94 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74  nt will fit on t
17a95 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75  he page, then pu
17a96 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20  t it there.  If 
17a97 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66  it.** will not f
17a98 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20  it, then make a 
17a99 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c  copy of the cell
17a9a 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54   content into pT
17a9b 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20  emp if.** pTemp 
17a9c 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65  is not null.  Re
17a9d 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d  gardless of pTem
17a9e 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  p, allocate a ne
17a9f 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50  w entry.** in pP
17aa0 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64  age->aOvfl[] and
17aa1 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74   make it point t
17aa2 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  o the cell conte
17aa3 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e  nt (either.** in
17aa4 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72   pTemp or the or
17aa5 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e  iginal pCell) an
17aa6 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74  d also record it
17aa7 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c  s index. .** All
17aa8 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e  ocating a new en
17aa9 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43  try in pPage->aC
17aaa 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68  ell[] implies th
17aab 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f  at .** pPage->nO
17aac 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65  verflow is incre
17aad 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mented..**.** If
17aae 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65   nSkip is non-ze
17aaf 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  ro, then do not 
17ab0 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e  copy the first n
17ab1 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68  Skip bytes of th
17ab2 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63  e.** cell. The c
17ab3 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77  aller will overw
17ab4 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20  rite them after 
17ab5 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
17ab6 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b  turns. If.** nSk
17ab7 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ip is non-zero, 
17ab8 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e  then pCell may n
17ab9 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69  ot point to an i
17aba 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f  nvalid memory lo
17abb 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20  cation .** (but 
17abc 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61  pCell+nSkip is a
17abd 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f  lways valid)..*/
17abe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
17abf 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61  ertCell(.  MemPa
17ac0 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20  ge *pPage,   /* 
17ac1 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20  Page into which 
17ac2 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a  we are copying *
17ac3 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20  /.  int i,      
17ac4 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c        /* New cel
17ac5 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d  l becomes the i-
17ac6 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70  th cell of the p
17ac7 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65  age */.  u8 *pCe
17ac8 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ll,        /* Co
17ac9 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77  ntent of the new
17aca 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73   cell */.  int s
17acb 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  z,           /* 
17acc 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74  Bytes of content
17acd 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75   in pCell */.  u
17ace 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20  8 *pTemp,       
17acf 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65   /* Temp storage
17ad0 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c   space for pCell
17ad1 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20  , if needed */. 
17ad2 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20   Pgno iChild,   
17ad3 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72     /* If non-zer
17ad4 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 74  o, replace first
17ad5 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68   4 bytes with th
17ad6 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e  is value */.  in
17ad7 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 20  t *pRC          
17ad8 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
17ad9 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72  e return code fr
17ada 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  om here */.){.  
17adb 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
17adc 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72    /* Where to wr
17add 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e  ite new cell con
17ade 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a  tent in data[] *
17adf 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20  /.  int j;      
17ae0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
17ae1 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65  unter */.  int e
17ae2 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nd;          /* 
17ae3 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20  First byte past 
17ae4 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f  the last cell po
17ae5 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20  inter in data[] 
17ae6 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20  */.  int ins;   
17ae7 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
17ae8 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20  in data[] where 
17ae9 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  new cell pointer
17aea 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   is inserted */.
17aeb 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74    int cellOffset
17aec 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f  ;   /* Address o
17aed 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69  f first cell poi
17aee 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a  nter in data[] *
17aef 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20  /.  u8 *data;   
17af0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
17af1 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c  tent of the whol
17af2 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  e page */.  u8 *
17af3 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ptr;          /*
17af4 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67   Used for moving
17af5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f   information aro
17af6 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  und in data[] */
17af7 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20  ..  int nSkip = 
17af8 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29  (iChild ? 4 : 0)
17af9 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20  ;..  if( *pRC ) 
17afa 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72  return;..  asser
17afb 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50  t( i>=0 && i<=pP
17afc 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65  age->nCell+pPage
17afd 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20  ->nOverflow );. 
17afe 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
17aff 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70  nCell<=MX_CELL(p
17b00 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58  Page->pBt) && MX
17b01 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74  _CELL(pPage->pBt
17b02 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73  )<=5460 );.  ass
17b03 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
17b04 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65  rflow<=ArraySize
17b05 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29  (pPage->aOvfl) )
17b06 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17b07 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17b08 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
17b09 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 63 65  ) );.  /* The ce
17b0a 6c 6c 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c  ll should normal
17b0b 6c 79 20 62 65 20 73 69 7a 65 64 20 63 6f 72 72  ly be sized corr
17b0c 65 63 74 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c  ectly.  However,
17b0d 20 77 68 65 6e 20 6d 6f 76 69 6e 67 20 61 0a 20   when moving a. 
17b0e 20 2a 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65   ** malformed ce
17b0f 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70  ll from a leaf p
17b10 61 67 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69  age to an interi
17b11 6f 72 20 70 61 67 65 2c 20 69 66 20 74 68 65 20  or page, if the 
17b12 63 65 6c 6c 20 73 69 7a 65 0a 20 20 2a 2a 20 77  cell size.  ** w
17b13 61 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73  anted to be less
17b14 20 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20   than 4 but got 
17b15 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20  rounded up to 4 
17b16 6f 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65  on the leaf, the
17b17 6e 20 73 69 7a 65 0a 20 20 2a 2a 20 6d 69 67 68  n size.  ** migh
17b18 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38  t be less than 8
17b19 20 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f   (leaf-size + po
17b1a 69 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e  inter) on the in
17b1b 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65  terior node.  He
17b1c 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 74 65 72  nce.  ** the ter
17b1d 6d 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20 69  m after the || i
17b1e 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
17b1f 61 73 73 65 72 74 28 29 2e 20 2a 2f 0a 20 20 61  assert(). */.  a
17b20 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53  ssert( sz==cellS
17b21 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43  izePtr(pPage, pC
17b22 65 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38 20 26  ell) || (sz==8 &
17b23 26 20 69 43 68 69 6c 64 3e 30 29 20 29 3b 0a 20  & iChild>0) );. 
17b24 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65   if( pPage->nOve
17b25 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50  rflow || sz+2>pP
17b26 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20  age->nFree ){.  
17b27 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20    if( pTemp ){. 
17b28 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d       memcpy(pTem
17b29 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e  p+nSkip, pCell+n
17b2a 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b  Skip, sz-nSkip);
17b2b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70  .      pCell = p
17b2c 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Temp;.    }.    
17b2d 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20  if( iChild ){.  
17b2e 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65      put4byte(pCe
17b2f 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20  ll, iChild);.   
17b30 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65   }.    j = pPage
17b31 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20  ->nOverflow++;. 
17b32 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e     assert( j<(in
17b33 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d  t)(sizeof(pPage-
17b34 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70  >aOvfl)/sizeof(p
17b35 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29  Page->aOvfl[0]))
17b36 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61   );.    pPage->a
17b37 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20  Ovfl[j].pCell = 
17b38 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65  pCell;.    pPage
17b39 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d  ->aOvfl[j].idx =
17b3a 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65   (u16)i;.  }else
17b3b 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
17b3c 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
17b3d 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
17b3e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
17b3f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17b40 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20   *pRC = rc;.    
17b41 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
17b42 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
17b43 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17b44 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
17b45 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20  ge) );.    data 
17b46 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
17b47 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d      cellOffset =
17b48 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73   pPage->cellOffs
17b49 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65  et;.    end = ce
17b4a 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61  llOffset + 2*pPa
17b4b 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69  ge->nCell;.    i
17b4c 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20  ns = cellOffset 
17b4d 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20  + 2*i;.    rc = 
17b4e 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50  allocateSpace(pP
17b4f 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a  age, sz, &idx);.
17b50 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70      if( rc ){ *p
17b51 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b  RC = rc; return;
17b52 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c   }.    /* The al
17b53 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f  locateSpace() ro
17b54 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73  utine guarantees
17b55 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
17b56 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20  wo properties.  
17b57 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72    ** if it retur
17b58 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20  ns success */.  
17b59 20 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d    assert( idx >=
17b5a 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73   end+2 );.    as
17b5b 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20  sert( idx+sz <= 
17b5c 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
17b5d 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50  leSize );.    pP
17b5e 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20  age->nCell++;.  
17b5f 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d    pPage->nFree -
17b60 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b  = (u16)(2 + sz);
17b61 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74  .    memcpy(&dat
17b62 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43  a[idx+nSkip], pC
17b63 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53  ell+nSkip, sz-nS
17b64 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43  kip);.    if( iC
17b65 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75  hild ){.      pu
17b66 74 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78  t4byte(&data[idx
17b67 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20  ], iChild);.    
17b68 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c  }.    for(j=end,
17b69 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a   ptr=&data[j]; j
17b6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d  >ins; j-=2, ptr-
17b6b 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30  =2){.      ptr[0
17b6c 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20  ] = ptr[-2];.   
17b6d 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b     ptr[1] = ptr[
17b6e 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  -1];.    }.    p
17b6f 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e  ut2byte(&data[in
17b70 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75  s], idx);.    pu
17b71 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61  t2byte(&data[pPa
17b72 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d  ge->hdrOffset+3]
17b73 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b  , pPage->nCell);
17b74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
17b75 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
17b76 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70      if( pPage->p
17b77 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
17b78 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  {.      /* The c
17b79 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ell may contain 
17b7a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
17b7b 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49  overflow page. I
17b7c 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20  f so, write.    
17b7d 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66    ** the entry f
17b7e 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
17b7f 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f  page into the po
17b80 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20  inter map..     
17b81 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70   */.      ptrmap
17b82 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65  PutOvflPtr(pPage
17b83 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20  , pCell, pRC);. 
17b84 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
17b85 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c  }../*.** Add a l
17b86 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20  ist of cells to 
17b87 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67  a page.  The pag
17b88 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74  e should be init
17b89 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20  ially empty..** 
17b8a 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75  The cells are gu
17b8b 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20  aranteed to fit 
17b8c 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a  on the page..*/.
17b8d 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65  static void asse
17b8e 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50  mblePage(.  MemP
17b8f 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a  age *pPage,   /*
17b90 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20   The page to be 
17b91 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20  assemblied */.  
17b92 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20  int nCell,      
17b93 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20    /* The number 
17b94 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20  of cells to add 
17b95 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  to this page */.
17b96 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20    u8 **apCell,  
17b97 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
17b98 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a  to cell bodies *
17b99 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20  /.  u16 *aSize  
17b9a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f        /* Sizes o
17b9b 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29  f the cells */.)
17b9c 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
17b9d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
17b9e 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70  unter */.  u8 *p
17b9f 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20  Cellptr;     /* 
17ba0 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20  Address of next 
17ba1 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
17ba2 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20    int cellbody; 
17ba3 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
17ba4 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79  f next cell body
17ba5 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74   */.  u8 * const
17ba6 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
17ba7 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
17ba8 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
17ba9 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a  data for pPage *
17baa 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64  /.  const int hd
17bab 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
17bac 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
17bad 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61  /* Offset of hea
17bae 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a  der on pPage */.
17baf 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61    const int nUsa
17bb0 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  ble = pPage->pBt
17bb1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a  ->usableSize; /*
17bb2 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20   Usable size of 
17bb3 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  page */..  asser
17bb4 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
17bb5 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  low==0 );.  asse
17bb6 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17bb7 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
17bb8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
17bb9 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20  ssert( nCell>=0 
17bba 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c  && nCell<=MX_CEL
17bbb 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26  L(pPage->pBt) &&
17bbc 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e   MX_CELL(pPage->
17bbd 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20  pBt)<=5460 );.  
17bbe 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
17bbf 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
17bc0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
17bc1 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  );..  /* Check t
17bc2 68 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73  hat the page has
17bc3 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65   just been zeroe
17bc4 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20  d by zeroPage() 
17bc5 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
17bc6 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  ge->nCell==0 );.
17bc7 20 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79    assert( get2by
17bc8 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
17bc9 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20  ==nUsable );..  
17bca 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61  pCellptr = &data
17bcb 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73  [pPage->cellOffs
17bcc 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20  et + nCell*2];. 
17bcd 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61   cellbody = nUsa
17bce 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65  ble;.  for(i=nCe
17bcf 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  ll-1; i>=0; i--)
17bd0 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d  {.    pCellptr -
17bd1 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64  = 2;.    cellbod
17bd2 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20  y -= aSize[i];. 
17bd3 20 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c     put2byte(pCel
17bd4 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b  lptr, cellbody);
17bd5 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74  .    memcpy(&dat
17bd6 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43  a[cellbody], apC
17bd7 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d  ell[i], aSize[i]
17bd8 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74  );.  }.  put2byt
17bd9 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20  e(&data[hdr+3], 
17bda 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79  nCell);.  put2by
17bdb 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
17bdc 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50   cellbody);.  pP
17bdd 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e  age->nFree -= (n
17bde 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65  Cell*2 + nUsable
17bdf 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20   - cellbody);.  
17be0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28  pPage->nCell = (
17be1 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a  u16)nCell;.}../*
17be2 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
17be3 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74  g parameters det
17be4 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20  ermine how many 
17be5 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67  adjacent pages g
17be6 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69  et involved.** i
17be7 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70  n a balancing op
17be8 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20  eration.  NN is 
17be9 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65  the number of ne
17bea 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65  ighbors on eithe
17beb 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65  r side.** of the
17bec 20 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69   page that parti
17bed 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61  cipate in the ba
17bee 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f  lancing operatio
17bef 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a  n.  NB is the.**
17bf0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
17bf1 20 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74   pages that part
17bf2 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69  icipate, includi
17bf3 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61  ng the target pa
17bf4 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69  ge and.** NN nei
17bf5 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
17bf6 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   side..**.** The
17bf7 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f   minimum value o
17bf8 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f  f NN is 1 (of co
17bf9 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69  urse).  Increasi
17bfa 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a  ng NN above 1.**
17bfb 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76   (to 2 or 3) giv
17bfc 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72  es a modest impr
17bfd 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43  ovement in SELEC
17bfe 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72  T and DELETE per
17bff 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65  formance.** in e
17c00 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61  xchange for a la
17c01 72 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e  rger degradation
17c02 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55   in INSERT and U
17c03 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63  PDATE performanc
17c04 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  e..** The value 
17c05 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f  of NN appears to
17c06 20 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72   give the best r
17c07 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a  esults overall..
17c08 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20  */.#define NN 1 
17c09 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17c0a 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f  umber of neighbo
17c0b 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64  rs on either sid
17c0c 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64  e of pPage */.#d
17c0d 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31  efine NB (NN*2+1
17c0e 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  )      /* Total 
17c0f 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69  pages involved i
17c10 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f  n the balance */
17c11 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
17c12 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41  E_OMIT_QUICKBALA
17c13 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  NCE./*.** This v
17c14 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63  ersion of balanc
17c15 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20  e() handles the 
17c16 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63  common special c
17c17 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e  ase where.** a n
17c18 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e  ew entry is bein
17c19 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68  g inserted on th
17c1a 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d  e extreme right-
17c1b 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72  end of the.** tr
17c1c 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72  ee, in other wor
17c1d 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77  ds, when the new
17c1e 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f   entry will beco
17c1f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a  me the largest.*
17c20 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  * entry in the t
17c21 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65  ree..**.** Inste
17c22 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20  ad of trying to 
17c23 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69  balance the 3 ri
17c24 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61  ght-most leaf pa
17c25 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a  ges, just add.**
17c26 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74   a new page to t
17c27 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
17c28 64 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f  de and put the o
17c29 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a  ne new entry in.
17c2a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54  ** that page.  T
17c2b 68 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72  his leaves the r
17c2c 69 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65  ight side of the
17c2d 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a   tree somewhat.*
17c2e 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42  * unbalanced.  B
17c2f 75 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74  ut odds are that
17c30 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65   we will be inse
17c31 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65  rting new entrie
17c32 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20  s.** at the end 
17c33 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20  soon afterwards 
17c34 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d  so the nearly em
17c35 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75  pty page will qu
17c36 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70  ickly.** fill up
17c37 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a  .  On average..*
17c38 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68  *.** pPage is th
17c39 65 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63  e leaf page whic
17c3a 68 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d  h is the right-m
17c3b 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ost page in the 
17c3c 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74  tree..** pParent
17c3d 20 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20   is its parent. 
17c3e 20 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65   pPage must have
17c3f 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c   a single overfl
17c40 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63  ow entry.** whic
17c41 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69  h is also the ri
17c42 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f  ght-most entry o
17c43 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  n the page..**.*
17c44 2a 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66  * The pSpace buf
17c45 66 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73  fer is used to s
17c46 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79  tore a temporary
17c47 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76   copy of the div
17c48 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61  ider.** cell tha
17c49 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74  t will be insert
17c4a 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e  ed into pParent.
17c4b 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e   Such a cell con
17c4c 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20  sists of a 4.** 
17c4d 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72  byte page number
17c4e 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76   followed by a v
17c4f 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69  ariable length i
17c50 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72  nteger. In other
17c51 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f  .** words, at mo
17c52 73 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e  st 13 bytes. Hen
17c53 63 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75  ce the pSpace bu
17c54 66 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a  ffer must be at.
17c55 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65  ** least 13 byte
17c56 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74  s in size..*/.st
17c57 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65  atic int balance
17c58 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a  _quick(MemPage *
17c59 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65  pParent, MemPage
17c5a 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70   *pPage, u8 *pSp
17c5b 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ace){.  BtShared
17c5c 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50   *const pBt = pP
17c5d 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20  age->pBt;    /* 
17c5e 42 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20  B-Tree Database 
17c5f 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e  */.  MemPage *pN
17c60 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
17c61 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77            /* New
17c62 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  ly allocated pag
17c63 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
17c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
17c66 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
17c67 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20  Pgno pgnoNew;   
17c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c69 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
17c6a 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a  ber of pNew */..
17c6b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17c6c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
17c6d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
17c6e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
17c6f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
17c70 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44  able(pParent->pD
17c71 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
17c72 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
17c73 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66  flow==1 );..  if
17c74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d  ( pPage->nCell<=
17c75 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
17c76 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
17c77 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
17c78 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20   new page. This 
17c79 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65  page will become
17c7a 20 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69   the right-sibli
17c7b 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67  ng of .  ** pPag
17c7c 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65  e. Make the pare
17c7d 6e 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65  nt page writable
17c7e 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65  , so that the ne
17c7f 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20  w divider cell. 
17c80 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72   ** may be inser
17c81 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65  ted. If both the
17c82 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  se operations ar
17c83 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72  e successful, pr
17c84 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63  oceed..  */.  rc
17c85 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
17c86 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c  Page(pBt, &pNew,
17c87 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29   &pgnoNew, 0, 0)
17c88 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
17c89 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75  ITE_OK ){..    u
17c8a 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63  8 *pOut = &pSpac
17c8b 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43  e[4];.    u8 *pC
17c8c 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76  ell = pPage->aOv
17c8d 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20  fl[0].pCell;.   
17c8e 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65   u16 szCell = ce
17c8f 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
17c90 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20   pCell);.    u8 
17c91 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73  *pStop;..    ass
17c92 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
17c93 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65  rIswriteable(pNe
17c94 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  w->pDbPage) );. 
17c95 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
17c96 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46  ->aData[0]==(PTF
17c97 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46  _INTKEY|PTF_LEAF
17c98 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29  DATA|PTF_LEAF) )
17c99 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70  ;.    zeroPage(p
17c9a 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c  New, PTF_INTKEY|
17c9b 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46  PTF_LEAFDATA|PTF
17c9c 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65  _LEAF);.    asse
17c9d 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31  mblePage(pNew, 1
17c9e 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c  , &pCell, &szCel
17c9f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  l);..    /* If t
17ca0 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  his is an auto-v
17ca1 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20  acuum database, 
17ca2 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74  update the point
17ca3 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69  er map.    ** wi
17ca4 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74  th entries for t
17ca5 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64  he new page, and
17ca6 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f   any pointer fro
17ca7 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65  m the .    ** ce
17ca8 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74  ll on the page t
17ca9 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  o an overflow pa
17caa 67 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66  ge. If either of
17cab 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70   these.    ** op
17cac 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20  erations fails, 
17cad 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
17cae 69 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20  is set, but the 
17caf 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20  contents.    ** 
17cb0 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  of the parent pa
17cb1 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e  ge are still man
17cb2 69 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20  ipulated by thh 
17cb3 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20  code below..    
17cb4 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61  ** That is Ok, a
17cb5 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
17cb6 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20   parent page is 
17cb7 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20  guaranteed to.  
17cb8 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61    ** be marked a
17cb9 73 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69  s dirty. Returni
17cba 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ng an error code
17cbb 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20   will cause a.  
17cbc 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75    ** rollback, u
17cbd 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67  ndoing any chang
17cbe 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70  es made to the p
17cbf 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20  arent page..    
17cc0 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54  */.    if( ISAUT
17cc1 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20  OVACUUM ){.     
17cc2 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
17cc3 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f  pgnoNew, PTRMAP_
17cc4 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e  BTREE, pParent->
17cc5 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20  pgno, &rc);.    
17cc6 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65    if( szCell>pNe
17cc7 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20  w->minLocal ){. 
17cc8 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74         ptrmapPut
17cc9 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43  OvflPtr(pNew, pC
17cca 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  ell, &rc);.     
17ccb 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
17ccc 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69  /* Create a divi
17ccd 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65  der cell to inse
17cce 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e  rt into pParent.
17ccf 20 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   The divider cel
17cd0 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74  l.    ** consist
17cd1 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61  s of a 4-byte pa
17cd2 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70  ge number (the p
17cd3 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50  age number of pP
17cd4 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20  age) and.    ** 
17cd5 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  a variable lengt
17cd6 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69  h key value (whi
17cd7 63 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ch must be the s
17cd8 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65  ame value as the
17cd9 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20  .    ** largest 
17cda 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20  key on pPage).. 
17cdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20     **.    ** To 
17cdc 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74  find the largest
17cdd 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50   key value on pP
17cde 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20  age, first find 
17cdf 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a  the right-most .
17ce0 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70      ** cell on p
17ce1 50 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20  Page. The first 
17ce2 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68  two fields of th
17ce3 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20  is cell are the 
17ce4 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c  .    ** record-l
17ce5 65 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c  ength (a variabl
17ce6 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e length integer
17ce7 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73   at most 32-bits
17ce8 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a   in size).    **
17ce9 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c   and the key val
17cea 75 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c  ue (a variable l
17ceb 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d  ength integer, m
17cec 61 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75  ay have any valu
17ced 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66  e)..    ** The f
17cee 69 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c  irst of the whil
17cef 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c  e(...) loops bel
17cf0 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68  ow skips over th
17cf1 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a  e record-length.
17cf2 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68      ** field. Th
17cf3 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e  e second while(.
17cf4 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20  ..) loop copies 
17cf5 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72  the key value fr
17cf6 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65  om the.    ** ce
17cf7 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f  ll on pPage into
17cf8 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66   the pSpace buff
17cf9 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  er..    */.    p
17cfa 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
17cfb 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43  pPage, pPage->nC
17cfc 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f  ell-1);.    pSto
17cfd 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20  p = &pCell[9];. 
17cfe 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65     while( (*(pCe
17cff 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70  ll++)&0x80) && p
17d00 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20  Cell<pStop );.  
17d01 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c    pStop = &pCell
17d02 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  [9];.    while( 
17d03 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28  ((*(pOut++) = *(
17d04 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20  pCell++))&0x80) 
17d05 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29  && pCell<pStop )
17d06 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ;..    /* Insert
17d07 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72   the new divider
17d08 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65   cell into pPare
17d09 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72  nt. */.    inser
17d0a 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70  tCell(pParent, p
17d0b 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70  Parent->nCell, p
17d0c 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75  Space, (int)(pOu
17d0d 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20  t-pSpace),.     
17d0e 20 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61            0, pPa
17d0f 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a  ge->pgno, &rc);.
17d10 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20  .    /* Set the 
17d11 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e  right-child poin
17d12 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74  ter of pParent t
17d13 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e  o point to the n
17d14 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20  ew page. */.    
17d15 70 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e  put4byte(&pParen
17d16 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74  t->aData[pParent
17d17 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20  ->hdrOffset+8], 
17d18 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20  pgnoNew);.  .   
17d19 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
17d1a 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
17d1b 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20   new page. */.  
17d1c 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e    releasePage(pN
17d1d 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ew);.  }..  retu
17d1e 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
17d1f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51  /* SQLITE_OMIT_Q
17d20 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a  UICKBALANCE */..
17d21 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73  #if 0./*.** This
17d22 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
17d23 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e  ot contribute an
17d24 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70  ything to the op
17d25 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74  eration of SQLit
17d26 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65  e..** it is some
17d27 74 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20  times activated 
17d28 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c  temporarily whil
17d29 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65  e debugging code
17d2a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a   responsible .**
17d2b 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69   for setting poi
17d2c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
17d2d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
17d2e 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73  ptrmapCheckPages
17d2f 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67  (MemPage **apPag
17d30 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20  e, int nPage){. 
17d31 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72   int i, j;.  for
17d32 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69  (i=0; i<nPage; i
17d33 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b  ++){.    Pgno n;
17d34 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d  .    u8 e;.    M
17d35 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
17d36 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42  apPage[i];.    B
17d37 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
17d38 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61  Page->pBt;.    a
17d39 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73  ssert( pPage->is
17d3a 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72  Init );..    for
17d3b 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e  (j=0; j<pPage->n
17d3c 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  Cell; j++){.    
17d3d 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
17d3e 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20  .      u8 *z;.  
17d3f 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69     .      z = fi
17d40 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29  ndCell(pPage, j)
17d41 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72  ;.      btreePar
17d42 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
17d43 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20   z, &info);.    
17d44 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72    if( info.iOver
17d45 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20  flow ){.        
17d46 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34  Pgno ovfl = get4
17d47 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76  byte(&z[info.iOv
17d48 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20  erflow]);.      
17d49 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c    ptrmapGet(pBt,
17d4a 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a   ovfl, &e, &n);.
17d4b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
17d4c 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26  n==pPage->pgno &
17d4d 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  & e==PTRMAP_OVER
17d4e 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d  FLOW1 );.      }
17d4f 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67  .      if( !pPag
17d50 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
17d51 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20     Pgno child = 
17d52 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20  get4byte(z);.   
17d53 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70       ptrmapGet(p
17d54 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26  Bt, child, &e, &
17d55 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  n);.        asse
17d56 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67  rt( n==pPage->pg
17d57 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f  no && e==PTRMAP_
17d58 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d  BTREE );.      }
17d59 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
17d5a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
17d5b 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20       Pgno child 
17d5c 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
17d5d 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
17d5e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
17d5f 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70       ptrmapGet(p
17d60 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26  Bt, child, &e, &
17d61 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  n);.      assert
17d62 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f  ( n==pPage->pgno
17d63 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54   && e==PTRMAP_BT
17d64 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  REE );.    }.  }
17d65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
17d66 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
17d67 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
17d68 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63  ed to copy the c
17d69 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62  ontents of the b
17d6a 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65  -tree node store
17d6b 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46  d .** on page pF
17d6c 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e  rom to page pTo.
17d6d 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77   If page pFrom w
17d6e 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61  as not a leaf pa
17d6f 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ge, then.** the 
17d70 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
17d71 69 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69  ies for each chi
17d72 6c 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61  ld page are upda
17d73 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a  ted so that the.
17d74 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73  ** parent page s
17d75 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69  tored in the poi
17d76 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65  nter map is page
17d77 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63   pTo. If pFrom c
17d78 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20  ontained.** any 
17d79 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66  cells with overf
17d7a 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72  low page pointer
17d7b 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72  s, then the corr
17d7c 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65  esponding pointe
17d7d 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73  r.** map entries
17d7e 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65   are also update
17d7f 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61  d so that the pa
17d80 72 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67  rent page is pag
17d81 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  e pTo..**.** If 
17d82 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74  pFrom is current
17d83 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20  ly carrying any 
17d84 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28  overflow cells (
17d85 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a  entries in the.*
17d86 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b  * MemPage.aOvfl[
17d87 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61  ] array), they a
17d88 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f  re not copied to
17d89 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66   pTo. .**.** Bef
17d8a 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70  ore returning, p
17d8b 61 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69  age pTo is reini
17d8c 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62  tialized using b
17d8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a  treeInitPage()..
17d8e 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72  **.** The perfor
17d8f 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75  mance of this fu
17d90 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72  nction is not cr
17d91 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e  itical. It is on
17d92 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74  ly used by .** t
17d93 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c  he balance_shall
17d94 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e  ower() and balan
17d95 63 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63  ce_deeper() proc
17d96 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20  edures, neither 
17d97 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20  of.** which are 
17d98 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64  called often und
17d99 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d  er normal circum
17d9a 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74  stances..*/.stat
17d9b 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65  ic void copyNode
17d9c 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20  Content(MemPage 
17d9d 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20  *pFrom, MemPage 
17d9e 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b  *pTo, int *pRC){
17d9f 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53  .  if( (*pRC)==S
17da0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17da1 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74  BtShared * const
17da2 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42   pBt = pFrom->pB
17da3 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73  t;.    u8 * cons
17da4 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d  t aFrom = pFrom-
17da5 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a  >aData;.    u8 *
17da6 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f   const aTo = pTo
17da7 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74  ->aData;.    int
17da8 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20   const iFromHdr 
17da9 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73  = pFrom->hdrOffs
17daa 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73  et;.    int cons
17dab 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f  t iToHdr = ((pTo
17dac 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30  ->pgno==1) ? 100
17dad 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 54 4f   : 0);.    TESTO
17dae 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 20 20  NLY(int rc;).   
17daf 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 0a 20   int iData;.  . 
17db0 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46   .    assert( pF
17db1 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20  rom->isInit );. 
17db2 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
17db3 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 72 20  ->nFree>=iToHdr 
17db4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67  );.    assert( g
17db5 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69  et2byte(&aFrom[i
17db6 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 42 74  FromHdr+5])<=pBt
17db7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a  ->usableSize );.
17db8 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 74    .    /* Copy t
17db9 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 63  he b-tree node c
17dba 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 67 65  ontent from page
17dbb 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70   pFrom to page p
17dbc 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 74 61  To. */.    iData
17dbd 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 46 72   = get2byte(&aFr
17dbe 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b  om[iFromHdr+5]);
17dbf 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f  .    memcpy(&aTo
17dc0 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b  [iData], &aFrom[
17dc1 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61  iData], pBt->usa
17dc2 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 3b 0a  bleSize-iData);.
17dc3 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b      memcpy(&aTo[
17dc4 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b  iToHdr], &aFrom[
17dc5 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d  iFromHdr], pFrom
17dc6 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32  ->cellOffset + 2
17dc7 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a  *pFrom->nCell);.
17dc8 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e 69 74    .    /* Reinit
17dc9 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54 6f 20  ialize page pTo 
17dca 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74  so that the cont
17dcb 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 6d 50  ents of the MemP
17dcc 61 67 65 20 73 74 72 75 63 74 75 72 65 0a 20 20  age structure.  
17dcd 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 20 6e    ** match the n
17dce 65 77 20 64 61 74 61 2e 20 54 68 65 20 69 6e 69  ew data. The ini
17dcf 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 70  tialization of p
17dd0 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 69 6c  To "cannot" fail
17dd1 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20  , as the.    ** 
17dd2 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d  data copied from
17dd3 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 6e 20   pFrom is known 
17dd4 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 2a 2f  to be valid.  */
17dd5 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74  .    pTo->isInit
17dd6 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 4f 4e   = 0;.    TESTON
17dd7 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65 65 49  LY(rc = ) btreeI
17dd8 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a 20 20  nitPage(pTo);.  
17dd9 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
17dda 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a 20 20  LITE_OK );.  .  
17ddb 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
17ddc 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
17ddd 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20  atabase, update 
17dde 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
17ddf 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 66  entries.    ** f
17de0 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20 6f 72  or any b-tree or
17de1 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
17de2 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e  that pTo now con
17de3 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65  tains the pointe
17de4 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20  rs to..    */.  
17de5 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
17de6 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43  UM ){.      *pRC
17de7 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61   = setChildPtrma
17de8 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20  ps(pTo);.    }. 
17de9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
17dea 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72   routine redistr
17deb 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20  ibutes cells on 
17dec 74 68 65 20 69 50 61 72 65 6e 74 49 64 78 27 74  the iParentIdx't
17ded 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65  h child of pPare
17dee 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 65 72  nt.** (hereafter
17def 20 22 74 68 65 20 70 61 67 65 22 29 20 61 6e 64   "the page") and
17df0 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69 6e 67   up to 2 sibling
17df1 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61  s so that all pa
17df2 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74  ges have about t
17df3 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e  he.** same amoun
17df4 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e  t of free space.
17df5 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c   Usually a singl
17df6 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74  e sibling on eit
17df7 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 0a  her side of the.
17df8 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73 65 64  ** page are used
17df9 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e   in the balancin
17dfa 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 73  g, though both s
17dfb 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f  iblings might co
17dfc 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73  me from one.** s
17dfd 69 64 65 20 69 66 20 74 68 65 20 70 61 67 65 20  ide if the page 
17dfe 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 72 20  is the first or 
17dff 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20 69 74  last child of it
17e00 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74 68 65  s parent. If the
17e01 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 66 65   page .** has fe
17e02 77 65 72 20 74 68 61 6e 20 32 20 73 69 62 6c 69  wer than 2 sibli
17e03 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 20 77  ngs (something w
17e04 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61  hich can only ha
17e05 70 70 65 6e 20 69 66 20 74 68 65 20 70 61 67 65  ppen if the page
17e06 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 70 61  .** is a root pa
17e07 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20 6f 66  ge or a child of
17e08 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20 74 68   a root page) th
17e09 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65  en all available
17e0a 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72   siblings.** par
17e0b 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20  ticipate in the 
17e0c 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a  balancing..**.**
17e0d 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   The number of s
17e0e 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 20 70  iblings of the p
17e0f 61 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63  age might be inc
17e10 72 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61  reased or decrea
17e11 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f  sed by .** one o
17e12 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f  r two in an effo
17e13 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73  rt to keep pages
17e14 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74   nearly full but
17e15 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20   not over full. 
17e16 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
17e17 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
17e18 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f  ne is called, so
17e19 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20  me of the cells 
17e1a 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d  on the page.** m
17e1b 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c  ight not actuall
17e1c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 4d  y be stored in M
17e1d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20  emPage.aData[]. 
17e1e 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a  This can happen.
17e1f 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ** if the page i
17e20 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 69 73  s overfull. This
17e21 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73   routine ensures
17e22 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20   that all cells 
17e23 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20  allocated.** to 
17e24 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73  the page and its
17e25 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20 69 6e   siblings fit in
17e26 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61  to MemPage.aData
17e27 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  [] before return
17e28 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ing..**.** In th
17e29 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61  e course of bala
17e2a 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65 20 61  ncing the page a
17e2b 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 2c  nd its siblings,
17e2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a   cells may be.**
17e2d 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 6f   inserted into o
17e2e 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  r removed from t
17e2f 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 28  he parent page (
17e30 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20  pParent). Doing 
17e31 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 65 20  so.** may cause 
17e32 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
17e33 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75  to become overfu
17e34 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e  ll or underfull.
17e35 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 70 70   If this.** happ
17e36 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65 20 72  ens, it is the r
17e37 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
17e38 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 69   the caller to i
17e39 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 65 63  nvoke the correc
17e3a 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72  t.** balancing r
17e3b 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20 74 68  outine to fix th
17e3c 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 65 20  is problem (see 
17e3d 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 72 6f  the balance() ro
17e3e 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49  utine). .**.** I
17e3f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
17e40 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 61  ails for any rea
17e41 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 6c 65  son, it might le
17e42 61 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ave the database
17e43 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 70 74  .** in a corrupt
17e44 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69 66 20  ed state. So if 
17e45 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69  this routine fai
17e46 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  ls, the database
17e47 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f   should.** be ro
17e48 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  lled back..**.**
17e49 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
17e4a 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
17e4b 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65  tion, aOvflSpace
17e4c 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  , is a pointer t
17e4d 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 62 69  o a.** buffer bi
17e4e 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
17e4f 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 77 68   one page. If wh
17e50 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 63 65  ile inserting ce
17e51 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72  lls into the par
17e52 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 50 61  ent.** page (pPa
17e53 72 65 6e 74 29 20 74 68 65 20 70 61 72 65 6e 74  rent) the parent
17e54 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76   page becomes ov
17e55 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 75 66  erfull, this buf
17e56 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 20 74  fer is.** used t
17e57 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61 72 65  o store the pare
17e58 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 63 65  nt's overflow ce
17e59 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74 68 69  lls. Because thi
17e5a 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72  s function inser
17e5b 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20  ts.** a maximum 
17e5c 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65 72 20  of four divider 
17e5d 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70  cells into the p
17e5e 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e 64 20  arent page, and 
17e5f 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73  the maximum.** s
17e60 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 73 74  ize of a cell st
17e61 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e 20 69  ored within an i
17e62 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20  nternal node is 
17e63 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68 61 6e  always less than
17e64 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70   1/4.** of the p
17e65 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f  age-size, the aO
17e66 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 66 65  vflSpace[] buffe
17e67 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  r is guaranteed 
17e68 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65  to be large.** e
17e69 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76  nough for all ov
17e6a 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a  erflow cells..**
17e6b 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 61 63  .** If aOvflSpac
17e6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6e 75  e is set to a nu
17e6d 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73  ll pointer, this
17e6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
17e6f 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  s .** SQLITE_NOM
17e70 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  EM..*/.static in
17e71 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  t balance_nonroo
17e72 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  t(.  MemPage *pP
17e73 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  arent,          
17e74 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70       /* Parent p
17e75 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 73 20  age of siblings 
17e76 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a  being balanced *
17e77 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 49  /.  int iParentI
17e78 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  dx,             
17e79 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
17e7a 22 74 68 65 20 70 61 67 65 22 20 69 6e 20 70 50  "the page" in pP
17e7b 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61  arent */.  u8 *a
17e7c 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20 20 20  OvflSpace,      
17e7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 61             /* pa
17e7e 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20 6f 66  ge-size bytes of
17e7f 20 73 70 61 63 65 20 66 6f 72 20 70 61 72 65 6e   space for paren
17e80 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20  t ovfl */.  int 
17e81 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20 20 20  isRoot          
17e82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17e83 72 75 65 20 69 66 20 70 50 61 72 65 6e 74 20 69  rue if pParent i
17e84 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f  s a root-page */
17e85 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  .){.  BtShared *
17e86 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  pBt;            
17e87 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20     /* The whole 
17e88 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
17e89 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20  t nCell = 0;    
17e8a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
17e8b 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
17e8c 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69   apCell[] */.  i
17e8d 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30  nt nMaxCells = 0
17e8e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;           /* A
17e8f 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
17e90 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c   apCell, szCell,
17e91 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74   aFrom. */.  int
17e92 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20   nNew = 0;      
17e93 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
17e94 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
17e95 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74  apNew[] */.  int
17e96 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20   nOld;          
17e97 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
17e98 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
17e99 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74  apOld[] */.  int
17e9a 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20   i, j, k;       
17e9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
17e9c 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
17e9d 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20  int nxDiv;      
17e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e9f 4e 65 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f  Next divider slo
17ea0 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43  t in pParent->aC
17ea1 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72  ell[] */.  int r
17ea2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
17ea3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
17ea4 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
17ea5 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63 74 69  u16 leafCorrecti
17ea6 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  on;          /* 
17ea7 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20  4 if pPage is a 
17ea8 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20  leaf.  0 if not 
17ea9 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74  */.  int leafDat
17eaa 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
17eab 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61    /* True if pPa
17eac 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20  ge is a leaf of 
17ead 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20  a LEAFDATA tree 
17eae 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53  */.  int usableS
17eaf 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20  pace;           
17eb0 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50    /* Bytes in pP
17eb1 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68  age beyond the h
17eb2 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70  eader */.  int p
17eb3 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20  ageFlags;       
17eb4 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
17eb5 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61   of pPage->aData
17eb6 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62  [0] */.  int sub
17eb7 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20  total;          
17eb8 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61        /* Subtota
17eb9 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65  l of bytes in ce
17eba 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20  lls on one page 
17ebb 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 31  */.  int iSpace1
17ebc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
17ebd 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65    /* First unuse
17ebe 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65  d byte of aSpace
17ebf 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76  1[] */.  int iOv
17ec0 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 20 20  flSpace = 0;    
17ec1 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75        /* First u
17ec2 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 4f  nused byte of aO
17ec3 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20  vflSpace[] */.  
17ec4 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20  int szScratch;  
17ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17ec6 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63 68 20  Size of scratch 
17ec7 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 65 64  memory requested
17ec8 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61   */.  MemPage *a
17ec9 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20  pOld[NB];       
17eca 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20     /* pPage and 
17ecb 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e  up to two siblin
17ecc 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  gs */.  MemPage 
17ecd 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20  *apCopy[NB];    
17ece 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20       /* Private 
17ecf 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b  copies of apOld[
17ed0 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d  ] pages */.  Mem
17ed1 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32  Page *apNew[NB+2
17ed2 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61  ];        /* pPa
17ed3 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20  ge and up to NB 
17ed4 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62  siblings after b
17ed5 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38  alancing */.  u8
17ed6 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20   *pRight;       
17ed7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
17ed8 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74  cation in parent
17ed9 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c 69 6e   of right-siblin
17eda 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75  g pointer */.  u
17edb 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20  8 *apDiv[NB-1]; 
17edc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
17edd 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20  ivider cells in 
17ede 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  pParent */.  int
17edf 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20   cntNew[NB+2];  
17ee0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
17ee1 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66  ex in aCell[] of
17ee2 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68   cell after i-th
17ee3 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73   page */.  int s
17ee4 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20  zNew[NB+2];     
17ee5 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69          /* Combi
17ee6 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c  ned size of cell
17ee7 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20  s place on i-th 
17ee8 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61  page */.  u8 **a
17ee9 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20  pCell = 0;      
17eea 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65         /* All ce
17eeb 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63  lls begin balanc
17eec 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43  ed */.  u16 *szC
17eed 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
17eee 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69       /* Local si
17eef 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20  ze of all cells 
17ef0 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  in apCell[] */. 
17ef1 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20   u8 *aSpace1;   
17ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17ef3 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70 69 65   Space for copie
17ef4 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 63 65  s of dividers ce
17ef5 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  lls */.  Pgno pg
17ef6 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
17ef7 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 76 61        /* Temp va
17ef8 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70 61 67  r to store a pag
17ef9 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a  e number in */..
17efa 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e 74 2d    pBt = pParent-
17efb 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20  >pBt;.  assert( 
17efc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
17efd 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
17efe 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17eff 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17f00 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  ble(pParent->pDb
17f01 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a  Page) );..#if 0.
17f02 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43    TRACE(("BALANC
17f03 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64  E: begin page %d
17f04 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c   child of %d\n",
17f05 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50   pPage->pgno, pP
17f06 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23  arent->pgno));.#
17f07 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74  endif..  /* At t
17f08 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65 6e  his point pParen
17f09 74 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d 6f  t may have at mo
17f0a 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20  st one overflow 
17f0b 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a  cell. And if.  *
17f0c 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20  * this overflow 
17f0d 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e 74 2c  cell is present,
17f0e 20 69 74 20 6d 75 73 74 20 62 65 20 74 68 65 20   it must be the 
17f0f 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20  cell with .  ** 
17f10 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49 64 78  index iParentIdx
17f11 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69 6f 20  . This scenario 
17f12 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e  comes about when
17f13 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20   this function. 
17f14 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 28 69   ** is called (i
17f15 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f 6d 20  ndirectly) from 
17f16 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65  sqlite3BtreeDele
17f17 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  te()..  */.  ass
17f18 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f  ert( pParent->nO
17f19 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50  verflow==0 || pP
17f1a 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
17f1b 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==1 );.  assert(
17f1c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66   pParent->nOverf
17f1d 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e  low==0 || pParen
17f1e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d  t->aOvfl[0].idx=
17f1f 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a  =iParentIdx );..
17f20 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70 61 63    if( !aOvflSpac
17f21 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
17f22 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
17f23 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  }..  /* Find the
17f24 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74   sibling pages t
17f25 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20  o balance. Also 
17f26 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c 73  locate the cells
17f27 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 20 2a   in pParent .  *
17f28 2a 20 74 68 61 74 20 64 69 76 69 64 65 20 74 68  * that divide th
17f29 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61  e siblings. An a
17f2a 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
17f2b 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e  o find NN siblin
17f2c 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68  gs on .  ** eith
17f2d 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65  er side of pPage
17f2e 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20  . More siblings 
17f2f 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f  are taken from o
17f30 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72  ne side, however
17f31 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65  , .  ** if there
17f32 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20   are fewer than 
17f33 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74  NN siblings on t
17f34 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 49  he other side. I
17f35 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68  f pParent.  ** h
17f36 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63  as NB or fewer c
17f37 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c  hildren then all
17f38 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61   children of pPa
17f39 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 20  rent are taken. 
17f3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73   .  **.  ** This
17f3b 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73   loop also drops
17f3c 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
17f3d 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65  ls from the pare
17f3e 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a 20 20  nt page. This.  
17f3f 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65 6d 61  ** way, the rema
17f40 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66 75 6e  inder of the fun
17f41 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68  ction does not h
17f42 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ave to deal with
17f43 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c   any.  ** overfl
17f44 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20  ow cells in the 
17f45 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73 69 6e  parent page, sin
17f46 63 65 20 69 66 20 61 6e 79 20 65 78 69 73 74 65  ce if any existe
17f47 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a  d they will.  **
17f48 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
17f49 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f  en removed..  */
17f4a 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e  .  i = pParent->
17f4b 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 61 72  nOverflow + pPar
17f4c 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66  ent->nCell;.  if
17f4d 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44  ( i<2 ){.    nxD
17f4e 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64  iv = 0;.    nOld
17f4f 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b   = i+1;.  }else{
17f50 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20  .    nOld = 3;. 
17f51 20 20 20 69 66 28 20 69 50 61 72 65 6e 74 49 64     if( iParentId
17f52 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  x==0 ){         
17f53 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 6e          .      n
17f54 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65  xDiv = 0;.    }e
17f55 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e 74 49  lse if( iParentI
17f56 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e  dx==i ){.      n
17f57 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20  xDiv = i-2;.    
17f58 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 78 44  }else{.      nxD
17f59 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64 78 2d  iv = iParentIdx-
17f5a 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d  1;.    }.    i =
17f5b 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 69   2;.  }.  if( (i
17f5c 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e  +nxDiv-pParent->
17f5d 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72  nOverflow)==pPar
17f5e 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  ent->nCell ){.  
17f5f 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 61 72    pRight = &pPar
17f60 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
17f61 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
17f62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
17f63 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c  Right = findCell
17f64 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69  (pParent, i+nxDi
17f65 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  v-pParent->nOver
17f66 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e  flow);.  }.  pgn
17f67 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 52 69  o = get4byte(pRi
17f68 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 31  ght);.  while( 1
17f69 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 74   ){.    rc = get
17f6a 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
17f6b 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d   pgno, &apOld[i]
17f6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
17f6d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70  .      memset(ap
17f6e 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69  Old, 0, (i+1)*si
17f6f 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b  zeof(MemPage*));
17f70 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61  .      goto bala
17f71 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
17f72 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73   }.    nMaxCells
17f73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e   += 1+apOld[i]->
17f74 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e  nCell+apOld[i]->
17f75 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69  nOverflow;.    i
17f76 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72  f( (i--)==0 ) br
17f77 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b  eak;..    if( i+
17f78 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e  nxDiv==pParent->
17f79 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20  aOvfl[0].idx && 
17f7a 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17f7b 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69  ow ){.      apDi
17f7c 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e  v[i] = pParent->
17f7d 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a  aOvfl[0].pCell;.
17f7e 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74        pgno = get
17f7f 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b  4byte(apDiv[i]);
17f80 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20  .      szNew[i] 
17f81 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
17f82 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29  arent, apDiv[i])
17f83 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
17f84 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >nOverflow = 0;.
17f85 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
17f86 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64   apDiv[i] = find
17f87 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b  Cell(pParent, i+
17f88 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e  nxDiv-pParent->n
17f89 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20  Overflow);.     
17f8a 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65   pgno = get4byte
17f8b 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20  (apDiv[i]);.    
17f8c 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c    szNew[i] = cel
17f8d 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74  lSizePtr(pParent
17f8e 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20  , apDiv[i]);..  
17f8f 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20      /* Drop the 
17f90 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 61  cell from the pa
17f91 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 76  rent page. apDiv
17f92 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73  [i] still points
17f93 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   to.      ** the
17f94 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68 65   cell within the
17f95 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74 68   parent, even th
17f96 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 65 6e  ough it has been
17f97 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 20 20   dropped..      
17f98 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66 65 20  ** This is safe 
17f99 62 65 63 61 75 73 65 20 64 72 6f 70 70 69 6e 67  because dropping
17f9a 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65   a cell only ove
17f9b 72 77 72 69 74 65 73 20 74 68 65 20 66 69 72 73  rwrites the firs
17f9c 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20  t.      ** four 
17f9d 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61 6e 64  bytes of it, and
17f9e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   this function d
17f9f 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 68 65  oes not need the
17fa0 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20   first.      ** 
17fa1 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 74 68  four bytes of th
17fa2 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20  e divider cell. 
17fa3 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69  So the pointer i
17fa4 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a 20 20  s safe to use.  
17fa5 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e      ** later on.
17fa6 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20    .      **.    
17fa7 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69    ** Unless SQLi
17fa8 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69  te is compiled i
17fa9 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20  n secure-delete 
17faa 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61  mode. In this ca
17fab 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  se,.      ** the
17fac 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74   dropCell() rout
17fad 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 72 69  ine will overwri
17fae 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 63 65  te the entire ce
17faf 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a  ll with zeroes..
17fb0 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73        ** In this
17fb1 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 72 69   case, temporari
17fb2 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65 6c 6c  ly copy the cell
17fb3 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 6c 53   into the aOvflS
17fb4 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20  pace[].      ** 
17fb5 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c 6c 20  buffer. It will 
17fb6 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20 61 67  be copied out ag
17fb7 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 20 74  ain as soon as t
17fb8 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75 66 66  he aSpace[] buff
17fb9 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61  er.      ** is a
17fba 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69  llocated.  */.#i
17fbb 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55  fdef SQLITE_SECU
17fbc 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20  RE_DELETE.      
17fbd 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61  memcpy(&aOvflSpa
17fbe 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72  ce[apDiv[i]-pPar
17fbf 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44  ent->aData], apD
17fc0 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29  iv[i], szNew[i])
17fc1 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d  ;.      apDiv[i]
17fc2 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61   = &aOvflSpace[a
17fc3 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d  pDiv[i]-pParent-
17fc4 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a  >aData];.#endif.
17fc5 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
17fc6 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d  Parent, i+nxDiv-
17fc7 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17fc8 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72  ow, szNew[i], &r
17fc9 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  c);.    }.  }.. 
17fca 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c   /* Make nMaxCel
17fcb 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66  ls a multiple of
17fcc 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70   4 in order to p
17fcd 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20  reserve 8-byte. 
17fce 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f   ** alignment */
17fcf 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28  .  nMaxCells = (
17fd0 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e  nMaxCells + 3)&~
17fd1 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c  3;..  /*.  ** Al
17fd2 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
17fd3 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
17fd4 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42  es.  */.  k = pB
17fd5 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 52 4f  t->pageSize + RO
17fd6 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50  UND8(sizeof(MemP
17fd7 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 61 74  age));.  szScrat
17fd8 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78  ch =.       nMax
17fd9 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a  Cells*sizeof(u8*
17fda 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
17fdb 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c          /* apCel
17fdc 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78  l */.     + nMax
17fdd 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36  Cells*sizeof(u16
17fde 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
17fdf 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c          /* szCel
17fe0 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d  l */.     + pBt-
17fe1 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20  >pageSize       
17fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fe3 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63          /* aSpac
17fe4 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e  e1 */.     + k*n
17fe5 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  Old;            
17fe6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17fe7 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
17fe8 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70 79 29   copies (apCopy)
17fe9 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73   */.  apCell = s
17fea 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
17feb 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29  loc( szScratch )
17fec 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d  ; .  if( apCell=
17fed 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
17fee 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
17fef 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
17ff0 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43  eanup;.  }.  szC
17ff1 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43  ell = (u16*)&apC
17ff2 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a  ell[nMaxCells];.
17ff3 20 20 61 53 70 61 63 65 31 20 3d 20 28 75 38 2a    aSpace1 = (u8*
17ff4 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c  )&szCell[nMaxCel
17ff5 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ls];.  assert( E
17ff6 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
17ff7 45 4e 54 28 61 53 70 61 63 65 31 29 20 29 3b 0a  ENT(aSpace1) );.
17ff8 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20  .  /*.  ** Load 
17ff9 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20  pointers to all 
17ffa 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67  cells on sibling
17ffb 20 70 61 67 65 73 20 61 6e 64 20 74 68 65 20 64   pages and the d
17ffc 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a  ivider cells.  *
17ffd 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c  * into the local
17ffe 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e   apCell[] array.
17fff 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 6f 66    Make copies of
18000 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
18001 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61  ls.  ** into spa
18002 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ce obtained from
18003 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 20 72   aSpace1[] and r
18004 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20 64 69  emove the the di
18005 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a  vider Cells.  **
18006 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20   from pParent.. 
18007 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
18008 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20  siblings are on 
18009 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68 65 6e  leaf pages, then
1800a 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69 6e 74   the child point
1800b 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ers of the.  ** 
1800c 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 61 72  divider cells ar
1800d 65 20 73 74 72 69 70 70 65 64 20 66 72 6f 6d 20  e stripped from 
1800e 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f 72 65  the cells before
1800f 20 74 68 65 79 20 61 72 65 20 63 6f 70 69 65 64   they are copied
18010 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63  .  ** into aSpac
18011 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77  e1[].  In this w
18012 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e  ay, all cells in
18013 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69   apCell[] are wi
18014 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64  thout.  ** child
18015 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 20 73   pointers.  If s
18016 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20  iblings are not 
18017 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c  leaves, then all
18018 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70   cell in.  ** ap
18019 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63  Cell[] include c
1801a 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20  hild pointers.  
1801b 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c 6c 20  Either way, all 
1801c 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b  cells in apCell[
1801d 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65  ].  ** are alike
1801e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66  ..  **.  ** leaf
1801f 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 20 69  Correction:  4 i
18020 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61  f pPage is a lea
18021 66 2e 20 20 30 20 69 66 20 70 50 61 67 65 20 69  f.  0 if pPage i
18022 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20  s not a leaf..  
18023 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44 61 74  **       leafDat
18024 61 3a 20 20 31 20 69 66 20 70 50 61 67 65 20 68  a:  1 if pPage h
18025 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 61 6e  olds key+data an
18026 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 73 20  d pParent holds 
18027 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a  only keys..  */.
18028 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e    leafCorrection
18029 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61   = apOld[0]->lea
1802a 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20  f*4;.  leafData 
1802b 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44  = apOld[0]->hasD
1802c 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ata;.  for(i=0; 
1802d 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20  i<nOld; i++){.  
1802e 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20    int limit;.   
1802f 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 65 20   .    /* Before 
18030 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 65  doing anything e
18031 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f 70 79  lse, take a copy
18032 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f 72 69   of the i'th ori
18033 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20  ginal sibling.  
18034 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66    ** The rest of
18035 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
18036 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f  ill use data fro
18037 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74  m the copies rat
18038 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  her.    ** that 
18039 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
1803a 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69  es since the ori
1803b 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c  ginal pages will
1803c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20 20 2a   be in the.    *
1803d 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69  * process of bei
1803e 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
1803f 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20   */.    MemPage 
18040 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69  *pOld = apCopy[i
18041 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26 61  ] = (MemPage*)&a
18042 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65  Space1[pBt->page
18043 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20  Size + k*i];.   
18044 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70   memcpy(pOld, ap
18045 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d  Old[i], sizeof(M
18046 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 70 4f  emPage));.    pO
18047 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f 69  ld->aData = (voi
18048 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20  d*)&pOld[1];.   
18049 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44   memcpy(pOld->aD
1804a 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61  ata, apOld[i]->a
1804b 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53  Data, pBt->pageS
1804c 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74  ize);..    limit
1804d 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70   = pOld->nCell+p
1804e 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a  Old->nOverflow;.
1804f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c      for(j=0; j<l
18050 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  imit; j++){.    
18051 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c    assert( nCell<
18052 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
18053 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     apCell[nCell]
18054 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43   = findOverflowC
18055 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20  ell(pOld, j);.  
18056 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c      szCell[nCell
18057 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  ] = cellSizePtr(
18058 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65  pOld, apCell[nCe
18059 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c  ll]);.      nCel
1805a 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  l++;.    }.    i
1805b 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21  f( i<nOld-1 && !
1805c 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20 20 20  leafData){.     
1805d 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36 29 73   u16 sz = (u16)s
1805e 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75  zNew[i];.      u
1805f 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20  8 *pTemp;.      
18060 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d  assert( nCell<nM
18061 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20  axCells );.     
18062 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   szCell[nCell] =
18063 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 6d 70   sz;.      pTemp
18064 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61   = &aSpace1[iSpa
18065 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 70 61  ce1];.      iSpa
18066 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20  ce1 += sz;.     
18067 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74   assert( sz<=pBt
18068 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a  ->pageSize/4 );.
18069 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53        assert( iS
1806a 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65  pace1<=pBt->page
1806b 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 6d 65  Size );.      me
1806c 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69  mcpy(pTemp, apDi
1806d 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20  v[i], sz);.     
1806e 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   apCell[nCell] =
1806f 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65   pTemp+leafCorre
18070 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73  ction;.      ass
18071 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74  ert( leafCorrect
18072 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f  ion==0 || leafCo
18073 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20  rrection==4 );. 
18074 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c       szCell[nCel
18075 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c  l] = szCell[nCel
18076 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 63 74  l] - leafCorrect
18077 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ion;.      if( !
18078 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  pOld->leaf ){.  
18079 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
1807a 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20  afCorrection==0 
1807b 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
1807c 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73  t( pOld->hdrOffs
1807d 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  et==0 );.       
1807e 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f   /* The right po
1807f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69  inter of the chi
18080 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63  ld page pOld bec
18081 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20  omes the left.  
18082 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72        ** pointer
18083 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20   of the divider 
18084 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20  cell */.        
18085 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43  memcpy(apCell[nC
18086 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61  ell], &pOld->aDa
18087 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20  ta[8], 4);.     
18088 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
18089 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72  assert( leafCorr
1808a 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20  ection==4 );.   
1808b 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b       if( szCell[
1808c 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20  nCell]<4 ){.    
1808d 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
1808e 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20  allow any cells 
1808f 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20 62  smaller than 4 b
18090 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ytes. */.       
18091 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     szCell[nCell]
18092 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 4;.        }.
18093 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43        }.      nC
18094 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ell++;.    }.  }
18095 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75  ..  /*.  ** Figu
18096 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62 65  re out the numbe
18097 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64 65  r of pages neede
18098 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43  d to hold all nC
18099 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20  ell cells..  ** 
1809a 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62 65  Store this numbe
1809b 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20  r in "k".  Also 
1809c 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20  compute szNew[] 
1809d 77 68 69 63 68 20 69 73 20 74 68 65 20 74 6f 74  which is the tot
1809e 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20  al.  ** size of 
1809f 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65  all cells on the
180a0 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20 63   i-th page and c
180a1 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73  ntNew[] which is
180a2 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20   the index.  ** 
180a3 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74  in apCell[] of t
180a4 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69 76  he cell that div
180a5 69 64 65 73 20 70 61 67 65 20 69 20 66 72 6f 6d  ides page i from
180a6 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a   page i+1.  .  *
180a7 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75  * cntNew[k] shou
180a8 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a  ld equal nCell..
180a9 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73    **.  ** Values
180aa 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 69   computed by thi
180ab 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20  s block:.  **.  
180ac 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a 20  **           k: 
180ad 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  The total number
180ae 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67 65   of sibling page
180af 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b  s.  **    szNew[
180b0 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64 20  i]: Spaced used 
180b1 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c  on the i-th sibl
180b2 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20  ing page..  **  
180b3 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65   cntNew[i]: Inde
180b4 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e  x in apCell[] an
180b5 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74  d szCell[] for t
180b6 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f  he first cell to
180b7 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .  **           
180b8 20 20 20 74 68 65 20 72 69 67 68 74 20 6f 66 20     the right of 
180b9 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67  the i-th sibling
180ba 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62   page..  ** usab
180bb 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 20  leSpace: Number 
180bc 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
180bd 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65  e available on e
180be 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a  ach sibling..  *
180bf 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65  * .  */.  usable
180c0 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73 61  Space = pBt->usa
180c1 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c  bleSize - 12 + l
180c2 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20  eafCorrection;. 
180c3 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d   for(subtotal=k=
180c4 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
180c5 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
180c6 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  i<nMaxCells );. 
180c7 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73     subtotal += s
180c8 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20  zCell[i] + 2;.  
180c9 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20 3e    if( subtotal >
180ca 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b 0a   usableSpace ){.
180cb 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d        szNew[k] =
180cc 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65   subtotal - szCe
180cd 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74  ll[i];.      cnt
180ce 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20  New[k] = i;.    
180cf 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 20 29    if( leafData )
180d0 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73  { i--; }.      s
180d1 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20  ubtotal = 0;.   
180d2 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66     k++;.      if
180d3 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d  ( k>NB+1 ){ rc =
180d4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
180d5 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
180d6 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20  eanup; }.    }. 
180d7 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20   }.  szNew[k] = 
180d8 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e  subtotal;.  cntN
180d9 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20  ew[k] = nCell;. 
180da 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a   k++;..  /*.  **
180db 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d   The packing com
180dc 70 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65  puted by the pre
180dd 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62  vious block is b
180de 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65  iased toward the
180df 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f   siblings.  ** o
180e0 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e  n the left side.
180e1 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69    The left sibli
180e2 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e  ngs are always n
180e3 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c  early full, whil
180e4 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74  e the.  ** right
180e5 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69  -most sibling mi
180e6 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d  ght be nearly em
180e7 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b  pty.  This block
180e8 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74   of code attempt
180e9 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74  s.  ** to adjust
180ea 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20   the packing of 
180eb 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20  siblings to get 
180ec 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65  a better balance
180ed 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
180ee 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d   adjustment is m
180ef 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69  ore than an opti
180f0 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70  mization.  The p
180f1 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67  acking above mig
180f2 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75  ht.  ** be so ou
180f3 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20  t of balance as 
180f4 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20  to be illegal.  
180f5 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
180f6 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a   right-most.  **
180f7 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62   sibling might b
180f8 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70  e completely emp
180f9 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74  ty.  This adjust
180fa 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69  ment is not opti
180fb 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  onal..  */.  for
180fc 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d  (i=k-1; i>0; i--
180fd 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67  ){.    int szRig
180fe 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20  ht = szNew[i];  
180ff 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69  /* Size of sibli
18100 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  ng on the right 
18101 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66  */.    int szLef
18102 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20  t = szNew[i-1]; 
18103 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69  /* Size of sibli
18104 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a  ng on the left *
18105 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20  /.    int r;    
18106 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
18107 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74  ex of right-most
18108 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69   cell in left si
18109 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74  bling */.    int
1810a 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   d;             
1810b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72   /* Index of fir
1810c 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c  st cell to the l
1810d 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62  eft of right sib
1810e 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d  ling */..    r =
1810f 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31   cntNew[i-1] - 1
18110 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20  ;.    d = r + 1 
18111 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20  - leafData;.    
18112 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65  assert( d<nMaxCe
18113 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72  lls );.    asser
18114 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( r<nMaxCells )
18115 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52  ;.    while( szR
18116 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67  ight==0 || szRig
18117 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d  ht+szCell[d]+2<=
18118 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72  szLeft-(szCell[r
18119 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a  ]+2) ){.      sz
1811a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b  Right += szCell[
1811b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a  d] + 2;.      sz
1811c 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72  Left -= szCell[r
1811d 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74  ] + 2;.      cnt
1811e 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20  New[i-1]--;.    
1811f 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31    r = cntNew[i-1
18120 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d  ] - 1;.      d =
18121 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74   r + 1 - leafDat
18122 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e  a;.    }.    szN
18123 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b  ew[i] = szRight;
18124 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20  .    szNew[i-1] 
18125 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20  = szLeft;.  }.. 
18126 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f   /* Either we fo
18127 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  und one or more 
18128 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d  cells (cntnew[0]
18129 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20 69 73  )>0) or pPage is
1812a 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20  .  ** a virtual 
1812b 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 76 69  root page.  A vi
1812c 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20  rtual root page 
1812d 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 61 6c  is when the real
1812e 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20   root.  ** page 
1812f 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 77 65  is page 1 and we
18130 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 63 68   are the only ch
18131 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 67 65  ild of that page
18132 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
18133 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20   cntNew[0]>0 || 
18134 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d  (pParent->pgno==
18135 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43  1 && pParent->nC
18136 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52  ell==0) );..  TR
18137 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f  ACE(("BALANCE: o
18138 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 22 2c  ld: %d %d %d  ",
18139 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70  .    apOld[0]->p
1813a 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d  gno, .    nOld>=
1813b 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67  2 ? apOld[1]->pg
1813c 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64  no : 0,.    nOld
1813d 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e  >=3 ? apOld[2]->
1813e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a  pgno : 0.  ));..
1813f 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61    /*.  ** Alloca
18140 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20  te k new pages. 
18141 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73   Reuse old pages
18142 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e   where possible.
18143 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c  .  */.  if( apOl
18144 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b  d[0]->pgno<=1 ){
18145 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
18146 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 67 6f  _CORRUPT;.    go
18147 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
18148 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c  up;.  }.  pageFl
18149 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e  ags = apOld[0]->
1814a 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28  aData[0];.  for(
1814b 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a  i=0; i<k; i++){.
1814c 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65      MemPage *pNe
1814d 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c  w;.    if( i<nOl
1814e 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20  d ){.      pNew 
1814f 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f  = apNew[i] = apO
18150 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f  ld[i];.      apO
18151 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20  ld[i] = 0;.     
18152 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
18153 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e 70 44  erWrite(pNew->pD
18154 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e  bPage);.      nN
18155 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ew++;.      if( 
18156 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63  rc ) goto balanc
18157 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  e_cleanup;.    }
18158 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
18159 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 20 20  rt( i>0 );.     
1815a 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
1815b 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e  reePage(pBt, &pN
1815c 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c  ew, &pgno, pgno,
1815d 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
1815e 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65  c ) goto balance
1815f 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20  _cleanup;.      
18160 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b  apNew[i] = pNew;
18161 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a  .      nNew++;..
18162 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65        /* Set the
18163 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
18164 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73  ry for the new s
18165 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a  ibling page. */.
18166 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f        if( ISAUTO
18167 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
18168 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
18169 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52   pNew->pgno, PTR
1816a 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65  MAP_BTREE, pPare
1816b 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a  nt->pgno, &rc);.
1816c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1816d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1816e 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61         goto bala
1816f 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
18170 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
18171 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
18172 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67 65  ree any old page
18173 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 20  s that were not 
18174 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 70 61  reused as new pa
18175 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  ges..  */.  whil
18176 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20  e( i<nOld ){.   
18177 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b   freePage(apOld[
18178 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66  i], &rc);.    if
18179 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
1817a 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
1817b 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f   releasePage(apO
1817c 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c  ld[i]);.    apOl
1817d 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b  d[i] = 0;.    i+
1817e 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  +;.  }..  /*.  *
1817f 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 70 61  * Put the new pa
18180 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67  ges in accending
18181 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68 65   order.  This he
18182 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70  lps to.  ** keep
18183 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
18184 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64  disk file in ord
18185 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 61  er so that a sca
18186 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61  n.  ** of the ta
18187 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 20  ble is a linear 
18188 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65  scan through the
18189 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a   file.  That.  *
1818a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73 20  * in turn helps 
1818b 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
1818c 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72 20  stem to deliver 
1818d 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20  pages.  ** from 
1818e 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72 61  the disk more ra
1818f 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  pidly..  **.  **
18190 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72   An O(n^2) inser
18191 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69  tion sort algori
18192 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75 74  thm is used, but
18193 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73   since.  ** n is
18194 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e   never more than
18195 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e   NB (a small con
18196 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68 6f  stant), that sho
18197 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20  uld.  ** not be 
18198 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a  a problem..  **.
18199 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c    ** When NB==3,
1819a 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69   this one optimi
1819b 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65  zation makes the
1819c 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 61   database.  ** a
1819d 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 72 20  bout 25% faster 
1819e 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72 74  for large insert
1819f 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69 6f  ions and deletio
181a0 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ns..  */.  for(i
181a1 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b  =0; i<k-1; i++){
181a2 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20  .    int minV = 
181a3 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a  apNew[i]->pgno;.
181a4 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69      int minI = i
181a5 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b  ;.    for(j=i+1;
181a6 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<k; j++){.    
181a7 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e    if( apNew[j]->
181a8 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d  pgno<(unsigned)m
181a9 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  inV ){.        m
181aa 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  inI = j;.       
181ab 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d   minV = apNew[j]
181ac 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  ->pgno;.      }.
181ad 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69      }.    if( mi
181ae 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e  nI>i ){.      in
181af 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61  t t;.      MemPa
181b0 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20  ge *pT;.      t 
181b1 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f  = apNew[i]->pgno
181b2 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e  ;.      pT = apN
181b3 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e  ew[i];.      apN
181b4 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69  ew[i] = apNew[mi
181b5 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77  nI];.      apNew
181b6 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20  [minI] = pT;.   
181b7 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28   }.  }.  TRACE((
181b8 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25 64 28  "new: %d(%d) %d(
181b9 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64  %d) %d(%d) %d(%d
181ba 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20  ) %d(%d)\n",.   
181bb 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c   apNew[0]->pgno,
181bc 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e   szNew[0],.    n
181bd 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31  New>=2 ? apNew[1
181be 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65  ]->pgno : 0, nNe
181bf 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20  w>=2 ? szNew[1] 
181c0 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33  : 0,.    nNew>=3
181c1 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e   ? apNew[2]->pgn
181c2 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f  o : 0, nNew>=3 ?
181c3 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20   szNew[2] : 0,. 
181c4 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e     nNew>=4 ? apN
181c5 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c  ew[3]->pgno : 0,
181c6 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77   nNew>=4 ? szNew
181c7 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65  [3] : 0,.    nNe
181c8 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d  w>=5 ? apNew[4]-
181c9 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e  >pgno : 0, nNew>
181ca 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20  =5 ? szNew[4] : 
181cb 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  0));..  assert( 
181cc 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
181cd 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d  iteable(pParent-
181ce 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 70  >pDbPage) );.  p
181cf 75 74 34 62 79 74 65 28 70 52 69 67 68 74 2c 20  ut4byte(pRight, 
181d0 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70  apNew[nNew-1]->p
181d1 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  gno);..  /*.  **
181d2 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75   Evenly distribu
181d3 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61  te the data in a
181d4 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74  pCell[] across t
181d5 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20  he new pages..  
181d6 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65  ** Insert divide
181d7 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61  r cells into pPa
181d8 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72  rent as necessar
181d9 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b  y..  */.  j = 0;
181da 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e  .  for(i=0; i<nN
181db 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a  ew; i++){.    /*
181dc 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65   Assemble the ne
181dd 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20  w sibling page. 
181de 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  */.    MemPage *
181df 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b  pNew = apNew[i];
181e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e  .    assert( j<n
181e1 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20  MaxCells );.    
181e2 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70  zeroPage(pNew, p
181e3 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 20 61  ageFlags);.    a
181e4 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77  ssemblePage(pNew
181e5 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26  , cntNew[i]-j, &
181e6 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65  apCell[j], &szCe
181e7 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65  ll[j]);.    asse
181e8 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e  rt( pNew->nCell>
181e9 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26  0 || (nNew==1 &&
181ea 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29   cntNew[0]==0) )
181eb 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ;.    assert( pN
181ec 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ew->nOverflow==0
181ed 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74   );..    j = cnt
181ee 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20  New[i];..    /* 
181ef 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70  If the sibling p
181f0 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62  age assembled ab
181f1 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20  ove was not the 
181f2 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69  right-most sibli
181f3 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72  ng,.    ** inser
181f4 74 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c  t a divider cell
181f5 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
181f6 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
181f7 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65 77    assert( i<nNew
181f8 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29  -1 || j==nCell )
181f9 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c  ;.    if( j<nCel
181fa 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  l ){.      u8 *p
181fb 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a  Cell;.      u8 *
181fc 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74  pTemp;.      int
181fd 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65   sz;..      asse
181fe 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20  rt( j<nMaxCells 
181ff 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  );.      pCell =
18200 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20   apCell[j];.    
18201 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d    sz = szCell[j]
18202 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f   + leafCorrectio
18203 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d  n;.      pTemp =
18204 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76   &aOvflSpace[iOv
18205 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20  flSpace];.      
18206 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20  if( !pNew->leaf 
18207 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
18208 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38  y(&pNew->aData[8
18209 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20  ], pCell, 4);.  
1820a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
1820b 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20  afData ){.      
1820c 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65    /* If the tree
1820d 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20   is a leaf-data 
1820e 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69  tree, and the si
1820f 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65  blings are leave
18210 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  s, .        ** t
18211 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hen there is no 
18212 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20  divider cell in 
18213 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61  apCell[]. Instea
18214 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a  d, the divider .
18215 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20          ** cell 
18216 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
18217 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  integer key for 
18218 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63  the right-most c
18219 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20  ell of .        
1821a 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70  ** the sibling-p
1821b 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62  age assembled ab
1821c 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20  ove only..      
1821d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c    */.        Cel
1821e 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20  lInfo info;.    
1821f 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20      j--;.       
18220 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
18221 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b  tr(pNew, apCell[
18222 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  j], &info);.    
18223 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d      pCell = pTem
18224 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20  p;.        sz = 
18225 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28 26 70  4 + putVarint(&p
18226 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b  Cell[4], info.nK
18227 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65  ey);.        pTe
18228 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  mp = 0;.      }e
18229 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65  lse{.        pCe
1822a 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20  ll -= 4;.       
1822b 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65   /* Obscure case
1822c 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61   for non-leaf-da
1822d 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65  ta trees: If the
1822e 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77   cell at pCell w
1822f 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72  as.        ** pr
18230 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20  eviously stored 
18231 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20  on a leaf node, 
18232 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64  and its reported
18233 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20   size was 4.    
18234 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68      ** bytes, th
18235 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c  en it may actual
18236 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68  ly be smaller th
18237 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  an this .       
18238 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65 50 61   ** (see btreePa
18239 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20  rseCellPtr(), 4 
1823a 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e  bytes is the min
1823b 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20  imum size of.   
1823c 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c       ** any cell
1823d 29 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70  ). But it is imp
1823e 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74  ortant to pass t
1823f 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20  he correct size 
18240 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  to .        ** i
18241 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20  nsertCell(), so 
18242 72 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c  reparse the cell
18243 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a   now..        **
18244 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  .        ** Note
18245 20 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e   that this can n
18246 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61  ever happen in a
18247 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69  n SQLite data fi
18248 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20  le, as all.     
18249 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20     ** cells are 
1824a 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
1824b 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e  . It only happen
1824c 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65  s in b-trees use
1824d 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  d.        ** to 
1824e 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45  evaluate "IN (SE
1824f 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73  LECT ...)" and s
18250 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a  imilar clauses..
18251 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
18252 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d     if( szCell[j]
18253 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==4 ){.         
18254 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72   assert(leafCorr
18255 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20  ection==4);.    
18256 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53        sz = cellS
18257 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20  izePtr(pParent, 
18258 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20  pCell);.        
18259 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1825a 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a  iOvflSpace += sz
1825b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1825c 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  sz<=pBt->pageSiz
1825d 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73  e/4 );.      ass
1825e 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63 65 3c  ert( iOvflSpace<
1825f 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29  =pBt->pageSize )
18260 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74 43 65  ;.      insertCe
18261 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69  ll(pParent, nxDi
18262 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54  v, pCell, sz, pT
18263 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c  emp, pNew->pgno,
18264 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28   &rc);.      if(
18265 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
18266 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
18267 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 73 73  eanup;.      ass
18268 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
18269 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
1826a 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29  rent->pDbPage) )
1826b 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20  ;..      j++;.  
1826c 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20      nxDiv++;.   
1826d 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
1826e 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61   j==nCell );.  a
1826f 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b  ssert( nOld>0 );
18270 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e  .  assert( nNew>
18271 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65  0 );.  if( (page
18272 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46  Flags & PTF_LEAF
18273 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a  )==0 ){.    u8 *
18274 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79  zChild = &apCopy
18275 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b  [nOld-1]->aData[
18276 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  8];.    memcpy(&
18277 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61  apNew[nNew-1]->a
18278 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c  Data[8], zChild,
18279 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20   4);.  }..  if( 
1827a 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72 65 6e  isRoot && pParen
1827b 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70  t->nCell==0 && p
1827c 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65  Parent->hdrOffse
1827d 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72  t<=apNew[0]->nFr
1827e 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ee ){.    /* The
1827f 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68   root page of th
18280 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e  e b-tree now con
18281 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20  tains no cells. 
18282 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67  The only sibling
18283 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20  .    ** page is 
18284 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
18285 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e 20 43  of the parent. C
18286 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  opy the contents
18287 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   of the.    ** c
18288 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f 20 74  hild page into t
18289 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63 72 65  he parent, decre
1828a 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72 61 6c  asing the overal
1828b 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 0a  l height of the.
1828c 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 73 74      ** b-tree st
1828d 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65 2e 20  ructure by one. 
1828e 54 68 69 73 20 69 73 20 64 65 73 63 72 69 62 65  This is describe
1828f 64 20 61 73 20 74 68 65 20 22 62 61 6c 61 6e 63  d as the "balanc
18290 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20  e-shallower".   
18291 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 74 68   ** sub-algorith
18292 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65  m in some docume
18293 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ntation..    **.
18294 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
18295 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
18296 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63   database, the c
18297 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43  all to copyNodeC
18298 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a  ontent() .    **
18299 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65   sets all pointe
1829a 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f  r-map entries co
1829b 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64  rresponding to d
1829c 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 70 61  atabase image pa
1829d 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20  ges .    ** for 
1829e 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e 74 65  which the pointe
1829f 72 20 69 73 20 73 74 6f 72 65 64 20 77 69 74 68  r is stored with
182a0 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62  in the content b
182a1 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 20 20  eing copied..   
182a2 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73   **.    ** The s
182a3 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62 65 6c  econd assert bel
182a4 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 61 74  ow verifies that
182a5 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
182a6 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65 64 0a  is defragmented.
182a7 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 74 20      ** (it must 
182a8 62 65 2c 20 61 73 20 69 74 20 77 61 73 20 6a 75  be, as it was ju
182a9 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64  st reconstructed
182aa 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c 65 50   using assembleP
182ab 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20 20 20  age()). This.   
182ac 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   ** is important
182ad 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 70   if the parent p
182ae 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62  age happens to b
182af 65 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 20  e page 1 of the 
182b0 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20  database.    ** 
182b1 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61  image.  */.    a
182b2 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29  ssert( nNew==1 )
182b3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 70  ;.    assert( ap
182b4 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d  New[0]->nFree ==
182b5 20 0a 20 20 20 20 20 20 20 20 28 67 65 74 32 62   .        (get2b
182b6 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61  yte(&apNew[0]->a
182b7 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30  Data[5])-apNew[0
182b8 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70  ]->cellOffset-ap
182b9 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29  New[0]->nCell*2)
182ba 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 6f 70   .    );.    cop
182bb 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e  yNodeContent(apN
182bc 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20  ew[0], pParent, 
182bd 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65 50 61  &rc);.    freePa
182be 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63  ge(apNew[0], &rc
182bf 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 49  );.  }else if( I
182c0 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20  SAUTOVACUUM ){. 
182c1 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 70 6f     /* Fix the po
182c2 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65  inter-map entrie
182c3 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 63 65  s for all the ce
182c4 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20 73 68  lls that were sh
182c5 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20  ifted around. . 
182c6 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65 20     ** There are 
182c7 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e  several differen
182c8 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69 6e 74  t types of point
182c9 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 74  er-map entries t
182ca 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20  hat need to.    
182cb 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68  ** be dealt with
182cc 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
182cd 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 65 20  . Some of these 
182ce 68 61 76 65 20 62 65 65 6e 20 73 65 74 20 61 6c  have been set al
182cf 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20 20 2a  ready, but.    *
182d0 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e  * many have not.
182d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
182d2 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20  s a summary:.   
182d3 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 29 20   **.    **   1) 
182d4 54 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  The entries asso
182d5 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 20  ciated with new 
182d6 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 68  sibling pages th
182d7 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 20 20  at were not.    
182d8 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e 67 73  **      siblings
182d9 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
182da 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 20  ion was called. 
182db 54 68 65 73 65 20 68 61 76 65 20 61 6c 72 65 61  These have alrea
182dc 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62  dy.    **      b
182dd 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f 6e 27  een set. We don'
182de 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20  t need to worry 
182df 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e  about old siblin
182e0 67 73 20 74 68 61 74 20 77 65 72 65 0a 20 20 20  gs that were.   
182e1 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 20 74   **      moved t
182e2 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  o the free-list 
182e3 2d 20 74 68 65 20 66 72 65 65 50 61 67 65 28 29  - the freePage()
182e4 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65 6e 20   code has taken 
182e5 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  care.    **     
182e6 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a   of those..    *
182e7 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54 68  *.    **   2) Th
182e8 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
182e9 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
182ea 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
182eb 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20  overflow.    ** 
182ec 20 20 20 20 20 70 61 67 65 20 69 6e 20 61 6e 79       page in any
182ed 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 73   overflow chains
182ee 20 75 73 65 64 20 62 79 20 6e 65 77 20 64 69 76   used by new div
182ef 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 65 73  ider cells. Thes
182f0 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 68  e .    **      h
182f1 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 64 79  ave also already
182f2 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 72 65   been taken care
182f3 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73 65 72   of by the inser
182f4 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20  tCell() code..  
182f5 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29    **.    **   3)
182f6 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20   If the sibling 
182f7 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65  pages are not le
182f8 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63  aves, then the c
182f9 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a 20 20  hild pages of.  
182fa 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c 73 20    **      cells 
182fb 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 73 69  stored on the si
182fc 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 79 20  bling pages may 
182fd 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74  need to be updat
182fe 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed..    **.    *
182ff 2a 20 20 20 34 29 20 49 66 20 74 68 65 20 73 69  *   4) If the si
18300 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20  bling pages are 
18301 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74  not internal int
18302 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20  key nodes, then 
18303 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  any.    **      
18304 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 75  overflow pages u
18305 73 65 64 20 62 79 20 74 68 65 73 65 20 63 65 6c  sed by these cel
18306 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  ls may need to b
18307 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a  e updated.    **
18308 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61 6c 20        (internal 
18309 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76  intkey nodes nev
1830a 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74  er contain point
1830b 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20  ers to overflow 
1830c 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20  pages)..    **. 
1830d 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20 74 68     **   5) If th
1830e 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  e sibling pages 
1830f 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20  are not leaves, 
18310 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72  then the pointer
18311 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 20 20  -map.    **     
18312 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65   entries for the
18313 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 61 67   right-child pag
18314 65 73 20 6f 66 20 65 61 63 68 20 73 69 62 6c 69  es of each sibli
18315 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20  ng may need.    
18316 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20 75 70  **      to be up
18317 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  dated..    **.  
18318 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61 6e 64    ** Cases 1 and
18319 20 32 20 61 72 65 20 64 65 61 6c 74 20 77 69 74   2 are dealt wit
1831a 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68 65 72  h above by other
1831b 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 74 0a   code. The next.
1831c 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61      ** block dea
1831d 6c 73 20 77 69 74 68 20 63 61 73 65 73 20 33 20  ls with cases 3 
1831e 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20 6f 6e  and 4 and the on
1831f 65 20 61 66 74 65 72 20 74 68 61 74 2c 20 63 61  e after that, ca
18320 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 20 20  se 5. Since.    
18321 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70 6f 69  ** setting a poi
18322 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 69  nter map entry i
18323 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20 65  s a relatively e
18324 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69  xpensive operati
18325 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  on, this.    ** 
18326 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 20 70  code only sets p
18327 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69  ointer map entri
18328 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f 72 20  es for child or 
18329 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74  overflow pages t
1832a 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a 2a 20  hat have.    ** 
1832b 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62  actually moved b
1832c 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20 20 2a  etween pages.  *
1832d 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
1832e 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a  New = apNew[0];.
1832f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c      MemPage *pOl
18330 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20  d = apCopy[0];. 
18331 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77     int nOverflow
18332 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c   = pOld->nOverfl
18333 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e 65 78  ow;.    int iNex
18334 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65  tOld = pOld->nCe
18335 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a  ll + nOverflow;.
18336 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f      int iOverflo
18337 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f  w = (nOverflow ?
18338 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e   pOld->aOvfl[0].
18339 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a  idx : -1);.    j
1833a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1833b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1833c 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c    /* Current 'ol
1833d 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  d' sibling page 
1833e 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 20 20  */.    k = 0;   
1833f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
18341 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62 6c 69  rent 'new' sibli
18342 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 66  ng page */.    f
18343 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
18344 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
18345 20 69 73 44 69 76 69 64 65 72 20 3d 20 30 3b 0a   isDivider = 0;.
18346 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 3d 3d        while( i==
18347 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20  iNextOld ){.    
18348 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73      /* Cell i is
18349 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69   the cell immedi
1834a 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
1834b 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e  the last cell on
1834c 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   old.        ** 
1834d 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20  sibling page j. 
1834e 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20  If the siblings 
1834f 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67  are not leaf pag
18350 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20  es of an.       
18351 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65   ** intkey b-tre
18352 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 77  e, then cell i w
18353 61 73 20 61 20 64 69 76 69 64 65 72 20 63 65 6c  as a divider cel
18354 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f  l. */.        pO
18355 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d  ld = apCopy[++j]
18356 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 4f  ;.        iNextO
18357 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61  ld = i + !leafDa
18358 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c  ta + pOld->nCell
18359 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c   + pOld->nOverfl
1835a 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ow;.        if( 
1835b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  pOld->nOverflow 
1835c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 76  ){.          nOv
1835d 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e  erflow = pOld->n
1835e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20  Overflow;.      
1835f 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20      iOverflow = 
18360 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20  i + !leafData + 
18361 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  pOld->aOvfl[0].i
18362 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  dx;.        }.  
18363 20 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20        isDivider 
18364 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20  = !leafData;  . 
18365 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73       }..      as
18366 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30  sert(nOverflow>0
18367 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20   || iOverflow<i 
18368 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
18369 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70  nOverflow<2 || p
1836a 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64  Old->aOvfl[0].id
1836b 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31  x==pOld->aOvfl[1
1836c 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20 20  ].idx-1);.      
1836d 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77  assert(nOverflow
1836e 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66  <3 || pOld->aOvf
1836f 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e  l[1].idx==pOld->
18370 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b  aOvfl[2].idx-1);
18371 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 69 4f  .      if( i==iO
18372 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
18373 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 31     isDivider = 1
18374 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 2d  ;.        if( (-
18375 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b  -nOverflow)>0 ){
18376 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72  .          iOver
18377 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20  flow++;.        
18378 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
18379 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b   if( i==cntNew[k
1837a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ] ){.        /* 
1837b 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65  Cell i is the ce
1837c 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66  ll immediately f
1837d 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73  ollowing the las
1837e 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20  t cell on new.  
1837f 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67        ** sibling
18380 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68 65 20   page k. If the 
18381 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74  siblings are not
18382 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61   leaf pages of a
18383 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74  n.        ** int
18384 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e  key b-tree, then
18385 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64 69 76   cell i is a div
18386 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20  ider cell.  */. 
18387 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70         pNew = ap
18388 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20  New[++k];.      
18389 20 20 69 66 28 20 21 6c 65 61 66 44 61 74 61 20    if( !leafData 
1838a 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1838b 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
1838c 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20  ( j<nOld );.    
1838d 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77    assert( k<nNew
1838e 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66   );..      /* If
1838f 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6f 72   the cell was or
18390 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 65 72  iginally divider
18391 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f   cell (and is no
18392 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 20 20  t now) or.      
18393 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63  ** an overflow c
18394 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 20 63  ell, or if the c
18395 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 64 20  ell was located 
18396 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 73  on a different s
18397 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  ibling.      ** 
18398 70 61 67 65 20 62 65 66 6f 72 65 20 74 68 65 20  page before the 
18399 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20  balancing, then 
1839a 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
1839b 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
1839c 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68  ed.      ** with
1839d 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 6f 76   any child or ov
1839e 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e 65 65  erflow pages nee
1839f 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e  d to be updated.
183a0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69    */.      if( i
183a1 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64  sDivider || pOld
183a2 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67  ->pgno!=pNew->pg
183a3 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  no ){.        if
183a4 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  ( !leafCorrectio
183a5 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  n ){.          p
183a6 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65  trmapPut(pBt, ge
183a7 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d  t4byte(apCell[i]
183a8 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  ), PTRMAP_BTREE,
183a9 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63   pNew->pgno, &rc
183aa 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
183ab 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b       if( szCell[
183ac 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61  i]>pNew->minLoca
183ad 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  l ){.          p
183ae 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28  trmapPutOvflPtr(
183af 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c  pNew, apCell[i],
183b0 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d   &rc);.        }
183b1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
183b2 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72      if( !leafCor
183b3 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20  rection ){.     
183b4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77   for(i=0; i<nNew
183b5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
183b6 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 62 79  u32 key = get4by
183b7 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44  te(&apNew[i]->aD
183b8 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20  ata[8]);.       
183b9 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
183ba 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  key, PTRMAP_BTRE
183bb 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e  E, apNew[i]->pgn
183bc 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 7d  o, &rc);.      }
183bd 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20  .    }..#if 0.  
183be 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 70 43    /* The ptrmapC
183bf 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f 6e 74  heckPages() cont
183c0 61 69 6e 73 20 61 73 73 65 72 74 28 29 20 73 74  ains assert() st
183c1 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 76 65  atements that ve
183c2 72 69 66 79 20 74 68 61 74 0a 20 20 20 20 2a 2a  rify that.    **
183c3 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70   all pointer map
183c4 20 70 61 67 65 73 20 61 72 65 20 73 65 74 20 63   pages are set c
183c5 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73 20 69  orrectly. This i
183c6 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c 65 20  s helpful while 
183c7 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67 69 6e  .    ** debuggin
183c8 67 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c  g. This is usual
183c9 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65 63 61  ly disabled beca
183ca 75 73 65 20 61 20 63 6f 72 72 75 70 74 20 64 61  use a corrupt da
183cb 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20 20 2a  tabase may.    *
183cc 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73 65 72  * cause an asser
183cd 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  t() statement to
183ce 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70   fail.  */.    p
183cf 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28  trmapCheckPages(
183d0 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20  apNew, nNew);.  
183d1 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67    ptrmapCheckPag
183d2 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31 29 3b  es(&pParent, 1);
183d3 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61  .#endif.  }..  a
183d4 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
183d5 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 41 43  isInit );.  TRAC
183d6 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e  E(("BALANCE: fin
183d7 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65  ished: old=%d ne
183d8 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22  w=%d cells=%d\n"
183d9 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 6c 64  ,.          nOld
183da 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b  , nNew, nCell));
183db 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61  ..  /*.  ** Clea
183dc 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72  nup before retur
183dd 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e  ning..  */.balan
183de 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  ce_cleanup:.  sq
183df 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65  lite3ScratchFree
183e0 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28  (apCell);.  for(
183e1 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b  i=0; i<nOld; i++
183e2 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
183e3 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20  ge(apOld[i]);.  
183e4 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  }.  for(i=0; i<n
183e5 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72  New; i++){.    r
183e6 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e 65 77  eleasePage(apNew
183e7 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  [i]);.  }..  ret
183e8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
183e9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
183ea 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
183eb 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
183ec 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75  a b-tree structu
183ed 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c  re is.** overful
183ee 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f  l (has one or mo
183ef 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  re overflow page
183f0 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20  s)..**.** A new 
183f1 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c  child page is al
183f2 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20  located and the 
183f3 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
183f4 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20  current root.** 
183f5 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  page, including 
183f6 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20  overflow cells, 
183f7 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  are copied into 
183f8 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72  the child. The r
183f9 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74  oot.** page is t
183fa 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20  hen overwritten 
183fb 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d  to make it an em
183fc 70 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68  pty page with th
183fd 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a  e right-child .*
183fe 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69  * pointer pointi
183ff 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61  ng to the new pa
18400 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65  ge..**.** Before
18401 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20   returning, all 
18402 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
18403 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ies correspondin
18404 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74  g to pages .** t
18405 68 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c  hat the new chil
18406 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61  d-page now conta
18407 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ins pointers to 
18408 61 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65  are updated. The
18409 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73  .** entry corres
1840a 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e  ponding to the n
1840b 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70  ew right-child p
1840c 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f  ointer of the ro
1840d 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c  ot.** page is al
1840e 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a  so updated..**.*
1840f 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
18410 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74   *ppChild is set
18411 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65   to contain a re
18412 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63  ference to the c
18413 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e  hild .** page an
18414 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  d SQLITE_OK is r
18415 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
18416 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72   case the caller
18417 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20   is required.** 
18418 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50  to call releaseP
18419 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c  age() on *ppChil
1841a 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20  d exactly once. 
1841b 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1841c 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20  rs,.** an error 
1841d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
1841e 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73   and *ppChild is
1841f 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74   set to 0..*/.st
18420 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65  atic int balance
18421 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20  _deeper(MemPage 
18422 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20  *pRoot, MemPage 
18423 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e  **ppChild){.  in
18424 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
18425 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18426 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  Return value fro
18427 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20  m subprocedures 
18428 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43  */.  MemPage *pC
18429 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20  hild = 0;       
1842a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1842b 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61  o a new child pa
1842c 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  ge */.  Pgno pgn
1842d 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20  oChild = 0;     
1842e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
1842f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77  umber of the new
18430 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20   child page */. 
18431 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
18432 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20   pRoot->pBt;    
18433 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a  /* The BTree */.
18434 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74  .  assert( pRoot
18435 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b  ->nOverflow>0 );
18436 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
18437 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
18438 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  t->mutex) );..  
18439 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74  /* Make pRoot, t
1843a 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
1843b 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74  the b-tree, writ
1843c 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61  able. Allocate a
1843d 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20   new .  ** page 
1843e 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65  that will become
1843f 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63   the new right-c
18440 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43  hild of pPage. C
18441 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  opy the contents
18442 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64  .  ** of the nod
18443 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f  e stored on pRoo
18444 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63  t into the new c
18445 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  hild page..  */.
18446 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
18447 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e  gerWrite(pRoot->
18448 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
18449 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1844a 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  .    rc = alloca
1844b 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1844c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69  &pChild,&pgnoChi
1844d 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30  ld,pRoot->pgno,0
1844e 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43  );.    copyNodeC
1844f 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43  ontent(pRoot, pC
18450 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20  hild, &rc);.    
18451 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
18452 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70   ){.      ptrmap
18453 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69  Put(pBt, pgnoChi
18454 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  ld, PTRMAP_BTREE
18455 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26  , pRoot->pgno, &
18456 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rc);.    }.  }. 
18457 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a   if( rc ){.    *
18458 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20  ppChild = 0;.   
18459 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68   releasePage(pCh
1845a 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ild);.    return
1845b 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
1845c 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1845d 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c  swriteable(pChil
1845e 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  d->pDbPage) );. 
1845f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18460 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
18461 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
18462 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
18463 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f  hild->nCell==pRo
18464 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20  ot->nCell );..  
18465 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
18466 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e   copy root %d in
18467 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d  to %d\n", pRoot-
18468 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70  >pgno, pChild->p
18469 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70  gno));..  /* Cop
1846a 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  y the overflow c
1846b 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20  ells from pRoot 
1846c 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d  to pChild */.  m
1846d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f  emcpy(pChild->aO
1846e 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66  vfl, pRoot->aOvf
1846f 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66  l, pRoot->nOverf
18470 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74  low*sizeof(pRoot
18471 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20  ->aOvfl[0]));.  
18472 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  pChild->nOverflo
18473 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72  w = pRoot->nOver
18474 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f  flow;..  /* Zero
18475 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
18476 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73   pRoot. Then ins
18477 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 20 74  tall pChild as t
18478 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 2e 20  he right-child. 
18479 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52  */.  zeroPage(pR
1847a 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61  oot, pChild->aDa
1847b 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41  ta[0] & ~PTF_LEA
1847c 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  F);.  put4byte(&
1847d 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f  pRoot->aData[pRo
1847e 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ot->hdrOffset+8]
1847f 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20  , pgnoChild);.. 
18480 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 68 69   *ppChild = pChi
18481 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ld;.  return SQL
18482 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
18483 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 70   The page that p
18484 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  Cur currently po
18485 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73 74  ints to has just
18486 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 69   been modified i
18487 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54  n.** some way. T
18488 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 67  his function fig
18489 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 69 73  ures out if this
1848a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 65   modification me
1848b 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65 20  ans the.** tree 
1848c 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61  needs to be bala
1848d 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f 20  nced, and if so 
1848e 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f 70  calls the approp
1848f 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67 20  riate balancing 
18490 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c  .** routine. Bal
18491 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  ancing routines 
18492 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c  are:.**.**   bal
18493 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20  ance_quick().** 
18494 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72    balance_deeper
18495 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f  ().**   balance_
18496 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61  nonroot().*/.sta
18497 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28  tic int balance(
18498 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
18499 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1849a 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69  TE_OK;.  const i
1849b 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e  nt nMin = pCur->
1849c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1849d 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 61 42  * 2 / 3;.  u8 aB
1849e 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
1849f 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65  [13];.  u8 *pFre
184a0 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e  e = 0;..  TESTON
184a1 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  LY( int balance_
184a2 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30  quick_called = 0
184a3 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20   );.  TESTONLY( 
184a4 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70  int balance_deep
184a5 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b  er_called = 0 );
184a6 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74  ..  do {.    int
184a7 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69   iPage = pCur->i
184a8 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67  Page;.    MemPag
184a9 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d  e *pPage = pCur-
184aa 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a  >apPage[iPage];.
184ab 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 3d 3d  .    if( iPage==
184ac 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
184ad 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
184ae 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
184af 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
184b0 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f 76 65  he b-tree is ove
184b1 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63  rfull. In this c
184b2 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 20 20  ase call the.   
184b3 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f       ** balance_
184b4 64 65 65 70 65 72 28 29 20 66 75 6e 63 74 69 6f  deeper() functio
184b5 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  n to create a ne
184b6 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68 65 20  w child for the 
184b7 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20  root-page.      
184b8 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 74 68    ** and copy th
184b9 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e  e current conten
184ba 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70  ts of the root-p
184bb 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65 0a 20  age to it. The. 
184bc 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20 69         ** next i
184bd 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
184be 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c  do-loop will bal
184bf 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64 20 70  ance the child p
184c0 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20  age..        */ 
184c1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
184c2 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72   (balance_deeper
184c3 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b  _called++)==0 );
184c4 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61  .        rc = ba
184c5 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70 50 61  lance_deeper(pPa
184c6 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67  ge, &pCur->apPag
184c7 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 69  e[1]);.        i
184c8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
184c9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
184ca 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20  ur->iPage = 1;. 
184cb 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61           pCur->a
184cc 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20  iIdx[0] = 0;.   
184cd 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49         pCur->aiI
184ce 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20  dx[1] = 0;.     
184cf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
184d0 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f  r->apPage[1]->nO
184d1 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20  verflow );.     
184d2 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
184d3 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
184d4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
184d5 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f  se if( pPage->nO
184d6 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50  verflow==0 && pP
184d7 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e  age->nFree<=nMin
184d8 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   ){.      break;
184d9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
184da 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73    MemPage * cons
184db 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72  t pParent = pCur
184dc 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 2d 31  ->apPage[iPage-1
184dd 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e  ];.      int con
184de 73 74 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e  st iIdx = pCur->
184df 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a  aiIdx[iPage-1];.
184e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
184e1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
184e2 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b  arent->pDbPage);
184e3 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
184e4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e  QLITE_OK ){.#ifn
184e5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
184e6 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20  QUICKBALANCE.   
184e7 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
184e8 68 61 73 44 61 74 61 0a 20 20 20 20 20 20 20 20  hasData.        
184e9 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72   && pPage->nOver
184ea 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 20 20  flow==1.        
184eb 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c   && pPage->aOvfl
184ec 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e  [0].idx==pPage->
184ed 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 26  nCell.         &
184ee 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21  & pParent->pgno!
184ef 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  =1.         && p
184f0 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69  Parent->nCell==i
184f1 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  Idx.        ){. 
184f2 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c           /* Call
184f3 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29   balance_quick()
184f4 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
184f5 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 61 67   sibling of pPag
184f6 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 20 20  e on which.     
184f7 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65       ** to store
184f8 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
184f9 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63  ll. balance_quic
184fa 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20 6e 65  k() inserts a ne
184fb 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 20  w cell.         
184fc 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 6e 74   ** into pParent
184fd 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73  , which may caus
184fe 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72 66 6c  e pParent overfl
184ff 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20  ow. If this.    
18500 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73        ** happens
18501 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74 65 72  , the next inter
18502 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d  ation of the do-
18503 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63  loop will balanc
18504 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20 20 20  e pParent .     
18505 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69 74 68       ** use eith
18506 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  er balance_nonro
18507 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f  ot() or balance_
18508 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69 6c 20  deeper(). Until 
18509 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  this.          *
1850a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 6f  * happens, the o
1850b 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20  verflow cell is 
1850c 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 42  stored in the aB
1850d 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
1850e 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  [].          ** 
1850f 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20 20 20  buffer. .       
18510 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20     **.          
18511 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f  ** The purpose o
18512 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
18513 61 73 73 65 72 74 28 29 20 69 73 20 74 6f 20 63  assert() is to c
18514 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61  heck that only a
18515 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69  .          ** si
18516 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c  ngle call to bal
18517 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73 20  ance_quick() is 
18518 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20 63 61  made for each ca
18519 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 20 20  ll to this.     
1851a 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
1851b 2e 20 49 66 20 74 68 69 73 20 77 65 72 65 20 6e  . If this were n
1851c 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61 20 73  ot verified, a s
1851d 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76  ubtle bug involv
1851e 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20 20 20  ing reuse.      
1851f 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 61 42      ** of the aB
18520 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
18521 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b 20 69  [] might sneak i
18522 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  n..          */.
18523 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
18524 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  ( (balance_quick
18525 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b  _called++)==0 );
18526 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
18527 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 50  balance_quick(pP
18528 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 61 42  arent, pPage, aB
18529 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65  alanceQuickSpace
1852a 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
1852b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
1852c 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
1852d 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63 61 6c  n this case, cal
1852e 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  l balance_nonroo
1852f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72 69 62  t() to redistrib
18530 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 20 20  ute cells.      
18531 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 70      ** between p
18532 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 32  Page and up to 2
18533 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 67 20   of its sibling 
18534 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e 76 6f  pages. This invo
18535 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a  lves.          *
18536 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20  * modifying the 
18537 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 61 72  contents of pPar
18538 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63  ent, which may c
18539 61 75 73 65 20 70 50 61 72 65 6e 74 20 74 6f 0a  ause pParent to.
1853a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63            ** bec
1853b 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20  ome overfull or 
1853c 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e  underfull. The n
1853d 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
1853e 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20   the do-loop.   
1853f 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62         ** will b
18540 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e  alance the paren
18541 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72 65 63  t page to correc
18542 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20 20 20  t this..        
18543 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 20 20    ** .          
18544 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74  ** If the parent
18545 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76   page becomes ov
18546 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72  erfull, the over
18547 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c  flow cell or cel
18548 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ls.          ** 
18549 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  are stored in th
1854a 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20  e pSpace buffer 
1854b 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 64 69  allocated immedi
1854c 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20  ately below. .  
1854d 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 75 62          ** A sub
1854e 73 65 71 75 65 6e 74 20 69 74 65 72 61 74 69 6f  sequent iteratio
1854f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70  n of the do-loop
18550 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 68 20   will deal with 
18551 74 68 69 73 20 62 79 0a 20 20 20 20 20 20 20 20  this by.        
18552 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c    ** calling bal
18553 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28  ance_nonroot() (
18554 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29  balance_deeper()
18555 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 66   may be called f
18556 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20  irst,.          
18557 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 6e 27  ** but it doesn'
18558 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76 65 72  t deal with over
18559 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73  flow cells - jus
1855a 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 6f 20  t moves them to 
1855b 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64  a.          ** d
1855c 69 66 66 65 72 65 6e 74 20 70 61 67 65 29 2e 20  ifferent page). 
1855d 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73 65 71  Once this subseq
1855e 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c  uent call to bal
1855f 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a  ance_nonroot() .
18560 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73            ** has
18561 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 20 69   completed, it i
18562 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65 61 73  s safe to releas
18563 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  e the pSpace buf
18564 66 65 72 20 75 73 65 64 20 62 79 0a 20 20 20 20  fer used by.    
18565 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65        ** the pre
18566 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 20 74  vious call, as t
18567 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
18568 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76 65 20   data will have 
18569 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20  been .          
1856a 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68 65 72  ** copied either
1856b 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f   into the body o
1856c 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  f a database pag
1856d 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6e 65  e or into the ne
1856e 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70  w.          ** p
1856f 53 70 61 63 65 20 62 75 66 66 65 72 20 70 61 73  Space buffer pas
18570 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74 74 65  sed to the latte
18571 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63  r call to balanc
18572 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20  e_nonroot()..   
18573 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
18574 20 20 20 20 75 38 20 2a 70 53 70 61 63 65 20 3d      u8 *pSpace =
18575 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
18576 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61  oc(pCur->pBt->pa
18577 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20  geSize);.       
18578 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f     rc = balance_
18579 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c  nonroot(pParent,
1857a 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c 20 69   iIdx, pSpace, i
1857b 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20  Page==1);.      
1857c 20 20 20 20 69 66 28 20 70 46 72 65 65 20 29 7b      if( pFree ){
1857d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
1857e 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f 74 20  If pFree is not 
1857f 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20  NULL, it points 
18580 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75  to the pSpace bu
18581 66 66 65 72 20 75 73 65 64 20 0a 20 20 20 20 20  ffer used .     
18582 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61 20 70         ** by a p
18583 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20  revious call to 
18584 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
18585 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20  ). Its contents 
18586 61 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  are.            
18587 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 65 69  ** now stored ei
18588 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 61 74  ther on real dat
18589 61 62 61 73 65 20 70 61 67 65 73 20 6f 72 20 77  abase pages or w
1858a 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20 20 20  ithin the .     
1858b 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 70 53         ** new pS
1858c 70 61 63 65 20 62 75 66 66 65 72 2c 20 73 6f 20  pace buffer, so 
1858d 69 74 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79  it may be safely
1858e 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a   freed here. */.
1858f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
18590 74 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65  te3PageFree(pFre
18591 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  e);.          }.
18592 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
18593 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20  e pSpace buffer 
18594 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 66  will be freed af
18595 74 65 72 20 74 68 65 20 6e 65 78 74 20 63 61 6c  ter the next cal
18596 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a  l to.          *
18597 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  * balance_nonroo
18598 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 65 66  t(), or just bef
18599 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
1859a 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69 63 68  n returns, which
1859b 65 76 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a  ever.          *
1859c 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a  * comes first. *
1859d 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 65  /.          pFre
1859e 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20  e = pSpace;.    
1859f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
185a0 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65       pPage->nOve
185a1 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20  rflow = 0;..    
185a2 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 74    /* The next it
185a3 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
185a4 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20  o-loop balances 
185a5 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
185a6 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73   */.      releas
185a7 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
185a8 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d      pCur->iPage-
185a9 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c  -;.    }.  }whil
185aa 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
185ab 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 65 65   );..  if( pFree
185ac 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   ){.    sqlite3P
185ad 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a  ageFree(pFree);.
185ae 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
185af 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  .}.../*.** Inser
185b0 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69  t a new record i
185b1 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20  nto the BTree.  
185b2 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e  The key is given
185b3 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a   by (pKey,nKey).
185b4 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20  ** and the data 
185b5 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61  is given by (pDa
185b6 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20  ta,nData).  The 
185b7 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f  cursor is used o
185b8 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65  nly to.** define
185b9 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20   what table the 
185ba 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65  record should be
185bb 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20   inserted into. 
185bc 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   The cursor.** i
185bd 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
185be 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61  at a random loca
185bf 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  tion..**.** For 
185c0 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c  an INTKEY table,
185c1 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76   only the nKey v
185c2 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20  alue of the key 
185c3 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69  is used.  pKey i
185c4 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46  s.** ignored.  F
185c5 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61  or a ZERODATA ta
185c6 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61  ble, the pData a
185c7 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74  nd nData are bot
185c8 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  h ignored..**.**
185c9 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75   If the seekResu
185ca 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  lt parameter is 
185cb 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61  non-zero, then a
185cc 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
185cd 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70   to.** MovetoUnp
185ce 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65 6b 20  acked() to seek 
185cf 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f 20 28  cursor pCur to (
185d0 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 73 20  pKey, nKey) has 
185d1 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20  already.** been 
185d2 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52  performed. seekR
185d3 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 65 61  esult is the sea
185d4 72 63 68 20 72 65 73 75 6c 74 20 72 65 74 75 72  rch result retur
185d5 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76 65 0a  ned (a negative.
185d6 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 43 75  ** number if pCu
185d7 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65  r points at an e
185d8 6e 74 72 79 20 74 68 61 74 20 69 73 20 73 6d 61  ntry that is sma
185d9 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 79 2c  ller than (pKey,
185da 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20   nKey), or.** a 
185db 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69  positive value i
185dc 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74  f pCur points at
185dd 20 61 6e 20 65 74 72 79 20 74 68 61 74 20 69 73   an etry that is
185de 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a   larger than .**
185df 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20   (pKey, nKey)). 
185e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65  .**.** If the se
185e1 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74  ekResult paramet
185e2 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  er is non-zero, 
185e3 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20  then the caller 
185e4 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a  guarantees that.
185e5 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72 20 69  ** cursor pCur i
185e6 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68  s pointing at th
185e7 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70 79 20  e existing copy 
185e8 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20 69 73  of a row that is
185e9 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72   to be.** overwr
185ea 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 20 73  itten.  If the s
185eb 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65  eekResult parame
185ec 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 63  ter is 0, then c
185ed 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a  ursor pCur may.*
185ee 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65  * point to any e
185ef 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e  ntry or to no en
185f0 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 20 73  try at all and s
185f1 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
185f2 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74  has to seek.** t
185f3 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65  he cursor before
185f4 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e   the new key can
185f5 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f   be inserted..*/
185f6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
185f7 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
185f8 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73  Insert(.  BtCurs
185f9 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20  or *pCur,       
185fa 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65           /* Inse
185fb 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  rt data into the
185fc 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63   table of this c
185fd 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  ursor */.  const
185fe 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
185ff 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65   nKey,    /* The
18600 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20   key of the new 
18601 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73  record */.  cons
18602 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69  t void *pData, i
18603 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68  nt nData,  /* Th
18604 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65  e data of the ne
18605 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  w record */.  in
18606 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20  t nZero,        
18607 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18608 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20  Number of extra 
18609 30 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e  0 bytes to appen
1860a 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69  d to data */.  i
1860b 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20  nt appendBias,  
1860c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1860d 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
1860e 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e   likely an appen
1860f 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52  d */.  int seekR
18610 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 20 20  esult           
18611 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
18612 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55  of prior MovetoU
18613 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a  npacked() call *
18614 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
18615 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52   int loc = seekR
18616 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20  esult;          
18617 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 64 65  /* -1: before de
18618 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20  sired location  
18619 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 20 69  +1: after */.  i
1861a 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20  nt szNew = 0;.  
1861b 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61  int idx;.  MemPa
1861c 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72  ge *pPage;.  Btr
1861d 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42  ee *p = pCur->pB
1861e 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64  tree;.  BtShared
1861f 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
18620 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
18621 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69  *oldCell;.  unsi
18622 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65  gned char *newCe
18623 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70  ll = 0;..  if( p
18624 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
18625 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20  SOR_FAULT ){.   
18626 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73   assert( pCur->s
18627 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f  kipNext!=SQLITE_
18628 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  OK );.    return
18629 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
1862a 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1862b 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1862c 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
1862d 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  rt( pCur->wrFlag
1862e 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
1862f 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
18630 49 54 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61  ITE && !pBt->rea
18631 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72  dOnly );.  asser
18632 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68  t( hasSharedCach
18633 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43  eTableLock(p, pC
18634 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43  ur->pgnoRoot, pC
18635 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c  ur->pKeyInfo!=0,
18636 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73   2) );..  /* Ass
18637 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c  ert that the cal
18638 6c 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e  ler has been con
18639 73 69 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73  sistent. If this
1863a 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e   cursor was open
1863b 65 64 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e  ed.  ** expectin
1863c 67 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65  g an index b-tre
1863d 65 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  e, then the call
1863e 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73  er should be ins
1863f 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a  erting blob.  **
18640 20 6b 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73   keys with no as
18641 73 6f 63 69 61 74 65 64 20 64 61 74 61 2e 20 49  sociated data. I
18642 66 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73  f the cursor was
18643 20 6f 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e   opened expectin
18644 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79  g an.  ** intkey
18645 20 74 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c   table, the call
18646 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73  er should be ins
18647 65 72 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b  erting integer k
18648 65 79 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20  eys with a.  ** 
18649 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74  blob of associat
1864a 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61  ed data.  */.  a
1864b 73 73 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29  ssert( (pKey==0)
1864c 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  ==(pCur->pKeyInf
1864d 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  o==0) );..  /* I
1864e 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73  f this is an ins
1864f 65 72 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65  ert into a table
18650 20 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64   b-tree, invalid
18651 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62  ate any incrblob
18652 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f   .  ** cursors o
18653 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62  pen on the row b
18654 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61  eing replaced (a
18655 73 73 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20  ssuming this is 
18656 61 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f  a replace.  ** o
18657 70 65 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74  peration - if it
18658 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c   is not, the fol
18659 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f  lowing is a no-o
1865a 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  p).  */.  if( pC
1865b 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20  ur->pKeyInfo==0 
1865c 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74  ){.    invalidat
1865d 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73  eIncrblobCursors
1865e 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20  (p, nKey, 0);.  
1865f 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65  }..  /* Save the
18660 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e   positions of an
18661 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20  y other cursors 
18662 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62  open on this tab
18663 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  le..  **.  ** In
18664 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65   some cases, the
18665 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f   call to btreeMo
18666 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20  veto() below is 
18667 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a  a no-op. For.  *
18668 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20  * example, when 
18669 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69  inserting data i
1866a 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68  nto a table with
1866b 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20   auto-generated 
1866c 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79  integer.  ** key
1866d 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65  s, the VDBE laye
1866e 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65  r invokes sqlite
1866f 33 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20  3BtreeLast() to 
18670 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a  figure out the .
18671 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79    ** integer key
18672 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e   to use. It then
18673 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63   calls this func
18674 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79  tion to actually
18675 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a   insert the .  *
18676 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  * data into the 
18677 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49  intkey B-Tree. I
18678 6e 20 74 68 69 73 20 63 61 73 65 20 62 74 72 65  n this case btre
18679 65 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e  eMoveto() recogn
1867a 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74  izes.  ** that t
1867b 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72  he cursor is alr
1867c 65 61 64 79 20 77 68 65 72 65 20 69 74 20 6e 65  eady where it ne
1867d 65 64 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65  eds to be and re
1867e 74 75 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20  turns without.  
1867f 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72  ** doing any wor
18680 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61  k. To avoid thwa
18681 72 74 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69  rting these opti
18682 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73  mizations, it is
18683 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20   important.  ** 
18684 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65  not to clear the
18685 20 63 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20   cursor here..  
18686 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  */.  rc = saveAl
18687 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43  lCursors(pBt, pC
18688 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43  ur->pgnoRoot, pC
18689 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ur);.  if( rc ) 
1868a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28  return rc;.  if(
1868b 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20   !loc ){.    rc 
1868c 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43  = btreeMoveto(pC
1868d 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20  ur, pKey, nKey, 
1868e 61 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63  appendBias, &loc
1868f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
18690 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
18691 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
18692 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
18693 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53  LID || (pCur->eS
18694 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56  tate==CURSOR_INV
18695 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a  ALID && loc) );.
18696 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
18697 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18698 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  age];.  assert( 
18699 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c  pPage->intKey ||
1869a 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73   nKey>=0 );.  as
1869b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61  sert( pPage->lea
1869c 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74  f || !pPage->int
1869d 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28  Key );..  TRACE(
1869e 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d  ("INSERT: table=
1869f 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61  %d nkey=%lld nda
186a0 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73  ta=%d page=%d %s
186a1 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
186a2 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e  Cur->pgnoRoot, n
186a3 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67  Key, nData, pPag
186a4 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20  e->pgno,.       
186a5 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65     loc==0 ? "ove
186a6 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65  rwrite" : "new e
186a7 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72  ntry"));.  asser
186a8 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
186a9 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65   );.  allocateTe
186aa 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20  mpSpace(pBt);.  
186ab 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70  newCell = pBt->p
186ac 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20  TmpSpace;.  if( 
186ad 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74  newCell==0 ) ret
186ae 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
186af 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43  ;.  rc = fillInC
186b0 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65  ell(pPage, newCe
186b1 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20  ll, pKey, nKey, 
186b2 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a  pData, nData, nZ
186b3 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20  ero, &szNew);.  
186b4 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
186b5 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65  d_insert;.  asse
186b6 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53  rt( szNew==cellS
186b7 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65  izePtr(pPage, ne
186b8 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65  wCell) );.  asse
186b9 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45  rt( szNew<=MX_CE
186ba 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a  LL_SIZE(pBt) );.
186bb 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69    idx = pCur->ai
186bc 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
186bd 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29  ;.  if( loc==0 )
186be 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b  {.    u16 szOld;
186bf 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78  .    assert( idx
186c0 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  <pPage->nCell );
186c1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
186c2 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
186c3 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
186c4 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
186c5 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74   goto end_insert
186c6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43  ;.    }.    oldC
186c7 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
186c8 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20  Page, idx);.    
186c9 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
186ca 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
186cb 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c  (newCell, oldCel
186cc 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20  l, 4);.    }.   
186cd 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a   szOld = cellSiz
186ce 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43  ePtr(pPage, oldC
186cf 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63  ell);.    rc = c
186d0 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20  learCell(pPage, 
186d1 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72  oldCell);.    dr
186d2 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  opCell(pPage, id
186d3 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a  x, szOld, &rc);.
186d4 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
186d5 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20  o end_insert;.  
186d6 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20  }else if( loc<0 
186d7 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e  && pPage->nCell>
186d8 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
186d9 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a   pPage->leaf );.
186da 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72      idx = ++pCur
186db 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
186dc 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  age];.  }else{. 
186dd 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
186de 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20  ->leaf );.  }.  
186df 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65  insertCell(pPage
186e0 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20  , idx, newCell, 
186e1 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63  szNew, 0, 0, &rc
186e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
186e3 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
186e4 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20  age->nCell>0 || 
186e5 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
186e6 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e  >0 );..  /* If n
186e7 6f 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  o error has occu
186e8 72 65 64 20 61 6e 64 20 70 50 61 67 65 20 68 61  red and pPage ha
186e9 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65  s an overflow ce
186ea 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65  ll, call balance
186eb 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69  () .  ** to redi
186ec 73 74 72 69 62 75 74 65 20 74 68 65 20 63 65 6c  stribute the cel
186ed 6c 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 72  ls within the tr
186ee 65 65 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63  ee. Since balanc
186ef 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a  e() may move.  *
186f0 2a 20 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65  * the cursor, ze
186f1 72 6f 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e  ro the BtCursor.
186f2 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42  info.nSize and B
186f3 74 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65  tCursor.validNKe
186f4 79 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73  y.  ** variables
186f5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76  ..  **.  ** Prev
186f6 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66  ious versions of
186f7 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d   SQLite called m
186f8 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d  oveToRoot() to m
186f9 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20  ove the cursor. 
186fa 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20   ** back to the 
186fb 72 6f 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c  root page as bal
186fc 61 6e 63 65 28 29 20 75 73 65 64 20 74 6f 20 69  ance() used to i
186fd 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f  nvalidate the co
186fe 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42  ntents.  ** of B
186ff 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d  tCursor.apPage[]
18700 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69   and BtCursor.ai
18701 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f  Idx[]. Instead o
18702 66 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20  f doing that,.  
18703 2a 2a 20 73 65 74 20 74 68 65 20 63 75 72 73 6f  ** set the curso
18704 72 20 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61  r state to "inva
18705 6c 69 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73  lid". This makes
18706 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f   common insert o
18707 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73  perations.  ** s
18708 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a  lightly faster..
18709 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20    **.  ** There 
1870a 69 73 20 61 20 73 75 62 74 6c 65 20 62 75 74 20  is a subtle but 
1870b 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69  important optimi
1870c 7a 61 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e  zation here too.
1870d 20 57 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a   When inserting.
1870e 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65    ** multiple re
1870f 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e  cords into an in
18710 74 6b 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e  tkey b-tree usin
18711 67 20 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f  g a single curso
18712 72 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68  r (as can.  ** h
18713 61 70 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63  appen while proc
18714 65 73 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52  essing an "INSER
18715 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43  T INTO ... SELEC
18716 54 22 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69  T" statement), i
18717 74 0a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74  t.  ** is advant
18718 61 67 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20  ageous to leave 
18719 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  the cursor point
1871a 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20  ing to the last 
1871b 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68  entry in.  ** th
1871c 65 20 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73  e b-tree if poss
1871d 69 62 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72  ible. If the cur
1871e 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
1871f 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74  ting to the last
18720 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74  .  ** entry in t
18721 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68  he table, and th
18722 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72  e next row inser
18723 74 65 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67  ted has an integ
18724 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67  er key.  ** larg
18725 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  er than the larg
18726 65 73 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79  est existing key
18727 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  , it is possible
18728 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20   to insert the. 
18729 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20   ** row without 
1872a 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73  seeking the curs
1872b 6f 72 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  or. This can be 
1872c 61 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63  a big performanc
1872d 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20  e boost..  */.  
1872e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1872f 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61   = 0;.  pCur->va
18730 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69  lidNKey = 0;.  i
18731 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
18732 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72   && pPage->nOver
18733 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d  flow ){.    rc =
18734 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a   balance(pCur);.
18735 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b  .    /* Must mak
18736 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77  e sure nOverflow
18737 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72   is reset to zer
18738 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61  o even if the ba
18739 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66  lance().    ** f
1873a 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64  ails. Internal d
1873b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f  ata structure co
1873c 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65  rruption will re
1873d 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20  sult otherwise. 
1873e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65  .    ** Also, se
1873f 74 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61  t the cursor sta
18740 74 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54  te to invalid. T
18741 68 69 73 20 73 74 6f 70 73 20 73 61 76 65 43 75  his stops saveCu
18742 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20  rsorPosition(). 
18743 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e     ** from tryin
18744 67 20 74 6f 20 73 61 76 65 20 74 68 65 20 63 75  g to save the cu
18745 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f  rrent position o
18746 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a  f the cursor.  *
18747 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61  /.    pCur->apPa
18748 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
18749 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >nOverflow = 0;.
1874a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
1874b 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
1874c 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  D;.  }.  assert(
1874d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1874e 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65  ur->iPage]->nOve
1874f 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64  rflow==0 );..end
18750 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72  _insert:.  retur
18751 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
18752 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20  elete the entry 
18753 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
18754 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  is pointing to. 
18755 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   The cursor.** i
18756 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
18757 61 74 20 61 20 61 72 62 69 74 72 61 72 79 20 6c  at a arbitrary l
18758 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ocation..*/.SQLI
18759 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1875a 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
1875b 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
1875c 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20  ){.  Btree *p = 
1875d 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20  pCur->pBtree;.  
1875e 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1875f 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20  p->pBt;         
18760 20 20 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20       .  int rc; 
18761 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18762 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18763 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
18764 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
18765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18766 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f        /* Page to
18767 20 64 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f   delete cell fro
18768 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  m */.  unsigned 
18769 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20  char *pCell;    
1876a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1876b 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74  ointer to cell t
1876c 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e  o delete */.  in
1876d 74 20 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20  t iCellIdx;     
1876e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1876f 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
18770 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ell to delete */
18771 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74  .  int iCellDept
18772 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
18773 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68          /* Depth
18774 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e   of node contain
18775 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20  ing pCell */ .. 
18776 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
18777 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
18778 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
18779 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1877a 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
1877b 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
1877c 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73  readOnly );.  as
1877d 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c  sert( pCur->wrFl
1877e 61 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ag );.  assert( 
1877f 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61  hasSharedCacheTa
18780 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d  bleLock(p, pCur-
18781 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d  >pgnoRoot, pCur-
18782 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29  >pKeyInfo!=0, 2)
18783 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68   );.  assert( !h
18784 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
18785 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  p, pCur->pgnoRoo
18786 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56  t) );..  if( NEV
18787 45 52 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  ER(pCur->aiIdx[p
18788 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75  Cur->iPage]>=pCu
18789 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1878a 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a  iPage]->nCell) .
1878b 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72     || NEVER(pCur
1878c 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52  ->eState!=CURSOR
1878d 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20  _VALID).  ){.   
1878e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1878f 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68  RROR;  /* Someth
18790 69 6e 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72  ing has gone awr
18791 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20  y. */.  }..  /* 
18792 49 66 20 74 68 69 73 20 69 73 20 61 20 64 65 6c  If this is a del
18793 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  ete operation to
18794 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72   remove a row fr
18795 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65  om a table b-tre
18796 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61  e,.  ** invalida
18797 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20  te any incrblob 
18798 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  cursors open on 
18799 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65  the row being de
1879a 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28  leted.  */.  if(
1879b 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d   pCur->pKeyInfo=
1879c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69  =0 ){.    invali
1879d 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73  dateIncrblobCurs
1879e 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66  ors(p, pCur->inf
1879f 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a  o.nKey, 0);.  }.
187a0 0a 20 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20  .  iCellDepth = 
187a1 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69  pCur->iPage;.  i
187a2 43 65 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e  CellIdx = pCur->
187a3 61 69 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68  aiIdx[iCellDepth
187a4 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  ];.  pPage = pCu
187a5 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44  r->apPage[iCellD
187a6 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d  epth];.  pCell =
187a7 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
187a8 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f   iCellIdx);..  /
187a9 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 63 6f  * If the page co
187aa 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74  ntaining the ent
187ab 72 79 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20  ry to delete is 
187ac 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c  not a leaf page,
187ad 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63   move.  ** the c
187ae 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72  ursor to the lar
187af 67 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  gest entry in th
187b0 65 20 74 72 65 65 20 74 68 61 74 20 69 73 20 73  e tree that is s
187b1 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a  maller than.  **
187b2 20 74 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67   the entry being
187b3 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63   deleted. This c
187b4 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65  ell will replace
187b5 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20   the cell being 
187b6 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f  deleted.  ** fro
187b7 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  m the internal n
187b8 6f 64 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f  ode. The 'previo
187b9 75 73 27 20 65 6e 74 72 79 20 69 73 20 75 73 65  us' entry is use
187ba 64 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65  d for this inste
187bb 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27  ad.  ** of the '
187bc 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20  next' entry, as 
187bd 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74  the previous ent
187be 72 79 20 69 73 20 61 6c 77 61 79 73 20 61 20 70  ry is always a p
187bf 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  art of the.  ** 
187c0 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
187c1 62 79 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  by the child pag
187c2 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65  e of the cell be
187c3 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69  ing deleted. Thi
187c4 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c  s makes.  ** bal
187c5 61 6e 63 69 6e 67 20 74 68 65 20 74 72 65 65 20  ancing the tree 
187c6 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65  following the de
187c7 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65  lete operation e
187c8 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28  asier.  */.  if(
187c9 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
187ca 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64  .    int notUsed
187cb 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
187cc 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
187cd 70 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b  pCur, &notUsed);
187ce 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
187cf 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
187d0 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69  /* Save the posi
187d1 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68  tions of any oth
187d2 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  er cursors open 
187d3 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 62 65  on this table be
187d4 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67  fore.  ** making
187d5 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f   any modificatio
187d6 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67  ns. Make the pag
187d7 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
187d8 20 65 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20   entry to be .  
187d9 2a 2a 20 64 65 6c 65 74 65 64 20 77 72 69 74 61  ** deleted writa
187da 62 6c 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61  ble. Then free a
187db 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ny overflow page
187dc 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
187dd 68 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72  h the .  ** entr
187de 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65  y and finally re
187df 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74  move the cell it
187e0 73 65 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e  self from within
187e1 20 74 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a   the page.  .  *
187e2 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c  /.  rc = saveAll
187e3 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75  Cursors(pBt, pCu
187e4 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75  r->pgnoRoot, pCu
187e5 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  r);.  if( rc ) r
187e6 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
187e7 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
187e8 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
187e9 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  e);.  if( rc ) r
187ea 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
187eb 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65   clearCell(pPage
187ec 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70  , pCell);.  drop
187ed 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
187ee 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74  lIdx, cellSizePt
187ef 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c  r(pPage, pCell),
187f0 20 26 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20   &rc);.  if( rc 
187f1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
187f2 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64  /* If the cell d
187f3 65 6c 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c  eleted was not l
187f4 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66  ocated on a leaf
187f5 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20   page, then the 
187f6 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63  cursor.  ** is c
187f7 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
187f8 67 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74  g to the largest
187f9 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75   entry in the su
187fa 62 2d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20  b-tree headed.  
187fb 2a 2a 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d  ** by the child-
187fc 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  page of the cell
187fd 20 74 68 61 74 20 77 61 73 20 6a 75 73 74 20 64   that was just d
187fe 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69  eleted from an i
187ff 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64  nternal.  ** nod
18800 65 2e 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d  e. The cell from
18801 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e   the leaf node n
18802 65 65 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64  eeds to be moved
18803 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   to the internal
18804 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65  .  ** node to re
18805 70 6c 61 63 65 20 74 68 65 20 64 65 6c 65 74 65  place the delete
18806 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66  d cell.  */.  if
18807 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
18808 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  {.    MemPage *p
18809 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50  Leaf = pCur->apP
1880a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1880b 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b  ;.    int nCell;
1880c 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43  .    Pgno n = pC
1880d 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c  ur->apPage[iCell
1880e 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a  Depth+1]->pgno;.
1880f 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
18810 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43  r *pTmp;..    pC
18811 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
18812 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65  Leaf, pLeaf->nCe
18813 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c  ll-1);.    nCell
18814 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
18815 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Leaf, pCell);.  
18816 20 20 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c    assert( MX_CEL
18817 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65  L_SIZE(pBt)>=nCe
18818 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63  ll );..    alloc
18819 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  ateTempSpace(pBt
1881a 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42  );.    pTmp = pB
1881b 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20  t->pTmpSpace;.. 
1881c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1881d 61 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d  agerWrite(pLeaf-
1881e 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
1881f 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c  nsertCell(pPage,
18820 20 69 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c   iCellIdx, pCell
18821 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d  -4, nCell+4, pTm
18822 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20  p, n, &rc);.    
18823 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20  dropCell(pLeaf, 
18824 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20  pLeaf->nCell-1, 
18825 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20  nCell, &rc);.   
18826 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
18827 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42   rc;.  }..  /* B
18828 61 6c 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e  alance the tree.
18829 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 64 65   If the entry de
1882a 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65  leted was locate
1882b 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65  d on a leaf page
1882c 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20  ,.  ** then the 
1882d 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69  cursor still poi
1882e 6e 74 73 20 74 6f 20 74 68 61 74 20 70 61 67 65  nts to that page
1882f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
18830 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61  he first.  ** ca
18831 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20  ll to balance() 
18832 72 65 70 61 69 72 73 20 74 68 65 20 74 72 65 65  repairs the tree
18833 2c 20 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e  , and the if(...
18834 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20  ) condition is. 
18835 20 2a 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a   ** never true..
18836 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77    **.  ** Otherw
18837 69 73 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72  ise, if the entr
18838 79 20 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e  y deleted was on
18839 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64   an internal nod
1883a 65 20 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a  e page, then.  *
1883b 2a 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69  * pCur is pointi
1883c 6e 67 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70  ng to the leaf p
1883d 61 67 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61  age from which a
1883e 20 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65   cell was remove
1883f 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63  d to.  ** replac
18840 65 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74  e the cell delet
18841 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ed from the inte
18842 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20  rnal node. This 
18843 69 73 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a  is slightly.  **
18844 20 74 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c   tricky as the l
18845 65 61 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20  eaf node may be 
18846 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74  underfull, and t
18847 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
18848 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74   may.  ** be eit
18849 68 65 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65  her under or ove
1884a 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63  rfull. In this c
1884b 61 73 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61  ase run the bala
1884c 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a  ncing algorithm.
1884d 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66    ** on the leaf
1884e 20 6e 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20   node first. If 
1884f 74 68 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63  the balance proc
18850 65 65 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20  eeds far enough 
18851 75 70 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65  up the.  ** tree
18852 20 74 68 61 74 20 77 65 20 63 61 6e 20 62 65 20   that we can be 
18853 73 75 72 65 20 74 68 61 74 20 61 6e 79 20 70 72  sure that any pr
18854 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74  oblem in the int
18855 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20  ernal node has. 
18856 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74   ** been correct
18857 65 64 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74  ed, so be it. Ot
18858 68 65 72 77 69 73 65 2c 20 61 66 74 65 72 20 62  herwise, after b
18859 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61  alancing the lea
1885a 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c  f node,.  ** wal
1885b 6b 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20  k the cursor up 
1885c 74 68 65 20 74 72 65 65 20 74 6f 20 74 68 65 20  the tree to the 
1885d 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e  internal node an
1885e 64 20 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20  d balance it as 
1885f 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a  .  ** well.  */.
18860 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70    rc = balance(p
18861 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  Cur);.  if( rc==
18862 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75  SQLITE_OK && pCu
18863 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65  r->iPage>iCellDe
18864 70 74 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  pth ){.    while
18865 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43  ( pCur->iPage>iC
18866 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20  ellDepth ){.    
18867 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
18868 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18869 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20  >iPage--]);.    
1886a 7d 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e  }.    rc = balan
1886b 63 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20  ce(pCur);.  }.. 
1886c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1886d 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f  OK ){.    moveTo
1886e 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a  Root(pCur);.  }.
1886f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
18870 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
18871 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20  ew BTree table. 
18872 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54   Write into *piT
18873 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a  able the page.**
18874 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
18875 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
18876 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   new table..**.*
18877 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79  * The type of ty
18878 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  pe is determined
18879 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61   by the flags pa
1887a 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74  rameter.  Only t
1887b 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
1887c 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20  values of flags 
1887d 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  are currently in
1887e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c   use.  Other val
1887f 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73  ues for.** flags
18880 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a   might not work:
18881 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45  .**.**     BTREE
18882 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45  _INTKEY|BTREE_LE
18883 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20  AFDATA     Used 
18884 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77  for SQL tables w
18885 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a  ith rowid keys.*
18886 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f  *     BTREE_ZERO
18887 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20  DATA            
18888 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53        Used for S
18889 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74  QL indices.*/.st
1888a 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72  atic int btreeCr
1888b 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20  eateTable(Btree 
1888c 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65  *p, int *piTable
1888d 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1888e 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1888f 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
18890 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f  e *pRoot;.  Pgno
18891 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74   pgnoRoot;.  int
18892 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
18893 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
18894 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61  sMutex(p) );.  a
18895 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
18896 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
18897 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
18898 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
18899 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  ly );..#ifdef SQ
1889a 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1889b 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f  CUUM.  rc = allo
1889c 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
1889d 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f  t, &pRoot, &pgno
1889e 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69  Root, 1, 0);.  i
1889f 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
188a0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73  urn rc;.  }.#els
188a1 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  e.  if( pBt->aut
188a2 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50  oVacuum ){.    P
188a3 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20  gno pgnoMove;   
188a4 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67     /* Move a pag
188a5 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72  e here to make r
188a6 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  oom for the root
188a7 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d  -page */.    Mem
188a8 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b  Page *pPageMove;
188a9 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20   /* The page to 
188aa 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20  move to. */..   
188ab 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e   /* Creating a n
188ac 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f  ew table may pro
188ad 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f  bably require mo
188ae 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  ving an existing
188af 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a   database.    **
188b0 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f   to make room fo
188b1 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73  r the new tables
188b2 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63   root page. In c
188b3 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75  ase this page tu
188b4 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74  rns.    ** out t
188b5 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77  o be an overflow
188b6 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c   page, delete al
188b7 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  l overflow page-
188b8 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a  map caches.    *
188b9 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63  * held by open c
188ba 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  ursors..    */. 
188bb 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c     invalidateAll
188bc 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42  OverflowCache(pB
188bd 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64  t);..    /* Read
188be 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65   the value of me
188bf 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64  ta[3] from the d
188c0 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72  atabase to deter
188c1 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20  mine where the. 
188c2 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20     ** root page 
188c3 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
188c4 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61   should go. meta
188c5 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65  [3] is the large
188c6 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
188c7 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66   ** created so f
188c8 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72  ar, so the new r
188c9 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74  oot-page is (met
188ca 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a  a[3]+1)..    */.
188cb 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
188cc 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45  GetMeta(p, BTREE
188cd 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41  _LARGEST_ROOT_PA
188ce 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a  GE, &pgnoRoot);.
188cf 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a      pgnoRoot++;.
188d0 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20  .    /* The new 
188d1 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f  root-page may no
188d2 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f  t be allocated o
188d3 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  n a pointer-map 
188d4 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20  page, or the.   
188d5 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   ** PENDING_BYTE
188d6 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
188d7 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f    while( pgnoRoo
188d8 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  t==PTRMAP_PAGENO
188d9 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20  (pBt, pgnoRoot) 
188da 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52  ||.        pgnoR
188db 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  oot==PENDING_BYT
188dc 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
188dd 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b       pgnoRoot++;
188de 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
188df 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29  t( pgnoRoot>=3 )
188e0 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  ;..    /* Alloca
188e1 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70  te a page. The p
188e2 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74  age that current
188e3 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67  ly resides at pg
188e4 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20  noRoot will.    
188e5 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74  ** be moved to t
188e6 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67  he allocated pag
188e7 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c  e (unless the al
188e8 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70  located page hap
188e9 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72  pens.    ** to r
188ea 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f  eside at pgnoRoo
188eb 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  t)..    */.    r
188ec 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
188ed 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67  ePage(pBt, &pPag
188ee 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65  eMove, &pgnoMove
188ef 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a  , pgnoRoot, 1);.
188f0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
188f1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
188f2 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
188f3 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76  .    if( pgnoMov
188f4 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20  e!=pgnoRoot ){. 
188f5 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74       /* pgnoRoot
188f6 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61   is the page tha
188f7 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
188f8 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  or the root-page
188f9 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   of.      ** the
188fa 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75   new table (assu
188fb 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69  ming an error di
188fc 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75  d not occur). Bu
188fd 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20  t we were.      
188fe 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e  ** allocated pgn
188ff 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72  oMove. If requir
18900 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77  ed (i.e. if it w
18901 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64  as not allocated
18902 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74  .      ** by ext
18903 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29  ending the file)
18904 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  , the current pa
18905 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70  ge at position p
18906 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a  gnoMove.      **
18907 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72   is already jour
18908 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  naled..      */.
18909 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20 3d        u8 eType =
1890a 20 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69   0;.      Pgno i
1890b 50 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20  PtrPage = 0;..  
1890c 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1890d 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20  pPageMove);..   
1890e 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70     /* Move the p
1890f 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74  age currently at
18910 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e   pgnoRoot to pgn
18911 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20  oMove. */.      
18912 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
18913 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  e(pBt, pgnoRoot,
18914 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20   &pRoot, 0);.   
18915 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18916 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
18917 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18918 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74   }.      rc = pt
18919 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e  rmapGet(pBt, pgn
1891a 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26  oRoot, &eType, &
1891b 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20  iPtrPage);.     
1891c 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
1891d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65  AP_ROOTPAGE || e
1891e 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45  Type==PTRMAP_FRE
1891f 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20  EPAGE ){.       
18920 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
18921 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
18922 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21   }.      if( rc!
18923 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
18924 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
18925 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20  e(pRoot);.      
18926 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
18927 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
18928 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50  t( eType!=PTRMAP
18929 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20  _ROOTPAGE );.   
1892a 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65     assert( eType
1892b 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  !=PTRMAP_FREEPAG
1892c 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  E );.      rc = 
1892d 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74  relocatePage(pBt
1892e 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20  , pRoot, eType, 
1892f 69 50 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f  iPtrPage, pgnoMo
18930 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65  ve, 0);.      re
18931 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
18932 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61  ;..      /* Obta
18933 69 6e 20 74 68 65 20 70 61 67 65 20 61 74 20 70  in the page at p
18934 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20  gnoRoot */.     
18935 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18936 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
18937 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
18938 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65  .      rc = btre
18939 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67  eGetPage(pBt, pg
1893a 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20  noRoot, &pRoot, 
1893b 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
1893c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1893d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1893e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1893f 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
18940 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44  rWrite(pRoot->pD
18941 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66  bPage);.      if
18942 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18943 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  ){.        relea
18944 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20  sePage(pRoot);. 
18945 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
18946 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
18947 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74  lse{.      pRoot
18948 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20   = pPageMove;.  
18949 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64    } ..    /* Upd
1894a 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  ate the pointer-
1894b 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74  map and meta-dat
1894c 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72  a with the new r
1894d 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e  oot-page number.
1894e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75   */.    ptrmapPu
1894f 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  t(pBt, pgnoRoot,
18950 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
18951 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  , 0, &rc);.    i
18952 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
18953 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74  eleasePage(pRoot
18954 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
18955 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
18956 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55   = sqlite3BtreeU
18957 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20  pdateMeta(p, 4, 
18958 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69  pgnoRoot);.    i
18959 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
1895a 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74  eleasePage(pRoot
1895b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1895c 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c  rc;.    }..  }el
1895d 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c  se{.    rc = all
1895e 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
1895f 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e  Bt, &pRoot, &pgn
18960 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20  oRoot, 1, 0);.  
18961 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
18962 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  n rc;.  }.#endif
18963 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
18964 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
18965 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  le(pRoot->pDbPag
18966 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  e) );.  zeroPage
18967 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20  (pRoot, flags | 
18968 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c  PTF_LEAF);.  sql
18969 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
1896a 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Root->pDbPage);.
1896b 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e    *piTable = (in
1896c 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65  t)pgnoRoot;.  re
1896d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1896e 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
1896f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
18970 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72  eCreateTable(Btr
18971 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61  ee *p, int *piTa
18972 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  ble, int flags){
18973 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
18974 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
18975 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43  );.  rc = btreeC
18976 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69  reateTable(p, pi
18977 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20  Table, flags);. 
18978 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18979 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
1897a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61  rc;.}../*.** Era
1897b 73 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74  se the given dat
1897c 61 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61  abase page and a
1897d 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e  ll its children.
1897e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20    Return.** the 
1897f 70 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65  page to the free
18980 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
18981 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73  int clearDatabas
18982 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65  ePage(.  BtShare
18983 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20  d *pBt,         
18984 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74    /* The BTree t
18985 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
18986 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f   table */.  Pgno
18987 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
18988 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
18989 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a  ber to clear */.
1898a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c    int freePageFl
1898b 61 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 65  ag,        /* De
1898c 61 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66  allocate page if
1898d 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   true */.  int *
1898e 70 6e 43 68 61 6e 67 65 20 20 20 20 20 20 20 20  pnChange        
1898f 20 20 20 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65      /* Add numbe
18990 72 20 6f 66 20 43 65 6c 6c 73 20 66 72 65 65 64  r of Cells freed
18991 20 74 6f 20 74 68 69 73 20 63 6f 75 6e 74 65 72   to this counter
18992 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65   */.){.  MemPage
18993 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72   *pPage;.  int r
18994 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
18995 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74  ar *pCell;.  int
18996 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   i;..  assert( s
18997 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
18998 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
18999 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65  .  if( pgno>page
1899a 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
1899b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1899c 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1899d 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67  T;.  }..  rc = g
1899e 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
1899f 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29  t, pgno, &pPage)
189a0 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
189a1 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d  urn rc;.  for(i=
189a2 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  0; i<pPage->nCel
189a3 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65  l; i++){.    pCe
189a4 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
189a5 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28  age, i);.    if(
189a6 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
189a7 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61  .      rc = clea
189a8 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42  rDatabasePage(pB
189a9 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 6c  t, get4byte(pCel
189aa 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29  l), 1, pnChange)
189ab 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
189ac 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62   goto cleardatab
189ad 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  asepage_out;.   
189ae 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61   }.    rc = clea
189af 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65  rCell(pPage, pCe
189b0 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ll);.    if( rc 
189b1 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61  ) goto cleardata
189b2 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  basepage_out;.  
189b3 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  }.  if( !pPage->
189b4 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d  leaf ){.    rc =
189b5 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61   clearDatabasePa
189b6 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65  ge(pBt, get4byte
189b7 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38  (&pPage->aData[8
189b8 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29  ]), 1, pnChange)
189b9 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
189ba 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
189bb 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c  epage_out;.  }el
189bc 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20  se if( pnChange 
189bd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
189be 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
189bf 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d      *pnChange +=
189c0 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
189c1 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67   }.  if( freePag
189c2 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65  eFlag ){.    fre
189c3 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63  ePage(pPage, &rc
189c4 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  );.  }else if( (
189c5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
189c6 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
189c7 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20  bPage))==0 ){.  
189c8 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65    zeroPage(pPage
189c9 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30  , pPage->aData[0
189ca 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20  ] | PTF_LEAF);. 
189cb 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73   }..cleardatabas
189cc 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c  epage_out:.  rel
189cd 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
189ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
189cf 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c  ./*.** Delete al
189d0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  l information fr
189d1 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  om a single tabl
189d2 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
189d3 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a  e.  iTable is.**
189d4 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
189d5 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   of the root of 
189d6 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65  the table.  Afte
189d7 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  r this routine r
189d8 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72  eturns,.** the r
189d9 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74  oot page is empt
189da 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69  y, but still exi
189db 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  sts..**.** This 
189dc 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69  routine will fai
189dd 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f  l with SQLITE_LO
189de 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72  CKED if there ar
189df 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65  e any open.** re
189e0 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68  ad cursors on th
189e1 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77  e table.  Open w
189e2 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65  rite cursors are
189e3 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a   moved to the.**
189e4 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62   root of the tab
189e5 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43  le..**.** If pnC
189e6 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c  hange is not NUL
189e7 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54  L, then table iT
189e8 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20  able must be an 
189e9 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68  intkey table. Th
189ea 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c  e.** integer val
189eb 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ue pointed to by
189ec 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63   pnChange is inc
189ed 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20  remented by the 
189ee 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74  number of.** ent
189ef 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c  ries in the tabl
189f0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
189f1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
189f2 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
189f3 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
189f4 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61  able, int *pnCha
189f5 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  nge){.  int rc;.
189f6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
189f7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
189f8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
189f9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
189fa 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
189fb 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  ITE );..  /* Inv
189fc 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72  alidate all incr
189fd 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65  blob cursors ope
189fe 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61 62 6c  n on table iTabl
189ff 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54 61 62  e (assuming iTab
18a00 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72  le.  ** is the r
18a01 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65 20 62  oot of a table b
18a02 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20 69 73  -tree - if it is
18a03 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77   not, the follow
18a04 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a  ing call is.  **
18a05 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20   a no-op).  */. 
18a06 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62   invalidateIncrb
18a07 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 30 2c  lobCursors(p, 0,
18a08 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76   1);..  rc = sav
18a09 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
18a0a 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30   (Pgno)iTable, 0
18a0b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
18a0c 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 63  OK==rc ){.    rc
18a0d 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65   = clearDatabase
18a0e 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29  Page(pBt, (Pgno)
18a0f 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61  iTable, 0, pnCha
18a10 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  nge);.  }.  sqli
18a11 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
18a12 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
18a13 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c  ../*.** Erase al
18a14 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  l information in
18a15 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 64 64   a table and add
18a16 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18a17 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65   table to.** the
18a18 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 63 65   freelist.  Exce
18a19 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 20  pt, the root of 
18a1a 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 74 61  the principle ta
18a1b 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a  ble (the one on.
18a1c 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 6e 65  ** page 1) is ne
18a1d 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 68 65  ver added to the
18a1e 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   freelist..**.**
18a1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
18a20 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c  ll fail with SQL
18a21 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68  ITE_LOCKED if th
18a22 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e  ere are any open
18a23 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74  .** cursors on t
18a24 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  he table..**.** 
18a25 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73  If AUTOVACUUM is
18a26 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 65   enabled and the
18a27 20 70 61 67 65 20 61 74 20 69 54 61 62 6c 65 20   page at iTable 
18a28 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a  is not the last.
18a29 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20  ** root page in 
18a2a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
18a2b 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74  e, then the last
18a2c 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69   root page .** i
18a2d 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
18a2e 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 6e 74  ile is moved int
18a2f 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65  o the slot forme
18a30 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 0a  rly occupied by.
18a31 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 74 68  ** iTable and th
18a32 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72  at last slot for
18a33 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62  merly occupied b
18a34 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20  y the last root 
18a35 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 64  page.** is added
18a36 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
18a37 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 61 62   instead of iTab
18a38 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 61 79  le.  In this say
18a39 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61  , all.** root pa
18a3a 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 74 20  ges are kept at 
18a3b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
18a3c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
18a3d 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20  le, which.** is 
18a3e 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 41 55  necessary for AU
18a3f 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b  TOVACUUM to work
18a40 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65   right.  *piMove
18a41 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
18a42 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20  .** page number 
18a43 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 65 20  that used to be 
18a44 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61  the last root pa
18a45 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 62  ge in the file b
18a46 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76  efore.** the mov
18a47 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 67  e.  If no page g
18a48 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f  ets moved, *piMo
18a49 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e  ved is set to 0.
18a4a 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 6f 6f  .** The last roo
18a4b 74 20 70 61 67 65 20 69 73 20 72 65 63 6f 72 64  t page is record
18a4c 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e  ed in meta[3] an
18a4d 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a  d the value of.*
18a4e 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 64  * meta[3] is upd
18a4f 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72 6f  ated by this pro
18a50 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  cedure..*/.stati
18a51 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f 70 54  c int btreeDropT
18a52 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50  able(Btree *p, P
18a53 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74 20  gno iTable, int 
18a54 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74  *piMoved){.  int
18a55 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a   rc;.  MemPage *
18a56 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53  pPage = 0;.  BtS
18a57 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
18a58 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
18a59 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
18a5a 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61  sMutex(p) );.  a
18a5b 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
18a5c 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
18a5d 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c  ;..  /* It is il
18a5e 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 61 20  legal to drop a 
18a5f 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 75 72  table if any cur
18a60 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e  sors are open on
18a61 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
18a62 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  se. This is beca
18a63 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 63 75  use in auto-vacu
18a64 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 63 6b  um mode the back
18a65 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65  end may.  ** nee
18a66 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65  d to move anothe
18a67 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66  r root-page to f
18a68 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 20 62  ill a gap left b
18a69 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a 20 20  y the deleted.  
18a6a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 66  ** root page. If
18a6b 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20   an open cursor 
18a6c 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 20 70  was using this p
18a6d 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f  age a problem wo
18a6e 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e  uld .  ** occur.
18a6f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
18a70 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20  error is caught 
18a71 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e 74  long before cont
18a72 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68 69 73  rol reaches this
18a73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69   point..  */.  i
18a74 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 70 43  f( NEVER(pBt->pC
18a75 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71  ursor) ){.    sq
18a76 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
18a77 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42  locked(p->db, pB
18a78 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72  t->pCursor->pBtr
18a79 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74  ee->db);.    ret
18a7a 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
18a7b 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20  D_SHAREDCACHE;. 
18a7c 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65   }..  rc = btree
18a7d 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67  GetPage(pBt, (Pg
18a7e 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67  no)iTable, &pPag
18a7f 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
18a80 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
18a81 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
18a82 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54  ClearTable(p, iT
18a83 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20  able, 0);.  if( 
18a84 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73  rc ){.    releas
18a85 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
18a86 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
18a87 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30  ..  *piMoved = 0
18a88 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e  ;..  if( iTable>
18a89 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  1 ){.#ifdef SQLI
18a8a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18a8b 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67 65 28  UM.    freePage(
18a8c 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20  pPage, &rc);.   
18a8d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
18a8e 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69  ge);.#else.    i
18a8f 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
18a90 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f  um ){.      Pgno
18a91 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20   maxRootPgno;.  
18a92 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
18a93 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45  GetMeta(p, BTREE
18a94 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41  _LARGEST_ROOT_PA
18a95 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f  GE, &maxRootPgno
18a96 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54  );..      if( iT
18a97 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e  able==maxRootPgn
18a98 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  o ){.        /* 
18a99 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  If the table bei
18a9a 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74 68  ng dropped is th
18a9b 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65  e table with the
18a9c 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61   largest root-pa
18a9d 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75  ge.        ** nu
18a9e 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 61  mber in the data
18a9f 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f  base, put the ro
18aa0 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66  ot page on the f
18aa1 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20  ree list. .     
18aa2 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 72     */.        fr
18aa3 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72  eePage(pPage, &r
18aa4 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  c);.        rele
18aa5 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
18aa6 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
18aa7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18aa8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
18aa9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
18aaa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
18aab 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
18aac 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73  ing dropped does
18aad 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61   not have the la
18aae 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
18aaf 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
18ab0 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
18ab1 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70  e. So move the p
18ab2 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e  age that does in
18ab3 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  to the .        
18ab4 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20 74  ** gap left by t
18ab5 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d  he deleted root-
18ab6 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  page..        */
18ab7 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65  .        MemPage
18ab8 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20   *pMove;.       
18ab9 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
18aba 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ge);.        rc 
18abb 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
18abc 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c  Bt, maxRootPgno,
18abd 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20   &pMove, 0);.   
18abe 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18abf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18ac0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18ac1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
18ac2 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61   rc = relocatePa
18ac3 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50  ge(pBt, pMove, P
18ac4 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20  TRMAP_ROOTPAGE, 
18ac5 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20  0, iTable, 0);. 
18ac6 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
18ac7 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20  ge(pMove);.     
18ac8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18ac9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
18aca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
18acb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
18acc 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Move = 0;.      
18acd 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
18ace 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  age(pBt, maxRoot
18acf 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29  Pgno, &pMove, 0)
18ad0 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 61  ;.        freePa
18ad1 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a  ge(pMove, &rc);.
18ad2 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
18ad3 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20  age(pMove);.    
18ad4 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
18ad5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
18ad6 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
18ad7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
18ad8 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f  *piMoved = maxRo
18ad9 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  otPgno;.      }.
18ada 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68  .      /* Set th
18adb 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d  e new 'max-root-
18adc 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74  page' value in t
18add 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64  he database head
18ade 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a  er. This.      *
18adf 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c  * is the old val
18ae0 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73  ue less one, les
18ae1 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68  s one more if th
18ae2 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20  at happens to.  
18ae3 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74      ** be a root
18ae4 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65  -page number, le
18ae5 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20  ss one again if 
18ae6 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20 20  that is the.    
18ae7 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54    ** PENDING_BYT
18ae8 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f  E_PAGE..      */
18ae9 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67  .      maxRootPg
18aea 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c  no--;.      whil
18aeb 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d  e( maxRootPgno==
18aec 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
18aed 45 28 70 42 74 29 0a 20 20 20 20 20 20 20 20 20  E(pBt).         
18aee 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53      || PTRMAP_IS
18aef 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f  PAGE(pBt, maxRoo
18af0 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20  tPgno) ){.      
18af1 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b    maxRootPgno--;
18af2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
18af3 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67  ssert( maxRootPg
18af4 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  no!=PENDING_BYTE
18af5 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20  _PAGE(pBt) );.. 
18af6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
18af7 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
18af8 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67  (p, 4, maxRootPg
18af9 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  no);.    }else{.
18afa 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 70        freePage(p
18afb 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20  Page, &rc);.    
18afc 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
18afd 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  age);.    }.#end
18afe 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  if.  }else{.    
18aff 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 74 72  /* If sqlite3Btr
18b00 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20  eeDropTable was 
18b01 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31  called on page 1
18b02 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 72 65  ..    ** This re
18b03 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f 75 6c  ally never shoul
18b04 64 20 68 61 70 70 65 6e 20 65 78 63 65 70 74 20  d happen except 
18b05 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20 20 20  in a corrupt.   
18b06 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20   ** database. . 
18b07 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61     */.    zeroPa
18b08 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e  ge(pPage, PTF_IN
18b09 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b  TKEY|PTF_LEAF );
18b0a 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
18b0b 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72  (pPage);.  }.  r
18b0c 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51  eturn rc;  .}.SQ
18b0d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18b0e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
18b0f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  pTable(Btree *p,
18b10 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74   int iTable, int
18b11 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e   *piMoved){.  in
18b12 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
18b13 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
18b14 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61  rc = btreeDropTa
18b15 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70  ble(p, iTable, p
18b16 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74  iMoved);.  sqlit
18b17 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18b18 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18b19 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
18b1a 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62  ction may only b
18b1b 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20  e called if the 
18b1c 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f  b-tree connectio
18b1d 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73  n already.** has
18b1e 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65   a read or write
18b1f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
18b20 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
18b21 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68  e..**.** Read th
18b22 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69  e meta-informati
18b23 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61  on out of a data
18b24 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61  base file.  Meta
18b25 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75  [0].** is the nu
18b26 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67  mber of free pag
18b27 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  es currently in 
18b28 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d  the database.  M
18b29 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67  eta[1].** throug
18b2a 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61  h meta[15] are a
18b2b 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65  vailable for use
18b2c 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 72   by higher layer
18b2d 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69  s.  Meta[0].** i
18b2e 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65  s read-only, the
18b2f 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61 64   others are read
18b30 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54  /write..** .** T
18b31 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20  he schema layer 
18b32 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c  numbers meta val
18b33 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e  ues differently.
18b34 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a    At the schema.
18b35 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68  ** layer (and th
18b36 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20  e SetCookie and 
18b37 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64  ReadCookie opcod
18b38 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  es) the number o
18b39 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20  f.** free pages 
18b3a 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20  is not visible. 
18b3b 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73   So Cookie[0] is
18b3c 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74   the same as Met
18b3d 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  a[1]..*/.SQLITE_
18b3e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
18b3f 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
18b40 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69  (Btree *p, int i
18b41 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b  dx, u32 *pMeta){
18b42 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
18b43 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71   = p->pBt;..  sq
18b44 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
18b45 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p);.  assert( p-
18b46 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e  >inTrans>TRANS_N
18b47 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ONE );.  assert(
18b48 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72   SQLITE_OK==quer
18b49 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
18b4a 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f  eLock(p, MASTER_
18b4b 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29  ROOT, READ_LOCK)
18b4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
18b4d 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 61  t->pPage1 );.  a
18b4e 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26  ssert( idx>=0 &&
18b4f 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a   idx<=15 );..  *
18b50 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74 65  pMeta = get4byte
18b51 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  (&pBt->pPage1->a
18b52 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d  Data[36 + idx*4]
18b53 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f  );..  /* If auto
18b54 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73 61 62  -vacuum is disab
18b55 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c  led in this buil
18b56 64 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 6e  d and this is an
18b57 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a   auto-vacuum.  *
18b58 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61 72 6b  * database, mark
18b59 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73   the database as
18b5a 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a   read-only.  */.
18b5b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
18b5c 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18b5d 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c  if( idx==BTREE_L
18b5e 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45  ARGEST_ROOT_PAGE
18b5f 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70   && *pMeta>0 ) p
18b60 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31  Bt->readOnly = 1
18b61 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69  ;.#endif..  sqli
18b62 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
18b63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
18b64 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
18b65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  n back into the 
18b66 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b  database.  Meta[
18b67 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e  0] is.** read-on
18b68 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62  ly and may not b
18b69 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51  e written..*/.SQ
18b6a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18b6b 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
18b6c 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70  ateMeta(Btree *p
18b6d 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69  , int idx, u32 i
18b6e 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65  Meta){.  BtShare
18b6f 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
18b70 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
18b71 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b   *pP1;.  int rc;
18b72 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d  .  assert( idx>=
18b73 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a  1 && idx<=15 );.
18b74 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
18b75 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74  ter(p);.  assert
18b76 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
18b77 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
18b78 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
18b79 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d  e1!=0 );.  pP1 =
18b7a 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44   pBt->pPage1->aD
18b7b 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ata;.  rc = sqli
18b7c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42  te3PagerWrite(pB
18b7d 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61  t->pPage1->pDbPa
18b7e 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ge);.  if( rc==S
18b7f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18b80 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 36  put4byte(&pP1[36
18b81 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61   + idx*4], iMeta
18b82 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18b83 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18b84 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 42  M.    if( idx==B
18b85 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d  TREE_INCR_VACUUM
18b86 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
18b87 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
18b88 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b  m || iMeta==0 );
18b89 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
18b8a 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61  Meta==0 || iMeta
18b8b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74  ==1 );.      pBt
18b8c 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
18b8d 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a  u8)iMeta;.    }.
18b8e 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c  #endif.  }.  sql
18b8f 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18b90 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
18b91 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
18b92 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e  E_OMIT_BTREECOUN
18b93 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  T./*.** The firs
18b94 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72  t argument, pCur
18b95 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70  , is a cursor op
18b96 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74  ened on some b-t
18b97 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a  ree. Count the.*
18b98 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * number of entr
18b99 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65  ies in the b-tre
18b9a 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20  e and write the 
18b9b 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74  result to *pnEnt
18b9c 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  ry..**.** SQLITE
18b9d 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
18b9e 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  if the operation
18b9f 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79   is successfully
18ba0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f   executed. .** O
18ba1 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20  therwise, if an 
18ba2 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
18ba3 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f  ered (i.e. an IO
18ba4 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61   error or databa
18ba5 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e  se.** corruption
18ba6 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  ) an SQLite erro
18ba7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
18ba8 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
18ba9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18baa 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75  3BtreeCount(BtCu
18bab 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20  rsor *pCur, i64 
18bac 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34  *pnEntry){.  i64
18bad 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20   nEntry = 0;    
18bae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18baf 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
18bb0 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79  turn in *pnEntry
18bb1 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
18bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18bb3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
18bb4 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72  turn code */.  r
18bb5 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70  c = moveToRoot(p
18bb6 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65  Cur);..  /* Unle
18bb7 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ss an error occu
18bb8 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rs, the followin
18bb9 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20  g loop runs one 
18bba 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61  iteration for ea
18bbb 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20  ch.  ** page in 
18bbc 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75 63  the B-Tree struc
18bbd 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64  ture (not includ
18bbe 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ing overflow pag
18bbf 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69  es). .  */.  whi
18bc0 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
18bc1 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64  K ){.    int iId
18bc2 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
18bc3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
18bc4 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f  ndex of child no
18bc5 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a  de in parent */.
18bc6 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
18bc7 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
18bc8 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
18bc9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d  t page of the b-
18bca 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  tree */..    /* 
18bcb 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65 61  If this is a lea
18bcc 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74 72  f page or the tr
18bcd 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  ee is not an int
18bce 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20  -key tree, then 
18bcf 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67  .    ** this pag
18bd0 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74  e contains count
18bd1 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e  able entries. In
18bd2 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72  crement the entr
18bd3 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a  y counter.    **
18bd4 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20   accordingly..  
18bd5 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d    */.    pPage =
18bd6 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
18bd7 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
18bd8 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
18bd9 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65  || !pPage->intKe
18bda 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72  y ){.      nEntr
18bdb 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c  y += pPage->nCel
18bdc 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  l;.    }..    /*
18bdd 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66   pPage is a leaf
18bde 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70   node. This loop
18bdf 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20 63   navigates the c
18be0 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74  ursor so that it
18be1 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20   .    ** points 
18be2 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 74  to the first int
18be3 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20  erior cell that 
18be4 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
18be5 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a   parent of.    *
18be6 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  * the next page 
18be7 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74  in the tree that
18be8 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
18be9 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20  n visited. The. 
18bea 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64     ** pCur->aiId
18beb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76  x[pCur->iPage] v
18bec 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 74  alue is set to t
18bed 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
18bee 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20  parent cell.    
18bef 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20  ** of the page, 
18bf0 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  or to the number
18bf1 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65   of cells in the
18bf2 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65 78   page if the nex
18bf3 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f  t page.    ** to
18bf4 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72 69   visit is the ri
18bf5 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73  ght-child of its
18bf6 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a   parent..    **.
18bf7 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61      ** If all pa
18bf8 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20  ges in the tree 
18bf9 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65  have been visite
18bfa 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d, return SQLITE
18bfb 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a  _OK to the.    *
18bfc 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f  * caller..    */
18bfd 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
18bfe 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f  leaf ){.      do
18bff 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   {.        if( p
18c00 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
18c01 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c  .          /* Al
18c02 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 62  l pages of the b
18c03 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20  -tree have been 
18c04 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20  visited. Return 
18c05 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f  successfully. */
18c06 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e  .          *pnEn
18c07 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20  try = nEntry;.  
18c08 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
18c09 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
18c0a 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76 65    }.        move
18c0b 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a  ToParent(pCur);.
18c0c 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70        }while ( p
18c0d 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
18c0e 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61  >iPage]>=pCur->a
18c0f 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
18c10 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20  e]->nCell );..  
18c11 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
18c12 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a  pCur->iPage]++;.
18c13 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43        pPage = pC
18c14 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18c15 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a  >iPage];.    }..
18c16 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74      /* Descend t
18c17 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65  o the child node
18c18 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61   of the cell tha
18c19 74 20 74 68 65 20 63 75 72 73 6f 72 20 63 75 72  t the cursor cur
18c1a 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70  rently .    ** p
18c1b 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69  oints at. This i
18c1c 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  s the right-chil
18c1d 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67  d if (iIdx==pPag
18c1e 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a  e->nCell)..    *
18c1f 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70 43 75  /.    iIdx = pCu
18c20 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
18c21 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69  Page];.    if( i
18c22 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  Idx==pPage->nCel
18c23 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
18c24 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
18c25 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  , get4byte(&pPag
18c26 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
18c27 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a  hdrOffset+8]));.
18c28 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18c29 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
18c2a 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65  d(pCur, get4byte
18c2b 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c  (findCell(pPage,
18c2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a   iIdx)));.    }.
18c2d 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72    }..  /* An err
18c2e 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  or has occurred.
18c2f 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
18c30 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75   code. */.  retu
18c31 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a  rn rc;.}.#endif.
18c32 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18c33 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61 74  e pager associat
18c34 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e  ed with a BTree.
18c35 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
18c36 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65  s used for.** te
18c37 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
18c38 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ing only..*/.SQL
18c39 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67 65  ITE_PRIVATE Page
18c3a 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50  r *sqlite3BtreeP
18c3b 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  ager(Btree *p){.
18c3c 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
18c3d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e  >pPager;.}..#ifn
18c3e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18c3f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
18c40 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d  /*.** Append a m
18c41 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65 72  essage to the er
18c42 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69  ror message stri
18c43 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
18c44 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73  id checkAppendMs
18c45 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b  g(.  IntegrityCk
18c46 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72   *pCheck,.  char
18c47 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74   *zMsg1,.  const
18c48 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a   char *zFormat,.
18c49 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69    ....){.  va_li
18c4a 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43  st ap;.  if( !pC
18c4b 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65  heck->mxErr ) re
18c4c 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e  turn;.  pCheck->
18c4d 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63  mxErr--;.  pChec
18c4e 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f  k->nErr++;.  va_
18c4f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
18c50 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b  t);.  if( pCheck
18c51 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29  ->errMsg.nChar )
18c52 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72  {.    sqlite3Str
18c53 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68  AccumAppend(&pCh
18c54 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e  eck->errMsg, "\n
18c55 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ", 1);.  }.  if(
18c56 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71   zMsg1 ){.    sq
18c57 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
18c58 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72  end(&pCheck->err
18c59 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b  Msg, zMsg1, -1);
18c5a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58  .  }.  sqlite3VX
18c5b 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e  Printf(&pCheck->
18c5c 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d  errMsg, 1, zForm
18c5d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
18c5e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68  d(ap);.  if( pCh
18c5f 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c  eck->errMsg.mall
18c60 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
18c61 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61  pCheck->mallocFa
18c62 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a  iled = 1;.  }.}.
18c63 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18c64 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
18c65 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
18c66 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18c67 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18c68 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65  .** Add 1 to the
18c69 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
18c6a 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e   for page iPage.
18c6b 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65    If this is the
18c6c 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72   second.** refer
18c6d 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65  ence to the page
18c6e 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  , add an error m
18c6f 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b  essage to pCheck
18c70 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65  ->zErrMsg..** Re
18c71 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 20  turn 1 if there 
18c72 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72  are 2 ore more r
18c73 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
18c74 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a   page and 0 if.*
18c75 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  * if this is the
18c76 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 65   first reference
18c77 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a   to the page..**
18c78 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74  .** Also check t
18c79 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d  hat the page num
18c7a 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73  ber is in bounds
18c7b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
18c7c 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69  checkRef(Integri
18c7d 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67  tyCk *pCheck, Pg
18c7e 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a  no iPage, char *
18c7f 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28  zContext){.  if(
18c80 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75   iPage==0 ) retu
18c81 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67  rn 1;.  if( iPag
18c82 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20  e>pCheck->nPage 
18c83 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  ){.    checkAppe
18c84 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18c85 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64  ontext, "invalid
18c86 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22   page number %d"
18c87 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65  , iPage);.    re
18c88 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66  turn 1;.  }.  if
18c89 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b  ( pCheck->anRef[
18c8a 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20  iPage]==1 ){.   
18c8b 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18c8c 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
18c8d 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65  , "2nd reference
18c8e 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50   to page %d", iP
18c8f 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  age);.    return
18c90 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
18c91 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66    (pCheck->anRef
18c92 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a  [iPage]++)>1;.}.
18c93 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18c94 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18c95 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
18c96 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   the entry in th
18c97 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f  e pointer-map fo
18c98 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61  r page iChild ma
18c99 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69  ps to .** page i
18c9a 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20  Parent, pointer 
18c9b 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49 66  type ptrType. If
18c9c 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20   not, append an 
18c9d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
18c9e 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73   to pCheck..*/.s
18c9f 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b  tatic void check
18ca0 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72  Ptrmap(.  Integr
18ca1 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
18ca2 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68   /* Integrity ch
18ca3 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  eck context */. 
18ca4 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20   Pgno iChild,   
18ca5 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64          /* Child
18ca6 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
18ca7 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20    u8 eType,     
18ca8 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65           /* Expe
18ca9 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70  cted pointer map
18caa 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   type */.  Pgno 
18cab 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20  iParent,        
18cac 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f    /* Expected po
18cad 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74  inter map parent
18cae 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
18caf 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74    char *zContext
18cb0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
18cb1 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20  ext description 
18cb2 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20  (used for error 
18cb3 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  msg) */.){.  int
18cb4 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61   rc;.  u8 ePtrma
18cb5 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50  pType;.  Pgno iP
18cb6 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20  trmapParent;..  
18cb7 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70  rc = ptrmapGet(p
18cb8 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69  Check->pBt, iChi
18cb9 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65  ld, &ePtrmapType
18cba 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74  , &iPtrmapParent
18cbb 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
18cbc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
18cbd 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
18cbe 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
18cbf 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70  _IOERR_NOMEM ) p
18cc0 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69  Check->mallocFai
18cc1 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65  led = 1;.    che
18cc2 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18cc3 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46  ck, zContext, "F
18cc4 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74  ailed to read pt
18cc5 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43  rmap key=%d", iC
18cc6 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72  hild);.    retur
18cc7 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50  n;.  }..  if( eP
18cc8 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65  trmapType!=eType
18cc9 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e   || iPtrmapParen
18cca 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20  t!=iParent ){.  
18ccb 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18ccc 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
18ccd 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 20 70  t, .      "Bad p
18cce 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79  tr map entry key
18ccf 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 25 64  =%d expected=(%d
18cd0 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29  ,%d) got=(%d,%d)
18cd1 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64  ", .      iChild
18cd2 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74  , eType, iParent
18cd3 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69  , ePtrmapType, i
18cd4 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20  PtrmapParent);. 
18cd5 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
18cd6 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74  ** Check the int
18cd7 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72  egrity of the fr
18cd8 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20  eelist or of an 
18cd9 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
18cda 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68  st..** Verify th
18cdb 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  at the number of
18cdc 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69   pages on the li
18cdd 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74  st is N..*/.stat
18cde 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73  ic void checkLis
18cdf 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b  t(.  IntegrityCk
18ce0 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e   *pCheck,  /* In
18ce1 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67  tegrity checking
18ce2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
18ce3 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20  t isFreeList,   
18ce4 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
18ce5 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c  a freelist.  Fal
18ce6 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20  se for overflow 
18ce7 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69  page list */.  i
18ce8 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 20  nt iPage,       
18ce9 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
18cea 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 70 61  ber for first pa
18ceb 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  ge in the list *
18cec 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20  /.  int N,      
18ced 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
18cee 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  ected number of 
18cef 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73  pages in the lis
18cf0 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  t */.  char *zCo
18cf1 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20  ntext        /* 
18cf2 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f  Context for erro
18cf3 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b  r messages */.){
18cf4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
18cf5 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20  expected = N;.  
18cf6 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 50 61  int iFirst = iPa
18cf7 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d  ge;.  while( N--
18cf8 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e   > 0 && pCheck->
18cf9 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50  mxErr ){.    DbP
18cfa 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a  age *pOvflPage;.
18cfb 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
18cfc 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20  r *pOvflData;.  
18cfd 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b    if( iPage<1 ){
18cfe 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
18cff 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18d00 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18d01 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 65 73   "%d of %d pages
18d02 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76   missing from ov
18d03 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72  erflow list star
18d04 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20  ting at %d",.   
18d05 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65         N+1, expe
18d06 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20  cted, iFirst);. 
18d07 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
18d08 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52  }.    if( checkR
18d09 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65  ef(pCheck, iPage
18d0a 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72  , zContext) ) br
18d0b 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  eak;.    if( sql
18d0c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 43 68  ite3PagerGet(pCh
18d0d 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67  eck->pPager, (Pg
18d0e 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c  no)iPage, &pOvfl
18d0f 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63  Page) ){.      c
18d10 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18d11 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18d12 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70  "failed to get p
18d13 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b  age %d", iPage);
18d14 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
18d15 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74    }.    pOvflDat
18d16 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  a = (unsigned ch
18d17 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ar *)sqlite3Page
18d18 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61  rGetData(pOvflPa
18d19 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46  ge);.    if( isF
18d1a 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  reeList ){.     
18d1b 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74   int n = get4byt
18d1c 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29  e(&pOvflData[4])
18d1d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
18d1e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
18d1f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
18d20 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
18d21 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  um ){.        ch
18d22 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b  eckPtrmap(pCheck
18d23 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f  , iPage, PTRMAP_
18d24 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f  FREEPAGE, 0, zCo
18d25 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a  ntext);.      }.
18d26 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
18d27 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e   n>pCheck->pBt->
18d28 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29  usableSize/4-2 )
18d29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
18d2a 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18d2b 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20   zContext,.     
18d2c 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 74 20        "freelist 
18d2d 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62  leaf count too b
18d2e 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20  ig on page %d", 
18d2f 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  iPage);.        
18d30 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  N--;.      }else
18d31 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
18d32 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<n; i++){.  
18d33 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72          Pgno iFr
18d34 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 79 74  eePage = get4byt
18d35 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69  e(&pOvflData[8+i
18d36 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  *4]);.#ifndef SQ
18d37 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
18d38 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69  CUUM.          i
18d39 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e  f( pCheck->pBt->
18d3a 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
18d3b 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b 50            checkP
18d3c 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46  trmap(pCheck, iF
18d3d 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f  reePage, PTRMAP_
18d3e 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f  FREEPAGE, 0, zCo
18d3f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
18d40 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
18d41 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 70 43       checkRef(pC
18d42 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c  heck, iFreePage,
18d43 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
18d44 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20      }.        N 
18d45 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  -= n;.      }.  
18d46 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
18d47 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18d48 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20  UM.    else{.   
18d49 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61     /* If this da
18d4a 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
18d4b 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20  auto-vacuum and 
18d4c 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65  iPage is not the
18d4d 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70   last.      ** p
18d4e 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72  age in this over
18d4f 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b  flow list, check
18d50 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
18d51 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a  r-map entry for.
18d52 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c        ** the fol
18d53 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63  lowing page matc
18d54 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20  hes iPage..     
18d55 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43   */.      if( pC
18d56 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  heck->pBt->autoV
18d57 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a  acuum && N>0 ){.
18d58 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74 34          i = get4
18d59 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b  byte(pOvflData);
18d5a 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
18d5b 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20  rmap(pCheck, i, 
18d5c 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
18d5d 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78  , iPage, zContex
18d5e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
18d5f 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61  }.#endif.    iPa
18d60 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f  ge = get4byte(pO
18d61 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71  vflData);.    sq
18d62 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
18d63 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a  pOvflPage);.  }.
18d64 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
18d65 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
18d66 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e  Y_CHECK */..#ifn
18d67 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18d68 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
18d69 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73  /*.** Do various
18d6a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f   sanity checks o
18d6b 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
18d6c 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 74 75  of a tree.  Retu
18d6d 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64  rn.** the tree d
18d6e 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65  epth.  Root page
18d6f 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72  s return 0.  Par
18d70 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67  ents of root pag
18d71 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20  es.** return 1, 
18d72 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a  and so forth..**
18d73 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b   .** These check
18d74 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a  s are done:.**.*
18d75 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20  *      1.  Make 
18d76 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20  sure that cells 
18d77 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64  and freeblocks d
18d78 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a  o not overlap.**
18d79 20 20 20 20 20 20 20 20 20 20 62 75 74 20 63 6f            but co
18d7a 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74  mbine to complet
18d7b 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 61  ely cover the pa
18d7c 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20  ge..**  NO  2.  
18d7d 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b  Make sure cell k
18d7e 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72  eys are in order
18d7f 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61  ..**  NO  3.  Ma
18d80 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
18d81 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
18d82 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f  qual to zLowerBo
18d83 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20  und..**  NO  4. 
18d84 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65   Make sure no ke
18d85 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  y is greater tha
18d86 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55  n or equal to zU
18d87 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20  pperBound..**   
18d88 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65     5.  Check the
18d89 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76   integrity of ov
18d8a 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a  erflow pages..**
18d8b 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 72 73        6.  Recurs
18d8c 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b  ively call check
18d8d 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20  TreePage on all 
18d8e 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20  children..**    
18d8f 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 68 61    7.  Verify tha
18d90 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 61  t the depth of a
18d91 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74  ll children is t
18d92 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20  he same..**     
18d93 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74   8.  Make sure t
18d94 68 69 73 20 70 61 67 65 20 69 73 20 61 74 20 6c  his page is at l
18d95 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72  east 33% full or
18d96 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20   else it is.**  
18d97 20 20 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74          the root
18d98 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f   of the tree..*/
18d99 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
18d9a 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74  kTreePage(.  Int
18d9b 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b  egrityCk *pCheck
18d9c 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f  ,  /* Context fo
18d9d 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65  r the sanity che
18d9e 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67  ck */.  int iPag
18d9f 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
18da0 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
18da1 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65 63  the page to chec
18da2 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61  k */.  char *zPa
18da3 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20  rentContext  /* 
18da4 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a  Parent context *
18da5 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  /.){.  MemPage *
18da6 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20  pPage;.  int i, 
18da7 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70  rc, depth, d2, p
18da8 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20  gno, cnt;.  int 
18da9 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a  hdr, cellStart;.
18daa 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75    int nCell;.  u
18dab 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61  8 *data;.  BtSha
18dac 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20  red *pBt;.  int 
18dad 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68  usableSize;.  ch
18dae 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d  ar zContext[100]
18daf 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20  ;.  char *hit = 
18db0 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  0;..  sqlite3_sn
18db1 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43  printf(sizeof(zC
18db2 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78  ontext), zContex
18db3 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20  t, "Page %d: ", 
18db4 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68  iPage);..  /* Ch
18db5 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67  eck that the pag
18db6 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20  e exists.  */.  
18db7 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42  pBt = pCheck->pB
18db8 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20  t;.  usableSize 
18db9 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
18dba 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d  e;.  if( iPage==
18dbb 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
18dbc 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68  if( checkRef(pCh
18dbd 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72  eck, iPage, zPar
18dbe 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65  entContext) ) re
18dbf 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72  turn 0;.  if( (r
18dc0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
18dc1 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 67  (pBt, (Pgno)iPag
18dc2 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d  e, &pPage, 0))!=
18dc3 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70  0 ){.    checkAp
18dc4 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
18dc5 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  zContext,.      
18dc6 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20   "unable to get 
18dc7 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f 72 20  the page. error 
18dc8 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20  code=%d", rc);. 
18dc9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
18dca 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d  ..  /* Clear Mem
18dcb 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d  Page.isInit to m
18dcc 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 72  ake sure the cor
18dcd 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 69 6f  ruption detectio
18dce 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62  n code in.  ** b
18dcf 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 69  treeInitPage() i
18dd0 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a  s executed.  */.
18dd1 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
18dd2 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d  = 0;.  if( (rc =
18dd3 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70   btreeInitPage(p
18dd4 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20 20 20  Page))!=0 ){.   
18dd5 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
18dd6 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 20 20  ITE_CORRUPT );  
18dd7 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73  /* The only poss
18dd8 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d 20  ible error from 
18dd9 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20 20 20  InitPage */.    
18dda 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18ddb 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18ddc 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
18ddd 20 20 20 20 20 22 62 74 72 65 65 49 6e 69 74 50       "btreeInitP
18dde 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65 72  age() returns er
18ddf 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63  ror code %d", rc
18de0 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  );.    releasePa
18de1 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
18de2 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
18de3 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c  /* Check out all
18de4 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f   the cells..  */
18de5 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20  .  depth = 0;.  
18de6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65  for(i=0; i<pPage
18de7 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63  ->nCell && pChec
18de8 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a  k->mxErr; i++){.
18de9 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20      u8 *pCell;. 
18dea 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43     u32 sz;.    C
18deb 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20  ellInfo info;.. 
18dec 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c     /* Check payl
18ded 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  oad overflow pag
18dee 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  es.    */.    sq
18def 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
18df0 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c  izeof(zContext),
18df1 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20   zContext,.     
18df2 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65          "On tree
18df3 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64   page %d cell %d
18df4 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a  : ", iPage, i);.
18df5 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
18df6 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20  Cell(pPage,i);. 
18df7 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
18df8 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
18df9 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73  l, &info);.    s
18dfa 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a  z = info.nData;.
18dfb 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e      if( !pPage->
18dfc 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28  intKey ) sz += (
18dfd 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  int)info.nKey;. 
18dfe 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69     assert( sz==i
18dff 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a  nfo.nPayload );.
18e00 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f      if( (sz>info
18e01 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20 26  .nLocal) .     &
18e02 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  & (&pCell[info.i
18e03 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67  Overflow]<=&pPag
18e04 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73  e->aData[pBt->us
18e05 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20 29  ableSize]).    )
18e06 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67  {.      int nPag
18e07 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e  e = (sz - info.n
18e08 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69  Local + usableSi
18e09 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53  ze - 5)/(usableS
18e0a 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20  ize - 4);.      
18e0b 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20  Pgno pgnoOvfl = 
18e0c 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b  get4byte(&pCell[
18e0d 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29  info.iOverflow])
18e0e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
18e0f 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
18e10 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
18e11 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20  autoVacuum ){.  
18e12 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18e13 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76  p(pCheck, pgnoOv
18e14 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  fl, PTRMAP_OVERF
18e15 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f  LOW1, iPage, zCo
18e16 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a  ntext);.      }.
18e17 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65  #endif.      che
18e18 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30  ckList(pCheck, 0
18e19 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67  , pgnoOvfl, nPag
18e1a 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
18e1b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63    }..    /* Chec
18e1c 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74  k sanity of left
18e1d 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20   child page..   
18e1e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61   */.    if( !pPa
18e1f 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
18e20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74    pgno = get4byt
18e21 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65  e(pCell);.#ifnde
18e22 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18e23 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69  TOVACUUM.      i
18e24 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
18e25 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  um ){.        ch
18e26 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b  eckPtrmap(pCheck
18e27 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42  , pgno, PTRMAP_B
18e28 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f  TREE, iPage, zCo
18e29 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a  ntext);.      }.
18e2a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20  #endif.      d2 
18e2b 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28  = checkTreePage(
18e2c 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43  pCheck, pgno, zC
18e2d 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69  ontext);.      i
18e2e 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65  f( i>0 && d2!=de
18e2f 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63  pth ){.        c
18e30 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18e31 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18e32 22 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 74  "Child page dept
18e33 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20  h differs");.   
18e34 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68     }.      depth
18e35 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d   = d2;.    }.  }
18e36 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
18e37 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20  eaf ){.    pgno 
18e38 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
18e39 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
18e3a 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
18e3b 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
18e3c 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74  ntf(sizeof(zCont
18e3d 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  ext), zContext, 
18e3e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
18e3f 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25        "On page %
18e40 64 20 61 74 20 72 69 67 68 74 20 63 68 69 6c 64  d at right child
18e41 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66  : ", iPage);.#if
18e42 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18e43 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
18e44 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
18e45 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65  uum ){.      che
18e46 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c  ckPtrmap(pCheck,
18e47 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54   pgno, PTRMAP_BT
18e48 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a  REE, iPage, 0);.
18e49 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
18e4a 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70   checkTreePage(p
18e4b 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f  Check, pgno, zCo
18e4c 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20  ntext);.  }. .  
18e4d 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d  /* Check for com
18e4e 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f  plete coverage o
18e4f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a  f the page.  */.
18e50 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
18e51 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70  aData;.  hdr = p
18e52 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
18e53 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33  .  hit = sqlite3
18e54 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d  PageMalloc( pBt-
18e55 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69  >pageSize );.  i
18e56 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20  f( hit==0 ){.   
18e57 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46   pCheck->mallocF
18e58 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c  ailed = 1;.  }el
18e59 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74  se{.    u16 cont
18e5a 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32  entOffset = get2
18e5b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
18e5c 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ]);.    assert( 
18e5d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75  contentOffset<=u
18e5e 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a  sableSize );  /*
18e5f 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72   Enforced by btr
18e60 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a  eeInitPage() */.
18e61 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b 63      memset(hit+c
18e62 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c  ontentOffset, 0,
18e63 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74   usableSize-cont
18e64 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  entOffset);.    
18e65 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 63  memset(hit, 1, c
18e66 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20  ontentOffset);. 
18e67 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62     nCell = get2b
18e68 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
18e69 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 74  );.    cellStart
18e6a 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a   = hdr + 12 - 4*
18e6b 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20  pPage->leaf;.   
18e6c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
18e6d 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
18e6e 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74 65  nt pc = get2byte
18e6f 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 74  (&data[cellStart
18e70 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31  +i*2]);.      u1
18e71 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20  6 size = 1024;. 
18e72 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
18e73 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65    if( pc<=usable
18e74 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20  Size-4 ){.      
18e75 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a    size = cellSiz
18e76 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74  ePtr(pPage, &dat
18e77 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  a[pc]);.      }.
18e78 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73 69        if( (pc+si
18e79 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a  ze-1)>=usableSiz
18e7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  e ){.        che
18e7b 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18e7c 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20  ck, 0, .        
18e7d 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20      "Corruption 
18e7e 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c  detected in cell
18e7f 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c   %d on page %d",
18e80 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20  i,iPage,0);.    
18e81 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
18e82 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31   for(j=pc+size-1
18e83 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69  ; j>=pc; j--) hi
18e84 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  t[j]++;.      }.
18e85 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 67 65      }.    i = ge
18e86 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
18e87 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  +1]);.    while(
18e88 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e   i>0 ){.      in
18e89 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20  t size, j;.     
18e8a 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73 61 62   assert( i<=usab
18e8b 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 20 20  leSize-4 );     
18e8c 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62  /* Enforced by b
18e8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a  treeInitPage() *
18e8e 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  /.      size = g
18e8f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b  et2byte(&data[i+
18e90 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  2]);.      asser
18e91 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c  t( i+size<=usabl
18e92 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66  eSize );  /* Enf
18e93 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e  orced by btreeIn
18e94 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20  itPage() */.    
18e95 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31    for(j=i+size-1
18e96 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74  ; j>=i; j--) hit
18e97 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d  [j]++;.      j =
18e98 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
18e99 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i]);.      asser
18e9a 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73  t( j==0 || j>i+s
18e9b 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72  ize );  /* Enfor
18e9c 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74  ced by btreeInit
18e9d 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20  Page() */.      
18e9e 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c  assert( j<=usabl
18e9f 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20  eSize-4 );   /* 
18ea0 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65  Enforced by btre
18ea1 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20  eInitPage() */. 
18ea2 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20       i = j;.    
18ea3 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d  }.    for(i=cnt=
18ea4 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b  0; i<usableSize;
18ea5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
18ea6 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20   hit[i]==0 ){.  
18ea7 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
18ea8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69 74     }else if( hit
18ea9 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 20  [i]>1 ){.       
18eaa 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18eab 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20  pCheck, 0,.     
18eac 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 75       "Multiple u
18ead 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64 20  ses for byte %d 
18eae 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c 20  of page %d", i, 
18eaf 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  iPage);.        
18eb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
18eb1 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74     }.    if( cnt
18eb2 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b  !=data[hdr+7] ){
18eb3 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
18eb4 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c  ndMsg(pCheck, 0,
18eb5 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72 61   .          "Fra
18eb6 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64  gmentation of %d
18eb7 20 62 79 74 65 73 20 72 65 70 6f 72 74 65 64 20   bytes reported 
18eb8 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64  as %d on page %d
18eb9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74  ",.          cnt
18eba 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69  , data[hdr+7], i
18ebb 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Page);.    }.  }
18ebc 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72  .  sqlite3PageFr
18ebd 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c 65 61  ee(hit);.  relea
18ebe 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
18ebf 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b   return depth+1;
18ec0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
18ec1 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
18ec2 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
18ec3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18ec4 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
18ec5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
18ec6 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c  ine does a compl
18ec7 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ete check of the
18ec8 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c   given BTree fil
18ec9 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a  e.  aRoot[] is.*
18eca 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61  * an array of pa
18ecb 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65  ges numbers were
18ecc 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65   each page numbe
18ecd 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61  r is the root pa
18ece 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65  ge of.** a table
18ecf 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20  .  nRoot is the 
18ed0 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
18ed1 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a  s in aRoot..**.*
18ed2 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72  * A read-only or
18ed3 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e   read-write tran
18ed4 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  saction must be 
18ed5 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61  opened before ca
18ed6 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75  lling.** this fu
18ed7 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72  nction..**.** Wr
18ed8 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
18ed9 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20  f error seen in 
18eda 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20  *pnErr.  Except 
18edb 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a  for some memory.
18edc 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ** allocation er
18edd 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20  rors,  an error 
18ede 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20  message held in 
18edf 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
18ee0 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69  from.** malloc i
18ee1 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70  s returned if *p
18ee2 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  nErr is non-zero
18ee3 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20  .  If *pnErr==0 
18ee4 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20  then NULL is.** 
18ee5 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20  returned.  If a 
18ee6 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
18ee7 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
18ee8 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
18ee9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18eea 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
18eeb 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43  3BtreeIntegrityC
18eec 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70  heck(.  Btree *p
18eed 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72  ,     /* The btr
18eee 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64  ee to be checked
18eef 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74   */.  int *aRoot
18ef0 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20  ,   /* An array 
18ef1 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75  of root pages nu
18ef2 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69  mbers for indivi
18ef3 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20  dual trees */.  
18ef4 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a  int nRoot,    /*
18ef5 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
18ef6 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f  es in aRoot[] */
18ef7 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20  .  int mxErr,   
18ef8 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69   /* Stop reporti
18ef9 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65 72 20  ng errors after 
18efa 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69  this many */.  i
18efb 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20  nt *pnErr    /* 
18efc 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20  Write number of 
18efd 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74  errors seen to t
18efe 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  his variable */.
18eff 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69  ){.  Pgno i;.  i
18f00 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67  nt nRef;.  Integ
18f01 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20  rityCk sCheck;. 
18f02 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
18f03 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20   p->pBt;.  char 
18f04 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71  zErr[100];..  sq
18f05 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
18f06 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p);.  assert( p-
18f07 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e  >inTrans>TRANS_N
18f08 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 72  ONE && pBt->inTr
18f09 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f  ansaction>TRANS_
18f0a 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d  NONE );.  nRef =
18f0b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
18f0c 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
18f0d 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42 74  r);.  sCheck.pBt
18f0e 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b   = pBt;.  sCheck
18f0f 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  .pPager = pBt->p
18f10 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e  Pager;.  sCheck.
18f11 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67  nPage = pagerPag
18f12 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42  ecount(sCheck.pB
18f13 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45  t);.  sCheck.mxE
18f14 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43  rr = mxErr;.  sC
18f15 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20  heck.nErr = 0;. 
18f16 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61   sCheck.mallocFa
18f17 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45  iled = 0;.  *pnE
18f18 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43  rr = 0;.  if( sC
18f19 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b  heck.nPage==0 ){
18f1a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
18f1b 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72  eLeave(p);.    r
18f1c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
18f1d 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71  Check.anRef = sq
18f1e 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43  lite3Malloc( (sC
18f1f 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69  heck.nPage+1)*si
18f20 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65  zeof(sCheck.anRe
18f21 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 21  f[0]) );.  if( !
18f22 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a  sCheck.anRef ){.
18f23 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a      *pnErr = 1;.
18f24 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
18f25 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65  Leave(p);.    re
18f26 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f  turn 0;.  }.  fo
18f27 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b  r(i=0; i<=sCheck
18f28 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43  .nPage; i++){ sC
18f29 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20  heck.anRef[i] = 
18f2a 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49  0; }.  i = PENDI
18f2b 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
18f2c 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65  );.  if( i<=sChe
18f2d 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20  ck.nPage ){.    
18f2e 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20  sCheck.anRef[i] 
18f2f 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 1;.  }.  sqlit
18f30 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26  e3StrAccumInit(&
18f31 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a  sCheck.errMsg, z
18f32 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72  Err, sizeof(zErr
18f33 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a  ), 20000);..  /*
18f34 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
18f35 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65  rity of the free
18f36 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63  list.  */.  chec
18f37 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31  kList(&sCheck, 1
18f38 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d  , get4byte(&pBt-
18f39 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
18f3a 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  2]),.           
18f3b 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
18f3c 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
18f3d 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69  ]), "Main freeli
18f3e 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68  st: ");..  /* Ch
18f3f 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c  eck all the tabl
18f40 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  es..  */.  for(i
18f41 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74  =0; (int)i<nRoot
18f42 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72   && sCheck.mxErr
18f43 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
18f44 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f  aRoot[i]==0 ) co
18f45 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20  ntinue;.#ifndef 
18f46 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18f47 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70  VACUUM.    if( p
18f48 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26  Bt->autoVacuum &
18f49 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a  & aRoot[i]>1 ){.
18f4a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18f4b 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74  p(&sCheck, aRoot
18f4c 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54  [i], PTRMAP_ROOT
18f4d 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20  PAGE, 0, 0);.   
18f4e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68   }.#endif.    ch
18f4f 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68  eckTreePage(&sCh
18f50 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22  eck, aRoot[i], "
18f51 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f  List of tree roo
18f52 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ts: ");.  }..  /
18f53 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72  * Make sure ever
18f54 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69  y page in the fi
18f55 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  le is referenced
18f56 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b  .  */.  for(i=1;
18f57 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65   i<=sCheck.nPage
18f58 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72   && sCheck.mxErr
18f59 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53  ; i++){.#ifdef S
18f5a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18f5b 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43  ACUUM.    if( sC
18f5c 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30  heck.anRef[i]==0
18f5d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
18f5e 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b  ppendMsg(&sCheck
18f5f 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73  , 0, "Page %d is
18f60 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29   never used", i)
18f61 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ;.    }.#else.  
18f62 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
18f63 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
18f64 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20  to-vacuum, make 
18f65 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63  sure no tables c
18f66 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65  ontain.    ** re
18f67 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e  ferences to poin
18f68 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20  ter-map pages.. 
18f69 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43     */.    if( sC
18f6a 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30  heck.anRef[i]==0
18f6b 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52   && .       (PTR
18f6c 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
18f6d 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61  i)!=i || !pBt->a
18f6e 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20  utoVacuum) ){.  
18f6f 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18f70 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22  sg(&sCheck, 0, "
18f71 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72  Page %d is never
18f72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20   used", i);.    
18f73 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b  }.    if( sCheck
18f74 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20  .anRef[i]!=0 && 
18f75 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f  .       (PTRMAP_
18f76 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d  PAGENO(pBt, i)==
18f77 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61  i && pBt->autoVa
18f78 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63  cuum) ){.      c
18f79 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
18f7a 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74  Check, 0, "Point
18f7b 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69  er map page %d i
18f7c 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69  s referenced", i
18f7d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
18f7e 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
18f7f 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69  ure this analysi
18f80 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20  s did not leave 
18f81 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65  any unref() page
18f82 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  s..  ** This is 
18f83 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73  an internal cons
18f84 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61  istency check; a
18f85 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  n integrity chec
18f86 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e  k.  ** of the in
18f87 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20  tegrity check.. 
18f88 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
18f89 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50  nRef != sqlite3P
18f8a 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74  agerRefcount(pBt
18f8b 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20  ->pPager)) ){.  
18f8c 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18f8d 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20  (&sCheck, 0, .  
18f8e 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67      "Outstanding
18f8f 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73   page count goes
18f90 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64   from %d to %d d
18f91 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79  uring this analy
18f92 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66  sis",.      nRef
18f93 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  , sqlite3PagerRe
18f94 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  fcount(pBt->pPag
18f95 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  er).    );.  }..
18f96 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61    /* Clean  up a
18f97 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73  nd report errors
18f98 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
18f99 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18f9a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43   sqlite3_free(sC
18f9b 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69  heck.anRef);.  i
18f9c 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63  f( sCheck.malloc
18f9d 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71  Failed ){.    sq
18f9e 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
18f9f 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  et(&sCheck.errMs
18fa0 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d  g);.    *pnErr =
18fa1 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a   sCheck.nErr+1;.
18fa2 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
18fa3 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68  }.  *pnErr = sCh
18fa4 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20  eck.nErr;.  if( 
18fa5 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29  sCheck.nErr==0 )
18fa6 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
18fa7 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72  Reset(&sCheck.er
18fa8 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20  rMsg);.  return 
18fa9 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
18faa 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72  inish(&sCheck.er
18fab 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  rMsg);.}.#endif 
18fac 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
18fad 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a  NTEGRITY_CHECK *
18fae 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
18faf 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  the full pathnam
18fb0 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79  e of the underly
18fb1 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c  ing database fil
18fb2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  e..**.** The pag
18fb3 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69  er filename is i
18fb4 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67  nvariant as long
18fb5 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 73   as the pager is
18fb6 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69  .** open so it i
18fb7 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73  s safe to access
18fb8 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 53   without the BtS
18fb9 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a  hared mutex..*/.
18fba 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
18fbb 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
18fbc 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
18fbd 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  me(Btree *p){.  
18fbe 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e  assert( p->pBt->
18fbf 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72  pPager!=0 );.  r
18fc0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67  eturn sqlite3Pag
18fc1 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42  erFilename(p->pB
18fc2 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  t->pPager);.}../
18fc3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
18fc4 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
18fc5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
18fc6 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2e 20   this database. 
18fc7 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  The return.** va
18fc8 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74  lue of this rout
18fc9 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  ine is the same 
18fca 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
18fcb 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61  ether the journa
18fcc 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65  l file.** has be
18fcd 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f  en created or no
18fce 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  t..**.** The pag
18fcf 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e  er journal filen
18fd0 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74  ame is invariant
18fd1 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20   as long as the 
18fd2 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e  pager is.** open
18fd3 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74   so it is safe t
18fd4 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74  o access without
18fd5 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75   the BtShared mu
18fd6 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tex..*/.SQLITE_P
18fd7 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
18fd8 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47  r *sqlite3BtreeG
18fd9 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74  etJournalname(Bt
18fda 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
18fdb 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65  t( p->pBt->pPage
18fdc 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  r!=0 );.  return
18fdd 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
18fde 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d  rnalname(p->pBt-
18fdf 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  >pPager);.}../*.
18fe0 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  ** Return non-ze
18fe1 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ro if a transact
18fe2 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a  ion is active..*
18fe3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18fe4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
18fe5 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65  eIsInTrans(Btree
18fe6 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
18fe7 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  p==0 || sqlite3_
18fe8 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
18fe9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65  ->mutex) );.  re
18fea 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69  turn (p && (p->i
18feb 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
18fec 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ITE));.}../*.** 
18fed 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
18fee 69 66 20 61 20 72 65 61 64 20 28 6f 72 20 77 72  if a read (or wr
18fef 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e  ite) transaction
18ff0 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53   is active..*/.S
18ff1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18ff2 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
18ff3 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 65  InReadTrans(Btre
18ff4 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
18ff5 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p );.  assert( 
18ff6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
18ff7 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
18ff8 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e   );.  return p->
18ff9 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e  inTrans!=TRANS_N
18ffa 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50  ONE;.}..SQLITE_P
18ffb 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18ffc 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75  e3BtreeIsInBacku
18ffd 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  p(Btree *p){.  a
18ffe 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73  ssert( p );.  as
18fff 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
19000 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
19001 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75  mutex) );.  retu
19002 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30  rn p->nBackup!=0
19003 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
19004 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
19005 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
19006 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61  blob of memory a
19007 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a  ssociated with.*
19008 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65  * a single share
19009 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d  d-btree. The mem
1900a 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20 63  ory is used by c
1900b 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69  lient code for i
1900c 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73  ts own.** purpos
1900d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c  es (for example,
1900e 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68   to store a high
1900f 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73  -level schema as
19010 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a  sociated with .*
19011 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  * the shared-btr
19012 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c  ee). The btree l
19013 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66  ayer manages ref
19014 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20  erence counting 
19015 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  issues..**.** Th
19016 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
19017 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61  s is called on a
19018 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e   shared-btree, n
19019 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d  Bytes bytes of m
1901a 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c  emory.** are all
1901b 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20  ocated, zeroed, 
1901c 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20  and returned to 
1901d 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20  the caller. For 
1901e 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 20  each subsequent 
1901f 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79  .** call the nBy
19020 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  tes parameter is
19021 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70   ignored and a p
19022 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61  ointer to the sa
19023 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65  me blob.** of me
19024 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a  mory returned. .
19025 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79  **.** If the nBy
19026 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  tes parameter is
19027 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20   0 and the blob 
19028 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f  of memory has no
19029 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c  t yet been.** al
1902a 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20  located, a null 
1902b 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72  pointer is retur
1902c 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62  ned. If the blob
1902d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1902e 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20  n.** allocated, 
1902f 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  it is returned a
19030 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20  s normal..**.** 
19031 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20  Just before the 
19032 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73 20  shared-btree is 
19033 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63  closed, the func
19034 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74  tion passed as t
19035 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67  he .** xFree arg
19036 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d  ument when the m
19037 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
19038 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76   was made is inv
19039 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20  oked on the .** 
1903a 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65  blob of allocate
1903b 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66  d memory. This f
1903c 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  unction should n
1903d 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  ot call sqlite3_
1903e 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65  free().** on the
1903f 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72   memory, the btr
19040 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68  ee layer does th
19041 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  at..*/.SQLITE_PR
19042 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
19043 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42  te3BtreeSchema(B
19044 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79  tree *p, int nBy
19045 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65  tes, void(*xFree
19046 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74  )(void *)){.  Bt
19047 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
19048 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42  >pBt;.  sqlite3B
19049 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1904a 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d  if( !pBt->pSchem
1904b 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20  a && nBytes ){. 
1904c 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20     pBt->pSchema 
1904d 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
1904e 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20  ero(nBytes);.   
1904f 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d   pBt->xFreeSchem
19050 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20  a = xFree;.  }. 
19051 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
19052 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
19053 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a  pBt->pSchema;.}.
19054 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  ./*.** Return SQ
19055 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52  LITE_LOCKED_SHAR
19056 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68  EDCACHE if anoth
19057 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20 73  er user of the s
19058 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62  ame shared .** b
19059 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67 75  tree as the argu
1905a 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64  ment handle hold
1905b 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
1905c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73  ock on the .** s
1905d 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1905e 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51  le. Otherwise SQ
1905f 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49  LITE_OK..*/.SQLI
19060 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19061 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
19062 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70  aLocked(Btree *p
19063 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  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 2d 3e 64 62 2d  utex_held(p->db-
19066 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
19067 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
19068 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53  );.  rc = queryS
19069 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1906a 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f  ock(p, MASTER_RO
1906b 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a  OT, READ_LOCK);.
1906c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1906d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
1906e 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
1906f 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71  REDCACHE );.  sq
19070 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
19071 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
19072 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .}...#ifndef SQL
19073 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
19074 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61  CACHE./*.** Obta
19075 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  in a lock on the
19076 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f   table whose roo
19077 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e 20  t page is iTab. 
19078 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20   The.** lock is 
19079 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20  a write lock if 
1907a 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74  isWritelock is t
1907b 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f  rue or a read lo
1907c 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66  ck.** if it is f
1907d 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alse..*/.SQLITE_
1907e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1907f 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c  te3BtreeLockTabl
19080 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
19081 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65  iTab, u8 isWrite
19082 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20  Lock){.  int rc 
19083 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
19084 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
19085 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b  s!=TRANS_NONE );
19086 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62  .  if( p->sharab
19087 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63  le ){.    u8 loc
19088 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43  kType = READ_LOC
19089 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b  K + isWriteLock;
1908a 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 45 41  .    assert( REA
1908b 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f  D_LOCK+1==WRITE_
1908c 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65  LOCK );.    asse
1908d 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d  rt( isWriteLock=
1908e 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63  =0 || isWriteLoc
1908f 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c  k==1 );..    sql
19090 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
19091 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 72  );.    rc = quer
19092 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
19093 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c  eLock(p, iTab, l
19094 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 66  ockType);.    if
19095 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19096 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65  ){.      rc = se
19097 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
19098 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c  eLock(p, iTab, l
19099 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a  ockType);.    }.
1909a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1909b 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20  Leave(p);.  }.  
1909c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
1909d 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1909e 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1909f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  B./*.** Argument
190a0 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 20   pCsr must be a 
190a1 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f  cursor opened fo
190a2 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20  r writing on an 
190a3 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  .** INTKEY table
190a4 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
190a5 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 74  ing at a valid t
190a6 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20  able entry. .** 
190a7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f  This function mo
190a8 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61 20  difies the data 
190a9 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f  stored as part o
190aa 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a  f that entry..**
190ab 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74  .** Only the dat
190ac 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e  a content may on
190ad 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20  ly be modified, 
190ae 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
190af 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65  le to .** change
190b0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
190b1 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e 20  he data stored. 
190b2 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  If this function
190b3 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0a   is called with.
190b4 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68  ** parameters th
190b5 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72  at attempt to wr
190b6 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  ite past the end
190b7 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 6e 67   of the existing
190b8 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64   data,.** no mod
190b9 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20 6d  ifications are m
190ba 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 43  ade and SQLITE_C
190bb 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72 6e  ORRUPT is return
190bc 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
190bd 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
190be 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74  3BtreePutData(Bt
190bf 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33  Cursor *pCsr, u3
190c0 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
190c1 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69  t, void *z){.  i
190c2 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
190c3 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
190c4 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73  x(pCsr) );.  ass
190c5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
190c6 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42  ex_held(pCsr->pB
190c7 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
190c8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
190c9 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  sr->isIncrblobHa
190ca 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20  ndle );..  rc = 
190cb 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
190cc 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69  ition(pCsr);.  i
190cd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
190ce 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
190cf 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  c;.  }.  assert(
190d0 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCsr->eState!=C
190d1 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
190d2 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d  K );.  if( pCsr-
190d3 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
190d4 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74  VALID ){.    ret
190d5 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  urn SQLITE_ABORT
190d6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
190d7 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f  k some assumptio
190d8 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20  ns: .  **   (a) 
190d9 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70  the cursor is op
190da 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a  en for writing,.
190db 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65    **   (b) there
190dc 20 69 73 20 61 20 72 65 61 64 2f 77 72 69 74 65   is a read/write
190dd 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
190de 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68  n,.  **   (c) th
190df 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c  e connection hol
190e0 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  ds a write-lock 
190e1 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28 69 66  on the table (if
190e2 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a   required),.  **
190e3 20 20 20 28 64 29 20 74 68 65 72 65 20 61 72 65     (d) there are
190e4 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20   no conflicting 
190e5 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a  read-locks, and.
190e6 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65 20 63    **   (e) the c
190e7 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20  ursor points at 
190e8 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61  a valid row of a
190e9 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a  n intKey table..
190ea 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72    */.  if( !pCsr
190eb 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20  ->wrFlag ){.    
190ec 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45  return SQLITE_RE
190ed 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73  ADONLY;.  }.  as
190ee 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74  sert( !pCsr->pBt
190ef 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 70 43  ->readOnly && pC
190f0 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73  sr->pBt->inTrans
190f1 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
190f2 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
190f3 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54   hasSharedCacheT
190f4 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70  ableLock(pCsr->p
190f5 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e  Btree, pCsr->pgn
190f6 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a  oRoot, 0, 2) );.
190f7 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65    assert( !hasRe
190f8 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72  adConflicts(pCsr
190f9 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e  ->pBtree, pCsr->
190fa 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61  pgnoRoot) );.  a
190fb 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 70 50  ssert( pCsr->apP
190fc 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d  age[pCsr->iPage]
190fd 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72  ->intKey );..  r
190fe 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c  eturn accessPayl
190ff 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74  oad(pCsr, offset
19100 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64  , amt, (unsigned
19101 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d   char *)z, 1);.}
19102 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66  ../* .** Set a f
19103 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72 73  lag on this curs
19104 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65 20  or to cache the 
19105 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 67  locations of pag
19106 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20  es from the .** 
19107 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f  overflow list fo
19108 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
19109 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  w. This is used 
1910a 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65  by cursors opene
1910b 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65  d.** for increme
1910c 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c  ntal blob IO onl
1910d 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  y..**.** This fu
1910e 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66 6c  nction sets a fl
1910f 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74  ag only. The act
19110 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 6f  ual page locatio
19111 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72  n cache.** (stor
19112 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 61  ed in BtCursor.a
19113 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61  Overflow[]) is a
19114 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73 65  llocated and use
19115 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  d by function.**
19116 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29   accessPayload()
19117 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75 6e   (the worker fun
19118 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65  ction for sqlite
19119 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e 64  3BtreeData() and
1911a 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65  .** sqlite3Btree
1911b 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53  PutData())..*/.S
1911c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1911d 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
1911e 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43  acheOverflow(BtC
1911f 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
19120 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
19121 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
19122 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
19123 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
19124 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
19125 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
19126 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e 63  ert(!pCur->isInc
19127 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20  rblobHandle);.  
19128 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f  assert(!pCur->aO
19129 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72  verflow);.  pCur
1912a 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
1912b 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  le = 1;.}.#endif
1912c 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1912d 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 63  * End of btree.c
1912e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1912f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19131 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
19132 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 61 63  * Begin file bac
19133 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  kup.c **********
19134 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19135 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19136 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e  /./*.** 2009 Jan
19137 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68  uary 28.**.** Th
19138 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
19139 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1913a 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1913b 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1913c 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1913d 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1913e 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1913f 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
19140 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
19141 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
19142 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
19143 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
19144 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
19145 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
19146 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
19147 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
19148 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
19149 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1914a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1914b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1914c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1914d 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1914e 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
1914f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
19150 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  of the sqlite3_b
19151 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20  ackup_XXX() .** 
19152 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  API functions an
19153 64 20 74 68 65 20 72 65 6c 61 74 65 64 20 66 65  d the related fe
19154 61 74 75 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d  atures..*/../* M
19155 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65  acro to find the
19156 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20   minimum of two 
19157 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a  numeric values..
19158 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23  */.#ifndef MIN.#
19159 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29   define MIN(x,y)
1915a 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79   ((x)<(y)?(x):(y
1915b 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )).#endif../*.**
1915c 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63   Structure alloc
1915d 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 62 61  ated for each ba
1915e 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  ckup operation..
1915f 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
19160 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c  3_backup {.  sql
19161 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b 20 20  ite3* pDestDb;  
19162 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61        /* Destina
19163 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61  tion database ha
19164 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20  ndle */.  Btree 
19165 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 20  *pDest;         
19166 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
19167 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f  n b-tree file */
19168 0a 20 20 75 33 32 20 69 44 65 73 74 53 63 68 65  .  u32 iDestSche
19169 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ma;         /* O
1916a 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61 20 63  riginal schema c
1916b 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69 6e 61  ookie in destina
1916c 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44  tion */.  int bD
1916d 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20  estLocked;      
1916e 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20     /* True once 
1916f 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
19170 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70  ion is open on p
19171 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20  Dest */..  Pgno 
19172 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  iNext;          
19173 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
19174 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73  er of the next s
19175 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20 63 6f  ource page to co
19176 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a  py */.  sqlite3*
19177 20 70 53 72 63 44 62 3b 20 20 20 20 20 20 20 20   pSrcDb;        
19178 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62   /* Source datab
19179 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
1917a 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20 20 20  Btree *pSrc;    
1917b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
1917c 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a  ce b-tree file *
1917d 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  /..  int rc;    
1917e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1917f 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20   Backup process 
19180 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20  error code */.. 
19181 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 76 61   /* These two va
19182 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 74 20  riables are set 
19183 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 74 6f  by every call to
19184 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20   backup_step(). 
19185 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20 72 65  They are.  ** re
19186 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 62  ad by calls to b
19187 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
19188 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 61 67  ) and backup_pag
19189 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20  ecount()..  */. 
1918a 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67   Pgno nRemaining
1918b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
1918c 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c 65 66  ber of pages lef
1918d 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50  t to copy */.  P
1918e 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20  gno nPagecount; 
1918f 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
19190 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
19191 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69   to copy */..  i
19192 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b 20 20  nt isAttached;  
19193 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
19194 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61 73 20  once backup has 
19195 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 20  been registered 
19196 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a 20 20  with pager */.  
19197 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19198 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74  pNext;   /* Next
19199 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69 61 74   backup associat
1919a 65 64 20 77 69 74 68 20 73 6f 75 72 63 65 20 70  ed with source p
1919b 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ager */.};../*.*
1919c 2a 20 54 48 52 45 41 44 20 53 41 46 45 54 59 20  * THREAD SAFETY 
1919d 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  NOTES:.**.**   O
1919e 6e 63 65 20 69 74 20 68 61 73 20 62 65 65 6e 20  nce it has been 
1919f 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 62 61  created using ba
191a0 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61 20 73  ckup_init(), a s
191a1 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f 62 61  ingle sqlite3_ba
191a2 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75 63 74  ckup.**   struct
191a3 75 72 65 20 6d 61 79 20 62 65 20 61 63 63 65 73  ure may be acces
191a4 73 65 64 20 76 69 61 20 74 77 6f 20 67 72 6f 75  sed via two grou
191a5 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73 61 66  ps of thread-saf
191a6 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a  e entry points:.
191a7 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 20  **.**     * Via 
191a8 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
191a9 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e  up_XXX() API fun
191aa 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65  ction backup_ste
191ab 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20  p() and .**     
191ac 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28    backup_finish(
191ad 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 66 75  ). Both these fu
191ae 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74  nctions obtain t
191af 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
191b0 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61 6e 64  se.**       hand
191b1 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74 68 65  le mutex and the
191b2 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
191b3 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63  d with the sourc
191b4 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a 20 20  e BtShared .**  
191b5 20 20 20 20 20 73 74 72 75 63 74 75 72 65 2c 20       structure, 
191b6 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a  in that order..*
191b7 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74  *.**     * Via t
191b8 68 65 20 42 61 63 6b 75 70 55 70 64 61 74 65 28  he BackupUpdate(
191b9 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65 73 74  ) and BackupRest
191ba 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c  art() functions,
191bb 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 20 20   which are.**   
191bc 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79 20 74      invoked by t
191bd 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 74  he pager layer t
191be 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f 75 73  o report various
191bf 20 73 74 61 74 65 20 63 68 61 6e 67 65 73 20 69   state changes i
191c0 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 20 70  n.**       the p
191c1 61 67 65 20 63 61 63 68 65 20 61 73 73 6f 63 69  age cache associ
191c2 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f  ated with the so
191c3 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54  urce database. T
191c4 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20  he mutex.**     
191c5 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74    associated wit
191c6 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  h the source dat
191c7 61 62 61 73 65 20 42 74 53 68 61 72 65 64 20 73  abase BtShared s
191c8 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 61 6c  tructure will al
191c9 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20 20 62  ways .**       b
191ca 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69 74 68  e held when eith
191cb 65 72 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63  er of these func
191cc 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65  tions are invoke
191cd 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f  d..**.**   The o
191ce 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62 61 63  ther sqlite3_bac
191cf 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 75  kup_XXX() API fu
191d0 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f  nctions, backup_
191d1 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a  remaining() and.
191d2 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61 67 65  **   backup_page
191d3 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f 74 20  count() are not 
191d4 74 68 72 65 61 64 2d 73 61 66 65 20 66 75 6e 63  thread-safe func
191d5 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79 20 61  tions. If they a
191d6 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77  re called.**   w
191d7 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  hile some other 
191d8 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c 69 6e  thread is callin
191d9 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  g backup_step() 
191da 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  or backup_finish
191db 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c  (),.**   the val
191dc 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d 61 79  ues returned may
191dd 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54 68 65   be invalid. The
191de 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72  re is no way for
191df 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20   a call to.**   
191e0 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 6f  BackupUpdate() o
191e1 72 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 28  r BackupRestart(
191e2 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65 20 77  ) to interfere w
191e3 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d 61 69  ith backup_remai
191e4 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62  ning().**   or b
191e5 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28  ackup_pagecount(
191e6 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e  )..**.**   Depen
191e7 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51 4c 69  ding on the SQLi
191e8 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  te configuration
191e9 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  , the database h
191ea 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a  andles and/or.**
191eb 20 20 20 74 68 65 20 42 74 72 65 65 20 6f 62 6a     the Btree obj
191ec 65 63 74 73 20 6d 61 79 20 68 61 76 65 20 74 68  ects may have th
191ed 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65 73 20  eir own mutexes 
191ee 74 68 61 74 20 72 65 71 75 69 72 65 20 6c 6f 63  that require loc
191ef 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73  king..**   Non-s
191f0 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 28  harable Btrees (
191f1 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
191f2 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c 65 29  ses for example)
191f3 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a  , do not have.**
191f4 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 6d 75     associated mu
191f5 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  texes..*/../*.**
191f6 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
191f7 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  r corresponding 
191f8 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44 62 20  to database zDb 
191f9 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74  (i.e. "main", "t
191fa 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e  emp").** in conn
191fb 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44  ection handle pD
191fc 62 2e 20 49 66 20 73 75 63 68 20 61 20 64 61 74  b. If such a dat
191fd 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20  abase cannot be 
191fe 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a  found, return.**
191ff 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
19200 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65 72 72  and write an err
19201 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45  or message to pE
19202 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66  rrorDb..**.** If
19203 20 74 68 65 20 22 74 65 6d 70 22 20 64 61 74 61   the "temp" data
19204 62 61 73 65 20 69 73 20 72 65 71 75 65 73 74 65  base is requeste
19205 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74  d, it may need t
19206 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79 20 74  o be opened by t
19207 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
19208 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
19209 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67  curs while doing
1920a 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 61 6e   so, return 0 an
1920b 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65  d write an .** e
1920c 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
1920d 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61  pErrorDb..*/.sta
1920e 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e 64 42  tic Btree *findB
1920f 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a 70 45  tree(sqlite3 *pE
19210 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 33 20  rrorDb, sqlite3 
19211 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  *pDb, const char
19212 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20 69 20   *zDb){.  int i 
19213 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e  = sqlite3FindDbN
19214 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a  ame(pDb, zDb);..
19215 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20    if( i==1 ){.  
19216 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
19217 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  .    int rc = 0;
19218 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20 73 71  .    pParse = sq
19219 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a  lite3StackAllocZ
1921a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20 73 69  ero(pErrorDb, si
1921b 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a  zeof(*pParse));.
1921c 20 20 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d      if( pParse==
1921d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1921e 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62  e3Error(pErrorDb
1921f 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  , SQLITE_NOMEM, 
19220 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29  "out of memory")
19221 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
19222 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
19223 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 72  else{.      pPar
19224 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20  se->db = pDb;.  
19225 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
19226 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28  penTempDatabase(
19227 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
19228 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43     sqlite3ErrorC
19229 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
1922a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1922b 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61  or(pErrorDb, pPa
1922c 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c 20 70  rse->rc, "%s", p
1922d 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b  Parse->zErrMsg);
1922e 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1922f 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
19230 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
19231 33 53 74 61 63 6b 46 72 65 65 28 70 45 72 72 6f  3StackFree(pErro
19232 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20  rDb, pParse);.  
19233 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29    }.    if( rc )
19234 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
19235 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
19236 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71  f( i<0 ){.    sq
19237 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f  lite3Error(pErro
19238 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f  rDb, SQLITE_ERRO
19239 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61  R, "unknown data
1923a 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29 3b 0a  base %s", zDb);.
1923b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1923c 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44 62 2d  }..  return pDb-
1923d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a  >aDb[i].pBt;.}..
1923e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 20  /*.** Create an 
1923f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 70  sqlite3_backup p
19240 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 20 74  rocess to copy t
19241 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a  he contents of z
19242 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f  SrcDb from.** co
19243 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
19244 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 74 44  pSrcDb to zDestD
19245 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 49 66  b in pDestDb. If
19246 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74   successful, ret
19247 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
19248 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 6c 69   to the new sqli
19249 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63  te3_backup objec
1924a 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  t..**.** If an e
1924b 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c  rror occurs, NUL
1924c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  L is returned an
1924d 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
1924e 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  and error messag
1924f 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 64  e.** stored in d
19250 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 70  atabase handle p
19251 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54  DestDb..*/.SQLIT
19252 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61  E_API sqlite3_ba
19253 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61  ckup *sqlite3_ba
19254 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c  ckup_init(.  sql
19255 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c 20 20  ite3* pDestDb,  
19256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19257 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74     /* Database t
19258 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20  o write to */.  
19259 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73  const char *zDes
1925a 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tDb,            
1925b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1925c 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e   database within
1925d 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 73 71   pDestDb */.  sq
1925e 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c 20 20  lite3* pSrcDb,  
1925f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19260 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
19261 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65  connection to re
19262 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e  ad from */.  con
19263 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 62 20  st char *zSrcDb 
19264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19265 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
19266 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 53  tabase within pS
19267 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  rcDb */.){.  sql
19268 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20  ite3_backup *p; 
19269 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1926a 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
1926b 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c  eturn */..  /* L
1926c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 20 64  ock the source d
1926d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
1926e 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  The destination 
1926f 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68 61  database.  ** ha
19270 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b  ndle is not lock
19271 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69  ed in this routi
19272 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 6c 6f  ne, but it is lo
19273 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 71 6c  cked in.  ** sql
19274 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
19275 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 73 20  (). The user is 
19276 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e 73 75  required to ensu
19277 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a 2a 20  re that no.  ** 
19278 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 63 63  other thread acc
19279 65 73 73 65 73 20 74 68 65 20 64 65 73 74 69 6e  esses the destin
1927a 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72  ation handle for
1927b 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a 20 20   the duration.  
1927c 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b 75 70  ** of the backup
1927d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79   operation.  Any
1927e 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
1927f 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a  the destination.
19280 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f    ** database co
19281 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 61  nnection while a
19282 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 72   backup is in pr
19283 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 73 65  ogress may cause
19284 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e 63 74  .  ** a malfunct
19285 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c 6f 63  ion or a deadloc
19286 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  k..  */.  sqlite
19287 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 53  3_mutex_enter(pS
19288 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  rcDb->mutex);.  
19289 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1928a 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d 75 74  ter(pDestDb->mut
1928b 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 72 63  ex);..  if( pSrc
1928c 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b 0a 20  Db==pDestDb ){. 
1928d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1928e 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 44 62  .        pDestDb
1928f 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  , SQLITE_ERROR, 
19290 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74  "source and dest
19291 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  ination must be 
19292 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 29 3b  distinct".    );
19293 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65  .    p = 0;.  }e
19294 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c  lse {.    /* All
19295 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
19296 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 61  a new sqlite3_ba
19297 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ckup object */. 
19298 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f     p = (sqlite3_
19299 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74 65 33  backup *)sqlite3
1929a 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73  _malloc(sizeof(s
1929b 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b  qlite3_backup));
1929c 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20  .    if( !p ){. 
1929d 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1929e 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c 49 54  r(pDestDb, SQLIT
1929f 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20  E_NOMEM, 0);.   
192a0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
192a1 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  the allocation s
192a2 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75 6c 61  ucceeded, popula
192a3 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63  te the new objec
192a4 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b  t. */.  if( p ){
192a5 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
192a6 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  , sizeof(sqlite3
192a7 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20 70  _backup));.    p
192a8 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42 74 72  ->pSrc = findBtr
192a9 65 65 28 70 44 65 73 74 44 62 2c 20 70 53 72 63  ee(pDestDb, pSrc
192aa 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20 20 20  Db, zSrcDb);.   
192ab 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69 6e 64   p->pDest = find
192ac 42 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70  Btree(pDestDb, p
192ad 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44 62 29  DestDb, zDestDb)
192ae 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 44 62  ;.    p->pDestDb
192af 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20 20 20   = pDestDb;.    
192b0 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53 72 63  p->pSrcDb = pSrc
192b1 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74  Db;.    p->iNext
192b2 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69 73 41   = 1;.    p->isA
192b3 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20  ttached = 0;..  
192b4 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63    if( 0==p->pSrc
192b5 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20   || 0==p->pDest 
192b6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20  ){.      /* One 
192b7 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 68 65  (or both) of the
192b8 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 73   named databases
192b9 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74 2e 20   did not exist. 
192ba 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 20 20  An error has.   
192bb 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65     ** already be
192bc 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  en written into 
192bd 74 68 65 20 70 44 65 73 74 44 62 20 68 61 6e 64  the pDestDb hand
192be 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 73 20  le. All that is 
192bf 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f  left.      ** to
192c0 20 64 6f 20 68 65 72 65 20 69 73 20 66 72 65 65   do here is free
192c1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   the sqlite3_bac
192c2 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20  kup structure.. 
192c3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
192c4 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
192c5 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20       p = 0;.    
192c6 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b  }.  }.  if( p ){
192c7 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42  .    p->pSrc->nB
192c8 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20  ackup++;.  }..  
192c9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
192ca 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74  ave(pDestDb->mut
192cb 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ex);.  sqlite3_m
192cc 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44  utex_leave(pSrcD
192cd 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
192ce 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
192cf 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61  Argument rc is a
192d0 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
192d1 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65  ode. Return true
192d2 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 20 69   if this error i
192d3 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64  s .** considered
192d4 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e   fatal if encoun
192d5 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 20 62  tered during a b
192d6 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
192d7 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61   All errors.** a
192d8 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61  re considered fa
192d9 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53  tal except for S
192da 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53  QLITE_BUSY and S
192db 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f  QLITE_LOCKED..*/
192dc 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 46 61  .static int isFa
192dd 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29  talError(int rc)
192de 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d  {.  return (rc!=
192df 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
192e0 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20  =SQLITE_BUSY && 
192e1 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c 49 54  ALWAYS(rc!=SQLIT
192e2 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f  E_LOCKED));.}../
192e3 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
192e4 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 20 74  SrcData points t
192e5 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  o a buffer conta
192e6 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 20 66  ining the data f
192e7 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 72 63  or .** page iSrc
192e8 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72  Pg from the sour
192e9 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6f 70  ce database. Cop
192ea 79 20 74 68 69 73 20 64 61 74 61 20 69 6e 74 6f  y this data into
192eb 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61   the .** destina
192ec 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a  tion database..*
192ed 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63  /.static int bac
192ee 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c 69 74  kupOnePage(sqlit
192ef 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 50 67  e3_backup *p, Pg
192f0 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e 73 74  no iSrcPg, const
192f1 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a   u8 *zSrcData){.
192f2 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20    Pager * const 
192f3 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c  pDestPager = sql
192f4 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
192f5 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73  ->pDest);.  cons
192f6 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d  t int nSrcPgsz =
192f7 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
192f8 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63  PageSize(p->pSrc
192f9 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 50 67  );.  int nDestPg
192fa 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  sz = sqlite3Btre
192fb 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e  eGetPageSize(p->
192fc 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20  pDest);.  const 
192fd 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28  int nCopy = MIN(
192fe 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 74 50  nSrcPgsz, nDestP
192ff 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36  gsz);.  const i6
19300 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 69 53  4 iEnd = (i64)iS
19301 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 50 67  rcPg*(i64)nSrcPg
19302 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20  sz;..  int rc = 
19303 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 36 34  SQLITE_OK;.  i64
19304 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 72 74   iOff;..  assert
19305 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64  ( p->bDestLocked
19306 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69   );.  assert( !i
19307 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72  sFatalError(p->r
19308 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  c) );.  assert( 
19309 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f  iSrcPg!=PENDING_
1930a 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72  BYTE_PAGE(p->pSr
1930b 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61 73 73  c->pBt) );.  ass
1930c 65 72 74 28 20 7a 53 72 63 44 61 74 61 20 29 3b  ert( zSrcData );
1930d 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74 68 65  ..  /* Catch the
1930e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
1930f 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61  destination is a
19310 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
19311 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20 20 2a  base and the.  *
19312 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f 66 20  * page sizes of 
19313 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64  the source and d
19314 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66 66 65  estination diffe
19315 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  r. .  */.  if( n
19316 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74 50 67  SrcPgsz!=nDestPg
19317 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50 61 67  sz && sqlite3Pag
19318 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69 74 65  erIsMemdb(sqlite
19319 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70  3BtreePager(p->p
1931a 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20 72 63  Dest)) ){.    rc
1931b 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e   = SQLITE_READON
1931c 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  LY;.  }..  /* Th
1931d 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63  is loop runs onc
1931e 65 20 66 6f 72 20 65 61 63 68 20 64 65 73 74 69  e for each desti
1931f 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70 61 6e  nation page span
19320 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75 72 63  ned by the sourc
19321 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20 46 6f  e .  ** page. Fo
19322 72 20 65 61 63 68 20 69 74 65 72 61 74 69 6f 6e  r each iteration
19323 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66 66 20  , variable iOff 
19324 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79  is set to the by
19325 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a 20 6f  te offset.  ** o
19326 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  f the destinatio
19327 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 66  n page..  */.  f
19328 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28 69 36  or(iOff=iEnd-(i6
19329 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63 3d 3d  4)nSrcPgsz; rc==
1932a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66  SQLITE_OK && iOf
1932b 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44  f<iEnd; iOff+=nD
1932c 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20 44 62  estPgsz){.    Db
1932d 50 61 67 65 20 2a 70 44 65 73 74 50 67 20 3d 20  Page *pDestPg = 
1932e 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44 65 73  0;.    Pgno iDes
1932f 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f  t = (Pgno)(iOff/
19330 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a 20 20  nDestPgsz)+1;.  
19331 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50 45 4e    if( iDest==PEN
19332 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
19333 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 20  ->pDest->pBt) ) 
19334 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
19335 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
19336 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
19337 65 74 28 70 44 65 73 74 50 61 67 65 72 2c 20 69  et(pDestPager, i
19338 44 65 73 74 2c 20 26 70 44 65 73 74 50 67 29 29  Dest, &pDestPg))
19339 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f  .     && SQLITE_
1933a 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
1933b 33 50 61 67 65 72 57 72 69 74 65 28 70 44 65 73  3PagerWrite(pDes
1933c 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20 20 20  tPg)).    ){.   
1933d 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e     const u8 *zIn
1933e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69 4f 66   = &zSrcData[iOf
1933f 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20 20 20  f%nSrcPgsz];.   
19340 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61 74 61     u8 *zDestData
19341 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
19342 65 74 44 61 74 61 28 70 44 65 73 74 50 67 29 3b  etData(pDestPg);
19343 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75 74 20  .      u8 *zOut 
19344 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69 4f 66  = &zDestData[iOf
19345 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a 0a 20  f%nDestPgsz];.. 
19346 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65       /* Copy the
19347 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73   data from the s
19348 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74 6f 20  ource page into 
19349 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
1934a 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54  page..      ** T
1934b 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 42 74  hen clear the Bt
1934c 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50 61 67  ree layer MemPag
1934d 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e 20 42  e.isInit flag. B
1934e 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c 65 0a  oth this module.
1934f 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65        ** and the
19350 20 70 61 67 65 72 20 63 6f 64 65 20 75 73 65 20   pager code use 
19351 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c 65 61  this trick (clea
19352 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 62  ring the first b
19353 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  yte.      ** of 
19354 74 68 65 20 70 61 67 65 20 27 65 78 74 72 61 27  the page 'extra'
19355 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61 6c 69   space to invali
19356 64 61 74 65 20 74 68 65 20 42 74 72 65 65 20 6c  date the Btree l
19357 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 63  ayers.      ** c
19358 61 63 68 65 64 20 70 61 72 73 65 20 6f 66 20 74  ached parse of t
19359 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50 61 67  he page). MemPag
1935a 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61 72 6b  e.isInit is mark
1935b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22 4d 55  ed .      ** "MU
1935c 53 54 20 42 45 20 46 49 52 53 54 22 20 66 6f 72  ST BE FIRST" for
1935d 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e 0a 20   this purpose.. 
1935e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d 65       */.      me
1935f 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20  mcpy(zOut, zIn, 
19360 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 28 28  nCopy);.      ((
19361 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  u8 *)sqlite3Page
19362 72 47 65 74 45 78 74 72 61 28 70 44 65 73 74 50  rGetExtra(pDestP
19363 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20  g))[0] = 0;.    
19364 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  }.    sqlite3Pag
19365 65 72 55 6e 72 65 66 28 70 44 65 73 74 50 67 29  erUnref(pDestPg)
19366 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
19367 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
19368 70 46 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74  pFile is current
19369 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69  ly larger than i
1936a 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e  Size bytes, then
1936b 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 6f 0a   truncate it to.
1936c 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69 7a 65  ** exactly iSize
1936d 20 62 79 74 65 73 2e 20 49 66 20 70 46 69 6c 65   bytes. If pFile
1936e 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72 20 74   is not larger t
1936f 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73 2c  han iSize bytes,
19370 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75   then.** this fu
19371 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
19372 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  p..**.** Return 
19373 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65  SQLITE_OK if eve
19374 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65  rything is succe
19375 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c  ssful, or an SQL
19376 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f  ite error .** co
19377 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  de if an error o
19378 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ccurs..*/.static
19379 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75 6e 63   int backupTrunc
1937a 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65 33 5f  ateFile(sqlite3_
1937b 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 36 34  file *pFile, i64
1937c 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34 20 69   iSize){.  i64 i
1937d 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74 20 72  Current;.  int r
1937e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
1937f 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26 69 43  eSize(pFile, &iC
19380 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72  urrent);.  if( r
19381 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
19382 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65 20 29  iCurrent>iSize )
19383 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
19384 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 46 69  e3OsTruncate(pFi
19385 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a  le, iSize);.  }.
19386 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19387 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
19388 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a 65 63  his backup objec
19389 74 20 77 69 74 68 20 74 68 65 20 61 73 73 6f 63  t with the assoc
1938a 69 61 74 65 64 20 73 6f 75 72 63 65 20 70 61 67  iated source pag
1938b 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c 62 61  er for.** callba
1938c 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73 20 61  cks when pages a
1938d 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20 74 68  re changed or th
1938e 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69 64 61  e cache invalida
1938f 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
19390 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b 75 70  oid attachBackup
19391 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33 5f 62  Object(sqlite3_b
19392 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c  ackup *p){.  sql
19393 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70  ite3_backup **pp
19394 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
19395 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
19396 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b 0a 20  ex(p->pSrc) );. 
19397 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67   pp = sqlite3Pag
19398 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69  erBackupPtr(sqli
19399 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
1939a 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e 70 4e  >pSrc));.  p->pN
1939b 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70  ext = *pp;.  *pp
1939c 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41 74 74   = p;.  p->isAtt
1939d 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  ached = 1;.}../*
1939e 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65 20 70  .** Copy nPage p
1939f 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 6f  ages from the so
193a0 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f 20 74  urce b-tree to t
193a1 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a  he destination..
193a2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
193a3 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
193a4 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61  _step(sqlite3_ba
193a5 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61  ckup *p, int nPa
193a6 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ge){.  int rc;..
193a7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
193a8 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d  enter(p->pSrcDb-
193a9 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
193aa 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e  e3BtreeEnter(p->
193ab 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e  pSrc);.  if( p->
193ac 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
193ad 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
193ae 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  er(p->pDestDb->m
193af 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63  utex);.  }..  rc
193b0 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20   = p->rc;.  if( 
193b1 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63  !isFatalError(rc
193b2 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a  ) ){.    Pager *
193b3 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72   const pSrcPager
193b4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
193b5 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20  ager(p->pSrc);  
193b6 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67     /* Source pag
193b7 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20  er */.    Pager 
193b8 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67  * const pDestPag
193b9 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
193ba 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29  ePager(p->pDest)
193bb 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65  ;   /* Dest page
193bc 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b  r */.    int ii;
193bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
193bf 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
193c0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63   */.    int nSrc
193c1 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20  Page = -1;      
193c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
193c3 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20  ze of source db 
193c4 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20  in pages */.    
193c5 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20  int bCloseTrans 
193c6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
193c7 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72     /* True if sr
193c8 63 20 64 62 20 72 65 71 75 69 72 65 73 20 75 6e  c db requires un
193c9 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20  locking */..    
193ca 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  /* If the source
193cb 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e   pager is curren
193cc 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74  tly in a write-t
193cd 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75  ransaction, retu
193ce 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  rn.    ** SQLITE
193cf 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c  _BUSY immediatel
193d0 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
193d1 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20  ( p->pDestDb && 
193d2 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e  p->pSrc->pBt->in
193d3 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
193d4 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
193d5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
193d6 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  SY;.    }else{. 
193d7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
193d8 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  _OK;.    }..    
193d9 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74  /* Lock the dest
193da 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
193db 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c  , if it is not l
193dc 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a  ocked already. *
193dd 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  /.    if( SQLITE
193de 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44  _OK==rc && p->bD
193df 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20  estLocked==0.   
193e0 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
193e1 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
193e2 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e  eeBeginTrans(p->
193e3 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20  pDest, 2)) .    
193e4 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73  ){.      p->bDes
193e5 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20  tLocked = 1;.   
193e6 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
193e7 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c  etMeta(p->pDest,
193e8 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45   BTREE_SCHEMA_VE
193e9 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74  RSION, &p->iDest
193ea 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a  Schema);.    }..
193eb 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
193ec 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d  is no open read-
193ed 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
193ee 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
193ef 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20  se, open.    ** 
193f0 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
193f1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
193f2 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69  ned here, then i
193f3 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64  t will be closed
193f4 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74  .    ** before t
193f5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69  his function exi
193f6 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
193f7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
193f8 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74   && 0==sqlite3Bt
193f9 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
193fa 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20  (p->pSrc) ){.   
193fb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
193fc 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70  treeBeginTrans(p
193fd 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20  ->pSrc, 0);.    
193fe 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20    bCloseTrans = 
193ff 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  1;.    }.  .    
19400 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65 72  /* Now that ther
19401 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  e is a read-lock
19402 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   on the source d
19403 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 20 74  atabase, query t
19404 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65  he.    ** source
19405 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 20 6e   pager for the n
19406 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
19407 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
19408 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
19409 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1940a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1940b 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
1940c 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e 53 72  (pSrcPager, &nSr
1940d 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  cPage);.    }.  
1940e 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61    for(ii=0; (nPa
1940f 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65  ge<0 || ii<nPage
19410 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28  ) && p->iNext<=(
19411 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 26 26  Pgno)nSrcPage &&
19412 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20   !rc; ii++){.   
19413 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53     const Pgno iS
19414 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b  rcPg = p->iNext;
19415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19416 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20   /* Source page 
19417 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20  number */.      
19418 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44  if( iSrcPg!=PEND
19419 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d  ING_BYTE_PAGE(p-
1941a 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20  >pSrc->pBt) ){. 
1941b 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70         DbPage *p
1941c 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 20 20  SrcPg;          
1941d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1941e 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67     /* Source pag
1941f 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  e object */.    
19420 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19421 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67  PagerGet(pSrcPag
19422 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72  er, iSrcPg, &pSr
19423 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66  cPg);.        if
19424 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19425 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
19426 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28  = backupOnePage(
19427 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c 69 74  p, iSrcPg, sqlit
19428 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
19429 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 20 20  SrcPg));.       
1942a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
1942b 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20  nref(pSrcPg);.  
1942c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1942d 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b        p->iNext++
1942e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1942f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
19430 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 65 63  .      p->nPagec
19431 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 65 3b  ount = nSrcPage;
19432 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69  .      p->nRemai
19433 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 65 2b  ning = nSrcPage+
19434 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20  1-p->iNext;.    
19435 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28    if( p->iNext>(
19436 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 29 7b  Pgno)nSrcPage ){
19437 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
19438 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
19439 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d 3e 69   }else if( !p->i
1943a 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20  sAttached ){.   
1943b 20 20 20 20 20 61 74 74 61 63 68 42 61 63 6b 75       attachBacku
1943c 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20 20 20  pObject(p);.    
1943d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
1943e 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73   /* Update the s
1943f 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 66 69  chema version fi
19440 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73 74 69  eld in the desti
19441 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
19442 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20   This.    ** is 
19443 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
19444 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72  t the schema-ver
19445 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73  sion really does
19446 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20 20 2a   change in.    *
19447 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65  * the case where
19448 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20   the source and 
19449 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
1944a 62 61 73 65 73 20 68 61 76 65 20 74 68 65 0a 20  bases have the. 
1944b 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68 65 6d     ** same schem
1944c 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a  a version..    *
1944d 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
1944e 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20 20 20  LITE_DONE .     
1944f 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  && (rc = sqlite3
19450 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
19451 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e 69 44  p->pDest,1,p->iD
19452 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d 3d 53  estSchema+1))==S
19453 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29 7b 0a  QLITE_OK.    ){.
19454 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
19455 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d 20 73  nSrcPagesize = s
19456 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
19457 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b  geSize(p->pSrc);
19458 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  .      const int
19459 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 3d   nDestPagesize =
1945a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1945b 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73  PageSize(p->pDes
1945c 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 44  t);.      int nD
1945d 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a  estTruncate;.  .
1945e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65        if( p->pDe
1945f 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20 20 20  stDb ){.        
19460 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
19461 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44  rnalSchema(p->pD
19462 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20 20 20  estDb, 0);.     
19463 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74   }..      /* Set
19464 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 74   nDestTruncate t
19465 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62  o the final numb
19466 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
19467 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20  he destination. 
19468 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65       ** database
19469 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61 74 69  . The complicati
1946a 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20  on here is that 
1946b 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
1946c 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73 69  page.      ** si
1946d 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 65 72  ze may be differ
1946e 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 72 63  ent to the sourc
1946f 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a 20 20  e page size. .  
19470 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
19471 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  If the source pa
19472 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c  ge size is small
19473 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74  er than the dest
19474 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 69 7a  ination page siz
19475 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75  e, .      ** rou
19476 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73 20 63  nd up. In this c
19477 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  ase the call to 
19478 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
19479 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20  e() below will. 
1947a 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68 65 20       ** fix the 
1947b 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
1947c 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69 73 20  . However it is 
1947d 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c  important to cal
1947e 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74  l.      ** sqlit
1947f 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
19480 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f 20 74  mage() here so t
19481 68 61 74 20 61 6e 79 20 70 61 67 65 73 20 69 6e  hat any pages in
19482 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 64   the .      ** d
19483 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20  estination file 
19484 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e 64 20  that lie beyond 
19485 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63 61 74  the nDestTruncat
19486 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72 65 0a  e page mark are.
19487 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
19488 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f 6d 6d  led by PagerComm
19489 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62 65 66  itPhaseOne() bef
1948a 6f 72 65 20 74 68 65 79 20 61 72 65 20 64 65 73  ore they are des
1948b 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a 2a 20  troyed.      ** 
1948c 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 75 6e  by the file trun
1948d 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  cation..      */
1948e 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50  .      if( nSrcP
1948f 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67  agesize<nDestPag
19490 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  esize ){.       
19491 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e 44 65   int ratio = nDe
19492 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72 63 50  stPagesize/nSrcP
19493 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20 20 20  agesize;.       
19494 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d   nDestTruncate =
19495 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 69 6f   (nSrcPage+ratio
19496 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20  -1)/ratio;.     
19497 20 20 20 69 66 28 20 6e 44 65 73 74 54 72 75 6e     if( nDestTrun
19498 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49  cate==(int)PENDI
19499 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e  NG_BYTE_PAGE(p->
1949a 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20  pDest->pBt) ){. 
1949b 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72           nDestTr
1949c 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20  uncate--;.      
1949d 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1949e 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72  .        nDestTr
1949f 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50 61 67  uncate = nSrcPag
194a0 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73 69 7a  e * (nSrcPagesiz
194a1 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a 65 29  e/nDestPagesize)
194a2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
194a3 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
194a4 63 61 74 65 49 6d 61 67 65 28 70 44 65 73 74 50  cateImage(pDestP
194a5 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75 6e 63  ager, nDestTrunc
194a6 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  ate);..      if(
194a7 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44   nSrcPagesize<nD
194a8 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20  estPagesize ){. 
194a9 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
194aa 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 69 7a   source page-siz
194ab 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  e is smaller tha
194ac 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  n the destinatio
194ad 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20  n page-size,.   
194ae 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78 74 72       ** two extr
194af 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e 65 65  a things may nee
194b0 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20  d to happen:.   
194b1 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
194b2 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73 74 69  **   * The desti
194b3 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20  nation may need 
194b4 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65 64 2c  to be truncated,
194b5 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a   and.        **.
194b6 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 44          **   * D
194b7 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 74 68  ata stored on th
194b8 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69 61 74  e pages immediat
194b9 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
194ba 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  e .        **   
194bb 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70    pending-byte p
194bc 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75 72 63  age in the sourc
194bd 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 6e  e database may n
194be 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20 20 20  eed to be.      
194bf 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 64 20    **     copied 
194c0 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  into the destina
194c1 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 20  tion database.. 
194c2 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
194c3 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53 69 7a    const i64 iSiz
194c4 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50 61 67  e = (i64)nSrcPag
194c5 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e 53 72  esize * (i64)nSr
194c6 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 73  cPage;.        s
194c7 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f  qlite3_file * co
194c8 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 6c 69  nst pFile = sqli
194c9 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 44 65  te3PagerFile(pDe
194ca 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20  stPager);..     
194cb 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
194cc 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
194cd 72 74 28 20 28 69 36 34 29 6e 44 65 73 74 54 72  rt( (i64)nDestTr
194ce 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44 65 73  uncate*(i64)nDes
194cf 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69 53 69  tPagesize >= iSi
194d0 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20 20 20  ze || (.        
194d1 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63        nDestTrunc
194d2 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49  ate==(int)(PENDI
194d3 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e  NG_BYTE_PAGE(p->
194d4 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20  pDest->pBt)-1). 
194d5 20 20 20 20 20 20 20 20 20 20 26 26 20 69 53 69            && iSi
194d6 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ze>=PENDING_BYTE
194d7 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49   && iSize<=PENDI
194d8 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50 61 67  NG_BYTE+nDestPag
194d9 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 29 29  esize.        ))
194da 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 53 51  ;.        if( SQ
194db 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
194dc 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
194dd 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50  tPhaseOne(pDestP
194de 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20 20 20  ager, 0, 1)).   
194df 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f        && SQLITE_
194e0 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b 75 70  OK==(rc = backup
194e1 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70 46 69  TruncateFile(pFi
194e2 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20 20 20  le, iSize)).    
194e3 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f       && SQLITE_O
194e4 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
194e5 50 61 67 65 72 53 79 6e 63 28 70 44 65 73 74 50  PagerSync(pDestP
194e6 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 29  ager)).        )
194e7 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 20  {.          i64 
194e8 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 20 20  iOff;.          
194e9 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50  i64 iEnd = MIN(P
194ea 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44  ENDING_BYTE + nD
194eb 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69 53 69  estPagesize, iSi
194ec 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66  ze);.          f
194ed 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  or(.            
194ee 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54  iOff=PENDING_BYT
194ef 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 20  E+nSrcPagesize; 
194f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 3d  .            rc=
194f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f  =SQLITE_OK && iO
194f2 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20 20 20  ff<iEnd; .      
194f3 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63        iOff+=nSrc
194f4 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20  Pagesize.       
194f5 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
194f6 20 20 50 67 48 64 72 20 2a 70 53 72 63 50 67 20    PgHdr *pSrcPg 
194f7 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
194f8 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63   const Pgno iSrc
194f9 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66  Pg = (Pgno)((iOf
194fa 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 29 2b  f/nSrcPagesize)+
194fb 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
194fc 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
194fd 72 47 65 74 28 70 53 72 63 50 61 67 65 72 2c 20  rGet(pSrcPager, 
194fe 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 67 29  iSrcPg, &pSrcPg)
194ff 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
19500 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19501 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
19502 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c   u8 *zData = sql
19503 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
19504 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20  (pSrcPg);.      
19505 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
19506 69 74 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c  ite3OsWrite(pFil
19507 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63 50 61  e, zData, nSrcPa
19508 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20  gesize, iOff);. 
19509 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1950a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1950b 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50  PagerUnref(pSrcP
1950c 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  g);.          }.
1950d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1950e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1950f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
19510 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
19511 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 30 29  DestPager, 0, 0)
19512 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20  ;.      }.  .   
19513 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d     /* Finish com
19514 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e  mitting the tran
19515 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64  saction to the d
19516 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
19517 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  ase. */.      if
19518 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a  ( SQLITE_OK==rc.
19519 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45         && SQLITE
1951a 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
1951b 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1951c 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74 29 29  seTwo(p->pDest))
1951d 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
1951e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f    rc = SQLITE_DO
1951f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NE;.      }.    
19520 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 62  }.  .    /* If b
19521 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20 74 72  CloseTrans is tr
19522 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  ue, then this fu
19523 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20  nction opened a 
19524 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
19525 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73  .    ** on the s
19526 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20  ource database. 
19527 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64 20 74  Close the read t
19528 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72 65 2e  ransaction here.
19529 20 54 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a   There is.    **
1952a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63   no need to chec
1952b 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  k the return val
1952c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72 65 65  ues of the btree
1952d 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c 20 61   methods here, a
1952e 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74  s.    ** "commit
1952f 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c  ting" a read-onl
19530 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61  y transaction ca
19531 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a  nnot fail..    *
19532 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f 73 65  /.    if( bClose
19533 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 54  Trans ){.      T
19534 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32  ESTONLY( int rc2
19535 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e   );.      TESTON
19536 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73 71 6c  LY( rc2  = ) sql
19537 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
19538 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c  haseOne(p->pSrc,
19539 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53 54 4f   0);.      TESTO
1953a 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20 73 71  NLY( rc2 |= ) sq
1953b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1953c 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53 72 63  PhaseTwo(p->pSrc
1953d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1953e 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   rc2==SQLITE_OK 
1953f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
19540 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a  p->rc = rc;.  }.
19541 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62    if( p->pDestDb
19542 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
19543 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70  mutex_leave(p->p
19544 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  DestDb->mutex);.
19545 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
19546 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29  eeLeave(p->pSrc)
19547 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
19548 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72 63 44  x_leave(p->pSrcD
19549 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
1954a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1954b 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72 65 73   Release all res
1954c 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65  ources associate
1954d 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69 74 65  d with an sqlite
1954e 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65  3_backup* handle
1954f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
19550 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  int sqlite3_back
19551 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65  up_finish(sqlite
19552 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20  3_backup *p){.  
19553 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19554 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pp;            
19555 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f 20 68       /* Ptr to h
19556 65 61 64 20 6f 66 20 70 61 67 65 72 73 20 62 61  ead of pagers ba
19557 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73  ckup list */.  s
19558 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
19559 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
1955a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20      /* Mutex to 
1955b 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65 20 64  protect source d
1955c 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
1955d 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1955e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1955f 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
19560 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e  turn */..  /* En
19561 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 20  ter the mutexes 
19562 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  */.  if( p==0 ) 
19563 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
19564 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
19565 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44  x_enter(p->pSrcD
19566 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
19567 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
19568 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74 65 78  ->pSrc);.  mutex
19569 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75   = p->pSrcDb->mu
1956a 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44  tex;.  if( p->pD
1956b 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  estDb ){.    sql
1956c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1956d 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74  (p->pDestDb->mut
1956e 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  ex);.  }..  /* D
1956f 65 74 61 63 68 20 74 68 69 73 20 62 61 63 6b 75  etach this backu
19570 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63  p from the sourc
19571 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66  e pager. */.  if
19572 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a  ( p->pDestDb ){.
19573 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61      p->pSrc->nBa
19574 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66  ckup--;.  }.  if
19575 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20  ( p->isAttached 
19576 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c 69  ){.    pp = sqli
19577 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74  te3PagerBackupPt
19578 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  r(sqlite3BtreePa
19579 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20  ger(p->pSrc));. 
1957a 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 3d 70     while( *pp!=p
1957b 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d 20 26   ){.      pp = &
1957c 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20  (*pp)->pNext;.  
1957d 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 2d    }.    *pp = p-
1957e 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f  >pNext;.  }..  /
1957f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  * If a transacti
19580 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70 65 6e  on is still open
19581 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c 20 72   on the Btree, r
19582 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a 2f 0a  oll it back. */.
19583 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f    sqlite3BtreeRo
19584 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73 74 29  llback(p->pDest)
19585 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
19586 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 74 68  error code of th
19587 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
19588 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
19589 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72 63 3d  /.  rc = (p->rc=
1958a 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20  =SQLITE_DONE) ? 
1958b 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d 3e 72  SQLITE_OK : p->r
1958c 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  c;.  sqlite3Erro
1958d 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 72 63  r(p->pDestDb, rc
1958e 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78 69 74  , 0);..  /* Exit
1958f 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 6e 64   the mutexes and
19590 20 66 72 65 65 20 74 68 65 20 62 61 63 6b 75 70   free the backup
19591 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75   context structu
19592 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  re. */.  if( p->
19593 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
19594 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
19595 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  ve(p->pDestDb->m
19596 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  utex);.  }.  sql
19597 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
19598 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70  ->pSrc);.  if( p
19599 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
1959a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
1959b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1959c 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
1959d 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
1959e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1959f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
195a0 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65  ages still to be
195a1 20 62 61 63 6b 65 64 20 75 70 20 61 73 20 6f 66   backed up as of
195a2 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
195a3 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  .** call to sqli
195a4 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
195a5 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
195a6 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
195a7 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71  kup_remaining(sq
195a8 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29  lite3_backup *p)
195a9 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52  {.  return p->nR
195aa 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f 2a 0a  emaining;.}../*.
195ab 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  ** Return the to
195ac 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
195ad 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63  ges in the sourc
195ae 65 20 64 61 74 61 62 61 73 65 20 61 73 20 6f 66  e database as of
195af 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20 72 65   the most .** re
195b0 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  cent call to sql
195b1 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
195b2 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
195b3 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
195b4 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73  ckup_pagecount(s
195b5 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
195b6 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  ){.  return p->n
195b7 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a 2f 2a  Pagecount;.}../*
195b8 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
195b9 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  n is called afte
195ba 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  r the contents o
195bb 66 20 70 61 67 65 20 69 50 61 67 65 20 6f 66 20  f page iPage of 
195bc 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20 64 61  the.** source da
195bd 74 61 62 61 73 65 20 68 61 76 65 20 62 65 65 6e  tabase have been
195be 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20 70 61   modified. If pa
195bf 67 65 20 69 50 61 67 65 20 68 61 73 20 61 6c 72  ge iPage has alr
195c0 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20 63 6f  eady been .** co
195c1 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65  pied into the de
195c2 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
195c3 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74  se, then the dat
195c4 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  a written to the
195c5 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  .** destination 
195c6 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64 61 74  is now invalidat
195c7 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74  ed. The destinat
195c8 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50 61 67  ion copy of iPag
195c9 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65  e needs.** to be
195ca 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
195cb 65 20 6e 65 77 20 64 61 74 61 20 62 65 66 6f 72  e new data befor
195cc 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  e the backup ope
195cd 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d  ration is.** com
195ce 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  plete..**.** It 
195cf 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
195d0 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69  the mutex associ
195d1 61 74 65 64 20 77 69 74 68 20 74 68 65 20 42 74  ated with the Bt
195d2 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a  Shared object.**
195d3 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
195d4 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  o the source dat
195d5 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 77 68  abase is held wh
195d6 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
195d7 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a   is.** called..*
195d8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
195d9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63   void sqlite3Bac
195da 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 74 65  kupUpdate(sqlite
195db 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75  3_backup *pBacku
195dc 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c 20 63  p, Pgno iPage, c
195dd 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b  onst u8 *aData){
195de 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
195df 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  p *p;           
195e0 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
195e1 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
195e2 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b    for(p=pBackup;
195e3 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
195e4 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
195e5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
195e6 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75  p->pSrc->pBt->mu
195e7 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20  tex) );.    if( 
195e8 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d  !isFatalError(p-
195e9 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c 70 2d  >rc) && iPage<p-
195ea 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >iNext ){.      
195eb 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20 70 72  /* The backup pr
195ec 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c 72 65  ocess p has alre
195ed 61 64 79 20 63 6f 70 69 65 64 20 70 61 67 65 20  ady copied page 
195ee 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77 20 69  iPage. But now i
195ef 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62  t.      ** has b
195f0 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20  een modified by 
195f1 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  a transaction on
195f2 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
195f3 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a  r. Copy.      **
195f4 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 6e   the new data in
195f5 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e 0a 20  to the backup.. 
195f6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e       */.      in
195f7 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65  t rc = backupOne
195f8 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c 20 61  Page(p, iPage, a
195f9 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61 73 73  Data);.      ass
195fa 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
195fb 42 55 53 59 20 26 26 20 72 63 21 3d 53 51 4c 49  BUSY && rc!=SQLI
195fc 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20  TE_LOCKED );.   
195fd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
195fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
195ff 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  p->rc = rc;.    
19600 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
19601 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74 20 74  ./*.** Restart t
19602 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73  he backup proces
19603 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  s. This is calle
19604 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72  d when the pager
19605 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65 63 74   layer.** detect
19606 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  s that the datab
19607 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  ase has been mod
19608 69 66 69 65 64 20 62 79 20 61 6e 20 65 78 74 65  ified by an exte
19609 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a  rnal database.**
1960a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 20   connection. In 
1960b 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20  this case there 
1960c 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f  is no way of kno
1960d 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20 74 68  wing which of th
1960e 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20  e.** pages that 
1960f 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 65 64  have been copied
19610 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e   into the destin
19611 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61  ation database a
19612 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76 61 6c  re still .** val
19613 69 64 20 61 6e 64 20 77 68 69 63 68 20 61 72 65  id and which are
19614 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65 6e 74   not, so the ent
19615 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65 65 64  ire process need
19616 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74 65  s to be restarte
19617 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61  d..**.** It is a
19618 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
19619 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64  mutex associated
1961a 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72   with the BtShar
1961b 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72  ed object.** cor
1961c 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
1961d 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1961e 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74  e is held when t
1961f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  his function is.
19620 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51  ** called..*/.SQ
19621 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19622 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52  d sqlite3BackupR
19623 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 5f 62  estart(sqlite3_b
19624 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 29 7b  ackup *pBackup){
19625 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
19626 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  p *p;           
19627 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
19628 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
19629 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b    for(p=pBackup;
1962a 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
1962b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1962c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1962d 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75  p->pSrc->pBt->mu
1962e 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d 3e 69  tex) );.    p->i
1962f 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a  Next = 1;.  }.}.
19630 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
19631 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a  OMIT_VACUUM./*.*
19632 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c  * Copy the compl
19633 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70  ete content of p
19634 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54  BtFrom into pBtT
19635 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f  o.  A transactio
19636 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 63 74  n.** must be act
19637 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66 69 6c  ive for both fil
19638 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69  es..**.** The si
19639 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d  ze of file pTo m
1963a 61 79 20 62 65 20 72 65 64 75 63 65 64 20 62 79  ay be reduced by
1963b 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e   this operation.
1963c 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a 2a 2a   If anything .**
1963d 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 68 65   goes wrong, the
1963e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
1963f 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  pTo is rolled ba
19640 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  ck. If successfu
19641 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73  l, the .** trans
19642 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
19643 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 72  ted before retur
19644 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ning..*/.SQLITE_
19645 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19646 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
19647 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72  (Btree *pTo, Btr
19648 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e  ee *pFrom){.  in
19649 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
1964a 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71 6c 69  backup b;.  sqli
1964b 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 54  te3BtreeEnter(pT
1964c 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  o);.  sqlite3Btr
1964d 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a  eeEnter(pFrom);.
1964e 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61 6e 20  .  /* Set up an 
1964f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f  sqlite3_backup o
19650 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33 5f 62  bject. sqlite3_b
19651 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20 6d 75  ackup.pDestDb mu
19652 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74  st be set.  ** t
19653 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75 73 65  o 0. This is use
19654 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65  d by the impleme
19655 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69  ntations of sqli
19656 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
19657 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c 69 74  ).  ** and sqlit
19658 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
19659 28 29 20 74 6f 20 64 65 74 65 63 74 20 74 68 61  () to detect tha
1965a 74 20 74 68 65 79 20 61 72 65 20 62 65 69 6e 67  t they are being
1965b 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66 72 6f   called.  ** fro
1965c 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  m this function,
1965d 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20 62 79   not directly by
1965e 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a 2f 0a   the user..  */.
1965f 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30 2c 20    memset(&b, 0, 
19660 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20 62 2e  sizeof(b));.  b.
19661 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d 2d 3e  pSrcDb = pFrom->
19662 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d 20 70  db;.  b.pSrc = p
19663 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73 74 20  From;.  b.pDest 
19664 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65 78 74  = pTo;.  b.iNext
19665 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78 37 46   = 1;..  /* 0x7F
19666 46 46 46 46 46 46 20 69 73 20 74 68 65 20 68 61  FFFFFF is the ha
19667 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65  rd limit for the
19668 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
19669 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 0a 20   in a database. 
1966a 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70 61 73   ** file. By pas
1966b 73 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65  sing this as the
1966c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1966d 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20 2a 2a   to copy to.  **
1966e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
1966f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e 20 67  step(), we can g
19670 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
19671 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65 73 20  e copy finishes 
19672 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61 20 73  .  ** within a s
19673 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e 6c 65  ingle call (unle
19674 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ss an error occu
19675 72 73 29 2e 20 54 68 65 20 61 73 73 65 72 74 28  rs). The assert(
19676 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a  ) statement.  **
19677 20 63 68 65 63 6b 73 20 74 68 69 73 20 61 73 73   checks this ass
19678 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e 72 63  umption - (p->rc
19679 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20  ) should be set 
1967a 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49 54 45  to either SQLITE
1967b 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72 20 61  _DONE .  ** or a
1967c 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20  n error code..  
1967d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  */.  sqlite3_bac
1967e 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30 78 37  kup_step(&b, 0x7
1967f 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73 73 65  FFFFFFF);.  asse
19680 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49 54 45  rt( b.rc!=SQLITE
19681 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  _OK );.  rc = sq
19682 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
19683 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28 20 72  ish(&b);.  if( r
19684 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
19685 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e 70 61      pTo->pBt->pa
19686 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 30 3b  geSizeFixed = 0;
19687 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42  .  }..  sqlite3B
19688 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f 6d 29  treeLeave(pFrom)
19689 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1968a 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20 72 65  Leave(pTo);.  re
1968b 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
1968c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1968d 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 2a 2a  _VACUUM */../***
1968e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1968f 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a  of backup.c ****
19690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19692 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
19693 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
19694 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63  n file vdbemem.c
19695 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
19696 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19697 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
19698 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a  * 2004 May 26.**
19699 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1969a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1969b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1969c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1969d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1969e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1969f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
196a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
196a1 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
196a2 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
196a3 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
196a4 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
196a5 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
196a6 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
196a7 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
196a8 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
196a9 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
196aa 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
196ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
196af 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
196b0 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74  tains code use t
196b1 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65  o manipulate "Me
196b2 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20 20 41  m" structure.  A
196b3 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73   "Mem".** stores
196b4 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20   a single value 
196b5 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65  in the VDBE.  Me
196b6 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73  m is an opaque s
196b7 74 72 75 63 74 75 72 65 20 76 69 73 69 62 6c 65  tructure visible
196b8 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20  .** only within 
196b9 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72  the VDBE.  Inter
196ba 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65  face routines re
196bb 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69  fer to a Mem usi
196bc 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73  ng the.** name s
196bd 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a  qlite_value.*/..
196be 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
196bf 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
196c0 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70  lob() on the sup
196c1 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70  plied value (typ
196c2 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20  e Mem*).** P if 
196c3 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65  required..*/.#de
196c4 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28  fine expandBlob(
196c5 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26  P) (((P)->flags&
196c6 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65  MEM_Zero)?sqlite
196c7 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
196c8 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20  ob(P):0)../*.** 
196c9 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62  If pMem is an ob
196ca 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69  ject with a vali
196cb 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  d string represe
196cc 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f  ntation, this ro
196cd 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73  utine.** ensures
196ce 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e   the internal en
196cf 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73  coding for the s
196d0 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
196d1 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69  tion is.** 'desi
196d2 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20  redEnc', one of 
196d3 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
196d4 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
196d5 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a  QLITE_UTF16BE..*
196d6 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  *.** If pMem is 
196d7 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a  not a string obj
196d8 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f  ect, or the enco
196d9 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69  ding of the stri
196da 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61  ng.** representa
196db 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20  tion is already 
196dc 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65  stored using the
196dd 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64   requested encod
196de 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a  ing, then this.*
196df 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  * routine is a n
196e0 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  o-op..**.** SQLI
196e1 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
196e2 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73  d if the convers
196e3 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75  ion is successfu
196e4 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72  l (or not requir
196e5 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  ed)..** SQLITE_N
196e6 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75  OMEM may be retu
196e7 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63  rned if a malloc
196e8 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20  () fails during 
196e9 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65  conversion.** be
196ea 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a  tween formats..*
196eb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
196ec 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
196ed 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d  ChangeEncoding(M
196ee 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65  em *pMem, int de
196ef 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74  siredEnc){.  int
196f0 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 28   rc;.  assert( (
196f1 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
196f2 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
196f3 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 45  assert( desiredE
196f4 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
196f5 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  || desiredEnc==S
196f6 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 20  QLITE_UTF16LE.  
196f7 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73 69           || desi
196f8 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
196f9 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20  TF16BE );.  if( 
196fa 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  !(pMem->flags&ME
196fb 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e  M_Str) || pMem->
196fc 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 20  enc==desiredEnc 
196fd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
196fe 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61  LITE_OK;.  }.  a
196ff 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
19700 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
19701 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
19702 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66  b->mutex) );.#if
19703 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19704 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20 53  UTF16.  return S
19705 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c  QLITE_ERROR;.#el
19706 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e  se..  /* MemTran
19707 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74 75  slate() may retu
19708 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20  rn SQLITE_OK or 
19709 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66  SQLITE_NOMEM. If
1970a 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e   NOMEM is return
1970b 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68  ed,.  ** then th
1970c 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  e encoding of th
1970d 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20  e value may not 
1970e 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 20  have changed..  
1970f 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
19710 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
19711 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 69  e(pMem, (u8)desi
19712 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72  redEnc);.  asser
19713 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  t(rc==SQLITE_OK 
19714 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
19715 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72  _NOMEM);.  asser
19716 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  t(rc==SQLITE_OK 
19717 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21     || pMem->enc!
19718 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20  =desiredEnc);.  
19719 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
1971a 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d  E_NOMEM || pMem-
1971b 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63  >enc==desiredEnc
1971c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1971d 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
1971e 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e  Make sure pMem->
1971f 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 72  z points to a wr
19720 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f  itable allocatio
19721 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a 2a  n of at least .*
19722 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  * n bytes..**.**
19723 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   If the memory c
19724 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ell currently co
19725 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f 72  ntains string or
19726 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e   blob data.** an
19727 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75  d the third argu
19728 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74  ment passed to t
19729 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1972a 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75  true, the .** cu
1972b 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66  rrent content of
1972c 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72 65   the cell is pre
1972d 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69 73  served. Otherwis
1972e 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20  e, it may.** be 
1972f 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a  discarded.  .**.
19730 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
19731 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 79   sets the MEM_Dy
19732 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 72  n flag and clear
19733 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62  s any xDel callb
19734 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20  ack..** It also 
19735 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d  clears MEM_Ephem
19736 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e   and MEM_Static.
19737 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76 65   If the preserve
19738 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74   flag is .** not
19739 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a   set, Mem.n is z
1973a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eroed..*/.SQLITE
1973b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1973c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1973d 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e  Mem *pMem, int n
1973e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 7b  , int preserve){
1973f 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d 0a  .  assert( 1 >=.
19740 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c      ((pMem->zMal
19741 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61  loc && pMem->zMa
19742 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f  lloc==pMem->z) ?
19743 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 28   1 : 0) +.    ((
19744 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
19745 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65  _Dyn)&&pMem->xDe
19746 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20  l) ? 1 : 0) + . 
19747 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73     ((pMem->flags
19748 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20  &MEM_Ephem) ? 1 
19749 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d  : 0) + .    ((pM
1974a 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  em->flags&MEM_St
1974b 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a 20  atic) ? 1 : 0). 
1974c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1974d 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52  Mem->flags&MEM_R
1974e 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20  owSet)==0 );..  
1974f 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33  if( n<32 ) n = 3
19750 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  2;.  if( sqlite3
19751 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65  DbMallocSize(pMe
19752 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61  m->db, pMem->zMa
19753 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69  lloc)<n ){.    i
19754 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20 70  f( preserve && p
19755 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d  Mem->z==pMem->zM
19756 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70  alloc ){.      p
19757 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a  Mem->z = pMem->z
19758 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  Malloc = sqlite3
19759 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
1975a 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e  pMem->db, pMem->
1975b 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 65  z, n);.      pre
1975c 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d  serve = 0;.    }
1975d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1975e 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e  te3DbFree(pMem->
1975f 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  db, pMem->zMallo
19760 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  c);.      pMem->
19761 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  zMalloc = sqlite
19762 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65  3DbMallocRaw(pMe
19763 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d  m->db, n);.    }
19764 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d  .  }..  if( pMem
19765 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65 20  ->z && preserve 
19766 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  && pMem->zMalloc
19767 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65   && pMem->z!=pMe
19768 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  m->zMalloc ){.  
19769 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a    memcpy(pMem->z
1976a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c  Malloc, pMem->z,
1976b 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20   pMem->n);.  }. 
1976c 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
1976d 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d  &MEM_Dyn && pMem
1976e 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d  ->xDel ){.    pM
1976f 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a  em->xDel((void *
19770 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d  )(pMem->z));.  }
19771 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d  ..  pMem->z = pM
19772 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69  em->zMalloc;.  i
19773 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b  f( pMem->z==0 ){
19774 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
19775 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d   = MEM_Null;.  }
19776 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  else{.    pMem->
19777 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45  flags &= ~(MEM_E
19778 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
19779 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44  ;.  }.  pMem->xD
1977a 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  el = 0;.  return
1977b 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49   (pMem->z ? SQLI
1977c 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e  TE_OK : SQLITE_N
1977d 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  OMEM);.}../*.** 
1977e 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20 4d  Make the given M
1977f 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79  em object MEM_Dy
19780 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  n.  In other wor
19781 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a  ds, make it so.*
19782 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54 20  * that any TEXT 
19783 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20  or BLOB content 
19784 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  is stored in mem
19785 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
19786 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20  m.** malloc().  
19787 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 20  In this way, we 
19788 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d 65  know that the me
19789 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f 20  mory is safe to 
1978a 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65  be.** overwritte
1978b 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a  n or altered..**
1978c 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1978d 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
1978e 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  or SQLITE_NOMEM 
1978f 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e  if malloc fails.
19790 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19791 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19792 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
19793 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  le(Mem *pMem){. 
19794 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 74   int f;.  assert
19795 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19796 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19797 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19798 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19799 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
1979a 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
1979b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d  .  expandBlob(pM
1979c 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d  em);.  f = pMem-
1979d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 66  >flags;.  if( (f
1979e 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c  &(MEM_Str|MEM_Bl
1979f 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21  ob)) && pMem->z!
197a0 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29  =pMem->zMalloc )
197a1 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
197a2 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
197a3 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20  m, pMem->n + 2, 
197a4 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  1) ){.      retu
197a5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
197a6 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
197a7 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b  >z[pMem->n] = 0;
197a8 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65  .    pMem->z[pMe
197a9 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20  m->n+1] = 0;.   
197aa 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
197ab 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20  MEM_Term;.  }.. 
197ac 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
197ad 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  K;.}../*.** If t
197ae 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61  he given Mem* ha
197af 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  s a zero-filled 
197b0 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e  tail, turn it in
197b1 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a  to an ordinary.*
197b2 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e  * blob stored in
197b3 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
197b4 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f  ocated space..*/
197b5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
197b6 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51  OMIT_INCRBLOB.SQ
197b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
197b8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
197b9 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70  xpandBlob(Mem *p
197ba 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d  Mem){.  if( pMem
197bb 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
197bc 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42  ro ){.    int nB
197bd 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  yte;.    assert(
197be 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d   pMem->flags&MEM
197bf 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 73  _Blob );.    ass
197c0 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
197c1 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  s&MEM_RowSet)==0
197c2 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
197c3 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
197c4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
197c5 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
197c6 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  x) );..    /* Se
197c7 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20 6e  t nByte to the n
197c8 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
197c9 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
197ca 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62 6c   the expanded bl
197cb 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65  ob. */.    nByte
197cc 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65   = pMem->n + pMe
197cd 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  m->u.nZero;.    
197ce 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a  if( nByte<=0 ){.
197cf 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31 3b        nByte = 1;
197d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
197d1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
197d2 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31  w(pMem, nByte, 1
197d3 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
197d4 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
197d5 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65      }..    memse
197d6 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d  t(&pMem->z[pMem-
197d7 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e  >n], 0, pMem->u.
197d8 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d  nZero);.    pMem
197d9 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e  ->n += pMem->u.n
197da 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Zero;.    pMem->
197db 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a  flags &= ~(MEM_Z
197dc 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20  ero|MEM_Term);. 
197dd 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
197de 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
197df 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
197e0 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20  e the given Mem 
197e1 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e  is \u0000 termin
197e2 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
197e3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
197e4 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72  te3VdbeMemNulTer
197e5 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d  minate(Mem *pMem
197e6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ){.  assert( pMe
197e7 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
197e8 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
197e9 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
197ea 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e  );.  if( (pMem->
197eb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d  flags & MEM_Term
197ec 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66  )!=0 || (pMem->f
197ed 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d  lags & MEM_Str)=
197ee 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
197ef 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a   SQLITE_OK;   /*
197f0 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a   Nothing to do *
197f1 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  /.  }.  if( sqli
197f2 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
197f3 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20  Mem, pMem->n+2, 
197f4 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  1) ){.    return
197f5 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
197f6 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65   }.  pMem->z[pMe
197f7 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65  m->n] = 0;.  pMe
197f8 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20  m->z[pMem->n+1] 
197f9 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  = 0;.  pMem->fla
197fa 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
197fb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
197fc 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
197fd 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 20   MEM_Str to the 
197fe 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e 74  set of represent
197ff 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 67  ations for the g
19800 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65  iven Mem.  Numbe
19801 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72  rs.** are conver
19802 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ted using sqlite
19803 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43  3_snprintf().  C
19804 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42  onverting a BLOB
19805 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20   to a string.** 
19806 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
19807 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 65  * Existing repre
19808 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49  sentations MEM_I
19809 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20  nt and MEM_Real 
1980a 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69  are *not* invali
1980b 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d  dated..**.** A M
1980c 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69  EM_Null value wi
1980d 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 73  ll never be pass
1980e 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
1980f 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ion. This functi
19810 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f  on is.** used fo
19811 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c  r converting val
19812 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 20  ues to text for 
19813 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 65  returning to the
19814 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 0a   user (i.e. via.
19815 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
19816 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f 72  _text()), or for
19817 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 76   ensuring that v
19818 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 64  alues to be used
19819 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79   as btree.** key
1981a 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 49  s are strings. I
1981b 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 73  n the former cas
1981c 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
1981d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 65   is returned the
1981e 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 65  .** user and the
1981f 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e 74   later is an int
19820 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e  ernal programmin
19821 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  g error..*/.SQLI
19822 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19823 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
19824 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d  ingify(Mem *pMem
19825 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e  , int enc){.  in
19826 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
19827 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d 65  ;.  int fg = pMe
19828 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73  m->flags;.  cons
19829 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 32  t int nByte = 32
1982a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
1982b 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
1982c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1982d 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
1982e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 66  );.  assert( !(f
1982f 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20  g&MEM_Zero) );. 
19830 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 4d   assert( !(fg&(M
19831 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
19832 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66  ) );.  assert( f
19833 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  g&(MEM_Int|MEM_R
19834 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74  eal) );.  assert
19835 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
19836 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
19837 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
19838 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
19839 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28  pMem) );...  if(
1983a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1983b 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c  row(pMem, nByte,
1983c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
1983d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1983e 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20    }..  /* For a 
1983f 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c  Real or Integer,
19840 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 72   use sqlite3_mpr
19841 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 63  intf() to produc
19842 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a  e the UTF-8.  **
19843 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
19844 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61  tation of the va
19845 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 68  lue. Then, if th
19846 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f 64  e required encod
19847 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d  ing.  ** is UTF-
19848 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 65  16le or UTF-16be
19849 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 6f   do a translatio
1984a 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49  n..  ** .  ** FI
1984b 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 62  X ME: It would b
1984c 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c 69  e better if sqli
1984d 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 63  te3_snprintf() c
1984e 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a  ould do UTF-16..
1984f 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 20    */.  if( fg & 
19850 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73  MEM_Int ){.    s
19851 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19852 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  nByte, pMem->z, 
19853 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e  "%lld", pMem->u.
19854 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  i);.  }else{.   
19855 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d 45   assert( fg & ME
19856 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 71  M_Real );.    sq
19857 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
19858 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22  Byte, pMem->z, "
19859 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72  %!.15g", pMem->r
1985a 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e  );.  }.  pMem->n
1985b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1985c 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70  30(pMem->z);.  p
1985d 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
1985e 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e  E_UTF8;.  pMem->
1985f 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72  flags |= MEM_Str
19860 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c  |MEM_Term;.  sql
19861 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
19862 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63  coding(pMem, enc
19863 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
19864 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20  }../*.** Memory 
19865 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69  cell pMem contai
19866 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f  ns the context o
19867 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66  f an aggregate f
19868 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  unction..** This
19869 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74   routine calls t
1986a 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68  he finalize meth
1986b 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e 63  od for that func
1986c 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65  tion.  The.** re
1986d 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 72  sult of the aggr
1986e 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 20  egate is stored 
1986f 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a  back into pMem..
19870 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
19871 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 65  ITE_ERROR if the
19872 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72   finalizer repor
19873 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51  ts an error.  SQ
19874 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72  LITE_OK.** other
19875 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wise..*/.SQLITE_
19876 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19877 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69  te3VdbeMemFinali
19878 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75  ze(Mem *pMem, Fu
19879 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20  ncDef *pFunc){. 
1987a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1987b 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59  _OK;.  if( ALWAY
1987c 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63  S(pFunc && pFunc
1987d 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a  ->xFinalize) ){.
1987e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
1987f 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73 73  ext ctx;.    ass
19880 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
19881 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30  s & MEM_Null)!=0
19882 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d   || pFunc==pMem-
19883 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 61  >u.pDef );.    a
19884 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
19885 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
19886 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
19887 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  b->mutex) );.   
19888 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30 2c   memset(&ctx, 0,
19889 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20   sizeof(ctx));. 
1988a 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d     ctx.s.flags =
1988b 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63   MEM_Null;.    c
1988c 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e  tx.s.db = pMem->
1988d 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d  db;.    ctx.pMem
1988e 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78   = pMem;.    ctx
1988f 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a  .pFunc = pFunc;.
19890 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61      pFunc->xFina
19891 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 20  lize(&ctx);.    
19892 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d  assert( 0==(pMem
19893 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  ->flags&MEM_Dyn)
19894 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20   && !pMem->xDel 
19895 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
19896 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70  Free(pMem->db, p
19897 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20  Mem->zMalloc);. 
19898 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20     memcpy(pMem, 
19899 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63  &ctx.s, sizeof(c
1989a 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d  tx.s));.    rc =
1989b 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20   ctx.isError;.  
1989c 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1989d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d  ../*.** If the m
1989e 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61  emory cell conta
1989f 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c  ins a string val
198a0 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20  ue that must be 
198a1 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f  freed by.** invo
198a2 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61 6c  king an external
198a3 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20   callback, free 
198a4 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20  it now. Calling 
198a5 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  this function.**
198a6 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 61   does not free a
198a7 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62  ny Mem.zMalloc b
198a8 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  uffer..*/.SQLITE
198a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
198aa 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
198ab 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20  aseExternal(Mem 
198ac 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
198ad 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
198ae 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
198af 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
198b0 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c   testcase( p->fl
198b1 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b  ags & MEM_Agg );
198b2 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e  .  testcase( p->
198b3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20  flags & MEM_Dyn 
198b4 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
198b5 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
198b6 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63 61  wSet );.  testca
198b7 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  se( p->flags & M
198b8 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69 66  EM_Frame );.  if
198b9 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  ( p->flags&(MEM_
198ba 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  Agg|MEM_Dyn|MEM_
198bb 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65  RowSet|MEM_Frame
198bc 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) ){.    if( p->
198bd 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b  flags&MEM_Agg ){
198be 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
198bf 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c  beMemFinalize(p,
198c0 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20   p->u.pDef);.   
198c1 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66     assert( (p->f
198c2 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d  lags & MEM_Agg)=
198c3 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
198c4 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
198c5 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  e(p);.    }else 
198c6 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  if( p->flags&MEM
198c7 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20  _Dyn && p->xDel 
198c8 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
198c9 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52   (p->flags&MEM_R
198ca 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20  owSet)==0 );.   
198cb 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64     p->xDel((void
198cc 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20   *)p->z);.      
198cd 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20  p->xDel = 0;.   
198ce 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
198cf 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29  ags&MEM_RowSet )
198d0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
198d1 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e  owSetClear(p->u.
198d2 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 65  pRowSet);.    }e
198d3 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  lse if( p->flags
198d4 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20  &MEM_Frame ){.  
198d5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
198d6 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20  emSetNull(p);.  
198d7 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
198d8 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d   Release any mem
198d9 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20  ory held by the 
198da 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c 65  Mem. This may le
198db 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 61  ave the Mem in a
198dc 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e  n.** inconsisten
198dd 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78 61  t state, for exa
198de 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a  mple with (Mem.z
198df 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d  ==0) and.** (Mem
198e0 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45  .type==SQLITE_TE
198e1 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  XT)..*/.SQLITE_P
198e2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
198e3 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
198e4 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c  e(Mem *p){.  sql
198e5 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
198e6 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20  seExternal(p);. 
198e7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
198e8 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63  ->db, p->zMalloc
198e9 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20  );.  p->z = 0;. 
198ea 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b   p->zMalloc = 0;
198eb 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  .  p->xDel = 0;.
198ec 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
198ed 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20 64   a 64-bit IEEE d
198ee 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d  ouble into a 64-
198ef 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
198f0 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f  er..** If the do
198f1 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67  uble is too larg
198f2 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30 30  e, return 0x8000
198f3 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a  000000000000..**
198f4 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73  .** Most systems
198f5 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68   appear to do th
198f6 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73  is simply by ass
198f7 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62  igning.** variab
198f8 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20  les and without 
198f9 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65 20  the extra range 
198fa 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74  tests.  But.** t
198fb 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74 73  here are reports
198fc 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68   that windows th
198fd 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f  rows an expectio
198fe 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61  n.** if the floa
198ff 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
19900 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
19901 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 32  . (See ticket #2
19902 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65  880.).** Because
19903 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c   we do not compl
19904 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64  etely understand
19905 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65   the problem, we
19906 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68   will.** take th
19907 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61  e conservative a
19908 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61  pproach and alwa
19909 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74  ys do range test
1990a 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65  s.** before atte
1990b 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65  mpting the conve
1990c 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  rsion..*/.static
1990d 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74   i64 doubleToInt
1990e 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20  64(double r){.  
1990f 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d  /*.  ** Many com
19910 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e  pilers we encoun
19911 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e  ter do not defin
19912 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20  e constants for 
19913 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d  the.  ** minimum
19914 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d   and maximum 64-
19915 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72  bit integers, or
19916 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68 65   they define the
19917 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74  m.  ** inconsist
19918 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79  ently.  And many
19919 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61   do not understa
1991a 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61  nd the "LL" nota
1991b 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65  tion..  ** So we
1991c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20   define our own 
1991d 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73  static constants
1991e 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68   here using noth
1991f 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20  ing.  ** larger 
19920 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e  than a 32-bit in
19921 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a  teger constant..
19922 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
19923 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d  nst i64 maxInt =
19924 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a   LARGEST_INT64;.
19925 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
19926 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c  64 minInt = SMAL
19927 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69  LEST_INT64;..  i
19928 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e  f( r<(double)min
19929 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Int ){.    retur
1992a 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73  n minInt;.  }els
1992b 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29  e if( r>(double)
1992c 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a  maxInt ){.    /*
1992d 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 65   minInt is corre
1992e 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61  ct here - not ma
1992f 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 20  xInt.  It turns 
19930 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e 69  out that assigni
19931 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 79  ng.    ** a very
19932 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65 20   large positive 
19933 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74  number to an int
19934 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e 20  eger results in 
19935 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20 20  a very large.   
19936 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e 74   ** negative int
19937 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b 65  eger.  This make
19938 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 20  s no sense, but 
19939 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20 68  it is what x86 h
1993a 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 64  ardware.    ** d
1993b 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61  oes so for compa
1993c 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c 6c  tibility we will
1993d 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 20   do the same in 
1993e 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20  software. */.   
1993f 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a   return minInt;.
19940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
19941 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a  urn (i64)r;.  }.
19942 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
19943 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74  some kind of int
19944 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63 68  eger value which
19945 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65 20   is the best we 
19946 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70  can do.** at rep
19947 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76 61  resenting the va
19948 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20 64  lue that *pMem d
19949 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20 69  escribes as an i
1994a 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d  nteger..** If pM
1994b 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  em is an integer
1994c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
1994d 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20 70   is exact.  If p
1994e 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61  Mem is.** a floa
1994f 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20  ting-point then 
19950 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
19951 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67 65  ed is the intege
19952 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d  r part..** If pM
19953 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f  em is a string o
19954 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20  r blob, then we 
19955 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74 20  make an attempt 
19956 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74  to convert.** it
19957 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72 20   into a integer 
19958 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 2e  and return that.
19959 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73    If pMem repres
1995a 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51  ents an.** an SQ
1995b 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65  L-NULL value, re
1995c 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  turn 0..**.** If
1995d 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73   pMem represents
1995e 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c   a string value,
1995f 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69   its encoding mi
19960 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e 0a  ght be changed..
19961 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19962 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62  E i64 sqlite3Vdb
19963 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70  eIntValue(Mem *p
19964 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67  Mem){.  int flag
19965 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  s;.  assert( pMe
19966 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
19967 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
19968 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
19969 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
1996a 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
1996b 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61  T(pMem) );.  fla
1996c 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  gs = pMem->flags
1996d 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
1996e 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72  MEM_Int ){.    r
1996f 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b  eturn pMem->u.i;
19970 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61  .  }else if( fla
19971 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
19972 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75 62  .    return doub
19973 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e  leToInt64(pMem->
19974 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  r);.  }else if( 
19975 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72  flags & (MEM_Str
19976 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20  |MEM_Blob) ){.  
19977 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20    i64 value;.   
19978 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20   pMem->flags |= 
19979 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28  MEM_Str;.    if(
1997a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1997b 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c  geEncoding(pMem,
1997c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20   SQLITE_UTF8).  
1997d 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56       || sqlite3V
1997e 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
1997f 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20  te(pMem) ){.    
19980 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
19981 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  }.    assert( pM
19982 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c  em->z );.    sql
19983 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d  ite3Atoi64(pMem-
19984 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20  >z, &value);.   
19985 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20   return value;. 
19986 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
19987 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 0;.  }.}../*.
19988 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 65  ** Return the be
19989 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  st representatio
1998a 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 77  n of pMem that w
1998b 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 61  e can get into a
1998c 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 20  .** double.  If 
1998d 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 20  pMem is already 
1998e 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69  a double or an i
1998f 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 69  nteger, return i
19990 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66  ts.** value.  If
19991 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67 20   it is a string 
19992 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20  or blob, try to 
19993 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20  convert it to a 
19994 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74  double..** If it
19995 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75   is a NULL, retu
19996 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54  rn 0.0..*/.SQLIT
19997 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c 65  E_PRIVATE double
19998 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
19999 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29  Value(Mem *pMem)
1999a 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  {.  assert( pMem
1999b 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
1999c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
1999d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
1999e 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  ;.  assert( EIGH
1999f 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
199a0 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28 20  (pMem) );.  if( 
199a1 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
199a2 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65  M_Real ){.    re
199a3 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20  turn pMem->r;.  
199a4 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e  }else if( pMem->
199a5 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
199a6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 64  ){.    return (d
199a7 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b  ouble)pMem->u.i;
199a8 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65  .  }else if( pMe
199a9 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  m->flags & (MEM_
199aa 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
199ab 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29  .    /* (double)
199ac 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  0 In case of SQL
199ad 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
199ae 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
199af 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28    double val = (
199b0 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d  double)0;.    pM
199b1 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
199b2 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71  _Str;.    if( sq
199b3 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
199b4 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51  ncoding(pMem, SQ
199b5 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20  LITE_UTF8).     
199b6 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65    || sqlite3Vdbe
199b7 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
199b8 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f  pMem) ){.      /
199b9 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
199ba 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
199bb 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
199bc 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65  T... */.      re
199bd 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a  turn (double)0;.
199be 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
199bf 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20  ( pMem->z );.   
199c0 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65   sqlite3AtoF(pMe
199c1 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20  m->z, &val);.   
199c2 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
199c3 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f  else{.    /* (do
199c4 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f  uble)0 In case o
199c5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
199c6 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20  OATING_POINT... 
199c7 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28 64  */.    return (d
199c8 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a  ouble)0;.  }.}..
199c9 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74  /*.** The MEM st
199ca 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 61  ructure is alrea
199cb 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20  dy a MEM_Real.  
199cc 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65  Try to also make
199cd 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74   it a.** MEM_Int
199ce 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53   if we can..*/.S
199cf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
199d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  id sqlite3VdbeIn
199d1 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65  tegerAffinity(Me
199d2 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65  m *pMem){.  asse
199d3 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  rt( pMem->flags 
199d4 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20  & MEM_Real );.  
199d5 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
199d6 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
199d7 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  t)==0 );.  asser
199d8 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
199d9 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
199da 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
199db 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
199dc 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
199dd 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b  IGNMENT(pMem) );
199de 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  ..  pMem->u.i = 
199df 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d  doubleToInt64(pM
199e0 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e  em->r);..  /* On
199e1 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c 75  ly mark the valu
199e2 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20  e as an integer 
199e3 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  if.  **.  **    
199e4 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74 72  (1) the round-tr
199e5 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65  ip conversion re
199e6 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73  al->int->real is
199e7 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20   a no-op, and.  
199e8 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69 6e  **    (2) The in
199e9 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65 72  teger is neither
199ea 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f 72   the largest nor
199eb 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20   the smallest.  
199ec 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69 62  **        possib
199ed 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63 6b  le integer (tick
199ee 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20  et #3922).  **. 
199ef 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
199f0 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20 69  nd third terms i
199f1 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
199f2 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f  conditional enfo
199f3 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73 65  rces.  ** the se
199f4 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75  cond condition u
199f5 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70 74  nder the assumpt
199f6 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69 6f  ion that additio
199f7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73 65  n overflow cause
199f8 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f  s.  ** values to
199f9 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f   wrap around.  O
199fa 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c 20  n x86 hardware, 
199fb 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 69  the third term i
199fc 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72  s always.  ** tr
199fd 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65 20  ue and could be 
199fe 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77 65  omitted.  But we
199ff 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65 63   leave it in bec
19a00 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20  ause other.  ** 
19a01 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d 69  architectures mi
19a02 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66 65  ght behave diffe
19a03 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69  rently..  */.  i
19a04 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75  f( pMem->r==(dou
19a05 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26  ble)pMem->u.i &&
19a06 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c   pMem->u.i>SMALL
19a07 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20 20  EST_INT64.      
19a08 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e  && ALWAYS(pMem->
19a09 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36  u.i<LARGEST_INT6
19a0a 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  4) ){.    pMem->
19a0b 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74  flags |= MEM_Int
19a0c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
19a0d 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 74  onvert pMem to t
19a0e 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 6e  ype integer.  In
19a0f 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
19a10 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
19a11 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
19a12 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19a13 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
19a14 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  fy(Mem *pMem){. 
19a15 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19a16 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19a17 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19a18 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19a19 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19a1a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19a1b 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  et)==0 );.  asse
19a1c 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
19a1d 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
19a1e 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  ;..  pMem->u.i =
19a1f 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
19a20 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65  alue(pMem);.  Me
19a21 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65  mSetTypeFlag(pMe
19a22 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72  m, MEM_Int);.  r
19a23 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
19a24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
19a25 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
19a26 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 4d  t is of type MEM
19a27 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69  _Real..** Invali
19a28 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
19a29 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
19a2a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19a2b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19a2c 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20  eMemRealify(Mem 
19a2d 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
19a2e 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19a2f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19a30 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19a31 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19a32 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
19a33 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
19a34 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c  .  pMem->r = sql
19a35 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
19a36 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65  e(pMem);.  MemSe
19a37 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
19a38 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74  MEM_Real);.  ret
19a39 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
19a3a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
19a3b 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20  pMem so that it 
19a3c 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65  has types MEM_Re
19a3d 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72  al or MEM_Int or
19a3e 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69   both..** Invali
19a3f 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
19a40 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
19a41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19a42 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19a43 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d  eMemNumerify(Mem
19a44 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c   *pMem){.  doubl
19a45 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20  e r1, r2;.  i64 
19a46 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  i;.  assert( (pM
19a47 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
19a48 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45  _Int|MEM_Real|ME
19a49 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20  M_Null))==0 );. 
19a4a 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19a4b 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f  flags & (MEM_Blo
19a4c 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29  b|MEM_Str))!=0 )
19a4d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
19a4e 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
19a4f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
19a50 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
19a51 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33  ;.  r1 = sqlite3
19a52 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d  VdbeRealValue(pM
19a53 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c  em);.  i = doubl
19a54 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20  eToInt64(r1);.  
19a55 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a  r2 = (double)i;.
19a56 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a    if( r1==r2 ){.
19a57 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19a58 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65  emIntegerify(pMe
19a59 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
19a5a 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20   pMem->r = r1;. 
19a5b 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
19a5c 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c  g(pMem, MEM_Real
19a5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
19a5e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
19a5f 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
19a60 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
19a61 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
19a62 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20  stored in *pMem 
19a63 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
19a64 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a65 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a66 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29  tNull(Mem *pMem)
19a67 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  {.  if( pMem->fl
19a68 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20  ags & MEM_Frame 
19a69 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
19a6a 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70 4d  beFrameDelete(pM
19a6b 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20  em->u.pFrame);. 
19a6c 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66   }.  if( pMem->f
19a6d 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19a6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
19a6f 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d  RowSetClear(pMem
19a70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20  ->u.pRowSet);.  
19a71 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  }.  MemSetTypeFl
19a72 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c  ag(pMem, MEM_Nul
19a73 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  l);.  pMem->type
19a74 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a   = SQLITE_NULL;.
19a75 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
19a76 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c  any previous val
19a77 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  ue and set the v
19a78 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f  alue to be a BLO
19a79 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e  B of length.** n
19a7a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20   containing all 
19a7b 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  zeros..*/.SQLITE
19a7c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19a7d 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
19a7e 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65  eroBlob(Mem *pMe
19a7f 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c  m, int n){.  sql
19a80 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19a81 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
19a82 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
19a83 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70  ob|MEM_Zero;.  p
19a84 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
19a85 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d  TE_BLOB;.  pMem-
19a86 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c  >n = 0;.  if( n<
19a87 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65  0 ) n = 0;.  pMe
19a88 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a  m->u.nZero = n;.
19a89 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51    pMem->enc = SQ
19a8a 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64  LITE_UTF8;..#ifd
19a8b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
19a8c 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65  NCRBLOB.  sqlite
19a8d 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
19a8e 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20  m, n, 0);.  if( 
19a8f 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70  pMem->z ){.    p
19a90 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20  Mem->n = n;.    
19a91 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20  memset(pMem->z, 
19a92 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  0, n);.  }.#endi
19a93 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  f.}../*.** Delet
19a94 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76  e any previous v
19a95 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65  alue and set the
19a96 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
19a97 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a   *pMem to val,.*
19a98 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20  * manifest type 
19a99 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49  INTEGER..*/.SQLI
19a9a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19a9b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19a9c 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d  tInt64(Mem *pMem
19a9d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73 71  , i64 val){.  sq
19a9e 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19a9f 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
19aa0 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20  m->u.i = val;.  
19aa1 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
19aa2 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74  M_Int;.  pMem->t
19aa3 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
19aa4 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  EGER;.}../*.** D
19aa5 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f  elete any previo
19aa6 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74  us value and set
19aa7 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
19aa8 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61  d in *pMem to va
19aa9 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74  l,.** manifest t
19aaa 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c  ype REAL..*/.SQL
19aab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19aac 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19aad 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d  etDouble(Mem *pM
19aae 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b  em, double val){
19aaf 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
19ab0 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20 20  NaN(val) ){.    
19ab1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19ab2 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d  tNull(pMem);.  }
19ab3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
19ab4 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
19ab5 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d  pMem);.    pMem-
19ab6 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d  >r = val;.    pM
19ab7 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
19ab8 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Real;.    pMem->
19ab9 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c  type = SQLITE_FL
19aba 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  OAT;.  }.}../*.*
19abb 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65  * Delete any pre
19abc 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20  vious value and 
19abd 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
19abe 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a   pMem to be an.*
19abf 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20  * empty boolean 
19ac0 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45  index..*/.SQLITE
19ac1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19ac2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52  lite3VdbeMemSetR
19ac3 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29  owSet(Mem *pMem)
19ac4 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
19ac5 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73  = pMem->db;.  as
19ac6 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20  sert( db!=0 );. 
19ac7 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19ac8 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19ac9 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  et)==0 );.  sqli
19aca 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19acb 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d  e(pMem);.  pMem-
19acc 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
19acd 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
19ace 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62 2d  , 64);.  if( db-
19acf 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
19ad0 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
19ad1 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d   = MEM_Null;.  }
19ad2 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
19ad3 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  ( pMem->zMalloc 
19ad4 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70  );.    pMem->u.p
19ad5 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65 33  RowSet = sqlite3
19ad6 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20 70  RowSetInit(db, p
19ad7 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20  Mem->zMalloc, . 
19ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ada 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 4d        sqlite3DbM
19adb 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d  allocSize(db, pM
19adc 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20  em->zMalloc));. 
19add 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
19ade 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 3b  >u.pRowSet!=0 );
19adf 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
19ae0 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20   = MEM_RowSet;. 
19ae1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
19ae2 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 4d  rn true if the M
19ae3 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69  em object contai
19ae4 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f  ns a TEXT or BLO
19ae5 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f  B that is.** too
19ae6 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20 73   large - whose s
19ae7 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c 49  ize exceeds SQLI
19ae8 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a  TE_MAX_LENGTH..*
19ae9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19aea 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19aeb 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70  MemTooBig(Mem *p
19aec 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
19aed 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  db!=0 );.  if( p
19aee 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53  ->flags & (MEM_S
19aef 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a  tr|MEM_Blob) ){.
19af0 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e      int n = p->n
19af1 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61  ;.    if( p->fla
19af2 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
19af3 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75  .      n += p->u
19af4 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20  .nZero;.    }.  
19af5 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62    return n>p->db
19af6 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
19af7 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20  LIMIT_LENGTH];. 
19af8 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a   }.  return 0; .
19af9 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66  }../*.** Size of
19afa 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20   struct Mem not 
19afb 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d 65  including the Me
19afc 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72  m.zMalloc member
19afd 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  ..*/.#define MEM
19afe 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74  CELLSIZE (size_t
19aff 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e  )(&(((Mem *)0)->
19b00 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a  zMalloc))../*.**
19b01 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77   Make an shallow
19b02 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 69   copy of pFrom i
19b03 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 20  nto pTo.  Prior 
19b04 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70  contents of.** p
19b05 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20 54  To are freed.  T
19b06 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c  he pFrom->z fiel
19b07 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63 61  d is not duplica
19b08 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f  ted.  If.** pFro
19b09 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74 68  m->z is used, th
19b0a 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73  en pTo->z points
19b0b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69   to the same thi
19b0c 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a  ng as pFrom->z.*
19b0d 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74 73  * and flags gets
19b0e 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65 72   srcType (either
19b0f 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45   MEM_Ephem or ME
19b10 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51  M_Static)..*/.SQ
19b11 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19b12 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19b13 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20  ShallowCopy(Mem 
19b14 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20  *pTo, const Mem 
19b15 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54  *pFrom, int srcT
19b16 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ype){.  assert( 
19b17 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20  (pFrom->flags & 
19b18 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
19b19 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
19b1a 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
19b1b 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79  l(pTo);.  memcpy
19b1c 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d  (pTo, pFrom, MEM
19b1d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f  CELLSIZE);.  pTo
19b1e 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66  ->xDel = 0;.  if
19b1f 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26  ( (pFrom->flags&
19b20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70  MEM_Dyn)!=0 || p
19b21 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e  From->z==pFrom->
19b22 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70  zMalloc ){.    p
19b23 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  To->flags &= ~(M
19b24 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69  EM_Dyn|MEM_Stati
19b25 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20  c|MEM_Ephem);.  
19b26 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79 70    assert( srcTyp
19b27 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20  e==MEM_Ephem || 
19b28 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61  srcType==MEM_Sta
19b29 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e  tic );.    pTo->
19b2a 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65  flags |= srcType
19b2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
19b2c 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20  ake a full copy 
19b2d 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54  of pFrom into pT
19b2e 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e  o.  Prior conten
19b2f 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a  ts of pTo are.**
19b30 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74 68   freed before th
19b31 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a  e copy is made..
19b32 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19b33 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19b34 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54  eMemCopy(Mem *pT
19b35 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46  o, const Mem *pF
19b36 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  rom){.  int rc =
19b37 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
19b38 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66  ssert( (pFrom->f
19b39 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19b3a 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74  t)==0 );.  sqlit
19b3b 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19b3c 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20  External(pTo);. 
19b3d 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72   memcpy(pTo, pFr
19b3e 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29  om, MEMCELLSIZE)
19b3f 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26  ;.  pTo->flags &
19b40 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69  = ~MEM_Dyn;..  i
19b41 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d  f( pTo->flags&(M
19b42 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
19b43 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28   ){.    if( 0==(
19b44 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  pFrom->flags&MEM
19b45 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20  _Static) ){.    
19b46 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20    pTo->flags |= 
19b47 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
19b48 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
19b49 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
19b4a 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  e(pTo);.    }.  
19b4b 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
19b4c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65  }../*.** Transfe
19b4d 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  r the contents o
19b4e 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20  f pFrom to pTo. 
19b4f 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 6c  Any existing val
19b50 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20  ue in pTo is.** 
19b51 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d 20  freed. If pFrom 
19b52 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 72  contains ephemer
19b53 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 20  al data, a copy 
19b54 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70  is made..**.** p
19b55 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e  From contains an
19b56 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74   SQL NULL when t
19b57 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
19b58 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rns..*/.SQLITE_P
19b59 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
19b5a 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d  te3VdbeMemMove(M
19b5b 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46  em *pTo, Mem *pF
19b5c 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rom){.  assert( 
19b5d 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20  pFrom->db==0 || 
19b5e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19b5f 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75  ld(pFrom->db->mu
19b60 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19b61 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20  ( pTo->db==0 || 
19b62 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19b63 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65  ld(pTo->db->mute
19b64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
19b65 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20  pFrom->db==0 || 
19b66 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46  pTo->db==0 || pF
19b67 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62  rom->db==pTo->db
19b68 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64   );..  sqlite3Vd
19b69 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f  beMemRelease(pTo
19b6a 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c  );.  memcpy(pTo,
19b6b 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d   pFrom, sizeof(M
19b6c 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66  em));.  pFrom->f
19b6d 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
19b6e 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d  .  pFrom->xDel =
19b6f 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61   0;.  pFrom->zMa
19b70 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  lloc = 0;.}../*.
19b71 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
19b72 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20  lue of a Mem to 
19b73 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 61  be a string or a
19b74 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   BLOB..**.** The
19b75 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65   memory manageme
19b76 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70 65  nt strategy depe
19b77 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
19b78 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20   of the xDel.** 
19b79 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 68  parameter. If th
19b7a 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 69  e value passed i
19b7b 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  s SQLITE_TRANSIE
19b7c 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a  NT, then the .**
19b7d 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 65   string is copie
19b7e 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 62  d into a (possib
19b7f 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 66  ly existing) buf
19b80 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 74  fer managed by t
19b81 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63  he .** Mem struc
19b82 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  ture. Otherwise,
19b83 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 75   any existing bu
19b84 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61 6e  ffer is freed an
19b85 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72  d the.** pointer
19b86 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   copied..**.** I
19b87 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  f the string is 
19b88 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 74  too large (if it
19b89 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51 4c   exceeds the SQL
19b8a 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
19b8b 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 20  .** size limit) 
19b8c 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61  then no memory a
19b8d 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
19b8e 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e 67  .  If the string
19b8f 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65   can be.** store
19b90 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61  d without alloca
19b91 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65  ting memory, the
19b92 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 6d  n it is.  If a m
19b93 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
19b94 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20  .** is required 
19b95 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 72  to store the str
19b96 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 20  ing, then value 
19b97 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 61  of pMem is uncha
19b98 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74  nged.  In.** eit
19b99 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 45  her case, SQLITE
19b9a 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 72  _TOOBIG is retur
19b9b 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
19b9c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19b9d 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
19b9e 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20  .  Mem *pMem,   
19b9f 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
19ba0 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20   cell to set to 
19ba1 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a  string value */.
19ba2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
19ba3 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
19ba4 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  pointer */.  int
19ba5 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
19ba6 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72   /* Bytes in str
19ba7 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65  ing, or negative
19ba8 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
19ba9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
19baa 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66  oding of z.  0 f
19bab 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f  or BLOBs */.  vo
19bac 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
19bad 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20  ) /* Destructor 
19bae 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
19baf 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20   int nByte = n; 
19bb0 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75       /* New valu
19bb1 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f  e for pMem->n */
19bb2 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20  .  int iLimit;  
19bb3 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
19bb4 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67  m allowed string
19bb5 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f   or blob size */
19bb6 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30  .  u16 flags = 0
19bb7 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61  ;      /* New va
19bb8 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c  lue for pMem->fl
19bb9 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ags */..  assert
19bba 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19bbb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19bbc 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19bbd 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19bbe 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
19bbf 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
19bc0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73  );..  /* If z is
19bc1 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
19bc2 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e   set pMem to con
19bc3 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  tain an SQL NULL
19bc4 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b  . */.  if( !z ){
19bc5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19bc6 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29  MemSetNull(pMem)
19bc7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
19bc8 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69  ITE_OK;.  }..  i
19bc9 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20  f( pMem->db ){. 
19bca 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d     iLimit = pMem
19bcb 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
19bcc 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
19bcd 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
19bce 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f  iLimit = SQLITE_
19bcf 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a  MAX_LENGTH;.  }.
19bd0 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d    flags = (enc==
19bd1 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53  0?MEM_Blob:MEM_S
19bd2 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  tr);.  if( nByte
19bd3 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  <0 ){.    assert
19bd4 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20  ( enc!=0 );.    
19bd5 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  if( enc==SQLITE_
19bd6 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f  UTF8 ){.      fo
19bd7 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65  r(nByte=0; nByte
19bd8 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42  <=iLimit && z[nB
19bd9 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d  yte]; nByte++){}
19bda 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
19bdb 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e    for(nByte=0; n
19bdc 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20  Byte<=iLimit && 
19bdd 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42  (z[nByte] | z[nB
19bde 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d  yte+1]); nByte+=
19bdf 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66  2){}.    }.    f
19be0 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
19be1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
19be2 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
19be3 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c  sets the new val
19be4 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64  ues of Mem.z and
19be5 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20   Mem.xDel. It.  
19be6 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66  ** also sets a f
19be7 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72  lag in local var
19be8 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f  iable "flags" to
19be9 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65   indicate the me
19bea 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65  mory.  ** manage
19beb 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d  ment (one of MEM
19bec 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74  _Dyn or MEM_Stat
19bed 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ic)..  */.  if( 
19bee 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41  xDel==SQLITE_TRA
19bef 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e  NSIENT ){.    in
19bf0 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65  t nAlloc = nByte
19bf1 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26  ;.    if( flags&
19bf2 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20  MEM_Term ){.    
19bf3 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63    nAlloc += (enc
19bf4 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
19bf5 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  2);.    }.    if
19bf6 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29  ( nByte>iLimit )
19bf7 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
19bf8 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
19bf9 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
19bfa 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19bfb 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20  Mem, nAlloc, 0) 
19bfc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19bfd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
19bfe 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70    }.    memcpy(p
19bff 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f  Mem->z, z, nAllo
19c00 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  c);.  }else if( 
19c01 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e  xDel==SQLITE_DYN
19c02 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69  AMIC ){.    sqli
19c03 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19c04 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65  e(pMem);.    pMe
19c05 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65  m->zMalloc = pMe
19c06 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a  m->z = (char *)z
19c07 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c  ;.    pMem->xDel
19c08 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
19c09 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19c0a 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
19c0b 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63      pMem->z = (c
19c0c 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65  har *)z;.    pMe
19c0d 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a  m->xDel = xDel;.
19c0e 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78      flags |= ((x
19c0f 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54  Del==SQLITE_STAT
19c10 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d  IC)?MEM_Static:M
19c11 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20  EM_Dyn);.  }..  
19c12 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b  pMem->n = nByte;
19c13 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d  .  pMem->flags =
19c14 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e   flags;.  pMem->
19c15 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20  enc = (enc==0 ? 
19c16 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e  SQLITE_UTF8 : en
19c17 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  c);.  pMem->type
19c18 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c   = (enc==0 ? SQL
19c19 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54  ITE_BLOB : SQLIT
19c1a 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65  E_TEXT);..#ifnde
19c1b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
19c1c 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  F16.  if( pMem->
19c1d 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc!=SQLITE_UTF8
19c1e 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d   && sqlite3VdbeM
19c1f 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d  emHandleBom(pMem
19c20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
19c21 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
19c22 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
19c23 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a  nByte>iLimit ){.
19c24 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
19c25 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20  E_TOOBIG;.  }.. 
19c26 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
19c27 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  K;.}../*.** Comp
19c28 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63  are the values c
19c29 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20  ontained by the 
19c2a 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  two memory cells
19c2b 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e  , returning.** n
19c2c 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72  egative, zero or
19c2d 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65   positive if pMe
19c2e 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  m1 is less than,
19c2f 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72   equal to, or gr
19c30 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d  eater.** than pM
19c31 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64  em2. Sorting ord
19c32 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72  er is NULL's fir
19c33 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  st, followed by 
19c34 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72  numbers (integer
19c35 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20  s.** and reals) 
19c36 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c  sorted numerical
19c37 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ly, followed by 
19c38 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20  text ordered by 
19c39 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a  the collating.**
19c3a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20   sequence pColl 
19c3b 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62  and finally blob
19c3c 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65  's ordered by me
19c3d 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77  mcmp()..**.** Tw
19c3e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72  o NULL values ar
19c3f 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75  e considered equ
19c40 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  al by this funct
19c41 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
19c42 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19c43 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e  e3MemCompare(con
19c44 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63  st Mem *pMem1, c
19c45 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c  onst Mem *pMem2,
19c46 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a   const CollSeq *
19c47 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63  pColl){.  int rc
19c48 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a  ;.  int f1, f2;.
19c49 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66    int combined_f
19c4a 6c 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d  lags;..  f1 = pM
19c4b 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32  em1->flags;.  f2
19c4c 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b   = pMem2->flags;
19c4d 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  .  combined_flag
19c4e 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73  s = f1|f2;.  ass
19c4f 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66  ert( (combined_f
19c50 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19c51 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20  t)==0 );. .  /* 
19c52 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20  If one value is 
19c53 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73  NULL, it is less
19c54 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e   than the other.
19c55 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a   If both values.
19c56 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72    ** are NULL, r
19c57 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20  eturn 0..  */.  
19c58 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61  if( combined_fla
19c59 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  gs&MEM_Null ){. 
19c5a 20 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45     return (f2&ME
19c5b 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45  M_Null) - (f1&ME
19c5c 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20  M_Null);.  }..  
19c5d 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20  /* If one value 
19c5e 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20  is a number and 
19c5f 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74  the other is not
19c60 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  , the number is 
19c61 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f  less..  ** If bo
19c62 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20  th are numbers, 
19c63 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73  compare as reals
19c64 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61   if one is a rea
19c65 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72  l, or as integer
19c66 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76  s.  ** if both v
19c67 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65  alues are intege
19c68 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  rs..  */.  if( c
19c69 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d  ombined_flags&(M
19c6a 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
19c6b 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31   ){.    if( !(f1
19c6c 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  &(MEM_Int|MEM_Re
19c6d 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  al)) ){.      re
19c6e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19c6f 20 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f    if( !(f2&(MEM_
19c70 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29  Int|MEM_Real)) )
19c71 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  {.      return -
19c72 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
19c73 20 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f   (f1 & f2 & MEM_
19c74 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Int)==0 ){.     
19c75 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a   double r1, r2;.
19c76 20 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45        if( (f1&ME
19c77 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Real)==0 ){.  
19c78 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62        r1 = (doub
19c79 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20  le)pMem1->u.i;. 
19c7a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19c7b 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e      r1 = pMem1->
19c7c 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
19c7d 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61   if( (f2&MEM_Rea
19c7e 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
19c7f 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d   r2 = (double)pM
19c80 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20  em2->u.i;.      
19c81 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
19c82 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20  2 = pMem2->r;.  
19c83 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
19c84 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d  r1<r2 ) return -
19c85 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e  1;.      if( r1>
19c86 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  r2 ) return 1;. 
19c87 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
19c88 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
19c89 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49  assert( f1&MEM_I
19c8a 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  nt );.      asse
19c8b 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29  rt( f2&MEM_Int )
19c8c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  ;.      if( pMem
19c8d 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e  1->u.i < pMem2->
19c8e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  u.i ) return -1;
19c8f 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31  .      if( pMem1
19c90 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75  ->u.i > pMem2->u
19c91 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  .i ) return 1;. 
19c92 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
19c93 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
19c94 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61  f one value is a
19c95 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20   string and the 
19c96 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c  other is a blob,
19c97 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c   the string is l
19c98 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74  ess..  ** If bot
19c99 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63  h are strings, c
19c9a 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65  ompare using the
19c9b 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
19c9c 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
19c9d 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
19c9e 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69  MEM_Str ){.    i
19c9f 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72  f( (f1 & MEM_Str
19ca0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )==0 ){.      re
19ca1 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19ca2 20 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f    if( (f2 & MEM_
19ca3 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Str)==0 ){.     
19ca4 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
19ca5 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  }..    assert( p
19ca6 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32  Mem1->enc==pMem2
19ca7 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73  ->enc );.    ass
19ca8 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  ert( pMem1->enc=
19ca9 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20  =SQLITE_UTF8 || 
19caa 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65  .            pMe
19cab 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
19cac 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31  UTF16LE || pMem1
19cad 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
19cae 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a  F16BE );..    /*
19caf 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   The collation s
19cb0 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20  equence must be 
19cb1 64 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20  defined at this 
19cb2 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20  point, even if. 
19cb3 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64     ** the user d
19cb4 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61  eletes the colla
19cb5 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66  tion sequence af
19cb6 74 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f  ter the vdbe pro
19cb7 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63  gram is.    ** c
19cb8 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61  ompiled (this wa
19cb9 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65  s not always the
19cba 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20   case)..    */. 
19cbb 20 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c     assert( !pCol
19cbc 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l || pColl->xCmp
19cbd 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f   );..    if( pCo
19cbe 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
19cbf 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c  pMem1->enc==pCol
19cc0 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20  l->enc ){.      
19cc1 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73    /* The strings
19cc2 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20   are already in 
19cc3 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f  the correct enco
19cc4 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a  ding.  Call the.
19cc5 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61          ** compa
19cc6 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64  rison function d
19cc7 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20  irectly */.     
19cc8 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d     return pColl-
19cc9 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73  >xCmp(pColl->pUs
19cca 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d  er,pMem1->n,pMem
19ccb 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d  1->z,pMem2->n,pM
19ccc 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d  em2->z);.      }
19ccd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
19cce 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76  nst void *v1, *v
19ccf 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  2;.        int n
19cd0 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d  1, n2;.        M
19cd1 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d  em c1;.        M
19cd2 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d  em c2;.        m
19cd3 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69  emset(&c1, 0, si
19cd4 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20  zeof(c1));.     
19cd5 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30     memset(&c2, 0
19cd6 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20  , sizeof(c2));. 
19cd7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19cd8 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
19cd9 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d  (&c1, pMem1, MEM
19cda 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20  _Ephem);.       
19cdb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19cdc 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20  hallowCopy(&c2, 
19cdd 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d  pMem2, MEM_Ephem
19cde 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20  );.        v1 = 
19cdf 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
19ce0 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  ((sqlite3_value*
19ce1 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63  )&c1, pColl->enc
19ce2 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20  );.        n1 = 
19ce3 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e  v1==0 ? 0 : c1.n
19ce4 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73  ;.        v2 = s
19ce5 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
19ce6 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
19ce7 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29  &c2, pColl->enc)
19ce8 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76  ;.        n2 = v
19ce9 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b  2==0 ? 0 : c2.n;
19cea 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43  .        rc = pC
19ceb 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d  oll->xCmp(pColl-
19cec 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20  >pUser, n1, v1, 
19ced 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20  n2, v2);.       
19cee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19cef 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20  elease(&c1);.   
19cf0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19cf1 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b  MemRelease(&c2);
19cf2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
19cf3 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
19cf4 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55  }.    /* If a NU
19cf5 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70  LL pointer was p
19cf6 61 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c  assed as the col
19cf7 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66  late function, f
19cf8 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20  all through.    
19cf9 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63  ** to the blob c
19cfa 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63  ase and use memc
19cfb 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a  mp().  */.  }. .
19cfc 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73    /* Both values
19cfd 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20   must be blobs. 
19cfe 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d   Compare using m
19cff 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72  emcmp().  */.  r
19d00 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31  c = memcmp(pMem1
19d01 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28  ->z, pMem2->z, (
19d02 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e  pMem1->n>pMem2->
19d03 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d  n)?pMem2->n:pMem
19d04 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d  1->n);.  if( rc=
19d05 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  =0 ){.    rc = p
19d06 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d  Mem1->n - pMem2-
19d07 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  >n;.  }.  return
19d08 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f   rc;.}../*.** Mo
19d09 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61  ve data out of a
19d0a 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61   btree key or da
19d0b 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74  ta field and int
19d0c 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72  o a Mem structur
19d0d 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f  e..** The data o
19d0e 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66  r key is taken f
19d0f 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68  rom the entry th
19d10 61 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65  at pCur is curre
19d11 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a  ntly pointing.**
19d12 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64   to.  offset and
19d13 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77   amt determine w
19d14 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  hat portion of t
19d15 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74  he data or key t
19d16 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b  o retrieve..** k
19d17 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65  ey is true to ge
19d18 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c  t the key or fal
19d19 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20  se to get data. 
19d1a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77   The result is w
19d1b 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74  ritten.** into t
19d1c 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e  he pMem element.
19d1d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20  .**.** The pMem 
19d1e 73 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73  structure is ass
19d1f 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69  umed to be unini
19d20 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70  tialized.  Any p
19d21 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20  rior content.** 
19d22 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  is overwritten w
19d23 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65  ithout being fre
19d24 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  ed..**.** If thi
19d25 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20  s routine fails 
19d26 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28  for any reason (
19d27 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e  malloc returns N
19d28 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a  ULL or unable.**
19d29 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68   to read from th
19d2a 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65  e disk) then the
19d2b 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e   pMem is left in
19d2c 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
19d2d 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54   state..*/.SQLIT
19d2e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19d2f 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
19d30 42 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f  Btree(.  BtCurso
19d31 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75  r *pCur,   /* Cu
19d32 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
19d33 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69   record to retri
19d34 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66  eve. */.  int of
19d35 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f  fset,       /* O
19d36 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73  ffset from the s
19d37 74 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20  tart of data to 
19d38 72 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f  return bytes fro
19d39 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  m. */.  int amt,
19d3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
19d3b 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
19d3c 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74  return. */.  int
19d3d 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f   key,          /
19d3e 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69  * If true, retri
19d3f 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72  eve from the btr
19d40 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61  ee key, not data
19d41 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  . */.  Mem *pMem
19d42 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
19d43 20 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20   Return data in 
19d44 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75  this Mem structu
19d45 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72  re. */.){.  char
19d46 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20   *zData;        
19d47 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65  /* Data from the
19d48 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a   btree layer */.
19d49 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20    int available 
19d4a 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20  = 0;  /* Number 
19d4b 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62  of bytes availab
19d4c 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  le on the local 
19d4d 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20  btree page */.  
19d4e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
19d4f 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f  OK; /* Return co
19d50 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  de */..  assert(
19d51 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
19d52 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29  sorIsValid(pCur)
19d53 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20   );..  /* Note: 
19d54 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72  the calls to Btr
19d55 65 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64  eeKeyFetch() and
19d56 20 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c   DataFetch() bel
19d57 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a  ow assert() .  *
19d58 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20  * that both the 
19d59 42 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74  BtShared and dat
19d5a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74  abase handle mut
19d5b 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a  exes are held. *
19d5c 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  /.  assert( (pMe
19d5d 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
19d5e 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69  owSet)==0 );.  i
19d5f 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44  f( key ){.    zD
19d60 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ata = (char *)sq
19d61 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74  lite3BtreeKeyFet
19d62 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61  ch(pCur, &availa
19d63 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ble);.  }else{. 
19d64 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
19d65 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44   *)sqlite3BtreeD
19d66 61 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26  ataFetch(pCur, &
19d67 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a  available);.  }.
19d68 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21    assert( zData!
19d69 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66  =0 );..  if( off
19d6a 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62  set+amt<=availab
19d6b 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61  le && (pMem->fla
19d6c 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29  gs&MEM_Dyn)==0 )
19d6d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
19d6e 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
19d6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d  );.    pMem->z =
19d70 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b   &zData[offset];
19d71 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
19d72 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f   = MEM_Blob|MEM_
19d73 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69  Ephem;.  }else i
19d74 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  f( SQLITE_OK==(r
19d75 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
19d76 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74  emGrow(pMem, amt
19d77 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70  +2, 0)) ){.    p
19d78 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
19d79 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45  _Blob|MEM_Dyn|ME
19d7a 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d  M_Term;.    pMem
19d7b 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70  ->enc = 0;.    p
19d7c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
19d7d 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28  TE_BLOB;.    if(
19d7e 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63   key ){.      rc
19d7f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
19d80 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c  ey(pCur, offset,
19d81 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a   amt, pMem->z);.
19d82 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19d83 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19d84 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66  eeData(pCur, off
19d85 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e  set, amt, pMem->
19d86 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  z);.    }.    pM
19d87 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a  em->z[amt] = 0;.
19d88 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b      pMem->z[amt+
19d89 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  1] = 0;.    if( 
19d8a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
19d8b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19d8c 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
19d8d 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  m);.    }.  }.  
19d8e 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a  pMem->n = amt;..
19d8f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19d90 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
19d91 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
19d92 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69  le internally, i
19d93 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
19d94 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c   the.** external
19d95 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69   API. It works i
19d96 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20  n a similar way 
19d97 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  to sqlite3_value
19d98 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65  _text(),.** exce
19d99 70 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75  pt the data retu
19d9a 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65  rned is in the e
19d9b 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65  ncoding specifie
19d9c 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a  d by the second.
19d9d 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68  ** parameter, wh
19d9e 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20  ich must be one 
19d9f 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  of SQLITE_UTF16B
19da0 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
19da1 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55  E or.** SQLITE_U
19da2 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36  TF8..**.** (2006
19da3 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e  -02-16:)  The en
19da4 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f  c value can be o
19da5 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45  r-ed with SQLITE
19da6 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a  _UTF16_ALIGNED..
19da7 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68  ** If that is th
19da8 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65  e case, then the
19da9 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20   result must be 
19daa 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76  aligned on an ev
19dab 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64  en byte.** bound
19dac 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
19dad 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
19dae 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54  d *sqlite3ValueT
19daf 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ext(sqlite3_valu
19db0 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29  e* pVal, u8 enc)
19db1 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20  {.  if( !pVal ) 
19db2 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73  return 0;..  ass
19db3 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30  ert( pVal->db==0
19db4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
19db5 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d  x_held(pVal->db-
19db6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
19db7 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65  ert( (enc&3)==(e
19db8 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36  nc&~SQLITE_UTF16
19db9 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61  _ALIGNED) );.  a
19dba 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c  ssert( (pVal->fl
19dbb 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
19dbc 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70  )==0 );..  if( p
19dbd 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e  Val->flags&MEM_N
19dbe 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
19dbf 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
19dc0 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  t( (MEM_Blob>>3)
19dc1 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20   == MEM_Str );. 
19dc2 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20   pVal->flags |= 
19dc3 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d  (pVal->flags & M
19dc4 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65  EM_Blob)>>3;.  e
19dc5 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b  xpandBlob(pVal);
19dc6 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61  .  if( pVal->fla
19dc7 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20  gs&MEM_Str ){.  
19dc8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
19dc9 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c  ngeEncoding(pVal
19dca 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  , enc & ~SQLITE_
19dcb 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a  UTF16_ALIGNED);.
19dcc 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53      if( (enc & S
19dcd 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
19dce 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31  NED)!=0 && 1==(1
19dcf 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49  &SQLITE_PTR_TO_I
19dd0 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a  NT(pVal->z)) ){.
19dd1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
19dd2 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Val->flags & (ME
19dd3 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74  M_Ephem|MEM_Stat
19dd4 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20  ic))!=0 );.     
19dd5 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
19dd6 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
19dd7 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f  (pVal)!=SQLITE_O
19dd8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
19dd9 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
19dda 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
19ddb 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
19ddc 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  ate(pVal);.  }el
19ddd 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
19dde 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pVal->flags&MEM
19ddf 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20  _Blob)==0 );.   
19de0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19de1 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 65  tringify(pVal, e
19de2 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  nc);.    assert(
19de3 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54   0==(1&SQLITE_PT
19de4 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a  R_TO_INT(pVal->z
19de5 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  )) );.  }.  asse
19de6 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65  rt(pVal->enc==(e
19de7 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
19de8 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70  16_ALIGNED) || p
19de9 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20  Val->db==0.     
19dea 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 61 6c           || pVal
19deb 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
19dec 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 61 6c  ed );.  if( pVal
19ded 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53  ->enc==(enc & ~S
19dee 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
19def 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 74 75  NED) ){.    retu
19df0 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65  rn pVal->z;.  }e
19df1 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
19df2 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
19df3 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c  Create a new sql
19df4 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
19df5 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
19df6 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c  VATE sqlite3_val
19df7 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  ue *sqlite3Value
19df8 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 62 29  New(sqlite3 *db)
19df9 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c  {.  Mem *p = sql
19dfa 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
19dfb 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29  (db, sizeof(*p))
19dfc 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
19dfd 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f   p->flags = MEM_
19dfe 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70  Null;.    p->typ
19dff 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
19e00 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  .    p->db = db;
19e01 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
19e02 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
19e03 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76   a new sqlite3_v
19e04 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e  alue object, con
19e05 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75  taining the valu
19e06 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a  e of pExpr..**.*
19e07 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b  * This only work
19e08 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c  s for very simpl
19e09 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  e expressions th
19e0a 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e  at consist of on
19e0b 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f  e constant.** to
19e0c 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22  ken (i.e. "5", "
19e0d 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 6e 67  5.1", "'a string
19e0e 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70 72  '"). If the expr
19e0f 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65  ession can.** be
19e10 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65 63   converted direc
19e11 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75 65  tly into a value
19e12 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
19e13 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
19e14 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 77  d.** a pointer w
19e15 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c  ritten to *ppVal
19e16 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20  . The caller is 
19e17 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
19e18 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20  deallocating.** 
19e19 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 61 73  the value by pas
19e1a 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69 74  sing it to sqlit
19e1b 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 6c 61  e3ValueFree() la
19e1c 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 20 65  ter on. If the e
19e1d 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e  xpression.** can
19e1e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  not be converted
19e1f 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 65   to a value, the
19e20 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 74 20  n *ppVal is set 
19e21 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
19e22 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19e23 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
19e24 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xpr(.  sqlite3 *
19e25 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
19e26 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
19e27 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
19e28 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
19e29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
19e2a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
19e2b 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38  evaluate */.  u8
19e2c 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20   enc,           
19e2d 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
19e2e 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  ing to use */.  
19e2f 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20  u8 affinity,    
19e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66            /* Aff
19e31 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a  inity to use */.
19e32 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
19e33 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a 20 57  **ppVal     /* W
19e34 72 69 74 65 20 74 68 65 20 6e 65 77 20 76 61 6c  rite the new val
19e35 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ue here */.){.  
19e36 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a  int op;.  char *
19e37 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69  zVal = 0;.  sqli
19e38 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
19e39 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70 45 78  = 0;..  if( !pEx
19e3a 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c  pr ){.    *ppVal
19e3b 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
19e3c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
19e3d 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
19e3e 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52  ;.  if( op==TK_R
19e3f 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20 6f  EGISTER ){.    o
19e40 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32 3b 20  p = pExpr->op2; 
19e41 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61   /* This only ha
19e42 70 70 65 6e 73 20 77 69 74 68 20 53 51 4c 49 54  ppens with SQLIT
19e43 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a  E_ENABLE_STAT2 *
19e44 2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d  /.  }..  if( op=
19e45 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70  =TK_STRING || op
19e46 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70  ==TK_FLOAT || op
19e47 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
19e48 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74      pVal = sqlit
19e49 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a  e3ValueNew(db);.
19e4a 20 20 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20      if( pVal==0 
19e4b 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
19e4c 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
19e4d 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
19e4e 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0a 20 20  _IntValue) ){.  
19e4f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19e50 65 6d 53 65 74 49 6e 74 36 34 28 70 56 61 6c 2c  emSetInt64(pVal,
19e51 20 28 69 36 34 29 70 45 78 70 72 2d 3e 75 2e 69   (i64)pExpr->u.i
19e52 56 61 6c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73  Value);.    }els
19e53 65 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20  e{.      zVal = 
19e54 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
19e55 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  db, pExpr->u.zTo
19e56 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ken);.      if( 
19e57 7a 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  zVal==0 ) goto n
19e58 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c  o_mem;.      sql
19e59 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
19e5a 70 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20  pVal, -1, zVal, 
19e5b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
19e5c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
19e5d 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46      if( op==TK_F
19e5e 4c 4f 41 54 20 29 20 70 56 61 6c 2d 3e 74 79 70  LOAT ) pVal->typ
19e5f 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  e = SQLITE_FLOAT
19e60 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
19e61 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20  (op==TK_INTEGER 
19e62 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  || op==TK_FLOAT 
19e63 29 20 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53  ) && affinity==S
19e64 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
19e65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19e66 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
19e67 79 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41  y(pVal, SQLITE_A
19e68 46 46 5f 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49  FF_NUMERIC, SQLI
19e69 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 65  TE_UTF8);.    }e
19e6a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
19e6b 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69  e3ValueApplyAffi
19e6c 6e 69 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e  nity(pVal, affin
19e6d 69 74 79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  ity, SQLITE_UTF8
19e6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
19e6f 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46   enc!=SQLITE_UTF
19e70 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  8 ){.      sqlit
19e71 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
19e72 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b  ding(pVal, enc);
19e73 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
19e74 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53  f( op==TK_UMINUS
19e75 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c   ) {.    if( SQL
19e76 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56  ITE_OK==sqlite3V
19e77 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c  alueFromExpr(db,
19e78 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63  pExpr->pLeft,enc
19e79 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29  ,affinity,&pVal)
19e7a 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e   ){.      pVal->
19e7b 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d  u.i = -1 * pVal-
19e7c 3e 75 2e 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28  >u.i;.      /* (
19e7d 64 6f 75 62 6c 65 29 2d 31 20 49 6e 20 63 61 73  double)-1 In cas
19e7e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
19e7f 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
19e80 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c  .. */.      pVal
19e81 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31  ->r = (double)-1
19e82 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20   * pVal->r;.    
19e83 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  }.  }.#ifndef SQ
19e84 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
19e85 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66  ITERAL.  else if
19e86 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b  ( op==TK_BLOB ){
19e87 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20  .    int nVal;. 
19e88 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
19e89 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27  ->u.zToken[0]=='
19e8a 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a  x' || pExpr->u.z
19e8b 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b  Token[0]=='X' );
19e8c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
19e8d 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d  pr->u.zToken[1]=
19e8e 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61  ='\'' );.    pVa
19e8f 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  l = sqlite3Value
19e90 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28  New(db);.    if(
19e91 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f   !pVal ) goto no
19e92 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d  _mem;.    zVal =
19e93 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65   &pExpr->u.zToke
19e94 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d  n[2];.    nVal =
19e95 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
19e96 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73  (zVal)-1;.    as
19e97 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d  sert( zVal[nVal]
19e98 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 73 71  =='\'' );.    sq
19e99 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
19e9a 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33  tr(pVal, sqlite3
19e9b 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56  HexToBlob(db, zV
19e9c 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f  al, nVal), nVal/
19e9d 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
19e9e 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53              0, S
19e9f 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
19ea0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
19ea1 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 73 71  ( pVal ){.    sq
19ea2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
19ea3 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20 20 7d  eType(pVal);.  }
19ea4 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c  .  *ppVal = pVal
19ea5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
19ea6 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20  E_OK;..no_mem:. 
19ea7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
19ea8 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33  d = 1;.  sqlite3
19ea9 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c 29  DbFree(db, zVal)
19eaa 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  ;.  sqlite3Value
19eab 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70  Free(pVal);.  *p
19eac 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75  pVal = 0;.  retu
19ead 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
19eae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
19eaf 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75   the string valu
19eb0 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f  e of an sqlite3_
19eb1 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a  value object.*/.
19eb2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19eb3 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
19eb4 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74 65  SetStr(.  sqlite
19eb5 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 20  3_value *v,     
19eb6 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 73  /* Value to be s
19eb7 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20  et */.  int n,  
19eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19eb9 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e   Length of strin
19eba 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  g z */.  const v
19ebb 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f  oid *z,        /
19ebc 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e 65  * Text of the ne
19ebd 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38  w string */.  u8
19ebe 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20   enc,           
19ebf 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20      /* Encoding 
19ec0 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64  to use */.  void
19ec1 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20   (*xDel)(void*) 
19ec2 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
19ec3 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2a  for the string *
19ec4 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20 73  /.){.  if( v ) s
19ec5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
19ec6 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c  Str((Mem *)v, z,
19ec7 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a   n, enc, xDel);.
19ec8 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e  }../*.** Free an
19ec9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
19eca 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bject.*/.SQLITE_
19ecb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19ecc 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71  ite3ValueFree(sq
19ecd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b  lite3_value *v){
19ece 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75  .  if( !v ) retu
19ecf 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  rn;.  sqlite3Vdb
19ed0 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d  eMemRelease((Mem
19ed1 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65 33   *)v);.  sqlite3
19ed2 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 29  DbFree(((Mem*)v)
19ed3 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a  ->db, v);.}../*.
19ed4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
19ed5 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
19ed6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   the sqlite3_val
19ed7 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d 69  ue object assumi
19ed8 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75 73  ng.** that it us
19ed9 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  es the encoding 
19eda 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f  "enc".*/.SQLITE_
19edb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19edc 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71  te3ValueBytes(sq
19edd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
19ede 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65  l, u8 enc){.  Me
19edf 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61  m *p = (Mem*)pVa
19ee0 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61  l;.  if( (p->fla
19ee1 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d  gs & MEM_Blob)!=
19ee2 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75  0 || sqlite3Valu
19ee3 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63 29  eText(pVal, enc)
19ee4 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66   ){.    if( p->f
19ee5 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
19ee6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19ee7 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72  p->n + p->u.nZer
19ee8 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  o;.    }else{.  
19ee9 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b      return p->n;
19eea 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
19eeb 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  urn 0;.}../*****
19eec 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
19eed 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a   vdbemem.c *****
19eee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19eef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ef0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
19ef1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
19ef2 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63 20 2a  file vdbeaux.c *
19ef3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ef4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ef5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
19ef6 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36  2003 September 6
19ef7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
19ef8 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
19ef9 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
19efa 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
19efb 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
19efc 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
19efd 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
19efe 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
19eff 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
19f00 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
19f01 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
19f02 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
19f03 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
19f04 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
19f05 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
19f06 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
19f07 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
19f08 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
19f09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
19f0d 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
19f0e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
19f0f 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20 64 65  for creating, de
19f10 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f  stroying, and po
19f11 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44  pulating.** a VD
19f12 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c 69 74  BE (or an "sqlit
19f13 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74 20 69  e3_stmt" as it i
19f14 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f  s known to the o
19f15 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 20 20  utside world.)  
19f16 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73  Prior.** to vers
19f17 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74  ion 2.8.7, all t
19f18 68 69 73 20 63 6f 64 65 20 77 61 73 20 63 6f 6d  his code was com
19f19 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 76  bined into the v
19f1a 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c  dbe.c source fil
19f1b 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 66  e..** But that f
19f1c 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e 67 20  ile was getting 
19f1d 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 73 20  too big so this 
19f1e 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 72 65  subroutines were
19f1f 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f 0a 0a   split out..*/..
19f20 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62  ../*.** When deb
19f21 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20  ugging the code 
19f22 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73  generator in a s
19f23 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72  ymbolic debugger
19f24 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74  , one can.** set
19f25 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
19f26 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20  AddopTrace to 1 
19f27 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  and all opcodes 
19f28 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a  will be printed.
19f29 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61  ** as they are a
19f2a 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74  dded to the inst
19f2b 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a  ruction stream..
19f2c 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
19f2d 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
19f2e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19f2f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
19f30 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
19f31 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
19f32 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
19f33 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c  e engine..*/.SQL
19f34 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
19f35 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65   *sqlite3VdbeCre
19f36 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ate(sqlite3 *db)
19f37 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70  {.  Vdbe *p;.  p
19f38 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
19f39 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
19f3a 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28  f(Vdbe) );.  if(
19f3b 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
19f3c 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
19f3d 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
19f3e 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  ){.    db->pVdbe
19f3f 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d  ->pPrev = p;.  }
19f40 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62  .  p->pNext = db
19f41 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50  ->pVdbe;.  p->pP
19f42 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70  rev = 0;.  db->p
19f43 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d  Vdbe = p;.  p->m
19f44 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
19f45 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
19f46 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   p;.}../*.** Rem
19f47 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74  ember the SQL st
19f48 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61  ring for a prepa
19f49 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  red statement..*
19f4a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19f4b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19f4c 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c  eSetSql(Vdbe *p,
19f4d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
19f4e 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65  int n, int isPre
19f4f 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 65 72  pareV2){.  asser
19f50 74 28 20 69 73 50 72 65 70 61 72 65 56 32 3d 3d  t( isPrepareV2==
19f51 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 56 32  1 || isPrepareV2
19f52 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  ==0 );.  if( p==
19f53 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 64  0 ) return;.#ifd
19f54 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
19f55 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 50 72  RACE.  if( !isPr
19f56 65 70 61 72 65 56 32 20 29 20 72 65 74 75 72 6e  epareV2 ) return
19f57 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72  ;.#endif.  asser
19f58 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b  t( p->zSql==0 );
19f59 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c  .  p->zSql = sql
19f5a 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d  ite3DbStrNDup(p-
19f5b 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d  >db, z, n);.  p-
19f5c 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d 20 28  >isPrepareV2 = (
19f5d 75 38 29 69 73 50 72 65 70 61 72 65 56 32 3b 0a  u8)isPrepareV2;.
19f5e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
19f5f 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74  the SQL associat
19f60 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72  ed with a prepar
19f61 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a  ed statement.*/.
19f62 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
19f63 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
19f64 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ql(sqlite3_stmt 
19f65 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
19f66 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
19f67 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  mt;.  return (p-
19f68 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70  >isPrepareV2 ? p
19f69 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a  ->zSql : 0);.}..
19f6a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63  /*.** Swap all c
19f6b 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74  ontent between t
19f6c 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72  wo VDBE structur
19f6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
19f6e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19f6f 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20  e3VdbeSwap(Vdbe 
19f70 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a  *pA, Vdbe *pB){.
19f71 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d    Vdbe tmp, *pTm
19f72 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b  p;.  char *zTmp;
19f73 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20  .  tmp = *pA;.  
19f74 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42  *pA = *pB;.  *pB
19f75 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d   = tmp;.  pTmp =
19f76 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41   pA->pNext;.  pA
19f77 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e  ->pNext = pB->pN
19f78 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74  ext;.  pB->pNext
19f79 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20   = pTmp;.  pTmp 
19f7a 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70  = pA->pPrev;.  p
19f7b 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70  A->pPrev = pB->p
19f7c 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65  Prev;.  pB->pPre
19f7d 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70  v = pTmp;.  zTmp
19f7e 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70   = pA->zSql;.  p
19f7f 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53  A->zSql = pB->zS
19f80 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d  ql;.  pB->zSql =
19f81 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50   zTmp;.  pB->isP
19f82 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69  repareV2 = pA->i
19f83 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23  sPrepareV2;.}..#
19f84 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
19f85 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72  UG./*.** Turn tr
19f86 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a  acing on or off.
19f87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19f88 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19f89 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c  beTrace(Vdbe *p,
19f8a 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20   FILE *trace){. 
19f8b 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63   p->trace = trac
19f8c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
19f8d 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64  ** Resize the Vd
19f8e 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20  be.aOp array so 
19f8f 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65  that it is at le
19f90 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65  ast one op large
19f91 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61  r than .** it wa
19f92 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f  s..**.** If an o
19f93 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72  ut-of-memory err
19f94 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
19f95 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72  resizing the arr
19f96 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ay, return.** SQ
19f97 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74  LITE_NOMEM. In t
19f98 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f  his case Vdbe.aO
19f99 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c  p and Vdbe.nOpAl
19f9a 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75  loc remain .** u
19f9b 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69  nchanged (this i
19f9c 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70  s so that any op
19f9d 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c  codes already al
19f9e 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a  located can be .
19f9f 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61  ** correctly dea
19fa0 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77  llocated along w
19fa1 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20  ith the rest of 
19fa2 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74  the Vdbe)..*/.st
19fa3 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41  atic int growOpA
19fa4 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20  rray(Vdbe *p){. 
19fa5 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20   VdbeOp *pNew;. 
19fa6 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e   int nNew = (p->
19fa7 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f  nOpAlloc ? p->nO
19fa8 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29  pAlloc*2 : (int)
19fa9 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29  (1024/sizeof(Op)
19faa 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  ));.  pNew = sql
19fab 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d  ite3DbRealloc(p-
19fac 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65  >db, p->aOp, nNe
19fad 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20  w*sizeof(Op));. 
19fae 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
19faf 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73   p->nOpAlloc = s
19fb0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
19fb1 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f  ze(p->db, pNew)/
19fb2 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20  sizeof(Op);.    
19fb3 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20  p->aOp = pNew;. 
19fb4 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65   }.  return (pNe
19fb5 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  w ? SQLITE_OK : 
19fb6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
19fb7 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
19fb8 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  w instruction to
19fb9 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73   the list of ins
19fba 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  tructions curren
19fbb 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45  t in the.** VDBE
19fbc 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64  .  Return the ad
19fbd 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77  dress of the new
19fbe 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
19fbf 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a  .** Parameters:.
19fc0 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20  **.**    p      
19fc1 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72           Pointer
19fc2 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a   to the VDBE.**.
19fc3 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20  **    op        
19fc4 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65        The opcode
19fc5 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75   for this instru
19fc6 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ction.**.**    p
19fc7 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f  1, p2, p3      O
19fc8 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73  perands.**.** Us
19fc9 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  e the sqlite3Vdb
19fca 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20  eResolveLabel() 
19fcb 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20  function to fix 
19fcc 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a  an address and.*
19fcd 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  * the sqlite3Vdb
19fce 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63  eChangeP4() func
19fcf 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74  tion to change t
19fd0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
19fd1 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a  P4.** operand..*
19fd2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19fd3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19fd4 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20  AddOp3(Vdbe *p, 
19fd5 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20  int op, int p1, 
19fd6 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b  int p2, int p3){
19fd7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65  .  int i;.  Vdbe
19fd8 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20  Op *pOp;..  i = 
19fd9 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74  p->nOp;.  assert
19fda 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
19fdb 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
19fdc 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26   assert( op>0 &&
19fdd 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66   op<0xff );.  if
19fde 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69  ( p->nOpAlloc<=i
19fdf 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77   ){.    if( grow
19fe0 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20  OpArray(p) ){.  
19fe1 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
19fe2 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70    }.  }.  p->nOp
19fe3 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  ++;.  pOp = &p->
19fe4 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f  aOp[i];.  pOp->o
19fe5 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a  pcode = (u8)op;.
19fe6 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20    pOp->p5 = 0;. 
19fe7 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20   pOp->p1 = p1;. 
19fe8 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20   pOp->p2 = p2;. 
19fe9 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20   pOp->p3 = p3;. 
19fea 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a   pOp->p4.p = 0;.
19feb 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
19fec 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d  P4_NOTUSED;.  p-
19fed 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69  >expired = 0;.#i
19fee 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
19fef 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e  G.  pOp->zCommen
19ff0 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c  t = 0;.  if( sql
19ff1 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
19ff2 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  ce ) sqlite3Vdbe
19ff3 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70  PrintOp(0, i, &p
19ff4 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69  ->aOp[i]);.#endi
19ff5 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  f.#ifdef VDBE_PR
19ff6 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63  OFILE.  pOp->cyc
19ff7 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e  les = 0;.  pOp->
19ff8 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  cnt = 0;.#endif.
19ff9 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51    return i;.}.SQ
19ffa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19ffb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19ffc 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p0(Vdbe *p, int 
19ffd 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  op){.  return sq
19ffe 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
19fff 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b  p, op, 0, 0, 0);
1a000 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1a001 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1a002 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c  eAddOp1(Vdbe *p,
1a003 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29   int op, int p1)
1a004 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1a005 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
1a006 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d  op, p1, 0, 0);.}
1a007 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a008 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
1a009 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69  ddOp2(Vdbe *p, i
1a00a 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
1a00b 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e  nt p2){.  return
1a00c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1a00d 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32  p3(p, op, p1, p2
1a00e 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , 0);.}.../*.** 
1a00f 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68  Add an opcode th
1a010 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20  at includes the 
1a011 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f  p4 value as a po
1a012 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
1a013 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a014 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a  ite3VdbeAddOp4(.
1a015 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20    Vdbe *p,      
1a016 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65        /* Add the
1a017 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20   opcode to this 
1a018 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  VM */.  int op, 
1a019 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a01a 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f  he new opcode */
1a01b 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20  .  int p1,      
1a01c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31         /* The P1
1a01d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
1a01e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20  t p2,           
1a01f 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72    /* The P2 oper
1a020 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c  and */.  int p3,
1a021 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a022 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a  The P3 operand *
1a023 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1a024 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50  zP4,    /* The P
1a025 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  4 operand */.  i
1a026 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20  nt p4type       
1a027 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64     /* P4 operand
1a028 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   type */.){.  in
1a029 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33  t addr = sqlite3
1a02a 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70  VdbeAddOp3(p, op
1a02b 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20  , p1, p2, p3);. 
1a02c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a02d 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50  geP4(p, addr, zP
1a02e 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65  4, p4type);.  re
1a02f 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a  turn addr;.}../*
1a030 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64  .** Add an opcod
1a031 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20  e that includes 
1a032 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73 20  the p4 value as 
1a033 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53  an integer..*/.S
1a034 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a035 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
1a036 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65 20 2a  Op4Int(.  Vdbe *
1a037 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p,            /*
1a038 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20   Add the opcode 
1a039 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  to this VM */.  
1a03a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
1a03b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f      /* The new o
1a03c 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70  pcode */.  int p
1a03d 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1,             /
1a03e 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64  * The P1 operand
1a03f 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20   */.  int p2,   
1a040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1a041 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P2 operand */. 
1a042 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20   int p3,        
1a043 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f       /* The P3 o
1a044 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
1a045 70 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p4              
1a046 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e  /* The P4 operan
1a047 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20  d as an integer 
1a048 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 72  */.){.  int addr
1a049 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1a04a 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20  dOp3(p, op, p1, 
1a04b 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69 74  p2, p3);.  sqlit
1a04c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 70  e3VdbeChangeP4(p
1a04d 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45 5f 49  , addr, SQLITE_I
1a04e 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c 20 50  NT_TO_PTR(p4), P
1a04f 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65 74 75  4_INT32);.  retu
1a050 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn addr;.}../*.*
1a051 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73  * Create a new s
1a052 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f  ymbolic label fo
1a053 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  r an instruction
1a054 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f   that has yet to
1a055 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54   be.** coded.  T
1a056 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65  he symbolic labe
1a057 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74  l is really just
1a058 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
1a059 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65  er.  The.** labe
1a05a 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  l can be used as
1a05b 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66   the P2 value of
1a05c 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20   an operation.  
1a05d 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74  Later, when.** t
1a05e 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f  he label is reso
1a05f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66  lved to a specif
1a060 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20  ic address, the 
1a061 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a  VDBE will scan.*
1a062 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70  * through its op
1a063 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64  eration list and
1a064 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75   change all valu
1a065 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d  es of P2 which m
1a066 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65  atch.** the labe
1a067 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c  l into the resol
1a068 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a  ved address..**.
1a069 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77  ** The VDBE know
1a06a 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75  s that a P2 valu
1a06b 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63  e is a label bec
1a06c 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a  ause labels are.
1a06d 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69  ** always negati
1a06e 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73  ve and P2 values
1a06f 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20   are suppose to 
1a070 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  be non-negative.
1a071 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67  .** Hence, a neg
1a072 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69  ative P2 value i
1a073 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68  s a label that h
1a074 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73  as yet to be res
1a075 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72  olved..**.** Zer
1a076 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  o is returned if
1a077 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1a078 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
1a079 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1a07a 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64  VdbeMakeLabel(Vd
1a07b 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  be *p){.  int i;
1a07c 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c  .  i = p->nLabel
1a07d 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  ++;.  assert( p-
1a07e 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a07f 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
1a080 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c   i>=p->nLabelAll
1a081 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20  oc ){.    int n 
1a082 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63  = p->nLabelAlloc
1a083 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61  *2 + 5;.    p->a
1a084 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 44  Label = sqlite3D
1a085 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70  bReallocOrFree(p
1a086 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c  ->db, p->aLabel,
1a087 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1a088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a089 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66          n*sizeof
1a08a 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b  (p->aLabel[0]));
1a08b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c  .    p->nLabelAl
1a08c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  loc = sqlite3DbM
1a08d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c  allocSize(p->db,
1a08e 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65   p->aLabel)/size
1a08f 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29  of(p->aLabel[0])
1a090 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61  ;.  }.  if( p->a
1a091 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e  Label ){.    p->
1a092 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a  aLabel[i] = -1;.
1a093 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d    }.  return -1-
1a094 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f  i;.}../*.** Reso
1a095 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 6f  lve label "x" to
1a096 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73 20   be the address 
1a097 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  of the next inst
1a098 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65  ruction to.** be
1a099 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65 20   inserted.  The 
1a09a 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d 75  parameter "x" mu
1a09b 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74  st have been obt
1a09c 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20  ained from.** a 
1a09d 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
1a09e 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1a09f 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  el()..*/.SQLITE_
1a0a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a0a1 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
1a0a2 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e  abel(Vdbe *p, in
1a0a3 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20  t x){.  int j = 
1a0a4 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28 20  -1-x;.  assert( 
1a0a5 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
1a0a6 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61  AGIC_INIT );.  a
1a0a7 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a  ssert( j>=0 && j
1a0a8 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20  <p->nLabel );.  
1a0a9 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b  if( p->aLabel ){
1a0aa 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a  .    p->aLabel[j
1a0ab 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a  ] = p->nOp;.  }.
1a0ac 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
1a0ad 5f 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 74 65  _DEBUG /* sqlite
1a0ae 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28  3AssertMayAbort(
1a0af 29 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a  ) logic */../*.*
1a0b0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1a0b1 74 79 70 65 20 61 6e 64 20 66 75 6e 63 74 69 6f  type and functio
1a0b2 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 74  n are used to it
1a0b3 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c  erate through al
1a0b4 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20  l opcodes.** in 
1a0b5 61 20 56 64 62 65 20 6d 61 69 6e 20 70 72 6f 67  a Vdbe main prog
1a0b6 72 61 6d 20 61 6e 64 20 65 61 63 68 20 6f 66 20  ram and each of 
1a0b7 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 73  the sub-programs
1a0b8 20 28 74 72 69 67 67 65 72 73 29 20 69 74 20 6d   (triggers) it m
1a0b9 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69  ay .** invoke di
1a0ba 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65  rectly or indire
1a0bb 63 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c 64 20  ctly. It should 
1a0bc 62 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f  be used as follo
1a0bd 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a  ws:.**.**   Op *
1a0be 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70  pOp;.**   VdbeOp
1a0bf 49 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a  Iter sIter;.**.*
1a0c0 2a 20 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65  *   memset(&sIte
1a0c1 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74  r, 0, sizeof(sIt
1a0c2 65 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72  er));.**   sIter
1a0c3 2e 76 20 3d 20 76 3b 20 20 20 20 20 20 20 20 20  .v = v;         
1a0c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0c5 20 20 20 2f 2f 20 76 20 69 73 20 6f 66 20 74 79     // v is of ty
1a0c6 70 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77  pe Vdbe* .**   w
1a0c7 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49  hile( (pOp = opI
1a0c8 74 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29  terNext(&sIter))
1a0c9 20 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f   ){.**     // Do
1a0ca 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20   something with 
1a0cb 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20  pOp.**   }.**   
1a0cc 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d  sqlite3DbFree(v-
1a0cd 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62  >db, sIter.apSub
1a0ce 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65  );.** .*/.typede
1a0cf 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 49  f struct VdbeOpI
1a0d0 74 65 72 20 56 64 62 65 4f 70 49 74 65 72 3b 0a  ter VdbeOpIter;.
1a0d1 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65  struct VdbeOpIte
1a0d2 72 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20  r {.  Vdbe *v;  
1a0d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0d4 20 2f 2a 20 56 64 62 65 20 74 6f 20 69 74 65 72   /* Vdbe to iter
1a0d5 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20  ate through the 
1a0d6 6f 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20  opcodes of */.  
1a0d7 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53  SubProgram **apS
1a0d8 75 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72  ub;        /* Ar
1a0d9 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 72 61  ray of subprogra
1a0da 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62  ms */.  int nSub
1a0db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a0dc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1a0dd 65 6e 74 72 69 65 73 20 69 6e 20 61 70 53 75 62  entries in apSub
1a0de 20 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 72 3b   */.  int iAddr;
1a0df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0e0 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
1a0e1 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1a0e2 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69  to return */.  i
1a0e3 6e 74 20 69 53 75 62 3b 20 20 20 20 20 20 20 20  nt iSub;        
1a0e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 3d            /* 0 =
1a0e5 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31   main program, 1
1a0e6 20 3d 20 66 69 72 73 74 20 73 75 62 2d 70 72 6f   = first sub-pro
1a0e7 67 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a  gram etc. */.};.
1a0e8 73 74 61 74 69 63 20 4f 70 20 2a 6f 70 49 74 65  static Op *opIte
1a0e9 72 4e 65 78 74 28 56 64 62 65 4f 70 49 74 65 72  rNext(VdbeOpIter
1a0ea 20 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a 76 20   *p){.  Vdbe *v 
1a0eb 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52  = p->v;.  Op *pR
1a0ec 65 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f  et = 0;.  Op *aO
1a0ed 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20  p;.  int nOp;.. 
1a0ee 20 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d   if( p->iSub<=p-
1a0ef 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20 69 66  >nSub ){..    if
1a0f0 28 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a  ( p->iSub==0 ){.
1a0f1 20 20 20 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61        aOp = v->a
1a0f2 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20  Op;.      nOp = 
1a0f3 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73  v->nOp;.    }els
1a0f4 65 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 70  e{.      aOp = p
1a0f5 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d  ->apSub[p->iSub-
1a0f6 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e  1]->aOp;.      n
1a0f7 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d  Op = p->apSub[p-
1a0f8 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20  >iSub-1]->nOp;. 
1a0f9 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1a0fa 20 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b   p->iAddr<nOp );
1a0fb 0a 0a 20 20 20 20 70 52 65 74 20 3d 20 26 61 4f  ..    pRet = &aO
1a0fc 70 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20  p[p->iAddr];.   
1a0fd 20 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20   p->iAddr++;.   
1a0fe 20 69 66 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e   if( p->iAddr==n
1a0ff 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69  Op ){.      p->i
1a100 53 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e  Sub++;.      p->
1a101 69 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d  iAddr = 0;.    }
1a102 0a 20 20 0a 20 20 20 20 69 66 28 20 70 52 65 74  .  .    if( pRet
1a103 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42  ->p4type==P4_SUB
1a104 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20  PROGRAM ){.     
1a105 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 70 2d   int nByte = (p-
1a106 3e 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28  >nSub+1)*sizeof(
1a107 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20  SubProgram*);.  
1a108 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
1a109 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e   for(j=0; j<p->n
1a10a 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Sub; j++){.     
1a10b 20 20 20 69 66 28 20 70 2d 3e 61 70 53 75 62 5b     if( p->apSub[
1a10c 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72  j]==pRet->p4.pPr
1a10d 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20  ogram ) break;. 
1a10e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1a10f 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20   j==p->nSub ){. 
1a110 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 20         p->apSub 
1a111 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
1a112 6f 63 4f 72 46 72 65 65 28 76 2d 3e 64 62 2c 20  ocOrFree(v->db, 
1a113 70 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74 65 29  p->apSub, nByte)
1a114 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
1a115 2d 3e 61 70 53 75 62 20 29 7b 0a 20 20 20 20 20  ->apSub ){.     
1a116 20 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20       pRet = 0;. 
1a117 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1a118 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62          p->apSub
1a119 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52  [p->nSub++] = pR
1a11a 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b  et->p4.pProgram;
1a11b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1a11c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
1a11d 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a  return pRet;.}..
1a11e 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66 20 74  /*.** Check if t
1a11f 68 65 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65  he program store
1a120 64 20 69 6e 20 74 68 65 20 56 4d 20 61 73 73 6f  d in the VM asso
1a121 63 69 61 74 65 64 20 77 69 74 68 20 70 50 61 72  ciated with pPar
1a122 73 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20  se may.** throw 
1a123 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69  an ABORT excepti
1a124 6f 6e 20 28 63 61 75 73 69 6e 67 20 74 68 65 20  on (causing the 
1a125 73 74 61 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e  statement, but n
1a126 6f 74 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  ot entire transa
1a127 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72  ction.** to be r
1a128 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 68 69  olled back). Thi
1a129 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74  s condition is t
1a12a 72 75 65 20 69 66 20 74 68 65 20 6d 61 69 6e 20  rue if the main 
1a12b 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a  program or any.*
1a12c 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63  * sub-programs c
1a12d 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 20 74  ontains any of t
1a12e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
1a12f 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74  .**   *  OP_Halt
1a130 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f   with P1=SQLITE_
1a131 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50  CONSTRAINT and P
1a132 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20  2=OE_Abort..**  
1a133 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c   *  OP_HaltIfNul
1a134 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45  l with P1=SQLITE
1a135 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20  _CONSTRAINT and 
1a136 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20  P2=OE_Abort..** 
1a137 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a    *  OP_Destroy.
1a138 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61  **   *  OP_VUpda
1a139 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52  te.**   *  OP_VR
1a13a 65 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50  ename.**   *  OP
1a13b 5f 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 68 20  _FkCounter with 
1a13c 50 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 74 65  P2==0 (immediate
1a13d 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
1a13e 73 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54  straint).**.** T
1a13f 68 65 6e 20 63 68 65 63 6b 20 74 68 61 74 20 74  hen check that t
1a140 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 72 73  he value of Pars
1a141 65 2e 6d 61 79 41 62 6f 72 74 20 69 73 20 74 72  e.mayAbort is tr
1a142 75 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52  ue if an.** ABOR
1a143 54 20 6d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c  T may be thrown,
1a144 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77   or false otherw
1a145 69 73 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65  ise. Return true
1a146 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d   if it does.** m
1a147 61 74 63 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f  atch, or false o
1a148 74 68 65 72 77 69 73 65 2e 20 54 68 69 73 20 66  therwise. This f
1a149 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e  unction is inten
1a14a 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 61  ded to be used a
1a14b 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 20  s.** part of an 
1a14c 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74  assert statement
1a14d 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   in the compiler
1a14e 2e 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a  . Similar to:.**
1a14f 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 71  .**   assert( sq
1a150 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d  lite3VdbeAssertM
1a151 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 2d 3e  ayAbort(pParse->
1a152 70 56 64 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d  pVdbe, pParse->m
1a153 61 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53  ayAbort) );.*/.S
1a154 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a155 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73  t sqlite3VdbeAss
1a156 65 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65  ertMayAbort(Vdbe
1a157 20 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72   *v, int mayAbor
1a158 74 29 7b 0a 20 20 69 6e 74 20 68 61 73 41 62 6f  t){.  int hasAbo
1a159 72 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f  rt = 0;.  Op *pO
1a15a 70 3b 0a 20 20 56 64 62 65 4f 70 49 74 65 72 20  p;.  VdbeOpIter 
1a15b 73 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28  sIter;.  memset(
1a15c 26 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f  &sIter, 0, sizeo
1a15d 66 28 73 49 74 65 72 29 29 3b 0a 20 20 73 49 74  f(sIter));.  sIt
1a15e 65 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69  er.v = v;..  whi
1a15f 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65  le( (pOp = opIte
1a160 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 21 3d  rNext(&sIter))!=
1a161 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63  0 ){.    int opc
1a162 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64  ode = pOp->opcod
1a163 65 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64  e;.    if( opcod
1a164 65 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c  e==OP_Destroy ||
1a165 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64   opcode==OP_VUpd
1a166 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f  ate || opcode==O
1a167 50 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64  P_VRename .#ifnd
1a168 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
1a169 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20  OREIGN_KEY.     
1a16a 7c 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46  || (opcode==OP_F
1a16b 6b 43 6f 75 6e 74 65 72 20 26 26 20 70 4f 70 2d  kCounter && pOp-
1a16c 3e 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1==0 && pOp->p
1a16d 32 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a 20 20  2==1) .#endif.  
1a16e 20 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d     || ((opcode==
1a16f 4f 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64  OP_Halt || opcod
1a170 65 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c  e==OP_HaltIfNull
1a171 29 20 0a 20 20 20 20 20 20 26 26 20 28 70 4f 70  ) .      && (pOp
1a172 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->p1==SQLITE_CON
1a173 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e  STRAINT && pOp->
1a174 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20  p2==OE_Abort)). 
1a175 20 20 20 29 7b 0a 20 20 20 20 20 20 68 61 73 41     ){.      hasA
1a176 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  bort = 1;.      
1a177 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1a178 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1a179 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70  (v->db, sIter.ap
1a17a 53 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75  Sub);..  /* Retu
1a17b 72 6e 20 74 72 75 65 20 69 66 20 68 61 73 41 62  rn true if hasAb
1a17c 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f  ort==mayAbort. O
1a17d 72 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61  r if a malloc fa
1a17e 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20  ilure occured.. 
1a17f 20 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61   ** If malloc fa
1a180 69 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 77  iled, then the w
1a181 68 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76  hile() loop abov
1a182 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 69  e may not have i
1a183 74 65 72 61 74 65 64 0a 20 20 2a 2a 20 74 68 72  terated.  ** thr
1a184 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73  ough all opcodes
1a185 20 61 6e 64 20 68 61 73 41 62 6f 72 74 20 6d 61   and hasAbort ma
1a186 79 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65  y be set incorre
1a187 63 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a  ctly. Return.  *
1a188 2a 20 74 72 75 65 20 66 6f 72 20 74 68 69 73 20  * true for this 
1a189 63 61 73 65 20 74 6f 20 70 72 65 76 65 6e 74 20  case to prevent 
1a18a 74 68 65 20 61 73 73 65 72 74 28 29 20 69 6e 20  the assert() in 
1a18b 74 68 65 20 63 61 6c 6c 65 72 73 20 66 72 61 6d  the callers fram
1a18c 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c  e.  ** from fail
1a18d 69 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72  ing.  */.  retur
1a18e 6e 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  n ( v->db->mallo
1a18f 63 46 61 69 6c 65 64 20 7c 7c 20 68 61 73 41 62  cFailed || hasAb
1a190 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b  ort==mayAbort );
1a191 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1a192 49 54 45 5f 44 45 42 55 47 20 2d 20 74 68 65 20  ITE_DEBUG - the 
1a193 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79  sqlite3AssertMay
1a194 41 62 6f 72 74 28 29 20 66 75 6e 63 74 69 6f 6e  Abort() function
1a195 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20   */../*.** Loop 
1a196 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67  through the prog
1a197 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ram looking for 
1a198 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61  P2 values that a
1a199 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f  re negative.** o
1a19a 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  n jump instructi
1a19b 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20  ons.  Each such 
1a19c 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c  value is a label
1a19d 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a  .  Resolve the.*
1a19e 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69  * label by setti
1a19f 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20  ng the P2 value 
1a1a0 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e  to its correct n
1a1a1 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a  on-zero value..*
1a1a2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1a1a3 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  e is called once
1a1a4 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64   after all opcod
1a1a5 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73  es have been ins
1a1a6 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72  erted..**.** Var
1a1a7 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41  iable *pMaxFuncA
1a1a8 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68  rgs is set to th
1a1a9 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
1a1aa 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65  of any P2 argume
1a1ab 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f  nt .** to an OP_
1a1ac 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67  Function, OP_Agg
1a1ad 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74  Step or OP_VFilt
1a1ae 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20  er opcode. This 
1a1af 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73  is used by .** s
1a1b0 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
1a1b1 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68  ady() to size th
1a1b2 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61  e Vdbe.apArg[] a
1a1b3 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rray..**.** The 
1a1b4 4f 70 2e 6f 70 66 6c 61 67 73 20 66 69 65 6c 64  Op.opflags field
1a1b5 20 69 73 20 73 65 74 20 6f 6e 20 61 6c 6c 20 6f   is set on all o
1a1b6 70 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  pcodes..*/.stati
1a1b7 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32  c void resolveP2
1a1b8 56 61 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20  Values(Vdbe *p, 
1a1b9 69 6e 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67  int *pMaxFuncArg
1a1ba 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  s){.  int i;.  i
1a1bb 6e 74 20 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70  nt nMaxArgs = *p
1a1bc 4d 61 78 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f  MaxFuncArgs;.  O
1a1bd 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61  p *pOp;.  int *a
1a1be 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65  Label = p->aLabe
1a1bf 6c 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79  l;.  p->readOnly
1a1c0 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d   = 1;.  for(pOp=
1a1c1 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70  p->aOp, i=p->nOp
1a1c2 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  -1; i>=0; i--, p
1a1c3 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70  Op++){.    u8 op
1a1c4 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f  code = pOp->opco
1a1c5 64 65 3b 0a 0a 20 20 20 20 70 4f 70 2d 3e 6f 70  de;..    pOp->op
1a1c6 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 4f  flags = sqlite3O
1a1c7 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70  pcodeProperty[op
1a1c8 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 20 6f  code];.    if( o
1a1c9 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69  pcode==OP_Functi
1a1ca 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50  on || opcode==OP
1a1cb 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20  _AggStep ){.    
1a1cc 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d    if( pOp->p5>nM
1a1cd 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67  axArgs ) nMaxArg
1a1ce 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 20  s = pOp->p5;.   
1a1cf 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64   }else if( opcod
1a1d0 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  e==OP_Transactio
1a1d1 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30 20  n && pOp->p2!=0 
1a1d2 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 64  ){.      p->read
1a1d3 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65  Only = 0;.#ifnde
1a1d4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1a1d5 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d  RTUALTABLE.    }
1a1d6 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d  else if( opcode=
1a1d7 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20  =OP_VUpdate ){. 
1a1d8 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32       if( pOp->p2
1a1d9 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78  >nMaxArgs ) nMax
1a1da 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  Args = pOp->p2;.
1a1db 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
1a1dc 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72  code==OP_VFilter
1a1dd 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b   ){.      int n;
1a1de 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a1df 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29  ->nOp - i >= 3 )
1a1e0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a1e1 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
1a1e2 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20  OP_Integer );.  
1a1e3 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e      n = pOp[-1].
1a1e4 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  p1;.      if( n>
1a1e5 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41  nMaxArgs ) nMaxA
1a1e6 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a  rgs = n;.#endif.
1a1e7 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 28      }..    if( (
1a1e8 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f  pOp->opflags & O
1a1e9 50 46 4c 47 5f 4a 55 4d 50 29 21 3d 30 20 26 26  PFLG_JUMP)!=0 &&
1a1ea 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20   pOp->p2<0 ){.  
1a1eb 20 20 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70      assert( -1-p
1a1ec 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c  Op->p2<p->nLabel
1a1ed 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70   );.      pOp->p
1a1ee 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f  2 = aLabel[-1-pO
1a1ef 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20  p->p2];.    }.  
1a1f0 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
1a1f1 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62  e(p->db, p->aLab
1a1f2 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c  el);.  p->aLabel
1a1f3 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75   = 0;..  *pMaxFu
1a1f4 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67  ncArgs = nMaxArg
1a1f5 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  s;.}../*.** Retu
1a1f6 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  rn the address o
1a1f7 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  f the next instr
1a1f8 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73  uction to be ins
1a1f9 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  erted..*/.SQLITE
1a1fa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a1fb 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
1a1fc 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ddr(Vdbe *p){.  
1a1fd 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a1fe 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
1a1ff 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  T );.  return p-
1a200 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  >nOp;.}../*.** T
1a201 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
1a202 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
1a203 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f  o the array of o
1a204 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65  pcodes associate
1a205 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64  d with.** the Vd
1a206 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
1a207 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
1a208 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65   It is the calle
1a209 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  rs responsibilit
1a20a 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20  y.** to arrange 
1a20b 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e 65 64  for the returned
1a20c 20 61 72 72 61 79 20 74 6f 20 62 65 20 65 76 65   array to be eve
1a20d 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73  ntually freed us
1a20e 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65  ing the .** vdbe
1a20f 46 72 65 65 4f 70 41 72 72 61 79 28 29 20 66 75  FreeOpArray() fu
1a210 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65  nction..**.** Be
1a211 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20  fore returning, 
1a212 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20  *pnOp is set to 
1a213 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  the number of en
1a214 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 74  tries in the ret
1a215 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20  urned.** array. 
1a216 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20  Also, *pnMaxArg 
1a217 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61  is set to the la
1a218 72 67 65 72 20 6f 66 20 69 74 73 20 63 75 72 72  rger of its curr
1a219 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a  ent value and .*
1a21a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1a21b 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 56  entries in the V
1a21c 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61  dbe.apArg[] arra
1a21d 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78  y required to ex
1a21e 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65  ecute the .** re
1a21f 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a  turned program..
1a220 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a221 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65  E VdbeOp *sqlite
1a222 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79  3VdbeTakeOpArray
1a223 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70  (Vdbe *p, int *p
1a224 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41  nOp, int *pnMaxA
1a225 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61  rg){.  VdbeOp *a
1a226 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61  Op = p->aOp;.  a
1a227 73 73 65 72 74 28 20 61 4f 70 20 26 26 20 21 70  ssert( aOp && !p
1a228 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a229 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  ed );..  /* Chec
1a22a 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33 56 64  k that sqlite3Vd
1a22b 62 65 55 73 65 73 42 74 72 65 65 28 29 20 77 61  beUsesBtree() wa
1a22c 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20  s not called on 
1a22d 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73  this VM */.  ass
1a22e 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e  ert( p->aMutex.n
1a22f 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72  Mutex==0 );..  r
1a230 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70  esolveP2Values(p
1a231 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a  , pnMaxArg);.  *
1a232 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20  pnOp = p->nOp;. 
1a233 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72   p->aOp = 0;.  r
1a234 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a  eturn aOp;.}../*
1a235 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20  .** Add a whole 
1a236 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f  list of operatio
1a237 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74  ns to the operat
1a238 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75  ion stack.  Retu
1a239 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73  rn the.** addres
1a23a 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f  s of the first o
1a23b 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a  peration added..
1a23c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a23d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1a23e 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20  eAddOpList(Vdbe 
1a23f 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62  *p, int nOp, Vdb
1a240 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61  eOpList const *a
1a241 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b  Op){.  int addr;
1a242 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
1a243 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1a244 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d  INIT );.  if( p-
1a245 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e  >nOp + nOp > p->
1a246 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77  nOpAlloc && grow
1a247 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20  OpArray(p) ){.  
1a248 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1a249 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b    addr = p->nOp;
1a24a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f  .  if( ALWAYS(nO
1a24b 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  p>0) ){.    int 
1a24c 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73  i;.    VdbeOpLis
1a24d 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61  t const *pIn = a
1a24e 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  Op;.    for(i=0;
1a24f 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e   i<nOp; i++, pIn
1a250 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  ++){.      int p
1a251 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20  2 = pIn->p2;.   
1a252 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20     VdbeOp *pOut 
1a253 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72  = &p->aOp[i+addr
1a254 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f  ];.      pOut->o
1a255 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63  pcode = pIn->opc
1a256 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  ode;.      pOut-
1a257 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20  >p1 = pIn->p1;. 
1a258 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26       if( p2<0 &&
1a259 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50   (sqlite3OpcodeP
1a25a 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70  roperty[pOut->op
1a25b 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55  code] & OPFLG_JU
1a25c 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  MP)!=0 ){.      
1a25d 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64    pOut->p2 = add
1a25e 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20  r + ADDR(p2);.  
1a25f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a260 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32     pOut->p2 = p2
1a261 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a262 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e  pOut->p3 = pIn->
1a263 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  p3;.      pOut->
1a264 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55  p4type = P4_NOTU
1a265 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  SED;.      pOut-
1a266 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20  >p4.p = 0;.     
1a267 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23   pOut->p5 = 0;.#
1a268 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1a269 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a  UG.      pOut->z
1a26a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20  Comment = 0;.   
1a26b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
1a26c 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a  beAddopTrace ){.
1a26d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a26e 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b  dbePrintOp(0, i+
1a26f 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b  addr, &p->aOp[i+
1a270 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  addr]);.      }.
1a271 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20  #endif.    }.   
1a272 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a   p->nOp += nOp;.
1a273 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64    }.  return add
1a274 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  r;.}../*.** Chan
1a275 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
1a276 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66  the P1 operand f
1a277 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
1a278 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
1a279 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
1a27a 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67  eful when a larg
1a27b 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61  e program is loa
1a27c 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74  ded from a.** st
1a27d 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67  atic array using
1a27e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1a27f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e  pList but we wan
1a280 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66  t to make a.** f
1a281 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73  ew minor changes
1a282 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   to the program.
1a283 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a284 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a285 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65  dbeChangeP1(Vdbe
1a286 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69   *p, int addr, i
1a287 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72  nt val){.  asser
1a288 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( p!=0 );.  ass
1a289 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a  ert( addr>=0 );.
1a28a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64    if( p->nOp>add
1a28b 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  r ){.    p->aOp[
1a28c 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a  addr].p1 = val;.
1a28d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
1a28e 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
1a28f 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20   the P2 operand 
1a290 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69  for a specific i
1a291 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
1a292 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
1a293 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e  seful for settin
1a294 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  g a jump destina
1a295 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
1a296 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a297 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1a298 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
1a299 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20  dr, int val){.  
1a29a 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
1a29b 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d    assert( addr>=
1a29c 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f  0 );.  if( p->nO
1a29d 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d  p>addr ){.    p-
1a29e 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20  >aOp[addr].p2 = 
1a29f 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  val;.  }.}../*.*
1a2a0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
1a2a1 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65  ue of the P3 ope
1a2a2 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69  rand for a speci
1a2a3 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  fic instruction.
1a2a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a2a5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a2a6 64 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65  dbeChangeP3(Vdbe
1a2a7 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69   *p, int addr, i
1a2a8 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72  nt val){.  asser
1a2a9 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( p!=0 );.  ass
1a2aa 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a  ert( addr>=0 );.
1a2ab 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64    if( p->nOp>add
1a2ac 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  r ){.    p->aOp[
1a2ad 61 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a  addr].p3 = val;.
1a2ae 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
1a2af 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
1a2b0 20 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20   the P5 operand 
1a2b1 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63  for the most rec
1a2b2 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f  ently.** added o
1a2b3 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  peration..*/.SQL
1a2b4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a2b5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a2b6 67 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38  geP5(Vdbe *p, u8
1a2b7 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28   val){.  assert(
1a2b8 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70   p!=0 );.  if( p
1a2b9 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73  ->aOp ){.    ass
1a2ba 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b  ert( p->nOp>0 );
1a2bb 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  .    p->aOp[p->n
1a2bc 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a  Op-1].p5 = val;.
1a2bd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
1a2be 6e 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61  nge the P2 opera
1a2bf 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  nd of instructio
1a2c0 6e 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69  n addr so that i
1a2c1 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74  t points to.** t
1a2c2 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  he address of th
1a2c3 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1a2c4 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a  on to be coded..
1a2c5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a2c6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a2c7 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20  beJumpHere(Vdbe 
1a2c8 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20  *p, int addr){. 
1a2c9 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a2ca 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d  geP2(p, addr, p-
1a2cb 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  >nOp);.}.../*.**
1a2cc 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75   If the input Fu
1a2cd 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20  ncDef structure 
1a2ce 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68  is ephemeral, th
1a2cf 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a  en free it.  If.
1a2d0 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69  ** the FuncDef i
1a2d1 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20  s not ephermal, 
1a2d2 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  then do nothing.
1a2d3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a2d4 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e  freeEphemeralFun
1a2d5 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64  ction(sqlite3 *d
1a2d6 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  b, FuncDef *pDef
1a2d7 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ){.  if( ALWAYS(
1a2d8 70 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e  pDef) && (pDef->
1a2d9 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
1a2da 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b  UNC_EPHEM)!=0 ){
1a2db 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1a2dc 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20  ee(db, pDef);.  
1a2dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  }.}../*.** Delet
1a2de 65 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20  e a P4 value if 
1a2df 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74  necessary..*/.st
1a2e0 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34  atic void freeP4
1a2e1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
1a2e2 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a  t p4type, void *
1a2e3 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b  p4){.  if( p4 ){
1a2e4 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74  .    switch( p4t
1a2e5 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
1a2e6 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20  e P4_REAL:.     
1a2e7 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a   case P4_INT64:.
1a2e8 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50        case P4_MP
1a2e9 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73  RINTF:.      cas
1a2ea 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20  e P4_DYNAMIC:.  
1a2eb 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49      case P4_KEYI
1a2ec 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20  NFO:.      case 
1a2ed 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20  P4_INTARRAY:.   
1a2ee 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
1a2ef 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20  FO_HANDOFF: {.  
1a2f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a2f1 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20  ree(db, p4);.   
1a2f2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a2f3 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50    }.      case P
1a2f4 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20  4_VDBEFUNC: {.  
1a2f5 20 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a        VdbeFunc *
1a2f6 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62  pVdbeFunc = (Vdb
1a2f7 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20  eFunc *)p4;.    
1a2f8 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61      freeEphemera
1a2f9 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56  lFunction(db, pV
1a2fa 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b  dbeFunc->pFunc);
1a2fb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a2fc 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74  VdbeDeleteAuxDat
1a2fd 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b  a(pVdbeFunc, 0);
1a2fe 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a2ff 44 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65  DbFree(db, pVdbe
1a300 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62  Func);.        b
1a301 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1a302 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43      case P4_FUNC
1a303 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66  DEF: {.        f
1a304 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63  reeEphemeralFunc
1a305 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65  tion(db, (FuncDe
1a306 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20  f*)p4);.        
1a307 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a308 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d       case P4_MEM
1a309 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
1a30a 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71  te3ValueFree((sq
1a30b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29  lite3_value*)p4)
1a30c 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1a30d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
1a30e 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a  ase P4_VTAB : {.
1a30f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a310 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c  tabUnlock((VTabl
1a311 65 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20  e *)p4);.       
1a312 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1a313 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55        case P4_SU
1a314 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20  BPROGRAM : {.   
1a315 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a316 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62  ProgramDelete(db
1a317 2c 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29  , (SubProgram *)
1a318 70 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  p4, 1);.        
1a319 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a31a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1a31b 2a 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65  * Free the space
1a31c 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
1a31d 4f 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61  Op and any p4 va
1a31e 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lues allocated f
1a31f 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65  or the.** opcode
1a320 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  s contained with
1a321 69 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f  in. If aOp is no
1a322 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73  t NULL it is ass
1a323 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  umed to contain 
1a324 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e  .** nOp entries.
1a325 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
1a326 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
1a327 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70  (sqlite3 *db, Op
1a328 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b   *aOp, int nOp){
1a329 0a 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20  .  if( aOp ){.  
1a32a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66    Op *pOp;.    f
1a32b 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c  or(pOp=aOp; pOp<
1a32c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b  &aOp[nOp]; pOp++
1a32d 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28  ){.      freeP4(
1a32e 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c  db, pOp->p4type,
1a32f 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66   pOp->p4.p);.#if
1a330 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1a331 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1a332 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43  Free(db, pOp->zC
1a333 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20  omment);.#endif 
1a334 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20      .    }.  }. 
1a335 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1a336 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  b, aOp);.}../*.*
1a337 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
1a338 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65  ref-count on the
1a339 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75   SubProgram stru
1a33a 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20  cture passed as 
1a33b 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72  the.** second ar
1a33c 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72  gument. If the r
1a33d 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73  ef-count reaches
1a33e 20 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20   zero, free the 
1a33f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
1a340 20 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44   The array of VD
1a341 42 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65  BE opcodes store
1a342 64 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e  d as SubProgram.
1a343 61 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a  aOp is freed if.
1a344 2a 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65  ** either the re
1a345 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  f-count reaches 
1a346 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65  zero or paramete
1a347 72 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d  r freeop is non-
1a348 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63  zero..**.** Sinc
1a349 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f  e the array of o
1a34a 70 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74  pcodes pointed t
1a34b 6f 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e  o by SubProgram.
1a34c 61 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79  aOp may directly
1a34d 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c  .** or indirectl
1a34e 79 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65  y contain a refe
1a34f 72 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62  rence to the Sub
1a350 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72  Program structur
1a351 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20  e itself..** By 
1a352 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65  passing a non-ze
1a353 72 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65  ro freeop parame
1a354 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  ter, the caller 
1a355 6d 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20  may ensure that 
1a356 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61  all.** SubProgra
1a357 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64  m structures and
1a358 20 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79   their aOp array
1a359 73 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65  s are freed, eve
1a35a 6e 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20  n when there.** 
1a35b 61 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61  are such circula
1a35c 72 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f  r references..*/
1a35d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a35e 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a35f 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71  ProgramDelete(sq
1a360 6c 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72  lite3 *db, SubPr
1a361 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72  ogram *p, int fr
1a362 65 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29  eeop){.  if( p )
1a363 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
1a364 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69  >nRef>0 );.    i
1a365 66 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e  f( freeop || p->
1a366 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20  nRef==1 ){.     
1a367 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f   Op *aOp = p->aO
1a368 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20  p;.      p->aOp 
1a369 3d 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46  = 0;.      vdbeF
1a36a 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61  reeOpArray(db, a
1a36b 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20  Op, p->nOp);.   
1a36c 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20     p->nOp = 0;. 
1a36d 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66     }.    p->nRef
1a36e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  --;.    if( p->n
1a36f 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Ref==0 ){.      
1a370 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1a371 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , p);.    }.  }.
1a372 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  }.../*.** Change
1a373 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74   N opcodes start
1a374 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e  ing at addr to N
1a375 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  o-ops..*/.SQLITE
1a376 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a377 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
1a378 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69  oNoop(Vdbe *p, i
1a379 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b  nt addr, int N){
1a37a 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b  .  if( p->aOp ){
1a37b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70  .    VdbeOp *pOp
1a37c 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d   = &p->aOp[addr]
1a37d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
1a37e 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77  b = p->db;.    w
1a37f 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20  hile( N-- ){.   
1a380 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f     freeP4(db, pO
1a381 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e  p->p4type, pOp->
1a382 70 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d  p4.p);.      mem
1a383 73 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65  set(pOp, 0, size
1a384 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20  of(pOp[0]));.   
1a385 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d     pOp->opcode =
1a386 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20   OP_Noop;.      
1a387 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  pOp++;.    }.  }
1a388 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1a389 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1a38a 65 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72  e P4 operand for
1a38b 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74   a specific inst
1a38c 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  ruction..** This
1a38d 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66   routine is usef
1a38e 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20  ul when a large 
1a38f 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65  program is loade
1a390 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74  d from a.** stat
1a391 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73  ic array using s
1a392 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
1a393 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20  ist but we want 
1a394 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77  to make a.** few
1a395 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74   minor changes t
1a396 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a  o the program..*
1a397 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65  *.** If n>=0 the
1a398 6e 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64  n the P4 operand
1a399 20 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61   is dynamic, mea
1a39a 6e 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79  ning that a copy
1a39b 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e   of.** the strin
1a39c 67 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d  g is made into m
1a39d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
1a39e 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
1a39f 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65  oc()..** A value
1a3a0 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63   of n==0 means c
1a3a1 6f 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34  opy bytes of zP4
1a3a2 20 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75   up to and inclu
1a3a3 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73  ding the.** firs
1a3a4 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66  t null byte.  If
1a3a5 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e   n>0 then copy n
1a3a6 2b 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e  +1 bytes of zP4.
1a3a7 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f  .**.** If n==P4_
1a3a8 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73  KEYINFO it means
1a3a9 20 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70   that zP4 is a p
1a3aa 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49  ointer to a KeyI
1a3ab 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  nfo structure..*
1a3ac 2a 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65  * A copy is made
1a3ad 20 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20   of the KeyInfo 
1a3ae 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d  structure into m
1a3af 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
1a3b0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  rom.** sqlite3_m
1a3b1 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65  alloc, to be fre
1a3b2 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65  ed when the Vdbe
1a3b3 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a   is finalized..*
1a3b4 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f  * n==P4_KEYINFO_
1a3b5 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65  HANDOFF indicate
1a3b6 73 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74  s that zP4 point
1a3b7 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73  s to a KeyInfo s
1a3b8 74 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72  tructure.** stor
1a3b9 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61  ed in memory tha
1a3ba 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73  t the caller has
1a3bb 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
1a3bc 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54  qlite3_malloc. T
1a3bd 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68  he .** caller sh
1a3be 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68  ould not free th
1a3bf 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74  e allocation, it
1a3c0 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77   will be freed w
1a3c1 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a  hen the Vdbe is.
1a3c2 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  ** finalized..**
1a3c3 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65   .** Other value
1a3c4 73 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49  s of n (P4_STATI
1a3c5 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74  C, P4_COLLSEQ et
1a3c6 63 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61  c.) indicate tha
1a3c7 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20  t zP4 points.** 
1a3c8 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73  to a string or s
1a3c9 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73  tructure that is
1a3ca 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65   guaranteed to e
1a3cb 78 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66  xist for the lif
1a3cc 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20  etime of.** the 
1a3cd 56 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63  Vdbe. In these c
1a3ce 61 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74  ases we can just
1a3cf 20 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65   copy the pointe
1a3d0 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72  r..**.** If addr
1a3d1 3c 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50  <0 then change P
1a3d2 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  4 on the most re
1a3d3 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20  cently inserted 
1a3d4 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1a3d5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a3d6 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
1a3d7 68 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c  hangeP4(Vdbe *p,
1a3d8 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74   int addr, const
1a3d9 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20   char *zP4, int 
1a3da 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20  n){.  Op *pOp;. 
1a3db 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
1a3dc 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
1a3dd 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20    db = p->db;.  
1a3de 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a3df 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
1a3e0 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f  T );.  if( p->aO
1a3e1 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  p==0 || db->mall
1a3e2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1a3e3 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e  if ( n!=P4_KEYIN
1a3e4 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42  FO && n!=P4_VTAB
1a3e5 20 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50   ) {.      freeP
1a3e6 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29  4(db, n, (void*)
1a3e7 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a  *(char**)&zP4);.
1a3e8 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1a3e9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1a3ea 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73  p->nOp>0 );.  as
1a3eb 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f  sert( addr<p->nO
1a3ec 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c  p );.  if( addr<
1a3ed 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20  0 ){.    addr = 
1a3ee 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a  p->nOp - 1;.  }.
1a3ef 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
1a3f0 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28  addr];.  freeP4(
1a3f1 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c  db, pOp->p4type,
1a3f2 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70   pOp->p4.p);.  p
1a3f3 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20  Op->p4.p = 0;.  
1a3f4 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20  if( n==P4_INT32 
1a3f5 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20  ){.    /* Note: 
1a3f6 74 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66  this cast is saf
1a3f7 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f  e, because the o
1a3f8 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74  rigin data point
1a3f9 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20   was an int.    
1a3fa 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74  ** that was cast
1a3fb 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61   to a (const cha
1a3fc 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70  r *). */.    pOp
1a3fd 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f  ->p4.i = SQLITE_
1a3fe 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b  PTR_TO_INT(zP4);
1a3ff 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
1a400 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d   = P4_INT32;.  }
1a401 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20  else if( zP4==0 
1a402 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  ){.    pOp->p4.p
1a403 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70   = 0;.    pOp->p
1a404 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53  4type = P4_NOTUS
1a405 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ED;.  }else if( 
1a406 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b  n==P4_KEYINFO ){
1a407 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
1a408 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20  eyInfo;.    int 
1a409 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a  nField, nByte;..
1a40a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b      nField = ((K
1a40b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46  eyInfo*)zP4)->nF
1a40c 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20  ield;.    nByte 
1a40d 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e  = sizeof(*pKeyIn
1a40e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29  fo) + (nField-1)
1a40f 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f  *sizeof(pKeyInfo
1a410 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46  ->aColl[0]) + nF
1a411 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e  ield;.    pKeyIn
1a412 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  fo = sqlite3Mall
1a413 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20  oc( nByte );.   
1a414 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66   pOp->p4.pKeyInf
1a415 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  o = pKeyInfo;.  
1a416 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29    if( pKeyInfo )
1a417 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72  {.      u8 *aSor
1a418 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65  tOrder;.      me
1a419 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a  mcpy(pKeyInfo, z
1a41a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  P4, nByte);.    
1a41b 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70    aSortOrder = p
1a41c 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72  KeyInfo->aSortOr
1a41d 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61  der;.      if( a
1a41e 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20  SortOrder ){.   
1a41f 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61       pKeyInfo->a
1a420 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73  SortOrder = (uns
1a421 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65  igned char*)&pKe
1a422 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69  yInfo->aColl[nFi
1a423 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65  eld];.        me
1a424 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61  mcpy(pKeyInfo->a
1a425 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74  SortOrder, aSort
1a426 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a  Order, nField);.
1a427 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f        }.      pO
1a428 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b  p->p4type = P4_K
1a429 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73  EYINFO;.    }els
1a42a 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e  e{.      p->db->
1a42b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1a42c 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74  ;.      pOp->p4t
1a42d 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
1a42e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
1a42f 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46  if( n==P4_KEYINF
1a430 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20  O_HANDOFF ){.   
1a431 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f   pOp->p4.p = (vo
1a432 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70  id*)zP4;.    pOp
1a433 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45  ->p4type = P4_KE
1a434 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69  YINFO;.  }else i
1a435 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b  f( n==P4_VTAB ){
1a436 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d  .    pOp->p4.p =
1a437 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20   (void*)zP4;.   
1a438 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1a439 34 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69  4_VTAB;.    sqli
1a43a 74 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61  te3VtabLock((VTa
1a43b 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20  ble *)zP4);.    
1a43c 61 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65  assert( ((VTable
1a43d 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e   *)zP4)->db==p->
1a43e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  db );.  }else if
1a43f 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70  ( n<0 ){.    pOp
1a440 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29  ->p4.p = (void*)
1a441 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  zP4;.    pOp->p4
1a442 74 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63  type = (signed c
1a443 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  har)n;.  }else{.
1a444 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e      if( n==0 ) n
1a445 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1a446 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70  30(zP4);.    pOp
1a447 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33  ->p4.z = sqlite3
1a448 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c  DbStrNDup(p->db,
1a449 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f   zP4, n);.    pO
1a44a 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44  p->p4type = P4_D
1a44b 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23  YNAMIC;.  }.}..#
1a44c 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
1a44d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63  .** Change the c
1a44e 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68  omment on the th
1a44f 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
1a450 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f  coded instructio
1a451 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74  n.  Or.** insert
1a452 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64   a No-op and add
1a453 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20   the comment to 
1a454 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63  that new instruc
1a455 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d  tion.  This.** m
1a456 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61  akes the code ea
1a457 73 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72  sier to read dur
1a458 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20  ing debugging.  
1a459 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70  None of this hap
1a45a 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f  pens.** in a pro
1a45b 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a  duction build..*
1a45c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a45d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a45e 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70  eComment(Vdbe *p
1a45f 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
1a460 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
1a461 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28  a_list ap;.  if(
1a462 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20   !p ) return;.  
1a463 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
1a464 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b   || p->aOp==0 );
1a465 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f  .  assert( p->aO
1a466 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70  p==0 || p->aOp[p
1a467 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e  ->nOp-1].zCommen
1a468 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d  t==0 || p->db->m
1a469 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
1a46a 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20   if( p->nOp ){. 
1a46b 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26     char **pz = &
1a46c 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d  p->aOp[p->nOp-1]
1a46d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76  .zComment;.    v
1a46e 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
1a46f 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mat);.    sqlite
1a470 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a  3DbFree(p->db, *
1a471 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73  pz);.    *pz = s
1a472 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70  qlite3VMPrintf(p
1a473 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  ->db, zFormat, a
1a474 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61  p);.    va_end(a
1a475 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45  p);.  }.}.SQLITE
1a476 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a477 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d  lite3VdbeNoopCom
1a478 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f  ment(Vdbe *p, co
1a479 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
1a47a 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
1a47b 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20  st ap;.  if( !p 
1a47c 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
1a47d 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c  te3VdbeAddOp0(p,
1a47e 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73   OP_Noop);.  ass
1a47f 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c  ert( p->nOp>0 ||
1a480 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20   p->aOp==0 );.  
1a481 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d  assert( p->aOp==
1a482 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  0 || p->aOp[p->n
1a483 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d  Op-1].zComment==
1a484 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c  0 || p->db->mall
1a485 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
1a486 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20  ( p->nOp ){.    
1a487 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e  char **pz = &p->
1a488 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43  aOp[p->nOp-1].zC
1a489 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73  omment;.    va_s
1a48a 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
1a48b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1a48c 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29  Free(p->db, *pz)
1a48d 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69  ;.    *pz = sqli
1a48e 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64  te3VMPrintf(p->d
1a48f 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
1a490 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  .    va_end(ap);
1a491 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f  .  }.}.#endif  /
1a492 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a  * NDEBUG */../*.
1a493 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70  ** Return the op
1a494 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e  code for a given
1a495 20 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68   address.  If th
1a496 65 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c  e address is -1,
1a497 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20   then.** return 
1a498 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
1a499 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64  y inserted opcod
1a49a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  e..**.** If a me
1a49b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1a49c 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
1a49d 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ed prior to the 
1a49e 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a  calling of this.
1a49f 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e  ** routine, then
1a4a0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1a4a1 64 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c  dummy VdbeOp wil
1a4a2 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20  l be returned.  
1a4a3 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69  That opcode.** i
1a4a4 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  s readable and w
1a4a5 72 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20  ritable, but it 
1a4a6 68 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20  has no effect.  
1a4a7 54 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20  The return of a 
1a4a8 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20  dummy.** opcode 
1a4a9 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20  allows the call 
1a4aa 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63  to continue func
1a4ab 74 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20  tioning after a 
1a4ac 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75  OOM fault withou
1a4ad 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63  t.** having to c
1a4ae 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1a4af 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74  he return from t
1a4b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1a4b1 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a   valid pointer..
1a4b2 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20  **.** About the 
1a4b3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1a4b4 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61  IT_TRACE:  Norma
1a4b5 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  lly, this routin
1a4b6 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
1a4b7 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e  d.** unless p->n
1a4b8 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62  Op>0.  This is b
1a4b9 65 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62  ecause in the ab
1a4ba 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f  sense of SQLITE_
1a4bb 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61  OMIT_TRACE,.** a
1a4bc 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72  n OP_Trace instr
1a4bd 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73  uction is always
1a4be 20 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c   inserted by sql
1a4bf 69 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73  ite3VdbeGet() as
1a4c0 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65   soon as.** a ne
1a4c1 77 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65  w VDBE is create
1a4c2 64 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72  d.  So we are fr
1a4c3 65 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74  ee to set addr t
1a4c4 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f  o p->nOp-1 witho
1a4c5 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20  ut.** having to 
1a4c6 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20  double-check to 
1a4c7 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
1a4c8 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e  he result is non
1a4c9 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a  -negative. But.*
1a4ca 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * if SQLITE_OMIT
1a4cb 5f 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65  _TRACE is define
1a4cc 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20  d, the OP_Trace 
1a4cd 69 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77  is omitted and w
1a4ce 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20  e do need to.** 
1a4cf 63 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20  check the value 
1a4d0 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f  of p->nOp-1 befo
1a4d1 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a  re continuing..*
1a4d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a4d3 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
1a4d4 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a  VdbeGetOp(Vdbe *
1a4d5 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20  p, int addr){.  
1a4d6 73 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75  static VdbeOp du
1a4d7 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70  mmy;.  assert( p
1a4d8 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1a4d9 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
1a4da 28 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64  ( addr<0 ){.#ifd
1a4db 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1a4dc 52 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e  RACE.    if( p->
1a4dd 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  nOp==0 ) return 
1a4de 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20  &dummy;.#endif. 
1a4df 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70     addr = p->nOp
1a4e0 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65   - 1;.  }.  asse
1a4e1 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20  rt( (addr>=0 && 
1a4e2 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20  addr<p->nOp) || 
1a4e3 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
1a4e4 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  led );.  if( p->
1a4e5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a4e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   ){.    return &
1a4e7 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a  dummy;.  }else{.
1a4e8 20 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61      return &p->a
1a4e9 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a  Op[addr];.  }.}.
1a4ea 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1a4eb 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
1a4ec 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e  N) || !defined(N
1a4ed 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c  DEBUG) \.     ||
1a4ee 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52   defined(VDBE_PR
1a4ef 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65  OFILE) || define
1a4f0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
1a4f1 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20  /*.** Compute a 
1a4f2 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63  string that desc
1a4f3 72 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72  ribes the P4 par
1a4f4 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70  ameter for an op
1a4f5 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65  code..** Use zTe
1a4f6 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69  mp for any requi
1a4f7 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75  red temporary bu
1a4f8 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73  ffer space..*/.s
1a4f9 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70  tatic char *disp
1a4fa 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63  layP4(Op *pOp, c
1a4fb 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20  har *zTemp, int 
1a4fc 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a  nTemp){.  char *
1a4fd 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61  zP4 = zTemp;.  a
1a4fe 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30  ssert( nTemp>=20
1a4ff 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f   );.  switch( pO
1a500 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20  p->p4type ){.   
1a501 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f   case P4_KEYINFO
1a502 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73  _STATIC:.    cas
1a503 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a  e P4_KEYINFO: {.
1a504 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a        int i, j;.
1a505 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
1a506 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
1a507 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  4.pKeyInfo;.    
1a508 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a509 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
1a50a 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70   "keyinfo(%d", p
1a50b 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29  KeyInfo->nField)
1a50c 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69  ;.      i = sqli
1a50d 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d  te3Strlen30(zTem
1a50e 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  p);.      for(j=
1a50f 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e  0; j<pKeyInfo->n
1a510 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  Field; j++){.   
1a511 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
1a512 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  oll = pKeyInfo->
1a513 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20  aColl[j];.      
1a514 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
1a515 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d           int n =
1a516 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a517 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a  (pColl->zName);.
1a518 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b            if( i+
1a519 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20  n>nTemp-6 ){.   
1a51a 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
1a51b 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22  &zTemp[i],",..."
1a51c 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ,4);.           
1a51d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1a51e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54    }.          zT
1a51f 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a  emp[i++] = ',';.
1a520 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b            if( pK
1a521 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
1a522 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e  er && pKeyInfo->
1a523 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b  aSortOrder[j] ){
1a524 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65  .            zTe
1a525 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20  mp[i++] = '-';. 
1a526 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1a527 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65       memcpy(&zTe
1a528 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e  mp[i], pColl->zN
1a529 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20  ame,n+1);.      
1a52a 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20      i += n;.    
1a52b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b      }else if( i+
1a52c 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20  4<nTemp-6 ){.   
1a52d 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a         memcpy(&z
1a52e 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34  Temp[i],",nil",4
1a52f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b  );.          i +
1a530 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 4;.        }. 
1a531 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65       }.      zTe
1a532 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20  mp[i++] = ')';. 
1a533 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20       zTemp[i] = 
1a534 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  0;.      assert(
1a535 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20   i<nTemp );.    
1a536 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a537 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53     case P4_COLLS
1a538 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  EQ: {.      Coll
1a539 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70  Seq *pColl = pOp
1a53a 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20  ->p4.pColl;.    
1a53b 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a53c 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
1a53d 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29   "collseq(%.20s)
1a53e 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  ", pColl->zName)
1a53f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a540 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
1a541 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20  _FUNCDEF: {.    
1a542 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20    FuncDef *pDef 
1a543 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
1a544 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1a545 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
1a546 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20  Temp, "%s(%d)", 
1a547 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65  pDef->zName, pDe
1a548 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20  f->nArg);.      
1a549 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1a54a 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20   case P4_INT64: 
1a54b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1a54c 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1a54d 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a  zTemp, "%lld", *
1a54e 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20  pOp->p4.pI64);. 
1a54f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a550 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e  }.    case P4_IN
1a551 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  T32: {.      sql
1a552 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a553 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22  emp, zTemp, "%d"
1a554 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20  , pOp->p4.i);.  
1a555 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a556 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41  .    case P4_REA
1a557 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
1a558 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a559 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67  p, zTemp, "%.16g
1a55a 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61  ", *pOp->p4.pRea
1a55b 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  l);.      break;
1a55c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a55d 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20  P4_MEM: {.      
1a55e 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d  Mem *pMem = pOp-
1a55f 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20  >p4.pMem;.      
1a560 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
1a561 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1a562 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==0 );.      if(
1a563 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
1a564 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20  EM_Str ){.      
1a565 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b    zP4 = pMem->z;
1a566 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1a567 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
1a568 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20  EM_Int ){.      
1a569 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a56a 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
1a56b 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
1a56c 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  .i);.      }else
1a56d 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
1a56e 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
1a56f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
1a570 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
1a571 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70  Temp, "%.16g", p
1a572 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d  Mem->r);.      }
1a573 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
1a574 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
1a575 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a  s & MEM_Blob );.
1a576 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28          zP4 = "(
1a577 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a  blob)";.      }.
1a578 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a579 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1a57a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1a57b 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f  BLE.    case P4_
1a57c 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71  VTAB: {.      sq
1a57d 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1a57e 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
1a57f 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20  b->pVtab;.      
1a580 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a581 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1a582 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74  vtab:%p:%p", pVt
1a583 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  ab, pVtab->pModu
1a584 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  le);.      break
1a585 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1a586 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52     case P4_INTAR
1a587 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  RAY: {.      sql
1a588 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a589 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74  emp, zTemp, "int
1a58a 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62  array");.      b
1a58b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a58c 63 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52  case P4_SUBPROGR
1a58d 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AM: {.      sqli
1a58e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a58f 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67  mp, zTemp, "prog
1a590 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65  ram");.      bre
1a591 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
1a592 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a  fault: {.      z
1a593 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  P4 = pOp->p4.z;.
1a594 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30        if( zP4==0
1a595 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20   ){.        zP4 
1a596 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20  = zTemp;.       
1a597 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20   zTemp[0] = 0;. 
1a598 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1a599 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d  .  assert( zP4!=
1a59a 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50  0 );.  return zP
1a59b 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  4;.}.#endif../*.
1a59c 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68  ** Declare to th
1a59d 65 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20  e Vdbe that the 
1a59e 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20  BTree object at 
1a59f 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73  db->aDb[i] is us
1a5a0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1a5a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a5a2 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
1a5a3 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b  Vdbe *p, int i){
1a5a4 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61  .  int mask;.  a
1a5a5 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1a5a6 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69  <p->db->nDb && i
1a5a7 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29  <sizeof(u32)*8 )
1a5a8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69  ;.  assert( i<(i
1a5a9 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72  nt)sizeof(p->btr
1a5aa 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d  eeMask)*8 );.  m
1a5ab 61 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c  ask = ((u32)1)<<
1a5ac 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72  i;.  if( (p->btr
1a5ad 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d  eeMask & mask)==
1a5ae 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65  0 ){.    p->btre
1a5af 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20  eMask |= mask;. 
1a5b0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d     sqlite3BtreeM
1a5b1 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28  utexArrayInsert(
1a5b2 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64  &p->aMutex, p->d
1a5b3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a  b->aDb[i].pBt);.
1a5b4 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69    }.}...#if defi
1a5b5 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45  ned(VDBE_PROFILE
1a5b6 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
1a5b7 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
1a5b8 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20   Print a single 
1a5b9 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f  opcode.  This ro
1a5ba 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
1a5bb 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  r debugging only
1a5bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a5bd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a5be 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45  VdbePrintOp(FILE
1a5bf 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20   *pOut, int pc, 
1a5c0 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72  Op *pOp){.  char
1a5c1 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50   *zP4;.  char zP
1a5c2 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63  tr[50];.  static
1a5c3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
1a5c4 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31  rmat1 = "%4d %-1
1a5c5 33 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25  3s %4d %4d %4d %
1a5c6 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a  -4s %.2X %s\n";.
1a5c7 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20    if( pOut==0 ) 
1a5c8 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20  pOut = stdout;. 
1a5c9 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34   zP4 = displayP4
1a5ca 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65  (pOp, zPtr, size
1a5cb 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72  of(zPtr));.  fpr
1a5cc 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d  intf(pOut, zForm
1a5cd 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20  at1, pc, .      
1a5ce 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
1a5cf 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20  e(pOp->opcode), 
1a5d0 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32  pOp->p1, pOp->p2
1a5d1 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20  , pOp->p3, zP4, 
1a5d2 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20  pOp->p5,.#ifdef 
1a5d3 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1a5d4 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74     pOp->zComment
1a5d5 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74   ? pOp->zComment
1a5d6 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20   : "".#else.    
1a5d7 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b    "".#endif.  );
1a5d8 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b  .  fflush(pOut);
1a5d9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1a5da 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61   Release an arra
1a5db 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65  y of N Mem eleme
1a5dc 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nts.*/.static vo
1a5dd 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  id releaseMemArr
1a5de 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e  ay(Mem *p, int N
1a5df 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20  ){.  if( p && N 
1a5e0 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64  ){.    Mem *pEnd
1a5e1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
1a5e2 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75  b = p->db;.    u
1a5e3 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20  8 malloc_failed 
1a5e4 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  = db->mallocFail
1a5e5 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64  ed;.    for(pEnd
1a5e6 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20  =&p[N]; p<pEnd; 
1a5e7 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  p++){.      asse
1a5e8 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e  rt( (&p[1])==pEn
1a5e9 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b  d || p[0].db==p[
1a5ea 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20  1].db );..      
1a5eb 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73  /* This block is
1a5ec 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e   really an inlin
1a5ed 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71  ed version of sq
1a5ee 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1a5ef 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74  ase().      ** t
1a5f0 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74  hat takes advant
1a5f1 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20  age of the fact 
1a5f2 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20  that the memory 
1a5f3 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20  cell value is . 
1a5f4 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65       ** being se
1a5f5 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20  t to NULL after 
1a5f6 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79  releasing any dy
1a5f7 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e  namic resources.
1a5f8 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1a5f9 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61  ** The justifica
1a5fa 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61  tion for duplica
1a5fb 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61  ting code is tha
1a5fc 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a  t according to .
1a5fd 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69        ** callgri
1a5fe 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20  nd, this causes 
1a5ff 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63  a certain test c
1a600 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43  ase to hit the C
1a601 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a  PU 4.7 .      **
1a602 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78   percent less (x
1a603 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65  86 linux, gcc ve
1a604 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36  rsion 4.1.2, -O6
1a605 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20  ) than if .     
1a606 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65   ** sqlite3MemRe
1a607 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c  lease() were cal
1a608 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57  led from here. W
1a609 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75  ith -O2, this ju
1a60a 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  mps.      ** to 
1a60b 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65  6.6 percent. The
1a60c 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e   test case is in
1a60d 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77  serting 1000 row
1a60e 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a  s into a table .
1a60f 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f        ** with no
1a610 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61   indexes using a
1a611 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64   single prepared
1a612 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
1a613 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20  t, bind() .     
1a614 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e   ** and reset().
1a615 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f   Inserts are gro
1a616 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e  uped into a tran
1a617 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  saction..      *
1a618 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66  /.      if( p->f
1a619 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45  lags&(MEM_Agg|ME
1a61a 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c  M_Dyn|MEM_Frame|
1a61b 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20  MEM_RowSet) ){. 
1a61c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a61d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b  beMemRelease(p);
1a61e 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1a61f 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20   p->zMalloc ){. 
1a620 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1a621 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c  Free(db, p->zMal
1a622 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  loc);.        p-
1a623 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  >zMalloc = 0;.  
1a624 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e      }..      p->
1a625 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1a626 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
1a627 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d  mallocFailed = m
1a628 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20  alloc_failed;.  
1a629 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  }.}../*.** Delet
1a62a 65 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62  e a VdbeFrame ob
1a62b 6a 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e  ject and its con
1a62c 74 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65  tents. VdbeFrame
1a62d 20 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20   objects are.** 
1a62e 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65  allocated by the
1a62f 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f   OP_Program opco
1a630 64 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62  de in sqlite3Vdb
1a631 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  eExec()..*/.SQLI
1a632 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a633 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65  sqlite3VdbeFrame
1a634 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65  Delete(VdbeFrame
1a635 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1a636 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62   Mem *aMem = Vdb
1a637 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20  eFrameMem(p);.  
1a638 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43  VdbeCursor **apC
1a639 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72  sr = (VdbeCursor
1a63a 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68   **)&aMem[p->nCh
1a63b 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69  ildMem];.  for(i
1a63c 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43  =0; i<p->nChildC
1a63d 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  sr; i++){.    sq
1a63e 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
1a63f 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b  sor(p->v, apCsr[
1a640 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61  i]);.  }.  relea
1a641 73 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c  seMemArray(aMem,
1a642 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a   p->nChildMem);.
1a643 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a644 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a  p->v->db, p);.}.
1a645 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a646 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a  OMIT_EXPLAIN./*.
1a647 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e  ** Give a listin
1a648 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d  g of the program
1a649 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
1a64a 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  machine..**.** T
1a64b 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  he interface is 
1a64c 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69  the same as sqli
1a64d 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20  te3VdbeExec().  
1a64e 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a  But instead of.*
1a64f 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f  * running the co
1a650 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74  de, it invokes t
1a651 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65  he callback once
1a652 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75   for each instru
1a653 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66  ction..** This f
1a654 65 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74  eature is used t
1a655 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50  o implement "EXP
1a656 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  LAIN"..**.** Whe
1a657 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c  n p->explain==1,
1a658 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
1a659 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68  n is listed.  Wh
1a65a 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e  en.** p->explain
1a65b 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70  ==2, only OP_Exp
1a65c 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  lain instruction
1a65d 73 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64  s are listed and
1a65e 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68   these.** are sh
1a65f 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65  own in a differe
1a660 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65  nt format.  p->e
1a661 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65  xplain==2 is use
1a662 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a  d to implement.*
1a663 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  * EXPLAIN QUERY 
1a664 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  PLAN..*/.SQLITE_
1a665 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1a666 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56  te3VdbeList(.  V
1a667 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20  dbe *p          
1a668 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1a669 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  VDBE */.){.  int
1a66a 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20   nRow;          
1a66b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a66c 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
1a66d 72 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74  r of rows to ret
1a66e 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75  urn */.  int nSu
1a66f 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
1a670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a671 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76   Number of sub-v
1a672 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72  dbes seen so far
1a673 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d   */.  SubProgram
1a674 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20   **apSub = 0;   
1a675 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
1a676 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73  ray of sub-vdbes
1a677 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20   */.  Mem *pSub 
1a678 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  = 0;.  sqlite3 *
1a679 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e  db = p->db;.  in
1a67a 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t i;.  int rc = 
1a67b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d  SQLITE_OK;.  Mem
1a67c 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73   *pMem = p->pRes
1a67d 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65  ultSet = &p->aMe
1a67e 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  m[1];..  assert(
1a67f 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20   p->explain );. 
1a680 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1a681 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1a682 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  N );.  assert( d
1a683 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
1a684 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20  _MAGIC_BUSY );. 
1a685 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d   assert( p->rc==
1a686 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
1a687 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1a688 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  || p->rc==SQLITE
1a689 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20  _NOMEM );..  /* 
1a68a 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73  Even though this
1a68b 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74   opcode does not
1a68c 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72   use dynamic str
1a68d 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68  ings for.  ** th
1a68e 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74  e result, result
1a68f 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63   columns may bec
1a690 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74  ome dynamic if t
1a691 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20  he user calls.  
1a692 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
1a693 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73  n_text16(), caus
1a694 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f  ing a translatio
1a695 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f  n to UTF-16 enco
1a696 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c  ding..  */.  rel
1a697 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65  easeMemArray(pMe
1a698 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d  m, 8);..  if( p-
1a699 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45  >rc==SQLITE_NOME
1a69a 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  M ){.    /* This
1a69b 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61   happens if a ma
1a69c 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20  lloc() inside a 
1a69d 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
1a69e 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72  column_text() or
1a69f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
1a6a0 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
1a6a1 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20  failed.  */.    
1a6a2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a6a3 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
1a6a4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1a6a5 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20   }..  /* Figure 
1a6a6 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  out total number
1a6a7 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69   of rows that wi
1a6a8 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  ll be returned b
1a6a9 79 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50  y this .  ** EXP
1a6aa 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a  LAIN program.  *
1a6ab 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f  /.  nRow = p->nO
1a6ac 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c  p;.  if( p->expl
1a6ad 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53  ain==1 ){.    pS
1a6ae 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d  ub = &p->aMem[9]
1a6af 3b 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e  ;.    if( pSub->
1a6b0 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29  flags&MEM_Blob )
1a6b1 7b 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70  {.      nSub = p
1a6b2 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64  Sub->n/sizeof(Vd
1a6b3 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75  be*);.      apSu
1a6b4 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20  b = (SubProgram 
1a6b5 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20  **)pSub->z;.    
1a6b6 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
1a6b7 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nSub; i++){.   
1a6b8 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62     nRow += apSub
1a6b9 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a  [i]->nOp;.    }.
1a6ba 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69    }..  do{.    i
1a6bb 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77   = p->pc++;.  }w
1a6bc 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20  hile( i<nRow && 
1a6bd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26  p->explain==2 &&
1a6be 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64   p->aOp[i].opcod
1a6bf 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b  e!=OP_Explain );
1a6c0 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29  .  if( i>=nRow )
1a6c1 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
1a6c2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20  LITE_OK;.    rc 
1a6c3 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
1a6c4 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75   }else if( db->u
1a6c5 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
1a6c6 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  ){.    p->rc = S
1a6c7 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
1a6c8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1a6c9 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
1a6ca 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1a6cb 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1a6cc 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
1a6cd 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c  r(p->rc));.  }el
1a6ce 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  se{.    char *z;
1a6cf 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  .    Op *pOp;.  
1a6d0 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29    if( i<p->nOp )
1a6d1 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70  {.      pOp = &p
1a6d2 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65  ->aOp[i];.    }e
1a6d3 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a  lse{.      int j
1a6d4 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e  ;.      i -= p->
1a6d5 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  nOp;.      for(j
1a6d6 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d  =0; i>=apSub[j]-
1a6d7 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nOp; j++){.    
1a6d8 20 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a      i -= apSub[j
1a6d9 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a  ]->nOp;.      }.
1a6da 20 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53        pOp = &apS
1a6db 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20  ub[j]->aOp[i];. 
1a6dc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e     }.    if( p->
1a6dd 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
1a6de 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1a6df 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
1a6e0 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1a6e1 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20  LITE_INTEGER;.  
1a6e2 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1a6e3 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1a6e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6e5 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75    /* Program cou
1a6e6 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d  nter */.      pM
1a6e7 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70  em++;.  .      p
1a6e8 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a6e9 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c  _Static|MEM_Str|
1a6ea 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20  MEM_Term;.      
1a6eb 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a  pMem->z = (char*
1a6ec 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61  )sqlite3OpcodeNa
1a6ed 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b  me(pOp->opcode);
1a6ee 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20    /* Opcode */. 
1a6ef 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
1a6f0 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20  m->z!=0 );.     
1a6f1 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74   pMem->n = sqlit
1a6f2 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d  e3Strlen30(pMem-
1a6f3 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >z);.      pMem-
1a6f4 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54  >type = SQLITE_T
1a6f5 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  EXT;.      pMem-
1a6f6 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
1a6f7 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b  F8;.      pMem++
1a6f8 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ;..      if( pOp
1a6f9 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42  ->p4type==P4_SUB
1a6fa 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20  PROGRAM ){.     
1a6fb 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
1a6fc 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53  nSub+1)*sizeof(S
1a6fd 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20  ubProgram*);.   
1a6fe 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
1a6ff 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
1a700 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Sub; j++){.     
1a701 20 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a       if( apSub[j
1a702 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67  ]==pOp->p4.pProg
1a703 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ram ) break;.   
1a704 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1a705 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51  f( j==nSub && SQ
1a706 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33  LITE_OK==sqlite3
1a707 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62  VdbeMemGrow(pSub
1a708 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20  , nByte, 1) ){. 
1a709 20 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d           apSub =
1a70a 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29   (SubProgram **)
1a70b 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20  pSub->z;.       
1a70c 20 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d     apSub[nSub++]
1a70d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67   = pOp->p4.pProg
1a70e 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ram;.          p
1a70f 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Sub->flags |= ME
1a710 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20  M_Blob;.        
1a711 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62    pSub->n = nSub
1a712 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72  *sizeof(SubProgr
1a713 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  am*);.        }.
1a714 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1a715 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a716 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d   MEM_Int;.    pM
1a717 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  em->u.i = pOp->p
1a718 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1a719 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1a71a 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  1 */.    pMem->t
1a71b 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
1a71c 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b  EGER;.    pMem++
1a71d 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ;..    pMem->fla
1a71e 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1a71f 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f    pMem->u.i = pO
1a720 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20  p->p2;          
1a721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a722 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65  /* P2 */.    pMe
1a723 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a724 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d  _INTEGER;.    pM
1a725 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
1a726 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
1a727 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1a728 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a729 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70     pMem->u.i = p
1a72a 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20  Op->p3;         
1a72b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a72c 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20   /* P3 */.      
1a72d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a72e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
1a72f 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d     pMem++;.    }
1a730 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
1a731 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
1a732 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20  m, 32, 0) ){    
1a733 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f          /* P4 */
1a734 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a735 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a736 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ed );.      retu
1a737 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1a738 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
1a739 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e  >flags = MEM_Dyn
1a73a 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72  |MEM_Str|MEM_Ter
1a73b 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c  m;.    z = displ
1a73c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e  ayP4(pOp, pMem->
1a73d 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20  z, 32);.    if( 
1a73e 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20  z!=pMem->z ){.  
1a73f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1a740 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a  emSetStr(pMem, z
1a741 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
1a742 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  8, 0);.    }else
1a743 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1a744 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  pMem->z!=0 );.  
1a745 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
1a746 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
1a747 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d  em->z);.      pM
1a748 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
1a749 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20  _UTF8;.    }.   
1a74a 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1a74b 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70  LITE_TEXT;.    p
1a74c 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20  Mem++;..    if( 
1a74d 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b  p->explain==1 ){
1a74e 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1a74f 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
1a750 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20  em, 4, 0) ){.   
1a751 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
1a752 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a753 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   );.        retu
1a754 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1a755 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1a756 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a757 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  _Dyn|MEM_Str|MEM
1a758 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65  _Term;.      pMe
1a759 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20  m->n = 2;.      
1a75a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a75b 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e  (3, pMem->z, "%.
1a75c 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20  2x", pOp->p5);  
1a75d 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20   /* P5 */.      
1a75e 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a75f 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20  ITE_TEXT;.      
1a760 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
1a761 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70  TE_UTF8;.      p
1a762 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66  Mem++;.  .#ifdef
1a763 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1a764 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f      if( pOp->zCo
1a765 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  mment ){.       
1a766 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a767 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Str|MEM_Term;
1a768 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  .        pMem->z
1a769 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74   = pOp->zComment
1a76a 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
1a76b 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
1a76c 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
1a76d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20        pMem->enc 
1a76e 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
1a76f 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
1a770 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
1a771 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  .      }else.#en
1a772 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
1a773 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1a774 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20  = MEM_Null;     
1a775 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a776 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a    /* Comment */.
1a777 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79          pMem->ty
1a778 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
1a779 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a77a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  .    p->nResColu
1a77b 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65  mn = 8 - 5*(p->e
1a77c 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70  xplain-1);.    p
1a77d 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
1a77e 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1a77f 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74  E_ROW;.  }.  ret
1a780 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
1a781 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1a782 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64  EXPLAIN */..#ifd
1a783 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a784 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  /*.** Print the 
1a785 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65  SQL that was use
1a786 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
1a787 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  VDBE program..*/
1a788 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a789 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a78a 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70  PrintSql(Vdbe *p
1a78b 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70  ){.  int nOp = p
1a78c 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20  ->nOp;.  VdbeOp 
1a78d 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c  *pOp;.  if( nOp<
1a78e 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f  1 ) return;.  pO
1a78f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a  p = &p->aOp[0];.
1a790 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1a791 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70  e==OP_Trace && p
1a792 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20  Op->p4.z!=0 ){. 
1a793 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1a794 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
1a795 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
1a796 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b  Isspace(*z) ) z+
1a797 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53  +;.    printf("S
1a798 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b  QL: [%s]\n", z);
1a799 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
1a79a 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1a79b 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26  TE_OMIT_TRACE) &
1a79c 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
1a79d 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29  _ENABLE_IOTRACE)
1a79e 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20  ./*.** Print an 
1a79f 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20  IOTRACE message 
1a7a0 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74  showing SQL cont
1a7a1 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
1a7a2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a7a3 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
1a7a4 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  l(Vdbe *p){.  in
1a7a5 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  t nOp = p->nOp;.
1a7a6 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
1a7a7 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72   if( sqlite3IoTr
1a7a8 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ace==0 ) return;
1a7a9 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72  .  if( nOp<1 ) r
1a7aa 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26  eturn;.  pOp = &
1a7ab 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28  p->aOp[0];.  if(
1a7ac 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1a7ad 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70  _Trace && pOp->p
1a7ae 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  4.z!=0 ){.    in
1a7af 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72  t i, j;.    char
1a7b0 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71   z[1000];.    sq
1a7b1 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1a7b2 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73  izeof(z), z, "%s
1a7b3 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  ", pOp->p4.z);. 
1a7b4 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69     for(i=0; sqli
1a7b5 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
1a7b6 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72  ; i++){}.    for
1a7b7 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29  (j=0; z[i]; i++)
1a7b8 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1a7b9 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
1a7ba 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1a7bb 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20  z[i-1]!=' ' ){. 
1a7bc 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
1a7bd 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d  = ' ';.        }
1a7be 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1a7bf 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
1a7c0 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [i];.      }.   
1a7c1 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b   }.    z[j] = 0;
1a7c2 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72  .    sqlite3IoTr
1a7c3 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20  ace("SQL %s\n", 
1a7c4 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  z);.  }.}.#endif
1a7c5 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
1a7c6 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45  _TRACE && SQLITE
1a7c7 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
1a7c8 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  */../*.** Alloca
1a7c9 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20  te space from a 
1a7ca 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65  fixed size buffe
1a7cb 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69  r.  Make *pp poi
1a7cc 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c  nt to the.** all
1a7cd 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28  ocated space.  (
1a7ce 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63  Note:  pp is a c
1a7cf 68 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e  har* rather than
1a7d0 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20   a void** to.** 
1a7d1 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20  work around the 
1a7d2 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67  pointer aliasing
1a7d3 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a   rules of C.)  *
1a7d4 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61  pp should initia
1a7d5 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20  lly.** be zero. 
1a7d6 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a   If *pp is not z
1a7d7 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  ero, that means 
1a7d8 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68  that the space h
1a7d9 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  as already.** be
1a7da 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
1a7db 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1a7dc 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e   a noop..**.** n
1a7dd 42 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62  Byte is the numb
1a7de 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
1a7df 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a  pace needed..**.
1a7e0 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74  ** *ppFrom point
1a7e1 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70   to available sp
1a7e2 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69  ace and pEnd poi
1a7e3 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  nts to the end o
1a7e4 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62  f the.** availab
1a7e5 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  le space..**.** 
1a7e6 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75  *pnByte is a cou
1a7e7 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62  nter of the numb
1a7e8 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
1a7e9 70 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66  pace that have f
1a7ea 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f  ailed.** to allo
1a7eb 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20  cate.  If there 
1a7ec 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20  is insufficient 
1a7ed 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d  space in *ppFrom
1a7ee 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
1a7ef 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e  ** request, then
1a7f0 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79   increment *pnBy
1a7f1 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74  te by the amount
1a7f2 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e   of the request.
1a7f3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a7f4 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68  allocSpace(.  ch
1a7f5 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20  ar *pp,         
1a7f6 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65     /* IN/OUT: Se
1a7f7 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74  t *pp to point t
1a7f8 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  o allocated buff
1a7f9 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  er */.  int nByt
1a7fa 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
1a7fb 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1a7fc 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
1a7fd 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20   u8 **ppFrom,   
1a7fe 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
1a7ff 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a   Allocate from *
1a800 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a  ppFrom */.  u8 *
1a801 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  pEnd,           
1a802 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31   /* Pointer to 1
1a803 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
1a804 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75  nd of *ppFrom bu
1a805 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ffer */.  int *p
1a806 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f  nByte          /
1a807 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  * If allocation 
1a808 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20  cannot be made, 
1a809 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74  increment *pnByt
1a80a 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74  e */.){.  assert
1a80b 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
1a80c 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20  GNMENT(*ppFrom) 
1a80d 29 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64  );.  if( (*(void
1a80e 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20  **)pp)==0 ){.   
1a80f 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28   nByte = ROUND8(
1a810 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20  nByte);.    if( 
1a811 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65  &(*ppFrom)[nByte
1a812 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20  ] <= pEnd ){.   
1a813 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d     *(void**)pp =
1a814 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d   (void *)*ppFrom
1a815 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20  ;.      *ppFrom 
1a816 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65  += nByte;.    }e
1a817 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79  lse{.      *pnBy
1a818 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20  te += nByte;.   
1a819 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1a81a 50 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61  Prepare a virtua
1a81b 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78  l machine for ex
1a81c 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69  ecution.  This i
1a81d 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73  nvolves things s
1a81e 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61  uch.** as alloca
1a81f 74 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65  ting stack space
1a820 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e   and initializin
1a821 67 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f  g the program co
1a822 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20  unter..** After 
1a823 74 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20  the VDBE has be 
1a824 70 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20  prepped, it can 
1a825 62 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f  be executed by o
1a826 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61  ne or more.** ca
1a827 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64  lls to sqlite3Vd
1a828 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a  beExec().  .**.*
1a829 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e  * This is the on
1a82a 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61  ly way to move a
1a82b 20 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f   VDBE from VDBE_
1a82c 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a  MAGIC_INIT to.**
1a82d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e   VDBE_MAGIC_RUN.
1a82e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
1a82f 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
1a830 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ed more than onc
1a831 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69  e on a single vi
1a832 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a  rtual machine..*
1a833 2a 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c  * The first call
1a834 20 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63   is made while c
1a835 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c  ompiling the SQL
1a836 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73   statement. Subs
1a837 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20  equent.** calls 
1a838 61 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74  are made as part
1a839 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20   of the process 
1a83a 6f 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73  of resetting a s
1a83b 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a  tatement to be.*
1a83c 2a 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66  * re-executed (f
1a83d 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  rom a call to sq
1a83e 6c 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20  lite3_reset()). 
1a83f 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20  The nVar, nMem, 
1a840 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20  nCursor .** and 
1a841 69 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65  isExplain parame
1a842 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61  ters are only pa
1a843 73 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c  ssed correct val
1a844 75 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69  ues the first ti
1a845 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69  me.** the functi
1a846 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e  on is called. On
1a847 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
1a848 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  s, from sqlite3_
1a849 72 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a  reset(), nVar.**
1a84a 20 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e   is passed -1 an
1a84b 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20  d nMem, nCursor 
1a84c 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72  and isExplain ar
1a84d 65 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72  e all passed zer
1a84e 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
1a84f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a850 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a  3VdbeMakeReady(.
1a851 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20    Vdbe *p,      
1a852 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a853 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a   /* The VDBE */.
1a854 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20    int nVar,     
1a855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a856 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f   /* Number of '?
1a857 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c  ' see in the SQL
1a858 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1a859 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20  int nMem,       
1a85a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a85b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
1a85c 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f  ry cells to allo
1a85d 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  cate */.  int nC
1a85e 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20  ursor,          
1a85f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1a860 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f  er of cursors to
1a861 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69   allocate */.  i
1a862 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20  nt nArg,        
1a863 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a864 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
1a865 6f 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72  of args in SubPr
1a866 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20  ograms */.  int 
1a867 69 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20  isExplain,      
1a868 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1a869 75 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49  ue if the EXPLAI
1a86a 4e 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72  N keywords is pr
1a86b 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75  esent */.  int u
1a86c 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20  sesStmtJournal  
1a86d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1a86e 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73  e to set Vdbe.us
1a86f 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f  esStmtJournal */
1a870 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73  .){.  int n;.  s
1a871 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
1a872 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  db;..  assert( p
1a873 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1a874 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1a875 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20  MAGIC_INIT );.. 
1a876 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64   /* There should
1a877 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   be at least one
1a878 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20   opcode..  */.  
1a879 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
1a87a 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68   );..  /* Set th
1a87b 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f  e magic to VDBE_
1a87c 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72  MAGIC_RUN sooner
1a87d 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74   rather than lat
1a87e 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69  er. */.  p->magi
1a87f 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  c = VDBE_MAGIC_R
1a880 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61  UN;..  /* For ea
1a881 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72  ch cursor requir
1a882 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74  ed, also allocat
1a883 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  e a memory cell.
1a884 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c   Memory.  ** cel
1a885 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73  ls (nMem+1-nCurs
1a886 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75  or)..nMem, inclu
1a887 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72  sive, will never
1a888 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a   be used by.  **
1a889 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
1a88a 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20  m. Instead they 
1a88b 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f  are used to allo
1a88c 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20  cate space for. 
1a88d 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42   ** VdbeCursor/B
1a88e 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72  tCursor structur
1a88f 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20  es. The blob of 
1a890 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
1a891 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72  d with .  ** cur
1a892 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20  sor 0 is stored 
1a893 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e  in memory cell n
1a894 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c  Mem. Memory cell
1a895 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73   (nMem-1).  ** s
1a896 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f  tores the blob o
1a897 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  f memory associa
1a898 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20  ted with cursor 
1a899 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a  1, etc..  **.  *
1a89a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f  * See also: allo
1a89b 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20  cateCursor()..  
1a89c 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75  */.  nMem += nCu
1a89d 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  rsor;..  /* Allo
1a89e 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d  cate space for m
1a89f 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c  emory registers,
1a8a0 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20   SQL variables, 
1a8a1 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64  VDBE cursors and
1a8a2 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20   .  ** an array 
1a8a3 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66  to marshal SQL f
1a8a4 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74  unction argument
1a8a5 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e  s in. This is on
1a8a6 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a  ly done the.  **
1a8a7 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
1a8a8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
1a8a9 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20  led for a given 
1a8aa 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69  VDBE, not when i
1a8ab 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20  t is.  ** being 
1a8ac 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69  called from sqli
1a8ad 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72  te3_reset() to r
1a8ae 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c  eset the virtual
1a8af 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20   machine..  */. 
1a8b0 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20   if( nVar>=0 && 
1a8b1 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f  ALWAYS(db->mallo
1a8b2 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20  cFailed==0) ){. 
1a8b3 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75     u8 *zCsr = (u
1a8b4 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  8 *)&p->aOp[p->n
1a8b5 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e  Op];.    u8 *zEn
1a8b6 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f  d = (u8 *)&p->aO
1a8b7 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a  p[p->nOpAlloc];.
1a8b8 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20      int nByte;. 
1a8b9 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75     resolveP2Valu
1a8ba 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20  es(p, &nArg);.  
1a8bb 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75    p->usesStmtJou
1a8bc 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 73 53  rnal = (u8)usesS
1a8bd 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20  tmtJournal;.    
1a8be 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26  if( isExplain &&
1a8bf 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20   nMem<10 ){.    
1a8c0 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20    nMem = 10;.   
1a8c1 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 43   }.    memset(zC
1a8c2 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72  sr, 0, zEnd-zCsr
1a8c3 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 28  );.    zCsr += (
1a8c4 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29 26 37  zCsr - (u8*)0)&7
1a8c5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 49  ;.    assert( EI
1a8c6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
1a8c7 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20  NT(zCsr) );..   
1a8c8 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42 79 74   do {.      nByt
1a8c9 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c  e = 0;.      all
1a8ca 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26  ocSpace((char*)&
1a8cb 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69  p->aMem, nMem*si
1a8cc 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72  zeof(Mem), &zCsr
1a8cd 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b  , zEnd, &nByte);
1a8ce 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63  .      allocSpac
1a8cf 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 56 61  e((char*)&p->aVa
1a8d0 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d  r, nVar*sizeof(M
1a8d1 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64  em), &zCsr, zEnd
1a8d2 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  , &nByte);.     
1a8d3 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61   allocSpace((cha
1a8d4 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41  r*)&p->apArg, nA
1a8d5 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c  rg*sizeof(Mem*),
1a8d6 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
1a8d7 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c  Byte);.      all
1a8d8 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26  ocSpace((char*)&
1a8d9 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73  p->azVar, nVar*s
1a8da 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a  izeof(char*), &z
1a8db 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
1a8dc 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53  e);.      allocS
1a8dd 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e  pace((char*)&p->
1a8de 61 70 43 73 72 2c 20 0a 20 20 20 20 20 20 20 20  apCsr, .        
1a8df 20 20 20 20 20 20 20 20 20 6e 43 75 72 73 6f 72           nCursor
1a8e0 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73  *sizeof(VdbeCurs
1a8e1 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e  or*), &zCsr, zEn
1a8e2 64 2c 20 26 6e 42 79 74 65 0a 20 20 20 20 20 20  d, &nByte.      
1a8e3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 42 79  );.      if( nBy
1a8e4 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  te ){.        p-
1a8e5 3e 70 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33  >pFree = sqlite3
1a8e6 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
1a8e7 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d   nByte);.      }
1a8e8 0a 20 20 20 20 20 20 7a 43 73 72 20 3d 20 70 2d  .      zCsr = p-
1a8e9 3e 70 46 72 65 65 3b 0a 20 20 20 20 20 20 7a 45  >pFree;.      zE
1a8ea 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65  nd = &zCsr[nByte
1a8eb 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 6e  ];.    }while( n
1a8ec 42 79 74 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c  Byte && !db->mal
1a8ed 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20  locFailed );..  
1a8ee 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28    p->nCursor = (
1a8ef 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20  u16)nCursor;.   
1a8f0 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a   if( p->aVar ){.
1a8f1 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20        p->nVar = 
1a8f2 28 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20  (ynVar)nVar;.   
1a8f3 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56     for(n=0; n<nV
1a8f4 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20  ar; n++){.      
1a8f5 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61    p->aVar[n].fla
1a8f6 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1a8f7 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e         p->aVar[n
1a8f8 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20  ].db = db;.     
1a8f9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1a8fa 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20   p->aMem ){.    
1a8fb 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20    p->aMem--;    
1a8fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a8fd 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73    /* aMem[] goes
1a8fe 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f   from 1..nMem */
1a8ff 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d  .      p->nMem =
1a900 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   nMem;          
1a901 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20         /*       
1a902 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d  not from 0..nMem
1a903 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28  -1 */.      for(
1a904 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b  n=1; n<=nMem; n+
1a905 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61  +){.        p->a
1a906 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d  Mem[n].flags = M
1a907 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  EM_Null;.       
1a908 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d   p->aMem[n].db =
1a909 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   db;.      }.   
1a90a 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51   }.  }.#ifdef SQ
1a90b 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72  LITE_DEBUG.  for
1a90c 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b  (n=1; n<p->nMem;
1a90d 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   n++){.    asser
1a90e 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62  t( p->aMem[n].db
1a90f 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64  ==db );.  }.#end
1a910 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31  if..  p->pc = -1
1a911 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49  ;.  p->rc = SQLI
1a912 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f  TE_OK;.  p->erro
1a913 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f  rAction = OE_Abo
1a914 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e  rt;.  p->explain
1a915 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20   |= isExplain;. 
1a916 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
1a917 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d  _MAGIC_RUN;.  p-
1a918 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  >nChange = 0;.  
1a919 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b  p->cacheCtr = 1;
1a91a 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69  .  p->minWriteFi
1a91b 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a  leFormat = 255;.
1a91c 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20    p->iStatement 
1a91d 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45  = 0;.#ifdef VDBE
1a91e 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20  _PROFILE.  {.   
1a91f 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
1a920 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69  i=0; i<p->nOp; i
1a921 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f  ++){.      p->aO
1a922 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20  p[i].cnt = 0;.  
1a923 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79      p->aOp[i].cy
1a924 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  cles = 0;.    }.
1a925 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
1a926 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45  .** Close a VDBE
1a927 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65   cursor and rele
1a928 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f  ase all the reso
1a929 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73 6f  urces that curso
1a92a 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f  r .** happens to
1a92b 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   hold..*/.SQLITE
1a92c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a92d 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
1a92e 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62  sor(Vdbe *p, Vdb
1a92f 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20  eCursor *pCx){. 
1a930 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20   if( pCx==0 ){. 
1a931 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1a932 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b   if( pCx->pBt ){
1a933 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1a934 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29  eClose(pCx->pBt)
1a935 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78  ;.    /* The pCx
1a936 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62  ->pCursor will b
1a937 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69  e close automati
1a938 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69  cally, if it exi
1a939 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74  sts, by.    ** t
1a93a 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a  he call above. *
1a93b 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43  /.  }else if( pC
1a93c 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  x->pCursor ){.  
1a93d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
1a93e 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70  oseCursor(pCx->p
1a93f 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66  Cursor);.  }.#if
1a940 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1a941 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1a942 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75  if( pCx->pVtabCu
1a943 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rsor ){.    sqli
1a944 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1a945 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70  *pVtabCursor = p
1a946 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b  Cx->pVtabCursor;
1a947 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  .    const sqlit
1a948 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1a949 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c  le = pCx->pModul
1a94a 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  e;.    p->inVtab
1a94b 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20  Method = 1;.    
1a94c 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1a94d 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20  etyOff(p->db);. 
1a94e 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
1a94f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b  se(pVtabCursor);
1a950 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1a951 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62  e3SafetyOn(p->db
1a952 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  );.    p->inVtab
1a953 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a  Method = 0;.  }.
1a954 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
1a955 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73 20  Copy the values 
1a956 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64  stored in the Vd
1a957 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72  beFrame structur
1a958 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20 54  e to its Vdbe. T
1a959 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20  his.** is used, 
1a95a 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65  for example, whe
1a95b 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 62 2d  n a trigger sub-
1a95c 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74 65  program is halte
1a95d 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20  d to restore.** 
1a95e 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d  control to the m
1a95f 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  ain program..*/.
1a960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1a961 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  nt sqlite3VdbeFr
1a962 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46  ameRestore(VdbeF
1a963 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20  rame *pFrame){. 
1a964 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61 6d   Vdbe *v = pFram
1a965 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d  e->v;.  v->aOp =
1a966 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20   pFrame->aOp;.  
1a967 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d  v->nOp = pFrame-
1a968 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20  >nOp;.  v->aMem 
1a969 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a  = pFrame->aMem;.
1a96a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61    v->nMem = pFra
1a96b 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61  me->nMem;.  v->a
1a96c 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61  pCsr = pFrame->a
1a96d 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73  pCsr;.  v->nCurs
1a96e 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75  or = pFrame->nCu
1a96f 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c  rsor;.  v->db->l
1a970 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d  astRowid = pFram
1a971 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20  e->lastRowid;.  
1a972 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72  v->nChange = pFr
1a973 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20  ame->nChange;.  
1a974 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70  return pFrame->p
1a975 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
1a976 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a  e all cursors..*
1a977 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73  *.** Also releas
1a978 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65  e any dynamic me
1a979 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65  mory held by the
1a97a 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e   VM in the Vdbe.
1a97b 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  aMem memory .** 
1a97c 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73  cell array. This
1a97d 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73   is necessary as
1a97e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1a97f 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61   array may conta
1a980 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74  in.** pointers t
1a981 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65  o VdbeFrame obje
1a982 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69  cts, which may i
1a983 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70  n turn contain p
1a984 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70  ointers to.** op
1a985 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73  en cursors..*/.s
1a986 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65  tatic void close
1a987 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20  AllCursors(Vdbe 
1a988 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46  *p){.  if( p->pF
1a989 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65  rame ){.    Vdbe
1a98a 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20  Frame *pFrame = 
1a98b 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20 66  p->pFrame;.    f
1a98c 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72  or(pFrame=p->pFr
1a98d 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61  ame; pFrame->pPa
1a98e 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46 72  rent; pFrame=pFr
1a98f 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  ame->pParent);. 
1a990 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72     sqlite3VdbeFr
1a991 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d  ameRestore(pFram
1a992 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72  e);.  }.  p->pFr
1a993 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46  ame = 0;.  p->nF
1a994 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28  rame = 0;..  if(
1a995 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20 20   p->apCsr ){.   
1a996 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
1a997 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  i=0; i<p->nCurso
1a998 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56  r; i++){.      V
1a999 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d 20  dbeCursor *pC = 
1a99a 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20  p->apCsr[i];.   
1a99b 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20 20     if( pC ){.   
1a99c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a99d 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43  FreeCursor(p, pC
1a99e 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70  );.        p->ap
1a99f 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  Csr[i] = 0;.    
1a9a0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1a9a1 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20  if( p->aMem ){. 
1a9a2 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72     releaseMemArr
1a9a3 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20  ay(&p->aMem[1], 
1a9a4 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a  p->nMem);.  }.}.
1a9a5 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20  ./*.** Clean up 
1a9a6 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65  the VM after exe
1a9a7 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cution..**.** Th
1a9a8 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1a9a9 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c  automatically cl
1a9aa 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c  ose any cursors,
1a9ab 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a   lists, and/or.*
1a9ac 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77  * sorters that w
1a9ad 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20  ere left open.  
1a9ae 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20  It also deletes 
1a9af 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a  the values of.**
1a9b0 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68   variables in th
1a9b1 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a  e aVar[] array..
1a9b2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43  */.static void C
1a9b3 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b  leanup(Vdbe *p){
1a9b4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1a9b5 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20   p->db;..#ifdef 
1a9b6 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f  SQLITE_DEBUG.  /
1a9b7 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 72 74  * Execute assert
1a9b8 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  () statements to
1a9b9 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
1a9ba 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e   Vdbe.apCsr[] an
1a9bb 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65  d .  ** Vdbe.aMe
1a9bc 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65 20  m[] arrays have 
1a9bd 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c 65  already been cle
1a9be 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69  aned up.  */.  i
1a9bf 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
1a9c0 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69   i<p->nCursor; i
1a9c1 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ++) assert( p->a
1a9c2 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70  pCsr==0 || p->ap
1a9c3 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66  Csr[i]==0 );.  f
1a9c4 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d  or(i=1; i<=p->nM
1a9c5 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28  em; i++) assert(
1a9c6 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70   p->aMem==0 || p
1a9c7 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d  ->aMem[i].flags=
1a9c8 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e  =MEM_Null );.#en
1a9c9 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44 62  dif..  sqlite3Db
1a9ca 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1a9cb 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d  Msg);.  p->zErrM
1a9cc 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65  sg = 0;.  p->pRe
1a9cd 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a  sultSet = 0;.}..
1a9ce 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75  /*.** Set the nu
1a9cf 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63  mber of result c
1a9d0 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c  olumns that will
1a9d1 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20   be returned by 
1a9d2 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74  this SQL.** stat
1a9d3 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e  ement. This is n
1a9d4 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c  ow set at compil
1a9d5 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74  e time, rather t
1a9d6 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78  han during.** ex
1a9d7 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ecution of the v
1a9d8 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74  dbe program so t
1a9d9 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  hat sqlite3_colu
1a9da 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a  mn_count() can.*
1a9db 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61  * be called on a
1a9dc 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1a9dd 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73  before sqlite3_s
1a9de 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tep()..*/.SQLITE
1a9df 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a9e0 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
1a9e1 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ols(Vdbe *p, int
1a9e2 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20   nResColumn){.  
1a9e3 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20  Mem *pColName;. 
1a9e4 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65   int n;.  sqlite
1a9e5 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a  3 *db = p->db;..
1a9e6 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
1a9e7 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70  y(p->aColName, p
1a9e8 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c  ->nResColumn*COL
1a9e9 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74  NAME_N);.  sqlit
1a9ea 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1a9eb 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d  aColName);.  n =
1a9ec 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e   nResColumn*COLN
1a9ed 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73  AME_N;.  p->nRes
1a9ee 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52  Column = (u16)nR
1a9ef 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61  esColumn;.  p->a
1a9f0 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61  ColName = pColNa
1a9f1 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74  me = (Mem*)sqlit
1a9f2 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
1a9f3 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e  b, sizeof(Mem)*n
1a9f4 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f   );.  if( p->aCo
1a9f5 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72  lName==0 ) retur
1a9f6 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20  n;.  while( n-- 
1a9f7 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e  > 0 ){.    pColN
1a9f8 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  ame->flags = MEM
1a9f9 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e  _Null;.    pColN
1a9fa 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  ame->db = p->db;
1a9fb 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b  .    pColName++;
1a9fc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
1a9fd 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  t the name of th
1a9fe 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20  e idx'th column 
1a9ff 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  to be returned b
1aa00 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  y the SQL statem
1aa01 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75  ent..** zName mu
1aa02 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20  st be a pointer 
1aa03 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61  to a nul termina
1aa04 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  ted string..**.*
1aa05 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 74  * This call must
1aa06 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 61   be made after a
1aa07 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1aa08 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29  VdbeSetNumCols()
1aa09 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61  ..**.** The fina
1aa0a 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 65  l parameter, xDe
1aa0b 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  l, must be one o
1aa0c 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  f SQLITE_DYNAMIC
1aa0d 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 0a  , SQLITE_STATIC.
1aa0e 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 41  ** or SQLITE_TRA
1aa0f 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 73  NSIENT. If it is
1aa10 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c   SQLITE_DYNAMIC,
1aa11 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 72   then the buffer
1aa12 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
1aa13 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20  y zName will be 
1aa14 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33  freed by sqlite3
1aa15 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 68  DbFree() when th
1aa16 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72 6f  e vdbe is destro
1aa17 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  yed..*/.SQLITE_P
1aa18 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1aa19 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
1aa1a 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20  (.  Vdbe *p,    
1aa1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa1c 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 69       /* Vdbe bei
1aa1d 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f  ng configured */
1aa1e 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 20  .  int idx,     
1aa1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1aa21 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70  column zName app
1aa22 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  lies to */.  int
1aa23 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 20   var,           
1aa24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1aa25 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e   One of the COLN
1aa26 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20  AME_* constants 
1aa27 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1aa28 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
1aa29 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1aa2a 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61   to buffer conta
1aa2b 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20  ining name */.  
1aa2c 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1aa2d 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20  d*)             
1aa2e 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67   /* Memory manag
1aa2f 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 66  ement strategy f
1aa30 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20  or zName */.){. 
1aa31 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a   int rc;.  Mem *
1aa32 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65  pColName;.  asse
1aa33 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43  rt( idx<p->nResC
1aa34 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72  olumn );.  asser
1aa35 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e  t( var<COLNAME_N
1aa36 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d   );.  if( p->db-
1aa37 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1aa38 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a 4e  .    assert( !zN
1aa39 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c  ame || xDel!=SQL
1aa3a 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20  ITE_DYNAMIC );. 
1aa3b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1aa3c 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
1aa3d 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d  sert( p->aColNam
1aa3e 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61  e!=0 );.  pColNa
1aa3f 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61  me = &(p->aColNa
1aa40 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52  me[idx+var*p->nR
1aa41 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63  esColumn]);.  rc
1aa42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
1aa43 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65  mSetStr(pColName
1aa44 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c  , zName, -1, SQL
1aa45 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b  ITE_UTF8, xDel);
1aa46 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 30  .  assert( rc!=0
1aa47 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70   || !zName || (p
1aa48 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d  ColName->flags&M
1aa49 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20  EM_Term)!=0 );. 
1aa4a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1aa4b 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 77  *.** A read or w
1aa4c 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1aa4d 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
1aa4e 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 74  be active on dat
1aa4f 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20  abase handle.** 
1aa50 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  db. If a transac
1aa51 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20  tion is active, 
1aa52 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 68  commit it. If th
1aa53 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 74  ere is a.** writ
1aa54 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 70  e-transaction sp
1aa55 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  anning more than
1aa56 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69   one database fi
1aa57 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  le, this routine
1aa58 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 6f  .** takes care o
1aa59 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
1aa5a 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a  rnal trickery..*
1aa5b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62  /.static int vdb
1aa5c 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20  eCommit(sqlite3 
1aa5d 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20  *db, Vdbe *p){. 
1aa5e 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54   int i;.  int nT
1aa5f 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75  rans = 0;  /* Nu
1aa60 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65  mber of database
1aa61 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 65  s with an active
1aa62 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
1aa63 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  on */.  int rc =
1aa64 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
1aa65 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20  t needXcommit = 
1aa66 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  0;..#ifdef SQLIT
1aa67 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1aa68 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74 68  BLE.  /* With th
1aa69 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74  is option, sqlit
1aa6a 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 20  e3VtabSync() is 
1aa6b 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 69  defined to be si
1aa6c 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54  mply .  ** SQLIT
1aa6d 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74  E_OK so p is not
1aa6e 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55   used. .  */.  U
1aa6f 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1aa70 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  p);.#endif..  /*
1aa71 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e   Before doing an
1aa72 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c  ything else, cal
1aa73 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61  l the xSync() ca
1aa74 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20  llback for any. 
1aa75 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75   ** virtual modu
1aa76 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65  le tables writte
1aa77 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61  n in this transa
1aa78 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20  ction. This has 
1aa79 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20  to.  ** be done 
1aa7a 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69  before determini
1aa7b 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73  ng whether a mas
1aa7c 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1aa7d 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72   is .  ** requir
1aa7e 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28  ed, as an xSync(
1aa7f 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61  ) callback may a
1aa80 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  dd an attached d
1aa81 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20  atabase.  ** to 
1aa82 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1aa83 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
1aa84 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c  ite3VtabSync(db,
1aa85 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20   &p->zErrMsg);. 
1aa86 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1aa87 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1aa88 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54   rc;.  }..  /* T
1aa89 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69  his loop determi
1aa8a 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63  nes (a) if the c
1aa8b 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c  ommit hook shoul
1aa8c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64  d be invoked and
1aa8d 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61  .  ** (b) how ma
1aa8e 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ny database file
1aa8f 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74  s have open writ
1aa90 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20  e transactions, 
1aa91 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64  not .  ** includ
1aa92 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74  ing the temp dat
1aa93 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d  abase. (b) is im
1aa94 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20  portant because 
1aa95 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20  if more than .  
1aa96 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  ** one database 
1aa97 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e  file has an open
1aa98 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1aa99 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  on, a master jou
1aa9a 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69  rnal.  ** file i
1aa9b 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61  s required for a
1aa9c 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e  n atomic commit.
1aa9d 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30  .  */ .  for(i=0
1aa9e 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1aa9f 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70  ){ .    Btree *p
1aaa0 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
1aaa1 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  pBt;.    if( sql
1aaa2 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
1aaa3 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  ns(pBt) ){.     
1aaa4 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31   needXcommit = 1
1aaa5 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31  ;.      if( i!=1
1aaa6 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20   ) nTrans++;.   
1aaa7 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1aaa8 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72  there are any wr
1aaa9 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite-transactions
1aaaa 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20   at all, invoke 
1aaab 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20  the commit hook 
1aaac 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f  */.  if( needXco
1aaad 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d  mmit && db->xCom
1aaae 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20  mitCallback ){. 
1aaaf 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1aab0 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1aab1 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d     rc = db->xCom
1aab2 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e  mitCallback(db->
1aab3 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20  pCommitArg);.   
1aab4 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1aab5 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20  fetyOn(db);.    
1aab6 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
1aab7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1aab8 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a  NSTRAINT;.    }.
1aab9 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69    }..  /* The si
1aaba 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d  mple case - no m
1aabb 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74  ore than one dat
1aabc 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20  abase file (not 
1aabd 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a  counting the.  *
1aabe 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29  * TEMP database)
1aabf 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69   has a transacti
1aac0 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65  on active.   The
1aac1 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f  re is no need fo
1aac2 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65  r the.  ** maste
1aac3 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a  r-journal..  **.
1aac4 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75    ** If the retu
1aac5 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69  rn value of sqli
1aac6 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
1aac7 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20  ame() is a zero 
1aac8 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69  length.  ** stri
1aac9 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65  ng, it means the
1aaca 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69   main database i
1aacb 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20  s :memory: or a 
1aacc 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a  temp file.  In .
1aacd 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77    ** that case w
1aace 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  e do not support
1aacf 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69   atomic multi-fi
1aad0 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75  le commits, so u
1aad1 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d  se the .  ** sim
1aad2 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f  ple case then to
1aad3 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d  o..  */.  if( 0=
1aad4 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  =sqlite3Strlen30
1aad5 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  (sqlite3BtreeGet
1aad6 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62  Filename(db->aDb
1aad7 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20  [0].pBt)).   || 
1aad8 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20  nTrans<=1.  ){. 
1aad9 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d     for(i=0; rc==
1aada 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
1aadb 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
1aadc 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
1aadd 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
1aade 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
1aadf 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
1aae0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1aae1 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30  tPhaseOne(pBt, 0
1aae2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1aae3 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
1aae4 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61  commit only if a
1aae5 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63  ll databases suc
1aae6 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65  cessfully comple
1aae7 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20  te phase 1. .   
1aae8 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68   ** If one of th
1aae9 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e BtreeCommitPha
1aaea 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61  seOne() calls fa
1aaeb 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61  ils, this indica
1aaec 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f  tes an.    ** IO
1aaed 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c   error while del
1aaee 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74  eting or truncat
1aaef 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ing a journal fi
1aaf0 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65  le. It is unlike
1aaf1 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63  ly,.    ** but c
1aaf2 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20  ould happen. In 
1aaf3 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f  this case abando
1aaf4 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64  n processing and
1aaf5 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
1aaf6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  r..    */.    fo
1aaf7 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
1aaf8 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
1aaf9 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
1aafa 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
1aafb 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
1aafc 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
1aafd 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1aafe 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
1aaff 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20  eTwo(pBt);.     
1ab00 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1ab01 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ab02 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1ab03 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20  tabCommit(db);. 
1ab04 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
1ab05 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20  he complex case 
1ab06 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c  - There is a mul
1ab07 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72  ti-file write-tr
1ab08 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65  ansaction active
1ab09 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75  ..  ** This requ
1ab0a 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f  ires a master jo
1ab0b 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e  urnal file to en
1ab0c 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  sure the transac
1ab0d 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d  tion is.  ** com
1ab0e 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e  mitted atomicly.
1ab0f 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
1ab10 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
1ab11 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c  .  else{.    sql
1ab12 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
1ab13 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69   db->pVfs;.    i
1ab14 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  nt needSync = 0;
1ab15 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  .    char *zMast
1ab16 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c  er = 0;   /* Fil
1ab17 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d  e-name for the m
1ab18 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f  aster journal */
1ab19 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20  .    char const 
1ab1a 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c  *zMainFile = sql
1ab1b 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
1ab1c 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  name(db->aDb[0].
1ab1d 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
1ab1e 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20  3_file *pMaster 
1ab1f 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66  = 0;.    i64 off
1ab20 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  set = 0;.    int
1ab21 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   res;..    /* Se
1ab22 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f  lect a master jo
1ab23 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
1ab24 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  */.    do {.    
1ab25 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20    u32 iRandom;. 
1ab26 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1ab27 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1ab28 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
1ab29 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
1ab2a 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e  (iRandom), &iRan
1ab2b 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73  dom);.      zMas
1ab2c 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ter = sqlite3MPr
1ab2d 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25  intf(db, "%s-mj%
1ab2e 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c  08X", zMainFile,
1ab2f 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66   iRandom&0x7ffff
1ab30 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20  fff);.      if( 
1ab31 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  !zMaster ){.    
1ab32 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ab33 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
1ab34 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1ab35 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73  te3OsAccess(pVfs
1ab36 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54  , zMaster, SQLIT
1ab37 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
1ab38 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69   &res);.    }whi
1ab39 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
1ab3a 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20  K && res );.    
1ab3b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1ab3c 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70  K ){.      /* Op
1ab3d 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  en the master jo
1ab3e 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20  urnal. */.      
1ab3f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
1ab40 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a  enMalloc(pVfs, z
1ab41 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72  Master, &pMaster
1ab42 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  , .          SQL
1ab43 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1ab44 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  TE|SQLITE_OPEN_C
1ab45 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20  REATE|.         
1ab46 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
1ab47 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50  LUSIVE|SQLITE_OP
1ab48 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
1ab49 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20  L, 0.      );.  
1ab4a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
1ab4b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1ab4c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ab4d 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
1ab4e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1ab4f 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57      }. .    /* W
1ab50 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66  rite the name of
1ab51 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66   each database f
1ab52 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73  ile in the trans
1ab53 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  action into the 
1ab54 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65  new.    ** maste
1ab55 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
1ab56 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1ab57 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  rs at this point
1ab58 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e   close.    ** an
1ab59 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  d delete the mas
1ab5a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1ab5b 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69  . All the indivi
1ab5c 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  dual journal fil
1ab5d 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20  es.    ** still 
1ab5e 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74  have 'null' as t
1ab5f 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1ab60 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68  l pointer, so th
1ab61 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20  ey will roll.   
1ab62 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e   ** back indepen
1ab63 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c  dently if a fail
1ab64 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20  ure occurs..    
1ab65 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
1ab66 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1ab67 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
1ab68 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1ab69 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  Bt;.      if( i=
1ab6a 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =1 ) continue;  
1ab6b 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54   /* Ignore the T
1ab6c 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  EMP database */.
1ab6d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1ab6e 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
1ab6f 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
1ab70 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c  char const *zFil
1ab71 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  e = sqlite3Btree
1ab72 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70  GetJournalname(p
1ab73 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Bt);.        if(
1ab74 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63   zFile[0]==0 ) c
1ab75 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e  ontinue;  /* Ign
1ab76 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74  ore :memory: dat
1ab77 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20  abases */.      
1ab78 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20    if( !needSync 
1ab79 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65  && !sqlite3Btree
1ab7a 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74  SyncDisabled(pBt
1ab7b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
1ab7c 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
1ab7d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
1ab7e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
1ab7f 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c  te(pMaster, zFil
1ab80 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
1ab81 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66  30(zFile)+1, off
1ab82 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66  set);.        of
1ab83 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53  fset += sqlite3S
1ab84 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31  trlen30(zFile)+1
1ab85 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1ab86 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1ab87 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ab88 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73  OsCloseFree(pMas
1ab89 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ter);.          
1ab8a 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
1ab8b 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30  pVfs, zMaster, 0
1ab8c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1ab8d 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1ab8e 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20  Master);.       
1ab8f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1ab90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1ab91 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79      }..    /* Sy
1ab92 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  nc the master jo
1ab93 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74  urnal file. If t
1ab94 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  he IOCAP_SEQUENT
1ab95 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a  IAL device.    *
1ab96 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68  * flag is set th
1ab97 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  is is not requir
1ab98 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
1ab99 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20 20  f( needSync .   
1ab9a 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65 33    && 0==(sqlite3
1ab9b 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
1ab9c 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29  ristics(pMaster)
1ab9d 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45  &SQLITE_IOCAP_SE
1ab9e 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20 26  QUENTIAL).     &
1ab9f 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
1aba0 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
1aba1 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45  (pMaster, SQLITE
1aba2 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20  _SYNC_NORMAL)). 
1aba3 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69     ){.      sqli
1aba4 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70  te3OsCloseFree(p
1aba5 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 73  Master);.      s
1aba6 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
1aba7 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29  Vfs, zMaster, 0)
1aba8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
1aba9 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65  bFree(db, zMaste
1abaa 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
1abab 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
1abac 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 20  /* Sync all the 
1abad 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65  db files involve
1abae 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63  d in the transac
1abaf 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 63  tion. The same c
1abb0 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 20  all.    ** sets 
1abb1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1abb2 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 61  al pointer in ea
1abb3 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  ch individual jo
1abb4 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a  urnal. If.    **
1abb5 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1abb6 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65   here, do not de
1abb7 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
1abb8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20  journal file..  
1abb9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
1abba 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  he error occurs 
1abbb 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 74  during the first
1abbc 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20   call to.    ** 
1abbd 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1abbe 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 68  itPhaseOne(), th
1abbf 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 68  en there is a ch
1abc0 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 20  ance that the.  
1abc1 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72    ** master jour
1abc2 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 65  nal file will be
1abc3 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 77   orphaned. But w
1abc4 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20  e cannot delete 
1abc5 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61  it,.    ** in ca
1abc6 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  se the master jo
1abc7 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
1abc8 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  was written into
1abc9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20   the journal.   
1abca 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20   ** file before 
1abcb 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 75  the failure occu
1abcc 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rred..    */.   
1abcd 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
1abce 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d  LITE_OK && i<db-
1abcf 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nDb; i++){ .   
1abd0 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1abd1 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1abd2 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
1abd3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1abd4 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1abd5 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d  PhaseOne(pBt, zM
1abd6 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  aster);.      }.
1abd7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1abd8 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61  3OsCloseFree(pMa
1abd9 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ster);.    if( r
1abda 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1abdb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1abdc 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29  ree(db, zMaster)
1abdd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
1abde 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
1abdf 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74   Delete the mast
1abe0 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
1abe1 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74 68   This commits th
1abe2 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  e transaction. A
1abe3 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e  fter.    ** doin
1abe4 67 20 74 68 69 73 20 74 68 65 20 64 69 72 65 63  g this the direc
1abe5 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20 61  tory is synced a
1abe6 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79 20  gain before any 
1abe7 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20 2a  individual.    *
1abe8 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69  * transaction fi
1abe9 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 64 2e  les are deleted.
1abea 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
1abeb 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1abec 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
1abed 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  1);.    sqlite3D
1abee 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65  bFree(db, zMaste
1abef 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20  r);.    zMaster 
1abf0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 20  = 0;.    if( rc 
1abf1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1abf2 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
1abf3 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20  * All files and 
1abf4 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 76 65  directories have
1abf5 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79   already been sy
1abf6 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c  nced, so the fol
1abf7 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61  lowing.    ** ca
1abf8 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74  lls to sqlite3Bt
1abf9 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
1abfa 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f  o() are only clo
1abfb 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a 20  sing files and. 
1abfc 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f     ** deleting o
1abfd 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75  r truncating jou
1abfe 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68  rnals. If someth
1abff 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 77  ing goes wrong w
1ac00 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73  hile.    ** this
1ac01 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 65   is happening we
1ac02 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 61   don't really ca
1ac03 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74  re. The integrit
1ac04 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  y of the.    ** 
1ac05 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
1ac06 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 65 65  lready guarantee
1ac07 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72 61  d, but some stra
1ac08 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c  y 'cold' journal
1ac09 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 20  s.    ** may be 
1ac0a 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65  lying around. Re
1ac0b 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72  turning an error
1ac0c 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c 70   code won't help
1ac0d 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f   matters..    */
1ac0e 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69 6d  .    disable_sim
1ac0f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
1ac10 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ();.    sqlite3B
1ac11 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
1ac12 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ();.    for(i=0;
1ac13 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1ac14 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  { .      Btree *
1ac15 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
1ac16 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
1ac17 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  pBt ){.        s
1ac18 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1ac19 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a  tPhaseTwo(pBt);.
1ac1a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ac1b 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
1ac1c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
1ac1d 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  enable_simulated
1ac1e 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20  _io_errors();.. 
1ac1f 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f     sqlite3VtabCo
1ac20 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  mmit(db);.  }.#e
1ac21 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72  ndif..  return r
1ac22 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  c;.}../* .** Thi
1ac23 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
1ac24 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65   that the sqlite
1ac25 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  3.activeVdbeCnt 
1ac26 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a  count variable.*
1ac27 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75  * matches the nu
1ac28 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 69  mber of vdbe's i
1ac29 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 74  n the list sqlit
1ac2a 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 72  e3.pVdbe that ar
1ac2b 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 61  e.** currently a
1ac2c 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72 74  ctive. An assert
1ac2d 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68 65  ion fails if the
1ac2e 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e   two counts do n
1ac2f 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69  ot match..** Thi
1ac30 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c  s is an internal
1ac31 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79   self-check only
1ac32 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e 20   - it is not an 
1ac33 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65 73  essential proces
1ac34 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a  sing.** step..**
1ac35 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f  .** This is a no
1ac36 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69 73  -op if NDEBUG is
1ac37 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66   defined..*/.#if
1ac38 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74  ndef NDEBUG.stat
1ac39 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63 74  ic void checkAct
1ac3a 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69 74  iveVdbeCnt(sqlit
1ac3b 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20  e3 *db){.  Vdbe 
1ac3c 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20  *p;.  int cnt = 
1ac3d 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20  0;.  int nWrite 
1ac3e 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70  = 0;.  p = db->p
1ac3f 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 70  Vdbe;.  while( p
1ac40 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d   ){.    if( p->m
1ac41 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1ac42 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30  _RUN && p->pc>=0
1ac43 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b   ){.      cnt++;
1ac44 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 65  .      if( p->re
1ac45 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69  adOnly==0 ) nWri
1ac46 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  te++;.    }.    
1ac47 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
1ac48 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d  }.  assert( cnt=
1ac49 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43  =db->activeVdbeC
1ac4a 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nt );.  assert( 
1ac4b 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74  nWrite==db->writ
1ac4c 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65  eVdbeCnt );.}.#e
1ac4d 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65 63  lse.#define chec
1ac4e 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 78  kActiveVdbeCnt(x
1ac4f 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
1ac50 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 65 20  For every Btree 
1ac51 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 73 65  that in database
1ac52 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77   connection db w
1ac53 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65 65  hich .** has bee
1ac54 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72 69  n modified, "tri
1ac55 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65  p" or invalidate
1ac56 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e 0a   each cursor in.
1ac57 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d 69  ** that Btree mi
1ac58 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d 6f  ght have been mo
1ac59 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 20 74  dified so that t
1ac5a 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e  he cursor.** can
1ac5b 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61   never be used a
1ac5c 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 70  gain.  This happ
1ac5d 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62  ens when a rollb
1ac5e 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20  ack.*** occurs. 
1ac5f 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 70   We have to trip
1ac60 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63   all the other c
1ac61 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20  ursors, even.** 
1ac62 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65  cursor from othe
1ac63 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72 65  r VMs in differe
1ac64 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  nt database conn
1ac65 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74  ections,.** so t
1ac66 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d  hat none of them
1ac67 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 20   try to use the 
1ac68 64 61 74 61 20 61 74 20 77 68 69 63 68 20 74 68  data at which th
1ac69 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74  ey.** were point
1ac6a 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e 6f  ing and which no
1ac6b 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20  w may have been 
1ac6c 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74  changed due.** t
1ac6d 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a  o the rollback..
1ac6e 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74  **.** Remember t
1ac6f 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63  hat a rollback c
1ac70 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65 73  an delete tables
1ac71 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a   complete and.**
1ac72 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61 67   reorder rootpag
1ac73 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e 6f  es.  So it is no
1ac74 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75 73  t sufficient jus
1ac75 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65  t to save.** the
1ac76 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75   state of the cu
1ac77 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20 74  rsor.  We have t
1ac78 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  o invalidate the
1ac79 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68   cursor.** so th
1ac7a 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 75  at it is never u
1ac7b 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74  sed again..*/.st
1ac7c 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69  atic void invali
1ac7d 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64  dateCursorsOnMod
1ac7e 69 66 69 65 64 42 74 72 65 65 73 28 73 71 6c 69  ifiedBtrees(sqli
1ac7f 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
1ac80 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1ac81 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1ac82 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62     Btree *p = db
1ac83 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1ac84 20 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 74    if( p && sqlit
1ac85 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
1ac86 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  (p) ){.      sql
1ac87 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
1ac88 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 54  Cursors(p, SQLIT
1ac89 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a  E_ABORT);.    }.
1ac8a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20    }.}../*.** If 
1ac8b 74 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20  the Vdbe passed 
1ac8c 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
1ac8d 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20 73  ument opened a s
1ac8e 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
1ac8f 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69  tion,.** close i
1ac90 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20  t now. Argument 
1ac91 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74 68  eOp must be eith
1ac92 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  er SAVEPOINT_ROL
1ac93 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45  LBACK or.** SAVE
1ac94 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49  POINT_RELEASE. I
1ac95 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e  f it is SAVEPOIN
1ac96 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e  T_ROLLBACK, then
1ac97 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   the statement.*
1ac98 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
1ac99 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66   rolled back. If
1ac9a 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49 4e   eOp is SAVEPOIN
1ac9b 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20  T_RELEASE, then 
1ac9c 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  the .** statemen
1ac9d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t transaction is
1ac9e 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a   commtted..**.**
1ac9f 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
1aca0 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49 54  occurs, an SQLIT
1aca1 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f  E_IOERR_XXX erro
1aca2 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
1aca3 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed. .** Otherwis
1aca4 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  e SQLITE_OK..*/.
1aca5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1aca6 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c  nt sqlite3VdbeCl
1aca7 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62  oseStatement(Vdb
1aca8 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a  e *p, int eOp){.
1aca9 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74    sqlite3 *const
1acaa 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69   db = p->db;.  i
1acab 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1acac 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69  K;..  /* If p->i
1acad 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72 65  Statement is gre
1acae 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20  ater than zero, 
1acaf 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 20 6f  then this Vdbe o
1acb0 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73 74  pened a .  ** st
1acb1 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1acb2 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ion that should 
1acb3 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e 20  be closed here. 
1acb4 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69  The only excepti
1acb5 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74 20  on.  ** is that 
1acb6 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79 20  an IO error may 
1acb7 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63 61  have occured, ca
1acb8 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e  using an emergen
1acb9 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a  cy rollback..  *
1acba 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 28  * In this case (
1acbb 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d  db->nStatement==
1acbc 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73  0), and there is
1acbd 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a   nothing to do..
1acbe 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e    */.  if( db->n
1acbf 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e  Statement && p->
1acc0 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20  iStatement ){.  
1acc1 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e    int i;.    con
1acc2 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  st int iSavepoin
1acc3 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  t = p->iStatemen
1acc4 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  t-1;..    assert
1acc5 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  ( eOp==SAVEPOINT
1acc6 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70  _ROLLBACK || eOp
1acc7 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
1acc8 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ASE);.    assert
1acc9 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  ( db->nStatement
1acca 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
1accb 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d  ( p->iStatement=
1accc 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  =(db->nStatement
1accd 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29  +db->nSavepoint)
1acce 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30   );..    for(i=0
1accf 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1acd0 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 63  ){ .      int rc
1acd1 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  2 = SQLITE_OK;. 
1acd2 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
1acd3 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1acd4 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
1acd5 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65  ){.        if( e
1acd6 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  Op==SAVEPOINT_RO
1acd7 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20  LLBACK ){.      
1acd8 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65      rc2 = sqlite
1acd9 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
1acda 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52  pBt, SAVEPOINT_R
1acdb 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70 6f  OLLBACK, iSavepo
1acdc 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  int);.        }.
1acdd 20 20 20 20 20 20 20 20 69 66 28 20 72 63 32 3d          if( rc2=
1acde 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1acdf 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71          rc2 = sq
1ace0 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f  lite3BtreeSavepo
1ace1 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49  int(pBt, SAVEPOI
1ace2 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61 76  NT_RELEASE, iSav
1ace3 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  epoint);.       
1ace4 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
1ace5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1ace6 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 72            rc = r
1ace7 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c2;.        }.  
1ace8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1ace9 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d  db->nStatement--
1acea 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d  ;.    p->iStatem
1aceb 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a  ent = 0;..    /*
1acec 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   If the statemen
1aced 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t transaction is
1acee 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61   being rolled ba
1acef 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72 65  ck, also restore
1acf0 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74   the .    ** dat
1acf1 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65  abase handles de
1acf2 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
1acf3 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68 65  t counter to the
1acf4 20 76 61 6c 75 65 20 69 74 20 68 61 64 20 77 68   value it had wh
1acf5 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  en .    ** the s
1acf6 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1acf7 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e  tion was opened.
1acf8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f 70    */.    if( eOp
1acf9 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
1acfa 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64 62  BACK ){.      db
1acfb 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20  ->nDeferredCons 
1acfc 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e  = p->nStmtDefCon
1acfd 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  s;.    }.  }.  r
1acfe 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1acff 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  ** If SQLite is 
1ad00 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70 70  compiled to supp
1ad01 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65  ort shared-cache
1ad02 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65 20   mode and to be 
1ad03 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74  threadsafe,.** t
1ad04 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 61  his routine obta
1ad05 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61 73  ins the mutex as
1ad06 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61  sociated with ea
1ad07 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75  ch BtShared stru
1ad08 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61  cture.** that ma
1ad09 79 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  y be accessed by
1ad0a 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 20 61   the VM passed a
1ad0b 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49  s an argument. I
1ad0c 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a  n doing so it.**
1ad0d 20 73 65 74 73 20 74 68 65 20 42 74 53 68 61 72   sets the BtShar
1ad0e 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66 20  ed.db member of 
1ad0f 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 53 68  each of the BtSh
1ad10 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 2c  ared structures,
1ad11 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61   ensuring.** tha
1ad12 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 75  t the correct bu
1ad13 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  sy-handler callb
1ad14 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69  ack is invoked i
1ad15 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a  f required..**.*
1ad16 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e  * If SQLite is n
1ad17 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 62 75  ot threadsafe bu
1ad18 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 73  t does support s
1ad19 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65  hared-cache mode
1ad1a 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65  , then.** sqlite
1ad1b 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29  3BtreeEnterAll()
1ad1c 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73   is invoked to s
1ad1d 65 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  et the BtShared.
1ad1e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20  db variables.** 
1ad1f 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61 72  of all of BtShar
1ad20 65 64 20 73 74 72 75 63 74 75 72 65 73 20 61 63  ed structures ac
1ad21 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65  cessible via the
1ad22 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1ad23 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20   .** associated 
1ad24 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66 20  with the VM. Of 
1ad25 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73 75  course only a su
1ad26 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 73 74  bset of these st
1ad27 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c  ructures.** will
1ad28 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20   be accessed by 
1ad29 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20 63  the VM, and we c
1ad2a 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62 74  ould use Vdbe.bt
1ad2b 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75 72  reeMask to figur
1ad2c 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65 74  e.** that subset
1ad2d 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65 20   out, but there 
1ad2e 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65 20  is no advantage 
1ad2f 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a  to doing so..**.
1ad30 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  ** If SQLite is 
1ad31 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 61  not threadsafe a
1ad32 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70  nd does not supp
1ad33 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65  ort shared-cache
1ad34 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66   mode, this.** f
1ad35 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
1ad36 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  op..*/.#ifndef S
1ad37 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1ad38 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50  D_CACHE.SQLITE_P
1ad39 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1ad3a 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61  te3VdbeMutexArra
1ad3b 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 7b  yEnter(Vdbe *p){
1ad3c 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
1ad3d 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
1ad3e 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
1ad3f 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 29  nter(&p->aMutex)
1ad40 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65  ;.#else.  sqlite
1ad41 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 70  3BtreeEnterAll(p
1ad42 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  ->db);.#endif.}.
1ad43 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1ad44 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1ad45 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61  alled when a tra
1ad46 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20  nsaction opened 
1ad47 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  by the database 
1ad48 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63  .** handle assoc
1ad49 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56  iated with the V
1ad4a 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  M passed as an a
1ad4b 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74  rgument is about
1ad4c 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69   to be .** commi
1ad4d 74 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61  tted. If there a
1ad4e 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64  re outstanding d
1ad4f 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20  eferred foreign 
1ad50 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  key constraint.*
1ad51 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65  * violations, re
1ad52 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1ad53 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51  R. Otherwise, SQ
1ad54 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49  LITE_OK..**.** I
1ad55 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  f there are outs
1ad56 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61  tanding FK viola
1ad57 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66  tions and this f
1ad58 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
1ad59 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  .** SQLITE_ERROR
1ad5a 2c 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74  , set the result
1ad5b 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51   of the VM to SQ
1ad5c 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
1ad5d 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20  and write.** an 
1ad5e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
1ad5f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e   it. Then return
1ad60 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a   SQLITE_ERROR..*
1ad61 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1ad62 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
1ad63 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  Y.SQLITE_PRIVATE
1ad64 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1ad65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c  CheckFk(Vdbe *p,
1ad66 20 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a   int deferred){.
1ad67 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1ad68 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65  p->db;.  if( (de
1ad69 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44  ferred && db->nD
1ad6a 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c  eferredCons>0) |
1ad6b 7c 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20  | (!deferred && 
1ad6c 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74  p->nFkConstraint
1ad6d 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  >0) ){.    p->rc
1ad6e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
1ad6f 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72  AINT;.    p->err
1ad70 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62  orAction = OE_Ab
1ad71 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ort;.    sqlite3
1ad72 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1ad73 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65  rrMsg, db, "fore
1ad74 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
1ad75 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20  nt failed");.   
1ad76 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1ad77 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75  RROR;.  }.  retu
1ad78 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1ad79 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1ad7a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
1ad7b 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20  lled the when a 
1ad7c 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61  VDBE tries to ha
1ad7d 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45  lt.  If the VDBE
1ad7e 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61  .** has made cha
1ad7f 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61  nges and is in a
1ad80 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20  utocommit mode, 
1ad81 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73  then commit thos
1ad82 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49  e.** changes.  I
1ad83 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20  f a rollback is 
1ad84 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20  needed, then do 
1ad85 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  the rollback..**
1ad86 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1ad87 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79   is the only way
1ad88 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61   to move the sta
1ad89 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a  te of a VM from.
1ad8a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ** SQLITE_MAGIC_
1ad8b 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41  RUN to SQLITE_MA
1ad8c 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73  GIC_HALT.  It is
1ad8d 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20   harmless to.** 
1ad8e 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56  call this on a V
1ad8f 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65  M that is in the
1ad90 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41   SQLITE_MAGIC_HA
1ad91 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  LT state..**.** 
1ad92 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
1ad93 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f  code.  If the co
1ad94 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63  mmit could not c
1ad95 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20  omplete because 
1ad96 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65  of.** lock conte
1ad97 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51  ntion, return SQ
1ad98 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53  LITE_BUSY.  If S
1ad99 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65  QLITE_BUSY is re
1ad9a 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65  turned, it.** me
1ad9b 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69  ans the close di
1ad9c 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64  d not happen and
1ad9d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70   needs to be rep
1ad9e 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eated..*/.SQLITE
1ad9f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ada0 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62  ite3VdbeHalt(Vdb
1ada1 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
1ada2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ada3 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
1ada4 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69   to store transi
1ada5 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73  ent return codes
1ada6 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
1ada7 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  b = p->db;..  /*
1ada8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
1ada9 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69  ontains the logi
1adaa 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  c that determine
1adab 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74  s if a statement
1adac 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63   or.  ** transac
1adad 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d  tion will be com
1adae 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
1adaf 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c   back as a resul
1adb0 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78  t of the.  ** ex
1adb1 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ecution of this 
1adb2 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
1adb3 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61   .  **.  ** If a
1adb4 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ny of the follow
1adb5 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72  ing errors occur
1adb6 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
1adb7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a  SQLITE_NOMEM.  *
1adb8 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45  *     SQLITE_IOE
1adb9 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49  RR.  **     SQLI
1adba 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20  TE_FULL.  **    
1adbb 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
1adbc 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e  T.  **.  ** Then
1adbd 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61   the internal ca
1adbe 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62  che might have b
1adbf 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69  een left in an i
1adc0 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a  nconsistent.  **
1adc1 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64   state.  We need
1adc2 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   to rollback the
1adc3 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1adc4 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65  action, if there
1adc5 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72   is.  ** one, or
1adc6 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72   the complete tr
1adc7 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65  ansaction if the
1adc8 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65  re is no stateme
1adc9 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  nt transaction..
1adca 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64    */..  if( p->d
1adcb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1adcc 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  ){.    p->rc = S
1adcd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
1adce 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f  .  closeAllCurso
1adcf 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e  rs(p);.  if( p->
1add0 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49  magic!=VDBE_MAGI
1add1 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74  C_RUN ){.    ret
1add2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1add3 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65   }.  checkActive
1add4 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20  VdbeCnt(db);..  
1add5 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20  /* No commit or 
1add6 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20  rollback needed 
1add7 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e  if the program n
1add8 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a  ever started */.
1add9 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29    if( p->pc>=0 )
1adda 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20  {.    int mrc;  
1addb 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f   /* Primary erro
1addc 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72  r code from p->r
1addd 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74  c */.    int eSt
1adde 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20  atementOp = 0;. 
1addf 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c     int isSpecial
1ade0 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20  Error;          
1ade1 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65    /* Set to true
1ade2 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20   if a 'special' 
1ade3 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a  error */..    /*
1ade4 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73   Lock all btrees
1ade5 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61   used by the sta
1ade6 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71  tement */.    sq
1ade7 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72  lite3VdbeMutexAr
1ade8 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20  rayEnter(p);..  
1ade9 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f    /* Check for o
1adea 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61  ne of the specia
1adeb 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20  l errors */.    
1adec 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 78  mrc = p->rc & 0x
1aded 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ff;.    assert( 
1adee 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  p->rc!=SQLITE_IO
1adef 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20  ERR_BLOCKED );  
1adf0 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e 6f  /* This error no
1adf1 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 2a   longer exists *
1adf2 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c 45  /.    isSpecialE
1adf3 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49  rror = mrc==SQLI
1adf4 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d  TE_NOMEM || mrc=
1adf5 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20  =SQLITE_IOERR.  
1adf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1adf7 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54     || mrc==SQLIT
1adf8 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d  E_INTERRUPT || m
1adf9 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  rc==SQLITE_FULL;
1adfa 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63 69  .    if( isSpeci
1adfb 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  alError ){.     
1adfc 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79   /* If the query
1adfd 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20   was read-only, 
1adfe 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f  we need do no ro
1adff 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f  llback at all. O
1ae00 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 20  therwise,.      
1ae01 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 20  ** proceed with 
1ae02 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64  the special hand
1ae03 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ling..      */. 
1ae04 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61       if( !p->rea
1ae05 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51  dOnly || mrc!=SQ
1ae06 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29  LITE_INTERRUPT )
1ae07 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d  {.        if( (m
1ae08 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1ae09 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f   || mrc==SQLITE_
1ae0a 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65 73  FULL) && p->uses
1ae0b 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  StmtJournal ){. 
1ae0c 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d           eStatem
1ae0d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e  entOp = SAVEPOIN
1ae0e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20  T_ROLLBACK;.    
1ae0f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ae10 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66       /* We are f
1ae11 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61  orced to roll ba
1ae12 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 72  ck the active tr
1ae13 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72  ansaction. Befor
1ae14 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20  e doing.        
1ae15 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61    ** so, abort a
1ae16 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65  ny other stateme
1ae17 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20  nts this handle 
1ae18 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63  currently has ac
1ae19 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20  tive..          
1ae1a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76  */.          inv
1ae1b 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e  alidateCursorsOn
1ae1c 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64  ModifiedBtrees(d
1ae1d 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
1ae1e 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
1ae1f 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1ae20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
1ae21 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  points(db);.    
1ae22 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
1ae23 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  mmit = 1;.      
1ae24 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1ae25 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  }..    /* Check 
1ae26 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66 6f  for immediate fo
1ae27 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74  reign key violat
1ae28 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ions. */.    if(
1ae29 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
1ae2a 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
1ae2b 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c  e3VdbeCheckFk(p,
1ae2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20   0);.    }.  .  
1ae2d 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f    /* If the auto
1ae2e 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20  -commit flag is 
1ae2f 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20  set and this is 
1ae30 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20  the only active 
1ae31 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 56  writer .    ** V
1ae32 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 69  M, then we do ei
1ae33 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72  ther a commit or
1ae34 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65   rollback of the
1ae35 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1ae36 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20  tion. .    **.  
1ae37 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20    ** Note: This 
1ae38 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20  block also runs 
1ae39 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70  if one of the sp
1ae3a 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e  ecial errors han
1ae3b 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f  dled .    ** abo
1ae3c 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  ve has occurred.
1ae3d 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28   .    */.    if(
1ae3e 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53   !sqlite3VtabInS
1ae3f 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 26  ync(db) .     &&
1ae40 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1ae41 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 69  .     && db->wri
1ae42 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72  teVdbeCnt==(p->r
1ae43 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20  eadOnly==0) .   
1ae44 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   ){.      if( p-
1ae45 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1ae46 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f  | (p->errorActio
1ae47 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69  n==OE_Fail && !i
1ae48 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29  sSpecialError) )
1ae49 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
1ae4a 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1ae4b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20  (p, 1) ){.      
1ae4c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1ae4d 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28  MutexArrayLeave(
1ae4e 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20  &p->aMutex);.   
1ae4f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
1ae50 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1ae51 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
1ae52 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   The auto-commit
1ae53 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74   flag is true, t
1ae54 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20  he vdbe program 
1ae55 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a  was successful .
1ae56 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69          ** or hi
1ae57 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63  t an 'OR FAIL' c
1ae58 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68  onstraint and th
1ae59 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72  ere are no defer
1ae5a 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20  red foreign.    
1ae5b 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74      ** key const
1ae5c 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75  raints to hold u
1ae5d 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  p the transactio
1ae5e 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20  n. This means a 
1ae5f 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20  commit .        
1ae60 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  ** is required. 
1ae61 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
1ae62 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20   vdbeCommit(db, 
1ae63 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  p);.        if( 
1ae64 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1ae65 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1ae66 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1ae67 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75  rayLeave(&p->aMu
1ae68 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  tex);.          
1ae69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
1ae6a 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  SY;.        }els
1ae6b 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45  e if( rc!=SQLITE
1ae6c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1ae6d 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20   p->rc = rc;.   
1ae6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f         sqlite3Ro
1ae6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20  llbackAll(db);. 
1ae70 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1ae71 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66          db->nDef
1ae72 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20  erredCons = 0;. 
1ae73 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1ae74 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
1ae75 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20  anges(db);.     
1ae76 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1ae77 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1ae78 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1ae79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ae7a 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d  db->nStatement =
1ae7b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
1ae7c 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d  ( eStatementOp==
1ae7d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
1ae7e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1ae7f 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f  || p->errorActio
1ae80 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20  n==OE_Fail ){.  
1ae81 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74        eStatement
1ae82 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52  Op = SAVEPOINT_R
1ae83 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65  ELEASE;.      }e
1ae84 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72  lse if( p->error
1ae85 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74  Action==OE_Abort
1ae86 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61   ){.        eSta
1ae87 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50  tementOp = SAVEP
1ae88 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20  OINT_ROLLBACK;. 
1ae89 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1ae8a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75      invalidateCu
1ae8b 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42  rsorsOnModifiedB
1ae8c 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20  trees(db);.     
1ae8d 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61     sqlite3Rollba
1ae8e 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  ckAll(db);.     
1ae8f 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53     sqlite3CloseS
1ae90 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20  avepoints(db);. 
1ae91 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1ae92 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  ommit = 1;.     
1ae93 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
1ae94 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74  /* If eStatement
1ae95 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  Op is non-zero, 
1ae96 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  then a statement
1ae97 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65   transaction nee
1ae98 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20  ds to.    ** be 
1ae99 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c  committed or rol
1ae9a 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73  led back. Call s
1ae9b 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53  qlite3VdbeCloseS
1ae9c 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20  tatement() to.  
1ae9d 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74    ** do so. If t
1ae9e 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65  his operation re
1ae9f 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20  turns an error, 
1aea0 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
1aea1 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a  statement.    **
1aea2 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53   error code is S
1aea3 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49  QLITE_OK or SQLI
1aea4 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74  TE_CONSTRAINT, t
1aea5 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f  hen set the erro
1aea6 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f  r.    ** code to
1aea7 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a   the new value..
1aea8 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65      */.    if( e
1aea9 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20  StatementOp ){. 
1aeaa 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1aeab 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d  3VdbeCloseStatem
1aeac 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e  ent(p, eStatemen
1aead 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  tOp);.      if( 
1aeae 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51  rc && (p->rc==SQ
1aeaf 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1aeb0 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  ==SQLITE_CONSTRA
1aeb1 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20  INT) ){.        
1aeb2 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  p->rc = rc;.    
1aeb3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1aeb4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1aeb5 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45  );.        p->zE
1aeb6 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20  rrMsg = 0;.     
1aeb7 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
1aeb8 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61  /* If this was a
1aeb9 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  n INSERT, UPDATE
1aeba 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e   or DELETE and n
1aebb 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  o statement tran
1aebc 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68  saction.    ** h
1aebd 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62  as been rolled b
1aebe 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20  ack, update the 
1aebf 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1aec0 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  ion change-count
1aec1 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  er. .    */.    
1aec2 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74  if( p->changeCnt
1aec3 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  On ){.      if( 
1aec4 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41  eStatementOp!=SA
1aec5 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
1aec6 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1aec7 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65  te3VdbeSetChange
1aec8 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65  s(db, p->nChange
1aec9 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1aeca 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1aecb 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62  dbeSetChanges(db
1aecc 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1aecd 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d      p->nChange =
1aece 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20   0;.    }.  .   
1aecf 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20   /* Rollback or 
1aed0 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d  commit any schem
1aed1 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f  a changes that o
1aed2 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20  ccurred. */.    
1aed3 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54  if( p->rc!=SQLIT
1aed4 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67  E_OK && db->flag
1aed5 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  s&SQLITE_InternC
1aed6 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20  hanges ){.      
1aed7 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
1aed8 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
1aed9 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61  );.      db->fla
1aeda 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20  gs = (db->flags 
1aedb 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  | SQLITE_InternC
1aedc 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a  hanges);.    }..
1aedd 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74      /* Release t
1aede 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20  he locks */.    
1aedf 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
1aee0 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
1aee1 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20  aMutex);.  }..  
1aee2 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65  /* We have succe
1aee3 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61  ssfully halted a
1aee4 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d  nd closed the VM
1aee5 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66  .  Record this f
1aee6 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d  act. */.  if( p-
1aee7 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62  >pc>=0 ){.    db
1aee8 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d  ->activeVdbeCnt-
1aee9 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72  -;.    if( !p->r
1aeea 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  eadOnly ){.     
1aeeb 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
1aeec 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  t--;.    }.    a
1aeed 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76  ssert( db->activ
1aeee 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72  eVdbeCnt>=db->wr
1aeef 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20  iteVdbeCnt );.  
1aef0 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56  }.  p->magic = V
1aef1 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a  DBE_MAGIC_HALT;.
1aef2 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62    checkActiveVdb
1aef3 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20  eCnt(db);.  if( 
1aef4 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
1aef5 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  led ){.    p->rc
1aef6 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1aef7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
1aef8 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c  e auto-commit fl
1aef9 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ag is set to tru
1aefa 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b  e, then any lock
1aefb 73 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64  s that were held
1aefc 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74  .  ** by connect
1aefd 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20  ion db have now 
1aefe 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43  been released. C
1aeff 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  all sqlite3Conne
1af00 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20  ctionUnlocked() 
1af01 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20  .  ** to invoke 
1af02 61 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c  any required unl
1af03 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
1af04 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  acks..  */.  if(
1af05 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1af06 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
1af07 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
1af08 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  (db);.  }..  ass
1af09 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56  ert( db->activeV
1af0a 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e  dbeCnt>0 || db->
1af0b 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c  autoCommit==0 ||
1af0c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d   db->nStatement=
1af0d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  =0 );.  return S
1af0e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
1af0f 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f  .** Each VDBE ho
1af10 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f  lds the result o
1af11 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
1af12 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  t sqlite3_step()
1af13 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72   call.** in p->r
1af14 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  c.  This routine
1af15 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c   sets that resul
1af16 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45  t back to SQLITE
1af17 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
1af18 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1af19 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70  te3VdbeResetStep
1af1a 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b  Result(Vdbe *p){
1af1b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1af1c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
1af1d 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61  lean up a VDBE a
1af1e 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62  fter execution b
1af1f 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65  ut do not delete
1af20 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79   the VDBE just y
1af21 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79  et..** Write any
1af22 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
1af23 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20  into *pzErrMsg. 
1af24 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75   Return the resu
1af25 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41  lt code..**.** A
1af26 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  fter this routin
1af27 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44  e is run, the VD
1af28 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61  BE should be rea
1af29 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65  dy to be execute
1af2a 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a  d.** again..**.*
1af2b 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20  * To look at it 
1af2c 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69  another way, thi
1af2d 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73  s routine resets
1af2e 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
1af2f 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63  e.** virtual mac
1af30 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d  hine from VDBE_M
1af31 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45  AGIC_RUN or VDBE
1af32 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b  _MAGIC_HALT back
1af33 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49   to.** VDBE_MAGI
1af34 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54  C_INIT..*/.SQLIT
1af35 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1af36 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56  lite3VdbeReset(V
1af37 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  dbe *p){.  sqlit
1af38 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70  e3 *db;.  db = p
1af39 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  ->db;..  /* If t
1af3a 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75  he VM did not ru
1af3b 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20  n to completion 
1af3c 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74  or if it encount
1af3d 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72  ered an.  ** err
1af3e 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68  or, then it migh
1af3f 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20  t not have been 
1af40 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e  halted properly.
1af41 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69    So halt.  ** i
1af42 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76  t now..  */.  (v
1af43 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1af44 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  yOn(db);.  sqlit
1af45 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
1af46 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1af47 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20  fetyOff(db);..  
1af48 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68  /* If the VDBE h
1af49 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70  as be run even p
1af4a 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74  artially, then t
1af4b 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f  ransfer the erro
1af4c 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20  r code.  ** and 
1af4d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72  error message fr
1af4e 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f  om the VDBE into
1af4f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1af50 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42  se structure.  B
1af51 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56  ut.  ** if the V
1af52 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65  DBE has just bee
1af53 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74  n set to run but
1af54 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c   has not actuall
1af55 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20  y executed any. 
1af56 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   ** instructions
1af57 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20   yet, leave the 
1af58 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72  main database er
1af59 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ror information 
1af5a 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a  unchanged..  */.
1af5b 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29    if( p->pc>=0 )
1af5c 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72  {.    if( p->zEr
1af5d 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71  rMsg ){.      sq
1af5e 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
1af5f 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
1af60 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
1af61 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70  tr(db->pErr,-1,p
1af62 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45  ->zErrMsg,SQLITE
1af63 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41  _UTF8,SQLITE_TRA
1af64 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73  NSIENT);.      s
1af65 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1af66 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64  alloc();.      d
1af67 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e  b->errCode = p->
1af68 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
1af69 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1af6a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70  ErrMsg);.      p
1af6b 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1af6c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
1af6d 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
1af6e 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e  te3Error(db, p->
1af6f 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  rc, 0);.    }els
1af70 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
1af71 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
1af72 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  _OK, 0);.    }. 
1af73 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63   }else if( p->rc
1af74 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29   && p->expired )
1af75 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70  {.    /* The exp
1af76 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65  ired flag was se
1af77 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65  t on the VDBE be
1af78 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63  fore the first c
1af79 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71  all.    ** to sq
1af7a 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f  lite3_step(). Fo
1af7b 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73  r consistency (s
1af7c 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65  ince sqlite3_ste
1af7d 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63  p() was.    ** c
1af7e 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20  alled), set the 
1af7f 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69  database error i
1af80 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77  n this case as w
1af81 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ell..    */.    
1af82 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1af83 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20   p->rc, 0);.    
1af84 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
1af85 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c  tr(db->pErr, -1,
1af86 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c   p->zErrMsg, SQL
1af87 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
1af88 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
1af89 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1af8a 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1af8b 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d      p->zErrMsg =
1af8c 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65   0;.  }..  /* Re
1af8d 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
1af8e 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42   used by the VDB
1af8f 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70  E.  */.  Cleanup
1af90 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20  (p);..  /* Save 
1af91 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d  profiling inform
1af92 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20  ation from this 
1af93 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23  VDBE run..  */.#
1af94 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
1af95 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20  LE.  {.    FILE 
1af96 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64  *out = fopen("vd
1af97 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c  be_profile.out",
1af98 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f   "a");.    if( o
1af99 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ut ){.      int 
1af9a 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  i;.      fprintf
1af9b 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a  (out, "---- ");.
1af9c 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
1af9d 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20  <p->nOp; i++){. 
1af9e 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f         fprintf(o
1af9f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61  ut, "%02x", p->a
1afa0 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20  Op[i].opcode);. 
1afa1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72       }.      fpr
1afa2 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
1afa3 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
1afa4 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a  i<p->nOp; i++){.
1afa5 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
1afa6 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64  out, "%6d %10lld
1afa7 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20   %8lld ",.      
1afa8 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63       p->aOp[i].c
1afa9 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70  nt,.           p
1afaa 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c  ->aOp[i].cycles,
1afab 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  .           p->a
1afac 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d  Op[i].cnt>0 ? p-
1afad 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70  >aOp[i].cycles/p
1afae 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30  ->aOp[i].cnt : 0
1afaf 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
1afb0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1afb1 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26  rintOp(out, i, &
1afb2 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20  p->aOp[i]);.    
1afb3 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65    }.      fclose
1afb4 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (out);.    }.  }
1afb5 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67  .#endif.  p->mag
1afb6 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f  ic = VDBE_MAGIC_
1afb7 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70  INIT;.  return p
1afb8 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61  ->rc & db->errMa
1afb9 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c  sk;.}. ./*.** Cl
1afba 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74  ean up and delet
1afbb 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65  e a VDBE after e
1afbc 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72  xecution.  Retur
1afbd 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  n an integer whi
1afbe 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73  ch is.** the res
1afbf 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65  ult code.  Write
1afc0 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
1afc1 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a  ge text into *pz
1afc2 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ErrMsg..*/.SQLIT
1afc3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1afc4 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
1afc5 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  e(Vdbe *p){.  in
1afc6 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1afc7 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
1afc8 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  ==VDBE_MAGIC_RUN
1afc9 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44   || p->magic==VD
1afca 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b  BE_MAGIC_HALT ){
1afcb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1afcc 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20  3VdbeReset(p);. 
1afcd 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26     assert( (rc &
1afce 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29   p->db->errMask)
1afcf 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71  ==rc );.  }.  sq
1afd0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28  lite3VdbeDelete(
1afd1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
1afd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74  .}../*.** Call t
1afd3 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f  he destructor fo
1afd4 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65  r each auxdata e
1afd5 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e  ntry in pVdbeFun
1afd6 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74  c for which.** t
1afd7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1afd8 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20   bit in mask is 
1afd9 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20  clear.  Auxdata 
1afda 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33  entries beyond 3
1afdb 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20  1.** are always 
1afdc 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64  destroyed.  To d
1afdd 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61  estroy all auxda
1afde 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c  ta entries, call
1afdf 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
1afe0 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a   with mask==0..*
1afe1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1afe2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1afe3 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56  eDeleteAuxData(V
1afe4 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
1afe5 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20  nc, int mask){. 
1afe6 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1afe7 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e  0; i<pVdbeFunc->
1afe8 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nAux; i++){.    
1afe9 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a  struct AuxData *
1afea 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e  pAux = &pVdbeFun
1afeb 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20  c->apAux[i];.   
1afec 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28   if( (i>31 || !(
1afed 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c  mask&(((u32)1)<<
1afee 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41  i))) && pAux->pA
1afef 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ux ){.      if( 
1aff0 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b  pAux->xDelete ){
1aff1 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78  .        pAux->x
1aff2 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75  Delete(pAux->pAu
1aff3 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  x);.      }.    
1aff4 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30    pAux->pAux = 0
1aff5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
1aff6 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
1aff7 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53  ntire VDBE..*/.S
1aff8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1aff9 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
1affa 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20  lete(Vdbe *p){. 
1affb 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20   sqlite3 *db;.. 
1affc 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29   if( NEVER(p==0)
1affd 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
1affe 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  = p->db;.  if( p
1afff 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70  ->pPrev ){.    p
1b000 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
1b001 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c   p->pNext;.  }el
1b002 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
1b003 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a  db->pVdbe==p );.
1b004 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20      db->pVdbe = 
1b005 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
1b006 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a  if( p->pNext ){.
1b007 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50      p->pNext->pP
1b008 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  rev = p->pPrev;.
1b009 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d    }.  releaseMem
1b00a 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70  Array(p->aVar, p
1b00b 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61  ->nVar);.  relea
1b00c 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43  seMemArray(p->aC
1b00d 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43  olName, p->nResC
1b00e 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29  olumn*COLNAME_N)
1b00f 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72  ;.  vdbeFreeOpAr
1b010 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20  ray(db, p->aOp, 
1b011 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74  p->nOp);.  sqlit
1b012 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1b013 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74  aLabel);.  sqlit
1b014 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1b015 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  aColName);.  sql
1b016 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1b017 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61  ->zSql);.  p->ma
1b018 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
1b019 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33  _DEAD;.  sqlite3
1b01a 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46  DbFree(db, p->pF
1b01b 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ree);.  sqlite3D
1b01c 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a  bFree(db, p);.}.
1b01d 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
1b01e 20 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73   the cursor p is
1b01f 20 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f   ready to read o
1b020 72 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20  r write the row 
1b021 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77  to which it.** w
1b022 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e  as last position
1b023 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65  ed.  Return an e
1b024 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20  rror code if an 
1b025 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f  OOM fault or I/O
1b026 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e   error.** preven
1b027 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74  ts us from posit
1b028 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f  ioning the curso
1b029 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74  r to its correct
1b02a 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   position..**.**
1b02b 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65   If a MoveTo ope
1b02c 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e  ration is pendin
1b02d 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63  g on the given c
1b02e 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74  ursor, then do t
1b02f 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f  hat.** MoveTo no
1b030 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69  w.  If no move i
1b031 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b  s pending, check
1b032 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72   to see if the r
1b033 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64  ow has been.** d
1b034 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20  eleted out from 
1b035 75 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72  under the cursor
1b036 20 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20   and if it has, 
1b037 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a  mark the row as.
1b038 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a  ** a NULL row..*
1b039 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73  *.** If the curs
1b03a 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f  or is already po
1b03b 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f  inting to the co
1b03c 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68  rrect row and th
1b03d 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f  at row has.** no
1b03e 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f  t been deleted o
1b03f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68  ut from under th
1b040 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74  e cursor, then t
1b041 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1b042 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
1b043 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1b044 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1b045 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72  oveto(VdbeCursor
1b046 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64   *p){.  if( p->d
1b047 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b  eferredMoveto ){
1b048 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63  .    int res, rc
1b049 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1b04a 54 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20  TEST.    extern 
1b04b 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72  int sqlite3_sear
1b04c 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66  ch_count;.#endif
1b04d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1b04e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72  isTable );.    r
1b04f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1b050 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70  MovetoUnpacked(p
1b051 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d  ->pCursor, 0, p-
1b052 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30  >movetoTarget, 0
1b053 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28  , &res);.    if(
1b054 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1b055 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69  .    p->lastRowi
1b056 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72  d = p->movetoTar
1b057 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69  get;.    p->rowi
1b058 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59  dIsValid = ALWAY
1b059 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a  S(res==0) ?1:0;.
1b05a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65      if( NEVER(re
1b05b 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63  s<0) ){.      rc
1b05c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   = sqlite3BtreeN
1b05d 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20  ext(p->pCursor, 
1b05e 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28  &res);.      if(
1b05f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1b060 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51  .    }.#ifdef SQ
1b061 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71  LITE_TEST.    sq
1b062 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75  lite3_search_cou
1b063 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20  nt++;.#endif.   
1b064 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65   p->deferredMove
1b065 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63  to = 0;.    p->c
1b066 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1b067 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73  HE_STALE;.  }els
1b068 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e  e if( ALWAYS(p->
1b069 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
1b06a 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20  int hasMoved;.  
1b06b 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
1b06c 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73  e3BtreeCursorHas
1b06d 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72  Moved(p->pCursor
1b06e 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20  , &hasMoved);.  
1b06f 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1b070 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61  n rc;.    if( ha
1b071 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20  sMoved ){.      
1b072 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  p->cacheStatus =
1b073 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1b074 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d      p->nullRow =
1b075 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
1b076 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1b077 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
1b078 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
1b079 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  ns:.**.** sqlite
1b07a 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
1b07b 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65  ).** sqlite3Vdbe
1b07c 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a  SerialTypeLen().
1b07d 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  ** sqlite3VdbeSe
1b07e 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c  rialLen().** sql
1b07f 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75  ite3VdbeSerialPu
1b080 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  t().** sqlite3Vd
1b081 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a  beSerialGet().**
1b082 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20  .** encapsulate 
1b083 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65  the code that se
1b084 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20  rializes values 
1b085 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53  for storage in S
1b086 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e  QLite.** data an
1b087 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e  d index records.
1b088 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64   Each serialized
1b089 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20   value consists 
1b08a 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d  of a.** 'serial-
1b08b 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62  type' and a blob
1b08c 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65   of data. The se
1b08d 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20  rial type is an 
1b08e 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a  8-byte unsigned.
1b08f 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72  ** integer, stor
1b090 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a  ed as a varint..
1b091 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69  **.** In an SQLi
1b092 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c  te index record,
1b093 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
1b094 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63   is stored direc
1b095 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  tly before.** th
1b096 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74  e blob of data t
1b097 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e  hat it correspon
1b098 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c  ds to. In a tabl
1b099 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65  e record, all se
1b09a 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72  rial.** types ar
1b09b 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20  e stored at the 
1b09c 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63  start of the rec
1b09d 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f  ord, and the blo
1b09e 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a  bs of data at.**
1b09f 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20   the end. Hence 
1b0a0 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
1b0a1 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72  allow the caller
1b0a2 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a   to handle the.*
1b0a3 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e  * serial-type an
1b0a4 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65  d data blob sepe
1b0a5 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
1b0a6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
1b0a7 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
1b0a8 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20  various storage 
1b0a9 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61  classes for data
1b0aa 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c  :.**.**   serial
1b0ab 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74   type        byt
1b0ac 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20  es of data      
1b0ad 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  type.**   ------
1b0ae 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d  --------     ---
1b0af 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
1b0b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1b0b1 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20  **      0       
1b0b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
1b0b3 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a             NULL.
1b0b4 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
1b0b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
1b0b6 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
1b0b7 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
1b0b8 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
1b0b9 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
1b0ba 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
1b0bb 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ger.**      3   
1b0bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0bd 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73    3            s
1b0be 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1b0bf 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20        4         
1b0c0 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20              4   
1b0c1 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
1b0c2 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
1b0c3 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5               
1b0c4 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20        6         
1b0c5 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
1b0c6 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20  r.**      6     
1b0c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0c8 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  8            sig
1b0c9 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
1b0ca 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20      7           
1b0cb 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20            8     
1b0cc 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61         IEEE floa
1b0cd 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20  t.**      8     
1b0ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0cf 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74  0            Int
1b0d0 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a  eger constant 0.
1b0d1 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20  **      9       
1b0d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
1b0d3 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67             Integ
1b0d4 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a  er constant 1.**
1b0d5 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20       10,11      
1b0d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0d7 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65           reserve
1b0d8 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a  d for expansion.
1b0d9 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20  **    N>=12 and 
1b0da 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32  even       (N-12
1b0db 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a  )/2        BLOB.
1b0dc 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20  **    N>=13 and 
1b0dd 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33  odd        (N-13
1b0de 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a  )/2        text.
1b0df 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20  **.** The 8 and 
1b0e0 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64  9 types were add
1b0e1 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c  ed in 3.3.0, fil
1b0e2 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69  e format 4.  Pri
1b0e3 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f  or versions.** o
1b0e4 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  f SQLite will no
1b0e5 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f  t understand tho
1b0e6 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  se serial types.
1b0e7 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
1b0e8 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70  n the serial-typ
1b0e9 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20  e for the value 
1b0ea 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a  stored in pMem..
1b0eb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b0ec 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
1b0ed 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20  eSerialType(Mem 
1b0ee 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f  *pMem, int file_
1b0ef 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66  format){.  int f
1b0f0 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  lags = pMem->fla
1b0f1 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20  gs;.  int n;..  
1b0f2 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  if( flags&MEM_Nu
1b0f3 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
1b0f4 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   0;.  }.  if( fl
1b0f5 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  ags&MEM_Int ){. 
1b0f6 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
1b0f7 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20   whether to use 
1b0f8 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20  1, 2, 4, 6 or 8 
1b0f9 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65  bytes. */.#   de
1b0fa 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28  fine MAX_6BYTE (
1b0fb 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30  (((i64)0x0000800
1b0fc 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69  0)<<32)-1).    i
1b0fd 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69  64 i = pMem->u.i
1b0fe 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20  ;.    u64 u;.   
1b0ff 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74   if( file_format
1b100 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20  >=4 && (i&1)==i 
1b101 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1b102 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a  8+(u32)i;.    }.
1b103 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69      u = i<0 ? -i
1b104 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c   : i;.    if( u<
1b105 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b  =127 ) return 1;
1b106 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36  .    if( u<=3276
1b107 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20  7 ) return 2;.  
1b108 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37    if( u<=8388607
1b109 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20   ) return 3;.   
1b10a 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36   if( u<=21474836
1b10b 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20  47 ) return 4;. 
1b10c 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42     if( u<=MAX_6B
1b10d 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a  YTE ) return 5;.
1b10e 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20      return 6;.  
1b10f 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45  }.  if( flags&ME
1b110 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65  M_Real ){.    re
1b111 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73  turn 7;.  }.  as
1b112 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e  sert( pMem->db->
1b113 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
1b114 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d  flags&(MEM_Str|M
1b115 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20  EM_Blob) );.  n 
1b116 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28  = pMem->n;.  if(
1b117 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72   flags & MEM_Zer
1b118 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d  o ){.    n += pM
1b119 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d  em->u.nZero;.  }
1b11a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20  .  assert( n>=0 
1b11b 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a  );.  return ((n*
1b11c 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67  2) + 12 + ((flag
1b11d 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b  s&MEM_Str)!=0));
1b11e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1b11f 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
1b120 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f  he data correspo
1b121 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70  nding to the sup
1b122 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70  plied serial-typ
1b123 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b124 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
1b125 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1b126 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70  n(u32 serial_typ
1b127 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c  e){.  if( serial
1b128 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20  _type>=12 ){.   
1b129 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f   return (serial_
1b12a 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65  type-12)/2;.  }e
1b12b 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
1b12c 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d  const u8 aSize[]
1b12d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c   = { 0, 1, 2, 3,
1b12e 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20   4, 6, 8, 8, 0, 
1b12f 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72  0, 0, 0 };.    r
1b130 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69  eturn aSize[seri
1b131 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a  al_type];.  }.}.
1b132 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
1b133 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74   on an architect
1b134 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65  ure with mixed-e
1b135 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a  ndian floating .
1b136 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41  ** points (ex: A
1b137 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74  RM7) then swap t
1b138 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73  he lower 4 bytes
1b139 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70   with the .** up
1b13a 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65  per 4 bytes.  Re
1b13b 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e  turn the result.
1b13c 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20  .**.** For most 
1b13d 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74  architectures, t
1b13e 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
1b13f 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20  **.** (later):  
1b140 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74  It is reported t
1b141 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69  o me that the mi
1b142 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c  xed-endian probl
1b143 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73  em.** on ARM7 is
1b144 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47   an issue with G
1b145 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65  CC, not with the
1b146 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20   ARM7 chip.  It 
1b147 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61  seems.** that ea
1b148 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  rly versions of 
1b149 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74  GCC stored the t
1b14a 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34  wo words of a 64
1b14b 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e  -bit.** float in
1b14c 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72   the wrong order
1b14d 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f  .  And that erro
1b14e 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61  r has been propa
1b14f 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69  gated.** ever si
1b150 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20  nce.  The blame 
1b151 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
1b152 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f  ly with GCC, tho
1b153 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68  ugh..** GCC migh
1b154 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79  t have just copy
1b155 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ing the problem 
1b156 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d  from a prior com
1b157 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61  piler..** I am a
1b158 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65  lso told that ne
1b159 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  wer versions of 
1b15a 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  GCC that follow 
1b15b 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41  a different.** A
1b15c 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20  BI get the byte 
1b15d 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a  order right..**.
1b15e 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73  ** Developers us
1b15f 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e  ing SQLite on an
1b160 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d   ARM7 should com
1b161 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65  pile and run the
1b162 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  ir.** applicatio
1b163 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45  n using -DSQLITE
1b164 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73  _DEBUG=1 at leas
1b165 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45  t once.  With DE
1b166 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  BUG.** enabled, 
1b167 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c  some asserts bel
1b168 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74  ow will ensure t
1b169 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64  hat the byte ord
1b16a 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e  er of.** floatin
1b16b 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69  g point values i
1b16c 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a  s correct..**.**
1b16d 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46   (2007-08-30)  F
1b16e 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61  rank van Vugt ha
1b16f 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70  s studied this p
1b170 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a  roblem closely.*
1b171 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68  * and has send h
1b172 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74  is findings to t
1b173 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f  he SQLite develo
1b174 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20  pers.  Frank.** 
1b175 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65  writes that some
1b176 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f   Linux kernels o
1b177 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f  ffer floating po
1b178 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20  int hardware.** 
1b179 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75  emulation that u
1b17a 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20  ses only 32-bit 
1b17b 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61  mantissas instea
1b17c 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20  d of a full .** 
1b17d 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69  48-bits as requi
1b17e 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20  red by the IEEE 
1b17f 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73  standard.  (This
1b180 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49   is the.** CONFI
1b181 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70  G_FPE_FASTFPE op
1b182 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20  tion.)  On such 
1b183 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e  systems, floatin
1b184 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20  g point.** byte 
1b185 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73  swapping becomes
1b186 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65   very complicate
1b187 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f  d.  To avoid pro
1b188 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65  blems,.** the ne
1b189 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61  cessary byte swa
1b18a 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64  pping is carried
1b18b 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d   out using a 64-
1b18c 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72  bit integer.** r
1b18d 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d  ather than a 64-
1b18e 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e  bit float.  Fran
1b18f 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61  k assures us tha
1b190 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a  t the code here.
1b191 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d  ** works for him
1b192 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c  .  We, the devel
1b193 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77  opers, have no w
1b194 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e  ay to independen
1b195 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68  tly.** verify th
1b196 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65  is, but Frank se
1b197 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74  ems to know what
1b198 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61   he is talking a
1b199 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72  bout.** so we tr
1b19a 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64  ust him..*/.#ifd
1b19b 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
1b19c 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
1b19d 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c  AT.static u64 fl
1b19e 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b  oatSwap(u64 in){
1b19f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75  .  union {.    u
1b1a0 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b  64 r;.    u32 i[
1b1a1 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32  2];.  } u;.  u32
1b1a2 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b   t;..  u.r = in;
1b1a3 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20  .  t = u.i[0];. 
1b1a4 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d   u.i[0] = u.i[1]
1b1a5 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a  ;.  u.i[1] = t;.
1b1a6 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a    return u.r;.}.
1b1a7 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78  # define swapMix
1b1a8 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29  edEndianFloat(X)
1b1a9 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28    X = floatSwap(
1b1aa 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  X).#else.# defin
1b1ab 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61  e swapMixedEndia
1b1ac 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66  nFloat(X).#endif
1b1ad 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ../*.** Write th
1b1ae 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74  e serialized dat
1b1af 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76  a blob for the v
1b1b0 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70  alue stored in p
1b1b1 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66  Mem into .** buf
1b1b2 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  . It is assumed 
1b1b3 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
1b1b4 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75  has allocated su
1b1b5 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a  fficient space..
1b1b6 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1b1b7 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72  mber of bytes wr
1b1b8 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75  itten..**.** nBu
1b1b9 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20  f is the amount 
1b1ba 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e  of space left in
1b1bb 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75   buf[].  nBuf mu
1b1bc 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20  st always be.** 
1b1bd 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
1b1be 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20  hold the entire 
1b1bf 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20  field.  Except, 
1b1c0 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a  if the field is.
1b1c1 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61  ** a blob with a
1b1c2 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
1b1c3 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69  l, then buf[] mi
1b1c4 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20  ght be just the 
1b1c5 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f  right.** size to
1b1c6 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67   hold everything
1b1c7 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20   except for the 
1b1c8 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1b1c9 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69  .  If buf[].** i
1b1ca 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67  s only big enoug
1b1cb 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f  h to hold the no
1b1cc 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74  n-zero prefix, t
1b1cd 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74  hen only write t
1b1ce 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e  hat.** prefix in
1b1cf 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69  to buf[].  But i
1b1d0 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65  f buf[] is large
1b1d1 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
1b1d2 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66  both the.** pref
1b1d3 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20  ix and the tail 
1b1d4 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70  then write the p
1b1d5 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68  refix and set th
1b1d6 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a  e tail to all.**
1b1d7 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65   zeros..**.** Re
1b1d8 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1b1d9 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c  of bytes actuall
1b1da 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62  y written into b
1b1db 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65  uf[].  The numbe
1b1dc 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e  r.** of bytes in
1b1dd 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64   the zero-filled
1b1de 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65   tail is include
1b1df 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20  d in the return 
1b1e0 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66  value only.** if
1b1e1 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72   those bytes wer
1b1e2 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b  e zeroed in buf[
1b1e3 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  ]..*/ .SQLITE_PR
1b1e4 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
1b1e5 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75  3VdbeSerialPut(u
1b1e6 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66  8 *buf, int nBuf
1b1e7 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  , Mem *pMem, int
1b1e8 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20   file_format){. 
1b1e9 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1b1ea 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1b1eb 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66  rialType(pMem, f
1b1ec 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75  ile_format);.  u
1b1ed 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e  32 len;..  /* In
1b1ee 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a  teger and Real *
1b1ef 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74  /.  if( serial_t
1b1f0 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c  ype<=7 && serial
1b1f1 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75  _type>0 ){.    u
1b1f2 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b  64 v;.    u32 i;
1b1f3 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f  .    if( serial_
1b1f4 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20  type==7 ){.     
1b1f5 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
1b1f6 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d  v)==sizeof(pMem-
1b1f7 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  >r) );.      mem
1b1f8 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72  cpy(&v, &pMem->r
1b1f9 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20  , sizeof(v));.  
1b1fa 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64      swapMixedEnd
1b1fb 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20  ianFloat(v);.   
1b1fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20   }else{.      v 
1b1fd 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20  = pMem->u.i;.   
1b1fe 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d   }.    len = i =
1b1ff 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b200 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
1b201 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65  _type);.    asse
1b202 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42  rt( len<=(u32)nB
1b203 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  uf );.    while(
1b204 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75   i-- ){.      bu
1b205 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78  f[i] = (u8)(v&0x
1b206 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d  FF);.      v >>=
1b207 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65   8;.    }.    re
1b208 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20  turn len;.  }.. 
1b209 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c   /* String or bl
1b20a 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69  ob */.  if( seri
1b20b 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20  al_type>=12 ){. 
1b20c 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
1b20d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61  >n + ((pMem->fla
1b20e 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70  gs & MEM_Zero)?p
1b20f 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a  Mem->u.nZero:0).
1b210 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20               == 
1b211 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65  (int)sqlite3Vdbe
1b212 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65  SerialTypeLen(se
1b213 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20  rial_type) );.  
1b214 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1b215 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c  n<=nBuf );.    l
1b216 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20  en = pMem->n;.  
1b217 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d    memcpy(buf, pM
1b218 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20  em->z, len);.   
1b219 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
1b21a 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
1b21b 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d       len += pMem
1b21c 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20  ->u.nZero;.     
1b21d 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30   assert( nBuf>=0
1b21e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65   );.      if( le
1b21f 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b  n > (u32)nBuf ){
1b220 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28  .        len = (
1b221 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20  u32)nBuf;.      
1b222 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  }.      memset(&
1b223 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c  buf[pMem->n], 0,
1b224 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20   len-pMem->n);. 
1b225 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
1b226 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  len;.  }..  /* N
1b227 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73  ULL or constants
1b228 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74   0 or 1 */.  ret
1b229 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
1b22a 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20  Deserialize the 
1b22b 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65  data blob pointe
1b22c 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73  d to by buf as s
1b22d 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61  erial type seria
1b22e 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74  l_type.** and st
1b22f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1b230 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20  n pMem.  Return 
1b231 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1b232 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51  tes read..*/ .SQ
1b233 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
1b234 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b235 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75  alGet(.  const u
1b236 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75  nsigned char *bu
1b237 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72  f,     /* Buffer
1b238 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20   to deserialize 
1b239 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65  from */.  u32 se
1b23a 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20  rial_type,      
1b23b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61          /* Seria
1b23c 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69  l type to deseri
1b23d 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a  alize */.  Mem *
1b23e 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20  pMem            
1b23f 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
1b240 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65  ry cell to write
1b241 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29   value into */.)
1b242 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69  {.  switch( seri
1b243 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63  al_type ){.    c
1b244 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73  ase 10:   /* Res
1b245 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65  erved for future
1b246 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65   use */.    case
1b247 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76   11:   /* Reserv
1b248 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73  ed for future us
1b249 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a  e */.    case 0:
1b24a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20   {  /* NULL */. 
1b24b 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b24c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1b24d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1b24e 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a      case 1: { /*
1b24f 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   1-byte signed i
1b250 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
1b251 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67  pMem->u.i = (sig
1b252 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b  ned char)buf[0];
1b253 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1b254 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b255 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1b256 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20    }.    case 2: 
1b257 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e  { /* 2-byte sign
1b258 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20  ed integer */.  
1b259 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1b25a 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62  (((signed char)b
1b25b 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66  uf[0])<<8) | buf
1b25c 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  [1];.      pMem-
1b25d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1b25e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32  ;.      return 2
1b25f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1b260 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20   3: { /* 3-byte 
1b261 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
1b262 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1b263 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68  i = (((signed ch
1b264 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20  ar)buf[0])<<16) 
1b265 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20  | (buf[1]<<8) | 
1b266 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d  buf[2];.      pM
1b267 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1b268 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1b269 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 3;.    }.    c
1b26a 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79  ase 4: { /* 4-by
1b26b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1b26c 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d  r */.      pMem-
1b26d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c  >u.i = (buf[0]<<
1b26e 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31  24) | (buf[1]<<1
1b26f 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29  6) | (buf[2]<<8)
1b270 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20   | buf[3];.     
1b271 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1b272 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65  EM_Int;.      re
1b273 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20  turn 4;.    }.  
1b274 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36    case 5: { /* 6
1b275 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74  -byte signed int
1b276 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36  eger */.      u6
1b277 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20  4 x = (((signed 
1b278 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29  char)buf[0])<<8)
1b279 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20   | buf[1];.     
1b27a 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d   u32 y = (buf[2]
1b27b 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c  <<24) | (buf[3]<
1b27c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c  <16) | (buf[4]<<
1b27d 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20  8) | buf[5];.   
1b27e 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c     x = (x<<32) |
1b27f 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   y;.      pMem->
1b280 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b  u.i = *(i64*)&x;
1b281 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1b282 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b283 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20      return 6;.  
1b284 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20    }.    case 6: 
1b285 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e    /* 8-byte sign
1b286 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20  ed integer */.  
1b287 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49    case 7: { /* I
1b288 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  EEE floating poi
1b289 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20  nt */.      u64 
1b28a 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a  x;.      u32 y;.
1b28b 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
1b28c 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
1b28d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f  (SQLITE_OMIT_FLO
1b28e 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20  ATING_POINT).   
1b28f 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61     /* Verify tha
1b290 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66  t integers and f
1b291 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
1b292 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d  lues use the sam
1b293 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20  e.      ** byte 
1b294 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74  order.  Or, that
1b295 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44   if SQLITE_MIXED
1b296 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c  _ENDIAN_64BIT_FL
1b297 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  OAT is.      ** 
1b298 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d  defined that 64-
1b299 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  bit floating poi
1b29a 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79  nt values really
1b29b 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20   are mixed.     
1b29c 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20   ** endian..    
1b29d 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69    */.      stati
1b29e 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d  c const u64 t1 =
1b29f 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30   ((u64)0x3ff0000
1b2a0 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74  0)<<32;.      st
1b2a1 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c  atic const doubl
1b2a2 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20  e r1 = 1.0;.    
1b2a3 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20    u64 t2 = t1;. 
1b2a4 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e       swapMixedEn
1b2a5 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20  dianFloat(t2);. 
1b2a6 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
1b2a7 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28  eof(r1)==sizeof(
1b2a8 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72  t2) && memcmp(&r
1b2a9 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72  1, &t2, sizeof(r
1b2aa 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66  1))==0 );.#endif
1b2ab 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66  ..      x = (buf
1b2ac 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [0]<<24) | (buf[
1b2ad 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32  1]<<16) | (buf[2
1b2ae 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a  ]<<8) | buf[3];.
1b2af 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34        y = (buf[4
1b2b0 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d  ]<<24) | (buf[5]
1b2b1 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c  <<16) | (buf[6]<
1b2b2 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20  <8) | buf[7];.  
1b2b3 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20      x = (x<<32) 
1b2b4 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73  | y;.      if( s
1b2b5 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b  erial_type==6 ){
1b2b6 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  .        pMem->u
1b2b7 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a  .i = *(i64*)&x;.
1b2b8 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
1b2b9 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1b2ba 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1b2bb 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
1b2bc 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65  of(x)==8 && size
1b2bd 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29  of(pMem->r)==8 )
1b2be 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69  ;.        swapMi
1b2bf 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78  xedEndianFloat(x
1b2c0 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  );.        memcp
1b2c1 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20  y(&pMem->r, &x, 
1b2c2 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20  sizeof(x));.    
1b2c3 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b2c4 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70  = sqlite3IsNaN(p
1b2c5 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75  Mem->r) ? MEM_Nu
1b2c6 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20  ll : MEM_Real;. 
1b2c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
1b2c8 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 8;.    }.   
1b2c9 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49   case 8:    /* I
1b2ca 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20  nteger 0 */.    
1b2cb 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e  case 9: {  /* In
1b2cc 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20  teger 1 */.     
1b2cd 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72   pMem->u.i = ser
1b2ce 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20  ial_type-8;.    
1b2cf 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b2d0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
1b2d1 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1b2d2 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1b2d3 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73      u32 len = (s
1b2d4 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32  erial_type-12)/2
1b2d5 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  ;.      pMem->z 
1b2d6 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20  = (char *)buf;. 
1b2d7 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c       pMem->n = l
1b2d8 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  en;.      pMem->
1b2d9 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  xDel = 0;.      
1b2da 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26  if( serial_type&
1b2db 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20  0x01 ){.        
1b2dc 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b2dd 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65  M_Str | MEM_Ephe
1b2de 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  m;.      }else{.
1b2df 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
1b2e0 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c  ags = MEM_Blob |
1b2e1 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
1b2e2 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
1b2e3 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   len;.    }.  }.
1b2e4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a    return 0;.}...
1b2e5 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
1b2e6 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69  nKey-byte encodi
1b2e7 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69  ng of a record i
1b2e8 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20  n pKey[], parse 
1b2e9 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e  the.** record in
1b2ea 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63  to a UnpackedRec
1b2eb 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20  ord structure.  
1b2ec 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1b2ed 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75   to.** that stru
1b2ee 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
1b2ef 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
1b2f0 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20  n might provide 
1b2f1 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66  szSpace bytes of
1b2f2 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65   memory.** space
1b2f3 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69   at pSpace.  Thi
1b2f4 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75  s space can be u
1b2f5 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sed to hold the 
1b2f6 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65  returned.** VDbe
1b2f7 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72  ParsedRecord str
1b2f8 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20  ucture if it is 
1b2f9 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49  large enough.  I
1b2fa 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62  f it is.** not b
1b2fb 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65  ig enough, space
1b2fc 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
1b2fd 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
1b2fe 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ()..**.** The re
1b2ff 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65  turned structure
1b300 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65   should be close
1b301 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a  d by a call to.*
1b302 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  * sqlite3VdbeDel
1b303 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
1b304 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  d()..*/ .SQLITE_
1b305 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64  PRIVATE Unpacked
1b306 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56  Record *sqlite3V
1b307 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28  dbeRecordUnpack(
1b308 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
1b309 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66  Info,     /* Inf
1b30a 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
1b30b 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74  he record format
1b30c 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20   */.  int nKey, 
1b30d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b30e 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61  Size of the bina
1b30f 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  ry record */.  c
1b310 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
1b311 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e        /* The bin
1b312 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ary record */.  
1b313 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20  char *pSpace,   
1b314 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67         /* Unalig
1b315 6e 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61  ned space availa
1b316 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ble to hold the 
1b317 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  object */.  int 
1b318 73 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20  szSpace         
1b319 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53     /* Size of pS
1b31a 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20  pace[] in bytes 
1b31b 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  */.){.  const un
1b31c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65  signed char *aKe
1b31d 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67  y = (const unsig
1b31e 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b  ned char *)pKey;
1b31f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1b320 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e  d *p;  /* The un
1b321 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68  packed record th
1b322 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72  at we will retur
1b323 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  n */.  int nByte
1b324 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65  ;          /* Me
1b325 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65  mory space neede
1b326 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20  d to hold p, in 
1b327 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64  bytes */.  int d
1b328 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75  ;.  u32 idx;.  u
1b329 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20  16 u;           
1b32a 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c     /* Unsigned l
1b32b 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1b32c 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65   u32 szHdr;.  Me
1b32d 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e  m *pMem;.  int n
1b32e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Off;           /
1b32f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63  * Increase pSpac
1b330 65 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74  e by this much t
1b331 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69  o 8-byte align i
1b332 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  t */.  .  /*.  *
1b333 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69  * We want to shi
1b334 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70  ft the pointer p
1b335 53 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68  Space up such th
1b336 61 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20  at it is 8-byte 
1b337 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68  aligned..  ** Th
1b338 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63  us, we need to c
1b339 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65  alculate a value
1b33a 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20  , nOff, between 
1b33b 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66  0 and 7, to shif
1b33c 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20  t .  ** it by.  
1b33d 49 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72  If pSpace is alr
1b33e 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67  eady 8-byte alig
1b33f 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64  ned, nOff should
1b340 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20   be zero..  */. 
1b341 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51   nOff = (8 - (SQ
1b342 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
1b343 70 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20  pSpace) & 7)) & 
1b344 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e  7;.  pSpace += n
1b345 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d  Off;.  szSpace -
1b346 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20  = nOff;.  nByte 
1b347 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  = ROUND8(sizeof(
1b348 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29  UnpackedRecord))
1b349 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28   + sizeof(Mem)*(
1b34a 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1b34b 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  +1);.  if( nByte
1b34c 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20  >szSpace ){.    
1b34d 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
1b34e 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d  locRaw(pKeyInfo-
1b34f 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  >db, nByte);.   
1b350 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
1b351 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  rn 0;.    p->fla
1b352 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45  gs = UNPACKED_NE
1b353 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b  ED_FREE | UNPACK
1b354 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b  ED_NEED_DESTROY;
1b355 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
1b356 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  = (UnpackedRecor
1b357 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70  d*)pSpace;.    p
1b358 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b  ->flags = UNPACK
1b359 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b  ED_NEED_DESTROY;
1b35a 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e  .  }.  p->pKeyIn
1b35b 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20  fo = pKeyInfo;. 
1b35c 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65   p->nField = pKe
1b35d 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20  yInfo->nField + 
1b35e 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70  1;.  p->aMem = p
1b35f 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63  Mem = (Mem*)&((c
1b360 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73  har*)p)[ROUND8(s
1b361 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65  izeof(UnpackedRe
1b362 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72  cord))];.  asser
1b363 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
1b364 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b  IGNMENT(pMem) );
1b365 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69  .  idx = getVari
1b366 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72  nt32(aKey, szHdr
1b367 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a  );.  d = szHdr;.
1b368 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65    u = 0;.  while
1b369 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75  ( idx<szHdr && u
1b36a 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c  <p->nField && d<
1b36b 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32  =nKey ){.    u32
1b36c 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20   serial_type;.. 
1b36d 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72     idx += getVar
1b36e 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d  int32(&aKey[idx]
1b36f 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a  , serial_type);.
1b370 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
1b371 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20  pKeyInfo->enc;. 
1b372 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b     pMem->db = pK
1b373 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20  eyInfo->db;.    
1b374 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b  pMem->flags = 0;
1b375 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  .    pMem->zMall
1b376 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d  oc = 0;.    d +=
1b377 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b378 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20  alGet(&aKey[d], 
1b379 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65  serial_type, pMe
1b37a 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  m);.    pMem++;.
1b37b 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61      u++;.  }.  a
1b37c 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e  ssert( u<=pKeyIn
1b37d 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29  fo->nField + 1 )
1b37e 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20  ;.  p->nField = 
1b37f 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  u;.  return (voi
1b380 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  d*)p;.}../*.** T
1b381 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74  his routine dest
1b382 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52  roys a UnpackedR
1b383 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ecord object..*/
1b384 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b385 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1b386 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65  DeleteUnpackedRe
1b387 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63  cord(UnpackedRec
1b388 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ord *p){.  int i
1b389 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a  ;.  Mem *pMem;..
1b38a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
1b38b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66  ;.  assert( p->f
1b38c 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1b38d 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a  NEED_DESTROY );.
1b38e 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d    for(i=0, pMem=
1b38f 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46  p->aMem; i<p->nF
1b390 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b  ield; i++, pMem+
1b391 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75  +){.    /* The u
1b392 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69  npacked record i
1b393 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75  s always constru
1b394 63 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20  cted by the.    
1b395 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e  ** sqlite3VdbeUn
1b396 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e  packRecord() fun
1b397 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69  ction above, whi
1b398 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20  ch makes all.   
1b399 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20   ** strings and 
1b39a 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41  blobs static.  A
1b39b 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65  nd none of the e
1b39c 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20  lements are.    
1b39d 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72  ** ever transfor
1b39e 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73  med, so there is
1b39f 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20   never anything 
1b3a0 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a  to delete..    *
1b3a1 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  /.    if( NEVER(
1b3a2 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29  pMem->zMalloc) )
1b3a3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b3a4 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
1b3a5 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  }.  if( p->flags
1b3a6 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44   & UNPACKED_NEED
1b3a7 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c  _FREE ){.    sql
1b3a8 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b  ite3DbFree(p->pK
1b3a9 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a  eyInfo->db, p);.
1b3aa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
1b3ab 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61  s function compa
1b3ac 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c  res the two tabl
1b3ad 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20  e rows or index 
1b3ae 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69  records.** speci
1b3af 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20  fied by {nKey1, 
1b3b0 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79  pKey1} and pPKey
1b3b1 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61  2.  It returns a
1b3b2 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a   negative, zero.
1b3b3 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ** or positive i
1b3b4 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69  nteger if key1 i
1b3b5 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
1b3b6 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65  al to or .** gre
1b3b7 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20  ater than key2. 
1b3b8 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65   The {nKey1, pKe
1b3b9 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20  y1} key must be 
1b3ba 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65  a blob.** create
1b3bb 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52  d by th OP_MakeR
1b3bc 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20  ecord opcode of 
1b3bd 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70  the VDBE.  The p
1b3be 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73  PKey2.** key mus
1b3bf 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65  t be a parsed ke
1b3c0 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e  y such as obtain
1b3c1 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74  ed from.** sqlit
1b3c2 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72  e3VdbeParseRecor
1b3c3 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e  d..**.** Key1 an
1b3c4 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61  d Key2 do not ha
1b3c5 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  ve to contain th
1b3c6 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66  e same number of
1b3c7 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20   fields..** The 
1b3c8 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66  key with fewer f
1b3c9 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79  ields is usually
1b3ca 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74   compares less t
1b3cb 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67  han the .** long
1b3cc 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72  er key.  However
1b3cd 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44   if the UNPACKED
1b3ce 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69  _INCRKEY flags i
1b3cf 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a  n pPKey2 is set.
1b3d0 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f  ** and the commo
1b3d1 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65  n prefixes are e
1b3d2 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20  qual, then key1 
1b3d3 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79  is less than key
1b3d4 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20  2..** Or if the 
1b3d5 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50  UNPACKED_MATCH_P
1b3d6 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65  REFIX flag is se
1b3d7 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78  t and the prefix
1b3d8 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c  es are.** equal,
1b3d9 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61   then the keys a
1b3da 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  re considered to
1b3db 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a   be equal and.**
1b3dc 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e   the parts beyon
1b3dd 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65  d the common pre
1b3de 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  fix are ignored.
1b3df 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e  .**.** If the UN
1b3e0 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
1b3e1 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c  WID flag is set,
1b3e2 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62   then the last b
1b3e3 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65  yte of.** the he
1b3e4 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73  ader of pKey1 is
1b3e5 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73   ignored.  It is
1b3e6 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b   assumed that pK
1b3e7 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64  ey1 is.** an ind
1b3e8 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73  ex key, and thus
1b3e9 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77   ends with a row
1b3ea 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c  id value.  The l
1b3eb 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74  ast byte.** of t
1b3ec 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74  he header will t
1b3ed 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20  herefore be the 
1b3ee 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1b3ef 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65  he rowid:.** one
1b3f0 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20   of 1, 2, 3, 4, 
1b3f1 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20  5, 6, 8, or 9 - 
1b3f2 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69  the integer seri
1b3f3 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65  al types..** The
1b3f4 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   serial type of 
1b3f5 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20  the final rowid 
1b3f6 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61  will always be a
1b3f7 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a   single byte..**
1b3f8 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69   By ignoring thi
1b3f9 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74  s last byte of t
1b3fa 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f  he header, we fo
1b3fb 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  rce the comparis
1b3fc 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20  on.** to ignore 
1b3fd 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65  the rowid at the
1b3fe 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f   end of key1..*/
1b3ff 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b400 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
1b401 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20  ecordCompare(.  
1b402 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
1b403 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a   void *pKey1, /*
1b404 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55   Left key */.  U
1b405 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
1b406 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20  PKey2        /* 
1b407 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a  Right key */.){.
1b408 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20    int d1;       
1b409 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
1b40a 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65  nto aKey[] of ne
1b40b 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20  xt data element 
1b40c 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20  */.  u32 idx1;  
1b40d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1b40e 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66  t into aKey[] of
1b40f 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65   next header ele
1b410 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a  ment */.  u32 sz
1b411 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20  Hdr1;        /* 
1b412 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1b413 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  in header */.  i
1b414 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20  nt i = 0;.  int 
1b415 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63  nField;.  int rc
1b416 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e   = 0;.  const un
1b417 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65  signed char *aKe
1b418 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69  y1 = (const unsi
1b419 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79  gned char *)pKey
1b41a 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  1;.  KeyInfo *pK
1b41b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65  eyInfo;.  Mem me
1b41c 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20  m1;..  pKeyInfo 
1b41d 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e  = pPKey2->pKeyIn
1b41e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d  fo;.  mem1.enc =
1b41f 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a   pKeyInfo->enc;.
1b420 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79    mem1.db = pKey
1b421 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 2f 2a 20 6d  Info->db;.  /* m
1b422 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b 20 20  em1.flags = 0;  
1b423 2f 2f 20 57 69 6c 6c 20 62 65 20 69 6e 69 74 69  // Will be initi
1b424 61 6c 69 7a 65 64 20 62 79 20 73 71 6c 69 74 65  alized by sqlite
1b425 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29  3VdbeSerialGet()
1b426 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20   */.  VVA_ONLY( 
1b427 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30  mem1.zMalloc = 0
1b428 3b 20 29 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64  ; ) /* Only need
1b429 65 64 20 62 79 20 61 73 73 65 72 74 28 29 20 73  ed by assert() s
1b42a 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20  tatements */..  
1b42b 2f 2a 20 43 6f 6d 70 69 6c 65 72 73 20 6d 61 79  /* Compilers may
1b42c 20 63 6f 6d 70 6c 61 69 6e 20 74 68 61 74 20 6d   complain that m
1b42d 65 6d 31 2e 75 2e 69 20 69 73 20 70 6f 74 65 6e  em1.u.i is poten
1b42e 74 69 61 6c 6c 79 20 75 6e 69 6e 69 74 69 61 6c  tially uninitial
1b42f 69 7a 65 64 2e 0a 20 20 2a 2a 20 57 65 20 63 6f  ized..  ** We co
1b430 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69  uld initialize i
1b431 74 2c 20 61 73 20 73 68 6f 77 6e 20 68 65 72 65  t, as shown here
1b432 2c 20 74 6f 20 73 69 6c 65 6e 63 65 20 74 68 6f  , to silence tho
1b433 73 65 20 63 6f 6d 70 6c 61 69 6e 74 73 2e 0a 20  se complaints.. 
1b434 20 2a 2a 20 42 75 74 20 69 6e 20 66 61 63 74 2c   ** But in fact,
1b435 20 6d 65 6d 31 2e 75 2e 69 20 77 69 6c 6c 20 6e   mem1.u.i will n
1b436 65 76 65 72 20 61 63 74 75 61 6c 6c 79 20 62 65  ever actually be
1b437 20 75 73 65 64 20 69 6e 69 74 69 61 6c 69 7a 65   used initialize
1b438 64 2c 20 61 6e 64 20 64 6f 69 6e 67 20 0a 20 20  d, and doing .  
1b439 2a 2a 20 74 68 65 20 75 6e 6e 65 63 65 73 73 61  ** the unnecessa
1b43a 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ry initializatio
1b43b 6e 20 68 61 73 20 61 20 6d 65 61 73 75 72 61 62  n has a measurab
1b43c 6c 65 20 6e 65 67 61 74 69 76 65 20 70 65 72 66  le negative perf
1b43d 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 69 6d 70  ormance.  ** imp
1b43e 61 63 74 2c 20 73 69 6e 63 65 20 74 68 69 73 20  act, since this 
1b43f 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76 65 72  routine is a ver
1b440 79 20 68 69 67 68 20 72 75 6e 6e 65 72 2e 20 20  y high runner.  
1b441 41 6e 64 20 73 6f 2c 20 77 65 20 63 68 6f 6f 73  And so, we choos
1b442 65 0a 20 20 2a 2a 20 74 6f 20 69 67 6e 6f 72 65  e.  ** to ignore
1b443 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 77 61   the compiler wa
1b444 72 6e 69 6e 67 73 20 61 6e 64 20 6c 65 61 76 65  rnings and leave
1b445 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 75   this variable u
1b446 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20  ninitialized..  
1b447 2a 2f 0a 20 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e  */.  /*  mem1.u.
1b448 69 20 3d 20 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e  i = 0;  // not n
1b449 65 65 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73  eeded, here to s
1b44a 69 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20  ilence compiler 
1b44b 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20  warning */.  .  
1b44c 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e 74  idx1 = getVarint
1b44d 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 31  32(aKey1, szHdr1
1b44e 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72 31  );.  d1 = szHdr1
1b44f 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e  ;.  if( pPKey2->
1b450 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
1b451 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b  _IGNORE_ROWID ){
1b452 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20  .    szHdr1--;. 
1b453 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b   }.  nField = pK
1b454 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a  eyInfo->nField;.
1b455 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a    while( idx1<sz
1b456 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32  Hdr1 && i<pPKey2
1b457 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ->nField ){.    
1b458 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31  u32 serial_type1
1b459 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
1b45a 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20  he serial types 
1b45b 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65  for the next ele
1b45c 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79  ment in each key
1b45d 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d  . */.    idx1 +=
1b45e 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b   getVarint32( aK
1b45f 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c  ey1+idx1, serial
1b460 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66  _type1 );.    if
1b461 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73  ( d1>=nKey1 && s
1b462 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b463 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
1b464 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b  ype1)>0 ) break;
1b465 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74  ..    /* Extract
1b466 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62   the values to b
1b467 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20  e compared..    
1b468 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c  */.    d1 += sql
1b469 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1b46a 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65  t(&aKey1[d1], se
1b46b 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d  rial_type1, &mem
1b46c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74  1);..    /* Do t
1b46d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20  he comparison.  
1b46e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
1b46f 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
1b470 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e  &mem1, &pPKey2->
1b471 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20  aMem[i],.       
1b472 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b473 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70      i<nField ? p
1b474 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
1b475 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ] : 0);.    if( 
1b476 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  rc!=0 ){.      a
1b477 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c  ssert( mem1.zMal
1b478 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 65  loc==0 );  /* Se
1b479 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f 77 20  e comment below 
1b47a 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76  */..      /* Inv
1b47b 65 72 74 20 74 68 65 20 72 65 73 75 6c 74 20 69  ert the result i
1b47c 66 20 77 65 20 61 72 65 20 75 73 69 6e 67 20 44  f we are using D
1b47d 45 53 43 20 73 6f 72 74 20 6f 72 64 65 72 2e 20  ESC sort order. 
1b47e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65  */.      if( pKe
1b47f 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65  yInfo->aSortOrde
1b480 72 20 26 26 20 69 3c 6e 46 69 65 6c 64 20 26 26  r && i<nField &&
1b481 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
1b482 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20  Order[i] ){.    
1b483 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20      rc = -rc;.  
1b484 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20      }.    .     
1b485 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 49   /* If the PREFI
1b486 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 73  X_SEARCH flag is
1b487 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 65   set and all fie
1b488 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 66  lds except the f
1b489 69 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 72 6f  inal.      ** ro
1b48a 77 69 64 20 66 69 65 6c 64 20 77 65 72 65 20 65  wid field were e
1b48b 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72  qual, then clear
1b48c 20 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52   the PREFIX_SEAR
1b48d 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65 74 20  CH flag and set 
1b48e 0a 20 20 20 20 20 20 2a 2a 20 70 50 4b 65 79 32  .      ** pPKey2
1b48f 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65 20 76  ->rowid to the v
1b490 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69  alue of the rowi
1b491 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b 65 79  d field in (pKey
1b492 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 20 20 20  1, nKey1)..     
1b493 20 2a 2a 20 54 68 69 73 20 69 73 20 75 73 65 64   ** This is used
1b494 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55 6e 69   by the OP_IsUni
1b495 71 75 65 20 6f 70 63 6f 64 65 2e 0a 20 20 20 20  que opcode..    
1b496 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28    */.      if( (
1b497 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20  pPKey2->flags & 
1b498 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f  UNPACKED_PREFIX_
1b499 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d 28 70  SEARCH) && i==(p
1b49a 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29  PKey2->nField-1)
1b49b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1b49c 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 31  rt( idx1==szHdr1
1b49d 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20 20 20   && rc );.      
1b49e 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 66    assert( mem1.f
1b49f 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
1b4a0 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65 79 32  ;.        pPKey2
1b4a1 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41  ->flags &= ~UNPA
1b4a2 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1b4a3 43 48 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65  CH;.        pPKe
1b4a4 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31  y2->rowid = mem1
1b4a5 2e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  .u.i;.      }.  
1b4a6 20 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20    .      return 
1b4a7 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b  rc;.    }.    i+
1b4a8 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20  +;.  }..  /* No 
1b4a9 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1b4aa 6e 20 69 73 20 65 76 65 72 20 75 73 65 64 20 6f  n is ever used o
1b4ab 6e 20 6d 65 6d 31 2e 20 20 50 72 6f 76 65 20 74  n mem1.  Prove t
1b4ac 68 69 73 20 75 73 69 6e 67 0a 20 20 2a 2a 20 74  his using.  ** t
1b4ad 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  he following ass
1b4ae 65 72 74 28 29 2e 20 20 49 66 20 74 68 65 20 61  ert().  If the a
1b4af 73 73 65 72 74 28 29 20 66 61 69 6c 73 2c 20 69  ssert() fails, i
1b4b0 74 20 69 6e 64 69 63 61 74 65 73 20 61 0a 20 20  t indicates a.  
1b4b1 2a 2a 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 61  ** memory leak a
1b4b2 6e 64 20 61 20 6e 65 65 64 20 74 6f 20 63 61 6c  nd a need to cal
1b4b3 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  l sqlite3VdbeMem
1b4b4 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 2e 0a  Release(&mem1)..
1b4b5 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6d    */.  assert( m
1b4b6 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29  em1.zMalloc==0 )
1b4b7 3b 0a 0a 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65  ;..  /* rc==0 he
1b4b8 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e  re means that on
1b4b9 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61  e of the keys ra
1b4ba 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20  n out of fields 
1b4bb 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 65  and.  ** all the
1b4bc 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68   fields up to th
1b4bd 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71  at point were eq
1b4be 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41  ual. If the UNPA
1b4bf 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 2a  CKED_INCRKEY.  *
1b4c0 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  * flag is set, t
1b4c1 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69  hen break the ti
1b4c2 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65  e by treating ke
1b4c3 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20  y2 as larger..  
1b4c4 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 4b 45  ** If the UPACKE
1b4c5 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 66  D_PREFIX_MATCH f
1b4c6 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
1b4c7 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f   keys with commo
1b4c8 6e 20 70 72 65 66 69 78 65 73 0a 20 20 2a 2a 20  n prefixes.  ** 
1b4c9 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74  are considered t
1b4ca 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68  o be equal.  Oth
1b4cb 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67  erwise, the long
1b4cc 65 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20  er key is the . 
1b4cd 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20   ** larger.  As 
1b4ce 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  it happens, the 
1b4cf 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61  pPKey2 will alwa
1b4d0 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72  ys be the longer
1b4d1 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 69  .  ** if there i
1b4d2 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 2e 0a  s a difference..
1b4d3 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72    */.  assert( r
1b4d4 63 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 50  c==0 );.  if( pP
1b4d5 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e  Key2->flags & UN
1b4d6 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29  PACKED_INCRKEY )
1b4d7 7b 0a 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20  {.    rc = -1;. 
1b4d8 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65 79   }else if( pPKey
1b4d9 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  2->flags & UNPAC
1b4da 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48  KED_PREFIX_MATCH
1b4db 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65   ){.    /* Leave
1b4dc 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 7d 65 6c 73   rc==0 */.  }els
1b4dd 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64 72  e if( idx1<szHdr
1b4de 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b  1 ){.    rc = 1;
1b4df 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1b4e0 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75  ;.}. ../*.** pCu
1b4e1 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69  r points at an i
1b4e2 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 74  ndex entry creat
1b4e3 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f  ed using the OP_
1b4e4 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64  MakeRecord opcod
1b4e5 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72  e..** Read the r
1b4e6 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 66  owid (the last f
1b4e7 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f  ield in the reco
1b4e8 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74  rd) and store it
1b4e9 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52   in *rowid..** R
1b4ea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
1b4eb 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
1b4ec 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72  rks, or an error
1b4ed 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e   code otherwise.
1b4ee 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68  .**.** pCur migh
1b4ef 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
1b4f0 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66   text obtained f
1b4f1 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61  rom a corrupt da
1b4f2 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
1b4f3 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63  So the content c
1b4f4 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64  annot be trusted
1b4f5 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74  .  Do appropriat
1b4f6 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20  e checks on the 
1b4f7 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  content..*/.SQLI
1b4f8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1b4f9 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77  qlite3VdbeIdxRow
1b4fa 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  id(sqlite3 *db, 
1b4fb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
1b4fc 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69  i64 *rowid){.  i
1b4fd 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b  64 nCellKey = 0;
1b4fe 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32  .  int rc;.  u32
1b4ff 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f   szHdr;        /
1b500 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65  * Size of the he
1b501 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 74 79  ader */.  u32 ty
1b502 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53  peRowid;    /* S
1b503 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68  erial type of th
1b504 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32  e rowid */.  u32
1b505 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f   lenRowid;     /
1b506 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 72 6f  * Size of the ro
1b507 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20  wid */.  Mem m, 
1b508 76 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  v;..  UNUSED_PAR
1b509 41 4d 45 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f  AMETER(db);..  /
1b50a 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f  * Get the size o
1b50b 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
1b50c 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73  y.  Only indices
1b50d 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73   entries of less
1b50e 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20  .  ** than 2GiB 
1b50f 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e  are support - an
1b510 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73  ything large mus
1b511 74 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f  t be database co
1b512 72 72 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41  rruption..  ** A
1b513 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73  ny corruption is
1b514 20 64 65 74 65 63 74 65 64 20 69 6e 20 73 71 6c   detected in sql
1b515 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
1b516 6c 6c 50 74 72 28 29 2c 20 74 68 6f 75 67 68 2c  llPtr(), though,
1b517 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f   so.  ** this co
1b518 64 65 20 63 61 6e 20 73 61 66 65 6c 79 20 61 73  de can safely as
1b519 73 75 6d 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b  sume that nCellK
1b51a 65 79 20 69 73 20 33 32 2d 62 69 74 73 20 20 0a  ey is 32-bits  .
1b51b 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73    */.  assert( s
1b51c 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1b51d 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29  rIsValid(pCur) )
1b51e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1b51f 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
1b520 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
1b521 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1b522 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a  ITE_OK );     /*
1b523 20 70 43 75 72 20 69 73 20 61 6c 77 61 79 73 20   pCur is always 
1b524 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65  valid so KeySize
1b525 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a   cannot fail */.
1b526 20 20 61 73 73 65 72 74 28 20 28 6e 43 65 6c 6c    assert( (nCell
1b527 4b 65 79 20 26 20 53 51 4c 49 54 45 5f 4d 41 58  Key & SQLITE_MAX
1b528 5f 55 33 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c  _U32)==(u64)nCel
1b529 6c 4b 65 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65  lKey );..  /* Re
1b52a 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65  ad in the comple
1b52b 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  te content of th
1b52c 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f  e index entry */
1b52d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c  .  memset(&m, 0,
1b52e 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72   sizeof(m));.  r
1b52f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1b530 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72  emFromBtree(pCur
1b531 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b  , 0, (int)nCellK
1b532 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66  ey, 1, &m);.  if
1b533 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
1b534 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1b535 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   The index entry
1b536 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74 68   must begin with
1b537 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a   a header size *
1b538 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  /.  (void)getVar
1b539 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20  int32((u8*)m.z, 
1b53a 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61  szHdr);.  testca
1b53b 73 65 28 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a  se( szHdr==3 );.
1b53c 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64    testcase( szHd
1b53d 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20  r==m.n );.  if( 
1b53e 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33  unlikely(szHdr<3
1b53f 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d   || (int)szHdr>m
1b540 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  .n) ){.    goto 
1b541 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
1b542 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
1b543 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f  The last field o
1b544 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75  f the index shou
1b545 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  ld be an integer
1b546 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20   - the ROWID..  
1b547 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  ** Verify that t
1b548 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65  he last entry re
1b549 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67  ally is an integ
1b54a 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67  er. */.  (void)g
1b54b 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29  etVarint32((u8*)
1b54c 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74  &m.z[szHdr-1], t
1b54d 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73  ypeRowid);.  tes
1b54e 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
1b54f 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==1 );.  testcas
1b550 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20  e( typeRowid==2 
1b551 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
1b552 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20  ypeRowid==3 );. 
1b553 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
1b554 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73  owid==4 );.  tes
1b555 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
1b556 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==5 );.  testcas
1b557 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20  e( typeRowid==6 
1b558 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
1b559 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20  ypeRowid==8 );. 
1b55a 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
1b55b 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28  owid==9 );.  if(
1b55c 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f   unlikely(typeRo
1b55d 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77  wid<1 || typeRow
1b55e 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69  id>9 || typeRowi
1b55f 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74  d==7) ){.    got
1b560 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  o idx_rowid_corr
1b561 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65  uption;.  }.  le
1b562 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  nRowid = sqlite3
1b563 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1b564 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20  n(typeRowid);.  
1b565 74 65 73 74 63 61 73 65 28 20 28 75 33 32 29 6d  testcase( (u32)m
1b566 2e 6e 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77  .n==szHdr+lenRow
1b567 69 64 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69  id );.  if( unli
1b568 6b 65 6c 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a  kely((u32)m.n<sz
1b569 48 64 72 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b  Hdr+lenRowid) ){
1b56a 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f  .    goto idx_ro
1b56b 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a  wid_corruption;.
1b56c 20 20 7d 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20    }..  /* Fetch 
1b56d 74 68 65 20 69 6e 74 65 67 65 72 20 6f 66 66 20  the integer off 
1b56e 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
1b56f 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20  ndex record */. 
1b570 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b571 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b  alGet((u8*)&m.z[
1b572 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74  m.n-lenRowid], t
1b573 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20  ypeRowid, &v);. 
1b574 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b   *rowid = v.u.i;
1b575 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b576 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20  mRelease(&m);.  
1b577 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1b578 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  ;..  /* Jump her
1b579 65 20 69 66 20 64 61 74 61 62 61 73 65 20 63 6f  e if database co
1b57a 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65  rruption is dete
1b57b 63 74 65 64 20 61 66 74 65 72 20 6d 20 68 61 73  cted after m has
1b57c 20 62 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63   been.  ** alloc
1b57d 61 74 65 64 2e 20 20 46 72 65 65 20 74 68 65 20  ated.  Free the 
1b57e 6d 20 6f 62 6a 65 63 74 20 61 6e 64 20 72 65 74  m object and ret
1b57f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1b580 50 54 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64  PT. */.idx_rowid
1b581 5f 63 6f 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74  _corruption:.  t
1b582 65 73 74 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c  estcase( m.zMall
1b583 6f 63 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74  oc!=0 );.  sqlit
1b584 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1b585 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  (&m);.  return S
1b586 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1b587 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  PT;.}../*.** Com
1b588 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f 66 20  pare the key of 
1b589 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
1b58a 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 20 69  that cursor pC i
1b58b 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67  s pointing to ag
1b58c 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79  ainst.** the key
1b58d 20 73 74 72 69 6e 67 20 69 6e 20 70 55 6e 70 61   string in pUnpa
1b58e 63 6b 65 64 2e 20 20 57 72 69 74 65 20 69 6e 74  cked.  Write int
1b58f 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72  o *pRes a number
1b590 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61  .** that is nega
1b591 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
1b592 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73  ositive if pC is
1b593 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
1b594 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61  l to,.** or grea
1b595 74 65 72 20 74 68 61 6e 20 70 55 6e 70 61 63 6b  ter than pUnpack
1b596 65 64 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  ed.  Return SQLI
1b597 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
1b598 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65  ..**.** pUnpacke
1b599 64 20 69 73 20 65 69 74 68 65 72 20 63 72 65 61  d is either crea
1b59a 74 65 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f  ted without a ro
1b59b 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61  wid or is trunca
1b59c 74 65 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a  ted so that it.*
1b59d 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69  * omits the rowi
1b59e 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54  d at the end.  T
1b59f 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20  he rowid at the 
1b5a0 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  end of the index
1b5a1 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e   entry.** is ign
1b5a2 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 20 20 48  ored as well.  H
1b5a3 65 6e 63 65 2c 20 74 68 69 73 20 72 6f 75 74 69  ence, this routi
1b5a4 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73  ne only compares
1b5a5 20 74 68 65 20 70 72 65 66 69 78 65 73 20 0a 2a   the prefixes .*
1b5a6 2a 20 6f 66 20 74 68 65 20 6b 65 79 73 20 70 72  * of the keys pr
1b5a7 69 6f 72 20 74 6f 20 74 68 65 20 66 69 6e 61 6c  ior to the final
1b5a8 20 72 6f 77 69 64 2c 20 6e 6f 74 20 74 68 65 20   rowid, not the 
1b5a9 65 6e 74 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53  entire key..*/.S
1b5aa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1b5ab 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
1b5ac 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64  KeyCompare(.  Vd
1b5ad 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20  beCursor *pC,   
1b5ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1b5af 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61   cursor to compa
1b5b0 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20  re against */.  
1b5b1 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1b5b2 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55  pUnpacked,  /* U
1b5b3 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20  npacked version 
1b5b4 6f 66 20 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72  of key to compar
1b5b5 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69  e against */.  i
1b5b6 6e 74 20 2a 72 65 73 20 20 20 20 20 20 20 20 20  nt *res         
1b5b7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
1b5b8 69 74 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ite the comparis
1b5b9 6f 6e 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a  on result here *
1b5ba 2f 0a 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c  /.){.  i64 nCell
1b5bb 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Key = 0;.  int r
1b5bc 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  c;.  BtCursor *p
1b5bd 43 75 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f  Cur = pC->pCurso
1b5be 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61  r;.  Mem m;..  a
1b5bf 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1b5c0 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
1b5c1 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
1b5c2 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
1b5c3 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c  Size(pCur, &nCel
1b5c4 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28  lKey);.  assert(
1b5c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1b5c6 3b 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20  ;    /* pCur is 
1b5c7 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20  always valid so 
1b5c8 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66  KeySize cannot f
1b5c9 61 69 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c  ail */.  /* nCel
1b5ca 6c 4b 65 79 20 77 69 6c 6c 20 61 6c 77 61 79 73  lKey will always
1b5cb 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e   be between 0 an
1b5cc 64 20 30 78 66 66 66 66 66 66 66 66 20 62 65 63  d 0xffffffff bec
1b5cd 61 75 73 65 20 6f 66 20 74 68 65 20 73 61 79 0a  ause of the say.
1b5ce 20 20 2a 2a 20 74 68 61 74 20 62 74 72 65 65 50    ** that btreeP
1b5cf 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 61 6e  arseCellPtr() an
1b5d0 64 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  d sqlite3GetVari
1b5d1 6e 74 33 32 28 29 20 61 72 65 20 69 6d 70 6c 65  nt32() are imple
1b5d2 6d 65 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20  mented */.  if( 
1b5d3 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e  nCellKey<=0 || n
1b5d4 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66  CellKey>0x7fffff
1b5d5 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d  ff ){.    *res =
1b5d6 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
1b5d7 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20  QLITE_CORRUPT;. 
1b5d8 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20   }.  memset(&m, 
1b5d9 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20  0, sizeof(m));. 
1b5da 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1b5db 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
1b5dc 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69  ->pCursor, 0, (i
1b5dd 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20  nt)nCellKey, 1, 
1b5de 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  &m);.  if( rc ){
1b5df 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1b5e0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55    }.  assert( pU
1b5e1 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26  npacked->flags &
1b5e2 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
1b5e3 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73  _ROWID );.  *res
1b5e4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1b5e5 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c  cordCompare(m.n,
1b5e6 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29   m.z, pUnpacked)
1b5e7 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1b5e8 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20  emRelease(&m);. 
1b5e9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1b5ea 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
1b5eb 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1b5ec 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65  e value to be re
1b5ed 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71  turned by subseq
1b5ee 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  uent calls to.**
1b5ef 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1b5f0 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  () on the databa
1b5f1 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20  se handle 'db'. 
1b5f2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b5f3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1b5f4 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71  dbeSetChanges(sq
1b5f5 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e  lite3 *db, int n
1b5f6 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72  Change){.  asser
1b5f7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b5f8 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
1b5f9 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67   );.  db->nChang
1b5fa 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64  e = nChange;.  d
1b5fb 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20  b->nTotalChange 
1b5fc 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f  += nChange;.}../
1b5fd 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20  *.** Set a flag 
1b5fe 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75  in the vdbe to u
1b5ff 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
1b600 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74   counter when it
1b601 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a   is finalised.**
1b602 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51   or reset..*/.SQ
1b603 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1b604 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75  d sqlite3VdbeCou
1b605 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a  ntChanges(Vdbe *
1b606 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43  v){.  v->changeC
1b607 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  ntOn = 1;.}../*.
1b608 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72  ** Mark every pr
1b609 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1b60a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1b60b 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
1b60c 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70  ection.** as exp
1b60d 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  ired..**.** An e
1b60e 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74  xpired statement
1b60f 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f   means that reco
1b610 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65  mpilation of the
1b611 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a   statement is.**
1b612 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61   recommend.  Sta
1b613 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77  tements expire w
1b614 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65  hen things happe
1b615 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69  n that make thei
1b616 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62  r.** programs ob
1b617 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e  solete.  Removin
1b618 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66  g user-defined f
1b619 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c  unctions or coll
1b61a 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63  ating.** sequenc
1b61b 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20  es, or changing 
1b61c 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
1b61d 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
1b61e 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68  e types of.** th
1b61f 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70  ings that make p
1b620 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1b621 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a  ts obsolete..*/.
1b622 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1b623 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72  oid sqlite3Expir
1b624 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
1b625 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nts(sqlite3 *db)
1b626 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66  {.  Vdbe *p;.  f
1b627 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65  or(p = db->pVdbe
1b628 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
1b629 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64  {.    p->expired
1b62a 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 1;.  }.}../*.
1b62b 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61  ** Return the da
1b62c 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65  tabase associate
1b62d 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e  d with the Vdbe.
1b62e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b62f 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  TE sqlite3 *sqli
1b630 74 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a  te3VdbeDb(Vdbe *
1b631 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e  v){.  return v->
1b632 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  db;.}../*.** Ret
1b633 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1b634 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
1b635 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
1b636 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65  aining the value
1b637 20 62 6f 75 6e 64 0a 2a 2a 20 70 61 72 61 6d 65   bound.** parame
1b638 74 65 72 20 69 56 61 72 20 6f 66 20 56 4d 20 76  ter iVar of VM v
1b639 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65  . Except, if the
1b63a 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51 4c   value is an SQL
1b63b 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 0a 2a   NULL, return .*
1b63c 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55 6e 6c  * 0 instead. Unl
1b63d 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20  ess it is NULL, 
1b63e 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 61  apply affinity a
1b63f 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65 20 53  ff (one of the S
1b640 51 4c 49 54 45 5f 41 46 46 5f 2a 0a 2a 2a 20 63  QLITE_AFF_*.** c
1b641 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74 68 65  onstants) to the
1b642 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 72 65   value before re
1b643 74 75 72 6e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a  turning it..**.*
1b644 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
1b645 61 6c 75 65 20 6d 75 73 74 20 62 65 20 66 72 65  alue must be fre
1b646 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
1b647 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 61   using sqlite3Va
1b648 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2f 0a 53 51  lueFree()..*/.SQ
1b649 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
1b64a 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
1b64b 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 65 28  te3VdbeGetValue(
1b64c 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56 61  Vdbe *v, int iVa
1b64d 72 2c 20 75 38 20 61 66 66 29 7b 0a 20 20 61 73  r, u8 aff){.  as
1b64e 73 65 72 74 28 20 69 56 61 72 3e 30 20 29 3b 0a  sert( iVar>0 );.
1b64f 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 4d    if( v ){.    M
1b650 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 76 2d 3e 61  em *pMem = &v->a
1b651 56 61 72 5b 69 56 61 72 2d 31 5d 3b 0a 20 20 20  Var[iVar-1];.   
1b652 20 69 66 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66   if( 0==(pMem->f
1b653 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1b654 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1b655 33 5f 76 61 6c 75 65 20 2a 70 52 65 74 20 3d 20  3_value *pRet = 
1b656 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
1b657 76 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 66  v->db);.      if
1b658 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20  ( pRet ){.      
1b659 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b65a 43 6f 70 79 28 28 4d 65 6d 20 2a 29 70 52 65 74  Copy((Mem *)pRet
1b65b 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  , pMem);.       
1b65c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70   sqlite3ValueApp
1b65d 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 74 2c  lyAffinity(pRet,
1b65e 20 61 66 66 2c 20 53 51 4c 49 54 45 5f 55 54 46   aff, SQLITE_UTF
1b65f 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  8);.        sqli
1b660 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54  te3VdbeMemStoreT
1b661 79 70 65 28 28 4d 65 6d 20 2a 29 70 52 65 74 29  ype((Mem *)pRet)
1b662 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1b663 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 20 20 20  return pRet;.   
1b664 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1b665 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66  0;.}../*.** Conf
1b666 69 67 75 72 65 20 53 51 4c 20 76 61 72 69 61 62  igure SQL variab
1b667 6c 65 20 69 56 61 72 20 73 6f 20 74 68 61 74 20  le iVar so that 
1b668 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20 76 61  binding a new va
1b669 6c 75 65 20 74 6f 20 69 74 20 73 69 67 6e 61 6c  lue to it signal
1b66a 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  s.** to sqlite3_
1b66b 72 65 6f 70 74 69 6d 69 7a 65 28 29 20 74 68 61  reoptimize() tha
1b66c 74 20 72 65 2d 70 72 65 70 61 72 69 6e 67 20 74  t re-preparing t
1b66d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79  he statement may
1b66e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 20   result.** in a 
1b66f 62 65 74 74 65 72 20 71 75 65 72 79 20 70 6c 61  better query pla
1b670 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1b671 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b672 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28  3VdbeSetVarmask(
1b673 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56 61  Vdbe *v, int iVa
1b674 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 56  r){.  assert( iV
1b675 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 20 69 56  ar>0 );.  if( iV
1b676 61 72 3e 33 32 20 29 7b 0a 20 20 20 20 76 2d 3e  ar>32 ){.    v->
1b677 65 78 70 6d 61 73 6b 20 3d 20 30 78 66 66 66 66  expmask = 0xffff
1b678 66 66 66 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ffff;.  }else{. 
1b679 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 7c 3d     v->expmask |=
1b67a 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69 56 61   ((u32)1 << (iVa
1b67b 72 2d 31 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  r-1));.  }.}../*
1b67c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1b67d 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 2a  d of vdbeaux.c *
1b67e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b67f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1b681 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1b682 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 69  gin file vdbeapi
1b683 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1b684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1b686 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a  .** 2004 May 26.
1b687 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1b688 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1b689 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1b68a 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1b68b 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1b68c 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1b68d 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1b68e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1b68f 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1b690 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1b691 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1b692 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1b693 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1b694 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1b695 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1b696 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1b697 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1b698 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1b699 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b69a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b69b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b69c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1b69d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1b69e 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
1b69f 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50   to implement AP
1b6a0 49 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74  Is that are part
1b6a1 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   of the.** VDBE.
1b6a2 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
1b6a3 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
1b6a4 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  TED./*.** Return
1b6a5 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29   TRUE (non-zero)
1b6a6 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   of the statemen
1b6a7 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e  t supplied as an
1b6a8 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a   argument needs.
1b6a9 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69  ** to be recompi
1b6aa 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d 65 6e  led.  A statemen
1b6ab 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  t needs to be re
1b6ac 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 76 65  compiled wheneve
1b6ad 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 74 69  r the.** executi
1b6ae 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63  on environment c
1b6af 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 79 20  hanges in a way 
1b6b0 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 65 72  that would alter
1b6b1 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20   the program.** 
1b6b2 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 72 65  that sqlite3_pre
1b6b3 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 65 73  pare() generates
1b6b4 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
1b6b5 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73  if new functions
1b6b6 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67   or.** collating
1b6b7 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 72   sequences are r
1b6b8 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 66 20  egistered or if 
1b6b9 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 66 75  an authorizer fu
1b6ba 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64  nction is.** add
1b6bb 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a  ed or changed..*
1b6bc 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1b6bd 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
1b6be 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b6bf 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1b6c0 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1b6c1 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
1b6c2 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a  | p->expired;.}.
1b6c3 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1b6c4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1b6c5 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 76  ine destroys a v
1b6c6 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74  irtual machine t
1b6c7 68 61 74 20 69 73 20 63 72 65 61 74 65 64 20 62  hat is created b
1b6c8 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  y.** the sqlite3
1b6c9 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 74 69  _compile() routi
1b6ca 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20  ne. The integer 
1b6cb 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e 20 53  returned is an S
1b6cc 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73  QLITE_.** succes
1b6cd 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 20 74  s/failure code t
1b6ce 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
1b6cf 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 65 63  e result of exec
1b6d0 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 75 61  uting the virtua
1b6d1 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  l.** machine..**
1b6d2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1b6d3 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
1b6d4 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
1b6d5 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
1b6d6 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
1b6d7 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
1b6d8 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
1b6d9 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
1b6da 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b6db 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71  ite3_finalize(sq
1b6dc 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1b6dd 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
1b6de 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a  if( pStmt==0 ){.
1b6df 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1b6e0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
1b6e1 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
1b6e2 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
1b6e3 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62  ite3 *db = v->db
1b6e4 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
1b6e5 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69  EADSAFE.    sqli
1b6e6 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
1b6e7 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b   = v->db->mutex;
1b6e8 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69  .#endif.    sqli
1b6e9 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1b6ea 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d  mutex);.    rc =
1b6eb 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
1b6ec 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 63 20  lize(v);.    rc 
1b6ed 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
1b6ee 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71  (db, rc);.    sq
1b6ef 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1b6f0 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  e(mutex);.  }.  
1b6f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1b6f2 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 74 68  .** Terminate th
1b6f3 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 75 74  e current execut
1b6f4 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 74  ion of an SQL st
1b6f5 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 73 65  atement and rese
1b6f6 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 6f 20  t it.** back to 
1b6f7 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 74 61  its starting sta
1b6f8 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  te so that it ca
1b6f9 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 20 73  n be reused. A s
1b6fa 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f 6d  uccess code from
1b6fb 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 65 78  .** the prior ex
1b6fc 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 75 72  ecution is retur
1b6fd 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
1b6fe 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
1b6ff 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
1b700 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20  string returned 
1b701 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72  by.** sqlite3_er
1b702 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33  rcode(), sqlite3
1b703 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
1b704 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
1b705 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b706 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73 65  int sqlite3_rese
1b707 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1b708 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63  pStmt){.  int rc
1b709 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30  ;.  if( pStmt==0
1b70a 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1b70b 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
1b70c 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28  .    Vdbe *v = (
1b70d 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20  Vdbe*)pStmt;.   
1b70e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1b70f 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 74 65  nter(v->db->mute
1b710 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  x);.    rc = sql
1b711 69 74 65 33 56 64 62 65 52 65 73 65 74 28 76 29  ite3VdbeReset(v)
1b712 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1b713 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d 31  eMakeReady(v, -1
1b714 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29  , 0, 0, 0, 0, 0)
1b715 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72  ;.    assert( (r
1b716 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d  c & (v->db->errM
1b717 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20  ask))==rc );.   
1b718 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1b719 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 29 3b  Exit(v->db, rc);
1b71a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1b71b 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e  ex_leave(v->db->
1b71c 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1b71d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1b71e 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 61  * Set all the pa
1b71f 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 20  rameters in the 
1b720 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
1b721 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a  tement to NULL..
1b722 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1b723 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f  t sqlite3_clear_
1b724 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  bindings(sqlite3
1b725 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
1b726 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
1b727 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1b728 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1b729 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49  )pStmt;.#if SQLI
1b72a 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
1b72b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
1b72c 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 70  utex = ((Vdbe*)p
1b72d 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78  Stmt)->db->mutex
1b72e 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
1b72f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
1b730 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30  utex);.  for(i=0
1b731 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b  ; i<p->nVar; i++
1b732 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1b733 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d  beMemRelease(&p-
1b734 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70  >aVar[i]);.    p
1b735 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20  ->aVar[i].flags 
1b736 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a  = MEM_Null;.  }.
1b737 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61    if( p->isPrepa
1b738 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70 6d 61  reV2 && p->expma
1b739 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70  sk ){.    p->exp
1b73a 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  ired = 1;.  }.  
1b73b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1b73c 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65  ave(mutex);.  re
1b73d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a  turn rc;.}.../**
1b73e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b73f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ********** sqlit
1b740 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a  e3_value_  *****
1b741 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b742 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1b743 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1b744 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e 66  ines extract inf
1b745 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20  ormation from a 
1b746 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76  Mem or sqlite3_v
1b747 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72  alue.** structur
1b748 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
1b749 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1b74a 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
1b74b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b74c 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d  Val){.  Mem *p =
1b74d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69   (Mem*)pVal;.  i
1b74e 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d  f( p->flags & (M
1b74f 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29  EM_Blob|MEM_Str)
1b750 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1b751 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
1b752 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  (p);.    p->flag
1b753 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20  s &= ~MEM_Str;. 
1b754 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d     p->flags |= M
1b755 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74  EM_Blob;.    ret
1b756 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73  urn p->z;.  }els
1b757 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  e{.    return sq
1b758 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1b759 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51  (pVal);.  }.}.SQ
1b75a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b75b 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1b75c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1b75d 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1b75e 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
1b75f 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  s(pVal, SQLITE_U
1b760 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  TF8);.}.SQLITE_A
1b761 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
1b762 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c  alue_bytes16(sql
1b763 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1b764 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1b765 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56  te3ValueBytes(pV
1b766 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  al, SQLITE_UTF16
1b767 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54  NATIVE);.}.SQLIT
1b768 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c  E_API double sql
1b769 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
1b76a 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e(sqlite3_value 
1b76b 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
1b76c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
1b76d 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c  Value((Mem*)pVal
1b76e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b76f 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
1b770 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61  e_int(sqlite3_va
1b771 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
1b772 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 65  turn (int)sqlite
1b773 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d  3VdbeIntValue((M
1b774 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c  em*)pVal);.}.SQL
1b775 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69  ITE_API sqlite_i
1b776 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c  nt64 sqlite3_val
1b777 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33  ue_int64(sqlite3
1b778 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1b779 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1b77a 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d  dbeIntValue((Mem
1b77b 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  *)pVal);.}.SQLIT
1b77c 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69  E_API const unsi
1b77d 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74  gned char *sqlit
1b77e 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71  e3_value_text(sq
1b77f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b780 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 6f  l){.  return (co
1b781 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1b782 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65  r *)sqlite3Value
1b783 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
1b784 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64  E_UTF8);.}.#ifnd
1b785 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1b786 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1b787 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1b788 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1b789 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
1b78a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1b78b 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
1b78c 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
1b78d 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51  F16NATIVE);.}.SQ
1b78e 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1b78f 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  oid *sqlite3_val
1b790 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69  ue_text16be(sqli
1b791 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1b792 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1b793 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
1b794 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  , SQLITE_UTF16BE
1b795 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b796 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1b797 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1b798 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
1b799 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1b79a 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  n sqlite3ValueTe
1b79b 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
1b79c 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64  UTF16LE);.}.#end
1b79d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1b79e 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54  T_UTF16 */.SQLIT
1b79f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b7a0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c  3_value_type(sql
1b7a1 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c  ite3_value* pVal
1b7a2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c  ){.  return pVal
1b7a3 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  ->type;.}../****
1b7a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7a5 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
1b7a6 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a  _result_  ******
1b7a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
1b7a9 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
1b7aa 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20  nes are used by 
1b7ab 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
1b7ac 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 66  ctions to specif
1b7ad 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f  y.** the functio
1b7ae 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  n result..**.** 
1b7af 54 68 65 20 73 65 74 53 74 72 4f 72 45 72 72 6f  The setStrOrErro
1b7b0 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c  r() funtion call
1b7b1 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  s sqlite3VdbeMem
1b7b2 53 65 74 53 74 72 28 29 20 74 6f 20 73 74 6f 72  SetStr() to stor
1b7b3 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20  e the.** result 
1b7b4 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  as a string or b
1b7b5 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65 20 73  lob but if the s
1b7b6 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73  tring or blob is
1b7b7 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a   too large, it.*
1b7b8 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20  * then sets the 
1b7b9 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51  error code to SQ
1b7ba 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73  LITE_TOOBIG.*/.s
1b7bb 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 52 65  tatic void setRe
1b7bc 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 0a  sultStrOrError(.
1b7bd 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b7be 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e  t *pCtx,  /* Fun
1b7bf 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
1b7c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1b7c1 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ,          /* St
1b7c2 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  ring pointer */.
1b7c3 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20    int n,        
1b7c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
1b7c5 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72  es in string, or
1b7c6 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 75   negative */.  u
1b7c7 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20  8 enc,          
1b7c8 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69         /* Encodi
1b7c9 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20  ng of z.  0 for 
1b7ca 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20  BLOBs */.  void 
1b7cb 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20  (*xDel)(void*)  
1b7cc 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72     /* Destructor
1b7cd 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
1b7ce 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1b7cf 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
1b7d0 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20  ->s, z, n, enc, 
1b7d1 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f  xDel)==SQLITE_TO
1b7d2 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71 6c 69  OBIG ){.    sqli
1b7d3 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1b7d4 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a 20  _toobig(pCtx);. 
1b7d5 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20   }.}.SQLITE_API 
1b7d6 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b7d7 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69  ult_blob(.  sqli
1b7d8 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b7d9 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  x, .  const void
1b7da 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a   *z, .  int n, .
1b7db 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1b7dc 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65  oid *).){.  asse
1b7dd 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73  rt( n>=0 );.  as
1b7de 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b7df 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b7e0 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b7e1 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45   setResultStrOrE
1b7e2 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c  rror(pCtx, z, n,
1b7e3 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c   0, xDel);.}.SQL
1b7e4 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b7e5 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
1b7e6 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  le(sqlite3_conte
1b7e7 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65  xt *pCtx, double
1b7e8 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74   rVal){.  assert
1b7e9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b7ea 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b7eb 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
1b7ec 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f  ite3VdbeMemSetDo
1b7ed 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 72  uble(&pCtx->s, r
1b7ee 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Val);.}.SQLITE_A
1b7ef 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b7f0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c  result_error(sql
1b7f1 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b7f2 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tx, const char *
1b7f3 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73  z, int n){.  ass
1b7f4 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b7f5 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b7f6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b7f7 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
1b7f8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1b7f9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b7fa 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a  tStr(&pCtx->s, z
1b7fb 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , n, SQLITE_UTF8
1b7fc 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
1b7fd 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  NT);.}.#ifndef S
1b7fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1b7ff 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b800 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b801 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
1b802 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63  context *pCtx, c
1b803 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
1b804 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t n){.  assert( 
1b805 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b806 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b807 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d  utex) );.  pCtx-
1b808 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54  >isError = SQLIT
1b809 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74  E_ERROR;.  sqlit
1b80a 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1b80b 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20  &pCtx->s, z, n, 
1b80c 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
1b80d 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  VE, SQLITE_TRANS
1b80e 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  IENT);.}.#endif.
1b80f 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b810 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
1b811 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
1b812 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 56  xt *pCtx, int iV
1b813 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  al){.  assert( s
1b814 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b815 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b816 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1b817 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
1b818 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 29  (&pCtx->s, (i64)
1b819 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  iVal);.}.SQLITE_
1b81a 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b81b 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71  _result_int64(sq
1b81c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b81d 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a  Ctx, i64 iVal){.
1b81e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b81f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b820 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b821 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1b822 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74  MemSetInt64(&pCt
1b823 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53  x->s, iVal);.}.S
1b824 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b825 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
1b826 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ll(sqlite3_conte
1b827 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73  xt *pCtx){.  ass
1b828 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b829 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b82a 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b82b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b82c 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b  tNull(&pCtx->s);
1b82d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b82e 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b82f 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74 65  t_text(.  sqlite
1b830 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b831 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
1b832 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76  z, .  int n,.  v
1b833 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1b834 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   *).){.  assert(
1b835 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b836 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b837 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52  mutex) );.  setR
1b838 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28  esultStrOrError(
1b839 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49  pCtx, z, n, SQLI
1b83a 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a  TE_UTF8, xDel);.
1b83b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1b83c 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1b83d 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b83e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
1b83f 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  6(.  sqlite3_con
1b840 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
1b841 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  onst void *z, . 
1b842 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20   int n, .  void 
1b843 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
1b844 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b845 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b846 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b847 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c  x) );.  setResul
1b848 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78  tStrOrError(pCtx
1b849 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
1b84a 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c  TF16NATIVE, xDel
1b84b 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b84c 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b84d 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20  ult_text16be(.  
1b84e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b84f 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
1b850 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  void *z, .  int 
1b851 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  n, .  void (*xDe
1b852 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20  l)(void *).){.  
1b853 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b854 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b855 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b856 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f  .  setResultStrO
1b857 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20  rError(pCtx, z, 
1b858 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  n, SQLITE_UTF16B
1b859 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49  E, xDel);.}.SQLI
1b85a 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b85b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
1b85c 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  6le(.  sqlite3_c
1b85d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20  ontext *pCtx, . 
1b85e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
1b85f 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69  .  int n, .  voi
1b860 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
1b861 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ).){.  assert( s
1b862 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b863 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b864 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73  tex) );.  setRes
1b865 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43  ultStrOrError(pC
1b866 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  tx, z, n, SQLITE
1b867 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b  _UTF16LE, xDel);
1b868 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1b869 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1b86a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
1b86b 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b86c 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63  _value(sqlite3_c
1b86d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71  ontext *pCtx, sq
1b86e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b86f 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  lue){.  assert( 
1b870 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b871 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b872 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b873 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70  e3VdbeMemCopy(&p
1b874 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b  Ctx->s, pValue);
1b875 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b876 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b877 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74  t_zeroblob(sqlit
1b878 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b879 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65  , int n){.  asse
1b87a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b87b 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b87c 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1b87d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b87e 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e  ZeroBlob(&pCtx->
1b87f 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  s, n);.}.SQLITE_
1b880 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b881 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
1b882 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  de(sqlite3_conte
1b883 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72  xt *pCtx, int er
1b884 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e  rCode){.  pCtx->
1b885 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 6f 64  isError = errCod
1b886 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 73  e;.  if( pCtx->s
1b887 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  .flags & MEM_Nul
1b888 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1b889 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
1b88a 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45  Ctx->s, sqlite3E
1b88b 72 72 53 74 72 28 65 72 72 43 6f 64 65 29 2c 20  rrStr(errCode), 
1b88c 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  -1, .           
1b88d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
1b88e 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
1b88f 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
1b890 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51  ../* Force an SQ
1b891 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f  LITE_TOOBIG erro
1b892 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r. */.SQLITE_API
1b893 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b894 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
1b895 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
1b896 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65  t *pCtx){.  asse
1b897 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b898 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b899 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1b89a 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53  Ctx->isError = S
1b89b 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20  QLITE_TOOBIG;.  
1b89c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b89d 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22  tStr(&pCtx->s, "
1b89e 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74  string or blob t
1b89f 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20  oo big", -1, .  
1b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b8a1 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38       SQLITE_UTF8
1b8a2 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1b8a3 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54  ;.}../* An SQLIT
1b8a4 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a  E_NOMEM error. *
1b8a5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
1b8a6 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b8a7 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c  _error_nomem(sql
1b8a8 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b8a9 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  tx){.  assert( s
1b8aa 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b8ab 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b8ac 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1b8ad 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1b8ae 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74  &pCtx->s);.  pCt
1b8af 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
1b8b0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74  ITE_NOMEM;.  pCt
1b8b1 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  x->s.db->mallocF
1b8b2 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  ailed = 1;.}../*
1b8b3 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20  .** Execute the 
1b8b4 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c  statement pStmt,
1b8b5 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20   either until a 
1b8b6 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72  row of data is r
1b8b7 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61  eady, the.** sta
1b8b8 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65  tement is comple
1b8b9 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72  tely executed or
1b8ba 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1b8bb 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1b8bc 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tine implements 
1b8bd 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20  the bulk of the 
1b8be 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 65  logic behind the
1b8bf 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a   sqlite_step().*
1b8c0 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79  * API.  The only
1b8c1 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69   thing omitted i
1b8c2 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20  s the automatic 
1b8c3 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a  recompile if a .
1b8c4 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ** schema change
1b8c5 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20   has occurred.  
1b8c6 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 68  That detail is h
1b8c7 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a  andled by the.**
1b8c8 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73   outer sqlite3_s
1b8c9 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 72  tep() wrapper pr
1b8ca 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ocedure..*/.stat
1b8cb 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  ic int sqlite3St
1b8cc 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73  ep(Vdbe *p){.  s
1b8cd 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e  qlite3 *db;.  in
1b8ce 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1b8cf 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  p);.  if( p->mag
1b8d0 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
1b8d1 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  UN ){.    return
1b8d2 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
1b8d3 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74    }..  /* Assert
1b8d4 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68   that malloc() h
1b8d5 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f  as not failed */
1b8d6 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  .  db = p->db;. 
1b8d7 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1b8d8 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
1b8d9 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1b8da 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
1b8db 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69  pc<=0 && p->expi
1b8dc 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 41  red ){.    if( A
1b8dd 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c  LWAYS(p->rc==SQL
1b8de 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d  ITE_OK || p->rc=
1b8df 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 29 20  =SQLITE_SCHEMA) 
1b8e0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  ){.      p->rc =
1b8e1 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
1b8e2 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53      }.    rc = S
1b8e3 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1b8e4 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65   goto end_of_ste
1b8e5 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  p;.  }.  if( sql
1b8e6 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1b8e7 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1b8e8 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1b8e9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b8ea 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
1b8eb 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20  f( p->pc<0 ){.  
1b8ec 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1b8ed 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65  e no other state
1b8ee 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20  ments currently 
1b8ef 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20  running, then.  
1b8f0 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69    ** reset the i
1b8f1 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20  nterrupt flag.  
1b8f2 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1b8f3 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
1b8f4 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a  interrupt.    **
1b8f5 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69   from interrupti
1b8f6 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ng a statement t
1b8f7 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  hat has not yet 
1b8f8 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  started..    */.
1b8f9 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
1b8fa 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a  veVdbeCnt==0 ){.
1b8fb 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49        db->u1.isI
1b8fc 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a  nterrupted = 0;.
1b8fd 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
1b8fe 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65  t( db->writeVdbe
1b8ff 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74  Cnt>0 || db->aut
1b900 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
1b901 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3d  ->nDeferredCons=
1b902 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  =0 );..#ifndef S
1b903 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1b904 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72  .    if( db->xPr
1b905 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
1b906 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
1b907 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1b908 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72      sqlite3OsCur
1b909 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
1b90a 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20  s, &rNow);.     
1b90b 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20   p->startTime = 
1b90c 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69  (u64)((rNow - (i
1b90d 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
1b90e 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
1b90f 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  0);.    }.#endif
1b910 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65  ..    db->active
1b911 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69  VdbeCnt++;.    i
1b912 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d  f( p->readOnly==
1b913 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62  0 ) db->writeVdb
1b914 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70  eCnt++;.    p->p
1b915 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64  c = 0;.  }.#ifnd
1b916 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
1b917 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e  XPLAIN.  if( p->
1b918 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72  explain ){.    r
1b919 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c  c = sqlite3VdbeL
1b91a 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a  ist(p);.  }else.
1b91b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b91c 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f  _OMIT_EXPLAIN */
1b91d 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  .  {.    rc = sq
1b91e 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29  lite3VdbeExec(p)
1b91f 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c  ;.  }..  if( sql
1b920 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1b921 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  ) ){.    rc = SQ
1b922 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
1b923 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1b924 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a  _OMIT_TRACE.  /*
1b925 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66   Invoke the prof
1b926 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  ile callback if 
1b927 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a  there is one.  *
1b928 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
1b929 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50  TE_ROW && db->xP
1b92a 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69  rofile && !db->i
1b92b 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 7a  nit.busy && p->z
1b92c 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c  Sql ){.    doubl
1b92d 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20  e rNow;.    u64 
1b92e 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20  elapseTime;..   
1b92f 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
1b930 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20  tTime(db->pVfs, 
1b931 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70  &rNow);.    elap
1b932 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28  seTime = (u64)((
1b933 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77  rNow - (int)rNow
1b934 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30  )*3600.0*24.0*10
1b935 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20  00000000.0);.   
1b936 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20 70   elapseTime -= p
1b937 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 20  ->startTime;.   
1b938 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62   db->xProfile(db
1b939 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 70  ->pProfileArg, p
1b93a 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65 54 69  ->zSql, elapseTi
1b93b 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  me);.  }.#endif.
1b93c 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d  .  db->errCode =
1b93d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c 49 54   rc;.  if( SQLIT
1b93e 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33  E_NOMEM==sqlite3
1b93f 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70  ApiExit(p->db, p
1b940 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70 2d 3e  ->rc) ){.    p->
1b941 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1b942 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74  M;.  }.end_of_st
1b943 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73  ep:.  /* At this
1b944 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76 61 72   point local var
1b945 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73 20 74  iable rc holds t
1b946 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 73 68  he value that sh
1b947 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20 72 65  ould be .  ** re
1b948 74 75 72 6e 65 64 20 69 66 20 74 68 69 73 20 73  turned if this s
1b949 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f 6d  tatement was com
1b94a 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68 65 20  piled using the 
1b94b 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73 71 6c  legacy .  ** sql
1b94c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 69  ite3_prepare() i
1b94d 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f 72 64  nterface. Accord
1b94e 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63 73 2c  ing to the docs,
1b94f 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 0a 20   this can only. 
1b950 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68   ** be one of th
1b951 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  e values in the 
1b952 66 69 72 73 74 20 61 73 73 65 72 74 28 29 20 62  first assert() b
1b953 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65 20 70  elow. Variable p
1b954 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e 74 61  ->rc .  ** conta
1b955 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 74 68  ins the value th
1b956 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65 74 75  at would be retu
1b957 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65 33 5f  rned if sqlite3_
1b958 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20 2a 2a  finalize() .  **
1b959 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20   were called on 
1b95a 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20 20 2a  statement p..  *
1b95b 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  /.  assert( rc==
1b95c 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c 20 72  SQLITE_ROW  || r
1b95d 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20  c==SQLITE_DONE  
1b95e 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45   || rc==SQLITE_E
1b95f 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c 7c 20  RROR .       || 
1b960 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1b961 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4d 49  || rc==SQLITE_MI
1b962 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73 73 65  SUSE.  );.  asse
1b963 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54  rt( p->rc!=SQLIT
1b964 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 21 3d  E_ROW && p->rc!=
1b965 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20  SQLITE_DONE );. 
1b966 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72   if( p->isPrepar
1b967 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c 49 54  eV2 && rc!=SQLIT
1b968 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c  E_ROW && rc!=SQL
1b969 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
1b96a 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65  /* If this state
1b96b 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65  ment was prepare
1b96c 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
1b96d 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 61 6e  prepare_v2(), an
1b96e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72 72 6f  d an.    ** erro
1b96f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20 74  r has occured, t
1b970 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 65  hen return the e
1b971 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70 2d 3e  rror code in p->
1b972 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a  rc to the.    **
1b973 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74 68 65   caller. Set the
1b974 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 74   error code in t
1b975 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1b976 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 76  le to the same v
1b977 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a 20 20  alue..    */ .  
1b978 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 43 6f    rc = db->errCo
1b979 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a  de = p->rc;.  }.
1b97a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 62 2d    return (rc&db-
1b97b 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a  >errMask);.}../*
1b97c 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
1b97d 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d  top-level implem
1b97e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
1b97f 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43 61 6c  te3_step().  Cal
1b980 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 70  l.** sqlite3Step
1b981 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66  () to do most of
1b982 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 61   the work.  If a
1b983 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20 6f 63   schema error oc
1b984 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71  curs,.** call sq
1b985 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29  lite3Reprepare()
1b986 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a   and try again..
1b987 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1b988 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  t sqlite3_step(s
1b989 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b98a 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  mt){.  int rc = 
1b98b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1b98c 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20   if( pStmt ){.  
1b98d 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
1b98e 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64     Vdbe *v = (Vd
1b98f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73  be*)pStmt;.    s
1b990 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e  qlite3 *db = v->
1b991 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  db;.    sqlite3_
1b992 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1b993 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c  mutex);.    whil
1b994 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  e( (rc = sqlite3
1b995 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45  Step(v))==SQLITE
1b996 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20  _SCHEMA.        
1b997 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a     && cnt++ < 5.
1b998 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 72             && (r
1b999 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70 72 65  c = sqlite3Repre
1b99a 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49 54 45  pare(v))==SQLITE
1b99b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
1b99c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
1b99d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70 69  );.      v->expi
1b99e 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  red = 0;.    }. 
1b99f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1b9a0 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c 57 41  E_SCHEMA && ALWA
1b9a1 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72 65 56  YS(v->isPrepareV
1b9a2 32 29 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d  2) && ALWAYS(db-
1b9a3 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20 20 20  >pErr) ){.      
1b9a4 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63  /* This case occ
1b9a5 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69 6e  urs after failin
1b9a6 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61  g to recompile a
1b9a7 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e  n sql statement.
1b9a8 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65   .      ** The e
1b9a9 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f  rror message fro
1b9aa 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c  m the SQL compil
1b9ab 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  er has already b
1b9ac 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20 20  een loaded .    
1b9ad 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 61    ** into the da
1b9ae 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54  tabase handle. T
1b9af 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73  his block copies
1b9b0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
1b9b1 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f  ge .      ** fro
1b9b2 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  m the database h
1b9b3 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20 73  andle into the s
1b9b4 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65 74  tatement and set
1b9b5 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a  s the statement.
1b9b6 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d        ** program
1b9b7 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 6f   counter to 0 to
1b9b8 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 65   ensure that whe
1b9b9 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
1b9ba 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e  is .      ** fin
1b9bb 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74 20  alized or reset 
1b9bc 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f 72  the parser error
1b9bd 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69   message is avai
1b9be 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20 20  lable via.      
1b9bf 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  ** sqlite3_errms
1b9c0 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
1b9c1 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20 20  errcode()..     
1b9c2 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
1b9c3 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f  char *zErr = (co
1b9c4 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
1b9c5 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62  e3_value_text(db
1b9c6 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20 20  ->pErr); .      
1b9c7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1b9c8 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , v->zErrMsg);. 
1b9c9 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
1b9ca 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1b9cb 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67        v->zErrMsg
1b9cc 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
1b9cd 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  up(db, zErr);.  
1b9ce 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
1b9cf 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20       v->zErrMsg 
1b9d0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e  = 0;.        v->
1b9d1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1b9d2 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
1b9d3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b9d4 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
1b9d5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1b9d6 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
1b9d7 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1b9d8 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1b9d9 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72  Extract the user
1b9da 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c   data from a sql
1b9db 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72  ite3_context str
1b9dc 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
1b9dd 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  n a.** pointer t
1b9de 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o it..*/.SQLITE_
1b9df 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
1b9e0 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c 69  3_user_data(sqli
1b9e1 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b  te3_context *p){
1b9e2 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1b9e3 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65  p->pFunc );.  re
1b9e4 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e 70  turn p->pFunc->p
1b9e5 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a  UserData;.}../*.
1b9e6 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75  ** Extract the u
1b9e7 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ser data from a 
1b9e8 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b9e9 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65  structure and re
1b9ea 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  turn a.** pointe
1b9eb 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49  r to it..*/.SQLI
1b9ec 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a  TE_API sqlite3 *
1b9ed 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
1b9ee 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65  db_handle(sqlite
1b9ef 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20  3_context *p){. 
1b9f0 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
1b9f1 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75  >pFunc );.  retu
1b9f2 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f  rn p->s.db;.}../
1b9f3 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1b9f4 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  ng is the implem
1b9f5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53  entation of an S
1b9f6 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  QL function that
1b9f7 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c 73   always.** fails
1b9f8 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
1b9f9 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20 74  essage stating t
1b9fa 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  hat the function
1b9fb 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 0a   is used in the.
1b9fc 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74  ** wrong context
1b9fd 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  .  The sqlite3_o
1b9fe 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
1b9ff 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f 6e  () API might con
1ba00 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66 75  struct.** SQL fu
1ba01 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 20  nction that use 
1ba02 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f 20  this routine so 
1ba03 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
1ba04 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a 2a  ns will exist.**
1ba05 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75   for name resolu
1ba06 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63 74  tion but are act
1ba07 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64  ually overloaded
1ba08 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75 6e   by the xFindFun
1ba09 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20  ction.** method 
1ba0a 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  of virtual table
1ba0b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
1ba0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1ba0d 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e  3InvalidFunction
1ba0e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1ba0f 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20 2f  ext *context,  /
1ba10 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 63  * The function c
1ba11 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  alling context *
1ba12 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  /.  int NotUsed,
1ba13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ba14 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
1ba15 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
1ba16 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
1ba17 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
1ba18 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f  ed2   /* Value o
1ba19 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20  f each argument 
1ba1a 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  */.){.  const ch
1ba1b 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74  ar *zName = cont
1ba1c 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d  ext->pFunc->zNam
1ba1d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b  e;.  char *zErr;
1ba1e 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1ba1f 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
1ba20 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72 20  tUsed2);.  zErr 
1ba21 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
1ba22 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 6c 65  f(.      "unable
1ba23 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e   to use function
1ba24 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75 65   %s in the reque
1ba25 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a  sted context", z
1ba26 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
1ba27 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
1ba28 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
1ba29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
1ba2a 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (zErr);.}../*.**
1ba2b 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65 74   Allocate or ret
1ba2c 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61 74  urn the aggregat
1ba2d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61 20  e context for a 
1ba2e 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20  user function.  
1ba2f 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74  A new.** context
1ba30 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e   is allocated on
1ba31 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 2e   the first call.
1ba32 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c    Subsequent cal
1ba33 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  ls return the.**
1ba34 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 68   same context th
1ba35 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20  at was returned 
1ba36 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a  on prior calls..
1ba37 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
1ba38 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72  id *sqlite3_aggr
1ba39 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71  egate_context(sq
1ba3a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1ba3b 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
1ba3c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73  Mem *pMem;.  ass
1ba3d 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75  ert( p && p->pFu
1ba3e 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e  nc && p->pFunc->
1ba3f 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65 72  xStep );.  asser
1ba40 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1ba41 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d  _held(p->s.db->m
1ba42 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20  utex) );.  pMem 
1ba43 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28  = p->pMem;.  if(
1ba44 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
1ba45 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20  MEM_Agg)==0 ){. 
1ba46 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20     if( nByte==0 
1ba47 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1ba48 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
1ba49 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20  ternal(pMem);.  
1ba4a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1ba4b 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
1ba4c 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20    pMem->z = 0;. 
1ba4d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ba4e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1ba4f 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
1ba50 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0);.      pMem->
1ba51 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b  flags = MEM_Agg;
1ba52 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70  .      pMem->u.p
1ba53 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a  Def = p->pFunc;.
1ba54 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e        if( pMem->
1ba55 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  z ){.        mem
1ba56 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20  set(pMem->z, 0, 
1ba57 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a  nByte);.      }.
1ba58 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1ba59 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e  rn (void*)pMem->
1ba5a 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  z;.}../*.** Retu
1ba5b 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20  rn the auxilary 
1ba5c 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66  data pointer, if
1ba5d 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41   any, for the iA
1ba5e 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74  rg'th argument t
1ba5f 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75  o.** the user-fu
1ba60 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62  nction defined b
1ba61 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54  y pCtx..*/.SQLIT
1ba62 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
1ba63 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
1ba64 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1ba65 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29  *pCtx, int iArg)
1ba66 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56  {.  VdbeFunc *pV
1ba67 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65  dbeFunc;..  asse
1ba68 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1ba69 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1ba6a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1ba6b 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d  VdbeFunc = pCtx-
1ba6c 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66  >pVdbeFunc;.  if
1ba6d 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20  ( !pVdbeFunc || 
1ba6e 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d  iArg>=pVdbeFunc-
1ba6f 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20  >nAux || iArg<0 
1ba70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1ba71 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56  .  }.  return pV
1ba72 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69  dbeFunc->apAux[i
1ba73 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a  Arg].pAux;.}../*
1ba74 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69  .** Set the auxi
1ba75 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65  lary data pointe
1ba76 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e  r and delete fun
1ba77 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69  ction, for the i
1ba78 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65  Arg'th.** argume
1ba79 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66  nt to the user-f
1ba7a 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  unction defined 
1ba7b 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65  by pCtx. Any pre
1ba7c 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a  vious value is.*
1ba7d 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c  * deleted by cal
1ba7e 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20  ling the delete 
1ba7f 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69  function specifi
1ba80 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73  ed when it was s
1ba81 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
1ba82 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73  I void sqlite3_s
1ba83 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71  et_auxdata(.  sq
1ba84 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1ba85 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67  Ctx, .  int iArg
1ba86 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  , .  void *pAux,
1ba87 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65   .  void (*xDele
1ba88 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  te)(void*).){.  
1ba89 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a  struct AuxData *
1ba8a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65  pAuxData;.  Vdbe
1ba8b 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b  Func *pVdbeFunc;
1ba8c 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20  .  if( iArg<0 ) 
1ba8d 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20  goto failed;..  
1ba8e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1ba8f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1ba90 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1ba91 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70  .  pVdbeFunc = p
1ba92 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a  Ctx->pVdbeFunc;.
1ba93 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63    if( !pVdbeFunc
1ba94 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e   || pVdbeFunc->n
1ba95 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20  Aux<=iArg ){.   
1ba96 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64   int nAux = (pVd
1ba97 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75  beFunc ? pVdbeFu
1ba98 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20  nc->nAux : 0);. 
1ba99 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d     int nMalloc =
1ba9a 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63   sizeof(VdbeFunc
1ba9b 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63  ) + sizeof(struc
1ba9c 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b  t AuxData)*iArg;
1ba9d 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d  .    pVdbeFunc =
1ba9e 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
1ba9f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56  c(pCtx->s.db, pV
1baa0 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63  dbeFunc, nMalloc
1baa1 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62  );.    if( !pVdb
1baa2 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67  eFunc ){.      g
1baa3 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
1baa4 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62  }.    pCtx->pVdb
1baa5 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e  eFunc = pVdbeFun
1baa6 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70  c;.    memset(&p
1baa7 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
1baa8 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66  nAux], 0, sizeof
1baa9 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29  (struct AuxData)
1baaa 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b  *(iArg+1-nAux));
1baab 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e  .    pVdbeFunc->
1baac 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20  nAux = iArg+1;. 
1baad 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46     pVdbeFunc->pF
1baae 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e  unc = pCtx->pFun
1baaf 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61  c;.  }..  pAuxDa
1bab0 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d  ta = &pVdbeFunc-
1bab1 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20  >apAux[iArg];.  
1bab2 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41  if( pAuxData->pA
1bab3 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e  ux && pAuxData->
1bab4 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70  xDelete ){.    p
1bab5 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65  AuxData->xDelete
1bab6 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29  (pAuxData->pAux)
1bab7 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61  ;.  }.  pAuxData
1bab8 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20  ->pAux = pAux;. 
1bab9 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
1baba 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20  te = xDelete;.  
1babb 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a  return;..failed:
1babc 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29  .  if( xDelete )
1babd 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41  {.    xDelete(pA
1babe 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ux);.  }.}..#ifn
1babf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bac0 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
1bac1 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1bac2 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20  er of times the 
1bac3 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66  Step function of
1bac4 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73   a aggregate has
1bac5 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64   been .** called
1bac6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
1bac7 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
1bac8 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65  ted.  Do not use
1bac9 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65   it for new code
1baca 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76  .  It is.** prov
1bacb 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69  ide only to avoi
1bacc 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63  d breaking legac
1bacd 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67  y code.  New agg
1bace 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a  regate function.
1bacf 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
1bad0 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74  ns should keep t
1bad1 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20  heir own counts 
1bad2 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67  within their agg
1bad3 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78  regate.** contex
1bad4 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
1bad5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67   int sqlite3_agg
1bad6 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c  regate_count(sql
1bad7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29  ite3_context *p)
1bad8 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26  {.  assert( p &&
1bad9 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d 3e 70   p->pMem && p->p
1bada 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
1badb 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 74  ->xStep );.  ret
1badc 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a  urn p->pMem->n;.
1badd 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1bade 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1badf 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1bae0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
1bae1 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  or the statement
1bae2 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
1bae3 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bae4 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73  3_column_count(s
1bae5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bae6 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d  mt){.  Vdbe *pVm
1bae7 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1bae8 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f  ;.  return pVm ?
1bae9 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e   pVm->nResColumn
1baea 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   : 0;.}../*.** R
1baeb 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1baec 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c   of values avail
1baed 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75  able from the cu
1baee 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
1baef 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78  .** currently ex
1baf0 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e  ecuting statemen
1baf1 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49  t pStmt..*/.SQLI
1baf2 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1baf3 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71  e3_data_count(sq
1baf4 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1baf5 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20  t){.  Vdbe *pVm 
1baf6 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1baf7 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c  .  if( pVm==0 ||
1baf8 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74   pVm->pResultSet
1baf9 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1bafa 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52    return pVm->nR
1bafb 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a  esColumn;.}.../*
1bafc 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
1bafd 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20   if column iCol 
1bafe 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 61  of the given sta
1baff 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 2e  tement is valid.
1bb00 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72    If.** it is, r
1bb01 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1bb02 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 74  to the Mem for t
1bb03 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74  he value of that
1bb04 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69   column..** If i
1bb05 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 64  Col is not valid
1bb06 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
1bb07 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 63  er to a Mem whic
1bb08 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a  h has a value.**
1bb09 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61   of NULL..*/.sta
1bb0a 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d  tic Mem *columnM
1bb0b 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  em(sqlite3_stmt 
1bb0c 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bb0d 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69    Vdbe *pVm;.  i
1bb0e 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a  nt vals;.  Mem *
1bb0f 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28  pOut;..  pVm = (
1bb10 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1bb11 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e  if( pVm && pVm->
1bb12 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26 26  pResultSet!=0 &&
1bb13 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75   i<pVm->nResColu
1bb14 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20  mn && i>=0 ){.  
1bb15 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bb16 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d  enter(pVm->db->m
1bb17 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73 20  utex);.    vals 
1bb18 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  = sqlite3_data_c
1bb19 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20  ount(pStmt);.   
1bb1a 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52   pOut = &pVm->pR
1bb1b 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d  esultSet[i];.  }
1bb1c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
1bb1d 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
1bb1e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
1bb1f 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f  rgument is out o
1bb20 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e 0a  f range, return.
1bb21 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72      ** a pointer
1bb22 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
1bb23 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f 62 6a  g static Mem obj
1bb24 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 61 69  ect which contai
1bb25 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 61  ns the.    ** va
1bb26 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 76  lue SQL NULL. Ev
1bb27 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 4d 65  en though the Me
1bb28 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  m structure cont
1bb29 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e 74 0a  ains an element.
1bb2a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65 20 69      ** of type i
1bb2b 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e 20 61  64, on certain a
1bb2c 72 63 68 69 74 65 63 74 75 72 65 20 28 78 38 36  rchitecture (x86
1bb2d 29 20 77 69 74 68 20 63 65 72 74 61 69 6e 20 63  ) with certain c
1bb2e 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a 20 73  ompiler.    ** s
1bb2f 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c 20 67  witches (-Os), g
1bb30 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74 68 69  cc may align thi
1bb31 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f 6e 20  s Mem object on 
1bb32 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  a 4-byte boundar
1bb33 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64  y.    ** instead
1bb34 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 6f 6e   of an 8-byte on
1bb35 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f 72 6b  e. This all work
1bb36 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74 20 74  s fine, except t
1bb37 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20  hat when.    ** 
1bb38 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c  running with SQL
1bb39 49 54 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65  ITE_DEBUG define
1bb3a 64 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64  d the SQLite cod
1bb3b 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73 73 65  e sometimes asse
1bb3c 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74 68 61  rt()s.    ** tha
1bb3d 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72  t a Mem structur
1bb3e 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20  e is located on 
1bb3f 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61  an 8-byte bounda
1bb40 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74 0a 20  ry. To prevent. 
1bb41 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73 65 72     ** this asser
1bb42 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67  t() from failing
1bb43 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  , when building 
1bb44 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
1bb45 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a  G defined.    **
1bb46 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f 72 63   using gcc, forc
1bb47 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 20  e nullMem to be 
1bb48 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 75  8-byte aligned u
1bb49 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63 61 6c  sing the magical
1bb4a 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 69 62  .    ** __attrib
1bb4b 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38  ute__((aligned(8
1bb4c 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20  ))) macro.  */. 
1bb4d 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1bb4e 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66  Mem nullMem .#if
1bb4f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1bb50 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65  DEBUG) && define
1bb51 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20 20 20  d(__GNUC__).    
1bb52 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f 5f 28    __attribute__(
1bb53 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 0a 23  (aligned(8))) .#
1bb54 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20 7b 7b  endif.      = {{
1bb55 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c 20 30  0}, (double)0, 0
1bb56 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c  , "", 0, MEM_Nul
1bb57 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  l, SQLITE_NULL, 
1bb58 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 20 20  0, 0, 0 };..    
1bb59 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 41 59  if( pVm && ALWAY
1bb5a 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a 20 20  S(pVm->db) ){.  
1bb5b 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1bb5c 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d  x_enter(pVm->db-
1bb5d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 73  >mutex);.      s
1bb5e 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d 2d  qlite3Error(pVm-
1bb5f 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47  >db, SQLITE_RANG
1bb60 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  E, 0);.    }.   
1bb61 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e   pOut = (Mem*)&n
1bb62 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65  ullMem;.  }.  re
1bb63 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a  turn pOut;.}../*
1bb64 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1bb65 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  n is called afte
1bb66 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71  r invoking an sq
1bb67 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 20  lite3_value_XXX 
1bb68 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a  function on a .*
1bb69 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28  * column value (
1bb6a 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 74  i.e. a value ret
1bb6b 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 74  urned by evaluat
1bb6c 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65  ing an SQL expre
1bb6d 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20  ssion in the.** 
1bb6e 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 61  select list of a
1bb6f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1bb70 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 73  t) that may caus
1bb71 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  e a malloc() fai
1bb72 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c  lure. If .** mal
1bb73 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64  loc() has failed
1bb74 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d 61  , the threads ma
1bb75 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
1bb76 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 74  is cleared and t
1bb77 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64  he result.** cod
1bb78 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 70  e of statement p
1bb79 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c 49  Stmt set to SQLI
1bb7a 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20  TE_NOMEM..**.** 
1bb7b 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68  Specifically, th
1bb7c 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  is is called fro
1bb7d 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20  m within:.**.** 
1bb7e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1bb7f 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20  mn_int().**     
1bb80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
1bb81 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 71  nt64().**     sq
1bb82 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1bb83 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  t().**     sqlit
1bb84 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1bb85 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
1bb86 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a  3_column_real().
1bb87 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1bb88 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
1bb89 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
1bb8a 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a  umn_bytes16().**
1bb8b 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 20  .** But not for 
1bb8c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
1bb8d 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 76  lob(), which nev
1bb8e 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28  er calls malloc(
1bb8f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
1bb90 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61  d columnMallocFa
1bb91 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 74  ilure(sqlite3_st
1bb92 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f  mt *pStmt).{.  /
1bb93 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61  * If malloc() fa
1bb94 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 65  iled during an e
1bb95 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69  ncoding conversi
1bb96 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a  on within an.  *
1bb97 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
1bb98 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 73  _XXX API, then s
1bb99 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  et the return co
1bb9a 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  de of the statem
1bb9b 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49  ent to.  ** SQLI
1bb9c 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65  TE_NOMEM. The ne
1bb9d 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70  xt call to _step
1bb9e 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c 6c  () (if any) will
1bb9f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1bba0 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66  RROR.  ** and _f
1bba1 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72  inalize() will r
1bba2 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a  eturn NOMEM..  *
1bba3 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  /.  Vdbe *p = (V
1bba4 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69  dbe *)pStmt;.  i
1bba5 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72  f( p ){.    p->r
1bba6 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1bba7 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29  it(p->db, p->rc)
1bba8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bba9 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bbaa 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a  >mutex);.  }.}..
1bbab 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1bbac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  ************* sq
1bbad 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a  lite3_column_  *
1bbae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bbaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1bbb0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1bbb1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
1bbb2 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 6d  d to access elem
1bbb3 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 72  ents of the curr
1bbb4 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68  ent row.** in th
1bbb5 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f  e result set..*/
1bbb6 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1bbb7 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1bbb8 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69  column_blob(sqli
1bbb9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bbba 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74   int i){.  const
1bbbb 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61   void *val;.  va
1bbbc 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1bbbd 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65  e_blob( columnMe
1bbbe 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20  m(pStmt,i) );.  
1bbbf 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74  /* Even though t
1bbc0 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64  here is no encod
1bbc1 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20  ing conversion, 
1bbc2 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67  value_blob() mig
1bbc3 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ht.  ** need to 
1bbc4 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f  call malloc() to
1bbc5 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 75   expand the resu
1bbc6 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62  lt of a zeroblob
1bbc7 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73  () .  ** express
1bbc8 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c  ion. .  */.  col
1bbc9 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1bbca 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1bbcb 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1bbcc 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bbcd 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c  column_bytes(sql
1bbce 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bbcf 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
1bbd0 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1bbd1 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d  lue_bytes( colum
1bbd2 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1bbd3 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1bbd4 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bbd5 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1bbd6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bbd7 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1bbd8 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  es16(sqlite3_stm
1bbd9 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bbda 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
1bbdb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1bbdc 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  s16( columnMem(p
1bbdd 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1bbde 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1bbdf 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1bbe0 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1bbe1 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
1bbe2 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65  e3_column_double
1bbe3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bbe4 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1bbe5 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c  double val = sql
1bbe6 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
1bbe7 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  e( columnMem(pSt
1bbe8 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1bbe9 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1bbea 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1bbeb 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1bbec 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
1bbed 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33  lumn_int(sqlite3
1bbee 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bbef 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
1bbf0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1bbf1 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  int( columnMem(p
1bbf2 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c  Stmt,i) );.  col
1bbf3 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65  umnMallocFailure
1bbf4 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72  (pStmt);.  retur
1bbf5 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f  n val;.}.SQLITE_
1bbf6 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
1bbf7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bbf8 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74  int64(sqlite3_st
1bbf9 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bbfa 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
1bbfb 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  4 val = sqlite3_
1bbfc 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c  value_int64( col
1bbfd 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1bbfe 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1bbff 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1bc00 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1bc01 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1bc02 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1bc03 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1bc04 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  text(sqlite3_stm
1bc05 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bc06 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
1bc07 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 73  ed char *val = s
1bc08 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1bc09 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  t( columnMem(pSt
1bc0a 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1bc0b 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1bc0c 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1bc0d 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1bc0e 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  I sqlite3_value 
1bc0f 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1bc10 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
1bc11 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bc12 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d  ){.  Mem *pOut =
1bc13 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1bc14 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 74  , i);.  if( pOut
1bc15 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74  ->flags&MEM_Stat
1bc16 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  ic ){.    pOut->
1bc17 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74  flags &= ~MEM_St
1bc18 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  atic;.    pOut->
1bc19 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68  flags |= MEM_Eph
1bc1a 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e  em;.  }.  column
1bc1b 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1bc1c 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  tmt);.  return (
1bc1d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29  sqlite3_value *)
1bc1e 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  pOut;.}.#ifndef 
1bc1f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1bc20 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1bc21 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1bc22 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73  _column_text16(s
1bc23 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bc24 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f  mt, int i){.  co
1bc25 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20  nst void *val = 
1bc26 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bc27 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  xt16( columnMem(
1bc28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
1bc29 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1bc2a 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1bc2b 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66  rn val;.}.#endif
1bc2c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1bc2d 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f  UTF16 */.SQLITE_
1bc2e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bc2f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69  column_type(sqli
1bc30 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bc31 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 69   int i){.  int i
1bc32 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Type = sqlite3_v
1bc33 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d  alue_type( colum
1bc34 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1bc35 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1bc36 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bc37 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d   return iType;.}
1bc38 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
1bc39 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  ng function is e
1bc3a 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20  xperimental and 
1bc3b 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
1bc3c 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20  e or.** removal 
1bc3d 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33  */./*int sqlite3
1bc3e 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f  _column_numeric_
1bc3f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
1bc40 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bc41 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 6c  {.**  return sql
1bc42 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
1bc43 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d  ic_type( columnM
1bc44 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a  em(pStmt,i) );.*
1bc45 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  *}.*/../*.** Con
1bc46 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 6c  vert the N-th el
1bc47 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e  ement of pStmt->
1bc48 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20  pColName[] into 
1bc49 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a  a string using.*
1bc4a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 72  * xFunc() then r
1bc4b 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 6e  eturn that strin
1bc4c 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 20  g.  If N is out 
1bc4d 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e  of range, return
1bc4e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20   0..**.** There 
1bc4f 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d 65  are up to 5 name
1bc50 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  s for each colum
1bc51 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 65  n.  useType dete
1bc52 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20  rmines which.** 
1bc53 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 64  name is returned
1bc54 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 20  .  Here are the 
1bc55 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  names:.**.**    
1bc56 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 6d  0      The colum
1bc57 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 6f  n name as it sho
1bc58 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 64  uld be displayed
1bc59 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20   for output.**  
1bc5a 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 74    1      The dat
1bc5b 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 74  atype name for t
1bc5c 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20  he column.**    
1bc5d 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20  2      The name 
1bc5e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1bc5f 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1bc60 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20  derives from.** 
1bc61 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 61     3      The na
1bc62 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1bc63 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
1bc64 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20  derives from.** 
1bc65 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 61     4      The na
1bc66 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1bc67 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
1bc68 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65  result column de
1bc69 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a  rives from.**.**
1bc6a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
1bc6b 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 63  s not a simple c
1bc6c 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1bc6d 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 70  (if it is an exp
1bc6e 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20  ression.** or a 
1bc6f 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 75  constant) then u
1bc70 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 6e  seTypes 2, 3, an
1bc71 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  d 4 return NULL.
1bc72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
1bc73 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d   void *columnNam
1bc74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  e(.  sqlite3_stm
1bc75 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20  t *pStmt,.  int 
1bc76 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  N,.  const void 
1bc77 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c  *(*xFunc)(Mem*),
1bc78 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a 29  .  int useType.)
1bc79 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
1bc7a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 20  ret = 0;.  Vdbe 
1bc7b 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1bc7c 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73  mt;.  int n;.  s
1bc7d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
1bc7e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  db;.  .  assert(
1bc7f 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20 3d 20   db!=0 );.  n = 
1bc80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
1bc81 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 69  ount(pStmt);.  i
1bc82 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29  f( N<n && N>=0 )
1bc83 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 54 79  {.    N += useTy
1bc84 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  pe*n;.    sqlite
1bc85 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1bc86 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 61 73  ->mutex);.    as
1bc87 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
1bc88 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 20  Failed==0 );.   
1bc89 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d   ret = xFunc(&p-
1bc8a 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20  >aColName[N]);. 
1bc8b 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20      /* A malloc 
1bc8c 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64 20  may have failed 
1bc8d 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78 46  inside of the xF
1bc8e 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20 74  unc() call. If t
1bc8f 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74 68  his.    ** is th
1bc90 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20 74 68  e case, clear th
1bc91 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
1bc92 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  lag and return N
1bc93 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ULL..    */.    
1bc94 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1bc95 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 64 62  iled ){.      db
1bc96 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1bc97 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20 3d 20   0;.      ret = 
1bc98 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  0;.    }.    sql
1bc99 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bc9a 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  (db->mutex);.  }
1bc9b 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d  .  return ret;.}
1bc9c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1bc9d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e  he name of the N
1bc9e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
1bc9f 20 72 65 73 75 6c 74 20 73 65 74 20 72 65 74 75   result set retu
1bca0 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73  rned by SQL.** s
1bca1 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a  tatement pStmt..
1bca2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1bca3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1bca4 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71  3_column_name(sq
1bca5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bca6 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1bca7 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1bca8 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1bca9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1bcaa 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1bcab 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d  lue_text, COLNAM
1bcac 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64  E_NAME);.}.#ifnd
1bcad 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1bcae 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1bcaf 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1bcb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
1bcb1 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
1bcb2 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1bcb3 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1bcb4 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1bcb5 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1bcb6 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1bcb7 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
1bcb8 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d  COLNAME_NAME);.}
1bcb9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
1bcba 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20 79  onstraint:  If y
1bcbb 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f 43  ou have ENABLE_C
1bcbc 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74  OLUMN_METADATA t
1bcbd 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20  hen you must.** 
1bcbe 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f  not define OMIT_
1bcbf 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66  DECLTYPE..*/.#if
1bcc0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1bcc1 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20 26  OMIT_DECLTYPE) &
1bcc2 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
1bcc3 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
1bcc4 45 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f 72  ETADATA).# error
1bcc5 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69 6e   "Must not defin
1bcc6 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d  e both SQLITE_OM
1bcc7 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20 20  IT_DECLTYPE \.  
1bcc8 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49 54         and SQLIT
1bcc9 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
1bcca 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69 66  METADATA".#endif
1bccb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1bccc 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f  _OMIT_DECLTYPE./
1bccd 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1bcce 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69  column declarati
1bccf 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70 70 6c  on type (if appl
1bcd0 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20 27  icable) of the '
1bcd1 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  i'th column.** o
1bcd2 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
1bcd3 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
1bcd4 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49  t pStmt..*/.SQLI
1bcd5 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
1bcd6 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
1bcd7 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74  n_decltype(sqlit
1bcd8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bcd9 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1bcda 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1bcdb 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1bcdc 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1bcdd 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1bcde 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44  _text, COLNAME_D
1bcdf 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e  ECLTYPE);.}.#ifn
1bce0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bce1 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49  UTF16.SQLITE_API
1bce2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
1bce3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
1bce4 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73  type16(sqlite3_s
1bce5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bce6 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1bce7 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1bce8 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1bce9 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1bcea 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1bceb 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43  t16, COLNAME_DEC
1bcec 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  LTYPE);.}.#endif
1bced 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1bcee 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20  UTF16 */.#endif 
1bcef 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  /* SQLITE_OMIT_D
1bcf0 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64  ECLTYPE */..#ifd
1bcf1 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
1bcf2 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
1bcf3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bcf4 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
1bcf5 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69 63  tabase from whic
1bcf6 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  h a result colum
1bcf7 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55  n derives..** NU
1bcf8 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  LL is returned i
1bcf9 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  f the result col
1bcfa 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73  umn is an expres
1bcfb 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74  sion or constant
1bcfc 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   or.** anything 
1bcfd 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f  else which is no
1bcfe 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20  t an unabiguous 
1bcff 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64  reference to a d
1bd00 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a  atabase column..
1bd01 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1bd02 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1bd03 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
1bd04 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
1bd05 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bd06 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1bd07 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1bd08 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1bd09 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1bd0a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1bd0b 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  t, COLNAME_DATAB
1bd0c 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  ASE);.}.#ifndef 
1bd0d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1bd0e 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1bd0f 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1bd10 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
1bd11 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
1bd12 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bd13 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bd14 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bd15 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bd16 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bd17 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bd18 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41  xt16, COLNAME_DA
1bd19 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69  TABASE);.}.#endi
1bd1a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1bd1b 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
1bd1c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
1bd1d 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72   of the table fr
1bd1e 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c  om which a resul
1bd1f 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  t column derives
1bd20 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  ..** NULL is ret
1bd21 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73  urned if the res
1bd22 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e  ult column is an
1bd23 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63   expression or c
1bd24 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e  onstant or.** an
1bd25 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63  ything else whic
1bd26 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62  h is not an unab
1bd27 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65  iguous reference
1bd28 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63   to a database c
1bd29 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
1bd2a 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1bd2b 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1bd2c 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74  table_name(sqlit
1bd2d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bd2e 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1bd2f 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1bd30 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1bd31 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1bd32 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1bd33 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54  _text, COLNAME_T
1bd34 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  ABLE);.}.#ifndef
1bd35 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bd36 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1bd37 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1bd38 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
1bd39 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ame16(sqlite3_st
1bd3a 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
1bd3b 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
1bd3c 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
1bd3d 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
1bd3e 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
1bd3f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1bd40 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c  16, COLNAME_TABL
1bd41 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  E);.}.#endif /* 
1bd42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1bd43 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  6 */../*.** Retu
1bd44 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  rn the name of t
1bd45 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  he table column 
1bd46 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73  from which a res
1bd47 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  ult column deriv
1bd48 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72  es..** NULL is r
1bd49 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72  eturned if the r
1bd4a 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20  esult column is 
1bd4b 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  an expression or
1bd4c 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20   constant or.** 
1bd4d 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68  anything else wh
1bd4e 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e  ich is not an un
1bd4f 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e  abiguous referen
1bd50 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  ce to a database
1bd51 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
1bd52 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
1bd53 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
1bd54 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71  n_origin_name(sq
1bd55 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bd56 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1bd57 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1bd58 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1bd59 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1bd5a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1bd5b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d  lue_text, COLNAM
1bd5c 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66  E_COLUMN);.}.#if
1bd5d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1bd5e 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1bd5f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1bd60 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
1bd61 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  gin_name16(sqlit
1bd62 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bd63 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1bd64 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1bd65 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1bd66 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1bd67 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1bd68 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45  _text16, COLNAME
1bd69 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64  _COLUMN);.}.#end
1bd6a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bd6b 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69  T_UTF16 */.#endi
1bd6c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
1bd6d 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
1bd6e 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  TA */.../*******
1bd6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd70 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
1bd71 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  _bind_  ********
1bd72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd73 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69  ***.** .** Routi
1bd74 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 61  nes used to atta
1bd75 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 6c  ch values to wil
1bd76 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d 70  dcards in a comp
1bd77 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
1bd78 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62  nt..*/./*.** Unb
1bd79 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62 6f  ind the value bo
1bd7a 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65 20  und to variable 
1bd7b 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61 63  i in virtual mac
1bd7c 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73 20  hine p. This is 
1bd7d 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65  the .** the same
1bd7e 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e 55   as binding a NU
1bd7f 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20  LL value to the 
1bd80 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20 22  column. If the "
1bd81 69 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a  i" parameter is.
1bd82 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  ** out of range,
1bd83 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e   then SQLITE_RAN
1bd84 47 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  GE is returned. 
1bd85 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54 45 5f  Othewise SQLITE_
1bd86 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63  OK..**.** A succ
1bd87 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f  essful evaluatio
1bd88 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  n of this routin
1bd89 65 20 61 63 71 75 69 72 65 73 20 74 68 65 20 6d  e acquires the m
1bd8a 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68  utex on p..** th
1bd8b 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65 61  e mutex is relea
1bd8c 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64 20  sed if any kind 
1bd8d 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  of error occurs.
1bd8e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72  .**.** The error
1bd8f 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20   code stored in 
1bd90 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20 69  database p->db i
1bd91 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
1bd92 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a  th the return.**
1bd93 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63 61   value in any ca
1bd94 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
1bd95 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64 62  t vdbeUnbind(Vdb
1bd96 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20  e *p, int i){.  
1bd97 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66 28  Mem *pVar;.  if(
1bd98 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53   p==0 ) return S
1bd99 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1bd9a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1bd9b 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ter(p->db->mutex
1bd9c 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  );.  if( p->magi
1bd9d 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c!=VDBE_MAGIC_RU
1bd9e 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b  N || p->pc>=0 ){
1bd9f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1bda0 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  r(p->db, SQLITE_
1bda1 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20  MISUSE, 0);.    
1bda2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bda3 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1bda4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
1bda5 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
1bda6 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e  .  if( i<1 || i>
1bda7 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73  p->nVar ){.    s
1bda8 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64  qlite3Error(p->d
1bda9 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c  b, SQLITE_RANGE,
1bdaa 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1bdab 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1bdac 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
1bdad 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 41  return SQLITE_RA
1bdae 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a  NGE;.  }.  i--;.
1bdaf 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61    pVar = &p->aVa
1bdb0 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56  r[i];.  sqlite3V
1bdb1 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 56  dbeMemRelease(pV
1bdb2 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61  ar);.  pVar->fla
1bdb3 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1bdb4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
1bdb5 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  >db, SQLITE_OK, 
1bdb6 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  0);..  /* If the
1bdb7 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69   bit correspondi
1bdb8 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72 69 61  ng to this varia
1bdb9 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78 70 6d  ble in Vdbe.expm
1bdba 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68 65 6e  ask is set, then
1bdbb 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67 20 61   .  ** binding a
1bdbc 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 74 68   new value to th
1bdbd 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 76 61  is variable inva
1bdbe 6c 69 64 61 74 65 73 20 74 68 65 20 63 75 72 72  lidates the curr
1bdbf 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a  ent query plan..
1bdc0 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73    */.  if( p->is
1bdc1 50 72 65 70 61 72 65 56 32 20 26 26 0a 20 20 20  PrepareV2 &&.   
1bdc2 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d 3e 65    ((i<32 && p->e
1bdc3 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32 29 31  xpmask & ((u32)1
1bdc4 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e 65 78   << i)) || p->ex
1bdc5 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66  pmask==0xfffffff
1bdc6 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d 3e 65  f).  ){.    p->e
1bdc7 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1bdc8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bdc9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e  OK;.}../*.** Bin
1bdca 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f 42  d a text or BLOB
1bdcb 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
1bdcc 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 0a  c int bindText(.
1bdcd 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1bdce 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20  pStmt,   /* The 
1bdcf 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e  statement to bin
1bdd0 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69  d against */.  i
1bdd1 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
1bdd2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1bdd3 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
1bdd4 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e  to bind */.  con
1bdd5 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20  st void *zData, 
1bdd6 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1bdd7 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 65  o the data to be
1bdd8 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20   bound */.  int 
1bdd9 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20  nData,          
1bdda 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1bddb 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 6f  bytes of data to
1bddc 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76   be bound */.  v
1bddd 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1bdde 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 63  *),   /* Destruc
1bddf 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  tor for the data
1bde0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e   */.  u8 encodin
1bde1 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g            /* 
1bde2 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  Encoding for the
1bde3 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64   data */.){.  Vd
1bde4 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
1bde5 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56  pStmt;.  Mem *pV
1bde6 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  ar;.  int rc;.. 
1bde7 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
1bde8 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
1bde9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1bdea 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 20     if( zData!=0 
1bdeb 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d 20  ){.      pVar = 
1bdec 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20  &p->aVar[i-1];. 
1bded 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1bdee 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1bdef 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  Var, zData, nDat
1bdf0 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65  a, encoding, xDe
1bdf1 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  l);.      if( rc
1bdf2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65  ==SQLITE_OK && e
1bdf3 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20  ncoding!=0 ){.  
1bdf4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bdf5 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1bdf6 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 70  ding(pVar, ENC(p
1bdf7 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a  ->db));.      }.
1bdf8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1bdf9 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29  or(p->db, rc, 0)
1bdfa 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1bdfb 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64  ite3ApiExit(p->d
1bdfc 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20  b, rc);.    }.  
1bdfd 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bdfe 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1bdff 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1be00 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
1be01 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75  Bind a blob valu
1be02 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74  e to an SQL stat
1be03 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a  ement variable..
1be04 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1be05 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  t sqlite3_bind_b
1be06 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  lob(.  sqlite3_s
1be07 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1be08 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76  nt i, .  const v
1be09 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  oid *zData, .  i
1be0a 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
1be0b 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1be0c 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
1be0d 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
1be0e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
1be0f 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45  el, 0);.}.SQLITE
1be10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1be11 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c  _bind_double(sql
1be12 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1be13 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20  , int i, double 
1be14 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72  rValue){.  int r
1be15 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
1be16 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1be17 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
1be18 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
1be19 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1be1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1be1b 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56  SetDouble(&p->aV
1be1c 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29  ar[i-1], rValue)
1be1d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1be1e 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1be1f 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1be20 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
1be21 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1be22 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69  e3_bind_int(sqli
1be23 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74  te3_stmt *p, int
1be24 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b   i, int iValue){
1be25 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1be26 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20  3_bind_int64(p, 
1be27 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 3b  i, (i64)iValue);
1be28 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1be29 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
1be2a 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d  nt64(sqlite3_stm
1be2b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c  t *pStmt, int i,
1be2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 56   sqlite_int64 iV
1be2d 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
1be2e 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1be2f 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  be *)pStmt;.  rc
1be30 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
1be31 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
1be32 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1be33 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1be34 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b  tInt64(&p->aVar[
1be35 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20  i-1], iValue);. 
1be36 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1be37 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1be38 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1be39 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
1be3a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1be3b 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65  bind_null(sqlite
1be3c 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1be3d 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b  nt i){.  int rc;
1be3e 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1be3f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20  be*)pStmt;.  rc 
1be40 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
1be41 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
1be42 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
1be43 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1be44 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1be45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1be46 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
1be47 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1be48 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 65  _text( .  sqlite
1be49 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a  3_stmt *pStmt, .
1be4a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73    int i, .  cons
1be4b 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a  t char *zData, .
1be4c 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20    int nData, .  
1be4d 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1be4e 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  d*).){.  return 
1be4f 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20  bindText(pStmt, 
1be50 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c  i, zData, nData,
1be51 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54   xDel, SQLITE_UT
1be52 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  F8);.}.#ifndef S
1be53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1be54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1be55 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
1be56 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  t16(.  sqlite3_s
1be57 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1be58 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76  nt i, .  const v
1be59 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  oid *zData, .  i
1be5a 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
1be5b 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1be5c 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
1be5d 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
1be5e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
1be5f 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  el, SQLITE_UTF16
1be60 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69  NATIVE);.}.#endi
1be61 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1be62 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45  _UTF16 */.SQLITE
1be63 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1be64 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69  _bind_value(sqli
1be65 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1be66 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 71   int i, const sq
1be67 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1be68 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  lue){.  int rc;.
1be69 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75 65    switch( pValue
1be6a 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  ->type ){.    ca
1be6b 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
1be6c 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  R: {.      rc = 
1be6d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
1be6e 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61  64(pStmt, i, pVa
1be6f 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20  lue->u.i);.     
1be70 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1be71 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
1be72 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  OAT: {.      rc 
1be73 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  = sqlite3_bind_d
1be74 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20  ouble(pStmt, i, 
1be75 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20  pValue->r);.    
1be76 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1be77 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
1be78 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  LOB: {.      if(
1be79 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26   pValue->flags &
1be7a 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1be7b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1be7c 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
1be7d 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
1be7e 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20  ->u.nZero);.    
1be7f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1be80 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
1be81 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69  nd_blob(pStmt, i
1be82 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61  , pValue->z, pVa
1be83 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52  lue->n,SQLITE_TR
1be84 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
1be85 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1be86 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
1be87 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20  LITE_TEXT: {.   
1be88 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 74     rc = bindText
1be89 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75  (pStmt,i,  pValu
1be8a 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c  e->z, pValue->n,
1be8b 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
1be8c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  T,.             
1be8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1be8e 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20   pValue->enc);. 
1be8f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1be90 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
1be91 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1be92 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53  te3_bind_null(pS
1be93 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 62  tmt, i);.      b
1be94 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1be95 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
1be96 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1be97 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
1be98 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lob(sqlite3_stmt
1be99 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20   *pStmt, int i, 
1be9a 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63  int n){.  int rc
1be9b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
1be9c 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72  dbe *)pStmt;.  r
1be9d 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
1be9e 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
1be9f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bea0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1bea1 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61  etZeroBlob(&p->a
1bea2 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20  Var[i-1], n);.  
1bea3 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bea4 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1bea5 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1bea6 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
1bea7 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1bea8 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68   of wildcards th
1bea9 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74  at can be potent
1beaa 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a  ially bound to..
1beab 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1beac 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70  is added to supp
1bead 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e  ort DBD::SQLite.
1beae 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49    .*/.SQLITE_API
1beaf 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1beb0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
1beb1 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1beb2 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1beb3 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1beb4 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70  ;.  return p ? p
1beb5 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f  ->nVar : 0;.}../
1beb6 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61  *.** Create a ma
1beb7 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61  pping from varia
1beb8 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76  ble numbers to v
1beb9 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a  ariable names.**
1beba 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56   in the Vdbe.azV
1bebb 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73  ar[] array, if s
1bebc 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f  uch a mapping do
1bebd 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a  es not already.*
1bebe 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  * exist..*/.stat
1bebf 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61  ic void createVa
1bec0 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20  rMap(Vdbe *p){. 
1bec1 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29   if( !p->okVar )
1bec2 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20  {.    int j;.   
1bec3 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71   Op *pOp;.    sq
1bec4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1bec5 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(p->db->mutex);
1bec6 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63 65  .    /* The race
1bec7 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 20   condition here 
1bec8 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49 66  is harmless.  If
1bec9 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61 6c   two threads cal
1beca 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f  l this.    ** ro
1becb 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d  utine on the sam
1becc 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73 61  e Vdbe at the sa
1becd 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62 6f  me time, they bo
1bece 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20 20  th might end.   
1becf 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69 7a   ** up initializ
1bed0 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a 56  ing the Vdbe.azV
1bed1 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 61  ar[] array.  Tha
1bed2 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65 78  t is a little ex
1bed3 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20  tra.    ** work 
1bed4 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20 69  but it results i
1bed5 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65  n the same answe
1bed6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  r..    */.    fo
1bed7 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f  r(j=0, pOp=p->aO
1bed8 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b  p; j<p->nOp; j++
1bed9 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20  , pOp++){.      
1beda 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1bedb 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a  =OP_Variable ){.
1bedc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1bedd 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70  pOp->p1>0 && pOp
1bede 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b  ->p1<=p->nVar );
1bedf 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61  .        p->azVa
1bee0 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70  r[pOp->p1-1] = p
1bee1 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20  Op->p4.z;.      
1bee2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f  }.    }.    p->o
1bee3 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73 71  kVar = 1;.    sq
1bee4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bee5 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1bee6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1bee7 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bee8 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61   a wildcard para
1bee9 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 4e  meter.  Return N
1beea 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 78  ULL if the index
1beeb 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 61  .** is out of ra
1beec 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 69  nge or if the wi
1beed 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d 65  ldcard is unname
1beee 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  d..**.** The res
1beef 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 54  ult is always UT
1bef0 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  F-8..*/.SQLITE_A
1bef1 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1bef2 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
1bef3 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74  meter_name(sqlit
1bef4 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bef5 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a  int i){.  Vdbe *
1bef6 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1bef7 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
1bef8 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72  i<1 || i>p->nVar
1bef9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1befa 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61  ;.  }.  createVa
1befb 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 72  rMap(p);.  retur
1befc 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b  n p->azVar[i-1];
1befd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
1befe 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d  a wildcard param
1beff 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 72  eter name, retur
1bf00 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  n the index of t
1bf01 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77  he variable.** w
1bf02 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 20  ith that name.  
1bf03 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 76  If there is no v
1bf04 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 65  ariable with the
1bf05 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20   given name,.** 
1bf06 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c  return 0..*/.SQL
1bf07 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1bf08 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d  sqlite3VdbeParam
1bf09 65 74 65 72 49 6e 64 65 78 28 56 64 62 65 20 2a  eterIndex(Vdbe *
1bf0a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
1bf0b 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29  Name, int nName)
1bf0c 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
1bf0d 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   p==0 ){.    ret
1bf0e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65  urn 0;.  }.  cre
1bf0f 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20 0a 20  ateVarMap(p); . 
1bf10 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20   if( zName ){.  
1bf11 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
1bf12 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nVar; i++){.    
1bf13 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1bf14 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20  = p->azVar[i];. 
1bf15 20 20 20 20 20 69 66 28 20 7a 20 26 26 20 6d 65       if( z && me
1bf16 6d 63 6d 70 28 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61  mcmp(z,zName,nNa
1bf17 6d 65 29 3d 3d 30 20 26 26 20 7a 5b 6e 4e 61 6d  me)==0 && z[nNam
1bf18 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e]==0 ){.       
1bf19 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
1bf1a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1bf1b 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c   return 0;.}.SQL
1bf1c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bf1d 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
1bf1e 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33  er_index(sqlite3
1bf1f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f  _stmt *pStmt, co
1bf20 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
1bf21 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1bf22 65 33 56 64 62 65 50 61 72 61 6d 65 74 65 72 49  e3VdbeParameterI
1bf23 6e 64 65 78 28 28 56 64 62 65 2a 29 70 53 74 6d  ndex((Vdbe*)pStm
1bf24 74 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  t, zName, sqlite
1bf25 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
1bf26 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  );.}../*.** Tran
1bf27 73 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67  sfer all binding
1bf28 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74  s from the first
1bf29 20 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20   statement over 
1bf2a 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a  to the second..*
1bf2b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1bf2c 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e   int sqlite3Tran
1bf2d 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c  sferBindings(sql
1bf2e 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d  ite3_stmt *pFrom
1bf2f 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74  Stmt, sqlite3_st
1bf30 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20  mt *pToStmt){.  
1bf31 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56  Vdbe *pFrom = (V
1bf32 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a  dbe*)pFromStmt;.
1bf33 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56    Vdbe *pTo = (V
1bf34 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20  dbe*)pToStmt;.  
1bf35 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
1bf36 20 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d   pTo->db==pFrom-
1bf37 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >db );.  assert(
1bf38 20 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f   pTo->nVar==pFro
1bf39 6d 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c  m->nVar );.  sql
1bf3a 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1bf3b 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
1bf3c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
1bf3d 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29  From->nVar; i++)
1bf3e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1bf3f 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61  eMemMove(&pTo->a
1bf40 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e  Var[i], &pFrom->
1bf41 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  aVar[i]);.  }.  
1bf42 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bf43 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  ave(pTo->db->mut
1bf44 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
1bf45 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
1bf46 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bf47 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
1bf48 20 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65   Deprecated exte
1bf49 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20  rnal interface. 
1bf4a 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53   Internal/core S
1bf4b 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68  QLite code.** sh
1bf4c 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65  ould call sqlite
1bf4d 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
1bf4e 73 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d  s..**.** Is is m
1bf4f 69 73 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68  isuse to call th
1bf50 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
1bf51 73 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20  statements from 
1bf52 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74  different.** dat
1bf53 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1bf54 73 2e 20 20 42 75 74 20 61 73 20 74 68 69 73 20  s.  But as this 
1bf55 69 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20  is a deprecated 
1bf56 69 6e 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a  interface, we.**
1bf57 20 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72   will not bother
1bf58 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68   to check for th
1bf59 61 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a  at condition..**
1bf5a 0a 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73  .** If the two s
1bf5b 74 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69  tatements contai
1bf5c 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75  n a different nu
1bf5d 6d 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73  mber of bindings
1bf5e 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c  , then.** an SQL
1bf5f 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74  ITE_ERROR is ret
1bf60 75 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20  urned.  Nothing 
1bf61 65 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e  else can go wron
1bf62 67 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a  g, so otherwise.
1bf63 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
1bf64 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
1bf65 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bf66 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e  te3_transfer_bin
1bf67 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
1bf68 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73  mt *pFromStmt, s
1bf69 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f  qlite3_stmt *pTo
1bf6a 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1bf6b 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46  From = (Vdbe*)pF
1bf6c 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20  romStmt;.  Vdbe 
1bf6d 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54  *pTo = (Vdbe*)pT
1bf6e 6f 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72  oStmt;.  if( pFr
1bf6f 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e  om->nVar!=pTo->n
1bf70 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Var ){.    retur
1bf71 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1bf72 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69    }.  if( pTo->i
1bf73 73 50 72 65 70 61 72 65 56 32 20 26 26 20 70 54  sPrepareV2 && pT
1bf74 6f 2d 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20  o->expmask ){.  
1bf75 20 20 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d    pTo->expired =
1bf76 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46   1;.  }.  if( pF
1bf77 72 6f 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32  rom->isPrepareV2
1bf78 20 26 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61   && pFrom->expma
1bf79 73 6b 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d  sk ){.    pFrom-
1bf7a 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20  >expired = 1;.  
1bf7b 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
1bf7c 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e  e3TransferBindin
1bf7d 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54  gs(pFromStmt, pT
1bf7e 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  oStmt);.}.#endif
1bf7f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1bf80 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  he sqlite3* data
1bf81 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77  base handle to w
1bf82 68 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65  hich the prepare
1bf83 64 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65  d statement give
1bf84 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75  n.** in the argu
1bf85 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54  ment belongs.  T
1bf86 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20  his is the same 
1bf87 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1bf88 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20  that was.** the 
1bf89 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
1bf8a 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72  o the sqlite3_pr
1bf8b 65 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73  epare() that was
1bf8c 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a   used to create.
1bf8d 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ** the statement
1bf8e 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c   in the first pl
1bf8f 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
1bf90 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
1bf91 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71  te3_db_handle(sq
1bf92 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bf93 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74  t){.  return pSt
1bf94 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74  mt ? ((Vdbe*)pSt
1bf95 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a  mt)->db : 0;.}..
1bf96 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
1bf97 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
1bf98 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74  xt prepared stat
1bf99 65 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d  ement after pStm
1bf9a 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  t associated.** 
1bf9b 77 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f  with database co
1bf9c 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49  nnection pDb.  I
1bf9d 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c  f pStmt is NULL,
1bf9e 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73   return the firs
1bf9f 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74  t.** prepared st
1bfa0 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20  atement for the 
1bfa1 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1bfa2 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  ion.  Return NUL
1bfa3 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72  L if there.** ar
1bfa4 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51  e no more..*/.SQ
1bfa5 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
1bfa6 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e  _stmt *sqlite3_n
1bfa7 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33  ext_stmt(sqlite3
1bfa8 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73   *pDb, sqlite3_s
1bfa9 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73  tmt *pStmt){.  s
1bfaa 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65  qlite3_stmt *pNe
1bfab 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  xt;.  sqlite3_mu
1bfac 74 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d  tex_enter(pDb->m
1bfad 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74  utex);.  if( pSt
1bfae 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65  mt==0 ){.    pNe
1bfaf 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  xt = (sqlite3_st
1bfb0 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a  mt*)pDb->pVdbe;.
1bfb1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
1bfb2 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  xt = (sqlite3_st
1bfb3 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d  mt*)((Vdbe*)pStm
1bfb4 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  t)->pNext;.  }. 
1bfb5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1bfb6 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29  eave(pDb->mutex)
1bfb7 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74  ;.  return pNext
1bfb8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1bfb9 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  n the value of a
1bfba 20 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20   status counter 
1bfbb 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73  for a prepared s
1bfbc 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49  tatement.*/.SQLI
1bfbd 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bfbe 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73  e3_stmt_status(s
1bfbf 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bfc0 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  mt, int op, int 
1bfc1 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64  resetFlag){.  Vd
1bfc2 62 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62  be *pVdbe = (Vdb
1bfc3 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  e*)pStmt;.  int 
1bfc4 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e  v = pVdbe->aCoun
1bfc5 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28  ter[op-1];.  if(
1bfc6 20 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64   resetFlag ) pVd
1bfc7 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d  be->aCounter[op-
1bfc8 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  1] = 0;.  return
1bfc9 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   v;.}../********
1bfca 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
1bfcb 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  beapi.c ********
1bfcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfce 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1bfcf 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1bfd0 65 20 76 64 62 65 74 72 61 63 65 2e 63 20 2a 2a  e vdbetrace.c **
1bfd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfd3 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1bfd4 39 20 4e 6f 76 65 6d 62 65 72 20 32 35 0a 2a 2a  9 November 25.**
1bfd5 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1bfd6 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1bfd7 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1bfd8 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1bfd9 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1bfda 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1bfdb 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1bfdc 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1bfdd 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1bfde 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1bfdf 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1bfe0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1bfe1 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1bfe2 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1bfe3 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1bfe4 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1bfe5 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1bfe6 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1bfe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bfea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1bfeb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1bfec 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
1bfed 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76 61  to insert the va
1bfee 6c 75 65 73 20 6f 66 20 68 6f 73 74 20 70 61 72  lues of host par
1bfef 61 6d 65 74 65 72 73 0a 2a 2a 20 28 61 6b 61 20  ameters.** (aka 
1bff0 22 77 69 6c 64 63 61 72 64 73 22 29 20 69 6e 74  "wildcards") int
1bff1 6f 20 74 68 65 20 53 51 4c 20 74 65 78 74 20 6f  o the SQL text o
1bff2 75 74 70 75 74 20 62 79 20 73 71 6c 69 74 65 33  utput by sqlite3
1bff3 5f 74 72 61 63 65 28 29 2e 0a 2a 2f 0a 0a 23 69  _trace()..*/..#i
1bff4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bff5 54 5f 54 52 41 43 45 0a 0a 2f 2a 0a 2a 2a 20 7a  T_TRACE../*.** z
1bff6 53 71 6c 20 69 73 20 61 20 7a 65 72 6f 2d 74 65  Sql is a zero-te
1bff7 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20  rminated string 
1bff8 6f 66 20 55 54 46 2d 38 20 53 51 4c 20 74 65 78  of UTF-8 SQL tex
1bff9 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e  t.  Return the n
1bffa 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65  umber of.** byte
1bffb 73 20 69 6e 20 74 68 69 73 20 74 65 78 74 20 75  s in this text u
1bffc 70 20 74 6f 20 62 75 74 20 65 78 63 6c 75 64 69  p to but excludi
1bffd 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 68 61  ng the first cha
1bffe 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20 61 20 68  racter in.** a h
1bfff 6f 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ost parameter.  
1c000 49 66 20 74 68 65 20 74 65 78 74 20 63 6f 6e 74  If the text cont
1c001 61 69 6e 73 20 6e 6f 20 68 6f 73 74 20 70 61 72  ains no host par
1c002 61 6d 65 74 65 72 73 2c 20 72 65 74 75 72 6e 0a  ameters, return.
1c003 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  ** the total num
1c004 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1c005 74 68 65 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61  the text..*/.sta
1c006 74 69 63 20 69 6e 74 20 66 69 6e 64 4e 65 78 74  tic int findNext
1c007 48 6f 73 74 50 61 72 61 6d 65 74 65 72 28 63 6f  HostParameter(co
1c008 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
1c009 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20  int *pnToken){. 
1c00a 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a   int tokenType;.
1c00b 20 20 69 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30    int nTotal = 0
1c00c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 2a 70  ;.  int n;..  *p
1c00d 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 77 68  nToken = 0;.  wh
1c00e 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a  ile( zSql[0] ){.
1c00f 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47      n = sqlite3G
1c010 65 74 54 6f 6b 65 6e 28 28 75 38 2a 29 7a 53 71  etToken((u8*)zSq
1c011 6c 2c 20 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a  l, &tokenType);.
1c012 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
1c013 26 26 20 74 6f 6b 65 6e 54 79 70 65 21 3d 54 4b  && tokenType!=TK
1c014 5f 49 4c 4c 45 47 41 4c 20 29 3b 0a 20 20 20 20  _ILLEGAL );.    
1c015 69 66 28 20 74 6f 6b 65 6e 54 79 70 65 3d 3d 54  if( tokenType==T
1c016 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20 20  K_VARIABLE ){.  
1c017 20 20 20 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e      *pnToken = n
1c018 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1c019 20 20 20 7d 0a 20 20 20 20 6e 54 6f 74 61 6c 20     }.    nTotal 
1c01a 2b 3d 20 6e 3b 0a 20 20 20 20 7a 53 71 6c 20 2b  += n;.    zSql +
1c01b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = n;.  }.  retur
1c01c 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 2f 2a 0a  n nTotal;.}../*.
1c01d 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
1c01e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20  ter to a string 
1c01f 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
1c020 65 64 20 66 6f 72 6d 20 73 71 6c 69 74 65 33 44  ed form sqlite3D
1c021 62 4d 61 6c 6c 6f 63 28 29 20 77 68 69 63 68 0a  bMalloc() which.
1c022 2a 2a 20 68 6f 6c 64 73 20 61 20 63 6f 70 79 20  ** holds a copy 
1c023 6f 66 20 7a 52 61 77 53 71 6c 20 62 75 74 20 77  of zRawSql but w
1c024 69 74 68 20 68 6f 73 74 20 70 61 72 61 6d 65 74  ith host paramet
1c025 65 72 73 20 65 78 70 61 6e 64 65 64 20 74 6f 20  ers expanded to 
1c026 74 68 65 69 72 0a 2a 2a 20 63 75 72 72 65 6e 74  their.** current
1c027 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a   bindings..**.**
1c028 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
1c029 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ction is respons
1c02a 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20  ible for making 
1c02b 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  sure the memory 
1c02c 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 65  returned.** is e
1c02d 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e  ventually freed.
1c02e 0a 2a 2a 0a 2a 2a 20 41 4c 47 4f 52 49 54 48 4d  .**.** ALGORITHM
1c02f 3a 20 20 53 63 61 6e 20 74 68 65 20 69 6e 70 75  :  Scan the inpu
1c030 74 20 73 74 72 69 6e 67 20 6c 6f 6f 6b 69 6e 67  t string looking
1c031 20 66 6f 72 20 68 6f 73 74 20 70 61 72 61 6d 65   for host parame
1c032 74 65 72 73 20 69 6e 20 61 6e 79 20 6f 66 0a 2a  ters in any of.*
1c033 2a 20 74 68 65 73 65 20 66 6f 72 6d 73 3a 20 20  * these forms:  
1c034 3f 2c 20 3f 4e 2c 20 24 41 2c 20 40 41 2c 20 3a  ?, ?N, $A, @A, :
1c035 41 2e 20 20 54 61 6b 65 20 63 61 72 65 20 74 6f  A.  Take care to
1c036 20 61 76 6f 69 64 20 74 65 78 74 20 77 69 74 68   avoid text with
1c037 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74  in.** string lit
1c038 65 72 61 6c 73 2c 20 71 75 6f 74 65 64 20 69 64  erals, quoted id
1c039 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 2c 20  entifier names, 
1c03a 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2e 20 20 46  and comments.  F
1c03b 6f 72 20 74 65 78 74 20 66 6f 72 6d 73 2c 0a 2a  or text forms,.*
1c03c 2a 20 74 68 65 20 68 6f 73 74 20 70 61 72 61 6d  * the host param
1c03d 65 74 65 72 20 69 6e 64 65 78 20 69 73 20 66 6f  eter index is fo
1c03e 75 6e 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 20  und by scanning 
1c03f 74 68 65 20 70 65 72 70 61 72 65 64 0a 2a 2a 20  the perpared.** 
1c040 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68  statement for th
1c041 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1c042 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f  OP_Variable opco
1c043 64 65 2e 20 20 4f 6e 63 65 20 74 68 65 20 68 6f  de.  Once the ho
1c044 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  st.** parameter 
1c045 69 6e 64 65 78 20 69 73 20 6b 6e 6f 77 6e 2c 20  index is known, 
1c046 6c 6f 63 61 74 65 20 74 68 65 20 76 61 6c 75 65  locate the value
1c047 20 69 6e 20 70 2d 3e 61 56 61 72 5b 5d 2e 20 20   in p->aVar[].  
1c048 54 68 65 6e 20 72 65 6e 64 65 72 0a 2a 2a 20 74  Then render.** t
1c049 68 65 20 76 61 6c 75 65 20 61 73 20 61 20 6c 69  he value as a li
1c04a 74 65 72 61 6c 20 69 6e 20 70 6c 61 63 65 20 6f  teral in place o
1c04b 66 20 74 68 65 20 68 6f 73 74 20 70 61 72 61 6d  f the host param
1c04c 65 74 65 72 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51  eter name..*/.SQ
1c04d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
1c04e 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65 45 78  r *sqlite3VdbeEx
1c04f 70 61 6e 64 53 71 6c 28 0a 20 20 56 64 62 65 20  pandSql(.  Vdbe 
1c050 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1c051 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61      /* The prepa
1c052 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 62 65  red statement be
1c053 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 2a 2f  ing evaluated */
1c054 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1c055 52 61 77 53 71 6c 20 20 20 20 20 20 2f 2a 20 52  RawSql      /* R
1c056 61 77 20 74 65 78 74 20 6f 66 20 74 68 65 20 53  aw text of the S
1c057 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  QL statement */.
1c058 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1c059 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c05a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1c05b 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
1c05c 74 20 69 64 78 20 3d 20 30 3b 20 20 20 20 20 20  t idx = 0;      
1c05d 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1c05e 6f 66 20 61 20 68 6f 73 74 20 70 61 72 61 6d 65  of a host parame
1c05f 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78  ter */.  int nex
1c060 74 49 6e 64 65 78 20 3d 20 31 3b 20 20 20 20 20  tIndex = 1;     
1c061 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65    /* Index of ne
1c062 78 74 20 3f 20 68 6f 73 74 20 70 61 72 61 6d 65  xt ? host parame
1c063 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20  ter */.  int n; 
1c064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c065 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61    /* Length of a
1c066 20 74 6f 6b 65 6e 20 70 72 65 66 69 78 20 2a 2f   token prefix */
1c067 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20  .  int nToken;  
1c068 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c069 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 72  ength of the par
1c06a 61 6d 65 74 65 72 20 74 6f 6b 65 6e 20 2a 2f 0a  ameter token */.
1c06b 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
1c06c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1c06d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1c06e 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20  Mem *pVar;      
1c06f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1c070 65 20 6f 66 20 61 20 68 6f 73 74 20 70 61 72 61  e of a host para
1c071 6d 65 74 65 72 20 2a 2f 0a 20 20 53 74 72 41 63  meter */.  StrAc
1c072 63 75 6d 20 6f 75 74 3b 20 20 20 20 20 20 20 20  cum out;        
1c073 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
1c074 65 20 74 68 65 20 6f 75 74 70 75 74 20 68 65 72  e the output her
1c075 65 20 2a 2f 0a 20 20 63 68 61 72 20 7a 42 61 73  e */.  char zBas
1c076 65 5b 31 30 30 5d 3b 20 20 20 20 20 20 20 20 20  e[100];         
1c077 2f 2a 20 49 6e 69 74 69 61 6c 20 77 6f 72 6b 69  /* Initial worki
1c078 6e 67 20 73 70 61 63 65 20 2a 2f 0a 0a 20 20 64  ng space */..  d
1c079 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c  b = p->db;.  sql
1c07a 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
1c07b 28 26 6f 75 74 2c 20 7a 42 61 73 65 2c 20 73 69  (&out, zBase, si
1c07c 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 0a 20 20  zeof(zBase), .  
1c07d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c07e 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
1c07f 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1c080 54 48 5d 29 3b 0a 20 20 6f 75 74 2e 64 62 20 3d  TH]);.  out.db =
1c081 20 64 62 3b 0a 20 20 77 68 69 6c 65 28 20 7a 52   db;.  while( zR
1c082 61 77 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20  awSql[0] ){.    
1c083 6e 20 3d 20 66 69 6e 64 4e 65 78 74 48 6f 73 74  n = findNextHost
1c084 50 61 72 61 6d 65 74 65 72 28 7a 52 61 77 53 71  Parameter(zRawSq
1c085 6c 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  l, &nToken);.   
1c086 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
1c087 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
1c088 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20  cumAppend(&out, 
1c089 7a 52 61 77 53 71 6c 2c 20 6e 29 3b 0a 20 20 20  zRawSql, n);.   
1c08a 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 3b 0a 20   zRawSql += n;. 
1c08b 20 20 20 61 73 73 65 72 74 28 20 7a 52 61 77 53     assert( zRawS
1c08c 71 6c 5b 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e 3d  ql[0] || nToken=
1c08d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 54  =0 );.    if( nT
1c08e 6f 6b 65 6e 3d 3d 30 20 29 20 62 72 65 61 6b 3b  oken==0 ) break;
1c08f 0a 20 20 20 20 69 66 28 20 7a 52 61 77 53 71 6c  .    if( zRawSql
1c090 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20  [0]=='?' ){.    
1c091 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3e 31 20 29    if( nToken>1 )
1c092 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1c093 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
1c094 28 7a 52 61 77 53 71 6c 5b 31 5d 29 20 29 3b 0a  (zRawSql[1]) );.
1c095 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
1c096 65 74 49 6e 74 33 32 28 26 7a 52 61 77 53 71 6c  etInt32(&zRawSql
1c097 5b 31 5d 2c 20 26 69 64 78 29 3b 0a 20 20 20 20  [1], &idx);.    
1c098 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1c099 20 69 64 78 20 3d 20 6e 65 78 74 49 6e 64 65 78   idx = nextIndex
1c09a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1c09b 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1c09c 74 28 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27  t( zRawSql[0]=='
1c09d 3a 27 20 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d  :' || zRawSql[0]
1c09e 3d 3d 27 24 27 20 7c 7c 20 7a 52 61 77 53 71 6c  =='$' || zRawSql
1c09f 5b 30 5d 3d 3d 27 40 27 20 29 3b 0a 20 20 20 20  [0]=='@' );.    
1c0a0 20 20 74 65 73 74 63 61 73 65 28 20 7a 52 61 77    testcase( zRaw
1c0a1 53 71 6c 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20  Sql[0]==':' );. 
1c0a2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
1c0a3 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 24 27 20 29  RawSql[0]=='$' )
1c0a4 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1c0a5 28 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 40  ( zRawSql[0]=='@
1c0a6 27 20 29 3b 0a 20 20 20 20 20 20 69 64 78 20 3d  ' );.      idx =
1c0a7 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 61   sqlite3VdbePara
1c0a8 6d 65 74 65 72 49 6e 64 65 78 28 70 2c 20 7a 52  meterIndex(p, zR
1c0a9 61 77 53 71 6c 2c 20 6e 54 6f 6b 65 6e 29 3b 0a  awSql, nToken);.
1c0aa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64        assert( id
1c0ab 78 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  x>0 );.    }.   
1c0ac 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 54 6f 6b   zRawSql += nTok
1c0ad 65 6e 3b 0a 20 20 20 20 6e 65 78 74 49 6e 64 65  en;.    nextInde
1c0ae 78 20 3d 20 69 64 78 20 2b 20 31 3b 0a 20 20 20  x = idx + 1;.   
1c0af 20 61 73 73 65 72 74 28 20 69 64 78 3e 30 20 26   assert( idx>0 &
1c0b0 26 20 69 64 78 3c 3d 70 2d 3e 6e 56 61 72 20 29  & idx<=p->nVar )
1c0b1 3b 0a 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d  ;.    pVar = &p-
1c0b2 3e 61 56 61 72 5b 69 64 78 2d 31 5d 3b 0a 20 20  >aVar[idx-1];.  
1c0b3 20 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61 67    if( pVar->flag
1c0b4 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1c0b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
1c0b6 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74  AccumAppend(&out
1c0b7 2c 20 22 4e 55 4c 4c 22 2c 20 34 29 3b 0a 20 20  , "NULL", 4);.  
1c0b8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72    }else if( pVar
1c0b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1c0ba 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
1c0bb 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20  e3XPrintf(&out, 
1c0bc 22 25 6c 6c 64 22 2c 20 70 56 61 72 2d 3e 75 2e  "%lld", pVar->u.
1c0bd 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  i);.    }else if
1c0be 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20  ( pVar->flags & 
1c0bf 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
1c0c0 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66    sqlite3XPrintf
1c0c1 28 26 6f 75 74 2c 20 22 25 21 2e 31 35 67 22 2c  (&out, "%!.15g",
1c0c2 20 70 56 61 72 2d 3e 72 29 3b 0a 20 20 20 20 7d   pVar->r);.    }
1c0c3 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66  else if( pVar->f
1c0c4 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29  lags & MEM_Str )
1c0c5 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
1c0c6 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 20 20  _OMIT_UTF16.    
1c0c7 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64    u8 enc = ENC(d
1c0c8 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e  b);.      if( en
1c0c9 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
1c0ca 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 75 74  {.        Mem ut
1c0cb 66 38 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  f8;.        mems
1c0cc 65 74 28 26 75 74 66 38 2c 20 30 2c 20 73 69 7a  et(&utf8, 0, siz
1c0cd 65 6f 66 28 75 74 66 38 29 29 3b 0a 20 20 20 20  eof(utf8));.    
1c0ce 20 20 20 20 75 74 66 38 2e 64 62 20 3d 20 64 62      utf8.db = db
1c0cf 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1c0d0 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26  3VdbeMemSetStr(&
1c0d1 75 74 66 38 2c 20 70 56 61 72 2d 3e 7a 2c 20 70  utf8, pVar->z, p
1c0d2 56 61 72 2d 3e 6e 2c 20 65 6e 63 2c 20 53 51 4c  Var->n, enc, SQL
1c0d3 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
1c0d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c0d5 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26  ChangeEncoding(&
1c0d6 75 74 66 38 2c 20 53 51 4c 49 54 45 5f 55 54 46  utf8, SQLITE_UTF
1c0d7 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  8);.        sqli
1c0d8 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c  te3XPrintf(&out,
1c0d9 20 22 27 25 2e 2a 71 27 22 2c 20 75 74 66 38 2e   "'%.*q'", utf8.
1c0da 6e 2c 20 75 74 66 38 2e 7a 29 3b 0a 20 20 20 20  n, utf8.z);.    
1c0db 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1c0dc 65 6d 52 65 6c 65 61 73 65 28 26 75 74 66 38 29  emRelease(&utf8)
1c0dd 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65  ;.      }else.#e
1c0de 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20  ndif.      {.   
1c0df 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69       sqlite3XPri
1c0e0 6e 74 66 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71  ntf(&out, "'%.*q
1c0e1 27 22 2c 20 70 56 61 72 2d 3e 6e 2c 20 70 56 61  '", pVar->n, pVa
1c0e2 72 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r->z);.      }. 
1c0e3 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 56 61     }else if( pVa
1c0e4 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  r->flags & MEM_Z
1c0e5 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ero ){.      sql
1c0e6 69 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74  ite3XPrintf(&out
1c0e7 2c 20 22 7a 65 72 6f 62 6c 6f 62 28 25 64 29 22  , "zeroblob(%d)"
1c0e8 2c 20 70 56 61 72 2d 3e 75 2e 6e 5a 65 72 6f 29  , pVar->u.nZero)
1c0e9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1c0ea 20 20 20 61 73 73 65 72 74 28 20 70 56 61 72 2d     assert( pVar-
1c0eb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1c0ec 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  b );.      sqlit
1c0ed 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
1c0ee 28 26 6f 75 74 2c 20 22 78 27 22 2c 20 32 29 3b  (&out, "x'", 2);
1c0ef 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
1c0f0 69 3c 70 56 61 72 2d 3e 6e 3b 20 69 2b 2b 29 7b  i<pVar->n; i++){
1c0f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c0f2 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25  XPrintf(&out, "%
1c0f3 30 32 78 22 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d  02x", pVar->z[i]
1c0f4 26 30 78 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a  &0xff);.      }.
1c0f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
1c0f6 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74  AccumAppend(&out
1c0f7 2c 20 22 27 22 2c 20 31 29 3b 0a 20 20 20 20 7d  , "'", 1);.    }
1c0f8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
1c0f9 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
1c0fa 69 73 68 28 26 6f 75 74 29 3b 0a 7d 0a 0a 23 65  ish(&out);.}..#e
1c0fb 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20  ndif /* #ifndef 
1c0fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1c0fd 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
1c0fe 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1c0ff 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  etrace.c *******
1c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c101 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c102 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1c103 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1c104 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1c105 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c106 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c107 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
1c108 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
1c109 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1c10a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1c10b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1c10c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1c10d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1c10e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1c10f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1c110 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1c111 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1c112 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1c113 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1c114 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1c115 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1c116 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1c117 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1c118 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1c119 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1c11a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1c11b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c11c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c11d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c11e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1c11f 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
1c120 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
1c121 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f   execution metho
1c122 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72  d of the .** Vir
1c123 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e  tual Database En
1c124 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20  gine (VDBE).  A 
1c125 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22  separate file ("
1c126 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68  vdbeaux.c").** h
1c127 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70  andles housekeep
1c128 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68  ing details such
1c129 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64   as creating and
1c12a 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42   deleting.** VDB
1c12b 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68  E instances.  Th
1c12c 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c  is file is solel
1c12d 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20  y interested in 
1c12e 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65  executing.** the
1c12f 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a   VDBE program..*
1c130 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65  *.** In the exte
1c131 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20  rnal interface, 
1c132 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  an "sqlite3_stmt
1c133 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20  *" is an opaque 
1c134 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20  pointer.** to a 
1c135 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  VDBE..**.** The 
1c136 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72  SQL parser gener
1c137 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77  ates a program w
1c138 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65  hich is then exe
1c139 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cuted by.** the 
1c13a 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77  VDBE to do the w
1c13b 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ork of the SQL s
1c13c 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20  tatement.  VDBE 
1c13d 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a  programs are .**
1c13e 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d   similar in form
1c13f 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e   to assembly lan
1c140 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67  guage.  The prog
1c141 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a  ram consists of.
1c142 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75  ** a linear sequ
1c143 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f  ence of operatio
1c144 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74  ns.  Each operat
1c145 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64  ion has an opcod
1c146 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72  e .** and 5 oper
1c147 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20  ands.  Operands 
1c148 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61  P1, P2, and P3 a
1c149 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70  re integers.  Op
1c14a 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20  erand P4 .** is 
1c14b 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
1c14c 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61  d string.  Opera
1c14d 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69  nd P5 is an unsi
1c14e 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a  gned character..
1c14f 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75  ** Few opcodes u
1c150 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64  se all 5 operand
1c151 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61  s..**.** Computa
1c152 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65  tion results are
1c153 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74   stored on a set
1c154 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75   of registers nu
1c155 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67  mbered beginning
1c156 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67  .** with 1 and g
1c157 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e  oing up to Vdbe.
1c158 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69  nMem.  Each regi
1c159 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a  ster can store.*
1c15a 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65  * either an inte
1c15b 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  ger, a null-term
1c15c 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61  inated string, a
1c15d 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a   floating point.
1c15e 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68  ** number, or th
1c15f 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c  e SQL "NULL" val
1c160 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74  ue.  An implicit
1c161 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d   conversion from
1c162 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20   one.** type to 
1c163 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73  the other occurs
1c164 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
1c165 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68  * .** Most of th
1c166 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
1c167 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20  ile is taken up 
1c168 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64  by the sqlite3Vd
1c169 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63  beExec().** func
1c16a 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20  tion which does 
1c16b 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65  the work of inte
1c16c 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20  rpreting a VDBE 
1c16d 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20  program..** But 
1c16e 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61  other routines a
1c16f 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64  re also provided
1c170 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c   to help in buil
1c171 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f  ding up.** a pro
1c172 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
1c173 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e   by instruction.
1c174 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73  .**.** Various s
1c175 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73  cripts scan this
1c176 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20   source file in 
1c177 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
1c178 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65  e HTML.** docume
1c179 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73  ntation, headers
1c17a 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72   files, or other
1c17b 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20   derived files. 
1c17c 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a   The formatting.
1c17d 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  ** of the code i
1c17e 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20  n this file is, 
1c17f 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72  therefore, impor
1c180 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72  tant.  See other
1c181 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20   comments.** in 
1c182 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65  this file for de
1c183 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f  tails.  If in do
1c184 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69  ubt, do not devi
1c185 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e  ate from existin
1c186 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20  g.** commenting 
1c187 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20  and indentation 
1c188 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63  practices when c
1c189 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e  hanging or addin
1c18a 67 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  g code..*/../*.*
1c18b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1c18c 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1c18d 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1c18e 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73  very time a curs
1c18f 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74  or.** moves, eit
1c190 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65  her by the OP_Se
1c191 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f  ekXX, OP_Next, o
1c192 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65  r OP_Prev opcode
1c193 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20  s.  The test.** 
1c194 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74  procedures use t
1c195 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
1c196 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1c197 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a  t indices are.**
1c198 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74   working correct
1c199 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62  ly.  This variab
1c19a 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69  le has no functi
1c19b 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f  on other than to
1c19c 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20  .** help verify 
1c19d 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72  the correct oper
1c19e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  ation of the lib
1c19f 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rary..*/.#ifdef 
1c1a0 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
1c1a1 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1c1a2 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20  e3_search_count 
1c1a3 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
1c1a4 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f  ** When this glo
1c1a5 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1c1a6 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74  positive, it get
1c1a7 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e  s decremented on
1c1a8 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63  ce before.** eac
1c1a9 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e  h instruction in
1c1aa 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e   the VDBE.  When
1c1ab 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74   reaches zero, t
1c1ac 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70  he u1.isInterrup
1c1ad 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  ted.** field of 
1c1ae 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75  the sqlite3 stru
1c1af 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20  cture is set in 
1c1b0 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74  order to simulat
1c1b1 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e  e and interrupt.
1c1b2 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69  .**.** This faci
1c1b3 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72  lity is used for
1c1b4 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65   testing purpose
1c1b5 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73  s only.  It does
1c1b6 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   not function.**
1c1b7 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20   in an ordinary 
1c1b8 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  build..*/.#ifdef
1c1b9 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1c1ba 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1c1bb 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1c1bc 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
1c1bd 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ./*.** The next 
1c1be 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1c1bf 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1c1c0 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f  ach type the OP_
1c1c1 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69  Sort opcode.** i
1c1c2 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65  s executed.  The
1c1c3 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
1c1c4 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d   use this inform
1c1c5 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
1c1c6 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69  re that.** sorti
1c1c7 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20  ng is occurring 
1c1c8 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67  or not occurring
1c1c9 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20   at appropriate 
1c1ca 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61  times.   This va
1c1cb 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f  riable.** has no
1c1cc 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
1c1cd 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72  than to help ver
1c1ce 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1c1cf 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1c1d0 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  .** library..*/.
1c1d1 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1c1d2 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1c1d3 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63  t sqlite3_sort_c
1c1d4 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1c1d5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
1c1d6 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1c1d7 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a   records the siz
1c1d8 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  e of the largest
1c1d9 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20   MEM_Blob.** or 
1c1da 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73  MEM_Str that has
1c1db 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20   been used by a 
1c1dc 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68  VDBE opcode.  Th
1c1dd 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  e test procedure
1c1de 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e  s.** use this in
1c1df 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1c1e0 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1c1e1 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69  zero-blob functi
1c1e2 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f  onality.** is wo
1c1e3 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  rking correctly.
1c1e4 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65     This variable
1c1e5 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e   has no function
1c1e6 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a   other than to.*
1c1e7 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68  * help verify th
1c1e8 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
1c1e9 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
1c1ea 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ry..*/.#ifdef SQ
1c1eb 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
1c1ec 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1c1ed 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20  _max_blobsize = 
1c1ee 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  0;.static void u
1c1ef 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65  pdateMaxBlobsize
1c1f0 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20  (Mem *p){.  if( 
1c1f1 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  (p->flags & (MEM
1c1f2 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21  _Str|MEM_Blob))!
1c1f3 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74  =0 && p->n>sqlit
1c1f4 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
1c1f5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
1c1f6 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d  ax_blobsize = p-
1c1f7 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  >n;.  }.}.#endif
1c1f8 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
1c1f9 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1c1fa 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
1c1fb 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50  each type the OP
1c1fc 5f 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a  _Found opcode.**
1c1fd 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68   is executed. Th
1c1fe 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65  is is used to te
1c1ff 73 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  st whether or no
1c200 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  t the foreign ke
1c201 79 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69  y.** operation i
1c202 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
1c203 20 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20   OP_FkIsZero is 
1c204 77 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61  working. This va
1c205 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f  riable.** has no
1c206 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
1c207 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72  than to help ver
1c208 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1c209 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1c20a 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  .** library..*/.
1c20b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1c20c 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1c20d 74 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f  t sqlite3_found_
1c20e 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
1c20f 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20  f../*.** Test a 
1c210 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20  register to see 
1c211 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68  if it exceeds th
1c212 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75  e current maximu
1c213 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20  m blob size..** 
1c214 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f  If it does, reco
1c215 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d  rd the new maxim
1c216 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f  um blob size..*/
1c217 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
1c218 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65  ITE_TEST) && !de
1c219 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1c21a 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a  T_BUILTIN_TEST).
1c21b 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f  # define UPDATE_
1c21c 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20  MAX_BLOBSIZE(P) 
1c21d 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69   updateMaxBlobsi
1c21e 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65  ze(P).#else.# de
1c21f 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f  fine UPDATE_MAX_
1c220 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64  BLOBSIZE(P).#end
1c221 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  if../*.** Conver
1c222 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69  t the given regi
1c223 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69  ster into a stri
1c224 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f  ng if it isn't o
1c225 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52  ne.** already. R
1c226 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
1c227 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
1c228 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ls..*/.#define S
1c229 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29  tringify(P, enc)
1c22a 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66   \.   if(((P)->f
1c22b 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  lags&(MEM_Str|ME
1c22c 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73  M_Blob))==0 && s
1c22d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
1c22e 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c  ingify(P,enc)) \
1c22f 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f  .     { goto no_
1c230 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  mem; }../*.** An
1c231 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e   ephemeral strin
1c232 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69  g value (signifi
1c233 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70  ed by the MEM_Ep
1c234 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69  hem flag) contai
1c235 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  ns.** a pointer 
1c236 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  to a dynamically
1c237 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e   allocated strin
1c238 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68  g where some oth
1c239 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20  er entity.** is 
1c23a 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1c23b 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61  deallocating tha
1c23c 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75  t string.  Becau
1c23d 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a  se the register.
1c23e 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  ** does not cont
1c23f 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20  rol the string, 
1c240 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65  it might be dele
1c241 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20  ted without the 
1c242 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77  register.** know
1c243 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ing it..**.** Th
1c244 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65  is routine conve
1c245 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c  rts an ephemeral
1c246 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64   string into a d
1c247 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
1c248 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74  ated.** string t
1c249 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72  hat the register
1c24a 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73   itself controls
1c24b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1c24c 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74  s, it.** convert
1c24d 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73  s an MEM_Ephem s
1c24e 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45  tring into an ME
1c24f 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f  M_Dyn string..*/
1c250 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d  .#define Deephem
1c251 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20  eralize(P) \.   
1c252 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26  if( ((P)->flags&
1c253 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a  MEM_Ephem)!=0 \.
1c254 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
1c255 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
1c256 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f  eable(P) ){ goto
1c257 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a   no_mem;}../*.**
1c258 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62   Call sqlite3Vdb
1c259 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29  eMemExpandBlob()
1c25a 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64   on the supplied
1c25b 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d   value (type Mem
1c25c 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69  *).** P if requi
1c25d 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  red..*/.#define 
1c25e 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28  ExpandBlob(P) ((
1c25f 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  (P)->flags&MEM_Z
1c260 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65  ero)?sqlite3Vdbe
1c261 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29  MemExpandBlob(P)
1c262 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  :0)../*.** Argum
1c263 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20  ent pMem points 
1c264 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68  at a register th
1c265 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  at will be passe
1c266 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64  d to a.** user-d
1c267 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
1c268 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  or returned to t
1c269 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72  he user as the r
1c26a 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79  esult of a query
1c26b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1c26c 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d  e sets the pMem-
1c26d 3e 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75  >type variable u
1c26e 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74  sed by the sqlit
1c26f 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a  e3_value_*() .**
1c270 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51   routines..*/.SQ
1c271 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1c272 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
1c273 53 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70  StoreType(Mem *p
1c274 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67  Mem){.  int flag
1c275 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b  s = pMem->flags;
1c276 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  .  if( flags & M
1c277 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70  EM_Null ){.    p
1c278 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1c279 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65  TE_NULL;.  }.  e
1c27a 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
1c27b 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70  MEM_Int ){.    p
1c27c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1c27d 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a  TE_INTEGER;.  }.
1c27e 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73    else if( flags
1c27f 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
1c280 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1c281 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20  SQLITE_FLOAT;.  
1c282 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61  }.  else if( fla
1c283 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a  gs & MEM_Str ){.
1c284 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1c285 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1c286 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
1c287 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42  >type = SQLITE_B
1c288 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  LOB;.  }.}../*.*
1c289 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43  * Allocate VdbeC
1c28a 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75  ursor number iCu
1c28b 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
1c28c 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74  nter to it.  Ret
1c28d 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77  urn NULL.** if w
1c28e 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d  e run out of mem
1c28f 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56  ory..*/.static V
1c290 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63  dbeCursor *alloc
1c291 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62  ateCursor(.  Vdb
1c292 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
1c293 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
1c294 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69  l machine */.  i
1c295 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
1c296 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1c297 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72   the new VdbeCur
1c298 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  sor */.  int nFi
1c299 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  eld,           /
1c29a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * Number of fiel
1c29b 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ds in the table 
1c29c 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  or index */.  in
1c29d 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
1c29e 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61      /* When data
1c29f 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20  base the cursor 
1c2a0 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d  belongs to, or -
1c2a1 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72  1 */.  int isBtr
1c2a2 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20  eeCursor     /* 
1c2a3 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65 2e  True for B-Tree.
1c2a4 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65 75    False for pseu
1c2a5 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61 62  do-table or vtab
1c2a6 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64   */.){.  /* Find
1c2a7 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1c2a8 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73   that will be us
1c2a9 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
1c2aa 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20  blob of memory. 
1c2ab 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72   ** required for
1c2ac 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72   this VdbeCursor
1c2ad 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69   structure. It i
1c2ae 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20  s convenient to 
1c2af 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65  use a .  ** vdbe
1c2b0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20   memory cell to 
1c2b1 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72  manage the memor
1c2b2 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
1c2b3 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a  uired for a.  **
1c2b4 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75   VdbeCursor stru
1c2b5 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f  cture for the fo
1c2b6 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a  llowing reasons:
1c2b7 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53  .  **.  **   * S
1c2b8 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20  ometimes cursor 
1c2b9 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64  numbers are used
1c2ba 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66   for a couple of
1c2bb 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20   different.  ** 
1c2bc 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20      purposes in 
1c2bd 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  a vdbe program. 
1c2be 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73  The different us
1c2bf 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65  es might require
1c2c0 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72  .  **     differ
1c2c1 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61  ent sized alloca
1c2c2 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65  tions. Memory ce
1c2c3 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77  lls provide grow
1c2c4 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c  able.  **     al
1c2c5 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a  locations..  **.
1c2c6 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73    **   * When us
1c2c7 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ing ENABLE_MEMOR
1c2c8 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65  Y_MANAGEMENT, me
1c2c9 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72  mory cell buffer
1c2ca 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62  s can.  **     b
1c2cb 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76  e freed lazily v
1c2cc 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  ia the sqlite3_r
1c2cd 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
1c2ce 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20  API. This.  **  
1c2cf 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65     minimizes the
1c2d0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f   number of mallo
1c2d1 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20  c calls made by 
1c2d2 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a  the system..  **
1c2d3 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c  .  ** Memory cel
1c2d4 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61  ls for cursors a
1c2d5 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  re allocated at 
1c2d6 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61  the top of the a
1c2d7 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63  ddress.  ** spac
1c2d8 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28  e. Memory cell (
1c2d9 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70  p->nMem) corresp
1c2da 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30  onds to cursor 0
1c2db 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a  . Space for.  **
1c2dc 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e   cursor 1 is man
1c2dd 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63  aged by memory c
1c2de 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c  ell (p->nMem-1),
1c2df 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d   etc..  */.  Mem
1c2e0 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65   *pMem = &p->aMe
1c2e1 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b  m[p->nMem-iCur];
1c2e2 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  ..  int nByte;. 
1c2e3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c2e4 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20   = 0;.  nByte = 
1c2e5 0a 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69  .      ROUND8(si
1c2e6 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29  zeof(VdbeCursor)
1c2e7 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74  ) + .      (isBt
1c2e8 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65  reeCursor?sqlite
1c2e9 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
1c2ea 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32  ():0) + .      2
1c2eb 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75  *nField*sizeof(u
1c2ec 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  32);..  assert( 
1c2ed 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  iCur<p->nCursor 
1c2ee 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73  );.  if( p->apCs
1c2ef 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73  r[iCur] ){.    s
1c2f0 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
1c2f1 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72  rsor(p, p->apCsr
1c2f2 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e  [iCur]);.    p->
1c2f3 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b  apCsr[iCur] = 0;
1c2f4 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
1c2f5 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62  E_OK==sqlite3Vdb
1c2f6 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
1c2f7 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
1c2f8 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d  p->apCsr[iCur] =
1c2f9 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73   pCx = (VdbeCurs
1c2fa 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  or*)pMem->z;.   
1c2fb 20 6d 65 6d 73 65 74 28 70 43 78 2c 20 30 2c 20   memset(pCx, 0, 
1c2fc 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f  sizeof(VdbeCurso
1c2fd 72 29 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44  r));.    pCx->iD
1c2fe 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78  b = iDb;.    pCx
1c2ff 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c  ->nField = nFiel
1c300 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c  d;.    if( nFiel
1c301 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e  d ){.      pCx->
1c302 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26  aType = (u32 *)&
1c303 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73  pMem->z[ROUND8(s
1c304 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1c305 29 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ))];.    }.    i
1c306 66 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72  f( isBtreeCursor
1c307 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70   ){.      pCx->p
1c308 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73  Cursor = (BtCurs
1c309 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26  or*).          &
1c30a 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73  pMem->z[ROUND8(s
1c30b 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1c30c 29 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65  ))+2*nField*size
1c30d 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 20 20  of(u32)];.      
1c30e 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1c30f 6f 72 5a 65 72 6f 28 70 43 78 2d 3e 70 43 75 72  orZero(pCx->pCur
1c310 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  sor);.    }.  }.
1c311 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a    return pCx;.}.
1c312 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f  ./*.** Try to co
1c313 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e  nvert a value in
1c314 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70  to a numeric rep
1c315 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77  resentation if w
1c316 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77  e can.** do so w
1c317 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69  ithout loss of i
1c318 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20  nformation.  In 
1c319 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
1c31a 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f  the string.** lo
1c31b 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65  oks like a numbe
1c31c 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e  r, convert it in
1c31d 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66  to a number.  If
1c31e 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20   it does not.** 
1c31f 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62  look like a numb
1c320 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f  er, leave it alo
1c321 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ne..*/.static vo
1c322 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  id applyNumericA
1c323 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65  ffinity(Mem *pRe
1c324 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d  c){.  if( (pRec-
1c325 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65  >flags & (MEM_Re
1c326 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20  al|MEM_Int))==0 
1c327 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e  ){.    int realn
1c328 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  um;.    sqlite3V
1c329 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
1c32a 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66  te(pRec);.    if
1c32b 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ( (pRec->flags&M
1c32c 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20  EM_Str).        
1c32d 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   && sqlite3IsNum
1c32e 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65  ber(pRec->z, &re
1c32f 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63  alnum, pRec->enc
1c330 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  ) ){.      i64 v
1c331 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  alue;.      sqli
1c332 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1c333 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49  oding(pRec, SQLI
1c334 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20  TE_UTF8);.      
1c335 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20  if( !realnum && 
1c336 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52  sqlite3Atoi64(pR
1c337 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29  ec->z, &value) )
1c338 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e  {.        pRec->
1c339 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20  u.i = value;.   
1c33a 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46       MemSetTypeF
1c33b 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e  lag(pRec, MEM_In
1c33c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
1c33d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c33e 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70  VdbeMemRealify(p
1c33f 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Rec);.      }.  
1c340 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1c341 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64   Processing is d
1c342 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20  etermine by the 
1c343 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74  affinity paramet
1c344 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  er:.**.** SQLITE
1c345 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a  _AFF_INTEGER:.**
1c346 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
1c347 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  :.** SQLITE_AFF_
1c348 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54  NUMERIC:.**    T
1c349 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52  ry to convert pR
1c34a 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  ec to an integer
1c34b 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1c34c 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61  or a .**    floa
1c34d 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65  ting-point repre
1c34e 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20  sentation if an 
1c34f 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
1c350 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20  tation.**    is 
1c351 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e  not possible.  N
1c352 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74  ote that the int
1c353 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
1c354 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77  ion is.**    alw
1c355 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65  ays preferred, e
1c356 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e  ven if the affin
1c357 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63  ity is REAL, bec
1c358 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e  ause.**    an in
1c359 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1c35a 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61  tion is more spa
1c35b 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20  ce efficient on 
1c35c 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  disk..**.** SQLI
1c35d 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20  TE_AFF_TEXT:.** 
1c35e 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20     Convert pRec 
1c35f 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73  to a text repres
1c360 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1c361 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a  SQLITE_AFF_NONE:
1c362 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70  .**    No-op.  p
1c363 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64  Rec is unchanged
1c364 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1c365 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a   applyAffinity(.
1c366 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20    Mem *pRec,    
1c367 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c        /* The val
1c368 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69  ue to apply affi
1c369 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61  nity to */.  cha
1c36a 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20  r affinity,     
1c36b 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
1c36c 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a   to be applied *
1c36d 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20  /.  u8 enc      
1c36e 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1c36f 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  his text encodin
1c370 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66  g */.){.  if( af
1c371 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1c372 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f  FF_TEXT ){.    /
1c373 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74  * Only attempt t
1c374 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f  he conversion to
1c375 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69   TEXT if there i
1c376 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20  s an integer or 
1c377 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72  real.    ** repr
1c378 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62  esentation (blob
1c379 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74   and NULL do not
1c37a 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20   get converted) 
1c37b 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20  but no string.  
1c37c 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
1c37d 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
1c37e 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c  if( 0==(pRec->fl
1c37f 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20  ags&MEM_Str) && 
1c380 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45  (pRec->flags&(ME
1c381 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29  M_Real|MEM_Int))
1c382 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c383 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66  3VdbeMemStringif
1c384 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20  y(pRec, enc);.  
1c385 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c    }.    pRec->fl
1c386 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61  ags &= ~(MEM_Rea
1c387 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65  l|MEM_Int);.  }e
1c388 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79  lse if( affinity
1c389 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  !=SQLITE_AFF_NON
1c38a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  E ){.    assert(
1c38b 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1c38c 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c  E_AFF_INTEGER ||
1c38d 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
1c38e 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20  E_AFF_REAL.     
1c38f 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e          || affin
1c390 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
1c391 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61  NUMERIC );.    a
1c392 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1c393 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69  ity(pRec);.    i
1c394 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  f( pRec->flags &
1c395 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
1c396 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e     sqlite3VdbeIn
1c397 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52  tegerAffinity(pR
1c398 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ec);.    }.  }.}
1c399 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63  ../*.** Try to c
1c39a 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20  onvert the type 
1c39b 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72  of a function ar
1c39c 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75  gument or a resu
1c39d 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74  lt column.** int
1c39e 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72  o a numeric repr
1c39f 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65  esentation.  Use
1c3a0 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20   either INTEGER 
1c3a1 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65  or REAL whicheve
1c3a2 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69  r.** is appropri
1c3a3 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64  ate.  But only d
1c3a4 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  o the conversion
1c3a5 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
1c3a6 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f  le without.** lo
1c3a7 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f  ss of informatio
1c3a8 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  n and return the
1c3a9 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66   revised type of
1c3aa 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
1c3ab 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  *.** This is an 
1c3ac 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69  EXPERIMENTAL api
1c3ad 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20   and is subject 
1c3ae 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d  to change or rem
1c3af 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oval..*/.SQLITE_
1c3b0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1c3b1 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
1c3b2 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
1c3b3 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a   *pVal){.  Mem *
1c3b4 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61  pMem = (Mem*)pVa
1c3b5 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  l;.  applyNumeri
1c3b6 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b  cAffinity(pMem);
1c3b7 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1c3b8 6d 53 74 6f 72 65 54 79 70 65 28 70 4d 65 6d 29  mStoreType(pMem)
1c3b9 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d  ;.  return pMem-
1c3ba 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  >type;.}../*.** 
1c3bb 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e  Exported version
1c3bc 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74   of applyAffinit
1c3bd 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f  y(). This one wo
1c3be 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76  rks on sqlite3_v
1c3bf 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74  alue*, .** not t
1c3c0 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a  he internal Mem*
1c3c1 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   type..*/.SQLITE
1c3c2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c3c3 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
1c3c4 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74  ffinity(.  sqlit
1c3c5 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
1c3c6 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20  .  u8 affinity, 
1c3c7 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61  .  u8 enc.){.  a
1c3c8 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65  pplyAffinity((Me
1c3c9 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69  m *)pVal, affini
1c3ca 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66  ty, enc);.}..#if
1c3cb 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1c3cc 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e  ./*.** Write a n
1c3cd 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  ice string repre
1c3ce 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1c3cf 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c   contents of cel
1c3d0 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62  l pMem.** into b
1c3d1 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67  uffer zBuf, leng
1c3d2 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49  th nBuf..*/.SQLI
1c3d3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1c3d4 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
1c3d5 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70  ettyPrint(Mem *p
1c3d6 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Mem, char *zBuf)
1c3d7 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d  {.  char *zCsr =
1c3d8 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d   zBuf;.  int f =
1c3d9 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20   pMem->flags;.. 
1c3da 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
1c3db 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d  ar *const encnam
1c3dc 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22  es[] = {"(X)", "
1c3dd 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20  (8)", "(16LE)", 
1c3de 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66  "(16BE)"};..  if
1c3df 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a  ( f&MEM_Blob ){.
1c3e0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
1c3e1 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66  har c;.    if( f
1c3e2 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20   & MEM_Dyn ){.  
1c3e3 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20      c = 'z';.   
1c3e4 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c3e5 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
1c3e6 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20  Ephem))==0 );.  
1c3e7 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20    }else if( f & 
1c3e8 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
1c3e9 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20      c = 't';.   
1c3ea 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c3eb 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  (MEM_Dyn|MEM_Eph
1c3ec 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
1c3ed 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1c3ee 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20  _Ephem ){.      
1c3ef 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61  c = 'e';.      a
1c3f0 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
1c3f1 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29  _Static|MEM_Dyn)
1c3f2 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
1c3f3 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27  e{.      c = 's'
1c3f4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1c3f5 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
1c3f6 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63  0, zCsr, "%c", c
1c3f7 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73  );.    zCsr += s
1c3f8 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
1c3f9 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1c3fa 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c3fb 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65  zCsr, "%d[", pMe
1c3fc 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20  m->n);.    zCsr 
1c3fd 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c3fe 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f  30(zCsr);.    fo
1c3ff 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69  r(i=0; i<16 && i
1c400 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a  <pMem->n; i++){.
1c401 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1c402 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1c403 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29  , "%02X", ((int)
1c404 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46  pMem->z[i] & 0xF
1c405 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20  F));.      zCsr 
1c406 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c407 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a  30(zCsr);.    }.
1c408 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31      for(i=0; i<1
1c409 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20  6 && i<pMem->n; 
1c40a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  i++){.      char
1c40b 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b   z = pMem->z[i];
1c40c 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20  .      if( z<32 
1c40d 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72  || z>126 ) *zCsr
1c40e 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  ++ = '.';.      
1c40f 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a  else *zCsr++ = z
1c410 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1c411 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
1c412 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20  0, zCsr, "]%s", 
1c413 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65  encnames[pMem->e
1c414 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  nc]);.    zCsr +
1c415 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1c416 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28  0(zCsr);.    if(
1c417 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b   f & MEM_Zero ){
1c418 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1c419 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
1c41a 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75  r,"+%dz",pMem->u
1c41b 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a  .nZero);.      z
1c41c 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
1c41d 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20  rlen30(zCsr);.  
1c41e 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20    }.    *zCsr = 
1c41f 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66  '\0';.  }else if
1c420 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ( f & MEM_Str ){
1c421 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20  .    int j, k;. 
1c422 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27     zBuf[0] = ' '
1c423 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1c424 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a  M_Dyn ){.      z
1c425 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20  Buf[1] = 'z';.  
1c426 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26      assert( (f &
1c427 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d   (MEM_Static|MEM
1c428 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
1c429 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
1c42a 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20   MEM_Static ){. 
1c42b 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
1c42c 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t';.      assert
1c42d 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c  ( (f & (MEM_Dyn|
1c42e 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
1c42f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1c430 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b  f & MEM_Ephem ){
1c431 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d  .      zBuf[1] =
1c432 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'e';.      asse
1c433 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1c434 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d  atic|MEM_Dyn))==
1c435 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
1c436 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20        zBuf[1] = 
1c437 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b  's';.    }.    k
1c438 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 2;.    sqlite
1c439 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c43a 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20  &zBuf[k], "%d", 
1c43b 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20  pMem->n);.    k 
1c43c 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c43d 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
1c43e 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b    zBuf[k++] = '[
1c43f 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ';.    for(j=0; 
1c440 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e  j<15 && j<pMem->
1c441 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75  n; j++){.      u
1c442 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d  8 c = pMem->z[j]
1c443 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30  ;.      if( c>=0
1c444 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b  x20 && c<0x7f ){
1c445 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b  .        zBuf[k+
1c446 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65  +] = c;.      }e
1c447 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  lse{.        zBu
1c448 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20  f[k++] = '.';.  
1c449 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1c44a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b  zBuf[k++] = ']';
1c44b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1c44c 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b  rintf(100,&zBuf[
1c44d 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65  k], encnames[pMe
1c44e 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20  m->enc]);.    k 
1c44f 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
1c450 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
1c451 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b    zBuf[k++] = 0;
1c452 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
1c453 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1c454 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  UG./*.** Print t
1c455 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65  he value of a re
1c456 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69  gister for traci
1c457 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a  ng purposes:.*/.
1c458 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54  static void memT
1c459 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a  racePrint(FILE *
1c45a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  out, Mem *p){.  
1c45b 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
1c45c 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66  EM_Null ){.    f
1c45d 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55  printf(out, " NU
1c45e 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  LL");.  }else if
1c45f 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d  ( (p->flags & (M
1c460 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29  EM_Int|MEM_Str))
1c461 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  ==(MEM_Int|MEM_S
1c462 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  tr) ){.    fprin
1c463 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c  tf(out, " si:%ll
1c464 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d  d", p->u.i);.  }
1c465 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
1c466 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1c467 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1c468 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e  " i:%lld", p->u.
1c469 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  i);.#ifndef SQLI
1c46a 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
1c46b 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69  _POINT.  }else i
1c46c 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
1c46d 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70  M_Real ){.    fp
1c46e 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25  rintf(out, " r:%
1c46f 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69  g", p->r);.#endi
1c470 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  f.  }else if( p-
1c471 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1c472 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  Set ){.    fprin
1c473 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65  tf(out, " (rowse
1c474 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  t)");.  }else{. 
1c475 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30     char zBuf[200
1c476 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
1c477 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
1c478 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66  (p, zBuf);.    f
1c479 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29  printf(out, " ")
1c47a 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
1c47b 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  t, "%s", zBuf);.
1c47c 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
1c47d 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28  d registerTrace(
1c47e 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69  FILE *out, int i
1c47f 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  Reg, Mem *p){.  
1c480 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45  fprintf(out, "RE
1c481 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29  G[%d] = ", iReg)
1c482 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e  ;.  memTracePrin
1c483 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72  t(out, p);.  fpr
1c484 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
1c485 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .}.#endif..#ifde
1c486 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
1c487 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
1c488 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28  R_TRACE(R,M) if(
1c489 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65  p->trace)registe
1c48a 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c  rTrace(p->trace,
1c48b 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  R,M).#else.#  de
1c48c 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52  fine REGISTER_TR
1c48d 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a  ACE(R,M).#endif.
1c48e 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ..#ifdef VDBE_PR
1c48f 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  OFILE../* .** hw
1c490 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
1c491 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
1c492 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
1c493 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
1c494 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
1c495 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
1c496 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1c497 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
1c498 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
1c499 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1c49a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1c49b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1c49c 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
1c49d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
1c49e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c49f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1c4a0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
1c4a1 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
1c4a2 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1c4a3 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1c4a4 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1c4a5 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1c4a6 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1c4a7 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1c4a8 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1c4a9 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1c4aa 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1c4ab 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1c4ac 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1c4ad 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1c4ae 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1c4af 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1c4b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1c4b1 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1c4b2 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1c4b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4b7 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1c4b8 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
1c4b9 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
1c4ba 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
1c4bb 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
1c4bc 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
1c4bd 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
1c4be 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
1c4bf 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
1c4c0 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
1c4c1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
1c4c2 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
1c4c3 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
1c4c4 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
1c4c5 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
1c4c6 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
1c4c7 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
1c4c8 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
1c4c9 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
1c4ca 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
1c4cb 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
1c4cc 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
1c4cd 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
1c4ce 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
1c4cf 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
1c4d0 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
1c4d1 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
1c4d2 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
1c4d3 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
1c4d4 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
1c4d5 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
1c4d6 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
1c4d7 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
1c4d8 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
1c4d9 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
1c4da 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
1c4db 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
1c4dc 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
1c4dd 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
1c4de 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
1c4df 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
1c4e0 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
1c4e1 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
1c4e2 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
1c4e3 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
1c4e4 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
1c4e5 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
1c4e6 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
1c4e7 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
1c4e8 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
1c4e9 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
1c4ea 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
1c4eb 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
1c4ec 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
1c4ed 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
1c4ee 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
1c4ef 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
1c4f0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
1c4f1 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
1c4f2 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
1c4f3 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
1c4f4 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
1c4f5 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
1c4f6 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
1c4f7 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
1c4f8 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
1c4f9 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
1c4fa 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
1c4fb 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
1c4fc 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
1c4fd 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
1c4fe 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
1c4ff 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
1c500 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
1c501 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
1c502 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
1c503 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
1c504 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
1c505 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
1c506 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
1c507 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
1c508 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
1c509 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
1c50a 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
1c50b 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
1c50c 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
1c50d 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
1c50e 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
1c50f 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
1c510 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
1c511 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
1c512 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
1c513 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
1c514 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
1c515 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
1c516 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
1c517 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
1c518 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
1c519 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
1c51a 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
1c51b 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
1c51c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
1c51d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
1c51e 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
1c51f 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
1c520 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
1c521 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
1c522 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
1c523 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
1c524 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
1c525 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
1c526 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
1c527 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
1c528 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
1c529 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
1c52a 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
1c52b 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
1c52c 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
1c52d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
1c52e 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
1c52f 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
1c530 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
1c531 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
1c532 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
1c533 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
1c534 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
1c535 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1c536 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
1c537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1c53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
1c53b 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
1c53c 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65  left off in vdbe
1c53d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1c53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e  **********/..#en
1c53f 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43  dif../*.** The C
1c540 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
1c541 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  PT macro defined
1c542 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73   here looks to s
1c543 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c  ee if the.** sql
1c544 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
1c545 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65   routine has bee
1c546 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74  n called.  If it
1c547 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a   has been, then.
1c548 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66  ** processing of
1c549 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
1c54a 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  m is interrupted
1c54b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ..**.** This mac
1c54c 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72  ro added to ever
1c54d 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  y instruction th
1c54e 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69  at does a jump i
1c54f 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d  n order to.** im
1c550 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20  plement a loop. 
1c551 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20   This test used 
1c552 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73  to be on every s
1c553 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
1c554 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d  n,.** but that m
1c555 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73  eant we more tes
1c556 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65  ting that we nee
1c557 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65  ded.  By only te
1c558 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61  sting the.** fla
1c559 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75  g on jump instru
1c55a 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61  ctions, we get a
1c55b 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69   (small) speed i
1c55c 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23  mprovement..*/.#
1c55d 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52  define CHECK_FOR
1c55e 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20  _INTERRUPT \.   
1c55f 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
1c560 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20  errupted ) goto 
1c561 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74  abort_due_to_int
1c562 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20  errupt;..#ifdef 
1c563 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61  SQLITE_DEBUG.sta
1c564 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73  tic int fileExis
1c565 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ts(sqlite3 *db, 
1c566 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
1c567 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20  e){.  int res = 
1c568 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  0;.  int rc = SQ
1c569 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20  LITE_OK;.#ifdef 
1c56a 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a  SQLITE_TEST.  /*
1c56b 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65   If we are curre
1c56c 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20  ntly testing IO 
1c56d 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20  errors, then do 
1c56e 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73  not call OsAcces
1c56f 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74  s() to.  ** test
1c570 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63   for the presenc
1c571 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73  e of zFile. This
1c572 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20   is because any 
1c573 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20  IO error that.  
1c574 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77  ** occurs here w
1c575 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72  ill not be repor
1c576 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65  ted, causing the
1c577 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20   test to fail.. 
1c578 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74   */.  extern int
1c579 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
1c57a 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28  r_pending;.  if(
1c57b 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
1c57c 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23  r_pending<=0 ).#
1c57d 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73  endif.    rc = s
1c57e 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64  qlite3OsAccess(d
1c57f 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20  b->pVfs, zFile, 
1c580 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1c581 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72  ISTS, &res);.  r
1c582 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63  eturn (res && rc
1c583 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a  ==SQLITE_OK);.}.
1c584 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1c585 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69  NDEBUG./*.** Thi
1c586 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  s function is on
1c587 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  ly called from w
1c588 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28  ithin an assert(
1c589 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74  ) expression. It
1c58a 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20  .** checks that 
1c58b 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61  the sqlite3.nTra
1c58c 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c  nsaction variabl
1c58d 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73  e is correctly s
1c58e 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d  et to.** the num
1c58f 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73  ber of non-trans
1c590 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
1c591 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  s currently in t
1c592 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69  he .** linked li
1c593 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 73  st starting at s
1c594 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e  qlite3.pSavepoin
1c595 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a  t..** .** Usage:
1c596 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72  .**.**     asser
1c597 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e  t( checkSavepoin
1c598 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f  tCount(db) );.*/
1c599 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
1c59a 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28  kSavepointCount(
1c59b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
1c59c 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76  int n = 0;.  Sav
1c59d 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72  epoint *p;.  for
1c59e 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  (p=db->pSavepoin
1c59f 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  t; p; p=p->pNext
1c5a0 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28  ) n++;.  assert(
1c5a1 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f   n==(db->nSavepo
1c5a2 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e  int + db->isTran
1c5a3 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74  sactionSavepoint
1c5a4 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ) );.  return 1;
1c5a5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1c5a6 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 68   Execute as much
1c5a7 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 72   of a VDBE progr
1c5a8 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 65  am as we can the
1c5a9 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20  n return..**.** 
1c5aa 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
1c5ab 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20 63  eady() must be c
1c5ac 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69  alled before thi
1c5ad 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64  s routine in ord
1c5ae 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74  er to.** close t
1c5af 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20  he program with 
1c5b0 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20  a final OP_Halt 
1c5b1 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 68  and to set up th
1c5b2 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61  e callbacks.** a
1c5b3 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  nd the error mes
1c5b4 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  sage pointer..**
1c5b5 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72  .** Whenever a r
1c5b6 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74  ow or result dat
1c5b7 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  a is available, 
1c5b8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1c5b9 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f  l either.** invo
1c5ba 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63 61  ke the result ca
1c5bb 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65  llback (if there
1c5bc 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75   is one) or retu
1c5bd 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54  rn with.** SQLIT
1c5be 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  E_ROW..**.** If 
1c5bf 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  an attempt is ma
1c5c0 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63  de to open a loc
1c5c1 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68  ked database, th
1c5c2 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
1c5c3 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69  ** will either i
1c5c4 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63  nvoke the busy c
1c5c5 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72  allback (if ther
1c5c6 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20  e is one) or it 
1c5c7 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53  will.** return S
1c5c8 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a  QLITE_BUSY..**.*
1c5c9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
1c5ca 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d  curs, an error m
1c5cb 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65  essage is writte
1c5cc 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  n to memory obta
1c5cd 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  ined.** from sql
1c5ce 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e  ite3_malloc() an
1c5cf 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20  d p->zErrMsg is 
1c5d0 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
1c5d1 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a   that memory..**
1c5d2 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   The error code 
1c5d3 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e  is stored in p->
1c5d4 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  rc and this rout
1c5d5 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
1c5d6 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
1c5d7 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  If the callback 
1c5d8 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e  ever returns non
1c5d9 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
1c5da 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a  program exits.**
1c5db 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54   immediately.  T
1c5dc 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20  here will be no 
1c5dd 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75  error message bu
1c5de 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c  t the p->rc fiel
1c5df 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53  d is.** set to S
1c5e0 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20  QLITE_ABORT and 
1c5e1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1c5e2 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  l return SQLITE_
1c5e3 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d  ERROR..**.** A m
1c5e4 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
1c5e5 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d   error causes p-
1c5e6 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f  >rc to be set to
1c5e7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e   SQLITE_NOMEM an
1c5e8 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  d this.** routin
1c5e9 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49  e to return SQLI
1c5ea 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
1c5eb 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f  Other fatal erro
1c5ec 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  rs return SQLITE
1c5ed 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66  _ERROR..**.** Af
1c5ee 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
1c5ef 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73   has finished, s
1c5f0 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
1c5f1 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a  ze() should be.*
1c5f2 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20  * used to clean 
1c5f3 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61 74  up the mess that
1c5f4 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64   was left behind
1c5f5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1c5f6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1c5f7 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65 20  dbeExec(.  Vdbe 
1c5f8 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *p              
1c5f9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
1c5fa 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63  E */.){.  int pc
1c5fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c5fc 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67       /* The prog
1c5fd 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ram counter */. 
1c5fe 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f   Op *aOp = p->aO
1c5ff 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  p;          /* C
1c600 6f 70 79 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f  opy of p->aOp */
1c601 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20  .  Op *pOp;     
1c602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c603 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69   Current operati
1c604 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  on */.  int rc =
1c605 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
1c606 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
1c607 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  eturn */.  sqlit
1c608 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20  e3 *db = p->db; 
1c609 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1c60a 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65  abase */.  u8 re
1c60b 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74  setSchemaOnFault
1c60c 20 3d 20 30 3b 20 2f 2a 20 52 65 73 65 74 20 73   = 0; /* Reset s
1c60d 63 68 65 6d 61 20 61 66 74 65 72 20 61 6e 20 65  chema after an e
1c60e 72 72 6f 72 20 69 66 20 74 72 75 65 20 2a 2f 0a  rror if true */.
1c60f 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20    u8 encoding = 
1c610 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20  ENC(db);     /* 
1c611 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
1c612 6f 64 69 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66  oding */.#ifndef
1c613 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
1c614 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20  GRESS_CALLBACK. 
1c615 20 69 6e 74 20 63 68 65 63 6b 50 72 6f 67 72 65   int checkProgre
1c616 73 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ss;         /* T
1c617 72 75 65 20 69 66 20 70 72 6f 67 72 65 73 73 20  rue if progress 
1c618 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 65 6e  callbacks are en
1c619 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  abled */.  int n
1c61a 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
1c61b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73        /* Opcodes
1c61c 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20   executed since 
1c61d 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
1c61e 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d  k. */.#endif.  M
1c61f 65 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d  em *aMem = p->aM
1c620 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70  em;       /* Cop
1c621 79 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a  y of p->aMem */.
1c622 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b    Mem *pIn1 = 0;
1c623 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c624 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e  1st input operan
1c625 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32  d */.  Mem *pIn2
1c626 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1c627 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f    /* 2nd input o
1c628 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20  perand */.  Mem 
1c629 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20  *pIn3 = 0;      
1c62a 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e         /* 3rd in
1c62b 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
1c62c 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20   Mem *pOut = 0; 
1c62d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1c62e 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f  utput operand */
1c62f 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20  .  int iCompare 
1c630 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
1c631 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20   Result of last 
1c632 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61  OP_Compare opera
1c633 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tion */.  int *a
1c634 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20  Permute = 0;    
1c635 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74       /* Permutat
1c636 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66  ion of columns f
1c637 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f  or OP_Compare */
1c638 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
1c639 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74  FILE.  u64 start
1c63a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c63b 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63    /* CPU clock c
1c63c 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66  ount at start of
1c63d 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74   opcode */.  int
1c63e 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20   origPc;        
1c63f 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
1c640 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74  am counter at st
1c641 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f  art of opcode */
1c642 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a  .#endif.  /*****
1c643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1c647 20 20 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c    ** Automatical
1c648 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  ly generated cod
1c649 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  e.  **.  ** The 
1c64a 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20  following union 
1c64b 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
1c64c 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
1c64d 65 0a 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70  e.  ** vdbe-comp
1c64e 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e  ress.tcl script.
1c64f 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66    The purpose of
1c650 20 74 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74   this union is t
1c651 6f 0a 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68  o.  ** reduce th
1c652 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63  e amount of stac
1c653 6b 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  k space required
1c654 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1c655 6e 2e 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d  n..  ** See comm
1c656 65 6e 74 73 20 69 6e 20 74 68 65 20 76 64 62 65  ents in the vdbe
1c657 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63  -compress.tcl sc
1c658 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73  ript for details
1c659 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76  ..  */.  union v
1c65a 64 62 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20  dbeExecUnion {. 
1c65b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65     struct OP_Yie
1c65c 6c 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ld_stack_vars {.
1c65d 20 20 20 20 20 20 69 6e 74 20 70 63 44 65 73 74        int pcDest
1c65e 3b 0a 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20  ;.    } aa;.    
1c65f 73 74 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62  struct OP_Variab
1c660 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  le_stack_vars {.
1c661 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20        int p1;   
1c662 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62         /* Variab
1c663 6c 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20  le to copy from 
1c664 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b  */.      int p2;
1c665 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c666 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f  ister to copy to
1c667 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b   */.      int n;
1c668 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c669 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c  mber of values l
1c66a 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1c66b 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20       Mem *pVar; 
1c66c 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62        /* Value b
1c66d 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64  eing transferred
1c66e 20 2a 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20   */.    } ab;.  
1c66f 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65    struct OP_Move
1c670 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c671 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f      char *zMallo
1c672 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20  c;   /* Holding 
1c673 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c  variable for all
1c674 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  ocated memory */
1c675 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20  .      int n;   
1c676 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1c677 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c  r of registers l
1c678 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
1c679 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20       int p1;    
1c67a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c67b 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a  r to copy from *
1c67c 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20  /.      int p2; 
1c67d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
1c67e 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20  ster to copy to 
1c67f 2a 2f 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20  */.    } ac;.   
1c680 20 73 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c   struct OP_Resul
1c681 74 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20  tRow_stack_vars 
1c682 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65  {.      Mem *pMe
1c683 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  m;.      int i;.
1c684 20 20 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74      } ad;.    st
1c685 72 75 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73  ruct OP_Concat_s
1c686 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c687 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20    i64 nByte;.   
1c688 20 7d 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63   } ae;.    struc
1c689 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73  t OP_Remainder_s
1c68a 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c68b 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
1c68c 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45    /* Combined ME
1c68d 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62  M_* flags from b
1c68e 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20  oth inputs */.  
1c68f 20 20 20 20 69 36 34 20 69 41 3b 20 20 20 20 20      i64 iA;     
1c690 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
1c691 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65  alue of left ope
1c692 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36  rand */.      i6
1c693 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  4 iB;         /*
1c694 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f   Integer value o
1c695 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  f right operand 
1c696 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  */.      double 
1c697 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c  rA;      /* Real
1c698 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f   value of left o
1c699 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20  perand */.      
1c69a 64 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20  double rB;      
1c69b 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66  /* Real value of
1c69c 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
1c69d 2f 0a 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20  /.    } af;.    
1c69e 73 74 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69  struct OP_Functi
1c69f 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  on_stack_vars {.
1c6a0 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1c6a1 20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20     Mem *pArg;.  
1c6a2 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
1c6a3 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73  ext ctx;.      s
1c6a4 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
1c6a5 70 56 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20  pVal;.      int 
1c6a6 6e 3b 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20  n;.    } ag;.   
1c6a7 20 73 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74   struct OP_Shift
1c6a8 52 69 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73  Right_stack_vars
1c6a9 20 7b 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a   {.      i64 a;.
1c6aa 20 20 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20        i64 b;.   
1c6ab 20 7d 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63   } ah;.    struc
1c6ac 74 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61  t OP_Ge_stack_va
1c6ad 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  rs {.      int r
1c6ae 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  es;            /
1c6af 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
1c6b0 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49  comparison of pI
1c6b1 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20  n1 against pIn3 
1c6b2 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66  */.      char af
1c6b3 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20  finity;      /* 
1c6b4 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20  Affinity to use 
1c6b5 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a  for comparison *
1c6b6 2f 0a 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20  /.    } ai;.    
1c6b7 73 74 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72  struct OP_Compar
1c6b8 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  e_stack_vars {. 
1c6b9 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
1c6ba 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69    int i;.      i
1c6bb 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74  nt p1;.      int
1c6bc 20 70 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74   p2;.      const
1c6bd 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1c6be 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  fo;.      int id
1c6bf 78 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  x;.      CollSeq
1c6c0 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
1c6c1 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1c6c2 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
1c6c3 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69   term */.      i
1c6c4 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20  nt bRev;        
1c6c5 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45    /* True for DE
1c6c6 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72  SCENDING sort or
1c6c7 64 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b  der */.    } aj;
1c6c8 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f  .    struct OP_O
1c6c9 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  r_stack_vars {. 
1c6ca 20 20 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20       int v1;    
1c6cb 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a  /* Left operand:
1c6cc 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54    0==FALSE, 1==T
1c6cd 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20  RUE, 2==UNKNOWN 
1c6ce 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  or NULL */.     
1c6cf 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52   int v2;    /* R
1c6d0 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d  ight operand: 0=
1c6d1 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c  =FALSE, 1==TRUE,
1c6d2 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e   2==UNKNOWN or N
1c6d3 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b  ULL */.    } ak;
1c6d4 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c6d5 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  fNot_stack_vars 
1c6d6 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20  {.      int c;. 
1c6d7 20 20 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72     } al;.    str
1c6d8 75 63 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74  uct OP_Column_st
1c6d9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6da 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65   u32 payloadSize
1c6db 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
1c6dc 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65   bytes in the re
1c6dd 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36  cord */.      i6
1c6de 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b  4 payloadSize64;
1c6df 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c6e0 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1c6e1 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70  d */.      int p
1c6e2 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1;            /*
1c6e3 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65   P1 value of the
1c6e4 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20   opcode */.     
1c6e5 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
1c6e6 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75      /* column nu
1c6e7 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65  mber to retrieve
1c6e8 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75   */.      VdbeCu
1c6e9 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20  rsor *pC;    /* 
1c6ea 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
1c6eb 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  */.      char *z
1c6ec 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
1c6ed 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
1c6ee 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
1c6ef 2f 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  /.      BtCursor
1c6f0 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68   *pCrsr;   /* Th
1c6f1 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a  e BTree cursor *
1c6f2 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 54 79  /.      u32 *aTy
1c6f3 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54  pe;        /* aT
1c6f4 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65  ype[i] holds the
1c6f5 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66   numeric type of
1c6f6 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e   the i-th column
1c6f7 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61   */.      u32 *a
1c6f8 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
1c6f9 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66  aOffset[i] is of
1c6fa 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66  fset to start of
1c6fb 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63   data for i-th c
1c6fc 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69  olumn */.      i
1c6fd 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1c6fe 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66    /* number of f
1c6ff 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
1c700 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ord */.      int
1c701 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20   len;           
1c702 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  /* The length of
1c703 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
1c704 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c  data for the col
1c705 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  umn */.      int
1c706 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1c707 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1c708 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  */.      char *z
1c709 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50  Data;       /* P
1c70a 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72  art of the recor
1c70b 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20  d being decoded 
1c70c 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44  */.      Mem *pD
1c70d 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  est;        /* W
1c70e 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
1c70f 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75  e extracted valu
1c710 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73  e */.      Mem s
1c711 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
1c712 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   For storing the
1c713 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65   record being de
1c714 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75  coded */.      u
1c715 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20  8 *zIdx;        
1c716 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20    /* Index into 
1c717 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20  header */.      
1c718 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20  u8 *zEndHdr;    
1c719 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1c71a 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
1c71b 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  r the header */.
1c71c 20 20 20 20 20 20 75 33 32 20 6f 66 66 73 65 74        u32 offset
1c71d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  ;        /* Offs
1c71e 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  et into the data
1c71f 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66   */.      u64 of
1c720 66 73 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20  fset64;      /* 
1c721 36 34 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20  64-bit offset.  
1c722 36 34 20 62 69 74 73 20 6e 65 65 64 65 64 20 74  64 bits needed t
1c723 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77  o catch overflow
1c724 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a   */.      int sz
1c725 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Hdr;         /* 
1c726 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64  Size of the head
1c727 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74  er size field at
1c728 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64   start of record
1c729 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76   */.      int av
1c72a 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ail;         /* 
1c72b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c72c 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74  of available dat
1c72d 61 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  a */.      Mem *
1c72e 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  pReg;         /*
1c72f 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70   PseudoTable inp
1c730 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  ut register */. 
1c731 20 20 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72     } am;.    str
1c732 75 63 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f  uct OP_Affinity_
1c733 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c734 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c735 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54  Affinity;   /* T
1c736 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62  he affinity to b
1c737 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20  e applied */.   
1c738 20 20 20 63 68 61 72 20 63 41 66 66 3b 20 20 20     char cAff;   
1c739 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1c73a 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
1c73b 72 20 6f 66 20 61 66 66 69 6e 69 74 79 20 2a 2f  r of affinity */
1c73c 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73  .    } an;.    s
1c73d 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63  truct OP_MakeRec
1c73e 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ord_stack_vars {
1c73f 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52  .      u8 *zNewR
1c740 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a  ecord;        /*
1c741 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
1c742 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  d the data for t
1c743 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63  .      Mem *pRec
1c745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c746 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   The new record 
1c747 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61  */.      u64 nDa
1c748 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
1c749 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c74a 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65  es of data space
1c74b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   */.      int nH
1c74c 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
1c74d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c74e 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70  tes of header sp
1c74f 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34  ace */.      i64
1c750 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
1c751 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
1c752 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1c753 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  his record */.  
1c754 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20      int nZero;  
1c755 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c756 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74  mber of zero byt
1c757 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  es at the end of
1c758 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c759 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74       int nVarint
1c75a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1c75b 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1c75c 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20  n a varint */.  
1c75d 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74      u32 serial_t
1c75e 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79  ype;       /* Ty
1c75f 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20  pe field */.    
1c760 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20    Mem *pData0;  
1c761 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
1c762 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f  t field to be co
1c763 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20  mbined into the 
1c764 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
1c765 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20  Mem *pLast;     
1c766 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66         /* Last f
1c767 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f  ield of the reco
1c768 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
1c769 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20  nField;         
1c76a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c76b 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65  fields in the re
1c76c 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68  cord */.      ch
1c76d 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20  ar *zAffinity;  
1c76e 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69       /* The affi
1c76f 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
1c770 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c771 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72      int file_for
1c772 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  mat;       /* Fi
1c773 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65  le format to use
1c774 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f   for encoding */
1c775 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
1c776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c777 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a   Space used in z
1c778 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20  NewRecord[] */. 
1c779 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20       int len;   
1c77a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c77b 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64  ength of a field
1c77c 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20   */.    } ao;.  
1c77d 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e    struct OP_Coun
1c77e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c77f 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b       i64 nEntry;
1c780 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c781 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70  *pCrsr;.    } ap
1c782 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c783 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f  Savepoint_stack_
1c784 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c785 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   p1;            
1c786 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c787 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72  Value of P1 oper
1c788 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  and */.      cha
1c789 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
1c78a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c78b 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e  Name of savepoin
1c78c 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
1c78d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65  Name;.      Save
1c78e 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20  point *pNew;.   
1c78f 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53     Savepoint *pS
1c790 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1c791 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b  Savepoint *pTmp;
1c792 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65  .      int iSave
1c793 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74  point;.      int
1c794 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20   ii;.    } aq;. 
1c795 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74     struct OP_Aut
1c796 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61  oCommit_stack_va
1c797 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  rs {.      int d
1c798 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1c799 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c  ;.      int iRol
1c79a 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74  lback;.      int
1c79b 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d   turnOnAC;.    }
1c79c 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ar;.    struct 
1c79d 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73  OP_Transaction_s
1c79e 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c79f 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20    Btree *pBt;.  
1c7a0 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75    } as;.    stru
1c7a1 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  ct OP_ReadCookie
1c7a2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c7a3 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20      int iMeta;. 
1c7a4 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
1c7a5 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b      int iCookie;
1c7a6 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73  .    } at;.    s
1c7a7 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b  truct OP_SetCook
1c7a8 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ie_stack_vars {.
1c7a9 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20        Db *pDb;. 
1c7aa 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72     } au;.    str
1c7ab 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f  uct OP_VerifyCoo
1c7ac 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  kie_stack_vars {
1c7ad 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61  .      int iMeta
1c7ae 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ;.      Btree *p
1c7af 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20  Bt;.    } av;.  
1c7b0 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e    struct OP_Open
1c7b1 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73  Write_stack_vars
1c7b2 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69   {.      int nFi
1c7b3 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e  eld;.      KeyIn
1c7b4 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
1c7b5 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20      int p2;.    
1c7b6 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1c7b7 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20   int wrFlag;.   
1c7b8 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20     Btree *pX;.  
1c7b9 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c7ba 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a  pCur;.      Db *
1c7bb 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20  pDb;.    } aw;. 
1c7bc 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65     struct OP_Ope
1c7bd 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b  nEphemeral_stack
1c7be 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c7bf 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1c7c0 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72     } ax;.    str
1c7c1 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  uct OP_OpenPseud
1c7c2 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  o_stack_vars {. 
1c7c3 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c7c4 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a  *pCx;.    } ay;.
1c7c5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c7c6 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ekGt_stack_vars 
1c7c7 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  {.      int res;
1c7c8 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20  .      int oc;. 
1c7c9 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c7ca 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63  *pC;.      Unpac
1c7cb 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
1c7cc 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20     int nField;. 
1c7cd 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20       i64 iKey;  
1c7ce 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
1c7cf 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20   we are to seek 
1c7d0 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a  to */.    } az;.
1c7d1 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c7d2 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ek_stack_vars {.
1c7d3 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c7d4 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a   *pC;.    } ba;.
1c7d5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f      struct OP_Fo
1c7d6 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  und_stack_vars {
1c7d7 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61  .      int alrea
1c7d8 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20  dyExists;.      
1c7d9 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c7da 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c7db 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63       UnpackedRec
1c7dc 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20  ord *pIdxKey;.  
1c7dd 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
1c7de 72 64 20 72 3b 0a 20 20 20 20 20 20 63 68 61 72  rd r;.      char
1c7df 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38   aTempRec[ROUND8
1c7e0 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64  (sizeof(Unpacked
1c7e1 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f  Record)) + sizeo
1c7e2 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20  f(Mem)*3 + 7];. 
1c7e3 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 73 74 72     } bb;.    str
1c7e4 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f  uct OP_IsUnique_
1c7e5 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c7e6 20 20 20 75 31 36 20 69 69 3b 0a 20 20 20 20 20     u16 ii;.     
1c7e7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c7e8 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c7e9 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75   *pCrsr;.      u
1c7ea 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20  16 nField;.     
1c7eb 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20   Mem *aMx;.     
1c7ec 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c7ed 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1c7ee 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e      /* B-Tree in
1c7ef 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a  dex search key *
1c7f0 2f 0a 20 20 20 20 20 20 69 36 34 20 52 3b 20 20  /.      i64 R;  
1c7f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c7f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
1c7f3 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65  wid stored in re
1c7f4 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20  gister P3 */.   
1c7f5 20 7d 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63   } bc;.    struc
1c7f6 74 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73  t OP_NotExists_s
1c7f7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c7f8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c7f9 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c7fa 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
1c7fb 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36  nt res;.      u6
1c7fc 34 20 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64  4 iKey;.    } bd
1c7fd 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c7fe 4e 65 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76  NewRowid_stack_v
1c7ff 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20  ars {.      i64 
1c800 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
1c801 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
1c802 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62  wid */.      Vdb
1c803 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20  eCursor *pC;    
1c804 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66      /* Cursor of
1c805 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68   table to get th
1c806 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20  e new rowid */. 
1c807 20 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20       int res;   
1c808 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1c809 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69  esult of an sqli
1c80a 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a  te3BtreeLast() *
1c80b 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b  /.      int cnt;
1c80c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c80d 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d  * Counter to lim
1c80e 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  it the number of
1c80f 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20   searches */.   
1c810 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20     Mem *pMem;   
1c811 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1c812 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61  ister holding la
1c813 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20  rgest rowid for 
1c814 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
1c815 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65  .      VdbeFrame
1c816 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a   *pFrame;     /*
1c817 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56   Root frame of V
1c818 44 42 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b  DBE */.    } be;
1c819 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c81a 6e 73 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76  nsertInt_stack_v
1c81b 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c81c 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a  *pData;       /*
1c81d 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e   MEM cell holdin
1c81e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  g data for the r
1c81f 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65  ecord to be inse
1c820 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65  rted */.      Me
1c821 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20  m *pKey;        
1c822 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64  /* MEM cell hold
1c823 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65  ing key  for the
1c824 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c825 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
1c826 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65     /* The intege
1c827 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66  r ROWID or key f
1c828 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1c829 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1c82a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c82b 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f   *pC;   /* Curso
1c82c 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20  r to table into 
1c82d 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20  which insert is 
1c82e 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20  written */.     
1c82f 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20   int nZero;     
1c830 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c831 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70  zero-bytes to ap
1c832 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  pend */.      in
1c833 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20  t seekResult;   
1c834 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69  /* Result of pri
1c835 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20  or seek or 0 if 
1c836 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54  no USESEEKRESULT
1c837 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63   flag */.      c
1c838 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
1c839 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d   /* database nam
1c83a 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20  e - used by the 
1c83b 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20  update hook */. 
1c83c 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1c83d 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20  *zTbl; /* Table 
1c83e 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74  name - used by t
1c83f 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a  he opdate hook *
1c840 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20  /.      int op; 
1c841 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
1c842 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68  ode for update h
1c843 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41  ook: SQLITE_UPDA
1c844 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53  TE or SQLITE_INS
1c845 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b  ERT */.    } bf;
1c846 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44  .    struct OP_D
1c847 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73  elete_stack_vars
1c848 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65   {.      i64 iKe
1c849 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  y;.      VdbeCur
1c84a 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62  sor *pC;.    } b
1c84b 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  g;.    struct OP
1c84c 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76  _RowData_stack_v
1c84d 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c84e 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
1c84f 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c850 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a  r;.      u32 n;.
1c851 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20        i64 n64;. 
1c852 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72     } bh;.    str
1c853 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61  uct OP_Rowid_sta
1c854 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c855 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c856 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20        i64 v;.   
1c857 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
1c858 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f  *pVtab;.      co
1c859 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1c85a 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  le *pModule;.   
1c85b 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63   } bi;.    struc
1c85c 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61  t OP_NullRow_sta
1c85d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c85e 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c85f 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74      } bj;.    st
1c860 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61  ruct OP_Last_sta
1c861 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c862 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c863 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
1c864 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74  pCrsr;.      int
1c865 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a   res;.    } bk;.
1c866 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65      struct OP_Re
1c867 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  wind_stack_vars 
1c868 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c869 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74  or *pC;.      Bt
1c86a 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1c86b 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
1c86c 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75    } bl;.    stru
1c86d 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b  ct OP_Next_stack
1c86e 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c86f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c870 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c871 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  rsr;.      int r
1c872 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20  es;.    } bm;.  
1c873 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49    struct OP_IdxI
1c874 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73  nsert_stack_vars
1c875 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c876 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c877 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c878 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a        int nKey;.
1c879 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1c87a 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e   *zKey;.    } bn
1c87b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c87c 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f  IdxDelete_stack_
1c87d 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c87e 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c87f 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c880 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  sr;.      int re
1c881 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65  s;.      Unpacke
1c882 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d  dRecord r;.    }
1c883 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bo;.    struct 
1c884 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63  OP_IdxRowid_stac
1c885 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42  k_vars {.      B
1c886 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c887 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c888 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20   *pC;.      i64 
1c889 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b  rowid;.    } bp;
1c88a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c88b 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20  dxGE_stack_vars 
1c88c 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c88d 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e  or *pC;.      in
1c88e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70  t res;.      Unp
1c88f 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20  ackedRecord r;. 
1c890 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72     } bq;.    str
1c891 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73  uct OP_Destroy_s
1c892 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c893 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20    int iMoved;.  
1c894 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20      int iCnt;.  
1c895 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b      Vdbe *pVdbe;
1c896 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a  .      int iDb;.
1c897 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74      } br;.    st
1c898 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74  ruct OP_Clear_st
1c899 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c89a 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20   int nChange;.  
1c89b 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75    } bs;.    stru
1c89c 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  ct OP_CreateTabl
1c89d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  e_stack_vars {. 
1c89e 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20       int pgno;. 
1c89f 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a       int flags;.
1c8a0 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20        Db *pDb;. 
1c8a1 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72     } bt;.    str
1c8a2 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65  uct OP_ParseSche
1c8a3 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ma_stack_vars {.
1c8a4 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20        int iDb;. 
1c8a5 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1c8a6 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20  *zMaster;.      
1c8a7 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
1c8a8 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44    InitData initD
1c8a9 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20  ata;.    } bu;. 
1c8aa 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74     struct OP_Int
1c8ab 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76  egrityCk_stack_v
1c8ac 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c8ad 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e  nRoot;      /* N
1c8ae 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
1c8af 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62  to check.  (Numb
1c8b0 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  er of root pages
1c8b1 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  .) */.      int 
1c8b2 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41  *aRoot;     /* A
1c8b3 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65  rray of rootpage
1c8b4 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62   numbers for tab
1c8b5 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65  les to be checke
1c8b6 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  d */.      int j
1c8b7 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ;          /* Lo
1c8b8 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1c8b9 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20      int nErr;   
1c8ba 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c8bb 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64   errors reported
1c8bc 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a   */.      char *
1c8bd 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  z;        /* Tex
1c8be 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72  t of the error r
1c8bf 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d  eport */.      M
1c8c0 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f  em *pnErr;     /
1c8c1 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69  * Register keepi
1c8c2 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f  ng track of erro
1c8c3 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a  rs remaining */.
1c8c4 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74      } bv;.    st
1c8c5 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65  ruct OP_RowSetRe
1c8c6 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ad_stack_vars {.
1c8c7 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20        i64 val;. 
1c8c8 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72     } bw;.    str
1c8c9 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73  uct OP_RowSetTes
1c8ca 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c8cb 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20       int iSet;. 
1c8cc 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b       int exists;
1c8cd 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73  .    } bx;.    s
1c8ce 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d  truct OP_Program
1c8cf 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c8d0 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20      int nMem;   
1c8d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1c8d2 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
1c8d3 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75  registers for su
1c8d4 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20  b-program */.   
1c8d5 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20     int nByte;   
1c8d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1c8d7 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73  tes of runtime s
1c8d8 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f  pace required fo
1c8d9 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
1c8da 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b  .      Mem *pRt;
1c8db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c8dc 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c  * Register to al
1c8dd 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73  locate runtime s
1c8de 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65  pace */.      Me
1c8df 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
1c8e0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
1c8e1 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
1c8e2 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f   memory cells */
1c8e3 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64  .      Mem *pEnd
1c8e4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1c8e5 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65  * Last memory ce
1c8e6 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20  ll in new array 
1c8e7 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61  */.      VdbeFra
1c8e8 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
1c8e9 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61   /* New vdbe fra
1c8ea 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e  me to execute in
1c8eb 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f   */.      SubPro
1c8ec 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
1c8ed 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d    /* Sub-program
1c8ee 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20   to execute */. 
1c8ef 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20       void *t;   
1c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c8f1 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e  Token identifyin
1c8f2 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20  g trigger */.   
1c8f3 20 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63   } by;.    struc
1c8f4 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b  t OP_Param_stack
1c8f5 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c8f6 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1c8f7 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b  .      Mem *pIn;
1c8f8 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73  .    } bz;.    s
1c8f9 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f  truct OP_MemMax_
1c8fa 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c8fb 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20     Mem *pIn1;.  
1c8fc 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c8fd 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b  Frame;.    } ca;
1c8fe 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41  .    struct OP_A
1c8ff 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72  ggStep_stack_var
1c900 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b  s {.      int n;
1c901 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1c902 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20      Mem *pMem;. 
1c903 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a       Mem *pRec;.
1c904 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
1c905 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20  ntext ctx;.     
1c906 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1c907 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62  *apVal;.    } cb
1c908 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c909 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76  AggFinal_stack_v
1c90a 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c90b 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b  *pMem;.    } cc;
1c90c 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c90d 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f  ncrVacuum_stack_
1c90e 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72  vars {.      Btr
1c90f 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63  ee *pBt;.    } c
1c910 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  d;.    struct OP
1c911 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61  _VBegin_stack_va
1c912 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c  rs {.      VTabl
1c913 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20  e *pVTab;.    } 
1c914 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ce;.    struct O
1c915 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61  P_VOpen_stack_va
1c916 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c917 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20  ursor *pCur;.   
1c918 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f     sqlite3_vtab_
1c919 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1c91a 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  sor;.      sqlit
1c91b 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c91c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f        sqlite3_mo
1c91d 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c91e 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72     } cf;.    str
1c91f 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73  uct OP_VFilter_s
1c920 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c921 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
1c922 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20    int iQuery;.  
1c923 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
1c924 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1c925 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51  e;.      Mem *pQ
1c926 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20  uery;.      Mem 
1c927 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71  *pArgc;.      sq
1c928 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1c929 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1c92a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c92b 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c92c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c92d 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
1c92e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  ;.      int i;. 
1c92f 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67       Mem **apArg
1c930 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20  ;.    } cg;.    
1c931 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d  struct OP_VColum
1c932 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  n_stack_vars {. 
1c933 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
1c934 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20  b *pVtab;.      
1c935 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1c936 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c937 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b       Mem *pDest;
1c938 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
1c939 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
1c93a 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20 73  .    } ch;.    s
1c93b 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73  truct OP_VNext_s
1c93c 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c93d 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1c93e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e  pVtab;.      con
1c93f 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1c940 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  e *pModule;.    
1c941 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
1c942 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c943 72 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20  r;.    } ci;.   
1c944 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61   struct OP_VRena
1c945 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  me_stack_vars {.
1c946 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c947 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c948 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20   Mem *pName;.   
1c949 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 63   } cj;.    struc
1c94a 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61  t OP_VUpdate_sta
1c94b 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c94c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c94d 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  tab;.      sqlit
1c94e 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1c94f 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41  le;.      int nA
1c950 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  rg;.      int i;
1c951 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  .      sqlite_in
1c952 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20  t64 rowid;.     
1c953 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20   Mem **apArg;.  
1c954 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20      Mem *pX;.   
1c955 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63   } ck;.    struc
1c956 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73  t OP_Pagecount_s
1c957 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c958 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20    int p1;.      
1c959 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20  int nPage;.     
1c95a 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
1c95b 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74      } cl;.    st
1c95c 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74  ruct OP_Trace_st
1c95d 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c95e 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20   char *zTrace;. 
1c95f 20 20 20 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a     } cm;.  } u;.
1c960 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74    /* End automat
1c961 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
1c962 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a   code.  ********
1c963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c964 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20  ************/.. 
1c967 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1c968 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1c969 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33  N );  /* sqlite3
1c96a 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73  _step() verifies
1c96b 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72   this */.  asser
1c96c 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  t( db->magic==SQ
1c96d 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
1c96e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1c96f 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
1c970 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d  p);.  if( p->rc=
1c971 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
1c972 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1c973 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63  pens if a malloc
1c974 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c  () inside a call
1c975 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
1c976 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20  mn_text() or.   
1c977 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
1c978 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c  mn_text16() fail
1c979 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f  ed.  */.    goto
1c97a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61   no_mem;.  }.  a
1c97b 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51  ssert( p->rc==SQ
1c97c 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1c97d 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b  ==SQLITE_BUSY );
1c97e 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1c97f 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1c980 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b  p->explain==0 );
1c981 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74  .  p->pResultSet
1c982 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79   = 0;.  db->busy
1c983 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
1c984 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  0;.  CHECK_FOR_I
1c985 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69  NTERRUPT;.  sqli
1c986 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
1c987 6c 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  l(p);.#ifndef SQ
1c988 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
1c989 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68  SS_CALLBACK.  ch
1c98a 65 63 6b 50 72 6f 67 72 65 73 73 20 3d 20 64 62  eckProgress = db
1c98b 2d 3e 78 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a  ->xProgress!=0;.
1c98c 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
1c98d 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c  LITE_DEBUG.  sql
1c98e 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
1c98f 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70  alloc();.  if( p
1c990 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28  ->pc==0 .   && (
1c991 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  (p->db->flags & 
1c992 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
1c993 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74  ng) || fileExist
1c994 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c  s(db, "vdbe_expl
1c995 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20  ain")).  ){.    
1c996 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74  int i;.    print
1c997 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20  f("VDBE Program 
1c998 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20  Listing:\n");.  
1c999 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c99a 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f  ntSql(p);.    fo
1c99b 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
1c99c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
1c99d 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
1c99e 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b  stdout, i, &aOp[
1c99f 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i]);.    }.  }. 
1c9a0 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28   if( fileExists(
1c9a1 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22  db, "vdbe_trace"
1c9a2 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63  ) ){.    p->trac
1c9a3 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a  e = stdout;.  }.
1c9a4 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
1c9a5 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64  gnMalloc();.#end
1c9a6 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70  if.  for(pc=p->p
1c9a7 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  c; rc==SQLITE_OK
1c9a8 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73  ; pc++){.    ass
1c9a9 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63  ert( pc>=0 && pc
1c9aa 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69  <p->nOp );.    i
1c9ab 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1c9ac 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  led ) goto no_me
1c9ad 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  m;.#ifdef VDBE_P
1c9ae 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50  ROFILE.    origP
1c9af 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72  c = pc;.    star
1c9b0 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t = sqlite3Hwtim
1c9b1 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  e();.#endif.    
1c9b2 70 4f 70 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a  pOp = &aOp[pc];.
1c9b3 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c  .    /* Only all
1c9b4 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51  ow tracing if SQ
1c9b5 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
1c9b6 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
1c9b7 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1c9b8 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
1c9b9 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
1c9ba 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pc==0 ){.       
1c9bb 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78   printf("VDBE Ex
1c9bc 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e  ecution Trace:\n
1c9bd 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
1c9be 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
1c9bf 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
1c9c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c9c1 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70  ntOp(p->trace, p
1c9c2 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20  c, pOp);.    }. 
1c9c3 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d     if( p->trace=
1c9c4 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20  =0 && pc==0 ){. 
1c9c5 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
1c9c6 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1c9c7 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45  .      if( fileE
1c9c8 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
1c9c9 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20  sqltrace") ){.  
1c9ca 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c9cb 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20  ePrintSql(p);.  
1c9cc 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1c9cd 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1c9ce 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  oc();.    }.#end
1c9cf 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  if.      ..    /
1c9d0 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1c9d1 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d  f we need to sim
1c9d2 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75  ulate an interru
1c9d3 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68  pt.  This only h
1c9d4 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66  appens.    ** if
1c9d5 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69   we have a speci
1c9d6 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20  al test build.. 
1c9d7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
1c9d8 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28  ITE_TEST.    if(
1c9d9 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1c9da 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20  pt_count>0 ){.  
1c9db 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1c9dc 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20  rrupt_count--;. 
1c9dd 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1c9de 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c9df 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
1c9e0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1c9e1 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
1c9e2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
1c9e3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c9e4 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
1c9e5 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  K.    /* Call th
1c9e6 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c9e7 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ack if it is con
1c9e8 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20  figured and the 
1c9e9 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a  required number.
1c9ea 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f      ** of VDBE o
1c9eb 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65  ps have been exe
1c9ec 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69  cuted (either si
1c9ed 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74  nce this invocat
1c9ee 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71  ion of.    ** sq
1c9ef 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20  lite3VdbeExec() 
1c9f0 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69  or since last ti
1c9f1 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  me the progress 
1c9f2 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c  callback was cal
1c9f3 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20  led)..    ** If 
1c9f4 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c9f5 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c9f6 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65  n-zero, exit the
1c9f7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1c9f8 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72   with.    ** a r
1c9f9 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54  eturn code SQLIT
1c9fa 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a  E_ABORT..    */.
1c9fb 20 20 20 20 69 66 28 20 63 68 65 63 6b 50 72 6f      if( checkPro
1c9fc 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69  gress ){.      i
1c9fd 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73  f( db->nProgress
1c9fe 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70  Ops==nProgressOp
1c9ff 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  s ){.        int
1ca00 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66   prc;.        if
1ca01 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1ca02 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1ca03 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1ca04 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d  e;.        prc =
1ca05 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62  db->xProgress(db
1ca06 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b  ->pProgressArg);
1ca07 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1ca08 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1ca09 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1ca0a 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1ca0b 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20       if( prc!=0 
1ca0c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1ca0d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1ca0e 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
1ca0f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1ca10 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  lt;.        }.  
1ca11 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1ca12 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ps = 0;.      }.
1ca13 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
1ca14 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ps++;.    }.#end
1ca15 69 66 0a 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e  if..    /* On an
1ca16 79 20 6f 70 63 6f 64 65 20 77 69 74 68 20 74 68  y opcode with th
1ca17 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c 61 73  e "out2-prerelas
1ca18 65 22 20 74 61 67 2c 20 66 72 65 65 20 61 6e 79  e" tag, free any
1ca19 0a 20 20 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c  .    ** external
1ca1a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74   allocations out
1ca1b 20 6f 66 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20   of mem[p2] and 
1ca1c 73 65 74 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62  set mem[p2] to b
1ca1d 65 0a 20 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65  e.    ** an unde
1ca1e 66 69 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20  fined integer.  
1ca1f 4f 70 63 6f 64 65 73 20 77 69 6c 6c 20 65 69 74  Opcodes will eit
1ca20 68 65 72 20 66 69 6c 6c 20 69 6e 20 74 68 65 20  her fill in the 
1ca21 69 6e 74 65 67 65 72 0a 20 20 20 20 2a 2a 20 76  integer.    ** v
1ca22 61 6c 75 65 20 6f 72 20 63 6f 6e 76 65 72 74 20  alue or convert 
1ca23 6d 65 6d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66  mem[p2] to a dif
1ca24 66 65 72 65 6e 74 20 74 79 70 65 2e 0a 20 20 20  ferent type..   
1ca25 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
1ca26 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71  pOp->opflags==sq
1ca27 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65  lite3OpcodePrope
1ca28 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d  rty[pOp->opcode]
1ca29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
1ca2a 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47  >opflags & OPFLG
1ca2b 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45  _OUT2_PRERELEASE
1ca2c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1ca2d 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20  ( pOp->p2>0 );. 
1ca2e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1ca2f 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p2<=p->nMem );
1ca30 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 61  .      pOut = &a
1ca31 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1ca32 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1ca33 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1ca34 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70  l(pOut);.      p
1ca35 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1ca36 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _Int;.    }..   
1ca37 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b   /* Sanity check
1ca38 69 6e 67 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65  ing on other ope
1ca39 72 61 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20  rands */.#ifdef 
1ca3a 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1ca3b 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61   if( (pOp->opfla
1ca3c 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21  gs & OPFLG_IN1)!
1ca3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1ca3e 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1ca3f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1ca40 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1ca41 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  );.      REGISTE
1ca42 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1ca43 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29   &aMem[pOp->p1])
1ca44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1ca45 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20  (pOp->opflags & 
1ca46 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b  OPFLG_IN2)!=0 ){
1ca47 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1ca48 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20  Op->p2>0 );.    
1ca49 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ca4a 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
1ca4b 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1ca4c 43 45 28 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65  CE(pOp->p2, &aMe
1ca4d 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20  m[pOp->p2]);.   
1ca4e 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d   }.    if( (pOp-
1ca4f 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47  >opflags & OPFLG
1ca50 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN3)!=0 ){.    
1ca51 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ca52 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  3>0 );.      ass
1ca53 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
1ca54 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 52  >nMem );.      R
1ca55 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1ca56 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70  p->p3, &aMem[pOp
1ca57 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  ->p3]);.    }.  
1ca58 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c    if( (pOp->opfl
1ca59 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32  ags & OPFLG_OUT2
1ca5a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  )!=0 ){.      as
1ca5b 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1ca5c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1ca5d 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65   pOp->p2<=p->nMe
1ca5e 6d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  m );.    }.    i
1ca5f 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  f( (pOp->opflags
1ca60 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d   & OPFLG_OUT3)!=
1ca61 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1ca62 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a  t( pOp->p3>0 );.
1ca63 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1ca64 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1ca65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
1ca66 20 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f   .    switch( pO
1ca67 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a  p->opcode ){../*
1ca68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1ca6d 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20  What follows is 
1ca6e 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63 68  a massive switch
1ca6f 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72 65   statement where
1ca70 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c 65   each case imple
1ca71 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61 72  ments a.** separ
1ca72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ate instruction 
1ca73 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  in the virtual m
1ca74 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20 66  achine.  If we f
1ca75 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c 0a  ollow the usual.
1ca76 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63  ** indentation c
1ca77 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68  onventions, each
1ca78 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20   case should be 
1ca79 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73 70  indented by 6 sp
1ca7a 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68  aces.  But.** th
1ca7b 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20 77  at is a lot of w
1ca7c 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20 74  asted space on t
1ca7d 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20  he left margin. 
1ca7e 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69 74   So the code wit
1ca7f 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74 63  hin.** the switc
1ca80 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  h statement will
1ca81 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e 76   break with conv
1ca82 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c  ention and be fl
1ca83 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65  ush-left. Anothe
1ca84 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74  r.** big comment
1ca85 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69   (similar to thi
1ca86 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b  s one) will mark
1ca87 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74 68   the point in th
1ca88 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20  e code where.** 
1ca89 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 61  we transition ba
1ca8a 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64  ck to normal ind
1ca8b 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1ca8c 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f  The formatting o
1ca8d 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20 69  f each case is i
1ca8e 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d  mportant.  The m
1ca8f 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c 69  akefile for SQLi
1ca90 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 20  te.** generates 
1ca91 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 63  two C files "opc
1ca92 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 63  odes.h" and "opc
1ca93 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e 6e  odes.c" by scann
1ca94 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65  ing this.** file
1ca95 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e   looking for lin
1ca96 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69  es that begin wi
1ca97 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 20  th "case OP_".  
1ca98 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 69  The opcodes.h fi
1ca99 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66  les.** will be f
1ca9a 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 69  illed with #defi
1ca9b 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 6e  nes that give un
1ca9c 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 6c  ique integer val
1ca9d 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f  ues to each.** o
1ca9e 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f 70  pcode and the op
1ca9f 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73 20  codes.c file is 
1caa0 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20 61  filled with an a
1caa1 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73 20  rray of strings 
1caa2 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 74  where.** each st
1caa3 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d 62  ring is the symb
1caa4 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74 68  olic name for th
1caa5 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
1caa6 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65 0a  opcode.  If the.
1caa7 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65 6e  ** case statemen
1caa8 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79  t is followed by
1caa9 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68   a comment of th
1caaa 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20  e form "/# same 
1caab 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68  as ... #/".** th
1caac 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73  at comment is us
1caad 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
1caae 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76  the particular v
1caaf 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f  alue of the opco
1cab0 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20  de..**.** Other 
1cab1 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65 20  keywords in the 
1cab2 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c  comment that fol
1cab3 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20 61  lows each case a
1cab4 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f  re used to.** co
1cab5 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46 4c  nstruct the OPFL
1cab6 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76 61  G_INITIALIZER va
1cab7 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c  lue that initial
1cab8 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70 65  izes opcodePrope
1cab9 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72  rty[]..** Keywor
1caba 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c  ds include: in1,
1cabb 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f   in2, in3, out2_
1cabc 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74 32  prerelease, out2
1cabd 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20  , out3.  See.** 
1cabe 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
1cabf 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64 64  k script for add
1cac0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1cac1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d  ion..**.** Docum
1cac2 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 56  entation about V
1cac3 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 67  DBE opcodes is g
1cac4 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 6e  enerated by scan
1cac5 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a 2a  ning this file.*
1cac6 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74  * for lines of t
1cac7 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63  hat contain "Opc
1cac8 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e  ode:".  That lin
1cac9 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 71  e and all subseq
1caca 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20  uent.** comment 
1cacb 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69  lines are used i
1cacc 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f 6e  n the generation
1cacd 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 68   of the opcode.h
1cace 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  tml documentatio
1cacf 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  n.** file..**.**
1cad0 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20   SUMMARY:.**.** 
1cad1 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 69      Formatting i
1cad2 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73  s important to s
1cad3 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 6e  cripts that scan
1cad4 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20   this file..**  
1cad5 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 74     Do not deviat
1cad6 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61  e from the forma
1cad7 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 72  tting style curr
1cad8 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a  ently in use..**
1cad9 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1cada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cadb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cadc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cadd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1cade 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74  ./* Opcode:  Got
1cadf 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a  o * P2 * * *.**.
1cae0 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f  ** An unconditio
1cae1 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64 72  nal jump to addr
1cae2 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e  ess P2..** The n
1cae3 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1cae4 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62 65  executed will be
1cae5 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74 20   .** the one at 
1cae6 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74 68  index P2 from th
1cae7 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a  e beginning of.*
1cae8 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a  * the program..*
1cae9 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20  /.case OP_Goto: 
1caea 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
1caeb 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b   jump */.  CHECK
1caec 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1caed 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1caee 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   1;.  break;.}..
1caef 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75  /* Opcode:  Gosu
1caf0 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  b P1 P2 * * *.**
1caf1 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75  .** Write the cu
1caf2 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f 6e  rrent address on
1caf3 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a  to register P1.*
1caf4 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20  * and then jump 
1caf5 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a  to address P2..*
1caf6 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a  /.case OP_Gosub:
1caf7 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
1caf8 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1caf9 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1cafa 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1cafb 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1cafc 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20  MEM_Dyn)==0 );. 
1cafd 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d   pIn1->flags = M
1cafe 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e  EM_Int;.  pIn1->
1caff 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49  u.i = pc;.  REGI
1cb00 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1cb01 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20  p1, pIn1);.  pc 
1cb02 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1cb03 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cb04 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31  code:  Return P1
1cb05 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a   * * * *.**.** J
1cb06 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ump to the next 
1cb07 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65  instruction afte
1cb08 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e  r the address in
1cb09 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f   register P1..*/
1cb0a 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a  .case OP_Return:
1cb0b 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1cb0c 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20  in1 */.  pIn1 = 
1cb0d 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1cb0e 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1cb0f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
1cb10 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70  );.  pc = (int)p
1cb11 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61  In1->u.i;.  brea
1cb12 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1cb13 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a    Yield P1 * * *
1cb14 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68   *.**.** Swap th
1cb15 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
1cb16 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65  r with the value
1cb17 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1cb18 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c  .*/.case OP_Yiel
1cb19 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  d: {            
1cb1a 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20  /* in1 */.#if 0 
1cb1b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1cb1c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1cb1d 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44  .aa */.  int pcD
1cb1e 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  est;.#endif /* l
1cb1f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1cb20 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a  oved into u.aa *
1cb21 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1cb22 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1cb23 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67  ert( (pIn1->flag
1cb24 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20  s & MEM_Dyn)==0 
1cb25 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1cb26 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e   = MEM_Int;.  u.
1cb27 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69 6e 74  aa.pcDest = (int
1cb28 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49  )pIn1->u.i;.  pI
1cb29 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20  n1->u.i = pc;.  
1cb2a 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1cb2b 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20  Op->p1, pIn1);. 
1cb2c 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 65 73   pc = u.aa.pcDes
1cb2d 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  t;.  break;.}../
1cb2e 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49  * Opcode:  HaltI
1cb2f 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20  fNull  P1 P2 P3 
1cb30 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b  P4 *.**.** Check
1cb31 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cb32 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69  gister P3.  If i
1cb33 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48  s is NULL then H
1cb34 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72  alt using.** par
1cb35 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61  ameter P1, P2, a
1cb36 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73  nd P4 as if this
1cb37 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73   were a Halt ins
1cb38 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  truction.  If th
1cb39 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65  e.** value in re
1cb3a 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74  gister P3 is not
1cb3b 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
1cb3c 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1cb3d 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  -op..*/.case OP_
1cb3e 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20  HaltIfNull: {   
1cb3f 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70     /* in3 */.  p
1cb40 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In3 = &aMem[pOp-
1cb41 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e  >p3];.  if( (pIn
1cb42 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  3->flags & MEM_N
1cb43 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  ull)==0 ) break;
1cb44 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75  .  /* Fall throu
1cb45 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20  gh into OP_Halt 
1cb46 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  */.}../* Opcode:
1cb47 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50    Halt P1 P2 * P
1cb48 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69  4 *.**.** Exit i
1cb49 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c  mmediately.  All
1cb4a 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65   open cursors, e
1cb4b 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a  tc are closed.**
1cb4c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
1cb4d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1cb4e 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75  result code retu
1cb4f 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
1cb50 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f  exec(), sqlite3_
1cb51 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73  reset(),.** or s
1cb52 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1cb53 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c  ).  For a normal
1cb54 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75   halt, this shou
1cb55 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20  ld be SQLITE_OK 
1cb56 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f  (0)..** For erro
1cb57 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f  rs, it can be so
1cb58 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20  me other value. 
1cb59 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50   If P1!=0 then P
1cb5a 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65  2 will determine
1cb5b 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e  .** whether or n
1cb5c 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ot to rollback t
1cb5d 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
1cb5e 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20  action.  Do not 
1cb5f 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50  rollback.** if P
1cb60 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74  2==OE_Fail. Do t
1cb61 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50  he rollback if P
1cb62 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20  2==OE_Rollback. 
1cb63 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74   If P2==OE_Abort
1cb64 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f  ,.** then back o
1cb65 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74  ut all changes t
1cb66 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65  hat have occurre
1cb67 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78  d during this ex
1cb68 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  ecution of the.*
1cb69 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e  * VDBE, but do n
1cb6a 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  ot rollback the 
1cb6b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a  transaction. .**
1cb6c 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74  .** If P4 is not
1cb6d 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73   null then it is
1cb6e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1cb6f 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  e string..**.** 
1cb70 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c  There is an impl
1cb71 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22  ied "Halt 0 0 0"
1cb72 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73   instruction ins
1cb73 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72  erted at the ver
1cb74 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72  y end of.** ever
1cb75 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61  y program.  So a
1cb76 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c   jump past the l
1cb77 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ast instruction 
1cb78 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  of the program.*
1cb79 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  * is the same as
1cb7a 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e   executing Halt.
1cb7b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74  .*/.case OP_Halt
1cb7c 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  : {.  if( pOp->p
1cb7d 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  1==SQLITE_OK && 
1cb7e 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20  p->pFrame ){.   
1cb7f 20 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62   /* Halt the sub
1cb80 2d 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e  -program. Return
1cb81 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20   control to the 
1cb82 70 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f  parent frame. */
1cb83 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a  .    VdbeFrame *
1cb84 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61  pFrame = p->pFra
1cb85 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d  me;.    p->pFram
1cb86 65 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72  e = pFrame->pPar
1cb87 65 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61  ent;.    p->nFra
1cb88 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65  me--;.    sqlite
1cb89 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1cb8a 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
1cb8b 0a 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65  .    pc = sqlite
1cb8c 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72  3VdbeFrameRestor
1cb8d 65 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69  e(pFrame);.    i
1cb8e 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49  f( pOp->p2==OE_I
1cb8f 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f  gnore ){.      /
1cb90 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63  * Instruction pc
1cb91 20 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72   is the OP_Progr
1cb92 61 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  am that invoked 
1cb93 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
1cb94 0a 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e  .      ** curren
1cb95 74 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64  tly being halted
1cb96 2e 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74  . If the p2 inst
1cb97 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ruction of this 
1cb98 4f 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a  OP_Halt.      **
1cb99 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1cb9a 73 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65  set to OE_Ignore
1cb9b 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70  , then the sub-p
1cb9c 72 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69  rogram is throwi
1cb9d 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49  ng.      ** an I
1cb9e 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e  GNORE exception.
1cb9f 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75   In this case ju
1cba0 6d 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73  mp to the addres
1cba1 73 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20  s specified.    
1cba2 20 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f    ** as the p2 o
1cba3 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50  f the calling OP
1cba4 5f 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20  _Program.  */.  
1cba5 20 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b      pc = p->aOp[
1cba6 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a  pc].p2-1;.    }.
1cba7 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70      aOp = p->aOp
1cba8 3b 0a 20 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e  ;.    aMem = p->
1cba9 61 4d 65 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b  aMem;.    break;
1cbaa 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20  .  }..  p->rc = 
1cbab 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72  pOp->p1;.  p->er
1cbac 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29  rorAction = (u8)
1cbad 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63  pOp->p2;.  p->pc
1cbae 20 3d 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70   = pc;.  if( pOp
1cbaf 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71  ->p4.z ){.    sq
1cbb0 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1cbb1 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1cbb2 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  "%s", pOp->p4.z)
1cbb3 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
1cbb4 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1cbb5 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
1cbb6 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63  QLITE_BUSY || rc
1cbb7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  ==SQLITE_OK || r
1cbb8 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  c==SQLITE_ERROR 
1cbb9 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1cbba 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1cbbb 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1cbbc 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
1cbbd 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  e{.    assert( r
1cbbe 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1cbbf 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f  p->rc==SQLITE_CO
1cbc0 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20  NSTRAINT );.    
1cbc1 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1cbc2 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65  TE_OK || db->nDe
1cbc3 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a  ferredCons>0 );.
1cbc4 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f      rc = p->rc ?
1cbc5 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20   SQLITE_ERROR : 
1cbc6 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
1cbc7 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
1cbc8 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
1cbc9 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32  e: Integer P1 P2
1cbca 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1cbcb 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
1cbcc 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74  value P1 is writ
1cbcd 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ten into registe
1cbce 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1cbcf 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20  _Integer: {     
1cbd0 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1cbd1 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74  elease */.  pOut
1cbd2 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b  ->u.i = pOp->p1;
1cbd3 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cbd4 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20  Opcode: Int64 * 
1cbd5 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1cbd6 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1cbd7 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  to a 64-bit inte
1cbd8 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72  ger value..** Wr
1cbd9 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69  ite that value i
1cbda 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1cbdb 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36  .*/.case OP_Int6
1cbdc 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f  4: {           /
1cbdd 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1cbde 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1cbdf 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29  Op->p4.pI64!=0 )
1cbe0 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1cbe1 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20  *pOp->p4.pI64;. 
1cbe2 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cbe3 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20  code: Real * P2 
1cbe4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1cbe5 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1cbe6 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e  a 64-bit floatin
1cbe7 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a  g point value..*
1cbe8 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c  * Write that val
1cbe9 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
1cbea 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1cbeb 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Real: {         
1cbec 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cbed 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65  _FLOAT, out2-pre
1cbee 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75  release */.  pOu
1cbef 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52  t->flags = MEM_R
1cbf0 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21  eal;.  assert( !
1cbf1 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f  sqlite3IsNaN(*pO
1cbf2 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a  p->p4.pReal) );.
1cbf3 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70    pOut->r = *pOp
1cbf4 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72  ->p4.pReal;.  br
1cbf5 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cbf6 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20  e: String8 * P2 
1cbf7 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1cbf8 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20  points to a nul 
1cbf9 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
1cbfa 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70   string. This op
1cbfb 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
1cbfc 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20  med .** into an 
1cbfd 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65  OP_String before
1cbfe 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20   it is executed 
1cbff 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69  for the first ti
1cc00 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  me..*/.case OP_S
1cc01 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20  tring8: {       
1cc02 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cc03 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65  STRING, out2-pre
1cc04 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73  release */.  ass
1cc05 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d  ert( pOp->p4.z!=
1cc06 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  0 );.  pOp->opco
1cc07 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a  de = OP_String;.
1cc08 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69    pOp->p1 = sqli
1cc09 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d  te3Strlen30(pOp-
1cc0a 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66  >p4.z);..#ifndef
1cc0b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1cc0c 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e  16.  if( encodin
1cc0d 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  g!=SQLITE_UTF8 )
1cc0e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1cc0f 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
1cc10 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c  pOut, pOp->p4.z,
1cc11 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
1cc12 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
1cc13 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1cc14 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f  LITE_TOOBIG ) go
1cc15 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
1cc16 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1cc17 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1cc18 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65  Encoding(pOut, e
1cc19 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20  ncoding) ) goto 
1cc1a 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65  no_mem;.    asse
1cc1b 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f  rt( pOut->zMallo
1cc1c 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20  c==pOut->z );.  
1cc1d 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e    assert( pOut->
1cc1e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20  flags & MEM_Dyn 
1cc1f 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61  );.    pOut->zMa
1cc20 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f  lloc = 0;.    pO
1cc21 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  ut->flags |= MEM
1cc22 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75  _Static;.    pOu
1cc23 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  t->flags &= ~MEM
1cc24 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f  _Dyn;.    if( pO
1cc25 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59  p->p4type==P4_DY
1cc26 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
1cc27 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1cc28 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1cc29 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79   }.    pOp->p4ty
1cc2a 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b  pe = P4_DYNAMIC;
1cc2b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
1cc2c 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f   pOut->z;.    pO
1cc2d 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b  p->p1 = pOut->n;
1cc2e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
1cc2f 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c  ( pOp->p1>db->aL
1cc30 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1cc31 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1cc32 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1cc33 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72   }.  /* Fall thr
1cc34 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1cc35 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67   case, OP_String
1cc36 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f   */.}.  ./* Opco
1cc37 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32  de: String P1 P2
1cc38 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68   * P4 *.**.** Th
1cc39 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50  e string value P
1cc3a 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28  4 of length P1 (
1cc3b 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64  bytes) is stored
1cc3c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1cc3d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
1cc3e 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  ng: {          /
1cc3f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1cc40 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
1cc41 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20  Op->p4.z!=0 );. 
1cc42 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1cc43 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69  EM_Str|MEM_Stati
1cc44 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f  c|MEM_Term;.  pO
1cc45 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e  ut->z = pOp->p4.
1cc46 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70  z;.  pOut->n = p
1cc47 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e  Op->p1;.  pOut->
1cc48 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1cc49 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1cc4a 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1cc4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cc4c 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20  de: Null * P2 * 
1cc4d 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1cc4e 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69  a NULL into regi
1cc4f 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1cc50 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20   OP_Null: {     
1cc51 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1cc52 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f  erelease */.  pO
1cc53 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1cc54 4e 75 6c 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Null;.  break;.}
1cc55 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c  .../* Opcode: Bl
1cc56 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a  ob P1 P2 * P4.**
1cc57 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f  .** P4 points to
1cc58 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20   a blob of data 
1cc59 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20  P1 bytes long.  
1cc5a 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c  Store this.** bl
1cc5b 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ob in register P
1cc5c 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  2. This instruct
1cc5d 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64  ion is not coded
1cc5e 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
1cc5f 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e  the compiler. In
1cc60 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69  stead, the compi
1cc61 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66  ler layer specif
1cc62 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78  ies.** an OP_Hex
1cc63 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74  Blob opcode, wit
1cc64 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67  h the hex string
1cc65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1cc66 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61  of.** the blob a
1cc67 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64  s P4. This opcod
1cc68 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  e is transformed
1cc69 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a   to an OP_Blob.*
1cc6a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
1cc6b 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e   it is executed.
1cc6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62  .*/.case OP_Blob
1cc6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1cc6e 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1cc6f 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72  lease */.  asser
1cc70 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51  t( pOp->p1 <= SQ
1cc71 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
1cc72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1cc73 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20  MemSetStr(pOut, 
1cc74 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e  pOp->p4.z, pOp->
1cc75 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75  p1, 0, 0);.  pOu
1cc76 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1cc77 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1cc78 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1cc79 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cc7a 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20  pcode: Variable 
1cc7b 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1cc7c 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65  .** Transfer the
1cc7d 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64   values of bound
1cc7e 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e 2e   parameters P1..
1cc7f 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65 67  P1+P3-1 into reg
1cc80 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32  isters.** P2..P2
1cc81 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  +P3-1..**.** If 
1cc82 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  the parameter is
1cc83 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74 73   named, then its
1cc84 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69 6e   name appears in
1cc85 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a   P4 and P3==1..*
1cc86 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 69  * The P4 value i
1cc87 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65  s used by sqlite
1cc88 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
1cc89 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65  _name()..*/.case
1cc8a 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a   OP_Variable: {.
1cc8b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1cc8c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1cc8d 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69  into u.ab */.  i
1cc8e 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20  nt p1;          
1cc8f 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63  /* Variable to c
1cc90 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  opy from */.  in
1cc91 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f  t p2;          /
1cc92 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f  * Register to co
1cc93 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e  py to */.  int n
1cc94 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1cc95 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
1cc96 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  left to copy */.
1cc97 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20    Mem *pVar;    
1cc98 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e     /* Value bein
1cc99 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f  g transferred */
1cc9a 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1cc9b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cc9c 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20   into u.ab */.. 
1cc9d 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e   u.ab.p1 = pOp->
1cc9e 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70  p1 - 1;.  u.ab.p
1cc9f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  2 = pOp->p2;.  u
1cca0 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b  .ab.n = pOp->p3;
1cca1 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e  .  assert( u.ab.
1cca2 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70 31  p1>=0 && u.ab.p1
1cca3 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72  +u.ab.n<=p->nVar
1cca4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1cca5 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61 62  ab.p2>=1 && u.ab
1cca6 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d  .p2+u.ab.n-1<=p-
1cca7 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72  >nMem );.  asser
1cca8 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20  t( pOp->p4.z==0 
1cca9 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c  || pOp->p3==1 ||
1ccaa 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a   pOp->p3==0 );..
1ccab 20 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d    while( u.ab.n-
1ccac 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61  - > 0 ){.    u.a
1ccad 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61  b.pVar = &p->aVa
1ccae 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20  r[u.ab.p1++];.  
1ccaf 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1ccb0 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e  eMemTooBig(u.ab.
1ccb1 70 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67  pVar) ){.      g
1ccb2 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20  oto too_big;.   
1ccb3 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 61   }.    pOut = &a
1ccb4 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a  Mem[u.ab.p2++];.
1ccb5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1ccb6 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1ccb7 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75  l(pOut);.    pOu
1ccb8 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  t->flags = MEM_N
1ccb9 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ull;.    sqlite3
1ccba 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
1ccbb 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56  py(pOut, u.ab.pV
1ccbc 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b  ar, MEM_Static);
1ccbd 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  .    UPDATE_MAX_
1ccbe 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1ccbf 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ccc0 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20  /* Opcode: Move 
1ccc1 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ccc2 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75  ** Move the valu
1ccc3 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
1ccc4 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20  1..P1+P3-1 over 
1ccc5 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
1ccc6 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20  s P2..P2+P3-1.  
1ccc7 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31  Registers P1..P1
1ccc8 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66  +P1-1 are.** lef
1ccc9 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c  t holding a NULL
1ccca 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  .  It is an erro
1cccb 72 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72  r for register r
1cccc 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b  anges.** P1..P1+
1cccd 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b  P3-1 and P2..P2+
1ccce 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e  P3-1 to overlap.
1cccf 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65  .*/.case OP_Move
1ccd0 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1ccd1 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1ccd2 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f  ved into u.ac */
1ccd3 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63  .  char *zMalloc
1ccd4 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76  ;   /* Holding v
1ccd5 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f  ariable for allo
1ccd6 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a  cated memory */.
1ccd7 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
1ccd8 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1ccd9 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74  registers left t
1ccda 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20  o copy */.  int 
1ccdb 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
1ccdc 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
1ccdd 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70   from */.  int p
1ccde 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  2;          /* R
1ccdf 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20  egister to copy 
1cce0 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  to */.#endif /* 
1cce1 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cce2 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20  moved into u.ac 
1cce3 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70  */..  u.ac.n = p
1cce4 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70  Op->p3;.  u.ac.p
1cce5 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1cce6 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .ac.p2 = pOp->p2
1cce7 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63  ;.  assert( u.ac
1cce8 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e  .n>0 && u.ac.p1>
1cce9 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29  0 && u.ac.p2>0 )
1ccea 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63  ;.  assert( u.ac
1cceb 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63  .p1+u.ac.n<=u.ac
1ccec 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75  .p2 || u.ac.p2+u
1cced 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29  .ac.n<=u.ac.p1 )
1ccee 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  ;..  pIn1 = &aMe
1ccef 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f  m[u.ac.p1];.  pO
1ccf0 75 74 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e  ut = &aMem[u.ac.
1ccf1 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e  p2];.  while( u.
1ccf2 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73  ac.n-- ){.    as
1ccf3 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65  sert( pOut<=&aMe
1ccf4 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20  m[p->nMem] );.  
1ccf5 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d    assert( pIn1<=
1ccf6 26 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29  &aMem[p->nMem] )
1ccf7 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c  ;.    u.ac.zMall
1ccf8 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  oc = pOut->zMall
1ccf9 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d  oc;.    pOut->zM
1ccfa 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73  alloc = 0;.    s
1ccfb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1ccfc 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20  e(pOut, pIn1);. 
1ccfd 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63     pIn1->zMalloc
1ccfe 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b   = u.ac.zMalloc;
1ccff 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
1cd00 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70  ACE(u.ac.p2++, p
1cd01 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b  Out);.    pIn1++
1cd02 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20  ;.    pOut++;.  
1cd03 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1cd04 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31   Opcode: Copy P1
1cd05 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1cd06 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72  Make a copy of r
1cd07 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20  egister P1 into 
1cd08 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1cd09 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1cd0a 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70  ion makes a deep
1cd0b 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
1cd0c 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65  ue.  A duplicate
1cd0d 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61  .** is made of a
1cd0e 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  ny string or blo
1cd0f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65  b constant.  See
1cd10 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a   also OP_SCopy..
1cd11 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a  */.case OP_Copy:
1cd12 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1cd13 2a 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20  * in1, out2 */. 
1cd14 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
1cd15 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d  p->p1];.  pOut =
1cd16 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b   &aMem[pOp->p2];
1cd17 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21  .  assert( pOut!
1cd18 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74  =pIn1 );.  sqlit
1cd19 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
1cd1a 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
1cd1b 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44   MEM_Ephem);.  D
1cd1c 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f  eephemeralize(pO
1cd1d 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  ut);.  REGISTER_
1cd1e 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1cd1f 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1cd20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f  ../* Opcode: SCo
1cd21 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  py P1 P2 * * *.*
1cd22 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c  *.** Make a shal
1cd23 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69  low copy of regi
1cd24 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67  ster P1 into reg
1cd25 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1cd26 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1cd27 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77   makes a shallow
1cd28 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
1cd29 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ue.  If the valu
1cd2a 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67  e.** is a string
1cd2b 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74   or blob, then t
1cd2c 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20  he copy is only 
1cd2d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1cd2e 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64  .** original and
1cd2f 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72   hence if the or
1cd30 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73  iginal changes s
1cd31 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e  o will the copy.
1cd32 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68  .** Worse, if th
1cd33 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65  e original is de
1cd34 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63  allocated, the c
1cd35 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61  opy becomes inva
1cd36 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65  lid..** Thus the
1cd37 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75   program must gu
1cd38 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
1cd39 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e   original will n
1cd3a 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72  ot change.** dur
1cd3b 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65  ing the lifetime
1cd3c 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55   of the copy.  U
1cd3d 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61  se OP_Copy to ma
1cd3e 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a  ke a complete.**
1cd3f 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f   copy..*/.case O
1cd40 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20  P_SCopy: {      
1cd41 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75        /* in1, ou
1cd42 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26  t2 */.  pIn1 = &
1cd43 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1cd44 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1cd45 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1cd46 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a  ( pOut!=pIn1 );.
1cd47 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cd48 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1cd49 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65  , pIn1, MEM_Ephe
1cd4a 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  m);.  REGISTER_T
1cd4b 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
1cd4c 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1cd4d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75  ./* Opcode: Resu
1cd4e 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20  ltRow P1 P2 * * 
1cd4f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69  *.**.** The regi
1cd50 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68  sters P1 through
1cd51 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e   P1+P2-1 contain
1cd52 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
1cd53 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69  .** results. Thi
1cd54 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20  s opcode causes 
1cd55 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  the sqlite3_step
1cd56 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69  () call to termi
1cd57 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20  nate.** with an 
1cd58 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72  SQLITE_ROW retur
1cd59 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65  n code and it se
1cd5a 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65  ts up the sqlite
1cd5b 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74  3_stmt.** struct
1cd5c 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61  ure to provide a
1cd5d 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70  ccess to the top
1cd5e 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68   P1 values as th
1cd5f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e  e result.** row.
1cd60 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75  .*/.case OP_Resu
1cd61 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20  ltRow: {.#if 0  
1cd62 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cd63 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cd64 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  ad */.  Mem *pMe
1cd65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64  m;.  int i;.#end
1cd66 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1cd67 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1cd68 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65 72   u.ad */.  asser
1cd69 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  t( p->nResColumn
1cd6a 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61  ==pOp->p2 );.  a
1cd6b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30  ssert( pOp->p1>0
1cd6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1cd6d 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70  p->p1+pOp->p2<=p
1cd6e 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f  ->nMem+1 );..  /
1cd6f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d  * If this statem
1cd70 65 6e 74 20 68 61 73 20 76 69 6f 6c 61 74 65 64  ent has violated
1cd71 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69   immediate forei
1cd72 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1cd73 74 73 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20  ts, do.  ** not 
1cd74 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
1cd75 72 20 6f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69  r of rows modifi
1cd76 65 64 2e 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52  ed. And do not R
1cd77 45 4c 45 41 53 45 20 74 68 65 20 73 74 61 74 65  ELEASE the state
1cd78 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61  ment.  ** transa
1cd79 63 74 69 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20  ction. It needs 
1cd7a 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
1cd7b 6b 2e 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c  k.  */.  if( SQL
1cd7c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
1cd7d 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1cd7e 28 70 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 61  (p, 0)) ){.    a
1cd7f 73 73 65 72 74 28 20 64 62 2d 3e 66 6c 61 67 73  ssert( db->flags
1cd80 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77  &SQLITE_CountRow
1cd81 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  s );.    assert(
1cd82 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72   p->usesStmtJour
1cd83 6e 61 6c 20 29 3b 0a 20 20 20 20 62 72 65 61 6b  nal );.    break
1cd84 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1cd85 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  he SQLITE_CountR
1cd86 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20  ows flag is set 
1cd87 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73  in sqlite3.flags
1cd88 20 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a   mask, then.  **
1cd89 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   DML statements 
1cd8a 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f  invoke this opco
1cd8b 64 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65  de to return the
1cd8c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a   number of rows.
1cd8d 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f    ** modified to
1cd8e 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20   the user. This 
1cd8f 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
1cd90 74 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20  that a VM that. 
1cd91 20 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74   ** opens a stat
1cd92 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1cd93 6e 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69  n may invoke thi
1cd94 73 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20  s opcode..  **. 
1cd95 20 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73   ** In case this
1cd96 20 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65   is such a state
1cd97 6d 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20  ment, close any 
1cd98 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1cd99 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65  ction.  ** opene
1cd9a 64 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66  d by this VM bef
1cd9b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f  ore returning co
1cd9c 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65  ntrol to the use
1cd9d 72 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20  r. This is to.  
1cd9e 2a 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73  ** ensure that s
1cd9f 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
1cda0 74 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73  tions are always
1cda1 20 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65   nested, not ove
1cda2 72 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49  rlapping..  ** I
1cda3 66 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65  f the open state
1cda4 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
1cda5 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68   is not closed h
1cda6 65 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73  ere, then the us
1cda7 65 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70  er.  ** may step
1cda8 20 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74   another VM that
1cda9 20 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73   opens its own s
1cdaa 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1cdab 74 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20  tion. This.  ** 
1cdac 6d 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72  may lead to over
1cdad 6c 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e  lapping statemen
1cdae 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a  t transactions..
1cdaf 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74    **.  ** The st
1cdb0 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1cdb1 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74  ion is never a t
1cdb2 6f 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63  op-level transac
1cdb3 74 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a  tion.  Hence.  *
1cdb4 2a 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61  * the RELEASE ca
1cdb5 6c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76  ll below can nev
1cdb6 65 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  er fail..  */.  
1cdb7 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74  assert( p->iStat
1cdb8 65 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e  ement==0 || db->
1cdb9 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75  flags&SQLITE_Cou
1cdba 6e 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d  ntRows );.  rc =
1cdbb 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
1cdbc 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41  eStatement(p, SA
1cdbd 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29  VEPOINT_RELEASE)
1cdbe 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1cdbf 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a  !=SQLITE_OK) ){.
1cdc0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
1cdc1 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20    /* Invalidate 
1cdc2 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75  all ephemeral cu
1cdc3 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20  rsor row caches 
1cdc4 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72  */.  p->cacheCtr
1cdc5 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20   = (p->cacheCtr 
1cdc6 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61  + 2)|1;..  /* Ma
1cdc7 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75  ke sure the resu
1cdc8 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  lts of the curre
1cdc9 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20  nt row are \000 
1cdca 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20  terminated.  ** 
1cdcb 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69  and have an assi
1cdcc 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20  gned type.  The 
1cdcd 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65  results are de-e
1cdce 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a  phemeralized as.
1cdcf 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66    ** as side eff
1cdd0 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64  ect..  */.  u.ad
1cdd1 2e 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75  .pMem = p->pResu
1cdd2 6c 74 53 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f  ltSet = &aMem[pO
1cdd3 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p1];.  for(u.
1cdd4 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70  ad.i=0; u.ad.i<p
1cdd5 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b  Op->p2; u.ad.i++
1cdd6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1cdd7 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
1cdd8 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61  e(&u.ad.pMem[u.a
1cdd9 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  d.i]);.    sqlit
1cdda 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
1cddb 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e  pe(&u.ad.pMem[u.
1cddc 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49  ad.i]);.    REGI
1cddd 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1cdde 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64  p1+u.ad.i, &u.ad
1cddf 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a  .pMem[u.ad.i]);.
1cde0 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
1cde1 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1cde2 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20  o no_mem;..  /* 
1cde3 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f  Return SQLITE_RO
1cde4 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d  W.  */.  p->pc =
1cde5 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20   pc + 1;.  rc = 
1cde6 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f  SQLITE_ROW;.  go
1cde7 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1cde8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1cde9 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20  ncat P1 P2 P3 * 
1cdea 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
1cdeb 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72  text in register
1cdec 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64   P1 onto the end
1cded 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a   of the text in.
1cdee 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61  ** register P2 a
1cdef 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1cdf0 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1cdf1 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1cdf2 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65   the P1 or P2 te
1cdf3 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e  xt are NULL then
1cdf4 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50   store NULL in P
1cdf5 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20  3..**.**   P3 = 
1cdf6 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49  P2 || P1.**.** I
1cdf7 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72  t is illegal for
1cdf8 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65   P1 and P3 to be
1cdf9 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74   the same regist
1cdfa 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a  er. Sometimes,.*
1cdfb 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73  * if P3 is the s
1cdfc 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20  ame register as 
1cdfd 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  P2, the implemen
1cdfe 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a  tation is able.*
1cdff 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d  * to avoid a mem
1ce00 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  cpy()..*/.case O
1ce01 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20  P_Concat: {     
1ce02 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1ce03 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c   TK_CONCAT, in1,
1ce04 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69   in2, out3 */.#i
1ce05 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ce06 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ce07 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34  to u.ae */.  i64
1ce08 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f   nByte;.#endif /
1ce09 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ce0a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1ce0b 65 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26  e */..  pIn1 = &
1ce0c 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1ce0d 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn2 = &aMem[pO
1ce0e 70 2d 3e 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d  p->p2];.  pOut =
1ce0f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
1ce10 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21  .  assert( pIn1!
1ce11 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28  =pOut );.  if( (
1ce12 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1ce13 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d  n2->flags) & MEM
1ce14 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1ce15 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1ce16 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72  ll(pOut);.    br
1ce17 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45  eak;.  }.  if( E
1ce18 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20  xpandBlob(pIn1) 
1ce19 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49  || ExpandBlob(pI
1ce1a 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  n2) ) goto no_me
1ce1b 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70  m;.  Stringify(p
1ce1c 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  In1, encoding);.
1ce1d 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32    Stringify(pIn2
1ce1e 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75  , encoding);.  u
1ce1f 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31  .ae.nByte = pIn1
1ce20 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20  ->n + pIn2->n;. 
1ce21 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e   if( u.ae.nByte>
1ce22 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1ce23 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1ce24 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1ce25 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65  big;.  }.  MemSe
1ce26 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1ce27 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20  MEM_Str);.  if( 
1ce28 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1ce29 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e  ow(pOut, (int)u.
1ce2a 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74  ae.nByte+2, pOut
1ce2b 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67  ==pIn2) ){.    g
1ce2c 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1ce2d 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32    if( pOut!=pIn2
1ce2e 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   ){.    memcpy(p
1ce2f 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c  Out->z, pIn2->z,
1ce30 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20   pIn2->n);.  }. 
1ce31 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a   memcpy(&pOut->z
1ce32 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d  [pIn2->n], pIn1-
1ce33 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20  >z, pIn1->n);.  
1ce34 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79  pOut->z[u.ae.nBy
1ce35 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d  te] = 0;.  pOut-
1ce36 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d  >z[u.ae.nByte+1]
1ce37 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c   = 0;.  pOut->fl
1ce38 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
1ce39 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e  .  pOut->n = (in
1ce3a 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20  t)u.ae.nByte;.  
1ce3b 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  pOut->enc = enco
1ce3c 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ding;.  UPDATE_M
1ce3d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1ce3e 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1ce3f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31  * Opcode: Add P1
1ce40 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ce41 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69   Add the value i
1ce42 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f  n register P1 to
1ce43 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ce44 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1ce45 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1ce46 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1ce47 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1ce48 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1ce49 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1ce4a 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1ce4b 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50  Multiply P1 P2 P
1ce4c 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d  3 * *.**.**.** M
1ce4d 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75  ultiply the valu
1ce4e 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1ce4f 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e   by the value in
1ce50 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20   register P2.** 
1ce51 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
1ce52 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1ce53 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
1ce54 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
1ce55 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1ce56 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ULL..*/./* Opcod
1ce57 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50  e: Subtract P1 P
1ce58 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  2 P3 * *.**.** S
1ce59 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75  ubtract the valu
1ce5a 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1ce5b 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20   from the value 
1ce5c 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1ce5d 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1ce5e 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1ce5f 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1ce60 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1ce61 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1ce62 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1ce63 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50  ode: Divide P1 P
1ce64 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44  2 P3 * *.**.** D
1ce65 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20  ivide the value 
1ce66 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62  in register P1 b
1ce67 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
1ce68 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e  egister P2.** an
1ce69 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
1ce6a 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  lt in register P
1ce6b 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66  3 (P3=P2/P1). If
1ce6c 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a   the value in .*
1ce6d 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  * register P1 is
1ce6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
1ce6f 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20  result is NULL. 
1ce70 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1ce71 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65  is .** NULL, the
1ce72 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1ce73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .*/./* Opcode: R
1ce74 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50  emainder P1 P2 P
1ce75 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  3 * *.**.** Comp
1ce76 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65  ute the remainde
1ce77 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20  r after integer 
1ce78 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20  division of the 
1ce79 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69  value in.** regi
1ce7a 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
1ce7b 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ce7c 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68   P2 and store th
1ce7d 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20  e result in P3. 
1ce7e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1ce7f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20   in register P2 
1ce80 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75  is zero the resu
1ce81 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49  lt is NULL..** I
1ce82 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
1ce83 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1ce84 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1ce85 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20  .case OP_Add:   
1ce86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce87 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c  /* same as TK_PL
1ce88 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  US, in1, in2, ou
1ce89 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75  t3 */.case OP_Su
1ce8a 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20  btract:         
1ce8b 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1ce8c 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69  TK_MINUS, in1, i
1ce8d 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1ce8e 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20   OP_Multiply:   
1ce8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1ce90 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69  me as TK_STAR, i
1ce91 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1ce92 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a  .case OP_Divide:
1ce93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce94 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
1ce95 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  ASH, in1, in2, o
1ce96 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ut3 */.case OP_R
1ce97 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20  emainder: {     
1ce98 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1ce99 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e   TK_REM, in1, in
1ce9a 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30  2, out3 */.#if 0
1ce9b 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1ce9c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ce9d 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  u.af */.  int fl
1ce9e 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d  ags;      /* Com
1ce9f 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67  bined MEM_* flag
1cea0 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75  s from both inpu
1cea1 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20  ts */.  i64 iA; 
1cea2 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
1cea3 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  er value of left
1cea4 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36   operand */.  i6
1cea5 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  4 iB;         /*
1cea6 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f   Integer value o
1cea7 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  f right operand 
1cea8 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20  */.  double rA; 
1cea9 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1ceaa 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
1ceab 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72  nd */.  double r
1ceac 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20  B;      /* Real 
1cead 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
1ceae 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66  perand */.#endif
1ceaf 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ceb0 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ceb1 2e 61 66 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d  .af */..  pIn1 =
1ceb2 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1ceb3 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  .  applyNumericA
1ceb4 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20  ffinity(pIn1);. 
1ceb5 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn2 = &aMem[pO
1ceb6 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e  p->p2];.  applyN
1ceb7 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
1ceb8 49 6e 32 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26  In2);.  pOut = &
1ceb9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1ceba 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20 70 49   u.af.flags = pI
1cebb 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32  n1->flags | pIn2
1cebc 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28  ->flags;.  if( (
1cebd 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d  u.af.flags & MEM
1cebe 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f  _Null)!=0 ) goto
1cebf 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75   arithmetic_resu
1cec0 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66  lt_is_null;.  if
1cec1 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1cec2 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn2->flags & M
1cec3 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74  EM_Int)==MEM_Int
1cec4 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69 41 20   ){.    u.af.iA 
1cec5 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn1->u.i;.   
1cec6 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d   u.af.iB = pIn2-
1cec7 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68  >u.i;.    switch
1cec8 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1cec9 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41  .      case OP_A
1ceca 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66  dd:         u.af
1cecb 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20  .iB += u.af.iA; 
1cecc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cecd 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72     case OP_Subtr
1cece 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69 42 20  act:    u.af.iB 
1cecf 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20  -= u.af.iA;     
1ced0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1ced1 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a  ase OP_Multiply:
1ced2 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75      u.af.iB *= u
1ced3 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72  .af.iA;       br
1ced4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
1ced5 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20  OP_Divide: {.   
1ced6 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1ced7 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
1ced8 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1ced9 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a  null;.        /*
1ceda 20 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61   Dividing the la
1cedb 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e  rgest possible n
1cedc 65 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69  egative 64-bit i
1cedd 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62  nteger (1<<63) b
1cede 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20  y.        ** -1 
1cedf 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
1cee0 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  er too large to 
1cee1 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69  store in a 64-bi
1cee2 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a  t data-type. On.
1cee3 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20          ** some 
1cee4 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74  architectures, t
1cee5 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f  he value overflo
1cee6 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f  ws to (1<<63). O
1cee7 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20  n others,.      
1cee8 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73    ** a SIGFPE is
1cee9 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c   issued. The fol
1ceea 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
1ceeb 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73   normalizes this
1ceec 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61  .        ** beha
1ceed 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c  vior so that all
1ceee 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62   architectures b
1ceef 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65  ehave as if inte
1cef0 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ger.        ** o
1cef1 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64  verflow occurred
1cef2 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1cef3 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1cef4 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69 42 3d  ==-1 && u.af.iB=
1cef5 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20  =SMALLEST_INT64 
1cef6 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20  ) u.af.iA = 1;. 
1cef7 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2f         u.af.iB /
1cef8 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20  = u.af.iA;.     
1cef9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cefa 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
1cefb 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75   {.        if( u
1cefc 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f  .af.iA==0 ) goto
1cefd 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75   arithmetic_resu
1cefe 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20  lt_is_null;.    
1ceff 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d      if( u.af.iA=
1cf00 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20  =-1 ) u.af.iA = 
1cf01 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e  1;.        u.af.
1cf02 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20  iB %= u.af.iA;. 
1cf03 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cf04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1cf05 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66  pOut->u.i = u.af
1cf06 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54  .iB;.    MemSetT
1cf07 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1cf08 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  M_Int);.  }else{
1cf09 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d 20 73  .    u.af.rA = s
1cf0a 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1cf0b 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 75  lue(pIn1);.    u
1cf0c 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74 65 33  .af.rB = sqlite3
1cf0d 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49  VdbeRealValue(pI
1cf0e 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  n2);.    switch(
1cf0f 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a   pOp->opcode ){.
1cf10 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64        case OP_Ad
1cf11 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e  d:         u.af.
1cf12 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20  rB += u.af.rA;  
1cf13 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1cf14 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61    case OP_Subtra
1cf15 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2d  ct:    u.af.rB -
1cf16 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20  = u.af.rA;      
1cf17 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1cf18 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20  se OP_Multiply: 
1cf19 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e     u.af.rB *= u.
1cf1a 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65  af.rA;       bre
1cf1b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1cf1c 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20  P_Divide: {.    
1cf1d 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
1cf1e 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
1cf1f 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
1cf20 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
1cf21 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 72 41       if( u.af.rA
1cf22 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 67 6f  ==(double)0 ) go
1cf23 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1cf24 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1cf25 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d        u.af.rB /=
1cf26 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 20 20   u.af.rA;.      
1cf27 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1cf28 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
1cf29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69  {.        u.af.i
1cf2a 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 41  A = (i64)u.af.rA
1cf2b 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69  ;.        u.af.i
1cf2c 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 42  B = (i64)u.af.rB
1cf2d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  ;.        if( u.
1cf2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20  af.iA==0 ) goto 
1cf2f 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c  arithmetic_resul
1cf30 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20  t_is_null;.     
1cf31 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d     if( u.af.iA==
1cf32 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31  -1 ) u.af.iA = 1
1cf33 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72  ;.        u.af.r
1cf34 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75 2e 61  B = (double)(u.a
1cf35 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41 29 3b  f.iB % u.af.iA);
1cf36 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1cf37 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1cf38 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e    if( sqlite3IsN
1cf39 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20  aN(u.af.rB) ){. 
1cf3a 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d       goto arithm
1cf3b 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1cf3c 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ull;.    }.    p
1cf3d 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42  Out->r = u.af.rB
1cf3e 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1cf3f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52  Flag(pOut, MEM_R
1cf40 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 75  eal);.    if( (u
1cf41 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  .af.flags & MEM_
1cf42 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Real)==0 ){.    
1cf43 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74    sqlite3VdbeInt
1cf44 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75  egerAffinity(pOu
1cf45 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  t);.    }.  }.  
1cf46 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74  break;..arithmet
1cf47 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1cf48 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  l:.  sqlite3Vdbe
1cf49 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29  MemSetNull(pOut)
1cf4a 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cf4b 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71   Opcode: CollSeq
1cf4c 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34   * * P4.**.** P4
1cf4d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1cf4e 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
1cf4f 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  t. If the next c
1cf50 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75  all to a user fu
1cf51 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67  nction.** or agg
1cf52 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c  regate calls sql
1cf53 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
1cf54 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61  eq(), this colla
1cf55 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
1cf56 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65  ll.** be returne
1cf57 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  d. This is used 
1cf58 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  by the built-in 
1cf59 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64  min(), max() and
1cf5a 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e   nullif().** fun
1cf5b 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ctions..**.** Th
1cf5c 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64  e interface used
1cf5d 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
1cf5e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66  tation of the af
1cf5f 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e  orementioned fun
1cf60 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74  ctions.** to ret
1cf61 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74  rieve the collat
1cf62 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74  ion sequence set
1cf63 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20   by this opcode 
1cf64 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
1cf65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e  .** publicly, on
1cf66 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74  ly to user funct
1cf67 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20  ions defined in 
1cf68 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20  func.c..*/.case 
1cf69 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20  OP_CollSeq: {.  
1cf6a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1cf6b 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20  ype==P4_COLLSEQ 
1cf6c 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1cf6d 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69  * Opcode: Functi
1cf6e 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50  on P1 P2 P3 P4 P
1cf6f 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  5.**.** Invoke a
1cf70 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28   user function (
1cf71 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1cf72 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74  to a Function st
1cf73 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20  ructure that.** 
1cf74 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63  defines the func
1cf75 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72  tion) with P5 ar
1cf76 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72  guments taken fr
1cf77 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61  om register P2 a
1cf78 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  nd.** successors
1cf79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66  .  The result of
1cf7a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
1cf7b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1cf7c 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73  ter P3..** Regis
1cf7d 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20  ter P3 must not 
1cf7e 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75  be one of the fu
1cf7f 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a  nction inputs..*
1cf80 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d  *.** P1 is a 32-
1cf81 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69  bit bitmask indi
1cf82 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f  cating whether o
1cf83 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d  r not each argum
1cf84 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66  ent to the .** f
1cf85 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65  unction was dete
1cf86 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e  rmined to be con
1cf87 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
1cf88 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69   time. If the fi
1cf89 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
1cf8a 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65  was constant the
1cf8b 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73  n bit 0 of P1 is
1cf8c 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73   set. This is us
1cf8d 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a  ed to determine.
1cf8e 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20  ** whether meta 
1cf8f 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
1cf90 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63  with a user func
1cf91 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73  tion argument us
1cf92 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ing the.** sqlit
1cf93 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
1cf94 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65   API may be safe
1cf95 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69  ly retained unti
1cf96 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e  l the next.** in
1cf97 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  vocation of this
1cf98 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53   opcode..**.** S
1cf99 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70  ee also: AggStep
1cf9a 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f   and AggFinal.*/
1cf9b 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f  .case OP_Functio
1cf9c 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  n: {.#if 0  /* l
1cf9d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1cf9e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a  oved into u.ag *
1cf9f 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  /.  int i;.  Mem
1cfa0 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65   *pArg;.  sqlite
1cfa1 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
1cfa2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1cfa3 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b  *apVal;.  int n;
1cfa4 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1cfa5 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cfa6 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20   into u.ag */.. 
1cfa7 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70   u.ag.n = pOp->p
1cfa8 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61 6c 20  5;.  u.ag.apVal 
1cfa9 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73  = p->apArg;.  as
1cfaa 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56 61 6c  sert( u.ag.apVal
1cfab 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b   || u.ag.n==0 );
1cfac 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67  ..  assert( u.ag
1cfad 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70  .n==0 || (pOp->p
1cfae 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75  2>0 && pOp->p2+u
1cfaf 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  .ag.n<=p->nMem+1
1cfb0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1cfb1 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c  Op->p3<pOp->p2 |
1cfb2 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e  | pOp->p3>=pOp->
1cfb3 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75  p2+u.ag.n );.  u
1cfb4 2e 61 67 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d  .ag.pArg = &aMem
1cfb5 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72  [pOp->p2];.  for
1cfb6 28 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e  (u.ag.i=0; u.ag.
1cfb7 69 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69  i<u.ag.n; u.ag.i
1cfb8 2b 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29  ++, u.ag.pArg++)
1cfb9 7b 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c  {.    u.ag.apVal
1cfba 5b 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e  [u.ag.i] = u.ag.
1cfbb 70 41 72 67 3b 0a 20 20 20 20 73 71 6c 69 74 65  pArg;.    sqlite
1cfbc 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70  3VdbeMemStoreTyp
1cfbd 65 28 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20  e(u.ag.pArg);.  
1cfbe 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cfbf 28 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70  (pOp->p2, u.ag.p
1cfc0 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  Arg);.  }..  ass
1cfc1 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1cfc2 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20  ==P4_FUNCDEF || 
1cfc3 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1cfc4 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66  VDBEFUNC );.  if
1cfc5 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1cfc6 34 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20  4_FUNCDEF ){.   
1cfc7 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20   u.ag.ctx.pFunc 
1cfc8 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
1cfc9 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56  .    u.ag.ctx.pV
1cfca 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d  dbeFunc = 0;.  }
1cfcb 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63  else{.    u.ag.c
1cfcc 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28  tx.pVdbeFunc = (
1cfcd 56 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70  VdbeFunc*)pOp->p
1cfce 34 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20  4.pVdbeFunc;.   
1cfcf 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20   u.ag.ctx.pFunc 
1cfd0 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65  = u.ag.ctx.pVdbe
1cfd1 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d  Func->pFunc;.  }
1cfd2 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1cfd3 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1cfd4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1cfd5 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  Out = &aMem[pOp-
1cfd6 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78  >p3];.  u.ag.ctx
1cfd7 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
1cfd8 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e  ull;.  u.ag.ctx.
1cfd9 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61  s.db = db;.  u.a
1cfda 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30  g.ctx.s.xDel = 0
1cfdb 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a  ;.  u.ag.ctx.s.z
1cfdc 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f  Malloc = 0;..  /
1cfdd 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c  * The output cel
1cfde 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61  l may already ha
1cfdf 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  ve a buffer allo
1cfe0 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a  cated. Move.  **
1cfe1 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
1cfe2 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e  u.ag.ctx.s so in
1cfe3 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
1cfe4 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a  unction can use.
1cfe5 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79    ** the already
1cfe6 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1cfe7 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c  r instead of all
1cfe8 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e  ocating a new on
1cfe9 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e..  */.  sqlite
1cfea 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e  3VdbeMemMove(&u.
1cfeb 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b  ag.ctx.s, pOut);
1cfec 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1cfed 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d  g(&u.ag.ctx.s, M
1cfee 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61  EM_Null);..  u.a
1cfef 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20  g.ctx.isError = 
1cff0 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  0;.  if( u.ag.ct
1cff1 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26  x.pFunc->flags &
1cff2 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
1cff3 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73  DCOLL ){.    ass
1cff4 65 72 74 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a  ert( pOp>aOp );.
1cff5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
1cff6 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43  -1].p4type==P4_C
1cff7 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73  OLLSEQ );.    as
1cff8 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70  sert( pOp[-1].op
1cff9 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71  code==OP_CollSeq
1cffa 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78   );.    u.ag.ctx
1cffb 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
1cffc 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
1cffd 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1cffe 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1cfff 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1d000 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63  suse;.  (*u.ag.c
1d001 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29  tx.pFunc->xFunc)
1d002 28 26 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67  (&u.ag.ctx, u.ag
1d003 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b  .n, u.ag.apVal);
1d004 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1d005 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1d006 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d007 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74  Release(&u.ag.ct
1d008 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61  x.s);.    goto a
1d009 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1d00a 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  se;.  }.  if( db
1d00b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1d00c 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68  {.    /* Even th
1d00d 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20  ough a malloc() 
1d00e 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20  has failed, the 
1d00f 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1d010 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65  f the.    ** use
1d011 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68  r function may h
1d012 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71  ave called an sq
1d013 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58  lite3_result_XXX
1d014 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20  () function.    
1d015 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76  ** to return a v
1d016 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  alue. The follow
1d017 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65  ing call release
1d018 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a  s any resources.
1d019 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65      ** associate
1d01a 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61  d with such a va
1d01b 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lue..    **.    
1d01c 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d  ** Note: Maybe M
1d01d 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75  emRelease() shou
1d01e 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20  ld be called if 
1d01f 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1d020 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61  ).    ** fails a
1d021 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29  lso (the if(...)
1d022 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65   statement above
1d023 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65  ). But if people
1d024 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75   are.    ** misu
1d025 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65  sing sqlite, the
1d026 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72  y have bigger pr
1d027 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65  oblems than a le
1d028 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20  aked value..    
1d029 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1d02a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e  beMemRelease(&u.
1d02b 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67  ag.ctx.s);.    g
1d02c 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1d02d 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78  .  /* If any aux
1d02e 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e 63  iliary data func
1d02f 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
1d030 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75  called by this u
1d031 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20  ser function,.  
1d032 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63  ** immediately c
1d033 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
1d034 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73  or for any non-s
1d035 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20  tatic values..  
1d036 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  */.  if( u.ag.ct
1d037 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20  x.pVdbeFunc ){. 
1d038 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
1d039 6c 65 74 65 41 75 78 44 61 74 61 28 75 2e 61 67  leteAuxData(u.ag
1d03a 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20  .ctx.pVdbeFunc, 
1d03b 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f  pOp->p1);.    pO
1d03c 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20  p->p4.pVdbeFunc 
1d03d 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65  = u.ag.ctx.pVdbe
1d03e 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  Func;.    pOp->p
1d03f 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46  4type = P4_VDBEF
1d040 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  UNC;.  }..  /* I
1d041 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  f the function r
1d042 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72  eturned an error
1d043 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70  , throw an excep
1d044 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e  tion */.  if( u.
1d045 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29  ag.ctx.isError )
1d046 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1d047 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1d048 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1d049 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1d04a 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a  (&u.ag.ctx.s));.
1d04b 20 20 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74      rc = u.ag.ct
1d04c 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a  x.isError;.  }..
1d04d 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65    /* Copy the re
1d04e 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63  sult of the func
1d04f 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74  tion into regist
1d050 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74  er P3 */.  sqlit
1d051 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1d052 64 69 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73  ding(&u.ag.ctx.s
1d053 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73  , encoding);.  s
1d054 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1d055 65 28 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74  e(pOut, &u.ag.ct
1d056 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  x.s);.  if( sqli
1d057 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1d058 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f  (pOut) ){.    go
1d059 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1d05a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1d05b 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b  (pOp->p3, pOut);
1d05c 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1d05d 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
1d05e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d05f 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50  ode: BitAnd P1 P
1d060 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  2 P3 * *.**.** T
1d061 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65  ake the bit-wise
1d062 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75   AND of the valu
1d063 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
1d064 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20  1 and P2 and.** 
1d065 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1d066 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1d067 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
1d068 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
1d069 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1d06a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42  .*/./* Opcode: B
1d06b 69 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20  itOr P1 P2 P3 * 
1d06c 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
1d06d 20 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20   bit-wise OR of 
1d06e 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
1d06f 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32  gister P1 and P2
1d070 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68   and.** store th
1d071 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1d072 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1d073 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1d074 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1d075 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1d076 70 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74  pcode: ShiftLeft
1d077 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1d078 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e  .** Shift the in
1d079 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72  teger value in r
1d07a 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68  egister P2 to th
1d07b 65 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a  e left by the.**
1d07c 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
1d07d 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
1d07e 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69   integer in regi
1d07f 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65  ser P1..** Store
1d080 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1d081 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1d082 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1d083 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1d084 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1d085 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52  * Opcode: ShiftR
1d086 69 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20  ight P1 P2 P3 * 
1d087 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68  *.**.** Shift th
1d088 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
1d089 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74  in register P2 t
1d08a 6f 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74  o the right by t
1d08b 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
1d08c 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62  bits specified b
1d08d 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e  y the integer in
1d08e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1d08f 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
1d090 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1d091 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1d092 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1d093 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1d094 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74  ..*/.case OP_Bit
1d095 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  And:            
1d096 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d097 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20  TK_BITAND, in1, 
1d098 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1d099 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20  e OP_BitOr:     
1d09a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d09b 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52  same as TK_BITOR
1d09c 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1d09d 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66   */.case OP_Shif
1d09e 74 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20  tLeft:          
1d09f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d0a0 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69  K_LSHIFT, in1, i
1d0a1 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1d0a2 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20   OP_ShiftRight: 
1d0a3 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  {           /* s
1d0a4 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54  ame as TK_RSHIFT
1d0a5 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1d0a6 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1d0a7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1d0a8 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f  ved into u.ah */
1d0a9 0a 20 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20  .  i64 a;.  i64 
1d0aa 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1d0ab 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d0ac 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a  ed into u.ah */.
1d0ad 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1d0ae 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32  pOp->p1];.  pIn2
1d0af 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
1d0b0 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ];.  pOut = &aMe
1d0b1 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66  m[pOp->p3];.  if
1d0b2 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  ( (pIn1->flags |
1d0b3 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20   pIn2->flags) & 
1d0b4 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1d0b5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1d0b6 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20  tNull(pOut);.   
1d0b7 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e   break;.  }.  u.
1d0b8 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64  ah.a = sqlite3Vd
1d0b9 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
1d0ba 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c  ;.  u.ah.b = sql
1d0bb 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1d0bc 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68  (pIn1);.  switch
1d0bd 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1d0be 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74  .    case OP_Bit
1d0bf 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61  And:      u.ah.a
1d0c0 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20   &= u.ah.b;     
1d0c1 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1d0c2 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1d0c3 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62  u.ah.a |= u.ah.b
1d0c4 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1d0c5 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65   case OP_ShiftLe
1d0c6 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d  ft:   u.ah.a <<=
1d0c7 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61   u.ah.b;    brea
1d0c8 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
1d0c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
1d0ca 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69  code==OP_ShiftRi
1d0cb 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ght );.         
1d0cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d0cd 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e  u.ah.a >>= u.ah.
1d0ce 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  b;    break;.  }
1d0cf 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75  .  pOut->u.i = u
1d0d0 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54  .ah.a;.  MemSetT
1d0d1 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1d0d2 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1d0d3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1d0d4 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a  ddImm  P1 P2 * *
1d0d5 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68   *.** .** Add th
1d0d6 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f  e constant P2 to
1d0d7 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1d0d8 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68  gister P1..** Th
1d0d9 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61  e result is alwa
1d0da 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ys an integer..*
1d0db 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e  *.** To force an
1d0dc 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65  y register to be
1d0dd 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73   an integer, jus
1d0de 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65  t add 0..*/.case
1d0df 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20   OP_AddImm: {   
1d0e0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1d0e1 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1d0e2 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71  m[pOp->p1];.  sq
1d0e3 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
1d0e4 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  gerify(pIn1);.  
1d0e5 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70  pIn1->u.i += pOp
1d0e6 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ->p2;.  break;.}
1d0e7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73  ../* Opcode: Mus
1d0e8 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a  tBeInt P1 P2 * *
1d0e9 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20   *.** .** Force 
1d0ea 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1d0eb 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61  ister P1 to be a
1d0ec 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74  n integer.  If t
1d0ed 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50  he value.** in P
1d0ee 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  1 is not an inte
1d0ef 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62  ger and cannot b
1d0f0 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
1d0f1 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77   an integer.** w
1d0f2 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73  ithout data loss
1d0f3 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65  , then jump imme
1d0f4 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f  diately to P2, o
1d0f5 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61  r if P2==0.** ra
1d0f6 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49  ise an SQLITE_MI
1d0f7 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e  SMATCH exception
1d0f8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73  ..*/.case OP_Mus
1d0f9 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  tBeInt: {       
1d0fa 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1d0fb 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1d0fc 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1d0fd 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
1d0fe 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  n1, SQLITE_AFF_N
1d0ff 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67  UMERIC, encoding
1d100 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  );.  if( (pIn1->
1d101 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
1d102 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
1d103 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20  Op->p2==0 ){.   
1d104 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
1d105 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67  ISMATCH;.      g
1d106 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1d107 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  _error;.    }els
1d108 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  e{.      pc = pO
1d109 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
1d10a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65  .  }else{.    Me
1d10b 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e  mSetTypeFlag(pIn
1d10c 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  1, MEM_Int);.  }
1d10d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d10e 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69  Opcode: RealAffi
1d10f 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  nity P1 * * * *.
1d110 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65  **.** If registe
1d111 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e  r P1 holds an in
1d112 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74  teger convert it
1d113 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65   to a real value
1d114 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1d115 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e  ode is used when
1d116 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f   extracting info
1d117 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63  rmation from a c
1d118 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
1d119 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e  s REAL affinity.
1d11a 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61    Such column va
1d11b 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  lues may still b
1d11c 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69  e stored as.** i
1d11d 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61  ntegers, for spa
1d11e 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62  ce efficiency, b
1d11f 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74  ut after extract
1d120 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d  ion we want them
1d121 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  .** to have only
1d122 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a   a real value..*
1d123 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66  /.case OP_RealAf
1d124 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20  finity: {       
1d125 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1d126 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1d127 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1d128 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1d129 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1d12a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1d12b 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  ealify(pIn1);.  
1d12c 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69  }.  break;.}..#i
1d12d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d12e 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65  T_CAST./* Opcode
1d12f 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20  : ToText P1 * * 
1d130 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20  * *.**.** Force 
1d131 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1d132 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74  ister P1 to be t
1d133 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76  ext..** If the v
1d134 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c  alue is numeric,
1d135 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1d136 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68   string using th
1d137 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
1d138 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c  of printf().  Bl
1d139 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e  ob values are un
1d13a 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61  changed and.** a
1d13b 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69  re afterwards si
1d13c 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64  mply interpreted
1d13d 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   as text..**.** 
1d13e 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  A NULL value is 
1d13f 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74  not changed by t
1d140 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74  his routine.  It
1d141 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a   remains NULL..*
1d142 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74  /.case OP_ToText
1d143 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1d144 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d145 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20  TK_TO_TEXT, in1 
1d146 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1d147 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
1d148 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1d149 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b  MEM_Null ) break
1d14a 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f  ;.  assert( MEM_
1d14b 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e  Str==(MEM_Blob>>
1d14c 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c  3) );.  pIn1->fl
1d14d 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c  ags |= (pIn1->fl
1d14e 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33  ags&MEM_Blob)>>3
1d14f 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74  ;.  applyAffinit
1d150 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
1d151 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e  FF_TEXT, encodin
1d152 67 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e  g);.  rc = Expan
1d153 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61  dBlob(pIn1);.  a
1d154 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1d155 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20  gs & MEM_Str || 
1d156 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d157 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67   );.  pIn1->flag
1d158 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d  s &= ~(MEM_Int|M
1d159 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62  EM_Real|MEM_Blob
1d15a 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50  |MEM_Zero);.  UP
1d15b 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1d15c 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b  E(pIn1);.  break
1d15d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d15e 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20  ToBlob P1 * * * 
1d15f 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1d160 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d161 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42  ter P1 to be a B
1d162 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76  LOB..** If the v
1d163 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c  alue is numeric,
1d164 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1d165 20 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a   string first..*
1d166 2a 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69  * Strings are si
1d167 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74  mply reinterpret
1d168 65 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68  ed as blobs with
1d169 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f   no change.** to
1d16a 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1d16b 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  data..**.** A NU
1d16c 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1d16d 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1d16e 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1d16f 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1d170 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20  se OP_ToBlob: { 
1d171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d172 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1d173 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20  O_BLOB, in1 */. 
1d174 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
1d175 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 49  p->p1];.  if( pI
1d176 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1d177 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20  Null ) break;.  
1d178 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1d179 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20   & MEM_Blob)==0 
1d17a 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  ){.    applyAffi
1d17b 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54  nity(pIn1, SQLIT
1d17c 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f  E_AFF_TEXT, enco
1d17d 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72  ding);.    asser
1d17e 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  t( pIn1->flags &
1d17f 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e   MEM_Str || db->
1d180 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1d181 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1d182 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f  ag(pIn1, MEM_Blo
1d183 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  b);.  }else{.   
1d184 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20   pIn1->flags &= 
1d185 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e  ~(MEM_TypeMask&~
1d186 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20  MEM_Blob);.  }. 
1d187 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1d188 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72  SIZE(pIn1);.  br
1d189 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d18a 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20  e: ToNumeric P1 
1d18b 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f  * * * *.**.** Fo
1d18c 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
1d18d 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1d18e 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68  be numeric (eith
1d18f 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72  er an.** integer
1d190 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70   or a floating-p
1d191 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a  oint number.).**
1d192 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
1d193 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74   text or blob, t
1d194 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74  ry to convert it
1d195 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65   to an using the
1d196 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
1d197 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66  f atoi() or atof
1d198 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69  () and store 0 i
1d199 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72  f no such conver
1d19a 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73  sion .** is poss
1d19b 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ible..**.** A NU
1d19c 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1d19d 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1d19e 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1d19f 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1d1a0 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a  se OP_ToNumeric:
1d1a1 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1d1a2 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d1a3 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e  K_TO_NUMERIC, in
1d1a4 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  1 */.  pIn1 = &a
1d1a5 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1d1a6 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1d1a7 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d   & (MEM_Null|MEM
1d1a8 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d  _Int|MEM_Real))=
1d1a9 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1d1aa 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79  3VdbeMemNumerify
1d1ab 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
1d1ac 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1d1ad 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
1d1ae 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  T */../* Opcode:
1d1af 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20   ToInt P1 * * * 
1d1b0 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1d1b1 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d1b2 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e 74  ter P1 be an int
1d1b3 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65  eger.  If.** The
1d1b4 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e   value is curren
1d1b5 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 65  tly a real numbe
1d1b6 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 63  r, drop its frac
1d1b7 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20  tional part..** 
1d1b8 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1d1b9 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72  text or blob, tr
1d1ba 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
1d1bb 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73  to an integer us
1d1bc 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
1d1bd 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20  alent of atoi() 
1d1be 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
1d1bf 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
1d1c0 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  n is possible..*
1d1c1 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
1d1c2 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
1d1c3 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1d1c4 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
1d1c5 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
1d1c6 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  oInt: {         
1d1c7 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d1c8 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69   as TK_TO_INT, i
1d1c9 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26  n1 */.  pIn1 = &
1d1ca 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1d1cb 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1d1cc 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1d1cd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1d1ce 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1d1cf 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
1d1d0 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  eak;.}..#ifndef 
1d1d1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
1d1d2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65  ./* Opcode: ToRe
1d1d3 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  al P1 * * * *.**
1d1d4 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1d1d5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1d1d6 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74  P1 to be a float
1d1d7 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing point number
1d1d8 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75  ..** If The valu
1d1d9 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  e is currently a
1d1da 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65  n integer, conve
1d1db 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65  rt it..** If the
1d1dc 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
1d1dd 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
1d1de 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
1d1df 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
1d1e0 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
1d1e1 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
1d1e2 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75  ore 0.0 if no su
1d1e3 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  ch conversion is
1d1e4 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a   possible..**.**
1d1e5 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1d1e6 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1d1e7 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1d1e8 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1d1e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61  */.case OP_ToRea
1d1ea 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  l: {            
1d1eb 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1d1ec 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31   TK_TO_REAL, in1
1d1ed 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d   */.  pIn1 = &aM
1d1ee 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69  em[pOp->p1];.  i
1d1ef 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1d1f0 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1d1f1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1d1f2 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
1d1f3 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1d1f4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d1f5 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
1d1f6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50  ./* Opcode: Lt P
1d1f7 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1d1f8 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
1d1f9 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1d1fa 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49  er P1 and P3.  I
1d1fb 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31  f reg(P3)<reg(P1
1d1fc 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74  ) then.** jump t
1d1fd 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a  o address P2.  .
1d1fe 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
1d1ff 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62  ITE_JUMPIFNULL b
1d200 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20  it of P5 is set 
1d201 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50  and either reg(P
1d202 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29  1) or.** reg(P3)
1d203 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
1d204 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66  ke the jump.  If
1d205 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1d206 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69  IFNULL .** bit i
1d207 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c  s clear then fal
1d208 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72  l thru if either
1d209 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d20a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
1d20b 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74  TE_AFF_MASK port
1d20c 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62  ion of P5 must b
1d20d 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  e an affinity ch
1d20e 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c  aracter -.** SQL
1d20f 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51  ITE_AFF_TEXT, SQ
1d210 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1d211 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1d212 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
1d213 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65  de .** to coerce
1d214 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63   both inputs acc
1d215 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61  ording to this a
1d216 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74  ffinity before t
1d217 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  he.** comparison
1d218 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65   is made. If the
1d219 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
1d21a 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e   is 0x00, then n
1d21b 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69  umeric.** affini
1d21c 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65  ty is used. Note
1d21d 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69   that the affini
1d21e 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  ty conversions a
1d21f 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63  re stored.** bac
1d220 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74  k into the input
1d221 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d222 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f  d P3.  So this o
1d223 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a  pcode can cause.
1d224 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68  ** persistent ch
1d225 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65  anges to registe
1d226 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a  rs P1 and P3..**
1d227 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e  .** Once any con
1d228 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61  versions have ta
1d229 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e  ken place, and n
1d22a 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20  either value is 
1d22b 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61  NULL, .** the va
1d22c 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65  lues are compare
1d22d 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  d. If both value
1d22e 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e  s are blobs then
1d22f 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20   memcmp() is.** 
1d230 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1d231 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
1d232 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
1d233 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d234 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68  .** are text, th
1d235 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
1d236 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  te collating fun
1d237 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
1d238 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  in.** P4 is  use
1d239 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
1d23a 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
1d23b 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
1d23c 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29  then.** memcmp()
1d23d 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
1d23e 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e  are text string.
1d23f 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d240 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c   are.** numeric,
1d241 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20   then a numeric 
1d242 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73  comparison is us
1d243 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76  ed. If the two v
1d244 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20  alues.** are of 
1d245 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c  different types,
1d246 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72   then numbers ar
1d247 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d248 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67  s than.** string
1d249 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72  s and strings ar
1d24a 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d24b 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a  s than blobs..**
1d24c 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
1d24d 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66  E_STOREP2 bit of
1d24e 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
1d24f 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49   do not jump.  I
1d250 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65  nstead,.** store
1d251 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c   a boolean resul
1d252 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20  t (either 0, or 
1d253 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72  1, or NULL) in r
1d254 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f  egister P2..*/./
1d255 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20  * Opcode: Ne P1 
1d256 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1d257 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
1d258 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
1d259 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
1d25a 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
1d25b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65  en if.** the ope
1d25c 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65  rands in registe
1d25d 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65  rs P1 and P3 are
1d25e 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65   not equal.  See
1d25f 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
1d260 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  or.** additional
1d261 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
1d262 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55  .** If SQLITE_NU
1d263 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50  LLEQ is set in P
1d264 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  5 then the resul
1d265 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20  t of comparison 
1d266 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72  is always either
1d267 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73  .** true or fals
1d268 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e  e and is never N
1d269 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70  ULL.  If both op
1d26a 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20  erands are NULL 
1d26b 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a  then the result.
1d26c 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e  ** of comparison
1d26d 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65   is false.  If e
1d26e 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d26f 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1d270 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a  esult is true..*
1d271 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65  * If neither ope
1d272 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
1d273 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74   the result is t
1d274 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f  he same as it wo
1d275 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65  uld be if.** the
1d276 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66   SQLITE_NULLEQ f
1d277 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64  lag were omitted
1d278 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20   from P5..*/./* 
1d279 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32  Opcode: Eq P1 P2
1d27a 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1d27b 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1d27c 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1d27d 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1d27e 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1d27f 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61   if.** the opera
1d280 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73  nds in registers
1d281 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65   P1 and P3 are e
1d282 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65  qual..** See the
1d283 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
1d284 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1d285 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ation..**.** If 
1d286 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73  SQLITE_NULLEQ is
1d287 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20   set in P5 then 
1d288 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f  the result of co
1d289 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61  mparison is alwa
1d28a 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75  ys either.** tru
1d28b 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69  e or false and i
1d28c 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49  s never NULL.  I
1d28d 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20  f both operands 
1d28e 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68  are NULL then th
1d28f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63  e result.** of c
1d290 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75  omparison is tru
1d291 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70  e.  If either op
1d292 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1d293 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1d294 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65   false..** If ne
1d295 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d296 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65   NULL the the re
1d297 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65  sult is the same
1d298 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
1d299 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  if.** the SQLITE
1d29a 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72  _NULLEQ flag wer
1d29b 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50  e omitted from P
1d29c 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  5..*/./* Opcode:
1d29d 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Le P1 P2 P3 P4 
1d29e 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1d29f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1d2a0 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1d2a1 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1d2a2 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1d2a3 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d2a4 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65  egister P3 is le
1d2a5 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1d2a6 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
1d2a7 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
1d2a8 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
1d2a9 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1d2aa 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1d2ab 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d2ac 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Gt P1 P2 P3 P4 P
1d2ad 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1d2ae 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1d2af 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1d2b0 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1d2b1 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1d2b2 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1d2b3 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65  gister P3 is gre
1d2b4 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f  ater than the co
1d2b5 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69  ntent of.** regi
1d2b6 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68  ster P1.  See th
1d2b7 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20  e Lt opcode for 
1d2b8 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
1d2b9 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70  mation..*/./* Op
1d2ba 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50  code: Ge P1 P2 P
1d2bb 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68  3 P4 P5.**.** Th
1d2bc 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  is works just li
1d2bd 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ke the Lt opcode
1d2be 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
1d2bf 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
1d2c0 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  f.** the content
1d2c1 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1d2c2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1d2c3 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1d2c4 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
1d2c5 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
1d2c6 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1d2c7 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1d2c8 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
1d2c9 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20  e OP_Eq:        
1d2ca 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d2cb 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69  s TK_EQ, jump, i
1d2cc 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
1d2cd 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20  OP_Ne:          
1d2ce 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d2cf 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_NE, jump, in1
1d2d0 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50  , in3 */.case OP
1d2d1 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Lt:            
1d2d2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1d2d3 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20  _LT, jump, in1, 
1d2d4 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c  in3 */.case OP_L
1d2d5 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e:              
1d2d6 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
1d2d7 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  E, jump, in1, in
1d2d8 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a  3 */.case OP_Gt:
1d2d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d2da 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c  * same as TK_GT,
1d2db 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1d2dc 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b  */.case OP_Ge: {
1d2dd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d2de 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a  same as TK_GE, j
1d2df 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1d2e0 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d2e1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d2e2 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20   into u.ai */.  
1d2e3 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20  int res;        
1d2e4 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
1d2e5 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1d2e6 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20  of pIn1 against 
1d2e7 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61  pIn3 */.  char a
1d2e8 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a  ffinity;      /*
1d2e9 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
1d2ea 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20   for comparison 
1d2eb 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1d2ec 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d2ed 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a  ed into u.ai */.
1d2ee 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1d2ef 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33  pOp->p1];.  pIn3
1d2f0 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1d2f1 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  ];.  if( (pIn1->
1d2f2 66 6c 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c  flags | pIn3->fl
1d2f3 61 67 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  ags)&MEM_Null ){
1d2f4 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62  .    /* One or b
1d2f5 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65  oth operands are
1d2f6 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28   NULL */.    if(
1d2f7 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
1d2f8 45 5f 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20  E_NULLEQ ){.    
1d2f9 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e    /* If SQLITE_N
1d2fa 55 4c 4c 45 51 20 69 73 20 73 65 74 20 28 77 68  ULLEQ is set (wh
1d2fb 69 63 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61  ich will only ha
1d2fc 70 70 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72  ppen if the oper
1d2fd 61 74 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a  ator is.      **
1d2fe 20 4f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29   OP_Eq or OP_Ne)
1d2ff 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
1d300 75 6d 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e  ump or not depen
1d301 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a  ding on whether.
1d302 20 20 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20        ** or not 
1d303 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72  both operands ar
1d304 65 20 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f  e null..      */
1d305 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1d306 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45  Op->opcode==OP_E
1d307 71 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  q || pOp->opcode
1d308 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
1d309 20 75 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e   u.ai.res = (pIn
1d30a 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d  1->flags & pIn3-
1d30b 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d30c 6c 29 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65  l)==0;.    }else
1d30d 7b 0a 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54  {.      /* SQLIT
1d30e 45 5f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61  E_NULLEQ is clea
1d30f 72 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f  r and at least o
1d310 6e 65 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  ne operand is NU
1d311 4c 4c 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  LL,.      ** the
1d312 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1d313 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20  always NULL..   
1d314 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69     ** The jump i
1d315 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53  s taken if the S
1d316 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1d317 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20   bit is set..   
1d318 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
1d319 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45  pOp->p5 & SQLITE
1d31a 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20  _STOREP2 ){.    
1d31b 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d      pOut = &aMem
1d31c 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1d31d 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1d31e 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pOut, MEM_Null
1d31f 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53  );.        REGIS
1d320 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1d321 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  2, pOut);.      
1d322 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
1d323 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  5 & SQLITE_JUMPI
1d324 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  FNULL ){.       
1d325 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1d326 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1d327 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  reak;.    }.  }e
1d328 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74  lse{.    /* Neit
1d329 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
1d32a 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61  ULL.  Do a compa
1d32b 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e  rison. */.    u.
1d32c 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f  ai.affinity = pO
1d32d 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41  p->p5 & SQLITE_A
1d32e 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28  FF_MASK;.    if(
1d32f 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 29   u.ai.affinity )
1d330 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66  {.      applyAff
1d331 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 69  inity(pIn1, u.ai
1d332 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64  .affinity, encod
1d333 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70 6c  ing);.      appl
1d334 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20  yAffinity(pIn3, 
1d335 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65  u.ai.affinity, e
1d336 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20 20  ncoding);.      
1d337 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1d338 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  iled ) goto no_m
1d339 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61  em;.    }..    a
1d33a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1d33b 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c  pe==P4_COLLSEQ |
1d33c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d  | pOp->p4.pColl=
1d33d 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64  =0 );.    Expand
1d33e 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20 20  Blob(pIn1);.    
1d33f 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29  ExpandBlob(pIn3)
1d340 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d  ;.    u.ai.res =
1d341 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
1d342 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70  re(pIn3, pIn1, p
1d343 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20  Op->p4.pColl);. 
1d344 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70   }.  switch( pOp
1d345 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20  ->opcode ){.    
1d346 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 75  case OP_Eq:    u
1d347 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72  .ai.res = u.ai.r
1d348 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es==0;     break
1d349 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65  ;.    case OP_Ne
1d34a 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20  :    u.ai.res = 
1d34b 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 20  u.ai.res!=0;    
1d34c 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
1d34d 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e   OP_Lt:    u.ai.
1d34e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30  res = u.ai.res<0
1d34f 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;      break;.  
1d350 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20    case OP_Le:   
1d351 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69   u.ai.res = u.ai
1d352 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65  .res<=0;     bre
1d353 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f  ak;.    case OP_
1d354 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20  Gt:    u.ai.res 
1d355 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20  = u.ai.res>0;   
1d356 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
1d357 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e 61  fault:       u.a
1d358 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73  i.res = u.ai.res
1d359 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  >=0;     break;.
1d35a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e    }..  if( pOp->
1d35b 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52  p5 & SQLITE_STOR
1d35c 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20  EP2 ){.    pOut 
1d35d 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
1d35e 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1d35f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1d360 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  nt);.    pOut->u
1d361 2e 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20  .i = u.ai.res;. 
1d362 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1d363 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1d364 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e  ;.  }else if( u.
1d365 61 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63  ai.res ){.    pc
1d366 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
1d367 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d368 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61   Opcode: Permuta
1d369 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  tion * * * P4 *.
1d36a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65  **.** Set the pe
1d36b 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62  rmutation used b
1d36c 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65  y the OP_Compare
1d36d 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20   operator to be 
1d36e 74 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20  the array.** of 
1d36f 69 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a  integers in P4..
1d370 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74  **.** The permut
1d371 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61  ation is only va
1d372 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65  lid until the ne
1d373 78 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  xt OP_Permutatio
1d374 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a  n, OP_Compare,.*
1d375 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50  * OP_Halt, or OP
1d376 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70  _ResultRow.  Typ
1d377 69 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65  ically the OP_Pe
1d378 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  rmutation should
1d379 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69   occur.** immedi
1d37a 61 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74  ately prior to t
1d37b 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a  he OP_Compare..*
1d37c 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74  /.case OP_Permut
1d37d 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72  ation: {.  asser
1d37e 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1d37f 50 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20  P4_INTARRAY );. 
1d380 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1d381 2e 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74  .ai );.  aPermut
1d382 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a  e = pOp->p4.ai;.
1d383 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d384 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50  pcode: Compare P
1d385 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1d386 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65  ** Compare to ve
1d387 63 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65  ctors of registe
1d388 72 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72  rs in reg(P1)..r
1d389 65 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c  eg(P1+P3-1) (all
1d38a 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22   this.** one "A"
1d38b 29 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29  ) and in reg(P2)
1d38c 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28  ..reg(P2+P3-1) (
1d38d 22 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20  "B").  Save the 
1d38e 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65  result of.** the
1d38f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20   comparison for 
1d390 75 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20  use by the next 
1d391 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74  OP_Jump instruct
1d392 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  ..**.** P4 is a 
1d393 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1d394 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63  e that defines c
1d395 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1d396 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f  es and sort.** o
1d397 72 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f  rders for the co
1d398 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70  mparison.  The p
1d399 65 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69  ermutation appli
1d39a 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a  es to registers.
1d39b 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65  ** only.  The Ke
1d39c 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61  yInfo elements a
1d39d 72 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69  re used sequenti
1d39e 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ally..**.** The 
1d39f 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20  comparison is a 
1d3a0 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c  sort comparison,
1d3a1 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72   so NULLs compar
1d3a2 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c  e equal,.** NULL
1d3a3 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20  s are less than 
1d3a4 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73  numbers, numbers
1d3a5 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73   are less than s
1d3a6 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73  trings,.** and s
1d3a7 74 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20  trings are less 
1d3a8 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63  than blobs..*/.c
1d3a9 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20  ase OP_Compare: 
1d3aa 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1d3ab 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d3ac 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20  d into u.aj */. 
1d3ad 20 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b   int n;.  int i;
1d3ae 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74  .  int p1;.  int
1d3af 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79   p2;.  const Key
1d3b0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a  Info *pKeyInfo;.
1d3b1 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c    int idx;.  Col
1d3b2 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20  lSeq *pColl;    
1d3b3 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
1d3b4 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20  uence to use on 
1d3b5 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69  this term */.  i
1d3b6 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20  nt bRev;        
1d3b7 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45    /* True for DE
1d3b8 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72  SCENDING sort or
1d3b9 64 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  der */.#endif /*
1d3ba 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d3bb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a   moved into u.aj
1d3bc 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20   */..  u.aj.n = 
1d3bd 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e  pOp->p3;.  u.aj.
1d3be 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
1d3bf 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61  p4.pKeyInfo;.  a
1d3c0 73 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20  ssert( u.aj.n>0 
1d3c1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  );.  assert( u.a
1d3c2 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b  j.pKeyInfo!=0 );
1d3c3 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70  .  u.aj.p1 = pOp
1d3c4 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20  ->p1;.  u.aj.p2 
1d3c5 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53  = pOp->p2;.#if S
1d3c6 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
1d3c7 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20  ( aPermute ){.  
1d3c8 20 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b    int k, mx = 0;
1d3c9 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c  .    for(k=0; k<
1d3ca 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28  u.aj.n; k++) if(
1d3cb 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20   aPermute[k]>mx 
1d3cc 29 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b  ) mx = aPermute[
1d3cd 6b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  k];.    assert( 
1d3ce 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61  u.aj.p1>0 && u.a
1d3cf 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d  j.p1+mx<=p->nMem
1d3d0 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +1 );.    assert
1d3d1 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75  ( u.aj.p2>0 && u
1d3d2 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d  .aj.p2+mx<=p->nM
1d3d3 65 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  em+1 );.  }else{
1d3d4 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61  .    assert( u.a
1d3d5 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70  j.p1>0 && u.aj.p
1d3d6 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65  1+u.aj.n<=p->nMe
1d3d7 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  m+1 );.    asser
1d3d8 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20  t( u.aj.p2>0 && 
1d3d9 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d  u.aj.p2+u.aj.n<=
1d3da 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d  p->nMem+1 );.  }
1d3db 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d3dc 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72  E_DEBUG */.  for
1d3dd 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e  (u.aj.i=0; u.aj.
1d3de 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69  i<u.aj.n; u.aj.i
1d3df 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64  ++){.    u.aj.id
1d3e0 78 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61  x = aPermute ? a
1d3e1 50 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20  Permute[u.aj.i] 
1d3e2 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45  : u.aj.i;.    RE
1d3e3 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61  GISTER_TRACE(u.a
1d3e4 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26  j.p1+u.aj.idx, &
1d3e5 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61  aMem[u.aj.p1+u.a
1d3e6 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47  j.idx]);.    REG
1d3e7 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a  ISTER_TRACE(u.aj
1d3e8 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61  .p2+u.aj.idx, &a
1d3e9 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a  Mem[u.aj.p2+u.aj
1d3ea 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65  .idx]);.    asse
1d3eb 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e  rt( u.aj.i<u.aj.
1d3ec 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1d3ed 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f   );.    u.aj.pCo
1d3ee 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e  ll = u.aj.pKeyIn
1d3ef 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69  fo->aColl[u.aj.i
1d3f0 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76  ];.    u.aj.bRev
1d3f1 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f   = u.aj.pKeyInfo
1d3f2 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61  ->aSortOrder[u.a
1d3f3 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61  j.i];.    iCompa
1d3f4 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  re = sqlite3MemC
1d3f5 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61  ompare(&aMem[u.a
1d3f6 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20  j.p1+u.aj.idx], 
1d3f7 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  &aMem[u.aj.p2+u.
1d3f8 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43  aj.idx], u.aj.pC
1d3f9 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43  oll);.    if( iC
1d3fa 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20  ompare ){.      
1d3fb 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20  if( u.aj.bRev ) 
1d3fc 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1d3fd 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
1d3fe 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
1d3ff 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
1d400 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d401 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
1d402 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1d403 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
1d404 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
1d405 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
1d406 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
1d407 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1d408 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1d409 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1d40a 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1d40b 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1d40c 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1d40d 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
1d40e 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
1d40f 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
1d410 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
1d411 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1d412 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
1d413 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1d414 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
1d415 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
1d416 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1d417 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1d418 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1d419 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1d41a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d41b 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1d41c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1d41d 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
1d41e 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1d41f 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d420 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
1d421 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
1d422 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1d423 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1d424 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
1d425 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
1d426 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
1d427 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1d428 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1d429 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1d42a 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1d42b 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1d42c 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1d42d 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
1d42e 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1d42f 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
1d430 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1d431 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1d432 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1d433 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
1d434 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1d435 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1d436 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
1d437 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1d438 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1d439 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1d43a 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1d43b 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1d43c 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1d43d 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
1d43e 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
1d43f 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
1d440 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d441 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
1d442 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1d443 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
1d444 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d445 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
1d446 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1d447 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d448 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d449 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20  k */.  int v1;  
1d44a 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e    /* Left operan
1d44b 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  d:  0==FALSE, 1=
1d44c 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1d44d 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  N or NULL */.  i
1d44e 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
1d44f 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
1d450 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1d451 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1d452 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  L */.#endif /* l
1d453 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d454 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a  oved into u.ak *
1d455 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  /..  pIn1 = &aMe
1d456 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
1d457 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
1d458 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1d459 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d  u.ak.v1 = 2;.  }
1d45a 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76  else{.    u.ak.v
1d45b 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  1 = sqlite3VdbeI
1d45c 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  ntValue(pIn1)!=0
1d45d 3b 0a 20 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26  ;.  }.  pIn2 = &
1d45e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1d45f 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73   if( pIn2->flags
1d460 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1d461 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a     u.ak.v2 = 2;.
1d462 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61    }else{.    u.a
1d463 6b 2e 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64  k.v2 = sqlite3Vd
1d464 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
1d465 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  !=0;.  }.  if( p
1d466 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41  Op->opcode==OP_A
1d467 6e 64 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  nd ){.    static
1d468 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1d469 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d  char and_logic[]
1d46a 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c   = { 0, 0, 0, 0,
1d46b 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d   1, 2, 0, 2, 2 }
1d46c 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20  ;.    u.ak.v1 = 
1d46d 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76  and_logic[u.ak.v
1d46e 31 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20  1*3+u.ak.v2];.  
1d46f 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69  }else{.    stati
1d470 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
1d471 20 63 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d   char or_logic[]
1d472 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c   = { 0, 1, 2, 1,
1d473 20 31 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d   1, 1, 2, 1, 2 }
1d474 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20  ;.    u.ak.v1 = 
1d475 6f 72 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31  or_logic[u.ak.v1
1d476 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d  *3+u.ak.v2];.  }
1d477 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b  .  pOut = &aMem[
1d478 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1d479 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20  u.ak.v1==2 ){.  
1d47a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1d47b 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pOut, MEM_Null)
1d47c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1d47d 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e  Out->u.i = u.ak.
1d47e 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  v1;.    MemSetTy
1d47f 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1d480 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65  _Int);.  }.  bre
1d481 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1d482 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20  : Not P1 P2 * * 
1d483 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  *.**.** Interpre
1d484 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  t the value in r
1d485 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20  egister P1 as a 
1d486 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20  boolean value.  
1d487 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f  Store the.** boo
1d488 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20  lean complement 
1d489 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1d48a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1d48b 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d48c 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61  .** NULL, then a
1d48d 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20   NULL is stored 
1d48e 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  in P2..*/.case O
1d48f 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  P_Not: {        
1d490 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d491 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20  as TK_NOT, in1, 
1d492 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d  out2 */.  pIn1 =
1d493 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1d494 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b  .  pOut = &aMem[
1d495 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20  pOp->p2];.  if( 
1d496 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1d497 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71  M_Null ){.    sq
1d498 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1d499 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c  ull(pOut);.  }el
1d49a 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se{.    sqlite3V
1d49b 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70  dbeMemSetInt64(p
1d49c 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62  Out, !sqlite3Vdb
1d49d 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29  eIntValue(pIn1))
1d49e 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d49f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74  ../* Opcode: Bit
1d4a0 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Not P1 P2 * * *.
1d4a1 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
1d4a2 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d4a3 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e  egister P1 as an
1d4a4 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65   integer.  Store
1d4a5 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d   the.** ones-com
1d4a6 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50  plement of the P
1d4a7 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67  1 value into reg
1d4a8 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31  ister P2.  If P1
1d4a9 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c   holds.** a NULL
1d4aa 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55   then store a NU
1d4ab 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73  LL in P2..*/.cas
1d4ac 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20  e OP_BitNot: {  
1d4ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1d4ae 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c  me as TK_BITNOT,
1d4af 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20   in1, out2 */.  
1d4b0 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1d4b1 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20  ->p1];.  pOut = 
1d4b2 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  &aMem[pOp->p2];.
1d4b3 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1d4b4 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1d4b5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d4b6 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
1d4b7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
1d4b8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
1d4b9 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69  nt64(pOut, ~sqli
1d4ba 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1d4bb 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72  pIn1));.  }.  br
1d4bc 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d4bd 65 3a 20 49 66 20 50 31 20 50 32 20 50 33 20 2a  e: If P1 P2 P3 *
1d4be 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d4bf 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d4c0 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d4c1 69 73 20 74 72 75 65 2e 20 20 54 68 65 20 76 61  is true.  The va
1d4c2 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e  lue is.** is con
1d4c3 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20  sidered true if 
1d4c4 69 74 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e  it is numeric an
1d4c5 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  d non-zero.  If 
1d4c6 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
1d4c7 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P1 is NULL then 
1d4c8 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1d4c9 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a   P3 is true..*/.
1d4ca 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74  /* Opcode: IfNot
1d4cb 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1d4cc 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69  .** Jump to P2 i
1d4cd 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  f the value in r
1d4ce 65 67 69 73 74 65 72 20 50 31 20 69 73 20 46 61  egister P1 is Fa
1d4cf 6c 73 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20  lse.  The value 
1d4d0 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  is.** is conside
1d4d1 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 68  red true if it h
1d4d2 61 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c  as a numeric val
1d4d3 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20  ue of zero.  If 
1d4d4 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20  the value.** in 
1d4d5 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P1 is NULL then 
1d4d6 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1d4d7 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a   P3 is true..*/.
1d4d8 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20  case OP_If:     
1d4d9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1d4da 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65  ump, in1 */.case
1d4db 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20   OP_IfNot: {    
1d4dc 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1d4dd 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in1 */.#if 0  /
1d4de 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d4df 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d4e0 6c 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65  l */.  int c;.#e
1d4e1 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d4e2 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d4e3 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e  to u.al */.  pIn
1d4e4 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
1d4e5 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e  1];.  if( pIn1->
1d4e6 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1d4e7 20 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d   ){.    u.al.c =
1d4e8 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73   pOp->p3;.  }els
1d4e9 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e{.#ifdef SQLITE
1d4ea 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
1d4eb 4f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20  OINT.    u.al.c 
1d4ec 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1d4ed 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a  Value(pIn1)!=0;.
1d4ee 23 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63  #else.    u.al.c
1d4ef 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1d4f0 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30  alValue(pIn1)!=0
1d4f1 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69  .0;.#endif.    i
1d4f2 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1d4f3 4f 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e  OP_IfNot ) u.al.
1d4f4 63 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d  c = !u.al.c;.  }
1d4f5 0a 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b  .  if( u.al.c ){
1d4f6 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1d4f7 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  2-1;.  }.  break
1d4f8 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d4f9 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a  IsNull P1 P2 * *
1d4fa 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d4fb 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d4fc 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d4fd 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  is NULL..*/.case
1d4fe 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20   OP_IsNull: {   
1d4ff 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d500 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a   as TK_ISNULL, j
1d501 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49  ump, in1 */.  pI
1d502 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
1d503 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31  p1];.  if( (pIn1
1d504 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1d505 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 63  ll)!=0 ){.    pc
1d506 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1d507 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d508 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75  /* Opcode: NotNu
1d509 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ll P1 P2 * * *.*
1d50a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20  *.** Jump to P2 
1d50b 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  if the value in 
1d50c 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e  register P1 is n
1d50d 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61  ot NULL.  .*/.ca
1d50e 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b  se OP_NotNull: {
1d50f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d510 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c  ame as TK_NOTNUL
1d511 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  L, jump, in1 */.
1d512 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1d513 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28  Op->p1];.  if( (
1d514 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1d515 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Null)==0 ){.  
1d516 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1d517 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1d518 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1d519 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 50  olumn P1 P2 P3 P
1d51a 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72  4 P5.**.** Inter
1d51b 70 72 65 74 20 74 68 65 20 64 61 74 61 20 74 68  pret the data th
1d51c 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69  at cursor P1 poi
1d51d 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 72 75  nts to as a stru
1d51e 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 69 6e  cture built usin
1d51f 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65 63  g.** the MakeRec
1d520 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ord instruction.
1d521 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b 65 52    (See the MakeR
1d522 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f 72  ecord opcode for
1d523 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
1d524 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1d525 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   the format of t
1d526 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 72 61  he data.)  Extra
1d527 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c  ct the P2-th col
1d528 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73  umn.** from this
1d529 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 68 65   record.  If the
1d52a 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 74  re are less that
1d52b 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75   (P2+1) .** valu
1d52c 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
1d52d 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c  , extract a NULL
1d52e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
1d52f 65 20 65 78 74 72 61 63 74 65 64 20 69 73 20 73  e extracted is s
1d530 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1d531 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  r P3..**.** If t
1d532 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  he column contai
1d533 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32  ns fewer than P2
1d534 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78   fields, then ex
1d535 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f  tract a NULL.  O
1d536 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 34 20  r,.** if the P4 
1d537 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50 34  argument is a P4
1d538 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61 6c  _MEM use the val
1d539 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 72 67  ue of the P4 arg
1d53a 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20  ument as.** the 
1d53b 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  result..**.** If
1d53c 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41   the OPFLAG_CLEA
1d53d 52 43 41 43 48 45 20 62 69 74 20 69 73 20 73 65  RCACHE bit is se
1d53e 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 20 69  t on P5 and P1 i
1d53f 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  s a pseudo-table
1d540 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e   cursor,.** then
1d541 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 74 68   the cache of th
1d542 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 73 65  e cursor is rese
1d543 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 72 61  t prior to extra
1d544 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e  cting the column
1d545 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 4f  ..** The first O
1d546 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74  P_Column against
1d547 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20   a pseudo-table 
1d548 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 65 20  after the value 
1d549 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a  of the content.*
1d54a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 20 63  * register has c
1d54b 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 68 61  hanged should ha
1d54c 76 65 20 74 68 69 73 20 62 69 74 20 73 65 74 2e  ve this bit set.
1d54d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75  .*/.case OP_Colu
1d54e 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  mn: {.#if 0  /* 
1d54f 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d550 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20  moved into u.am 
1d551 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64  */.  u32 payload
1d552 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
1d553 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1d554 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36  e record */.  i6
1d555 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b  4 payloadSize64;
1d556 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d557 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1d558 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20  d */.  int p1;  
1d559 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20            /* P1 
1d55a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63  value of the opc
1d55b 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b  ode */.  int p2;
1d55c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
1d55d 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20  olumn number to 
1d55e 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 56 64  retrieve */.  Vd
1d55f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20  beCursor *pC;   
1d560 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
1d561 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  sor */.  char *z
1d562 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50  Rec;        /* P
1d563 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65  ointer to comple
1d564 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a  te record-data *
1d565 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
1d566 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54  rsr;   /* The BT
1d567 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  ree cursor */.  
1d568 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20  u32 *aType;     
1d569 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68     /* aType[i] h
1d56a 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63  olds the numeric
1d56b 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74   type of the i-t
1d56c 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33  h column */.  u3
1d56d 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
1d56e 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69   /* aOffset[i] i
1d56f 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72  s offset to star
1d570 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d  t of data for i-
1d571 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  th column */.  i
1d572 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1d573 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66    /* number of f
1d574 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
1d575 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e  ord */.  int len
1d576 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
1d577 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
1d578 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61   serialized data
1d579 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
1d57a 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
1d57b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1d57c 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61  counter */.  cha
1d57d 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20  r *zData;       
1d57e 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72  /* Part of the r
1d57f 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1d580 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  ded */.  Mem *pD
1d581 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57  est;        /* W
1d582 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68  here to write th
1d583 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75  e extracted valu
1d584 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b  e */.  Mem sMem;
1d585 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
1d586 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63   storing the rec
1d587 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65  ord being decode
1d588 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b  d */.  u8 *zIdx;
1d589 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
1d58a 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a  ex into header *
1d58b 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b  /.  u8 *zEndHdr;
1d58c 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1d58d 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  r to first byte 
1d58e 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72  after the header
1d58f 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74   */.  u32 offset
1d590 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  ;        /* Offs
1d591 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  et into the data
1d592 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 65 74   */.  u64 offset
1d593 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62  64;      /* 64-b
1d594 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62  it offset.  64 b
1d595 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61  its needed to ca
1d596 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a  tch overflow */.
1d597 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20    int szHdr;    
1d598 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1d599 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20  the header size 
1d59a 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f  field at start o
1d59b 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  f record */.  in
1d59c 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20  t avail;        
1d59d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1d59e 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  tes of available
1d59f 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a   data */.  Mem *
1d5a0 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  pReg;         /*
1d5a1 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70   PseudoTable inp
1d5a2 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23  ut register */.#
1d5a3 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1d5a4 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d5a5 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20  nto u.am */...  
1d5a6 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70  u.am.p1 = pOp->p
1d5a7 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70  1;.  u.am.p2 = p
1d5a8 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70  Op->p2;.  u.am.p
1d5a9 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28  C = 0;.  memset(
1d5aa 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73  &u.am.sMem, 0, s
1d5ab 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29  izeof(u.am.sMem)
1d5ac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  );.  assert( u.a
1d5ad 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  m.p1<p->nCursor 
1d5ae 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1d5af 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1d5b0 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1d5b1 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d  u.am.pDest = &aM
1d5b2 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d  em[pOp->p3];.  M
1d5b3 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 75 2e  emSetTypeFlag(u.
1d5b4 61 6d 2e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75  am.pDest, MEM_Nu
1d5b5 6c 6c 29 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63  ll);.  u.am.zRec
1d5b6 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73   = 0;..  /* This
1d5b7 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20   block sets the 
1d5b8 76 61 72 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61  variable u.am.pa
1d5b9 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20  yloadSize to be 
1d5ba 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1d5bb 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69   of.  ** bytes i
1d5bc 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20  n the record..  
1d5bd 2a 2a 0a 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65  **.  ** u.am.zRe
1d5be 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74  c is set to be t
1d5bf 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
1d5c0 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   of the record i
1d5c1 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  f it is availabl
1d5c2 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70  e..  ** The comp
1d5c3 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74  lete record text
1d5c4 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c   is always avail
1d5c5 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d  able for pseudo-
1d5c6 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74  tables.  ** If t
1d5c7 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f  he record is sto
1d5c8 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c  red in a cursor,
1d5c9 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65   the complete re
1d5ca 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d  cord text.  ** m
1d5cb 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c  ight be availabl
1d5cc 65 20 69 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70  e in the  u.am.p
1d5cd 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d5ce 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  Or it might not 
1d5cf 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  be..  ** If the 
1d5d0 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61  data is unavaila
1d5d1 62 6c 65 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20  ble,  u.am.zRec 
1d5d2 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a  is set to NULL..
1d5d3 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73    **.  ** We als
1d5d4 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75  o compute the nu
1d5d5 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d5d6 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20  in the record.  
1d5d7 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a  For cursors,.  *
1d5d8 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1d5d9 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65  columns is store
1d5da 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72  d in the VdbeCur
1d5db 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65  sor.nField eleme
1d5dc 6e 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e  nt..  */.  u.am.
1d5dd 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e  pC = p->apCsr[u.
1d5de 61 6d 2e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  am.p1];.  assert
1d5df 28 20 75 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a  ( u.am.pC!=0 );.
1d5e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d5e1 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1d5e2 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e  .  assert( u.am.
1d5e3 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d  pC->pVtabCursor=
1d5e4 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75  =0 );.#endif.  u
1d5e5 2e 61 6d 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d  .am.pCrsr = u.am
1d5e6 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1d5e7 69 66 28 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d  if( u.am.pCrsr!=
1d5e8 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  0 ){.    /* The 
1d5e9 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d5ea 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a   in a B-Tree */.
1d5eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d5ec 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1d5ed 28 75 2e 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69  (u.am.pC);.    i
1d5ee 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1d5ef 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1d5f0 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 43  .    if( u.am.pC
1d5f1 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1d5f2 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d5f3 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
1d5f4 73 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e  se if( u.am.pC->
1d5f5 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e  cacheStatus==p->
1d5f6 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20  cacheCtr ){.    
1d5f7 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69    u.am.payloadSi
1d5f8 7a 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61  ze = u.am.pC->pa
1d5f9 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20  yloadSize;.     
1d5fa 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68   u.am.zRec = (ch
1d5fb 61 72 2a 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f  ar*)u.am.pC->aRo
1d5fc 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  w;.    }else if(
1d5fd 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d5fe 78 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  x ){.      asser
1d5ff 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  t( sqlite3BtreeC
1d600 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 61  ursorIsValid(u.a
1d601 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 20 20  m.pCrsr) );.    
1d602 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1d603 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e  reeKeySize(u.am.
1d604 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79  pCrsr, &u.am.pay
1d605 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20  loadSize64);.   
1d606 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1d607 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a  QLITE_OK );   /*
1d608 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66   True because of
1d609 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
1d60a 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20  call above */.  
1d60b 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74      /* sqlite3Bt
1d60c 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1d60d 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74  ) uses getVarint
1d60e 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20  32() to extract 
1d60f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79  the.      ** pay
1d610 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74  load size, so it
1d611 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
1d612 6f 72 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  or u.am.payloadS
1d613 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20  ize64 to be.    
1d614 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e    ** larger than
1d615 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20   32 bits. */.   
1d616 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6d     assert( (u.am
1d617 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26  .payloadSize64 &
1d618 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
1d619 3d 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c  ==(u64)u.am.payl
1d61a 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20  oadSize64 );.   
1d61b 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d61c 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e  ize = (u32)u.am.
1d61d 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
1d61e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d61f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1d620 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
1d621 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b  d(u.am.pCrsr) );
1d622 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d623 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
1d624 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d625 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b  am.payloadSize);
1d626 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
1d627 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1d628 20 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20    /* DataSize() 
1d629 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20  cannot fail */. 
1d62a 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1d62b 20 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f   u.am.pC->pseudo
1d62c 54 61 62 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20  TableReg>0 ){.  
1d62d 20 20 75 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61    u.am.pReg = &a
1d62e 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65  Mem[u.am.pC->pse
1d62f 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20  udoTableReg];.  
1d630 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d631 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Reg->flags & MEM
1d632 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61  _Blob );.    u.a
1d633 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20  m.payloadSize = 
1d634 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20  u.am.pReg->n;.  
1d635 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e    u.am.zRec = u.
1d636 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20  am.pReg->z;.    
1d637 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d638 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26  atus = (pOp->p5&
1d639 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48  OPFLAG_CLEARCACH
1d63a 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45  E) ? CACHE_STALE
1d63b 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a   : p->cacheCtr;.
1d63c 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d63d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20  .payloadSize==0 
1d63e 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20  || u.am.zRec!=0 
1d63f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1d640 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20  /* Consider the 
1d641 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a  row to be NULL *
1d642 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f  /.    u.am.paylo
1d643 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a  adSize = 0;.  }.
1d644 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61  .  /* If u.am.pa
1d645 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20  yloadSize is 0, 
1d646 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20  then just store 
1d647 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20  a NULL */.  if( 
1d648 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d649 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1d64a 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66  t( u.am.pDest->f
1d64b 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  lags&MEM_Null );
1d64c 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c  .    goto op_col
1d64d 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61  umn_out;.  }.  a
1d64e 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69  ssert( db->aLimi
1d64f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1d650 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69  ENGTH]>=0 );.  i
1d651 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  f( u.am.payloadS
1d652 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61  ize > (u32)db->a
1d653 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d654 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d655 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d656 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65    }..  u.am.nFie
1d657 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46  ld = u.am.pC->nF
1d658 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20  ield;.  assert( 
1d659 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69  u.am.p2<u.am.nFi
1d65a 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
1d65b 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
1d65c 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
1d65d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1d65e 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
1d65f 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
1d660 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
1d661 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
1d662 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61  rsor..  */.  u.a
1d663 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70  m.aType = u.am.p
1d664 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20  C->aType;.  if( 
1d665 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d666 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1d667 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f  r ){.    u.am.aO
1d668 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d  ffset = u.am.pC-
1d669 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73  >aOffset;.  }els
1d66a 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e  e{.    assert(u.
1d66b 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75  am.aType);.    u
1d66c 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20  .am.avail = 0;. 
1d66d 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66     u.am.pC->aOff
1d66e 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73  set = u.am.aOffs
1d66f 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65  et = &u.am.aType
1d670 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.am.nField];. 
1d671 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c     u.am.pC->payl
1d672 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d673 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
1d674 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d675 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43  atus = p->cacheC
1d676 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75  tr;..    /* Figu
1d677 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
1d678 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65  bytes are in the
1d679 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69   header */.    i
1d67a 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d67b 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61        u.am.zData
1d67c 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20   = u.am.zRec;.  
1d67d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1d67e 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e  f( u.am.pC->isIn
1d67f 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75  dex ){.        u
1d680 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d681 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b  r*)sqlite3BtreeK
1d682 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72  eyFetch(u.am.pCr
1d683 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29  sr, &u.am.avail)
1d684 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d685 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d686 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d687 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
1d688 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d689 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20  am.avail);.     
1d68a 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b   }.      /* If K
1d68b 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65  eyFetch()/DataFe
1d68c 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f  tch() managed to
1d68d 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20   get the entire 
1d68e 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a  payload,.      *
1d68f 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f  * save the paylo
1d690 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70  ad in the u.am.p
1d691 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d692 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75  That will save u
1d693 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20  s from.      ** 
1d694 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61  having to make a
1d695 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20  dditional calls 
1d696 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
1d697 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a  tent portion of.
1d698 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
1d699 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ord..      */.  
1d69a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d69b 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20  .avail>=0 );.   
1d69c 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c     if( u.am.payl
1d69d 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29  oadSize <= (u32)
1d69e 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20  u.am.avail ){.  
1d69f 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d6a0 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20  = u.am.zData;.  
1d6a1 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61        u.am.pC->a
1d6a2 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e  Row = (u8*)u.am.
1d6a3 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
1d6a4 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d  se{.        u.am
1d6a5 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20  .pC->aRow = 0;. 
1d6a6 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d6a7 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1d6a8 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65  g assert is true
1d6a9 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63   in all cases ac
1d6aa 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  cept when.    **
1d6ab 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1d6ac 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  le has been corr
1d6ad 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79  upted externally
1d6ae 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65  ..    **    asse
1d6af 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30  rt( u.am.zRec!=0
1d6b0 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d6b1 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d6b2 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d6b3 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d  9 ); */.    u.am
1d6b4 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69  .szHdr = getVari
1d6b5 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  nt32((u8*)u.am.z
1d6b6 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65  Data, u.am.offse
1d6b7 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  t);..    /* Make
1d6b8 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20   sure a corrupt 
1d6b9 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
1d6ba 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65   given us an ove
1d6bb 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20  rsize header..  
1d6bc 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77    ** Do this now
1d6bd 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65   to avoid an ove
1d6be 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  rsize memory all
1d6bf 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ocation..    **.
1d6c0 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72      ** Type entr
1d6c1 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65  ies can be betwe
1d6c2 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73  en 1 and 5 bytes
1d6c3 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e   each.  But 4 an
1d6c4 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  d 5 byte.    ** 
1d6c5 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63  types use so muc
1d6c6 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61  h data space tha
1d6c7 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  t there can only
1d6c8 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20   be 4096 and 32 
1d6c9 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20  of.    ** them, 
1d6ca 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53  respectively.  S
1d6cb 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  o the maximum he
1d6cc 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75  ader length resu
1d6cd 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a  lts from a.    *
1d6ce 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
1d6cf 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
1d6d0 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
1d6d1 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
1d6d2 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62  e.    ** extra b
1d6d3 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61  ytes for the hea
1d6d4 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c  der length itsel
1d6d5 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20  f.  32768*3 + 3 
1d6d6 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a  = 98307..    */.
1d6d7 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66      if( u.am.off
1d6d8 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20  set > 98307 ){. 
1d6d9 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1d6da 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1d6db 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
1d6dc 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  umn_out;.    }..
1d6dd 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69      /* Compute i
1d6de 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e  n u.am.len the n
1d6df 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d6e0 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74  f data we need t
1d6e1 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a  o read in order.
1d6e2 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e      ** to get u.
1d6e3 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76  am.nField type v
1d6e4 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66  alues.  u.am.off
1d6e5 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20  set is an upper 
1d6e6 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20  bound on this.  
1d6e7 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e  But.    ** u.am.
1d6e8 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20  nField might be 
1d6e9 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65  significantly le
1d6ea 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65  ss than the true
1d6eb 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1d6ec 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ns.    ** in the
1d6ed 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74   table, and in t
1d6ee 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d  hat case, 5*u.am
1d6ef 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20  .nField+3 might 
1d6f0 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
1d6f1 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20  u.am.offset..   
1d6f2 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d   ** We want to m
1d6f3 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e  inimize u.am.len
1d6f4 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d   in order to lim
1d6f5 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  it the size of t
1d6f6 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  he memory.    **
1d6f7 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70   allocation, esp
1d6f8 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72  ecially if a cor
1d6f9 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d6fa 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e  le has caused u.
1d6fb 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a  am.offset.    **
1d6fc 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64   to be oversized
1d6fd 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69  . Offset is limi
1d6fe 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f  ted to 98307 abo
1d6ff 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d  ve.  But 98307 m
1d700 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c  ight.    ** stil
1d701 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20  l exceed Robson 
1d702 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1d703 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65  n limits on some
1d704 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e   configurations.
1d705 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
1d706 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
1d707 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
1d708 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
1d709 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b  , u.am.nField*5+
1d70a 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69  3.    ** will li
1d70b 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61  kely be much sma
1d70c 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e  ller since u.am.
1d70d 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65  nField will like
1d70e 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a  ly be less than.
1d70f 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e      ** 20 or so.
1d710 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74    This insures t
1d711 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72  hat Robson memor
1d712 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d  y allocation lim
1d713 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  its are.    ** n
1d714 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e  ot exceeded even
1d715 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74   for corrupt dat
1d716 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20  abase files..   
1d717 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e   */.    u.am.len
1d718 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35   = u.am.nField*5
1d719 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e   + 3;.    if( u.
1d71a 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e  am.len > (int)u.
1d71b 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d  am.offset ) u.am
1d71c 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d  .len = (int)u.am
1d71d 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a  .offset;..    /*
1d71e 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
1d71f 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
1d720 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
1d721 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
1d722 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
1d723 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
1d724 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
1d725 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
1d726 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
1d727 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
1d728 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
1d729 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
1d72a 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
1d72b 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
1d72c 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
1d72d 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1d72e 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
1d72f 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d730 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
1d731 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
1d732 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
1d733 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e    */.    if( !u.
1d734 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e  am.zRec && u.am.
1d735 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29  avail<u.am.len )
1d736 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65  {.      u.am.sMe
1d737 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  m.flags = 0;.   
1d738 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20     u.am.sMem.db 
1d739 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
1d73a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d73b 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72  omBtree(u.am.pCr
1d73c 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c  sr, 0, u.am.len,
1d73d 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d73e 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a  x, &u.am.sMem);.
1d73f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1d740 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d741 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1d742 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1d743 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20       u.am.zData 
1d744 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.am.sMem.z;. 
1d745 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45     }.    u.am.zE
1d746 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75  ndHdr = (u8 *)&u
1d747 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c  .am.zData[u.am.l
1d748 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49  en];.    u.am.zI
1d749 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d  dx = (u8 *)&u.am
1d74a 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64  .zData[u.am.szHd
1d74b 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  r];..    /* Scan
1d74c 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20   the header and 
1d74d 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69  use it to fill i
1d74e 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65  n the u.am.aType
1d74f 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66  [] and u.am.aOff
1d750 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
1d751 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65  ays.  u.am.aType
1d752 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f  [u.am.i] will co
1d753 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
1d754 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75  nteger for the u
1d755 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20  .am.i-th.    ** 
1d756 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e  column and u.am.
1d757 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d758 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
1d759 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f   u.am.offset fro
1d75a 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
1d75b 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
1d75c 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
1d75d 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
1d75e 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20  r the u.am.i-th 
1d75f 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
1d760 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20    u.am.offset64 
1d761 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20  = u.am.offset;. 
1d762 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b     for(u.am.i=0;
1d763 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69   u.am.i<u.am.nFi
1d764 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a  eld; u.am.i++){.
1d765 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a        if( u.am.z
1d766 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72  Idx<u.am.zEndHdr
1d767 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d   ){.        u.am
1d768 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d  .aOffset[u.am.i]
1d769 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66   = (u32)u.am.off
1d76a 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75  set64;.        u
1d76b 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56  .am.zIdx += getV
1d76c 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64  arint32(u.am.zId
1d76d 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  x, u.am.aType[u.
1d76e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  am.i]);.        
1d76f 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d  u.am.offset64 +=
1d770 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1d771 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61  alTypeLen(u.am.a
1d772 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20  Type[u.am.i]);. 
1d773 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d774 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69      /* If u.am.i
1d775 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e   is less that u.
1d776 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20  am.nField, then 
1d777 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66  there are less f
1d778 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20  ields in this.  
1d779 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20        ** record 
1d77a 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  than SetNumColum
1d77b 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65  ns indicated the
1d77c 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69  re are columns i
1d77d 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
1d77e 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
1d77f 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20  u.am.offset for 
1d780 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e  any extra column
1d781 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
1d782 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
1d783 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69  record to 0. Thi
1d784 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c  s tells code bel
1d785 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55  ow to store a NU
1d786 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  LL.        ** in
1d787 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61  stead of deseria
1d788 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66  lizing a value f
1d789 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a  rom the record..
1d78a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1d78b 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b     u.am.aOffset[
1d78c 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20  u.am.i] = 0;.   
1d78d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1d78e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1d78f 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29  ease(&u.am.sMem)
1d790 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e  ;.    u.am.sMem.
1d791 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d792 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
1d793 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
1d794 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
1d795 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
1d796 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
1d797 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
1d798 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
1d799 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
1d79a 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1d79b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
1d79c 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
1d79d 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1d79e 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1d79f 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
1d7a0 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
1d7a1 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
1d7a2 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
1d7a3 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
1d7a4 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a  e dealing.    **
1d7a5 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20   with a corrupt 
1d7a6 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
1d7a7 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a  .    if( (u.am.z
1d7a8 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48  Idx > u.am.zEndH
1d7a9 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73  dr)|| (u.am.offs
1d7aa 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c  et64 > u.am.payl
1d7ab 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c  oadSize).     ||
1d7ac 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61   (u.am.zIdx==u.a
1d7ad 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61  m.zEndHdr && u.a
1d7ae 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34  m.offset64!=(u64
1d7af 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  )u.am.payloadSiz
1d7b0 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  e) ){.      rc =
1d7b1 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1d7b2 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
1d7b3 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a   op_column_out;.
1d7b4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1d7b5 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  Get the column i
1d7b6 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75  nformation. If u
1d7b7 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d  .am.aOffset[u.am
1d7b8 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  .p2] is non-zero
1d7b9 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65  , then.  ** dese
1d7ba 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75  rialize the valu
1d7bb 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
1d7bc 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73  d. If u.am.aOffs
1d7bd 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a  et[u.am.p2] is z
1d7be 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ero,.  ** then t
1d7bf 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f  here are not eno
1d7c0 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68  ugh fields in th
1d7c1 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69  e record to sati
1d7c2 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71  sfy the.  ** req
1d7c3 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63  uest.  In this c
1d7c4 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c  ase, set the val
1d7c5 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34  ue NULL or to P4
1d7c6 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61   if P4 is.  ** a
1d7c7 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1d7c8 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20  m object..  */. 
1d7c9 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65   if( u.am.aOffse
1d7ca 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20  t[u.am.p2] ){.  
1d7cb 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1d7cc 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69  LITE_OK );.    i
1d7cd 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d7ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d7cf 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1d7d0 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b  nal(u.am.pDest);
1d7d1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d7d2 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20  beSerialGet((u8 
1d7d3 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61  *)&u.am.zRec[u.a
1d7d4 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d7d5 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b  2]], u.am.aType[
1d7d6 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70  u.am.p2], u.am.p
1d7d7 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
1d7d8 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e  {.      u.am.len
1d7d9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d7da 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d  rialTypeLen(u.am
1d7db 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29  .aType[u.am.p2])
1d7dc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d7dd 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d  dbeMemMove(&u.am
1d7de 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73  .sMem, u.am.pDes
1d7df 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
1d7e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1d7e1 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73  mBtree(u.am.pCrs
1d7e2 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  r, u.am.aOffset[
1d7e3 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c  u.am.p2], u.am.l
1d7e4 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49  en, u.am.pC->isI
1d7e5 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d  ndex, &u.am.sMem
1d7e6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1d7e7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d7e8 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1d7e9 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
1d7ea 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61  }.      u.am.zDa
1d7eb 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  ta = u.am.sMem.z
1d7ec 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d7ed 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
1d7ee 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d7ef 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32  am.aType[u.am.p2
1d7f0 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  ], u.am.pDest);.
1d7f1 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70      }.    u.am.p
1d7f2 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  Dest->enc = enco
1d7f3 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ding;.  }else{. 
1d7f4 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79     if( pOp->p4ty
1d7f5 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20  pe==P4_MEM ){.  
1d7f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d7f7 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e  emShallowCopy(u.
1d7f8 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70  am.pDest, pOp->p
1d7f9 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74  4.pMem, MEM_Stat
1d7fa 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ic);.    }else{.
1d7fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1d7fc 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  am.pDest->flags&
1d7fd 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
1d7fe 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  }.  }..  /* If w
1d7ff 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
1d800 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f  located space to
1d801 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28   hold the data (
1d802 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  in the.  ** sqli
1d803 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1d804 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  ree() call above
1d805 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  ) then transfer 
1d806 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a  control of that.
1d807 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79    ** dynamically
1d808 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1d809 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61   over to the u.a
1d80a 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72  m.pDest structur
1d80b 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65  e..  ** This pre
1d80c 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63  vents a memory c
1d80d 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  opy..  */.  if( 
1d80e 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d80f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
1d810 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e   u.am.sMem.z==u.
1d811 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  am.sMem.zMalloc 
1d812 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
1d813 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61  (u.am.pDest->fla
1d814 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b  gs & MEM_Dyn) );
1d815 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75  .    assert( !(u
1d816 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73  .am.pDest->flags
1d817 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
1d818 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70  _Str)) || u.am.p
1d819 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d  Dest->z==u.am.sM
1d81a 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d  em.z );.    u.am
1d81b 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d  .pDest->flags &=
1d81c 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
1d81d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e  _Static);.    u.
1d81e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  am.pDest->flags 
1d81f 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  |= MEM_Term;.   
1d820 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d   u.am.pDest->z =
1d821 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20   u.am.sMem.z;.  
1d822 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d    u.am.pDest->zM
1d823 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65  alloc = u.am.sMe
1d824 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a  m.zMalloc;.  }..
1d825 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d826 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1d827 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  le(u.am.pDest);.
1d828 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a  .op_column_out:.
1d829 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1d82a 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74  BSIZE(u.am.pDest
1d82b 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1d82c 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61  ACE(pOp->p3, u.a
1d82d 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61  m.pDest);.  brea
1d82e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d82f 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20   Affinity P1 P2 
1d830 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70  * P4 *.**.** App
1d831 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f  ly affinities to
1d832 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72   a range of P2 r
1d833 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1d834 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a  g with P1..**.**
1d835 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20   P4 is a string 
1d836 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61  that is P2 chara
1d837 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20  cters long. The 
1d838 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66  nth character of
1d839 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69   the.** string i
1d83a 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c  ndicates the col
1d83b 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61  umn affinity tha
1d83c 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
1d83d 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20   for the nth.** 
1d83e 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74  memory cell in t
1d83f 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73  he range..*/.cas
1d840 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b  e OP_Affinity: {
1d841 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d842 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d843 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20   into u.an */.  
1d844 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66  const char *zAff
1d845 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20  inity;   /* The 
1d846 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61  affinity to be a
1d847 70 70 6c 69 65 64 20 2a 2f 0a 20 20 63 68 61 72  pplied */.  char
1d848 20 63 41 66 66 3b 20 20 20 20 20 20 20 20 20 20   cAff;          
1d849 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
1d84a 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 61 66   character of af
1d84b 66 69 6e 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66  finity */.#endif
1d84c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d84d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d84e 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a  .an */..  u.an.z
1d84f 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  Affinity = pOp->
1d850 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20  p4.z;.  assert( 
1d851 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d  u.an.zAffinity!=
1d852 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1d853 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f  .an.zAffinity[pO
1d854 70 2d 3e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70  p->p2]==0 );.  p
1d855 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
1d856 3e 70 31 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28  >p1];.  while( (
1d857 75 2e 61 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e  u.an.cAff = *(u.
1d858 61 6e 2e 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29  an.zAffinity++))
1d859 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  !=0 ){.    asser
1d85a 74 28 20 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61  t( pIn1 <= &p->a
1d85b 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a  Mem[p->nMem] );.
1d85c 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70      ExpandBlob(p
1d85d 49 6e 31 29 3b 0a 20 20 20 20 61 70 70 6c 79 41  In1);.    applyA
1d85e 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e  ffinity(pIn1, u.
1d85f 61 6e 2e 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e  an.cAff, encodin
1d860 67 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a  g);.    pIn1++;.
1d861 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d862 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52  /* Opcode: MakeR
1d863 65 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50  ecord P1 P2 P3 P
1d864 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72  4 *.**.** Conver
1d865 74 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62  t P2 registers b
1d866 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31  eginning with P1
1d867 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65   into a single e
1d868 6e 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65  ntry.** suitable
1d869 20 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61   for use as a da
1d86a 74 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64  ta record in a d
1d86b 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1d86c 20 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20   as a key.** in 
1d86d 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64  an index.  The d
1d86e 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f  etails of the fo
1d86f 72 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76  rmat are irrelev
1d870 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a  ant as long as.*
1d871 2a 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  * the OP_Column 
1d872 6f 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64  opcode can decod
1d873 65 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74  e the record lat
1d874 65 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20  er..** Refer to 
1d875 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d  source code comm
1d876 65 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74  ents for the det
1d877 61 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f  ails of the reco
1d878 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a  rd.** format..**
1d879 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20  .** P4 may be a 
1d87a 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50  string that is P
1d87b 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e  2 characters lon
1d87c 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72  g.  The nth char
1d87d 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  acter of the.** 
1d87e 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73  string indicates
1d87f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
1d880 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
1d881 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
1d882 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66   nth.** field of
1d883 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a   the index key..
1d884 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e  **.** The mappin
1d885 67 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72  g from character
1d886 20 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20   to affinity is 
1d887 67 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c  given by the SQL
1d888 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72  ITE_AFF_.** macr
1d889 6f 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71  os defined in sq
1d88a 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a  liteInt.h..**.**
1d88b 20 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74   If P4 is NULL t
1d88c 68 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69  hen all index fi
1d88d 65 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66  elds have the af
1d88e 66 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a  finity NONE..*/.
1d88f 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  case OP_MakeReco
1d890 72 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  rd: {.#if 0  /* 
1d891 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d892 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20  moved into u.ao 
1d893 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63  */.  u8 *zNewRec
1d894 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ord;        /* A
1d895 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
1d896 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1d897 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
1d898 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20   Mem *pRec;     
1d899 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
1d89a 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  ew record */.  u
1d89b 36 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20  64 nData;       
1d89c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d89d 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1d89e 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20   space */.  int 
1d89f 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  nHdr;           
1d8a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d8a1 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20  bytes of header 
1d8a2 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e  space */.  i64 n
1d8a3 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
1d8a4 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20    /* Data space 
1d8a5 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
1d8a6 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  s record */.  in
1d8a7 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20  t nZero;        
1d8a8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d8a9 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20  f zero bytes at 
1d8aa 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72  the end of the r
1d8ab 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1d8ac 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20  Varint;         
1d8ad 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d8ae 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74  ytes in a varint
1d8af 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c   */.  u32 serial
1d8b0 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  _type;       /* 
1d8b1 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20  Type field */.  
1d8b2 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20  Mem *pData0;    
1d8b3 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1d8b4 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62  field to be comb
1d8b5 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65  ined into the re
1d8b6 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  cord */.  Mem *p
1d8b7 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Last;           
1d8b8 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f   /* Last field o
1d8b9 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  f the record */.
1d8ba 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20    int nField;   
1d8bb 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1d8bc 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1d8bd 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1d8be 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b  char *zAffinity;
1d8bf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66         /* The af
1d8c0 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f  finity string fo
1d8c1 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  r the record */.
1d8c2 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61    int file_forma
1d8c3 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65  t;       /* File
1d8c4 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66   format to use f
1d8c5 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  or encoding */. 
1d8c6 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1d8c7 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
1d8c8 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63   used in zNewRec
1d8c9 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c  ord[] */.  int l
1d8ca 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
1d8cb 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61    /* Length of a
1d8cc 20 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66   field */.#endif
1d8cd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d8ce 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d8cf 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73  .ao */..  /* Ass
1d8d0 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  uming the record
1d8d1 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c   contains N fiel
1d8d2 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66  ds, the record f
1d8d3 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a  ormat looks.  **
1d8d4 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a   like this:.  **
1d8d5 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .  ** ----------
1d8d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20  --------------. 
1d8da 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c   ** | hdr-size |
1d8db 20 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31   type 0 | type 1
1d8dc 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d   | ... | type N-
1d8dd 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20  1 | data0 | ... 
1d8de 7c 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a  | data N-1 |.  *
1d8df 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
1d8e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8e2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d8e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a  ----------.  **.
1d8e4 20 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20    ** Data(0) is 
1d8e5 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73  taken from regis
1d8e6 74 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29  ter P1.  Data(1)
1d8e7 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69   comes from regi
1d8e8 73 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61  ster P1+1.  ** a
1d8e9 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a  nd so froth..  *
1d8ea 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65  *.  ** Each type
1d8eb 20 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69   field is a vari
1d8ec 6e 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  nt representing 
1d8ed 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  the serial type 
1d8ee 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72  of the.  ** corr
1d8ef 65 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65  esponding data e
1d8f0 6c 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69  lement (see sqli
1d8f1 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1d8f2 65 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68  e()). The.  ** h
1d8f3 64 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73  dr-size field is
1d8f4 20 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77   also a varint w
1d8f5 68 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73  hich is the offs
1d8f6 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  et from the begi
1d8f7 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68  nning.  ** of th
1d8f8 65 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61  e record to data
1d8f9 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  0..  */.  u.ao.n
1d8fa 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20  Data = 0;       
1d8fb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d8fc 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61  ytes of data spa
1d8fd 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64  ce */.  u.ao.nHd
1d8fe 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
1d8ff 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1d900 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61  es of header spa
1d901 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79  ce */.  u.ao.nBy
1d902 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  te = 0;         
1d903 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65  /* Data space re
1d904 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20  quired for this 
1d905 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f  record */.  u.ao
1d906 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20  .nZero = 0;     
1d907 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d908 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74   zero bytes at t
1d909 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65  he end of the re
1d90a 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  cord */.  u.ao.n
1d90b 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b  Field = pOp->p1;
1d90c 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74  .  u.ao.zAffinit
1d90d 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  y = pOp->p4.z;. 
1d90e 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46   assert( u.ao.nF
1d90f 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ield>0 && pOp->p
1d910 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75  2>0 && pOp->p2+u
1d911 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e  .ao.nField<=p->n
1d912 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e  Mem+1 );.  u.ao.
1d913 70 44 61 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75  pData0 = &aMem[u
1d914 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75  .ao.nField];.  u
1d915 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  .ao.nField = pOp
1d916 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61  ->p2;.  u.ao.pLa
1d917 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61  st = &u.ao.pData
1d918 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d  0[u.ao.nField-1]
1d919 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f  ;.  u.ao.file_fo
1d91a 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69  rmat = p->minWri
1d91b 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20  teFileFormat;.. 
1d91c 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
1d91d 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68   the elements th
1d91e 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20  at will make up 
1d91f 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69  the record to fi
1d920 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f  gure.  ** out ho
1d921 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20  w much space is 
1d922 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65  required for the
1d923 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a   new record..  *
1d924 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65  /.  for(u.ao.pRe
1d925 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75  c=u.ao.pData0; u
1d926 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70  .ao.pRec<=u.ao.p
1d927 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b  Last; u.ao.pRec+
1d928 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f  +){.    if( u.ao
1d929 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20  .zAffinity ){.  
1d92a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1d92b 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61  y(u.ao.pRec, u.a
1d92c 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f  o.zAffinity[u.ao
1d92d 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61  .pRec-u.ao.pData
1d92e 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  0], encoding);. 
1d92f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61     }.    if( u.a
1d930 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45  o.pRec->flags&ME
1d931 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70  M_Zero && u.ao.p
1d932 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20  Rec->n>0 ){.    
1d933 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d934 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e  ExpandBlob(u.ao.
1d935 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pRec);.    }.   
1d936 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70   u.ao.serial_typ
1d937 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
1d938 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70  erialType(u.ao.p
1d939 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66  Rec, u.ao.file_f
1d93a 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f  ormat);.    u.ao
1d93b 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64  .len = sqlite3Vd
1d93c 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1d93d 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d93e 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74  );.    u.ao.nDat
1d93f 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20  a += u.ao.len;. 
1d940 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20     u.ao.nHdr += 
1d941 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
1d942 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70  (u.ao.serial_typ
1d943 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f  e);.    if( u.ao
1d944 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d  .pRec->flags & M
1d945 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20  EM_Zero ){.     
1d946 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65   /* Only pure ze
1d947 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20  ro-filled BLOBs 
1d948 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20  can be input to 
1d949 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20  this Opcode..   
1d94a 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20     ** We do not 
1d94b 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68  allow blobs with
1d94c 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20   a prefix and a 
1d94d 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c  zero-filled tail
1d94e 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e  . */.      u.ao.
1d94f 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52  nZero += u.ao.pR
1d950 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  ec->u.nZero;.   
1d951 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e   }else if( u.ao.
1d952 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61  len ){.      u.a
1d953 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20  o.nZero = 0;.   
1d954 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64   }.  }..  /* Add
1d955 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61   the initial hea
1d956 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74  der varint and t
1d957 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f  otal the size */
1d958 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20  .  u.ao.nHdr += 
1d959 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73  u.ao.nVarint = s
1d95a 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d95b 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66  u.ao.nHdr);.  if
1d95c 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73  ( u.ao.nVarint<s
1d95d 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d95e 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20  u.ao.nHdr) ){.  
1d95f 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20    u.ao.nHdr++;. 
1d960 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20   }.  u.ao.nByte 
1d961 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f  = u.ao.nHdr+u.ao
1d962 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72  .nData-u.ao.nZer
1d963 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42  o;.  if( u.ao.nB
1d964 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  yte>db->aLimit[S
1d965 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1d966 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TH] ){.    goto 
1d967 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20  too_big;.  }..  
1d968 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
1d969 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72   output register
1d96a 20 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61   has a buffer la
1d96b 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74  rge enough to st
1d96c 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77  ore.  ** the new
1d96d 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74   record. The out
1d96e 70 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f  put register (pO
1d96f 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c  p->p3) is not al
1d970 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65  lowed to.  ** be
1d971 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75   one of the inpu
1d972 74 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63  t registers (bec
1d973 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ause the followi
1d974 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20  ng call to.  ** 
1d975 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1d976 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62  ow() could clobb
1d977 65 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66  er the value bef
1d978 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e  ore it is used).
1d979 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1d97a 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20  pOp->p3<pOp->p1 
1d97b 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d  || pOp->p3>=pOp-
1d97c 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20  >p1+pOp->p2 );. 
1d97d 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1d97e 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71  p->p3];.  if( sq
1d97f 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1d980 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f  (pOut, (int)u.ao
1d981 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20  .nByte, 0) ){.  
1d982 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1d983 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65   }.  u.ao.zNewRe
1d984 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75  cord = (u8 *)pOu
1d985 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74  t->z;..  /* Writ
1d986 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  e the record */.
1d987 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61    u.ao.i = putVa
1d988 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77  rint32(u.ao.zNew
1d989 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64  Record, u.ao.nHd
1d98a 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70  r);.  for(u.ao.p
1d98b 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b  Rec=u.ao.pData0;
1d98c 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f   u.ao.pRec<=u.ao
1d98d 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65  .pLast; u.ao.pRe
1d98e 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73  c++){.    u.ao.s
1d98f 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c  erial_type = sql
1d990 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1d991 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e  pe(u.ao.pRec, u.
1d992 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  ao.file_format);
1d993 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70  .    u.ao.i += p
1d994 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e 61 6f  utVarint32(&u.ao
1d995 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f  .zNewRecord[u.ao
1d996 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c  .i], u.ao.serial
1d997 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20  _type);      /* 
1d998 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20  serial type */. 
1d999 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52   }.  for(u.ao.pR
1d99a 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20  ec=u.ao.pData0; 
1d99b 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e  u.ao.pRec<=u.ao.
1d99c 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63  pLast; u.ao.pRec
1d99d 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20  ++){  /* serial 
1d99e 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f  data */.    u.ao
1d99f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62  .i += sqlite3Vdb
1d9a0 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e 61 6f  eSerialPut(&u.ao
1d9a1 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f  .zNewRecord[u.ao
1d9a2 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e  .i], (int)(u.ao.
1d9a3 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75  nByte-u.ao.i), u
1d9a4 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69  .ao.pRec,u.ao.fi
1d9a5 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a  le_format);.  }.
1d9a6 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 69    assert( u.ao.i
1d9a7 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a  ==u.ao.nByte );.
1d9a8 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1d9a9 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1d9aa 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1d9ab 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61  ut->n = (int)u.a
1d9ac 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d  o.nByte;.  pOut-
1d9ad 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
1d9ae 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70  b | MEM_Dyn;.  p
1d9af 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20  Out->xDel = 0;. 
1d9b0 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20   if( u.ao.nZero 
1d9b1 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e  ){.    pOut->u.n
1d9b2 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72  Zero = u.ao.nZer
1d9b3 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  o;.    pOut->fla
1d9b4 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a  gs |= MEM_Zero;.
1d9b5 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20    }.  pOut->enc 
1d9b6 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20  = SQLITE_UTF8;  
1d9b7 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62  /* In case the b
1d9b8 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76  lob is ever conv
1d9b9 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f  erted to text */
1d9ba 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1d9bb 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29  E(pOp->p3, pOut)
1d9bc 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ;.  UPDATE_MAX_B
1d9bd 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1d9be 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d9bf 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20 50  code: Count P1 P
1d9c0 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74  2 * * *.**.** St
1d9c1 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ore the number o
1d9c2 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69 6e  f entries (an in
1d9c3 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e 20  teger value) in 
1d9c4 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
1d9c5 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79  ex .** opened by
1d9c6 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72 65   cursor P1 in re
1d9c7 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66  gister P2.*/.#if
1d9c8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d9c9 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73 65  _BTREECOUNT.case
1d9ca 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20   OP_Count: {    
1d9cb 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1d9cc 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1d9cd 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1d9ce 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d9cf 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45  u.ap */.  i64 nE
1d9d0 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 6f 72  ntry;.  BtCursor
1d9d1 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 66 20   *pCrsr;.#endif 
1d9d2 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d9d3 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d9d4 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43  ap */..  u.ap.pC
1d9d5 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  rsr = p->apCsr[p
1d9d6 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72  Op->p1]->pCursor
1d9d7 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 43 72  ;.  if( u.ap.pCr
1d9d8 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  sr ){.    rc = s
1d9d9 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74  qlite3BtreeCount
1d9da 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 75 2e  (u.ap.pCrsr, &u.
1d9db 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65  ap.nEntry);.  }e
1d9dc 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45  lse{.    u.ap.nE
1d9dd 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ntry = 0;.  }.  
1d9de 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70  pOut->u.i = u.ap
1d9df 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b  .nEntry;.  break
1d9e0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f  ;.}.#endif../* O
1d9e1 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74  pcode: Savepoint
1d9e2 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1d9e3 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65  ** Open, release
1d9e4 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   or rollback the
1d9e5 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64   savepoint named
1d9e6 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34   by parameter P4
1d9e7 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f  , depending.** o
1d9e8 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  n the value of P
1d9e9 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77  1. To open a new
1d9ea 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d   savepoint, P1==
1d9eb 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63  0. To release (c
1d9ec 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69  ommit) an.** exi
1d9ed 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c  sting savepoint,
1d9ee 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f   P1==1, or to ro
1d9ef 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69  llback an existi
1d9f0 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d  ng savepoint P1=
1d9f1 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  =2..*/.case OP_S
1d9f2 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20  avepoint: {.#if 
1d9f3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1d9f4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d9f5 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70   u.aq */.  int p
1d9f6 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1d9f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
1d9f8 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e  lue of P1 operan
1d9f9 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
1d9fa 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
1d9fb 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1d9fc 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20  f savepoint */. 
1d9fd 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61   int nName;.  Sa
1d9fe 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20  vepoint *pNew;. 
1d9ff 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76   Savepoint *pSav
1da00 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f  epoint;.  Savepo
1da01 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74  int *pTmp;.  int
1da02 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69   iSavepoint;.  i
1da03 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a  nt ii;.#endif /*
1da04 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1da05 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71   moved into u.aq
1da06 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d   */..  u.aq.p1 =
1da07 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71   pOp->p1;.  u.aq
1da08 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34  .zName = pOp->p4
1da09 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74  .z;..  /* Assert
1da0a 20 74 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70   that the u.aq.p
1da0b 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76  1 parameter is v
1da0c 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20  alid. Also that 
1da0d 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f  if there is no o
1da0e 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  pen.  ** transac
1da0f 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65  tion, then there
1da10 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73   cannot be any s
1da11 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a  avepoints..  */.
1da12 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53    assert( db->pS
1da13 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64  avepoint==0 || d
1da14 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30  b->autoCommit==0
1da15 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1da16 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1da17 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d  _BEGIN||u.aq.p1=
1da18 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
1da19 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56  SE||u.aq.p1==SAV
1da1a 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1da1b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
1da1c 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64  >pSavepoint || d
1da1d 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
1da1e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a  Savepoint==0 );.
1da1f 20 20 61 73 73 65 72 74 28 20 63 68 65 63 6b 53    assert( checkS
1da20 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62  avepointCount(db
1da21 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71  ) );..  if( u.aq
1da22 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42  .p1==SAVEPOINT_B
1da23 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20  EGIN ){.    if( 
1da24 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1da25 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41  >0 ){.      /* A
1da26 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63   new savepoint c
1da27 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64  annot be created
1da28 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63   if there are ac
1da29 74 69 76 65 20 77 72 69 74 65 0a 20 20 20 20 20  tive write.     
1da2a 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28   ** statements (
1da2b 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77  i.e. open read/w
1da2c 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c  rite incremental
1da2d 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a   blob handles)..
1da2e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1da2f 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1da30 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1da31 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61   "cannot open sa
1da32 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20  vepoint - ".    
1da33 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65      "SQL stateme
1da34 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22  nts in progress"
1da35 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
1da36 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
1da37 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71  else{.      u.aq
1da38 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  .nName = sqlite3
1da39 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e  Strlen30(u.aq.zN
1da3a 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ame);..      /* 
1da3b 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76  Create a new sav
1da3c 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65  epoint structure
1da3d 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e  . */.      u.aq.
1da3e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
1da3f 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
1da40 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b  zeof(Savepoint)+
1da41 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20  u.aq.nName+1);. 
1da42 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e       if( u.aq.pN
1da43 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  ew ){.        u.
1da44 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d  aq.pNew->zName =
1da45 20 28 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70   (char *)&u.aq.p
1da46 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  New[1];.        
1da47 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77  memcpy(u.aq.pNew
1da48 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e  ->zName, u.aq.zN
1da49 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b  ame, u.aq.nName+
1da4a 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  1);..        /* 
1da4b 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f  If there is no o
1da4c 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  pen transaction,
1da4d 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20   then mark this 
1da4e 61 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20  as a special.   
1da4f 20 20 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63       ** "transac
1da50 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e  tion savepoint".
1da51 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
1da52 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1da53 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  {.          db->
1da54 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a  autoCommit = 0;.
1da55 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73            db->is
1da56 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1da57 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  oint = 1;.      
1da58 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1da59 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1da5a 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  t++;.        }..
1da5b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20          /* Link 
1da5c 74 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  the new savepoin
1da5d 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  t into the datab
1da5e 61 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73  ase handle's lis
1da5f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e  t. */.        u.
1da60 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d  aq.pNew->pNext =
1da61 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b   db->pSavepoint;
1da62 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61  .        db->pSa
1da63 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70  vepoint = u.aq.p
1da64 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  New;.        u.a
1da65 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65  q.pNew->nDeferre
1da66 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66  dCons = db->nDef
1da67 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20  erredCons;.     
1da68 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
1da69 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65  {.    u.aq.iSave
1da6a 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  point = 0;..    
1da6b 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65  /* Find the name
1da6c 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20  d savepoint. If 
1da6d 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68  there is no such
1da6e 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e   savepoint, then
1da6f 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72   an.    ** an er
1da70 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
1da71 74 6f 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f  to the user.  */
1da72 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20  .    for(.      
1da73 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1da74 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  = db->pSavepoint
1da75 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61  ;.      u.aq.pSa
1da76 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74  vepoint && sqlit
1da77 65 33 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70  e3StrICmp(u.aq.p
1da78 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65  Savepoint->zName
1da79 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20  , u.aq.zName);. 
1da7a 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70       u.aq.pSavep
1da7b 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76  oint = u.aq.pSav
1da7c 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20  epoint->pNext.  
1da7d 20 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e    ){.      u.aq.
1da7e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20  iSavepoint++;.  
1da7f 20 20 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61    }.    if( !u.a
1da80 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  q.pSavepoint ){.
1da81 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74        sqlite3Set
1da82 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1da83 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68  sg, db, "no such
1da84 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c   savepoint: %s",
1da85 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20   u.aq.zName);.  
1da86 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1da87 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
1da88 20 69 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d   if(.        db-
1da89 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20  >writeVdbeCnt>0 
1da8a 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56  || (u.aq.p1==SAV
1da8b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
1da8c 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  && db->activeVdb
1da8d 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20  eCnt>1).    ){. 
1da8e 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f       /* It is no
1da8f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  t possible to re
1da90 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
1da91 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68   savepoint if th
1da92 65 72 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  ere are.      **
1da93 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 74   active write st
1da94 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20  atements. It is 
1da95 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
1da96 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70  rollback a savep
1da97 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66  oint.      ** if
1da98 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
1da99 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
1da9a 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a   at all..      *
1da9b 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  /.      sqlite3S
1da9c 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1da9d 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20  rMsg, db,.      
1da9e 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76    "cannot %s sav
1da9f 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61  epoint - SQL sta
1daa0 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1daa1 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75  ess",.        (u
1daa2 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1daa3 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f  T_ROLLBACK ? "ro
1daa4 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73  llback": "releas
1daa5 65 22 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  e").      );.   
1daa6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1daa7 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  USY;.    }else{.
1daa8 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d  .      /* Determ
1daa9 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
1daaa 6f 74 20 74 68 69 73 20 69 73 20 61 20 74 72 61  ot this is a tra
1daab 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69  nsaction savepoi
1daac 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20  nt. If so,.     
1daad 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20   ** and this is 
1daae 61 20 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e  a RELEASE comman
1daaf 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72  d, then the curr
1dab0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
1dab1 20 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d        ** is comm
1dab2 69 74 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  itted..      */.
1dab3 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e        int isTran
1dab4 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70  saction = u.aq.p
1dab5 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1dab6 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61  ==0 && db->isTra
1dab7 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1dab8 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54  t;.      if( isT
1dab9 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e  ransaction && u.
1daba 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1dabb 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20  _RELEASE ){.    
1dabc 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71      if( (rc = sq
1dabd 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1dabe 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f  (p, 1))!=SQLITE_
1dabf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1dac0 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1dac1 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1dac2 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1dac3 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  it = 1;.        
1dac4 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48  if( sqlite3VdbeH
1dac5 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42  alt(p)==SQLITE_B
1dac6 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  USY ){.         
1dac7 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
1dac8 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1dac9 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  ommit = 0;.     
1daca 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20       p->rc = rc 
1dacb 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
1dacc 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64           goto vd
1dacd 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20  be_return;.     
1dace 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d     }.        db-
1dacf 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
1dad0 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20  vepoint = 0;.   
1dad1 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b       rc = p->rc;
1dad2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1dad3 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65        u.aq.iSave
1dad4 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76  point = db->nSav
1dad5 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53  epoint - u.aq.iS
1dad6 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20  avepoint - 1;.  
1dad7 20 20 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69        for(u.aq.i
1dad8 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d  i=0; u.aq.ii<db-
1dad9 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29  >nDb; u.aq.ii++)
1dada 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1dadb 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76   sqlite3BtreeSav
1dadc 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75  epoint(db->aDb[u
1dadd 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61  .aq.ii].pBt, u.a
1dade 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65  q.p1, u.aq.iSave
1dadf 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  point);.        
1dae0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1dae1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1dae2 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1dae3 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1dae4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1dae5 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  }.        if( u.
1dae6 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54  aq.p1==SAVEPOINT
1dae7 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62  _ROLLBACK && (db
1dae8 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49  ->flags&SQLITE_I
1dae9 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30  nternChanges)!=0
1daea 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
1daeb 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1daec 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1daed 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1daee 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1daef 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
1daf0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1daf1 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61  }..      /* Rega
1daf2 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
1daf3 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45  r this is a RELE
1daf4 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c  ASE or ROLLBACK,
1daf5 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20   destroy all.   
1daf6 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73     ** savepoints
1daf7 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f   nested inside o
1daf8 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  f the savepoint 
1daf9 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f  being operated o
1dafa 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  n. */.      whil
1dafb 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  e( db->pSavepoin
1dafc 74 21 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69  t!=u.aq.pSavepoi
1dafd 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  nt ){.        u.
1dafe 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53  aq.pTmp = db->pS
1daff 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20  avepoint;.      
1db00 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74    db->pSavepoint
1db01 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e   = u.aq.pTmp->pN
1db02 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ext;.        sql
1db03 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75  ite3DbFree(db, u
1db04 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20  .aq.pTmp);.     
1db05 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1db06 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  t--;.      }..  
1db07 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20      /* If it is 
1db08 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20  a RELEASE, then 
1db09 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65  destroy the save
1db0a 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72  point being oper
1db0b 61 74 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a  ated on.      **
1db0c 20 74 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61   too. If it is a
1db0d 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68   ROLLBACK TO, th
1db0e 65 6e 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65  en set the numbe
1db0f 72 20 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20  r of deferred.  
1db10 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e      ** constrain
1db11 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65  t violations pre
1db12 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61  sent in the data
1db13 62 61 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75  base to the valu
1db14 65 20 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a  e stored.      *
1db15 2a 20 77 68 65 6e 20 74 68 65 20 73 61 76 65 70  * when the savep
1db16 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64  oint was created
1db17 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .  */.      if( 
1db18 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49  u.aq.p1==SAVEPOI
1db19 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20  NT_RELEASE ){.  
1db1a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1db1b 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64  aq.pSavepoint==d
1db1c 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b  b->pSavepoint );
1db1d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61  .        db->pSa
1db1e 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70  vepoint = u.aq.p
1db1f 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1db20 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1db21 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71  3DbFree(db, u.aq
1db22 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20  .pSavepoint);.  
1db23 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72 61        if( !isTra
1db24 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
1db25 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70        db->nSavep
1db26 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20  oint--;.        
1db27 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
1db28 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65         db->nDefe
1db29 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e  rredCons = u.aq.
1db2a 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66  pSavepoint->nDef
1db2b 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20  erredCons;.     
1db2c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
1db2d 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1db2e 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20  ode: AutoCommit 
1db2f 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1db30 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61  * Set the databa
1db31 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  se auto-commit f
1db32 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20  lag to P1 (1 or 
1db33 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75  0). If P2 is tru
1db34 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20  e, roll.** back 
1db35 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63  any currently ac
1db36 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73  tive btree trans
1db37 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72  actions. If ther
1db38 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65  e are any active
1db39 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66  .** VMs (apart f
1db3a 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74  rom this one), t
1db3b 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66  hen a ROLLBACK f
1db3c 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20  ails.  A COMMIT 
1db3d 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72  fails if.** ther
1db3e 65 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69  e are active wri
1db3f 74 69 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69  ting VMs or acti
1db40 76 65 20 56 4d 73 20 74 68 61 74 20 75 73 65 20  ve VMs that use 
1db41 73 68 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a  shared cache..**
1db42 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1db43 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
1db44 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63  VM to halt..*/.c
1db45 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  ase OP_AutoCommi
1db46 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  t: {.#if 0  /* l
1db47 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1db48 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a  oved into u.ar *
1db49 2f 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41  /.  int desiredA
1db4a 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74  utoCommit;.  int
1db4b 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e   iRollback;.  in
1db4c 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64  t turnOnAC;.#end
1db4d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1db4e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1db4f 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72   u.ar */..  u.ar
1db50 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1db51 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  it = pOp->p1;.  
1db52 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d  u.ar.iRollback =
1db53 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72   pOp->p2;.  u.ar
1db54 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72  .turnOnAC = u.ar
1db55 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1db56 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43  it && !db->autoC
1db57 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28  ommit;.  assert(
1db58 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74   u.ar.desiredAut
1db59 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e  oCommit==1 || u.
1db5a 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  ar.desiredAutoCo
1db5b 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  mmit==0 );.  ass
1db5c 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65  ert( u.ar.desire
1db5d 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c  dAutoCommit==1 |
1db5e 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b  | u.ar.iRollback
1db5f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1db60 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
1db61 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c  nt>0 );  /* At l
1db62 65 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d  east this one VM
1db63 20 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20   is active */.. 
1db64 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e   if( u.ar.turnOn
1db65 41 43 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c  AC && u.ar.iRoll
1db66 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69  back && db->acti
1db67 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20  veVdbeCnt>1 ){. 
1db68 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e     /* If this in
1db69 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d  struction implem
1db6a 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20  ents a ROLLBACK 
1db6b 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72  and other VMs ar
1db6c 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72  e.    ** still r
1db6d 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72  unning, and a tr
1db6e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1db6f 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  ive, return an e
1db70 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a  rror indicating.
1db71 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
1db72 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63  other VMs must c
1db73 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20  omplete first.. 
1db74 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1db75 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1db76 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e  ErrMsg, db, "can
1db77 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61  not rollback tra
1db78 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20  nsaction - ".   
1db79 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d       "SQL statem
1db7a 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
1db7b 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ");.    rc = SQL
1db7c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
1db7d 65 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f  e if( u.ar.turnO
1db7e 6e 41 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f  nAC && !u.ar.iRo
1db7f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72  llback && db->wr
1db80 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a  iteVdbeCnt>0 ){.
1db81 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1db82 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65  nstruction imple
1db83 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61  ments a COMMIT a
1db84 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65  nd other VMs are
1db85 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20   writing.    ** 
1db86 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1db87 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
1db88 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75  the other VMs mu
1db89 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73  st complete firs
1db8a 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  t..    */.    sq
1db8b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1db8c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1db8d 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74  "cannot commit t
1db8e 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20  ransaction - ". 
1db8f 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74         "SQL stat
1db90 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65  ements in progre
1db91 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ss");.    rc = S
1db92 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65  QLITE_BUSY;.  }e
1db93 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73  lse if( u.ar.des
1db94 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d  iredAutoCommit!=
1db95 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
1db96 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69  {.    if( u.ar.i
1db97 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
1db98 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64    assert( u.ar.d
1db99 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
1db9a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
1db9b 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28  ite3RollbackAll(
1db9c 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61  db);.      db->a
1db9d 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20  utoCommit = 1;. 
1db9e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63     }else if( (rc
1db9f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
1dba0 65 63 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51  eckFk(p, 1))!=SQ
1dba1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1dba2 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1dba3 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  n;.    }else{.  
1dba4 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1dba5 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65  it = (u8)u.ar.de
1dba6 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b  siredAutoCommit;
1dba7 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1dba8 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53  e3VdbeHalt(p)==S
1dba9 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1dbaa 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1dbab 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ;.        db->au
1dbac 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28  toCommit = (u8)(
1dbad 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75  1-u.ar.desiredAu
1dbae 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20  toCommit);.     
1dbaf 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1dbb0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1dbb1 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72       goto vdbe_r
1dbb2 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20  eturn;.      }. 
1dbb3 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1dbb4 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d   db->nStatement=
1dbb5 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
1dbb6 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
1dbb7 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  (db);.    if( p-
1dbb8 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
1dbb9 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1dbba 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65  ITE_DONE;.    }e
1dbbb 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1dbbc 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1dbbd 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62    }.    goto vdb
1dbbe 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  e_return;.  }els
1dbbf 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  e{.    sqlite3Se
1dbc0 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1dbc1 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20  Msg, db,.       
1dbc2 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41   (!u.ar.desiredA
1dbc3 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e  utoCommit)?"cann
1dbc4 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73  ot start a trans
1dbc5 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20  action within a 
1dbc6 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20  transaction":(. 
1dbc7 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f         (u.ar.iRo
1dbc8 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20  llback)?"cannot 
1dbc9 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72  rollback - no tr
1dbca 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1dbcb 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20  ive":.          
1dbcc 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
1dbcd 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61   commit - no tra
1dbce 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
1dbcf 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d  ve"));..    rc =
1dbd0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1dbd1 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1dbd2 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61  * Opcode: Transa
1dbd3 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20  ction P1 P2 * * 
1dbd4 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20  *.**.** Begin a 
1dbd5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68  transaction.  Th
1dbd6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e  e transaction en
1dbd7 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74  ds when a Commit
1dbd8 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20   or Rollback.** 
1dbd9 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e  opcode is encoun
1dbda 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e  tered.  Dependin
1dbdb 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46  g on the ON CONF
1dbdc 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68  LICT setting, th
1dbdd 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
1dbde 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72   might also be r
1dbdf 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e  olled back if an
1dbe0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
1dbe1 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  tered..**.** P1 
1dbe2 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
1dbe3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1dbe4 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74  e on which the t
1dbe5 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
1dbe6 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78   started.  Index
1dbe7 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64   0 is the main d
1dbe8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1dbe9 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a   index 1 is the.
1dbea 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  ** file used for
1dbeb 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1dbec 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32  s.  Indices of 2
1dbed 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65   or more are use
1dbee 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65  d for.** attache
1dbef 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a  d databases..**.
1dbf0 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d  ** If P2 is non-
1dbf1 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69  zero, then a wri
1dbf2 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
1dbf3 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45  s started.  A RE
1dbf4 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a  SERVED lock is.*
1dbf5 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68  * obtained on th
1dbf6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1dbf7 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61  when a write-tra
1dbf8 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
1dbf9 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65  ted.  No.** othe
1dbfa 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74  r process can st
1dbfb 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74  art another writ
1dbfc 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  e transaction wh
1dbfd 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63  ile this transac
1dbfe 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72  tion is.** under
1dbff 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61  way.  Starting a
1dc00 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1dc01 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20  on also creates 
1dc02 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
1dc03 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74  al. A.** write t
1dc04 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20  ransaction must 
1dc05 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72  be started befor
1dc06 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61  e any changes ca
1dc07 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65  n be made to the
1dc08 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49  .** database.  I
1dc09 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65  f P2 is 2 or gre
1dc0a 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43  ater then an EXC
1dc0b 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61  LUSIVE lock is a
1dc0c 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  lso obtained.** 
1dc0d 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a  on the file..**.
1dc0e 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74 72  ** If a write-tr
1dc0f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
1dc10 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64 62  rted and the Vdb
1dc11 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61  e.usesStmtJourna
1dc12 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75  l flag is.** tru
1dc13 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73 20  e (this flag is 
1dc14 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65 20  set if the Vdbe 
1dc15 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20  may modify more 
1dc16 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64  than one row and
1dc17 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e   may.** throw an
1dc18 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e   ABORT exception
1dc19 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ), a statement t
1dc1a 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61  ransaction may a
1dc1b 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a  lso be opened..*
1dc1c 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61  * More specifica
1dc1d 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  lly, a statement
1dc1e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1dc1f 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20 64  opened iff the d
1dc20 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
1dc21 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74  ction is current
1dc22 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f  ly not in autoco
1dc23 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66  mmit mode, or if
1dc24 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72   there are other
1dc25 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 65  .** active state
1dc26 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65  ments. A stateme
1dc27 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  nt transaction a
1dc28 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 74  llows the affect
1dc29 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42  s of this.** VDB
1dc2a 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  E to be rolled b
1dc2b 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72  ack after an err
1dc2c 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  or without havin
1dc2d 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74  g to roll back t
1dc2e 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61  he.** entire tra
1dc2f 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  nsaction. If no 
1dc30 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
1dc31 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d  ered, the statem
1dc32 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ent transaction.
1dc33 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69  ** will automati
1dc34 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65  cally commit whe
1dc35 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73  n the VDBE halts
1dc36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73  ..**.** If P2 is
1dc37 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65   zero, then a re
1dc38 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69  ad-lock is obtai
1dc39 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
1dc3a 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73  ase file..*/.cas
1dc3b 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e  e OP_Transaction
1dc3c 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1dc3d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1dc3e 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f  ved into u.as */
1dc3f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23  .  Btree *pBt;.#
1dc40 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1dc41 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dc42 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61  nto u.as */..  a
1dc43 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1dc44 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1dc45 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1dc46 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1dc47 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1dc48 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74  =0 );.  u.as.pBt
1dc49 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e   = db->aDb[pOp->
1dc4a 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20  p1].pBt;..  if( 
1dc4b 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20  u.as.pBt ){.    
1dc4c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dc4d 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73  eBeginTrans(u.as
1dc4e 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a  .pBt, pOp->p2);.
1dc4f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1dc50 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  TE_BUSY ){.     
1dc51 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
1dc52 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1dc53 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
1dc54 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1dc55 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  urn;.    }.    i
1dc56 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1dc57 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
1dc58 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1dc59 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  r;.    }..    if
1dc5a 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e  ( pOp->p2 && p->
1dc5b 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a  usesStmtJournal.
1dc5c 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74       && (db->aut
1dc5d 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
1dc5e 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1dc5f 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  1).    ){.      
1dc60 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1dc61 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e  treeIsInTrans(u.
1dc62 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20  as.pBt) );.     
1dc63 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65   if( p->iStateme
1dc64 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  nt==0 ){.       
1dc65 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74   assert( db->nSt
1dc66 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62  atement>=0 && db
1dc67 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20  ->nSavepoint>=0 
1dc68 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e  );.        db->n
1dc69 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20  Statement++;.   
1dc6a 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65       p->iStateme
1dc6b 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f  nt = db->nSavepo
1dc6c 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65  int + db->nState
1dc6d 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ment;.      }.  
1dc6e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1dc6f 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75  BtreeBeginStmt(u
1dc70 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61  .as.pBt, p->iSta
1dc71 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20  tement);..      
1dc72 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63 75 72  /* Store the cur
1dc73 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
1dc74 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1dc75 65 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  es deferred cons
1dc76 74 72 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20  traint.      ** 
1dc77 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20  counter. If the 
1dc78 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1dc79 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62  ction needs to b
1dc7a 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20  e rolled back,. 
1dc7b 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75       ** the valu
1dc7c 65 20 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65  e of this counte
1dc7d 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  r needs to be re
1dc7e 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a  stored too.  */.
1dc7f 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65        p->nStmtDe
1dc80 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66  fCons = db->nDef
1dc81 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d  erredCons;.    }
1dc82 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1dc83 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64  ./* Opcode: Read
1dc84 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
1dc85 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63  * *.**.** Read c
1dc86 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20  ookie number P3 
1dc87 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31  from database P1
1dc88 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e   and write it in
1dc89 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1dc8a 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20  ** P3==1 is the 
1dc8b 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20  schema version. 
1dc8c 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61   P3==2 is the da
1dc8d 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a  tabase format..*
1dc8e 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72  * P3==3 is the r
1dc8f 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72  ecommended pager
1dc90 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64   cache size, and
1dc91 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
1dc92 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e  0 is.** the main
1dc93 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1dc94 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
1dc95 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
1dc96 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74  ed to store.** t
1dc97 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1dc98 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
1dc99 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b  t be a read-lock
1dc9a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1dc9b 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73   (either a trans
1dc9c 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62  action.** must b
1dc9d 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65  e started or the
1dc9e 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70  re must be an op
1dc9f 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72  en cursor) befor
1dca0 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74  e.** executing t
1dca1 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  his instruction.
1dca2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64  .*/.case OP_Read
1dca3 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20  Cookie: {       
1dca4 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1dca5 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69  prerelease */.#i
1dca6 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1dca7 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dca8 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74  to u.at */.  int
1dca9 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44   iMeta;.  int iD
1dcaa 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65  b;.  int iCookie
1dcab 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1dcac 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1dcad 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a  d into u.at */..
1dcae 20 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70    u.at.iDb = pOp
1dcaf 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f  ->p1;.  u.at.iCo
1dcb0 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  okie = pOp->p3;.
1dcb1 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1dcb2 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  3<SQLITE_N_BTREE
1dcb3 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72  _META );.  asser
1dcb4 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26  t( u.at.iDb>=0 &
1dcb5 26 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e  & u.at.iDb<db->n
1dcb6 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1dcb7 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62  db->aDb[u.at.iDb
1dcb8 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ].pBt!=0 );.  as
1dcb9 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1dcba 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69  ask & (1<<u.at.i
1dcbb 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71  Db))!=0 );..  sq
1dcbc 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
1dcbd 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69  a(db->aDb[u.at.i
1dcbe 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43  Db].pBt, u.at.iC
1dcbf 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75  ookie, (u32 *)&u
1dcc0 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f  .at.iMeta);.  pO
1dcc1 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69  ut->u.i = u.at.i
1dcc2 4d 65 74 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Meta;.  break;.}
1dcc3 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74  ../* Opcode: Set
1dcc4 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
1dcc5 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1dcc6 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1dcc7 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65  egister P3 (inte
1dcc8 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e  rpreted as an in
1dcc9 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63  teger).** into c
1dcca 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20  ookie number P2 
1dccb 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  of database P1. 
1dccc 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63   P2==1 is the sc
1dccd 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a  hema version.  .
1dcce 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20  ** P2==2 is the 
1dccf 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
1dcd0 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65   P2==3 is the re
1dcd1 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20  commended pager 
1dcd2 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20  cache .** size, 
1dcd3 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50  and so forth.  P
1dcd4 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e  1==0 is the main
1dcd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1dcd6 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
1dcd7 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
1dcd8 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
1dcd9 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1dcda 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61  ..**.** A transa
1dcdb 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74  ction must be st
1dcdc 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65  arted before exe
1dcdd 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f  cuting this opco
1dcde 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  de..*/.case OP_S
1dcdf 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20  etCookie: {     
1dce0 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20    /* in3 */.#if 
1dce1 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1dce2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dce3 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70   u.au */.  Db *p
1dce4 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  Db;.#endif /* lo
1dce5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1dce6 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f  ved into u.au */
1dce7 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1dce8 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45  p2<SQLITE_N_BTRE
1dce9 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65  E_META );.  asse
1dcea 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1dceb 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1dcec 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1dced 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1dcee 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1dcef 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20  );.  u.au.pDb = 
1dcf0 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31  &db->aDb[pOp->p1
1dcf1 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  ];.  assert( u.a
1dcf2 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b  u.pDb->pBt!=0 );
1dcf3 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b  .  pIn3 = &aMem[
1dcf4 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69  pOp->p3];.  sqli
1dcf5 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1dcf6 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a  rify(pIn3);.  /*
1dcf7 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20   See note about 
1dcf8 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f  index shifting o
1dcf9 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20  n OP_ReadCookie 
1dcfa 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1dcfb 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
1dcfc 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20  (u.au.pDb->pBt, 
1dcfd 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49  pOp->p2, (int)pI
1dcfe 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20  n3->u.i);.  if( 
1dcff 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53  pOp->p2==BTREE_S
1dd00 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b  CHEMA_VERSION ){
1dd01 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65  .    /* When the
1dd02 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
1dd03 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74  hanges, record t
1dd04 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e  he new cookie in
1dd05 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20  ternally */.    
1dd06 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d  u.au.pDb->pSchem
1dd07 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
1dd08 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e   = (int)pIn3->u.
1dd09 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  i;.    db->flags
1dd0a 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
1dd0b 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73  nChanges;.  }els
1dd0c 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42  e if( pOp->p2==B
1dd0d 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
1dd0e 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72   ){.    /* Recor
1dd0f 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  d changes in the
1dd10 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a   file format */.
1dd11 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53      u.au.pDb->pS
1dd12 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
1dd13 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75  at = (u8)pIn3->u
1dd14 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  .i;.  }.  if( pO
1dd15 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20  p->p1==1 ){.    
1dd16 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c  /* Invalidate al
1dd17 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  l prepared state
1dd18 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74  ments whenever t
1dd19 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
1dd1a 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69  .    ** schema i
1dd1b 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b  s changed.  Tick
1dd1c 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20  et #1644 */.    
1dd1d 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1dd1e 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1dd1f 64 62 29 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69  db);.    p->expi
1dd20 72 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  red = 0;.  }.  b
1dd21 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1dd22 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65  de: VerifyCookie
1dd23 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43   P1 P2 *.**.** C
1dd24 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f  heck the value o
1dd25 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73  f global databas
1dd26 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62  e parameter numb
1dd27 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68  er 0 (the.** sch
1dd28 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64  ema version) and
1dd29 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
1dd2a 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a   equal to P2.  .
1dd2b 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74  ** P1 is the dat
1dd2c 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69  abase number whi
1dd2d 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20  ch is 0 for the 
1dd2e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1dd2f 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20  le.** and 1 for 
1dd30 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67  the file holding
1dd31 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1dd32 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65  s and some highe
1dd33 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20  r number.** for 
1dd34 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
1dd35 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ses..**.** The c
1dd36 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74  ookie changes it
1dd37 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72  s value whenever
1dd38 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1dd39 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a  hema changes..**
1dd3a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   This operation 
1dd3b 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63  is used to detec
1dd3c 74 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20  t when that the 
1dd3d 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67  cookie has chang
1dd3e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74  ed.** and that t
1dd3f 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65  he current proce
1dd40 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65  ss needs to rere
1dd41 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a  ad the schema..*
1dd42 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72  *.** Either a tr
1dd43 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20  ansaction needs 
1dd44 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61  to have been sta
1dd45 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70  rted or an OP_Op
1dd46 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62  en needs.** to b
1dd47 65 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65  e executed (to e
1dd48 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20  stablish a read 
1dd49 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69  lock) before thi
1dd4a 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69  s opcode is.** i
1dd4b 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  nvoked..*/.case 
1dd4c 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a  OP_VerifyCookie:
1dd4d 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1dd4e 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1dd4f 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a  ed into u.av */.
1dd50 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42    int iMeta;.  B
1dd51 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69  tree *pBt;.#endi
1dd52 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1dd53 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1dd54 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.av */.  assert
1dd55 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1dd56 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1dd57 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1dd58 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1dd59 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1dd5a 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62  .  u.av.pBt = db
1dd5b 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1dd5c 42 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70  Bt;.  if( u.av.p
1dd5d 42 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Bt ){.    sqlite
1dd5e 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e  3BtreeGetMeta(u.
1dd5f 61 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43  av.pBt, BTREE_SC
1dd60 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75  HEMA_VERSION, (u
1dd61 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61  32 *)&u.av.iMeta
1dd62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1dd63 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a  u.av.iMeta = 0;.
1dd64 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69    }.  if( u.av.i
1dd65 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b  Meta!=pOp->p2 ){
1dd66 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1dd67 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1dd68 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
1dd69 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  sg = sqlite3DbSt
1dd6a 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61  rDup(db, "databa
1dd6b 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  se schema has ch
1dd6c 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20  anged");.    /* 
1dd6d 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f  If the schema-co
1dd6e 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61  okie from the da
1dd6f 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63  tabase file matc
1dd70 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20  hes the cookie. 
1dd71 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74     ** stored wit
1dd72 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  h the in-memory 
1dd73 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
1dd74 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f  f the schema, do
1dd75 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f  .    ** not relo
1dd76 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72  ad the schema fr
1dd77 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
1dd78 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
1dd79 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74   ** If virtual-t
1dd7a 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65  ables are in use
1dd7b 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75  , this is not ju
1dd7c 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  st an optimizati
1dd7d 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e  on..    ** Often
1dd7e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65  , v-tables store
1dd7f 20 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f   their data in o
1dd80 74 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c  ther SQLite tabl
1dd81 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a  es, which.    **
1dd82 20 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f   are queried fro
1dd83 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29  m within xNext()
1dd84 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62   and other v-tab
1dd85 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67  le methods using
1dd86 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64  .    ** prepared
1dd87 20 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63   queries. If suc
1dd88 68 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74  h a query is out
1dd89 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20  -of-date, we do 
1dd8a 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20  not want to.    
1dd8b 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64  ** discard the d
1dd8c 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20  atabase schema, 
1dd8d 61 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65  as the user code
1dd8e 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
1dd8f 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65  e.    ** v-table
1dd90 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62   would have to b
1dd91 65 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20  e ready for the 
1dd92 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
1dd93 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20  ucture itself.  
1dd94 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c    ** to be inval
1dd95 69 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20  idated whenever 
1dd96 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69  sqlite3_step() i
1dd97 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
1dd98 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d  thin.    ** a v-
1dd99 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20  table method..  
1dd9a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d    */.    if( db-
1dd9b 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53  >aDb[pOp->p1].pS
1dd9c 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f  chema->schema_co
1dd9d 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61  okie!=u.av.iMeta
1dd9e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1dd9f 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1dda0 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31  hema(db, pOp->p1
1dda1 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  );.    }..    sq
1dda2 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
1dda3 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
1dda4 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1dda5 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20  TE_SCHEMA;.  }. 
1dda6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1dda7 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50  code: OpenRead P
1dda8 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1dda9 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d  .** Open a read-
1ddaa 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20  only cursor for 
1ddab 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
1ddac 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61  le whose root pa
1ddad 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61  ge is.** P2 in a
1ddae 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1ddaf 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
1ddb0 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  le is determined
1ddb1 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d   by P3. .** P3==
1ddb2 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e  0 means the main
1ddb3 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31   database, P3==1
1ddb4 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62   means the datab
1ddb5 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a  ase used for .**
1ddb6 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
1ddb7 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e  s, and P3>1 mean
1ddb8 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65  s used the corre
1ddb9 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65  sponding attache
1ddba 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20  d.** database.  
1ddbb 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72  Give the new cur
1ddbc 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65  sor an identifie
1ddbd 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31  r of P1.  The P1
1ddbe 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20  .** values need 
1ddbf 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75  not be contiguou
1ddc0 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c  s but all P1 val
1ddc1 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d  ues should be sm
1ddc2 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  all integers..**
1ddc3 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   It is an error 
1ddc4 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67  for P1 to be neg
1ddc5 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ative..**.** If 
1ddc6 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74  P5!=0 then use t
1ddc7 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1ddc8 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65  gister P2 as the
1ddc9 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a   root page, not.
1ddca 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
1ddcb 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a  P2 itself..**.**
1ddcc 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61   There will be a
1ddcd 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68   read lock on th
1ddce 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65  e database whene
1ddcf 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a  ver there is an.
1ddd0 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20  ** open cursor. 
1ddd1 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
1ddd2 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72   was unlocked pr
1ddd3 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74  ior to this inst
1ddd4 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20  ruction.** then 
1ddd5 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61  a read lock is a
1ddd6 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20  cquired as part 
1ddd7 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  of this instruct
1ddd8 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20  ion.  A read.** 
1ddd9 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65  lock allows othe
1ddda 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72  r processes to r
1dddb 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
1dddc 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a   but prohibits.*
1dddd 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  * any other proc
1ddde 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69  ess from modifyi
1dddf 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
1dde0 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20    The read lock 
1dde1 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77  is.** released w
1dde2 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20  hen all cursors 
1dde3 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20  are closed.  If 
1dde4 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1dde5 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20   attempts.** to 
1dde6 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  get a read lock 
1dde7 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73  but fails, the s
1dde8 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73  cript terminates
1dde9 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49   with an.** SQLI
1ddea 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f  TE_BUSY error co
1ddeb 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34  de..**.** The P4
1ddec 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69   value may be ei
1dded 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20  ther an integer 
1ddee 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20  (P4_INT32) or a 
1ddef 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20  pointer to.** a 
1ddf0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1ddf1 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20  e (P4_KEYINFO). 
1ddf2 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74  If it is a point
1ddf3 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20  er to a KeyInfo 
1ddf4 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74  .** structure, t
1ddf5 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75  hen said structu
1ddf6 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63  re defines the c
1ddf7 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61  ontent and colla
1ddf8 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63  ting .** sequenc
1ddf9 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62  e of the index b
1ddfa 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68  eing opened. Oth
1ddfb 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73  erwise, if P4 is
1ddfc 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20   an integer .** 
1ddfd 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74  value, it is set
1ddfe 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
1ddff 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1de00 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65   table..**.** Se
1de01 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65  e also OpenWrite
1de02 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1de03 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20  OpenWrite P1 P2 
1de04 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f  P3 P4 P5.**.** O
1de05 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65  pen a read/write
1de06 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31   cursor named P1
1de07 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72   on the table or
1de08 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1de09 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e  t.** page is P2.
1de0a 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73    Or if P5!=0 us
1de0b 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1de0c 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
1de0d 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  find the.** root
1de0e 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   page..**.** The
1de0f 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65   P4 value may be
1de10 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67   either an integ
1de11 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72  er (P4_INT32) or
1de12 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
1de13 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
1de14 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f  ture (P4_KEYINFO
1de15 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f  ). If it is a po
1de16 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
1de17 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  fo .** structure
1de18 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75  , then said stru
1de19 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
1de1a 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f  e content and co
1de1b 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75  llating .** sequ
1de1c 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65  ence of the inde
1de1d 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20  x being opened. 
1de1e 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34  Otherwise, if P4
1de1f 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a   is an integer .
1de20 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20  ** value, it is 
1de21 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
1de22 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1de23 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f  the table, or to
1de24 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20   the.** largest 
1de25 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c  index of any col
1de26 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  umn of the table
1de27 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c   that is actuall
1de28 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  y used..**.** Th
1de29 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  is instruction w
1de2a 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f  orks just like O
1de2b 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74  penRead except t
1de2c 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65  hat it opens the
1de2d 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65   cursor.** in re
1de2e 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20  ad/write mode.  
1de2f 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c  For a given tabl
1de30 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20  e, there can be 
1de31 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64  one or more read
1de32 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73  -only.** cursors
1de33 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61   or a single rea
1de34 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62  d/write cursor b
1de35 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a  ut not both..**.
1de36 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e  ** See also Open
1de37 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  Read..*/.case OP
1de38 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20  _OpenRead:.case 
1de39 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a  OP_OpenWrite: {.
1de3a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1de3b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1de3c 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69  into u.aw */.  i
1de3d 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79  nt nField;.  Key
1de3e 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a  Info *pKeyInfo;.
1de3f 20 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20    int p2;.  int 
1de40 69 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61  iDb;.  int wrFla
1de41 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a  g;.  Btree *pX;.
1de42 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1de43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23  ur;.  Db *pDb;.#
1de44 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1de45 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1de46 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69  nto u.aw */..  i
1de47 66 28 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b  f( p->expired ){
1de48 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1de49 5f 41 42 4f 52 54 3b 0a 20 20 20 20 62 72 65 61  _ABORT;.    brea
1de4a 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e  k;.  }..  u.aw.n
1de4b 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61  Field = 0;.  u.a
1de4c 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a  w.pKeyInfo = 0;.
1de4d 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d    u.aw.p2 = pOp-
1de4e 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20  >p2;.  u.aw.iDb 
1de4f 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73  = pOp->p3;.  ass
1de50 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30  ert( u.aw.iDb>=0
1de51 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d   && u.aw.iDb<db-
1de52 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1de53 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1de54 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29  & (1<<u.aw.iDb))
1de55 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44  !=0 );.  u.aw.pD
1de56 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61  b = &db->aDb[u.a
1de57 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70  w.iDb];.  u.aw.p
1de58 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42  X = u.aw.pDb->pB
1de59 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  t;.  assert( u.a
1de5a 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28  w.pX!=0 );.  if(
1de5b 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1de5c 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20  _OpenWrite ){.  
1de5d 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20    u.aw.wrFlag = 
1de5e 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e  1;.    if( u.aw.
1de5f 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1de60 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d  le_format < p->m
1de61 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
1de62 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69  t ){.      p->mi
1de63 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74  nWriteFileFormat
1de64 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63   = u.aw.pDb->pSc
1de65 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1de66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  t;.    }.  }else
1de67 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61  {.    u.aw.wrFla
1de68 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  g = 0;.  }.  if(
1de69 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20   pOp->p5 ){.    
1de6a 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e  assert( u.aw.p2>
1de6b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1de6c 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65   u.aw.p2<=p->nMe
1de6d 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20  m );.    pIn2 = 
1de6e 26 61 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a  &aMem[u.aw.p2];.
1de6f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1de70 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e  emIntegerify(pIn
1de71 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20  2);.    u.aw.p2 
1de72 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69  = (int)pIn2->u.i
1de73 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61  ;.    /* The u.a
1de74 77 2e 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79  w.p2 value alway
1de75 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70  s comes from a p
1de76 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61  rior OP_CreateTa
1de77 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20  ble opcode and. 
1de78 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64     ** that opcod
1de79 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65  e will always se
1de7a 74 20 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61  t the u.aw.p2 va
1de7b 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65  lue to 2 or more
1de7c 20 6f 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20   or else fail.. 
1de7d 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77     ** If there w
1de7e 65 72 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74  ere a failure, t
1de7f 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
1de80 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65  ement would have
1de81 20 68 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62   halted.    ** b
1de82 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74  efore reaching t
1de83 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  his instruction.
1de84 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45   */.    if( NEVE
1de85 52 28 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b  R(u.aw.p2<2) ) {
1de86 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1de87 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1de88 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1de89 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1de8a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1de8b 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1de8c 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75  KEYINFO ){.    u
1de8d 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  .aw.pKeyInfo = p
1de8e 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
1de8f 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e  .    u.aw.pKeyIn
1de90 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
1de91 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e  >db);.    u.aw.n
1de92 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65  Field = u.aw.pKe
1de93 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b  yInfo->nField+1;
1de94 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70  .  }else if( pOp
1de95 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
1de96 33 32 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e  32 ){.    u.aw.n
1de97 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e  Field = pOp->p4.
1de98 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  i;.  }.  assert(
1de99 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20   pOp->p1>=0 );. 
1de9a 20 75 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c   u.aw.pCur = all
1de9b 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70  ocateCursor(p, p
1de9c 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69  Op->p1, u.aw.nFi
1de9d 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31  eld, u.aw.iDb, 1
1de9e 29 3b 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43  );.  if( u.aw.pC
1de9f 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ur==0 ) goto no_
1dea0 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72  mem;.  u.aw.pCur
1dea1 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1dea2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1dea3 65 65 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58  eeCursor(u.aw.pX
1dea4 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e  , u.aw.p2, u.aw.
1dea5 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65  wrFlag, u.aw.pKe
1dea6 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72  yInfo, u.aw.pCur
1dea7 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e  ->pCursor);.  u.
1dea8 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  aw.pCur->pKeyInf
1dea9 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66  o = u.aw.pKeyInf
1deaa 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69  o;..  /* Since i
1deab 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65  t performs no me
1deac 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1dead 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20  or IO, the only 
1deae 76 61 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a  values that.  **
1deaf 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1deb0 73 6f 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e  sor() may return
1deb1 20 61 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54   are SQLITE_EMPT
1deb2 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e  Y and SQLITE_OK.
1deb3 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50  .  ** SQLITE_EMP
1deb4 54 59 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72  TY is only retur
1deb5 6e 65 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74  ned when attempt
1deb6 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ing to open the 
1deb7 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65  table.  ** roote
1deb8 64 20 61 74 20 70 61 67 65 20 31 20 6f 66 20 61  d at page 1 of a
1deb9 20 7a 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62   zero-byte datab
1deba 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ase.  */.  asser
1debb 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d  t( rc==SQLITE_EM
1debc 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  PTY || rc==SQLIT
1debd 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63  E_OK );.  if( rc
1debe 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29  ==SQLITE_EMPTY )
1debf 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d  {.    u.aw.pCur-
1dec0 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  >pCursor = 0;.  
1dec1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1dec2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
1dec3 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69  the VdbeCursor.i
1dec4 73 54 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64  sTable and isInd
1dec5 65 78 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72  ex variables. Pr
1dec6 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20  evious versions 
1dec7 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75  of.  ** SQLite u
1dec8 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20  sed to check if 
1dec9 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c  the root-page fl
1deca 61 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74  ags were sane at
1decb 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a   this point.  **
1decc 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61   and report data
1decd 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
1dece 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74  if they were not
1decf 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b  , but this check
1ded0 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20   has.  ** since 
1ded1 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62  moved into the b
1ded2 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a  tree layer.  */.
1ded3 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54    u.aw.pCur->isT
1ded4 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79  able = pOp->p4ty
1ded5 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a  pe!=P4_KEYINFO;.
1ded6 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49    u.aw.pCur->isI
1ded7 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75  ndex = !u.aw.pCu
1ded8 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72  r->isTable;.  br
1ded9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1deda 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  e: OpenEphemeral
1dedb 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
1dedc 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63  .** Open a new c
1dedd 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72  ursor P1 to a tr
1dede 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  ansient table..*
1dedf 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20  * The cursor is 
1dee0 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65  always opened re
1dee1 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66  ad/write even if
1dee2 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61   .** the main da
1dee3 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f  tabase is read-o
1dee4 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69  nly.  The transi
1dee5 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a  ent or virtual.*
1dee6 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74  * table is delet
1dee7 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
1dee8 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   when the cursor
1dee9 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a   is closed..**.*
1deea 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62  * P2 is the numb
1deeb 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
1deec 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1deed 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  le..** The curso
1deee 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54  r points to a BT
1deef 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d  ree table if P4=
1def0 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65  =0 and to a BTre
1def1 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34  e index.** if P4
1def2 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50   is not 0.  If P
1def3 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69  4 is not NULL, i
1def4 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65  t points to a Ke
1def5 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  yInfo structure.
1def6 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ** that defines 
1def7 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65  the format of ke
1def8 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ys in the index.
1def9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1defa 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c  de was once call
1defb 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75  ed OpenTemp.  Bu
1defc 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a  t that created.*
1defd 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61  * confusion beca
1defe 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74 65  use the term "te
1deff 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74  mp table", might
1df00 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a   refer either.**
1df01 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65   to a TEMP table
1df02 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65   at the SQL leve
1df03 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65  l, or to a table
1df04 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68   opened by.** th
1df05 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e  is opcode.  Then
1df06 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73   this opcode was
1df07 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61   call OpenVirtua
1df08 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20  l.  But.** that 
1df09 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f  created confusio
1df0a 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65  n with the whole
1df0b 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69   virtual-table i
1df0c 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  dea..*/.case OP_
1df0d 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b  OpenEphemeral: {
1df0e 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1df0f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1df10 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20   into u.ax */.  
1df11 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b  VdbeCursor *pCx;
1df12 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1df13 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1df14 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20   into u.ax */.  
1df15 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
1df16 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20   openFlags =.   
1df17 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
1df18 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20  EADWRITE |.     
1df19 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
1df1a 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ATE |.      SQLI
1df1b 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
1df1c 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
1df1d 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
1df1e 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  OSE |.      SQLI
1df1f 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
1df20 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28  T_DB;..  assert(
1df21 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20   pOp->p1>=0 );. 
1df22 20 75 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f   u.ax.pCx = allo
1df23 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f  cateCursor(p, pO
1df24 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20  p->p1, pOp->p2, 
1df25 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e  -1, 1);.  if( u.
1df26 61 78 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f  ax.pCx==0 ) goto
1df27 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e   no_mem;.  u.ax.
1df28 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  pCx->nullRow = 1
1df29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1df2a 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
1df2b 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45   0, 1, SQLITE_DE
1df2c 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45  FAULT_TEMP_CACHE
1df2d 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73  _SIZE, openFlags
1df2e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1df2f 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 2e               &u.
1df30 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20  ax.pCx->pBt);.  
1df31 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1df32 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
1df33 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
1df34 72 61 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70  rans(u.ax.pCx->p
1df35 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66  Bt, 1);.  }.  if
1df36 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1df37 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74  ){.    /* If a t
1df38 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69  ransient index i
1df39 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 65 61  s required, crea
1df3a 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67  te it by calling
1df3b 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  .    ** sqlite3B
1df3c 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
1df3d 29 20 77 69 74 68 20 74 68 65 20 42 54 52 45 45  ) with the BTREE
1df3e 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62  _ZERODATA flag b
1df3f 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65  efore.    ** ope
1df40 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72  ning it. If a tr
1df41 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73  ansient table is
1df42 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20   required, just 
1df43 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61  use the.    ** a
1df44 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
1df45 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 68 20  ated table with 
1df46 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20  root-page 1 (an 
1df47 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20  INTKEY table).. 
1df48 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f     */.    if( pO
1df49 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29  p->p4.pKeyInfo )
1df4a 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f  {.      int pgno
1df4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1df4c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1df4d 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20  KEYINFO );.     
1df4e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1df4f 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e  eeCreateTable(u.
1df50 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67  ax.pCx->pBt, &pg
1df51 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41  no, BTREE_ZERODA
1df52 54 41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  TA);.      if( r
1df53 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1df54 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1df55 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f  pgno==MASTER_ROO
1df56 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72  T+1 );.        r
1df57 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1df58 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d  Cursor(u.ax.pCx-
1df59 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20  >pBt, pgno, 1,. 
1df5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1df5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1df5c 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34  KeyInfo*)pOp->p4
1df5d 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43  .z, u.ax.pCx->pC
1df5e 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20  ursor);.        
1df5f 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e  u.ax.pCx->pKeyIn
1df60 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1df61 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75  yInfo;.        u
1df62 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66  .ax.pCx->pKeyInf
1df63 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e  o->enc = ENC(p->
1df64 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  db);.      }.   
1df65 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54     u.ax.pCx->isT
1df66 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65  able = 0;.    }e
1df67 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1df68 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1df69 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74  or(u.ax.pCx->pBt
1df6a 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31  , MASTER_ROOT, 1
1df6b 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70  , 0, u.ax.pCx->p
1df6c 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75  Cursor);.      u
1df6d 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65  .ax.pCx->isTable
1df6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
1df6f 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e    u.ax.pCx->isIn
1df70 64 65 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d  dex = !u.ax.pCx-
1df71 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61  >isTable;.  brea
1df72 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1df73 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50   OpenPseudo P1 P
1df74 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f  2 P3 * *.**.** O
1df75 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72  pen a new cursor
1df76 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20   that points to 
1df77 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61  a fake table tha
1df78 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e  t contains a sin
1df79 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61  gle.** row of da
1df7a 74 61 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ta.  The content
1df7b 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77   of that one row
1df7c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   in the content 
1df7d 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67  of memory.** reg
1df7e 69 73 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74  ister P2.  In ot
1df7f 68 65 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f  her words, curso
1df80 72 20 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20  r P1 becomes an 
1df81 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a  alias for the .*
1df82 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65  * MEM_Blob conte
1df83 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  nt contained in 
1df84 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1df85 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c  ** A pseudo-tabl
1df86 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  e created by thi
1df87 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  s opcode is used
1df88 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73   to hold the a s
1df89 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74  ingle.** row out
1df8a 70 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72  put from the sor
1df8b 74 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20  ter so that the 
1df8c 72 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d  row can be decom
1df8d 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e  posed into.** in
1df8e 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73  dividual columns
1df8f 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f   using the OP_Co
1df90 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68  lumn opcode.  Th
1df91 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
1df92 64 65 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c  de.** is the onl
1df93 79 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20  y cursor opcode 
1df94 74 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20  that works with 
1df95 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a  a pseudo-table..
1df96 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20  **.** P3 is the 
1df97 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  number of fields
1df98 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20   in the records 
1df99 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f  that will be sto
1df9a 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73  red by.** the ps
1df9b 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63  eudo-table..*/.c
1df9c 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  ase OP_OpenPseud
1df9d 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  o: {.#if 0  /* l
1df9e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1df9f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a  oved into u.ay *
1dfa0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1dfa1 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  pCx;.#endif /* l
1dfa2 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1dfa3 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a  oved into u.ay *
1dfa4 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1dfa5 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61  ->p1>=0 );.  u.a
1dfa6 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65  y.pCx = allocate
1dfa7 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70  Cursor(p, pOp->p
1dfa8 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20  1, pOp->p3, -1, 
1dfa9 30 29 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70  0);.  if( u.ay.p
1dfaa 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  Cx==0 ) goto no_
1dfab 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d  mem;.  u.ay.pCx-
1dfac 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
1dfad 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f  u.ay.pCx->pseudo
1dfae 54 61 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e  TableReg = pOp->
1dfaf 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e  p2;.  u.ay.pCx->
1dfb0 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75  isTable = 1;.  u
1dfb1 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78  .ay.pCx->isIndex
1dfb2 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1dfb3 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f  ../* Opcode: Clo
1dfb4 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  se P1 * * * *.**
1dfb5 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73  .** Close a curs
1dfb6 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
1dfb7 65 6e 65 64 20 61 73 20 50 31 2e 20 20 49 66 20  ened as P1.  If 
1dfb8 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72  P1 is not.** cur
1dfb9 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69  rently open, thi
1dfba 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  s instruction is
1dfbb 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73   a no-op..*/.cas
1dfbc 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20  e OP_Close: {.  
1dfbd 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1dfbe 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1dfbf 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71  >nCursor );.  sq
1dfc0 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72  lite3VdbeFreeCur
1dfc1 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b  sor(p, p->apCsr[
1dfc2 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e  pOp->p1]);.  p->
1dfc3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d  apCsr[pOp->p1] =
1dfc4 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
1dfc5 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47  /* Opcode: SeekG
1dfc6 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
1dfc7 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20  **.** If cursor 
1dfc8 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20  P1 refers to an 
1dfc9 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65  SQL table (B-Tre
1dfca 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65  e that uses inte
1dfcb 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75  ger keys), .** u
1dfcc 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  se the value in 
1dfcd 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74  register P3 as t
1dfce 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73  he key.  If curs
1dfcf 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
1dfd0 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
1dfd1 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
1dfd2 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
1dfd3 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
1dfd4 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
1dfd5 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
1dfd6 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
1dfd7 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
1dfd8 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1dfd9 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  at  it points to
1dfda 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e   the smallest en
1dfdb 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1dfdc 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1dfdd 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1dfde 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1dfdf 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1dfe0 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
1dfe1 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1dfe2 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e   key and P2 is n
1dfe3 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75  ot zero, then ju
1dfe4 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1dfe5 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
1dfe6 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69   NotFound, Disti
1dfe7 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65  nct, SeekLt, See
1dfe8 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f  kGt, SeekLe.*/./
1dfe9 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74  * Opcode: SeekGt
1dfea 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1dfeb 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1dfec 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1dfed 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1dfee 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1dfef 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1dff0 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1dff1 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
1dff2 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50  key. If cursor P
1dff3 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20  1 refers .** to 
1dff4 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
1dff5 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
1dff6 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
1dff7 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a  f P4 registers .
1dff8 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64  ** that are used
1dff9 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20   as an unpacked 
1dffa 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a  index key. .**.*
1dffb 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72  * Reposition cur
1dffc 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20  sor P1 so that  
1dffd 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
1dffe 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20   smallest entry 
1dfff 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61  that .** is grea
1e000 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79  ter than the key
1e001 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1e002 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1e003 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a  greater than .**
1e004 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1e005 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1e006 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1e007 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1e008 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1e009 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c  istinct, SeekLt,
1e00a 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
1e00b 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
1e00c 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34  ekLt P1 P2 P3 P4
1e00d 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72   * .**.** If cur
1e00e 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f  sor P1 refers to
1e00f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42   an SQL table (B
1e010 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20  -Tree that uses 
1e011 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a  integer keys), .
1e012 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65  ** use the value
1e013 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1e014 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
1e015 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
1e016 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
1e017 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
1e018 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
1e019 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
1e01a 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
1e01b 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
1e01c 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1e01d 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
1e01e 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
1e01f 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
1e020 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  o the largest en
1e021 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20  try that .** is 
1e022 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65  less than the ke
1e023 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72  y value. If ther
1e024 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73  e are no records
1e025 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74   less than .** t
1e026 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1e027 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1e028 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1e029 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1e02a 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1e02b 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53  tinct, SeekGt, S
1e02c 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGe, SeekLe.*/
1e02d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1e02e 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Le P1 P2 P3 P4 *
1e02f 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1e030 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1e031 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1e032 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1e033 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1e034 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1e035 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1e036 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1e037 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1e038 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1e039 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1e03a 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1e03b 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1e03c 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1e03d 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1e03e 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1e03f 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1e040 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1e041 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1e042 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20  e largest entry 
1e043 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73  that .** is less
1e044 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1e045 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e  o the key value.
1e046 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1e047 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73   records .** les
1e048 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
1e049 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  to the key and P
1e04a 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
1e04b 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
1e04c 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e04d 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1e04e 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47   Distinct, SeekG
1e04f 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
1e050 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  t.*/.case OP_See
1e051 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kLt:         /* 
1e052 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1e053 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20  e OP_SeekLe:    
1e054 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e055 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  3 */.case OP_See
1e056 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20  kGe:         /* 
1e057 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1e058 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20  e OP_SeekGt: {  
1e059 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e05a 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1e05b 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e05c 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a  oved into u.az *
1e05d 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  /.  int res;.  i
1e05e 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72  nt oc;.  VdbeCur
1e05f 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63  sor *pC;.  Unpac
1e060 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69  kedRecord r;.  i
1e061 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34  nt nField;.  i64
1e062 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   iKey;      /* T
1e063 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20  he rowid we are 
1e064 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65  to seek to */.#e
1e065 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e066 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e067 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73  to u.az */..  as
1e068 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e069 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e06a 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1e06b 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29  rt( pOp->p2!=0 )
1e06c 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d  ;.  u.az.pC = p-
1e06d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e06e 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e  .  assert( u.az.
1e06f 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1e070 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75  t( u.az.pC->pseu
1e071 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b  doTableReg==0 );
1e072 0a 20 20 61 73 73 65 72 74 28 20 4f 50 5f 53 65  .  assert( OP_Se
1e073 65 6b 4c 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c  ekLe == OP_SeekL
1e074 74 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  t+1 );.  assert(
1e075 20 4f 50 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50   OP_SeekGe == OP
1e076 5f 53 65 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61  _SeekLt+2 );.  a
1e077 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 47 74  ssert( OP_SeekGt
1e078 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20   == OP_SeekLt+3 
1e079 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 43  );.  if( u.az.pC
1e07a 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
1e07b 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f      u.az.oc = pO
1e07c 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75  p->opcode;.    u
1e07d 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .az.pC->nullRow 
1e07e 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 61  = 0;.    if( u.a
1e07f 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b  z.pC->isTable ){
1e080 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e  .      /* The in
1e081 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20  put value in P3 
1e082 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20  might be of any 
1e083 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72  type: integer, r
1e084 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20  eal, string,.   
1e085 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e     ** blob, or N
1e086 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65  ULL.  But it nee
1e087 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  ds to be an inte
1e088 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61  ger before we ca
1e089 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  n do.      ** th
1e08a 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72  e seek, so cover
1e08b 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70  t it. */.      p
1e08c 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In3 = &aMem[pOp-
1e08d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c  >p3];.      appl
1e08e 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  yNumericAffinity
1e08f 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e  (pIn3);.      u.
1e090 61 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65  az.iKey = sqlite
1e091 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1e092 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e  n3);.      u.az.
1e093 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e094 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 0;..      /* 
1e095 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1e096 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e  could not be con
1e097 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69  verted into an i
1e098 6e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20  nteger without. 
1e099 20 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20       ** loss of 
1e09a 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65  information, the
1e09b 6e 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73  n special proces
1e09c 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64  sing is required
1e09d 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ... */.      if(
1e09e 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20   (pIn3->flags & 
1e09f 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20  MEM_Int)==0 ){. 
1e0a0 20 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33         if( (pIn3
1e0a1 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
1e0a2 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  al)==0 ){.      
1e0a3 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33      /* If the P3
1e0a4 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65   value cannot be
1e0a5 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
1e0a6 61 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75  any kind of a nu
1e0a7 6d 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  mber,.          
1e0a8 2a 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b  ** then the seek
1e0a9 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1e0aa 2c 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20  , so jump to P2 
1e0ab 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20  */.          pc 
1e0ac 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1e0ad 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1e0ae 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e0af 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68    /* If we reach
1e0b0 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
1e0b1 6e 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d  n the P3 value m
1e0b2 75 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e  ust be a floatin
1e0b3 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69  g.        ** poi
1e0b4 6e 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20  nt number. */.  
1e0b5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
1e0b6 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In3->flags & MEM
1e0b7 5f 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20  _Real)!=0 );..  
1e0b8 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69        if( u.az.i
1e0b9 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e  Key==SMALLEST_IN
1e0ba 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c  T64 && (pIn3->r<
1e0bb 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65  (double)u.az.iKe
1e0bc 79 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20  y || pIn3->r>0) 
1e0bd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1e0be 54 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20  The P3 value is 
1e0bf 74 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67  too large in mag
1e0c0 6e 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70  nitude to be exp
1e0c1 72 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20  ressed as an.   
1e0c2 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65         ** intege
1e0c3 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  r. */.          
1e0c4 75 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20  u.az.res = 1;.  
1e0c5 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33          if( pIn3
1e0c6 2d 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ->r<0 ){.       
1e0c7 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63       if( u.az.oc
1e0c8 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20  >=OP_SeekGe ){  
1e0c9 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d  assert( u.az.oc=
1e0ca 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e  =OP_SeekGe || u.
1e0cb 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  az.oc==OP_SeekGt
1e0cc 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
1e0cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e0ce 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43  reeFirst(u.az.pC
1e0cf 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a  ->pCursor, &u.az
1e0d0 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  .res);.         
1e0d1 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1e0d2 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1e0d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1e0d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1e0d5 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1e0d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1e0d7 20 75 2e 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65   u.az.oc<=OP_See
1e0d8 6b 4c 65 20 29 7b 20 20 61 73 73 65 72 74 28 20  kLe ){  assert( 
1e0d9 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1e0da 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  Lt || u.az.oc==O
1e0db 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20  P_SeekLe );.    
1e0dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
1e0dd 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
1e0de 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.az.pC->pCursor
1e0df 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20  , &u.az.res);.  
1e0e0 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1e0e1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1e0e2 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1e0e3 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1e0e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1e0e5 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
1e0e6 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20   u.az.res ){.   
1e0e7 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f           pc = pO
1e0e8 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1e0e9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1e0ea 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1e0eb 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f  }else if( u.az.o
1e0ec 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20  c==OP_SeekLt || 
1e0ed 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1e0ee 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Ge ){.          
1e0ef 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c 69  /* Use the ceili
1e0f0 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ng() function to
1e0f1 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
1e0f2 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
1e0f3 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64  if( pIn3->r > (d
1e0f4 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20  ouble)u.az.iKey 
1e0f5 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20  ) u.az.iKey++;. 
1e0f6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1e0f7 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74          /* Use t
1e0f8 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74  he floor() funct
1e0f9 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72  ion to convert r
1e0fa 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20  eal->int */.    
1e0fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e0fc 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65  az.oc==OP_SeekLe
1e0fd 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f   || u.az.oc==OP_
1e0fe 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20  SeekGt );.      
1e0ff 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1e100 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69  < (double)u.az.i
1e101 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d  Key ) u.az.iKey-
1e102 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  -;.        }.   
1e103 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1e104 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1e105 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e  toUnpacked(u.az.
1e106 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1e107 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20  (u64)u.az.iKey, 
1e108 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20  0, &u.az.res);. 
1e109 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1e10a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1e10b 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1e10c 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1e10d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61   }.      if( u.a
1e10e 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  z.res==0 ){.    
1e10f 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77      u.az.pC->row
1e110 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20  idIsValid = 1;. 
1e111 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e         u.az.pC->
1e112 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a  lastRowid = u.az
1e113 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20  .iKey;.      }. 
1e114 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e115 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f  u.az.nField = pO
1e116 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61  p->p4.i;.      a
1e117 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1e118 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a  pe==P4_INT32 );.
1e119 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e11a 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20  az.nField>0 );. 
1e11b 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79       u.az.r.pKey
1e11c 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e  Info = u.az.pC->
1e11d 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pKeyInfo;.      
1e11e 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20  u.az.r.nField = 
1e11f 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64  (u16)u.az.nField
1e120 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;..      /* The 
1e121 6e 65 78 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64  next line of cod
1e122 65 20 63 6f 6d 70 75 74 65 73 20 61 73 20 66 6f  e computes as fo
1e123 6c 6c 6f 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74  llows, only fast
1e124 65 72 3a 0a 20 20 20 20 20 20 2a 2a 20 20 20 69  er:.      **   i
1e125 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  f( u.az.oc==OP_S
1e126 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63  eekGt || u.az.oc
1e127 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20  ==OP_SeekLe ){. 
1e128 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a       **     u.az
1e129 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  .r.flags = UNPAC
1e12a 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20  KED_INCRKEY;.   
1e12b 20 20 20 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20     **   }else{. 
1e12c 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a       **     u.az
1e12d 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  .r.flags = 0;.  
1e12e 20 20 20 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20      **   }.     
1e12f 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72   */.      u.az.r
1e130 2e 66 6c 61 67 73 20 3d 20 28 75 31 36 29 28 55  .flags = (u16)(U
1e131 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
1e132 2a 20 28 31 20 26 20 28 75 2e 61 7a 2e 6f 63 20  * (1 & (u.az.oc 
1e133 2d 20 4f 50 5f 53 65 65 6b 4c 74 29 29 29 3b 0a  - OP_SeekLt)));.
1e134 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e135 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 47 74  az.oc!=OP_SeekGt
1e136 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73   || u.az.r.flags
1e137 3d 3d 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  ==UNPACKED_INCRK
1e138 45 59 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  EY );.      asse
1e139 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f  rt( u.az.oc!=OP_
1e13a 53 65 65 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 72  SeekLe || u.az.r
1e13b 2e 66 6c 61 67 73 3d 3d 55 4e 50 41 43 4b 45 44  .flags==UNPACKED
1e13c 5f 49 4e 43 52 4b 45 59 20 29 3b 0a 20 20 20 20  _INCRKEY );.    
1e13d 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f    assert( u.az.o
1e13e 63 21 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20  c!=OP_SeekGe || 
1e13f 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20  u.az.r.flags==0 
1e140 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1e141 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65   u.az.oc!=OP_See
1e142 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c  kLt || u.az.r.fl
1e143 61 67 73 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20  ags==0 );..     
1e144 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26   u.az.r.aMem = &
1e145 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1e146 20 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28       ExpandBlob(
1e147 75 2e 61 7a 2e 72 2e 61 4d 65 6d 29 3b 0a 20 20  u.az.r.aMem);.  
1e148 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e149 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1e14a 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75  ked(u.az.pC->pCu
1e14b 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30  rsor, &u.az.r, 0
1e14c 2c 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b  , 0, &u.az.res);
1e14d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1e14e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1e14f 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1e150 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1e151 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e     }.      u.az.
1e152 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e153 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1e154 75 2e 61 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.az.pC->deferre
1e155 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20  dMoveto = 0;.   
1e156 20 75 2e 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53   u.az.pC->cacheS
1e157 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1e158 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  ALE;.#ifdef SQLI
1e159 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
1e15a 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
1e15b 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69  ++;.#endif.    i
1e15c 66 28 20 75 2e 61 7a 2e 6f 63 3e 3d 4f 50 5f 53  f( u.az.oc>=OP_S
1e15d 65 65 6b 47 65 20 29 7b 20 20 61 73 73 65 72 74  eekGe ){  assert
1e15e 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.az.oc==OP_Se
1e15f 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d  ekGe || u.az.oc=
1e160 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20  =OP_SeekGt );.  
1e161 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73      if( u.az.res
1e162 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d  <0 || (u.az.res=
1e163 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  =0 && u.az.oc==O
1e164 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20 20 20  P_SeekGt) ){.   
1e165 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e166 33 42 74 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e  3BtreeNext(u.az.
1e167 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1e168 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20  az.res);.       
1e169 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e16a 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
1e16b 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1e16c 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72        u.az.pC->r
1e16d 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1e16e 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1e16f 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d        u.az.res =
1e170 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1e171 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1e172 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50  ert( u.az.oc==OP
1e173 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e  _SeekLt || u.az.
1e174 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b  oc==OP_SeekLe );
1e175 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e  .      if( u.az.
1e176 72 65 73 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72  res>0 || (u.az.r
1e177 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63  es==0 && u.az.oc
1e178 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a  ==OP_SeekLt) ){.
1e179 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1e17a 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75  ite3BtreePreviou
1e17b 73 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73  s(u.az.pC->pCurs
1e17c 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a  or, &u.az.res);.
1e17d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1e17e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
1e17f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1e180 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  ror;.        u.a
1e181 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  z.pC->rowidIsVal
1e182 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  id = 0;.      }e
1e183 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
1e184 75 2e 61 7a 2e 72 65 73 20 6d 69 67 68 74 20 62  u.az.res might b
1e185 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75  e negative becau
1e186 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
1e187 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f  empty.  Check to
1e188 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20  .        ** see 
1e189 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  if this is the c
1e18a 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ase..        */.
1e18b 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73          u.az.res
1e18c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45   = sqlite3BtreeE
1e18d 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72  of(u.az.pC->pCur
1e18e 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  sor);.      }.  
1e18f 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1e190 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20  pOp->p2>0 );.   
1e191 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b   if( u.az.res ){
1e192 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1e193 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
1e194 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1e195 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
1e196 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f   attempting to o
1e197 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  pen the sqlite3_
1e198 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20  master table.   
1e199 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 63   ** for read acc
1e19a 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ess returns SQLI
1e19b 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 69  TE_EMPTY. In thi
1e19c 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20 20  s case always.  
1e19d 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75    ** take the ju
1e19e 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 20  mp (since there 
1e19f 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 69  are no records i
1e1a0 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20 20  n the table)..  
1e1a1 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f    */.    pc = pO
1e1a2 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1e1a3 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1e1a4 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50 32  code: Seek P1 P2
1e1a5 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20   * * *.**.** P1 
1e1a6 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65  is an open table
1e1a7 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69   cursor and P2 i
1e1a8 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67 65  s a rowid intege
1e1a9 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66  r.  Arrange.** f
1e1aa 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f  or P1 to move so
1e1ab 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1e1ac 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69 76  to the rowid giv
1e1ad 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  en by P2..**.** 
1e1ae 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c 79  This is actually
1e1af 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65 6b   a deferred seek
1e1b0 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75 61  .  Nothing actua
1e1b1 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74 69  lly happens unti
1e1b2 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20  l.** the cursor 
1e1b3 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20  is used to read 
1e1b4 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74 20  a record.  That 
1e1b5 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64 73  way, if no reads
1e1b6 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e  .** occur, no un
1e1b7 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68 61  necessary I/O ha
1e1b8 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ppens..*/.case O
1e1b9 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20  P_Seek: {    /* 
1e1ba 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in2 */.#if 0  /*
1e1bb 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e1bc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61   moved into u.ba
1e1bd 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e1be 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   *pC;.#endif /* 
1e1bf 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e1c0 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20  moved into u.ba 
1e1c1 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
1e1c2 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e1c3 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e1c4 3b 0a 20 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d  ;.  u.ba.pC = p-
1e1c5 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e1c6 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e  .  assert( u.ba.
1e1c7 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41  pC!=0 );.  if( A
1e1c8 4c 57 41 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70  LWAYS(u.ba.pC->p
1e1c9 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20  Cursor!=0) ){.  
1e1ca 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70    assert( u.ba.p
1e1cb 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1e1cc 20 20 75 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52    u.ba.pC->nullR
1e1cd 6f 77 20 3d 20 30 3b 0a 20 20 20 20 70 49 6e 32  ow = 0;.    pIn2
1e1ce 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
1e1cf 5d 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e  ];.    u.ba.pC->
1e1d0 6d 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73  movetoTarget = s
1e1d1 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1e1d2 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e  ue(pIn2);.    u.
1e1d3 62 61 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  ba.pC->rowidIsVa
1e1d4 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  lid = 0;.    u.b
1e1d5 61 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  a.pC->deferredMo
1e1d6 76 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  veto = 1;.  }.  
1e1d7 62 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20  break;.}.  ../* 
1e1d8 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31  Opcode: Found P1
1e1d9 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1e1da 2a 20 49 66 20 50 34 3d 3d 30 20 74 68 65 6e 20  * If P4==0 then 
1e1db 72 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64  register P3 hold
1e1dc 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
1e1dd 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f  cted by MakeReco
1e1de 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20  rd.  If.** P4>0 
1e1df 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 50 33  then register P3
1e1e0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 66   is the first of
1e1e1 20 50 34 20 72 65 67 69 73 74 65 72 73 20 74 68   P4 registers th
1e1e2 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63  at form an unpac
1e1e3 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a  ked.** record..*
1e1e4 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 69  *.** Cursor P1 i
1e1e5 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 74  s on an index bt
1e1e6 72 65 65 2e 20 20 49 66 20 74 68 65 20 72 65 63  ree.  If the rec
1e1e7 6f 72 64 20 69 64 65 6e 74 69 66 69 65 64 20 62  ord identified b
1e1e8 79 20 50 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69  y P3 and P4.** i
1e1e9 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 61 6e  s a prefix of an
1e1ea 79 20 65 6e 74 72 79 20 69 6e 20 50 31 20 74 68  y entry in P1 th
1e1eb 65 6e 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64  en a jump is mad
1e1ec 65 20 74 6f 20 50 32 20 61 6e 64 0a 2a 2a 20 50  e to P2 and.** P
1e1ed 31 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  1 is left pointi
1e1ee 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69  ng at the matchi
1e1ef 6e 67 20 65 6e 74 72 79 2e 0a 2a 2f 0a 2f 2a 20  ng entry..*/./* 
1e1f0 4f 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64  Opcode: NotFound
1e1f1 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1e1f2 2a 0a 2a 2a 20 49 66 20 50 34 3d 3d 30 20 74 68  *.** If P4==0 th
1e1f3 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 68  en register P3 h
1e1f4 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  olds a blob cons
1e1f5 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52  tructed by MakeR
1e1f6 65 63 6f 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34  ecord.  If.** P4
1e1f7 3e 30 20 74 68 65 6e 20 72 65 67 69 73 74 65 72  >0 then register
1e1f8 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74   P3 is the first
1e1f9 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1e1fa 20 74 68 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e   that form an un
1e1fb 70 61 63 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64  packed.** record
1e1fc 2e 0a 2a 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20  ..** .** Cursor 
1e1fd 50 31 20 69 73 20 6f 6e 20 61 6e 20 69 6e 64 65  P1 is on an inde
1e1fe 78 20 62 74 72 65 65 2e 20 20 49 66 20 74 68 65  x btree.  If the
1e1ff 20 72 65 63 6f 72 64 20 69 64 65 6e 74 69 66 69   record identifi
1e200 65 64 20 62 79 20 50 33 20 61 6e 64 20 50 34 0a  ed by P3 and P4.
1e201 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 70 72  ** is not the pr
1e202 65 66 69 78 20 6f 66 20 61 6e 79 20 65 6e 74 72  efix of any entr
1e203 79 20 69 6e 20 50 31 20 74 68 65 6e 20 61 20 6a  y in P1 then a j
1e204 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50  ump is made to P
1e205 32 2e 20 20 49 66 20 50 31 20 0a 2a 2a 20 64 6f  2.  If P1 .** do
1e206 65 73 20 63 6f 6e 74 61 69 6e 20 61 6e 20 65 6e  es contain an en
1e207 74 72 79 20 77 68 6f 73 65 20 70 72 65 66 69 78  try whose prefix
1e208 20 6d 61 74 63 68 65 73 20 74 68 65 20 50 33 2f   matches the P3/
1e209 50 34 20 72 65 63 6f 72 64 20 74 68 65 6e 20 63  P4 record then c
1e20a 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20  ontrol.** falls 
1e20b 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1e20c 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1e20d 61 6e 64 20 50 31 20 69 73 20 6c 65 66 74 20 70  and P1 is left p
1e20e 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 0a 2a  ointing at the.*
1e20f 2a 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79  * matching entry
1e210 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1e211 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73  : Found, NotExis
1e212 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a  ts, IsUnique.*/.
1e213 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64  case OP_NotFound
1e214 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c  :       /* jump,
1e215 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1e216 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 20 20  Found: {        
1e217 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1e218 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1e219 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e21a 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69  into u.bb */.  i
1e21b 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 74 73  nt alreadyExists
1e21c 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1e21d 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  pC;.  int res;. 
1e21e 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1e21f 2a 70 49 64 78 4b 65 79 3b 0a 20 20 55 6e 70 61  *pIdxKey;.  Unpa
1e220 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20  ckedRecord r;.  
1e221 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f  char aTempRec[RO
1e222 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61  UND8(sizeof(Unpa
1e223 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73  ckedRecord)) + s
1e224 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37  izeof(Mem)*3 + 7
1e225 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  ];.#endif /* loc
1e226 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e227 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a  ed into u.bb */.
1e228 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1e229 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 66 6f  EST.  sqlite3_fo
1e22a 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  und_count++;.#en
1e22b 64 69 66 0a 0a 20 20 75 2e 62 62 2e 61 6c 72 65  dif..  u.bb.alre
1e22c 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20  adyExists = 0;. 
1e22d 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e22e 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1e22f 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1e230 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1e231 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a  pe==P4_INT32 );.
1e232 20 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61    u.bb.pC = p->a
1e233 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e234 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43   assert( u.bb.pC
1e235 21 3d 30 20 29 3b 0a 20 20 70 49 6e 33 20 3d 20  !=0 );.  pIn3 = 
1e236 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1e237 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62    if( ALWAYS(u.b
1e238 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  b.pC->pCursor!=0
1e239 29 20 29 7b 0a 0a 20 20 20 20 61 73 73 65 72 74  ) ){..    assert
1e23a 28 20 75 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62  ( u.bb.pC->isTab
1e23b 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  le==0 );.    if(
1e23c 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 29 7b 0a   pOp->p4.i>0 ){.
1e23d 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 70 4b 65        u.bb.r.pKe
1e23e 79 49 6e 66 6f 20 3d 20 75 2e 62 62 2e 70 43 2d  yInfo = u.bb.pC-
1e23f 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  >pKeyInfo;.     
1e240 20 75 2e 62 62 2e 72 2e 6e 46 69 65 6c 64 20 3d   u.bb.r.nField =
1e241 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b   (u16)pOp->p4.i;
1e242 0a 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 61 4d  .      u.bb.r.aM
1e243 65 6d 20 3d 20 70 49 6e 33 3b 0a 20 20 20 20 20  em = pIn3;.     
1e244 20 75 2e 62 62 2e 72 2e 66 6c 61 67 73 20 3d 20   u.bb.r.flags = 
1e245 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f  UNPACKED_PREFIX_
1e246 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 75 2e 62  MATCH;.      u.b
1e247 62 2e 70 49 64 78 4b 65 79 20 3d 20 26 75 2e 62  b.pIdxKey = &u.b
1e248 62 2e 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  b.r;.    }else{.
1e249 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
1e24a 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1e24b 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 45 78  Blob );.      Ex
1e24c 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a  pandBlob(pIn3);.
1e24d 20 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b        u.bb.pIdxK
1e24e 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
1e24f 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62  RecordUnpack(u.b
1e250 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20  b.pC->pKeyInfo, 
1e251 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a  pIn3->n, pIn3->z
1e252 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1e253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e254 20 20 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61            u.bb.a
1e255 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28  TempRec, sizeof(
1e256 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b  u.bb.aTempRec));
1e257 0a 20 20 20 20 20 20 69 66 28 20 75 2e 62 62 2e  .      if( u.bb.
1e258 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  pIdxKey==0 ){.  
1e259 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65        goto no_me
1e25a 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
1e25b 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d 3e 66   u.bb.pIdxKey->f
1e25c 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44  lags |= UNPACKED
1e25d 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20  _PREFIX_MATCH;. 
1e25e 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
1e25f 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
1e260 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e 70 43  Unpacked(u.bb.pC
1e261 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 62 2e  ->pCursor, u.bb.
1e262 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26  pIdxKey, 0, 0, &
1e263 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20 20 69  u.bb.res);.    i
1e264 66 28 20 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30 20  f( pOp->p4.i==0 
1e265 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1e266 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
1e267 65 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49  edRecord(u.bb.pI
1e268 64 78 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20  dxKey);.    }.  
1e269 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1e26a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 72 65  _OK ){.      bre
1e26b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e  ak;.    }.    u.
1e26c 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74 73  bb.alreadyExists
1e26d 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d 30 29   = (u.bb.res==0)
1e26e 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 64  ;.    u.bb.pC->d
1e26f 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e270 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e  0;.    u.bb.pC->
1e271 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e272 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
1e273 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1e274 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
1e275 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72 65 61    if( u.bb.alrea
1e276 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20  dyExists ) pc = 
1e277 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1e278 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 75  else{.    if( !u
1e279 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74  .bb.alreadyExist
1e27a 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  s ) pc = pOp->p2
1e27b 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1e27c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e27d 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 32 20   IsUnique P1 P2 
1e27e 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75  P3 P4 *.**.** Cu
1e27f 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65 6e 20  rsor P1 is open 
1e280 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72  on an index b-tr
1e281 65 65 20 2d 20 74 68 61 74 20 69 73 20 74 6f 20  ee - that is to 
1e282 73 61 79 2c 20 61 20 62 74 72 65 65 20 77 68 69  say, a btree whi
1e283 63 68 0a 2a 2a 20 6e 6f 20 64 61 74 61 20 61 6e  ch.** no data an
1e284 64 20 77 68 65 72 65 20 74 68 65 20 6b 65 79 20  d where the key 
1e285 61 72 65 20 72 65 63 6f 72 64 73 20 67 65 6e 65  are records gene
1e286 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65  rated by OP_Make
1e287 52 65 63 6f 72 64 20 77 69 74 68 0a 2a 2a 20 74  Record with.** t
1e288 68 65 20 6c 69 73 74 20 66 69 65 6c 64 20 62 65  he list field be
1e289 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 72 20  ing the integer 
1e28a 52 4f 57 49 44 20 6f 66 20 74 68 65 20 65 6e 74  ROWID of the ent
1e28b 72 79 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  ry that the inde
1e28c 78 0a 2a 2a 20 65 6e 74 72 79 20 72 65 66 65 72  x.** entry refer
1e28d 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s to..**.** The 
1e28e 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74  P3 register cont
1e28f 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20  ains an integer 
1e290 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43  record number. C
1e291 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64 20  all this record 
1e292 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52 65  .** number R. Re
1e293 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68 65  gister P4 is the
1e294 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74 20   first in a set 
1e295 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20  of N contiguous 
1e296 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68 61  registers.** tha
1e297 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e 70  t make up an unp
1e298 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20  acked index key 
1e299 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
1e29a 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31 2e   with cursor P1.
1e29b 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66  .** The value of
1e29c 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72 72   N can be inferr
1e29d 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 73  ed from the curs
1e29e 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20 74  or. N includes t
1e29f 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75  he rowid.** valu
1e2a0 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  e appended to th
1e2a1 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
1e2a2 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73 20  ex record. This 
1e2a3 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79 0a  rowid value may.
1e2a4 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65  ** or may not be
1e2a5 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e 0a   the same as R..
1e2a6 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  **.** If any of 
1e2a7 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20  the N registers 
1e2a8 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 72  beginning with r
1e2a9 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74 61  egister P4 conta
1e2aa 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61  ins a NULL.** va
1e2ab 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69  lue, jump immedi
1e2ac 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a  ately to P2..**.
1e2ad 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  ** Otherwise, th
1e2ae 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63  is instruction c
1e2af 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72 20  hecks if cursor 
1e2b0 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65  P1 contains an e
1e2b1 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74 68  ntry.** where th
1e2b2 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66 69  e first (N-1) fi
1e2b3 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20 74  elds match but t
1e2b4 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 61  he rowid value a
1e2b5 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  t the end.** of 
1e2b6 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
1e2b7 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68 65  is not R. If the
1e2b8 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65 6e  re is no such en
1e2b9 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d  try, control jum
1e2ba 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63  ps.** to instruc
1e2bb 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69  tion P2. Otherwi
1e2bc 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f 66  se, the rowid of
1e2bd 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67   the conflicting
1e2be 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20   index.** entry 
1e2bf 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65 67  is copied to reg
1e2c0 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f 6e  ister P3 and con
1e2c1 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75  trol falls throu
1e2c2 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a  gh to the next.*
1e2c3 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  * instruction..*
1e2c4 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e  *.** See also: N
1e2c5 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73  otFound, NotExis
1e2c6 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73  ts, Found.*/.cas
1e2c7 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b  e OP_IsUnique: {
1e2c8 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1e2c9 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in3 */.#if 0  /
1e2ca 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e2cb 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e2cc 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20  c */.  u16 ii;. 
1e2cd 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1e2ce 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1e2cf 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c  rsr;.  u16 nFiel
1e2d0 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20  d;.  Mem *aMx;. 
1e2d1 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1e2d2 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1e2d3 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e      /* B-Tree in
1e2d4 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a  dex search key *
1e2d5 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 20 20  /.  i64 R;      
1e2d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e2d7 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
1e2d8 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1e2d9 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 66 20  er P3 */.#endif 
1e2da 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e2db 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e2dc 62 63 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20  bc */..  pIn3 = 
1e2dd 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1e2de 20 20 75 2e 62 63 2e 61 4d 78 20 3d 20 26 61 4d    u.bc.aMx = &aM
1e2df 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20  em[pOp->p4.i];. 
1e2e0 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
1e2e1 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 70 61  the values of pa
1e2e2 72 61 6d 65 74 65 72 73 20 50 31 20 61 6e 64 20  rameters P1 and 
1e2e3 50 34 20 61 72 65 20 69 6e 20 72 61 6e 67 65 2e  P4 are in range.
1e2e4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1e2e5 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e  p->p4type==P4_IN
1e2e6 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28  T32 );.  assert(
1e2e7 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20   pOp->p4.i>0 && 
1e2e8 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d  pOp->p4.i<=p->nM
1e2e9 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  em );.  assert( 
1e2ea 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e2eb 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1e2ec 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74   );..  /* Find t
1e2ed 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 2e  he index cursor.
1e2ee 20 2a 2f 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d   */.  u.bc.pCx =
1e2ef 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e2f0 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1e2f1 62 63 2e 70 43 78 2d 3e 64 65 66 65 72 72 65 64  bc.pCx->deferred
1e2f2 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75  Moveto==0 );.  u
1e2f3 2e 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73  .bc.pCx->seekRes
1e2f4 75 6c 74 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e  ult = 0;.  u.bc.
1e2f5 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75 73  pCx->cacheStatus
1e2f6 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1e2f7 20 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 75    u.bc.pCrsr = u
1e2f8 2e 62 63 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72  .bc.pCx->pCursor
1e2f9 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f  ;..  /* If any o
1e2fa 66 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65  f the values are
1e2fb 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20   NULL, take the 
1e2fc 6a 75 6d 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e  jump. */.  u.bc.
1e2fd 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43  nField = u.bc.pC
1e2fe 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69  x->pKeyInfo->nFi
1e2ff 65 6c 64 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e  eld;.  for(u.bc.
1e300 69 69 3d 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e  ii=0; u.bc.ii<u.
1e301 62 63 2e 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e  bc.nField; u.bc.
1e302 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 75  ii++){.    if( u
1e303 2e 62 63 2e 61 4d 78 5b 75 2e 62 63 2e 69 69 5d  .bc.aMx[u.bc.ii]
1e304 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  .flags & MEM_Nul
1e305 6c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  l ){.      pc = 
1e306 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1e307 20 20 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20     u.bc.pCrsr = 
1e308 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  0;.      break;.
1e309 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
1e30a 72 74 28 20 28 75 2e 62 63 2e 61 4d 78 5b 75 2e  rt( (u.bc.aMx[u.
1e30b 62 63 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73  bc.nField].flags
1e30c 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20   & MEM_Null)==0 
1e30d 29 3b 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70  );..  if( u.bc.p
1e30e 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f  Crsr!=0 ){.    /
1e30f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69  * Populate the i
1e310 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e  ndex search key.
1e311 20 2a 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70   */.    u.bc.r.p
1e312 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70  KeyInfo = u.bc.p
1e313 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  Cx->pKeyInfo;.  
1e314 20 20 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20    u.bc.r.nField 
1e315 3d 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20  = u.bc.nField + 
1e316 31 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c  1;.    u.bc.r.fl
1e317 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50  ags = UNPACKED_P
1e318 52 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20  REFIX_SEARCH;.  
1e319 20 20 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20    u.bc.r.aMem = 
1e31a 75 2e 62 63 2e 61 4d 78 3b 0a 0a 20 20 20 20 2f  u.bc.aMx;..    /
1e31b 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61  * Extract the va
1e31c 6c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72  lue of u.bc.R fr
1e31d 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  om register P3. 
1e31e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1e31f 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1e320 70 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e  pIn3);.    u.bc.
1e321 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a  R = pIn3->u.i;..
1e322 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68      /* Search th
1e323 65 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20  e B-Tree index. 
1e324 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e  If no conflictin
1e325 67 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e  g record is foun
1e326 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74  d, jump.    ** t
1e327 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  o P2. Otherwise,
1e328 20 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20   copy the rowid 
1e329 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69  of the conflicti
1e32a 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20  ng record to.   
1e32b 20 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20   ** register P3 
1e32c 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  and fall through
1e32d 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1e32e 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20  truction.  */.  
1e32f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e330 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1e331 64 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75  d(u.bc.pCrsr, &u
1e332 2e 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e  .bc.r, 0, 0, &u.
1e333 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75  bc.pCx->seekResu
1e334 6c 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e  lt);.    if( (u.
1e335 62 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50  bc.r.flags & UNP
1e336 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1e337 52 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72  RCH) || u.bc.r.r
1e338 6f 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a  owid==u.bc.R ){.
1e339 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1e33a 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  p2 - 1;.    }els
1e33b 65 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75  e{.      pIn3->u
1e33c 2e 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69  .i = u.bc.r.rowi
1e33d 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  d;.    }.  }.  b
1e33e 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1e33f 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31  de: NotExists P1
1e340 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1e341 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74   Use the content
1e342 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1e343 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79  as a integer key
1e344 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a  .  If a record .
1e345 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79  ** with that key
1e346 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
1e347 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20  in table of P1, 
1e348 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1e349 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f   .** If the reco
1e34a 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74  rd does exist, t
1e34b 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20  hen fall thru.  
1e34c 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
1e34d 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20  ft .** pointing 
1e34e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66  to the record if
1e34f 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a   it exists..**.*
1e350 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
1e351 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70   between this op
1e352 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46  eration and NotF
1e353 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69  ound is that thi
1e354 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61  s.** operation a
1e355 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69  ssumes the key i
1e356 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  s an integer and
1e357 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61   that P1 is a ta
1e358 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e  ble whereas.** N
1e359 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20  otFound assumes 
1e35a 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f  key is a blob co
1e35b 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d  nstructed from M
1e35c 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a  akeRecord and.**
1e35d 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e   P1 is an index.
1e35e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1e35f 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
1e360 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61  , IsUnique.*/.ca
1e361 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a  se OP_NotExists:
1e362 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1e363 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20  p, in3 */.#if 0 
1e364 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e365 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e366 2e 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bd */.  VdbeCur
1e367 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1e368 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1e369 74 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65  t res;.  u64 iKe
1e36a 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  y;.#endif /* loc
1e36b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e36c 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a  ed into u.bd */.
1e36d 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b  .  pIn3 = &aMem[
1e36e 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65  pOp->p3];.  asse
1e36f 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20  rt( pIn3->flags 
1e370 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61  & MEM_Int );.  a
1e371 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e372 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e373 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e374 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  d.pC = p->apCsr[
1e375 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e376 72 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29  rt( u.bd.pC!=0 )
1e377 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64  ;.  assert( u.bd
1e378 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  .pC->isTable );.
1e379 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70    assert( u.bd.p
1e37a 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65  C->pseudoTableRe
1e37b 67 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70  g==0 );.  u.bd.p
1e37c 43 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e  Crsr = u.bd.pC->
1e37d 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75  pCursor;.  if( u
1e37e 2e 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a  .bd.pCrsr!=0 ){.
1e37f 20 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30      u.bd.res = 0
1e380 3b 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20  ;.    u.bd.iKey 
1e381 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn3->u.i;.   
1e382 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e383 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1e384 28 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20  (u.bd.pCrsr, 0, 
1e385 75 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75  u.bd.iKey, 0, &u
1e386 2e 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e  .bd.res);.    u.
1e387 62 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64  bd.pC->lastRowid
1e388 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20   = pIn3->u.i;.  
1e389 20 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64    u.bd.pC->rowid
1e38a 49 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72  IsValid = u.bd.r
1e38b 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20  es==0 ?1:0;.    
1e38c 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.bd.pC->nullRow
1e38d 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70   = 0;.    u.bd.p
1e38e 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1e38f 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1e390 20 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72    u.bd.pC->defer
1e391 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1e392 20 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21     if( u.bd.res!
1e393 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  =0 ){.      pc =
1e394 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1e395 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64      assert( u.bd
1e396 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e397 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  d==0 );.    }.  
1e398 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52    u.bd.pC->seekR
1e399 65 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73  esult = u.bd.res
1e39a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
1e39b 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  * This happens w
1e39c 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74  hen an attempt t
1e39d 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75  o open a read cu
1e39e 72 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20  rsor on the.    
1e39f 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  ** sqlite_master
1e3a0 20 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53   table returns S
1e3a1 51 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20  QLITE_EMPTY..   
1e3a2 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   */.    pc = pOp
1e3a3 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73  ->p2 - 1;.    as
1e3a4 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72  sert( u.bd.pC->r
1e3a5 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29  owidIsValid==0 )
1e3a6 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73  ;.    u.bd.pC->s
1e3a7 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20  eekResult = 0;. 
1e3a8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e3a9 2a 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e  * Opcode: Sequen
1e3aa 63 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ce P1 P2 * * *.*
1e3ab 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65  *.** Find the ne
1e3ac 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71  xt available seq
1e3ad 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72  uence number for
1e3ae 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57   cursor P1..** W
1e3af 72 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63  rite the sequenc
1e3b0 65 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65  e number into re
1e3b1 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68  gister P2..** Th
1e3b2 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
1e3b3 72 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20  r on the cursor 
1e3b4 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61  is incremented a
1e3b5 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73  fter this.** ins
1e3b6 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63  truction.  .*/.c
1e3b7 61 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a  ase OP_Sequence:
1e3b8 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1e3b9 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1e3ba 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1e3bb 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e3bc 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e3bd 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70  .  assert( p->ap
1e3be 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20  Csr[pOp->p1]!=0 
1e3bf 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  );.  pOut->u.i =
1e3c0 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e3c1 31 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a  1]->seqCount++;.
1e3c2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20    break;.}.../* 
1e3c3 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64  Opcode: NewRowid
1e3c4 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1e3c5 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e  .** Get a new in
1e3c6 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d  teger record num
1e3c7 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69  ber (a.k.a "rowi
1e3c8 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20  d") used as the 
1e3c9 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a  key to a table..
1e3ca 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75  ** The record nu
1e3cb 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76  mber is not prev
1e3cc 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61  iously used as a
1e3cd 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61   key in the data
1e3ce 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68  base.** table th
1e3cf 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69  at cursor P1 poi
1e3d0 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77  nts to.  The new
1e3d1 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
1e3d2 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69  s written.** wri
1e3d3 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tten to register
1e3d4 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33   P2..**.** If P3
1e3d5 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20  >0 then P3 is a 
1e3d6 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20  register in the 
1e3d7 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68  root frame of th
1e3d8 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c  is VDBE that hol
1e3d9 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  ds .** the large
1e3da 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65  st previously ge
1e3db 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e  nerated record n
1e3dc 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65  umber. No new re
1e3dd 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65  cord numbers are
1e3de 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62  .** allowed to b
1e3df 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73  e less than this
1e3e0 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69   value. When thi
1e3e1 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20  s value reaches 
1e3e2 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a  its maximum, .**
1e3e3 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65   a SQLITE_FULL e
1e3e4 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65  rror is generate
1e3e5 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74  d. The P3 regist
1e3e6 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69  er is updated wi
1e3e7 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65  th the '.** gene
1e3e8 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d  rated record num
1e3e9 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63  ber. This P3 mec
1e3ea 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74  hanism is used t
1e3eb 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74  o help implement
1e3ec 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52   the.** AUTOINCR
1e3ed 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a  EMENT feature..*
1e3ee 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77  /.case OP_NewRow
1e3ef 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1e3f0 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e3f1 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1e3f2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e3f3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20  moved into u.be 
1e3f4 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20  */.  i64 v;     
1e3f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e3f6 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a  he new rowid */.
1e3f7 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e3f8 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ;        /* Curs
1e3f9 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67  or of table to g
1e3fa 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64  et the new rowid
1e3fb 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20   */.  int res;  
1e3fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e3fd 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c  Result of an sql
1e3fe 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20  ite3BtreeLast() 
1e3ff 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
1e400 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1e401 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20  ounter to limit 
1e402 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
1e403 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20  arches */.  Mem 
1e404 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
1e405 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
1e406 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72  olding largest r
1e407 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43  owid for AUTOINC
1e408 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65  REMENT */.  Vdbe
1e409 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
1e40a 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65     /* Root frame
1e40b 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64   of VDBE */.#end
1e40c 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e40d 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e40e 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65   u.be */..  u.be
1e40f 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72  .v = 0;.  u.be.r
1e410 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  es = 0;.  assert
1e411 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e412 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e413 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20  or );.  u.be.pC 
1e414 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e415 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e416 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69  .be.pC!=0 );.  i
1e417 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43  f( NEVER(u.be.pC
1e418 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b  ->pCursor==0) ){
1e419 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f  .    /* The zero
1e41a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
1e41b 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61  above is all tha
1e41c 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  t is needed */. 
1e41d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1e41e 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72  he next rowid or
1e41f 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
1e420 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20  different terms 
1e421 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
1e422 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62   ** thing) is ob
1e423 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d  tained in a two-
1e424 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a  step algorithm..
1e425 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69      **.    ** Fi
1e426 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74  rst we attempt t
1e427 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
1e428 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
1e429 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20  d and add one.  
1e42a 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
1e42b 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
1e42c 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1e42d 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
1e42e 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70  maximum.    ** p
1e42f 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c  ositive integer,
1e430 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c   we have to fall
1e431 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1e432 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72  second.    ** pr
1e433 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f  obabilistic algo
1e434 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20  rithm.    **.   
1e435 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
1e436 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73  lgorithm is to s
1e437 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74  elect a rowid at
1e438 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20   random and see 
1e439 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72  if.    ** it alr
1e43a 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74  eady exists in t
1e43b 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74  he table.  If it
1e43c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
1e43d 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
1e43e 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74  succeeded.  If t
1e43f 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  he random rowid 
1e440 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73  does exist, we s
1e441 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a  elect a new one.
1e442 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61      ** and try a
1e443 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20  gain, up to 100 
1e444 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  times..    */.  
1e445 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70    assert( u.be.p
1e446 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1e447 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a    u.be.cnt = 0;.
1e448 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
1e449 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
1e44a 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1e44b 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
1e44c 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
1e44d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
1e44e 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
1e44f 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
1e450 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
1e451 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
1e452 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
1e453 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
1e454 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
1e455 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
1e456 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
1e457 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
1e458 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
1e459 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
1e45a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
1e45b 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
1e45c 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
1e45d 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
1e45e 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a  64)0xffffffff ).
1e45f 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
1e460 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e  !u.be.pC->useRan
1e461 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1e462 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74    u.be.v = sqlit
1e463 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64  e3BtreeGetCached
1e464 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70  Rowid(u.be.pC->p
1e465 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69  Cursor);.      i
1e466 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a  f( u.be.v==0 ){.
1e467 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1e468 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e  ite3BtreeLast(u.
1e469 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  be.pC->pCursor, 
1e46a 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20  &u.be.res);.    
1e46b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1e46c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1e46d 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1e46e 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1e46f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1e470 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20  ( u.be.res ){.  
1e471 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d          u.be.v =
1e472 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
1e473 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
1e474 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1e475 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1e476 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29  .be.pC->pCursor)
1e477 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
1e478 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1e479 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e  eySize(u.be.pC->
1e47a 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76  pCursor, &u.be.v
1e47b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1e47c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1e47d 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f  OK );   /* Canno
1e47e 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67  t fail following
1e47f 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a   BtreeLast() */.
1e480 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1e481 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  be.v==MAX_ROWID 
1e482 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
1e483 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f  .be.pC->useRando
1e484 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20  mRowid = 1;.    
1e485 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1e486 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b           u.be.v+
1e487 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  +;.          }. 
1e488 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1e489 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e48a 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1e48b 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f  ENT.      if( pO
1e48c 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20  p->p3 ){.       
1e48d 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
1e48e 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65  P3 is a valid me
1e48f 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20  mory cell. */.  
1e490 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1e491 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20  p->p3>0 );.     
1e492 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65     if( p->pFrame
1e493 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
1e494 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d  r(u.be.pFrame=p-
1e495 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46  >pFrame; u.be.pF
1e496 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75  rame->pParent; u
1e497 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e  .be.pFrame=u.be.
1e498 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29  pFrame->pParent)
1e499 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;.          /* A
1e49a 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73  ssert that P3 is
1e49b 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20   a valid memory 
1e49c 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  cell. */.       
1e49d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e49e 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d  p3<=u.be.pFrame-
1e49f 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20  >nMem );.       
1e4a0 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26     u.be.pMem = &
1e4a1 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65  u.be.pFrame->aMe
1e4a2 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1e4a3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e4a4 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74       /* Assert t
1e4a5 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69  hat P3 is a vali
1e4a6 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a  d memory cell. *
1e4a7 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  /.          asse
1e4a8 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e  rt( pOp->p3<=p->
1e4a9 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
1e4aa 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 61    u.be.pMem = &a
1e4ab 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1e4ac 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
1e4ad 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1e4ae 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 65 2e 70 4d  pOp->p3, u.be.pM
1e4af 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
1e4b0 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1e4b1 65 72 69 66 79 28 75 2e 62 65 2e 70 4d 65 6d 29  erify(u.be.pMem)
1e4b2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1e4b3 28 20 28 75 2e 62 65 2e 70 4d 65 6d 2d 3e 66 6c  ( (u.be.pMem->fl
1e4b4 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d  ags & MEM_Int)!=
1e4b5 30 20 29 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29  0 );  /* mem(P3)
1e4b6 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65   holds an intege
1e4b7 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  r */.        if(
1e4b8 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 3d   u.be.pMem->u.i=
1e4b9 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 75 2e  =MAX_ROWID || u.
1e4ba 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  be.pC->useRandom
1e4bb 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
1e4bc 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46     rc = SQLITE_F
1e4bd 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67  ULL;.          g
1e4be 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e4bf 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1e4c0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  }.        if( u.
1e4c1 62 65 2e 76 3c 75 2e 62 65 2e 70 4d 65 6d 2d 3e  be.v<u.be.pMem->
1e4c2 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20 20 20  u.i+1 ){.       
1e4c3 20 20 20 75 2e 62 65 2e 76 20 3d 20 75 2e 62 65     u.be.v = u.be
1e4c4 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a  .pMem->u.i + 1;.
1e4c5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e4c6 20 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69    u.be.pMem->u.i
1e4c7 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 20 20 20   = u.be.v;.     
1e4c8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
1e4c9 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
1e4ca 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 65  CachedRowid(u.be
1e4cb 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e  .pC->pCursor, u.
1e4cc 62 65 2e 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f  be.v<MAX_ROWID ?
1e4cd 20 75 2e 62 65 2e 76 2b 31 20 3a 20 30 29 3b 0a   u.be.v+1 : 0);.
1e4ce 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e      }.    if( u.
1e4cf 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  be.pC->useRandom
1e4d0 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61  Rowid ){.      a
1e4d1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d  ssert( pOp->p3==
1e4d2 30 20 29 3b 20 20 2f 2a 20 57 65 20 63 61 6e 6e  0 );  /* We cann
1e4d3 6f 74 20 62 65 20 69 6e 20 72 61 6e 64 6f 6d 20  ot be in random 
1e4d4 72 6f 77 69 64 20 6d 6f 64 65 20 69 66 20 74 68  rowid mode if th
1e4d5 69 73 20 69 73 0a 20 20 20 20 20 20 20 20 20 20  is is.          
1e4d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4d7 20 20 20 2a 2a 20 61 6e 20 41 55 54 4f 49 4e 43     ** an AUTOINC
1e4d8 52 45 4d 45 4e 54 20 74 61 62 6c 65 2e 20 2a 2f  REMENT table. */
1e4d9 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20  .      u.be.v = 
1e4da 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  db->lastRowid;. 
1e4db 20 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20       u.be.cnt = 
1e4dc 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20  0;.      do{.   
1e4dd 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e       if( u.be.cn
1e4de 74 3d 3d 30 20 26 26 20 28 75 2e 62 65 2e 76 26  t==0 && (u.be.v&
1e4df 30 78 66 66 66 66 66 66 29 3d 3d 75 2e 62 65 2e  0xffffff)==u.be.
1e4e0 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75  v ){.          u
1e4e1 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20  .be.v++;.       
1e4e2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1e4e3 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
1e4e4 6e 65 73 73 28 73 69 7a 65 6f 66 28 75 2e 62 65  ness(sizeof(u.be
1e4e5 2e 76 29 2c 20 26 75 2e 62 65 2e 76 29 3b 0a 20  .v), &u.be.v);. 
1e4e6 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62           if( u.b
1e4e7 65 2e 63 6e 74 3c 35 20 29 20 75 2e 62 65 2e 76  e.cnt<5 ) u.be.v
1e4e8 20 26 3d 20 30 78 66 66 66 66 66 66 3b 0a 20 20   &= 0xffffff;.  
1e4e9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e4ea 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e4eb 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1e4ec 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.be.pC->pCursor
1e4ed 2c 20 30 2c 20 28 75 36 34 29 75 2e 62 65 2e 76  , 0, (u64)u.be.v
1e4ee 2c 20 30 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b  , 0, &u.be.res);
1e4ef 0a 20 20 20 20 20 20 20 20 75 2e 62 65 2e 63 6e  .        u.be.cn
1e4f0 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c  t++;.      }whil
1e4f1 65 28 20 75 2e 62 65 2e 63 6e 74 3c 31 30 30 20  e( u.be.cnt<100 
1e4f2 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
1e4f3 20 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20   && u.be.res==0 
1e4f4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1e4f5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e  =SQLITE_OK && u.
1e4f6 62 65 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  be.res==0 ){.   
1e4f7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1e4f8 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67  _FULL;.        g
1e4f9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e4fa 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a  _error;.      }.
1e4fb 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 65 2e 70      }.    u.be.p
1e4fc 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e4fd 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43  = 0;.    u.be.pC
1e4fe 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1e4ff 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70   = 0;.    u.be.p
1e500 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1e501 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1e502 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  }.  pOut->u.i = 
1e503 75 2e 62 65 2e 76 3b 0a 20 20 62 72 65 61 6b 3b  u.be.v;.  break;
1e504 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1e505 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 50  nsert P1 P2 P3 P
1e506 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  4 P5.**.** Write
1e507 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74   an entry into t
1e508 68 65 20 74 61 62 6c 65 20 6f 66 20 63 75 72 73  he table of curs
1e509 6f 72 20 50 31 2e 20 20 41 20 6e 65 77 20 65 6e  or P1.  A new en
1e50a 74 72 79 20 69 73 0a 2a 2a 20 63 72 65 61 74 65  try is.** create
1e50b 64 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20  d if it doesn't 
1e50c 61 6c 72 65 61 64 79 20 65 78 69 73 74 20 6f 72  already exist or
1e50d 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 61 6e   the data for an
1e50e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 65 6e 74   existing.** ent
1e50f 72 79 20 69 73 20 6f 76 65 72 77 72 69 74 74 65  ry is overwritte
1e510 6e 2e 20 20 54 68 65 20 64 61 74 61 20 69 73 20  n.  The data is 
1e511 74 68 65 20 76 61 6c 75 65 20 4d 45 4d 5f 42 6c  the value MEM_Bl
1e512 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  ob stored in reg
1e513 69 73 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20  ister.** number 
1e514 50 32 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73  P2. The key is s
1e515 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e516 72 20 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75  r P3. The key mu
1e517 73 74 0a 2a 2a 20 62 65 20 61 20 4d 45 4d 5f 49  st.** be a MEM_I
1e518 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  nt..**.** If the
1e519 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
1e51a 66 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65  flag of P5 is se
1e51b 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20  t, then the row 
1e51c 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a  change count is.
1e51d 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28  ** incremented (
1e51e 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20  otherwise not). 
1e51f 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c   If the OPFLAG_L
1e520 41 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66  ASTROWID flag of
1e521 20 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74   P5 is set,.** t
1e522 68 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f  hen rowid is sto
1e523 72 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65  red for subseque
1e524 6e 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65  nt return by the
1e525 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  .** sqlite3_last
1e526 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20  _insert_rowid() 
1e527 66 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77  function (otherw
1e528 69 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69  ise it is unmodi
1e529 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  fied)..**.** If 
1e52a 74 68 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  the OPFLAG_USESE
1e52b 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 6f 66  EKRESULT flag of
1e52c 20 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 69   P5 is set and i
1e52d 66 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a  f the result of.
1e52e 2a 2a 20 74 68 65 20 6c 61 73 74 20 73 65 65 6b  ** the last seek
1e52f 20 6f 70 65 72 61 74 69 6f 6e 20 28 4f 50 5f 4e   operation (OP_N
1e530 6f 74 45 78 69 73 74 73 29 20 77 61 73 20 61 20  otExists) was a 
1e531 73 75 63 63 65 73 73 2c 20 74 68 65 6e 20 74 68  success, then th
1e532 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20  is.** operation 
1e533 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74  will not attempt
1e534 20 74 6f 20 66 69 6e 64 20 74 68 65 20 61 70 70   to find the app
1e535 72 6f 70 72 69 61 74 65 20 72 6f 77 20 62 65 66  ropriate row bef
1e536 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 65  ore doing.** the
1e537 20 69 6e 73 65 72 74 20 62 75 74 20 77 69 6c 6c   insert but will
1e538 20 69 6e 73 74 65 61 64 20 6f 76 65 72 77 72 69   instead overwri
1e539 74 65 20 74 68 65 20 72 6f 77 20 74 68 61 74 20  te the row that 
1e53a 74 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a  the cursor is.**
1e53b 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1e53c 69 6e 67 20 74 6f 2e 20 20 50 72 65 73 75 6d 61  ing to.  Presuma
1e53d 62 6c 79 2c 20 74 68 65 20 70 72 69 6f 72 20 4f  bly, the prior O
1e53e 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 70 63 6f  P_NotExists opco
1e53f 64 65 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  de.** has alread
1e540 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 74 68 65  y positioned the
1e541 20 63 75 72 73 6f 72 20 63 6f 72 72 65 63 74 6c   cursor correctl
1e542 79 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f  y.  This is an o
1e543 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74  ptimization.** t
1e544 68 61 74 20 62 6f 6f 73 74 73 20 70 65 72 66 6f  hat boosts perfo
1e545 72 6d 61 6e 63 65 20 62 79 20 61 76 6f 69 64 69  rmance by avoidi
1e546 6e 67 20 72 65 64 75 6e 64 61 6e 74 20 73 65 65  ng redundant see
1e547 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ks..**.** If the
1e548 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45   OPFLAG_ISUPDATE
1e549 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
1e54a 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69  en this opcode i
1e54b 73 20 70 61 72 74 20 6f 66 20 61 6e 0a 2a 2a 20  s part of an.** 
1e54c 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
1e54d 2e 20 20 4f 74 68 65 72 77 69 73 65 20 28 69 66  .  Otherwise (if
1e54e 20 74 68 65 20 66 6c 61 67 20 69 73 20 63 6c 65   the flag is cle
1e54f 61 72 29 20 74 68 65 6e 20 74 68 69 73 20 6f 70  ar) then this op
1e550 63 6f 64 65 0a 2a 2a 20 69 73 20 70 61 72 74 20  code.** is part 
1e551 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65  of an INSERT ope
1e552 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 69 66  ration.  The dif
1e553 66 65 72 65 6e 63 65 20 69 73 20 6f 6e 6c 79 20  ference is only 
1e554 69 6d 70 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20  important to.** 
1e555 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e  the update hook.
1e556 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  .**.** Parameter
1e557 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f   P4 may point to
1e558 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69   a string contai
1e559 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d 6e  ning the table-n
1e55a 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62  ame, or.** may b
1e55b 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73  e NULL. If it is
1e55c 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
1e55d 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
1e55e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 70  .** (sqlite3.xUp
1e55f 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 73  dateCallback) is
1e560 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69   invoked followi
1e561 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c 20  ng a successful 
1e562 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57  insert..**.** (W
1e563 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20  ARNING/TODO: If 
1e564 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d 63  P1 is a pseudo-c
1e565 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20  ursor and P2 is 
1e566 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61  dynamically.** a
1e567 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 6f  llocated, then o
1e568 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 69  wnership of P2 i
1e569 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 6f  s transferred to
1e56a 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 73   the pseudo-curs
1e56b 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 74  or.** and regist
1e56c 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 70  er P2 becomes ep
1e56d 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 65  hemeral.  If the
1e56e 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e 67   cursor is chang
1e56f 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  ed, the.** value
1e570 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20   of register P2 
1e571 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 65  will then change
1e572 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  .  Make sure thi
1e573 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61  s does not.** ca
1e574 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73  use any problems
1e575 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  .).**.** This in
1e576 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77  struction only w
1e577 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20  orks on tables. 
1e578 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   The equivalent 
1e579 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66  instruction.** f
1e57a 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f 50  or indices is OP
1e57b 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 2f  _IdxInsert..*/./
1e57c 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74  * Opcode: Insert
1e57d 49 6e 74 20 50 31 20 50 32 20 50 33 20 50 34 20  Int P1 P2 P3 P4 
1e57e 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1e57f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c 69 6b 65  rks exactly like
1e580 20 4f 50 5f 49 6e 73 65 72 74 20 65 78 63 65 70   OP_Insert excep
1e581 74 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 69  t that the key i
1e582 73 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72  s the.** integer
1e583 20 76 61 6c 75 65 20 50 33 2c 20 6e 6f 74 20 74   value P3, not t
1e584 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1e585 69 6e 74 65 67 65 72 20 73 74 6f 72 65 64 20 69  integer stored i
1e586 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1e587 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74  /.case OP_Insert
1e588 3a 20 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72  : .case OP_Inser
1e589 74 49 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f  tInt: {.#if 0  /
1e58a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e58b 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e58c 66 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74  f */.  Mem *pDat
1e58d 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20  a;       /* MEM 
1e58e 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74  cell holding dat
1e58f 61 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  a for the record
1e590 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
1e591 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20  */.  Mem *pKey; 
1e592 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65         /* MEM ce
1e593 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20  ll holding key  
1e594 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a  for the record *
1e595 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20  /.  i64 iKey;   
1e596 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74        /* The int
1e597 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65  eger ROWID or ke
1e598 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  y for the record
1e599 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
1e59a 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1e59b 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72  *pC;   /* Cursor
1e59c 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77   to table into w
1e59d 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77  hich insert is w
1e59e 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
1e59f 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a  nZero;        /*
1e5a0 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d   Number of zero-
1e5a1 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20  bytes to append 
1e5a2 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73  */.  int seekRes
1e5a3 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74  ult;   /* Result
1e5a4 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b 20 6f   of prior seek o
1e5a5 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53 45 45  r 0 if no USESEE
1e5a6 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a  KRESULT flag */.
1e5a7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1e5a8 62 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20  b;  /* database 
1e5a9 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74  name - used by t
1e5aa 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a  he update hook *
1e5ab 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1e5ac 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e  zTbl; /* Table n
1e5ad 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68  ame - used by th
1e5ae 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f  e opdate hook */
1e5af 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20  .  int op;      
1e5b0 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66       /* Opcode f
1e5b1 6f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20  or update hook: 
1e5b2 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72  SQLITE_UPDATE or
1e5b3 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a   SQLITE_INSERT *
1e5b4 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1e5b5 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e5b6 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a  d into u.bf */..
1e5b7 20 20 75 2e 62 66 2e 70 44 61 74 61 20 3d 20 26    u.bf.pData = &
1e5b8 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1e5b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e5ba 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1e5bb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  ->nCursor );.  u
1e5bc 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .bf.pC = p->apCs
1e5bd 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1e5be 73 65 72 74 28 20 75 2e 62 66 2e 70 43 21 3d 30  sert( u.bf.pC!=0
1e5bf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1e5c0 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bf.pC->pCursor!=
1e5c1 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1e5c2 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61  .bf.pC->pseudoTa
1e5c3 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61  bleReg==0 );.  a
1e5c4 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e  ssert( u.bf.pC->
1e5c5 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47  isTable );.  REG
1e5c6 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1e5c7 3e 70 32 2c 20 75 2e 62 66 2e 70 44 61 74 61 29  >p2, u.bf.pData)
1e5c8 3b 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  ;..  if( pOp->op
1e5c9 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20  code==OP_Insert 
1e5ca 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 4b 65 79  ){.    u.bf.pKey
1e5cb 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1e5cc 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ];.    assert( u
1e5cd 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 20  .bf.pKey->flags 
1e5ce 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20  & MEM_Int );.   
1e5cf 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1e5d0 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b  pOp->p3, u.bf.pK
1e5d1 65 79 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b  ey);.    u.bf.iK
1e5d2 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e  ey = u.bf.pKey->
1e5d3 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  u.i;.  }else{.  
1e5d4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f    assert( pOp->o
1e5d5 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74  pcode==OP_Insert
1e5d6 49 6e 74 20 29 3b 0a 20 20 20 20 75 2e 62 66 2e  Int );.    u.bf.
1e5d7 69 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  iKey = pOp->p3;.
1e5d8 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e    }..  if( pOp->
1e5d9 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41  p5 & OPFLAG_NCHA
1e5da 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65  NGE ) p->nChange
1e5db 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  ++;.  if( pOp->p
1e5dc 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52  5 & OPFLAG_LASTR
1e5dd 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61 73 74 52  OWID ) db->lastR
1e5de 6f 77 69 64 20 3d 20 75 2e 62 66 2e 69 4b 65 79  owid = u.bf.iKey
1e5df 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 61  ;.  if( u.bf.pDa
1e5e0 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ta->flags & MEM_
1e5e1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66  Null ){.    u.bf
1e5e2 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20  .pData->z = 0;. 
1e5e3 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e     u.bf.pData->n
1e5e4 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1e5e5 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e     assert( u.bf.
1e5e6 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28  pData->flags & (
1e5e7 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
1e5e8 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e  ) );.  }.  u.bf.
1e5e9 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 70  seekResult = ((p
1e5ea 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e5eb 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f  USESEEKRESULT) ?
1e5ec 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bf.pC->seekRe
1e5ed 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 28  sult : 0);.  if(
1e5ee 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61   u.bf.pData->fla
1e5ef 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
1e5f0 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20  .    u.bf.nZero 
1e5f1 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e  = u.bf.pData->u.
1e5f2 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nZero;.  }else{.
1e5f3 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d      u.bf.nZero =
1e5f4 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
1e5f5 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52  3BtreeSetCachedR
1e5f6 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43  owid(u.bf.pC->pC
1e5f7 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20  ursor, 0);.  rc 
1e5f8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  = sqlite3BtreeIn
1e5f9 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43  sert(u.bf.pC->pC
1e5fa 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69  ursor, 0, u.bf.i
1e5fb 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
1e5fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
1e5fd 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e  .bf.pData->z, u.
1e5fe 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62  bf.pData->n, u.b
1e5ff 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20  f.nZero,.       
1e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e601 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46     pOp->p5 & OPF
1e602 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66  LAG_APPEND, u.bf
1e603 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b  .seekResult.  );
1e604 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69  .  u.bf.pC->rowi
1e605 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e606 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bf.pC->deferre
1e607 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75  dMoveto = 0;.  u
1e608 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bf.pC->cacheSta
1e609 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1e60a 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  E;..  /* Invoke 
1e60b 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
1e60c 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
1e60d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e60e 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61  _OK && db->xUpda
1e60f 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
1e610 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75  p->p4.z ){.    u
1e611 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44  .bf.zDb = db->aD
1e612 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e  b[u.bf.pC->iDb].
1e613 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e  zName;.    u.bf.
1e614 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  zTbl = pOp->p4.z
1e615 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20  ;.    u.bf.op = 
1e616 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ((pOp->p5 & OPFL
1e617 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53  AG_ISUPDATE) ? S
1e618 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53  QLITE_UPDATE : S
1e619 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20  QLITE_INSERT);. 
1e61a 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e     assert( u.bf.
1e61b 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1e61c 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
1e61d 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
1e61e 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20  teArg, u.bf.op, 
1e61f 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a  u.bf.zDb, u.bf.z
1e620 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b  Tbl, u.bf.iKey);
1e621 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e622 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a  f.pC->iDb>=0 );.
1e623 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1e624 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74  /* Opcode: Delet
1e625 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  e P1 P2 * P4 *.*
1e626 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
1e627 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20  record at which 
1e628 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73  the P1 cursor is
1e629 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1e62a 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ing..**.** The c
1e62b 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65  ursor will be le
1e62c 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65  ft pointing at e
1e62d 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f  ither the next o
1e62e 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  r the previous.*
1e62f 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * record in the 
1e630 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20  table. If it is 
1e631 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1e632 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64   the next record
1e633 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
1e634 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74  xt Next instruct
1e635 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f  ion will be a no
1e636 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69  -op.  Hence it i
1e637 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a  s OK to delete.*
1e638 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  * a record from 
1e639 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c  within an Next l
1e63a 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  oop..**.** If th
1e63b 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  e OPFLAG_NCHANGE
1e63c 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73   flag of P2 is s
1e63d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  et, then the row
1e63e 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73   change count is
1e63f 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  .** incremented 
1e640 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e  (otherwise not).
1e641 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e  .**.** P1 must n
1e642 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62  ot be pseudo-tab
1e643 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62  le.  It has to b
1e644 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77  e a real table w
1e645 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ith.** multiple 
1e646 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  rows..**.** If P
1e647 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  4 is not NULL, t
1e648 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61  hen it is the na
1e649 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1e64a 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f  that P1 is.** po
1e64b 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20  inting to.  The 
1e64c 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c  update hook will
1e64d 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20   be invoked, if 
1e64e 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66  it exists..** If
1e64f 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20   P4 is not NULL 
1e650 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73  then the P1 curs
1e651 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  or must have bee
1e652 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20  n positioned.** 
1e653 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e  using OP_NotFoun
1e654 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
1e655 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1e656 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65  .*/.case OP_Dele
1e657 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
1e658 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e659 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20  moved into u.bg 
1e65a 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a 20  */.  i64 iKey;. 
1e65b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e65c 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e65d 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e65e 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20   into u.bg */.. 
1e65f 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a   u.bg.iKey = 0;.
1e660 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e661 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e662 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e663 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bg.pC = p->apC
1e664 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e665 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 3d  ssert( u.bg.pC!=
1e666 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1e667 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bg.pC->pCursor!
1e668 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76  =0 );  /* Only v
1e669 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61  alid for real ta
1e66a 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74  bles, no pseudot
1e66b 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  ables */..  /* I
1e66c 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  f the update-hoo
1e66d 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  k will be invoke
1e66e 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65 79  d, set u.bg.iKey
1e66f 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66   to the rowid of
1e670 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65   the.  ** row be
1e671 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a  ing deleted..  *
1e672 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64  /.  if( db->xUpd
1e673 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1e674 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1e675 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d  assert( u.bg.pC-
1e676 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1e677 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d  assert( u.bg.pC-
1e678 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 3b  >rowidIsValid );
1e679 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 73    /* lastRowid s
1e67a 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 4f  et by previous O
1e67b 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20  P_NotFound */.  
1e67c 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e    u.bg.iKey = u.
1e67d 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64  bg.pC->lastRowid
1e67e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
1e67f 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65  OP_Delete opcode
1e680 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20   always follows 
1e681 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  an OP_NotExists 
1e682 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20  or OP_Last or.  
1e683 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20  ** OP_Column on 
1e684 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 77  the same table w
1e685 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 72  ithout any inter
1e686 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  vening operation
1e687 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68  s that.  ** migh
1e688 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69  t move or invali
1e689 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 2e  date the cursor.
1e68a 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 75    Hence cursor u
1e68b 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79 73  .bg.pC is always
1e68c 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74   pointing.  ** t
1e68d 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  o the row to be 
1e68e 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20  deleted and the 
1e68f 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e690 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 74  rMoveto() operat
1e691 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69  ion.  ** below i
1e692 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70  s always a no-op
1e693 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c   and cannot fail
1e694 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 69  .  We will run i
1e695 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68  t anyhow, though
1e696 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 20  ,.  ** to guard 
1e697 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 63  against future c
1e698 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f  hanges to the co
1e699 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20  de generator..  
1e69a 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e  **/.  assert( u.
1e69b 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bg.pC->deferredM
1e69c 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63  oveto==0 );.  rc
1e69d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1e69e 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e  rsorMoveto(u.bg.
1e69f 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  pC);.  if( NEVER
1e6a0 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
1e6a1 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e6a2 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71  _to_error;..  sq
1e6a3 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1e6a4 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 43  hedRowid(u.bg.pC
1e6a5 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20  ->pCursor, 0);. 
1e6a6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e6a7 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70 43  eeDelete(u.bg.pC
1e6a8 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e  ->pCursor);.  u.
1e6a9 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bg.pC->cacheStat
1e6aa 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e6ab 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ;..  /* Invoke t
1e6ac 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69  he update-hook i
1e6ad 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20  f required. */. 
1e6ae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e6af 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74  OK && db->xUpdat
1e6b0 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
1e6b1 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f  ->p4.z ){.    co
1e6b2 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
1e6b3 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d  db->aDb[u.bg.pC-
1e6b4 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >iDb].zName;.   
1e6b5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
1e6b6 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  l = pOp->p4.z;. 
1e6b7 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
1e6b8 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
1e6b9 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45  teArg, SQLITE_DE
1e6ba 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c  LETE, zDb, zTbl,
1e6bb 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20   u.bg.iKey);.   
1e6bc 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
1e6bd 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a  ->iDb>=0 );.  }.
1e6be 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20    if( pOp->p2 & 
1e6bf 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1e6c0 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1e6c1 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63   break;.}./* Opc
1e6c2 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20  ode: ResetCount 
1e6c3 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
1e6c4 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
1e6c5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1e6c6 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
1e6c7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1e6c8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
1e6c9 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20  er (returned by 
1e6ca 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1e6cb 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e   to sqlite3_chan
1e6cc 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20  ges())..** Then 
1e6cd 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c  the VMs internal
1e6ce 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1e6cf 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20  resets to 0..** 
1e6d0 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1e6d1 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
1e6d2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73  ..*/.case OP_Res
1e6d3 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c  etCount: {.  sql
1e6d4 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
1e6d5 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67  es(db, p->nChang
1e6d6 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65  e);.  p->nChange
1e6d7 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1e6d8 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  ../* Opcode: Row
1e6d9 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a  Data P1 P2 * * *
1e6da 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  .**.** Write int
1e6db 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68  o register P2 th
1e6dc 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64  e complete row d
1e6dd 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50  ata for cursor P
1e6de 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e  1..** There is n
1e6df 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  o interpretation
1e6e0 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a   of the data.  .
1e6e1 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f  ** It is just co
1e6e2 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32  pied onto the P2
1e6e3 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c   register exactl
1e6e4 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66  y as .** it is f
1e6e5 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
1e6e6 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
1e6e7 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f   If the P1 curso
1e6e8 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
1e6e9 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  ng to a valid ro
1e6ea 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f  w (not a NULL ro
1e6eb 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20  w).** of a real 
1e6ec 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1e6ed 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a  udo-table..*/./*
1e6ee 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20   Opcode: RowKey 
1e6ef 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e6f0 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67  * Write into reg
1e6f1 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d  ister P2 the com
1e6f2 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f  plete row key fo
1e6f3 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20  r cursor P1..** 
1e6f4 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65  There is no inte
1e6f5 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68  rpretation of th
1e6f6 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65  e data.  .** The
1e6f7 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f   key is copied o
1e6f8 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73  nto the P3 regis
1e6f9 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a  ter exactly as .
1e6fa 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69  ** it is found i
1e6fb 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1e6fc 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
1e6fd 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1e6fe 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   be pointing to 
1e6ff 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74  a valid row (not
1e700 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20   a NULL row).** 
1e701 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  of a real table,
1e702 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61   not a pseudo-ta
1e703 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1e704 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f  RowKey:.case OP_
1e705 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 30  RowData: {.#if 0
1e706 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e707 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e708 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bh */.  VdbeCu
1e709 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75  rsor *pC;.  BtCu
1e70a 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75  rsor *pCrsr;.  u
1e70b 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b  32 n;.  i64 n64;
1e70c 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e70d 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e70e 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20   into u.bh */.. 
1e70f 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1e710 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f  p->p2];..  /* No
1e711 74 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61  te that RowKey a
1e712 6e 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72  nd RowData are r
1e713 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68  eally exactly th
1e714 65 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69  e same instructi
1e715 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  on */.  assert( 
1e716 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e717 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1e718 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20   );.  u.bh.pC = 
1e719 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1e71a 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
1e71b 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c  h.pC->isTable ||
1e71c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1e71d 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73 73  _RowKey );.  ass
1e71e 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73  ert( u.bh.pC->is
1e71f 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70  Index || pOp->op
1e720 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61  code==OP_RowData
1e721 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1e722 62 68 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73  bh.pC!=0 );.  as
1e723 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e  sert( u.bh.pC->n
1e724 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61  ullRow==0 );.  a
1e725 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e  ssert( u.bh.pC->
1e726 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d  pseudoTableReg==
1e727 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1e728 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bh.pC->pCursor!
1e729 3d 30 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72  =0 );.  u.bh.pCr
1e72a 73 72 20 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43  sr = u.bh.pC->pC
1e72b 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28  ursor;.  assert(
1e72c 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1e72d 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 68 2e  sorIsValid(u.bh.
1e72e 70 43 72 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20  pCrsr) );..  /* 
1e72f 54 68 65 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e  The OP_RowKey an
1e730 64 20 4f 50 5f 52 6f 77 44 61 74 61 20 6f 70 63  d OP_RowData opc
1e731 6f 64 65 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c  odes always foll
1e732 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  ow OP_NotExists 
1e733 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e  or.  ** OP_Rewin
1e734 64 2f 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e  d/Op_Next with n
1e735 6f 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e  o intervening in
1e736 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20  structions that 
1e737 6d 69 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65  might invalidate
1e738 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72  .  ** the cursor
1e739 2e 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c  .  Hence the fol
1e73a 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64  lowing sqlite3Vd
1e73b 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29  beCursorMoveto()
1e73c 20 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a   call is always.
1e73d 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64    ** a no-op and
1e73e 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
1e73f 20 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69    But we leave i
1e740 74 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20  t in place as a 
1e741 73 61 66 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61  safety..  */.  a
1e742 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e  ssert( u.bh.pC->
1e743 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1e744 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
1e745 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1e746 65 74 6f 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20  eto(u.bh.pC);.  
1e747 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51  if( NEVER(rc!=SQ
1e748 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20  LITE_OK) ) goto 
1e749 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1e74a 6f 72 3b 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e  or;..  if( u.bh.
1e74b 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20  pC->isIndex ){. 
1e74c 20 20 20 61 73 73 65 72 74 28 20 21 75 2e 62 68     assert( !u.bh
1e74d 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  .pC->isTable );.
1e74e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e74f 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62  BtreeKeySize(u.b
1e750 68 2e 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e  h.pCrsr, &u.bh.n
1e751 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  64);.    assert(
1e752 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1e753 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63  ;    /* True bec
1e754 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f  ause of CursorMo
1e755 76 65 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76  veto() call abov
1e756 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62  e */.    if( u.b
1e757 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74  h.n64>db->aLimit
1e758 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1e759 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67  NGTH] ){.      g
1e75a 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20  oto too_big;.   
1e75b 20 7d 0a 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20   }.    u.bh.n = 
1e75c 28 75 33 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20  (u32)u.bh.n64;. 
1e75d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
1e75e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
1e75f 61 53 69 7a 65 28 75 2e 62 68 2e 70 43 72 73 72  aSize(u.bh.pCrsr
1e760 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20  , &u.bh.n);.    
1e761 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1e762 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44  TE_OK );    /* D
1e763 61 74 61 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74  ataSize() cannot
1e764 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28   fail */.    if(
1e765 20 75 2e 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d   u.bh.n>(u32)db-
1e766 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1e767 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1e768 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
1e769 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ig;.    }.  }.  
1e76a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
1e76b 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62  emGrow(pOut, u.b
1e76c 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67  h.n, 0) ){.    g
1e76d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1e76e 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68    pOut->n = u.bh
1e76f 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  .n;.  MemSetType
1e770 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42  Flag(pOut, MEM_B
1e771 6c 6f 62 29 3b 0a 20 20 69 66 28 20 75 2e 62 68  lob);.  if( u.bh
1e772 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a  .pC->isIndex ){.
1e773 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e774 42 74 72 65 65 4b 65 79 28 75 2e 62 68 2e 70 43  BtreeKey(u.bh.pC
1e775 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20  rsr, 0, u.bh.n, 
1e776 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73  pOut->z);.  }els
1e777 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
1e778 74 65 33 42 74 72 65 65 44 61 74 61 28 75 2e 62  te3BtreeData(u.b
1e779 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68  h.pCrsr, 0, u.bh
1e77a 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20  .n, pOut->z);.  
1e77b 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20  }.  pOut->enc = 
1e77c 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a  SQLITE_UTF8;  /*
1e77d 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f   In case the blo
1e77e 62 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74  b is ever cast t
1e77f 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41  o text */.  UPDA
1e780 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1e781 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
1e782 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  }../* Opcode: Ro
1e783 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  wid P1 P2 * * *.
1e784 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72  **.** Store in r
1e785 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e  egister P2 an in
1e786 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
1e787 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61  he key of the ta
1e788 62 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a  ble entry that.*
1e789 2a 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c  * P1 is currentl
1e78a 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a  y point to..**.*
1e78b 2a 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68  * P1 can be eith
1e78c 65 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74  er an ordinary t
1e78d 61 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61  able or a virtua
1e78e 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20  l table.  There 
1e78f 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20  used to.** be a 
1e790 73 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77  separate OP_VRow
1e791 69 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73  id opcode for us
1e792 65 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74  e with virtual t
1e793 61 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a  ables, but this.
1e794 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f  ** one opcode no
1e795 77 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68  w works for both
1e796 20 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f   table types..*/
1e797 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20  .case OP_Rowid: 
1e798 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1e799 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1e79a 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
1e79b 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e79c 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e79d 69 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  i */.  VdbeCurso
1e79e 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a  r *pC;.  i64 v;.
1e79f 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1e7a0 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73  pVtab;.  const s
1e7a1 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1e7a2 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f  Module;.#endif /
1e7a3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e7a4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e7a5 69 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  i */..  assert( 
1e7a6 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e7a7 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1e7a8 20 29 3b 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20   );.  u.bi.pC = 
1e7a9 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1e7aa 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
1e7ab 69 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73  i.pC!=0 );.  ass
1e7ac 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73  ert( u.bi.pC->ps
1e7ad 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20  eudoTableReg==0 
1e7ae 29 3b 0a 20 20 69 66 28 20 75 2e 62 69 2e 70 43  );.  if( u.bi.pC
1e7af 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1e7b0 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1e7b1 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 62 72 65  EM_Null;.    bre
1e7b2 61 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ak;.  }else if( 
1e7b3 75 2e 62 69 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bi.pC->deferre
1e7b4 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 20 75  dMoveto ){.    u
1e7b5 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d  .bi.v = u.bi.pC-
1e7b6 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a 23  >movetoTarget;.#
1e7b7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1e7b8 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1e7b9 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69    }else if( u.bi
1e7ba 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  .pC->pVtabCursor
1e7bb 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74   ){.    u.bi.pVt
1e7bc 61 62 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 70 56  ab = u.bi.pC->pV
1e7bd 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1e7be 3b 0a 20 20 20 20 75 2e 62 69 2e 70 4d 6f 64 75  ;.    u.bi.pModu
1e7bf 6c 65 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d  le = u.bi.pVtab-
1e7c0 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 61 73  >pModule;.    as
1e7c1 73 65 72 74 28 20 75 2e 62 69 2e 70 4d 6f 64 75  sert( u.bi.pModu
1e7c2 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20  le->xRowid );.  
1e7c3 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1e7c4 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1e7c5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1e7c6 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d 20  isuse;.    rc = 
1e7c7 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52  u.bi.pModule->xR
1e7c8 6f 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70 56  owid(u.bi.pC->pV
1e7c9 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 62 69  tabCursor, &u.bi
1e7ca 2e 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .v);.    sqlite3
1e7cb 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1e7cc 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1e7cd 45 72 72 4d 73 67 20 3d 20 75 2e 62 69 2e 70 56  ErrMsg = u.bi.pV
1e7ce 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1e7cf 20 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45    u.bi.pVtab->zE
1e7d0 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 69  rrMsg = 0;.    i
1e7d1 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1e7d2 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1e7d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1e7d4 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  e;.#endif /* SQL
1e7d5 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1e7d6 54 41 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65  TABLE */.  }else
1e7d7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  {.    assert( u.
1e7d8 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bi.pC->pCursor!=
1e7d9 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 );.    rc = sq
1e7da 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1e7db 6f 76 65 74 6f 28 75 2e 62 69 2e 70 43 29 3b 0a  oveto(u.bi.pC);.
1e7dc 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1e7dd 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1e7de 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e  rror;.    if( u.
1e7df 62 69 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bi.pC->rowidIsVa
1e7e0 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 75 2e 62  lid ){.      u.b
1e7e1 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6c  i.v = u.bi.pC->l
1e7e2 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d 65  astRowid;.    }e
1e7e3 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1e7e4 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
1e7e5 69 7a 65 28 75 2e 62 69 2e 70 43 2d 3e 70 43 75  ize(u.bi.pC->pCu
1e7e6 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a  rsor, &u.bi.v);.
1e7e7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
1e7e8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20  ==SQLITE_OK );  
1e7e9 2f 2a 20 41 6c 77 61 79 73 20 73 6f 20 62 65 63  /* Always so bec
1e7ea 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f  ause of CursorMo
1e7eb 76 65 74 6f 28 29 20 61 62 6f 76 65 20 2a 2f 0a  veto() above */.
1e7ec 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74      }.  }.  pOut
1e7ed 2d 3e 75 2e 69 20 3d 20 75 2e 62 69 2e 76 3b 0a  ->u.i = u.bi.v;.
1e7ee 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e7ef 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f 77 20 50  pcode: NullRow P
1e7f0 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1e7f1 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1e7f2 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77  P1 to a null row
1e7f3 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e  .  Any OP_Column
1e7f4 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 74   operations.** t
1e7f5 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c 65 20  hat occur while 
1e7f6 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f 6e  the cursor is on
1e7f7 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20 77 69   the null row wi
1e7f8 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 77 72 69  ll always.** wri
1e7f9 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  te a NULL..*/.ca
1e7fa 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b  se OP_NullRow: {
1e7fb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1e7fc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e7fd 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20   into u.bj */.  
1e7fe 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1e7ff 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e800 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e801 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20  into u.bj */..  
1e802 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1e803 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1e804 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  >nCursor );.  u.
1e805 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  bj.pC = p->apCsr
1e806 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1e807 65 72 74 28 20 75 2e 62 6a 2e 70 43 21 3d 30 20  ert( u.bj.pC!=0 
1e808 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75  );.  u.bj.pC->nu
1e809 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 62  llRow = 1;.  u.b
1e80a 6a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  j.pC->rowidIsVal
1e80b 69 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e  id = 0;.  if( u.
1e80c 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 20 29  bj.pC->pCursor )
1e80d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
1e80e 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 75 2e  eeClearCursor(u.
1e80f 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b  bj.pC->pCursor);
1e810 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e811 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74  ./* Opcode: Last
1e812 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e813 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20  ** The next use 
1e814 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20  of the Rowid or 
1e815 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69  Column or Next i
1e816 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50  nstruction for P
1e817 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72  1 .** will refer
1e818 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
1e819 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
1e81a 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  se table or inde
1e81b 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62  x..** If the tab
1e81c 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65  le or index is e
1e81d 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74  mpty and P2>0, t
1e81e 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  hen jump immedia
1e81f 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49  tely to P2..** I
1e820 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20  f P2 is 0 or if 
1e821 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
1e822 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c  ex is not empty,
1e823 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a   fall through.**
1e824 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
1e825 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  g instruction..*
1e826 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20  /.case OP_Last: 
1e827 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e828 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e829 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e82a 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f  ved into u.bk */
1e82b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e82c 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1e82d 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1e82e 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e82f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e830 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20   into u.bk */.. 
1e831 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e832 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1e833 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  ->nCursor );.  u
1e834 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .bk.pC = p->apCs
1e835 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1e836 73 65 72 74 28 20 75 2e 62 6b 2e 70 43 21 3d 30  sert( u.bk.pC!=0
1e837 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 72 73 72   );.  u.bk.pCrsr
1e838 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e 70 43 75 72   = u.bk.pC->pCur
1e839 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 6b 2e  sor;.  if( u.bk.
1e83a 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20  pCrsr==0 ){.    
1e83b 75 2e 62 6b 2e 72 65 73 20 3d 20 31 3b 0a 20 20  u.bk.res = 1;.  
1e83c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1e83d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74  sqlite3BtreeLast
1e83e 28 75 2e 62 6b 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bk.pCrsr, &u.
1e83f 62 6b 2e 72 65 73 29 3b 0a 20 20 7d 0a 20 20 75  bk.res);.  }.  u
1e840 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bk.pC->nullRow 
1e841 3d 20 28 75 38 29 75 2e 62 6b 2e 72 65 73 3b 0a  = (u8)u.bk.res;.
1e842 20 20 75 2e 62 6b 2e 70 43 2d 3e 64 65 66 65 72    u.bk.pC->defer
1e843 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1e844 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f 77 69 64 49   u.bk.pC->rowidI
1e845 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e  sValid = 0;.  u.
1e846 62 6b 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bk.pC->cacheStat
1e847 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e848 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e  ;.  if( pOp->p2>
1e849 30 20 26 26 20 75 2e 62 6b 2e 72 65 73 20 29 7b  0 && u.bk.res ){
1e84a 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1e84b 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1e84c 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64  ak;.}.../* Opcod
1e84d 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20  e: Sort P1 P2 * 
1e84e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  * *.**.** This o
1e84f 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74  pcode does exact
1e850 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  ly the same thin
1e851 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65  g as OP_Rewind e
1e852 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74  xcept that.** it
1e853 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75   increments an u
1e854 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62  ndocumented glob
1e855 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73 65 64  al variable used
1e856 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a   for testing..**
1e857 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61  .** Sorting is a
1e858 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77  ccomplished by w
1e859 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69  riting records i
1e85a 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  nto a sorting in
1e85b 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77  dex,.** then rew
1e85c 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65  inding that inde
1e85d 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74  x and playing it
1e85e 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e   back from begin
1e85f 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20  ning to.** end. 
1e860 20 57 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53   We use the OP_S
1e861 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65  ort opcode inste
1e862 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20  ad of OP_Rewind 
1e863 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77  to do the.** rew
1e864 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74  inding so that t
1e865 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  he global variab
1e866 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65  le will be incre
1e867 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65  mented and.** re
1e868 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63  gression tests c
1e869 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  an determine whe
1e86a 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
1e86b 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20  optimizer is.** 
1e86c 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69  correctly optimi
1e86d 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a  zing out sorts..
1e86e 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a  */.case OP_Sort:
1e86f 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1e870 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  p */.#ifdef SQLI
1e871 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65  TE_TEST.  sqlite
1e872 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_sort_count++;.
1e873 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
1e874 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66  _count--;.#endif
1e875 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53  .  p->aCounter[S
1e876 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
1e877 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a  _SORT-1]++;.  /*
1e878 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e   Fall through in
1e879 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a  to OP_Rewind */.
1e87a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77  }./* Opcode: Rew
1e87b 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ind P1 P2 * * *.
1e87c 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75  **.** The next u
1e87d 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20  se of the Rowid 
1e87e 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78  or Column or Nex
1e87f 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f  t instruction fo
1e880 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65  r P1 .** will re
1e881 66 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  fer to the first
1e882 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1e883 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1e884 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65  index..** If the
1e885 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1e886 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e  is empty and P2>
1e887 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d  0, then jump imm
1e888 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1e889 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72  ** If P2 is 0 or
1e88a 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   if the table or
1e88b 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d   index is not em
1e88c 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  pty, fall throug
1e88d 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1e88e 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1e88f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  n..*/.case OP_Re
1e890 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f  wind: {        /
1e891 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20  * jump */.#if 0 
1e892 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e893 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e894 2e 62 6c 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bl */.  VdbeCur
1e895 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1e896 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1e897 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a  t res;.#endif /*
1e898 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e899 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c   moved into u.bl
1e89a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e89b 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e89c 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e89d 29 3b 0a 20 20 75 2e 62 6c 2e 70 43 20 3d 20 70  );.  u.bl.pC = p
1e89e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e89f 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6c  ;.  assert( u.bl
1e8a0 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  .pC!=0 );.  if( 
1e8a1 28 75 2e 62 6c 2e 70 43 72 73 72 20 3d 20 75 2e  (u.bl.pCrsr = u.
1e8a2 62 6c 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 21  bl.pC->pCursor)!
1e8a3 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  =0 ){.    rc = s
1e8a4 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74  qlite3BtreeFirst
1e8a5 28 75 2e 62 6c 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bl.pCrsr, &u.
1e8a6 62 6c 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62  bl.res);.    u.b
1e8a7 6c 2e 70 43 2d 3e 61 74 46 69 72 73 74 20 3d 20  l.pC->atFirst = 
1e8a8 75 2e 62 6c 2e 72 65 73 3d 3d 30 20 3f 31 3a 30  u.bl.res==0 ?1:0
1e8a9 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 64  ;.    u.bl.pC->d
1e8aa 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e8ab 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e  0;.    u.bl.pC->
1e8ac 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e8ad 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75  CHE_STALE;.    u
1e8ae 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bl.pC->rowidIsV
1e8af 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  alid = 0;.  }els
1e8b0 65 7b 0a 20 20 20 20 75 2e 62 6c 2e 72 65 73 20  e{.    u.bl.res 
1e8b1 3d 20 31 3b 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e  = 1;.  }.  u.bl.
1e8b2 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75  pC->nullRow = (u
1e8b3 38 29 75 2e 62 6c 2e 72 65 73 3b 0a 20 20 61 73  8)u.bl.res;.  as
1e8b4 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1e8b5 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f  && pOp->p2<p->nO
1e8b6 70 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 6c 2e  p );.  if( u.bl.
1e8b7 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20  res ){.    pc = 
1e8b8 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1e8b9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e8ba 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50 31 20  Opcode: Next P1 
1e8bb 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1e8bc 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20 50 31  dvance cursor P1
1e8bd 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
1e8be 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6b  ts to the next k
1e8bf 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20  ey/data pair in 
1e8c0 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20  its.** table or 
1e8c1 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65  index.  If there
1e8c2 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b 65 79   are no more key
1e8c3 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65  /value pairs the
1e8c4 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  n fall through.*
1e8c5 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1e8c6 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ng instruction. 
1e8c7 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73   But if the curs
1e8c8 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73 20 73  or advance was s
1e8c9 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75  uccessful,.** ju
1e8ca 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1e8cb 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o P2..**.** The 
1e8cc 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62  P1 cursor must b
1e8cd 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62  e for a real tab
1e8ce 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f  le, not a pseudo
1e8cf 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65  -table..**.** Se
1e8d0 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a  e also: Prev.*/.
1e8d1 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65 76 20  /* Opcode: Prev 
1e8d2 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e8d3 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73 6f 72  * Back up cursor
1e8d4 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70   P1 so that it p
1e8d5 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 72 65  oints to the pre
1e8d6 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61 20 70  vious key/data p
1e8d7 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61  air in its.** ta
1e8d8 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49  ble or index.  I
1e8d9 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 72  f there is no pr
1e8da 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c 75 65  evious key/value
1e8db 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c   pairs then fall
1e8dc 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
1e8dd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
1e8de 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69  truction.  But i
1e8df 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 61 63  f the cursor bac
1e8e0 6b 75 70 20 77 61 73 20 73 75 63 63 65 73 73 66  kup was successf
1e8e1 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65  ul,.** jump imme
1e8e2 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a  diately to P2..*
1e8e3 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73  *.** The P1 curs
1e8e4 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61  or must be for a
1e8e5 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74   real table, not
1e8e6 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e   a pseudo-table.
1e8e7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 65 76  .*/.case OP_Prev
1e8e8 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  :          /* ju
1e8e9 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  mp */.case OP_Ne
1e8ea 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  xt: {        /* 
1e8eb 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1e8ec 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e8ed 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e8ee 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  m */.  VdbeCurso
1e8ef 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
1e8f0 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20  r *pCrsr;.  int 
1e8f1 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  res;.#endif /* l
1e8f2 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e8f3 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a  oved into u.bm *
1e8f4 2f 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  /..  CHECK_FOR_I
1e8f5 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65  NTERRUPT;.  asse
1e8f6 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e8f7 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e8f8 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6d 2e 70  rsor );.  u.bm.p
1e8f9 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e8fa 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 75 2e 62  ->p1];.  if( u.b
1e8fb 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62  m.pC==0 ){.    b
1e8fc 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69  reak;  /* See ti
1e8fd 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20  cket #2273 */.  
1e8fe 7d 0a 20 20 75 2e 62 6d 2e 70 43 72 73 72 20 3d  }.  u.bm.pCrsr =
1e8ff 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bm.pC->pCurso
1e900 72 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43  r;.  if( u.bm.pC
1e901 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e  rsr==0 ){.    u.
1e902 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d  bm.pC->nullRow =
1e903 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   1;.    break;. 
1e904 20 7d 0a 20 20 75 2e 62 6d 2e 72 65 73 20 3d 20   }.  u.bm.res = 
1e905 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  1;.  assert( u.b
1e906 6d 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  m.pC->deferredMo
1e907 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20  veto==0 );.  rc 
1e908 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  = pOp->opcode==O
1e909 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 33  P_Next ? sqlite3
1e90a 42 74 72 65 65 4e 65 78 74 28 75 2e 62 6d 2e 70  BtreeNext(u.bm.p
1e90b 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29  Crsr, &u.bm.res)
1e90c 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   :.             
1e90d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e90e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65   sqlite3BtreePre
1e90f 76 69 6f 75 73 28 75 2e 62 6d 2e 70 43 72 73 72  vious(u.bm.pCrsr
1e910 2c 20 26 75 2e 62 6d 2e 72 65 73 29 3b 0a 20 20  , &u.bm.res);.  
1e911 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.bm.pC->nullRow
1e912 20 3d 20 28 75 38 29 75 2e 62 6d 2e 72 65 73 3b   = (u8)u.bm.res;
1e913 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 63 61 63 68  .  u.bm.pC->cach
1e914 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1e915 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 75 2e 62  STALE;.  if( u.b
1e916 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  m.res==0 ){.    
1e917 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1e918 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  ;.    if( pOp->p
1e919 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b  5 ) p->aCounter[
1e91a 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69  pOp->p5-1]++;.#i
1e91b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1e91c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61  .    sqlite3_sea
1e91d 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  rch_count++;.#en
1e91e 64 69 66 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70  dif.  }.  u.bm.p
1e91f 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e920 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  = 0;.  break;.}.
1e921 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 49  ./* Opcode: IdxI
1e922 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 2a  nsert P1 P2 P3 *
1e923 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74   P5.**.** Regist
1e924 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 53 51  er P2 holds a SQ
1e925 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 64 65  L index key made
1e926 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 4d 61   using the.** Ma
1e927 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63  keRecord instruc
1e928 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f 70 63  tions.  This opc
1e929 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 74 20  ode writes that 
1e92a 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  key.** into the 
1e92b 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 61 20  index P1.  Data 
1e92c 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 69 73  for the entry is
1e92d 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69   nil..**.** P3 i
1e92e 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 70 72  s a flag that pr
1e92f 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 74 6f  ovides a hint to
1e930 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65   the b-tree laye
1e931 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 69  r that this.** i
1e932 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20  nsert is likely 
1e933 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 2e  to be an append.
1e934 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1e935 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72  ruction only wor
1e936 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 2e 20  ks for indices. 
1e937 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   The equivalent 
1e938 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66  instruction.** f
1e939 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f 50 5f  or tables is OP_
1e93a 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20  Insert..*/.case 
1e93b 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 7b 20  OP_IdxInsert: { 
1e93c 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f         /* in2 */
1e93d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1e93e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e93f 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20   into u.bn */.  
1e940 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1e941 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1e942 72 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  r;.  int nKey;. 
1e943 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65   const char *zKe
1e944 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  y;.#endif /* loc
1e945 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e946 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a  ed into u.bn */.
1e947 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e948 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e949 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e94a 20 75 2e 62 6e 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bn.pC = p->ap
1e94b 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e94c 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43 21  assert( u.bn.pC!
1e94d 3d 30 20 29 3b 0a 20 20 70 49 6e 32 20 3d 20 26  =0 );.  pIn2 = &
1e94e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1e94f 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66   assert( pIn2->f
1e950 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20  lags & MEM_Blob 
1e951 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73 72 20  );.  u.bn.pCrsr 
1e952 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73  = u.bn.pC->pCurs
1e953 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  or;.  if( ALWAYS
1e954 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30 29 20  (u.bn.pCrsr!=0) 
1e955 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ){.    assert( u
1e956 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d  .bn.pC->isTable=
1e957 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 45  =0 );.    rc = E
1e958 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b  xpandBlob(pIn2);
1e959 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1e95a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1e95b 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32  u.bn.nKey = pIn2
1e95c 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e  ->n;.      u.bn.
1e95d 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a  zKey = pIn2->z;.
1e95e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1e95f 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 75 2e  e3BtreeInsert(u.
1e960 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a  bn.pCrsr, u.bn.z
1e961 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20  Key, u.bn.nKey, 
1e962 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70  "", 0, 0, pOp->p
1e963 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28 70  3,.          ((p
1e964 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e965 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f  USESEEKRESULT) ?
1e966 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bn.pC->seekRe
1e967 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20 20  sult : 0).      
1e968 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1e969 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72   u.bn.pC->deferr
1e96a 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1e96b 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61       u.bn.pC->ca
1e96c 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1e96d 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20  E_STALE;.    }. 
1e96e 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e96f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c  * Opcode: IdxDel
1e970 65 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ete P1 P2 P3 * *
1e971 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65  .**.** The conte
1e972 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73 74 65  nt of P3 registe
1e973 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72  rs starting at r
1e974 65 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a  egister P2 form.
1e975 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  ** an unpacked i
1e976 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f  ndex key. This o
1e977 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68  pcode removes th
1e978 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  at entry from th
1e979 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e  e .** index open
1e97a 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e  ed by cursor P1.
1e97b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44  .*/.case OP_IdxD
1e97c 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20  elete: {.#if 0  
1e97d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e97e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e97f 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  bo */.  VdbeCurs
1e980 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
1e981 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74  or *pCrsr;.  int
1e982 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64   res;.  Unpacked
1e983 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66  Record r;.#endif
1e984 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e985 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e986 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .bo */..  assert
1e987 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20  ( pOp->p3>0 );. 
1e988 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1e989 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f  >0 && pOp->p2+pO
1e98a 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  p->p3<=p->nMem+1
1e98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1e98c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e98d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e98e 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d  ;.  u.bo.pC = p-
1e98f 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e990 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e  .  assert( u.bo.
1e991 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e  pC!=0 );.  u.bo.
1e992 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d  pCrsr = u.bo.pC-
1e993 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  >pCursor;.  if( 
1e994 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43 72 73  ALWAYS(u.bo.pCrs
1e995 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 2e 62  r!=0) ){.    u.b
1e996 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75  o.r.pKeyInfo = u
1e997 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f  .bo.pC->pKeyInfo
1e998 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69  ;.    u.bo.r.nFi
1e999 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e  eld = (u16)pOp->
1e99a 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66  p3;.    u.bo.r.f
1e99b 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e  lags = 0;.    u.
1e99c 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65  bo.r.aMem = &aMe
1e99d 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1e99e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e99f 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1e9a0 75 2e 62 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bo.pCrsr, &u.b
1e9a1 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f  o.r, 0, 0, &u.bo
1e9a2 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  .res);.    if( r
1e9a3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1e9a4 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20  u.bo.res==0 ){. 
1e9a5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e9a6 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62  3BtreeDelete(u.b
1e9a7 6f 2e 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a  o.pCrsr);.    }.
1e9a8 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f      assert( u.bo
1e9a9 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e9aa 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e  eto==0 );.    u.
1e9ab 62 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bo.pC->cacheStat
1e9ac 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e9ad 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1e9ae 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
1e9af 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20  Rowid P1 P2 * * 
1e9b0 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e  *.**.** Write in
1e9b1 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 61  to register P2 a
1e9b2 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1e9b3 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  is the last entr
1e9b4 79 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  y in the record 
1e9b5 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
1e9b6 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 70   the index key p
1e9b7 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75 72  ointed to by cur
1e9b8 73 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69 6e  sor P1.  This in
1e9b9 74 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65 0a  teger should be.
1e9ba 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ** the rowid of 
1e9bb 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
1e9bc 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 69 6e  to which this in
1e9bd 64 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  dex entry points
1e9be 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1e9bf 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63  : Rowid, MakeRec
1e9c0 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ord..*/.case OP_
1e9c1 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 20 20 20  IdxRowid: {     
1e9c2 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1e9c3 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23  -prerelease */.#
1e9c4 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e9c5 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e9c6 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74  nto u.bp */.  Bt
1e9c7 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1e9c8 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e9c9 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 23 65  .  i64 rowid;.#e
1e9ca 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e9cb 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e9cc 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73  to u.bp */..  as
1e9cd 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e9ce 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e9cf 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70  Cursor );.  u.bp
1e9d0 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e9d1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e9d2 74 28 20 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b  t( u.bp.pC!=0 );
1e9d3 0a 20 20 75 2e 62 70 2e 70 43 72 73 72 20 3d 20  .  u.bp.pCrsr = 
1e9d4 75 2e 62 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bp.pC->pCursor
1e9d5 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20  ;.  pOut->flags 
1e9d6 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66  = MEM_Null;.  if
1e9d7 28 20 41 4c 57 41 59 53 28 75 2e 62 70 2e 70 43  ( ALWAYS(u.bp.pC
1e9d8 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 72  rsr!=0) ){.    r
1e9d9 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  c = sqlite3VdbeC
1e9da 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 70  ursorMoveto(u.bp
1e9db 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 4e 45  .pC);.    if( NE
1e9dc 56 45 52 28 72 63 29 20 29 20 67 6f 74 6f 20 61  VER(rc) ) goto a
1e9dd 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e9de 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  r;.    assert( u
1e9df 2e 62 70 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bp.pC->deferred
1e9e0 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
1e9e1 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43   assert( u.bp.pC
1e9e2 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  ->isTable==0 );.
1e9e3 20 20 20 20 69 66 28 20 21 75 2e 62 70 2e 70 43      if( !u.bp.pC
1e9e4 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20  ->nullRow ){.   
1e9e5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1e9e6 64 62 65 49 64 78 52 6f 77 69 64 28 64 62 2c 20  dbeIdxRowid(db, 
1e9e7 75 2e 62 70 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bp.pCrsr, &u.b
1e9e8 70 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  p.rowid);.      
1e9e9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1e9ea 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
1e9eb 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1e9ec 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rror;.      }.  
1e9ed 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1e9ee 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20  u.bp.rowid;.    
1e9ef 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1e9f0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 20  MEM_Int;.    }. 
1e9f1 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e9f2 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20  * Opcode: IdxGE 
1e9f3 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1e9f4 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69  *.** The P4 regi
1e9f5 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69  ster values begi
1e9f6 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f  nning with P3 fo
1e9f7 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  rm an unpacked i
1e9f8 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61  ndex .** key tha
1e9f9 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49  t omits the ROWI
1e9fa 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73  D.  Compare this
1e9fb 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e   key value again
1e9fc 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a  st the index .**
1e9fd 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72   that P1 is curr
1e9fe 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
1e9ff 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20  o, ignoring the 
1ea00 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20  ROWID on the P1 
1ea01 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  index..**.** If 
1ea02 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74  the P1 index ent
1ea03 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ry is greater th
1ea04 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1ea05 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20  he key value.** 
1ea06 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1ea07 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c    Otherwise fall
1ea08 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1ea09 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1ea0a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73  ..**.** If P5 is
1ea0b 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74   non-zero then t
1ea0c 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20  he key value is 
1ea0d 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20  increased by an 
1ea0e 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f  epsilon .** prio
1ea0f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69  r to the compari
1ea10 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20  son.  This make 
1ea11 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
1ea12 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70  like IdxGT excep
1ea13 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65  t.** that if the
1ea14 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74   key from regist
1ea15 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69  er P3 is a prefi
1ea16 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20  x of the key in 
1ea17 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74  the cursor,.** t
1ea18 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c  he result is fal
1ea19 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f  se whereas it wo
1ea1a 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68  uld be true with
1ea1b 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70   IdxGT..*/./* Op
1ea1c 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50  code: IdxLT P1 P
1ea1d 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20  2 P3 * P5.**.** 
1ea1e 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20  The P4 register 
1ea1f 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67  values beginning
1ea20 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e   with P3 form an
1ea21 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20   unpacked index 
1ea22 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69  .** key that omi
1ea23 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43  ts the ROWID.  C
1ea24 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20  ompare this key 
1ea25 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68  value against th
1ea26 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74  e index .** that
1ea27 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79   P1 is currently
1ea28 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67   pointing to, ig
1ea29 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44  noring the ROWID
1ea2a 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78   on the P1 index
1ea2b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
1ea2c 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  1 index entry is
1ea2d 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b   less than the k
1ea2e 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75  ey value then ju
1ea2f 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68  mp to P2..** Oth
1ea30 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f  erwise fall thro
1ea31 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1ea32 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
1ea33 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d  ** If P5 is non-
1ea34 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65  zero then the ke
1ea35 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65  y value is incre
1ea36 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c  ased by an epsil
1ea37 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20  on prior .** to 
1ea38 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
1ea39 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20   This makes the 
1ea3a 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65  opcode work like
1ea3b 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20   IdxLE..*/.case 
1ea3c 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20  OP_IdxLT:       
1ea3d 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61     /* jump */.ca
1ea3e 73 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20  se OP_IdxGE: {  
1ea3f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1ea40 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ea41 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ea42 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20   into u.bq */.  
1ea43 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1ea44 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70    int res;.  Unp
1ea45 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23  ackedRecord r;.#
1ea46 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1ea47 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ea48 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61  nto u.bq */..  a
1ea49 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1ea4a 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1ea4b 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1ea4c 71 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  q.pC = p->apCsr[
1ea4d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1ea4e 72 74 28 20 75 2e 62 71 2e 70 43 21 3d 30 20 29  rt( u.bq.pC!=0 )
1ea4f 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ;.  if( ALWAYS(u
1ea50 2e 62 71 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bq.pC->pCursor!
1ea51 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  =0) ){.    asser
1ea52 74 28 20 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65  t( u.bq.pC->defe
1ea53 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1ea54 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1ea55 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e  ->p5==0 || pOp->
1ea56 70 35 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73  p5==1 );.    ass
1ea57 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1ea58 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20  ==P4_INT32 );.  
1ea59 20 20 75 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66    u.bq.r.pKeyInf
1ea5a 6f 20 3d 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65  o = u.bq.pC->pKe
1ea5b 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e  yInfo;.    u.bq.
1ea5c 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  r.nField = (u16)
1ea5d 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69  pOp->p4.i;.    i
1ea5e 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20  f( pOp->p5 ){.  
1ea5f 20 20 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73      u.bq.r.flags
1ea60 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52   = UNPACKED_INCR
1ea61 4b 45 59 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49  KEY | UNPACKED_I
1ea62 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20  GNORE_ROWID;.   
1ea63 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e   }else{.      u.
1ea64 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  bq.r.flags = UNP
1ea65 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57  ACKED_IGNORE_ROW
1ea66 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e  ID;.    }.    u.
1ea67 62 71 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65  bq.r.aMem = &aMe
1ea68 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1ea69 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1ea6a 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e  IdxKeyCompare(u.
1ea6b 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20  bq.pC, &u.bq.r, 
1ea6c 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20  &u.bq.res);.    
1ea6d 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1ea6e 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20  =OP_IdxLT ){.   
1ea6f 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75     u.bq.res = -u
1ea70 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c  .bq.res;.    }el
1ea71 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
1ea72 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1ea73 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20  P_IdxGE );.     
1ea74 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20   u.bq.res++;.   
1ea75 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e   }.    if( u.bq.
1ea76 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70  res>0 ){.      p
1ea77 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20  c = pOp->p2 - 1 
1ea78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1ea79 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ea7a 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32  e: Destroy P1 P2
1ea7b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65   P3 * *.**.** De
1ea7c 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64  lete an entire d
1ea7d 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1ea7e 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1ea7f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  t page in the da
1ea80 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69  tabase.** file i
1ea81 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a  s given by P1..*
1ea82 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62  *.** The table b
1ea83 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69  eing destroyed i
1ea84 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  s in the main da
1ea85 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1ea86 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d  3==0.  If.** P3=
1ea87 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  =1 then the tabl
1ea88 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73  e to be clear is
1ea89 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
1ea8a 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  y database file.
1ea8b 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20  ** that is used 
1ea8c 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20  to store tables 
1ea8d 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45  create using CRE
1ea8e 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41  ATE TEMPORARY TA
1ea8f 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55  BLE..**.** If AU
1ea90 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62  TOVACUUM is enab
1ea91 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70  led then it is p
1ea92 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f  ossible that ano
1ea93 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a  ther root page.*
1ea94 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64  * might be moved
1ea95 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20   into the newly 
1ea96 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67  deleted root pag
1ea97 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  e in order to ke
1ea98 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70  ep all.** root p
1ea99 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20  ages contiguous 
1ea9a 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
1ea9b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1ea9c 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a  .  The former.**
1ea9d 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1ea9e 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76  ot page that mov
1ea9f 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62  ed - its value b
1eaa0 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f  efore the move o
1eaa1 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20  ccurred -.** is 
1eaa2 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1eaa3 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61  er P2.  If no pa
1eaa4 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20  ge .** movement 
1eaa5 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65  was required (be
1eaa6 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
1eaa7 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61  being dropped wa
1eaa8 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68  s already .** th
1eaa9 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68  e last one in th
1eaaa 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e  e database) then
1eaab 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65   a zero is store
1eaac 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  d in register P2
1eaad 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55  ..** If AUTOVACU
1eaae 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74  UM is disabled t
1eaaf 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74  hen a zero is st
1eab0 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1eab1 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1eab2 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61  lso: Clear.*/.ca
1eab3 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b  se OP_Destroy: {
1eab4 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1eab5 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1eab6 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1eab7 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1eab8 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  u.br */.  int iM
1eab9 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74  oved;.  int iCnt
1eaba 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  ;.  Vdbe *pVdbe;
1eabb 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64  .  int iDb;.#end
1eabc 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1eabd 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1eabe 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66   u.br */.#ifndef
1eabf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1eac0 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72  TUALTABLE.  u.br
1eac1 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72  .iCnt = 0;.  for
1eac2 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e  (u.br.pVdbe=db->
1eac3 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62  pVdbe; u.br.pVdb
1eac4 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20  e; u.br.pVdbe = 
1eac5 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78  u.br.pVdbe->pNex
1eac6 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72  t){.    if( u.br
1eac7 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56  .pVdbe->magic==V
1eac8 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26  DBE_MAGIC_RUN &&
1eac9 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56   u.br.pVdbe->inV
1eaca 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75  tabMethod<2 && u
1eacb 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30  .br.pVdbe->pc>=0
1eacc 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69   ){.      u.br.i
1eacd 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  Cnt++;.    }.  }
1eace 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43  .#else.  u.br.iC
1eacf 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56  nt = db->activeV
1ead0 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20  dbeCnt;.#endif. 
1ead1 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1ead2 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 75  EM_Null;.  if( u
1ead3 2e 62 72 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20  .br.iCnt>1 ){.  
1ead4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f    rc = SQLITE_LO
1ead5 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72  CKED;.    p->err
1ead6 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62  orAction = OE_Ab
1ead7 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ort;.  }else{.  
1ead8 20 20 75 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70    u.br.iDb = pOp
1ead9 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 74  ->p3;.    assert
1eada 28 20 75 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29  ( u.br.iCnt==1 )
1eadb 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1eadc 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1eadd 3c 3c 75 2e 62 72 2e 69 44 62 29 29 21 3d 30 20  <<u.br.iDb))!=0 
1eade 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1eadf 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
1eae0 65 28 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69  e(db->aDb[u.br.i
1eae1 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31  Db].pBt, pOp->p1
1eae2 2c 20 26 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b  , &u.br.iMoved);
1eae3 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1eae4 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1eae5 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72  pOut->u.i = u.br
1eae6 2e 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66  .iMoved;.#ifndef
1eae7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1eae8 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
1eae9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1eaea 20 75 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20   u.br.iMoved!=0 
1eaeb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1eaec 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64  RootPageMoved(&d
1eaed 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d  b->aDb[u.br.iDb]
1eaee 2c 20 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70  , u.br.iMoved, p
1eaef 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 20 20 72  Op->p1);.      r
1eaf0 65 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c  esetSchemaOnFaul
1eaf1 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e  t = 1;.    }.#en
1eaf2 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  dif.  }.  break;
1eaf3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1eaf4 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a  lear P1 P2 P3.**
1eaf5 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63  .** Delete all c
1eaf6 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64  ontents of the d
1eaf7 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1eaf8 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1eaf9 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65  t page.** in the
1eafa 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1eafb 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20  s given by P1.  
1eafc 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74  But, unlike Dest
1eafd 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72  roy, do not.** r
1eafe 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20  emove the table 
1eaff 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68  or index from th
1eb00 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1eb01 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
1eb02 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20   being clear is 
1eb03 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
1eb04 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d  base file if P2=
1eb05 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31  =0.  If.** P2==1
1eb06 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
1eb07 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69  to be clear is i
1eb08 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
1eb09 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
1eb0a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
1eb0b 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72   store tables cr
1eb0c 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54  eate using CREAT
1eb0d 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c  E TEMPORARY TABL
1eb0e 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  E..**.** If the 
1eb0f 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d  P3 value is non-
1eb10 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74  zero, then the t
1eb11 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
1eb12 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69   must be an.** i
1eb13 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20  ntkey table (an 
1eb14 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61  SQL table, not a
1eb15 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69  n index). In thi
1eb16 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63  s case the row c
1eb17 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20  hange .** count 
1eb18 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62  is incremented b
1eb19 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
1eb1a 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
1eb1b 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e  e being cleared.
1eb1c 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72   .** If P3 is gr
1eb1d 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c  eater than zero,
1eb1e 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
1eb1f 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1eb20 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f  er P3 is.** also
1eb21 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20   incremented by 
1eb22 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
1eb23 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ws in the table 
1eb24 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a  being cleared..*
1eb25 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44  *.** See also: D
1eb26 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f  estroy.*/.case O
1eb27 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30  P_Clear: {.#if 0
1eb28 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1eb29 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1eb2a 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  u.bs */.  int nC
1eb2b 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  hange;.#endif /*
1eb2c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1eb2d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73   moved into u.bs
1eb2e 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61   */..  u.bs.nCha
1eb2f 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  nge = 0;.  asser
1eb30 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1eb31 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29   & (1<<pOp->p2))
1eb32 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  !=0 );.  rc = sq
1eb33 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
1eb34 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e  able(.      db->
1eb35 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74  aDb[pOp->p2].pBt
1eb36 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d  , pOp->p1, (pOp-
1eb37 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61  >p3 ? &u.bs.nCha
1eb38 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20  nge : 0).  );.  
1eb39 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20  if( pOp->p3 ){. 
1eb3a 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d     p->nChange +=
1eb3b 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20   u.bs.nChange;. 
1eb3c 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30     if( pOp->p3>0
1eb3d 20 29 7b 0a 20 20 20 20 20 20 61 4d 65 6d 5b 70   ){.      aMem[p
1eb3e 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75  Op->p3].u.i += u
1eb3f 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20  .bs.nChange;.   
1eb40 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1eb41 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72  }../* Opcode: Cr
1eb42 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32 20  eateTable P1 P2 
1eb43 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f  * * *.**.** Allo
1eb44 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65  cate a new table
1eb45 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
1eb46 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
1eb47 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a  ==0 or in the.**
1eb48 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
1eb49 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d  ase file if P1==
1eb4a 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63  1 or in an attac
1eb4b 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66 0a  hed database if.
1eb4c 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20  ** P1>1.  Write 
1eb4d 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
1eb4e 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20  mber of the new 
1eb4f 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65  table into.** re
1eb50 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20  gister P2.**.** 
1eb51 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
1eb52 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61  etween a table a
1eb53 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74  nd an index is t
1eb54 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75  his:  A table mu
1eb55 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62  st.** have a 4-b
1eb56 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20  yte integer key 
1eb57 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72 62  and can have arb
1eb58 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e  itrary data.  An
1eb59 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e   index.** has an
1eb5a 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20 62   arbitrary key b
1eb5b 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a  ut no data..**.*
1eb5c 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61  * See also: Crea
1eb5d 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70  teIndex.*/./* Op
1eb5e 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65  code: CreateInde
1eb5f 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  x P1 P2 * * *.**
1eb60 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
1eb61 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20  ew index in the 
1eb62 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1eb63 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69  le if P1==0 or i
1eb64 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61  n the.** auxilia
1eb65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1eb66 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20   if P1==1 or in 
1eb67 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61  an attached data
1eb68 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e  base if.** P1>1.
1eb69 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74    Write the root
1eb6a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
1eb6b 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  the new table in
1eb6c 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  to.** register P
1eb6d 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63  2..**.** See doc
1eb6e 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50  umentation on OP
1eb6f 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72  _CreateTable for
1eb70 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1eb71 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  rmation..*/.case
1eb72 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a   OP_CreateIndex:
1eb73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1eb74 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1eb75 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65  /.case OP_Create
1eb76 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20  Table: {        
1eb77 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1eb78 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
1eb79 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eb7a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1eb7b 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b  t */.  int pgno;
1eb7c 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20  .  int flags;.  
1eb7d 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20  Db *pDb;.#endif 
1eb7e 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1eb7f 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1eb80 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67  bt */..  u.bt.pg
1eb81 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  no = 0;.  assert
1eb82 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1eb83 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1eb84 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1eb85 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1eb86 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1eb87 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d 20 26 64  .  u.bt.pDb = &d
1eb88 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b  b->aDb[pOp->p1];
1eb89 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 74 2e  .  assert( u.bt.
1eb8a 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20  pDb->pBt!=0 );. 
1eb8b 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1eb8c 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  ==OP_CreateTable
1eb8d 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e   ){.    /* u.bt.
1eb8e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e  flags = BTREE_IN
1eb8f 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62  TKEY; */.    u.b
1eb90 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f  t.flags = BTREE_
1eb91 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49  LEAFDATA|BTREE_I
1eb92 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NTKEY;.  }else{.
1eb93 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d      u.bt.flags =
1eb94 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b   BTREE_ZERODATA;
1eb95 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
1eb96 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
1eb97 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42  ble(u.bt.pDb->pB
1eb98 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75  t, &u.bt.pgno, u
1eb99 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f  .bt.flags);.  pO
1eb9a 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70  ut->u.i = u.bt.p
1eb9b 67 6e 6f 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  gno;.  break;.}.
1eb9c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 73  ./* Opcode: Pars
1eb9d 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a 20  eSchema P1 P2 * 
1eb9e 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20  P4 *.**.** Read 
1eb9f 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65 6e  and parse all en
1eba0 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 53  tries from the S
1eba1 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
1eba2 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 50  le of database P
1eba3 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 20  1.** that match 
1eba4 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1eba5 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 65 20   P4.  P2 is the 
1eba6 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20 20  "force" flag.   
1eba7 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65  Always do.** the
1eba8 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 20 69   parsing if P2 i
1eba9 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 20 69  s true.  If P2 i
1ebaa 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68  s false, then th
1ebab 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 0a  is routine is a.
1ebac 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20  ** no-op if the 
1ebad 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63 75  schema is not cu
1ebae 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20  rrently loaded. 
1ebaf 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1ebb0 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c   if P2.** is fal
1ebb1 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 4d  se, the SQLITE_M
1ebb2 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 20 6f  ASTER table is o
1ebb3 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 74 68  nly parsed if th
1ebb4 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a  e rest of the.**
1ebb5 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61   schema is alrea
1ebb6 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74  dy loaded into t
1ebb7 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e  he symbol table.
1ebb8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1ebb9 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70  de invokes the p
1ebba 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 20  arser to create 
1ebbb 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61  a new virtual ma
1ebbc 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72  chine,.** then r
1ebbd 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 74  uns the new virt
1ebbe 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 74  ual machine.  It
1ebbf 20 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 6e   is thus a re-en
1ebc0 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f  trant opcode..*/
1ebc1 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63  .case OP_ParseSc
1ebc2 68 65 6d 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f  hema: {.#if 0  /
1ebc3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ebc4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1ebc5 75 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a  u */.  int iDb;.
1ebc6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
1ebc7 61 73 74 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a  aster;.  char *z
1ebc8 53 71 6c 3b 0a 20 20 49 6e 69 74 44 61 74 61 20  Sql;.  InitData 
1ebc9 69 6e 69 74 44 61 74 61 3b 0a 23 65 6e 64 69 66  initData;.#endif
1ebca 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ebcb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ebcc 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69  .bu */..  u.bu.i
1ebcd 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
1ebce 61 73 73 65 72 74 28 20 75 2e 62 75 2e 69 44 62  assert( u.bu.iDb
1ebcf 3e 3d 30 20 26 26 20 75 2e 62 75 2e 69 44 62 3c  >=0 && u.bu.iDb<
1ebd0 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a  db->nDb );..  /*
1ebd1 20 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20 30   If pOp->p2 is 0
1ebd2 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f  , then this opco
1ebd3 64 65 20 69 73 20 62 65 69 6e 67 20 65 78 65 63  de is being exec
1ebd4 75 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a 20  uted to read a. 
1ebd5 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20   ** single row, 
1ebd6 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20  for example the 
1ebd7 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  row correspondin
1ebd8 67 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65 78  g to a new index
1ebd9 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79  .  ** created by
1ebda 20 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f 6d   this VDBE, from
1ebdb 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1ebdc 65 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c  er table. It onl
1ebdd 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73  y.  ** does this
1ebde 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f   if the correspo
1ebdf 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20  nding in-memory 
1ebe0 73 63 68 65 6d 61 20 69 73 20 63 75 72 72 65 6e  schema is curren
1ebe1 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e  tly.  ** loaded.
1ebe2 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
1ebe3 6e 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e 69  new index defini
1ebe4 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64  tion can be load
1ebe5 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69  ed along.  ** wi
1ebe6 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
1ebe7 68 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20 69  he schema when i
1ebe8 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20  t is required.. 
1ebe9 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67   **.  ** Althoug
1ebea 68 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 74  h the mutex on t
1ebeb 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  he BtShared obje
1ebec 63 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  ct that correspo
1ebed 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61  nds to.  ** data
1ebee 62 61 73 65 20 75 2e 62 75 2e 69 44 62 20 28 74  base u.bu.iDb (t
1ebef 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
1ebf0 61 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69 74  aining the sqlit
1ebf1 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20  e_master table. 
1ebf2 20 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69 73   ** read by this
1ebf3 20 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69 73   instruction) is
1ebf4 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c   currently held,
1ebf5 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79   it is necessary
1ebf6 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20   to.  ** obtain 
1ebf7 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 61  the mutexes on a
1ebf8 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ll attached data
1ebf9 62 61 73 65 73 20 62 65 66 6f 72 65 20 63 68 65  bases before che
1ebfa 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68  cking if.  ** th
1ebfb 65 20 73 63 68 65 6d 61 20 6f 66 20 75 2e 62 75  e schema of u.bu
1ebfc 2e 69 44 62 20 69 73 20 6c 6f 61 64 65 64 2e 20  .iDb is loaded. 
1ebfd 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 2c  This is because,
1ebfe 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
1ebff 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  .  ** the sqlite
1ec00 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 62 65  3_exec() call be
1ec01 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c  low, SQLite will
1ec02 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c   invoke.  ** sql
1ec03 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
1ec04 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65  l(). If all mute
1ec05 78 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 72 65  xes are not alre
1ec06 61 64 79 20 68 65 6c 64 2c 20 74 68 65 0a 20 20  ady held, the.  
1ec07 2a 2a 20 75 2e 62 75 2e 69 44 62 20 6d 75 74 65  ** u.bu.iDb mute
1ec08 78 20 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61  x may be tempora
1ec09 72 69 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f  rily released to
1ec0a 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e   avoid deadlock.
1ec0b 20 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 68 61   If.  ** this ha
1ec0c 70 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65  ppens, then some
1ec0d 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61   other thread ma
1ec0e 79 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d  y delete the in-
1ec0f 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65  memory.  ** sche
1ec10 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 75  ma of database u
1ec11 2e 62 75 2e 69 44 62 20 62 65 66 6f 72 65 20 74  .bu.iDb before t
1ec12 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1ec13 20 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d   runs. The schem
1ec14 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  a.  ** will not 
1ec15 62 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75  be reloaded becu
1ec16 61 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74  ase the db->init
1ec17 2e 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65  .busy flag is se
1ec18 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e  t. This.  ** can
1ec19 20 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f   result in a "no
1ec1a 20 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c   such table: sql
1ec1b 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22  ite_master" or "
1ec1c 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64  malformed.  ** d
1ec1d 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20  atabase schema" 
1ec1e 65 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75  error being retu
1ec1f 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  rned to the user
1ec20 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
1ec21 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1ec22 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b  dsMutex(db->aDb[
1ec23 75 2e 62 75 2e 69 44 62 5d 2e 70 42 74 29 20 29  u.bu.iDb].pBt) )
1ec24 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1ec25 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20  EnterAll(db);.  
1ec26 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44  if( pOp->p2 || D
1ec27 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
1ec28 20 75 2e 62 75 2e 69 44 62 2c 20 44 42 5f 53 63   u.bu.iDb, DB_Sc
1ec29 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20  hemaLoaded) ){. 
1ec2a 20 20 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20     u.bu.zMaster 
1ec2b 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 75  = SCHEMA_TABLE(u
1ec2c 2e 62 75 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e  .bu.iDb);.    u.
1ec2d 62 75 2e 69 6e 69 74 44 61 74 61 2e 64 62 20 3d  bu.initData.db =
1ec2e 20 64 62 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e   db;.    u.bu.in
1ec2f 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70  itData.iDb = pOp
1ec30 2d 3e 70 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69  ->p1;.    u.bu.i
1ec31 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67  nitData.pzErrMsg
1ec32 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a   = &p->zErrMsg;.
1ec33 20 20 20 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20      u.bu.zSql = 
1ec34 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1ec35 62 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43  b,.       "SELEC
1ec36 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65  T name, rootpage
1ec37 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e  , sql FROM '%q'.
1ec38 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20  %s WHERE %s",.  
1ec39 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62       db->aDb[u.b
1ec3a 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e  u.iDb].zName, u.
1ec3b 62 75 2e 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d  bu.zMaster, pOp-
1ec3c 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20  >p4.z);.    if( 
1ec3d 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a  u.bu.zSql==0 ){.
1ec3e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1ec3f 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
1ec40 73 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29  se{.      (void)
1ec41 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1ec42 28 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73 65  (db);.      asse
1ec43 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  rt( db->init.bus
1ec44 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62  y==0 );.      db
1ec45 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b  ->init.busy = 1;
1ec46 0a 20 20 20 20 20 20 75 2e 62 75 2e 69 6e 69 74  .      u.bu.init
1ec47 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45  Data.rc = SQLITE
1ec48 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  _OK;.      asser
1ec49 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
1ec4a 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 63  iled );.      rc
1ec4b 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
1ec4c 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73  db, u.bu.zSql, s
1ec4d 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
1ec4e 63 6b 2c 20 26 75 2e 62 75 2e 69 6e 69 74 44 61  ck, &u.bu.initDa
1ec4f 74 61 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  ta, 0);.      if
1ec50 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1ec51 29 20 72 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74  ) rc = u.bu.init
1ec52 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 20 20 73  Data.rc;.      s
1ec53 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ec54 20 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20   u.bu.zSql);.   
1ec55 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79     db->init.busy
1ec56 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69   = 0;.      (voi
1ec57 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1ec58 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  n(db);.    }.  }
1ec59 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
1ec5a 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69  eaveAll(db);.  i
1ec5b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
1ec5c 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  MEM ){.    goto 
1ec5d 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72  no_mem;.  }.  br
1ec5e 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66  eak;.}..#if !def
1ec5f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1ec60 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63  _ANALYZE)./* Opc
1ec61 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69  ode: LoadAnalysi
1ec62 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  s P1 * * * *.**.
1ec63 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69  ** Read the sqli
1ec64 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66  te_stat1 table f
1ec65 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61  or database P1 a
1ec66 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74  nd load the cont
1ec67 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74  ent.** of that t
1ec68 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e  able into the in
1ec69 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73  ternal index has
1ec6a 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77  h table.  This w
1ec6b 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65  ill cause.** the
1ec6c 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20   analysis to be 
1ec6d 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72  used when prepar
1ec6e 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65  ing all subseque
1ec6f 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63  nt queries..*/.c
1ec70 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79  ase OP_LoadAnaly
1ec71 73 69 73 3a 20 7b 0a 20 20 61 73 73 65 72 74 28  sis: {.  assert(
1ec72 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1ec73 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
1ec74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1ec75 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c  AnalysisLoad(db,
1ec76 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65   pOp->p1);.  bre
1ec77 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f  ak;  .}.#endif /
1ec78 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
1ec79 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20  E_OMIT_ANALYZE) 
1ec7a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  */../* Opcode: D
1ec7b 72 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20  ropTable P1 * * 
1ec7c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
1ec7d 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1ec7e 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1ec7f 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1ec80 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1ec81 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69  table named P4 i
1ec82 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20  n database P1.  
1ec83 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61  This is called a
1ec84 66 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20  fter a table.** 
1ec85 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72  is dropped in or
1ec86 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20  der to keep the 
1ec87 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
1ec88 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a  ntation of the.*
1ec89 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74  * schema consist
1ec8a 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73  ent with what is
1ec8b 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73   on disk..*/.cas
1ec8c 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20  e OP_DropTable: 
1ec8d 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  {.  sqlite3Unlin
1ec8e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28  kAndDeleteTable(
1ec8f 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
1ec90 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b  ->p4.z);.  break
1ec91 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ec92 44 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a  DropIndex P1 * *
1ec93 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f   P4 *.**.** Remo
1ec94 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ve the internal 
1ec95 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61  (in-memory) data
1ec96 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74   structures that
1ec97 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65   describe.** the
1ec98 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20   index named P4 
1ec99 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20  in database P1. 
1ec9a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
1ec9b 61 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a  after an index.*
1ec9c 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20  * is dropped in 
1ec9d 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68  order to keep th
1ec9e 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65  e internal repre
1ec9f 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1eca0 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69  .** schema consi
1eca1 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20  stent with what 
1eca2 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63  is on disk..*/.c
1eca3 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78  ase OP_DropIndex
1eca4 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c  : {.  sqlite3Unl
1eca5 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65  inkAndDeleteInde
1eca6 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  x(db, pOp->p1, p
1eca7 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65  Op->p4.z);.  bre
1eca8 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1eca9 3a 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31  : DropTrigger P1
1ecaa 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1ecab 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72  Remove the inter
1ecac 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20  nal (in-memory) 
1ecad 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1ecae 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a  that describe.**
1ecaf 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d   the trigger nam
1ecb0 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73  ed P4 in databas
1ecb1 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63  e P1.  This is c
1ecb2 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72  alled after a tr
1ecb3 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70  igger.** is drop
1ecb4 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ped in order to 
1ecb5 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61  keep the interna
1ecb6 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
1ecb7 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d   of the.** schem
1ecb8 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74  a consistent wit
1ecb9 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73  h what is on dis
1ecba 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72  k..*/.case OP_Dr
1ecbb 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73  opTrigger: {.  s
1ecbc 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
1ecbd 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c  eleteTrigger(db,
1ecbe 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1ecbf 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.z);.  break;.}
1ecc0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1ecc1 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
1ecc2 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65  _CHECK./* Opcode
1ecc3 3a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31  : IntegrityCk P1
1ecc4 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a   P2 P3 * P5.**.*
1ecc5 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73  * Do an analysis
1ecc6 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c   of the currentl
1ecc7 79 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e  y open database.
1ecc8 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65    Store in.** re
1ecc9 67 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65  gister P1 the te
1ecca 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d  xt of an error m
1eccb 65 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e  essage describin
1eccc 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a  g any problems..
1eccd 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d  ** If no problem
1ecce 73 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f  s are found, sto
1eccf 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67  re a NULL in reg
1ecd0 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20  ister P1..**.** 
1ecd1 54 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20  The register P3 
1ecd2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78  contains the max
1ecd3 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
1ecd4 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a  llowed errors..*
1ecd5 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33  * At most reg(P3
1ecd6 29 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65  ) errors will be
1ecd7 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e   reported..** In
1ecd8 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
1ecd9 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73  e analysis stops
1ecda 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28   as soon as reg(
1ecdb 50 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a  P1) errors are .
1ecdc 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31  ** seen.  Reg(P1
1ecdd 29 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74  ) is updated wit
1ecde 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  h the number of 
1ecdf 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67  errors remaining
1ece0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ..**.** The root
1ece1 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66   page numbers of
1ece2 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74   all tables in t
1ece3 68 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20  he database are 
1ece4 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65  integer.** store
1ece5 64 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65  d in reg(P1), re
1ece6 67 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b  g(P1+1), reg(P1+
1ece7 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20  2), ....  There 
1ece8 61 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a  are P2 tables.**
1ece9 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66   total..**.** If
1ecea 20 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P5 is not zero,
1eceb 20 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f   the check is do
1ecec 6e 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69  ne on the auxili
1eced 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ary database.** 
1ecee 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61  file, not the ma
1ecef 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
1ecf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1ecf1 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69  ode is used to i
1ecf2 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74  mplement the int
1ecf3 65 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61  egrity_check pra
1ecf4 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  gma..*/.case OP_
1ecf5 49 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23  IntegrityCk: {.#
1ecf6 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ecf7 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ecf8 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e  nto u.bv */.  in
1ecf9 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a  t nRoot;      /*
1ecfa 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65   Number of table
1ecfb 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75  s to check.  (Nu
1ecfc 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67  mber of root pag
1ecfd 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  es.) */.  int *a
1ecfe 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72  Root;     /* Arr
1ecff 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e  ay of rootpage n
1ed00 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65  umbers for table
1ed01 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20  s to be checked 
1ed02 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20  */.  int j;     
1ed03 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1ed04 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  nter */.  int nE
1ed05 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  rr;       /* Num
1ed06 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65  ber of errors re
1ed07 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72  ported */.  char
1ed08 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54   *z;        /* T
1ed09 65 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ext of the error
1ed0a 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d   report */.  Mem
1ed0b 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20   *pnErr;     /* 
1ed0c 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67  Register keeping
1ed0d 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73   track of errors
1ed0e 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65   remaining */.#e
1ed0f 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ed10 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ed11 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e  to u.bv */..  u.
1ed12 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e  bv.nRoot = pOp->
1ed13 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  p2;.  assert( u.
1ed14 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20  bv.nRoot>0 );.  
1ed15 75 2e 62 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c  u.bv.aRoot = sql
1ed16 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1ed17 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a  db, sizeof(int)*
1ed18 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29  (u.bv.nRoot+1) )
1ed19 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e 61 52 6f  ;.  if( u.bv.aRo
1ed1a 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ot==0 ) goto no_
1ed1b 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  mem;.  assert( p
1ed1c 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
1ed1d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1ed1e 20 20 75 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26    u.bv.pnErr = &
1ed1f 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1ed20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70   assert( (u.bv.p
1ed21 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45  nErr->flags & ME
1ed22 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61  M_Int)!=0 );.  a
1ed23 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45  ssert( (u.bv.pnE
1ed24 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  rr->flags & (MEM
1ed25 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d  _Str|MEM_Blob))=
1ed26 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26  =0 );.  pIn1 = &
1ed27 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1ed28 20 66 6f 72 28 75 2e 62 76 2e 6a 3d 30 3b 20 75   for(u.bv.j=0; u
1ed29 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f 74  .bv.j<u.bv.nRoot
1ed2a 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20 20  ; u.bv.j++){.   
1ed2b 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76   u.bv.aRoot[u.bv
1ed2c 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69 74  .j] = (int)sqlit
1ed2d 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26  e3VdbeIntValue(&
1ed2e 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a 20  pIn1[u.bv.j]);. 
1ed2f 20 7d 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b   }.  u.bv.aRoot[
1ed30 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20 61  u.bv.j] = 0;.  a
1ed31 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64  ssert( pOp->p5<d
1ed32 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
1ed33 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1ed34 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29  k & (1<<pOp->p5)
1ed35 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62 76 2e 7a  )!=0 );.  u.bv.z
1ed36 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
1ed37 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64 62  ntegrityCheck(db
1ed38 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70  ->aDb[pOp->p5].p
1ed39 42 74 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 2c 20  Bt, u.bv.aRoot, 
1ed3a 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20 20  u.bv.nRoot,.    
1ed3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed3c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
1ed3d 74 29 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e  t)u.bv.pnErr->u.
1ed3e 69 2c 20 26 75 2e 62 76 2e 6e 45 72 72 29 3b 0a  i, &u.bv.nErr);.
1ed3f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ed40 64 62 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 29 3b  db, u.bv.aRoot);
1ed41 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75  .  u.bv.pnErr->u
1ed42 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e 45 72 72 3b  .i -= u.bv.nErr;
1ed43 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1ed44 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a  mSetNull(pIn1);.
1ed45 20 20 69 66 28 20 75 2e 62 76 2e 6e 45 72 72 3d    if( u.bv.nErr=
1ed46 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
1ed47 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a 20  ( u.bv.z==0 );. 
1ed48 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 76 2e   }else if( u.bv.
1ed49 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  z==0 ){.    goto
1ed4a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65   no_mem;.  }else
1ed4b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1ed4c 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31 2c  eMemSetStr(pIn1,
1ed4d 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c 20 53 51 4c   u.bv.z, -1, SQL
1ed4e 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65  ITE_UTF8, sqlite
1ed4f 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55  3_free);.  }.  U
1ed50 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1ed51 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69  ZE(pIn1);.  sqli
1ed52 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1ed53 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f  oding(pIn1, enco
1ed54 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  ding);.  break;.
1ed55 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1ed56 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
1ed57 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f  Y_CHECK */../* O
1ed58 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64 64  pcode: RowSetAdd
1ed59 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1ed5a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69 6e  ** Insert the in
1ed5b 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c 64  teger value held
1ed5c 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32 20   by register P2 
1ed5d 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 69  into a boolean i
1ed5e 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e 20  ndex.** held in 
1ed5f 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a  register P1..**.
1ed60 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  ** An assertion 
1ed61 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20 6e  fails if P2 is n
1ed62 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ot an integer..*
1ed63 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74  /.case OP_RowSet
1ed64 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20  Add: {       /* 
1ed65 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 70 49  in1, in2 */.  pI
1ed66 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
1ed67 70 31 5d 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61  p1];.  pIn2 = &a
1ed68 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1ed69 61 73 73 65 72 74 28 20 28 70 49 6e 32 2d 3e 66  assert( (pIn2->f
1ed6a 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21  lags & MEM_Int)!
1ed6b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e  =0 );.  if( (pIn
1ed6c 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  1->flags & MEM_R
1ed6d 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20  owSet)==0 ){.   
1ed6e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1ed6f 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a  etRowSet(pIn1);.
1ed70 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66      if( (pIn1->f
1ed71 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
1ed72 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  t)==0 ) goto no_
1ed73 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  mem;.  }.  sqlit
1ed74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70  e3RowSetInsert(p
1ed75 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20  In1->u.pRowSet, 
1ed76 70 49 6e 32 2d 3e 75 2e 69 29 3b 0a 20 20 62 72  pIn2->u.i);.  br
1ed77 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ed78 65 3a 20 52 6f 77 53 65 74 52 65 61 64 20 50 31  e: RowSetRead P1
1ed79 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ed7a 20 45 78 74 72 61 63 74 20 74 68 65 20 73 6d 61   Extract the sma
1ed7b 6c 6c 65 73 74 20 76 61 6c 75 65 20 66 72 6f 6d  llest value from
1ed7c 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50   boolean index P
1ed7d 31 20 61 6e 64 20 70 75 74 20 74 68 61 74 20 76  1 and put that v
1ed7e 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67  alue into.** reg
1ed7f 69 73 74 65 72 20 50 33 2e 20 20 4f 72 2c 20 69  ister P3.  Or, i
1ed80 66 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20  f boolean index 
1ed81 50 31 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  P1 is initially 
1ed82 65 6d 70 74 79 2c 20 6c 65 61 76 65 20 50 33 0a  empty, leave P3.
1ed83 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64  ** unchanged and
1ed84 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63   jump to instruc
1ed85 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  tion P2..*/.case
1ed86 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 3a 20   OP_RowSetRead: 
1ed87 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c  {       /* jump,
1ed88 20 69 6e 31 2c 20 6f 75 74 33 20 2a 2f 0a 23 69   in1, out3 */.#i
1ed89 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ed8a 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ed8b 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 69 36 34  to u.bw */.  i64
1ed8c 20 76 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   val;.#endif /* 
1ed8d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ed8e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 77 20  moved into u.bw 
1ed8f 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  */.  CHECK_FOR_I
1ed90 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 49 6e 31  NTERRUPT;.  pIn1
1ed91 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
1ed92 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  ];.  if( (pIn1->
1ed93 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1ed94 65 74 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c  et)==0.   || sql
1ed95 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 70  ite3RowSetNext(p
1ed96 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20  In1->u.pRowSet, 
1ed97 26 75 2e 62 77 2e 76 61 6c 29 3d 3d 30 0a 20 20  &u.bw.val)==0.  
1ed98 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f  ){.    /* The bo
1ed99 6f 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65  olean index is e
1ed9a 6d 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69  mpty */.    sqli
1ed9b 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1ed9c 6c 28 70 49 6e 31 29 3b 0a 20 20 20 20 70 63 20  l(pIn1);.    pc 
1ed9d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1ed9e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41   }else{.    /* A
1ed9f 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c 6c 65   value was pulle
1eda0 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78  d from the index
1eda1 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
1eda2 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26  dbeMemSetInt64(&
1eda3 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2c 20 75  aMem[pOp->p3], u
1eda4 2e 62 77 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20  .bw.val);.  }.  
1eda5 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1eda6 6f 64 65 3a 20 52 6f 77 53 65 74 54 65 73 74 20  ode: RowSetTest 
1eda7 50 31 20 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a  P1 P2 P3 P4.**.*
1eda8 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 69 73  * Register P3 is
1eda9 20 61 73 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64   assumed to hold
1edaa 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65   a 64-bit intege
1edab 72 20 76 61 6c 75 65 2e 20 49 66 20 72 65 67 69  r value. If regi
1edac 73 74 65 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61  ster P1.** conta
1edad 69 6e 73 20 61 20 52 6f 77 53 65 74 20 6f 62 6a  ins a RowSet obj
1edae 65 63 74 20 61 6e 64 20 74 68 61 74 20 52 6f 77  ect and that Row
1edaf 53 65 74 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61  Set object conta
1edb0 69 6e 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  ins.** the value
1edb1 20 68 65 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d   held in P3, jum
1edb2 70 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  p to register P2
1edb3 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 73  . Otherwise, ins
1edb4 65 72 74 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67  ert the.** integ
1edb5 65 72 20 69 6e 20 50 33 20 69 6e 74 6f 20 74 68  er in P3 into th
1edb6 65 20 52 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e  e RowSet and con
1edb7 74 69 6e 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a  tinue on to the.
1edb8 2a 2a 20 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a  ** next opcode..
1edb9 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74  **.** The RowSet
1edba 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 74 69 6d   object is optim
1edbb 69 7a 65 64 20 66 6f 72 20 74 68 65 20 63 61 73  ized for the cas
1edbc 65 20 77 68 65 72 65 20 73 75 63 63 65 73 73 69  e where successi
1edbd 76 65 20 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e  ve sets.** of in
1edbe 74 65 67 65 72 73 2c 20 77 68 65 72 65 20 65 61  tegers, where ea
1edbf 63 68 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20  ch set contains 
1edc0 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2e 20 45  no duplicates. E
1edc1 61 63 68 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61  ach set.** of va
1edc2 6c 75 65 73 20 69 73 20 69 64 65 6e 74 69 66 69  lues is identifi
1edc3 65 64 20 62 79 20 61 20 75 6e 69 71 75 65 20 50  ed by a unique P
1edc4 34 20 76 61 6c 75 65 2e 20 54 68 65 20 66 69 72  4 value. The fir
1edc5 73 74 20 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68  st set.** must h
1edc6 61 76 65 20 50 34 3d 3d 30 2c 20 74 68 65 20 66  ave P4==0, the f
1edc7 69 6e 61 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20  inal set P4=-1. 
1edc8 20 50 34 20 6d 75 73 74 20 62 65 20 65 69 74 68   P4 must be eith
1edc9 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d  er -1 or.** non-
1edca 6e 65 67 61 74 69 76 65 2e 20 20 46 6f 72 20 6e  negative.  For n
1edcb 6f 6e 2d 6e 65 67 61 74 69 76 65 20 76 61 6c 75  on-negative valu
1edcc 65 73 20 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68  es of P4 only th
1edcd 65 20 6c 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74  e lower 4.** bit
1edce 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61 6e  s are significan
1edcf 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c  t..**.** This al
1edd0 6c 6f 77 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  lows optimizatio
1edd1 6e 73 3a 20 28 61 29 20 77 68 65 6e 20 50 34 3d  ns: (a) when P4=
1edd2 3d 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e  =0 there is no n
1edd3 65 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74  eed to test.** t
1edd4 68 65 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74  he rowset object
1edd5 20 66 6f 72 20 50 33 2c 20 61 73 20 69 74 20 69   for P3, as it i
1edd6 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74  s guaranteed not
1edd7 20 74 6f 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a   to contain it,.
1edd8 2a 2a 20 28 62 29 20 77 68 65 6e 20 50 34 3d 3d  ** (b) when P4==
1edd9 2d 31 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e  -1 there is no n
1edda 65 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74 68  eed to insert th
1eddb 65 20 76 61 6c 75 65 2c 20 61 73 20 69 74 20 77  e value, as it w
1eddc 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20  ill.** never be 
1eddd 74 65 73 74 65 64 20 66 6f 72 2c 20 61 6e 64 20  tested for, and 
1edde 28 63 29 20 77 68 65 6e 20 61 20 76 61 6c 75 65  (c) when a value
1eddf 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66   that is part of
1ede0 20 73 65 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73   set X is.** ins
1ede1 65 72 74 65 64 2c 20 74 68 65 72 65 20 69 73 20  erted, there is 
1ede2 6e 6f 20 6e 65 65 64 20 74 6f 20 73 65 61 72 63  no need to searc
1ede3 68 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  h to see if the 
1ede4 73 61 6d 65 20 76 61 6c 75 65 20 77 61 73 0a 2a  same value was.*
1ede5 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73  * previously ins
1ede6 65 72 74 65 64 20 61 73 20 70 61 72 74 20 6f 66  erted as part of
1ede7 20 73 65 74 20 58 20 28 6f 6e 6c 79 20 69 66 20   set X (only if 
1ede8 69 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  it was previousl
1ede9 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 61 73  y.** inserted as
1edea 20 70 61 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74   part of some ot
1edeb 68 65 72 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73  her set)..*/.cas
1edec 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 3a  e OP_RowSetTest:
1eded 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1edee 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1edef 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  in1, in3 */.#if 
1edf0 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1edf1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1edf2 20 75 2e 62 78 20 2a 2f 0a 20 20 69 6e 74 20 69   u.bx */.  int i
1edf3 53 65 74 3b 0a 20 20 69 6e 74 20 65 78 69 73 74  Set;.  int exist
1edf4 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  s;.#endif /* loc
1edf5 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1edf6 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a  ed into u.bx */.
1edf7 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1edf8 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33  pOp->p1];.  pIn3
1edf9 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1edfa 5d 3b 0a 20 20 75 2e 62 78 2e 69 53 65 74 20 3d  ];.  u.bx.iSet =
1edfb 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73   pOp->p4.i;.  as
1edfc 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67  sert( pIn3->flag
1edfd 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20  s&MEM_Int );..  
1edfe 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
1edff 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68  nything other th
1ee00 61 6e 20 61 20 72 6f 77 73 65 74 20 6f 62 6a 65  an a rowset obje
1ee01 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c  ct in memory cel
1ee02 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74  l P1,.  ** delet
1ee03 65 20 69 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69  e it now and ini
1ee04 74 69 61 6c 69 7a 65 20 50 31 20 77 69 74 68 20  tialize P1 with 
1ee05 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 0a  an empty rowset.
1ee06 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31    */.  if( (pIn1
1ee07 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1ee08 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  wSet)==0 ){.    
1ee09 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ee0a 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20  tRowSet(pIn1);. 
1ee0b 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c     if( (pIn1->fl
1ee0c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
1ee0d 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  )==0 ) goto no_m
1ee0e 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  em;.  }..  asser
1ee0f 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1ee10 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73  P4_INT32 );.  as
1ee11 73 65 72 74 28 20 75 2e 62 78 2e 69 53 65 74 3d  sert( u.bx.iSet=
1ee12 3d 2d 31 20 7c 7c 20 75 2e 62 78 2e 69 53 65 74  =-1 || u.bx.iSet
1ee13 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62  >=0 );.  if( u.b
1ee14 78 2e 69 53 65 74 20 29 7b 0a 20 20 20 20 75 2e  x.iSet ){.    u.
1ee15 62 78 2e 65 78 69 73 74 73 20 3d 20 73 71 6c 69  bx.exists = sqli
1ee16 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 70 49  te3RowSetTest(pI
1ee17 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20  n1->u.pRowSet,. 
1ee18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75                (u
1ee1a 38 29 28 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20  8)(u.bx.iSet>=0 
1ee1b 3f 20 75 2e 62 78 2e 69 53 65 74 20 26 20 30 78  ? u.bx.iSet & 0x
1ee1c 66 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20  f : 0xff),.     
1ee1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee1e 20 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e            pIn3->
1ee1f 75 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 75 2e  u.i);.    if( u.
1ee20 62 78 2e 65 78 69 73 74 73 20 29 7b 0a 20 20 20  bx.exists ){.   
1ee21 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1ee22 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  - 1;.      break
1ee23 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1ee24 28 20 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20 29  ( u.bx.iSet>=0 )
1ee25 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77  {.    sqlite3Row
1ee26 53 65 74 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e  SetInsert(pIn1->
1ee27 75 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d  u.pRowSet, pIn3-
1ee28 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65  >u.i);.  }.  bre
1ee29 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  ak;.}...#ifndef 
1ee2a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
1ee2b 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  GER../* Opcode: 
1ee2c 50 72 6f 67 72 61 6d 20 50 31 20 50 32 20 50 33  Program P1 P2 P3
1ee2d 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63   P4 *.**.** Exec
1ee2e 75 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ute the trigger 
1ee2f 70 72 6f 67 72 61 6d 20 70 61 73 73 65 64 20 61  program passed a
1ee30 73 20 50 34 20 28 74 79 70 65 20 50 34 5f 53 55  s P4 (type P4_SU
1ee31 42 50 52 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a  BPROGRAM). .**.*
1ee32 2a 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 74 68  * P1 contains th
1ee33 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
1ee34 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   memory cell tha
1ee35 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66  t contains the f
1ee36 69 72 73 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  irst memory .** 
1ee37 63 65 6c 6c 20 69 6e 20 61 6e 20 61 72 72 61 79  cell in an array
1ee38 20 6f 66 20 76 61 6c 75 65 73 20 75 73 65 64 20   of values used 
1ee39 61 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  as arguments to 
1ee3a 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e  the sub-program.
1ee3b 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73   P2 .** contains
1ee3c 20 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20   the address to 
1ee3d 6a 75 6d 70 20 74 6f 20 69 66 20 74 68 65 20 73  jump to if the s
1ee3e 75 62 2d 70 72 6f 67 72 61 6d 20 74 68 72 6f 77  ub-program throw
1ee3f 73 20 61 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20  s an IGNORE .** 
1ee40 65 78 63 65 70 74 69 6f 6e 20 75 73 69 6e 67 20  exception using 
1ee41 74 68 65 20 52 41 49 53 45 28 29 20 66 75 6e 63  the RAISE() func
1ee42 74 69 6f 6e 2e 20 52 65 67 69 73 74 65 72 20 50  tion. Register P
1ee43 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61  3 contains the a
1ee44 64 64 72 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20  ddress .** of a 
1ee45 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74  memory cell in t
1ee46 68 69 73 20 28 74 68 65 20 70 61 72 65 6e 74 29  his (the parent)
1ee47 20 56 4d 20 74 68 61 74 20 69 73 20 75 73 65 64   VM that is used
1ee48 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65   to allocate the
1ee49 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75   .** memory requ
1ee4a 69 72 65 64 20 62 79 20 74 68 65 20 73 75 62 2d  ired by the sub-
1ee4b 76 64 62 65 20 61 74 20 72 75 6e 74 69 6d 65 2e  vdbe at runtime.
1ee4c 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
1ee4d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 4d  ointer to the VM
1ee4e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1ee4f 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
1ee50 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67  .*/.case OP_Prog
1ee51 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ram: {        /*
1ee52 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20   jump */.#if 0  
1ee53 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ee54 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ee55 62 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  by */.  int nMem
1ee56 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1ee57 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
1ee58 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f  ory registers fo
1ee59 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
1ee5a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20  .  int nByte;   
1ee5b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1ee5c 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73  tes of runtime s
1ee5d 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f  pace required fo
1ee5e 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
1ee5f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20  .  Mem *pRt;    
1ee60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1ee61 67 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61  gister to alloca
1ee62 74 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65  te runtime space
1ee63 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b   */.  Mem *pMem;
1ee64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ee65 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   Used to iterate
1ee66 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20   through memory 
1ee67 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a  cells */.  Mem *
1ee68 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20  pEnd;           
1ee69 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72     /* Last memor
1ee6a 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72  y cell in new ar
1ee6b 72 61 79 20 2a 2f 0a 20 20 56 64 62 65 46 72 61  ray */.  VdbeFra
1ee6c 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
1ee6d 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61   /* New vdbe fra
1ee6e 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e  me to execute in
1ee6f 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d   */.  SubProgram
1ee70 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a   *pProgram;   /*
1ee71 20 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20   Sub-program to 
1ee72 65 78 65 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69  execute */.  voi
1ee73 64 20 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20  d *t;           
1ee74 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64       /* Token id
1ee75 65 6e 74 69 66 79 69 6e 67 20 74 72 69 67 67 65  entifying trigge
1ee76 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  r */.#endif /* l
1ee77 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ee78 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a  oved into u.by *
1ee79 2f 0a 0a 20 20 75 2e 62 79 2e 70 50 72 6f 67 72  /..  u.by.pProgr
1ee7a 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72  am = pOp->p4.pPr
1ee7b 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 79 2e 70 52  ogram;.  u.by.pR
1ee7c 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  t = &aMem[pOp->p
1ee7d 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  3];.  assert( u.
1ee7e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70  by.pProgram->nOp
1ee7f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  >0 );..  /* If t
1ee80 68 65 20 70 35 20 66 6c 61 67 20 69 73 20 63 6c  he p5 flag is cl
1ee81 65 61 72 2c 20 74 68 65 6e 20 72 65 63 75 72 73  ear, then recurs
1ee82 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ive invocation o
1ee83 66 20 74 72 69 67 67 65 72 73 20 69 73 0a 20 20  f triggers is.  
1ee84 2a 2a 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  ** disabled for 
1ee85 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
1ee86 69 62 69 6c 69 74 79 20 28 70 35 20 69 73 20 73  ibility (p5 is s
1ee87 65 74 20 69 66 20 74 68 69 73 20 73 75 62 2d 70  et if this sub-p
1ee88 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 69 73 20 72  rogram.  ** is r
1ee89 65 61 6c 6c 79 20 61 20 74 72 69 67 67 65 72 2c  eally a trigger,
1ee8a 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e 20 6b   not a foreign k
1ee8b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64 20 74  ey action, and t
1ee8c 68 65 20 66 6c 61 67 20 73 65 74 0a 20 20 2a 2a  he flag set.  **
1ee8d 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20   and cleared by 
1ee8e 74 68 65 20 22 50 52 41 47 4d 41 20 72 65 63 75  the "PRAGMA recu
1ee8f 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 22 20  rsive_triggers" 
1ee90 63 6f 6d 6d 61 6e 64 20 69 73 20 63 6c 65 61 72  command is clear
1ee91 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20  )..  **.  ** It 
1ee92 69 73 20 72 65 63 75 72 73 69 76 65 20 69 6e 76  is recursive inv
1ee93 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 67  ocation of trigg
1ee94 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 4c 20  ers, at the SQL 
1ee95 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 0a 20  level, that is. 
1ee96 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 49 6e   ** disabled. In
1ee97 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 73 69   some cases a si
1ee98 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d 61 79  ngle trigger may
1ee99 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 20 74   generate more t
1ee9a 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53 75 62  han one.  ** Sub
1ee9b 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68 65 20  Program (if the 
1ee9c 74 72 69 67 67 65 72 20 6d 61 79 20 62 65 20 65  trigger may be e
1ee9d 78 65 63 75 74 65 64 20 77 69 74 68 20 6d 6f 72  xecuted with mor
1ee9e 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66 66 65  e than one diffe
1ee9f 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f 4e  rent.  ** ON CON
1eea0 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 29  FLICT algorithm)
1eea1 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72  . SubProgram str
1eea2 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74  uctures associat
1eea3 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 73  ed with a.  ** s
1eea4 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 61 6c  ingle trigger al
1eea5 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  l have the same 
1eea6 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 53 75  value for the Su
1eea7 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a 20  bProgram.token. 
1eea8 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 20 20 2a   ** variable.  *
1eea9 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  /.  if( pOp->p5 
1eeaa 29 7b 0a 20 20 20 20 75 2e 62 79 2e 74 20 3d 20  ){.    u.by.t = 
1eeab 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74  u.by.pProgram->t
1eeac 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 75 2e  oken;.    for(u.
1eead 62 79 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72  by.pFrame=p->pFr
1eeae 61 6d 65 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65  ame; u.by.pFrame
1eeaf 20 26 26 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d   && u.by.pFrame-
1eeb0 3e 74 6f 6b 65 6e 21 3d 75 2e 62 79 2e 74 3b 20  >token!=u.by.t; 
1eeb1 75 2e 62 79 2e 70 46 72 61 6d 65 3d 75 2e 62 79  u.by.pFrame=u.by
1eeb2 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74  .pFrame->pParent
1eeb3 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 79 2e  );.    if( u.by.
1eeb4 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b 3b 0a  pFrame ) break;.
1eeb5 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 6e 46    }..  if( p->nF
1eeb6 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74  rame>=db->aLimit
1eeb7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52  [SQLITE_LIMIT_TR
1eeb8 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29 7b 0a  IGGER_DEPTH] ){.
1eeb9 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1eeba 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
1eebb 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1eebc 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 74 6f  zErrMsg, db, "to
1eebd 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66  o many levels of
1eebe 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69   trigger recursi
1eebf 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b  on");.    break;
1eec0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73  .  }..  /* Regis
1eec1 74 65 72 20 75 2e 62 79 2e 70 52 74 20 69 73 20  ter u.by.pRt is 
1eec2 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
1eec3 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65  e memory require
1eec4 64 20 74 6f 20 73 61 76 65 20 74 68 65 20 73 74  d to save the st
1eec5 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ate.  ** of the 
1eec6 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61 6d 2c  current program,
1eec7 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20   and the memory 
1eec8 72 65 71 75 69 72 65 64 20 61 74 20 72 75 6e 74  required at runt
1eec9 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65 0a 20  ime to execute. 
1eeca 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20   ** the trigger 
1eecb 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 69 73  program. If this
1eecc 20 74 72 69 67 67 65 72 20 68 61 73 20 62 65 65   trigger has bee
1eecd 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65 2c 20  n fired before, 
1eece 74 68 65 6e 20 75 2e 62 79 2e 70 52 74 0a 20 20  then u.by.pRt.  
1eecf 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 61 6c  ** is already al
1eed0 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72 77 69  located. Otherwi
1eed1 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 69  se, it must be i
1eed2 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a  nitialized.  */.
1eed3 20 20 69 66 28 20 28 75 2e 62 79 2e 70 52 74 2d    if( (u.by.pRt-
1eed4 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65  >flags&MEM_Frame
1eed5 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53  )==0 ){.    /* S
1eed6 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20 69  ubProgram.nMem i
1eed7 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
1eed8 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65  ber of memory ce
1eed9 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68 65 0a  lls used by the.
1eeda 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 73      ** program s
1eedb 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72 6f 67  tored in SubProg
1eedc 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65 6c 6c  ram.aOp. As well
1eedd 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65 20 6d   as these, one m
1eede 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63 65 6c  emory.    ** cel
1eedf 6c 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  l is required fo
1eee0 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 75 73  r each cursor us
1eee1 65 64 20 62 79 20 74 68 65 20 70 72 6f 67 72 61  ed by the progra
1eee2 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20 20 20  m. Set local.   
1eee3 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75 2e 62   ** variable u.b
1eee4 79 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61 74 65  y.nMem (and late
1eee5 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e 43 68  r, VdbeFrame.nCh
1eee6 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69 73 20  ildMem) to this 
1eee7 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
1eee8 20 20 75 2e 62 79 2e 6e 4d 65 6d 20 3d 20 75 2e    u.by.nMem = u.
1eee9 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65  by.pProgram->nMe
1eeea 6d 20 2b 20 75 2e 62 79 2e 70 50 72 6f 67 72 61  m + u.by.pProgra
1eeeb 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62  m->nCsr;.    u.b
1eeec 79 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38  y.nByte = ROUND8
1eeed 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d  (sizeof(VdbeFram
1eeee 65 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  e)).            
1eeef 20 20 2b 20 75 2e 62 79 2e 6e 4d 65 6d 20 2a 20    + u.by.nMem * 
1eef0 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20 20 20  sizeof(Mem).    
1eef1 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62 79            + u.by
1eef2 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20  .pProgram->nCsr 
1eef3 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72  * sizeof(VdbeCur
1eef4 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e 62 79  sor *);.    u.by
1eef5 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69 74 65  .pFrame = sqlite
1eef6 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1eef7 2c 20 75 2e 62 79 2e 6e 42 79 74 65 29 3b 0a 20  , u.by.nByte);. 
1eef8 20 20 20 69 66 28 20 21 75 2e 62 79 2e 70 46 72     if( !u.by.pFr
1eef9 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ame ){.      got
1eefa 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
1eefb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1eefc 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 79 2e 70  emRelease(u.by.p
1eefd 52 74 29 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52  Rt);.    u.by.pR
1eefe 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 46  t->flags = MEM_F
1eeff 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 79 2e 70  rame;.    u.by.p
1ef00 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d 20 75  Rt->u.pFrame = u
1ef01 2e 62 79 2e 70 46 72 61 6d 65 3b 0a 0a 20 20 20  .by.pFrame;..   
1ef02 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 76 20   u.by.pFrame->v 
1ef03 3d 20 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46  = p;.    u.by.pF
1ef04 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20  rame->nChildMem 
1ef05 3d 20 75 2e 62 79 2e 6e 4d 65 6d 3b 0a 20 20 20  = u.by.nMem;.   
1ef06 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43   u.by.pFrame->nC
1ef07 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 79 2e 70  hildCsr = u.by.p
1ef08 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20  Program->nCsr;. 
1ef09 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e     u.by.pFrame->
1ef0a 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75 2e 62  pc = pc;.    u.b
1ef0b 79 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 3d  y.pFrame->aMem =
1ef0c 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20 75 2e   p->aMem;.    u.
1ef0d 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20  by.pFrame->nMem 
1ef0e 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 75  = p->nMem;.    u
1ef0f 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 61 70 43 73  .by.pFrame->apCs
1ef10 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a 20 20  r = p->apCsr;.  
1ef11 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e    u.by.pFrame->n
1ef12 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43 75 72  Cursor = p->nCur
1ef13 73 6f 72 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46  sor;.    u.by.pF
1ef14 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e 61  rame->aOp = p->a
1ef15 4f 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72  Op;.    u.by.pFr
1ef16 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e 4f  ame->nOp = p->nO
1ef17 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61  p;.    u.by.pFra
1ef18 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62 79  me->token = u.by
1ef19 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e  .pProgram->token
1ef1a 3b 0a 0a 20 20 20 20 75 2e 62 79 2e 70 45 6e 64  ;..    u.by.pEnd
1ef1b 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d   = &VdbeFrameMem
1ef1c 28 75 2e 62 79 2e 70 46 72 61 6d 65 29 5b 75 2e  (u.by.pFrame)[u.
1ef1d 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  by.pFrame->nChil
1ef1e 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 75  dMem];.    for(u
1ef1f 2e 62 79 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61  .by.pMem=VdbeFra
1ef20 6d 65 4d 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d  meMem(u.by.pFram
1ef21 65 29 3b 20 75 2e 62 79 2e 70 4d 65 6d 21 3d 75  e); u.by.pMem!=u
1ef22 2e 62 79 2e 70 45 6e 64 3b 20 75 2e 62 79 2e 70  .by.pEnd; u.by.p
1ef23 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e  Mem++){.      u.
1ef24 62 79 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d  by.pMem->flags =
1ef25 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
1ef26 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e 64 62 20 3d   u.by.pMem->db =
1ef27 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   db;.    }.  }el
1ef28 73 65 7b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72  se{.    u.by.pFr
1ef29 61 6d 65 20 3d 20 75 2e 62 79 2e 70 52 74 2d 3e  ame = u.by.pRt->
1ef2a 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 61 73  u.pFrame;.    as
1ef2b 73 65 72 74 28 20 75 2e 62 79 2e 70 50 72 6f 67  sert( u.by.pProg
1ef2c 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 79 2e 70  ram->nMem+u.by.p
1ef2d 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75  Program->nCsr==u
1ef2e 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69  .by.pFrame->nChi
1ef2f 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 73 73  ldMem );.    ass
1ef30 65 72 74 28 20 75 2e 62 79 2e 70 50 72 6f 67 72  ert( u.by.pProgr
1ef31 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 79 2e 70  am->nCsr==u.by.p
1ef32 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72  Frame->nChildCsr
1ef33 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1ef34 70 63 3d 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d  pc==u.by.pFrame-
1ef35 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 70 2d  >pc );.  }..  p-
1ef36 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e 62  >nFrame++;.  u.b
1ef37 79 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e  y.pFrame->pParen
1ef38 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20  t = p->pFrame;. 
1ef39 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6c 61   u.by.pFrame->la
1ef3a 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61  stRowid = db->la
1ef3b 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62 79 2e  stRowid;.  u.by.
1ef3c 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20  pFrame->nChange 
1ef3d 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20  = p->nChange;.  
1ef3e 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  p->nChange = 0;.
1ef3f 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e    p->pFrame = u.
1ef40 62 79 2e 70 46 72 61 6d 65 3b 0a 20 20 70 2d 3e  by.pFrame;.  p->
1ef41 61 4d 65 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56  aMem = aMem = &V
1ef42 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79  dbeFrameMem(u.by
1ef43 2e 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20  .pFrame)[-1];.  
1ef44 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e 70  p->nMem = u.by.p
1ef45 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d  Frame->nChildMem
1ef46 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d  ;.  p->nCursor =
1ef47 20 28 75 31 36 29 75 2e 62 79 2e 70 46 72 61 6d   (u16)u.by.pFram
1ef48 65 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20  e->nChildCsr;.  
1ef49 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65  p->apCsr = (Vdbe
1ef4a 43 75 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b  Cursor **)&aMem[
1ef4b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d  p->nMem+1];.  p-
1ef4c 3e 61 4f 70 20 3d 20 61 4f 70 20 3d 20 75 2e 62  >aOp = aOp = u.b
1ef4d 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b  y.pProgram->aOp;
1ef4e 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 79  .  p->nOp = u.by
1ef4f 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a  .pProgram->nOp;.
1ef50 20 20 70 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72    pc = -1;..  br
1ef51 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ef52 65 3a 20 50 61 72 61 6d 20 50 31 20 50 32 20 2a  e: Param P1 P2 *
1ef53 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   * *.**.** This 
1ef54 6f 70 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65  opcode is only e
1ef55 76 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 73  ver present in s
1ef56 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c  ub-programs call
1ef57 65 64 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f  ed via the .** O
1ef58 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75  P_Program instru
1ef59 63 74 69 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61  ction. Copy a va
1ef5a 6c 75 65 20 63 75 72 72 65 6e 74 6c 79 20 73 74  lue currently st
1ef5b 6f 72 65 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79  ored in a memory
1ef5c 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65   .** cell of the
1ef5d 20 63 61 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74   calling (parent
1ef5e 29 20 66 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20  ) frame to cell 
1ef5f 50 32 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  P2 in the curren
1ef60 74 20 66 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64  t frames .** add
1ef61 72 65 73 73 20 73 70 61 63 65 2e 20 54 68 69 73  ress space. This
1ef62 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67   is used by trig
1ef63 67 65 72 20 70 72 6f 67 72 61 6d 73 20 74 6f 20  ger programs to 
1ef64 61 63 63 65 73 73 20 74 68 65 20 6e 65 77 2e 2a  access the new.*
1ef65 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76   .** and old.* v
1ef66 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  alues..**.** The
1ef67 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   address of the 
1ef68 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
1ef69 6e 74 20 66 72 61 6d 65 20 69 73 20 64 65 74 65  nt frame is dete
1ef6a 72 6d 69 6e 65 64 20 62 79 20 61 64 64 69 6e 67  rmined by adding
1ef6b 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
1ef6c 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74   the P1 argument
1ef6d 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
1ef6e 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74   the P1 argument
1ef6f 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69   to the.** calli
1ef70 6e 67 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e  ng OP_Program in
1ef71 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1ef72 73 65 20 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20  se OP_Param: {  
1ef73 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1ef74 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23  -prerelease */.#
1ef75 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ef76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ef77 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 56 64  nto u.bz */.  Vd
1ef78 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1ef79 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e  .  Mem *pIn;.#en
1ef7a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1ef7b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ef7c 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 75 2e 62 7a  o u.bz */.  u.bz
1ef7d 2e 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72  .pFrame = p->pFr
1ef7e 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 49 6e 20  ame;.  u.bz.pIn 
1ef7f 3d 20 26 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e  = &u.bz.pFrame->
1ef80 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75  aMem[pOp->p1 + u
1ef81 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b  .bz.pFrame->aOp[
1ef82 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 5d  u.bz.pFrame->pc]
1ef83 2e 70 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56  .p1];.  sqlite3V
1ef84 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
1ef85 79 28 70 4f 75 74 2c 20 75 2e 62 7a 2e 70 49 6e  y(pOut, u.bz.pIn
1ef86 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20  , MEM_Ephem);.  
1ef87 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66  break;.}..#endif
1ef88 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
1ef89 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
1ef8a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1ef8b 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
1ef8c 4b 45 59 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46  KEY./* Opcode: F
1ef8d 6b 43 6f 75 6e 74 65 72 20 50 31 20 50 32 20 2a  kCounter P1 P2 *
1ef8e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65   * *.**.** Incre
1ef8f 6d 65 6e 74 20 61 20 22 63 6f 6e 73 74 72 61 69  ment a "constrai
1ef90 6e 74 20 63 6f 75 6e 74 65 72 22 20 62 79 20 50  nt counter" by P
1ef91 32 20 28 50 32 20 6d 61 79 20 62 65 20 6e 65 67  2 (P2 may be neg
1ef92 61 74 69 76 65 20 6f 72 20 70 6f 73 69 74 69 76  ative or positiv
1ef93 65 29 2e 0a 2a 2a 20 49 66 20 50 31 20 69 73 20  e)..** If P1 is 
1ef94 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61  non-zero, the da
1ef95 74 61 62 61 73 65 20 63 6f 6e 73 74 72 61 69 6e  tabase constrain
1ef96 74 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63  t counter is inc
1ef97 72 65 6d 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65  remented .** (de
1ef98 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b  ferred foreign k
1ef99 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e  ey constraints).
1ef9a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50   Otherwise, if P
1ef9b 31 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a  1 is zero, the .
1ef9c 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 75  ** statement cou
1ef9d 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  nter is incremen
1ef9e 74 65 64 20 28 69 6d 6d 65 64 69 61 74 65 20 66  ted (immediate f
1ef9f 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
1efa0 72 61 69 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65  raints)..*/.case
1efa1 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b   OP_FkCounter: {
1efa2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29  .  if( pOp->p1 )
1efa3 7b 0a 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72  {.    db->nDefer
1efa4 72 65 64 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e  redCons += pOp->
1efa5 70 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  p2;.  }else{.   
1efa6 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e   p->nFkConstrain
1efa7 74 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  t += pOp->p2;.  
1efa8 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1efa9 20 4f 70 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72   Opcode: FkIfZer
1efaa 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  o P1 P2 * * *.**
1efab 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1efac 74 65 73 74 73 20 69 66 20 61 20 66 6f 72 65 69  tests if a forei
1efad 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1efae 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 63 75 72  t-counter is cur
1efaf 72 65 6e 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20  rently zero..** 
1efb0 49 66 20 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69  If so, jump to i
1efb1 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f  nstruction P2. O
1efb2 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74  therwise, fall t
1efb3 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1efb4 78 74 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69  xt .** instructi
1efb5 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20  on..**.** If P1 
1efb6 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1efb7 6e 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  n the jump is ta
1efb8 6b 65 6e 20 69 66 20 74 68 65 20 64 61 74 61 62  ken if the datab
1efb9 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63  ase constraint-c
1efba 6f 75 6e 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72  ounter.** is zer
1efbb 6f 20 28 74 68 65 20 6f 6e 65 20 74 68 61 74 20  o (the one that 
1efbc 63 6f 75 6e 74 73 20 64 65 66 65 72 72 65 64 20  counts deferred 
1efbd 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
1efbe 74 69 6f 6e 73 29 2e 20 49 66 20 50 31 20 69 73  tions). If P1 is
1efbf 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75  .** zero, the ju
1efc0 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74  mp is taken if t
1efc1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e  he statement con
1efc2 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20  straint-counter 
1efc3 69 73 20 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65  is zero.** (imme
1efc4 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
1efc5 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  y constraint vio
1efc6 6c 61 74 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73  lations)..*/.cas
1efc7 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b  e OP_FkIfZero: {
1efc8 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1efc9 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70   */.  if( pOp->p
1efca 31 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  1 ){.    if( db-
1efcb 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d  >nDeferredCons==
1efcc 30 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  0 ) pc = pOp->p2
1efcd 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  -1;.  }else{.   
1efce 20 69 66 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74   if( p->nFkConst
1efcf 72 61 69 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20  raint==0 ) pc = 
1efd0 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20  pOp->p2-1;.  }. 
1efd1 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1efd2 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49   /* #ifndef SQLI
1efd3 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
1efd4 4b 45 59 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  KEY */..#ifndef 
1efd5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1efd6 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63  INCREMENT./* Opc
1efd7 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50  ode: MemMax P1 P
1efd8 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31  2 * * *.**.** P1
1efd9 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20 69   is a register i
1efda 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65  n the root frame
1efdb 20 6f 66 20 74 68 69 73 20 56 4d 20 28 74 68 65   of this VM (the
1efdc 20 72 6f 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a   root frame is.*
1efdd 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  * different from
1efde 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61   the current fra
1efdf 6d 65 20 69 66 20 74 68 69 73 20 69 6e 73 74 72  me if this instr
1efe0 75 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  uction is being 
1efe1 65 78 65 63 75 74 65 64 0a 2a 2a 20 77 69 74 68  executed.** with
1efe2 69 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 61 6d  in a sub-program
1efe3 29 2e 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ). Set the value
1efe4 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1efe5 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  to the maximum o
1efe6 66 20 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 6e  f .** its curren
1efe7 74 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20  t value and the 
1efe8 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1efe9 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  r P2..**.** This
1efea 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 72   instruction thr
1efeb 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20  ows an error if 
1efec 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1efed 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79  is not initially
1efee 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a  .** an integer..
1efef 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61  */.case OP_MemMa
1eff0 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69  x: {        /* i
1eff1 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  n2 */.#if 0  /* 
1eff2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1eff3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20  moved into u.ca 
1eff4 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a  */.  Mem *pIn1;.
1eff5 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72    VdbeFrame *pFr
1eff6 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  ame;.#endif /* l
1eff7 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1eff8 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a  oved into u.ca *
1eff9 2f 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d  /.  if( p->pFram
1effa 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63  e ){.    for(u.c
1effb 61 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61  a.pFrame=p->pFra
1effc 6d 65 3b 20 75 2e 63 61 2e 70 46 72 61 6d 65 2d  me; u.ca.pFrame-
1effd 3e 70 50 61 72 65 6e 74 3b 20 75 2e 63 61 2e 70  >pParent; u.ca.p
1effe 46 72 61 6d 65 3d 75 2e 63 61 2e 70 46 72 61 6d  Frame=u.ca.pFram
1efff 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  e->pParent);.   
1f000 20 75 2e 63 61 2e 70 49 6e 31 20 3d 20 26 75 2e   u.ca.pIn1 = &u.
1f001 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b  ca.pFrame->aMem[
1f002 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73  pOp->p1];.  }els
1f003 65 7b 0a 20 20 20 20 75 2e 63 61 2e 70 49 6e 31  e{.    u.ca.pIn1
1f004 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
1f005 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  ];.  }.  sqlite3
1f006 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1f007 79 28 75 2e 63 61 2e 70 49 6e 31 29 3b 0a 20 20  y(u.ca.pIn1);.  
1f008 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn2 = &aMem[pOp
1f009 2d 3e 70 32 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ->p2];.  sqlite3
1f00a 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1f00b 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20 75  y(pIn2);.  if( u
1f00c 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49  .ca.pIn1->u.i<pI
1f00d 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e  n2->u.i){.    u.
1f00e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70  ca.pIn1->u.i = p
1f00f 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20  In2->u.i;.  }.  
1f010 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1f011 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
1f012 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a  UTOINCREMENT */.
1f013 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f  ./* Opcode: IfPo
1f014 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  s P1 P2 * * *.**
1f015 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1f016 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20   of register P1 
1f017 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c  is 1 or greater,
1f018 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a   jump to P2..**.
1f019 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  ** It is illegal
1f01a 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73   to use this ins
1f01b 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65  truction on a re
1f01c 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73  gister that does
1f01d 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  .** not contain 
1f01e 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20  an integer.  An 
1f01f 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1f020 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79  will result if y
1f021 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20  ou try..*/.case 
1f022 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20  OP_IfPos: {     
1f023 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1f024 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1f025 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1f026 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1f027 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  s&MEM_Int );.  i
1f028 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29  f( pIn1->u.i>0 )
1f029 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1f02a 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1f02b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1f02c 64 65 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20  de: IfNeg P1 P2 
1f02d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74  * * *.**.** If t
1f02e 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69  he value of regi
1f02f 73 74 65 72 20 50 31 20 69 73 20 6c 65 73 73 20  ster P1 is less 
1f030 74 68 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20  than zero, jump 
1f031 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74  to P2. .**.** It
1f032 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75   is illegal to u
1f033 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  se this instruct
1f034 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65  ion on a registe
1f035 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e  r that does.** n
1f036 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e  ot contain an in
1f037 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72  teger.  An asser
1f038 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20  tion fault will 
1f039 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72  result if you tr
1f03a 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66  y..*/.case OP_If
1f03b 4e 65 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  Neg: {        /*
1f03c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1f03d 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1f03e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1f03f 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d   pIn1->flags&MEM
1f040 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49  _Int );.  if( pI
1f041 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20  n1->u.i<0 ){.   
1f042 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1f043 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1f044 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1f045 66 5a 65 72 6f 20 50 31 20 50 32 20 50 33 20 2a  fZero P1 P2 P3 *
1f046 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67   *.**.** The reg
1f047 69 73 74 65 72 20 50 31 20 6d 75 73 74 20 63 6f  ister P1 must co
1f048 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72  ntain an integer
1f049 2e 20 20 41 64 64 20 6c 69 74 65 72 61 6c 20 50  .  Add literal P
1f04a 33 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75  3 to the.** valu
1f04b 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1f04c 2e 20 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  .  If the result
1f04d 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a   is exactly 0, j
1f04e 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a  ump to P2. .**.*
1f04f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
1f050 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
1f051 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67  ruction on a reg
1f052 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a  ister that does.
1f053 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
1f054 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
1f055 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
1f056 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
1f057 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  u try..*/.case O
1f058 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20  P_IfZero: {     
1f059 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1f05a 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1f05b 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1f05c 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1f05d 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70  s&MEM_Int );.  p
1f05e 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d  In1->u.i += pOp-
1f05f 3e 70 33 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  >p3;.  if( pIn1-
1f060 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >u.i==0 ){.     
1f061 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1f062 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1f063 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67  ../* Opcode: Agg
1f064 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 34 20  Step * P2 P3 P4 
1f065 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65  P5.**.** Execute
1f066 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69   the step functi
1f067 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67  on for an aggreg
1f068 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e  ate.  The.** fun
1f069 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67  ction has P5 arg
1f06a 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20  uments.   P4 is 
1f06b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1f06c 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75   FuncDef.** stru
1f06d 63 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69  cture that speci
1f06e 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f  fies the functio
1f06f 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74 65 72  n.  Use register
1f070 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20 61 63  .** P3 as the ac
1f071 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  cumulator..**.**
1f072 20 54 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74   The P5 argument
1f073 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
1f074 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64   register P2 and
1f075 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f   its.** successo
1f076 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  rs..*/.case OP_A
1f077 67 67 53 74 65 70 3a 20 7b 0a 23 69 66 20 30 20  ggStep: {.#if 0 
1f078 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f079 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f07a 2e 63 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a  .cb */.  int n;.
1f07b 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a    int i;.  Mem *
1f07c 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65  pMem;.  Mem *pRe
1f07d 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c;.  sqlite3_con
1f07e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69  text ctx;.  sqli
1f07f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
1f080 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  l;.#endif /* loc
1f081 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f082 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a  ed into u.cb */.
1f083 0a 20 20 75 2e 63 62 2e 6e 20 3d 20 70 4f 70 2d  .  u.cb.n = pOp-
1f084 3e 70 35 3b 0a 20 20 61 73 73 65 72 74 28 20 75  >p5;.  assert( u
1f085 2e 63 62 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e  .cb.n>=0 );.  u.
1f086 63 62 2e 70 52 65 63 20 3d 20 26 61 4d 65 6d 5b  cb.pRec = &aMem[
1f087 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 62  pOp->p2];.  u.cb
1f088 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72  .apVal = p->apAr
1f089 67 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  g;.  assert( u.c
1f08a 62 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 63 62 2e  b.apVal || u.cb.
1f08b 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e  n==0 );.  for(u.
1f08c 63 62 2e 69 3d 30 3b 20 75 2e 63 62 2e 69 3c 75  cb.i=0; u.cb.i<u
1f08d 2e 63 62 2e 6e 3b 20 75 2e 63 62 2e 69 2b 2b 2c  .cb.n; u.cb.i++,
1f08e 20 75 2e 63 62 2e 70 52 65 63 2b 2b 29 7b 0a 20   u.cb.pRec++){. 
1f08f 20 20 20 75 2e 63 62 2e 61 70 56 61 6c 5b 75 2e     u.cb.apVal[u.
1f090 63 62 2e 69 5d 20 3d 20 75 2e 63 62 2e 70 52 65  cb.i] = u.cb.pRe
1f091 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  c;.    sqlite3Vd
1f092 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75  beMemStoreType(u
1f093 2e 63 62 2e 70 52 65 63 29 3b 0a 20 20 7d 0a 20  .cb.pRec);.  }. 
1f094 20 75 2e 63 62 2e 63 74 78 2e 70 46 75 6e 63 20   u.cb.ctx.pFunc 
1f095 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
1f096 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1f097 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1f098 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e  =p->nMem );.  u.
1f099 63 62 2e 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e  cb.ctx.pMem = u.
1f09a 63 62 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b  cb.pMem = &aMem[
1f09b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 62  pOp->p3];.  u.cb
1f09c 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e  .pMem->n++;.  u.
1f09d 63 62 2e 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d  cb.ctx.s.flags =
1f09e 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63   MEM_Null;.  u.c
1f09f 62 2e 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20  b.ctx.s.z = 0;. 
1f0a0 20 75 2e 63 62 2e 63 74 78 2e 73 2e 7a 4d 61 6c   u.cb.ctx.s.zMal
1f0a1 6c 6f 63 20 3d 20 30 3b 0a 20 20 75 2e 63 62 2e  loc = 0;.  u.cb.
1f0a2 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a  ctx.s.xDel = 0;.
1f0a3 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e 64 62 20    u.cb.ctx.s.db 
1f0a4 3d 20 64 62 3b 0a 20 20 75 2e 63 62 2e 63 74 78  = db;.  u.cb.ctx
1f0a5 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  .isError = 0;.  
1f0a6 75 2e 63 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d  u.cb.ctx.pColl =
1f0a7 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 62 2e 63   0;.  if( u.cb.c
1f0a8 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20  tx.pFunc->flags 
1f0a9 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
1f0aa 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73  EDCOLL ){.    as
1f0ab 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70  sert( pOp>p->aOp
1f0ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1f0ad 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d  pOp[-1].p4type==
1f0ae 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20  P4_COLLSEQ );.  
1f0af 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
1f0b0 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c  ].opcode==OP_Col
1f0b1 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 63 62  lSeq );.    u.cb
1f0b2 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70  .ctx.pColl = pOp
1f0b3 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20  [-1].p4.pColl;. 
1f0b4 20 7d 0a 20 20 28 75 2e 63 62 2e 63 74 78 2e 70   }.  (u.cb.ctx.p
1f0b5 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 26 75 2e  Func->xStep)(&u.
1f0b6 63 62 2e 63 74 78 2c 20 75 2e 63 62 2e 6e 2c 20  cb.ctx, u.cb.n, 
1f0b7 75 2e 63 62 2e 61 70 56 61 6c 29 3b 0a 20 20 69  u.cb.apVal);.  i
1f0b8 66 28 20 75 2e 63 62 2e 63 74 78 2e 69 73 45 72  f( u.cb.ctx.isEr
1f0b9 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ror ){.    sqlit
1f0ba 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1f0bb 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1f0bc 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ", sqlite3_value
1f0bd 5f 74 65 78 74 28 26 75 2e 63 62 2e 63 74 78 2e  _text(&u.cb.ctx.
1f0be 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e  s));.    rc = u.
1f0bf 63 62 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a  cb.ctx.isError;.
1f0c0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1f0c1 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 63  eMemRelease(&u.c
1f0c2 62 2e 63 74 78 2e 73 29 3b 0a 20 20 62 72 65 61  b.ctx.s);.  brea
1f0c3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1f0c4 20 41 67 67 46 69 6e 61 6c 20 50 31 20 50 32 20   AggFinal P1 P2 
1f0c5 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65  * P4 *.**.** Exe
1f0c6 63 75 74 65 20 74 68 65 20 66 69 6e 61 6c 69 7a  cute the finaliz
1f0c7 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  er function for 
1f0c8 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20 20 50  an aggregate.  P
1f0c9 31 20 69 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f  1 is.** the memo
1f0ca 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74  ry location that
1f0cb 20 69 73 20 74 68 65 20 61 63 63 75 6d 75 6c 61   is the accumula
1f0cc 74 6f 72 20 66 6f 72 20 74 68 65 20 61 67 67 72  tor for the aggr
1f0cd 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  egate..**.** P2 
1f0ce 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1f0cf 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20   arguments that 
1f0d0 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f  the step functio
1f0d1 6e 20 74 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50  n takes and.** P
1f0d2 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1f0d3 6f 20 74 68 65 20 46 75 6e 63 44 65 66 20 66 6f  o the FuncDef fo
1f0d4 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  r this function.
1f0d5 20 20 54 68 65 20 50 32 0a 2a 2a 20 61 72 67 75    The P2.** argu
1f0d6 6d 65 6e 74 20 69 73 20 6e 6f 74 20 75 73 65 64  ment is not used
1f0d7 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 2e   by this opcode.
1f0d8 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 74 68 65    It is only the
1f0d9 72 65 20 74 6f 20 64 69 73 61 6d 62 69 67 75 61  re to disambigua
1f0da 74 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  te.** functions 
1f0db 74 68 61 74 20 63 61 6e 20 74 61 6b 65 20 76 61  that can take va
1f0dc 72 79 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66  rying numbers of
1f0dd 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65   arguments.  The
1f0de 0a 2a 2a 20 50 34 20 61 72 67 75 6d 65 6e 74 20  .** P4 argument 
1f0df 69 73 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66  is only needed f
1f0e0 6f 72 20 74 68 65 20 64 65 67 65 6e 65 72 61 74  or the degenerat
1f0e1 65 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20  e case where.** 
1f0e2 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f  the step functio
1f0e3 6e 20 77 61 73 20 6e 6f 74 20 70 72 65 76 69 6f  n was not previo
1f0e4 75 73 6c 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  usly called..*/.
1f0e5 63 61 73 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c  case OP_AggFinal
1f0e6 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1f0e7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1f0e8 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f  ved into u.cc */
1f0e9 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65  .  Mem *pMem;.#e
1f0ea 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f0eb 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f0ec 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 61 73 73  to u.cc */.  ass
1f0ed 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26  ert( pOp->p1>0 &
1f0ee 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d  & pOp->p1<=p->nM
1f0ef 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 70 4d 65  em );.  u.cc.pMe
1f0f0 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  m = &aMem[pOp->p
1f0f1 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75  1];.  assert( (u
1f0f2 2e 63 63 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  .cc.pMem->flags 
1f0f3 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d  & ~(MEM_Null|MEM
1f0f4 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72  _Agg))==0 );.  r
1f0f5 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1f0f6 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 63 2e  emFinalize(u.cc.
1f0f7 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46  pMem, pOp->p4.pF
1f0f8 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29  unc);.  if( rc )
1f0f9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1f0fa 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1f0fb 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1f0fc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1f0fd 28 75 2e 63 63 2e 70 4d 65 6d 29 29 3b 0a 20 20  (u.cc.pMem));.  
1f0fe 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  }.  sqlite3VdbeC
1f0ff 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e  hangeEncoding(u.
1f100 63 63 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e  cc.pMem, encodin
1f101 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  g);.  UPDATE_MAX
1f102 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 63 2e 70  _BLOBSIZE(u.cc.p
1f103 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Mem);.  if( sqli
1f104 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1f105 28 75 2e 63 63 2e 70 4d 65 6d 29 20 29 7b 0a 20  (u.cc.pMem) ){. 
1f106 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1f107 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1f108 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1f109 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1f10a 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
1f10b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
1f10c 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61  H)./* Opcode: Va
1f10d 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a  cuum * * * * *.*
1f10e 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20  *.** Vacuum the 
1f10f 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e  entire database.
1f110 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69    This opcode wi
1f111 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76  ll cause other v
1f112 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
1f113 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  es to be created
1f114 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61   and run.  It ma
1f115 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  y not be called 
1f116 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61  from within.** a
1f117 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
1f118 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a  .case OP_Vacuum:
1f119 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   {.  if( sqlite3
1f11a 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1f11b 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f11c 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20  o_misuse; .  rc 
1f11d 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  = sqlite3RunVacu
1f11e 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  um(&p->zErrMsg, 
1f11f 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  db);.  if( sqlit
1f120 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1f121 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f122 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65  to_misuse;.  bre
1f123 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
1f124 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1f125 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1f126 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  M)./* Opcode: In
1f127 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a  crVacuum P1 P2 *
1f128 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f   * *.**.** Perfo
1f129 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70  rm a single step
1f12a 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   of the incremen
1f12b 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65  tal vacuum proce
1f12c 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50  dure on.** the P
1f12d 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  1 database. If t
1f12e 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69  he vacuum has fi
1f12f 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20  nished, jump to 
1f130 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50  instruction.** P
1f131 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61  2. Otherwise, fa
1f132 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1f133 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1f134 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  on..*/.case OP_I
1f135 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20  ncrVacuum: {    
1f136 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1f137 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f138 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f139 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 42 74  nto u.cd */.  Bt
1f13a 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66  ree *pBt;.#endif
1f13b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f13c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f13d 2e 63 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .cd */..  assert
1f13e 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1f13f 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1f140 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1f141 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1f142 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1f143 0a 20 20 75 2e 63 64 2e 70 42 74 20 3d 20 64 62  .  u.cd.pBt = db
1f144 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1f145 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
1f146 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
1f147 6d 28 75 2e 63 64 2e 70 42 74 29 3b 0a 20 20 69  m(u.cd.pBt);.  i
1f148 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
1f149 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  NE ){.    pc = p
1f14a 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1f14b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f14c 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1f14d 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65  endif../* Opcode
1f14e 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20  : Expire P1 * * 
1f14f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20  * *.**.** Cause 
1f150 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  precompiled stat
1f151 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65  ements to become
1f152 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70   expired. An exp
1f153 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ired statement.*
1f154 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20  * fails with an 
1f155 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51  error code of SQ
1f156 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69  LITE_SCHEMA if i
1f157 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74  t is ever execut
1f158 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69  ed .** (via sqli
1f159 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20  te3_step())..** 
1f15a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20  .** If P1 is 0, 
1f15b 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61  then all SQL sta
1f15c 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65  tements become e
1f15d 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73  xpired. If P1 is
1f15e 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68   non-zero,.** th
1f15f 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72  en only the curr
1f160 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20  ently executing 
1f161 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66  statement is aff
1f162 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20  ected. .*/.case 
1f163 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69  OP_Expire: {.  i
1f164 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  f( !pOp->p1 ){. 
1f165 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1f166 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1f167 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b  ts(db);.  }else{
1f168 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
1f169 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 1;.  }.  break
1f16a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1f16b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1f16c 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  CACHE./* Opcode:
1f16d 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32   TableLock P1 P2
1f16e 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f   P3 P4 *.**.** O
1f16f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20  btain a lock on 
1f170 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
1f171 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63  le. This instruc
1f172 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65  tion is only use
1f173 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68  d when.** the sh
1f174 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75  ared-cache featu
1f175 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a  re is enabled. .
1f176 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1f177 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1f178 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33  abase in sqlite3
1f179 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61  .aDb[] of the da
1f17a 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69  tabase.** on whi
1f17b 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  ch the lock is a
1f17c 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64  cquired.  A read
1f17d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
1f17e 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20   if P3==0 or.** 
1f17f 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20  a write lock if 
1f180 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  P3==1..**.** P2 
1f181 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f  contains the roo
1f182 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  t-page of the ta
1f183 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a  ble to lock..**.
1f184 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61  ** P4 contains a
1f185 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1f186 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1f187 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20  e being locked. 
1f188 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  This is only.** 
1f189 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
1f18a 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1f18b 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61  e if the lock ca
1f18c 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
1f18d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62  ..*/.case OP_Tab
1f18e 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69  leLock: {.  u8 i
1f18f 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38  sWriteLock = (u8
1f190 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20  )pOp->p3;.  if( 
1f191 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30  isWriteLock || 0
1f192 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  ==(db->flags&SQL
1f193 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
1f194 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  ted) ){.    int 
1f195 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20  p1 = pOp->p1; . 
1f196 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30     assert( p1>=0
1f197 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29   && p1<db->nDb )
1f198 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1f199 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1f19a 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20  <<p1))!=0 );.   
1f19b 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65   assert( isWrite
1f19c 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69  Lock==0 || isWri
1f19d 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20  teLock==1 );.   
1f19e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1f19f 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e  eeLockTable(db->
1f1a0 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70  aDb[p1].pBt, pOp
1f1a1 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63  ->p2, isWriteLoc
1f1a2 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26  k);.    if( (rc&
1f1a3 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f  0xFF)==SQLITE_LO
1f1a4 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f  CKED ){.      co
1f1a5 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f  nst char *z = pO
1f1a6 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73  p->p4.z;.      s
1f1a7 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1f1a8 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1f1a9 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65   "database table
1f1aa 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
1f1ab 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   z);.    }.  }. 
1f1ac 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f1ad 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f1ae 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
1f1af 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f1b0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f1b1 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65  E./* Opcode: VBe
1f1b2 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  gin * * * P4 *.*
1f1b3 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61  *.** P4 may be a
1f1b4 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
1f1b5 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1f1b6 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61  cture. If so, ca
1f1b7 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69  ll the .** xBegi
1f1b8 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61  n method for tha
1f1b9 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  t table..**.** A
1f1ba 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20  lso, whether or 
1f1bb 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63  not P4 is set, c
1f1bc 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69  heck that this i
1f1bd 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c  s not being call
1f1be 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69  ed from.** withi
1f1bf 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  n a callback to 
1f1c0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1f1c1 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20  xSync() method. 
1f1c2 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72  If it is, the er
1f1c3 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c  ror.** code will
1f1c4 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
1f1c5 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73  E_LOCKED..*/.cas
1f1c6 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23  e OP_VBegin: {.#
1f1c7 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f1c8 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f1c9 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 56 54  nto u.ce */.  VT
1f1ca 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e  able *pVTab;.#en
1f1cb 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1f1cc 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f1cd 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 75 2e 63 65  o u.ce */.  u.ce
1f1ce 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34  .pVTab = pOp->p4
1f1cf 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73  .pVtab;.  rc = s
1f1d0 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
1f1d1 64 62 2c 20 75 2e 63 65 2e 70 56 54 61 62 29 3b  db, u.ce.pVTab);
1f1d2 0a 20 20 69 66 28 20 75 2e 63 65 2e 70 56 54 61  .  if( u.ce.pVTa
1f1d3 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1f1d4 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1f1d5 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1f1d6 45 72 72 4d 73 67 20 3d 20 75 2e 63 65 2e 70 56  ErrMsg = u.ce.pV
1f1d7 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1f1d8 4d 73 67 3b 0a 20 20 20 20 75 2e 63 65 2e 70 56  Msg;.    u.ce.pV
1f1d9 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1f1da 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  Msg = 0;.  }.  b
1f1db 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1f1dc 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1f1dd 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1f1de 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f1df 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f1e0 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61  /* Opcode: VCrea
1f1e1 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  te P1 * * P4 *.*
1f1e2 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e  *.** P4 is the n
1f1e3 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ame of a virtual
1f1e4 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61   table in databa
1f1e5 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20  se P1. Call the 
1f1e6 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
1f1e7 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65  * for that table
1f1e8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72  ..*/.case OP_VCr
1f1e9 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73  eate: {.  rc = s
1f1ea 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
1f1eb 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  eate(db, pOp->p1
1f1ec 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d  , pOp->p4.z, &p-
1f1ed 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65  >zErrMsg);.  bre
1f1ee 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1f1ef 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f1f0 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1f1f1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f1f2 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1f1f3 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f   Opcode: VDestro
1f1f4 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  y P1 * * P4 *.**
1f1f5 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61  .** P4 is the na
1f1f6 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20  me of a virtual 
1f1f7 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  table in databas
1f1f8 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20  e P1.  Call the 
1f1f9 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a  xDestroy method.
1f1fa 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  ** of that table
1f1fb 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65  ..*/.case OP_VDe
1f1fc 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e  stroy: {.  p->in
1f1fd 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a  VtabMethod = 2;.
1f1fe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1f1ff 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62  abCallDestroy(db
1f200 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1f201 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  p4.z);.  p->inVt
1f202 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1f203 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1f204 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1f205 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1f206 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f207 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f208 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65  ./* Opcode: VOpe
1f209 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  n P1 * * P4 *.**
1f20a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1f20b 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
1f20c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
1f20d 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
1f20e 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20  tructure..** P1 
1f20f 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62  is a cursor numb
1f210 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  er.  This opcode
1f211 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20   opens a cursor 
1f212 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  to the virtual.*
1f213 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
1f214 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69  es that cursor i
1f215 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P1..*/.case OP
1f216 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20  _VOpen: {.#if 0 
1f217 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f218 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f219 2e 63 66 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .cf */.  VdbeCur
1f21a 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c  sor *pCur;.  sql
1f21b 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1f21c 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20   *pVtabCursor;. 
1f21d 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1f21e 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vtab;.  sqlite3_
1f21f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1f220 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f221 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f222 20 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 0a 20   into u.cf */.. 
1f223 20 75 2e 63 66 2e 70 43 75 72 20 3d 20 30 3b 0a   u.cf.pCur = 0;.
1f224 20 20 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73    u.cf.pVtabCurs
1f225 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 66 2e 70  or = 0;.  u.cf.p
1f226 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Vtab = pOp->p4.p
1f227 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75  Vtab->pVtab;.  u
1f228 2e 63 66 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73  .cf.pModule = (s
1f229 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29  qlite3_module *)
1f22a 75 2e 63 66 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.cf.pVtab->pMod
1f22b 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e  ule;.  assert(u.
1f22c 63 66 2e 70 56 74 61 62 20 26 26 20 75 2e 63 66  cf.pVtab && u.cf
1f22d 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28  .pModule);.  if(
1f22e 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1f22f 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1f230 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1f231 3b 0a 20 20 72 63 20 3d 20 75 2e 63 66 2e 70 4d  ;.  rc = u.cf.pM
1f232 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63  odule->xOpen(u.c
1f233 66 2e 70 56 74 61 62 2c 20 26 75 2e 63 66 2e 70  f.pVtab, &u.cf.p
1f234 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73  VtabCursor);.  s
1f235 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f236 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1f237 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1f238 66 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  f.pVtab->zErrMsg
1f239 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e  ;.  u.cf.pVtab->
1f23a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69  zErrMsg = 0;.  i
1f23b 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1f23c 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1f23d 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f23e 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  e;.  if( SQLITE_
1f23f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a  OK==rc ){.    /*
1f240 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69   Initialize sqli
1f241 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1f242 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20  base class */.  
1f243 20 20 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73    u.cf.pVtabCurs
1f244 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 66  or->pVtab = u.cf
1f245 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20  .pVtab;..    /* 
1f246 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20  Initialise vdbe 
1f247 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  cursor object */
1f248 0a 20 20 20 20 75 2e 63 66 2e 70 43 75 72 20 3d  .    u.cf.pCur =
1f249 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28   allocateCursor(
1f24a 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d  p, pOp->p1, 0, -
1f24b 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75  1, 0);.    if( u
1f24c 2e 63 66 2e 70 43 75 72 20 29 7b 0a 20 20 20 20  .cf.pCur ){.    
1f24d 20 20 75 2e 63 66 2e 70 43 75 72 2d 3e 70 56 74    u.cf.pCur->pVt
1f24e 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 66 2e  abCursor = u.cf.
1f24f 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20  pVtabCursor;.   
1f250 20 20 20 75 2e 63 66 2e 70 43 75 72 2d 3e 70 4d     u.cf.pCur->pM
1f251 6f 64 75 6c 65 20 3d 20 75 2e 63 66 2e 70 56 74  odule = u.cf.pVt
1f252 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
1f253 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65  >pModule;.    }e
1f254 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  lse{.      db->m
1f255 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1f256 0a 20 20 20 20 20 20 75 2e 63 66 2e 70 4d 6f 64  .      u.cf.pMod
1f257 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 66  ule->xClose(u.cf
1f258 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  .pVtabCursor);. 
1f259 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1f25a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1f25b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f25c 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1f25d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f25e 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1f25f 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50  pcode: VFilter P
1f260 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1f261 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1f262 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56  r opened using V
1f263 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20  Open.  P2 is an 
1f264 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
1f265 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c  to if.** the fil
1f266 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74  tered result set
1f267 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a   is empty..**.**
1f268 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55   P4 is either NU
1f269 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74  LL or a string t
1f26a 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
1f26b 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
1f26c 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  dex.** method of
1f26d 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68   the module.  Th
1f26e 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  e interpretation
1f26f 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e   of the P4 strin
1f270 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20  g is left.** to 
1f271 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  the module imple
1f272 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
1f273 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1f274 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72  okes the xFilter
1f275 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
1f276 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65  irtual table spe
1f277 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e  cified.** by P1.
1f278 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75    The integer qu
1f279 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74  ery plan paramet
1f27a 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73  er to xFilter is
1f27b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1f27c 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73  ter.** P3. Regis
1f27d 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20  ter P3+1 stores 
1f27e 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74  the argc paramet
1f27f 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20  er to be passed 
1f280 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65  to the.** xFilte
1f281 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74  r method. Regist
1f282 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61  ers P3+2..P3+1+a
1f283 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63  rgc are the argc
1f284 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70  .** additional p
1f285 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20  arameters which 
1f286 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  are passed to.**
1f287 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76   xFilter as argv
1f288 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20  . Register P3+2 
1f289 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20  becomes argv[0] 
1f28a 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78  when passed to x
1f28b 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  Filter..**.** A 
1f28c 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20  jump is made to 
1f28d 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  P2 if the result
1f28e 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65   set after filte
1f28f 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d  ring would be em
1f290 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  pty..*/.case OP_
1f291 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20  VFilter: {   /* 
1f292 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1f293 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1f294 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1f295 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b  g */.  int nArg;
1f296 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20  .  int iQuery;. 
1f297 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1f298 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1f299 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20    Mem *pQuery;. 
1f29a 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73   Mem *pArgc;.  s
1f29b 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
1f29c 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
1f29d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
1f29e 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75  *pVtab;.  VdbeCu
1f29f 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e  rsor *pCur;.  in
1f2a0 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  t res;.  int i;.
1f2a1 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23    Mem **apArg;.#
1f2a2 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1f2a3 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f2a4 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75  nto u.cg */..  u
1f2a5 2e 63 67 2e 70 51 75 65 72 79 20 3d 20 26 61 4d  .cg.pQuery = &aM
1f2a6 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75  em[pOp->p3];.  u
1f2a7 2e 63 67 2e 70 41 72 67 63 20 3d 20 26 75 2e 63  .cg.pArgc = &u.c
1f2a8 67 2e 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75  g.pQuery[1];.  u
1f2a9 2e 63 67 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70  .cg.pCur = p->ap
1f2aa 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1f2ab 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1f2ac 4f 70 2d 3e 70 33 2c 20 75 2e 63 67 2e 70 51 75  Op->p3, u.cg.pQu
1f2ad 65 72 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ery);.  assert( 
1f2ae 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61 62  u.cg.pCur->pVtab
1f2af 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 63 67  Cursor );.  u.cg
1f2b0 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75  .pVtabCursor = u
1f2b1 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .cg.pCur->pVtabC
1f2b2 75 72 73 6f 72 3b 0a 20 20 75 2e 63 67 2e 70 56  ursor;.  u.cg.pV
1f2b3 74 61 62 20 3d 20 75 2e 63 67 2e 70 56 74 61 62  tab = u.cg.pVtab
1f2b4 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20  Cursor->pVtab;. 
1f2b5 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.cg.pModule = 
1f2b6 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.cg.pVtab->pMod
1f2b7 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20  ule;..  /* Grab 
1f2b8 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72  the index number
1f2b9 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65   and argc parame
1f2ba 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  ters */.  assert
1f2bb 28 20 28 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e  ( (u.cg.pQuery->
1f2bc 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d  flags&MEM_Int)!=
1f2bd 30 20 26 26 20 75 2e 63 67 2e 70 41 72 67 63 2d  0 && u.cg.pArgc-
1f2be 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20  >flags==MEM_Int 
1f2bf 29 3b 0a 20 20 75 2e 63 67 2e 6e 41 72 67 20 3d  );.  u.cg.nArg =
1f2c0 20 28 69 6e 74 29 75 2e 63 67 2e 70 41 72 67 63   (int)u.cg.pArgc
1f2c1 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 67 2e 69 51  ->u.i;.  u.cg.iQ
1f2c2 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e 63 67  uery = (int)u.cg
1f2c3 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20  .pQuery->u.i;.. 
1f2c4 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78   /* Invoke the x
1f2c5 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f  Filter method */
1f2c6 0a 20 20 7b 0a 20 20 20 20 75 2e 63 67 2e 72 65  .  {.    u.cg.re
1f2c7 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 63 67 2e  s = 0;.    u.cg.
1f2c8 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67  apArg = p->apArg
1f2c9 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 67 2e 69  ;.    for(u.cg.i
1f2ca 20 3d 20 30 3b 20 75 2e 63 67 2e 69 3c 75 2e 63   = 0; u.cg.i<u.c
1f2cb 67 2e 6e 41 72 67 3b 20 75 2e 63 67 2e 69 2b 2b  g.nArg; u.cg.i++
1f2cc 29 7b 0a 20 20 20 20 20 20 75 2e 63 67 2e 61 70  ){.      u.cg.ap
1f2cd 41 72 67 5b 75 2e 63 67 2e 69 5d 20 3d 20 26 75  Arg[u.cg.i] = &u
1f2ce 2e 63 67 2e 70 41 72 67 63 5b 75 2e 63 67 2e 69  .cg.pArgc[u.cg.i
1f2cf 2b 31 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1];.      sqlit
1f2d0 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
1f2d1 70 65 28 75 2e 63 67 2e 61 70 41 72 67 5b 75 2e  pe(u.cg.apArg[u.
1f2d2 63 67 2e 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20  cg.i]);.    }.. 
1f2d3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1f2d4 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1f2d5 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f2d6 6d 69 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69  misuse;.    p->i
1f2d7 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b  nVtabMethod = 1;
1f2d8 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 67 2e 70  .    rc = u.cg.p
1f2d9 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28  Module->xFilter(
1f2da 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72  u.cg.pVtabCursor
1f2db 2c 20 75 2e 63 67 2e 69 51 75 65 72 79 2c 20 70  , u.cg.iQuery, p
1f2dc 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63 67 2e 6e  Op->p4.z, u.cg.n
1f2dd 41 72 67 2c 20 75 2e 63 67 2e 61 70 41 72 67 29  Arg, u.cg.apArg)
1f2de 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d  ;.    p->inVtabM
1f2df 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73  ethod = 0;.    s
1f2e0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f2e1 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1f2e2 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75    p->zErrMsg = u
1f2e3 2e 63 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .cg.pVtab->zErrM
1f2e4 73 67 3b 0a 20 20 20 20 75 2e 63 67 2e 70 56 74  sg;.    u.cg.pVt
1f2e5 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1f2e6 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1f2e7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1f2e8 75 2e 63 67 2e 72 65 73 20 3d 20 75 2e 63 67 2e  u.cg.res = u.cg.
1f2e9 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e  pModule->xEof(u.
1f2ea 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b  cg.pVtabCursor);
1f2eb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
1f2ec 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1f2ed 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1f2ee 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a  due_to_misuse;..
1f2ef 20 20 20 20 69 66 28 20 75 2e 63 67 2e 72 65 73      if( u.cg.res
1f2f0 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
1f2f1 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1f2f2 7d 0a 20 20 7d 0a 20 20 75 2e 63 67 2e 70 43 75  }.  }.  u.cg.pCu
1f2f3 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  r->nullRow = 0;.
1f2f4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1f2f5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1f2f6 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1f2f7 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1f2f8 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f2f9 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1f2fa 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20  Column P1 P2 P3 
1f2fb 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20  * *.**.** Store 
1f2fc 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1f2fd 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66   P2-th column of
1f2fe 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74  .** the row of t
1f2ff 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
1f300 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31   that the .** P1
1f301 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
1f302 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69  ing to into regi
1f303 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65  ster P3..*/.case
1f304 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23   OP_VColumn: {.#
1f305 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f306 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f307 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 20 20 73 71  nto u.ch */.  sq
1f308 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1f309 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  b;.  const sqlit
1f30a 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1f30b 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74  le;.  Mem *pDest
1f30c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ;.  sqlite3_cont
1f30d 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65  ext sContext;.#e
1f30e 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f30f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f310 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 56 64  to u.ch */..  Vd
1f311 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d  beCursor *pCur =
1f312 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1f313 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  1];.  assert( pC
1f314 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
1f315 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1f316 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1f317 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1f318 75 2e 63 68 2e 70 44 65 73 74 20 3d 20 26 61 4d  u.ch.pDest = &aM
1f319 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69  em[pOp->p3];.  i
1f31a 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  f( pCur->nullRow
1f31b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1f31c 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e  dbeMemSetNull(u.
1f31d 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62  ch.pDest);.    b
1f31e 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 68  reak;.  }.  u.ch
1f31f 2e 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70  .pVtab = pCur->p
1f320 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1f321 62 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c  b;.  u.ch.pModul
1f322 65 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e  e = u.ch.pVtab->
1f323 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72  pModule;.  asser
1f324 74 28 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d  t( u.ch.pModule-
1f325 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65  >xColumn );.  me
1f326 6d 73 65 74 28 26 75 2e 63 68 2e 73 43 6f 6e 74  mset(&u.ch.sCont
1f327 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  ext, 0, sizeof(u
1f328 2e 63 68 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a  .ch.sContext));.
1f329 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74  .  /* The output
1f32a 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64   cell may alread
1f32b 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20  y have a buffer 
1f32c 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a  allocated. Move.
1f32d 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74    ** the current
1f32e 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63   contents to u.c
1f32f 68 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20  h.sContext.s so 
1f330 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72  in case the user
1f331 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63  -function.  ** c
1f332 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61  an use the alrea
1f333 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  dy allocated buf
1f334 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61  fer instead of a
1f335 6c 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a  llocating a.  **
1f336 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20   new one..  */. 
1f337 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1f338 6f 76 65 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65  ove(&u.ch.sConte
1f339 78 74 2e 73 2c 20 75 2e 63 68 2e 70 44 65 73 74  xt.s, u.ch.pDest
1f33a 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  );.  MemSetTypeF
1f33b 6c 61 67 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65  lag(&u.ch.sConte
1f33c 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b  xt.s, MEM_Null);
1f33d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1f33e 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1f33f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1f340 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20  _misuse;.  rc = 
1f341 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43  u.ch.pModule->xC
1f342 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61  olumn(pCur->pVta
1f343 62 43 75 72 73 6f 72 2c 20 26 75 2e 63 68 2e 73  bCursor, &u.ch.s
1f344 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32  Context, pOp->p2
1f345 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1f346 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1f347 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1f348 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a   = u.ch.pVtab->z
1f349 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 68 2e 70  ErrMsg;.  u.ch.p
1f34a 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1f34b 30 3b 0a 20 20 69 66 28 20 75 2e 63 68 2e 73 43  0;.  if( u.ch.sC
1f34c 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29  ontext.isError )
1f34d 7b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 68 2e  {.    rc = u.ch.
1f34e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72  sContext.isError
1f34f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79  ;.  }..  /* Copy
1f350 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
1f351 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74  he function to t
1f352 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e 20  he P3 register. 
1f353 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20  We.  ** do this 
1f354 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
1f355 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20  ether or not an 
1f356 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74  error occurred t
1f357 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a  o ensure any.  *
1f358 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61  * dynamic alloca
1f359 74 69 6f 6e 20 69 6e 20 75 2e 63 68 2e 73 43 6f  tion in u.ch.sCo
1f35a 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73  ntext.s (a Mem s
1f35b 74 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 61  truct) is  relea
1f35c 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  sed..  */.  sqli
1f35d 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1f35e 6f 64 69 6e 67 28 26 75 2e 63 68 2e 73 43 6f 6e  oding(&u.ch.sCon
1f35f 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67  text.s, encoding
1f360 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1f361 4d 65 6d 4d 6f 76 65 28 75 2e 63 68 2e 70 44 65  MemMove(u.ch.pDe
1f362 73 74 2c 20 26 75 2e 63 68 2e 73 43 6f 6e 74 65  st, &u.ch.sConte
1f363 78 74 2e 73 29 3b 0a 20 20 52 45 47 49 53 54 45  xt.s);.  REGISTE
1f364 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1f365 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20 20   u.ch.pDest);.  
1f366 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1f367 49 5a 45 28 75 2e 63 68 2e 70 44 65 73 74 29 3b  IZE(u.ch.pDest);
1f368 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
1f369 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
1f36a 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1f36b 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d  e_to_misuse;.  }
1f36c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1f36d 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 68  beMemTooBig(u.ch
1f36e 2e 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67  .pDest) ){.    g
1f36f 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
1f370 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1f371 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1f372 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1f373 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1f374 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f375 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1f376 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Next P1 P2 * * *
1f377 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76  .**.** Advance v
1f378 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20  irtual table P1 
1f379 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20  to the next row 
1f37a 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65  in its result se
1f37b 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f  t and.** jump to
1f37c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
1f37d 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72    Or, if the vir
1f37e 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72  tual table has r
1f37f 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e  eached.** the en
1f380 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20  d of its result 
1f381 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74  set, then fall t
1f382 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1f383 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
1f384 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74  */.case OP_VNext
1f385 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f  : {   /* jump */
1f386 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1f387 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f388 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20   into u.ci */.  
1f389 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1f38a 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  tab;.  const sql
1f38b 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1f38c 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b  dule;.  int res;
1f38d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1f38e 43 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Cur;.#endif /* l
1f38f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f390 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a  oved into u.ci *
1f391 2f 0a 0a 20 20 75 2e 63 69 2e 72 65 73 20 3d 20  /..  u.ci.res = 
1f392 30 3b 0a 20 20 75 2e 63 69 2e 70 43 75 72 20 3d  0;.  u.ci.pCur =
1f393 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1f394 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1f395 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75  ci.pCur->pVtabCu
1f396 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e  rsor );.  if( u.
1f397 63 69 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  ci.pCur->nullRow
1f398 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   ){.    break;. 
1f399 20 7d 0a 20 20 75 2e 63 69 2e 70 56 74 61 62 20   }.  u.ci.pVtab 
1f39a 3d 20 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74  = u.ci.pCur->pVt
1f39b 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
1f39c 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20  .  u.ci.pModule 
1f39d 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d  = u.ci.pVtab->pM
1f39e 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  odule;.  assert(
1f39f 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.ci.pModule->x
1f3a0 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e  Next );..  /* In
1f3a1 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29  voke the xNext()
1f3a2 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d   method of the m
1f3a3 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20  odule. There is 
1f3a4 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20  no way for the. 
1f3a5 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69   ** underlying i
1f3a6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f  mplementation to
1f3a7 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
1f3a8 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64   if one occurs d
1f3a9 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74  uring.  ** xNext
1f3aa 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20  (). Instead, if 
1f3ab 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
1f3ac 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65   true is returne
1f3ad 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68  d (indicating th
1f3ae 61 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20  at.  ** data is 
1f3af 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74  available) and t
1f3b0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  he error code re
1f3b1 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c  turned when xCol
1f3b2 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65  umn or.  ** some
1f3b3 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73   other method is
1f3b4 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e   next invoked on
1f3b5 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61   the save virtua
1f3b6 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a  l table cursor..
1f3b7 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1f3b8 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1f3b9 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1f3ba 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d  _to_misuse;.  p-
1f3bb 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1f3bc 31 3b 0a 20 20 72 63 20 3d 20 75 2e 63 69 2e 70  1;.  rc = u.ci.p
1f3bd 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e  Module->xNext(u.
1f3be 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75  ci.pCur->pVtabCu
1f3bf 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  rsor);.  p->inVt
1f3c0 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1f3c1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f3c2 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1f3c3 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e   p->zErrMsg = u.
1f3c4 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ci.pVtab->zErrMs
1f3c5 67 3b 0a 20 20 75 2e 63 69 2e 70 56 74 61 62 2d  g;.  u.ci.pVtab-
1f3c6 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1f3c7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1f3c8 4b 20 29 7b 0a 20 20 20 20 75 2e 63 69 2e 72 65  K ){.    u.ci.re
1f3c9 73 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65  s = u.ci.pModule
1f3ca 2d 3e 78 45 6f 66 28 75 2e 63 69 2e 70 43 75 72  ->xEof(u.ci.pCur
1f3cb 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a  ->pVtabCursor);.
1f3cc 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
1f3cd 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1f3ce 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f3cf 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28  o_misuse;..  if(
1f3d0 20 21 75 2e 63 69 2e 72 65 73 20 29 7b 0a 20 20   !u.ci.res ){.  
1f3d1 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
1f3d2 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50   data, jump to P
1f3d3 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f  2 */.    pc = pO
1f3d4 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1f3d5 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f3d6 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f3d7 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1f3d8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f3d9 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f3da 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65  E./* Opcode: VRe
1f3db 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a  name P1 * * P4 *
1f3dc 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
1f3dd 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
1f3de 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
1f3df 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
1f3e0 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  b structure..** 
1f3e1 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f  This opcode invo
1f3e2 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f  kes the correspo
1f3e3 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65  nding xRename me
1f3e4 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a  thod. The value.
1f3e5 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ** in register P
1f3e6 31 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  1 is passed as t
1f3e7 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e  he zName argumen
1f3e8 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65  t to the xRename
1f3e9 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65   method..*/.case
1f3ea 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23   OP_VRename: {.#
1f3eb 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f3ec 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f3ed 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71  nto u.cj */.  sq
1f3ee 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1f3ef 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b  b;.  Mem *pName;
1f3f0 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f3f1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f3f2 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 0a 20   into u.cj */.. 
1f3f3 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d 20 70 4f   u.cj.pVtab = pO
1f3f4 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74  p->p4.pVtab->pVt
1f3f5 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4e 61 6d 65  ab;.  u.cj.pName
1f3f6 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
1f3f7 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  ];.  assert( u.c
1f3f8 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  j.pVtab->pModule
1f3f9 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 20 52  ->xRename );.  R
1f3fa 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1f3fb 70 2d 3e 70 31 2c 20 75 2e 63 6a 2e 70 4e 61 6d  p->p1, u.cj.pNam
1f3fc 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  e);.  assert( u.
1f3fd 63 6a 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67 73 20  cj.pName->flags 
1f3fe 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 69  & MEM_Str );.  i
1f3ff 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1f400 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1f401 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1f402 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e  se;.  rc = u.cj.
1f403 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  pVtab->pModule->
1f404 78 52 65 6e 61 6d 65 28 75 2e 63 6a 2e 70 56 74  xRename(u.cj.pVt
1f405 61 62 2c 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e  ab, u.cj.pName->
1f406 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  z);.  sqlite3DbF
1f407 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1f408 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1f409 67 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e  g = u.cj.pVtab->
1f40a 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e  zErrMsg;.  u.cj.
1f40b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1f40c 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
1f40d 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1f40e 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f40f 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65  o_misuse;..  bre
1f410 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
1f411 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f412 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1f413 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74  * Opcode: VUpdat
1f414 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  e P1 P2 P3 P4 *.
1f415 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  **.** P4 is a po
1f416 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75  inter to a virtu
1f417 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c  al table object,
1f418 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62   an sqlite3_vtab
1f419 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
1f41a 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b  his opcode invok
1f41b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  es the correspon
1f41c 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74  ding xUpdate met
1f41d 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a  hod. P2 values.*
1f41e 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73  * are contiguous
1f41f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74   memory cells st
1f420 61 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20  arting at P3 to 
1f421 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64  pass to the xUpd
1f422 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  ate .** invocati
1f423 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e  on. The value in
1f424 20 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32   register (P3+P2
1f425 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  -1) corresponds 
1f426 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20  to the .** p2th 
1f427 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
1f428 72 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64  rgv array passed
1f429 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a   to xUpdate..**.
1f42a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d  ** The xUpdate m
1f42b 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20  ethod will do a 
1f42c 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53  DELETE or an INS
1f42d 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20  ERT or both..** 
1f42e 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d  The argv[0] elem
1f42f 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65  ent (which corre
1f430 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79  sponds to memory
1f431 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20   cell P3).** is 
1f432 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72  the rowid of a r
1f433 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49  ow to delete.  I
1f434 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c  f argv[0] is NUL
1f435 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65  L then no .** de
1f436 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20  letion occurs.  
1f437 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d  The argv[1] elem
1f438 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64  ent is the rowid
1f439 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20   of the new .** 
1f43a 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62  row.  This can b
1f43b 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74  e NULL to have t
1f43c 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1f43d 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20   select the new 
1f43e 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74  .** rowid for it
1f43f 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65  self.  The subse
1f440 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69  quent elements i
1f441 6e 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20  n the array are 
1f442 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f  .** the values o
1f443 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1f444 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20   new row..**.** 
1f445 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f  If P2==1 then no
1f446 20 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f   insert is perfo
1f447 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69  rmed.  argv[0] i
1f448 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a  s the rowid of.*
1f449 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74  * a row to delet
1f44a 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61  e..**.** P1 is a
1f44b 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49   boolean flag. I
1f44c 66 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74  f it is set to t
1f44d 72 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64  rue and the xUpd
1f44e 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73  ate call.** is s
1f44f 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20  uccessful, then 
1f450 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
1f451 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61  ed by sqlite3_la
1f452 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
1f453 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  ) .** is set to 
1f454 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1f455 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72   rowid for the r
1f456 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64  ow just inserted
1f457 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70  ..*/.case OP_VUp
1f458 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f  date: {.#if 0  /
1f459 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1f45a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1f45b 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  k */.  sqlite3_v
1f45c 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71  tab *pVtab;.  sq
1f45d 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1f45e 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72  odule;.  int nAr
1f45f 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71  g;.  int i;.  sq
1f460 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69 64  lite_int64 rowid
1f461 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b  ;.  Mem **apArg;
1f462 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e 64  .  Mem *pX;.#end
1f463 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1f464 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f465 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 75 2e 63 6b   u.ck */..  u.ck
1f466 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34  .pVtab = pOp->p4
1f467 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20  .pVtab->pVtab;. 
1f468 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.ck.pModule = 
1f469 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
1f46a 2a 29 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d  *)u.ck.pVtab->pM
1f46b 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6b 2e 6e 41  odule;.  u.ck.nA
1f46c 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  rg = pOp->p2;.  
1f46d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1f46e 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a  ype==P4_VTAB );.
1f46f 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 63    if( ALWAYS(u.c
1f470 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61  k.pModule->xUpda
1f471 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63 6b 2e  te) ){.    u.ck.
1f472 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67  apArg = p->apArg
1f473 3b 0a 20 20 20 20 75 2e 63 6b 2e 70 58 20 3d 20  ;.    u.ck.pX = 
1f474 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1f475 20 20 20 20 66 6f 72 28 75 2e 63 6b 2e 69 3d 30      for(u.ck.i=0
1f476 3b 20 75 2e 63 6b 2e 69 3c 75 2e 63 6b 2e 6e 41  ; u.ck.i<u.ck.nA
1f477 72 67 3b 20 75 2e 63 6b 2e 69 2b 2b 29 7b 0a 20  rg; u.ck.i++){. 
1f478 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f479 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e 63  MemStoreType(u.c
1f47a 6b 2e 70 58 29 3b 0a 20 20 20 20 20 20 75 2e 63  k.pX);.      u.c
1f47b 6b 2e 61 70 41 72 67 5b 75 2e 63 6b 2e 69 5d 20  k.apArg[u.ck.i] 
1f47c 3d 20 75 2e 63 6b 2e 70 58 3b 0a 20 20 20 20 20  = u.ck.pX;.     
1f47d 20 75 2e 63 6b 2e 70 58 2b 2b 3b 0a 20 20 20 20   u.ck.pX++;.    
1f47e 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1f47f 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1f480 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f481 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72  to_misuse;.    r
1f482 63 20 3d 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65  c = u.ck.pModule
1f483 2d 3e 78 55 70 64 61 74 65 28 75 2e 63 6b 2e 70  ->xUpdate(u.ck.p
1f484 56 74 61 62 2c 20 75 2e 63 6b 2e 6e 41 72 67 2c  Vtab, u.ck.nArg,
1f485 20 75 2e 63 6b 2e 61 70 41 72 67 2c 20 26 75 2e   u.ck.apArg, &u.
1f486 63 6b 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 73  ck.rowid);.    s
1f487 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f488 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1f489 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75    p->zErrMsg = u
1f48a 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .ck.pVtab->zErrM
1f48b 73 67 3b 0a 20 20 20 20 75 2e 63 6b 2e 70 56 74  sg;.    u.ck.pVt
1f48c 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1f48d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1f48e 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1f48f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1f490 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28  _misuse;.    if(
1f491 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1f492 26 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20  & pOp->p1 ){.   
1f493 20 20 20 61 73 73 65 72 74 28 20 75 2e 63 6b 2e     assert( u.ck.
1f494 6e 41 72 67 3e 31 20 26 26 20 75 2e 63 6b 2e 61  nArg>1 && u.ck.a
1f495 70 41 72 67 5b 30 5d 20 26 26 20 28 75 2e 63 6b  pArg[0] && (u.ck
1f496 2e 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73  .apArg[0]->flags
1f497 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20  &MEM_Null) );.  
1f498 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69      db->lastRowi
1f499 64 20 3d 20 75 2e 63 6b 2e 72 6f 77 69 64 3b 0a  d = u.ck.rowid;.
1f49a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68      }.    p->nCh
1f49b 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72  ange++;.  }.  br
1f49c 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1f49d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f49e 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1f49f 66 6e 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d  fndef  SQLITE_OM
1f4a0 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
1f4a1 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65  ./* Opcode: Page
1f4a2 63 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20  count P1 P2 * * 
1f4a3 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
1f4a4 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72  e current number
1f4a5 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 74   of pages in dat
1f4a6 61 62 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f  abase P1 to memo
1f4a7 72 79 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63  ry cell P2..*/.c
1f4a8 61 73 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74  ase OP_Pagecount
1f4a9 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1f4aa 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1f4ab 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1f4ac 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f4ad 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a  oved into u.cl *
1f4ae 2f 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e  /.  int p1;.  in
1f4af 74 20 6e 50 61 67 65 3b 0a 20 20 50 61 67 65 72  t nPage;.  Pager
1f4b0 20 2a 70 50 61 67 65 72 3b 0a 23 65 6e 64 69 66   *pPager;.#endif
1f4b1 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f4b2 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f4b3 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e 63 6c 2e 70  .cl */..  u.cl.p
1f4b4 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1f4b5 2e 63 6c 2e 70 50 61 67 65 72 20 3d 20 73 71 6c  .cl.pPager = sql
1f4b6 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
1f4b7 62 2d 3e 61 44 62 5b 75 2e 63 6c 2e 70 31 5d 2e  b->aDb[u.cl.p1].
1f4b8 70 42 74 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  pBt);.  rc = sql
1f4b9 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
1f4ba 6e 74 28 75 2e 63 6c 2e 70 50 61 67 65 72 2c 20  nt(u.cl.pPager, 
1f4bb 26 75 2e 63 6c 2e 6e 50 61 67 65 29 3b 0a 20 20  &u.cl.nPage);.  
1f4bc 2f 2a 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20  /* OP_Pagecount 
1f4bd 69 73 20 61 6c 77 61 79 73 20 63 61 6c 6c 65 64  is always called
1f4be 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72   from within a r
1f4bf 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ead transaction.
1f4c0 20 20 54 68 65 0a 20 20 2a 2a 20 70 61 67 65 20    The.  ** page 
1f4c1 63 6f 75 6e 74 20 68 61 73 20 61 6c 72 65 61 64  count has alread
1f4c2 79 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75  y been successfu
1f4c3 6c 6c 79 20 72 65 61 64 20 61 6e 64 20 63 61 63  lly read and cac
1f4c4 68 65 64 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a  hed.  So the.  *
1f4c5 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  * sqlite3PagerPa
1f4c6 67 65 63 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61  gecount() call a
1f4c7 62 6f 76 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c  bove cannot fail
1f4c8 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59  . */.  if( ALWAY
1f4c9 53 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  S(rc==SQLITE_OK)
1f4ca 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e   ){.    pOut->u.
1f4cb 69 20 3d 20 75 2e 63 6c 2e 6e 50 61 67 65 3b 0a  i = u.cl.nPage;.
1f4cc 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1f4cd 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1f4ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1f4cf 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63  ./* Opcode: Trac
1f4d0 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a  e * * * P4 *.**.
1f4d1 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73  ** If tracing is
1f4d2 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65   enabled (by the
1f4d3 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29   sqlite3_trace()
1f4d4 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  ) interface, the
1f4d5 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73  n.** the UTF-8 s
1f4d6 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20  tring contained 
1f4d7 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64  in P4 is emitted
1f4d8 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61   on the trace ca
1f4d9 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20  llback..*/.case 
1f4da 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69 66 20  OP_Trace: {.#if 
1f4db 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f4dc 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f4dd 20 75 2e 63 6d 20 2a 2f 0a 20 20 63 68 61 72 20   u.cm */.  char 
1f4de 2a 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69 66 20  *zTrace;.#endif 
1f4df 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f4e0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f4e1 63 6d 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 7a 54  cm */..  u.cm.zT
1f4e2 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e  race = (pOp->p4.
1f4e3 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20  z ? pOp->p4.z : 
1f4e4 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20  p->zSql);.  if( 
1f4e5 75 2e 63 6d 2e 7a 54 72 61 63 65 20 29 7b 0a 20  u.cm.zTrace ){. 
1f4e6 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63     if( db->xTrac
1f4e7 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  e ){.      char 
1f4e8 2a 7a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  *z = sqlite3Vdbe
1f4e9 45 78 70 61 6e 64 53 71 6c 28 70 2c 20 75 2e 63  ExpandSql(p, u.c
1f4ea 6d 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 20  m.zTrace);.     
1f4eb 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e   db->xTrace(db->
1f4ec 70 54 72 61 63 65 41 72 67 2c 20 7a 29 3b 0a 20  pTraceArg, z);. 
1f4ed 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f4ee 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 7d  ee(db, z);.    }
1f4ef 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1f4f0 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62  EBUG.    if( (db
1f4f1 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1f4f2 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b  _SqlTrace)!=0 ){
1f4f3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
1f4f4 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74  bugPrintf("SQL-t
1f4f5 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63  race: %s\n", u.c
1f4f6 6d 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d  m.zTrace);.    }
1f4f7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f4f8 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20  E_DEBUG */.  }. 
1f4f9 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f4fa 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  .../* Opcode: No
1f4fb 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  op * * * * *.**.
1f4fc 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20  ** Do nothing.  
1f4fd 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1f4fe 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c   is often useful
1f4ff 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65   as a jump.** de
1f500 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  stination..*/./*
1f501 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78  .** The magic Ex
1f502 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65  plain opcode are
1f503 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77   only inserted w
1f504 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28  hen explain==2 (
1f505 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73  which.** is to s
1f506 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c  ay when the EXPL
1f507 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73  AIN QUERY PLAN s
1f508 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a  yntax is used.).
1f509 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  ** This opcode r
1f50a 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69  ecords informati
1f50b 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69  on from the opti
1f50c 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68  mizer.  It is th
1f50d 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  e.** the same as
1f50e 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20   a no-op.  This 
1f50f 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70  opcodesnever app
1f510 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56  ears in a real V
1f511 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65  M program..*/.de
1f512 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20  fault: {        
1f513 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61    /* This is rea
1f514 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20  lly OP_Noop and 
1f515 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20  OP_Explain */.  
1f516 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  break;.}../*****
1f517 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f519 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f51a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f51b 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
1f51c 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69  cases of the swi
1f51d 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62  tch statement ab
1f51e 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68  ove this line sh
1f51f 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65  ould all be inde
1f520 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61  nted.** by 6 spa
1f521 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65  ces.  But the le
1f522 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73  ft-most 6 spaces
1f523 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76   have been remov
1f524 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68  ed to improve th
1f525 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79  e.** readability
1f526 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69  .  From this poi
1f527 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20  nt on down, the 
1f528 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69  normal indentati
1f529 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20  on rules are.** 
1f52a 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a  restored..******
1f52b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f52c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f52d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f52e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f52f 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a  *******/.    }..
1f530 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1f531 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ILE.    {.      
1f532 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71  u64 elapsed = sq
1f533 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20  lite3Hwtime() - 
1f534 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70  start;.      pOp
1f535 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70  ->cycles += elap
1f536 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  sed;.      pOp->
1f537 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20  cnt++;.#if 0.   
1f538 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
1f539 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20  out, "%10llu ", 
1f53a 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20  elapsed);.      
1f53b 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1f53c 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69  ntOp(stdout, ori
1f53d 67 50 63 2c 20 26 61 4f 70 5b 6f 72 69 67 50 63  gPc, &aOp[origPc
1f53e 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  ]);.#endif.    }
1f53f 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
1f540 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
1f541 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e 67 20  de adds nothing 
1f542 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 75  to the actual fu
1f543 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20  nctionality.    
1f544 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61  ** of the progra
1f545 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 68  m.  It is only h
1f546 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20  ere for testing 
1f547 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e 0a 20  and debugging.. 
1f548 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68     ** On the oth
1f549 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f 65 73  er hand, it does
1f54a 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c 65 73   burn CPU cycles
1f54b 20 65 76 65 72 79 20 74 69 6d 65 20 74 68 72 6f   every time thro
1f54c 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 65  ugh.    ** the e
1f54d 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20  valuator loop.  
1f54e 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 65 20  So we can leave 
1f54f 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44 45 42  it out when NDEB
1f550 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20  UG is defined.. 
1f551 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44     */.#ifndef ND
1f552 45 42 55 47 0a 20 20 20 20 61 73 73 65 72 74 28  EBUG.    assert(
1f553 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d   pc>=-1 && pc<p-
1f554 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65 66 20  >nOp );..#ifdef 
1f555 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1f556 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b   if( p->trace ){
1f557 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30  .      if( rc!=0
1f558 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 72   ) fprintf(p->tr
1f559 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63  ace,"rc=%d\n",rc
1f55a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  );.      if( pOp
1f55b 2d 3e 6f 70 66 6c 61 67 73 20 26 20 28 4f 50 46  ->opflags & (OPF
1f55c 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41  LG_OUT2_PRERELEA
1f55d 53 45 7c 4f 50 46 4c 47 5f 4f 55 54 32 29 20 29  SE|OPFLG_OUT2) )
1f55e 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74  {.        regist
1f55f 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65  erTrace(p->trace
1f560 2c 20 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d  , pOp->p2, &aMem
1f561 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20  [pOp->p2]);.    
1f562 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f    }.      if( pO
1f563 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46  p->opflags & OPF
1f564 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20  LG_OUT3 ){.     
1f565 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65     registerTrace
1f566 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e  (p->trace, pOp->
1f567 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  p3, &aMem[pOp->p
1f568 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  3]);.      }.   
1f569 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51   }.#endif  /* SQ
1f56a 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65  LITE_DEBUG */.#e
1f56b 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20  ndif  /* NDEBUG 
1f56c 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65  */.  }  /* The e
1f56d 6e 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b  nd of the for(;;
1f56e 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73  ) loop the loops
1f56f 20 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73   through opcodes
1f570 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20   */..  /* If we 
1f571 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
1f572 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
1f573 65 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e  execution is fin
1f574 69 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20  ished with.  ** 
1f575 61 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65  an error of some
1f576 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65   kind..  */.vdbe
1f577 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61  _error_halt:.  a
1f578 73 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70  ssert( rc );.  p
1f579 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c  ->rc = rc;.  sql
1f57a 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1f57b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1f57c 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20  E_IOERR_NOMEM ) 
1f57d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1f57e 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c   = 1;.  rc = SQL
1f57f 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28  ITE_ERROR;.  if(
1f580 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e 46 61   resetSchemaOnFa
1f581 75 6c 74 20 29 20 73 71 6c 69 74 65 33 52 65 73  ult ) sqlite3Res
1f582 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
1f583 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54  (db, 0);..  /* T
1f584 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1f585 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20  way out of this 
1f586 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68  procedure.  We h
1f587 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65  ave to.  ** rele
1f588 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20  ase the mutexes 
1f589 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77  on btrees that w
1f58a 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20  ere acquired at 
1f58b 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f  the.  ** top. */
1f58c 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20  .vdbe_return:.  
1f58d 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
1f58e 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
1f58f 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  aMutex);.  retur
1f590 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  n rc;..  /* Jump
1f591 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74   to here if a st
1f592 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72  ring or blob lar
1f593 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  ger than SQLITE_
1f594 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20  MAX_LENGTH.  ** 
1f595 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1f596 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20    */.too_big:.  
1f597 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1f598 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1f599 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  , "string or blo
1f59a 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72  b too big");.  r
1f59b 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  c = SQLITE_TOOBI
1f59c 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  G;.  goto vdbe_e
1f59d 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1f59e 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
1f59f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1f5a0 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a  s..  */.no_mem:.
1f5a1 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1f5a2 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1f5a3 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1f5a4 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74  ErrMsg, db, "out
1f5a5 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
1f5a6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1f5a7 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  M;.  goto vdbe_e
1f5a8 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1f5a9 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
1f5aa 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  r an SQLITE_MISU
1f5ab 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61  SE error..  */.a
1f5ac 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1f5ad 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  se:.  rc = SQLIT
1f5ae 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46  E_MISUSE;.  /* F
1f5af 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62  all thru into ab
1f5b0 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1f5b1 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74   */..  /* Jump t
1f5b2 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f  o here for any o
1f5b3 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74  ther kind of fat
1f5b4 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22  al error.  The "
1f5b5 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a  rc" variable.  *
1f5b6 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68  * should hold th
1f5b7 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a  e error number..
1f5b8 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74    */.abort_due_t
1f5b9 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72  o_error:.  asser
1f5ba 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  t( p->zErrMsg==0
1f5bb 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
1f5bc 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20  llocFailed ) rc 
1f5bd 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1f5be 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f5bf 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
1f5c0 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1f5c1 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1f5c2 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1f5c3 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1f5c4 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f    }.  goto vdbe_
1f5c5 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f  error_halt;..  /
1f5c6 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
1f5c7 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
1f5c8 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65  terrupt() API se
1f5c9 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74  ts the interrupt
1f5ca 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f  .  ** flag..  */
1f5cb 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e  .abort_due_to_in
1f5cc 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72  terrupt:.  asser
1f5cd 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  t( db->u1.isInte
1f5ce 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20  rrupted );.  rc 
1f5cf 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1f5d0 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63  PT;.  p->rc = rc
1f5d1 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74  ;.  sqlite3SetSt
1f5d2 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1f5d3 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1f5d4 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1f5d5 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1f5d6 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  r_halt;.}../****
1f5d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1f5d8 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1f5d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1f5dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1f5dd 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63   file vdbeblob.c
1f5de 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1f5df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1f5e1 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a   2007 May 1.**.*
1f5e2 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1f5e3 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1f5e4 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1f5e5 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1f5e6 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1f5e7 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1f5e8 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1f5e9 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1f5ea 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1f5eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1f5ec 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1f5ed 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1f5ee 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1f5ef 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1f5f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1f5f1 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1f5f2 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1f5f3 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f5f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f5f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
1f5f8 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1f5f9 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1f5fa 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65   implement incre
1f5fb 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e  mental BLOB I/O.
1f5fc 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  .*/...#ifndef SQ
1f5fd 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
1f5fe 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20  OB../*.** Valid 
1f5ff 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61  sqlite3_blob* ha
1f600 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 6f 20 49  ndles point to I
1f601 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 74 75 72  ncrblob structur
1f602 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
1f603 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 49  truct Incrblob I
1f604 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20  ncrblob;.struct 
1f605 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74  Incrblob {.  int
1f606 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
1f607 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
1f608 22 66 6c 61 67 73 22 20 70 61 73 73 65 64 20 74  "flags" passed t
1f609 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  o sqlite3_blob_o
1f60a 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  pen() */.  int n
1f60b 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
1f60c 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70     /* Size of op
1f60d 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65  en blob, in byte
1f60e 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  s */.  int iOffs
1f60f 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
1f610 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  * Byte offset of
1f611 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 6f 72 20   blob in cursor 
1f612 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73  data */.  BtCurs
1f613 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20  or *pCsr;       
1f614 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
1f615 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 72 6f 77  ting at blob row
1f616 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
1f617 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a  mt *pStmt;    /*
1f618 20 53 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69   Statement holdi
1f619 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a  ng cursor open *
1f61a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
1f61b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1f61c 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61  he associated da
1f61d 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  tabase */.};../*
1f61e 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20  .** Open a blob 
1f61f 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
1f620 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1f621 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73  3_blob_open(.  s
1f622 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20 20  qlite3* db,     
1f623 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1f624 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1f625 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
1f626 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20 20 2f  r *zDb,        /
1f627 2a 20 54 68 65 20 61 74 74 61 63 68 65 64 20 64  * The attached d
1f628 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
1f629 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20  ng the blob */. 
1f62a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
1f62b 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  ble,     /* The 
1f62c 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
1f62d 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63   the blob */.  c
1f62e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75  onst char *zColu
1f62f 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  mn,    /* The co
1f630 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20  lumn containing 
1f631 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71  the blob */.  sq
1f632 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c  lite_int64 iRow,
1f633 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77        /* The row
1f634 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
1f635 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  glob */.  int fl
1f636 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
1f637 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 72 65 61    /* True -> rea
1f638 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c 20  d/write access, 
1f639 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e  false -> read-on
1f63a 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ly */.  sqlite3_
1f63b 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20  blob **ppBlob   
1f63c 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63  /* Handle for ac
1f63d 63 65 73 73 69 6e 67 20 74 68 65 20 62 6c 6f 62  cessing the blob
1f63e 20 72 65 74 75 72 6e 65 64 20 68 65 72 65 20 2a   returned here *
1f63f 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65  /.){.  int nAtte
1f640 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  mpt = 0;.  int i
1f641 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
1f642 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a     /* Index of z
1f643 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65  Column in row-re
1f644 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  cord */..  /* Th
1f645 69 73 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20  is VDBE program 
1f646 73 65 65 6b 73 20 61 20 62 74 72 65 65 20 63 75  seeks a btree cu
1f647 72 73 6f 72 20 74 6f 20 74 68 65 20 69 64 65 6e  rsor to the iden
1f648 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f  tified .  ** db/
1f649 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e  table/row entry.
1f64a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20   The reason for 
1f64b 75 73 69 6e 67 20 61 20 76 64 62 65 20 70 72 6f  using a vdbe pro
1f64c 67 72 61 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a  gram instead.  *
1f64d 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 63 6f 64  * of writing cod
1f64e 65 20 74 6f 20 75 73 65 20 74 68 65 20 62 2d 74  e to use the b-t
1f64f 72 65 65 20 6c 61 79 65 72 20 64 69 72 65 63 74  ree layer direct
1f650 6c 79 20 69 73 20 74 68 61 74 20 74 68 65 0a 20  ly is that the. 
1f651 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d   ** vdbe program
1f652 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e   will take advan
1f653 74 61 67 65 20 6f 66 20 74 68 65 20 76 61 72 69  tage of the vari
1f654 6f 75 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ous transaction,
1f655 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e  .  ** locking an
1f656 64 20 65 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67  d error handling
1f657 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20   infrastructure 
1f658 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 76  built into the v
1f659 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  dbe..  **.  ** A
1f65a 66 74 65 72 20 73 65 65 6b 69 6e 67 20 74 68 65  fter seeking the
1f65b 20 63 75 72 73 6f 72 2c 20 74 68 65 20 76 64 62   cursor, the vdb
1f65c 65 20 65 78 65 63 75 74 65 73 20 61 6e 20 4f 50  e executes an OP
1f65d 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a  _ResultRow..  **
1f65e 20 43 6f 64 65 20 65 78 74 65 72 6e 61 6c 20 74   Code external t
1f65f 6f 20 74 68 65 20 56 64 62 65 20 74 68 65 6e 20  o the Vdbe then 
1f660 22 62 6f 72 72 6f 77 73 22 20 74 68 65 20 62 2d  "borrows" the b-
1f661 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e 64 0a  tree cursor and.
1f662 20 20 2a 2a 20 75 73 65 73 20 69 74 20 74 6f 20    ** uses it to 
1f663 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c  implement the bl
1f664 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f  ob_read(), blob_
1f665 77 72 69 74 65 28 29 20 61 6e 64 20 0a 20 20 2a  write() and .  *
1f666 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 29 20 66  * blob_bytes() f
1f667 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20  unctions..  **. 
1f668 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f   ** The sqlite3_
1f669 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e  blob_close() fun
1f66a 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20  ction finalizes 
1f66b 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
1f66c 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f  ,.  ** which clo
1f66d 73 65 73 20 74 68 65 20 62 2d 74 72 65 65 20 63  ses the b-tree c
1f66e 75 72 73 6f 72 20 61 6e 64 20 28 70 6f 73 73 69  ursor and (possi
1f66f 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 74 68 65  bly) commits the
1f670 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69   .  ** transacti
1f671 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  on..  */.  stati
1f672 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
1f673 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20  st openBlob[] = 
1f674 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61  {.    {OP_Transa
1f675 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c  ction, 0, 0, 0},
1f676 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 61 72 74       /* 0: Start
1f677 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a   a transaction *
1f678 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 69 66 79  /.    {OP_Verify
1f679 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d  Cookie, 0, 0, 0}
1f67a 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b  ,    /* 1: Check
1f67b 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
1f67c 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61  ie */.    {OP_Ta
1f67d 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30  bleLock, 0, 0, 0
1f67e 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 41  },       /* 2: A
1f67f 63 71 75 69 72 65 20 61 20 72 65 61 64 20 6f 72  cquire a read or
1f680 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a   write lock */..
1f681 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
1f682 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  e following two 
1f683 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20  instructions is 
1f684 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 20 4f  replaced by an O
1f685 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b  P_Noop. */.    {
1f686 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20  OP_OpenRead, 0, 
1f687 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a  0, 0},        /*
1f688 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20   3: Open cursor 
1f689 30 20 66 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f  0 for reading */
1f68a 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69  .    {OP_OpenWri
1f68b 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  te, 0, 0, 0},   
1f68c 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63      /* 4: Open c
1f68d 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64  ursor 0 for read
1f68e 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b  /write */..    {
1f68f 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 31 2c 20  OP_Variable, 1, 
1f690 31 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a  1, 1},        /*
1f691 20 35 3a 20 50 75 73 68 20 74 68 65 20 72 6f 77   5: Push the row
1f692 69 64 20 74 6f 20 74 68 65 20 73 74 61 63 6b 20  id to the stack 
1f693 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78  */.    {OP_NotEx
1f694 69 73 74 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20  ists, 0, 9, 1}, 
1f695 20 20 20 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b        /* 6: Seek
1f696 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20   the cursor */. 
1f697 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30     {OP_Column, 0
1f698 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20  , 0, 1},        
1f699 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b    /* 7  */.    {
1f69a 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
1f69b 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a   0, 0},       /*
1f69c 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43   8  */.    {OP_C
1f69d 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  lose, 0, 0, 0}, 
1f69e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39 20 20            /* 9  
1f69f 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c  */.    {OP_Halt,
1f6a0 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1f6a1 20 20 20 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20        /* 10 */. 
1f6a2 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d   };..  Vdbe *v =
1f6a3 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   0;.  int rc = S
1f6a4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72  QLITE_OK;.  char
1f6a5 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 54 61   *zErr = 0;.  Ta
1f6a6 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 50 61 72  ble *pTab;.  Par
1f6a7 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a  se *pParse;..  *
1f6a8 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71  ppBlob = 0;.  sq
1f6a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1f6aa 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1f6ab 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33  pParse = sqlite3
1f6ac 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62  StackAllocRaw(db
1f6ad 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65  , sizeof(*pParse
1f6ae 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  ));.  if( pParse
1f6af 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
1f6b0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1f6b1 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
1f6b2 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b  _out;.  }.  do {
1f6b3 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 72  .    memset(pPar
1f6b4 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61  se, 0, sizeof(Pa
1f6b5 72 73 65 29 29 3b 0a 20 20 20 20 70 50 61 72 73  rse));.    pPars
1f6b6 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20  e->db = db;..   
1f6b7 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f6b8 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
1f6b9 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f6ba 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
1f6bb 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
1f6bc 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
1f6bd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
1f6be 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1f6bf 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ex);.      retur
1f6c0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
1f6c1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
1f6c2 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1f6c3 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d  (db);.    pTab =
1f6c4 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
1f6c5 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a  ble(pParse, 0, z
1f6c6 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20  Table, zDb);.   
1f6c7 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73 56   if( pTab && IsV
1f6c8 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
1f6c9 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a        pTab = 0;.
1f6ca 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1f6cb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
1f6cc 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 74 75  annot open virtu
1f6cd 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a  al table: %s", z
1f6ce 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69  Table);.    }.#i
1f6cf 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f6d0 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 20 70  T_VIEW.    if( p
1f6d1 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 65  Tab && pTab->pSe
1f6d2 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 70 54  lect ){.      pT
1f6d3 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ab = 0;.      sq
1f6d4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1f6d5 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70  arse, "cannot op
1f6d6 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20 7a 54  en view: %s", zT
1f6d7 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  able);.    }.#en
1f6d8 64 69 66 0a 20 20 20 20 69 66 28 20 21 70 54 61  dif.    if( !pTa
1f6d9 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  b ){.      if( p
1f6da 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29  Parse->zErrMsg )
1f6db 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1f6dc 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
1f6dd 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20  );.        zErr 
1f6de 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  = pParse->zErrMs
1f6df 67 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  g;.        pPars
1f6e0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  e->zErrMsg = 0;.
1f6e1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
1f6e2 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1f6e3 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
1f6e4 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1f6e5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f6e6 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
1f6e7 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c  );.      goto bl
1f6e8 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ob_open_out;.   
1f6e9 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73   }..    /* Now s
1f6ea 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20 74  earch pTab for t
1f6eb 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e  he exact column.
1f6ec 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c   */.    for(iCol
1f6ed 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d  =0; iCol < pTab-
1f6ee 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b  >nCol; iCol++) {
1f6ef 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1f6f0 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e  e3StrICmp(pTab->
1f6f1 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
1f6f2 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b  , zColumn)==0 ){
1f6f3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1f6f4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1f6f5 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62    if( iCol==pTab
1f6f6 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
1f6f7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f6f8 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a  , zErr);.      z
1f6f9 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
1f6fa 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63  intf(db, "no suc
1f6fb 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22  h column: \"%s\"
1f6fc 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  ", zColumn);.   
1f6fd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f6fe 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69  RROR;.      (voi
1f6ff 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1f700 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71  ff(db);.      sq
1f701 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
1f702 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f  ll(db);.      go
1f703 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1f704 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1f705 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
1f706 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72  being opened for
1f707 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20   writing, check 
1f708 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  that the.    ** 
1f709 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e  column is not in
1f70a 64 65 78 65 64 2c 20 61 6e 64 20 74 68 61 74 20  dexed, and that 
1f70b 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
1f70c 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e  f a foreign key.
1f70d 20 0a 20 20 20 20 2a 2a 20 49 74 20 69 73 20 61   .    ** It is a
1f70e 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c 65 73  gainst the rules
1f70f 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d   to open a colum
1f710 6e 20 74 6f 20 77 68 69 63 68 20 65 69 74 68 65  n to which eithe
1f711 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a  r of these.    *
1f712 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 61  * descriptions a
1f713 70 70 6c 69 65 73 20 66 6f 72 20 77 72 69 74 69  pplies for writi
1f714 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  ng.  */.    if( 
1f715 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 63  flags ){.      c
1f716 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 61 75 6c  onst char *zFaul
1f717 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 49 6e 64  t = 0;.      Ind
1f718 65 78 20 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65  ex *pIdx;.#ifnde
1f719 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
1f71a 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20  REIGN_KEY.      
1f71b 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51  if( db->flags&SQ
1f71c 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
1f71d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
1f71e 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 63 6f  heck that the co
1f71f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70 61 72 74  lumn is not part
1f720 20 6f 66 20 61 6e 20 46 4b 20 63 68 69 6c 64 20   of an FK child 
1f721 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20  key definition. 
1f722 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  It.        ** is
1f723 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74   not necessary t
1f724 6f 20 63 68 65 63 6b 20 69 66 20 69 74 20 69 73  o check if it is
1f725 20 70 61 72 74 20 6f 66 20 61 20 70 61 72 65 6e   part of a paren
1f726 74 20 6b 65 79 2c 20 61 73 20 70 61 72 65 6e 74  t key, as parent
1f727 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20  .        ** key 
1f728 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20  columns must be 
1f729 69 6e 64 65 78 65 64 2e 20 54 68 65 20 63 68 65  indexed. The che
1f72a 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69  ck below will pi
1f72b 63 6b 20 75 70 20 74 68 69 73 20 0a 20 20 20 20  ck up this .    
1f72c 20 20 20 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f      ** case.  */
1f72d 0a 20 20 20 20 20 20 20 20 46 4b 65 79 20 2a 70  .        FKey *p
1f72e 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 66 6f  FKey;.        fo
1f72f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46  r(pFKey=pTab->pF
1f730 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  Key; pFKey; pFKe
1f731 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72  y=pFKey->pNextFr
1f732 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  om){.          i
1f733 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20  nt j;.          
1f734 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79  for(j=0; j<pFKey
1f735 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
1f736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 46            if( pF
1f737 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72  Key->aCol[j].iFr
1f738 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  om==iCol ){.    
1f739 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74            zFault
1f73a 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 22   = "foreign key"
1f73b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1f73c 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1f73d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65      }.      }.#e
1f73e 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 70  ndif.      for(p
1f73f 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
1f740 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
1f741 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
1f742 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
1f743 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64    for(j=0; j<pId
1f744 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29  x->nColumn; j++)
1f745 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1f746 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
1f747 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20  ]==iCol ){.     
1f748 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20         zFault = 
1f749 22 69 6e 64 65 78 65 64 22 3b 0a 20 20 20 20 20  "indexed";.     
1f74a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1f74b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1f74c 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20  f( zFault ){.   
1f74d 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f74e 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
1f74f 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c        zErr = sql
1f750 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
1f751 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20  "cannot open %s 
1f752 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69  column for writi
1f753 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20  ng", zFault);.  
1f754 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1f755 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
1f756 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1f757 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1f758 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1f759 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1f75a 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62         goto blob
1f75b 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20  _open_out;.     
1f75c 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20   }.    }..    v 
1f75d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65  = sqlite3VdbeCre
1f75e 61 74 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28  ate(db);.    if(
1f75f 20 76 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20   v ){.      int 
1f760 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1f761 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1f762 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1f763 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f764 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a  AddOpList(v, siz
1f765 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69  eof(openBlob)/si
1f766 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29  zeof(VdbeOpList)
1f767 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20  , openBlob);.   
1f768 20 20 20 66 6c 61 67 73 20 3d 20 21 21 66 6c 61     flags = !!fla
1f769 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
1f76a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28      /* flags = (
1f76b 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29 3b 20  flags ? 1 : 0); 
1f76c 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  */..      /* Con
1f76d 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 72  figure the OP_Tr
1f76e 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  ansaction */.   
1f76f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f770 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 62  angeP1(v, 0, iDb
1f771 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f772 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1f773 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20  0, flags);..    
1f774 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74    /* Configure t
1f775 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  he OP_VerifyCook
1f776 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ie */.      sqli
1f777 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
1f778 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  v, 1, iDb);.    
1f779 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f77a 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62  ngeP2(v, 1, pTab
1f77b 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d  ->pSchema->schem
1f77c 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20  a_cookie);..    
1f77d 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
1f77e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f   mutex is held o
1f77f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  n the table to b
1f780 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20  e accessed */.  
1f781 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
1f782 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
1f783 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ; ..      /* Con
1f784 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 61  figure the OP_Ta
1f785 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74  bleLock instruct
1f786 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ion */.      sql
1f787 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
1f788 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a 20 20 20  (v, 2, iDb);.   
1f789 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f78a 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 70 54 61  angeP2(v, 2, pTa
1f78b 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  b->tnum);.      
1f78c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f78d 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 67 73 29  eP3(v, 2, flags)
1f78e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f78f 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 32  dbeChangeP4(v, 2
1f790 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50  , pTab->zName, P
1f791 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20  4_TRANSIENT);.. 
1f792 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65       /* Remove e
1f793 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65  ither the OP_Ope
1f794 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65  nWrite or OpenRe
1f795 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a  ad. Set the P2 .
1f796 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74        ** paramet
1f797 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  er of the other 
1f798 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20  to pTab->tnum.  
1f799 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1f79a 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70  VdbeChangeToNoop
1f79b 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 2c 20 31  (v, 4 - flags, 1
1f79c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f79d 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1f79e 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 61 62 2d  3 + flags, pTab-
1f79f 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71  >tnum);.      sq
1f7a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1f7a1 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20  3(v, 3 + flags, 
1f7a2 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  iDb);..      /* 
1f7a3 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 6e 75  Configure the nu
1f7a4 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e  mber of columns.
1f7a5 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63   Configure the c
1f7a6 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a  ursor to.      *
1f7a7 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65  * think that the
1f7a8 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d   table has one m
1f7a9 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20  ore column than 
1f7aa 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20  it really.      
1f7ab 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43  ** does. An OP_C
1f7ac 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76  olumn to retriev
1f7ad 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79  e this imaginary
1f7ae 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20   column will.   
1f7af 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74     ** always ret
1f7b0 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e  urn an SQL NULL.
1f7b1 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20   This is useful 
1f7b2 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73  because it means
1f7b3 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e  .      ** we can
1f7b4 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d   invoke OP_Colum
1f7b5 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65  n to fill in the
1f7b6 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 74 79   vdbe cursors ty
1f7b7 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  pe .      ** and
1f7b8 20 6f 66 66 73 65 74 20 63 61 63 68 65 20 77 69   offset cache wi
1f7b9 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e  thout causing an
1f7ba 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  y IO..      */. 
1f7bb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f7bc 43 68 61 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c  ChangeP4(v, 3+fl
1f7bd 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ags, SQLITE_INT_
1f7be 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f  TO_PTR(pTab->nCo
1f7bf 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a  l+1),P4_INT32);.
1f7c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f7c1 65 43 68 61 6e 67 65 50 32 28 76 2c 20 37 2c 20  eChangeP2(v, 7, 
1f7c2 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20  pTab->nCol);.   
1f7c3 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c     if( !db->mall
1f7c4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1f7c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1f7c6 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20 31  akeReady(v, 1, 1
1f7c7 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 1, 0, 0, 0);. 
1f7c8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1f7c9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f7ca 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1f7cb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
1f7cc 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
1f7cd 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d    if( NEVER(rc!=
1f7ce 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62  SQLITE_OK) || db
1f7cf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1f7d0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f  {.      goto blo
1f7d1 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
1f7d2 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  }..    sqlite3_b
1f7d3 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c 69 74  ind_int64((sqlit
1f7d4 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20  e3_stmt *)v, 1, 
1f7d5 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20  iRow);.    rc = 
1f7d6 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28 73 71  sqlite3_step((sq
1f7d7 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b  lite3_stmt *)v);
1f7d8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1f7d9 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
1f7da 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20   nAttempt++;.   
1f7db 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1f7dc 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65  finalize((sqlite
1f7dd 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20  3_stmt *)v);.   
1f7de 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f7df 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
1f7e0 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33    zErr = sqlite3
1f7e1 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 71 6c 69  MPrintf(db, sqli
1f7e2 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b  te3_errmsg(db));
1f7e3 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a 20 20  .      v = 0;.  
1f7e4 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e    }.  } while( n
1f7e5 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 63 3d  Attempt<5 && rc=
1f7e6 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
1f7e7 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
1f7e8 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f  ITE_ROW ){.    /
1f7e9 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f 72 64  * The row-record
1f7ea 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64   has been opened
1f7eb 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 43   successfully. C
1f7ec 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20  heck that the.  
1f7ed 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71    ** column in q
1f7ee 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  uestion contains
1f7ef 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e   text or a blob.
1f7f0 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a   If it contains.
1f7f1 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 74 20      ** text, it 
1f7f2 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 61 6c  is up to the cal
1f7f3 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 65  ler to get the e
1f7f4 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e 0a 20  ncoding right.. 
1f7f5 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c     */.    Incrbl
1f7f6 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75  ob *pBlob;.    u
1f7f7 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 70 43  32 type = v->apC
1f7f8 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f  sr[0]->aType[iCo
1f7f9 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 79 70  l];..    if( typ
1f7fa 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 73 71  e<12 ){.      sq
1f7fb 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f7fc 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72  zErr);.      zEr
1f7fd 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
1f7fe 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f  tf(db, "cannot o
1f7ff 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70  pen value of typ
1f800 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20  e %s",.         
1f801 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a   type==0?"null":
1f802 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a   type==7?"real":
1f803 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20   "integer".     
1f804 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   );.      rc = S
1f805 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1f806 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1f807 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  n_out;.    }.   
1f808 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c   pBlob = (Incrbl
1f809 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  ob *)sqlite3DbMa
1f80a 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
1f80b 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a  eof(Incrblob));.
1f80c 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
1f80d 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1f80e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f80f 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20  db, pBlob);.    
1f810 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
1f811 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
1f812 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66  pBlob->flags = f
1f813 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d  lags;.    pBlob-
1f814 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 73  >pCsr =  v->apCs
1f815 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  r[0]->pCursor;. 
1f816 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
1f817 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c 6f 62  nterCursor(pBlob
1f818 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c  ->pCsr);.    sql
1f819 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76  ite3BtreeCacheOv
1f81a 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43  erflow(pBlob->pC
1f81b 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sr);.    sqlite3
1f81c 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72  BtreeLeaveCursor
1f81d 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20  (pBlob->pCsr);. 
1f81e 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20     pBlob->pStmt 
1f81f 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  = (sqlite3_stmt 
1f820 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e  *)v;.    pBlob->
1f821 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 43  iOffset = v->apC
1f822 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69  sr[0]->aOffset[i
1f823 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d  Col];.    pBlob-
1f824 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33  >nByte = sqlite3
1f825 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1f826 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 42 6c  n(type);.    pBl
1f827 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20  ob->db = db;.   
1f828 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69   *ppBlob = (sqli
1f829 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  te3_blob *)pBlob
1f82a 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1f82b 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66  E_OK;.  }else if
1f82c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1f82d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
1f82e 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
1f82f 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74      zErr = sqlit
1f830 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
1f831 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c  o such rowid: %l
1f832 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20  ld", iRow);.    
1f833 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f834 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65  R;.  }..blob_ope
1f835 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 76 20 26  n_out:.  if( v &
1f836 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  & (rc!=SQLITE_OK
1f837 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1f838 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c  iled) ){.    sql
1f839 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1f83a 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  (v);.  }.  sqlit
1f83b 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
1f83c 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  zErr);.  sqlite3
1f83d 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
1f83e 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ;.  sqlite3Stack
1f83f 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29  Free(db, pParse)
1f840 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1f841 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
1f842 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1f843 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1f844 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1f845 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
1f846 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 68 61   blob handle tha
1f847 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
1f848 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a   created using.*
1f849 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  * sqlite3_blob_o
1f84a 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pen()..*/.SQLITE
1f84b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1f84c 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69  _blob_close(sqli
1f84d 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29  te3_blob *pBlob)
1f84e 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20  {.  Incrblob *p 
1f84f 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42  = (Incrblob *)pB
1f850 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  lob;.  int rc;. 
1f851 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20   sqlite3 *db;.. 
1f852 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 64 62   if( p ){.    db
1f853 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71   = p->db;.    sq
1f854 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1f855 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1f856 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66    rc = sqlite3_f
1f857 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74  inalize(p->pStmt
1f858 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1f859 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20  Free(db, p);.   
1f85a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1f85b 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1f85c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1f85d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1f85e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f85f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
1f860 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  a read or write 
1f861 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62  operation on a b
1f862 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  lob.*/.static in
1f863 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  t blobReadWrite(
1f864 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  .  sqlite3_blob 
1f865 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20  *pBlob, .  void 
1f866 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1f867 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 0a 20   int iOffset, . 
1f868 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 42 74   int (*xCall)(Bt
1f869 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33  Cursor*, u32, u3
1f86a 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  2, void*).){.  i
1f86b 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f  nt rc;.  Incrblo
1f86c 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62  b *p = (Incrblob
1f86d 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65   *)pBlob;.  Vdbe
1f86e 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *v;.  sqlite3 *
1f86f 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20  db;..  if( p==0 
1f870 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1f871 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 3d 20 70  MISUSE;.  db = p
1f872 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ->db;.  sqlite3_
1f873 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1f874 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56  mutex);.  v = (V
1f875 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a  dbe*)p->pStmt;..
1f876 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66    if( n<0 || iOf
1f877 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73  fset<0 || (iOffs
1f878 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29  et+n)>p->nByte )
1f879 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74  {.    /* Request
1f87a 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
1f87b 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73  . Return a trans
1f87c 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20  ient error. */. 
1f87d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f87e 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65  RROR;.    sqlite
1f87f 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
1f880 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d  E_ERROR, 0);.  }
1f881 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29   else if( v==0 )
1f882 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  {.    /* If ther
1f883 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
1f884 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74  t handle, then t
1f885 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68  he blob-handle h
1f886 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  as.    ** alread
1f887 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74  y been invalidat
1f888 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54  ed. Return SQLIT
1f889 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20  E_ABORT in this 
1f88a 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
1f88b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f   rc = SQLITE_ABO
1f88c 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  RT;.  }else{.   
1f88d 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20   /* Call either 
1f88e 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42  BtreeData() or B
1f88f 74 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49  treePutData(). I
1f890 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69  f SQLITE_ABORT i
1f891 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65  s.    ** returne
1f892 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20  d, clean-up the 
1f893 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
1f894 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
1f895 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62  ert( db == v->db
1f896 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   );.    sqlite3B
1f897 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28  treeEnterCursor(
1f898 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63  p->pCsr);.    rc
1f899 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72   = xCall(p->pCsr
1f89a 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66  , iOffset+p->iOf
1f89b 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20  fset, n, z);.   
1f89c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1f89d 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72  veCursor(p->pCsr
1f89e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1f89f 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20  QLITE_ABORT ){. 
1f8a0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f8a1 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20  Finalize(v);.   
1f8a2 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b     p->pStmt = 0;
1f8a3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f8a4 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20    db->errCode = 
1f8a5 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20  rc;.      v->rc 
1f8a6 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  = rc;.    }.  }.
1f8a7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
1f8a8 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
1f8a9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1f8aa 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1f8ab 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1f8ac 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
1f8ad 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e   from a blob han
1f8ae 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1f8af 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f8b0 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33  lob_read(sqlite3
1f8b1 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f  _blob *pBlob, vo
1f8b2 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e  id *z, int n, in
1f8b3 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65  t iOffset){.  re
1f8b4 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69  turn blobReadWri
1f8b5 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20  te(pBlob, z, n, 
1f8b6 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33  iOffset, sqlite3
1f8b7 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f  BtreeData);.}../
1f8b8 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
1f8b9 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  to a blob handle
1f8ba 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f8bb 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f8bc 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62  _write(sqlite3_b
1f8bd 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73  lob *pBlob, cons
1f8be 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e  t void *z, int n
1f8bf 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a  , int iOffset){.
1f8c0 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61    return blobRea
1f8c1 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76  dWrite(pBlob, (v
1f8c2 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66  oid *)z, n, iOff
1f8c3 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65  set, sqlite3Btre
1f8c4 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a  ePutData);.}../*
1f8c5 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62  .** Query a blob
1f8c6 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20   handle for the 
1f8c7 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
1f8c8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72  ..**.** The Incr
1f8c9 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64  blob.nByte field
1f8ca 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68   is fixed for th
1f8cb 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68  e lifetime of th
1f8cc 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f  e Incrblob.** so
1f8cd 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71   no mutex is req
1f8ce 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73  uired for access
1f8cf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f8d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f8d1 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62  _bytes(sqlite3_b
1f8d2 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49  lob *pBlob){.  I
1f8d3 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e  ncrblob *p = (In
1f8d4 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a  crblob *)pBlob;.
1f8d5 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e    return p ? p->
1f8d6 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65  nByte : 0;.}..#e
1f8d7 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20  ndif /* #ifndef 
1f8d8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
1f8d9 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  BLOB */../******
1f8da 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1f8db 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a  vdbeblob.c *****
1f8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8de 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1f8df 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1f8e0 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a  ile journal.c **
1f8e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8e3 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1f8e4 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2a 2a  007 August 22.**
1f8e5 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1f8e6 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1f8e7 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1f8e8 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1f8e9 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1f8ea 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1f8eb 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1f8ec 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1f8ed 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1f8ee 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1f8ef 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1f8f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1f8f1 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1f8f2 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1f8f3 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1f8f4 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1f8f5 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1f8f6 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1f8f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1f8fb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
1f8fc 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61  lements a specia
1f8fd 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65  l kind of sqlite
1f8fe 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73  3_file object us
1f8ff 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20  ed.** by SQLite 
1f900 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61  to create journa
1f901 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61  l files if the a
1f902 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
1f903 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65  mization.** is e
1f904 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  nabled..**.** Th
1f905 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68  e distinctive ch
1f906 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20  aracteristic of 
1f907 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c  this sqlite3_fil
1f908 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  e is that the.**
1f909 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20   actual on disk 
1f90a 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20  file is created 
1f90b 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65  lazily. When the
1f90c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
1f90d 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20  ,.** the caller 
1f90e 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66  specifies a buff
1f90f 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69  er size for an i
1f910 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  n-memory buffer 
1f911 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f  to.** be used to
1f912 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20   service read() 
1f913 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75  and write() requ
1f914 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c  ests. The actual
1f915 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b   file.** on disk
1f916 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20   is not created 
1f917 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74  or populated unt
1f918 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a  il either:.**.**
1f919 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d     1) The in-mem
1f91a 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
1f91b 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72  on grows too lar
1f91c 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63  ge for the alloc
1f91d 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75  ated .**      bu
1f91e 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29  ffer, or.**   2)
1f91f 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72   The sqlite3Jour
1f920 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63  nalCreate() func
1f921 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
1f922 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1f923 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57  _ENABLE_ATOMIC_W
1f924 52 49 54 45 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a  RITE.../*.** A J
1f925 6f 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63  ournalFile objec
1f926 74 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20  t is a subclass 
1f927 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  of sqlite3_file 
1f928 75 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e  used by.** as an
1f929 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
1f92a 65 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69  e for journal fi
1f92b 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a  les..*/.struct J
1f92c 6f 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73  ournalFile {.  s
1f92d 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
1f92e 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f  s *pMethod;    /
1f92f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e  * I/O methods on
1f930 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a   journal files *
1f931 2f 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20  /.  int nBuf;   
1f932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f933 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a      /* Size of z
1f934 42 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a  Buf[] in bytes *
1f935 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20  /.  char *zBuf; 
1f936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f937 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
1f938 62 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77  buffer journal w
1f939 72 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rites */.  int i
1f93a 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
1f93b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d             /* Am
1f93c 6f 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63  ount of zBuf[] c
1f93d 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f  urrently used */
1f93e 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20  .  int flags;   
1f93f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f940 20 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67     /* xOpen flag
1f941 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  s */.  sqlite3_v
1f942 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20  fs *pVfs;       
1f943 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72         /* The "r
1f944 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20  eal" underlying 
1f945 56 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  VFS */.  sqlite3
1f946 5f 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20  _file *pReal;   
1f947 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f948 22 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e  "real" underlyin
1f949 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  g file descripto
1f94a 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
1f94b 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  r *zJournal;    
1f94c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1f94d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
1f94e 6c 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  le */.};.typedef
1f94f 20 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46   struct JournalF
1f950 69 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b  ile JournalFile;
1f951 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f  ../*.** If it do
1f952 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  es not already e
1f953 78 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e  xists, create an
1f954 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f  d populate the o
1f955 6e 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20  n-disk file .** 
1f956 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  for JournalFile 
1f957 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
1f958 20 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72   createFile(Jour
1f959 6e 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69  nalFile *p){.  i
1f95a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1f95b 4b 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65  K;.  if( !p->pRe
1f95c 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  al ){.    sqlite
1f95d 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20  3_file *pReal = 
1f95e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
1f95f 26 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20  &p[1];.    rc = 
1f960 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d  sqlite3OsOpen(p-
1f961 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e  >pVfs, p->zJourn
1f962 61 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c  al, pReal, p->fl
1f963 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ags, 0);.    if(
1f964 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1f965 7b 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c  {.      p->pReal
1f966 20 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20   = pReal;.      
1f967 69 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29  if( p->iSize>0 )
1f968 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1f969 28 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42  (p->iSize<=p->nB
1f96a 75 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  uf);.        rc 
1f96b 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
1f96c 28 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42  (p->pReal, p->zB
1f96d 75 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29  uf, p->iSize, 0)
1f96e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f96f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1f970 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
1f971 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1f972 74 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73  tic int jrnlClos
1f973 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1f974 70 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c  pJfd){.  Journal
1f975 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e  File *p = (Journ
1f976 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20  alFile *)pJfd;. 
1f977 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b   if( p->pReal ){
1f978 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
1f979 6f 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20  ose(p->pReal);. 
1f97a 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
1f97b 65 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65  e(p->zBuf);.  re
1f97c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1f97d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  }../*.** Read da
1f97e 74 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ta from the file
1f97f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f980 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69  jrnlRead(.  sqli
1f981 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1f982 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
1f983 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63  l file from whic
1f984 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76  h to read */.  v
1f985 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1f986 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65        /* Put the
1f987 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
1f988 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20  .  int iAmt,    
1f989 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1f98a 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
1f98b 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  read */.  sqlite
1f98c 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20  _int64 iOfst    
1f98d 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e   /* Begin readin
1f98e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74  g at this offset
1f98f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
1f990 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a  = SQLITE_OK;.  J
1f991 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1f992 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1f993 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52  Jfd;.  if( p->pR
1f994 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eal ){.    rc = 
1f995 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d  sqlite3OsRead(p-
1f996 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41  >pReal, zBuf, iA
1f997 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65  mt, iOfst);.  }e
1f998 6c 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f  lse if( (iAmt+iO
1f999 66 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b  fst)>p->iSize ){
1f99a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1f99b 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
1f99c 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  D;.  }else{.    
1f99d 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d  memcpy(zBuf, &p-
1f99e 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41  >zBuf[iOfst], iA
1f99f 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  mt);.  }.  retur
1f9a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  n rc;.}../*.** W
1f9a1 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
1f9a2 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1f9a3 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a   int jrnlWrite(.
1f9a4 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1f9a5 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20  pJfd,    /* The 
1f9a6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74  journal file int
1f9a7 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65  o which to write
1f9a8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1f9a9 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20   *zBuf,      /* 
1f9aa 54 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20  Take data to be 
1f9ab 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72  written from her
1f9ac 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c  e */.  int iAmt,
1f9ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1f9ae 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1f9af 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73   to write */.  s
1f9b0 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73  qlite_int64 iOfs
1f9b1 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77  t     /* Begin w
1f9b2 72 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f  riting at this o
1f9b3 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66  ffset into the f
1f9b4 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ile */.){.  int 
1f9b5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f9b6 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1f9b7 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1f9b8 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70  *)pJfd;.  if( !p
1f9b9 2d 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73  ->pReal && (iOfs
1f9ba 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20  t+iAmt)>p->nBuf 
1f9bb 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61  ){.    rc = crea
1f9bc 74 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20  teFile(p);.  }. 
1f9bd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f9be 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  OK ){.    if( p-
1f9bf 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20  >pReal ){.      
1f9c0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1f9c1 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42  ite(p->pReal, zB
1f9c2 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29  uf, iAmt, iOfst)
1f9c3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1f9c4 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42     memcpy(&p->zB
1f9c5 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c  uf[iOfst], zBuf,
1f9c6 20 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66   iAmt);.      if
1f9c7 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73  ( p->iSize<(iOfs
1f9c8 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20  t+iAmt) ){.     
1f9c9 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69     p->iSize = (i
1f9ca 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20  Ofst+iAmt);.    
1f9cb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1f9cc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f9cd 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65  .** Truncate the
1f9ce 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1f9cf 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74   int jrnlTruncat
1f9d0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1f9d1 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74  pJfd, sqlite_int
1f9d2 36 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20  64 size){.  int 
1f9d3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f9d4 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1f9d5 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1f9d6 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1f9d7 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63  >pReal ){.    rc
1f9d8 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
1f9d9 63 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73  cate(p->pReal, s
1f9da 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ize);.  }else if
1f9db 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20  ( size<p->iSize 
1f9dc 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20  ){.    p->iSize 
1f9dd 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65  = size;.  }.  re
1f9de 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1f9df 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e  * Sync the file.
1f9e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1f9e1 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  rnlSync(sqlite3_
1f9e2 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20  file *pJfd, int 
1f9e3 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63  flags){.  int rc
1f9e4 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  ;.  JournalFile 
1f9e5 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c  *p = (JournalFil
1f9e6 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20  e *)pJfd;.  if( 
1f9e7 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20  p->pReal ){.    
1f9e8 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
1f9e9 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61  nc(p->pReal, fla
1f9ea 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  gs);.  }else{.  
1f9eb 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1f9ec 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1f9ed 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72  c;.}../*.** Quer
1f9ee 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  y the size of th
1f9ef 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e  e file in bytes.
1f9f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1f9f1 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  rnlFileSize(sqli
1f9f2 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1f9f3 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53  sqlite_int64 *pS
1f9f4 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ize){.  int rc =
1f9f5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f   SQLITE_OK;.  Jo
1f9f6 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f9f7 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f9f8 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
1f9f9 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
1f9fa 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
1f9fb 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65  (p->pReal, pSize
1f9fc 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1f9fd 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65  *pSize = (sqlite
1f9fe 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65  _int64) p->iSize
1f9ff 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1fa00 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c  c;.}../*.** Tabl
1fa01 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72  e of methods for
1fa02 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c   JournalFile sql
1fa03 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74  ite3_file object
1fa04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
1fa05 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ct sqlite3_io_me
1fa06 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c  thods JournalFil
1fa07 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31  eMethods = {.  1
1fa08 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1fa09 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a   iVersion */.  j
1fa0a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a  rnlClose,     /*
1fa0b 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e   xClose */.  jrn
1fa0c 6c 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78  lRead,      /* x
1fa0d 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72  Read */.  jrnlWr
1fa0e 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69  ite,     /* xWri
1fa0f 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e  te */.  jrnlTrun
1fa10 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63  cate,  /* xTrunc
1fa11 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e  ate */.  jrnlSyn
1fa12 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63  c,      /* xSync
1fa13 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69   */.  jrnlFileSi
1fa14 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a  ze,  /* xFileSiz
1fa15 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  e */.  0,       
1fa16 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a        /* xLock *
1fa17 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1fa18 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
1fa19 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1fa1a 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
1fa1b 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  vedLock */.  0, 
1fa1c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1fa1d 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20  FileControl */. 
1fa1e 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1fa1f 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a  /* xSectorSize *
1fa20 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20  /.  0           
1fa21 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61     /* xDeviceCha
1fa22 72 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a  racteristics */.
1fa23 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20  };../* .** Open 
1fa24 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  a journal file..
1fa25 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1fa26 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75  E int sqlite3Jou
1fa27 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69  rnalOpen(.  sqli
1fa28 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
1fa29 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46         /* The VF
1fa2a 53 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74  S to use for act
1fa2b 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a  ual file I/O */.
1fa2c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
1fa2d 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ame,         /* 
1fa2e 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  Name of the jour
1fa2f 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71  nal file */.  sq
1fa30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1fa31 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61  ,        /* Prea
1fa32 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20  llocated, blank 
1fa33 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  file handle */. 
1fa34 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
1fa35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1fa36 70 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a  pening flags */.
1fa37 20 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20    int nBuf      
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fa39 42 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62  Bytes buffered b
1fa3a 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68  efore opening th
1fa3b 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a  e file */.){.  J
1fa3c 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1fa3d 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1fa3e 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c  Jfd;.  memset(p,
1fa3f 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e   0, sqlite3Journ
1fa40 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20  alSize(pVfs));. 
1fa41 20 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20   if( nBuf>0 ){. 
1fa42 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c     p->zBuf = sql
1fa43 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e  ite3MallocZero(n
1fa44 42 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70  Buf);.    if( !p
1fa45 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20  ->zBuf ){.      
1fa46 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1fa47 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  MEM;.    }.  }el
1fa48 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
1fa49 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
1fa4a 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20  s, zName, pJfd, 
1fa4b 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20  flags, 0);.  }. 
1fa4c 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a   p->pMethod = &J
1fa4d 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64  ournalFileMethod
1fa4e 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e  s;.  p->nBuf = n
1fa4f 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20  Buf;.  p->flags 
1fa50 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a  = flags;.  p->zJ
1fa51 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a  ournal = zName;.
1fa52 20 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73    p->pVfs = pVfs
1fa53 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1fa54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
1fa55 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70  f the argument p
1fa56 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75   points to a Jou
1fa57 72 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75  rnalFile structu
1fa58 72 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65  re, and the unde
1fa59 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68  rlying.** file h
1fa5a 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  as not yet been 
1fa5b 63 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20  created, create 
1fa5c 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54  it now..*/.SQLIT
1fa5d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fa5e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
1fa5f 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
1fa60 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d  *p){.  if( p->pM
1fa61 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c  ethods!=&Journal
1fa62 46 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20  FileMethods ){. 
1fa63 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1fa64 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  _OK;.  }.  retur
1fa65 6e 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f  n createFile((Jo
1fa66 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a  urnalFile *)p);.
1fa67 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e  }../* .** Return
1fa68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1fa69 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ytes required to
1fa6a 20 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c   store a Journal
1fa6b 46 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76  File that uses v
1fa6c 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72  fs.** pVfs to cr
1fa6d 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79  eate the underly
1fa6e 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65  ing on-disk file
1fa6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
1fa70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1fa71 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69  JournalSize(sqli
1fa72 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a  te3_vfs *pVfs){.
1fa73 20 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e    return (pVfs->
1fa74 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28  szOsFile+sizeof(
1fa75 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d  JournalFile));.}
1fa76 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
1fa77 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1fa78 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1fa79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa7b 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1fa7c 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1fa7d 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63  ile memjournal.c
1fa7e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1fa7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1fa81 30 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a  008 October 7.**
1fa82 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1fa83 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1fa84 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1fa85 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1fa86 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1fa87 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1fa88 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1fa89 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1fa8a 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1fa8b 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1fa8c 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1fa8d 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1fa8e 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1fa8f 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1fa90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1fa91 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1fa92 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1fa93 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1fa94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fa97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1fa98 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1fa99 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74  tains code use t
1fa9a 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69  o implement an i
1fa9b 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63  n-memory rollbac
1fa9c 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68  k journal..** Th
1fa9d 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c  e in-memory roll
1fa9e 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20  back journal is 
1fa9f 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  used to journal 
1faa0 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72  transactions for
1faa1 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64  .** ":memory:" d
1faa2 61 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65  atabases and whe
1faa3 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  n the journal_mo
1faa4 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61  de=MEMORY pragma
1faa5 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a   is used..*/../*
1faa6 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
1faa7 63 65 73 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20  ces to internal 
1faa8 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 74 79  structures */.ty
1faa9 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
1faaa 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e  Journal MemJourn
1faab 61 6c 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  al;.typedef stru
1faac 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c  ct FilePoint Fil
1faad 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20  ePoint;.typedef 
1faae 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b  struct FileChunk
1faaf 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20   FileChunk;../* 
1fab0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
1fab1 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
1fab2 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  al is allocated 
1fab3 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66  in increments of
1fab4 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79 20 62 79  .** this many by
1fab5 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  tes..**.** The s
1fab6 69 7a 65 20 63 68 6f 73 65 6e 20 69 73 20 61 20  ize chosen is a 
1fab7 6c 69 74 74 6c 65 20 6c 65 73 73 20 74 68 61 6e  little less than
1fab8 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e   a power of two.
1fab9 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74    That way,.** t
1faba 68 65 20 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a  he FileChunk obj
1fabb 65 63 74 20 77 69 6c 6c 20 68 61 76 65 20 61 20  ect will have a 
1fabc 73 69 7a 65 20 74 68 61 74 20 61 6c 6d 6f 73 74  size that almost
1fabd 20 65 78 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a   exactly fills.*
1fabe 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f  * a power-of-two
1fabf 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68   allocation.  Th
1fac0 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73  is mimimizes was
1fac1 74 65 64 20 73 70 61 63 65 20 69 6e 20 70 6f 77  ted space in pow
1fac2 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d  er-of-two.** mem
1fac3 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a  ory allocators..
1fac4 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e  */.#define JOURN
1fac5 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 28 28 69  AL_CHUNKSIZE ((i
1fac6 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 6f 66 28  nt)(1024-sizeof(
1fac7 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f  FileChunk*)))../
1fac8 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20  * Macro to find 
1fac9 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74  the minimum of t
1faca 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65  wo numeric value
1facb 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49  s..*/.#ifndef MI
1facc 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78  N.# define MIN(x
1facd 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29  ,y) ((x)<(y)?(x)
1face 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  :(y)).#endif../*
1facf 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  .** The rollback
1fad0 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70   journal is comp
1fad1 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64  osed of a linked
1fad2 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 73   list of these s
1fad3 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74  tructures..*/.st
1fad4 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b  ruct FileChunk {
1fad5 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e  .  FileChunk *pN
1fad6 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
1fad7 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b     /* Next chunk
1fad8 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
1fad9 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a  */.  u8 zChunk[J
1fada 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1fadb 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20  ];   /* Content 
1fadc 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f  of this chunk */
1fadd 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
1fade 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
1fadf 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 73 20  bject serves as 
1fae0 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68  a cursor into th
1fae1 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
1fae2 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  al..** The curso
1fae3 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  r can be either 
1fae4 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77  for reading or w
1fae5 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63  riting..*/.struc
1fae6 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20  t FilePoint {.  
1fae7 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f  sqlite3_int64 iO
1fae8 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1fae9 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74  /* Offset from t
1faea 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
1faeb 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69  the file */.  Fi
1faec 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b  leChunk *pChunk;
1faed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1faee 20 53 70 65 63 69 66 69 63 20 63 68 75 6e 6b 20   Specific chunk 
1faef 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72 73 6f  into which curso
1faf0 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a  r points */.};..
1faf1 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62 63 6c  /*.** This subcl
1faf2 61 73 73 20 69 73 20 61 20 73 75 62 63 6c 61 73  ass is a subclas
1faf3 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c  s of sqlite3_fil
1faf4 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20 6d 65  e.  Each open me
1faf5 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  mory-journal.** 
1faf6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
1faf7 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f  f this class..*/
1faf8 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e  .struct MemJourn
1faf9 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  al {.  sqlite3_i
1fafa 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
1fafb 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74  od;    /* Parent
1fafc 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42 45 20   class. MUST BE 
1fafd 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43  FIRST */.  FileC
1fafe 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 20 20  hunk *pFirst;   
1faff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65             /* He
1fb00 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  ad of in-memory 
1fb01 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20  chunk-list */.  
1fb02 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69  FilePoint endpoi
1fb03 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
1fb04 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
1fb05 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
1fb06 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74  e */.  FilePoint
1fb07 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 20 20   readpoint;     
1fb08 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1fb09 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  r to the end of 
1fb0a 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 28 29  the last xRead()
1fb0b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
1fb0c 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ad data from the
1fb0d 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
1fb0e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69  al file.  This i
1fb0f 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
1fb10 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73  tion.** of the s
1fb11 71 6c 69 74 65 33 5f 76 66 73 2e 78 52 65 61 64  qlite3_vfs.xRead
1fb12 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
1fb13 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65  ic int memjrnlRe
1fb14 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  ad(.  sqlite3_fi
1fb15 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20  le *pJfd,    /* 
1fb16 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
1fb17 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72   from which to r
1fb18 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a  ead */.  void *z
1fb19 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
1fb1a 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c  /* Put the resul
1fb1b 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
1fb1c 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20   iAmt,          
1fb1d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1fb1e 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a   bytes to read *
1fb1f 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
1fb20 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65   iOfst     /* Be
1fb21 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74  gin reading at t
1fb22 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b  his offset */.){
1fb23 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70  .  MemJournal *p
1fb24 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a   = (MemJournal *
1fb25 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75  )pJfd;.  u8 *zOu
1fb26 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20  t = zBuf;.  int 
1fb27 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20  nRead = iAmt;.  
1fb28 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  int iChunkOffset
1fb29 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ;.  FileChunk *p
1fb2a 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c  Chunk;..  /* SQL
1fb2b 69 74 65 20 6e 65 76 65 72 20 74 72 69 65 73 20  ite never tries 
1fb2c 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65  to read past the
1fb2d 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61   end of a rollba
1fb2e 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ck journal file 
1fb2f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4f 66  */.  assert( iOf
1fb30 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70  st+iAmt<=p->endp
1fb31 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a  oint.iOffset );.
1fb32 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 64 70 6f  .  if( p->readpo
1fb33 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66  int.iOffset!=iOf
1fb34 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29  st || iOfst==0 )
1fb35 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  {.    sqlite3_in
1fb36 74 36 34 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20  t64 iOff = 0;.  
1fb37 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e    for(pChunk=p->
1fb38 70 46 69 72 73 74 3b 20 0a 20 20 20 20 20 20 20  pFirst; .       
1fb39 20 41 4c 57 41 59 53 28 70 43 68 75 6e 6b 29 20   ALWAYS(pChunk) 
1fb3a 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c  && (iOff+JOURNAL
1fb3b 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66  _CHUNKSIZE)<=iOf
1fb3c 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75  st;.        pChu
1fb3d 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74  nk=pChunk->pNext
1fb3e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 4f  .    ){.      iO
1fb3f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48  ff += JOURNAL_CH
1fb40 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20  UNKSIZE;.    }. 
1fb41 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 68 75   }else{.    pChu
1fb42 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e  nk = p->readpoin
1fb43 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20  t.pChunk;.  }.. 
1fb44 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20   iChunkOffset = 
1fb45 28 69 6e 74 29 28 69 4f 66 73 74 25 4a 4f 55 52  (int)(iOfst%JOUR
1fb46 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a  NAL_CHUNKSIZE);.
1fb47 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69    do {.    int i
1fb48 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f  Space = JOURNAL_
1fb49 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75  CHUNKSIZE - iChu
1fb4a 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e  nkOffset;.    in
1fb4b 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52  t nCopy = MIN(nR
1fb4c 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48  ead, (JOURNAL_CH
1fb4d 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b  UNKSIZE - iChunk
1fb4e 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 20 6d 65  Offset));.    me
1fb4f 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 43 68 75  mcpy(zOut, &pChu
1fb50 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e  nk->zChunk[iChun
1fb51 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29  kOffset], nCopy)
1fb52 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43  ;.    zOut += nC
1fb53 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d  opy;.    nRead -
1fb54 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 69 43  = iSpace;.    iC
1fb55 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a  hunkOffset = 0;.
1fb56 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 65 61 64    } while( nRead
1fb57 3e 3d 30 20 26 26 20 28 70 43 68 75 6e 6b 3d 70  >=0 && (pChunk=p
1fb58 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30  Chunk->pNext)!=0
1fb59 20 26 26 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20   && nRead>0 );. 
1fb5a 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f   p->readpoint.iO
1fb5b 66 66 73 65 74 20 3d 20 69 4f 66 73 74 2b 69 41  ffset = iOfst+iA
1fb5c 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69  mt;.  p->readpoi
1fb5d 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 43 68 75  nt.pChunk = pChu
1fb5e 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  nk;..  return SQ
1fb5f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1fb60 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1fb61 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1fb62 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57  tic int memjrnlW
1fb63 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
1fb64 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f  file *pJfd,    /
1fb65 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
1fb66 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f  le into which to
1fb67 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73   write */.  cons
1fb68 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20  t void *zBuf,   
1fb69 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 61 20     /* Take data 
1fb6a 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 66 72  to be written fr
1fb6b 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  om here */.  int
1fb6c 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20   iAmt,          
1fb6d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1fb6e 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20   bytes to write 
1fb6f 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  */.  sqlite_int6
1fb70 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42  4 iOfst     /* B
1fb71 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
1fb72 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e 74 6f  this offset into
1fb73 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a   the file */.){.
1fb74 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20    MemJournal *p 
1fb75 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29  = (MemJournal *)
1fb76 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69  pJfd;.  int nWri
1fb77 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20  te = iAmt;.  u8 
1fb78 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 20 2a 29  *zWrite = (u8 *)
1fb79 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69  zBuf;..  /* An i
1fb7a 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
1fb7b 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c   file should onl
1fb7c 79 20 65 76 65 72 20 62 65 20 61 70 70 65 6e 64  y ever be append
1fb7d 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20  ed to. Random.  
1fb7e 2a 2a 20 61 63 63 65 73 73 20 77 72 69 74 65 73  ** access writes
1fb7f 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65   are not require
1fb80 64 20 62 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a  d by sqlite..  *
1fb81 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4f 66 73  /.  assert( iOfs
1fb82 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69  t==p->endpoint.i
1fb83 4f 66 66 73 65 74 20 29 3b 0a 20 20 55 4e 55 53  Offset );.  UNUS
1fb84 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f 66  ED_PARAMETER(iOf
1fb85 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e  st);..  while( n
1fb86 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 20 20 46  Write>0 ){.    F
1fb87 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b  ileChunk *pChunk
1fb88 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70   = p->endpoint.p
1fb89 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69  Chunk;.    int i
1fb8a 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69  ChunkOffset = (i
1fb8b 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e  nt)(p->endpoint.
1fb8c 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f  iOffset%JOURNAL_
1fb8d 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20  CHUNKSIZE);.    
1fb8e 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e  int iSpace = MIN
1fb8f 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c  (nWrite, JOURNAL
1fb90 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68  _CHUNKSIZE - iCh
1fb91 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20  unkOffset);..   
1fb92 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65   if( iChunkOffse
1fb93 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  t==0 ){.      /*
1fb94 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65   New chunk is re
1fb95 71 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64  quired to extend
1fb96 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20   the file. */.  
1fb97 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70      FileChunk *p
1fb98 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  New = sqlite3_ma
1fb99 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65  lloc(sizeof(File
1fb9a 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69  Chunk));.      i
1fb9b 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20  f( !pNew ){.    
1fb9c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1fb9d 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20  E_IOERR_NOMEM;. 
1fb9e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65       }.      pNe
1fb9f 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  w->pNext = 0;.  
1fba0 20 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29      if( pChunk )
1fba1 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1fba2 28 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20  ( p->pFirst );. 
1fba3 20 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70         pChunk->p
1fba4 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  Next = pNew;.   
1fba5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fba6 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46    assert( !p->pF
1fba7 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20  irst );.        
1fba8 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77  p->pFirst = pNew
1fba9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1fbaa 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75  p->endpoint.pChu
1fbab 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d  nk = pNew;.    }
1fbac 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d  ..    memcpy(&p-
1fbad 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b  >endpoint.pChunk
1fbae 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f  ->zChunk[iChunkO
1fbaf 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20  ffset], zWrite, 
1fbb0 69 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72  iSpace);.    zWr
1fbb1 69 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20  ite += iSpace;. 
1fbb2 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70     nWrite -= iSp
1fbb3 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70  ace;.    p->endp
1fbb4 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20  oint.iOffset += 
1fbb5 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72  iSpace;.  }..  r
1fbb6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1fbb7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  .}../*.** Trunca
1fbb8 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  te the file..*/.
1fbb9 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1fbba 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  nlTruncate(sqlit
1fbbb 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1fbbc 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65  qlite_int64 size
1fbbd 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1fbbe 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1fbbf 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43   *)pJfd;.  FileC
1fbc0 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20  hunk *pChunk;.  
1fbc1 61 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b  assert(size==0);
1fbc2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1fbc3 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68  TER(size);.  pCh
1fbc4 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b  unk = p->pFirst;
1fbc5 0a 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b  .  while( pChunk
1fbc6 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e   ){.    FileChun
1fbc7 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b  k *pTmp = pChunk
1fbc8 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70  ;.    pChunk = p
1fbc9 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  Chunk->pNext;.  
1fbca 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1fbcb 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Tmp);.  }.  sqli
1fbcc 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
1fbcd 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72  n(pJfd);.  retur
1fbce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1fbcf 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
1fbd0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1fbd1 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65  int memjrnlClose
1fbd2 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1fbd3 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54  Jfd){.  memjrnlT
1fbd4 72 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29  runcate(pJfd, 0)
1fbd5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1fbd6 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
1fbd7 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a  Sync the file..*
1fbd8 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20  *.** Syncing an 
1fbd9 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61  in-memory journa
1fbda 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41  l is a no-op.  A
1fbdb 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 74 68 69  nd, in fact, thi
1fbdc 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
1fbdd 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 6e 20  never called in 
1fbde 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d  a working implem
1fbdf 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 69 73 20  entation.  This 
1fbe0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
1fbe1 2a 20 65 78 69 73 74 73 20 70 75 72 65 6c 79 20  * exists purely 
1fbe2 61 73 20 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79  as a contingency
1fbe3 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d  , in case some m
1fbe4 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f  alfunction in so
1fbe5 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74  me other.** part
1fbe6 20 6f 66 20 53 51 4c 69 74 65 20 63 61 75 73 65   of SQLite cause
1fbe7 73 20 53 79 6e 63 20 74 6f 20 62 65 20 63 61 6c  s Sync to be cal
1fbe8 6c 65 64 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a  led by mistake..
1fbe9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
1fbea 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65  mjrnlSync(sqlite
1fbeb 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c  3_file *NotUsed,
1fbec 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 20   int NotUsed2){ 
1fbed 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20    /*NO_TEST*/.  
1fbee 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1fbef 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
1fbf0 65 64 32 29 3b 20 20 20 20 20 20 20 20 20 20 20  ed2);           
1fbf1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e               /*N
1fbf2 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73 73 65 72  O_TEST*/.  asser
1fbf3 74 28 20 30 20 29 3b 20 20 20 20 20 20 20 20 20  t( 0 );         
1fbf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbf6 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53          /*NO_TES
1fbf7 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  T*/.  return SQL
1fbf8 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
1fbf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbfb 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d     /*NO_TEST*/.}
1fbfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1fc00 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a  NO_TEST*/../*.**
1fc01 20 51 75 65 72 79 20 74 68 65 20 73 69 7a 65 20   Query the size 
1fc02 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62  of the file in b
1fc03 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ytes..*/.static 
1fc04 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53  int memjrnlFileS
1fc05 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
1fc06 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69   *pJfd, sqlite_i
1fc07 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20  nt64 *pSize){.  
1fc08 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1fc09 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1fc0a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28  fd;.  *pSize = (
1fc0b 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 70 2d  sqlite_int64) p-
1fc0c 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65  >endpoint.iOffse
1fc0d 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  t;.  return SQLI
1fc0e 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1fc0f 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73  Table of methods
1fc10 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20   for MemJournal 
1fc11 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a  sqlite3_file obj
1fc12 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ect..*/.static s
1fc13 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
1fc14 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72  _methods MemJour
1fc15 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  nalMethods = {. 
1fc16 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
1fc17 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a     /* iVersion *
1fc18 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65  /.  memjrnlClose
1fc19 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20  ,     /* xClose 
1fc1a 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64  */.  memjrnlRead
1fc1b 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20  ,      /* xRead 
1fc1c 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74  */.  memjrnlWrit
1fc1d 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65  e,     /* xWrite
1fc1e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75   */.  memjrnlTru
1fc1f 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e  ncate,  /* xTrun
1fc20 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e  cate */.  memjrn
1fc21 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78  lSync,      /* x
1fc22 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e  Sync */.  memjrn
1fc23 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78  lFileSize,  /* x
1fc24 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c  FileSize */.  0,
1fc25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc26 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c  /* xLock */.  0,
1fc27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc28 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20  /* xUnlock */.  
1fc29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1fc2a 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
1fc2b 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  vedLock */.  0, 
1fc2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fc2d 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a  * xFileControl *
1fc2e 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1fc2f 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72        /* xSector
1fc30 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20  Size */.  0     
1fc31 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1fc32 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
1fc33 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  stics */.};../* 
1fc34 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e  .** Open a journ
1fc35 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  al file..*/.SQLI
1fc36 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1fc37 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
1fc38 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69  lOpen(sqlite3_fi
1fc39 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d  le *pJfd){.  Mem
1fc3a 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
1fc3b 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
1fc3c 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
1fc3d 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
1fc3e 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70  p) );.  memset(p
1fc3f 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a  , 0, sqlite3MemJ
1fc40 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20  ournalSize());. 
1fc41 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d   p->pMethod = &M
1fc42 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73  emJournalMethods
1fc43 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1fc44 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69  n true if the fi
1fc45 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73 73 65 64  le-handle passed
1fc46 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
1fc47 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d  is .** an in-mem
1fc48 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a  ory journal .*/.
1fc49 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fc4a 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a  nt sqlite3IsMemJ
1fc4b 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66  ournal(sqlite3_f
1fc4c 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65  ile *pJfd){.  re
1fc4d 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68  turn pJfd->pMeth
1fc4e 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c  ods==&MemJournal
1fc4f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a  Methods;.}../* .
1fc50 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1fc51 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
1fc52 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20  quired to store 
1fc53 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61  a MemJournal tha
1fc54 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56  t uses vfs.** pV
1fc55 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  fs to create the
1fc56 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64   underlying on-d
1fc57 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51  isk files..*/.SQ
1fc58 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1fc59 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
1fc5a 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20  alSize(void){.  
1fc5b 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65  return sizeof(Me
1fc5c 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a  mJournal);.}../*
1fc5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1fc5e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e  d of memjournal.
1fc5f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1fc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1fc62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1fc63 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e  gin file walker.
1fc64 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1fc65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1fc67 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20  .** 2008 August 
1fc68 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  16.**.** The aut
1fc69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1fc6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1fc6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1fc6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1fc6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1fc6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1fc6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1fc70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1fc71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1fc72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1fc73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1fc74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1fc75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1fc76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1fc77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1fc78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1fc79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1fc7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc7e 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1fc7f 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73  ontains routines
1fc80 20 75 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e   used for walkin
1fc81 67 20 74 68 65 20 70 61 72 73 65 72 20 74 72 65  g the parser tre
1fc82 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20  e for.** an SQL 
1fc83 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a  statement..*/...
1fc84 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
1fc85 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
1fc86 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
1fc87 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  ack once for eac
1fc88 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65  h node.** of the
1fc89 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69   expression, whi
1fc8a 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28  le decending.  (
1fc8b 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1fc8c 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  the callback.** 
1fc8d 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72  is invoked befor
1fc8e 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64  e visiting child
1fc8f 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ren.).**.** The 
1fc90 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1fc91 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73  m the callback s
1fc92 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20  hould be one of 
1fc93 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e  the WRC_*.** con
1fc94 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66  stants to specif
1fc95 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64  y how to proceed
1fc96 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a   with the walk..
1fc97 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e  **.**    WRC_Con
1fc98 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69  tinue      Conti
1fc99 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64  nue descending d
1fc9a 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a  own the tree..**
1fc9b 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65  .**    WRC_Prune
1fc9c 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20           Do not 
1fc9d 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69  descend into chi
1fc9e 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61  ld nodes.  But a
1fc9f 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20  llow.**         
1fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
1fca1 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75   walk to continu
1fca2 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e  e with sibling n
1fca3 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57  odes..**.**    W
1fca4 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20  RC_Abort        
1fca5 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c   Do no more call
1fca6 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74  backs.  Unwind t
1fca7 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20  he stack and.** 
1fca8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fca9 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 20       return the 
1fcaa 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63  top-level walk c
1fcab 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  all..**.** The r
1fcac 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
1fcad 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1fcae 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62   WRC_Abort to ab
1fcaf 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77  andon the tree w
1fcb0 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43  alk.** and WRC_C
1fcb1 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69  ontinue to conti
1fcb2 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nue..*/.SQLITE_P
1fcb3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1fcb4 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65  e3WalkExpr(Walke
1fcb5 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72  r *pWalker, Expr
1fcb6 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20   *pExpr){.  int 
1fcb7 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d  rc;.  if( pExpr=
1fcb8 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  =0 ) return WRC_
1fcb9 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 74  Continue;.  test
1fcba 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f  case( ExprHasPro
1fcbb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1fcbc 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20  TokenOnly) );.  
1fcbd 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61  testcase( ExprHa
1fcbe 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
1fcbf 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a   EP_Reduced) );.
1fcc0 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e    rc = pWalker->
1fcc1 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57  xExprCallback(pW
1fcc2 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20  alker, pExpr);. 
1fcc3 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e   if( rc==WRC_Con
1fcc4 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20  tinue.          
1fcc5 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41      && !ExprHasA
1fcc6 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
1fcc7 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29  ,EP_TokenOnly) )
1fcc8 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1fcc9 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1fcca 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  r, pExpr->pLeft)
1fccb 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fccc 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ort;.    if( sql
1fccd 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1fcce 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69  lker, pExpr->pRi
1fccf 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  ght) ) return WR
1fcd0 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28  C_Abort;.    if(
1fcd1 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1fcd2 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
1fcd3 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69  lect) ){.      i
1fcd4 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  f( sqlite3WalkSe
1fcd5 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45  lect(pWalker, pE
1fcd6 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20  xpr->x.pSelect) 
1fcd7 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fcd8 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  rt;.    }else{. 
1fcd9 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fcda 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61  WalkExprList(pWa
1fcdb 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70  lker, pExpr->x.p
1fcdc 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57  List) ) return W
1fcdd 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  RC_Abort;.    }.
1fcde 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
1fcdf 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a  & WRC_Abort;.}..
1fce0 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
1fce1 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72  e3WalkExpr() for
1fce2 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
1fce3 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75  n in list p or u
1fce4 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74  ntil.** an abort
1fce5 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e   request is seen
1fce6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fce7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1fce8 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b  alkExprList(Walk
1fce9 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1fcea 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74  rList *p){.  int
1fceb 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70   i;.  struct Exp
1fcec 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
1fced 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  m;.  if( p ){.  
1fcee 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72    for(i=p->nExpr
1fcef 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e  , pItem=p->a; i>
1fcf0 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
1fcf1 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1fcf2 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fcf3 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  ker, pItem->pExp
1fcf4 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  r) ) return WRC_
1fcf5 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Abort;.    }.  }
1fcf6 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f  .  return WRC_Co
1fcf7 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ntinue;.}../*.**
1fcf8 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73   Walk all expres
1fcf9 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64  sions associated
1fcfa 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 61   with SELECT sta
1fcfb 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a  tement p.  Do.**
1fcfc 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20   not invoke the 
1fcfd 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20  SELECT callback 
1fcfe 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66  on p, but do (of
1fcff 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a   course) invoke.
1fd00 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c  ** any expr call
1fd01 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54  backs and SELECT
1fd02 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20   callbacks that 
1fd03 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65  come from subque
1fd04 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  ries..** Return 
1fd05 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
1fd06 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51  _Continue..*/.SQ
1fd07 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1fd08 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1fd09 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70  ctExpr(Walker *p
1fd0a 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1fd0b 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  p){.  if( sqlite
1fd0c 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fd0d 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74  alker, p->pEList
1fd0e 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fd0f 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1fd10 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fd11 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20  ker, p->pWhere) 
1fd12 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fd13 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1fd14 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fd15 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70  alker, p->pGroup
1fd16 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  By) ) return WRC
1fd17 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1fd18 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1fd19 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e  alker, p->pHavin
1fd1a 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  g) ) return WRC_
1fd1b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1fd1c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74  ite3WalkExprList
1fd1d 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72  (pWalker, p->pOr
1fd1e 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e 20  derBy) ) return 
1fd1f 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28  WRC_Abort;.  if(
1fd20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1fd21 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69  (pWalker, p->pLi
1fd22 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  mit) ) return WR
1fd23 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1fd24 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1fd25 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73  Walker, p->pOffs
1fd26 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  et) ) return WRC
1fd27 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e  _Abort;.  return
1fd28 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1fd29 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65  ../*.** Walk the
1fd2a 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 73   parse trees ass
1fd2b 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c  ociated with all
1fd2c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
1fd2d 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  he.** FROM claus
1fd2e 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74  e of SELECT stat
1fd2f 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74  ement p.  Do not
1fd30 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65   invoke the sele
1fd31 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f  ct.** callback o
1fd32 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f  n p, but do invo
1fd33 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52  ke it on each FR
1fd34 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 65  OM clause subque
1fd35 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79  ry.** and on any
1fd36 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 74   subqueries furt
1fd37 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20  her down in the 
1fd38 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a  tree.  Return .*
1fd39 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57  * WRC_Abort or W
1fd3a 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a  RC_Continue;.*/.
1fd3b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fd3c 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  nt sqlite3WalkSe
1fd3d 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20  lectFrom(Walker 
1fd3e 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
1fd3f 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20   *p){.  SrcList 
1fd40 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a  *pSrc;.  int i;.
1fd41 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1fd42 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20  _item *pItem;.. 
1fd43 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b   pSrc = p->pSrc;
1fd44 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53  .  if( ALWAYS(pS
1fd45 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  rc) ){.    for(i
1fd46 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74  =pSrc->nSrc, pIt
1fd47 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b  em=pSrc->a; i>0;
1fd48 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
1fd49 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1fd4a 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1fd4b 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  ker, pItem->pSel
1fd4c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ect) ){.        
1fd4d 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fd4e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1fd4f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43    }.  return WRC
1fd50 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f  _Continue;.} ../
1fd51 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1fd52 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20  3WalkExpr() for 
1fd53 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
1fd54 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65   in Select state
1fd55 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b  ment p..** Invok
1fd56 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  e sqlite3WalkSel
1fd57 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65  ect() for subque
1fd58 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
1fd59 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f   clause and.** o
1fd5a 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  n the compound s
1fd5b 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e  elect chain, p->
1fd5c 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65  pPrior..**.** Re
1fd5d 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1fd5e 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63  e under normal c
1fd5f 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75  onditions.  Retu
1fd60 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a  rn WRC_Abort if.
1fd61 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61  ** there is an a
1fd62 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a  bort request..**
1fd63 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65  .** If the Walke
1fd64 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  r does not have 
1fd65 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  an xSelectCallba
1fd66 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72  ck() then this r
1fd67 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
1fd68 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57  o-op returning W
1fd69 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a  RC_Continue..*/.
1fd6a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fd6b 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  nt sqlite3WalkSe
1fd6c 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61  lect(Walker *pWa
1fd6d 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1fd6e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  {.  int rc;.  if
1fd6f 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65  ( p==0 || pWalke
1fd70 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  r->xSelectCallba
1fd71 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ck==0 ) return W
1fd72 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72  RC_Continue;.  r
1fd73 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  c = WRC_Continue
1fd74 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b  ;.  while( p  ){
1fd75 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65  .    rc = pWalke
1fd76 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  r->xSelectCallba
1fd77 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a  ck(pWalker, p);.
1fd78 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65      if( rc ) bre
1fd79 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ak;.    if( sqli
1fd7a 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
1fd7b 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20  r(pWalker, p) ) 
1fd7c 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fd7d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1fd7e 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28  3WalkSelectFrom(
1fd7f 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65  pWalker, p) ) re
1fd80 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1fd81 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f      p = p->pPrio
1fd82 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
1fd83 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a  rc & WRC_Abort;.
1fd84 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1fd85 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72  ** End of walker
1fd86 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1fd87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd89 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1fd8a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65  ** Begin file re
1fd8b 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  solve.c ********
1fd8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fd8e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75  */./*.** 2008 Au
1fd8f 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68  gust 18.**.** Th
1fd90 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1fd91 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1fd92 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1fd93 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1fd94 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1fd95 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1fd96 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1fd97 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1fd98 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1fd99 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1fd9a 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1fd9b 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1fd9c 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1fd9d 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1fd9e 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1fd9f 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1fda0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1fda1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fda2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fda3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fda4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fda5 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1fda6 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1fda7 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f  routines used fo
1fda8 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61  r walking the pa
1fda9 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a  rser tree and.**
1fdaa 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65   resolve all ide
1fdab 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f  ntifiers by asso
1fdac 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74  ciating them wit
1fdad 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  h a particular.*
1fdae 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  * table and colu
1fdaf 6d 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75  mn..*/../*.** Tu
1fdb0 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70  rn the pExpr exp
1fdb1 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20  ression into an 
1fdb2 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43  alias for the iC
1fdb3 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  ol-th column of 
1fdb4 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
1fdb5 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a  t in pEList..**.
1fdb6 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
1fdb7 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61   set column is a
1fdb8 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72   simple column r
1fdb9 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74  eference, then t
1fdba 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
1fdbb 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f  akes an exact co
1fdbc 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79  py.  But for any
1fdbd 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65   other kind of e
1fdbe 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a  xpression, this.
1fdbf 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20  ** routine make 
1fdc0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
1fdc1 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20  sult set column 
1fdc2 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
1fdc3 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20  to the.** TK_AS 
1fdc4 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54  operator.  The T
1fdc5 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61  K_AS operator ca
1fdc6 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73  uses the express
1fdc7 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61  ion to be.** eva
1fdc8 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65  luated just once
1fdc9 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64   and then reused
1fdca 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e   for each alias.
1fdcb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f  .**.** The reaso
1fdcc 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e  n for suppressin
1fdcd 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d  g the TK_AS term
1fdce 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   when the expres
1fdcf 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65  sion is a simple
1fdd0 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  .** column refer
1fdd1 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20  ence is so that 
1fdd2 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  the column refer
1fdd3 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63  ence will be rec
1fdd4 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73  ognized as.** us
1fdd5 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20  able by indices 
1fdd6 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45  within the WHERE
1fdd7 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
1fdd8 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a  ng logic. .**.**
1fdd9 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41   Hack:  The TK_A
1fdda 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e  S operator is in
1fddb 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65  hibited if zType
1fddc 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20  [0]=='G'.  This 
1fddd 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e  means.** that in
1fdde 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75   a GROUP BY clau
1fddf 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69  se, the expressi
1fde0 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  on is evaluated 
1fde1 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a  twice.  Hence:.*
1fde2 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1fde3 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c  random()%5 AS x,
1fde4 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74   count(*) FROM t
1fde5 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a  ab GROUP BY x.**
1fde6 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e  .** Is equivalen
1fde7 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  t to:.**.**     
1fde8 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25  SELECT random()%
1fde9 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29  5 AS x, count(*)
1fdea 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20   FROM tab GROUP 
1fdeb 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a  BY random()%5.**
1fdec 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f  .** The result o
1fded 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20  f random()%5 in 
1fdee 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
1fdef 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20  use is probably 
1fdf0 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f  different.** fro
1fdf1 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  m the result in 
1fdf2 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20  the result-set. 
1fdf3 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68   We might fix th
1fdf4 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a  is someday.  Or.
1fdf5 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77  ** then again, w
1fdf6 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a  e might not....*
1fdf7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
1fdf8 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61  solveAlias(.  Pa
1fdf9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1fdfa 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
1fdfb 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
1fdfc 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
1fdfd 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20      /* A result 
1fdfe 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  set */.  int iCo
1fdff 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
1fe00 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  /* A column in t
1fe01 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1fe02 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  0..pEList->nExpr
1fe03 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  -1 */.  Expr *pE
1fe04 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  xpr,           /
1fe05 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73  * Transform this
1fe06 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74   into an alias t
1fe07 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  o the result set
1fe08 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1fe09 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20   *zType      /* 
1fe0a 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45  "GROUP" or "ORDE
1fe0b 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20  R" or "" */.){. 
1fe0c 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20   Expr *pOrig;   
1fe0d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
1fe0e 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
1fe0f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1fe10 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b  */.  Expr *pDup;
1fe11 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1fe12 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a  opy of pOrig */.
1fe13 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1fe14 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1fe15 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1fe16 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
1fe17 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  ( iCol>=0 && iCo
1fe18 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l<pEList->nExpr 
1fe19 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c  );.  pOrig = pEL
1fe1a 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
1fe1b 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  pr;.  assert( pO
1fe1c 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  rig!=0 );.  asse
1fe1d 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73  rt( pOrig->flags
1fe1e 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29   & EP_Resolved )
1fe1f 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1fe20 3e 64 62 3b 0a 20 20 69 66 28 20 70 4f 72 69 67  >db;.  if( pOrig
1fe21 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op!=TK_COLUMN 
1fe22 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27  && zType[0]!='G'
1fe23 20 29 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73   ){.    pDup = s
1fe24 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1fe25 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20  , pOrig, 0);.   
1fe26 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 50   pDup = sqlite3P
1fe27 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
1fe28 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b  AS, pDup, 0, 0);
1fe29 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30  .    if( pDup==0
1fe2a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
1fe2b 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f  f( pEList->a[iCo
1fe2c 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a  l].iAlias==0 ){.
1fe2d 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b        pEList->a[
1fe2e 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28  iCol].iAlias = (
1fe2f 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e  u16)(++pParse->n
1fe30 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20  Alias);.    }.  
1fe31 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d    pDup->iTable =
1fe32 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d   pEList->a[iCol]
1fe33 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 65 6c 73 65  .iAlias;.  }else
1fe34 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
1fe35 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 49  erty(pOrig, EP_I
1fe36 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70 4f 72 69  ntValue) || pOri
1fe37 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29  g->u.zToken==0 )
1fe38 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c  {.    pDup = sql
1fe39 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
1fe3a 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 69  pOrig, 0);.    i
1fe3b 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1fe3c 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
1fe3d 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d    char *zToken =
1fe3e 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e   pOrig->u.zToken
1fe3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 54  ;.    assert( zT
1fe40 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 70  oken!=0 );.    p
1fe41 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d  Orig->u.zToken =
1fe42 20 30 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73   0;.    pDup = s
1fe43 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1fe44 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20  , pOrig, 0);.   
1fe45 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e   pOrig->u.zToken
1fe46 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69   = zToken;.    i
1fe47 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1fe48 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  urn;.    assert(
1fe49 20 28 70 44 75 70 2d 3e 66 6c 61 67 73 20 26 20   (pDup->flags & 
1fe4a 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54  (EP_Reduced|EP_T
1fe4b 6f 6b 65 6e 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b  okenOnly))==0 );
1fe4c 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73  .    pDup->flags
1fe4d 32 20 7c 3d 20 45 50 32 5f 4d 61 6c 6c 6f 63 65  2 |= EP2_Malloce
1fe4e 64 54 6f 6b 65 6e 3b 0a 20 20 20 20 70 44 75 70  dToken;.    pDup
1fe4f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c  ->u.zToken = sql
1fe50 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1fe51 20 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20   zToken);.  }.  
1fe52 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73  if( pExpr->flags
1fe53 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
1fe54 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70 43   ){.    pDup->pC
1fe55 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f  oll = pExpr->pCo
1fe56 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c  ll;.    pDup->fl
1fe57 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
1fe58 6c 61 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  late;.  }..  /* 
1fe59 42 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73  Before calling s
1fe5a 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1fe5b 28 29 2c 20 73 65 74 20 74 68 65 20 45 50 5f 53  (), set the EP_S
1fe5c 74 61 74 69 63 20 66 6c 61 67 2e 20 54 68 69 73  tatic flag. This
1fe5d 20 0a 20 20 2a 2a 20 70 72 65 76 65 6e 74 73 20   .  ** prevents 
1fe5e 45 78 70 72 44 65 6c 65 74 65 28 29 20 66 72 6f  ExprDelete() fro
1fe5f 6d 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 45  m deleting the E
1fe60 78 70 72 20 73 74 72 75 63 74 75 72 65 20 69 74  xpr structure it
1fe61 73 65 6c 66 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 77  self,.  ** allow
1fe62 69 6e 67 20 69 74 20 74 6f 20 62 65 20 72 65 70  ing it to be rep
1fe63 6f 70 75 6c 61 74 65 64 20 62 79 20 74 68 65 20  opulated by the 
1fe64 6d 65 6d 63 70 79 28 29 20 6f 6e 20 74 68 65 20  memcpy() on the 
1fe65 66 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 2e 0a  following line..
1fe66 20 20 2a 2f 0a 20 20 45 78 70 72 53 65 74 50 72    */.  ExprSetPr
1fe67 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1fe68 5f 53 74 61 74 69 63 29 3b 0a 20 20 73 71 6c 69  _Static);.  sqli
1fe69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1fe6a 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63  , pExpr);.  memc
1fe6b 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20  py(pExpr, pDup, 
1fe6c 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b  sizeof(*pExpr));
1fe6d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1fe6e 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f  (db, pDup);.}../
1fe6f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e  *.** Given the n
1fe70 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  ame of a column 
1fe71 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e  of the form X.Y.
1fe72 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74  Z or Y.Z or just
1fe73 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74   Z, look up.** t
1fe74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  hat name in the 
1fe75 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61  set of source ta
1fe76 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74  bles in pSrcList
1fe77 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45   and make the pE
1fe78 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  xpr .** expressi
1fe79 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61  on node refer ba
1fe7a 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63  ck to that sourc
1fe7b 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66  e column.  The f
1fe7c 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73  ollowing changes
1fe7d 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20  .** are made to 
1fe7e 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  pExpr:.**.**    
1fe7f 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20  pExpr->iDb      
1fe80 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e 64       Set the ind
1fe81 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  ex in db->aDb[] 
1fe82 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1fe83 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  X.**            
1fe84 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 76               (ev
1fe85 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69  en if X is impli
1fe86 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  ed)..**    pExpr
1fe87 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20  ->iTable        
1fe88 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f  Set to the curso
1fe89 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
1fe8a 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a   table obtained.
1fe8b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1fe8c 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20             from 
1fe8d 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20  pSrcList..**    
1fe8e 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20  pExpr->pTab     
1fe8f 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74       Points to t
1fe90 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
1fe91 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20  re of X.Y (even 
1fe92 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  if.**           
1fe93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20                X 
1fe94 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70  and/or Y are imp
1fe95 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78  lied.).**    pEx
1fe96 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20  pr->iColumn     
1fe97 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c    Set to the col
1fe98 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69  umn number withi
1fe99 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  n the table..** 
1fe9a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20     pExpr->op    
1fe9b 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 54          Set to T
1fe9c 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20  K_COLUMN..**    
1fe9d 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20  pExpr->pLeft    
1fe9e 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73       Any express
1fe9f 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20  ion this points 
1fea0 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a  to is deleted.**
1fea1 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68      pExpr->pRigh
1fea2 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70  t        Any exp
1fea3 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69  ression this poi
1fea4 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65  nts to is delete
1fea5 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 44 62  d..**.** The zDb
1fea6 20 76 61 72 69 61 62 6c 65 20 69 73 20 74 68 65   variable is the
1fea7 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
1fea8 61 62 61 73 65 20 28 74 68 65 20 22 58 22 29 2e  abase (the "X").
1fea9 20 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79    This value may
1feaa 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e   be.** NULL mean
1feab 69 6e 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73  ing that name is
1feac 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a   of the form Y.Z
1fead 20 6f 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69   or Z.  Any avai
1feae 6c 61 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a  lable database.*
1feaf 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20  * can be used.  
1feb0 54 68 65 20 7a 54 61 62 6c 65 20 76 61 72 69 61  The zTable varia
1feb1 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ble is the name 
1feb2 6f 66 20 74 68 65 20 74 61 62 6c 65 20 28 74 68  of the table (th
1feb3 65 20 22 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a  e "Y").  This.**
1feb4 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 4e 55   value can be NU
1feb5 4c 4c 20 69 66 20 7a 44 62 20 69 73 20 61 6c 73  LL if zDb is als
1feb6 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 7a 54 61 62  o NULL.  If zTab
1feb7 6c 65 20 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a  le is NULL it.**
1feb8 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
1feb9 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65  form of the name
1feba 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74 20 63   is Z and that c
1febb 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20  olumns from any 
1febc 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20  table.** can be 
1febd 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  used..**.** If t
1febe 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62  he name cannot b
1febf 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62  e resolved unamb
1fec0 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20  iguously, leave 
1fec1 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1fec2 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e  .** in pParse an
1fec3 64 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  d return WRC_Abo
1fec4 72 74 2e 20 20 52 65 74 75 72 6e 20 57 52 43 5f  rt.  Return WRC_
1fec5 50 72 75 6e 65 20 6f 6e 20 73 75 63 63 65 73 73  Prune on success
1fec6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1fec7 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61  lookupName(.  Pa
1fec8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1fec9 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
1feca 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  g context */.  c
1fecb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20  onst char *zDb, 
1fecc 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1fecd 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
1fece 61 69 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72  aining table, or
1fecf 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
1fed0 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 20 20 20   char *zTab,    
1fed1 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65  /* Name of table
1fed2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75   containing colu
1fed3 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  mn, or NULL */. 
1fed4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
1fed5 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  l,    /* Name of
1fed6 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a   the column. */.
1fed7 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1fed8 4e 43 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  NC,    /* The na
1fed9 6d 65 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20  me context used 
1feda 74 6f 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e  to resolve the n
1fedb 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ame */.  Expr *p
1fedc 45 78 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a  Expr          /*
1fedd 20 4d 61 6b 65 20 74 68 69 73 20 45 58 50 52 20   Make this EXPR 
1fede 6e 6f 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68  node point to th
1fedf 65 20 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d  e selected colum
1fee0 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  n */.){.  int i,
1fee1 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   j;            /
1fee2 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
1fee3 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30  */.  int cnt = 0
1fee4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fee5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1fee6 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c   of matching col
1fee7 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69  umn names */.  i
1fee8 6e 74 20 63 6e 74 54 61 62 20 3d 20 30 3b 20 20  nt cntTab = 0;  
1fee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1feea 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61   /* Number of ma
1feeb 74 63 68 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d  tching table nam
1feec 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  es */.  sqlite3 
1feed 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1feee 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
1feef 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1fef0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
1fef1 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1fef2 49 74 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55  Item;       /* U
1fef3 73 65 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  se for looping o
1fef4 76 65 72 20 70 53 72 63 4c 69 73 74 20 69 74 65  ver pSrcList ite
1fef5 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  ms */.  struct S
1fef6 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61  rcList_item *pMa
1fef7 74 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65  tch = 0;  /* The
1fef8 20 6d 61 74 63 68 69 6e 67 20 70 53 72 63 4c 69   matching pSrcLi
1fef9 73 74 20 69 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d  st item */.  Nam
1fefa 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43  eContext *pTopNC
1fefb 20 3d 20 70 4e 43 3b 20 20 20 20 20 20 20 20 2f   = pNC;        /
1fefc 2a 20 46 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74  * First namecont
1fefd 65 78 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ext in the list 
1fefe 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  */.  Schema *pSc
1feff 68 65 6d 61 20 3d 20 30 3b 20 20 20 20 20 20 20  hema = 0;       
1ff00 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61         /* Schema
1ff01 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1ff02 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 72  on */.  int isTr
1ff03 69 67 67 65 72 20 3d 20 30 3b 0a 0a 20 20 61 73  igger = 0;..  as
1ff04 73 65 72 74 28 20 70 4e 43 20 29 3b 20 20 20 20  sert( pNC );    
1ff05 20 2f 2a 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e   /* the name con
1ff06 74 65 78 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e  text cannot be N
1ff07 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ULL. */.  assert
1ff08 28 20 7a 43 6f 6c 20 29 3b 20 20 20 20 2f 2a 20  ( zCol );    /* 
1ff09 54 68 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63  The Z in X.Y.Z c
1ff0a 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  annot be NULL */
1ff0b 0a 20 20 61 73 73 65 72 74 28 20 7e 45 78 70 72  .  assert( ~Expr
1ff0c 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
1ff0d 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  Expr, EP_TokenOn
1ff0e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
1ff0f 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
1ff10 7a 65 20 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e  ze the node to n
1ff11 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 20 70 45 78  o-match */.  pEx
1ff12 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b  pr->iTable = -1;
1ff13 0a 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d  .  pExpr->pTab =
1ff14 20 30 3b 0a 20 20 45 78 70 72 53 65 74 49 72 72   0;.  ExprSetIrr
1ff15 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b  educible(pExpr);
1ff16 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20  ..  /* Start at 
1ff17 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63  the inner-most c
1ff18 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20  ontext and move 
1ff19 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20  outward until a 
1ff1a 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a  match is found *
1ff1b 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26  /.  while( pNC &
1ff1c 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  & cnt==0 ){.    
1ff1d 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1ff1e 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  ;.    SrcList *p
1ff1f 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  SrcList = pNC->p
1ff20 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66  SrcList;..    if
1ff21 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20  ( pSrcList ){.  
1ff22 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
1ff23 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20  em=pSrcList->a; 
1ff24 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63  i<pSrcList->nSrc
1ff25 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1ff26 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a  .        Table *
1ff27 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e  pTab;.        in
1ff28 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43  t iDb;.        C
1ff29 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a  olumn *pCol;.  .
1ff2a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1ff2b 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
1ff2c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
1ff2d 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61  !=0 && pTab->zNa
1ff2e 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  me!=0 );.       
1ff2f 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1ff30 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1ff31 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1ff32 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1ff33 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a  pTab->nCol>0 );.
1ff34 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62          if( zTab
1ff35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
1ff36 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  ( pItem->zAlias 
1ff37 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
1ff38 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20  har *zTabName = 
1ff39 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20  pItem->zAlias;. 
1ff3a 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
1ff3b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
1ff3c 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30  abName, zTab)!=0
1ff3d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1ff3e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1ff3f 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
1ff40 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d  zTabName = pTab-
1ff41 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  >zName;.        
1ff42 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54      if( NEVER(zT
1ff43 61 62 4e 61 6d 65 3d 3d 30 29 20 7c 7c 20 73 71  abName==0) || sq
1ff44 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61  lite3StrICmp(zTa
1ff45 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20  bName, zTab)!=0 
1ff46 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1ff47 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
1ff48 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ff49 20 20 20 20 20 69 66 28 20 7a 44 62 21 3d 30 20       if( zDb!=0 
1ff4a 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1ff4b 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  p(db->aDb[iDb].z
1ff4c 4e 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b  Name, zDb)!=0 ){
1ff4d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63  .              c
1ff4e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
1ff4f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1ff50 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1ff51 20 20 20 20 20 69 66 28 20 30 3d 3d 28 63 6e 74       if( 0==(cnt
1ff52 54 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20  Tab++) ){.      
1ff53 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1ff54 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  e = pItem->iCurs
1ff55 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45  or;.          pE
1ff56 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  xpr->pTab = pTab
1ff57 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 63 68  ;.          pSch
1ff58 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
1ff59 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ema;.          p
1ff5a 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20  Match = pItem;. 
1ff5b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ff5c 20 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70   for(j=0, pCol=p
1ff5d 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61  Tab->aCol; j<pTa
1ff5e 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43  b->nCol; j++, pC
1ff5f 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ol++){.         
1ff60 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1ff61 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c  Cmp(pCol->zName,
1ff62 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
1ff63 20 20 20 20 20 20 20 20 20 49 64 4c 69 73 74 20           IdList 
1ff64 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 20  *pUsing;.       
1ff65 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
1ff66 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1ff67 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
1ff68 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
1ff69 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1ff6a 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  = pTab;.        
1ff6b 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74      pMatch = pIt
1ff6c 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  em;.            
1ff6d 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
1ff6e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
1ff6f 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75       /* Substitu
1ff70 74 65 20 74 68 65 20 72 6f 77 69 64 20 28 63 6f  te the rowid (co
1ff71 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 65  lumn -1) for the
1ff72 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
1ff73 20 4b 45 59 20 2a 2f 0a 20 20 20 20 20 20 20 20   KEY */.        
1ff74 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1ff75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50  mn = j==pTab->iP
1ff76 4b 65 79 20 3f 20 2d 31 20 3a 20 28 69 31 36 29  Key ? -1 : (i16)
1ff77 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  j;.            i
1ff78 66 28 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e  f( i<pSrcList->n
1ff79 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  Src-1 ){.       
1ff7a 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
1ff7b 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a  [1].jointype & J
1ff7c 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20  T_NATURAL ){.   
1ff7d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ff7e 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f 63  If this match oc
1ff7f 63 75 72 72 65 64 20 69 6e 20 74 68 65 20 6c 65  curred in the le
1ff80 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6e 61  ft table of a na
1ff81 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20  tural join,.    
1ff82 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
1ff83 68 65 6e 20 73 6b 69 70 20 74 68 65 20 72 69 67  hen skip the rig
1ff84 68 74 20 74 61 62 6c 65 20 74 6f 20 61 76 6f 69  ht table to avoi
1ff85 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61  d a duplicate ma
1ff86 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  tch */.         
1ff87 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a         pItem++;.
1ff88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff89 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  i++;.           
1ff8a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 55     }else if( (pU
1ff8b 73 69 6e 67 20 3d 20 70 49 74 65 6d 5b 31 5d 2e  sing = pItem[1].
1ff8c 70 55 73 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20  pUsing)!=0 ){.  
1ff8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ff8e 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f   If this match o
1ff8f 63 63 75 72 73 20 6f 6e 20 61 20 63 6f 6c 75 6d  ccurs on a colum
1ff90 6e 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65  n that is in the
1ff91 20 55 53 49 4e 47 20 63 6c 61 75 73 65 0a 20 20   USING clause.  
1ff92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
1ff93 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 70   of a join, skip
1ff94 20 74 68 65 20 73 65 61 72 63 68 20 6f 66 20 74   the search of t
1ff95 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
1ff96 66 20 74 68 65 20 6a 6f 69 6e 0a 20 20 20 20 20  f the join.     
1ff97 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f             ** to
1ff98 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61   avoid a duplica
1ff99 74 65 20 6d 61 74 63 68 20 74 68 65 72 65 2e 20  te match there. 
1ff9a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
1ff9b 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20     int k;.      
1ff9c 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b 3d            for(k=
1ff9d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 64  0; k<pUsing->nId
1ff9e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
1ff9f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1ffa0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 55 73  lite3StrICmp(pUs
1ffa1 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c  ing->a[k].zName,
1ffa2 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
1ffa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffa4 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20   pItem++;.      
1ffa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 2b                i+
1ffa6 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  +;.             
1ffa7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1ffa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffa9 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
1ffaa 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1ffab 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1ffac 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  }.            br
1ffad 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
1ffae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1ffaf 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65   }.    }..#ifnde
1ffb0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1ffb1 49 47 47 45 52 0a 20 20 20 20 2f 2a 20 49 66 20  IGGER.    /* If 
1ffb2 77 65 20 68 61 76 65 20 6e 6f 74 20 61 6c 72 65  we have not alre
1ffb3 61 64 79 20 72 65 73 6f 6c 76 65 64 20 74 68 65  ady resolved the
1ffb4 20 6e 61 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62   name, then mayb
1ffb5 65 20 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20  e .    ** it is 
1ffb6 61 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a  a new.* or old.*
1ffb7 20 74 72 69 67 67 65 72 20 61 72 67 75 6d 65 6e   trigger argumen
1ffb8 74 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 20  t reference.    
1ffb9 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 3d 3d  */.    if( zDb==
1ffba 30 20 26 26 20 7a 54 61 62 21 3d 30 20 26 26 20  0 && zTab!=0 && 
1ffbb 63 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 73 65  cnt==0 && pParse
1ffbc 2d 3e 70 54 72 69 67 67 65 72 54 61 62 21 3d 30  ->pTriggerTab!=0
1ffbd 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70   ){.      int op
1ffbe 20 3d 20 70 50 61 72 73 65 2d 3e 65 54 72 69 67   = pParse->eTrig
1ffbf 67 65 72 4f 70 3b 0a 20 20 20 20 20 20 54 61 62  gerOp;.      Tab
1ffc0 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20  le *pTab = 0;.  
1ffc1 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d      assert( op==
1ffc2 54 4b 5f 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d  TK_DELETE || op=
1ffc3 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70  =TK_UPDATE || op
1ffc4 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 29 3b 0a 20  ==TK_INSERT );. 
1ffc5 20 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f       if( op!=TK_
1ffc6 44 45 4c 45 54 45 20 26 26 20 73 71 6c 69 74 65  DELETE && sqlite
1ffc7 33 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a  3StrICmp("new",z
1ffc8 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20  Tab) == 0 ){.   
1ffc9 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1ffca 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  le = 1;.        
1ffcb 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
1ffcc 54 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20  TriggerTab;.    
1ffcd 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 21 3d    }else if( op!=
1ffce 54 4b 5f 49 4e 53 45 52 54 20 26 26 20 73 71 6c  TK_INSERT && sql
1ffcf 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c 64  ite3StrICmp("old
1ffd0 22 2c 7a 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20  ",zTab)==0 ){.  
1ffd1 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
1ffd2 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ble = 0;.       
1ffd3 20 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e   pTab = pParse->
1ffd4 70 54 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20  pTriggerTab;.   
1ffd5 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
1ffd6 70 54 61 62 20 29 7b 20 0a 20 20 20 20 20 20 20  pTab ){ .       
1ffd7 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20   int iCol;.     
1ffd8 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61     pSchema = pTa
1ffd9 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
1ffda 20 20 20 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20      cntTab++;.  
1ffdb 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1ffdc 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29  3IsRowid(zCol) )
1ffdd 7b 0a 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c  {.          iCol
1ffde 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d   = -1;.        }
1ffdf 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1ffe0 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
1ffe1 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f  <pTab->nCol; iCo
1ffe2 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l++){.          
1ffe3 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
1ffe4 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
1ffe5 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  l];.            
1ffe6 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1ffe7 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20  mp(pCol->zName, 
1ffe8 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  zCol)==0 ){.    
1ffe9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43            if( iC
1ffea 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  ol==pTab->iPKey 
1ffeb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1ffec 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20     iCol = -1;.  
1ffed 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1ffee 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
1ffef 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
1fff0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1fff1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1fff2 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  f( iCol<pTab->nC
1fff3 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
1fff4 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  cnt++;.         
1fff5 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20   if( iCol<0 ){. 
1fff6 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
1fff7 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
1fff8 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b  ITE_AFF_INTEGER;
1fff9 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
1fffa 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62   if( pExpr->iTab
1fffb 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  le==0 ){.       
1fffc 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
1fffd 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20  Col==31 );.     
1fffe 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
1ffff 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20   iCol==32 );.   
20000 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
20001 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 28 69 43 6f  >oldmask |= (iCo
20002 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66 66 66  l>=32 ? 0xffffff
20003 66 66 20 3a 20 28 28 28 75 33 32 29 31 29 3c 3c  ff : (((u32)1)<<
20004 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20 20 20  iCol));.        
20005 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
20006 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
20007 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20  Col==31 );.     
20008 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
20009 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20   iCol==32 );.   
2000a 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
2000b 3e 6e 65 77 6d 61 73 6b 20 7c 3d 20 28 69 43 6f  >newmask |= (iCo
2000c 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66 66 66  l>=32 ? 0xffffff
2000d 66 66 20 3a 20 28 28 28 75 33 32 29 31 29 3c 3c  ff : (((u32)1)<<
2000e 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20 20 20  iCol));.        
2000f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45    }.          pE
20010 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28  xpr->iColumn = (
20011 69 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20  i16)iCol;.      
20012 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
20013 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  = pTab;.        
20014 20 20 69 73 54 72 69 67 67 65 72 20 3d 20 31 3b    isTrigger = 1;
20015 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20016 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20   }.    }.#endif 
20017 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
20018 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
20019 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   */..    /*.    
2001a 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e  ** Perhaps the n
2001b 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 65 6e  ame is a referen
2001c 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a  ce to the ROWID.
2001d 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63      */.    if( c
2001e 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d  nt==0 && cntTab=
2001f 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49 73 52  =1 && sqlite3IsR
20020 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20  owid(zCol) ){.  
20021 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20      cnt = 1;.   
20022 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
20023 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45  n = -1;.      pE
20024 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  xpr->affinity = 
20025 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
20026 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ER;.    }..    /
20027 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
20028 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65 20  input is of the 
20029 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20  form Z (not Y.Z 
2002a 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74  or X.Y.Z) then t
2002b 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a  he name Z.    **
2002c 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20   might refer to 
2002d 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c  an result-set al
2002e 69 61 73 2e 20 20 54 68 69 73 20 68 61 70 70 65  ias.  This happe
2002f 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ns, for example,
20030 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20   when.    ** we 
20031 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  are resolving na
20032 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  mes in the WHERE
20033 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66   clause of the f
20034 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64  ollowing command
20035 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
20036 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41      SELECT a+b A
20037 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57  S x FROM table W
20038 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a  HERE x<10;.    *
20039 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65  *.    ** In case
2003a 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70  s like this, rep
2003b 6c 61 63 65 20 70 45 78 70 72 20 77 69 74 68 20  lace pExpr with 
2003c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78  a copy of the ex
2003d 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20  pression that.  
2003e 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72    ** forms the r
2003f 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20  esult set entry 
20040 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78  ("a+b" in the ex
20041 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72  ample) and retur
20042 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20  n immediately.. 
20043 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20     ** Note that 
20044 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
20045 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
20046 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72   should have alr
20047 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a  eady been.    **
20048 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65   resolved by the
20049 20 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20   time the WHERE 
2004a 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76  clause is resolv
2004b 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
2004c 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45  f( cnt==0 && (pE
2004d 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69  List = pNC->pELi
2004e 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d  st)!=0 && zTab==
2004f 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a  0 ){.      for(j
20050 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; j<pEList->nE
20051 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  xpr; j++){.     
20052 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70     char *zAs = p
20053 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d  EList->a[j].zNam
20054 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  e;.        if( z
20055 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33  As!=0 && sqlite3
20056 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f  StrICmp(zAs, zCo
20057 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
20058 20 20 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a     Expr *pOrig;.
20059 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2005a 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d  ( pExpr->pLeft==
2005b 30 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67  0 && pExpr->pRig
2005c 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ht==0 );.       
2005d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2005e 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a  ->x.pList==0 );.
2005f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
20060 28 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  ( pExpr->x.pSele
20061 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ct==0 );.       
20062 20 20 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73     pOrig = pELis
20063 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b 0a 20  t->a[j].pExpr;. 
20064 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4e           if( !pN
20065 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26 26 20 45  C->allowAgg && E
20066 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20067 4f 72 69 67 2c 20 45 50 5f 41 67 67 29 20 29 7b  Orig, EP_Agg) ){
20068 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
20069 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2006a 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20  rse, "misuse of 
2006b 61 6c 69 61 73 65 64 20 61 67 67 72 65 67 61 74  aliased aggregat
2006c 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a 20 20 20  e %s", zAs);.   
2006d 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2006e 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20  WRC_Abort;.     
2006f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
20070 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50   resolveAlias(pP
20071 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c  arse, pEList, j,
20072 20 70 45 78 70 72 2c 20 22 22 29 3b 0a 20 20 20   pExpr, "");.   
20073 20 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a         cnt = 1;.
20074 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
20075 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
20076 61 73 73 65 72 74 28 20 7a 54 61 62 3d 3d 30 20  assert( zTab==0 
20077 26 26 20 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20  && zDb==0 );.   
20078 20 20 20 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b         goto look
20079 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 20 20  upname_end;.    
2007a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20      }.      } . 
2007b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76     }..    /* Adv
2007c 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ance to the next
2007d 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20   name context.  
2007e 54 68 65 20 6c 6f 6f 70 20 77 69 6c 6c 20 65 78  The loop will ex
2007f 69 74 20 77 68 65 6e 20 65 69 74 68 65 72 0a 20  it when either. 
20080 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 61 20     ** we have a 
20081 6d 61 74 63 68 20 28 63 6e 74 3e 30 29 20 6f 72  match (cnt>0) or
20082 20 77 68 65 6e 20 77 65 20 72 75 6e 20 6f 75 74   when we run out
20083 20 6f 66 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74   of name context
20084 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
20085 28 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( cnt==0 ){.    
20086 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e 65    pNC = pNC->pNe
20087 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  xt;.    }.  }.. 
20088 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 58 20 61 6e   /*.  ** If X an
20089 64 20 59 20 61 72 65 20 4e 55 4c 4c 20 28 69 6e  d Y are NULL (in
2008a 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20   other words if 
2008b 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  only the column 
2008c 6e 61 6d 65 20 5a 20 69 73 0a 20 20 2a 2a 20 73  name Z is.  ** s
2008d 75 70 70 6c 69 65 64 29 20 61 6e 64 20 74 68 65  upplied) and the
2008e 20 76 61 6c 75 65 20 6f 66 20 5a 20 69 73 20 65   value of Z is e
2008f 6e 63 6c 6f 73 65 64 20 69 6e 20 64 6f 75 62 6c  nclosed in doubl
20090 65 2d 71 75 6f 74 65 73 2c 20 74 68 65 6e 0a 20  e-quotes, then. 
20091 20 2a 2a 20 5a 20 69 73 20 61 20 73 74 72 69 6e   ** Z is a strin
20092 67 20 6c 69 74 65 72 61 6c 20 69 66 20 69 74 20  g literal if it 
20093 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20 61 6e  doesn't match an
20094 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20  y column names. 
20095 20 49 6e 20 74 68 61 74 0a 20 20 2a 2a 20 63 61   In that.  ** ca
20096 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72  se, we need to r
20097 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79  eturn right away
20098 20 61 6e 64 20 6e 6f 74 20 6d 61 6b 65 20 61 6e   and not make an
20099 79 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20 2a  y changes to.  *
2009a 2a 20 70 45 78 70 72 2e 0a 20 20 2a 2a 0a 20 20  * pExpr..  **.  
2009b 2a 2a 20 42 65 63 61 75 73 65 20 6e 6f 20 72 65  ** Because no re
2009c 66 65 72 65 6e 63 65 20 77 61 73 20 6d 61 64 65  ference was made
2009d 20 74 6f 20 6f 75 74 65 72 20 63 6f 6e 74 65 78   to outer contex
2009e 74 73 2c 20 74 68 65 20 70 4e 43 2d 3e 6e 52 65  ts, the pNC->nRe
2009f 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 61 72  f.  ** fields ar
200a0 65 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 69 6e  e not changed in
200a1 20 61 6e 79 20 63 6f 6e 74 65 78 74 2e 0a 20 20   any context..  
200a2 2a 2f 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30 20  */.  if( cnt==0 
200a3 26 26 20 7a 54 61 62 3d 3d 30 20 26 26 20 45 78  && zTab==0 && Ex
200a4 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
200a5 78 70 72 2c 45 50 5f 44 62 6c 51 75 6f 74 65 64  xpr,EP_DblQuoted
200a6 29 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e  ) ){.    pExpr->
200a7 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a  op = TK_STRING;.
200a8 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
200a9 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
200aa 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a  WRC_Prune;.  }..
200ab 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30    /*.  ** cnt==0
200ac 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73   means there was
200ad 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74   not match.  cnt
200ae 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77  >1 means there w
200af 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20  ere two or.  ** 
200b0 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45  more matches.  E
200b1 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61  ither way, we ha
200b2 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a  ve an error..  *
200b3 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29  /.  if( cnt!=1 )
200b4 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
200b5 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72   *zErr;.    zErr
200b6 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20   = cnt==0 ? "no 
200b7 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22  such column" : "
200b8 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e  ambiguous column
200b9 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20   name";.    if( 
200ba 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  zDb ){.      sql
200bb 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
200bc 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e  rse, "%s: %s.%s.
200bd 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20  %s", zErr, zDb, 
200be 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zTab, zCol);.   
200bf 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20   }else if( zTab 
200c0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
200c1 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
200c2 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45   "%s: %s.%s", zE
200c3 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b  rr, zTab, zCol);
200c4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
200c5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
200c6 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25  g(pParse, "%s: %
200c7 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b  s", zErr, zCol);
200c8 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e  .    }.    pTopN
200c9 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a  C->nErr++;.  }..
200ca 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e    /* If a column
200cb 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e   from a table in
200cc 20 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66   pSrcList is ref
200cd 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65  erenced, then re
200ce 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66  cord.  ** this f
200cf 61 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c  act in the pSrcL
200d0 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20  ist.a[].colUsed 
200d1 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e  bitmask.  Column
200d2 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62   0 causes.  ** b
200d3 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20  it 0 to be set. 
200d4 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62   Column 1 sets b
200d5 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f  it 1.  And so fo
200d6 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a  rth.  If the.  *
200d7 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * column number 
200d8 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
200d9 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  the number of bi
200da 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73  ts in the bitmas
200db 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20  k.  ** then set 
200dc 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62  the high-order b
200dd 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73  it of the bitmas
200de 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45  k..  */.  if( pE
200df 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20  xpr->iColumn>=0 
200e0 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a  && pMatch!=0 ){.
200e1 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70      int n = pExp
200e2 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
200e3 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53  testcase( n==BMS
200e4 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e  -1 );.    if( n>
200e5 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e 20  =BMS ){.      n 
200e6 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20  = BMS-1;.    }. 
200e7 20 20 20 61 73 73 65 72 74 28 20 70 4d 61 74 63     assert( pMatc
200e8 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78 70  h->iCursor==pExp
200e9 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20  r->iTable );.   
200ea 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64   pMatch->colUsed
200eb 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31 29   |= ((Bitmask)1)
200ec 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  <<n;.  }..  /* C
200ed 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75  lean up and retu
200ee 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rn.  */.  sqlite
200ef 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
200f0 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
200f1 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20   pExpr->pLeft = 
200f2 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  0;.  sqlite3Expr
200f3 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
200f4 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78  ->pRight);.  pEx
200f5 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a  pr->pRight = 0;.
200f6 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28 69    pExpr->op = (i
200f7 73 54 72 69 67 67 65 72 20 3f 20 54 4b 5f 54 52  sTrigger ? TK_TR
200f8 49 47 47 45 52 20 3a 20 54 4b 5f 43 4f 4c 55 4d  IGGER : TK_COLUM
200f9 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65  N);.lookupname_e
200fa 6e 64 3a 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31  nd:.  if( cnt==1
200fb 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
200fc 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 73 71  pNC!=0 );.    sq
200fd 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 70 50  lite3AuthRead(pP
200fe 61 72 73 65 2c 20 70 45 78 70 72 2c 20 70 53 63  arse, pExpr, pSc
200ff 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c  hema, pNC->pSrcL
20100 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63  ist);.    /* Inc
20101 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 65 66 20  rement the nRef 
20102 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d  value on all nam
20103 65 20 63 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20  e contexts from 
20104 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 20 20 20  TopNC up to.    
20105 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 77 68 65  ** the point whe
20106 72 65 20 74 68 65 20 6e 61 6d 65 20 6d 61 74 63  re the name matc
20107 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  hed. */.    for(
20108 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ;;){.      asser
20109 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a  t( pTopNC!=0 );.
2010a 20 20 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52        pTopNC->nR
2010b 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ef++;.      if( 
2010c 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72  pTopNC==pNC ) br
2010d 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e  eak;.      pTopN
2010e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78  C = pTopNC->pNex
2010f 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  t;.    }.    ret
20110 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
20111 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65   } else {.    re
20112 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
20113 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c    }.}../*.** All
20114 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
20115 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
20116 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 6c   expression to l
20117 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  oad the column i
20118 43 6f 6c 0a 2a 2a 20 66 72 6f 6d 20 64 61 74 61  Col.** from data
20119 73 6f 75 72 63 65 20 69 53 72 63 20 64 61 74 61  source iSrc data
2011a 73 6f 75 72 63 65 20 69 6e 20 53 72 63 4c 69 73  source in SrcLis
2011b 74 20 70 53 72 63 2e 0a 2a 2f 0a 53 51 4c 49 54  t pSrc..*/.SQLIT
2011c 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
2011d 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c  sqlite3CreateCol
2011e 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20  umnExpr(sqlite3 
2011f 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 53  *db, SrcList *pS
20120 72 63 2c 20 69 6e 74 20 69 53 72 63 2c 20 69 6e  rc, int iSrc, in
20121 74 20 69 43 6f 6c 29 7b 0a 20 20 45 78 70 72 20  t iCol){.  Expr 
20122 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  *p = sqlite3Expr
20123 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 43 4f 4c  Alloc(db, TK_COL
20124 55 4d 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  UMN, 0, 0);.  if
20125 28 20 70 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ( p ){.    struc
20126 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
20127 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61  pItem = &pSrc->a
20128 5b 69 53 72 63 5d 3b 0a 20 20 20 20 70 2d 3e 70  [iSrc];.    p->p
20129 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61  Tab = pItem->pTa
2012a 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65  b;.    p->iTable
2012b 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
2012c 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54  r;.    if( p->pT
2012d 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20  ab->iPKey==iCol 
2012e 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c  ){.      p->iCol
2012f 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65  umn = -1;.    }e
20130 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43  lse{.      p->iC
20131 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69  olumn = (ynVar)i
20132 43 6f 6c 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  Col;.      pItem
20133 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42  ->colUsed |= ((B
20134 69 74 6d 61 73 6b 29 31 29 3c 3c 28 69 43 6f 6c  itmask)1)<<(iCol
20135 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d 31 20 3a 20  >=BMS ? BMS-1 : 
20136 69 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  iCol);.    }.   
20137 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
20138 28 70 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29  (p, EP_Resolved)
20139 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
2013a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2013b 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 62  routine is callb
2013c 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 57  ack for sqlite3W
2013d 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a  alkExpr()..**.**
2013e 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69   Resolve symboli
2013f 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f  c names into TK_
20140 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73  COLUMN operators
20141 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
20142 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 65 20  .** node in the 
20143 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
20144 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20 63 6f    Return 0 to co
20145 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61 72 63  ntinue the searc
20146 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72  h down.** the tr
20147 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f 72 74  ee or 2 to abort
20148 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a   the tree walk..
20149 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2014a 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 72 72  ne also does err
2014b 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e 64 20  or checking and 
2014c 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20  name resolution 
2014d 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  for.** function 
2014e 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 65 72  names.  The oper
2014f 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65 67 61  ator for aggrega
20150 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 73 20  te functions is 
20151 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b  changed.** to TK
20152 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a  _AGG_FUNCTION..*
20153 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
20154 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57 61 6c  olveExprStep(Wal
20155 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
20156 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61  pr *pExpr){.  Na
20157 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a  meContext *pNC;.
20158 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
20159 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65  ..  pNC = pWalke
2015a 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65  r->u.pNC;.  asse
2015b 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20  rt( pNC!=0 );.  
2015c 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
2015d 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74 28 20  arse;.  assert( 
2015e 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d  pParse==pWalker-
2015f 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 69 66  >pParse );..  if
20160 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70  ( ExprHasAnyProp
20161 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52  erty(pExpr, EP_R
20162 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74 75 72  esolved) ) retur
20163 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45  n WRC_Prune;.  E
20164 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
20165 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65  Expr, EP_Resolve
20166 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  d);.#ifndef NDEB
20167 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e 70 53  UG.  if( pNC->pS
20168 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d 3e 70  rcList && pNC->p
20169 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e  SrcList->nAlloc>
2016a 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74  0 ){.    SrcList
2016b 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43   *pSrcList = pNC
2016c 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20  ->pSrcList;.    
2016d 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
2016e 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c  =0; i<pNC->pSrcL
2016f 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
20170 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
20171 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43  SrcList->a[i].iC
20172 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 72 63  ursor>=0 && pSrc
20173 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
20174 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 29  or<pParse->nTab)
20175 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
20176 69 66 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  if.  switch( pEx
20177 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64  pr->op ){..#if d
20178 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
20179 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45  ABLE_UPDATE_DELE
2017a 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65  TE_LIMIT) && !de
2017b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2017c 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20 20 20  T_SUBQUERY).    
2017d 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c 20 6f  /* The special o
2017e 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d  perator TK_ROW m
2017f 65 61 6e 73 20 75 73 65 20 74 68 65 20 72 6f 77  eans use the row
20180 69 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  id for the first
20181 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
20182 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
20183 65 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  e.  This is used
20184 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20 61 6e   by the LIMIT an
20185 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a  d ORDER BY.    *
20186 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  * clause process
20187 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 61 6e  ing on UPDATE an
20188 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  d DELETE stateme
20189 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  nts..    */.    
2018a 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20  case TK_ROW: {. 
2018b 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53       SrcList *pS
2018c 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
2018d 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 73 74  rcList;.      st
2018e 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2018f 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20  m *pItem;.      
20190 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74  assert( pSrcList
20191 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53   && pSrcList->nS
20192 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70  rc==1 );.      p
20193 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d  Item = pSrcList-
20194 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78 70 72  >a; .      pExpr
20195 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e  ->op = TK_COLUMN
20196 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70  ;.      pExpr->p
20197 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61  Tab = pItem->pTa
20198 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  b;.      pExpr->
20199 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e  iTable = pItem->
2019a 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70  iCursor;.      p
2019b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
2019c 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  -1;.      pExpr-
2019d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49  >affinity = SQLI
2019e 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a  TE_AFF_INTEGER;.
2019f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
201a0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66   }.#endif /* def
201a1 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
201a2 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45  LE_UPDATE_DELETE
201a3 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69  _LIMIT) && !defi
201a4 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
201a5 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20  SUBQUERY) */..  
201a6 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e    /* A lone iden
201a7 74 69 66 69 65 72 20 69 73 20 74 68 65 20 6e 61  tifier is the na
201a8 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a  me of a column..
201a9 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
201aa 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 20 72  TK_ID: {.      r
201ab 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65  eturn lookupName
201ac 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 70  (pParse, 0, 0, p
201ad 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
201ae 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  pNC, pExpr);.   
201af 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 74   }.  .    /* A t
201b0 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f  able name and co
201b1 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49  lumn name:     I
201b2 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61  D.ID.    ** Or a
201b3 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
201b4 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44   and column:  ID
201b5 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20  .ID.ID.    */.  
201b6 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b    case TK_DOT: {
201b7 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
201b8 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r *zColumn;.    
201b9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
201ba 61 62 6c 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73  able;.      cons
201bb 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 20  t char *zDb;.   
201bc 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b     Expr *pRight;
201bd 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70  ..      /* if( p
201be 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 72 65  SrcList==0 ) bre
201bf 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 52 69  ak; */.      pRi
201c0 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69  ght = pExpr->pRi
201c1 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ght;.      if( p
201c2 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  Right->op==TK_ID
201c3 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 44 62 20   ){.        zDb 
201c4 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 54 61  = 0;.        zTa
201c5 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  ble = pExpr->pLe
201c6 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ft->u.zToken;.  
201c7 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20        zColumn = 
201c8 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
201c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
201ca 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
201cb 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f  Right->op==TK_DO
201cc 54 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 44 62  T );.        zDb
201cd 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d   = pExpr->pLeft-
201ce 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  >u.zToken;.     
201cf 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 52 69 67     zTable = pRig
201d0 68 74 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f  ht->pLeft->u.zTo
201d1 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f  ken;.        zCo
201d2 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70  lumn = pRight->p
201d3 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  Right->u.zToken;
201d4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
201d5 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65  eturn lookupName
201d6 28 70 50 61 72 73 65 2c 20 7a 44 62 2c 20 7a 54  (pParse, zDb, zT
201d7 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70  able, zColumn, p
201d8 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  NC, pExpr);.    
201d9 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76  }..    /* Resolv
201da 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73  e function names
201db 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
201dc 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a   TK_CONST_FUNC:.
201dd 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
201de 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78  TION: {.      Ex
201df 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
201e0 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 20  pExpr->x.pList; 
201e1 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65     /* The argume
201e2 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  nt list */.     
201e3 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f   int n = pList ?
201e4 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20   pList->nExpr : 
201e5 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  0;    /* Number 
201e6 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
201e7 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63        int no_suc
201e8 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 20  h_func = 0;     
201e9 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 20    /* True if no 
201ea 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78  such function ex
201eb 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ists */.      in
201ec 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73  t wrong_num_args
201ed 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
201ee 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65  e if wrong numbe
201ef 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a  r of arguments *
201f0 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 5f 61  /.      int is_a
201f1 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gg = 0;         
201f2 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
201f3 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66  s an aggregate f
201f4 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
201f5 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20 20 20   int auth;      
201f6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
201f7 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f  Authorization to
201f8 20 75 73 65 20 74 68 65 20 66 75 6e 63 74 69 6f   use the functio
201f9 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  n */.      int n
201fa 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Id;             
201fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
201fc 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69   of characters i
201fd 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  n function name 
201fe 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  */.      const c
201ff 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20  har *zId;       
20200 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63       /* The func
20201 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  tion name. */.  
20202 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65      FuncDef *pDe
20203 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
20204 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
20205 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74 69 6f  bout the functio
20206 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 6e  n */.      u8 en
20207 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e  c = ENC(pParse->
20208 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 61  db);   /* The da
20209 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20  tabase encoding 
2020a 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61  */..      testca
2020b 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
2020c 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a  K_CONST_FUNC );.
2020d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
2020e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2020f 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
20210 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64  ct) );.      zId
20211 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b   = pExpr->u.zTok
20212 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20  en;.      nId = 
20213 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
20214 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44 65 66  zId);.      pDef
20215 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
20216 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64  nction(pParse->d
20217 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20  b, zId, nId, n, 
20218 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  enc, 0);.      i
20219 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20  f( pDef==0 ){.  
2021a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
2021b 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
2021c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
2021d 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20  , nId, -1, enc, 
2021e 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
2021f 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pDef==0 ){.     
20220 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e       no_such_fun
20221 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
20222 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
20223 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d  wrong_num_args =
20224 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
20225 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20226 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 65 66     is_agg = pDef
20227 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20  ->xFunc==0;.    
20228 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
20229 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2022a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 28 20  ATION.      if( 
2022b 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20  pDef ){.        
2022c 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75  auth = sqlite3Au
2022d 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2022e 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c  SQLITE_FUNCTION,
2022f 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c   0, pDef->zName,
20230 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
20231 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b   auth!=SQLITE_OK
20232 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
20233 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44  ( auth==SQLITE_D
20234 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ENY ){.         
20235 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
20236 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20  sg(pParse, "not 
20237 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 75 73  authorized to us
20238 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c  e function: %s",
20239 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2023a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2023b 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65       pDef->zName
2023c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
2023d 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  NC->nErr++;.    
2023e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2023f 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
20240 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20  _NULL;.         
20241 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
20242 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
20243 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
20244 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 26 20    if( is_agg && 
20245 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29  !pNC->allowAgg )
20246 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
20247 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20248 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67  , "misuse of agg
20249 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
2024a 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64  %.*s()", nId,zId
2024b 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
2024c 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
2024d 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 20 20  is_agg = 0;.    
2024e 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73    }else if( no_s
2024f 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20  uch_func ){.    
20250 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
20251 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
20252 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25  such function: %
20253 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b  .*s", nId, zId);
20254 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45  .        pNC->nE
20255 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rr++;.      }els
20256 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f  e if( wrong_num_
20257 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20  args ){.        
20258 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20259 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75  pParse,"wrong nu
2025a 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
2025b 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e  s to function %.
2025c 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 20 20  *s()",.         
2025d 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20      nId, zId);. 
2025e 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72         pNC->nErr
2025f 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
20260 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 7b 0a    if( is_agg ){.
20261 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
20262 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  p = TK_AGG_FUNCT
20263 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  ION;.        pNC
20264 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20  ->hasAgg = 1;.  
20265 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
20266 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c  is_agg ) pNC->al
20267 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20  lowAgg = 0;.    
20268 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70    sqlite3WalkExp
20269 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70  rList(pWalker, p
2026a 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28  List);.      if(
2026b 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61   is_agg ) pNC->a
2026c 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20  llowAgg = 1;.   
2026d 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43     /* FIX ME:  C
2026e 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66  ompute pExpr->af
2026f 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20  finity based on 
20270 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65 74  the expected ret
20271 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70  urn.      ** typ
20272 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
20273 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n .      */.    
20274 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
20275 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ne;.    }.#ifnde
20276 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
20277 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20  BQUERY.    case 
20278 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63  TK_SELECT:.    c
20279 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 20 20  ase TK_EXISTS:  
2027a 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
2027b 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29  >op==TK_EXISTS )
2027c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73  ;.#endif.    cas
2027d 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20  e TK_IN: {.     
2027e 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
2027f 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20  ->op==TK_IN );. 
20280 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
20281 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
20282 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
20283 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65  .        int nRe
20284 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23  f = pNC->nRef;.#
20285 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20286 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20  IT_CHECK.       
20287 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63   if( pNC->isChec
20288 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  k ){.          s
20289 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2028a 50 61 72 73 65 2c 22 73 75 62 71 75 65 72 69 65  Parse,"subquerie
2028b 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20  s prohibited in 
2028c 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74  CHECK constraint
2028d 73 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  s");.        }.#
2028e 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 73 71  endif.        sq
2028f 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
20290 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e  pWalker, pExpr->
20291 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
20292 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d      assert( pNC-
20293 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20  >nRef>=nRef );. 
20294 20 20 20 20 20 20 20 69 66 28 20 6e 52 65 66 21         if( nRef!
20295 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20  =pNC->nRef ){.  
20296 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74 50          ExprSetP
20297 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20298 50 5f 56 61 72 53 65 6c 65 63 74 29 3b 0a 20 20  P_VarSelect);.  
20299 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2029a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2029b 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2029c 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20  E_OMIT_CHECK.   
2029d 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c   case TK_VARIABL
2029e 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70  E: {.      if( p
2029f 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20  NC->isCheck ){. 
202a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
202a1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 70  rorMsg(pParse,"p
202a2 61 72 61 6d 65 74 65 72 73 20 70 72 6f 68 69 62  arameters prohib
202a3 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f  ited in CHECK co
202a4 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20  nstraints");.   
202a5 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
202a6 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
202a7 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61   }.  return (pPa
202a8 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61  rse->nErr || pPa
202a9 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
202aa 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f  ailed) ? WRC_Abo
202ab 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75  rt : WRC_Continu
202ac 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69  e;.}../*.** pELi
202ad 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
202ae 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63  expressions whic
202af 68 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65  h are really the
202b0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
202b1 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73  he.** a SELECT s
202b2 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 20 69 73  tatement.  pE is
202b3 20 61 20 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52   a term in an OR
202b4 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
202b5 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68  BY clause..** Th
202b6 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
202b7 73 20 74 6f 20 73 65 65 20 69 66 20 70 45 20 69  s to see if pE i
202b8 73 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74  s a simple ident
202b9 69 66 69 65 72 20 77 68 69 63 68 20 63 6f 72 72  ifier which corr
202ba 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68  esponds.** to th
202bb 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65  e AS-name of one
202bc 20 6f 66 20 74 68 65 20 74 65 72 6d 73 20 6f 66   of the terms of
202bd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
202be 6c 69 73 74 2e 20 20 49 66 20 69 74 20 69 73 2c  list.  If it is,
202bf 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
202c0 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   return an integ
202c1 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  er between 1 and
202c2 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68   N where N is th
202c3 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65  e number of.** e
202c4 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 4c 69 73  lements in pELis
202c5 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  t, corresponding
202c6 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67   to the matching
202c7 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 72   entry.  If ther
202c8 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68  e is.** no match
202c9 2c 20 6f 72 20 69 66 20 70 45 20 69 73 20 6e 6f  , or if pE is no
202ca 74 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74  t a simple ident
202cb 69 66 69 65 72 2c 20 74 68 65 6e 20 74 68 69 73  ifier, then this
202cc 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75   routine.** retu
202cd 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69  rn 0..**.** pELi
202ce 73 74 20 68 61 73 20 62 65 65 6e 20 72 65 73 6f  st has been reso
202cf 6c 76 65 64 2e 20 20 70 45 20 68 61 73 20 6e 6f  lved.  pE has no
202d0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
202d1 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a   resolveAsName(.
202d2 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
202d3 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
202d4 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f  context for erro
202d5 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20  r messages */.  
202d6 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
202d7 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78  ,  /* List of ex
202d8 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 73 63 61  pressions to sca
202d9 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20  n */.  Expr *pE 
202da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
202db 72 65 73 73 69 6f 6e 20 77 65 20 61 72 65 20 74  ression we are t
202dc 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a  rying to match *
202dd 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
202de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
202df 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
202e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
202e1 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28  (pParse);..  if(
202e2 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29   pE->op==TK_ID )
202e3 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  {.    char *zCol
202e4 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b   = pE->u.zToken;
202e5 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
202e6 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
202e7 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ++){.      char 
202e8 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61  *zAs = pEList->a
202e9 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
202ea 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73   if( zAs!=0 && s
202eb 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41  qlite3StrICmp(zA
202ec 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20  s, zCol)==0 ){. 
202ed 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
202ee 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
202ef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
202f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20  .}../*.** pE is 
202f1 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
202f2 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68  expression which
202f3 20 69 73 20 61 20 73 69 6e 67 6c 65 20 74 65 72   is a single ter
202f4 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45  m in the.** ORDE
202f5 52 20 42 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75  R BY of a compou
202f6 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20  nd SELECT.  The 
202f7 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 6e  expression has n
202f8 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20  ot been.** name 
202f9 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20  resolved..**.** 
202fa 41 74 20 74 68 65 20 70 6f 69 6e 74 20 74 68 69  At the point thi
202fb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
202fc 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 64 79 20  led, we already 
202fd 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 0a 2a 2a  know that the.**
202fe 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69   ORDER BY term i
202ff 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72  s not an integer
20300 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20   index into the 
20301 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 61  result set.  Tha
20302 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 68 61 6e  t.** case is han
20303 64 6c 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  dled by the call
20304 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  ing routine..**.
20305 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6d 61  ** Attempt to ma
20306 74 63 68 20 70 45 20 61 67 61 69 6e 73 74 20 72  tch pE against r
20307 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
20308 73 20 69 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f  s in the left-mo
20309 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61  st.** SELECT sta
2030a 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20  tement.  Return 
2030b 74 68 65 20 69 6e 64 65 78 20 69 20 6f 66 20 74  the index i of t
2030c 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75  he matching colu
2030d 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64  mn,.** as an ind
2030e 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 65 20 63  ication to the c
2030f 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20 73 68  aller that it sh
20310 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 74 68 65  ould sort by the
20311 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a   i-th column..**
20312 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63   The left-most c
20313 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20  olumn is 1.  In 
20314 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
20315 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
20316 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69  is the.** same i
20317 6e 74 65 67 65 72 20 76 61 6c 75 65 20 74 68 61  nteger value tha
20318 74 20 77 6f 75 6c 64 20 62 65 20 75 73 65 64 20  t would be used 
20319 69 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  in the SQL state
2031a 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 61 74 65  ment to indicate
2031b 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a  .** the column..
2031c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
2031d 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75  s no match, retu
2031e 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 2d 31  rn 0.  Return -1
2031f 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
20320 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
20321 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42  nt resolveOrderB
20322 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28  yTermToExprList(
20323 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20324 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  ,     /* Parsing
20325 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72   context for err
20326 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20  or messages */. 
20327 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
20328 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43  ,   /* The SELEC
20329 54 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  T statement with
2032a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2032b 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ause */.  Expr *
2032c 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pE           /* 
2032d 54 68 65 20 73 70 65 63 69 66 69 63 20 4f 52 44  The specific ORD
2032e 45 52 20 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b  ER BY term */.){
2032f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
20330 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
20331 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c  unter */.  ExprL
20332 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a  ist *pEList;  /*
20333 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   The columns of 
20334 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
20335 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
20336 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63  nc;    /* Name c
20337 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 73 6f 6c  ontext for resol
20338 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73  ving pE */..  as
20339 73 65 72 74 28 20 73 71 6c 69 74 65 33 45 78 70  sert( sqlite3Exp
2033a 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26  rIsInteger(pE, &
2033b 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73  i)==0 );.  pELis
2033c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  t = pSelect->pEL
2033d 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  ist;..  /* Resol
2033e 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20  ve all names in 
2033f 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  the ORDER BY ter
20340 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a  m expression.  *
20341 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20  /.  memset(&nc, 
20342 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a  0, sizeof(nc));.
20343 20 20 6e 63 2e 70 50 61 72 73 65 20 3d 20 70 50    nc.pParse = pP
20344 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c  arse;.  nc.pSrcL
20345 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
20346 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74  Src;.  nc.pEList
20347 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e   = pEList;.  nc.
20348 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20  allowAgg = 1;.  
20349 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69  nc.nErr = 0;.  i
2034a 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
2034b 65 45 78 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20  eExprNames(&nc, 
2034c 70 45 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  pE) ){.    sqlit
2034d 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61  e3ErrorClear(pPa
2034e 72 73 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rse);.    return
2034f 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72   0;.  }..  /* Tr
20350 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 4f  y to match the O
20351 52 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69  RDER BY expressi
20352 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e 20 65 78  on against an ex
20353 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e  pression.  ** in
20354 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
20355 20 20 52 65 74 75 72 6e 20 61 6e 20 31 2d 62 61    Return an 1-ba
20356 73 65 64 20 69 6e 64 65 78 20 6f 66 20 74 68 65  sed index of the
20357 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72   matching.  ** r
20358 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e  esult-set entry.
20359 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
2035a 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   i<pEList->nExpr
2035b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2035c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
2035d 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  re(pEList->a[i].
2035e 70 45 78 70 72 2c 20 70 45 29 20 29 7b 0a 20 20  pExpr, pE) ){.  
2035f 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
20360 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
20361 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74  If no match, ret
20362 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75  urn 0. */.  retu
20363 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rn 0;.}../*.** G
20364 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 44 45 52  enerate an ORDER
20365 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
20366 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67  term out-of-rang
20367 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74  e error..*/.stat
20368 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f  ic void resolveO
20369 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 0a  utOfRangeError(.
2036a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2036b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2036c 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e  error context in
2036d 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74  to which to writ
2036e 65 20 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20  e the error */. 
2036f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
20370 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f 52 44 45  pe,     /* "ORDE
20371 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f  R" or "GROUP" */
20372 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20  .  int i,       
20373 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
20374 20 69 6e 64 65 78 20 28 31 2d 62 61 73 65 64 29   index (1-based)
20375 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6f 75 74   of the term out
20376 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69   of range */.  i
20377 6e 74 20 6d 78 20 20 20 20 20 20 20 20 20 20 20  nt mx           
20378 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74        /* Largest
20379 20 70 65 72 6d 69 73 73 69 62 6c 65 20 76 61 6c   permissible val
2037a 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20  ue of i */.){.  
2037b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2037c 70 50 61 72 73 65 2c 20 0a 20 20 20 20 22 25 72  pParse, .    "%r
2037d 20 25 73 20 42 59 20 74 65 72 6d 20 6f 75 74 20   %s BY term out 
2037e 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c  of range - shoul
2037f 64 20 62 65 20 22 0a 20 20 20 20 22 62 65 74 77  d be ".    "betw
20380 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c 20 69  een 1 and %d", i
20381 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a  , zType, mx);.}.
20382 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74  ./*.** Analyze t
20383 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
20384 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  se in a compound
20385 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
20386 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65  t.   Modify.** e
20387 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ach term of the 
20388 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
20389 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e  is a constant in
2038a 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 0a  teger between 1.
2038b 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e  ** and N where N
2038c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2038d 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
2038e 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
2038f 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59  ..**.** ORDER BY
20390 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20   terms that are 
20391 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 74 65 67  already an integ
20392 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  er between 1 and
20393 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69   N are.** unmodi
20394 66 69 65 64 2e 20 20 4f 52 44 45 52 20 42 59 20  fied.  ORDER BY 
20395 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 69  terms that are i
20396 6e 74 65 67 65 72 73 20 6f 75 74 73 69 64 65 20  ntegers outside 
20397 74 68 65 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20  the range of.** 
20398 31 20 74 68 72 6f 75 67 68 20 4e 20 67 65 6e 65  1 through N gene
20399 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 20 20  rate an error.  
2039a 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
2039b 68 61 74 20 61 72 65 20 65 78 70 72 65 73 73 69  hat are expressi
2039c 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68  ons.** are match
2039d 65 64 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c  ed against resul
2039e 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t set expression
2039f 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45  s of compound SE
203a0 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e  LECT.** beginnin
203a1 67 20 77 69 74 68 20 74 68 65 20 6c 65 66 74 2d  g with the left-
203a2 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 6e 64 20  most SELECT and 
203a3 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 64 20 74  working toward t
203a4 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20  he right..** At 
203a5 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 2c  the first match,
203a6 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 65 78   the ORDER BY ex
203a7 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 61 6e  pression is tran
203a8 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20  sformed into.** 
203a9 74 68 65 20 69 6e 74 65 67 65 72 20 63 6f 6c 75  the integer colu
203aa 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a  mn number..**.**
203ab 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
203ac 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65  er of errors see
203ad 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
203ae 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64   resolveCompound
203af 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65  OrderBy(.  Parse
203b0 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
203b1 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
203b2 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f  ext.  Leave erro
203b3 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20  r messages here 
203b4 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
203b5 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20 54 68  lect       /* Th
203b6 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
203b7 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  nt containing th
203b8 65 20 4f 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b  e ORDER BY */.){
203b9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72  .  int i;.  Expr
203ba 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a  List *pOrderBy;.
203bb 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
203bc 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  st;.  sqlite3 *d
203bd 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44  b;.  int moreToD
203be 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72  o = 1;..  pOrder
203bf 42 79 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f  By = pSelect->pO
203c0 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 70 4f  rderBy;.  if( pO
203c1 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75  rderBy==0 ) retu
203c2 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61  rn 0;.  db = pPa
203c3 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c  rse->db;.#if SQL
203c4 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20  ITE_MAX_COLUMN. 
203c5 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e   if( pOrderBy->n
203c6 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  Expr>db->aLimit[
203c7 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c  SQLITE_LIMIT_COL
203c8 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  UMN] ){.    sqli
203c9 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
203ca 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65  se, "too many te
203cb 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 42 59 20  rms in ORDER BY 
203cc 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 72 65  clause");.    re
203cd 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64  turn 1;.  }.#end
203ce 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  if.  for(i=0; i<
203cf 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
203d0 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65   i++){.    pOrde
203d1 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d  rBy->a[i].done =
203d2 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63   0;.  }.  pSelec
203d3 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  t->pNext = 0;.  
203d4 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e  while( pSelect->
203d5 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 53  pPrior ){.    pS
203d6 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70  elect->pPrior->p
203d7 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a  Next = pSelect;.
203d8 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53      pSelect = pS
203d9 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  elect->pPrior;. 
203da 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c   }.  while( pSel
203db 65 63 74 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20  ect && moreToDo 
203dc 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  ){.    struct Ex
203dd 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
203de 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f  em;.    moreToDo
203df 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c 69 73 74   = 0;.    pEList
203e0 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
203e1 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  st;.    assert( 
203e2 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20  pEList!=0 );.   
203e3 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
203e4 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70  pOrderBy->a; i<p
203e5 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
203e6 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
203e7 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
203e8 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  -1;.      Expr *
203e9 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 20 20 20  pE, *pDup;.     
203ea 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65   if( pItem->done
203eb 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
203ec 20 20 20 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70     pE = pItem->p
203ed 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20  Expr;.      if( 
203ee 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
203ef 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20  eger(pE, &iCol) 
203f0 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
203f1 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70  Col<=0 || iCol>p
203f2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  EList->nExpr ){.
203f3 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76            resolv
203f4 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72  eOutOfRangeError
203f5 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 52 22  (pParse, "ORDER"
203f6 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e  , i+1, pEList->n
203f7 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20  Expr);.         
203f8 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
203f9 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
203fa 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d  {.        iCol =
203fb 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70   resolveAsName(p
203fc 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
203fd 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  E);.        if( 
203fe 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20  iCol==0 ){.     
203ff 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69       pDup = sqli
20400 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
20401 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  E, 0);.         
20402 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
20403 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
20404 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44 75        assert(pDu
20405 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  p);.            
20406 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72  iCol = resolveOr
20407 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c  derByTermToExprL
20408 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ist(pParse, pSel
20409 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  ect, pDup);.    
2040a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2040b 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2040c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20  ete(db, pDup);. 
2040d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2040e 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e  .      if( iCol>
2040f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c  0 ){.        Col
20410 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45  lSeq *pColl = pE
20411 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  ->pColl;.       
20412 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45 2d   int flags = pE-
20413 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
20414 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20  ollate;.        
20415 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
20416 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20 20  e(db, pE);.     
20417 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20     pItem->pExpr 
20418 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45 78  = pE = sqlite3Ex
20419 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47 45  pr(db, TK_INTEGE
2041a 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  R, 0);.        i
2041b 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74 75 72  f( pE==0 ) retur
2041c 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70 45 2d  n 1;.        pE-
2041d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
2041e 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c 61 67          pE->flag
2041f 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65  s |= EP_IntValue
20420 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20   | flags;.      
20421 20 20 70 45 2d 3e 75 2e 69 56 61 6c 75 65 20 3d    pE->u.iValue =
20422 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70   iCol;.        p
20423 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31  Item->iCol = (u1
20424 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  6)iCol;.        
20425 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b  pItem->done = 1;
20426 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
20427 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d        moreToDo =
20428 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
20429 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20  }.    pSelect = 
2042a 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a  pSelect->pNext;.
2042b 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
2042c 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
2042d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2042e 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64  pOrderBy->a[i].d
2042f 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  one==0 ){.      
20430 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20431 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45  pParse, "%r ORDE
20432 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e  R BY term does n
20433 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20  ot match any ". 
20434 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75             "colu
20435 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  mn in the result
20436 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20   set", i+1);.   
20437 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
20438 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
20439 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  0;.}../*.** Chec
2043a 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  k every term in 
2043b 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  the ORDER BY or 
2043c 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
2043d 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74  pOrderBy of.** t
2043e 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2043f 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66  ent pSelect.  If
20440 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66   any term is ref
20441 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72  erence to a.** r
20442 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
20443 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69  sion (as determi
20444 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c  ned by the ExprL
20445 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64  ist.a.iCol field
20446 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72  ).** then conver
20447 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f  t that term into
20448 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63   a copy of the c
20449 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73  orresponding res
2044a 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d  ult set.** colum
2044b 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20  n..**.** If any 
2044c 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63  errors are detec
2044d 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f  ted, add an erro
2044e 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61  r message to pPa
2044f 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  rse and.** retur
20450 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74  n non-zero.  Ret
20451 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65  urn zero if no e
20452 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a  rrors are seen..
20453 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20454 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
20455 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
20456 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
20457 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
20458 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
20459 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
2045a 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ges here */.  Se
2045b 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
2045c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
2045d 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74  T statement cont
2045e 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73  aining the claus
2045f 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
20460 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20  *pOrderBy,   /* 
20461 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20  The ORDER BY or 
20462 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
20463 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20  to be processed 
20464 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
20465 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f  *zType     /* "O
20466 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22  RDER" or "GROUP"
20467 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
20468 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20469 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78  pParse->db;.  Ex
2046a 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
2046b 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2046c 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
2046d 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d    if( pOrderBy==
2046e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d  0 || pParse->db-
2046f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
20470 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51  return 0;.#if SQ
20471 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a  LITE_MAX_COLUMN.
20472 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e    if( pOrderBy->
20473 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74  nExpr>db->aLimit
20474 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
20475 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c  LUMN] ){.    sql
20476 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
20477 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74  rse, "too many t
20478 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c  erms in %s BY cl
20479 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20  ause", zType);. 
2047a 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
2047b 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74  .#endif.  pEList
2047c 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
2047d 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  st;.  assert( pE
2047e 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73  List!=0 );  /* s
2047f 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
20480 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69  ) guarantees thi
20481 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  s */.  for(i=0, 
20482 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
20483 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  a; i<pOrderBy->n
20484 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
20485 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
20486 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20  em->iCol ){.    
20487 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f    if( pItem->iCo
20488 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l>pEList->nExpr 
20489 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c  ){.        resol
2048a 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
2048b 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c  r(pParse, zType,
2048c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45   i+1, pEList->nE
2048d 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65  xpr);.        re
2048e 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
2048f 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69        resolveAli
20490 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  as(pParse, pELis
20491 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31  t, pItem->iCol-1
20492 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  , pItem->pExpr, 
20493 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zType);.    }.  
20494 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
20495 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20  ./*.** pOrderBy 
20496 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  is an ORDER BY o
20497 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
20498 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74  e in SELECT stat
20499 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a  ement pSelect..*
2049a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65  * The Name conte
2049b 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
2049c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e   statement is pN
2049d 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74  C.  zType is eit
2049e 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f  her.** "ORDER" o
2049f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64  r "GROUP" depend
204a0 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70  ing on which typ
204a1 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64  e of clause pOrd
204a2 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54  erBy is..**.** T
204a3 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f  his routine reso
204a4 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f  lves each term o
204a5 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74  f the clause int
204a6 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e  o an expression.
204a7 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72  .** If the order
204a8 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69  -by term is an i
204a9 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e  nteger I between
204aa 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20   1 and N (where 
204ab 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  N is the.** numb
204ac 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
204ad 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
204ae 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74  of the SELECT) t
204af 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
204b0 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  on.** in the res
204b1 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70  olution is a cop
204b2 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65  y of the I-th re
204b3 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
204b4 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  ion.  If.** the 
204b5 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73  order-by term is
204b6 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61   an identify tha
204b7 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
204b8 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a   the AS-name of.
204b9 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20  ** a result-set 
204ba 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e  expression, then
204bb 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76   the term resolv
204bc 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20  es to a copy of 
204bd 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  the.** result-se
204be 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f  t expression.  O
204bf 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78  therwise, the ex
204c0 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f  pression is reso
204c1 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75  lved in.** the u
204c2 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67  sual way - using
204c3 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
204c4 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a  xprNames()..**.*
204c5 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
204c6 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
204c7 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66  r of errors.  If
204c8 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
204c9 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70  hen.** an approp
204ca 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73  riate error mess
204cb 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66  age might be lef
204cc 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f  t in pParse.  (O
204cd 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63  OM errors.** exc
204ce 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69  epted.).*/.stati
204cf 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
204d0 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d  erGroupBy(.  Nam
204d1 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20  eContext *pNC,  
204d2 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63     /* The name c
204d3 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45  ontext of the SE
204d4 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
204d5 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
204d6 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ect,      /* The
204d7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
204d8 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72  t holding pOrder
204d9 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  By */.  ExprList
204da 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
204db 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   An ORDER BY or 
204dc 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
204dd 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20  to resolve */.  
204de 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
204df 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20  e     /* Either 
204e0 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55  "ORDER" or "GROU
204e1 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  P", as appropria
204e2 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  te */.){.  int i
204e3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
204e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
204e5 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
204e6 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
204e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
204e8 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a   Column number *
204e9 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
204ea 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
204eb 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20     /* A term of 
204ec 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
204ed 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  use */.  Parse *
204ee 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
204ef 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
204f0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
204f1 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20  int nResult;    
204f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
204f3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
204f4 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
204f5 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f  set */..  if( pO
204f6 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75  rderBy==0 ) retu
204f7 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20  rn 0;.  nResult 
204f8 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
204f9 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72  t->nExpr;.  pPar
204fa 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
204fb 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
204fc 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
204fd 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
204fe 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
204ff 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d  {.    Expr *pE =
20500 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20   pItem->pExpr;. 
20501 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76     iCol = resolv
20502 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  eAsName(pParse, 
20503 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c  pSelect->pEList,
20504 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43   pE);.    if( iC
20505 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ol>0 ){.      /*
20506 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d   If an AS-name m
20507 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d  atch is found, m
20508 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52 20 42  ark this ORDER B
20509 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e  Y column as bein
2050a 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 70  g.      ** a cop
2050b 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68  y of the iCol-th
2050c 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75   result-set colu
2050d 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65 71 75  mn.  The subsequ
2050e 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20  ent call to.    
2050f 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 6f    ** sqlite3Reso
20510 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
20511 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74  ) will convert t
20512 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
20513 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79   a.      ** copy
20514 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20   of the iCol-th 
20515 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
20516 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ssion. */.      
20517 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75  pItem->iCol = (u
20518 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63  16)iCol;.      c
20519 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
2051a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
2051b 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20  prIsInteger(pE, 
2051c 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  &iCol) ){.      
2051d 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2051e 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67  term is an integ
2051f 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67  er constant.  Ag
20520 61 69 6e 2c 20 73 65 74 20 74 68 65 20 63 6f 6c  ain, set the col
20521 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d  umn.      ** num
20522 62 65 72 20 73 6f 20 74 68 61 74 20 73 71 6c 69  ber so that sqli
20523 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47  te3ResolveOrderG
20524 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f  roupBy() will co
20525 6e 76 65 72 74 20 74 68 65 0a 20 20 20 20 20 20  nvert the.      
20526 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d  ** order-by term
20527 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68   to a copy of th
20528 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  e result-set exp
20529 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ression */.     
2052a 20 69 66 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20   if( iCol<1 ){. 
2052b 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75         resolveOu
2052c 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50  tOfRangeError(pP
2052d 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31  arse, zType, i+1
2052e 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20 20 20 20  , nResult);.    
2052f 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
20530 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 74 65      }.      pIte
20531 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  m->iCol = (u16)i
20532 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  Col;.      conti
20533 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nue;.    }..    
20534 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 72  /* Otherwise, tr
20535 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 42 59  eat the ORDER BY
20536 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 64 69   term as an ordi
20537 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  nary expression 
20538 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43  */.    pItem->iC
20539 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  ol = 0;.    if( 
2053a 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
2053b 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 29  prNames(pNC, pE)
2053c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2053d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
2053e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65  return sqlite3Re
2053f 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
20540 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  y(pParse, pSelec
20541 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a 54 79  t, pOrderBy, zTy
20542 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  pe);.}../*.** Re
20543 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74  solve names in t
20544 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
20545 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66  ent p and all of
20546 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e 74 73   its descendents
20547 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
20548 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65  resolveSelectSte
20549 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  p(Walker *pWalke
2054a 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  r, Select *p){. 
2054b 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f   NameContext *pO
2054c 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74  uterNC;  /* Cont
2054d 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ext that contain
2054e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f  s this SELECT */
2054f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73  .  NameContext s
20550 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  NC;        /* Na
20551 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68  me context of th
20552 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  is SELECT */.  i
20553 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20  nt isCompound;  
20554 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
20555 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  f p is a compoun
20556 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e  d select */.  in
20557 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20  t nCompound;    
20558 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
20559 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d  of compound term
2055a 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66  s processed so f
2055b 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ar */.  Parse *p
2055c 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20  Parse;          
2055d 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2055e 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
2055f 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20   *pEList;       
20560 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20 65 78  /* Result set ex
20561 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f  pression list */
20562 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
20563 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
20564 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
20565 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70  ExprList *pGroup
20566 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 47  By;     /* The G
20567 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
20568 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 66  /.  Select *pLef
20569 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c  tmost;      /* L
2056a 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45  eft-most of SELE
2056b 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64  CT of a compound
2056c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2056d 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
2056e 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2056f 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73  tion */.  ..  as
20570 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
20571 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  if( p->selFlags 
20572 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b  & SF_Resolved ){
20573 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
20574 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75  Prune;.  }.  pOu
20575 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d  terNC = pWalker-
20576 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73 65  >u.pNC;.  pParse
20577 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72   = pWalker->pPar
20578 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73  se;.  db = pPars
20579 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72  e->db;..  /* Nor
2057a 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53 65 6c  mally sqlite3Sel
2057b 65 63 74 45 78 70 61 6e 64 28 29 20 77 69 6c 6c  ectExpand() will
2057c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74   be called first
2057d 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a 20   and will have. 
2057e 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 70 61   ** already expa
2057f 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45 43 54  nded this SELECT
20580 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74  .  However, if t
20581 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65 72  his is a subquer
20582 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e  y within.  ** an
20583 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c   expression, sql
20584 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
20585 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65 20 63  ames() will be c
20586 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20 61 0a  alled without a.
20587 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20    ** prior call 
20588 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  to sqlite3Select
20589 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65 6e 20  Expand().  When 
2058a 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 6c 65  that happens, le
2058b 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65  t.  ** sqlite3Se
2058c 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 61 6c  lectPrep() do al
2058d 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73  l of the process
2058e 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c  ing for this SEL
2058f 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65  ECT..  ** sqlite
20590 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 77 69  3SelectPrep() wi
20591 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73  ll invoke both s
20592 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
20593 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68  nd() and.  ** th
20594 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  is routine in th
20595 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 2e  e correct order.
20596 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e  .  */.  if( (p->
20597 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78  selFlags & SF_Ex
20598 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20  panded)==0 ){.  
20599 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50    sqlite3SelectP
2059a 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  rep(pParse, p, p
2059b 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 72 65  OuterNC);.    re
2059c 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45  turn (pParse->nE
2059d 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
2059e 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62  Failed) ? WRC_Ab
2059f 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b  ort : WRC_Prune;
205a0 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75  .  }..  isCompou
205a1 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d  nd = p->pPrior!=
205a2 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d  0;.  nCompound =
205a3 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20   0;.  pLeftmost 
205a4 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 20  = p;.  while( p 
205a5 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ){.    assert( (
205a6 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
205a7 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29 3b  _Expanded)!=0 );
205a8 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
205a9 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52  >selFlags & SF_R
205aa 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20  esolved)==0 );. 
205ab 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c     p->selFlags |
205ac 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a  = SF_Resolved;..
205ad 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74      /* Resolve t
205ae 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  he expressions i
205af 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20  n the LIMIT and 
205b0 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20  OFFSET clauses. 
205b1 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61 72 65  These.    ** are
205b2 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
205b3 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d  refer to any nam
205b4 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e 20 65  es, so pass an e
205b5 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74  mpty NameContext
205b6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d  ..    */.    mem
205b7 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
205b8 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73  eof(sNC));.    s
205b9 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
205ba 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  se;.    if( sqli
205bb 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
205bc 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69  mes(&sNC, p->pLi
205bd 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  mit) ||.        
205be 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
205bf 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d  prNames(&sNC, p-
205c0 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20  >pOffset) ){.   
205c1 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
205c2 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  ort;.    }.  .  
205c3 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20    /* Set up the 
205c4 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65  local name-conte
205c5 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20 73 71  xt to pass to sq
205c6 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
205c7 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20 20 2a  Names() to.    *
205c8 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 72 65  * resolve the re
205c9 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
205ca 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f  ion list..    */
205cb 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67  .    sNC.allowAg
205cc 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70  g = 1;.    sNC.p
205cd 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72  SrcList = p->pSr
205ce 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74  c;.    sNC.pNext
205cf 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a   = pOuterNC;.  .
205d0 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e      /* Resolve n
205d1 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65 73 75  ames in the resu
205d2 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70  lt set. */.    p
205d3 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
205d4 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
205d5 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20  EList!=0 );.    
205d6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
205d7 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
205d8 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d        Expr *pX =
205d9 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45   pEList->a[i].pE
205da 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73  xpr;.      if( s
205db 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
205dc 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 29  rNames(&sNC, pX)
205dd 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
205de 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
205df 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
205e0 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 6c     /* Recursivel
205e1 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20  y resolve names 
205e2 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  in all subquerie
205e3 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  s.    */.    for
205e4 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d  (i=0; i<p->pSrc-
205e5 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
205e6 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
205e7 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
205e8 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a  &p->pSrc->a[i];.
205e9 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
205ea 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
205eb 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
205ec 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20 3d 20  zSavedContext = 
205ed 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
205ee 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66  text;.        if
205ef 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29  ( pItem->zName )
205f0 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
205f1 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a  ntext = pItem->z
205f2 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71  Name;.        sq
205f3 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65  lite3ResolveSele
205f4 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  ctNames(pParse, 
205f5 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20  pItem->pSelect, 
205f6 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 20  pOuterNC);.     
205f7 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68     pParse->zAuth
205f8 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64  Context = zSaved
205f9 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20  Context;.       
205fa 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
205fb 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
205fc 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 57  ailed ) return W
205fd 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
205fe 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
205ff 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  * If there are n
20600 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  o aggregate func
20601 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 72 65 73  tions in the res
20602 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20  ult-set, and no 
20603 47 52 4f 55 50 20 42 59 20 0a 20 20 20 20 2a 2a  GROUP BY .    **
20604 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20   expression, do 
20605 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 72 65 67  not allow aggreg
20606 61 74 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74  ates in any of t
20607 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 73 73  he other express
20608 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ions..    */.   
20609 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c   assert( (p->sel
2060a 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65  Flags & SF_Aggre
2060b 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  gate)==0 );.    
2060c 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
2060d 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 66 28 20  roupBy;.    if( 
2060e 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e  pGroupBy || sNC.
2060f 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20  hasAgg ){.      
20610 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  p->selFlags |= S
20611 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20  F_Aggregate;.   
20612 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e   }else{.      sN
20613 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a  C.allowAgg = 0;.
20614 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
20615 49 66 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75  If a HAVING clau
20616 73 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74  se is present, t
20617 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62  hen there must b
20618 65 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61  e a GROUP BY cla
20619 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  use..    */.    
2061a 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26  if( p->pHaving &
2061b 26 20 21 70 47 72 6f 75 70 42 79 20 29 7b 0a 20  & !pGroupBy ){. 
2061c 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2061d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20  rMsg(pParse, "a 
2061e 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
2061f 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  is required befo
20620 72 65 20 48 41 56 49 4e 47 22 29 3b 0a 20 20 20  re HAVING");.   
20621 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
20622 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  ort;.    }.  .  
20623 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 78 70    /* Add the exp
20624 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20  ression list to 
20625 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  the name-context
20626 20 62 65 66 6f 72 65 20 70 61 72 73 69 6e 67 20   before parsing 
20627 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72  the.    ** other
20628 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
20629 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2062a 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 73 6f  ment. This is so
2062b 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 65 78 70   that.    ** exp
2062c 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
2062d 57 48 45 52 45 20 63 6c 61 75 73 65 20 28 65 74  WHERE clause (et
2062e 63 2e 29 20 63 61 6e 20 72 65 66 65 72 20 74 6f  c.) can refer to
2062f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62 79 0a   expressions by.
20630 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 73 20 69      ** aliases i
20631 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
20632 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
20633 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20  Minor point: If 
20634 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
20635 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65  , then the expre
20636 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20  ssion will be.  
20637 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 61 74 65    ** re-evaluate
20638 64 20 66 6f 72 20 65 61 63 68 20 72 65 66 65 72  d for each refer
20639 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 20 20 20  ence to it..    
2063a 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73  */.    sNC.pELis
2063b 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
2063c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2063d 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
2063e 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 65 29 20  sNC, p->pWhere) 
2063f 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65  ||.       sqlite
20640 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
20641 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69  s(&sNC, p->pHavi
20642 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ng).    ){.     
20643 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
20644 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  t;.    }..    /*
20645 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 61 6e   The ORDER BY an
20646 64 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  d GROUP BY claus
20647 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 66 65 72  es may not refer
20648 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a 20 20 20   to terms in.   
20649 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 69 65   ** outer querie
2064a 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e  s .    */.    sN
2064b 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  C.pNext = 0;.   
2064c 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20   sNC.allowAgg = 
2064d 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  1;..    /* Proce
2064e 73 73 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ss the ORDER BY 
2064f 63 6c 61 75 73 65 20 66 6f 72 20 73 69 6e 67 6c  clause for singl
20650 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 74 61 74  eton SELECT stat
20651 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54  ements..    ** T
20652 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
20653 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73  se for compounds
20654 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
20655 74 73 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20  ts is handled.  
20656 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65    ** below, afte
20657 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 73  r all of the res
20658 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c  ult-sets for all
20659 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73   of the elements
2065a 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63   of.    ** the c
2065b 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65  ompound have bee
2065c 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20  n resolved..    
2065d 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 43 6f  */.    if( !isCo
2065e 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76  mpound && resolv
2065f 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73  eOrderGroupBy(&s
20660 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72  NC, p, p->pOrder
20661 42 79 2c 20 22 4f 52 44 45 52 22 29 20 29 7b 0a  By, "ORDER") ){.
20662 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
20663 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
20664 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
20665 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
20666 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
20667 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
20668 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 47 52  * Resolve the GR
20669 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20  OUP BY clause.  
2066a 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  At the same time
2066b 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a 20 20 20  , make sure .   
2066c 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59   ** the GROUP BY
2066d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74   clause does not
2066e 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61   contain aggrega
2066f 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  te functions..  
20670 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72    */.    if( pGr
20671 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73  oupBy ){.      s
20672 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
20673 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
20674 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 6f 6c  .      if( resol
20675 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26  veOrderGroupBy(&
20676 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 70 42 79  sNC, p, pGroupBy
20677 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c 20 64 62  , "GROUP") || db
20678 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
20679 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
2067a 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
2067b 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
2067c 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42  0, pItem=pGroupB
2067d 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79  y->a; i<pGroupBy
2067e 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
2067f 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
20680 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
20681 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72  rty(pItem->pExpr
20682 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20  , EP_Agg) ){.   
20683 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
20684 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20685 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
20686 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  ons are not allo
20687 77 65 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20  wed in ".       
20688 20 20 20 20 20 20 20 22 74 68 65 20 47 52 4f 55         "the GROU
20689 50 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20  P BY clause");. 
2068a 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2068b 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20  WRC_Abort;.     
2068c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2068d 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e   }..    /* Advan
2068e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74  ce to the next t
2068f 65 72 6d 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f  erm of the compo
20690 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  und.    */.    p
20691 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
20692 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20    nCompound++;. 
20693 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65   }..  /* Resolve
20694 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 6e   the ORDER BY on
20695 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
20696 43 54 20 61 66 74 65 72 20 61 6c 6c 20 74 65 72  CT after all ter
20697 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63  ms of.  ** the c
20698 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65  ompound have bee
20699 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f  n resolved..  */
2069a 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e  .  if( isCompoun
2069b 64 20 26 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70  d && resolveComp
2069c 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72  oundOrderBy(pPar
2069d 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 29 20 29  se, pLeftmost) )
2069e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
2069f 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72  _Abort;.  }..  r
206a0 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
206a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
206a2 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20  outine walks an 
206a3 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
206a4 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 72 65 66  and resolves ref
206a5 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61  erences to.** ta
206a6 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20  ble columns and 
206a7 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d  result-set colum
206a8 6e 73 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65  ns.  At the same
206a9 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a   time, do error.
206aa 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66  ** checking on f
206ab 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 20 61 6e  unction usage an
206ac 64 20 73 65 74 20 61 20 66 6c 61 67 20 69 66 20  d set a flag if 
206ad 61 6e 79 20 61 67 67 72 65 67 61 74 65 20 66 75  any aggregate fu
206ae 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73  nctions.** are s
206af 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65  een..**.** To re
206b0 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 6f 6c 75  solve table colu
206b1 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 77  mns references w
206b2 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73  e look for nodes
206b3 20 28 6f 72 20 73 75 62 74 72 65 65 73 29 20 6f   (or subtrees) o
206b4 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58  f the .** form X
206b5 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a  .Y.Z or Y.Z or j
206b6 75 73 74 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a  ust Z where.**.*
206b7 2a 20 20 20 20 20 20 58 3a 20 20 20 54 68 65 20  *      X:   The 
206b8 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61  name of a databa
206b9 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 69 6e 22  se.  Ex:  "main"
206ba 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a   or "temp" or.**
206bb 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73             the s
206bc 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73  ymbolic name ass
206bd 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 54 54 41  igned to an ATTA
206be 43 48 2d 65 64 20 64 61 74 61 62 61 73 65 2e 0a  CH-ed database..
206bf 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a 20 20 20  **.**      Y:   
206c0 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 61  The name of a ta
206c1 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c  ble in a FROM cl
206c2 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 61 20 74  ause.  Or in a t
206c3 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  rigger.**       
206c4 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 65 20 73      one of the s
206c5 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c  pecial names "ol
206c6 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a  d" or "new"..**.
206c7 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 54 68 65  **      Z:   The
206c8 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d   name of a colum
206c9 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a  n in table Y..**
206ca 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20  .** The node at 
206cb 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20  the root of the 
206cc 73 75 62 74 72 65 65 20 69 73 20 6d 6f 64 69 66  subtree is modif
206cd 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ied as follows:.
206ce 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70  **.**    Expr.op
206cf 20 20 20 20 20 20 20 20 43 68 61 6e 67 65 64 20          Changed 
206d0 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20  to TK_COLUMN.** 
206d1 20 20 20 45 78 70 72 2e 70 54 61 62 20 20 20 20     Expr.pTab    
206d2 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20    Points to the 
206d3 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72  Table object for
206d4 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e   X.Y.**    Expr.
206d5 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f  iColumn   The co
206d6 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e  lumn index in X.
206d7 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 65 20 72  Y.  -1 for the r
206d8 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72  owid..**    Expr
206d9 2e 69 54 61 62 6c 65 20 20 20 20 54 68 65 20 56  .iTable    The V
206da 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
206db 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a  r for X.Y.**.**.
206dc 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 72 65  ** To resolve re
206dd 73 75 6c 74 2d 73 65 74 20 72 65 66 65 72 65 6e  sult-set referen
206de 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78  ces, look for ex
206df 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f  pression nodes o
206e0 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20  f the.** form Z 
206e1 28 77 69 74 68 20 6e 6f 20 58 20 61 6e 64 20 59  (with no X and Y
206e2 20 70 72 65 66 69 78 29 20 77 68 65 72 65 20 74   prefix) where t
206e3 68 65 20 5a 20 6d 61 74 63 68 65 73 20 74 68 65  he Z matches the
206e4 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73   right-hand.** s
206e5 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 63 6c 61  ize of an AS cla
206e6 75 73 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c  use in the resul
206e7 74 2d 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t-set of a SELEC
206e8 54 2e 20 20 54 68 65 20 5a 20 65 78 70 72 65 73  T.  The Z expres
206e9 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61  sion.** is repla
206ea 63 65 64 20 62 79 20 61 20 63 6f 70 79 20 6f 66  ced by a copy of
206eb 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73   the left-hand s
206ec 69 64 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ide of the resul
206ed 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
206ee 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20  ..** Table-name 
206ef 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 73  and function res
206f0 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f  olution occurs o
206f1 6e 20 74 68 65 20 73 75 62 73 74 69 74 75 74 65  n the substitute
206f2 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  d expression.** 
206f3 74 72 65 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  tree.  For examp
206f4 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  le, in:.**.**   
206f5 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53     SELECT a+b AS
206f6 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f   x, c+d AS y FRO
206f7 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 78 3b  M t1 ORDER BY x;
206f8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 22 20 74  .**.** The "x" t
206f9 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 64 65 72  erm of the order
206fa 20 62 79 20 69 73 20 72 65 70 6c 61 63 65 64 20   by is replaced 
206fb 62 79 20 22 61 2b 62 22 20 74 6f 20 72 65 6e 64  by "a+b" to rend
206fc 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53  er:.**.**      S
206fd 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20  ELECT a+b AS x, 
206fe 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31  c+d AS y FROM t1
206ff 20 4f 52 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a   ORDER BY a+b;.*
20700 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61  *.** Function ca
20701 6c 6c 73 20 61 72 65 20 63 68 65 63 6b 65 64 20  lls are checked 
20702 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
20703 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  t the function i
20704 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e  s .** defined an
20705 64 20 74 68 61 74 20 74 68 65 20 63 6f 72 72 65  d that the corre
20706 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ct number of arg
20707 75 6d 65 6e 74 73 20 61 72 65 20 73 70 65 63 69  uments are speci
20708 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  fied..** If the 
20709 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 61  function is an a
2070a 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2070b 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 4e 43 2d  n, then the pNC-
2070c 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a 20 73 65  >hasAgg is.** se
2070d 74 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65  t and the opcode
2070e 20 69 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d   is changed from
2070f 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20   TK_FUNCTION to 
20710 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e  TK_AGG_FUNCTION.
20711 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 72 65 73  .** If an expres
20712 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67  sion contains ag
20713 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
20714 73 20 74 68 65 6e 20 74 68 65 20 45 50 5f 41 67  s then the EP_Ag
20715 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e  g.** property on
20716 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20717 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e  is set..**.** An
20718 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
20719 73 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65  s left in pParse
2071a 20 69 66 20 61 6e 79 74 68 69 6e 67 20 69 73 20   if anything is 
2071b 61 6d 69 73 73 2e 20 20 54 68 65 20 6e 75 6d 62  amiss.  The numb
2071c 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f 72 73 20  er.** if errors 
2071d 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
2071e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2071f 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  nt sqlite3Resolv
20720 65 45 78 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e  eExprNames( .  N
20721 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
20722 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70         /* Namesp
20723 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 65  ace to resolve e
20724 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a  xpressions in. *
20725 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
20726 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
20727 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
20728 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f   be analyzed. */
20729 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 65 64 48  .){.  int savedH
2072a 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20  asAgg;.  Walker 
2072b 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d  w;..  if( pExpr=
2072c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  =0 ) return 0;.#
2072d 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  if SQLITE_MAX_EX
2072e 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20  PR_DEPTH>0.  {. 
2072f 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65     Parse *pParse
20730 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
20731 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
20732 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70  xprCheckHeight(p
20733 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48  Parse, pExpr->nH
20734 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73  eight+pNC->pPars
20735 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 7b 0a 20  e->nHeight) ){. 
20736 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
20737 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d     }.    pParse-
20738 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45 78 70  >nHeight += pExp
20739 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a  r->nHeight;.  }.
2073a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 64 48 61  #endif.  savedHa
2073b 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41  sAgg = pNC->hasA
2073c 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67  gg;.  pNC->hasAg
2073d 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72  g = 0;.  w.xExpr
2073e 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c  Callback = resol
2073f 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e  veExprStep;.  w.
20740 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
20741 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53  = resolveSelectS
20742 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20  tep;.  w.pParse 
20743 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20  = pNC->pParse;. 
20744 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a   w.u.pNC = pNC;.
20745 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70    sqlite3WalkExp
20746 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 23 69  r(&w, pExpr);.#i
20747 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
20748 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d  R_DEPTH>0.  pNC-
20749 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74  >pParse->nHeight
2074a 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67   -= pExpr->nHeig
2074b 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  ht;.#endif.  if(
2074c 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20   pNC->nErr>0 || 
2074d 77 2e 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  w.pParse->nErr>0
2074e 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50   ){.    ExprSetP
2074f 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20750 50 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20  P_Error);.  }.  
20751 69 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20  if( pNC->hasAgg 
20752 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
20753 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
20754 5f 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69  _Agg);.  }else i
20755 66 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29  f( savedHasAgg )
20756 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67  {.    pNC->hasAg
20757 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  g = 1;.  }.  ret
20758 75 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65  urn ExprHasPrope
20759 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72  rty(pExpr, EP_Er
2075a 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ror);.}.../*.** 
2075b 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65  Resolve all name
2075c 73 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73  s in all express
2075d 69 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54  ions of a SELECT
2075e 20 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64   and in all.** d
2075f 65 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65  ecendents of the
20760 20 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69   SELECT, includi
20761 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66  ng compounds off
20762 20 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a   of p->pPrior,.*
20763 2a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  * subqueries in 
20764 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64  expressions, and
20765 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64   subqueries used
20766 20 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a   as FROM clause.
20767 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20  ** terms..**.** 
20768 53 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  See sqlite3Resol
20769 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f  veExprNames() fo
2076a 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  r a description 
2076b 6f 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a  of the kinds of.
2076c 2a 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f  ** transformatio
2076d 6e 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a  ns that occur..*
2076e 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20  *.** All SELECT 
2076f 73 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c  statements shoul
20770 64 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61  d have been expa
20771 6e 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71  nded using.** sq
20772 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e  lite3SelectExpan
20773 64 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  d() prior to inv
20774 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  oking this routi
20775 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
20776 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20777 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
20778 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70  ames(.  Parse *p
20779 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
2077a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2077b 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
2077c 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
2077d 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2077e 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
2077f 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43  oded. */.  NameC
20780 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43  ontext *pOuterNC
20781 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
20782 74 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c  t for parent SEL
20783 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
20784 0a 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  .){.  Walker w;.
20785 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
20786 29 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c  );.  w.xExprCall
20787 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78  back = resolveEx
20788 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c  prStep;.  w.xSel
20789 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65  ectCallback = re
2078a 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b  solveSelectStep;
2078b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
2078c 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20  arse;.  w.u.pNC 
2078d 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71  = pOuterNC;.  sq
2078e 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
2078f 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  &w, p);.}../****
20790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
20791 66 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a  f resolve.c ****
20792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20793 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20794 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
20795 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
20796 20 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a   file expr.c ***
20797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20798 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20799 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2079a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
2079b 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2079c 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2079d 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2079e 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2079f 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
207a0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
207a1 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
207a2 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
207a3 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
207a4 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
207a5 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
207a6 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
207a7 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
207a8 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
207a9 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
207aa 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
207ab 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
207ac 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
207ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207b1 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
207b2 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73  ontains routines
207b3 20 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a   used for analyz
207b4 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ing expressions 
207b5 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72  and.** for gener
207b6 61 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20  ating VDBE code 
207b7 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65  that evaluates e
207b8 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51  xpressions in SQ
207b9 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Lite..*/../*.** 
207ba 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 69  Return the 'affi
207bb 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 70  nity' of the exp
207bc 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 66  ression pExpr if
207bd 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70   any..**.** If p
207be 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e  Expr is a column
207bf 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  , a reference to
207c0 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e   a column via an
207c1 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20   'AS' alias,.** 
207c2 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20  or a sub-select 
207c3 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73  with a column as
207c4 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
207c5 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20  e, then the .** 
207c6 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74  affinity of that
207c7 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72   column is retur
207c8 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
207c9 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 64  0x00 is returned
207ca 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  ,.** indicating 
207cb 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20  no affinity for 
207cc 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  the expression..
207cd 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57  **.** i.e. the W
207ce 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72  HERE clause expr
207cf 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  esssions in the 
207d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d  following statem
207d1 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65  ents all.** have
207d2 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a   an affinity:.**
207d3 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45  .** CREATE TABLE
207d4 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43   t1(a);.** SELEC
207d5 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52  T * FROM t1 WHER
207d6 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61  E a;.** SELECT a
207d7 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 48   AS b FROM t1 WH
207d8 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54  ERE b;.** SELECT
207d9 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   * FROM t1 WHERE
207da 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d 20   (select a from 
207db 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  t1);.*/.SQLITE_P
207dc 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
207dd 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
207de 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
207df 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  int op = pExpr->
207e0 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  op;.  if( op==TK
207e1 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 61  _SELECT ){.    a
207e2 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 66 6c  ssert( pExpr->fl
207e3 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 63 74  ags&EP_xIsSelect
207e4 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   );.    return s
207e5 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
207e6 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  ty(pExpr->x.pSel
207e7 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  ect->pEList->a[0
207e8 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 23 69  ].pExpr);.  }.#i
207e9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
207ea 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f 70 3d  T_CAST.  if( op=
207eb 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 20 20  =TK_CAST ){.    
207ec 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
207ed 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
207ee 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
207ef 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
207f0 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70  e3AffinityType(p
207f1 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b  Expr->u.zToken);
207f2 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66  .  }.#endif.  if
207f3 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f  ( (op==TK_AGG_CO
207f4 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43  LUMN || op==TK_C
207f5 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  OLUMN || op==TK_
207f6 52 45 47 49 53 54 45 52 29 20 0a 20 20 20 26 26  REGISTER) .   &&
207f7 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a   pExpr->pTab!=0.
207f8 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d    ){.    /* op==
207f9 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70  TK_REGISTER && p
207fa 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 20 68 61  Expr->pTab!=0 ha
207fb 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72  ppens when pExpr
207fc 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a   was originally.
207fd 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55      ** a TK_COLU
207fe 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69  MN but was previ
207ff 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20  ously evaluated 
20800 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20  and cached in a 
20801 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
20802 69 6e 74 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69  int j = pExpr->i
20803 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20  Column;.    if( 
20804 6a 3c 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  j<0 ) return SQL
20805 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b  ITE_AFF_INTEGER;
20806 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
20807 70 72 2d 3e 70 54 61 62 20 26 26 20 6a 3c 70 45  pr->pTab && j<pE
20808 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20  xpr->pTab->nCol 
20809 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 45  );.    return pE
2080a 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b  xpr->pTab->aCol[
2080b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d  j].affinity;.  }
2080c 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d  .  return pExpr-
2080d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a  >affinity;.}../*
2080e 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c  .** Set the coll
2080f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
20810 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  or expression pE
20811 78 70 72 20 74 6f 20 62 65 20 74 68 65 20 63 6f  xpr to be the co
20812 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65  llating.** seque
20813 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 70 54 6f  nce named by pTo
20814 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e 20 61 20  ken.   Return a 
20815 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
20816 65 76 69 73 65 64 20 65 78 70 72 65 73 73 69 6f  evised expressio
20817 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74  n..** The collat
20818 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
20819 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 6c 69  marked as "expli
2081a 63 69 74 22 20 75 73 69 6e 67 20 74 68 65 20 45  cit" using the E
2081b 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20  P_ExpCollate.** 
2081c 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 6c 69 63  flag.  An explic
2081d 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  it collating seq
2081e 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 65 72 72  uence will overr
2081f 69 64 65 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20  ide implicit.** 
20820 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
20821 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ces..*/.SQLITE_P
20822 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
20823 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
20824 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
20825 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65  xpr *pExpr, Toke
20826 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20  n *pCollName){. 
20827 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30   char *zColl = 0
20828 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
20829 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66  Dequoted name of
2082a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2082b 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  nce */.  CollSeq
2082c 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74   *pColl;.  sqlit
2082d 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2082e 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73  >db;.  zColl = s
2082f 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
20830 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d  ken(db, pCollNam
20831 65 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20  e);.  if( pExpr 
20832 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  && zColl ){.    
20833 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c  pColl = sqlite3L
20834 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61  ocateCollSeq(pPa
20835 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20  rse, zColl);.   
20836 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
20837 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c      pExpr->pColl
20838 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
20839 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20  pExpr->flags |= 
2083a 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
2083b 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
2083c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
2083d 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45  ll);.  return pE
2083e 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  xpr;.}../*.** Re
2083f 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74  turn the default
20840 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
20841 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72  nce for the expr
20842 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66  ession pExpr. If
20843 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
20844 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
20845 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30  n type, return 0
20846 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20847 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
20848 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
20849 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2084a 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43  xpr *pExpr){.  C
2084b 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
2084c 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70  0;.  Expr *p = p
2084d 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 41  Expr;.  while( A
2084e 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20  LWAYS(p) ){.    
2084f 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c  int op;.    pCol
20850 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  l = p->pColl;.  
20851 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62 72    if( pColl ) br
20852 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d  eak;.    op = p-
20853 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  >op;.    if( p->
20854 70 54 61 62 21 3d 30 20 26 26 20 28 0a 20 20 20  pTab!=0 && (.   
20855 20 20 20 20 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f       op==TK_AGG_
20856 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b  COLUMN || op==TK
20857 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
20858 4b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20 6f 70  K_REGISTER || op
20859 3d 3d 54 4b 5f 54 52 49 47 47 45 52 0a 20 20 20  ==TK_TRIGGER.   
2085a 20 29 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70   )){.      /* op
2085b 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26  ==TK_REGISTER &&
2085c 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70   p->pTab!=0 happ
2085d 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77  ens when pExpr w
2085e 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20  as originally.  
2085f 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55      ** a TK_COLU
20860 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69  MN but was previ
20861 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20  ously evaluated 
20862 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20  and cached in a 
20863 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
20864 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
20865 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  oll;.      int j
20866 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
20867 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b       if( j>=0 ){
20868 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20869 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2086a 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c  b;.        zColl
2086b 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c   = p->pTab->aCol
2086c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  [j].zColl;.     
2086d 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2086e 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2086f 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c  , ENC(db), zColl
20870 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 45  , 0);.        pE
20871 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f  xpr->pColl = pCo
20872 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
20873 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
20874 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41     if( op!=TK_CA
20875 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c  ST && op!=TK_UPL
20876 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61  US ){.      brea
20877 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d  k;.    }.    p =
20878 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20   p->pLeft;.  }. 
20879 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 65 63   if( sqlite3Chec
2087a 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  kCollSeq(pParse,
2087b 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20   pColl) ){ .    
2087c 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20  pColl = 0;.  }. 
2087d 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d   return pColl;.}
2087e 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73  ../*.** pExpr is
2087f 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 61   an operand of a
20880 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72   comparison oper
20881 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20 74  ator.  aff2 is t
20882 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e  he.** type affin
20883 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72  ity of the other
20884 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20   operand.  This 
20885 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
20886 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69  the.** type affi
20887 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
20888 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
20889 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72   comparison oper
2088a 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ator..*/.SQLITE_
2088b 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
2088c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
2088d 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c  ity(Expr *pExpr,
2088e 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20 63   char aff2){.  c
2088f 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69 74  har aff1 = sqlit
20890 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
20891 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66 66  Expr);.  if( aff
20892 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20 20  1 && aff2 ){.   
20893 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20 6f   /* Both sides o
20894 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  f the comparison
20895 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66   are columns. If
20896 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 63   one has numeric
20897 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 79  .    ** affinity
20898 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68 65  , use that. Othe
20899 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 66  rwise use no aff
2089a 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  inity..    */.  
2089b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e    if( sqlite3IsN
2089c 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61  umericAffinity(a
2089d 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49  ff1) || sqlite3I
2089e 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  sNumericAffinity
2089f 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20 20  (aff2) ){.      
208a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46  return SQLITE_AF
208a1 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d  F_NUMERIC;.    }
208a2 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75  else{.      retu
208a3 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  rn SQLITE_AFF_NO
208a4 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  NE;.    }.  }els
208a5 65 20 69 66 28 20 21 61 66 66 31 20 26 26 20 21  e if( !aff1 && !
208a6 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e  aff2 ){.    /* N
208a7 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74  either side of t
208a8 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  he comparison is
208a9 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70   a column.  Comp
208aa 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72  are the.    ** r
208ab 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79 2e  esults directly.
208ac 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75  .    */.    retu
208ad 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  rn SQLITE_AFF_NO
208ae 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  NE;.  }else{.   
208af 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 20   /* One side is 
208b0 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74  a column, the ot
208b1 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 20  her is not. Use 
208b2 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69  the columns affi
208b3 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73  nity. */.    ass
208b4 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20  ert( aff1==0 || 
208b5 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72  aff2==0 );.    r
208b6 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61 66  eturn (aff1 + af
208b7 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  f2);.  }.}../*.*
208b8 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d  * pExpr is a com
208b9 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
208ba 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 79  .  Return the ty
208bb 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74  pe affinity that
208bc 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70   should.** be ap
208bd 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f 70  plied to both op
208be 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f 20  erands prior to 
208bf 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 72  doing the compar
208c0 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ison..*/.static 
208c1 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41  char comparisonA
208c2 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45  ffinity(Expr *pE
208c3 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66 66  xpr){.  char aff
208c4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
208c5 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20  r->op==TK_EQ || 
208c6 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pExpr->op==TK_IN
208c7 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54   || pExpr->op==T
208c8 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 20  K_LT ||.        
208c9 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f    pExpr->op==TK_
208ca 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  GT || pExpr->op=
208cb 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d  =TK_GE || pExpr-
208cc 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20  >op==TK_LE ||.  
208cd 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
208ce 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 70 45 78 70  p==TK_NE || pExp
208cf 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20  r->op==TK_IS || 
208d0 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  pExpr->op==TK_IS
208d1 4e 4f 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  NOT );.  assert(
208d2 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29 3b   pExpr->pLeft );
208d3 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33  .  aff = sqlite3
208d4 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78  ExprAffinity(pEx
208d5 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66  pr->pLeft);.  if
208d6 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20  ( pExpr->pRight 
208d7 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c  ){.    aff = sql
208d8 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
208d9 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ity(pExpr->pRigh
208da 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65  t, aff);.  }else
208db 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
208dc 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
208dd 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
208de 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f   aff = sqlite3Co
208df 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45  mpareAffinity(pE
208e0 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e  xpr->x.pSelect->
208e1 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
208e2 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73  pr, aff);.  }els
208e3 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 20  e if( !aff ){.  
208e4 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
208e5 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72  FF_NONE;.  }.  r
208e6 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a  eturn aff;.}../*
208e7 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63  .** pExpr is a c
208e8 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 73  omparison expres
208e9 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27  sion, eg. '=', '
208ea 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e  <', IN(...) etc.
208eb 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 79  .** idx_affinity
208ec 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 79   is the affinity
208ed 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63   of an indexed c
208ee 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72  olumn. Return tr
208ef 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e 64  ue.** if the ind
208f0 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 79  ex with affinity
208f1 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d 61   idx_affinity ma
208f2 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70  y be used to imp
208f3 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f  lement.** the co
208f4 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 70  mparison in pExp
208f5 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
208f6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
208f7 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28  IndexAffinityOk(
208f8 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
208f9 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 7b  r idx_affinity){
208fa 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f  .  char aff = co
208fb 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
208fc 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 63  (pExpr);.  switc
208fd 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 61  h( aff ){.    ca
208fe 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  se SQLITE_AFF_NO
208ff 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e  NE:.      return
20900 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   1;.    case SQL
20901 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20  ITE_AFF_TEXT:.  
20902 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f 61      return idx_a
20903 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
20904 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 65  AFF_TEXT;.    de
20905 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 74  fault:.      ret
20906 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  urn sqlite3IsNum
20907 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 78  ericAffinity(idx
20908 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a  _affinity);.  }.
20909 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2090a 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 61  the P5 value tha
2090b 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
2090c 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 6f   for a binary co
2090d 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f  mparison.** opco
2090e 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65  de (OP_Eq, OP_Ge
2090f 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 63   etc.) used to c
20910 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 6e  ompare pExpr1 an
20911 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61  d pExpr2..*/.sta
20912 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f 6d  tic u8 binaryCom
20913 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 78  pareP5(Expr *pEx
20914 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pr1, Expr *pExpr
20915 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c  2, int jumpIfNul
20916 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 28  l){.  u8 aff = (
20917 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 72  char)sqlite3Expr
20918 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 29  Affinity(pExpr2)
20919 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 71  ;.  aff = (u8)sq
2091a 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
2091b 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66  nity(pExpr1, aff
2091c 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e 75  ) | (u8)jumpIfNu
2091d 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 66  ll;.  return aff
2091e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
2091f 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
20920 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
20921 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 6c  uence that shoul
20922 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  d be used by.** 
20923 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69  a binary compari
20924 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d  son operator com
20925 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e 64  paring pLeft and
20926 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49   pRight..**.** I
20927 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
20928 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61  expression has a
20929 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2092a 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 69  nce type, then i
2092b 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74  t is.** used. Ot
2092c 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c 6c  herwise the coll
2092d 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
2092e 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 6e  or the right han
2092f 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  d expression.** 
20930 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 20  is used, or the 
20931 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 29  default (BINARY)
20932 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 72   if neither expr
20933 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c  ession has a col
20934 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a  lating.** type..
20935 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70  **.** Argument p
20936 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 70  Right (but not p
20937 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 6e  Left) may be a n
20938 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20  ull pointer. In 
20939 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74  this case,.** it
2093a 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72   is not consider
2093b 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2093c 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
2093d 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
2093e 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61  areCollSeq(.  Pa
2093f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20  rse *pParse, .  
20940 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20  Expr *pLeft, .  
20941 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a  Expr *pRight.){.
20942 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
20943 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  ;.  assert( pLef
20944 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  t );.  if( pLeft
20945 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
20946 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61  Collate ){.    a
20947 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 43  ssert( pLeft->pC
20948 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  oll );.    pColl
20949 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b   = pLeft->pColl;
2094a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69  .  }else if( pRi
2094b 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 66  ght && pRight->f
2094c 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
2094d 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65  late ){.    asse
2094e 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  rt( pRight->pCol
2094f 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  l );.    pColl =
20950 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a   pRight->pColl;.
20951 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f    }else{.    pCo
20952 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
20953 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
20954 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
20955 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
20956 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
20957 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
20958 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20  e, pRight);.    
20959 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
2095a 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Coll;.}../*.** G
2095b 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 72  enerate the oper
2095c 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 61  ands for a compa
2095d 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e  rison operation.
2095e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65    Before.** gene
2095f 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20  rating the code 
20960 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e 64  for each operand
20961 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e 79  , set the EP_Any
20962 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74  Aff.** flag on t
20963 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f  he expression so
20964 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65   that it will be
20965 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 0a   able to used a.
20966 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e  ** cached column
20967 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 20   value that has 
20968 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 72  previously under
20969 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e  gone an.** affin
2096a 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73  ity change..*/.s
2096b 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 43  tatic void codeC
2096c 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 0a  ompareOperands(.
2096d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2096e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61      /* Parsing a
2096f 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  nd code generati
20970 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
20971 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
20972 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
20973 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  erand */.  int *
20974 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20  pRegLeft,    /* 
20975 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 6c  Register where l
20976 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73  eft operand is s
20977 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  tored */.  int *
20978 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20  pFreeLeft,   /* 
20979 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 74  Free this regist
2097a 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a  er when done */.
2097b 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20    Expr *pRight, 
2097c 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
2097d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
2097e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 20  t *pRegRight,   
2097f 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72  /* Register wher
20980 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
20981 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69  is stored */.  i
20982 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 20  nt *pFreeRight  
20983 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 72   /* Write temp r
20984 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 68  egister for righ
20985 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 20  t operand there 
20986 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
20987 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  Left->op==TK_UPL
20988 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c 65  US ) pLeft = pLe
20989 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65  ft->pLeft;.  pLe
2098a 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ft->flags |= EP_
2098b 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c  AnyAff;.  *pRegL
2098c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  eft = sqlite3Exp
2098d 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
2098e 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c 65  , pLeft, pFreeLe
2098f 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 52  ft);.  while( pR
20990 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  ight->op==TK_UPL
20991 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 52  US ) pRight = pR
20992 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70  ight->pLeft;.  p
20993 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  Right->flags |= 
20994 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52  EP_AnyAff;.  *pR
20995 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65  egRight = sqlite
20996 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
20997 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 46  arse, pRight, pF
20998 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a  reeRight);.}../*
20999 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2099a 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73  e for a comparis
2099b 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a  on operator..*/.
2099c 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 43  static int codeC
2099d 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 20  ompare(.  Parse 
2099e 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 54  *pParse,    /* T
2099f 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 20  he parsing (and 
209a0 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 29  code generating)
209a1 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
209a2 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20  pr *pLeft,      
209a3 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72  /* The left oper
209a4 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  and */.  Expr *p
209a5 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 68  Right,     /* Th
209a6 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
209a7 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c  */.  int opcode,
209a8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
209a9 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20  mparison opcode 
209aa 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e  */.  int in1, in
209ab 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74  t in2, /* Regist
209ac 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61  er holding opera
209ad 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73  nds */.  int des
209ae 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t,         /* Ju
209af 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 2e  mp here if true.
209b0 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49    */.  int jumpI
209b1 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74  fNull    /* If t
209b2 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 74  rue, jump if eit
209b3 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e  her operand is N
209b4 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ULL */.){.  int 
209b5 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a  p5;.  int addr;.
209b6 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a    CollSeq *p4;..
209b7 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 69    p4 = sqlite3Bi
209b8 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53  naryCompareCollS
209b9 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  eq(pParse, pLeft
209ba 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 20  , pRight);.  p5 
209bb 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50  = binaryCompareP
209bc 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  5(pLeft, pRight,
209bd 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
209be 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
209bf 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d  beAddOp4(pParse-
209c0 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20  >pVdbe, opcode, 
209c1 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a  in2, dest, in1,.
209c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
209c3 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
209c4 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51  *)p4, P4_COLLSEQ
209c5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
209c6 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 2d  ChangeP5(pParse-
209c7 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 3b  >pVdbe, (u8)p5);
209c8 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 4c  .  if( (p5 & SQL
209c9 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53  ITE_AFF_MASK)!=S
209ca 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
209cb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
209cc 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
209cd 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 31  ange(pParse, in1
209ce 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
209cf 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
209d0 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
209d1 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20   in2, 1);.  }.  
209d2 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a  return addr;.}..
209d3 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45  #if SQLITE_MAX_E
209d4 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a  XPR_DEPTH>0./*.*
209d5 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 67  * Check that arg
209d6 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 73  ument nHeight is
209d7 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
209d8 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  ual to the maxim
209d9 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  um.** expression
209da 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20   depth allowed. 
209db 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65  If it is not, le
209dc 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
209dd 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73  sage in.** pPars
209de 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
209df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
209e0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
209e1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
209e2 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 69  nt nHeight){.  i
209e3 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
209e4 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68  K;.  int mxHeigh
209e5 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  t = pParse->db->
209e6 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
209e7 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b  MIT_EXPR_DEPTH];
209e8 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e 6d  .  if( nHeight>m
209e9 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 73  xHeight ){.    s
209ea 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
209eb 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22  Parse, .       "
209ec 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  Expression tree 
209ed 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61  is too large (ma
209ee 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 22  ximum depth %d)"
209ef 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 29  , mxHeight.    )
209f0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
209f1 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
209f2 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
209f3 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  The following th
209f4 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68  ree functions, h
209f5 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68  eightOfExpr(), h
209f6 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
209f7 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f  ).** and heightO
209f8 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75  fSelect(), are u
209f9 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
209fa 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69   the maximum hei
209fb 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78  ght.** of any ex
209fc 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65  pression tree re
209fd 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20  ferenced by the 
209fe 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
209ff 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74   as the.** first
20a00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
20a01 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d   If this maximum
20a02 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74   height is great
20a03 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  er than the curr
20a04 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  ent value pointe
20a05 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69  d.** to by pnHei
20a06 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  ght, the second 
20a07 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20  parameter, then 
20a08 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f  set *pnHeight to
20a09 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a   that.** value..
20a0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  */.static void h
20a0b 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72  eightOfExpr(Expr
20a0c 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67   *p, int *pnHeig
20a0d 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ht){.  if( p ){.
20a0e 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67      if( p->nHeig
20a0f 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a  ht>*pnHeight ){.
20a10 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20        *pnHeight 
20a11 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20  = p->nHeight;.  
20a12 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
20a13 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78   void heightOfEx
20a14 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20  prList(ExprList 
20a15 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
20a16 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
20a17 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
20a18 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70  r(i=0; i<p->nExp
20a19 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68  r; i++){.      h
20a1a 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61  eightOfExpr(p->a
20a1b 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69  [i].pExpr, pnHei
20a1c 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
20a1d 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  }.static void he
20a1e 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c  ightOfSelect(Sel
20a1f 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48  ect *p, int *pnH
20a20 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20  eight){.  if( p 
20a21 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  ){.    heightOfE
20a22 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70  xpr(p->pWhere, p
20a23 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
20a24 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48  ightOfExpr(p->pH
20a25 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29  aving, pnHeight)
20a26 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
20a27 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e  pr(p->pLimit, pn
20a28 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
20a29 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66  ghtOfExpr(p->pOf
20a2a 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b  fset, pnHeight);
20a2b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
20a2c 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c  rList(p->pEList,
20a2d 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
20a2e 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
20a2f 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e  (p->pGroupBy, pn
20a30 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
20a31 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
20a32 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69  >pOrderBy, pnHei
20a33 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
20a34 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69  OfSelect(p->pPri
20a35 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  or, pnHeight);. 
20a36 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
20a37 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74  the Expr.nHeight
20a38 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65   variable in the
20a39 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
20a3a 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75  d as an .** argu
20a3b 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73  ment. An express
20a3c 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c  ion with no chil
20a3d 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74  dren, Expr.pList
20a3e 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65   or .** Expr.pSe
20a3f 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20  lect member has 
20a40 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41  a height of 1. A
20a41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73  ny other express
20a42 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69  ion.** has a hei
20a43 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65  ght equal to the
20a44 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20   maximum height 
20a45 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a  of any other .**
20a46 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72   referenced Expr
20a47 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74   plus one..*/.st
20a48 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 65  atic void exprSe
20a49 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 29  tHeight(Expr *p)
20a4a 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20  {.  int nHeight 
20a4b 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45  = 0;.  heightOfE
20a4c 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e  xpr(p->pLeft, &n
20a4d 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68  Height);.  heigh
20a4e 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68  tOfExpr(p->pRigh
20a4f 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, &nHeight);.  
20a50 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
20a51 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c  rty(p, EP_xIsSel
20a52 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69 67  ect) ){.    heig
20a53 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78 2e  htOfSelect(p->x.
20a54 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 68  pSelect, &nHeigh
20a55 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
20a56 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
20a57 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e  t(p->x.pList, &n
20a58 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70  Height);.  }.  p
20a59 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69  ->nHeight = nHei
20a5a 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ght + 1;.}../*.*
20a5b 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e  * Set the Expr.n
20a5c 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 20  Height variable 
20a5d 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 65  using the exprSe
20a5e 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 69  tHeight() functi
20a5f 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 65  on. If.** the he
20a60 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20  ight is greater 
20a61 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  than the maximum
20a62 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 73   allowed express
20a63 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c 65  ion depth,.** le
20a64 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  ave an error in 
20a65 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
20a66 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20a67 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
20a68 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ght(Parse *pPars
20a69 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 65  e, Expr *p){.  e
20a6a 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29 3b  xprSetHeight(p);
20a6b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  .  sqlite3ExprCh
20a6c 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 65  eckHeight(pParse
20a6d 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 7d  , p->nHeight);.}
20a6e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
20a6f 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68  he maximum heigh
20a70 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73  t of any express
20a71 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e  ion tree referen
20a72 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 65  ced.** by the se
20a73 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70  lect statement p
20a74 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
20a75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
20a76 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20a77 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69  te3SelectExprHei
20a78 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a  ght(Select *p){.
20a79 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20    int nHeight = 
20a7a 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 6c  0;.  heightOfSel
20a7b 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 29  ect(p, &nHeight)
20a7c 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 67  ;.  return nHeig
20a7d 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64  ht;.}.#else.  #d
20a7e 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 69  efine exprSetHei
20a7f 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f 2a  ght(y).#endif /*
20a80 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
20a81 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a  _DEPTH>0 */../*.
20a82 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
20a83 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c 6c 6f  is the core allo
20a84 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72 20 6e  cator for Expr n
20a85 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73  odes..**.** Cons
20a86 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72  truct a new expr
20a87 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20  ession node and 
20a88 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
20a89 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a   to it.  Memory.
20a8a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65  ** for this node
20a8b 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 54 6f   and for the pTo
20a8c 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ken argument is 
20a8d 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74  a single allocat
20a8e 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20  ion.** obtained 
20a8f 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d 61  from sqlite3DbMa
20a90 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61 6c  lloc().  The cal
20a91 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
20a92 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
20a93 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20  for making sure 
20a94 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 61  the node eventua
20a95 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e 0a  lly gets freed..
20a96 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65  **.** If dequote
20a97 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
20a98 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74 20  he token (if it 
20a99 65 78 69 73 74 73 29 20 69 73 20 64 65 71 75 6f  exists) is dequo
20a9a 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 75 6f  ted..** If dequo
20a9b 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e 6f 20  te is false, no 
20a9c 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70 65 72  dequoting is per
20a9d 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 20 64  formance.  The d
20a9e 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 6d 65  eQuote.** parame
20a9f 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 69  ter is ignored i
20aa0 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c  f pToken is NULL
20aa1 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b 65 6e   or if the token
20aa2 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 70   does not.** app
20aa3 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74 65 64  ear to be quoted
20aa4 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74 65 73  .  If the quotes
20aa5 20 77 65 72 65 20 6f 66 20 74 68 65 20 66 6f 72   were of the for
20aa6 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 2d  m "..." (double-
20aa7 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 6e 20  quotes).** then 
20aa8 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64  the EP_DblQuoted
20aa9 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20   flag is set on 
20aaa 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  the expression n
20aab 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69  ode..**.** Speci
20aac 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f 70 3d  al case:  If op=
20aad 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e 64 20  =TK_INTEGER and 
20aae 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 74 6f  pToken points to
20aaf 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a 2a   a string that.*
20ab0 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73 6c 61  * can be transla
20ab1 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d 62 69  ted into a 32-bi
20ab2 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20  t integer, then 
20ab3 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e 6f 74  the token is not
20ab4 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 75 2e  .** stored in u.
20ab5 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 61 64  zToken.  Instead
20ab6 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  , the integer va
20ab7 6c 75 65 73 20 69 73 20 77 72 69 74 74 65 6e 0a  lues is written.
20ab8 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c 75 65  ** into u.iValue
20ab9 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e 74 56   and the EP_IntV
20aba 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73 65 74  alue flag is set
20abb 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74 6f 72  .  No extra stor
20abc 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61  age.** is alloca
20abd 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ted to hold the 
20abe 69 6e 74 65 67 65 72 20 74 65 78 74 20 61 6e 64  integer text and
20abf 20 74 68 65 20 64 65 71 75 6f 74 65 20 66 6c 61   the dequote fla
20ac0 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f  g is ignored..*/
20ac1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20ac2 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
20ac3 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65  rAlloc(.  sqlite
20ac4 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
20ac5 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20    /* Handle for 
20ac6 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
20ac7 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75  ero() (may be nu
20ac8 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  ll) */.  int op,
20ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20aca 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f   /* Expression o
20acb 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  pcode */.  const
20acc 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20   Token *pToken, 
20acd 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75     /* Token argu
20ace 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20  ment.  Might be 
20acf 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 65  NULL */.  int de
20ad0 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 20  quote           
20ad1 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65 71    /* True to deq
20ad2 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  uote */.){.  Exp
20ad3 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e  r *pNew;.  int n
20ad4 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 6e 74  Extra = 0;.  int
20ad5 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 20   iValue = 0;..  
20ad6 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20  if( pToken ){.  
20ad7 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 54    if( op!=TK_INT
20ad8 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e  EGER || pToken->
20ad9 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 7c  z==0.          |
20ada 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33  | sqlite3GetInt3
20adb 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56  2(pToken->z, &iV
20adc 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  alue)==0 ){.    
20add 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f 6b 65    nExtra = pToke
20ade 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20  n->n+1;.    }.  
20adf 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  }.  pNew = sqlit
20ae0 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
20ae1 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 2b  b, sizeof(Expr)+
20ae2 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28 20 70  nExtra);.  if( p
20ae3 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d  New ){.    pNew-
20ae4 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20  >op = (u8)op;.  
20ae5 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d    pNew->iAgg = -
20ae6 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 6b 65  1;.    if( pToke
20ae7 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  n ){.      if( n
20ae8 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20 20 20  Extra==0 ){.    
20ae9 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
20aea 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a  |= EP_IntValue;.
20aeb 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e          pNew->u.
20aec 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 65 3b  iValue = iValue;
20aed 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
20aee 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20        int c;.   
20aef 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f       pNew->u.zTo
20af0 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 70 4e  ken = (char*)&pN
20af1 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d  ew[1];.        m
20af2 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e 7a 54  emcpy(pNew->u.zT
20af3 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c  oken, pToken->z,
20af4 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20   pToken->n);.   
20af5 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f       pNew->u.zTo
20af6 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d  ken[pToken->n] =
20af7 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
20af8 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78 74 72  dequote && nExtr
20af9 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20 20 20  a>=3 .          
20afa 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f 6b     && ((c = pTok
20afb 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20  en->z[0])=='\'' 
20afc 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d  || c=='"' || c==
20afd 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 29  '[' || c=='`') )
20afe 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
20aff 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65 77 2d  te3Dequote(pNew-
20b00 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
20b01 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22 27        if( c=='"'
20b02 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c   ) pNew->flags |
20b03 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b 0a  = EP_DblQuoted;.
20b04 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
20b05 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51 4c 49  }.    }.#if SQLI
20b06 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
20b07 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 48  H>0.    pNew->nH
20b08 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e 64 69  eight = 1;.#endi
20b09 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  f  .  }.  return
20b0a 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
20b0b 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65  Allocate a new e
20b0c 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66  xpression node f
20b0d 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69  rom a zero-termi
20b0e 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68 61 74  nated token that
20b0f 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
20b10 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e 0a 2a  been dequoted..*
20b11 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20b12 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
20b13 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  pr(.  sqlite3 *d
20b14 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
20b15 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69   Handle for sqli
20b16 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
20b17 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20  ) (may be null) 
20b18 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
20b19 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20b1a 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64  Expression opcod
20b1b 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
20b1c 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20 20 2f  r *zToken      /
20b1d 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74  * Token argument
20b1e 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  .  Might be NULL
20b1f 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 78   */.){.  Token x
20b20 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e  ;.  x.z = zToken
20b21 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e  ;.  x.n = zToken
20b22 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   ? sqlite3Strlen
20b23 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a  30(zToken) : 0;.
20b24 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
20b25 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 6f 70  ExprAlloc(db, op
20b26 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  , &x, 0);.}../*.
20b27 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74 72 65  ** Attach subtre
20b28 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  es pLeft and pRi
20b29 67 68 74 20 74 6f 20 74 68 65 20 45 78 70 72 20  ght to the Expr 
20b2a 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a  node pRoot..**.*
20b2b 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c  * If pRoot==NULL
20b2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
20b2d 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
20b2e 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  tion error has o
20b2f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e 20 74  ccurred..** In t
20b30 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65 74 65  hat case, delete
20b31 20 74 68 65 20 73 75 62 74 72 65 65 73 20 70 4c   the subtrees pL
20b32 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 0a  eft and pRight..
20b33 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20b34 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
20b35 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73  prAttachSubtrees
20b36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
20b37 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74 2c 0a  .  Expr *pRoot,.
20b38 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 0a 20    Expr *pLeft,. 
20b39 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b   Expr *pRight.){
20b3a 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d 30 20  .  if( pRoot==0 
20b3b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
20b3c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20b3d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
20b3e 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65  prDelete(db, pLe
20b3f 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
20b40 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
20b41 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  Right);.  }else{
20b42 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20  .    if( pRight 
20b43 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e  ){.      pRoot->
20b44 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b  pRight = pRight;
20b45 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68  .      if( pRigh
20b46 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
20b47 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
20b48 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73      pRoot->flags
20b49 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
20b4a 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74  e;.        pRoot
20b4b 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74  ->pColl = pRight
20b4c 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d  ->pColl;.      }
20b4d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
20b4e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 70 52  Left ){.      pR
20b4f 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65  oot->pLeft = pLe
20b50 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c  ft;.      if( pL
20b51 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  eft->flags & EP_
20b52 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20  ExpCollate ){.  
20b53 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61        pRoot->fla
20b54 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
20b55 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f  ate;.        pRo
20b56 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66  ot->pColl = pLef
20b57 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  t->pColl;.      
20b58 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78 70 72  }.    }.    expr
20b59 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f 74 29  SetHeight(pRoot)
20b5a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
20b5b 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72 20 6e  llocate a Expr n
20b5c 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e 73 20  ode which joins 
20b5d 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f 20 73  as many as two s
20b5e 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f  ubtrees..**.** O
20b5f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 74 68  ne or both of th
20b60 65 20 73 75 62 74 72 65 65 73 20 63 61 6e 20 62  e subtrees can b
20b61 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72 6e 20  e NULL.  Return 
20b62 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
20b63 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e 6f 64   new.** Expr nod
20b64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20 4f 4f  e.  Or, if an OO
20b65 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
20b66 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  set pParse->db->
20b67 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a  mallocFailed,.**
20b68 20 66 72 65 65 20 74 68 65 20 73 75 62 74 72 65   free the subtre
20b69 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55  es and return NU
20b6a 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  LL..*/.SQLITE_PR
20b6b 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
20b6c 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 73  te3PExpr(.  Pars
20b6d 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
20b6e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
20b6f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
20b70 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
20b71 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
20b72 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78  n opcode */.  Ex
20b73 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20  pr *pLeft,      
20b74 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70        /* Left op
20b75 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20  erand */.  Expr 
20b76 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20  *pRight,        
20b77 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72     /* Right oper
20b78 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54  and */.  const T
20b79 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20  oken *pToken    
20b7a 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b   /* Argument tok
20b7b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  en */.){.  Expr 
20b7c 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  *p = sqlite3Expr
20b7d 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64 62  Alloc(pParse->db
20b7e 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20 31 29  , op, pToken, 1)
20b7f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 41  ;.  sqlite3ExprA
20b80 74 74 61 63 68 53 75 62 74 72 65 65 73 28 70 50  ttachSubtrees(pP
20b81 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70 4c 65  arse->db, p, pLe
20b82 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 72  ft, pRight);.  r
20b83 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
20b84 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65  * Join two expre
20b85 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20  ssions using an 
20b86 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49  AND operator.  I
20b87 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73  f either express
20b88 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20  ion is.** NULL, 
20b89 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e  then just return
20b8a 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65   the other expre
20b8b 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ssion..*/.SQLITE
20b8c 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
20b8d 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71  qlite3ExprAnd(sq
20b8e 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
20b8f 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 52  *pLeft, Expr *pR
20b90 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c 65  ight){.  if( pLe
20b91 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ft==0 ){.    ret
20b92 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d 65  urn pRight;.  }e
20b93 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d 3d  lse if( pRight==
20b94 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
20b95 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pLeft;.  }else{.
20b96 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d      Expr *pNew =
20b97 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f   sqlite3ExprAllo
20b98 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c  c(db, TK_AND, 0,
20b99 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
20b9a 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65  ExprAttachSubtre
20b9b 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70 4c 65  es(db, pNew, pLe
20b9c 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  ft, pRight);.   
20b9d 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 20 20   return pNew;.  
20b9e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  }.}../*.** Const
20b9f 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65  ruct a new expre
20ba0 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61  ssion node for a
20ba1 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d   function with m
20ba2 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d  ultiple.** argum
20ba3 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
20ba4 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
20ba5 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
20ba6 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
20ba7 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
20ba8 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29  , Token *pToken)
20ba9 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a  {.  Expr *pNew;.
20baa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20bab 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
20bac 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a  sert( pToken );.
20bad 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
20bae 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b  ExprAlloc(db, TK
20baf 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b 65  _FUNCTION, pToke
20bb0 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 4e 65  n, 1);.  if( pNe
20bb1 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  w==0 ){.    sqli
20bb2 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
20bb3 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a  e(db, pList); /*
20bb4 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 6c 65   Avoid memory le
20bb5 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66  ak when malloc f
20bb6 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  ails */.    retu
20bb7 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  rn 0;.  }.  pNew
20bb8 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ->x.pList = pLis
20bb9 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78  t;.  assert( !Ex
20bba 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e  prHasProperty(pN
20bbb 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  ew, EP_xIsSelect
20bbc 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ) );.  sqlite3Ex
20bbd 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
20bbe 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74  se, pNew);.  ret
20bbf 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
20bc0 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 69  ** Assign a vari
20bc1 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61  able number to a
20bc2 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
20bc3 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64  t encodes a wild
20bc4 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f  card.** in the o
20bc5 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
20bc6 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57  ement.  .**.** W
20bc7 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74  ildcards consist
20bc8 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ing of a single 
20bc9 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  "?" are assigned
20bca 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e   the next sequen
20bcb 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65  tial.** variable
20bcc 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57   number..**.** W
20bcd 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
20bce 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20  form "?nnn" are 
20bcf 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d  assigned the num
20bd0 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d  ber "nnn".  We m
20bd1 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e  ake.** sure "nnn
20bd2 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20  " is not too be 
20bd3 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61  to avoid a denia
20bd4 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 74  l of service att
20bd5 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ack when.** the 
20bd6 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  SQL statement co
20bd7 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65  mes from an exte
20bd8 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a  rnal source..**.
20bd9 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20  ** Wildcards of 
20bda 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 2c  the form ":aaa",
20bdb 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24 61 61   "@aaa", or "$aa
20bdc 61 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20  a" are assigned 
20bdd 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a  the same number.
20bde 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 6f  ** as the previo
20bdf 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  us instance of t
20be0 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 64  he same wildcard
20be1 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 73  .  Or if this is
20be2 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e   the first.** in
20be3 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 69  stance of the wi
20be4 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 74  ldcard, the next
20be5 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 61   sequenial varia
20be6 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a  ble number is.**
20be7 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51   assigned..*/.SQ
20be8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20be9 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73  d sqlite3ExprAss
20bea 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72  ignVarNumber(Par
20beb 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
20bec 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71 6c 69   *pExpr){.  sqli
20bed 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
20bee 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ->db;.  const ch
20bef 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20 70 45  ar *z;..  if( pE
20bf0 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  xpr==0 ) return;
20bf1 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
20bf2 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
20bf3 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
20bf4 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f  e|EP_Reduced|EP_
20bf5 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20  TokenOnly) );.  
20bf6 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  z = pExpr->u.zTo
20bf7 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  ken;.  assert( z
20bf8 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
20bf9 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66   z[0]!=0 );.  if
20bfa 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20  ( z[1]==0 ){.   
20bfb 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20   /* Wildcard of 
20bfc 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41  the form "?".  A
20bfd 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20 76  ssign the next v
20bfe 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a  ariable number *
20bff 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b  /.    assert( z[
20c00 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20 20 70  0]=='?' );.    p
20c01 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
20c02 28 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73 65  (ynVar)(++pParse
20c03 2d 3e 6e 56 61 72 29 3b 0a 20 20 7d 65 6c 73 65  ->nVar);.  }else
20c04 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29   if( z[0]=='?' )
20c05 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72  {.    /* Wildcar
20c06 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f  d of the form "?
20c07 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 22  nnn".  Convert "
20c08 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 67  nnn" to an integ
20c09 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73  er and.    ** us
20c0a 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72 69  e it as the vari
20c0b 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  able number */. 
20c0c 20 20 20 69 6e 74 20 69 20 3d 20 61 74 6f 69 28     int i = atoi(
20c0d 28 63 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20  (char*)&z[1]);. 
20c0e 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
20c0f 6e 20 3d 20 28 79 6e 56 61 72 29 69 3b 0a 20 20  n = (ynVar)i;.  
20c10 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30    testcase( i==0
20c11 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
20c12 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65  ( i==1 );.    te
20c13 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61  stcase( i==db->a
20c14 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
20c15 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
20c16 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73  ER]-1 );.    tes
20c17 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c  tcase( i==db->aL
20c18 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
20c19 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
20c1a 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c  R] );.    if( i<
20c1b 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69  1 || i>db->aLimi
20c1c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
20c1d 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
20c1e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20c1f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20c20 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65   "variable numbe
20c21 72 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65  r must be betwee
20c22 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20  n ?1 and ?%d",. 
20c23 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69           db->aLi
20c24 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
20c25 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
20c26 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ]);.    }.    if
20c27 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72  ( i>pParse->nVar
20c28 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65   ){.      pParse
20c29 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20  ->nVar = i;.    
20c2a 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
20c2b 2a 20 57 69 6c 64 63 61 72 64 73 20 6c 69 6b 65  * Wildcards like
20c2c 20 22 3a 61 61 61 22 2c 20 22 24 61 61 61 22 20   ":aaa", "$aaa" 
20c2d 6f 72 20 22 40 61 61 61 22 2e 20 20 52 65 75 73  or "@aaa".  Reus
20c2e 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61  e the same varia
20c2f 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65  ble.    ** numbe
20c30 72 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61  r as the prior a
20c31 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65  ppearance of the
20c32 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69   same name, or i
20c33 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a  f the name.    *
20c34 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65  * has never appe
20c35 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75  ared before, reu
20c36 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69  se the same vari
20c37 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20  able number.    
20c38 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  */.    int i;.  
20c39 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d    u32 n;.    n =
20c3a 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
20c3b 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  (z);.    for(i=0
20c3c 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72  ; i<pParse->nVar
20c3d 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
20c3e 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 50 61    Expr *pE = pPa
20c3f 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 69  rse->apVarExpr[i
20c40 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
20c41 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   pE!=0 );.      
20c42 69 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75  if( memcmp(pE->u
20c43 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d  .zToken, z, n)==
20c44 30 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65  0 && pE->u.zToke
20c45 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  n[n]==0 ){.     
20c46 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
20c47 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3b  n = pE->iColumn;
20c48 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
20c49 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
20c4a 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d    if( i>=pParse-
20c4b 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20  >nVarExpr ){.   
20c4c 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
20c4d 6e 20 3d 20 28 79 6e 56 61 72 29 28 2b 2b 70 50  n = (ynVar)(++pP
20c4e 61 72 73 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 20  arse->nVar);.   
20c4f 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e     if( pParse->n
20c50 56 61 72 45 78 70 72 3e 3d 70 50 61 72 73 65 2d  VarExpr>=pParse-
20c51 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31  >nVarExprAlloc-1
20c52 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72   ){.        pPar
20c53 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f  se->nVarExprAllo
20c54 63 20 2b 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61  c += pParse->nVa
20c55 72 45 78 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b  rExprAlloc + 10;
20c56 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
20c57 3e 61 70 56 61 72 45 78 70 72 20 3d 0a 20 20 20  >apVarExpr =.   
20c58 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20c59 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
20c5a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  .              d
20c5b 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
20c5c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
20c5d 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
20c5e 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78    pParse->nVarEx
20c5f 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70  prAlloc*sizeof(p
20c60 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
20c61 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20  [0]).           
20c62 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
20c63 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
20c64 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
20c65 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
20c66 65 2d 3e 61 70 56 61 72 45 78 70 72 21 3d 30 20  e->apVarExpr!=0 
20c67 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  );.        pPars
20c68 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 70 50 61  e->apVarExpr[pPa
20c69 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d  rse->nVarExpr++]
20c6a 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
20c6b 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69  }.    }.  } .  i
20c6c 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 45 72 72  f( !pParse->nErr
20c6d 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 56 61 72   && pParse->nVar
20c6e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
20c6f 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
20c70 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20  E_NUMBER] ){.   
20c71 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
20c72 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
20c73 6e 79 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73  ny SQL variables
20c74 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ");.  }.}../*.**
20c75 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c   Recursively del
20c76 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ete an expressio
20c77 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54  n tree..*/.SQLIT
20c78 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20c79 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20c7a 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
20c7b 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  pr *p){.  if( p=
20c7c 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
20c7d 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  f( !ExprHasAnyPr
20c7e 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b  operty(p, EP_Tok
20c7f 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73  enOnly) ){.    s
20c80 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20c81 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a  (db, p->pLeft);.
20c82 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
20c83 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69  elete(db, p->pRi
20c84 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21 45  ght);.    if( !E
20c85 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20c86 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 26  , EP_Reduced) &&
20c87 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50   (p->flags2 & EP
20c88 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29  2_MallocedToken)
20c89 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
20c8a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20c8b 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20  ->u.zToken);.   
20c8c 20 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72 48   }.    if( ExprH
20c8d 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
20c8e 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
20c8f 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
20c90 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  ctDelete(db, p->
20c91 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
20c92 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
20c93 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
20c94 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73  te(db, p->x.pLis
20c95 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  t);.    }.  }.  
20c96 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  if( !ExprHasProp
20c97 65 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74 69  erty(p, EP_Stati
20c98 63 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  c) ){.    sqlite
20c99 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
20c9a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
20c9b 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
20c9c 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65  f bytes allocate
20c9d 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73  d for the expres
20c9e 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a  sion structure .
20c9f 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ** passed as the
20ca0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
20ca1 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20   This is always 
20ca2 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c  one of EXPR_FULL
20ca3 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45  SIZE,.** EXPR_RE
20ca4 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50  DUCEDSIZE or EXP
20ca5 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e  R_TOKENONLYSIZE.
20ca6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
20ca7 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78  xprStructSize(Ex
20ca8 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78  pr *p){.  if( Ex
20ca9 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
20caa 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29   EP_TokenOnly) )
20cab 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b   return EXPR_TOK
20cac 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66  ENONLYSIZE;.  if
20cad 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
20cae 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29  y(p, EP_Reduced)
20caf 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52   ) return EXPR_R
20cb0 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65  EDUCEDSIZE;.  re
20cb1 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49  turn EXPR_FULLSI
20cb2 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ZE;.}../*.** The
20cb3 20 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65 28   dupedExpr*Size(
20cb4 29 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68 20  ) routines each 
20cb5 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
20cb6 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
20cb7 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20  red.** to store 
20cb8 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 70  a copy of an exp
20cb9 72 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72 65  ression or expre
20cba 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68 65  ssion tree.  The
20cbb 79 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68  y differ in.** h
20cbc 6f 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20 74  ow much of the t
20cbd 72 65 65 20 69 73 20 6d 65 61 73 75 72 65 64 2e  ree is measured.
20cbe 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65 64  .**.**     duped
20cbf 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29  ExprStructSize()
20cc0 20 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c       Size of onl
20cc1 79 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63  y the Expr struc
20cc2 74 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75 70  ture .**     dup
20cc3 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 29  edExprNodeSize()
20cc4 20 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 45         Size of E
20cc5 78 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72 20  xpr + space for 
20cc6 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70  token.**     dup
20cc7 65 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 20  edExprSize()    
20cc8 20 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 6f         Expr + to
20cc9 6b 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 6f  ken + subtree co
20cca 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a  mponents.**.****
20ccb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ccd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20cce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ccf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
20cd0 65 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63  e dupedExprStruc
20cd1 74 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e  tSize() function
20cd2 20 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61 6c   returns two val
20cd3 75 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74 68  ues OR-ed togeth
20cd4 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68 65  er:  .** (1) the
20cd5 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
20cd6 66 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68  for a copy of th
20cd7 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  e Expr structure
20cd8 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32   only and .** (2
20cd9 29 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c 61  ) the EP_xxx fla
20cda 67 73 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  gs that indicate
20cdb 20 77 68 61 74 20 74 68 65 20 73 74 72 75 63 74   what the struct
20cdc 75 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64 20  ure size should 
20cdd 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  be..** The retur
20cde 6e 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77 61  n values is alwa
20cdf 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a  ys one of:.**.**
20ce0 20 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c 53        EXPR_FULLS
20ce1 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50 52  IZE.**      EXPR
20ce2 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 7c  _REDUCEDSIZE   |
20ce3 20 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20   EP_Reduced.**  
20ce4 20 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e      EXPR_TOKENON
20ce5 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65  LYSIZE | EP_Toke
20ce6 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nOnly.**.** The 
20ce7 73 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72 75  size of the stru
20ce8 63 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f 75  cture can be fou
20ce9 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68  nd by masking th
20cea 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
20ceb 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * of this routin
20cec 65 20 77 69 74 68 20 30 78 66 66 66 2e 20 20 54  e with 0xfff.  T
20ced 68 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65 20  he flags can be 
20cee 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67  found by masking
20cef 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76   the.** return v
20cf0 61 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65 64  alue with EP_Red
20cf1 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  uced|EP_TokenOnl
20cf2 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  y..**.** Note th
20cf3 61 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d 45  at with flags==E
20cf4 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 74  XPRDUP_REDUCE, t
20cf5 68 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f 72  his routines wor
20cf6 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a  ks on full-size.
20cf7 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20 45  ** (unreduced) E
20cf8 78 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20 74  xpr objects as t
20cf9 68 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c  hey or originall
20cfa 79 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  y constructed by
20cfb 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20   the parser..** 
20cfc 44 75 72 69 6e 67 20 65 78 70 72 65 73 73 69 6f  During expressio
20cfd 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74 72  n analysis, extr
20cfe 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  a information is
20cff 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f   computed and mo
20d00 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65  ved into.** late
20d01 72 20 70 61 72 74 73 20 6f 66 20 74 65 68 20 45  r parts of teh E
20d02 78 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 74  xpr object and t
20d03 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d  hat extra inform
20d04 61 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74 20  ation might get 
20d05 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69  chopped.** off i
20d06 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
20d07 20 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e 6f   is reduced.  No
20d08 74 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74 20  te also that it 
20d09 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f  does not work to
20d0a 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52 44  .** make a EXPRD
20d0b 55 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20 6f  UP_REDUCE copy o
20d0c 66 20 61 20 72 65 64 75 63 65 64 20 65 78 70 72  f a reduced expr
20d0d 65 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f  ession.  It is o
20d0e 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20  nly legal.** to 
20d0f 72 65 64 75 63 65 20 61 20 70 72 69 73 74 69 6e  reduce a pristin
20d10 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
20d11 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65  e from the parse
20d12 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  r.  The implemen
20d13 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70  tation.** of dup
20d14 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65  edExprStructSize
20d15 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69  () contain multi
20d16 70 6c 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ple assert() sta
20d17 74 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74 74  tements that att
20d18 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72  empt.** to enfor
20d19 63 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  ce this constrai
20d1a 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
20d1b 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63  t dupedExprStruc
20d1c 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69  tSize(Expr *p, i
20d1d 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
20d1e 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74   nSize;.  assert
20d1f 28 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50  ( flags==EXPRDUP
20d20 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67 73  _REDUCE || flags
20d21 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f  ==0 ); /* Only o
20d22 6e 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61 6c  ne flag value al
20d23 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20 30  lowed */.  if( 0
20d24 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50  ==(flags&EXPRDUP
20d25 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20  _REDUCE) ){.    
20d26 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c  nSize = EXPR_FUL
20d27 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  LSIZE;.  }else{.
20d28 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
20d29 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
20d2a 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  p, EP_TokenOnly|
20d2b 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
20d2c 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
20d2d 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
20d2e 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a  P_FromJoin) ); .
20d2f 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
20d30 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c  flags2 & EP2_Mal
20d31 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29  locedToken)==0 )
20d32 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
20d33 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 49  ->flags2 & EP2_I
20d34 72 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20 29  rreducible)==0 )
20d35 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65  ;.    if( p->pLe
20d36 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20  ft || p->pRight 
20d37 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70  || p->pColl || p
20d38 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20  ->x.pList ){.   
20d39 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f     nSize = EXPR_
20d3a 52 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45 50  REDUCEDSIZE | EP
20d3b 5f 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65  _Reduced;.    }e
20d3c 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65  lse{.      nSize
20d3d 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c   = EXPR_TOKENONL
20d3e 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e  YSIZE | EP_Token
20d3f 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Only;.    }.  }.
20d40 20 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a    return nSize;.
20d41 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
20d42 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
20d43 68 65 20 73 70 61 63 65 20 69 6e 20 62 79 74 65  he space in byte
20d44 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74  s required to st
20d45 6f 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a  ore the copy .**
20d46 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72   of the Expr str
20d47 75 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f 70  ucture and a cop
20d48 79 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 2e  y of the Expr.u.
20d49 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69  zToken string (i
20d4a 66 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67  f that.** string
20d4b 20 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f   is defined.).*/
20d4c 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65  .static int dupe
20d4d 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78  dExprNodeSize(Ex
20d4e 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  pr *p, int flags
20d4f 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  ){.  int nByte =
20d50 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
20d51 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 26  Size(p, flags) &
20d52 20 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21 45   0xfff;.  if( !E
20d53 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20d54 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26  , EP_IntValue) &
20d55 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b  & p->u.zToken ){
20d56 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71  .    nByte += sq
20d57 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d  lite3Strlen30(p-
20d58 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20  >u.zToken)+1;.  
20d59 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44  }.  return ROUND
20d5a 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a  8(nByte);.}../*.
20d5b 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
20d5c 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
20d5d 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65  quired to create
20d5e 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20   a duplicate of 
20d5f 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  the .** expressi
20d60 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65  on passed as the
20d61 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
20d62 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
20d63 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73  ment is a.** mas
20d64 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50  k containing EXP
20d65 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a  RDUP_XXX flags..
20d66 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
20d67 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65  returned include
20d68 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74  s space to creat
20d69 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
20d6a 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69  Expr struct.** i
20d6b 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75  tself and the bu
20d6c 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f  ffer referred to
20d6d 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65   by Expr.u.zToke
20d6e 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a  n, if any..**.**
20d6f 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f   If the EXPRDUP_
20d70 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73  REDUCE flag is s
20d71 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74  et, then the ret
20d72 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 64  urn value includ
20d73 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20  es .** space to 
20d74 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 78  duplicate all Ex
20d75 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 20  pr nodes in the 
20d76 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 45  tree formed by E
20d77 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e  xpr.pLeft .** an
20d78 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 61  d Expr.pRight va
20d79 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f 74  riables (but not
20d7a 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 75   for any structu
20d7b 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f  res pointed to o
20d7c 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 20  r .** descended 
20d7d 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 2e  from the Expr.x.
20d7e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e  pList or Expr.x.
20d7f 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c 65  pSelect variable
20d80 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  s)..*/.static in
20d81 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28  t dupedExprSize(
20d82 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
20d83 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  gs){.  int nByte
20d84 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b   = 0;.  if( p ){
20d85 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 70  .    nByte = dup
20d86 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70  edExprNodeSize(p
20d87 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66  , flags);.    if
20d88 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f  ( flags&EXPRDUP_
20d89 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 20  REDUCE ){.      
20d8a 6e 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78  nByte += dupedEx
20d8b 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c  prSize(p->pLeft,
20d8c 20 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64 45   flags) + dupedE
20d8d 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67 68  xprSize(p->pRigh
20d8e 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d  t, flags);.    }
20d8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42  .  }.  return nB
20d90 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  yte;.}../*.** Th
20d91 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73  is function is s
20d92 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65  imilar to sqlite
20d93 33 45 78 70 72 44 75 70 28 29 2c 20 65 78 63 65  3ExprDup(), exce
20d94 70 74 20 74 68 61 74 20 69 66 20 70 7a 42 75 66  pt that if pzBuf
20d95 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e  fer .** is not N
20d96 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66  ULL then *pzBuff
20d97 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  er is assumed to
20d98 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
20d99 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20  er large enough 
20d9a 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65  .** to store the
20d9b 20 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73   copy of express
20d9c 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65  ion p, the copie
20d9d 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e  s of p->u.zToken
20d9e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62  .** (if applicab
20d9f 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70  le), and the cop
20da0 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c  ies of the p->pL
20da1 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68  eft and p->pRigh
20da2 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a  t expressions,.*
20da3 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65  * if any. Before
20da4 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42   returning, *pzB
20da5 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20  uffer is set to 
20da6 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70  the first byte p
20da7 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72  assed the.** por
20da8 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66  tion of the buff
20da9 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62  er copied into b
20daa 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
20dab 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20  .*/.static Expr 
20dac 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33  *exprDup(sqlite3
20dad 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69   *db, Expr *p, i
20dae 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70  nt flags, u8 **p
20daf 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72  zBuffer){.  Expr
20db0 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20   *pNew = 0;     
20db1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20db2 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
20db3 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29  urn */.  if( p )
20db4 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  {.    const int 
20db5 69 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61  isReduced = (fla
20db6 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43  gs&EXPRDUP_REDUC
20db7 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c  E);.    u8 *zAll
20db8 6f 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61 74  oc;.    u32 stat
20db9 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20  icFlag = 0;..   
20dba 20 61 73 73 65 72 74 28 20 70 7a 42 75 66 66 65   assert( pzBuffe
20dbb 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63 65  r==0 || isReduce
20dbc 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67  d );..    /* Fig
20dbd 75 72 65 20 6f 75 74 20 77 68 65 72 65 20 74 6f  ure out where to
20dbe 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 45   write the new E
20dbf 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 2a  xpr structure. *
20dc0 2f 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66 66  /.    if( pzBuff
20dc1 65 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c  er ){.      zAll
20dc2 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a  oc = *pzBuffer;.
20dc3 20 20 20 20 20 20 73 74 61 74 69 63 46 6c 61 67        staticFlag
20dc4 20 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20 20   = EP_Static;.  
20dc5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
20dc6 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  Alloc = sqlite3D
20dc7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64  bMallocRaw(db, d
20dc8 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20  upedExprSize(p, 
20dc9 66 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20  flags));.    }. 
20dca 20 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20     pNew = (Expr 
20dcb 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69  *)zAlloc;..    i
20dcc 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
20dcd 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65   /* Set nNewSize
20dce 20 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c   to the size all
20dcf 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 73  ocated for the s
20dd0 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64  tructure pointed
20dd1 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20   to.      ** by 
20dd2 70 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65 69  pNew. This is ei
20dd3 74 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49  ther EXPR_FULLSI
20dd4 5a 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44  ZE, EXPR_REDUCED
20dd5 53 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a  SIZE or.      **
20dd6 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53   EXPR_TOKENONLYS
20dd7 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73  IZE. nToken is s
20dd8 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
20dd9 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d   of bytes consum
20dda 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74  ed.      ** by t
20ddb 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70  he copy of the p
20ddc 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e  ->u.zToken strin
20ddd 67 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20  g (if any)..    
20dde 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74    */.      const
20ddf 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75 63   unsigned nStruc
20de0 74 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70  tSize = dupedExp
20de1 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66  rStructSize(p, f
20de2 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e  lags);.      con
20de3 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20  st int nNewSize 
20de4 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26 20  = nStructSize & 
20de5 30 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e 74  0xfff;.      int
20de6 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69   nToken;.      i
20de7 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  f( !ExprHasPrope
20de8 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c  rty(p, EP_IntVal
20de9 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b  ue) && p->u.zTok
20dea 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 54  en ){.        nT
20deb 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  oken = sqlite3St
20dec 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b  rlen30(p->u.zTok
20ded 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d  en) + 1;.      }
20dee 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 54  else{.        nT
20def 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  oken = 0;.      
20df0 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65  }.      if( isRe
20df1 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20  duced ){.       
20df2 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73   assert( ExprHas
20df3 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52  Property(p, EP_R
20df4 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20  educed)==0 );.  
20df5 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c        memcpy(zAl
20df6 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65  loc, p, nNewSize
20df7 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
20df8 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a          int nSiz
20df9 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69  e = exprStructSi
20dfa 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d  ze(p);.        m
20dfb 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c  emcpy(zAlloc, p,
20dfc 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20   nSize);.       
20dfd 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b   memset(&zAlloc[
20dfe 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f  nSize], 0, EXPR_
20dff 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b  FULLSIZE-nSize);
20e00 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
20e01 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65  /* Set the EP_Re
20e02 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f  duced, EP_TokenO
20e03 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61 74  nly, and EP_Stat
20e04 69 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70 72  ic flags appropr
20e05 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20  iately. */.     
20e06 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20   pNew->flags &= 
20e07 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f  ~(EP_Reduced|EP_
20e08 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61  TokenOnly|EP_Sta
20e09 74 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  tic);.      pNew
20e0a 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75  ->flags |= nStru
20e0b 63 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65 64  ctSize & (EP_Red
20e0c 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  uced|EP_TokenOnl
20e0d 79 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  y);.      pNew->
20e0e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63 46  flags |= staticF
20e0f 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  lag;..      /* C
20e10 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f  opy the p->u.zTo
20e11 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20 61  ken string, if a
20e12 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ny. */.      if(
20e13 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20   nToken ){.     
20e14 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20     char *zToken 
20e15 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e  = pNew->u.zToken
20e16 20 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f   = (char*)&zAllo
20e17 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20  c[nNewSize];.   
20e18 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b       memcpy(zTok
20e19 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  en, p->u.zToken,
20e1a 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   nToken);.      
20e1b 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d  }..      if( 0==
20e1c 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d  ((p->flags|pNew-
20e1d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f 6b  >flags) & EP_Tok
20e1e 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20  enOnly) ){.     
20e1f 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68     /* Fill in th
20e20 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63  e pNew->x.pSelec
20e21 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69  t or pNew->x.pLi
20e22 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20  st member. */.  
20e23 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
20e24 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
20e25 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
20e26 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e          pNew->x.
20e27 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
20e28 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
20e29 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52  ->x.pSelect, isR
20e2a 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20  educed);.       
20e2b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20e2c 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20    pNew->x.pList 
20e2d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
20e2e 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c  tDup(db, p->x.pL
20e2f 69 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b  ist, isReduced);
20e30 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
20e31 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c   }..      /* Fil
20e32 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74  l in pNew->pLeft
20e33 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68   and pNew->pRigh
20e34 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  t. */.      if( 
20e35 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
20e36 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75  ty(pNew, EP_Redu
20e37 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ced|EP_TokenOnly
20e38 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c  ) ){.        zAl
20e39 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70 72  loc += dupedExpr
20e3a 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67  NodeSize(p, flag
20e3b 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  s);.        if( 
20e3c 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20e3d 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64  pNew, EP_Reduced
20e3e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
20e3f 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70  New->pLeft = exp
20e40 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66  rDup(db, p->pLef
20e41 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  t, EXPRDUP_REDUC
20e42 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20  E, &zAlloc);.   
20e43 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 69         pNew->pRi
20e44 67 68 74 20 3d 20 65 78 70 72 44 75 70 28 64 62  ght = exprDup(db
20e45 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58 50  , p->pRight, EXP
20e46 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41  RDUP_REDUCE, &zA
20e47 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 7d  lloc);.        }
20e48 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 7a 42  .        if( pzB
20e49 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 20  uffer ){.       
20e4a 20 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a     *pzBuffer = z
20e4b 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d  Alloc;.        }
20e4c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
20e4d 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
20e4e 73 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  s2 = 0;.        
20e4f 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  if( !ExprHasAnyP
20e50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f  roperty(p, EP_To
20e51 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20  kenOnly) ){.    
20e52 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66        pNew->pLef
20e53 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
20e54 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c  up(db, p->pLeft,
20e55 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70   0);.          p
20e56 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71  New->pRight = sq
20e57 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20e58 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a   p->pRight, 0);.
20e59 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
20e5a 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  }..    }.  }.  r
20e5b 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
20e5c 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
20e5d 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  ng group of rout
20e5e 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63  ines make deep c
20e5f 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73  opies of express
20e60 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73  ions,.** express
20e61 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69  ion lists, ID li
20e62 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20  sts, and select 
20e63 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65  statements.  The
20e64 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62   copies can.** b
20e65 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65  e deleted (by be
20e66 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68  ing passed to th
20e67 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e  eir respective .
20e68 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69  ..Delete() routi
20e69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20  nes).** without 
20e6a 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72  effecting the or
20e6b 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54  iginals..**.** T
20e6c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
20e6d 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72  st, ID, and sour
20e6e 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20  ce lists return 
20e6f 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  by sqlite3ExprLi
20e70 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69  stDup(),.** sqli
20e71 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20  te3IdListDup(), 
20e72 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  and sqlite3SrcLi
20e73 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20  stDup() can not 
20e74 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e  be further expan
20e75 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65  ded .** by subse
20e76 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73  quent calls to s
20e77 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64  qlite*ListAppend
20e78 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a  () routines..**.
20e79 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68  ** Any tables th
20e7a 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d  at the SrcList m
20e7b 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72  ight point to ar
20e7c 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64  e not duplicated
20e7d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67  ..**.** The flag
20e7e 73 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74  s parameter cont
20e7f 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69  ains a combinati
20e80 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55  on of the EXPRDU
20e81 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20  P_XXX flags..** 
20e82 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52  If the EXPRDUP_R
20e83 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65  EDUCE flag is se
20e84 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 75  t, then the stru
20e85 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 69  cture returned i
20e86 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64  s a.** truncated
20e87 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
20e88 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 63  usual Expr struc
20e89 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62  ture that will b
20e8a 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70  e stored as.** p
20e8b 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65  art of the in-me
20e8c 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74  mory representat
20e8d 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62  ion of the datab
20e8e 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53  ase schema..*/.S
20e8f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
20e90 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44  pr *sqlite3ExprD
20e91 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
20e92 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61  Expr *p, int fla
20e93 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  gs){.  return ex
20e94 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c 61  prDup(db, p, fla
20e95 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45  gs, 0);.}.SQLITE
20e96 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
20e97 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
20e98 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
20e99 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20  b, ExprList *p, 
20e9a 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78  int flags){.  Ex
20e9b 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  prList *pNew;.  
20e9c 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
20e9d 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f  item *pItem, *pO
20e9e 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  ldItem;.  int i;
20e9f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
20ea0 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d  turn 0;.  pNew =
20ea1 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20ea2 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
20ea3 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70  pNew) );.  if( p
20ea4 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
20ea5 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72  0;.  pNew->iECur
20ea6 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  sor = 0;.  pNew-
20ea7 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e  >nExpr = pNew->n
20ea8 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72  Alloc = p->nExpr
20ea9 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49  ;.  pNew->a = pI
20eaa 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  tem = sqlite3DbM
20eab 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d  allocRaw(db,  p-
20eac 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d  >nExpr*sizeof(p-
20ead 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  >a[0]) );.  if( 
20eae 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20  pItem==0 ){.    
20eaf 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20eb0 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74  , pNew);.    ret
20eb1 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f  urn 0;.  } .  pO
20eb2 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20  ldItem = p->a;. 
20eb3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
20eb4 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
20eb5 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b  ++, pOldItem++){
20eb6 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45  .    Expr *pOldE
20eb7 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  xpr = pOldItem->
20eb8 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d  pExpr;.    pItem
20eb9 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  ->pExpr = sqlite
20eba 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c  3ExprDup(db, pOl
20ebb 64 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20  dExpr, flags);. 
20ebc 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20     pItem->zName 
20ebd 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20ebe 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20ebf 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65  zName);.    pIte
20ec0 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74  m->zSpan = sqlit
20ec1 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
20ec2 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b  OldItem->zSpan);
20ec3 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74  .    pItem->sort
20ec4 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d  Order = pOldItem
20ec5 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  ->sortOrder;.   
20ec6 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30   pItem->done = 0
20ec7 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f  ;.    pItem->iCo
20ec8 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43  l = pOldItem->iC
20ec9 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69  ol;.    pItem->i
20eca 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d  Alias = pOldItem
20ecb 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20  ->iAlias;.  }.  
20ecc 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
20ecd 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73  /*.** If cursors
20ece 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77  , triggers, view
20ecf 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  s and subqueries
20ed0 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64   are all omitted
20ed1 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69   from.** the bui
20ed2 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  ld, then none of
20ed3 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   the following r
20ed4 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20  outines, except 
20ed5 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53  for .** sqlite3S
20ed6 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20  electDup(), can 
20ed7 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74  be called. sqlit
20ed8 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73  e3SelectDup() is
20ed9 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61   sometimes.** ca
20eda 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
20edb 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69   argument..*/.#i
20edc 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
20edd 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
20ede 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
20edf 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a  OMIT_TRIGGER) \.
20ee0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
20ee1 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
20ee2 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  Y).SQLITE_PRIVAT
20ee3 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
20ee4 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c  e3SrcListDup(sql
20ee5 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73  ite3 *db, SrcLis
20ee6 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  t *p, int flags)
20ee7 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65  {.  SrcList *pNe
20ee8 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  w;.  int i;.  in
20ee9 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70  t nByte;.  if( p
20eea 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
20eeb 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
20eec 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e  (*p) + (p->nSrc>
20eed 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b  0 ? sizeof(p->a[
20eee 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31  0]) * (p->nSrc-1
20eef 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d  ) : 0);.  pNew =
20ef0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20ef1 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 3b  Raw(db, nByte );
20ef2 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
20ef3 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
20ef4 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e  w->nSrc = pNew->
20ef5 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63  nAlloc = p->nSrc
20ef6 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
20ef7 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
20ef8 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
20ef9 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20  _item *pNewItem 
20efa 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20  = &pNew->a[i];. 
20efb 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
20efc 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d  t_item *pOldItem
20efd 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20   = &p->a[i];.   
20efe 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
20eff 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74    pNewItem->zDat
20f00 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44  abase = sqlite3D
20f01 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20f02 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  Item->zDatabase)
20f03 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
20f04 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
20f05 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20f06 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
20f07 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69    pNewItem->zAli
20f08 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  as = sqlite3DbSt
20f09 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
20f0a 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20  m->zAlias);.    
20f0b 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pNewItem->jointy
20f0c 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a  pe = pOldItem->j
20f0d 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e 65  ointype;.    pNe
20f0e 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d  wItem->iCursor =
20f0f 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73   pOldItem->iCurs
20f10 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  or;.    pNewItem
20f11 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20  ->isPopulated = 
20f12 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75  pOldItem->isPopu
20f13 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 49  lated;.    pNewI
20f14 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71  tem->zIndex = sq
20f15 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
20f16 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64  , pOldItem->zInd
20f17 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  ex);.    pNewIte
20f18 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20  m->notIndexed = 
20f19 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64  pOldItem->notInd
20f1a 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74  exed;.    pNewIt
20f1b 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c  em->pIndex = pOl
20f1c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20  dItem->pIndex;. 
20f1d 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74     pTab = pNewIt
20f1e 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49  em->pTab = pOldI
20f1f 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  tem->pTab;.    i
20f20 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
20f21 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20   pTab->nRef++;. 
20f22 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 65     }.    pNewIte
20f23 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  m->pSelect = sql
20f24 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
20f25 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c  , pOldItem->pSel
20f26 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  ect, flags);.   
20f27 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d   pNewItem->pOn =
20f28 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20f29 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f  db, pOldItem->pO
20f2a 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70  n, flags);.    p
20f2b 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20  NewItem->pUsing 
20f2c 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  = sqlite3IdListD
20f2d 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
20f2e 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e  >pUsing);.    pN
20f2f 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20  ewItem->colUsed 
20f30 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55  = pOldItem->colU
20f31 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  sed;.  }.  retur
20f32 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45  n pNew;.}.SQLITE
20f33 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20  _PRIVATE IdList 
20f34 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75  *sqlite3IdListDu
20f35 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49  p(sqlite3 *db, I
20f36 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c  dList *p){.  IdL
20f37 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74  ist *pNew;.  int
20f38 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   i;.  if( p==0 )
20f39 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
20f3a 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
20f3b 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
20f3c 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66  f(*pNew) );.  if
20f3d 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
20f3e 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49  rn 0;.  pNew->nI
20f3f 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63  d = pNew->nAlloc
20f40 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65   = p->nId;.  pNe
20f41 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62  w->a = sqlite3Db
20f42 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d  MallocRaw(db, p-
20f43 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61  >nId*sizeof(p->a
20f44 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  [0]) );.  if( pN
20f45 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20  ew->a==0 ){.    
20f46 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20f47 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74  , pNew);.    ret
20f48 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72  urn 0;.  }.  for
20f49 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20  (i=0; i<p->nId; 
20f4a 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
20f4b 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e   IdList_item *pN
20f4c 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e  ewItem = &pNew->
20f4d 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74  a[i];.    struct
20f4e 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f   IdList_item *pO
20f4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69  ldItem = &p->a[i
20f50 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  ];.    pNewItem-
20f51 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
20f52 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
20f53 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  dItem->zName);. 
20f54 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78     pNewItem->idx
20f55 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78   = pOldItem->idx
20f56 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
20f57 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  New;.}.SQLITE_PR
20f58 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
20f59 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73  lite3SelectDup(s
20f5a 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65  qlite3 *db, Sele
20f5b 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  ct *p, int flags
20f5c 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  ){.  Select *pNe
20f5d 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  w;.  if( p==0 ) 
20f5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
20f5f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
20f60 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
20f61 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  (*p) );.  if( pN
20f62 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ew==0 ) return 0
20f63 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74  ;.  pNew->pEList
20f64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
20f65 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c  stDup(db, p->pEL
20f66 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  ist, flags);.  p
20f67 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69  New->pSrc = sqli
20f68 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62  te3SrcListDup(db
20f69 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73  , p->pSrc, flags
20f6a 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72  );.  pNew->pWher
20f6b 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  e = sqlite3ExprD
20f6c 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  up(db, p->pWhere
20f6d 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
20f6e 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c  ->pGroupBy = sql
20f6f 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
20f70 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
20f71 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
20f72 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74  >pHaving = sqlit
20f73 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
20f74 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29  >pHaving, flags)
20f75 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72  ;.  pNew->pOrder
20f76 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
20f77 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
20f78 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b  OrderBy, flags);
20f79 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d  .  pNew->op = p-
20f7a 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72  >op;.  pNew->pPr
20f7b 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ior = sqlite3Sel
20f7c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50  ectDup(db, p->pP
20f7d 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20  rior, flags);.  
20f7e 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73  pNew->pLimit = s
20f7f 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
20f80 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61  , p->pLimit, fla
20f81 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66  gs);.  pNew->pOf
20f82 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78  fset = sqlite3Ex
20f83 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66  prDup(db, p->pOf
20f84 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  fset, flags);.  
20f85 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30  pNew->iLimit = 0
20f86 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65  ;.  pNew->iOffse
20f87 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73  t = 0;.  pNew->s
20f88 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c  elFlags = p->sel
20f89 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73  Flags & ~SF_Uses
20f8a 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65  Ephemeral;.  pNe
20f8b 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  w->pRightmost = 
20f8c 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  0;.  pNew->addrO
20f8d 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b  penEphm[0] = -1;
20f8e 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
20f8f 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20  nEphm[1] = -1;. 
20f90 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
20f91 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72  phm[2] = -1;.  r
20f92 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65  eturn pNew;.}.#e
20f93 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
20f94 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
20f95 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
20f96 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20  te3 *db, Select 
20f97 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
20f98 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29    assert( p==0 )
20f99 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
20f9a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
20f9b 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  dd a new element
20f9c 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61   to the end of a
20f9d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
20f9e 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a  t.  If pList is.
20f9f 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c  ** initially NUL
20fa0 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  L, then create a
20fa1 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
20fa2 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  list..**.** If a
20fa3 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
20fa4 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  on error occurs,
20fa5 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74   the entire list
20fa6 20 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a   is freed and.**
20fa7 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
20fa8 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20  d.  If non-NULL 
20fa9 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65  is returned, the
20faa 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  n it is guarante
20fab 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e  ed.** that the n
20fac 65 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 63  ew entry was suc
20fad 63 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e 64  cessfully append
20fae 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
20faf 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
20fb0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
20fb1 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 2a  ppend(.  Parse *
20fb2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
20fb3 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20fb4 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
20fb5 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20  t *pList,       
20fb6 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63   /* List to whic
20fb7 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67  h to append. Mig
20fb8 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ht be NULL */.  
20fb9 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
20fba 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
20fbb 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 65  ssion to be appe
20fbc 6e 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 4e  nded. Might be N
20fbd 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ULL */.){.  sqli
20fbe 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
20fbf 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73  ->db;.  if( pLis
20fc0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73  t==0 ){.    pLis
20fc1 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
20fc2 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
20fc3 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a  of(ExprList) );.
20fc4 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30      if( pList==0
20fc5 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
20fc6 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
20fc7 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
20fc8 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d  nAlloc==0 );.  }
20fc9 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41  .  if( pList->nA
20fca 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78  lloc<=pList->nEx
20fcb 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  pr ){.    struct
20fcc 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
20fcd 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  a;.    int n = p
20fce 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b  List->nAlloc*2 +
20fcf 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69   4;.    a = sqli
20fd0 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
20fd1 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a   pList->a, n*siz
20fd2 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29  eof(pList->a[0])
20fd3 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20  );.    if( a==0 
20fd4 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
20fd5 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
20fd6 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20  pList->a = a;.  
20fd7 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20    pList->nAlloc 
20fd8 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20fd9 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a  cSize(db, a)/siz
20fda 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20  eof(a[0]);.  }. 
20fdb 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
20fdc 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31 20  a!=0 );.  if( 1 
20fdd 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  ){.    struct Ex
20fde 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
20fdf 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70  em = &pList->a[p
20fe0 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a  List->nExpr++];.
20fe1 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d      memset(pItem
20fe2 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74  , 0, sizeof(*pIt
20fe3 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d  em));.    pItem-
20fe4 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a  >pExpr = pExpr;.
20fe5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69    }.  return pLi
20fe6 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20  st;..no_mem:    
20fe7 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61   .  /* Avoid lea
20fe8 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d  king memory if m
20fe9 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64  alloc has failed
20fea 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78  . */.  sqlite3Ex
20feb 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78  prDelete(db, pEx
20fec 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  pr);.  sqlite3Ex
20fed 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
20fee 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72   pList);.  retur
20fef 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  n 0;.}../*.** Se
20ff0 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61  t the ExprList.a
20ff1 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74  [].zName element
20ff2 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
20ff3 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d  ently added item
20ff4 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65  .** on the expre
20ff5 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  ssion list..**.*
20ff6 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65  * pList might be
20ff7 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20   NULL following 
20ff8 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42  an OOM error.  B
20ff9 75 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20  ut pName should 
20ffa 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c  never be.** NULL
20ffb 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  .  If a memory a
20ffc 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
20ffd 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d   the pParse->db-
20ffe 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c  >mallocFailed fl
20fff 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f  ag.** is set..*/
21000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21001 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
21002 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20 50  ListSetName(.  P
21003 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
21004 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
21005 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
21006 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
21007 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74         /* List t
21008 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74  o which to add t
21009 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f  he span. */.  To
2100a 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20  ken *pName,     
2100b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f        /* Name to
2100c 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69   be added */.  i
2100d 6e 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20  nt dequote      
2100e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
2100f 6f 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d 65  o cause the name
21010 20 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64 20   to be dequoted 
21011 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  */.){.  assert( 
21012 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61 72  pList!=0 || pPar
21013 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
21014 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28  iled!=0 );.  if(
21015 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74   pList ){.    st
21016 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
21017 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61  em *pItem;.    a
21018 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45  ssert( pList->nE
21019 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49 74  xpr>0 );.    pIt
2101a 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70  em = &pList->a[p
2101b 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a  List->nExpr-1];.
2101c 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65      assert( pIte
2101d 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20  m->zName==0 );. 
2101e 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20     pItem->zName 
2101f 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
21020 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  up(pParse->db, p
21021 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e  Name->z, pName->
21022 6e 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71 75  n);.    if( dequ
21023 6f 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e  ote && pItem->zN
21024 61 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65 71  ame ) sqlite3Deq
21025 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d  uote(pItem->zNam
21026 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
21027 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73   Set the ExprLis
21028 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d  t.a[].zSpan elem
21029 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ent of the most 
2102a 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69  recently added i
2102b 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78  tem.** on the ex
2102c 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
2102d 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74  *.** pList might
2102e 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69   be NULL followi
2102f 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e  ng an OOM error.
21030 20 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f 75    But pSpan shou
21031 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e  ld never be.** N
21032 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ULL.  If a memor
21033 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
21034 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e  ls, the pParse->
21035 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21036 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e   flag.** is set.
21037 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21038 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
21039 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 0a  xprListSetSpan(.
2103a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2103b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2103c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2103d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2103e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  t,        /* Lis
2103f 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64  t to which to ad
21040 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20  d the span. */. 
21041 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e   ExprSpan *pSpan
21042 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
21043 73 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65 64  span to be added
21044 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
21045 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
21046 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  b;.  assert( pLi
21047 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c  st!=0 || db->mal
21048 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a  locFailed!=0 );.
21049 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
2104a 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2104b 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2104c 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74   &pList->a[pList
2104d 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20  ->nExpr-1];.    
2104e 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
2104f 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 73  Expr>0 );.    as
21050 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
21051 46 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d  Failed || pItem-
21052 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70  >pExpr==pSpan->p
21053 45 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c 69  Expr );.    sqli
21054 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
21055 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20  tem->zSpan);.   
21056 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20   pItem->zSpan = 
21057 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
21058 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 61  (db, (char*)pSpa
21059 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 20  n->zStart,.     
2105a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2105b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2105c 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64  int)(pSpan->zEnd
2105d 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74   - pSpan->zStart
2105e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
2105f 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
21060 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63  on list pEList c
21061 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61  ontains more tha
21062 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74  n iLimit element
21063 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65  s,.** leave an e
21064 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20  rror message in 
21065 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
21066 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21067 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68  qlite3ExprListCh
21068 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72  eckLength(.  Par
21069 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78  se *pParse,.  Ex
2106a 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a  prList *pEList,.
2106b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f    const char *zO
2106c 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d  bject.){.  int m
2106d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  x = pParse->db->
2106e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2106f 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74  MIT_COLUMN];.  t
21070 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20  estcase( pEList 
21071 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  && pEList->nExpr
21072 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61  ==mx );.  testca
21073 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45  se( pEList && pE
21074 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b  List->nExpr==mx+
21075 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  1 );.  if( pELis
21076 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78  t && pEList->nEx
21077 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c  pr>mx ){.    sql
21078 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
21079 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63  rse, "too many c
2107a 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a  olumns in %s", z
2107b 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a  Object);.  }.}..
2107c 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
2107d 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f  entire expressio
2107e 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  n list..*/.SQLIT
2107f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21080 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
21081 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
21082 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
21083 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  t){.  int i;.  s
21084 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
21085 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66  tem *pItem;.  if
21086 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
21087 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
21088 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70  List->a!=0 || (p
21089 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26  List->nExpr==0 &
2108a 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d  & pList->nAlloc=
2108b 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  =0) );.  assert(
2108c 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70   pList->nExpr<=p
2108d 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a  List->nAlloc );.
2108e 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73    for(pItem=pLis
2108f 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69  t->a, i=0; i<pLi
21090 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  st->nExpr; i++, 
21091 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71  pItem++){.    sq
21092 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
21093 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  db, pItem->pExpr
21094 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
21095 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
21096 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69  zName);.    sqli
21097 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
21098 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d  tem->zSpan);.  }
21099 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2109a 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a  (db, pList->a);.
2109b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2109c 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f  db, pList);.}../
2109d 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2109e 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 63  nes are Walker c
2109f 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65  allbacks.  Walke
210a0 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 6e  r.u.pi is a poin
210a1 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74  ter.** to an int
210a2 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f 75  eger.  These rou
210a3 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b 69  tines are checki
210a4 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ng an expression
210a5 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 74   to see.** if it
210a6 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20   is a constant. 
210a7 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70   Set *Walker.u.p
210a8 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 78  i to 0 if the ex
210a9 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e  pression is.** n
210aa 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a  ot constant..**.
210ab 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 63  ** These callbac
210ac 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  k routines are u
210ad 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
210ae 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
210af 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  **.**     sqlite
210b0 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
210b1 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
210b2 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
210b3 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73  tJoin().**     s
210b4 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
210b5 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29  tantOrFunction()
210b6 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  .**.*/.static in
210b7 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73  t exprNodeIsCons
210b8 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61  tant(Walker *pWa
210b9 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
210ba 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61  r){..  /* If pWa
210bb 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 74  lker->u.i is 3 t
210bc 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 20  hen any term of 
210bd 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
210be 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20  hat comes from. 
210bf 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53   ** the ON or US
210c0 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61  ING clauses of a
210c1 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69   join disqualifi
210c2 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  es the expressio
210c3 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e  n.  ** from bein
210c4 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e  g considered con
210c5 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20  stant. */.  if( 
210c6 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20  pWalker->u.i==3 
210c7 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  && ExprHasAnyPro
210c8 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
210c9 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20  FromJoin) ){.   
210ca 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20   pWalker->u.i = 
210cb 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  0;.    return WR
210cc 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20  C_Abort;.  }..  
210cd 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
210ce 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73  p ){.    /* Cons
210cf 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74  ider functions t
210d0 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 66  o be constant if
210d1 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 6d   all their argum
210d2 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 6e  ents are constan
210d3 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 61  t.    ** and pWa
210d4 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a  lker->u.i==2 */.
210d5 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
210d6 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20  TION:.      if( 
210d7 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20  pWalker->u.i==2 
210d8 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
210d9 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
210da 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  h */.    case TK
210db 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _ID:.    case TK
210dc 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73  _COLUMN:.    cas
210dd 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  e TK_AGG_FUNCTIO
210de 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  N:.    case TK_A
210df 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20  GG_COLUMN:.     
210e0 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
210e1 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20  ->op==TK_ID );. 
210e2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
210e3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
210e4 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73  UMN );.      tes
210e5 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
210e6 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  ==TK_AGG_FUNCTIO
210e7 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  N );.      testc
210e8 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
210e9 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b  TK_AGG_COLUMN );
210ea 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e  .      pWalker->
210eb 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  u.i = 0;.      r
210ec 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
210ed 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
210ee 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
210ef 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45  xpr->op==TK_SELE
210f0 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e  CT ); /* selectN
210f1 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69  odeIsConstant wi
210f2 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20  ll disallow */. 
210f3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
210f4 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49  Expr->op==TK_EXI
210f5 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74  STS ); /* select
210f6 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77  NodeIsConstant w
210f7 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a  ill disallow */.
210f8 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
210f9 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d  _Continue;.  }.}
210fa 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65  .static int sele
210fb 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  ctNodeIsConstant
210fc 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
210fd 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65  , Select *NotUse
210fe 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
210ff 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
21100 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20  .  pWalker->u.i 
21101 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52  = 0;.  return WR
21102 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69  C_Abort;.}.stati
21103 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73  c int exprIsCons
21104 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69  t(Expr *p, int i
21105 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b  nitFlag){.  Walk
21106 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20  er w;.  w.u.i = 
21107 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45  initFlag;.  w.xE
21108 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78  xprCallback = ex
21109 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  prNodeIsConstant
2110a 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c  ;.  w.xSelectCal
2110b 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f  lback = selectNo
2110c 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20  deIsConstant;.  
2110d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
2110e 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e  &w, p);.  return
2110f 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   w.u.i;.}../*.**
21110 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
21111 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72  ion tree.  Retur
21112 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65  n 1 if the expre
21113 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e  ssion is constan
21114 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74  t.** and 0 if it
21115 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62   involves variab
21116 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20  les or function 
21117 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  calls..**.** For
21118 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
21119 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
2111a 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20  a double-quoted 
2111b 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63  string (ex: "abc
2111c 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  ").** is conside
2111d 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62  red a variable b
2111e 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74  ut a single-quot
2111f 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27  ed string (ex: '
21120 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f  abc') is.** a co
21121 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
21122 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
21123 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
21124 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ant(Expr *p){.  
21125 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e  return exprIsCon
21126 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a  st(p, 1);.}../*.
21127 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65  ** Walk an expre
21128 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74  ssion tree.  Ret
21129 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70  urn 1 if the exp
2112a 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74  ression is const
2112b 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73  ant.** that does
2112c 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72   no originate fr
2112d 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  om the ON or USI
2112e 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20  NG clauses of a 
2112f 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  join..** Return 
21130 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73  0 if it involves
21131 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75   variables or fu
21132 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20  nction calls or 
21133 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e  terms from.** an
21134 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
21135 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  use..*/.SQLITE_P
21136 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21137 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
21138 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29  NotJoin(Expr *p)
21139 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49  {.  return exprI
2113a 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a  sConst(p, 3);.}.
2113b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
2113c 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
2113d 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
2113e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63   expression is c
2113f 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20  onstant.** or a 
21140 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69  function call wi
21141 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75  th constant argu
21142 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61  ments.  Return a
21143 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a  nd 0 if there.**
21144 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c   are any variabl
21145 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  es..**.** For th
21146 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
21147 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64  is function, a d
21148 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
21149 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a  ing (ex: "abc").
2114a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
2114b 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20   a variable but 
2114c 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20  a single-quoted 
2114d 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63  string (ex: 'abc
2114e 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74  ') is.** a const
2114f 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ant..*/.SQLITE_P
21150 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21151 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
21152 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20  OrFunction(Expr 
21153 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  *p){.  return ex
21154 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b  prIsConst(p, 2);
21155 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
21156 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f   expression p co
21157 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69  des a constant i
21158 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73  nteger that is s
21159 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74  mall enough.** t
2115a 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69  o fit in a 32-bi
2115b 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72  t integer, retur
2115c 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20  n 1 and put the 
2115d 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
2115e 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c  eger.** in *pVal
2115f 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72  ue.  If the expr
21160 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e  ession is not an
21161 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69   integer or if i
21162 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20  t is too big.** 
21163 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e  to fit in a sign
21164 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65  ed 32-bit intege
21165 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20  r, return 0 and 
21166 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e  leave *pValue un
21167 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
21168 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21169 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
2116a 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74  ger(Expr *p, int
2116b 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74   *pValue){.  int
2116c 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70   rc = 0;.  if( p
2116d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74  ->flags & EP_Int
2116e 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56  Value ){.    *pV
2116f 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c  alue = p->u.iVal
21170 75 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ue;.    return 1
21171 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20  ;.  }.  switch( 
21172 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  p->op ){.    cas
21173 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
21174 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
21175 65 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e  e3GetInt32(p->u.
21176 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b  zToken, pValue);
21177 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
21178 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  c==0 );.      br
21179 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2117a 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a  ase TK_UPLUS: {.
2117b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2117c 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
2117d 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65  p->pLeft, pValue
2117e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2117f 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21180 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20  K_UMINUS: {.    
21181 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69    int v;.      i
21182 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
21183 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74  Integer(p->pLeft
21184 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20  , &v) ){.       
21185 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20   *pValue = -v;. 
21186 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20         rc = 1;. 
21187 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
21188 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
21189 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20  fault: break;.  
2118a 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  }.  if( rc ){.  
2118b 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61    assert( ExprHa
2118c 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20  sAnyProperty(p, 
2118d 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f  EP_Reduced|EP_To
2118e 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20  kenOnly).       
2118f 20 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66          || (p->f
21190 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c  lags2 & EP2_Mall
21191 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b  ocedToken)==0 );
21192 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f  .    p->op = TK_
21193 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e  INTEGER;.    p->
21194 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56  flags |= EP_IntV
21195 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69  alue;.    p->u.i
21196 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b  Value = *pValue;
21197 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
21198 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
21199 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 72 65  n FALSE if there
2119a 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68   is no chance th
2119b 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  at the expressio
2119c 6e 20 63 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a 2a  n can be NULL..*
2119d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
2119e 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 62 65 20  ession might be 
2119f 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 65  NULL or if the e
211a0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 6f 6f  xpression is too
211a1 20 63 6f 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20 74   complex.** to t
211a2 65 6c 6c 20 72 65 74 75 72 6e 20 54 52 55 45 2e  ell return TRUE.
211a3 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f    .**.** This ro
211a4 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 61 73  utine is used as
211a5 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
211a6 2c 20 74 6f 20 73 6b 69 70 20 4f 50 5f 49 73 4e  , to skip OP_IsN
211a7 75 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 77  ull opcodes.** w
211a8 68 65 6e 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  hen we know that
211a9 20 61 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20   a value cannot 
211aa 62 65 20 4e 55 4c 4c 2e 20 20 48 65 6e 63 65 2c  be NULL.  Hence,
211ab 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76   a false positiv
211ac 65 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20  e.** (returning 
211ad 54 52 55 45 20 77 68 65 6e 20 69 6e 20 66 61 63  TRUE when in fac
211ae 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
211af 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 4e 55   can never be NU
211b0 4c 4c 29 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20  LL) might.** be 
211b1 61 20 73 6d 61 6c 6c 20 70 65 72 66 6f 72 6d 61  a small performa
211b2 6e 63 65 20 68 69 74 20 62 75 74 20 69 73 20 6f  nce hit but is o
211b3 74 68 65 72 77 69 73 65 20 68 61 72 6d 6c 65 73  therwise harmles
211b4 73 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  s.  On the other
211b5 0a 2a 2a 20 68 61 6e 64 2c 20 61 20 66 61 6c 73  .** hand, a fals
211b6 65 20 6e 65 67 61 74 69 76 65 20 28 72 65 74 75  e negative (retu
211b7 72 6e 69 6e 67 20 46 41 4c 53 45 20 77 68 65 6e  rning FALSE when
211b8 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 75 6c   the result coul
211b9 64 20 62 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77 69  d be NULL).** wi
211ba 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73 75 6c 74  ll likely result
211bb 20 69 6e 20 61 6e 20 69 6e 63 6f 72 72 65 63 74   in an incorrect
211bc 20 61 6e 73 77 65 72 2e 20 20 53 6f 20 77 68 65   answer.  So whe
211bd 6e 20 69 6e 20 64 6f 75 62 74 2c 20 72 65 74 75  n in doubt, retu
211be 72 6e 0a 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a 53  rn.** TRUE..*/.S
211bf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
211c0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 6e  t sqlite3ExprCan
211c1 42 65 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70  BeNull(const Exp
211c2 72 20 2a 70 29 7b 0a 20 20 75 38 20 6f 70 3b 0a  r *p){.  u8 op;.
211c3 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d    while( p->op==
211c4 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f  TK_UPLUS || p->o
211c5 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20  p==TK_UMINUS ){ 
211c6 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a  p = p->pLeft; }.
211c7 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20    op = p->op;.  
211c8 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  if( op==TK_REGIS
211c9 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70  TER ) op = p->op
211ca 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  2;.  switch( op 
211cb 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
211cc 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65  NTEGER:.    case
211cd 20 54 4b 5f 53 54 52 49 4e 47 3a 0a 20 20 20 20   TK_STRING:.    
211ce 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20  case TK_FLOAT:. 
211cf 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
211d0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
211d1 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
211d2 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
211d3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
211d4 61 74 65 20 61 6e 20 4f 50 5f 49 73 4e 75 6c 6c  ate an OP_IsNull
211d5 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
211d6 74 20 74 65 73 74 73 20 72 65 67 69 73 74 65 72  t tests register
211d7 20 69 52 65 67 20 61 6e 64 20 6a 75 6d 70 73 0a   iReg and jumps.
211d8 2a 2a 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69  ** to location i
211d9 44 65 73 74 20 69 66 20 74 68 65 20 76 61 6c 75  Dest if the valu
211da 65 20 69 6e 20 69 52 65 67 20 69 73 20 4e 55 4c  e in iReg is NUL
211db 4c 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 6e  L.  The value in
211dc 20 69 52 65 67 20 0a 2a 2a 20 77 61 73 20 63 6f   iReg .** was co
211dd 6d 70 75 74 65 64 20 62 79 20 70 45 78 70 72 2e  mputed by pExpr.
211de 20 20 49 66 20 77 65 20 63 61 6e 20 6c 6f 6f 6b    If we can look
211df 20 61 74 20 70 45 78 70 72 20 61 74 20 63 6f 6d   at pExpr at com
211e0 70 69 6c 65 2d 74 69 6d 65 20 61 6e 64 0a 2a 2a  pile-time and.**
211e1 20 64 65 74 65 72 6d 69 6e 65 20 74 68 61 74 20   determine that 
211e2 69 74 20 63 61 6e 20 6e 65 76 65 72 20 67 65 6e  it can never gen
211e3 65 72 61 74 65 20 61 20 4e 55 4c 4c 2c 20 74 68  erate a NULL, th
211e4 65 6e 20 74 68 65 20 4f 50 5f 49 73 4e 75 6c 6c  en the OP_IsNull
211e5 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 63 61   operation.** ca
211e6 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f  n be omitted..*/
211e7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
211e8 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
211e9 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 0a  CodeIsNullJump(.
211ea 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20    Vdbe *v,      
211eb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
211ec 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  E under construc
211ed 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
211ee 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 2f 2a  Expr *pExpr,  /*
211ef 20 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 20 4f   Only generate O
211f0 50 5f 49 73 4e 75 6c 6c 20 69 66 20 74 68 69 73  P_IsNull if this
211f1 20 65 78 70 72 20 63 61 6e 20 62 65 20 4e 55 4c   expr can be NUL
211f2 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c  L */.  int iReg,
211f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
211f4 73 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  st the value in 
211f5 74 68 69 73 20 72 65 67 69 73 74 65 72 20 66 6f  this register fo
211f6 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  r NULL */.  int 
211f7 69 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20  iDest           
211f8 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
211f9 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6c  the value is nul
211fa 6c 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71  l */.){.  if( sq
211fb 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e 75  lite3ExprCanBeNu
211fc 6c 6c 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  ll(pExpr) ){.   
211fd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
211fe 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
211ff 20 69 52 65 67 2c 20 69 44 65 73 74 29 3b 0a 20   iReg, iDest);. 
21200 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
21201 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67  rn TRUE if the g
21202 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
21203 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 77 68  is a constant wh
21204 69 63 68 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20  ich would be.** 
21205 75 6e 63 68 61 6e 67 65 64 20 62 79 20 4f 50 5f  unchanged by OP_
21206 41 66 66 69 6e 69 74 79 20 77 69 74 68 20 74 68  Affinity with th
21207 65 20 61 66 66 69 6e 69 74 79 20 67 69 76 65 6e  e affinity given
21208 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a   in the second.*
21209 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  * argument..**.*
2120a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2120b 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
2120c 69 6e 65 20 69 66 20 74 68 65 20 4f 50 5f 41 66  ine if the OP_Af
2120d 66 69 6e 69 74 79 20 6f 70 65 72 61 74 69 6f 6e  finity operation
2120e 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 6d 69 74 74  .** can be omitt
2120f 65 64 2e 20 20 57 68 65 6e 20 69 6e 20 64 6f 75  ed.  When in dou
21210 62 74 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e  bt return FALSE.
21211 20 20 41 20 66 61 6c 73 65 20 6e 65 67 61 74 69    A false negati
21212 76 65 0a 2a 2a 20 69 73 20 68 61 72 6d 6c 65 73  ve.** is harmles
21213 73 2e 20 20 41 20 66 61 6c 73 65 20 70 6f 73 69  s.  A false posi
21214 74 69 76 65 2c 20 68 6f 77 65 76 65 72 2c 20 63  tive, however, c
21215 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65  an result in the
21216 20 77 72 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65 72   wrong.** answer
21217 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21218 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
21219 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e 69  xprNeedsNoAffini
2121a 74 79 43 68 61 6e 67 65 28 63 6f 6e 73 74 20 45  tyChange(const E
2121b 78 70 72 20 2a 70 2c 20 63 68 61 72 20 61 66 66  xpr *p, char aff
2121c 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 69 66  ){.  u8 op;.  if
2121d 28 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  ( aff==SQLITE_AF
2121e 46 5f 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  F_NONE ) return 
2121f 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f  1;.  while( p->o
21220 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70  p==TK_UPLUS || p
21221 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20  ->op==TK_UMINUS 
21222 29 7b 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b  ){ p = p->pLeft;
21223 20 7d 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b   }.  op = p->op;
21224 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45  .  if( op==TK_RE
21225 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70 2d  GISTER ) op = p-
21226 3e 6f 70 32 3b 0a 20 20 73 77 69 74 63 68 28 20  >op2;.  switch( 
21227 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
21228 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  K_INTEGER: {.   
21229 20 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d 53     return aff==S
2122a 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
2122b 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45  R || aff==SQLITE
2122c 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20  _AFF_NUMERIC;.  
2122d 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2122e 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72  FLOAT: {.      r
2122f 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54  eturn aff==SQLIT
21230 45 5f 41 46 46 5f 52 45 41 4c 20 7c 7c 20 61 66  E_AFF_REAL || af
21231 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  f==SQLITE_AFF_NU
21232 4d 45 52 49 43 3b 0a 20 20 20 20 7d 0a 20 20 20  MERIC;.    }.   
21233 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a   case TK_STRING:
21234 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
21235 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
21236 54 45 58 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  TEXT;.    }.    
21237 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a  case TK_BLOB: {.
21238 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
21239 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
2123a 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
2123b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 54 61    assert( p->iTa
2123c 62 6c 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 70 20  ble>=0 );  /* p 
2123d 63 61 6e 6e 6f 74 20 62 65 20 70 61 72 74 20 6f  cannot be part o
2123e 66 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  f a CHECK constr
2123f 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 72 65  aint */.      re
21240 74 75 72 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3c  turn p->iColumn<
21241 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28  0.          && (
21242 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
21243 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d  INTEGER || aff==
21244 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
21245 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  IC);.    }.    d
21246 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
21247 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
21248 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
21249 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
2124a 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 20  given string is 
2124b 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20  a row-id column 
2124c 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  name..*/.SQLITE_
2124d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2124e 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74  te3IsRowid(const
2124f 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28   char *z){.  if(
21250 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
21251 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30  z, "_ROWID_")==0
21252 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
21253 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
21254 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30  p(z, "ROWID")==0
21255 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
21256 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
21257 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29  p(z, "OID")==0 )
21258 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
21259 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
2125a 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 77  Return true if w
2125b 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74 68  e are able to th
2125c 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70  e IN operator op
2125d 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a  timization on a.
2125e 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65 20  ** query of the 
2125f 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  form.**.**      
21260 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e   x IN (SELECT ..
21261 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74  .).**.** Where t
21262 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61  he SELECT... cla
21263 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69 66  use is as specif
21264 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61 6d  ied by the param
21265 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20  eter to this.** 
21266 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  routine..**.** T
21267 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63 74  he Select object
21268 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20 61   passed in has a
21269 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 70  lready been prep
2126a 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a  rocessed and no.
2126b 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20 62  ** errors have b
2126c 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69  een found..*/.#i
2126d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2126e 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74 69  T_SUBQUERY.stati
2126f 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61 74  c int isCandidat
21270 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63 74  eForInOpt(Select
21271 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20   *p){.  SrcList 
21272 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69 73  *pSrc;.  ExprLis
21273 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61 62  t *pEList;.  Tab
21274 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28 20  le *pTab;.  if( 
21275 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
21276 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21277 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64     /* right-hand
21278 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20 53   side of IN is S
21279 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70  ELECT */.  if( p
2127a 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75 72  ->pPrior ) retur
2127b 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
2127c 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f    /* Not a compo
2127d 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  und SELECT */.  
2127e 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  if( p->selFlags 
2127f 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53  & (SF_Distinct|S
21280 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b 0a  F_Aggregate) ){.
21281 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70      testcase( (p
21282 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
21283 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
21284 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73  regate))==SF_Dis
21285 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65 73  tinct );.    tes
21286 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c  tcase( (p->selFl
21287 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
21288 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
21289 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65 20  )==SF_Aggregate 
2128a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
2128b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54 20   /* No DISTINCT 
2128c 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61  keyword and no a
2128d 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2128e 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  ns */.  }.  asse
2128f 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d  rt( p->pGroupBy=
21290 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20  =0 );           
21291 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f     /* Has no GRO
21292 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  UP BY clause */.
21293 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20    if( p->pLimit 
21294 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
21295 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20           /* Has 
21296 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  no LIMIT clause 
21297 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
21298 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20  pOffset==0 );   
21299 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2129a 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f  o LIMIT means no
2129b 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66 28   OFFSET */.  if(
2129c 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74   p->pWhere ) ret
2129d 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2129e 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48      /* Has no WH
2129f 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
212a0 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
212a1 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 3d    assert( pSrc!=
212a2 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 2d  0 );.  if( pSrc-
212a3 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 72  >nSrc!=1 ) retur
212a4 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  n 0;          /*
212a5 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20   Single term in 
212a6 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
212a7 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e   if( pSrc->a[0].
212a8 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e  pSelect ) return
212a9 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20   0;     /* FROM 
212aa 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 72  is not a subquer
212ab 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 70  y or view */.  p
212ac 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d  Tab = pSrc->a[0]
212ad 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45 56  .pTab;.  if( NEV
212ae 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65  ER(pTab==0) ) re
212af 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 74  turn 0;.  assert
212b0 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d  ( pTab->pSelect=
212b1 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20  =0 );           
212b2 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
212b3 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f  is not a view */
212b4 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
212b5 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20  (pTab) ) return 
212b6 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f  0;        /* FRO
212b7 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 76  M clause not a v
212b8 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
212b9 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
212ba 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69  List;.  if( pELi
212bb 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72  st->nExpr!=1 ) r
212bc 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 2f  eturn 0;       /
212bd 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20  * One column in 
212be 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
212bf 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  /.  if( pEList->
212c0 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d  a[0].pExpr->op!=
212c1 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75  TK_COLUMN ) retu
212c2 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20  rn 0; /* Result 
212c3 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  is a column */. 
212c4 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
212c5 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
212c6 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
212c7 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
212c8 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74  ion is used by t
212c9 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
212ca 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e  n of the IN (...
212cb 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49  ) operator..** I
212cc 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 69  t's job is to fi
212cd 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 62  nd or create a b
212ce 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
212cf 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64  that may be used
212d0 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 65  .** either to te
212d1 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69  st for membershi
212d2 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 73  p of the (...) s
212d3 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 65  et or to iterate
212d4 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20   through.** its 
212d5 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e  members, skippin
212d6 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a  g duplicates..**
212d7 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66  .** The index of
212d8 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65 6e   the cursor open
212d9 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65  ed on the b-tree
212da 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c 65   (database table
212db 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  , database index
212dc 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c   .** or ephermal
212dd 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65   table) is store
212de 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20  d in pX->iTable 
212df 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63  before this func
212e0 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  tion returns..**
212e1 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61   The returned va
212e2 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
212e3 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20 74  tion indicates t
212e4 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c 20  he b-tree type, 
212e5 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
212e6 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57  *   IN_INDEX_ROW
212e7 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20  ID - The cursor 
212e8 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20  was opened on a 
212e9 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a  database table..
212ea 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  **   IN_INDEX_IN
212eb 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f 72  DEX - The cursor
212ec 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61   was opened on a
212ed 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 2e   database index.
212ee 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45  .**   IN_INDEX_E
212ef 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 6f  PH -   The curso
212f0 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
212f1 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 61  a specially crea
212f2 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ted and.**      
212f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f                po
212f4 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d 61  pulated epherema
212f5 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  l table..**.** A
212f6 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72 65  n existing b-tre
212f7 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  e may only be us
212f8 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54  ed if the SELECT
212f9 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c   is of the simpl
212fa 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a  e.** form:.**.**
212fb 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c       SELECT <col
212fc 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65  umn> FROM <table
212fd 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  >.**.** If the p
212fe 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65  rNotFound parame
212ff 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74  ter is 0, then t
21300 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 62  he b-tree will b
21301 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74  e used to iterat
21302 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  e.** through the
21303 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b   set members, sk
21304 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69  ipping any dupli
21305 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63  cates. In this c
21306 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65  ase an.** ephere
21307 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62  mal table must b
21308 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68  e used unless th
21309 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75  e selected <colu
2130a 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65  mn> is guarantee
2130b 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75  d.** to be uniqu
2130c 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 75  e - either becau
2130d 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45  se it is an INTE
2130e 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
2130f 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20 55  or it.** has a U
21310 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
21311 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65 78   or UNIQUE index
21312 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
21313 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65  rNotFound parame
21314 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  ter is not 0, th
21315 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77 69  en the b-tree wi
21316 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66  ll be used .** f
21317 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62  or fast set memb
21318 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e  ership tests. In
21319 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70   this case an ep
2131a 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75  heremal table mu
2131b 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75  st .** be used u
2131c 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69  nless <column> i
2131d 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
2131e 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69  MARY KEY or an i
2131f 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20  ndex can .** be 
21320 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75  found with <colu
21321 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d  mn> as its left-
21322 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  most column..**.
21323 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74 72  ** When the b-tr
21324 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65 64  ee is being used
21325 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20   for membership 
21326 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c 69  tests, the calli
21327 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e  ng function.** n
21328 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  eeds to know whe
21329 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2132a 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2132b 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a  ns an SQL NULL .
2132c 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65  ** value in orde
2132d 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65  r to correctly e
2132e 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69  valuate expressi
2132f 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28  ons like "X IN (
21330 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68  Y, Z)"..** If th
21331 65 72 65 20 69 73 20 61 6e 79 20 63 68 61 6e 63  ere is any chanc
21332 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29  e that the (...)
21333 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61   might contain a
21334 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a   NULL value at.*
21335 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20  * runtime, then 
21336 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c  a register is al
21337 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20  located and the 
21338 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
21339 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70  written.** to *p
2133a 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68  rNotFound. If th
2133b 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65  ere is no chance
2133c 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 20   that the (...) 
2133d 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55  contains a.** NU
2133e 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a  LL value, then *
2133f 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65  prNotFound is le
21340 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  ft unchanged..**
21341 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 65  .** If a registe
21342 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  r is allocated a
21343 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20  nd its location 
21344 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74  stored in *prNot
21345 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69  Found, then.** i
21346 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  ts initial value
21347 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68   is NULL.  If th
21348 65 20 28 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f 74  e (...) does not
21349 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74   remain constant
2134a 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 61  .** for the dura
2134b 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 72  tion of the quer
2134c 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c 45  y (i.e. the SELE
2134d 43 54 20 77 69 74 68 69 6e 20 74 68 65 20 28 2e  CT within the (.
2134e 2e 2e 29 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72  ..).** is a corr
2134f 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 29  elated subquery)
21350 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
21351 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  of the allocated
21352 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20   register is.** 
21353 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61  reset to NULL ea
21354 63 68 20 74 69 6d 65 20 74 68 65 20 73 75 62 71  ch time the subq
21355 75 65 72 79 20 69 73 20 72 65 72 75 6e 2e 20 54  uery is rerun. T
21356 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a  his allows the.*
21357 2a 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20  * caller to use 
21358 76 64 62 65 20 63 6f 64 65 20 65 71 75 69 76 61  vdbe code equiva
21359 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c  lent to the foll
2135a 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  owing:.**.**   i
2135b 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c  f( register==NUL
2135c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f  L ){.**     has_
2135d 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20  null = <test if 
2135e 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63  data structure c
2135f 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a  ontains null>.**
21360 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d 20       register = 
21361 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69  1.**   }.**.** i
21362 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64  n order to avoid
21363 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65   running the <te
21364 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63  st if data struc
21365 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75  ture contains nu
21366 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65  ll>.** test more
21367 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e   often than is n
21368 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66  ecessary..*/.#if
21369 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2136a 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45  _SUBQUERY.SQLITE
2136b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2136c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
2136d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2136e 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72  xpr *pX, int *pr
2136f 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c  NotFound){.  Sel
21370 65 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20  ect *p;         
21371 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21372 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20     /* SELECT to 
21373 74 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20  the right of IN 
21374 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e  operator */.  in
21375 74 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20  t eType = 0;    
21376 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21377 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52      /* Type of R
21378 48 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44  HS table. IN_IND
21379 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54  EX_* */.  int iT
2137a 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ab = pParse->nTa
2137b 62 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20  b++;            
2137c 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65  /* Cursor of the
2137d 20 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20   RHS table */.  
2137e 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65  int mustBeUnique
2137f 20 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d   = (prNotFound==
21380 30 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  0);   /* True if
21381 20 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69   RHS must be uni
21382 71 75 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  que */..  assert
21383 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20  ( pX->op==TK_IN 
21384 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  );..  /* Check t
21385 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78 69 73  o see if an exis
21386 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 6e  ting table or in
21387 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20  dex can be used 
21388 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 79 20  to.  ** satisfy 
21389 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73  the query.  This
2138a 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 20 74   is preferable t
2138b 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 6e  o generating a n
2138c 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72  ew .  ** ephemer
2138d 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  al table..  */. 
2138e 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f   p = (ExprHasPro
2138f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73  perty(pX, EP_xIs
21390 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e  Select) ? pX->x.
21391 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20  pSelect : 0);.  
21392 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 72 73  if( ALWAYS(pPars
21393 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 69  e->nErr==0) && i
21394 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f  sCandidateForInO
21395 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 6c  pt(p) ){.    sql
21396 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
21397 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20  e->db;          
21398 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
21399 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2139a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2139b 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  p->pEList->a[0].
2139c 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78 70 72  pExpr;   /* Expr
2139d 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e 20  ession <column> 
2139e 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  */.    int iCol 
2139f 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
213a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
213a1 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
213a2 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20   <column> */.   
213a3 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
213a4 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
213a5 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75  );      /* Virtu
213a6 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67  al machine being
213a7 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 54 61   coded */.    Ta
213a8 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70  ble *pTab = p->p
213a9 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 20  Src->a[0].pTab; 
213aa 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 3c 74       /* Table <t
213ab 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 6e  able>. */.    in
213ac 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
213ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213ae 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
213af 20 69 64 78 20 66 6f 72 20 70 54 61 62 20 2a 2f   idx for pTab */
213b0 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f 64 65  .   .    /* Code
213b1 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f   an OP_VerifyCoo
213b2 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c 65  kie and OP_Table
213b3 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65 3e  Lock for <table>
213b4 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  . */.    iDb = s
213b5 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
213b6 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
213b7 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69  chema);.    sqli
213b8 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
213b9 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
213ba 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61 62  ;.    sqlite3Tab
213bb 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
213bc 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
213bd 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
213be 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75  ..    /* This fu
213bf 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63  nction is only c
213c0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20 70  alled from two p
213c1 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20 63  laces. In both c
213c2 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20 20  ases the vdbe.  
213c3 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79    ** has already
213c4 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e   been allocated.
213c5 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69 74   So assume sqlit
213c6 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20 61  e3GetVdbe() is a
213c7 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75 63  lways.    ** suc
213c8 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20 20  cessful here..  
213c9 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
213ca 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  v);.    if( iCol
213cb 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  <0 ){.      int 
213cc 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iMem = ++pParse-
213cd 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74  >nMem;.      int
213ce 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 20 69   iAddr;..      i
213cf 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Addr = sqlite3Vd
213d0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
213d1 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20  f, iMem);.      
213d2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
213d3 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
213d4 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20   1, iMem);..    
213d5 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
213d6 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c  le(pParse, iTab,
213d7 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
213d8 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20  penRead);.      
213d9 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58  eType = IN_INDEX
213da 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73  _ROWID;..      s
213db 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
213dc 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20  re(v, iAddr);.  
213dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49    }else{.      I
213de 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
213df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213e0 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
213e1 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20  variable */..   
213e2 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74     /* The collat
213e3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65  ion sequence use
213e4 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69  d by the compari
213e5 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78  son. If an index
213e6 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20   is to.      ** 
213e7 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  be used in place
213e8 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65   of a temp-table
213e9 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64  , it must be ord
213ea 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20  ered according. 
213eb 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20       ** to this 
213ec 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
213ed 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f  ce.  */.      Co
213ee 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71  llSeq *pReq = sq
213ef 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
213f0 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  reCollSeq(pParse
213f1 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78  , pX->pLeft, pEx
213f2 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  pr);..      /* C
213f3 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66  heck that the af
213f4 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c  finity that will
213f5 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66   be used to perf
213f6 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a  orm the .      *
213f7 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  * comparison is 
213f8 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
213f9 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
213fa 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20  column. If.     
213fb 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69   ** it is not, i
213fc 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
213fd 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64  e to use any ind
213fe 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ex..      */.   
213ff 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f     char aff = co
21400 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
21401 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  (pX);.      int 
21402 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70  affinity_ok = (p
21403 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
21404 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61  affinity==aff||a
21405 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
21406 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72  ONE);..      for
21407 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
21408 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 70  ex; pIdx && eTyp
21409 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 79  e==0 && affinity
2140a 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  _ok; pIdx=pIdx->
2140b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
2140c 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c  if( (pIdx->aiCol
2140d 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20  umn[0]==iCol).  
2140e 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
2140f 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
21410 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e   ENC(db), pIdx->
21411 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d 70  azColl[0], 0)==p
21412 52 65 71 0a 20 20 20 20 20 20 20 20 20 26 26 20  Req.         && 
21413 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c  (!mustBeUnique |
21414 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  | (pIdx->nColumn
21415 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45  ==1 && pIdx->onE
21416 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a  rror!=OE_None)).
21417 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
21418 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20       int iMem = 
21419 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2141a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41            int iA
2141b 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63  ddr;.          c
2141c 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20  har *pKey;.  .  
2141d 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28          pKey = (
2141e 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e  char *)sqlite3In
2141f 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
21420 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20  e, pIdx);.      
21421 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69      iAddr = sqli
21422 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
21423 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20   OP_If, iMem);. 
21424 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21425 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21426 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65  _Integer, 1, iMe
21427 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20  m);.  .         
21428 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21429 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p4(v, OP_OpenRea
2142a 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74  d, iTab, pIdx->t
2142b 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
2142c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2142d 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34           pKey,P4
2142e 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
2142f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62  );.          Vdb
21430 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
21431 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  ", pIdx->zName))
21432 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70  ;.          eTyp
21433 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44  e = IN_INDEX_IND
21434 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73  EX;..          s
21435 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21436 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20  re(v, iAddr);.  
21437 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f          if( prNo
21438 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d  tFound && !pTab-
21439 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e  >aCol[iCol].notN
2143a 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ull ){.         
2143b 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d     *prNotFound =
2143c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2143d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2143e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2143f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
21440 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20  eType==0 ){.    
21441 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 6f 75  /* Could not fou
21442 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  nd an existing t
21443 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 6f  able or index to
21444 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53 20   use as the RHS 
21445 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 57  b-tree..    ** W
21446 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 67  e will have to g
21447 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65 6d  enerate an ephem
21448 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64 6f  eral table to do
21449 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a 2f   the job..    */
2144a 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76  .    int rMayHav
2144b 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 65  eNull = 0;.    e
2144c 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f  Type = IN_INDEX_
2144d 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72 4e  EPH;.    if( prN
2144e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20  otFound ){.     
2144f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 72   *prNotFound = r
21450 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b 2b  MayHaveNull = ++
21451 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21452 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d 3e    }else if( pX->
21453 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 30  pLeft->iColumn<0
21454 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50   && !ExprHasAnyP
21455 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78  roperty(pX, EP_x
21456 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
21457 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e     eType = IN_IN
21458 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d  DEX_ROWID;.    }
21459 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
2145a 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65  Subselect(pParse
2145b 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e 75  , pX, rMayHaveNu
2145c 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e  ll, eType==IN_IN
2145d 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65  DEX_ROWID);.  }e
2145e 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61  lse{.    pX->iTa
2145f 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a  ble = iTab;.  }.
21460 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b 0a    return eType;.
21461 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
21462 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
21463 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 72  r scalar subquer
21464 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 65  ies used as an e
21465 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64  xpression.** and
21466 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 20   IN operators.  
21467 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
21468 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 52      (SELECT a FR
21469 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 2d  OM b)          -
2146a 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20  - subquery.**   
2146b 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54    EXISTS (SELECT
2146c 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20   a FROM b)   -- 
2146d 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79 0a  EXISTS subquery.
2146e 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35  **     x IN (4,5
2146f 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20  ,11)            
21470 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72    -- IN operator
21471 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69   with list on ri
21472 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a  ght-hand side.**
21473 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43       x IN (SELEC
21474 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20  T a FROM b)     
21475 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77  -- IN operator w
21476 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20  ith subquery on 
21477 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20  the right.**.** 
21478 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d 65  The pExpr parame
21479 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68  ter describes th
2147a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
2147b 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49  t contains the I
2147c 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72  N.** operator or
2147d 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a   subquery..**.**
2147e 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 73   If parameter is
2147f 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72  Rowid is non-zer
21480 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 69  o, then expressi
21481 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 72  on pExpr is guar
21482 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20  anteed.** to be 
21483 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f  of the form "<ro
21484 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f  wid> IN (?, ?, ?
21485 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 64  )", where <rowid
21486 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65  > is a reference
21487 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65  .** to some inte
21488 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f  ger key column o
21489 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 65  f a table B-Tree
2148a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20  . In this case, 
2148b 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79  use an.** intkey
2148c 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 65   B-Tree to store
2148d 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 2e   the set of IN(.
2148e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 65  ..) values inste
2148f 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c 0a  ad of the usual.
21490 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 69  ** (slower) vari
21491 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 73  able length keys
21492 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49   B-Tree..**.** I
21493 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69  f rMayHaveNull i
21494 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 74  s non-zero, that
21495 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
21496 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
21497 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45 4c  IN.** (not a SEL
21498 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20 61  ECT or EXISTS) a
21499 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53 20  nd that the RHS 
2149a 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20 4e  might contains N
2149b 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65 72  ULLs..** Further
2149c 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73 20  more, the IN is 
2149d 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  in a WHERE claus
2149e 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72 65  e and that we re
2149f 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f 20  ally want.** to 
214a0 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68 65  iterate over the
214a1 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f   RHS of the IN o
214a2 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65 72  perator in order
214a3 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63 61   to quickly loca
214a4 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 73  te.** all corres
214a5 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65 6d  ponding LHS elem
214a6 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73 20  ents.  All this 
214a7 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73 20  routine does is 
214a8 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68  initialize.** th
214a9 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65 6e  e register given
214aa 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   by rMayHaveNull
214ab 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c 69   to NULL.  Calli
214ac 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c  ng routines will
214ad 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f 66   take.** care of
214ae 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 72   changing this r
214af 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74 6f  egister value to
214b0 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 65   non-NULL if the
214b1 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72 65   RHS is NULL-fre
214b2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79  e..**.** If rMay
214b3 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72 6f  HaveNull is zero
214b4 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
214b5 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69  t the subquery i
214b6 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a 20  s being used.** 
214b7 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74  for membership t
214b8 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68  esting only.  Th
214b9 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
214ba 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79  o initialize any
214bb 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 6f  .** registers to
214bc 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70 72   indicate the pr
214bd 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e 63  esense or absenc
214be 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 68  e of NULLs on th
214bf 65 20 52 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  e RHS..**.** For
214c0 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 49   a SELECT or EXI
214c1 53 54 53 20 6f 70 65 72 61 74 6f 72 2c 20 72 65  STS operator, re
214c2 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65  turn the registe
214c3 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  r that holds the
214c4 0a 2a 2a 20 72 65 73 75 6c 74 2e 20 20 46 6f 72  .** result.  For
214c5 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 6f 72   IN operators or
214c6 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
214c7 75 72 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20  urs, the return 
214c8 76 61 6c 75 65 20 69 73 20 30 2e 0a 2a 2f 0a 23  value is 0..*/.#
214c9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
214ca 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49  IT_SUBQUERY.SQLI
214cb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
214cc 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
214cd 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ect(.  Parse *pP
214ce 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
214cf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
214d0 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
214d1 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pr,            /
214d2 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c 45 43 54  * The IN, SELECT
214d3 2c 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72  , or EXISTS oper
214d4 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d  ator */.  int rM
214d5 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20  ayHaveNull,     
214d6 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68    /* Register th
214d7 61 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 68  at records wheth
214d8 65 72 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 69  er NULLs exist i
214d9 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69  n RHS */.  int i
214da 73 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  sRowid          
214db 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c     /* If true, L
214dc 48 53 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f  HS of IN operato
214dd 72 20 69 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a  r is a rowid */.
214de 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64  ){.  int testAdd
214df 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
214e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
214e1 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64  One-time test ad
214e2 64 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 72  dress */.  int r
214e3 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Reg = 0;        
214e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214e5 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 73     /* Register s
214e6 74 6f 72 69 6e 67 20 72 65 73 75 6c 74 69 6e 67  toring resulting
214e7 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
214e8 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
214e9 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 4e 45  Parse);.  if( NE
214ea 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75  VER(v==0) ) retu
214eb 72 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45  rn 0;.  sqlite3E
214ec 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
214ed 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  rse);..  /* This
214ee 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75   code must be ru
214ef 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74  n in its entiret
214f0 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20  y every time it 
214f1 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20  is encountered. 
214f2 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68   ** if any of th
214f3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74  e following is t
214f4 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  rue:.  **.  **  
214f5 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68    *  The right-h
214f6 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f  and side is a co
214f7 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72  rrelated subquer
214f8 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65  y.  **    *  The
214f9 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
214fa 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
214fb 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e  n list containin
214fc 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a  g variables.  **
214fd 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e      *  We are in
214fe 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20  side a trigger. 
214ff 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20   **.  ** If all 
21500 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65  of the above are
21501 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20   false, then we 
21502 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64  can run this cod
21503 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a  e just once.  **
21504 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74   save the result
21505 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65  s, and reuse the
21506 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20   same result on 
21507 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63  subsequent invoc
21508 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ations..  */.  i
21509 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  f( !ExprHasAnyPr
2150a 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2150b 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21  _VarSelect) && !
2150c 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2150d 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d  Tab ){.    int m
2150e 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
2150f 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
21510 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
21511 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74  _If, mem);.    t
21512 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 65  estAddr = sqlite
21513 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21514 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65  P_Integer, 1, me
21515 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
21516 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 50  testAddr>0 || pP
21517 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
21518 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20  Failed );.  }.. 
21519 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
2151a 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2151b 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68  K_IN: {.      ch
2151c 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  ar affinity;.   
2151d 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e     KeyInfo keyIn
2151e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64  fo;.      int ad
2151f 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 64  dr;        /* Ad
21520 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e  dress of OP_Open
21521 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75  Ephemeral instru
21522 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45  ction */.      E
21523 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78  xpr *pLeft = pEx
21524 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20  pr->pLeft;..    
21525 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75    if( rMayHaveNu
21526 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ll ){.        sq
21527 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21528 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
21529 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
2152a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 66      }..      aff
2152b 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
2152c 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66  xprAffinity(pLef
2152d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68  t);..      /* Wh
2152e 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 6e  ether this is an
2152f 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e   'x IN(SELECT...
21530 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c  )' or an 'x IN(<
21531 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 20  exprlist>)'.    
21532 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
21533 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 68  it is handled th
21534 65 20 73 61 6d 65 20 77 61 79 2e 20 20 41 6e 20  e same way.  An 
21535 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
21536 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c  is .      ** fil
21537 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 2d  led with single-
21538 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 73  field index keys
21539 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
2153a 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20  e results.      
2153b 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45  ** from the SELE
2153c 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 6c  CT or the <exprl
2153d 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  ist>..      **. 
2153e 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 27       ** If the '
2153f 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  x' expression is
21540 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c   a column value,
21541 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e   or the SELECT..
21542 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65  ..      ** state
21543 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 63  ment returns a c
21544 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65  olumn value, the
21545 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  n the affinity o
21546 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20  f that.      ** 
21547 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 74  column is used t
21548 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 65  o build the inde
21549 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 20  x keys. If both 
2154a 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 20  'x' and the.    
2154b 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73    ** SELECT... s
2154c 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c  tatement are col
2154d 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72  umns, then numer
2154e 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75  ic affinity is u
2154f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 20  sed.      ** if 
21550 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61  either column ha
21551 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e 54  s NUMERIC or INT
21552 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 49  EGER affinity. I
21553 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 20  f neither.      
21554 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 53  ** 'x' nor the S
21555 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65  ELECT... stateme
21556 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20  nt are columns, 
21557 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66  then numeric aff
21558 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 69  inity.      ** i
21559 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f  s used..      */
2155a 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
2155b 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  able = pParse->n
2155c 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64  Tab++;.      add
2155d 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2155e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
2155f 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72  Ephemeral, pExpr
21560 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f 77  ->iTable, !isRow
21561 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  id);.      memse
21562 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73  t(&keyInfo, 0, s
21563 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b  izeof(keyInfo));
21564 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e  .      keyInfo.n
21565 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20  Field = 1;..    
21566 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
21567 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21568 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
21569 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a        /* Case 1:
2156a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 45       expr IN (SE
2156b 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20  LECT ...).      
2156c 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2156d 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2156e 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   write the resul
2156f 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ts of the select
21570 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f 72   into the tempor
21571 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ary.        ** t
21572 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61  able allocated a
21573 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e  nd opened above.
21574 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
21575 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64      SelectDest d
21576 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 70  est;.        Exp
21577 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a  rList *pEList;..
21578 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
21579 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 20  !isRowid );.    
2157a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2157b 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
2157c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 2d   SRT_Set, pExpr-
2157d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20  >iTable);.      
2157e 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 20    dest.affinity 
2157f 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b 0a  = (u8)affinity;.
21580 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
21581 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 30  (pExpr->iTable&0
21582 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 70  x0000FFFF)==pExp
21583 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20  r->iTable );.   
21584 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
21585 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
21586 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c  Expr->x.pSelect,
21587 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20   &dest) ){.     
21588 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
21589 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2158a 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   pEList = pExpr-
2158b 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  >x.pSelect->pELi
2158c 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  st;.        if( 
2158d 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d 30  ALWAYS(pEList!=0
2158e 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
2158f 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 20  r>0) ){ .       
21590 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c     keyInfo.aColl
21591 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  [0] = sqlite3Bin
21592 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
21593 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  q(pParse, pExpr-
21594 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 20  >pLeft,.        
21595 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b        pEList->a[
21596 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  0].pExpr);.     
21597 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
21598 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c   if( pExpr->x.pL
21599 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist!=0 ){.      
2159a 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20    /* Case 2:    
2159b 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69   expr IN (exprli
2159c 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  st).        **. 
2159d 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61         ** For ea
2159e 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62  ch expression, b
2159f 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65  uild an index ke
215a0 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75  y from the evalu
215a1 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20  ation and.      
215a2 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e    ** store it in
215a3 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
215a4 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20  able. If <expr> 
215a5 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  is a column, the
215a6 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  n use.        **
215a7 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66   that columns af
215a8 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c  finity when buil
215a9 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e  ding index keys.
215aa 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f   If <expr> is no
215ab 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63  t.        ** a c
215ac 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72  olumn, use numer
215ad 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20  ic affinity..   
215ae 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
215af 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45  int i;.        E
215b0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
215b1 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
215b2 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
215b3 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
215b4 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e  Item;.        in
215b5 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20  t r1, r2, r3;.. 
215b6 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69         if( !affi
215b7 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20  nity ){.        
215b8 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c    affinity = SQL
215b9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
215ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
215bb 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d  keyInfo.aColl[0]
215bc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
215bd 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
215be 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  xpr->pLeft);..  
215bf 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
215c0 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65  rough each expre
215c1 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69  ssion in <exprli
215c2 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  st>. */.        
215c3 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
215c4 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
215c5 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c          r2 = sql
215c6 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
215c7 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
215c8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
215c9 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
215ca 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f   r2);.        fo
215cb 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r(i=pList->nExpr
215cc 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
215cd 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
215ce 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
215cf 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65  Expr *pE2 = pIte
215d0 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
215d1 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49 6e      int iValToIn
215d2 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  s;..          /*
215d3 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
215d4 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61  on is not consta
215d5 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  nt then we will 
215d6 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20  need to.        
215d7 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65    ** disable the
215d8 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20 67   test that was g
215d9 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 74  enerated above t
215da 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20  hat makes sure. 
215db 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73           ** this
215dc 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75   code only execu
215dd 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75  tes once.  Becau
215de 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e  se for a non-con
215df 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20  stant.          
215e0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65  ** expression we
215e1 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74   need to rerun t
215e2 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 69  his code each ti
215e3 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  me..          */
215e4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 74  .          if( t
215e5 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c 69  estAddr && !sqli
215e6 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
215e7 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20  t(pE2) ){.      
215e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
215e9 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
215ea 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b   testAddr-1, 2);
215eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73  .            tes
215ec 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20  tAddr = 0;.     
215ed 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
215ee 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68    /* Evaluate th
215ef 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  e expression and
215f0 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20   insert it into 
215f1 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  the temp table *
215f2 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
215f3 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69 74  isRowid && sqlit
215f4 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
215f5 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73 29  pE2, &iValToIns)
215f6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
215f7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
215f8 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49 6e  3(v, OP_InsertIn
215f9 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t, pExpr->iTable
215fa 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73 29  , r2, iValToIns)
215fb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
215fc 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  e{.            r
215fd 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  3 = sqlite3ExprC
215fe 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
215ff 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20  , pE2, r1);.    
21600 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52 6f          if( isRo
21601 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  wid ){.         
21602 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21603 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73  AddOp2(v, OP_Mus
21604 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20 20  tBeInt, r3,.    
21605 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21606 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
21607 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
21608 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20  dr(v)+2);.      
21609 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2160a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2160b 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69  Insert, pExpr->i
2160c 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a  Table, r2, r3);.
2160d 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
2160e 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
2160f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21610 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p4(v, OP_MakeRec
21611 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20  ord, r3, 1, r2, 
21612 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20  &affinity, 1);. 
21613 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
21614 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
21615 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
21616 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 20  se, r3, 1);.    
21617 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21618 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21619 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78  P_IdxInsert, pEx
2161a 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b  pr->iTable, r2);
2161b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2161c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2161d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
2161e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2161f 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
21620 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
21621 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21622 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20  arse, r2);.     
21623 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 73   }.      if( !is
21624 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
21625 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
21626 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 76  geP4(v, addr, (v
21627 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20  oid *)&keyInfo, 
21628 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20  P4_KEYINFO);.   
21629 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
2162a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73  ;.    }..    cas
2162b 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20  e TK_EXISTS:.   
2162c 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a   case TK_SELECT:
2162d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2162e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
2162f 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 61   has to be a sca
21630 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 6e  lar SELECT.  Gen
21631 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 75  erate code to pu
21632 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 76  t the.      ** v
21633 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 6c  alue of this sel
21634 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20  ect in a memory 
21635 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 20  cell and record 
21636 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 20  the number.     
21637 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72   ** of the memor
21638 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d  y cell in iColum
21639 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 61  n.  If this is a
2163a 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65 0a  n EXISTS, write.
2163b 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65        ** an inte
2163c 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73 74  ger 0 (not exist
2163d 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73 29  s) or 1 (exists)
2163e 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20 63   into a memory c
2163f 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  ell.      ** and
21640 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65 6d   record that mem
21641 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c  ory cell in iCol
21642 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  umn..      */.  
21643 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
21644 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 22   Token one = { "
21645 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f 6b  1", 1 };  /* Tok
21646 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20 76  en for literal v
21647 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20 20  alue 1 */.      
21648 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20  Select *pSel;   
21649 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2164a 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20        /* SELECT 
2164b 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e 63  statement to enc
2164c 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c  ode */.      Sel
2164d 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20  ectDest dest;   
2164e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2164f 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65 61     /* How to dea
21650 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72 65  l with SELECt re
21651 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20 74  sult */..      t
21652 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
21653 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b  op==TK_EXISTS );
21654 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21655 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53   pExpr->op==TK_S
21656 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 61  ELECT );.      a
21657 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
21658 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20 70  ==TK_EXISTS || p
21659 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c  Expr->op==TK_SEL
2165a 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61 73  ECT );..      as
2165b 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 6f  sert( ExprHasPro
2165c 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2165d 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
2165e 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72      pSel = pExpr
2165f 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20  ->x.pSelect;.   
21660 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
21661 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
21662 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65  0, ++pParse->nMe
21663 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45  m);.      if( pE
21664 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45  xpr->op==TK_SELE
21665 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65  CT ){.        de
21666 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 4d  st.eDest = SRT_M
21667 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  em;.        sqli
21668 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21669 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73   OP_Null, 0, des
2166a 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  t.iParm);.      
2166b 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2166c 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72 79  , "Init subquery
2166d 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20   result"));.    
2166e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2166f 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52   dest.eDest = SR
21670 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20  T_Exists;.      
21671 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21672 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
21673 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d  r, 0, dest.iParm
21674 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  );.        VdbeC
21675 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74  omment((v, "Init
21676 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22 29   EXISTS result")
21677 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21678 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
21679 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
2167a 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20  Sel->pLimit);.  
2167b 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74      pSel->pLimit
2167c 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2167d 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47  pParse, TK_INTEG
2167e 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b  ER, 0, 0, &one);
2167f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
21680 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
21681 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29 7b   pSel, &dest) ){
21682 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
21683 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
21684 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50 61   rReg = dest.iPa
21685 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65  rm;.      ExprSe
21686 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
21687 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  pr);.      break
21688 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
21689 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20  f( testAddr ){. 
2168a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2168b 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64  mpHere(v, testAd
2168c 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  dr-1);.  }.  sql
2168d 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70  ite3ExprCachePop
2168e 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20  (pParse, 1);..  
2168f 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a 23  return rReg;.}.#
21690 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
21691 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
21692 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
21693 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f  _OMIT_SUBQUERY./
21694 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
21695 64 65 20 66 6f 72 20 61 6e 20 49 4e 20 65 78 70  de for an IN exp
21696 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20  ression..**.**  
21697 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54      x IN (SELECT
21698 20 2e 2e 2e 29 0a 2a 2a 20 20 20 20 20 20 78 20   ...).**      x 
21699 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c 75 65  IN (value, value
2169a 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  , ...).**.** The
2169b 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
2169c 28 4c 48 53 29 20 69 73 20 61 20 73 63 61 6c 61  (LHS) is a scala
2169d 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54  r expression.  T
2169e 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
2169f 64 65 20 28 52 48 53 29 0a 2a 2a 20 69 73 20 61  de (RHS).** is a
216a0 6e 20 61 72 72 61 79 20 6f 66 20 7a 65 72 6f 20  n array of zero 
216a1 6f 72 20 6d 6f 72 65 20 76 61 6c 75 65 73 2e 20  or more values. 
216a2 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
216a3 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 4c  is true if the L
216a4 48 53 20 69 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e  HS is.** contain
216a5 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48  ed within the RH
216a6 53 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66  S.  The value of
216a7 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
216a8 69 73 20 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c 4c  is unknown (NULL
216a9 29 0a 2a 2a 20 69 66 20 74 68 65 20 4c 48 53 20  ).** if the LHS 
216aa 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68  is NULL or if th
216ab 65 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e  e LHS is not con
216ac 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68  tained within th
216ad 65 20 52 48 53 20 61 6e 64 20 74 68 65 0a 2a 2a  e RHS and the.**
216ae 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 6f 6e   RHS contains on
216af 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76  e or more NULL v
216b0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  alues..**.** Thi
216b1 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  s routine genera
216b2 74 65 73 20 63 6f 64 65 20 77 69 6c 6c 20 6a 75  tes code will ju
216b3 6d 70 20 74 6f 20 64 65 73 74 49 66 46 61 6c 73  mp to destIfFals
216b4 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20  e if the LHS is 
216b5 6e 6f 74 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65  not .** containe
216b6 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 53  d within the RHS
216b7 2e 20 20 49 66 20 64 75 65 20 74 6f 20 4e 55 4c  .  If due to NUL
216b8 4c 73 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 74  Ls we cannot det
216b9 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4c 48  ermine if the LH
216ba 53 0a 2a 2a 20 69 73 20 63 6f 6e 74 61 69 6e 65  S.** is containe
216bb 64 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 65  d in the RHS the
216bc 6e 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 49 66  n jump to destIf
216bd 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 20 4c 48  Null.  If the LH
216be 53 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a  S is contained.*
216bf 2a 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 53  * within the RHS
216c0 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75   then fall throu
216c1 67 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  gh..*/.static vo
216c2 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
216c3 64 65 49 4e 28 0a 20 20 50 61 72 73 65 20 2a 70  deIN(.  Parse *p
216c4 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
216c5 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
216c6 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
216c7 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
216c8 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
216c9 2f 2a 20 54 68 65 20 49 4e 20 65 78 70 72 65 73  /* The IN expres
216ca 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65  sion */.  int de
216cb 73 74 49 66 46 61 6c 73 65 2c 20 20 20 20 20 20  stIfFalse,      
216cc 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
216cd 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61  LHS is not conta
216ce 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53 20  ined in the RHS 
216cf 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49 66 4e  */.  int destIfN
216d0 75 6c 6c 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  ull        /* Ju
216d1 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20 72  mp here if the r
216d2 65 73 75 6c 74 73 20 61 72 65 20 75 6e 6b 6e 6f  esults are unkno
216d3 77 6e 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20  wn due to NULLs 
216d4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 52 68 73  */.){.  int rRhs
216d5 48 61 73 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f 2a  HasNull = 0;  /*
216d6 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20 69   Register that i
216d7 73 20 74 72 75 65 20 69 66 20 52 48 53 20 63 6f  s true if RHS co
216d8 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76 61 6c 75  ntains NULL valu
216d9 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66  es */.  char aff
216da 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 2f 2a  inity;        /*
216db 20 43 6f 6d 70 61 72 69 73 6f 6e 20 61 66 66 69   Comparison affi
216dc 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20  nity to use */. 
216dd 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20 20   int eType;     
216de 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
216df 66 20 74 68 65 20 52 48 53 20 2a 2f 0a 20 20 69  f the RHS */.  i
216e0 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 20  nt r1;          
216e1 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
216e2 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a  y use register *
216e3 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
216e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
216e5 74 65 6d 65 6e 74 20 75 6e 64 65 72 20 63 6f 6e  tement under con
216e6 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  struction */..  
216e7 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 52  /* Compute the R
216e8 48 53 2e 20 20 20 41 66 74 65 72 20 74 68 69 73  HS.   After this
216e9 20 73 74 65 70 2c 20 74 68 65 20 74 61 62 6c 65   step, the table
216ea 20 77 69 74 68 20 63 75 72 73 6f 72 0a 20 20 2a   with cursor.  *
216eb 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  * pExpr->iTable 
216ec 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68  will contains th
216ed 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61  e values that ma
216ee 6b 65 20 75 70 20 74 68 65 20 52 48 53 2e 0a 20  ke up the RHS.. 
216ef 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 65   */.  v = pParse
216f0 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72  ->pVdbe;.  asser
216f1 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 20  t( v!=0 );      
216f2 20 2f 2a 20 4f 4f 4d 20 64 65 74 65 63 74 65 64   /* OOM detected
216f3 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 72   prior to this r
216f4 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 56 64 62 65  outine */.  Vdbe
216f5 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
216f6 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 22 29  "begin IN expr")
216f7 29 3b 0a 20 20 65 54 79 70 65 20 3d 20 73 71 6c  );.  eType = sql
216f8 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
216f9 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
216fa 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a 20  rRhsHasNull);.. 
216fb 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
216fc 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75  he affinity to u
216fd 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b  se to create a k
216fe 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75  ey from the resu
216ff 6c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  lts.  ** of the 
21700 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69  expression. affi
21701 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 61  nityStr stores a
21702 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 73   static string s
21703 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 2a 2a  uitable for.  **
21704 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65   P4 of OP_MakeRe
21705 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 61 66 66  cord..  */.  aff
21706 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73  inity = comparis
21707 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  onAffinity(pExpr
21708 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68  );..  /* Code th
21709 65 20 4c 48 53 2c 20 74 68 65 20 3c 65 78 70 72  e LHS, the <expr
2170a 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49  > from "<expr> I
2170b 4e 20 28 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a 20  N (...)"..  */. 
2170c 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2170d 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20  ePush(pParse);. 
2170e 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2170f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
21710 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  .  sqlite3ExprCo
21711 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
21712 2d 3e 70 4c 65 66 74 2c 20 72 31 29 3b 0a 20 20  ->pLeft, r1);.  
21713 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21714 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
21715 72 31 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b  r1, destIfNull);
21716 0a 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d  ...  if( eType==
21717 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29  IN_INDEX_ROWID )
21718 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73  {.    /* In this
21719 20 63 61 73 65 2c 20 74 68 65 20 52 48 53 20 69   case, the RHS i
2171a 73 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74  s the ROWID of t
2171b 61 62 6c 65 20 62 2d 74 72 65 65 0a 20 20 20 20  able b-tree.    
2171c 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
2171d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d  beAddOp2(v, OP_M
2171e 75 73 74 42 65 49 6e 74 2c 20 72 31 2c 20 64 65  ustBeInt, r1, de
2171f 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20  stIfFalse);.    
21720 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21721 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
21722 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  s, pExpr->iTable
21723 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 72  , destIfFalse, r
21724 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
21725 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
21726 2c 20 74 68 65 20 52 48 53 20 69 73 20 61 6e 20  , the RHS is an 
21727 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 0a 20 20  index b-tree..  
21728 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
21729 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2172a 5f 41 66 66 69 6e 69 74 79 2c 20 72 31 2c 20 31  _Affinity, r1, 1
2172b 2c 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c 20  , 0, &affinity, 
2172c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  1);..    /* If t
2172d 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69  he set membershi
2172e 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 68  p test fails, th
2172f 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
21730 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 78 20   the .    ** "x 
21731 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73  IN (...)" expres
21732 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74  sion must be eit
21733 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49  her 0 or NULL. I
21734 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 2a 2a  f the set.    **
21735 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c   contains no NUL
21736 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 74  L values, then t
21737 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e 20  he result is 0. 
21738 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20 20  If the set .    
21739 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  ** contains one 
2173a 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c  or more NULL val
2173b 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ues, then the re
2173c 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20  sult of the.    
2173d 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  ** expression is
2173e 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20   also NULL..    
2173f 2a 2f 0a 20 20 20 20 69 66 28 20 72 52 68 73 48  */.    if( rRhsH
21740 61 73 4e 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65 73  asNull==0 || des
21741 74 49 66 46 61 6c 73 65 3d 3d 64 65 73 74 49 66  tIfFalse==destIf
21742 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Null ){.      /*
21743 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e   This branch run
21744 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e  s if it is known
21745 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
21746 20 74 68 61 74 20 74 68 65 20 52 48 53 0a 20 20   that the RHS.  
21747 20 20 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f      ** cannot co
21748 6e 74 61 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 65  ntain NULL value
21749 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  s. This happens 
2174a 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20  as the result.  
2174b 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54      ** of a "NOT
2174c 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e   NULL" constrain
2174d 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
2174e 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 20 20  e schema..      
2174f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f  **.      ** Also
21750 20 72 75 6e 20 74 68 69 73 20 62 72 61 6e 63 68   run this branch
21751 20 69 66 20 4e 55 4c 4c 20 69 73 20 65 71 75 69   if NULL is equi
21752 76 61 6c 65 6e 74 20 74 6f 20 46 41 4c 53 45 0a  valent to FALSE.
21753 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 69        ** for thi
21754 73 20 70 61 72 74 69 63 75 6c 61 72 20 49 4e 20  s particular IN 
21755 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 20 20  operator..      
21756 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
21757 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c  VdbeAddOp4Int(v,
21758 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70 45   OP_NotFound, pE
21759 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64 65 73  xpr->iTable, des
2175a 74 49 66 46 61 6c 73 65 2c 20 72 31 2c 20 31 29  tIfFalse, r1, 1)
2175b 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ;..    }else{.  
2175c 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 62      /* In this b
2175d 72 61 6e 63 68 2c 20 74 68 65 20 52 48 53 20 6f  ranch, the RHS o
2175e 66 20 74 68 65 20 49 4e 20 6d 69 67 68 74 20 63  f the IN might c
2175f 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 61 6e  ontain a NULL an
21760 64 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70  d.      ** the p
21761 72 65 73 65 6e 63 65 20 6f 66 20 61 20 4e 55 4c  resence of a NUL
21762 4c 20 6f 6e 20 74 68 65 20 52 48 53 20 6d 61 6b  L on the RHS mak
21763 65 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 20  es a difference 
21764 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  in the.      ** 
21765 6f 75 74 63 6f 6d 65 2e 0a 20 20 20 20 20 20 2a  outcome..      *
21766 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 2c 20  /.      int j1, 
21767 6a 32 2c 20 6a 33 3b 0a 0a 20 20 20 20 20 20 2f  j2, j3;..      /
21768 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f  * First check to
21769 20 73 65 65 20 69 66 20 74 68 65 20 4c 48 53 20   see if the LHS 
2176a 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  is contained in 
2176b 74 68 65 20 52 48 53 2e 20 20 49 66 20 73 6f 2c  the RHS.  If so,
2176c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  .      ** then t
2176d 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 4e  he presence of N
2176e 55 4c 4c 73 20 69 6e 20 74 68 65 20 52 48 53 20  ULLs in the RHS 
2176f 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2c  does not matter,
21770 20 73 6f 20 6a 75 6d 70 0a 20 20 20 20 20 20 2a   so jump.      *
21771 2a 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68  * over all of th
21772 65 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c  e code that foll
21773 6f 77 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ows..      */.  
21774 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
21775 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c  VdbeAddOp4Int(v,
21776 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72   OP_Found, pExpr
21777 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 31 2c  ->iTable, 0, r1,
21778 20 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 48   1);..      /* H
21779 65 72 65 20 77 65 20 62 65 67 69 6e 20 67 65 6e  ere we begin gen
2177a 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 68 61  erating code tha
2177b 74 20 72 75 6e 73 20 69 66 20 74 68 65 20 4c 48  t runs if the LH
2177c 53 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a  S is not.      *
2177d 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  * contained with
2177e 69 6e 20 74 68 65 20 52 48 53 2e 20 20 47 65 6e  in the RHS.  Gen
2177f 65 72 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c  erate additional
21780 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20 20   code that.     
21781 20 2a 2a 20 74 65 73 74 73 20 74 68 65 20 52 48   ** tests the RH
21782 53 20 66 6f 72 20 4e 55 4c 4c 73 2e 20 20 49 66  S for NULLs.  If
21783 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69 6e   the RHS contain
21784 73 20 61 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 20  s a NULL then.  
21785 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 64      ** jump to d
21786 65 73 74 49 66 4e 75 6c 6c 2e 20 20 49 66 20 74  estIfNull.  If t
21787 68 65 72 65 20 61 72 65 20 6e 6f 20 4e 55 4c 4c  here are no NULL
21788 73 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 65  s in the RHS the
21789 6e 0a 20 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20  n.      ** jump 
2178a 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 2e 0a  to destIfFalse..
2178b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a        */.      j
2178c 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
2178d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
2178e 75 6c 6c 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c  ull, rRhsHasNull
2178f 29 3b 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 71  );.      j3 = sq
21790 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49  lite3VdbeAddOp4I
21791 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20  nt(v, OP_Found, 
21792 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30  pExpr->iTable, 0
21793 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31  , rRhsHasNull, 1
21794 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21795 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21796 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 52  _Integer, -1, rR
21797 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 20  hsHasNull);.    
21798 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21799 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20  pHere(v, j3);.  
2179a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2179b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2179c 6d 6d 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c  mm, rRhsHasNull,
2179d 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
2179e 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2179f 2c 20 6a 32 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  , j2);..      /*
217a0 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 61 70 70   Jump to the app
217a1 72 6f 70 72 69 61 74 65 20 74 61 72 67 65 74 20  ropriate target 
217a2 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65  depending on whe
217a3 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20  ther or not.    
217a4 20 20 2a 2a 20 74 68 65 20 52 48 53 20 63 6f 6e    ** the RHS con
217a5 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 20 20 20  tains a NULL.   
217a6 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
217a7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
217a8 20 4f 50 5f 49 66 2c 20 72 52 68 73 48 61 73 4e   OP_If, rRhsHasN
217a9 75 6c 6c 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29  ull, destIfNull)
217aa 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
217ab 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
217ac 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 49 66 46  Goto, 0, destIfF
217ad 61 6c 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  alse);..      /*
217ae 20 54 68 65 20 4f 50 5f 46 6f 75 6e 64 20 61 74   The OP_Found at
217af 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73   the top of this
217b0 20 62 72 61 6e 63 68 20 6a 75 6d 70 73 20 68 65   branch jumps he
217b1 72 65 20 77 68 65 6e 20 74 72 75 65 2c 20 0a 20  re when true, . 
217b2 20 20 20 20 20 2a 2a 20 63 61 75 73 69 6e 67 20       ** causing 
217b3 74 68 65 20 6f 76 65 72 61 6c 6c 20 49 4e 20 65  the overall IN e
217b4 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61  xpression evalua
217b5 74 69 6f 6e 20 74 6f 20 66 61 6c 6c 20 74 68 72  tion to fall thr
217b6 6f 75 67 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ough..      */. 
217b7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
217b8 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
217b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
217ba 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
217bb 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
217bc 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
217bd 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
217be 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28  ;.  VdbeComment(
217bf 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72  (v, "end IN expr
217c0 22 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  "));.}.#endif /*
217c1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
217c2 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  QUERY */../*.** 
217c3 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62  Duplicate an 8-b
217c4 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61  yte value.*/.sta
217c5 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79  tic char *dup8by
217c6 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e  tes(Vdbe *v, con
217c7 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20  st char *in){.  
217c8 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69  char *out = sqli
217c9 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
217ca 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
217cb 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29   8);.  if( out )
217cc 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74  {.    memcpy(out
217cd 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20  , in, 8);.  }.  
217ce 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
217cf 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
217d0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
217d1 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66  t will put the f
217d2 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
217d3 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64   value described
217d4 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e   by z[0..n-1] in
217d5 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
217d6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20  ..**.** The z[] 
217d7 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62  string will prob
217d8 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f  ably not be zero
217d9 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75  -terminated.  Bu
217da 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63  t the .** z[n] c
217db 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72  haracter is guar
217dc 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d  anteed to be som
217dd 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73  ething that does
217de 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b   not look.** lik
217df 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69  e the continuati
217e0 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  on of the number
217e1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
217e2 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a   codeReal(Vdbe *
217e3 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  v, const char *z
217e4 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67  , int negateFlag
217e5 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69  , int iMem){.  i
217e6 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30 29 20  f( ALWAYS(z!=0) 
217e7 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61  ){.    double va
217e8 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  lue;.    char *z
217e9 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74  V;.    sqlite3At
217ea 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20  oF(z, &value);. 
217eb 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69     assert( !sqli
217ec 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20  te3IsNaN(value) 
217ed 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 74  ); /* The new At
217ee 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73  oF never returns
217ef 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20   NaN */.    if( 
217f0 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c  negateFlag ) val
217f1 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20  ue = -value;.   
217f2 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28   zV = dup8bytes(
217f3 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65  v, (char*)&value
217f4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
217f5 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52  beAddOp4(v, OP_R
217f6 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c  eal, 0, iMem, 0,
217f7 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20   zV, P4_REAL);. 
217f8 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   }.}.../*.** Gen
217f9 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63  erate an instruc
217fa 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70  tion that will p
217fb 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64  ut the integer d
217fc 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65  escribe by.** te
217fd 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74  xt z[0..n-1] int
217fe 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e  o register iMem.
217ff 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73  .**.** The z[] s
21800 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61  tring will proba
21801 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d  bly not be zero-
21802 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74  terminated.  But
21803 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68   the .** z[n] ch
21804 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61  aracter is guara
21805 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65  nteed to be some
21806 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20  thing that does 
21807 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65  not look.** like
21808 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f   the continuatio
21809 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e  n of the number.
2180a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2180b 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62 65  codeInteger(Vdbe
2180c 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72   *v, Expr *pExpr
2180d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69  , int negFlag, i
2180e 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20  nt iMem){.  if( 
2180f 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
21810 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20  P_IntValue ){.  
21811 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72 2d    int i = pExpr-
21812 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 69  >u.iValue;.    i
21813 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d  f( negFlag ) i =
21814 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   -i;.    sqlite3
21815 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21816 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65  _Integer, i, iMe
21817 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
21818 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
21819 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
2181a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 21  ;.    assert( z!
2181b 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71  =0 );.    if( sq
2181c 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
2181d 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b  s(z, negFlag) ){
2181e 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65  .      i64 value
2181f 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56  ;.      char *zV
21820 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  ;.      sqlite3A
21821 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29  toi64(z, &value)
21822 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46  ;.      if( negF
21823 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76  lag ) value = -v
21824 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d  alue;.      zV =
21825 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63   dup8bytes(v, (c
21826 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20  har*)&value);.  
21827 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21828 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36  ddOp4(v, OP_Int6
21829 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a  4, 0, iMem, 0, z
2182a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20  V, P4_INT64);.  
2182b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
2182c 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65  odeReal(v, z, ne
2182d 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20  gFlag, iMem);.  
2182e 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2182f 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 65   Clear a cache e
21830 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ntry..*/.static 
21831 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79 43  void cacheEntryC
21832 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72  lear(Parse *pPar
21833 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43  se, struct yColC
21834 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  ache *p){.  if( 
21835 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20  p->tempReg ){.  
21836 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54    if( pParse->nT
21837 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65  empReg<ArraySize
21838 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65  (pParse->aTempRe
21839 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  g) ){.      pPar
2183a 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61  se->aTempReg[pPa
2183b 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d  rse->nTempReg++]
2183c 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20   = p->iReg;.    
2183d 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67  }.    p->tempReg
2183e 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
2183f 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68  .** Record in th
21840 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74  e column cache t
21841 68 61 74 20 61 20 70 61 72 74 69 63 75 6c 61 72  hat a particular
21842 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a   column from a.*
21843 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  * particular tab
21844 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  le is stored in 
21845 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67  a particular reg
21846 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ister..*/.SQLITE
21847 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21848 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74  lite3ExprCacheSt
21849 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ore(Parse *pPars
2184a 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74  e, int iTab, int
2184b 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29   iCol, int iReg)
2184c 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
2184d 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69   minLru;.  int i
2184e 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20  dxLru;.  struct 
2184f 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20  yColCache *p;.. 
21850 20 61 73 73 65 72 74 28 20 69 52 65 67 3e 30 20   assert( iReg>0 
21851 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  );  /* Register 
21852 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 77 61  numbers are alwa
21853 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20  ys positive */. 
21854 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d   assert( iCol>=-
21855 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36 38 20  1 && iCol<32768 
21856 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f  );  /* Finite co
21857 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a  lumn numbers */.
21858 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65 70 6c  .  /* First repl
21859 61 63 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67  ace any existing
2185a 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28   entry */.  for(
2185b 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
2185c 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2185d 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
2185e 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
2185f 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e  ( p->iReg && p->
21860 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20  iTable==iTab && 
21861 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c  p->iColumn==iCol
21862 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45   ){.      cacheE
21863 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65  ntryClear(pParse
21864 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  , p);.      p->i
21865 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e  Level = pParse->
21866 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20  iCacheLevel;.   
21867 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65     p->iReg = iRe
21868 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43  g;.      p->affC
21869 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20  hange = 0;.     
2186a 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65   p->lru = pParse
2186b 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20  ->iCacheCnt++;. 
2186c 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2186d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e   }.  }..  /* Fin
2186e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20  d an empty slot 
2186f 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74 20 2a  and replace it *
21870 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  /.  for(i=0, p=p
21871 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
21872 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
21873 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
21874 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65  {.    if( p->iRe
21875 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d  g==0 ){.      p-
21876 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  >iLevel = pParse
21877 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20  ->iCacheLevel;. 
21878 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d       p->iTable =
21879 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e   iTab;.      p->
2187a 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a  iColumn = iCol;.
2187b 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
2187c 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61  iReg;.      p->a
2187d 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  ffChange = 0;.  
2187e 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d      p->tempReg =
2187f 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75   0;.      p->lru
21880 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
21881 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65  eCnt++;.      re
21882 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
21883 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68  .  /* Replace th
21884 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c 79 20  e last recently 
21885 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75  used */.  minLru
21886 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20   = 0x7fffffff;. 
21887 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20   idxLru = -1;.  
21888 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
21889 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
2188a 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
2188b 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
2188c 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e    if( p->lru<min
2188d 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 64 78  Lru ){.      idx
2188e 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d  Lru = i;.      m
2188f 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a  inLru = p->lru;.
21890 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
21891 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e 3d 30  ALWAYS(idxLru>=0
21892 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50  ) ){.    p = &pP
21893 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b  arse->aColCache[
21894 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e  idxLru];.    p->
21895 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d  iLevel = pParse-
21896 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20  >iCacheLevel;.  
21897 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54    p->iTable = iT
21898 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75  ab;.    p->iColu
21899 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70  mn = iCol;.    p
2189a 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20  ->iReg = iReg;. 
2189b 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20     p->affChange 
2189c 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70  = 0;.    p->temp
2189d 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  Reg = 0;.    p->
2189e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  lru = pParse->iC
2189f 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72  acheCnt++;.    r
218a0 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  eturn;.  }.}../*
218a1 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61  .** Indicate tha
218a2 74 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20  t a register is 
218a3 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65  being overwritte
218a4 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20 72 65  n.  Purge the re
218a5 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74  gister.** from t
218a6 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e  he column cache.
218a7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
218a8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
218a9 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50  xprCacheRemove(P
218aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
218ab 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t iReg){.  int i
218ac 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
218ad 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69  ache *p;.  for(i
218ae 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
218af 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
218b0 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
218b1 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
218b2 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29   p->iReg==iReg )
218b3 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74  {.      cacheEnt
218b4 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20  ryClear(pParse, 
218b5 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65  p);.      p->iRe
218b6 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
218b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62  .}../*.** Rememb
218b8 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  er the current c
218b9 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74  olumn cache cont
218ba 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e  ext.  Any new en
218bb 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a 20 61  tries added.** a
218bc 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f 6c 75  dded to the colu
218bd 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72 20 74  mn cache after t
218be 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72 65 6d  his call are rem
218bf 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  oved when the.**
218c0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70   corresponding p
218c1 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  op occurs..*/.SQ
218c2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
218c3 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
218c4 68 65 50 75 73 68 28 50 61 72 73 65 20 2a 70 50  hePush(Parse *pP
218c5 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73 65 2d  arse){.  pParse-
218c6 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a  >iCacheLevel++;.
218c7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
218c8 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20  from the column 
218c9 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72 69 65  cache any entrie
218ca 73 20 74 68 61 74 20 77 65 72 65 20 61 64 64 65  s that were adde
218cb 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74  d since the.** t
218cc 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20 50 75  he previous N Pu
218cd 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20  sh operations.  
218ce 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
218cf 72 65 73 74 6f 72 65 20 74 68 65 20 63 61 63 68  restore the cach
218d0 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 74  e.** to the stat
218d1 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20 50 75  e it was in N Pu
218d2 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c  shes ago..*/.SQL
218d3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
218d4 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
218d5 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50 61 72  ePop(Parse *pPar
218d6 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e  se, int N){.  in
218d7 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43  t i;.  struct yC
218d8 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 73  olCache *p;.  as
218d9 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61  sert( N>0 );.  a
218da 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69  ssert( pParse->i
218db 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b  CacheLevel>=N );
218dc 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68  .  pParse->iCach
218dd 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66  eLevel -= N;.  f
218de 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
218df 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
218e0 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
218e1 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
218e2 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20   if( p->iReg && 
218e3 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 65  p->iLevel>pParse
218e4 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 29 7b  ->iCacheLevel ){
218e5 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72  .      cacheEntr
218e6 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70  yClear(pParse, p
218e7 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  );.      p->iReg
218e8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
218e9 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  }../*.** When a 
218ea 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73  cached column is
218eb 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20 73 75   reused, make su
218ec 72 65 20 74 68 61 74 20 69 74 73 20 72 65 67 69  re that its regi
218ed 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f  ster is.** no lo
218ee 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65 20 61  nger available a
218ef 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65  s a temp registe
218f0 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38 37 39  r.  ticket #3879
218f1 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20  :  that same.** 
218f2 72 65 67 69 73 74 65 72 20 6d 69 67 68 74 20 62  register might b
218f3 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 69  e in the cache i
218f4 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65  n multiple place
218f5 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 74 6f  s, so be sure to
218f6 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61 6c 6c  .** get them all
218f7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
218f8 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
218f9 65 50 69 6e 52 65 67 69 73 74 65 72 28 50 61 72  ePinRegister(Par
218fa 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
218fb 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  iReg){.  int i;.
218fc 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
218fd 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30  he *p;.  for(i=0
218fe 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
218ff 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
21900 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
21901 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
21902 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a  ->iReg==iReg ){.
21903 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67        p->tempReg
21904 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
21905 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
21906 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
21907 20 65 78 74 72 61 63 74 20 74 68 65 20 69 43 6f   extract the iCo
21908 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66  lumn-th column f
21909 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61  rom.** table pTa
2190a 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  b and store the 
2190b 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20  column value in 
2190c 61 20 72 65 67 69 73 74 65 72 2e 20 20 41 6e 20  a register.  An 
2190d 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64  effort.** is mad
2190e 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  e to store the c
2190f 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72  olumn value in r
21910 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 62 75  egister iReg, bu
21911 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74  t this is.** not
21912 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 54 68   guaranteed.  Th
21913 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  e location of th
21914 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69  e column value i
21915 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
21916 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20  * There must be 
21917 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74  an open cursor t
21918 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62 6c 65  o pTab in iTable
21919 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
2191a 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e  ne.** is called.
2191b 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74    If iColumn<0 t
2191c 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e 65  hen code is gene
2191d 72 61 74 65 64 20 74 68 61 74 20 65 78 74 72 61  rated that extra
2191e 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a  cts the rowid..*
2191f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
21920 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70 74 20  e might attempt 
21921 74 6f 20 72 65 75 73 65 20 74 68 65 20 76 61 6c  to reuse the val
21922 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ue of the column
21923 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72   that.** has alr
21924 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64  eady been loaded
21925 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72   into a register
21926 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77 69 6c  .  The value wil
21927 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75  l always.** be u
21928 73 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f  sed if it has no
21929 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20  t undergone any 
2192a 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73  affinity changes
2192b 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20  .  But if.** an 
2192c 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20  affinity change 
2192d 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68  has occurred, th
2192e 65 6e 20 74 68 65 20 63 61 63 68 65 64 20 76 61  en the cached va
2192f 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65  lue will only be
21930 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f  .** used if allo
21931 77 41 66 66 43 68 6e 67 20 69 73 20 74 72 75 65  wAffChng is true
21932 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21933 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
21934 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e  xprCodeGetColumn
21935 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
21936 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  e,   /* Parsing 
21937 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
21938 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
21939 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2193a 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 6f 6e    /* Description
2193b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65   of the table we
2193c 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f   are reading fro
2193d 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75  m */.  int iColu
2193e 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  mn,     /* Index
2193f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
21940 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54  lumn */.  int iT
21941 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  able,      /* Th
21942 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
21943 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a  g to the table *
21944 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20  /.  int iReg,   
21945 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 72 65       /* Store re
21946 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
21947 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67  int allowAffChng
21948 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 69 6f   /* True if prio
21949 72 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67  r affinity chang
2194a 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a  es are OK */.){.
2194b 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2194c 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2194d 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
2194e 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f  lCache *p;..  fo
2194f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
21950 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
21951 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
21952 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
21953 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 26  if( p->iReg>0 &&
21954 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62   p->iTable==iTab
21955 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e  le && p->iColumn
21956 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20  ==iColumn.      
21957 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66       && (!p->aff
21958 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41  Change || allowA
21959 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20 20 20  ffChng) ){.     
2195a 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65   p->lru = pParse
2195b 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20  ->iCacheCnt++;. 
2195c 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2195d 43 61 63 68 65 50 69 6e 52 65 67 69 73 74 65 72  CachePinRegister
2195e 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52 65 67  (pParse, p->iReg
2195f 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
21960 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20  p->iReg;.    }. 
21961 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28 20 76   }  .  assert( v
21962 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f  !=0 );.  if( iCo
21963 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71  lumn<0 ){.    sq
21964 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21965 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61  v, OP_Rowid, iTa
21966 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65  ble, iReg);.  }e
21967 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70  lse if( ALWAYS(p
21968 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 69  Tab!=0) ){.    i
21969 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74 75 61  nt op = IsVirtua
2196a 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f  l(pTab) ? OP_VCo
2196b 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e  lumn : OP_Column
2196c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2196d 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69  eAddOp3(v, op, i
2196e 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Table, iColumn, 
2196f 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  iReg);.    sqlit
21970 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28  e3ColumnDefault(
21971 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e  v, pTab, iColumn
21972 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73  , iReg);.  }.  s
21973 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53  qlite3ExprCacheS
21974 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54 61  tore(pParse, iTa
21975 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52  ble, iColumn, iR
21976 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52  eg);.  return iR
21977 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  eg;.}../*.** Cle
21978 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61  ar all column ca
21979 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a  che entries..*/.
2197a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2197b 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
2197c 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 20  acheClear(Parse 
2197d 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20  *pParse){.  int 
2197e 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
2197f 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72  Cache *p;..  for
21980 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
21981 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
21982 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
21983 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
21984 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20  f( p->iReg ){.  
21985 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c      cacheEntryCl
21986 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  ear(pParse, p);.
21987 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
21988 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
21989 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65  /*.** Record the
2198a 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66   fact that an af
2198b 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61  finity change ha
2198c 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43  s occurred on iC
2198d 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72  ount.** register
2198e 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  s starting with 
2198f 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54  iStart..*/.SQLIT
21990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21991 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
21992 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61  ffinityChange(Pa
21993 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
21994 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f   iStart, int iCo
21995 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64  unt){.  int iEnd
21996 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75   = iStart + iCou
21997 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b  nt - 1;.  int i;
21998 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
21999 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d  che *p;.  for(i=
2199a 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
2199b 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45  lCache; i<SQLITE
2199c 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b  _N_COLCACHE; i++
2199d 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  , p++){.    int 
2199e 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20  r = p->iReg;.   
2199f 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26   if( r>=iStart &
219a0 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20  & r<=iEnd ){.   
219a1 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20     p->affChange 
219a2 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 1;.    }.  }.}
219a3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
219a4 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f   code to move co
219a5 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73  ntent from regis
219a6 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72  ters iFrom...iFr
219a7 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65  om+nReg-1.** ove
219a8 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52  r to iTo..iTo+nR
219a9 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63  eg-1. Keep the c
219aa 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74  olumn cache up-t
219ab 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  o-date..*/.SQLIT
219ac 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
219ad 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f  qlite3ExprCodeMo
219ae 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ve(Parse *pParse
219af 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
219b0 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b   iTo, int nReg){
219b1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
219b2 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b  ct yColCache *p;
219b3 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46 72  .  if( NEVER(iFr
219b4 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 72  om==iTo) ) retur
219b5 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  n;.  sqlite3Vdbe
219b6 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70  AddOp3(pParse->p
219b7 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69  Vdbe, OP_Move, i
219b8 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29  From, iTo, nReg)
219b9 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  ;.  for(i=0, p=p
219ba 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
219bb 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
219bc 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
219bd 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 2d  {.    int x = p-
219be 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 78  >iReg;.    if( x
219bf 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 46 72  >=iFrom && x<iFr
219c0 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 20  om+nReg ){.     
219c1 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d   p->iReg += iTo-
219c2 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  iFrom;.    }.  }
219c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
219c4 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20  te code to copy 
219c5 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67  content from reg
219c6 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69  isters iFrom...i
219c7 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f  From+nReg-1.** o
219c8 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b  ver to iTo..iTo+
219c9 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54  nReg-1..*/.SQLIT
219ca 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
219cb 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
219cc 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  py(Parse *pParse
219cd 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74  , int iFrom, int
219ce 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b   iTo, int nReg){
219cf 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
219d0 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f  NEVER(iFrom==iTo
219d1 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  ) ) return;.  fo
219d2 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69  r(i=0; i<nReg; i
219d3 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
219d4 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73  VdbeAddOp2(pPars
219d5 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70  e->pVdbe, OP_Cop
219d6 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b  y, iFrom+i, iTo+
219d7 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  i);.  }.}../*.**
219d8 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
219d9 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e 20  any register in 
219da 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e  the range iFrom.
219db 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29  .iTo (inclusive)
219dc 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 70  .** is used as p
219dd 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  art of the colum
219de 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74  n cache..*/.stat
219df 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c  ic int usedAsCol
219e0 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a  umnCache(Parse *
219e1 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f  pParse, int iFro
219e2 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69  m, int iTo){.  i
219e3 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79  nt i;.  struct y
219e4 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66  ColCache *p;.  f
219e5 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
219e6 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
219e7 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
219e8 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
219e9 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67   int r = p->iReg
219ea 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72  ;.    if( r>=iFr
219eb 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72  om && r<=iTo ) r
219ec 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
219ed 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
219ee 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69 6e  * If the last in
219ef 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 20  struction coded 
219f0 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  is an ephemeral 
219f1 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a  copy of any of.*
219f2 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20  * the registers 
219f3 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67 69  in the nReg regi
219f4 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
219f5 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e 0a  with iReg, then.
219f6 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c  ** convert the l
219f7 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ast instruction 
219f8 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f  from OP_SCopy to
219f9 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c   OP_Copy..*/.SQL
219fa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
219fb 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64   sqlite3ExprHard
219fc 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72  Copy(Parse *pPar
219fd 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e  se, int iReg, in
219fe 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62 65 4f  t nReg){.  VdbeO
219ff 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a  p *pOp;.  Vdbe *
21a00 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  v;..  assert( pP
21a01 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
21a02 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76  Failed==0 );.  v
21a03 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
21a04 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
21a05 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69   );.  pOp = sqli
21a06 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
21a07 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  -1);.  assert( p
21a08 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  Op!=0 );.  if( p
21a09 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
21a0a 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e  Copy && pOp->p1>
21a0b 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31  =iReg && pOp->p1
21a0c 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20  <iReg+nReg ){.  
21a0d 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
21a0e 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a  OP_Copy;.  }.}..
21a0f 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21a10 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ode to store the
21a11 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41   value of the iA
21a12 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e  lias-th alias in
21a13 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72   register.** tar
21a14 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74 20  get.  The first 
21a15 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c  time this is cal
21a16 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65 76  led, pExpr is ev
21a17 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75  aluated to compu
21a18 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  te.** the value 
21a19 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54  of the alias.  T
21a1a 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
21a1b 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61  ed in an auxilia
21a1c 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61  ry register.** a
21a1d 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
21a1e 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20 69   that register i
21a1f 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20  s returned.  On 
21a20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
21a21 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65  ,.** the registe
21a22 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75  r number is retu
21a23 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e  rned without gen
21a24 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65  erating any code
21a25 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
21a26 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74  t in order for t
21a27 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64  his to work, cod
21a28 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61  e must be genera
21a29 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61  ted in the.** sa
21a2a 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69 74  me order that it
21a2b 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a   is executed..**
21a2c 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20  .** Aliases are 
21a2d 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e  numbered startin
21a2e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41  g with 1.  So iA
21a2f 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20 72  lias is in the r
21a30 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20  ange.** of 1 to 
21a31 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69  pParse->nAlias i
21a32 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a  nclusive.  .**.*
21a33 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  * pParse->aAlias
21a34 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72  [iAlias-1] recor
21a35 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ds the register 
21a36 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 65  number where the
21a37 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65   value.** of the
21a38 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73   iAlias-th alias
21a39 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20   is stored.  If 
21a3a 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73  zero, that means
21a3b 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69   that the.** ali
21a3c 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  as has not yet b
21a3d 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f  een computed..*/
21a3e 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
21a3f 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50 61  Alias(Parse *pPa
21a40 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c  rse, int iAlias,
21a41 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
21a42 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66 20 30  t target){.#if 0
21a43 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
21a44 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
21a45 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28 20 70  nt iReg;.  if( p
21a46 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c  Parse->nAliasAll
21a47 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  oc<pParse->nAlia
21a48 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  s ){.    pParse-
21a49 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65  >aAlias = sqlite
21a4a 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
21a4b 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c  (db, pParse->aAl
21a4c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ias,.           
21a4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a4e 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 50 61        sizeof(pPa
21a4f 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a  rse->aAlias[0])*
21a50 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29  pParse->nAlias )
21a51 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
21a52 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21a53 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69   && pParse->nAli
21a54 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20  asAlloc>0 );.   
21a55 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
21a56 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30  ailed ) return 0
21a57 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 50  ;.    memset(&pP
21a58 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61  arse->aAlias[pPa
21a59 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63  rse->nAliasAlloc
21a5a 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  ], 0,.          
21a5b 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73   (pParse->nAlias
21a5c 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41  -pParse->nAliasA
21a5d 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50 61  lloc)*sizeof(pPa
21a5e 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29  rse->aAlias[0]))
21a5f 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 41  ;.    pParse->nA
21a60 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72  liasAlloc = pPar
21a61 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a  se->nAlias;.  }.
21a62 20 20 61 73 73 65 72 74 28 20 69 41 6c 69 61 73    assert( iAlias
21a63 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50  >0 && iAlias<=pP
21a64 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a  arse->nAlias );.
21a65 20 20 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d    iReg = pParse-
21a66 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31  >aAlias[iAlias-1
21a67 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30  ];.  if( iReg==0
21a68 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72   ){.    if( pPar
21a69 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e  se->iCacheLevel>
21a6a 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20  0 ){.      iReg 
21a6b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
21a6c 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
21a6d 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a  pExpr, target);.
21a6e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21a6f 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65   iReg = ++pParse
21a70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71  ->nMem;.      sq
21a71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
21a72 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65  arse, pExpr, iRe
21a73 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  g);.      pParse
21a74 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d  ->aAlias[iAlias-
21a75 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d  1] = iReg;.    }
21a76 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52  .  }.  return iR
21a77 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53  eg;.#else.  UNUS
21a78 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 41 6c  ED_PARAMETER(iAl
21a79 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ias);.  return s
21a7a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21a7b 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21a7c 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23 65 6e  pr, target);.#en
21a7d 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  dif.}../*.** Gen
21a7e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20  erate code into 
21a7f 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 65  the current Vdbe
21a80 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65   to evaluate the
21a81 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73   given.** expres
21a82 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74  sion.  Attempt t
21a83 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  o store the resu
21a84 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  lts in register 
21a85 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74  "target"..** Ret
21a86 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72  urn the register
21a87 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61   where results a
21a88 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  re stored..**.**
21a89 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74 69   With this routi
21a8a 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ne, there is no 
21a8b 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 72  guarantee that r
21a8c 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62  esults will.** b
21a8d 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67  e stored in targ
21a8e 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  et.  The result 
21a8f 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64 20  might be stored 
21a90 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  in some other.**
21a91 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74 20   register if it 
21a92 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f  is convenient to
21a93 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c   do so.  The cal
21a94 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
21a95 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20   must check the 
21a96 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20  return code and 
21a97 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74 73  move the results
21a98 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a   to the desired.
21a99 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a  ** register..*/.
21a9a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
21a9b 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
21a9c 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20 2a  deTarget(Parse *
21a9d 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
21a9e 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29  xpr, int target)
21a9f 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
21aa0 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a  arse->pVdbe;  /*
21aa1 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f   The VM under co
21aa2 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20  nstruction */.  
21aa3 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
21aa4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
21aa5 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f   opcode being co
21aa6 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52  ded */.  int inR
21aa7 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20  eg = target;    
21aa8 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74     /* Results st
21aa9 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
21aaa 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20   inReg */.  int 
21aab 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20  regFree1 = 0;   
21aac 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d        /* If non-
21aad 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 74  zero free this t
21aae 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
21aaf 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72  r */.  int regFr
21ab0 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ee2 = 0;        
21ab1 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20   /* If non-zero 
21ab2 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72  free this tempor
21ab3 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  ary register */.
21ab4 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33    int r1, r2, r3
21ab5 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56  , r4;       /* V
21ab6 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72 20  arious register 
21ab7 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c  numbers */.  sql
21ab8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
21ab9 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 61  e->db; /* The da
21aba 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
21abb 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  n */..  assert( 
21abc 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67  target>0 && targ
21abd 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  et<=pParse->nMem
21abe 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29   );.  if( v==0 )
21abf 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
21ac0 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
21ac1 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65  Failed );.    re
21ac2 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  turn 0;.  }..  i
21ac3 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20  f( pExpr==0 ){. 
21ac4 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b     op = TK_NULL;
21ac5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70  .  }else{.    op
21ac6 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
21ac7 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  }.  switch( op )
21ac8 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
21ac9 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  G_COLUMN: {.    
21aca 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49    AggInfo *pAggI
21acb 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67  nfo = pExpr->pAg
21acc 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72  gInfo;.      str
21acd 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20  uct AggInfo_col 
21ace 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66  *pCol = &pAggInf
21acf 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69  o->aCol[pExpr->i
21ad0 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  Agg];.      if( 
21ad1 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63  !pAggInfo->direc
21ad2 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20  tMode ){.       
21ad3 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69   assert( pCol->i
21ad4 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Mem>0 );.       
21ad5 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69   inReg = pCol->i
21ad6 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65  Mem;.        bre
21ad7 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ak;.      }else 
21ad8 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73  if( pAggInfo->us
21ad9 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20  eSortingIdx ){. 
21ada 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21adb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
21adc 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d  olumn, pAggInfo-
21add 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20  >sortingIdx,.   
21ade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21adf 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
21ae0 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20  >iSorterColumn, 
21ae1 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
21ae2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
21ae3 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69        /* Otherwi
21ae4 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e  se, fall thru in
21ae5 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e  to the TK_COLUMN
21ae6 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20   case */.    }. 
21ae7 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d     case TK_COLUM
21ae8 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70  N: {.      if( p
21ae9 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29  Expr->iTable<0 )
21aea 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
21aeb 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77  s only happens w
21aec 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b  hen coding check
21aed 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
21aee 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
21aef 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30  pParse->ckBase>0
21af0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65   );.        inRe
21af1 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  g = pExpr->iColu
21af2 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42  mn + pParse->ckB
21af3 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ase;.      }else
21af4 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  {.        testca
21af5 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67  se( (pExpr->flag
21af6 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d  s & EP_AnyAff)!=
21af7 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52  0 );.        inR
21af8 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
21af9 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50  CodeGetColumn(pP
21afa 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61  arse, pExpr->pTa
21afb 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
21afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21afd 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
21afe 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  mn, pExpr->iTabl
21aff 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20  e, target,.     
21b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b01 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
21b02 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e  r->flags & EP_An
21b03 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20  yAff);.      }. 
21b04 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21b05 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  }.    case TK_IN
21b06 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63  TEGER: {.      c
21b07 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45  odeInteger(v, pE
21b08 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b  xpr, 0, target);
21b09 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21b0a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21b0b 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 61  FLOAT: {.      a
21b0c 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
21b0d 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21b0e 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21b0f 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c       codeReal(v,
21b10 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
21b11 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
21b12 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21b13 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52  .    case TK_STR
21b14 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ING: {.      ass
21b15 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21b16 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21b17 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
21b18 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21b19 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
21b1a 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 30  g8, 0, target, 0
21b1b 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
21b1c 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  n, 0);.      bre
21b1d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
21b1e 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20  se TK_NULL: {.  
21b1f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21b20 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
21b21 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
21b22 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21b23 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21b24 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
21b25 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c  L.    case TK_BL
21b26 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  OB: {.      int 
21b27 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  n;.      const c
21b28 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68  har *z;.      ch
21b29 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20  ar *zBlob;.     
21b2a 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
21b2b 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
21b2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b   EP_IntValue) );
21b2d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21b2e 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30  Expr->u.zToken[0
21b2f 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d  ]=='x' || pExpr-
21b30 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58  >u.zToken[0]=='X
21b31 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ' );.      asser
21b32 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  t( pExpr->u.zTok
21b33 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20  en[1]=='\'' );. 
21b34 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70 72 2d       z = &pExpr-
21b35 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20  >u.zToken[2];.  
21b36 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53      n = sqlite3S
21b37 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a  trlen30(z) - 1;.
21b38 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b        assert( z[
21b39 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  n]=='\'' );.    
21b3a 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65    zBlob = sqlite
21b3b 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74  3HexToBlob(sqlit
21b3c 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20  e3VdbeDb(v), z, 
21b3d 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
21b3e 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
21b3f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72  P_Blob, n/2, tar
21b40 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50  get, 0, zBlob, P
21b41 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
21b42 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
21b43 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
21b44 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20  K_VARIABLE: {.  
21b45 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b      VdbeOp *pOp;
21b46 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
21b47 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
21b48 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
21b49 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ue) );.      ass
21b4a 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ert( pExpr->u.zT
21b4b 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20  oken!=0 );.     
21b4c 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
21b4d 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29  u.zToken[0]!=0 )
21b4e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
21b4f 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d  r->u.zToken[1]==
21b50 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70  0.         && (p
21b51 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
21b52 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f  GetOp(v, -1))->o
21b53 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62  pcode==OP_Variab
21b54 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  le.         && p
21b55 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d  Op->p1+pOp->p3==
21b56 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20  pExpr->iColumn. 
21b57 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e          && pOp->
21b58 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67  p2+pOp->p3==targ
21b59 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  et.         && p
21b5a 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20  Op->p4.z==0.    
21b5b 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20    ){.        /* 
21b5c 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  If the previous 
21b5d 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20  instruction was 
21b5e 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72  a copy of the pr
21b5f 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20  evious unnamed. 
21b60 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65         ** parame
21b61 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65  ter into the pre
21b62 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20  vious register, 
21b63 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72  then simply incr
21b64 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20  ement the.      
21b65 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e    ** repeat coun
21b66 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69  t on the prior i
21b67 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65  nstruction rathe
21b68 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20  r than making a 
21b69 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  new.        ** i
21b6a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20  nstruction..    
21b6b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
21b6c 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20  Op->p3++;.      
21b6d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
21b6e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
21b6f 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c  (v, OP_Variable,
21b70 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c   pExpr->iColumn,
21b71 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20   target, 1);.   
21b72 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21b73 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29  u.zToken[1]!=0 )
21b74 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
21b75 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
21b76 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e  v, -1, pExpr->u.
21b77 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20  zToken, 0);.    
21b78 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
21b79 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21b7a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
21b7b 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  ISTER: {.      i
21b7c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54  nReg = pExpr->iT
21b7d 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61  able;.      brea
21b7e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21b7f 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20  e TK_AS: {.     
21b80 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69   inReg = codeAli
21b81 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  as(pParse, pExpr
21b82 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d  ->iTable, pExpr-
21b83 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
21b84 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21b85 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
21b86 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20  TE_OMIT_CAST.   
21b87 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b   case TK_CAST: {
21b88 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73  .      /* Expres
21b89 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72  sions of the for
21b8a 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20  m:   CAST(pLeft 
21b8b 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20  AS token) */.   
21b8c 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f     int aff, to_o
21b8d 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  p;.      inReg =
21b8e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21b8f 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
21b90 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72  Expr->pLeft, tar
21b91 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  get);.      asse
21b92 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
21b93 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
21b94 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
21b95 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41    aff = sqlite3A
21b96 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70  ffinityType(pExp
21b97 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
21b98 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20      to_op = aff 
21b99 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  - SQLITE_AFF_TEX
21b9a 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20  T + OP_ToText;. 
21b9b 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
21b9c 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20  op==OP_ToText   
21b9d 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
21b9e 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20  AFF_TEXT    );. 
21b9f 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
21ba0 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20  op==OP_ToBlob   
21ba1 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
21ba2 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20  AFF_NONE    );. 
21ba3 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
21ba4 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  op==OP_ToNumeric
21ba5 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
21ba6 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20  AFF_NUMERIC );. 
21ba7 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
21ba8 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20  op==OP_ToInt    
21ba9 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
21baa 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20  AFF_INTEGER );. 
21bab 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f       assert( to_
21bac 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20  op==OP_ToReal   
21bad 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f   || aff!=SQLITE_
21bae 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20  AFF_REAL    );. 
21baf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
21bb0 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20  o_op==OP_ToText 
21bb1 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21bb2 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42  e( to_op==OP_ToB
21bb3 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73  lob );.      tes
21bb4 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50  tcase( to_op==OP
21bb5 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20  _ToNumeric );.  
21bb6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f      testcase( to
21bb7 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b  _op==OP_ToInt );
21bb8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21bb9 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61   to_op==OP_ToRea
21bba 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  l );.      if( i
21bbb 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a  nReg!=target ){.
21bbc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21bbd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21bbe 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61  SCopy, inReg, ta
21bbf 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69  rget);.        i
21bc0 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
21bc1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
21bc2 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21bc3 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b  , to_op, inReg);
21bc4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21bc5 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63   usedAsColumnCac
21bc6 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67  he(pParse, inReg
21bc7 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20  , inReg) );.    
21bc8 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21bc9 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
21bca 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20  (pParse, inReg, 
21bcb 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
21bcc 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
21bcd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
21bce 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b  T */.    case TK
21bcf 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
21bd0 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
21bd1 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
21bd2 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
21bd3 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
21bd4 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  _EQ: {.      ass
21bd5 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c  ert( TK_LT==OP_L
21bd6 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
21bd7 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20  t( TK_LE==OP_Le 
21bd8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21bd9 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b   TK_GT==OP_Gt );
21bda 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21bdb 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  K_GE==OP_Ge );. 
21bdc 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21bdd 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20  EQ==OP_Eq );.   
21bde 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45     assert( TK_NE
21bdf 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
21be0 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21be1 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LT );.      te
21be2 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
21be3 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
21be4 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29  ase( op==TK_GT )
21be5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21be6 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20  ( op==TK_GE );. 
21be7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21be8 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20  p==TK_EQ );.    
21be9 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21bea 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63  TK_NE );.      c
21beb 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
21bec 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
21bed 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
21bee 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
21bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21bf0 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
21bf1 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
21bf2 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
21bf3 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
21bf4 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21bf5 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
21bf6 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
21bf7 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
21bf8 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54  inReg, SQLITE_ST
21bf9 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65  OREP2);.      te
21bfa 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
21bfb 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
21bfc 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21bfd 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
21bfe 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21bff 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73  e TK_IS:.    cas
21c00 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20  e TK_ISNOT: {.  
21c01 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21c02 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20  ==TK_IS );.     
21c03 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21c04 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20  K_ISNOT );.     
21c05 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72   codeCompareOper
21c06 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78  ands(pParse, pEx
21c07 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20  pr->pLeft, &r1, 
21c08 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
21c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21c0b 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c  pr->pRight, &r2,
21c0c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
21c0d 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f     op = (op==TK_
21c0e 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b  IS) ? TK_EQ : TK
21c0f 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  _NE;.      codeC
21c10 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
21c11 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78  Expr->pLeft, pEx
21c12 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a  pr->pRight, op,.
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c14 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c    r1, r2, inReg,
21c15 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20   SQLITE_STOREP2 
21c16 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29  | SQLITE_NULLEQ)
21c17 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c18 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21c19 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c1a 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
21c1b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21c1c 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41   }.    case TK_A
21c1d 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ND:.    case TK_
21c1e 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OR:.    case TK_
21c1f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54  PLUS:.    case T
21c20 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65  K_STAR:.    case
21c21 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63   TK_MINUS:.    c
21c22 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20  ase TK_REM:.    
21c23 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a  case TK_BITAND:.
21c24 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f      case TK_BITO
21c25 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  R:.    case TK_S
21c26 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54  LASH:.    case T
21c27 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61  K_LSHIFT:.    ca
21c28 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20  se TK_RSHIFT: . 
21c29 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41     case TK_CONCA
21c2a 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  T: {.      asser
21c2b 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e  t( TK_AND==OP_An
21c2c 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
21c2d 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20  t( TK_OR==OP_Or 
21c2e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21c2f 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64   TK_PLUS==OP_Add
21c30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21c31 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53  ( TK_MINUS==OP_S
21c32 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20  ubtract );.     
21c33 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d   assert( TK_REM=
21c34 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b  =OP_Remainder );
21c35 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21c36 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74  K_BITAND==OP_Bit
21c37 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73  And );.      ass
21c38 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f  ert( TK_BITOR==O
21c39 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20  P_BitOr );.     
21c3a 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53   assert( TK_SLAS
21c3b 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a  H==OP_Divide );.
21c3c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21c3d 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66  _LSHIFT==OP_Shif
21c3e 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61  tLeft );.      a
21c3f 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54  ssert( TK_RSHIFT
21c40 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20  ==OP_ShiftRight 
21c41 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21c42 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43   TK_CONCAT==OP_C
21c43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74  oncat );.      t
21c44 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21c45 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73  AND );.      tes
21c46 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52  tcase( op==TK_OR
21c47 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c48 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20  se( op==TK_PLUS 
21c49 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21c4a 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20  e( op==TK_MINUS 
21c4b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21c4c 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b  e( op==TK_REM );
21c4d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c4e 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29   op==TK_BITAND )
21c4f 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c50 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29  ( op==TK_BITOR )
21c51 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c52 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29  ( op==TK_SLASH )
21c53 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c54 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20  ( op==TK_LSHIFT 
21c55 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21c56 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54  e( op==TK_RSHIFT
21c57 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c58 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41  se( op==TK_CONCA
21c59 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  T );.      r1 = 
21c5a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21c5b 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
21c5c 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
21c5d 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d  ee1);.      r2 =
21c5e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21c5f 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21c60 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67  pr->pRight, &reg
21c61 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71  Free2);.      sq
21c62 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21c63 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74  v, op, r2, r1, t
21c64 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65  arget);.      te
21c65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
21c66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
21c67 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21c68 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
21c69 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21c6a 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20  e TK_UMINUS: {. 
21c6b 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
21c6c 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
21c6d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21c6e 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66  Left );.      if
21c6f 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
21c70 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20  FLOAT ){.       
21c71 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
21c72 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
21c73 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b   EP_IntValue) );
21c74 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 61  .        codeRea
21c75 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54  l(v, pLeft->u.zT
21c76 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 29  oken, 1, target)
21c77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
21c78 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
21c79 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
21c7a 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76     codeInteger(v
21c7b 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67  , pLeft, 1, targ
21c7c 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  et);.      }else
21c7d 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72 65  {.        regFre
21c7e 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65  e1 = r1 = sqlite
21c7f 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
21c80 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
21c81 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21c82 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
21c83 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32   r1);.        r2
21c84 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
21c85 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
21c86 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
21c87 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20  gFree2);.       
21c88 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21c89 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63  p3(v, OP_Subtrac
21c8a 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65  t, r2, r1, targe
21c8b 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  t);.        test
21c8c 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
21c8d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  0 );.      }.   
21c8e 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65     inReg = targe
21c8f 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  t;.      break;.
21c90 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21c91 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61  K_BITNOT:.    ca
21c92 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20  se TK_NOT: {.   
21c93 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49     assert( TK_BI
21c94 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20  TNOT==OP_BitNot 
21c95 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21c96 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20   TK_NOT==OP_Not 
21c97 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21c98 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54  e( op==TK_BITNOT
21c99 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c9a 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29  se( op==TK_NOT )
21c9b 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
21c9c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21c9d 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21c9e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
21c9f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21ca0 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
21ca1 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20  ;.      inReg = 
21ca2 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71  target;.      sq
21ca3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21ca4 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67  v, op, r1, inReg
21ca5 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21ca6 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21ca7 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61  K_ISNULL:.    ca
21ca8 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b  se TK_NOTNULL: {
21ca9 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
21caa 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21cab 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e  K_ISNULL==OP_IsN
21cac 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ull );.      ass
21cad 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d  ert( TK_NOTNULL=
21cae 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20  =OP_NotNull );. 
21caf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21cb0 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a  p==TK_ISNULL );.
21cb1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21cb2 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29  op==TK_NOTNULL )
21cb3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21cb4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21cb5 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67  Integer, 1, targ
21cb6 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  et);.      r1 = 
21cb7 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21cb8 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
21cb9 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
21cba 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee1);.      test
21cbb 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
21cbc 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20  0 );.      addr 
21cbd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21cbe 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a  Op1(v, op, r1);.
21cbf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21cc0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
21cc1 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31  dImm, target, -1
21cc2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21cc3 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
21cc4 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65  addr);.      bre
21cc5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
21cc6 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
21cc7 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49  ON: {.      AggI
21cc8 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78  nfo *pInfo = pEx
21cc9 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20  pr->pAggInfo;.  
21cca 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30      if( pInfo==0
21ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
21ccc 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
21ccd 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
21cce 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
21ccf 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
21cd0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73  Msg(pParse, "mis
21cd1 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65  use of aggregate
21cd2 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d 3e  : %s()", pExpr->
21cd3 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
21cd4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21cd5 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61  inReg = pInfo->a
21cd6 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67  Func[pExpr->iAgg
21cd7 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a  ].iMem;.      }.
21cd8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21cd9 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   }.    case TK_C
21cda 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63  ONST_FUNC:.    c
21cdb 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a  ase TK_FUNCTION:
21cdc 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73   {.      ExprLis
21cdd 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20 20  t *pFarg;       
21cde 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 74  /* List of funct
21cdf 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f  ion arguments */
21ce0 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72 67  .      int nFarg
21ce1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
21ce2 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 74   Number of funct
21ce3 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f  ion arguments */
21ce4 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a  .      FuncDef *
21ce5 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f 2a  pDef;         /*
21ce6 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65   The function de
21ce7 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20  finition object 
21ce8 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64  */.      int nId
21ce9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21cea 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
21ceb 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69   function name i
21cec 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 20  n bytes */.     
21ced 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64   const char *zId
21cee 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  ;       /* The f
21cef 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
21cf0 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d        int constM
21cf1 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ask = 0;     /* 
21cf2 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e  Mask of function
21cf3 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20   arguments that 
21cf4 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a  are constant */.
21cf5 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
21cf6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21cf7 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
21cf8 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45        u8 enc = E
21cf9 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a 20  NC(db);      /* 
21cfa 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  The text encodin
21cfb 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64  g used by this d
21cfc 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20  atabase */.     
21cfd 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
21cfe 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c  = 0;    /* A col
21cff 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
21d00 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  */..      assert
21d01 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21d02 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21d03 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
21d04 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21d05 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a  K_CONST_FUNC );.
21d06 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21d07 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op==TK_FUNCTION 
21d08 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70  );.      if( Exp
21d09 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
21d0a 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f  pExpr, EP_TokenO
21d0b 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20  nly) ){.        
21d0c 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20  pFarg = 0;.     
21d0d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21d0e 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78  pFarg = pExpr->x
21d0f 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a  .pList;.      }.
21d10 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46        nFarg = pF
21d11 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78  arg ? pFarg->nEx
21d12 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 73  pr : 0;.      as
21d13 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21d14 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21d15 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21d16 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d      zId = pExpr-
21d17 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  >u.zToken;.     
21d18 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74   nId = sqlite3St
21d19 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20  rlen30(zId);.   
21d1a 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
21d1b 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
21d1c 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72  , zId, nId, nFar
21d1d 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20  g, enc, 0);.    
21d1e 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b    if( pDef==0 ){
21d1f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21d20 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21d21 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69   "unknown functi
21d22 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64  on: %.*s()", nId
21d23 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20  , zId);.        
21d24 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
21d25 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 74        /* Attempt
21d26 20 61 20 64 69 72 65 63 74 20 69 6d 70 6c 65 6d   a direct implem
21d27 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
21d28 62 75 69 6c 74 2d 69 6e 20 43 4f 41 4c 45 53 43  built-in COALESC
21d29 45 28 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  E() and.      **
21d2a 20 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69   IFNULL() functi
21d2b 6f 6e 73 2e 20 20 54 68 69 73 20 61 76 6f 69 64  ons.  This avoid
21d2c 73 20 75 6e 6e 65 63 65 73 73 61 72 79 20 65 76  s unnecessary ev
21d2d 61 6c 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20  alation of.     
21d2e 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61   ** arguments pa
21d2f 73 74 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e  st the first non
21d30 2d 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a  -NULL argument..
21d31 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
21d32 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  f( pDef->flags &
21d33 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41   SQLITE_FUNC_COA
21d34 4c 45 53 43 45 20 29 7b 0a 20 20 20 20 20 20 20  LESCE ){.       
21d35 20 69 6e 74 20 65 6e 64 43 6f 61 6c 65 73 63 65   int endCoalesce
21d36 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
21d37 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
21d38 20 20 20 20 61 73 73 65 72 74 28 20 6e 46 61 72      assert( nFar
21d39 67 3e 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20  g>=2 );.        
21d3a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
21d3b 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61  pParse, pFarg->a
21d3c 5b 30 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65  [0].pExpr, targe
21d3d 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  t);.        for(
21d3e 69 3d 31 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b  i=1; i<nFarg; i+
21d3f 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  +){.          sq
21d40 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21d41 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 74  v, OP_NotNull, t
21d42 61 72 67 65 74 2c 20 65 6e 64 43 6f 61 6c 65 73  arget, endCoales
21d43 63 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ce);.          s
21d44 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 52  qlite3ExprCacheR
21d45 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 74 61  emove(pParse, ta
21d46 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 20  rget);.         
21d47 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21d48 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20  ePush(pParse);. 
21d49 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21d4a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
21d4b 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78   pFarg->a[i].pEx
21d4c 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  pr, target);.   
21d4d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21d4e 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
21d4f 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  e, 1);.        }
21d50 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21d51 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
21d52 28 76 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29  (v, endCoalesce)
21d53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
21d54 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20  .      }...     
21d55 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20   if( pFarg ){.  
21d56 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
21d57 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
21d58 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20  Parse, nFarg);. 
21d59 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21d5a 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72  prCachePush(pPar
21d5b 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b  se);     /* Tick
21d5c 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20  et 2ea2425d34be 
21d5d 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
21d5e 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
21d5f 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67  st(pParse, pFarg
21d60 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  , r1, 1);.      
21d61 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21d62 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
21d63 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65  ;   /* Ticket 2e
21d64 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20  a2425d34be */.  
21d65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21d66 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 20     r1 = 0;.     
21d67 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
21d68 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
21d69 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73  BLE.      /* Pos
21d6a 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74  sibly overload t
21d6b 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74  he function if t
21d6c 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
21d6d 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 20  t is.      ** a 
21d6e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f  virtual table co
21d6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  lumn..      **. 
21d70 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69       ** For infi
21d71 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b  x functions (LIK
21d72 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c  E, GLOB, REGEXP,
21d73 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 20   and MATCH) use 
21d74 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63  the.      ** sec
21d75 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f  ond argument, no
21d76 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 20  t the first, as 
21d77 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
21d78 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  test to.      **
21d79 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 20   see if it is a 
21d7a 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74  column in a virt
21d7b 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
21d7c 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 65   is done because
21d7d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65  .      ** the le
21d7e 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e  ft operand of in
21d7f 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74  fix functions (t
21d80 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61  he operand we wa
21d81 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63  nt to.      ** c
21d82 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69  ontrol overloadi
21d83 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74  ng) ends up as t
21d84 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
21d85 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20  nt to the.      
21d86 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  ** function.  Th
21d87 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20  e expression "A 
21d88 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76  glob B" is equiv
21d89 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20  alent to .      
21d8a 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20  ** "glob(B,A).  
21d8b 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74  We want to use t
21d8c 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 20  he A in "A glob 
21d8d 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 20  B" to test.     
21d8e 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e   ** for function
21d8f 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42   overloading.  B
21d90 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 20  ut we use the B 
21d91 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c  term in "glob(B,
21d92 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  A)"..      */.  
21d93 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d 32      if( nFarg>=2
21d94 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 67   && (pExpr->flag
21d95 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  s & EP_InfixFunc
21d96 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  ) ){.        pDe
21d97 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f  f = sqlite3VtabO
21d98 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
21d99 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c  db, pDef, nFarg,
21d9a 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78   pFarg->a[1].pEx
21d9b 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  pr);.      }else
21d9c 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a   if( nFarg>0 ){.
21d9d 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
21d9e 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
21d9f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70  adFunction(db, p
21da0 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72  Def, nFarg, pFar
21da1 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a  g->a[0].pExpr);.
21da2 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
21da3 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
21da4 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  nFarg; i++){.   
21da5 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26 26       if( i<32 &&
21da6 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
21da7 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b  nstant(pFarg->a[
21da8 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  i].pExpr) ){.   
21da9 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b         constMask
21daa 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20   |= (1<<i);.    
21dab 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
21dac 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  ( (pDef->flags &
21dad 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
21dae 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43  DCOLL)!=0 && !pC
21daf 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  oll ){.         
21db0 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
21db1 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
21db2 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e  se, pFarg->a[i].
21db3 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
21db4 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
21db5 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20  if( pDef->flags 
21db6 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
21db7 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20  EDCOLL ){.      
21db8 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 70    if( !pColl ) p
21db9 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
21dba 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 73  Coll; .        s
21dbb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
21dbc 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20  (v, OP_CollSeq, 
21dbd 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a  0, 0, 0, (char *
21dbe 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53  )pColl, P4_COLLS
21dbf 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  EQ);.      }.   
21dc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21dc1 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74  dOp4(v, OP_Funct
21dc2 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20  ion, constMask, 
21dc3 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20  r1, target,.    
21dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21dc5 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 2c      (char*)pDef,
21dc6 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20   P4_FUNCDEF);.  
21dc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
21dc8 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e  hangeP5(v, (u8)n
21dc9 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66 28  Farg);.      if(
21dca 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20   nFarg ){.      
21dcb 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21dcc 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
21dcd 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20  , r1, nFarg);.  
21dce 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
21dcf 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
21dd0 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
21dd1 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20  e, r1, nFarg);. 
21dd2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21dd3 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
21dd4 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
21dd5 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54     case TK_EXIST
21dd6 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  S:.    case TK_S
21dd7 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74  ELECT: {.      t
21dd8 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21dd9 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20  EXISTS );.      
21dda 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21ddb 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20  _SELECT );.     
21ddc 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
21ddd 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50  CodeSubselect(pP
21dde 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20  arse, pExpr, 0, 
21ddf 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
21de0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21de1 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69  TK_IN: {.      i
21de2 6e 74 20 64 65 73 74 49 66 46 61 6c 73 65 20 3d  nt destIfFalse =
21de3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
21de4 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
21de5 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d  int destIfNull =
21de6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
21de7 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
21de8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21de9 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
21dea 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
21deb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49  sqlite3ExprCodeI
21dec 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  N(pParse, pExpr,
21ded 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64 65   destIfFalse, de
21dee 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  stIfNull);.     
21def 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21df0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
21df1 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
21df2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
21df3 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64  esolveLabel(v, d
21df4 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20  estIfFalse);.   
21df5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21df6 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
21df7 6d 2c 20 74 61 72 67 65 74 2c 20 30 29 3b 0a 20  m, target, 0);. 
21df8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21df9 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
21dfa 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  destIfNull);.   
21dfb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21dfc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
21dfd 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
21dfe 2f 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  /...    /*.    *
21dff 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
21e00 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20   AND z.    **.  
21e01 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75    ** This is equ
21e02 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a  ivalent to.    *
21e03 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79  *.    **    x>=y
21e04 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a   AND x<=z.    **
21e05 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f  .    ** X is sto
21e06 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
21e07 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73  eft..    ** Y is
21e08 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
21e09 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  ->pList->a[0].pE
21e0a 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73  xpr..    ** Z is
21e0b 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
21e0c 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ->pList->a[1].pE
21e0d 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  xpr..    */.    
21e0e 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
21e0f 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70   {.      Expr *p
21e10 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
21e11 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63  eft;.      struc
21e12 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
21e13 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d  *pLItem = pExpr-
21e14 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20  >x.pList->a;.   
21e15 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
21e16 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b  = pLItem->pExpr;
21e17 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ..      codeComp
21e18 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72  areOperands(pPar
21e19 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20  se, pLeft, &r1, 
21e1a 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
21e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
21e1d 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
21e1e 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee2);.      test
21e1f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
21e20 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
21e21 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
21e22 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73   );.      r3 = s
21e23 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
21e24 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
21e25 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r4 = sqlite3GetT
21e26 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
21e27 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
21e28 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c  e(pParse, pLeft,
21e29 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a   pRight, OP_Ge,.
21e2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e2b 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51    r1, r2, r3, SQ
21e2c 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20  LITE_STOREP2);. 
21e2d 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20       pLItem++;. 
21e2e 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c       pRight = pL
21e2f 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Item->pExpr;.   
21e30 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
21e31 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
21e32 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20   regFree2);.    
21e33 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
21e34 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
21e35 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46  e, pRight, &regF
21e36 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73  ree2);.      tes
21e37 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21e38 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  =0 );.      code
21e39 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
21e3a 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f  pLeft, pRight, O
21e3b 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34  P_Le, r1, r2, r4
21e3c 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
21e3d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21e3e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21e3f 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61  _And, r3, r4, ta
21e40 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  rget);.      sql
21e41 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21e42 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a  eg(pParse, r3);.
21e43 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
21e44 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
21e45 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62  se, r4);.      b
21e46 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21e47 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b  case TK_UPLUS: {
21e48 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73  .      inReg = s
21e49 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21e4a 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21e4b 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
21e4c 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
21e4d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
21e4e 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20   TK_TRIGGER: {. 
21e4f 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f       /* If the o
21e50 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49 47  pcode is TK_TRIG
21e51 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65 78  GER, then the ex
21e52 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72 65  pression is a re
21e53 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a  ference.      **
21e54 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   to a column in 
21e55 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64  the new.* or old
21e56 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73  .* pseudo-tables
21e57 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20   available to.  
21e58 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 70      ** trigger p
21e59 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69 73  rograms. In this
21e5a 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62 6c   case Expr.iTabl
21e5b 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66 6f  e is set to 1 fo
21e5c 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e  r the.      ** n
21e5d 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c  ew.* pseudo-tabl
21e5e 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65 20  e, or 0 for the 
21e5f 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  old.* pseudo-tab
21e60 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  le. Expr.iColumn
21e61 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74  .      ** is set
21e62 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   to the column o
21e63 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62  f the pseudo-tab
21e64 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20 74  le to read, or t
21e65 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a 2a  o -1 to.      **
21e66 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64 20   read the rowid 
21e67 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a 0a  field..      **.
21e68 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78 70        ** The exp
21e69 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c 65  ression is imple
21e6a 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e 20  mented using an 
21e6b 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65 2e  OP_Param opcode.
21e6c 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a 2a   The p1.      **
21e6d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73 65   parameter is se
21e6e 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c  t to 0 for an ol
21e6f 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63  d.rowid referenc
21e70 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a 20  e, or to (i+1). 
21e71 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 72       ** to refer
21e72 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c  ence another col
21e73 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a  umn of the old.*
21e74 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77   pseudo-table, w
21e75 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69  here .      ** i
21e76 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
21e77 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72   the column. For
21e78 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 66   a new.rowid ref
21e79 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20 20  erence, p1 is.  
21e7a 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e      ** set to (n
21e7b 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 20  +1), where n is 
21e7c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
21e7d 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 73  lumns in each ps
21e7e 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 20  eudo-table..    
21e7f 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65 72    ** For a refer
21e80 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68 65  ence to any othe
21e81 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  r column in the 
21e82 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  new.* pseudo-tab
21e83 6c 65 2c 20 70 31 0a 20 20 20 20 20 20 2a 2a 20  le, p1.      ** 
21e84 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32 2b 69  is set to (n+2+i
21e85 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64 20 69  ), where n and i
21e86 20 61 72 65 20 61 73 20 64 65 66 69 6e 65 64 20   are as defined 
21e87 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f 72 0a  previously. For.
21e88 20 20 20 20 20 20 2a 2a 20 65 78 61 6d 70 6c 65        ** example
21e89 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f  , if the table o
21e8a 6e 20 77 68 69 63 68 20 74 72 69 67 67 65 72 73  n which triggers
21e8b 20 61 72 65 20 62 65 69 6e 67 20 66 69 72 65 64   are being fired
21e8c 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 63   is.      ** dec
21e8d 6c 61 72 65 64 20 61 73 3a 0a 20 20 20 20 20 20  lared as:.      
21e8e 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 43 52  **.      **   CR
21e8f 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
21e90 20 62 29 3b 0a 20 20 20 20 20 20 2a 2a 0a 20 20   b);.      **.  
21e91 20 20 20 20 2a 2a 20 54 68 65 6e 20 70 31 20 69      ** Then p1 i
21e92 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
21e93 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20   follows:.      
21e94 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31  **.      **   p1
21e95 3d 3d 30 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e  ==0   ->    old.
21e96 72 6f 77 69 64 20 20 20 20 20 70 31 3d 3d 33 20  rowid     p1==3 
21e97 20 20 2d 3e 20 20 20 20 6e 65 77 2e 72 6f 77 69    ->    new.rowi
21e98 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d  d.      **   p1=
21e99 3d 31 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 61  =1   ->    old.a
21e9a 20 20 20 20 20 20 20 20 20 70 31 3d 3d 34 20 20           p1==4  
21e9b 20 2d 3e 20 20 20 20 6e 65 77 2e 61 0a 20 20 20   ->    new.a.   
21e9c 20 20 20 2a 2a 20 20 20 70 31 3d 3d 32 20 20 20     **   p1==2   
21e9d 2d 3e 20 20 20 20 6f 6c 64 2e 62 20 20 20 20 20  ->    old.b     
21e9e 20 20 20 20 70 31 3d 3d 35 20 20 20 2d 3e 20 20      p1==5   ->  
21e9f 20 20 6e 65 77 2e 62 20 20 20 20 20 20 20 0a 20    new.b       . 
21ea0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61       */.      Ta
21ea1 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 45 78 70  ble *pTab = pExp
21ea2 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 69  r->pTab;.      i
21ea3 6e 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e 69  nt p1 = pExpr->i
21ea4 54 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e 6e  Table * (pTab->n
21ea5 43 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45 78  Col+1) + 1 + pEx
21ea6 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20  pr->iColumn;..  
21ea7 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
21ea8 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c 20  r->iTable==0 || 
21ea9 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 31  pExpr->iTable==1
21eaa 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21eab 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
21eac 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d 3e 69  >=-1 && pExpr->i
21ead 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f  Column<pTab->nCo
21eae 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
21eaf 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30  t( pTab->iPKey<0
21eb0 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75   || pExpr->iColu
21eb1 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  mn!=pTab->iPKey 
21eb2 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21eb3 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 28 70 54   p1>=0 && p1<(pT
21eb4 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b  ab->nCol*2+2) );
21eb5 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
21eb6 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21eb7 50 61 72 61 6d 2c 20 70 31 2c 20 74 61 72 67 65  Param, p1, targe
21eb8 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  t);.      VdbeCo
21eb9 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73  mment((v, "%s.%s
21eba 20 2d 3e 20 24 25 64 22 2c 0a 20 20 20 20 20 20   -> $%d",.      
21ebb 20 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65    (pExpr->iTable
21ebc 20 3f 20 22 6e 65 77 22 20 3a 20 22 6f 6c 64 22   ? "new" : "old"
21ebd 29 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78 70  ),.        (pExp
21ebe 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22  r->iColumn<0 ? "
21ebf 72 6f 77 69 64 22 20 3a 20 70 45 78 70 72 2d 3e  rowid" : pExpr->
21ec0 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  pTab->aCol[pExpr
21ec1 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65  ->iColumn].zName
21ec2 29 2c 0a 20 20 20 20 20 20 20 20 74 61 72 67 65  ),.        targe
21ec3 74 0a 20 20 20 20 20 20 29 29 3b 0a 0a 20 20 20  t.      ));..   
21ec4 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
21ec5 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66 66  umn has REAL aff
21ec6 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63 75  inity, it may cu
21ec7 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72 65  rrently be store
21ec8 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 2a 2a  d as an.      **
21ec9 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20 4f 50   integer. Use OP
21eca 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 74 6f  _RealAffinity to
21ecb 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
21ecc 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20 20 2a   really real.  *
21ecd 2f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  /.      if( pExp
21ece 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20  r->iColumn>=0 . 
21ecf 20 20 20 20 20 20 26 26 20 70 54 61 62 2d 3e 61        && pTab->a
21ed0 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75  Col[pExpr->iColu
21ed1 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51  mn].affinity==SQ
21ed2 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20  LITE_AFF_REAL.  
21ed3 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73      ){.        s
21ed4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
21ed5 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  (v, OP_RealAffin
21ed6 69 74 79 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  ity, target);.  
21ed7 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21ed8 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f  k;.    }...    /
21ed9 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a  *.    ** Form A:
21eda 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 78  .    **   CASE x
21edb 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31   WHEN e1 THEN r1
21edc 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32   WHEN e2 THEN r2
21edd 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45   ... WHEN eN THE
21ede 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a  N rN ELSE y END.
21edf 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
21ee0 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43  rm B:.    **   C
21ee1 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 4e  ASE WHEN e1 THEN
21ee2 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e   r1 WHEN e2 THEN
21ee3 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20   r2 ... WHEN eN 
21ee4 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45  THEN rN ELSE y E
21ee5 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ND.    **.    **
21ee6 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 62   Form A is can b
21ee7 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e  e transformed in
21ee8 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e  to the equivalen
21ee9 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c  t form B as foll
21eea 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41  ows:.    **   CA
21eeb 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 45  SE WHEN x=e1 THE
21eec 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 54  N r1 WHEN x=e2 T
21eed 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a  HEN r2 ....    *
21eee 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 3d  *        WHEN x=
21eef 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20  eN THEN rN ELSE 
21ef0 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20  y END.    **.   
21ef1 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 69   ** X (if it exi
21ef2 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 72  sts) is in pExpr
21ef3 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20  ->pLeft..    ** 
21ef4 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70  Y is in pExpr->p
21ef5 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 73  Right.  The Y is
21ef6 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20   also optional. 
21ef7 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a   If there is no.
21ef8 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75      ** ELSE clau
21ef9 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20  se and no other 
21efa 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 68  term matches, th
21efb 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
21efc 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72   the.    ** expr
21efd 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20  ssion is NULL.. 
21efe 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 70     ** Ei is in p
21eff 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69  Expr->pList->a[i
21f00 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 45  *2] and Ri is pE
21f01 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a  xpr->pList->a[i*
21f02 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  2+1]..    **.   
21f03 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f   ** The result o
21f04 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
21f05 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 74   is the Ri for t
21f06 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e  he first matchin
21f07 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20  g Ei,.    ** or 
21f08 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
21f09 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 20  atching Ei, the 
21f0a 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 20  ELSE term Y, or 
21f0b 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 20  if there is.    
21f0c 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c  ** no ELSE term,
21f0d 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20   NULL..    */.  
21f0e 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72    default: asser
21f0f 74 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 29  t( op==TK_CASE )
21f10 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e  ; {.      int en
21f11 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20  dLabel;         
21f12 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
21f13 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e  OTO label for en
21f14 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a  d of CASE stmt *
21f15 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74  /.      int next
21f16 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  Case;           
21f17 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54            /* GOT
21f18 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74  O label for next
21f19 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a   WHEN clause */.
21f1a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b        int nExpr;
21f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f1c 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75          /* 2x nu
21f1d 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72  mber of WHEN ter
21f1e 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ms */.      int 
21f1f 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
21f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21f21 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
21f22 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
21f23 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20  *pEList;        
21f24 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
21f25 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a   of WHEN terms *
21f26 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  /.      struct E
21f27 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c  xprList_item *aL
21f28 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72  istelem;  /* Arr
21f29 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73  ay of WHEN terms
21f2a 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f   */.      Expr o
21f2b 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20  pCompare;       
21f2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
21f2d 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73  he X==Ei express
21f2e 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  ion */.      Exp
21f2f 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 20  r cacheX;       
21f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21f31 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73 73  * Cached express
21f32 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45  ion X */.      E
21f33 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20  xpr *pX;        
21f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f35 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 73   /* The X expres
21f36 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78  sion */.      Ex
21f37 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20  pr *pTest = 0;  
21f38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f39 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41  /* X==Ei (form A
21f3a 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f  ) or just Ei (fo
21f3b 72 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56  rm B) */.      V
21f3c 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61  VA_ONLY( int iCa
21f3d 63 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73  cheLevel = pPars
21f3e 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20  e->iCacheLevel; 
21f3f 29 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  )..      assert(
21f40 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
21f41 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
21f42 65 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 2d  elect) && pExpr-
21f43 3e 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20  >x.pList );.    
21f44 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d    assert((pExpr-
21f45 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  >x.pList->nExpr 
21f46 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20  % 2) == 0);.    
21f47 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e    assert(pExpr->
21f48 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e  x.pList->nExpr >
21f49 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73   0);.      pELis
21f4a 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
21f4b 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65  st;.      aListe
21f4c 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b  lem = pEList->a;
21f4d 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70  .      nExpr = p
21f4e 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
21f4f 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73      endLabel = s
21f50 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
21f51 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66  bel(v);.      if
21f52 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70  ( (pX = pExpr->p
21f53 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Left)!=0 ){.    
21f54 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58      cacheX = *pX
21f55 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
21f56 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43  se( pX->op==TK_C
21f57 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20  OLUMN );.       
21f58 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f   testcase( pX->o
21f59 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29  p==TK_REGISTER )
21f5a 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58  ;.        cacheX
21f5b 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65  .iTable = sqlite
21f5c 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
21f5d 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46 72  arse, pX, &regFr
21f5e 65 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74 65  ee1);.        te
21f5f 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
21f60 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 63  ==0 );.        c
21f61 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45  acheX.op = TK_RE
21f62 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20  GISTER;.        
21f63 6f 70 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54  opCompare.op = T
21f64 4b 5f 45 51 3b 0a 20 20 20 20 20 20 20 20 6f 70  K_EQ;.        op
21f65 43 6f 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d 20  Compare.pLeft = 
21f66 26 63 61 63 68 65 58 3b 0a 20 20 20 20 20 20 20  &cacheX;.       
21f67 20 70 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70   pTest = &opComp
21f68 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  are;.      }.   
21f69 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45     for(i=0; i<nE
21f6a 78 70 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20  xpr; i=i+2){.   
21f6b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21f6c 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
21f6d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
21f6e 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  X ){.          a
21f6f 73 73 65 72 74 28 20 70 54 65 73 74 21 3d 30 20  ssert( pTest!=0 
21f70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 70 43  );.          opC
21f71 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 3d 20  ompare.pRight = 
21f72 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78  aListelem[i].pEx
21f73 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  pr;.        }els
21f74 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  e{.          pTe
21f75 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69  st = aListelem[i
21f76 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
21f77 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 74 43   }.        nextC
21f78 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ase = sqlite3Vdb
21f79 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
21f7a 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
21f7b 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43   pTest->op==TK_C
21f7c 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20  OLUMN );.       
21f7d 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
21f7e 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 73  lse(pParse, pTes
21f7f 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51 4c  t, nextCase, SQL
21f80 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
21f81 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
21f82 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31  e( aListelem[i+1
21f83 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ].pExpr->op==TK_
21f84 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
21f85 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69 73    testcase( aLis
21f86 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72  telem[i+1].pExpr
21f87 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ->op==TK_REGISTE
21f88 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  R );.        sql
21f89 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
21f8a 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b 69  rse, aListelem[i
21f8b 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65  +1].pExpr, targe
21f8c 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
21f8d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21f8e 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64   OP_Goto, 0, end
21f8f 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 20 20  Label);.        
21f90 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21f91 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a  Pop(pParse, 1);.
21f92 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21f93 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
21f94 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a 20 20  v, nextCase);.  
21f95 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
21f96 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b  pExpr->pRight ){
21f97 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21f98 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
21f99 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
21f9a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
21f9b 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
21f9c 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20  ight, target);. 
21f9d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
21f9e 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
21f9f 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c  e, 1);.      }el
21fa0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
21fa1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21fa2 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72   OP_Null, 0, tar
21fa3 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  get);.      }.  
21fa4 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
21fa5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
21fa6 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 0a  pParse->nErr>0 .
21fa7 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50             || pP
21fa8 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
21fa9 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 29  l==iCacheLevel )
21faa 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21fab 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
21fac 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20  v, endLabel);.  
21fad 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21fae 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21faf 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
21fb0 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20   case TK_RAISE: 
21fb1 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
21fb2 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d  pExpr->affinity=
21fb3 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20 20  =OE_Rollback .  
21fb4 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70           || pExp
21fb5 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
21fb6 41 62 6f 72 74 0a 20 20 20 20 20 20 20 20 20 20  Abort.          
21fb7 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e   || pExpr->affin
21fb8 69 74 79 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20  ity==OE_Fail.   
21fb9 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72          || pExpr
21fba 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49  ->affinity==OE_I
21fbb 67 6e 6f 72 65 0a 20 20 20 20 20 20 29 3b 0a 20  gnore.      );. 
21fbc 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65       if( !pParse
21fbd 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b  ->pTriggerTab ){
21fbe 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21fbf 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21fc0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21fc1 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29          "RAISE()
21fc2 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65   may only be use
21fc3 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  d within a trigg
21fc4 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20  er-program");.  
21fc5 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
21fc6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21fc7 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  ( pExpr->affinit
21fc8 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20  y==OE_Abort ){. 
21fc9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61         sqlite3Ma
21fca 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a  yAbort(pParse);.
21fcb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
21fcc 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21fcd 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21fce 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21fcf 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 61      if( pExpr->a
21fd0 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f  ffinity==OE_Igno
21fd1 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  re ){.        sq
21fd2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
21fd3 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2c 20  .            v, 
21fd4 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f  OP_Halt, SQLITE_
21fd5 4f 4b 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 30  OK, OE_Ignore, 0
21fd6 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
21fd7 6e 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  n,0);.      }els
21fd8 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
21fd9 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
21fda 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21fdb 61 66 66 69 6e 69 74 79 2c 20 70 45 78 70 72 2d  affinity, pExpr-
21fdc 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20  >u.zToken, 0);. 
21fdd 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 62 72       }..      br
21fde 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
21fdf 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  f.  }.  sqlite3R
21fe0 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21fe1 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b  arse, regFree1);
21fe2 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
21fe3 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
21fe4 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65   regFree2);.  re
21fe5 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f  turn inReg;.}../
21fe6 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
21fe7 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61  de to evaluate a
21fe8 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  n expression and
21fe9 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
21fea 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67  ts.** into a reg
21feb 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74  ister.  Return t
21fec 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
21fed 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73  er where the res
21fee 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72  ults.** are stor
21fef 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
21ff0 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74   register is a t
21ff1 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
21ff2 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65  r that can be de
21ff3 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68  allocated,.** th
21ff4 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d  en write its num
21ff5 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20  ber into *pReg. 
21ff6 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72   If the result r
21ff7 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a  egister is not.*
21ff8 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74  * a temporary, t
21ff9 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f  hen set *pReg to
21ffa 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45   zero..*/.SQLITE
21ffb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
21ffc 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21ffd 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21ffe 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
21fff 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72   *pReg){.  int r
22000 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
22001 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
22002 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65   int r2 = sqlite
22003 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
22004 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72  pParse, pExpr, r
22005 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31  1);.  if( r2==r1
22006 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20   ){.    *pReg = 
22007 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r1;.  }else{.   
22008 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
22009 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2200a 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20  1);.    *pReg = 
2200b 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2200c 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  r2;.}../*.** Gen
2200d 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
2200e 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78  will evaluate ex
2200f 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61  pression pExpr a
22010 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20  nd store the.** 
22011 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73  results in regis
22012 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65  ter target.  The
22013 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61   results are gua
22014 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61  ranteed to appea
22015 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  r.** in register
22016 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49   target..*/.SQLI
22017 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
22018 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50  qlite3ExprCode(P
22019 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
2201a 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74  pr *pExpr, int t
2201b 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e  arget){.  int in
2201c 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Reg;..  assert( 
2201d 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67  target>0 && targ
2201e 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  et<=pParse->nMem
2201f 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71   );.  inReg = sq
22020 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
22021 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
22022 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73  r, target);.  as
22023 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56  sert( pParse->pV
22024 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  dbe || pParse->d
22025 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22026 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d  );.  if( inReg!=
22027 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65  target && pParse
22028 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73  ->pVdbe ){.    s
22029 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2202a 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
2202b 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c  OP_SCopy, inReg,
2202c 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20   target);.  }.  
2202d 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d  return target;.}
2202e 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2202f 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75   code that evalu
22030 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78  tes the given ex
22031 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74  pression and put
22032 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
22033 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67  in register targ
22034 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d  et..**.** Also m
22035 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
22036 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73  e expression res
22037 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65  ults into anothe
22038 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74  r "cache" regist
22039 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79  er.** and modify
2203a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2203b 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  so that the next
2203c 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c   time it is eval
2203d 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65  uated,.** the re
2203e 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f  sult is a copy o
2203f 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69  f the cache regi
22040 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ster..**.** This
22041 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
22042 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
22043 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d   that are used m
22044 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65  ultiple .** time
22045 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61  s.  They are eva
22046 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20  luated once and 
22047 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
22048 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
22049 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a   are reused..*/.
2204a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2204b 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
2204c 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65  deAndCache(Parse
2204d 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2204e 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
2204f 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  t){.  Vdbe *v = 
22050 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
22051 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e   int inReg;.  in
22052 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
22053 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
22054 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
22055 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
22056 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f   );.  /* This ro
22057 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
22058 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 49 4e 53  for terms to INS
22059 45 52 54 20 6f 72 20 55 50 44 41 54 45 2e 20 20  ERT or UPDATE.  
2205a 41 6e 64 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a  And the only.  *
2205b 2a 20 6f 74 68 65 72 20 70 6c 61 63 65 20 77 68  * other place wh
2205c 65 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ere expressions 
2205d 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64  can be converted
2205e 20 69 6e 74 6f 20 54 4b 5f 52 45 47 49 53 54 45   into TK_REGISTE
2205f 52 20 69 73 0a 20 20 2a 2a 20 69 6e 20 57 48 45  R is.  ** in WHE
22060 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73  RE clause proces
22061 73 69 6e 67 2e 20 20 53 6f 20 61 73 20 63 75 72  sing.  So as cur
22062 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e 74  rently implement
22063 65 64 2c 20 74 68 65 72 65 20 69 73 0a 20 20 2a  ed, there is.  *
22064 2a 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 54  * no way for a T
22065 4b 5f 52 45 47 49 53 54 45 52 20 74 6f 20 65 78  K_REGISTER to ex
22066 69 73 74 20 68 65 72 65 2e 20 20 42 75 74 20 69  ist here.  But i
22067 74 20 73 65 65 6d 73 20 70 72 75 64 65 6e 74 20  t seems prudent 
22068 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 74 68 65  to.  ** keep the
22069 20 41 4c 57 41 59 53 28 29 20 69 6e 20 63 61 73   ALWAYS() in cas
2206a 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73  e the conditions
2206b 20 61 62 6f 76 65 20 63 68 61 6e 67 65 20 77 69   above change wi
2206c 74 68 20 66 75 74 75 72 65 0a 20 20 2a 2a 20 6d  th future.  ** m
2206d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 6f 72 20  odifications or 
2206e 65 6e 68 61 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f  enhancements. */
2206f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 45  .  if( ALWAYS(pE
22070 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49  xpr->op!=TK_REGI
22071 53 54 45 52 29 20 29 7b 20 20 0a 20 20 20 20 69  STER) ){  .    i
22072 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 20 69 4d 65  nt iMem;.    iMe
22073 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
22074 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
22075 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22076 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65  Copy, inReg, iMe
22077 6d 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69  m);.    pExpr->i
22078 54 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20 20  Table = iMem;.  
22079 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70    pExpr->op2 = p
2207a 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45  Expr->op;.    pE
2207b 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47  xpr->op = TK_REG
2207c 49 53 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74  ISTER;.  }.  ret
2207d 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a  urn inReg;.}../*
2207e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
2207f 69 66 20 70 45 78 70 72 20 69 73 20 61 6e 20 63  if pExpr is an c
22080 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69  onstant expressi
22081 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72 6f  on that is appro
22082 70 72 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61  priate.** for fa
22083 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61  ctoring out of a
22084 20 6c 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69   loop.  Appropri
22085 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ate expressions 
22086 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  are:.**.**    * 
22087 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20   Any expression 
22088 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 74  that evaluates t
22089 6f 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70  o two or more op
2208a 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  codes..**.**    
2208b 2a 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65  *  Any OP_Intege
2208c 72 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53  r, OP_Real, OP_S
2208d 74 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20  tring, OP_Blob, 
2208e 4f 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20  OP_Null, .**    
2208f 20 20 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c     or OP_Variabl
22090 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  e that does not 
22091 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65  need to be place
22092 64 20 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20  d in a .**      
22093 20 73 70 65 63 69 66 69 63 20 72 65 67 69 73 74   specific regist
22094 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  er..**.** There 
22095 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66  is no point in f
22096 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e  actoring out sin
22097 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20  gle-instruction 
22098 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72  constant.** expr
22099 65 73 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65  essions that nee
2209a 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69  d to be placed i
2209b 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72  n a particular r
2209c 65 67 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65  egister.  .** We
2209d 20 63 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68   could factor th
2209e 65 6d 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e  em out, but then
2209f 20 77 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70   we would end up
220a0 20 61 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50   adding an.** OP
220a1 5f 53 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69  _SCopy instructi
220a2 6f 6e 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76  on to move the v
220a3 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f  alue into the co
220a4 72 72 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a  rrect register.*
220a5 2a 20 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67  * later.  We mig
220a6 68 74 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20  ht as well just 
220a7 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  use the original
220a8 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64   instruction and
220a9 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50  .** avoid the OP
220aa 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69  _SCopy..*/.stati
220ab 63 20 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69  c int isAppropri
220ac 61 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28  ateForFactoring(
220ad 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  Expr *p){.  if( 
220ae 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f  !sqlite3ExprIsCo
220af 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29  nstantNotJoin(p)
220b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
220b1 3b 20 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74  ;  /* Only const
220b2 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ant expressions 
220b3 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  are appropriate 
220b4 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f  for factoring */
220b5 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66  .  }.  if( (p->f
220b6 6c 61 67 73 20 26 20 45 50 5f 46 69 78 65 64 44  lags & EP_FixedD
220b7 65 73 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  est)==0 ){.    r
220b8 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79  eturn 1;  /* Any
220b9 20 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75   constant withou
220ba 74 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e  t a fixed destin
220bb 61 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72  ation is appropr
220bc 69 61 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68  iate */.  }.  wh
220bd 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ile( p->op==TK_U
220be 50 4c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c  PLUS ) p = p->pL
220bf 65 66 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70  eft;.  switch( p
220c0 2d 3e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20  ->op ){.#ifndef 
220c1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
220c2 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73  _LITERAL.    cas
220c3 65 20 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69  e TK_BLOB:.#endi
220c4 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41  f.    case TK_VA
220c5 52 49 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65  RIABLE:.    case
220c6 20 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20   TK_INTEGER:.   
220c7 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a   case TK_FLOAT:.
220c8 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c      case TK_NULL
220c9 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54  :.    case TK_ST
220ca 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65  RING: {.      te
220cb 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
220cc 4b 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20  K_BLOB );.      
220cd 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
220ce 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a  =TK_VARIABLE );.
220cf 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
220d0 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45  p->op==TK_INTEGE
220d1 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  R );.      testc
220d2 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46  ase( p->op==TK_F
220d3 4c 4f 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65  LOAT );.      te
220d4 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
220d5 4b 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  K_NULL );.      
220d6 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
220d7 3d 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20  =TK_STRING );.  
220d8 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e      /* Single-in
220d9 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61  struction consta
220da 6e 74 73 20 77 69 74 68 20 61 20 66 69 78 65 64  nts with a fixed
220db 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65   destination are
220dc 0a 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72  .      ** better
220dd 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20   done in-line.  
220de 49 66 20 77 65 20 66 61 63 74 6f 72 20 74 68 65  If we factor the
220df 6d 2c 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73  m, they will jus
220e0 74 20 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75  t end.      ** u
220e1 70 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20  p generating an 
220e2 4f 50 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65  OP_SCopy to move
220e3 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68   the value to th
220e4 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20  e destination.  
220e5 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e      ** register.
220e6 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
220e7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   0;.    }.    ca
220e8 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a  se TK_UMINUS: {.
220e9 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65        if( p->pLe
220ea 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54  ft->op==TK_FLOAT
220eb 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70   || p->pLeft->op
220ec 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a  ==TK_INTEGER ){.
220ed 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
220ee 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
220ef 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
220f0 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
220f1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
220f2 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
220f3 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72  ../*.** If pExpr
220f4 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65   is a constant e
220f5 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69  xpression that i
220f6 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
220f7 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f  r.** factoring o
220f8 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68  ut of a loop, th
220f9 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  en evaluate the 
220fa 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e  expression.** in
220fb 74 6f 20 61 20 72 65 67 69 73 74 65 72 20 61 6e  to a register an
220fc 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78  d convert the ex
220fd 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20  pression into a 
220fe 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65  TK_REGISTER.** e
220ff 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74  xpression..*/.st
22100 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e  atic int evalCon
22101 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70  stExpr(Walker *p
22102 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
22103 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  xpr){.  Parse *p
22104 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d  Parse = pWalker-
22105 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63  >pParse;.  switc
22106 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
22107 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 0a      case TK_IN:.
22108 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49      case TK_REGI
22109 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65  STER: {.      re
2210a 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
2210b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
2210c 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  K_FUNCTION:.    
2210d 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43  case TK_AGG_FUNC
2210e 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  TION:.    case T
2210f 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a  K_CONST_FUNC: {.
22110 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67        /* The arg
22111 75 6d 65 6e 74 73 20 74 6f 20 61 20 66 75 6e 63  uments to a func
22112 74 69 6f 6e 20 68 61 76 65 20 61 20 66 69 78 65  tion have a fixe
22113 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 20  d destination.. 
22114 20 20 20 20 20 2a 2a 20 4d 61 72 6b 20 74 68 65       ** Mark the
22115 6d 20 74 68 69 73 20 77 61 79 20 74 6f 20 61 76  m this way to av
22116 6f 69 64 20 67 65 6e 65 72 61 74 65 64 20 75 6e  oid generated un
22117 6e 65 65 64 65 64 20 4f 50 5f 53 43 6f 70 79 0a  needed OP_SCopy.
22118 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63        ** instruc
22119 74 69 6f 6e 73 2e 20 0a 20 20 20 20 20 20 2a 2f  tions. .      */
2211a 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2211b 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  *pList = pExpr->
2211c 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 61  x.pList;.      a
2211d 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2211e 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2211f 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
22120 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 20        if( pList 
22121 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
22122 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
22123 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
22124 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
22125 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 3b  Item = pList->a;
22126 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 69  .        for(; i
22127 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
22128 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
22129 20 41 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e 70   ALWAYS(pItem->p
2212a 45 78 70 72 29 20 29 20 70 49 74 65 6d 2d 3e 70  Expr) ) pItem->p
2212b 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
2212c 50 5f 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20  P_FixedDest;.   
2212d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2212e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2212f 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70  }.  }.  if( isAp
22130 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74  propriateForFact
22131 6f 72 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a  oring(pExpr) ){.
22132 20 20 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70      int r1 = ++p
22133 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
22134 20 69 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20   int r2;.    r2 
22135 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
22136 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
22137 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20  pExpr, r1);.    
22138 69 66 28 20 4e 45 56 45 52 28 72 31 21 3d 72 32  if( NEVER(r1!=r2
22139 29 20 29 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ) ) sqlite3Relea
2213a 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2213b 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78 70 72  , r1);.    pExpr
2213c 2d 3e 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f  ->op2 = pExpr->o
2213d 70 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  p;.    pExpr->op
2213e 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
2213f 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
22140 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74 75  e = r2;.    retu
22141 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
22142 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
22143 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
22144 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63 6f  * Preevaluate co
22145 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73  nstant subexpres
22146 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45 78  sions within pEx
22147 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  pr and store the
22148 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 72  .** results in r
22149 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69 66  egisters.  Modif
2214a 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20  y pExpr so that 
2214b 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62  the constant sub
2214c 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72  expresions.** ar
2214d 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f 70  e TK_REGISTER op
2214e 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65 72  codes that refer
2214f 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70 75   to the precompu
22150 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53  ted values..*/.S
22151 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22152 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
22153 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73  deConstants(Pars
22154 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
22155 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65  *pExpr){.  Walke
22156 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  r w;.  w.xExprCa
22157 6c 6c 62 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e  llback = evalCon
22158 73 74 45 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c  stExpr;.  w.xSel
22159 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b  ectCallback = 0;
2215a 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
2215b 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
2215c 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70  alkExpr(&w, pExp
2215d 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  r);.}.../*.** Ge
2215e 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2215f 20 70 75 73 68 65 73 20 74 68 65 20 76 61 6c 75   pushes the valu
22160 65 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65  e of every eleme
22161 6e 74 20 6f 66 20 74 68 65 20 67 69 76 65 6e 0a  nt of the given.
22162 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
22163 73 74 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e  st into a sequen
22164 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ce of registers 
22165 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 74 61 72  beginning at tar
22166 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  get..**.** Retur
22167 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
22168 65 6c 65 6d 65 6e 74 73 20 65 76 61 6c 75 61 74  elements evaluat
22169 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2216a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2216b 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
2216c 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
2216d 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
2216e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2216f 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
22170 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
22171 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 62 65 20  sion list to be 
22172 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74  coded */.  int t
22173 61 72 67 65 74 2c 20 20 20 20 20 20 20 20 2f 2a  arget,        /*
22174 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
22175 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
22176 20 64 6f 48 61 72 64 43 6f 70 79 20 20 20 20 20   doHardCopy     
22177 2f 2a 20 4d 61 6b 65 20 61 20 68 61 72 64 20 63  /* Make a hard c
22178 6f 70 79 20 6f 66 20 65 76 65 72 79 20 65 6c 65  opy of every ele
22179 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  ment */.){.  str
2217a 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2217b 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20  m *pItem;.  int 
2217c 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  i, n;.  assert( 
2217d 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 61 73  pList!=0 );.  as
2217e 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29  sert( target>0 )
2217f 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e  ;.  n = pList->n
22180 45 78 70 72 3b 0a 20 20 66 6f 72 28 70 49 74 65  Expr;.  for(pIte
22181 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
22182 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d   i<n; i++, pItem
22183 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74  ++){.    if( pIt
22184 65 6d 2d 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20  em->iAlias ){.  
22185 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 63      int iReg = c
22186 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  odeAlias(pParse,
22187 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20   pItem->iAlias, 
22188 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61  pItem->pExpr, ta
22189 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 56  rget+i);.      V
2218a 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
2218b 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2218c 0a 20 20 20 20 20 20 69 66 28 20 69 52 65 67 21  .      if( iReg!
2218d 3d 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20  =target+i ){.   
2218e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2218f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
22190 70 79 2c 20 69 52 65 67 2c 20 74 61 72 67 65 74  py, iReg, target
22191 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
22192 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
22193 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
22194 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78  arse, pItem->pEx
22195 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20  pr, target+i);. 
22196 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6f 48     }.    if( doH
22197 61 72 64 43 6f 70 79 20 26 26 20 21 70 50 61 72  ardCopy && !pPar
22198 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
22199 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71  iled ){.      sq
2219a 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70  lite3ExprHardCop
2219b 79 28 70 50 61 72 73 65 2c 20 74 61 72 67 65 74  y(pParse, target
2219c 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , n);.    }.  }.
2219d 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
2219e 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2219f 64 65 20 66 6f 72 20 61 20 42 45 54 57 45 45 4e  de for a BETWEEN
221a0 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a   operator..**.**
221a1 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 20      x BETWEEN y 
221a2 41 4e 44 20 7a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  AND z.**.** The 
221a3 61 62 6f 76 65 20 69 73 20 65 71 75 69 76 61 6c  above is equival
221a4 65 6e 74 20 74 6f 20 0a 2a 2a 0a 2a 2a 20 20 20  ent to .**.**   
221a5 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 2a   x>=y AND x<=z.*
221a6 2a 0a 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20  *.** Code it as 
221a7 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72  such, taking car
221a8 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d  e to do the comm
221a9 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  on subexpression
221aa 0a 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e  .** elementation
221ab 20 6f 66 20 78 2e 0a 2a 2f 0a 73 74 61 74 69 63   of x..*/.static
221ac 20 76 6f 69 64 20 65 78 70 72 43 6f 64 65 42 65   void exprCodeBe
221ad 74 77 65 65 6e 28 0a 20 20 50 61 72 73 65 20 2a  tween(.  Parse *
221ae 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61  pParse,    /* Pa
221af 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
221b0 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
221b1 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
221b2 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  pr,      /* The 
221b3 42 45 54 57 45 45 4e 20 65 78 70 72 65 73 73 69  BETWEEN expressi
221b4 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74  on */.  int dest
221b5 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d  ,         /* Jum
221b6 70 20 68 65 72 65 20 69 66 20 74 68 65 20 6a 75  p here if the ju
221b7 6d 70 20 69 73 20 74 61 6b 65 6e 20 2a 2f 0a 20  mp is taken */. 
221b8 20 69 6e 74 20 6a 75 6d 70 49 66 54 72 75 65 2c   int jumpIfTrue,
221b9 20 20 20 2f 2a 20 54 61 6b 65 20 74 68 65 20 6a     /* Take the j
221ba 75 6d 70 20 69 66 20 74 68 65 20 42 45 54 57 45  ump if the BETWE
221bb 45 4e 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20  EN is true */.  
221bc 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20  int jumpIfNull  
221bd 20 20 2f 2a 20 54 61 6b 65 20 74 68 65 20 6a 75    /* Take the ju
221be 6d 70 20 69 66 20 74 68 65 20 42 45 54 57 45 45  mp if the BETWEE
221bf 4e 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a  N is NULL */.){.
221c0 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 20    Expr exprAnd; 
221c1 20 20 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f      /* The AND o
221c2 70 65 72 61 74 6f 72 20 69 6e 20 20 78 3e 3d 79  perator in  x>=y
221c3 20 41 4e 44 20 78 3c 3d 7a 20 20 2a 2f 0a 20 20   AND x<=z  */.  
221c4 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 20 20  Expr compLeft;  
221c5 20 20 2f 2a 20 54 68 65 20 20 78 3e 3d 79 20 20    /* The  x>=y  
221c6 74 65 72 6d 20 2a 2f 0a 20 20 45 78 70 72 20 63  term */.  Expr c
221c7 6f 6d 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 54  ompRight;   /* T
221c8 68 65 20 20 78 3c 3d 7a 20 20 74 65 72 6d 20 2a  he  x<=z  term *
221c9 2f 0a 20 20 45 78 70 72 20 65 78 70 72 58 3b 20  /.  Expr exprX; 
221ca 20 20 20 20 20 20 2f 2a 20 54 68 65 20 20 78 20        /* The  x 
221cb 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a   subexpression *
221cc 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31  /.  int regFree1
221cd 20 3d 20 30 3b 20 2f 2a 20 54 65 6d 70 6f 72 61   = 0; /* Tempora
221ce 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20  ry use register 
221cf 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 21 45  */..  assert( !E
221d0 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
221d1 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
221d2 63 74 29 20 29 3b 0a 20 20 65 78 70 72 58 20 3d  ct) );.  exprX =
221d3 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a   *pExpr->pLeft;.
221d4 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54    exprAnd.op = T
221d5 4b 5f 41 4e 44 3b 0a 20 20 65 78 70 72 41 6e 64  K_AND;.  exprAnd
221d6 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65  .pLeft = &compLe
221d7 66 74 3b 0a 20 20 65 78 70 72 41 6e 64 2e 70 52  ft;.  exprAnd.pR
221d8 69 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68  ight = &compRigh
221d9 74 3b 0a 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70  t;.  compLeft.op
221da 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 63 6f 6d 70   = TK_GE;.  comp
221db 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78  Left.pLeft = &ex
221dc 70 72 58 3b 0a 20 20 63 6f 6d 70 4c 65 66 74 2e  prX;.  compLeft.
221dd 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
221de 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  x.pList->a[0].pE
221df 78 70 72 3b 0a 20 20 63 6f 6d 70 52 69 67 68 74  xpr;.  compRight
221e0 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 63  .op = TK_LE;.  c
221e1 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d  ompRight.pLeft =
221e2 20 26 65 78 70 72 58 3b 0a 20 20 63 6f 6d 70 52   &exprX;.  compR
221e3 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45  ight.pRight = pE
221e4 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b  xpr->x.pList->a[
221e5 31 5d 2e 70 45 78 70 72 3b 0a 20 20 65 78 70 72  1].pExpr;.  expr
221e6 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
221e7 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
221e8 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26  Parse, &exprX, &
221e9 72 65 67 46 72 65 65 31 29 3b 0a 20 20 65 78 70  regFree1);.  exp
221ea 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  rX.op = TK_REGIS
221eb 54 45 52 3b 0a 20 20 69 66 28 20 6a 75 6d 70 49  TER;.  if( jumpI
221ec 66 54 72 75 65 20 29 7b 0a 20 20 20 20 73 71 6c  fTrue ){.    sql
221ed 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
221ee 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e 64 2c  Parse, &exprAnd,
221ef 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
221f0 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  l);.  }else{.   
221f1 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
221f2 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65 78 70  lse(pParse, &exp
221f3 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70  rAnd, dest, jump
221f4 49 66 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73  IfNull);.  }.  s
221f5 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
221f6 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
221f7 46 72 65 65 31 29 3b 0a 0a 20 20 2f 2a 20 45 6e  Free1);..  /* En
221f8 73 75 72 65 20 61 64 65 71 75 61 74 65 20 74 65  sure adequate te
221f9 73 74 20 63 6f 76 65 72 61 67 65 20 2a 2f 0a 20  st coverage */. 
221fa 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
221fb 66 54 72 75 65 3d 3d 30 20 26 26 20 6a 75 6d 70  fTrue==0 && jump
221fc 49 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67  IfNull==0 && reg
221fd 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 74 65  Free1==0 );.  te
221fe 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72  stcase( jumpIfTr
221ff 75 65 3d 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e  ue==0 && jumpIfN
22200 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72 65  ull==0 && regFre
22201 65 31 21 3d 30 20 29 3b 0a 20 20 74 65 73 74 63  e1!=0 );.  testc
22202 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65 3d  ase( jumpIfTrue=
22203 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c  =0 && jumpIfNull
22204 21 3d 30 20 26 26 20 72 65 67 46 72 65 65 31 3d  !=0 && regFree1=
22205 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =0 );.  testcase
22206 28 20 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30 20  ( jumpIfTrue==0 
22207 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30  && jumpIfNull!=0
22208 20 26 26 20 72 65 67 46 72 65 65 31 21 3d 30 20   && regFree1!=0 
22209 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a  );.  testcase( j
2220a 75 6d 70 49 66 54 72 75 65 21 3d 30 20 26 26 20  umpIfTrue!=0 && 
2220b 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 26 26  jumpIfNull==0 &&
2220c 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
2220d 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2220e 49 66 54 72 75 65 21 3d 30 20 26 26 20 6a 75 6d  IfTrue!=0 && jum
2220f 70 49 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65  pIfNull==0 && re
22210 67 46 72 65 65 31 21 3d 30 20 29 3b 0a 20 20 74  gFree1!=0 );.  t
22211 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54  estcase( jumpIfT
22212 72 75 65 21 3d 30 20 26 26 20 6a 75 6d 70 49 66  rue!=0 && jumpIf
22213 4e 75 6c 6c 21 3d 30 20 26 26 20 72 65 67 46 72  Null!=0 && regFr
22214 65 65 31 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74  ee1==0 );.  test
22215 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65  case( jumpIfTrue
22216 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c  !=0 && jumpIfNul
22217 6c 21 3d 30 20 26 26 20 72 65 67 46 72 65 65 31  l!=0 && regFree1
22218 21 3d 30 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  !=0 );.}../*.** 
22219 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
2221a 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72  r a boolean expr
2221b 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74  ession such that
2221c 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a   a jump is made.
2221d 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20  ** to the label 
2221e 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65 78  "dest" if the ex
2221f 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65  pression is true
22220 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a   but execution.*
22221 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61  * continues stra
22222 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68 65  ight thru if the
22223 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66   expression is f
22224 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  alse..**.** If t
22225 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76  he expression ev
22226 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20  aluates to NULL 
22227 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f  (neither true no
22228 72 20 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a  r false), then.*
22229 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20  * take the jump 
2222a 69 66 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c  if the jumpIfNul
2222b 6c 20 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45  l flag is SQLITE
2222c 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a  _JUMPIFNULL..**.
2222d 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 64 65 70  ** This code dep
2222e 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74  ends on the fact
2222f 20 74 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f   that certain to
22230 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20  ken values (ex: 
22231 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68  TK_EQ).** are th
22232 65 20 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65  e same as opcode
22233 20 76 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f   values (ex: OP_
22234 45 71 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  Eq) that impleme
22235 6e 74 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  nt the correspon
22236 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f  ding.** operatio
22237 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d  n.  Special comm
22238 65 6e 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61  ents in vdbe.c a
22239 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68  nd the mkopcodeh
2223a 2e 61 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a  .awk script in.*
2223b 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65  * the make proce
2223c 73 73 20 63 61 75 73 65 20 74 68 65 73 65 20 76  ss cause these v
2223d 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20  alues to align. 
2223e 20 41 73 73 65 72 74 28 29 73 20 69 6e 20 74 68   Assert()s in th
2223f 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20  e code.** below 
22240 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  verify that the 
22241 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67  numbers are alig
22242 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a  ned correctly..*
22243 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22244 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
22245 72 49 66 54 72 75 65 28 50 61 72 73 65 20 2a 70  rIfTrue(Parse *p
22246 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
22247 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e  pr, int dest, in
22248 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20  t jumpIfNull){. 
22249 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2224a 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2224b 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  op = 0;.  int re
2224c 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e  gFree1 = 0;.  in
2224d 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a  t regFree2 = 0;.
2224e 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20    int r1, r2;.. 
2224f 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e   assert( jumpIfN
22250 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50  ull==SQLITE_JUMP
22251 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66  IFNULL || jumpIf
22252 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28  Null==0 );.  if(
22253 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 20   NEVER(v==0) )  
22254 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45     return;  /* E
22255 78 69 73 74 61 6e 63 65 20 6f 66 20 56 44 42 45  xistance of VDBE
22256 20 63 68 65 63 6b 65 64 20 62 79 20 63 61 6c 6c   checked by call
22257 65 72 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  er */.  if( NEVE
22258 52 28 70 45 78 70 72 3d 3d 30 29 20 29 20 72 65  R(pExpr==0) ) re
22259 74 75 72 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61 79  turn;  /* No way
2225a 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   this can happen
2225b 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 45 78 70 72   */.  op = pExpr
2225c 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20  ->op;.  switch( 
2225d 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2225e 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69  K_AND: {.      i
2225f 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56  nt d2 = sqlite3V
22260 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
22261 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22262 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
22263 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
22264 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
22265 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
22266 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
22267 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
22268 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75  eft, d2,jumpIfNu
22269 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  ll^SQLITE_JUMPIF
2226a 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c  NULL);.      sql
2226b 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
2226c 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
2226d 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
2226e 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
2226f 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
22270 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20  eLabel(v, d2);. 
22271 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
22272 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
22273 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
22274 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
22275 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20   TK_OR: {.      
22276 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
22277 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
22278 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
22279 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
2227a 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
2227b 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
2227c 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
2227d 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
2227e 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
2227f 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
22280 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
22281 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
22282 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
22283 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
22284 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
22285 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
22286 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
22287 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ft, dest, jumpIf
22288 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
22289 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2228a 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61  se TK_LT:.    ca
2228b 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61  se TK_LE:.    ca
2228c 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61  se TK_GT:.    ca
2228d 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61  se TK_GE:.    ca
2228e 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61  se TK_NE:.    ca
2228f 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20  se TK_EQ: {.    
22290 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
22291 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20  =OP_Lt );.      
22292 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f  assert( TK_LE==O
22293 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Le );.      as
22294 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f  sert( TK_GT==OP_
22295 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Gt );.      asse
22296 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65  rt( TK_GE==OP_Ge
22297 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
22298 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29  ( TK_EQ==OP_Eq )
22299 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2229a 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a  TK_NE==OP_Ne );.
2229b 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2229c 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20  op==TK_LT );.   
2229d 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2229e 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20  =TK_LE );.      
2229f 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
222a0 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _GT );.      tes
222a1 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45  tcase( op==TK_GE
222a2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
222a3 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b  se( op==TK_EQ );
222a4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
222a5 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20   op==TK_NE );.  
222a6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
222a7 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
222a8 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
222a9 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c  Operands(pParse,
222aa 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
222ab 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20  r1, &regFree1,. 
222ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222ae 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
222af 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b  &r2, &regFree2);
222b0 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
222b1 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  re(pParse, pExpr
222b2 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e  ->pLeft, pExpr->
222b3 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20  pRight, op,.    
222b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
222b5 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70  , r2, dest, jump
222b6 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74  IfNull);.      t
222b7 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
222b8 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
222b9 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
222ba 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
222bb 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
222bc 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61  se TK_IS:.    ca
222bd 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20  se TK_ISNOT: {. 
222be 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
222bf 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20  p==TK_IS );.    
222c0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
222c1 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20  TK_ISNOT );.    
222c2 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
222c3 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
222c4 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
222c5 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
222c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
222c8 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
222c9 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
222ca 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b      op = (op==TK
222cb 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54  _IS) ? TK_EQ : T
222cc 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65  K_NE;.      code
222cd 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
222ce 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
222cf 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
222d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
222d1 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c     r1, r2, dest,
222d2 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b   SQLITE_NULLEQ);
222d3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
222d4 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
222d5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
222d6 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
222d7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
222d8 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  }.    case TK_IS
222d9 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54  NULL:.    case T
222da 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20  K_NOTNULL: {.   
222db 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53     assert( TK_IS
222dc 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20  NULL==OP_IsNull 
222dd 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
222de 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f   TK_NOTNULL==OP_
222df 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20  NotNull );.     
222e0 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
222e1 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  K_ISNULL );.    
222e2 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
222e3 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20  TK_NOTNULL );.  
222e4 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
222e5 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
222e6 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
222e7 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
222e8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
222e9 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31  AddOp2(v, op, r1
222ea 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74  , dest);.      t
222eb 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
222ec 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  1==0 );.      br
222ed 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
222ee 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20  ase TK_BETWEEN: 
222ef 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
222f0 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
222f1 29 3b 0a 20 20 20 20 20 20 65 78 70 72 43 6f 64  );.      exprCod
222f2 65 42 65 74 77 65 65 6e 28 70 50 61 72 73 65 2c  eBetween(pParse,
222f3 20 70 45 78 70 72 2c 20 64 65 73 74 2c 20 31 2c   pExpr, dest, 1,
222f4 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
222f5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
222f6 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
222f7 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73   {.      int des
222f8 74 49 66 46 61 6c 73 65 20 3d 20 73 71 6c 69 74  tIfFalse = sqlit
222f9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
222fa 76 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 64 65  v);.      int de
222fb 73 74 49 66 4e 75 6c 6c 20 3d 20 6a 75 6d 70 49  stIfNull = jumpI
222fc 66 4e 75 6c 6c 20 3f 20 64 65 73 74 20 3a 20 64  fNull ? dest : d
222fd 65 73 74 49 66 46 61 6c 73 65 3b 0a 20 20 20 20  estIfFalse;.    
222fe 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
222ff 65 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70  eIN(pParse, pExp
22300 72 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20  r, destIfFalse, 
22301 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  destIfNull);.   
22302 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22303 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
22304 20 30 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20   0, dest);.     
22305 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
22306 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73 74  lveLabel(v, dest
22307 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 20 20  IfFalse);.      
22308 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
22309 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
2230a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
2230b 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2230c 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72  e, pExpr, &regFr
2230d 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
2230e 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2230f 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65 73 74   OP_If, r1, dest
22310 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29  , jumpIfNull!=0)
22311 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
22312 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
22313 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22314 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
22315 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
22316 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
22317 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
22318 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
22319 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
2231a 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2231b 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 20 20  se, regFree2);  
2231c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
2231d 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f  te code for a bo
2231e 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
2231f 20 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d   such that a jum
22320 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  p is made.** to 
22321 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22  the label "dest"
22322 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
22323 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 75 74 20  on is false but 
22324 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e  execution.** con
22325 74 69 6e 75 65 73 20 73 74 72 61 69 67 68 74 20  tinues straight 
22326 74 68 72 75 20 69 66 20 74 68 65 20 65 78 70 72  thru if the expr
22327 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a  ession is true..
22328 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
22329 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65  ression evaluate
2232a 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68  s to NULL (neith
2232b 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73  er true nor fals
2232c 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20  e) then.** jump 
2232d 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73  if jumpIfNull is
2232e 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2232f 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75  LL or fall throu
22330 67 68 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  gh if jumpIfNull
22331 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c  .** is 0..*/.SQL
22332 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22333 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
22334 6c 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  lse(Parse *pPars
22335 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
22336 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75  int dest, int ju
22337 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62  mpIfNull){.  Vdb
22338 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
22339 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d  Vdbe;.  int op =
2233a 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
2233b 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  e1 = 0;.  int re
2233c 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e  gFree2 = 0;.  in
2233d 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73  t r1, r2;..  ass
2233e 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ert( jumpIfNull=
2233f 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  =SQLITE_JUMPIFNU
22340 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c  LL || jumpIfNull
22341 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56  ==0 );.  if( NEV
22342 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72  ER(v==0) ) retur
22343 6e 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20  n; /* Existance 
22344 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 64 20  of VDBE checked 
22345 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69  by caller */.  i
22346 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 20 20  f( pExpr==0 )   
22347 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54   return;..  /* T
22348 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70  he value of pExp
22349 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65  r->op and op are
2234a 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c   related as foll
2234b 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ows:.  **.  **  
2234c 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20       pExpr->op  
2234d 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a            op.  *
2234e 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  *       --------
2234f 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d  -          -----
22350 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20  -----.  **      
22351 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20   TK_ISNULL      
22352 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20      OP_NotNull. 
22353 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54   **       TK_NOT
22354 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f  NULL         OP_
22355 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20  IsNull.  **     
22356 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20    TK_NE         
22357 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20       OP_Eq.  ** 
22358 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20        TK_EQ     
22359 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20           OP_Ne. 
2235a 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20   **       TK_GT 
2235b 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f               OP_
2235c 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b  Le.  **       TK
2235d 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  _LE             
2235e 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20   OP_Gt.  **     
2235f 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20    TK_GE         
22360 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20       OP_Lt.  ** 
22361 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20        TK_LT     
22362 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20           OP_Ge. 
22363 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68   **.  ** For oth
22364 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78  er values of pEx
22365 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e  pr->op, op is un
22366 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73  defined and unus
22367 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c  ed..  ** The val
22368 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50  ue of TK_ and OP
22369 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  _ constants are 
2236a 61 72 72 61 6e 67 65 64 20 73 75 63 68 20 74 68  arranged such th
2236b 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63  at we.  ** can c
2236c 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70 70 69  ompute the mappi
2236d 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74  ng above using t
2236e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70  he following exp
2236f 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73  ression..  ** As
22370 73 65 72 74 28 29 73 20 76 65 72 69 66 79 20 74  sert()s verify t
22371 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74  hat the computat
22372 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a  ion is correct..
22373 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45    */.  op = ((pE
22374 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55  xpr->op+(TK_ISNU
22375 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53  LL&1))^1)-(TK_IS
22376 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56  NULL&1);..  /* V
22377 65 72 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c  erify correct al
22378 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61  ignment of TK_ a
22379 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73  nd OP_ constants
2237a 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2237b 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53  pExpr->op!=TK_IS
2237c 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e  NULL || op==OP_N
2237d 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65  otNull );.  asse
2237e 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
2237f 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d  K_NOTNULL || op=
22380 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20  =OP_IsNull );.  
22381 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
22382 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d  p!=TK_NE || op==
22383 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72  OP_Eq );.  asser
22384 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
22385 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65  _EQ || op==OP_Ne
22386 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
22387 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c  xpr->op!=TK_LT |
22388 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  | op==OP_Ge );. 
22389 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2238a 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d  op!=TK_LE || op=
2238b 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65  =OP_Gt );.  asse
2238c 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
2238d 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GT || op==OP_L
2238e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
2238f 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20  Expr->op!=TK_GE 
22390 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a  || op==OP_Lt );.
22391 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72  .  switch( pExpr
22392 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ->op ){.    case
22393 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20   TK_AND: {.     
22394 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
22395 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
22396 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
22397 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
22398 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
22399 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
2239a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2239b 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2239c 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65  Expr->pRight, de
2239d 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
2239e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2239f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
223a0 4f 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  OR: {.      int 
223a1 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  d2 = sqlite3Vdbe
223a2 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
223a3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
223a4 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
223a5 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
223a6 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
223a7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
223a8 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73  ExprIfTrue(pPars
223a9 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
223aa 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e   d2, jumpIfNull^
223ab 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
223ac 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  L);.      sqlite
223ad 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
223ae 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
223af 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
223b0 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Null);.      sql
223b1 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
223b2 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20  abel(v, d2);.   
223b3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
223b4 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
223b5 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
223b6 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
223b7 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74  K_NOT: {.      t
223b8 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
223b9 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
223ba 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
223bb 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
223bc 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75  >pLeft, dest, ju
223bd 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
223be 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
223bf 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20    case TK_LT:.  
223c0 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20    case TK_LE:.  
223c1 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20    case TK_GT:.  
223c2 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20    case TK_GE:.  
223c3 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20    case TK_NE:.  
223c4 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a    case TK_EQ: {.
223c5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
223c6 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20  op==TK_LT );.   
223c7 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
223c8 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20  =TK_LE );.      
223c9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
223ca 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _GT );.      tes
223cb 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45  tcase( op==TK_GE
223cc 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
223cd 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b  se( op==TK_EQ );
223ce 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
223cf 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20   op==TK_NE );.  
223d0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
223d1 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
223d2 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
223d3 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c  Operands(pParse,
223d4 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
223d5 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20  r1, &regFree1,. 
223d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223d8 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
223d9 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b  &r2, &regFree2);
223da 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
223db 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  re(pParse, pExpr
223dc 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e  ->pLeft, pExpr->
223dd 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20  pRight, op,.    
223de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
223df 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70  , r2, dest, jump
223e0 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74  IfNull);.      t
223e1 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
223e2 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
223e3 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
223e4 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
223e5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
223e6 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61  se TK_IS:.    ca
223e7 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20  se TK_ISNOT: {. 
223e8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
223e9 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20  Expr->op==TK_IS 
223ea 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
223eb 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
223ec 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20  _ISNOT );.      
223ed 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
223ee 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  nds(pParse, pExp
223ef 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  r->pLeft, &r1, &
223f0 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
223f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223f2 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
223f3 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20  r->pRight, &r2, 
223f4 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
223f5 20 20 6f 70 20 3d 20 28 70 45 78 70 72 2d 3e 6f    op = (pExpr->o
223f6 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f 4e  p==TK_IS) ? TK_N
223f7 45 20 3a 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  E : TK_EQ;.     
223f8 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
223f9 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
223fa 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
223fb 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
223fc 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
223fd 64 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55 4c  dest, SQLITE_NUL
223fe 4c 45 51 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LEQ);.      test
223ff 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
22400 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
22401 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
22402 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
22403 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
22404 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
22405 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
22406 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
22407 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
22408 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
22409 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
2240a 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  L );.      r1 = 
2240b 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
2240c 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
2240d 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
2240e 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
2240f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22410 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a   op, r1, dest);.
22411 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22412 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
22413 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22414 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  }.    case TK_BE
22415 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 74  TWEEN: {.      t
22416 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
22417 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
22418 65 78 70 72 43 6f 64 65 42 65 74 77 65 65 6e 28  exprCodeBetween(
22419 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64  pParse, pExpr, d
2241a 65 73 74 2c 20 30 2c 20 6a 75 6d 70 49 66 4e 75  est, 0, jumpIfNu
2241b 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
2241c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2241d 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20   TK_IN: {.      
2241e 69 66 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 29  if( jumpIfNull )
2241f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
22420 33 45 78 70 72 43 6f 64 65 49 4e 28 70 50 61 72  3ExprCodeIN(pPar
22421 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74 2c  se, pExpr, dest,
22422 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 7d 65   dest);.      }e
22423 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
22424 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20 73 71   destIfNull = sq
22425 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
22426 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73  el(v);.        s
22427 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 4e  qlite3ExprCodeIN
22428 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
22429 64 65 73 74 2c 20 64 65 73 74 49 66 4e 75 6c 6c  dest, destIfNull
2242a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2242b 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2242c 65 6c 28 76 2c 20 64 65 73 74 49 66 4e 75 6c 6c  el(v, destIfNull
2242d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2242e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2242f 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
22430 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
22431 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
22432 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46  se, pExpr, &regF
22433 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
22434 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22435 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20  , OP_IfNot, r1, 
22436 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
22437 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74  !=0);.      test
22438 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
22439 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
2243a 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
2243b 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
2243c 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
2243d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2243e 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2243f 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
22440 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
22441 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
22442 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61  );.}../*.** Do a
22443 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e   deep comparison
22444 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69   of two expressi
22445 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72  on trees.  Retur
22446 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f  n TRUE (non-zero
22447 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65  ).** if they are
22448 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72   identical and r
22449 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74  eturn FALSE if t
2244a 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e  hey differ in an
2244b 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d  y way..**.** Som
2244c 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74  etimes this rout
2244d 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
2244e 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68  FALSE even if th
2244f 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  e two expression
22450 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20  s.** really are 
22451 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20  equivalent.  If 
22452 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20  we cannot prove 
22453 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73  that the express
22454 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e  ions are.** iden
22455 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e  tical, we return
22456 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62   FALSE just to b
22457 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74  e safe.  So if t
22458 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
22459 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68  eturns false, th
2245a 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65  en you do not re
2245b 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65  ally know for ce
2245c 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f  rtain if the two
2245d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  .** expressions 
2245e 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42  are the same.  B
2245f 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20  ut if you get a 
22460 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65  TRUE return, the
22461 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20  n you.** can be 
22462 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73  sure the express
22463 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d  ions are the sam
22464 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65  e.  In the place
22465 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20  s where.** this 
22466 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c  routine is used,
22467 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72   it does not hur
22468 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72  t to get an extr
22469 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a  a FALSE - that.*
2246a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73  * just might res
2246b 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67  ult in some slig
2246c 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65  htly slower code
2246d 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67  .  But returning
2246e 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74  .** an incorrect
2246f 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64   TRUE could lead
22470 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f   to a malfunctio
22471 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
22472 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
22473 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72  ExprCompare(Expr
22474 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b   *pA, Expr *pB){
22475 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
22476 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a  pA==0||pB==0 ){.
22477 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70      return pB==p
22478 41 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  A;.  }.  assert(
22479 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
2247a 65 72 74 79 28 70 41 2c 20 45 50 5f 54 6f 6b 65  erty(pA, EP_Toke
2247b 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
2247c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ) );.  assert( !
2247d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
2247e 74 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f  ty(pB, EP_TokenO
2247f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
22480 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73  );.  if( ExprHas
22481 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f  Property(pA, EP_
22482 78 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45 78  xIsSelect) || Ex
22483 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42  prHasProperty(pB
22484 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
22485 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
22486 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e  .  }.  if( (pA->
22487 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69  flags & EP_Disti
22488 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73  nct)!=(pB->flags
22489 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 20   & EP_Distinct) 
2248a 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
2248b 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70  ( pA->op!=pB->op
2248c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2248d 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43  f( !sqlite3ExprC
2248e 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66 74  ompare(pA->pLeft
2248f 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20 72  , pB->pLeft) ) r
22490 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
22491 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
22492 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70  re(pA->pRight, p
22493 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74  B->pRight) ) ret
22494 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 41  urn 0;..  if( pA
22495 2d 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 42 2d  ->x.pList && pB-
22496 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  >x.pList ){.    
22497 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d  if( pA->x.pList-
22498 3e 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e 70 4c  >nExpr!=pB->x.pL
22499 69 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 65 74  ist->nExpr ) ret
2249a 75 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  urn 0;.    for(i
2249b 3d 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c 69 73  =0; i<pA->x.pLis
2249c 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
2249d 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
2249e 72 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 73 74  rA = pA->x.pList
2249f 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
224a0 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 42      Expr *pExprB
224a1 20 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e   = pB->x.pList->
224a2 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
224a3 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78    if( !sqlite3Ex
224a4 70 72 43 6f 6d 70 61 72 65 28 70 45 78 70 72 41  prCompare(pExprA
224a5 2c 20 70 45 78 70 72 42 29 20 29 20 72 65 74 75  , pExprB) ) retu
224a6 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  rn 0;.    }.  }e
224a7 6c 73 65 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c  lse if( pA->x.pL
224a8 69 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69  ist || pB->x.pLi
224a9 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  st ){.    return
224aa 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70   0;.  }..  if( p
224ab 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69  A->iTable!=pB->i
224ac 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f  Table || pA->iCo
224ad 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d  lumn!=pB->iColum
224ae 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  n ) return 0;.  
224af 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
224b0 72 74 79 28 70 41 2c 20 45 50 5f 49 6e 74 56 61  rty(pA, EP_IntVa
224b1 6c 75 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20  lue) ){.    if( 
224b2 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
224b3 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  (pB, EP_IntValue
224b4 29 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61 6c 75  ) || pA->u.iValu
224b5 65 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75 65 20  e!=pB->u.iValue 
224b6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
224b7 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  0;.    }.  }else
224b8 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f   if( pA->op!=TK_
224b9 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 75 2e  COLUMN && pA->u.
224ba 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66  zToken ){.    if
224bb 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
224bc 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75  y(pB, EP_IntValu
224bd 65 29 20 7c 7c 20 4e 45 56 45 52 28 70 42 2d 3e  e) || NEVER(pB->
224be 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29 20 72  u.zToken==0) ) r
224bf 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28  eturn 0;.    if(
224c0 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
224c1 70 41 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d  pA->u.zToken,pB-
224c2 3e 75 2e 7a 54 6f 6b 65 6e 29 21 3d 30 20 29 7b  >u.zToken)!=0 ){
224c3 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
224c4 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
224c5 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 1;.}.../*.**
224c6 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65   Add a new eleme
224c7 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e  nt to the pAggIn
224c8 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79  fo->aCol[] array
224c9 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e  .  Return the in
224ca 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  dex of.** the ne
224cb 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75  w element.  Retu
224cc 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  rn a negative nu
224cd 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66  mber if malloc f
224ce 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ails..*/.static 
224cf 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 6f  int addAggInfoCo
224d0 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  lumn(sqlite3 *db
224d1 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f  , AggInfo *pInfo
224d2 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49  ){.  int i;.  pI
224d3 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69  nfo->aCol = sqli
224d4 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65  te3ArrayAllocate
224d5 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20  (.       db,.   
224d6 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c      pInfo->aCol,
224d7 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70  .       sizeof(p
224d8 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a  Info->aCol[0]),.
224d9 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20         3,.      
224da 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e   &pInfo->nColumn
224db 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d  ,.       &pInfo-
224dc 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20  >nColumnAlloc,. 
224dd 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20        &i.  );.  
224de 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a  return i;.}    .
224df 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
224e0 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20   element to the 
224e1 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b  pAggInfo->aFunc[
224e2 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e  ] array.  Return
224e3 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a   the index of.**
224e4 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74   the new element
224e5 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61  .  Return a nega
224e6 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d  tive number if m
224e7 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
224e8 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67  static int addAg
224e9 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 65  gInfoFunc(sqlite
224ea 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a  3 *db, AggInfo *
224eb 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b  pInfo){.  int i;
224ec 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20  .  pInfo->aFunc 
224ed 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c  = sqlite3ArrayAl
224ee 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 64  locate(.       d
224ef 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 6f  b, .       pInfo
224f0 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 20  ->aFunc,.       
224f1 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46  sizeof(pInfo->aF
224f2 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  unc[0]),.       
224f3 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  3,.       &pInfo
224f4 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20  ->nFunc,.       
224f5 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c  &pInfo->nFuncAll
224f6 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20  oc,.       &i.  
224f7 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d  );.  return i;.}
224f8 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73      ../*.** This
224f9 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61 6c   is the xExprCal
224fa 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 65  lback for a tree
224fb 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 20   walker.  It is 
224fc 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65  used to.** imple
224fd 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72  ment sqlite3Expr
224fe 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
224ff 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 65  s().  See sqlite
22500 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72  3ExprAnalyzeAggr
22501 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64  egates.** for ad
22502 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
22503 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
22504 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65  int analyzeAggre
22505 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 61  gate(Walker *pWa
22506 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
22507 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e  r){.  int i;.  N
22508 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20  ameContext *pNC 
22509 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43  = pWalker->u.pNC
2250a 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ;.  Parse *pPars
2250b 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
2250c 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2250d 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
2250e 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f 20  List;.  AggInfo 
2250f 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 2d  *pAggInfo = pNC-
22510 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 77  >pAggInfo;..  sw
22511 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
22512 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
22513 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63  GG_COLUMN:.    c
22514 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b  ase TK_COLUMN: {
22515 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22516 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41   pExpr->op==TK_A
22517 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20  GG_COLUMN );.   
22518 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
22519 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
2251a 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 68  N );.      /* Ch
2251b 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
2251c 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 6f  e column is in o
2251d 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73  ne of the tables
2251e 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 20   in the FROM.   
2251f 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20     ** clause of 
22520 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75  the aggregate qu
22521 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ery */.      if(
22522 20 41 4c 57 41 59 53 28 70 53 72 63 4c 69 73 74   ALWAYS(pSrcList
22523 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
22524 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
22525 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72  tem *pItem = pSr
22526 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20  cList->a;.      
22527 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72    for(i=0; i<pSr
22528 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
22529 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
2252a 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67        struct Agg
2252b 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a  Info_col *pCol;.
2252c 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2252d 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
2252e 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2252f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64  TokenOnly|EP_Red
22530 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 20 20  uced) );.       
22531 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54     if( pExpr->iT
22532 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75  able==pItem->iCu
22533 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rsor ){.        
22534 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61      /* If we rea
22535 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69  ch this point, i
22536 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78  t means that pEx
22537 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74  pr refers to a t
22538 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  able.           
22539 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74   ** that is in t
2253a 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2253b 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  f the aggregate 
2253c 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20  query.  .       
2253d 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2253e 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65      ** Make an e
2253f 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c  ntry for the col
22540 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  umn in pAggInfo-
22541 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65  >aCol[] if there
22542 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
22543 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20  is not an entry 
22544 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20  there already.. 
22545 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
22546 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b            int k;
22547 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f  .            pCo
22548 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43  l = pAggInfo->aC
22549 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ol;.            
2254a 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49  for(k=0; k<pAggI
2254b 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b  nfo->nColumn; k+
2254c 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
2254d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
2254e 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70  ol->iTable==pExp
2254f 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20  r->iTable &&.   
22550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22551 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45  Col->iColumn==pE
22552 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a  xpr->iColumn ){.
22553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22554 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
22555 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
22556 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
22557 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66   if( (k>=pAggInf
22558 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20  o->nColumn).    
22559 20 20 20 20 20 20 20 20 20 26 26 20 28 6b 20 3d           && (k =
2255a 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d   addAggInfoColum
2255b 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41  n(pParse->db, pA
2255c 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20  ggInfo))>=0 .   
2255d 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20           ){.    
2255e 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d            pCol =
2255f 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c   &pAggInfo->aCol
22560 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  [k];.           
22561 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20     pCol->pTab = 
22562 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20  pExpr->pTab;.   
22563 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
22564 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d  >iTable = pExpr-
22565 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20  >iTable;.       
22566 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f         pCol->iCo
22567 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43  lumn = pExpr->iC
22568 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20  olumn;.         
22569 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20       pCol->iMem 
2256a 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2256b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2256c 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c  pCol->iSorterCol
2256d 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
2256e 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45          pCol->pE
2256f 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20  xpr = pExpr;.   
22570 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
22571 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42  AggInfo->pGroupB
22572 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  y ){.           
22573 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20       int j, n;. 
22574 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
22575 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70  xprList *pGB = p
22576 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42  AggInfo->pGroupB
22577 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y;.             
22578 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
22579 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d  st_item *pTerm =
2257a 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20   pGB->a;.       
2257b 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42           n = pGB
2257c 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->nExpr;.       
2257d 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
2257e 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72  ; j<n; j++, pTer
2257f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
22580 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
22581 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
22582 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22583 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54     if( pE->op==T
22584 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e  K_COLUMN && pE->
22585 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69  iTable==pExpr->i
22586 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20  Table &&.       
22587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22588 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70  E->iColumn==pExp
22589 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  r->iColumn ){.  
2258a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2258b 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
2258c 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20  olumn = j;.     
2258d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
2258e 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2258f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22590 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22591 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22592 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
22593 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
22594 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  n<0 ){.         
22595 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f         pCol->iSo
22596 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67  rterColumn = pAg
22597 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43  gInfo->nSortingC
22598 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20  olumn++;.       
22599 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2259a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2259b 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e     /* There is n
2259c 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20  ow an entry for 
2259d 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66  pExpr in pAggInf
2259e 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65  o->aCol[] (eithe
2259f 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  r.            **
225a0 20 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20   because it was 
225a1 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20  there before or 
225a2 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20  because we just 
225a3 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20 20  created it)..   
225a4 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76           ** Conv
225a5 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f  ert the pExpr to
225a6 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c   be a TK_AGG_COL
225a7 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f  UMN referring to
225a8 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20   that.          
225a9 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61    ** pAggInfo->a
225aa 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20  Col[] entry..   
225ab 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
225ac 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74 49          ExprSetI
225ad 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72  rreducible(pExpr
225ae 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
225af 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d  Expr->pAggInfo =
225b0 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20   pAggInfo;.     
225b1 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
225b2 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   = TK_AGG_COLUMN
225b3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
225b4 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 31 36  xpr->iAgg = (i16
225b5 29 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  )k;.            
225b6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
225b7 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 70   } /* endif pExp
225b8 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d  r->iTable==pItem
225b9 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20  ->iCursor */.   
225ba 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f       } /* end lo
225bb 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74  op over pSrcList
225bc 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
225bd 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
225be 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ne;.    }.    ca
225bf 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
225c0 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  ON: {.      /* T
225c1 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d  he pNC->nDepth==
225c2 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61 67  0 test causes ag
225c3 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
225c4 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 0a  s in subqueries.
225c5 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69        ** to be i
225c6 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  gnored */.      
225c7 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  if( pNC->nDepth=
225c8 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
225c9 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
225ca 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c   pExpr is a dupl
225cb 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72  icate of another
225cc 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20   aggregate .    
225cd 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20      ** function 
225ce 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20  that is already 
225cf 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20  in the pAggInfo 
225d0 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20  structure.      
225d1 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72    */.        str
225d2 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
225d3 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e   *pItem = pAggIn
225d4 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20  fo->aFunc;.     
225d5 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41     for(i=0; i<pA
225d6 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
225d7 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
225d8 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
225d9 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
225da 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78  Item->pExpr, pEx
225db 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  pr) ){.         
225dc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
225dd 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
225de 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 70          if( i>=p
225df 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29  AggInfo->nFunc )
225e0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70  {.          /* p
225e1 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c  Expr is original
225e2 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e  .  Make a new en
225e3 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d  try in pAggInfo-
225e4 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20  >aFunc[].       
225e5 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
225e6 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61  u8 enc = ENC(pPa
225e7 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20  rse->db);.      
225e8 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 6e      i = addAggIn
225e9 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e 64  foFunc(pParse->d
225ea 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20  b, pAggInfo);.  
225eb 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 30          if( i>=0
225ec 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
225ed 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
225ee 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
225ef 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
225f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74  .            pIt
225f1 65 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e  em = &pAggInfo->
225f2 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20  aFunc[i];.      
225f3 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78        pItem->pEx
225f4 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20  pr = pExpr;.    
225f5 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
225f6 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Mem = ++pParse->
225f7 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  nMem;.          
225f8 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
225f9 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
225fa 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
225fb 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49  ;.            pI
225fc 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73 71 6c  tem->pFunc = sql
225fd 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
225fe 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20 20 20  (pParse->db,.   
225ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22600 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  pExpr->u.zToken,
22601 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
22602 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pExpr->u.zToken
22603 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
22604 20 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e 70        pExpr->x.p
22605 4c 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 78 2e  List ? pExpr->x.
22606 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30  pList->nExpr : 0
22607 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20  , enc, 0);.     
22608 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
22609 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73  ->flags & EP_Dis
2260a 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 20 20  tinct ){.       
2260b 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44         pItem->iD
2260c 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65  istinct = pParse
2260d 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
2260e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2260f 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
22610 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31  ->iDistinct = -1
22611 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
22612 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22613 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
22614 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f 69 6e   Make pExpr poin
22615 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72  t to the appropr
22616 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61  iate pAggInfo->a
22617 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20  Func[] entry.   
22618 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
22619 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2261a 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
2261b 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  r, EP_TokenOnly|
2261c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
2261d 20 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72         ExprSetIr
2261e 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29  reducible(pExpr)
2261f 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  ;.        pExpr-
22620 3e 69 41 67 67 20 3d 20 28 69 31 36 29 69 3b 0a  >iAgg = (i16)i;.
22621 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
22622 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e  AggInfo = pAggIn
22623 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  fo;.        retu
22624 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
22625 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
22626 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
22627 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20  tinue;.}.static 
22628 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65  int analyzeAggre
22629 67 61 74 65 73 49 6e 53 65 6c 65 63 74 28 57 61  gatesInSelect(Wa
2262a 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
2262b 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b  elect *pSelect){
2262c 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
2262d 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  pNC = pWalker->u
2262e 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d  .pNC;.  if( pNC-
2262f 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20  >nDepth==0 ){.  
22630 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b    pNC->nDepth++;
22631 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  .    sqlite3Walk
22632 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
22633 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e  pSelect);.    pN
22634 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20  C->nDepth--;.   
22635 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
22636 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
22637 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
22638 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  nue;.  }.}../*.*
22639 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 67 69  * Analyze the gi
2263a 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  ven expression l
2263b 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67 72 65  ooking for aggre
2263c 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61  gate functions a
2263d 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69 61 62  nd.** for variab
2263e 6c 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  les that need to
2263f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
22640 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20   pParse->aAgg[] 
22641 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61  array..** Make a
22642 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65  dditional entrie
22643 73 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d  s to the pParse-
22644 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 20 61 73  >aAgg[] array as
22645 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
22646 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
22647 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61  hould only be ca
22648 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 65  lled after the e
22649 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62 65  xpression has be
2264a 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62  en.** analyzed b
2264b 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  y sqlite3Resolve
2264c 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a  ExprNames()..*/.
2264d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2264e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
2264f 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
22650 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e  (NameContext *pN
22651 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  C, Expr *pExpr){
22652 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77  .  Walker w;.  w
22653 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
22654 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
22655 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  e;.  w.xSelectCa
22656 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65  llback = analyze
22657 41 67 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65  AggregatesInSele
22658 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20  ct;.  w.u.pNC = 
22659 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pNC;.  assert( p
2265a 4e 43 2d 3e 70 53 72 63 4c 69 73 74 21 3d 30 20  NC->pSrcList!=0 
2265b 29 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  );.  sqlite3Walk
2265c 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b  Expr(&w, pExpr);
2265d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73  .}../*.** Call s
2265e 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
2265f 65 41 67 67 72 65 67 61 74 65 73 28 29 20 66 6f  eAggregates() fo
22660 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69  r every expressi
22661 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 70 72  on in an.** expr
22662 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65  ession list.  Re
22663 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
22664 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a  of errors..**.**
22665 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20   If an error is 
22666 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79  found, the analy
22667 73 69 73 20 69 73 20 63 75 74 20 73 68 6f 72 74  sis is cut short
22668 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22669 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2266a 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69  ExprAnalyzeAggLi
2266b 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  st(NameContext *
2266c 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  pNC, ExprList *p
2266d 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63 74 20  List){.  struct 
2266e 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2266f 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Item;.  int i;. 
22670 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
22671 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73    for(pItem=pLis
22672 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69  t->a, i=0; i<pLi
22673 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  st->nExpr; i++, 
22674 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
22675 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
22676 7a 65 41 67 67 72 65 67 61 74 65 73 28 70 4e 43  zeAggregates(pNC
22677 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b  , pItem->pExpr);
22678 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
22679 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 73  .** Allocate a s
2267a 69 6e 67 6c 65 20 6e 65 77 20 72 65 67 69 73 74  ingle new regist
2267b 65 72 20 66 6f 72 20 75 73 65 20 74 6f 20 68 6f  er for use to ho
2267c 6c 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d 65 64  ld some intermed
2267d 69 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a  iate result..*/.
2267e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2267f 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
22680 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72  pReg(Parse *pPar
22681 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73  se){.  if( pPars
22682 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29  e->nTempReg==0 )
22683 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70  {.    return ++p
22684 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d  Parse->nMem;.  }
22685 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65  .  return pParse
22686 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61  ->aTempReg[--pPa
22687 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a  rse->nTempReg];.
22688 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  }../*.** Dealloc
22689 61 74 65 20 61 20 72 65 67 69 73 74 65 72 2c 20  ate a register, 
2268a 6d 61 6b 69 6e 67 20 61 76 61 69 6c 61 62 6c 65  making available
2268b 20 66 6f 72 20 72 65 75 73 65 20 66 6f 72 20 73   for reuse for s
2268c 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72  ome other.** pur
2268d 70 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  pose..**.** If a
2268e 20 72 65 67 69 73 74 65 72 20 69 73 20 63 75 72   register is cur
2268f 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 75 73 65  rently being use
22690 64 20 62 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20  d by the column 
22691 63 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a 20 74  cache, then.** t
22692 68 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  he dallocation i
22693 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
22694 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
22695 65 20 6c 69 6e 65 20 74 68 61 74 20 75 73 65 73  e line that uses
22696 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72  .** the register
22697 20 62 65 63 6f 6d 65 73 20 73 74 61 6c 65 2e 0a   becomes stale..
22698 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22699 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
2269a 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72  leaseTempReg(Par
2269b 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2269c 69 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65  iReg){.  if( iRe
2269d 67 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65  g && pParse->nTe
2269e 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28  mpReg<ArraySize(
2269f 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67  pParse->aTempReg
226a0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  ) ){.    int i;.
226a1 20 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43      struct yColC
226a2 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 66 6f 72  ache *p;.    for
226a3 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
226a4 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
226a5 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
226a6 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 20  i++, p++){.     
226a7 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52   if( p->iReg==iR
226a8 65 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  eg ){.        p-
226a9 3e 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a 20 20  >tempReg = 1;.  
226aa 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
226ab 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
226ac 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67  pParse->aTempReg
226ad 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65  [pParse->nTempRe
226ae 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d  g++] = iReg;.  }
226af 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
226b0 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65  te or deallocate
226b1 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67   a block of nReg
226b2 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67   consecutive reg
226b3 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45  isters.*/.SQLITE
226b4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
226b5 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
226b6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
226b7 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74  int nReg){.  int
226b8 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61   i, n;.  i = pPa
226b9 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a  rse->iRangeReg;.
226ba 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52    n = pParse->nR
226bb 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e  angeReg;.  if( n
226bc 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41  Reg<=n && !usedA
226bd 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61  sColumnCache(pPa
226be 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29  rse, i, i+n-1) )
226bf 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52  {.    pParse->iR
226c0 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b  angeReg += nReg;
226c1 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61  .    pParse->nRa
226c2 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a  ngeReg -= nReg;.
226c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d    }else{.    i =
226c4 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
226c5 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
226c6 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20  m += nReg;.  }. 
226c7 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c   return i;.}.SQL
226c8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
226c9 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
226ca 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a  empRange(Parse *
226cb 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
226cc 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69  , int nReg){.  i
226cd 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e  f( nReg>pParse->
226ce 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20  nRangeReg ){.   
226cf 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52   pParse->nRangeR
226d0 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70  eg = nReg;.    p
226d1 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67  Parse->iRangeReg
226d2 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a   = iReg;.  }.}..
226d3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
226d4 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a  End of expr.c **
226d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
226d8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
226d9 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72  Begin file alter
226da 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
226db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
226dd 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75  /*.** 2005 Febru
226de 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ary 15.**.** The
226df 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
226e0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
226e1 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
226e2 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
226e3 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
226e4 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
226e5 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
226e6 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
226e7 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
226e8 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
226e9 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
226ea 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
226eb 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
226ec 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
226ed 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
226ee 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
226ef 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
226f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
226f4 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
226f5 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
226f6 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
226f7 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
226f8 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74  e VDBE code.** t
226f9 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
226fa 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63  he ALTER TABLE c
226fb 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  ommand..*/../*.*
226fc 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
226fd 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69  is file only exi
226fe 73 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f  sts if we are no
226ff 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a  t omitting the.*
22700 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f  * ALTER TABLE lo
22701 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69  gic from the bui
22702 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ld..*/.#ifndef S
22703 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52  QLITE_OMIT_ALTER
22704 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  TABLE.../*.** Th
22705 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
22706 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72  sed by SQL gener
22707 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ated to implemen
22708 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20  t the .** ALTER 
22709 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54  TABLE command. T
2270a 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2270b 74 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66  t is the text of
2270c 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
2270d 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44  or.** CREATE IND
2270e 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20  EX command. The 
2270f 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c  second is a tabl
22710 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c  e name. The tabl
22711 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68  e name in .** th
22712 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  e CREATE TABLE o
22713 72 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  r CREATE INDEX s
22714 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c  tatement is repl
22715 61 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68  aced with the th
22716 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ird.** argument 
22717 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72  and the result r
22718 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65  eturned. Example
22719 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f  s:.**.** sqlite_
2271a 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52  rename_table('CR
2271b 45 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61  EATE TABLE abc(a
2271c 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29  , b, c)', 'def')
2271d 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41  .**     -> 'CREA
2271e 54 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20  TE TABLE def(a, 
2271f 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c  b, c)'.**.** sql
22720 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  ite_rename_table
22721 28 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69  ('CREATE INDEX i
22722 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65   ON abc(a)', 'de
22723 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43  f').**     -> 'C
22724 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e  REATE INDEX i ON
22725 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a   def(a, b, c)'.*
22726 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
22727 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20  nameTableFunc(. 
22728 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
22729 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2272a 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
2272b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2272c 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  .){.  unsigned c
2272d 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20  har const *zSql 
2272e 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2272f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
22730 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
22731 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65  onst *zTableName
22732 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
22733 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
22734 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20  .  int token;.  
22735 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75  Token tname;.  u
22736 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e  nsigned char con
22737 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b  st *zCsr = zSql;
22738 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a  .  int len = 0;.
22739 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20    char *zRet;.. 
2273a 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2273b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2273c 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2273d 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
2273e 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
2273f 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63  ..  /* The princ
22740 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63  iple used to loc
22741 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ate the table na
22742 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
22743 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61   TABLE .  ** sta
22744 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74  tement is that t
22745 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
22746 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73   the first non-s
22747 70 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a  pace token that.
22748 20 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74    ** is immediat
22749 65 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ely followed by 
2274a 61 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53  a TK_LP or TK_US
2274b 49 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a  ING token..  */.
2274c 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
2274d 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28    do {.      if(
2274e 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20   !*zCsr ){.     
2274f 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66     /* Ran out of
22750 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69   input before fi
22751 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67  nding an opening
22752 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e   bracket. Return
22753 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20   NULL. */.      
22754 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
22755 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72  }..      /* Stor
22756 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  e the token that
22757 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20   zCsr points to 
22758 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20  in tname. */.   
22759 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68     tname.z = (ch
2275a 61 72 2a 29 7a 43 73 72 3b 0a 20 20 20 20 20 20  ar*)zCsr;.      
2275b 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a  tname.n = len;..
2275c 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65        /* Advance
2275d 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78   zCsr to the nex
2275e 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74  t token. Store t
2275f 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  hat token type i
22760 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20  n 'token',.     
22761 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67   ** and its leng
22762 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20  th in 'len' (to 
22763 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65  be used next ite
22764 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c  ration of this l
22765 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  oop)..      */. 
22766 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20       do {.      
22767 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20    zCsr += len;. 
22768 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c         len = sql
22769 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73  ite3GetToken(zCs
2276a 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  r, &token);.    
2276b 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e    } while( token
2276c 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20  ==TK_SPACE );.  
2276d 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e      assert( len>
2276e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65  0 );.    } while
2276f 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26  ( token!=TK_LP &
22770 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e  & token!=TK_USIN
22771 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d  G );..    zRet =
22772 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
22773 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25  db, "%.*s\"%w\"%
22774 73 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d 65 2e  s", ((u8*)tname.
22775 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c  z) - zSql, zSql,
22776 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 4e   .       zTableN
22777 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61  ame, tname.z+tna
22778 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  me.n);.    sqlit
22779 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
2277a 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31  ontext, zRet, -1
2277b 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2277c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2277d 54 68 69 73 20 43 20 66 75 6e 63 74 69 6f 6e 20  This C function 
2277e 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 53 51  implements an SQ
2277f 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20  L user function 
22780 74 68 61 74 20 69 73 20 75 73 65 64 20 62 79 20  that is used by 
22781 53 51 4c 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65  SQL code.** gene
22782 72 61 74 65 64 20 62 79 20 74 68 65 20 41 4c 54  rated by the ALT
22783 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e  ER TABLE ... REN
22784 41 4d 45 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 6d  AME command to m
22785 6f 64 69 66 79 20 74 68 65 20 64 65 66 69 6e 69  odify the defini
22786 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 6e 79 20 66  tion.** of any f
22787 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
22788 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65 20  raints that use 
22789 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
2278a 72 65 6e 61 6d 65 64 20 61 73 20 74 68 65 20 0a  renamed as the .
2278b 2a 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  ** parent table.
2278c 20 49 74 20 69 73 20 70 61 73 73 65 64 20 74 68   It is passed th
2278d 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a  ree arguments:.*
2278e 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 63 6f  *.**   1) The co
2278f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
22790 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
22791 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20  statement being 
22792 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 32  modified,.**   2
22793 29 20 54 68 65 20 6f 6c 64 20 6e 61 6d 65 20 6f  ) The old name o
22794 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
22795 67 20 72 65 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a  g renamed, and.*
22796 2a 20 20 20 33 29 20 54 68 65 20 6e 65 77 20 6e  *   3) The new n
22797 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
22798 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a   being renamed..
22799 2a 2a 0a 2a 2a 20 49 74 20 72 65 74 75 72 6e 73  **.** It returns
2279a 20 74 68 65 20 6e 65 77 20 43 52 45 41 54 45 20   the new CREATE 
2279b 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
2279c 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
2279d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 5f 72 65 6e  .**   sqlite_ren
2279e 61 6d 65 5f 70 61 72 65 6e 74 28 27 43 52 45 41  ame_parent('CREA
2279f 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 52 45  TE TABLE t1(a RE
227a0 46 45 52 45 4e 43 45 53 20 74 32 29 27 2c 20 27  FERENCES t2)', '
227a1 74 32 27 2c 20 27 74 33 27 29 0a 2a 2a 20 20 20  t2', 't3').**   
227a2 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54      -> 'CREATE T
227a3 41 42 4c 45 20 74 31 28 61 20 52 45 46 45 52 45  ABLE t1(a REFERE
227a4 4e 43 45 53 20 74 33 29 27 0a 2a 2f 0a 23 69 66  NCES t3)'.*/.#if
227a5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
227a6 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 73 74 61  _FOREIGN_KEY.sta
227a7 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 50  tic void renameP
227a8 61 72 65 6e 74 46 75 6e 63 28 0a 20 20 73 71 6c  arentFunc(.  sql
227a9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
227aa 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
227ab 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
227ac 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
227ad 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
227ae 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
227af 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
227b0 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74  t);.  char *zOut
227b1 70 75 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  put = 0;.  char 
227b2 2a 7a 52 65 73 75 6c 74 3b 0a 20 20 75 6e 73 69  *zResult;.  unsi
227b3 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
227b4 2a 7a 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65  *zInput = sqlite
227b5 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
227b6 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65  v[0]);.  unsigne
227b7 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f  d char const *zO
227b8 6c 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ld = sqlite3_val
227b9 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
227ba 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
227bb 72 20 63 6f 6e 73 74 20 2a 7a 4e 65 77 20 3d 20  r const *zNew = 
227bc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
227bd 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 0a 20 20  xt(argv[2]);..  
227be 75 6e 73 69 67 6e 65 64 20 63 6f 6e 73 74 20 63  unsigned const c
227bf 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
227c0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f  /* Pointer to to
227c1 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20  ken */.  int n; 
227c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
227c3 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
227c4 74 68 20 6f 66 20 74 6f 6b 65 6e 20 7a 20 2a 2f  th of token z */
227c5 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 20 20 20  .  int token;   
227c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
227c7 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 6f     /* Type of to
227c8 6b 65 6e 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  ken */..  UNUSED
227c9 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
227ca 65 64 29 3b 0a 20 20 66 6f 72 28 7a 3d 7a 49 6e  ed);.  for(z=zIn
227cb 70 75 74 3b 20 2a 7a 3b 20 7a 3d 7a 2b 6e 29 7b  put; *z; z=z+n){
227cc 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  .    n = sqlite3
227cd 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74 6f 6b  GetToken(z, &tok
227ce 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 74 6f 6b  en);.    if( tok
227cf 65 6e 3d 3d 54 4b 5f 52 45 46 45 52 45 4e 43 45  en==TK_REFERENCE
227d0 53 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  S ){.      char 
227d1 2a 7a 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20  *zParent;.      
227d2 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 20 2b  do {.        z +
227d3 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 6e 20 3d  = n;.        n =
227d4 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
227d5 28 7a 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20  (z, &token);.   
227d6 20 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e     }while( token
227d7 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 0a 20  ==TK_SPACE );.. 
227d8 20 20 20 20 20 7a 50 61 72 65 6e 74 20 3d 20 73       zParent = s
227d9 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
227da 64 62 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20  db, (const char 
227db 2a 29 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69  *)z, n);.      i
227dc 66 28 20 7a 50 61 72 65 6e 74 3d 3d 30 20 29 20  f( zParent==0 ) 
227dd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 73 71 6c  break;.      sql
227de 69 74 65 33 44 65 71 75 6f 74 65 28 7a 50 61 72  ite3Dequote(zPar
227df 65 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ent);.      if( 
227e0 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
227e1 70 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  p((const char *)
227e2 7a 4f 6c 64 2c 20 7a 50 61 72 65 6e 74 29 20 29  zOld, zParent) )
227e3 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
227e4 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  zOut = sqlite3MP
227e5 72 69 6e 74 66 28 64 62 2c 20 22 25 73 25 2e 2a  rintf(db, "%s%.*
227e6 73 5c 22 25 77 5c 22 22 2c 20 0a 20 20 20 20 20  s\"%w\"", .     
227e7 20 20 20 20 20 20 20 28 7a 4f 75 74 70 75 74 3f         (zOutput?
227e8 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a 2d 7a  zOutput:""), z-z
227e9 49 6e 70 75 74 2c 20 7a 49 6e 70 75 74 2c 20 28  Input, zInput, (
227ea 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 4e 65  const char *)zNe
227eb 77 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  w.        );.   
227ec 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
227ed 65 65 28 64 62 2c 20 7a 4f 75 74 70 75 74 29 3b  ee(db, zOutput);
227ee 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 70 75 74  .        zOutput
227ef 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20 20   = zOut;.       
227f0 20 7a 49 6e 70 75 74 20 3d 20 26 7a 5b 6e 5d 3b   zInput = &z[n];
227f1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
227f2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
227f3 20 7a 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d   zParent);.    }
227f4 0a 20 20 7d 0a 0a 20 20 7a 52 65 73 75 6c 74 20  .  }..  zResult 
227f5 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
227f6 28 64 62 2c 20 22 25 73 25 73 22 2c 20 28 7a 4f  (db, "%s%s", (zO
227f7 75 74 70 75 74 3f 7a 4f 75 74 70 75 74 3a 22 22  utput?zOutput:""
227f8 29 2c 20 7a 49 6e 70 75 74 29 2c 20 0a 20 20 73  ), zInput), .  s
227f9 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
227fa 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73  xt(context, zRes
227fb 75 6c 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ult, -1, SQLITE_
227fc 44 59 4e 41 4d 49 43 29 3b 0a 20 20 73 71 6c 69  DYNAMIC);.  sqli
227fd 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f  te3DbFree(db, zO
227fe 75 74 70 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66  utput);.}.#endif
227ff 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22800 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a  _OMIT_TRIGGER./*
22801 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
22802 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65  s used by SQL ge
22803 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65  nerated to imple
22804 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 45  ment the.** ALTE
22805 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e  R TABLE command.
22806 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
22807 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20  ent is the text 
22808 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47  of a CREATE TRIG
22809 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  GER .** statemen
2280a 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73  t. The second is
2280b 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54   a table name. T
2280c 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e  he table name in
2280d 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a 20   the CREATE .** 
2280e 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
2280f 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69  t is replaced wi
22810 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72 67  th the third arg
22811 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65  ument and the re
22812 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e 65  sult .** returne
22813 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c 61  d. This is anala
22814 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54 61  gous to renameTa
22815 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65 2c  bleFunc() above,
22816 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45 41   except for CREA
22817 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20 6e  TE.** TRIGGER, n
22818 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ot CREATE INDEX 
22819 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c 45  and CREATE TABLE
2281a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2281b 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75   renameTriggerFu
2281c 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2281d 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2281e 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
2281f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
22820 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67  *argv.){.  unsig
22821 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
22822 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  zSql = sqlite3_v
22823 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
22824 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ]);.  unsigned c
22825 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c  har const *zTabl
22826 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  eName = sqlite3_
22827 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
22828 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65  1]);..  int toke
22829 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65  n;.  Token tname
2282a 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20 33  ;.  int dist = 3
2282b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
2282c 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20  r const *zCsr = 
2282d 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20  zSql;.  int len 
2282e 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65  = 0;.  char *zRe
2282f 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
22830 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
22831 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
22832 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44  text);..  UNUSED
22833 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
22834 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70  ed);..  /* The p
22835 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f  rinciple used to
22836 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   locate the tabl
22837 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52  e name in the CR
22838 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20 20  EATE TRIGGER .  
22839 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ** statement is 
2283a 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e  that the table n
2283b 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74  ame is the first
2283c 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 69   token that is i
2283d 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a  mmediatedly.  **
2283e 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69 74   preceded by eit
2283f 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f  her TK_ON or TK_
22840 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 74  DOT and immediat
22841 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79  edly followed by
22842 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f   one.  ** of TK_
22843 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f  WHEN, TK_BEGIN o
22844 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20  r TK_FOR..  */. 
22845 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20   if( zSql ){.   
22846 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66 28   do {..      if(
22847 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20   !*zCsr ){.     
22848 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66     /* Ran out of
22849 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69   input before fi
2284a 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  nding the table 
2284b 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c  name. Return NUL
2284c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  L. */.        re
2284d 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  turn;.      }.. 
2284e 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68       /* Store th
2284f 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73  e token that zCs
22850 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74  r points to in t
22851 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74  name. */.      t
22852 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29  name.z = (char*)
22853 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d  zCsr;.      tnam
22854 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20  e.n = len;..    
22855 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73    /* Advance zCs
22856 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f  r to the next to
22857 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20  ken. Store that 
22858 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74  token type in 't
22859 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20  oken',.      ** 
2285a 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69  and its length i
2285b 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75  n 'len' (to be u
2285c 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69  sed next iterati
2285d 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29  on of this loop)
2285e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2285f 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43   do {.        zC
22860 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  sr += len;.     
22861 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
22862 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26  GetToken(zCsr, &
22863 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77  token);.      }w
22864 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f  hile( token==TK_
22865 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61  SPACE );.      a
22866 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a  ssert( len>0 );.
22867 0a 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62  .      /* Variab
22868 6c 65 20 27 64 69 73 74 27 20 73 74 6f 72 65 73  le 'dist' stores
22869 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
2286a 6f 6b 65 6e 73 20 72 65 61 64 20 73 69 6e 63 65  okens read since
2286b 20 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 20 20   the most.      
2286c 2a 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 4f 54  ** recent TK_DOT
2286d 20 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 73 20   or TK_ON. This 
2286e 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20  means that when 
2286f 61 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 20 42  a WHEN, FOR or B
22870 45 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a 20 74  EGIN .      ** t
22871 6f 6b 65 6e 20 69 73 20 72 65 61 64 20 61 6e 64  oken is read and
22872 20 27 64 69 73 74 27 20 65 71 75 61 6c 73 20 32   'dist' equals 2
22873 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
22874 73 74 61 74 65 64 20 61 62 6f 76 65 0a 20 20 20  stated above.   
22875 20 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 74 2e     ** to be met.
22876 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
22877 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f 4e 20  ** Note that ON 
22878 63 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 74 61  cannot be a data
22879 62 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 20 63  base, table or c
2287a 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f 0a 20  olumn name, so. 
2287b 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73       ** there is
2287c 20 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f 72 72   no need to worr
2287d 79 20 61 62 6f 75 74 20 73 79 6e 74 61 78 20 6c  y about syntax l
2287e 69 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 22 43  ike .      ** "C
2287f 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2e 2e  REATE TRIGGER ..
22880 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e  . ON ON.ON BEGIN
22881 20 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 20 20   ..." etc..     
22882 20 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 2b 2b   */.      dist++
22883 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6f 6b 65  ;.      if( toke
22884 6e 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b  n==TK_DOT || tok
22885 65 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 20 20  en==TK_ON ){.   
22886 20 20 20 20 20 64 69 73 74 20 3d 20 30 3b 0a 20       dist = 0;. 
22887 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 68 69       }.    } whi
22888 6c 65 28 20 64 69 73 74 21 3d 32 20 7c 7c 20 28  le( dist!=2 || (
22889 74 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e 20 26  token!=TK_WHEN &
2288a 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f 52 20  & token!=TK_FOR 
2288b 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 45 47  && token!=TK_BEG
2288c 49 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 56  IN) );..    /* V
2288d 61 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 6e 6f  ariable tname no
2288e 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  w contains the t
2288f 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 74 68 65  oken that is the
22890 20 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d 65 0a   old table-name.
22891 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 43 52      ** in the CR
22892 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61  EATE TRIGGER sta
22893 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  tement..    */. 
22894 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65     zRet = sqlite
22895 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e  3MPrintf(db, "%.
22896 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 28 28 75  *s\"%w\"%s", ((u
22897 38 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a 53  8*)tname.z) - zS
22898 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20  ql, zSql, .     
22899 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e    zTableName, tn
2289a 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a  ame.z+tname.n);.
2289b 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2289c 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
2289d 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54   zRet, -1, SQLIT
2289e 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a  E_DYNAMIC);.  }.
2289f 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 21 53  }.#endif   /* !S
228a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
228a1 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ER */../*.** Reg
228a2 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66  ister built-in f
228a3 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f  unctions used to
228a4 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20   help implement 
228a5 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53  ALTER TABLE.*/.S
228a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
228a7 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
228a8 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
228a9 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33   *db){.  sqlite3
228aa 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
228ab 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61  sqlite_rename_ta
228ac 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ble", 2, SQLITE_
228ad 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20  UTF8, 0,.       
228ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228af 20 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e    renameTableFun
228b0 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65  c, 0, 0);.#ifnde
228b1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
228b2 49 47 47 45 52 0a 20 20 73 71 6c 69 74 65 33 43  IGGER.  sqlite3C
228b3 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73  reateFunc(db, "s
228b4 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69  qlite_rename_tri
228b5 67 67 65 72 22 2c 20 32 2c 20 53 51 4c 49 54 45  gger", 2, SQLITE
228b6 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20  _UTF8, 0,.      
228b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228b8 20 20 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72     renameTrigger
228b9 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e  Func, 0, 0);.#en
228ba 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
228bb 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
228bc 4b 45 59 0a 20 20 73 71 6c 69 74 65 33 43 72 65  KEY.  sqlite3Cre
228bd 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c  ateFunc(db, "sql
228be 69 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e  ite_rename_paren
228bf 74 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54  t", 3, SQLITE_UT
228c0 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  F8, 0,.         
228c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228c2 72 65 6e 61 6d 65 50 61 72 65 6e 74 46 75 6e 63  renameParentFunc
228c3 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a  , 0, 0);.#endif.
228c4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
228c5 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
228c6 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 65 78  o create the tex
228c7 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
228c8 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a   of the form:.**
228c9 0a 2a 2a 20 20 20 6e 61 6d 65 3d 3c 63 6f 6e 73  .**   name=<cons
228ca 74 61 6e 74 31 3e 20 4f 52 20 6e 61 6d 65 3d 3c  tant1> OR name=<
228cb 63 6f 6e 73 74 61 6e 74 32 3e 20 4f 52 20 2e 2e  constant2> OR ..
228cc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d  ..**.** If argum
228cd 65 6e 74 20 7a 57 68 65 72 65 20 69 73 20 4e 55  ent zWhere is NU
228ce 4c 4c 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74  LL, then a point
228cf 65 72 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69  er string contai
228d0 6e 69 6e 67 20 74 68 65 20 74 65 78 74 20 0a 2a  ning the text .*
228d1 2a 20 22 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e  * "name=<constan
228d2 74 3e 22 20 69 73 20 72 65 74 75 72 6e 65 64 2c  t>" is returned,
228d3 20 77 68 65 72 65 20 3c 63 6f 6e 73 74 61 6e 74   where <constant
228d4 3e 20 69 73 20 74 68 65 20 71 75 6f 74 65 64 20  > is the quoted 
228d5 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  version.** of th
228d6 65 20 73 74 72 69 6e 67 20 70 61 73 73 65 64 20  e string passed 
228d7 61 73 20 61 72 67 75 6d 65 6e 74 20 7a 43 6f 6e  as argument zCon
228d8 73 74 61 6e 74 2e 20 54 68 65 20 72 65 74 75 72  stant. The retur
228d9 6e 65 64 20 62 75 66 66 65 72 20 69 73 0a 2a 2a  ned buffer is.**
228da 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
228db 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
228dc 28 29 2e 20 49 74 20 69 73 20 74 68 65 20 72 65  (). It is the re
228dd 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
228de 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  the.** caller to
228df 20 65 6e 73 75 72 65 20 74 68 61 74 20 69 74 20   ensure that it 
228e0 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72  is eventually fr
228e1 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72  eed..**.** If ar
228e2 67 75 6d 65 6e 74 20 7a 57 68 65 72 65 20 69 73  gument zWhere is
228e3 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
228e4 74 68 65 20 73 74 72 69 6e 67 20 72 65 74 75 72  the string retur
228e5 6e 65 64 20 69 73 20 0a 2a 2a 20 22 3c 77 68 65  ned is .** "<whe
228e6 72 65 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e  re> OR name=<con
228e7 73 74 61 6e 74 3e 22 2c 20 77 68 65 72 65 20 3c  stant>", where <
228e8 77 68 65 72 65 3e 20 69 73 20 74 68 65 20 63 6f  where> is the co
228e9 6e 74 65 6e 74 73 20 6f 66 20 7a 57 68 65 72 65  ntents of zWhere
228ea 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73  ..** In this cas
228eb 65 20 7a 57 68 65 72 65 20 69 73 20 70 61 73 73  e zWhere is pass
228ec 65 64 20 74 6f 20 73 71 6c 69 74 65 33 44 62 46  ed to sqlite3DbF
228ed 72 65 65 28 29 20 62 65 66 6f 72 65 20 72 65 74  ree() before ret
228ee 75 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2f 0a 73  urning..** .*/.s
228ef 74 61 74 69 63 20 63 68 61 72 20 2a 77 68 65 72  tatic char *wher
228f0 65 4f 72 4e 61 6d 65 28 73 71 6c 69 74 65 33 20  eOrName(sqlite3 
228f1 2a 64 62 2c 20 63 68 61 72 20 2a 7a 57 68 65 72  *db, char *zWher
228f2 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 73 74 61  e, char *zConsta
228f3 6e 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65  nt){.  char *zNe
228f4 77 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65  w;.  if( !zWhere
228f5 20 29 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73   ){.    zNew = s
228f6 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
228f7 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 7a 43 6f  , "name=%Q", zCo
228f8 6e 73 74 61 6e 74 29 3b 0a 20 20 7d 65 6c 73 65  nstant);.  }else
228f9 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c  {.    zNew = sql
228fa 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
228fb 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22 2c  "%s OR name=%Q",
228fc 20 7a 57 68 65 72 65 2c 20 7a 43 6f 6e 73 74 61   zWhere, zConsta
228fd 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
228fe 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
228ff 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
22900 20 7a 4e 65 77 3b 0a 7d 0a 0a 23 69 66 20 21 64   zNew;.}..#if !d
22901 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22902 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20  IT_FOREIGN_KEY) 
22903 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
22904 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
22905 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22906 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57 48  the text of a WH
22907 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 77  ERE expression w
22908 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
22909 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a   to select all.*
2290a 2a 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61  * tables that ha
2290b 76 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ve foreign key c
2290c 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 20  onstraints that 
2290d 72 65 66 65 72 20 74 6f 20 74 61 62 6c 65 20 70  refer to table p
2290e 54 61 62 20 28 69 2e 65 2e 0a 2a 2a 20 63 6f 6e  Tab (i.e..** con
2290f 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69  straints for whi
22910 63 68 20 70 54 61 62 20 69 73 20 74 68 65 20 70  ch pTab is the p
22911 61 72 65 6e 74 20 74 61 62 6c 65 29 20 66 72 6f  arent table) fro
22912 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  m the sqlite_mas
22913 74 65 72 0a 2a 2a 20 74 61 62 6c 65 2e 0a 2a 2f  ter.** table..*/
22914 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68  .static char *wh
22915 65 72 65 46 6f 72 65 69 67 6e 4b 65 79 73 28 50  ereForeignKeys(P
22916 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
22917 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 46 4b  ble *pTab){.  FK
22918 65 79 20 2a 70 3b 0a 20 20 63 68 61 72 20 2a 7a  ey *p;.  char *z
22919 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 66 6f 72  Where = 0;.  for
2291a 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66 65  (p=sqlite3FkRefe
2291b 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 3b  rences(pTab); p;
2291c 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a   p=p->pNextTo){.
2291d 20 20 20 20 7a 57 68 65 72 65 20 3d 20 77 68 65      zWhere = whe
2291e 72 65 4f 72 4e 61 6d 65 28 70 50 61 72 73 65 2d  reOrName(pParse-
2291f 3e 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 2d 3e  >db, zWhere, p->
22920 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pFrom->zName);. 
22921 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65   }.  return zWhe
22922 72 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  re;.}.#endif../*
22923 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
22924 20 74 65 78 74 20 6f 66 20 61 20 57 48 45 52 45   text of a WHERE
22925 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63   expression whic
22926 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
22927 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74   select all.** t
22928 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72  emporary trigger
22929 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20  s on table pTab 
2292a 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
2292b 74 65 6d 70 5f 6d 61 73 74 65 72 20 74 61 62 6c  temp_master tabl
2292c 65 2e 20 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70  e. If.** table p
2292d 54 61 62 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f  Tab has no tempo
2292e 72 61 72 79 20 74 72 69 67 67 65 72 73 2c 20 6f  rary triggers, o
2292f 72 20 69 73 20 69 74 73 65 6c 66 20 73 74 6f 72  r is itself stor
22930 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65  ed in the .** te
22931 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
22932 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  , NULL is return
22933 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ed..*/.static ch
22934 61 72 20 2a 77 68 65 72 65 54 65 6d 70 54 72 69  ar *whereTempTri
22935 67 67 65 72 73 28 50 61 72 73 65 20 2a 70 50 61  ggers(Parse *pPa
22936 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
22937 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  ){.  Trigger *pT
22938 72 69 67 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68  rig;.  char *zWh
22939 65 72 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  ere = 0;.  const
2293a 20 53 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63   Schema *pTempSc
2293b 68 65 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64  hema = pParse->d
2293c 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
2293d 61 3b 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63  a; /* Temp db sc
2293e 68 65 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  hema */..  /* If
2293f 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f   the table is no
22940 74 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65  t located in the
22941 20 74 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69   temp-db (in whi
22942 63 68 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20  ch case NULL is 
22943 0a 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20  .  ** returned, 
22944 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  loop through the
22945 20 74 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20   tables list of 
22946 74 72 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61  triggers. For ea
22947 63 68 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20  ch trigger.  ** 
22948 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74  that is not part
22949 20 6f 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20   of the temp-db 
2294a 73 63 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c  schema, add a cl
2294b 61 75 73 65 20 74 6f 20 74 68 65 20 57 48 45 52  ause to the WHER
2294c 45 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69  E .  ** expressi
2294d 6f 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75  on being built u
2294e 70 20 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a  p in zWhere..  *
2294f 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  /.  if( pTab->pS
22950 63 68 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65  chema!=pTempSche
22951 6d 61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ma ){.    sqlite
22952 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
22953 64 62 3b 0a 20 20 20 20 66 6f 72 28 70 54 72 69  db;.    for(pTri
22954 67 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  g=sqlite3Trigger
22955 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  List(pParse, pTa
22956 62 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67  b); pTrig; pTrig
22957 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a  =pTrig->pNext){.
22958 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67 2d        if( pTrig-
22959 3e 70 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53  >pSchema==pTempS
2295a 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20  chema ){.       
2295b 20 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65 4f   zWhere = whereO
2295c 72 4e 61 6d 65 28 64 62 2c 20 7a 57 68 65 72 65  rName(db, zWhere
2295d 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 29 3b  , pTrig->zName);
2295e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2295f 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65   }.  return zWhe
22960 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  re;.}../*.** Gen
22961 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72  erate code to dr
22962 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68  op and reload th
22963 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65  e internal repre
22964 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 61 62  sentation of tab
22965 6c 65 0a 2a 2a 20 70 54 61 62 20 66 72 6f 6d 20  le.** pTab from 
22966 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 6e  the database, in
22967 63 6c 75 64 69 6e 67 20 74 72 69 67 67 65 72 73  cluding triggers
22968 20 61 6e 64 20 74 65 6d 70 6f 72 61 72 79 20 74   and temporary t
22969 72 69 67 67 65 72 73 2e 0a 2a 2a 20 41 72 67 75  riggers..** Argu
2296a 6d 65 6e 74 20 7a 4e 61 6d 65 20 69 73 20 74 68  ment zName is th
2296b 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
2296c 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
2296d 61 73 65 20 73 63 68 65 6d 61 20 61 74 0a 2a 2a  ase schema at.**
2296e 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 67 65   the time the ge
2296f 6e 65 72 61 74 65 64 20 63 6f 64 65 20 69 73 20  nerated code is 
22970 65 78 65 63 75 74 65 64 2e 20 54 68 69 73 20 63  executed. This c
22971 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20  an be different 
22972 66 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e  from.** pTab->zN
22973 61 6d 65 20 69 66 20 74 68 69 73 20 66 75 6e 63  ame if this func
22974 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61  tion is being ca
22975 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 70 61 72  lled to code par
22976 74 20 6f 66 20 61 6e 20 0a 2a 2a 20 22 41 4c 54  t of an .** "ALT
22977 45 52 20 54 41 42 4c 45 20 52 45 4e 41 4d 45 20  ER TABLE RENAME 
22978 54 4f 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  TO" statement..*
22979 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
2297a 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28  loadTableSchema(
2297b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2297c 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73  able *pTab, cons
2297d 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
2297e 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 63 68 61    Vdbe *v;.  cha
2297f 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 69 6e 74  r *zWhere;.  int
22980 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
22981 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
22982 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
22983 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a  taining pTab */.
22984 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22985 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 54 72  MIT_TRIGGER.  Tr
22986 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 23 65  igger *pTrig;.#e
22987 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ndif..  v = sqli
22988 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22989 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  e);.  if( NEVER(
2298a 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  v==0) ) return;.
2298b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2298c 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
2298d 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62  texes(pParse->db
2298e 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ) );.  iDb = sql
2298f 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
22990 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
22991 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
22992 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29  assert( iDb>=0 )
22993 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
22994 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
22995 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 62   /* Drop any tab
22996 6c 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d  le triggers from
22997 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63   the internal sc
22998 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70  hema. */.  for(p
22999 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72 69 67  Trig=sqlite3Trig
2299a 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  gerList(pParse, 
2299b 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20 70 54  pTab); pTrig; pT
2299c 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74  rig=pTrig->pNext
2299d 29 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69 67  ){.    int iTrig
2299e 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2299f 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
229a0 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63  ->db, pTrig->pSc
229a1 68 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 72  hema);.    asser
229a2 74 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62 20  t( iTrigDb==iDb 
229a3 7c 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29 3b  || iTrigDb==1 );
229a4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
229a5 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f  AddOp4(v, OP_Dro
229a6 70 54 72 69 67 67 65 72 2c 20 69 54 72 69 67 44  pTrigger, iTrigD
229a7 62 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e  b, 0, 0, pTrig->
229a8 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23  zName, 0);.  }.#
229a9 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70  endif..  /* Drop
229aa 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69   the table and i
229ab 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e  ndex from the in
229ac 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 20  ternal schema.  
229ad 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
229ae 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f  AddOp4(v, OP_Dro
229af 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20  pTable, iDb, 0, 
229b0 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
229b1 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64  0);..  /* Reload
229b2 20 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65   the table, inde
229b3 78 20 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20  x and permanent 
229b4 74 72 69 67 67 65 72 20 73 63 68 65 6d 61 73 2e  trigger schemas.
229b5 20 2a 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20 73   */.  zWhere = s
229b6 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50  qlite3MPrintf(pP
229b7 61 72 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e  arse->db, "tbl_n
229b8 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b  ame=%Q", zName);
229b9 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65 20 29  .  if( !zWhere )
229ba 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
229bb 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
229bc 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
229bd 69 44 62 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72  iDb, 0, 0, zWher
229be 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P4_DYNAMIC);.
229bf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
229c0 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
229c1 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 20 74 61  * Now, if the ta
229c2 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72 65  ble is not store
229c3 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61  d in the temp da
229c4 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 64 20 61  tabase, reload a
229c5 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72  ny temp .  ** tr
229c6 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 20 75 73  iggers. Don't us
229c7 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73  e IN(...) in cas
229c8 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  e SQLITE_OMIT_SU
229c9 42 51 55 45 52 59 20 69 73 20 64 65 66 69 6e 65  BQUERY is define
229ca 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  d. .  */.  if( (
229cb 7a 57 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70  zWhere=whereTemp
229cc 54 72 69 67 67 65 72 73 28 70 50 61 72 73 65 2c  Triggers(pParse,
229cd 20 70 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20   pTab))!=0 ){.  
229ce 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
229cf 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op4(v, OP_ParseS
229d0 63 68 65 6d 61 2c 20 31 2c 20 30 2c 20 30 2c 20  chema, 1, 0, 0, 
229d1 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d  zWhere, P4_DYNAM
229d2 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  IC);.  }.#endif.
229d3 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
229d4 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  e code to implem
229d5 65 6e 74 20 74 68 65 20 22 41 4c 54 45 52 20 54  ent the "ALTER T
229d6 41 42 4c 45 20 78 78 78 20 52 45 4e 41 4d 45 20  ABLE xxx RENAME 
229d7 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d  TO yyy" .** comm
229d8 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  and. .*/.SQLITE_
229d9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
229da 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54  ite3AlterRenameT
229db 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
229dc 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
229dd 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
229de 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  ext. */.  SrcLis
229df 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20  t *pSrc,        
229e0 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
229e1 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20   to rename. */. 
229e2 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20   Token *pName   
229e3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
229e4 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
229e5 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44  . */.){.  int iD
229e6 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
229e7 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
229e8 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
229e9 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  e table */.  cha
229ea 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
229eb 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
229ec 66 20 64 61 74 61 62 61 73 65 20 69 44 62 20 2a  f database iDb *
229ed 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
229ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
229ef 20 54 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e   Table being ren
229f0 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  amed */.  char *
229f1 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20  zName = 0;      
229f2 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d      /* NULL-term
229f3 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f  inated version o
229f4 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 20 73 71  f pName */ .  sq
229f5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
229f6 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62  se->db; /* Datab
229f7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
229f8 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e 61 6d 65  /.  int nTabName
229f9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
229fa 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 46 2d 38   Number of UTF-8
229fb 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a   characters in z
229fc 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  TabName */.  con
229fd 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d  st char *zTabNam
229fe 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e  e;     /* Origin
229ff 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  al name of the t
22a00 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a  able */.  Vdbe *
22a01 76 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  v;.#ifndef SQLIT
22a02 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
22a03 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20   char *zWhere = 
22a04 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68  0;         /* Wh
22a05 65 72 65 20 63 6c 61 75 73 65 20 74 6f 20 6c 6f  ere clause to lo
22a06 63 61 74 65 20 74 65 6d 70 20 74 72 69 67 67 65  cate temp trigge
22a07 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56  rs */.#endif.  V
22a08 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 30  Table *pVTab = 0
22a09 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d  ;        /* Non-
22a0a 7a 65 72 6f 20 69 66 20 74 68 69 73 20 69 73 20  zero if this is 
22a0b 61 20 76 2d 74 61 62 20 77 69 74 68 20 61 6e 20  a v-tab with an 
22a0c 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 0a  xRename() */.  .
22a0d 20 20 69 66 28 20 4e 45 56 45 52 28 64 62 2d 3e    if( NEVER(db->
22a0e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 20  mallocFailed) ) 
22a0f 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
22a10 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74  _table;.  assert
22a11 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20  ( pSrc->nSrc==1 
22a12 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
22a13 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
22a14 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d  lMutexes(pParse-
22a15 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 20  >db) );..  pTab 
22a16 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
22a17 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
22a18 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  pSrc->a[0].zName
22a19 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61  , pSrc->a[0].zDa
22a1a 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21  tabase);.  if( !
22a1b 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74  pTab ) goto exit
22a1c 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
22a1d 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
22a1e 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
22a1f 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
22a20 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20  chema);.  zDb = 
22a21 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
22a22 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20  me;..  /* Get a 
22a23 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20  NULL terminated 
22a24 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e  version of the n
22a25 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a  ew table name. *
22a26 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  /.  zName = sqli
22a27 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
22a28 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  (db, pName);.  i
22a29 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f  f( !zName ) goto
22a2a 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
22a2b 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  le;..  /* Check 
22a2c 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20  that a table or 
22a2d 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61  index named 'zNa
22a2e 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72  me' does not alr
22a2f 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20  eady exist.  ** 
22a30 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e  in database iDb.
22a31 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20   If so, this is 
22a32 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20  an error..  */. 
22a33 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
22a34 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c  Table(db, zName,
22a35 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33   zDb) || sqlite3
22a36 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e  FindIndex(db, zN
22a37 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  ame, zDb) ){.   
22a38 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22a39 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22a3a 20 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65    "there is alre
22a3b 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c  ady another tabl
22a3c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20  e or index with 
22a3d 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20  this name: %s", 
22a3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
22a3f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
22a40 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  le;.  }..  /* Ma
22a41 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f  ke sure it is no
22a42 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65  t a system table
22a43 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20   being altered, 
22a44 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e 61  or a reserved na
22a45 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65  me.  ** that the
22a46 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20   table is being 
22a47 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f  renamed to..  */
22a48 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
22a49 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61  rlen30(pTab->zNa
22a4a 6d 65 29 3e 36 20 0a 20 20 20 26 26 20 30 3d 3d  me)>6 .   && 0==
22a4b 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
22a4c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
22a4d 6c 69 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b 0a  lite_", 7).  ){.
22a4e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22a4f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
22a50 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
22a51 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d   altered", pTab-
22a52 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
22a53 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
22a54 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53  ble;.  }.  if( S
22a55 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
22a56 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
22a57 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
22a58 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
22a59 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
22a5a 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
22a5b 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69  TE_OMIT_VIEW.  i
22a5c 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
22a5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
22a5e 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
22a5f 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74  "view %s may not
22a60 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54   be altered", pT
22a61 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
22a62 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
22a63 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
22a64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
22a65 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
22a66 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ATION.  /* Invok
22a67 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  e the authorizat
22a68 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ion callback. */
22a69 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
22a6a 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
22a6b 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
22a6c 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a  LE, zDb, pTab->z
22a6d 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Name, 0) ){.    
22a6e 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
22a6f 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
22a70 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
22a71 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
22a72 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74  ABLE.  if( sqlit
22a73 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
22a74 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
22a75 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  b) ){.    goto e
22a76 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
22a77 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69  ;.  }.  if( IsVi
22a78 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
22a79 20 20 20 70 56 54 61 62 20 3d 20 73 71 6c 69 74     pVTab = sqlit
22a7a 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
22a7b 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70  pTab);.    if( p
22a7c 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f  VTab->pVtab->pMo
22a7d 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30  dule->xRename==0
22a7e 20 29 7b 0a 20 20 20 20 20 20 70 56 54 61 62 20   ){.      pVTab 
22a7f 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  = 0;.    }.  }.#
22a80 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69  endif..  /* Begi
22a81 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  n a transaction 
22a82 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65 72  and code the Ver
22a83 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61  ifyCookie for da
22a84 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20 2a  tabase iDb. .  *
22a85 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 68  * Then modify th
22a86 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
22a87 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52  (since the ALTER
22a88 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20   TABLE modifies 
22a89 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29  the.  ** schema)
22a8a 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d 65  . Open a stateme
22a8b 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
22a8c 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61  f the table is a
22a8d 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
22a8e 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  ble..  */.  v = 
22a8f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
22a90 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
22a91 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  =0 ){.    goto e
22a92 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
22a93 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  ;.  }.  sqlite3B
22a94 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
22a95 6f 6e 28 70 50 61 72 73 65 2c 20 70 56 54 61 62  on(pParse, pVTab
22a96 21 3d 30 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c  !=0, iDb);.  sql
22a97 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
22a98 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a  (pParse, iDb);..
22a99 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
22a9a 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  a virtual table,
22a9b 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65 6e   invoke the xRen
22a9c 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ame() function i
22a9d 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64 65  f.  ** one is de
22a9e 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e 61  fined. The xRena
22a9f 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77 69  me() callback wi
22aa0 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e 61  ll modify the na
22aa1 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 20  mes.  ** of any 
22aa2 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 20 62  resources used b
22aa3 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d  y the v-table im
22aa4 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69 6e  plementation (in
22aa5 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20 20  cluding other.  
22aa6 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73  ** SQLite tables
22aa7 29 20 74 68 61 74 20 61 72 65 20 69 64 65 6e 74  ) that are ident
22aa8 69 66 69 65 64 20 62 79 20 74 68 65 20 6e 61 6d  ified by the nam
22aa9 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  e of the virtual
22aaa 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66   table..  */.#if
22aab 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22aac 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
22aad 69 66 28 20 70 56 54 61 62 20 29 7b 0a 20 20 20  if( pVTab ){.   
22aae 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73   int i = ++pPars
22aaf 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
22ab0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
22ab1 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
22ab2 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29   i, 0, zName, 0)
22ab3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22ab4 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52  eAddOp4(v, OP_VR
22ab5 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28  ename, i, 0, 0,(
22ab6 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 56 54 61  const char*)pVTa
22ab7 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20  b, P4_VTAB);.   
22ab8 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
22ab9 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 65  (pParse);.  }.#e
22aba 6e 64 69 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72  ndif..  /* figur
22abb 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 55  e out how many U
22abc 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20  TF-8 characters 
22abd 61 72 65 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a  are in zName */.
22abe 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61    zTabName = pTa
22abf 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62  b->zName;.  nTab
22ac0 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 55 74  Name = sqlite3Ut
22ac1 66 38 43 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61  f8CharLen(zTabNa
22ac2 6d 65 2c 20 2d 31 29 3b 0a 0a 23 69 66 20 21 64  me, -1);..#if !d
22ac3 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
22ac4 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20  IT_FOREIGN_KEY) 
22ac5 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
22ac6 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
22ac7 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
22ac8 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
22ac9 65 79 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  eys ){.    /* If
22aca 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75 70   foreign-key sup
22acb 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2c  port is enabled,
22acc 20 72 65 77 72 69 74 65 20 74 68 65 20 43 52 45   rewrite the CRE
22acd 41 54 45 20 54 41 42 4c 45 20 0a 20 20 20 20 2a  ATE TABLE .    *
22ace 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f 72  * statements cor
22acf 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61 6c  responding to al
22ad0 6c 20 63 68 69 6c 64 20 74 61 62 6c 65 73 20 6f  l child tables o
22ad1 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  f foreign key co
22ad2 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a  nstraints.    **
22ad3 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 72   for which the r
22ad4 65 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20  enamed table is 
22ad5 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
22ad6 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 7a  .  */.    if( (z
22ad7 57 68 65 72 65 3d 77 68 65 72 65 46 6f 72 65 69  Where=whereForei
22ad8 67 6e 4b 65 79 73 28 70 50 61 72 73 65 2c 20 70  gnKeys(pParse, p
22ad9 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  Tab))!=0 ){.    
22ada 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
22adb 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20  arse(pParse, .  
22adc 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
22add 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 53 45  sqlite_master SE
22ade 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  T ".            
22adf 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f    "sql = sqlite_
22ae0 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74 28 73 71  rename_parent(sq
22ae1 6c 2c 20 25 51 2c 20 25 51 29 20 22 0a 20 20 20  l, %Q, %Q) ".   
22ae2 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52             "WHER
22ae3 45 20 25 73 3b 22 2c 20 7a 54 61 62 4e 61 6d 65  E %s;", zTabName
22ae4 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29  , zName, zWhere)
22ae5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
22ae6 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65  bFree(db, zWhere
22ae7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  );.    }.  }.#en
22ae8 64 69 66 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79  dif..  /* Modify
22ae9 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
22aea 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65 20  er table to use 
22aeb 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  the new table na
22aec 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  me. */.  sqlite3
22aed 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
22aee 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54  se,.      "UPDAT
22aef 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23 69  E %Q.%s SET ".#i
22af0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
22af1 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20  _TRIGGER.       
22af2 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65     "sql = sqlite
22af3 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71  _rename_table(sq
22af4 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a  l, %Q), ".#else.
22af5 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d            "sql =
22af6 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 20   CASE ".        
22af7 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20 3d      "WHEN type =
22af8 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e 20   'trigger' THEN 
22af9 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72  sqlite_rename_tr
22afa 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22 0a  igger(sql, %Q)".
22afb 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53              "ELS
22afc 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f  E sqlite_rename_
22afd 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20 45  table(sql, %Q) E
22afe 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20 20  ND, ".#endif.   
22aff 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65         "tbl_name
22b00 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20 20   = %Q, ".       
22b01 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 20     "name = CASE 
22b02 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
22b03 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65 27  HEN type='table'
22b04 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20 20   THEN %Q ".     
22b05 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61 6d         "WHEN nam
22b06 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f 61  e LIKE 'sqlite_a
22b07 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44 20  utoindex%%' AND 
22b08 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48 45  type='index' THE
22b09 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  N ".            
22b0a 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e   "'sqlite_autoin
22b0b 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73  dex_' || %Q || s
22b0c 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38  ubstr(name,%d+18
22b0d 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) ".            
22b0e 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22  "ELSE name END "
22b0f 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74 62  .      "WHERE tb
22b10 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a  l_name=%Q AND ".
22b11 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70 65            "(type
22b12 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70 65  ='table' OR type
22b13 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70 65  ='index' OR type
22b14 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20 0a  ='trigger');", .
22b15 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d        zDb, SCHEM
22b16 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e  A_TABLE(iDb), zN
22b17 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  ame, zName, zNam
22b18 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  e, .#ifndef SQLI
22b19 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
22b1a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e        zName,.#en
22b1b 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c  dif.      zName,
22b1c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e   nTabName, zTabN
22b1d 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65  ame.  );..#ifnde
22b1e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
22b1f 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a  TOINCREMENT.  /*
22b20 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   If the sqlite_s
22b21 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78  equence table ex
22b22 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74  ists in this dat
22b23 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61  abase, then upda
22b24 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68  te .  ** it with
22b25 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
22b26 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ame..  */.  if( 
22b27 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
22b28 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71  (db, "sqlite_seq
22b29 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a  uence", zDb) ){.
22b2a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
22b2b 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
22b2c 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c         "UPDATE \
22b2d 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71  "%w\".sqlite_seq
22b2e 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 3d  uence set name =
22b2f 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 3d   %Q WHERE name =
22b30 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44   %Q",.        zD
22b31 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b, zName, pTab->
22b32 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64  zName);.  }.#end
22b33 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
22b34 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
22b35 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
22b36 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20  e TEMP triggers 
22b37 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 6d  on this table, m
22b38 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65  odify the sqlite
22b39 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a  _temp_master.  *
22b3a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64  * table. Don't d
22b3b 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74 61  o this if the ta
22b3c 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52 65  ble being ALTERe
22b3d 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63 61  d is itself loca
22b3e 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ted in.  ** the 
22b3f 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a 20  temp database.. 
22b40 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72   */.  if( (zWher
22b41 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67  e=whereTempTrigg
22b42 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ers(pParse, pTab
22b43 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))!=0 ){.    sql
22b44 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
22b45 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
22b46 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f   "UPDATE sqlite_
22b47 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20  temp_master SET 
22b48 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73  ".            "s
22b49 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61  ql = sqlite_rena
22b4a 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c 20  me_trigger(sql, 
22b4b 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20 20  %Q), ".         
22b4c 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25     "tbl_name = %
22b4d 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  Q ".            
22b4e 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61  "WHERE %s;", zNa
22b4f 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72  me, zName, zWher
22b50 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
22b51 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65  bFree(db, zWhere
22b52 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
22b53 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
22b54 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
22b55 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 64  KEY) && !defined
22b56 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
22b57 47 47 45 52 29 0a 20 20 69 66 28 20 64 62 2d 3e  GGER).  if( db->
22b58 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72  flags&SQLITE_For
22b59 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20  eignKeys ){.    
22b5a 46 4b 65 79 20 2a 70 3b 0a 20 20 20 20 66 6f 72  FKey *p;.    for
22b5b 28 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66 65  (p=sqlite3FkRefe
22b5c 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 3b  rences(pTab); p;
22b5d 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a   p=p->pNextTo){.
22b5e 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 46 72        Table *pFr
22b5f 6f 6d 20 3d 20 70 2d 3e 70 46 72 6f 6d 3b 0a 20  om = p->pFrom;. 
22b60 20 20 20 20 20 69 66 28 20 70 46 72 6f 6d 21 3d       if( pFrom!=
22b61 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  pTab ){.        
22b62 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d  reloadTableSchem
22b63 61 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 46 72  a(pParse, p->pFr
22b64 6f 6d 2c 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65  om, pFrom->zName
22b65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
22b66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
22b67 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61  * Drop and reloa
22b68 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74  d the internal t
22b69 61 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a  able schema. */.
22b6a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68    reloadTableSch
22b6b 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62  ema(pParse, pTab
22b6c 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 74 5f  , zName);..exit_
22b6d 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a 20 20  rename_table:.  
22b6e 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
22b6f 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a  lete(db, pSrc);.
22b70 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22b71 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a  db, zName);.}...
22b72 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
22b73 6f 64 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ode to make sure
22b74 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
22b75 20 6e 75 6d 62 65 72 20 69 73 20 61 74 20 6c 65   number is at le
22b76 61 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a  ast minFormat..*
22b77 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20  * The generated 
22b78 63 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72 65 61  code will increa
22b79 73 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  se the file form
22b7a 61 74 20 6e 75 6d 62 65 72 20 69 66 20 6e 65 63  at number if nec
22b7b 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  essary..*/.SQLIT
22b7c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22b7d 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c  qlite3MinimumFil
22b7e 65 46 6f 72 6d 61 74 28 50 61 72 73 65 20 2a 70  eFormat(Parse *p
22b7f 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 2c 20  Parse, int iDb, 
22b80 69 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a  int minFormat){.
22b81 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 20 3d    Vdbe *v;.  v =
22b82 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
22b83 70 50 61 72 73 65 29 3b 0a 20 20 2f 2a 20 54 68  pParse);.  /* Th
22b84 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68 61  e VDBE should ha
22b85 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  ve been allocate
22b86 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f  d before this ro
22b87 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
22b88 0a 20 20 2a 2a 20 49 66 20 74 68 61 74 20 61 6c  .  ** If that al
22b89 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2c  location failed,
22b8a 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 71   we would have q
22b8b 75 69 74 20 62 65 66 6f 72 65 20 72 65 61 63 68  uit before reach
22b8c 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f  ing this.  ** po
22b8d 69 6e 74 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57  int */.  if( ALW
22b8e 41 59 53 28 76 29 20 29 7b 0a 20 20 20 20 69 6e  AYS(v) ){.    in
22b8f 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
22b90 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
22b91 3b 0a 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73  ;.    int r2 = s
22b92 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
22b93 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 6e  (pParse);.    in
22b94 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c 69 74 65  t j1;.    sqlite
22b95 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22b96 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44  P_ReadCookie, iD
22b97 62 2c 20 72 31 2c 20 42 54 52 45 45 5f 46 49 4c  b, r1, BTREE_FIL
22b98 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73  E_FORMAT);.    s
22b99 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
22b9a 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
22b9b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22b9c 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
22b9d 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29  , minFormat, r2)
22b9e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
22b9f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22ba0 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c 20 72 31  OP_Ge, r2, 0, r1
22ba1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22ba2 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53  beAddOp3(v, OP_S
22ba3 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42  etCookie, iDb, B
22ba4 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
22ba5 2c 20 72 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  , r2);.    sqlit
22ba6 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
22ba7 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j1);.    sqlit
22ba8 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
22ba9 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
22baa 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
22bab 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
22bac 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  r2);.  }.}../*.*
22bad 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
22bae 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
22baf 61 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  an "ALTER TABLE 
22bb0 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65  ... ADD" stateme
22bb1 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70  nt.** has been p
22bb2 61 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20  arsed. Argument 
22bb3 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73  pColDef contains
22bb4 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65   the text of the
22bb5 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64   new.** column d
22bb6 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  efinition..**.**
22bb7 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   The Table struc
22bb8 74 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65  ture pParse->pNe
22bb9 77 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e  wTable was exten
22bba 64 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a  ded to include.*
22bbb 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  * the new column
22bbc 20 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e   during parsing.
22bbd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22bbe 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
22bbf 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
22bc0 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  umn(Parse *pPars
22bc1 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65  e, Token *pColDe
22bc2 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65  f){.  Table *pNe
22bc3 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
22bc4 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73  /* Copy of pPars
22bc5 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a  e->pNewTable */.
22bc6 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
22bc7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22bc8 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
22bc9 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ed */.  int iDb;
22bca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22bcb 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
22bcc 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  mber */.  const 
22bcd 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
22bce 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
22bcf 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
22bd0 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20  char *zTab;     
22bd1 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
22bd2 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  e */.  char *zCo
22bd3 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
22bd4 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61   /* Null-termina
22bd5 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e  ted column defin
22bd6 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d  ition */.  Colum
22bd7 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20  n *pCol;        
22bd8 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
22bd9 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72  column */.  Expr
22bda 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20   *pDflt;        
22bdb 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
22bdc 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e   value for the n
22bdd 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73  ew column */.  s
22bde 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
22bdf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22be0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
22be1 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20  ion; */..  db = 
22be2 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
22be3 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
22be4 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
22be5 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  ed ) return;.  p
22be6 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  New = pParse->pN
22be7 65 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72  ewTable;.  asser
22be8 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73  t( pNew );..  as
22be9 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
22bea 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
22beb 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d  s(db) );.  iDb =
22bec 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
22bed 49 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e  Index(db, pNew->
22bee 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
22bef 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
22bf0 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26  Name;.  zTab = &
22bf1 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b  pNew->zName[16];
22bf2 20 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73    /* Skip the "s
22bf3 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22  qlite_altertab_"
22bf4 20 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e   prefix on the n
22bf5 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20  ame */.  pCol = 
22bf6 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77  &pNew->aCol[pNew
22bf7 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66  ->nCol-1];.  pDf
22bf8 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  lt = pCol->pDflt
22bf9 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
22bfa 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
22bfb 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73  zTab, zDb);.  as
22bfc 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23  sert( pTab );..#
22bfd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22bfe 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
22bff 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
22c00 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
22c01 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66  allback. */.  if
22c02 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
22c03 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
22c04 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a  E_ALTER_TABLE, z
22c05 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  Db, pTab->zName,
22c06 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
22c07 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
22c08 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75   /* If the defau
22c09 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
22c0a 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20   new column was 
22c0b 73 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61  specified with a
22c0c 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e   .  ** literal N
22c0d 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44  ULL, then set pD
22c0e 66 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73  flt to 0. This s
22c0f 69 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69  implifies checki
22c10 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53  ng.  ** for an S
22c11 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20  QL NULL default 
22c12 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  below..  */.  if
22c13 28 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74  ( pDflt && pDflt
22c14 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b  ->op==TK_NULL ){
22c15 0a 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a  .    pDflt = 0;.
22c16 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
22c17 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f 6c  that the new col
22c18 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63 69  umn is not speci
22c19 66 69 65 64 20 61 73 20 50 52 49 4d 41 52 59 20  fied as PRIMARY 
22c1a 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20  KEY or UNIQUE.. 
22c1b 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
22c1c 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  a NOT NULL const
22c1d 72 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20  raint, then the 
22c1e 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
22c1f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d  r the.  ** colum
22c20 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55  n must not be NU
22c21 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  LL..  */.  if( p
22c22 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 20 29  Col->isPrimKey )
22c23 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
22c24 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43  orMsg(pParse, "C
22c25 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d  annot add a PRIM
22c26 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29  ARY KEY column")
22c27 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
22c28 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49  }.  if( pNew->pI
22c29 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69  ndex ){.    sqli
22c2a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22c2b 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
22c2c 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22  a UNIQUE column"
22c2d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
22c2e 20 7d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c   }.  if( (db->fl
22c2f 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69  ags&SQLITE_Forei
22c30 67 6e 4b 65 79 73 29 20 26 26 20 70 4e 65 77 2d  gnKeys) && pNew-
22c31 3e 70 46 4b 65 79 20 26 26 20 70 44 66 6c 74 20  >pFKey && pDflt 
22c32 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
22c33 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
22c34 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 20          "Cannot 
22c35 61 64 64 20 61 20 52 45 46 45 52 45 4e 43 45 53  add a REFERENCES
22c36 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e   column with non
22c37 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76 61  -NULL default va
22c38 6c 75 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72  lue");.    retur
22c39 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f  n;.  }.  if( pCo
22c3a 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  l->notNull && !p
22c3b 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Dflt ){.    sqli
22c3c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22c3d 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61  se, .        "Ca
22c3e 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e  nnot add a NOT N
22c3f 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  ULL column with 
22c40 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55  default value NU
22c41 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  LL");.    return
22c42 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75  ;.  }..  /* Ensu
22c43 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65  re the default e
22c44 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d  xpression is som
22c45 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69  ething that sqli
22c46 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
22c47 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64  ().  ** can hand
22c48 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52  le (i.e. not CUR
22c49 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a  RENT_TIME etc.).
22c4a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74    */.  if( pDflt
22c4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
22c4c 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20  value *pVal;.   
22c4d 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75   if( sqlite3Valu
22c4e 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44  eFromExpr(db, pD
22c4f 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  flt, SQLITE_UTF8
22c50 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  , SQLITE_AFF_NON
22c51 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20  E, &pVal) ){.   
22c52 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
22c53 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
22c54 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
22c55 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20   if( !pVal ){.  
22c56 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22c57 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e  Msg(pParse, "Can
22c58 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e  not add a column
22c59 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61   with non-consta
22c5a 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20  nt default");.  
22c5b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
22c5c 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
22c5d 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
22c5e 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74  }..  /* Modify t
22c5f 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
22c60 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
22c61 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  zCol = sqlite3Db
22c62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61  StrNDup(db, (cha
22c63 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70  r*)pColDef->z, p
22c64 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66  ColDef->n);.  if
22c65 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68  ( zCol ){.    ch
22c66 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c  ar *zEnd = &zCol
22c67 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a  [pColDef->n-1];.
22c68 20 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e      while( zEnd>
22c69 7a 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d  zCol && (*zEnd==
22c6a 27 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49 73  ';' || sqlite3Is
22c6b 73 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b  space(*zEnd)) ){
22c6c 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d  .      *zEnd-- =
22c6d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20   '\0';.    }.   
22c6e 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
22c6f 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20  rse(pParse, .   
22c70 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25       "UPDATE \"%
22c71 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20  w\".%s SET ".   
22c72 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75         "sql = su
22c73 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c  bstr(sql,1,%d) |
22c74 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20  | ', ' || %Q || 
22c75 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22  substr(sql,%d) "
22c76 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20  .        "WHERE 
22c77 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41  type = 'table' A
22c78 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a  ND name = %Q", .
22c79 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d        zDb, SCHEM
22c7a 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e  A_TABLE(iDb), pN
22c7b 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74  ew->addColOffset
22c7c 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64  , zCol, pNew->ad
22c7d 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20  dColOffset+1,.  
22c7e 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a      zTab.    );.
22c7f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
22c80 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d  e(db, zCol);.  }
22c81 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65  ..  /* If the de
22c82 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74  fault value of t
22c83 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  he new column is
22c84 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20   NULL, then set 
22c85 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f  the file.  ** fo
22c86 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 68  rmat to 2. If th
22c87 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  e default value 
22c88 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  of the new colum
22c89 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20  n is not NULL,. 
22c8a 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f 72   ** the file for
22c8b 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20  mat becomes 3.. 
22c8c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e   */.  sqlite3Min
22c8d 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70  imumFileFormat(p
22c8e 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 6c  Parse, iDb, pDfl
22c8f 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f  t ? 3 : 2);..  /
22c90 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 68  * Reload the sch
22c91 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 66  ema of the modif
22c92 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20  ied table. */.  
22c93 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d  reloadTableSchem
22c94 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  a(pParse, pTab, 
22c95 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a  pTab->zName);.}.
22c96 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
22c97 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
22c98 79 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74  y the parser aft
22c99 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d  er the table-nam
22c9a 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45  e in.** an "ALTE
22c9b 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e  R TABLE <table-n
22c9c 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 6d  ame> ADD" statem
22c9d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 41  ent is parsed. A
22c9e 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63  rgument .** pSrc
22c9f 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d   is the full-nam
22ca0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62  e of the table b
22ca1 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a  eing altered..**
22ca2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22ca3 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 61   makes a (partia
22ca4 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54  l) copy of the T
22ca5 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 2a  able structure.*
22ca6 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
22ca7 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 6e  being altered an
22ca8 64 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e 65  d sets Parse.pNe
22ca9 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a  wTable to point.
22caa 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e  ** to it. Routin
22cab 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  es called by the
22cac 20 70 61 72 73 65 72 20 61 73 20 74 68 65 20 63   parser as the c
22cad 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
22cae 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 69  .** is parsed (i
22caf 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 6f  .e. sqlite3AddCo
22cb0 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 20  lumn()) add the 
22cb1 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20  new Column data 
22cb2 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e  to .** the copy.
22cb3 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65   The copy of the
22cb4 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
22cb5 20 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 74   is deleted by t
22cb6 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66  okenize.c .** af
22cb7 74 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 66  ter parsing is f
22cb8 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52  inished..**.** R
22cb9 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 6c  outine sqlite3Al
22cba 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
22cbb 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  mn() will be cal
22cbc 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a  led to complete.
22cbd 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 41  ** coding the "A
22cbe 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41  LTER TABLE ... A
22cbf 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  DD" statement..*
22cc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22cc1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
22cc2 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e  erBeginAddColumn
22cc3 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
22cc4 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a  SrcList *pSrc){.
22cc5 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20    Table *pNew;. 
22cc6 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
22cc7 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69  Vdbe *v;.  int i
22cc8 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  Db;.  int i;.  i
22cc9 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c  nt nAlloc;.  sql
22cca 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
22ccb 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  e->db;..  /* Loo
22ccc 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62  k up the table b
22ccd 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f  eing altered. */
22cce 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
22ccf 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20  e->pNewTable==0 
22cd0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
22cd1 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
22cd2 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
22cd3 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
22cd4 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78  Failed ) goto ex
22cd5 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
22cd6 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  umn;.  pTab = sq
22cd7 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
22cd8 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63  (pParse, 0, pSrc
22cd9 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53  ->a[0].zName, pS
22cda 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  rc->a[0].zDataba
22cdb 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  se);.  if( !pTab
22cdc 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
22cdd 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a  in_add_column;..
22cde 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22cdf 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
22ce0 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
22ce1 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
22ce2 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22ce3 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74  arse, "virtual t
22ce4 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65  ables may not be
22ce5 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20   altered");.    
22ce6 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
22ce7 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
22ce8 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b  #endif..  /* Mak
22ce9 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e  e sure this is n
22cea 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  ot an attempt to
22ceb 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a   ALTER a view. *
22cec 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  /.  if( pTab->pS
22ced 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
22cee 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
22cef 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64  rse, "Cannot add
22cf0 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76   a column to a v
22cf1 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  iew");.    goto 
22cf2 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
22cf3 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73  olumn;.  }..  as
22cf4 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 43  sert( pTab->addC
22cf5 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20  olOffset>0 );.  
22cf6 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
22cf7 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
22cf8 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  Tab->pSchema);..
22cf9 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 20    /* Put a copy 
22cfa 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  of the Table str
22cfb 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e 65  uct in Parse.pNe
22cfc 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a 20  wTable for the. 
22cfd 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 6f   ** sqlite3AddCo
22cfe 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  lumn() function 
22cff 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 6d  and friends to m
22d00 6f 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 69  odify.  But modi
22d01 66 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65  fy.  ** the name
22d02 20 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73   by adding an "s
22d03 71 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22  qlite_altertab_"
22d04 20 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 64   prefix.  By add
22d05 69 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 72  ing this.  ** pr
22d06 65 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20  efix, we insure 
22d07 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69  that the name wi
22d08 6c 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77  ll not collide w
22d09 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  ith an existing.
22d0a 20 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75    ** table becau
22d0b 73 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72  se user table ar
22d0c 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  e not allowed to
22d0d 20 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74   have the "sqlit
22d0e 65 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 20  e_".  ** prefix 
22d0f 6f 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20  on their name.. 
22d10 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61   */.  pNew = (Ta
22d11 62 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  ble*)sqlite3DbMa
22d12 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
22d13 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69  eof(Table));.  i
22d14 66 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20  f( !pNew ) goto 
22d15 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
22d16 6f 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d  olumn;.  pParse-
22d17 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65  >pNewTable = pNe
22d18 77 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20  w;.  pNew->nRef 
22d19 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d  = 1;.  pNew->dbM
22d1a 65 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d  em = pTab->dbMem
22d1b 3b 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d  ;.  pNew->nCol =
22d1c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61   pTab->nCol;.  a
22d1d 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f  ssert( pNew->nCo
22d1e 6c 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20  l>0 );.  nAlloc 
22d1f 3d 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d  = (((pNew->nCol-
22d20 31 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73  1)/8)*8)+8;.  as
22d21 73 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e  sert( nAlloc>=pN
22d22 65 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c  ew->nCol && nAll
22d23 6f 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f  oc%8==0 && nAllo
22d24 63 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29  c-pNew->nCol<8 )
22d25 3b 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d  ;.  pNew->aCol =
22d26 20 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65   (Column*)sqlite
22d27 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
22d28 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29  , sizeof(Column)
22d29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77  *nAlloc);.  pNew
22d2a 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
22d2b 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71  3MPrintf(db, "sq
22d2c 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73  lite_altertab_%s
22d2d 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
22d2e 0a 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43  .  if( !pNew->aC
22d2f 6f 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61  ol || !pNew->zNa
22d30 6d 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  me ){.    db->ma
22d31 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
22d32 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65      goto exit_be
22d33 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
22d34 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65    }.  memcpy(pNe
22d35 77 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61  w->aCol, pTab->a
22d36 43 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75  Col, sizeof(Colu
22d37 6d 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b  mn)*pNew->nCol);
22d38 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e  .  for(i=0; i<pN
22d39 65 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ew->nCol; i++){.
22d3a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c      Column *pCol
22d3b 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69   = &pNew->aCol[i
22d3c 5d 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61  ];.    pCol->zNa
22d3d 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
22d3e 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  rDup(db, pCol->z
22d3f 4e 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d  Name);.    pCol-
22d40 3e 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  >zColl = 0;.    
22d41 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b  pCol->zType = 0;
22d42 0a 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  .    pCol->pDflt
22d43 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e   = 0;.    pCol->
22d44 7a 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  zDflt = 0;.  }. 
22d45 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d   pNew->pSchema =
22d46 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
22d47 63 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61  chema;.  pNew->a
22d48 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54  ddColOffset = pT
22d49 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74  ab->addColOffset
22d4a 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d  ;.  pNew->nRef =
22d4b 20 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20   1;..  /* Begin 
22d4c 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  a transaction an
22d4d 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  d increment the 
22d4e 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20  schema cookie.  
22d4f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  */.  sqlite3Begi
22d50 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
22d51 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
22d52 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
22d53 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
22d54 20 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65   if( !v ) goto e
22d55 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
22d56 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43  lumn;.  sqlite3C
22d57 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
22d58 73 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f  se, iDb);..exit_
22d59 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
22d5a 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
22d5b 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72  stDelete(db, pSr
22d5c 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  c);.  return;.}.
22d5d 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
22d5e 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f  E_ALTER_TABLE */
22d5f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
22d60 2a 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63  * End of alter.c
22d61 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
22d62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d64 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
22d65 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61  * Begin file ana
22d66 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lyze.c *********
22d67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d69 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c  /./*.** 2005 Jul
22d6a 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 8.**.** The au
22d6b 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
22d6c 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
22d6d 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
22d6e 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
22d6f 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
22d70 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
22d71 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
22d72 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
22d73 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
22d74 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
22d75 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
22d76 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
22d77 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
22d78 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
22d79 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
22d7a 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
22d7b 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
22d7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d80 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
22d81 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73  contains code as
22d82 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
22d83 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e  e ANALYZE comman
22d84 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
22d85 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
22d86 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  E../*.** This ro
22d87 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
22d88 63 6f 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20  code that opens 
22d89 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
22d8a 20 74 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 77 72   table for.** wr
22d8b 69 74 69 6e 67 20 77 69 74 68 20 63 75 72 73 6f  iting with curso
22d8c 72 20 69 53 74 61 74 43 75 72 2e 20 49 66 20 74  r iStatCur. If t
22d8d 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 62  he library was b
22d8e 75 69 6c 74 20 77 69 74 68 20 74 68 65 0a 2a 2a  uilt with the.**
22d8f 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
22d90 54 41 54 32 20 6d 61 63 72 6f 20 64 65 66 69 6e  TAT2 macro defin
22d91 65 64 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c  ed, then the sql
22d92 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20  ite_stat2 table 
22d93 69 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72  is.** opened for
22d94 20 77 72 69 74 69 6e 67 20 75 73 69 6e 67 20 63   writing using c
22d95 75 72 73 6f 72 20 28 69 53 74 61 74 43 75 72 2b  ursor (iStatCur+
22d96 31 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  1).**.** If the 
22d97 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22d98 6c 65 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65  les does not pre
22d99 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69  viously exist, i
22d9a 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a  t is created..**
22d9b 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74   Similarly, if t
22d9c 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
22d9d 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65  table does not e
22d9e 78 69 73 74 20 61 6e 64 20 74 68 65 20 6c 69 62  xist and the lib
22d9f 72 61 72 79 0a 2a 2a 20 69 73 20 63 6f 6d 70 69  rary.** is compi
22da0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  led with SQLITE_
22da1 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 64 65 66  ENABLE_STAT2 def
22da2 69 6e 65 64 2c 20 69 74 20 69 73 20 63 72 65 61  ined, it is crea
22da3 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 41 72 67 75  ted. .**.** Argu
22da4 6d 65 6e 74 20 7a 57 68 65 72 65 20 6d 61 79 20  ment zWhere may 
22da5 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
22da6 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  a buffer contain
22da7 69 6e 67 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ing a table name
22da8 2c 0a 2a 2a 20 6f 72 20 69 74 20 6d 61 79 20 62  ,.** or it may b
22da9 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
22daa 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e  . If it is not N
22dab 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 65 6e  ULL, then all en
22dac 74 72 69 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20  tries in.** the 
22dad 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64  sqlite_stat1 and
22dae 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29   (if applicable)
22daf 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
22db0 62 6c 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a  bles associated.
22db1 2a 2a 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ** with the name
22db2 64 20 74 61 62 6c 65 20 61 72 65 20 64 65 6c 65  d table are dele
22db3 74 65 64 2e 20 49 66 20 7a 57 68 65 72 65 3d 3d  ted. If zWhere==
22db4 30 2c 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20  0, then code is 
22db5 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 74 6f 20  generated.** to 
22db6 64 65 6c 65 74 65 20 61 6c 6c 20 73 74 61 74 20  delete all stat 
22db7 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 0a 2a  table entries..*
22db8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70  /.static void op
22db9 65 6e 53 74 61 74 54 61 62 6c 65 28 0a 20 20 50  enStatTable(.  P
22dba 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
22dbb 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
22dbc 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
22dbd 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
22dbe 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
22dbf 74 61 62 61 73 65 20 77 65 20 61 72 65 20 6c 6f  tabase we are lo
22dc0 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e  oking in */.  in
22dc1 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 20  t iStatCur,     
22dc2 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68        /* Open th
22dc3 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
22dc4 61 62 6c 65 20 6f 6e 20 74 68 69 73 20 63 75 72  able on this cur
22dc5 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sor */.  const c
22dc6 68 61 72 20 2a 7a 57 68 65 72 65 20 20 20 20 20  har *zWhere     
22dc7 20 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 72 69   /* Delete entri
22dc8 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
22dc9 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  th this table */
22dca 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72  .){.  static str
22dcb 75 63 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  uct {.    const 
22dcc 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20  char *zName;.   
22dcd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
22dce 6c 73 3b 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d  ls;.  } aTable[]
22dcf 20 3d 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69   = {.    { "sqli
22dd0 74 65 5f 73 74 61 74 31 22 2c 20 22 74 62 6c 2c  te_stat1", "tbl,
22dd1 69 64 78 2c 73 74 61 74 22 20 7d 2c 0a 23 69 66  idx,stat" },.#if
22dd2 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
22dd3 45 5f 53 54 41 54 32 0a 20 20 20 20 7b 20 22 73  E_STAT2.    { "s
22dd4 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20 22 74  qlite_stat2", "t
22dd5 62 6c 2c 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c  bl,idx,sampleno,
22dd6 73 61 6d 70 6c 65 22 20 7d 2c 0a 23 65 6e 64 69  sample" },.#endi
22dd7 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 61 52  f.  };..  int aR
22dd8 6f 6f 74 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a  oot[] = {0, 0};.
22dd9 20 20 75 38 20 61 43 72 65 61 74 65 54 62 6c 5b    u8 aCreateTbl[
22dda 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 0a 20 20 69  ] = {0, 0};..  i
22ddb 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20  nt i;.  sqlite3 
22ddc 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
22ddd 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 56  ;.  Db *pDb;.  V
22dde 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
22ddf 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
22de0 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
22de1 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
22de2 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
22de3 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
22de4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
22de5 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64  ite3VdbeDb(v)==d
22de6 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  b );.  pDb = &db
22de7 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 66  ->aDb[iDb];..  f
22de8 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53  or(i=0; i<ArrayS
22de9 69 7a 65 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b  ize(aTable); i++
22dea 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
22deb 72 20 2a 7a 54 61 62 20 3d 20 61 54 61 62 6c 65  r *zTab = aTable
22dec 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 54  [i].zName;.    T
22ded 61 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 20  able *pStat;.   
22dee 20 69 66 28 20 28 70 53 74 61 74 20 3d 20 73 71   if( (pStat = sq
22def 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22df0 62 2c 20 7a 54 61 62 2c 20 70 44 62 2d 3e 7a 4e  b, zTab, pDb->zN
22df1 61 6d 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ame))==0 ){.    
22df2 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f    /* The sqlite_
22df3 73 74 61 74 5b 31 32 5d 20 74 61 62 6c 65 20 64  stat[12] table d
22df4 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 43  oes not exist. C
22df5 72 65 61 74 65 20 69 74 2e 20 4e 6f 74 65 20 74  reate it. Note t
22df6 68 61 74 20 61 20 0a 20 20 20 20 20 20 2a 2a 20  hat a .      ** 
22df7 73 69 64 65 2d 65 66 66 65 63 74 20 6f 66 20 74  side-effect of t
22df8 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
22df9 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 6f 20  statement is to 
22dfa 6c 65 61 76 65 20 74 68 65 20 72 6f 6f 74 70 61  leave the rootpa
22dfb 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ge .      ** of 
22dfc 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  the new table in
22dfd 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65   register pParse
22dfe 2d 3e 72 65 67 52 6f 6f 74 2e 20 54 68 69 73 20  ->regRoot. This 
22dff 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 0a 20 20  is important .  
22e00 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74      ** because t
22e01 68 65 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63  he OpenWrite opc
22e02 6f 64 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62  ode below will b
22e03 65 20 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f  e needing it. */
22e04 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65  .      sqlite3Ne
22e05 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
22e06 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45  ,.          "CRE
22e07 41 54 45 20 54 41 42 4c 45 20 25 51 2e 25 73 28  ATE TABLE %Q.%s(
22e08 25 73 29 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65  %s)", pDb->zName
22e09 2c 20 7a 54 61 62 2c 20 61 54 61 62 6c 65 5b 69  , zTab, aTable[i
22e0a 5d 2e 7a 43 6f 6c 73 0a 20 20 20 20 20 20 29 3b  ].zCols.      );
22e0b 0a 20 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20  .      aRoot[i] 
22e0c 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  = pParse->regRoo
22e0d 74 3b 0a 20 20 20 20 20 20 61 43 72 65 61 74 65  t;.      aCreate
22e0e 54 62 6c 5b 69 5d 20 3d 20 31 3b 0a 20 20 20 20  Tbl[i] = 1;.    
22e0f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
22e10 54 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64  The table alread
22e11 79 20 65 78 69 73 74 73 2e 20 49 66 20 7a 57 68  y exists. If zWh
22e12 65 72 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  ere is not NULL,
22e13 20 64 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72   delete all entr
22e14 69 65 73 20 0a 20 20 20 20 20 20 2a 2a 20 61 73  ies .      ** as
22e15 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
22e16 65 20 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20  e table zWhere. 
22e17 49 66 20 7a 57 68 65 72 65 20 69 73 20 4e 55 4c  If zWhere is NUL
22e18 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20  L, delete the.  
22e19 20 20 20 20 2a 2a 20 65 6e 74 69 72 65 20 63 6f      ** entire co
22e1a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61  ntents of the ta
22e1b 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 52  ble. */.      aR
22e1c 6f 6f 74 5b 69 5d 20 3d 20 70 53 74 61 74 2d 3e  oot[i] = pStat->
22e1d 74 6e 75 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69  tnum;.      sqli
22e1e 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
22e1f 72 73 65 2c 20 69 44 62 2c 20 61 52 6f 6f 74 5b  rse, iDb, aRoot[
22e20 69 5d 2c 20 31 2c 20 7a 54 61 62 29 3b 0a 20 20  i], 1, zTab);.  
22e21 20 20 20 20 69 66 28 20 7a 57 68 65 72 65 20 29      if( zWhere )
22e22 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
22e23 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
22e24 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rse,.           
22e25 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
22e26 25 73 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22  %s WHERE tbl=%Q"
22e27 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54  , pDb->zName, zT
22e28 61 62 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 20  ab, zWhere.     
22e29 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73     );.      }els
22e2a 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
22e2b 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32  e sqlite_stat[12
22e2c 5d 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  ] table already 
22e2d 65 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20  exists.  Delete 
22e2e 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20  all rows. */.   
22e2f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22e30 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65  AddOp2(v, OP_Cle
22e31 61 72 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44  ar, aRoot[i], iD
22e32 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
22e33 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  }.  }..  /* Open
22e34 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
22e35 5b 31 32 5d 20 74 61 62 6c 65 73 20 66 6f 72 20  [12] tables for 
22e36 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 66 6f  writing. */.  fo
22e37 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69  r(i=0; i<ArraySi
22e38 7a 65 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29  ze(aTable); i++)
22e39 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
22e3a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
22e3b 65 6e 57 72 69 74 65 2c 20 69 53 74 61 74 43 75  enWrite, iStatCu
22e3c 72 2b 69 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69  r+i, aRoot[i], i
22e3d 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
22e3e 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
22e3f 2d 31 2c 20 28 63 68 61 72 20 2a 29 33 2c 20 50  -1, (char *)3, P
22e40 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71  4_INT32);.    sq
22e41 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
22e42 35 28 76 2c 20 61 43 72 65 61 74 65 54 62 6c 5b  5(v, aCreateTbl[
22e43 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  i]);.  }.}../*.*
22e44 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
22e45 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  to do an analysi
22e46 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73  s of all indices
22e47 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
22e48 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62  .** a single tab
22e49 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
22e4a 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62  id analyzeOneTab
22e4b 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
22e4c 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72  rse,   /* Parser
22e4d 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
22e4e 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f  ble *pTab,     /
22e4f 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e  * Table whose in
22e50 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20  dices are to be 
22e51 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e  analyzed */.  in
22e52 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f  t iStatCur,    /
22e53 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43  * Index of VdbeC
22e54 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65  ursor that write
22e55 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  s the sqlite_sta
22e56 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  t1 table */.  in
22e57 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f  t iMem         /
22e58 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f  * Available memo
22e59 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67  ry locations beg
22e5a 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  in here */.){.  
22e5b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
22e5c 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 2f 2a 20  arse->db;    /* 
22e5d 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
22e5e 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
22e5f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22e60 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f    /* An index to
22e61 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20   being analyzed 
22e62 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72  */.  int iIdxCur
22e63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22e64 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e    /* Cursor open
22e65 20 6f 6e 20 69 6e 64 65 78 20 62 65 69 6e 67 20   on index being 
22e66 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 56 64  analyzed */.  Vd
22e67 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
22e68 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22e69 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
22e6a 65 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70  e being built up
22e6b 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
22e6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e6d 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
22e6e 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 4f  er */.  int topO
22e6f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20  fLoop;          
22e70 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20       /* The top 
22e71 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
22e72 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20   int endOfLoop; 
22e73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22e74 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   The end of the 
22e75 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64  loop */.  int ad
22e76 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
22e77 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 64         /* The ad
22e78 64 72 65 73 73 20 6f 66 20 61 6e 20 69 6e 73 74  dress of an inst
22e79 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  ruction */.  int
22e7a 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
22e7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
22e7c 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  ex of database c
22e7d 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a  ontaining pTab *
22e7e 2f 0a 20 20 69 6e 74 20 72 65 67 54 61 62 6e 61  /.  int regTabna
22e7f 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  me = iMem++;    
22e80 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e   /* Register con
22e81 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 6e 61  taining table na
22e82 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49  me */.  int regI
22e83 64 78 6e 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b  dxname = iMem++;
22e84 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
22e85 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 64 65   containing inde
22e86 78 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  x name */.  int 
22e87 72 65 67 53 61 6d 70 6c 65 6e 6f 20 3d 20 69 4d  regSampleno = iM
22e88 65 6d 2b 2b 3b 20 20 20 20 2f 2a 20 52 65 67 69  em++;    /* Regi
22e89 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
22e8a 6e 65 78 74 20 73 61 6d 70 6c 65 20 6e 75 6d 62  next sample numb
22e8b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43  er */.  int regC
22e8c 6f 6c 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  ol = iMem++;    
22e8d 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
22e8e 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 61 6e 61 6c  of a column anal
22e8f 79 7a 65 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  yzed table */.  
22e90 69 6e 74 20 72 65 67 52 65 63 20 3d 20 69 4d 65  int regRec = iMe
22e91 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  m++;         /* 
22e92 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
22e93 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f 72   completed recor
22e94 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54 65  d */.  int regTe
22e95 6d 70 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  mp = iMem++;    
22e96 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79      /* Temporary
22e97 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f   use register */
22e98 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 20  .  int regRowid 
22e99 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20  = iMem++;       
22e9a 2f 2a 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65  /* Rowid for the
22e9b 20 69 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64   inserted record
22e9c 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
22e9d 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a  TE_ENABLE_STAT2.
22e9e 20 20 69 6e 74 20 72 65 67 54 65 6d 70 32 20 3d    int regTemp2 =
22e9f 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f   iMem++;       /
22ea0 2a 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20  * Temporary use 
22ea1 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e  register */.  in
22ea2 74 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f  t regSamplerecno
22ea3 20 3d 20 69 4d 65 6d 2b 2b 3b 20 2f 2a 20 49 6e   = iMem++; /* In
22ea4 64 65 78 20 6f 66 20 6e 65 78 74 20 73 61 6d 70  dex of next samp
22ea5 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a  le to record */.
22ea6 20 20 69 6e 74 20 72 65 67 52 65 63 6e 6f 20 3d    int regRecno =
22ea7 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f   iMem++;       /
22ea8 2a 20 43 75 72 72 65 6e 74 20 73 61 6d 70 6c 65  * Current sample
22ea9 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
22eaa 72 65 67 4c 61 73 74 20 3d 20 69 4d 65 6d 2b 2b  regLast = iMem++
22eab 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  ;        /* Inde
22eac 78 20 6f 66 20 6c 61 73 74 20 73 61 6d 70 6c 65  x of last sample
22ead 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20   to record */.  
22eae 69 6e 74 20 72 65 67 46 69 72 73 74 20 3d 20 69  int regFirst = i
22eaf 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
22eb0 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20 73  Index of first s
22eb1 61 6d 70 6c 65 20 74 6f 20 72 65 63 6f 72 64 20  ample to record 
22eb2 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d  */.#endif..  v =
22eb3 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
22eb4 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
22eb5 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 54 61  ==0 || NEVER(pTa
22eb6 62 3d 3d 30 29 20 7c 7c 20 70 54 61 62 2d 3e 70  b==0) || pTab->p
22eb7 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Index==0 ){.    
22eb8 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69  /* Do no analysi
22eb9 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 68 61  s for tables tha
22eba 74 20 68 61 76 65 20 6e 6f 20 69 6e 64 69 63 65  t have no indice
22ebb 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b  s */.    return;
22ebc 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73  .  }.  assert( s
22ebd 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
22ebe 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29  AllMutexes(db) )
22ebf 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
22ec0 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
22ec1 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
22ec2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
22ec3 3e 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  >=0 );.#ifndef S
22ec4 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
22ec5 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73  RIZATION.  if( s
22ec6 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
22ec7 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
22ec8 4e 41 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e  NALYZE, pTab->zN
22ec9 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62  ame, 0,.      db
22eca 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
22ecb 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e   ) ){.    return
22ecc 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
22ecd 2f 2a 20 45 73 74 61 62 6c 69 73 68 20 61 20 72  /* Establish a r
22ece 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead-lock on the 
22ecf 74 61 62 6c 65 20 61 74 20 74 68 65 20 73 68 61  table at the sha
22ed0 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e  red-cache level.
22ed1 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62   */.  sqlite3Tab
22ed2 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
22ed3 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
22ed4 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
22ed5 0a 0a 20 20 69 49 64 78 43 75 72 20 3d 20 70 50  ..  iIdxCur = pP
22ed6 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
22ed7 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
22ed8 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
22ed9 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
22eda 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70      int nCol = p
22edb 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Idx->nColumn;.  
22edc 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20    KeyInfo *pKey 
22edd 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65  = sqlite3IndexKe
22ede 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49  yinfo(pParse, pI
22edf 64 78 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 4d  dx);..    if( iM
22ee0 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70 50  em+1+(nCol*2)>pP
22ee1 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20  arse->nMem ){.  
22ee2 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
22ee3 20 3d 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a   = iMem+1+(nCol*
22ee4 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  2);.    }..    /
22ee5 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20  * Open a cursor 
22ee6 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20  to the index to 
22ee7 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a  be analyzed. */.
22ee8 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3d      assert( iDb=
22ee9 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f  =sqlite3SchemaTo
22eea 49 6e 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e  Index(db, pIdx->
22eeb 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 20 20  pSchema) );.    
22eec 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22eed 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  4(v, OP_OpenRead
22eee 2c 20 69 49 64 78 43 75 72 2c 20 70 49 64 78 2d  , iIdxCur, pIdx-
22eef 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20  >tnum, iDb,.    
22ef0 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79      (char *)pKey
22ef1 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
22ef2 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43  DOFF);.    VdbeC
22ef3 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
22ef4 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pIdx->zName));.
22ef5 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65  .    /* Populate
22ef6 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20 63   the registers c
22ef7 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
22ef8 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 6e 61  ble and index na
22ef9 6d 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  mes. */.    if( 
22efa 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49  pTab->pIndex==pI
22efb 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  dx ){.      sqli
22efc 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
22efd 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
22efe 72 65 67 54 61 62 6e 61 6d 65 2c 20 30 2c 20 70  regTabname, 0, p
22eff 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
22f00 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
22f01 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22f02 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65  P_String8, 0, re
22f03 67 49 64 78 6e 61 6d 65 2c 20 30 2c 20 70 49 64  gIdxname, 0, pId
22f04 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 23  x->zName, 0);..#
22f05 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
22f06 42 4c 45 5f 53 54 41 54 32 0a 0a 20 20 20 20 2f  BLE_STAT2..    /
22f07 2a 20 49 66 20 74 68 69 73 20 69 74 65 72 61 74  * If this iterat
22f08 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  ion of the loop 
22f09 69 73 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  is generating co
22f0a 64 65 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  de to analyze th
22f0b 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69  e.    ** first i
22f0c 6e 64 65 78 20 69 6e 20 74 68 65 20 70 54 61 62  ndex in the pTab
22f0d 2d 3e 70 49 6e 64 65 78 20 6c 69 73 74 2c 20 74  ->pIndex list, t
22f0e 68 65 6e 20 72 65 67 69 73 74 65 72 20 72 65 67  hen register reg
22f0f 4c 61 73 74 20 68 61 73 0a 20 20 20 20 2a 2a 20  Last has.    ** 
22f10 6e 6f 74 20 62 65 65 6e 20 70 6f 70 75 6c 61 74  not been populat
22f11 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
22f12 20 70 6f 70 75 6c 61 74 65 20 69 74 20 6e 6f 77   populate it now
22f13 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54  .  */.    if( pT
22f14 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78  ab->pIndex==pIdx
22f15 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
22f16 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22f17 50 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54  P_Integer, SQLIT
22f18 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2c  E_INDEX_SAMPLES,
22f19 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29   regSamplerecno)
22f1a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
22f1b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22f1c 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f  Integer, SQLITE_
22f1d 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2d  INDEX_SAMPLES*2-
22f1e 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  1, regTemp);.   
22f1f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f20 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22f21 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  er, SQLITE_INDEX
22f22 5f 53 41 4d 50 4c 45 53 2a 32 2c 20 72 65 67 54  _SAMPLES*2, regT
22f23 65 6d 70 32 29 3b 0a 0a 20 20 20 20 20 20 73 71  emp2);..      sq
22f24 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22f25 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64  v, OP_Count, iId
22f26 78 43 75 72 2c 20 72 65 67 4c 61 73 74 29 3b 0a  xCur, regLast);.
22f27 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22f28 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
22f29 6c 6c 2c 20 30 2c 20 72 65 67 46 69 72 73 74 29  ll, 0, regFirst)
22f2a 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
22f2b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22f2c 28 76 2c 20 4f 50 5f 4c 74 2c 20 72 65 67 53 61  (v, OP_Lt, regSa
22f2d 6d 70 6c 65 72 65 63 6e 6f 2c 20 30 2c 20 72 65  mplerecno, 0, re
22f2e 67 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73 71  gLast);.      sq
22f2f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22f30 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 72 65  v, OP_Divide, re
22f31 67 54 65 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c  gTemp2, regLast,
22f32 20 72 65 67 46 69 72 73 74 29 3b 0a 20 20 20 20   regFirst);.    
22f33 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22f34 4f 70 33 28 76 2c 20 4f 50 5f 4d 75 6c 74 69 70  Op3(v, OP_Multip
22f35 6c 79 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67  ly, regLast, reg
22f36 54 65 6d 70 2c 20 72 65 67 4c 61 73 74 29 3b 0a  Temp, regLast);.
22f37 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22f38 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
22f39 64 49 6d 6d 2c 20 72 65 67 4c 61 73 74 2c 20 53  dImm, regLast, S
22f3a 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
22f3b 4c 45 53 2a 32 2d 32 29 3b 0a 20 20 20 20 20 20  LES*2-2);.      
22f3c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22f3d 33 28 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20  3(v, OP_Divide, 
22f3e 20 72 65 67 54 65 6d 70 32 2c 20 72 65 67 4c 61   regTemp2, regLa
22f3f 73 74 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20  st, regLast);.  
22f40 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
22f41 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
22f42 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
22f43 5a 65 72 6f 20 74 68 65 20 72 65 67 53 61 6d 70  Zero the regSamp
22f44 6c 65 6e 6f 20 61 6e 64 20 72 65 67 52 65 63 6e  leno and regRecn
22f45 6f 20 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a  o registers. */.
22f46 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22f47 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
22f48 67 65 72 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c  ger, 0, regSampl
22f49 65 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eno);.    sqlite
22f4a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22f4b 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65  P_Integer, 0, re
22f4c 67 52 65 63 6e 6f 29 3b 0a 20 20 20 20 73 71 6c  gRecno);.    sql
22f4d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22f4e 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 46 69  , OP_Copy, regFi
22f4f 72 73 74 2c 20 72 65 67 53 61 6d 70 6c 65 72 65  rst, regSamplere
22f50 63 6e 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  cno);.#endif..  
22f51 20 20 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 20 6f    /* The block o
22f52 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 69  f memory cells i
22f53 6e 69 74 69 61 6c 69 7a 65 64 20 68 65 72 65 20  nitialized here 
22f54 69 73 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f  is used as follo
22f55 77 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ws..    **.    *
22f56 2a 20 20 20 20 69 4d 65 6d 3a 20 20 20 20 20 20  *    iMem:      
22f57 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 2a            .    *
22f58 2a 20 20 20 20 20 20 20 20 54 68 65 20 74 6f 74  *        The tot
22f59 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  al number of row
22f5a 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
22f5b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
22f5c 20 69 4d 65 6d 2b 31 20 2e 2e 20 69 4d 65 6d 2b   iMem+1 .. iMem+
22f5d 6e 43 6f 6c 3a 20 0a 20 20 20 20 2a 2a 20 20 20  nCol: .    **   
22f5e 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64       Number of d
22f5f 69 73 74 69 6e 63 74 20 65 6e 74 72 69 65 73 20  istinct entries 
22f60 69 6e 20 69 6e 64 65 78 20 63 6f 6e 73 69 64 65  in index conside
22f61 72 69 6e 67 20 74 68 65 20 0a 20 20 20 20 2a 2a  ring the .    **
22f62 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73          left-mos
22f63 74 20 4e 20 63 6f 6c 75 6d 6e 73 20 6f 6e 6c 79  t N columns only
22f64 2c 20 77 68 65 72 65 20 4e 20 69 73 20 62 65 74  , where N is bet
22f65 77 65 65 6e 20 31 20 61 6e 64 20 6e 43 6f 6c 2c  ween 1 and nCol,
22f66 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
22f67 69 6e 63 6c 75 73 69 76 65 2e 0a 20 20 20 20 2a  inclusive..    *
22f68 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d  *.    **    iMem
22f69 2b 6e 43 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b 32  +nCol+1 .. Mem+2
22f6a 2a 6e 43 6f 6c 3a 20 20 0a 20 20 20 20 2a 2a 20  *nCol:  .    ** 
22f6b 20 20 20 20 20 20 20 50 72 65 76 69 6f 75 73 20         Previous 
22f6c 76 61 6c 75 65 20 6f 66 20 69 6e 64 65 78 65 64  value of indexed
22f6d 20 63 6f 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20 6c   columns, from l
22f6e 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 20 20  eft to right..  
22f6f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c    **.    ** Cell
22f70 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69  s iMem through i
22f71 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69  Mem+nCol are ini
22f72 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 54  tialized to 0. T
22f73 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 0a 20  he others are . 
22f74 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65     ** initialize
22f75 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20  d to contain an 
22f76 53 51 4c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f  SQL NULL..    */
22f77 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
22f78 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  =nCol; i++){.   
22f79 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f7a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22f7b 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a  er, 0, iMem+i);.
22f7c 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
22f7d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
22f7e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22f7f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
22f80 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f  ull, 0, iMem+nCo
22f81 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20  l+i+1);.    }.. 
22f82 20 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20     /* Start the 
22f83 61 6e 61 6c 79 73 69 73 20 6c 6f 6f 70 2e 20 54  analysis loop. T
22f84 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 74 68  his loop runs th
22f85 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 65 6e  rough all the en
22f86 74 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20  tries in.    ** 
22f87 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65  the index b-tree
22f88 2e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c  .  */.    endOfL
22f89 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
22f8a 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
22f8b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f8c 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
22f8d 64 2c 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f  d, iIdxCur, endO
22f8e 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f  fLoop);.    topO
22f8f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  fLoop = sqlite3V
22f90 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
22f91 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22f92 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
22f93 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b  ddImm, iMem, 1);
22f94 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
22f95 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
22f96 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f97 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
22f98 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72  n, iIdxCur, i, r
22f99 65 67 43 6f 6c 29 3b 0a 23 69 66 64 65 66 20 53  egCol);.#ifdef S
22f9a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
22f9b 54 32 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  T2.      if( i==
22f9c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
22f9d 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 65 63  Check if the rec
22f9e 6f 72 64 20 74 68 61 74 20 63 75 72 73 6f 72 20  ord that cursor 
22f9f 69 49 64 78 43 75 72 20 70 6f 69 6e 74 73 20 74  iIdxCur points t
22fa0 6f 20 63 6f 6e 74 61 69 6e 73 20 61 0a 20 20 20  o contains a.   
22fa1 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 74 68       ** value th
22fa2 61 74 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f  at should be sto
22fa3 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  red in the sqlit
22fa4 65 5f 73 74 61 74 32 20 74 61 62 6c 65 2e 20 49  e_stat2 table. I
22fa5 66 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a  f so,.        **
22fa6 20 73 74 6f 72 65 20 69 74 2e 20 20 2a 2f 0a 20   store it.  */. 
22fa7 20 20 20 20 20 20 20 69 6e 74 20 6e 65 20 3d 20         int ne = 
22fa8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22fa9 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 52  3(v, OP_Ne, regR
22faa 65 63 6e 6f 2c 20 30 2c 20 72 65 67 53 61 6d 70  ecno, 0, regSamp
22fab 6c 65 72 65 63 6e 6f 29 3b 0a 20 20 20 20 20 20  lerecno);.      
22fac 20 20 61 73 73 65 72 74 28 20 72 65 67 54 61 62    assert( regTab
22fad 6e 61 6d 65 2b 31 3d 3d 72 65 67 49 64 78 6e 61  name+1==regIdxna
22fae 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  me .            
22faf 20 26 26 20 72 65 67 54 61 62 6e 61 6d 65 2b 32   && regTabname+2
22fb0 3d 3d 72 65 67 53 61 6d 70 6c 65 6e 6f 0a 20 20  ==regSampleno.  
22fb1 20 20 20 20 20 20 20 20 20 20 20 26 26 20 72 65             && re
22fb2 67 54 61 62 6e 61 6d 65 2b 33 3d 3d 72 65 67 43  gTabname+3==regC
22fb3 6f 6c 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20  ol.        );.  
22fb4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22fb5 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c  eChangeP5(v, SQL
22fb6 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
22fb7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22fb8 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22fb9 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
22fba 54 61 62 6e 61 6d 65 2c 20 34 2c 20 72 65 67 52  Tabname, 4, regR
22fbb 65 63 2c 20 22 61 61 61 62 22 2c 20 30 29 3b 0a  ec, "aaab", 0);.
22fbc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22fbd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22fbe 4e 65 77 52 6f 77 69 64 2c 20 69 53 74 61 74 43  NewRowid, iStatC
22fbf 75 72 2b 31 2c 20 72 65 67 52 6f 77 69 64 29 3b  ur+1, regRowid);
22fc0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22fc1 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22fc2 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75  _Insert, iStatCu
22fc3 72 2b 31 2c 20 72 65 67 52 65 63 2c 20 72 65 67  r+1, regRec, reg
22fc4 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 20 20 20  Rowid);..       
22fc5 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 6e 65   /* Calculate ne
22fc6 77 20 76 61 6c 75 65 73 20 66 6f 72 20 72 65 67  w values for reg
22fc7 53 61 6d 70 6c 65 72 65 63 6e 6f 20 61 6e 64 20  Samplerecno and 
22fc8 72 65 67 53 61 6d 70 6c 65 6e 6f 2e 0a 20 20 20  regSampleno..   
22fc9 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
22fca 2a 2a 20 20 20 73 61 6d 70 6c 65 6e 6f 20 3d 20  **   sampleno = 
22fcb 73 61 6d 70 6c 65 6e 6f 20 2b 20 31 0a 20 20 20  sampleno + 1.   
22fcc 20 20 20 20 20 2a 2a 20 20 20 73 61 6d 70 6c 65       **   sample
22fcd 72 65 63 6e 6f 20 3d 20 73 61 6d 70 6c 65 72 65  recno = samplere
22fce 63 6e 6f 2b 28 72 65 6d 61 69 6e 69 6e 67 20 72  cno+(remaining r
22fcf 65 63 6f 72 64 73 29 2f 28 72 65 6d 61 69 6e 69  ecords)/(remaini
22fd0 6e 67 20 73 61 6d 70 6c 65 73 29 0a 20 20 20 20  ng samples).    
22fd1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
22fd2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22fd3 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
22fd4 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 31 29 3b 0a  egSampleno, 1);.
22fd5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22fd6 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22fd7 53 75 62 74 72 61 63 74 2c 20 72 65 67 52 65 63  Subtract, regRec
22fd8 6e 6f 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67  no, regLast, reg
22fd9 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73  Temp);.        s
22fda 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22fdb 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
22fdc 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20  egTemp, -1);.   
22fdd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22fde 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
22fdf 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44  eger, SQLITE_IND
22fe0 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 54  EX_SAMPLES, regT
22fe1 65 6d 70 32 29 3b 0a 20 20 20 20 20 20 20 20 73  emp2);.        s
22fe2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22fe3 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c  (v, OP_Subtract,
22fe4 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65   regSampleno, re
22fe5 67 54 65 6d 70 32 2c 20 72 65 67 54 65 6d 70 32  gTemp2, regTemp2
22fe6 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22fe7 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22fe8 4f 50 5f 44 69 76 69 64 65 2c 20 72 65 67 54 65  OP_Divide, regTe
22fe9 6d 70 32 2c 20 72 65 67 54 65 6d 70 2c 20 72 65  mp2, regTemp, re
22fea 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 20 20  gTemp);.        
22feb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22fec 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 72 65 67  3(v, OP_Add, reg
22fed 53 61 6d 70 6c 65 72 65 63 6e 6f 2c 20 72 65 67  Samplerecno, reg
22fee 54 65 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 72  Temp, regSampler
22fef 65 63 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 20 20  ecno);..        
22ff0 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
22ff1 65 72 65 28 76 2c 20 6e 65 29 3b 0a 20 20 20 20  ere(v, ne);.    
22ff2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22ff3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
22ff4 6d 6d 2c 20 72 65 67 52 65 63 6e 6f 2c 20 31 29  mm, regRecno, 1)
22ff5 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
22ff6 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
22ff7 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22ff8 4e 65 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69  Ne, regCol, 0, i
22ff9 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20  Mem+nCol+i+1);. 
22ffa 20 20 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a       /**** TODO:
22ffb 20 20 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20    add collating 
22ffc 73 65 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a  sequence *****/.
22ffd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22ffe 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c  eChangeP5(v, SQL
22fff 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
23000 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
23001 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
23002 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ){.      /* If a
23003 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
23004 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68  has occurred, th
23005 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
23006 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
23007 0a 20 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64  .      ** passed
23008 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
23009 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63  rgument to the c
2300a 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64  all to sqlite3Vd
2300b 62 65 4a 75 6d 70 48 65 72 65 28 29 20 0a 20 20  beJumpHere() .  
2300c 20 20 20 20 2a 2a 20 62 65 6c 6f 77 20 6d 61 79      ** below may
2300d 20 62 65 20 6e 65 67 61 74 69 76 65 2e 20 57 68   be negative. Wh
2300e 69 63 68 20 63 61 75 73 65 73 20 61 6e 20 61 73  ich causes an as
2300f 73 65 72 74 28 29 20 74 6f 20 66 61 69 6c 20 28  sert() to fail (
23010 6f 72 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f  or an.      ** o
23011 75 74 2d 6f 66 2d 62 6f 75 6e 64 73 20 77 72 69  ut-of-bounds wri
23012 74 65 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42  te if SQLITE_DEB
23013 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  UG is not define
23014 64 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 72 65  d).  */.      re
23015 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
23016 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23017 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
23018 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   endOfLoop);.   
23019 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
2301a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
2301b 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2301c 65 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65  e(v, sqlite3Vdbe
2301d 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 28  CurrentAddr(v)-(
2301e 6e 43 6f 6c 2a 32 29 29 3b 0a 20 20 20 20 20 20  nCol*2));.      
2301f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23020 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
23021 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20  iMem+i+1, 1);.  
23022 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23023 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
23024 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20  mn, iIdxCur, i, 
23025 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a  iMem+nCol+i+1);.
23026 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e      }..    /* En
23027 64 20 6f 66 20 74 68 65 20 61 6e 61 6c 79 73 69  d of the analysi
23028 73 20 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 73  s loop. */.    s
23029 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2302a 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
2302b 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oop);.    sqlite
2302c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2302d 50 5f 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c  P_Next, iIdxCur,
2302e 20 74 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20   topOfLoop);.   
2302f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23030 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
23031 69 49 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f  iIdxCur);..    /
23032 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75  * Store the resu
23033 6c 74 73 20 69 6e 20 73 71 6c 69 74 65 5f 73 74  lts in sqlite_st
23034 61 74 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  at1..    **.    
23035 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  ** The result is
23036 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
23037 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
23038 31 20 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69  1 table.  The fi
23039 72 73 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63  rst.    ** two c
2303a 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e  olumns are the n
2303b 61 6d 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ames of the tabl
2303c 65 20 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68  e and index.  Th
2303d 65 20 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20  e third column. 
2303e 20 20 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e     ** is a strin
2303f 67 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20  g composed of a 
23040 6c 69 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20  list of integer 
23041 73 74 61 74 69 73 74 69 63 73 20 61 62 6f 75 74  statistics about
23042 20 74 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65   the.    ** inde
23043 78 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e  x.  The first in
23044 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73  teger in the lis
23045 74 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  t is the total n
23046 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
23047 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69  .    ** in the i
23048 6e 64 65 78 2e 20 20 54 68 65 72 65 20 69 73 20  ndex.  There is 
23049 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  one additional i
2304a 6e 74 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69  nteger in the li
2304b 73 74 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20  st for each.    
2304c 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ** column of the
2304d 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64   table.  This ad
2304e 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72  ditional integer
2304f 20 69 73 20 61 20 67 75 65 73 73 20 6f 66 20 68   is a guess of h
23050 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72  ow many.    ** r
23051 6f 77 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ows of the table
23052 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20   the index will 
23053 73 65 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73  select.  If D is
23054 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69   the count of di
23055 73 74 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61  stinct.    ** va
23056 6c 75 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68  lues and K is th
23057 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
23058 66 20 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65  f rows, then the
23059 20 69 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70   integer is comp
2305a 75 74 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a  uted.    ** as:.
2305b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2305c 20 20 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29       I = (K+D-1)
2305d 2f 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  /D.    **.    **
2305e 20 49 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f   If K==0 then no
2305f 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69   entry is made i
23060 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73  nto the sqlite_s
23061 74 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20  tat1 table.  .  
23062 20 20 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e    ** If K>0 then
23063 20 69 74 20 69 73 20 61 6c 77 61 79 73 20 74 68   it is always th
23064 65 20 63 61 73 65 20 74 68 65 20 44 3e 30 20 73  e case the D>0 s
23065 6f 20 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65  o division by ze
23066 72 6f 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76  ro.    ** is nev
23067 65 72 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20  er possible..   
23068 20 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73   */.    addr = s
23069 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2306a 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d  (v, OP_IfNot, iM
2306b 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  em);.    sqlite3
2306c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2306d 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 65  _SCopy, iMem, re
2306e 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20  gSampleno);.    
2306f 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
23070 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
23071 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
23072 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
23073 20 72 65 67 54 65 6d 70 2c 20 30 2c 20 22 20 22   regTemp, 0, " "
23074 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
23075 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23076 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54   OP_Concat, regT
23077 65 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f  emp, regSampleno
23078 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a  , regSampleno);.
23079 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2307a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64  eAddOp3(v, OP_Ad
2307b 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b  d, iMem, iMem+i+
2307c 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  1, regTemp);.   
2307d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2307e 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2307f 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b  m, regTemp, -1);
23080 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23081 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44  beAddOp3(v, OP_D
23082 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c  ivide, iMem+i+1,
23083 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d   regTemp, regTem
23084 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
23085 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
23086 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70  P_ToInt, regTemp
23087 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
23088 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23089 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70  _Concat, regTemp
2308a 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72  , regSampleno, r
2308b 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20  egSampleno);.   
2308c 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2308d 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
2308e 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61  akeRecord, regTa
2308f 62 6e 61 6d 65 2c 20 33 2c 20 72 65 67 52 65 63  bname, 3, regRec
23090 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a 20 20 20  , "aaa", 0);.   
23091 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23092 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
23093 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72 65 67  d, iStatCur, reg
23094 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
23095 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23096 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61   OP_Insert, iSta
23097 74 43 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65  tCur, regRec, re
23098 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  gRowid);.    sql
23099 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2309a 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  (v, OPFLAG_APPEN
2309b 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  D);.    sqlite3V
2309c 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2309d 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ddr);.  }.}../*.
2309e 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2309f 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75 73 65   that will cause
230a0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
230a1 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69 73 20   index analysis 
230a2 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 64 20  to.** be laoded 
230a3 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61  into internal ha
230a4 73 68 20 74 61 62 6c 65 73 20 77 68 65 72 65 20  sh tables where 
230a5 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  is can be used..
230a6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
230a7 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61 72 73  oadAnalysis(Pars
230a8 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
230a9 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  Db){.  Vdbe *v =
230aa 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
230ab 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
230ac 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
230ad 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
230ae 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20 69 44  LoadAnalysis, iD
230af 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  b);.  }.}../*.**
230b0 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
230b1 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61  hat will do an a
230b2 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20 65 6e  nalysis of an en
230b3 74 69 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2f  tire database.*/
230b4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61  .static void ana
230b5 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50 61 72  lyzeDatabase(Par
230b6 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
230b7 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  iDb){.  sqlite3 
230b8 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
230b9 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  ;.  Schema *pSch
230ba 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ema = db->aDb[iD
230bb 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20 20 2f  b].pSchema;    /
230bc 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61 74 61  * Schema of data
230bd 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 48 61  base iDb */.  Ha
230be 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74  shElem *k;.  int
230bf 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69 6e 74   iStatCur;.  int
230c0 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65   iMem;..  sqlite
230c1 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
230c2 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
230c3 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72  iDb);.  iStatCur
230c4 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
230c5 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20  .  pParse->nTab 
230c6 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74  += 2;.  openStat
230c7 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
230c8 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b  b, iStatCur, 0);
230c9 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  .  iMem = pParse
230ca 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28  ->nMem+1;.  for(
230cb 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
230cc 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  t(&pSchema->tblH
230cd 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74  ash); k; k=sqlit
230ce 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20  eHashNext(k)){. 
230cf 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
230d0 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48   (Table*)sqliteH
230d1 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20  ashData(k);.    
230d2 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
230d3 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53  pParse, pTab, iS
230d4 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20  tatCur, iMem);. 
230d5 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69   }.  loadAnalysi
230d6 73 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  s(pParse, iDb);.
230d7 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
230d8 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
230d9 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   do an analysis 
230da 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
230db 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61  e in.** a databa
230dc 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
230dd 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28  id analyzeTable(
230de 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
230df 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69  able *pTab){.  i
230e0 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53  nt iDb;.  int iS
230e1 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72  tatCur;..  asser
230e2 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20  t( pTab!=0 );.  
230e3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
230e4 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
230e5 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20  xes(pParse->db) 
230e6 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
230e7 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
230e8 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
230e9 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ->pSchema);.  sq
230ea 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
230eb 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
230ec 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61   0, iDb);.  iSta
230ed 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tCur = pParse->n
230ee 54 61 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  Tab;.  pParse->n
230ef 54 61 62 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e  Tab += 2;.  open
230f0 53 74 61 74 54 61 62 6c 65 28 70 50 61 72 73 65  StatTable(pParse
230f1 2c 20 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c  , iDb, iStatCur,
230f2 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
230f3 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65   analyzeOneTable
230f4 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69  (pParse, pTab, i
230f5 53 74 61 74 43 75 72 2c 20 70 50 61 72 73 65 2d  StatCur, pParse-
230f6 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64  >nMem+1);.  load
230f7 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c  Analysis(pParse,
230f8 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iDb);.}../*.** 
230f9 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
230fa 72 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f  r the ANALYZE co
230fb 6d 6d 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73  mmand.  The pars
230fc 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
230fd 75 74 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74  utine.** when it
230fe 20 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41   recognizes an A
230ff 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a  NALYZE command..
23100 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41  **.**        ANA
23101 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20  LYZE            
23102 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23103 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 41  -- 1.**        A
23104 4e 41 4c 59 5a 45 20 20 3c 64 61 74 61 62 61 73  NALYZE  <databas
23105 65 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e>              
23106 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20    -- 2.**       
23107 20 41 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61   ANALYZE  ?<data
23108 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d  base>.?<tablenam
23109 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46  e>  -- 3.**.** F
2310a 6f 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c  orm 1 causes all
2310b 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20   indices in all 
2310c 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
2310d 65 73 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  es to be analyze
2310e 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61  d..** Form 2 ana
2310f 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65  lyzes all indice
23110 73 20 74 68 65 20 73 69 6e 67 6c 65 20 64 61 74  s the single dat
23111 61 62 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20  abase named..** 
23112 46 6f 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20  Form 3 analyzes 
23113 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f  all indices asso
23114 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
23115 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a  named table..*/.
23116 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23117 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  oid sqlite3Analy
23118 7a 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ze(Parse *pParse
23119 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c  , Token *pName1,
2311a 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b   Token *pName2){
2311b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2311c 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
2311d 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b  nt iDb;.  int i;
2311e 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62  .  char *z, *zDb
2311f 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
23120 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
23121 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  Name;..  /* Read
23122 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
23123 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f  hema. If an erro
23124 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20  r occurs, leave 
23125 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
23126 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69  .  ** and code i
23127 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
23128 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61  urn NULL. */.  a
23129 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
2312a 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
2312b 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
2312c 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
2312d 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63  K!=sqlite3ReadSc
2312e 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
2312f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
23130 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
23131 32 21 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d  2!=0 || pName1==
23132 30 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65  0 );.  if( pName
23133 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  1==0 ){.    /* F
23134 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 1:  Analyze 
23135 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20  everything */.  
23136 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
23137 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
23138 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e    if( i==1 ) con
23139 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f  tinue;  /* Do no
2313a 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45  t analyze the TE
2313b 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
2313c 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
2313d 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b  base(pParse, i);
2313e 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
2313f 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20  f( pName2->n==0 
23140 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32  ){.    /* Form 2
23141 3a 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64  :  Analyze the d
23142 61 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65  atabase or table
23143 20 6e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44   named */.    iD
23144 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  b = sqlite3FindD
23145 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20  b(db, pName1);. 
23146 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b     if( iDb>=0 ){
23147 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61  .      analyzeDa
23148 74 61 62 61 73 65 28 70 50 61 72 73 65 2c 20 69  tabase(pParse, i
23149 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Db);.    }else{.
2314a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
2314b 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2314c 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
2314d 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
2314e 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
2314f 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
23150 72 73 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20  rse, 0, z, 0);. 
23151 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
23152 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
23153 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
23154 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79  .          analy
23155 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  zeTable(pParse, 
23156 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d  pTab);.        }
23157 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
23158 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46   }else{.    /* F
23159 6f 72 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74  orm 3: Analyze t
2315a 68 65 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69  he fully qualifi
2315b 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  ed table name */
2315c 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
2315d 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
2315e 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
2315f 61 6d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d  ame2, &pTableNam
23160 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e  e);.    if( iDb>
23161 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20  =0 ){.      zDb 
23162 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
23163 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20  Name;.      z = 
23164 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
23165 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e  oken(db, pTableN
23166 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
23167 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  z ){.        pTa
23168 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
23169 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
2316a 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20  , z, zDb);.     
2316b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2316c 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20  (db, z);.       
2316d 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
2316e 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61         analyzeTa
2316f 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
23170 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
23171 20 20 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20     }.    }   .  
23172 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20  }.}../*.** Used 
23173 74 6f 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74  to pass informat
23174 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61  ion from the ana
23175 6c 79 7a 65 72 20 72 65 61 64 65 72 20 74 68 72  lyzer reader thr
23176 6f 75 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63  ough to the.** c
23177 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e  allback routine.
23178 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
23179 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20  ct analysisInfo 
2317a 61 6e 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74  analysisInfo;.st
2317b 72 75 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66  ruct analysisInf
2317c 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  o {.  sqlite3 *d
2317d 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
2317e 2a 7a 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a  *zDatabase;.};..
2317f 2f 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62  /*.** This callb
23180 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f  ack is invoked o
23181 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64  nce for each ind
23182 65 78 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20  ex when reading 
23183 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74  the.** sqlite_st
23184 61 74 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a  at1 table.  .**.
23185 2a 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d  **     argv[0] =
23186 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   name of the ind
23187 65 78 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31  ex.**     argv[1
23188 5d 20 3d 20 72 65 73 75 6c 74 73 20 6f 66 20 61  ] = results of a
23189 6e 61 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74  nalysis - on int
2318a 65 67 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f  eger for each co
2318b 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lumn.*/.static i
2318c 6e 74 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65  nt analysisLoade
2318d 72 28 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69  r(void *pData, i
2318e 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
2318f 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74  argv, char **Not
23190 55 73 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69  Used){.  analysi
23191 73 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28  sInfo *pInfo = (
23192 61 6e 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44  analysisInfo*)pD
23193 61 74 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  ata;.  Index *pI
23194 6e 64 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63  ndex;.  int i, c
23195 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ;.  unsigned int
23196 20 76 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   v;.  const char
23197 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *z;..  assert( 
23198 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55  argc==2 );.  UNU
23199 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
2319a 6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a  otUsed, argc);..
2319b 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c    if( argv==0 ||
2319c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61   argv[0]==0 || a
2319d 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20  rgv[1]==0 ){.   
2319e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2319f 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65   pIndex = sqlite
231a0 33 46 69 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f  3FindIndex(pInfo
231a1 2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70  ->db, argv[0], p
231a2 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29  Info->zDatabase)
231a3 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d  ;.  if( pIndex==
231a4 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
231a5 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67  0;.  }.  z = arg
231a6 76 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  v[1];.  for(i=0;
231a7 20 2a 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65 78   *z && i<=pIndex
231a8 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
231a9 0a 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20  .    v = 0;.    
231aa 77 68 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e  while( (c=z[0])>
231ab 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
231ac 7b 0a 20 20 20 20 20 20 76 20 3d 20 76 2a 31 30  {.      v = v*10
231ad 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20   + c - '0';.    
231ae 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20    z++;.    }.   
231af 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73   pIndex->aiRowEs
231b0 74 5b 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66  t[i] = v;.    if
231b1 28 20 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b  ( *z==' ' ) z++;
231b2 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
231b3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
231b4 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c 65 20 76   Index.aSample v
231b5 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 4e  ariable is not N
231b6 55 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 20  ULL, delete the 
231b7 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79 0a  aSample[] array.
231b8 2a 2a 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65  ** and its conte
231b9 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
231ba 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
231bb 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61  te3DeleteIndexSa
231bc 6d 70 6c 65 73 28 49 6e 64 65 78 20 2a 70 49 64  mples(Index *pId
231bd 78 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  x){.#ifdef SQLIT
231be 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20  E_ENABLE_STAT2. 
231bf 20 69 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70   if( pIdx->aSamp
231c0 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b  le ){.    int j;
231c1 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
231c2 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Mem = pIdx->pTab
231c3 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 66  le->dbMem;.    f
231c4 6f 72 28 6a 3d 30 3b 20 6a 3c 53 51 4c 49 54 45  or(j=0; j<SQLITE
231c5 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20  _INDEX_SAMPLES; 
231c6 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 49 6e 64 65  j++){.      Inde
231c7 78 53 61 6d 70 6c 65 20 2a 70 20 3d 20 26 70 49  xSample *p = &pI
231c8 64 78 2d 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b 0a  dx->aSample[j];.
231c9 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 54 79        if( p->eTy
231ca 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20  pe==SQLITE_TEXT 
231cb 7c 7c 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c  || p->eType==SQL
231cc 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  ITE_BLOB ){.    
231cd 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
231ce 65 28 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e  e(pIdx->pTable->
231cf 64 62 4d 65 6d 2c 20 70 2d 3e 75 2e 7a 29 3b 0a  dbMem, p->u.z);.
231d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
231d1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
231d2 64 62 4d 65 6d 2c 20 70 49 64 78 2d 3e 61 53 61  dbMem, pIdx->aSa
231d3 6d 70 6c 65 29 3b 0a 20 20 20 20 70 49 64 78 2d  mple);.    pIdx-
231d4 3e 61 53 61 6d 70 6c 65 20 3d 20 30 3b 0a 20 20  >aSample = 0;.  
231d5 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  }.#else.  UNUSED
231d6 5f 50 41 52 41 4d 45 54 45 52 28 70 49 64 78 29  _PARAMETER(pIdx)
231d7 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
231d8 2a 20 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65  * Load the conte
231d9 6e 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  nt of the sqlite
231da 5f 73 74 61 74 31 20 61 6e 64 20 73 71 6c 69 74  _stat1 and sqlit
231db 65 5f 73 74 61 74 32 20 74 61 62 6c 65 73 2e 20  e_stat2 tables. 
231dc 54 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20  The.** contents 
231dd 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  of sqlite_stat1 
231de 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75  are used to popu
231df 6c 61 74 65 20 74 68 65 20 49 6e 64 65 78 2e 61  late the Index.a
231e0 69 52 6f 77 45 73 74 5b 5d 0a 2a 2a 20 61 72 72  iRowEst[].** arr
231e1 61 79 73 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74  ays. The content
231e2 73 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74  s of sqlite_stat
231e3 32 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f  2 are used to po
231e4 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 49 6e  pulate the.** In
231e5 64 65 78 2e 61 53 61 6d 70 6c 65 5b 5d 20 61 72  dex.aSample[] ar
231e6 72 61 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rays..**.** If t
231e7 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
231e8 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 70 72 65  table is not pre
231e9 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61  sent in the data
231ea 62 61 73 65 2c 20 53 51 4c 49 54 45 5f 45 52 52  base, SQLITE_ERR
231eb 4f 52 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  OR.** is returne
231ec 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  d. In this case,
231ed 20 65 76 65 6e 20 69 66 20 53 51 4c 49 54 45 5f   even if SQLITE_
231ee 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 77 61 73  ENABLE_STAT2 was
231ef 20 64 65 66 69 6e 65 64 20 0a 2a 2a 20 64 75 72   defined .** dur
231f0 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ing compilation 
231f1 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73  and the sqlite_s
231f2 74 61 74 32 20 74 61 62 6c 65 20 69 73 20 70 72  tat2 table is pr
231f3 65 73 65 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69  esent, no data i
231f4 73 20 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20  s .** read from 
231f5 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  it..**.** If SQL
231f6 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
231f7 20 77 61 73 20 64 65 66 69 6e 65 64 20 64 75 72   was defined dur
231f8 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ing compilation 
231f9 61 6e 64 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69  and the .** sqli
231fa 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69  te_stat2 table i
231fb 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
231fc 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 53   the database, S
231fd 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 0a 2a  QLITE_ERROR is.*
231fe 2a 20 72 65 74 75 72 6e 65 64 2e 20 48 6f 77 65  * returned. Howe
231ff 76 65 72 2c 20 69 6e 20 74 68 69 73 20 63 61 73  ver, in this cas
23200 65 2c 20 64 61 74 61 20 69 73 20 72 65 61 64 20  e, data is read 
23201 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
23202 73 74 61 74 31 0a 2a 2a 20 74 61 62 6c 65 20 28  stat1.** table (
23203 69 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74  if it is present
23204 29 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  ) before returni
23205 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
23206 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73  OOM error occurs
23207 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
23208 61 6c 77 61 79 73 20 73 65 74 73 20 64 62 2d 3e  always sets db->
23209 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2a  mallocFailed..**
2320a 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74   This means if t
2320b 68 65 20 63 61 6c 6c 65 72 20 64 6f 65 73 20 6e  he caller does n
2320c 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 6f 74  ot care about ot
2320d 68 65 72 20 65 72 72 6f 72 73 2c 20 74 68 65 20  her errors, the 
2320e 72 65 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 20 6d  return.** code m
2320f 61 79 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a  ay be ignored..*
23210 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23211 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c   int sqlite3Anal
23212 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33  ysisLoad(sqlite3
23213 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a   *db, int iDb){.
23214 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73    analysisInfo s
23215 49 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d  Info;.  HashElem
23216 20 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71   *i;.  char *zSq
23217 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  l;.  int rc;..  
23218 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
23219 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
2321a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
2321b 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29  Db[iDb].pBt!=0 )
2321c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2321d 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
2321e 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ex(db->aDb[iDb].
2321f 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c  pBt) );..  /* Cl
23220 65 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74  ear any prior st
23221 61 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f  atistics */.  fo
23222 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
23223 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62  rst(&db->aDb[iDb
23224 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ].pSchema->idxHa
23225 73 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61  sh);i;i=sqliteHa
23226 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20  shNext(i)){.    
23227 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71  Index *pIdx = sq
23228 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29 3b  liteHashData(i);
23229 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66 61  .    sqlite3Defa
2322a 75 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b  ultRowEst(pIdx);
2322b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
2322c 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 70  teIndexSamples(p
2322d 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Idx);.  }..  /* 
2322e 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  Check to make su
2322f 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  re the sqlite_st
23230 61 74 31 20 74 61 62 6c 65 20 65 78 69 73 74 73  at1 table exists
23231 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d   */.  sInfo.db =
23232 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61   db;.  sInfo.zDa
23233 74 61 62 61 73 65 20 3d 20 64 62 2d 3e 61 44 62  tabase = db->aDb
23234 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69  [iDb].zName;.  i
23235 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  f( sqlite3FindTa
23236 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  ble(db, "sqlite_
23237 73 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44  stat1", sInfo.zD
23238 61 74 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20  atabase)==0 ){. 
23239 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2323a 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
2323b 2a 20 4c 6f 61 64 20 6e 65 77 20 73 74 61 74 69  * Load new stati
2323c 73 74 69 63 73 20 6f 75 74 20 6f 66 20 74 68 65  stics out of the
2323d 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2323e 62 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20  ble */.  zSql = 
2323f 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
23240 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
23241 54 20 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d  T idx, stat FROM
23242 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
23243 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
23244 73 65 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d  se);.  if( zSql=
23245 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
23246 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
23247 65 6c 73 65 7b 0a 20 20 20 20 28 76 6f 69 64 29  else{.    (void)
23248 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
23249 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  (db);.    rc = s
2324a 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
2324b 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f  zSql, analysisLo
2324c 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29  ader, &sInfo, 0)
2324d 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
2324e 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
2324f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
23250 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  ee(db, zSql);.  
23251 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68  }...  /* Load th
23252 65 20 73 74 61 74 69 73 74 69 63 73 20 66 72 6f  e statistics fro
23253 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  m the sqlite_sta
23254 74 32 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 66  t2 table. */.#if
23255 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
23256 45 5f 53 54 41 54 32 0a 20 20 69 66 28 20 72 63  E_STAT2.  if( rc
23257 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
23258 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
23259 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
2325a 74 32 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61  t2", sInfo.zData
2325b 62 61 73 65 29 20 29 7b 0a 20 20 20 20 72 63 20  base) ){.    rc 
2325c 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2325d 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
2325e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
2325f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
23260 6d 74 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 53 71  mt = 0;..    zSq
23261 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
23262 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20  tf(db, .        
23263 22 53 45 4c 45 43 54 20 69 64 78 2c 73 61 6d 70  "SELECT idx,samp
23264 6c 65 6e 6f 2c 73 61 6d 70 6c 65 20 46 52 4f 4d  leno,sample FROM
23265 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 32   %Q.sqlite_stat2
23266 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
23267 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 53  se);.    if( !zS
23268 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ql ){.      rc =
23269 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2326a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2326b 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2326c 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
2326d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
2326e 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c  repare(db, zSql,
2326f 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
23270 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
23271 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
23272 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
23273 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b  bFree(db, zSql);
23274 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
23275 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
23276 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
23277 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
23278 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
23279 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
2327a 6d 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  mt)==SQLITE_ROW 
2327b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
2327c 2a 7a 49 6e 64 65 78 20 3d 20 28 63 68 61 72 20  *zIndex = (char 
2327d 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
2327e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 3b  _text(pStmt, 0);
2327f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a  .        Index *
23280 70 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69  pIdx = sqlite3Fi
23281 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64  ndIndex(db, zInd
23282 65 78 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62  ex, sInfo.zDatab
23283 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ase);.        if
23284 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
23285 20 20 20 20 69 6e 74 20 69 53 61 6d 70 6c 65 20      int iSample 
23286 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
23287 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 29 3b 0a  _int(pStmt, 1);.
23288 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23289 33 20 2a 64 62 4d 65 6d 20 3d 20 70 49 64 78 2d  3 *dbMem = pIdx-
2328a 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a  >pTable->dbMem;.
2328b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2328c 28 20 64 62 4d 65 6d 3d 3d 64 62 20 7c 7c 20 64  ( dbMem==db || d
2328d 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20 20 20 20 20  bMem==0 );.     
2328e 20 20 20 20 20 69 66 28 20 69 53 61 6d 70 6c 65       if( iSample
2328f 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  <SQLITE_INDEX_SA
23290 4d 50 4c 45 53 20 26 26 20 69 53 61 6d 70 6c 65  MPLES && iSample
23291 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
23292 20 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 73     int eType = s
23293 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
23294 70 65 28 70 53 74 6d 74 2c 20 32 29 3b 0a 0a 20  pe(pStmt, 2);.. 
23295 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
23296 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20  Idx->aSample==0 
23297 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
23298 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e   static const in
23299 74 20 73 7a 20 3d 20 73 69 7a 65 6f 66 28 49 6e  t sz = sizeof(In
2329a 64 65 78 53 61 6d 70 6c 65 29 2a 53 51 4c 49 54  dexSample)*SQLIT
2329b 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b  E_INDEX_SAMPLES;
2329c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2329d 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 28  Idx->aSample = (
2329e 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 29 73 71  IndexSample *)sq
2329f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
232a0 6f 28 64 62 4d 65 6d 2c 20 73 7a 29 3b 0a 20 20  o(dbMem, sz);.  
232a1 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
232a2 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30  pIdx->aSample==0
232a3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
232a4 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
232a5 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
232a6 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
232a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
232a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  .            }..
232a9 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
232aa 72 74 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c  rt( pIdx->aSampl
232ab 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  e );.           
232ac 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
232ad 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70 53   IndexSample *pS
232ae 61 6d 70 6c 65 20 3d 20 26 70 49 64 78 2d 3e 61  ample = &pIdx->a
232af 53 61 6d 70 6c 65 5b 69 53 61 6d 70 6c 65 5d 3b  Sample[iSample];
232b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
232b1 53 61 6d 70 6c 65 2d 3e 65 54 79 70 65 20 3d 20  Sample->eType = 
232b2 28 75 38 29 65 54 79 70 65 3b 0a 20 20 20 20 20  (u8)eType;.     
232b3 20 20 20 20 20 20 20 20 20 69 66 28 20 65 54 79           if( eTy
232b4 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47  pe==SQLITE_INTEG
232b5 45 52 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c  ER || eType==SQL
232b6 49 54 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20  ITE_FLOAT ){.   
232b7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61               pSa
232b8 6d 70 6c 65 2d 3e 75 2e 72 20 3d 20 73 71 6c 69  mple->u.r = sqli
232b9 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c  te3_column_doubl
232ba 65 28 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20  e(pStmt, 2);.   
232bb 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
232bc 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49   if( eType==SQLI
232bd 54 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65  TE_TEXT || eType
232be 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
232bf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
232c1 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 28   (const char *)(
232c2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232c3 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c       (eType==SQL
232c4 49 54 45 5f 42 4c 4f 42 29 20 3f 0a 20 20 20 20  ITE_BLOB) ?.    
232c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232c6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
232c7 6c 6f 62 28 70 53 74 6d 74 2c 20 32 29 3a 0a 20  lob(pStmt, 2):. 
232c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232c9 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
232ca 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 32 29  n_text(pStmt, 2)
232cb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232cc 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
232cd 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
232ce 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
232cf 28 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20  (pStmt, 2);.    
232d0 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
232d1 6e 3e 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20  n>24 ){.        
232d2 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 32 34            n = 24
232d3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
232d4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
232d5 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 42 79      pSample->nBy
232d6 74 65 20 3d 20 28 75 38 29 6e 3b 0a 20 20 20 20  te = (u8)n;.    
232d7 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61 6d              pSam
232d8 70 6c 65 2d 3e 75 2e 7a 20 3d 20 73 71 6c 69 74  ple->u.z = sqlit
232d9 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
232da 4d 65 6d 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  Mem, n);.       
232db 20 20 20 20 20 20 20 20 20 69 66 28 20 70 53 61           if( pSa
232dc 6d 70 6c 65 2d 3e 75 2e 7a 20 29 7b 0a 20 20 20  mple->u.z ){.   
232dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
232de 65 6d 63 70 79 28 70 53 61 6d 70 6c 65 2d 3e 75  emcpy(pSample->u
232df 2e 7a 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20  .z, z, n);.     
232e0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
232e1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
232e2 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
232e3 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
232e4 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
232e5 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
232e6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
232e7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
232e8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
232e9 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
232ea 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
232eb 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
232ec 6d 74 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64  mt);.      (void
232ed 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
232ee 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  (db);.    }.  }.
232ef 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63  #endif..  if( rc
232f0 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
232f1 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
232f2 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
232f3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
232f4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
232f5 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20 2a  E_OMIT_ANALYZE *
232f6 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
232f7 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61 6c 79 7a  ** End of analyz
232f8 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
232f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232fb 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
232fc 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 74  ** Begin file at
232fd 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tach.c *********
232fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23300 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70  */./*.** 2003 Ap
23301 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ril 6.**.** The 
23302 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
23303 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
23304 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
23305 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
23306 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
23307 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
23308 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
23309 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2330a 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2330b 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2330c 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2330d 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2330e 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2330f 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
23310 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
23311 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
23312 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
23313 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23314 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23315 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23316 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23317 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
23318 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
23319 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
2331a 74 20 74 68 65 20 41 54 54 41 43 48 20 61 6e 64  t the ATTACH and
2331b 20 44 45 54 41 43 48 20 63 6f 6d 6d 61 6e 64 73   DETACH commands
2331c 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
2331d 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
2331e 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61  ./*.** Resolve a
2331f 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
23320 74 20 77 61 73 20 70 61 72 74 20 6f 66 20 61 6e  t was part of an
23321 20 41 54 54 41 43 48 20 6f 72 20 44 45 54 41 43   ATTACH or DETAC
23322 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69  H statement. Thi
23323 73 0a 2a 2a 20 69 73 20 73 6c 69 67 68 74 6c 79  s.** is slightly
23324 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
23325 72 65 73 6f 6c 76 69 6e 67 20 61 20 6e 6f 72 6d  resolving a norm
23326 61 6c 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f  al SQL expressio
23327 6e 2c 20 62 65 63 61 75 73 65 20 73 69 6d 70 6c  n, because simpl
23328 65 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 73  e.** identifiers
23329 20 61 72 65 20 74 72 65 61 74 65 64 20 61 73 20   are treated as 
2332a 73 74 72 69 6e 67 73 2c 20 6e 6f 74 20 70 6f 73  strings, not pos
2332b 73 69 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  sible column nam
2332c 65 73 20 6f 72 20 61 6c 69 61 73 65 73 2e 0a 2a  es or aliases..*
2332d 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66 20 74 68 65  *.** i.e. if the
2332e 20 70 61 72 73 65 72 20 73 65 65 73 3a 0a 2a 2a   parser sees:.**
2332f 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44  .**     ATTACH D
23330 41 54 41 42 41 53 45 20 61 62 63 20 41 53 20 64  ATABASE abc AS d
23331 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20 74 72 65 61  ef.**.** it trea
23332 74 73 20 74 68 65 20 74 77 6f 20 65 78 70 72 65  ts the two expre
23333 73 73 69 6f 6e 73 20 61 73 20 6c 69 74 65 72 61  ssions as litera
23334 6c 20 73 74 72 69 6e 67 73 20 27 61 62 63 27 20  l strings 'abc' 
23335 61 6e 64 20 27 64 65 66 27 20 69 6e 73 74 65 61  and 'def' instea
23336 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20  d of.** looking 
23337 66 6f 72 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  for columns of t
23338 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a  he same name..**
23339 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 61 70  .** This only ap
2333a 70 6c 69 65 73 20 74 6f 20 74 68 65 20 72 6f 6f  plies to the roo
2333b 74 20 6e 6f 64 65 20 6f 66 20 70 45 78 70 72 2c  t node of pExpr,
2333c 20 73 6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e   so the statemen
2333d 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54  t:.**.**     ATT
2333e 41 43 48 20 44 41 54 41 42 41 53 45 20 61 62 63  ACH DATABASE abc
2333f 7c 7c 64 65 66 20 41 53 20 27 64 62 32 27 0a 2a  ||def AS 'db2'.*
23340 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 62  *.** will fail b
23341 65 63 61 75 73 65 20 6e 65 69 74 68 65 72 20 61  ecause neither a
23342 62 63 20 6f 72 20 64 65 66 20 63 61 6e 20 62 65  bc or def can be
23343 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74   resolved..*/.st
23344 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
23345 41 74 74 61 63 68 45 78 70 72 28 4e 61 6d 65 43  AttachExpr(NameC
23346 6f 6e 74 65 78 74 20 2a 70 4e 61 6d 65 2c 20 45  ontext *pName, E
23347 78 70 72 20 2a 70 45 78 70 72 29 0a 7b 0a 20 20  xpr *pExpr).{.  
23348 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
23349 4f 4b 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20  OK;.  if( pExpr 
2334a 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72  ){.    if( pExpr
2334b 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20 29 7b 0a 20  ->op!=TK_ID ){. 
2334c 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2334d 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2334e 73 28 70 4e 61 6d 65 2c 20 70 45 78 70 72 29 3b  s(pName, pExpr);
2334f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
23350 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c  QLITE_OK && !sql
23351 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
23352 6e 74 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  nt(pExpr) ){.   
23353 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
23354 72 4d 73 67 28 70 4e 61 6d 65 2d 3e 70 50 61 72  rMsg(pName->pPar
23355 73 65 2c 20 22 69 6e 76 61 6c 69 64 20 6e 61 6d  se, "invalid nam
23356 65 3a 20 5c 22 25 73 5c 22 22 2c 20 70 45 78 70  e: \"%s\"", pExp
23357 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
23358 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
23359 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
2335a 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
2335b 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
2335c 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 7d  TK_STRING;.    }
2335d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2335e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51  ;.}../*.** An SQ
2335f 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  L user-function 
23360 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f  registered to do
23361 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20   the work of an 
23362 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  ATTACH statement
23363 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61  . The.** three a
23364 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
23365 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69  function come di
23366 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20 61  rectly from an a
23367 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a  ttach statement:
23368 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43  .**.**     ATTAC
23369 48 20 44 41 54 41 42 41 53 45 20 78 20 41 53 20  H DATABASE x AS 
2336a 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20 20  y KEY z.**.**   
2336b 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 5f    SELECT sqlite_
2336c 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29 0a  attach(x, y, z).
2336d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 74  **.** If the opt
2336e 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73 79  ional "KEY z" sy
2336f 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64 2c  ntax is omitted,
23370 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73 20   an SQL NULL is 
23371 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a  passed as the.**
23372 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 2e   third argument.
23373 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23374 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73 71  attachFunc(.  sq
23375 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
23376 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
23377 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
23378 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
23379 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2337a 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  rc = 0;.  sqlite
2337b 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2337c 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2337d 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 6f  e(context);.  co
2337e 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
2337f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23380 46 69 6c 65 3b 0a 20 20 44 62 20 2a 61 4e 65 77  File;.  Db *aNew
23381 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 44 79  ;.  char *zErrDy
23382 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e 55 53 45 44  n = 0;..  UNUSED
23383 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
23384 65 64 29 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d 20  ed);..  zFile = 
23385 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
23386 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
23387 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61  (argv[0]);.  zNa
23388 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
23389 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
2338a 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
2338b 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30 20 29    if( zFile==0 )
2338c 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20 20 69   zFile = "";.  i
2338d 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e  f( zName==0 ) zN
2338e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a 20  ame = "";..  /* 
2338f 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 66 6f  Check for the fo
23390 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 3a 0a  llowing errors:.
23391 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 2a 20    **.  **     * 
23392 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65  Too many attache
23393 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20 20 2a  d databases,.  *
23394 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63 74  *     * Transact
23395 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  ion currently op
23396 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53 70  en.  **     * Sp
23397 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65  ecified database
23398 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62 65   name already be
23399 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f 0a 20  ing used..  */. 
2339a 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d 64 62   if( db->nDb>=db
2339b 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
2339c 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 2b  LIMIT_ATTACHED]+
2339d 32 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e  2 ){.    zErrDyn
2339e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2339f 66 28 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  f(db, "too many 
233a0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
233a1 65 73 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a 20  es - max %d", . 
233a2 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b       db->aLimit[
233a3 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
233a4 41 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a 20 20  ACHED].    );.  
233a5 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
233a6 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  ror;.  }.  if( !
233a7 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
233a8 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20  {.    zErrDyn = 
233a9 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
233aa 62 2c 20 22 63 61 6e 6e 6f 74 20 41 54 54 41 43  b, "cannot ATTAC
233ab 48 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69  H database withi
233ac 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b  n transaction");
233ad 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68  .    goto attach
233ae 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 66 6f  _error;.  }.  fo
233af 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
233b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
233b1 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d   *z = db->aDb[i]
233b2 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 61 73 73 65  .zName;.    asse
233b3 72 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20 29  rt( z && zName )
233b4 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
233b5 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d  3StrICmp(z, zNam
233b6 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a  e)==0 ){.      z
233b7 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
233b8 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61 74  MPrintf(db, "dat
233b9 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72 65  abase %s is alre
233ba 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e 61  ady in use", zNa
233bb 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  me);.      goto 
233bc 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
233bd 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c    }.  }..  /* Al
233be 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20 65  locate the new e
233bf 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d 3e  ntry in the db->
233c0 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64 20  aDb[] array and 
233c1 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20 73  initialise the s
233c2 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68 20  chema.  ** hash 
233c3 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  tables..  */.  i
233c4 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e  f( db->aDb==db->
233c5 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20  aDbStatic ){.   
233c6 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   aNew = sqlite3D
233c7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
233c8 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
233c9 29 2a 33 20 29 3b 0a 20 20 20 20 69 66 28 20 61  )*3 );.    if( a
233ca 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  New==0 ) return;
233cb 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e 65 77  .    memcpy(aNew
233cc 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f  , db->aDb, sizeo
233cd 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 32 29  f(db->aDb[0])*2)
233ce 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
233cf 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
233d0 65 61 6c 6c 6f 63 28 64 62 2c 20 64 62 2d 3e 61  ealloc(db, db->a
233d1 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61  Db, sizeof(db->a
233d2 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b  Db[0])*(db->nDb+
233d3 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61 4e  1) );.    if( aN
233d4 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ew==0 ) return;.
233d5 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20    }.  db->aDb = 
233d6 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d 20 26  aNew;.  aNew = &
233d7 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 5d  db->aDb[db->nDb]
233d8 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c  ;.  memset(aNew,
233d9 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77   0, sizeof(*aNew
233da 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74  ));..  /* Open t
233db 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
233dc 2e 20 49 66 20 74 68 65 20 62 74 72 65 65 20 69  . If the btree i
233dd 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  s successfully o
233de 70 65 6e 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20  pened, use.  ** 
233df 69 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  it to obtain the
233e0 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
233e1 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  . At this point 
233e2 74 68 65 20 73 63 68 65 6d 61 20 6d 61 79 0a 20  the schema may. 
233e3 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62   ** or may not b
233e4 65 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20  e initialised.. 
233e5 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
233e6 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
233e7 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c  b, zFile, 0, SQL
233e8 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
233e9 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 20 20  E_SIZE,.        
233ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
233eb 20 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73     db->openFlags
233ec 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   | SQLITE_OPEN_M
233ed 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20 20  AIN_DB,.        
233ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
233ef 20 20 20 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a     &aNew->pBt);.
233f0 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20 69    db->nDb++;.  i
233f1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f  f( rc==SQLITE_CO
233f2 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 20 20 20  NSTRAINT ){.    
233f3 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
233f4 52 3b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d  R;.    zErrDyn =
233f5 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
233f6 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 69 73  db, "database is
233f7 20 61 6c 72 65 61 64 79 20 61 74 74 61 63 68 65   already attache
233f8 64 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  d");.  }else if(
233f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
233fa 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61  {.    Pager *pPa
233fb 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70  ger;.    aNew->p
233fc 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33  Schema = sqlite3
233fd 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61 4e  SchemaGet(db, aN
233fe 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 66  ew->pBt);.    if
233ff 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61  ( !aNew->pSchema
23400 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
23401 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
23402 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77 2d   }else if( aNew-
23403 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
23404 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e 70  ormat && aNew->p
23405 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43  Schema->enc!=ENC
23406 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 7a 45  (db) ){.      zE
23407 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d  rrDyn = sqlite3M
23408 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20  Printf(db, .    
23409 20 20 20 20 22 61 74 74 61 63 68 65 64 20 64 61      "attached da
2340a 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73 65  tabases must use
2340b 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20 65   the same text e
2340c 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e 20  ncoding as main 
2340d 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20  database");.    
2340e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2340f 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ROR;.    }.    p
23410 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
23411 74 72 65 65 50 61 67 65 72 28 61 4e 65 77 2d 3e  treePager(aNew->
23412 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
23413 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
23414 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66  e(pPager, db->df
23415 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20 20  ltLockMode);.   
23416 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
23417 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c  rnalMode(pPager,
23418 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c   db->dfltJournal
23419 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61 4e 65  Mode);.  }.  aNe
2341a 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  w->zName = sqlit
2341b 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
2341c 4e 61 6d 65 29 3b 0a 20 20 61 4e 65 77 2d 3e 73  Name);.  aNew->s
2341d 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b  afety_level = 3;
2341e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  ..#if SQLITE_HAS
2341f 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 72 63 3d  _CODEC.  if( rc=
23420 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
23421 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
23422 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
23423 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
23424 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29  onst void*, int)
23425 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69  ;.    extern voi
23426 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65  d sqlite3CodecGe
23427 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69  tKey(sqlite3*, i
23428 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a  nt, void**, int*
23429 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b  );.    int nKey;
2342a 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b  .    char *zKey;
2342b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 73 71 6c  .    int t = sql
2342c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2342d 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 73 77  argv[2]);.    sw
2342e 69 74 63 68 28 20 74 20 29 7b 0a 20 20 20 20 20  itch( t ){.     
2342f 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
23430 45 47 45 52 3a 0a 20 20 20 20 20 20 63 61 73 65  EGER:.      case
23431 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a 20   SQLITE_FLOAT:. 
23432 20 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d         zErrDyn =
23433 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
23434 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b 65  (db, "Invalid ke
23435 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20  y value");.     
23436 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
23437 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 62 72  RROR;.        br
23438 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20 20  eak;.        .  
23439 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2343a 54 45 58 54 3a 0a 20 20 20 20 20 20 63 61 73 65  TEXT:.      case
2343b 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20   SQLITE_BLOB:.  
2343c 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 73 71 6c        nKey = sql
2343d 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2343e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20  (argv[2]);.     
2343f 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 61 72 20     zKey = (char 
23440 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
23441 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a 20  blob(argv[2]);. 
23442 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
23443 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 64  te3CodecAttach(d
23444 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b  b, db->nDb-1, zK
23445 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20  ey, nKey);.     
23446 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20     break;..     
23447 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c   case SQLITE_NUL
23448 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  L:.        /* No
23449 20 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e 20   key specified. 
2344a 20 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f   Use the key fro
2344b 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  m the main datab
2344c 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  ase */.        s
2344d 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65  qlite3CodecGetKe
2344e 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a  y(db, 0, (void**
2344f 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a  )&zKey, &nKey);.
23450 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
23451 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
23452 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a  db, db->nDb-1, z
23453 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20  Key, nKey);.    
23454 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
23455 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
23456 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61  * If the file wa
23457 73 20 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73  s opened success
23458 66 75 6c 6c 79 2c 20 72 65 61 64 20 74 68 65 20  fully, read the 
23459 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20 6e  schema for the n
2345a 65 77 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ew database..  *
2345b 2a 20 49 66 20 74 68 69 73 20 66 61 69 6c 73 2c  * If this fails,
2345c 20 6f 72 20 69 66 20 6f 70 65 6e 69 6e 67 20 74   or if opening t
2345d 68 65 20 66 69 6c 65 20 66 61 69 6c 65 64 2c 20  he file failed, 
2345e 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 66  then close the f
2345f 69 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ile and .  ** re
23460 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20 66  move the entry f
23461 72 6f 6d 20 74 68 65 20 64 62 2d 3e 61 44 62 5b  rom the db->aDb[
23462 5d 20 61 72 72 61 79 2e 20 69 2e 65 2e 20 70 75  ] array. i.e. pu
23463 74 20 65 76 65 72 79 74 68 69 6e 67 20 62 61 63  t everything bac
23464 6b 20 74 68 65 20 77 61 79 0a 20 20 2a 2a 20 77  k the way.  ** w
23465 65 20 66 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f  e found it..  */
23466 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
23467 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69  E_OK ){.    (voi
23468 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
23469 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  n(db);.    sqlit
2346a 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
2346b 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  db);.    rc = sq
2346c 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a  lite3Init(db, &z
2346d 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c  ErrDyn);.    sql
2346e 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
2346f 6c 28 64 62 29 3b 0a 20 20 20 20 28 76 6f 69 64  l(db);.    (void
23470 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
23471 66 28 64 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28  f(db);.  }.  if(
23472 20 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   rc ){.    int i
23473 44 62 20 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31  Db = db->nDb - 1
23474 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
23475 62 3e 3d 32 20 29 3b 0a 20 20 20 20 69 66 28 20  b>=2 );.    if( 
23476 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
23477 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23478 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e  3BtreeClose(db->
23479 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20  aDb[iDb].pBt);. 
2347a 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
2347b 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20  ].pBt = 0;.     
2347c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
2347d 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d  chema = 0;.    }
2347e 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
2347f 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
23480 64 62 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e  db, 0);.    db->
23481 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 69  nDb = iDb;.    i
23482 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
23483 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
23484 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
23485 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
23486 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
23487 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
23488 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
23489 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
2348a 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2348b 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
2348c 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ");.    }else if
2348d 28 20 7a 45 72 72 44 79 6e 3d 3d 30 20 29 7b 0a  ( zErrDyn==0 ){.
2348e 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
2348f 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
23490 62 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70  b, "unable to op
23491 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25 73 22  en database: %s"
23492 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  , zFile);.    }.
23493 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
23494 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20 20  error;.  }.  .  
23495 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68 5f  return;..attach_
23496 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74 75  error:.  /* Retu
23497 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 20 77  rn an error if w
23498 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a 20 20  e get here */.  
23499 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a 20  if( zErrDyn ){. 
2349a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2349b 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
2349c 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a 20   zErrDyn, -1);. 
2349d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2349e 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
2349f 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20 73 71   }.  if( rc ) sq
234a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
234a1 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74 2c  or_code(context,
234a2 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41   rc);.}../*.** A
234a3 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74  n SQL user-funct
234a4 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 74  ion registered t
234a5 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
234a6 20 61 6e 20 44 45 54 41 43 48 20 73 74 61 74 65   an DETACH state
234a7 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72  ment. The.** thr
234a8 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
234a9 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  the function com
234aa 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  e directly from 
234ab 61 20 64 65 74 61 63 68 20 73 74 61 74 65 6d 65  a detach stateme
234ac 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45  nt:.**.**     DE
234ad 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78 0a  TACH DATABASE x.
234ae 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
234af 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 78   sqlite_detach(x
234b0 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ).*/.static void
234b1 20 64 65 74 61 63 68 46 75 6e 63 28 0a 20 20 73   detachFunc(.  s
234b2 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
234b3 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
234b4 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
234b5 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
234b6 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
234b7 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
234b8 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
234b9 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
234ba 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
234bb 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
234bc 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
234bd 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  text);.  int i;.
234be 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20    Db *pDb = 0;. 
234bf 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b   char zErr[128];
234c0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
234c1 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
234c2 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
234c3 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20 20 66   zName = "";.  f
234c4 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
234c5 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 44 62  b; i++){.    pDb
234c6 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
234c7 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
234c8 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
234c9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
234ca 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d  trICmp(pDb->zNam
234cb 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  e, zName)==0 ) b
234cc 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  reak;.  }..  if(
234cd 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a 20   i>=db->nDb ){. 
234ce 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
234cf 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
234d0 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20  ,zErr, "no such 
234d1 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a  database: %s", z
234d2 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
234d3 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  detach_error;.  
234d4 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20  }.  if( i<2 ){. 
234d5 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
234d6 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
234d7 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64  ,zErr, "cannot d
234d8 65 74 61 63 68 20 64 61 74 61 62 61 73 65 20 25  etach database %
234d9 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
234da 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
234db 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  r;.  }.  if( !db
234dc 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
234dd 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
234de 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
234df 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20  ), zErr,.       
234e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
234e1 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64 61 74  annot DETACH dat
234e2 61 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61  abase within tra
234e3 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
234e4 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
234e5 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  r;.  }.  if( sql
234e6 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
234e7 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 29  dTrans(pDb->pBt)
234e8 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
234e9 49 73 49 6e 42 61 63 6b 75 70 28 70 44 62 2d 3e  IsInBackup(pDb->
234ea 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  pBt) ){.    sqli
234eb 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
234ec 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
234ed 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73 20  "database %s is 
234ee 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b  locked", zName);
234ef 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
234f0 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73  _error;.  }..  s
234f1 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
234f2 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44  (pDb->pBt);.  pD
234f3 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70 44  b->pBt = 0;.  pD
234f4 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
234f5 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
234f6 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
234f7 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a   0);.  return;..
234f8 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20  detach_error:.  
234f9 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
234fa 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
234fb 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  rr, -1);.}../*.*
234fc 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 72 65  * This procedure
234fd 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
234fe 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c  code for a singl
234ff 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  e invocation of 
23500 65 69 74 68 65 72 20 74 68 65 0a 2a 2a 20 73 71  either the.** sq
23501 6c 69 74 65 5f 64 65 74 61 63 68 28 29 20 6f 72  lite_detach() or
23502 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 29   sqlite_attach()
23503 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69   SQL user functi
23504 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
23505 6f 69 64 20 63 6f 64 65 41 74 74 61 63 68 28 0a  oid codeAttach(.
23506 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23507 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
23508 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
23509 20 20 69 6e 74 20 74 79 70 65 2c 20 20 20 20 20    int type,     
2350a 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72         /* Either
2350b 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 6f   SQLITE_ATTACH o
2350c 72 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20  r SQLITE_DETACH 
2350d 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46  */.  FuncDef *pF
2350e 75 6e 63 2c 20 20 20 20 20 20 2f 2a 20 46 75 6e  unc,      /* Fun
2350f 63 44 65 66 20 77 72 61 70 70 65 72 20 66 6f 72  cDef wrapper for
23510 20 64 65 74 61 63 68 46 75 6e 63 28 29 20 6f 72   detachFunc() or
23511 20 61 74 74 61 63 68 46 75 6e 63 28 29 20 2a 2f   attachFunc() */
23512 0a 20 20 45 78 70 72 20 2a 70 41 75 74 68 41 72  .  Expr *pAuthAr
23513 67 2c 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  g,      /* Expre
23514 73 73 69 6f 6e 20 74 6f 20 70 61 73 73 20 74 6f  ssion to pass to
23515 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
23516 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 45 78 70  allback */.  Exp
23517 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *pFilename,   
23518 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
23519 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
2351a 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 20  Expr *pDbname,  
2351b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2351c 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
2351d 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a  use internally *
2351e 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65 79 20 20  /.  Expr *pKey  
2351f 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
23520 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65 6e 63  base key for enc
23521 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73 69 6f  ryption extensio
23522 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  n */.){.  int rc
23523 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
23524 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65 20 2a 76  sName;.  Vdbe *v
23525 3b 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 20  ;.  sqlite3* db 
23526 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23527 69 6e 74 20 72 65 67 41 72 67 73 3b 0a 0a 20 20  int regArgs;..  
23528 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30  memset(&sName, 0
23529 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e  , sizeof(NameCon
2352a 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e  text));.  sName.
2352b 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
2352c 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53  ..  if( .      S
2352d 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
2352e 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
2352f 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e  r(&sName, pFilen
23530 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53  ame)) ||.      S
23531 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
23532 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
23533 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d  r(&sName, pDbnam
23534 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c  e)) ||.      SQL
23535 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
23536 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28  solveAttachExpr(
23537 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20  &sName, pKey)). 
23538 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
23539 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f  nErr++;.    goto
2353a 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d   attach_end;.  }
2353b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2353c 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2353d 49 4f 4e 0a 20 20 69 66 28 20 70 41 75 74 68 41  ION.  if( pAuthA
2353e 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  rg ){.    char *
2353f 7a 41 75 74 68 41 72 67 20 3d 20 70 41 75 74 68  zAuthArg = pAuth
23540 41 72 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  Arg->u.zToken;. 
23541 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 41 75     if( NEVER(zAu
23542 74 68 41 72 67 3d 3d 30 29 20 29 7b 0a 20 20 20  thArg==0) ){.   
23543 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
23544 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  nd;.    }.    rc
23545 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
23546 65 63 6b 28 70 50 61 72 73 65 2c 20 74 79 70 65  eck(pParse, type
23547 2c 20 7a 41 75 74 68 41 72 67 2c 20 30 2c 20 30  , zAuthArg, 0, 0
23548 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51  );.    if(rc!=SQ
23549 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2354a 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64   goto attach_end
2354b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2354c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2354d 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20  T_AUTHORIZATION 
2354e 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  */...  v = sqlit
2354f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
23550 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73  );.  regArgs = s
23551 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
23552 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20  ge(pParse, 4);. 
23553 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
23554 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61  (pParse, pFilena
23555 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20  me, regArgs);.  
23556 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
23557 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c  pParse, pDbname,
23558 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73   regArgs+1);.  s
23559 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2355a 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67  Parse, pKey, reg
2355b 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65  Args+2);..  asse
2355c 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rt( v || db->mal
2355d 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
2355e 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
2355f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
23560 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c   OP_Function, 0,
23561 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63   regArgs+3-pFunc
23562 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b  ->nArg, regArgs+
23563 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  3);.    assert( 
23564 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20  pFunc->nArg==-1 
23565 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67 26  || (pFunc->nArg&
23566 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41  0xff)==pFunc->nA
23567 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rg );.    sqlite
23568 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
23569 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41 72   (u8)(pFunc->nAr
2356a 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g));.    sqlite3
2356b 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2356c 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75 6e  -1, (char *)pFun
2356d 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  c, P4_FUNCDEF);.
2356e 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20  .    /* Code an 
2356f 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 61  OP_Expire. For a
23570 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65  n ATTACH stateme
23571 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 72  nt, set P1 to tr
23572 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73 0a  ue (expire this.
23573 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
23574 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41   only). For DETA
23575 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 61  CH, set it to fa
23576 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c 20  lse (expire all 
23577 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20  existing.    ** 
23578 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20 20  statements)..   
23579 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2357a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2357b 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d 53  Expire, (type==S
2357c 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b 0a  QLITE_ATTACH));.
2357d 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65 6e    }.  .attach_en
2357e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  d:.  sqlite3Expr
2357f 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c 65  Delete(db, pFile
23580 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  name);.  sqlite3
23581 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
23582 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  Dbname);.  sqlit
23583 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
23584 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pKey);.}../*.**
23585 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   Called by the p
23586 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65  arser to compile
23587 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65 6d   a DETACH statem
23588 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  ent..**.**     D
23589 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a 2f  ETACH pDbname.*/
2358a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2358b 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61  void sqlite3Deta
2358c 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ch(Parse *pParse
2358d 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 29  , Expr *pDbname)
2358e 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44  {.  static FuncD
2358f 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20 3d  ef detach_func =
23590 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
23591 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67           /* nArg
23592 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55   */.    SQLITE_U
23593 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72  TF8,      /* iPr
23594 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  efEnc */.    0, 
23595 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23596 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30  * flags */.    0
23597 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23598 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f   /* pUserData */
23599 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
2359a 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
2359b 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75 6e  */.    detachFun
2359c 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e  c,       /* xFun
2359d 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  c */.    0,     
2359e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
2359f 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  tep */.    0,   
235a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
235a1 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20  xFinalize */.   
235a2 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22   "sqlite_detach"
235a3 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20  ,  /* zName */. 
235a4 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
235a5 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f       /* pHash */
235a6 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61  .  };.  codeAtta
235a7 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ch(pParse, SQLIT
235a8 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 63  E_DETACH, &detac
235a9 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c  h_func, pDbname,
235aa 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b   0, 0, pDbname);
235ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
235ac 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
235ad 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54  o compile an ATT
235ae 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
235af 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
235b0 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 59  p AS pDbname KEY
235b1 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f   pKey.*/.SQLITE_
235b2 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
235b3 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65  ite3Attach(Parse
235b4 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
235b5 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  p, Expr *pDbname
235b6 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20  , Expr *pKey){. 
235b7 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20   static FuncDef 
235b8 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a  attach_func = {.
235b9 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20 20      3,          
235ba 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f        /* nArg */
235bb 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  .    SQLITE_UTF8
235bc 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45  ,      /* iPrefE
235bd 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
235be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
235bf 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20  lags */.    0,  
235c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
235c1 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20   pUserData */.  
235c2 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
235c3 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
235c4 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c 20      attachFunc, 
235c5 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a        /* xFunc *
235c6 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
235c7 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70          /* xStep
235c8 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
235c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
235ca 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73  nalize */.    "s
235cb 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 20  qlite_attach",  
235cc 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
235cd 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
235ce 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20    /* pHash */.  
235cf 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28  };.  codeAttach(
235d0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
235d1 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f 66  TTACH, &attach_f
235d2 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61  unc, p, p, pDbna
235d3 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e  me, pKey);.}.#en
235d4 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
235d5 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a  IT_ATTACH */../*
235d6 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
235d7 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 75   DbFixer structu
235d8 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  re.  This routin
235d9 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
235da 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73   prior.** to pas
235db 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75  sing the structu
235dc 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65  re to one of the
235dd 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 29   sqliteFixAAAA()
235de 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
235df 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
235e0 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  n value indicate
235e1 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
235e2 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71   fixation is req
235e3 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20  uired.  TRUE.** 
235e4 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64  means we do need
235e5 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 61   to fix the data
235e6 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c  base references,
235e7 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20   FALSE means we 
235e8 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  do not..*/.SQLIT
235e9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
235ea 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20  lite3FixInit(.  
235eb 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
235ec 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72      /* The fixer
235ed 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
235ee 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
235ef 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45  Parse,      /* E
235f0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69  rror messages wi
235f1 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 65  ll be written he
235f2 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  re */.  int iDb,
235f3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
235f4 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62  his is the datab
235f5 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  ase that must be
235f6 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74   used */.  const
235f7 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f   char *zType,  /
235f8 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67  * "view", "trigg
235f9 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20  er", or "index" 
235fa 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
235fb 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65   *pName  /* Name
235fc 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 72   of the view, tr
235fd 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20  igger, or index 
235fe 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
235ff 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  *db;..  if( NEVE
23600 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44 62 3d  R(iDb<0) || iDb=
23601 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =1 ) return 0;. 
23602 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
23603 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
23604 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46 69  nDb>iDb );.  pFi
23605 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  x->pParse = pPar
23606 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20  se;.  pFix->zDb 
23607 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
23608 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 54  Name;.  pFix->zT
23609 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20 70  ype = zType;.  p
2360a 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e 61  Fix->pName = pNa
2360b 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  me;.  return 1;.
2360c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
2360d 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f  lowing set of ro
2360e 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f  utines walk thro
2360f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74 72  ugh the parse tr
23610 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a 2a  ee and assign.**
23611 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 61   a specific data
23612 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62 6c  base to all tabl
23613 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 65  e references whe
23614 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
23615 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66 74  name.** was left
23616 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e 20   unspecified in 
23617 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
23618 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
23619 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65 0a   pFix structure.
2361a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  ** must have bee
2361b 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79  n initialized by
2361c 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
2361d 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
2361e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  )..**.** These r
2361f 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
23620 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
23621 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72 69  at an index, tri
23622 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 77  gger, or.** view
23623 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65   in one database
23624 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72 20   does not refer 
23625 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61 20  to objects in a 
23626 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
23627 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69 6f  se..** (Exceptio
23628 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69 67  n: indices, trig
23629 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73 20  gers, and views 
2362a 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  in the TEMP data
2362b 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f  base are.** allo
2362c 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20  wed to refer to 
2362d 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20 61  anything.)  If a
2362e 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65 78   reference is ex
2362f 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a 2a  plicitly made.**
23630 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69 6e   to an object in
23631 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 74   a different dat
23632 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72 20  abase, an error 
23633 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65 64  message is added
23634 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a   to.** pParse->z
23635 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73 65  ErrMsg and these
23636 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
23637 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 65   non-zero.  If e
23638 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68 65  verything.** che
23639 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20 72  cks out, these r
2363a 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 30  outines return 0
2363b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2363c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
2363d 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62 46  ixSrcList(.  DbF
2363e 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
2363f 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
23640 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
23641 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74    SrcList *pList
23642 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 6f         /* The So
23643 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68 65  urce list to che
23644 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f  ck and modify */
23645 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  .){.  int i;.  c
23646 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a  onst char *zDb;.
23647 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
23648 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20  _item *pItem;.. 
23649 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74   if( NEVER(pList
2364a 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ==0) ) return 0;
2364b 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 3e 7a  .  zDb = pFix->z
2364c 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  Db;.  for(i=0, p
2364d 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
2364e 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  <pList->nSrc; i+
2364f 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
23650 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74   if( pItem->zDat
23651 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  abase==0 ){.    
23652 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61    pItem->zDataba
23653 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  se = sqlite3DbSt
23654 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 72 73  rDup(pFix->pPars
23655 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 20 20  e->db, zDb);.   
23656 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
23657 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d 2d  e3StrICmp(pItem-
23658 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29 21  >zDatabase,zDb)!
23659 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2365a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69 78  te3ErrorMsg(pFix
2365b 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  ->pParse,.      
2365c 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f 74     "%s %T cannot
2365d 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65 63   reference objec
2365e 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 25  ts in database %
2365f 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69  s",.         pFi
23660 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d 3e  x->zType, pFix->
23661 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44  pName, pItem->zD
23662 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20  atabase);.      
23663 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
23664 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
23665 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
23666 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
23667 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
23668 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
23669 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
2366a 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 29  Item->pSelect) )
2366b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69   return 1;.    i
2366c 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2366d 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70  r(pFix, pItem->p
2366e 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  On) ) return 1;.
2366f 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
23670 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64 65  urn 0;.}.#if !de
23671 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
23672 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
23673 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23674 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f  TRIGGER).SQLITE_
23675 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
23676 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20 20  te3FixSelect(.  
23677 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
23678 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
23679 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
2367a 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
2367b 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68 65  lect      /* The
2367c 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2367d 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f  t to be fixed to
2367e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
2367f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 65  .){.  while( pSe
23680 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28 20  lect ){.    if( 
23681 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
23682 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  st(pFix, pSelect
23683 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20 20  ->pEList) ){.   
23684 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
23685 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
23686 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46 69  e3FixSrcList(pFi
23687 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  x, pSelect->pSrc
23688 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
23689 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
2368a 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2368b 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d  r(pFix, pSelect-
2368c 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20  >pWhere) ){.    
2368d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2368e 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
2368f 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
23690 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67 29  Select->pHaving)
23691 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
23692 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53   1;.    }.    pS
23693 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d  elect = pSelect-
23694 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
23695 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54  eturn 0;.}.SQLIT
23696 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23697 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a 20 20  lite3FixExpr(.  
23698 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
23699 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
2369a 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
2369b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
2369c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2369d 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69  ression to be fi
2369e 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62  xed to one datab
2369f 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c  ase */.){.  whil
236a0 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  e( pExpr ){.    
236a1 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
236a2 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
236a3 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 62 72  _TokenOnly) ) br
236a4 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45 78 70  eak;.    if( Exp
236a5 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
236a6 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
236a7 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ) ){.      if( s
236a8 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
236a9 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70  pFix, pExpr->x.p
236aa 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e  Select) ) return
236ab 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
236ac 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
236ad 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78  FixExprList(pFix
236ae 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
236af 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
236b0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
236b1 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c  te3FixExpr(pFix,
236b2 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20   pExpr->pRight) 
236b3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
236b4 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78  1;.    }.    pEx
236b5 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  pr = pExpr->pLef
236b6 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
236b7 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  0;.}.SQLITE_PRIV
236b8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
236b9 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62  ixExprList(.  Db
236ba 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
236bb 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74   /* Context of t
236bc 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20  he fixation */. 
236bd 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
236be 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
236bf 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65  ssion to be fixe
236c0 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73  d to one databas
236c1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  e */.){.  int i;
236c2 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
236c3 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
236c4 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
236c5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72   return 0;.  for
236c6 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
236c7 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
236c8 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
236c9 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
236ca 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
236cb 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20  , pItem->pExpr) 
236cc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
236cd 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
236ce 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
236cf 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
236d0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53  E_OMIT_TRIGGER.S
236d1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
236d2 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67  t sqlite3FixTrig
236d3 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 69 78  gerStep(.  DbFix
236d4 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a  er *pFix,     /*
236d5 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
236d6 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72  fixation */.  Tr
236d7 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
236d8 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
236d9 73 74 65 70 20 62 65 20 66 69 78 65 64 20 74 6f  step be fixed to
236da 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
236db 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74  .){.  while( pSt
236dc 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  ep ){.    if( sq
236dd 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70  lite3FixSelect(p
236de 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c  Fix, pStep->pSel
236df 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ect) ){.      re
236e0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
236e1 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
236e2 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 65 70  Expr(pFix, pStep
236e3 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  ->pWhere) ){.   
236e4 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
236e5 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
236e6 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46  e3FixExprList(pF
236e7 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72  ix, pStep->pExpr
236e8 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72  List) ){.      r
236e9 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
236ea 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 65 70     pStep = pStep
236eb 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  ->pNext;.  }.  r
236ec 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
236ed 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
236ee 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68  ** End of attach
236ef 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
236f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236f2 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
236f3 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75  ** Begin file au
236f4 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  th.c ***********
236f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
236f7 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61  */./*.** 2003 Ja
236f8 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54  nuary 11.**.** T
236f9 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
236fa 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
236fb 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
236fc 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
236fd 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
236fe 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
236ff 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
23700 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
23701 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
23702 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
23703 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
23704 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
23705 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
23706 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
23707 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
23708 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
23709 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2370a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2370b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2370c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2370d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2370e 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2370f 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
23710 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
23711 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33  ment the sqlite3
23712 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
23713 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20  ).** API.  This 
23714 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f  facility is an o
23715 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20  ptional feature 
23716 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  of the library. 
23717 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73   Embedded.** sys
23718 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  tems that do not
23719 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 69 6c   need this facil
2371a 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20  ity may omit it 
2371b 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a  by recompiling.*
2371c 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 69  * the library wi
2371d 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54  th -DSQLITE_OMIT
2371e 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31  _AUTHORIZATION=1
2371f 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f  .*/../*.** All o
23720 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
23721 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f  is file may be o
23722 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e 69  mitted by defini
23723 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d  ng a single.** m
23724 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  acro..*/.#ifndef
23725 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
23726 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a  HORIZATION../*.*
23727 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74  * Set or clear t
23728 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72  he access author
23729 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ization function
2372a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65  ..**.** The acce
2372b 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
2372c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20   function is be 
2372d 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68  called during th
2372e 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a  e compilation.**
2372f 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66 79   phase to verify
23730 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 68   that the user h
23731 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 77  as read and/or w
23732 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72 6d  rite access perm
23733 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72  ission on.** var
23734 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20 74  ious fields of t
23735 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
23736 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
23737 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
23738 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f  ction.** is a co
23739 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61 72  py of the 3rd ar
2373a 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 72  gument to this r
2373b 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65 63  outine.  The sec
2373c 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
2373d 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  to the auth func
2373e 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74  tion is one of t
2373f 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a  hese constants:.
23740 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  **.**       SQLI
23741 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 0a  TE_CREATE_INDEX.
23742 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
23743 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20  CREATE_TABLE.** 
23744 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
23745 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ATE_TEMP_INDEX.*
23746 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
23747 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45  REATE_TEMP_TABLE
23748 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23749 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49  _CREATE_TEMP_TRI
2374a 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
2374b 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
2374c 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
2374d 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49  QLITE_CREATE_TRI
2374e 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
2374f 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57  LITE_CREATE_VIEW
23750 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23751 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20  _DELETE.**      
23752 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44   SQLITE_DROP_IND
23753 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EX.**       SQLI
23754 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a  TE_DROP_TABLE.**
23755 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
23756 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a  OP_TEMP_INDEX.**
23757 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
23758 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a  OP_TEMP_TABLE.**
23759 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
2375a 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a  OP_TEMP_TRIGGER.
2375b 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2375c 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a  DROP_TEMP_VIEW.*
2375d 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
2375e 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20  ROP_TRIGGER.**  
2375f 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
23760 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
23761 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20  QLITE_INSERT.** 
23762 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52 41        SQLITE_PRA
23763 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GMA.**       SQL
23764 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20  ITE_READ.**     
23765 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a    SQLITE_SELECT.
23766 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
23767 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20  TRANSACTION.**  
23768 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44 41       SQLITE_UPDA
23769 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  TE.**.** The thi
2376a 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72  rd and fourth ar
2376b 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61  guments to the a
2376c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65  uth function are
2376d 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20   the name of.** 
2376e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68  the table and th
2376f 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72  e column that ar
23770 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64  e being accessed
23771 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e 63  .  The auth func
23772 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72  tion.** should r
23773 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c  eturn either SQL
23774 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44  ITE_OK, SQLITE_D
23775 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49  ENY, or SQLITE_I
23776 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51  GNORE.  If.** SQ
23777 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
23778 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ned, it means th
23779 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c 6c  at access is all
2377a 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 45  owed.  SQLITE_DE
2377b 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74  NY.** means that
2377c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2377d 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75  nt will never-ru
2377e 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 5f  n - the sqlite3_
2377f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77  exec() call.** w
23780 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68 20  ill return with 
23781 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54  an error.  SQLIT
23782 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74  E_IGNORE means t
23783 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74  hat the SQL stat
23784 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20  ement.** should 
23785 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74 73  run but attempts
23786 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70 65   to read the spe
23787 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69  cified column wi
23788 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a  ll return NULL.*
23789 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20 74  * and attempts t
2378a 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c 75  o write the colu
2378b 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72  mn will be ignor
2378c 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e  ed..**.** Settin
2378d 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  g the auth funct
2378e 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61  ion to NULL disa
2378f 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20  bles this hook. 
23790 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   The default.** 
23791 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 61  setting of the a
23792 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  uth function is 
23793 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
23794 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
23795 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a  set_authorizer(.
23796 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
23797 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
23798 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
23799 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
2379a 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
2379b 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64  t char*),.  void
2379c 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69   *pArg.){.  sqli
2379d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2379e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
2379f 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b  ->xAuth = xAuth;
237a0 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67 20  .  db->pAuthArg 
237a1 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
237a2 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
237a3 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20  tatements(db);. 
237a4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
237a5 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
237a6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
237a7 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  _OK;.}../*.** Wr
237a8 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
237a9 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65  sage into pParse
237aa 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65  ->zErrMsg that e
237ab 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65  xplains that the
237ac 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65  .** user-supplie
237ad 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  d authorization 
237ae 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65  function returne
237af 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c  d an illegal val
237b0 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
237b1 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 64  id sqliteAuthBad
237b2 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65  ReturnCode(Parse
237b3 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
237b4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
237b5 72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65 72  rse, "authorizer
237b6 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a   malfunction");.
237b7 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
237b8 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a  QLITE_ERROR;.}..
237b9 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
237ba 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
237bb 61 6c 6c 62 61 63 6b 20 66 6f 72 20 70 65 72 6d  allback for perm
237bc 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20 63  ission to read c
237bd 6f 6c 75 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d 0a  olumn zCol from.
237be 2a 2a 20 74 61 62 6c 65 20 7a 54 61 62 20 69 6e  ** table zTab in
237bf 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 54   database zDb. T
237c0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  his function ass
237c1 75 6d 65 73 20 74 68 61 74 20 61 6e 20 61 75 74  umes that an aut
237c2 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61  horization.** ca
237c3 6c 6c 62 61 63 6b 20 68 61 73 20 62 65 65 6e 20  llback has been 
237c4 72 65 67 69 73 74 65 72 65 64 20 28 69 2e 65 2e  registered (i.e.
237c5 20 74 68 61 74 20 73 71 6c 69 74 65 33 2e 78 41   that sqlite3.xA
237c6 75 74 68 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29  uth is not NULL)
237c7 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
237c8 45 5f 49 47 4e 4f 52 45 20 69 73 20 72 65 74 75  E_IGNORE is retu
237c9 72 6e 65 64 20 61 6e 64 20 70 45 78 70 72 20 69  rned and pExpr i
237ca 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
237cb 20 70 45 78 70 72 20 69 73 20 63 68 61 6e 67 65   pExpr is change
237cc 64 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 4e  d.** to an SQL N
237cd 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  ULL expression. 
237ce 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 45  Otherwise, if pE
237cf 78 70 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  xpr is NULL, the
237d0 6e 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 0a  n SQLITE_IGNORE.
237d1 2a 2a 20 69 73 20 74 72 65 61 74 65 64 20 61 73  ** is treated as
237d2 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 49 6e   SQLITE_DENY. In
237d3 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 72   this case an er
237d4 72 6f 72 20 69 73 20 6c 65 66 74 20 69 6e 20 70  ror is left in p
237d5 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Parse..*/.SQLITE
237d6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
237d7 69 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28  ite3AuthReadCol(
237d8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
237d9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
237da 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
237db 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
237dc 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c 20  nst char *zTab, 
237dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
237de 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   Table name */. 
237df 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
237e0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
237e1 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20   /* Column name 
237e2 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20 20 20 20  */.  int iDb    
237e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237e4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
237e5 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61   containing data
237e6 62 61 73 65 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71  base. */.){.  sq
237e7 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
237e8 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a  se->db;       /*
237e9 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
237ea 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 20   */.  char *zDb 
237eb 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
237ec 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
237ed 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
237ee 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  se */.  int rc; 
237ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237f0 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 20          /* Auth 
237f1 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 20  callback return 
237f2 63 6f 64 65 20 2a 2f 0a 0a 20 20 72 63 20 3d 20  code */..  rc = 
237f3 64 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41  db->xAuth(db->pA
237f4 75 74 68 41 72 67 2c 20 53 51 4c 49 54 45 5f 52  uthArg, SQLITE_R
237f5 45 41 44 2c 20 7a 54 61 62 2c 7a 43 6f 6c 2c 7a  EAD, zTab,zCol,z
237f6 44 62 2c 70 50 61 72 73 65 2d 3e 7a 41 75 74 68  Db,pParse->zAuth
237f7 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20  Context);.  if( 
237f8 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
237f9 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e  ){.    if( db->n
237fa 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30 20 29  Db>2 || iDb!=0 )
237fb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
237fc 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
237fd 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73  "access to %s.%s
237fe 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74 65  .%s is prohibite
237ff 64 22 2c 7a 44 62 2c 7a 54 61 62 2c 7a 43 6f 6c  d",zDb,zTab,zCol
23800 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
23801 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23802 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63  Msg(pParse, "acc
23803 65 73 73 20 74 6f 20 25 73 2e 25 73 20 69 73 20  ess to %s.%s is 
23804 70 72 6f 68 69 62 69 74 65 64 22 2c 20 7a 54 61  prohibited", zTa
23805 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a  b, zCol);.    }.
23806 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
23807 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20   SQLITE_AUTH;.  
23808 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
23809 4c 49 54 45 5f 49 47 4e 4f 52 45 20 26 26 20 72  LITE_IGNORE && r
2380a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2380b 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61      sqliteAuthBa
2380c 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72  dReturnCode(pPar
2380d 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  se);.  }.  retur
2380e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2380f 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64 20  he pExpr should 
23810 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65  be a TK_COLUMN e
23811 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20  xpression.  The 
23812 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74  table referred t
23813 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 4c  o.** is in pTabL
23814 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20 69  ist or else it i
23815 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  s the NEW or OLD
23816 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69 67   table of a trig
23817 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20  ger.  .** Check 
23818 74 6f 20 73 65 65 20 69 66 20 69 74 20 69 73 20  to see if it is 
23819 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73 20  OK to read this 
2381a 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d  particular colum
2381b 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
2381c 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65  auth function re
2381d 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e  turns SQLITE_IGN
2381e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65 20  ORE, change the 
2381f 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e  TK_COLUMN .** in
23820 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61  struction into a
23821 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68   TK_NULL.  If th
23822 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20  e auth function 
23823 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  returns SQLITE_D
23824 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e  ENY,.** then gen
23825 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a  erate an error..
23826 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23827 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
23828 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65 20  thRead(.  Parse 
23829 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2382a 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2382b 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
2382c 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
2382d 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2382e 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74 68  on to check auth
2382f 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a  orization on */.
23830 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
23831 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  a,      /* The s
23832 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70  chema of the exp
23833 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63  ression */.  Src
23834 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20  List *pTabList  
23835 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20     /* All table 
23836 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68 74  that pExpr might
23837 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a   refer to */.){.
23838 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23839 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54 61  pParse->db;.  Ta
2383a 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20  ble *pTab = 0;  
2383b 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2383c 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20   being read */. 
2383d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
2383e 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  l;     /* Name o
2383f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  f the column of 
23840 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
23841 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20 20  nt iSrc;        
23842 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
23843 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f   pTabList->a[] o
23844 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  f table being re
23845 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ad */.  int iDb;
23846 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23847 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
23848 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 65  e database the e
23849 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73  xpression refers
2384a 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f   to */.  int iCo
2384b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
2384c 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
2384d 6e 20 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a 20  n in table */.. 
2384e 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d   if( db->xAuth==
2384f 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44  0 ) return;.  iD
23850 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
23851 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
23852 3e 64 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a 20  >db, pSchema);. 
23853 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
23854 20 20 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74 20    /* An attempt 
23855 74 6f 20 72 65 61 64 20 61 20 63 6f 6c 75 6d 6e  to read a column
23856 20 6f 75 74 20 6f 66 20 61 20 73 75 62 71 75 65   out of a subque
23857 72 79 20 6f 72 20 6f 74 68 65 72 0a 20 20 20 20  ry or other.    
23858 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ** temporary tab
23859 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  le. */.    retur
2385a 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  n;.  }..  assert
2385b 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2385c 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d  COLUMN || pExpr-
2385d 3e 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20  >op==TK_TRIGGER 
2385e 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  );.  if( pExpr->
2385f 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29  op==TK_TRIGGER )
23860 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  {.    pTab = pPa
23861 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
23862 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
23863 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 20  ssert( pTabList 
23864 29 3b 0a 20 20 20 20 66 6f 72 28 69 53 72 63 3d  );.    for(iSrc=
23865 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c 70  0; ALWAYS(iSrc<p
23866 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20  TabList->nSrc); 
23867 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20 69  iSrc++){.      i
23868 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
23869 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53  ==pTabList->a[iS
2386a 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 7b 0a 20  rc].iCursor ){. 
2386b 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54         pTab = pT
2386c 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e  abList->a[iSrc].
2386d 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 62 72  pTab;.        br
2386e 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2386f 20 7d 0a 20 20 7d 0a 20 20 69 43 6f 6c 20 3d 20   }.  }.  iCol = 
23870 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pExpr->iColumn;.
23871 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62    if( NEVER(pTab
23872 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  ==0) ) return;..
23873 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b    if( iCol>=0 ){
23874 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f  .    assert( iCo
23875 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  l<pTab->nCol );.
23876 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d      zCol = pTab-
23877 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
23878 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
23879 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  Tab->iPKey>=0 ){
2387a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
2387b 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e  b->iPKey<pTab->n
2387c 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20  Col );.    zCol 
2387d 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61  = pTab->aCol[pTa
2387e 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b  b->iPKey].zName;
2387f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
23880 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20  ol = "ROWID";.  
23881 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  }.  assert( iDb>
23882 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
23883 62 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  b );.  if( SQLIT
23884 45 5f 49 47 4e 4f 52 45 3d 3d 73 71 6c 69 74 65  E_IGNORE==sqlite
23885 33 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50 61  3AuthReadCol(pPa
23886 72 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  rse, pTab->zName
23887 2c 20 7a 43 6f 6c 2c 20 69 44 62 29 20 29 7b 0a  , zCol, iDb) ){.
23888 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
23889 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a  TK_NULL;.  }.}..
2388a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68  /*.** Do an auth
2388b 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20  orization check 
2388c 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61  using the code a
2388d 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76  nd arguments giv
2388e 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65  en.  Return.** e
2388f 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20  ither SQLITE_OK 
23890 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45  (zero) or SQLITE
23891 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54  _IGNORE or SQLIT
23892 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49  E_DENY.  If SQLI
23893 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65  TE_DENY.** is re
23894 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  turned, then the
23895 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64   error count and
23896 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
23897 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20  n pParse are.** 
23898 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72  modified appropr
23899 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  iately..*/.SQLIT
2389a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2389b 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a  lite3AuthCheck(.
2389c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2389d 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63  .  int code,.  c
2389e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31  onst char *zArg1
2389f 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
238a0 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63  zArg2,.  const c
238a1 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20  har *zArg3.){.  
238a2 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
238a3 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
238a4 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20  rc;..  /* Don't 
238a5 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61  do any authoriza
238a6 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74  tion checks if t
238a7 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69  he database is i
238a8 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a  nitialising.  **
238a9 20 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65   or if the parse
238aa 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b  r is being invok
238ab 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73  ed from within s
238ac 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
238ad 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tab..  */.  if( 
238ae 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c  db->init.busy ||
238af 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
238b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
238b1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
238b2 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d   if( db->xAuth==
238b3 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
238b4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
238b5 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28   rc = db->xAuth(
238b6 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f  db->pAuthArg, co
238b7 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32  de, zArg1, zArg2
238b8 2c 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d  , zArg3, pParse-
238b9 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a  >zAuthContext);.
238ba 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
238bb 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c  _DENY ){.    sql
238bc 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
238bd 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72  rse, "not author
238be 69 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72  ized");.    pPar
238bf 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
238c0 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66  AUTH;.  }else if
238c1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
238c2 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47  && rc!=SQLITE_IG
238c3 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d  NORE ){.    rc =
238c4 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20   SQLITE_DENY;.  
238c5 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52    sqliteAuthBadR
238c6 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65  eturnCode(pParse
238c7 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
238c8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73  rc;.}../*.** Pus
238c9 68 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  h an authorizati
238ca 6f 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74  on context.  Aft
238cb 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
238cc 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a  is called, the.*
238cd 2a 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74  * zArg3 argument
238ce 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   to authorizatio
238cf 6e 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c  n callbacks will
238d0 20 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74   be zContext unt
238d1 69 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f  il.** popped.  O
238d2 72 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20  r if pParse==0, 
238d3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
238d4 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49  a no-op..*/.SQLI
238d5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
238d6 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
238d7 78 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20  xtPush(.  Parse 
238d8 2a 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43  *pParse,.  AuthC
238d9 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
238da 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
238db 2a 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 61  *zContext.){.  a
238dc 73 73 65 72 74 28 20 70 50 61 72 73 65 20 29 3b  ssert( pParse );
238dd 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61  .  pContext->pPa
238de 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
238df 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43  pContext->zAuthC
238e0 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d  ontext = pParse-
238e1 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20  >zAuthContext;. 
238e2 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f   pParse->zAuthCo
238e3 6e 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74  ntext = zContext
238e4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 61  ;.}../*.** Pop a
238e5 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  n authorization 
238e6 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61 73  context that was
238e7 20 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73 68   previously push
238e8 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33  ed.** by sqlite3
238e9 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 0a  AuthContextPush.
238ea 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
238eb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
238ec 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74  thContextPop(Aut
238ed 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  hContext *pConte
238ee 78 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e 74  xt){.  if( pCont
238ef 65 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20  ext->pParse ){. 
238f0 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61     pContext->pPa
238f1 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
238f2 74 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41  t = pContext->zA
238f3 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  uthContext;.    
238f4 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65  pContext->pParse
238f5 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e   = 0;.  }.}..#en
238f6 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
238f7 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
238f8 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
238f9 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74 68  **** End of auth
238fa 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
238fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
238fd 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
238fe 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
238ff 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  build.c ********
23900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23902 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
23903 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
23904 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
23905 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
23906 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
23907 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
23908 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
23909 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2390a 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2390b 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2390c 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2390d 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2390e 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2390f 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
23910 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
23911 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
23912 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
23913 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
23914 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
23915 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
23916 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23918 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2391a 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2391b 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
2391c 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
2391d 64 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20  d by the SQLite 
2391e 70 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73  parser.** when s
2391f 79 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65 20  yntax rules are 
23920 72 65 64 75 63 65 64 2e 20 20 54 68 65 20 72 6f  reduced.  The ro
23921 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
23922 69 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a  ile handle the.*
23923 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64  * following kind
23924 73 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78 3a  s of SQL syntax:
23925 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
23926 45 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 44  E TABLE.**     D
23927 52 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20  ROP TABLE.**    
23928 20 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a 2a   CREATE INDEX.**
23929 20 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58 0a       DROP INDEX.
2392a 2a 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67 20  **     creating 
2392b 49 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20 20  ID lists.**     
2392c 42 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49 4f  BEGIN TRANSACTIO
2392d 4e 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54 0a  N.**     COMMIT.
2392e 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 0a  **     ROLLBACK.
2392f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
23930 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
23931 20 77 68 65 6e 20 61 20 6e 65 77 20 53 51 4c 20   when a new SQL 
23932 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 67  statement is beg
23933 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20  inning to.** be 
23934 70 61 72 73 65 64 2e 20 20 49 6e 69 74 69 61 6c  parsed.  Initial
23935 69 7a 65 20 74 68 65 20 70 50 61 72 73 65 20 73  ize the pParse s
23936 74 72 75 63 74 75 72 65 20 61 73 20 6e 65 65 64  tructure as need
23937 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
23938 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23939 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 72  e3BeginParse(Par
2393a 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2393b 65 78 70 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20  explainFlag){.  
2393c 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20  pParse->explain 
2393d 3d 20 28 75 38 29 65 78 70 6c 61 69 6e 46 6c 61  = (u8)explainFla
2393e 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61  g;.  pParse->nVa
2393f 72 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65  r = 0;.}..#ifnde
23940 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
23941 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
23942 20 54 68 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73   The TableLock s
23943 74 72 75 63 74 75 72 65 20 69 73 20 6f 6e 6c 79  tructure is only
23944 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
23945 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20  ite3TableLock() 
23946 61 6e 64 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65  and.** codeTable
23947 4c 6f 63 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e  Locks() function
23948 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62  s..*/.struct Tab
23949 6c 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69  leLock {.  int i
2394a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2394b 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2394c 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
2394d 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
2394e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b  d */.  int iTab;
2394f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
23950 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
23951 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
23952 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69  locked */.  u8 i
23953 73 57 72 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20  sWriteLock;     
23954 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 77 72 69   /* True for wri
23955 74 65 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20  te lock.  False 
23956 66 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  for a read lock 
23957 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
23958 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d  *zName;   /* Nam
23959 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  e of the table *
2395a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  /.};../*.** Reco
2395b 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
2395c 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b   we want to lock
2395d 20 61 20 74 61 62 6c 65 20 61 74 20 72 75 6e 2d   a table at run-
2395e 74 69 6d 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  time.  .**.** Th
2395f 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f  e table to be lo
23960 63 6b 65 64 20 68 61 73 20 72 6f 6f 74 20 70 61  cked has root pa
23961 67 65 20 69 54 61 62 20 61 6e 64 20 69 73 20 66  ge iTab and is f
23962 6f 75 6e 64 20 69 6e 20 64 61 74 61 62 61 73 65  ound in database
23963 20 69 44 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20   iDb..** A read 
23964 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20  or a write lock 
23965 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64 65 70  can be taken dep
23966 65 6e 64 69 6e 67 20 6f 6e 20 69 73 57 72 69 74  ending on isWrit
23967 65 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  elock..**.** Thi
23968 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 72  s routine just r
23969 65 63 6f 72 64 73 20 74 68 65 20 66 61 63 74 20  ecords the fact 
2396a 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 69 73  that the lock is
2396b 20 64 65 73 69 72 65 64 2e 20 20 54 68 65 0a 2a   desired.  The.*
2396c 2a 20 63 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74  * code to make t
2396d 68 65 20 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73  he lock occur is
2396e 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 20   generated by a 
2396f 6c 61 74 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a  later call to.**
23970 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
23971 29 20 77 68 69 63 68 20 6f 63 63 75 72 73 20 64  ) which occurs d
23972 75 72 69 6e 67 20 73 71 6c 69 74 65 33 46 69 6e  uring sqlite3Fin
23973 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a  ishCoding()..*/.
23974 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23975 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
23976 4c 6f 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  Lock(.  Parse *p
23977 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
23978 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
23979 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
2397a 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2397b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
2397c 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
2397d 62 6c 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20  ble to lock */. 
2397e 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20   int iTab,      
2397f 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65      /* Root page
23980 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74   number of the t
23981 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
23982 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69 74  d */.  u8 isWrit
23983 65 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75  eLock,    /* Tru
23984 65 20 66 6f 72 20 61 20 77 72 69 74 65 20 6c 6f  e for a write lo
23985 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ck */.  const ch
23986 61 72 20 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61  ar *zName  /* Na
23987 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
23988 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a  to be locked */.
23989 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  ){.  Parse *pTop
2398a 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
2398b 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
2398c 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  rse);.  int i;. 
2398d 20 69 6e 74 20 6e 42 79 74 65 73 3b 0a 20 20 54   int nBytes;.  T
2398e 61 62 6c 65 4c 6f 63 6b 20 2a 70 3b 0a 20 20 61  ableLock *p;.  a
2398f 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
23990 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
23991 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65  Toplevel->nTable
23992 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Lock; i++){.    
23993 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e  p = &pToplevel->
23994 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20  aTableLock[i];. 
23995 20 20 20 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69     if( p->iDb==i
23996 44 62 20 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69  Db && p->iTab==i
23997 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Tab ){.      p->
23998 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70  isWriteLock = (p
23999 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c  ->isWriteLock ||
2399a 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20   isWriteLock);. 
2399b 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2399c 20 7d 0a 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73   }.  }..  nBytes
2399d 20 3d 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c   = sizeof(TableL
2399e 6f 63 6b 29 20 2a 20 28 70 54 6f 70 6c 65 76 65  ock) * (pTopleve
2399f 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29  l->nTableLock+1)
239a0 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  ;.  pToplevel->a
239a1 54 61 62 6c 65 4c 6f 63 6b 20 3d 0a 20 20 20 20  TableLock =.    
239a2 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c    sqlite3DbReall
239a3 6f 63 4f 72 46 72 65 65 28 70 54 6f 70 6c 65 76  ocOrFree(pToplev
239a4 65 6c 2d 3e 64 62 2c 20 70 54 6f 70 6c 65 76 65  el->db, pTopleve
239a5 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  l->aTableLock, n
239a6 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 54  Bytes);.  if( pT
239a7 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c  oplevel->aTableL
239a8 6f 63 6b 20 29 7b 0a 20 20 20 20 70 20 3d 20 26  ock ){.    p = &
239a9 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c  pToplevel->aTabl
239aa 65 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d  eLock[pToplevel-
239ab 3e 6e 54 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a  >nTableLock++];.
239ac 20 20 20 20 70 2d 3e 69 44 62 20 3d 20 69 44 62      p->iDb = iDb
239ad 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 20 3d 20  ;.    p->iTab = 
239ae 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 73 57  iTab;.    p->isW
239af 72 69 74 65 4c 6f 63 6b 20 3d 20 69 73 57 72 69  riteLock = isWri
239b0 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a  teLock;.    p->z
239b1 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20  Name = zName;.  
239b2 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c  }else{.    pTopl
239b3 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b  evel->nTableLock
239b4 20 3d 20 30 3b 0a 20 20 20 20 70 54 6f 70 6c 65   = 0;.    pTople
239b5 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  vel->db->mallocF
239b6 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d  ailed = 1;.  }.}
239b7 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20  ../*.** Code an 
239b8 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73  OP_TableLock ins
239b9 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61 63  truction for eac
239ba 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62  h table locked b
239bb 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  y the.** stateme
239bc 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20 62  nt (configured b
239bd 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  y calls to sqlit
239be 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a  e3TableLock())..
239bf 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
239c0 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61  odeTableLocks(Pa
239c1 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
239c2 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 70  int i;.  Vdbe *p
239c3 56 64 62 65 3b 20 0a 0a 20 20 70 56 64 62 65 20  Vdbe; ..  pVdbe 
239c4 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
239c5 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
239c6 72 74 28 20 70 56 64 62 65 21 3d 30 20 29 3b 20  rt( pVdbe!=0 ); 
239c7 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 56 64 62  /* sqlite3GetVdb
239c8 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 3a 20 56  e cannot fail: V
239c9 44 42 45 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f  DBE already allo
239ca 63 61 74 65 64 20 2a 2f 0a 0a 20 20 66 6f 72 28  cated */..  for(
239cb 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e  i=0; i<pParse->n
239cc 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  TableLock; i++){
239cd 0a 20 20 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a  .    TableLock *
239ce 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61  p = &pParse->aTa
239cf 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20  bleLock[i];.    
239d0 69 6e 74 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b  int p1 = p->iDb;
239d1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
239d2 41 64 64 4f 70 34 28 70 56 64 62 65 2c 20 4f 50  AddOp4(pVdbe, OP
239d3 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20  _TableLock, p1, 
239d4 70 2d 3e 69 54 61 62 2c 20 70 2d 3e 69 73 57 72  p->iTab, p->isWr
239d5 69 74 65 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20  iteLock,.       
239d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
239d7 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
239d8 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65  IC);.  }.}.#else
239d9 0a 20 20 23 64 65 66 69 6e 65 20 63 6f 64 65 54  .  #define codeT
239da 61 62 6c 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e  ableLocks(x).#en
239db 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
239dc 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
239dd 64 20 61 66 74 65 72 20 61 20 73 69 6e 67 6c 65  d after a single
239de 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 68   SQL statement h
239df 61 73 20 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65  as been.** parse
239e0 64 20 61 6e 64 20 61 20 56 44 42 45 20 70 72 6f  d and a VDBE pro
239e1 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20  gram to execute 
239e2 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 20 68  that statement h
239e3 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61  as been.** prepa
239e4 72 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  red.  This routi
239e5 6e 65 20 70 75 74 73 20 74 68 65 20 66 69 6e 69  ne puts the fini
239e6 73 68 69 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e  shing touches on
239e7 20 74 68 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f   the.** VDBE pro
239e8 67 72 61 6d 20 61 6e 64 20 72 65 73 65 74 73 20  gram and resets 
239e9 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
239ea 74 75 72 65 20 66 6f 72 20 74 68 65 20 6e 65 78  ture for the nex
239eb 74 0a 2a 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a  t.** parse..**.*
239ec 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61  * Note that if a
239ed 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  n error occurred
239ee 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 68  , it might be th
239ef 65 20 63 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e  e case that.** n
239f0 6f 20 56 44 42 45 20 63 6f 64 65 20 77 61 73 20  o VDBE code was 
239f1 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51  generated..*/.SQ
239f2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
239f3 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
239f4 6f 64 69 6e 67 28 50 61 72 73 65 20 2a 70 50 61  oding(Parse *pPa
239f5 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
239f6 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
239f7 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
239f8 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  db;.  if( db->ma
239f9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
239fa 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73  urn;.  if( pPars
239fb 65 2d 3e 6e 65 73 74 65 64 20 29 20 72 65 74 75  e->nested ) retu
239fc 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  rn;.  if( pParse
239fd 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b  ->nErr ) return;
239fe 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20  ..  /* Begin by 
239ff 67 65 6e 65 72 61 74 69 6e 67 20 73 6f 6d 65 20  generating some 
23a00 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65  termination code
23a01 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
23a02 68 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f  he.  ** vdbe pro
23a03 67 72 61 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  gram.  */.  v = 
23a04 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
23a05 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74  Parse);.  assert
23a06 28 20 21 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c  ( !pParse->isMul
23a07 74 69 57 72 69 74 65 20 0a 20 20 20 20 20 20 20  tiWrite .       
23a08 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 41 73  || sqlite3VdbeAs
23a09 73 65 72 74 4d 61 79 41 62 6f 72 74 28 76 2c 20  sertMayAbort(v, 
23a0a 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74  pParse->mayAbort
23a0b 29 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  ));.  if( v ){. 
23a0c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23a0d 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29  dOp0(v, OP_Halt)
23a0e 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  ;..    /* The co
23a0f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69  okie mask contai
23a10 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20 65  ns one bit for e
23a11 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c  ach database fil
23a12 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28  e open..    ** (
23a13 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61 69  Bit 0 is for mai
23a14 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72 20  n, bit 1 is for 
23a15 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72  temp, and so for
23a16 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a 20  th.)  Bits are. 
23a17 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65 61     ** set for ea
23a18 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61 74  ch database that
23a19 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65 72   is used.  Gener
23a1a 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61 72  ate code to star
23a1b 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  t a.    ** trans
23a1c 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75  action on each u
23a1d 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e 64  sed database and
23a1e 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20 73   to verify the s
23a1f 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20  chema cookie.   
23a20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65 64   ** on each used
23a21 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a   database..    *
23a22 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  /.    if( pParse
23a23 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29  ->cookieGoto>0 )
23a24 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73 6b  {.      u32 mask
23a25 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b  ;.      int iDb;
23a26 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23a27 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 50  beJumpHere(v, pP
23a28 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  arse->cookieGoto
23a29 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  -1);.      for(i
23a2a 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44  Db=0, mask=1; iD
23a2b 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c  b<db->nDb; mask<
23a2c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20  <=1, iDb++){.   
23a2d 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26       if( (mask &
23a2e 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d   pParse->cookieM
23a2f 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  ask)==0 ) contin
23a30 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ue;.        sqli
23a31 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
23a32 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  (v, iDb);.      
23a33 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23a34 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63  Op2(v,OP_Transac
23a35 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b  tion, iDb, (mask
23a36 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74 65   & pParse->write
23a37 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20  Mask)!=0);.     
23a38 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e     if( db->init.
23a39 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  busy==0 ){.     
23a3a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23a3b 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72 69  AddOp2(v,OP_Veri
23a3c 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 70  fyCookie, iDb, p
23a3d 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c  Parse->cookieVal
23a3e 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20 20  ue[iDb]);.      
23a3f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e    }.      }.#ifn
23a40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23a41 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
23a42 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74     {.        int
23a43 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   i;.        for(
23a44 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e  i=0; i<pParse->n
23a45 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a  VtabLock; i++){.
23a46 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
23a47 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 73  vtab = (char *)s
23a48 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
23a49 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74  db, pParse->apVt
23a4a 61 62 4c 6f 63 6b 5b 69 5d 29 3b 0a 20 20 20 20  abLock[i]);.    
23a4b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23a4c 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 42  eAddOp4(v, OP_VB
23a4d 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 76  egin, 0, 0, 0, v
23a4e 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
23a4f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
23a50 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f   pParse->nVtabLo
23a51 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ck = 0;.      }.
23a52 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a  #endif..      /*
23a53 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 63 6f   Once all the co
23a54 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65 6e 20  okies have been 
23a55 76 65 72 69 66 69 65 64 20 61 6e 64 20 74 72 61  verified and tra
23a56 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64  nsactions opened
23a57 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 61  , .      ** obta
23a58 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 20  in the required 
23a59 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 69  table-locks. Thi
23a5a 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c  s is a no-op unl
23a5b 65 73 73 20 74 68 65 20 0a 20 20 20 20 20 20 2a  ess the .      *
23a5c 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66  * shared-cache f
23a5d 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65  eature is enable
23a5e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
23a5f 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73    codeTableLocks
23a60 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20  (pParse);..     
23a61 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61   /* Initialize a
23a62 6e 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  ny AUTOINCREMENT
23a63 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
23a64 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20   required..     
23a65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
23a66 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65  3AutoincrementBe
23a67 67 69 6e 28 70 50 61 72 73 65 29 3b 0a 0a 20 20  gin(pParse);..  
23a68 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20      /* Finally, 
23a69 6a 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68 65  jump back to the
23a6a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
23a6b 65 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f 64  e executable cod
23a6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  e. */.      sqli
23a6d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23a6e 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61   OP_Goto, 0, pPa
23a6f 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29  rse->cookieGoto)
23a70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20  ;.    }.  }...  
23a71 2f 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20  /* Get the VDBE 
23a72 70 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f  program ready fo
23a73 72 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f  r execution.  */
23a74 0a 20 20 69 66 28 20 76 20 26 26 20 41 4c 57 41  .  if( v && ALWA
23a75 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  YS(pParse->nErr=
23a76 3d 30 29 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c  =0) && !db->mall
23a77 6f 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64  ocFailed ){.#ifd
23a78 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
23a79 20 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20      FILE *trace 
23a7a 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  = (db->flags & S
23a7b 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 29  QLITE_VdbeTrace)
23a7c 21 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30  !=0 ? stdout : 0
23a7d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23a7e 65 54 72 61 63 65 28 76 2c 20 74 72 61 63 65 29  eTrace(v, trace)
23a7f 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73  ;.#endif.    ass
23a80 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61  ert( pParse->iCa
23a81 63 68 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20  cheLevel==0 );  
23a82 2f 2a 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20  /* Disables and 
23a83 72 65 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68  re-enables match
23a84 20 2a 2f 0a 20 20 20 20 2f 2a 20 41 20 6d 69 6e   */.    /* A min
23a85 69 6d 75 6d 20 6f 66 20 6f 6e 65 20 63 75 72 73  imum of one curs
23a86 6f 72 20 69 73 20 72 65 71 75 69 72 65 64 20 69  or is required i
23a87 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  f autoincrement 
23a88 69 73 20 75 73 65 64 0a 20 20 20 20 2a 20 20 53  is used.    *  S
23a89 65 65 20 74 69 63 6b 65 74 20 5b 61 36 39 36 33  ee ticket [a6963
23a8a 37 39 63 31 66 30 38 38 36 36 5d 20 2a 2f 0a 20  79c1f08866] */. 
23a8b 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70     if( pParse->p
23a8c 41 69 6e 63 21 3d 30 20 26 26 20 70 50 61 72 73  Ainc!=0 && pPars
23a8d 65 2d 3e 6e 54 61 62 3d 3d 30 20 29 20 70 50 61  e->nTab==0 ) pPa
23a8e 72 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20  rse->nTab = 1;. 
23a8f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61     sqlite3VdbeMa
23a90 6b 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73  keReady(v, pPars
23a91 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65 2d  e->nVar, pParse-
23a92 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20  >nMem,.         
23a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23a94 70 50 61 72 73 65 2d 3e 6e 54 61 62 2c 20 70 50  pParse->nTab, pP
23a95 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 2c 20 70  arse->nMaxArg, p
23a96 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 2c 0a  Parse->explain,.
23a97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23a98 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
23a99 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 26 26  >isMultiWrite &&
23a9a 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72   pParse->mayAbor
23a9b 74 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  t);.    pParse->
23a9c 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
23a9d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f  ;.    pParse->co
23a9e 6c 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20  lNamesSet = 0;. 
23a9f 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73   }else if( pPars
23aa0 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e->rc==SQLITE_OK
23aa1 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
23aa2 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
23aa3 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  R;.  }.  pParse-
23aa4 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61  >nTab = 0;.  pPa
23aa5 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20  rse->nMem = 0;. 
23aa6 20 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20   pParse->nSet = 
23aa7 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61  0;.  pParse->nVa
23aa8 72 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d  r = 0;.  pParse-
23aa9 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b  >cookieMask = 0;
23aaa 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  .  pParse->cooki
23aab 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  eGoto = 0;.}../*
23aac 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73  .** Run the pars
23aad 65 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  er and code gene
23aae 72 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c  rator recursivel
23aaf 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  y in order to ge
23ab0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
23ab1 6f 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  or the SQL state
23ab2 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20  ment given onto 
23ab3 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
23ab4 50 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a  Parse context.**
23ab5 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
23ab6 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20   construction.  
23ab7 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20  When the parser 
23ab8 69 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65  is run recursive
23ab9 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20  ly.** this way, 
23aba 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c  the final OP_Hal
23abb 74 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65  t is not appende
23abc 64 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74  d and other init
23abd 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e  ialization.** an
23abe 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73  d finalization s
23abf 74 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64  teps are omitted
23ac0 20 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61   because those a
23ac1 72 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74  re handling by t
23ac2 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20  he.** outermost 
23ac3 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  parser..**.** No
23ac4 74 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20  t everything is 
23ac5 6e 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20  nestable.  This 
23ac6 66 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69  facility is desi
23ac7 67 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a  gned to permit.*
23ac8 2a 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  * INSERT, UPDATE
23ac9 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65  , and DELETE ope
23aca 72 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20  rations against 
23acb 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20  SQLITE_MASTER.  
23acc 55 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79  Use.** care if y
23acd 6f 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79  ou decide to try
23ace 20 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75   to use this rou
23acf 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74  tine for some ot
23ad0 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f  her purposes..*/
23ad1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23ad2 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74  void sqlite3Nest
23ad3 65 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70  edParse(Parse *p
23ad4 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
23ad5 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
23ad6 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
23ad7 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
23ad8 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
23ad9 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  0;.  sqlite3 *db
23ada 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23   = pParse->db;.#
23adb 20 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20   define SAVE_SZ 
23adc 20 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20   (sizeof(Parse) 
23add 2d 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65  - offsetof(Parse
23ade 2c 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73  ,nVar)).  char s
23adf 61 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b  aveBuf[SAVE_SZ];
23ae0 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
23ae1 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20  nErr ) return;. 
23ae2 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
23ae3 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f  >nested<10 );  /
23ae4 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64  * Nesting should
23ae5 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69   only be of limi
23ae6 74 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76  ted depth */.  v
23ae7 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
23ae8 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73  mat);.  zSql = s
23ae9 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
23aea 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
23aeb 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
23aec 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a   if( zSql==0 ){.
23aed 20 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a      return;   /*
23aee 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68   A malloc must h
23aef 61 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20  ave failed */.  
23af0 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  }.  pParse->nest
23af1 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73  ed++;.  memcpy(s
23af2 61 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d  aveBuf, &pParse-
23af3 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b  >nVar, SAVE_SZ);
23af4 0a 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73  .  memset(&pPars
23af5 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45  e->nVar, 0, SAVE
23af6 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  _SZ);.  sqlite3R
23af7 75 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c  unParser(pParse,
23af8 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29   zSql, &zErrMsg)
23af9 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
23afa 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  e(db, zErrMsg);.
23afb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23afc 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d  db, zSql);.  mem
23afd 63 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  cpy(&pParse->nVa
23afe 72 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45  r, saveBuf, SAVE
23aff 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  _SZ);.  pParse->
23b00 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a  nested--;.}../*.
23b01 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
23b02 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
23b03 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
23b04 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
23b05 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
23b06 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
23b07 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64  f that table and
23b08 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68   (optionally) th
23b09 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
23b0a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
23b0b 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20  ning the table. 
23b0c 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
23b0d 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  not found..**.**
23b0e 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73   If zDatabase is
23b0f 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65   0, all database
23b10 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66  s are searched f
23b11 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  or the table and
23b12 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61   the.** first ma
23b13 74 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20  tching table is 
23b14 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
23b15 68 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c  hecking for dupl
23b16 69 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e  icate table.** n
23b17 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20  ames is done.)  
23b18 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72  The search order
23b19 20 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20   is TEMP first, 
23b1a 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20  then MAIN, then 
23b1b 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79  any.** auxiliary
23b1c 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64   databases added
23b1d 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43   using the ATTAC
23b1e 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  H command..**.**
23b1f 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65   See also sqlite
23b20 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a  3LocateTable()..
23b21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23b22 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
23b23 46 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65  FindTable(sqlite
23b24 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
23b25 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20  r *zName, const 
23b26 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29  char *zDatabase)
23b27 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30  {.  Table *p = 0
23b28 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
23b29 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74   nName;.  assert
23b2a 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20  ( zName!=0 );.  
23b2b 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
23b2c 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
23b2d 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d    for(i=OMIT_TEM
23b2e 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  PDB; i<db->nDb; 
23b2f 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20  i++){.    int j 
23b30 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20  = (i<2) ? i^1 : 
23b31 69 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20 54  i;   /* Search T
23b32 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20  EMP before MAIN 
23b33 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61  */.    if( zData
23b34 62 61 73 65 21 3d 30 20 26 26 20 73 71 6c 69 74  base!=0 && sqlit
23b35 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61 62  e3StrICmp(zDatab
23b36 61 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ase, db->aDb[j].
23b37 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75  zName) ) continu
23b38 65 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  e;.    p = sqlit
23b39 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e  e3HashFind(&db->
23b3a 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[j].pSchema->
23b3b 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  tblHash, zName, 
23b3c 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  nName);.    if( 
23b3d 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  p ) break;.  }. 
23b3e 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
23b3f 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69  .** Locate the i
23b40 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  n-memory structu
23b41 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
23b42 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  s a particular d
23b43 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65  atabase.** table
23b44 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20   given the name 
23b45 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e  of that table an
23b46 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74  d (optionally) t
23b47 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
23b48 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  * database conta
23b49 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e  ining the table.
23b4a 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66    Return NULL if
23b4b 20 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73   not found.  Als
23b4c 6f 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72  o leave an.** er
23b4d 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
23b4e 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a  Parse->zErrMsg..
23b4f 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72  **.** The differ
23b50 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69  ence between thi
23b51 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73 71  s routine and sq
23b52 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29  lite3FindTable()
23b53 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a   is that this.**
23b54 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 20   routine leaves 
23b55 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
23b56 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   in pParse->zErr
23b57 4d 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c  Msg where.** sql
23b58 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20  ite3FindTable() 
23b59 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c  does not..*/.SQL
23b5a 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
23b5b 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  e *sqlite3Locate
23b5c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
23b5d 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
23b5e 2f 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68  /* context in wh
23b5f 69 63 68 20 74 6f 20 72 65 70 6f 72 74 20 65 72  ich to report er
23b60 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73  rors */.  int is
23b61 56 69 65 77 2c 20 20 20 20 20 20 20 20 20 20 20  View,           
23b62 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b   /* True if look
23b63 69 6e 67 20 66 6f 72 20 61 20 56 49 45 57 20 72  ing for a VIEW r
23b64 61 74 68 65 72 20 74 68 61 6e 20 61 20 54 41 42  ather than a TAB
23b65 4c 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  LE */.  const ch
23b66 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f  ar *zName,     /
23b67 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
23b68 62 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ble we are looki
23b69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73  ng for */.  cons
23b6a 74 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 20  t char *zDbase  
23b6b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
23b6c 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 67  e database.  Mig
23b6d 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b  ht be NULL */.){
23b6e 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20  .  Table *p;..  
23b6f 2f 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61  /* Read the data
23b70 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20  base schema. If 
23b71 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
23b72 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
23b73 6d 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64  message.  ** and
23b74 20 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20   code in pParse 
23b75 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
23b76 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
23b77 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
23b78 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
23b79 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
23b7a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74    }..  p = sqlit
23b7b 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72  e3FindTable(pPar
23b7c 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a  se->db, zName, z
23b7d 44 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d  Dbase);.  if( p=
23b7e 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  =0 ){.    const 
23b7f 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69 73 56  char *zMsg = isV
23b80 69 65 77 20 3f 20 22 6e 6f 20 73 75 63 68 20 76  iew ? "no such v
23b81 69 65 77 22 20 3a 20 22 6e 6f 20 73 75 63 68 20  iew" : "no such 
23b82 74 61 62 6c 65 22 3b 0a 20 20 20 20 69 66 28 20  table";.    if( 
23b83 7a 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20  zDbase ){.      
23b84 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
23b85 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e  pParse, "%s: %s.
23b86 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73  %s", zMsg, zDbas
23b87 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  e, zName);.    }
23b88 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
23b89 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23b8a 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d  se, "%s: %s", zM
23b8b 73 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  sg, zName);.    
23b8c 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68  }.    pParse->ch
23b8d 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20  eckSchema = 1;. 
23b8e 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
23b8f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74  ../*.** Locate t
23b90 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72  he in-memory str
23b91 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
23b92 72 69 62 65 73 20 0a 2a 2a 20 61 20 70 61 72 74  ribes .** a part
23b93 69 63 75 6c 61 72 20 69 6e 64 65 78 20 67 69 76  icular index giv
23b94 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  en the name of t
23b95 68 61 74 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64  hat index.** and
23b96 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
23b97 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63   database that c
23b98 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65  ontains the inde
23b99 78 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c  x..** Return NUL
23b9a 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  L if not found..
23b9b 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61  **.** If zDataba
23b9c 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74  se is 0, all dat
23b9d 61 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63  abases are searc
23b9e 68 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74  hed for the.** t
23b9f 61 62 6c 65 20 61 6e 64 20 74 68 65 20 66 69 72  able and the fir
23ba0 73 74 20 6d 61 74 63 68 69 6e 67 20 69 6e 64 65  st matching inde
23ba1 78 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  x is returned.  
23ba2 28 4e 6f 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20  (No checking.** 
23ba3 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20 69 6e  for duplicate in
23ba4 64 65 78 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e  dex names is don
23ba5 65 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20  e.)  The search 
23ba6 6f 72 64 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50  order is.** TEMP
23ba7 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49   first, then MAI
23ba8 4e 2c 20 74 68 65 6e 20 61 6e 79 20 61 75 78 69  N, then any auxi
23ba9 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20  liary databases 
23baa 61 64 64 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74  added.** using t
23bab 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e  he ATTACH comman
23bac 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
23bad 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69  VATE Index *sqli
23bae 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c  te3FindIndex(sql
23baf 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
23bb0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e  char *zName, con
23bb1 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20  st char *zDb){. 
23bb2 20 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20   Index *p = 0;. 
23bb3 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e   int i;.  int nN
23bb4 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
23bb5 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
23bb6 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44  for(i=OMIT_TEMPD
23bb7 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  B; i<db->nDb; i+
23bb8 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20  +){.    int j = 
23bb9 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b  (i<2) ? i^1 : i;
23bba 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50    /* Search TEMP
23bbb 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a   before MAIN */.
23bbc 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68      Schema *pSch
23bbd 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d  ema = db->aDb[j]
23bbe 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 61 73  .pSchema;.    as
23bbf 73 65 72 74 28 20 70 53 63 68 65 6d 61 20 29 3b  sert( pSchema );
23bc0 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26 26 20  .    if( zDb && 
23bc1 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
23bc2 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a  Db, db->aDb[j].z
23bc3 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65  Name) ) continue
23bc4 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
23bc5 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68 65  3HashFind(&pSche
23bc6 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61  ma->idxHash, zNa
23bc7 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
23bc8 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20  if( p ) break;. 
23bc9 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
23bca 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20  ../*.** Reclaim 
23bcb 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  the memory used 
23bcc 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a 73  by an index.*/.s
23bcd 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 49  tatic void freeI
23bce 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a  ndex(Index *p){.
23bcf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23bd0 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d  p->pTable->dbMem
23bd1 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
23bd2 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20  _OMIT_ANALYZE.  
23bd3 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
23bd4 65 78 53 61 6d 70 6c 65 73 28 70 29 3b 0a 23 65  exSamples(p);.#e
23bd5 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 44 62  ndif.  sqlite3Db
23bd6 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c  Free(db, p->zCol
23bd7 41 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Aff);.  sqlite3D
23bd8 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a  bFree(db, p);.}.
23bd9 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
23bda 65 20 67 69 76 65 6e 20 69 6e 64 65 78 20 66 72  e given index fr
23bdb 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 68 61 73  om the index has
23bdc 68 20 74 61 62 6c 65 2c 20 61 6e 64 20 66 72 65  h table, and fre
23bdd 65 0a 2a 2a 20 69 74 73 20 6d 65 6d 6f 72 79 20  e.** its memory 
23bde 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a  structures..**.*
23bdf 2a 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 72  * The index is r
23be0 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
23be1 64 61 74 61 62 61 73 65 20 68 61 73 68 20 74 61  database hash ta
23be2 62 6c 65 73 20 62 75 74 0a 2a 2a 20 69 74 20 69  bles but.** it i
23be3 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 65 64 20 66  s not unlinked f
23be4 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 74 68  rom the Table th
23be5 61 74 20 69 74 20 69 6e 64 65 78 65 73 2e 0a 2a  at it indexes..*
23be6 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d  * Unlinking from
23be7 20 74 68 65 20 54 61 62 6c 65 20 6d 75 73 74 20   the Table must 
23be8 62 65 20 64 6f 6e 65 20 62 79 20 74 68 65 20 63  be done by the c
23be9 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
23bea 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23beb 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
23bec 65 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20  ex(Index *p){.  
23bed 49 6e 64 65 78 20 2a 70 4f 6c 64 3b 0a 20 20 63  Index *pOld;.  c
23bee 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
23bef 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20 20   = p->zName;..  
23bf0 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48 61  pOld = sqlite3Ha
23bf1 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70 53 63  shInsert(&p->pSc
23bf2 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a  hema->idxHash, z
23bf3 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
23bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23bf5 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
23bf6 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20 20 61  (zName), 0);.  a
23bf7 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 30 20 7c  ssert( pOld==0 |
23bf8 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66  | pOld==p );.  f
23bf9 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a  reeIndex(p);.}..
23bfa 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e  /*.** For the in
23bfb 64 65 78 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e  dex called zIdxN
23bfc 61 6d 65 20 77 68 69 63 68 20 69 73 20 66 6f 75  ame which is fou
23bfd 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nd in the databa
23bfe 73 65 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b  se iDb,.** unlik
23bff 65 20 74 68 61 74 20 69 6e 64 65 78 20 66 72 6f  e that index fro
23c00 6d 20 69 74 73 20 54 61 62 6c 65 20 74 68 65 6e  m its Table then
23c01 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65   remove the inde
23c02 78 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e  x from.** the in
23c03 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 20 61  dex hash table a
23c04 6e 64 20 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f  nd free all memo
23c05 72 79 20 73 74 72 75 63 74 75 72 65 73 20 61 73  ry structures as
23c06 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
23c07 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53   the index..*/.S
23c08 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23c09 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
23c0a 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
23c0b 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
23c0c 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
23c0d 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e  *zIdxName){.  In
23c0e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69  dex *pIndex;.  i
23c0f 6e 74 20 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a  nt len;.  Hash *
23c10 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 62  pHash = &db->aDb
23c11 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69  [iDb].pSchema->i
23c12 64 78 48 61 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d  dxHash;..  len =
23c13 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
23c14 28 7a 49 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49  (zIdxName);.  pI
23c15 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 48 61  ndex = sqlite3Ha
23c16 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20  shInsert(pHash, 
23c17 7a 49 64 78 4e 61 6d 65 2c 20 6c 65 6e 2c 20 30  zIdxName, len, 0
23c18 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  );.  if( pIndex 
23c19 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65  ){.    if( pInde
23c1a 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
23c1b 78 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  x==pIndex ){.   
23c1c 20 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c     pIndex->pTabl
23c1d 65 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64  e->pIndex = pInd
23c1e 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ex->pNext;.    }
23c1f 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65  else{.      Inde
23c20 78 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a 20 4a  x *p;.      /* J
23c21 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20  ustification of 
23c22 41 4c 57 41 59 53 28 29 3b 20 20 54 68 65 20 69  ALWAYS();  The i
23c23 6e 64 65 78 20 6d 75 73 74 20 62 65 20 6f 6e 20  ndex must be on 
23c24 74 68 65 20 6c 69 73 74 20 6f 66 0a 20 20 20 20  the list of.    
23c25 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 2a 2f    ** indices. */
23c26 0a 20 20 20 20 20 20 70 20 3d 20 70 49 6e 64 65  .      p = pInde
23c27 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65  x->pTable->pInde
23c28 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  x;.      while( 
23c29 41 4c 57 41 59 53 28 70 29 20 26 26 20 70 2d 3e  ALWAYS(p) && p->
23c2a 70 4e 65 78 74 21 3d 70 49 6e 64 65 78 20 29 7b  pNext!=pIndex ){
23c2b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 7d   p = p->pNext; }
23c2c 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
23c2d 53 28 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d  S(p && p->pNext=
23c2e 3d 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20  =pIndex) ){.    
23c2f 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
23c30 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20  Index->pNext;.  
23c31 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
23c32 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78  freeIndex(pIndex
23c33 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61  );.  }.  db->fla
23c34 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
23c35 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f  ernChanges;.}../
23c36 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 73  *.** Erase all s
23c37 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
23c38 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65  n from the in-me
23c39 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 73  mory hash tables
23c3a 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20   of.** a single 
23c3b 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
23c3c 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
23c3d 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d  d to reclaim mem
23c3e 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68  ory.** before th
23c3f 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f 73 65  e database close
23c40 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 20 63  s.  It is also c
23c41 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 72  alled during a r
23c42 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68  ollback.** if th
23c43 65 72 65 20 77 65 72 65 20 73 63 68 65 6d 61 20  ere were schema 
23c44 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67 20 74  changes during t
23c45 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
23c46 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61  r if a.** schema
23c47 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68  -cookie mismatch
23c48 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49   occurs..**.** I
23c49 66 20 69 44 62 3d 3d 30 20 74 68 65 6e 20 72 65  f iDb==0 then re
23c4a 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  set the internal
23c4b 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 20 66   schema tables f
23c4c 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a  or all database.
23c4d 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20 69 44  ** files.  If iD
23c4e 62 3e 3d 31 20 74 68 65 6e 20 72 65 73 65 74 20  b>=1 then reset 
23c4f 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  the internal sch
23c50 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65  ema for only the
23c51 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20  .** single file 
23c52 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51  indicated..*/.SQ
23c53 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23c54 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e  d sqlite3ResetIn
23c55 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c  ternalSchema(sql
23c56 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
23c57 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  b){.  int i, j;.
23c58 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
23c59 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
23c5a 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d 3d 30  );..  if( iDb==0
23c5b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
23c5c 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
23c5d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44  ;.  }.  for(i=iD
23c5e 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  b; i<db->nDb; i+
23c5f 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  +){.    Db *pDb 
23c60 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20  = &db->aDb[i];. 
23c61 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68     if( pDb->pSch
23c62 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ema ){.      ass
23c63 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62  ert(i==1 || (pDb
23c64 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74 65 33  ->pBt && sqlite3
23c65 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
23c66 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20  pDb->pBt)));.   
23c67 20 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61     sqlite3Schema
23c68 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68 65 6d  Free(pDb->pSchem
23c69 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  a);.    }.    if
23c6a 28 20 69 44 62 3e 30 20 29 20 72 65 74 75 72 6e  ( iDb>0 ) return
23c6b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
23c6c 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e  iDb==0 );.  db->
23c6d 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45  flags &= ~SQLITE
23c6e 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a  _InternChanges;.
23c6f 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
23c70 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a 20 20 73  ockList(db);.  s
23c71 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
23c72 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49  All(db);..  /* I
23c73 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  f one or more of
23c74 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
23c75 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68 61  atabase files ha
23c76 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 2c 0a 20  s been closed,. 
23c77 20 2a 2a 20 74 68 65 6e 20 72 65 6d 6f 76 65 20   ** then remove 
23c78 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20 61 75  them from the au
23c79 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
23c7a 20 6c 69 73 74 2e 20 20 57 65 20 74 61 6b 65 20   list.  We take 
23c7b 74 68 65 0a 20 20 2a 2a 20 6f 70 70 6f 72 74 75  the.  ** opportu
23c7c 6e 69 74 79 20 74 6f 20 64 6f 20 74 68 69 73 20  nity to do this 
23c7d 68 65 72 65 20 73 69 6e 63 65 20 77 65 20 68 61  here since we ha
23c7e 76 65 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20  ve just deleted 
23c7f 61 6c 6c 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  all of the.  ** 
23c80 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 62 6c  schema hash tabl
23c81 65 73 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65  es and therefore
23c82 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20   do not have to 
23c83 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73  make any changes
23c84 0a 20 20 2a 2a 20 74 6f 20 61 6e 79 20 6f 66 20  .  ** to any of 
23c85 74 68 6f 73 65 20 74 61 62 6c 65 73 2e 0a 20 20  those tables..  
23c86 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20  */.  for(i=j=2; 
23c87 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
23c88 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20 2a  .    struct Db *
23c89 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
23c8a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e  ];.    if( pDb->
23c8b 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pBt==0 ){.      
23c8c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23c8d 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  , pDb->zName);. 
23c8e 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20       pDb->zName 
23c8f 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  = 0;.      conti
23c90 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
23c91 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20  f( j<i ){.      
23c92 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d  db->aDb[j] = db-
23c93 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  >aDb[i];.    }. 
23c94 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65     j++;.  }.  me
23c95 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d  mset(&db->aDb[j]
23c96 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29  , 0, (db->nDb-j)
23c97 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b  *sizeof(db->aDb[
23c98 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20  j]));.  db->nDb 
23c99 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e  = j;.  if( db->n
23c9a 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61 44 62  Db<=2 && db->aDb
23c9b 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20  !=db->aDbStatic 
23c9c 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62  ){.    memcpy(db
23c9d 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64 62 2d  ->aDbStatic, db-
23c9e 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64  >aDb, 2*sizeof(d
23c9f 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20  b->aDb[0]));.   
23ca0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
23ca1 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20  b, db->aDb);.   
23ca2 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61   db->aDb = db->a
23ca3 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a  DbStatic;.  }.}.
23ca4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
23ca5 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
23ca6 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75  en a commit occu
23ca7 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rs..*/.SQLITE_PR
23ca8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23ca9 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
23caa 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
23cab 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67  *db){.  db->flag
23cac 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74  s &= ~SQLITE_Int
23cad 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f  ernChanges;.}../
23cae 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63  *.** Clear the c
23caf 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d  olumn names from
23cb0 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77   a table or view
23cb1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23cb2 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75   sqliteResetColu
23cb3 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20 2a 70  mnNames(Table *p
23cb4 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b  Table){.  int i;
23cb5 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
23cb6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23cb7 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a   pTable->dbMem;.
23cb8 20 20 74 65 73 74 63 61 73 65 28 20 64 62 3d 3d    testcase( db==
23cb9 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
23cba 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  Table!=0 );.  if
23cbb 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62 6c 65  ( (pCol = pTable
23cbc 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a 20 20  ->aCol)!=0 ){.  
23cbd 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
23cbe 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  ble->nCol; i++, 
23cbf 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 73  pCol++){.      s
23cc0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23cc1 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pCol->zName);. 
23cc2 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
23cc3 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d  Delete(db, pCol-
23cc4 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 73  >pDflt);.      s
23cc5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23cc6 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a 20   pCol->zDflt);. 
23cc7 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
23cc8 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79  ee(db, pCol->zTy
23cc9 70 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pe);.      sqlit
23cca 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f  e3DbFree(db, pCo
23ccb 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d  l->zColl);.    }
23ccc 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
23ccd 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61  ee(db, pTable->a
23cce 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20 70 54 61 62  Col);.  }.  pTab
23ccf 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->aCol = 0;.  
23cd0 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30  pTable->nCol = 0
23cd1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  ;.}../*.** Remov
23cd2 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 74  e the memory dat
23cd3 61 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73  a structures ass
23cd4 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
23cd5 20 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e   given.** Table.
23cd6 20 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65    No changes are
23cd7 20 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 79   made to disk by
23cd8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
23cd9 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23cda 65 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 74  e just deletes t
23cdb 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  he data structur
23cdc 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
23cdd 75 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61  unlink.** the ta
23cde 62 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ble data structu
23cdf 72 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  re from the hash
23ce0 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74 20   table.  But it 
23ce1 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a 20  does destroy.** 
23ce2 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
23ce3 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73  s of the indices
23ce4 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   and foreign key
23ce5 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
23ce6 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e  h .** the table.
23ce7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23ce8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
23ce9 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65  eleteTable(Table
23cea 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49 6e 64   *pTable){.  Ind
23ceb 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70 4e 65  ex *pIndex, *pNe
23cec 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  xt;.  sqlite3 *d
23ced 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 6c 65  b;..  if( pTable
23cee 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
23cef 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d  db = pTable->dbM
23cf0 65 6d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  em;.  testcase( 
23cf1 64 62 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44  db==0 );..  /* D
23cf2 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65  o not delete the
23cf3 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 74 68 65   table until the
23cf4 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
23cf5 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a   reaches zero. *
23cf6 2f 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66  /.  pTable->nRef
23cf7 2d 2d 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65  --;.  if( pTable
23cf8 2d 3e 6e 52 65 66 3e 30 20 29 7b 0a 20 20 20 20  ->nRef>0 ){.    
23cf9 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
23cfa 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 52  sert( pTable->nR
23cfb 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44  ef==0 );..  /* D
23cfc 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 63 65  elete all indice
23cfd 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
23cfe 68 20 74 68 69 73 20 74 61 62 6c 65 0a 20 20 2a  h this table.  *
23cff 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20 3d  /.  for(pIndex =
23d00 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b   pTable->pIndex;
23d01 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d   pIndex; pIndex=
23d02 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78  pNext){.    pNex
23d03 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78  t = pIndex->pNex
23d04 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
23d05 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d  Index->pSchema==
23d06 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20  pTable->pSchema 
23d07 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
23d08 6c 65 74 65 49 6e 64 65 78 28 70 49 6e 64 65 78  leteIndex(pIndex
23d09 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c  );.  }..  /* Del
23d0a 65 74 65 20 61 6e 79 20 66 6f 72 65 69 67 6e 20  ete any foreign 
23d0b 6b 65 79 73 20 61 74 74 61 63 68 65 64 20 74 6f  keys attached to
23d0c 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
23d0d 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74    sqlite3FkDelet
23d0e 65 28 70 54 61 62 6c 65 29 3b 0a 0a 20 20 2f 2a  e(pTable);..  /*
23d0f 20 44 65 6c 65 74 65 20 74 68 65 20 54 61 62 6c   Delete the Tabl
23d10 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  e structure itse
23d11 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  lf..  */.  sqlit
23d12 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  eResetColumnName
23d13 73 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c  s(pTable);.  sql
23d14 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
23d15 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  Table->zName);. 
23d16 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
23d17 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41  b, pTable->zColA
23d18 66 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  ff);.  sqlite3Se
23d19 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
23d1a 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b  Table->pSelect);
23d1b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23d1c 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c  OMIT_CHECK.  sql
23d1d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
23d1e 62 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63  b, pTable->pChec
23d1f 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c  k);.#endif.  sql
23d20 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 70 54  ite3VtabClear(pT
23d21 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  able);.  sqlite3
23d22 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
23d23 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c  e);.}../*.** Unl
23d24 69 6e 6b 20 74 68 65 20 67 69 76 65 6e 20 74 61  ink the given ta
23d25 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73  ble from the has
23d26 68 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65  h tables and the
23d27 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74   delete the.** t
23d28 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 77  able structure w
23d29 69 74 68 20 61 6c 6c 20 69 74 73 20 69 6e 64 69  ith all its indi
23d2a 63 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20  ces and foreign 
23d2b 6b 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  keys..*/.SQLITE_
23d2c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23d2d 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
23d2e 65 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33  eteTable(sqlite3
23d2f 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
23d30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e  onst char *zTabN
23d31 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ame){.  Table *p
23d32 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20  ;.  Db *pDb;..  
23d33 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
23d34 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
23d35 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
23d36 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54   );.  assert( zT
23d37 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61  abName && zTabNa
23d38 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d  me[0] );.  pDb =
23d39 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
23d3a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73    p = sqlite3Has
23d3b 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53  hInsert(&pDb->pS
23d3c 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
23d3d 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20  zTabName,.      
23d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d3f 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33    sqlite3Strlen3
23d40 30 28 7a 54 61 62 4e 61 6d 65 29 2c 30 29 3b 0a  0(zTabName),0);.
23d41 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
23d42 61 62 6c 65 28 70 29 3b 0a 20 20 64 62 2d 3e 66  able(p);.  db->f
23d43 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
23d44 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
23d45 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74  ./*.** Given a t
23d46 6f 6b 65 6e 2c 20 72 65 74 75 72 6e 20 61 20 73  oken, return a s
23d47 74 72 69 6e 67 20 74 68 61 74 20 63 6f 6e 73 69  tring that consi
23d48 73 74 73 20 6f 66 20 74 68 65 20 74 65 78 74 20  sts of the text 
23d49 6f 66 20 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e  of that.** token
23d4a 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
23d4b 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
23d4c 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61 69  ring.** is obtai
23d4d 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
23d4e 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74  alloc() and must
23d4f 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68 65   be freed by the
23d50 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63   calling.** func
23d51 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  tion..**.** Any 
23d52 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 20  quotation marks 
23d53 28 65 78 3a 20 20 22 6e 61 6d 65 22 2c 20 27 6e  (ex:  "name", 'n
23d54 61 6d 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20 6f 72  ame', [name], or
23d55 20 60 6e 61 6d 65 60 29 20 74 68 61 74 0a 2a 2a   `name`) that.**
23d56 20 73 75 72 72 6f 75 6e 64 20 74 68 65 20 62 6f   surround the bo
23d57 64 79 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  dy of the token 
23d58 61 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a  are removed..**.
23d59 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f 66  ** Tokens are of
23d5a 74 65 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65 72  ten just pointer
23d5b 73 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69  s into the origi
23d5c 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 61 6e 64  nal SQL text and
23d5d 20 73 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 5c   so.** are not \
23d5e 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61  000 terminated a
23d5f 6e 64 20 61 72 65 20 6e 6f 74 20 70 65 72 73 69  nd are not persi
23d60 73 74 65 6e 74 2e 20 20 54 68 65 20 72 65 74 75  stent.  The retu
23d61 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69  rned string.** i
23d62 73 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65  s \000 terminate
23d63 64 20 61 6e 64 20 69 73 20 70 65 72 73 69 73 74  d and is persist
23d64 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
23d65 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
23d66 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
23d67 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  n(sqlite3 *db, T
23d68 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20  oken *pName){.  
23d69 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
23d6a 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( pName ){.    
23d6b 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
23d6c 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
23d6d 61 72 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e  ar*)pName->z, pN
23d6e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c  ame->n);.    sql
23d6f 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d  ite3Dequote(zNam
23d70 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
23d71 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a   zName = 0;.  }.
23d72 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a    return zName;.
23d73 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  }../*.** Open th
23d74 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
23d75 74 61 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20  table stored in 
23d76 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  database number 
23d77 69 44 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69  iDb for.** writi
23d78 6e 67 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73  ng. The table is
23d79 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75   opened using cu
23d7a 72 73 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  rsor 0..*/.SQLIT
23d7b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23d7c 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
23d7d 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20  Table(Parse *p, 
23d7e 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65  int iDb){.  Vdbe
23d7f 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
23d80 56 64 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74  Vdbe(p);.  sqlit
23d81 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  e3TableLock(p, i
23d82 44 62 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  Db, MASTER_ROOT,
23d83 20 31 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45   1, SCHEMA_TABLE
23d84 28 69 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65  (iDb));.  sqlite
23d85 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23d86 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20  P_OpenWrite, 0, 
23d87 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62  MASTER_ROOT, iDb
23d88 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
23d89 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
23d8a 28 63 68 61 72 20 2a 29 35 2c 20 50 34 5f 49 4e  (char *)5, P4_IN
23d8b 54 33 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c 75  T32);  /* 5 colu
23d8c 6d 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66  mn table */.  if
23d8d 28 20 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a  ( p->nTab==0 ){.
23d8e 20 20 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b      p->nTab = 1;
23d8f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  .  }.}../*.** Pa
23d90 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f  rameter zName po
23d91 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65  ints to a nul-te
23d92 72 6d 69 6e 61 74 65 64 20 62 75 66 66 65 72 20  rminated buffer 
23d93 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e  containing the n
23d94 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61  ame.** of a data
23d95 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74  base ("main", "t
23d96 65 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65  emp" or the name
23d97 20 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20   of an attached 
23d98 64 62 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e  db). This.** fun
23d99 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
23d9a 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e  e index of the n
23d9b 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 69 6e  amed database in
23d9c 20 64 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a   db->aDb[], or.*
23d9d 2a 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65  * -1 if the name
23d9e 64 20 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 66  d db cannot be f
23d9f 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ound..*/.SQLITE_
23da0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
23da1 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71  te3FindDbName(sq
23da2 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
23da3 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
23da4 20 69 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 20   int i = -1;    
23da5 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
23da6 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 28   number */.  if(
23da7 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 62   zName ){.    Db
23da8 20 2a 70 44 62 3b 0a 20 20 20 20 69 6e 74 20 6e   *pDb;.    int n
23da9 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
23daa 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66  30(zName);.    f
23dab 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29  or(i=(db->nDb-1)
23dac 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69  , pDb=&db->aDb[i
23dad 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44  ]; i>=0; i--, pD
23dae 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20  b--){.      if( 
23daf 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c  (!OMIT_TEMPDB ||
23db0 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 71   i!=1 ) && n==sq
23db1 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44  lite3Strlen30(pD
23db2 62 2d 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20  b->zName) && .  
23db3 20 20 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74          0==sqlit
23db4 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a  e3StrICmp(pDb->z
23db5 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a  Name, zName) ){.
23db6 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
23db7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
23db8 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a  .  return i;.}..
23db9 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e 20  /*.** The token 
23dba 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20  *pName contains 
23dbb 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61  the name of a da
23dbc 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20 22  tabase (either "
23dbd 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 6d  main" or.** "tem
23dbe 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f  p" or the name o
23dbf 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62  f an attached db
23dc0 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ). This routine 
23dc1 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69  returns the.** i
23dc2 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d 65  ndex of the name
23dc3 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 62  d database in db
23dc4 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 69  ->aDb[], or -1 i
23dc5 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 0a  f the named db .
23dc6 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  ** does not exis
23dc7 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
23dc8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
23dc9 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20 2a  FindDb(sqlite3 *
23dca 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  db, Token *pName
23dcb 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
23dcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23dcd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
23dce 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  abase number */.
23dcf 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
23dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23dd1 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 77         /* Name w
23dd2 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
23dd3 66 6f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d  for */.  zName =
23dd4 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
23dd5 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29  Token(db, pName)
23dd6 3b 0a 20 20 69 20 3d 20 73 71 6c 69 74 65 33 46  ;.  i = sqlite3F
23dd7 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e  indDbName(db, zN
23dd8 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
23dd9 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
23dda 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  ;.  return i;.}.
23ddb 0a 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72  ./* The table or
23ddc 20 76 69 65 77 20 6f 72 20 74 72 69 67 67 65 72   view or trigger
23ddd 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64 20   name is passed 
23dde 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
23ddf 76 69 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e  via tokens.** pN
23de0 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e  ame1 and pName2.
23de1 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61   If the table na
23de2 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61  me was fully qua
23de3 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d  lified, for exam
23de4 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54  ple:.**.** CREAT
23de5 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20  E TABLE xxx.yyy 
23de6 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68  (...);.** .** Th
23de7 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74  en pName1 is set
23de8 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e   to "xxx" and pN
23de9 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74  ame2 "yyy". On t
23dea 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66  he other hand if
23deb 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61  .** the table na
23dec 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20  me is not fully 
23ded 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a  qualified, i.e.:
23dee 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41  .**.** CREATE TA
23def 42 4c 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a  BLE yyy(...);.**
23df0 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20  .** Then pName1 
23df1 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20  is set to "yyy" 
23df2 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22  and pName2 is ""
23df3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
23df4 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70  tine sets the *p
23df5 70 55 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20  pUnqual pointer 
23df6 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
23df7 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72  token (pName1 or
23df8 0a 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74  .** pName2) that
23df9 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75   stores the unqu
23dfa 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61  alified table na
23dfb 6d 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f  me.  The index o
23dfc 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
23dfd 65 20 22 78 78 78 22 20 69 73 20 72 65 74 75 72  e "xxx" is retur
23dfe 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
23dff 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
23e00 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20  e3TwoPartName(. 
23e01 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
23e02 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
23e03 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
23e04 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
23e05 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
23e06 20 20 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78       /* The "xxx
23e07 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78  " in the name "x
23e08 78 78 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22  xx.yyy" or "xxx"
23e09 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
23e0a 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me2,      /* The
23e0b 20 22 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61   "yyy" in the na
23e0c 6d 65 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a  me "xxx.yyy" */.
23e0d 20 20 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61    Token **pUnqua
23e0e 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  l     /* Write t
23e0f 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f  he unqualified o
23e10 62 6a 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20  bject name here 
23e11 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  */.){.  int iDb;
23e12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e13 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
23e14 68 6f 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65  holding the obje
23e15 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ct */.  sqlite3 
23e16 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
23e17 3b 0a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ;..  if( ALWAYS(
23e18 70 4e 61 6d 65 32 21 3d 30 29 20 26 26 20 70 4e  pName2!=0) && pN
23e19 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20  ame2->n>0 ){.   
23e1a 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75   if( db->init.bu
23e1b 73 79 20 29 20 7b 0a 20 20 20 20 20 20 73 71 6c  sy ) {.      sql
23e1c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23e1d 72 73 65 2c 20 22 63 6f 72 72 75 70 74 20 64 61  rse, "corrupt da
23e1e 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20  tabase");.      
23e1f 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
23e20 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b        return -1;
23e21 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71  .    }.    *pUnq
23e22 75 61 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20  ual = pName2;.  
23e23 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46    iDb = sqlite3F
23e24 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31  indDb(db, pName1
23e25 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30  );.    if( iDb<0
23e26 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23e27 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23e28 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62  , "unknown datab
23e29 61 73 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29  ase %T", pName1)
23e2a 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
23e2b 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65  nErr++;.      re
23e2c 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  turn -1;.    }. 
23e2d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
23e2e 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62  rt( db->init.iDb
23e2f 3d 3d 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e  ==0 || db->init.
23e30 62 75 73 79 20 29 3b 0a 20 20 20 20 69 44 62 20  busy );.    iDb 
23e31 3d 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a  = db->init.iDb;.
23e32 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70      *pUnqual = p
23e33 4e 61 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74  Name1;.  }.  ret
23e34 75 72 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn iDb;.}../*.*
23e35 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23e36 73 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20  s used to check 
23e37 69 66 20 74 68 65 20 55 54 46 2d 38 20 73 74 72  if the UTF-8 str
23e38 69 6e 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c  ing zName is a l
23e39 65 67 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66  egal.** unqualif
23e3a 69 65 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e  ied name for a n
23e3b 65 77 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74  ew schema object
23e3c 20 28 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20   (table, index, 
23e3d 76 69 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67  view or.** trigg
23e3e 65 72 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61  er). All names a
23e3f 72 65 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20  re legal except 
23e40 74 68 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e  those that begin
23e41 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67   with the string
23e42 0a 2a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69  .** "sqlite_" (i
23e43 6e 20 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f  n upper, lower o
23e44 72 20 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54  r mixed case). T
23e45 68 69 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  his portion of t
23e46 68 65 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20  he namespace.** 
23e47 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72 20  is reserved for 
23e48 69 6e 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f  internal use..*/
23e49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23e4a 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b  int sqlite3Check
23e4b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65  ObjectName(Parse
23e4c 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
23e4d 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
23e4e 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d  if( !pParse->db-
23e4f 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50  >init.busy && pP
23e50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
23e51 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70  .          && (p
23e52 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
23e53 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53   & SQLITE_WriteS
23e54 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20  chema)==0.      
23e55 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65      && 0==sqlite
23e56 33 53 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c  3StrNICmp(zName,
23e57 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29   "sqlite_", 7) )
23e58 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
23e59 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
23e5a 62 6a 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72  bject name reser
23e5b 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  ved for internal
23e5c 20 75 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65   use: %s", zName
23e5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
23e5e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
23e5f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
23e60 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67  OK;.}../*.** Beg
23e61 69 6e 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20  in constructing 
23e62 61 20 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72  a new table repr
23e63 65 73 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65  esentation in me
23e64 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a  mory.  This is.*
23e65 2a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 73  * the first of s
23e66 65 76 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f  everal action ro
23e67 75 74 69 6e 65 73 20 74 68 61 74 20 67 65 74 20  utines that get 
23e68 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e  called in respon
23e69 73 65 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54  se.** to a CREAT
23e6a 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
23e6b 74 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61  t.  In particula
23e6c 72 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  r, this routine 
23e6d 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74  is called.** aft
23e6e 65 72 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73  er seeing tokens
23e6f 20 22 43 52 45 41 54 45 22 20 61 6e 64 20 22 54   "CREATE" and "T
23e70 41 42 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61  ABLE" and the ta
23e71 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73  ble name. The is
23e72 54 65 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20  Temp.** flag is 
23e73 74 72 75 65 20 69 66 20 74 68 65 20 74 61 62 6c  true if the tabl
23e74 65 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72  e should be stor
23e75 65 64 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69  ed in the auxili
23e76 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ary database.** 
23e77 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20  file instead of 
23e78 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
23e79 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73  base file.  This
23e7a 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65   is normally the
23e7b 20 63 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68   case.** when th
23e7c 65 20 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d  e "TEMP" or "TEM
23e7d 50 4f 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20  PORARY" keyword 
23e7e 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 65  occurs in betwee
23e7f 6e 0a 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20  n.** CREATE and 
23e80 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  TABLE..**.** The
23e81 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72   new table recor
23e82 64 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  d is initialized
23e83 20 61 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72   and put in pPar
23e84 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a  se->pNewTable..*
23e85 2a 20 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65  * As more of the
23e86 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
23e87 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65  atement is parse
23e88 64 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63  d, additional ac
23e89 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
23e8a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
23e8b 74 6f 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f  to add more info
23e8c 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20  rmation to this 
23e8d 72 65 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68  record..** At th
23e8e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45  e end of the CRE
23e8f 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
23e90 65 6e 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33  ent, the sqlite3
23e91 45 6e 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69  EndTable() routi
23e92 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  ne.** is called 
23e93 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
23e94 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20  construction of 
23e95 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65  the new table re
23e96 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cord..*/.SQLITE_
23e97 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23e98 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 0a  ite3StartTable(.
23e99 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23e9a 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
23e9b 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  text */.  Token 
23e9c 2a 70 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69  *pName1,   /* Fi
23e9d 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  rst part of the 
23e9e 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
23e9f 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 54  e or view */.  T
23ea0 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
23ea1 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f  /* Second part o
23ea2 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  f the name of th
23ea3 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
23ea4 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c  */.  int isTemp,
23ea5 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
23ea6 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 20   this is a TEMP 
23ea7 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
23ea8 73 56 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 54  sView,      /* T
23ea9 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
23eaa 20 56 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69   VIEW */.  int i
23eab 73 56 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54  sVirtual,   /* T
23eac 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
23ead 20 56 49 52 54 55 41 4c 20 74 61 62 6c 65 20 2a   VIRTUAL table *
23eae 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20  /.  int noErr   
23eaf 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69       /* Do nothi
23eb0 6e 67 20 69 66 20 74 61 62 6c 65 20 61 6c 72 65  ng if table alre
23eb1 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
23eb2 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  .  Table *pTable
23eb3 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  ;.  char *zName 
23eb4 3d 20 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65  = 0; /* The name
23eb5 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
23eb6 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
23eb7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
23eb8 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e  .  Vdbe *v;.  in
23eb9 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f  t iDb;         /
23eba 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
23ebb 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  r to create the 
23ebc 74 61 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f  table in */.  To
23ebd 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f  ken *pName;    /
23ebe 2a 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61  * Unqualified na
23ebf 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
23ec0 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20  to create */..  
23ec1 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20  /* The table or 
23ec2 76 69 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 65  view name to cre
23ec3 61 74 65 20 69 73 20 70 61 73 73 65 64 20 74 6f  ate is passed to
23ec4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69   this routine vi
23ec5 61 20 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e  a tokens.  ** pN
23ec6 61 6d 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e  ame1 and pName2.
23ec7 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61   If the table na
23ec8 6d 65 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61  me was fully qua
23ec9 6c 69 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d  lified, for exam
23eca 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43  ple:.  **.  ** C
23ecb 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e  REATE TABLE xxx.
23ecc 79 79 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20  yyy (...);.  ** 
23ecd 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65  .  ** Then pName
23ece 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78  1 is set to "xxx
23ecf 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79  " and pName2 "yy
23ed0 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  y". On the other
23ed1 20 68 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68   hand if.  ** th
23ed2 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
23ed3 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  not fully qualif
23ed4 69 65 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a  ied, i.e.:.  **.
23ed5 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c    ** CREATE TABL
23ed6 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a  E yyy(...);.  **
23ed7 0a 20 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65  .  ** Then pName
23ed8 31 20 69 73 20 73 65 74 20 74 6f 20 22 79 79 79  1 is set to "yyy
23ed9 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20  " and pName2 is 
23eda 22 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ""..  **.  ** Th
23edb 65 20 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74  e call below set
23edc 73 20 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e  s the pName poin
23edd 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  ter to point at 
23ede 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65  the token (pName
23edf 31 20 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32  1 or.  ** pName2
23ee0 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68  ) that stores th
23ee1 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61  e unqualified ta
23ee2 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61  ble name. The va
23ee3 72 69 61 62 6c 65 20 69 44 62 20 69 73 0a 20 20  riable iDb is.  
23ee4 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e  ** set to the in
23ee5 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
23ee6 61 73 65 20 74 68 61 74 20 74 68 65 20 74 61 62  ase that the tab
23ee7 6c 65 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f  le or view is to
23ee8 20 62 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64   be.  ** created
23ee9 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20   in..  */.  iDb 
23eea 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
23eeb 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
23eec 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e  me1, pName2, &pN
23eed 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c  ame);.  if( iDb<
23eee 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
23eef 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
23ef0 26 20 69 73 54 65 6d 70 20 26 26 20 69 44 62 3e  & isTemp && iDb>
23ef1 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63  1 ){.    /* If c
23ef2 72 65 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74  reating a temp t
23ef3 61 62 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d  able, the name m
23ef4 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66  ay not be qualif
23ef5 69 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ied */.    sqlit
23ef6 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
23ef7 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61  e, "temporary ta
23ef8 62 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65  ble name must be
23ef9 20 75 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a   unqualified");.
23efa 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
23efb 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
23efc 44 42 20 26 26 20 69 73 54 65 6d 70 20 29 20 69  DB && isTemp ) i
23efd 44 62 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73  Db = 1;..  pPars
23efe 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20  e->sNameToken = 
23eff 2a 70 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20  *pName;.  zName 
23f00 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
23f01 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
23f02 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d  );.  if( zName==
23f03 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
23f04 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
23f05 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
23f06 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
23f07 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62  e) ){.    goto b
23f08 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
23f09 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e  ;.  }.  if( db->
23f0a 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73  init.iDb==1 ) is
23f0b 54 65 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65  Temp = 1;.#ifnde
23f0c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23f0d 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73  THORIZATION.  as
23f0e 73 65 72 74 28 20 28 69 73 54 65 6d 70 20 26 20  sert( (isTemp & 
23f0f 31 29 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20  1)==isTemp );.  
23f10 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a  {.    int code;.
23f11 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20      char *zDb = 
23f12 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
23f13 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  me;.    if( sqli
23f14 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
23f15 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45  rse, SQLITE_INSE
23f16 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  RT, SCHEMA_TABLE
23f17 28 69 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62  (isTemp), 0, zDb
23f18 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
23f19 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23f1a 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  r;.    }.    if(
23f1b 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20   isView ){.     
23f1c 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
23f1d 42 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20  B && isTemp ){. 
23f1e 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
23f1f 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
23f20 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c  _VIEW;.      }el
23f21 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65  se{.        code
23f22 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
23f23 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20  _VIEW;.      }. 
23f24 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23f25 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
23f26 20 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20   && isTemp ){.  
23f27 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
23f28 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
23f29 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c  TABLE;.      }el
23f2a 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65  se{.        code
23f2b 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
23f2c 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a  _TABLE;.      }.
23f2d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69      }.    if( !i
23f2e 73 56 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69  sVirtual && sqli
23f2f 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
23f30 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65  rse, code, zName
23f31 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
23f32 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
23f33 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ble_error;.    }
23f34 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
23f35 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
23f36 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64  new table name d
23f37 6f 65 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20  oes not collide 
23f38 77 69 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67  with an existing
23f39 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74  .  ** index or t
23f3a 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65  able name in the
23f3b 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20   same database. 
23f3c 20 49 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20   Issue an error 
23f3d 6d 65 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20  message if.  ** 
23f3e 69 74 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63  it does. The exc
23f3f 65 70 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65  eption is if the
23f40 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
23f41 20 70 61 72 73 65 64 20 77 61 73 20 70 61 73 73   parsed was pass
23f42 65 64 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71  ed.  ** to an sq
23f43 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
23f44 61 62 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68  ab() call. In th
23f45 61 74 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65  at case only the
23f46 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20   column names.  
23f47 2a 2a 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c  ** and types wil
23f48 6c 20 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68  l be used, so th
23f49 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74  ere is no need t
23f4a 6f 20 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73  o test for names
23f4b 70 61 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73  pace.  ** collis
23f4c 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
23f4d 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41   !IN_DECLARE_VTA
23f4e 42 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c  B ){.    if( SQL
23f4f 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
23f50 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
23f51 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
23f52 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23f53 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61  r;.    }.    pTa
23f54 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ble = sqlite3Fin
23f55 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
23f56 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
23f57 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
23f58 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69  Table ){.      i
23f59 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20  f( !noErr ){.   
23f5a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
23f5b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
23f5c 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20 65  ble %T already e
23f5d 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a  xists", pName);.
23f5e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f        }.      go
23f5f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  to begin_table_e
23f60 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
23f61 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49  if( sqlite3FindI
23f62 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ndex(db, zName, 
23f63 30 29 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 30  0)!=0 && (iDb==0
23f64 20 7c 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75   || !db->init.bu
23f65 73 79 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sy) ){.      sql
23f66 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23f67 72 73 65 2c 20 22 74 68 65 72 65 20 69 73 20 61  rse, "there is a
23f68 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20  lready an index 
23f69 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65  named %s", zName
23f6a 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  );.      goto be
23f6b 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
23f6c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54  .    }.  }..  pT
23f6d 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  able = sqlite3Db
23f6e 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
23f6f 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20  izeof(Table));. 
23f70 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29   if( pTable==0 )
23f71 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
23f72 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
23f73 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
23f74 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70  ITE_NOMEM;.    p
23f75 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
23f76 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
23f77 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ble_error;.  }. 
23f78 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d   pTable->zName =
23f79 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65   zName;.  pTable
23f7a 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20  ->iPKey = -1;.  
23f7b 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20  pTable->pSchema 
23f7c 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
23f7d 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65  Schema;.  pTable
23f7e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54  ->nRef = 1;.  pT
23f7f 61 62 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b  able->dbMem = 0;
23f80 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
23f81 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20  e->pNewTable==0 
23f82 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  );.  pParse->pNe
23f83 77 54 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b  wTable = pTable;
23f84 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
23f85 73 20 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69  s the magic sqli
23f86 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
23f87 65 20 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e  e used by autoin
23f88 63 72 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68  crement,.  ** th
23f89 65 6e 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e  en record a poin
23f8a 74 65 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c  ter to this tabl
23f8b 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  e in the main da
23f8c 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
23f8d 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e  .  ** so that IN
23f8e 53 45 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68  SERT can find th
23f8f 65 20 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a  e table easily..
23f90 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
23f91 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
23f92 52 45 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50  REMENT.  if( !pP
23f93 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20  arse->nested && 
23f94 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73  strcmp(zName, "s
23f95 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29  qlite_sequence")
23f96 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c  ==0 ){.    pTabl
23f97 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  e->pSchema->pSeq
23f98 54 61 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20  Tab = pTable;.  
23f99 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42  }.#endif..  /* B
23f9a 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
23f9b 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  the code that wi
23f9c 6c 6c 20 69 6e 73 65 72 74 20 74 68 65 20 74 61  ll insert the ta
23f9d 62 6c 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a  ble record into.
23f9e 20 20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f    ** the SQLITE_
23f9f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e  MASTER table.  N
23fa0 6f 74 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61  ote in particula
23fa1 72 20 74 68 61 74 20 77 65 20 6d 75 73 74 20 67  r that we must g
23fa2 6f 20 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64  o ahead.  ** and
23fa3 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65   allocate the re
23fa4 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20  cord number for 
23fa5 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20  the table entry 
23fa6 6e 6f 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79  now.  Before any
23fa7 0a 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45  .  ** PRIMARY KE
23fa8 59 20 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77  Y or UNIQUE keyw
23fa9 6f 72 64 73 20 61 72 65 20 70 61 72 73 65 64 2e  ords are parsed.
23faa 20 20 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73    Those keywords
23fab 20 77 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a   will cause.  **
23fac 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63   indices to be c
23fad 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 74  reated and the t
23fae 61 62 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74  able record must
23faf 20 63 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65   come before the
23fb0 20 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20   .  ** indices. 
23fb1 20 48 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f   Hence, the reco
23fb2 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
23fb3 65 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  e table must be 
23fb4 61 6c 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e  allocated.  ** n
23fb5 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
23fb6 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
23fb7 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   (v = sqlite3Get
23fb8 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d 30  Vdbe(pParse))!=0
23fb9 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a   ){.    int j1;.
23fba 20 20 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d      int fileForm
23fbb 61 74 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31  at;.    int reg1
23fbc 2c 20 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20  , reg2, reg3;.  
23fbd 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
23fbe 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
23fbf 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23  rse, 0, iDb);..#
23fc0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23fc1 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
23fc2 20 20 20 20 69 66 28 20 69 73 56 69 72 74 75 61      if( isVirtua
23fc3 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
23fc4 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
23fc5 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20  OP_VBegin);.    
23fc6 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
23fc7 20 49 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72   If the file for
23fc8 6d 61 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67  mat and encoding
23fc9 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
23fca 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73   have not been s
23fcb 65 74 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20  et, .    ** set 
23fcc 74 68 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f  them now..    */
23fcd 0a 20 20 20 20 72 65 67 31 20 3d 20 70 50 61 72  .    reg1 = pPar
23fce 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b  se->regRowid = +
23fcf 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
23fd0 20 20 20 72 65 67 32 20 3d 20 70 50 61 72 73 65     reg2 = pParse
23fd1 2d 3e 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50  ->regRoot = ++pP
23fd2 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
23fd3 72 65 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d  reg3 = ++pParse-
23fd4 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
23fd5 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23fd6 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
23fd7 44 62 2c 20 72 65 67 33 2c 20 42 54 52 45 45 5f  Db, reg3, BTREE_
23fd8 46 49 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20  FILE_FORMAT);.  
23fd9 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
23fda 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
23fdb 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
23fdc 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
23fdd 5f 49 66 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  _If, reg3);.    
23fde 66 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64 62  fileFormat = (db
23fdf 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
23fe0 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 29 21  _LegacyFileFmt)!
23fe1 3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20  =0 ?.           
23fe2 20 20 20 20 20 20 20 31 20 3a 20 53 51 4c 49 54         1 : SQLIT
23fe3 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41  E_MAX_FILE_FORMA
23fe4 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  T;.    sqlite3Vd
23fe5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
23fe6 6e 74 65 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d  nteger, fileForm
23fe7 61 74 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73  at, reg3);.    s
23fe8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
23fe9 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
23fea 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46 49 4c  , iDb, BTREE_FIL
23feb 45 5f 46 4f 52 4d 41 54 2c 20 72 65 67 33 29 3b  E_FORMAT, reg3);
23fec 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23fed 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
23fee 65 67 65 72 2c 20 45 4e 43 28 64 62 29 2c 20 72  eger, ENC(db), r
23fef 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eg3);.    sqlite
23ff0 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
23ff1 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62  P_SetCookie, iDb
23ff2 2c 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  , BTREE_TEXT_ENC
23ff3 4f 44 49 4e 47 2c 20 72 65 67 33 29 3b 0a 20 20  ODING, reg3);.  
23ff4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
23ff5 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20  pHere(v, j1);.. 
23ff6 20 20 20 2f 2a 20 54 68 69 73 20 6a 75 73 74 20     /* This just 
23ff7 63 72 65 61 74 65 73 20 61 20 70 6c 61 63 65 2d  creates a place-
23ff8 68 6f 6c 64 65 72 20 72 65 63 6f 72 64 20 69 6e  holder record in
23ff9 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
23ffa 65 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a  er table..    **
23ffb 20 54 68 65 20 72 65 63 6f 72 64 20 63 72 65 61   The record crea
23ffc 74 65 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  ted does not con
23ffd 74 61 69 6e 20 61 6e 79 74 68 69 6e 67 20 79 65  tain anything ye
23ffe 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 72  t.  It will be r
23fff 65 70 6c 61 63 65 64 0a 20 20 20 20 2a 2a 20 62  eplaced.    ** b
24000 79 20 74 68 65 20 72 65 61 6c 20 65 6e 74 72 79  y the real entry
24001 20 69 6e 20 63 6f 64 65 20 67 65 6e 65 72 61 74   in code generat
24002 65 64 20 61 74 20 73 71 6c 69 74 65 33 45 6e 64  ed at sqlite3End
24003 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a  Table()..    **.
24004 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64      ** The rowid
24005 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74   for the new ent
24006 72 79 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65  ry is left in re
24007 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72  gister pParse->r
24008 65 67 52 6f 77 69 64 2e 0a 20 20 20 20 2a 2a 20  egRowid..    ** 
24009 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  The root page nu
2400a 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20  mber of the new 
2400b 74 61 62 6c 65 20 69 73 20 6c 65 66 74 20 69 6e  table is left in
2400c 20 72 65 67 20 70 50 61 72 73 65 2d 3e 72 65 67   reg pParse->reg
2400d 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65  Root..    ** The
2400e 20 72 6f 77 69 64 20 61 6e 64 20 72 6f 6f 74 20   rowid and root 
2400f 70 61 67 65 20 6e 75 6d 62 65 72 20 76 61 6c 75  page number valu
24010 65 73 20 61 72 65 20 6e 65 65 64 65 64 20 62 79  es are needed by
24011 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 0a 20   the code that. 
24012 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64     ** sqlite3End
24013 54 61 62 6c 65 20 77 69 6c 6c 20 67 65 6e 65 72  Table will gener
24014 61 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20  ate..    */.#if 
24015 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
24016 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
24017 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
24018 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
24019 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20  .    if( isView 
2401a 7c 7c 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a  || isVirtual ){.
2401b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2401c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2401d 74 65 67 65 72 2c 20 30 2c 20 72 65 67 32 29 3b  teger, 0, reg2);
2401e 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
2401f 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71  f.    {.      sq
24020 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24021 76 2c 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  v, OP_CreateTabl
24022 65 2c 20 69 44 62 2c 20 72 65 67 32 29 3b 0a 20  e, iDb, reg2);. 
24023 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
24024 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
24025 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
24026 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24027 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
24028 69 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20 20  id, 0, reg1);.  
24029 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2402a 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2402b 30 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71  0, reg3);.    sq
2402c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2402d 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c  v, OP_Insert, 0,
2402e 20 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 20 20   reg3, reg1);.  
2402f 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
24030 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
24031 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c  APPEND);.    sql
24032 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
24033 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d  , OP_Close);.  }
24034 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e  ..  /* Normal (n
24035 6f 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e  on-error) return
24036 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a  . */.  return;..
24037 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
24038 20 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70   occurs, we jump
24039 20 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74   here */.begin_t
2403a 61 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71  able_error:.  sq
2403b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2403c 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e  zName);.  return
2403d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2403e 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 6f  macro is used to
2403f 20 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74 72   compare two str
24040 69 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69  ings in a case-i
24041 6e 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65  nsensitive manne
24042 72 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69 67  r..** It is slig
24043 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e  htly faster than
24044 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
24045 53 74 72 49 43 6d 70 28 29 20 64 69 72 65 63 74  StrICmp() direct
24046 6c 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75  ly, but.** produ
24047 63 65 73 20 6c 61 72 67 65 72 20 63 6f 64 65 2e  ces larger code.
24048 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20  .**.** WARNING: 
24049 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f  This macro is no
2404a 74 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74  t compatible wit
2404b 68 20 74 68 65 20 73 74 72 63 6d 70 28 29 20 66  h the strcmp() f
2404c 61 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74  amily. It.** ret
2404d 75 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65  urns true if the
2404e 20 74 77 6f 20 73 74 72 69 6e 67 73 20 61 72 65   two strings are
2404f 20 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 69 73   equal, otherwis
24050 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66  e false..*/.#def
24051 69 6e 65 20 53 54 52 49 43 4d 50 28 78 2c 20 79  ine STRICMP(x, y
24052 29 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 70 65  ) (\.sqlite3Uppe
24053 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67  rToLower[*(unsig
24054 6e 65 64 20 63 68 61 72 20 2a 29 28 78 29 5d 3d  ned char *)(x)]=
24055 3d 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 70 70  =   \.sqlite3Upp
24056 65 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69  erToLower[*(unsi
24057 67 6e 65 64 20 63 68 61 72 20 2a 29 28 79 29 5d  gned char *)(y)]
24058 20 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 74 65       \.&& sqlite
24059 33 53 74 72 49 43 6d 70 28 28 78 29 2b 31 2c 28  3StrICmp((x)+1,(
2405a 79 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a  y)+1)==0 )../*.*
2405b 2a 20 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75  * Add a new colu
2405c 6d 6e 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  mn to the table 
2405d 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20  currently being 
2405e 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a  constructed..**.
2405f 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
24060 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
24061 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63   once for each c
24062 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
24063 6e 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54 45  n.** in a CREATE
24064 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
24065 2e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54  .  sqlite3StartT
24066 61 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c 6c  able() gets call
24067 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20 67  ed.** first to g
24068 65 74 20 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e  et things going.
24069 20 20 54 68 65 6e 20 74 68 69 73 20 72 6f 75 74    Then this rout
2406a 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f  ine is called fo
2406b 72 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e  r each.** column
2406c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2406d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2406e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20  AddColumn(Parse 
2406f 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
24070 70 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20  pName){.  Table 
24071 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  *p;.  int i;.  c
24072 68 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e  har *z;.  Column
24073 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65   *pCol;.  sqlite
24074 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
24075 64 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70  db;.  if( (p = p
24076 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
24077 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23  )==0 ) return;.#
24078 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  if SQLITE_MAX_CO
24079 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43  LUMN.  if( p->nC
2407a 6f 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  ol+1>db->aLimit[
2407b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c  SQLITE_LIMIT_COL
2407c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  UMN] ){.    sqli
2407d 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2407e 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f  se, "too many co
2407f 6c 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d  lumns on %s", p-
24080 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
24081 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  urn;.  }.#endif.
24082 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d    z = sqlite3Nam
24083 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
24084 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d  Name);.  if( z==
24085 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  0 ) return;.  fo
24086 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c  r(i=0; i<p->nCol
24087 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
24088 53 54 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43  STRICMP(z, p->aC
24089 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a  ol[i].zName) ){.
2408a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2408b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64  orMsg(pParse, "d
2408c 75 70 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20  uplicate column 
2408d 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20  name: %s", z);. 
2408e 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2408f 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20  ee(db, z);.     
24090 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
24091 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f   }.  if( (p->nCo
24092 6c 20 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20  l & 0x7)==0 ){. 
24093 20 20 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b     Column *aNew;
24094 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69  .    aNew = sqli
24095 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
24096 70 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c  p->aCol,(p->nCol
24097 2b 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43  +8)*sizeof(p->aC
24098 6f 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28  ol[0]));.    if(
24099 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
2409a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2409b 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65  db, z);.      re
2409c 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2409d 70 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a  p->aCol = aNew;.
2409e 20 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d    }.  pCol = &p-
2409f 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a  >aCol[p->nCol];.
240a0 20 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30    memset(pCol, 0
240a1 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c  , sizeof(p->aCol
240a2 5b 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a  [0]));.  pCol->z
240a3 4e 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a  Name = z;. .  /*
240a4 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
240a5 74 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20  type specified, 
240a6 63 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 68 65  columns have the
240a7 20 64 65 66 61 75 6c 74 20 61 66 66 69 6e 69 74   default affinit
240a8 79 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49  y.  ** 'NONE'. I
240a9 66 20 74 68 65 72 65 20 69 73 20 61 20 74 79 70  f there is a typ
240aa 65 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  e specified, the
240ab 6e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  n sqlite3AddColu
240ac 6d 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a 20 20  mnType() will.  
240ad 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e 65 78  ** be called nex
240ae 74 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61  t to set pCol->a
240af 66 66 69 6e 69 74 79 20 63 6f 72 72 65 63 74 6c  ffinity correctl
240b0 79 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e  y..  */.  pCol->
240b1 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
240b2 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d  E_AFF_NONE;.  p-
240b3 3e 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  >nCol++;.}../*.*
240b4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
240b5 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
240b6 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20  parser while in 
240b7 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a  the middle of.**
240b8 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54   parsing a CREAT
240b9 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
240ba 74 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22  t.  A "NOT NULL"
240bb 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 0a   constraint has.
240bc 2a 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20  ** been seen on 
240bd 61 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20  a column.  This 
240be 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
240bf 20 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e   notNull flag on
240c0 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63  .** the column c
240c1 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
240c2 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  onstruction..*/.
240c3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
240c4 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f  oid sqlite3AddNo
240c5 74 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61  tNull(Parse *pPa
240c6 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72  rse, int onError
240c7 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
240c8 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65   p = pParse->pNe
240c9 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d  wTable;.  if( p=
240ca 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e  =0 || NEVER(p->n
240cb 43 6f 6c 3c 31 29 20 29 20 72 65 74 75 72 6e 3b  Col<1) ) return;
240cc 0a 20 20 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43  .  p->aCol[p->nC
240cd 6f 6c 2d 31 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20  ol-1].notNull = 
240ce 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a  (u8)onError;.}..
240cf 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20 63  /*.** Scan the c
240d0 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65 20  olumn type name 
240d1 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e 54  zType (length nT
240d2 79 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20  ype) and return 
240d3 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  the.** associate
240d4 64 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 2e  d affinity type.
240d5 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
240d6 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65 2d  ine does a case-
240d7 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61 72  independent sear
240d8 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72 20  ch of zType for 
240d9 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69 6e  the .** substrin
240da 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  gs in the follow
240db 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e  ing table. If on
240dc 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69  e of the substri
240dd 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c  ngs is.** found,
240de 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
240df 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73 20 72  ng affinity is r
240e0 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 70  eturned. If zTyp
240e1 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f  e contains.** mo
240e2 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74  re than one of t
240e3 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20 65  he substrings, e
240e4 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20 74 68  ntries toward th
240e5 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 65  e top of .** the
240e6 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69 6f   table take prio
240e7 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c  rity. For exampl
240e8 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20 27  e, if zType is '
240e9 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 51  BLOBINT', .** SQ
240ea 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
240eb 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
240ec 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20 20  .** Substring   
240ed 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a 20    | Affinity.** 
240ee 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
240ef 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
240f0 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20 20  .** 'INT'       
240f1 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49    | SQLITE_AFF_I
240f2 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52 27  NTEGER.** 'CHAR'
240f3 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
240f4 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43 4c  _AFF_TEXT.** 'CL
240f5 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  OB'        | SQL
240f6 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20  ITE_AFF_TEXT.** 
240f7 27 54 45 58 54 27 20 20 20 20 20 20 20 20 7c 20  'TEXT'        | 
240f8 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a  SQLITE_AFF_TEXT.
240f9 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20 20  ** 'BLOB'       
240fa 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   | SQLITE_AFF_NO
240fb 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20 20  NE.** 'REAL'    
240fc 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
240fd 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20  _REAL.** 'FLOA' 
240fe 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
240ff 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f 55  AFF_REAL.** 'DOU
24100 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  B'        | SQLI
24101 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a  TE_AFF_REAL.**.*
24102 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65  * If none of the
24103 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74   substrings in t
24104 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20 61  he above table a
24105 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c  re found,.** SQL
24106 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
24107 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
24108 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
24109 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e  har sqlite3Affin
2410a 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63 68  ityType(const ch
2410b 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 33 32 20  ar *zIn){.  u32 
2410c 68 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 66  h = 0;.  char af
2410d 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  f = SQLITE_AFF_N
2410e 55 4d 45 52 49 43 3b 0a 0a 20 20 69 66 28 20 7a  UMERIC;..  if( z
2410f 49 6e 20 29 20 77 68 69 6c 65 28 20 7a 49 6e 5b  In ) while( zIn[
24110 30 5d 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68  0] ){.    h = (h
24111 3c 3c 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70  <<8) + sqlite3Up
24112 70 65 72 54 6f 4c 6f 77 65 72 5b 28 2a 7a 49 6e  perToLower[(*zIn
24113 29 26 30 78 66 66 5d 3b 0a 20 20 20 20 7a 49 6e  )&0xff];.    zIn
24114 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28  ++;.    if( h==(
24115 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c  ('c'<<24)+('h'<<
24116 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27  16)+('a'<<8)+'r'
24117 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  ) ){            
24118 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20   /* CHAR */.    
24119 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
2411a 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65  FF_TEXT; .    }e
2411b 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27  lse if( h==(('c'
2411c 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b  <<24)+('l'<<16)+
2411d 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b  ('o'<<8)+'b') ){
2411e 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a         /* CLOB *
2411f 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  /.      aff = SQ
24120 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20  LITE_AFF_TEXT;. 
24121 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
24122 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c  (('t'<<24)+('e'<
24123 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74  <16)+('x'<<8)+'t
24124 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54  ') ){       /* T
24125 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66  EXT */.      aff
24126 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   = SQLITE_AFF_TE
24127 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  XT;.    }else if
24128 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b  ( h==(('b'<<24)+
24129 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c  ('l'<<16)+('o'<<
2412a 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20  8)+'b')         
2412b 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20   /* BLOB */.    
2412c 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c      && (aff==SQL
2412d 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
2412e 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  || aff==SQLITE_A
2412f 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20  FF_REAL) ){.    
24130 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
24131 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66  FF_NONE;.#ifndef
24132 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
24133 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
24134 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
24135 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36  r'<<24)+('e'<<16
24136 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20  )+('a'<<8)+'l') 
24137 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c           /* REAL
24138 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61   */.        && a
24139 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
2413a 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  UMERIC ){.      
2413b 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
2413c 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65  _REAL;.    }else
2413d 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32   if( h==(('f'<<2
2413e 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f  4)+('l'<<16)+('o
2413f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20  '<<8)+'a')      
24140 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20      /* FLOA */. 
24141 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53         && aff==S
24142 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
24143 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d  C ){.      aff =
24144 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
24145 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
24146 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27  h==(('d'<<24)+('
24147 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29  o'<<16)+('u'<<8)
24148 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f  +'b')          /
24149 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20  * DOUB */.      
2414a 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45    && aff==SQLITE
2414b 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a  _AFF_NUMERIC ){.
2414c 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
2414d 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e  TE_AFF_REAL;.#en
2414e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  dif.    }else if
2414f 28 20 28 68 26 30 78 30 30 46 46 46 46 46 46 29  ( (h&0x00FFFFFF)
24150 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e  ==(('i'<<16)+('n
24151 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20  '<<8)+'t') ){   
24152 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20   /* INT */.     
24153 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
24154 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
24155 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
24156 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b  }..  return aff;
24157 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
24158 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
24159 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77   by the parser w
2415a 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64  hile in the midd
2415b 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67  le of.** parsing
2415c 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
2415d 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
2415e 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20  pFirst token is 
2415f 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b  the first.** tok
24160 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e  en in the sequen
24161 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61  ce of tokens tha
24162 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74  t describe the t
24163 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ype of the.** co
24164 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75  lumn currently u
24165 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
24166 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 74 68  n.   pLast is th
24167 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20  e last token.** 
24168 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e  in the sequence.
24169 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72    Use this infor
2416a 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72  mation to constr
2416b 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  uct a string.** 
2416c 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
2416d 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68  e typename of th
2416e 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f  e column and sto
2416f 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a  re that string.*
24170 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a  * in zType..*/ .
24171 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24172 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f  oid sqlite3AddCo
24173 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a  lumnType(Parse *
24174 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
24175 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Type){.  Table *
24176 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  p;.  Column *pCo
24177 6c 3b 0a 0a 20 20 70 20 3d 20 70 50 61 72 73 65  l;..  p = pParse
24178 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
24179 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  f( p==0 || NEVER
2417a 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65  (p->nCol<1) ) re
2417b 74 75 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26  turn;.  pCol = &
2417c 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d  p->aCol[p->nCol-
2417d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  1];.  assert( pC
2417e 6f 6c 2d 3e 7a 54 79 70 65 3d 3d 30 20 29 3b 0a  ol->zType==0 );.
2417f 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20    pCol->zType = 
24180 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
24181 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  oken(pParse->db,
24182 20 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d   pType);.  pCol-
24183 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69  >affinity = sqli
24184 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28  te3AffinityType(
24185 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 7d 0a  pCol->zType);.}.
24186 0a 2f 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65  ./*.** The expre
24187 73 73 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66  ssion is the def
24188 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
24189 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
2418a 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a   added column.**
2418b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 75   of the table cu
2418c 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
2418d 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
2418e 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * Default value 
2418f 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74  expressions must
24190 20 62 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52   be constant.  R
24191 61 69 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f  aise an exceptio
24192 6e 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20  n if this.** is 
24193 6e 6f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a  not the case..**
24194 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
24195 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
24196 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
24197 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
24198 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
24199 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
2419a 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
2419b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2419c 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
2419d 75 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ue(Parse *pParse
2419e 2c 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61  , ExprSpan *pSpa
2419f 6e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  n){.  Table *p;.
241a0 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
241a1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
241a2 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20  pParse->db;.  p 
241a3 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
241a4 62 6c 65 3b 0a 20 20 69 66 28 20 70 21 3d 30 20  ble;.  if( p!=0 
241a5 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28  ){.    pCol = &(
241a6 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d  p->aCol[p->nCol-
241a7 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71  1]);.    if( !sq
241a8 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
241a9 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 53  antOrFunction(pS
241aa 70 61 6e 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20  pan->pExpr) ){. 
241ab 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
241ac 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 65  rMsg(pParse, "de
241ad 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 63  fault value of c
241ae 6f 6c 75 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f  olumn [%s] is no
241af 74 20 63 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20  t constant",.   
241b0 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61         pCol->zNa
241b1 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  me);.    }else{.
241b2 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20        /* A copy 
241b3 6f 66 20 70 45 78 70 72 20 69 73 20 75 73 65 64  of pExpr is used
241b4 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
241b5 6f 72 69 67 69 6e 61 6c 2c 20 61 73 20 70 45 78  original, as pEx
241b6 70 72 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20  pr contains.    
241b7 20 20 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74    ** tokens that
241b8 20 70 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69   point to volati
241b9 6c 65 20 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27  le memory. The '
241ba 73 70 61 6e 27 20 6f 66 20 74 68 65 20 65 78 70  span' of the exp
241bb 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ression.      **
241bc 20 69 73 20 72 65 71 75 69 72 65 64 20 62 79 20   is required by 
241bd 70 72 61 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66  pragma table_inf
241be 6f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  o..      */.    
241bf 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
241c0 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44  ete(db, pCol->pD
241c1 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  flt);.      pCol
241c2 2d 3e 70 44 66 6c 74 20 3d 20 73 71 6c 69 74 65  ->pDflt = sqlite
241c3 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 70  3ExprDup(db, pSp
241c4 61 6e 2d 3e 70 45 78 70 72 2c 20 45 58 50 52 44  an->pExpr, EXPRD
241c5 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
241c6 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
241c7 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29  db, pCol->zDflt)
241c8 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 44  ;.      pCol->zD
241c9 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 44 62 53  flt = sqlite3DbS
241ca 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
241cb 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c  *)pSpan->zStart,
241cc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
241cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
241ce 20 20 20 20 20 20 28 69 6e 74 29 28 70 53 70 61        (int)(pSpa
241cf 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d  n->zEnd - pSpan-
241d0 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 20 20 7d  >zStart));.    }
241d1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
241d2 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 53 70  prDelete(db, pSp
241d3 61 6e 2d 3e 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f  an->pExpr);.}../
241d4 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74  *.** Designate t
241d5 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66  he PRIMARY KEY f
241d6 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70  or the table.  p
241d7 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f  List is a list o
241d8 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63  f names .** of c
241d9 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d  olumns that form
241da 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
241db 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e  .  If pList is N
241dc 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
241dd 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
241de 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  dded column of t
241df 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
241e0 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a  primary key..**.
241e1 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68  ** A table can h
241e2 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  ave at most one 
241e3 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66  primary key.  If
241e4 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61   the table alrea
241e5 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d  dy has.** a prim
241e6 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69  ary key (and thi
241e7 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  s is the second 
241e8 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65  primary key) the
241e9 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65  n create an.** e
241ea 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rror..**.** If t
241eb 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69  he PRIMARY KEY i
241ec 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f  s on a single co
241ed 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74  lumn whose datat
241ee 79 70 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a  ype is INTEGER,.
241ef 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  ** then we will 
241f0 74 72 79 20 74 6f 20 75 73 65 20 74 68 61 74 20  try to use that 
241f1 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f  column as the ro
241f2 77 69 64 2e 20 20 53 65 74 20 74 68 65 20 54 61  wid.  Set the Ta
241f3 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65  ble.iPKey.** fie
241f4 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ld of the table 
241f5 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
241f6 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64  on to be the ind
241f7 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54  ex of the.** INT
241f8 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
241f9 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e   column.  Table.
241fa 69 50 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20  iPKey is set to 
241fb 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a  -1 if there is.*
241fc 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49  * no INTEGER PRI
241fd 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20  MARY KEY..**.** 
241fe 49 66 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f  If the key is no
241ff 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  t an INTEGER PRI
24200 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63  MARY KEY, then c
24201 72 65 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a  reate a unique.*
24202 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20  * index for the 
24203 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69  key.  No index i
24204 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e  s created for IN
24205 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
24206 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Ys..*/.SQLITE_PR
24207 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
24208 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28  e3AddPrimaryKey(
24209 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2420a 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
2420b 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2420c 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f  rList *pList,  /
2420d 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20  * List of field 
2420e 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65  names to be inde
2420f 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45  xed */.  int onE
24210 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68  rror,      /* Wh
24211 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20  at to do with a 
24212 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c  uniqueness confl
24213 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74  ict */.  int aut
24214 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72  oInc,      /* Tr
24215 75 65 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e  ue if the AUTOIN
24216 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20  CREMENT keyword 
24217 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
24218 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20  int sortOrder   
24219 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41    /* SQLITE_SO_A
2421a 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f  SC or SQLITE_SO_
2421b 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  DESC */.){.  Tab
2421c 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73  le *pTab = pPars
2421d 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
2421e 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b  char *zType = 0;
2421f 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31  .  int iCol = -1
24220 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  , i;.  if( pTab=
24221 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45  =0 || IN_DECLARE
24222 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69  _VTAB ) goto pri
24223 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20  mary_key_exit;. 
24224 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c   if( pTab->tabFl
24225 61 67 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d  ags & TF_HasPrim
24226 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  aryKey ){.    sq
24227 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24228 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61  arse, .      "ta
24229 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d  ble \"%s\" has m
2422a 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69  ore than one pri
2422b 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d  mary key", pTab-
2422c 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
2422d 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  o primary_key_ex
2422e 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e  it;.  }.  pTab->
2422f 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48  tabFlags |= TF_H
24230 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20  asPrimaryKey;.  
24231 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
24232 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d      iCol = pTab-
24233 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70  >nCol - 1;.    p
24234 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
24235 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20  isPrimKey = 1;. 
24236 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28   }else{.    for(
24237 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
24238 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
24239 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
2423a 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
2423b 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ol++){.        i
2423c 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2423d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e  p(pList->a[i].zN
2423e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ame, pTab->aCol[
2423f 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20  iCol].zName)==0 
24240 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ){.          bre
24241 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
24242 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
24243 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  iCol<pTab->nCol 
24244 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ){.        pTab-
24245 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72  >aCol[iCol].isPr
24246 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20  imKey = 1;.     
24247 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
24248 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20   pList->nExpr>1 
24249 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d  ) iCol = -1;.  }
2424a 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26  .  if( iCol>=0 &
2424b 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  & iCol<pTab->nCo
2424c 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  l ){.    zType =
2424d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
2424e 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69  ].zType;.  }.  i
2424f 66 28 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69  f( zType && sqli
24250 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65  te3StrICmp(zType
24251 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a  , "INTEGER")==0.
24252 20 20 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f          && sortO
24253 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f  rder==SQLITE_SO_
24254 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d  ASC ){.    pTab-
24255 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20  >iPKey = iCol;. 
24256 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66     pTab->keyConf
24257 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a   = (u8)onError;.
24258 20 20 20 20 61 73 73 65 72 74 28 20 61 75 74 6f      assert( auto
24259 49 6e 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e  Inc==0 || autoIn
2425a 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62  c==1 );.    pTab
2425b 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75  ->tabFlags |= au
2425c 74 6f 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63  toInc*TF_Autoinc
2425d 72 65 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20  rement;.  }else 
2425e 69 66 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23  if( autoInc ){.#
2425f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24260 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
24261 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
24262 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 41 55  rMsg(pParse, "AU
24263 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f  TOINCREMENT is o
24264 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61  nly allowed on a
24265 6e 20 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45  n ".       "INTE
24266 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22  GER PRIMARY KEY"
24267 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73  );.#endif.  }els
24268 65 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b  e{.    Index *p;
24269 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
2426a 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
2426b 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69  se, 0, 0, 0, pLi
2426c 73 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20  st, onError, 0, 
2426d 30 2c 20 73 6f 72 74 4f 72 64 65 72 2c 20 30 29  0, sortOrder, 0)
2426e 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20  ;.    if( p ){. 
2426f 20 20 20 20 20 70 2d 3e 61 75 74 6f 49 6e 64 65       p->autoInde
24270 78 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20  x = 2;.    }.   
24271 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d 0a   pList = 0;.  }.
24272 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69  .primary_key_exi
24273 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  t:.  sqlite3Expr
24274 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
24275 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  e->db, pList);. 
24276 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
24277 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45 43  * Add a new CHEC
24278 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20  K constraint to 
24279 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
2427a 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
2427b 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  uction..*/.SQLIT
2427c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2427d 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f  qlite3AddCheckCo
2427e 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 73  nstraint(.  Pars
2427f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
24280 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
24281 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68 65   */.  Expr *pChe
24282 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 63  ckExpr  /* The c
24283 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e 20  heck expression 
24284 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
24285 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24286 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
24287 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 61  _OMIT_CHECK.  Ta
24288 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72  ble *pTab = pPar
24289 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
2428a 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49 4e   if( pTab && !IN
2428b 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b  _DECLARE_VTAB ){
2428c 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68 65 63  .    pTab->pChec
2428d 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  k = sqlite3ExprA
2428e 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70 43 68  nd(db, pTab->pCh
2428f 65 63 6b 2c 20 70 43 68 65 63 6b 45 78 70 72 29  eck, pCheckExpr)
24290 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  ;.  }else.#endif
24291 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  .  {.    sqlite3
24292 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
24293 43 68 65 63 6b 45 78 70 72 29 3b 0a 20 20 7d 0a  CheckExpr);.  }.
24294 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
24295 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
24296 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ion of the most 
24297 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20  recently parsed 
24298 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  table column.** 
24299 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67  to the CollSeq g
2429a 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  iven..*/.SQLITE_
2429b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2429c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
2429d 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
2429e 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29  , Token *pToken)
2429f 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
242a0 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a  int i;.  char *z
242a1 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Coll;           
242a2 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e     /* Dequoted n
242a3 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e  ame of collation
242a4 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73   sequence */.  s
242a5 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69  qlite3 *db;..  i
242a6 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  f( (p = pParse->
242a7 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20  pNewTable)==0 ) 
242a8 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d  return;.  i = p-
242a9 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20  >nCol-1;.  db = 
242aa 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43  pParse->db;.  zC
242ab 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  oll = sqlite3Nam
242ac 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
242ad 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a  Token);.  if( !z
242ae 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Coll ) return;..
242af 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63    if( sqlite3Loc
242b0 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
242b1 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20  e, zColl) ){.   
242b2 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
242b3 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f    p->aCol[i].zCo
242b4 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a 20  ll = zColl;.  . 
242b5 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
242b6 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64 20  umn is declared 
242b7 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41  as "<name> PRIMA
242b8 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c  RY KEY COLLATE <
242b9 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74  type>",.    ** t
242ba 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 79  hen an index may
242bb 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61 74   have been creat
242bc 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d  ed on this colum
242bd 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20  n before the.   
242be 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79   ** collation ty
242bf 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43 6f  pe was added. Co
242c0 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69 74  rrect this if it
242c1 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20   is the case..  
242c2 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 64    */.    for(pId
242c3 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  x=p->pIndex; pId
242c4 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
242c5 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ext){.      asse
242c6 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  rt( pIdx->nColum
242c7 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n==1 );.      if
242c8 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
242c9 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20  [0]==i ){.      
242ca 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30    pIdx->azColl[0
242cb 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  ] = p->aCol[i].z
242cc 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
242cd 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
242ce 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
242cf 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 7d  b, zColl);.  }.}
242d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
242d1 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
242d2 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
242d3 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61 73  ence for databas
242d4 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a 2a  e native text.**
242d5 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74 69   encoding identi
242d6 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72 69  fied by the stri
242d7 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68  ng zName, length
242d8 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   nName..**.** If
242d9 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
242da 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
242db 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
242dc 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69 6c  le, or not avail
242dd 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64  able.** in the d
242de 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65  atabase native e
242df 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f 6c  ncoding, the col
242e0 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 69  lation factory i
242e1 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
242e2 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20 74  request it. If t
242e3 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63  he collation fac
242e4 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73 75  tory does not su
242e5 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71 75  pply such a sequ
242e6 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68 65  ence,.** and the
242e7 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76 61   sequence is ava
242e8 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65  ilable in anothe
242e9 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2c  r text encoding,
242ea 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a 2a   then that is.**
242eb 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61   returned instea
242ec 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 76  d..**.** If no v
242ed 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 72  ersions of the r
242ee 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69  equested collati
242ef 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72 65  ons sequence are
242f0 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a 2a   available, or.*
242f1 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72 20  * another error 
242f2 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20  occurs, NULL is 
242f3 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20  returned and an 
242f4 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  error message wr
242f5 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70 50  itten into.** pP
242f6 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  arse..**.** This
242f7 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77 72   routine is a wr
242f8 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c  apper around sql
242f9 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
242fa 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ).  This routine
242fb 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  .** invokes the 
242fc 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
242fd 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 63  y if the named c
242fe 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  ollation cannot 
242ff 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64 20  be found.** and 
24300 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
24301 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a  or message..**.*
24302 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69  * See also: sqli
24303 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29  te3FindCollSeq()
24304 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c  , sqlite3GetColl
24305 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Seq().*/.SQLITE_
24306 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
24307 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f  *sqlite3LocateCo
24308 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
24309 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
2430a 2a 7a 4e 61 6d 65 29 7b 0a 20 20 73 71 6c 69 74  *zName){.  sqlit
2430b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2430c 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 3d 20  >db;.  u8 enc = 
2430d 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 69 6e  ENC(db);.  u8 in
2430e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 6e 69  itbusy = db->ini
2430f 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c 53 65  t.busy;.  CollSe
24310 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43 6f  q *pColl;..  pCo
24311 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ll = sqlite3Find
24312 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c  CollSeq(db, enc,
24313 20 7a 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73 79   zName, initbusy
24314 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62 75  );.  if( !initbu
24315 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c  sy && (!pColl ||
24316 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29   !pColl->xCmp) )
24317 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
24318 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28  lite3GetCollSeq(
24319 64 62 2c 20 65 6e 63 2c 20 70 43 6f 6c 6c 2c 20  db, enc, pColl, 
2431a 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
2431b 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
2431c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2431d 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2431e 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2431f 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29  nce: %s", zName)
24320 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
24321 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
24322 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
24323 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69  code that will i
24324 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68  ncrement the sch
24325 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a  ema cookie..**.*
24326 2a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  * The schema coo
24327 6b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  kie is used to d
24328 65 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74 68  etermine when th
24329 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65  e schema for the
2432a 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68 61  .** database cha
2432b 6e 67 65 73 2e 20 20 41 66 74 65 72 20 65 61 63  nges.  After eac
2432c 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2c  h schema change,
2432d 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75   the cookie valu
2432e 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 57  e.** changes.  W
2432f 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66 69  hen a process fi
24330 72 73 74 20 72 65 61 64 73 20 74 68 65 20 73 63  rst reads the sc
24331 68 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73 20  hema it records 
24332 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20  the.** cookie.  
24333 54 68 65 72 65 61 66 74 65 72 2c 20 77 68 65 6e  Thereafter, when
24334 65 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f 20  ever it goes to 
24335 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62  access the datab
24336 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b  ase,.** it check
24337 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20  s the cookie to 
24338 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 63  make sure the sc
24339 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68 61  hema has not cha
2433a 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74  nged.** since it
2433b 20 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e 0a   was last read..
2433c 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20  **.** This plan 
2433d 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c  is not completel
2433e 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20  y bullet-proof. 
2433f 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   It is possible 
24340 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d  for.** the schem
24341 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74  a to change mult
24342 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20 66  iple times and f
24343 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f  or the cookie to
24344 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20   be.** set back 
24345 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e 20  to prior value. 
24346 20 42 75 74 20 73 63 68 65 6d 61 20 63 68 61 6e   But schema chan
24347 67 65 73 20 61 72 65 20 69 6e 66 72 65 71 75 65  ges are infreque
24348 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72  nt.** and the pr
24349 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69 74  obability of hit
2434a 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 6f  ting the same co
2434b 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f 6e  okie value is on
2434c 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20 69  ly.** 1 chance i
2434d 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27 72  n 2^32.  So we'r
2434e 65 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a  e safe enough..*
2434f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24350 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61   void sqlite3Cha
24351 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 20  ngeCookie(Parse 
24352 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
24353 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73 71  ){.  int r1 = sq
24354 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
24355 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
24356 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
24357 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  >db;.  Vdbe *v =
24358 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
24359 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2435a 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2435b 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  r, db->aDb[iDb].
2435c 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f  pSchema->schema_
2435d 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20  cookie+1, r1);. 
2435e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2435f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
24360 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 53  ie, iDb, BTREE_S
24361 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 72  CHEMA_VERSION, r
24362 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
24363 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
24364 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  se, r1);.}../*.*
24365 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e 75  * Measure the nu
24366 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
24367 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75 74  rs needed to out
24368 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  put the given.**
24369 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 54 68   identifier.  Th
2436a 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65  e number returne
2436b 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 20 71  d includes any q
2436c 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 62 75  uotes used.** bu
2436d 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  t does not inclu
2436e 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d  de the null term
2436f 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
24370 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 63 6f  e estimate is co
24371 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 74 20  nservative.  It 
24372 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20  might be larger 
24373 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a 2a 20  that what is.** 
24374 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a  really needed..*
24375 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 64 65  /.static int ide
24376 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 20 63  ntLength(const c
24377 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e  har *z){.  int n
24378 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b  ;.  for(n=0; *z;
24379 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20   n++, z++){.    
2437a 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e  if( *z=='"' ){ n
2437b 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75  ++; }.  }.  retu
2437c 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a  rn n + 2;.}../*.
2437d 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
2437e 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e  ameter is a poin
2437f 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 75 74  ter to an output
24380 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 65 63   buffer. The sec
24381 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74 65  ond .** paramete
24382 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
24383 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  o an integer tha
24384 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6f  t contains the o
24385 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68 69 63  ffset at.** whic
24386 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20  h to write into 
24387 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
24388 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
24389 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a 20 6e   copies the.** n
2438a 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ul-terminated st
2438b 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20  ring pointed to 
2438c 62 79 20 74 68 65 20 74 68 69 72 64 20 70 61 72  by the third par
2438d 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65 64 49  ameter, zSignedI
2438e 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68 65 20  dent,.** to the 
2438f 73 70 65 63 69 66 69 65 64 20 6f 66 66 73 65 74  specified offset
24390 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 61   in the buffer a
24391 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49 64 78  nd updates *pIdx
24392 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74 6f 20   to refer.** to 
24393 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61  the first byte a
24394 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 62 79  fter the last by
24395 74 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f 72  te written befor
24396 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 20  e returning..** 
24397 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e  .** If the strin
24398 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 20 63  g zSignedIdent c
24399 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c 79  onsists entirely
2439a 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65 72 69   of alpha-numeri
2439b 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2c  c.** characters,
2439c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20   does not begin 
2439d 77 69 74 68 20 61 20 64 69 67 69 74 20 61 6e 64  with a digit and
2439e 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20 6b   is not an SQL k
2439f 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e 20  eyword,.** then 
243a0 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20  it is copied to 
243a1 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
243a2 72 20 65 78 61 63 74 6c 79 20 61 73 20 69 74 20  r exactly as it 
243a3 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a  is. Otherwise,.*
243a4 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64 20 75  * it is quoted u
243a5 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75 6f 74  sing double-quot
243a6 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
243a7 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61 72  id identPut(char
243a8 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20   *z, int *pIdx, 
243a9 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64 65  char *zSignedIde
243aa 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  nt){.  unsigned 
243ab 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28  char *zIdent = (
243ac 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a  unsigned char*)z
243ad 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20 69  SignedIdent;.  i
243ae 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f  nt i, j, needQuo
243af 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b  te;.  i = *pIdx;
243b0 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64  ..  for(j=0; zId
243b1 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20  ent[j]; j++){.  
243b2 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
243b3 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d 29  alnum(zIdent[j])
243b4 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d 27   && zIdent[j]!='
243b5 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  _' ) break;.  }.
243b6 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 73 71    needQuote = sq
243b7 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 49 64  lite3Isdigit(zId
243b8 65 6e 74 5b 30 5d 29 20 7c 7c 20 73 71 6c 69 74  ent[0]) || sqlit
243b9 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a 49  e3KeywordCode(zI
243ba 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b  dent, j)!=TK_ID;
243bb 0a 20 20 69 66 28 20 21 6e 65 65 64 51 75 6f 74  .  if( !needQuot
243bc 65 20 29 7b 0a 20 20 20 20 6e 65 65 64 51 75 6f  e ){.    needQuo
243bd 74 65 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a  te = zIdent[j];.
243be 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65 64 51    }..  if( needQ
243bf 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20  uote ) z[i++] = 
243c0 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  '"';.  for(j=0; 
243c1 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b  zIdent[j]; j++){
243c2 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49  .    z[i++] = zI
243c3 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28  dent[j];.    if(
243c4 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20   zIdent[j]=='"' 
243c5 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a  ) z[i++] = '"';.
243c6 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51 75    }.  if( needQu
243c7 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27  ote ) z[i++] = '
243c8 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a  "';.  z[i] = 0;.
243c9 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a    *pIdx = i;.}..
243ca 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
243cb 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
243cc 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72 69  atement appropri
243cd 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76 65  ate for the give
243ce 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d  n.** table.  Mem
243cf 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
243d0 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61 74  text of the stat
243d1 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e 65  ement is obtaine
243d2 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65  d.** from sqlite
243d3 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73  Malloc() and mus
243d4 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68  t be freed by th
243d5 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
243d6 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  on..*/.static ch
243d7 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65 53  ar *createTableS
243d8 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  tmt(sqlite3 *db,
243d9 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e   Table *p){.  in
243da 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61  t i, k, n;.  cha
243db 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72  r *zStmt;.  char
243dc 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20   *zSep, *zSep2, 
243dd 2a 7a 45 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e 20  *zEnd;.  Column 
243de 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a  *pCol;.  n = 0;.
243df 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e    for(pCol = p->
243e0 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e  aCol, i=0; i<p->
243e1 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
243e2 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65  +){.    n += ide
243e3 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a  ntLength(pCol->z
243e4 4e 61 6d 65 29 20 2b 20 35 3b 0a 20 20 7d 0a 20  Name) + 5;.  }. 
243e5 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74   n += identLengt
243e6 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  h(p->zName);.  i
243e7 66 28 20 6e 3c 35 30 20 29 7b 20 0a 20 20 20 20  f( n<50 ){ .    
243e8 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a  zSep = "";.    z
243e9 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20  Sep2 = ",";.    
243ea 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65  zEnd = ")";.  }e
243eb 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20  lse{.    zSep = 
243ec 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70  "\n  ";.    zSep
243ed 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20  2 = ",\n  ";.   
243ee 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20   zEnd = "\n)";. 
243ef 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36   }.  n += 35 + 6
243f0 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d  *p->nCol;.  zStm
243f1 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
243f2 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53  c( n );.  if( zS
243f3 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  tmt==0 ){.    db
243f4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
243f5 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
243f6 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
243f7 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d  snprintf(n, zStm
243f8 74 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c 45  t, "CREATE TABLE
243f9 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69 74   ");.  k = sqlit
243fa 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d 74  e3Strlen30(zStmt
243fb 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a 53  );.  identPut(zS
243fc 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d  tmt, &k, p->zNam
243fd 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d  e);.  zStmt[k++]
243fe 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70 43   = '(';.  for(pC
243ff 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b  ol=p->aCol, i=0;
24400 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c   i<p->nCol; i++,
24401 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 74   pCol++){.    st
24402 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
24403 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65 5b 5d  * const azType[]
24404 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20   = {.        /* 
24405 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
24406 20 20 20 2a 2f 20 22 20 54 45 58 54 22 2c 0a 20     */ " TEXT",. 
24407 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45         /* SQLITE
24408 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 2a 2f 20  _AFF_NONE    */ 
24409 22 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53  "",.        /* S
2440a 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
2440b 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20 20 20  C */ " NUM",.   
2440c 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41       /* SQLITE_A
2440d 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22 20  FF_INTEGER */ " 
2440e 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a  INT",.        /*
2440f 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
24410 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22 0a 20      */ " REAL". 
24411 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6c 65     };.    int le
24412 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  n;.    const cha
24413 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20 20 73  r *zType;..    s
24414 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
24415 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20  n-k, &zStmt[k], 
24416 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  zSep);.    k += 
24417 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
24418 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20 20  &zStmt[k]);.    
24419 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20 20  zSep = zSep2;.  
2441a 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d 74    identPut(zStmt
2441b 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  , &k, pCol->zNam
2441c 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  e);.    assert( 
2441d 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53  pCol->affinity-S
2441e 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 3e  QLITE_AFF_TEXT >
2441f 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  = 0 );.    asser
24420 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  t( pCol->affinit
24421 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  y-SQLITE_AFF_TEX
24422 54 20 3c 20 73 69 7a 65 6f 66 28 61 7a 54 79 70  T < sizeof(azTyp
24423 65 29 2f 73 69 7a 65 6f 66 28 61 7a 54 79 70 65  e)/sizeof(azType
24424 5b 30 5d 29 20 29 3b 0a 20 20 20 20 74 65 73 74  [0]) );.    test
24425 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69  case( pCol->affi
24426 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
24427 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 74 65 73  _TEXT );.    tes
24428 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66  tcase( pCol->aff
24429 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46  inity==SQLITE_AF
2442a 46 5f 4e 4f 4e 45 20 29 3b 0a 20 20 20 20 74 65  F_NONE );.    te
2442b 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
2442c 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
2442d 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20  FF_NUMERIC );.  
2442e 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c    testcase( pCol
2442f 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  ->affinity==SQLI
24430 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29  TE_AFF_INTEGER )
24431 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
24432 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
24433 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
24434 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54 79 70  );.    .    zTyp
24435 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f 6c 2d  e = azType[pCol-
24436 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51 4c 49  >affinity - SQLI
24437 54 45 5f 41 46 46 5f 54 45 58 54 5d 3b 0a 20 20  TE_AFF_TEXT];.  
24438 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53    len = sqlite3S
24439 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b 0a  trlen30(zType);.
2443a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c      assert( pCol
2443b 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  ->affinity==SQLI
2443c 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 0a 20 20 20  TE_AFF_NONE .   
2443d 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 6f 6c           || pCol
2443e 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71 6c 69  ->affinity==sqli
2443f 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28  te3AffinityType(
24440 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20 6d 65  zType) );.    me
24441 6d 63 70 79 28 26 7a 53 74 6d 74 5b 6b 5d 2c 20  mcpy(&zStmt[k], 
24442 7a 54 79 70 65 2c 20 6c 65 6e 29 3b 0a 20 20 20  zType, len);.   
24443 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 61   k += len;.    a
24444 73 73 65 72 74 28 20 6b 3c 3d 6e 20 29 3b 0a 20  ssert( k<=n );. 
24445 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70   }.  sqlite3_snp
24446 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d  rintf(n-k, &zStm
24447 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64  t[k], "%s", zEnd
24448 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d  );.  return zStm
24449 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
2444a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2444b 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65  ed to report the
2444c 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20   final ")" that 
2444d 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20  terminates.** a 
2444e 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
2444f 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  tement..**.** Th
24450 65 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72  e table structur
24451 65 20 74 68 61 74 20 6f 74 68 65 72 20 61 63 74  e that other act
24452 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76  ion routines hav
24453 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a  e been building.
24454 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74  ** is added to t
24455 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  he internal hash
24456 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e   tables, assumin
24457 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65  g no errors have
24458 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a  .** occurred..**
24459 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72  .** An entry for
2445a 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61   the table is ma
2445b 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72  de in the master
2445c 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20   table on disk, 
2445d 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69  unless.** this i
2445e 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  s a temporary ta
2445f 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e  ble or db->init.
24460 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64  busy==1.  When d
24461 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a  b->init.busy==1.
24462 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61  ** it means we a
24463 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 73  re reading the s
24464 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
24465 6c 65 20 62 65 63 61 75 73 65 20 77 65 20 6a 75  le because we ju
24466 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20  st.** connected 
24467 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
24468 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 73  or because the s
24469 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
2446a 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74  le has.** recent
2446b 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74  ly changed, so t
2446c 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69  he entry for thi
2446d 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  s table already 
2446e 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65  exists in.** the
2446f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
24470 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74  able.  We do not
24471 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20   want to create 
24472 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20  it again..**.** 
24473 49 66 20 74 68 65 20 70 53 65 6c 65 63 74 20 61  If the pSelect a
24474 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e  rgument is not N
24475 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ULL, it means th
24476 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  at this routine.
24477 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f  ** was called to
24478 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20   create a table 
24479 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61  generated from a
2447a 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42   .** "CREATE TAB
2447b 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54  LE ... AS SELECT
2447c 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e   ..." statement.
2447d 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d    The column nam
2447e 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77  es of.** the new
2447f 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63   table will matc
24480 68 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  h the result set
24481 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a   of the SELECT..
24482 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24483 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e  E void sqlite3En
24484 64 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  dTable(.  Parse 
24485 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
24486 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
24487 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
24488 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20  Cons,           
24489 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e  /* The ',' token
2448a 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20   after the last 
2448b 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a  column defn. */.
2448c 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20    Token *pEnd,  
2448d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2448e 20 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e   final ')' token
2448f 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
24490 41 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ABLE */.  Select
24491 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20   *pSelect       
24492 20 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d    /* Select from
24493 20 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41   a "CREATE ... A
24494 53 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a  S SELECT" */.){.
24495 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71    Table *p;.  sq
24496 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
24497 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
24498 62 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d  b;..  if( (pEnd=
24499 3d 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30  =0 && pSelect==0
2449a 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ) || db->mallocF
2449b 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
2449c 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70  urn;.  }.  p = p
2449d 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2449e 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
2449f 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
244a0 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
244a1 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a   || !pSelect );.
244a2 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
244a3 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
244a4 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  , p->pSchema);..
244a5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
244a6 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52  MIT_CHECK.  /* R
244a7 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20  esolve names in 
244a8 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
244a9 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73  aint expressions
244aa 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
244ab 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72  pCheck ){.    Sr
244ac 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20  cList sSrc;     
244ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
244ae 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f   Fake SrcList fo
244af 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  r pParse->pNewTa
244b0 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ble */.    NameC
244b1 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20  ontext sNC;     
244b2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
244b3 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
244b4 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
244b5 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28   */..    memset(
244b6 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
244b7 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  sNC));.    memse
244b8 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65  t(&sSrc, 0, size
244b9 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73  of(sSrc));.    s
244ba 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20  Src.nSrc = 1;.  
244bb 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d    sSrc.a[0].zNam
244bc 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  e = p->zName;.  
244bd 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62    sSrc.a[0].pTab
244be 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61   = p;.    sSrc.a
244bf 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31  [0].iCursor = -1
244c0 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
244c1 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73   = pParse;.    s
244c2 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73  NC.pSrcList = &s
244c3 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43  Src;.    sNC.isC
244c4 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66  heck = 1;.    if
244c5 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
244c6 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
244c7 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20  p->pCheck) ){.  
244c8 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
244c9 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
244ca 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
244cb 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a  OMIT_CHECK) */..
244cc 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e    /* If the db->
244cd 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69  init.busy is 1 i
244ce 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72  t means we are r
244cf 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f  eading the SQL o
244d0 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c  ff the.  ** "sql
244d1 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22  ite_master" or "
244d2 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
244d3 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  er" table on the
244d4 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64   disk..  ** So d
244d5 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  o not write to t
244d6 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20  he disk again.  
244d7 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74  Extract the root
244d8 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
244d9 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
244da 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69  from the db->ini
244db 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e  t.newTnum field.
244dc 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62    (The page numb
244dd 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68  er.  ** should h
244de 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65  ave been put the
244df 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  re by the sqlite
244e0 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29  OpenCb routine.)
244e1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
244e2 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
244e3 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69   p->tnum = db->i
244e4 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d  nit.newTnum;.  }
244e5 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e  ..  /* If not in
244e6 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e  itializing, then
244e7 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64   create a record
244e8 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62   for the new tab
244e9 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53  le.  ** in the S
244ea 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
244eb 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  le of the databa
244ec 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  se..  **.  ** If
244ed 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f   this is a TEMPO
244ee 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74  RARY table, writ
244ef 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f  e the entry into
244f0 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20   the auxiliary. 
244f1 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64   ** file instead
244f2 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69   of into the mai
244f3 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
244f4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
244f5 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
244f6 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62    int n;.    Vdb
244f7 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a  e *v;.    char *
244f8 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69  zType;    /* "vi
244f9 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a  ew" or "table" *
244fa 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70  /.    char *zTyp
244fb 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20  e2;   /* "VIEW" 
244fc 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20  or "TABLE" */.  
244fd 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20    char *zStmt;  
244fe 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65    /* Text of the
244ff 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
24500 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61   CREATE VIEW sta
24501 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76  tement */..    v
24502 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
24503 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
24504 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29  f( NEVER(v==0) )
24505 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73 71   return;..    sq
24506 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
24507 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 29 3b  v, OP_Close, 0);
24508 0a 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a  ..    /* .    **
24509 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54 79 70   Initialize zTyp
2450a 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 76 69  e for the new vi
2450b 65 77 20 6f 72 20 74 61 62 6c 65 2e 0a 20 20 20  ew or table..   
2450c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
2450d 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20  Select==0 ){.   
2450e 20 20 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20     /* A regular 
2450f 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a  table */.      z
24510 54 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a  Type = "table";.
24511 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22        zType2 = "
24512 54 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20  TABLE";.#ifndef 
24513 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
24514 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
24515 20 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20    /* A view */. 
24516 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69       zType = "vi
24517 65 77 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65  ew";.      zType
24518 32 20 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e 64  2 = "VIEW";.#end
24519 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  if.    }..    /*
2451a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 43 52   If this is a CR
2451b 45 41 54 45 20 54 41 42 4c 45 20 78 78 20 41 53  EATE TABLE xx AS
2451c 20 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65   SELECT ..., exe
2451d 63 75 74 65 20 74 68 65 20 53 45 4c 45 43 54 0a  cute the SELECT.
2451e 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
2451f 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65   to populate the
24520 20 6e 65 77 20 74 61 62 6c 65 2e 20 54 68 65 20   new table. The 
24521 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
24522 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20   for the.    ** 
24523 6e 65 77 20 74 61 62 6c 65 20 69 73 20 69 6e 20  new table is in 
24524 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d  register pParse-
24525 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a  >regRoot..    **
24526 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65  .    ** Once the
24527 20 53 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e   SELECT has been
24528 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65   coded by sqlite
24529 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73  3Select(), it is
2452a 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69   in a.    ** sui
2452b 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71  table state to q
2452c 75 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c  uery for the col
2452d 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79  umn names and ty
2452e 70 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20  pes to be used. 
2452f 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77     ** by the new
24530 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20   table..    **. 
24531 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63     ** A shared-c
24532 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20  ache write-lock 
24533 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20  is not required 
24534 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  to write to the 
24535 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a  new table,.    *
24536 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f  * as a schema-lo
24537 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72  ck must have alr
24538 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e  eady been obtain
24539 65 64 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e  ed to create it.
2453a 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20   Since.    ** a 
2453b 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c  schema-lock excl
2453c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64  udes all other d
2453d 61 74 61 62 61 73 65 20 75 73 65 72 73 2c 20 74  atabase users, t
2453e 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f  he write-lock wo
2453f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65  uld.    ** be re
24540 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a  dundant..    */.
24541 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20      if( pSelect 
24542 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44  ){.      SelectD
24543 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20  est dest;.      
24544 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a  Table *pSelTab;.
24545 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50  .      assert(pP
24546 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 31 29 3b 0a  arse->nTab==1);.
24547 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24548 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70  eAddOp3(v, OP_Op
24549 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72  enWrite, 1, pPar
2454a 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62  se->regRoot, iDb
2454b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2454c 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2454d 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  1);.      pParse
2454e 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20  ->nTab = 2;.    
2454f 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
24550 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
24551 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20  RT_Table, 1);.  
24552 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
24553 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
24554 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20  t, &dest);.     
24555 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24556 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
24557 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  1);.      if( pP
24558 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b  arse->nErr==0 ){
24559 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
2455a 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74   = sqlite3Result
2455b 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72  SetOfSelect(pPar
2455c 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  se, pSelect);.  
2455d 20 20 20 20 20 20 69 66 28 20 70 53 65 6c 54 61        if( pSelTa
2455e 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
2455f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
24560 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20  ->aCol==0 );.   
24561 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70       p->nCol = p
24562 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20  SelTab->nCol;.  
24563 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20        p->aCol = 
24564 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20  pSelTab->aCol;. 
24565 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e         pSelTab->
24566 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nCol = 0;.      
24567 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20    pSelTab->aCol 
24568 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 0;.        sql
24569 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2456a 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20  pSelTab);.      
2456b 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2456c 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70  Compute the comp
2456d 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
2456e 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e   CREATE statemen
2456f 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65  t */.    if( pSe
24570 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53  lect ){.      zS
24571 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c  tmt = createTabl
24572 65 53 74 6d 74 28 64 62 2c 20 70 29 3b 0a 20 20  eStmt(db, p);.  
24573 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
24574 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a   = (int)(pEnd->z
24575 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65   - pParse->sName
24576 54 6f 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20 20  Token.z) + 1;.  
24577 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
24578 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a  te3MPrintf(db, .
24579 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
2457a 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70  E %s %.*s", zTyp
2457b 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73  e2, n, pParse->s
2457c 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20  NameToken.z.    
2457d 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20    );.    }..    
2457e 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74 68  /* A slot for th
2457f 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c 72  e record has alr
24580 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
24581 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ted in the .    
24582 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  ** SQLITE_MASTER
24583 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73 74   table.  We just
24584 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65 20   need to update 
24585 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20 61  that slot with a
24586 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  ll.    ** the in
24587 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65 20  formation we've 
24588 63 6f 6c 6c 65 63 74 65 64 2e 0a 20 20 20 20 2a  collected..    *
24589 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  /.    sqlite3Nes
2458a 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2458b 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
2458c 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20  Q.%s ".         
2458d 22 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c 20  "SET type='%s', 
2458e 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d  name=%Q, tbl_nam
2458f 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23  e=%Q, rootpage=#
24590 25 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  %d, sql=%Q ".   
24591 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
24592 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d  =#%d",.      db-
24593 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
24594 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
24595 62 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 2c  b),.      zType,
24596 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c  .      p->zName,
24597 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c  .      p->zName,
24598 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72  .      pParse->r
24599 65 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53  egRoot,.      zS
2459a 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73  tmt,.      pPars
2459b 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20  e->regRowid.    
2459c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2459d 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b  Free(db, zStmt);
2459e 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
2459f 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
245a0 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
245a1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
245a2 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a  INCREMENT.    /*
245a3 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
245a4 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61   we need to crea
245a5 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71  te an sqlite_seq
245a6 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a  uence table for.
245a7 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74      ** keeping t
245a8 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72  rack of autoincr
245a9 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20  ement keys..    
245aa 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 61  */.    if( p->ta
245ab 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
245ac 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20  increment ){.   
245ad 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62     Db *pDb = &db
245ae 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20  ->aDb[iDb];.    
245af 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
245b0 6d 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29  ma->pSeqTab==0 )
245b1 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
245b2 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
245b3 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22  rse,.          "
245b4 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e  CREATE TABLE %Q.
245b5 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28  sqlite_sequence(
245b6 6e 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20  name,seq)",.    
245b7 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
245b8 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
245b9 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
245ba 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65  ..    /* Reparse
245bb 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75   everything to u
245bc 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e  pdate our intern
245bd 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72  al data structur
245be 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  es */.    sqlite
245bf 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
245c0 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69  P_ParseSchema, i
245c1 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  Db, 0, 0,.      
245c2 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66    sqlite3MPrintf
245c3 28 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27  (db, "tbl_name='
245c4 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20  %q'",p->zName), 
245c5 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  P4_DYNAMIC);.  }
245c6 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  ...  /* Add the 
245c7 74 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d  table to the in-
245c8 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74  memory represent
245c9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74  ation of the dat
245ca 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  abase..  */.  if
245cb 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
245cc 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f  ){.    Table *pO
245cd 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a  ld;.    Schema *
245ce 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63  pSchema = p->pSc
245cf 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c 64 20 3d  hema;.    pOld =
245d0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
245d1 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c  rt(&pSchema->tbl
245d2 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a  Hash, p->zName,.
245d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
245d4 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
245d5 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e  ite3Strlen30(p->
245d6 7a 4e 61 6d 65 29 2c 70 29 3b 0a 20 20 20 20 69  zName),p);.    i
245d7 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20  f( pOld ){.     
245d8 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64   assert( p==pOld
245d9 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d   );  /* Malloc m
245da 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20  ust have failed 
245db 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72  inside HashInser
245dc 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d  t() */.      db-
245dd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
245de 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  1;.      return;
245df 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
245e0 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30  e->pNewTable = 0
245e1 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c 65  ;.    db->nTable
245e2 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  ++;.    db->flag
245e3 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
245e4 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e  rnChanges;..#ifn
245e5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
245e6 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20 69  ALTERTABLE.    i
245e7 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20 29  f( !p->pSelect )
245e8 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
245e9 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e  ar *zName = (con
245ea 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73 65  st char *)pParse
245eb 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a  ->sNameToken.z;.
245ec 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b        int nName;
245ed 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
245ee 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e 73  pSelect && pCons
245ef 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20 20   && pEnd );.    
245f0 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d    if( pCons->z==
245f1 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  0 ){.        pCo
245f2 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20  ns = pEnd;.     
245f3 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d   }.      nName =
245f4 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63 68   (int)((const ch
245f5 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20  ar *)pCons->z - 
245f6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d  zName);.      p-
245f7 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20  >addColOffset = 
245f8 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66 38  13 + sqlite3Utf8
245f9 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e  CharLen(zName, n
245fa 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  Name);.    }.#en
245fb 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  dif.  }.}..#ifnd
245fc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
245fd 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  IEW./*.** The pa
245fe 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
245ff 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72  routine in order
24600 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
24601 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f   VIEW.*/.SQLITE_
24602 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
24603 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28 0a  ite3CreateView(.
24604 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
24605 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
24606 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
24607 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20   Token *pBegin, 
24608 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
24609 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
2460a 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ins the statemen
2460b 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
2460c 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 65  ame1,     /* The
2460d 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64   token that hold
2460e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
2460f 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65  e view */.  Toke
24610 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f  n *pName2,     /
24611 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  * The token that
24612 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20   holds the name 
24613 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20  of the view */. 
24614 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
24615 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20  ,   /* A SELECT 
24616 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77  statement that w
24617 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
24618 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74  ew view */.  int
24619 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 20   isTemp,        
2461a 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54 45  /* TRUE for a TE
2461b 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f 0a  MPORARY view */.
2461c 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20    int noErr     
2461d 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73       /* Suppress
2461e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
2461f 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79 20  if VIEW already 
24620 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54  exists */.){.  T
24621 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e  able *p;.  int n
24622 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
24623 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64 3b  z;.  Token sEnd;
24624 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b  .  DbFixer sFix;
24625 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b  .  Token *pName;
24626 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71  .  int iDb;.  sq
24627 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
24628 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70  se->db;..  if( p
24629 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29 7b  Parse->nVar>0 ){
2462a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2462b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61  rMsg(pParse, "pa
2462c 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f 74  rameters are not
2462d 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65 77   allowed in view
2462e 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s");.    sqlite3
2462f 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
24630 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
24631 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  eturn;.  }.  sql
24632 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
24633 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
24634 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70 2c 20 31  Name2, isTemp, 1
24635 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a 20 20 70  , 0, noErr);.  p
24636 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
24637 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  able;.  if( p==0
24638 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
24639 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2463a 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65  pSelect);.    re
2463b 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65  turn;.  }.  asse
2463c 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  rt( pParse->nErr
2463d 3d 3d 30 20 29 3b 20 2f 2a 20 49 66 20 73 71 6c  ==0 ); /* If sql
2463e 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 20 72  ite3StartTable r
2463f 65 74 75 72 6e 20 6e 6f 6e 2d 4e 55 4c 4c 20 74  eturn non-NULL t
24640 68 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20  hen.            
24641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24642 20 2a 2a 20 74 68 65 72 65 20 63 6f 75 6c 64 20   ** there could 
24643 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 61 6e  not have been an
24644 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69   error */.  sqli
24645 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70  te3TwoPartName(p
24646 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
24647 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a  Name2, &pName);.
24648 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
24649 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2464a 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20   p->pSchema);.  
2464b 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
2464c 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
2464d 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20 70  , iDb, "view", p
2464e 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73 71 6c  Name).    && sql
2464f 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26 73  ite3FixSelect(&s
24650 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a 20 20  Fix, pSelect).  
24651 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
24652 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
24653 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74  Select);.    ret
24654 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  urn;.  }..  /* M
24655 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
24656 65 20 65 6e 74 69 72 65 20 53 45 4c 45 43 54 20  e entire SELECT 
24657 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64  statement that d
24658 65 66 69 6e 65 73 20 74 68 65 20 76 69 65 77 2e  efines the view.
24659 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20  .  ** This will 
2465a 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 20 45 78  force all the Ex
2465b 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65  pr.token.z value
2465c 73 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61  s to be dynamica
2465d 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  lly.  ** allocat
2465e 65 64 20 72 61 74 68 65 72 20 74 68 61 6e 20 70  ed rather than p
2465f 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e 70 75  oint to the inpu
24660 74 20 73 74 72 69 6e 67 20 2d 20 77 68 69 63 68  t string - which
24661 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 2a 2a   means that.  **
24662 20 74 68 65 79 20 77 69 6c 6c 20 70 65 72 73 69   they will persi
24663 73 74 20 61 66 74 65 72 20 74 68 65 20 63 75 72  st after the cur
24664 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65  rent sqlite3_exe
24665 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  c() call returns
24666 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 53 65 6c  ..  */.  p->pSel
24667 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
24668 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65  ectDup(db, pSele
24669 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ct, EXPRDUP_REDU
2466a 43 45 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  CE);.  sqlite3Se
2466b 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2466c 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20 64  Select);.  if( d
2466d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2466e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
2466f 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e   }.  if( !db->in
24670 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 73  it.busy ){.    s
24671 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
24672 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
24673 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c   p);.  }..  /* L
24674 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66  ocate the end of
24675 20 74 68 65 20 43 52 45 41 54 45 20 56 49 45 57   the CREATE VIEW
24676 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61 6b   statement.  Mak
24677 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f 0a  e sEnd point to.
24678 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20 20    ** the end..  
24679 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61 72  */.  sEnd = pPar
2467a 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b 0a  se->sLastToken;.
2467b 20 20 69 66 28 20 41 4c 57 41 59 53 28 73 45 6e    if( ALWAYS(sEn
2467c 64 2e 7a 5b 30 5d 21 3d 30 29 20 26 26 20 73 45  d.z[0]!=0) && sE
2467d 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a  nd.z[0]!=';' ){.
2467e 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45      sEnd.z += sE
2467f 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64  nd.n;.  }.  sEnd
24680 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 69  .n = 0;.  n = (i
24681 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65  nt)(sEnd.z - pBe
24682 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20 70  gin->z);.  z = p
24683 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c  Begin->z;.  whil
24684 65 28 20 41 4c 57 41 59 53 28 6e 3e 30 29 20 26  e( ALWAYS(n>0) &
24685 26 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  & sqlite3Isspace
24686 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d 3b  (z[n-1]) ){ n--;
24687 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26 7a   }.  sEnd.z = &z
24688 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e 20  [n-1];.  sEnd.n 
24689 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 73  = 1;..  /* Use s
2468a 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29  qlite3EndTable()
2468b 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65 77   to add the view
2468c 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   to the SQLITE_M
2468d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a 20  ASTER table */. 
2468e 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
2468f 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45 6e  (pParse, 0, &sEn
24690 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b  d, 0);.  return;
24691 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
24692 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f  ITE_OMIT_VIEW */
24693 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
24694 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
24695 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
24696 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
24697 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68 65  TABLE)./*.** The
24698 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
24699 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c 6c   pTable is reall
2469a 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c 20  y a VIEW.  Fill 
2469b 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a  in the names of.
2469c 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  ** the columns o
2469d 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74 68  f the view in th
2469e 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74 75  e pTable structu
2469f 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  re.  Return the 
246a0 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72 72  number.** of err
246a1 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72 6f  ors.  If an erro
246a2 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65 20  r is seen leave 
246a3 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
246a4 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   in pParse->zErr
246a5 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Msg..*/.SQLITE_P
246a6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
246a7 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
246a8 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61 72  ames(Parse *pPar
246a9 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c  se, Table *pTabl
246aa 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53 65  e){.  Table *pSe
246ab 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61 6b  lTab;   /* A fak
246ac 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
246ad 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72 65  ch we get the re
246ae 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53 65  sult set */.  Se
246af 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20 20  lect *pSel;     
246b0 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20 53  /* Copy of the S
246b1 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c 65  ELECT that imple
246b2 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20 2a  ments the view *
246b3 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30  /.  int nErr = 0
246b4 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
246b5 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  of errors encoun
246b6 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  tered */.  int n
246b7 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
246b8 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c 64  Temporarily hold
246b9 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
246ba 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65 64  cursors assigned
246bb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
246bc 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
246bd 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
246be 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c 6c  nection for mall
246bf 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69  oc errors */.  i
246c0 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64  nt (*xAuth)(void
246c1 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
246c2 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
246c3 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
246c4 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73 65 72  char*);..  asser
246c5 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23 69  t( pTable );..#i
246c6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
246c7 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
246c8 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61 62   if( sqlite3Vtab
246c9 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61 72  CallConnect(pPar
246ca 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a 20  se, pTable) ){. 
246cb 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
246cc 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66  _ERROR;.  }.  if
246cd 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
246ce 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  le) ) return 0;.
246cf 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
246d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
246d1 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76 65  .  /* A positive
246d2 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65 20   nCol means the 
246d3 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66 6f  columns names fo
246d4 72 20 74 68 69 73 20 76 69 65 77 20 61 72 65 0a  r this view are.
246d5 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e 6f    ** already kno
246d6 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  wn..  */.  if( p
246d7 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29 20  Table->nCol>0 ) 
246d8 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
246d9 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 20  A negative nCol 
246da 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61 72  is a special mar
246db 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ker meaning that
246dc 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   we are currentl
246dd 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f  y.  ** trying to
246de 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6c   compute the col
246df 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20 77  umn names.  If w
246e0 65 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f 75  e enter this rou
246e1 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20 61  tine with.  ** a
246e2 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c 20   negative nCol, 
246e3 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72 20  it means two or 
246e4 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d 20  more views form 
246e5 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68 69  a loop, like thi
246e6 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
246e7 20 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e 65   CREATE VIEW one
246e8 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f   AS SELECT * FRO
246e9 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20 20  M two;.  **     
246ea 43 52 45 41 54 45 20 56 49 45 57 20 74 77 6f 20  CREATE VIEW two 
246eb 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  AS SELECT * FROM
246ec 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20   one;.  **.  ** 
246ed 41 63 74 75 61 6c 6c 79 2c 20 74 68 65 20 65 72  Actually, the er
246ee 72 6f 72 20 61 62 6f 76 65 20 69 73 20 6e 6f 77  ror above is now
246ef 20 63 61 75 67 68 74 20 70 72 69 6f 72 20 74 6f   caught prior to
246f0 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 70   reaching this p
246f1 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75 74 20 74  oint..  ** But t
246f2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73  he following tes
246f3 74 20 69 73 20 73 74 69 6c 6c 20 69 6d 70 6f 72  t is still impor
246f4 74 61 6e 74 20 61 73 20 69 74 20 64 6f 65 73 20  tant as it does 
246f5 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20 69 6e 20  come up.  ** in 
246f6 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20  the following:. 
246f7 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 43 52   ** .  **     CR
246f8 45 41 54 45 20 54 41 42 4c 45 20 6d 61 69 6e 2e  EATE TABLE main.
246f9 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20 20 20 20  ex1(a);.  **    
246fa 20 43 52 45 41 54 45 20 54 45 4d 50 20 56 49 45   CREATE TEMP VIE
246fb 57 20 65 78 31 20 41 53 20 53 45 4c 45 43 54 20  W ex1 AS SELECT 
246fc 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20 20 2a 2a  a FROM ex1;.  **
246fd 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
246fe 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a 20 20 2a  OM temp.ex1;.  *
246ff 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e  /.  if( pTable->
24700 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73 71  nCol<0 ){.    sq
24701 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24702 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20 69  arse, "view %s i
24703 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65 66  s circularly def
24704 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e 7a  ined", pTable->z
24705 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Name);.    retur
24706 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 1;.  }.  asser
24707 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e  t( pTable->nCol>
24708 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77  =0 );..  /* If w
24709 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20  e get this far, 
2470a 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65 64  it means we need
2470b 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20   to compute the 
2470c 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20 2a  table names..  *
2470d 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
2470e 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 52  call to sqlite3R
2470f 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
24710 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20 61  () will expand a
24711 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65 6d  ny.  ** "*" elem
24712 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73 75  ents in the resu
24713 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65 20 76  lts set of the v
24714 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73 73  iew and will ass
24715 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a 2a  ign cursors.  **
24716 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74 73   to the elements
24717 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
24718 75 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f 20  use.  But we do 
24719 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20 63  not want these c
2471a 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62  hanges.  ** to b
2471b 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53 6f  e permanent.  So
2471c 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e   the computation
2471d 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63 6f   is done on a co
2471e 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  py of the SELECT
2471f 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
24720 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
24721 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61 73   view..  */.  as
24722 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53  sert( pTable->pS
24723 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c 20  elect );.  pSel 
24724 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
24725 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70  up(db, pTable->p
24726 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 66  Select, 0);.  if
24727 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 75 38  ( pSel ){.    u8
24728 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
24729 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
2472a 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20 6e  .bEnabled;.    n
2472b 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
2472c 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c  .    sqlite3SrcL
2472d 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
2472e 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70  (pParse, pSel->p
2472f 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65  Src);.    pTable
24730 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20  ->nCol = -1;.   
24731 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
24732 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69 66  Enabled = 0;.#if
24733 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24734 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
24735 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78     xAuth = db->x
24736 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41  Auth;.    db->xA
24737 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65  uth = 0;.    pSe
24738 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65  lTab = sqlite3Re
24739 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
2473a 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20  pParse, pSel);. 
2473b 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78     db->xAuth = x
2473c 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20  Auth;.#else.    
2473d 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65  pSelTab = sqlite
2473e 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
2473f 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29  ct(pParse, pSel)
24740 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 62 2d  ;.#endif.    db-
24741 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
24742 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b  led = enableLook
24743 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61 72 73  aside;.    pPars
24744 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 20  e->nTab = n;.   
24745 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b 0a   if( pSelTab ){.
24746 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
24747 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b  able->aCol==0 );
24748 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e  .      pTable->n
24749 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e  Col = pSelTab->n
2474a 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  Col;.      pTabl
2474b 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61  e->aCol = pSelTa
2474c 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 70  b->aCol;.      p
2474d 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30  SelTab->nCol = 0
2474e 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d  ;.      pSelTab-
2474f 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >aCol = 0;.     
24750 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
24751 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20 20  ble(pSelTab);.  
24752 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68      pTable->pSch
24753 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44 42  ema->flags |= DB
24754 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a 20  _UnresetViews;. 
24755 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24756 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30  pTable->nCol = 0
24757 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a  ;.      nErr++;.
24758 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
24759 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2475a 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c 73  , pSel);.  } els
2475b 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b 0a  e {.    nErr++;.
2475c 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2475d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
2475e 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b  /.  return nErr;
2475f 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21    .}.#endif /* !
24760 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
24761 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
24762 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
24763 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20  T_VIRTUALTABLE) 
24764 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
24765 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a  TE_OMIT_VIEW./*.
24766 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c  ** Clear the col
24767 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 65  umn names from e
24768 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61 74  very VIEW in dat
24769 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 74  abase idx..*/.st
2476a 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
2476b 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71 6c  ViewResetAll(sql
2476c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 64  ite3 *db, int id
2476d 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  x){.  HashElem *
2476e 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73 50  i;.  if( !DbHasP
2476f 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c  roperty(db, idx,
24770 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
24771 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  ) ) return;.  fo
24772 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(i=sqliteHashFi
24773 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64 78  rst(&db->aDb[idx
24774 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ].pSchema->tblHa
24775 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65 48  sh); i;i=sqliteH
24776 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20  ashNext(i)){.   
24777 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
24778 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
24779 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e  ;.    if( pTab->
2477a 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
2477b 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75   sqliteResetColu
2477c 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a 20  mnNames(pTab);. 
2477d 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c 65     }.  }.  DbCle
2477e 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  arProperty(db, i
2477f 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69  dx, DB_UnresetVi
24780 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20  ews);.}.#else.# 
24781 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69 65  define sqliteVie
24782 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a 23  wResetAll(A,B).#
24783 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
24784 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a  OMIT_VIEW */../*
24785 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
24786 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
24787 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75 73  he VDBE to adjus
24788 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
24789 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62 79  chema.** used by
2478a 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68 65   SQLite when the
2478b 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f 76   btree layer mov
2478c 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74 20  es a table root 
2478d 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f 6f  page. The.** roo
2478e 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62 6c  t-page of a tabl
2478f 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64 61  e or index in da
24790 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20 63  tabase iDb has c
24791 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72 6f  hanged from iFro
24792 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a 0a  m.** to iTo..**.
24793 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38 3a  ** Ticket #1728:
24794 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61 62    The symbol tab
24795 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 63  le might still c
24796 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74 69  ontain informati
24797 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 20  on.** on tables 
24798 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20 74  and/or indices t
24799 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f 63  hat are the proc
2479a 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65 6c  ess of being del
2479b 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 20  eted..** If you 
2479c 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65  are unlucky, one
2479d 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74 65   of those delete
2479e 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61 62  d indices or tab
2479f 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76  les might.** hav
247a0 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 70  e the same rootp
247a1 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74 68  age number as th
247a2 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72 20  e real table or 
247a3 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a 2a  index that is.**
247a4 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20 53   being moved.  S
247a5 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 70  o we cannot stop
247a6 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65 72   searching after
247a7 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68   the first match
247a8 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65   .** because the
247a9 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69 67   first match mig
247aa 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f 66  ht be for one of
247ab 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e 64   the deleted ind
247ac 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c 65  ices.** or table
247ad 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74 61  s and not the ta
247ae 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20 69  ble/index that i
247af 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67  s actually being
247b0 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d 75   moved..** We mu
247b1 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f 70  st continue loop
247b2 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74 61  ing until all ta
247b3 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
247b4 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 67   with.** rootpag
247b5 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62 65  e==iFrom have be
247b6 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  en converted to 
247b7 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65 20  have a rootpage 
247b8 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 64  of iTo.** in ord
247b9 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69 6e  er to be certain
247ba 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68 65   that we got the
247bb 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a 23   right one..*/.#
247bc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
247bd 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53 51  IT_AUTOVACUUM.SQ
247be 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
247bf 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67  d sqlite3RootPag
247c0 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c 20  eMoved(Db *pDb, 
247c1 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
247c2 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  To){.  HashElem 
247c3 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20 2a  *pElem;.  Hash *
247c4 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68 20  pHash;..  pHash 
247c5 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  = &pDb->pSchema-
247c6 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72 28  >tblHash;.  for(
247c7 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
247c8 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45  First(pHash); pE
247c9 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74  lem; pElem=sqlit
247ca 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29  eHashNext(pElem)
247cb 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
247cc 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
247cd 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20  ata(pElem);.    
247ce 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d 3d  if( pTab->tnum==
247cf 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70  iFrom ){.      p
247d0 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b  Tab->tnum = iTo;
247d1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48 61  .    }.  }.  pHa
247d2 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65  sh = &pDb->pSche
247d3 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20 66  ma->idxHash;.  f
247d4 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48  or(pElem=sqliteH
247d5 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b  ashFirst(pHash);
247d6 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71   pElem; pElem=sq
247d7 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c  liteHashNext(pEl
247d8 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  em)){.    Index 
247d9 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61  *pIdx = sqliteHa
247da 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20  shData(pElem);. 
247db 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75     if( pIdx->tnu
247dc 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20  m==iFrom ){.    
247dd 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 69    pIdx->tnum = i
247de 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  To;.    }.  }.}.
247df 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72  #endif../*.** Wr
247e0 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61 73  ite code to eras
247e1 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  e the table with
247e2 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c   root-page iTabl
247e3 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  e from database 
247e4 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 69  iDb..** Also wri
247e5 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69 66  te code to modif
247e6 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  y the sqlite_mas
247e7 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  ter table and in
247e8 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a  ternal schema.**
247e9 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65 20   if a root-page 
247ea 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65  of another table
247eb 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65   is moved by the
247ec 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68 69   btree-layer whi
247ed 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20 69  lst.** erasing i
247ee 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e 20  Table (this can 
247ef 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61  happen with an a
247f0 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
247f1 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  ase)..*/ .static
247f2 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f 6f   void destroyRoo
247f3 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50 61  tPage(Parse *pPa
247f4 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c  rse, int iTable,
247f5 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
247f6 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
247f7 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
247f8 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
247f9 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
247fa 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
247fb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44  beAddOp3(v, OP_D
247fc 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c 20  estroy, iTable, 
247fd 72 31 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  r1, iDb);.  sqli
247fe 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72  te3MayAbort(pPar
247ff 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  se);.#ifndef SQL
24800 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
24801 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73 74  UUM.  /* OP_Dest
24802 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20 69 6e  roy stores an in
24803 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49 66 20   integer r1. If 
24804 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20 20 2a  this integer.  *
24805 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  * is non-zero, t
24806 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72 6f  hen it is the ro
24807 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
24808 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65 64 20  f a table moved 
24809 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e  to.  ** location
2480a 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c   iTable. The fol
2480b 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64 69  lowing code modi
2480c 66 69 65 73 20 74 68 65 20 73 71 6c 69 74 65 5f  fies the sqlite_
2480d 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f 0a  master table to.
2480e 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74 68 69    ** reflect thi
2480f 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  s..  **.  ** The
24810 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20 53   "#NNN" in the S
24811 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  QL is a special 
24812 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d 65  constant that me
24813 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61 6c  ans whatever val
24814 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72 65  ue.  ** is in re
24815 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65 65  gister NNN.  See
24816 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73 20 61   grammar rules a
24817 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
24818 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 20  he TK_REGISTER. 
24819 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20 61 64   ** token for ad
2481a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
2481b 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  tion..  */.  sql
2481c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2481d 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55  pParse, .     "U
2481e 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20  PDATE %Q.%s SET 
2481f 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52  rootpage=%d WHER
24820 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61  E #%d AND rootpa
24821 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50  ge=#%d",.     pP
24822 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44  arse->db->aDb[iD
24823 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
24824 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61  _TABLE(iDb), iTa
24825 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65  ble, r1, r1);.#e
24826 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65  ndif.  sqlite3Re
24827 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
24828 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
24829 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f  ** Write VDBE co
2482a 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c  de to erase tabl
2482b 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61  e pTab and all a
2482c 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65  ssociated indice
2482d 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f  s on disk..** Co
2482e 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65  de to update the
2482f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
24830 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e  ables and intern
24831 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69  al schema defini
24832 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65  tions.** in case
24833 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c   a root-page bel
24834 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65  onging to anothe
24835 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
24836 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61   by the btree la
24837 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61  yer.** is also a
24838 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68  dded (this can h
24839 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75  appen with an au
2483a 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
2483b 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  se)..*/.static v
2483c 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65  oid destroyTable
2483d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2483e 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69  Table *pTab){.#i
2483f 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
24840 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e  _AUTOVACUUM.  In
24841 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74  dex *pIdx;.  int
24842 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
24843 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
24844 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
24845 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f  chema);.  destro
24846 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
24847 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44  , pTab->tnum, iD
24848 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  b);.  for(pIdx=p
24849 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2484a 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2484b 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f  ext){.    destro
2484c 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
2484d 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
2484e 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  b);.  }.#else.  
2484f 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
24850 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76  se may be auto-v
24851 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69  acuum capable (i
24852 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
24853 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73  TOVACUUM.  ** is
24854 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74   not defined), t
24855 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74  hen it is import
24856 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44  ant to call OP_D
24857 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20  estroy on the.  
24858 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ** table and ind
24859 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e  ex root-pages in
2485a 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67   order, starting
2485b 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69   with the numeri
2485c 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67  cally .  ** larg
2485d 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75  est root-page nu
2485e 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61  mber. This guara
2485f 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20  ntees that none 
24860 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  of the root-page
24861 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73  s.  ** to be des
24862 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61  troyed is reloca
24863 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65  ted by an earlie
24864 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e  r OP_Destroy. i.
24865 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66  e. if the.  ** f
24866 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f  ollowing were co
24867 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f  ded:.  **.  ** O
24868 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20  P_Destroy 4 0.  
24869 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44  ** ....  ** OP_D
2486a 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a  estroy 5 0.  **.
2486b 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61    ** and root pa
2486c 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f  ge 5 happened to
2486d 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20   be the largest 
2486e 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72  root-page number
2486f 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74   in the.  ** dat
24870 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74  abase, then root
24871 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65   page 5 would be
24872 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34   moved to page 4
24873 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f   by the .  ** "O
24874 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f  P_Destroy 4 0" o
24875 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65  pcode. The subse
24876 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f  quent "OP_Destro
24877 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74  y 5 0" would hit
24878 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73  .  ** a free-lis
24879 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69  t page..  */.  i
2487a 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e  nt iTab = pTab->
2487b 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73  tnum;.  int iDes
2487c 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77  troyed = 0;..  w
2487d 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49  hile( 1 ){.    I
2487e 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
2487f 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30  int iLargest = 0
24880 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74  ;..    if( iDest
24881 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62  royed==0 || iTab
24882 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20  <iDestroyed ){. 
24883 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20       iLargest = 
24884 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iTab;.    }.    
24885 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
24886 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
24887 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
24888 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d        int iIdx =
24889 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20   pIdx->tnum;.   
2488a 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
2488b 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
2488c 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
2488d 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64   if( (iDestroyed
2488e 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65  ==0 || (iIdx<iDe
2488f 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64  stroyed)) && iId
24890 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20  x>iLargest ){.  
24891 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d        iLargest =
24892 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20   iIdx;.      }. 
24893 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61     }.    if( iLa
24894 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  rgest==0 ){.    
24895 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65    return;.    }e
24896 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
24897 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24898 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
24899 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2489a 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74  ema);.      dest
2489b 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72  royRootPage(pPar
2489c 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44  se, iLargest, iD
2489d 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72  b);.      iDestr
2489e 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b  oyed = iLargest;
2489f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
248a0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
248a1 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
248a2 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
248a3 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45   of a DROP TABLE
248a4 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70   statement..** p
248a5 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
248a6 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
248a7 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a   be dropped..*/.
248a8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
248a9 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
248aa 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
248ab 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61  se, SrcList *pNa
248ac 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20  me, int isView, 
248ad 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61  int noErr){.  Ta
248ae 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62  ble *pTab;.  Vdb
248af 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
248b0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
248b1 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
248b2 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
248b3 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
248b4 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
248b5 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
248b6 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
248b7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61  );.  assert( pNa
248b8 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  me->nSrc==1 );. 
248b9 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
248ba 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
248bb 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20 20 20  e, isView, .    
248bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
248bd 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 61          pName->a
248be 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65  [0].zName, pName
248bf 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
248c0 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  );..  if( pTab==
248c1 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f 45  0 ){.    if( noE
248c2 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rr ){.      sqli
248c3 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50  te3ErrorClear(pP
248c4 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  arse);.    }.   
248c5 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
248c6 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44 62  table;.  }.  iDb
248c7 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
248c8 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
248c9 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
248ca 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
248cb 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a  iDb<db->nDb );..
248cc 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
248cd 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  a virtual table,
248ce 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43 6f 6c   call ViewGetCol
248cf 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20 65 6e  umnNames() to en
248d0 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69 73 20  sure.  ** it is 
248d1 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a  initialized..  *
248d2 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
248d3 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c 69 74  l(pTab) && sqlit
248d4 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
248d5 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
248d6 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  b) ){.    goto e
248d7 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
248d8 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
248d9 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
248da 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e  ATION.  {.    in
248db 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f 6e 73  t code;.    cons
248dc 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53  t char *zTab = S
248dd 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
248de 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
248df 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
248e0 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
248e1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
248e2 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73  2 = 0;.    if( s
248e3 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
248e4 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
248e5 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20  ELETE, zTab, 0, 
248e6 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74  zDb)){.      got
248e7 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
248e8 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
248e9 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20   isView ){.     
248ea 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
248eb 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20  B && iDb==1 ){. 
248ec 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
248ed 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56  LITE_DROP_TEMP_V
248ee 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IEW;.      }else
248ef 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
248f0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45   SQLITE_DROP_VIE
248f1 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64  W;.      }.#ifnd
248f2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
248f3 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
248f4 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72 74  }else if( IsVirt
248f5 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
248f6 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
248f7 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a 20 20  _DROP_VTABLE;.  
248f8 20 20 20 20 7a 41 72 67 32 20 3d 20 73 71 6c 69      zArg2 = sqli
248f9 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
248fa 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e   pTab)->pMod->zN
248fb 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ame;.#endif.    
248fc 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
248fd 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
248fe 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20   iDb==1 ){.     
248ff 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
24900 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45  _DROP_TEMP_TABLE
24901 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
24902 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
24903 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b  LITE_DROP_TABLE;
24904 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24905 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
24906 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
24907 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  code, pTab->zNam
24908 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20 29  e, zArg2, zDb) )
24909 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
2490a 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
2490b 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
2490c 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2490d 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
2490e 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
2490f 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
24910 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
24911 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
24912 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73  }.#endif.  if( s
24913 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
24914 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
24915 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  ite_", 7)==0 ){.
24916 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24917 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
24918 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
24919 20 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62 2d   dropped", pTab-
2491a 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
2491b 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
2491c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  e;.  }..#ifndef 
2491d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2491e 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f  .  /* Ensure DRO
2491f 50 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20 75  P TABLE is not u
24920 73 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20 61  sed on a view, a
24921 6e 64 20 44 52 4f 50 20 56 49 45 57 20 69 73 20  nd DROP VIEW is 
24922 6e 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e  not used.  ** on
24923 20 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20   a table..  */. 
24924 20 69 66 28 20 69 73 56 69 65 77 20 26 26 20 70   if( isView && p
24925 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
24926 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
24927 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24928 75 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20 74  use DROP TABLE t
24929 6f 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20 25  o delete table %
2492a 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  s", pTab->zName)
2492b 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
2492c 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  drop_table;.  }.
2492d 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26    if( !isView &&
2492e 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2492f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
24930 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
24931 73 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f 20  se DROP VIEW to 
24932 64 65 6c 65 74 65 20 76 69 65 77 20 25 73 22 2c  delete view %s",
24933 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
24934 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24935 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  p_table;.  }.#en
24936 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
24937 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  te code to remov
24938 65 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  e the table from
24939 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
2493a 65 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a  e.  ** on disk..
2493b 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
2493c 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2493d 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
2493e 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2493f 67 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44 62  ger;.    Db *pDb
24940 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
24941 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67  ;.    sqlite3Beg
24942 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
24943 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29  (pParse, 1, iDb)
24944 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
24945 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
24946 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69  BLE.    if( IsVi
24947 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
24948 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24949 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65  AddOp0(v, OP_VBe
2494a 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  gin);.    }.#end
2494b 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b  if.    sqlite3Fk
2494c 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65  DropTable(pParse
2494d 2c 20 70 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a  , pName, pTab);.
2494e 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  .    /* Drop all
2494f 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
24950 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61  ated with the ta
24951 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
24952 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 69  d. Code.    ** i
24953 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 72  s generated to r
24954 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72  emove entries fr
24955 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  om sqlite_master
24956 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 73   and/or.    ** s
24957 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
24958 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 20  r if required.. 
24959 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 67     */.    pTrigg
2495a 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  er = sqlite3Trig
2495b 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  gerList(pParse, 
2495c 70 54 61 62 29 3b 0a 20 20 20 20 77 68 69 6c 65  pTab);.    while
2495d 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
2495e 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69      assert( pTri
2495f 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  gger->pSchema==p
24960 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20  Tab->pSchema || 
24961 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67  .          pTrig
24962 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62  ger->pSchema==db
24963 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
24964 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
24965 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
24966 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
24967 29 3b 0a 20 20 20 20 20 20 70 54 72 69 67 67 65  );.      pTrigge
24968 72 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e  r = pTrigger->pN
24969 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e  ext;.    }..#ifn
2496a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2496b 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
2496c 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20    /* Remove any 
2496d 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 73  entries of the s
2496e 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
2496f 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20  able associated 
24970 77 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20  with.    ** the 
24971 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70  table being drop
24972 70 65 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e  ped. This is don
24973 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 61 62  e before the tab
24974 6c 65 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20  le is dropped.  
24975 20 20 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65    ** at the btre
24976 65 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65  e level, in case
24977 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
24978 65 6e 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73  ence table needs
24979 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20   to.    ** move 
2497a 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74  as a result of t
2497b 68 65 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70  he drop (can hap
2497c 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75  pen in auto-vacu
2497d 75 6d 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f  um mode)..    */
2497e 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74  .    if( pTab->t
2497f 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
24980 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
24981 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
24982 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
24983 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
24984 52 4f 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 65  ROM %s.sqlite_se
24985 71 75 65 6e 63 65 20 57 48 45 52 45 20 6e 61 6d  quence WHERE nam
24986 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 70  e=%Q",.        p
24987 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  Db->zName, pTab-
24988 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a  >zName.      );.
24989 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
2498a 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51    /* Drop all SQ
2498b 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
2498c 65 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72  e and index entr
2498d 69 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74  ies that refer t
2498e 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62  o the.    ** tab
2498f 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20  le. The program 
24990 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75  name loops throu
24991 67 68 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  gh the master ta
24992 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a  ble and deletes.
24993 20 20 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77      ** every row
24994 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20   that refers to 
24995 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73  a table of the s
24996 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20  ame name as the 
24997 6f 6e 65 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a  one being.    **
24998 20 64 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65   dropped. Trigge
24999 72 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73  rs are handled s
2499a 65 70 65 72 61 74 65 6c 79 20 62 65 63 61 75 73  eperately becaus
2499b 65 20 61 20 74 72 69 67 67 65 72 20 63 61 6e 20  e a trigger can 
2499c 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65  be.    ** create
2499d 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61  d in the temp da
2499e 74 61 62 61 73 65 20 74 68 61 74 20 72 65 66 65  tabase that refe
2499f 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 69 6e  rs to a table in
249a0 20 61 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20   another.    ** 
249a1 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
249a2 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
249a3 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  edParse(pParse, 
249a4 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45  .        "DELETE
249a5 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52   FROM %Q.%s WHER
249a6 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e  E tbl_name=%Q an
249a7 64 20 74 79 70 65 21 3d 27 74 72 69 67 67 65 72  d type!='trigger
249a8 27 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d  '",.        pDb-
249a9 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  >zName, SCHEMA_T
249aa 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d  ABLE(iDb), pTab-
249ab 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a  >zName);..    /*
249ac 20 44 72 6f 70 20 61 6e 79 20 73 74 61 74 69 73   Drop any statis
249ad 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73 71  tics from the sq
249ae 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
249af 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 20 2a  , if it exists *
249b0 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
249b1 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
249b2 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64  sqlite_stat1", d
249b3 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
249b4 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  e) ){.      sqli
249b5 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
249b6 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22  Parse,.        "
249b7 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73  DELETE FROM %Q.s
249b8 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52  qlite_stat1 WHER
249b9 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e  E tbl=%Q", pDb->
249ba 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
249bb 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  me.      );.    
249bc 7d 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  }..    if( !isVi
249bd 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ew && !IsVirtual
249be 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
249bf 64 65 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61  destroyTable(pPa
249c0 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
249c1 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65  }..    /* Remove
249c2 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
249c3 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69   from SQLite's i
249c4 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61  nternal schema a
249c5 6e 64 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a  nd modify.    **
249c6 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
249c7 69 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ie..    */.    i
249c8 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
249c9 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
249ca 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
249cb 20 4f 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44   OP_VDestroy, iD
249cc 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  b, 0, 0, pTab->z
249cd 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Name, 0);.    }.
249ce 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
249cf 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
249d0 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30  Table, iDb, 0, 0
249d1 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
249d2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
249d3 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
249d4 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73  e, iDb);.  }.  s
249d5 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c  qliteViewResetAl
249d6 6c 28 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69  l(db, iDb);..exi
249d7 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20  t_drop_table:.  
249d8 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
249d9 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b  lete(db, pName);
249da 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
249db 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
249dc 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
249dd 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20   foreign key on 
249de 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72  the table.** cur
249df 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
249e0 73 74 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f  struction.  pFro
249e1 6d 43 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20  mCol determines 
249e2 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  which columns.**
249e3 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
249e4 74 61 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74  table point to t
249e5 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20  he foreign key. 
249e6 20 49 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20   If pFromCol==0 
249e7 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20  then.** connect 
249e8 74 68 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c  the key to the l
249e9 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72  ast column inser
249ea 74 65 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65  ted.  pTo is the
249eb 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20   name of.** the 
249ec 74 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74  table referred t
249ed 6f 2e 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20  o.  pToCol is a 
249ee 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69  list of tables i
249ef 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70  n the other.** p
249f0 54 6f 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  To table that th
249f1 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f  e foreign key po
249f2 69 6e 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20  ints to.  flags 
249f3 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20  contains all.** 
249f4 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
249f5 74 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72  t the conflict r
249f6 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69  esolution algori
249f7 74 68 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a  thms specified.*
249f8 2a 20 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45  * in the ON DELE
249f9 54 45 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e  TE, ON UPDATE an
249fa 64 20 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75  d ON INSERT clau
249fb 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b  ses..**.** An FK
249fc 65 79 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ey structure is 
249fd 63 72 65 61 74 65 64 20 61 6e 64 20 61 64 64 65  created and adde
249fe 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63  d to the table c
249ff 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65  urrently.** unde
24a00 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69  r construction i
24a01 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e  n the pParse->pN
24a02 65 77 54 61 62 6c 65 20 66 69 65 6c 64 2e 0a 2a  ewTable field..*
24a03 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 65 69 67 6e  *.** The foreign
24a04 20 6b 65 79 20 69 73 20 73 65 74 20 66 6f 72 20   key is set for 
24a05 49 4d 4d 45 44 49 41 54 45 20 70 72 6f 63 65 73  IMMEDIATE proces
24a06 73 69 6e 67 2e 20 20 41 20 73 75 62 73 65 71 75  sing.  A subsequ
24a07 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73  ent call.** to s
24a08 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
24a09 67 6e 4b 65 79 28 29 20 6d 69 67 68 74 20 63 68  gnKey() might ch
24a0a 61 6e 67 65 20 74 68 69 73 20 74 6f 20 44 45 46  ange this to DEF
24a0b 45 52 52 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ERRED..*/.SQLITE
24a0c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
24a0d 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69  lite3CreateForei
24a0e 67 6e 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a  gnKey(.  Parse *
24a0f 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
24a10 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
24a11 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
24a12 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f  pFromCol,  /* Co
24a13 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61  lumns in this ta
24a14 62 6c 65 20 74 68 61 74 20 70 6f 69 6e 74 20 74  ble that point t
24a15 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f  o other table */
24a16 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20  .  Token *pTo,  
24a17 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
24a18 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 61 62  of the other tab
24a19 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  le */.  ExprList
24a1a 20 2a 70 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20   *pToCol,    /* 
24a1b 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f  Columns in the o
24a1c 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ther table */.  
24a1d 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20 20 20  int flags       
24a1e 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74       /* Conflict
24a1f 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f   resolution algo
24a20 72 69 74 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20  rithms. */.){.  
24a21 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
24a22 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65  arse->db;.#ifnde
24a23 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
24a24 52 45 49 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79  REIGN_KEY.  FKey
24a25 20 2a 70 46 4b 65 79 20 3d 20 30 3b 0a 20 20 46   *pFKey = 0;.  F
24a26 4b 65 79 20 2a 70 4e 65 78 74 54 6f 3b 0a 20 20  Key *pNextTo;.  
24a27 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72 73  Table *p = pPars
24a28 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20  e->pNewTable;.  
24a29 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74  int nByte;.  int
24a2a 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a   i;.  int nCol;.
24a2b 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73    char *z;..  as
24a2c 73 65 72 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a  sert( pTo!=0 );.
24a2d 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 49 4e    if( p==0 || IN
24a2e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 20  _DECLARE_VTAB ) 
24a2f 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69  goto fk_end;.  i
24a30 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29  f( pFromCol==0 )
24a31 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d  {.    int iCol =
24a32 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20   p->nCol-1;.    
24a33 69 66 28 20 4e 45 56 45 52 28 69 43 6f 6c 3c 30  if( NEVER(iCol<0
24a34 29 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b  ) ) goto fk_end;
24a35 0a 20 20 20 20 69 66 28 20 70 54 6f 43 6f 6c 20  .    if( pToCol 
24a36 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72  && pToCol->nExpr
24a37 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=1 ){.      sql
24a38 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24a39 72 73 65 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65  rse, "foreign ke
24a3a 79 20 6f 6e 20 25 73 22 0a 20 20 20 20 20 20 20  y on %s".       
24a3b 20 20 22 20 73 68 6f 75 6c 64 20 72 65 66 65 72    " should refer
24a3c 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f  ence only one co
24a3d 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 25 54  lumn of table %T
24a3e 22 2c 0a 20 20 20 20 20 20 20 20 20 70 2d 3e 61  ",.         p->a
24a3f 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c  Col[iCol].zName,
24a40 20 70 54 6f 29 3b 0a 20 20 20 20 20 20 67 6f 74   pTo);.      got
24a41 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a  o fk_end;.    }.
24a42 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20      nCol = 1;.  
24a43 7d 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f 6c  }else if( pToCol
24a44 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70   && pToCol->nExp
24a45 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78  r!=pFromCol->nEx
24a46 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  pr ){.    sqlite
24a47 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24a48 2c 0a 20 20 20 20 20 20 20 20 22 6e 75 6d 62 65  ,.        "numbe
24a49 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
24a4a 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73  foreign key does
24a4b 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e   not match the n
24a4c 75 6d 62 65 72 20 6f 66 20 22 0a 20 20 20 20 20  umber of ".     
24a4d 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74     "columns in t
24a4e 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  he referenced ta
24a4f 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ble");.    goto 
24a50 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73 65 7b  fk_end;.  }else{
24a51 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46 72 6f  .    nCol = pFro
24a52 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d  mCol->nExpr;.  }
24a53 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  .  nByte = sizeo
24a54 66 28 2a 70 46 4b 65 79 29 20 2b 20 28 6e 43 6f  f(*pFKey) + (nCo
24a55 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 70 46 4b 65  l-1)*sizeof(pFKe
24a56 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 70 54  y->aCol[0]) + pT
24a57 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 28 20  o->n + 1;.  if( 
24a58 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f  pToCol ){.    fo
24a59 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c 2d  r(i=0; i<pToCol-
24a5a 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
24a5b 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c      nByte += sql
24a5c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 6f  ite3Strlen30(pTo
24a5d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  Col->a[i].zName)
24a5e 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   + 1;.    }.  }.
24a5f 20 20 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65    pFKey = sqlite
24a60 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
24a61 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28  , nByte );.  if(
24a62 20 70 46 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20   pFKey==0 ){.   
24a63 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
24a64 7d 0a 20 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  }.  pFKey->pFrom
24a65 20 3d 20 70 3b 0a 20 20 70 46 4b 65 79 2d 3e 70   = p;.  pFKey->p
24a66 4e 65 78 74 46 72 6f 6d 20 3d 20 70 2d 3e 70 46  NextFrom = p->pF
24a67 4b 65 79 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72  Key;.  z = (char
24a68 2a 29 26 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6e  *)&pFKey->aCol[n
24a69 43 6f 6c 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 7a  Col];.  pFKey->z
24a6a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d 63 70 79  To = z;.  memcpy
24a6b 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70 54 6f 2d  (z, pTo->z, pTo-
24a6c 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d 3e 6e 5d  >n);.  z[pTo->n]
24a6d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44   = 0;.  sqlite3D
24a6e 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 7a 20 2b  equote(z);.  z +
24a6f 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46  = pTo->n+1;.  pF
24a70 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c  Key->nCol = nCol
24a71 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c  ;.  if( pFromCol
24a72 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46 4b 65 79  ==0 ){.    pFKey
24a73 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20  ->aCol[0].iFrom 
24a74 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 7d  = p->nCol-1;.  }
24a75 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d  else{.    for(i=
24a76 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
24a77 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
24a78 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
24a79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
24a7a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
24a7b 33 53 74 72 49 43 6d 70 28 70 2d 3e 61 43 6f 6c  3StrICmp(p->aCol
24a7c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46 72 6f 6d  [j].zName, pFrom
24a7d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  Col->a[i].zName)
24a7e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
24a7f 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e   pFKey->aCol[i].
24a80 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20 20 20 20 20  iFrom = j;.     
24a81 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
24a82 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
24a83 20 20 20 20 69 66 28 20 6a 3e 3d 70 2d 3e 6e 43      if( j>=p->nC
24a84 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ol ){.        sq
24a85 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24a86 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
24a87 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e   "unknown column
24a88 20 5c 22 25 73 5c 22 20 69 6e 20 66 6f 72 65 69   \"%s\" in forei
24a89 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
24a8a 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70  n", .          p
24a8b 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e  FromCol->a[i].zN
24a8c 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ame);.        go
24a8d 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 20  to fk_end;.     
24a8e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
24a8f 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20  f( pToCol ){.   
24a90 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
24a91 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  ; i++){.      in
24a92 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
24a93 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b  len30(pToCol->a[
24a94 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  i].zName);.     
24a95 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e   pFKey->aCol[i].
24a96 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20  zCol = z;.      
24a97 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c  memcpy(z, pToCol
24a98 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29  ->a[i].zName, n)
24a99 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30  ;.      z[n] = 0
24a9a 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31  ;.      z += n+1
24a9b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46  ;.    }.  }.  pF
24a9c 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20  Key->isDeferred 
24a9d 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 41  = 0;.  pFKey->aA
24a9e 63 74 69 6f 6e 5b 30 5d 20 3d 20 28 75 38 29 28  ction[0] = (u8)(
24a9f 66 6c 61 67 73 20 26 20 30 78 66 66 29 3b 20 20  flags & 0xff);  
24aa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20            /* ON 
24aa1 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 20 2a 2f  DELETE action */
24aa2 0a 20 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f  .  pFKey->aActio
24aa3 6e 5b 31 5d 20 3d 20 28 75 38 29 28 28 66 6c 61  n[1] = (u8)((fla
24aa4 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 66 66  gs >> 8 ) & 0xff
24aa5 29 3b 20 20 20 20 2f 2a 20 4f 4e 20 55 50 44 41  );    /* ON UPDA
24aa6 54 45 20 61 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  TE action */..  
24aa7 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65 79 20  pNextTo = (FKey 
24aa8 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  *)sqlite3HashIns
24aa9 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d  ert(&p->pSchema-
24aaa 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20 20 20  >fkeyHash, .    
24aab 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73 71    pFKey->zTo, sq
24aac 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 46  lite3Strlen30(pF
24aad 4b 65 79 2d 3e 7a 54 6f 29 2c 20 28 76 6f 69 64  Key->zTo), (void
24aae 20 2a 29 70 46 4b 65 79 0a 20 20 29 3b 0a 20 20   *)pFKey.  );.  
24aaf 69 66 28 20 70 4e 65 78 74 54 6f 3d 3d 70 46 4b  if( pNextTo==pFK
24ab0 65 79 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  ey ){.    db->ma
24ab1 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
24ab2 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b      goto fk_end;
24ab3 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 78 74  .  }.  if( pNext
24ab4 54 6f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  To ){.    assert
24ab5 28 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76  ( pNextTo->pPrev
24ab6 54 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 46 4b  To==0 );.    pFK
24ab7 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70 4e  ey->pNextTo = pN
24ab8 65 78 74 54 6f 3b 0a 20 20 20 20 70 4e 65 78 74  extTo;.    pNext
24ab9 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d 20 70 46  To->pPrevTo = pF
24aba 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  Key;.  }..  /* L
24abb 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20  ink the foreign 
24abc 6b 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65  key to the table
24abd 20 61 73 20 74 68 65 20 6c 61 73 74 20 73 74 65   as the last ste
24abe 70 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b  p..  */.  p->pFK
24abf 65 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46  ey = pFKey;.  pF
24ac0 4b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64  Key = 0;..fk_end
24ac1 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  :.  sqlite3DbFre
24ac2 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65  e(db, pFKey);.#e
24ac3 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
24ac4 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
24ac5 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73  EIGN_KEY) */.  s
24ac6 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
24ac7 6c 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f  lete(db, pFromCo
24ac8 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  l);.  sqlite3Exp
24ac9 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
24aca 70 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pToCol);.}../*.*
24acb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
24acc 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  s called when an
24acd 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
24ace 49 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c  IATE or INITIALL
24acf 59 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c  Y DEFERRED.** cl
24ad0 61 75 73 65 20 69 73 20 73 65 65 6e 20 61 73 20  ause is seen as 
24ad1 70 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67  part of a foreig
24ad2 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e  n key definition
24ad3 2e 20 20 54 68 65 20 69 73 44 65 66 65 72 72 65  .  The isDeferre
24ad4 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  d.** parameter i
24ad5 73 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c  s 1 for INITIALL
24ad6 59 20 44 45 46 45 52 52 45 44 20 61 6e 64 20 30  Y DEFERRED and 0
24ad7 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49   for INITIALLY I
24ad8 4d 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65  MMEDIATE..** The
24ad9 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65   behavior of the
24ada 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
24adb 72 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b  reated foreign k
24adc 65 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a  ey is adjusted.*
24add 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a  * accordingly..*
24ade 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24adf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66   void sqlite3Def
24ae0 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72  erForeignKey(Par
24ae1 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
24ae2 69 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66  isDeferred){.#if
24ae3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24ae4 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54  _FOREIGN_KEY.  T
24ae5 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b  able *pTab;.  FK
24ae6 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28  ey *pFKey;.  if(
24ae7 20 28 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d   (pTab = pParse-
24ae8 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c  >pNewTable)==0 |
24ae9 7c 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d  | (pFKey = pTab-
24aea 3e 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74  >pFKey)==0 ) ret
24aeb 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69  urn;.  assert( i
24aec 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20  sDeferred==0 || 
24aed 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b  isDeferred==1 );
24aee 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d   /* EV: R-30323-
24aef 32 31 39 31 37 20 2a 2f 0a 20 20 70 46 4b 65 79  21917 */.  pFKey
24af0 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 28  ->isDeferred = (
24af1 75 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a 23  u8)isDeferred;.#
24af2 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
24af3 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
24af4 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64  t will erase and
24af5 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70   refill index *p
24af6 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a  Idx.  This is.**
24af7 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c   used to initial
24af8 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61  ize a newly crea
24af9 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20  ted index or to 
24afa 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  recompute the.**
24afb 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69   content of an i
24afc 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  ndex in response
24afd 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f   to a REINDEX co
24afe 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20  mmand..**.** if 
24aff 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e  memRootPage is n
24b00 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20  ot negative, it 
24b01 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69  means that the i
24b02 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a  ndex is newly.**
24b03 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72   created.  The r
24b04 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65  egister specifie
24b05 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65  d by memRootPage
24b06 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a   contains the.**
24b07 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
24b08 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  r of the index. 
24b09 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20   If memRootPage 
24b0a 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
24b0b 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61  n.** the index a
24b0c 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e  lready exists an
24b0d 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65  d must be cleare
24b0e 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72  d before being r
24b0f 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74  efilled and.** t
24b10 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
24b11 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
24b12 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70   is taken from p
24b13 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a  Index->tnum..*/.
24b14 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
24b15 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50  te3RefillIndex(P
24b16 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e  arse *pParse, In
24b17 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74  dex *pIndex, int
24b18 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20   memRootPage){. 
24b19 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
24b1a 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20  Index->pTable;  
24b1b 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61  /* The table tha
24b1c 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a  t is indexed */.
24b1d 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61    int iTab = pPa
24b1e 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20  rse->nTab++;    
24b1f 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72   /* Btree cursor
24b20 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a   used for pTab *
24b21 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70  /.  int iIdx = p
24b22 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20  Parse->nTab++;  
24b23 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73     /* Btree curs
24b24 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64  or used for pInd
24b25 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ex */.  int addr
24b26 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
24b27 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
24b28 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
24b29 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
24b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b2b 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
24b2c 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20  e of index */.  
24b2d 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
24b2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24b2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
24b30 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61  into this virtua
24b31 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b  l machine */.  K
24b32 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20  eyInfo *pKey;   
24b33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24b34 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64   KeyInfo for ind
24b35 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49  ex */.  int regI
24b36 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  dxKey;          
24b37 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
24b38 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ers containing t
24b39 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a  he index key */.
24b3a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
24b3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b3c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
24b3d 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20  ding assemblied 
24b3e 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a  index record */.
24b3f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24b40 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
24b41 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
24b42 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
24b43 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
24b44 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
24b45 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  db, pIndex->pSch
24b46 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
24b47 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
24b48 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73  RIZATION.  if( s
24b49 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
24b4a 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52  pParse, SQLITE_R
24b4b 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e  EINDEX, pIndex->
24b4c 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20  zName, 0,.      
24b4d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
24b4e 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75  me ) ){.    retu
24b4f 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
24b50 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77    /* Require a w
24b51 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
24b52 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72   table to perfor
24b53 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  m this operation
24b54 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62   */.  sqlite3Tab
24b55 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
24b56 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
24b57 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  1, pTab->zName);
24b58 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
24b59 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24b5a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
24b5b 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f  urn;.  if( memRo
24b5c 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20  otPage>=0 ){.   
24b5d 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50   tnum = memRootP
24b5e 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  age;.  }else{.  
24b5f 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d    tnum = pIndex-
24b60 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >tnum;.    sqlit
24b61 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24b62 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20  OP_Clear, tnum, 
24b63 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79  iDb);.  }.  pKey
24b64 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
24b65 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
24b66 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  Index);.  sqlite
24b67 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
24b68 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64  P_OpenWrite, iId
24b69 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20  x, tnum, iDb, . 
24b6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b6b 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c     (char *)pKey,
24b6c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
24b6d 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52  OFF);.  if( memR
24b6e 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ootPage>=0 ){.  
24b6f 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
24b70 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d  ngeP5(v, 1);.  }
24b71 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
24b72 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62  ble(pParse, iTab
24b73 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f  , iDb, pTab, OP_
24b74 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64  OpenRead);.  add
24b75 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
24b76 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
24b77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  ind, iTab, 0);. 
24b78 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
24b79 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
24b7a 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78  Parse);.  regIdx
24b7b 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e  Key = sqlite3Gen
24b7c 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50  erateIndexKey(pP
24b7d 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54  arse, pIndex, iT
24b7e 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31  ab, regRecord, 1
24b7f 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d  );.  if( pIndex-
24b80 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
24b81 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  e ){.    const i
24b82 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65  nt regRowid = re
24b83 67 49 64 78 4b 65 79 20 2b 20 70 49 6e 64 65 78  gIdxKey + pIndex
24b84 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 63  ->nColumn;.    c
24b85 6f 6e 73 74 20 69 6e 74 20 6a 32 20 3d 20 73 71  onst int j2 = sq
24b86 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
24b87 41 64 64 72 28 76 29 20 2b 20 32 3b 0a 20 20 20  Addr(v) + 2;.   
24b88 20 76 6f 69 64 20 2a 20 63 6f 6e 73 74 20 70 52   void * const pR
24b89 65 67 4b 65 79 20 3d 20 53 51 4c 49 54 45 5f 49  egKey = SQLITE_I
24b8a 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49 64 78  NT_TO_PTR(regIdx
24b8b 4b 65 79 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  Key);..    /* Th
24b8c 65 20 72 65 67 69 73 74 65 72 73 20 61 63 63 65  e registers acce
24b8d 73 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49  ssed by the OP_I
24b8e 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 77  sUnique opcode w
24b8f 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20  ere allocated.  
24b90 20 20 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74    ** using sqlit
24b91 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 29  e3GetTempRange()
24b92 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 73   inside of the s
24b93 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e  qlite3GenerateIn
24b94 64 65 78 4b 65 79 28 29 0a 20 20 20 20 2a 2a 20  dexKey().    ** 
24b95 63 61 6c 6c 20 61 62 6f 76 65 2e 20 4a 75 73 74  call above. Just
24b96 20 62 65 66 6f 72 65 20 74 68 61 74 20 66 75 6e   before that fun
24b97 63 74 69 6f 6e 20 77 61 73 20 66 72 65 65 64 20  ction was freed 
24b98 74 68 65 79 20 77 65 72 65 20 72 65 6c 65 61 73  they were releas
24b99 65 64 0a 20 20 20 20 2a 2a 20 28 6d 61 64 65 20  ed.    ** (made 
24b9a 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65  available to the
24b9b 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20 72 65   compiler for re
24b9c 75 73 65 29 20 75 73 69 6e 67 20 0a 20 20 20 20  use) using .    
24b9d 2a 2a 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  ** sqlite3Releas
24b9e 65 54 65 6d 70 52 61 6e 67 65 28 29 2e 20 53 6f  eTempRange(). So
24b9f 20 69 6e 20 73 6f 6d 65 20 77 61 79 73 20 68 61   in some ways ha
24ba0 76 69 6e 67 20 74 68 65 20 4f 50 5f 49 73 55 6e  ving the OP_IsUn
24ba1 69 71 75 65 0a 20 20 20 20 2a 2a 20 6f 70 63 6f  ique.    ** opco
24ba2 64 65 20 75 73 65 20 74 68 65 20 76 61 6c 75 65  de use the value
24ba3 73 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20  s stored within 
24ba4 73 65 65 6d 73 20 64 61 6e 67 65 72 6f 75 73 2e  seems dangerous.
24ba5 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 0a   However, since.
24ba6 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 62 65      ** we can be
24ba7 20 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74   sure that no ot
24ba8 68 65 72 20 74 65 6d 70 20 72 65 67 69 73 74 65  her temp registe
24ba9 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c  rs have been all
24baa 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20 73 69  ocated.    ** si
24bab 6e 63 65 20 73 71 6c 69 74 65 33 52 65 6c 65 61  nce sqlite3Relea
24bac 73 65 54 65 6d 70 52 61 6e 67 65 28 29 20 77 61  seTempRange() wa
24bad 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20  s called, it is 
24bae 73 61 66 65 20 74 6f 20 64 6f 20 73 6f 2e 0a 20  safe to do so.. 
24baf 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
24bb0 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
24bb1 50 5f 49 73 55 6e 69 71 75 65 2c 20 69 49 64 78  P_IsUnique, iIdx
24bb2 2c 20 6a 32 2c 20 72 65 67 52 6f 77 69 64 2c 20  , j2, regRowid, 
24bb3 70 52 65 67 4b 65 79 2c 20 50 34 5f 49 4e 54 33  pRegKey, P4_INT3
24bb4 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48  2);.    sqlite3H
24bb5 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20  altConstraint(. 
24bb6 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 4f         pParse, O
24bb7 45 5f 41 62 6f 72 74 2c 20 22 69 6e 64 65 78 65  E_Abort, "indexe
24bb8 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f  d columns are no
24bb9 74 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54  t unique", P4_ST
24bba 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ATIC);.  }.  sql
24bbb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24bbc 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
24bbd 69 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29  iIdx, regRecord)
24bbe 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
24bbf 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
24bc0 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29  G_USESEEKRESULT)
24bc1 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
24bc2 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
24bc3 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
24bc4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24bc5 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54  2(v, OP_Next, iT
24bc6 61 62 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20 20  ab, addr1+1);.  
24bc7 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
24bc8 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20  ere(v, addr1);. 
24bc9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24bca 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
24bcb 69 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  iTab);.  sqlite3
24bcc 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
24bcd 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d  _Close, iIdx);.}
24bce 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
24bcf 20 6e 65 77 20 69 6e 64 65 78 20 66 6f 72 20 61   new index for a
24bd0 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e  n SQL table.  pN
24bd1 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69 73 20 74  ame1.pName2 is t
24bd2 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69  he name of the i
24bd3 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62  ndex .** and pTb
24bd4 6c 4c 69 73 74 20 69 73 20 74 68 65 20 6e 61 6d  lList is the nam
24bd5 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
24bd6 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64  hat is to be ind
24bd7 65 78 65 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c  exed.  Both will
24bd8 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72   .** be NULL for
24bd9 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f   a primary key o
24bda 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 20  r an index that 
24bdb 69 73 20 63 72 65 61 74 65 64 20 74 6f 20 73 61  is created to sa
24bdc 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55  tisfy a.** UNIQU
24bdd 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49  E constraint.  I
24bde 66 20 70 54 61 62 6c 65 20 61 6e 64 20 70 49 6e  f pTable and pIn
24bdf 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73  dex are NULL, us
24be0 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e pParse->pNewTa
24be1 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65 20 74 61  ble.** as the ta
24be2 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65  ble to be indexe
24be3 64 2e 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  d.  pParse->pNew
24be4 54 61 62 6c 65 20 69 73 20 61 20 74 61 62 6c 65  Table is a table
24be5 20 74 68 61 74 20 69 73 0a 2a 2a 20 63 75 72 72   that is.** curr
24be6 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73  ently being cons
24be7 74 72 75 63 74 65 64 20 62 79 20 61 20 43 52 45  tructed by a CRE
24be8 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
24be9 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74  ent..**.** pList
24bea 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f   is a list of co
24beb 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65  lumns to be inde
24bec 78 65 64 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c  xed.  pList will
24bed 20 62 65 20 4e 55 4c 4c 20 69 66 20 74 68 69 73   be NULL if this
24bee 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d 61 72 79  .** is a primary
24bef 20 6b 65 79 20 6f 72 20 75 6e 69 71 75 65 2d 63   key or unique-c
24bf0 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65  onstraint on the
24bf1 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 6f 6c   most recent col
24bf2 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20  umn added.** to 
24bf3 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
24bf4 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
24bf5 75 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20  uction.  .**.** 
24bf6 49 66 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  If the index is 
24bf7 63 72 65 61 74 65 64 20 73 75 63 63 65 73 73 66  created successf
24bf8 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20 61 20 70  ully, return a p
24bf9 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
24bfa 77 20 49 6e 64 65 78 0a 2a 2a 20 73 74 72 75 63  w Index.** struc
24bfb 74 75 72 65 2e 20 54 68 69 73 20 69 73 20 75 73  ture. This is us
24bfc 65 64 20 62 79 20 73 71 6c 69 74 65 33 41 64 64  ed by sqlite3Add
24bfd 50 72 69 6d 61 72 79 4b 65 79 28 29 20 74 6f 20  PrimaryKey() to 
24bfe 6d 61 72 6b 20 74 68 65 20 69 6e 64 65 78 0a 2a  mark the index.*
24bff 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 73 20  * as the tables 
24c00 70 72 69 6d 61 72 79 20 6b 65 79 20 28 49 6e 64  primary key (Ind
24c01 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d 3d 32 29  ex.autoIndex==2)
24c02 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24c03 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74  ATE Index *sqlit
24c04 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 0a 20  e3CreateIndex(. 
24c05 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24c06 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f 72      /* All infor
24c07 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 69  mation about thi
24c08 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54 6f 6b  s parse */.  Tok
24c09 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20  en *pName1,     
24c0a 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20 6f 66  /* First part of
24c0b 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79   index name. May
24c0c 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f   be NULL */.  To
24c0d 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
24c0e 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20   /* Second part 
24c0f 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d  of index name. M
24c10 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
24c11 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e 61 6d  SrcList *pTblNam
24c12 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 69  e, /* Table to i
24c13 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72 73 65  ndex. Use pParse
24c14 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66 20 30  ->pNewTable if 0
24c15 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
24c16 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20 6c 69  pList,   /* A li
24c17 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  st of columns to
24c18 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20   be indexed */. 
24c19 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20   int onError,   
24c1a 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c      /* OE_Abort,
24c1b 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52   OE_Ignore, OE_R
24c1c 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f  eplace, or OE_No
24c1d 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  ne */.  Token *p
24c1e 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20 54 68  Start,     /* Th
24c1f 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20 74  e CREATE token t
24c20 68 61 74 20 62 65 67 69 6e 73 20 74 68 69 73 20  hat begins this 
24c21 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54  statement */.  T
24c22 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20  oken *pEnd,     
24c23 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74 68 61    /* The ")" tha
24c24 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43 52 45  t closes the CRE
24c25 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
24c26 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72  ent */.  int sor
24c27 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a 20 53  tOrder,     /* S
24c28 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70 72 69  ort order of pri
24c29 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20 70 4c  mary key when pL
24c2a 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20 69  ist==NULL */.  i
24c2b 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20 20 20  nt ifNotExist   
24c2c 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f 72 20    /* Omit error 
24c2d 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79  if index already
24c2e 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
24c2f 49 6e 64 65 78 20 2a 70 52 65 74 20 3d 20 30 3b  Index *pRet = 0;
24c30 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
24c31 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 54  to return */.  T
24c32 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
24c33 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
24c34 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20  be indexed */.  
24c35 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 20 3d 20  Index *pIndex = 
24c36 30 3b 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65  0;   /* The inde
24c37 78 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  x to be created 
24c38 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
24c39 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 61 6d   = 0;     /* Nam
24c3a 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a  e of the index *
24c3b 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20  /.  int nName;  
24c3c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
24c3d 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
24c3e 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 69   in zName */.  i
24c3f 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f 6b 65 6e  nt i, j;.  Token
24c40 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20 20 20 20   nullId;        
24c41 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e 20 66 6f  /* Fake token fo
24c42 72 20 61 6e 20 65 6d 70 74 79 20 49 44 20 6c 69  r an empty ID li
24c43 73 74 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20  st */.  DbFixer 
24c44 73 46 69 78 3b 20 20 20 20 20 20 20 20 2f 2a 20  sFix;        /* 
24c45 46 6f 72 20 61 73 73 69 67 6e 69 6e 67 20 64 61  For assigning da
24c46 74 61 62 61 73 65 20 6e 61 6d 65 73 20 74 6f 20  tabase names to 
24c47 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  pTable */.  int 
24c48 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 20 20  sortOrderMask;  
24c49 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f 72 20 44   /* 1 to honor D
24c4a 45 53 43 20 69 6e 20 69 6e 64 65 78 2e 20 20 30  ESC in index.  0
24c4b 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a 2f 0a 20   to ignore. */. 
24c4c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
24c4d 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20  Parse->db;.  Db 
24c4e 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  *pDb;           
24c4f 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
24c50 63 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  c table containi
24c51 6e 67 20 74 68 65 20 69 6e 64 65 78 65 64 20 64  ng the indexed d
24c52 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
24c53 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
24c54 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
24c55 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
24c56 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e  is being written
24c57 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
24c58 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a 20 55 6e  me = 0;    /* Un
24c59 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f  qualified name o
24c5a 66 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 63  f the index to c
24c5b 72 65 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63  reate */.  struc
24c5c 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
24c5d 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f 2a 20 46  *pListItem; /* F
24c5e 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
24c5f 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  pList */.  int n
24c60 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72  Col;.  int nExtr
24c61 61 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a  a = 0;.  char *z
24c62 45 78 74 72 61 3b 0a 0a 20 20 61 73 73 65 72 74  Extra;..  assert
24c63 28 20 70 53 74 61 72 74 3d 3d 30 20 7c 7c 20 70  ( pStart==0 || p
24c64 45 6e 64 21 3d 30 20 29 3b 20 2f 2a 20 70 45 6e  End!=0 ); /* pEn
24c65 64 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 4e 55  d must be non-NU
24c66 4c 4c 20 69 66 20 70 53 74 61 72 74 20 69 73 20  LL if pStart is 
24c67 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
24c68 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20  rse->nErr==0 ); 
24c69 20 20 20 20 20 2f 2a 20 4e 65 76 65 72 20 63 61       /* Never ca
24c6a 6c 6c 65 64 20 77 69 74 68 20 70 72 69 6f 72 20  lled with prior 
24c6b 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 66 28 20  errors */.  if( 
24c6c 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24c6d 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
24c6e 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TAB ){.    goto 
24c6f 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24c70 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c  x;.  }.  if( SQL
24c71 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
24c72 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
24c73 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  ) ){.    goto ex
24c74 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24c75 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
24c76 46 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 74  Find the table t
24c77 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 64  hat is to be ind
24c78 65 78 65 64 2e 20 20 52 65 74 75 72 6e 20 65 61  exed.  Return ea
24c79 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  rly if not found
24c7a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 62  ..  */.  if( pTb
24c7b 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20  lName!=0 ){..   
24c7c 20 2f 2a 20 55 73 65 20 74 68 65 20 74 77 6f 2d   /* Use the two-
24c7d 70 61 72 74 20 69 6e 64 65 78 20 6e 61 6d 65 20  part index name 
24c7e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
24c7f 20 64 61 74 61 62 61 73 65 20 0a 20 20 20 20 2a   database .    *
24c80 2a 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20  * to search for 
24c81 74 68 65 20 74 61 62 6c 65 2e 20 27 46 69 78 27  the table. 'Fix'
24c82 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
24c83 74 6f 20 74 68 69 73 20 64 62 0a 20 20 20 20 2a  to this db.    *
24c84 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67  * before looking
24c85 20 75 70 20 74 68 65 20 74 61 62 6c 65 2e 0a 20   up the table.. 
24c86 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
24c87 28 20 70 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d  ( pName1 && pNam
24c88 65 32 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20  e2 );.    iDb = 
24c89 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
24c8a 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
24c8b 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d  1, pName2, &pNam
24c8c 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c  e);.    if( iDb<
24c8d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  0 ) goto exit_cr
24c8e 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 23 69 66  eate_index;..#if
24c8f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24c90 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49  _TEMPDB.    /* I
24c91 66 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  f the index name
24c92 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64   was unqualified
24c93 2c 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 74  , check if the t
24c94 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  he table.    ** 
24c95 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2e  is a temp table.
24c96 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20   If so, set the 
24c97 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e 20 44  database to 1. D
24c98 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a 20 20  o not do this.  
24c99 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 6c 69    ** if initiali
24c9a 73 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20  sing a database 
24c9b 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20  schema..    */. 
24c9c 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
24c9d 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 70  .busy ){.      p
24c9e 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
24c9f 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
24ca0 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20  e, pTblName);.  
24ca1 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e      if( pName2->
24ca2 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26 20  n==0 && pTab && 
24ca3 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  pTab->pSchema==d
24ca4 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
24ca5 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 44 62  a ){.        iDb
24ca6 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
24ca7 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
24ca8 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
24ca9 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
24caa 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20  , iDb, "index", 
24cab 70 4e 61 6d 65 29 20 26 26 0a 20 20 20 20 20 20  pName) &&.      
24cac 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c    sqlite3FixSrcL
24cad 69 73 74 28 26 73 46 69 78 2c 20 70 54 62 6c 4e  ist(&sFix, pTblN
24cae 61 6d 65 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  ame).    ){.    
24caf 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74 68 65    /* Because the
24cb0 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72 75 63   parser construc
24cb1 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d  ts pTblName from
24cb2 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e 74 69   a single identi
24cb3 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73  fier,.      ** s
24cb4 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
24cb5 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
24cb6 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
24cb7 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  (0);.    }.    p
24cb8 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
24cb9 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
24cba 20 30 2c 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b   0, pTblName->a[
24cbb 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20  0].zName, .     
24cbc 20 20 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30     pTblName->a[0
24cbd 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ].zDatabase);.  
24cbe 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20 64    if( !pTab || d
24cbf 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24cc0 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
24cc1 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61 73  te_index;.    as
24cc2 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
24cc3 62 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  b].pSchema==pTab
24cc4 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d  ->pSchema );.  }
24cc5 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
24cc6 28 20 70 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20  ( pName==0 );.  
24cc7 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d    pTab = pParse-
24cc8 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20 20  >pNewTable;.    
24cc9 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f  if( !pTab ) goto
24cca 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24ccb 65 78 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71  ex;.    iDb = sq
24ccc 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
24ccd 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
24cce 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62  hema);.  }.  pDb
24ccf 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
24cd0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  ;..  assert( pTa
24cd1 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
24cd2 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
24cd3 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  0 );.  if( sqlit
24cd4 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d  e3StrNICmp(pTab-
24cd5 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  >zName, "sqlite_
24cd6 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 20 20  ", 7)==0 .      
24cd7 20 26 26 20 6d 65 6d 63 6d 70 28 26 70 54 61 62   && memcmp(&pTab
24cd8 2d 3e 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74 65  ->zName[7],"alte
24cd9 72 74 61 62 5f 22 2c 39 29 21 3d 30 20 29 7b 0a  rtab_",9)!=0 ){.
24cda 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24cdb 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
24cdc 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  le %s may not be
24cdd 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d   indexed", pTab-
24cde 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
24cdf 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24ce0 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  dex;.  }.#ifndef
24ce1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
24ce2 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  W.  if( pTab->pS
24ce3 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
24ce4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24ce5 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20  rse, "views may 
24ce6 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29  not be indexed")
24ce7 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
24ce8 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
24ce9 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
24cea 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
24ceb 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
24cec 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
24ced 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
24cee 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24cef 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  virtual tables m
24cf0 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65  ay not be indexe
24cf1 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d");.    goto ex
24cf2 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24cf3 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
24cf4 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
24cf5 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
24cf6 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  x.  Make sure th
24cf7 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  ere is not alrea
24cf8 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20  dy another.  ** 
24cf9 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77  index or table w
24cfa 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ith the same nam
24cfb 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  e.  .  **.  ** E
24cfc 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65  xception:  If we
24cfd 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65   are reading the
24cfe 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e   names of perman
24cff 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d  ent indices from
24d00 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
24d01 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62  _master table (b
24d02 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65  ecause some othe
24d03 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65  r process change
24d04 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e  d the schema) an
24d05 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  d.  ** one of th
24d06 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f  e index names co
24d07 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20  llides with the 
24d08 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72  name of a tempor
24d09 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a  ary table or.  *
24d0a 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65  * index, then we
24d0b 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
24d0c 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69  o process this i
24d0d 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ndex..  **.  ** 
24d0e 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d  If pName==0 it m
24d0f 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65  eans that we are
24d10 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69  .  ** dealing wi
24d11 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  th a primary key
24d12 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
24d13 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20  raint.  We have 
24d14 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20  to invent our.  
24d15 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a  ** own name..  *
24d16 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b  /.  if( pName ){
24d17 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
24d18 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
24d19 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
24d1a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
24d1b 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24d1c 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 66 28  e_index;.    if(
24d1d 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
24d1e 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61  te3CheckObjectNa
24d1f 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65  me(pParse, zName
24d20 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
24d21 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24d22 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  x;.    }.    if(
24d23 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
24d24 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
24d25 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
24d26 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29  , zName, 0)!=0 )
24d27 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
24d28 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24d29 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
24d2a 61 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ady a table name
24d2b 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  d %s", zName);. 
24d2c 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
24d2d 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24d2e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
24d2f 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
24d30 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
24d31 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20   pDb->zName)!=0 
24d32 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66  ){.      if( !if
24d33 4e 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20  NotExist ){.    
24d34 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24d35 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
24d36 65 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78  ex %s already ex
24d37 69 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ists", zName);. 
24d38 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
24d39 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24d3a 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  dex;.    }.  }el
24d3b 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20  se{.    int n;. 
24d3c 20 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b     Index *pLoop;
24d3d 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70  .    for(pLoop=p
24d3e 54 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31  Tab->pIndex, n=1
24d3f 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70  ; pLoop; pLoop=p
24d40 4c 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b  Loop->pNext, n++
24d41 29 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  ){}.    zName = 
24d42 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
24d43 62 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69  b, "sqlite_autoi
24d44 6e 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61  ndex_%s_%d", pTa
24d45 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20  b->zName, n);.  
24d46 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
24d47 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
24d48 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24d49 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24d4a 43 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72  Check for author
24d4b 69 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74  ization to creat
24d4c 65 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f  e an index..  */
24d4d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24d4e 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
24d4f 4f 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ON.  {.    const
24d50 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62   char *zDb = pDb
24d51 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28  ->zName;.    if(
24d52 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
24d53 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
24d54 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f  _INSERT, SCHEMA_
24d55 54 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a  TABLE(iDb), 0, z
24d56 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
24d57 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24d58 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  dex;.    }.    i
24d59 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
24d5a 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20  _INDEX;.    if( 
24d5b 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
24d5c 69 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c  iDb==1 ) i = SQL
24d5d 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
24d5e 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73  INDEX;.    if( s
24d5f 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
24d60 70 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65  pParse, i, zName
24d61 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
24d62 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
24d63 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24d64 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  dex;.    }.  }.#
24d65 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70  endif..  /* If p
24d66 4c 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e  List==0, it mean
24d67 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
24d68 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b  as called to mak
24d69 65 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a  e a primary.  **
24d6a 20 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20   key out of the 
24d6b 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65  last column adde
24d6c 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75  d to the table u
24d6d 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
24d6e 6e 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74  n..  ** So creat
24d6f 65 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f  e a fake list to
24d70 20 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a   simulate this..
24d71 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74    */.  if( pList
24d72 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49  ==0 ){.    nullI
24d73 64 2e 7a 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  d.z = pTab->aCol
24d74 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e 7a  [pTab->nCol-1].z
24d75 4e 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49 64  Name;.    nullId
24d76 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  .n = sqlite3Strl
24d77 65 6e 33 30 28 28 63 68 61 72 2a 29 6e 75 6c 6c  en30((char*)null
24d78 49 64 2e 7a 29 3b 0a 20 20 20 20 70 4c 69 73 74  Id.z);.    pList
24d79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
24d7a 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
24d7b 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
24d7c 70 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20  pList==0 ) goto 
24d7d 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24d7e 78 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  x;.    sqlite3Ex
24d7f 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50  prListSetName(pP
24d80 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26 6e 75  arse, pList, &nu
24d81 6c 6c 49 64 2c 20 30 29 3b 0a 20 20 20 20 70 4c  llId, 0);.    pL
24d82 69 73 74 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72  ist->a[0].sortOr
24d83 64 65 72 20 3d 20 28 75 38 29 73 6f 72 74 4f 72  der = (u8)sortOr
24d84 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  der;.  }..  /* F
24d85 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
24d86 6e 79 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  ny bytes of spac
24d87 65 20 61 72 65 20 72 65 71 75 69 72 65 64 20 74  e are required t
24d88 6f 20 73 74 6f 72 65 20 65 78 70 6c 69 63 69 74  o store explicit
24d89 6c 79 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65  ly.  ** specifie
24d8a 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
24d8b 65 6e 63 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f  ence names..  */
24d8c 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c  .  for(i=0; i<pL
24d8d 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
24d8e 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  {.    Expr *pExp
24d8f 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  r = pList->a[i].
24d90 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70  pExpr;.    if( p
24d91 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 43 6f  Expr ){.      Co
24d92 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70  llSeq *pColl = p
24d93 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Expr->pColl;.   
24d94 20 20 20 2f 2a 20 45 69 74 68 65 72 20 70 43 6f     /* Either pCo
24d95 6c 6c 21 3d 30 20 6f 72 20 74 68 65 72 65 20 77  ll!=0 or there w
24d96 61 73 20 61 6e 20 4f 4f 4d 20 66 61 69 6c 75 72  as an OOM failur
24d97 65 2e 20 20 42 75 74 20 69 66 20 61 6e 20 4f 4f  e.  But if an OO
24d98 4d 0a 20 20 20 20 20 20 2a 2a 20 66 61 69 6c 75  M.      ** failu
24d99 72 65 20 77 65 20 68 61 76 65 20 71 75 69 74 20  re we have quit 
24d9a 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20  before reaching 
24d9b 74 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20  this point. */. 
24d9c 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
24d9d 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20  pColl) ){.      
24d9e 20 20 6e 45 78 74 72 61 20 2b 3d 20 28 31 20 2b    nExtra += (1 +
24d9f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
24da0 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29 3b  (pColl->zName));
24da1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24da2 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41   }..  /* .  ** A
24da3 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65  llocate the inde
24da4 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a 20 20  x structure. .  
24da5 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  */.  nName = sql
24da6 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
24da7 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c  me);.  nCol = pL
24da8 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49  ist->nExpr;.  pI
24da9 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62  ndex = sqlite3Db
24daa 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a  MallocZero(db, .
24dab 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e 64        sizeof(Ind
24dac 65 78 29 20 2b 20 20 20 20 20 20 20 20 20 20 20  ex) +           
24dad 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75     /* Index stru
24dae 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20 20  cture  */.      
24daf 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c  sizeof(int)*nCol
24db0 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   +           /* 
24db1 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20  Index.aiColumn  
24db2 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66   */.      sizeof
24db3 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b  (int)*(nCol+1) +
24db4 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
24db5 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20  aiRowEst   */.  
24db6 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 20      sizeof(char 
24db7 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20  *)*nCol +       
24db8 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c   /* Index.azColl
24db9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69       */.      si
24dba 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20  zeof(u8)*nCol + 
24dbb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
24dbc 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a  dex.aSortOrder *
24dbd 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20  /.      nName + 
24dbe 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  1 +             
24dbf 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e       /* Index.zN
24dc0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ame      */.    
24dc1 20 20 6e 45 78 74 72 61 20 20 20 20 20 20 20 20    nExtra        
24dc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24dc3 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  * Collation sequ
24dc4 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20  ence names */.  
24dc5 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
24dc6 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
24dc7 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24dc8 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70  e_index;.  }.  p
24dc9 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20  Index->azColl = 
24dca 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65 78  (char**)(&pIndex
24dcb 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  [1]);.  pIndex->
24dcc 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20  aiColumn = (int 
24dcd 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f  *)(&pIndex->azCo
24dce 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e  ll[nCol]);.  pIn
24dcf 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20  dex->aiRowEst = 
24dd0 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70 49  (unsigned *)(&pI
24dd1 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  ndex->aiColumn[n
24dd2 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d  Col]);.  pIndex-
24dd3 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  >aSortOrder = (u
24dd4 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69  8 *)(&pIndex->ai
24dd5 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b  RowEst[nCol+1]);
24dd6 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  .  pIndex->zName
24dd7 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e   = (char *)(&pIn
24dd8 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  dex->aSortOrder[
24dd9 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61  nCol]);.  zExtra
24dda 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49 6e   = (char *)(&pIn
24ddb 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65  dex->zName[nName
24ddc 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  +1]);.  memcpy(p
24ddd 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  Index->zName, zN
24dde 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20  ame, nName+1);. 
24ddf 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20   pIndex->pTable 
24de0 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65 78  = pTab;.  pIndex
24de1 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73  ->nColumn = pLis
24de2 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64  t->nExpr;.  pInd
24de3 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 28 75  ex->onError = (u
24de4 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e  8)onError;.  pIn
24de5 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d  dex->autoIndex =
24de6 20 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30 29 3b   (u8)(pName==0);
24de7 0a 20 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65  .  pIndex->pSche
24de8 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
24de9 5d 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a  ].pSchema;..  /*
24dea 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
24deb 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72   we should honor
24dec 20 44 45 53 43 20 72 65 71 75 65 73 74 73 20 6f   DESC requests o
24ded 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a  n index columns.
24dee 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e    */.  if( pDb->
24def 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
24df0 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73  rmat>=4 ){.    s
24df1 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d  ortOrderMask = -
24df2 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45  1;   /* Honor DE
24df3 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20  SC */.  }else{. 
24df4 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b     sortOrderMask
24df5 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f   = 0;    /* Igno
24df6 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a  re DESC */.  }..
24df7 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 6e 61    /* Scan the na
24df8 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  mes of the colum
24df9 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ns of the table 
24dfa 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 6e  to be indexed an
24dfb 64 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20  d.  ** load the 
24dfc 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 20 69  column indices i
24dfd 6e 74 6f 20 74 68 65 20 49 6e 64 65 78 20 73 74  nto the Index st
24dfe 72 75 63 74 75 72 65 2e 20 20 52 65 70 6f 72 74  ructure.  Report
24dff 20 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69   an error.  ** i
24e00 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20  f any column is 
24e01 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2a 0a  not found..  **.
24e02 20 20 2a 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20    ** TODO:  Add 
24e03 61 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73  a test to make s
24e04 75 72 65 20 74 68 61 74 20 74 68 65 20 73 61 6d  ure that the sam
24e05 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  e column is not 
24e06 6e 61 6d 65 64 0a 20 20 2a 2a 20 6d 6f 72 65 20  named.  ** more 
24e07 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e  than once within
24e08 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 2e   the same index.
24e09 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74    Only the first
24e0a 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 2a   instance of.  *
24e0b 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c  * the column wil
24e0c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64 20 62  l ever be used b
24e0d 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e  y the optimizer.
24e0e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73 69 6e    Note that usin
24e0f 67 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20  g the.  ** same 
24e10 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20 74 68 61 6e  column more than
24e11 20 6f 6e 63 65 20 63 61 6e 6e 6f 74 20 62 65 20   once cannot be 
24e12 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75 73 65  an error because
24e13 20 74 68 61 74 20 77 6f 75 6c 64 20 0a 20 20 2a   that would .  *
24e14 2a 20 62 72 65 61 6b 20 62 61 63 6b 77 61 72 64  * break backward
24e15 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  s compatibility 
24e16 2d 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  - it needs to be
24e17 20 61 20 77 61 72 6e 69 6e 67 2e 0a 20 20 2a 2f   a warning..  */
24e18 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73  .  for(i=0, pLis
24e19 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  tItem=pList->a; 
24e1a 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
24e1b 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b  i++, pListItem++
24e1c 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
24e1d 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c  r *zColName = pL
24e1e 69 73 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a  istItem->zName;.
24e1f 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62      Column *pTab
24e20 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65 71  Col;.    int req
24e21 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b  uestedSortOrder;
24e22 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  .    char *zColl
24e23 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
24e24 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e      /* Collation
24e25 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a   sequence name *
24e26 2f 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20  /..    for(j=0, 
24e27 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43  pTabCol=pTab->aC
24e28 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c  ol; j<pTab->nCol
24e29 3b 20 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b  ; j++, pTabCol++
24e2a 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
24e2b 69 74 65 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c  ite3StrICmp(zCol
24e2c 4e 61 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a  Name, pTabCol->z
24e2d 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
24e2e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
24e2f 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  j>=pTab->nCol ){
24e30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
24e31 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24e32 74 61 62 6c 65 20 25 73 20 68 61 73 20 6e 6f 20  table %s has no 
24e33 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22  column named %s"
24e34 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e  ,.        pTab->
24e35 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29  zName, zColName)
24e36 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  ;.      goto exi
24e37 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24e38 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64 65 78      }.    pIndex
24e39 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20  ->aiColumn[i] = 
24e3a 6a 3b 0a 20 20 20 20 2f 2a 20 4a 75 73 74 69 66  j;.    /* Justif
24e3b 69 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 41  ication of the A
24e3c 4c 57 41 59 53 28 70 4c 69 73 74 49 74 65 6d 2d  LWAYS(pListItem-
24e3d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 3a 20  >pExpr->pColl): 
24e3e 20 42 65 63 61 75 73 65 20 6f 66 0a 20 20 20 20   Because of.    
24e3f 2a 2a 20 74 68 65 20 77 61 79 20 74 68 65 20 22  ** the way the "
24e40 69 64 78 6c 69 73 74 22 20 6e 6f 6e 2d 74 65 72  idxlist" non-ter
24e41 6d 69 6e 61 6c 20 69 73 20 63 6f 6e 73 74 72 75  minal is constru
24e42 63 74 65 64 20 62 79 20 74 68 65 20 70 61 72 73  cted by the pars
24e43 65 72 2c 0a 20 20 20 20 2a 2a 20 69 66 20 70 4c  er,.    ** if pL
24e44 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 69  istItem->pExpr i
24e45 73 20 6e 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20  s not null then 
24e46 65 69 74 68 65 72 20 70 4c 69 73 74 49 74 65 6d  either pListItem
24e47 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 0a 20  ->pExpr->pColl. 
24e48 20 20 20 2a 2a 20 6d 75 73 74 20 65 78 69 73 74     ** must exist
24e49 20 6f 72 20 65 6c 73 65 20 74 68 65 72 65 20 6d   or else there m
24e4a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 61 6e  ust have been an
24e4b 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74   OOM error.  But
24e4c 20 69 66 20 74 68 65 72 65 0a 20 20 20 20 2a 2a   if there.    **
24e4d 20 77 61 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f   was an OOM erro
24e4e 72 2c 20 77 65 20 77 6f 75 6c 64 20 6e 65 76 65  r, we would neve
24e4f 72 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69  r reach this poi
24e50 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  nt. */.    if( p
24e51 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20  ListItem->pExpr 
24e52 26 26 20 41 4c 57 41 59 53 28 70 4c 69 73 74 49  && ALWAYS(pListI
24e53 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
24e54 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  l) ){.      int 
24e55 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7a 43 6f  nColl;.      zCo
24e56 6c 6c 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e  ll = pListItem->
24e57 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e  pExpr->pColl->zN
24e58 61 6d 65 3b 0a 20 20 20 20 20 20 6e 43 6f 6c 6c  ame;.      nColl
24e59 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
24e5a 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31 3b 0a 20  30(zColl) + 1;. 
24e5b 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 45 78       assert( nEx
24e5c 74 72 61 3e 3d 6e 43 6f 6c 6c 20 29 3b 0a 20 20  tra>=nColl );.  
24e5d 20 20 20 20 6d 65 6d 63 70 79 28 7a 45 78 74 72      memcpy(zExtr
24e5e 61 2c 20 7a 43 6f 6c 6c 2c 20 6e 43 6f 6c 6c 29  a, zColl, nColl)
24e5f 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
24e60 7a 45 78 74 72 61 3b 0a 20 20 20 20 20 20 7a 45  zExtra;.      zE
24e61 78 74 72 61 20 2b 3d 20 6e 43 6f 6c 6c 3b 0a 20  xtra += nColl;. 
24e62 20 20 20 20 20 6e 45 78 74 72 61 20 2d 3d 20 6e       nExtra -= n
24e63 43 6f 6c 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Coll;.    }else{
24e64 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
24e65 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f  Tab->aCol[j].zCo
24e66 6c 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a  ll;.      if( !z
24e67 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Coll ){.        
24e68 7a 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  zColl = db->pDfl
24e69 74 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  tColl->zName;.  
24e6a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
24e6b 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
24e6c 73 79 20 26 26 20 21 73 71 6c 69 74 65 33 4c 6f  sy && !sqlite3Lo
24e6d 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  cateCollSeq(pPar
24e6e 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  se, zColl) ){.  
24e6f 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
24e70 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
24e71 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a  }.    pIndex->az
24e72 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b  Coll[i] = zColl;
24e73 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53 6f  .    requestedSo
24e74 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74 49  rtOrder = pListI
24e75 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26  tem->sortOrder &
24e76 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a   sortOrderMask;.
24e77 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72      pIndex->aSor
24e78 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75 38 29  tOrder[i] = (u8)
24e79 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64  requestedSortOrd
24e7a 65 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  er;.  }.  sqlite
24e7b 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70  3DefaultRowEst(p
24e7c 49 6e 64 65 78 29 3b 0a 0a 20 20 69 66 28 20 70  Index);..  if( p
24e7d 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65  Tab==pParse->pNe
24e7e 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a  wTable ){.    /*
24e7f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   This routine ha
24e80 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f  s been called to
24e81 20 63 72 65 61 74 65 20 61 6e 20 61 75 74 6f 6d   create an autom
24e82 61 74 69 63 20 69 6e 64 65 78 20 61 73 20 61 0a  atic index as a.
24e83 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f 66      ** result of
24e84 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f   a PRIMARY KEY o
24e85 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20  r UNIQUE clause 
24e86 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66 69  on a column defi
24e87 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a  nition, or.    *
24e88 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  * a PRIMARY KEY 
24e89 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65  or UNIQUE clause
24e8a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 63   following the c
24e8b 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
24e8c 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20 6f  s..    ** i.e. o
24e8d 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20  ne of:.    **.  
24e8e 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c    ** CREATE TABL
24e8f 45 20 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45  E t(x PRIMARY KE
24e90 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43 52  Y, y);.    ** CR
24e91 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c 20  EATE TABLE t(x, 
24e92 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29 29  y, UNIQUE(x, y))
24e93 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
24e94 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65 63  Either way, chec
24e95 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
24e96 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61  table already ha
24e97 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78 2e  s such an index.
24e98 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 64   If.    ** so, d
24e99 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72 65 61  on't bother crea
24e9a 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20 54  ting this one. T
24e9b 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73  his only applies
24e9c 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d   to.    ** autom
24e9d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64  atically created
24e9e 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72 73 20   indices. Users 
24e9f 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79 20 77  can do as they w
24ea0 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a 2a 20  ish with.    ** 
24ea1 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63 65 73  explicit indices
24ea2 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
24ea3 54 77 6f 20 55 4e 49 51 55 45 20 6f 72 20 50 52  Two UNIQUE or PR
24ea4 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72  IMARY KEY constr
24ea5 61 69 6e 74 73 20 61 72 65 20 63 6f 6e 73 69 64  aints are consid
24ea6 65 72 65 64 20 65 71 75 69 76 61 6c 65 6e 74 0a  ered equivalent.
24ea7 20 20 20 20 2a 2a 20 28 61 6e 64 20 74 68 75 73      ** (and thus
24ea8 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65   suppressing the
24ea9 20 73 65 63 6f 6e 64 20 6f 6e 65 29 20 65 76 65   second one) eve
24eaa 6e 20 69 66 20 74 68 65 79 20 68 61 76 65 20 64  n if they have d
24eab 69 66 66 65 72 65 6e 74 0a 20 20 20 20 2a 2a 20  ifferent.    ** 
24eac 73 6f 72 74 20 6f 72 64 65 72 73 2e 0a 20 20 20  sort orders..   
24ead 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
24eae 65 72 65 20 61 72 65 20 64 69 66 66 65 72 65 6e  ere are differen
24eaf 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
24eb0 65 6e 63 65 73 20 6f 72 20 69 66 20 74 68 65 20  ences or if the 
24eb1 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 20 20 2a  columns of.    *
24eb2 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  * the constraint
24eb3 20 6f 63 63 75 72 20 69 6e 20 64 69 66 66 65 72   occur in differ
24eb4 65 6e 74 20 6f 72 64 65 72 73 2c 20 74 68 65 6e  ent orders, then
24eb5 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73   the constraints
24eb6 20 61 72 65 0a 20 20 20 20 2a 2a 20 63 6f 6e 73   are.    ** cons
24eb7 69 64 65 72 65 64 20 64 69 73 74 69 6e 63 74 20  idered distinct 
24eb8 61 6e 64 20 62 6f 74 68 20 72 65 73 75 6c 74 20  and both result 
24eb9 69 6e 20 73 65 70 61 72 61 74 65 20 69 6e 64 69  in separate indi
24eba 63 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ces..    */.    
24ebb 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
24ebc 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
24ebd 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
24ebe 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
24ebf 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20  .      int k;.  
24ec0 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
24ec1 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
24ec2 6e 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ne );.      asse
24ec3 72 74 28 20 70 49 64 78 2d 3e 61 75 74 6f 49 6e  rt( pIdx->autoIn
24ec4 64 65 78 20 29 3b 0a 20 20 20 20 20 20 61 73 73  dex );.      ass
24ec5 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45  ert( pIndex->onE
24ec6 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b  rror!=OE_None );
24ec7 0a 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ..      if( pIdx
24ec8 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 49 6e 64 65  ->nColumn!=pInde
24ec9 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e  x->nColumn ) con
24eca 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66 6f 72  tinue;.      for
24ecb 28 6b 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 43  (k=0; k<pIdx->nC
24ecc 6f 6c 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20 20 20  olumn; k++){.   
24ecd 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
24ece 2a 7a 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  *z1;.        con
24ecf 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 20  st char *z2;.   
24ed0 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
24ed1 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64  iColumn[k]!=pInd
24ed2 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20  ex->aiColumn[k] 
24ed3 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
24ed4 20 7a 31 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f   z1 = pIdx->azCo
24ed5 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 7a  ll[k];.        z
24ed6 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f  2 = pIndex->azCo
24ed7 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[k];.        i
24ed8 66 28 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c  f( z1!=z2 && sql
24ed9 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20  ite3StrICmp(z1, 
24eda 7a 32 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  z2) ) break;.   
24edb 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b     }.      if( k
24edc 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  ==pIdx->nColumn 
24edd 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
24ede 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49  Idx->onError!=pI
24edf 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b  ndex->onError ){
24ee0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
24ee1 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72  is constraint cr
24ee2 65 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 69  eates the same i
24ee3 6e 64 65 78 20 61 73 20 61 20 70 72 65 76 69 6f  ndex as a previo
24ee4 75 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  us.          ** 
24ee5 63 6f 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69  constraint speci
24ee6 66 69 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69  fied somewhere i
24ee7 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
24ee8 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  LE statement..  
24ee9 20 20 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76          ** Howev
24eea 65 72 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  er the ON CONFLI
24eeb 43 54 20 63 6c 61 75 73 65 73 20 61 72 65 20 64  CT clauses are d
24eec 69 66 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74  ifferent. If bot
24eed 68 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20  h this .        
24eee 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20    ** constraint 
24eef 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
24ef0 20 65 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73   equivalent cons
24ef1 74 72 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c  traint have expl
24ef2 69 63 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a  icit.          *
24ef3 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  * ON CONFLICT cl
24ef4 61 75 73 65 73 20 74 68 69 73 20 69 73 20 61 6e  auses this is an
24ef5 20 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73   error. Otherwis
24ef6 65 2c 20 75 73 65 20 74 68 65 0a 20 20 20 20 20  e, use the.     
24ef7 20 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74       ** explicit
24ef8 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 65 68  ly specified beh
24ef9 61 76 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69  aviour for the i
24efa 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20  ndex..          
24efb 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
24efc 20 21 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72   !(pIdx->onError
24efd 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20  ==OE_Default || 
24efe 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d  pIndex->onError=
24eff 3d 4f 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a  =OE_Default) ){.
24f00 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
24f01 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
24f02 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
24f03 20 20 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e       "conflictin
24f04 67 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  g ON CONFLICT cl
24f05 61 75 73 65 73 20 73 70 65 63 69 66 69 65 64 22  auses specified"
24f06 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
24f07 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  }.          if( 
24f08 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  pIdx->onError==O
24f09 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
24f0a 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f           pIdx->o
24f0b 6e 45 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d  nError = pIndex-
24f0c 3e 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20  >onError;.      
24f0d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
24f0e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69          goto exi
24f0f 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24f10 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24f11 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65  }..  /* Link the
24f12 20 6e 65 77 20 49 6e 64 65 78 20 73 74 72 75 63   new Index struc
24f13 74 75 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c  ture to its tabl
24f14 65 20 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68  e and to the oth
24f15 65 72 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72  er.  ** in-memor
24f16 79 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63  y database struc
24f17 74 75 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69  tures. .  */.  i
24f18 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
24f19 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
24f1a 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
24f1b 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e  3HashInsert(&pIn
24f1c 64 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64  dex->pSchema->id
24f1d 78 48 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20  xHash, .        
24f1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f1f 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c    pIndex->zName,
24f20 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
24f21 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c  (pIndex->zName),
24f22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24f23 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64 65             pInde
24f24 78 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  x);.    if( p ){
24f25 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24f26 3d 3d 70 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20  ==pIndex );  /* 
24f27 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65  Malloc must have
24f28 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20   failed */.     
24f29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
24f2a 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f 74  d = 1;.      got
24f2b 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
24f2c 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  dex;.    }.    d
24f2d 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
24f2e 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
24f2f 3b 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61  ;.    if( pTblNa
24f30 6d 65 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  me!=0 ){.      p
24f31 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62  Index->tnum = db
24f32 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a  ->init.newTnum;.
24f33 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24f34 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e  If the db->init.
24f35 62 75 73 79 20 69 73 20 30 20 74 68 65 6e 20 63  busy is 0 then c
24f36 72 65 61 74 65 20 74 68 65 20 69 6e 64 65 78 20  reate the index 
24f37 6f 6e 20 64 69 73 6b 2e 20 20 54 68 69 73 0a 20  on disk.  This. 
24f38 20 2a 2a 20 69 6e 76 6f 6c 76 65 73 20 77 72 69   ** involves wri
24f39 74 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 69  ting the index i
24f3a 6e 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 74  nto the master t
24f3b 61 62 6c 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67  able and filling
24f3c 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64   in the.  ** ind
24f3d 65 78 20 77 69 74 68 20 74 68 65 20 63 75 72 72  ex with the curr
24f3e 65 6e 74 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e  ent table conten
24f3f 74 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ts..  **.  ** Th
24f40 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  e db->init.busy 
24f41 69 73 20 30 20 77 68 65 6e 20 74 68 65 20 75 73  is 0 when the us
24f42 65 72 20 66 69 72 73 74 20 65 6e 74 65 72 73 20  er first enters 
24f43 61 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 0a  a CREATE INDEX .
24f44 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64    ** command.  d
24f45 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
24f46 31 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73  1 when a databas
24f47 65 20 69 73 20 6f 70 65 6e 65 64 20 61 6e 64 20  e is opened and 
24f48 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 49 4e 44  .  ** CREATE IND
24f49 45 58 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  EX statements ar
24f4a 65 20 72 65 61 64 20 6f 75 74 20 6f 66 20 74 68  e read out of th
24f4b 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e master table. 
24f4c 20 49 6e 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74   In.  ** the lat
24f4d 74 65 72 20 63 61 73 65 20 74 68 65 20 69 6e 64  ter case the ind
24f4e 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ex already exist
24f4f 73 20 6f 6e 20 64 69 73 6b 2c 20 77 68 69 63 68  s on disk, which
24f50 20 69 73 20 77 68 79 0a 20 20 2a 2a 20 77 65 20   is why.  ** we 
24f51 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 72 65  don't want to re
24f52 63 72 65 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a  create it..  **.
24f53 20 20 2a 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65    ** If pTblName
24f54 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68 69  ==0 it means thi
24f55 73 20 69 6e 64 65 78 20 69 73 20 67 65 6e 65 72  s index is gener
24f56 61 74 65 64 20 61 73 20 61 20 70 72 69 6d 61 72  ated as a primar
24f57 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e  y key.  ** or UN
24f58 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20  IQUE constraint 
24f59 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  of a CREATE TABL
24f5a 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 69  E statement.  Si
24f5b 6e 63 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20  nce the table.  
24f5c 2a 2a 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ** has just been
24f5d 20 63 72 65 61 74 65 64 2c 20 69 74 20 63 6f 6e   created, it con
24f5e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e  tains no data an
24f5f 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 69 74  d the index init
24f60 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20  ialization.  ** 
24f61 73 74 65 70 20 63 61 6e 20 62 65 20 73 6b 69 70  step can be skip
24f62 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65  ped..  */.  else
24f63 7b 20 2f 2a 20 69 66 28 20 64 62 2d 3e 69 6e 69  { /* if( db->ini
24f64 74 2e 62 75 73 79 3d 3d 30 20 29 20 2a 2f 0a 20  t.busy==0 ) */. 
24f65 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
24f66 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20  char *zStmt;.   
24f67 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50   int iMem = ++pP
24f68 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20  arse->nMem;..   
24f69 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
24f6a 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
24f6b 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f   if( v==0 ) goto
24f6c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24f6d 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65  ex;...    /* Cre
24f6e 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65  ate the rootpage
24f6f 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20   for the index. 
24f70 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
24f71 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
24f72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
24f73 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
24f74 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24f75 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69  P_CreateIndex, i
24f76 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20  Db, iMem);..    
24f77 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20 63 6f  /* Gather the co
24f78 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
24f79 68 65 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  he CREATE INDEX 
24f7a 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20  statement into. 
24f7b 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20     ** the zStmt 
24f7c 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a  variable.    */.
24f7d 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20 29      if( pStart )
24f7e 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
24f7f 70 45 6e 64 21 3d 30 20 29 3b 0a 20 20 20 20 20  pEnd!=0 );.     
24f80 20 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64 65   /* A named inde
24f81 78 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69 63  x with an explic
24f82 69 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  it CREATE INDEX 
24f83 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20  statement */.   
24f84 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74     zStmt = sqlit
24f85 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 43  e3MPrintf(db, "C
24f86 52 45 41 54 45 25 73 20 49 4e 44 45 58 20 25 2e  REATE%s INDEX %.
24f87 2a 73 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45  *s",.        onE
24f88 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20  rror==OE_None ? 
24f89 22 22 20 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a  "" : " UNIQUE",.
24f8a 20 20 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20          pEnd->z 
24f8b 2d 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a  - pName->z + 1,.
24f8c 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a          pName->z
24f8d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
24f8e 20 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61      /* An automa
24f8f 74 69 63 20 69 6e 64 65 78 20 63 72 65 61 74 65  tic index create
24f90 64 20 62 79 20 61 20 50 52 49 4d 41 52 59 20 4b  d by a PRIMARY K
24f91 45 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e  EY or UNIQUE con
24f92 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20  straint */.     
24f93 20 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69   /* zStmt = sqli
24f94 74 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b 20  te3MPrintf(""); 
24f95 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  */.      zStmt =
24f96 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
24f97 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20 69  * Add an entry i
24f98 6e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  n sqlite_master 
24f99 66 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a 20  for this index. 
24f9a 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
24f9b 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
24f9c 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 49  rse, .        "I
24f9d 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73  NSERT INTO %Q.%s
24f9e 20 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27 2c   VALUES('index',
24f9f 25 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c  %Q,%Q,#%d,%Q);",
24fa0 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  .        db->aDb
24fa1 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48  [iDb].zName, SCH
24fa2 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a  EMA_TABLE(iDb),.
24fa3 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e          pIndex->
24fa4 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70  zName,.        p
24fa5 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  Tab->zName,.    
24fa6 20 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20      iMem,.      
24fa7 20 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20    zStmt.    );. 
24fa8 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24fa9 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20  (db, zStmt);..  
24faa 20 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e    /* Fill the in
24fab 64 65 78 20 77 69 74 68 20 64 61 74 61 20 61 6e  dex with data an
24fac 64 20 72 65 70 61 72 73 65 20 74 68 65 20 73 63  d reparse the sc
24fad 68 65 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50  hema. Code an OP
24fae 5f 45 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74  _Expire.    ** t
24faf 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  o invalidate all
24fb0 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74   pre-compiled st
24fb1 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f  atements..    */
24fb2 0a 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d  .    if( pTblNam
24fb3 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
24fb4 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50  e3RefillIndex(pP
24fb5 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d  arse, pIndex, iM
24fb6 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  em);.      sqlit
24fb7 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70  e3ChangeCookie(p
24fb8 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
24fb9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24fba 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
24fbb 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20  Schema, iDb, 0, 
24fbc 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  0,.         sqli
24fbd 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
24fbe 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64  name='%q'", pInd
24fbf 65 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44  ex->zName), P4_D
24fc0 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73  YNAMIC);.      s
24fc1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
24fc2 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30  (v, OP_Expire, 0
24fc3 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
24fc4 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61  /* When adding a
24fc5 6e 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 6c  n index to the l
24fc6 69 73 74 20 6f 66 20 69 6e 64 69 63 65 73 20 66  ist of indices f
24fc7 6f 72 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65  or a table, make
24fc8 0a 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69  .  ** sure all i
24fc9 6e 64 69 63 65 73 20 6c 61 62 65 6c 65 64 20 4f  ndices labeled O
24fca 45 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65 20 61  E_Replace come a
24fcb 66 74 65 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c  fter all those l
24fcc 61 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49  abeled.  ** OE_I
24fcd 67 6e 6f 72 65 2e 20 20 54 68 69 73 20 69 73 20  gnore.  This is 
24fce 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 74 68  necessary for th
24fcf 65 20 63 6f 72 72 65 63 74 20 63 6f 6e 73 74 72  e correct constr
24fd0 61 69 6e 74 20 63 68 65 63 6b 0a 20 20 2a 2a 20  aint check.  ** 
24fd1 70 72 6f 63 65 73 73 69 6e 67 20 28 69 6e 20 73  processing (in s
24fd2 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
24fd3 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 29  nstraintChecks()
24fd4 29 20 61 73 20 70 61 72 74 20 6f 66 0a 20 20 2a  ) as part of.  *
24fd5 2a 20 55 50 44 41 54 45 20 61 6e 64 20 49 4e 53  * UPDATE and INS
24fd6 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  ERT statements. 
24fd7 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d   .  */.  if( db-
24fd8 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 70 54  >init.busy || pT
24fd9 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  blName==0 ){.   
24fda 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45   if( onError!=OE
24fdb 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 61 62  _Replace || pTab
24fdc 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20 20 20  ->pIndex==0.    
24fdd 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e 70 49       || pTab->pI
24fde 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  ndex->onError==O
24fdf 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20 20 20  E_Replace){.    
24fe0 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20    pIndex->pNext 
24fe1 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a  = pTab->pIndex;.
24fe2 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49 6e 64        pTab->pInd
24fe3 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20  ex = pIndex;.   
24fe4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e   }else{.      In
24fe5 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20 70 54  dex *pOther = pT
24fe6 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20  ab->pIndex;.    
24fe7 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65 72 2d    while( pOther-
24fe8 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68 65 72  >pNext && pOther
24fe9 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f 72  ->pNext->onError
24fea 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a  !=OE_Replace ){.
24feb 20 20 20 20 20 20 20 20 70 4f 74 68 65 72 20 3d          pOther =
24fec 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a   pOther->pNext;.
24fed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49        }.      pI
24fee 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 4f  ndex->pNext = pO
24fef 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ther->pNext;.   
24ff0 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74     pOther->pNext
24ff1 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d   = pIndex;.    }
24ff2 0a 20 20 20 20 70 52 65 74 20 3d 20 70 49 6e 64  .    pRet = pInd
24ff3 65 78 3b 0a 20 20 20 20 70 49 6e 64 65 78 20 3d  ex;.    pIndex =
24ff4 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c   0;.  }..  /* Cl
24ff5 65 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78  ean up before ex
24ff6 69 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72  iting */.exit_cr
24ff7 65 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66  eate_index:.  if
24ff8 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
24ff9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e  sqlite3_free(pIn
24ffa 64 65 78 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20  dex->zColAff);. 
24ffb 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24ffc 28 64 62 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20  (db, pIndex);.  
24ffd 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  }.  sqlite3ExprL
24ffe 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
24fff 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
25000 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
25001 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71   pTblName);.  sq
25002 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
25003 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e  zName);.  return
25004 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pRet;.}../*.** 
25005 46 69 6c 6c 20 74 68 65 20 49 6e 64 65 78 2e 61  Fill the Index.a
25006 69 52 6f 77 45 73 74 5b 5d 20 61 72 72 61 79 20  iRowEst[] array 
25007 77 69 74 68 20 64 65 66 61 75 6c 74 20 69 6e 66  with default inf
25008 6f 72 6d 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72  ormation - infor
25009 6d 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20  mation.** to be 
2500a 75 73 65 64 20 77 68 65 6e 20 77 65 20 68 61 76  used when we hav
2500b 65 20 6e 6f 74 20 72 75 6e 20 74 68 65 20 41 4e  e not run the AN
2500c 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
2500d 2a 0a 2a 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d  *.** aiRowEst[0]
2500e 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 20 63   is suppose to c
2500f 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65  ontain the numbe
25010 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
25011 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53   the index..** S
25012 69 6e 63 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b  ince we do not k
25013 6e 6f 77 2c 20 67 75 65 73 73 20 31 20 6d 69 6c  now, guess 1 mil
25014 6c 69 6f 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b  lion.  aiRowEst[
25015 31 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74  1] is an estimat
25016 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  e of the.** numb
25017 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
25018 65 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  e table that mat
25019 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61  ch any particula
2501a 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a  r value of the.*
2501b 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  * first column o
2501c 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 61 69  f the index.  ai
2501d 52 6f 77 45 73 74 5b 32 5d 20 69 73 20 61 6e 20  RowEst[2] is an 
2501e 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
2501f 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77  number.** of row
25020 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  s that match any
25021 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6d 62   particular comb
25022 69 6e 69 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  iniation of the 
25023 66 69 72 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a  first 2 columns.
25024 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  ** of the index.
25025 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20    And so forth. 
25026 20 49 74 20 6d 75 73 74 20 61 6c 77 61 79 73 20   It must always 
25027 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
25028 0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .*.**           
25029 61 69 52 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52  aiRowEst[N]<=aiR
2502a 6f 77 45 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20  owEst[N-1].**   
2502b 20 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74          aiRowEst
2502c 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61  [N]>=1.**.** Apa
2502d 72 74 20 66 72 6f 6d 20 74 68 61 74 2c 20 77 65  rt from that, we
2502e 20 68 61 76 65 20 6c 69 74 74 6c 65 20 74 6f 20   have little to 
2502f 67 6f 20 6f 6e 20 62 65 73 69 64 65 73 20 69 6e  go on besides in
25030 74 75 69 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a  tuition as to.**
25031 20 68 6f 77 20 61 69 52 6f 77 45 73 74 5b 5d 20   how aiRowEst[] 
25032 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61  should be initia
25033 6c 69 7a 65 64 2e 20 20 54 68 65 20 6e 75 6d 62  lized.  The numb
25034 65 72 73 20 67 65 6e 65 72 61 74 65 64 20 68 65  ers generated he
25035 72 65 0a 2a 2a 20 61 72 65 20 62 61 73 65 64 20  re.** are based 
25036 6f 6e 20 74 79 70 69 63 61 6c 20 76 61 6c 75 65  on typical value
25037 73 20 66 6f 75 6e 64 20 69 6e 20 61 63 74 75 61  s found in actua
25038 6c 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51  l indices..*/.SQ
25039 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2503a 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  d sqlite3Default
2503b 52 6f 77 45 73 74 28 49 6e 64 65 78 20 2a 70 49  RowEst(Index *pI
2503c 64 78 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  dx){.  unsigned 
2503d 2a 61 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77  *a = pIdx->aiRow
2503e 45 73 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Est;.  int i;.  
2503f 61 73 73 65 72 74 28 20 61 21 3d 30 20 29 3b 0a  assert( a!=0 );.
25040 20 20 61 5b 30 5d 20 3d 20 31 30 30 30 30 30 30    a[0] = 1000000
25041 3b 0a 20 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e  ;.  for(i=pIdx->
25042 6e 43 6f 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69  nColumn; i>=5; i
25043 2d 2d 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20  --){.    a[i] = 
25044 35 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  5;.  }.  while( 
25045 69 3e 3d 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d  i>=1 ){.    a[i]
25046 20 3d 20 31 31 20 2d 20 69 3b 0a 20 20 20 20 69   = 11 - i;.    i
25047 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49  --;.  }.  if( pI
25048 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
25049 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 61 5b 70 49  None ){.    a[pI
2504a 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31  dx->nColumn] = 1
2504b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2504c 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
2504d 20 64 72 6f 70 20 61 6e 20 65 78 69 73 74 69 6e   drop an existin
2504e 67 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 20  g named index.  
2504f 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
25050 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 44  implements the D
25051 52 4f 50 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ROP INDEX statem
25052 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
25053 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
25054 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72  te3DropIndex(Par
25055 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
25056 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
25057 69 66 45 78 69 73 74 73 29 7b 0a 20 20 49 6e 64  ifExists){.  Ind
25058 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64  ex *pIndex;.  Vd
25059 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
2505a 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2505b 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
2505c 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2505d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 2f 2a  >nErr==0 );   /*
2505e 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69   Never called wi
2505f 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72 73 20  th prior errors 
25060 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  */.  if( db->mal
25061 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
25062 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
25063 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73 73  index;.  }.  ass
25064 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
25065 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51 4c  ==1 );.  if( SQL
25066 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
25067 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
25068 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  ) ){.    goto ex
25069 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20  it_drop_index;. 
2506a 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71   }.  pIndex = sq
2506b 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
2506c 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  b, pName->a[0].z
2506d 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30  Name, pName->a[0
2506e 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ].zDatabase);.  
2506f 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b  if( pIndex==0 ){
25070 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69 73  .    if( !ifExis
25071 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ts ){.      sqli
25072 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
25073 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e 64  se, "no such ind
25074 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20  ex: %S", pName, 
25075 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  0);.    }.    pP
25076 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
25077 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  a = 1;.    goto 
25078 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
25079 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64 65  .  }.  if( pInde
2507a 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b 0a  x->autoIndex ){.
2507b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2507c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64  Msg(pParse, "ind
2507d 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
2507e 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20 20  th UNIQUE ".    
2507f 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b 45    "or PRIMARY KE
25080 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e  Y constraint can
25081 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c  not be dropped",
25082 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78   0);.    goto ex
25083 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20  it_drop_index;. 
25084 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74   }.  iDb = sqlit
25085 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
25086 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  db, pIndex->pSch
25087 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ema);.#ifndef SQ
25088 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
25089 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
2508a 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  int code = SQLIT
2508b 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20 20  E_DROP_INDEX;.  
2508c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2508d 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 0a  pIndex->pTable;.
2508e 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2508f 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
25090 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
25091 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
25092 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
25093 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  b);.    if( sqli
25094 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
25095 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
25096 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
25097 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
25098 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
25099 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
2509a 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
2509b 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49  Db ) code = SQLI
2509c 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
2509d 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  EX;.    if( sqli
2509e 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2509f 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64 65  rse, code, pInde
250a0 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  x->zName, pTab->
250a1 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20  zName, zDb) ){. 
250a2 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
250a3 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d  rop_index;.    }
250a4 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
250a5 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
250a6 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69 6e  to remove the in
250a7 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68 65  dex and from the
250a8 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f   master table */
250a9 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
250aa 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
250ab 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71   if( v ){.    sq
250ac 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
250ad 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
250ae 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   1, iDb);.    sq
250af 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
250b0 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
250b1 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
250b2 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  %s WHERE name=%Q
250b3 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ",.       db->aD
250b4 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
250b5 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
250b6 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e  .       pIndex->
250b7 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20  zName.    );.   
250b8 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
250b9 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74  Table(db, "sqlit
250ba 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 44  e_stat1", db->aD
250bb 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b  b[iDb].zName) ){
250bc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65  .      sqlite3Ne
250bd 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
250be 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54  ,.        "DELET
250bf 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65  E FROM %Q.sqlite
250c0 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 64 78  _stat1 WHERE idx
250c1 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 64 62  =%Q",.        db
250c2 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
250c3 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a  , pIndex->zName.
250c4 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
250c5 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
250c6 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
250c7 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 6f 79  Db);.    destroy
250c8 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c  RootPage(pParse,
250c9 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20 69   pIndex->tnum, i
250ca 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
250cb 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
250cc 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 62 2c  _DropIndex, iDb,
250cd 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e 7a   0, 0, pIndex->z
250ce 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a 65  Name, 0);.  }..e
250cf 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3a 0a  xit_drop_index:.
250d0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
250d1 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d 65  Delete(db, pName
250d2 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 72  );.}../*.** pArr
250d3 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ay is a pointer 
250d4 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 6f  to an array of o
250d5 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f 62  bjects.  Each ob
250d6 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20 61  ject in the.** a
250d7 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79 20  rray is szEntry 
250d8 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 20  bytes in size.  
250d9 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c  This routine all
250da 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
250db 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65 6e  object on the en
250dc 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e 0a  d of the array..
250dd 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20 69  **.** *pnEntry i
250de 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
250df 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79 20  entries already 
250e0 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c 6f  in use.  *pnAllo
250e1 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65 76  c is.** the prev
250e2 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
250e3 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72   size of the arr
250e4 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69 73  ay.  initSize is
250e5 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65   the.** suggeste
250e6 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79 20  d initial array 
250e7 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  size allocation.
250e8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78  .**.** The index
250e9 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72   of the new entr
250ea 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  y is returned in
250eb 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   *pIdx..**.** Th
250ec 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
250ed 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
250ee 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62 6a  the array of obj
250ef 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20 6d  ects.  This.** m
250f0 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d 65  ight be the same
250f1 20 61 73 20 74 68 65 20 70 41 72 72 61 79 20 70   as the pArray p
250f2 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20 6d  arameter or it m
250f3 69 67 68 74 20 62 65 20 61 20 64 69 66 66 65 72  ight be a differ
250f4 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69  ent.** pointer i
250f5 66 20 74 68 65 20 61 72 72 61 79 20 77 61 73 20  f the array was 
250f6 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49  resized..*/.SQLI
250f7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
250f8 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c  *sqlite3ArrayAll
250f9 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  ocate(.  sqlite3
250fa 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43 6f   *db,      /* Co
250fb 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
250fc 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69  fy of malloc fai
250fd 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20  lures */.  void 
250fe 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a 20  *pArray,     /* 
250ff 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73  Array of objects
25100 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61 6c  .  Might be real
25101 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  located */.  int
25102 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20 2f   szEntry,      /
25103 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 6f  * Size of each o
25104 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72 72  bject in the arr
25105 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69 74  ay */.  int init
25106 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75 67  Size,     /* Sug
25107 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20 61  gested initial a
25108 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c  llocation, in el
25109 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
2510a 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f 2a  *pnEntry,     /*
2510b 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63   Number of objec
2510c 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  ts currently in 
2510d 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  use */.  int *pn
2510e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43 75  Alloc,     /* Cu
2510f 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68  rrent size of th
25110 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  e allocation, in
25111 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69   elements */.  i
25112 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 20 20  nt *pIdx        
25113 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69 6e   /* Write the in
25114 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c 6f  dex of a new slo
25115 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 63  t here */.){.  c
25116 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a 70  har *z;.  if( *p
25117 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c 6c  nEntry >= *pnAll
25118 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  oc ){.    void *
25119 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 65  pNew;.    int ne
2511a 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53 69  wSize;.    newSi
2511b 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29 2a  ze = (*pnAlloc)*
2511c 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20 20  2 + initSize;.  
2511d 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2511e 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41  DbRealloc(db, pA
2511f 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73 7a  rray, newSize*sz
25120 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28 20  Entry);.    if( 
25121 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
25122 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20 20   *pIdx = -1;.   
25123 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79     return pArray
25124 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e 41  ;.    }.    *pnA
25125 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
25126 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
25127 4e 65 77 29 2f 73 7a 45 6e 74 72 79 3b 0a 20 20  New)/szEntry;.  
25128 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65 77 3b    pArray = pNew;
25129 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72  .  }.  z = (char
2512a 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65 6d 73  *)pArray;.  mems
2512b 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72 79 20 2a  et(&z[*pnEntry *
2512c 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c 20 73 7a   szEntry], 0, sz
2512d 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49 64 78 20  Entry);.  *pIdx 
2512e 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 2b 2b  = *pnEntry;.  ++
2512f 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72 65 74 75  *pnEntry;.  retu
25130 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f 2a  rn pArray;.}../*
25131 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65 77  .** Append a new
25132 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20   element to the 
25133 67 69 76 65 6e 20 49 64 4c 69 73 74 2e 20 20 43  given IdList.  C
25134 72 65 61 74 65 20 61 20 6e 65 77 20 49 64 4c 69  reate a new IdLi
25135 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65  st if.** need be
25136 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 49 64  ..**.** A new Id
25137 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64  List is returned
25138 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c  , or NULL if mal
25139 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a  loc() fails..*/.
2513a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
2513b 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64  dList *sqlite3Id
2513c 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74  ListAppend(sqlit
2513d 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a  e3 *db, IdList *
2513e 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54  pList, Token *pT
2513f 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  oken){.  int i;.
25140 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
25141 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  {.    pList = sq
25142 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
25143 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 64 4c  o(db, sizeof(IdL
25144 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20  ist) );.    if( 
25145 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
25146 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e  n 0;.    pList->
25147 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a  nAlloc = 0;.  }.
25148 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71 6c    pList->a = sql
25149 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
2514a 65 28 0a 20 20 20 20 20 20 64 62 2c 0a 20 20 20  e(.      db,.   
2514b 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a 20 20 20     pList->a,.   
2514c 20 20 20 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d     sizeof(pList-
2514d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20 20 35 2c  >a[0]),.      5,
2514e 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e  .      &pList->n
2514f 49 64 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74  Id,.      &pList
25150 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20  ->nAlloc,.      
25151 26 69 0a 20 20 29 3b 0a 20 20 69 66 28 20 69 3c  &i.  );.  if( i<
25152 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
25153 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
25154 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74   pList);.    ret
25155 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69  urn 0;.  }.  pLi
25156 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d  st->a[i].zName =
25157 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
25158 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e  Token(db, pToken
25159 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73  );.  return pLis
2515a 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  t;.}../*.** Dele
2515b 74 65 20 61 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f  te an IdList..*/
2515c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2515d 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c 69  void sqlite3IdLi
2515e 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
2515f 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c   *db, IdList *pL
25160 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
25161 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
25162 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
25163 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b  0; i<pList->nId;
25164 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
25165 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
25166 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  st->a[i].zName);
25167 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
25168 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e  Free(db, pList->
25169 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  a);.  sqlite3DbF
2516a 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ree(db, pList);.
2516b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2516c 74 68 65 20 69 6e 64 65 78 20 69 6e 20 70 4c 69  the index in pLi
2516d 73 74 20 6f 66 20 74 68 65 20 69 64 65 6e 74 69  st of the identi
2516e 66 69 65 72 20 6e 61 6d 65 64 20 7a 49 64 2e 20  fier named zId. 
2516f 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a 20 69 66   Return -1.** if
25170 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53   not found..*/.S
25171 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
25172 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49  t sqlite3IdListI
25173 6e 64 65 78 28 49 64 4c 69 73 74 20 2a 70 4c 69  ndex(IdList *pLi
25174 73 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  st, const char *
25175 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b  zName){.  int i;
25176 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
25177 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 66  ) return -1;.  f
25178 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
25179 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
2517a 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2517b 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  mp(pList->a[i].z
2517c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20  Name, zName)==0 
2517d 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a  ) return i;.  }.
2517e 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a    return -1;.}..
2517f 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20 74 68 65  /*.** Expand the
25180 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
25181 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 53   for the given S
25182 72 63 4c 69 73 74 20 6f 62 6a 65 63 74 20 62 79  rcList object by
25183 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 6e 45 78  .** creating nEx
25184 74 72 61 20 6e 65 77 20 73 6c 6f 74 73 20 62 65  tra new slots be
25185 67 69 6e 6e 69 6e 67 20 61 74 20 69 53 74 61 72  ginning at iStar
25186 74 2e 20 20 69 53 74 61 72 74 20 69 73 20 7a 65  t.  iStart is ze
25187 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20 4e 65 77  ro based..** New
25188 20 73 6c 6f 74 73 20 61 72 65 20 7a 65 72 6f 65   slots are zeroe
25189 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  d..**.** For exa
2518a 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 20 61 20  mple, suppose a 
2518b 53 72 63 4c 69 73 74 20 69 6e 69 74 69 61 6c 6c  SrcList initiall
2518c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65  y contains two e
2518d 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a 2a 2a 20  ntries: A,B..** 
2518e 54 6f 20 61 70 70 65 6e 64 20 33 20 6e 65 77 20  To append 3 new 
2518f 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20 74 68 65  entries onto the
25190 20 65 6e 64 2c 20 64 6f 20 74 68 69 73 3a 0a 2a   end, do this:.*
25191 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 53  *.**    sqlite3S
25192 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62  rcListEnlarge(db
25193 2c 20 70 53 72 63 6c 69 73 74 2c 20 33 2c 20 32  , pSrclist, 3, 2
25194 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74  );.**.** After t
25195 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 20 69 74  he call above it
25196 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 3a 20   would contain: 
25197 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c 2c   A, B, nil, nil,
25198 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20   nil..** If the 
25199 69 53 74 61 72 74 20 61 72 67 75 6d 65 6e 74 20  iStart argument 
2519a 68 61 64 20 62 65 65 6e 20 31 20 69 6e 73 74 65  had been 1 inste
2519b 61 64 20 6f 66 20 32 2c 20 74 68 65 6e 20 74 68  ad of 2, then th
2519c 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77 6f 75 6c  e result.** woul
2519d 64 20 68 61 76 65 20 62 65 65 6e 3a 20 20 41 2c  d have been:  A,
2519e 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20   nil, nil, nil, 
2519f 42 2e 20 20 54 6f 20 70 72 65 70 65 6e 64 20 74  B.  To prepend t
251a0 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c 0a 2a 2a  he new slots,.**
251a1 20 74 68 65 20 69 53 74 61 72 74 20 76 61 6c 75   the iStart valu
251a2 65 20 77 6f 75 6c 64 20 62 65 20 30 2e 20 20 54  e would be 0.  T
251a3 68 65 20 72 65 73 75 6c 74 20 74 68 65 6e 20 77  he result then w
251a4 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e 69 6c 2c  ould.** be: nil,
251a5 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20 42 2e   nil, nil, A, B.
251a6 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
251a7 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
251a8 69 6c 73 20 74 68 65 20 53 72 63 4c 69 73 74 20  ils the SrcList 
251a9 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 54  is unchanged.  T
251aa 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c 6c 6f 63  he.** db->malloc
251ab 46 61 69 6c 65 64 20 66 6c 61 67 20 77 69 6c 6c  Failed flag will
251ac 20 62 65 20 73 65 74 20 74 6f 20 74 72 75 65 2e   be set to true.
251ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
251ae 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
251af 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67  te3SrcListEnlarg
251b0 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
251b1 2c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62  ,       /* Datab
251b2 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
251b3 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f 4f 4d 20  o notify of OOM 
251b4 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53 72 63 4c  errors */.  SrcL
251b5 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 2f  ist *pSrc,     /
251b6 2a 20 54 68 65 20 53 72 63 4c 69 73 74 20 74 6f  * The SrcList to
251b7 20 62 65 20 65 6e 6c 61 72 67 65 64 20 2a 2f 0a   be enlarged */.
251b8 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20    int nExtra,   
251b9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
251ba 66 20 6e 65 77 20 73 6c 6f 74 73 20 74 6f 20 61  f new slots to a
251bb 64 64 20 74 6f 20 70 53 72 63 2d 3e 61 5b 5d 20  dd to pSrc->a[] 
251bc 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20  */.  int iStart 
251bd 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
251be 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d 20 6f 66   in pSrc->a[] of
251bf 20 66 69 72 73 74 20 6e 65 77 20 73 6c 6f 74 20   first new slot 
251c0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 0a  */.){.  int i;..
251c1 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63    /* Sanity chec
251c2 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e 67 20  king on calling 
251c3 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20  parameters */.  
251c4 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3e 3d  assert( iStart>=
251c5 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
251c6 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20 20 61 73  Extra>=1 );.  as
251c7 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29 3b  sert( pSrc!=0 );
251c8 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72  .  assert( iStar
251c9 74 3c 3d 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b  t<=pSrc->nSrc );
251ca 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
251cb 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 61 63 65  additional space
251cc 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   if needed */.  
251cd 69 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e  if( pSrc->nSrc+n
251ce 45 78 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c  Extra>pSrc->nAll
251cf 6f 63 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73  oc ){.    SrcLis
251d0 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74  t *pNew;.    int
251d1 20 6e 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e   nAlloc = pSrc->
251d2 6e 53 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20 20  nSrc+nExtra;.   
251d3 20 69 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70   int nGot;.    p
251d4 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
251d5 65 61 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c  ealloc(db, pSrc,
251d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
251d7 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b 20  sizeof(*pSrc) + 
251d8 28 6e 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f  (nAlloc-1)*sizeo
251d9 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b  f(pSrc->a[0]) );
251da 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  .    if( pNew==0
251db 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
251dc 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
251dd 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ed );.      retu
251de 72 6e 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20  rn pSrc;.    }. 
251df 20 20 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a     pSrc = pNew;.
251e0 20 20 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69      nGot = (sqli
251e1 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
251e2 64 62 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65  db, pNew) - size
251e3 6f 66 28 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f  of(*pSrc))/sizeo
251e4 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b  f(pSrc->a[0])+1;
251e5 0a 20 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f  .    pSrc->nAllo
251e6 63 20 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a 20  c = (u16)nGot;. 
251e7 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65 78   }..  /* Move ex
251e8 69 73 74 69 6e 67 20 73 6c 6f 74 73 20 74 68 61  isting slots tha
251e9 74 20 63 6f 6d 65 20 61 66 74 65 72 20 74 68 65  t come after the
251ea 20 6e 65 77 6c 79 20 69 6e 73 65 72 74 65 64 20   newly inserted 
251eb 73 6c 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20 6f  slots.  ** out o
251ec 66 20 74 68 65 20 77 61 79 20 2a 2f 0a 20 20 66  f the way */.  f
251ed 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d  or(i=pSrc->nSrc-
251ee 31 3b 20 69 3e 3d 69 53 74 61 72 74 3b 20 69 2d  1; i>=iStart; i-
251ef 2d 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b  -){.    pSrc->a[
251f0 69 2b 6e 45 78 74 72 61 5d 20 3d 20 70 53 72 63  i+nExtra] = pSrc
251f1 2d 3e 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70 53  ->a[i];.  }.  pS
251f2 72 63 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31 36  rc->nSrc += (i16
251f3 29 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a  )nExtra;..  /* Z
251f4 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 6c  ero the newly al
251f5 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f  located slots */
251f6 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72 63 2d  .  memset(&pSrc-
251f7 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 73  >a[iStart], 0, s
251f8 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d  izeof(pSrc->a[0]
251f9 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f 72  )*nExtra);.  for
251fa 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74  (i=iStart; i<iSt
251fb 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29  art+nExtra; i++)
251fc 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d  {.    pSrc->a[i]
251fd 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20  .iCursor = -1;. 
251fe 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20   }..  /* Return 
251ff 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
25200 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c 69 73   enlarged SrcLis
25201 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 53  t */.  return pS
25202 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70  rc;.}.../*.** Ap
25203 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62 6c 65  pend a new table
25204 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67 69 76   name to the giv
25205 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43 72 65  en SrcList.  Cre
25206 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c 69 73  ate a new SrcLis
25207 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e  t if.** need be.
25208 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
25209 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
2520a 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69 66 20  SrcList even if 
2520b 70 54 61 62 6c 65 20 69 73 20 4e 55 4c 4c 2e 0a  pTable is NULL..
2520c 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74 20  **.** A SrcList 
2520d 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20  is returned, or 
2520e 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
2520f 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20   an OOM error.  
25210 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  The returned.** 
25211 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 62 65  SrcList might be
25212 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
25213 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 77 61   SrcList that wa
25214 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20 6d 69  s input or it mi
25215 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20  ght be.** a new 
25216 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20  one.  If an OOM 
25217 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63 75 72  error does occur
25218 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f  s, then the prio
25219 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69 73 74  r value of pList
2521a 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75  .** that is inpu
2521b 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  t to this routin
2521c 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  e is automatical
2521d 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  ly freed..**.** 
2521e 49 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20  If pDatabase is 
2521f 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61  not null, it mea
25220 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  ns that the tabl
25221 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61  e has an optiona
25222 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61  l.** database na
25223 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65  me prefix.  Like
25224 20 74 68 69 73 3a 20 20 22 64 61 74 61 62 61 73   this:  "databas
25225 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70  e.table".  The p
25226 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e  Database.** poin
25227 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ts to the table 
25228 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61  name and the pTa
25229 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ble points to th
2522a 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e  e database name.
2522b 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e  .** The SrcList.
2522c 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20  a[].zName field 
2522d 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74  is filled with t
2522e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68  he table name wh
2522f 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d  ich might.** com
25230 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69  e from pTable (i
25231 66 20 70 44 61 74 61 62 61 73 65 20 69 73 20 4e  f pDatabase is N
25232 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61  ULL) or from pDa
25233 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63  tabase.  .** Src
25234 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61  List.a[].zDataba
25235 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  se is filled wit
25236 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  h the database n
25237 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c  ame from pTable,
25238 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c  .** or with NULL
25239 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20   if no database 
2523a 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a  is specified..**
2523b 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
2523c 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65  ds, if call like
2523d 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
2523e 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
2523f 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c  istAppend(D,A,B,
25240 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42  0);.**.** Then B
25241 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65   is a table name
25242 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
25243 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63  e name is unspec
25244 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65  ified.  If calle
25245 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a  d.** like this:.
25246 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71  **.**         sq
25247 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
25248 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a  nd(D,A,B,C);.**.
25249 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74 68 65  ** Then C is the
2524a 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
2524b 42 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  B is the databas
2524c 65 20 6e 61 6d 65 2e 20 20 49 66 20 43 20 69 73  e name.  If C is
2524d 20 64 65 66 69 6e 65 64 0a 2a 2a 20 74 68 65 6e   defined.** then
2524e 20 73 6f 20 69 73 20 42 2e 20 20 49 6e 20 6f 74   so is B.  In ot
2524f 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20 6e 65  her words, we ne
25250 76 65 72 20 68 61 76 65 20 61 20 63 61 73 65 20  ver have a case 
25251 77 68 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  where:.**.**    
25252 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
25253 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 30 2c  istAppend(D,A,0,
25254 43 29 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 20 70  C);.**.** Both p
25255 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62  Table and pDatab
25256 61 73 65 20 61 72 65 20 61 73 73 75 6d 65 64 20  ase are assumed 
25257 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 54  to be quoted.  T
25258 68 65 79 20 61 72 65 20 64 65 71 75 6f 74 65 64  hey are dequoted
25259 0a 2a 2a 20 62 65 66 6f 72 65 20 62 65 69 6e 67  .** before being
2525a 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 72   added to the Sr
2525b 63 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cList..*/.SQLITE
2525c 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
2525d 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
2525e 41 70 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 65  Append(.  sqlite
2525f 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a  3 *db,        /*
25260 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e   Connection to n
25261 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20  otify of malloc 
25262 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72  failures */.  Sr
25263 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  cList *pList,   
25264 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 74    /* Append to t
25265 68 69 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c  his SrcList. NUL
25266 4c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 20  L creates a new 
25267 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b  SrcList */.  Tok
25268 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20  en *pTable,     
25269 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 70   /* Table to app
2526a 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  end */.  Token *
2526b 70 44 61 74 61 62 61 73 65 20 20 20 20 2f 2a 20  pDatabase    /* 
2526c 44 61 74 61 62 61 73 65 20 6f 66 20 74 68 65 20  Database of the 
2526d 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74  table */.){.  st
2526e 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2526f 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 65  m *pItem;.  asse
25270 72 74 28 20 70 44 61 74 61 62 61 73 65 3d 3d 30  rt( pDatabase==0
25271 20 7c 7c 20 70 54 61 62 6c 65 21 3d 30 20 29 3b   || pTable!=0 );
25272 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65    /* Cannot have
25273 20 43 20 77 69 74 68 6f 75 74 20 42 20 2a 2f 0a   C without B */.
25274 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
25275 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  {.    pList = sq
25276 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
25277 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 53 72 63  o(db, sizeof(Src
25278 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
25279 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
2527a 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d  rn 0;.    pList-
2527b 3e 6e 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d  >nAlloc = 1;.  }
2527c 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
2527d 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65  e3SrcListEnlarge
2527e 28 64 62 2c 20 70 4c 69 73 74 2c 20 31 2c 20 70  (db, pList, 1, p
2527f 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 69  List->nSrc);.  i
25280 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
25281 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
25282 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
25283 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20  db, pList);.    
25284 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
25285 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e  pItem = &pList->
25286 61 5b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 5d  a[pList->nSrc-1]
25287 3b 0a 20 20 69 66 28 20 70 44 61 74 61 62 61 73  ;.  if( pDatabas
25288 65 20 26 26 20 70 44 61 74 61 62 61 73 65 2d 3e  e && pDatabase->
25289 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 61 74  z==0 ){.    pDat
2528a 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  abase = 0;.  }. 
2528b 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20 29   if( pDatabase )
2528c 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 65  {.    Token *pTe
2528d 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 3b 0a  mp = pDatabase;.
2528e 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20      pDatabase = 
2528f 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54 61 62  pTable;.    pTab
25290 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20 7d 0a  le = pTemp;.  }.
25291 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
25292 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
25293 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65  Token(db, pTable
25294 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74  );.  pItem->zDat
25295 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 4e  abase = sqlite3N
25296 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
25297 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20 72   pDatabase);.  r
25298 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a  eturn pList;.}..
25299 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 56 64 62  /*.** Assign Vdb
2529a 65 43 75 72 73 6f 72 20 69 6e 64 65 78 20 6e 75  eCursor index nu
2529b 6d 62 65 72 73 20 74 6f 20 61 6c 6c 20 74 61 62  mbers to all tab
2529c 6c 65 73 20 69 6e 20 61 20 53 72 63 4c 69 73 74  les in a SrcList
2529d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2529e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
2529f 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
252a0 6f 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  ors(Parse *pPars
252a1 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73  e, SrcList *pLis
252a2 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  t){.  int i;.  s
252a3 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
252a4 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73  em *pItem;.  ass
252a5 65 72 74 28 70 4c 69 73 74 20 7c 7c 20 70 50 61  ert(pList || pPa
252a6 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
252a7 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
252a8 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28  List ){.    for(
252a9 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  i=0, pItem=pList
252aa 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53  ->a; i<pList->nS
252ab 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rc; i++, pItem++
252ac 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ){.      if( pIt
252ad 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30 20 29  em->iCursor>=0 )
252ae 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 49   break;.      pI
252af 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70  tem->iCursor = p
252b0 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
252b1 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
252b2 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
252b3 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
252b4 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70  tAssignCursors(p
252b5 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53  Parse, pItem->pS
252b6 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 0a 20 20  elect->pSrc);.  
252b7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
252b8 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
252b9 61 6e 20 65 6e 74 69 72 65 20 53 72 63 4c 69 73  an entire SrcLis
252ba 74 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20  t including all 
252bb 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65  its substructure
252bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
252bd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
252be 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71  SrcListDelete(sq
252bf 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69  lite3 *db, SrcLi
252c0 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e  st *pList){.  in
252c1 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72  t i;.  struct Sr
252c2 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
252c3 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  m;.  if( pList==
252c4 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f  0 ) return;.  fo
252c5 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
252c6 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  , i=0; i<pList->
252c7 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  nSrc; i++, pItem
252c8 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
252c9 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
252ca 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
252cb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
252cc 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  db, pItem->zName
252cd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
252ce 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
252cf 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 73 71 6c  zAlias);.    sql
252d0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
252d1 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20  Item->zIndex);. 
252d2 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
252d3 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61  Table(pItem->pTa
252d4 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  b);.    sqlite3S
252d5 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
252d6 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b  pItem->pSelect);
252d7 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
252d8 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d  Delete(db, pItem
252d9 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ->pOn);.    sqli
252da 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
252db 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e  db, pItem->pUsin
252dc 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  g);.  }.  sqlite
252dd 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
252de 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  t);.}../*.** Thi
252df 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
252e0 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
252e1 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20 74  r to add a new t
252e2 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e  erm to the.** en
252e3 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20 46  d of a growing F
252e4 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65  ROM clause.  The
252e5 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20 69   "p" parameter i
252e6 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a 2a  s the part of.**
252e7 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
252e8 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
252e9 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63 74  y been construct
252ea 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c 4c  ed.  "p" is NULL
252eb 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74  .** if this is t
252ec 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f 66  he first term of
252ed 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
252ee 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70 44  .  pTable and pD
252ef 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20 74  atabase.** are t
252f0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
252f1 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61 73  able and databas
252f2 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46  e named in the F
252f3 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 2e  ROM clause term.
252f4 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69 73  .** pDatabase is
252f5 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61 74   NULL if the dat
252f6 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c 69  abase name quali
252f7 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67 20  fier is missing 
252f8 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20 63  - the.** usual c
252f9 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65 72  ase.  If the ter
252fa 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c 20 74  m has a alias, t
252fb 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e 74  hen pAlias point
252fc 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69 61  s to the.** alia
252fd 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68 65  s token.  If the
252fe 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 71 75   term is a subqu
252ff 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71 75  ery, then pSubqu
25300 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53 45  ery is the.** SE
25301 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
25302 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79  hat the subquery
25303 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20 70   encodes.  The p
25304 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44 61  Table and.** pDa
25305 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 72  tabase parameter
25306 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20 73  s are NULL for s
25307 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 65 20  ubqueries.  The 
25308 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a 2a  pOn and pUsing.*
25309 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
2530a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
2530b 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  the ON and USING
2530c 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20   clauses..**.** 
2530d 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72 63  Return a new Src
2530e 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f 64  List which encod
2530f 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20 77  es is the FROM w
25310 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74  ith the new.** t
25311 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 51  erm added..*/.SQ
25312 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
25313 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
25314 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
25315 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rm(.  Parse *pPa
25316 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
25317 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
25318 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
25319 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2531a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74 20 6f   The left part o
2531b 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2531c 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e 20 2a  e already seen *
2531d 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  /.  Token *pTabl
2531e 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  e,          /* N
2531f 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
25320 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65 20 46   to add to the F
25321 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
25322 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65  Token *pDatabase
25323 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ,       /* Name 
25324 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
25325 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 6c  containing pTabl
25326 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  e */.  Token *pA
25327 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 2f  lias,          /
25328 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64  * The right-hand
25329 20 73 69 64 65 20 6f 66 20 74 68 65 20 41 53 20   side of the AS 
2532a 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  subexpression */
2532b 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62 71  .  Select *pSubq
2532c 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 41 20  uery,      /* A 
2532d 73 75 62 71 75 65 72 79 20 75 73 65 64 20 69 6e  subquery used in
2532e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c   place of a tabl
2532f 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72  e name */.  Expr
25330 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 20 20   *pOn,          
25331 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63 6c      /* The ON cl
25332 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a  ause of a join *
25333 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69  /.  IdList *pUsi
25334 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ng          /* T
25335 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
25336 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a  of a join */.){.
25337 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
25338 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
25339 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2533a 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
2533b 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20 70 55  !p && (pOn || pU
2533c 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 73 71 6c  sing) ){.    sql
2533d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2533e 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63 6c 61  rse, "a JOIN cla
2533f 75 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20  use is required 
25340 62 65 66 6f 72 65 20 25 73 22 2c 20 0a 20 20 20  before %s", .   
25341 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22 20 3a     (pOn ? "ON" :
25342 20 22 55 53 49 4e 47 22 29 0a 20 20 20 20 29 3b   "USING").    );
25343 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 6e 64  .    goto append
25344 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 20 7d  _from_error;.  }
25345 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72  .  p = sqlite3Sr
25346 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
25347 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61  p, pTable, pData
25348 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d  base);.  if( p==
25349 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 53  0 || NEVER(p->nS
2534a 72 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20 67 6f  rc==0) ){.    go
2534b 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65  to append_from_e
2534c 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 49 74 65  rror;.  }.  pIte
2534d 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72  m = &p->a[p->nSr
2534e 63 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  c-1];.  assert( 
2534f 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20 20 69  pAlias!=0 );.  i
25350 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a  f( pAlias->n ){.
25351 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61      pItem->zAlia
25352 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  s = sqlite3NameF
25353 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c  romToken(db, pAl
25354 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65  ias);.  }.  pIte
25355 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 75  m->pSelect = pSu
25356 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d  bquery;.  pItem-
25357 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49  >pOn = pOn;.  pI
25358 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55  tem->pUsing = pU
25359 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e 20 70  sing;.  return p
2535a 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f  ;.. append_from_
2535b 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28  error:.  assert(
2535c 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74   p==0 );.  sqlit
2535d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2535e 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33   pOn);.  sqlite3
2535f 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
25360 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71 6c 69   pUsing);.  sqli
25361 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
25362 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a  db, pSubquery);.
25363 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
25364 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44 45  *.** Add an INDE
25365 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e  XED BY or NOT IN
25366 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f 20  DEXED clause to 
25367 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
25368 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d  y added .** elem
25369 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72 63  ent of the sourc
2536a 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61 73  e-list passed as
2536b 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
2536c 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
2536d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2536e 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78  ite3SrcListIndex
2536f 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61 72  edBy(Parse *pPar
25370 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20  se, SrcList *p, 
25371 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42  Token *pIndexedB
25372 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49  y){.  assert( pI
25373 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b 0a 20  ndexedBy!=0 );. 
25374 20 69 66 28 20 70 20 26 26 20 41 4c 57 41 59 53   if( p && ALWAYS
25375 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 7b 0a 20  (p->nSrc>0) ){. 
25376 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
25377 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
25378 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d  &p->a[p->nSrc-1]
25379 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ;.    assert( pI
2537a 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d  tem->notIndexed=
2537b 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a 49 6e  =0 && pItem->zIn
2537c 64 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  dex==0 );.    if
2537d 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d  ( pIndexedBy->n=
2537e 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65 64 42  =1 && !pIndexedB
2537f 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a  y->z ){.      /*
25380 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45 44 22   A "NOT INDEXED"
25381 20 63 6c 61 75 73 65 20 77 61 73 20 73 75 70 70   clause was supp
25382 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73 65 2e  lied. See parse.
25383 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73  y .      ** cons
25384 74 72 75 63 74 20 22 69 6e 64 65 78 65 64 5f 6f  truct "indexed_o
25385 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  pt" for details.
25386 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d 2d   */.      pItem-
25387 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 31 3b  >notIndexed = 1;
25388 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
25389 20 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20    pItem->zIndex 
2538a 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2538b 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64  mToken(pParse->d
2538c 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29 3b 0a  b, pIndexedBy);.
2538d 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
2538e 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69 6e 67  ** When building
2538f 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61 75 73   up a FROM claus
25390 65 20 69 6e 20 74 68 65 20 70 61 72 73 65 72 2c  e in the parser,
25391 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   the join operat
25392 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c  or.** is initial
25393 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  ly attached to t
25394 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 2e  he left operand.
25395 20 20 42 75 74 20 74 68 65 20 63 6f 64 65 20 67    But the code g
25396 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78 70 65  enerator.** expe
25397 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65  cts the join ope
25398 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e 20 74  rator to be on t
25399 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  he right operand
2539a 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
2539b 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20 6a 6f  ** Shifts all jo
2539c 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66 72 6f  in operators fro
2539d 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 20  m left to right 
2539e 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 46 52  for an entire FR
2539f 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a  OM.** clause..**
253a0 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53 75 70  .** Example: Sup
253a1 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20 69 73  pose the join is
253a2 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
253a3 2a 20 20 20 20 20 20 20 20 20 20 20 41 20 6e 61  *           A na
253a4 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e  tural cross join
253a5 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 65   B.**.** The ope
253a6 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75 72 61  rator is "natura
253a7 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e 20 20  l cross join".  
253a8 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70 65 72  The A and B oper
253a9 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65 64 0a  ands are stored.
253aa 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e  ** in p->a[0] an
253ab 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73 70 65  d p->a[1], respe
253ac 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20 70 61  ctively.  The pa
253ad 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79 20 73  rser initially s
253ae 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f 70 65  tores the.** ope
253af 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20 20 54  rator with A.  T
253b0 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 69 66  his routine shif
253b1 74 73 20 74 68 61 74 20 6f 70 65 72 61 74 6f 72  ts that operator
253b2 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53   over to B..*/.S
253b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
253b4 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
253b5 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 53  tShiftJoinType(S
253b6 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 66  rcList *p){.  if
253b7 28 20 70 20 26 26 20 70 2d 3e 61 20 29 7b 0a 20  ( p && p->a ){. 
253b8 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
253b9 72 28 69 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20 69  r(i=p->nSrc-1; i
253ba 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20  >0; i--){.      
253bb 70 2d 3e 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65  p->a[i].jointype
253bc 20 3d 20 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69   = p->a[i-1].joi
253bd 6e 74 79 70 65 3b 0a 20 20 20 20 7d 0a 20 20 20  ntype;.    }.   
253be 20 70 2d 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70   p->a[0].jointyp
253bf 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  e = 0;.  }.}../*
253c0 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e  .** Begin a tran
253c1 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  saction.*/.SQLIT
253c2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
253c3 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
253c4 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  action(Parse *pP
253c5 61 72 73 65 2c 20 69 6e 74 20 74 79 70 65 29 7b  arse, int type){
253c6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
253c7 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74    Vdbe *v;.  int
253c8 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   i;..  assert( p
253c9 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64 62  Parse!=0 );.  db
253ca 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
253cb 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29   assert( db!=0 )
253cc 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44  ;./*  if( db->aD
253cd 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65  b[0].pBt==0 ) re
253ce 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73  turn; */.  if( s
253cf 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
253d0 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54  pParse, SQLITE_T
253d1 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 47  RANSACTION, "BEG
253d2 49 4e 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20  IN", 0, 0) ){.  
253d3 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
253d4 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
253d5 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
253d6 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !v ) return;. 
253d7 20 69 66 28 20 74 79 70 65 21 3d 54 4b 5f 44 45   if( type!=TK_DE
253d8 46 45 52 52 45 44 20 29 7b 0a 20 20 20 20 66 6f  FERRED ){.    fo
253d9 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
253da 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
253db 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
253dc 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  v, OP_Transactio
253dd 6e 2c 20 69 2c 20 28 74 79 70 65 3d 3d 54 4b 5f  n, i, (type==TK_
253de 45 58 43 4c 55 53 49 56 45 29 2b 31 29 3b 0a 20  EXCLUSIVE)+1);. 
253df 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
253e0 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 29 3b  UsesBtree(v, i);
253e1 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
253e2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
253e3 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
253e4 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   0, 0);.}../*.**
253e5 20 43 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61   Commit a transa
253e6 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ction.*/.SQLITE_
253e7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
253e8 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61  ite3CommitTransa
253e9 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ction(Parse *pPa
253ea 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
253eb 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
253ec 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
253ed 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70  e!=0 );.  db = p
253ee 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
253ef 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a  ert( db!=0 );./*
253f0 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d    if( db->aDb[0]
253f1 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  .pBt==0 ) return
253f2 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  ; */.  if( sqlit
253f3 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
253f4 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  se, SQLITE_TRANS
253f5 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22  ACTION, "COMMIT"
253f6 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  , 0, 0) ){.    r
253f7 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d  eturn;.  }.  v =
253f8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
253f9 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
253fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
253fb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
253fc 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30  AutoCommit, 1, 0
253fd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
253fe 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73  Rollback a trans
253ff 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  action.*/.SQLITE
25400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
25401 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
25402 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a  nsaction(Parse *
25403 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
25404 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a  e3 *db;.  Vdbe *
25405 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  v;..  assert( pP
25406 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64 62 20  arse!=0 );.  db 
25407 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
25408 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
25409 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  ./*  if( db->aDb
2540a 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74  [0].pBt==0 ) ret
2540b 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71  urn; */.  if( sq
2540c 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2540d 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52  Parse, SQLITE_TR
2540e 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c  ANSACTION, "ROLL
2540f 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29 7b 0a  BACK", 0, 0) ){.
25410 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
25411 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
25412 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
25413 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
25414 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25415 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
25416 20 31 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   1, 1);.  }.}../
25417 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
25418 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
25419 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20  the parser when 
2541a 69 74 20 70 61 72 73 65 73 20 61 20 63 6f 6d 6d  it parses a comm
2541b 61 6e 64 20 74 6f 20 63 72 65 61 74 65 2c 0a 2a  and to create,.*
2541c 2a 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c  * release or rol
2541d 6c 62 61 63 6b 20 61 6e 20 53 51 4c 20 73 61 76  lback an SQL sav
2541e 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a 53 51 4c 49  epoint. .*/.SQLI
2541f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25420 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74  sqlite3Savepoint
25421 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
25422 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70  int op, Token *p
25423 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a  Name){.  char *z
25424 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
25425 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72  meFromToken(pPar
25426 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  se->db, pName);.
25427 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20    if( zName ){. 
25428 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c     Vdbe *v = sql
25429 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2542a 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  se);.#ifndef SQL
2542b 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2542c 5a 41 54 49 4f 4e 0a 20 20 20 20 73 74 61 74 69  ZATION.    stati
2542d 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a  c const char *az
2542e 5b 5d 20 3d 20 7b 20 22 42 45 47 49 4e 22 2c 20  [] = { "BEGIN", 
2542f 22 52 45 4c 45 41 53 45 22 2c 20 22 52 4f 4c 4c  "RELEASE", "ROLL
25430 42 41 43 4b 22 20 7d 3b 0a 20 20 20 20 61 73 73  BACK" };.    ass
25431 65 72 74 28 20 21 53 41 56 45 50 4f 49 4e 54 5f  ert( !SAVEPOINT_
25432 42 45 47 49 4e 20 26 26 20 53 41 56 45 50 4f 49  BEGIN && SAVEPOI
25433 4e 54 5f 52 45 4c 45 41 53 45 3d 3d 31 20 26 26  NT_RELEASE==1 &&
25434 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
25435 41 43 4b 3d 3d 32 20 29 3b 0a 23 65 6e 64 69 66  ACK==2 );.#endif
25436 0a 20 20 20 20 69 66 28 20 21 76 20 7c 7c 20 73  .    if( !v || s
25437 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
25438 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53  pParse, SQLITE_S
25439 41 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d  AVEPOINT, az[op]
2543a 2c 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20  , zName, 0) ){. 
2543b 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2543c 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  ee(pParse->db, z
2543d 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  Name);.      ret
2543e 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  urn;.    }.    s
2543f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25440 28 76 2c 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74  (v, OP_Savepoint
25441 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d  , op, 0, 0, zNam
25442 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  e, P4_DYNAMIC);.
25443 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b    }.}../*.** Mak
25444 65 20 73 75 72 65 20 74 68 65 20 54 45 4d 50 20  e sure the TEMP 
25445 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
25446 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65 20 66   and available f
25447 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72 6e 0a  or use.  Return.
25448 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
25449 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76 65 20   errors.  Leave 
2544a 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67  any error messag
2544b 65 73 20 69 6e 20 74 68 65 20 70 50 61 72 73 65  es in the pParse
2544c 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53   structure..*/.S
2544d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2544e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d  t sqlite3OpenTem
2544f 70 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20  pDatabase(Parse 
25450 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69  *pParse){.  sqli
25451 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
25452 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
25453 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20 26 26  aDb[1].pBt==0 &&
25454 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69   !pParse->explai
25455 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  n ){.    int rc;
25456 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
25457 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 0a 20  t int flags = . 
25458 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
25459 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
2545a 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
2545b 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a  E_OPEN_CREATE |.
2545c 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2545d 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
2545e 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
2545f 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
25460 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20 20  CLOSE |.        
25461 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45    SQLITE_OPEN_TE
25462 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63 20 3d  MP_DB;..    rc =
25463 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
25464 74 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c 20 53  tory(db, 0, 0, S
25465 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
25466 43 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c  CHE_SIZE, flags,
25467 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
25468 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25469 20 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42    &db->aDb[1].pB
2546a 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
2546b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2546c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2546d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62  sg(pParse, "unab
2546e 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d  le to open a tem
2546f 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20  porary database 
25470 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c 65 20  ".        "file 
25471 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 6d 70  for storing temp
25472 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29 3b 0a  orary tables");.
25473 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
25474 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65 74   = rc;.      ret
25475 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
25476 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e 66 6c   assert( (db->fl
25477 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49 6e 54  ags & SQLITE_InT
25478 72 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e  rans)==0 || db->
25479 61 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a 20 20  autoCommit );.  
2547a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
2547b 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a  b[1].pSchema );.
2547c 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
2547d 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71 6c 69  JournalMode(sqli
2547e 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62  te3BtreePager(db
2547f 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c 0a 20  ->aDb[1].pBt),. 
25480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25481 20 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 64             db->d
25482 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b  fltJournalMode);
25483 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
25484 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
25485 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  te VDBE code tha
25486 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20 74 68  t will verify th
25487 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
25488 61 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61 20 72  and start.** a r
25489 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ead-transaction 
2548a 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20 64 61  for all named da
2548b 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2a  tabase files..**
2548c 0a 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f 72 74  .** It is import
2548d 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 73 63 68  ant that all sch
2548e 65 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65 20 76  ema cookies be v
2548f 65 72 69 66 69 65 64 20 61 6e 64 20 61 6c 6c 0a  erified and all.
25490 2a 2a 20 72 65 61 64 20 74 72 61 6e 73 61 63 74  ** read transact
25491 69 6f 6e 73 20 62 65 20 73 74 61 72 74 65 64 20  ions be started 
25492 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20  before anything 
25493 65 6c 73 65 20 68 61 70 70 65 6e 73 20 69 6e 0a  else happens in.
25494 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72 6f 67  ** the VDBE prog
25495 72 61 6d 2e 20 20 42 75 74 20 74 68 69 73 20 72  ram.  But this r
25496 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61  outine can be ca
25497 6c 6c 65 64 20 61 66 74 65 72 20 6d 75 63 68 20  lled after much 
25498 6f 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20 68 61  other.** code ha
25499 73 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64  s been generated
2549a 2e 20 20 53 6f 20 68 65 72 65 20 69 73 20 77 68  .  So here is wh
2549b 61 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a 20  at we do:.**.** 
2549c 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
2549d 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2549e 61 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65 20 61  alled, we code a
2549f 6e 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74 0a 2a  n OP_Goto that.*
254a0 2a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 61  * will jump to a
254a1 20 73 75 62 72 6f 75 74 69 6e 65 20 61 74 20 74   subroutine at t
254a2 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72  he end of the pr
254a3 6f 67 72 61 6d 2e 20 20 54 68 65 6e 20 77 65 0a  ogram.  Then we.
254a4 2a 2a 20 72 65 63 6f 72 64 20 65 76 65 72 79 20  ** record every 
254a5 64 61 74 61 62 61 73 65 20 74 68 61 74 20 6e 65  database that ne
254a6 65 64 73 20 69 74 73 20 73 63 68 65 6d 61 20 76  eds its schema v
254a7 65 72 69 66 69 65 64 20 69 6e 20 74 68 65 0a 2a  erified in the.*
254a8 2a 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  * pParse->cookie
254a9 4d 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c 61 74  Mask field.  Lat
254aa 65 72 2c 20 61 66 74 65 72 20 61 6c 6c 20 6f 74  er, after all ot
254ab 68 65 72 20 63 6f 64 65 20 68 61 73 20 62 65 65  her code has bee
254ac 6e 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 2c 20  n.** generated, 
254ad 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74  the subroutine t
254ae 68 61 74 20 64 6f 65 73 20 74 68 65 20 63 6f 6f  hat does the coo
254af 6b 69 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e  kie verification
254b0 73 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74 73 20  s and.** starts 
254b1 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  the transactions
254b2 20 77 69 6c 6c 20 62 65 20 63 6f 64 65 64 20 61   will be coded a
254b3 6e 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 50  nd the OP_Goto P
254b4 32 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20  2 value.** will 
254b5 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  be made to point
254b6 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f 75 74   to that subrout
254b7 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65 72 61  ine.  The genera
254b8 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 63  tion of the.** c
254b9 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69  ookie verificati
254ba 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20 63 6f  on subroutine co
254bb 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20 73 71  de happens in sq
254bc 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
254bd 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44  g()..**.** If iD
254be 62 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 74 68  b<0 then code th
254bf 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79 20 2d  e OP_Goto only -
254c0 20 64 6f 6e 27 74 20 73 65 74 20 66 6c 61 67 20   don't set flag 
254c1 74 6f 20 76 65 72 69 66 79 20 74 68 65 0a 2a 2a  to verify the.**
254c2 20 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79 20 64   schema on any d
254c3 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 20  atabases.  This 
254c4 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 70  can be used to p
254c5 6f 73 69 74 69 6f 6e 20 74 68 65 20 4f 50 5f 47  osition the OP_G
254c6 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69 6e 20  oto.** early in 
254c7 74 68 65 20 63 6f 64 65 2c 20 62 65 66 6f 72 65  the code, before
254c8 20 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e 79 20   we know if any 
254c9 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 73 20  database tables 
254ca 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2f  will be used..*/
254cb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
254cc 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
254cd 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72  VerifySchema(Par
254ce 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
254cf 69 44 62 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  iDb){.  Parse *p
254d0 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74  Toplevel = sqlit
254d1 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
254d2 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 20  pParse);..  if( 
254d3 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69  pToplevel->cooki
254d4 65 47 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  eGoto==0 ){.    
254d5 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
254d6 33 47 65 74 56 64 62 65 28 70 54 6f 70 6c 65 76  3GetVdbe(pToplev
254d7 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d  el);.    if( v==
254d8 30 20 29 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20  0 ) return;  /* 
254d9 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
254da 73 20 69 66 20 74 68 65 72 65 20 77 61 73 20 61  s if there was a
254db 20 70 72 69 6f 72 20 65 72 72 6f 72 20 2a 2f 0a   prior error */.
254dc 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63      pToplevel->c
254dd 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 73 71 6c 69  ookieGoto = sqli
254de 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
254df 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 2b   OP_Goto, 0, 0)+
254e0 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 62  1;.  }.  if( iDb
254e1 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
254e2 65 33 20 2a 64 62 20 3d 20 70 54 6f 70 6c 65 76  e3 *db = pToplev
254e3 65 6c 2d 3e 64 62 3b 0a 20 20 20 20 69 6e 74 20  el->db;.    int 
254e4 6d 61 73 6b 3b 0a 0a 20 20 20 20 61 73 73 65 72  mask;..    asser
254e5 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  t( iDb<db->nDb )
254e6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  ;.    assert( db
254e7 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d  ->aDb[iDb].pBt!=
254e8 30 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 20  0 || iDb==1 );. 
254e9 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 53     assert( iDb<S
254ea 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
254eb 45 44 2b 32 20 29 3b 0a 20 20 20 20 6d 61 73 6b  ED+2 );.    mask
254ec 20 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 20 20 69   = 1<<iDb;.    i
254ed 66 28 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 63  f( (pToplevel->c
254ee 6f 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61 73 6b  ookieMask & mask
254ef 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 54  )==0 ){.      pT
254f0 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d  oplevel->cookieM
254f1 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20  ask |= mask;.   
254f2 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f     pToplevel->co
254f3 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d  okieValue[iDb] =
254f4 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
254f5 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f  chema->schema_co
254f6 6f 6b 69 65 3b 0a 20 20 20 20 20 20 69 66 28 20  okie;.      if( 
254f7 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
254f8 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  iDb==1 ){.      
254f9 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d    sqlite3OpenTem
254fa 70 44 61 74 61 62 61 73 65 28 70 54 6f 70 6c 65  pDatabase(pTople
254fb 76 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  vel);.      }.  
254fc 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
254fd 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
254fe 6f 64 65 20 74 68 61 74 20 70 72 65 70 61 72 65  ode that prepare
254ff 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f  s for doing an o
25500 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a  peration that.**
25501 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68   might change th
25502 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
25503 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
25504 74 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e  tarts a new tran
25505 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72  saction if we ar
25506 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69  e not already wi
25507 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61  thin.** a transa
25508 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72  ction.  If we ar
25509 65 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  e already within
2550a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20   a transaction, 
2550b 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e  then a checkpoin
2550c 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74  t.** is set if t
2550d 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20  he setStatement 
2550e 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75  parameter is tru
2550f 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74  e.  A checkpoint
25510 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65   should.** be se
25511 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73  t for operations
25512 20 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c   that might fail
25513 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74   (due to a const
25514 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a  raint) part of.*
25515 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67  * the way throug
25516 68 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c  h and which will
25517 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f   need to undo so
25518 6d 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75  me writes withou
25519 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72  t having to.** r
2551a 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c  ollback the whol
2551b 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
2551c 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77  For operations w
2551d 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61  here all constra
2551e 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63  ints.** can be c
2551f 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e  hecked before an
25520 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  y changes are ma
25521 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  de to the databa
25522 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a  se, it is never.
25523 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  ** necessary to 
25524 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64  undo a write and
25525 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20   the checkpoint 
25526 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65  should not be se
25527 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
25528 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25529 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2552a 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
2552b 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 74 65  se, int setState
2552c 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 7b 0a  ment, int iDb){.
2552d 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
2552e 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
2552f 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
25530 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65  );.  sqlite3Code
25531 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
25532 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 70 54 6f  rse, iDb);.  pTo
25533 70 6c 65 76 65 6c 2d 3e 77 72 69 74 65 4d 61 73  plevel->writeMas
25534 6b 20 7c 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 70  k |= 1<<iDb;.  p
25535 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 6c 74  Toplevel->isMult
25536 69 57 72 69 74 65 20 7c 3d 20 73 65 74 53 74 61  iWrite |= setSta
25537 74 65 6d 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tement;.}../*.**
25538 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 74   Indicate that t
25539 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63 75 72  he statement cur
2553a 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
2553b 73 74 72 75 63 74 69 6f 6e 20 6d 69 67 68 74 20  struction might 
2553c 77 72 69 74 65 0a 2a 2a 20 6d 6f 72 65 20 74 68  write.** more th
2553d 61 6e 20 6f 6e 65 20 65 6e 74 72 79 20 28 65 78  an one entry (ex
2553e 61 6d 70 6c 65 3a 20 64 65 6c 65 74 69 6e 67 20  ample: deleting 
2553f 6f 6e 65 20 72 6f 77 20 74 68 65 6e 20 69 6e 73  one row then ins
25540 65 72 74 69 6e 67 20 61 6e 6f 74 68 65 72 2c 0a  erting another,.
25541 2a 2a 20 69 6e 73 65 72 74 69 6e 67 20 6d 75 6c  ** inserting mul
25542 74 69 70 6c 65 20 72 6f 77 73 20 69 6e 20 61 20  tiple rows in a 
25543 74 61 62 6c 65 2c 20 6f 72 20 69 6e 73 65 72 74  table, or insert
25544 69 6e 67 20 61 20 72 6f 77 20 61 6e 64 20 69 6e  ing a row and in
25545 64 65 78 20 65 6e 74 72 69 65 73 2e 29 0a 2a 2a  dex entries.).**
25546 20 49 66 20 61 6e 20 61 62 6f 72 74 20 6f 63 63   If an abort occ
25547 75 72 73 20 61 66 74 65 72 20 73 6f 6d 65 20 6f  urs after some o
25548 66 20 74 68 65 73 65 20 77 72 69 74 65 73 20 68  f these writes h
25549 61 76 65 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74  ave completed, t
2554a 68 65 6e 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 62  hen it will.** b
2554b 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75  e necessary to u
2554c 6e 64 6f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ndo the complete
2554d 64 20 77 72 69 74 65 73 2e 0a 2a 2f 0a 53 51 4c  d writes..*/.SQL
2554e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2554f 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69   sqlite3MultiWri
25550 74 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  te(Parse *pParse
25551 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  ){.  Parse *pTop
25552 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
25553 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
25554 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65  rse);.  pTopleve
25555 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20  l->isMultiWrite 
25556 3d 20 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54  = 1;.}../* .** T
25557 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
25558 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
25559 74 69 6e 65 20 69 66 20 69 73 20 64 69 73 63 6f  tine if is disco
2555a 76 65 72 73 20 74 68 61 74 20 69 74 20 69 73 0a  vers that it is.
2555b 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61  ** possible to a
2555c 62 6f 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74  bort a statement
2555d 20 70 72 69 6f 72 20 74 6f 20 63 6f 6d 70 6c 65   prior to comple
2555e 74 69 6f 6e 2e 20 20 49 6e 20 6f 72 64 65 72 20  tion.  In order 
2555f 74 6f 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 20 74  to .** perform t
25560 68 69 73 20 61 62 6f 72 74 20 77 69 74 68 6f 75  his abort withou
25561 74 20 63 6f 72 72 75 70 74 69 6e 67 20 74 68 65  t corrupting the
25562 20 64 61 74 61 62 61 73 65 2c 20 77 65 20 6e 65   database, we ne
25563 65 64 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 73 75  ed to make.** su
25564 72 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74  re that the stat
25565 65 6d 65 6e 74 20 69 73 20 70 72 6f 74 65 63 74  ement is protect
25566 65 64 20 62 79 20 61 20 73 74 61 74 65 6d 65 6e  ed by a statemen
25567 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  t transaction..*
25568 2a 0a 2a 2a 20 54 65 63 68 6e 69 63 61 6c 6c 79  *.** Technically
25569 2c 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74  , we only need t
2556a 6f 20 73 65 74 20 74 68 65 20 6d 61 79 41 62 6f  o set the mayAbo
2556b 72 74 20 66 6c 61 67 20 69 66 20 74 68 65 0a 2a  rt flag if the.*
2556c 2a 20 69 73 4d 75 6c 74 69 57 72 69 74 65 20 66  * isMultiWrite f
2556d 6c 61 67 20 77 61 73 20 70 72 65 76 69 6f 75 73  lag was previous
2556e 6c 79 20 73 65 74 2e 20 20 54 68 65 72 65 20 69  ly set.  There i
2556f 73 20 61 20 74 69 6d 65 20 64 65 70 65 6e 64 65  s a time depende
25570 6e 63 79 0a 2a 2a 20 73 75 63 68 20 74 68 61 74  ncy.** such that
25571 20 74 68 65 20 61 62 6f 72 74 20 6d 75 73 74 20   the abort must 
25572 6f 63 63 75 72 20 61 66 74 65 72 20 74 68 65 20  occur after the 
25573 6d 75 6c 74 69 77 72 69 74 65 2e 20 20 54 68 69  multiwrite.  Thi
25574 73 20 6d 61 6b 65 73 0a 2a 2a 20 73 6f 6d 65 20  s makes.** some 
25575 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6c  statements invol
25576 76 69 6e 67 20 74 68 65 20 52 45 50 4c 41 43 45  ving the REPLACE
25577 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
25578 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 0a 2a  tion algorithm.*
25579 2a 20 67 6f 20 61 20 6c 69 74 74 6c 65 20 66 61  * go a little fa
2557a 73 74 65 72 2e 20 20 42 75 74 20 74 61 6b 69 6e  ster.  But takin
2557b 67 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74  g advantage of t
2557c 68 69 73 20 74 69 6d 65 20 64 65 70 65 6e 64 65  his time depende
2557d 6e 63 79 0a 2a 2a 20 6d 61 6b 65 73 20 69 74 20  ncy.** makes it 
2557e 6d 6f 72 65 20 64 69 66 66 69 63 75 6c 74 20 74  more difficult t
2557f 6f 20 70 72 6f 76 65 20 74 68 61 74 20 74 68 65  o prove that the
25580 20 63 6f 64 65 20 69 73 20 63 6f 72 72 65 63 74   code is correct
25581 20 28 69 6e 20 0a 2a 2a 20 70 61 72 74 69 63 75   (in .** particu
25582 6c 61 72 2c 20 69 74 20 70 72 65 76 65 6e 74 73  lar, it prevents
25583 20 75 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67   us from writing
25584 20 61 6e 20 65 66 66 65 63 74 69 76 65 0a 2a 2a   an effective.**
25585 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
25586 6f 66 20 73 71 6c 69 74 65 33 41 73 73 65 72 74  of sqlite3Assert
25587 4d 61 79 41 62 6f 72 74 28 29 29 20 61 6e 64 20  MayAbort()) and 
25588 73 6f 20 77 65 20 68 61 76 65 20 63 68 6f 73 65  so we have chose
25589 6e 0a 2a 2a 20 74 6f 20 74 61 6b 65 20 74 68 65  n.** to take the
2558a 20 73 61 66 65 20 72 6f 75 74 65 20 61 6e 64 20   safe route and 
2558b 73 6b 69 70 20 74 68 65 20 6f 70 74 69 6d 69 7a  skip the optimiz
2558c 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
2558d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2558e 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61  lite3MayAbort(Pa
2558f 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
25590 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c  Parse *pToplevel
25591 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54   = sqlite3ParseT
25592 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b  oplevel(pParse);
25593 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6d 61  .  pToplevel->ma
25594 79 41 62 6f 72 74 20 3d 20 31 3b 0a 7d 0a 0a 2f  yAbort = 1;.}../
25595 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f  *.** Code an OP_
25596 48 61 6c 74 20 74 68 61 74 20 63 61 75 73 65 73  Halt that causes
25597 20 74 68 65 20 76 64 62 65 20 74 6f 20 72 65 74   the vdbe to ret
25598 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f  urn an SQLITE_CO
25599 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 65 72 72 6f  NSTRAINT.** erro
2559a 72 2e 20 54 68 65 20 6f 6e 45 72 72 6f 72 20 70  r. The onError p
2559b 61 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69  arameter determi
2559c 6e 65 73 20 77 68 69 63 68 20 28 69 66 20 61 6e  nes which (if an
2559d 79 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  y) of the statem
2559e 65 6e 74 0a 2a 2a 20 61 6e 64 2f 6f 72 20 63 75  ent.** and/or cu
2559f 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
255a0 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
255a1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
255a2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
255a3 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 50  HaltConstraint(P
255a4 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
255a5 74 20 6f 6e 45 72 72 6f 72 2c 20 63 68 61 72 20  t onError, char 
255a6 2a 70 34 2c 20 69 6e 74 20 70 34 74 79 70 65 29  *p4, int p4type)
255a7 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
255a8 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
255a9 72 73 65 29 3b 0a 20 20 69 66 28 20 6f 6e 45 72  rse);.  if( onEr
255aa 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b  ror==OE_Abort ){
255ab 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41  .    sqlite3MayA
255ac 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20  bort(pParse);.  
255ad 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
255ae 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp4(v, OP_Halt
255af 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  , SQLITE_CONSTRA
255b0 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c  INT, onError, 0,
255b1 20 70 34 2c 20 70 34 74 79 70 65 29 3b 0a 7d 0a   p4, p4type);.}.
255b2 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
255b3 73 65 65 20 69 66 20 70 49 6e 64 65 78 20 75 73  see if pIndex us
255b4 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  es the collating
255b5 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e   sequence pColl.
255b6 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65    Return.** true
255b7 20 69 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20   if it does and 
255b8 66 61 6c 73 65 20 69 66 20 69 74 20 64 6f 65 73  false if it does
255b9 20 6e 6f 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   not..*/.#ifndef
255ba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
255bb 4e 44 45 58 0a 73 74 61 74 69 63 20 69 6e 74 20  NDEX.static int 
255bc 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63  collationMatch(c
255bd 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
255be 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 29  , Index *pIndex)
255bf 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
255c0 65 72 74 28 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b  ert( zColl!=0 );
255c1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
255c2 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  ndex->nColumn; i
255c3 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ++){.    const c
255c4 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d  har *z = pIndex-
255c5 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20  >azColl[i];.    
255c6 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a  assert( z!=0 );.
255c7 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
255c8 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f  e3StrICmp(z, zCo
255c9 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ll) ){.      ret
255ca 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
255cb 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
255cc 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63  endif../*.** Rec
255cd 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63  ompute all indic
255ce 65 73 20 6f 66 20 70 54 61 62 20 74 68 61 74 20  es of pTab that 
255cf 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  use the collatin
255d0 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  g sequence pColl
255d1 2e 0a 2a 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30  ..** If pColl==0
255d2 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 20   then recompute 
255d3 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  all indices of p
255d4 54 61 62 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  Tab..*/.#ifndef 
255d5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
255d6 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20  DEX.static void 
255d7 72 65 69 6e 64 65 78 54 61 62 6c 65 28 50 61 72  reindexTable(Par
255d8 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
255d9 65 20 2a 70 54 61 62 2c 20 63 68 61 72 20 63 6f  e *pTab, char co
255da 6e 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49  nst *zColl){.  I
255db 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20  ndex *pIndex;   
255dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
255dd 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65   index associate
255de 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a  d with pTab */..
255df 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61    for(pIndex=pTa
255e0 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  b->pIndex; pInde
255e1 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78  x; pIndex=pIndex
255e2 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
255e3 28 20 7a 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f  ( zColl==0 || co
255e4 6c 6c 61 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f  llationMatch(zCo
255e5 6c 6c 2c 20 70 49 6e 64 65 78 29 20 29 7b 0a 20  ll, pIndex) ){. 
255e6 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73       int iDb = s
255e7 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
255e8 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
255e9 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
255ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
255eb 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
255ec 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29  (pParse, 0, iDb)
255ed 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
255ee 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
255ef 65 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a  e, pIndex, -1);.
255f0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
255f1 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
255f2 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
255f3 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  of all tables in
255f4 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77   all databases w
255f5 68 65 72 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69  here the.** indi
255f6 63 65 73 20 75 73 65 20 74 68 65 20 63 6f 6c 6c  ces use the coll
255f7 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 70  ating sequence p
255f8 43 6f 6c 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d  Coll.  If pColl=
255f9 3d 30 20 74 68 65 6e 20 72 65 63 6f 6d 70 75 74  =0 then recomput
255fa 65 0a 2a 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73  e.** all indices
255fb 20 65 76 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a   everywhere..*/.
255fc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
255fd 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74  MIT_REINDEX.stat
255fe 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 44  ic void reindexD
255ff 61 74 61 62 61 73 65 73 28 50 61 72 73 65 20 2a  atabases(Parse *
25600 70 50 61 72 73 65 2c 20 63 68 61 72 20 63 6f 6e  pParse, char con
25601 73 74 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62  st *zColl){.  Db
25602 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20 20   *pDb;          
25603 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
25604 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 2a  ingle database *
25605 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
25606 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25607 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
25608 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a  index number */.
25609 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2560a 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a  pParse->db;   /*
2560b 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
2560c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61  nnection */.  Ha
2560d 73 68 45 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20  shElem *k;      
2560e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
2560f 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61   looping over ta
25610 62 6c 65 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20  bles in pDb */. 
25611 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
25612 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25613 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
25614 61 74 61 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f  atabase */..  fo
25615 72 28 69 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d  r(iDb=0, pDb=db-
25616 3e 61 44 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44  >aDb; iDb<db->nD
25617 62 3b 20 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29  b; iDb++, pDb++)
25618 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44  {.    assert( pD
25619 62 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  b!=0 );.    for(
2561a 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  k=sqliteHashFirs
2561b 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t(&pDb->pSchema-
2561c 3e 74 62 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b  >tblHash);  k; k
2561d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
2561e 6b 29 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  k)){.      pTab 
2561f 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65  = (Table*)sqlite
25620 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20  HashData(k);.   
25621 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28     reindexTable(
25622 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 43  pParse, pTab, zC
25623 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  oll);.    }.  }.
25624 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
25625 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
25626 72 20 74 68 65 20 52 45 49 4e 44 45 58 20 63 6f  r the REINDEX co
25627 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  mmand..**.**    
25628 20 20 20 20 52 45 49 4e 44 45 58 20 20 20 20 20      REINDEX     
25629 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2562a 20 20 20 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20         -- 1.**  
2562b 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3c        REINDEX  <
2562c 63 6f 6c 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20  collation>      
2562d 20 20 20 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a           -- 2.**
2562e 20 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20          REINDEX 
2562f 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74   ?<database>.?<t
25630 61 62 6c 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a  ablename>  -- 3.
25631 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
25632 58 20 20 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f  X  ?<database>.?
25633 3c 69 6e 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20  <indexname>  -- 
25634 34 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63  4.**.** Form 1 c
25635 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69 63 65  auses all indice
25636 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  s in all attache
25637 64 20 64 61 74 61 62 61 73 65 73 20 74 6f 20 62  d databases to b
25638 65 20 72 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f  e rebuilt..** Fo
25639 72 6d 20 32 20 72 65 62 75 69 6c 64 73 20 61 6c  rm 2 rebuilds al
2563a 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
2563b 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
2563c 75 73 65 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a  use the named.**
2563d 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
2563e 69 6f 6e 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e  ion.  Forms 3 an
2563f 64 20 34 20 72 65 62 75 69 6c 64 20 74 68 65 20  d 4 rebuild the 
25640 6e 61 6d 65 64 20 69 6e 64 65 78 20 6f 72 20 61  named index or a
25641 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 61 73  ll.** indices as
25642 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
25643 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a  e named table..*
25644 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
25645 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 53 51  _OMIT_REINDEX.SQ
25646 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25647 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  d sqlite3Reindex
25648 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
25649 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54  Token *pName1, T
2564a 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20  oken *pName2){. 
2564b 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
2564c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2564d 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
2564e 63 65 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78  ce to be reindex
2564f 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ed, or NULL */. 
25650 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
25651 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25652 4e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  Name of a table 
25653 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f  or index */.  co
25654 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
25655 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
25656 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
25657 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
25658 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
25659 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
2565a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
2565b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
2565c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2565d 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73 6f 63  * An index assoc
2565e 69 61 74 65 64 20 77 69 74 68 20 70 54 61 62 20  iated with pTab 
2565f 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
25660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25661 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
25662 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f   index number */
25663 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25664 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f   pParse->db;   /
25665 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
25666 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
25667 6f 6b 65 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20  oken *pObjName; 
25668 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
25669 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2566a 6f 72 20 69 6e 64 65 78 20 74 6f 20 62 65 20 72  or index to be r
2566b 65 69 6e 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f  eindexed */..  /
2566c 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62  * Read the datab
2566d 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61  ase schema. If a
2566e 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2566f 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
25670 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20  essage.  ** and 
25671 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61  code in pParse a
25672 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  nd return NULL. 
25673 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
25674 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
25675 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
25676 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
25677 0a 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d  ..  if( pName1==
25678 30 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78  0 ){.    reindex
25679 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65  Databases(pParse
2567a 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
2567b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 4e 45  ;.  }else if( NE
2567c 56 45 52 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c  VER(pName2==0) |
2567d 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29  | pName2->z==0 )
2567e 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  {.    char *zCol
2567f 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  l;.    assert( p
25680 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20  Name1->z );.    
25681 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e  zColl = sqlite3N
25682 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61  ameFromToken(pPa
25683 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29  rse->db, pName1)
25684 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c  ;.    if( !zColl
25685 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 70   ) return;.    p
25686 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
25687 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
25688 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29  C(db), zColl, 0)
25689 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
2568a 29 7b 0a 20 20 20 20 20 20 72 65 69 6e 64 65 78  ){.      reindex
2568b 44 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65  Databases(pParse
2568c 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  , zColl);.      
2568d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2568e 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  , zColl);.      
2568f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
25690 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
25691 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a  db, zColl);.  }.
25692 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54    iDb = sqlite3T
25693 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73  woPartName(pPars
25694 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65  e, pName1, pName
25695 32 2c 20 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20  2, &pObjName);. 
25696 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74   if( iDb<0 ) ret
25697 75 72 6e 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  urn;.  z = sqlit
25698 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
25699 64 62 2c 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20  db, pObjName);. 
2569a 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
2569b 72 6e 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  rn;.  zDb = db->
2569c 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
2569d 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2569e 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 2c  FindTable(db, z,
2569f 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 70 54 61   zDb);.  if( pTa
256a0 62 20 29 7b 0a 20 20 20 20 72 65 69 6e 64 65 78  b ){.    reindex
256a1 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
256a2 61 62 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  ab, 0);.    sqli
256a3 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
256a4 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
256a5 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c  }.  pIndex = sql
256a6 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62  ite3FindIndex(db
256a7 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c  , z, zDb);.  sql
256a8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
256a9 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  );.  if( pIndex 
256aa 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ){.    sqlite3Be
256ab 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
256ac 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
256ad 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
256ae 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65  fillIndex(pParse
256af 2c 20 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20  , pIndex, -1);. 
256b0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
256b1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
256b2 28 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65  (pParse, "unable
256b3 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
256b4 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 72 65   object to be re
256b5 69 6e 64 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e  indexed");.}.#en
256b6 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
256b7 6e 20 61 20 64 79 6e 61 6d 69 63 6c 79 20 61 6c  n a dynamicly al
256b8 6c 6f 63 61 74 65 64 20 4b 65 79 49 6e 66 6f 20  located KeyInfo 
256b9 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 63  structure that c
256ba 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 77 69  an be used.** wi
256bb 74 68 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  th OP_OpenRead o
256bc 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 74  r OP_OpenWrite t
256bd 6f 20 61 63 63 65 73 73 20 64 61 74 61 62 61 73  o access databas
256be 65 20 69 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a  e index pIdx..**
256bf 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
256c0 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  l, a pointer to 
256c1 74 68 65 20 6e 65 77 20 73 74 72 75 63 74 75 72  the new structur
256c2 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  e is returned. I
256c3 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a 20 74  n this case.** t
256c4 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
256c5 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c  ponsible for cal
256c6 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46 72  ling sqlite3DbFr
256c7 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65 20  ee(db, ) on the 
256c8 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 70 6f 69  returned .** poi
256c9 6e 74 65 72 2e 20 49 66 20 61 6e 20 65 72 72 6f  nter. If an erro
256ca 72 20 6f 63 63 75 72 73 20 28 6f 75 74 20 6f 66  r occurs (out of
256cb 20 6d 65 6d 6f 72 79 20 6f 72 20 6d 69 73 73 69   memory or missi
256cc 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a  ng collation .**
256cd 20 73 65 71 75 65 6e 63 65 29 2c 20 4e 55 4c 4c   sequence), NULL
256ce 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
256cf 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 70 50   the state of pP
256d0 61 72 73 65 20 75 70 64 61 74 65 64 20 74 6f 20  arse updated to 
256d1 72 65 66 6c 65 63 74 0a 2a 2a 20 74 68 65 20 65  reflect.** the e
256d2 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
256d3 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20  PRIVATE KeyInfo 
256d4 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79  *sqlite3IndexKey
256d5 69 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61 72  info(Parse *pPar
256d6 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29  se, Index *pIdx)
256d7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
256d8 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43   nCol = pIdx->nC
256d9 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42 79  olumn;.  int nBy
256da 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 4b 65 79  tes = sizeof(Key
256db 49 6e 66 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31 29  Info) + (nCol-1)
256dc 2a 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  *sizeof(CollSeq*
256dd 29 20 2b 20 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69  ) + nCol;.  sqli
256de 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
256df 2d 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66 6f 20  ->db;.  KeyInfo 
256e0 2a 70 4b 65 79 20 3d 20 28 4b 65 79 49 6e 66 6f  *pKey = (KeyInfo
256e1 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
256e2 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65  ocZero(db, nByte
256e3 73 29 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79 20  s);..  if( pKey 
256e4 29 7b 0a 20 20 20 20 70 4b 65 79 2d 3e 64 62 20  ){.    pKey->db 
256e5 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
256e6 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64    pKey->aSortOrd
256e7 65 72 20 3d 20 28 75 38 20 2a 29 26 28 70 4b 65  er = (u8 *)&(pKe
256e8 79 2d 3e 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b  y->aColl[nCol]);
256e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 26 70 4b  .    assert( &pK
256ea 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e  ey->aSortOrder[n
256eb 43 6f 6c 5d 3d 3d 26 28 28 28 75 38 20 2a 29 70  Col]==&(((u8 *)p
256ec 4b 65 79 29 5b 6e 42 79 74 65 73 5d 29 20 29 3b  Key)[nBytes]) );
256ed 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
256ee 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
256ef 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20    char *zColl = 
256f0 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b  pIdx->azColl[i];
256f1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a  .      assert( z
256f2 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 70 4b  Coll );.      pK
256f3 65 79 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73  ey->aColl[i] = s
256f4 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
256f5 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  Seq(pParse, zCol
256f6 6c 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e  l);.      pKey->
256f7 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
256f8 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
256f9 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  [i];.    }.    p
256fa 4b 65 79 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75  Key->nField = (u
256fb 31 36 29 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20  16)nCol;.  }..  
256fc 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
256fd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
256fe 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 29 3b  bFree(db, pKey);
256ff 0a 20 20 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20  .    pKey = 0;. 
25700 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4b 65 79   }.  return pKey
25701 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
25702 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 75 69 6c  **** End of buil
25703 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  d.c ************
25704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25706 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
25707 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
25708 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a  callback.c *****
25709 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2570a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2570b 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20  ***/./*.** 2005 
2570c 4d 61 79 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54 68  May 23 .**.** Th
2570d 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2570e 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2570f 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
25710 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
25711 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
25712 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
25713 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
25714 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
25715 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
25716 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
25717 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
25718 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
25719 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2571a 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2571b 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2571c 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2571d 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2571e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2571f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25721 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25722 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
25723 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
25724 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
25725 6f 20 61 63 63 65 73 73 20 74 68 65 20 69 6e 74  o access the int
25726 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65  ernal hash table
25727 73 0a 2a 2a 20 6f 66 20 75 73 65 72 20 64 65 66  s.** of user def
25728 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61  ined functions a
25729 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  nd collation seq
2572a 75 65 6e 63 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a  uences..*/.../*.
2572b 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 27 63  ** Invoke the 'c
2572c 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64 27  ollation needed'
2572d 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65 71   callback to req
2572e 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  uest a collation
2572f 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e 20   sequence.** in 
25730 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63  the encoding enc
25731 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20   of name zName, 
25732 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2f  length nName..*/
25733 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 6c  .static void cal
25734 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69  lCollNeeded(sqli
25735 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 6e 63  te3 *db, int enc
25736 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
25737 61 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ame){.  assert( 
25738 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64  !db->xCollNeeded
25739 20 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65   || !db->xCollNe
2573a 65 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28 20  eded16 );.  if( 
2573b 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
2573c 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 78  ){.    char *zEx
2573d 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33  ternal = sqlite3
2573e 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61  DbStrDup(db, zNa
2573f 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 45  me);.    if( !zE
25740 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75 72 6e  xternal ) return
25741 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e  ;.    db->xCollN
25742 65 65 64 65 64 28 64 62 2d 3e 70 43 6f 6c 6c 4e  eeded(db->pCollN
25743 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 65 6e  eededArg, db, en
25744 63 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20  c, zExternal);. 
25745 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
25746 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  (db, zExternal);
25747 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
25748 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
25749 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65   if( db->xCollNe
2574a 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63 68  eded16 ){.    ch
2574b 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72  ar const *zExter
2574c 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nal;.    sqlite3
2574d 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73  _value *pTmp = s
2574e 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
2574f 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
25750 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70 2c  alueSetStr(pTmp,
25751 20 2d 31 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49   -1, zName, SQLI
25752 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
25753 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 45 78  STATIC);.    zEx
25754 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33  ternal = sqlite3
25755 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70 2c 20  ValueText(pTmp, 
25756 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
25757 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78  VE);.    if( zEx
25758 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20  ternal ){.      
25759 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31  db->xCollNeeded1
2575a 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65  6(db->pCollNeede
2575b 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29 45  dArg, db, (int)E
2575c 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e 61  NC(db), zExterna
2575d 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  l);.    }.    sq
2575e 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
2575f 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Tmp);.  }.#endif
25760 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
25761 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
25762 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   if the collatio
25763 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c 73 20  n factory fails 
25764 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20  to deliver a.** 
25765 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
25766 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74 20 65  on in the best e
25767 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68 65 72  ncoding but ther
25768 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72 20 76  e may be other v
25769 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68  ersions.** of th
2576a 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  is collation fun
2576b 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72  ction (for other
2576c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 29   text encodings)
2576d 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73 65 20   available. Use 
2576e 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20  one.** of these 
2576f 69 6e 73 74 65 61 64 20 69 66 20 74 68 65 79 20  instead if they 
25770 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61 20 55  exist. Avoid a U
25771 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20  TF-8 <-> UTF-16 
25772 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a  conversion if.**
25773 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74   possible..*/.st
25774 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68 43 6f  atic int synthCo
25775 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20 2a 64  llSeq(sqlite3 *d
25776 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  b, CollSeq *pCol
25777 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  l){.  CollSeq *p
25778 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a  Coll2;.  char *z
25779 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b   = pColl->zName;
2577a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 61 74  .  int i;.  stat
2577b 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45 6e 63  ic const u8 aEnc
2577c 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55 54  [] = { SQLITE_UT
2577d 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54  F16BE, SQLITE_UT
2577e 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55 54  F16LE, SQLITE_UT
2577f 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  F8 };.  for(i=0;
25780 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<3; i++){.    
25781 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65 33  pColl2 = sqlite3
25782 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
25783 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b 0a  aEnc[i], z, 0);.
25784 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e      if( pColl2->
25785 78 43 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20 20  xCmp!=0 ){.     
25786 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70   memcpy(pColl, p
25787 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f  Coll2, sizeof(Co
25788 6c 6c 53 65 71 29 29 3b 0a 20 20 20 20 20 20 70  llSeq));.      p
25789 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20  Coll->xDel = 0; 
2578a 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
2578b 74 20 63 6f 70 79 20 74 68 65 20 64 65 73 74 72  t copy the destr
2578c 75 63 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72  uctor */.      r
2578d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2578e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2578f 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
25790 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
25791 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70  function is resp
25792 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f  onsible for invo
25793 6b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69  king the collati
25794 6f 6e 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62  on factory callb
25795 61 63 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69  ack.** or substi
25796 74 75 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69  tuting a collati
25797 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 61  on sequence of a
25798 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64   different encod
25799 69 6e 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20  ing when the.** 
2579a 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74  requested collat
2579b 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
2579c 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  not available in
2579d 20 74 68 65 20 64 65 73 69 72 65 64 20 65 6e 63   the desired enc
2579e 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  oding..** .** If
2579f 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
257a0 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74   then pColl must
257a1 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61   point to the da
257a2 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65 6e  tabase native en
257a3 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61  coding .** colla
257a4 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
257a5 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c  th name zName, l
257a6 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a  ength nName..**.
257a7 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
257a8 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 74 68  lue is either th
257a9 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
257aa 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64 20  ence to be used 
257ab 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 64  in database.** d
257ac 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20  b for collation 
257ad 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c  type name zName,
257ae 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20 6f   length nName, o
257af 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f  r NULL, if no co
257b0 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65  llation.** seque
257b1 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nce can be found
257b2 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
257b3 3a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43  : sqlite3LocateC
257b4 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74 65  ollSeq(), sqlite
257b5 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 0a 2a  3FindCollSeq().*
257b6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
257b7 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
257b8 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20 20 73  3GetCollSeq(.  s
257b9 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20 20  qlite3* db,     
257ba 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
257bb 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
257bc 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
257bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
257be 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64 69  e desired encodi
257bf 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61  ng for the colla
257c0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
257c1 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
257c2 6c 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c  l,       /* Coll
257c3 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 77  ating sequence w
257c4 69 74 68 20 6e 61 74 69 76 65 20 65 6e 63 6f 64  ith native encod
257c5 69 6e 67 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ing, or NULL */.
257c6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
257c7 61 6d 65 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ame     /* Colla
257c8 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6e 61  ting sequence na
257c9 6d 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53  me */.){.  CollS
257ca 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43  eq *p;..  p = pC
257cb 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29 7b  oll;.  if( !p ){
257cc 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
257cd 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
257ce 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a  enc, zName, 0);.
257cf 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20    }.  if( !p || 
257d0 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20  !p->xCmp ){.    
257d1 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20  /* No collation 
257d2 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73  sequence of this
257d3 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 65   type for this e
257d4 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73  ncoding is regis
257d5 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61  tered..    ** Ca
257d6 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ll the collation
257d7 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20   factory to see 
257d8 69 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79  if it can supply
257d9 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20   us with one..  
257da 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c    */.    callCol
257db 6c 4e 65 65 64 65 64 28 64 62 2c 20 65 6e 63 2c  lNeeded(db, enc,
257dc 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d   zName);.    p =
257dd 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
257de 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61  Seq(db, enc, zNa
257df 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  me, 0);.  }.  if
257e0 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 20  ( p && !p->xCmp 
257e1 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28  && synthCollSeq(
257e2 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 20  db, p) ){.    p 
257e3 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  = 0;.  }.  asser
257e4 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70  t( !p || p->xCmp
257e5 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a   );.  return p;.
257e6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
257e7 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
257e8 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  on a collation s
257e9 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20 69  equence before i
257ea 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20  t is used to.** 
257eb 63 68 65 63 6b 20 74 68 61 74 20 69 74 20 69 73  check that it is
257ec 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64   defined. An und
257ed 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
257ee 20 73 65 71 75 65 6e 63 65 20 65 78 69 73 74 73   sequence exists
257ef 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62   when.** a datab
257f0 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74 68  ase is loaded th
257f1 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66 65  at contains refe
257f2 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74  rences to collat
257f3 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a  ion sequences.**
257f4 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20 62   that have not b
257f5 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20 73  een defined by s
257f6 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
257f7 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a  llation() etc..*
257f8 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65 64  *.** If required
257f9 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63  , this routine c
257fa 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61 74  alls the 'collat
257fb 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c 6c  ion needed' call
257fc 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65  back to.** reque
257fd 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e 20  st a definition 
257fe 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  of the collating
257ff 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74 68   sequence. If th
25800 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c  is doesn't work,
25801 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c 65   .** an equivale
25802 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  nt collating seq
25803 75 65 6e 63 65 20 74 68 61 74 20 75 73 65 73 20  uence that uses 
25804 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  a text encoding 
25805 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f  different.** fro
25806 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  m the main datab
25807 61 73 65 20 69 73 20 73 75 62 73 74 69 74 75 74  ase is substitut
25808 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76  ed, if one is av
25809 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  ailable..*/.SQLI
2580a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2580b 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53  qlite3CheckCollS
2580c 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
2580d 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c  , CollSeq *pColl
2580e 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  ){.  if( pColl )
2580f 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
25810 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d   *zName = pColl-
25811 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 71 6c 69  >zName;.    sqli
25812 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
25813 2d 3e 64 62 3b 0a 20 20 20 20 43 6f 6c 6c 53 65  ->db;.    CollSe
25814 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47 65  q *p = sqlite3Ge
25815 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43  tCollSeq(db, ENC
25816 28 64 62 29 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61  (db), pColl, zNa
25817 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  me);.    if( !p 
25818 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
25819 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2581a 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74   "no such collat
2581b 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73  ion sequence: %s
2581c 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
2581d 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
2581e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2581f 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
25820 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 3d  }.    assert( p=
25821 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20  =pColl );.  }.  
25822 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
25823 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  ;.}..../*.** Loc
25824 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ate and return a
25825 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  n entry from the
25826 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73   db.aCollSeq has
25827 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20  h table. If the 
25828 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69 66 69  entry.** specifi
25829 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e 64 20  ed by zName and 
2582a 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75  nName is not fou
2582b 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72  nd and parameter
2582c 20 27 63 72 65 61 74 65 27 20 69 73 0a 2a 2a 20   'create' is.** 
2582d 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61 74  true, then creat
2582e 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e 20 4f  e a new entry. O
2582f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20  therwise return 
25830 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  NULL..**.** Each
25831 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64 20   pointer stored 
25832 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  in the sqlite3.a
25833 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61 62  CollSeq hash tab
25834 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a  le contains an.*
25835 2a 20 61 72 72 61 79 20 6f 66 20 74 68 72 65 65  * array of three
25836 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75   CollSeq structu
25837 72 65 73 2e 20 54 68 65 20 66 69 72 73 74 20 69  res. The first i
25838 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  s the collation 
25839 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72 65 66  sequence.** pref
2583a 66 65 72 72 65 64 20 66 6f 72 20 55 54 46 2d 38  ferred for UTF-8
2583b 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 55 54 46  , the second UTF
2583c 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68 65 20 74  -16le, and the t
2583d 68 69 72 64 20 55 54 46 2d 31 36 62 65 2e 0a 2a  hird UTF-16be..*
2583e 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d 6d 65  *.** Stored imme
2583f 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68  diately after th
25840 65 20 74 68 72 65 65 20 63 6f 6c 6c 61 74 69 6f  e three collatio
25841 6e 20 73 65 71 75 65 6e 63 65 73 20 69 73 20 61  n sequences is a
25842 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
25843 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
25844 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69 6e 74  ce name. A point
25845 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 69 6e  er to this strin
25846 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a  g is stored in.*
25847 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74 69 6f 6e  * each collation
25848 20 73 65 71 75 65 6e 63 65 20 73 74 72 75 63 74   sequence struct
25849 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43  ure..*/.static C
2584a 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f 6c 6c  ollSeq *findColl
2584b 53 65 71 45 6e 74 72 79 28 0a 20 20 73 71 6c 69  SeqEntry(.  sqli
2584c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2584d 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2584e 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  nnection */.  co
2584f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
25850 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
25851 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
25852 75 65 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 63  uence */.  int c
25853 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
25854 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   /* Create a new
25855 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 2a   entry if true *
25856 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  /.){.  CollSeq *
25857 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e 61  pColl;.  int nNa
25858 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
25859 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  en30(zName);.  p
2585a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61  Coll = sqlite3Ha
2585b 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c  shFind(&db->aCol
2585c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  lSeq, zName, nNa
2585d 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70  me);..  if( 0==p
2585e 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65 20 29  Coll && create )
2585f 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
25860 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
25861 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a  o(db, 3*sizeof(*
25862 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b  pColl) + nName +
25863 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43   1 );.    if( pC
25864 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c  oll ){.      Col
25865 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a  lSeq *pDel = 0;.
25866 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
25867 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  Name = (char*)&p
25868 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70  Coll[3];.      p
25869 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51  Coll[0].enc = SQ
2586a 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20  LITE_UTF8;.     
2586b 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20   pColl[1].zName 
2586c 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b  = (char*)&pColl[
2586d 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  3];.      pColl[
2586e 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  1].enc = SQLITE_
2586f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20 70  UTF16LE;.      p
25870 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20  Coll[2].zName = 
25871 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d  (char*)&pColl[3]
25872 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d  ;.      pColl[2]
25873 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  .enc = SQLITE_UT
25874 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d  F16BE;.      mem
25875 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61  cpy(pColl[0].zNa
25876 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
25877 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  );.      pColl[0
25878 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d  ].zName[nName] =
25879 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20 3d   0;.      pDel =
2587a 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
2587b 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  rt(&db->aCollSeq
2587c 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65  , pColl[0].zName
2587d 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b  , nName, pColl);
2587e 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20  ..      /* If a 
2587f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
25880 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73 71 6c   occurred in sql
25881 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 29  ite3HashInsert()
25882 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20 20  , it will .     
25883 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 70   ** return the p
25884 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20  Coll pointer to 
25885 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 63 61  be deleted (beca
25886 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 61 64  use it wasn't ad
25887 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ded.      ** to 
25888 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 29 2e  the hash table).
25889 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2588a 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 30 20  assert( pDel==0 
2588b 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29  || pDel==pColl )
2588c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 6c  ;.      if( pDel
2588d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64  !=0 ){.        d
2588e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2588f 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 1;.        sql
25890 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
25891 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 70 43  Del);.        pC
25892 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  oll = 0;.      }
25893 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
25894 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a  urn pColl;.}../*
25895 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4e  .** Parameter zN
25896 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ame points to a 
25897 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73 74  UTF-8 encoded st
25898 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65 73  ring nName bytes
25899 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e   long..** Return
2589a 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f   the CollSeq* po
2589b 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f  inter for the co
2589c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2589d 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20   named zName.** 
2589e 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  for the encoding
2589f 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65 20   'enc' from the 
258a0 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a 2a  database 'db'..*
258a1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74 72  *.** If the entr
258a2 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20 6e  y specified is n
258a3 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63 72  ot found and 'cr
258a4 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20 74  eate' is true, t
258a5 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a 20  hen create a.** 
258a6 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68 65  new entry.  Othe
258a7 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c  rwise return NUL
258a8 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70 61 72  L..**.** A separ
258a9 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  ate function sql
258aa 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
258ab 71 28 29 20 69 73 20 61 20 77 72 61 70 70 65 72  q() is a wrapper
258ac 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69 73 20   around.** this 
258ad 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69 74 65  routine.  sqlite
258ae 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
258af 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c   invokes the col
258b0 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 0a 2a  lation factory.*
258b1 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 61  * if necessary a
258b2 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20  nd generates an 
258b3 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66  error message if
258b4 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
258b5 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f  equence.** canno
258b6 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a  t be found..**.*
258b7 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69  * See also: sqli
258b8 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
258b9 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f  (), sqlite3GetCo
258ba 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54  llSeq().*/.SQLIT
258bb 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
258bc 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  q *sqlite3FindCo
258bd 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33  llSeq(.  sqlite3
258be 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a   *db,.  u8 enc,.
258bf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
258c0 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 74  ame,.  int creat
258c1 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  e.){.  CollSeq *
258c2 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61  pColl;.  if( zNa
258c3 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20  me ){.    pColl 
258c4 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74  = findCollSeqEnt
258c5 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 63 72  ry(db, zName, cr
258c6 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  eate);.  }else{.
258c7 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e      pColl = db->
258c8 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  pDfltColl;.  }. 
258c9 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f   assert( SQLITE_
258ca 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49 54  UTF8==1 && SQLIT
258cb 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26 20  E_UTF16LE==2 && 
258cc 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d 3d  SQLITE_UTF16BE==
258cd 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  3 );.  assert( e
258ce 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc>=SQLITE_UTF8 
258cf 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55  && enc<=SQLITE_U
258d0 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20  TF16BE );.  if( 
258d1 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d  pColl ) pColl +=
258d2 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e   enc-1;.  return
258d3 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 75   pColl;.}../* Du
258d4 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68 20  ring the search 
258d5 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75 6e  for the best fun
258d6 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
258d7 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  , this procedure
258d8 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  .** is called to
258d9 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20 74   test how well t
258da 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73  he function pass
258db 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
258dc 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 63  argument.** matc
258dd 68 65 73 20 74 68 65 20 72 65 71 75 65 73 74 20  hes the request 
258de 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77  for a function w
258df 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65 6e  ith nArg argumen
258e0 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a 2a  ts in a system.*
258e1 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e 63 6f  * that uses enco
258e2 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76 61  ding enc. The va
258e3 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64  lue returned ind
258e4 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c 20  icates how well 
258e5 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20 69  the.** request i
258e6 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69 67  s matched. A hig
258e7 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63 61  her value indica
258e8 74 65 73 20 61 20 62 65 74 74 65 72 20 6d 61 74  tes a better mat
258e9 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ch..**.** The re
258ea 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
258eb 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20 30  always between 0
258ec 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c 6f   and 6, as follo
258ed 77 73 3a 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f 74  ws:.**.** 0: Not
258ee 20 61 20 6d 61 74 63 68 2c 20 6f 72 20 69 66 20   a match, or if 
258ef 6e 41 72 67 3c 30 20 61 6e 64 20 74 68 65 20 66  nArg<0 and the f
258f0 75 6e 63 74 69 6f 6e 20 69 73 20 68 61 73 20 6e  unction is has n
258f1 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  o implementation
258f2 2e 0a 2a 2a 20 31 3a 20 41 20 76 61 72 69 61 62  ..** 1: A variab
258f3 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e  le arguments fun
258f4 63 74 69 6f 6e 20 74 68 61 74 20 70 72 65 66 65  ction that prefe
258f5 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61 20  rs UTF-8 when a 
258f6 55 54 46 2d 31 36 0a 2a 2a 20 20 20 20 65 6e 63  UTF-16.**    enc
258f7 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74  oding is request
258f8 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
258f9 61 2e 0a 2a 2a 20 32 3a 20 41 20 76 61 72 69 61  a..** 2: A varia
258fa 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75  ble arguments fu
258fb 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73  nction that uses
258fc 20 55 54 46 2d 31 36 42 45 20 77 68 65 6e 20 55   UTF-16BE when U
258fd 54 46 2d 31 36 4c 45 20 69 73 0a 2a 2a 20 20 20  TF-16LE is.**   
258fe 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76   requested, or v
258ff 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 33 3a  ice versa..** 3:
25900 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75   A variable argu
25901 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 75  ments function u
25902 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74 65  sing the same te
25903 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20  xt encoding..** 
25904 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69  4: A function wi
25905 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d  th the exact num
25906 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
25907 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 0a   requested that.
25908 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 54  **    prefers UT
25909 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31  F-8 when a UTF-1
2590a 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65  6 encoding is re
2590b 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65  quested, or vice
2590c 20 76 65 72 73 61 2e 0a 2a 2a 20 35 3a 20 41 20   versa..** 5: A 
2590d 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2590e 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f  e exact number o
2590f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75  f arguments requ
25910 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20  ested that.**   
25911 20 70 72 65 66 65 72 73 20 55 54 46 2d 31 36 4c   prefers UTF-16L
25912 45 20 77 68 65 6e 20 55 54 46 2d 31 36 42 45 20  E when UTF-16BE 
25913 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72  is requested, or
25914 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20   vice versa..** 
25915 36 3a 20 41 6e 20 65 78 61 63 74 20 6d 61 74 63  6: An exact matc
25916 68 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20  h..**.*/.static 
25917 69 6e 74 20 6d 61 74 63 68 51 75 61 6c 69 74 79  int matchQuality
25918 28 46 75 6e 63 44 65 66 20 2a 70 2c 20 69 6e 74  (FuncDef *p, int
25919 20 6e 41 72 67 2c 20 75 38 20 65 6e 63 29 7b 0a   nArg, u8 enc){.
2591a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b    int match = 0;
2591b 0a 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d  .  if( p->nArg==
2591c 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d 6e  -1 || p->nArg==n
2591d 41 72 67 20 0a 20 20 20 7c 7c 20 28 6e 41 72 67  Arg .   || (nArg
2591e 3d 3d 2d 31 20 26 26 20 28 70 2d 3e 78 46 75 6e  ==-1 && (p->xFun
2591f 63 21 3d 30 20 7c 7c 20 70 2d 3e 78 53 74 65 70  c!=0 || p->xStep
25920 21 3d 30 29 29 0a 20 20 29 7b 0a 20 20 20 20 6d  !=0)).  ){.    m
25921 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69 66  atch = 1;.    if
25922 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20  ( p->nArg==nArg 
25923 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20  || nArg==-1 ){. 
25924 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a       match = 4;.
25925 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e      }.    if( en
25926 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 29  c==p->iPrefEnc )
25927 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d  {.      match +=
25928 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c   2;.    }.    el
25929 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c  se if( (enc==SQL
2592a 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20 70  ITE_UTF16LE && p
2592b 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49  ->iPrefEnc==SQLI
2592c 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a 20  TE_UTF16BE) ||. 
2592d 20 20 20 20 20 20 20 20 20 20 20 20 28 65 6e 63              (enc
2592e 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
2592f 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d   && p->iPrefEnc=
25930 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29  =SQLITE_UTF16LE)
25931 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20   ){.      match 
25932 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  += 1;.    }.  }.
25933 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a    return match;.
25934 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20  }../*.** Search 
25935 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 66 6f  a FuncDefHash fo
25936 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  r a function wit
25937 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65  h the given name
25938 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70  .  Return.** a p
25939 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 61  ointer to the ma
2593a 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20 69  tching FuncDef i
2593b 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66  f found, or 0 if
2593c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
2593d 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75  ch..*/.static Fu
2593e 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53  ncDef *functionS
2593f 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65 66  earch(.  FuncDef
25940 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a  Hash *pHash,  /*
25941 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20 73   Hash table to s
25942 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68  earch */.  int h
25943 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
25944 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20 6e  /* Hash of the n
25945 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
25946 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a  har *zFunc,   /*
25947 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f   Name of functio
25948 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63  n */.  int nFunc
25949 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2594a 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
2594b 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20  n zFunc */.){.  
2594c 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f  FuncDef *p;.  fo
2594d 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b  r(p=pHash->a[h];
2594e 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b   p; p=p->pHash){
2594f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
25950 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d  StrNICmp(p->zNam
25951 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29  e, zFunc, nFunc)
25952 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b  ==0 && p->zName[
25953 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20  nFunc]==0 ){.   
25954 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20     return p;.   
25955 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
25956 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  0;.}../*.** Inse
25957 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65 66  rt a new FuncDef
25958 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 48   into a FuncDefH
25959 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ash hash table..
2595a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2595b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75  E void sqlite3Fu
2595c 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20 46  ncDefInsert(.  F
2595d 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73  uncDefHash *pHas
2595e 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20  h,  /* The hash 
2595f 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
25960 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20   to insert */.  
25961 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20 20  FuncDef *pDef   
25962 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63       /* The func
25963 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
25964 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a  to insert */.){.
25965 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68 65    FuncDef *pOthe
25966 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  r;.  int nName =
25967 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
25968 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  (pDef->zName);. 
25969 20 75 38 20 63 31 20 3d 20 28 75 38 29 70 44 65   u8 c1 = (u8)pDe
2596a 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20 69  f->zName[0];.  i
2596b 6e 74 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55  nt h = (sqlite3U
2596c 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31 5d 20  pperToLower[c1] 
2596d 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79  + nName) % Array
2596e 53 69 7a 65 28 70 48 61 73 68 2d 3e 61 29 3b 0a  Size(pHash->a);.
2596f 20 20 70 4f 74 68 65 72 20 3d 20 66 75 6e 63 74    pOther = funct
25970 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c  ionSearch(pHash,
25971 20 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c   h, pDef->zName,
25972 20 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70   nName);.  if( p
25973 4f 74 68 65 72 20 29 7b 0a 20 20 20 20 61 73 73  Other ){.    ass
25974 65 72 74 28 20 70 4f 74 68 65 72 21 3d 70 44 65  ert( pOther!=pDe
25975 66 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65  f && pOther->pNe
25976 78 74 21 3d 70 44 65 66 20 29 3b 0a 20 20 20 20  xt!=pDef );.    
25977 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 70 4f  pDef->pNext = pO
25978 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ther->pNext;.   
25979 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d   pOther->pNext =
2597a 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pDef;.  }else{.
2597b 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 20      pDef->pNext 
2597c 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d 3e 70  = 0;.    pDef->p
2597d 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e 61 5b  Hash = pHash->a[
2597e 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d 3e 61  h];.    pHash->a
2597f 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20 7d 0a  [h] = pDef;.  }.
25980 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c  }.  .  ../*.** L
25981 6f 63 61 74 65 20 61 20 75 73 65 72 20 66 75 6e  ocate a user fun
25982 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61  ction given a na
25983 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  me, a number of 
25984 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 61 20  arguments and a 
25985 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69  flag.** indicati
25986 6e 67 20 77 68 65 74 68 65 72 20 74 68 65 20 66  ng whether the f
25987 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20  unction prefers 
25988 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54 46 2d  UTF-16 over UTF-
25989 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20  8.  Return a.** 
2598a 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
2598b 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
2598c 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
2598d 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20  at function, or 
2598e 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69  return.** NULL i
2598f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  f the function d
25990 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a  oes not exist..*
25991 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 65 61  *.** If the crea
25992 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20  teFlag argument 
25993 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 20  is true, then a 
25994 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63  new (blank) Func
25995 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  Def.** structure
25996 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20   is created and 
25997 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 22  liked into the "
25998 64 62 22 20 73 74 72 75 63 74 75 72 65 20 69 66  db" structure if
25999 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e   a.** no matchin
2599a 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 76 69  g function previ
2599b 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20  ously existed.  
2599c 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20  When createFlag 
2599d 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74  is true.** and t
2599e 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65  he nArg paramete
2599f 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e  r is -1, then on
259a0 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  ly a function th
259a1 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e  at accepts.** an
259a2 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  y number of argu
259a3 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 72 65  ments will be re
259a4 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
259a5 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66   createFlag is f
259a6 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69 73  alse and nArg is
259a7 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66 69   -1, then the fi
259a8 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e  rst valid.** fun
259a9 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72  ction found is r
259aa 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e 63  eturned.  A func
259ab 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69 66  tion is valid if
259ac 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a   either xFunc.**
259ad 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f 6e   or xStep is non
259ae 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  -zero..**.** If 
259af 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61  createFlag is fa
259b0 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 6e 63  lse, then a func
259b1 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65  tion with the re
259b2 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a  quired name and.
259b3 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ** number of arg
259b4 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 72 65  uments may be re
259b5 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 74  turned even if t
259b6 68 65 20 65 54 65 78 74 52 65 70 20 66 6c 61 67  he eTextRep flag
259b7 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74   does not.** mat
259b8 63 68 20 74 68 61 74 20 72 65 71 75 65 73 74 65  ch that requeste
259b9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
259ba 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71  VATE FuncDef *sq
259bb 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
259bc 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
259bd 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70  ,       /* An op
259be 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  en database */. 
259bf 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
259c0 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  me, /* Name of t
259c1 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f  he function.  No
259c2 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  t null-terminate
259c3 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  d */.  int nName
259c4 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ,         /* Num
259c5 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
259c6 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f  s in the name */
259c7 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20  .  int nArg,    
259c8 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
259c9 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 2d  of arguments.  -
259ca 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d 62  1 means any numb
259cb 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20  er */.  u8 enc, 
259cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
259cd 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e 63  eferred text enc
259ce 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63  oding */.  int c
259cf 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 2f 2a  reateFlag     /*
259d0 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e 74 72   Create new entr
259d1 79 20 69 66 20 74 72 75 65 20 61 6e 64 20 64 6f  y if true and do
259d2 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 65  es not otherwise
259d3 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46   exist */.){.  F
259d4 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20 20  uncDef *p;      
259d5 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
259d6 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75 6e  ariable */.  Fun
259d7 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20 30 3b  cDef *pBest = 0;
259d8 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 20 66   /* Best match f
259d9 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ound so far */. 
259da 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65 20 3d   int bestScore =
259db 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 6f 66   0;  /* Score of
259dc 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20   best match */. 
259dd 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
259de 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c       /* Hash val
259df 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74  ue */...  assert
259e0 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ( enc==SQLITE_UT
259e1 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54  F8 || enc==SQLIT
259e2 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65 6e 63  E_UTF16LE || enc
259e3 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
259e4 20 29 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69 74   );.  h = (sqlit
259e5 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
259e6 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e  u8)zName[0]] + n
259e7 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a  Name) % ArraySiz
259e8 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a  e(db->aFunc.a);.
259e9 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72  .  /* First sear
259ea 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20 61  ch for a match a
259eb 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c 69  mongst the appli
259ec 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
259ed 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  unctions..  */. 
259ee 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61   p = functionSea
259ef 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20  rch(&db->aFunc, 
259f0 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
259f1 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a  ;.  while( p ){.
259f2 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20      int score = 
259f3 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20  matchQuality(p, 
259f4 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20  nArg, enc);.    
259f5 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53 63  if( score>bestSc
259f6 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42 65  ore ){.      pBe
259f7 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62 65  st = p;.      be
259f8 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b  stScore = score;
259f9 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70  .    }.    p = p
259fa 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20  ->pNext;.  }..  
259fb 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69  /* If no match i
259fc 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20  s found, search 
259fd 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  the built-in fun
259fe 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ctions..  **.  *
259ff 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72 65  * Except, if cre
25a00 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 2c  ateFlag is true,
25a01 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
25a02 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
25a03 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61  o.  ** install a
25a04 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20   new function.  
25a05 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65 66  Whatever FuncDef
25a06 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
25a07 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a  turned will.  **
25a08 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76 65   have fields ove
25a09 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e 65  rwritten with ne
25a0a 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70  w information ap
25a0b 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
25a0c 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74  e.  ** new funct
25a0d 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46 75  ion.  But the Fu
25a0e 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c 74  ncDefs for built
25a0f 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  -in functions ar
25a10 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a  e read-only..  *
25a11 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74  * So we must not
25a12 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69 6c   search for buil
25a13 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61 74  t-ins when creat
25a14 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74 69  ing a new functi
25a15 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20  on..  */ .  if( 
25a16 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 21  !createFlag && !
25a17 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75 6e  pBest ){.    Fun
25a18 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
25a19 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
25a1a 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
25a1b 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
25a1c 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e      p = function
25a1d 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c  Search(pHash, h,
25a1e 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
25a1f 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a      while( p ){.
25a20 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20        int score 
25a21 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70  = matchQuality(p
25a22 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20  , nArg, enc);.  
25a23 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65      if( score>be
25a24 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20 20  stScore ){.     
25a25 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20 20     pBest = p;.  
25a26 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65 20        bestScore 
25a27 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d  = score;.      }
25a28 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e  .      p = p->pN
25a29 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ext;.    }.  }..
25a2a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65 61    /* If the crea
25a2b 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72  teFlag parameter
25a2c 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68 65   is true and the
25a2d 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20   search did not 
25a2e 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65  reveal an.  ** e
25a2f 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74  xact match for t
25a30 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20  he name, number 
25a31 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64  of arguments and
25a32 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20   encoding, then 
25a33 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65  add a.  ** new e
25a34 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73 68  ntry to the hash
25a35 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72   table and retur
25a36 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  n it..  */.  if(
25a37 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20 28   createFlag && (
25a38 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70  bestScore<6 || p
25a39 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67  Best->nArg!=nArg
25a3a 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42 65  ) && .      (pBe
25a3b 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
25a3c 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
25a3d 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d  eof(*pBest)+nNam
25a3e 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  e+1))!=0 ){.    
25a3f 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28  pBest->zName = (
25a40 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31 5d  char *)&pBest[1]
25a41 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41 72  ;.    pBest->nAr
25a42 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a 20  g = (u16)nArg;. 
25a43 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65 66 45     pBest->iPrefE
25a44 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20 6d 65  nc = enc;.    me
25a45 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61 6d  mcpy(pBest->zNam
25a46 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  e, zName, nName)
25a47 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61  ;.    pBest->zNa
25a48 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20  me[nName] = 0;. 
25a49 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
25a4a 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 46 75  fInsert(&db->aFu
25a4b 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20 7d 0a  nc, pBest);.  }.
25a4c 0a 20 20 69 66 28 20 70 42 65 73 74 20 26 26 20  .  if( pBest && 
25a4d 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20 7c 7c  (pBest->xStep ||
25a4e 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20 7c 7c   pBest->xFunc ||
25a4f 20 63 72 65 61 74 65 46 6c 61 67 29 20 29 7b 0a   createFlag) ){.
25a50 20 20 20 20 72 65 74 75 72 6e 20 70 42 65 73 74      return pBest
25a51 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
25a52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
25a53 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 68 65  all resources he
25a54 6c 64 20 62 79 20 74 68 65 20 73 63 68 65 6d 61  ld by the schema
25a55 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 20   structure. The 
25a56 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74 20 70  void* argument p
25a57 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20 53 63  oints.** at a Sc
25a58 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54 68 69  hema struct. Thi
25a59 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  s function does 
25a5a 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  not call sqlite3
25a5b 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20  DbFree(db, ) on 
25a5c 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  the .** pointer 
25a5d 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73 74 20  itself, it just 
25a5e 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73 69 64  cleans up subsid
25a5f 75 61 72 79 20 72 65 73 6f 75 72 63 65 73 20 28  uary resources (
25a60 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65 6e 74  i.e. the content
25a61 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65  s.** of the sche
25a62 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 29 2e  ma hash tables).
25a63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68 65 6d  .**.** The Schem
25a64 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76 61 72  a.cache_size var
25a65 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 6c 65  iable is not cle
25a66 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ared..*/.SQLITE_
25a67 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
25a68 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 76  ite3SchemaFree(v
25a69 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73 68 20  oid *p){.  Hash 
25a6a 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20 74 65  temp1;.  Hash te
25a6b 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20  mp2;.  HashElem 
25a6c 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65 6d 61  *pElem;.  Schema
25a6d 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63 68   *pSchema = (Sch
25a6e 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65 6d 70  ema *)p;..  temp
25a6f 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 62 6c  1 = pSchema->tbl
25a70 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20 3d 20  Hash;.  temp2 = 
25a71 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
25a72 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  h;.  sqlite3Hash
25a73 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e 74  Init(&pSchema->t
25a74 72 69 67 48 61 73 68 29 3b 0a 20 20 73 71 6c 69  rigHash);.  sqli
25a75 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53  te3HashClear(&pS
25a76 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b  chema->idxHash);
25a77 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
25a78 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
25a79 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp2); pElem; pEl
25a7a 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
25a7b 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73  t(pElem)){.    s
25a7c 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
25a7d 67 65 72 28 30 2c 20 28 54 72 69 67 67 65 72 2a  ger(0, (Trigger*
25a7e 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
25a7f 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73  pElem));.  }.  s
25a80 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
25a81 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74  &temp2);.  sqlit
25a82 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63 68  e3HashInit(&pSch
25a83 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20  ema->tblHash);. 
25a84 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74   for(pElem=sqlit
25a85 65 48 61 73 68 46 69 72 73 74 28 26 74 65 6d 70  eHashFirst(&temp
25a86 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d  1); pElem; pElem
25a87 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
25a88 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61 62  pElem)){.    Tab
25a89 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
25a8a 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29  eHashData(pElem)
25a8b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
25a8c 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a  ab->dbMem==0 );.
25a8d 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
25a8e 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20  eTable(pTab);.  
25a8f 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  }.  sqlite3HashC
25a90 6c 65 61 72 28 26 74 65 6d 70 31 29 3b 0a 20 20  lear(&temp1);.  
25a91 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
25a92 28 26 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48  (&pSchema->fkeyH
25a93 61 73 68 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d  ash);.  pSchema-
25a94 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a 20 20  >pSeqTab = 0;.  
25a95 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26  pSchema->flags &
25a96 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  = ~DB_SchemaLoad
25a97 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  ed;.}../*.** Fin
25a98 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  d and return the
25a99 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
25a9a 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e  ed with a BTree.
25a9b 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65    Create.** a ne
25a9c 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61  w one if necessa
25a9d 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
25a9e 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71  IVATE Schema *sq
25a9f 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73  lite3SchemaGet(s
25aa0 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 72 65  qlite3 *db, Btre
25aa1 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68 65 6d  e *pBt){.  Schem
25aa2 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70 42 74  a * p;.  if( pBt
25aa3 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68   ){.    p = (Sch
25aa4 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42 74 72  ema *)sqlite3Btr
25aa5 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20 73 69  eeSchema(pBt, si
25aa6 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20 73 71  zeof(Schema), sq
25aa7 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 29  lite3SchemaFree)
25aa8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
25aa9 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c   = (Schema *)sql
25aaa 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
25aab 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a  izeof(Schema));.
25aac 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29 7b 0a    }.  if( !p ){.
25aad 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
25aae 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73  iled = 1;.  }els
25aaf 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c  e if ( 0==p->fil
25ab0 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20  e_format ){.    
25ab1 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
25ab2 26 70 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20  &p->tblHash);.  
25ab3 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
25ab4 74 28 26 70 2d 3e 69 64 78 48 61 73 68 29 3b 0a  t(&p->idxHash);.
25ab5 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
25ab6 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61 73 68  nit(&p->trigHash
25ab7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  );.    sqlite3Ha
25ab8 73 68 49 6e 69 74 28 26 70 2d 3e 66 6b 65 79 48  shInit(&p->fkeyH
25ab9 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 65 6e 63  ash);.    p->enc
25aba 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
25abb 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
25abc 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
25abd 2a 2a 20 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61  ** End of callba
25abe 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ck.c ***********
25abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ac1 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
25ac2 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 65  ** Begin file de
25ac3 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lete.c *********
25ac4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ac5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ac6 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
25ac7 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
25ac8 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
25ac9 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
25aca 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
25acb 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
25acc 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
25acd 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
25ace 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
25acf 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
25ad0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
25ad1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
25ad2 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
25ad3 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
25ad4 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
25ad5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
25ad6 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
25ad7 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
25ad8 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
25ad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25adb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25adc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25add 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
25ade 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
25adf 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20  C code routines 
25ae0 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20  that are called 
25ae1 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  by the parser.**
25ae2 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e   in order to gen
25ae3 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 44  erate code for D
25ae4 45 4c 45 54 45 20 46 52 4f 4d 20 73 74 61 74 65  ELETE FROM state
25ae5 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ments..*/../*.**
25ae6 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74   Look up every t
25ae7 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d  able that is nam
25ae8 65 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66 20  ed in pSrc.  If 
25ae9 61 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  any table is not
25aea 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20 61   found,.** add a
25aeb 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
25aec 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
25aed 73 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55  sg and return NU
25aee 4c 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62 6c  LL.  If all tabl
25aef 65 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64 2c  es.** are found,
25af0 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
25af1 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74 61  r to the last ta
25af2 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
25af3 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
25af4 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
25af5 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  up(Parse *pParse
25af6 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29  , SrcList *pSrc)
25af7 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  {.  struct SrcLi
25af8 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
25af9 20 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61 62 6c   pSrc->a;.  Tabl
25afa 65 20 2a 70 54 61 62 3b 0a 20 20 61 73 73 65 72  e *pTab;.  asser
25afb 74 28 20 70 49 74 65 6d 20 26 26 20 70 53 72 63  t( pItem && pSrc
25afc 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70  ->nSrc==1 );.  p
25afd 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
25afe 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
25aff 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   0, pItem->zName
25b00 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
25b01 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  se);.  sqlite3De
25b02 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d  leteTable(pItem-
25b03 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65 6d 2d  >pTab);.  pItem-
25b04 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  >pTab = pTab;.  
25b05 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
25b06 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  pTab->nRef++;.  
25b07 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  }.  if( sqlite3I
25b08 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70  ndexedByLookup(p
25b09 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20 29 7b  Parse, pItem) ){
25b0a 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20  .    pTab = 0;. 
25b0b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62   }.  return pTab
25b0c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ;.}../*.** Check
25b0d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
25b0e 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 69 73  e given table is
25b0f 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69   writable.  If i
25b10 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72 69 74  t is not.** writ
25b11 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65 20 61  able, generate a
25b12 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
25b13 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20 20 49  and return 1.  I
25b14 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69 74 61  f it is.** writa
25b15 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2f  ble return 0;.*/
25b16 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25b17 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65 61  int sqlite3IsRea
25b18 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70 50 61  dOnly(Parse *pPa
25b19 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
25b1a 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b 0a 20  , int viewOk){. 
25b1b 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 73 20 6e   /* A table is n
25b1c 6f 74 20 77 72 69 74 61 62 6c 65 20 75 6e 64 65  ot writable unde
25b1d 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
25b1e 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 20  circumstances:. 
25b1f 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 29 20 49 74   **.  **   1) It
25b20 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61   is a virtual ta
25b21 62 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70 6c 65  ble and no imple
25b22 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
25b23 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 0a   xUpdate method.
25b24 20 20 2a 2a 20 20 20 20 20 20 68 61 73 20 62 65    **      has be
25b25 65 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f 72 0a  en provided, or.
25b26 20 20 2a 2a 20 20 20 32 29 20 49 74 20 69 73 20    **   2) It is 
25b27 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 28  a system table (
25b28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74  i.e. sqlite_mast
25b29 65 72 29 2c 20 74 68 69 73 20 63 61 6c 6c 20 69  er), this call i
25b2a 73 20 6e 6f 74 0a 20 20 2a 2a 20 20 20 20 20 20  s not.  **      
25b2b 70 61 72 74 20 6f 66 20 61 20 6e 65 73 74 65 64  part of a nested
25b2c 20 70 61 72 73 65 20 61 6e 64 20 77 72 69 74 61   parse and writa
25b2d 62 6c 65 5f 73 63 68 65 6d 61 20 70 72 61 67 6d  ble_schema pragm
25b2e 61 20 68 61 73 20 6e 6f 74 20 0a 20 20 2a 2a 20  a has not .  ** 
25b2f 20 20 20 20 20 62 65 65 6e 20 73 70 65 63 69 66       been specif
25b30 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ied..  **.  ** I
25b31 6e 20 65 69 74 68 65 72 20 63 61 73 65 20 6c 65  n either case le
25b32 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
25b33 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 20 61  sage in pParse a
25b34 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  nd return non-ze
25b35 72 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ro..  */.  if( (
25b36 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
25b37 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65   .     && sqlite
25b38 33 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73  3GetVTable(pPars
25b39 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70 4d  e->db, pTab)->pM
25b3a 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70  od->pModule->xUp
25b3b 64 61 74 65 3d 3d 30 20 29 0a 20 20 20 7c 7c 20  date==0 ).   || 
25b3c 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  ( (pTab->tabFlag
25b3d 73 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29  s & TF_Readonly)
25b3e 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 50 61  !=0.     && (pPa
25b3f 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
25b40 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
25b41 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 26 26 20  ema)==0.     && 
25b42 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
25b43 30 20 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c  0 ).  ){.    sql
25b44 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
25b45 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d  rse, "table %s m
25b46 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69  ay not be modifi
25b47 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
25b48 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
25b49 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
25b4a 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
25b4b 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26 26 20   if( !viewOk && 
25b4c 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
25b4d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
25b4e 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63 61 6e  rMsg(pParse,"can
25b4f 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 62 65  not modify %s be
25b50 63 61 75 73 65 20 69 74 20 69 73 20 61 20 76 69  cause it is a vi
25b51 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  ew",pTab->zName)
25b52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
25b53 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
25b54 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21  urn 0;.}...#if !
25b55 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
25b56 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65  MIT_VIEW) && !de
25b57 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
25b58 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a  T_TRIGGER)./*.**
25b59 20 45 76 61 6c 75 61 74 65 20 61 20 76 69 65 77   Evaluate a view
25b5a 20 61 6e 64 20 73 74 6f 72 65 20 69 74 73 20 72   and store its r
25b5b 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 70 68 65  esult in an ephe
25b5c 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  meral table.  Th
25b5d 65 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67 75  e.** pWhere argu
25b5e 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69 6f  ment is an optio
25b5f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
25b60 20 74 68 61 74 20 72 65 73 74 72 69 63 74 73 20   that restricts 
25b61 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f  the.** set of ro
25b62 77 73 20 69 6e 20 74 68 65 20 76 69 65 77 20 74  ws in the view t
25b63 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 61 64  hat are to be ad
25b64 64 65 64 20 74 6f 20 74 68 65 20 65 70 68 65 6d  ded to the ephem
25b65 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  eral table..*/.S
25b66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25b67 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69  id sqlite3Materi
25b68 61 6c 69 7a 65 56 69 65 77 28 0a 20 20 50 61 72  alizeView(.  Par
25b69 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
25b6a 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
25b6b 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
25b6c 2a 70 56 69 65 77 2c 20 20 20 20 20 20 20 20 2f  *pView,        /
25b6d 2a 20 56 69 65 77 20 64 65 66 69 6e 69 74 69 6f  * View definitio
25b6e 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
25b6f 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  ere,        /* O
25b70 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c  ptional WHERE cl
25b71 61 75 73 65 20 74 6f 20 62 65 20 61 64 64 65 64  ause to be added
25b72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 20   */.  int iCur  
25b73 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
25b74 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
25b75 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65  ephemerial table
25b76 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 44   */.){.  SelectD
25b77 65 73 74 20 64 65 73 74 3b 0a 20 20 53 65 6c 65  est dest;.  Sele
25b78 63 74 20 2a 70 44 75 70 3b 0a 20 20 73 71 6c 69  ct *pDup;.  sqli
25b79 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
25b7a 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20 3d 20  ->db;..  pDup = 
25b7b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
25b7c 28 64 62 2c 20 70 56 69 65 77 2d 3e 70 53 65 6c  (db, pView->pSel
25b7d 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ect, 0);.  if( p
25b7e 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72 63  Where ){.    Src
25b7f 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20  List *pFrom;.   
25b80 20 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73   .    pWhere = s
25b81 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
25b82 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20  , pWhere, 0);.  
25b83 20 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65    pFrom = sqlite
25b84 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64  3SrcListAppend(d
25b85 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  b, 0, 0, 0);.   
25b86 20 69 66 28 20 70 46 72 6f 6d 20 29 7b 0a 20 20   if( pFrom ){.  
25b87 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
25b88 6d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  m->nSrc==1 );.  
25b89 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e      pFrom->a[0].
25b8a 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
25b8b 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 56 69  DbStrDup(db, pVi
25b8c 65 77 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ew->zName);.    
25b8d 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 53    pFrom->a[0].pS
25b8e 65 6c 65 63 74 20 3d 20 70 44 75 70 3b 0a 20 20  elect = pDup;.  
25b8f 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
25b90 6d 2d 3e 61 5b 30 5d 2e 70 4f 6e 3d 3d 30 20 29  m->a[0].pOn==0 )
25b91 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
25b92 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 55 73 69  pFrom->a[0].pUsi
25b93 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ng==0 );.    }el
25b94 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
25b95 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
25b96 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 7d 0a 20  , pDup);.    }. 
25b97 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
25b98 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
25b99 65 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68  e, 0, pFrom, pWh
25b9a 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  ere, 0, 0, 0, 0,
25b9b 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71   0, 0);.  }.  sq
25b9c 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
25b9d 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45  nit(&dest, SRT_E
25b9e 70 68 65 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a  phemTab, iCur);.
25b9f 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
25ba0 70 50 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64  pParse, pDup, &d
25ba1 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  est);.  sqlite3S
25ba2 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
25ba3 70 44 75 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  pDup);.}.#endif 
25ba4 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
25ba5 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26  TE_OMIT_VIEW) &&
25ba6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
25ba7 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a  _OMIT_TRIGGER) *
25ba8 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  /..#if defined(S
25ba9 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
25baa 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
25bab 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
25bac 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
25bad 52 59 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  RY)./*.** Genera
25bae 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  te an expression
25baf 20 74 72 65 65 20 74 6f 20 69 6d 70 6c 65 6d 65   tree to impleme
25bb0 6e 74 20 74 68 65 20 57 48 45 52 45 2c 20 4f 52  nt the WHERE, OR
25bb1 44 45 52 20 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c  DER BY,.** and L
25bb2 49 4d 49 54 2f 4f 46 46 53 45 54 20 70 6f 72 74  IMIT/OFFSET port
25bb3 69 6f 6e 20 6f 66 20 44 45 4c 45 54 45 20 61 6e  ion of DELETE an
25bb4 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  d UPDATE stateme
25bb5 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  nts..**.**     D
25bb6 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
25bb7 5f 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20  _wxyz WHERE a<5 
25bb8 4f 52 44 45 52 20 42 59 20 61 20 4c 49 4d 49 54  ORDER BY a LIMIT
25bb9 20 31 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   1;.**          
25bba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25bbb 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
25bbc 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
25bbd 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
25bbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25bbf 70 4c 69 6d 69 74 57 68 65 72 65 20 28 70 49 6e  pLimitWhere (pIn
25bc0 43 6c 61 75 73 65 29 0a 2a 2f 0a 53 51 4c 49 54  Clause).*/.SQLIT
25bc1 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
25bc2 73 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72  sqlite3LimitWher
25bc3 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
25bc4 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
25bc5 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
25bc6 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
25bc7 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
25bc8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
25bc9 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20   FROM clause -- 
25bca 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20  which tables to 
25bcb 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
25bcc 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
25bcd 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
25bce 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ERE clause.  May
25bcf 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78   be null */.  Ex
25bd0 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
25bd1 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
25bd2 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
25bd3 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
25bd4 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69  */.  Expr *pLimi
25bd5 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
25bd6 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
25bd7 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e  lause.  May be n
25bd8 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ull */.  Expr *p
25bd9 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
25bda 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 46 46        /* The OFF
25bdb 53 45 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  SET clause.  May
25bdc 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68   be null */.  ch
25bdd 61 72 20 2a 7a 53 74 6d 74 54 79 70 65 20 20 20  ar *zStmtType   
25bde 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69             /* Ei
25bdf 74 68 65 72 20 44 45 4c 45 54 45 20 6f 72 20 55  ther DELETE or U
25be0 50 44 41 54 45 2e 20 20 46 6f 72 20 65 72 72 6f  PDATE.  For erro
25be1 72 20 6d 65 73 73 61 67 65 73 2e 20 2a 2f 0a 29  r messages. */.)
25be2 7b 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  {.  Expr *pWhere
25be3 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20  Rowid = NULL;   
25be4 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20   /* WHERE rowid 
25be5 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 49  .. */.  Expr *pI
25be6 6e 43 6c 61 75 73 65 20 3d 20 4e 55 4c 4c 3b 20  nClause = NULL; 
25be7 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72 6f       /* WHERE ro
25be8 77 69 64 20 49 4e 20 28 20 73 65 6c 65 63 74 20  wid IN ( select 
25be9 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 65  ) */.  Expr *pSe
25bea 6c 65 63 74 52 6f 77 69 64 20 3d 20 4e 55 4c 4c  lectRowid = NULL
25beb 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f  ;   /* SELECT ro
25bec 77 69 64 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70  wid ... */.  Exp
25bed 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20  rList *pEList = 
25bee 4e 55 4c 4c 3b 20 20 20 20 20 2f 2a 20 45 78 70  NULL;     /* Exp
25bef 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e  ression list con
25bf0 74 61 6e 69 6e 67 20 6f 6e 6c 79 20 70 53 65 6c  taning only pSel
25bf1 65 63 74 52 6f 77 69 64 20 2a 2f 0a 20 20 53 72  ectRowid */.  Sr
25bf2 63 4c 69 73 74 20 2a 70 53 65 6c 65 63 74 53 72  cList *pSelectSr
25bf3 63 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45  c = NULL;  /* SE
25bf4 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20  LECT rowid FROM 
25bf5 78 20 2e 2e 2e 20 28 64 75 70 20 6f 66 20 70 53  x ... (dup of pS
25bf6 72 63 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  rc) */.  Select 
25bf7 2a 70 53 65 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b  *pSelect = NULL;
25bf8 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74        /* Complet
25bf9 65 20 53 45 4c 45 43 54 20 74 72 65 65 20 2a 2f  e SELECT tree */
25bfa 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
25bfb 74 20 74 68 65 72 65 20 69 73 6e 27 74 20 61 6e  t there isn't an
25bfc 20 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f 75   ORDER BY withou
25bfd 74 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  t a LIMIT clause
25bfe 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72  ..  */.  if( pOr
25bff 64 65 72 42 79 20 26 26 20 28 70 4c 69 6d 69 74  derBy && (pLimit
25c00 20 3d 3d 20 30 29 20 29 20 7b 0a 20 20 20 20 73   == 0) ) {.    s
25c01 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
25c02 50 61 72 73 65 2c 20 22 4f 52 44 45 52 20 42 59  Parse, "ORDER BY
25c03 20 77 69 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f   without LIMIT o
25c04 6e 20 25 73 22 2c 20 7a 53 74 6d 74 54 79 70 65  n %s", zStmtType
25c05 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  );.    pParse->p
25c06 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
25c07 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68     goto limit_wh
25c08 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20  ere_cleanup_2;. 
25c09 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c 79   }..  /* We only
25c0a 20 6e 65 65 64 20 74 6f 20 67 65 6e 65 72 61 74   need to generat
25c0b 65 20 61 20 73 65 6c 65 63 74 20 65 78 70 72 65  e a select expre
25c0c 73 73 69 6f 6e 20 69 66 20 74 68 65 72 65 0a 20  ssion if there. 
25c0d 20 2a 2a 20 69 73 20 61 20 6c 69 6d 69 74 2f 6f   ** is a limit/o
25c0e 66 66 73 65 74 20 74 65 72 6d 20 74 6f 20 65 6e  ffset term to en
25c0f 66 6f 72 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  force..  */.  if
25c10 28 20 70 4c 69 6d 69 74 20 3d 3d 20 30 20 29 20  ( pLimit == 0 ) 
25c11 7b 0a 20 20 20 20 2f 2a 20 69 66 20 70 4c 69 6d  {.    /* if pLim
25c12 69 74 20 69 73 20 6e 75 6c 6c 2c 20 70 4f 66 66  it is null, pOff
25c13 73 65 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  set will always 
25c14 62 65 20 6e 75 6c 6c 20 61 73 20 77 65 6c 6c 2e  be null as well.
25c15 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
25c16 70 4f 66 66 73 65 74 20 3d 3d 20 30 20 29 3b 0a  pOffset == 0 );.
25c17 20 20 20 20 72 65 74 75 72 6e 20 70 57 68 65 72      return pWher
25c18 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  e;.  }..  /* Gen
25c19 65 72 61 74 65 20 61 20 73 65 6c 65 63 74 20 65  erate a select e
25c1a 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74  xpression tree t
25c1b 6f 20 65 6e 66 6f 72 63 65 20 74 68 65 20 6c 69  o enforce the li
25c1c 6d 69 74 2f 6f 66 66 73 65 74 20 0a 20 20 2a 2a  mit/offset .  **
25c1d 20 74 65 72 6d 20 66 6f 72 20 74 68 65 20 44 45   term for the DE
25c1e 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73  LETE or UPDATE s
25c1f 74 61 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20 65  tatement.  For e
25c20 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 20 20 20 44  xample:.  **   D
25c21 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
25c22 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20  _a WHERE col1=1 
25c23 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49  ORDER BY col2 LI
25c24 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a 20  MIT 1 OFFSET 1. 
25c25 20 2a 2a 20 62 65 63 6f 6d 65 73 3a 0a 20 20 2a   ** becomes:.  *
25c26 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  *   DELETE FROM 
25c27 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 72 6f  table_a WHERE ro
25c28 77 69 64 20 49 4e 20 28 20 0a 20 20 2a 2a 20 20  wid IN ( .  **  
25c29 20 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20     SELECT rowid 
25c2a 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45  FROM table_a WHE
25c2b 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20  RE col1=1 ORDER 
25c2c 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20  BY col2 LIMIT 1 
25c2d 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 20 20  OFFSET 1.  **   
25c2e 29 3b 0a 20 20 2a 2f 0a 0a 20 20 70 53 65 6c 65  );.  */..  pSele
25c2f 63 74 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  ctRowid = sqlite
25c30 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
25c31 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b  K_ROW, 0, 0, 0);
25c32 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 52 6f  .  if( pSelectRo
25c33 77 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20  wid == 0 ) goto 
25c34 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61  limit_where_clea
25c35 6e 75 70 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20  nup_2;.  pEList 
25c36 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
25c37 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
25c38 30 2c 20 70 53 65 6c 65 63 74 52 6f 77 69 64 29  0, pSelectRowid)
25c39 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d  ;.  if( pEList =
25c3a 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
25c3b 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
25c3c 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74  ;..  /* duplicat
25c3d 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  e the FROM claus
25c3e 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65  e as it is neede
25c3f 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45  d by both the DE
25c40 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65  LETE/UPDATE tree
25c41 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  .  ** and the SE
25c42 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f  LECT subtree. */
25c43 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20  .  pSelectSrc = 
25c44 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
25c45 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  p(pParse->db, pS
25c46 72 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  rc, 0);.  if( pS
25c47 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29 20  electSrc == 0 ) 
25c48 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
25c49 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
25c4a 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b  se->db, pEList);
25c4b 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f  .    goto limit_
25c4c 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
25c4d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72  .  }..  /* gener
25c4e 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 65  ate the SELECT e
25c4f 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
25c50 2a 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73  */.  pSelect = s
25c51 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
25c52 70 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70 53  pParse,pEList,pS
25c53 65 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65 2c  electSrc,pWhere,
25c54 30 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0,0,.           
25c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c56 20 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69    pOrderBy,0,pLi
25c57 6d 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20  mit,pOffset);.  
25c58 69 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30  if( pSelect == 0
25c59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
25c5a 2f 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20  /* now generate 
25c5b 74 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f  the new WHERE ro
25c5c 77 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f  wid IN clause fo
25c5d 72 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50  r the DELETE/UDP
25c5e 41 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52  ATE */.  pWhereR
25c5f 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50 45  owid = sqlite3PE
25c60 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52  xpr(pParse, TK_R
25c61 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  OW, 0, 0, 0);.  
25c62 69 66 28 20 70 57 68 65 72 65 52 6f 77 69 64 20  if( pWhereRowid 
25c63 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
25c64 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
25c65 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d  1;.  pInClause =
25c66 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
25c67 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68  arse, TK_IN, pWh
25c68 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b  ereRowid, 0, 0);
25c69 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65  .  if( pInClause
25c6a 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
25c6b 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25c6c 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65  _1;..  pInClause
25c6d 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70 53  ->x.pSelect = pS
25c6e 65 6c 65 63 74 3b 0a 20 20 70 49 6e 43 6c 61 75  elect;.  pInClau
25c6f 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  se->flags |= EP_
25c70 78 49 73 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c  xIsSelect;.  sql
25c71 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
25c72 74 28 70 50 61 72 73 65 2c 20 70 49 6e 43 6c 61  t(pParse, pInCla
25c73 75 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  use);.  return p
25c74 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20  InClause;..  /* 
25c75 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 77  something went w
25c76 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20 61  rong. clean up a
25c77 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65  nything allocate
25c78 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72  d. */.limit_wher
25c79 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73  e_cleanup_1:.  s
25c7a 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
25c7b 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
25c7c 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72  Select);.  retur
25c7d 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72  n 0;..limit_wher
25c7e 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73  e_cleanup_2:.  s
25c7f 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
25c80 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 57 68  (pParse->db, pWh
25c81 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ere);.  sqlite3E
25c82 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
25c83 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72  arse->db, pOrder
25c84 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  By);.  sqlite3Ex
25c85 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
25c86 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20  >db, pLimit);.  
25c87 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
25c88 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f  e(pParse->db, pO
25c89 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e  ffset);.  return
25c8a 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
25c8b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
25c8c 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c  NABLE_UPDATE_DEL
25c8d 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64  ETE_LIMIT) && !d
25c8e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
25c8f 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a  IT_SUBQUERY) */.
25c90 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
25c91 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54  code for a DELET
25c92 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74  E FROM statement
25c93 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45  ..**.**     DELE
25c94 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78  TE FROM table_wx
25c95 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44  yz WHERE a<5 AND
25c96 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20   b NOT NULL;.** 
25c97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25c98 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20  \________/      
25c99 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
25c9a 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
25c9b 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74          pTabList
25c9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
25c9d 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  here.*/.SQLITE_P
25c9e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
25c9f 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20  te3DeleteFrom(. 
25ca0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
25ca1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
25ca2 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
25ca3 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
25ca4 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
25ca5 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63   table from whic
25ca6 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65  h we should dele
25ca7 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45  te things */.  E
25ca8 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
25ca9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
25caa 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
25cab 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20  be null */.){.  
25cac 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
25cad 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
25cae 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
25caf 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65  ngine */.  Table
25cb0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
25cb1 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66    /* The table f
25cb2 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64  rom which record
25cb3 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  s will be delete
25cb4 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
25cb5 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a  r *zDb;       /*
25cb6 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
25cb7 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
25cb8 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64  /.  int end, add
25cb9 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20  r = 0;     /* A 
25cba 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65 73  couple addresses
25cbb 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f   of generated co
25cbc 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  de */.  int i;  
25cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25cbe 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
25cbf 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  /.  WhereInfo *p
25cc0 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e  WInfo;     /* In
25cc1 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
25cc2 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
25cc3 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
25cc4 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
25cc5 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
25cc6 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20   indices of the 
25cc7 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
25cc8 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
25cc9 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
25cca 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62   number for pTab
25ccb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
25ccc 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
25ccd 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  Main database st
25cce 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74  ructure */.  Aut
25ccf 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
25cd0 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61  t;  /* Authoriza
25cd1 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
25cd2 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
25cd3 43 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  C;       /* Name
25cd4 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f   context to reso
25cd5 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  lve expressions 
25cd6 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  in */.  int iDb;
25cd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25cd8 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
25cd9 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e  r */.  int memCn
25cda 74 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a  t = -1;       /*
25cdb 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65   Memory cell use
25cdc 64 20 66 6f 72 20 63 68 61 6e 67 65 20 63 6f 75  d for change cou
25cdd 6e 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72  nting */.  int r
25cde 63 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20  cauth;          
25cdf 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72    /* Value retur
25ce0 6e 65 64 20 62 79 20 61 75 74 68 6f 72 69 7a 61  ned by authoriza
25ce1 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  tion callback */
25ce2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
25ce3 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
25ce4 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20  int isView;     
25ce5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25ce6 54 72 75 65 20 69 66 20 61 74 74 65 6d 70 74 69  True if attempti
25ce7 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  ng to delete fro
25ce8 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72  m a view */.  Tr
25ce9 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
25cea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
25ceb 73 74 20 6f 66 20 74 61 62 6c 65 20 74 72 69 67  st of table trig
25cec 67 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 65  gers, if require
25ced 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6d  d */.#endif..  m
25cee 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c  emset(&sContext,
25cef 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74   0, sizeof(sCont
25cf0 65 78 74 29 29 3b 0a 20 20 64 62 20 3d 20 70 50  ext));.  db = pP
25cf1 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
25cf2 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
25cf3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
25cf4 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
25cf5 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
25cf6 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
25cf7 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
25cf8 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  1 );..  /* Locat
25cf9 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63  e the table whic
25cfa 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c  h we want to del
25cfb 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65  ete.  This table
25cfc 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20   has to be.  ** 
25cfd 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73  put in an SrcLis
25cfe 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61  t structure beca
25cff 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  use some of the 
25d00 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20  subroutines we. 
25d01 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c   ** will be call
25d02 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64  ing are designed
25d03 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75   to work with mu
25d04 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e  ltiple tables an
25d05 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e  d expect.  ** an
25d06 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65   SrcList* parame
25d07 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a  ter instead of j
25d08 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72  ust a Table* par
25d09 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ameter..  */.  p
25d0a 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
25d0b 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
25d0c 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
25d0d 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67  if( pTab==0 )  g
25d0e 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
25d0f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46  cleanup;..  /* F
25d10 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
25d11 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
25d12 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
25d13 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65  le being.  ** de
25d14 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20  leted from is a 
25d15 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
25d16 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
25d17 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
25d18 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
25d19 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
25d1a 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
25d1b 20 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77   0, 0);.  isView
25d1c 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74   = pTab->pSelect
25d1d 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  !=0;.#else.# def
25d1e 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23  ine pTrigger 0.#
25d1f 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
25d20 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
25d21 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
25d22 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23  # undef isView.#
25d23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
25d24 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
25d25 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20   pTab is really 
25d26 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72  a view, make sur
25d27 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e  e it has been in
25d28 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a  itialized..  */.
25d29 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
25d2a 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
25d2b 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
25d2c 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
25d2d 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
25d2e 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
25d2f 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72  3IsReadOnly(pPar
25d30 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67  se, pTab, (pTrig
25d31 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20  ger?1:0)) ){.   
25d32 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
25d33 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  m_cleanup;.  }. 
25d34 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
25d35 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
25d36 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
25d37 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
25d38 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d  ->nDb );.  zDb =
25d39 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
25d3a 61 6d 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20  ame;.  rcauth = 
25d3b 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
25d3c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
25d3d 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  DELETE, pTab->zN
25d3e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20  ame, 0, zDb);.  
25d3f 61 73 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d  assert( rcauth==
25d40 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61  SQLITE_OK || rca
25d41 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59  uth==SQLITE_DENY
25d42 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
25d43 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69  TE_IGNORE );.  i
25d44 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  f( rcauth==SQLIT
25d45 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f  E_DENY ){.    go
25d46 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
25d47 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73  leanup;.  }.  as
25d48 73 65 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20  sert(!isView || 
25d49 70 54 72 69 67 67 65 72 29 3b 0a 0a 20 20 2f 2a  pTrigger);..  /*
25d4a 20 41 73 73 69 67 6e 20 20 63 75 72 73 6f 72 20   Assign  cursor 
25d4b 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 74 61  number to the ta
25d4c 62 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20  ble and all its 
25d4d 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
25d4e 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
25d4f 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
25d50 43 75 72 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Cur = pTabList->
25d51 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70  a[0].iCursor = p
25d52 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
25d53 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
25d54 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
25d55 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
25d56 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  .    pParse->nTa
25d57 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  b++;.  }..  /* S
25d58 74 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f  tart the view co
25d59 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28  ntext.  */.  if(
25d5a 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73   isView ){.    s
25d5b 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
25d5c 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73  tPush(pParse, &s
25d5d 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a  Context, pTab->z
25d5e 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Name);.  }..  /*
25d5f 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e   Begin generatin
25d60 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76  g code..  */.  v
25d61 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
25d62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
25d63 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74   v==0 ){.    got
25d64 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
25d65 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
25d66 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
25d67 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  =0 ) sqlite3Vdbe
25d68 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b  CountChanges(v);
25d69 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
25d6a 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
25d6b 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a  arse, 1, iDb);..
25d6c 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74    /* If we are t
25d6d 72 79 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20  rying to delete 
25d6e 66 72 6f 6d 20 61 20 76 69 65 77 2c 20 72 65 61  from a view, rea
25d6f 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69  lize that view i
25d70 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d  nto.  ** a ephem
25d71 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  eral table..  */
25d72 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
25d73 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
25d74 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
25d75 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
25d76 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
25d77 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65  .    sqlite3Mate
25d78 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72  rializeView(pPar
25d79 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65  se, pTab, pWhere
25d7a 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e  , iCur);.  }.#en
25d7b 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  dif..  /* Resolv
25d7c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
25d7d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  es in the WHERE 
25d7e 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d  clause..  */.  m
25d7f 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73  emset(&sNC, 0, s
25d80 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73  izeof(sNC));.  s
25d81 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
25d82 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69  se;.  sNC.pSrcLi
25d83 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20  st = pTabList;. 
25d84 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
25d85 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
25d86 43 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20  C, pWhere) ){.  
25d87 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72    goto delete_fr
25d88 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  om_cleanup;.  }.
25d89 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
25d8a 20 74 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20   the counter of 
25d8b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
25d8c 77 73 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20  ws deleted, if. 
25d8d 20 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74   ** we are count
25d8e 69 6e 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20  ing rows..  */. 
25d8f 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
25d90 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
25d91 73 20 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20  s ){.    memCnt 
25d92 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
25d93 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25d94 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
25d95 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74  teger, 0, memCnt
25d96 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  );.  }..#ifndef 
25d97 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e  SQLITE_OMIT_TRUN
25d98 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CATE_OPTIMIZATIO
25d99 4e 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63  N.  /* Special c
25d9a 61 73 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69  ase: A DELETE wi
25d9b 74 68 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c  thout a WHERE cl
25d9c 61 75 73 65 20 64 65 6c 65 74 65 73 20 65 76 65  ause deletes eve
25d9d 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74  rything..  ** It
25d9e 20 69 73 20 65 61 73 69 65 72 20 6a 75 73 74 20   is easier just 
25d9f 74 6f 20 65 72 61 73 65 20 74 68 65 20 77 68 6f  to erase the who
25da0 6c 65 20 74 61 62 6c 65 2e 20 50 72 69 6f 72 20  le table. Prior 
25da1 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 35  to version 3.6.5
25da2 2c 0a 20 20 2a 2a 20 74 68 69 73 20 6f 70 74 69  ,.  ** this opti
25da3 6d 69 7a 61 74 69 6f 6e 20 63 61 75 73 65 64 20  mization caused 
25da4 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63  the row change c
25da5 6f 75 6e 74 20 28 74 68 65 20 76 61 6c 75 65 20  ount (the value 
25da6 72 65 74 75 72 6e 65 64 20 62 79 20 0a 20 20 2a  returned by .  *
25da7 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 73  * API function s
25da8 71 6c 69 74 65 33 5f 63 6f 75 6e 74 5f 63 68 61  qlite3_count_cha
25da9 6e 67 65 73 29 20 74 6f 20 62 65 20 73 65 74 20  nges) to be set 
25daa 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 20 2a 2f  incorrectly.  */
25dab 0a 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53  .  if( rcauth==S
25dac 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 57 68 65  QLITE_OK && pWhe
25dad 72 65 3d 3d 30 20 26 26 20 21 70 54 72 69 67 67  re==0 && !pTrigg
25dae 65 72 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  er && !IsVirtual
25daf 28 70 54 61 62 29 20 0a 20 20 20 26 26 20 30 3d  (pTab) .   && 0=
25db0 3d 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72  =sqlite3FkRequir
25db1 65 64 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ed(pParse, pTab,
25db2 20 30 2c 20 30 29 0a 20 20 29 7b 0a 20 20 20 20   0, 0).  ){.    
25db3 61 73 73 65 72 74 28 20 21 69 73 56 69 65 77 20  assert( !isView 
25db4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25db5 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43  beAddOp4(v, OP_C
25db6 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d  lear, pTab->tnum
25db7 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c 0a 20  , iDb, memCnt,. 
25db8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25db9 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65       pTab->zName
25dba 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
25dbb 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
25dbc 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
25dbd 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
25dbe 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
25dbf 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  pIdx->pSchema==p
25dc0 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
25dc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25dc2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
25dc3 65 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c  ear, pIdx->tnum,
25dc4 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   iDb);.    }.  }
25dc5 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
25dc6 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43  QLITE_OMIT_TRUNC
25dc7 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  ATE_OPTIMIZATION
25dc8 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73 75   */.  /* The usu
25dc9 61 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20 69  al case: There i
25dca 73 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  s a WHERE clause
25dcb 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73   so we have to s
25dcc 63 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a  can through.  **
25dcd 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70   the table and p
25dce 69 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64  ick which record
25dcf 73 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a  s to delete..  *
25dd0 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 52  /.  {.    int iR
25dd1 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65  owSet = ++pParse
25dd2 2d 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 52 65 67  ->nMem;   /* Reg
25dd3 69 73 74 65 72 20 66 6f 72 20 72 6f 77 73 65 74  ister for rowset
25dd4 20 6f 66 20 72 6f 77 73 20 74 6f 20 64 65 6c 65   of rows to dele
25dd5 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52  te */.    int iR
25dd6 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  owid = ++pParse-
25dd7 3e 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65  >nMem;    /* Use
25dd8 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f  d for storing ro
25dd9 77 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 20  wid values. */. 
25dda 20 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b     int regRowid;
25ddb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ddc 20 20 20 2f 2a 20 41 63 74 75 61 6c 20 72 65 67     /* Actual reg
25ddd 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
25dde 20 72 6f 77 69 64 73 20 2a 2f 0a 0a 20 20 20 20   rowids */..    
25ddf 2f 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77 69 64  /* Collect rowid
25de0 73 20 6f 66 20 65 76 65 72 79 20 72 6f 77 20 74  s of every row t
25de1 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 20 20  o be deleted..  
25de2 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
25de3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25de4 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77 53 65  _Null, 0, iRowSe
25de5 74 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d  t);.    pWInfo =
25de6 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
25de7 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
25de8 69 73 74 2c 20 70 57 68 65 72 65 2c 30 2c 57 48  ist, pWhere,0,WH
25de9 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f  ERE_DUPLICATES_O
25dea 4b 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e  K);.    if( pWIn
25deb 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c  fo==0 ) goto del
25dec 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
25ded 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  ;.    regRowid =
25dee 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
25def 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65  GetColumn(pParse
25df0 2c 20 70 54 61 62 2c 20 2d 31 2c 20 69 43 75 72  , pTab, -1, iCur
25df1 2c 20 69 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20  , iRowid, 0);.  
25df2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25df3 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Op2(v, OP_RowSet
25df4 41 64 64 2c 20 69 52 6f 77 53 65 74 2c 20 72 65  Add, iRowSet, re
25df5 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28  gRowid);.    if(
25df6 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
25df7 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b  ITE_CountRows ){
25df8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25df9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
25dfa 64 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31  ddImm, memCnt, 1
25dfb 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
25dfc 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
25dfd 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65  nfo);..    /* De
25dfe 6c 65 74 65 20 65 76 65 72 79 20 69 74 65 6d 20  lete every item 
25dff 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20 77 72  whose key was wr
25e00 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c 69 73  itten to the lis
25e01 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20 20 20  t during the.   
25e02 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 61   ** database sca
25e03 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64  n.  We have to d
25e04 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66 74 65  elete items afte
25e05 72 20 74 68 65 20 73 63 61 6e 20 69 73 20 63 6f  r the scan is co
25e06 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 62 65  mplete.    ** be
25e07 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67 20 61  cause deleting a
25e08 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61 6e 67  n item can chang
25e09 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64 65 72  e the scan order
25e0a 2e 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20  .  */.    end = 
25e0b 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
25e0c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a  abel(v);..    /*
25e0d 20 55 6e 6c 65 73 73 20 74 68 69 73 20 69 73 20   Unless this is 
25e0e 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 63 75 72  a view, open cur
25e0f 73 6f 72 73 20 66 6f 72 20 74 68 65 20 74 61 62  sors for the tab
25e10 6c 65 20 77 65 20 61 72 65 20 0a 20 20 20 20 2a  le we are .    *
25e11 2a 20 64 65 6c 65 74 69 6e 67 20 66 72 6f 6d 20  * deleting from 
25e12 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64 69  and all its indi
25e13 63 65 73 2e 20 49 66 20 74 68 69 73 20 69 73 20  ces. If this is 
25e14 61 20 76 69 65 77 2c 20 74 68 65 6e 20 74 68 65  a view, then the
25e15 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 65 66 66  .    ** only eff
25e16 65 63 74 20 74 68 69 73 20 73 74 61 74 65 6d 65  ect this stateme
25e17 6e 74 20 68 61 73 20 69 73 20 74 6f 20 66 69 72  nt has is to fir
25e18 65 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46  e the INSTEAD OF
25e19 20 0a 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72   .    ** trigger
25e1a 73 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  s.  */.    if( !
25e1b 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
25e1c 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
25e1d 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72 73  AndIndices(pPars
25e1e 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 4f  e, pTab, iCur, O
25e1f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
25e20 20 20 7d 0a 0a 20 20 20 20 61 64 64 72 20 3d 20    }..    addr = 
25e21 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25e22 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65  3(v, OP_RowSetRe
25e23 61 64 2c 20 69 52 6f 77 53 65 74 2c 20 65 6e 64  ad, iRowSet, end
25e24 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20  , iRowid);..    
25e25 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72 6f  /* Delete the ro
25e26 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  w */.#ifndef SQL
25e27 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
25e28 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73  TABLE.    if( Is
25e29 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
25e2a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
25e2b 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73  r *pVTab = (cons
25e2c 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
25e2d 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54  GetVTable(db, pT
25e2e 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
25e2f 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62  e3VtabMakeWritab
25e30 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  le(pParse, pTab)
25e31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25e32 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25e33 56 55 70 64 61 74 65 2c 20 30 2c 20 31 2c 20 69  VUpdate, 0, 1, i
25e34 52 6f 77 69 64 2c 20 70 56 54 61 62 2c 20 50 34  Rowid, pVTab, P4
25e35 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 73 71  _VTAB);.      sq
25e36 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
25e37 61 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  arse);.    }else
25e38 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20  .#endif.    {.  
25e39 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20      int count = 
25e3a 28 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d  (pParse->nested=
25e3b 3d 30 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  =0);    /* True 
25e3c 74 6f 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73  to count changes
25e3d 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
25e3e 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
25e3f 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  te(pParse, pTab,
25e40 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20 63   iCur, iRowid, c
25e41 6f 75 6e 74 2c 20 70 54 72 69 67 67 65 72 2c 20  ount, pTrigger, 
25e42 4f 45 5f 44 65 66 61 75 6c 74 29 3b 0a 20 20 20  OE_Default);.   
25e43 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f   }..    /* End o
25e44 66 20 74 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f  f the delete loo
25e45 70 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  p */.    sqlite3
25e46 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25e47 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b  _Goto, 0, addr);
25e48 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25e49 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
25e4a 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c  end);..    /* Cl
25e4b 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 73 20  ose the cursors 
25e4c 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c  open on the tabl
25e4d 65 20 61 6e 64 20 69 74 73 20 69 6e 64 65 78 65  e and its indexe
25e4e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69  s. */.    if( !i
25e4f 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74  sView && !IsVirt
25e50 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
25e51 20 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78     for(i=1, pIdx
25e52 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
25e53 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70  Idx; i++, pIdx=p
25e54 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
25e55 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25e56 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
25e57 73 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70 49  se, iCur + i, pI
25e58 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20  dx->tnum);.     
25e59 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
25e5a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
25e5b 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20  _Close, iCur);. 
25e5c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55     }.  }..  /* U
25e5d 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
25e5e 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
25e5f 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
25e60 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20  ontent of the.  
25e61 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  ** maximum rowid
25e62 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20   counter values 
25e63 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69  recorded while i
25e64 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20  nserting into.  
25e65 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ** autoincrement
25e66 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
25e67 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
25e68 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  ed==0 && pParse-
25e69 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20  >pTriggerTab==0 
25e6a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
25e6b 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70  toincrementEnd(p
25e6c 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Parse);.  }..  /
25e6d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
25e6e 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74  ber of rows that
25e6f 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e 20 49   were deleted. I
25e70 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
25e71 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69  s .  ** generati
25e72 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 20  ng code because 
25e73 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  of a call to sql
25e74 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
25e75 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69  ), do not.  ** i
25e76 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61  nvoke the callba
25e77 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  ck function..  *
25e78 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  /.  if( (db->fla
25e79 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  gs&SQLITE_CountR
25e7a 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d  ows) && !pParse-
25e7b 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50 61 72  >nested && !pPar
25e7c 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
25e7d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
25e7e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
25e7f 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e 74  esultRow, memCnt
25e80 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
25e81 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
25e82 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
25e83 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
25e84 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
25e85 41 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65 74  AME, "rows delet
25e86 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ed", SQLITE_STAT
25e87 49 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74 65  IC);.  }..delete
25e88 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20  _from_cleanup:. 
25e89 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
25e8a 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74  extPop(&sContext
25e8b 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  );.  sqlite3SrcL
25e8c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
25e8d 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  abList);.  sqlit
25e8e 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
25e8f 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75   pWhere);.  retu
25e90 72 6e 3b 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75  rn;.}./* Make su
25e91 72 65 20 22 69 73 56 69 65 77 22 20 61 6e 64 20  re "isView" and 
25e92 6f 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65 66  other macros def
25e93 69 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20 75  ined above are u
25e94 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72 77  ndefined. Otherw
25e95 69 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79  ise.** thely may
25e96 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 20   interfere with 
25e97 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f  compilation of o
25e98 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69  ther functions i
25e99 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28  n this file.** (
25e9a 6f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66 69  or in another fi
25e9b 6c 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c 65  le, if this file
25e9c 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66   becomes part of
25e9d 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
25e9e 6e 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 69  n).  */.#ifdef i
25e9f 73 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69 73  sView. #undef is
25ea0 56 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66 64  View.#endif.#ifd
25ea1 65 66 20 70 54 72 69 67 67 65 72 0a 20 23 75 6e  ef pTrigger. #un
25ea2 64 65 66 20 70 54 72 69 67 67 65 72 0a 23 65 6e  def pTrigger.#en
25ea3 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
25ea4 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
25ea5 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  s VDBE code that
25ea6 20 63 61 75 73 65 73 20 61 20 73 69 6e 67 6c 65   causes a single
25ea7 20 72 6f 77 20 6f 66 20 61 0a 2a 2a 20 73 69 6e   row of a.** sin
25ea8 67 6c 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  gle table to be 
25ea9 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  deleted..**.** T
25eaa 68 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20  he VDBE must be 
25eab 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  in a particular 
25eac 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20  state when this 
25ead 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
25eae 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20  d..** These are 
25eaf 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73  the requirements
25eb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20  :.**.**   1.  A 
25eb1 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
25eb2 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54  r pointing to pT
25eb3 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f  ab, the table co
25eb4 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
25eb5 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20  .**       to be 
25eb6 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65  deleted, must be
25eb7 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f   opened as curso
25eb8 72 20 6e 75 6d 62 65 72 20 24 69 43 75 72 2e 0a  r number $iCur..
25eb9 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64  **.**   2.  Read
25eba 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 66  /write cursors f
25ebb 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f  or all indices o
25ebc 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f  f pTab must be o
25ebd 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20  pen as.**       
25ebe 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61  cursor number ba
25ebf 73 65 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74  se+i for the i-t
25ec0 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20  h index..**.**  
25ec1 20 33 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20   3.  The record 
25ec2 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f  number of the ro
25ec3 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  w to be deleted 
25ec4 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 69  must be stored i
25ec5 6e 0a 2a 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72  n.**       memor
25ec6 79 20 63 65 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a  y cell iRowid..*
25ec7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
25ec8 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
25ec9 20 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20   to remove both 
25eca 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64  the table record
25ecb 20 61 6e 64 20 61 6c 6c 20 0a 2a 2a 20 69 6e 64   and all .** ind
25ecc 65 78 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ex entries that 
25ecd 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 72 65  point to that re
25ece 63 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cord..*/.SQLITE_
25ecf 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
25ed0 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44  ite3GenerateRowD
25ed1 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a  elete(.  Parse *
25ed2 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
25ed3 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
25ed4 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
25ed5 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
25ed6 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
25ed7 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  ow to be deleted
25ed8 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
25ed9 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
25eda 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
25edb 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
25edc 20 69 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20   iRowid,        
25edd 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  /* Memory cell t
25ede 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
25edf 20 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65   rowid to delete
25ee0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 2c   */.  int count,
25ee1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
25ee2 6f 6e 2d 7a 65 72 6f 2c 20 69 6e 63 72 65 6d 65  on-zero, increme
25ee3 6e 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  nt the row chang
25ee4 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 54  e counter */.  T
25ee5 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
25ee6 2c 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69  , /* List of tri
25ee7 67 67 65 72 73 20 74 6f 20 28 70 6f 74 65 6e 74  ggers to (potent
25ee8 69 61 6c 6c 79 29 20 66 69 72 65 20 2a 2f 0a 20  ially) fire */. 
25ee9 20 69 6e 74 20 6f 6e 63 6f 6e 66 20 20 20 20 20   int onconf     
25eea 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f      /* Default O
25eeb 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
25eec 79 20 66 6f 72 20 74 72 69 67 67 65 72 73 20 2a  y for triggers *
25eed 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
25eee 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
25eef 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 2a         /* Vdbe *
25ef0 2f 0a 20 20 69 6e 74 20 69 4f 6c 64 20 3d 20 30  /.  int iOld = 0
25ef1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25ef2 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
25ef3 69 73 74 65 72 20 69 6e 20 4f 4c 44 2e 2a 20 61  ister in OLD.* a
25ef4 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 4c  rray */.  int iL
25ef5 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  abel;           
25ef6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62            /* Lab
25ef7 65 6c 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 65  el resolved to e
25ef8 6e 64 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20  nd of generated 
25ef9 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 56 64  code */..  /* Vd
25efa 62 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  be is guaranteed
25efb 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 61 6c   to have been al
25efc 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20  located by this 
25efd 73 74 61 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65  stage. */.  asse
25efe 72 74 28 20 76 20 29 3b 0a 0a 20 20 2f 2a 20 53  rt( v );..  /* S
25eff 65 65 6b 20 63 75 72 73 6f 72 20 69 43 75 72 20  eek cursor iCur 
25f00 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 64 65  to the row to de
25f01 6c 65 74 65 2e 20 49 66 20 74 68 69 73 20 72 6f  lete. If this ro
25f02 77 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73  w no longer exis
25f03 74 73 20 0a 20 20 2a 2a 20 28 74 68 69 73 20 63  ts .  ** (this c
25f04 61 6e 20 68 61 70 70 65 6e 20 69 66 20 61 20 74  an happen if a t
25f05 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 68  rigger program h
25f06 61 73 20 61 6c 72 65 61 64 79 20 64 65 6c 65 74  as already delet
25f07 65 64 20 69 74 29 2c 20 64 6f 0a 20 20 2a 2a 20  ed it), do.  ** 
25f08 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64  not attempt to d
25f09 65 6c 65 74 65 20 69 74 20 6f 72 20 66 69 72 65  elete it or fire
25f0a 20 61 6e 79 20 44 45 4c 45 54 45 20 74 72 69 67   any DELETE trig
25f0b 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69 4c 61 62  gers.  */.  iLab
25f0c 65 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  el = sqlite3Vdbe
25f0d 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
25f0e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25f0f 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
25f10 73 2c 20 69 43 75 72 2c 20 69 4c 61 62 65 6c 2c  s, iCur, iLabel,
25f11 20 69 52 6f 77 69 64 29 3b 0a 20 0a 20 20 2f 2a   iRowid);. .  /*
25f12 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
25f13 79 20 74 72 69 67 67 65 72 73 20 74 6f 20 66 69  y triggers to fi
25f14 72 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 72  re, allocate a r
25f15 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65 72  ange of register
25f16 73 20 74 6f 0a 20 20 2a 2a 20 75 73 65 20 66 6f  s to.  ** use fo
25f17 72 20 74 68 65 20 6f 6c 64 2e 2a 20 72 65 66 65  r the old.* refe
25f18 72 65 6e 63 65 73 20 69 6e 20 74 68 65 20 74 72  rences in the tr
25f19 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69 66  iggers.  */.  if
25f1a 28 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69  ( sqlite3FkRequi
25f1b 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 61 62  red(pParse, pTab
25f1c 2c 20 30 2c 20 30 29 20 7c 7c 20 70 54 72 69 67  , 0, 0) || pTrig
25f1d 67 65 72 20 29 7b 0a 20 20 20 20 75 33 32 20 6d  ger ){.    u32 m
25f1e 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
25f1f 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
25f20 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e   of OLD.* column
25f21 73 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 20 20  s in use */.    
25f22 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20  int iCol;       
25f23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25f24 20 49 74 65 72 61 74 6f 72 20 75 73 65 64 20 77   Iterator used w
25f25 68 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20  hile populating 
25f26 4f 4c 44 2e 2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a  OLD.* */..    /*
25f27 20 54 4f 44 4f 3a 20 43 6f 75 6c 64 20 75 73 65   TODO: Could use
25f28 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
25f29 74 65 72 73 20 68 65 72 65 2e 20 41 6c 73 6f 20  ters here. Also 
25f2a 63 6f 75 6c 64 20 61 74 74 65 6d 70 74 20 74 6f  could attempt to
25f2b 0a 20 20 20 20 2a 2a 20 61 76 6f 69 64 20 63 6f  .    ** avoid co
25f2c 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  pying the conten
25f2d 74 73 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  ts of the rowid 
25f2e 72 65 67 69 73 74 65 72 2e 20 20 2a 2f 0a 20 20  register.  */.  
25f2f 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33    mask = sqlite3
25f30 54 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 0a  TriggerColmask(.
25f31 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
25f32 70 54 72 69 67 67 65 72 2c 20 30 2c 20 30 2c 20  pTrigger, 0, 0, 
25f33 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c 54  TRIGGER_BEFORE|T
25f34 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54  RIGGER_AFTER, pT
25f35 61 62 2c 20 6f 6e 63 6f 6e 66 0a 20 20 20 20 29  ab, onconf.    )
25f36 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 73 71  ;.    mask |= sq
25f37 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 70  lite3FkOldmask(p
25f38 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
25f39 20 20 69 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d    iOld = pParse-
25f3a 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61  >nMem+1;.    pPa
25f3b 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 28 31 20  rse->nMem += (1 
25f3c 2b 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a  + pTab->nCol);..
25f3d 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20      /* Populate 
25f3e 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f  the OLD.* pseudo
25f3f 2d 74 61 62 6c 65 20 72 65 67 69 73 74 65 72 20  -table register 
25f40 61 72 72 61 79 2e 20 54 68 65 73 65 20 76 61 6c  array. These val
25f41 75 65 73 20 77 69 6c 6c 20 62 65 20 0a 20 20 20  ues will be .   
25f42 20 2a 2a 20 75 73 65 64 20 62 79 20 61 6e 79 20   ** used by any 
25f43 42 45 46 4f 52 45 20 61 6e 64 20 41 46 54 45 52  BEFORE and AFTER
25f44 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20 65   triggers that e
25f45 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 73 71  xist.  */.    sq
25f46 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25f47 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 52 6f 77  v, OP_Copy, iRow
25f48 69 64 2c 20 69 4f 6c 64 29 3b 0a 20 20 20 20 66  id, iOld);.    f
25f49 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
25f4a 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
25f4b 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d  ++){.      if( m
25f4c 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 20  ask==0xffffffff 
25f4d 7c 7c 20 6d 61 73 6b 26 28 31 3c 3c 69 43 6f 6c  || mask&(1<<iCol
25f4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
25f4f 20 69 54 61 72 67 65 74 20 3d 20 69 4f 6c 64 20   iTarget = iOld 
25f50 2b 20 69 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20  + iCol + 1;.    
25f51 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25f52 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
25f53 6d 6e 2c 20 69 43 75 72 2c 20 69 43 6f 6c 2c 20  mn, iCur, iCol, 
25f54 69 54 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20  iTarget);.      
25f55 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
25f56 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
25f57 69 43 6f 6c 2c 20 69 54 61 72 67 65 74 29 3b 0a  iCol, iTarget);.
25f58 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
25f59 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 42 45 46     /* Invoke BEF
25f5a 4f 52 45 20 44 45 4c 45 54 45 20 74 72 69 67 67  ORE DELETE trigg
25f5b 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a  er programs. */.
25f5c 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
25f5d 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
25f5e 2c 20 70 54 72 69 67 67 65 72 2c 20 0a 20 20 20  , pTrigger, .   
25f5f 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20       TK_DELETE, 
25f60 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  0, TRIGGER_BEFOR
25f61 45 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c 20 6f  E, pTab, iOld, o
25f62 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a 20 20  nconf, iLabel.  
25f63 20 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65    );..    /* See
25f64 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  k the cursor to 
25f65 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
25f66 6c 65 74 65 64 20 61 67 61 69 6e 2e 20 49 74 20  leted again. It 
25f67 6d 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20  may be that.    
25f68 2a 2a 20 74 68 65 20 42 45 46 4f 52 45 20 74 72  ** the BEFORE tr
25f69 69 67 67 65 72 73 20 63 6f 64 65 64 20 61 62 6f  iggers coded abo
25f6a 76 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ve have already 
25f6b 72 65 6d 6f 76 65 64 20 74 68 65 20 72 6f 77 0a  removed the row.
25f6c 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 6c      ** being del
25f6d 65 74 65 64 2e 20 44 6f 20 6e 6f 74 20 61 74 74  eted. Do not att
25f6e 65 6d 70 74 20 74 6f 20 64 65 6c 65 74 65 20 74  empt to delete t
25f6f 68 65 20 72 6f 77 20 61 20 73 65 63 6f 6e 64 20  he row a second 
25f70 74 69 6d 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a  time, and .    *
25f71 2a 20 64 6f 20 6e 6f 74 20 66 69 72 65 20 41 46  * do not fire AF
25f72 54 45 52 20 74 72 69 67 67 65 72 73 2e 20 20 2a  TER triggers.  *
25f73 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
25f74 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
25f75 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 69  tExists, iCur, i
25f76 4c 61 62 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a  Label, iRowid);.
25f77 0a 20 20 20 20 2f 2a 20 44 6f 20 46 4b 20 70 72  .    /* Do FK pr
25f78 6f 63 65 73 73 69 6e 67 2e 20 54 68 69 73 20 63  ocessing. This c
25f79 61 6c 6c 20 63 68 65 63 6b 73 20 74 68 61 74 20  all checks that 
25f7a 61 6e 79 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  any FK constrain
25f7b 74 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  ts that.    ** r
25f7c 65 66 65 72 20 74 6f 20 74 68 69 73 20 74 61 62  efer to this tab
25f7d 6c 65 20 28 69 2e 65 2e 20 63 6f 6e 73 74 72 61  le (i.e. constra
25f7e 69 6e 74 73 20 61 74 74 61 63 68 65 64 20 74 6f  ints attached to
25f7f 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20 0a   other tables) .
25f80 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 76      ** are not v
25f81 69 6f 6c 61 74 65 64 20 62 79 20 64 65 6c 65 74  iolated by delet
25f82 69 6e 67 20 74 68 69 73 20 72 6f 77 2e 20 20 2a  ing this row.  *
25f83 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43  /.    sqlite3FkC
25f84 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61  heck(pParse, pTa
25f85 62 2c 20 69 4f 6c 64 2c 20 30 29 3b 0a 20 20 7d  b, iOld, 0);.  }
25f86 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
25f87 65 20 69 6e 64 65 78 20 61 6e 64 20 74 61 62 6c  e index and tabl
25f88 65 20 65 6e 74 72 69 65 73 2e 20 53 6b 69 70 20  e entries. Skip 
25f89 74 68 69 73 20 73 74 65 70 20 69 66 20 70 54 61  this step if pTa
25f8a 62 20 69 73 20 72 65 61 6c 6c 79 0a 20 20 2a 2a  b is really.  **
25f8b 20 61 20 76 69 65 77 20 28 69 6e 20 77 68 69 63   a view (in whic
25f8c 68 20 63 61 73 65 20 74 68 65 20 6f 6e 6c 79 20  h case the only 
25f8d 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 44 45  effect of the DE
25f8e 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  LETE statement i
25f8f 73 20 74 6f 0a 20 20 2a 2a 20 66 69 72 65 20 74  s to.  ** fire t
25f90 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  he INSTEAD OF tr
25f91 69 67 67 65 72 73 29 2e 20 20 2a 2f 20 0a 20 20  iggers).  */ .  
25f92 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
25f93 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
25f94 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e  te3GenerateRowIn
25f95 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73 65  dexDelete(pParse
25f96 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29  , pTab, iCur, 0)
25f97 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25f98 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65  eAddOp2(v, OP_De
25f99 6c 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75  lete, iCur, (cou
25f9a 6e 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  nt?OPFLAG_NCHANG
25f9b 45 3a 30 29 29 3b 0a 20 20 20 20 69 66 28 20 63  E:0));.    if( c
25f9c 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ount ){.      sq
25f9d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
25f9e 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
25f9f 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
25fa0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25fa1 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43  * Do any ON CASC
25fa2 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72  ADE, SET NULL or
25fa3 20 53 45 54 20 44 45 46 41 55 4c 54 20 6f 70 65   SET DEFAULT ope
25fa4 72 61 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64  rations required
25fa5 20 74 6f 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20   to.  ** handle 
25fa6 72 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 69  rows (possibly i
25fa7 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20  n other tables) 
25fa8 74 68 61 74 20 72 65 66 65 72 20 76 69 61 20 61  that refer via a
25fa9 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 2a   foreign key.  *
25faa 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a 75 73  * to the row jus
25fab 74 20 64 65 6c 65 74 65 64 2e 20 2a 2f 20 0a 20  t deleted. */ . 
25fac 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e   sqlite3FkAction
25fad 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
25fae 30 2c 20 69 4f 6c 64 29 3b 0a 0a 20 20 2f 2a 20  0, iOld);..  /* 
25faf 49 6e 76 6f 6b 65 20 41 46 54 45 52 20 44 45 4c  Invoke AFTER DEL
25fb0 45 54 45 20 74 72 69 67 67 65 72 20 70 72 6f 67  ETE trigger prog
25fb1 72 61 6d 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  rams. */.  sqlit
25fb2 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
25fb3 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
25fb4 72 2c 20 0a 20 20 20 20 20 20 54 4b 5f 44 45 4c  r, .      TK_DEL
25fb5 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ETE, 0, TRIGGER_
25fb6 41 46 54 45 52 2c 20 70 54 61 62 2c 20 69 4f 6c  AFTER, pTab, iOl
25fb7 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62 65  d, onconf, iLabe
25fb8 6c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  l.  );..  /* Jum
25fb9 70 20 68 65 72 65 20 69 66 20 74 68 65 20 72 6f  p here if the ro
25fba 77 20 68 61 64 20 61 6c 72 65 61 64 79 20 62 65  w had already be
25fbb 65 6e 20 64 65 6c 65 74 65 64 20 62 65 66 6f 72  en deleted befor
25fbc 65 20 61 6e 79 20 42 45 46 4f 52 45 0a 20 20 2a  e any BEFORE.  *
25fbd 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  * trigger progra
25fbe 6d 73 20 77 65 72 65 20 69 6e 76 6f 6b 65 64 2e  ms were invoked.
25fbf 20 4f 72 20 69 66 20 61 20 74 72 69 67 67 65 72   Or if a trigger
25fc0 20 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73 20   program throws 
25fc1 61 20 0a 20 20 2a 2a 20 52 41 49 53 45 28 49 47  a .  ** RAISE(IG
25fc2 4e 4f 52 45 29 20 65 78 63 65 70 74 69 6f 6e 2e  NORE) exception.
25fc3 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
25fc4 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
25fc5 2c 20 69 4c 61 62 65 6c 29 3b 0a 7d 0a 0a 2f 2a  , iLabel);.}../*
25fc6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
25fc7 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
25fc8 63 6f 64 65 20 74 68 61 74 20 63 61 75 73 65 73  code that causes
25fc9 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66   the deletion of
25fca 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e   all.** index en
25fcb 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
25fcc 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 72   with a single r
25fcd 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74  ow of a single t
25fce 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
25fcf 56 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20  VDBE must be in 
25fd0 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61  a particular sta
25fd1 74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  te when this rou
25fd2 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
25fd3 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65  ** These are the
25fd4 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   requirements:.*
25fd5 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61  *.**   1.  A rea
25fd6 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
25fd7 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c  ointing to pTab,
25fd8 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
25fd9 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a  ining the row.**
25fda 20 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c         to be del
25fdb 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70  eted, must be op
25fdc 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e  ened as cursor n
25fdd 75 6d 62 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a  umber "iCur"..**
25fde 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77  .**   2.  Read/w
25fdf 72 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72  rite cursors for
25fe0 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
25fe1 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65  pTab must be ope
25fe2 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75  n as.**       cu
25fe3 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72  rsor number iCur
25fe4 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
25fe5 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  index..**.**   3
25fe6 2e 20 20 54 68 65 20 22 69 43 75 72 22 20 63 75  .  The "iCur" cu
25fe7 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69  rsor must be poi
25fe8 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 6f 77  nting to the row
25fe9 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a   that is to be.*
25fea 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65 64 2e  *       deleted.
25feb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25fec 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
25fed 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
25fee 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a  elete(.  Parse *
25fef 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
25ff0 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
25ff1 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
25ff2 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
25ff3 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61  Tab,       /* Ta
25ff4 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
25ff5 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  he row to be del
25ff6 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  eted */.  int iC
25ff7 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ur,          /* 
25ff8 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
25ff9 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  r the table */. 
25ffa 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 20 20   int *aRegIdx   
25ffb 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65      /* Only dele
25ffc 74 65 20 69 66 20 61 52 65 67 49 64 78 21 3d 30  te if aRegIdx!=0
25ffd 20 26 26 20 61 52 65 67 49 64 78 5b 69 5d 3e 30   && aRegIdx[i]>0
25ffe 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
25fff 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
26000 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28   int r1;..  for(
26001 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
26002 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b  pIndex; pIdx; i+
26003 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  +, pIdx=pIdx->pN
26004 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 61 52  ext){.    if( aR
26005 65 67 49 64 78 21 3d 30 20 26 26 20 61 52 65 67  egIdx!=0 && aReg
26006 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f  Idx[i-1]==0 ) co
26007 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d  ntinue;.    r1 =
26008 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
26009 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c  IndexKey(pParse,
2600a 20 70 49 64 78 2c 20 69 43 75 72 2c 20 30 2c 20   pIdx, iCur, 0, 
2600b 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
2600c 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65  dbeAddOp3(pParse
2600d 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64 78 44  ->pVdbe, OP_IdxD
2600e 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 72  elete, iCur+i, r
2600f 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  1,pIdx->nColumn+
26010 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  1);.  }.}../*.**
26011 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
26012 68 61 74 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c  hat will assembl
26013 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 61  e an index key a
26014 6e 64 20 70 75 74 20 69 74 20 69 6e 20 72 65 67  nd put it in reg
26015 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e  ister.** regOut.
26016 20 20 54 68 65 20 6b 65 79 20 77 69 74 68 20 62    The key with b
26017 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49 64 78  e for index pIdx
26018 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 64   which is an ind
26019 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69  ex on pTab..** i
2601a 43 75 72 20 69 73 20 74 68 65 20 69 6e 64 65 78  Cur is the index
2601b 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f 70 65   of a cursor ope
2601c 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20 74 61  n on the pTab ta
2601d 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67  ble and pointing
2601e 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79   to.** the entry
2601f 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e 64 65   that needs inde
26020 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  xing..**.** Retu
26021 72 6e 20 61 20 72 65 67 69 73 74 65 72 20 6e 75  rn a register nu
26022 6d 62 65 72 20 77 68 69 63 68 20 69 73 20 74 68  mber which is th
26023 65 20 66 69 72 73 74 20 69 6e 20 61 20 62 6c 6f  e first in a blo
26024 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  ck of.** registe
26025 72 73 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  rs that holds th
26026 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  e elements of th
26027 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 20 54 68  e index key.  Th
26028 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65  e.** block of re
26029 67 69 73 74 65 72 73 20 68 61 73 20 61 6c 72 65  gisters has alre
2602a 61 64 79 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  ady been dealloc
2602b 61 74 65 64 20 62 79 20 74 68 65 20 74 69 6d 65  ated by the time
2602c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
2602d 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c   returns..*/.SQL
2602e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2602f 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
26030 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 72 73 65  ndexKey(.  Parse
26031 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
26032 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
26033 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
26034 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  x,       /* The 
26035 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63 68 20  index for which 
26036 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 6b 65  to generate a ke
26037 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  y */.  int iCur,
26038 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
26039 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
2603a 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 20  he pIdx->pTable 
2603b 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72  table */.  int r
2603c 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a  egOut,        /*
2603d 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 69   Write the new i
2603e 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68 69 73  ndex key to this
2603f 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
26040 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 20 20 20  nt doMakeRec    
26041 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f    /* Run the OP_
26042 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72  MakeRecord instr
26043 75 63 74 69 6f 6e 20 69 66 20 74 72 75 65 20 2a  uction if true *
26044 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
26045 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
26046 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65    int j;.  Table
26047 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70   *pTab = pIdx->p
26048 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67  Table;.  int reg
26049 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  Base;.  int nCol
2604a 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78  ;..  nCol = pIdx
2604b 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67  ->nColumn;.  reg
2604c 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Base = sqlite3Ge
2604d 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
2604e 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71  e, nCol+1);.  sq
2604f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26050 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75  v, OP_Rowid, iCu
26051 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29  r, regBase+nCol)
26052 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e  ;.  for(j=0; j<n
26053 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  Col; j++){.    i
26054 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61  nt idx = pIdx->a
26055 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20  iColumn[j];.    
26056 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69  if( idx==pTab->i
26057 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71  PKey ){.      sq
26058 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26059 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
2605a 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61  Base+nCol, regBa
2605b 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  se+j);.    }else
2605c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2605d 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2605e 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64  Column, iCur, id
2605f 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  x, regBase+j);. 
26060 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75       sqlite3Colu
26061 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61  mnDefault(v, pTa
26062 62 2c 20 69 64 78 2c 20 2d 31 29 3b 0a 20 20 20  b, idx, -1);.   
26063 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d   }.  }.  if( doM
26064 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20 73 71  akeRec ){.    sq
26065 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
26066 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
26067 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b  , regBase, nCol+
26068 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 20 20  1, regOut);.    
26069 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2606a 65 50 34 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74  eP4(v, -1, sqlit
2606b 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
2606c 74 72 28 76 2c 20 70 49 64 78 29 2c 20 30 29 3b  tr(v, pIdx), 0);
2606d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2606e 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2606f 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
26070 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  ase, nCol+1);.  
26071 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  }.  sqlite3Relea
26072 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
26073 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  se, regBase, nCo
26074 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  l+1);.  return r
26075 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  egBase;.}../****
26076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
26077 66 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a  f delete.c *****
26078 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26079 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2607a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2607b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2607c 20 66 69 6c 65 20 66 75 6e 63 2e 63 20 2a 2a 2a   file func.c ***
2607d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2607e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2607f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
26080 20 32 30 30 32 20 46 65 62 72 75 61 72 79 20 32   2002 February 2
26081 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
26082 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
26083 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
26084 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
26085 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
26086 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
26087 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
26088 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
26089 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2608a 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2608b 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2608c 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2608d 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2608e 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2608f 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
26090 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
26091 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
26092 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
26093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26095 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26096 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26097 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
26098 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
26099 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
2609a 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 53 51  ement various SQ
2609b 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6f  L.** functions o
2609c 66 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a  f SQLite.  .**.*
2609d 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  * There is only 
2609e 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d  one exported sym
2609f 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65  bol in this file
260a0 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a   - the function.
260a1 2a 2a 20 73 71 6c 69 74 65 52 65 67 69 73 74 65  ** sqliteRegiste
260a2 72 42 75 69 6c 64 69 6e 46 75 6e 63 74 69 6f 6e  rBuildinFunction
260a3 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65  s() found at the
260a4 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66   bottom of the f
260a5 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  ile..** All othe
260a6 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20  r code has file 
260a7 73 63 6f 70 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  scope..*/../*.**
260a8 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c   Return the coll
260a9 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61  ating function a
260aa 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
260ab 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
260ac 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71  atic CollSeq *sq
260ad 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c  lite3GetFuncColl
260ae 53 65 71 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Seq(sqlite3_cont
260af 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
260b0 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d   return context-
260b1 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  >pColl;.}../*.**
260b2 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
260b3 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65  of the non-aggre
260b4 67 61 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d  gate min() and m
260b5 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  ax() functions.*
260b6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69  /.static void mi
260b7 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73 71 6c 69  nmaxFunc(.  sqli
260b8 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
260b9 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
260ba 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
260bb 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
260bc 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b  t i;.  int mask;
260bd 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e      /* 0 for min
260be 28 29 20 6f 72 20 30 78 66 66 66 66 66 66 66 66  () or 0xffffffff
260bf 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20   for max() */.  
260c0 69 6e 74 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c  int iBest;.  Col
260c1 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20  lSeq *pColl;..  
260c2 61 73 73 65 72 74 28 20 61 72 67 63 3e 31 20 29  assert( argc>1 )
260c3 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74  ;.  mask = sqlit
260c4 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e  e3_user_data(con
260c5 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d  text)==0 ? 0 : -
260c6 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  1;.  pColl = sql
260c7 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
260c8 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61  eq(context);.  a
260c9 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a  ssert( pColl );.
260ca 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d    assert( mask==
260cb 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b  -1 || mask==0 );
260cc 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 20  .  iBest = 0;.  
260cd 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
260ce 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
260cf 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72  =SQLITE_NULL ) r
260d0 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31  eturn;.  for(i=1
260d1 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a  ; i<argc; i++){.
260d2 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
260d3 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
260d4 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  i])==SQLITE_NULL
260d5 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
260d6 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f  f( (sqlite3MemCo
260d7 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 74  mpare(argv[iBest
260d8 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c  ], argv[i], pCol
260d9 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20  l)^mask)>=0 ){. 
260da 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6d       testcase( m
260db 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ask==0 );.      
260dc 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d  iBest = i;.    }
260dd 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
260de 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
260df 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d  ext, argv[iBest]
260e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
260e1 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20 74  rn the type of t
260e2 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  he argument..*/.
260e3 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 70 65  static void type
260e4 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ofFunc(.  sqlite
260e5 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
260e6 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
260e7 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
260e8 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
260e9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30  onst char *z = 0
260ea 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
260eb 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
260ec 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33   switch( sqlite3
260ed 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
260ee 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65  [0]) ){.    case
260ef 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
260f0 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b 20   z = "integer"; 
260f1 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
260f2 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 20  SQLITE_TEXT:    
260f3 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 62  z = "text";    b
260f4 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
260f5 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a  QLITE_FLOAT:   z
260f6 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 72   = "real";    br
260f7 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
260f8 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20  LITE_BLOB:    z 
260f9 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 65  = "blob";    bre
260fa 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  ak;.    default:
260fb 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d               z =
260fc 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 61   "null";    brea
260fd 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  k;.  }.  sqlite3
260fe 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
260ff 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c  text, z, -1, SQL
26100 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
26101 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
26102 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e  ation of the len
26103 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  gth() function.*
26104 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
26105 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69  ngthFunc(.  sqli
26106 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
26107 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
26108 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
26109 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
2610a 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74  t len;..  assert
2610b 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
2610c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2610d 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28  argc);.  switch(
2610e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2610f 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a  ype(argv[0]) ){.
26110 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
26111 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 53  BLOB:.    case S
26112 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20  QLITE_INTEGER:. 
26113 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
26114 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71  LOAT: {.      sq
26115 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
26116 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65  (context, sqlite
26117 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
26118 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 62  gv[0]));.      b
26119 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2611a 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54  case SQLITE_TEXT
2611b 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  : {.      const 
2611c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2611d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2611e 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
2611f 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29        if( z==0 )
26120 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c   return;.      l
26121 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68  en = 0;.      wh
26122 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20  ile( *z ){.     
26123 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20     len++;.      
26124 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
26125 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20  F8(z);.      }. 
26126 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26127 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c  ult_int(context,
26128 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 65   len);.      bre
26129 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
2612a 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73  fault: {.      s
2612b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
2612c 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ll(context);.   
2612d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2612e 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
2612f 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
26130 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 69 6f  he abs() functio
26131 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  n.*/.static void
26132 20 61 62 73 46 75 6e 63 28 73 71 6c 69 74 65 33   absFunc(sqlite3
26133 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
26134 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
26135 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
26136 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72  v){.  assert( ar
26137 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
26138 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
26139 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
2613a 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2613b 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
2613c 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
2613d 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36 34  GER: {.      i64
2613e 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   iVal = sqlite3_
2613f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76  value_int64(argv
26140 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [0]);.      if( 
26141 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iVal<0 ){.      
26142 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d    if( (iVal<<1)=
26143 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
26144 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
26145 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69  rror(context, "i
26146 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22  nteger overflow"
26147 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20  , -1);.         
26148 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
26149 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 6c 20   }.        iVal 
2614a 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d  = -iVal;.      }
2614b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f   .      sqlite3_
2614c 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
2614d 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20  text, iVal);.   
2614e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2614f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
26150 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71  NULL: {.      sq
26151 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
26152 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  l(context);.    
26153 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
26154 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
26155 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 20      double rVal 
26156 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26157 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b  double(argv[0]);
26158 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 6c 3c  .      if( rVal<
26159 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c  0 ) rVal = -rVal
2615a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
2615b 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
2615c 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20  ntext, rVal);.  
2615d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2615e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
2615f 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
26160 74 68 65 20 73 75 62 73 74 72 28 29 20 66 75 6e  the substr() fun
26161 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62  ction..**.** sub
26162 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 72 65  str(x,p1,p2)  re
26163 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 63 74  turns p2 charact
26164 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e  ers of x[] begin
26165 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a  ning with p1..**
26166 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78 65 64   p1 is 1-indexed
26167 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78 2c 31  .  So substr(x,1
26168 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68 65 20  ,1) returns the 
26169 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 0a  first character.
2616a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 20 69  ** of x.  If x i
2616b 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77 65 20  s text, then we 
2616c 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55  actually count U
2616d 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 2e  TF-8 characters.
2616e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 62 6c  .** If x is a bl
2616f 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6e  ob, then we coun
26170 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  t bytes..**.** I
26171 66 20 70 31 20 69 73 20 6e 65 67 61 74 69 76 65  f p1 is negative
26172 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69 6e 20  , then we begin 
26173 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74 68 65  abs(p1) from the
26174 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2a 0a   end of x[]..**.
26175 2a 2a 20 49 66 20 70 32 20 69 73 20 6e 65 67 61  ** If p2 is nega
26176 74 69 76 65 2c 20 72 65 74 75 72 6e 20 74 68 65  tive, return the
26177 20 70 32 20 63 68 61 72 61 63 74 65 72 73 20 70   p2 characters p
26178 72 65 63 65 65 64 69 6e 67 20 70 31 2e 0a 2a 2f  receeding p1..*/
26179 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
2617a 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  strFunc(.  sqlit
2617b 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2617c 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
2617d 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2617e 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
2617f 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
26180 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73   *z;.  const uns
26181 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a  igned char *z2;.
26182 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74    int len;.  int
26183 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20 70   p0type;.  i64 p
26184 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67  1, p2;.  int neg
26185 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  P2 = 0;..  asser
26186 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72  t( argc==3 || ar
26187 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73  gc==2 );.  if( s
26188 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
26189 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49  e(argv[1])==SQLI
2618a 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61  TE_NULL.   || (a
2618b 72 67 63 3d 3d 33 20 26 26 20 73 71 6c 69 74 65  rgc==3 && sqlite
2618c 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
2618d 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[2])==SQLITE_NU
2618e 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  LL).  ){.    ret
2618f 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70  urn;.  }.  p0typ
26190 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
26191 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b  e_type(argv[0]);
26192 0a 20 20 70 31 20 3d 20 73 71 6c 69 74 65 33 5f  .  p1 = sqlite3_
26193 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31  value_int(argv[1
26194 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65  ]);.  if( p0type
26195 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
26196 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74  .    len = sqlit
26197 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
26198 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d  rgv[0]);.    z =
26199 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2619a 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
2619b 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
2619c 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  urn;.    assert(
2619d 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   len==sqlite3_va
2619e 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
2619f 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ]) );.  }else{. 
261a0 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
261a1 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
261a2 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  ]);.    if( z==0
261a3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c   ) return;.    l
261a4 65 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  en = 0;.    if( 
261a5 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 66 6f  p1<0 ){.      fo
261a6 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e  r(z2=z; *z2; len
261a7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 53 51 4c  ++){.        SQL
261a8 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32  ITE_SKIP_UTF8(z2
261a9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
261aa 0a 20 20 7d 0a 20 20 69 66 28 20 61 72 67 63 3d  .  }.  if( argc=
261ab 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73  =3 ){.    p2 = s
261ac 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
261ad 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69  (argv[2]);.    i
261ae 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20 20 20  f( p2<0 ){.     
261af 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20   p2 = -p2;.     
261b0 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20 20 20   negP2 = 1;.    
261b1 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  }.  }else{.    p
261b2 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  2 = sqlite3_cont
261b3 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
261b4 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53  ntext)->aLimit[S
261b5 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
261b6 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  TH];.  }.  if( p
261b7 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d  1<0 ){.    p1 +=
261b8 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 31   len;.    if( p1
261b9 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b  <0 ){.      p2 +
261ba 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20  = p1;.      if( 
261bb 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20  p2<0 ) p2 = 0;. 
261bc 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20       p1 = 0;.   
261bd 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
261be 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b  1>0 ){.    p1--;
261bf 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e  .  }else if( p2>
261c0 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20  0 ){.    p2--;. 
261c1 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32 20 29   }.  if( negP2 )
261c2 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a  {.    p1 -= p2;.
261c3 20 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a      if( p1<0 ){.
261c4 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a        p2 += p1;.
261c5 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20        p1 = 0;.  
261c6 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
261c7 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30  ( p1>=0 && p2>=0
261c8 20 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65   );.  if( p0type
261c9 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  !=SQLITE_BLOB ){
261ca 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 26  .    while( *z &
261cb 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20 53 51  & p1 ){.      SQ
261cc 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
261cd 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20  );.      p1--;. 
261ce 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d     }.    for(z2=
261cf 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32  z; *z2 && p2; p2
261d0 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  --){.      SQLIT
261d1 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b  E_SKIP_UTF8(z2);
261d2 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
261d3 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
261d4 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
261d5 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53  , (int)(z2-z), S
261d6 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
261d7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
261d8 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a  f( p1+p2>len ){.
261d9 20 20 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d 70        p2 = len-p
261da 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c  1;.      if( p2<
261db 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20 20  0 ) p2 = 0;.    
261dc 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  }.    sqlite3_re
261dd 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78  sult_blob(contex
261de 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d  t, (char*)&z[p1]
261df 2c 20 28 69 6e 74 29 70 32 2c 20 53 51 4c 49 54  , (int)p2, SQLIT
261e0 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
261e1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
261e2 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
261e3 20 72 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 6f   round() functio
261e4 6e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  n.*/.#ifndef SQL
261e5 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
261e6 47 5f 50 4f 49 4e 54 0a 73 74 61 74 69 63 20 76  G_POINT.static v
261e7 6f 69 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71  oid roundFunc(sq
261e8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
261e9 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
261ea 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
261eb 2a 2a 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e  **argv){.  int n
261ec 20 3d 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72   = 0;.  double r
261ed 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 0a  ;.  char *zBuf;.
261ee 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
261ef 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  1 || argc==2 );.
261f0 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b    if( argc==2 ){
261f1 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
261f2 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61  NULL==sqlite3_va
261f3 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
261f4 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  ) ) return;.    
261f5 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
261f6 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a  e_int(argv[1]);.
261f7 20 20 20 20 69 66 28 20 6e 3e 33 30 20 29 20 6e      if( n>30 ) n
261f8 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 28 20 6e   = 30;.    if( n
261f9 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a  <0 ) n = 0;.  }.
261fa 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
261fb 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
261fc 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
261fd 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d 20 73   return;.  r = s
261fe 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
261ff 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ble(argv[0]);.  
26200 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zBuf = sqlite3_m
26201 70 72 69 6e 74 66 28 22 25 2e 2a 66 22 2c 6e 2c  printf("%.*f",n,
26202 72 29 3b 0a 20 20 69 66 28 20 7a 42 75 66 3d 3d  r);.  if( zBuf==
26203 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
26204 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
26205 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
26206 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
26207 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29  e3AtoF(zBuf, &r)
26208 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
26209 65 65 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71  ee(zBuf);.    sq
2620a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
2620b 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b  ble(context, r);
2620c 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
2620d 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
2620e 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61  yte bytes of spa
2620f 63 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ce using sqlite3
26210 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 74 68  _malloc(). If th
26211 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
26212 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73 71 6c 69  fails, call sqli
26213 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
26214 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e 6f 74 69  _nomem() to noti
26215 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  fy.** the databa
26216 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d  se handle that m
26217 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c  alloc() has fail
26218 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55  ed and return NU
26219 4c 4c 2e 0a 2a 2a 20 49 66 20 6e 42 79 74 65 20  LL..** If nByte 
2621a 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
2621b 68 65 20 6d 61 78 69 6d 75 6d 20 73 74 72 69 6e  he maximum strin
2621c 67 20 6f 72 20 62 6c 6f 62 20 6c 65 6e 67 74 68  g or blob length
2621d 2c 20 74 68 65 6e 0a 2a 2a 20 72 61 69 73 65 20  , then.** raise 
2621e 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  an SQLITE_TOOBIG
2621f 20 65 78 63 65 70 74 69 6f 6e 20 61 6e 64 20 72   exception and r
26220 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  eturn NULL..*/.s
26221 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 74  tatic void *cont
26222 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65  extMalloc(sqlite
26223 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
26224 78 74 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a  xt, i64 nByte){.
26225 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c    char *z;.  sql
26226 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
26227 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
26228 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
26229 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30   assert( nByte>0
2622a 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
2622b 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69  nByte==db->aLimi
2622c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
2622d 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74  ENGTH] );.  test
2622e 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d  case( nByte==db-
2622f 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
26230 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29  IMIT_LENGTH]+1 )
26231 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62  ;.  if( nByte>db
26232 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
26233 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b  LIMIT_LENGTH] ){
26234 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
26235 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
26236 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7a  (context);.    z
26237 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
26238 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61     z = sqlite3Ma
26239 6c 6c 6f 63 28 28 69 6e 74 29 6e 42 79 74 65 29  lloc((int)nByte)
2623a 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a  ;.    if( !z ){.
2623b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
2623c 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
2623d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  (context);.    }
2623e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b  .  }.  return z;
2623f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
26240 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
26241 75 70 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65  upper() and lowe
26242 72 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  r() SQL function
26243 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
26244 64 20 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69  d upperFunc(sqli
26245 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
26246 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
26247 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
26248 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a  argv){.  char *z
26249 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1;.  const char 
2624a 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  *z2;.  int i, n;
2624b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2624c 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20  TER(argc);.  z2 
2624d 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
2624e 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2624f 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  [0]);.  n = sqli
26250 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26251 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56  argv[0]);.  /* V
26252 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63  erify that the c
26253 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20  all to _bytes() 
26254 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64  does not invalid
26255 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20  ate the _text() 
26256 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73  pointer */.  ass
26257 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29  ert( z2==(char*)
26258 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26259 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  xt(argv[0]) );. 
2625a 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a   if( z2 ){.    z
2625b 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  1 = contextMallo
2625c 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
2625d 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  )n)+1);.    if( 
2625e 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  z1 ){.      memc
2625f 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b  py(z1, z2, n+1);
26260 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
26261 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  z1[i]; i++){.   
26262 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 68       z1[i] = (ch
26263 61 72 29 73 71 6c 69 74 65 33 54 6f 75 70 70 65  ar)sqlite3Touppe
26264 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20  r(z1[i]);.      
26265 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
26266 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
26267 65 78 74 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c  ext, z1, -1, sql
26268 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20  ite3_free);.    
26269 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
2626a 6f 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71  oid lowerFunc(sq
2626b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2626c 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
2626d 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
2626e 2a 2a 61 72 67 76 29 7b 0a 20 20 75 38 20 2a 7a  **argv){.  u8 *z
2626f 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1;.  const char 
26270 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  *z2;.  int i, n;
26271 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
26272 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20  TER(argc);.  z2 
26273 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
26274 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26275 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  [0]);.  n = sqli
26276 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26277 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56  argv[0]);.  /* V
26278 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63  erify that the c
26279 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20  all to _bytes() 
2627a 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64  does not invalid
2627b 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20  ate the _text() 
2627c 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73  pointer */.  ass
2627d 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29  ert( z2==(char*)
2627e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2627f 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20  xt(argv[0]) );. 
26280 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a   if( z2 ){.    z
26281 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  1 = contextMallo
26282 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
26283 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20  )n)+1);.    if( 
26284 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  z1 ){.      memc
26285 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b  py(z1, z2, n+1);
26286 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
26287 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  z1[i]; i++){.   
26288 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 73 71 6c       z1[i] = sql
26289 69 74 65 33 54 6f 6c 6f 77 65 72 28 7a 31 5b 69  ite3Tolower(z1[i
2628a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
2628b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2628c 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
2628d 63 68 61 72 20 2a 29 7a 31 2c 20 2d 31 2c 20 73  char *)z1, -1, s
2628e 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
2628f 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20    }.  }.}...#if 
26290 30 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  0  /* This funct
26291 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 75 73 65  ion is never use
26292 64 2e 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  d. */./*.** The 
26293 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 20 49  COALESCE() and I
26294 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f 6e  FNULL() function
26295 73 20 75 73 65 64 20 74 6f 20 62 65 20 69 6d 70  s used to be imp
26296 6c 65 6d 65 6e 74 65 64 20 61 73 20 73 68 6f 77  lemented as show
26297 6e 0a 2a 2a 20 68 65 72 65 2e 20 20 42 75 74 20  n.** here.  But 
26298 6e 6f 77 20 74 68 65 79 20 61 72 65 20 69 6d 70  now they are imp
26299 6c 65 6d 65 6e 74 65 64 20 61 73 20 56 44 42 45  lemented as VDBE
2629a 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20 75 6e   code so that un
2629b 75 73 65 64 20 61 72 67 75 6d 65 6e 74 73 0a 2a  used arguments.*
2629c 2a 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f  * do not have to
2629d 20 62 65 20 63 6f 6d 70 75 74 65 64 2e 20 20 54   be computed.  T
2629e 68 69 73 20 6c 65 67 61 63 79 20 69 6d 70 6c 65  his legacy imple
2629f 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72 65 74  mentation is ret
262a0 61 69 6e 65 64 20 61 73 0a 2a 2a 20 63 6f 6d 6d  ained as.** comm
262a1 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6d  ent..*/./*.** Im
262a2 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
262a3 74 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56  the IFNULL(), NV
262a4 4c 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43  L(), and COALESC
262a5 45 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  E() functions.  
262a6 0a 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 6f  .** All three do
262a7 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e   the same thing.
262a8 20 20 54 68 65 79 20 72 65 74 75 72 6e 20 74 68    They return th
262a9 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c  e first non-NULL
262aa 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  .** argument..*/
262ab 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 66 6e  .static void ifn
262ac 75 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ullFunc(.  sqlit
262ad 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
262ae 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
262af 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
262b0 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74   **argv.){.  int
262b1 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
262b2 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
262b3 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   if( SQLITE_NULL
262b4 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  !=sqlite3_value_
262b5 74 79 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b  type(argv[i]) ){
262b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
262b7 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
262b8 65 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20  ext, argv[i]);. 
262b9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
262ba 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  }.  }.}.#endif /
262bb 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64  * NOT USED */.#d
262bc 65 66 69 6e 65 20 69 66 6e 75 6c 6c 46 75 6e 63  efine ifnullFunc
262bd 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 2f   versionFunc   /
262be 2a 20 53 75 62 73 74 69 74 75 74 65 20 66 75 6e  * Substitute fun
262bf 63 74 69 6f 6e 20 2d 20 6e 65 76 65 72 20 63 61  ction - never ca
262c0 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  lled */../*.** I
262c1 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
262c2 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75   random().  Retu
262c3 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65  rn a random inte
262c4 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ger.  .*/.static
262c5 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63   void randomFunc
262c6 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
262c7 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
262c8 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
262c9 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
262ca 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c  otUsed2.){.  sql
262cb 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 55  ite_int64 r;.  U
262cc 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
262cd 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
262ce 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  d2);.  sqlite3_r
262cf 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
262d0 28 72 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20  (r), &r);.  if( 
262d1 72 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65  r<0 ){.    /* We
262d2 20 6e 65 65 64 20 74 6f 20 70 72 65 76 65 6e 74   need to prevent
262d3 20 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72   a random number
262d4 20 6f 66 20 30 78 38 30 30 30 30 30 30 30 30 30   of 0x8000000000
262d5 30 30 30 30 30 30 20 0a 20 20 20 20 2a 2a 20 28  000000 .    ** (
262d6 6f 72 20 2d 39 32 32 33 33 37 32 30 33 36 38 35  or -922337203685
262d7 34 37 37 35 38 30 38 29 20 73 69 6e 63 65 20 77  4775808) since w
262d8 68 65 6e 20 79 6f 75 20 64 6f 20 61 62 73 28 29  hen you do abs()
262d9 20 6f 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   of that.    ** 
262da 6e 75 6d 62 65 72 20 6f 66 20 79 6f 75 20 67 65  number of you ge
262db 74 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  t the same value
262dc 20 62 61 63 6b 20 61 67 61 69 6e 2e 20 20 54 6f   back again.  To
262dd 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   do this.    ** 
262de 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 69 73  in a way that is
262df 20 74 65 73 74 61 62 6c 65 2c 20 6d 61 73 6b 20   testable, mask 
262e0 74 68 65 20 73 69 67 6e 20 62 69 74 20 6f 66 66  the sign bit off
262e1 20 6f 66 20 6e 65 67 61 74 69 76 65 0a 20 20 20   of negative.   
262e2 20 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65 73 75   ** values, resu
262e3 6c 74 69 6e 67 20 69 6e 20 61 20 70 6f 73 69 74  lting in a posit
262e4 69 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 6e  ive value.  Then
262e5 20 74 61 6b 65 20 74 68 65 20 0a 20 20 20 20 2a   take the .    *
262e6 2a 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 20  * 2s complement 
262e7 6f 66 20 74 68 61 74 20 70 6f 73 69 74 69 76 65  of that positive
262e8 20 76 61 6c 75 65 2e 20 20 54 68 65 20 65 6e 64   value.  The end
262e9 20 72 65 73 75 6c 74 20 63 61 6e 0a 20 20 20 20   result can.    
262ea 2a 2a 20 74 68 65 72 65 66 6f 72 65 20 62 65 20  ** therefore be 
262eb 6e 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d 39 32  no less than -92
262ec 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
262ed 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 20  7..    */.    r 
262ee 3d 20 2d 28 72 20 5e 20 28 28 28 73 71 6c 69 74  = -(r ^ (((sqlit
262ef 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c 36 33 29  e3_int64)1)<<63)
262f0 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
262f1 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
262f2 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a  ntext, r);.}../*
262f3 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
262f4 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62  on of randomblob
262f5 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72  (N).  Return a r
262f6 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68  andom blob.** th
262f7 61 74 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f  at is N bytes lo
262f8 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
262f9 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20  id randomBlob(. 
262fa 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
262fb 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
262fc 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
262fd 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
262fe 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69  .  int n;.  unsi
262ff 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20  gned char *p;.  
26300 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
26301 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
26302 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e  METER(argc);.  n
26303 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26304 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  _int(argv[0]);. 
26305 20 69 66 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20   if( n<1 ){.    
26306 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d  n = 1;.  }.  p =
26307 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63   contextMalloc(c
26308 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66  ontext, n);.  if
26309 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
2630a 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c  e3_randomness(n,
2630b 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   p);.    sqlite3
2630c 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e  _result_blob(con
2630d 74 65 78 74 2c 20 28 63 68 61 72 2a 29 70 2c 20  text, (char*)p, 
2630e 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  n, sqlite3_free)
2630f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
26310 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
26311 20 74 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74   the last_insert
26312 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e  _rowid() SQL fun
26313 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75  ction.  The retu
26314 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 74  rn.** value is t
26315 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  he same as the s
26316 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
26317 72 74 5f 72 6f 77 69 64 28 29 20 41 50 49 20 66  rt_rowid() API f
26318 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
26319 69 63 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73  ic void last_ins
2631a 65 72 74 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c  ert_rowid(.  sql
2631b 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2631c 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f  ntext, .  int No
2631d 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65  tUsed, .  sqlite
2631e 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
2631f 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  d2.){.  sqlite3 
26320 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
26321 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
26322 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53  context);.  UNUS
26323 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
26324 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
26325 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
26326 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
26327 2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  , sqlite3_last_i
26328 6e 73 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29  nsert_rowid(db))
26329 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
2632a 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2632b 20 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66   changes() SQL f
2632c 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65  unction.  The re
2632d 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
2632e 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 74 68 65  e.** same as the
2632f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
26330 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  () API function.
26331 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
26332 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74  changes(.  sqlit
26333 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
26334 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
26335 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
26336 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
26337 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
26338 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
26339 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
2633a 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ext);.  UNUSED_P
2633b 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
2633c 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
2633d 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2633e 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  nt(context, sqli
2633f 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62 29 29  te3_changes(db))
26340 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
26341 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
26342 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29   total_changes()
26343 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20   SQL function.  
26344 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
26345 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20   is.** the same 
26346 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74  as the sqlite3_t
26347 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 41  otal_changes() A
26348 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  PI function..*/.
26349 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61  static void tota
2634a 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c  l_changes(.  sql
2634b 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2634c 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
2634d 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
2634e 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
2634f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
26350 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
26351 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
26352 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
26353 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
26354 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
26355 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26356 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
26357 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
26358 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a  ges(db));.}../*.
26359 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 64  ** A structure d
2635a 65 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64  efining how to d
2635b 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d  o GLOB-style com
2635c 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  parisons..*/.str
2635d 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
2635e 7b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b  {.  u8 matchAll;
2635f 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a  .  u8 matchOne;.
26360 20 20 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20    u8 matchSet;. 
26361 20 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a   u8 noCase;.};..
26362 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61  /*.** For LIKE a
26363 6e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67  nd GLOB matching
26364 20 6f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69   on EBCDIC machi
26365 6e 65 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74  nes, assume that
26366 20 65 76 65 72 79 0a 2a 2a 20 63 68 61 72 61 63   every.** charac
26367 74 65 72 20 69 73 20 65 78 61 63 74 6c 79 20 6f  ter is exactly o
26368 6e 65 20 62 79 74 65 20 69 6e 20 73 69 7a 65 2e  ne byte in size.
26369 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72    Also, all char
2636a 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 61 62  acters are.** ab
2636b 6c 65 20 74 6f 20 70 61 72 74 69 63 69 70 61 74  le to participat
2636c 65 20 69 6e 20 75 70 70 65 72 2d 63 61 73 65 2d  e in upper-case-
2636d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61  to-lower-case ma
2636e 70 70 69 6e 67 73 20 69 6e 20 45 42 43 44 49 43  ppings in EBCDIC
2636f 0a 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e 6c 79  .** whereas only
26370 20 63 68 61 72 61 63 74 65 72 73 20 6c 65 73 73   characters less
26371 20 74 68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e   than 0x80 do in
26372 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64   ASCII..*/.#if d
26373 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 42  efined(SQLITE_EB
26374 43 44 49 43 29 0a 23 20 64 65 66 69 6e 65 20 73  CDIC).# define s
26375 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 41  qlite3Utf8Read(A
26376 2c 43 29 20 20 20 20 28 2a 28 41 2b 2b 29 29 0a  ,C)    (*(A++)).
26377 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70  # define GlogUpp
26378 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20  erToLower(A)    
26379 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65   A = sqlite3Uppe
2637a 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73  rToLower[A].#els
2637b 65 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55  e.# define GlogU
2637c 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20  pperToLower(A)  
2637d 20 20 20 69 66 28 20 41 3c 30 78 38 30 20 29 7b     if( A<0x80 ){
2637e 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65   A = sqlite3Uppe
2637f 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23  rToLower[A]; }.#
26380 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 63 6f  endif..static co
26381 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61  nst struct compa
26382 72 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20  reInfo globInfo 
26383 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b  = { '*', '?', '[
26384 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63  ', 0 };./* The c
26385 6f 72 72 65 63 74 20 53 51 4c 2d 39 32 20 62 65  orrect SQL-92 be
26386 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 74 68  havior is for th
26387 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
26388 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73  to ignore.** cas
26389 65 2e 20 20 54 68 75 73 20 20 27 61 27 20 4c 49  e.  Thus  'a' LI
2638a 4b 45 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20  KE 'A' would be 
2638b 74 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20  true. */.static 
2638c 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d  const struct com
2638d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66  pareInfo likeInf
2638e 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27  oNorm = { '%', '
2638f 5f 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a  _',   0, 1 };./*
26390 20 49 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f   If SQLITE_CASE_
26391 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69  SENSITIVE_LIKE i
26392 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  s defined, then 
26393 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
26394 72 0a 2a 2a 20 69 73 20 63 61 73 65 20 73 65 6e  r.** is case sen
26395 73 69 74 69 76 65 20 63 61 75 73 69 6e 67 20 27  sitive causing '
26396 61 27 20 4c 49 4b 45 20 27 41 27 20 74 6f 20 62  a' LIKE 'A' to b
26397 65 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69  e false */.stati
26398 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63  c const struct c
26399 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49  ompareInfo likeI
2639a 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20  nfoAlt = { '%', 
2639b 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a  '_',   0, 0 };..
2639c 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77  /*.** Compare tw
2639d 6f 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20  o UTF-8 strings 
2639e 66 6f 72 20 65 71 75 61 6c 69 74 79 20 77 68 65  for equality whe
2639f 72 65 20 74 68 65 20 66 69 72 73 74 20 73 74 72  re the first str
263a0 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e  ing can.** poten
263a1 74 69 61 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f  tially be a "glo
263a2 62 22 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  b" expression.  
263a3 52 65 74 75 72 6e 20 74 72 75 65 20 28 31 29 20  Return true (1) 
263a4 69 66 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 74  if they.** are t
263a5 68 65 20 73 61 6d 65 20 61 6e 64 20 66 61 6c 73  he same and fals
263a6 65 20 28 30 29 20 69 66 20 74 68 65 79 20 61 72  e (0) if they ar
263a7 65 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a  e different..**.
263a8 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65  ** Globbing rule
263a9 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a  s:.**.**      '*
263aa 27 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20  '       Matches 
263ab 61 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20  any sequence of 
263ac 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61  zero or more cha
263ad 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20  racters..**.**  
263ae 20 20 20 20 27 3f 27 20 20 20 20 20 20 20 4d 61      '?'       Ma
263af 74 63 68 65 73 20 65 78 61 63 74 6c 79 20 6f 6e  tches exactly on
263b0 65 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a  e character..**.
263b1 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20  **     [...]    
263b2 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68    Matches one ch
263b3 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65  aracter from the
263b4 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f   enclosed list o
263b5 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
263b6 20 20 20 20 63 68 61 72 61 63 74 65 72 73 2e 0a      characters..
263b7 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d  **.**     [^...]
263b8 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65       Matches one
263b9 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69   character not i
263ba 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c  n the enclosed l
263bb 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20  ist..**.** With 
263bc 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e  the [...] and [^
263bd 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61  ...] matching, a
263be 20 27 5d 27 20 63 68 61 72 61 63 74 65 72 20 63   ']' character c
263bf 61 6e 20 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a  an be included.*
263c0 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 20 62 79  * in the list by
263c1 20 6d 61 6b 69 6e 67 20 69 74 20 74 68 65 20 66   making it the f
263c2 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61  irst character a
263c3 66 74 65 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e  fter '[' or '^'.
263c4 20 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20    A.** range of 
263c5 63 68 61 72 61 63 74 65 72 73 20 63 61 6e 20 62  characters can b
263c6 65 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e  e specified usin
263c7 67 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a  g '-'.  Example:
263c8 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63  .** "[a-z]" matc
263c9 68 65 73 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c  hes any single l
263ca 6f 77 65 72 2d 63 61 73 65 20 6c 65 74 74 65 72  ower-case letter
263cb 2e 20 20 54 6f 20 6d 61 74 63 68 20 61 20 27 2d  .  To match a '-
263cc 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68  ', make.** it th
263cd 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
263ce 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a   in the list..**
263cf 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
263d0 20 69 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63   is usually quic
263d1 6b 2c 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a  k, but can be N*
263d2 2a 32 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20  *2 in the worst 
263d3 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74  case..**.** Hint
263d4 73 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a 27 20  s: to match '*' 
263d5 6f 72 20 27 3f 27 2c 20 70 75 74 20 74 68 65 6d  or '?', put them
263d6 20 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20   in "[]".  Like 
263d7 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
263d8 20 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20      abc[*]xyz   
263d9 20 20 20 20 20 4d 61 74 63 68 65 73 20 22 61 62       Matches "ab
263da 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73  c*xyz" only.*/.s
263db 74 61 74 69 63 20 69 6e 74 20 70 61 74 74 65 72  tatic int patter
263dc 6e 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73  nCompare(.  cons
263dd 74 20 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20  t u8 *zPattern, 
263de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
263df 54 68 65 20 67 6c 6f 62 20 70 61 74 74 65 72 6e  The glob pattern
263e0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a   */.  const u8 *
263e1 7a 53 74 72 69 6e 67 2c 20 20 20 20 20 20 20 20  zString,        
263e2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74         /* The st
263e3 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20  ring to compare 
263e4 61 67 61 69 6e 73 74 20 74 68 65 20 67 6c 6f 62  against the glob
263e5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75   */.  const stru
263e6 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a  ct compareInfo *
263e7 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d  pInfo, /* Inform
263e8 61 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20  ation about how 
263e9 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72  to do the compar
263ea 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74  e */.  const int
263eb 20 65 73 63 20 20 20 20 20 20 20 20 20 20 20 20   esc            
263ec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
263ed 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20  scape character 
263ee 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63  */.){.  int c, c
263ef 32 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72 74 3b  2;.  int invert;
263f0 0a 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75  .  int seen;.  u
263f1 38 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e  8 matchOne = pIn
263f2 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20  fo->matchOne;.  
263f3 75 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49  u8 matchAll = pI
263f4 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20  nfo->matchAll;. 
263f5 20 75 38 20 6d 61 74 63 68 53 65 74 20 3d 20 70   u8 matchSet = p
263f6 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a  Info->matchSet;.
263f7 20 20 75 38 20 6e 6f 43 61 73 65 20 3d 20 70 49    u8 noCase = pI
263f8 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20  nfo->noCase; .  
263f9 69 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d  int prevEscape =
263fa 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20   0;     /* True 
263fb 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  if the previous 
263fc 63 68 61 72 61 63 74 65 72 20 77 61 73 20 27 65  character was 'e
263fd 73 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69  scape' */..  whi
263fe 6c 65 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33  le( (c = sqlite3
263ff 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
26400 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30  n,&zPattern))!=0
26401 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65   ){.    if( !pre
26402 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61  vEscape && c==ma
26403 74 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20  tchAll ){.      
26404 77 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65  while( (c=sqlite
26405 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
26406 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 20 3d  rn,&zPattern)) =
26407 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20 20  = matchAll.     
26408 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20 3d            || c =
26409 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20  = matchOne ){.  
2640a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 74        if( c==mat
2640b 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65 33  chOne && sqlite3
2640c 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67  Utf8Read(zString
2640d 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29  , &zString)==0 )
2640e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
2640f 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 0;.        }.
26410 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
26411 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
26412 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
26413 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65    }else if( c==e
26414 73 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20  sc ){.        c 
26415 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
26416 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
26417 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
26418 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
26419 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
2641a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2641b 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
2641c 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20  chSet ){.       
2641d 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20   assert( esc==0 
2641e 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  );         /* Th
2641f 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20  is is GLOB, not 
26420 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20  LIKE */.        
26421 61 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74  assert( matchSet
26422 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27  <0x80 );  /* '['
26423 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74   is a single-byt
26424 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
26425 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
26426 53 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72  String && patter
26427 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65  nCompare(&zPatte
26428 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70  rn[-1],zString,p
26429 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a  Info,esc)==0 ){.
2642a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2642b 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69  _SKIP_UTF8(zStri
2642c 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ng);.        }. 
2642d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a         return *z
2642e 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20  String!=0;.     
2642f 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20   }.      while( 
26430 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66  (c2 = sqlite3Utf
26431 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 26 7a  8Read(zString,&z
26432 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20  String))!=0 ){. 
26433 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73         if( noCas
26434 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47  e ){.          G
26435 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
26436 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47  c2);.          G
26437 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
26438 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68  c);.          wh
26439 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20  ile( c2 != 0 && 
2643a 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20  c2 != c ){.     
2643b 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
2643c 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72  te3Utf8Read(zStr
2643d 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a  ing, &zString);.
2643e 20 20 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67              Glog
2643f 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29  UpperToLower(c2)
26440 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
26441 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26442 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32         while( c2
26443 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63   != 0 && c2 != c
26444 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
26445 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
26446 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
26447 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
26448 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
26449 20 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30         if( c2==0
2644a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
2644b 20 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e       if( pattern
2644c 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e  Compare(zPattern
2644d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65  ,zString,pInfo,e
2644e 73 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  sc) ) return 1;.
2644f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
26450 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73  turn 0;.    }els
26451 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
26452 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65  e && c==matchOne
26453 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
26454 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
26455 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29  tring, &zString)
26456 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
26457 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
26458 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63  .    }else if( c
26459 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20  ==matchSet ){.  
2645a 20 20 20 20 69 6e 74 20 70 72 69 6f 72 5f 63 20      int prior_c 
2645b 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 0;.      asser
2645c 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20  t( esc==0 );    
2645d 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63  /* This only occ
2645e 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f  urs for GLOB, no
2645f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20  t LIKE */.      
26460 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  seen = 0;.      
26461 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20 20 20  invert = 0;.    
26462 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66    c = sqlite3Utf
26463 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26  8Read(zString, &
26464 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  zString);.      
26465 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72  if( c==0 ) retur
26466 6e 20 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20  n 0;.      c2 = 
26467 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
26468 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74  zPattern, &zPatt
26469 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ern);.      if( 
2646a 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20  c2=='^' ){.     
2646b 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20     invert = 1;. 
2646c 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
2646d 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
2646e 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29  tern, &zPattern)
2646f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
26470 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20  if( c2==']' ){. 
26471 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5d         if( c==']
26472 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20  ' ) seen = 1;.  
26473 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
26474 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
26475 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
26476 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77  .      }.      w
26477 68 69 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d  hile( c2 && c2!=
26478 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ']' ){.        i
26479 66 28 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50  f( c2=='-' && zP
2647a 61 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26  attern[0]!=']' &
2647b 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30  & zPattern[0]!=0
2647c 20 26 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b   && prior_c>0 ){
2647d 0a 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20  .          c2 = 
2647e 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
2647f 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74  zPattern, &zPatt
26480 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ern);.          
26481 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26  if( c>=prior_c &
26482 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d  & c<=c2 ) seen =
26483 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72   1;.          pr
26484 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20  ior_c = 0;.     
26485 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26486 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20 29 7b      if( c==c2 ){
26487 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 65 65  .            see
26488 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  n = 1;.         
26489 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 72 69   }.          pri
2648a 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 20  or_c = c2;.     
2648b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 32 20     }.        c2 
2648c 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
2648d 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
2648e 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  ttern);.      }.
2648f 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20        if( c2==0 
26490 7c 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76 65 72  || (seen ^ inver
26491 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)==0 ){.       
26492 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
26493 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
26494 20 65 73 63 3d 3d 63 20 26 26 20 21 70 72 65 76   esc==c && !prev
26495 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20  Escape ){.      
26496 70 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a  prevEscape = 1;.
26497 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26498 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
26499 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26  8Read(zString, &
2649a 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  zString);.      
2649b 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
2649c 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54        GlogUpperT
2649d 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20  oLower(c);.     
2649e 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f     GlogUpperToLo
2649f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d  wer(c2);.      }
264a0 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32  .      if( c!=c2
264a1 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
264a2 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
264a3 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
264a4 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
264a5 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d  return *zString=
264a6 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75  =0;.}../*.** Cou
264a7 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
264a8 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20   times that the 
264a9 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f  LIKE operator (o
264aa 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a  r GLOB which is.
264ab 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74  ** just a variat
264ac 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74  ion of LIKE) get
264ad 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20  s called.  This 
264ae 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
264af 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ing.** only..*/.
264b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
264b1 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
264b2 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63  t sqlite3_like_c
264b3 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
264b4 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
264b5 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
264b6 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69  ike() SQL functi
264b7 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  on.  This functi
264b8 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a  on implements.**
264b9 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49   the build-in LI
264ba 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68  KE operator.  Th
264bb 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
264bc 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
264bd 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65   is the.** patte
264be 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  rn and the secon
264bf 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
264c0 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74  e string.  So, t
264c1 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
264c2 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41  s:.**.**       A
264c3 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73   LIKE B.**.** is
264c4 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
264c5 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a  like(B,A)..**.**
264c6 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74   This same funct
264c7 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66  ion (with a diff
264c8 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66  erent compareInf
264c9 6f 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d  o structure) com
264ca 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f  putes.** the GLO
264cb 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  B operator..*/.s
264cc 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46  tatic void likeF
264cd 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
264ce 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
264cf 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
264d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
264d1 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
264d2 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
264d3 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65  zA, *zB;.  int e
264d4 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74  scape = 0;.  int
264d5 20 6e 50 61 74 3b 0a 20 20 73 71 6c 69 74 65 33   nPat;.  sqlite3
264d6 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
264d7 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
264d8 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 42  (context);..  zB
264d9 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
264da 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
264db 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76    zA = sqlite3_v
264dc 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
264dd 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20  ]);..  /* Limit 
264de 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
264df 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70  e LIKE or GLOB p
264e0 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20  attern to avoid 
264e1 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66  problems.  ** of
264e2 20 64 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20   deep recursion 
264e3 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72  and N*N behavior
264e4 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61   in patternCompa
264e5 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61  re()..  */.  nPa
264e6 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
264e7 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
264e8 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50  ;.  testcase( nP
264e9 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  at==db->aLimit[S
264ea 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45  QLITE_LIMIT_LIKE
264eb 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d  _PATTERN_LENGTH]
264ec 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
264ed 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  nPat==db->aLimit
264ee 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49  [SQLITE_LIMIT_LI
264ef 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
264f0 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50  H]+1 );.  if( nP
264f1 61 74 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  at > db->aLimit[
264f2 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b  SQLITE_LIMIT_LIK
264f3 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
264f4 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
264f5 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
264f6 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20  ntext, "LIKE or 
264f7 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f  GLOB pattern too
264f8 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a   complex", -1);.
264f9 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
264fa 20 20 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71    assert( zB==sq
264fb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
264fc 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a  (argv[0]) );  /*
264fd 20 45 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f   Encoding did no
264fe 74 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69  t change */..  i
264ff 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20  f( argc==3 ){.  
26500 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20    /* The escape 
26501 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67  character string
26502 20 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66   must consist of
26503 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20   a single UTF-8 
26504 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a  character..    *
26505 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  * Otherwise, ret
26506 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
26507 20 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75    */.    const u
26508 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45  nsigned char *zE
26509 73 63 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  sc = sqlite3_val
2650a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
2650b 3b 0a 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d  ;.    if( zEsc==
2650c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
2650d 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 43  if( sqlite3Utf8C
2650e 68 61 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45  harLen((char*)zE
2650f 73 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20  sc, -1)!=1 ){.  
26510 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
26511 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
26512 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53  , .          "ES
26513 43 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20  CAPE expression 
26514 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65  must be a single
26515 20 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29   character", -1)
26516 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
26517 20 20 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65      }.    escape
26518 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
26519 61 64 28 7a 45 73 63 2c 20 26 7a 45 73 63 29 3b  ad(zEsc, &zEsc);
2651a 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20 26 26  .  }.  if( zA &&
2651b 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72 75 63   zB ){.    struc
2651c 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70  t compareInfo *p
2651d 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75  Info = sqlite3_u
2651e 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
2651f 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
26520 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65  _TEST.    sqlite
26521 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a  3_like_count++;.
26522 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20 20  #endif.    .    
26523 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
26524 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61 74 74  nt(context, patt
26525 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20 7a  ernCompare(zB, z
26526 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61 70 65  A, pInfo, escape
26527 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
26528 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
26529 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28 78 2c  of the NULLIF(x,
2652a 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  y) function.  Th
2652b 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20  e result is the 
2652c 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
2652d 74 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e  t if the argumen
2652e 74 73 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  ts are different
2652f 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  .  The result is
26530 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20   NULL if the.** 
26531 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 65 71  arguments are eq
26532 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74 68 65  ual to each othe
26533 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
26534 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20 20  d nullifFunc(.  
26535 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
26536 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
26537 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
26538 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
26539 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  ){.  CollSeq *pC
2653a 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
2653b 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74  FuncCollSeq(cont
2653c 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ext);.  UNUSED_P
2653d 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
2653e 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
2653f 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b  MemCompare(argv[
26540 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43 6f  0], argv[1], pCo
26541 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ll)!=0 ){.    sq
26542 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
26543 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76  ue(context, argv
26544 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  [0]);.  }.}../*.
26545 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
26546 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  n of the sqlite_
26547 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69  version() functi
26548 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  on.  The result 
26549 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e 0a 2a  is the version.*
2654a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  * of the SQLite 
2654b 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20  library that is 
2654c 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  running..*/.stat
2654d 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f 6e 46  ic void versionF
2654e 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2654f 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
26550 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
26551 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
26552 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20  **NotUsed2.){.  
26553 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
26554 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
26555 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ed2);.  sqlite3_
26556 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
26557 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 65 72  ext, sqlite3_ver
26558 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c 49 54 45  sion, -1, SQLITE
26559 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a  _STATIC);.}../*.
2655a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2655b 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  n of the sqlite_
2655c 73 6f 75 72 63 65 5f 69 64 28 29 20 66 75 6e 63  source_id() func
2655d 74 69 6f 6e 2e 20 54 68 65 20 72 65 73 75 6c 74  tion. The result
2655e 20 69 73 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20   is a string.** 
2655f 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20  that identifies 
26560 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76  the particular v
26561 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 6f  ersion of the so
26562 75 72 63 65 20 63 6f 64 65 20 75 73 65 64 20 74  urce code used t
26563 6f 20 62 75 69 6c 64 0a 2a 2a 20 53 51 4c 69 74  o build.** SQLit
26564 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
26565 64 20 73 6f 75 72 63 65 69 64 46 75 6e 63 28 0a  d sourceidFunc(.
26566 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
26567 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
26568 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
26569 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
2656a 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45  Used2.){.  UNUSE
2656b 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
2656c 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
2656d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2656e 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
2656f 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44  SQLITE_SOURCE_ID
26570 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
26571 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61  TIC);.}../* Arra
26572 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67  y for converting
26573 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73   from half-bytes
26574 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20   (nybbles) into 
26575 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69 67  ASCII hex.** dig
26576 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63  its. */.static c
26577 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64 69 67  onst char hexdig
26578 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c  its[] = {.  '0',
26579 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c 20   '1', '2', '3', 
2657a 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20 27  '4', '5', '6', '
2657b 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c 20  7',.  '8', '9', 
2657c 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20 27  'A', 'B', 'C', '
2657d 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b  D', 'E', 'F' .};
2657e 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45  ../*.** EXPERIME
2657f 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20 6e  NTAL - This is n
26580 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20 66  ot an official f
26581 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e  unction.  The in
26582 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63  terface may.** c
26583 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75 6e  hange.  This fun
26584 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70 70  ction may disapp
26585 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69  ear.  Do not wri
26586 74 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 70  te code that dep
26587 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20  ends.** on this 
26588 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
26589 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2658a 66 20 74 68 65 20 51 55 4f 54 45 28 29 20 66 75  f the QUOTE() fu
2658b 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75  nction.  This fu
2658c 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  nction takes a s
2658d 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  ingle.** argumen
2658e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d  t.  If the argum
2658f 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c 20  ent is numeric, 
26590 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
26591 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 0a   is the same as.
26592 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  ** the argument.
26593 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e    If the argumen
26594 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
26595 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74  eturn value is t
26596 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55  he string.** "NU
26597 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c  LL".  Otherwise,
26598 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
26599 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e   enclosed in sin
2659a 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68 0a  gle quotes with.
2659b 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20  ** single-quote 
2659c 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74  escapes..*/.stat
2659d 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75 6e  ic void quoteFun
2659e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
2659f 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
265a0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
265a1 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61  lue **argv){.  a
265a2 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
265a3 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
265a4 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77  ETER(argc);.  sw
265a5 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
265a6 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
265a7 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
265a8 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20  LITE_INTEGER:.  
265a9 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
265aa 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  OAT: {.      sql
265ab 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
265ac 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b  e(context, argv[
265ad 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0]);.      break
265ae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
265af 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a   SQLITE_BLOB: {.
265b0 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78        char *zTex
265b1 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61  t = 0;.      cha
265b2 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d  r const *zBlob =
265b3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
265b4 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
265b5 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20      int nBlob = 
265b6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
265b7 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
265b8 20 20 20 20 61 73 73 65 72 74 28 20 7a 42 6c 6f      assert( zBlo
265b9 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  b==sqlite3_value
265ba 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29  _blob(argv[0]) )
265bb 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67  ; /* No encoding
265bc 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20   change */.     
265bd 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20 2a   zText = (char *
265be 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63  )contextMalloc(c
265bf 6f 6e 74 65 78 74 2c 20 28 32 2a 28 69 36 34 29  ontext, (2*(i64)
265c0 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20 20 20  nBlob)+4); .    
265c1 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20    if( zText ){. 
265c2 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
265c3 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
265c4 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20  <nBlob; i++){.  
265c5 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69          zText[(i
265c6 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64 69 67 69  *2)+2] = hexdigi
265c7 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29  ts[(zBlob[i]>>4)
265c8 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20  &0x0F];.        
265c9 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d    zText[(i*2)+3]
265ca 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42   = hexdigits[(zB
265cb 6c 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20  lob[i])&0x0F];. 
265cc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
265cd 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29   zText[(nBlob*2)
265ce 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  +2] = '\'';.    
265cf 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62      zText[(nBlob
265d0 2a 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20  *2)+3] = '\0';. 
265d1 20 20 20 20 20 20 20 7a 54 65 78 74 5b 30 5d 20         zText[0] 
265d2 3d 20 27 58 27 3b 0a 20 20 20 20 20 20 20 20 7a  = 'X';.        z
265d3 54 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a  Text[1] = '\'';.
265d4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
265d5 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
265d6 65 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20  ext, zText, -1, 
265d7 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
265d8 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
265d9 65 33 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a  e3_free(zText);.
265da 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
265db 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
265dc 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
265dd 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 6a   {.      int i,j
265de 3b 0a 20 20 20 20 20 20 75 36 34 20 6e 3b 0a 20  ;.      u64 n;. 
265df 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67       const unsig
265e0 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67 20 3d  ned char *zArg =
265e1 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
265e2 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
265e3 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20      char *z;..  
265e4 20 20 20 20 69 66 28 20 7a 41 72 67 3d 3d 30 20      if( zArg==0 
265e5 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
265e6 66 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41  for(i=0, n=0; zA
265e7 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 28  rg[i]; i++){ if(
265e8 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29   zArg[i]=='\'' )
265e9 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 7a 20   n++; }.      z 
265ea 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  = contextMalloc(
265eb 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 69  context, ((i64)i
265ec 29 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b 0a 20  )+((i64)n)+3);. 
265ed 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20       if( z ){.  
265ee 20 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27 5c 27        z[0] = '\'
265ef 27 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  ';.        for(i
265f0 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d  =0, j=1; zArg[i]
265f1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
265f2 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b    z[j++] = zArg[
265f3 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  i];.          if
265f4 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20  ( zArg[i]=='\'' 
265f5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a  ){.            z
265f6 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20  [j++] = '\'';.  
265f7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
265f8 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b    }.        z[j+
265f9 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  +] = '\'';.     
265fa 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20     z[j] = 0;.   
265fb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
265fc 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
265fd 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  , z, j, sqlite3_
265fe 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  free);.      }. 
265ff 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
26600 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
26601 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
26602 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
26603 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
26604 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  TE_NULL );.     
26605 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
26606 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 4e  text(context, "N
26607 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f  ULL", 4, SQLITE_
26608 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 62  STATIC);.      b
26609 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
2660a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78  }../*.** The hex
2660b 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e  () function.  In
2660c 74 65 72 70 72 65 74 20 74 68 65 20 61 72 67 75  terpret the argu
2660d 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e 20  ment as a blob. 
2660e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78   Return.** a hex
2660f 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 69  adecimal renderi
26610 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73  ng as text..*/.s
26611 74 61 74 69 63 20 76 6f 69 64 20 68 65 78 46 75  tatic void hexFu
26612 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
26613 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
26614 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
26615 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
26616 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  gv.){.  int i, n
26617 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
26618 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a  ed char *pBlob;.
26619 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a    char *zHex, *z
2661a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
2661b 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ==1 );.  UNUSED_
2661c 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
2661d 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74  .  pBlob = sqlit
2661e 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
2661f 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71  gv[0]);.  n = sq
26620 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
26621 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73  s(argv[0]);.  as
26622 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c  sert( pBlob==sql
26623 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
26624 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20  argv[0]) );  /* 
26625 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e  No encoding chan
26626 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78  ge */.  z = zHex
26627 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
26628 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29  (context, ((i64)
26629 6e 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69 66 28  n)*2 + 1);.  if(
2662a 20 7a 48 65 78 20 29 7b 0a 20 20 20 20 66 6f 72   zHex ){.    for
2662b 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20  (i=0; i<n; i++, 
2662c 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20  pBlob++){.      
2662d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 20  unsigned char c 
2662e 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20  = *pBlob;.      
2662f 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69  *(z++) = hexdigi
26630 74 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a  ts[(c>>4)&0xf];.
26631 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68        *(z++) = h
26632 65 78 64 69 67 69 74 73 5b 63 26 30 78 66 5d 3b  exdigits[c&0xf];
26633 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20  .    }.    *z = 
26634 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  0;.    sqlite3_r
26635 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
26636 78 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73  xt, zHex, n*2, s
26637 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
26638 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a  }.}../*.** The z
26639 65 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74  eroblob(N) funct
2663a 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 7a 65  ion returns a ze
2663b 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f  ro-filled blob o
2663c 66 20 73 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a  f size N bytes..
2663d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a  */.static void z
2663e 65 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73  eroblobFunc(.  s
2663f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26640 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
26641 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
26642 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
26643 20 69 36 34 20 6e 3b 0a 20 20 73 71 6c 69 74 65   i64 n;.  sqlite
26644 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
26645 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
26646 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73  e(context);.  as
26647 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
26648 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
26649 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d  TER(argc);.  n =
2664a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
2664b 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20  nt64(argv[0]);. 
2664c 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62   testcase( n==db
2664d 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
2664e 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
2664f 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
26650 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
26651 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b  E_LIMIT_LENGTH]+
26652 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e 64 62 2d  1 );.  if( n>db-
26653 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
26654 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
26655 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
26656 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
26657 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73  context);.  }els
26658 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
26659 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63  esult_zeroblob(c
2665a 6f 6e 74 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b  ontext, (int)n);
2665b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
2665c 65 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63  e replace() func
2665d 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67  tion.  Three arg
2665e 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73  uments are all s
2665f 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20  trings: call.** 
26660 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43  them A, B, and C
26661 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  . The result is 
26662 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68  also a string wh
26663 69 63 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a  ich is derived.*
26664 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c  * from A by repl
26665 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75  acing every occu
26666 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20  rance of B with 
26667 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a  C.  The match.**
26668 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e 20   must be exact. 
26669 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
2666a 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65  nces are not use
2666b 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
2666c 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20  d replaceFunc(. 
2666d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2666e 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2666f 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
26670 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
26671 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
26672 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20  d char *zStr;   
26673 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75       /* The inpu
26674 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20  t string A */.  
26675 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
26676 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20  har *zPattern;  
26677 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e    /* The pattern
26678 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63   string B */.  c
26679 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2667a 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20  ar *zRep;       
2667b 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d   /* The replacem
2667c 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a  ent string C */.
2667d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
2667e 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20  *zOut;          
2667f 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75      /* The outpu
26680 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b  t */.  int nStr;
26681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26682 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20  /* Size of zStr 
26683 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72  */.  int nPatter
26684 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
26685 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72   Size of zPatter
26686 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b  n */.  int nRep;
26687 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26688 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20  /* Size of zRep 
26689 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20  */.  i64 nOut;  
2668a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2668b 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66   Maximum size of
2668c 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c   zOut */.  int l
2668d 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  oopLimit;       
2668e 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72      /* Last zStr
2668f 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61  [] that might ma
26690 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a  tch zPattern[] *
26691 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  /.  int i, j;   
26692 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26693 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
26694 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
26695 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ==3 );.  UNUSED_
26696 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
26697 0a 20 20 7a 53 74 72 20 3d 20 73 71 6c 69 74 65  .  zStr = sqlite
26698 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
26699 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 53 74  v[0]);.  if( zSt
2669a 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
2669b 20 6e 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f   nStr = sqlite3_
2669c 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
2669d 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20  [0]);.  assert( 
2669e 7a 53 74 72 3d 3d 73 71 6c 69 74 65 33 5f 76 61  zStr==sqlite3_va
2669f 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
266a0 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f  ) );  /* No enco
266a1 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20  ding change */. 
266a2 20 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69   zPattern = sqli
266a3 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
266a4 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[1]);.  if( z
266a5 50 61 74 74 65 72 6e 3d 3d 30 20 29 7b 0a 20 20  Pattern==0 ){.  
266a6 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
266a7 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
266a8 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[1])==SQLITE_NU
266a9 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  LL.            |
266aa 7c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  | sqlite3_contex
266ab 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
266ac 65 78 74 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ext)->mallocFail
266ad 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ed );.    return
266ae 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 50 61 74  ;.  }.  if( zPat
266af 74 65 72 6e 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20  tern[0]==0 ){.  
266b0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
266b1 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
266b2 76 5b 31 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55  v[1])!=SQLITE_NU
266b3 4c 4c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  LL );.    sqlite
266b4 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
266b5 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29  ontext, argv[0])
266b6 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
266b7 7d 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d 20 73  }.  nPattern = s
266b8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
266b9 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61  es(argv[1]);.  a
266ba 73 73 65 72 74 28 20 7a 50 61 74 74 65 72 6e 3d  ssert( zPattern=
266bb 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
266bc 65 78 74 28 61 72 67 76 5b 31 5d 29 20 29 3b 20  ext(argv[1]) ); 
266bd 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20   /* No encoding 
266be 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70  change */.  zRep
266bf 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
266c0 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a  _text(argv[2]);.
266c1 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 20    if( zRep==0 ) 
266c2 72 65 74 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d  return;.  nRep =
266c3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
266c4 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ytes(argv[2]);. 
266c5 20 61 73 73 65 72 74 28 20 7a 52 65 70 3d 3d 73   assert( zRep==s
266c6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
266c7 74 28 61 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20  t(argv[2]) );.  
266c8 6e 4f 75 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b  nOut = nStr + 1;
266c9 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 75 74 3c  .  assert( nOut<
266ca 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
266cb 48 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f  H );.  zOut = co
266cc 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
266cd 65 78 74 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b  ext, (i64)nOut);
266ce 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29  .  if( zOut==0 )
266cf 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
266d0 7d 0a 20 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20  }.  loopLimit = 
266d1 6e 53 74 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b  nStr - nPattern;
266d2 20 20 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20    .  for(i=j=0; 
266d3 69 3c 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b  i<=loopLimit; i+
266d4 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 53 74 72  +){.    if( zStr
266d5 5b 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d  [i]!=zPattern[0]
266d6 20 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72   || memcmp(&zStr
266d7 5b 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e  [i], zPattern, n
266d8 50 61 74 74 65 72 6e 29 20 29 7b 0a 20 20 20 20  Pattern) ){.    
266d9 20 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53    zOut[j++] = zS
266da 74 72 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  tr[i];.    }else
266db 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64  {.      u8 *zOld
266dc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20  ;.      sqlite3 
266dd 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
266de 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
266df 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
266e0 6e 4f 75 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e  nOut += nRep - n
266e1 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 20 20 74  Pattern;.      t
266e2 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 31 3d  estcase( nOut-1=
266e3 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
266e4 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
266e5 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
266e6 73 65 28 20 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e  se( nOut-2==db->
266e7 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
266e8 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20  MIT_LENGTH] );. 
266e9 20 20 20 20 20 69 66 28 20 6e 4f 75 74 2d 31 3e       if( nOut-1>
266ea 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
266eb 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
266ec 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
266ed 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
266ee 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
266ef 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
266f0 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 29  DbFree(db, zOut)
266f1 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
266f2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
266f3 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20  zOld = zOut;.   
266f4 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65     zOut = sqlite
266f5 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20  3_realloc(zOut, 
266f6 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20  (int)nOut);.    
266f7 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b    if( zOut==0 ){
266f8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
266f9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
266fa 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
266fb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
266fc 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20  ree(db, zOld);. 
266fd 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
266fe 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d       }.      mem
266ff 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52  cpy(&zOut[j], zR
26700 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20 20 20 20  ep, nRep);.     
26701 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20   j += nRep;.    
26702 20 20 69 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d    i += nPattern-
26703 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  1;.    }.  }.  a
26704 73 73 65 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b  ssert( j+nStr-i+
26705 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d  1==nOut );.  mem
26706 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a  cpy(&zOut[j], &z
26707 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b  Str[i], nStr-i);
26708 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69  .  j += nStr - i
26709 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e  ;.  assert( j<=n
2670a 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d  Out );.  zOut[j]
2670b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
2670c 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
2670d 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74  ext, (char*)zOut
2670e 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  , j, sqlite3_fre
2670f 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  e);.}../*.** Imp
26710 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
26711 68 65 20 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d  he TRIM(), LTRIM
26712 28 29 2c 20 61 6e 64 20 52 54 52 49 4d 28 29 20  (), and RTRIM() 
26713 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68  functions..** Th
26714 65 20 75 73 65 72 64 61 74 61 20 69 73 20 30 78  e userdata is 0x
26715 31 20 66 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c  1 for left trim,
26716 20 30 78 32 20 66 6f 72 20 72 69 67 68 74 20 74   0x2 for right t
26717 72 69 6d 2c 20 30 78 33 20 66 6f 72 20 62 6f 74  rim, 0x3 for bot
26718 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  h..*/.static voi
26719 64 20 74 72 69 6d 46 75 6e 63 28 0a 20 20 73 71  d trimFunc(.  sq
2671a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2671b 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
2671c 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
2671d 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
2671e 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2671f 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20  har *zIn;       
26720 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e    /* Input strin
26721 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73  g */.  const uns
26722 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 43 68 61  igned char *zCha
26723 72 53 65 74 3b 20 20 20 20 2f 2a 20 53 65 74 20  rSet;    /* Set 
26724 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f  of characters to
26725 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e   trim */.  int n
26726 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
26727 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26728 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
26729 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e  in input */.  in
2672a 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  t flags;        
2672b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2672c 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74 20 20  /* 1: trimleft  
2672d 32 3a 20 74 72 69 6d 72 69 67 68 74 20 20 33 3a  2: trimright  3:
2672e 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69   trim */.  int i
2672f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26731 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
26732 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
26733 2a 61 4c 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  *aLen = 0;      
26734 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
26735 20 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20   each character 
26736 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20  in zCharSet */. 
26737 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
26738 2a 61 7a 43 68 61 72 20 3d 20 30 3b 20 20 20 20  *azChar = 0;    
26739 20 20 20 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c     /* Individual
2673a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a   characters in z
2673b 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74  CharSet */.  int
2673c 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20   nChar;         
2673d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2673e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
2673f 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53  acters in zCharS
26740 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c  et */..  if( sql
26741 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
26742 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
26743 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74  _NULL ){.    ret
26744 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d  urn;.  }.  zIn =
26745 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
26746 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
26747 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74  if( zIn==0 ) ret
26748 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c  urn;.  nIn = sql
26749 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2674a 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73  (argv[0]);.  ass
2674b 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65  ert( zIn==sqlite
2674c 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2674d 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 61  v[0]) );.  if( a
2674e 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74  rgc==1 ){.    st
2674f 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
26750 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b  ned char lenOne[
26751 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73  ] = { 1 };.    s
26752 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 63  tatic unsigned c
26753 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e  har * const azOn
26754 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22 20 22  e[] = { (u8*)" "
26755 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20 3d 20   };.    nChar = 
26756 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20 28 75  1;.    aLen = (u
26757 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61  8*)lenOne;.    a
26758 7a 43 68 61 72 20 3d 20 28 75 6e 73 69 67 6e 65  zChar = (unsigne
26759 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b  d char **)azOne;
2675a 0a 20 20 20 20 7a 43 68 61 72 53 65 74 20 3d 20  .    zCharSet = 
2675b 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  0;.  }else if( (
2675c 7a 43 68 61 72 53 65 74 20 3d 20 73 71 6c 69 74  zCharSet = sqlit
2675d 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2675e 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20  gv[1]))==0 ){.  
2675f 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
26760 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  e{.    const uns
26761 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20  igned char *z;. 
26762 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65     for(z=zCharSe
26763 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20  t, nChar=0; *z; 
26764 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20  nChar++){.      
26765 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
26766 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  (z);.    }.    i
26767 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20  f( nChar>0 ){.  
26768 20 20 20 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e      azChar = con
26769 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
2676a 78 74 2c 20 28 28 69 36 34 29 6e 43 68 61 72 29  xt, ((i64)nChar)
2676b 2a 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b  *(sizeof(char*)+
2676c 31 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61  1));.      if( a
2676d 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20  zChar==0 ){.    
2676e 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2676f 20 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d    }.      aLen =
26770 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a   (unsigned char*
26771 29 26 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b  )&azChar[nChar];
26772 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68  .      for(z=zCh
26773 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20  arSet, nChar=0; 
26774 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20  *z; nChar++){.  
26775 20 20 20 20 20 20 61 7a 43 68 61 72 5b 6e 43 68        azChar[nCh
26776 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  ar] = (unsigned 
26777 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20  char *)z;.      
26778 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
26779 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 61  F8(z);.        a
2677a 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38  Len[nChar] = (u8
2677b 29 28 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68  )(z - azChar[nCh
2677c 61 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ar]);.      }.  
2677d 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43    }.  }.  if( nC
2677e 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61  har>0 ){.    fla
2677f 67 73 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f  gs = SQLITE_PTR_
26780 54 4f 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75  TO_INT(sqlite3_u
26781 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
26782 29 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67  ));.    if( flag
26783 73 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77  s & 1 ){.      w
26784 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20  hile( nIn>0 ){. 
26785 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d         int len =
26786 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   0;.        for(
26787 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b  i=0; i<nChar; i+
26788 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  +){.          le
26789 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20  n = aLen[i];.   
2678a 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d         if( len<=
2678b 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 7a 49  nIn && memcmp(zI
2678c 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65  n, azChar[i], le
2678d 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  n)==0 ) break;. 
2678e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2678f 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20   if( i>=nChar ) 
26790 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a  break;.        z
26791 49 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  In += len;.     
26792 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20     nIn -= len;. 
26793 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
26794 20 69 66 28 20 66 6c 61 67 73 20 26 20 32 20 29   if( flags & 2 )
26795 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e  {.      while( n
26796 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  In>0 ){.        
26797 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20  int len = 0;.   
26798 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
26799 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nChar; i++){.   
2679a 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65         len = aLe
2679b 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  n[i];.          
2679c 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20  if( len<=nIn && 
2679d 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d  memcmp(&zIn[nIn-
2679e 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c  len],azChar[i],l
2679f 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  en)==0 ) break;.
267a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
267a1 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29    if( i>=nChar )
267a2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
267a3 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20  nIn -= len;.    
267a4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
267a5 28 20 7a 43 68 61 72 53 65 74 20 29 7b 0a 20 20  ( zCharSet ){.  
267a6 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
267a7 28 61 7a 43 68 61 72 29 3b 0a 20 20 20 20 7d 0a  (azChar);.    }.
267a8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
267a9 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
267aa 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e 2c 20 6e  t, (char*)zIn, n
267ab 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  In, SQLITE_TRANS
267ac 49 45 4e 54 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65  IENT);.}...#ifde
267ad 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58  f SQLITE_SOUNDEX
267ae 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
267af 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64  he soundex encod
267b0 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a  ing of a word..*
267b1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f  /.static void so
267b2 75 6e 64 65 78 46 75 6e 63 28 0a 20 20 73 71 6c  undexFunc(.  sql
267b3 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
267b4 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
267b5 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
267b6 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
267b7 68 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a  har zResult[8];.
267b8 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b    const u8 *zIn;
267b9 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  .  int i, j;.  s
267ba 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
267bb 67 6e 65 64 20 63 68 61 72 20 69 43 6f 64 65 5b  gned char iCode[
267bc 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20  ] = {.    0, 0, 
267bd 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
267be 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
267bf 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
267c0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267c1 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
267c2 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
267c3 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
267c4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267c5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
267c6 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  ,.    0, 0, 0, 0
267c7 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
267c8 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267c9 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20  0, 0,.    0, 0, 
267ca 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32  1, 2, 3, 0, 1, 2
267cb 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c  , 0, 0, 2, 2, 4,
267cc 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c   5, 5, 0,.    1,
267cd 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20   2, 6, 2, 3, 0, 
267ce 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30  1, 0, 2, 0, 2, 0
267cf 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
267d0 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c    0, 0, 1, 2, 3,
267d1 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20   0, 1, 2, 0, 0, 
267d2 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30  2, 2, 4, 5, 5, 0
267d3 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32  ,.    1, 2, 6, 2
267d4 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c  , 3, 0, 1, 0, 2,
267d5 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 2, 0, 0, 0, 
267d6 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73  0, 0,.  };.  ass
267d7 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
267d8 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c    zIn = (u8*)sql
267d9 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
267da 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
267db 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28  zIn==0 ) zIn = (
267dc 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d  u8*)"";.  for(i=
267dd 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21 73 71  0; zIn[i] && !sq
267de 6c 69 74 65 33 49 73 61 6c 70 68 61 28 7a 49 6e  lite3Isalpha(zIn
267df 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69  [i]); i++){}.  i
267e0 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20  f( zIn[i] ){.   
267e1 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d 20 69   u8 prevcode = i
267e2 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66  Code[zIn[i]&0x7f
267e3 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 30  ];.    zResult[0
267e4 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 75 70 70  ] = sqlite3Toupp
267e5 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20  er(zIn[i]);.    
267e6 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20  for(j=1; j<4 && 
267e7 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  zIn[i]; i++){.  
267e8 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 69      int code = i
267e9 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66  Code[zIn[i]&0x7f
267ea 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63 6f 64  ];.      if( cod
267eb 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  e>0 ){.        i
267ec 66 28 20 63 6f 64 65 21 3d 70 72 65 76 63 6f 64  f( code!=prevcod
267ed 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  e ){.          p
267ee 72 65 76 63 6f 64 65 20 3d 20 63 6f 64 65 3b 0a  revcode = code;.
267ef 20 20 20 20 20 20 20 20 20 20 7a 52 65 73 75 6c            zResul
267f0 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20 2b 20  t[j++] = code + 
267f1 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  '0';.        }. 
267f2 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
267f3 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 30      prevcode = 0
267f4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
267f5 20 20 20 20 77 68 69 6c 65 28 20 6a 3c 34 20 29      while( j<4 )
267f6 7b 0a 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b  {.      zResult[
267f7 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20  j++] = '0';.    
267f8 7d 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d  }.    zResult[j]
267f9 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
267fa 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
267fb 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20  ntext, zResult, 
267fc 34 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  4, SQLITE_TRANSI
267fd 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ENT);.  }else{. 
267fe 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
267ff 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
26800 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c 49 54  "?000", 4, SQLIT
26801 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  E_STATIC);.  }.}
26802 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
26803 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
26804 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a  D_EXTENSION./*.*
26805 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  * A function tha
26806 74 20 6c 6f 61 64 73 20 61 20 73 68 61 72 65 64  t loads a shared
26807 2d 6c 69 62 72 61 72 79 20 65 78 74 65 6e 73 69  -library extensi
26808 6f 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20  on then returns 
26809 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
2680a 76 6f 69 64 20 6c 6f 61 64 45 78 74 28 73 71 6c  void loadExt(sql
2680b 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2680c 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
2680d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2680e 2a 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73 74 20  *argv){.  const 
2680f 63 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20 28 63  char *zFile = (c
26810 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
26811 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
26812 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74  rgv[0]);.  const
26813 20 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a 20 20   char *zProc;.  
26814 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
26815 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
26816 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
26817 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ;.  char *zErrMs
26818 67 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72  g = 0;..  if( ar
26819 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 50 72  gc==2 ){.    zPr
2681a 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  oc = (const char
2681b 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
2681c 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
2681d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 50 72    }else{.    zPr
2681e 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  oc = 0;.  }.  if
2681f 28 20 7a 46 69 6c 65 20 26 26 20 73 71 6c 69 74  ( zFile && sqlit
26820 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  e3_load_extensio
26821 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72  n(db, zFile, zPr
26822 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29 20 29 7b  oc, &zErrMsg) ){
26823 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
26824 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
26825 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31 29 3b  t, zErrMsg, -1);
26826 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
26827 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a  e(zErrMsg);.  }.
26828 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
26829 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
2682a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2682b 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68  ructure holds th
2682c 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 0a 2a  e context of a.*
2682d 2a 20 73 75 6d 28 29 20 6f 72 20 61 76 67 28 29  * sum() or avg()
2682e 20 61 67 67 72 65 67 61 74 65 20 63 6f 6d 70 75   aggregate compu
2682f 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  tation..*/.typed
26830 65 66 20 73 74 72 75 63 74 20 53 75 6d 43 74 78  ef struct SumCtx
26831 20 53 75 6d 43 74 78 3b 0a 73 74 72 75 63 74 20   SumCtx;.struct 
26832 53 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75 62 6c  SumCtx {.  doubl
26833 65 20 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20  e rSum;      /* 
26834 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  Floating point s
26835 75 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53 75 6d  um */.  i64 iSum
26836 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74  ;         /* Int
26837 65 67 65 72 20 73 75 6d 20 2a 2f 20 20 20 0a 20  eger sum */   . 
26838 20 69 36 34 20 63 6e 74 3b 20 20 20 20 20 20 20   i64 cnt;       
26839 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2683a 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65 64 20  elements summed 
2683b 2a 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c 6f 77  */.  u8 overflow
2683c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
2683d 66 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c  f integer overfl
2683e 6f 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20  ow seen */.  u8 
2683f 61 70 70 72 6f 78 3b 20 20 20 20 20 20 20 20 2f  approx;        /
26840 2a 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d 69 6e  * True if non-in
26841 74 65 67 65 72 20 76 61 6c 75 65 20 77 61 73 20  teger value was 
26842 69 6e 70 75 74 20 74 6f 20 74 68 65 20 73 75 6d  input to the sum
26843 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f   */.};../*.** Ro
26844 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 63  utines used to c
26845 6f 6d 70 75 74 65 20 74 68 65 20 73 75 6d 2c 20  ompute the sum, 
26846 61 76 65 72 61 67 65 2c 20 61 6e 64 20 74 6f 74  average, and tot
26847 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 55  al..**.** The SU
26848 4d 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 6c  M() function fol
26849 6c 6f 77 73 20 74 68 65 20 28 62 72 6f 6b 65 6e  lows the (broken
2684a 29 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 77  ) SQL standard w
2684b 68 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  hich means.** th
2684c 61 74 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  at it returns NU
2684d 4c 4c 20 69 66 20 69 74 20 73 75 6d 73 20 6f 76  LL if it sums ov
2684e 65 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20 20 54  er no inputs.  T
2684f 4f 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a 2a 20  OTAL returns.** 
26850 30 2e 30 20 69 6e 20 74 68 61 74 20 63 61 73 65  0.0 in that case
26851 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20  .  In addition, 
26852 54 4f 54 41 4c 20 61 6c 77 61 79 73 20 72 65 74  TOTAL always ret
26853 75 72 6e 73 20 61 20 66 6c 6f 61 74 20 77 68 65  urns a float whe
26854 72 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20  re.** SUM might 
26855 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65  return an intege
26856 72 20 69 66 20 69 74 20 6e 65 76 65 72 20 65 6e  r if it never en
26857 63 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f 61 74  counters a float
26858 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c  ing point.** val
26859 75 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 72  ue.  TOTAL never
2685a 20 66 61 69 6c 73 2c 20 62 75 74 20 53 55 4d 20   fails, but SUM 
2685b 6d 69 67 68 74 20 74 68 72 6f 75 67 68 20 61 6e  might through an
2685c 20 65 78 63 65 70 74 69 6f 6e 20 69 66 0a 2a 2a   exception if.**
2685d 20 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e   it overflows an
2685e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61   integer..*/.sta
2685f 74 69 63 20 76 6f 69 64 20 73 75 6d 53 74 65 70  tic void sumStep
26860 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
26861 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
26862 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
26863 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 53 75  ue **argv){.  Su
26864 6d 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74 20 74  mCtx *p;.  int t
26865 79 70 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61  ype;.  assert( a
26866 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
26867 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
26868 63 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  c);.  p = sqlite
26869 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
2686a 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a  ext(context, siz
2686b 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79 70 65  eof(*p));.  type
2686c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2686d 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 61 72  _numeric_type(ar
2686e 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 20  gv[0]);.  if( p 
2686f 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54 45 5f  && type!=SQLITE_
26870 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d 3e 63  NULL ){.    p->c
26871 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 74 79  nt++;.    if( ty
26872 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47  pe==SQLITE_INTEG
26873 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20  ER ){.      i64 
26874 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  v = sqlite3_valu
26875 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
26876 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20  ;.      p->rSum 
26877 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20  += v;.      if( 
26878 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f 76  (p->approx|p->ov
26879 65 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a 20 20  erflow)==0 ){.  
2687a 20 20 20 20 20 20 69 36 34 20 69 4e 65 77 53 75        i64 iNewSu
2687b 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20 76 3b  m = p->iSum + v;
2687c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 31 20  .        int s1 
2687d 3d 20 28 69 6e 74 29 28 70 2d 3e 69 53 75 6d 20  = (int)(p->iSum 
2687e 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a  >> (sizeof(i64)*
2687f 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 69  8-1));.        i
26880 6e 74 20 73 32 20 3d 20 28 69 6e 74 29 28 76 20  nt s2 = (int)(v 
26881 20 20 20 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66        >> (sizeof
26882 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20  (i64)*8-1));.   
26883 20 20 20 20 20 69 6e 74 20 73 33 20 3d 20 28 69       int s3 = (i
26884 6e 74 29 28 69 4e 65 77 53 75 6d 20 3e 3e 20 28  nt)(iNewSum >> (
26885 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29  sizeof(i64)*8-1)
26886 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6f 76  );.        p->ov
26887 65 72 66 6c 6f 77 20 3d 20 28 28 73 31 26 73 32  erflow = ((s1&s2
26888 26 7e 73 33 29 20 7c 20 28 7e 73 31 26 7e 73 32  &~s3) | (~s1&~s2
26889 26 73 33 29 29 3f 31 3a 30 3b 0a 20 20 20 20 20  &s3))?1:0;.     
2688a 20 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65     p->iSum = iNe
2688b 77 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  wSum;.      }.  
2688c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2688d 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65  ->rSum += sqlite
2688e 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
2688f 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70  rgv[0]);.      p
26890 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20  ->approx = 1;.  
26891 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
26892 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a   void sumFinaliz
26893 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
26894 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53  t *context){.  S
26895 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  umCtx *p;.  p = 
26896 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
26897 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
26898 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26  t, 0);.  if( p &
26899 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20  & p->cnt>0 ){.  
2689a 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f    if( p->overflo
2689b 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  w ){.      sqlit
2689c 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2689d 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72  context,"integer
2689e 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a   overflow",-1);.
2689f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
268a0 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20  >approx ){.     
268a1 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
268a2 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
268a3 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65  p->rSum);.    }e
268a4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
268a5 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
268a6 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d  context, p->iSum
268a7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
268a8 74 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69  tatic void avgFi
268a9 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63  nalize(sqlite3_c
268aa 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
268ab 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20  {.  SumCtx *p;. 
268ac 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
268ad 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
268ae 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66  ontext, 0);.  if
268af 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20  ( p && p->cnt>0 
268b0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
268b1 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
268b2 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64  text, p->rSum/(d
268b3 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20  ouble)p->cnt);. 
268b4 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
268b5 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73   totalFinalize(s
268b6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
268b7 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43  context){.  SumC
268b8 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
268b9 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
268ba 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
268bb 30 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62 6c 65  0);.  /* (double
268bc 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
268bd 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
268be 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
268bf 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
268c0 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
268c1 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 28 64  p ? p->rSum : (d
268c2 6f 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ouble)0);.}../*.
268c3 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
268c4 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
268c5 20 74 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20   track of state 
268c6 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
268c7 74 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61  the.** count() a
268c8 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
268c9 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
268ca 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f  ruct CountCtx Co
268cb 75 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43  untCtx;.struct C
268cc 6f 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20  ountCtx {.  i64 
268cd 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  n;.};../*.** Rou
268ce 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65  tines to impleme
268cf 6e 74 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61  nt the count() a
268d0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
268d1 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
268d2 64 20 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69  d countStep(sqli
268d3 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
268d4 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
268d5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
268d6 61 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74  argv){.  CountCt
268d7 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
268d8 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
268d9 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
268da 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66  izeof(*p));.  if
268db 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51  ( (argc==0 || SQ
268dc 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74  LITE_NULL!=sqlit
268dd 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
268de 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a  gv[0])) && p ){.
268df 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a      p->n++;.  }.
268e0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
268e1 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
268e2 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 33    /* The sqlite3
268e3 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
268e4 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  () function is d
268e5 65 70 72 65 63 61 74 65 64 2e 20 20 42 75 74 20  eprecated.  But 
268e6 6a 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20 20 2a  just to make.  *
268e7 2a 20 73 75 72 65 20 69 74 20 73 74 69 6c 6c 20  * sure it still 
268e8 6f 70 65 72 61 74 65 73 20 63 6f 72 72 65 63 74  operates correct
268e9 6c 79 2c 20 76 65 72 69 66 79 20 74 68 61 74 20  ly, verify that 
268ea 69 74 73 20 63 6f 75 6e 74 20 61 67 72 65 65 73  its count agrees
268eb 20 77 69 74 68 20 6f 75 72 20 0a 20 20 2a 2a 20   with our .  ** 
268ec 69 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74 20 77  internal count w
268ed 68 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e 74 28  hen using count(
268ee 2a 29 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20  *) and when the 
268ef 74 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61 6e 20  total count can 
268f0 62 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 65  be.  ** expresse
268f1 64 20 61 73 20 61 20 33 32 2d 62 69 74 20 69 6e  d as a 32-bit in
268f2 74 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73 73 65  teger. */.  asse
268f3 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 70  rt( argc==1 || p
268f4 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66  ==0 || p->n>0x7f
268f5 66 66 66 66 66 66 0a 20 20 20 20 20 20 20 20 20  ffffff.         
268f6 20 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65   || p->n==sqlite
268f7 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  3_aggregate_coun
268f8 74 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a 23 65  t(context) );.#e
268f9 6e 64 69 66 0a 7d 20 20 20 0a 73 74 61 74 69 63  ndif.}   .static
268fa 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c   void countFinal
268fb 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
268fc 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
268fd 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20   CountCtx *p;.  
268fe 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
268ff 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
26900 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c  ntext, 0);.  sql
26901 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
26902 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70  4(context, p ? p
26903 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ->n : 0);.}../*.
26904 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69  ** Routines to i
26905 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20 61  mplement min() a
26906 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61  nd max() aggrega
26907 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  te functions..*/
26908 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e  .static void min
26909 6d 61 78 53 74 65 70 28 0a 20 20 73 71 6c 69 74  maxStep(.  sqlit
2690a 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2690b 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55  ext, .  int NotU
2690c 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  sed, .  sqlite3_
2690d 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2690e 20 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d 20 28    Mem *pArg  = (
2690f 4d 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b 0a 20  Mem *)argv[0];. 
26910 20 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20 20 55   Mem *pBest;.  U
26911 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
26912 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28  NotUsed);..  if(
26913 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
26914 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
26915 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
26916 72 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d  rn;.  pBest = (M
26917 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67  em *)sqlite3_agg
26918 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
26919 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a  ontext, sizeof(*
2691a 70 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21  pBest));.  if( !
2691b 70 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a  pBest ) return;.
2691c 0a 20 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c  .  if( pBest->fl
2691d 61 67 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d  ags ){.    int m
2691e 61 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b  ax;.    int cmp;
2691f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  .    CollSeq *pC
26920 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
26921 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74  FuncCollSeq(cont
26922 65 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69  ext);.    /* Thi
26923 73 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  s step function 
26924 69 73 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68  is used for both
26925 20 74 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d   the min() and m
26926 61 78 28 29 20 61 67 67 72 65 67 61 74 65 73 2c  ax() aggregates,
26927 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79  .    ** the only
26928 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
26929 65 65 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e  een the two bein
2692a 67 20 74 68 61 74 20 74 68 65 20 73 65 6e 73 65  g that the sense
2692b 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   of the.    ** c
2692c 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76  omparison is inv
2692d 65 72 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d  erted. For the m
2692e 61 78 28 29 20 61 67 67 72 65 67 61 74 65 2c 20  ax() aggregate, 
2692f 74 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  the.    ** sqlit
26930 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66  e3_user_data() f
26931 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
26932 28 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20  (void *)-1. For 
26933 6d 69 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20  min() it.    ** 
26934 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29  returns (void *)
26935 64 62 2c 20 77 68 65 72 65 20 64 62 20 69 73 20  db, where db is 
26936 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74  the sqlite3* dat
26937 61 62 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20  abase pointer.. 
26938 20 20 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20     ** Therefore 
26939 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65  the next stateme
2693a 6e 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65  nt sets variable
2693b 20 27 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20   'max' to 1 for 
2693c 74 68 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a  the max().    **
2693d 20 61 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30   aggregate, or 0
2693e 20 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20   for min()..    
2693f 2a 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c  */.    max = sql
26940 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
26941 6f 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20  ontext)!=0;.    
26942 63 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  cmp = sqlite3Mem
26943 43 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70  Compare(pBest, p
26944 41 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20  Arg, pColl);.   
26945 20 69 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70   if( (max && cmp
26946 3c 30 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20  <0) || (!max && 
26947 63 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20  cmp>0) ){.      
26948 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
26949 70 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b  py(pBest, pArg);
2694a 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
2694b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
2694c 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41  emCopy(pBest, pA
2694d 72 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  rg);.  }.}.stati
2694e 63 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e  c void minMaxFin
2694f 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
26950 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
26951 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
26952 20 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d   *pRes;.  pRes =
26953 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20   (sqlite3_value 
26954 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  *)sqlite3_aggreg
26955 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
26956 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
26957 52 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 41  Res ){.    if( A
26958 4c 57 41 59 53 28 70 52 65 73 2d 3e 66 6c 61 67  LWAYS(pRes->flag
26959 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  s) ){.      sqli
2695a 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2695b 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b  (context, pRes);
2695c 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2695d 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
2695e 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  (pRes);.  }.}../
2695f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61  *.** group_conca
26960 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54  t(EXPR, ?SEPARAT
26961 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76  OR?).*/.static v
26962 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53  oid groupConcatS
26963 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  tep(.  sqlite3_c
26964 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
26965 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
26966 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
26967 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63  rgv.){.  const c
26968 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72  har *zVal;.  Str
26969 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20  Accum *pAccum;. 
2696a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65   const char *zSe
2696b 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e  p;.  int nVal, n
2696c 53 65 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61  Sep;.  assert( a
2696d 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d  rgc==1 || argc==
2696e 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  2 );.  if( sqlit
2696f 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
26970 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
26971 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ULL ) return;.  
26972 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63  pAccum = (StrAcc
26973 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  um*)sqlite3_aggr
26974 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
26975 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
26976 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20  Accum));..  if( 
26977 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71  pAccum ){.    sq
26978 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
26979 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
2697a 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
2697b 20 20 20 20 69 6e 74 20 66 69 72 73 74 54 65 72      int firstTer
2697c 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d  m = pAccum->useM
2697d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20 70 41  alloc==0;.    pA
2697e 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  ccum->useMalloc 
2697f 3d 20 31 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d  = 1;.    pAccum-
26980 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61  >mxAlloc = db->a
26981 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
26982 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20  IT_LENGTH];.    
26983 69 66 28 20 21 66 69 72 73 74 54 65 72 6d 20 29  if( !firstTerm )
26984 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63  {.      if( argc
26985 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==2 ){.        z
26986 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  Sep = (char*)sql
26987 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
26988 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20  argv[1]);.      
26989 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 65 33    nSep = sqlite3
2698a 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
2698b 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  v[1]);.      }el
2698c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70  se{.        zSep
2698d 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20   = ",";.        
2698e 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20  nSep = 1;.      
2698f 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  }.      sqlite3S
26990 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
26991 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70  ccum, zSep, nSep
26992 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 61  );.    }.    zVa
26993 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  l = (char*)sqlit
26994 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
26995 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c  gv[0]);.    nVal
26996 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26997 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
26998 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
26999 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
2699a 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a  m, zVal, nVal);.
2699b 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
2699c 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e  d groupConcatFin
2699d 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
2699e 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
2699f 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63  .  StrAccum *pAc
269a0 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  cum;.  pAccum = 
269a1 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
269a2 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
269a3 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63  t, 0);.  if( pAc
269a4 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70  cum ){.    if( p
269a5 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b  Accum->tooBig ){
269a6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
269a7 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
269a8 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
269a9 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63 63 75   }else if( pAccu
269aa 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  m->mallocFailed 
269ab 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
269ac 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
269ad 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
269ae 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20    }else{    .   
269af 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
269b0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
269b1 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
269b2 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d  inish(pAccum), -
269b3 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1, .            
269b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
269b5 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
269b6 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
269b7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
269b8 67 69 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20  gistered all of 
269b9 74 68 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63  the above C func
269ba 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20  tions as SQL.** 
269bb 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
269bc 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f   should be the o
269bd 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
269be 68 69 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a  his file with.**
269bf 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67   external linkag
269c0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
269c1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
269c2 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e  3RegisterBuiltin
269c3 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
269c4 33 20 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20  3 *db){.#ifndef 
269c5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
269c6 52 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  RTABLE.  sqlite3
269c7 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64  AlterFunctions(d
269c8 62 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  b);.#endif.  if(
269c9 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
269ca 65 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  ed ){.    int rc
269cb 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c   = sqlite3_overl
269cc 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  oad_function(db,
269cd 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20   "MATCH", 2);.  
269ce 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
269cf 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63  LITE_NOMEM || rc
269d0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
269d1 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
269d2 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  E_NOMEM ){.     
269d3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
269d4 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
269d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
269d6 65 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67 20 6f  e LIKEOPT flag o
269d7 6e 20 74 68 65 20 32 2d 61 72 67 75 6d 65 6e 74  n the 2-argument
269d8 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
269d9 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a  he given name..*
269da 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
269db 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73 71 6c  tLikeOptFlag(sql
269dc 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
269dd 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75 38 20  char *zName, u8 
269de 66 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75 6e 63  flagVal){.  Func
269df 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 70 44 65  Def *pDef;.  pDe
269e0 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  f = sqlite3FindF
269e1 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  unction(db, zNam
269e2 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
269e3 33 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20  30(zName),.     
269e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
269e5 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54          2, SQLIT
269e6 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66  E_UTF8, 0);.  if
269e7 28 20 41 4c 57 41 59 53 28 70 44 65 66 29 20 29  ( ALWAYS(pDef) )
269e8 7b 0a 20 20 20 20 70 44 65 66 2d 3e 66 6c 61 67  {.    pDef->flag
269e9 73 20 3d 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d  s = flagVal;.  }
269ea 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
269eb 65 72 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  er the built-in 
269ec 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75  LIKE and GLOB fu
269ed 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 63 61  nctions.  The ca
269ee 73 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70  seSensitive.** p
269ef 61 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69  arameter determi
269f0 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
269f1 6f 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  ot the LIKE oper
269f2 61 74 6f 72 20 69 73 20 63 61 73 65 0a 2a 2a 20  ator is case.** 
269f3 73 65 6e 73 69 74 69 76 65 2e 20 20 47 4c 4f 42  sensitive.  GLOB
269f4 20 69 73 20 61 6c 77 61 79 73 20 63 61 73 65 20   is always case 
269f5 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a 53 51  sensitive..*/.SQ
269f6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
269f7 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
269f8 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73  rLikeFunctions(s
269f9 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
269fa 63 61 73 65 53 65 6e 73 69 74 69 76 65 29 7b 0a  caseSensitive){.
269fb 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65    struct compare
269fc 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 69  Info *pInfo;.  i
269fd 66 28 20 63 61 73 65 53 65 6e 73 69 74 69 76 65  f( caseSensitive
269fe 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20   ){.    pInfo = 
269ff 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  (struct compareI
26a00 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  nfo*)&likeInfoAl
26a01 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
26a02 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20  pInfo = (struct 
26a03 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69  compareInfo*)&li
26a04 6b 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a  keInfoNorm;.  }.
26a05 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
26a06 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20  unc(db, "like", 
26a07 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70  2, SQLITE_ANY, p
26a08 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
26a09 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
26a0a 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22  CreateFunc(db, "
26a0b 6c 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49 54 45  like", 3, SQLITE
26a0c 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b  _ANY, pInfo, lik
26a0d 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20  eFunc, 0, 0);.  
26a0e 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
26a0f 63 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c  c(db, "glob", 2,
26a10 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a 20 20   SQLITE_ANY, .  
26a11 20 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70      (struct comp
26a12 61 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e  areInfo*)&globIn
26a13 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c  fo, likeFunc, 0,
26a14 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74  0);.  setLikeOpt
26a15 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c  Flag(db, "glob",
26a16 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
26a17 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  E | SQLITE_FUNC_
26a18 43 41 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65  CASE);.  setLike
26a19 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b  OptFlag(db, "lik
26a1a 65 22 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53  e", .      caseS
26a1b 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49  ensitive ? (SQLI
26a1c 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53  TE_FUNC_LIKE | S
26a1d 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
26a1e 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c   : SQLITE_FUNC_L
26a1f 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  IKE);.}../*.** p
26a20 45 78 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  Expr points to a
26a21 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69  n expression whi
26a22 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  ch implements a 
26a23 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a  function.  If.**
26a24 20 69 74 20 69 73 20 61 70 70 72 6f 70 72 69 61   it is appropria
26a25 74 65 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20  te to apply the 
26a26 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f  LIKE optimizatio
26a27 6e 20 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69  n to that functi
26a28 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61  on.** then set a
26a29 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57  Wc[0] through aW
26a2a 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64  c[2] to the wild
26a2b 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20  card characters 
26a2c 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52  and.** return TR
26a2d 55 45 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63  UE.  If the func
26a2e 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49  tion is not a LI
26a2f 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f  KE-style functio
26a30 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e  n then.** return
26a31 20 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54   FALSE..*/.SQLIT
26a32 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
26a33 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74  lite3IsLikeFunct
26a34 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ion(sqlite3 *db,
26a35 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
26a36 74 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68  t *pIsNocase, ch
26a37 61 72 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63  ar *aWc){.  Func
26a38 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28  Def *pDef;.  if(
26a39 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46   pExpr->op!=TK_F
26a3a 55 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c 20 21  UNCTION .   || !
26a3b 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0a  pExpr->x.pList .
26a3c 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70     || pExpr->x.p
26a3d 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 0a 20  List->nExpr!=2. 
26a3e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
26a3f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
26a40 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
26a41 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
26a42 6c 65 63 74 29 20 29 3b 0a 20 20 70 44 65 66 20  lect) );.  pDef 
26a43 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
26a44 63 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2d  ction(db, pExpr-
26a45 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 20 20 20  >u.zToken, .    
26a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a47 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
26a48 53 74 72 6c 65 6e 33 30 28 70 45 78 70 72 2d 3e  Strlen30(pExpr->
26a49 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20 20 20  u.zToken),.     
26a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a4b 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54          2, SQLIT
26a4c 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66  E_UTF8, 0);.  if
26a4d 28 20 4e 45 56 45 52 28 70 44 65 66 3d 3d 30 29  ( NEVER(pDef==0)
26a4e 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 73   || (pDef->flags
26a4f 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c   & SQLITE_FUNC_L
26a50 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  IKE)==0 ){.    r
26a51 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
26a52 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20  /* The memcpy() 
26a53 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d 65  statement assume
26a54 73 20 74 68 61 74 20 74 68 65 20 77 69 6c 64 63  s that the wildc
26a55 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ard characters a
26a56 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  re.  ** the firs
26a57 74 20 74 68 72 65 65 20 73 74 61 74 65 6d 65 6e  t three statemen
26a58 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 72  ts in the compar
26a59 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  eInfo structure.
26a5a 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 72    The.  ** asser
26a5b 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ts() that follow
26a5c 20 76 65 72 69 66 79 20 74 68 61 74 20 61 73 73   verify that ass
26a5d 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d  umption.  */.  m
26a5e 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d  emcpy(aWc, pDef-
26a5f 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0a  >pUserData, 3);.
26a60 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72 2a    assert( (char*
26a61 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d  )&likeInfoAlt ==
26a62 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
26a63 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b  oAlt.matchAll );
26a64 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68  .  assert( &((ch
26a65 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
26a66 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26  )[1] == (char*)&
26a67 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63  likeInfoAlt.matc
26a68 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 74  hOne );.  assert
26a69 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65  ( &((char*)&like
26a6a 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28  InfoAlt)[2] == (
26a6b 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
26a6c 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a 20  lt.matchSet );. 
26a6d 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 70   *pIsNocase = (p
26a6e 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
26a6f 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d  ITE_FUNC_CASE)==
26a70 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  0;.  return 1;.}
26a71 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20  ../*.** All all 
26a72 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  of the FuncDef s
26a73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65  tructures in the
26a74 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20   aBuiltinFunc[] 
26a75 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20 74  array above.** t
26a76 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e  o the global fun
26a77 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62 6c 65  ction hash table
26a78 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73 20 61  .  This occurs a
26a79 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28 61 73  t start-time (as
26a7a 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e 63  .** a consequenc
26a7b 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71 6c  e of calling sql
26a7c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
26a7d 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  ))..**.** After 
26a7e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  this routine run
26a7f 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s.*/.SQLITE_PRIV
26a80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
26a81 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
26a82 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20  nctions(void){. 
26a83 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c   /*.  ** The fol
26a84 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c  lowing array hol
26a85 64 73 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ds FuncDef struc
26a86 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66  tures for all of
26a87 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20   the functions. 
26a88 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20 74   ** defined in t
26a89 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20  his file..  **. 
26a8a 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20 63 61   ** The array ca
26a8b 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e 74  nnot be constant
26a8c 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73 20 61   since changes a
26a8d 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 20  re made to the. 
26a8e 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48 61 73   ** FuncDef.pHas
26a8f 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 73 74  h elements at st
26a90 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65 20 65  art-time.  The e
26a91 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69 73 20  lements of this 
26a92 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65 20 72  array.  ** are r
26a93 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69  ead-only after i
26a94 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73  nitialization is
26a95 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a   complete..  */.
26a96 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f    static SQLITE_
26a97 57 53 44 20 46 75 6e 63 44 65 66 20 61 42 75 69  WSD FuncDef aBui
26a98 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20  ltinFunc[] = {. 
26a99 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69     FUNCTION(ltri
26a9a 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
26a9b 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e  1, 1, 0, trimFun
26a9c 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
26a9d 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c   FUNCTION(ltrim,
26a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
26a9f 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   1, 0, trimFunc 
26aa0 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
26aa1 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20  UNCTION(rtrim,  
26aa2 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 32              1, 2
26aa3 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
26aa4 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26aa5 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20  CTION(rtrim,    
26aa6 20 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20            2, 2, 
26aa7 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
26aa8 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
26aa9 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20  ION(trim,       
26aaa 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20 30 2c          1, 3, 0,
26aab 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
26aac 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26aad 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  N(trim,         
26aae 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c 20 74        2, 3, 0, t
26aaf 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
26ab0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26ab1 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  min,            
26ab2 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e     -1, 0, 1, min
26ab3 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c  maxFunc       ),
26ab4 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69  .    FUNCTION(mi
26ab5 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
26ab6 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 20    0, 0, 1, 0    
26ab7 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20              ),. 
26ab8 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 69 6e     AGGREGATE(min
26ab9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26aba 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53  1, 0, 1, minmaxS
26abb 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78  tep,      minMax
26abc 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20  Finalize ),.    
26abd 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20  FUNCTION(max,   
26abe 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
26abf 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63  1, 1, minmaxFunc
26ac0 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26ac1 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20  NCTION(max,     
26ac2 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c             0, 1,
26ac3 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20   1, 0           
26ac4 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52       ),.    AGGR
26ac5 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20 20 20  EGATE(max,      
26ac6 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 31           1, 1, 1
26ac7 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20  , minmaxStep,   
26ac8 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a     minMaxFinaliz
26ac9 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  e ),.    FUNCTIO
26aca 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20 20 20  N(typeof,       
26acb 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 74        1, 0, 0, t
26acc 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20 20 20  ypeofFunc       
26acd 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26ace 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20 20 20  length,         
26acf 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 6e      1, 0, 0, len
26ad0 67 74 68 46 75 6e 63 20 20 20 20 20 20 20 29 2c  gthFunc       ),
26ad1 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75  .    FUNCTION(su
26ad2 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  bstr,           
26ad3 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74    2, 0, 0, subst
26ad4 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  rFunc       ),. 
26ad5 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73     FUNCTION(subs
26ad6 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
26ad7 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46  3, 0, 0, substrF
26ad8 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
26ad9 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 20   FUNCTION(abs,  
26ada 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
26adb 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 20   0, 0, absFunc  
26adc 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66 6e 64          ),.#ifnd
26add 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
26ade 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
26adf 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64    FUNCTION(round
26ae0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
26ae1 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e  , 0, 0, roundFun
26ae2 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  c        ),.    
26ae3 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20  FUNCTION(round, 
26ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
26ae5 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20  0, 0, roundFunc 
26ae6 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64 69 66         ),.#endif
26ae7 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 75 70  .    FUNCTION(up
26ae8 70 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  per,            
26ae9 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70 65 72    1, 0, 0, upper
26aea 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
26aeb 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 65     FUNCTION(lowe
26aec 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
26aed 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72 46 75  1, 0, 0, lowerFu
26aee 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  nc        ),.   
26aef 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
26af0 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c  ce,           1,
26af1 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20   0, 0, 0        
26af2 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
26af3 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65  UNCTION(coalesce
26af4 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30  ,           0, 0
26af5 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20  , 0, 0          
26af6 20 20 20 20 20 20 29 2c 0a 2f 2a 20 20 46 55 4e        ),./*  FUN
26af7 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20  CTION(coalesce, 
26af8 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
26af9 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20  0, ifnullFunc   
26afa 20 20 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 2d      ), */.    {-
26afb 31 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51  1,SQLITE_UTF8,SQ
26afc 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53  LITE_FUNC_COALES
26afd 43 45 2c 30 2c 30 2c 69 66 6e 75 6c 6c 46 75 6e  CE,0,0,ifnullFun
26afe 63 2c 30 2c 30 2c 22 63 6f 61 6c 65 73 63 65 22  c,0,0,"coalesce"
26aff 2c 30 7d 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  ,0},.    FUNCTIO
26b00 4e 28 68 65 78 2c 20 20 20 20 20 20 20 20 20 20  N(hex,          
26b01 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 68        1, 0, 0, h
26b02 65 78 46 75 6e 63 20 20 20 20 20 20 20 20 20 20  exFunc          
26b03 29 2c 0a 2f 2a 20 20 46 55 4e 43 54 49 4f 4e 28  ),./*  FUNCTION(
26b04 69 66 6e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20  ifnull,         
26b05 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 69 66 6e      2, 0, 0, ifn
26b06 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c  ullFunc       ),
26b07 20 2a 2f 0a 20 20 20 20 7b 32 2c 53 51 4c 49 54   */.    {2,SQLIT
26b08 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 46 55  E_UTF8,SQLITE_FU
26b09 4e 43 5f 43 4f 41 4c 45 53 43 45 2c 30 2c 30 2c  NC_COALESCE,0,0,
26b0a 69 66 6e 75 6c 6c 46 75 6e 63 2c 30 2c 30 2c 22  ifnullFunc,0,0,"
26b0b 69 66 6e 75 6c 6c 22 2c 30 7d 2c 0a 20 20 20 20  ifnull",0},.    
26b0c 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c  FUNCTION(random,
26b0d 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
26b0e 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63  0, 0, randomFunc
26b0f 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26b10 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f  NCTION(randomblo
26b11 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c  b,         1, 0,
26b12 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20   0, randomBlob  
26b13 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26b14 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20  TION(nullif,    
26b15 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31           2, 0, 1
26b16 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20  , nullifFunc    
26b17 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26b18 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f  ON(sqlite_versio
26b19 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  n,     0, 0, 0, 
26b1a 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20  versionFunc     
26b1b 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
26b1c 28 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69  (sqlite_source_i
26b1d 64 2c 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f  d,   0, 0, 0, so
26b1e 75 72 63 65 69 64 46 75 6e 63 20 20 20 20 20 29  urceidFunc     )
26b1f 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71  ,.    FUNCTION(q
26b20 75 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  uote,           
26b21 20 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74     1, 0, 0, quot
26b22 65 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a  eFunc        ),.
26b23 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73      FUNCTION(las
26b24 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20  t_insert_rowid, 
26b25 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69   0, 0, 0, last_i
26b26 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20  nsert_rowid),.  
26b27 20 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67    FUNCTION(chang
26b28 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 30  es,            0
26b29 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20  , 0, 0, changes 
26b2a 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
26b2b 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63  FUNCTION(total_c
26b2c 68 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20  hanges,      0, 
26b2d 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e  0, 0, total_chan
26b2e 67 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55  ges    ),.    FU
26b2f 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20  NCTION(replace, 
26b30 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c             3, 0,
26b31 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20   0, replaceFunc 
26b32 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26b33 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20  TION(zeroblob,  
26b34 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
26b35 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20  , zeroblobFunc  
26b36 20 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53     ),.  #ifdef S
26b37 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20  QLITE_SOUNDEX.  
26b38 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64    FUNCTION(sound
26b39 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 31  ex,            1
26b3a 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46  , 0, 0, soundexF
26b3b 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65  unc      ),.  #e
26b3c 6e 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53  ndif.  #ifndef S
26b3d 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
26b3e 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55  EXTENSION.    FU
26b3f 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65  NCTION(load_exte
26b40 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c  nsion,     1, 0,
26b41 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20   0, loadExt     
26b42 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26b43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73  TION(load_extens
26b44 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30  ion,     2, 0, 0
26b45 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20  , loadExt       
26b46 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20     ),.  #endif. 
26b47 20 20 20 41 47 47 52 45 47 41 54 45 28 73 75 6d     AGGREGATE(sum
26b48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26b49 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70  1, 0, 0, sumStep
26b4a 2c 20 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e  ,         sumFin
26b4b 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20  alize    ),.    
26b4c 41 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c  AGGREGATE(total,
26b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26b4e 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
26b4f 20 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61         totalFina
26b50 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41  lize    ),.    A
26b51 47 47 52 45 47 41 54 45 28 61 76 67 2c 20 20 20  GGREGATE(avg,   
26b52 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26b53 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20  , 0, sumStep,   
26b54 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a        avgFinaliz
26b55 65 20 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52  e    ),. /* AGGR
26b56 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
26b57 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
26b58 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
26b59 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
26b5a 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53    ), */.    {0,S
26b5b 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54  QLITE_UTF8,SQLIT
26b5c 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30  E_FUNC_COUNT,0,0
26b5d 2c 30 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75  ,0,countStep,cou
26b5e 6e 74 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e  ntFinalize,"coun
26b5f 74 22 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45  t",0},.    AGGRE
26b60 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20  GATE(count,     
26b61 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
26b62 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20   countStep,     
26b63 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20    countFinalize 
26b64 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
26b65 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20  E(group_concat, 
26b66 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72       1, 0, 0, gr
26b67 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67  oupConcatStep, g
26b68 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69  roupConcatFinali
26b69 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41  ze),.    AGGREGA
26b6a 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
26b6b 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67        2, 0, 0, g
26b6c 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
26b6d 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
26b6e 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b  ize),.  .    LIK
26b6f 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26  EFUNC(glob, 2, &
26b70 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45  globInfo, SQLITE
26b71 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54  _FUNC_LIKE|SQLIT
26b72 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20  E_FUNC_CASE),.  
26b73 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41  #ifdef SQLITE_CA
26b74 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b  SE_SENSITIVE_LIK
26b75 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  E.    LIKEFUNC(l
26b76 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66  ike, 2, &likeInf
26b77 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e  oAlt, SQLITE_FUN
26b78 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55  C_LIKE|SQLITE_FU
26b79 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49  NC_CASE),.    LI
26b7a 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20  KEFUNC(like, 3, 
26b7b 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51  &likeInfoAlt, SQ
26b7c 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53  LITE_FUNC_LIKE|S
26b7d 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
26b7e 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49  ,.  #else.    LI
26b7f 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20  KEFUNC(like, 2, 
26b80 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53  &likeInfoNorm, S
26b81 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
26b82 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  ,.    LIKEFUNC(l
26b83 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66  ike, 3, &likeInf
26b84 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55  oNorm, SQLITE_FU
26b85 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64  NC_LIKE),.  #end
26b86 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  if.  };..  int i
26b87 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  ;.  FuncDefHash 
26b88 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
26b89 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
26b8a 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
26b8b 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66  ions);.  FuncDef
26b8c 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44   *aFunc = (FuncD
26b8d 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63  ef*)&GLOBAL(Func
26b8e 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e  Def, aBuiltinFun
26b8f 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  c);..  for(i=0; 
26b90 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69  i<ArraySize(aBui
26b91 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b  ltinFunc); i++){
26b92 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63  .    sqlite3Func
26b93 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c  DefInsert(pHash,
26b94 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d   &aFunc[i]);.  }
26b95 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  .  sqlite3Regist
26b96 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
26b97 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ons();.}../*****
26b98 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
26b99 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
26b9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26b9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26b9c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
26b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
26b9e 66 69 6c 65 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a  file fkey.c ****
26b9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26ba1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a  ********/./*.**.
26ba2 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
26ba3 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
26ba4 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
26ba5 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
26ba6 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
26ba7 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
26ba8 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
26ba9 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
26baa 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
26bab 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
26bac 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
26bad 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
26bae 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
26baf 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
26bb0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
26bb1 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
26bb2 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
26bb3 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
26bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
26bb8 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
26bb9 73 20 63 6f 64 65 20 75 73 65 64 20 62 79 20 74  s code used by t
26bba 68 65 20 63 6f 6d 70 69 6c 65 72 20 74 6f 20 61  he compiler to a
26bbb 64 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a  dd foreign key.*
26bbc 2a 20 73 75 70 70 6f 72 74 20 74 6f 20 63 6f 6d  * support to com
26bbd 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d  piled SQL statem
26bbe 65 6e 74 73 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65  ents..*/..#ifnde
26bbf 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
26bc0 52 45 49 47 4e 5f 4b 45 59 0a 23 69 66 6e 64 65  REIGN_KEY.#ifnde
26bc1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
26bc2 49 47 47 45 52 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  IGGER../*.** Def
26bc3 65 72 72 65 64 20 61 6e 64 20 49 6d 6d 65 64 69  erred and Immedi
26bc4 61 74 65 20 46 4b 73 0a 2a 2a 20 2d 2d 2d 2d 2d  ate FKs.** -----
26bc5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26bc6 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 46 6f 72 65  -----.**.** Fore
26bc7 69 67 6e 20 6b 65 79 73 20 69 6e 20 53 51 4c 69  ign keys in SQLi
26bc8 74 65 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 66  te come in two f
26bc9 6c 61 76 6f 75 72 73 3a 20 64 65 66 65 72 72 65  lavours: deferre
26bca 64 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 2e  d and immediate.
26bcb 0a 2a 2a 20 49 66 20 61 6e 20 69 6d 6d 65 64 69  .** If an immedi
26bcc 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ate foreign key 
26bcd 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69  constraint is vi
26bce 6f 6c 61 74 65 64 2c 20 53 51 4c 49 54 45 5f 43  olated, SQLITE_C
26bcf 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 69 73 20  ONSTRAINT.** is 
26bd0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
26bd1 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65   current stateme
26bd2 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72  nt transaction r
26bd3 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 61  olled back. If a
26bd4 20 0a 2a 2a 20 64 65 66 65 72 72 65 64 20 66 6f   .** deferred fo
26bd5 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26bd6 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64  aint is violated
26bd7 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 69 73 20 74  , no action is t
26bd8 61 6b 65 6e 20 0a 2a 2a 20 69 6d 6d 65 64 69 61  aken .** immedia
26bd9 74 65 6c 79 2e 20 48 6f 77 65 76 65 72 20 69 66  tely. However if
26bda 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
26bdb 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 6f 6d   attempts to com
26bdc 6d 69 74 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e  mit the .** tran
26bdd 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 66  saction before f
26bde 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 73 74 72  ixing the constr
26bdf 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20  aint violation, 
26be0 74 68 65 20 61 74 74 65 6d 70 74 20 66 61 69 6c  the attempt fail
26be1 73 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 65 72 72 65  s..**.** Deferre
26be2 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  d constraints ar
26be3 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73  e implemented us
26be4 69 6e 67 20 61 20 73 69 6d 70 6c 65 20 63 6f 75  ing a simple cou
26be5 6e 74 65 72 20 61 73 73 6f 63 69 61 74 65 64 0a  nter associated.
26be6 2a 2a 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ** with the data
26be7 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65  base handle. The
26be8 20 63 6f 75 6e 74 65 72 20 69 73 20 73 65 74 20   counter is set 
26be9 74 6f 20 7a 65 72 6f 20 65 61 63 68 20 74 69 6d  to zero each tim
26bea 65 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  e a .** database
26beb 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
26bec 6f 70 65 6e 65 64 2e 20 45 61 63 68 20 74 69 6d  opened. Each tim
26bed 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73  e a statement is
26bee 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 74 68   executed .** th
26bef 61 74 20 63 61 75 73 65 73 20 61 20 66 6f 72 65  at causes a fore
26bf0 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f  ign key violatio
26bf1 6e 2c 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69  n, the counter i
26bf2 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 45  s incremented. E
26bf3 61 63 68 0a 2a 2a 20 74 69 6d 65 20 61 20 73 74  ach.** time a st
26bf4 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75  atement is execu
26bf5 74 65 64 20 74 68 61 74 20 72 65 6d 6f 76 65 73  ted that removes
26bf6 20 61 6e 20 65 78 69 73 74 69 6e 67 20 76 69 6f   an existing vio
26bf7 6c 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74  lation from.** t
26bf8 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  he database, the
26bf9 20 63 6f 75 6e 74 65 72 20 69 73 20 64 65 63 72   counter is decr
26bfa 65 6d 65 6e 74 65 64 2e 20 57 68 65 6e 20 74 68  emented. When th
26bfb 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
26bfc 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74  .** committed, t
26bfd 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20  he commit fails 
26bfe 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  if the current v
26bff 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 75 6e  alue of the coun
26c00 74 65 72 20 69 73 0a 2a 2a 20 67 72 65 61 74 65  ter is.** greate
26c01 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 54 68 69  r than zero. Thi
26c02 73 20 73 63 68 65 6d 65 20 68 61 73 20 74 77 6f  s scheme has two
26c03 20 62 69 67 20 64 72 61 77 62 61 63 6b 73 3a 0a   big drawbacks:.
26c04 2a 2a 0a 2a 2a 20 20 20 2a 20 57 68 65 6e 20 61  **.**   * When a
26c05 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 64 75   commit fails du
26c06 65 20 74 6f 20 61 20 64 65 66 65 72 72 65 64 20  e to a deferred 
26c07 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
26c08 74 72 61 69 6e 74 2c 20 0a 2a 2a 20 20 20 20 20  traint, .**     
26c09 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  there is no way 
26c0a 74 6f 20 74 65 6c 6c 20 77 68 69 63 68 20 66 6f  to tell which fo
26c0b 72 65 69 67 6e 20 63 6f 6e 73 74 72 61 69 6e 74  reign constraint
26c0c 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66 69 65   is not satisfie
26c0d 64 2c 0a 2a 2a 20 20 20 20 20 6f 72 20 77 68 69  d,.**     or whi
26c0e 63 68 20 72 6f 77 20 69 74 20 69 73 20 6e 6f 74  ch row it is not
26c0f 20 73 61 74 69 73 66 69 65 64 20 66 6f 72 2e 0a   satisfied for..
26c10 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
26c11 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
26c12 6e 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76  ns foreign key v
26c13 69 6f 6c 61 74 69 6f 6e 73 20 77 68 65 6e 20 74  iolations when t
26c14 68 65 20 0a 2a 2a 20 20 20 20 20 74 72 61 6e 73  he .**     trans
26c15 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
26c16 2c 20 74 68 69 73 20 6d 61 79 20 63 61 75 73 65  , this may cause
26c17 20 74 68 65 20 6d 65 63 68 61 6e 69 73 6d 20 74   the mechanism t
26c18 6f 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a  o malfunction..*
26c19 2a 0a 2a 2a 20 44 65 73 70 69 74 65 20 74 68 65  *.** Despite the
26c1a 73 65 20 70 72 6f 62 6c 65 6d 73 2c 20 74 68 69  se problems, thi
26c1b 73 20 61 70 70 72 6f 61 63 68 20 69 73 20 61 64  s approach is ad
26c1c 6f 70 74 65 64 20 61 73 20 69 74 20 73 65 65 6d  opted as it seem
26c1d 73 20 73 69 6d 70 6c 65 72 0a 2a 2a 20 74 68 61  s simpler.** tha
26c1e 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76  n the alternativ
26c1f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 53 45 52 54  es..**.** INSERT
26c20 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a   operations:.**.
26c21 2a 2a 20 20 20 49 2e 31 29 20 46 6f 72 20 65 61  **   I.1) For ea
26c22 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20  ch FK for which 
26c23 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  the table is the
26c24 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20 73 65   child table, se
26c25 61 72 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 74  arch.**        t
26c26 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
26c27 66 6f 72 20 61 20 6d 61 74 63 68 2e 20 49 66 20  for a match. If 
26c28 6e 6f 6e 65 20 69 73 20 66 6f 75 6e 64 20 69 6e  none is found in
26c29 63 72 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 20  crement the.**  
26c2a 20 20 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74        constraint
26c2b 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   counter..**.** 
26c2c 20 20 49 2e 32 29 20 46 6f 72 20 65 61 63 68 20    I.2) For each 
26c2d 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  FK for which the
26c2e 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 61   table is the pa
26c2f 72 65 6e 74 20 74 61 62 6c 65 2c 20 0a 2a 2a 20  rent table, .** 
26c30 20 20 20 20 20 20 20 73 65 61 72 63 68 20 74 68         search th
26c31 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20 66 6f  e child table fo
26c32 72 20 72 6f 77 73 20 74 68 61 74 20 63 6f 72 72  r rows that corr
26c33 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 6e 65  espond to the ne
26c34 77 0a 2a 2a 20 20 20 20 20 20 20 20 72 6f 77 20  w.**        row 
26c35 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  in the parent ta
26c36 62 6c 65 2e 20 44 65 63 72 65 6d 65 6e 74 20 74  ble. Decrement t
26c37 68 65 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 65  he counter for e
26c38 61 63 68 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20  ach row.**      
26c39 20 20 66 6f 75 6e 64 20 28 61 73 20 74 68 65 20    found (as the 
26c3a 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6e 6f  constraint is no
26c3b 77 20 73 61 74 69 73 66 69 65 64 29 2e 0a 2a 2a  w satisfied)..**
26c3c 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61  .** DELETE opera
26c3d 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 44  tions:.**.**   D
26c3e 2e 31 29 20 46 6f 72 20 65 61 63 68 20 46 4b 20  .1) For each FK 
26c3f 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 74 61  for which the ta
26c40 62 6c 65 20 69 73 20 74 68 65 20 63 68 69 6c 64  ble is the child
26c41 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20 20 20 20   table, .**     
26c42 20 20 20 73 65 61 72 63 68 20 74 68 65 20 70 61     search the pa
26c43 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72 20 61  rent table for a
26c44 20 72 6f 77 20 74 68 61 74 20 63 6f 72 72 65 73   row that corres
26c45 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a  ponds to the .**
26c46 20 20 20 20 20 20 20 20 64 65 6c 65 74 65 64 20          deleted 
26c47 72 6f 77 20 69 6e 20 74 68 65 20 63 68 69 6c 64  row in the child
26c48 20 74 61 62 6c 65 2e 20 49 66 20 73 75 63 68 20   table. If such 
26c49 61 20 72 6f 77 20 69 73 20 6e 6f 74 20 66 6f 75  a row is not fou
26c4a 6e 64 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 64  nd, .**        d
26c4b 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75  ecrement the cou
26c4c 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 2e  nter..**.**   D.
26c4d 32 29 20 46 6f 72 20 65 61 63 68 20 46 4b 20 66  2) For each FK f
26c4e 6f 72 20 77 68 69 63 68 20 74 68 65 20 74 61 62  or which the tab
26c4f 6c 65 20 69 73 20 74 68 65 20 70 61 72 65 6e 74  le is the parent
26c50 20 74 61 62 6c 65 2c 20 73 65 61 72 63 68 20 0a   table, search .
26c51 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20 63 68  **        the ch
26c52 69 6c 64 20 74 61 62 6c 65 20 66 6f 72 20 72 6f  ild table for ro
26c53 77 73 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  ws that correspo
26c54 6e 64 20 74 6f 20 74 68 65 20 64 65 6c 65 74 65  nd to the delete
26c55 64 20 72 6f 77 20 0a 2a 2a 20 20 20 20 20 20 20  d row .**       
26c56 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74   in the parent t
26c57 61 62 6c 65 2e 20 46 6f 72 20 65 61 63 68 20 66  able. For each f
26c58 6f 75 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74  ound increment t
26c59 68 65 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a  he counter..**.*
26c5a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61 74 69  * UPDATE operati
26c5b 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 41 6e 20  ons:.**.**   An 
26c5c 55 50 44 41 54 45 20 63 6f 6d 6d 61 6e 64 20 72  UPDATE command r
26c5d 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c  equires that all
26c5e 20 34 20 73 74 65 70 73 20 61 62 6f 76 65 20 61   4 steps above a
26c5f 72 65 20 74 61 6b 65 6e 2c 20 62 75 74 20 6f 6e  re taken, but on
26c60 6c 79 0a 2a 2a 20 20 20 66 6f 72 20 46 4b 20 63  ly.**   for FK c
26c61 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77  onstraints for w
26c62 68 69 63 68 20 74 68 65 20 61 66 66 65 63 74 65  hich the affecte
26c63 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 61 63  d columns are ac
26c64 74 75 61 6c 6c 79 20 0a 2a 2a 20 20 20 6d 6f 64  tually .**   mod
26c65 69 66 69 65 64 20 28 76 61 6c 75 65 73 20 6d 75  ified (values mu
26c66 73 74 20 62 65 20 63 6f 6d 70 61 72 65 64 20 61  st be compared a
26c67 74 20 72 75 6e 74 69 6d 65 29 2e 0a 2a 2a 0a 2a  t runtime)..**.*
26c68 2a 20 4e 6f 74 65 20 74 68 61 74 20 49 2e 31 20  * Note that I.1 
26c69 61 6e 64 20 44 2e 31 20 61 72 65 20 76 65 72 79  and D.1 are very
26c6a 20 73 69 6d 69 6c 61 72 20 6f 70 65 72 61 74 69   similar operati
26c6b 6f 6e 73 2c 20 61 73 20 61 72 65 20 49 2e 32 20  ons, as are I.2 
26c6c 61 6e 64 20 44 2e 32 2e 0a 2a 2a 20 54 68 69 73  and D.2..** This
26c6d 20 73 69 6d 70 6c 69 66 69 65 73 20 74 68 65 20   simplifies the 
26c6e 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
26c6f 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20   bit..**.** For 
26c70 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
26c71 69 6d 6d 65 64 69 61 74 65 20 46 4b 20 63 6f 6e  immediate FK con
26c72 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20 4f 52  straints, the OR
26c73 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63   REPLACE conflic
26c74 74 0a 2a 2a 20 72 65 73 6f 6c 75 74 69 6f 6e 20  t.** resolution 
26c75 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  is considered to
26c76 20 64 65 6c 65 74 65 20 72 6f 77 73 20 62 65 66   delete rows bef
26c77 6f 72 65 20 74 68 65 20 6e 65 77 20 72 6f 77 20  ore the new row 
26c78 69 73 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20  is inserted..** 
26c79 49 66 20 61 20 64 65 6c 65 74 65 20 63 61 75 73  If a delete caus
26c7a 65 64 20 62 79 20 4f 52 20 52 45 50 4c 41 43 45  ed by OR REPLACE
26c7b 20 76 69 6f 6c 61 74 65 73 20 61 6e 20 46 4b 20   violates an FK 
26c7c 63 6f 6e 73 74 72 61 69 6e 74 2c 20 61 6e 20 65  constraint, an e
26c7d 78 63 65 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 74  xception.** is t
26c7e 68 72 6f 77 6e 2c 20 65 76 65 6e 20 69 66 20 74  hrown, even if t
26c7f 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  he FK constraint
26c80 20 77 6f 75 6c 64 20 62 65 20 73 61 74 69 73 66   would be satisf
26c81 69 65 64 20 61 66 74 65 72 20 74 68 65 20 6e 65  ied after the ne
26c82 77 20 0a 2a 2a 20 72 6f 77 20 69 73 20 69 6e 73  w .** row is ins
26c83 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d  erted..**.** Imm
26c84 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e  ediate constrain
26c85 74 73 20 61 72 65 20 75 73 75 61 6c 6c 79 20 68  ts are usually h
26c86 61 6e 64 6c 65 64 20 73 69 6d 69 6c 61 72 6c 79  andled similarly
26c87 2e 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  . The only diffe
26c88 72 65 6e 63 65 20 0a 2a 2a 20 69 73 20 74 68 61  rence .** is tha
26c89 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20 75 73  t the counter us
26c8a 65 64 20 69 73 20 73 74 6f 72 65 64 20 61 73 20  ed is stored as 
26c8b 70 61 72 74 20 6f 66 20 65 61 63 68 20 69 6e 64  part of each ind
26c8c 69 76 69 64 75 61 6c 20 73 74 61 74 65 6d 65 6e  ividual statemen
26c8d 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 28 73 74 72  t.** object (str
26c8e 75 63 74 20 56 64 62 65 29 2e 20 49 66 2c 20 61  uct Vdbe). If, a
26c8f 66 74 65 72 20 74 68 65 20 73 74 61 74 65 6d 65  fter the stateme
26c90 6e 74 20 68 61 73 20 72 75 6e 2c 20 69 74 73 20  nt has run, its 
26c91 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 63 6f 6e  immediate.** con
26c92 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20  straint counter 
26c93 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
26c94 7a 65 72 6f 2c 20 69 74 20 72 65 74 75 72 6e 73  zero, it returns
26c95 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
26c96 4e 54 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74  NT.** and the st
26c97 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
26c98 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
26c99 63 6b 2e 20 41 6e 20 65 78 63 65 70 74 69 6f 6e  ck. An exception
26c9a 20 69 73 20 61 6e 20 49 4e 53 45 52 54 0a 2a 2a   is an INSERT.**
26c9b 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
26c9c 69 6e 73 65 72 74 73 20 61 20 73 69 6e 67 6c 65  inserts a single
26c9d 20 72 6f 77 20 6f 6e 6c 79 20 28 6e 6f 20 74 72   row only (no tr
26c9e 69 67 67 65 72 73 29 2e 20 49 6e 20 74 68 69 73  iggers). In this
26c9f 20 63 61 73 65 2c 0a 2a 2a 20 69 6e 73 74 65 61   case,.** instea
26ca0 64 20 6f 66 20 75 73 69 6e 67 20 61 20 63 6f 75  d of using a cou
26ca1 6e 74 65 72 2c 20 61 6e 20 65 78 63 65 70 74 69  nter, an excepti
26ca2 6f 6e 20 69 73 20 74 68 72 6f 77 6e 20 69 6d 6d  on is thrown imm
26ca3 65 64 69 61 74 65 6c 79 20 69 66 20 74 68 65 0a  ediately if the.
26ca4 2a 2a 20 49 4e 53 45 52 54 20 76 69 6f 6c 61 74  ** INSERT violat
26ca5 65 73 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79  es a foreign key
26ca6 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68 69   constraint. Thi
26ca7 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 61  s is necessary a
26ca8 73 20 73 75 63 68 0a 2a 2a 20 61 6e 20 49 4e 53  s such.** an INS
26ca9 45 52 54 20 64 6f 65 73 20 6e 6f 74 20 6f 70 65  ERT does not ope
26caa 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  n a statement tr
26cab 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
26cac 20 54 4f 44 4f 3a 20 48 6f 77 20 73 68 6f 75 6c   TODO: How shoul
26cad 64 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61 62  d dropping a tab
26cae 6c 65 20 62 65 20 68 61 6e 64 6c 65 64 3f 20 48  le be handled? H
26caf 6f 77 20 73 68 6f 75 6c 64 20 72 65 6e 61 6d 69  ow should renami
26cb0 6e 67 20 61 20 0a 2a 2a 20 74 61 62 6c 65 20 62  ng a .** table b
26cb1 65 20 68 61 6e 64 6c 65 64 3f 0a 2a 2a 0a 2a 2a  e handled?.**.**
26cb2 0a 2a 2a 20 51 75 65 72 79 20 41 50 49 20 4e 6f  .** Query API No
26cb3 74 65 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  tes.** ---------
26cb4 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 42 65 66  ------.**.** Bef
26cb5 6f 72 65 20 63 6f 64 69 6e 67 20 61 6e 20 55 50  ore coding an UP
26cb6 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 72  DATE or DELETE r
26cb7 6f 77 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  ow operation, th
26cb8 65 20 63 6f 64 65 2d 67 65 6e 65 72 61 74 6f 72  e code-generator
26cb9 0a 2a 2a 20 66 6f 72 20 74 68 6f 73 65 20 74 77  .** for those tw
26cba 6f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6e 65 65  o operations nee
26cbb 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68  ds to know wheth
26cbc 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70  er or not the op
26cbd 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 69  eration.** requi
26cbe 72 65 73 20 61 6e 79 20 46 4b 20 70 72 6f 63 65  res any FK proce
26cbf 73 73 69 6e 67 20 61 6e 64 2c 20 69 66 20 73 6f  ssing and, if so
26cc0 2c 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20  , which columns 
26cc1 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a  of the original.
26cc2 2a 2a 20 72 6f 77 20 61 72 65 20 72 65 71 75 69  ** row are requi
26cc3 72 65 64 20 62 79 20 74 68 65 20 46 4b 20 70 72  red by the FK pr
26cc4 6f 63 65 73 73 69 6e 67 20 56 44 42 45 20 63 6f  ocessing VDBE co
26cc5 64 65 20 28 69 2e 65 2e 20 69 66 20 46 4b 73 20  de (i.e. if FKs 
26cc6 77 65 72 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  were.** implemen
26cc7 74 65 64 20 75 73 69 6e 67 20 74 72 69 67 67 65  ted using trigge
26cc8 72 73 2c 20 77 68 69 63 68 20 6f 66 20 74 68 65  rs, which of the
26cc9 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 77   old.* columns w
26cca 6f 75 6c 64 20 62 65 20 0a 2a 2a 20 61 63 63 65  ould be .** acce
26ccb 73 73 65 64 29 2e 20 4e 6f 20 69 6e 66 6f 72 6d  ssed). No inform
26ccc 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ation is require
26ccd 64 20 62 79 20 74 68 65 20 63 6f 64 65 2d 67 65  d by the code-ge
26cce 6e 65 72 61 74 6f 72 20 62 65 66 6f 72 65 0a 2a  nerator before.*
26ccf 2a 20 63 6f 64 69 6e 67 20 61 6e 20 49 4e 53 45  * coding an INSE
26cd0 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68  RT operation. Th
26cd1 65 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  e functions used
26cd2 20 62 79 20 74 68 65 20 55 50 44 41 54 45 2f 44   by the UPDATE/D
26cd3 45 4c 45 54 45 0a 2a 2a 20 67 65 6e 65 72 61 74  ELETE.** generat
26cd4 69 6f 6e 20 63 6f 64 65 20 74 6f 20 71 75 65 72  ion code to quer
26cd5 79 20 66 6f 72 20 74 68 69 73 20 69 6e 66 6f 72  y for this infor
26cd6 6d 61 74 69 6f 6e 20 61 72 65 3a 0a 2a 2a 0a 2a  mation are:.**.*
26cd7 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 52 65 71  *   sqlite3FkReq
26cd8 75 69 72 65 64 28 29 20 2d 20 54 65 73 74 20 74  uired() - Test t
26cd9 6f 20 73 65 65 20 69 66 20 46 4b 20 70 72 6f 63  o see if FK proc
26cda 65 73 73 69 6e 67 20 69 73 20 72 65 71 75 69 72  essing is requir
26cdb 65 64 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  ed..**   sqlite3
26cdc 46 6b 4f 6c 64 6d 61 73 6b 28 29 20 20 2d 20 51  FkOldmask()  - Q
26cdd 75 65 72 79 20 66 6f 72 20 74 68 65 20 73 65 74  uery for the set
26cde 20 6f 66 20 72 65 71 75 69 72 65 64 20 6f 6c 64   of required old
26cdf 2e 2a 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a  .* columns..**.*
26ce0 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 6c 79 20  *.** Externally 
26ce1 61 63 63 65 73 73 69 62 6c 65 20 6d 6f 64 75 6c  accessible modul
26ce2 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 2d  e functions.** -
26ce3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ce4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ce5 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 20 73 71  -----.**.**   sq
26ce6 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 29 20 20  lite3FkCheck()  
26ce7 20 20 2d 20 43 68 65 63 6b 20 66 6f 72 20 66 6f    - Check for fo
26ce8 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74  reign key violat
26ce9 69 6f 6e 73 2e 0a 2a 2a 20 20 20 73 71 6c 69 74  ions..**   sqlit
26cea 65 33 46 6b 41 63 74 69 6f 6e 73 28 29 20 20 2d  e3FkActions()  -
26ceb 20 43 6f 64 65 20 74 72 69 67 67 65 72 73 20 66   Code triggers f
26cec 6f 72 20 4f 4e 20 55 50 44 41 54 45 2f 4f 4e 20  or ON UPDATE/ON 
26ced 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 73 2e 0a  DELETE actions..
26cee 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 44 65  **   sqlite3FkDe
26cef 6c 65 74 65 28 29 20 20 20 2d 20 44 65 6c 65 74  lete()   - Delet
26cf0 65 20 61 6e 20 46 4b 65 79 20 73 74 72 75 63 74  e an FKey struct
26cf1 75 72 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56  ure..*/../*.** V
26cf2 44 42 45 20 43 61 6c 6c 69 6e 67 20 43 6f 6e 76  DBE Calling Conv
26cf3 65 6e 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d  ention.** ------
26cf4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26cf5 2d 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a  -.**.** Example:
26cf6 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 74 68 65  .**.**   For the
26cf7 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 4e 53 45 52   following INSER
26cf8 54 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a  T statement:.**.
26cf9 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41  **     CREATE TA
26cfa 42 4c 45 20 74 31 28 61 2c 20 62 20 49 4e 54 45  BLE t1(a, b INTE
26cfb 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
26cfc 20 63 29 3b 0a 2a 2a 20 20 20 20 20 49 4e 53 45   c);.**     INSE
26cfd 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
26cfe 53 28 31 2c 20 32 2c 20 33 2e 31 29 3b 0a 2a 2a  S(1, 2, 3.1);.**
26cff 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28  .**   Register (
26d00 78 29 3a 20 20 20 20 20 20 20 20 32 20 20 20 20  x):        2    
26d01 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0a 2a  (type integer).*
26d02 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78 2b  *   Register (x+
26d03 31 29 3a 20 20 20 20 20 20 31 20 20 20 20 28 74  1):      1    (t
26d04 79 70 65 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20  ype integer).** 
26d05 20 20 52 65 67 69 73 74 65 72 20 28 78 2b 32 29    Register (x+2)
26d06 3a 20 20 20 20 20 20 4e 55 4c 4c 20 28 74 79 70  :      NULL (typ
26d07 65 20 4e 55 4c 4c 29 0a 2a 2a 20 20 20 52 65 67  e NULL).**   Reg
26d08 69 73 74 65 72 20 28 78 2b 33 29 3a 20 20 20 20  ister (x+3):    
26d09 20 20 33 2e 31 20 20 28 74 79 70 65 20 72 65 61    3.1  (type rea
26d0a 6c 29 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 66  l).*/../*.** A f
26d0b 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
26d0c 72 61 69 6e 74 20 72 65 71 75 69 72 65 73 20 74  raint requires t
26d0d 68 61 74 20 74 68 65 20 6b 65 79 20 63 6f 6c 75  hat the key colu
26d0e 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e  mns in the paren
26d0f 74 0a 2a 2a 20 74 61 62 6c 65 20 61 72 65 20 63  t.** table are c
26d10 6f 6c 6c 65 63 74 69 76 65 6c 79 20 73 75 62 6a  ollectively subj
26d11 65 63 74 20 74 6f 20 61 20 55 4e 49 51 55 45 20  ect to a UNIQUE 
26d12 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  or PRIMARY KEY c
26d13 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2a 20 47 69  onstraint..** Gi
26d14 76 65 6e 20 74 68 61 74 20 70 50 61 72 65 6e 74  ven that pParent
26d15 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
26d16 61 62 6c 65 20 66 6f 72 20 66 6f 72 65 69 67 6e  able for foreign
26d17 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
26d18 70 46 4b 65 79 2c 20 0a 2a 2a 20 73 65 61 72 63  pFKey, .** searc
26d19 68 20 74 68 65 20 73 63 68 65 6d 61 20 61 20 75  h the schema a u
26d1a 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 74  nique index on t
26d1b 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  he parent key co
26d1c 6c 75 6d 6e 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  lumns. .**.** If
26d1d 20 73 75 63 63 65 73 73 66 75 6c 2c 20 7a 65 72   successful, zer
26d1e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  o is returned. I
26d1f 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  f the parent key
26d20 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50   is an INTEGER P
26d21 52 49 4d 41 52 59 20 0a 2a 2a 20 4b 45 59 20 63  RIMARY .** KEY c
26d22 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 6f 75 74 70  olumn, then outp
26d23 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70 70 49  ut variable *ppI
26d24 64 78 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  dx is set to NUL
26d25 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70  L. Otherwise, *p
26d26 70 49 64 78 20 0a 2a 2a 20 69 73 20 73 65 74 20  pIdx .** is set 
26d27 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
26d28 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 0a 2a  unique index. .*
26d29 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72  * .** If the par
26d2a 65 6e 74 20 6b 65 79 20 63 6f 6e 73 69 73 74 73  ent key consists
26d2b 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c   of a single col
26d2c 75 6d 6e 20 28 74 68 65 20 66 6f 72 65 69 67 6e  umn (the foreign
26d2d 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a   key constraint.
26d2e 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70  ** is not a comp
26d2f 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  osite foreign ke
26d30 79 29 2c 20 6f 75 74 70 75 74 20 76 61 72 69 61  y), output varia
26d31 62 6c 65 20 2a 70 61 69 43 6f 6c 20 69 73 20 73  ble *paiCol is s
26d32 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 4f  et to NULL..** O
26d33 74 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 20  therwise, it is 
26d34 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
26d35 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 72 72  an allocated arr
26d36 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68  ay of size N, wh
26d37 65 72 65 0a 2a 2a 20 4e 20 69 73 20 74 68 65 20  ere.** N is the 
26d38 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
26d39 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  s in the parent 
26d3a 6b 65 79 2e 20 54 68 65 20 66 69 72 73 74 20 65  key. The first e
26d3b 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a  lement of the.**
26d3c 20 61 72 72 61 79 20 69 73 20 74 68 65 20 69 6e   array is the in
26d3d 64 65 78 20 6f 66 20 74 68 65 20 63 68 69 6c 64  dex of the child
26d3e 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68   table column th
26d3f 61 74 20 69 73 20 6d 61 70 70 65 64 20 62 79 20  at is mapped by 
26d40 74 68 65 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72  the FK.** constr
26d41 61 69 6e 74 20 74 6f 20 74 68 65 20 70 61 72 65  aint to the pare
26d42 6e 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  nt table column 
26d43 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6c 65  stored in the le
26d44 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 0a 2a  ft-most column.*
26d45 2a 20 6f 66 20 69 6e 64 65 78 20 2a 70 70 49 64  * of index *ppId
26d46 78 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 65 6c  x. The second el
26d47 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72  ement of the arr
26d48 61 79 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  ay is the index 
26d49 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c 64 20  of the.** child 
26d4a 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
26d4b 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
26d4c 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65 66 74   the second left
26d4d 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 0a  -most column of.
26d4e 2a 2a 20 2a 70 70 49 64 78 2c 20 61 6e 64 20 73  ** *ppIdx, and s
26d4f 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  o on..**.** If t
26d50 68 65 20 72 65 71 75 69 72 65 64 20 69 6e 64 65  he required inde
26d51 78 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  x cannot be foun
26d52 64 2c 20 65 69 74 68 65 72 20 62 65 63 61 75 73  d, either becaus
26d53 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68  e:.**.**   1) Th
26d54 65 20 6e 61 6d 65 64 20 70 61 72 65 6e 74 20 6b  e named parent k
26d55 65 79 20 63 6f 6c 75 6d 6e 73 20 64 6f 20 6e 6f  ey columns do no
26d56 74 20 65 78 69 73 74 2c 20 6f 72 0a 2a 2a 0a 2a  t exist, or.**.*
26d57 2a 20 20 20 32 29 20 54 68 65 20 6e 61 6d 65 64  *   2) The named
26d58 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
26d59 6d 6e 73 20 64 6f 20 65 78 69 73 74 2c 20 62 75  mns do exist, bu
26d5a 74 20 61 72 65 20 6e 6f 74 20 73 75 62 6a 65 63  t are not subjec
26d5b 74 20 74 6f 20 61 0a 2a 2a 20 20 20 20 20 20 55  t to a.**      U
26d5c 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59  NIQUE or PRIMARY
26d5d 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 2c   KEY constraint,
26d5e 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 4e   or.**.**   3) N
26d5f 6f 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  o parent key col
26d60 75 6d 6e 73 20 77 65 72 65 20 70 72 6f 76 69 64  umns were provid
26d61 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 61 73  ed explicitly as
26d62 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20   part of the.** 
26d63 20 20 20 20 20 66 6f 72 65 69 67 6e 20 6b 65 79       foreign key
26d64 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 61 6e 64   definition, and
26d65 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26d66 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  e does not have 
26d67 61 0a 2a 2a 20 20 20 20 20 20 50 52 49 4d 41 52  a.**      PRIMAR
26d68 59 20 4b 45 59 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  Y KEY, or.**.** 
26d69 20 20 34 29 20 4e 6f 20 70 61 72 65 6e 74 20 6b    4) No parent k
26d6a 65 79 20 63 6f 6c 75 6d 6e 73 20 77 65 72 65 20  ey columns were 
26d6b 70 72 6f 76 69 64 65 64 20 65 78 70 6c 69 63 69  provided explici
26d6c 74 6c 79 20 61 73 20 70 61 72 74 20 6f 66 20 74  tly as part of t
26d6d 68 65 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 69  he.**      forei
26d6e 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
26d6f 6e 2c 20 61 6e 64 20 74 68 65 20 50 52 49 4d 41  n, and the PRIMA
26d70 52 59 20 4b 45 59 20 6f 66 20 74 68 65 20 70 61  RY KEY of the pa
26d71 72 65 6e 74 20 74 61 62 6c 65 20 0a 2a 2a 20 20  rent table .**  
26d72 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f 66 20      consists of 
26d73 61 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75  a a different nu
26d74 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
26d75 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79  to the child key
26d76 20 69 6e 20 0a 2a 2a 20 20 20 20 20 20 74 68 65   in .**      the
26d77 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 0a 2a 2a   child table..**
26d78 0a 2a 2a 20 74 68 65 6e 20 6e 6f 6e 2d 7a 65 72  .** then non-zer
26d79 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 61  o is returned, a
26d7a 6e 64 20 61 20 22 66 6f 72 65 69 67 6e 20 6b 65  nd a "foreign ke
26d7b 79 20 6d 69 73 6d 61 74 63 68 22 20 65 72 72 6f  y mismatch" erro
26d7c 72 20 6c 6f 61 64 65 64 0a 2a 2a 20 69 6e 74 6f  r loaded.** into
26d7d 20 70 50 61 72 73 65 2e 20 49 66 20 61 6e 20 4f   pParse. If an O
26d7e 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  OM error occurs,
26d7f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74   non-zero is ret
26d80 75 72 6e 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a  urned and the.**
26d81 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
26d82 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
26d83 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  s set..*/.static
26d84 20 69 6e 74 20 6c 6f 63 61 74 65 46 6b 65 79 49   int locateFkeyI
26d85 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
26d86 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
26d87 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
26d88 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72   context to stor
26d89 65 20 61 6e 79 20 65 72 72 6f 72 20 69 6e 20 2a  e any error in *
26d8a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 50 61 72 65  /.  Table *pPare
26d8b 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nt,             
26d8c 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 74 61      /* Parent ta
26d8d 62 6c 65 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72  ble of FK constr
26d8e 61 69 6e 74 20 70 46 4b 65 79 20 2a 2f 0a 20 20  aint pFKey */.  
26d8f 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20  FKey *pFKey,    
26d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d91 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 74  /* Foreign key t
26d92 6f 20 66 69 6e 64 20 69 6e 64 65 78 20 66 6f 72  o find index for
26d93 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a 70 70   */.  Index **pp
26d94 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
26d95 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 55 6e        /* OUT: Un
26d96 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 70 61  ique index on pa
26d97 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20  rent table */.  
26d98 69 6e 74 20 2a 2a 70 61 69 43 6f 6c 20 20 20 20  int **paiCol    
26d99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d9a 2f 2a 20 4f 55 54 3a 20 4d 61 70 20 6f 66 20 69  /* OUT: Map of i
26d9b 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  ndex columns in 
26d9c 70 46 4b 65 79 20 2a 2f 0a 29 7b 0a 20 20 49 6e  pFKey */.){.  In
26d9d 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 20 20  dex *pIdx = 0;  
26d9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d9f 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
26da0 74 75 72 6e 20 76 69 61 20 2a 70 70 49 64 78 20  turn via *ppIdx 
26da1 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20  */.  int *aiCol 
26da2 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
26da3 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
26da4 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20  e to return via 
26da5 2a 70 61 69 43 6f 6c 20 2a 2f 0a 20 20 69 6e 74  *paiCol */.  int
26da6 20 6e 43 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 6e   nCol = pFKey->n
26da7 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
26da8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
26da9 6c 75 6d 6e 73 20 69 6e 20 70 61 72 65 6e 74 20  lumns in parent 
26daa 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  key */.  char *z
26dab 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  Key = pFKey->aCo
26dac 6c 5b 30 5d 2e 7a 43 6f 6c 3b 20 20 20 2f 2a 20  l[0].zCol;   /* 
26dad 4e 61 6d 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73  Name of left-mos
26dae 74 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  t parent key col
26daf 75 6d 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  umn */..  /* The
26db0 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f   caller is respo
26db1 6e 73 69 62 6c 65 20 66 6f 72 20 7a 65 72 6f 69  nsible for zeroi
26db2 6e 67 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65  ng output parame
26db3 74 65 72 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ters. */.  asser
26db4 74 28 20 70 70 49 64 78 20 26 26 20 2a 70 70 49  t( ppIdx && *ppI
26db5 64 78 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  dx==0 );.  asser
26db6 74 28 20 21 70 61 69 43 6f 6c 20 7c 7c 20 2a 70  t( !paiCol || *p
26db7 61 69 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 61 73  aiCol==0 );.  as
26db8 73 65 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a  sert( pParse );.
26db9 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
26dba 20 61 20 6e 6f 6e 2d 63 6f 6d 70 6f 73 69 74 65   a non-composite
26dbb 20 28 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 29   (single column)
26dbc 20 66 6f 72 65 69 67 6e 20 6b 65 79 2c 20 63 68   foreign key, ch
26dbd 65 63 6b 20 69 66 20 69 74 20 0a 20 20 2a 2a 20  eck if it .  ** 
26dbe 6d 61 70 73 20 74 6f 20 74 68 65 20 49 4e 54 45  maps to the INTE
26dbf 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
26dc0 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74  of table pParent
26dc1 2e 20 49 66 20 73 6f 2c 20 6c 65 61 76 65 20 2a  . If so, leave *
26dc2 70 70 49 64 78 20 0a 20 20 2a 2a 20 61 6e 64 20  ppIdx .  ** and 
26dc3 2a 70 61 69 43 6f 6c 20 73 65 74 20 74 6f 20 7a  *paiCol set to z
26dc4 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 65  ero and return e
26dc5 61 72 6c 79 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  arly. .  **.  **
26dc6 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f 72 20   Otherwise, for 
26dc7 61 20 63 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65  a composite fore
26dc8 69 67 6e 20 6b 65 79 20 28 6d 6f 72 65 20 74 68  ign key (more th
26dc9 61 6e 20 6f 6e 65 20 63 6f 6c 75 6d 6e 29 2c 20  an one column), 
26dca 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 73 70  allocate.  ** sp
26dcb 61 63 65 20 66 6f 72 20 74 68 65 20 61 69 43 6f  ace for the aiCo
26dcc 6c 20 61 72 72 61 79 20 28 72 65 74 75 72 6e 65  l array (returne
26dcd 64 20 76 69 61 20 6f 75 74 70 75 74 20 70 61 72  d via output par
26dce 61 6d 65 74 65 72 20 2a 70 61 69 43 6f 6c 29 2e  ameter *paiCol).
26dcf 0a 20 20 2a 2a 20 4e 6f 6e 2d 63 6f 6d 70 6f 73  .  ** Non-compos
26dd0 69 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  ite foreign keys
26dd1 20 64 6f 20 6e 6f 74 20 72 65 71 75 69 72 65 20   do not require 
26dd2 74 68 65 20 61 69 43 6f 6c 20 61 72 72 61 79 2e  the aiCol array.
26dd3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f 6c  .  */.  if( nCol
26dd4 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==1 ){.    /* Th
26dd5 65 20 46 4b 20 6d 61 70 73 20 74 6f 20 74 68 65  e FK maps to the
26dd6 20 49 50 4b 20 69 66 20 61 6e 79 20 6f 66 20 74   IPK if any of t
26dd7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
26dd8 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a 20 20   true:.    **.  
26dd9 20 20 2a 2a 20 20 20 31 29 20 54 68 65 72 65 20    **   1) There 
26dda 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  is an INTEGER PR
26ddb 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
26ddc 20 61 6e 64 20 74 68 65 20 46 4b 20 69 73 20 69   and the FK is i
26ddd 6d 70 6c 69 63 69 74 6c 79 20 0a 20 20 20 20 2a  mplicitly .    *
26dde 2a 20 20 20 20 20 20 6d 61 70 70 65 64 20 74 6f  *      mapped to
26ddf 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
26de0 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e   of table pParen
26de1 74 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 20 20 32  t, or.    **   2
26de2 29 20 54 68 65 20 46 4b 20 69 73 20 65 78 70 6c  ) The FK is expl
26de3 69 63 69 74 6c 79 20 6d 61 70 70 65 64 20 74 6f  icitly mapped to
26de4 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72   a column declar
26de5 65 64 20 61 73 20 49 4e 54 45 47 45 52 0a 20 20  ed as INTEGER.  
26de6 20 20 2a 2a 20 20 20 20 20 20 50 52 49 4d 41 52    **      PRIMAR
26de7 59 20 4b 45 59 2e 0a 20 20 20 20 2a 2f 0a 20 20  Y KEY..    */.  
26de8 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 69    if( pParent->i
26de9 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 20  PKey>=0 ){.     
26dea 20 69 66 28 20 21 7a 4b 65 79 20 29 20 72 65 74   if( !zKey ) ret
26deb 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 69 66 28  urn 0;.      if(
26dec 20 21 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70   !sqlite3StrICmp
26ded 28 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 70  (pParent->aCol[p
26dee 50 61 72 65 6e 74 2d 3e 69 50 4b 65 79 5d 2e 7a  Parent->iPKey].z
26def 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 29 20 72 65  Name, zKey) ) re
26df0 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
26df1 7d 65 6c 73 65 20 69 66 28 20 70 61 69 43 6f 6c  }else if( paiCol
26df2 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
26df3 6e 43 6f 6c 3e 31 20 29 3b 0a 20 20 20 20 61 69  nCol>1 );.    ai
26df4 43 6f 6c 20 3d 20 28 69 6e 74 20 2a 29 73 71 6c  Col = (int *)sql
26df5 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
26df6 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e 43 6f 6c  pParse->db, nCol
26df7 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 20  *sizeof(int));. 
26df8 20 20 20 69 66 28 20 21 61 69 43 6f 6c 20 29 20     if( !aiCol ) 
26df9 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 2a 70  return 1;.    *p
26dfa 61 69 43 6f 6c 20 3d 20 61 69 43 6f 6c 3b 0a 20  aiCol = aiCol;. 
26dfb 20 7d 0a 0a 20 20 66 6f 72 28 70 49 64 78 3d 70   }..  for(pIdx=p
26dfc 50 61 72 65 6e 74 2d 3e 70 49 6e 64 65 78 3b 20  Parent->pIndex; 
26dfd 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
26dfe 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
26dff 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d   pIdx->nColumn==
26e00 6e 43 6f 6c 20 26 26 20 70 49 64 78 2d 3e 6f 6e  nCol && pIdx->on
26e01 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
26e02 7b 20 0a 20 20 20 20 20 20 2f 2a 20 70 49 64 78  { .      /* pIdx
26e03 20 69 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64   is a UNIQUE ind
26e04 65 78 20 28 6f 72 20 61 20 50 52 49 4d 41 52 59  ex (or a PRIMARY
26e05 20 4b 45 59 29 20 61 6e 64 20 68 61 73 20 74 68   KEY) and has th
26e06 65 20 72 69 67 68 74 20 6e 75 6d 62 65 72 0a 20  e right number. 
26e07 20 20 20 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d       ** of colum
26e08 6e 73 2e 20 49 66 20 65 61 63 68 20 69 6e 64 65  ns. If each inde
26e09 78 65 64 20 63 6f 6c 75 6d 6e 20 63 6f 72 72 65  xed column corre
26e0a 73 70 6f 6e 64 73 20 74 6f 20 61 20 66 6f 72 65  sponds to a fore
26e0b 69 67 6e 20 6b 65 79 0a 20 20 20 20 20 20 2a 2a  ign key.      **
26e0c 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 46 4b 65 79   column of pFKey
26e0d 2c 20 74 68 65 6e 20 74 68 69 73 20 69 6e 64 65  , then this inde
26e0e 78 20 69 73 20 61 20 77 69 6e 6e 65 72 2e 20 20  x is a winner.  
26e0f 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 4b  */..      if( zK
26e10 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ey==0 ){.       
26e11 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73 20 4e   /* If zKey is N
26e12 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 66  ULL, then this f
26e13 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 69 6d  oreign key is im
26e14 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65 64 20  plicitly mapped 
26e15 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  to .        ** t
26e16 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  he PRIMARY KEY o
26e17 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2e  f table pParent.
26e18 20 54 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   The PRIMARY KEY
26e19 20 69 6e 64 65 78 20 6d 61 79 20 62 65 20 0a 20   index may be . 
26e1a 20 20 20 20 20 20 20 2a 2a 20 69 64 65 6e 74 69         ** identi
26e1b 66 69 65 64 20 62 79 20 74 68 65 20 74 65 73 74  fied by the test
26e1c 20 28 49 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65   (Index.autoInde
26e1d 78 3d 3d 32 29 2e 20 20 2a 2f 0a 20 20 20 20 20  x==2).  */.     
26e1e 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61 75 74     if( pIdx->aut
26e1f 6f 49 6e 64 65 78 3d 3d 32 20 29 7b 0a 20 20 20  oIndex==2 ){.   
26e20 20 20 20 20 20 20 20 69 66 28 20 61 69 43 6f 6c         if( aiCol
26e21 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
26e22 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20  int i;.         
26e23 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26e24 6f 6c 3b 20 69 2b 2b 29 20 61 69 43 6f 6c 5b 69  ol; i++) aiCol[i
26e25 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b  ] = pFKey->aCol[
26e26 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20  i].iFrom;.      
26e27 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
26e28 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
26e29 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
26e2a 20 20 20 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79        /* If zKey
26e2b 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68   is non-NULL, th
26e2c 65 6e 20 74 68 69 73 20 66 6f 72 65 69 67 6e 20  en this foreign 
26e2d 6b 65 79 20 77 61 73 20 64 65 63 6c 61 72 65 64  key was declared
26e2e 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d   to.        ** m
26e2f 61 70 20 74 6f 20 61 6e 20 65 78 70 6c 69 63 69  ap to an explici
26e30 74 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  t list of column
26e31 73 20 69 6e 20 74 61 62 6c 65 20 70 50 61 72 65  s in table pPare
26e32 6e 74 2e 20 43 68 65 63 6b 20 69 66 20 74 68 69  nt. Check if thi
26e33 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64  s.        ** ind
26e34 65 78 20 6d 61 74 63 68 65 73 20 74 68 6f 73 65  ex matches those
26e35 20 63 6f 6c 75 6d 6e 73 2e 20 41 6c 73 6f 2c 20   columns. Also, 
26e36 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 69  check that the i
26e37 6e 64 65 78 20 75 73 65 73 0a 20 20 20 20 20 20  ndex uses.      
26e38 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74    ** the default
26e39 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
26e3a 6e 63 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f  nces for each co
26e3b 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20  lumn. */.       
26e3c 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20   int i, j;.     
26e3d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26e3e 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
26e3f 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70      int iCol = p
26e40 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
26e41 3b 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f  ;     /* Index o
26e42 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 61 72 65  f column in pare
26e43 6e 74 20 74 62 6c 20 2a 2f 0a 20 20 20 20 20 20  nt tbl */.      
26e44 20 20 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 43      char *zDfltC
26e45 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  oll;            
26e46 20 20 20 20 20 20 2f 2a 20 44 65 66 2e 20 63 6f        /* Def. co
26e47 6c 6c 61 74 69 6f 6e 20 66 6f 72 20 63 6f 6c 75  llation for colu
26e48 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  mn */.          
26e49 63 68 61 72 20 2a 7a 49 64 78 43 6f 6c 3b 20 20  char *zIdxCol;  
26e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e4b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 69 6e 64    /* Name of ind
26e4c 65 78 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a  exed column */..
26e4d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
26e4e 74 68 65 20 69 6e 64 65 78 20 75 73 65 73 20 61  the index uses a
26e4f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
26e50 6e 63 65 20 74 68 61 74 20 69 73 20 64 69 66 66  nce that is diff
26e51 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 20 20 20  erent from.     
26e52 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61       ** the defa
26e53 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ult collation se
26e54 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 63  quence for the c
26e55 6f 6c 75 6d 6e 2c 20 74 68 69 73 20 69 6e 64 65  olumn, this inde
26e56 78 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  x is.          *
26e57 2a 20 75 6e 75 73 61 62 6c 65 2e 20 42 61 69 6c  * unusable. Bail
26e58 20 6f 75 74 20 65 61 72 6c 79 20 69 6e 20 74 68   out early in th
26e59 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20  is case.  */.   
26e5a 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f 6c 6c         zDfltColl
26e5b 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c   = pParent->aCol
26e5c 5b 69 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  [iCol].zColl;.  
26e5d 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 44 66          if( !zDf
26e5e 6c 74 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  ltColl ){.      
26e5f 20 20 20 20 20 20 7a 44 66 6c 74 43 6f 6c 6c 20        zDfltColl 
26e60 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 20 20  = "BINARY";.    
26e61 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26e62 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
26e63 49 43 6d 70 28 70 49 64 78 2d 3e 61 7a 43 6f 6c  ICmp(pIdx->azCol
26e64 6c 5b 69 5d 2c 20 7a 44 66 6c 74 43 6f 6c 6c 29  l[i], zDfltColl)
26e65 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20   ) break;..     
26e66 20 20 20 20 20 7a 49 64 78 43 6f 6c 20 3d 20 70       zIdxCol = p
26e67 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43 6f  Parent->aCol[iCo
26e68 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  l].zName;.      
26e69 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
26e6a 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
26e6b 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
26e6c 65 33 53 74 72 49 43 6d 70 28 70 46 4b 65 79 2d  e3StrICmp(pFKey-
26e6d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c 20 7a  >aCol[j].zCol, z
26e6e 49 64 78 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20  IdxCol)==0 ){.  
26e6f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
26e70 61 69 43 6f 6c 20 29 20 61 69 43 6f 6c 5b 69 5d  aiCol ) aiCol[i]
26e71 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a   = pFKey->aCol[j
26e72 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20  ].iFrom;.       
26e73 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
26e74 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26e75 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26e76 20 20 69 66 28 20 6a 3d 3d 6e 43 6f 6c 20 29 20    if( j==nCol ) 
26e77 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
26e78 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3d 3d  .        if( i==
26e79 6e 43 6f 6c 20 29 20 62 72 65 61 6b 3b 20 20 20  nCol ) break;   
26e7a 20 20 20 2f 2a 20 70 49 64 78 20 69 73 20 75 73     /* pIdx is us
26e7b 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  able */.      }.
26e7c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
26e7d 20 21 70 49 64 78 20 29 7b 0a 20 20 20 20 69 66   !pIdx ){.    if
26e7e 28 20 21 70 50 61 72 73 65 2d 3e 64 69 73 61 62  ( !pParse->disab
26e7f 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20  leTriggers ){.  
26e80 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
26e81 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72  Msg(pParse, "for
26e82 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
26e83 68 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  h");.    }.    s
26e84 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61  qlite3DbFree(pPa
26e85 72 73 65 2d 3e 64 62 2c 20 61 69 43 6f 6c 29 3b  rse->db, aiCol);
26e86 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
26e87 20 7d 0a 0a 20 20 2a 70 70 49 64 78 20 3d 20 70   }..  *ppIdx = p
26e88 49 64 78 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  Idx;.  return 0;
26e89 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
26e8a 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
26e8b 64 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20  d when a row is 
26e8c 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72  inserted into or
26e8d 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68   deleted from th
26e8e 65 20 0a 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c  e .** child tabl
26e8f 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79  e of foreign key
26e90 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65   constraint pFKe
26e91 79 2e 20 49 66 20 61 6e 20 53 51 4c 20 55 50 44  y. If an SQL UPD
26e92 41 54 45 20 69 73 20 65 78 65 63 75 74 65 64 20  ATE is executed 
26e93 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 68 69 6c 64  .** on the child
26e94 20 74 61 62 6c 65 20 6f 66 20 70 46 4b 65 79 2c   table of pFKey,
26e95 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
26e96 73 20 69 6e 76 6f 6b 65 64 20 74 77 69 63 65 20  s invoked twice 
26e97 66 6f 72 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20  for each row.** 
26e98 61 66 66 65 63 74 65 64 20 2d 20 6f 6e 63 65 20  affected - once 
26e99 74 6f 20 22 64 65 6c 65 74 65 22 20 74 68 65 20  to "delete" the 
26e9a 6f 6c 64 20 72 6f 77 2c 20 61 6e 64 20 74 68 65  old row, and the
26e9b 6e 20 61 67 61 69 6e 20 74 6f 20 22 69 6e 73 65  n again to "inse
26e9c 72 74 22 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72  rt" the.** new r
26e9d 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 74  ow..**.** Each t
26e9e 69 6d 65 20 69 74 20 69 73 20 63 61 6c 6c 65 64  ime it is called
26e9f 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
26ea0 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63  generates VDBE c
26ea1 6f 64 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ode to locate th
26ea2 65 0a 2a 2a 20 72 6f 77 20 69 6e 20 74 68 65 20  e.** row in the 
26ea3 70 61 72 65 6e 74 20 74 61 62 6c 65 20 74 68 61  parent table tha
26ea4 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
26ea5 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69   the row being i
26ea6 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 0a 2a 2a  nserted into .**
26ea7 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   or deleted from
26ea8 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
26ea9 2e 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  . If the parent 
26eaa 72 6f 77 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  row can be found
26eab 2c 20 6e 6f 20 0a 2a 2a 20 73 70 65 63 69 61 6c  , no .** special
26eac 20 61 63 74 69 6f 6e 20 69 73 20 74 61 6b 65 6e   action is taken
26ead 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
26eae 74 68 65 20 70 61 72 65 6e 74 20 72 6f 77 20 63  the parent row c
26eaf 61 6e 20 2a 6e 6f 74 2a 20 62 65 0a 2a 2a 20 66  an *not* be.** f
26eb0 6f 75 6e 64 20 69 6e 20 74 68 65 20 70 61 72 65  ound in the pare
26eb1 6e 74 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  nt table:.**.** 
26eb2 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c 20 46 4b    Operation | FK
26eb3 20 74 79 70 65 20 20 20 7c 20 41 63 74 69 6f 6e   type   | Action
26eb4 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d 2d 2d   taken.**   ----
26eb5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26eb6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26eb7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26eb8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26eb9 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 49 4e 53 45  ------.**   INSE
26eba 52 54 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74  RT      immediat
26ebb 65 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  e   Increment th
26ebc 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e  e "immediate con
26ebd 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22  straint counter"
26ebe 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45  ..**.**   DELETE
26ebf 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20        immediate 
26ec0 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20    Decrement the 
26ec1 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74  "immediate const
26ec2 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26ec3 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 20  **.**   INSERT  
26ec4 20 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20      deferred    
26ec5 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64  Increment the "d
26ec6 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
26ec7 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a  nt counter"..**.
26ec8 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20  **   DELETE     
26ec9 20 64 65 66 65 72 72 65 64 20 20 20 20 44 65 63   deferred    Dec
26eca 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65  rement the "defe
26ecb 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20  rred constraint 
26ecc 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20  counter"..**.** 
26ecd 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73  These operations
26ece 20 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20   are identified 
26ecf 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61  in the comment a
26ed0 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69  t the top of thi
26ed1 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b 65 79  s file .** (fkey
26ed2 2e 63 29 20 61 73 20 22 49 2e 31 22 20 61 6e 64  .c) as "I.1" and
26ed3 20 22 44 2e 31 22 2e 0a 2a 2f 0a 73 74 61 74 69   "D.1"..*/.stati
26ed4 63 20 76 6f 69 64 20 66 6b 4c 6f 6f 6b 75 70 50  c void fkLookupP
26ed5 61 72 65 6e 74 28 0a 20 20 50 61 72 73 65 20 2a  arent(.  Parse *
26ed6 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
26ed7 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
26ed8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
26ed9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
26eda 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
26edb 68 6f 75 73 69 6e 67 20 70 54 61 62 20 2a 2f 0a  housing pTab */.
26edc 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
26edd 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e          /* Paren
26ede 74 20 74 61 62 6c 65 20 6f 66 20 46 4b 20 70 46  t table of FK pF
26edf 4b 65 79 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  Key */.  Index *
26ee0 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f  pIdx,          /
26ee1 2a 20 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f  * Unique index o
26ee2 6e 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  n parent key col
26ee3 75 6d 6e 73 20 69 6e 20 70 54 61 62 20 2a 2f 0a  umns in pTab */.
26ee4 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20    FKey *pFKey,  
26ee5 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69          /* Forei
26ee6 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26ee7 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f  t */.  int *aiCo
26ee8 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l,           /* 
26ee9 4d 61 70 20 66 72 6f 6d 20 70 61 72 65 6e 74 20  Map from parent 
26eea 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63  key columns to c
26eeb 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d  hild table colum
26eec 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  ns */.  int regD
26eed 61 74 61 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ata,          /*
26eee 20 41 64 64 72 65 73 73 20 6f 66 20 61 72 72 61   Address of arra
26eef 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 68 69  y containing chi
26ef0 6c 64 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a  ld table row */.
26ef1 20 20 69 6e 74 20 6e 49 6e 63 72 2c 20 20 20 20    int nIncr,    
26ef2 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65          /* Incre
26ef3 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 20  ment constraint 
26ef4 63 6f 75 6e 74 65 72 20 62 79 20 74 68 69 73 20  counter by this 
26ef5 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72  */.  int isIgnor
26ef6 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  e          /* If
26ef7 20 74 72 75 65 2c 20 70 72 65 74 65 6e 64 20 70   true, pretend p
26ef8 54 61 62 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  Tab contains all
26ef9 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 2a 2f 0a   NULL values */.
26efa 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
26efb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26efc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26efd 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
26efe 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ble */.  Vdbe *v
26eff 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
26f00 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 20  e(pParse);      
26f01 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 61 64     /* Vdbe to ad
26f02 64 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20 20 69  d code to */.  i
26f03 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72 73 65  nt iCur = pParse
26f04 2d 3e 6e 54 61 62 20 2d 20 31 3b 20 20 20 20 20  ->nTab - 1;     
26f05 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
26f06 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 75 73 65  or number to use
26f07 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6b 20 3d 20   */.  int iOk = 
26f08 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
26f09 61 62 65 6c 28 76 29 3b 20 20 20 20 20 20 20 20  abel(v);        
26f0a 2f 2a 20 6a 75 6d 70 20 68 65 72 65 20 69 66 20  /* jump here if 
26f0b 70 61 72 65 6e 74 20 6b 65 79 20 66 6f 75 6e 64  parent key found
26f0c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 6e 49 6e   */..  /* If nIn
26f0d 63 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  cr is less than 
26f0e 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 65 63 6b  zero, then check
26f0f 20 61 74 20 72 75 6e 74 69 6d 65 20 69 66 20 74   at runtime if t
26f10 68 65 72 65 20 61 72 65 20 61 6e 79 0a 20 20 2a  here are any.  *
26f11 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 6f  * outstanding co
26f12 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72 65 73  nstraints to res
26f13 6f 6c 76 65 2e 20 49 66 20 74 68 65 72 65 20 61  olve. If there a
26f14 72 65 20 6e 6f 74 2c 20 74 68 65 72 65 20 69 73  re not, there is
26f15 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f   no need.  ** to
26f16 20 63 68 65 63 6b 20 69 66 20 64 65 6c 65 74 69   check if deleti
26f17 6e 67 20 74 68 69 73 20 72 6f 77 20 72 65 73 6f  ng this row reso
26f18 6c 76 65 73 20 61 6e 79 20 6f 75 74 73 74 61 6e  lves any outstan
26f19 64 69 6e 67 20 76 69 6f 6c 61 74 69 6f 6e 73 2e  ding violations.
26f1a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 68 65 63 6b  .  **.  ** Check
26f1b 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 6b   if any of the k
26f1c 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  ey columns in th
26f1d 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20 72 6f  e child table ro
26f1e 77 20 61 72 65 20 4e 55 4c 4c 2e 20 49 66 20 0a  w are NULL. If .
26f1f 20 20 2a 2a 20 61 6e 79 20 61 72 65 2c 20 74 68    ** any are, th
26f20 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
26f21 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  t is considered 
26f22 73 61 74 69 73 66 69 65 64 2e 20 4e 6f 20 6e 65  satisfied. No ne
26f23 65 64 20 74 6f 20 0a 20 20 2a 2a 20 73 65 61 72  ed to .  ** sear
26f24 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 69 6e  ch for a matchin
26f25 67 20 72 6f 77 20 69 6e 20 74 68 65 20 70 61 72  g row in the par
26f26 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20  ent table.  */. 
26f27 20 69 66 28 20 6e 49 6e 63 72 3c 30 20 29 7b 0a   if( nIncr<0 ){.
26f28 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26f29 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66  ddOp2(v, OP_FkIf
26f2a 5a 65 72 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44  Zero, pFKey->isD
26f2b 65 66 65 72 72 65 64 2c 20 69 4f 6b 29 3b 0a 20  eferred, iOk);. 
26f2c 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
26f2d 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  pFKey->nCol; i++
26f2e 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65 67 20  ){.    int iReg 
26f2f 3d 20 61 69 43 6f 6c 5b 69 5d 20 2b 20 72 65 67  = aiCol[i] + reg
26f30 44 61 74 61 20 2b 20 31 3b 0a 20 20 20 20 73 71  Data + 1;.    sq
26f31 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26f32 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52  v, OP_IsNull, iR
26f33 65 67 2c 20 69 4f 6b 29 3b 0a 20 20 7d 0a 0a 20  eg, iOk);.  }.. 
26f34 20 69 66 28 20 69 73 49 67 6e 6f 72 65 3d 3d 30   if( isIgnore==0
26f35 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49 64 78   ){.    if( pIdx
26f36 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
26f37 49 66 20 70 49 64 78 20 69 73 20 4e 55 4c 4c 2c  If pIdx is NULL,
26f38 20 74 68 65 6e 20 74 68 65 20 70 61 72 65 6e 74   then the parent
26f39 20 6b 65 79 20 69 73 20 74 68 65 20 49 4e 54 45   key is the INTE
26f3a 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 0a  GER PRIMARY KEY.
26f3b 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20        ** column 
26f3c 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  of the parent ta
26f3d 62 6c 65 20 28 74 61 62 6c 65 20 70 54 61 62 29  ble (table pTab)
26f3e 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  .  */.      int 
26f3f 69 4d 75 73 74 42 65 49 6e 74 3b 20 20 20 20 20  iMustBeInt;     
26f40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
26f41 72 65 73 73 20 6f 66 20 4d 75 73 74 42 65 49 6e  ress of MustBeIn
26f42 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  t instruction */
26f43 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 54 65  .      int regTe
26f44 6d 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  mp = sqlite3GetT
26f45 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
26f46 20 20 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f    .      /* Invo
26f47 6b 65 20 4d 75 73 74 42 65 49 6e 74 20 74 6f 20  ke MustBeInt to 
26f48 63 6f 65 72 63 65 20 74 68 65 20 63 68 69 6c 64  coerce the child
26f49 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 61 6e   key value to an
26f4a 20 69 6e 74 65 67 65 72 20 28 69 2e 65 2e 20 0a   integer (i.e. .
26f4b 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 79 20 74        ** apply t
26f4c 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
26f4d 68 65 20 70 61 72 65 6e 74 20 6b 65 79 29 2e 20  he parent key). 
26f4e 49 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 74  If this fails, t
26f4f 68 65 6e 20 74 68 65 72 65 0a 20 20 20 20 20 20  hen there.      
26f50 2a 2a 20 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e  ** is no matchin
26f51 67 20 70 61 72 65 6e 74 20 6b 65 79 2e 20 42 65  g parent key. Be
26f52 66 6f 72 65 20 75 73 69 6e 67 20 4d 75 73 74 42  fore using MustB
26f53 65 49 6e 74 2c 20 6d 61 6b 65 20 61 20 63 6f 70  eInt, make a cop
26f54 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68  y of.      ** th
26f55 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72 77 69  e value. Otherwi
26f56 73 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69 6e  se, the value in
26f57 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
26f58 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e  child key column
26f59 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 68  .      ** will h
26f5a 61 76 65 20 49 4e 54 45 47 45 52 20 61 66 66 69  ave INTEGER affi
26f5b 6e 69 74 79 20 61 70 70 6c 69 65 64 20 74 6f 20  nity applied to 
26f5c 69 74 2c 20 77 68 69 63 68 20 6d 61 79 20 6e 6f  it, which may no
26f5d 74 20 62 65 20 63 6f 72 72 65 63 74 2e 20 20 2a  t be correct.  *
26f5e 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
26f5f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26f60 53 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 30 5d 2b  SCopy, aiCol[0]+
26f61 31 2b 72 65 67 44 61 74 61 2c 20 72 65 67 54 65  1+regData, regTe
26f62 6d 70 29 3b 0a 20 20 20 20 20 20 69 4d 75 73 74  mp);.      iMust
26f63 42 65 49 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  BeInt = sqlite3V
26f64 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26f65 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 54 65  MustBeInt, regTe
26f66 6d 70 2c 20 30 29 3b 0a 20 20 0a 20 20 20 20 20  mp, 0);.  .     
26f67 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e   /* If the paren
26f68 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 73  t table is the s
26f69 61 6d 65 20 61 73 20 74 68 65 20 63 68 69 6c 64  ame as the child
26f6a 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65 20 61   table, and we a
26f6b 72 65 20 61 62 6f 75 74 0a 20 20 20 20 20 20 2a  re about.      *
26f6c 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74  * to increment t
26f6d 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f  he constraint-co
26f6e 75 6e 74 65 72 20 28 69 2e 65 2e 20 74 68 69 73  unter (i.e. this
26f6f 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 70   is an INSERT op
26f70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20 20 20 20  eration),.      
26f71 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20 69 66  ** then check if
26f72 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69   the row being i
26f73 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65 73 20  nserted matches 
26f74 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c 20 64  itself. If so, d
26f75 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69  o not.      ** i
26f76 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 6e  ncrement the con
26f77 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 2e  straint-counter.
26f78 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
26f79 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f  Tab==pFKey->pFro
26f7a 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20 29 7b  m && nIncr==1 ){
26f7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26f7c 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
26f7d 5f 45 71 2c 20 72 65 67 44 61 74 61 2c 20 69 4f  _Eq, regData, iO
26f7e 6b 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20  k, regTemp);.   
26f7f 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 73 71     }.  .      sq
26f80 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
26f81 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62  Parse, iCur, iDb
26f82 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52  , pTab, OP_OpenR
26f83 65 61 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ead);.      sqli
26f84 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26f85 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
26f86 43 75 72 2c 20 30 2c 20 72 65 67 54 65 6d 70 29  Cur, 0, regTemp)
26f87 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26f88 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26f89 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b 0a 20  Goto, 0, iOk);. 
26f8a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26f8b 4a 75 6d 70 48 65 72 65 28 76 2c 20 73 71 6c 69  JumpHere(v, sqli
26f8c 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
26f8d 64 72 28 76 29 2d 32 29 3b 0a 20 20 20 20 20 20  dr(v)-2);.      
26f8e 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
26f8f 65 72 65 28 76 2c 20 69 4d 75 73 74 42 65 49 6e  ere(v, iMustBeIn
26f90 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
26f91 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
26f92 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d 70 29  pParse, regTemp)
26f93 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
26f94 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 46     int nCol = pF
26f95 4b 65 79 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20  Key->nCol;.     
26f96 20 69 6e 74 20 72 65 67 54 65 6d 70 20 3d 20 73   int regTemp = s
26f97 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
26f98 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29  ge(pParse, nCol)
26f99 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52  ;.      int regR
26f9a 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ec = sqlite3GetT
26f9b 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
26f9c 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
26f9d 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
26f9e 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
26f9f 2c 20 70 49 64 78 29 3b 0a 20 20 0a 20 20 20 20  , pIdx);.  .    
26fa0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26fa1 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op3(v, OP_OpenRe
26fa2 61 64 2c 20 69 43 75 72 2c 20 70 49 64 78 2d 3e  ad, iCur, pIdx->
26fa3 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20  tnum, iDb);.    
26fa4 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
26fa5 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
26fa6 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59  ar*)pKey, P4_KEY
26fa7 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
26fa8 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
26fa9 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
26faa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26fab 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
26fac 79 2c 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65  y, aiCol[i]+1+re
26fad 67 44 61 74 61 2c 20 72 65 67 54 65 6d 70 2b 69  gData, regTemp+i
26fae 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20  );.      }.  .  
26faf 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61      /* If the pa
26fb0 72 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68  rent table is th
26fb1 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 68  e same as the ch
26fb2 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77  ild table, and w
26fb3 65 20 61 72 65 20 61 62 6f 75 74 0a 20 20 20 20  e are about.    
26fb4 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e    ** to incremen
26fb5 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  t the constraint
26fb6 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74  -counter (i.e. t
26fb7 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
26fb8 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20   operation),.   
26fb9 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b     ** then check
26fba 20 69 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e   if the row bein
26fbb 67 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63 68  g inserted match
26fbc 65 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73 6f  es itself. If so
26fbd 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a  , do not.      *
26fbe 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * increment the 
26fbf 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74  constraint-count
26fc0 65 72 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66  er.  */.      if
26fc1 28 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70  ( pTab==pFKey->p
26fc2 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31  From && nIncr==1
26fc3 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
26fc4 69 4a 75 6d 70 20 3d 20 73 71 6c 69 74 65 33 56  iJump = sqlite3V
26fc5 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
26fc6 29 20 2b 20 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20  ) + nCol + 1;.  
26fc7 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
26fc8 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
26fc9 20 20 20 20 20 20 20 69 6e 74 20 69 43 68 69 6c         int iChil
26fca 64 20 3d 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72  d = aiCol[i]+1+r
26fcb 65 67 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20  egData;.        
26fcc 20 20 69 6e 74 20 69 50 61 72 65 6e 74 20 3d 20    int iParent = 
26fcd 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
26fce 5d 2b 31 2b 72 65 67 44 61 74 61 3b 0a 20 20 20  ]+1+regData;.   
26fcf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26fd0 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
26fd1 65 2c 20 69 43 68 69 6c 64 2c 20 69 4a 75 6d 70  e, iChild, iJump
26fd2 2c 20 69 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  , iParent);.    
26fd3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
26fd4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26fd5 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
26fd6 4f 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a  Ok);.      }.  .
26fd7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26fd8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
26fd9 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 65 6d  keRecord, regTem
26fda 70 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29  p, nCol, regRec)
26fdb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26fdc 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
26fdd 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  1, sqlite3IndexA
26fde 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
26fdf 64 78 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  dx), 0);.      s
26fe0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
26fe1 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Int(v, OP_Found,
26fe2 20 69 43 75 72 2c 20 69 4f 6b 2c 20 72 65 67 52   iCur, iOk, regR
26fe3 65 63 2c 20 30 29 3b 0a 20 20 0a 20 20 20 20 20  ec, 0);.  .     
26fe4 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
26fe5 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
26fe6 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71  egRec);.      sq
26fe7 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26fe8 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
26fe9 67 54 65 6d 70 2c 20 6e 43 6f 6c 29 3b 0a 20 20  gTemp, nCol);.  
26fea 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21    }.  }..  if( !
26feb 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
26fec 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  d && !pParse->pT
26fed 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50 61 72  oplevel && !pPar
26fee 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  se->isMultiWrite
26fef 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
26ff0 61 6c 20 63 61 73 65 3a 20 49 66 20 74 68 69 73  al case: If this
26ff1 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 73 74   is an INSERT st
26ff2 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c  atement that wil
26ff3 6c 20 69 6e 73 65 72 74 20 65 78 61 63 74 6c 79  l insert exactly
26ff4 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20  .    ** one row 
26ff5 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 2c 20  into the table, 
26ff6 72 61 69 73 65 20 61 20 63 6f 6e 73 74 72 61 69  raise a constrai
26ff7 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  nt immediately i
26ff8 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a  nstead of.    **
26ff9 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 61 20   incrementing a 
26ffa 63 6f 75 6e 74 65 72 2e 20 54 68 69 73 20 69 73  counter. This is
26ffb 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74 68   necessary as th
26ffc 65 20 56 4d 20 63 6f 64 65 20 69 73 20 62 65 69  e VM code is bei
26ffd 6e 67 0a 20 20 20 20 2a 2a 20 67 65 6e 65 72 61  ng.    ** genera
26ffe 74 65 64 20 66 6f 72 20 77 69 6c 6c 20 6e 6f 74  ted for will not
26fff 20 6f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   open a statemen
27000 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  t transaction.  
27001 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  */.    assert( n
27002 49 6e 63 72 3d 3d 31 20 29 3b 0a 20 20 20 20 73  Incr==1 );.    s
27003 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
27004 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50  aint(.        pP
27005 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20  arse, OE_Abort, 
27006 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  "foreign key con
27007 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c  straint failed",
27008 20 50 34 5f 53 54 41 54 49 43 0a 20 20 20 20 29   P4_STATIC.    )
27009 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
2700a 66 28 20 6e 49 6e 63 72 3e 30 20 26 26 20 70 46  f( nIncr>0 && pF
2700b 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 3d  Key->isDeferred=
2700c 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2700d 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2700e 28 70 50 61 72 73 65 29 2d 3e 6d 61 79 41 62 6f  (pParse)->mayAbo
2700f 72 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  rt = 1;.    }.  
27010 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27011 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e  Op2(v, OP_FkCoun
27012 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65  ter, pFKey->isDe
27013 66 65 72 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0a  ferred, nIncr);.
27014 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 64    }..  sqlite3Vd
27015 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
27016 2c 20 69 4f 6b 29 3b 0a 20 20 73 71 6c 69 74 65  , iOk);.  sqlite
27017 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27018 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a  P_Close, iCur);.
27019 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
2701a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
2701b 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64   to generate cod
2701c 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 20  e executed when 
2701d 61 20 72 6f 77 20 69 73 20 64 65 6c 65 74 65 64  a row is deleted
2701e 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 61 72  .** from the par
2701f 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72  ent table of for
27020 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
27021 69 6e 74 20 70 46 4b 65 79 20 61 6e 64 2c 20 69  int pFKey and, i
27022 66 20 70 46 4b 65 79 20 69 73 20 0a 2a 2a 20 64  f pFKey is .** d
27023 65 66 65 72 72 65 64 2c 20 77 68 65 6e 20 61 20  eferred, when a 
27024 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  row is inserted 
27025 69 6e 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61  into the same ta
27026 62 6c 65 2e 20 57 68 65 6e 20 67 65 6e 65 72 61  ble. When genera
27027 74 69 6e 67 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  ting.** code for
27028 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45 20 6f   an SQL UPDATE o
27029 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73 20 66  peration, this f
2702a 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
2702b 61 6c 6c 65 64 20 74 77 69 63 65 20 2d 0a 2a 2a  alled twice -.**
2702c 20 6f 6e 63 65 20 74 6f 20 22 64 65 6c 65 74 65   once to "delete
2702d 22 20 74 68 65 20 6f 6c 64 20 72 6f 77 20 61 6e  " the old row an
2702e 64 20 6f 6e 63 65 20 74 6f 20 22 69 6e 73 65 72  d once to "inser
2702f 74 22 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a  t" the new row..
27030 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  **.** The code g
27031 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73  enerated by this
27032 20 66 75 6e 63 74 69 6f 6e 20 73 63 61 6e 73 20   function scans 
27033 74 68 72 6f 75 67 68 20 74 68 65 20 72 6f 77 73  through the rows
27034 20 69 6e 20 74 68 65 20 63 68 69 6c 64 0a 2a 2a   in the child.**
27035 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72   table that corr
27036 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 70 61  espond to the pa
27037 72 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 62  rent table row b
27038 65 69 6e 67 20 64 65 6c 65 74 65 64 20 6f 72 20  eing deleted or 
27039 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20 46 6f 72  inserted..** For
2703a 20 65 61 63 68 20 63 68 69 6c 64 20 72 6f 77 20   each child row 
2703b 66 6f 75 6e 64 2c 20 6f 6e 65 20 6f 66 20 74 68  found, one of th
2703c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 63 74 69  e following acti
2703d 6f 6e 73 20 69 73 20 74 61 6b 65 6e 3a 0a 2a 2a  ons is taken:.**
2703e 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e 20  .**   Operation 
2703f 7c 20 46 4b 20 74 79 70 65 20 20 20 7c 20 41 63  | FK type   | Ac
27040 74 69 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20  tion taken.**   
27041 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27042 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27043 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27044 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27045 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
27046 44 45 4c 45 54 45 20 20 20 20 20 20 69 6d 6d 65  DELETE      imme
27047 64 69 61 74 65 20 20 20 49 6e 63 72 65 6d 65 6e  diate   Incremen
27048 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65  t the "immediate
27049 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e   constraint coun
2704a 74 65 72 22 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ter"..**        
2704b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2704c 20 20 20 4f 72 2c 20 69 66 20 74 68 65 20 4f 4e     Or, if the ON
2704d 20 28 55 50 44 41 54 45 7c 44 45 4c 45 54 45 29   (UPDATE|DELETE)
2704e 20 61 63 74 69 6f 6e 20 69 73 20 52 45 53 54 52   action is RESTR
2704f 49 43 54 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ICT,.**         
27050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27051 20 20 74 68 72 6f 77 20 61 20 22 66 6f 72 65 69    throw a "forei
27052 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
27053 74 20 66 61 69 6c 65 64 22 20 65 78 63 65 70 74  t failed" except
27054 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53  ion..**.**   INS
27055 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64 69 61  ERT      immedia
27056 74 65 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74  te   Decrement t
27057 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f  he "immediate co
27058 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
27059 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54  "..**.**   DELET
2705a 45 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20  E      deferred 
2705b 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65     Increment the
2705c 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74   "deferred const
2705d 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
2705e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2705f 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 72 2c               Or,
27060 20 69 66 20 74 68 65 20 4f 4e 20 28 55 50 44 41   if the ON (UPDA
27061 54 45 7c 44 45 4c 45 54 45 29 20 61 63 74 69 6f  TE|DELETE) actio
27062 6e 20 69 73 20 52 45 53 54 52 49 43 54 2c 0a 2a  n is RESTRICT,.*
27063 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
27064 20 20 20 20 20 20 20 20 20 20 20 20 74 68 72 6f              thro
27065 77 20 61 20 22 66 6f 72 65 69 67 6e 20 6b 65 79  w a "foreign key
27066 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
27067 65 64 22 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a  ed" exception..*
27068 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20  *.**   INSERT   
27069 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20 44     deferred    D
2706a 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65  ecrement the "de
2706b 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
2706c 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a  t counter"..**.*
2706d 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f  * These operatio
2706e 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  ns are identifie
2706f 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
27070 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
27071 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b  his file .** (fk
27072 65 79 2e 63 29 20 61 73 20 22 49 2e 32 22 20 61  ey.c) as "I.2" a
27073 6e 64 20 22 44 2e 32 22 2e 0a 2a 2f 0a 73 74 61  nd "D.2"..*/.sta
27074 74 69 63 20 76 6f 69 64 20 66 6b 53 63 61 6e 43  tic void fkScanC
27075 68 69 6c 64 72 65 6e 28 0a 20 20 50 61 72 73 65  hildren(.  Parse
27076 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
27077 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
27078 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
27079 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
2707a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2707b 20 2f 2a 20 53 72 63 4c 69 73 74 20 63 6f 6e 74   /* SrcList cont
2707c 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
2707d 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 54 61   to scan */.  Ta
2707e 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 49 6e 64  ble *pTab,.  Ind
2707f 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
27080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27081 46 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 64 65  Foreign key inde
27082 78 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b  x */.  FKey *pFK
27083 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ey,             
27084 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67         /* Foreig
27085 6e 20 6b 65 79 20 72 65 6c 61 74 69 6f 6e 73 68  n key relationsh
27086 69 70 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43  ip */.  int *aiC
27087 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
27088 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 20 66          /* Map f
27089 72 6f 6d 20 70 49 64 78 20 63 6f 6c 73 20 74 6f  rom pIdx cols to
2708a 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
2708b 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  s */.  int regDa
2708c 74 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ta,             
2708d 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65         /* Refere
2708e 6e 63 65 64 20 74 61 62 6c 65 20 64 61 74 61 20  nced table data 
2708f 73 74 61 72 74 73 20 68 65 72 65 20 2a 2f 0a 20  starts here */. 
27090 20 69 6e 74 20 6e 49 6e 63 72 20 20 20 20 20 20   int nIncr      
27091 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27092 20 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f 20 69 6e   /* Amount to in
27093 63 72 65 6d 65 6e 74 20 64 65 66 65 72 72 65 64  crement deferred
27094 20 63 6f 75 6e 74 65 72 20 62 79 20 2a 2f 0a 29   counter by */.)
27095 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
27096 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
27097 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
27098 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  handle */.  int 
27099 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
2709a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2709b 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
2709c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2709d 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  re = 0;         
2709e 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2709f 6c 61 75 73 65 20 74 6f 20 73 63 61 6e 20 77 69  lause to scan wi
270a0 74 68 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  th */.  NameCont
270a1 65 78 74 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74  ext sNameContext
270a2 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ;       /* Conte
270a3 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  xt used to resol
270a4 76 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ve WHERE clause 
270a5 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
270a6 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  pWInfo;         
270a7 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
270a8 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 57  used by sqlite3W
270a9 68 65 72 65 58 58 58 28 29 20 2a 2f 0a 20 20 69  hereXXX() */.  i
270aa 6e 74 20 69 46 6b 49 66 5a 65 72 6f 20 3d 20 30  nt iFkIfZero = 0
270ab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
270ac 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  * Address of OP_
270ad 46 6b 49 66 5a 65 72 6f 20 2a 2f 0a 20 20 56 64  FkIfZero */.  Vd
270ae 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
270af 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
270b0 0a 20 20 61 73 73 65 72 74 28 20 21 70 49 64 78  .  assert( !pIdx
270b1 20 7c 7c 20 70 49 64 78 2d 3e 70 54 61 62 6c 65   || pIdx->pTable
270b2 3d 3d 70 54 61 62 20 29 3b 0a 0a 20 20 69 66 28  ==pTab );..  if(
270b3 20 6e 49 6e 63 72 3c 30 20 29 7b 0a 20 20 20 20   nIncr<0 ){.    
270b4 69 46 6b 49 66 5a 65 72 6f 20 3d 20 73 71 6c 69  iFkIfZero = sqli
270b5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
270b6 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 70 46   OP_FkIfZero, pF
270b7 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c  Key->isDeferred,
270b8 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43   0);.  }..  /* C
270b9 72 65 61 74 65 20 61 6e 20 45 78 70 72 20 6f 62  reate an Expr ob
270ba 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 69 6e  ject representin
270bb 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73  g an SQL express
270bc 69 6f 6e 20 6c 69 6b 65 3a 0a 20 20 2a 2a 0a 20  ion like:.  **. 
270bd 20 2a 2a 20 20 20 3c 70 61 72 65 6e 74 2d 6b 65   **   <parent-ke
270be 79 31 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79  y1> = <child-key
270bf 31 3e 20 41 4e 44 20 3c 70 61 72 65 6e 74 2d 6b  1> AND <parent-k
270c0 65 79 32 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65  ey2> = <child-ke
270c1 79 32 3e 20 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a  y2> ....  **.  *
270c2 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  * The collation 
270c3 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 66 6f  sequence used fo
270c4 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  r the comparison
270c5 20 73 68 6f 75 6c 64 20 62 65 20 74 68 61 74 20   should be that 
270c6 6f 66 0a 20 20 2a 2a 20 74 68 65 20 70 61 72 65  of.  ** the pare
270c7 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20  nt key columns. 
270c8 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  The affinity of 
270c9 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
270ca 6f 6c 75 6d 6e 20 73 68 6f 75 6c 64 0a 20 20 2a  olumn should.  *
270cb 2a 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f 20  * be applied to 
270cc 65 61 63 68 20 63 68 69 6c 64 20 6b 65 79 20 76  each child key v
270cd 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65 20  alue before the 
270ce 63 6f 6d 70 61 72 69 73 6f 6e 20 74 61 6b 65 73  comparison takes
270cf 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 66   place..  */.  f
270d0 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d  or(i=0; i<pFKey-
270d1 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
270d2 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20   Expr *pLeft;   
270d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
270d4 2a 20 56 61 6c 75 65 20 66 72 6f 6d 20 70 61 72  * Value from par
270d5 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f  ent table row */
270d6 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68  .    Expr *pRigh
270d7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
270d8 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66     /* Column ref
270d9 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20   to child table 
270da 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 71  */.    Expr *pEq
270db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
270dc 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
270dd 6f 6e 20 28 70 4c 65 66 74 20 3d 20 70 52 69 67  on (pLeft = pRig
270de 68 74 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ht) */.    int i
270df 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
270e0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
270e1 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63  x of column in c
270e2 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 20 0a 20  hild table */ . 
270e3 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
270e4 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
270e5 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75   /* Name of colu
270e6 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62 6c  mn in child tabl
270e7 65 20 2a 2f 0a 0a 20 20 20 20 70 4c 65 66 74 20  e */..    pLeft 
270e8 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
270e9 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30  , TK_REGISTER, 0
270ea 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74  );.    if( pLeft
270eb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74   ){.      /* Set
270ec 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
270ed 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66 66 69  equence and affi
270ee 6e 69 74 79 20 6f 66 20 74 68 65 20 4c 48 53 20  nity of the LHS 
270ef 6f 66 20 65 61 63 68 20 54 4b 5f 45 51 0a 20 20  of each TK_EQ.  
270f0 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
270f1 6e 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  n to the parent 
270f2 6b 65 79 20 63 6f 6c 75 6d 6e 20 64 65 66 61 75  key column defau
270f3 6c 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  lts.  */.      i
270f4 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
270f5 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b     Column *pCol;
270f6 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  .        iCol = 
270f7 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
270f8 5d 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 20  ];.        pCol 
270f9 3d 20 26 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d  = &pIdx->pTable-
270fa 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
270fb 20 20 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62       pLeft->iTab
270fc 6c 65 20 3d 20 72 65 67 44 61 74 61 2b 69 43 6f  le = regData+iCo
270fd 6c 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 4c 65  l+1;.        pLe
270fe 66 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 70  ft->affinity = p
270ff 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 20  Col->affinity;. 
27100 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 70 43         pLeft->pC
27101 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  oll = sqlite3Loc
27102 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
27103 65 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b  e, pCol->zColl);
27104 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27105 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69 54 61        pLeft->iTa
27106 62 6c 65 20 3d 20 72 65 67 44 61 74 61 3b 0a 20  ble = regData;. 
27107 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66         pLeft->af
27108 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
27109 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
2710a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
2710b 43 6f 6c 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69  Col = aiCol ? ai
2710c 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e  Col[i] : pFKey->
2710d 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20  aCol[0].iFrom;. 
2710e 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e     assert( iCol>
2710f 3d 30 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  =0 );.    zCol =
27110 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61   pFKey->pFrom->a
27111 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
27112 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71  .    pRight = sq
27113 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
27114 5f 49 44 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  _ID, zCol);.    
27115 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78  pEq = sqlite3PEx
27116 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51  pr(pParse, TK_EQ
27117 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
27118 20 30 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20   0);.    pWhere 
27119 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
2711a 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70 45 71  (db, pWhere, pEq
2711b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
2711c 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
2711d 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
2711e 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c  he parent table,
2711f 20 61 6e 64 20 74 68 69 73 20 73 63 61 6e 0a 20   and this scan. 
27120 20 2a 2a 20 69 73 20 74 61 6b 69 6e 67 20 70 6c   ** is taking pl
27121 61 63 65 20 61 73 20 70 61 72 74 20 6f 66 20 61  ace as part of a
27122 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
27123 6e 20 28 6f 70 65 72 61 74 69 6f 6e 20 44 2e 32  n (operation D.2
27124 29 2c 20 6f 6d 69 74 20 74 68 65 0a 20 20 2a 2a  ), omit the.  **
27125 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
27126 65 64 20 66 72 6f 6d 20 74 68 65 20 73 63 61 6e  ed from the scan
27127 20 62 79 20 61 64 64 69 6e 67 20 28 24 72 6f 77   by adding ($row
27128 69 64 20 21 3d 20 72 6f 77 69 64 29 20 74 6f 20  id != rowid) to 
27129 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a 2a 20  the WHERE .  ** 
2712a 63 6c 61 75 73 65 2c 20 77 68 65 72 65 20 24 72  clause, where $r
2712b 6f 77 69 64 20 69 73 20 74 68 65 20 72 6f 77 69  owid is the rowi
2712c 64 20 6f 66 20 74 68 65 20 72 6f 77 20 62 65 69  d of the row bei
2712d 6e 67 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a  ng deleted.  */.
2712e 20 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65    if( pTab==pFKe
2712f 79 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63  y->pFrom && nInc
27130 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20  r>0 ){.    Expr 
27131 2a 70 45 71 3b 20 20 20 20 20 20 20 20 20 20 20  *pEq;           
27132 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72           /* Expr
27133 65 73 73 69 6f 6e 20 28 70 4c 65 66 74 20 3d 20  ession (pLeft = 
27134 70 52 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 45  pRight) */.    E
27135 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20  xpr *pLeft;     
27136 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27137 56 61 6c 75 65 20 66 72 6f 6d 20 70 61 72 65 6e  Value from paren
27138 74 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20  t table row */. 
27139 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b     Expr *pRight;
2713a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2713b 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74   /* Column ref t
2713c 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f  o child table */
2713d 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c  .    pLeft = sql
2713e 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2713f 52 45 47 49 53 54 45 52 2c 20 30 29 3b 0a 20 20  REGISTER, 0);.  
27140 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74    pRight = sqlit
27141 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 43 4f  e3Expr(db, TK_CO
27142 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66  LUMN, 0);.    if
27143 28 20 70 4c 65 66 74 20 26 26 20 70 52 69 67 68  ( pLeft && pRigh
27144 74 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 66 74  t ){.      pLeft
27145 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67 44 61  ->iTable = regDa
27146 74 61 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 2d  ta;.      pLeft-
27147 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49  >affinity = SQLI
27148 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a  TE_AFF_INTEGER;.
27149 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 69 54        pRight->iT
2714a 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e 61 5b 30  able = pSrc->a[0
2714b 5d 2e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ].iCursor;.     
2714c 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e   pRight->iColumn
2714d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
2714e 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45   pEq = sqlite3PE
2714f 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
27150 45 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  E, pLeft, pRight
27151 2c 20 30 29 3b 0a 20 20 20 20 70 57 68 65 72 65  , 0);.    pWhere
27152 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e   = sqlite3ExprAn
27153 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70 45  d(db, pWhere, pE
27154 71 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  q);.  }..  /* Re
27155 73 6f 6c 76 65 20 74 68 65 20 72 65 66 65 72 65  solve the refere
27156 6e 63 65 73 20 69 6e 20 74 68 65 20 57 48 45 52  nces in the WHER
27157 45 20 63 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 6d  E clause. */.  m
27158 65 6d 73 65 74 28 26 73 4e 61 6d 65 43 6f 6e 74  emset(&sNameCont
27159 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e  ext, 0, sizeof(N
2715a 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  ameContext));.  
2715b 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72  sNameContext.pSr
2715c 63 4c 69 73 74 20 3d 20 70 53 72 63 3b 0a 20 20  cList = pSrc;.  
2715d 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 50 61  sNameContext.pPa
2715e 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
2715f 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
27160 70 72 4e 61 6d 65 73 28 26 73 4e 61 6d 65 43 6f  prNames(&sNameCo
27161 6e 74 65 78 74 2c 20 70 57 68 65 72 65 29 3b 0a  ntext, pWhere);.
27162 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 56 44 42  .  /* Create VDB
27163 45 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67  E to loop throug
27164 68 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e  h the entries in
27165 20 70 53 72 63 20 74 68 61 74 20 6d 61 74 63 68   pSrc that match
27166 20 74 68 65 20 57 48 45 52 45 0a 20 20 2a 2a 20   the WHERE.  ** 
27167 63 6c 61 75 73 65 2e 20 49 66 20 74 68 65 20 63  clause. If the c
27168 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6e 6f 74  onstraint is not
27169 20 64 65 66 65 72 72 65 64 2c 20 74 68 72 6f 77   deferred, throw
2716a 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 66 6f   an exception fo
2716b 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20  r.  ** each row 
2716c 66 6f 75 6e 64 2e 20 4f 74 68 65 72 77 69 73 65  found. Otherwise
2716d 2c 20 66 6f 72 20 64 65 66 65 72 72 65 64 20 63  , for deferred c
2716e 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69 6e 63 72  onstraints, incr
2716f 65 6d 65 6e 74 20 74 68 65 0a 20 20 2a 2a 20 64  ement the.  ** d
27170 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
27171 6e 74 20 63 6f 75 6e 74 65 72 20 62 79 20 6e 49  nt counter by nI
27172 6e 63 72 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ncr for each row
27173 20 73 65 6c 65 63 74 65 64 2e 20 20 2a 2f 0a 20   selected.  */. 
27174 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
27175 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
27176 73 65 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65  se, pSrc, pWhere
27177 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e  , 0, 0);.  if( n
27178 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65 79 2d  Incr>0 && pFKey-
27179 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 29  >isDeferred==0 )
2717a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  {.    sqlite3Par
2717b 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2717c 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31  e)->mayAbort = 1
2717d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
2717e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2717f 46 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79  FkCounter, pFKey
27180 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 6e 49  ->isDeferred, nI
27181 6e 63 72 29 3b 0a 20 20 69 66 28 20 70 57 49 6e  ncr);.  if( pWIn
27182 66 6f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  fo ){.    sqlite
27183 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
27184 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65  );.  }..  /* Cle
27185 61 6e 20 75 70 20 74 68 65 20 57 48 45 52 45 20  an up the WHERE 
27186 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 75 63 74  clause construct
27187 65 64 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 73  ed above. */.  s
27188 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
27189 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
2718a 69 66 28 20 69 46 6b 49 66 5a 65 72 6f 20 29 7b  if( iFkIfZero ){
2718b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2718c 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 46 6b 49  JumpHere(v, iFkI
2718d 66 5a 65 72 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  fZero);.  }.}../
2718e 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2718f 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  on returns a poi
27190 6e 74 65 72 20 74 6f 20 74 68 65 20 68 65 61 64  nter to the head
27191 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
27192 74 20 6f 66 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74  t of FK.** const
27193 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68  raints for which
27194 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 74   table pTab is t
27195 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
27196 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a   For example,.**
27197 20 67 69 76 65 6e 20 74 68 65 20 66 6f 6c 6c 6f   given the follo
27198 77 69 6e 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a  wing schema:.**.
27199 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
2719a 45 20 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b  E t1(a PRIMARY K
2719b 45 59 29 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45  EY);.**   CREATE
2719c 20 54 41 42 4c 45 20 74 32 28 62 20 52 45 46 45   TABLE t2(b REFE
2719d 52 45 4e 43 45 53 20 74 31 28 61 29 3b 0a 2a 2a  RENCES t1(a);.**
2719e 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73  .** Calling this
2719f 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
271a0 61 62 6c 65 20 22 74 31 22 20 61 73 20 61 6e 20  able "t1" as an 
271a1 61 72 67 75 6d 65 6e 74 20 72 65 74 75 72 6e 73  argument returns
271a2 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
271a3 20 74 68 65 20 46 4b 65 79 20 73 74 72 75 63 74   the FKey struct
271a4 75 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  ure representing
271a5 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
271a6 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74   constraint on t
271a7 61 62 6c 65 0a 2a 2a 20 22 74 32 22 2e 20 43 61  able.** "t2". Ca
271a8 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74  lling this funct
271a9 69 6f 6e 20 77 69 74 68 20 22 74 32 22 20 61 73  ion with "t2" as
271aa 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 6f   the argument wo
271ab 75 6c 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  uld return a.** 
271ac 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 61 73  NULL pointer (as
271ad 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 46 4b   there are no FK
271ae 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
271af 20 77 68 69 63 68 20 74 32 20 69 73 20 74 68 65   which t2 is the
271b0 20 70 61 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65   parent.** table
271b1 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
271b2 56 41 54 45 20 46 4b 65 79 20 2a 73 71 6c 69 74  VATE FKey *sqlit
271b3 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54  e3FkReferences(T
271b4 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69  able *pTab){.  i
271b5 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
271b6 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d  e3Strlen30(pTab-
271b7 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  >zName);.  retur
271b8 6e 20 28 46 4b 65 79 20 2a 29 73 71 6c 69 74 65  n (FKey *)sqlite
271b9 33 48 61 73 68 46 69 6e 64 28 26 70 54 61 62 2d  3HashFind(&pTab-
271ba 3e 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61  >pSchema->fkeyHa
271bb 73 68 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  sh, pTab->zName,
271bc 20 6e 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   nName);.}../*.*
271bd 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
271be 75 6d 65 6e 74 20 69 73 20 61 20 54 72 69 67 67  ument is a Trigg
271bf 65 72 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c  er structure all
271c0 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 0a 2a  ocated by the .*
271c1 2a 20 66 6b 41 63 74 69 6f 6e 54 72 69 67 67 65  * fkActionTrigge
271c2 72 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 69  r() routine. Thi
271c3 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74  s function delet
271c4 65 73 20 74 68 65 20 54 72 69 67 67 65 72 20 73  es the Trigger s
271c5 74 72 75 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20  tructure.** and 
271c6 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 2d 63  all of its sub-c
271c7 6f 6d 70 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  omponents..**.**
271c8 20 54 68 65 20 54 72 69 67 67 65 72 20 73 74 72   The Trigger str
271c9 75 63 74 75 72 65 20 6f 72 20 61 6e 79 20 6f 66  ucture or any of
271ca 20 69 74 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65   its sub-compone
271cb 6e 74 73 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  nts may be alloc
271cc 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  ated from.** the
271cd 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
271ce 72 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64  r belonging to d
271cf 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
271d0 62 4d 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bMem..*/.static 
271d1 76 6f 69 64 20 66 6b 54 72 69 67 67 65 72 44 65  void fkTriggerDe
271d2 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
271d3 4d 65 6d 2c 20 54 72 69 67 67 65 72 20 2a 70 29  Mem, Trigger *p)
271d4 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
271d5 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
271d6 74 65 70 20 3d 20 70 2d 3e 73 74 65 70 5f 6c 69  tep = p->step_li
271d7 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  st;.    sqlite3E
271d8 78 70 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c  xprDelete(dbMem,
271d9 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 29 3b   pStep->pWhere);
271da 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
271db 4c 69 73 74 44 65 6c 65 74 65 28 64 62 4d 65 6d  ListDelete(dbMem
271dc 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
271dd 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  st);.    sqlite3
271de 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 4d  SelectDelete(dbM
271df 65 6d 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  em, pStep->pSele
271e0 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ct);.    sqlite3
271e1 45 78 70 72 44 65 6c 65 74 65 28 64 62 4d 65 6d  ExprDelete(dbMem
271e2 2c 20 70 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20  , p->pWhen);.   
271e3 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
271e4 62 4d 65 6d 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a  bMem, p);.  }.}.
271e5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
271e6 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
271e7 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  o generate code 
271e8 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 20 74  that runs when t
271e9 61 62 6c 65 20 70 54 61 62 20 69 73 0a 2a 2a 20  able pTab is.** 
271ea 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 66 72  being dropped fr
271eb 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
271ec 20 54 68 65 20 53 72 63 4c 69 73 74 20 70 61 73   The SrcList pas
271ed 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
271ee 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  d argument.** to
271ef 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   this function c
271f0 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
271f1 20 65 6e 74 72 79 20 67 75 61 72 61 6e 74 65 65   entry guarantee
271f2 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0a  d to resolve to.
271f3 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a  ** table pTab..*
271f4 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e  *.** Normally, n
271f5 6f 20 63 6f 64 65 20 69 73 20 72 65 71 75 69 72  o code is requir
271f6 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  ed. However, if 
271f7 65 69 74 68 65 72 0a 2a 2a 0a 2a 2a 20 20 20 28  either.**.**   (
271f8 61 29 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  a) The table is 
271f9 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
271fa 20 6f 66 20 61 20 46 4b 20 63 6f 6e 73 74 72 61   of a FK constra
271fb 69 6e 74 2c 20 6f 72 0a 2a 2a 20 20 20 28 62 29  int, or.**   (b)
271fc 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   The table is th
271fd 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20 6f 66  e child table of
271fe 20 61 20 64 65 66 65 72 72 65 64 20 46 4b 20 63   a deferred FK c
271ff 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 69 74  onstraint and it
27200 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 64 65 74   is.**       det
27201 65 72 6d 69 6e 65 64 20 61 74 20 72 75 6e 74 69  ermined at runti
27202 6d 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72  me that there ar
27203 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  e outstanding de
27204 66 65 72 72 65 64 20 46 4b 20 0a 2a 2a 20 20 20  ferred FK .**   
27205 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 20 76      constraint v
27206 69 6f 6c 61 74 69 6f 6e 73 20 69 6e 20 74 68 65  iolations in the
27207 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 0a 2a 2a   database,.**.**
27208 20 74 68 65 6e 20 74 68 65 20 65 71 75 69 76 61   then the equiva
27209 6c 65 6e 74 20 6f 66 20 22 44 45 4c 45 54 45 20  lent of "DELETE 
2720a 46 52 4f 4d 20 3c 74 62 6c 3e 22 20 69 73 20 65  FROM <tbl>" is e
2720b 78 65 63 75 74 65 64 20 62 65 66 6f 72 65 20 64  xecuted before d
2720c 72 6f 70 70 69 6e 67 0a 2a 2a 20 74 68 65 20 74  ropping.** the t
2720d 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 64 61  able from the da
2720e 74 61 62 61 73 65 2e 20 54 72 69 67 67 65 72 73  tabase. Triggers
2720f 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 77 68   are disabled wh
27210 69 6c 65 20 72 75 6e 6e 69 6e 67 20 74 68 69 73  ile running this
27211 0a 2a 2a 20 44 45 4c 45 54 45 2c 20 62 75 74 20  .** DELETE, but 
27212 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
27213 6f 6e 73 20 61 72 65 20 6e 6f 74 2e 0a 2a 2f 0a  ons are not..*/.
27214 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
27215 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f  oid sqlite3FkDro
27216 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  pTable(Parse *pP
27217 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
27218 4e 61 6d 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  Name, Table *pTa
27219 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
2721a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2721b 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
2721c 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
2721d 65 79 73 29 20 26 26 20 21 49 73 56 69 72 74 75  eys) && !IsVirtu
2721e 61 6c 28 70 54 61 62 29 20 26 26 20 21 70 54 61  al(pTab) && !pTa
2721f 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
27220 20 20 69 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b    int iSkip = 0;
27221 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73  .    Vdbe *v = s
27222 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
27223 61 72 73 65 29 3b 0a 0a 20 20 20 20 61 73 73 65  arse);..    asse
27224 72 74 28 20 76 20 29 3b 20 20 20 20 20 20 20 20  rt( v );        
27225 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
27226 45 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  E has already be
27227 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
27228 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
27229 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
2722a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )==0 ){.      /*
2722b 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 64 65   Search for a de
2722c 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b  ferred foreign k
2722d 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ey constraint fo
2722e 72 20 77 68 69 63 68 20 74 68 69 73 20 74 61 62  r which this tab
2722f 6c 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74  le.      ** is t
27230 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20  he child table. 
27231 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
27232 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 77   found, return w
27233 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20 2a 2a  ithout .      **
27234 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20   generating any 
27235 56 44 42 45 20 63 6f 64 65 2e 20 49 66 20 6f 6e  VDBE code. If on
27236 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  e can be found, 
27237 74 68 65 6e 20 6a 75 6d 70 20 6f 76 65 72 0a 20  then jump over. 
27238 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69       ** the enti
27239 72 65 20 44 45 4c 45 54 45 20 69 66 20 74 68 65  re DELETE if the
2723a 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  re are no outsta
2723b 6e 64 69 6e 67 20 64 65 66 65 72 72 65 64 20 63  nding deferred c
2723c 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 20  onstraints.     
2723d 20 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 73 74   ** when this st
2723e 61 74 65 6d 65 6e 74 20 69 73 20 72 75 6e 2e 20  atement is run. 
2723f 20 2a 2f 0a 20 20 20 20 20 20 46 4b 65 79 20 2a   */.      FKey *
27240 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70  p;.      for(p=p
27241 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70  Tab->pFKey; p; p
27242 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a  =p->pNextFrom){.
27243 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69          if( p->i
27244 73 44 65 66 65 72 72 65 64 20 29 20 62 72 65 61  sDeferred ) brea
27245 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
27246 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e   if( !p ) return
27247 3b 0a 20 20 20 20 20 20 69 53 6b 69 70 20 3d 20  ;.      iSkip = 
27248 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
27249 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 73  abel(v);.      s
2724a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2724b 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c  (v, OP_FkIfZero,
2724c 20 31 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20   1, iSkip);.    
2724d 7d 0a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  }..    pParse->d
2724e 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 3d  isableTriggers =
2724f 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   1;.    sqlite3D
27250 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
27251 2c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  , sqlite3SrcList
27252 44 75 70 28 64 62 2c 20 70 4e 61 6d 65 2c 20 30  Dup(db, pName, 0
27253 29 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73  ), 0);.    pPars
27254 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65  e->disableTrigge
27255 72 73 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  rs = 0;..    /* 
27256 49 66 20 74 68 65 20 44 45 4c 45 54 45 20 68 61  If the DELETE ha
27257 73 20 67 65 6e 65 72 61 74 65 64 20 69 6d 6d 65  s generated imme
27258 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
27259 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a 20 20  y constraint .  
2725a 20 20 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c    ** violations,
2725b 20 68 61 6c 74 20 74 68 65 20 56 44 42 45 20 61   halt the VDBE a
2725c 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
2725d 6f 72 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  or at this point
2725e 2c 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20  , before.    ** 
2725f 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e  any modification
27260 73 20 74 6f 20 74 68 65 20 73 63 68 65 6d 61 20  s to the schema 
27261 61 72 65 20 6d 61 64 65 2e 20 54 68 69 73 20 69  are made. This i
27262 73 20 62 65 63 61 75 73 65 20 73 74 61 74 65 6d  s because statem
27263 65 6e 74 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  ent.    ** trans
27264 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20  actions are not 
27265 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
27266 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e   schema changes.
27267 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
27268 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27269 5f 46 6b 49 66 5a 65 72 6f 2c 20 30 2c 20 73 71  _FkIfZero, 0, sq
2726a 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2726b 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20  Addr(v)+2);.    
2726c 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
2726d 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70  raint(.        p
2726e 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c  Parse, OE_Abort,
2726f 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   "foreign key co
27270 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
27271 2c 20 50 34 5f 53 54 41 54 49 43 0a 20 20 20 20  , P4_STATIC.    
27272 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 53 6b 69  );..    if( iSki
27273 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
27274 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
27275 65 6c 28 76 2c 20 69 53 6b 69 70 29 3b 0a 20 20  el(v, iSkip);.  
27276 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
27277 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
27278 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e  s called when in
27279 73 65 72 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e  serting, deletin
2727a 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20  g or updating a 
2727b 72 6f 77 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 20  row of.** table 
2727c 70 54 61 62 20 74 6f 20 67 65 6e 65 72 61 74 65  pTab to generate
2727d 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 70 65   VDBE code to pe
2727e 72 66 6f 72 6d 20 66 6f 72 65 69 67 6e 20 6b 65  rform foreign ke
2727f 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a 2a 2a  y constraint .**
27280 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
27281 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  the operation..*
27282 2a 0a 2a 2a 20 46 6f 72 20 61 20 44 45 4c 45 54  *.** For a DELET
27283 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 61 72  E operation, par
27284 61 6d 65 74 65 72 20 72 65 67 4f 6c 64 20 69 73  ameter regOld is
27285 20 70 61 73 73 65 64 20 74 68 65 20 69 6e 64 65   passed the inde
27286 78 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73  x of the.** firs
27287 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 6e  t register in an
27288 20 61 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d   array of (pTab-
27289 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65  >nCol+1) registe
2728a 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
2728b 65 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68  e.** rowid of th
2728c 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
2728d 74 65 64 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ted, followed by
2728e 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 6f 6c   each of the col
2728f 75 6d 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6f 66  umn values.** of
27290 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64   the row being d
27291 65 6c 65 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66  eleted, from lef
27292 74 20 74 6f 20 72 69 67 68 74 2e 20 50 61 72 61  t to right. Para
27293 6d 65 74 65 72 20 72 65 67 4e 65 77 20 69 73 20  meter regNew is 
27294 70 61 73 73 65 64 0a 2a 2a 20 7a 65 72 6f 20 69  passed.** zero i
27295 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a  n this case..**.
27296 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 53 45 52 54  ** For an INSERT
27297 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65 67 4f   operation, regO
27298 6c 64 20 69 73 20 70 61 73 73 65 64 20 7a 65 72  ld is passed zer
27299 6f 20 61 6e 64 20 72 65 67 4e 65 77 20 69 73 20  o and regNew is 
2729a 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 66 69  passed the.** fi
2729b 72 73 74 20 72 65 67 69 73 74 65 72 20 6f 66 20  rst register of 
2729c 61 6e 20 61 72 72 61 79 20 6f 66 20 28 70 54 61  an array of (pTa
2729d 62 2d 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73  b->nCol+1) regis
2729e 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ters containing 
2729f 74 68 65 20 6e 65 77 0a 2a 2a 20 72 6f 77 20 64  the new.** row d
272a0 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  ata..**.** For a
272a1 6e 20 55 50 44 41 54 45 20 6f 70 65 72 61 74 69  n UPDATE operati
272a2 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  on, this functio
272a3 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 77 69 63  n is called twic
272a4 65 2e 20 4f 6e 63 65 20 62 65 66 6f 72 65 0a 2a  e. Once before.*
272a5 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 72  * the original r
272a6 65 63 6f 72 64 20 69 73 20 64 65 6c 65 74 65 64  ecord is deleted
272a7 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20   from the table 
272a8 75 73 69 6e 67 20 74 68 65 20 63 61 6c 6c 69 6e  using the callin
272a9 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20  g convention.** 
272aa 64 65 73 63 72 69 62 65 64 20 66 6f 72 20 44 45  described for DE
272ab 4c 45 54 45 2e 20 54 68 65 6e 20 61 67 61 69 6e  LETE. Then again
272ac 20 61 66 74 65 72 20 74 68 65 20 6f 72 69 67 69   after the origi
272ad 6e 61 6c 20 72 65 63 6f 72 64 20 69 73 20 64 65  nal record is de
272ae 6c 65 74 65 64 0a 2a 2a 20 62 75 74 20 62 65 66  leted.** but bef
272af 6f 72 65 20 74 68 65 20 6e 65 77 20 72 65 63 6f  ore the new reco
272b0 72 64 20 69 73 20 69 6e 73 65 72 74 65 64 20 75  rd is inserted u
272b1 73 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54 20  sing the INSERT 
272b2 63 6f 6e 76 65 6e 74 69 6f 6e 2e 20 0a 2a 2f 0a  convention. .*/.
272b3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
272b4 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65  oid sqlite3FkChe
272b5 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ck(.  Parse *pPa
272b6 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
272b7 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
272b8 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
272b9 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
272ba 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
272bb 6f 77 20 69 73 20 62 65 69 6e 67 20 64 65 6c 65  ow is being dele
272bc 74 65 64 20 66 72 6f 6d 20 74 68 69 73 20 74 61  ted from this ta
272bd 62 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20 72 65  ble */ .  int re
272be 67 4f 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  gOld,           
272bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
272c0 76 69 6f 75 73 20 72 6f 77 20 64 61 74 61 20 69  vious row data i
272c1 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f  s stored here */
272c2 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 20 20 20  .  int regNew   
272c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272c4 20 20 20 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61     /* New row da
272c5 74 61 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  ta is stored her
272c6 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  e */.){.  sqlite
272c7 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
272c8 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74  db;       /* Dat
272c9 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
272ca 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
272cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272cc 20 20 2f 2a 20 56 4d 20 74 6f 20 77 72 69 74 65    /* VM to write
272cd 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20 20 46 4b   code to */.  FK
272ce 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20  ey *pFKey;      
272cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
272d0 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   Used to iterate
272d1 20 74 68 72 6f 75 67 68 20 46 4b 73 20 2a 2f 0a   through FKs */.
272d2 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
272d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272d4 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
272d5 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
272d6 67 20 70 54 61 62 20 2a 2f 0a 20 20 63 6f 6e 73  g pTab */.  cons
272d7 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
272d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
272d9 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
272da 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
272db 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72  */.  int isIgnor
272dc 65 45 72 72 6f 72 73 20 3d 20 70 50 61 72 73 65  eErrors = pParse
272dd 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72  ->disableTrigger
272de 73 3b 0a 0a 20 20 2f 2a 20 45 78 61 63 74 6c 79  s;..  /* Exactly
272df 20 6f 6e 65 20 6f 66 20 72 65 67 4f 6c 64 20 61   one of regOld a
272e0 6e 64 20 72 65 67 4e 65 77 20 73 68 6f 75 6c 64  nd regNew should
272e1 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 2a 2f   be non-zero. */
272e2 0a 20 20 61 73 73 65 72 74 28 20 28 72 65 67 4f  .  assert( (regO
272e3 6c 64 3d 3d 30 29 21 3d 28 72 65 67 4e 65 77 3d  ld==0)!=(regNew=
272e4 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  =0) );..  /* If 
272e5 66 6f 72 65 69 67 6e 2d 6b 65 79 73 20 61 72 65  foreign-keys are
272e6 20 64 69 73 61 62 6c 65 64 2c 20 74 68 69 73 20   disabled, this 
272e7 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
272e8 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 28 64  -op. */.  if( (d
272e9 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
272ea 46 6f 72 65 69 67 6e 4b 65 79 73 29 3d 3d 30 20  ForeignKeys)==0 
272eb 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d  ) return;..  v =
272ec 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
272ed 70 50 61 72 73 65 29 3b 0a 20 20 69 44 62 20 3d  pParse);.  iDb =
272ee 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
272ef 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
272f0 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
272f1 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
272f2 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70  Name;..  /* Loop
272f3 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65   through all the
272f4 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
272f5 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69  straints for whi
272f6 63 68 20 70 54 61 62 20 69 73 20 74 68 65 0a 20  ch pTab is the. 
272f7 20 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20   ** child table 
272f8 28 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20  (the table that 
272f9 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
272fa 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 70 61  definition is pa
272fb 72 74 20 6f 66 29 2e 20 20 2a 2f 0a 20 20 66 6f  rt of).  */.  fo
272fc 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46  r(pFKey=pTab->pF
272fd 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  Key; pFKey; pFKe
272fe 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72  y=pFKey->pNextFr
272ff 6f 6d 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  om){.    Table *
27300 70 54 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  pTo;            
27301 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
27302 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67   table of foreig
27303 6e 20 6b 65 79 20 70 46 4b 65 79 20 2a 2f 0a 20  n key pFKey */. 
27304 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
27305 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
27306 20 2f 2a 20 49 6e 64 65 78 20 6f 6e 20 6b 65 79   /* Index on key
27307 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 6f 20   columns in pTo 
27308 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69 46 72  */.    int *aiFr
27309 65 65 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ee = 0;.    int 
2730a 2a 61 69 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20  *aiCol;.    int 
2730b 69 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 69 3b  iCol;.    int i;
2730c 0a 20 20 20 20 69 6e 74 20 69 73 49 67 6e 6f 72  .    int isIgnor
2730d 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46  e = 0;..    /* F
2730e 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74  ind the parent t
2730f 61 62 6c 65 20 6f 66 20 74 68 69 73 20 66 6f 72  able of this for
27310 65 69 67 6e 20 6b 65 79 2e 20 41 6c 73 6f 20 66  eign key. Also f
27311 69 6e 64 20 61 20 75 6e 69 71 75 65 20 69 6e 64  ind a unique ind
27312 65 78 20 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68  ex .    ** on th
27313 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  e parent key col
27314 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65  umns in the pare
27315 6e 74 20 74 61 62 6c 65 2e 20 49 66 20 65 69 74  nt table. If eit
27316 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 20 20  her of these .  
27317 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 74 65 6d    ** schema item
27318 73 20 63 61 6e 6e 6f 74 20 62 65 20 6c 6f 63 61  s cannot be loca
27319 74 65 64 2c 20 73 65 74 20 61 6e 20 65 72 72 6f  ted, set an erro
2731a 72 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  r in pParse and 
2731b 72 65 74 75 72 6e 20 0a 20 20 20 20 2a 2a 20 65  return .    ** e
2731c 61 72 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66  arly.  */.    if
2731d 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
2731e 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20  eTriggers ){.   
2731f 20 20 20 70 54 6f 20 3d 20 73 71 6c 69 74 65 33     pTo = sqlite3
27320 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 70 46  FindTable(db, pF
27321 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0a  Key->zTo, zDb);.
27322 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27323 20 70 54 6f 20 3d 20 73 71 6c 69 74 65 33 4c 6f   pTo = sqlite3Lo
27324 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
27325 2c 20 30 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c  , 0, pFKey->zTo,
27326 20 7a 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20   zDb);.    }.   
27327 20 69 66 28 20 21 70 54 6f 20 7c 7c 20 6c 6f 63   if( !pTo || loc
27328 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50 61  ateFkeyIndex(pPa
27329 72 73 65 2c 20 70 54 6f 2c 20 70 46 4b 65 79 2c  rse, pTo, pFKey,
2732a 20 26 70 49 64 78 2c 20 26 61 69 46 72 65 65 29   &pIdx, &aiFree)
2732b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69   ){.      if( !i
2732c 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20 7c 7c  sIgnoreErrors ||
2732d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2732e 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  d ) return;.    
2732f 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
27330 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  }.    assert( pF
27331 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 7c 7c 20  Key->nCol==1 || 
27332 28 61 69 46 72 65 65 20 26 26 20 70 49 64 78 29  (aiFree && pIdx)
27333 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 61 69 46   );..    if( aiF
27334 72 65 65 20 29 7b 0a 20 20 20 20 20 20 61 69 43  ree ){.      aiC
27335 6f 6c 20 3d 20 61 69 46 72 65 65 3b 0a 20 20 20  ol = aiFree;.   
27336 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43   }else{.      iC
27337 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  ol = pFKey->aCol
27338 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20  [0].iFrom;.     
27339 20 61 69 43 6f 6c 20 3d 20 26 69 43 6f 6c 3b 0a   aiCol = &iCol;.
2733a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
2733b 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c  0; i<pFKey->nCol
2733c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
2733d 28 20 61 69 43 6f 6c 5b 69 5d 3d 3d 70 54 61 62  ( aiCol[i]==pTab
2733e 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
2733f 20 20 20 61 69 43 6f 6c 5b 69 5d 20 3d 20 2d 31     aiCol[i] = -1
27340 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
27341 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
27342 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
27343 20 20 2f 2a 20 52 65 71 75 65 73 74 20 70 65 72    /* Request per
27344 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20  mission to read 
27345 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
27346 6f 6c 75 6d 6e 73 2e 20 49 66 20 74 68 65 20 0a  olumns. If the .
27347 20 20 20 20 20 20 2a 2a 20 61 75 74 68 6f 72 69        ** authori
27348 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20  zation callback 
27349 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
2734a 47 4e 4f 52 45 2c 20 62 65 68 61 76 65 20 61 73  GNORE, behave as
2734b 20 69 66 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a   if any.      **
2734c 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f   values read fro
2734d 6d 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  m the parent tab
2734e 6c 65 20 61 72 65 20 4e 55 4c 4c 2e 20 2a 2f 0a  le are NULL. */.
2734f 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 78 41        if( db->xA
27350 75 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 69  uth ){.        i
27351 6e 74 20 72 63 61 75 74 68 3b 0a 20 20 20 20 20  nt rcauth;.     
27352 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
27353 70 54 6f 2d 3e 61 43 6f 6c 5b 70 49 64 78 20 3f  pTo->aCol[pIdx ?
27354 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
27355 69 5d 20 3a 20 70 54 6f 2d 3e 69 50 4b 65 79 5d  i] : pTo->iPKey]
27356 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
27357 72 63 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33  rcauth = sqlite3
27358 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50 61 72  AuthReadCol(pPar
27359 73 65 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65 2c 20  se, pTo->zName, 
2735a 7a 43 6f 6c 2c 20 69 44 62 29 3b 0a 20 20 20 20  zCol, iDb);.    
2735b 20 20 20 20 69 73 49 67 6e 6f 72 65 20 3d 20 28      isIgnore = (
2735c 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49  rcauth==SQLITE_I
2735d 47 4e 4f 52 45 29 3b 0a 20 20 20 20 20 20 7d 0a  GNORE);.      }.
2735e 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20  #endif.    }..  
2735f 20 20 2f 2a 20 54 61 6b 65 20 61 20 73 68 61 72    /* Take a shar
27360 65 64 2d 63 61 63 68 65 20 61 64 76 69 73 6f 72  ed-cache advisor
27361 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74  y read-lock on t
27362 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
27363 20 41 6c 6c 6f 63 61 74 65 20 0a 20 20 20 20 2a   Allocate .    *
27364 2a 20 61 20 63 75 72 73 6f 72 20 74 6f 20 75 73  * a cursor to us
27365 65 20 74 6f 20 73 65 61 72 63 68 20 74 68 65 20  e to search the 
27366 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20  unique index on 
27367 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
27368 6f 6c 75 6d 6e 73 20 0a 20 20 20 20 2a 2a 20 69  olumns .    ** i
27369 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
2736a 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  le.  */.    sqli
2736b 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
2736c 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2d 3e 74  rse, iDb, pTo->t
2736d 6e 75 6d 2c 20 30 2c 20 70 54 6f 2d 3e 7a 4e 61  num, 0, pTo->zNa
2736e 6d 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  me);.    pParse-
2736f 3e 6e 54 61 62 2b 2b 3b 0a 0a 20 20 20 20 69 66  >nTab++;..    if
27370 28 20 72 65 67 4f 6c 64 21 3d 30 20 29 7b 0a 20  ( regOld!=0 ){. 
27371 20 20 20 20 20 2f 2a 20 41 20 72 6f 77 20 69 73       /* A row is
27372 20 62 65 69 6e 67 20 72 65 6d 6f 76 65 64 20 66   being removed f
27373 72 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61  rom the child ta
27374 62 6c 65 2e 20 53 65 61 72 63 68 20 66 6f 72 20  ble. Search for 
27375 74 68 65 20 70 61 72 65 6e 74 2e 0a 20 20 20 20  the parent..    
27376 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65    ** If the pare
27377 6e 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  nt does not exis
27378 74 2c 20 72 65 6d 6f 76 69 6e 67 20 74 68 65 20  t, removing the 
27379 63 68 69 6c 64 20 72 6f 77 20 72 65 73 6f 6c 76  child row resolv
2737a 65 73 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20  es an .      ** 
2737b 6f 75 74 73 74 61 6e 64 69 6e 67 20 66 6f 72 65  outstanding fore
2737c 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
2737d 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 2a 2f  nt violation. */
2737e 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50  .      fkLookupP
2737f 61 72 65 6e 74 28 70 50 61 72 73 65 2c 20 69 44  arent(pParse, iD
27380 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c 20 70 46  b, pTo, pIdx, pF
27381 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f  Key, aiCol, regO
27382 6c 64 2c 20 2d 31 2c 69 73 49 67 6e 6f 72 65 29  ld, -1,isIgnore)
27383 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
27384 72 65 67 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20  regNew!=0 ){.   
27385 20 20 20 2f 2a 20 41 20 72 6f 77 20 69 73 20 62     /* A row is b
27386 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20 74 68  eing added to th
27387 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49  e child table. I
27388 66 20 61 20 70 61 72 65 6e 74 20 72 6f 77 20 63  f a parent row c
27389 61 6e 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 62  annot.      ** b
2738a 65 20 66 6f 75 6e 64 2c 20 61 64 64 69 6e 67 20  e found, adding 
2738b 74 68 65 20 63 68 69 6c 64 20 72 6f 77 20 68 61  the child row ha
2738c 73 20 76 69 6f 6c 61 74 65 64 20 74 68 65 20 46  s violated the F
2738d 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f  K constraint. */
2738e 20 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70   .      fkLookup
2738f 50 61 72 65 6e 74 28 70 50 61 72 73 65 2c 20 69  Parent(pParse, i
27390 44 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c 20 70  Db, pTo, pIdx, p
27391 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67  FKey, aiCol, reg
27392 4e 65 77 2c 20 2b 31 2c 69 73 49 67 6e 6f 72 65  New, +1,isIgnore
27393 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  );.    }..    sq
27394 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
27395 61 69 46 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20  aiFree);.  }..  
27396 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
27397 61 6c 6c 20 74 68 65 20 66 6f 72 65 69 67 6e 20  all the foreign 
27398 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  key constraints 
27399 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68  that refer to th
2739a 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f  is table */.  fo
2739b 72 28 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65  r(pFKey = sqlite
2739c 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
2739d 61 62 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  ab); pFKey; pFKe
2739e 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f  y=pFKey->pNextTo
2739f 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
273a0 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dx = 0;         
273a1 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20       /* Foreign 
273a2 6b 65 79 20 69 6e 64 65 78 20 66 6f 72 20 70 46  key index for pF
273a3 4b 65 79 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69  Key */.    SrcLi
273a4 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20 69 6e  st *pSrc;.    in
273a5 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 0a 0a 20  t *aiCol = 0;.. 
273a6 20 20 20 69 66 28 20 21 70 46 4b 65 79 2d 3e 69     if( !pFKey->i
273a7 73 44 65 66 65 72 72 65 64 20 26 26 20 21 70 50  sDeferred && !pP
273a8 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  arse->pToplevel 
273a9 26 26 20 21 70 50 61 72 73 65 2d 3e 69 73 4d 75  && !pParse->isMu
273aa 6c 74 69 57 72 69 74 65 20 29 7b 0a 20 20 20 20  ltiWrite ){.    
273ab 20 20 61 73 73 65 72 74 28 20 72 65 67 4f 6c 64    assert( regOld
273ac 3d 3d 30 20 26 26 20 72 65 67 4e 65 77 21 3d 30  ==0 && regNew!=0
273ad 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73   );.      /* Ins
273ae 65 72 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  erting a single 
273af 72 6f 77 20 69 6e 74 6f 20 61 20 70 61 72 65 6e  row into a paren
273b0 74 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 63  t table cannot c
273b1 61 75 73 65 20 61 6e 20 69 6d 6d 65 64 69 61 74  ause an immediat
273b2 65 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 65 69  e.      ** forei
273b3 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e  gn key violation
273b4 2e 20 53 6f 20 64 6f 20 6e 6f 74 68 69 6e 67 20  . So do nothing 
273b5 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
273b6 2f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  /.      continue
273b7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
273b8 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78   locateFkeyIndex
273b9 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
273ba 46 4b 65 79 2c 20 26 70 49 64 78 2c 20 26 61 69  FKey, &pIdx, &ai
273bb 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 66  Col) ){.      if
273bc 28 20 21 69 73 49 67 6e 6f 72 65 45 72 72 6f 72  ( !isIgnoreError
273bd 73 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  s || db->mallocF
273be 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
273bf 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
273c0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
273c1 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79  ( aiCol || pFKey
273c2 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20  ->nCol==1 );..  
273c3 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 53 72    /* Create a Sr
273c4 63 4c 69 73 74 20 73 74 72 75 63 74 75 72 65 20  cList structure 
273c5 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 73 69 6e  containing a sin
273c6 67 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20 74  gle table (the t
273c7 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 74 68 65  able .    ** the
273c8 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61   foreign key tha
273c9 74 20 72 65 66 65 72 73 20 74 6f 20 74 68 69 73  t refers to this
273ca 20 74 61 62 6c 65 20 69 73 20 61 74 74 61 63 68   table is attach
273cb 65 64 20 74 6f 29 2e 20 54 68 69 73 0a 20 20 20  ed to). This.   
273cc 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20   ** is required 
273cd 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 57  for the sqlite3W
273ce 68 65 72 65 58 58 58 28 29 20 69 6e 74 65 72 66  hereXXX() interf
273cf 61 63 65 2e 20 20 2a 2f 0a 20 20 20 20 70 53 72  ace.  */.    pSr
273d0 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
273d1 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20  stAppend(db, 0, 
273d2 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  0, 0);.    if( p
273d3 53 72 63 20 29 7b 0a 20 20 20 20 20 20 73 74 72  Src ){.      str
273d4 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
273d5 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e   *pItem = pSrc->
273d6 61 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  a;.      pItem->
273d7 70 54 61 62 20 3d 20 70 46 4b 65 79 2d 3e 70 46  pTab = pFKey->pF
273d8 72 6f 6d 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  rom;.      pItem
273d9 2d 3e 7a 4e 61 6d 65 20 3d 20 70 46 4b 65 79 2d  ->zName = pFKey-
273da 3e 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20  >pFrom->zName;. 
273db 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62       pItem->pTab
273dc 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20  ->nRef++;.      
273dd 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d  pItem->iCursor =
273de 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
273df 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65  .  .      if( re
273e0 67 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20 20 20  gNew!=0 ){.     
273e1 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65     fkScanChildre
273e2 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20  n(pParse, pSrc, 
273e3 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46 4b 65  pTab, pIdx, pFKe
273e4 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77  y, aiCol, regNew
273e5 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , -1);.      }. 
273e6 20 20 20 20 20 69 66 28 20 72 65 67 4f 6c 64 21       if( regOld!
273e7 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
273e8 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 52   If there is a R
273e9 45 53 54 52 49 43 54 20 61 63 74 69 6f 6e 20 63  ESTRICT action c
273ea 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 74 68  onfigured for th
273eb 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74  e current operat
273ec 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ion.        ** o
273ed 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
273ee 6c 65 20 6f 66 20 74 68 69 73 20 46 4b 2c 20 74  le of this FK, t
273ef 68 65 6e 20 74 68 72 6f 77 20 61 6e 20 65 78 63  hen throw an exc
273f0 65 70 74 69 6f 6e 20 0a 20 20 20 20 20 20 20 20  eption .        
273f1 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  ** immediately i
273f2 66 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61  f the FK constra
273f3 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64 2c  int is violated,
273f4 20 65 76 65 6e 20 69 66 20 74 68 69 73 20 69 73   even if this is
273f5 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 65   a.        ** de
273f6 66 65 72 72 65 64 20 74 72 69 67 67 65 72 2e 20  ferred trigger. 
273f7 54 68 61 74 27 73 20 77 68 61 74 20 52 45 53 54  That's what REST
273f8 52 49 43 54 20 6d 65 61 6e 73 2e 20 54 6f 20 64  RICT means. To d
273f9 65 66 65 72 20 63 68 65 63 6b 69 6e 67 0a 20 20  efer checking.  
273fa 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e        ** the con
273fb 73 74 72 61 69 6e 74 2c 20 74 68 65 20 46 4b 20  straint, the FK 
273fc 73 68 6f 75 6c 64 20 73 70 65 63 69 66 79 20 4e  should specify N
273fd 4f 20 41 43 54 49 4f 4e 20 28 72 65 70 72 65 73  O ACTION (repres
273fe 65 6e 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  ented.        **
273ff 20 75 73 69 6e 67 20 4f 45 5f 4e 6f 6e 65 29 2e   using OE_None).
27400 20 4e 4f 20 41 43 54 49 4f 4e 20 69 73 20 74 68   NO ACTION is th
27401 65 20 64 65 66 61 75 6c 74 2e 20 20 2a 2f 0a 20  e default.  */. 
27402 20 20 20 20 20 20 20 66 6b 53 63 61 6e 43 68 69         fkScanChi
27403 6c 64 72 65 6e 28 70 50 61 72 73 65 2c 20 70 53  ldren(pParse, pS
27404 72 63 2c 20 70 54 61 62 2c 20 70 49 64 78 2c 20  rc, pTab, pIdx, 
27405 70 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65  pFKey, aiCol, re
27406 67 4f 6c 64 2c 20 31 29 3b 0a 20 20 20 20 20 20  gOld, 1);.      
27407 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  }.      pItem->z
27408 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
27409 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2740a 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a  lete(db, pSrc);.
2740b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2740c 33 44 62 46 72 65 65 28 64 62 2c 20 61 69 43 6f  3DbFree(db, aiCo
2740d 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 64 65 66 69  l);.  }.}..#defi
2740e 6e 65 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 78  ne COLUMN_MASK(x
2740f 29 20 28 28 28 78 29 3e 33 31 29 20 3f 20 30 78  ) (((x)>31) ? 0x
27410 66 66 66 66 66 66 66 66 20 3a 20 28 28 75 33 32  ffffffff : ((u32
27411 29 31 3c 3c 28 78 29 29 29 0a 0a 2f 2a 0a 2a 2a  )1<<(x)))../*.**
27412 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
27413 73 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20  s called before 
27414 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
27415 74 6f 20 75 70 64 61 74 65 20 6f 72 20 64 65 6c  to update or del
27416 65 74 65 20 61 20 0a 2a 2a 20 72 6f 77 20 63 6f  ete a .** row co
27417 6e 74 61 69 6e 65 64 20 69 6e 20 74 61 62 6c 65  ntained in table
27418 20 70 54 61 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pTab..*/.SQLITE
27419 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
2741a 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 0a 20  ite3FkOldmask(. 
2741b 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2741c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2741d 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2741e 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
2741f 61 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ab              
27420 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
27421 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 2a  being modified *
27422 2f 0a 29 7b 0a 20 20 75 33 32 20 6d 61 73 6b 20  /.){.  u32 mask 
27423 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73  = 0;.  if( pPars
27424 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  e->db->flags&SQL
27425 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20  ITE_ForeignKeys 
27426 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a  ){.    FKey *p;.
27427 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
27428 6f 72 28 70 3d 70 54 61 62 2d 3e 70 46 4b 65 79  or(p=pTab->pFKey
27429 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 46  ; p; p=p->pNextF
2742a 72 6f 6d 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  rom){.      for(
2742b 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
2742c 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c  i++) mask |= COL
2742d 55 4d 4e 5f 4d 41 53 4b 28 70 2d 3e 61 43 6f 6c  UMN_MASK(p->aCol
2742e 5b 69 5d 2e 69 46 72 6f 6d 29 3b 0a 20 20 20 20  [i].iFrom);.    
2742f 7d 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69  }.    for(p=sqli
27430 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28  te3FkReferences(
27431 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70  pTab); p; p=p->p
27432 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20 20 49  NextTo){.      I
27433 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 0a  ndex *pIdx = 0;.
27434 20 20 20 20 20 20 6c 6f 63 61 74 65 46 6b 65 79        locateFkey
27435 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54  Index(pParse, pT
27436 61 62 2c 20 70 2c 20 26 70 49 64 78 2c 20 30 29  ab, p, &pIdx, 0)
27437 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ;.      if( pIdx
27438 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
27439 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  i=0; i<pIdx->nCo
2743a 6c 75 6d 6e 3b 20 69 2b 2b 29 20 6d 61 73 6b 20  lumn; i++) mask 
2743b 7c 3d 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70  |= COLUMN_MASK(p
2743c 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
2743d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2743e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
2743f 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  sk;.}../*.** Thi
27440 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
27441 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65 6e 65  lled before gene
27442 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  rating code to u
27443 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65 20  pdate or delete 
27444 61 20 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61 69  a .** row contai
27445 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70 54 61  ned in table pTa
27446 62 2e 20 49 66 20 74 68 65 20 6f 70 65 72 61 74  b. If the operat
27447 69 6f 6e 20 69 73 20 61 20 44 45 4c 45 54 45 2c  ion is a DELETE,
27448 20 74 68 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74   then.** paramet
27449 65 72 20 61 43 68 61 6e 67 65 20 69 73 20 70 61  er aChange is pa
2744a 73 73 65 64 20 61 20 4e 55 4c 4c 20 76 61 6c 75  ssed a NULL valu
2744b 65 2e 20 46 6f 72 20 61 6e 20 55 50 44 41 54 45  e. For an UPDATE
2744c 2c 20 61 43 68 61 6e 67 65 20 70 6f 69 6e 74 73  , aChange points
2744d 0a 2a 2a 20 74 6f 20 61 6e 20 61 72 72 61 79 20  .** to an array 
2744e 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68 65 72 65  of size N, where
2744f 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
27450 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
27451 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 20 49 66  able pTab..** If
27452 20 74 68 65 20 69 27 74 68 20 63 6f 6c 75 6d 6e   the i'th column
27453 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64   is not modified
27454 20 62 79 20 74 68 65 20 55 50 44 41 54 45 2c 20   by the UPDATE, 
27455 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70  then the corresp
27456 6f 6e 64 69 6e 67 20 0a 2a 2a 20 65 6e 74 72 79  onding .** entry
27457 20 69 6e 20 74 68 65 20 61 43 68 61 6e 67 65 5b   in the aChange[
27458 5d 20 61 72 72 61 79 20 69 73 20 73 65 74 20 74  ] array is set t
27459 6f 20 2d 31 2e 20 49 66 20 74 68 65 20 63 6f 6c  o -1. If the col
2745a 75 6d 6e 20 69 73 20 6d 6f 64 69 66 69 65 64 2c  umn is modified,
2745b 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 69 73  .** the value is
2745c 20 30 20 6f 72 20 67 72 65 61 74 65 72 2e 20 50   0 or greater. P
2745d 61 72 61 6d 65 74 65 72 20 63 68 6e 67 52 6f 77  arameter chngRow
2745e 69 64 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  id is set to tru
2745f 65 20 69 66 20 74 68 65 0a 2a 2a 20 55 50 44 41  e if the.** UPDA
27460 54 45 20 73 74 61 74 65 6d 65 6e 74 20 6d 6f 64  TE statement mod
27461 69 66 69 65 73 20 74 68 65 20 72 6f 77 69 64 20  ifies the rowid 
27462 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 74 61  fields of the ta
27463 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ble..**.** If an
27464 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 72  y foreign key pr
27465 6f 63 65 73 73 69 6e 67 20 77 69 6c 6c 20 62 65  ocessing will be
27466 20 72 65 71 75 69 72 65 64 2c 20 74 68 69 73 20   required, this 
27467 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
27468 0a 2a 2a 20 74 72 75 65 2e 20 49 66 20 74 68 65  .** true. If the
27469 72 65 20 69 73 20 6e 6f 20 66 6f 72 65 69 67 6e  re is no foreign
2746a 20 6b 65 79 20 72 65 6c 61 74 65 64 20 70 72 6f   key related pro
2746b 63 65 73 73 69 6e 67 2c 20 74 68 69 73 20 66 75  cessing, this fu
2746c 6e 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 72  nction .** retur
2746d 6e 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c  ns false..*/.SQL
2746e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2746f 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65  sqlite3FkRequire
27470 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  d(.  Parse *pPar
27471 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
27472 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
27473 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
27474 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
27475 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
27476 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  ble being modifi
27477 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 43 68  ed */.  int *aCh
27478 61 6e 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  ange,           
27479 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 4e          /* Non-N
2747a 55 4c 4c 20 66 6f 72 20 55 50 44 41 54 45 20 6f  ULL for UPDATE o
2747b 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  perations */.  i
2747c 6e 74 20 63 68 6e 67 52 6f 77 69 64 20 20 20 20  nt chngRowid    
2747d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2747e 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54  * True for UPDAT
2747f 45 20 74 68 61 74 20 61 66 66 65 63 74 73 20 72  E that affects r
27480 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  owid */.){.  if(
27481 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
27482 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
27483 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 69 66 28  nKeys ){.    if(
27484 20 21 61 43 68 61 6e 67 65 20 29 7b 0a 20 20 20   !aChange ){.   
27485 20 20 20 2f 2a 20 41 20 44 45 4c 45 54 45 20 6f     /* A DELETE o
27486 70 65 72 61 74 69 6f 6e 2e 20 46 6f 72 65 69 67  peration. Foreig
27487 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67  n key processing
27488 20 69 73 20 72 65 71 75 69 72 65 64 20 69 66 20   is required if 
27489 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 61  the .      ** ta
2748a 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20  ble in question 
2748b 69 73 20 65 69 74 68 65 72 20 74 68 65 20 63 68  is either the ch
2748c 69 6c 64 20 6f 72 20 70 61 72 65 6e 74 20 74 61  ild or parent ta
2748d 62 6c 65 20 66 6f 72 20 61 6e 79 20 0a 20 20 20  ble for any .   
2748e 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65     ** foreign ke
2748f 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 2a  y constraint.  *
27490 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28  /.      return (
27491 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
27492 63 65 73 28 70 54 61 62 29 20 7c 7c 20 70 54 61  ces(pTab) || pTa
27493 62 2d 3e 70 46 4b 65 79 29 3b 0a 20 20 20 20 7d  b->pFKey);.    }
27494 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54  else{.      /* T
27495 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45  his is an UPDATE
27496 2e 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 70 72  . Foreign key pr
27497 6f 63 65 73 73 69 6e 67 20 69 73 20 6f 6e 6c 79  ocessing is only
27498 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
27499 0a 20 20 20 20 20 20 2a 2a 20 6f 70 65 72 61 74  .      ** operat
2749a 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 6f 6e 65  ion modifies one
2749b 20 6f 72 20 6d 6f 72 65 20 63 68 69 6c 64 20 6f   or more child o
2749c 72 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  r parent key col
2749d 75 6d 6e 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69  umns. */.      i
2749e 6e 74 20 69 3b 0a 20 20 20 20 20 20 46 4b 65 79  nt i;.      FKey
2749f 20 2a 70 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43   *p;..      /* C
274a0 68 65 63 6b 20 69 66 20 61 6e 79 20 63 68 69 6c  heck if any chil
274a1 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72  d key columns ar
274a2 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  e being modified
274a3 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 70  . */.      for(p
274a4 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b  =pTab->pFKey; p;
274a5 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29   p=p->pNextFrom)
274a6 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
274a7 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
274a8 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  +){.          in
274a9 74 20 69 43 68 69 6c 64 4b 65 79 20 3d 20 70 2d  t iChildKey = p-
274aa 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a  >aCol[i].iFrom;.
274ab 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 43            if( aC
274ac 68 61 6e 67 65 5b 69 43 68 69 6c 64 4b 65 79 5d  hange[iChildKey]
274ad 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  >=0 ) return 1;.
274ae 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43            if( iC
274af 68 69 6c 64 4b 65 79 3d 3d 70 54 61 62 2d 3e 69  hildKey==pTab->i
274b0 50 4b 65 79 20 26 26 20 63 68 6e 67 52 6f 77 69  PKey && chngRowi
274b1 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  d ) return 1;.  
274b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
274b3 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
274b4 69 66 20 61 6e 79 20 70 61 72 65 6e 74 20 6b 65  if any parent ke
274b5 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62 65  y columns are be
274b6 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 2a 2f  ing modified. */
274b7 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  .      for(p=sql
274b8 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
274b9 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e  (pTab); p; p=p->
274ba 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20 20  pNextTo){.      
274bb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
274bc 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
274bd 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79        char *zKey
274be 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43   = p->aCol[i].zC
274bf 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  ol;.          in
274c0 74 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  t iKey;.        
274c1 20 20 66 6f 72 28 69 4b 65 79 3d 30 3b 20 69 4b    for(iKey=0; iK
274c2 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69  ey<pTab->nCol; i
274c3 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Key++){.        
274c4 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c      Column *pCol
274c5 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = &pTab->aCol[i
274c6 4b 65 79 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  Key];.          
274c7 20 20 69 66 28 20 28 7a 4b 65 79 20 3f 20 21 73    if( (zKey ? !s
274c8 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
274c9 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29  ol->zName, zKey)
274ca 20 3a 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b   : pCol->isPrimK
274cb 65 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ey) ){.         
274cc 20 20 20 20 20 69 66 28 20 61 43 68 61 6e 67 65       if( aChange
274cd 5b 69 4b 65 79 5d 3e 3d 30 20 29 20 72 65 74 75  [iKey]>=0 ) retu
274ce 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 1;.          
274cf 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d 70 54      if( iKey==pT
274d0 61 62 2d 3e 69 50 4b 65 79 20 26 26 20 63 68 6e  ab->iPKey && chn
274d1 67 52 6f 77 69 64 20 29 20 72 65 74 75 72 6e 20  gRowid ) return 
274d2 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
274d3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
274d4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
274d5 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
274d6 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n 0;.}../*.** Th
274d7 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
274d8 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 55 50  alled when an UP
274d9 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 6f  DATE or DELETE o
274da 70 65 72 61 74 69 6f 6e 20 69 73 20 62 65 69 6e  peration is bein
274db 67 20 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 6f  g .** compiled o
274dc 6e 20 74 61 62 6c 65 20 70 54 61 62 2c 20 77 68  n table pTab, wh
274dd 69 63 68 20 69 73 20 74 68 65 20 70 61 72 65 6e  ich is the paren
274de 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69  t table of forei
274df 67 6e 2d 6b 65 79 20 70 46 4b 65 79 2e 0a 2a 2a  gn-key pFKey..**
274e0 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   If the current 
274e1 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
274e2 55 50 44 41 54 45 2c 20 74 68 65 6e 20 74 68 65  UPDATE, then the
274e3 20 70 43 68 61 6e 67 65 73 20 70 61 72 61 6d 65   pChanges parame
274e4 74 65 72 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ter is.** passed
274e5 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
274e6 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  e list of column
274e7 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  s being modified
274e8 2e 20 49 66 20 69 74 20 69 73 20 61 0a 2a 2a 20  . If it is a.** 
274e9 44 45 4c 45 54 45 2c 20 70 43 68 61 6e 67 65 73  DELETE, pChanges
274ea 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 4c   is passed a NUL
274eb 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
274ec 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f   It returns a po
274ed 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67  inter to a Trigg
274ee 65 72 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  er structure con
274ef 74 61 69 6e 69 6e 67 20 61 20 74 72 69 67 67 65  taining a trigge
274f0 72 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  r.** equivalent 
274f1 74 6f 20 74 68 65 20 4f 4e 20 55 50 44 41 54 45  to the ON UPDATE
274f2 20 6f 72 20 4f 4e 20 44 45 4c 45 54 45 20 61 63   or ON DELETE ac
274f3 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62  tion specified b
274f4 79 20 70 46 4b 65 79 2e 0a 2a 2a 20 49 66 20 74  y pFKey..** If t
274f5 68 65 20 61 63 74 69 6f 6e 20 69 73 20 22 4e 4f  he action is "NO
274f6 20 41 43 54 49 4f 4e 22 20 6f 72 20 22 52 45 53   ACTION" or "RES
274f7 54 52 49 43 54 22 2c 20 74 68 65 6e 20 61 20 4e  TRICT", then a N
274f8 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a  ULL pointer is.*
274f9 2a 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 73  * returned (thes
274fa 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72  e actions requir
274fb 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 68 61 6e  e no special han
274fc 64 6c 69 6e 67 20 62 79 20 74 68 65 20 74 72 69  dling by the tri
274fd 67 67 65 72 73 0a 2a 2a 20 73 75 62 2d 73 79 73  ggers.** sub-sys
274fe 74 65 6d 2c 20 63 6f 64 65 20 66 6f 72 20 74 68  tem, code for th
274ff 65 6d 20 69 73 20 63 72 65 61 74 65 64 20 62 79  em is created by
27500 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28   fkScanChildren(
27501 29 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ))..**.** For ex
27502 61 6d 70 6c 65 2c 20 69 66 20 70 46 4b 65 79 20  ample, if pFKey 
27503 69 73 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  is the foreign k
27504 65 79 20 61 6e 64 20 70 54 61 62 20 69 73 20 74  ey and pTab is t
27505 61 62 6c 65 20 22 70 22 20 69 6e 20 0a 2a 2a 20  able "p" in .** 
27506 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63  the following sc
27507 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52  hema:.**.**   CR
27508 45 41 54 45 20 54 41 42 4c 45 20 70 28 70 6b 20  EATE TABLE p(pk 
27509 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 2a 2a  PRIMARY KEY);.**
2750a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2750b 63 28 63 6b 20 52 45 46 45 52 45 4e 43 45 53 20  c(ck REFERENCES 
2750c 70 20 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43  p ON DELETE CASC
2750d 41 44 45 29 3b 0a 2a 2a 0a 2a 2a 20 74 68 65 6e  ADE);.**.** then
2750e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 74 72   the returned tr
2750f 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20  igger structure 
27510 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
27511 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45  :.**.**   CREATE
27512 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 44 45 4c   TRIGGER ... DEL
27513 45 54 45 20 4f 4e 20 70 20 42 45 47 49 4e 0a 2a  ETE ON p BEGIN.*
27514 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  *     DELETE FRO
27515 4d 20 63 20 57 48 45 52 45 20 63 6b 20 3d 20 6f  M c WHERE ck = o
27516 6c 64 2e 70 6b 3b 0a 2a 2a 20 20 20 45 4e 44 3b  ld.pk;.**   END;
27517 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
27518 6e 65 64 20 70 6f 69 6e 74 65 72 20 69 73 20 63  ned pointer is c
27519 61 63 68 65 64 20 61 73 20 70 61 72 74 20 6f 66  ached as part of
2751a 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
2751b 20 6f 62 6a 65 63 74 2e 20 49 74 0a 2a 2a 20 69   object. It.** i
2751c 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  s eventually fre
2751d 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ed along with th
2751e 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 66 6f  e rest of the fo
2751f 72 65 69 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74  reign key object
27520 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 46   by .** sqlite3F
27521 6b 44 65 6c 65 74 65 28 29 2e 0a 2a 2f 0a 73 74  kDelete()..*/.st
27522 61 74 69 63 20 54 72 69 67 67 65 72 20 2a 66 6b  atic Trigger *fk
27523 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28 0a 20  ActionTrigger(. 
27524 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27526 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
27527 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
27528 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
27529 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
2752a 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 6f 72  being updated or
2752b 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f   deleted from */
2752c 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20  .  FKey *pFKey, 
2752d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2752e 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65     /* Foreign ke
2752f 79 20 74 6f 20 67 65 74 20 61 63 74 69 6f 6e 20  y to get action 
27530 66 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  for */.  ExprLis
27531 74 20 2a 70 43 68 61 6e 67 65 73 20 20 20 20 20  t *pChanges     
27532 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e           /* Chan
27533 67 65 2d 6c 69 73 74 20 66 6f 72 20 55 50 44 41  ge-list for UPDA
27534 54 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c  TE, NULL for DEL
27535 45 54 45 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ETE */.){.  sqli
27536 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
27537 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44  ->db;       /* D
27538 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
27539 2f 0a 20 20 69 6e 74 20 61 63 74 69 6f 6e 3b 20  /.  int action; 
2753a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2753b 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 4f 45      /* One of OE
2753c 5f 4e 6f 6e 65 2c 20 4f 45 5f 43 61 73 63 61 64  _None, OE_Cascad
2753d 65 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67  e etc. */.  Trig
2753e 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
2753f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
27540 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f  rigger definitio
27541 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  n to return */. 
27542 20 69 6e 74 20 69 41 63 74 69 6f 6e 20 3d 20 28   int iAction = (
27543 70 43 68 61 6e 67 65 73 21 3d 30 29 3b 20 20 20  pChanges!=0);   
27544 20 2f 2a 20 31 20 66 6f 72 20 55 50 44 41 54 45   /* 1 for UPDATE
27545 2c 20 30 20 66 6f 72 20 44 45 4c 45 54 45 20 2a  , 0 for DELETE *
27546 2f 0a 0a 20 20 61 63 74 69 6f 6e 20 3d 20 70 46  /..  action = pF
27547 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 69 41 63  Key->aAction[iAc
27548 74 69 6f 6e 5d 3b 0a 20 20 70 54 72 69 67 67 65  tion];.  pTrigge
27549 72 20 3d 20 70 46 4b 65 79 2d 3e 61 70 54 72 69  r = pFKey->apTri
2754a 67 67 65 72 5b 69 41 63 74 69 6f 6e 5d 3b 0a 0a  gger[iAction];..
2754b 20 20 69 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45    if( action!=OE
2754c 5f 4e 6f 6e 65 20 26 26 20 21 70 54 72 69 67 67  _None && !pTrigg
2754d 65 72 20 29 7b 0a 20 20 20 20 75 38 20 65 6e 61  er ){.    u8 ena
2754e 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20  bleLookaside;   
2754f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
27550 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  of db->lookaside
27551 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20  .bEnabled */.   
27552 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 72   char const *zFr
27553 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  om;            /
27554 2a 20 4e 61 6d 65 20 6f 66 20 63 68 69 6c 64 20  * Name of child 
27555 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
27556 20 6e 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20   nFrom;         
27557 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
27558 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66  ngth in bytes of
27559 20 7a 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 49 6e   zFrom */.    In
2755a 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 20 20  dex *pIdx = 0;  
2755b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2755c 61 72 65 6e 74 20 6b 65 79 20 69 6e 64 65 78 20  arent key index 
2755d 66 6f 72 20 74 68 69 73 20 46 4b 20 2a 2f 0a 20  for this FK */. 
2755e 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20     int *aiCol = 
2755f 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
27560 20 2f 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20   /* child table 
27561 63 6f 6c 73 20 2d 3e 20 70 61 72 65 6e 74 20 6b  cols -> parent k
27562 65 79 20 63 6f 6c 73 20 2a 2f 0a 20 20 20 20 54  ey cols */.    T
27563 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
27564 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  p = 0;        /*
27565 20 46 69 72 73 74 20 28 6f 6e 6c 79 29 20 73 74   First (only) st
27566 65 70 20 6f 66 20 74 72 69 67 67 65 72 20 70 72  ep of trigger pr
27567 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 45 78 70  ogram */.    Exp
27568 72 20 2a 70 57 68 65 72 65 20 3d 20 30 3b 20 20  r *pWhere = 0;  
27569 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48             /* WH
2756a 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 72  ERE clause of tr
2756b 69 67 67 65 72 20 73 74 65 70 20 2a 2f 0a 20 20  igger step */.  
2756c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2756d 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2756e 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20  /* Changes list 
2756f 69 66 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  if ON UPDATE CAS
27570 43 41 44 45 20 2a 2f 0a 20 20 20 20 53 65 6c 65  CADE */.    Sele
27571 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b  ct *pSelect = 0;
27572 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
27573 52 45 53 54 52 49 43 54 2c 20 22 53 45 4c 45 43  RESTRICT, "SELEC
27574 54 20 52 41 49 53 45 28 2e 2e 2e 29 22 20 2a 2f  T RAISE(...)" */
27575 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
27576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27577 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
27578 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45  ariable */.    E
27579 78 70 72 20 2a 70 57 68 65 6e 20 3d 20 30 3b 20  xpr *pWhen = 0; 
2757a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2757b 57 48 45 4e 20 63 6c 61 75 73 65 20 66 6f 72 20  WHEN clause for 
2757c 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 0a  the trigger */..
2757d 20 20 20 20 69 66 28 20 6c 6f 63 61 74 65 46 6b      if( locateFk
2757e 65 79 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  eyIndex(pParse, 
2757f 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 26 70 49  pTab, pFKey, &pI
27580 64 78 2c 20 26 61 69 43 6f 6c 29 20 29 20 72 65  dx, &aiCol) ) re
27581 74 75 72 6e 20 30 3b 0a 20 20 20 20 61 73 73 65  turn 0;.    asse
27582 72 74 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b  rt( aiCol || pFK
27583 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a  ey->nCol==1 );..
27584 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
27585 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  FKey->nCol; i++)
27586 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 4f  {.      Token tO
27587 6c 64 20 3d 20 7b 20 22 6f 6c 64 22 2c 20 33 20  ld = { "old", 3 
27588 7d 3b 20 20 2f 2a 20 4c 69 74 65 72 61 6c 20 22  };  /* Literal "
27589 6f 6c 64 22 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  old" token */.  
2758a 20 20 20 20 54 6f 6b 65 6e 20 74 4e 65 77 20 3d      Token tNew =
2758b 20 7b 20 22 6e 65 77 22 2c 20 33 20 7d 3b 20 20   { "new", 3 };  
2758c 2f 2a 20 4c 69 74 65 72 61 6c 20 22 6e 65 77 22  /* Literal "new"
2758d 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20   token */.      
2758e 54 6f 6b 65 6e 20 74 46 72 6f 6d 43 6f 6c 3b 20  Token tFromCol; 
2758f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
27590 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  ame of column in
27591 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a   child table */.
27592 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 54 6f 43        Token tToC
27593 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
27594 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c    /* Name of col
27595 75 6d 6e 20 69 6e 20 70 61 72 65 6e 74 20 74 61  umn in parent ta
27596 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ble */.      int
27597 20 69 46 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20   iFromCol;      
27598 20 20 20 20 20 20 20 20 20 2f 2a 20 49 64 78 20           /* Idx 
27599 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69  of column in chi
2759a 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ld table */.    
2759b 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20 20 20    Expr *pEq;    
2759c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2759d 20 74 46 72 6f 6d 43 6f 6c 20 3d 20 4f 4c 44 2e   tFromCol = OLD.
2759e 74 54 6f 43 6f 6c 20 2a 2f 0a 0a 20 20 20 20 20  tToCol */..     
2759f 20 69 46 72 6f 6d 43 6f 6c 20 3d 20 61 69 43 6f   iFromCol = aiCo
275a0 6c 20 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a 20 70  l ? aiCol[i] : p
275a1 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46  FKey->aCol[0].iF
275a2 72 6f 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  rom;.      asser
275a3 74 28 20 69 46 72 6f 6d 43 6f 6c 3e 3d 30 20 29  t( iFromCol>=0 )
275a4 3b 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c 2e 7a  ;.      tToCol.z
275a5 20 3d 20 70 49 64 78 20 3f 20 70 54 61 62 2d 3e   = pIdx ? pTab->
275a6 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  aCol[pIdx->aiCol
275a7 75 6d 6e 5b 69 5d 5d 2e 7a 4e 61 6d 65 20 3a 20  umn[i]].zName : 
275a8 22 6f 69 64 22 3b 0a 20 20 20 20 20 20 74 46 72  "oid";.      tFr
275a9 6f 6d 43 6f 6c 2e 7a 20 3d 20 70 46 4b 65 79 2d  omCol.z = pFKey-
275aa 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72  >pFrom->aCol[iFr
275ab 6f 6d 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20  omCol].zName;.. 
275ac 20 20 20 20 20 74 54 6f 43 6f 6c 2e 6e 20 3d 20       tToCol.n = 
275ad 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
275ae 74 54 6f 43 6f 6c 2e 7a 29 3b 0a 20 20 20 20 20  tToCol.z);.     
275af 20 74 46 72 6f 6d 43 6f 6c 2e 6e 20 3d 20 73 71   tFromCol.n = sq
275b0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 74 46  lite3Strlen30(tF
275b1 72 6f 6d 43 6f 6c 2e 7a 29 3b 0a 0a 20 20 20 20  romCol.z);..    
275b2 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
275b3 65 78 70 72 65 73 73 69 6f 6e 20 22 4f 4c 44 2e  expression "OLD.
275b4 7a 54 6f 43 6f 6c 20 3d 20 7a 46 72 6f 6d 43 6f  zToCol = zFromCo
275b5 6c 22 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74  l". It is import
275b6 61 6e 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  ant.      ** tha
275b7 74 20 74 68 65 20 22 4f 4c 44 2e 7a 54 6f 43 6f  t the "OLD.zToCo
275b8 6c 22 20 74 65 72 6d 20 69 73 20 6f 6e 20 74 68  l" term is on th
275b9 65 20 4c 48 53 20 6f 66 20 74 68 65 20 3d 20 6f  e LHS of the = o
275ba 70 65 72 61 74 6f 72 2c 20 73 6f 0a 20 20 20 20  perator, so.    
275bb 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 61 66    ** that the af
275bc 66 69 6e 69 74 79 20 61 6e 64 20 63 6f 6c 6c 61  finity and colla
275bd 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 73  tion sequence as
275be 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
275bf 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e  e.      ** paren
275c0 74 20 74 61 62 6c 65 20 61 72 65 20 75 73 65 64  t table are used
275c1 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69   for the compari
275c2 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45  son. */.      pE
275c3 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  q = sqlite3PExpr
275c4 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 0a  (pParse, TK_EQ,.
275c5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
275c6 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
275c7 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20  K_DOT, .        
275c8 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72      sqlite3PExpr
275c9 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
275ca 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20  0, 0, &tOld),.  
275cb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
275cc 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
275cd 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f  K_ID, 0, 0, &tTo
275ce 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 20 2c  Col).          ,
275cf 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73   0),.          s
275d0 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
275d1 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
275d2 20 26 74 46 72 6f 6d 43 6f 6c 29 0a 20 20 20 20   &tFromCol).    
275d3 20 20 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 57    , 0);.      pW
275d4 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
275d5 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65 72 65  prAnd(db, pWhere
275d6 2c 20 70 45 71 29 3b 0a 0a 20 20 20 20 20 20 2f  , pEq);..      /
275d7 2a 20 46 6f 72 20 4f 4e 20 55 50 44 41 54 45 2c  * For ON UPDATE,
275d8 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6e   construct the n
275d9 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ext term of the 
275da 57 48 45 4e 20 63 6c 61 75 73 65 2e 0a 20 20 20  WHEN clause..   
275db 20 20 20 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20     ** The final 
275dc 57 48 45 4e 20 63 6c 61 75 73 65 20 77 69 6c 6c  WHEN clause will
275dd 20 62 65 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20   be like this:. 
275de 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
275df 20 20 20 20 57 48 45 4e 20 4e 4f 54 28 6f 6c 64      WHEN NOT(old
275e0 2e 63 6f 6c 31 20 49 53 20 6e 65 77 2e 63 6f 6c  .col1 IS new.col
275e1 31 20 41 4e 44 20 2e 2e 2e 20 41 4e 44 20 6f 6c  1 AND ... AND ol
275e2 64 2e 63 6f 6c 4e 20 49 53 20 6e 65 77 2e 63 6f  d.colN IS new.co
275e3 6c 4e 29 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  lN).      */.   
275e4 20 20 20 69 66 28 20 70 43 68 61 6e 67 65 73 20     if( pChanges 
275e5 29 7b 0a 20 20 20 20 20 20 20 20 70 45 71 20 3d  ){.        pEq =
275e6 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
275e7 61 72 73 65 2c 20 54 4b 5f 49 53 2c 0a 20 20 20  arse, TK_IS,.   
275e8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
275e9 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
275ea 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20  _DOT, .         
275eb 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
275ec 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
275ed 20 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20   0, 0, &tOld),. 
275ee 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
275ef 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
275f0 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
275f1 74 54 6f 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20  tToCol),.       
275f2 20 20 20 20 20 20 20 30 29 2c 0a 20 20 20 20 20         0),.     
275f3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
275f4 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
275f5 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  OT, .           
275f6 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28     sqlite3PExpr(
275f7 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
275f8 2c 20 30 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20  , 0, &tNew),.   
275f9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
275fa 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
275fb 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54  TK_ID, 0, 0, &tT
275fc 6f 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20 20 20  oCol),.         
275fd 20 20 20 20 20 30 29 2c 0a 20 20 20 20 20 20 20       0),.       
275fe 20 20 20 20 20 30 29 3b 0a 20 20 20 20 20 20 20       0);.       
275ff 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33   pWhen = sqlite3
27600 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65  ExprAnd(db, pWhe
27601 6e 2c 20 70 45 71 29 3b 0a 20 20 20 20 20 20 7d  n, pEq);.      }
27602 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 61 63  .  .      if( ac
27603 74 69 6f 6e 21 3d 4f 45 5f 52 65 73 74 72 69 63  tion!=OE_Restric
27604 74 20 26 26 20 28 61 63 74 69 6f 6e 21 3d 4f 45  t && (action!=OE
27605 5f 43 61 73 63 61 64 65 20 7c 7c 20 70 43 68 61  _Cascade || pCha
27606 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 20  nges) ){.       
27607 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20   Expr *pNew;.   
27608 20 20 20 20 20 69 66 28 20 61 63 74 69 6f 6e 3d       if( action=
27609 3d 4f 45 5f 43 61 73 63 61 64 65 20 29 7b 0a 20  =OE_Cascade ){. 
2760a 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20           pNew = 
2760b 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2760c 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20  rse, TK_DOT, .  
2760d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2760e 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2760f 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4e 65  K_ID, 0, 0, &tNe
27610 77 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  w),.            
27611 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
27612 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
27613 2c 20 26 74 54 6f 43 6f 6c 29 0a 20 20 20 20 20  , &tToCol).     
27614 20 20 20 20 20 2c 20 30 29 3b 0a 20 20 20 20 20       , 0);.     
27615 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 63 74     }else if( act
27616 69 6f 6e 3d 3d 4f 45 5f 53 65 74 44 66 6c 74 20  ion==OE_SetDflt 
27617 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  ){.          Exp
27618 72 20 2a 70 44 66 6c 74 20 3d 20 70 46 4b 65 79  r *pDflt = pFKey
27619 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46  ->pFrom->aCol[iF
2761a 72 6f 6d 43 6f 6c 5d 2e 70 44 66 6c 74 3b 0a 20  romCol].pDflt;. 
2761b 20 20 20 20 20 20 20 20 20 69 66 28 20 70 44 66           if( pDf
2761c 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  lt ){.          
2761d 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2761e 45 78 70 72 44 75 70 28 64 62 2c 20 70 44 66 6c  ExprDup(db, pDfl
2761f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  t, 0);.         
27620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27621 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
27622 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
27623 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30  TK_NULL, 0, 0, 0
27624 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
27625 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
27626 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73          pNew = s
27627 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
27628 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20  se, TK_NULL, 0, 
27629 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 0);.        }
2762a 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d  .        pList =
2762b 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2762c 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
2762d 4c 69 73 74 2c 20 70 4e 65 77 29 3b 0a 20 20 20  List, pNew);.   
2762e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2762f 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72  ListSetName(pPar
27630 73 65 2c 20 70 4c 69 73 74 2c 20 26 74 46 72 6f  se, pList, &tFro
27631 6d 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  mCol, 0);.      
27632 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
27633 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 69  te3DbFree(db, ai
27634 43 6f 6c 29 3b 0a 0a 20 20 20 20 7a 46 72 6f 6d  Col);..    zFrom
27635 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d   = pFKey->pFrom-
27636 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 46 72 6f  >zName;.    nFro
27637 6d 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  m = sqlite3Strle
27638 6e 33 30 28 7a 46 72 6f 6d 29 3b 0a 0a 20 20 20  n30(zFrom);..   
27639 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f   if( action==OE_
2763a 52 65 73 74 72 69 63 74 20 29 7b 0a 20 20 20 20  Restrict ){.    
2763b 20 20 54 6f 6b 65 6e 20 74 46 72 6f 6d 3b 0a 20    Token tFrom;. 
2763c 20 20 20 20 20 45 78 70 72 20 2a 70 52 61 69 73       Expr *pRais
2763d 65 3b 20 0a 0a 20 20 20 20 20 20 74 46 72 6f 6d  e; ..      tFrom
2763e 2e 7a 20 3d 20 7a 46 72 6f 6d 3b 0a 20 20 20 20  .z = zFrom;.    
2763f 20 20 74 46 72 6f 6d 2e 6e 20 3d 20 6e 46 72 6f    tFrom.n = nFro
27640 6d 3b 0a 20 20 20 20 20 20 70 52 61 69 73 65 20  m;.      pRaise 
27641 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
27642 2c 20 54 4b 5f 52 41 49 53 45 2c 20 22 66 6f 72  , TK_RAISE, "for
27643 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
27644 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20  int failed");.  
27645 20 20 20 20 69 66 28 20 70 52 61 69 73 65 20 29      if( pRaise )
27646 7b 0a 20 20 20 20 20 20 20 20 70 52 61 69 73 65  {.        pRaise
27647 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f  ->affinity = OE_
27648 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
27649 20 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 73       pSelect = s
2764a 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
2764b 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2764c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2764d 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2764e 20 30 2c 20 70 52 61 69 73 65 29 2c 0a 20 20 20   0, pRaise),.   
2764f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
27650 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
27651 30 2c 20 26 74 46 72 6f 6d 2c 20 30 29 2c 0a 20  0, &tFrom, 0),. 
27652 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 2c           pWhere,
27653 0a 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c  .          0, 0,
27654 20 30 2c 20 30 2c 20 30 2c 20 30 0a 20 20 20 20   0, 0, 0, 0.    
27655 20 20 29 3b 0a 20 20 20 20 20 20 70 57 68 65 72    );.      pWher
27656 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  e = 0;.    }..  
27657 20 20 2f 2a 20 49 6e 20 74 68 65 20 63 75 72 72    /* In the curr
27658 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ent implementati
27659 6f 6e 2c 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3d  on, pTab->dbMem=
2765a 3d 30 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65  =0 for all table
2765b 73 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20  s except.    ** 
2765c 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61  for temporary ta
2765d 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64 65 73  bles used to des
2765e 63 72 69 62 65 20 73 75 62 71 75 65 72 69 65 73  cribe subqueries
2765f 2e 20 20 41 6e 64 20 74 65 6d 70 6f 72 61 72 79  .  And temporary
27660 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20 64  .    ** tables d
27661 6f 20 6e 6f 74 20 68 61 76 65 20 66 6f 72 65 69  o not have forei
27662 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
27663 74 73 2e 20 20 48 65 6e 63 65 2c 20 70 54 61 62  ts.  Hence, pTab
27664 2d 3e 64 62 4d 65 6d 0a 20 20 20 20 2a 2a 20 73  ->dbMem.    ** s
27665 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
27666 30 20 74 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a  0 there..    */.
27667 20 20 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73      enableLookas
27668 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ide = db->lookas
27669 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20  ide.bEnabled;.  
2766a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2766b 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 20  bEnabled = 0;.. 
2766c 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 28 54     pTrigger = (T
2766d 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74 65 33  rigger *)sqlite3
2766e 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2766f 20 0a 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66   .        sizeof
27670 28 54 72 69 67 67 65 72 29 20 2b 20 20 20 20 20  (Trigger) +     
27671 20 20 20 20 2f 2a 20 73 74 72 75 63 74 20 54 72      /* struct Tr
27672 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  igger */.       
27673 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
27674 74 65 70 29 20 2b 20 20 20 20 20 2f 2a 20 53 69  tep) +     /* Si
27675 6e 67 6c 65 20 73 74 65 70 20 69 6e 20 74 72 69  ngle step in tri
27676 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
27677 20 20 20 20 20 20 20 20 6e 46 72 6f 6d 20 2b 20          nFrom + 
27678 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
27679 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 70    /* Space for p
2767a 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20 2a  Step->target.z *
2767b 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  /.    );.    if(
2767c 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2767d 20 20 20 70 53 74 65 70 20 3d 20 70 54 72 69 67     pStep = pTrig
2767e 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d  ger->step_list =
2767f 20 28 54 72 69 67 67 65 72 53 74 65 70 20 2a 29   (TriggerStep *)
27680 26 70 54 72 69 67 67 65 72 5b 31 5d 3b 0a 20 20  &pTrigger[1];.  
27681 20 20 20 20 70 53 74 65 70 2d 3e 74 61 72 67 65      pStep->targe
27682 74 2e 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70  t.z = (char *)&p
27683 53 74 65 70 5b 31 5d 3b 0a 20 20 20 20 20 20 70  Step[1];.      p
27684 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d  Step->target.n =
27685 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20 20 6d 65   nFrom;.      me
27686 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70 53 74  mcpy((char *)pSt
27687 65 70 2d 3e 74 61 72 67 65 74 2e 7a 2c 20 7a 46  ep->target.z, zF
27688 72 6f 6d 2c 20 6e 46 72 6f 6d 29 3b 0a 20 20 0a  rom, nFrom);.  .
27689 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70 57 68        pStep->pWh
2768a 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2768b 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
2768c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
2768d 3b 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70  ;.      pStep->p
2768e 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74  ExprList = sqlit
2768f 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
27690 2c 20 70 4c 69 73 74 2c 20 45 58 50 52 44 55 50  , pList, EXPRDUP
27691 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20  _REDUCE);.      
27692 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  pStep->pSelect =
27693 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
27694 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45  p(db, pSelect, E
27695 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
27696 20 20 20 20 20 20 69 66 28 20 70 57 68 65 6e 20        if( pWhen 
27697 29 7b 0a 20 20 20 20 20 20 20 20 70 57 68 65 6e  ){.        pWhen
27698 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
27699 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20  pParse, TK_NOT, 
2769a 70 57 68 65 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  pWhen, 0, 0);.  
2769b 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e        pTrigger->
2769c 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45  pWhen = sqlite3E
2769d 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 6e  xprDup(db, pWhen
2769e 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  , EXPRDUP_REDUCE
2769f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
276a0 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 65 6e 61 62  ..    /* Re-enab
276a1 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  le the lookaside
276a2 20 62 75 66 66 65 72 2c 20 69 66 20 69 74 20 77   buffer, if it w
276a3 61 73 20 64 69 73 61 62 6c 65 64 20 65 61 72 6c  as disabled earl
276a4 69 65 72 2e 20 2a 2f 0a 20 20 20 20 64 62 2d 3e  ier. */.    db->
276a5 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
276a6 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61  ed = enableLooka
276a7 73 69 64 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74  side;..    sqlit
276a8 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
276a9 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71   pWhere);.    sq
276aa 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
276ab 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20 20 20  db, pWhen);.    
276ac 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
276ad 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
276ae 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
276af 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
276b0 65 6c 65 63 74 29 3b 0a 20 20 20 20 69 66 28 20  elect);.    if( 
276b1 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
276b2 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 6b 54  ==1 ){.      fkT
276b3 72 69 67 67 65 72 44 65 6c 65 74 65 28 64 62 2c  riggerDelete(db,
276b4 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20   pTrigger);.    
276b5 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
276b6 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 61  }..    switch( a
276b7 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 63  ction ){.      c
276b8 61 73 65 20 4f 45 5f 52 65 73 74 72 69 63 74 3a  ase OE_Restrict:
276b9 0a 20 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e  .        pStep->
276ba 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 20  op = TK_SELECT; 
276bb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
276bc 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61        case OE_Ca
276bd 73 63 61 64 65 3a 20 0a 20 20 20 20 20 20 20 20  scade: .        
276be 69 66 28 20 21 70 43 68 61 6e 67 65 73 20 29 7b  if( !pChanges ){
276bf 20 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 65   .          pSte
276c0 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54  p->op = TK_DELET
276c1 45 3b 20 0a 20 20 20 20 20 20 20 20 20 20 62 72  E; .          br
276c2 65 61 6b 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a  eak; .        }.
276c3 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
276c4 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70         pStep->op
276c5 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20   = TK_UPDATE;.  
276c6 20 20 7d 0a 20 20 20 20 70 53 74 65 70 2d 3e 70    }.    pStep->p
276c7 54 72 69 67 20 3d 20 70 54 72 69 67 67 65 72 3b  Trig = pTrigger;
276c8 0a 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70  .    pTrigger->p
276c9 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70  Schema = pTab->p
276ca 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 54 72 69  Schema;.    pTri
276cb 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
276cc 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
276cd 3b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 70 54  ;.    pFKey->apT
276ce 72 69 67 67 65 72 5b 69 41 63 74 69 6f 6e 5d 20  rigger[iAction] 
276cf 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  = pTrigger;.    
276d0 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28  pTrigger->op = (
276d1 70 43 68 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50  pChanges ? TK_UP
276d2 44 41 54 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45  DATE : TK_DELETE
276d3 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
276d4 20 70 54 72 69 67 67 65 72 3b 0a 7d 0a 0a 2f 2a   pTrigger;.}../*
276d5 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
276d6 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
276d7 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64   deleting or upd
276d8 61 74 69 6e 67 20 61 20 72 6f 77 20 74 6f 20 69  ating a row to i
276d9 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 61 6e 79 20  mplement.** any 
276da 72 65 71 75 69 72 65 64 20 43 41 53 43 41 44 45  required CASCADE
276db 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
276dc 54 20 44 45 46 41 55 4c 54 20 61 63 74 69 6f 6e  T DEFAULT action
276dd 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
276de 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
276df 33 46 6b 41 63 74 69 6f 6e 73 28 0a 20 20 50 61  3FkActions(.  Pa
276e0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
276e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
276e2 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
276e3 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
276e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276e5 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
276e6 6e 67 20 75 70 64 61 74 65 64 20 6f 72 20 64 65  ng updated or de
276e7 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  leted from */.  
276e8 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
276e9 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
276ea 2f 2a 20 43 68 61 6e 67 65 2d 6c 69 73 74 20 66  /* Change-list f
276eb 6f 72 20 55 50 44 41 54 45 2c 20 4e 55 4c 4c 20  or UPDATE, NULL 
276ec 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20  for DELETE */.  
276ed 69 6e 74 20 72 65 67 4f 6c 64 20 20 20 20 20 20  int regOld      
276ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276ef 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 72  /* Address of ar
276f0 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f  ray containing o
276f1 6c 64 20 72 6f 77 20 2a 2f 0a 29 7b 0a 20 20 2f  ld row */.){.  /
276f2 2a 20 49 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79  * If foreign-key
276f3 20 73 75 70 70 6f 72 74 20 69 73 20 65 6e 61 62   support is enab
276f4 6c 65 64 2c 20 69 74 65 72 61 74 65 20 74 68 72  led, iterate thr
276f5 6f 75 67 68 20 61 6c 6c 20 46 4b 73 20 74 68 61  ough all FKs tha
276f6 74 20 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f  t .  ** refer to
276f7 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20   table pTab. If 
276f8 74 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 69  there is an acti
276f9 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
276fa 74 68 20 74 68 65 20 46 4b 20 0a 20 20 2a 2a 20  th the FK .  ** 
276fb 66 6f 72 20 74 68 69 73 20 6f 70 65 72 61 74 69  for this operati
276fc 6f 6e 20 28 65 69 74 68 65 72 20 75 70 64 61 74  on (either updat
276fd 65 20 6f 72 20 64 65 6c 65 74 65 29 2c 20 69 6e  e or delete), in
276fe 76 6f 6b 65 20 74 68 65 20 61 73 73 6f 63 69 61  voke the associa
276ff 74 65 64 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  ted .  ** trigge
27700 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20  r sub-program.  
27701 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  */.  if( pParse-
27702 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
27703 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
27704 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  .    FKey *pFKey
27705 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
27706 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
27707 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 66  ariable */.    f
27708 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c 69 74  or(pFKey = sqlit
27709 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70  e3FkReferences(p
2770a 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b  Tab); pFKey; pFK
2770b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54  ey=pFKey->pNextT
2770c 6f 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65  o){.      Trigge
2770d 72 20 2a 70 41 63 74 69 6f 6e 20 3d 20 66 6b 41  r *pAction = fkA
2770e 63 74 69 6f 6e 54 72 69 67 67 65 72 28 70 50 61  ctionTrigger(pPa
2770f 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b 65 79  rse, pTab, pFKey
27710 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 20  , pChanges);.   
27711 20 20 20 69 66 28 20 70 41 63 74 69 6f 6e 20 29     if( pAction )
27712 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
27713 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
27714 69 72 65 63 74 28 70 50 61 72 73 65 2c 20 70 41  irect(pParse, pA
27715 63 74 69 6f 6e 2c 20 70 54 61 62 2c 20 72 65 67  ction, pTab, reg
27716 4f 6c 64 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30  Old, OE_Abort, 0
27717 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
27718 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  .  }.}..#endif /
27719 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
2771a 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
2771b 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20  ./*.** Free all 
2771c 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
2771d 64 20 77 69 74 68 20 66 6f 72 65 69 67 6e 20 6b  d with foreign k
2771e 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 61  ey definitions a
2771f 74 74 61 63 68 65 64 20 74 6f 0a 2a 2a 20 74 61  ttached to.** ta
27720 62 6c 65 20 70 54 61 62 2e 20 52 65 6d 6f 76 65  ble pTab. Remove
27721 20 74 68 65 20 64 65 6c 65 74 65 64 20 66 6f 72   the deleted for
27722 65 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74  eign keys from t
27723 68 65 20 53 63 68 65 6d 61 2e 66 6b 65 79 48 61  he Schema.fkeyHa
27724 73 68 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  sh.** hash table
27725 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
27726 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
27727 46 6b 44 65 6c 65 74 65 28 54 61 62 6c 65 20 2a  FkDelete(Table *
27728 70 54 61 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70  pTab){.  FKey *p
27729 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  FKey;           
2772a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
2772b 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
2772c 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 3b 20  .  FKey *pNext; 
2772d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2772e 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 46     /* Copy of pF
2772f 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 2a  Key->pNextFrom *
27730 2f 0a 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70  /..  for(pFKey=p
27731 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65  Tab->pFKey; pFKe
27732 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 29 7b  y; pFKey=pNext){
27733 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  ..    /* Remove 
27734 74 68 65 20 46 4b 20 66 72 6f 6d 20 74 68 65 20  the FK from the 
27735 66 6b 65 79 48 61 73 68 20 68 61 73 68 20 74 61  fkeyHash hash ta
27736 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ble. */.    if( 
27737 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f 20 29  pFKey->pPrevTo )
27738 7b 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70  {.      pFKey->p
27739 50 72 65 76 54 6f 2d 3e 70 4e 65 78 74 54 6f 20  PrevTo->pNextTo 
2773a 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f  = pFKey->pNextTo
2773b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2773c 20 20 20 76 6f 69 64 20 2a 64 61 74 61 20 3d 20     void *data = 
2773d 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 2d 3e 70  (void *)pFKey->p
2773e 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 63 6f  NextTo;.      co
2773f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 64  nst char *z = (d
27740 61 74 61 20 3f 20 70 46 4b 65 79 2d 3e 70 4e 65  ata ? pFKey->pNe
27741 78 74 54 6f 2d 3e 7a 54 6f 20 3a 20 70 46 4b 65  xtTo->zTo : pFKe
27742 79 2d 3e 7a 54 6f 29 3b 0a 20 20 20 20 20 20 73  y->zTo);.      s
27743 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
27744 28 26 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d  (&pTab->pSchema-
27745 3e 66 6b 65 79 48 61 73 68 2c 20 7a 2c 20 73 71  >fkeyHash, z, sq
27746 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
27747 2c 20 64 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20  , data);.    }. 
27748 20 20 20 69 66 28 20 70 46 4b 65 79 2d 3e 70 4e     if( pFKey->pN
27749 65 78 74 54 6f 20 29 7b 0a 20 20 20 20 20 20 70  extTo ){.      p
2774a 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 70  FKey->pNextTo->p
2774b 50 72 65 76 54 6f 20 3d 20 70 46 4b 65 79 2d 3e  PrevTo = pFKey->
2774c 70 50 72 65 76 54 6f 3b 0a 20 20 20 20 7d 0a 0a  pPrevTo;.    }..
2774d 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e      /* Delete an
2774e 79 20 74 72 69 67 67 65 72 73 20 63 72 65 61 74  y triggers creat
2774f 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
27750 61 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 69 73  actions for this
27751 20 46 4b 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20   FK. */.#ifndef 
27752 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
27753 47 45 52 0a 20 20 20 20 66 6b 54 72 69 67 67 65  GER.    fkTrigge
27754 72 44 65 6c 65 74 65 28 70 54 61 62 2d 3e 64 62  rDelete(pTab->db
27755 4d 65 6d 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72  Mem, pFKey->apTr
27756 69 67 67 65 72 5b 30 5d 29 3b 0a 20 20 20 20 66  igger[0]);.    f
27757 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28 70  kTriggerDelete(p
27758 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b 65  Tab->dbMem, pFKe
27759 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 31 5d 29  y->apTrigger[1])
2775a 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  ;.#endif..    /*
2775b 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39   EV: R-30323-219
2775c 31 37 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20  17 Each foreign 
2775d 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  key constraint i
2775e 6e 20 53 51 4c 69 74 65 20 69 73 0a 20 20 20 20  n SQLite is.    
2775f 2a 2a 20 63 6c 61 73 73 69 66 69 65 64 20 61 73  ** classified as
27760 20 65 69 74 68 65 72 20 69 6d 6d 65 64 69 61 74   either immediat
27761 65 20 6f 72 20 64 65 66 65 72 72 65 64 2e 0a 20  e or deferred.. 
27762 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
27763 28 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  ( pFKey->isDefer
27764 72 65 64 3d 3d 30 20 7c 7c 20 70 46 4b 65 79 2d  red==0 || pFKey-
27765 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29  >isDeferred==1 )
27766 3b 0a 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  ;..    pNext = p
27767 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b  FKey->pNextFrom;
27768 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
27769 65 65 28 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20  ee(pTab->dbMem, 
2776a 70 46 4b 65 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65  pFKey);.  }.}.#e
2776b 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53  ndif /* ifndef S
2776c 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
2776d 47 4e 5f 4b 45 59 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  GN_KEY */../****
2776e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2776f 66 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fkey.c *******
27770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27771 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
27773 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
27774 20 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a   file insert.c *
27775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
27778 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
27779 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2777a 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2777b 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2777c 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2777d 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2777e 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2777f 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
27780 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
27781 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
27782 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
27783 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
27784 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
27785 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
27786 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
27787 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
27788 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
27789 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2778a 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2778b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2778c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2778d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2778e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2778f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
27790 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
27791 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
27792 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
27793 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64  arser.** to hand
27794 6c 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  le INSERT statem
27795 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ents in SQLite..
27796 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  */../*.** Genera
27797 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
27798 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 20 66  l open a table f
27799 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53  or reading..*/.S
2779a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2779b 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  id sqlite3OpenTa
2779c 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 2c  ble(.  Parse *p,
2779d 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
2779e 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
2779f 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20  s VDBE */.  int 
277a0 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a 20 54  iCur,       /* T
277a1 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
277a2 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   of the table */
277a3 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
277a4 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
277a5 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71 6c 69  se index in sqli
277a6 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20 54  te3.aDb[] */.  T
277a7 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 2f  able *pTab,    /
277a8 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62  * The table to b
277a9 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e  e opened */.  in
277aa 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20 2f 2a  t opcode      /*
277ab 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20   OP_OpenRead or 
277ac 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a  OP_OpenWrite */.
277ad 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  ){.  Vdbe *v;.  
277ae 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
277af 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ab) ) return;.  
277b0 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
277b1 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  be(p);.  assert(
277b2 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e   opcode==OP_Open
277b3 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d  Write || opcode=
277b4 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29 3b 0a  =OP_OpenRead );.
277b5 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
277b6 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61 62 2d  ck(p, iDb, pTab-
277b7 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d  >tnum, (opcode==
277b8 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f 31 3a  OP_OpenWrite)?1:
277b9 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
277ba 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
277bb 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20  dOp3(v, opcode, 
277bc 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d  iCur, pTab->tnum
277bd 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , iDb);.  sqlite
277be 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
277bf 20 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f   -1, SQLITE_INT_
277c0 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f  TO_PTR(pTab->nCo
277c1 6c 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20  l), P4_INT32);. 
277c2 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
277c3 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61   "%s", pTab->zNa
277c4 6d 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  me));.}../*.** R
277c5 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
277c6 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66  to the column af
277c7 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 61 73  finity string as
277c8 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e  sociated with in
277c9 64 65 78 0a 2a 2a 20 70 49 64 78 2e 20 41 20 63  dex.** pIdx. A c
277ca 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
277cb 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68  tring has one ch
277cc 61 72 61 63 74 65 72 20 66 6f 72 20 65 61 63 68  aracter for each
277cd 20 63 6f 6c 75 6d 6e 20 69 6e 20 0a 2a 2a 20 74   column in .** t
277ce 68 65 20 74 61 62 6c 65 2c 20 61 63 63 6f 72 64  he table, accord
277cf 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69 6e  ing to the affin
277d0 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ity of the colum
277d1 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63  n:.**.**  Charac
277d2 74 65 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  ter      Column 
277d3 61 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d  affinity.**  ---
277d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
277d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
277d6 27 61 27 20 20 20 20 20 20 20 20 20 20 20 20 54  'a'            T
277d7 45 58 54 0a 2a 2a 20 20 27 62 27 20 20 20 20 20  EXT.**  'b'     
277d8 20 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20         NONE.**  
277d9 27 63 27 20 20 20 20 20 20 20 20 20 20 20 20 4e  'c'            N
277da 55 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27 20 20  UMERIC.**  'd'  
277db 20 20 20 20 20 20 20 20 20 20 49 4e 54 45 47 45            INTEGE
277dc 52 0a 2a 2a 20 20 27 65 27 20 20 20 20 20 20 20  R.**  'e'       
277dd 20 20 20 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a 20       REAL.**.** 
277de 41 6e 20 65 78 74 72 61 20 27 62 27 20 69 73 20  An extra 'b' is 
277df 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
277e0 65 6e 64 20 6f 66 20 74 68 65 20 73 74 72 69 6e  end of the strin
277e1 67 20 74 6f 20 63 6f 76 65 72 20 74 68 65 0a 2a  g to cover the.*
277e2 2a 20 72 6f 77 69 64 20 74 68 61 74 20 61 70 70  * rowid that app
277e3 65 61 72 73 20 61 73 20 74 68 65 20 6c 61 73 74  ears as the last
277e4 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79   column in every
277e5 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 4d 65   index..**.** Me
277e6 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 62 75 66  mory for the buf
277e7 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  fer containing t
277e8 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  he column index 
277e9 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 0a  affinity string.
277ea 2a 2a 20 69 73 20 6d 61 6e 61 67 65 64 20 61 6c  ** is managed al
277eb 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65 73  ong with the res
277ec 74 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73  t of the Index s
277ed 74 72 75 63 74 75 72 65 2e 20 49 74 20 77 69 6c  tructure. It wil
277ee 6c 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64  l be.** released
277ef 20 77 68 65 6e 20 73 71 6c 69 74 65 33 44 65 6c   when sqlite3Del
277f0 65 74 65 49 6e 64 65 78 28 29 20 69 73 20 63 61  eteIndex() is ca
277f1 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
277f2 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
277f3 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78  ar *sqlite3Index
277f4 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65  AffinityStr(Vdbe
277f5 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49 64 78   *v, Index *pIdx
277f6 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78 2d 3e  ){.  if( !pIdx->
277f7 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 2f  zColAff ){.    /
277f8 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
277f9 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69   a column affini
277fa 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20  ty string for a 
277fb 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78  particular index
277fc 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 75 69   is.    ** requi
277fd 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63  red, it is alloc
277fe 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74  ated and populat
277ff 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74  ed here. It is t
27800 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a 20 20  hen stored as.  
27801 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20 6f 66    ** a member of
27802 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
27803 74 75 72 65 20 66 6f 72 20 73 75 62 73 65 71 75  ture for subsequ
27804 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a 2a 0a  ent use..    **.
27805 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d      ** The colum
27806 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
27807 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  g will eventuall
27808 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 79 0a  y be deleted by.
27809 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44 65 6c      ** sqliteDel
2780a 65 74 65 49 6e 64 65 78 28 29 20 77 68 65 6e 20  eteIndex() when 
2780b 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74  the Index struct
2780c 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63 6c  ure itself is cl
2780d 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75 70 2e  eaned.    ** up.
2780e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2780f 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  n;.    Table *pT
27810 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c  ab = pIdx->pTabl
27811 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  e;.    sqlite3 *
27812 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  db = sqlite3Vdbe
27813 44 62 28 76 29 3b 0a 20 20 20 20 70 49 64 78 2d  Db(v);.    pIdx-
27814 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72  >zColAff = (char
27815 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
27816 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32  (pIdx->nColumn+2
27817 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49 64 78  );.    if( !pIdx
27818 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20  ->zColAff ){.   
27819 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2781a 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
2781b 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
2781c 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49     for(n=0; n<pI
2781d 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b  dx->nColumn; n++
2781e 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a  ){.      pIdx->z
2781f 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62  ColAff[n] = pTab
27820 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43  ->aCol[pIdx->aiC
27821 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69  olumn[n]].affini
27822 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  ty;.    }.    pI
27823 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d  dx->zColAff[n++]
27824 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
27825 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43  NE;.    pIdx->zC
27826 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20  olAff[n] = 0;.  
27827 7d 0a 20 0a 20 20 72 65 74 75 72 6e 20 70 49 64  }. .  return pId
27828 78 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a 0a 2f  x->zColAff;.}../
27829 2a 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66 20 74  *.** Set P4 of t
2782a 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
2782b 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65   inserted opcode
2782c 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66   to a column aff
2782d 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20  inity.** string 
2782e 66 6f 72 20 74 61 62 6c 65 20 70 54 61 62 2e 20  for table pTab. 
2782f 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  A column affinit
27830 79 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65  y string has one
27831 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f   character.** fo
27832 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e  r each column in
27833 64 65 78 65 64 20 62 79 20 74 68 65 20 69 6e 64  dexed by the ind
27834 65 78 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ex, according to
27835 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
27836 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a   the.** column:.
27837 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72  **.**  Character
27838 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66        Column aff
27839 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  inity.**  ------
2783a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2783b 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27  --------.**  'a'
2783c 20 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54              TEXT
2783d 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20  .**  'b'        
2783e 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27      NONE.**  'c'
2783f 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45              NUME
27840 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20  RIC.**  'd'     
27841 20 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a         INTEGER.*
27842 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20  *  'e'          
27843 20 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45    REAL.*/.SQLITE
27844 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27845 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69  lite3TableAffini
27846 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20 54  tyStr(Vdbe *v, T
27847 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f  able *pTab){.  /
27848 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
27849 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69   a column affini
2784a 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20  ty string for a 
2784b 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65  particular table
2784c 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65  .  ** is require
2784d 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74  d, it is allocat
2784e 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64  ed and populated
2784f 20 68 65 72 65 2e 20 49 74 20 69 73 20 74 68 65   here. It is the
27850 6e 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61  n .  ** stored a
27851 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  s a member of th
27852 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
27853 65 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74  e for subsequent
27854 20 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   use..  **.  ** 
27855 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  The column affin
27856 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20  ity string will 
27857 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65  eventually be de
27858 6c 65 74 65 64 20 62 79 0a 20 20 2a 2a 20 73 71  leted by.  ** sq
27859 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
2785a 28 29 20 77 68 65 6e 20 74 68 65 20 54 61 62 6c  () when the Tabl
2785b 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  e structure itse
2785c 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 20 75 70  lf is cleaned up
2785d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54  ..  */.  if( !pT
2785e 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  ab->zColAff ){. 
2785f 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66     char *zColAff
27860 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
27861 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
27862 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b  qlite3VdbeDb(v);
27863 0a 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20  ..    zColAff = 
27864 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d  (char *)sqlite3M
27865 61 6c 6c 6f 63 28 70 54 61 62 2d 3e 6e 43 6f 6c  alloc(pTab->nCol
27866 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43  +1);.    if( !zC
27867 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64  olAff ){.      d
27868 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27869 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
2786a 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f  n;.    }..    fo
2786b 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
2786c 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2786d 20 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54   zColAff[i] = pT
2786e 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69  ab->aCol[i].affi
2786f 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nity;.    }.    
27870 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43  zColAff[pTab->nC
27871 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20  ol] = '\0';..   
27872 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d   pTab->zColAff =
27873 20 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20   zColAff;.  }.. 
27874 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
27875 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62  geP4(v, -1, pTab
27876 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d  ->zColAff, 0);.}
27877 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ../*.** Return n
27878 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 74  on-zero if the t
27879 61 62 6c 65 20 70 54 61 62 20 69 6e 20 64 61 74  able pTab in dat
2787a 61 62 61 73 65 20 69 44 62 20 6f 72 20 61 6e 79  abase iDb or any
2787b 20 6f 66 20 69 74 73 20 69 6e 64 69 63 65 73 0a   of its indices.
2787c 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
2787d 6e 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74  ned at any point
2787e 20 69 6e 20 74 68 65 20 56 44 42 45 20 70 72 6f   in the VDBE pro
2787f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67 20 61  gram beginning a
27880 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 53  t location.** iS
27881 74 61 72 74 41 64 64 72 20 74 68 72 6f 75 67 68  tartAddr through
27882 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
27883 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 69 73 20   program.  This 
27884 69 73 20 75 73 65 64 20 74 6f 20 73 65 65 20 69  is used to see i
27885 66 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e  f .** a statemen
27886 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 22  t of the form  "
27887 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69 44 62  INSERT INTO <iDb
27888 2c 20 70 54 61 62 3e 20 53 45 4c 45 43 54 20 2e  , pTab> SELECT .
27889 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75 6e 20  .." can .** run 
2788a 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 65  without using te
2788b 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 66 6f  mporary table fo
2788c 72 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  r the results of
2788d 20 74 68 65 20 53 45 4c 45 43 54 2e 20 0a 2a 2f   the SELECT. .*/
2788e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
2788f 73 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c  sTable(Parse *p,
27890 20 69 6e 74 20 69 53 74 61 72 74 41 64 64 72 2c   int iStartAddr,
27891 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 20   int iDb, Table 
27892 2a 70 54 61 62 29 7b 0a 20 20 56 64 62 65 20 2a  *pTab){.  Vdbe *
27893 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
27894 62 65 28 70 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  be(p);.  int i;.
27895 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c    int iEnd = sql
27896 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
27897 64 64 72 28 76 29 3b 0a 23 69 66 6e 64 65 66 20  ddr(v);.#ifndef 
27898 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
27899 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62 6c  UALTABLE.  VTabl
2789a 65 20 2a 70 56 54 61 62 20 3d 20 49 73 56 69 72  e *pVTab = IsVir
2789b 74 75 61 6c 28 70 54 61 62 29 20 3f 20 73 71 6c  tual(pTab) ? sql
2789c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 70 2d  ite3GetVTable(p-
2789d 3e 64 62 2c 20 70 54 61 62 29 20 3a 20 30 3b 0a  >db, pTab) : 0;.
2789e 23 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28 69 3d  #endif..  for(i=
2789f 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c 69 45  iStartAddr; i<iE
278a0 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 56 64  nd; i++){.    Vd
278a1 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69  beOp *pOp = sqli
278a2 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
278a3 69 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  i);.    assert( 
278a4 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  pOp!=0 );.    if
278a5 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
278a6 50 5f 4f 70 65 6e 52 65 61 64 20 26 26 20 70 4f  P_OpenRead && pO
278a7 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a 20 20  p->p3==iDb ){.  
278a8 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65      Index *pInde
278a9 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 6e 75  x;.      int tnu
278aa 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20  m = pOp->p2;.   
278ab 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 54 61     if( tnum==pTa
278ac 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20  b->tnum ){.     
278ad 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
278ae 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 70     }.      for(p
278af 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64  Index=pTab->pInd
278b0 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64  ex; pIndex; pInd
278b1 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74  ex=pIndex->pNext
278b2 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74  ){.        if( t
278b3 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e 75  num==pIndex->tnu
278b4 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  m ){.          r
278b5 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
278b6 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
278b7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
278b8 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
278b9 45 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f  E.    if( pOp->o
278ba 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20  pcode==OP_VOpen 
278bb 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62  && pOp->p4.pVtab
278bc 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20 20 20  ==pVTab ){.     
278bd 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
278be 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 20  .pVtab!=0 );.   
278bf 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
278c0 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20  p4type==P4_VTAB 
278c1 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
278c2 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  1;.    }.#endif.
278c3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
278c4 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
278c5 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
278c6 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  MENT./*.** Locat
278c7 65 20 6f 72 20 63 72 65 61 74 65 20 61 6e 20 41  e or create an A
278c8 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63  utoincInfo struc
278c9 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20  ture associated 
278ca 77 69 74 68 20 74 61 62 6c 65 20 70 54 61 62 0a  with table pTab.
278cb 2a 2a 20 77 68 69 63 68 20 69 73 20 69 6e 20 64  ** which is in d
278cc 61 74 61 62 61 73 65 20 69 44 62 2e 20 20 52 65  atabase iDb.  Re
278cd 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65  turn the registe
278ce 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
278cf 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 68 61   register.** tha
278d0 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61 78 69  t holds the maxi
278d1 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  mum rowid..**.**
278d2 20 54 68 65 72 65 20 69 73 20 61 74 20 6d 6f 73   There is at mos
278d3 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49 6e 66  t one AutoincInf
278d4 6f 20 73 74 72 75 63 74 75 72 65 20 70 65 72 20  o structure per 
278d5 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68  table even if th
278d6 65 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20  e.** same table 
278d7 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  is autoincrement
278d8 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
278d9 73 20 64 75 65 20 74 6f 20 69 6e 73 65 72 74 73  s due to inserts
278da 20 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69 67 67   within.** trigg
278db 65 72 73 2e 20 20 41 20 6e 65 77 20 41 75 74 6f  ers.  A new Auto
278dc 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75 72  incInfo structur
278dd 65 20 69 73 20 63 72 65 61 74 65 64 20 69 66 20  e is created if 
278de 74 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 66  this is the.** f
278df 69 72 73 74 20 75 73 65 20 6f 66 20 74 61 62 6c  irst use of tabl
278e0 65 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e 64 20  e pTab.  On 2nd 
278e1 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 75  and subsequent u
278e2 73 65 73 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ses, the origina
278e3 6c 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e 66 6f  l.** AutoincInfo
278e4 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
278e5 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65 20  ed..**.** Three 
278e6 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
278e7 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 3a 0a   are allocated:.
278e8 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52 65 67  **.**   (1)  Reg
278e9 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68  ister to hold th
278ea 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 54  e name of the pT
278eb 61 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 28  ab table..**   (
278ec 32 29 20 20 52 65 67 69 73 74 65 72 20 74 6f 20  2)  Register to 
278ed 68 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d 75 6d  hold the maximum
278ee 20 52 4f 57 49 44 20 6f 66 20 70 54 61 62 2e 0a   ROWID of pTab..
278ef 2a 2a 20 20 20 28 33 29 20 20 52 65 67 69 73 74  **   (3)  Regist
278f0 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  er to hold the r
278f1 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73  owid in sqlite_s
278f2 65 71 75 65 6e 63 65 20 6f 66 20 70 54 61 62 0a  equence of pTab.
278f3 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20 72 65  **.** The 2nd re
278f4 67 69 73 74 65 72 20 69 73 20 74 68 65 20 6f 6e  gister is the on
278f5 65 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e  e that is return
278f6 65 64 2e 20 20 54 68 61 74 20 69 73 20 61 6c 6c  ed.  That is all
278f7 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74 20 72   the.** insert r
278f8 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20  outine needs to 
278f9 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f 0a 73  know about..*/.s
278fa 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49 6e  tatic int autoIn
278fb 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20  cBegin(.  Parse 
278fc 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
278fd 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
278fe 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20   */.  int iDb,  
278ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
27900 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
27901 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20  se holding pTab 
27902 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
27903 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
27904 74 61 62 6c 65 20 77 65 20 61 72 65 20 77 72 69  table we are wri
27905 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20  ting to */.){.  
27906 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20  int memId = 0;  
27907 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
27908 68 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20  holding maximum 
27909 72 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20 70  rowid */.  if( p
2790a 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2790b 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
2790c 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70   ){.    Parse *p
2790d 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74  Toplevel = sqlit
2790e 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
2790f 70 50 61 72 73 65 29 3b 0a 20 20 20 20 41 75 74  pParse);.    Aut
27910 6f 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b  oincInfo *pInfo;
27911 0a 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 70 54  ..    pInfo = pT
27912 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 3b 0a  oplevel->pAinc;.
27913 20 20 20 20 77 68 69 6c 65 28 20 70 49 6e 66 6f      while( pInfo
27914 20 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61 62 21   && pInfo->pTab!
27915 3d 70 54 61 62 20 29 7b 20 70 49 6e 66 6f 20 3d  =pTab ){ pInfo =
27916 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b 20 7d   pInfo->pNext; }
27917 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d  .    if( pInfo==
27918 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f  0 ){.      pInfo
27919 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2791a 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62  ocRaw(pParse->db
2791b 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29  , sizeof(*pInfo)
2791c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e  );.      if( pIn
2791d 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  fo==0 ) return 0
2791e 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 70  ;.      pInfo->p
2791f 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76 65 6c  Next = pToplevel
27920 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20 20 70  ->pAinc;.      p
27921 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 20  Toplevel->pAinc 
27922 3d 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20 20 70  = pInfo;.      p
27923 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70 54 61  Info->pTab = pTa
27924 62 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  b;.      pInfo->
27925 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 20  iDb = iDb;.     
27926 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d   pToplevel->nMem
27927 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ++;             
27928 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
27929 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 6f 66   to hold name of
2792a 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20   table */.      
2792b 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20 3d 20  pInfo->regCtr = 
2792c 2b 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65  ++pToplevel->nMe
2792d 6d 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77 69 64  m;  /* Max rowid
2792e 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20   register */.   
2792f 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d     pToplevel->nM
27930 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20  em++;           
27931 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
27932 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  in sqlite_sequen
27933 63 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ce */.    }.    
27934 6d 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d 3e 72  memId = pInfo->r
27935 65 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72 65 74  egCtr;.  }.  ret
27936 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a  urn memId;.}../*
27937 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
27938 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
27939 74 68 61 74 20 77 69 6c 6c 20 69 6e 69 74 69 61  that will initia
2793a 6c 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a  lize all of the.
2793b 2a 2a 20 72 65 67 69 73 74 65 72 20 75 73 65 64  ** register used
2793c 20 62 79 20 74 68 65 20 61 75 74 6f 69 6e 63 72   by the autoincr
2793d 65 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e 20 20  ement tracker.  
2793e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2793f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
27940 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69  utoincrementBegi
27941 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  n(Parse *pParse)
27942 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  {.  AutoincInfo 
27943 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p;            /
27944 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
27945 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43 52 45  out an AUTOINCRE
27946 4d 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69 74 65  MENT */.  sqlite
27947 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
27948 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61  db;  /* The data
27949 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2794a 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  */.  Db *pDb;   
2794b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2794c 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 6e 6c 79  /* Database only
2794d 20 61 75 74 6f 69 6e 63 20 74 61 62 6c 65 20 2a   autoinc table *
2794e 2f 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b 20 20  /.  int memId;  
2794f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27950 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
27951 6e 67 20 6d 61 78 20 72 6f 77 69 64 20 2a 2f 0a  ng max rowid */.
27952 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20    int addr;     
27953 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27954 41 20 56 44 42 45 20 61 64 64 72 65 73 73 20 2a  A VDBE address *
27955 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  /.  Vdbe *v = pP
27956 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f  arse->pVdbe;   /
27957 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e  * VDBE under con
27958 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  struction */..  
27959 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
2795a 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
2795b 64 75 72 69 6e 67 20 74 72 69 67 67 65 72 2d 67  during trigger-g
2795c 65 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74 20 69  eneration.  It i
2795d 73 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61 6c 6c  s.  ** only call
2795e 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 2d  ed from the top-
2795f 6c 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73 65 72  level */.  asser
27960 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67  t( pParse->pTrig
27961 67 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20 20 61  gerTab==0 );.  a
27962 73 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d 73  ssert( pParse==s
27963 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
27964 76 65 6c 28 70 50 61 72 73 65 29 20 29 3b 0a 0a  vel(pParse) );..
27965 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 20 20    assert( v );  
27966 20 2f 2a 20 57 65 20 66 61 69 6c 65 64 20 6c 6f   /* We failed lo
27967 6e 67 20 61 67 6f 20 69 66 20 74 68 69 73 20 69  ng ago if this i
27968 73 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20 66 6f  s not so */.  fo
27969 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41  r(p = pParse->pA
2796a 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70  inc; p; p = p->p
2796b 4e 65 78 74 29 7b 0a 20 20 20 20 70 44 62 20 3d  Next){.    pDb =
2796c 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62   &db->aDb[p->iDb
2796d 5d 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d 20 70  ];.    memId = p
2796e 2d 3e 72 65 67 43 74 72 3b 0a 20 20 20 20 73 71  ->regCtr;.    sq
2796f 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
27970 50 61 72 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62  Parse, 0, p->iDb
27971 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
27972 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  pSeqTab, OP_Open
27973 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20  Read);.    addr 
27974 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
27975 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
27976 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27977 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
27978 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c  , 0, memId-1, 0,
27979 20 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   p->pTab->zName,
2797a 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2797b 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2797c 5f 52 65 77 69 6e 64 2c 20 30 2c 20 61 64 64 72  _Rewind, 0, addr
2797d 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +9);.    sqlite3
2797e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2797f 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 6d  _Column, 0, 0, m
27980 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
27981 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27982 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20  OP_Ne, memId-1, 
27983 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a  addr+7, memId);.
27984 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
27985 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
27986 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
27987 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27988 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
27989 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20  , 0, memId+1);. 
2798a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2798b 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2798c 6e 2c 20 30 2c 20 31 2c 20 6d 65 6d 49 64 29 3b  n, 0, 1, memId);
2798d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2798e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2798f 6f 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b 0a 20  o, 0, addr+9);. 
27990 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27991 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
27992 20 30 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 20   0, addr+2);.   
27993 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27994 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27995 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20  , 0, memId);.   
27996 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27997 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b  p0(v, OP_Close);
27998 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70  .  }.}../*.** Up
27999 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  date the maximum
2799a 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61 75   rowid for an au
2799b 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63  toincrement calc
2799c 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ulation..**.** T
2799d 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
2799e 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  ld be called whe
2799f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
279a0 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a   stack holds a.*
279a1 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61 74  * new rowid that
279a2 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20   is about to be 
279a3 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68  inserted.  If th
279a4 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73 0a  at new rowid is.
279a5 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ** larger than t
279a6 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
279a7 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d 65   in the memId me
279a8 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20  mory cell, then 
279a9 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65  the.** memory ce
279aa 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20 20  ll is updated.  
279ab 54 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e 63  The stack is unc
279ac 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  hanged..*/.stati
279ad 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53 74  c void autoIncSt
279ae 65 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ep(Parse *pParse
279af 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74  , int memId, int
279b0 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69 66   regRowid){.  if
279b1 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20  ( memId>0 ){.   
279b2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
279b3 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p2(pParse->pVdbe
279b4 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d  , OP_MemMax, mem
279b5 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  Id, regRowid);. 
279b6 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
279b7 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
279b8 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64  es the code need
279b9 65 64 20 74 6f 20 77 72 69 74 65 20 61 75 74 6f  ed to write auto
279ba 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 6d 61 78  increment.** max
279bb 69 6d 75 6d 20 72 6f 77 69 64 20 76 61 6c 75 65  imum rowid value
279bc 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  s back into the 
279bd 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
279be 72 65 67 69 73 74 65 72 2e 0a 2a 2a 20 45 76 65  register..** Eve
279bf 72 79 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  ry statement tha
279c0 74 20 6d 69 67 68 74 20 64 6f 20 61 6e 20 49 4e  t might do an IN
279c1 53 45 52 54 20 69 6e 74 6f 20 61 6e 20 61 75 74  SERT into an aut
279c2 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 74 61  oincrement.** ta
279c3 62 6c 65 20 28 65 69 74 68 65 72 20 64 69 72 65  ble (either dire
279c4 63 74 6c 79 20 6f 72 20 74 68 72 6f 75 67 68 20  ctly or through 
279c5 74 72 69 67 67 65 72 73 29 20 6e 65 65 64 73 20  triggers) needs 
279c6 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20  to call this.** 
279c7 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 62 65 66  routine just bef
279c8 6f 72 65 20 74 68 65 20 22 65 78 69 74 22 20 63  ore the "exit" c
279c9 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ode..*/.SQLITE_P
279ca 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
279cb 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
279cc 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73  End(Parse *pPars
279cd 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  e){.  AutoincInf
279ce 6f 20 2a 70 3b 0a 20 20 56 64 62 65 20 2a 76 20  o *p;.  Vdbe *v 
279cf 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
279d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
279d1 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
279d2 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 66  assert( v );.  f
279d3 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  or(p = pParse->p
279d4 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e  Ainc; p; p = p->
279d5 70 4e 65 78 74 29 7b 0a 20 20 20 20 44 62 20 2a  pNext){.    Db *
279d6 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70  pDb = &db->aDb[p
279d7 2d 3e 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20  ->iDb];.    int 
279d8 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20  j1, j2, j3, j4, 
279d9 6a 35 3b 0a 20 20 20 20 69 6e 74 20 69 52 65 63  j5;.    int iRec
279da 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 49 64 20  ;.    int memId 
279db 3d 20 70 2d 3e 72 65 67 43 74 72 3b 0a 0a 20 20  = p->regCtr;..  
279dc 20 20 69 52 65 63 20 3d 20 73 71 6c 69 74 65 33    iRec = sqlite3
279dd 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
279de 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  e);.    sqlite3O
279df 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
279e0 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d   0, p->iDb, pDb-
279e1 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  >pSchema->pSeqTa
279e2 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  b, OP_OpenWrite)
279e3 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
279e4 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
279e5 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65 6d 49  OP_NotNull, memI
279e6 64 2b 31 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73  d+1);.    j2 = s
279e7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
279e8 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 29 3b 0a  (v, OP_Rewind);.
279e9 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33      j3 = sqlite3
279ea 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
279eb 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 69  _Column, 0, 0, i
279ec 52 65 63 29 3b 0a 20 20 20 20 6a 34 20 3d 20 73  Rec);.    j4 = s
279ed 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
279ee 28 76 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d 49 64  (v, OP_Eq, memId
279ef 2d 31 2c 20 30 2c 20 69 52 65 63 29 3b 0a 20 20  -1, 0, iRec);.  
279f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279f1 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
279f2 30 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69  0, j3);.    sqli
279f3 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
279f4 76 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71 6c 69  v, j2);.    sqli
279f5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
279f6 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c   OP_NewRowid, 0,
279f7 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a   memId+1);.    j
279f8 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  5 = sqlite3VdbeA
279f9 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
279fa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
279fb 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34  beJumpHere(v, j4
279fc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
279fd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
279fe 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31  owid, 0, memId+1
279ff 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27a00 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
27a01 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27a02 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35  beJumpHere(v, j5
27a03 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27a04 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
27a05 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64  akeRecord, memId
27a06 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20  -1, 2, iRec);.  
27a07 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27a08 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
27a09 2c 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d 49 64  , 0, iRec, memId
27a0a 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
27a0b 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
27a0c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
27a0d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27a0e 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp0(v, OP_Clos
27a0f 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  e);.    sqlite3R
27a10 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
27a11 61 72 73 65 2c 20 69 52 65 63 29 3b 0a 20 20 7d  arse, iRec);.  }
27a12 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49  .}.#else./*.** I
27a13 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
27a14 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64  TOINCREMENT is d
27a15 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
27a16 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a   three routines.
27a17 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c  ** above are all
27a18 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66   no-ops.*/.# def
27a19 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  ine autoIncBegin
27a1a 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65  (A,B,C) (0).# de
27a1b 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70  fine autoIncStep
27a1c 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 20 2f  (A,B,C).#endif /
27a1d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
27a1e 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a  TOINCREMENT */..
27a1f 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
27a20 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  aration */.stati
27a21 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69  c int xferOptimi
27a22 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  zation(.  Parse 
27a23 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
27a24 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
27a25 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44  t */.  Table *pD
27a26 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
27a27 54 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65  The table we are
27a28 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20   inserting into 
27a29 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
27a2a 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20  lect,      /* A 
27a2b 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
27a2c 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64   to use as the d
27a2d 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20  ata source */.  
27a2e 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20  int onError,    
27a2f 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
27a30 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
27a31 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e  t errors */.  in
27a32 74 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20  t iDbDest       
27a33 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
27a34 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a  ase of pDest */.
27a35 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  );../*.** This r
27a36 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20 74  outine is call t
27a37 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20  o handle SQL of 
27a38 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
27a39 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e  rms:.**.**    in
27a3a 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20  sert into TABLE 
27a3b 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73 28  (IDLIST) values(
27a3c 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20  EXPRLIST).**    
27a3d 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
27a3e 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63  E (IDLIST) selec
27a3f 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49  t.**.** The IDLI
27a40 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ST following the
27a41 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61   table name is a
27a42 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  lways optional. 
27a43 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20   If omitted,.** 
27a44 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 61  then a list of a
27a45 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74  ll columns for t
27a46 68 65 20 74 61 62 6c 65 20 69 73 20 73 75 62 73  he table is subs
27a47 74 69 74 75 74 65 64 2e 20 20 54 68 65 20 49 44  tituted.  The ID
27a48 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73 20  LIST.** appears 
27a49 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70  in the pColumn p
27a4a 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75  arameter.  pColu
27a4b 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49 44  mn is NULL if ID
27a4c 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64 2e  LIST is omitted.
27a4d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74  .**.** The pList
27a4e 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64 73   parameter holds
27a4f 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68 65   EXPRLIST in the
27a50 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20 74   first form of t
27a51 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61  he INSERT.** sta
27a52 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e  tement above, an
27a53 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55 4c  d pSelect is NUL
27a54 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63 6f  L.  For the seco
27a55 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69  nd form, pList i
27a56 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53  s.** NULL and pS
27a57 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e 74  elect is a point
27a58 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74  er to the select
27a59 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20   statement used 
27a5a 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64  to generate.** d
27a5b 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73 65  ata for the inse
27a5c 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  rt..**.** The co
27a5d 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 6c  de generated fol
27a5e 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72  lows one of four
27a5f 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72   templates.  For
27a60 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c   a simple.** sel
27a61 65 63 74 20 77 69 74 68 20 64 61 74 61 20 63 6f  ect with data co
27a62 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
27a63 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63  ES clause, the c
27a64 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a 20  ode executes.** 
27a65 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20 64 6f  once straight do
27a66 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73 65  wn through.  Pse
27a67 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73  udo-code follows
27a68 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a   (we call this.*
27a69 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d 70 6c  * the "1st templ
27a6a 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ate"):.**.**    
27a6b 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20       open write 
27a6c 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65  cursor to <table
27a6d 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65  > and its indice
27a6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74  s.**         put
27a6f 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20  s VALUES clause 
27a70 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f  expressions onto
27a71 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20   the stack.**   
27a72 20 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20        write the 
27a73 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64  resulting record
27a74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a   into <table>.**
27a75 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
27a76 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65  .**.** The three
27a77 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c   remaining templ
27a78 61 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20  ates assume the 
27a79 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
27a7a 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
27a7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61   INSERT INTO <ta
27a7c 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a  ble> SELECT ....
27a7d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c  **.** If the SEL
27a7e 45 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66  ECT clause is of
27a7f 20 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20   the restricted 
27a80 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46  form "SELECT * F
27a81 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a  ROM <table2>" -.
27a82 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** in other word
27a83 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  s if the SELECT 
27a84 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e  pulls all column
27a85 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20  s from a single 
27a86 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65  table.** and the
27a87 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f  re is no WHERE o
27a88 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50  r LIMIT or GROUP
27a89 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20   BY or ORDER BY 
27a8a 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20  clauses, and.** 
27a8b 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20  if <table2> and 
27a8c 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73  <table1> are dis
27a8d 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74  tinct tables but
27a8e 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a   have identical.
27a8f 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c  ** schemas, incl
27a90 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61  uding all the sa
27a91 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e  me indices, then
27a92 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d   a special optim
27a93 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e  ization.** is in
27a94 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65  voked that copie
27a95 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72  s raw records fr
27a96 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72  om <table2> over
27a97 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a   to <table1>..**
27a98 20 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74   See the xferOpt
27a99 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63  imization() func
27a9a 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70  tion for the imp
27a9b 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
27a9c 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e  his.** template.
27a9d 20 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e    This is the 2n
27a9e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a  d template..**.*
27a9f 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61  *         open a
27aa0 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f   write cursor to
27aa1 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20   <table>.**     
27aa2 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63 75      open read cu
27aa3 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e  rsor on <table2>
27aa4 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e  .**         tran
27aa5 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73  sfer all records
27aa6 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65   in <table2> ove
27aa7 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  r to <table>.** 
27aa8 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
27aa9 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
27aaa 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20 6f   foreach index o
27aab 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  n <table>.**    
27aac 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
27aad 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ite cursor on th
27aae 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a  e <table> index.
27aaf 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65  **           ope
27ab0 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20  n a read cursor 
27ab1 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
27ab2 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e  ding <table2> in
27ab3 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  dex.**          
27ab4 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65   transfer all re
27ab5 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 72  cords from the r
27ab6 65 61 64 20 74 6f 20 74 68 65 20 77 72 69 74 65  ead to the write
27ab7 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20   cursors.**     
27ab8 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73        close curs
27ab9 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  ors.**         e
27aba 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a  nd foreach.**.**
27abb 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61 74   The 3rd templat
27abc 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74 68  e is for when th
27abd 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74  e second templat
27abe 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79  e does not apply
27abf 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45  .** and the SELE
27ac0 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  CT clause does n
27ac1 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74 61  ot read from <ta
27ac2 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d 65  ble> at any time
27ac3 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74  ..** The generat
27ac4 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20  ed code follows 
27ac5 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a  this template:.*
27ac6 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  *.**         EOF
27ac7 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20   <- 0.**        
27ac8 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20   X <- A.**      
27ac9 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
27aca 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
27acb 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
27acc 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
27acd 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53  he rows in the S
27ace 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
27acf 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20 69     load values i
27ad0 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 52 2e  nto registers R.
27ad1 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  .R+n.**         
27ad2 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
27ad3 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a       end loop.**
27ad4 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
27ad5 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43   after the SELEC
27ad6 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  T.**         EOF
27ad7 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   <- 1.**        
27ad8 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
27ad9 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20      goto A.**   
27ada 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74 65     B: open write
27adb 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
27adc 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
27add 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79 69  es.**      C: yi
27ade 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
27adf 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a   if EOF goto D.*
27ae0 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74  *         insert
27ae1 20 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75   the select resu
27ae2 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20  lt into <table> 
27ae3 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20  from R..R+n.**  
27ae4 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a         goto C.**
27ae5 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70        D: cleanup
27ae6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 74  .**.** The 4th t
27ae7 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64 20  emplate is used 
27ae8 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 73 74  if the insert st
27ae9 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69 74  atement takes it
27aea 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d  s.** values from
27aeb 20 61 20 53 45 4c 45 43 54 20 62 75 74 20 74 68   a SELECT but th
27aec 65 20 64 61 74 61 20 69 73 20 62 65 69 6e 67 20  e data is being 
27aed 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 20  inserted into a 
27aee 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73  table.** that is
27aef 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70 61   also read as pa
27af0 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  rt of the SELECT
27af1 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72 64 20  .  In the third 
27af2 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65  form,.** we have
27af3 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72 6d   to use a interm
27af4 65 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f 20  ediate table to 
27af5 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
27af6 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65  s of.** the sele
27af7 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61 74  ct.  The templat
27af8 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  e is like this:.
27af9 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  **.**         EO
27afa 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20  F <- 0.**       
27afb 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20    X <- A.**     
27afc 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20      goto B.**   
27afd 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
27afe 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
27aff 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20        loop over 
27b00 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  the tables in th
27b01 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  e SELECT.**     
27b02 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65        load value
27b03 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52   into register R
27b04 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
27b05 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
27b06 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a        end loop.*
27b07 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75  *         cleanu
27b08 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45  p after the SELE
27b09 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f  CT.**         EO
27b0a 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20  F <- 1.**       
27b0b 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
27b0c 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
27b0d 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20  **      B: open 
27b0e 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
27b0f 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a     L: yield X.**
27b10 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20           if EOF 
27b11 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20  goto M.**       
27b12 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f    insert row fro
27b13 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65  m R..R+n into te
27b14 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  mp table.**     
27b15 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20      goto L.**   
27b16 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65     M: open write
27b17 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
27b18 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63  e> and its indic
27b19 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 65  es.**         re
27b1a 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a  wind temp table.
27b1b 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20  **      C: loop 
27b1c 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74  over rows of int
27b1d 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a  ermediate table.
27b1e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61  **           tra
27b1f 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72  nsfer values for
27b20 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74  m intermediate t
27b21 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65  able into <table
27b22 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64  >.**         end
27b23 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a   loop.**      D:
27b24 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49   cleanup.*/.SQLI
27b25 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
27b26 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a 20  sqlite3Insert(. 
27b27 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27b28 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
27b29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
27b2a 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
27b2b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
27b2c 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
27b2d 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
27b2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
27b2f 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c  pList,      /* L
27b30 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
27b31 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
27b32 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
27b33 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
27b34 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
27b35 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
27b36 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c   source */.  IdL
27b37 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20  ist *pColumn,   
27b38 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
27b39 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  es corresponding
27b3a 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20   to IDLIST. */. 
27b3b 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20   int onError    
27b3c 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f         /* How to
27b3d 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69   handle constrai
27b3e 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a  nt errors */.){.
27b3f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
27b40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
27b41 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
27b42 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c  ucture */.  Tabl
27b43 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
27b44 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
27b45 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20  o insert into.  
27b46 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63  aka TABLE */.  c
27b47 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
27b48 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
27b49 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  the table into w
27b4a 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
27b4b 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74  rting */.  const
27b4c 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
27b4d 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
27b4e 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
27b4f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
27b50 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20   int i, j, idx; 
27b51 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
27b52 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62  ounters */.  Vdb
27b53 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
27b54 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
27b55 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69  ode into this vi
27b56 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f  rtual machine */
27b57 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
27b58 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
27b59 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
27b5a 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
27b5b 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  e */.  int nColu
27b5c 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  mn;          /* 
27b5d 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
27b5e 73 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f  s in the data */
27b5f 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d  .  int nHidden =
27b60 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62   0;      /* Numb
27b61 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c  er of hidden col
27b62 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73  umns if TABLE is
27b63 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e   virtual */.  in
27b64 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20 20  t baseCur = 0;  
27b65 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73      /* VDBE Curs
27b66 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54  or number for pT
27b67 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43  ab */.  int keyC
27b68 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a  olumn = -1;   /*
27b69 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   Column that is 
27b6a 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d  the INTEGER PRIM
27b6b 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74  ARY KEY */.  int
27b6c 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20   endOfLoop;     
27b6d 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20     /* Label for 
27b6e 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69  the end of the i
27b6f 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f  nsertion loop */
27b70 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54 61  .  int useTempTa
27b71 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72  ble = 0; /* Stor
27b72 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73  e SELECT results
27b73 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65   in intermediate
27b74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
27b75 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20 20  srcTab = 0;     
27b76 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73 20    /* Data comes 
27b77 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f 72  from this tempor
27b78 61 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e 3d  ary cursor if >=
27b79 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 49  0 */.  int addrI
27b7a 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20  nsTop = 0;   /* 
27b7b 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22 44  Jump to label "D
27b7c 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43  " */.  int addrC
27b7d 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ont = 0;     /* 
27b7e 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f  Top of insert lo
27b7f 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69 6e  op. Label "C" in
27b80 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e 64   templates 3 and
27b81 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72   4 */.  int addr
27b82 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a  Select = 0;   /*
27b83 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72 6f   Address of coro
27b84 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65  utine that imple
27b85 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54  ments the SELECT
27b86 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
27b87 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 44   dest;      /* D
27b88 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53  estination for S
27b89 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66 20  ELECT on rhs of 
27b8a 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20  INSERT */.  int 
27b8b 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
27b8c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
27b8d 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54  tabase holding T
27b8e 41 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44  ABLE */.  Db *pD
27b8f 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
27b90 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
27b91 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
27b92 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
27b93 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70  into */.  int ap
27b94 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20  pendFlag = 0;   
27b95 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 69  /* True if the i
27b96 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20  nsert is likely 
27b97 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20  to be an append 
27b98 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65  */..  /* Registe
27b99 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  r allocations */
27b9a 0a 20 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65  .  int regFromSe
27b9b 6c 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65  lect = 0;/* Base
27b9c 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 64 61   register for da
27b9d 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53  ta coming from S
27b9e 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72  ELECT */.  int r
27b9f 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20  egAutoinc = 0;  
27ba0 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
27ba1 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43  ding the AUTOINC
27ba2 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a  REMENT counter *
27ba3 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f  /.  int regRowCo
27ba4 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d  unt = 0;  /* Mem
27ba5 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
27ba6 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65  r the row counte
27ba7 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e  r */.  int regIn
27ba8 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s;           /* 
27ba9 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f  Block of regs ho
27baa 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61  lding rowid+data
27bab 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
27bac 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69  */.  int regRowi
27bad 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65  d;         /* re
27bae 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20  gisters holding 
27baf 69 6e 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a  insert rowid */.
27bb0 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20    int regData;  
27bb1 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73          /* regis
27bb2 74 65 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73  ter holding firs
27bb3 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65  t column to inse
27bb4 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  rt */.  int regR
27bb5 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a  ecord;        /*
27bb6 20 48 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d   Holds the assem
27bb7 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64  blied row record
27bb8 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66   */.  int regEof
27bb9 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 52   = 0;       /* R
27bba 65 67 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e  egister recordin
27bbb 67 20 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20  g end of SELECT 
27bbc 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  data */.  int *a
27bbd 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20  RegIdx = 0;     
27bbe 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20  /* One register 
27bbf 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63  allocated to eac
27bc0 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 23 69 66 6e  h index */..#ifn
27bc1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27bc2 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73  TRIGGER.  int is
27bc3 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  View;           
27bc4 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
27bc5 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 69   attempting to i
27bc6 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69 65  nsert into a vie
27bc7 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  w */.  Trigger *
27bc8 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20  pTrigger;       
27bc9 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72     /* List of tr
27bca 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20  iggers on pTab, 
27bcb 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
27bcc 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20   int tmask;     
27bcd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27bce 4d 61 73 6b 20 6f 66 20 74 72 69 67 67 65 72 20  Mask of trigger 
27bcf 74 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  times */.#endif.
27bd0 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
27bd1 64 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 65  db;.  memset(&de
27bd2 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 65  st, 0, sizeof(de
27bd3 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72  st));.  if( pPar
27bd4 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
27bd5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
27bd6 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
27bd7 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
27bd8 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
27bd9 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
27bda 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74  e will be insert
27bdb 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74  ing new informat
27bdc 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ion..  */.  asse
27bdd 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
27bde 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20  rc==1 );.  zTab 
27bdf 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  = pTabList->a[0]
27be0 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 4e 45  .zName;.  if( NE
27be1 56 45 52 28 7a 54 61 62 3d 3d 30 29 20 29 20 67  VER(zTab==0) ) g
27be2 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
27be3 75 70 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  up;.  pTab = sql
27be4 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
27be5 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
27be6 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
27be7 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  =0 ){.    goto i
27be8 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
27be9 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74   }.  iDb = sqlit
27bea 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
27beb 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
27bec 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
27bed 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70  b<db->nDb );.  p
27bee 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
27bef 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d  b];.  zDb = pDb-
27bf0 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71  >zName;.  if( sq
27bf1 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
27bf2 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e  Parse, SQLITE_IN
27bf3 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  SERT, pTab->zNam
27bf4 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  e, 0, zDb) ){.  
27bf5 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
27bf6 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
27bf7 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77   Figure out if w
27bf8 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67 67  e have any trigg
27bf9 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20 74  ers and if the t
27bfa 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20  able being.  ** 
27bfb 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 73  inserted into is
27bfc 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66   a view.  */.#if
27bfd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27bfe 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72 69 67  _TRIGGER.  pTrig
27bff 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ger = sqlite3Tri
27c00 67 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73  ggersExist(pPars
27c01 65 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e 53 45  e, pTab, TK_INSE
27c02 52 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29 3b 0a  RT, 0, &tmask);.
27c03 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d    isView = pTab-
27c04 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c  >pSelect!=0;.#el
27c05 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69  se.# define pTri
27c06 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20  gger 0.# define 
27c07 74 6d 61 73 6b 20 30 0a 23 20 64 65 66 69 6e 65  tmask 0.# define
27c08 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
27c09 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
27c0a 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66  MIT_VIEW.# undef
27c0b 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65   isView.# define
27c0c 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
27c0d 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 72 69  .  assert( (pTri
27c0e 67 67 65 72 20 26 26 20 74 6d 61 73 6b 29 20 7c  gger && tmask) |
27c0f 7c 20 28 70 54 72 69 67 67 65 72 3d 3d 30 20 26  | (pTrigger==0 &
27c10 26 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b 0a 0a  & tmask==0) );..
27c11 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
27c12 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
27c13 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
27c14 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
27c15 2e 0a 20 20 2a 2a 20 56 69 65 77 47 65 74 43 6f  ..  ** ViewGetCo
27c16 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73 20 61  lumnNames() is a
27c17 20 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62 20 69   no-op if pTab i
27c18 73 20 6e 6f 74 20 61 20 76 69 65 77 20 28 6f 72  s not a view (or
27c19 20 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d   virtual .  ** m
27c1a 6f 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20  odule table)..  
27c1b 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
27c1c 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
27c1d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  es(pParse, pTab)
27c1e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73   ){.    goto ins
27c1f 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
27c20 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68  ..  /* Ensure th
27c21 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68 65  at:.  *  (a) the
27c22 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72 65   table is not re
27c23 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20 28  ad-only, .  *  (
27c24 62 29 20 74 68 61 74 20 69 66 20 69 74 20 69 73  b) that if it is
27c25 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e 20   a view then ON 
27c26 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73 20  INSERT triggers 
27c27 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66 28  exist.  */.  if(
27c28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
27c29 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ly(pParse, pTab,
27c2a 20 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20 67   tmask) ){.    g
27c2b 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
27c2c 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c  up;.  }..  /* Al
27c2d 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20 20  locate a VDBE.  
27c2e 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  */.  v = sqlite3
27c2f 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
27c30 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f  .  if( v==0 ) go
27c31 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27c32 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  p;.  if( pParse-
27c33 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c  >nested==0 ) sql
27c34 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
27c35 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74  nges(v);.  sqlit
27c36 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
27c37 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 53  ation(pParse, pS
27c38 65 6c 65 63 74 20 7c 7c 20 70 54 72 69 67 67 65  elect || pTrigge
27c39 72 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  r, iDb);..#ifnde
27c3a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  f SQLITE_OMIT_XF
27c3b 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74  ER_OPT.  /* If t
27c3c 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
27c3d 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a  of the form.  **
27c3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53 45  .  **       INSE
27c3f 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e  RT INTO <table1>
27c40 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c   SELECT * FROM <
27c41 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20  table2>;.  **.  
27c42 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c 20  ** Then special 
27c43 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61  optimizations ca
27c44 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74 68 61  n be applied tha
27c45 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e 73  t make the trans
27c46 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66 61  fer.  ** very fa
27c47 73 74 20 61 6e 64 20 77 68 69 63 68 20 72 65 64  st and which red
27c48 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  uce fragmentatio
27c49 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20 20  n of indices..  
27c4a 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  **.  ** This is 
27c4b 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65  the 2nd template
27c4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ..  */.  if( pCo
27c4d 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f  lumn==0 && xferO
27c4e 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72  ptimization(pPar
27c4f 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65 63  se, pTab, pSelec
27c50 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29  t, onError, iDb)
27c51 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
27c52 21 70 54 72 69 67 67 65 72 20 29 3b 0a 20 20 20  !pTrigger );.   
27c53 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 3d 3d   assert( pList==
27c54 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  0 );.    goto in
27c55 73 65 72 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65  sert_end;.  }.#e
27c56 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
27c57 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a  MIT_XFER_OPT */.
27c58 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
27c59 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   an AUTOINCREMEN
27c5a 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70  T table, look up
27c5b 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75   the sequence nu
27c5c 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a  mber in the.  **
27c5d 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
27c5e 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65   table and store
27c5f 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65   it in memory ce
27c60 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20  ll regAutoinc.. 
27c61 20 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69 6e 63   */.  regAutoinc
27c62 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28   = autoIncBegin(
27c63 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61  pParse, iDb, pTa
27c64 62 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65  b);..  /* Figure
27c65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f   out how many co
27c66 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 61 72  lumns of data ar
27c67 65 20 73 75 70 70 6c 69 65 64 2e 20 20 49 66 20  e supplied.  If 
27c68 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20 69 73  the data.  ** is
27c69 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53   coming from a S
27c6a 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c  ELECT statement,
27c6b 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61   then generate a
27c6c 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74   co-routine that
27c6d 0a 20 20 2a 2a 20 70 72 6f 64 75 63 65 73 20 61  .  ** produces a
27c6e 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74   single row of t
27c6f 68 65 20 53 45 4c 45 43 54 20 6f 6e 20 65 61 63  he SELECT on eac
27c70 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54  h invocation.  T
27c71 68 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69  he.  ** co-routi
27c72 6e 65 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e  ne is the common
27c73 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 33   header to the 3
27c74 72 64 20 61 6e 64 20 34 74 68 20 74 65 6d 70 6c  rd and 4th templ
27c75 61 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ates..  */.  if(
27c76 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
27c77 2f 2a 20 44 61 74 61 20 69 73 20 63 6f 6d 69 6e  /* Data is comin
27c78 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e  g from a SELECT.
27c79 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20    Generate code 
27c7a 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61  to implement tha
27c7b 74 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20  t SELECT.    ** 
27c7c 61 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e  as a co-routine.
27c7d 20 20 54 68 65 20 63 6f 64 65 20 69 73 20 63 6f    The code is co
27c7e 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74 68 65  mmon to both the
27c7f 20 33 72 64 20 61 6e 64 20 34 74 68 0a 20 20 20   3rd and 4th.   
27c80 20 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20   ** templates:. 
27c81 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
27c82 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20       EOF <- 0.  
27c83 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c    **         X <
27c84 2d 20 41 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  - A.    **      
27c85 20 20 20 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a     goto B.    **
27c86 20 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66        A: setup f
27c87 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  or the SELECT.  
27c88 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f    **         loo
27c89 70 20 6f 76 65 72 20 74 68 65 20 74 61 62 6c 65  p over the table
27c8a 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
27c8b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
27c8c 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f   load value into
27c8d 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e   register R..R+n
27c8e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
27c8f 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a    yield X.    **
27c90 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f           end loo
27c91 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  p.    **        
27c92 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72 20 74   cleanup after t
27c93 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
27c94 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20           EOF <- 
27c95 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  1.    **        
27c96 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20   yield X.    ** 
27c97 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72          halt-err
27c98 6f 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  or.    **.    **
27c99 20 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74   On each invocat
27c9a 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f  ion of the co-ro
27c9b 75 74 69 6e 65 2c 20 69 74 20 70 75 74 73 20 61  utine, it puts a
27c9c 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74   single row of t
27c9d 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54  he.    ** SELECT
27c9e 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67   result into reg
27c9f 69 73 74 65 72 73 20 64 65 73 74 2e 69 4d 65 6d  isters dest.iMem
27ca0 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73  ...dest.iMem+des
27ca1 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a  t.nMem-1..    **
27ca2 20 28 54 68 65 73 65 20 6f 75 74 70 75 74 20 72   (These output r
27ca3 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c  egisters are all
27ca4 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
27ca5 33 53 65 6c 65 63 74 28 29 2e 29 20 20 57 68 65  3Select().)  Whe
27ca6 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 45 4c  n.    ** the SEL
27ca7 45 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69  ECT completes, i
27ca8 74 20 73 65 74 73 20 74 68 65 20 45 4f 46 20 66  t sets the EOF f
27ca9 6c 61 67 20 73 74 6f 72 65 64 20 69 6e 20 72 65  lag stored in re
27caa 67 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  gEof..    */.   
27cab 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20   int rc, j1;..  
27cac 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61    regEof = ++pPa
27cad 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
27cae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27caf 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
27cb0 30 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20 20  0, regEof);     
27cb1 20 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a   /* EOF <- 0 */.
27cb2 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
27cb3 28 76 2c 20 22 53 45 4c 45 43 54 20 65 6f 66 20  (v, "SELECT eof 
27cb4 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 73 71 6c  flag"));.    sql
27cb5 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
27cb6 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 43 6f  it(&dest, SRT_Co
27cb7 72 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73  routine, ++pPars
27cb8 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64  e->nMem);.    ad
27cb9 64 72 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  drSelect = sqlit
27cba 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
27cbb 72 28 76 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69  r(v)+2;.    sqli
27cbc 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27cbd 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61 64 64   OP_Integer, add
27cbe 72 53 65 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e  rSelect-1, dest.
27cbf 69 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d  iParm);.    j1 =
27cc0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27cc1 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27cc2 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  , 0);.    VdbeCo
27cc3 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20  mment((v, "Jump 
27cc4 6f 76 65 72 20 53 45 4c 45 43 54 20 63 6f 72 6f  over SELECT coro
27cc5 75 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f  utine"));..    /
27cc6 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78  * Resolve the ex
27cc7 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
27cc8 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27cc9 74 20 61 6e 64 20 65 78 65 63 75 74 65 20 69 74  t and execute it
27cca 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  . */.    rc = sq
27ccb 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
27ccc 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65  se, pSelect, &de
27ccd 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  st);.    assert(
27cce 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
27ccf 20 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20 69 66   || rc );.    if
27cd0 28 20 72 63 20 7c 7c 20 4e 45 56 45 52 28 70 50  ( rc || NEVER(pP
27cd1 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 64  arse->nErr) || d
27cd2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
27cd3 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  ){.      goto in
27cd4 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
27cd5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
27cd6 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27cd7 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45  Integer, 1, regE
27cd8 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  of);         /* 
27cd9 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20  EOF <- 1 */.    
27cda 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27cdb 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64  1(v, OP_Yield, d
27cdc 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a  est.iParm);   /*
27cdd 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20   yield X */.    
27cde 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27cdf 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  2(v, OP_Halt, SQ
27ce0 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f  LITE_INTERNAL, O
27ce1 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20 56 64  E_Abort);.    Vd
27ce2 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45  beComment((v, "E
27ce3 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63 6f 72  nd of SELECT cor
27ce4 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 73  outine"));.    s
27ce5 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
27ce6 72 65 28 76 2c 20 6a 31 29 3b 20 20 20 20 20 20  re(v, j1);      
27ce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ce8 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20      /* label B: 
27ce9 2a 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d 53  */..    regFromS
27cea 65 6c 65 63 74 20 3d 20 64 65 73 74 2e 69 4d 65  elect = dest.iMe
27ceb 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  m;.    assert( p
27cec 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29  Select->pEList )
27ced 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20  ;.    nColumn = 
27cee 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
27cef 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65  >nExpr;.    asse
27cf0 72 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e  rt( dest.nMem==n
27cf1 43 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f  Column );..    /
27cf2 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54 61 62  * Set useTempTab
27cf3 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20 74 68  le to TRUE if th
27cf4 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
27cf5 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
27cf6 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62  .    ** should b
27cf7 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61  e written into a
27cf8 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
27cf9 20 28 74 65 6d 70 6c 61 74 65 20 34 29 2e 20 20   (template 4).  
27cfa 53 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41  Set to.    ** FA
27cfb 4c 53 45 20 69 66 20 65 61 63 68 2a 20 72 6f 77  LSE if each* row
27cfc 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 63   of the SELECT c
27cfd 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 64 69  an be written di
27cfe 72 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20  rectly into.    
27cff 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  ** the destinati
27d00 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61  on table (templa
27d01 74 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  te 3)..    **.  
27d02 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61 62 6c    ** A temp tabl
27d03 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 69  e must be used i
27d04 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
27d05 67 20 75 70 64 61 74 65 64 20 69 73 20 61 6c 73  g updated is als
27d06 6f 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20  o one.    ** of 
27d07 74 68 65 20 74 61 62 6c 65 73 20 62 65 69 6e 67  the tables being
27d08 20 72 65 61 64 20 62 79 20 74 68 65 20 53 45 4c   read by the SEL
27d09 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
27d0a 41 6c 73 6f 20 75 73 65 20 61 20 0a 20 20 20 20  Also use a .    
27d0b 2a 2a 20 74 65 6d 70 20 74 61 62 6c 65 20 69 6e  ** temp table in
27d0c 20 74 68 65 20 63 61 73 65 20 6f 66 20 72 6f 77   the case of row
27d0d 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a   triggers..    *
27d0e 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67  /.    if( pTrigg
27d0f 65 72 20 7c 7c 20 72 65 61 64 73 54 61 62 6c 65  er || readsTable
27d10 28 70 50 61 72 73 65 2c 20 61 64 64 72 53 65 6c  (pParse, addrSel
27d11 65 63 74 2c 20 69 44 62 2c 20 70 54 61 62 29 20  ect, iDb, pTab) 
27d12 29 7b 0a 20 20 20 20 20 20 75 73 65 54 65 6d 70  ){.      useTemp
27d13 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d  Table = 1;.    }
27d14 0a 0a 20 20 20 20 69 66 28 20 75 73 65 54 65 6d  ..    if( useTem
27d15 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
27d16 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 6f  /* Invoke the co
27d17 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61  routine to extra
27d18 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ct information f
27d19 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 0a 20  rom the SELECT. 
27d1a 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 64 64 20       ** and add 
27d1b 69 74 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e  it to a transien
27d1c 74 20 74 61 62 6c 65 20 73 72 63 54 61 62 2e 20  t table srcTab. 
27d1d 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   The code genera
27d1e 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 68 65 72  ted.      ** her
27d1f 65 20 69 73 20 66 72 6f 6d 20 74 68 65 20 34 74  e is from the 4t
27d20 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20 20 20  h template:.    
27d21 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
27d22 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20     B: open temp 
27d23 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20  table.      **  
27d24 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 20      L: yield X. 
27d25 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
27d26 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 20 20  if EOF goto M.  
27d27 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
27d28 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52  nsert row from R
27d29 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20  ..R+n into temp 
27d2a 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20  table.      **  
27d2b 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 20 20         goto L.  
27d2c 20 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a 20 2e      **      M: .
27d2d 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ...      */.    
27d2e 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 20    int regRec;   
27d2f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
27d30 65 72 20 74 6f 20 68 6f 6c 64 20 70 61 63 6b 65  er to hold packe
27d31 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  d record */.    
27d32 20 20 69 6e 74 20 72 65 67 54 65 6d 70 52 6f 77    int regTempRow
27d33 69 64 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74  id;    /* Regist
27d34 65 72 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70 20  er to hold temp 
27d35 74 61 62 6c 65 20 52 4f 57 49 44 20 2a 2f 0a 20  table ROWID */. 
27d36 20 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70       int addrTop
27d37 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62  ;         /* Lab
27d38 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 20 20 20  el "L" */.      
27d39 69 6e 74 20 61 64 64 72 49 66 3b 20 20 20 20 20  int addrIf;     
27d3a 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
27d3b 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f 0a  of jump to M */.
27d3c 0a 20 20 20 20 20 20 73 72 63 54 61 62 20 3d 20  .      srcTab = 
27d3d 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
27d3e 20 20 20 20 20 20 72 65 67 52 65 63 20 3d 20 73        regRec = s
27d3f 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
27d40 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
27d41 72 65 67 54 65 6d 70 52 6f 77 69 64 20 3d 20 73  regTempRowid = s
27d42 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
27d43 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
27d44 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27d45 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
27d46 6d 65 72 61 6c 2c 20 73 72 63 54 61 62 2c 20 6e  meral, srcTab, n
27d47 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61  Column);.      a
27d48 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  ddrTop = sqlite3
27d49 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27d4a 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61  _Yield, dest.iPa
27d4b 72 6d 29 3b 0a 20 20 20 20 20 20 61 64 64 72 49  rm);.      addrI
27d4c 66 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  f = sqlite3VdbeA
27d4d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
27d4e 72 65 67 45 6f 66 29 3b 0a 20 20 20 20 20 20 73  regEof);.      s
27d4f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27d50 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
27d51 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  d, regFromSelect
27d52 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65  , nColumn, regRe
27d53 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  c);.      sqlite
27d54 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27d55 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72 63 54  P_NewRowid, srcT
27d56 61 62 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64  ab, regTempRowid
27d57 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27d58 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27d59 5f 49 6e 73 65 72 74 2c 20 73 72 63 54 61 62 2c  _Insert, srcTab,
27d5a 20 72 65 67 52 65 63 2c 20 72 65 67 54 65 6d 70   regRec, regTemp
27d5b 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71  Rowid);.      sq
27d5c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27d5d 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
27d5e 64 64 72 54 6f 70 29 3b 0a 20 20 20 20 20 20 73  ddrTop);.      s
27d5f 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
27d60 72 65 28 76 2c 20 61 64 64 72 49 66 29 3b 0a 20  re(v, addrIf);. 
27d61 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
27d62 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
27d63 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20  e, regRec);.    
27d64 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
27d65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
27d66 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20  regTempRowid);. 
27d67 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
27d68 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
27d69 20 63 61 73 65 20 69 66 20 74 68 65 20 64 61 74   case if the dat
27d6a 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45 52 54  a for the INSERT
27d6b 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20   is coming from 
27d6c 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20  a VALUES.    ** 
27d6d 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20  clause.    */.  
27d6e 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
27d6f 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  C;.    memset(&s
27d70 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e  NC, 0, sizeof(sN
27d71 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61  C));.    sNC.pPa
27d72 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
27d73 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20    srcTab = -1;. 
27d74 20 20 20 61 73 73 65 72 74 28 20 75 73 65 54 65     assert( useTe
27d75 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  mpTable==0 );.  
27d76 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73    nColumn = pLis
27d77 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  t ? pList->nExpr
27d78 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d   : 0;.    for(i=
27d79 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  0; i<nColumn; i+
27d7a 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
27d7b 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
27d7c 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c 69 73  Names(&sNC, pLis
27d7d 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29  t->a[i].pExpr) )
27d7e 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69  {.        goto i
27d7f 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
27d80 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
27d81 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
27d82 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
27d83 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f  olumns in the so
27d84 75 72 63 65 20 64 61 74 61 20 6d 61 74 63 68 65  urce data matche
27d85 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 2a  s the number.  *
27d86 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  * of columns to 
27d87 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  be inserted into
27d88 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 2a 2f   the table..  */
27d89 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
27d8a 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 66 6f  (pTab) ){.    fo
27d8b 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
27d8c 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
27d8d 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73 48   nHidden += (IsH
27d8e 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61  iddenColumn(&pTa
27d8f 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20  b->aCol[i]) ? 1 
27d90 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  : 0);.    }.  }.
27d91 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30    if( pColumn==0
27d92 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e   && nColumn && n
27d93 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d 3e 6e  Column!=(pTab->n
27d94 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29 7b 0a  Col-nHidden) ){.
27d95 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
27d96 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
27d97 20 20 20 20 22 74 61 62 6c 65 20 25 53 20 68 61      "table %S ha
27d98 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62 75 74  s %d columns but
27d99 20 25 64 20 76 61 6c 75 65 73 20 77 65 72 65 20   %d values were 
27d9a 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20 20 20  supplied",.     
27d9b 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70    pTabList, 0, p
27d9c 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65  Tab->nCol-nHidde
27d9d 6e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  n, nColumn);.   
27d9e 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
27d9f 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
27da0 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20 6e 43  pColumn!=0 && nC
27da1 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d 3e  olumn!=pColumn->
27da2 6e 49 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nId ){.    sqlit
27da3 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
27da4 65 2c 20 22 25 64 20 76 61 6c 75 65 73 20 66 6f  e, "%d values fo
27da5 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c 20 6e  r %d columns", n
27da6 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e 2d  Column, pColumn-
27da7 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74 6f 20  >nId);.    goto 
27da8 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
27da9 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
27daa 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
27dab 74 20 69 6e 63 6c 75 64 65 64 20 61 6e 20 49 44  t included an ID
27dac 4c 49 53 54 20 74 65 72 6d 2c 20 74 68 65 6e 20  LIST term, then 
27dad 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a 20 61  make sure.  ** a
27dae 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
27daf 68 65 20 49 44 4c 49 53 54 20 72 65 61 6c 6c 79  he IDLIST really
27db0 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   are columns of 
27db1 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 0a 20  the table and . 
27db2 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74 68 65   ** remember the
27db3 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e   column indices.
27db4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
27db5 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49  e table has an I
27db6 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
27db7 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68  EY column and th
27db8 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 69  at column.  ** i
27db9 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 49  s named in the I
27dba 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65 63 6f  DLIST, then reco
27dbb 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43 6f 6c  rd in the keyCol
27dbc 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20 20 2a  umn variable.  *
27dbd 2a 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  * the index into
27dbe 20 49 44 4c 49 53 54 20 6f 66 20 74 68 65 20 70   IDLIST of the p
27dbf 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d  rimary key colum
27dc0 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 69 73  n.  keyColumn is
27dc1 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  .  ** the index 
27dc2 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  of the primary k
27dc3 65 79 20 61 73 20 69 74 20 61 70 70 65 61 72 73  ey as it appears
27dc4 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f 74 20   in IDLIST, not 
27dc5 61 73 0a 20 20 2a 2a 20 69 73 20 61 70 70 65 61  as.  ** is appea
27dc6 72 73 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  rs in the origin
27dc7 61 6c 20 74 61 62 6c 65 2e 20 20 28 54 68 65 20  al table.  (The 
27dc8 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 72 69  index of the pri
27dc9 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 69 6e  mary.  ** key in
27dca 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61   the original ta
27dcb 62 6c 65 20 69 73 20 70 54 61 62 2d 3e 69 50 4b  ble is pTab->iPK
27dcc 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ey.).  */.  if( 
27dcd 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 66  pColumn ){.    f
27dce 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d  or(i=0; i<pColum
27dcf 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20  n->nId; i++){.  
27dd0 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69      pColumn->a[i
27dd1 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20  ].idx = -1;.    
27dd2 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
27dd3 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69  <pColumn->nId; i
27dd4 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a  ++){.      for(j
27dd5 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; j<pTab->nCol
27dd6 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
27dd7 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
27dd8 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d  mp(pColumn->a[i]
27dd9 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43  .zName, pTab->aC
27dda 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20  ol[j].zName)==0 
27ddb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f  ){.          pCo
27ddc 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d  lumn->a[i].idx =
27ddd 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   j;.          if
27dde 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79  ( j==pTab->iPKey
27ddf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
27de0 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
27de1 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
27de2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27de3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27de4 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d      if( j>=pTab-
27de5 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  >nCol ){.       
27de6 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f   if( sqlite3IsRo
27de7 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69  wid(pColumn->a[i
27de8 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
27de9 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20        keyColumn 
27dea 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = i;.        }el
27deb 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  se{.          sq
27dec 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
27ded 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 53 20  arse, "table %S 
27dee 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61  has no column na
27def 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20  med %s",.       
27df0 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c         pTabList,
27df1 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69   0, pColumn->a[i
27df2 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
27df3 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
27df4 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  ++;.          go
27df5 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27df6 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  p;.        }.   
27df7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
27df8 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
27df9 20 6e 6f 20 49 44 4c 49 53 54 20 74 65 72 6d 20   no IDLIST term 
27dfa 62 75 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  but the table ha
27dfb 73 20 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69  s an integer pri
27dfc 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c 20 74  mary.  ** key, t
27dfd 68 65 20 73 65 74 20 74 68 65 20 6b 65 79 43 6f  he set the keyCo
27dfe 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 20 74 6f  lumn variable to
27dff 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
27e00 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20   column index.  
27e01 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ** in the origin
27e02 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74  al table definit
27e03 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ion..  */.  if( 
27e04 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
27e05 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6b  olumn>0 ){.    k
27e06 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d  eyColumn = pTab-
27e07 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 20 20 20 20  >iPKey;.  }.    
27e08 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
27e09 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f   the count of ro
27e0a 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  ws to be inserte
27e0b 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  d.  */.  if( db-
27e0c 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
27e0d 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20  CountRows ){.   
27e0e 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b   regRowCount = +
27e0f 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
27e10 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27e11 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
27e12 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75  er, 0, regRowCou
27e13 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nt);.  }..  /* I
27e14 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  f this is not a 
27e15 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65 20 74  view, open the t
27e16 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61 6c 6c  able and and all
27e17 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 66   indices */.  if
27e18 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
27e19 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20 20 20   int nIdx;..    
27e1a 62 61 73 65 43 75 72 20 3d 20 70 50 61 72 73 65  baseCur = pParse
27e1b 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49 64 78  ->nTab;.    nIdx
27e1c 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61   = sqlite3OpenTa
27e1d 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50  bleAndIndices(pP
27e1e 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65  arse, pTab, base
27e1f 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  Cur, OP_OpenWrit
27e20 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64 78 20  e);.    aRegIdx 
27e21 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
27e22 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
27e23 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29 3b 0a  int)*(nIdx+1));.
27e24 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d      if( aRegIdx=
27e25 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
27e26 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27e27 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
27e28 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b 2b 29  =0; i<nIdx; i++)
27e29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64 78 5b  {.      aRegIdx[
27e2a 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  i] = ++pParse->n
27e2b 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  Mem;.    }.  }..
27e2c 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
27e2d 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61 69 6e   top of the main
27e2e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20   insertion loop 
27e2f 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65 6d 70  */.  if( useTemp
27e30 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Table ){.    /* 
27e31 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65 73  This block codes
27e32 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f 70   the top of loop
27e33 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d 70   only.  The comp
27e34 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74 68 65  lete loop is the
27e35 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
27e36 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28 74 65  g pseudocode (te
27e37 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20 20 2a  mplate 4):.    *
27e38 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
27e39 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62   rewind temp tab
27e3a 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 43  le.    **      C
27e3b 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f 77 73  : loop over rows
27e3c 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61 74 65   of intermediate
27e3d 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20   table.    **   
27e3e 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72          transfer
27e3f 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74   values form int
27e40 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20  ermediate table 
27e41 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20 20 20  into <table>.   
27e42 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20   **         end 
27e43 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20  loop.    **     
27e44 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20   D: ....    */. 
27e45 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20     addrInsTop = 
27e46 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27e47 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  1(v, OP_Rewind, 
27e48 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61 64 64  srcTab);.    add
27e49 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  rCont = sqlite3V
27e4a 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
27e4b 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
27e4c 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a  Select ){.    /*
27e4d 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65   This block code
27e4e 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f  s the top of loo
27e4f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d  p only.  The com
27e50 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74 68  plete loop is th
27e51 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69  e.    ** followi
27e52 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28 74  ng pseudocode (t
27e53 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20 20 20  emplate 3):.    
27e54 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 43  **.    **      C
27e55 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a  : yield X.    **
27e56 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20           if EOF 
27e57 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20 20 20  goto D.    **   
27e58 20 20 20 20 20 20 69 6e 73 65 72 74 20 74 68 65        insert the
27e59 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69   select result i
27e5a 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d  nto <table> from
27e5b 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20 20   R..R+n.    **  
27e5c 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 20 20         goto C.  
27e5d 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e    **      D: ...
27e5e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72  .    */.    addr
27e5f 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
27e60 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
27e61 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72 6d  ield, dest.iParm
27e62 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f  );.    addrInsTo
27e63 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  p = sqlite3VdbeA
27e64 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
27e65 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a 20 20  regEof);.  }..  
27e66 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 67 69  /* Allocate regi
27e67 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e  sters for holdin
27e68 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74  g the rowid of t
27e69 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20 2a 2a  he new row,.  **
27e6a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
27e6b 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61 6e 64  the new row, and
27e6c 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20   the assemblied 
27e6d 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f  row record..  */
27e6e 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 2b  .  regRecord = +
27e6f 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
27e70 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 49   regRowid = regI
27e71 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ns = pParse->nMe
27e72 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  m+1;.  pParse->n
27e73 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f  Mem += pTab->nCo
27e74 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49 73 56  l + 1;.  if( IsV
27e75 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
27e76 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b 3b 0a      regRowid++;.
27e77 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
27e78 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61 74  ++;.  }.  regDat
27e79 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 3b 0a  a = regRowid+1;.
27e7a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 42 45  .  /* Run the BE
27e7b 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45 41 44  FORE and INSTEAD
27e7c 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20 69 66   OF triggers, if
27e7d 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a 20   there are any. 
27e7e 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f 70 20   */.  endOfLoop 
27e7f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
27e80 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69 66 28  eLabel(v);.  if(
27e81 20 74 6d 61 73 6b 20 26 20 54 52 49 47 47 45 52   tmask & TRIGGER
27e82 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20 20 69  _BEFORE ){.    i
27e83 6e 74 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c  nt regCols = sql
27e84 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
27e85 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
27e86 43 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  Col+1);..    /* 
27e87 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e 2a 20  build the NEW.* 
27e88 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e 20 20  reference row.  
27e89 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74 68 65  Note that if the
27e8a 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  re is an INTEGER
27e8b 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52 59 20  .    ** PRIMARY 
27e8c 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68 20 61  KEY into which a
27e8d 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67 20 69   NULL is being i
27e8e 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20 4e 55  nserted, that NU
27e8f 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a  LL will be.    *
27e90 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74  * translated int
27e91 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f  o a unique ID fo
27e92 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75 74 20  r the row.  But 
27e93 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72 69 67  on a BEFORE trig
27e94 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65 20 64  ger,.    ** we d
27e95 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61 74 20  o not know what 
27e96 74 68 65 20 75 6e 69 71 75 65 20 49 44 20 77 69  the unique ID wi
27e97 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65 20 74  ll be (because t
27e98 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a 20 20  he insert has.  
27e99 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65 6e 65    ** not happene
27e9a 64 20 79 65 74 29 20 73 6f 20 77 65 20 73 75 62  d yet) so we sub
27e9b 73 74 69 74 75 74 65 20 61 20 72 6f 77 69 64 20  stitute a rowid 
27e9c 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20 20 20  of -1.    */.   
27e9d 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30   if( keyColumn<0
27e9e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
27e9f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27ea0 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72  P_Integer, -1, r
27ea1 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d 65 6c  egCols);.    }el
27ea2 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 31  se{.      int j1
27ea3 3b 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54  ;.      if( useT
27ea4 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
27ea5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27ea6 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
27ea7 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43  mn, srcTab, keyC
27ea8 6f 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c 73 29 3b  olumn, regCols);
27ea9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27eaa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
27eab 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  elect==0 );  /* 
27eac 4f 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d  Otherwise useTem
27ead 70 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a  pTable is true *
27eae 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
27eaf 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
27eb0 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f  , pList->a[keyCo
27eb1 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67  lumn].pExpr, reg
27eb2 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Cols);.      }. 
27eb3 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
27eb4 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27eb5 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 43 6f  P_NotNull, regCo
27eb6 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ls);.      sqlit
27eb7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27eb8 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
27eb9 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20  regCols);.      
27eba 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
27ebb 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20  ere(v, j1);.    
27ebc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27ebd 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op1(v, OP_MustBe
27ebe 49 6e 74 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20  Int, regCols);. 
27ebf 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e     }..    /* Can
27ec0 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67 65 72  not have trigger
27ec1 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74  s on a virtual t
27ec2 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65  able. If it were
27ec3 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a   possible,.    *
27ec4 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75  * this block wou
27ec5 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f 75  ld have to accou
27ec6 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63 6f  nt for hidden co
27ec7 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lumn..    */.   
27ec8 20 61 73 73 65 72 74 28 20 21 49 73 56 69 72 74   assert( !IsVirt
27ec9 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 0a 20 20  ual(pTab) );..  
27eca 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
27ecb 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a  new column data.
27ecc 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
27ecd 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
27ece 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
27ecf 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a  ( pColumn==0 ){.
27ed0 20 20 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20          j = i;. 
27ed1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27ed2 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
27ed3 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b  Column->nId; j++
27ed4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
27ed5 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69   pColumn->a[j].i
27ed6 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20  dx==i ) break;. 
27ed7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27ed8 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75  .      if( pColu
27ed9 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e  mn && j>=pColumn
27eda 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20  ->nId ){.       
27edb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
27edc 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61  (pParse, pTab->a
27edd 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65  Col[i].pDflt, re
27ede 67 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20 20 20  gCols+i+1);.    
27edf 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54    }else if( useT
27ee0 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
27ee1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27ee2 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
27ee3 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 72  mn, srcTab, j, r
27ee4 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0a 20 20  egCols+i+1); .  
27ee5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27ee6 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65     assert( pSele
27ee7 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65  ct==0 ); /* Othe
27ee8 72 77 69 73 65 20 75 73 65 54 65 6d 70 54 61 62  rwise useTempTab
27ee9 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20  le is true */.  
27eea 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27eeb 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50  rCodeAndCache(pP
27eec 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a  arse, pList->a[j
27eed 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73  ].pExpr, regCols
27eee 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  +i+1);.      }. 
27eef 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
27ef0 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52  this is an INSER
27ef1 54 20 6f 6e 20 61 20 76 69 65 77 20 77 69 74 68  T on a view with
27ef2 20 61 6e 20 49 4e 53 54 45 41 44 20 4f 46 20 49   an INSTEAD OF I
27ef3 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c 0a 20  NSERT trigger,. 
27ef4 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74     ** do not att
27ef5 65 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65 72 73  empt any convers
27ef6 69 6f 6e 73 20 62 65 66 6f 72 65 20 61 73 73 65  ions before asse
27ef7 6d 62 6c 69 6e 67 20 74 68 65 20 72 65 63 6f 72  mbling the recor
27ef8 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69  d..    ** If thi
27ef9 73 20 69 73 20 61 20 72 65 61 6c 20 74 61 62 6c  s is a real tabl
27efa 65 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e 76 65  e, attempt conve
27efb 72 73 69 6f 6e 73 20 61 73 20 72 65 71 75 69 72  rsions as requir
27efc 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a  ed by the.    **
27efd 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66   table column af
27efe 66 69 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f  finities..    */
27eff 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77  .    if( !isView
27f00 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
27f01 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27f02 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 65 67 43  P_Affinity, regC
27f03 6f 6c 73 2b 31 2c 20 70 54 61 62 2d 3e 6e 43 6f  ols+1, pTab->nCo
27f04 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
27f05 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74  3TableAffinitySt
27f06 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  r(v, pTab);.    
27f07 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42  }..    /* Fire B
27f08 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44  EFORE or INSTEAD
27f09 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a   OF triggers */.
27f0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
27f0b 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
27f0c 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 49  , pTrigger, TK_I
27f0d 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45  NSERT, 0, TRIGGE
27f0e 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20 20 20  R_BEFORE, .     
27f0f 20 20 20 70 54 61 62 2c 20 72 65 67 43 6f 6c 73     pTab, regCols
27f10 2d 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c 20 6f  -pTab->nCol-1, o
27f11 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f  nError, endOfLoo
27f12 70 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  p);..    sqlite3
27f13 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
27f14 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73  (pParse, regCols
27f15 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b  , pTab->nCol+1);
27f16 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 73 68 20  .  }..  /* Push 
27f17 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
27f18 72 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e  r for the new en
27f19 74 72 79 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  try onto the sta
27f1a 63 6b 2e 20 20 54 68 65 0a 20 20 2a 2a 20 72 65  ck.  The.  ** re
27f1b 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 61  cord number is a
27f1c 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61   randomly genera
27f1d 74 65 20 69 6e 74 65 67 65 72 20 63 72 65 61 74  te integer creat
27f1e 65 64 20 62 79 20 4e 65 77 52 6f 77 69 64 0a 20  ed by NewRowid. 
27f1f 20 2a 2a 20 65 78 63 65 70 74 20 77 68 65 6e 20   ** except when 
27f20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
27f21 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
27f22 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20   KEY column, in 
27f23 77 68 69 63 68 0a 20 20 2a 2a 20 63 61 73 65 20  which.  ** case 
27f24 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
27f25 72 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  r is the same as
27f26 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20   that column. . 
27f27 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56 69 65   */.  if( !isVie
27f28 77 20 29 7b 0a 20 20 20 20 69 66 28 20 49 73 56  w ){.    if( IsV
27f29 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
27f2a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77        /* The row
27f2b 20 74 68 61 74 20 74 68 65 20 56 55 70 64 61 74   that the VUpdat
27f2c 65 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 64 65  e opcode will de
27f2d 6c 65 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20  lete: none */.  
27f2e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27f2f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
27f30 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20  , 0, regIns);.  
27f31 20 20 7d 0a 20 20 20 20 69 66 28 20 6b 65 79 43    }.    if( keyC
27f32 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20  olumn>=0 ){.    
27f33 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62    if( useTempTab
27f34 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
27f35 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27f36 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
27f37 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c  cTab, keyColumn,
27f38 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27f39 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c    }else if( pSel
27f3a 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
27f3b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27f3c 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
27f3d 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6b 65 79 43  gFromSelect+keyC
27f3e 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29  olumn, regRowid)
27f3f 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
27f40 20 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70         VdbeOp *p
27f41 4f 70 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  Op;.        sqli
27f42 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
27f43 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79  se, pList->a[key
27f44 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72  Column].pExpr, r
27f45 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
27f46 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56    pOp = sqlite3V
27f47 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b  dbeGetOp(v, -1);
27f48 0a 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57  .        if( ALW
27f49 41 59 53 28 70 4f 70 29 20 26 26 20 70 4f 70 2d  AYS(pOp) && pOp-
27f4a 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c  >opcode==OP_Null
27f4b 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70   && !IsVirtual(p
27f4c 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
27f4d 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31    appendFlag = 1
27f4e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
27f4f 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77  >opcode = OP_New
27f50 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20  Rowid;.         
27f51 20 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43   pOp->p1 = baseC
27f52 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f  ur;.          pO
27f53 70 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64  p->p2 = regRowid
27f54 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
27f55 3e 70 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63  >p3 = regAutoinc
27f56 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
27f57 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20    }.      /* If 
27f58 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
27f59 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55  expression is NU
27f5a 4c 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f  LL, then use OP_
27f5b 4e 65 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a  NewRowid.      *
27f5c 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  * to generate a 
27f5d 75 6e 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b  unique primary k
27f5e 65 79 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20  ey value..      
27f5f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 61 70  */.      if( !ap
27f60 70 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20  pendFlag ){.    
27f61 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20      int j1;.    
27f62 20 20 20 20 69 66 28 20 21 49 73 56 69 72 74 75      if( !IsVirtu
27f63 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
27f64 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74        j1 = sqlit
27f65 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
27f66 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52  OP_NotNull, regR
27f67 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20  owid);.         
27f68 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27f69 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p3(v, OP_NewRowi
27f6a 64 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52  d, baseCur, regR
27f6b 6f 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63  owid, regAutoinc
27f6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
27f6d 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
27f6e 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20  (v, j1);.       
27f6f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27f70 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
27f71 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
27f72 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
27f73 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27f74 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52   OP_IsNull, regR
27f75 6f 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20  owid, j1+2);.   
27f76 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
27f77 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
27f78 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
27f79 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
27f7a 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
27f7b 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
27f7c 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
27f7d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27f7e 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
27f7f 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73  Rowid);.    }els
27f80 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
27f81 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27f82 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43  _NewRowid, baseC
27f83 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65  ur, regRowid, re
27f84 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20  gAutoinc);.     
27f85 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b   appendFlag = 1;
27f86 0a 20 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49  .    }.    autoI
27f87 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72  ncStep(pParse, r
27f88 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f  egAutoinc, regRo
27f89 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75  wid);..    /* Pu
27f8a 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  sh onto the stac
27f8b 6b 2c 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20  k, data for all 
27f8c 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e  columns of the n
27f8d 65 77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e  ew entry, beginn
27f8e 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20  ing.    ** with 
27f8f 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
27f90 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69  ..    */.    nHi
27f91 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f  dden = 0;.    fo
27f92 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
27f93 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
27f94 20 69 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d   int iRegStore =
27f95 20 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20   regRowid+1+i;. 
27f96 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62       if( i==pTab
27f97 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
27f98 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
27f99 6f 66 20 74 68 65 20 49 4e 54 45 47 45 52 20 50  of the INTEGER P
27f9a 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
27f9b 6e 20 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55  n is always a NU
27f9c 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57  LL..        ** W
27f9d 68 65 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c  henever this col
27f9e 75 6d 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65  umn is read, the
27f9f 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77   record number w
27fa0 69 6c 6c 20 62 65 20 73 75 62 73 74 69 74 75 74  ill be substitut
27fa1 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  ed.        ** in
27fa2 20 69 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20   its place.  So 
27fa3 77 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63  will fill this c
27fa4 6f 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c  olumn with a NUL
27fa5 4c 20 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20  L to avoid.     
27fa6 20 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20     ** taking up 
27fa7 64 61 74 61 20 73 70 61 63 65 20 77 69 74 68 20  data space with 
27fa8 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74  information that
27fa9 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75   will never be u
27faa 73 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sed. */.        
27fab 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27fac 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
27fad 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
27fae 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
27faf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
27fb0 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20   pColumn==0 ){. 
27fb1 20 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64         if( IsHid
27fb2 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d  denColumn(&pTab-
27fb3 3e 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20  >aCol[i]) ){.   
27fb4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 49         assert( I
27fb5 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27fb6 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20  ;.          j = 
27fb7 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48  -1;.          nH
27fb8 69 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20  idden++;.       
27fb9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27fba 20 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65    j = i - nHidde
27fbb 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
27fbc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27fbd 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f    for(j=0; j<pCo
27fbe 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b  lumn->nId; j++){
27fbf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
27fc0 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78  Column->a[j].idx
27fc1 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==i ) break;.   
27fc2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
27fc3 20 20 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20       if( j<0 || 
27fc4 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70  nColumn==0 || (p
27fc5 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f  Column && j>=pCo
27fc6 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20  lumn->nId) ){.  
27fc7 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
27fc8 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54  rCode(pParse, pT
27fc9 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c  ab->aCol[i].pDfl
27fca 74 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20  t, iRegStore);. 
27fcb 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75       }else if( u
27fcc 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20  seTempTable ){. 
27fcd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
27fce 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
27fcf 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a  olumn, srcTab, j
27fd0 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20  , iRegStore); . 
27fd1 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
27fd2 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
27fd3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27fd4 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
27fd5 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a   regFromSelect+j
27fd6 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20  , iRegStore);.  
27fd7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27fd8 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27fd9 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74  de(pParse, pList
27fda 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52  ->a[j].pExpr, iR
27fdb 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20  egStore);.      
27fdc 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
27fdd 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
27fde 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
27fdf 74 73 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20  ts and generate 
27fe0 69 6e 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20  index keys and. 
27fe1 20 20 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73     ** do the ins
27fe2 65 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23  ertion..    */.#
27fe3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27fe4 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
27fe5 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
27fe6 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
27fe7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 54   const char *pVT
27fe8 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ab = (const char
27fe9 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54 61   *)sqlite3GetVTa
27fea 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a 20  ble(db, pTab);. 
27feb 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
27fec 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61  MakeWritable(pPa
27fed 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
27fee 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27fef 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74  Op4(v, OP_VUpdat
27ff0 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  e, 1, pTab->nCol
27ff1 2b 32 2c 20 72 65 67 49 6e 73 2c 20 70 56 54 61  +2, regIns, pVTa
27ff2 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20  b, P4_VTAB);.   
27ff3 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f     sqlite3MayAbo
27ff4 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  rt(pParse);.    
27ff5 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
27ff6 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 52   {.      int isR
27ff7 65 70 6c 61 63 65 3b 20 20 20 20 2f 2a 20 53 65  eplace;    /* Se
27ff8 74 20 74 6f 20 74 72 75 65 20 69 66 20 63 6f 6e  t to true if con
27ff9 73 74 72 61 69 6e 74 73 20 6d 61 79 20 63 61 75  straints may cau
27ffa 73 65 20 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a  se a replace */.
27ffb 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
27ffc 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
27ffd 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54  hecks(pParse, pT
27ffe 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67  ab, baseCur, reg
27fff 49 6e 73 2c 20 61 52 65 67 49 64 78 2c 0a 20 20  Ins, aRegIdx,.  
28000 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d          keyColum
28001 6e 3e 3d 30 2c 20 30 2c 20 6f 6e 45 72 72 6f 72  n>=0, 0, onError
28002 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26 69 73  , endOfLoop, &is
28003 52 65 70 6c 61 63 65 0a 20 20 20 20 20 20 29 3b  Replace.      );
28004 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b  .      sqlite3Fk
28005 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54  Check(pParse, pT
28006 61 62 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a  ab, 0, regIns);.
28007 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d        sqlite3Com
28008 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a  pleteInsertion(.
28009 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
2800a 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
2800b 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78   regIns, aRegIdx
2800c 2c 20 30 2c 20 61 70 70 65 6e 64 46 6c 61 67 2c  , 0, appendFlag,
2800d 20 69 73 52 65 70 6c 61 63 65 3d 3d 30 0a 20 20   isReplace==0.  
2800e 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d      );.    }.  }
2800f 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
28010 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20  e count of rows 
28011 74 68 61 74 20 61 72 65 20 69 6e 73 65 72 74 65  that are inserte
28012 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  d.  */.  if( (db
28013 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
28014 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29  _CountRows)!=0 )
28015 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
28016 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
28017 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e  dImm, regRowCoun
28018 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  t, 1);.  }..  if
28019 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
2801a 20 20 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20    /* Code AFTER 
2801b 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20  triggers */.    
2801c 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2801d 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54  igger(pParse, pT
2801e 72 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52  rigger, TK_INSER
2801f 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46  T, 0, TRIGGER_AF
28020 54 45 52 2c 20 0a 20 20 20 20 20 20 20 20 70 54  TER, .        pT
28021 61 62 2c 20 72 65 67 44 61 74 61 2d 32 2d 70 54  ab, regData-2-pT
28022 61 62 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45 72 72 6f  ab->nCol, onErro
28023 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  r, endOfLoop);. 
28024 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74   }..  /* The bot
28025 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  tom of the main 
28026 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 2c 20  insertion loop, 
28027 69 66 20 74 68 65 20 64 61 74 61 20 73 6f 75 72  if the data sour
28028 63 65 0a 20 20 2a 2a 20 69 73 20 61 20 53 45 4c  ce.  ** is a SEL
28029 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  ECT statement.. 
2802a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2802b 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2802c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 69   endOfLoop);.  i
2802d 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
2802e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2802f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
28030 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61 64 64  ext, srcTab, add
28031 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69  rCont);.    sqli
28032 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
28033 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a  v, addrInsTop);.
28034 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28035 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
28036 65 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 7d 65  e, srcTab);.  }e
28037 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20  lse if( pSelect 
28038 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
28039 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2803a 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f 6e 74  oto, 0, addrCont
2803b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2803c 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2803d 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d 0a 0a  drInsTop);.  }..
2803e 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61 6c    if( !IsVirtual
2803f 28 70 54 61 62 29 20 26 26 20 21 69 73 56 69 65  (pTab) && !isVie
28040 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c 6f 73  w ){.    /* Clos
28041 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f 70 65  e all tables ope
28042 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ned */.    sqlit
28043 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
28044 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65 43 75  OP_Close, baseCu
28045 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d  r);.    for(idx=
28046 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
28047 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
28048 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64  =pIdx->pNext, id
28049 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  x++){.      sqli
2804a 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2804b 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62   OP_Close, idx+b
2804c 61 73 65 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  aseCur);.    }. 
2804d 20 7d 0a 0a 69 6e 73 65 72 74 5f 65 6e 64 3a 0a   }..insert_end:.
2804e 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
2804f 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
28050 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67  table by storing
28051 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
28052 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d  the.  ** maximum
28053 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76   rowid counter v
28054 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20 77  alues recorded w
28055 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69  hile inserting i
28056 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63  nto.  ** autoinc
28057 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20  rement tables.. 
28058 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65   */.  if( pParse
28059 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70  ->nested==0 && p
2805a 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54  Parse->pTriggerT
2805b 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ab==0 ){.    sql
2805c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
2805d 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a 20 20  tEnd(pParse);.  
2805e 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74  }..  /*.  ** Ret
2805f 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
28060 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65 64 2e  f rows inserted.
28061 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
28062 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61   is .  ** genera
28063 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73  ting code becaus
28064 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73  e of a call to s
28065 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
28066 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a  e(), do not.  **
28067 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   invoke the call
28068 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  back function.. 
28069 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66   */.  if( (db->f
2806a 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e  lags&SQLITE_Coun
2806b 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73  tRows) && !pPars
2806c 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50  e->nested && !pP
2806d 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
2806e 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
2806f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28070 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52  _ResultRow, regR
28071 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20  owCount, 1);.   
28072 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
28073 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
28074 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
28075 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
28076 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77  LNAME_NAME, "row
28077 73 20 69 6e 73 65 72 74 65 64 22 2c 20 53 51 4c  s inserted", SQL
28078 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
28079 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  ..insert_cleanup
2807a 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
2807b 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  stDelete(db, pTa
2807c 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  bList);.  sqlite
2807d 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2807e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71  db, pList);.  sq
2807f 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
28080 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
28081 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
28082 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d  elete(db, pColum
28083 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  n);.  sqlite3DbF
28084 72 65 65 28 64 62 2c 20 61 52 65 67 49 64 78 29  ree(db, aRegIdx)
28085 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72  ;.}../* Make sur
28086 65 20 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f  e "isView" and o
28087 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69  ther macros defi
28088 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e  ned above are un
28089 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69  defined. Otherwi
2808a 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20  se.** thely may 
2808b 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 63  interfere with c
2808c 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74  ompilation of ot
2808d 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  her functions in
2808e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f   this file.** (o
2808f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c  r in another fil
28090 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c 65 20  e, if this file 
28091 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20  becomes part of 
28092 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
28093 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73  ).  */.#ifdef is
28094 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56  View. #undef isV
28095 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  iew.#endif.#ifde
28096 66 20 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64  f pTrigger. #und
28097 65 66 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64  ef pTrigger.#end
28098 69 66 0a 23 69 66 64 65 66 20 74 6d 61 73 6b 0a  if.#ifdef tmask.
28099 20 23 75 6e 64 65 66 20 74 6d 61 73 6b 0a 23 65   #undef tmask.#e
2809a 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ndif.../*.** Gen
2809b 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f  erate code to do
2809c 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
2809d 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20 49  ks prior to an I
2809e 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44 41  NSERT or an UPDA
2809f 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  TE..**.** The in
280a0 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20 6f  put is a range o
280a1 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65  f consecutive re
280a2 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f  gisters as follo
280a3 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e 20  ws:.**.**    1. 
280a4 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   The rowid of th
280a5 65 20 72 6f 77 20 61 66 74 65 72 20 74 68 65 20  e row after the 
280a6 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  update..**.**   
280a7 20 32 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e   2.  The data in
280a8 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d   the first colum
280a9 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61  n of the entry a
280aa 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e  fter the update.
280ab 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61  .**.**    i.  Da
280ac 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63  ta from middle c
280ad 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  olumns....**.** 
280ae 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61 20     N.  The data 
280af 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  in the last colu
280b0 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20  mn of the entry 
280b1 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
280b2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 52  ..**.** The regR
280b3 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72 20 69  owid parameter i
280b4 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
280b5 68 65 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74  he register cont
280b6 61 69 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a 0a 2a  aining (1)..**.*
280b7 2a 20 49 66 20 69 73 55 70 64 61 74 65 20 69 73  * If isUpdate is
280b8 20 74 72 75 65 20 61 6e 64 20 72 6f 77 69 64 43   true and rowidC
280b9 68 6e 67 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  hng is non-zero,
280ba 20 74 68 65 6e 20 72 6f 77 69 64 43 68 6e 67 20   then rowidChng 
280bb 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
280bc 61 64 64 72 65 73 73 20 6f 66 20 61 20 72 65 67  address of a reg
280bd 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  ister containing
280be 20 74 68 65 20 72 6f 77 69 64 20 62 65 66 6f 72   the rowid befor
280bf 65 20 74 68 65 20 75 70 64 61 74 65 20 74 61 6b  e the update tak
280c0 65 73 0a 2a 2a 20 70 6c 61 63 65 2e 20 69 73 55  es.** place. isU
280c1 70 64 61 74 65 20 69 73 20 74 72 75 65 20 66 6f  pdate is true fo
280c2 72 20 55 50 44 41 54 45 73 20 61 6e 64 20 66 61  r UPDATEs and fa
280c3 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54 73 2e  lse for INSERTs.
280c4 20 49 66 20 69 73 55 70 64 61 74 65 0a 2a 2a 20   If isUpdate.** 
280c5 69 73 20 66 61 6c 73 65 2c 20 69 6e 64 69 63 61  is false, indica
280c6 74 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 20 73  ting an INSERT s
280c7 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 61  tatement, then a
280c8 20 6e 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69 64 43   non-zero rowidC
280c9 68 6e 67 20 0a 2a 2a 20 69 6e 64 69 63 61 74 65  hng .** indicate
280ca 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 64  s that the rowid
280cb 20 77 61 73 20 65 78 70 6c 69 63 69 74 6c 79 20   was explicitly 
280cc 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
280cd 74 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 53 45  t of the.** INSE
280ce 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 49 66  RT statement. If
280cf 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61   rowidChng is fa
280d0 6c 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  lse, it means th
280d1 61 74 20 20 74 68 65 20 72 6f 77 69 64 20 69 73  at  the rowid is
280d2 0a 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61 75 74  .** computed aut
280d3 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e  omatically in an
280d4 20 69 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20   insert or that 
280d5 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20  the rowid value 
280d6 69 73 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64 69 66  is not .** modif
280d7 69 65 64 20 62 79 20 61 6e 20 75 70 64 61 74 65  ied by an update
280d8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ..**.** The code
280d9 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
280da 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65  is routine store
280db 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69   new index entri
280dc 65 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73  es into.** regis
280dd 74 65 72 73 20 69 64 65 6e 74 69 66 69 65 64 20  ters identified 
280de 62 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e  by aRegIdx[].  N
280df 6f 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  o index entry is
280e0 20 63 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20   created for.** 
280e1 69 6e 64 69 63 65 73 20 77 68 65 72 65 20 61 52  indices where aR
280e2 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68  egIdx[i]==0.  Th
280e3 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63  e order of indic
280e4 65 73 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20  es in aRegIdx[] 
280e5 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61  is.** the same a
280e6 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69  s the order of i
280e7 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69  ndices on the li
280e8 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64  nked list of ind
280e9 69 63 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64  ices.** attached
280ea 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   to the table..*
280eb 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
280ec 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73  e also generates
280ed 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63   code to check c
280ee 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54  onstraints.  NOT
280ef 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c   NULL,.** CHECK,
280f0 20 61 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73   and UNIQUE cons
280f1 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20  traints are all 
280f2 63 68 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63  checked.  If a c
280f3 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c  onstraint fails,
280f4 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70  .** then the app
280f5 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20  ropriate action 
280f6 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54  is performed.  T
280f7 68 65 72 65 20 61 72 65 20 66 69 76 65 20 70 6f  here are five po
280f8 73 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e  ssible.** action
280f9 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f  s: ROLLBACK, ABO
280fa 52 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43  RT, FAIL, REPLAC
280fb 45 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a  E, and IGNORE..*
280fc 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74  *.**  Constraint
280fd 20 74 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20   type  Action   
280fe 20 20 20 20 57 68 61 74 20 48 61 70 70 65 6e 73      What Happens
280ff 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
28100 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----  ----------
28101 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
28102 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28103 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
28104 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
28105 20 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68   ROLLBACK     Th
28106 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
28107 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
28108 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20  back and.**     
28109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2810a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2810b 65 33 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e  e3_exec() return
2810c 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69  s immediately wi
2810d 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20  th a.**         
2810e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2810f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f         return co
28110 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e  de of SQLITE_CON
28111 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20  STRAINT..**.**  
28112 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
28113 20 41 42 4f 52 54 20 20 20 20 20 20 20 20 42 61   ABORT        Ba
28114 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66  ck out changes f
28115 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20  rom the current 
28116 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20  command.**      
28117 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28118 20 20 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28            only (
28119 64 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70  do not do a comp
2811a 6c 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74  lete rollback) t
2811b 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  hen.**          
2811c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2811d 20 20 20 20 20 20 63 61 75 73 65 20 73 71 6c 69        cause sqli
2811e 74 65 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65  te3_exec() to re
2811f 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79  turn immediately
28120 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
28121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28122 20 20 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43     with SQLITE_C
28123 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a  ONSTRAINT..**.**
28124 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
28125 20 20 20 46 41 49 4c 20 20 20 20 20 20 20 20 20     FAIL         
28126 53 71 6c 69 74 65 5f 65 78 65 63 28 29 20 72 65  Sqlite_exec() re
28127 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c  turns immediatel
28128 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20  y with a.**     
28129 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2812a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
2812b 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45  n code of SQLITE
2812c 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68  _CONSTRAINT.  Th
2812d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2812e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2812f 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20      transaction 
28130 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61  is not rolled ba
28131 63 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20  ck and any.**   
28132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28133 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69               pri
28134 6f 72 20 63 68 61 6e 67 65 73 20 61 72 65 20 72  or changes are r
28135 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  etained..**.**  
28136 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
28137 20 49 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68   IGNORE       Th
28138 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
28139 61 6e 64 20 64 61 74 61 20 69 73 20 70 6f 70 70  and data is popp
2813a 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20  ed from.**      
2813b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2813c 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 74            the st
2813d 61 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73  ack and there is
2813e 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75   an immediate ju
2813f 6d 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  mp.**           
28140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28141 20 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67       to label ig
28142 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20  noreDest..**.** 
28143 20 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20   NOT NULL       
28144 20 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 54    REPLACE      T
28145 68 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73  he NULL value is
28146 20 72 65 70 6c 61 63 65 20 62 79 20 74 68 65 20   replace by the 
28147 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20  default.**      
28148 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28149 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20            value 
2814a 66 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e  for that column.
2814b 20 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74    If the default
2814c 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20   value.**       
2814d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2814e 20 20 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c           is NULL
2814f 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  , the action is 
28150 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52  the same as ABOR
28151 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45  T..**.**  UNIQUE
28152 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41             REPLA
28153 43 45 20 20 20 20 20 20 54 68 65 20 6f 74 68 65  CE      The othe
28154 72 20 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c  r row that confl
28155 69 63 74 73 20 77 69 74 68 20 74 68 65 20 72 6f  icts with the ro
28156 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
28157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28158 20 20 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74      being insert
28159 65 64 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a  ed is removed..*
2815a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20  *.**  CHECK     
2815b 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20         REPLACE  
2815c 20 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68      Illegal.  Th
2815d 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20  e results in an 
2815e 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  exception..**.**
2815f 20 57 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f   Which action to
28160 20 74 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69   take is determi
28161 6e 65 64 20 62 79 20 74 68 65 20 6f 76 65 72 72  ned by the overr
28162 69 64 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74  ideError paramet
28163 65 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65  er..** Or if ove
28164 72 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44  rrideError==OE_D
28165 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65  efault, then the
28166 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72   pParse->onError
28167 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73   parameter.** is
28168 20 75 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50   used.  Or if pP
28169 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  arse->onError==O
2816a 45 5f 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74  E_Default then t
2816b 68 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65  he onError value
2816c 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73  .** for the cons
2816d 74 72 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a  traint is used..
2816e 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e  **.** The callin
2816f 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f  g routine must o
28170 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65  pen a read/write
28171 20 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62   cursor for pTab
28172 20 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20   with.** cursor 
28173 6e 75 6d 62 65 72 20 22 62 61 73 65 43 75 72 22  number "baseCur"
28174 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f  .  All indices o
28175 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f  f pTab must also
28176 20 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65   have open.** re
28177 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73  ad/write cursors
28178 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d   with cursor num
28179 62 65 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f  ber baseCur+i fo
2817a 72 20 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f  r the i-th curso
2817b 72 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66  r..** Except, if
2817c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73   there is no pos
2817d 73 69 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45  sibility of a RE
2817e 50 4c 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65  PLACE action the
2817f 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20  n.** cursors do 
28180 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f  not need to be o
28181 70 65 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20  pen for indices 
28182 77 68 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d  where aRegIdx[i]
28183 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ==0..*/.SQLITE_P
28184 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
28185 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
28186 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50  raintChecks(.  P
28187 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
28188 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
28189 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
2818a 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
2818b 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69    /* the table i
2818c 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
2818d 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
2818e 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
2818f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
28190 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  a read/write cur
28191 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
28192 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65  pTab */.  int re
28193 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a  gRowid,       /*
28194 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61   Index of the ra
28195 6e 67 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67  nge of input reg
28196 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  isters */.  int 
28197 2a 61 52 65 67 49 64 78 2c 20 20 20 20 20 20 20  *aRegIdx,       
28198 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64  /* Register used
28199 20 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20   by each index. 
2819a 20 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e   0 for unused in
2819b 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  dices */.  int r
2819c 6f 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f  owidChng,      /
2819d 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72 6f  * True if the ro
2819e 77 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64  wid might collid
2819f 65 20 77 69 74 68 20 65 78 69 73 74 69 6e 67 20  e with existing 
281a0 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  entry */.  int i
281a1 73 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f  sUpdate,       /
281a2 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54  * True for UPDAT
281a3 45 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53  E, False for INS
281a4 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65  ERT */.  int ove
281a5 72 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20  rrideError,  /* 
281a6 4f 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72  Override onError
281a7 20 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20   to this if not 
281a8 4f 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20  OE_Default */.  
281a9 69 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 2c 20  int ignoreDest, 
281aa 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74      /* Jump to t
281ab 68 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20  his label on an 
281ac 4f 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75  OE_Ignore resolu
281ad 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  tion */.  int *p
281ae 62 4d 61 79 52 65 70 6c 61 63 65 20 20 20 2f 2a  bMayReplace   /*
281af 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72 75   OUT: Set to tru
281b0 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20  e if constraint 
281b1 6d 61 79 20 63 61 75 73 65 20 61 20 72 65 70 6c  may cause a repl
281b2 61 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ace */.){.  int 
281b3 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
281b4 2f 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* loop counter 
281b5 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
281b6 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
281b7 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 74 69   under construti
281b8 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  on */.  int nCol
281b9 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
281ba 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
281bb 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
281bc 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  r;        /* Con
281bd 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
281be 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 69   strategy */.  i
281bf 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
281c0 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73 20 6f     /* Addresss o
281c1 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  f jump instructi
281c2 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32 20 3d  on */.  int j2 =
281c3 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a 20 41   0, j3;     /* A
281c4 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70  ddresses of jump
281c5 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f   instructions */
281c6 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20  .  int regData; 
281c7 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
281c8 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69  er containing fi
281c9 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d 6e 20  rst data column 
281ca 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20  */.  int iCur;  
281cb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
281cc 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
281cd 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
281ce 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  ;         /* Poi
281cf 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66 20 74  nter to one of t
281d0 68 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  he indices */.  
281d1 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 65 20  int seenReplace 
281d2 3d 20 30 3b 20 2f 2a 20 54 72 75 65 20 69 66 20  = 0; /* True if 
281d3 52 45 50 4c 41 43 45 20 69 73 20 75 73 65 64 20  REPLACE is used 
281d4 74 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54 20 50  to resolve INT P
281d5 4b 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20  K conflict */.  
281d6 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 20  int regOldRowid 
281d7 3d 20 28 72 6f 77 69 64 43 68 6e 67 20 26 26 20  = (rowidChng && 
281d8 69 73 55 70 64 61 74 65 29 20 3f 20 72 6f 77 69  isUpdate) ? rowi
281d9 64 43 68 6e 67 20 3a 20 72 65 67 52 6f 77 69 64  dChng : regRowid
281da 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  ;..  v = sqlite3
281db 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
281dc 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
281dd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  );.  assert( pTa
281de 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b  b->pSelect==0 );
281df 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20    /* This table 
281e0 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f  is not a VIEW */
281e1 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e  .  nCol = pTab->
281e2 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74 61 20  nCol;.  regData 
281e3 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a  = regRowid + 1;.
281e4 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 4e  .  /* Test all N
281e5 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
281e6 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  nts..  */.  for(
281e7 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
281e8 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 54  ){.    if( i==pT
281e9 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
281ea 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
281eb 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d   }.    onError =
281ec 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e   pTab->aCol[i].n
281ed 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66 28 20  otNull;.    if( 
281ee 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65  onError==OE_None
281ef 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
281f0 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72 72   if( overrideErr
281f1 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29  or!=OE_Default )
281f2 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20  {.      onError 
281f3 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b  = overrideError;
281f4 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
281f5 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
281f6 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
281f7 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
281f8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 6e      }.    if( on
281f9 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
281fa 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  e && pTab->aCol[
281fb 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b 0a 20  i].pDflt==0 ){. 
281fc 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
281fd 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
281fe 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
281ff 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or==OE_Rollback 
28200 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41  || onError==OE_A
28201 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  bort || onError=
28202 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20  =OE_Fail.       
28203 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
28204 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f  Ignore || onErro
28205 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b  r==OE_Replace );
28206 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
28207 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63 61  rror ){.      ca
28208 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20  se OE_Abort:.   
28209 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41       sqlite3MayA
2820a 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20  bort(pParse);.  
2820b 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c      case OE_Roll
2820c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65  back:.      case
2820d 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20   OE_Fail: {.    
2820e 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a      char *zMsg;.
2820f 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c          j1 = sql
28210 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
28211 2c 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 2c  , OP_HaltIfNull,
28212 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28214 20 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52     SQLITE_CONSTR
28215 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 72  AINT, onError, r
28216 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20  egData+i);.     
28217 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65     zMsg = sqlite
28218 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d  3MPrintf(pParse-
28219 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79 20  >db, "%s.%s may 
2821a 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20 20  not be NULL",.  
2821b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2821c 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2821d 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61  ->zName, pTab->a
2821e 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  Col[i].zName);. 
2821f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28220 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
28221 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d  , zMsg, P4_DYNAM
28222 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  IC);.        bre
28223 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
28224 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
28225 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
28226 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28227 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 44   OP_IsNull, regD
28228 61 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44 65 73  ata+i, ignoreDes
28229 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  t);.        brea
2822a 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2822b 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
2822c 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72      assert( onEr
2822d 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror==OE_Replace 
2822e 29 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20  );.        j1 = 
2822f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28230 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c  1(v, OP_NotNull,
28231 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
28232 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
28233 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61  Code(pParse, pTa
28234 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
28235 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
28236 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28237 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
28238 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
28239 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2823a 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c   }..  /* Test al
2823b 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
2823c 6e 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  nts.  */.#ifndef
2823d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
2823e 43 4b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  CK.  if( pTab->p
2823f 43 68 65 63 6b 20 26 26 20 28 70 50 61 72 73 65  Check && (pParse
28240 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
28241 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b  LITE_IgnoreCheck
28242 73 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  s)==0 ){.    int
28243 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33   allOk = sqlite3
28244 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
28245 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b  ;.    pParse->ck
28246 42 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b 0a  Base = regData;.
28247 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
28248 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 54  fTrue(pParse, pT
28249 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f  ab->pCheck, allO
2824a 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  k, SQLITE_JUMPIF
2824b 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45 72 72  NULL);.    onErr
2824c 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
2824d 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f  or!=OE_Default ?
2824e 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 3a   overrideError :
2824f 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69   OE_Abort;.    i
28250 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49  f( onError==OE_I
28251 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 73  gnore ){.      s
28252 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28253 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
28254 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20  ignoreDest);.   
28255 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
28256 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
28257 69 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e 45 72  int(pParse, onEr
28258 72 6f 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ror, 0, 0);.    
28259 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2825a 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2825b 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e   allOk);.  }.#en
2825c 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
2825d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
2825e 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77  K) */..  /* If w
2825f 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47 45  e have an INTEGE
28260 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6d  R PRIMARY KEY, m
28261 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 72 69  ake sure the pri
28262 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 66  mary key.  ** of
28263 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
28264 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f 75  does not previou
28265 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63 65  sly exist.  Exce
28266 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a 2a  pt, if this.  **
28267 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 61 6e   is an UPDATE an
28268 64 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  d the primary ke
28269 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e  y is not changin
2826a 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a 20  g, that is OK.. 
2826b 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64 43   */.  if( rowidC
2826c 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72  hng ){.    onErr
2826d 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f  or = pTab->keyCo
2826e 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72  nf;.    if( over
2826f 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65  rideError!=OE_De
28270 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f  fault ){.      o
28271 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64  nError = overrid
28272 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  eError;.    }els
28273 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f  e if( onError==O
28274 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
28275 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f     onError = OE_
28276 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20  Abort;.    }.   
28277 20 0a 20 20 20 20 69 66 28 20 69 73 55 70 64 61   .    if( isUpda
28278 74 65 20 29 7b 0a 20 20 20 20 20 20 6a 32 20 3d  te ){.      j2 =
28279 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2827a 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67  p3(v, OP_Eq, reg
2827b 52 6f 77 69 64 2c 20 30 2c 20 72 6f 77 69 64 43  Rowid, 0, rowidC
2827c 68 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  hng);.    }.    
2827d 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j3 = sqlite3Vdbe
2827e 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
2827f 45 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c  Exists, baseCur,
28280 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20   0, regRowid);. 
28281 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72     switch( onErr
28282 6f 72 20 29 7b 0a 20 20 20 20 20 20 64 65 66 61  or ){.      defa
28283 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6f  ult: {.        o
28284 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
28285 74 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61  t;.        /* Fa
28286 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65  ll thru into the
28287 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
28288 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
28289 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20   OE_Rollback:.  
2828a 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72      case OE_Abor
2828b 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  t:.      case OE
2828c 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20  _Fail: {.       
2828d 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
2828e 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20  traint(.        
2828f 20 20 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f    pParse, onErro
28290 72 2c 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20  r, "PRIMARY KEY 
28291 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c  must be unique",
28292 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
28293 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
28294 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
28295 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20  E_Replace: {.   
28296 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
28297 20 61 72 65 20 44 45 4c 45 54 45 20 74 72 69 67   are DELETE trig
28298 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62  gers on this tab
28299 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20  le and the.     
2829a 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 2d     ** recursive-
2829b 74 72 69 67 67 65 72 73 20 66 6c 61 67 20 69 73  triggers flag is
2829c 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72   set, call Gener
2829d 61 74 65 52 6f 77 44 65 6c 65 74 65 28 29 20 74  ateRowDelete() t
2829e 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 6d  o.        ** rem
2829f 6f 76 65 20 74 68 65 20 63 6f 6e 66 6c 69 63 74  ove the conflict
282a0 69 6e 67 20 72 6f 77 20 66 72 6f 6d 20 74 68 65  ing row from the
282a1 20 74 68 65 20 74 61 62 6c 65 2e 20 54 68 69 73   the table. This
282a2 20 77 69 6c 6c 20 66 69 72 65 0a 20 20 20 20 20   will fire.     
282a3 20 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65     ** the trigge
282a4 72 73 20 61 6e 64 20 72 65 6d 6f 76 65 20 62 6f  rs and remove bo
282a5 74 68 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  th the table and
282a6 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65 6e   index b-tree en
282a7 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  tries..        *
282a8 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68  *.        ** Oth
282a9 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 72 65  erwise, if there
282aa 20 61 72 65 20 6e 6f 20 74 72 69 67 67 65 72 73   are no triggers
282ab 20 6f 72 20 74 68 65 20 72 65 63 75 72 73 69 76   or the recursiv
282ac 65 2d 74 72 69 67 67 65 72 73 0a 20 20 20 20 20  e-triggers.     
282ad 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 6e 6f     ** flag is no
282ae 74 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65  t set, call Gene
282af 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
282b0 74 65 28 29 2e 20 54 68 69 73 20 72 65 6d 6f 76  te(). This remov
282b1 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  es.        ** th
282b2 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65  e index b-tree e
282b3 6e 74 72 69 65 73 20 6f 6e 6c 79 2e 20 54 68 65  ntries only. The
282b4 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 65 6e   table b-tree en
282b5 74 72 79 20 77 69 6c 6c 20 62 65 20 0a 20 20 20  try will be .   
282b6 20 20 20 20 20 2a 2a 20 72 65 70 6c 61 63 65 64       ** replaced
282b7 20 62 79 20 74 68 65 20 6e 65 77 20 65 6e 74 72   by the new entr
282b8 79 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 73  y when it is ins
282b9 65 72 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20  erted.  */.     
282ba 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69     Trigger *pTri
282bb 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  gger = 0;.      
282bc 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
282bd 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
282be 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20  ecTriggers ){.  
282bf 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
282c0 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
282c1 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
282c2 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
282c3 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
282c4 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
282c5 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50 61 72  3MultiWrite(pPar
282c6 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  se);.        if(
282c7 20 70 54 72 69 67 67 65 72 20 7c 7c 20 73 71 6c   pTrigger || sql
282c8 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70  ite3FkRequired(p
282c9 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
282ca 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  0) ){.          
282cb 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
282cc 6f 77 44 65 6c 65 74 65 28 0a 20 20 20 20 20 20  owDelete(.      
282cd 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
282ce 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
282cf 65 67 52 6f 77 69 64 2c 20 30 2c 20 70 54 72 69  egRowid, 0, pTri
282d0 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65  gger, OE_Replace
282d1 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20  .          );.  
282d2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
282d3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65         sqlite3Ge
282d4 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65  nerateRowIndexDe
282d5 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 61  lete(pParse, pTa
282d6 62 2c 20 62 61 73 65 43 75 72 2c 20 30 29 3b 0a  b, baseCur, 0);.
282d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
282d8 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20    seenReplace = 
282d9 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  1;.        break
282da 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
282db 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20  case OE_Ignore: 
282dc 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
282dd 28 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30  ( seenReplace==0
282de 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
282df 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
282e0 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e   OP_Goto, 0, ign
282e1 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20  oreDest);.      
282e2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
282e3 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
282e4 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
282e5 2c 20 6a 33 29 3b 0a 20 20 20 20 69 66 28 20 69  , j3);.    if( i
282e6 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20  sUpdate ){.     
282e7 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
282e8 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20  Here(v, j2);.   
282e9 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73   }.  }..  /* Tes
282ea 74 20 61 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e  t all UNIQUE con
282eb 73 74 72 61 69 6e 74 73 20 62 79 20 63 72 65 61  straints by crea
282ec 74 69 6e 67 20 65 6e 74 72 69 65 73 20 66 6f 72  ting entries for
282ed 20 65 61 63 68 20 55 4e 49 51 55 45 0a 20 20 2a   each UNIQUE.  *
282ee 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69  * index and maki
282ef 6e 67 20 73 75 72 65 20 74 68 61 74 20 64 75 70  ng sure that dup
282f0 6c 69 63 61 74 65 20 65 6e 74 72 69 65 73 20 64  licate entries d
282f1 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  o not already ex
282f2 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68  ist..  ** Add th
282f3 65 20 6e 65 77 20 72 65 63 6f 72 64 73 20 74 6f  e new records to
282f4 20 74 68 65 20 69 6e 64 69 63 65 73 20 61 73 20   the indices as 
282f5 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f  we go..  */.  fo
282f6 72 28 69 43 75 72 3d 30 2c 20 70 49 64 78 3d 70  r(iCur=0, pIdx=p
282f7 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
282f8 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
282f9 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20  ext, iCur++){.  
282fa 20 20 69 6e 74 20 72 65 67 49 64 78 3b 0a 20 20    int regIdx;.  
282fb 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20    int regR;..   
282fc 20 69 66 28 20 61 52 65 67 49 64 78 5b 69 43 75   if( aRegIdx[iCu
282fd 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  r]==0 ) continue
282fe 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65  ;  /* Skip unuse
282ff 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20  d indices */..  
28300 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6b 65    /* Create a ke
28301 79 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  y for accessing 
28302 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
28303 2a 2f 0a 20 20 20 20 72 65 67 49 64 78 20 3d 20  */.    regIdx = 
28304 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
28305 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 64 78  nge(pParse, pIdx
28306 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20  ->nColumn+1);.  
28307 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
28308 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
28309 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20  {.      int idx 
2830a 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
2830b 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69  [i];.      if( i
2830c 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  dx==pTab->iPKey 
2830d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2830e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2830f 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77  OP_SCopy, regRow
28310 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20  id, regIdx+i);. 
28311 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
28312 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28313 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
28314 79 2c 20 72 65 67 44 61 74 61 2b 69 64 78 2c 20  y, regData+idx, 
28315 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 20  regIdx+i);.     
28316 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
28317 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28318 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52  , OP_SCopy, regR
28319 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b  owid, regIdx+i);
2831a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2831b 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
2831c 65 52 65 63 6f 72 64 2c 20 72 65 67 49 64 78 2c  eRecord, regIdx,
2831d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31   pIdx->nColumn+1
2831e 2c 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29  , aRegIdx[iCur])
2831f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28320 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
28321 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
28322 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78  inityStr(v, pIdx
28323 29 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  ), 0);.    sqlit
28324 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
28325 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
28326 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e  , regIdx, pIdx->
28327 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20  nColumn+1);..   
28328 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61   /* Find out wha
28329 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  t action to take
2832a 20 69 6e 20 63 61 73 65 20 74 68 65 72 65 20 69   in case there i
2832b 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f  s an indexing co
2832c 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e  nflict */.    on
2832d 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e  Error = pIdx->on
2832e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f  Error;.    if( o
2832f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20  nError==OE_None 
28330 29 7b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ){ .      sqlite
28331 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
28332 65 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78  e(pParse, regIdx
28333 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  , pIdx->nColumn+
28334 31 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  1);.      contin
28335 75 65 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20  ue;  /* pIdx is 
28336 6e 6f 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64  not a UNIQUE ind
28337 65 78 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ex */.    }.    
28338 69 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f  if( overrideErro
28339 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r!=OE_Default ){
2833a 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
2833b 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a   overrideError;.
2833c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e      }else if( on
2833d 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
2833e 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
2833f 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or = OE_Abort;. 
28340 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65     }.    if( see
28341 6e 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  nReplace ){.    
28342 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
28343 45 5f 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72  E_Ignore ) onErr
28344 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b  or = OE_Replace;
28345 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
28346 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c  onError==OE_Fail
28347 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f   ) onError = OE_
28348 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20  Abort;.    }.   
28349 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74   .    /* Check t
2834a 6f 20 73 65 65 20 69 66 20 74 68 65 20 6e 65 77  o see if the new
2834b 20 69 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c   index entry wil
2834c 6c 20 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20  l be unique */. 
2834d 20 20 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65     regR = sqlite
2834e 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2834f 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
28350 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28351 5f 53 43 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f  _SCopy, regOldRo
28352 77 69 64 2c 20 72 65 67 52 29 3b 0a 20 20 20 20  wid, regR);.    
28353 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j3 = sqlite3Vdbe
28354 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55  AddOp4(v, OP_IsU
28355 6e 69 71 75 65 2c 20 62 61 73 65 43 75 72 2b 69  nique, baseCur+i
28356 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20  Cur+1, 0,.      
28357 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28358 20 20 20 20 20 72 65 67 52 2c 20 53 51 4c 49 54       regR, SQLIT
28359 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67  E_INT_TO_PTR(reg
2835a 49 64 78 29 2c 0a 20 20 20 20 20 20 20 20 20 20  Idx),.          
2835b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2835c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20   P4_INT32);.    
2835d 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2835e 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2835f 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
28360 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f  olumn+1);..    /
28361 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
28362 74 68 61 74 20 65 78 65 63 75 74 65 73 20 69 66  that executes if
28363 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65   the new index e
28364 6e 74 72 79 20 69 73 20 6e 6f 74 20 75 6e 69 71  ntry is not uniq
28365 75 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ue */.    assert
28366 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f  ( onError==OE_Ro
28367 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f  llback || onErro
28368 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f  r==OE_Abort || o
28369 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a  nError==OE_Fail.
2836a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72          || onErr
2836b 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c  or==OE_Ignore ||
2836c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70   onError==OE_Rep
2836d 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74  lace );.    swit
2836e 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  ch( onError ){. 
2836f 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c       case OE_Rol
28370 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73  lback:.      cas
28371 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20  e OE_Abort:.    
28372 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20    case OE_Fail: 
28373 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b  {.        int j;
28374 0a 20 20 20 20 20 20 20 20 53 74 72 41 63 63 75  .        StrAccu
28375 6d 20 65 72 72 4d 73 67 3b 0a 20 20 20 20 20 20  m errMsg;.      
28376 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
28377 65 70 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ep;.        char
28378 20 2a 7a 45 72 72 3b 0a 0a 20 20 20 20 20 20 20   *zErr;..       
28379 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
2837a 49 6e 69 74 28 26 65 72 72 4d 73 67 2c 20 30 2c  Init(&errMsg, 0,
2837b 20 30 2c 20 32 30 30 29 3b 0a 20 20 20 20 20 20   0, 200);.      
2837c 20 20 65 72 72 4d 73 67 2e 64 62 20 3d 20 70 50    errMsg.db = pP
2837d 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20  arse->db;.      
2837e 20 20 7a 53 65 70 20 3d 20 70 49 64 78 2d 3e 6e    zSep = pIdx->n
2837f 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75  Column>1 ? "colu
28380 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75 6d 6e 20  mns " : "column 
28381 22 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  ";.        for(j
28382 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; j<pIdx->nCol
28383 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  umn; j++){.     
28384 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20       char *zCol 
28385 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64  = pTab->aCol[pId
28386 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e  x->aiColumn[j]].
28387 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
28388 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
28389 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20  Append(&errMsg, 
2838a 7a 53 65 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zSep, -1);.     
2838b 20 20 20 20 20 7a 53 65 70 20 3d 20 22 2c 20 22       zSep = ", "
2838c 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2838d 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
2838e 64 28 26 65 72 72 4d 73 67 2c 20 7a 43 6f 6c 2c  d(&errMsg, zCol,
2838f 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a   -1);.        }.
28390 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
28391 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65  trAccumAppend(&e
28392 72 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20  rrMsg,.         
28393 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e     pIdx->nColumn
28394 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74 20 75  >1 ? " are not u
28395 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20 6e 6f  nique" : " is no
28396 74 20 75 6e 69 71 75 65 22 2c 20 2d 31 29 3b 0a  t unique", -1);.
28397 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73          zErr = s
28398 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
28399 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b 0a 20  nish(&errMsg);. 
2839a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61         sqlite3Ha
2839b 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  ltConstraint(pPa
2839c 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 45  rse, onError, zE
2839d 72 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  rr, 0);.        
2839e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 65 72  sqlite3DbFree(er
2839f 72 4d 73 67 2e 64 62 2c 20 7a 45 72 72 29 3b 0a  rMsg.db, zErr);.
283a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
283a1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
283a2 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20  e OE_Ignore: {. 
283a3 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
283a4 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b  eenReplace==0 );
283a5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
283a6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
283a7 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
283a8 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62  Dest);.        b
283a9 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
283aa 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
283ab 20 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a         Trigger *
283ac 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20  pTrigger = 0;.  
283ad 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e        assert( on
283ae 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
283af 65 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e );.        sql
283b0 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70  ite3MultiWrite(p
283b1 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
283b2 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  if( pParse->db->
283b3 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63  flags&SQLITE_Rec
283b4 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20  Triggers ){.    
283b5 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20 3d        pTrigger =
283b6 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73   sqlite3Triggers
283b7 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  Exist(pParse, pT
283b8 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30  ab, TK_DELETE, 0
283b9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 0);.        }.
283ba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
283bb 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65  enerateRowDelete
283bc 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  (.            pP
283bd 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65  arse, pTab, base
283be 43 75 72 2c 20 72 65 67 52 2c 20 30 2c 20 70 54  Cur, regR, 0, pT
283bf 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61  rigger, OE_Repla
283c0 63 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20  ce.        );.  
283c1 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63        seenReplac
283c2 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62  e = 1;.        b
283c3 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
283c4 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
283c5 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
283c6 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  3);.    sqlite3R
283c7 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
283c8 61 72 73 65 2c 20 72 65 67 52 29 3b 0a 20 20 7d  arse, regR);.  }
283c9 0a 20 20 0a 20 20 69 66 28 20 70 62 4d 61 79 52  .  .  if( pbMayR
283ca 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 2a 70  eplace ){.    *p
283cb 62 4d 61 79 52 65 70 6c 61 63 65 20 3d 20 73 65  bMayReplace = se
283cc 65 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d 0a 7d  enReplace;.  }.}
283cd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
283ce 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63  tine generates c
283cf 6f 64 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68  ode to finish th
283d0 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  e INSERT or UPDA
283d1 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  TE operation.** 
283d2 74 68 61 74 20 77 61 73 20 73 74 61 72 74 65 64  that was started
283d3 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c   by a prior call
283d4 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72   to sqlite3Gener
283d5 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
283d6 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63  cks..** A consec
283d7 75 74 69 76 65 20 72 61 6e 67 65 20 6f 66 20 72  utive range of r
283d8 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
283d9 67 20 61 74 20 72 65 67 52 6f 77 69 64 20 63 6f  g at regRowid co
283da 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f  ntains the.** ro
283db 77 69 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74  wid and the cont
283dc 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ent to be insert
283dd 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72  ed..**.** The ar
283de 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
283df 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62  routine should b
283e0 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
283e1 65 20 66 69 72 73 74 20 73 69 78 0a 2a 2a 20 61  e first six.** a
283e2 72 67 75 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69  rguments to sqli
283e3 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
283e4 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a  raintChecks..*/.
283e5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
283e6 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c  oid sqlite3Compl
283e7 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20  eteInsertion(.  
283e8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
283e9 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
283ea 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
283eb 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
283ec 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20     /* the table 
283ed 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72  into which we ar
283ee 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20  e inserting */. 
283ef 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20   int baseCur,   
283f0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
283f1 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   a read/write cu
283f2 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
283f3 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72   pTab */.  int r
283f4 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f  egRowid,       /
283f5 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65  * Range of conte
283f6 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65  nt */.  int *aRe
283f7 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52  gIdx,       /* R
283f8 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20  egister used by 
283f9 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66  each index.  0 f
283fa 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65  or unused indice
283fb 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64  s */.  int isUpd
283fc 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72  ate,       /* Tr
283fd 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46  ue for UPDATE, F
283fe 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20  alse for INSERT 
283ff 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42  */.  int appendB
28400 69 61 73 2c 20 20 20 20 20 2f 2a 20 54 72 75 65  ias,     /* True
28401 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65   if this is like
28402 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
28403 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53  nd */.  int useS
28404 65 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a 20 54  eekResult   /* T
28405 72 75 65 20 74 6f 20 73 65 74 20 74 68 65 20 55  rue to set the U
28406 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
28407 67 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73  g on OP_[Idx]Ins
28408 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ert */.){.  int 
28409 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  i;.  Vdbe *v;.  
2840a 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65  int nIdx;.  Inde
2840b 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20 70 69  x *pIdx;.  u8 pi
2840c 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72  k_flags;.  int r
2840d 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65  egData;.  int re
2840e 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  gRec;..  v = sql
2840f 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
28410 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
28411 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
28412 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
28413 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
28414 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
28415 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d  W */.  for(nIdx=
28416 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
28417 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
28418 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
28419 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d  dx++){}.  for(i=
2841a 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nIdx-1; i>=0; i-
2841b 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67  -){.    if( aReg
2841c 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  Idx[i]==0 ) cont
2841d 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  inue;.    sqlite
2841e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2841f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73  P_IdxInsert, bas
28420 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64  eCur+i+1, aRegId
28421 78 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 75  x[i]);.    if( u
28422 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a  seSeekResult ){.
28423 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28424 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
28425 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
28426 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  T);.    }.  }.  
28427 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77  regData = regRow
28428 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52 65 63  id + 1;.  regRec
28429 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2842a 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2842b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2842c 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
2842d 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61  rd, regData, pTa
2842e 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29  b->nCol, regRec)
2842f 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
28430 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
28431 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Tab);.  sqlite3E
28432 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
28433 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
28434 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43  egData, pTab->nC
28435 6f 6c 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  ol);.  if( pPars
28436 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20  e->nested ){.   
28437 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a   pik_flags = 0;.
28438 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b    }else{.    pik
28439 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f  _flags = OPFLAG_
2843a 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b  NCHANGE;.    pik
2843b 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64  _flags |= (isUpd
2843c 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44  ate?OPFLAG_ISUPD
2843d 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52  ATE:OPFLAG_LASTR
2843e 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28  OWID);.  }.  if(
2843f 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20   appendBias ){. 
28440 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20     pik_flags |= 
28441 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20  OPFLAG_APPEND;. 
28442 20 7d 0a 20 20 69 66 28 20 75 73 65 53 65 65 6b   }.  if( useSeek
28443 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 70 69  Result ){.    pi
28444 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41  k_flags |= OPFLA
28445 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 3b  G_USESEEKRESULT;
28446 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
28447 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
28448 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20  nsert, baseCur, 
28449 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
2844a 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  );.  if( !pParse
2844b 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
2844c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2844d 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP4(v, -1, pTab-
2844e 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  >zName, P4_STATI
2844f 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  C);.  }.  sqlite
28450 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
28451 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a   pik_flags);.}..
28452 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
28453 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70  ode that will op
28454 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  en cursors for a
28455 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61   table and for a
28456 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66  ll.** indices of
28457 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68   that table.  Th
28458 65 20 22 62 61 73 65 43 75 72 22 20 70 61 72 61  e "baseCur" para
28459 6d 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72  meter is the cur
2845a 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 0a  sor number used.
2845b 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
2845c 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 6f  .  Indices are o
2845d 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65 71 75  pened on subsequ
2845e 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a  ent cursors..**.
2845f 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
28460 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20  mber of indices 
28461 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f  on the table..*/
28462 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28463 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
28464 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 0a  ableAndIndices(.
28465 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
28466 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
28467 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
28468 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54   *pTab,     /* T
28469 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65  able to be opene
2846a 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43  d */.  int baseC
2846b 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  ur,     /* Curso
2846c 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e 65  r number assigne
2846d 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a  d to the table *
2846e 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20 20  /.  int op      
2846f 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52       /* OP_OpenR
28470 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72  ead or OP_OpenWr
28471 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ite */.){.  int 
28472 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  i;.  int iDb;.  
28473 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 56  Index *pIdx;.  V
28474 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 49  dbe *v;..  if( I
28475 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
28476 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44 62   return 0;.  iDb
28477 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
28478 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
28479 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2847a 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  a);.  v = sqlite
2847b 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2847c 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
2847d 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65   );.  sqlite3Ope
2847e 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 62  nTable(pParse, b
2847f 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54 61  aseCur, iDb, pTa
28480 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d  b, op);.  for(i=
28481 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  1, pIdx=pTab->pI
28482 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
28483 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b  =pIdx->pNext, i+
28484 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20  +){.    KeyInfo 
28485 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
28486 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
28487 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 61  se, pIdx);.    a
28488 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63  ssert( pIdx->pSc
28489 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
2848a 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ema );.    sqlit
2848b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2848c 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20 70  op, i+baseCur, p
2848d 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
2848e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2848f 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
28490 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
28491 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65  NDOFF);.    Vdbe
28492 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
28493 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
28494 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
28495 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75 72 2b  e->nTab<baseCur+
28496 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  i ){.    pParse-
28497 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75 72 2b  >nTab = baseCur+
28498 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
28499 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20  i-1;.}...#ifdef 
2849a 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
2849b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2849c 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
2849d 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
2849e 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 74  henever the.** t
2849f 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
284a0 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20 54  tion is used.  T
284a1 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
284a2 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f  testing.** purpo
284a3 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61  ses only - to ma
284a4 6b 65 20 73 75 72 65 20 74 68 65 20 74 72 61 6e  ke sure the tran
284a5 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
284a6 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68  n really.** is h
284a7 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20 69 74  appening when it
284a8 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a   is suppose to..
284a9 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
284aa 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70  t sqlite3_xferop
284ab 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20  t_count;.#endif 
284ac 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
284ad 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
284ae 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
284af 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
284b0 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20  collation names 
284b1 74 6f 20 73 65 65 20 69 66 20 74 68 65 79 20 61  to see if they a
284b2 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  re compatible..*
284b3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65  /.static int xfe
284b4 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
284b5 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  tion(const char 
284b6 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *z1, const char 
284b7 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d  *z2){.  if( z1==
284b8 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
284b9 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28  z2==0;.  }.  if(
284ba 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   z2==0 ){.    re
284bb 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65  turn 0;.  }.  re
284bc 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 49  turn sqlite3StrI
284bd 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a  Cmp(z1, z2)==0;.
284be 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
284bf 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78 20  to see if index 
284c0 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74 69 62  pSrc is compatib
284c1 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65 20 6f  le as a source o
284c2 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e  f data.** for in
284c3 64 65 78 20 70 44 65 73 74 20 69 6e 20 61 6e 20  dex pDest in an 
284c4 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65 72 20  insert transfer 
284c5 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54  optimization.  T
284c6 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20  he rules.** for 
284c7 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64  a compatible ind
284c8 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ex:.**.**    *  
284c9 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 6f 76   The index is ov
284ca 65 72 20 74 68 65 20 73 61 6d 65 20 73 65 74 20  er the same set 
284cb 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20  of columns.**   
284cc 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 44 45   *   The same DE
284cd 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72 6b 69  SC and ASC marki
284ce 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c  ngs occurs on al
284cf 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20  l columns.**    
284d0 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f 6e 45  *   The same onE
284d1 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20  rror processing 
284d2 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67  (OE_Abort, OE_Ig
284d3 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20  nore, etc).**   
284d4 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 63 6f   *   The same co
284d5 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
284d6 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a   on each column.
284d7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
284d8 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65  erCompatibleInde
284d9 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74 2c 20  x(Index *pDest, 
284da 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a 20 20  Index *pSrc){.  
284db 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
284dc 20 70 44 65 73 74 20 26 26 20 70 53 72 63 20 29   pDest && pSrc )
284dd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73  ;.  assert( pDes
284de 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63 2d  t->pTable!=pSrc-
284df 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69 66 28  >pTable );.  if(
284e0 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21   pDest->nColumn!
284e1 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  =pSrc->nColumn )
284e2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
284e3 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 6e    /* Different n
284e4 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
284e5 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
284e6 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53  est->onError!=pS
284e7 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  rc->onError ){. 
284e8 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
284e9 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6e 66  * Different conf
284ea 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
284eb 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a 20 20  strategies */.  
284ec 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
284ed 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Src->nColumn; i+
284ee 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53 72 63  +){.    if( pSrc
284ef 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70  ->aiColumn[i]!=p
284f0 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  Dest->aiColumn[i
284f1 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ] ){.      retur
284f2 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
284f3 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65  ent columns inde
284f4 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  xed */.    }.   
284f5 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f 72 74   if( pSrc->aSort
284f6 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d  Order[i]!=pDest-
284f7 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29  >aSortOrder[i] )
284f8 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
284f9 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
284fa 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a   sort orders */.
284fb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 78      }.    if( !x
284fc 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
284fd 6c 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61 7a 43  lation(pSrc->azC
284fe 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e 61 7a  oll[i],pDest->az
284ff 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20  Coll[i]) ){.    
28500 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
28501 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61   Different colla
28502 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a  ting sequences *
28503 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  /.    }.  }..  /
28504 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61 62 6f  * If no test abo
28505 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20 74 68  ve fails then th
28506 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74 20 62  e indices must b
28507 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a 2f 0a  e compatible */.
28508 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
28509 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 68 65  *.** Attempt the
2850a 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
2850b 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45 52 54  zation on INSERT
2850c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
2850d 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 49  .**     INSERT I
2850e 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43 54 20  NTO tab1 SELECT 
2850f 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a  * FROM tab2;.**.
28510 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a 61  ** This optimiza
28511 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 74 74  tion is only att
28512 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20  empted if.**.** 
28513 20 20 20 28 31 29 20 20 74 61 62 31 20 61 6e 64     (1)  tab1 and
28514 20 74 61 62 32 20 68 61 76 65 20 69 64 65 6e 74   tab2 have ident
28515 69 63 61 6c 20 73 63 68 65 6d 61 73 20 69 6e 63  ical schemas inc
28516 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 0a 2a  luding all the.*
28517 2a 20 20 20 20 20 20 20 20 20 73 61 6d 65 20 69  *         same i
28518 6e 64 69 63 65 73 20 61 6e 64 20 63 6f 6e 73 74  ndices and const
28519 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20  raints.**.**    
2851a 28 32 29 20 20 74 61 62 31 20 61 6e 64 20 74 61  (2)  tab1 and ta
2851b 62 32 20 61 72 65 20 64 69 66 66 65 72 65 6e 74  b2 are different
2851c 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20   tables.**.**   
2851d 20 28 33 29 20 20 54 68 65 72 65 20 6d 75 73 74   (3)  There must
2851e 20 62 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20   be no triggers 
2851f 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20  on tab1.**.**   
28520 20 28 34 29 20 20 54 68 65 20 72 65 73 75 6c 74   (4)  The result
28521 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
28522 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
28523 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28 35 29  "*".**.**    (5)
28524 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61    The SELECT sta
28525 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20 57 48  tement has no WH
28526 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f 52 44  ERE, HAVING, ORD
28527 45 52 20 42 59 2c 20 47 52 4f 55 50 20 42 59 2c  ER BY, GROUP BY,
28528 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 72 20 4c  .**         or L
28529 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  IMIT clause..**.
2852a 2a 2a 20 20 20 20 28 36 29 20 20 54 68 65 20 53  **    (6)  The S
2852b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2852c 69 73 20 61 20 73 69 6d 70 6c 65 20 28 6e 6f 74  is a simple (not
2852d 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73 65 6c   a compound) sel
2852e 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  ect that.**     
2852f 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c      contains onl
28530 79 20 74 61 62 32 20 69 6e 20 69 74 73 20 46 52  y tab2 in its FR
28531 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20  OM clause.**.** 
28532 54 68 69 73 20 6d 65 74 68 6f 64 20 66 6f 72 20  This method for 
28533 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65  implementing the
28534 20 49 4e 53 45 52 54 20 74 72 61 6e 73 66 65 72   INSERT transfer
28535 73 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72  s raw records fr
28536 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65 72 20  om.** tab2 over 
28537 74 6f 20 74 61 62 31 2e 20 20 54 68 65 20 63 6f  to tab1.  The co
28538 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 64 65  lumns are not de
28539 63 6f 64 65 64 2e 20 20 52 61 77 20 72 65 63 6f  coded.  Raw reco
2853a 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  rds from.** the 
2853b 69 6e 64 69 63 65 73 20 6f 66 20 74 61 62 32 20  indices of tab2 
2853c 61 72 65 20 74 72 61 6e 73 66 65 72 65 64 20 74  are transfered t
2853d 6f 20 74 61 62 31 20 61 73 20 77 65 6c 6c 2e 20  o tab1 as well. 
2853e 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a   In so doing,.**
2853f 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74   the resulting t
28540 61 62 31 20 68 61 73 20 6d 75 63 68 20 6c 65 73  ab1 has much les
28541 73 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 2e  s fragmentation.
28542 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
28543 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52 55 45  ine returns TRUE
28544 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   if the optimiza
28545 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 65  tion is attempte
28546 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66  d.  If any.** of
28547 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20   the conditions 
28548 61 62 6f 76 65 20 66 61 69 6c 20 73 6f 20 74 68  above fail so th
28549 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  at the optimizat
2854a 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a  ion should not.*
2854b 2a 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c 20  * be attempted, 
2854c 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
2854d 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 2e  e returns FALSE.
2854e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
2854f 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28  ferOptimization(
28550 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
28551 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
28552 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
28553 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20 20  Table *pDest,   
28554 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
28555 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74  le we are insert
28556 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53 65  ing into */.  Se
28557 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
28558 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20      /* A SELECT 
28559 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65  statement to use
2855a 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75   as the data sou
2855b 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45  rce */.  int onE
2855c 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f  rror,          /
2855d 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20  * How to handle 
2855e 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72  constraint error
2855f 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44 65  s */.  int iDbDe
28560 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st           /* 
28561 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66 20  The database of 
28562 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20 45 78  pDest */.){.  Ex
28563 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
28564 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28565 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
28566 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a   of the SELECT *
28567 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 72 63 3b  /.  Table *pSrc;
28568 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28569 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2856a 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
2856b 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20 2a  ause of SELECT *
2856c 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53 72 63 49  /.  Index *pSrcI
2856d 64 78 2c 20 2a 70 44 65 73 74 49 64 78 3b 20 20  dx, *pDestIdx;  
2856e 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 61       /* Source a
2856f 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  nd destination i
28570 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73 74 72 75  ndices */.  stru
28571 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
28572 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 2f 2a 20  *pItem;      /* 
28573 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53  An element of pS
28574 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f 0a 20  elect->pSrc */. 
28575 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
28576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28577 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
28578 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 53 72  r */.  int iDbSr
28579 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
2857a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2857b 61 74 61 62 61 73 65 20 6f 66 20 70 53 72 63 20  atabase of pSrc 
2857c 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 69  */.  int iSrc, i
2857d 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Dest;           
2857e 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73        /* Cursors
2857f 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 61 6e 64   from source and
28580 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a   destination */.
28581 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64    int addr1, add
28582 72 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r2;             
28583 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64 72 65     /* Loop addre
28584 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 65 6d  sses */.  int em
28585 70 74 79 44 65 73 74 54 65 73 74 3b 20 20 20 20  ptyDestTest;    
28586 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
28587 64 72 65 73 73 20 6f 66 20 74 65 73 74 20 66 6f  dress of test fo
28588 72 20 65 6d 70 74 79 20 70 44 65 73 74 20 2a 2f  r empty pDest */
28589 0a 20 20 69 6e 74 20 65 6d 70 74 79 53 72 63 54  .  int emptySrcT
2858a 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
2858b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2858c 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79  f test for empty
2858d 20 70 53 72 63 20 2a 2f 0a 20 20 56 64 62 65 20   pSrc */.  Vdbe 
2858e 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2858f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
28590 68 65 20 56 44 42 45 20 77 65 20 61 72 65 20 62  he VDBE we are b
28591 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b 65 79  uilding */.  Key
28592 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20  Info *pKey;     
28593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28594 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Key information
28595 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f   for an index */
28596 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 6e  .  int regAutoin
28597 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
28598 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65      /* Memory re
28599 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20 41  gister used by A
2859a 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e 74 20  UTOINC */.  int 
2859b 64 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78  destHasUniqueIdx
2859c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
2859d 54 72 75 65 20 69 66 20 70 44 65 73 74 20 68 61  True if pDest ha
2859e 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  s a UNIQUE index
2859f 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
285a0 61 2c 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20  a, regRowid;    
285a1 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
285a2 65 72 73 20 68 6f 6c 64 69 6e 67 20 64 61 74 61  ers holding data
285a3 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a 0a 20   and rowid */.. 
285a4 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d 30 20   if( pSelect==0 
285a5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
285a6 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 6f 66     /* Must be of
285a7 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53 45 52   the form  INSER
285a8 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43  T INTO ... SELEC
285a9 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69  T ... */.  }.  i
285aa 66 28 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  f( sqlite3Trigge
285ab 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 44  rList(pParse, pD
285ac 65 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  est) ){.    retu
285ad 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
285ae 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72  must not have tr
285af 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69  iggers */.  }.#i
285b0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
285b1 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
285b2 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62 46   if( pDest->tabF
285b3 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
285b4 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
285b5 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75 73  0;   /* tab1 mus
285b6 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74 75  t not be a virtu
285b7 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a  al table */.  }.
285b8 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e 45  #endif.  if( onE
285b9 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
285ba 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20   ){.    onError 
285bb 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a  = OE_Abort;.  }.
285bc 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f    if( onError!=O
285bd 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45 72 72  E_Abort && onErr
285be 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or!=OE_Rollback 
285bf 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
285c0 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f 20     /* Cannot do 
285c1 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20 4f 52  OR REPLACE or OR
285c2 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20 46 41   IGNORE or OR FA
285c3 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  IL */.  }.  asse
285c4 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  rt(pSelect->pSrc
285c5 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65  );   /* allocate
285c6 64 20 65 76 65 6e 20 69 66 20 74 68 65 72 65 20  d even if there 
285c7 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61 75 73  is no FROM claus
285c8 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65  e */.  if( pSele
285c9 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d  ct->pSrc->nSrc!=
285ca 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  1 ){.    return 
285cb 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61  0;   /* FROM cla
285cc 75 73 65 20 6d 75 73 74 20 68 61 76 65 20 65 78  use must have ex
285cd 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d 20 2a  actly one term *
285ce 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
285cf 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e  ect->pSrc->a[0].
285d0 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72  pSelect ){.    r
285d1 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52  eturn 0;   /* FR
285d2 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e 6f 74  OM clause cannot
285d3 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62 71 75   contain a subqu
285d4 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ery */.  }.  if(
285d5 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65   pSelect->pWhere
285d6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
285d7 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
285d8 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48 45  y not have a WHE
285d9 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  RE clause */.  }
285da 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
285db 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
285dc 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
285dd 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61  ELECT may not ha
285de 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  ve an ORDER BY c
285df 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 2f  lause */.  }.  /
285e0 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  * Do not need to
285e1 20 74 65 73 74 20 66 6f 72 20 61 20 48 41 56 49   test for a HAVI
285e2 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66 20 48  NG clause.  If H
285e3 41 56 49 4e 47 20 69 73 20 70 72 65 73 65 6e 74  AVING is present
285e4 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72 65 20   but.  ** there 
285e5 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 2c 20  is no ORDER BY, 
285e6 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e 20 65  we will get an e
285e7 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  rror. */.  if( p
285e8 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70 42 79  Select->pGroupBy
285e9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
285ea 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61  ;   /* SELECT ma
285eb 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47 52 4f  y not have a GRO
285ec 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  UP BY clause */.
285ed 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
285ee 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20  t->pLimit ){.   
285ef 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
285f0 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
285f1 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75  ave a LIMIT clau
285f2 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  se */.  }.  asse
285f3 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 66  rt( pSelect->pOf
285f4 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d  fset==0 );  /* M
285f5 75 73 74 20 62 65 20 73 6f 20 69 66 20 70 4c 69  ust be so if pLi
285f6 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20  mit==0 */.  if( 
285f7 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
285f8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
285f9 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
285fa 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f 75   not be a compou
285fb 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a  nd query */.  }.
285fc 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 73    if( pSelect->s
285fd 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
285fe 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65 74  tinct ){.    ret
285ff 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
28600 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44 49  CT may not be DI
28601 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20  STINCT */.  }.  
28602 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  pEList = pSelect
28603 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65  ->pEList;.  asse
28604 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
28605 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e  .  if( pEList->n
28606 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 72  Expr!=1 ){.    r
28607 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68  eturn 0;   /* Th
28608 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73  e result set mus
28609 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
2860a 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d  ne column */.  }
2860b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
2860c 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b  t->a[0].pExpr );
2860d 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61  .  if( pEList->a
2860e 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54  [0].pExpr->op!=T
2860f 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74  K_ALL ){.    ret
28610 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  urn 0;   /* The 
28611 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20  result set must 
28612 62 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f  be the special o
28613 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20  perator "*" */. 
28614 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73   }..  /* At this
28615 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65   point we have e
28616 73 74 61 62 6c 69 73 68 65 64 20 74 68 61 74 20  stablished that 
28617 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
28618 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72   of the.  ** cor
28619 72 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66  rect syntactic f
2861a 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61  orm to participa
2861b 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d  te in this optim
2861c 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20  ization.  Now.  
2861d 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68  ** we have to ch
2861e 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63  eck the semantic
2861f 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20  s..  */.  pItem 
28620 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d  = pSelect->pSrc-
28621 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c  >a;.  pSrc = sql
28622 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
28623 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d  pParse, 0, pItem
28624 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->zName, pItem->
28625 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  zDatabase);.  if
28626 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ( pSrc==0 ){.   
28627 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28628 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65 73  FROM clause does
28629 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 72   not contain a r
2862a 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  eal table */.  }
2862b 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44 65  .  if( pSrc==pDe
2862c 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  st ){.    return
2862d 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61 6e   0;   /* tab1 an
2862e 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20 62  d tab2 may not b
2862f 65 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  e the same table
28630 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   */.  }.#ifndef 
28631 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
28632 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
28633 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Src->tabFlags & 
28634 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20  TF_Virtual ){.  
28635 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
28636 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20 62   tab2 must not b
28637 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
28638 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  e */.  }.#endif.
28639 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65 6c    if( pSrc->pSel
2863a 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ect ){.    retur
2863b 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
2863c 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65 77  ay not be a view
2863d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   */.  }.  if( pD
2863e 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63 2d  est->nCol!=pSrc-
2863f 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65 74  >nCol ){.    ret
28640 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62  urn 0;   /* Numb
28641 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d 75  er of columns mu
28642 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 69  st be the same i
28643 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20  n tab1 and tab2 
28644 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65  */.  }.  if( pDe
28645 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63 2d  st->iPKey!=pSrc-
28646 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72 65  >iPKey ){.    re
28647 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f 74  turn 0;   /* Bot
28648 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68 61  h tables must ha
28649 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54 45  ve the same INTE
2864a 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
2864b 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  */.  }.  for(i=0
2864c 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c 3b  ; i<pDest->nCol;
2864d 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   i++){.    if( p
2864e 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66  Dest->aCol[i].af
2864f 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61 43  finity!=pSrc->aC
28650 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20 29  ol[i].affinity )
28651 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
28652 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79  ;    /* Affinity
28653 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
28654 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  e on all columns
28655 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
28656 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  ( !xferCompatibl
28657 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73 74  eCollation(pDest
28658 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c  ->aCol[i].zColl,
28659 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a   pSrc->aCol[i].z
2865a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72  Coll) ){.      r
2865b 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43  eturn 0;    /* C
2865c 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2865d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  e must be the sa
2865e 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e  me on all column
2865f 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
28660 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69  f( pDest->aCol[i
28661 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53  ].notNull && !pS
28662 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e  rc->aCol[i].notN
28663 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ull ){.      ret
28664 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62  urn 0;    /* tab
28665 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e 55  2 must be NOT NU
28666 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a 2f  LL if tab1 is */
28667 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
28668 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d  (pDestIdx=pDest-
28669 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64  >pIndex; pDestId
2866a 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65 73  x; pDestIdx=pDes
2866b 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  tIdx->pNext){.  
2866c 20 20 69 66 28 20 70 44 65 73 74 49 64 78 2d 3e    if( pDestIdx->
2866d 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2866e 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48 61   ){.      destHa
2866f 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b 0a  sUniqueIdx = 1;.
28670 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53      }.    for(pS
28671 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64  rcIdx=pSrc->pInd
28672 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53 72  ex; pSrcIdx; pSr
28673 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e  cIdx=pSrcIdx->pN
28674 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ext){.      if( 
28675 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
28676 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70 53  dex(pDestIdx, pS
28677 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b 0a  rcIdx) ) break;.
28678 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
28679 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20  rcIdx==0 ){.    
2867a 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
2867b 2a 20 70 44 65 73 74 49 64 78 20 68 61 73 20 6e  * pDestIdx has n
2867c 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  o corresponding 
2867d 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a 2f  index in pSrc */
2867e 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64  .    }.  }.#ifnd
2867f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
28680 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73 74  HECK.  if( pDest
28681 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71 6c  ->pCheck && !sql
28682 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
28683 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70 44  pSrc->pCheck, pD
28684 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a  est->pCheck) ){.
28685 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
28686 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20 64  /* Tables have d
28687 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20 63  ifferent CHECK c
28688 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69 63  onstraints.  Tic
28689 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20 7d  ket #2252 */.  }
2868a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
2868b 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72   we get this far
2868c 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68 65  , it means eithe
2868d 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  r:.  **.  **    
2868e 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61 79  *   We can alway
2868f 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66 65  s do the transfe
28690 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 63  r if the table c
28691 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a 20  ontains an.  ** 
28692 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67 65         an intege
28693 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20  r primary key.  
28694 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57  **.  **    *   W
28695 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e 61  e can conditiona
28696 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e 73  lly do the trans
28697 66 65 72 20 69 66 20 74 68 65 20 64 65 73 74 69  fer if the desti
28698 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 20  nation.  **     
28699 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74     table is empt
2869a 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  y..  */.#ifdef S
2869b 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c  QLITE_TEST.  sql
2869c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75  ite3_xferopt_cou
2869d 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69  nt++;.#endif.  i
2869e 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53  DbSrc = sqlite3S
2869f 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
286a0 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e 70  rse->db, pSrc->p
286a1 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73  Schema);.  v = s
286a2 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
286a3 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
286a4 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
286a5 28 70 50 61 72 73 65 2c 20 69 44 62 53 72 63 29  (pParse, iDbSrc)
286a6 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72 73  ;.  iSrc = pPars
286a7 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 44 65  e->nTab++;.  iDe
286a8 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  st = pParse->nTa
286a9 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f 69 6e  b++;.  regAutoin
286aa 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  c = autoIncBegin
286ab 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73 74  (pParse, iDbDest
286ac 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69  , pDest);.  sqli
286ad 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
286ae 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44 62 44  rse, iDest, iDbD
286af 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50 5f 4f  est, pDest, OP_O
286b0 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69 66 28  penWrite);.  if(
286b1 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79 3c 30   (pDest->iPKey<0
286b2 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e 64 65   && pDest->pInde
286b3 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48 61 73  x!=0) || destHas
286b4 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20 20 20  UniqueIdx ){.   
286b5 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20 64 6f   /* If tables do
286b6 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49 4e 54   not have an INT
286b7 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
286b8 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20 20 2a   and there.    *
286b9 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 6f  * are indices to
286ba 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64 20 74   be copied and t
286bb 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  he destination i
286bc 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20  s not empty,.   
286bd 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 64   ** we have to d
286be 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e  isallow the tran
286bf 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
286c0 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20  n because the.  
286c1 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64 73 20    ** the rowids 
286c2 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77 68 69  might change whi
286c3 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20  ch will mess up 
286c4 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20 2a 2a  indexing..    **
286c5 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20 74 68  .    ** Or if th
286c6 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61  e destination ha
286c7 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  s a UNIQUE index
286c8 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d 70 74   and is not empt
286c9 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61 6c 73  y,.    ** we als
286ca 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74  o disallow the t
286cb 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61  ransfer optimiza
286cc 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77 65 20  tion because we 
286cd 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 69 6e  cannot.    ** in
286ce 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 65 6e  sure that all en
286cf 74 72 69 65 73 20 69 6e 20 74 68 65 20 75 6e 69  tries in the uni
286d0 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64 20 53  on of DEST and S
286d1 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a  RC will be.    *
286d2 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f  * unique..    */
286d3 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
286d4 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
286d5 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 44 65  , OP_Rewind, iDe
286d6 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d 70 74  st, 0);.    empt
286d7 79 44 65 73 74 54 65 73 74 20 3d 20 73 71 6c 69  yDestTest = sqli
286d8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
286d9 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b   OP_Goto, 0, 0);
286da 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
286db 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
286dc 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  1);.  }else{.   
286dd 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d   emptyDestTest =
286de 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
286df 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
286e0 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72 63 2c  e, iSrc, iDbSrc,
286e1 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52 65   pSrc, OP_OpenRe
286e2 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72 63 54  ad);.  emptySrcT
286e3 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  est = sqlite3Vdb
286e4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
286e5 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wind, iSrc, 0);.
286e6 20 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c 69    regData = sqli
286e7 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
286e8 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f 77 69  arse);.  regRowi
286e9 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
286ea 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
286eb 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b 65   if( pDest->iPKe
286ec 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72  y>=0 ){.    addr
286ed 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
286ee 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
286ef 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69  d, iSrc, regRowi
286f0 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20  d);.    addr2 = 
286f1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
286f2 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
286f3 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72 65 67  s, iDest, 0, reg
286f4 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  Rowid);.    sqli
286f5 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
286f6 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  t(.        pPars
286f7 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50 52 49  e, onError, "PRI
286f8 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65  MARY KEY must be
286f9 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41   unique", P4_STA
286fa 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
286fb 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
286fc 20 61 64 64 72 32 29 3b 0a 20 20 20 20 61 75 74   addr2);.    aut
286fd 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c  oIncStep(pParse,
286fe 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67   regAutoinc, reg
286ff 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20  Rowid);.  }else 
28700 69 66 28 20 70 44 65 73 74 2d 3e 70 49 6e 64 65  if( pDest->pInde
28701 78 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72  x==0 ){.    addr
28702 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
28703 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
28704 6f 77 69 64 2c 20 69 44 65 73 74 2c 20 72 65 67  owid, iDest, reg
28705 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
28706 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
28707 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28708 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63  , OP_Rowid, iSrc
28709 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
2870a 20 61 73 73 65 72 74 28 20 28 70 44 65 73 74 2d   assert( (pDest-
2870b 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
2870c 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30  utoincrement)==0
2870d 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   );.  }.  sqlite
2870e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2870f 50 5f 52 6f 77 44 61 74 61 2c 20 69 53 72 63 2c  P_RowData, iSrc,
28710 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c   regData);.  sql
28711 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
28712 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65  , OP_Insert, iDe
28713 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  st, regData, reg
28714 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  Rowid);.  sqlite
28715 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
28716 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c   OPFLAG_NCHANGE|
28717 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44  OPFLAG_LASTROWID
28718 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b  |OPFLAG_APPEND);
28719 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
2871a 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 44  angeP4(v, -1, pD
2871b 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  est->zName, 0);.
2871c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2871d 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
2871e 69 53 72 63 2c 20 61 64 64 72 31 29 3b 0a 20 20  iSrc, addr1);.  
2871f 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65  for(pDestIdx=pDe
28720 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73  st->pIndex; pDes
28721 74 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70  tIdx; pDestIdx=p
28722 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b  DestIdx->pNext){
28723 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78  .    for(pSrcIdx
28724 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 41  =pSrc->pIndex; A
28725 4c 57 41 59 53 28 70 53 72 63 49 64 78 29 3b 20  LWAYS(pSrcIdx); 
28726 70 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d  pSrcIdx=pSrcIdx-
28727 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
28728 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  f( xferCompatibl
28729 65 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c  eIndex(pDestIdx,
2872a 20 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61   pSrcIdx) ) brea
2872b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  k;.    }.    ass
2872c 65 72 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a  ert( pSrcIdx );.
2872d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2872e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2872f 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20  e, iSrc, 0);.   
28730 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28731 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
28732 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70  iDest, 0);.    p
28733 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
28734 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
28735 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20  , pSrcIdx);.    
28736 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28737 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  4(v, OP_OpenRead
28738 2c 20 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d  , iSrc, pSrcIdx-
28739 3e 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20  >tnum, iDbSrc,. 
2873a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2873b 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2873c 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
2873d 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43  DOFF);.    VdbeC
2873e 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
2873f 20 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29   pSrcIdx->zName)
28740 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71  );.    pKey = sq
28741 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
28742 6f 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 49  o(pParse, pDestI
28743 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
28744 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
28745 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73  _OpenWrite, iDes
28746 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75  t, pDestIdx->tnu
28747 6d 2c 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20  m, iDbDest,.    
28748 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28749 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50    (char*)pKey, P
2874a 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2874b 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  F);.    VdbeComm
2874c 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44  ent((v, "%s", pD
2874d 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  estIdx->zName));
2874e 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
2874f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28750 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72  , OP_Rewind, iSr
28751 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  c, 0);.    sqlit
28752 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28753 4f 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c  OP_RowKey, iSrc,
28754 20 72 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73   regData);.    s
28755 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
28756 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
28757 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61 74 61  , iDest, regData
28758 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
28759 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2875a 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64  P_Next, iSrc, ad
2875b 64 72 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  dr1+1);.    sqli
2875c 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2875d 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20  v, addr1);.  }. 
2875e 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2875f 48 65 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63  Here(v, emptySrc
28760 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Test);.  sqlite3
28761 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
28762 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29  Parse, regRowid)
28763 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
28764 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
28765 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71  , regData);.  sq
28766 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28767 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72  v, OP_Close, iSr
28768 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  c, 0);.  sqlite3
28769 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2876a 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30  _Close, iDest, 0
2876b 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65  );.  if( emptyDe
2876c 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71  stTest ){.    sq
2876d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2876e 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49  v, OP_Halt, SQLI
2876f 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73  TE_OK, 0);.    s
28770 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
28771 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54  re(v, emptyDestT
28772 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  est);.    sqlite
28773 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28774 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20  P_Close, iDest, 
28775 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  0);.    return 0
28776 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
28777 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23  eturn 1;.  }.}.#
28778 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
28779 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f  OMIT_XFER_OPT */
2877a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2877b 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72 74 2e  * End of insert.
2877c 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2877d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2877e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2877f 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
28780 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 65 67  * Begin file leg
28781 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  acy.c **********
28782 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28783 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28784 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
28785 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
28786 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
28787 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
28788 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
28789 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2878a 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2878b 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2878c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2878d 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2878e 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2878f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
28790 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
28791 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
28792 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
28793 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
28794 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
28795 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
28796 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
28797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28798 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28799 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2879a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2879b 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e  ********.** Main
2879c 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51   file for the SQ
2879d 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54  Lite library.  T
2879e 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
2879f 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c  his file.** impl
287a0 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61  ement the progra
287a1 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74  mmer interface t
287a2 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  o the library.  
287a3 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f  Routines in.** o
287a4 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66  ther files are f
287a5 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
287a6 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68  by SQLite and sh
287a7 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61  ould not be.** a
287a8 63 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73  ccessed by users
287a9 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
287aa 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  .*/.../*.** Exec
287ab 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20 20 52  ute SQL code.  R
287ac 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
287ad 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65 73 73   SQLITE_ success
287ae 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f 64 65  /failure.** code
287af 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65 20 61  s.  Also write a
287b0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
287b1 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
287b2 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
287b3 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
287b4 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
287b5 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65 2e 0a  o that message..
287b6 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
287b7 20 69 73 20 61 20 71 75 65 72 79 2c 20 74 68 65   is a query, the
287b8 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  n for each row i
287b9 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  n the query resu
287ba 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c 6c 62  lt.** the xCallb
287bb 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ack() function i
287bc 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72 67 20  s called.  pArg 
287bd 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69 72 73  becomes the firs
287be 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  t.** argument to
287bf 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20 20 49   xCallback().  I
287c0 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c  f xCallback=NULL
287c1 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62 61 63   then no callbac
287c2 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2c  k.** is invoked,
287c3 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72 69 65   even for querie
287c4 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
287c5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
287c6 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
287c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
287c8 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
287c9 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
287ca 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
287cb 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
287cc 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
287cd 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
287ce 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  cuted */.  sqlit
287cf 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43 61 6c  e3_callback xCal
287d0 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65  lback, /* Invoke
287d1 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 72   this callback r
287d2 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f 69 64  outine */.  void
287d3 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20 20 20   *pArg,         
287d4 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
287d5 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61   argument to xCa
287d6 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20 63 68  llback() */.  ch
287d7 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
287d8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
287d9 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
287da 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
287db 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
287dc 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  K;         /* Re
287dd 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63  turn code */.  c
287de 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
287df 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 61  over;      /* Ta
287e0 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65 73 73 65  il of unprocesse
287e1 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71 6c 69 74  d SQL */.  sqlit
287e2 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d  e3_stmt *pStmt =
287e3 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75   0;    /* The cu
287e4 72 72 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d  rrent SQL statem
287e5 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ent */.  char **
287e6 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20 20 20 20  azCols = 0;     
287e7 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 20 6f 66       /* Names of
287e8 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
287e9 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74 72 79 20  */.  int nRetry 
287ea 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
287eb 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
287ec 74 72 79 20 61 74 74 65 6d 70 74 73 20 2a 2f 0a  try attempts */.
287ed 20 20 69 6e 74 20 63 61 6c 6c 62 61 63 6b 49 73    int callbackIs
287ee 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Init;         /*
287ef 20 54 72 75 65 20 69 66 20 63 61 6c 6c 62 61 63   True if callbac
287f0 6b 20 64 61 74 61 20 69 73 20 69 6e 69 74 69 61  k data is initia
287f1 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69 66 28 20  lized */..  if( 
287f2 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d  zSql==0 ) zSql =
287f3 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   "";..  sqlite3_
287f4 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
287f5 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
287f6 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
287f7 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c  E_OK, 0);.  whil
287f8 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  e( (rc==SQLITE_O
287f9 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45  K || (rc==SQLITE
287fa 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52  _SCHEMA && (++nR
287fb 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71  etry)<2)) && zSq
287fc 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  l[0] ){.    int 
287fd 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  nCol;.    char *
287fe 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20  *azVals = 0;..  
287ff 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20    pStmt = 0;.   
28800 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
28801 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
28802 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65  -1, &pStmt, &zLe
28803 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73  ftover);.    ass
28804 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
28805 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
28806 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
28807 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
28808 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
28809 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20  .    if( !pStmt 
2880a 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73  ){.      /* this
2880b 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63   happens for a c
2880c 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d  omment or white-
2880d 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a  space */.      z
2880e 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
2880f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
28810 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 6c 6c  .    }..    call
28811 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 30 3b 0a  backIsInit = 0;.
28812 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74      nCol = sqlit
28813 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
28814 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 77 68 69  pStmt);..    whi
28815 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20 69  le( 1 ){.      i
28816 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63 20 3d  nt i;.      rc =
28817 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53   sqlite3_step(pS
28818 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  tmt);..      /* 
28819 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
2881a 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  ack function if 
2881b 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20  required */.    
2881c 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 20    if( xCallback 
2881d 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  && (SQLITE_ROW==
2881e 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  rc || .         
2881f 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d 72   (SQLITE_DONE==r
28820 63 20 26 26 20 21 63 61 6c 6c 62 61 63 6b 49 73  c && !callbackIs
28821 49 6e 69 74 0a 20 20 20 20 20 20 20 20 20 20 20  Init.           
28822 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28823 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  && db->flags&SQL
28824 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b  ITE_NullCallback
28825 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  )) ){.        if
28826 28 20 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69  ( !callbackIsIni
28827 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  t ){.          a
28828 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 44  zCols = sqlite3D
28829 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2882a 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f  2*nCol*sizeof(co
2882b 6e 73 74 20 63 68 61 72 2a 29 20 2b 20 31 29 3b  nst char*) + 1);
2882c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
2882d 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20  zCols==0 ){.    
2882e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
2882f 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  c_out;.         
28830 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72   }.          for
28831 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
28832 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
28833 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azCols[i] = (cha
28834 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
28835 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69  mn_name(pStmt, i
28836 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  );.            /
28837 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  * sqlite3VdbeSet
28838 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73 74 61 6c  ColName() instal
28839 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ls column names 
2883a 61 73 20 55 54 46 38 0a 20 20 20 20 20 20 20 20  as UTF8.        
2883b 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 73      ** strings s
2883c 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  o there is no wa
2883d 79 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  y for sqlite3_co
2883e 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f 20 66  lumn_name() to f
2883f 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ail. */.        
28840 20 20 20 20 61 73 73 65 72 74 28 20 61 7a 43 6f      assert( azCo
28841 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 20 20  ls[i]!=0 );.    
28842 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28843 20 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74    callbackIsInit
28844 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
28845 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
28846 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
28847 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
28848 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a   &azCols[nCol];.
28849 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
2884a 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2884b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56  .            azV
2884c 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  als[i] = (char *
2884d 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
2884e 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a  text(pStmt, i);.
2884f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
28850 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71  !azVals[i] && sq
28851 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
28852 65 28 70 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c  e(pStmt, i)!=SQL
28853 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
28854 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
28855 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
28856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
28857 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20  to exec_out;.   
28858 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
28859 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2885a 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 43 61  .        if( xCa
2885b 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f  llback(pArg, nCo
2885c 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c  l, azVals, azCol
2885d 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  s) ){.          
2885e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
2885f 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
28860 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
28861 28 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b  ((Vdbe *)pStmt);
28862 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 6d 74  .          pStmt
28863 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
28864 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
28865 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30   SQLITE_ABORT, 0
28866 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
28867 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20  o exec_out;.    
28868 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
28869 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
2886a 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
2886b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
2886c 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
2886d 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20  e *)pStmt);.    
2886e 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20      pStmt = 0;. 
2886f 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
28870 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
28871 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72 79            nRetry
28872 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
28873 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72  zSql = zLeftover
28874 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
28875 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
28876 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71  e(zSql[0]) ) zSq
28877 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  l++;.        }. 
28878 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
28879 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2887a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2887b 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20  b, azCols);.    
2887c 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a  azCols = 0;.  }.
2887d 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28  .exec_out:.  if(
2887e 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74 65 33   pStmt ) sqlite3
2887f 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64  VdbeFinalize((Vd
28880 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 73  be *)pStmt);.  s
28881 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
28882 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20   azCols);..  rc 
28883 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
28884 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20  (db, rc);.  if( 
28885 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
28886 20 41 4c 57 41 59 53 28 72 63 3d 3d 73 71 6c 69   ALWAYS(rc==sqli
28887 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 29  te3_errcode(db))
28888 20 26 26 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a   && pzErrMsg ){.
28889 20 20 20 20 69 6e 74 20 6e 45 72 72 4d 73 67 20      int nErrMsg 
2888a 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72  = 1 + sqlite3Str
2888b 6c 65 6e 33 30 28 73 71 6c 69 74 65 33 5f 65 72  len30(sqlite3_er
2888c 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20 2a  rmsg(db));.    *
2888d 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
2888e 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72 4d 73 67  e3Malloc(nErrMsg
2888f 29 3b 0a 20 20 20 20 69 66 28 20 2a 70 7a 45 72  );.    if( *pzEr
28890 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 6d 65  rMsg ){.      me
28891 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73 67 2c 20  mcpy(*pzErrMsg, 
28892 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
28893 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b 0a 20 20  b), nErrMsg);.  
28894 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
28895 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
28896 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
28897 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
28898 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d  NOMEM, 0);.    }
28899 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45  .  }else if( pzE
2889a 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a  rrMsg ){.    *pz
2889b 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
2889c 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64  .  assert( (rc&d
2889d 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20  b->errMask)==rc 
2889e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2889f 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
288a0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
288a1 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
288a2 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61  **** End of lega
288a3 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cy.c ***********
288a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288a6 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
288a7 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
288a8 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a  loadext.c ******
288a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ab 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
288ac 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  June 7.**.** The
288ad 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
288ae 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
288af 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
288b0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
288b1 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
288b2 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
288b3 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
288b4 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
288b5 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
288b6 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
288b7 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
288b8 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
288b9 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
288ba 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
288bb 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
288bc 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
288bd 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
288be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288c2 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
288c3 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
288c4 20 75 73 65 64 20 74 6f 20 64 79 6e 61 6d 69 63   used to dynamic
288c5 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74 65 6e 73  ally load extens
288c6 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  ions into.** the
288c7 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
288c8 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
288c9 49 54 45 5f 43 4f 52 45 0a 20 20 23 64 65 66 69  ITE_CORE.  #defi
288ca 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
288cb 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 74 68 65    /* Disable the
288cc 20 41 50 49 20 72 65 64 65 66 69 6e 69 74 69 6f   API redefinitio
288cd 6e 20 69 6e 20 73 71 6c 69 74 65 33 65 78 74 2e  n in sqlite3ext.
288ce 68 20 2a 2f 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a  h */.#endif./***
288cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
288d0 75 64 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68  ude sqlite3ext.h
288d1 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
288d2 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a  f loadext.c ****
288d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
288d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
288d5 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 65 78  n file sqlite3ex
288d6 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
288d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
288d9 2a 20 32 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a  * 2006 June 7.**
288da 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
288db 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
288dc 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
288dd 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
288de 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
288df 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
288e0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
288e1 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
288e2 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
288e3 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
288e4 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
288e5 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
288e6 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
288e7 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
288e8 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
288e9 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
288ea 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
288eb 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
288ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
288f0 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
288f1 20 64 65 66 69 6e 65 73 20 74 68 65 20 53 51 4c   defines the SQL
288f2 69 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 6f  ite interface fo
288f3 72 20 75 73 65 20 62 79 0a 2a 2a 20 73 68 61 72  r use by.** shar
288f4 65 64 20 6c 69 62 72 61 72 69 65 73 20 74 68 61  ed libraries tha
288f5 74 20 77 61 6e 74 20 74 6f 20 62 65 20 69 6d 70  t want to be imp
288f6 6f 72 74 65 64 20 61 73 20 65 78 74 65 6e 73 69  orted as extensi
288f7 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53  ons into.** an S
288f8 51 4c 69 74 65 20 69 6e 73 74 61 6e 63 65 2e 20  QLite instance. 
288f9 20 53 68 61 72 65 64 20 6c 69 62 72 61 72 69 65   Shared librarie
288fa 73 20 74 68 61 74 20 69 6e 74 65 6e 64 20 74 6f  s that intend to
288fb 20 62 65 20 6c 6f 61 64 65 64 0a 2a 2a 20 61 73   be loaded.** as
288fc 20 65 78 74 65 6e 73 69 6f 6e 73 20 62 79 20 53   extensions by S
288fd 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 23 69 6e  QLite should #in
288fe 63 6c 75 64 65 20 74 68 69 73 20 66 69 6c 65 20  clude this file 
288ff 69 6e 73 74 65 61 64 20 6f 66 20 0a 2a 2a 20 73  instead of .** s
28900 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 23 69 66  qlite3.h..*/.#if
28901 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58 54  ndef _SQLITE3EXT
28902 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
28903 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70 65  ITE3EXT_H_..type
28904 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
28905 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
28906 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
28907 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ines;../*.** The
28908 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
28909 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e 74  ture holds point
2890a 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74 68  ers to all of th
2890b 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a 20  e SQLite API.** 
2890c 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
2890d 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72 64  WARNING:  In ord
2890e 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62  er to maintain b
2890f 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
28910 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65 77 0a  bility, add new.
28911 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f  ** interfaces to
28912 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73   the end of this
28913 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e   structure only.
28914 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72 74 20    If you insert 
28915 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
28916 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
28917 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
28918 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20 64 69  e, then older di
28919 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73 69  fferent.** versi
2891a 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77 69  ons of SQLite wi
2891b 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
2891c 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74 68 65  o load each othe
2891d 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c 69  rs' shared.** li
2891e 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72 75  braries!.*/.stru
2891f 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  ct sqlite3_api_r
28920 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69 64  outines {.  void
28921 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63   * (*aggregate_c
28922 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f  ontext)(sqlite3_
28923 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79  context*,int nBy
28924 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 61  tes);.  int  (*a
28925 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29 28  ggregate_count)(
28926 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28927 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
28928 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  _blob)(sqlite3_s
28929 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
2892a 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28  oid*,int n,void(
2892b 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
2892c 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c 65  t  (*bind_double
2892d 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
2892e 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 69  int,double);.  i
2892f 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29 28  nt  (*bind_int)(
28930 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
28931 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  t,int);.  int  (
28932 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73 71 6c  *bind_int64)(sql
28933 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 73  ite3_stmt*,int,s
28934 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20  qlite_int64);.  
28935 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c 6c  int  (*bind_null
28936 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28937 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  int);.  int  (*b
28938 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
28939 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  unt)(sqlite3_stm
2893a 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  t*);.  int  (*bi
2893b 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
2893c 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ex)(sqlite3_stmt
2893d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61  *,const char*zNa
2893e 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  me);.  const cha
2893f 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d  r * (*bind_param
28940 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69 74  eter_name)(sqlit
28941 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
28942 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65 78   int  (*bind_tex
28943 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
28944 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
28945 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76  ,int n,void(*)(v
28946 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  oid*));.  int  (
28947 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28 73 71  *bind_text16)(sq
28948 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
28949 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2894a 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2894b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 76  .  int  (*bind_v
2894c 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74  alue)(sqlite3_st
2894d 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73 71  mt*,int,const sq
2894e 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
2894f 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68 61 6e   int  (*busy_han
28950 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69  dler)(sqlite3*,i
28951 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29  nt(*)(void*,int)
28952 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
28953 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74 29 28  (*busy_timeout)(
28954 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29  sqlite3*,int ms)
28955 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e 67  ;.  int  (*chang
28956 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  es)(sqlite3*);. 
28957 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28 73   int  (*close)(s
28958 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20  qlite3*);.  int 
28959 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65   (*collation_nee
2895a 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ded)(sqlite3*,vo
2895b 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  id*,void(*)(void
2895c 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2895d 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
2895e 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar*));.  int  (*
2895f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28960 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  16)(sqlite3*,voi
28961 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  d*,void(*)(void*
28962 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
28963 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
28964 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  d*));.  const vo
28965 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c  id * (*column_bl
28966 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ob)(sqlite3_stmt
28967 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
28968 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
28969 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  es)(sqlite3_stmt
2896a 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
2896b 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74  nt  (*column_byt
2896c 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  es16)(sqlite3_st
2896d 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
2896e 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63   int  (*column_c
2896f 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
28970 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e  mt*pStmt);.  con
28971 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
28972 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28973 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28974 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
28975 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61  id * (*column_da
28976 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29 28 73  tabase_name16)(s
28977 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28978 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
28979 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  * (*column_declt
2897a 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ype)(sqlite3_stm
2897b 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e  t*,int i);.  con
2897c 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
2897d 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29 28 73  mn_decltype16)(s
2897e 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2897f 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 63  );.  double  (*c
28980 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71  olumn_double)(sq
28981 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
28982 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  iCol);.  int  (*
28983 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69  column_int)(sqli
28984 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
28985 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ol);.  sqlite_in
28986 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e  t64  (*column_in
28987 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  t64)(sqlite3_stm
28988 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
28989 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63  const char * (*c
2898a 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69  olumn_name)(sqli
2898b 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
2898c 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
2898d 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28  *column_name16)(
2898e 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2898f 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
28990 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67   * (*column_orig
28991 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  in_name)(sqlite3
28992 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
28993 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
28994 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
28995 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
28996 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
28997 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
28998 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69  table_name)(sqli
28999 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
2899a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
2899b 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  *column_table_na
2899c 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74  me16)(sqlite3_st
2899d 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
2899e 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2899f 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29  * (*column_text)
289a0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
289a1 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73  nt iCol);.  cons
289a2 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
289a3 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65  n_text16)(sqlite
289a4 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
289a5 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
289a6 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33  mn_type)(sqlite3
289a7 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
289a8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
289a9 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75  e* (*column_valu
289aa 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
289ab 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f  ,int iCol);.  vo
289ac 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f  id * (*commit_ho
289ad 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ok)(sqlite3*,int
289ae 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a  (*)(void*),void*
289af 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70  );.  int  (*comp
289b0 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61 72  lete)(const char
289b1 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  *sql);.  int  (*
289b2 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e 73  complete16)(cons
289b3 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69  t void*sql);.  i
289b4 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c  nt  (*create_col
289b5 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  lation)(sqlite3*
289b6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
289b7 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
289b8 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
289b9 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
289ba 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
289bb 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
289bc 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
289bd 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
289be 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  d*,int(*)(void*,
289bf 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
289c0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
289c1 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61  );.  int  (*crea
289c2 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c  te_function)(sql
289c3 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
289c4 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c  *,int,int,void*,
289c5 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
289c6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
289c7 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
289c8 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70  **),void (*xStep
289c9 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
289ca 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
289cb 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78  alue**),void (*x
289cc 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
289cd 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74  ontext*));.  int
289ce 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74    (*create_funct
289cf 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c  ion16)(sqlite3*,
289d0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
289d1 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28  int,void*,void (
289d2 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
289d3 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
289d4 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f  ite3_value**),vo
289d5 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
289d6 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
289d7 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
289d8 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29  ),void (*xFinal)
289d9 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
289da 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65  *));.  int (*cre
289db 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69  ate_module)(sqli
289dc 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
289dd 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
289de 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20  odule*,void*);. 
289df 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f 75   int  (*data_cou
289e0 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
289e1 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  *pStmt);.  sqlit
289e2 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65  e3 * (*db_handle
289e3 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
289e4 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61 72  ;.  int (*declar
289e5 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33 2a  e_vtab)(sqlite3*
289e6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
289e7 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73   int  (*enable_s
289e8 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e 74  hared_cache)(int
289e9 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72 63  );.  int  (*errc
289ea 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62 29  ode)(sqlite3*db)
289eb 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
289ec 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69 74   (*errmsg)(sqlit
289ed 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  e3*);.  const vo
289ee 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36 29  id * (*errmsg16)
289ef 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
289f0 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69 74  t  (*exec)(sqlit
289f1 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
289f2 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b  sqlite3_callback
289f3 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a  ,void*,char**);.
289f4 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64    int  (*expired
289f5 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
289f6 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c  ;.  int  (*final
289f7 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize)(sqlite3_stm
289f8 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
289f9 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29    (*free)(void*)
289fa 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65  ;.  void  (*free
289fb 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72 65  _table)(char**re
289fc 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  sult);.  int  (*
289fd 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28  get_autocommit)(
289fe 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
289ff 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61 74  d * (*get_auxdat
28a00 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
28a01 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  xt*,int);.  int 
28a02 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73 71   (*get_table)(sq
28a03 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
28a04 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c  r*,char***,int*,
28a05 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20  int*,char**);.  
28a06 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65  int  (*global_re
28a07 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20 20  cover)(void);.  
28a08 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75 70  void  (*interrup
28a09 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  tx)(sqlite3*);. 
28a0a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28   sqlite_int64  (
28a0b 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  *last_insert_row
28a0c 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  id)(sqlite3*);. 
28a0d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
28a0e 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69 64  libversion)(void
28a0f 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62 76  );.  int  (*libv
28a10 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76  ersion_number)(v
28a11 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  oid);.  void *(*
28a12 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20  malloc)(int);.  
28a13 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66  char * (*mprintf
28a14 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
28a15 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  .);.  int  (*ope
28a16 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  n)(const char*,s
28a17 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74  qlite3**);.  int
28a18 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73    (*open16)(cons
28a19 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  t void*,sqlite3*
28a1a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65  *);.  int  (*pre
28a1b 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63  pare)(sqlite3*,c
28a1c 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73  onst char*,int,s
28a1d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f  qlite3_stmt**,co
28a1e 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  nst char**);.  i
28a1f 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36 29  nt  (*prepare16)
28a20 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
28a21 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
28a22 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
28a23 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  oid**);.  void *
28a24 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69   (*profile)(sqli
28a25 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
28a26 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  d*,const char*,s
28a27 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f  qlite_uint64),vo
28a28 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  id*);.  void  (*
28a29 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
28a2a 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
28a2b 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69  nt(*)(void*),voi
28a2c 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72  d*);.  void *(*r
28a2d 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
28a2e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65 73  t);.  int  (*res
28a2f 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  et)(sqlite3_stmt
28a30 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20  *pStmt);.  void 
28a31 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28   (*result_blob)(
28a32 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28a33 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
28a34 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
28a35 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28a36 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  lt_double)(sqlit
28a37 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62  e3_context*,doub
28a38 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  le);.  void  (*r
28a39 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c  esult_error)(sql
28a3a 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
28a3b 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
28a3c 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
28a3d 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74 65  _error16)(sqlite
28a3e 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
28a3f 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76   void*,int);.  v
28a40 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
28a41 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
28a42 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64  xt*,int);.  void
28a43 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34    (*result_int64
28a44 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
28a45 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  t*,sqlite_int64)
28a46 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28a47 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33  lt_null)(sqlite3
28a48 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f  _context*);.  vo
28a49 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78  id  (*result_tex
28a4a 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
28a4b 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
28a4c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
28a4d 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
28a4e 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73 71  esult_text16)(sq
28a4f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
28a50 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
28a51 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
28a52 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
28a53 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74  _text16be)(sqlit
28a54 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
28a55 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
28a56 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
28a57 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
28a58 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f  xt16le)(sqlite3_
28a59 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
28a5a 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
28a5b 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
28a5c 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65    (*result_value
28a5d 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
28a5e 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t*,sqlite3_value
28a5f 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72  *);.  void * (*r
28a60 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71  ollback_hook)(sq
28a61 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
28a62 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
28a63 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68 6f  int  (*set_autho
28a64 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  rizer)(sqlite3*,
28a65 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
28a66 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
28a67 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
28a68 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
28a69 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
28a6a 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74 61 29    (*set_auxdata)
28a6b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
28a6c 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64  *,int,void*,void
28a6d 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20   (*)(void*));.  
28a6e 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74  char * (*snprint
28a6f 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e  f)(int,char*,con
28a70 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20  st char*,...);. 
28a71 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73 71   int  (*step)(sq
28a72 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
28a73 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c  int  (*table_col
28a74 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73 71  umn_metadata)(sq
28a75 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
28a76 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
28a77 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72 20  onst char*,char 
28a78 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f 6e  const**,char con
28a79 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  st**,int*,int*,i
28a7a 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  nt*);.  void  (*
28a7b 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 29 28  thread_cleanup)(
28a7c 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a  void);.  int  (*
28a7d 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29 28 73  total_changes)(s
28a7e 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64  qlite3*);.  void
28a7f 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71 6c 69   * (*trace)(sqli
28a80 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61 63  te3*,void(*xTrac
28a81 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
28a82 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  har*),void*);.  
28a83 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65 72 5f  int  (*transfer_
28a84 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65  bindings)(sqlite
28a85 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33 5f  3_stmt*,sqlite3_
28a86 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  stmt*);.  void *
28a87 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29 28   (*update_hook)(
28a88 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29  sqlite3*,void(*)
28a89 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61 72  (void*,int ,char
28a8a 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f 6e   const*,char con
28a8b 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  st*,sqlite_int64
28a8c 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
28a8d 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61 29 28   * (*user_data)(
28a8e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28a8f 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
28a90 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29 28  * (*value_blob)(
28a91 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28a92 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
28a93 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 76  bytes)(sqlite3_v
28a94 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
28a95 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36 29 28  *value_bytes16)(
28a96 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28a97 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76 61 6c  .  double  (*val
28a98 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  ue_double)(sqlit
28a99 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
28a9a 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29 28  t  (*value_int)(
28a9b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28a9c 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
28a9d 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29 28   (*value_int64)(
28a9e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28a9f 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
28aa0 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28 73 71  numeric_type)(sq
28aa1 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
28aa2 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
28aa3 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f 74  char * (*value_t
28aa4 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  ext)(sqlite3_val
28aa5 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
28aa6 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
28aa7 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  t16)(sqlite3_val
28aa8 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
28aa9 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
28aaa 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f 76  t16be)(sqlite3_v
28aab 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
28aac 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74  void * (*value_t
28aad 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33  ext16le)(sqlite3
28aae 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
28aaf 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28 73   (*value_type)(s
28ab0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
28ab1 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e    char *(*vmprin
28ab2 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  tf)(const char*,
28ab3 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20 41  va_list);.  /* A
28ab4 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69 6e  dded ??? */.  in
28ab5 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  t (*overload_fun
28ab6 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c  ction)(sqlite3*,
28ab7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
28ab8 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67  ncName, int nArg
28ab9 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79  );.  /* Added by
28aba 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e 74   3.3.13 */.  int
28abb 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29 28 73   (*prepare_v2)(s
28abc 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
28abd 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
28abe 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
28abf 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72  r**);.  int (*pr
28ac0 65 70 61 72 65 31 36 5f 76 32 29 28 73 71 6c 69  epare16_v2)(sqli
28ac1 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  te3*,const void*
28ac2 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
28ac3 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a  t**,const void**
28ac4 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61 72  );.  int (*clear
28ac5 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74  _bindings)(sqlit
28ac6 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a 20  e3_stmt*);.  /* 
28ac7 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31 20 2a  Added by 3.4.1 *
28ac8 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  /.  int (*create
28ac9 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c 69  _module_v2)(sqli
28aca 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
28acb 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
28acc 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f 69  odule*,void*,voi
28acd 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
28ace 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64  id *));.  /* Add
28acf 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20  ed by 3.5.0 */. 
28ad0 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f   int (*bind_zero
28ad1 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74  blob)(sqlite3_st
28ad2 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  mt*,int,int);.  
28ad3 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73  int (*blob_bytes
28ad4 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29  )(sqlite3_blob*)
28ad5 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63  ;.  int (*blob_c
28ad6 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62 6c  lose)(sqlite3_bl
28ad7 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  ob*);.  int (*bl
28ad8 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65 33  ob_open)(sqlite3
28ad9 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
28ada 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
28adb 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  char*,sqlite3_in
28adc 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  t64,int,sqlite3_
28add 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  blob**);.  int (
28ade 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c 69  *blob_read)(sqli
28adf 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c  te3_blob*,void*,
28ae0 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  int,int);.  int 
28ae1 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73 71  (*blob_write)(sq
28ae2 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73  lite3_blob*,cons
28ae3 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  t void*,int,int)
28ae4 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  ;.  int (*create
28ae5 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28 73  _collation_v2)(s
28ae6 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
28ae7 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e  ar*,int,void*,in
28ae8 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
28ae9 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
28aea 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69 64  onst void*),void
28aeb 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
28aec 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f  nt (*file_contro
28aed 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  l)(sqlite3*,cons
28aee 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
28aef 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  *);.  sqlite3_in
28af0 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67  t64 (*memory_hig
28af1 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20 20  hwater)(int);.  
28af2 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a  sqlite3_int64 (*
28af3 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69  memory_used)(voi
28af4 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d);.  sqlite3_mu
28af5 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c  tex *(*mutex_all
28af6 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  oc)(int);.  void
28af7 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29 28   (*mutex_enter)(
28af8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
28af9 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f  .  void (*mutex_
28afa 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  free)(sqlite3_mu
28afb 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  tex*);.  void (*
28afc 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71 6c  mutex_leave)(sql
28afd 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20  ite3_mutex*);.  
28afe 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79 29  int (*mutex_try)
28aff 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
28b00 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76  ;.  int (*open_v
28b01 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  2)(const char*,s
28b02 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e  qlite3**,int,con
28b03 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74  st char*);.  int
28b04 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72   (*release_memor
28b05 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  y)(int);.  void 
28b06 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  (*result_error_n
28b07 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f  omem)(sqlite3_co
28b08 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20  ntext*);.  void 
28b09 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  (*result_error_t
28b0a 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f 63  oobig)(sqlite3_c
28b0b 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20  ontext*);.  int 
28b0c 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20  (*sleep)(int);. 
28b0d 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65 61   void (*soft_hea
28b0e 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20  p_limit)(int);. 
28b0f 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a   sqlite3_vfs *(*
28b10 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20  vfs_find)(const 
28b11 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
28b12 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73 71  vfs_register)(sq
28b13 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b  lite3_vfs*,int);
28b14 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72  .  int (*vfs_unr
28b15 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
28b16 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  _vfs*);.  int (*
28b17 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f 69  xthreadsafe)(voi
28b18 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  d);.  void (*res
28b19 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71  ult_zeroblob)(sq
28b1a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
28b1b 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  nt);.  void (*re
28b1c 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29  sult_error_code)
28b1d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
28b1e 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  *,int);.  int (*
28b1f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e  test_control)(in
28b20 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20  t, ...);.  void 
28b21 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e  (*randomness)(in
28b22 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  t,void*);.  sqli
28b23 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64  te3 *(*context_d
28b24 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65  b_handle)(sqlite
28b25 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69  3_context*);.  i
28b26 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72 65  nt (*extended_re
28b27 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c 69  sult_codes)(sqli
28b28 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  te3*,int);.  int
28b29 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74 65   (*limit)(sqlite
28b2a 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73  3*,int,int);.  s
28b2b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e  qlite3_stmt *(*n
28b2c 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74 65  ext_stmt)(sqlite
28b2d 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  3*,sqlite3_stmt*
28b2e 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
28b2f 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33 5f  *(*sql)(sqlite3_
28b30 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  stmt*);.  int (*
28b31 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a  status)(int,int*
28b32 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a  ,int*,int);.};..
28b33 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
28b34 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66  ing macros redef
28b35 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75 74  ine the API rout
28b36 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68 65  ines so that the
28b37 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63  y are.** redirec
28b38 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68 65  ted throught the
28b39 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f   global sqlite3_
28b3a 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  api structure..*
28b3b 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
28b3c 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73   file is also us
28b3d 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78  ed by the loadex
28b3e 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a  t.c source file.
28b3f 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  ** (part of the 
28b40 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72  main SQLite libr
28b41 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74  ary - not an ext
28b42 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a  ension) so that.
28b43 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63  ** it can get ac
28b44 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
28b45 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
28b46 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65   structure.** de
28b47 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74  finition.  But t
28b48 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20  he main library 
28b49 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f  does not want to
28b4a 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65   redefine.** the
28b4b 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65   API.  So the re
28b4c 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f  definition macro
28b4d 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  s are only valid
28b4e 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54   if the.** SQLIT
28b4f 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73  E_CORE macros is
28b50 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23   undefined..*/.#
28b51 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
28b52 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  RE.#define sqlit
28b53 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
28b54 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74 65  text      sqlite
28b55 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65  3_api->aggregate
28b56 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65 66  _context.#ifndef
28b57 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
28b58 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20  RECATED.#define 
28b59 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
28b5a 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73  e_count        s
28b5b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72  qlite3_api->aggr
28b5c 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64  egate_count.#end
28b5d 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
28b5e 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20  e3_bind_blob    
28b5f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b60 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62  3_api->bind_blob
28b61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b62 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20  _bind_double    
28b63 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b64 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65  api->bind_double
28b65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28b66 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20 20  _bind_int       
28b67 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28b68 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64  api->bind_int.#d
28b69 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
28b6a 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  nd_int64        
28b6b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28b6c 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65  ->bind_int64.#de
28b6d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
28b6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  d_null          
28b6f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28b70 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69  >bind_null.#defi
28b71 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
28b72 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20  parameter_count 
28b73 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28b74 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
28b75 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
28b76 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
28b77 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74  er_index   sqlit
28b78 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
28b79 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64 65  ameter_index.#de
28b7a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
28b7b 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
28b7c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28b7d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
28b7e 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
28b7f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 20  ite3_bind_text  
28b80 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28b81 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65  te3_api->bind_te
28b82 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
28b83 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
28b84 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b85 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74  3_api->bind_text
28b86 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28b87 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20 20  e3_bind_value   
28b88 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b89 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75  3_api->bind_valu
28b8a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28b8b 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 20  3_busy_handler  
28b8c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28b8d 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c  _api->busy_handl
28b8e 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
28b8f 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 20  e3_busy_timeout 
28b90 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28b91 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65  3_api->busy_time
28b92 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  out.#define sqli
28b93 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20 20  te3_changes     
28b94 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28b95 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a  e3_api->changes.
28b96 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28b97 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  close           
28b98 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28b99 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e  pi->close.#defin
28b9a 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
28b9b 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20 20  ion_needed      
28b9c 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28b9d 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23  llation_needed.#
28b9e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28b9f 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
28ba0 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
28ba1 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  i->collation_nee
28ba2 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ded16.#define sq
28ba3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
28ba4 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  b            sql
28ba5 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
28ba6 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
28ba7 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
28ba8 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  es           sql
28ba9 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
28baa 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
28bab 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
28bac 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73 71  tes16         sq
28bad 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
28bae 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  n_bytes16.#defin
28baf 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28bb0 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20  _count          
28bb1 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28bb2 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69  lumn_count.#defi
28bb3 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bb4 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
28bb5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bb6 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
28bb7 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
28bb8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28bb9 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74  ase_name16 sqlit
28bba 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
28bbb 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23  atabase_name16.#
28bbc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28bbd 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20  olumn_decltype  
28bbe 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28bbf 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  i->column_declty
28bc0 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  pe.#define sqlit
28bc1 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
28bc2 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  pe16      sqlite
28bc3 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65  3_api->column_de
28bc4 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e 65  cltype16.#define
28bc5 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28bc6 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
28bc7 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
28bc8 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  umn_double.#defi
28bc9 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bca 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  n_int           
28bcb 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bcc 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e  olumn_int.#defin
28bcd 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28bce 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
28bcf 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28bd0 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69  lumn_int64.#defi
28bd1 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bd2 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  n_name          
28bd3 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bd4 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69  olumn_name.#defi
28bd5 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bd6 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20  n_name16        
28bd7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bd8 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  olumn_name16.#de
28bd9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28bda 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20  umn_origin_name 
28bdb 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bdc 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
28bdd 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
28bde 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
28bdf 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69 74  n_name16   sqlit
28be0 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f  e3_api->column_o
28be1 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  rigin_name16.#de
28be2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28be3 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
28be4 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28be5 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  >column_table_na
28be6 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
28be7 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
28be8 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74 65  name16    sqlite
28be9 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61  3_api->column_ta
28bea 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  ble_name16.#defi
28beb 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bec 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  n_text          
28bed 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bee 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69  olumn_text.#defi
28bef 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28bf0 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
28bf1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28bf2 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65  olumn_text16.#de
28bf3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28bf4 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20  umn_type        
28bf5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bf6 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65  >column_type.#de
28bf7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28bf8 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20  umn_value       
28bf9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bfa 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64  >column_value.#d
28bfb 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28bfc 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20  mmit_hook       
28bfd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28bfe 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64  ->commit_hook.#d
28bff 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28c00 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  mplete          
28c01 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28c02 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69  ->complete.#defi
28c03 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
28c04 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20 20  ete16           
28c05 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28c06 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69 6e  omplete16.#defin
28c07 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28c08 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20  _collation      
28c09 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
28c0a 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23  eate_collation.#
28c0b 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28c0c 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
28c0d 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
28c0e 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  i->create_collat
28c0f 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ion16.#define sq
28c10 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
28c11 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71 6c  ction        sql
28c12 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
28c13 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e  _function.#defin
28c14 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28c15 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20  _function16     
28c16 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
28c17 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a  eate_function16.
28c18 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28c19 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20  create_module   
28c1a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28c1b 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c  pi->create_modul
28c1c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28c1d 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
28c1e 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  v2       sqlite3
28c1f 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64  _api->create_mod
28c20 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  ule_v2.#define s
28c21 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
28c22 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
28c23 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f  lite3_api->data_
28c24 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
28c25 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
28c26 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28c27 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e  ite3_api->db_han
28c28 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dle.#define sqli
28c29 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
28c2a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28c2b 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f  e3_api->declare_
28c2c 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  vtab.#define sql
28c2d 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
28c2e 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c 69  ed_cache    sqli
28c2f 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f  te3_api->enable_
28c30 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64 65  shared_cache.#de
28c31 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
28c32 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20 20  code            
28c33 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28c34 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65  >errcode.#define
28c35 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20   sqlite3_errmsg 
28c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c37 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72  sqlite3_api->err
28c38 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  msg.#define sqli
28c39 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20  te3_errmsg16    
28c3a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28c3b 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36  e3_api->errmsg16
28c3c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c3d 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20 20  _exec           
28c3e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c3f 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65  api->exec.#ifnde
28c40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
28c41 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
28c42 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
28c43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c44 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 70  sqlite3_api->exp
28c45 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65 66  ired.#endif.#def
28c46 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ine sqlite3_fina
28c47 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  lize            
28c48 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28c49 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65  finalize.#define
28c4a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20 20   sqlite3_free   
28c4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c4c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65  sqlite3_api->fre
28c4d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28c4e 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20  3_free_table    
28c4f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28c50 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65  _api->free_table
28c51 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c52 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20  _get_autocommit 
28c53 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c54 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d  api->get_autocom
28c55 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
28c56 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 20  te3_get_auxdata 
28c57 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28c58 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78 64  e3_api->get_auxd
28c59 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
28c5a 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20  te3_get_table   
28c5b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28c5c 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c  e3_api->get_tabl
28c5d 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  e.#ifndef SQLITE
28c5e 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
28c5f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c60 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20  _global_recover 
28c61 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c62 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f  api->global_reco
28c63 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ver.#endif.#defi
28c64 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ne sqlite3_inter
28c65 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20 20  rupt            
28c66 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69    sqlite3_api->i
28c67 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69 6e  nterruptx.#defin
28c68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  e sqlite3_last_i
28c69 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20  nsert_rowid     
28c6a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61   sqlite3_api->la
28c6b 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a  st_insert_rowid.
28c6c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28c6d 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20  libversion      
28c6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28c6f 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23  pi->libversion.#
28c70 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
28c71 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
28c72 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28c73 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  i->libversion_nu
28c74 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  mber.#define sql
28c75 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20  ite3_malloc     
28c76 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28c77 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a  te3_api->malloc.
28c78 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28c79 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  mprintf         
28c7a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28c7b 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66  pi->mprintf.#def
28c7c 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
28c7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c7e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28c7f 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  open.#define sql
28c80 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
28c81 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28c82 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a  te3_api->open16.
28c83 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28c84 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20 20  prepare         
28c85 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28c86 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65 66  pi->prepare.#def
28c87 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
28c88 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20 20  are16           
28c89 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28c8a 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69 6e  prepare16.#defin
28c8b 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
28c8c 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20  e_v2            
28c8d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
28c8e 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65  epare_v2.#define
28c8f 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28c90 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
28c91 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
28c92 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e  pare16_v2.#defin
28c93 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  e sqlite3_profil
28c94 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
28c95 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
28c96 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ofile.#define sq
28c97 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
28c98 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71 6c  andler       sql
28c99 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65  ite3_api->progre
28c9a 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  ss_handler.#defi
28c9b 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ne sqlite3_reall
28c9c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oc              
28c9d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
28c9e 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  ealloc.#define s
28c9f 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20 20  qlite3_reset    
28ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28ca1 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65 74  lite3_api->reset
28ca2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28ca3 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20  _result_blob    
28ca4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28ca5 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62  api->result_blob
28ca6 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28ca7 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20  _result_double  
28ca8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28ca9 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62  api->result_doub
28caa 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
28cab 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20  e3_result_error 
28cac 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28cad 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72  3_api->result_er
28cae 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ror.#define sqli
28caf 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
28cb0 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
28cb1 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
28cb2 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20 73  rror16.#define s
28cb3 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
28cb4 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
28cb5 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28cb6 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  t_int.#define sq
28cb7 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
28cb8 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  64           sql
28cb9 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
28cba 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73  _int64.#define s
28cbb 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
28cbc 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ll            sq
28cbd 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28cbe 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73  t_null.#define s
28cbf 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
28cc0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71  xt            sq
28cc1 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28cc2 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  t_text.#define s
28cc3 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
28cc4 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73 71  xt16          sq
28cc5 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28cc6 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65  t_text16.#define
28cc7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28cc8 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
28cc9 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28cca 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64 65  ult_text16be.#de
28ccb 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
28ccc 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20  ult_text16le    
28ccd 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28cce 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  >result_text16le
28ccf 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28cd0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20 20  _result_value   
28cd1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28cd2 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75  api->result_valu
28cd3 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28cd4 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20  3_rollback_hook 
28cd5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28cd6 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68  _api->rollback_h
28cd7 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ook.#define sqli
28cd8 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
28cd9 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
28cda 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74 68  e3_api->set_auth
28cdb 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20 73  orizer.#define s
28cdc 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
28cdd 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ta            sq
28cde 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61  lite3_api->set_a
28cdf 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  uxdata.#define s
28ce0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 20  qlite3_snprintf 
28ce1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28ce2 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69  lite3_api->snpri
28ce3 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
28ce4 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20 20  te3_step        
28ce5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28ce6 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64 65  e3_api->step.#de
28ce7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
28ce8 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
28ce9 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  ta  sqlite3_api-
28cea 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  >table_column_me
28ceb 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  tadata.#define s
28cec 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
28ced 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73 71  eanup         sq
28cee 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61  lite3_api->threa
28cef 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e  d_cleanup.#defin
28cf0 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  e sqlite3_total_
28cf1 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  changes         
28cf2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f   sqlite3_api->to
28cf3 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65 66  tal_changes.#def
28cf4 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ine sqlite3_trac
28cf5 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
28cf6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28cf7 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53 51  trace.#ifndef SQ
28cf8 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
28cf9 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
28cfa 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
28cfb 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69  ndings      sqli
28cfc 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65  te3_api->transfe
28cfd 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69  r_bindings.#endi
28cfe 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
28cff 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20  3_update_hook   
28d00 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d01 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f  _api->update_hoo
28d02 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
28d03 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20 20  3_user_data     
28d04 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d05 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a  _api->user_data.
28d06 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d07 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20  value_blob      
28d08 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28d09 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23  pi->value_blob.#
28d0a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28d0b 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20 20  alue_bytes      
28d0c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d0d 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23  i->value_bytes.#
28d0e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28d0f 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20 20  alue_bytes16    
28d10 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d11 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36  i->value_bytes16
28d12 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28d13 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20  _value_double   
28d14 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28d15 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c  api->value_doubl
28d16 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28d17 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20  3_value_int     
28d18 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d19 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a  _api->value_int.
28d1a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d1b 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20  value_int64     
28d1c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28d1d 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a  pi->value_int64.
28d1e 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d1f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
28d20 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  pe     sqlite3_a
28d21 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69  pi->value_numeri
28d22 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  c_type.#define s
28d23 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28d24 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
28d25 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
28d26 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71  _text.#define sq
28d27 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28d28 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  16           sql
28d29 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
28d2a 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
28d2b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28d2c 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71  t16be         sq
28d2d 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
28d2e 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
28d2f 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
28d30 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
28d31 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
28d32 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65  lue_text16le.#de
28d33 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
28d34 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20 20  ue_type         
28d35 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d36 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66  >value_type.#def
28d37 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  ine sqlite3_vmpr
28d38 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
28d39 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28d3a 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  vmprintf.#define
28d3b 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
28d3c 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  d_function      
28d3d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65  sqlite3_api->ove
28d3e 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23  rload_function.#
28d3f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
28d40 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20  repare_v2       
28d41 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d42 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64  i->prepare_v2.#d
28d43 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
28d44 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
28d45 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28d46 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23  ->prepare16_v2.#
28d47 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28d48 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20  lear_bindings   
28d49 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d4a 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  i->clear_binding
28d4b 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
28d4c 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
28d4d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d4e 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62  _api->bind_zerob
28d4f 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
28d50 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20  te3_blob_bytes  
28d51 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d52 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74  e3_api->blob_byt
28d53 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
28d54 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20  e3_blob_close   
28d55 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28d56 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73  3_api->blob_clos
28d57 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28d58 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20  3_blob_open     
28d59 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d5a 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a  _api->blob_open.
28d5b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d5c 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20  blob_read       
28d5d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28d5e 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64  pi->blob_read.#d
28d5f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
28d60 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20 20  ob_write        
28d61 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28d62 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65  ->blob_write.#de
28d63 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
28d64 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
28d65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d66 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
28d67 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
28d68 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
28d69 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  l           sqli
28d6a 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f  te3_api->file_co
28d6b 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71  ntrol.#define sq
28d6c 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
28d6d 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c  hwater       sql
28d6e 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
28d6f 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66 69  _highwater.#defi
28d70 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  ne sqlite3_memor
28d71 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20 20  y_used          
28d72 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28d73 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69  emory_used.#defi
28d74 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
28d75 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  _alloc          
28d76 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28d77 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69  utex_alloc.#defi
28d78 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
28d79 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20 20  _enter          
28d7a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28d7b 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69  utex_enter.#defi
28d7c 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
28d7d 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20  _free           
28d7e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28d7f 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e  utex_free.#defin
28d80 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
28d81 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20 20  leave           
28d82 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
28d83 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e  tex_leave.#defin
28d84 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
28d85 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20  try             
28d86 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
28d87 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20  tex_try.#define 
28d88 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20  sqlite3_open_v2 
28d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28d8a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e  qlite3_api->open
28d8b 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
28d8c 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
28d8d 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ry         sqlit
28d8e 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f  e3_api->release_
28d8f 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73  memory.#define s
28d90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
28d91 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71  ror_nomem     sq
28d92 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28d93 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64  t_error_nomem.#d
28d94 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28d95 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
28d96 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  g    sqlite3_api
28d97 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  ->result_error_t
28d98 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71  oobig.#define sq
28d99 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20  lite3_sleep     
28d9a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28d9b 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a  ite3_api->sleep.
28d9c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d9d 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20  soft_heap_limit 
28d9e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28d9f 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69  pi->soft_heap_li
28da0 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
28da1 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20  te3_vfs_find    
28da2 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28da3 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64  e3_api->vfs_find
28da4 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28da5 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20 20  _vfs_register   
28da6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28da7 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65  api->vfs_registe
28da8 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
28da9 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
28daa 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28dab 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69  _api->vfs_unregi
28dac 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  ster.#define sql
28dad 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20  ite3_threadsafe 
28dae 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28daf 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64  te3_api->xthread
28db0 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  safe.#define sql
28db1 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
28db2 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69  blob        sqli
28db3 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28db4 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  zeroblob.#define
28db5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28db6 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20  error_code      
28db7 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
28db8 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23  ult_error_code.#
28db9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
28dba 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20  est_control     
28dbb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28dbc 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a  i->test_control.
28dbd 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28dbe 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20  randomness      
28dbf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28dc0 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23  pi->randomness.#
28dc1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28dc2 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
28dc3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28dc4 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  i->context_db_ha
28dc5 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
28dc6 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
28dc7 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c 69  sult_codes  sqli
28dc8 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65  te3_api->extende
28dc9 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23  d_result_codes.#
28dca 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
28dcb 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20  imit            
28dcc 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28dcd 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65  i->limit.#define
28dce 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74   sqlite3_next_st
28dcf 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mt              
28dd0 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78  sqlite3_api->nex
28dd1 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20 73  t_stmt.#define s
28dd2 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20 20  qlite3_sql      
28dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28dd4 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23  lite3_api->sql.#
28dd5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
28dd6 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20 20  tatus           
28dd7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28dd8 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64 69 66  i->status.#endif
28dd9 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20   /* SQLITE_CORE 
28dda 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */..#define SQLI
28ddb 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
28ddc 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  T1     const sql
28ddd 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
28dde 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69 20 3d  s *sqlite3_api =
28ddf 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   0;.#define SQLI
28de0 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
28de1 54 32 28 76 29 20 20 73 71 6c 69 74 65 33 5f 61  T2(v)  sqlite3_a
28de2 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20  pi = v;..#endif 
28de3 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48  /* _SQLITE3EXT_H
28de4 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
28de5 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
28de6 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a  ite3ext.h ******
28de7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28de8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28de9 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
28dea 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
28deb 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
28dec 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20  ff in loadext.c 
28ded 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28dee 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ****/..#ifndef S
28def 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
28df0 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a  EXTENSION../*.**
28df1 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e   Some API routin
28df2 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77  es are omitted w
28df3 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74  hen various feat
28df4 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c  ures are.** excl
28df5 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c  uded from a buil
28df6 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75  d of SQLite.  Su
28df7 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20  bstitute a NULL 
28df8 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61  pointer.** for a
28df9 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73 2e  ny missing APIs.
28dfa 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
28dfb 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
28dfc 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66 69  _METADATA.# defi
28dfd 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28dfe 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
28dff 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28e00 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
28e01 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20  base_name16 0.# 
28e02 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28e03 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
28e04 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
28e05 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28e06 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20  table_name16    
28e07 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28e08 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
28e09 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20 64 65  _name     0.# de
28e0a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28e0b 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
28e0c 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73  6   0.# define s
28e0d 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
28e0e 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 30 0a  umn_metadata  0.
28e0f 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28e10 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
28e11 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e  RIZATION.# defin
28e12 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
28e13 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20  thorizer        
28e14 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
28e15 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
28e16 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  F16.# define sql
28e17 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
28e18 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
28e19 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28e1a 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
28e1b 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  6     0.# define
28e1c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28e1d 64 65 63 6c 74 79 70 65 31 36 20 20 20 20 20 20  decltype16      
28e1e 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28e1f 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
28e20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
28e21 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28e22 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20  umn_text16      
28e23 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28e24 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
28e25 36 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a  6             0.
28e26 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28e27 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
28e28 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69  n16     0.# defi
28e29 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
28e2a 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
28e2b 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28e2c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
28e2d 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
28e2e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f  define sqlite3_o
28e2f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20  pen16           
28e30 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
28e31 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28e32 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
28e33 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28e34 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20  e3_prepare16_v2 
28e35 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
28e36 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
28e37 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
28e38 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28e39 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
28e3a 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a  xt16          0.
28e3b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28e3c 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
28e3d 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
28e3e 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
28e3f 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20  t_text16le      
28e40 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28e41 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
28e42 36 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  6           0.# 
28e43 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28e44 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20 20  alue_text16be   
28e45 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
28e46 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
28e47 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 20  ext16le         
28e48 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28e49 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
28e4a 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65  se_name16 0.# de
28e4b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
28e4c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36  umn_table_name16
28e4d 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
28e4e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
28e4f 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a  igin_name16   0.
28e50 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28e51 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c  QLITE_OMIT_COMPL
28e52 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ETE.# define sql
28e53 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a  ite3_complete 0.
28e54 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28e55 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65  _complete16 0.#e
28e56 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
28e57 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
28e58 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65 66  S_CALLBACK.# def
28e59 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  ine sqlite3_prog
28e5a 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a 23  ress_handler 0.#
28e5b 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
28e5c 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
28e5d 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20  LTABLE.# define 
28e5e 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
28e5f 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69 6e 65  odule 0.# define
28e60 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28e61 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20 64 65  module_v2 0.# de
28e62 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
28e63 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65 6e 64  lare_vtab 0.#end
28e64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
28e65 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
28e66 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  CHE.# define sql
28e67 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
28e68 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e 64 69  ed_cache 0.#endi
28e69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
28e6a 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20 64 65  _OMIT_TRACE.# de
28e6b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
28e6c 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23 20 64  file       0.# d
28e6d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
28e6e 61 63 65 20 20 20 20 20 20 20 20 20 30 0a 23 65  ace         0.#e
28e6f 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
28e70 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42  ITE_OMIT_GET_TAB
28e71 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  LE.# define sqli
28e72 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20  te3_free_table  
28e73 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
28e74 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20  ite3_get_table  
28e75 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66     0.#endif..#if
28e76 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28e77 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e 65  INCRBLOB.#define
28e78 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65   sqlite3_bind_ze
28e79 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66 69 6e  roblob  0.#defin
28e7a 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62  e sqlite3_blob_b
28e7b 79 74 65 73 20 20 20 20 20 30 0a 23 64 65 66 69  ytes     0.#defi
28e7c 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28e7d 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64 65 66  close     0.#def
28e7e 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28e7f 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23 64 65  _open      0.#de
28e80 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
28e81 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a 23 64  b_read      0.#d
28e82 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
28e83 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30 0a 23  ob_write     0.#
28e84 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
28e85 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
28e86 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f  ture contains po
28e87 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53 51  inters to all SQ
28e88 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65  Lite API routine
28e89 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  s..** A pointer 
28e8a 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
28e8b 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74 6f  e is passed into
28e8c 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e   extensions when
28e8d 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61   they are.** loa
28e8e 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ded so that the 
28e8f 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61  extension can ma
28e90 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e  ke calls back in
28e91 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a  to the SQLite.**
28e92 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
28e93 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77 20  When adding new 
28e94 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20 74  APIs, add them t
28e95 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  o the bottom of 
28e96 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
28e97 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72  * in order to pr
28e98 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64 73  eserve backwards
28e99 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
28e9a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73  **.** Extensions
28e9b 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72 20   that use newer 
28e9c 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72 73  APIs should firs
28e9d 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71  t call the.** sq
28e9e 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
28e9f 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b  _number() to mak
28ea0 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
28ea1 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65  API they.** inte
28ea2 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75 70  nd to use is sup
28ea3 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c 69  ported by the li
28ea4 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f  brary.  Extensio
28ea5 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73  ns should.** als
28ea6 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
28ea7 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 6f  sure that the po
28ea8 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e  inter to the fun
28ea9 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ction is.** not 
28eaa 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c  NULL before call
28eab 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  ing it..*/.stati
28eac 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
28ead 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c  api_routines sql
28eae 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20 20 73  ite3Apis = {.  s
28eaf 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
28eb0 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64 65  _context,.#ifnde
28eb1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
28eb2 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74  PRECATED.  sqlit
28eb3 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
28eb4 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  nt,.#else.  0,.#
28eb5 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
28eb6 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  bind_blob,.  sql
28eb7 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
28eb8 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28eb9 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
28eba 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73 71  bind_int64,.  sq
28ebb 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c  lite3_bind_null,
28ebc 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
28ebd 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c  parameter_count,
28ebe 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
28ebf 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c  parameter_index,
28ec0 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
28ec1 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c 0a  parameter_name,.
28ec2 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
28ec3 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ext,.  sqlite3_b
28ec4 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  ind_text16,.  sq
28ec5 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65  lite3_bind_value
28ec6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ,.  sqlite3_busy
28ec7 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
28ec8 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
28ec9 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  ,.  sqlite3_chan
28eca 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ges,.  sqlite3_c
28ecb 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lose,.  sqlite3_
28ecc 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28ecd 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ,.  sqlite3_coll
28ece 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c 0a  ation_needed16,.
28ecf 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28ed0 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  _blob,.  sqlite3
28ed1 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a 20  _column_bytes,. 
28ed2 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28ed3 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c 69 74  bytes16,.  sqlit
28ed4 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c  e3_column_count,
28ed5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28ed6 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c  n_database_name,
28ed7 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28ed8 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
28ed9 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
28eda 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20 20  umn_decltype,.  
28edb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
28edc 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73 71 6c  ecltype16,.  sql
28edd 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62  ite3_column_doub
28ede 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  le,.  sqlite3_co
28edf 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  lumn_int,.  sqli
28ee0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
28ee1 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28ee2 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  mn_name,.  sqlit
28ee3 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
28ee4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28ee5 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c 0a  mn_origin_name,.
28ee6 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28ee7 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c 0a  _origin_name16,.
28ee8 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28ee9 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20 73  _table_name,.  s
28eea 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
28eeb 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ble_name16,.  sq
28eec 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
28eed 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
28eee 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  umn_text16,.  sq
28eef 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
28ef0 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
28ef1 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  umn_value,.  sql
28ef2 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
28ef3 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ,.  sqlite3_comp
28ef4 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lete,.  sqlite3_
28ef5 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73 71  complete16,.  sq
28ef6 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
28ef7 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65  lation,.  sqlite
28ef8 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
28ef9 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  on16,.  sqlite3_
28efa 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 2c  create_function,
28efb 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
28efc 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20 20  e_function16,.  
28efd 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
28efe 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  odule,.  sqlite3
28eff 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20 73  _data_count,.  s
28f00 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
28f01 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  ,.  sqlite3_decl
28f02 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c 69  are_vtab,.  sqli
28f03 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65  te3_enable_share
28f04 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c 69 74  d_cache,.  sqlit
28f05 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73 71  e3_errcode,.  sq
28f06 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20 20  lite3_errmsg,.  
28f07 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
28f08 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63  ,.  sqlite3_exec
28f09 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
28f0a 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
28f0b 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  .  sqlite3_expir
28f0c 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  ed,.#else.  0,.#
28f0d 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
28f0e 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c 69  finalize,.  sqli
28f0f 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  te3_free,.  sqli
28f10 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c 0a  te3_free_table,.
28f11 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75    sqlite3_get_au
28f12 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c 69  tocommit,.  sqli
28f13 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 2c  te3_get_auxdata,
28f14 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  .  sqlite3_get_t
28f15 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20 2f  able,.  0,     /
28f16 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f 67 6c  * Was sqlite3_gl
28f17 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c 20  obal_recover(), 
28f18 62 75 74 20 74 68 61 74 20 66 75 6e 63 74 69 6f  but that functio
28f19 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 20  n is deprecated 
28f1a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
28f1b 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69 74 65  errupt,.  sqlite
28f1c 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
28f1d 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c  wid,.  sqlite3_l
28f1e 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71 6c  ibversion,.  sql
28f1f 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
28f20 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74 65  number,.  sqlite
28f21 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  3_malloc,.  sqli
28f22 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20 73  te3_mprintf,.  s
28f23 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20 73  qlite3_open,.  s
28f24 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a 20  qlite3_open16,. 
28f25 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28f26 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ,.  sqlite3_prep
28f27 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  are16,.  sqlite3
28f28 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c 69  _profile,.  sqli
28f29 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
28f2a 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  dler,.  sqlite3_
28f2b 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74  realloc,.  sqlit
28f2c 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c 69  e3_reset,.  sqli
28f2d 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 2c  te3_result_blob,
28f2e 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
28f2f 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69  t_double,.  sqli
28f30 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
28f31 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28f32 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73 71  lt_error16,.  sq
28f33 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
28f34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28f35 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69  lt_int64,.  sqli
28f36 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c  te3_result_null,
28f37 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
28f38 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  t_text,.  sqlite
28f39 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 2c  3_result_text16,
28f3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
28f3b 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71  t_text16be,.  sq
28f3c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
28f3d 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
28f3e 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a 20  _result_value,. 
28f3f 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63   sqlite3_rollbac
28f40 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  k_hook,.  sqlite
28f41 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
28f42 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ,.  sqlite3_set_
28f43 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69 74  auxdata,.  sqlit
28f44 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20 73  e3_snprintf,.  s
28f45 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20 20 73  qlite3_step,.  s
28f46 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
28f47 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23 69  umn_metadata,.#i
28f48 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
28f49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73  T_DEPRECATED.  s
28f4a 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
28f4b 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20 30  eanup,.#else.  0
28f4c 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ,.#endif.  sqlit
28f4d 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
28f4e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ,.  sqlite3_trac
28f4f 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e,.#ifndef SQLIT
28f50 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28f51 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61 6e  D.  sqlite3_tran
28f52 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 23  sfer_bindings,.#
28f53 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66  else.  0,.#endif
28f54 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  .  sqlite3_updat
28f55 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65  e_hook,.  sqlite
28f56 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20 73  3_user_data,.  s
28f57 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
28f58 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  b,.  sqlite3_val
28f59 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69  ue_bytes,.  sqli
28f5a 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
28f5b 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
28f5c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  ue_double,.  sql
28f5d 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0a  ite3_value_int,.
28f5e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28f5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
28f60 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
28f61 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ype,.  sqlite3_v
28f62 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71 6c  alue_text,.  sql
28f63 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
28f64 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  6,.  sqlite3_val
28f65 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73  ue_text16be,.  s
28f66 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28f67 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16le,.  sqlite3
28f68 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20 73  _value_type,.  s
28f69 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 2c  qlite3_vmprintf,
28f6a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6f  .  /*.  ** The o
28f6b 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74 20  riginal API set 
28f6c 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c 20  ends here.  All 
28f6d 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20 63  extensions can c
28f6e 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  all any.  ** of 
28f6f 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20 70  the APIs above p
28f70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68 65  rovided that the
28f71 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
28f72 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a 20  NULL.  But.  ** 
28f73 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 41  before calling A
28f74 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 2c  PIs that follow,
28f75 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75 6c   extension shoul
28f76 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a 2a  d check the.  **
28f77 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
28f78 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20  ion_number() to 
28f79 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61  make sure they a
28f7a 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 0a  re dealing with.
28f7b 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20 74    ** a library t
28f7c 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75 67  hat is new enoug
28f7d 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 61  h to support tha
28f7e 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a  t API..  *******
28f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28f83 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  **.  */.  sqlite
28f84 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
28f85 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ion,..  /*.  ** 
28f86 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33 2e  Added after 3.3.
28f87 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  13.  */.  sqlite
28f88 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20 20  3_prepare_v2,.  
28f89 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
28f8a 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f  6_v2,.  sqlite3_
28f8b 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c 0a  clear_bindings,.
28f8c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
28f8d 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f 0a   for 3.4.1.  */.
28f8e 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
28f8f 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20 2f  _module_v2,..  /
28f90 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72  *.  ** Added for
28f91 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73 71   3.5.0.  */.  sq
28f92 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
28f93 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  lob,.  sqlite3_b
28f94 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lob_bytes,.  sql
28f95 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 2c  ite3_blob_close,
28f96 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  .  sqlite3_blob_
28f97 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
28f98 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71 6c  blob_read,.  sql
28f99 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 2c  ite3_blob_write,
28f9a 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
28f9b 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c 0a  e_collation_v2,.
28f9c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63    sqlite3_file_c
28f9d 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65  ontrol,.  sqlite
28f9e 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
28f9f 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65  er,.  sqlite3_me
28fa0 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64 65  mory_used,.#ifde
28fa1 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
28fa2 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20 0a  MIT.  0, .  0, .
28fa3 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a 23    0,.  0,.  0,.#
28fa4 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f 6d  else.  sqlite3_m
28fa5 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73 71  utex_alloc,.  sq
28fa6 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
28fa7 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  r,.  sqlite3_mut
28fa8 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74  ex_free,.  sqlit
28fa9 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c 0a  e3_mutex_leave,.
28faa 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
28fab 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71  try,.#endif.  sq
28fac 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a 20  lite3_open_v2,. 
28fad 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
28fae 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69 74  _memory,.  sqlit
28faf 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
28fb0 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65 33  nomem,.  sqlite3
28fb1 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
28fb2 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  obig,.  sqlite3_
28fb3 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65 33  sleep,.  sqlite3
28fb4 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
28fb5 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ,.  sqlite3_vfs_
28fb6 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  find,.  sqlite3_
28fb7 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20 20  vfs_register,.  
28fb8 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
28fb9 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20 20  gister,..  /*.  
28fba 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35  ** Added for 3.5
28fbb 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  .8.  */.  sqlite
28fbc 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a 20 20  3_threadsafe,.  
28fbd 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
28fbe 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  eroblob,.  sqlit
28fbf 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
28fc0 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  code,.  sqlite3_
28fc1 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20  test_control,.  
28fc2 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
28fc3 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ss,.  sqlite3_co
28fc4 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 2c  ntext_db_handle,
28fc5 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
28fc6 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a 2f  d for 3.6.0.  */
28fc7 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  .  sqlite3_exten
28fc8 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
28fc9 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  ,.  sqlite3_limi
28fca 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65 78  t,.  sqlite3_nex
28fcb 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74 65  t_stmt,.  sqlite
28fcc 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65 33  3_sql,.  sqlite3
28fcd 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a  _status,.};../*.
28fce 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  ** Attempt to lo
28fcf 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74  ad an SQLite ext
28fd0 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63  ension library c
28fd1 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
28fd2 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20  file.** zFile.  
28fd3 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20  The entry point 
28fd4 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63  is zProc.  zProc
28fd5 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68 69   may be 0 in whi
28fd6 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66  ch case a.** def
28fd7 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74  ault entry point
28fd8 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65   name (sqlite3_e
28fd9 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69  xtension_init) i
28fda 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20  s used.  Use.** 
28fdb 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e  of the default n
28fdc 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ame is recommend
28fdd 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
28fde 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
28fdf 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45  ccess and SQLITE
28fe0 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68  _ERROR if someth
28fe1 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
28fe2 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
28fe3 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45  r occurs and pzE
28fe4 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20  rrMsg is not 0, 
28fe5 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72  then fill *pzErr
28fe6 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72  Msg with .** err
28fe7 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e  or message text.
28fe8 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
28fe9 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72  nction should fr
28fea 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a  ee this memory.*
28feb 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c  * by calling sql
28fec 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29  ite3DbFree(db, )
28fed 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
28fee 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e  sqlite3LoadExten
28fef 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  sion(.  sqlite3 
28ff0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
28ff1 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73   Load the extens
28ff2 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61  ion into this da
28ff3 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
28ff4 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
28ff5 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20  r *zFile,    /* 
28ff6 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72  Name of the shar
28ff7 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  ed library conta
28ff8 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20  ining extension 
28ff9 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
28ffa 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e  *zProc,    /* En
28ffb 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20  try point.  Use 
28ffc 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
28ffd 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f  on_init" if 0 */
28ffe 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
28fff 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20  sg       /* Put 
29000 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65  error message he
29001 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29  re if not 0 */.)
29002 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
29003 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73  *pVfs = db->pVfs
29004 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65  ;.  void *handle
29005 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29  ;.  int (*xInit)
29006 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
29007 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
29008 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20  pi_routines*);. 
29009 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d   char *zErrmsg =
2900a 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61   0;.  void **aHa
2900b 6e 64 6c 65 3b 0a 20 20 63 6f 6e 73 74 20 69 6e  ndle;.  const in
2900c 74 20 6e 4d 73 67 20 3d 20 33 30 30 3b 0a 0a 20  t nMsg = 300;.. 
2900d 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 20   if( pzErrMsg ) 
2900e 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a  *pzErrMsg = 0;..
2900f 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38 36    /* Ticket #186
29010 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20 63  3.  To avoid a c
29011 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74 79  reating security
29012 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 6c   problems for ol
29013 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61  der.  ** applica
29014 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69 6e  tions that relin
29015 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72 20  k against newer 
29016 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
29017 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62 69  te, the.  ** abi
29018 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61 64  lity to run load
29019 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74 75  _extension is tu
2901a 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66 61  rned off by defa
2901b 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d  ult.  One.  ** m
2901c 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ust call sqlite3
2901d 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
2901e 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72 6e  ension() to turn
2901f 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20 20   on extension.  
29020 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74 68  ** loading.  Oth
29021 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20 74  erwise you get t
29022 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72  he following err
29023 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  or..  */.  if( (
29024 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
29025 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
29026 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
29027 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
29028 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
29029 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e  lite3_mprintf("n
2902a 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b  ot authorized");
2902b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
2902c 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2902d 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f 63    }..  if( zProc
2902e 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63  ==0 ){.    zProc
2902f 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65   = "sqlite3_exte
29030 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d  nsion_init";.  }
29031 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71 6c  ..  handle = sql
29032 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56 66  ite3OsDlOpen(pVf
29033 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28  s, zFile);.  if(
29034 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20   handle==0 ){.  
29035 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
29036 7b 0a 20 20 20 20 20 20 7a 45 72 72 6d 73 67 20  {.      zErrmsg 
29037 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c  = sqlite3StackAl
29038 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 4d 73 67  locZero(db, nMsg
29039 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 45 72  );.      if( zEr
2903a 72 6d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20  rmsg ){.        
2903b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
2903c 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67 2c 20  (nMsg, zErrmsg, 
2903d 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 75 6e  .            "un
2903e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73 68 61  able to open sha
2903f 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d  red library [%s]
29040 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20  ", zFile);.     
29041 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72     sqlite3OsDlEr
29042 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d 31  ror(pVfs, nMsg-1
29043 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
29044 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
29045 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
29046 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  0, zErrmsg);.   
29047 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63       sqlite3Stac
29048 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72 6d 73  kFree(db, zErrms
29049 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
2904a 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  }.    return SQL
2904b 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
2904c 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29   xInit = (int(*)
2904d 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
2904e 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
2904f 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20  pi_routines*)). 
29050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29051 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d    sqlite3OsDlSym
29052 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c 20 7a  (pVfs, handle, z
29053 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78 49 6e  Proc);.  if( xIn
29054 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  it==0 ){.    if(
29055 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
29056 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c     zErrmsg = sql
29057 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
29058 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b 0a 20 20  ro(db, nMsg);.  
29059 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73 67 20      if( zErrmsg 
2905a 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2905b 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67  e3_snprintf(nMsg
2905c 2c 20 7a 45 72 72 6d 73 67 2c 0a 20 20 20 20 20  , zErrmsg,.     
2905d 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74 72 79         "no entry
2905e 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20 73   point [%s] in s
2905f 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25  hared library [%
29060 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c 65  s]", zProc,zFile
29061 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29062 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73  e3OsDlError(pVfs
29063 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d 73  , nMsg-1, zErrms
29064 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45  g);.        *pzE
29065 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44  rrMsg = sqlite3D
29066 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72 6d  bStrDup(0, zErrm
29067 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  sg);.        sql
29068 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62  ite3StackFree(db
29069 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  , zErrmsg);.    
2906a 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2906b 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c  3OsDlClose(pVfs,
2906c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d 0a   handle);.    }.
2906d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2906e 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65  E_ERROR;.  }else
2906f 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20 26   if( xInit(db, &
29070 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65  zErrmsg, &sqlite
29071 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 69 66  3Apis) ){.    if
29072 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
29073 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
29074 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
29075 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69 6e  "error during in
29076 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25 73  itialization: %s
29077 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ", zErrmsg);.   
29078 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
29079 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20  ree(zErrmsg);.  
2907a 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f    sqlite3OsDlClo
2907b 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 29  se(pVfs, handle)
2907c 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2907d 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
2907e 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20    /* Append the 
2907f 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62 72 61  new shared libra
29080 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65  ry handle to the
29081 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20   db->aExtension 
29082 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61 6e  array. */.  aHan
29083 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  dle = sqlite3DbM
29084 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
29085 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64 62  zeof(handle)*(db
29086 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29 29  ->nExtension+1))
29087 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65 3d  ;.  if( aHandle=
29088 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
29089 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2908a 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45 78   }.  if( db->nEx
2908b 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20 20  tension>0 ){.   
2908c 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65 2c   memcpy(aHandle,
2908d 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 2c   db->aExtension,
2908e 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a   sizeof(handle)*
2908f 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29 3b  db->nExtension);
29090 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
29091 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45 78  Free(db, db->aEx
29092 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e  tension);.  db->
29093 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48 61  aExtension = aHa
29094 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45 78  ndle;..  db->aEx
29095 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74  tension[db->nExt
29096 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e 64  ension++] = hand
29097 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  le;.  return SQL
29098 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45  ITE_OK;.}.SQLITE
29099 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2909a 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
2909b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2909c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64           /* Load
2909d 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   the extension i
2909e 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73  nto this databas
2909f 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
290a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
290a1 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ile,    /* Name 
290a2 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  of the shared li
290a3 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67  brary containing
290a4 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20   extension */.  
290a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f  const char *zPro
290a6 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70  c,    /* Entry p
290a7 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69  oint.  Use "sqli
290a8 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e  te3_extension_in
290a9 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68  it" if 0 */.  ch
290aa 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
290ab 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
290ac 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
290ad 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 69   not 0 */.){.  i
290ae 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
290af 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
290b0 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20  >mutex);.  rc = 
290b1 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e  sqlite3LoadExten
290b2 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20  sion(db, zFile, 
290b3 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67 29  zProc, pzErrMsg)
290b4 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
290b5 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
290b6 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
290b7 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
290b8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
290b9 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68  }../*.** Call th
290ba 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
290bb 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
290bc 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 69  nection is closi
290bd 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  ng in order.** t
290be 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61 64 65  o clean up loade
290bf 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f 0a  d extensions.*/.
290c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
290c1 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  oid sqlite3Close
290c2 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74  Extensions(sqlit
290c3 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
290c4 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
290c5 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
290c6 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
290c7 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 45  or(i=0; i<db->nE
290c8 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a  xtension; i++){.
290c9 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43      sqlite3OsDlC
290ca 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c 20 64  lose(db->pVfs, d
290cb 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69 5d  b->aExtension[i]
290cc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
290cd 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61  DbFree(db, db->a
290ce 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f  Extension);.}../
290cf 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
290d0 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e  isable extension
290d1 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74 65 6e   loading.  Exten
290d2 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20  sion loading is 
290d3 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a 20 64  disabled by.** d
290d4 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e 6f 74  efault so as not
290d5 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74   to open securit
290d6 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72  y holes in older
290d7 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a   applications..*
290d8 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
290d9 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
290da 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73  load_extension(s
290db 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
290dc 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74 65  onoff){.  sqlite
290dd 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
290de 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
290df 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64 62 2d  onoff ){.    db-
290e0 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
290e1 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a  _LoadExtension;.
290e2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d    }else{.    db-
290e3 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54  >flags &= ~SQLIT
290e4 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b  E_LoadExtension;
290e5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
290e6 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
290e7 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
290e8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65  SQLITE_OK;.}..#e
290e9 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
290ea 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
290eb 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ON */../*.** The
290ec 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f 6e 20   auto-extension 
290ed 63 6f 64 65 20 61 64 64 65 64 20 72 65 67 61 72  code added regar
290ee 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
290ef 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73 69 6f   or not extensio
290f0 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69 73 20  n.** loading is 
290f1 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65 20 6e  supported.  We n
290f2 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71 6c 69  eed a dummy sqli
290f3 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65 72 20  te3Apis pointer 
290f4 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65  for that.** code
290f5 20 69 66 20 72 65 67 75 6c 61 72 20 65 78 74 65   if regular exte
290f6 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
290f7 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20   not available. 
290f8 20 54 68 69 73 20 69 73 20 74 68 61 74 0a 2a 2a   This is that.**
290f9 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72 2e 0a   dummy pointer..
290fa 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
290fb 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
290fc 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f 6e 73  SION.static cons
290fd 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
290fe 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70  utines sqlite3Ap
290ff 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e 64  is = { 0 };.#end
29100 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  if.../*.** The f
29101 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20  ollowing object 
29102 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74 20 6f  holds the list o
29103 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  f automatically 
29104 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65 6e 73  loaded.** extens
29105 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ions..**.** This
29106 20 6c 69 73 74 20 69 73 20 73 68 61 72 65 64 20   list is shared 
29107 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 2e 20  across threads. 
29108 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   The SQLITE_MUTE
29109 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a  X_STATIC_MASTER.
2910a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
2910b 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63 63 65   held while acce
2910c 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73 74 2e  ssing this list.
2910d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2910e 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78  ct sqlite3AutoEx
2910f 74 4c 69 73 74 20 73 71 6c 69 74 65 33 41 75 74  tList sqlite3Aut
29110 6f 45 78 74 4c 69 73 74 3b 0a 73 74 61 74 69 63  oExtList;.static
29111 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
29112 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78  ct sqlite3AutoEx
29113 74 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45  tList {.  int nE
29114 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
29115 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
29116 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b 5d 20  tries in aExt[] 
29117 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20 20 76  */          .  v
29118 6f 69 64 20 28 2a 2a 61 45 78 74 29 28 76 6f 69  oid (**aExt)(voi
29119 64 29 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  d);   /* Pointer
2911a 73 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69  s to the extensi
2911b 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74 69 6f 6e  on init function
2911c 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 41 75  s */.} sqlite3Au
2911d 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20 30 20 7d  toext = { 0, 0 }
2911e 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 41 75  ;../* The "wsdAu
2911f 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77 69 6c  toext" macro wil
29120 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
29121 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e 0a 2a   autoextension.*
29122 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20  * state vector. 
29123 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61   If writable sta
29124 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75  tic data is unsu
29125 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74  pported on the t
29126 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76  arget,.** we hav
29127 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  e to locate the 
29128 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20  state vector at 
29129 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68  run-time.  In th
2912a 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a  e more common.**
2912b 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74   case where writ
2912c 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
2912d 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77   is supported, w
2912e 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 65 72  sdStat can refer
2912f 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20   directly.** to 
29130 74 68 65 20 22 73 71 6c 69 74 65 33 41 75 74 6f  the "sqlite3Auto
29131 65 78 74 22 20 73 74 61 74 65 20 76 65 63 74 6f  ext" state vecto
29132 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65  r declared above
29133 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
29134 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65  TE_OMIT_WSD.# de
29135 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49  fine wsdAutoextI
29136 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74 65 33 41  nit \.  sqlite3A
29137 75 74 6f 45 78 74 4c 69 73 74 20 2a 78 20 3d 20  utoExtList *x = 
29138 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 41  &GLOBAL(sqlite3A
29139 75 74 6f 45 78 74 4c 69 73 74 2c 73 71 6c 69 74  utoExtList,sqlit
2913a 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64 65 66  e3Autoext).# def
2913b 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20 78  ine wsdAutoext x
2913c 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  [0].#else.# defi
2913d 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69  ne wsdAutoextIni
2913e 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41 75  t.# define wsdAu
2913f 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41 75 74  toext sqlite3Aut
29140 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  oext.#endif.../*
29141 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 73  .** Register a s
29142 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64  tatically linked
29143 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61 74 20   extension that 
29144 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
29145 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20 65 76  .** loaded by ev
29146 65 72 79 20 6e 65 77 20 64 61 74 61 62 61 73 65  ery new database
29147 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
29148 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
29149 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65  qlite3_auto_exte
2914a 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 49 6e  nsion(void (*xIn
2914b 69 74 29 28 76 6f 69 64 29 29 7b 0a 20 20 69 6e  it)(void)){.  in
2914c 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2914d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2914e 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
2914f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
29150 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
29151 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
29152 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 0a 23  rn rc;.  }else.#
29153 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 69 6e  endif.  {.    in
29154 74 20 69 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  t i;.#if SQLITE_
29155 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73  THREADSAFE.    s
29156 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
29157 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
29158 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
29159 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
2915a 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ER);.#endif.    
2915b 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a  wsdAutoextInit;.
2915c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2915d 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
2915e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 77      for(i=0; i<w
2915f 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 3b 20  sdAutoext.nExt; 
29160 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
29161 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b  wsdAutoext.aExt[
29162 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62 72 65 61  i]==xInit ) brea
29163 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
29164 20 69 3d 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e   i==wsdAutoext.n
29165 45 78 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Ext ){.      int
29166 20 6e 42 79 74 65 20 3d 20 28 77 73 64 41 75 74   nByte = (wsdAut
29167 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a 73 69 7a  oext.nExt+1)*siz
29168 65 6f 66 28 77 73 64 41 75 74 6f 65 78 74 2e 61  eof(wsdAutoext.a
29169 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 76  Ext[0]);.      v
2916a 6f 69 64 20 28 2a 2a 61 4e 65 77 29 28 76 6f 69  oid (**aNew)(voi
2916b 64 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 20 3d  d);.      aNew =
2916c 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2916d 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
2916e 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
2916f 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( aNew==0 ){. 
29170 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
29171 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
29172 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77  }else{.        w
29173 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20 3d  sdAutoext.aExt =
29174 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 77   aNew;.        w
29175 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 77  sdAutoext.aExt[w
29176 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 5d 20  sdAutoext.nExt] 
29177 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20 20 20  = xInit;.       
29178 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74   wsdAutoext.nExt
29179 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
2917a 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
2917b 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
2917c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72  ;.    assert( (r
2917d 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20  c&0xff)==rc );. 
2917e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2917f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  }.}../*.** Reset
29180 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 65   the automatic e
29181 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
29182 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53   mechanism..*/.S
29183 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
29184 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
29185 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
29186 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
29187 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
29188 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
29189 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51 4c 49  itialize()==SQLI
2918a 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 20  TE_OK ).#endif. 
2918b 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48   {.#if SQLITE_TH
2918c 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c  READSAFE.    sql
2918d 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
2918e 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
2918f 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
29190 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
29191 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77 73  );.#endif.    ws
29192 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20  dAutoextInit;.  
29193 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
29194 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
29195 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 77    sqlite3_free(w
29196 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 29 3b  sdAutoext.aExt);
29197 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e  .    wsdAutoext.
29198 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20 77 73  aExt = 0;.    ws
29199 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20  dAutoext.nExt = 
2919a 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  0;.    sqlite3_m
2919b 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
2919c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2919d 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d 61 74  Load all automat
2919e 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  ic extensions..*
2919f 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67  *.** If anything
291a0 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73 65 74   goes wrong, set
291a1 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65   an error in the
291a2 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
291a3 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
291a4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
291a5 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65  ite3AutoLoadExte
291a6 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  nsions(sqlite3 *
291a7 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
291a8 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e  int go = 1;.  in
291a9 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74  t (*xInit)(sqlit
291aa 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74  e3*,char**,const
291ab 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
291ac 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64 41  tines*);..  wsdA
291ad 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69 66  utoextInit;.  if
291ae 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  ( wsdAutoext.nEx
291af 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43  t==0 ){.    /* C
291b0 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c  ommon case: earl
291b1 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65 76  y out without ev
291b2 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61 63  ery having to ac
291b3 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a 2f  quire a mutex */
291b4 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
291b5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67 6f 3b 20  .  for(i=0; go; 
291b6 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  i++){.    char *
291b7 7a 45 72 72 6d 73 67 3b 0a 23 69 66 20 53 51 4c  zErrmsg;.#if SQL
291b8 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
291b9 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
291ba 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65   *mutex = sqlite
291bb 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
291bc 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
291bd 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a  MASTER);.#endif.
291be 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
291bf 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
291c0 20 20 20 20 69 66 28 20 69 3e 3d 77 73 64 41 75      if( i>=wsdAu
291c1 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20  toext.nExt ){.  
291c2 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20      xInit = 0;. 
291c3 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20       go = 0;.   
291c4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 78 49   }else{.      xI
291c5 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71  nit = (int(*)(sq
291c6 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
291c7 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
291c8 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20  routines*)).    
291c9 20 20 20 20 20 20 20 20 20 20 77 73 64 41 75 74            wsdAut
291ca 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20  oext.aExt[i];.  
291cb 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
291cc 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
291cd 78 29 3b 0a 20 20 20 20 7a 45 72 72 6d 73 67 20  x);.    zErrmsg 
291ce 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 78 49 6e  = 0;.    if( xIn
291cf 69 74 20 26 26 20 78 49 6e 69 74 28 64 62 2c 20  it && xInit(db, 
291d0 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
291d1 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 20  e3Apis) ){.     
291d2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
291d3 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a  , SQLITE_ERROR,.
291d4 20 20 20 20 20 20 20 20 20 20 20 20 22 61 75 74              "aut
291d5 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e  omatic extension
291d6 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65 64 3a   loading failed:
291d7 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a   %s", zErrmsg);.
291d8 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20        go = 0;.  
291d9 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
291da 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20  free(zErrmsg);. 
291db 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
291dc 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61  ***** End of loa
291dd 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
291de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
291e1 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
291e2 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a   pragma.c ******
291e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291e5 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
291e6 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
291e7 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
291e8 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
291e9 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
291ea 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
291eb 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
291ec 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
291ed 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
291ee 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
291ef 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
291f0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
291f1 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
291f2 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
291f3 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
291f4 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
291f5 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
291f6 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
291f7 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
291f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
291fc 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
291fd 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
291fe 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
291ff 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20  ment the PRAGMA 
29200 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 0a 2f 2a 20  command..*/../* 
29201 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68 6f 6c  Ignore this whol
29202 65 20 66 69 6c 65 20 69 66 20 70 72 61 67 6d 61  e file if pragma
29203 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 0a 2a  s are disabled.*
29204 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
29205 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d  QLITE_OMIT_PRAGM
29206 41 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70  A)../*.** Interp
29207 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74  ret the given st
29208 72 69 6e 67 20 61 73 20 61 20 73 61 66 65 74 79  ring as a safety
29209 20 6c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 20   level.  Return 
2920a 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31 20  0 for OFF,.** 1 
2920b 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c  for ON or NORMAL
2920c 20 61 6e 64 20 32 20 66 6f 72 20 46 55 4c 4c 2e   and 2 for FULL.
2920d 20 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20 61    Return 1 for a
2920e 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a 20 75  n empty or .** u
2920f 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73 74 72 69  nrecognized stri
29210 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ng argument..**.
29211 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
29212 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
29213 20 61 72 65 20 6f 6e 65 20 6c 65 73 73 20 74 68   are one less th
29214 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 68  at the values th
29215 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  at.** should be 
29216 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c 69  passed into sqli
29217 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
29218 79 4c 65 76 65 6c 28 29 2e 20 20 54 68 65 20 69  yLevel().  The i
29219 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70  s done.** to sup
2921a 70 6f 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20  port legacy SQL 
2921b 63 6f 64 65 2e 20 20 54 68 65 20 73 61 66 65 74  code.  The safet
2921c 79 20 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20  y level used to 
2921d 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e  be boolean.** an
2921e 64 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20  d older scripts 
2921f 6d 61 79 20 68 61 76 65 20 75 73 65 64 20 6e 75  may have used nu
29220 6d 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20  mbers 0 for OFF 
29221 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f  and 1 for ON..*/
29222 0a 73 74 61 74 69 63 20 75 38 20 67 65 74 53 61  .static u8 getSa
29223 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20  fetyLevel(const 
29224 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20 20 20  char *z){.      
29225 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29226 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34 35 36         /* 123456
29227 37 38 39 20 31 32 33 34 35 36 37 38 39 20 2a 2f  789 123456789 */
29228 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
29229 63 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d 20 22  char zText[] = "
2922a 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74 72 75 65  onoffalseyestrue
2922b 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69 63 20  full";.  static 
2922c 63 6f 6e 73 74 20 75 38 20 69 4f 66 66 73 65 74  const u8 iOffset
2922d 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c 20 34  [] = {0, 1, 2, 4
2922e 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a 20 20  , 9, 12, 16};.  
2922f 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
29230 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c 20  iLength[] = {2, 
29231 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c 20 34  2, 3, 5, 3, 4, 4
29232 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
29233 74 20 75 38 20 69 56 61 6c 75 65 5b 5d 20 3d 20  t u8 iValue[] = 
29234 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c   {1, 0, 0, 0, 1,
29235 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20 69 2c   1, 2};.  int i,
29236 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   n;.  if( sqlite
29237 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a  3Isdigit(*z) ){.
29238 20 20 20 20 72 65 74 75 72 6e 20 28 75 38 29 61      return (u8)a
29239 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20  toi(z);.  }.  n 
2923a 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2923b 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  0(z);.  for(i=0;
2923c 20 69 3c 41 72 72 61 79 53 69 7a 65 28 69 4c 65   i<ArraySize(iLe
2923d 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  ngth); i++){.   
2923e 20 69 66 28 20 69 4c 65 6e 67 74 68 5b 69 5d 3d   if( iLength[i]=
2923f 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =n && sqlite3Str
29240 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 69 4f 66  NICmp(&zText[iOf
29241 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30  fset[i]],z,n)==0
29242 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
29243 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20 20 20   iValue[i];.    
29244 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
29245 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ;.}../*.** Inter
29246 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
29247 74 72 69 6e 67 20 61 73 20 61 20 62 6f 6f 6c 65  tring as a boole
29248 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  an value..*/.sta
29249 74 69 63 20 75 38 20 67 65 74 42 6f 6f 6c 65 61  tic u8 getBoolea
2924a 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  n(const char *z)
2924b 7b 0a 20 20 72 65 74 75 72 6e 20 67 65 74 53 61  {.  return getSa
2924c 66 65 74 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0a  fetyLevel(z)&1;.
2924d 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  }../*.** Interpr
2924e 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  et the given str
2924f 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b 69 6e 67  ing as a locking
29250 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a   mode value..*/.
29251 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 6f  static int getLo
29252 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e 73 74 20  ckingMode(const 
29253 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20  char *z){.  if( 
29254 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d  z ){.    if( 0==
29255 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29256 2c 20 22 65 78 63 6c 75 73 69 76 65 22 29 20 29  , "exclusive") )
29257 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
29258 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
29259 49 56 45 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d  IVE;.    if( 0==
2925a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2925b 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29 20 72 65  , "normal") ) re
2925c 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49  turn PAGER_LOCKI
2925d 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20  NGMODE_NORMAL;. 
2925e 20 7d 0a 20 20 72 65 74 75 72 6e 20 50 41 47 45   }.  return PAGE
2925f 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
29260 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ERY;.}..#ifndef 
29261 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
29262 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74  VACUUM./*.** Int
29263 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
29264 20 73 74 72 69 6e 67 20 61 73 20 61 6e 20 61 75   string as an au
29265 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 76  to-vacuum mode v
29266 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
29267 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67  following string
29268 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c  s, "none", "full
29269 22 20 61 6e 64 20 22 69 6e 63 72 65 6d 65 6e 74  " and "increment
2926a 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61 63 63 65  al" are .** acce
2926b 70 74 61 62 6c 65 2c 20 61 73 20 61 72 65 20 74  ptable, as are t
2926c 68 65 69 72 20 6e 75 6d 65 72 69 63 20 65 71 75  heir numeric equ
2926d 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20 31 20 61  ivalents: 0, 1 a
2926e 6e 64 20 32 20 72 65 73 70 65 63 74 69 76 65 6c  nd 2 respectivel
2926f 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
29270 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 63   getAutoVacuum(c
29271 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
29272 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 30 3d   int i;.  if( 0=
29273 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
29274 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20 72 65 74  z, "none") ) ret
29275 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
29276 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28  CUUM_NONE;.  if(
29277 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
29278 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29 20 29 20  mp(z, "full") ) 
29279 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
2927a 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20  OVACUUM_FULL;.  
2927b 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
2927c 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63 72 65 6d  rICmp(z, "increm
2927d 65 6e 74 61 6c 22 29 20 29 20 72 65 74 75 72 6e  ental") ) return
2927e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
2927f 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d 20 61 74  M_INCR;.  i = at
29280 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20  oi(z);.  return 
29281 28 75 38 29 28 28 69 3e 3d 30 26 26 69 3c 3d 32  (u8)((i>=0&&i<=2
29282 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66  )?i:0);.}.#endif
29283 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54   /* ifndef SQLIT
29284 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
29285 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  M */..#ifndef SQ
29286 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
29287 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e  PRAGMAS./*.** In
29288 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
29289 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 74 65  n string as a te
2928a 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20  mp db location. 
2928b 52 65 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c  Return 1 for fil
2928c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70  e.** backed temp
2928d 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c  orary databases,
2928e 20 32 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42   2 for the Red-B
2928f 6c 61 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d  lack tree in mem
29290 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ory database.** 
29291 61 6e 64 20 30 20 74 6f 20 75 73 65 20 74 68 65  and 0 to use the
29292 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65   compile-time de
29293 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  fault..*/.static
29294 20 69 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72   int getTempStor
29295 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  e(const char *z)
29296 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30  {.  if( z[0]>='0
29297 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29  ' && z[0]<='2' )
29298 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30  {.    return z[0
29299 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65  ] - '0';.  }else
2929a 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2929b 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d  Cmp(z, "file")==
2929c 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2929d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  1;.  }else if( s
2929e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
2929f 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b   "memory")==0 ){
292a0 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
292a1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
292a2 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rn 0;.  }.}.#end
292a3 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47  if /* SQLITE_PAG
292a4 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  ER_PRAGMAS */..#
292a5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
292a6 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
292a7 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  ./*.** Invalidat
292a8 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20  e temp storage, 
292a9 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20  either when the 
292aa 74 65 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20  temp storage is 
292ab 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20  changed.** from 
292ac 64 65 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e  default, or when
292ad 20 27 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20   'file' and the 
292ae 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
292af 74 6f 72 79 20 68 61 73 20 63 68 61 6e 67 65 64  tory has changed
292b0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
292b1 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f  nvalidateTempSto
292b2 72 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  rage(Parse *pPar
292b3 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  se){.  sqlite3 *
292b4 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
292b5 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31  .  if( db->aDb[1
292b6 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20  ].pBt!=0 ){.    
292b7 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
292b8 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  mit || sqlite3Bt
292b9 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
292ba 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29  (db->aDb[1].pBt)
292bb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
292bc 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
292bd 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 73 74 6f  , "temporary sto
292be 72 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 63  rage cannot be c
292bf 68 61 6e 67 65 64 20 22 0a 20 20 20 20 20 20 20  hanged ".       
292c0 20 22 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20   "from within a 
292c1 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20  transaction");. 
292c2 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
292c3 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
292c4 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
292c5 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d  Close(db->aDb[1]
292c6 2e 70 42 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61  .pBt);.    db->a
292c7 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20  Db[1].pBt = 0;. 
292c8 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
292c9 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
292ca 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
292cb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
292cc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
292cd 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a  _PAGER_PRAGMAS *
292ce 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
292cf 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
292d0 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  GMAS./*.** If th
292d1 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
292d2 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73 65 20 69  is open, close i
292d3 74 20 61 6e 64 20 6d 61 72 6b 20 74 68 65 20 64  t and mark the d
292d4 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a  atabase schema.*
292d5 2a 20 61 73 20 6e 65 65 64 69 6e 67 20 72 65 6c  * as needing rel
292d6 6f 61 64 69 6e 67 2e 20 20 54 68 69 73 20 6d 75  oading.  This mu
292d7 73 74 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20  st be done when 
292d8 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45  using the SQLITE
292d9 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f  _TEMP_STORE.** o
292da 72 20 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53  r DEFAULT_TEMP_S
292db 54 4f 52 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f  TORE pragmas..*/
292dc 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 61 6e  .static int chan
292dd 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61  geTempStorage(Pa
292de 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
292df 73 74 20 63 68 61 72 20 2a 7a 53 74 6f 72 61 67  st char *zStorag
292e0 65 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 73  eType){.  int ts
292e1 20 3d 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28   = getTempStore(
292e2 7a 53 74 6f 72 61 67 65 54 79 70 65 29 3b 0a 20  zStorageType);. 
292e3 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
292e4 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
292e5 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d   db->temp_store=
292e6 3d 74 73 20 29 20 72 65 74 75 72 6e 20 53 51 4c  =ts ) return SQL
292e7 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e  ITE_OK;.  if( in
292e8 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
292e9 61 67 65 28 20 70 50 61 72 73 65 20 29 20 21 3d  age( pParse ) !=
292ea 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20   SQLITE_OK ){.  
292eb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
292ec 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d  ERROR;.  }.  db-
292ed 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 28 75  >temp_store = (u
292ee 38 29 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  8)ts;.  return S
292ef 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
292f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47  if /* SQLITE_PAG
292f1 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f  ER_PRAGMAS */../
292f2 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
292f3 64 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 73  de to return a s
292f4 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 76 61  ingle integer va
292f5 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
292f6 6f 69 64 20 72 65 74 75 72 6e 53 69 6e 67 6c 65  oid returnSingle
292f7 49 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  Int(Parse *pPars
292f8 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
292f9 4c 61 62 65 6c 2c 20 69 36 34 20 76 61 6c 75 65  Label, i64 value
292fa 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
292fb 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
292fc 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 6d 65 6d  arse);.  int mem
292fd 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
292fe 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36 34 20 3d  m;.  i64 *pI64 =
292ff 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
29300 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Raw(pParse->db, 
29301 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29 3b 0a  sizeof(value));.
29302 20 20 69 66 28 20 70 49 36 34 20 29 7b 0a 20 20    if( pI64 ){.  
29303 20 20 6d 65 6d 63 70 79 28 70 49 36 34 2c 20 26    memcpy(pI64, &
29304 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28 76 61  value, sizeof(va
29305 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  lue));.  }.  sql
29306 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29307 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 6d  , OP_Int64, 0, m
29308 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a 29 70 49  em, 0, (char*)pI
29309 36 34 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20  64, P4_INT64);. 
2930a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2930b 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
2930c 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2930d 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2930e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c  AME_NAME, zLabel
2930f 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29310 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
29311 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29312 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b 0a  ltRow, mem, 1);.
29313 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
29314 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
29315 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  MAS./*.** Check 
29316 74 6f 20 73 65 65 20 69 66 20 7a 52 69 67 68 74  to see if zRight
29317 20 61 6e 64 20 7a 4c 65 66 74 20 72 65 66 65 72   and zLeft refer
29318 20 74 6f 20 61 20 70 72 61 67 6d 61 20 74 68 61   to a pragma tha
29319 74 20 71 75 65 72 69 65 73 0a 2a 2a 20 6f 72 20  t queries.** or 
2931a 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f 66 20 74  changes one of t
2931b 68 65 20 66 6c 61 67 73 20 69 6e 20 64 62 2d 3e  he flags in db->
2931c 66 6c 61 67 73 2e 20 20 52 65 74 75 72 6e 20 31  flags.  Return 1
2931d 20 69 66 20 73 6f 20 61 6e 64 20 30 20 69 66 20   if so and 0 if 
2931e 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c 20 69 6d  not..** Also, im
2931f 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 61 67  plement the prag
29320 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ma..*/.static in
29321 74 20 66 6c 61 67 50 72 61 67 6d 61 28 50 61 72  t flagPragma(Par
29322 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
29323 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63  t char *zLeft, c
29324 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68  onst char *zRigh
29325 74 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  t){.  static con
29326 73 74 20 73 74 72 75 63 74 20 73 50 72 61 67 6d  st struct sPragm
29327 61 54 79 70 65 20 7b 0a 20 20 20 20 63 6f 6e 73  aType {.    cons
29328 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
29329 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70  /* Name of the p
2932a 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20 69 6e 74  ragma */.    int
2932b 20 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20   mask;          
2932c 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 74 68 65   /* Mask for the
2932d 20 64 62 2d 3e 66 6c 61 67 73 20 76 61 6c 75 65   db->flags value
2932e 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67 6d 61 5b   */.  } aPragma[
2932f 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 66 75 6c  ] = {.    { "ful
29330 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c  l_column_names",
29331 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
29332 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0a  ullColNames  },.
29333 20 20 20 20 7b 20 22 73 68 6f 72 74 5f 63 6f 6c      { "short_col
29334 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20  umn_names",     
29335 20 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f    SQLITE_ShortCo
29336 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20 20 7b 20  lNames },.    { 
29337 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 22 2c  "count_changes",
29338 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
29339 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20  TE_CountRows    
2933a 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d 70 74 79   },.    { "empty
2933b 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62 61 63 6b  _result_callback
2933c 73 22 2c 20 20 20 53 51 4c 49 54 45 5f 4e 75 6c  s",   SQLITE_Nul
2933d 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c 0a 20 20  lCallback  },.  
2933e 20 20 7b 20 22 6c 65 67 61 63 79 5f 66 69 6c 65    { "legacy_file
2933f 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20 20 20 20  _format",       
29340 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
29341 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b 20 22 66  eFmt },.    { "f
29342 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20 20 20 20  ullfsync",      
29343 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
29344 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 7d  _FullFSync     }
29345 2c 0a 20 20 20 20 7b 20 22 72 65 76 65 72 73 65  ,.    { "reverse
29346 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65 6c 65 63  _unordered_selec
29347 74 73 22 2c 20 53 51 4c 49 54 45 5f 52 65 76 65  ts", SQLITE_Reve
29348 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a 23 69 66  rseOrder  },.#if
29349 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
2934a 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74 72 61 63  .    { "sql_trac
2934b 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
2934c 20 20 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61     SQLITE_SqlTra
2934d 63 65 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b  ce      },.    {
2934e 20 22 76 64 62 65 5f 6c 69 73 74 69 6e 67 22 2c   "vdbe_listing",
2934f 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
29350 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20  ITE_VdbeListing 
29351 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65    },.    { "vdbe
29352 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20 20 20  _trace",        
29353 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64         SQLITE_Vd
29354 62 65 54 72 61 63 65 20 20 20 20 20 7d 2c 0a 23  beTrace     },.#
29355 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
29356 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
29357 20 20 20 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68      { "ignore_ch
29358 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22  eck_constraints"
29359 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  , SQLITE_IgnoreC
2935a 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e 64 69 66  hecks  },.#endif
2935b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
2935c 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20 65 78  owing is VERY ex
2935d 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20 20  perimental */.  
2935e 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f 73 63    { "writable_sc
2935f 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20 20 20  hema",          
29360 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
29361 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f 76 65  ma|SQLITE_Recove
29362 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20 20 7b 20  ryMode },.    { 
29363 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b 22 2c  "omit_readlock",
29364 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
29365 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20  TE_NoReadlock   
29366 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f   },..    /* TODO
29367 3a 20 4d 61 79 62 65 20 69 74 20 73 68 6f 75 6c  : Maybe it shoul
29368 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c 65  dn't be possible
29369 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 52   to change the R
2936a 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 0a 20  eadUncommitted. 
2936b 20 20 20 2a 2a 20 66 6c 61 67 20 69 66 20 74 68     ** flag if th
2936c 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
2936d 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a  ve statements. *
2936e 2f 0a 20 20 20 20 7b 20 22 72 65 61 64 5f 75 6e  /.    { "read_un
2936f 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20 20 20 20  committed",     
29370 20 20 20 20 53 51 4c 49 54 45 5f 52 65 61 64 55      SQLITE_ReadU
29371 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c 0a 20 20  ncommitted },.  
29372 20 20 7b 20 22 72 65 63 75 72 73 69 76 65 5f 74    { "recursive_t
29373 72 69 67 67 65 72 73 22 2c 20 20 20 20 20 20 20  riggers",       
29374 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65  SQLITE_RecTrigge
29375 72 73 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 68  rs },..    /* Th
29376 69 73 20 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79  is flag may only
29377 20 62 65 20 73 65 74 20 69 66 20 62 6f 74 68 20   be set if both 
29378 66 6f 72 65 69 67 6e 2d 6b 65 79 20 61 6e 64 20  foreign-key and 
29379 74 72 69 67 67 65 72 20 73 75 70 70 6f 72 74 0a  trigger support.
2937a 20 20 20 20 2a 2a 20 61 72 65 20 70 72 65 73 65      ** are prese
2937b 6e 74 20 69 6e 20 74 68 65 20 62 75 69 6c 64 2e  nt in the build.
2937c 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
2937d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
2937e 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64  REIGN_KEY) && !d
2937f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
29380 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
29381 7b 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22  { "foreign_keys"
29382 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ,             SQ
29383 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
29384 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a   },.#endif.  };.
29385 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
29386 20 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54   struct sPragmaT
29387 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d  ype *p;.  for(i=
29388 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b 20 69 3c  0, p=aPragma; i<
29389 41 72 72 61 79 53 69 7a 65 28 61 50 72 61 67 6d  ArraySize(aPragm
2938a 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  a); i++, p++){. 
2938b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
2938c 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 70 2d 3e  rICmp(zLeft, p->
2938d 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
2938e 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
2938f 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20   pParse->db;.   
29390 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
29391 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
29392 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
29393 20 20 20 20 61 73 73 65 72 74 28 20 76 21 3d 30      assert( v!=0
29394 20 29 3b 20 20 2f 2a 20 41 6c 72 65 61 64 79 20   );  /* Already 
29395 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
29396 69 74 65 33 50 72 61 67 6d 61 28 29 20 2a 2f 0a  ite3Pragma() */.
29397 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
29398 28 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  (v) ){.        i
29399 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( zRight==0 ){.
2939a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
2939b 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
2939c 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d  , p->zName, (db-
2939d 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d 61 73 6b  >flags & p->mask
2939e 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )!=0 );.        
2939f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
293a0 20 69 6e 74 20 6d 61 73 6b 20 3d 20 70 2d 3e 6d   int mask = p->m
293a1 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ask;          /*
293a2 20 4d 61 73 6b 20 6f 66 20 62 69 74 73 20 74 6f   Mask of bits to
293a3 20 73 65 74 20 6f 72 20 63 6c 65 61 72 2e 20 2a   set or clear. *
293a4 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
293a5 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
293a6 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
293a7 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20   /* Foreign key 
293a8 73 75 70 70 6f 72 74 20 6d 61 79 20 6e 6f 74 20  support may not 
293a9 62 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69  be enabled or di
293aa 73 61 62 6c 65 64 20 77 68 69 6c 65 20 6e 6f 74  sabled while not
293ab 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
293ac 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d  in auto-commit m
293ad 6f 64 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ode.  */.       
293ae 20 20 20 20 20 6d 61 73 6b 20 26 3d 20 7e 28 53       mask &= ~(S
293af 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
293b0 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  s);.          }.
293b1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 67  .          if( g
293b2 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
293b3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
293b4 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 6d 61   db->flags |= ma
293b5 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  sk;.          }e
293b6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
293b7 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 6d   db->flags &= ~m
293b8 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ask;.          }
293b9 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ..          /* M
293ba 61 6e 79 20 6f 66 20 74 68 65 20 66 6c 61 67 2d  any of the flag-
293bb 70 72 61 67 6d 61 73 20 6d 6f 64 69 66 79 20 74  pragmas modify t
293bc 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  he code generate
293bd 64 20 62 79 20 74 68 65 20 53 51 4c 20 0a 20 20  d by the SQL .  
293be 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69          ** compi
293bf 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63  ler (eg. count_c
293c0 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64 64 20  hanges). So add 
293c1 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65 78 70  an opcode to exp
293c2 69 72 65 20 61 6c 6c 0a 20 20 20 20 20 20 20 20  ire all.        
293c3 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53 51    ** compiled SQ
293c4 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 66 74  L statements aft
293c5 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20 70  er modifying a p
293c6 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20 20 20  ragma value..   
293c7 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
293c8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
293c9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69  ddOp2(v, OP_Expi
293ca 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  re, 0, 0);.     
293cb 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
293cc 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
293cd 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
293ce 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
293cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
293d0 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a  _PRAGMAS */../*.
293d1 2a 2a 20 52 65 74 75 72 6e 20 61 20 68 75 6d 61  ** Return a huma
293d2 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d 65 20  n-readable name 
293d3 66 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e 74  for a constraint
293d4 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63 74 69   resolution acti
293d5 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
293d6 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
293d7 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63 20 63 6f  GN_KEY.static co
293d8 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69 6f 6e  nst char *action
293d9 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e 29 7b  Name(u8 action){
293da 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
293db 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63 68 28 20  Name;.  switch( 
293dc 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 63 61  action ){.    ca
293dd 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20 20  se OE_SetNull:  
293de 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e 55 4c  zName = "SET NUL
293df 4c 22 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b  L";        break
293e0 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65  ;.    case OE_Se
293e1 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65 20 3d 20  tDflt:  zName = 
293e2 22 53 45 54 20 44 45 46 41 55 4c 54 22 3b 20 20  "SET DEFAULT";  
293e3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
293e4 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a 20 20  se OE_Cascade:  
293e5 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43 41 44 45  zName = "CASCADE
293e6 22 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  ";         break
293e7 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65  ;.    case OE_Re
293e8 73 74 72 69 63 74 3a 20 7a 4e 61 6d 65 20 3d 20  strict: zName = 
293e9 22 52 45 53 54 52 49 43 54 22 3b 20 20 20 20 20  "RESTRICT";     
293ea 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
293eb 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
293ec 7a 4e 61 6d 65 20 3d 20 22 4e 4f 20 41 43 54 49  zName = "NO ACTI
293ed 4f 4e 22 3b 20 20 0a 20 20 20 20 20 20 20 20 20  ON";  .         
293ee 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73               ass
293ef 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f  ert( action==OE_
293f0 4e 6f 6e 65 20 29 3b 20 62 72 65 61 6b 3b 0a 20  None ); break;. 
293f1 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d   }.  return zNam
293f2 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
293f3 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72 61  ** Process a pra
293f4 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  gma statement.  
293f5 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61  .**.** Pragmas a
293f6 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a  re of this form:
293f7 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47  .**.**      PRAG
293f8 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  MA [database.]id
293f9 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a   [= value].**.**
293fa 20 54 68 65 20 69 64 65 6e 74 69 66 69 65 72 20   The identifier 
293fb 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20  might also be a 
293fc 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c  string.  The val
293fd 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20  ue is a string, 
293fe 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  and.** identifie
293ff 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20  r, or a number. 
29400 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73   If minusFlag is
29401 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
29402 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75  value is.** a nu
29403 6d 62 65 72 20 74 68 61 74 20 77 61 73 20 70 72  mber that was pr
29404 65 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75  eceded by a minu
29405 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  s sign..**.** If
29406 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
29407 73 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20  s "database.id" 
29408 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
29409 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
2940a 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20 74 68  * and pId2 is th
2940b 65 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65  e id.  If the le
2940c 66 74 20 73 69 64 65 20 69 73 20 6a 75 73 74 20  ft side is just 
2940d 22 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69  "id" then pId1 i
2940e 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20  s the.** id and 
2940f 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74  pId2 is any empt
29410 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  y string..*/.SQL
29411 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
29412 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a   sqlite3Pragma(.
29413 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
29414 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c   .  Token *pId1,
29415 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
29416 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61   part of [databa
29417 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a  se.]id field */.
29418 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20    Token *pId2,  
29419 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
2941a 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73  part of [databas
2941b 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20  e.]id field, or 
2941c 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
2941d 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a  *pValue,      /*
2941e 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75   Token for <valu
2941f 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  e>, or NULL */. 
29420 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20   int minusFlag  
29421 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
29422 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65  a '-' sign prece
29423 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29  ded <value> */.)
29424 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20  {.  char *zLeft 
29425 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  = 0;       /* Nu
29426 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
29427 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a  -8 string <id> *
29428 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74  /.  char *zRight
29429 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
2942a 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
2942b 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65  -8 string <value
2942c 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  >, or NULL */.  
2942d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
2942e 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61  = 0;   /* The da
2942f 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
29430 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20   Token *pId;    
29431 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
29432 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e  er to <id> token
29433 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
29434 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29435 44 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66  Database index f
29436 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f  or <database> */
29437 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29438 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
29439 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a  b *pDb;.  Vdbe *
2943a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2943b 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
2943c 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69 66 28  reate(db);.  if(
2943d 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
2943e 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
2943f 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70   2;..  /* Interp
29440 72 65 74 20 74 68 65 20 5b 64 61 74 61 62 61 73  ret the [databas
29441 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65 20  e.] part of the 
29442 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74  pragma statement
29443 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20 20 2a  . iDb is the.  *
29444 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * index of the d
29445 61 74 61 62 61 73 65 20 74 68 69 73 20 70 72 61  atabase this pra
29446 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70 70  gma is being app
29447 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61 44  lied to in db.aD
29448 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20  b[]. */.  iDb = 
29449 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
2944a 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64 31 2c  me(pParse, pId1,
2944b 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20 20   pId2, &pId);.  
2944c 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75  if( iDb<0 ) retu
2944d 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d  rn;.  pDb = &db-
2944e 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a  >aDb[iDb];..  /*
2944f 20 49 66 20 74 68 65 20 74 65 6d 70 20 64 61 74   If the temp dat
29450 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 65  abase has been e
29451 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64 20  xplicitly named 
29452 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 0a  as part of the .
29453 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b    ** pragma, mak
29454 65 20 73 75 72 65 20 69 74 20 69 73 20 6f 70 65  e sure it is ope
29455 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  n. .  */.  if( i
29456 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33  Db==1 && sqlite3
29457 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
29458 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
29459 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a  return;.  }..  z
2945a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e 61  Left = sqlite3Na
2945b 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2945c 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65  pId);.  if( !zLe
2945d 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ft ) return;.  i
2945e 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a  f( minusFlag ){.
2945f 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c      zRight = sql
29460 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
29461 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a  "-%T", pValue);.
29462 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69    }else{.    zRi
29463 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ght = sqlite3Nam
29464 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
29465 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 61  Value);.  }..  a
29466 73 73 65 72 74 28 20 70 49 64 32 20 29 3b 0a 20  ssert( pId2 );. 
29467 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e 3e 30   zDb = pId2->n>0
29468 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a 20   ? pDb->zName : 
29469 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  0;.  if( sqlite3
2946a 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2946b 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 2c  , SQLITE_PRAGMA,
2946c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c 20   zLeft, zRight, 
2946d 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  zDb) ){.    goto
2946e 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 7d   pragma_out;.  }
2946f 0a 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  . .#ifndef SQLIT
29470 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
29471 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
29472 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29473 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f  .]default_cache_
29474 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  size.  **  PRAGM
29475 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66  A [database.]def
29476 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 3d  ault_cache_size=
29477 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
29478 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
29479 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  ts the current p
2947a 65 72 73 69 73 74 65 6e 74 20 73 65 74 74 69 6e  ersistent settin
2947b 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70  g for the.  ** p
2947c 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20  age cache size. 
2947d 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
2947e 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d  ned is the maxim
2947f 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a  um number of.  *
29480 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70  * pages in the p
29481 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65 20  age cache.  The 
29482 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73  second form sets
29483 20 62 6f 74 68 20 74 68 65 20 63 75 72 72 65 6e   both the curren
29484 74 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  t.  ** page cach
29485 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61 6e 64  e size value and
29486 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20   the persistent 
29487 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 20  page cache size 
29488 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f 72 65  value.  ** store
29489 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
2948a 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a  e file..  **.  *
2948b 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 61  * The default ca
2948c 63 68 65 20 73 69 7a 65 20 69 73 20 73 74 6f 72  che size is stor
2948d 65 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c 75 65  ed in meta-value
2948e 20 32 20 6f 66 20 70 61 67 65 20 31 20 6f 66 20   2 of page 1 of 
2948f 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
29490 65 20 66 69 6c 65 2e 20 20 54 68 65 20 63 61 63  e file.  The cac
29491 68 65 20 73 69 7a 65 20 69 73 20 61 63 74 75 61  he size is actua
29492 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c 75 74 65  lly the absolute
29493 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 74   value of.  ** t
29494 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  his memory locat
29495 69 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e 20 6f  ion.  The sign o
29496 66 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 64  f meta-value 2 d
29497 65 74 65 72 6d 69 6e 65 73 20 74 68 65 0a 20 20  etermines the.  
29498 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73  ** synchronous s
29499 65 74 74 69 6e 67 2e 20 20 41 20 6e 65 67 61 74  etting.  A negat
2949a 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20  ive value means 
2949b 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f  synchronous is o
2949c 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 6f  ff.  ** and a po
2949d 73 69 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61  sitive value mea
2949e 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ns synchronous i
2949f 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  s on..  */.  if(
294a0 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
294a1 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c 74 5f 63  zLeft,"default_c
294a2 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  ache_size")==0 )
294a3 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
294a4 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 67 65  st VdbeOpList ge
294a5 74 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d 20 7b  tCacheSize[] = {
294a6 0a 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e  .      { OP_Tran
294a7 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 20  saction, 0, 0,  
294a8 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20 20        0},       
294a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294aa 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
294ab 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  { OP_ReadCookie,
294ac 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 42    0, 1,        B
294ad 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
294ae 48 45 5f 53 49 5a 45 7d 2c 20 20 2f 2a 20 31 20  HE_SIZE},  /* 1 
294af 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66  */.      { OP_If
294b0 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 37 2c  Pos,       1, 7,
294b1 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
294b2 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
294b3 20 20 20 20 30 2c 20 32 2c 20 20 20 20 20 20 20      0, 2,       
294b4 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   0},.      { OP_
294b5 53 75 62 74 72 61 63 74 2c 20 20 20 20 31 2c 20  Subtract,    1, 
294b6 32 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a 20 20  2,        1},.  
294b7 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20      { OP_IfPos, 
294b8 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20        1, 7,     
294b9 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
294ba 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30  P_Integer,     0
294bb 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 20  , 1,        0}, 
294bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294bd 20 20 20 20 20 20 20 20 2f 2a 20 36 20 2a 2f 0a          /* 6 */.
294be 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
294bf 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20 20  tRow,   1, 1,   
294c0 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a       0},.    };.
294c1 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
294c2 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
294c3 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
294c4 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
294c5 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
294c6 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
294c7 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  Db);.    if( !zR
294c8 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ight ){.      sq
294c9 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
294ca 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
294cb 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
294cc 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
294cd 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68  NAME_NAME, "cach
294ce 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f  e_size", SQLITE_
294cf 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70  STATIC);.      p
294d0 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32  Parse->nMem += 2
294d1 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
294d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
294d3 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
294d4 28 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20  (getCacheSize), 
294d5 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20  getCacheSize);. 
294d6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
294d7 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
294d8 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
294d9 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
294da 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69 44 62  1(v, addr+1, iDb
294db 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
294dc 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
294dd 61 64 64 72 2b 36 2c 20 53 51 4c 49 54 45 5f 44  addr+6, SQLITE_D
294de 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
294df 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  E);.    }else{. 
294e0 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
294e1 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
294e2 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
294e3 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
294e4 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
294e5 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
294e6 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
294e7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
294e8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
294e9 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20 31 29  nteger, size, 1)
294ea 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
294eb 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
294ec 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  ReadCookie, iDb,
294ed 20 32 2c 20 42 54 52 45 45 5f 44 45 46 41 55 4c   2, BTREE_DEFAUL
294ee 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20  T_CACHE_SIZE);. 
294ef 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
294f0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
294f1 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29   OP_IfPos, 2, 0)
294f2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
294f3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
294f4 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20  Integer, -size, 
294f5 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
294f6 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
294f7 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 73 71   addr);.      sq
294f8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
294f9 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
294fa 20 69 44 62 2c 20 42 54 52 45 45 5f 44 45 46 41   iDb, BTREE_DEFA
294fb 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20  ULT_CACHE_SIZE, 
294fc 31 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70  1);.      pDb->p
294fd 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
294fe 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20  ze = size;.     
294ff 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
29500 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70  CacheSize(pDb->p
29501 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  Bt, pDb->pSchema
29502 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20  ->cache_size);. 
29503 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
29504 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
29505 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f  [database.]page_
29506 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  size.  **  PRAGM
29507 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
29508 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20  e_size=N.  **.  
29509 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
2950a 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
2950b 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f  rrent setting fo
2950c 72 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  r the.  ** datab
2950d 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 6e  ase page size in
2950e 20 62 79 74 65 73 2e 20 20 54 68 65 20 73 65 63   bytes.  The sec
2950f 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68  ond form sets th
29510 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
29511 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75 65 2e  page size value.
29512 20 20 54 68 65 20 76 61 6c 75 65 20 63 61 6e 20    The value can 
29513 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66 0a 20  only be set if. 
29514 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
29515 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
29516 6e 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a  n created..  */.
29517 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
29518 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67 65  ICmp(zLeft,"page
29519 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _size")==0 ){.  
2951a 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
2951b 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73  Db->pBt;.    ass
2951c 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a 20  ert( pBt!=0 );. 
2951d 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
2951e 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65  {.      int size
2951f 20 3d 20 41 4c 57 41 59 53 28 70 42 74 29 20 3f   = ALWAYS(pBt) ?
29520 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
29521 50 61 67 65 53 69 7a 65 28 70 42 74 29 20 3a 20  PageSize(pBt) : 
29522 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  0;.      returnS
29523 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
29524 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20 73 69   "page_size", si
29525 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ze);.    }else{.
29526 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20        /* Malloc 
29527 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20 73 65  may fail when se
29528 74 74 69 6e 67 20 74 68 65 20 70 61 67 65 2d 73  tting the page-s
29529 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20 69 73  ize, as there is
2952a 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 20   an internal.   
2952b 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74 68 61     ** buffer tha
2952c 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f 64 75  t the pager modu
2952d 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69 6e 67  le resizes using
2952e 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2952f 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ()..      */.   
29530 20 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73     db->nextPages
29531 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68  ize = atoi(zRigh
29532 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  t);.      if( SQ
29533 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69  LITE_NOMEM==sqli
29534 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
29535 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78  ize(pBt, db->nex
29536 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c 20 30  tPagesize, -1, 0
29537 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d  ) ){.        db-
29538 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
29539 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2953a 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
2953b 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
2953c 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f  abase.]max_page_
2953d 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52 41 47  count.  **  PRAG
2953e 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61  MA [database.]ma
2953f 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e 0a 20  x_page_count=N. 
29540 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
29541 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
29542 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
29543 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
29544 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
29545 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
29546 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
29547 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f 6e 64  The .  ** second
29548 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73 20 74   form attempts t
29549 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20 73 65  o change this se
2954a 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20 20 2a  tting.  Both.  *
2954b 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e 20 74  * forms return t
2954c 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74 69  he current setti
2954d 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ng..  */.  if( s
2954e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2954f 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f 63 6f  eft,"max_page_co
29550 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  unt")==0 ){.    
29551 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62  Btree *pBt = pDb
29552 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74 20 6e  ->pBt;.    int n
29553 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20 61  ewMax = 0;.    a
29554 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b  ssert( pBt!=0 );
29555 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
29556 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78 20  ){.      newMax 
29557 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
29558 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 41 4c      }.    if( AL
29559 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20 20 20  WAYS(pBt) ){.   
2955a 20 20 20 6e 65 77 4d 61 78 20 3d 20 73 71 6c 69     newMax = sqli
2955b 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
2955c 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77 4d 61 78  ount(pBt, newMax
2955d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
2955e 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
2955f 72 73 65 2c 20 22 6d 61 78 5f 70 61 67 65 5f 63  rse, "max_page_c
29560 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78 29 3b 0a  ount", newMax);.
29561 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
29562 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
29563 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f 75 6e 74  base.]page_count
29564 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
29565 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
29566 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 70 65  pages in the spe
29567 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65 2e  cified database.
29568 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
29569 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
2956a 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d  ,"page_count")==
2956b 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65  0 ){.    int iRe
2956c 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  g;.    if( sqlit
2956d 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2956e 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
2956f 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69  ma_out;.    sqli
29570 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
29571 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
29572 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70  ;.    iReg = ++p
29573 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
29574 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29575 70 32 28 76 2c 20 4f 50 5f 50 61 67 65 63 6f 75  p2(v, OP_Pagecou
29576 6e 74 2c 20 69 44 62 2c 20 69 52 65 67 29 3b 0a  nt, iDb, iReg);.
29577 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29578 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29579 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b  ltRow, iReg, 1);
2957a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2957b 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
2957c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2957d 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
2957e 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2957f 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20 53 51  "page_count", SQ
29580 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29581 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
29582 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29583 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  se.]locking_mode
29584 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29585 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67  atabase.]locking
29586 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d 61 6c 7c  _mode = (normal|
29587 65 78 63 6c 75 73 69 76 65 29 0a 20 20 2a 2f 0a  exclusive).  */.
29588 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
29589 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f 63 6b  ICmp(zLeft,"lock
2958a 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b  ing_mode")==0 ){
2958b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2958c 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d 61 6c 22  *zRet = "normal"
2958d 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 20  ;.    int eMode 
2958e 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65  = getLockingMode
2958f 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20 20 20 69  (zRight);..    i
29590 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26  f( pId2->n==0 &&
29591 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
29592 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20  CKINGMODE_QUERY 
29593 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70  ){.      /* Simp
29594 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f 63 6b 69  le "PRAGMA locki
29595 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d  ng_mode;" statem
29596 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20 71  ent. This is a q
29597 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a  uery for.      *
29598 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 65  * the current de
29599 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 6d 6f  fault locking mo
2959a 64 65 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  de (which may be
2959b 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a 20 20   different to.  
2959c 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 69      ** the locki
2959d 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d  ng-mode of the m
2959e 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e 0a 20  ain database).. 
2959f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d       */.      eM
295a0 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f  ode = db->dfltLo
295a1 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73  ckMode;.    }els
295a2 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  e{.      Pager *
295a3 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 69 66  pPager;.      if
295a4 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a  ( pId2->n==0 ){.
295a5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
295a6 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 6e  indicates that n
295a7 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  o database name 
295a8 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61 73  was specified as
295a9 20 70 61 72 74 0a 20 20 20 20 20 20 20 20 2a 2a   part.        **
295aa 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41 20 63   of the PRAGMA c
295ab 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73 20  ommand. In this 
295ac 63 61 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  case the locking
295ad 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a 20 20  -mode must be.  
295ae 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20        ** set on 
295af 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
295b0 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c 6c 20  abases, as well 
295b1 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62 20 66  as the main db f
295b2 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ile..        **.
295b3 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c          ** Also,
295b4 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64 66 6c   the sqlite3.dfl
295b5 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72 69 61 62  tLockMode variab
295b6 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  le is set so tha
295b7 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79  t.        ** any
295b8 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74   subsequently at
295b9 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
295ba 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70   also use the sp
295bb 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20 20  ecified.        
295bc 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e  ** locking mode.
295bd 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
295be 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
295bf 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d      assert(pDb==
295c0 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20  &db->aDb[0]);.  
295c1 20 20 20 20 20 20 66 6f 72 28 69 69 3d 32 3b 20        for(ii=2; 
295c2 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b  ii<db->nDb; ii++
295c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  ){.          pPa
295c4 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
295c5 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b  eePager(db->aDb[
295c6 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20  ii].pBt);.      
295c7 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
295c8 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
295c9 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
295ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62      }.        db
295cb 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d  ->dfltLockMode =
295cc 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20   (u8)eMode;.    
295cd 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
295ce 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
295cf 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a  ager(pDb->pBt);.
295d0 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 71        eMode = sq
295d1 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
295d2 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d  gMode(pPager, eM
295d3 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ode);.    }..   
295d4 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d 3d 50   assert(eMode==P
295d5 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
295d6 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65 3d 3d  _NORMAL||eMode==
295d7 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
295d8 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20  E_EXCLUSIVE);.  
295d9 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50 41 47    if( eMode==PAG
295da 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
295db 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20  XCLUSIVE ){.    
295dc 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c 75 73    zRet = "exclus
295dd 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ive";.    }.    
295de 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
295df 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
295e0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
295e1 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
295e2 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 63 6b  NAME_NAME, "lock
295e3 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54  ing_mode", SQLIT
295e4 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
295e5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
295e6 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
295e7 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c 20 30  0, 1, 0, zRet, 0
295e8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
295e9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
295ea 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b  esultRow, 1, 1);
295eb 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
295ec 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
295ed 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d  abase.]journal_m
295ee 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ode.  **  PRAGMA
295ef 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72   [database.]jour
295f0 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65 6c 65  nal_mode = (dele
295f1 74 65 7c 70 65 72 73 69 73 74 7c 6f 66 66 7c 74  te|persist|off|t
295f2 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72 79 29 0a  runcate|memory).
295f3 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
295f4 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
295f5 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d  "journal_mode")=
295f6 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 4d  =0 ){.    int eM
295f7 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20  ode;.    static 
295f8 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4d  char * const azM
295f9 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20  odeName[] = {.  
295fa 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20 22 70      "delete", "p
295fb 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22 2c 20  ersist", "off", 
295fc 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d 65 6d  "truncate", "mem
295fd 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20 20 20  ory".    };..   
295fe 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29   if( zRight==0 )
295ff 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  {.      eMode = 
29600 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
29601 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d 65 6c  E_QUERY;.    }el
29602 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20  se{.      int n 
29603 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
29604 30 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  0(zRight);.     
29605 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65 6f 66 28   eMode = sizeof(
29606 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73 69 7a 65  azModeName)/size
29607 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 5b 30 5d  of(azModeName[0]
29608 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 77 68 69  ) - 1;.      whi
29609 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20  le( eMode>=0 && 
2960a 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2960b 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64 65 4e 61  zRight, azModeNa
2960c 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29 21 3d 30  me[eMode], n)!=0
2960d 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d 6f 64   ){.        eMod
2960e 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e--;.      }.   
2960f 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64 32 2d   }.    if( pId2-
29610 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d  >n==0 && eMode==
29611 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
29612 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20 20 20  E_QUERY ){.     
29613 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52 41 47   /* Simple "PRAG
29614 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3b  MA journal_mode;
29615 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69  " statement. Thi
29616 73 20 69 73 20 61 20 71 75 65 72 79 20 66 6f 72  s is a query for
29617 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75  .      ** the cu
29618 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20 6a 6f  rrent default jo
29619 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77 68 69 63  urnal mode (whic
2961a 68 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65  h may be differe
2961b 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  nt to.      ** t
2961c 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20  he journal-mode 
2961d 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
2961e 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  base)..      */.
2961f 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62        eMode = db
29620 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  ->dfltJournalMod
29621 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
29622 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
29623 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  r;.      if( pId
29624 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  2->n==0 ){.     
29625 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63     /* This indic
29626 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74  ates that no dat
29627 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73  abase name was s
29628 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
29629 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
2962a 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e  he PRAGMA comman
2962b 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
2962c 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
2962d 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20   must be.       
2962e 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61   ** set on all a
2962f 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
29630 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68  s, as well as th
29631 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a  e main db file..
29632 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
29633 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
29634 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a 6f 75 72  sqlite3.dfltJour
29635 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61 62 6c 65  nalMode variable
29636 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a   is set so that.
29637 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73          ** any s
29638 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61  ubsequently atta
29639 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61  ched databases a
2963a 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70 65 63  lso use the spec
2963b 69 66 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  ified.        **
2963c 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20   journal mode.. 
2963d 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2963e 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
2963f 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64    assert(pDb==&d
29640 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20  b->aDb[0]);.    
29641 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69      for(ii=1; ii
29642 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b  <db->nDb; ii++){
29643 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64  .          if( d
29644 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 20 29  b->aDb[ii].pBt )
29645 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  {.            pP
29646 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
29647 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
29648 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  [ii].pBt);.     
29649 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
2964a 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70  gerJournalMode(p
2964b 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20  Pager, eMode);. 
2964c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2964d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d     }.        db-
2964e 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65  >dfltJournalMode
2964f 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20   = (u8)eMode;.  
29650 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
29651 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
29652 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
29653 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
29654 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
29655 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  nalMode(pPager, 
29656 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eMode);.    }.  
29657 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d    assert( eMode=
29658 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
29659 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20  DE_DELETE.      
2965a 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
2965b 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
2965c 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20 20  ODE_TRUNCATE.   
2965d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
2965e 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
2965f 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20  ALMODE_PERSIST. 
29660 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
29661 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
29662 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 20 20 20  RNALMODE_OFF.   
29663 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
29664 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
29665 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b  ALMODE_MEMORY );
29666 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29667 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
29668 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29669 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
2966a 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2966b 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 2c 20  "journal_mode", 
2966c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2966d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2966e 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
2966f 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 0a 20  ng8, 0, 1, 0, . 
29670 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f 64 65            azMode
29671 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50 34 5f  Name[eMode], P4_
29672 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
29673 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29674 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
29675 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  1, 1);.  }else..
29676 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
29677 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75  A [database.]jou
29678 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 0a  rnal_size_limit.
29679 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2967a 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
2967b 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a 20 20 2a  size_limit=N.  *
2967c 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73 65  *.  ** Get or se
2967d 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74  t the size limit
2967e 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75   on rollback jou
2967f 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20 20 2a 2f  rnal files..  */
29680 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29681 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75  rICmp(zLeft,"jou
29682 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22  rnal_size_limit"
29683 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61 67 65  )==0 ){.    Page
29684 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
29685 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
29686 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 36 34  b->pBt);.    i64
29687 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b 0a 20 20   iLimit = -2;.  
29688 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
29689 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f        sqlite3Ato
2968a 69 36 34 28 7a 52 69 67 68 74 2c 20 26 69 4c 69  i64(zRight, &iLi
2968b 6d 69 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  mit);.      if( 
2968c 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69 4c 69 6d  iLimit<-1 ) iLim
2968d 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  it = -1;.    }. 
2968e 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71 6c 69     iLimit = sqli
2968f 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53  te3PagerJournalS
29690 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65 72 2c  izeLimit(pPager,
29691 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 72 65   iLimit);.    re
29692 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
29693 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f 73  arse, "journal_s
29694 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c 69 6d  ize_limit", iLim
29695 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23 65  it);.  }else..#e
29696 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
29697 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
29698 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  S */..  /*.  ** 
29699 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2969a 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 0a 20  e.]auto_vacuum. 
2969b 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
2969c 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75  abase.]auto_vacu
2969d 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47  um=N.  **.  ** G
2969e 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 76 61  et or set the va
2969f 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
296a0 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d  ase 'auto-vacuum
296a1 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a  ' parameter..  *
296a2 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 6f  * The value is o
296a3 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e 45 20 31  ne of:  0 NONE 1
296a4 20 46 55 4c 4c 20 32 20 49 4e 43 52 45 4d 45 4e   FULL 2 INCREMEN
296a5 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  TAL.  */.#ifndef
296a6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
296a7 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71  OVACUUM.  if( sq
296a8 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
296a9 66 74 2c 22 61 75 74 6f 5f 76 61 63 75 75 6d 22  ft,"auto_vacuum"
296aa 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65  )==0 ){.    Btre
296ab 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42  e *pBt = pDb->pB
296ac 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
296ad 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  Bt!=0 );.    if(
296ae 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
296af 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
296b0 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
296b1 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  out;.    }.    i
296b2 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
296b3 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76 61 63      int auto_vac
296b4 75 75 6d 3b 0a 20 20 20 20 20 20 69 66 28 20 41  uum;.      if( A
296b5 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20 20  LWAYS(pBt) ){.  
296b6 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75         auto_vacu
296b7 75 6d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  um = sqlite3Btre
296b8 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70  eGetAutoVacuum(p
296b9 42 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Bt);.      }else
296ba 7b 0a 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f  {.         auto_
296bb 76 61 63 75 75 6d 20 3d 20 53 51 4c 49 54 45 5f  vacuum = SQLITE_
296bc 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
296bd 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  UM;.      }.    
296be 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
296bf 74 28 70 50 61 72 73 65 2c 20 22 61 75 74 6f 5f  t(pParse, "auto_
296c0 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f 76 61  vacuum", auto_va
296c1 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65  cuum);.    }else
296c2 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41 75 74  {.      int eAut
296c3 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63 75 75  o = getAutoVacuu
296c4 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  m(zRight);.     
296c5 20 61 73 73 65 72 74 28 20 65 41 75 74 6f 3e 3d   assert( eAuto>=
296c6 30 20 26 26 20 65 41 75 74 6f 3c 3d 32 20 29 3b  0 && eAuto<=2 );
296c7 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 41  .      db->nextA
296c8 75 74 6f 76 61 63 20 3d 20 28 75 38 29 65 41 75  utovac = (u8)eAu
296c9 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 41 4c  to;.      if( AL
296ca 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29 20 29  WAYS(eAuto>=0) )
296cb 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  {.        /* Cal
296cc 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  l SetAutoVacuum(
296cd 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69 61 6c  ) to set initial
296ce 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ize the internal
296cf 20 61 75 74 6f 20 61 6e 64 0a 20 20 20 20 20 20   auto and.      
296d0 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75 75 6d    ** incr-vacuum
296d1 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69 73 20   flags. This is 
296d2 72 65 71 75 69 72 65 64 20 69 6e 20 63 61 73 65  required in case
296d3 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
296d4 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 72 65 61  .        ** crea
296d5 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  tes the database
296d6 20 66 69 6c 65 2e 20 49 74 20 69 73 20 69 6d 70   file. It is imp
296d7 6f 72 74 61 6e 74 20 74 68 61 74 20 69 74 20 69  ortant that it i
296d8 73 20 63 72 65 61 74 65 64 0a 20 20 20 20 20 20  s created.      
296d9 20 20 2a 2a 20 61 73 20 61 6e 20 61 75 74 6f 2d    ** as an auto-
296da 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64  vacuum capable d
296db 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  b..        */.  
296dc 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73        int rc = s
296dd 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
296de 74 6f 56 61 63 75 75 6d 28 70 42 74 2c 20 65 41  toVacuum(pBt, eA
296df 75 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66  uto);.        if
296e0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
296e1 26 26 20 28 65 41 75 74 6f 3d 3d 31 20 7c 7c 20  && (eAuto==1 || 
296e2 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a 20 20 20  eAuto==2) ){.   
296e3 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 73         /* When s
296e4 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 6f 5f  etting the auto_
296e5 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 6f 20 65  vacuum mode to e
296e6 69 74 68 65 72 20 22 66 75 6c 6c 22 20 6f 72 20  ither "full" or 
296e7 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 22 69  .          ** "i
296e8 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20 77 72 69  ncremental", wri
296e9 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  te the value of 
296ea 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68 65 20 64  meta[6] in the d
296eb 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 20 20  atabase.        
296ec 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 66 6f 72    ** file. Befor
296ed 65 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 74  e writing to met
296ee 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74 68 61 74  a[6], check that
296ef 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69 63 61 74   meta[3] indicat
296f0 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
296f1 74 68 61 74 20 74 68 69 73 20 72 65 61 6c 6c 79  that this really
296f2 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   is an auto-vacu
296f3 75 6d 20 63 61 70 61 62 6c 65 20 64 61 74 61 62  um capable datab
296f4 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ase..          *
296f5 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  /.          stat
296f6 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
296f7 69 73 74 20 73 65 74 4d 65 74 61 36 5b 5d 20 3d  ist setMeta6[] =
296f8 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b   {.            {
296f9 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c   OP_Transaction,
296fa 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 31      0,         1
296fb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
296fc 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f    0},    /* 0 */
296fd 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
296fe 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20  P_ReadCookie,   
296ff 20 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20    0,         1, 
29700 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 4c 41          BTREE_LA
29701 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 7d  RGEST_ROOT_PAGE}
29702 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
29703 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20 20 20  OP_If,          
29704 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 30 2c     1,         0,
29705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29706 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   0},    /* 2 */.
29707 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29708 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 20 20  _Halt,          
29709 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 41   SQLITE_OK, OE_A
2970a 62 6f 72 74 2c 20 20 20 20 20 20 20 20 20 20 30  bort,          0
2970b 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20  },    /* 3 */.  
2970c 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
2970d 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30  nteger,        0
2970e 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20  ,         1,    
2970f 20 20 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c               0},
29710 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20      /* 4 */.    
29711 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74          { OP_Set
29712 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20  Cookie,      0, 
29713 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e          BTREE_IN
29714 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d 2c 20 20  CR_VACUUM, 1},  
29715 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20    /* 5 */.      
29716 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
29717 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20   int iAddr;.    
29718 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
29719 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
2971a 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
2971b 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65  setMeta6), setMe
2971c 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ta6);.          
2971d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2971e 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44  eP1(v, iAddr, iD
2971f 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
29720 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29721 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44  1(v, iAddr+1, iD
29722 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
29723 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29724 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41  2(v, iAddr+2, iA
29725 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20  ddr+4);.        
29726 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29727 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34  ngeP1(v, iAddr+4
29728 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20  , eAuto-1);.    
29729 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2972a 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
2972b 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+5, iDb);.    
2972c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2972d 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
2972e 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
2972f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
29730 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  lse.#endif..  /*
29731 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29732 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65  atabase.]increme
29733 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20  ntal_vacuum(N). 
29734 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74   **.  ** Do N st
29735 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74  eps of increment
29736 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20  al vacuuming on 
29737 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  a database..  */
29738 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29739 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
2973a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2973b 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72  ICmp(zLeft,"incr
2973c 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29  emental_vacuum")
2973d 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
2973e 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20 20  Limit, addr;.   
2973f 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
29740 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
29741 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61  {.      goto pra
29742 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  gma_out;.    }. 
29743 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30     if( zRight==0
29744 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49   || !sqlite3GetI
29745 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c  nt32(zRight, &iL
29746 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c  imit) || iLimit<
29747 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d  =0 ){.      iLim
29748 69 74 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b  it = 0x7fffffff;
29749 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2974a 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2974b 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
2974c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
2974d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2974e 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d  OP_Integer, iLim
2974f 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 72  it, 1);.    addr
29750 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29751 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56  dOp1(v, OP_IncrV
29752 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20  acuum, iDb);.   
29753 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29754 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p1(v, OP_ResultR
29755 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ow, 1);.    sqli
29756 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29757 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d   OP_AddImm, 1, -
29758 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
29759 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2975a 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b  IfPos, 1, addr);
2975b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2975c 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2975d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
2975e 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
2975f 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
29760 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
29761 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29762 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a  .]cache_size.  *
29763 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
29764 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d  ase.]cache_size=
29765 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
29766 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
29767 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ts the current l
29768 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66 6f 72  ocal setting for
29769 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63   the.  ** page c
2976a 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20  ache size.  The 
2976b 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 63 61  local setting ca
2976c 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66  n be different f
2976d 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70 65 72  rom.  ** the per
2976e 73 69 73 74 65 6e 74 20 63 61 63 68 65 20 73 69  sistent cache si
2976f 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 73  ze value that is
29770 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
29771 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c  atabase.  ** fil
29772 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 76  e itself.  The v
29773 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
29774 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
29775 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
29776 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
29777 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
29778 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 20 6c   form sets the l
29779 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63  ocal.  ** page c
2977a 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 2e  ache size value.
2977b 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 63 68    It does not ch
2977c 61 6e 67 65 20 74 68 65 20 70 65 72 73 69 73 74  ange the persist
2977d 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65 20 73  ent.  ** cache s
2977e 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20 74 68  ize stored on th
2977f 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20 63 61  e disk so the ca
29780 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20 72 65  che size will re
29781 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73  vert.  ** to its
29782 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77   default value w
29783 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
29784 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 72   is closed and r
29785 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20  eopened..  ** N 
29786 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 73 69  should be a posi
29787 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a 20 20  tive integer..  
29788 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
29789 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63  StrICmp(zLeft,"c
2978a 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  ache_size")==0 )
2978b 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2978c 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
2978d 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
2978e 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21  a_out;.    if( !
2978f 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
29790 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
29791 70 50 61 72 73 65 2c 20 22 63 61 63 68 65 5f 73  pParse, "cache_s
29792 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63 68 65  ize", pDb->pSche
29793 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
29794 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
29795 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f    int size = ato
29796 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
29797 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
29798 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
29799 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
2979a 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a  cache_size = siz
2979b 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
2979c 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
2979d 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d  e(pDb->pBt, pDb-
2979e 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
2979f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  size);.    }.  }
297a0 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
297a1 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
297a2 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ore.  **   PRAGM
297a3 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22  A temp_store = "
297a4 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79  default"|"memory
297a5 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20  "|"file".  **.  
297a6 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
297a7 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
297a8 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f   of the temp_sto
297a9 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  re flag.  Changi
297aa 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61  ng.  ** the loca
297ab 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74  l value does not
297ac 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f   make changes to
297ad 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61   the disk file a
297ae 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20  nd the default. 
297af 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62   ** value will b
297b0 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
297b1 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
297b2 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e  abase is opened.
297b3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20  .  **.  ** Note 
297b4 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
297b5 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69 62 72  ble for the libr
297b6 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ary compile-time
297b7 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a   options to.  **
297b8 20 6f 76 65 72 72 69 64 65 20 74 68 69 73 20 73   override this s
297b9 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66  etting.  */.  if
297ba 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
297bb 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
297bc 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ore")==0 ){.    
297bd 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
297be 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
297bf 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 74 65  eInt(pParse, "te
297c0 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74  mp_store", db->t
297c1 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20  emp_store);.    
297c2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61  }else{.      cha
297c3 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70  ngeTempStorage(p
297c4 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a  Parse, zRight);.
297c5 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
297c6 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
297c7 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  A temp_store_dir
297c8 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52  ectory.  **   PR
297c9 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
297ca 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22  directory = ""|"
297cb 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a  directory_name".
297cc 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
297cd 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
297ce 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74  l value of the t
297cf 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
297d0 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e 67  ory flag.  Chang
297d1 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c  ing.  ** the val
297d2 75 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66  ue sets a specif
297d3 69 63 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20  ic directory to 
297d4 62 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70  be used for temp
297d5 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a  orary files..  *
297d6 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61 20 6e  * Setting to a n
297d7 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76 65 72  ull string rever
297d8 74 73 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c  ts to the defaul
297d9 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65  t temporary dire
297da 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a 20 20  ctory search..  
297db 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72 79 20  ** If temporary 
297dc 64 69 72 65 63 74 6f 72 79 20 69 73 20 63 68 61  directory is cha
297dd 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c  nged, then inval
297de 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65  idateTempStorage
297df 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66  ..  **.  */.  if
297e0 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
297e1 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
297e2 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 29 3d  ore_directory")=
297e3 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a  =0 ){.    if( !z
297e4 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
297e5 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
297e6 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
297e7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
297e8 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
297e9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
297ea 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
297eb 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
297ec 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ME, .           
297ed 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   "temp_store_dir
297ee 65 63 74 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f  ectory", SQLITE_
297ef 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
297f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
297f1 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
297f2 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74  , 0, 1, 0, sqlit
297f3 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
297f4 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  y, 0);.        s
297f5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
297f6 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
297f7 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  , 1, 1);.      }
297f8 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e  .    }else{.#ifn
297f9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
297fa 57 53 44 0a 20 20 20 20 20 20 69 66 28 20 7a 52  WSD.      if( zR
297fb 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  ight[0] ){.     
297fc 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
297fd 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
297fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
297ff 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66  OsAccess(db->pVf
29800 73 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c 49 54  s, zRight, SQLIT
29801 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
29802 54 45 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  TE, &res);.     
29803 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
29804 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29  E_OK || res==0 )
29805 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
29806 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
29807 73 65 2c 20 22 6e 6f 74 20 61 20 77 72 69 74 61  se, "not a writa
29808 62 6c 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b  ble directory");
29809 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
2980a 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
2980b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2980c 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54      if( SQLITE_T
2980d 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20  EMP_STORE==0.   
2980e 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54      || (SQLITE_T
2980f 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20  EMP_STORE==1 && 
29810 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d  db->temp_store<=
29811 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 53 51  1).       || (SQ
29812 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
29813 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73  =2 && db->temp_s
29814 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20 20 29  tore==1).      )
29815 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69  {.        invali
29816 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28  dateTempStorage(
29817 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d  pParse);.      }
29818 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
29819 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65 6d 70  ree(sqlite3_temp
2981a 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20  _directory);.   
2981b 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
2981c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2981d 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
2981e 72 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  ry = sqlite3DbSt
2981f 72 44 75 70 28 30 2c 20 7a 52 69 67 68 74 29 3b  rDup(0, zRight);
29820 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
29821 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65        sqlite3_te
29822 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30  mp_directory = 0
29823 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
29824 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
29825 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d  WSD */.    }.  }
29826 65 6c 73 65 0a 0a 23 69 66 20 21 64 65 66 69 6e  else..#if !defin
29827 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
29828 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a  _LOCKING_STYLE).
29829 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  #  if defined(__
2982a 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65  APPLE__).#    de
2982b 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
2982c 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
2982d 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20   1.#  else.#    
2982e 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
2982f 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
29830 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65  LE 0.#  endif.#e
29831 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
29832 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
29833 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a 20  TYLE.  /*.   ** 
29834 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29835 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66  se.]lock_proxy_f
29836 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50 52 41 47  ile.   **   PRAG
29837 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
29838 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 3d 20  ck_proxy_file = 
29839 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f 66  ":auto:"|"lock_f
2983a 69 6c 65 5f 70 61 74 68 22 0a 20 20 20 2a 2a 0a  ile_path".   **.
2983b 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20     ** Return or 
2983c 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
2983d 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f   the lock_proxy_
2983e 66 69 6c 65 20 66 6c 61 67 2e 20 20 43 68 61 6e  file flag.  Chan
2983f 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68 65 20 76  ging.   ** the v
29840 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
29841 69 66 69 63 20 66 69 6c 65 20 74 6f 20 62 65 20  ific file to be 
29842 75 73 65 64 20 66 6f 72 20 64 61 74 61 62 61 73  used for databas
29843 65 20 61 63 63 65 73 73 20 6c 6f 63 6b 73 2e 0a  e access locks..
29844 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69 66     **.   */.  if
29845 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29846 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72  (zLeft, "lock_pr
29847 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b  oxy_file")==0 ){
29848 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
29849 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20   ){.      Pager 
2984a 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  *pPager = sqlite
2984b 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d  3BtreePager(pDb-
2984c 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 63 68 61  >pBt);.      cha
2984d 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61  r *proxy_file_pa
2984e 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20  th = NULL;.     
2984f 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
29850 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61  File = sqlite3Pa
29851 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
29852 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
29853 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c  FileControl(pFil
29854 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f  e, SQLITE_GET_LO
29855 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20  CKPROXYFILE, .  
29856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29857 20 20 20 20 20 20 20 20 20 26 70 72 6f 78 79 5f           &proxy_
29858 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20 20 20 20  file_path);.    
29859 20 20 0a 20 20 20 20 20 20 69 66 28 20 70 72 6f    .      if( pro
2985a 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a  xy_file_path ){.
2985b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2985c 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
2985d 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
2985e 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2985f 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29860 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20  _NAME, .        
29861 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29862 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78        "lock_prox
29863 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f  y_file", SQLITE_
29864 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29865 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29866 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
29867 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79  , 0, 1, 0, proxy
29868 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a  _file_path, 0);.
29869 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2986a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2986b 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
2986c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2986d 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72  lse{.      Pager
2986e 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74   *pPager = sqlit
2986f 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62  e3BtreePager(pDb
29870 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71  ->pBt);.      sq
29871 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
29872 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
29873 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
29874 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
29875 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
29876 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d   ){.        res=
29877 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e  sqlite3OsFileCon
29878 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49  trol(pFile, SQLI
29879 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
2987a 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20  FILE, .         
2987b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2987c 20 20 20 20 20 20 20 20 20 20 20 20 7a 52 69 67              zRig
2987d 68 74 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  ht);.      } els
2987e 65 20 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d  e {.        res=
2987f 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e  sqlite3OsFileCon
29880 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49  trol(pFile, SQLI
29881 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
29882 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20  FILE, .         
29883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29884 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
29885 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
29886 20 69 66 28 20 72 65 73 21 3d 53 51 4c 49 54 45   if( res!=SQLITE
29887 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73  _OK ){.        s
29888 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
29889 50 61 72 73 65 2c 20 22 66 61 69 6c 65 64 20 74  Parse, "failed t
2988a 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79  o set lock proxy
2988b 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20 20 20   file");.       
2988c 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
2988d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2988e 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
2988f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
29890 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
29891 20 20 20 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a        .    .  /*
29892 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
29893 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72  database.]synchr
29894 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41  onous.  **   PRA
29895 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73  GMA [database.]s
29896 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f  ynchronous=OFF|O
29897 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20  N|NORMAL|FULL.  
29898 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f  **.  ** Return o
29899 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20  r set the local 
2989a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e  value of the syn
2989b 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20  chronous flag.  
2989c 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68  Changing.  ** th
2989d 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f  e local value do
2989e 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e  es not make chan
2989f 67 65 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20  ges to the disk 
298a0 66 69 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a  file and the.  *
298a1 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * default value 
298a2 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64  will be restored
298a3 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74   the next time t
298a4 68 65 20 64 61 74 61 62 61 73 65 20 69 73 0a 20  he database is. 
298a5 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f   ** opened..  */
298a6 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
298a7 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e  rICmp(zLeft,"syn
298a8 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b  chronous")==0 ){
298a9 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
298aa 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
298ab 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
298ac 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a  _out;.    if( !z
298ad 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72  Right ){.      r
298ae 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
298af 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e  Parse, "synchron
298b0 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74  ous", pDb->safet
298b1 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20  y_level-1);.    
298b2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
298b3 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
298b4 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
298b5 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
298b6 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
298b7 20 22 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d   "Safety level m
298b8 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  ay not be change
298b9 64 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73  d inside a trans
298ba 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20  action");.      
298bb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
298bc 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c  Db->safety_level
298bd 20 3d 20 67 65 74 53 61 66 65 74 79 4c 65 76 65   = getSafetyLeve
298be 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20 20  l(zRight)+1;.   
298bf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
298c0 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
298c1 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
298c2 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64  RAGMAS */..#ifnd
298c3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
298c4 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20 69 66  LAG_PRAGMAS.  if
298c5 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70 50 61  ( flagPragma(pPa
298c6 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67  rse, zLeft, zRig
298c7 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ht) ){.    /* Th
298c8 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73  e flagPragma() s
298c9 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67  ubroutine also g
298ca 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63  enerates any nec
298cb 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20 20 20  essary code.    
298cc 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68  ** there is noth
298cd 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68  ing more to do h
298ce 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23  ere */.  }else.#
298cf 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
298d0 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41  OMIT_FLAG_PRAGMA
298d1 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
298d2 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41  LITE_OMIT_SCHEMA
298d3 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20  _PRAGMAS.  /*.  
298d4 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61 62 6c  **   PRAGMA tabl
298d5 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a  e_info(<table>).
298d6 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
298d7 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f   a single row fo
298d8 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66  r each column of
298d9 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
298da 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66  . The columns of
298db 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75 72 6e  .  ** the return
298dc 65 64 20 64 61 74 61 20 73 65 74 20 61 72 65 3a  ed data set are:
298dd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20  .  **.  ** cid: 
298de 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64         Column id
298df 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20   (numbered from 
298e0 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c 20 73  left to right, s
298e1 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a 20 20  tarting at 0).  
298e2 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20 20 43  ** name:       C
298e3 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20  olumn name.  ** 
298e4 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f 6c 75  type:       Colu
298e5 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  mn declaration t
298e6 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c  ype..  ** notnul
298e7 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20 27 4e  l:    True if 'N
298e8 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61 72 74  OT NULL' is part
298e9 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61   of column decla
298ea 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74  ration.  ** dflt
298eb 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61  _value: The defa
298ec 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
298ed 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79  e column, if any
298ee 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
298ef 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
298f0 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29  t, "table_info")
298f1 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
298f2 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
298f3 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
298f4 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
298f5 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
298f6 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
298f7 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
298f8 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
298f9 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
298fa 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  b ){.      int i
298fb 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 69 64  ;.      int nHid
298fc 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43  den = 0;.      C
298fd 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20  olumn *pCol;.   
298fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
298ff 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a  tNumCols(v, 6);.
29900 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
29901 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20 73 71  em = 6;.      sq
29902 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
29903 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
29904 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53  E_NAME, "cid", S
29905 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
29906 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29907 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
29908 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29909 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
2990a 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2990b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2990c 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
2990d 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 53  _NAME, "type", S
2990e 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2990f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29910 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c  SetColName(v, 3,
29911 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29912 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45  notnull", SQLITE
29913 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29914 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29915 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e  lName(v, 4, COLN
29916 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f  AME_NAME, "dflt_
29917 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f 53  value", SQLITE_S
29918 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
29919 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2991a 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d  ame(v, 5, COLNAM
2991b 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51  E_NAME, "pk", SQ
2991c 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2991d 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47      sqlite3ViewG
2991e 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
2991f 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
29920 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c     for(i=0, pCol
29921 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70  =pTab->aCol; i<p
29922 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  Tab->nCol; i++, 
29923 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pCol++){.       
29924 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c   if( IsHiddenCol
29925 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20  umn(pCol) ){.   
29926 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b         nHidden++
29927 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
29928 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
29929 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2992a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2992b 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69 64 64  Integer, i-nHidd
2992c 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  en, 1);.        
2992d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2992e 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2992f 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e   0, 2, 0, pCol->
29930 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
29931 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29932 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
29933 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20  g8, 0, 3, 0,.   
29934 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54          pCol->zT
29935 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70  ype ? pCol->zTyp
29936 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20 20  e : "", 0);.    
29937 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29938 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29939 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e  ger, (pCol->notN
2993a 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20 34 29  ull ? 1 : 0), 4)
2993b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  ;.        if( pC
2993c 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a 20 20 20  ol->zDflt ){.   
2993d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2993e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
2993f 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20 30 2c  tring8, 0, 5, 0,
29940 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d 3e 7a 44   (char*)pCol->zD
29941 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  flt, 0);.       
29942 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
29943 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29944 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
29945 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 5);.        }
29946 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29947 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29948 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c 2d 3e  _Integer, pCol->
29949 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b 0a 20  isPrimKey, 6);. 
2994a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2994b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2994c 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36 29 3b  esultRow, 1, 6);
2994d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2994e 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71   }else..  if( sq
2994f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
29950 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66 6f 22  ft, "index_info"
29951 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
29952 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
29953 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  x;.    Table *pT
29954 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
29955 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
29956 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
29957 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 49 64  gma_out;.    pId
29958 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
29959 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68 74 2c  ndex(db, zRight,
2995a 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
2995b 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Idx ){.      int
2995c 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   i;.      pTab =
2995d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20   pIdx->pTable;. 
2995e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2995f 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
29960 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
29961 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20  nMem = 3;.      
29962 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29963 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29964 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 6e 6f  AME_NAME, "seqno
29965 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29966 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29967 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29968 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
29969 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54 45  E, "cid", SQLITE
2996a 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2996b 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2996c 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
2996d 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
2996e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2996f 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
29970 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   i<pIdx->nColumn
29971 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
29972 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64 78 2d  int cnum = pIdx-
29973 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20  >aiColumn[i];.  
29974 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29975 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
29976 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20  teger, i, 1);.  
29977 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29978 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
29979 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32 29 3b  teger, cnum, 2);
2997a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2997b 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d   pTab->nCol>cnum
2997c 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
2997d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2997e 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
2997f 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  3, 0, pTab->aCol
29980 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29  [cnum].zName, 0)
29981 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29982 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29983 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
29984 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  3);.      }.    
29985 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
29986 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29987 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 6c 69  zLeft, "index_li
29988 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  st")==0 && zRigh
29989 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  t ){.    Index *
2998a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20  pIdx;.    Table 
2998b 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73  *pTab;.    if( s
2998c 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2998d 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
2998e 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
2998f 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
29990 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67  ndTable(db, zRig
29991 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  ht, zDb);.    if
29992 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
29993 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
29994 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
29995 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d 3e 70    pIdx = pTab->p
29996 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69 66 28  Index;.      if(
29997 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20   pIdx ){.       
29998 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20   int i = 0; .   
29999 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2999a 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
2999b 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
2999c 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
2999d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2999e 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
2999f 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
299a0 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  eq", SQLITE_STAT
299a1 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
299a2 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
299a3 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
299a4 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53  _NAME, "name", S
299a5 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
299a6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
299a7 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
299a8 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
299a9 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c 49 54   "unique", SQLIT
299aa 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
299ab 20 20 20 77 68 69 6c 65 28 70 49 64 78 29 7b 0a     while(pIdx){.
299ac 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
299ad 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
299ae 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29  P_Integer, i, 1)
299af 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
299b0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
299b1 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
299b2 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  2, 0, pIdx->zNam
299b3 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  e, 0);.         
299b4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
299b5 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
299b6 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  , pIdx->onError!
299b7 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20 20  =OE_None, 3);.  
299b8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
299b9 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
299ba 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29  ResultRow, 1, 3)
299bb 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b  ;.          ++i;
299bc 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78 20  .          pIdx 
299bd 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a 20  = pIdx->pNext;. 
299be 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
299bf 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
299c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
299c1 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61 74  ICmp(zLeft, "dat
299c2 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d 30 20  abase_list")==0 
299c3 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
299c4 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
299c5 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
299c6 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
299c7 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
299c8 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
299c9 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  3);.    pParse->
299ca 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73 71  nMem = 3;.    sq
299cb 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
299cc 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
299cd 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53  E_NAME, "seq", S
299ce 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
299cf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
299d0 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
299d1 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
299d2 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
299d3 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
299d4 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
299d5 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
299d6 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c 49 54  E, "file", SQLIT
299d7 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  E_STATIC);.    f
299d8 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
299d9 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  b; i++){.      i
299da 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
299db 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
299dc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
299dd 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 21  b->aDb[i].zName!
299de 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
299df 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
299e0 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
299e1 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
299e2 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
299e3 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c  P_String8, 0, 2,
299e4 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a   0, db->aDb[i].z
299e5 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
299e6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
299e7 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
299e8 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20   0, 3, 0,.      
299e9 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
299ea 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d  eGetFilename(db-
299eb 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20 30 29  >aDb[i].pBt), 0)
299ec 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
299ed 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
299ee 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29  ResultRow, 1, 3)
299ef 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
299f0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
299f1 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63 6f  rICmp(zLeft, "co
299f2 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29 3d 3d  llation_list")==
299f3 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d  0 ){.    int i =
299f4 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d   0;.    HashElem
299f5 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   *p;.    sqlite3
299f6 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
299f7 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 2);.    pParse
299f8 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20  ->nMem = 2;.    
299f9 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
299fa 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
299fb 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
299fc 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
299fd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
299fe 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
299ff 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29a00 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
29a01 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 70  ATIC);.    for(p
29a02 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
29a03 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
29a04 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68   p; p=sqliteHash
29a05 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20  Next(p)){.      
29a06 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
29a07 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69   (CollSeq *)sqli
29a08 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20  teHashData(p);. 
29a09 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a0a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
29a0b 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b 0a 20  eger, i++, 1);. 
29a0c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a0d 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29a0e 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70  ing8, 0, 2, 0, p
29a0f 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  Coll->zName, 0);
29a10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29a11 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
29a12 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b  esultRow, 1, 2);
29a13 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
29a14 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29a15 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47  OMIT_SCHEMA_PRAG
29a16 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
29a17 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
29a18 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20 73 71  IGN_KEY.  if( sq
29a19 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
29a1a 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79  ft, "foreign_key
29a1b 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52  _list")==0 && zR
29a1c 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b 65 79  ight ){.    FKey
29a1d 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62 6c 65   *pFK;.    Table
29a1e 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20   *pTab;.    if( 
29a1f 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
29a20 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
29a21 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
29a22 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
29a23 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69  indTable(db, zRi
29a24 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69  ght, zDb);.    i
29a25 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
29a26 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
29a27 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
29a28 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d 3e 70     pFK = pTab->p
29a29 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66 28 20  FKey;.      if( 
29a2a 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  pFK ){.        i
29a2b 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20 20 20  nt i = 0; .     
29a2c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29a2d 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29 3b 0a  tNumCols(v, 8);.
29a2e 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
29a2f 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20 20 20  nMem = 8;.      
29a30 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29a31 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
29a32 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 64 22  LNAME_NAME, "id"
29a33 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29a34 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29a35 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29a36 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
29a37 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54  ME, "seq", SQLIT
29a38 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
29a39 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29a3a 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
29a3b 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 61  OLNAME_NAME, "ta
29a3c 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ble", SQLITE_STA
29a3d 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
29a3e 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
29a3f 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d  ame(v, 3, COLNAM
29a40 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20  E_NAME, "from", 
29a41 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29a42 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29a43 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
29a44 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   4, COLNAME_NAME
29a45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45 5f 53  , "to", SQLITE_S
29a46 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
29a47 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29a48 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e  lName(v, 5, COLN
29a49 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75 70  AME_NAME, "on_up
29a4a 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  date", SQLITE_ST
29a4b 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
29a4c 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29a4d 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c 4e 41  Name(v, 6, COLNA
29a4e 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65 6c  ME_NAME, "on_del
29a4f 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ete", SQLITE_STA
29a50 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
29a51 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
29a52 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e 41 4d  ame(v, 7, COLNAM
29a53 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68 22 2c  E_NAME, "match",
29a54 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
29a55 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 70  .        while(p
29a56 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  FK){.          i
29a57 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20  nt j;.          
29a58 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d 3e  for(j=0; j<pFK->
29a59 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  nCol; j++){.    
29a5a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43          char *zC
29a5b 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b 6a  ol = pFK->aCol[j
29a5c 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ].zCol;.        
29a5d 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 44 65 6c      char *zOnDel
29a5e 65 74 65 20 3d 20 28 63 68 61 72 20 2a 29 61 63  ete = (char *)ac
29a5f 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 61 41  tionName(pFK->aA
29a60 63 74 69 6f 6e 5b 30 5d 29 3b 0a 20 20 20 20 20  ction[0]);.     
29a61 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e         char *zOn
29a62 55 70 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a  Update = (char *
29a63 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d  )actionName(pFK-
29a64 3e 61 41 63 74 69 6f 6e 5b 31 5d 29 3b 0a 20 20  >aAction[1]);.  
29a65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29a66 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29a67 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29  P_Integer, i, 1)
29a68 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
29a69 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29a6a 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a  v, OP_Integer, j
29a6b 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
29a6c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29a6d 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29a6e 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46 4b 2d  8, 0, 3, 0, pFK-
29a6f 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20  >zTo, 0);.      
29a70 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29a71 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
29a72 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30 2c 0a  ring8, 0, 4, 0,.
29a73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
29a75 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61 43  ab->aCol[pFK->aC
29a76 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e 61  ol[j].iFrom].zNa
29a77 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
29a78 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29a79 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20 3f 20  ddOp4(v, zCol ? 
29a7a 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f 50 5f  OP_String8 : OP_
29a7b 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c 20 7a  Null, 0, 5, 0, z
29a7c 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Col, 0);.       
29a7d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a7e 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29a7f 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c 20 7a  ing8, 0, 6, 0, z
29a80 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a 20 20  OnUpdate, 0);.  
29a81 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29a82 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29a83 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 37 2c  P_String8, 0, 7,
29a84 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c 20 30   0, zOnDelete, 0
29a85 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
29a86 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29a87 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
29a88 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45 22 2c  0, 8, 0, "NONE",
29a89 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
29a8a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29a8b 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
29a8c 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20 20 20  ow, 1, 8);.     
29a8d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
29a8e 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20 20 20   ++i;.          
29a8f 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65 78 74  pFK = pFK->pNext
29a90 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  From;.        }.
29a91 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29a92 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
29a93 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
29a94 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
29a95 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ) */..#ifndef ND
29a96 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c 69 74  EBUG.  if( sqlit
29a97 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29a98 20 22 70 61 72 73 65 72 5f 74 72 61 63 65 22 29   "parser_trace")
29a99 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  ==0 ){.    if( z
29a9a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
29a9b 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52  f( getBoolean(zR
29a9c 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ight) ){.       
29a9d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
29a9e 61 63 65 28 73 74 64 65 72 72 2c 20 22 70 61 72  ace(stderr, "par
29a9f 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20 20 7d  ser: ");.      }
29aa0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
29aa1 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
29aa2 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  (0, 0);.      }.
29aa3 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
29aa4 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69 6e 73  ndif..  /* Reins
29aa5 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20 61 6e  tall the LIKE an
29aa6 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73  d GLOB functions
29aa7 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74 20 6f  .  The variant o
29aa8 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73 65 64  f LIKE.  ** used
29aa9 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20 73 65   will be case se
29aaa 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74 20 64  nsitive or not d
29aab 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
29aac 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  RHS..  */.  if( 
29aad 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29aae 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65 6e 73  Left, "case_sens
29aaf 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d 30 20  itive_like")==0 
29ab0 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  ){.    if( zRigh
29ab1 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
29ab2 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75  e3RegisterLikeFu
29ab3 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65 74 42  nctions(db, getB
29ab4 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 29 3b  oolean(zRight));
29ab5 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
29ab6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 49  #ifndef SQLITE_I
29ab7 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
29ab8 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66 69 6e  RROR_MAX.# defin
29ab9 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  e SQLITE_INTEGRI
29aba 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
29abb 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a 0a 23  AX 100.#endif..#
29abc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29abd 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
29abe 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61 20 22  CK.  /* Pragma "
29abf 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69 73 20  quick_check" is 
29ac0 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  an experimental 
29ac1 72 65 64 75 63 65 64 20 76 65 72 73 69 6f 6e 20  reduced version 
29ac2 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67 72 69  of .  ** integri
29ac3 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67 6e 65  ty_check designe
29ac4 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f 73 74  d to detect most
29ac5 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
29ac6 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 6f 75  tion.  ** withou
29ac7 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 6f 76  t most of the ov
29ac8 65 72 68 65 61 64 20 6f 66 20 61 20 66 75 6c 6c  erhead of a full
29ac9 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63 6b   integrity-check
29aca 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
29acb 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29acc 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68  t, "integrity_ch
29acd 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c 20 73  eck")==0.   || s
29ace 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29acf 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68 65 63  eft, "quick_chec
29ad0 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20  k")==0 .  ){.   
29ad1 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64 72 2c   int i, j, addr,
29ad2 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f 2a 20   mxErr;..    /* 
29ad3 43 6f 64 65 20 74 68 61 74 20 61 70 70 65 61 72  Code that appear
29ad4 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  s at the end of 
29ad5 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 63 68  the integrity ch
29ad6 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72 72 6f  eck.  If no erro
29ad7 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61 67 65  r.    ** message
29ad8 73 20 68 61 76 65 20 62 65 65 6e 20 67 65 6e 65  s have been gene
29ad9 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20 4f 4b  rated, output OK
29ada 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f 75 74  .  Otherwise out
29adb 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65  put the.    ** e
29adc 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 20  rror message.   
29add 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63   */.    static c
29ade 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
29adf 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20  endCode[] = {.  
29ae0 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
29ae1 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20        1, 0,     
29ae2 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a     0},    /* 0 *
29ae3 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 4e  /.      { OP_IfN
29ae4 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30 2c 20  eg,       1, 0, 
29ae5 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
29ae6 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50   1 */.      { OP
29ae7 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c  _String8,     0,
29ae8 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20   3,        0},  
29ae9 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
29aea 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
29aeb 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20 20 30    3, 1,        0
29aec 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69  },.    };..    i
29aed 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28 7a 4c  nt isQuick = (zL
29aee 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a 20  eft[0]=='q');.. 
29aef 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
29af0 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
29af1 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  m */.    if( sql
29af2 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
29af3 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
29af4 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 50  agma_out;.    pP
29af5 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a  arse->nMem = 6;.
29af6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
29af7 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
29af8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29af9 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
29afa 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29afb 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22  integrity_check"
29afc 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29afd 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
29afe 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72 20  e maximum error 
29aff 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d 78 45  count */.    mxE
29b00 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rr = SQLITE_INTE
29b01 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
29b02 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28 20 7a  R_MAX;.    if( z
29b03 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 6d  Right ){.      m
29b04 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52 69 67  xErr = atoi(zRig
29b05 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  ht);.      if( m
29b06 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20 20 20  xErr<=0 ){.     
29b07 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49 54     mxErr = SQLIT
29b08 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  E_INTEGRITY_CHEC
29b09 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20  K_ERROR_MAX;.   
29b0a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
29b0b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29b0c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
29b0d 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a 20 72  mxErr, 1);  /* r
29b0e 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72 72 6f  eg[1] holds erro
29b0f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20 20 20  rs left */..    
29b10 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72 69  /* Do an integri
29b11 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61 63 68  ty check on each
29b12 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
29b13 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
29b14 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
29b15 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a        HashElem *
29b16 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20 2a 70  x;.      Hash *p
29b17 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e 74 20  Tbls;.      int 
29b18 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  cnt = 0;..      
29b19 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20  if( OMIT_TEMPDB 
29b1a 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e  && i==1 ) contin
29b1b 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  ue;..      sqlit
29b1c 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
29b1d 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20  ma(pParse, i);. 
29b1e 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
29b1f 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
29b20 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20 2f   OP_IfPos, 1); /
29b21 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20 6f 66  * Halt if out of
29b22 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20   errors */.     
29b23 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29b24 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30  p2(v, OP_Halt, 0
29b25 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
29b26 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
29b27 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20 20  v, addr);..     
29b28 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72   /* Do an integr
29b29 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ity check of the
29b2a 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 2a 2a   B-Tree.      **
29b2b 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69 6e 20  .      ** Begin 
29b2c 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67 69 73  by filling regis
29b2d 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e 20 77  ters 2, 3, ... w
29b2e 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ith the root pag
29b2f 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20 20 20  es numbers.     
29b30 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c   ** for all tabl
29b31 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 20 69  es and indices i
29b32 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
29b33 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
29b34 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44 62 5b  Tbls = &db->aDb[
29b35 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  i].pSchema->tblH
29b36 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72 28 78  ash;.      for(x
29b37 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
29b38 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d 73 71  (pTbls); x; x=sq
29b39 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78 29 29  liteHashNext(x))
29b3a 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20  {.        Table 
29b3b 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61  *pTab = sqliteHa
29b3c 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20 20  shData(x);.     
29b3d 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
29b3e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29b3f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29b40 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d 3e 74  Integer, pTab->t
29b41 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20  num, 2+cnt);.   
29b42 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
29b43 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
29b44 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
29b45 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
29b46 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t){.          sq
29b47 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29b48 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
29b49 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74  Idx->tnum, 2+cnt
29b4a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74  );.          cnt
29b4b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
29b4c 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
29b4d 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66 69 63  Make sure suffic
29b4e 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 72  ient number of r
29b4f 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65  egisters have be
29b50 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
29b51 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
29b52 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34 20 29  ->nMem < cnt+4 )
29b53 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  {.        pParse
29b54 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b 0a  ->nMem = cnt+4;.
29b55 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
29b56 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65 65 20  * Do the b-tree 
29b57 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 73  integrity checks
29b58 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
29b59 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
29b5a 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c 20 32  P_IntegrityCk, 2
29b5b 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20  , cnt, 1);.     
29b5c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29b5d 67 65 50 35 28 76 2c 20 28 75 38 29 69 29 3b 0a  geP5(v, (u8)i);.
29b5e 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29b5f 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29b60 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b  , OP_IsNull, 2);
29b61 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29b62 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29b63 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
29b64 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
29b65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a  3MPrintf(db, "**
29b66 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73  * in database %s
29b67 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62   ***\n", db->aDb
29b68 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20  [i].zName),.    
29b69 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43 29       P4_DYNAMIC)
29b6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29b6b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29b6c 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29 3b 0a  Move, 2, 4, 1);.
29b6d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29b6e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
29b6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a  ncat, 4, 3, 2);.
29b70 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29b71 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
29b72 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a  sultRow, 2, 1);.
29b73 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29b74 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
29b75 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61  r);..      /* Ma
29b76 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20  ke sure all the 
29b77 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73  indices are cons
29b78 74 72 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c  tructed correctl
29b79 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  y..      */.    
29b7a 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61    for(x=sqliteHa
29b7b 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20  shFirst(pTbls); 
29b7c 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20 78  x && !isQuick; x
29b7d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
29b7e 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  x)){.        Tab
29b7f 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
29b80 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20  eHashData(x);.  
29b81 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
29b82 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  x;.        int l
29b83 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20  oopTop;..       
29b84 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65   if( pTab->pInde
29b85 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  x==0 ) continue;
29b86 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20  .        addr = 
29b87 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29b88 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31  1(v, OP_IfPos, 1
29b89 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f  );  /* Stop if o
29b8a 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a  ut of errors */.
29b8b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29b8c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29b8d 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
29b8e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29b8f 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
29b90 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29b91 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
29b92 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54  dices(pParse, pT
29b93 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65  ab, 1, OP_OpenRe
29b94 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ad);.        sql
29b95 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29b96 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
29b97 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29 20   2);  /* reg(2) 
29b98 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69  will count entri
29b99 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f  es */.        lo
29b9a 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  opTop = sqlite3V
29b9b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29b9c 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20  Rewind, 1, 0);. 
29b9d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29b9e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
29b9f 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20  ddImm, 2, 1);   
29ba0 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74  /* increment ent
29ba1 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  ry count */.    
29ba2 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64      for(j=0, pId
29ba3 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
29ba4 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
29ba5 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
29ba6 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70 32          int jmp2
29ba7 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
29ba8 72 31 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74  r1;.          st
29ba9 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
29baa 70 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d  pList idxErr[] =
29bab 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b   {.            {
29bac 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
29bad 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20   1, -1,  0},.   
29bae 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
29baf 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 33  ring8,     0,  3
29bb0 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a  ,  0},    /* 1 *
29bb1 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
29bb2 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20 20  OP_Rowid,       
29bb3 31 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20  1,  4,  0},.    
29bb4 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
29bb5 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 35 2c  ing8,     0,  5,
29bb6 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f    0},    /* 3 */
29bb7 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
29bb8 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30  P_String8,     0
29bb9 2c 20 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  6,  0},    /*
29bba 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   4 */.          
29bbb 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20    { OP_Concat,  
29bbc 20 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a      4,  3,  3},.
29bbd 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29bbe 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c  _Concat,      5,
29bbf 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20    3,  3},.      
29bc0 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
29bc1 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20 20  t,      6,  3,  
29bc2 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  3},.            
29bc3 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
29bc4 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20    3,  1,  0},.  
29bc5 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
29bc6 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 20  fPos,       1,  
29bc7 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20  0,  0},    /* 9 
29bc8 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
29bc9 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20   OP_Halt,       
29bca 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20   0,  0,  0},.   
29bcb 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20         };.      
29bcc 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
29bcd 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
29bce 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 31  (pParse, pIdx, 1
29bcf 2c 20 33 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 3, 0);.       
29bd0 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69 74 65     jmp2 = sqlite
29bd1 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76  3VdbeAddOp4Int(v
29bd2 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c  , OP_Found, j+2,
29bd3 20 30 2c 20 72 31 2c 20 70 49 64 78 2d 3e 6e 43   0, r1, pIdx->nC
29bd4 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 20 20  olumn+1);.      
29bd5 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
29bd6 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
29bd7 76 2c 20 41 72 72 61 79 53 69 7a 65 28 69 64 78  v, ArraySize(idx
29bd8 45 72 72 29 2c 20 69 64 78 45 72 72 29 3b 0a 20  Err), idxErr);. 
29bd9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29bda 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
29bdb 61 64 64 72 2b 31 2c 20 22 72 6f 77 69 64 20 22  addr+1, "rowid "
29bdc 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
29bdd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29bde 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61  dbeChangeP4(v, a
29bdf 64 64 72 2b 33 2c 20 22 20 6d 69 73 73 69 6e 67  ddr+3, " missing
29be0 20 66 72 6f 6d 20 69 6e 64 65 78 20 22 2c 20 50   from index ", P
29be1 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
29be2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29be3 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
29be4 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c  +4, pIdx->zName,
29be5 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
29be6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29be7 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
29be8 64 72 2b 39 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+9);.         
29be9 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
29bea 48 65 72 65 28 76 2c 20 6a 6d 70 32 29 3b 0a 20  Here(v, jmp2);. 
29beb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
29bec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29bed 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 31  p2(v, OP_Next, 1
29bee 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a 20 20  , loopTop+1);.  
29bef 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29bf0 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6c 6f 6f  eJumpHere(v, loo
29bf1 70 54 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 66  pTop);.        f
29bf2 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(j=0, pIdx=pTa
29bf3 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
29bf4 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
29bf5 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  t, j++){.       
29bf6 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
29bf7 56 64 62 65 4f 70 4c 69 73 74 20 63 6e 74 49 64  VdbeOpList cntId
29bf8 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  x[] = {.        
29bf9 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65       { OP_Intege
29bfa 72 2c 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20  r,      0,  3,  
29bfb 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
29bfc 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20   { OP_Rewind,   
29bfd 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20      0,  0,  0}, 
29bfe 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20   /* 1 */.       
29bff 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d        { OP_AddIm
29c00 6d 2c 20 20 20 20 20 20 20 33 2c 20 20 31 2c 20  m,       3,  1, 
29c01 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
29c02 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20    { OP_Next,    
29c03 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c       0,  0,  0},
29c04 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20    /* 3 */.      
29c05 20 20 20 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20         { OP_Eq, 
29c06 20 20 20 20 20 20 20 20 20 20 32 2c 20 20 30 2c            2,  0,
29c07 20 20 33 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20    3},  /* 4 */. 
29c08 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29c09 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 31  _AddImm,       1
29c0a 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  , -1,  0},.     
29c0b 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
29c0c 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20 20 32  ing8,      0,  2
29c0d 2c 20 20 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a  ,  0},  /* 6 */.
29c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
29c0f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20  P_String8,      
29c10 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20  0,  3,  0},  /* 
29c11 37 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  7 */.           
29c12 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20    { OP_Concat,  
29c13 20 20 20 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c       3,  2,  2},
29c14 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
29c15 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20  OP_ResultRow,   
29c16 20 32 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20   2,  1,  0},.   
29c17 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20         };.      
29c18 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
29c19 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
29c1a 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 0a 20 20  OP_IfPos, 1);.  
29c1b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29c1c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29c1d 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
29c1e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29c1f 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
29c20 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  dr);.          a
29c21 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
29c22 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
29c23 72 61 79 53 69 7a 65 28 63 6e 74 49 64 78 29 2c  raySize(cntIdx),
29c24 20 63 6e 74 49 64 78 29 3b 0a 20 20 20 20 20 20   cntIdx);.      
29c25 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29c26 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
29c27 31 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20  1, j+2);.       
29c28 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29c29 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 31  angeP2(v, addr+1
29c2a 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20  , addr+4);.     
29c2b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29c2c 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
29c2d 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20  +3, j+2);.      
29c2e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29c2f 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b  hangeP2(v, addr+
29c30 33 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20  3, addr+2);.    
29c31 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29c32 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
29c33 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
29c34 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29c35 65 50 34 28 76 2c 20 61 64 64 72 2b 36 2c 20 0a  eP4(v, addr+6, .
29c36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c37 20 20 20 20 20 22 77 72 6f 6e 67 20 23 20 6f 66       "wrong # of
29c38 20 65 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65   entries in inde
29c39 78 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  x ", P4_STATIC);
29c3a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
29c3b 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
29c3c 2c 20 61 64 64 72 2b 37 2c 20 70 49 64 78 2d 3e  , addr+7, pIdx->
29c3d 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
29c3e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
29c3f 20 20 20 7d 20 0a 20 20 20 20 7d 0a 20 20 20 20     } .    }.    
29c40 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
29c41 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
29c42 72 72 61 79 53 69 7a 65 28 65 6e 64 43 6f 64 65  rraySize(endCode
29c43 29 2c 20 65 6e 64 43 6f 64 65 29 3b 0a 20 20 20  ), endCode);.   
29c44 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29c45 67 65 50 32 28 76 2c 20 61 64 64 72 2c 20 2d 6d  geP2(v, addr, -m
29c46 78 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  xErr);.    sqlit
29c47 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
29c48 2c 20 61 64 64 72 2b 31 29 3b 0a 20 20 20 20 73  , addr+1);.    s
29c49 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29c4a 50 34 28 76 2c 20 61 64 64 72 2b 32 2c 20 22 6f  P4(v, addr+2, "o
29c4b 6b 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  k", P4_STATIC);.
29c4c 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
29c4d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
29c4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f  TEGRITY_CHECK */
29c4f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
29c50 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a  _OMIT_UTF16.  /*
29c51 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 65  .  **   PRAGMA e
29c52 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 20 20 50  ncoding.  **   P
29c53 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d  RAGMA encoding =
29c54 20 22 75 74 66 2d 38 22 7c 22 75 74 66 2d 31 36   "utf-8"|"utf-16
29c55 22 7c 22 75 74 66 2d 31 36 6c 65 22 7c 22 75 74  "|"utf-16le"|"ut
29c56 66 2d 31 36 62 65 22 0a 20 20 2a 2a 0a 20 20 2a  f-16be".  **.  *
29c57 2a 20 49 6e 20 69 74 73 20 66 69 72 73 74 20 66  * In its first f
29c58 6f 72 6d 2c 20 74 68 69 73 20 70 72 61 67 6d 61  orm, this pragma
29c59 20 72 65 74 75 72 6e 73 20 74 68 65 20 65 6e 63   returns the enc
29c5a 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69  oding of the mai
29c5b 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  n.  ** database.
29c5c 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
29c5d 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69   is not initiali
29c5e 7a 65 64 2c 20 69 74 20 69 73 20 69 6e 69 74 69  zed, it is initi
29c5f 61 6c 69 7a 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a  alized now..  **
29c60 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  .  ** The second
29c61 20 66 6f 72 6d 20 6f 66 20 74 68 69 73 20 70 72   form of this pr
29c62 61 67 6d 61 20 69 73 20 61 20 6e 6f 2d 6f 70 20  agma is a no-op 
29c63 69 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  if the main data
29c64 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 68  base file.  ** h
29c65 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62  as not already b
29c66 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  een initialized.
29c67 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   In this case it
29c68 20 73 65 74 73 20 74 68 65 20 64 65 66 61 75 6c   sets the defaul
29c69 74 0a 20 20 2a 2a 20 65 6e 63 6f 64 69 6e 67 20  t.  ** encoding 
29c6a 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65  that will be use
29c6b 64 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  d for the main d
29c6c 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
29c6d 61 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20  a new file.  ** 
29c6e 69 73 20 63 72 65 61 74 65 64 2e 20 49 66 20 61  is created. If a
29c6f 6e 20 65 78 69 73 74 69 6e 67 20 6d 61 69 6e 20  n existing main 
29c70 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
29c71 20 6f 70 65 6e 65 64 2c 20 74 68 65 6e 20 74 68   opened, then th
29c72 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 74  e.  ** default t
29c73 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ext encoding for
29c74 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61   the existing da
29c75 74 61 62 61 73 65 20 69 73 20 75 73 65 64 2e 0a  tabase is used..
29c76 20 20 2a 2a 20 0a 20 20 2a 2a 20 49 6e 20 61 6c    ** .  ** In al
29c77 6c 20 63 61 73 65 73 20 6e 65 77 20 64 61 74 61  l cases new data
29c78 62 61 73 65 73 20 63 72 65 61 74 65 64 20 75 73  bases created us
29c79 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63  ing the ATTACH c
29c7a 6f 6d 6d 61 6e 64 20 61 72 65 0a 20 20 2a 2a 20  ommand are.  ** 
29c7b 63 72 65 61 74 65 64 20 74 6f 20 75 73 65 20 74  created to use t
29c7c 68 65 20 73 61 6d 65 20 64 65 66 61 75 6c 74 20  he same default 
29c7d 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73  text encoding as
29c7e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
29c7f 73 65 2e 20 49 66 0a 20 20 2a 2a 20 74 68 65 20  se. If.  ** the 
29c80 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 68 61  main database ha
29c81 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74 69  s not been initi
29c82 61 6c 69 7a 65 64 20 61 6e 64 2f 6f 72 20 63 72  alized and/or cr
29c83 65 61 74 65 64 20 77 68 65 6e 20 41 54 54 41 43  eated when ATTAC
29c84 48 0a 20 20 2a 2a 20 69 73 20 65 78 65 63 75 74  H.  ** is execut
29c85 65 64 2c 20 74 68 69 73 20 69 73 20 64 6f 6e 65  ed, this is done
29c86 20 62 65 66 6f 72 65 20 74 68 65 20 41 54 54 41   before the ATTA
29c87 43 48 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  CH operation..  
29c88 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74 68 65 20 73  **.  ** In the s
29c89 65 63 6f 6e 64 20 66 6f 72 6d 20 74 68 69 73 20  econd form this 
29c8a 70 72 61 67 6d 61 20 73 65 74 73 20 74 68 65 20  pragma sets the 
29c8b 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 74 6f  text encoding to
29c8c 20 62 65 20 75 73 65 64 20 69 6e 0a 20 20 2a 2a   be used in.  **
29c8d 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 66 69   new database fi
29c8e 6c 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  les created usin
29c8f 67 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  g this database 
29c90 68 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6f 6e  handle. It is on
29c91 6c 79 0a 20 20 2a 2a 20 75 73 65 66 75 6c 20 69  ly.  ** useful i
29c92 66 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69  f invoked immedi
29c93 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20  ately after the 
29c94 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 0a  main database i.
29c95 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
29c96 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29c97 20 22 65 6e 63 6f 64 69 6e 67 22 29 3d 3d 30 20   "encoding")==0 
29c98 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
29c99 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61  nst struct EncNa
29c9a 6d 65 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20  me {.      char 
29c9b 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 75 38  *zName;.      u8
29c9c 20 65 6e 63 3b 0a 20 20 20 20 7d 20 65 6e 63 6e   enc;.    } encn
29c9d 61 6d 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  ames[] = {.     
29c9e 20 7b 20 22 55 54 46 38 22 2c 20 20 20 20 20 53   { "UTF8",     S
29c9f 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20  QLITE_UTF8      
29ca0 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
29ca1 46 2d 38 22 2c 20 20 20 20 53 51 4c 49 54 45 5f  F-8",    SQLITE_
29ca2 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c 20 20  UTF8        },  
29ca3 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65  /* Must be eleme
29ca4 6e 74 20 5b 31 5d 20 2a 2f 0a 20 20 20 20 20 20  nt [1] */.      
29ca5 7b 20 22 55 54 46 2d 31 36 6c 65 22 2c 20 53 51  { "UTF-16le", SQ
29ca6 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
29ca7 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20   },  /* Must be 
29ca8 65 6c 65 6d 65 6e 74 20 5b 32 5d 20 2a 2f 0a 20  element [2] */. 
29ca9 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65       { "UTF-16be
29caa 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  ", SQLITE_UTF16B
29cab 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73  E     },  /* Mus
29cac 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 33 5d  t be element [3]
29cad 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46   */.      { "UTF
29cae 31 36 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f 55  16le",  SQLITE_U
29caf 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20  TF16LE     },.  
29cb0 20 20 20 20 7b 20 22 55 54 46 31 36 62 65 22 2c      { "UTF16be",
29cb1 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45    SQLITE_UTF16BE
29cb2 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20       },.      { 
29cb3 22 55 54 46 2d 31 36 22 2c 20 20 20 30 20 20 20  "UTF-16",   0   
29cb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
29cb5 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31  , /* SQLITE_UTF1
29cb6 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20  6NATIVE */.     
29cb7 20 7b 20 22 55 54 46 31 36 22 2c 20 20 20 20 30   { "UTF16",    0
29cb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29cb9 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55    }, /* SQLITE_U
29cba 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20  TF16NATIVE */.  
29cbb 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 20      { 0, 0 }.   
29cbc 20 7d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74   };.    const st
29cbd 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 2a 70 45  ruct EncName *pE
29cbe 6e 63 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  nc;.    if( !zRi
29cbf 67 68 74 20 29 7b 20 20 20 20 2f 2a 20 22 50 52  ght ){    /* "PR
29cc0 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 22 20 2a  AGMA encoding" *
29cc1 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  /.      if( sqli
29cc2 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
29cc3 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
29cc4 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 73  gma_out;.      s
29cc5 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
29cc6 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
29cc7 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29cc8 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
29cc9 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 65 6e 63  LNAME_NAME, "enc
29cca 6f 64 69 6e 67 22 2c 20 53 51 4c 49 54 45 5f 53  oding", SQLITE_S
29ccb 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
29ccc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ccd 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
29cce 2c 20 31 29 3b 0a 20 20 20 20 20 20 61 73 73 65  , 1);.      asse
29ccf 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c  rt( encnames[SQL
29cd0 49 54 45 5f 55 54 46 38 5d 2e 65 6e 63 3d 3d 53  ITE_UTF8].enc==S
29cd1 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20  QLITE_UTF8 );.  
29cd2 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e      assert( encn
29cd3 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 31  ames[SQLITE_UTF1
29cd4 36 4c 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45  6LE].enc==SQLITE
29cd5 5f 55 54 46 31 36 4c 45 20 29 3b 0a 20 20 20 20  _UTF16LE );.    
29cd6 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d    assert( encnam
29cd7 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 42  es[SQLITE_UTF16B
29cd8 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  E].enc==SQLITE_U
29cd9 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20  TF16BE );.      
29cda 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29cdb 65 50 34 28 76 2c 20 2d 31 2c 20 65 6e 63 6e 61  eP4(v, -1, encna
29cdc 6d 65 73 5b 45 4e 43 28 70 50 61 72 73 65 2d 3e  mes[ENC(pParse->
29cdd 64 62 29 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53  db)].zName, P4_S
29cde 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
29cdf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ce0 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
29ce1 20 31 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73   1, 1);.    }els
29ce2 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e{              
29ce3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22 50 52            /* "PR
29ce4 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20  AGMA encoding = 
29ce5 58 58 58 22 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  XXX" */.      /*
29ce6 20 4f 6e 6c 79 20 63 68 61 6e 67 65 20 74 68 65   Only change the
29ce7 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65   value of sqlite
29ce8 2e 65 6e 63 20 69 66 20 74 68 65 20 64 61 74 61  .enc if the data
29ce9 62 61 73 65 20 68 61 6e 64 6c 65 20 69 73 20 6e  base handle is n
29cea 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 74  ot.      ** init
29ceb 69 61 6c 69 7a 65 64 2e 20 49 66 20 74 68 65 20  ialized. If the 
29cec 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 78  main database ex
29ced 69 73 74 73 2c 20 74 68 65 20 6e 65 77 20 73 71  ists, the new sq
29cee 6c 69 74 65 2e 65 6e 63 20 76 61 6c 75 65 0a 20  lite.enc value. 
29cef 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20       ** will be 
29cf0 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e  overwritten when
29cf1 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e   the schema is n
29cf2 65 78 74 20 6c 6f 61 64 65 64 2e 20 49 66 20 69  ext loaded. If i
29cf3 74 20 64 6f 65 73 20 6e 6f 74 0a 20 20 20 20 20  t does not.     
29cf4 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73   ** already exis
29cf5 74 73 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 63  ts, it will be c
29cf6 72 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68  reated to use th
29cf7 65 20 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20 76  e new encoding v
29cf8 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  alue..      */. 
29cf9 20 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20       if( .      
29cfa 20 20 21 28 44 62 48 61 73 50 72 6f 70 65 72 74    !(DbHasPropert
29cfb 79 28 64 62 2c 20 30 2c 20 44 42 5f 53 63 68 65  y(db, 0, DB_Sche
29cfc 6d 61 4c 6f 61 64 65 64 29 29 20 7c 7c 20 0a 20  maLoaded)) || . 
29cfd 20 20 20 20 20 20 20 44 62 48 61 73 50 72 6f 70         DbHasProp
29cfe 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 45  erty(db, 0, DB_E
29cff 6d 70 74 79 29 20 0a 20 20 20 20 20 20 29 7b 0a  mpty) .      ){.
29d00 20 20 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63          for(pEnc
29d01 3d 26 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70  =&encnames[0]; p
29d02 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63  Enc->zName; pEnc
29d03 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
29d04 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
29d05 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 70 45 6e  ICmp(zRight, pEn
29d06 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  c->zName) ){.   
29d07 20 20 20 20 20 20 20 20 20 45 4e 43 28 70 50 61           ENC(pPa
29d08 72 73 65 2d 3e 64 62 29 20 3d 20 70 45 6e 63 2d  rse->db) = pEnc-
29d09 3e 65 6e 63 20 3f 20 70 45 6e 63 2d 3e 65 6e 63  >enc ? pEnc->enc
29d0a 20 3a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e   : SQLITE_UTF16N
29d0b 41 54 49 56 45 3b 0a 20 20 20 20 20 20 20 20 20  ATIVE;.         
29d0c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
29d0d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29d0e 20 20 20 20 20 20 20 20 69 66 28 20 21 70 45 6e          if( !pEn
29d0f 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  c->zName ){.    
29d10 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
29d11 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
29d12 6e 73 75 70 70 6f 72 74 65 64 20 65 6e 63 6f 64  nsupported encod
29d13 69 6e 67 3a 20 25 73 22 2c 20 7a 52 69 67 68 74  ing: %s", zRight
29d14 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
29d15 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
29d16 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
29d17 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
29d18 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
29d19 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45  E_OMIT_SCHEMA_VE
29d1a 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 0a 20 20  RSION_PRAGMAS.  
29d1b 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
29d1c 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65   [database.]sche
29d1d 6d 61 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20  ma_version.  ** 
29d1e 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29d1f 73 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69  se.]schema_versi
29d20 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20  on = <integer>. 
29d21 20 2a 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   **.  **   PRAGM
29d22 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 75 73 65  A [database.]use
29d23 72 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20  r_version.  **  
29d24 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
29d25 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 20  e.]user_version 
29d26 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a  = <integer>.  **
29d27 0a 20 20 2a 2a 20 54 68 65 20 70 72 61 67 6d 61  .  ** The pragma
29d28 27 73 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  's schema_versio
29d29 6e 20 61 6e 64 20 75 73 65 72 5f 76 65 72 73 69  n and user_versi
29d2a 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 73  on are used to s
29d2b 65 74 20 6f 72 20 67 65 74 0a 20 20 2a 2a 20 74  et or get.  ** t
29d2c 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
29d2d 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61  schema-version a
29d2e 6e 64 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 2c  nd user-version,
29d2f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 42   respectively. B
29d30 6f 74 68 0a 20 20 2a 2a 20 74 68 65 20 73 63 68  oth.  ** the sch
29d31 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20  ema-version and 
29d32 74 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e  the user-version
29d33 20 61 72 65 20 33 32 2d 62 69 74 20 73 69 67 6e   are 32-bit sign
29d34 65 64 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a  ed integers.  **
29d35 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
29d36 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e 0a  atabase header..
29d37 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 63    **.  ** The sc
29d38 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75  hema-cookie is u
29d39 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69  sually only mani
29d3a 70 75 6c 61 74 65 64 20 69 6e 74 65 72 6e 61 6c  pulated internal
29d3b 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74  ly by SQLite. It
29d3c 0a 20 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d 65  .  ** is increme
29d3d 6e 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77  nted by SQLite w
29d3e 68 65 6e 65 76 65 72 20 74 68 65 20 64 61 74 61  henever the data
29d3f 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 6d  base schema is m
29d40 6f 64 69 66 69 65 64 20 28 62 79 0a 20 20 2a 2a  odified (by.  **
29d41 20 63 72 65 61 74 69 6e 67 20 6f 72 20 64 72 6f   creating or dro
29d42 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20 6f 72  pping a table or
29d43 20 69 6e 64 65 78 29 2e 20 54 68 65 20 73 63 68   index). The sch
29d44 65 6d 61 20 76 65 72 73 69 6f 6e 20 69 73 20 75  ema version is u
29d45 73 65 64 20 62 79 0a 20 20 2a 2a 20 53 51 4c 69  sed by.  ** SQLi
29d46 74 65 20 65 61 63 68 20 74 69 6d 65 20 61 20 71  te each time a q
29d47 75 65 72 79 20 69 73 20 65 78 65 63 75 74 65 64  uery is executed
29d48 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
29d49 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63  the internal cac
29d4a 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73  he.  ** of the s
29d4b 63 68 65 6d 61 20 75 73 65 64 20 77 68 65 6e 20  chema used when 
29d4c 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51  compiling the SQ
29d4d 4c 20 71 75 65 72 79 20 6d 61 74 63 68 65 73 20  L query matches 
29d4e 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 20 20  the schema of.  
29d4f 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
29d50 61 67 61 69 6e 73 74 20 77 68 69 63 68 20 74 68  against which th
29d51 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79  e compiled query
29d52 20 69 73 20 61 63 74 75 61 6c 6c 79 20 65 78 65   is actually exe
29d53 63 75 74 65 64 2e 0a 20 20 2a 2a 20 53 75 62 76  cuted..  ** Subv
29d54 65 72 74 69 6e 67 20 74 68 69 73 20 6d 65 63 68  erting this mech
29d55 61 6e 69 73 6d 20 62 79 20 75 73 69 6e 67 20 22  anism by using "
29d56 50 52 41 47 4d 41 20 73 63 68 65 6d 61 5f 76 65  PRAGMA schema_ve
29d57 72 73 69 6f 6e 22 20 74 6f 20 6d 6f 64 69 66 79  rsion" to modify
29d58 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
29d59 2d 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 74 65  -version is pote
29d5a 6e 74 69 61 6c 6c 79 20 64 61 6e 67 65 72 6f 75  ntially dangerou
29d5b 73 20 61 6e 64 20 6d 61 79 20 6c 65 61 64 20 74  s and may lead t
29d5c 6f 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 63  o program.  ** c
29d5d 72 61 73 68 65 73 20 6f 72 20 64 61 74 61 62 61  rashes or databa
29d5e 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 55  se corruption. U
29d5f 73 65 20 77 69 74 68 20 63 61 75 74 69 6f 6e 21  se with caution!
29d60 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 75  .  **.  ** The u
29d61 73 65 72 2d 76 65 72 73 69 6f 6e 20 69 73 20 6e  ser-version is n
29d62 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  ot used internal
29d63 6c 79 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74  ly by SQLite. It
29d64 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 0a   may be used by.
29d65 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e    ** application
29d66 73 20 66 6f 72 20 61 6e 79 20 70 75 72 70 6f 73  s for any purpos
29d67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
29d68 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
29d69 66 74 2c 20 22 73 63 68 65 6d 61 5f 76 65 72 73  ft, "schema_vers
29d6a 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20  ion")==0 .   || 
29d6b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29d6c 4c 65 66 74 2c 20 22 75 73 65 72 5f 76 65 72 73  Left, "user_vers
29d6d 69 6f 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20  ion")==0 .   || 
29d6e 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29d6f 4c 65 66 74 2c 20 22 66 72 65 65 6c 69 73 74 5f  Left, "freelist_
29d70 63 6f 75 6e 74 22 29 3d 3d 30 20 0a 20 20 29 7b  count")==0 .  ){
29d71 0a 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65  .    int iCookie
29d72 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69 6e  ;   /* Cookie in
29d73 64 65 78 2e 20 31 20 66 6f 72 20 73 63 68 65 6d  dex. 1 for schem
29d74 61 2d 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72 20  a-cookie, 6 for 
29d75 75 73 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f 0a  user-cookie. */.
29d76 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
29d77 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
29d78 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 7a 4c  ;.    switch( zL
29d79 65 66 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20  eft[0] ){.      
29d7a 63 61 73 65 20 27 66 27 3a 20 63 61 73 65 20 27  case 'f': case '
29d7b 46 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f  F':.        iCoo
29d7c 6b 69 65 20 3d 20 42 54 52 45 45 5f 46 52 45 45  kie = BTREE_FREE
29d7d 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20  _PAGE_COUNT;.   
29d7e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29d7f 20 20 63 61 73 65 20 27 73 27 3a 20 63 61 73 65    case 's': case
29d80 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 69 43   'S':.        iC
29d81 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f 53 43  ookie = BTREE_SC
29d82 48 45 4d 41 5f 56 45 52 53 49 4f 4e 3b 0a 20 20  HEMA_VERSION;.  
29d83 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29d84 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
29d85 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54      iCookie = BT
29d86 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e  REE_USER_VERSION
29d87 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
29d88 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
29d89 7a 52 69 67 68 74 20 26 26 20 69 43 6f 6f 6b 69  zRight && iCooki
29d8a 65 21 3d 42 54 52 45 45 5f 46 52 45 45 5f 50 41  e!=BTREE_FREE_PA
29d8b 47 45 5f 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20  GE_COUNT ){.    
29d8c 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73    /* Write the s
29d8d 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20  pecified cookie 
29d8e 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73  value */.      s
29d8f 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
29d90 4f 70 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69 65  OpList setCookie
29d91 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b  [] = {.        {
29d92 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c   OP_Transaction,
29d93 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20      0,  1,  0}, 
29d94 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20     /* 0 */.     
29d95 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
29d96 20 20 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20          0,  1,  
29d97 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20  0},    /* 1 */. 
29d98 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43         { OP_SetC
29d99 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20  ookie,      0,  
29d9a 30 2c 20 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20  0,  1},    /* 2 
29d9b 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20  */.      };.    
29d9c 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c    int addr = sql
29d9d 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
29d9e 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73  t(v, ArraySize(s
29d9f 65 74 43 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f  etCookie), setCo
29da0 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  okie);.      sql
29da1 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
29da2 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a  (v, addr, iDb);.
29da3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29da4 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
29da5 72 2b 31 2c 20 61 74 6f 69 28 7a 52 69 67 68 74  r+1, atoi(zRight
29da6 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
29da7 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
29da8 20 61 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20   addr+2, iDb);. 
29da9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29daa 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
29dab 2b 32 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20  +2, iCookie);.  
29dac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
29dad 2a 20 52 65 61 64 20 74 68 65 20 73 70 65 63 69  * Read the speci
29dae 66 69 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75  fied cookie valu
29daf 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69  e */.      stati
29db0 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
29db1 73 74 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20  st readCookie[] 
29db2 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50  = {.        { OP
29db3 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20  _Transaction,   
29db4 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20    0,  0,  0},   
29db5 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20   /* 0 */.       
29db6 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65   { OP_ReadCookie
29db7 2c 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30  ,      0,  1,  0
29db8 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20  },    /* 1 */.  
29db9 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
29dba 74 52 6f 77 2c 20 20 20 20 20 20 20 31 2c 20 20  tRow,       1,  
29dbb 31 2c 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a  1,  0}.      };.
29dbc 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
29dbd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29dbe 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
29dbf 7a 65 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20  ze(readCookie), 
29dc0 72 65 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  readCookie);.   
29dc1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29dc2 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20  angeP1(v, addr, 
29dc3 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
29dc4 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
29dc5 76 2c 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b  v, addr+1, iDb);
29dc6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29dc7 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64  beChangeP3(v, ad
29dc8 64 72 2b 31 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a  dr+1, iCookie);.
29dc9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29dca 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
29dcb 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29dcc 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29dcd 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
29dce 45 2c 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45  E, zLeft, SQLITE
29dcf 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
29dd0 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
29dd1 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
29dd2 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f  _SCHEMA_VERSION_
29dd3 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20  PRAGMAS */..#if 
29dd4 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
29dd5 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
29dd6 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20  (SQLITE_TEST).  
29dd7 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74  /*.  ** Report t
29dd8 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
29dd9 20 6f 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f   of file logs fo
29dda 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0a  r all databases.
29ddb 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
29ddc 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29ddd 20 22 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d   "lock_status")=
29dde 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  =0 ){.    static
29ddf 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
29de0 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20  st azLockName[] 
29de1 3d 20 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63  = {.      "unloc
29de2 6b 65 64 22 2c 20 22 73 68 61 72 65 64 22 2c 20  ked", "shared", 
29de3 22 72 65 73 65 72 76 65 64 22 2c 20 22 70 65 6e  "reserved", "pen
29de4 64 69 6e 67 22 2c 20 22 65 78 63 6c 75 73 69 76  ding", "exclusiv
29de5 65 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e  e".    };.    in
29de6 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t i;.    sqlite3
29de7 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
29de8 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 2);.    pParse
29de9 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20  ->nMem = 2;.    
29dea 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29deb 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29dec 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62  AME_NAME, "datab
29ded 61 73 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ase", SQLITE_STA
29dee 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
29def 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29df0 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
29df1 4d 45 2c 20 22 73 74 61 74 75 73 22 2c 20 53 51  ME, "status", SQ
29df2 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29df3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
29df4 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
29df5 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20    Btree *pBt;.  
29df6 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
29df7 72 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  r;.      const c
29df8 68 61 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75  har *zState = "u
29df9 6e 6b 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69  nknown";.      i
29dfa 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20  nt j;.      if( 
29dfb 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
29dfc 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
29dfd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29dfe 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
29dff 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20  ring8, 0, 1, 0, 
29e00 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
29e01 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
29e02 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44      pBt = db->aD
29e03 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
29e04 69 66 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70  if( pBt==0 || (p
29e05 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
29e06 74 72 65 65 50 61 67 65 72 28 70 42 74 29 29 3d  treePager(pBt))=
29e07 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53  =0 ){.        zS
29e08 74 61 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b  tate = "closed";
29e09 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
29e0a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
29e0b 6e 74 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62  ntrol(db, i ? db
29e0c 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a  ->aDb[i].zName :
29e0d 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
29e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e0f 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
29e10 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
29e11 2c 20 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  , &j)==SQLITE_OK
29e12 20 29 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74   ){.         zSt
29e13 61 74 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65  ate = azLockName
29e14 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [j];.      }.   
29e15 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29e16 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
29e17 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74  g8, 0, 2, 0, zSt
29e18 61 74 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ate, P4_STATIC);
29e19 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29e1a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
29e1b 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b  esultRow, 1, 2);
29e1c 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a  .    }..  }else.
29e1d 23 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49  #endif..#if SQLI
29e1e 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69  TE_HAS_CODEC.  i
29e1f 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
29e20 70 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d  p(zLeft, "key")=
29e21 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
29e22 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28      sqlite3_key(
29e23 64 62 2c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69  db, zRight, sqli
29e24 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67  te3Strlen30(zRig
29e25 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20  ht));.  }else.  
29e26 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
29e27 6d 70 28 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79  mp(zLeft, "rekey
29e28 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
29e29 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
29e2a 65 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c  ekey(db, zRight,
29e2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
29e2c 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c  (zRight));.  }el
29e2d 73 65 0a 20 20 69 66 28 20 7a 52 69 67 68 74 20  se.  if( zRight 
29e2e 26 26 20 28 73 71 6c 69 74 65 33 53 74 72 49 43  && (sqlite3StrIC
29e2f 6d 70 28 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65  mp(zLeft, "hexke
29e30 79 22 29 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20  y")==0 ||.      
29e31 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
29e32 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29e33 20 22 68 65 78 72 65 6b 65 79 22 29 3d 3d 30 29   "hexrekey")==0)
29e34 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 68   ){.    int i, h
29e35 31 2c 20 68 32 3b 0a 20 20 20 20 63 68 61 72 20  1, h2;.    char 
29e36 7a 4b 65 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f  zKey[40];.    fo
29e37 72 28 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69  r(i=0; (h1 = zRi
29e38 67 68 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68  ght[i])!=0 && (h
29e39 32 20 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29  2 = zRight[i+1])
29e3a 21 3d 30 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20  !=0; i+=2){.    
29e3b 20 20 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31    h1 += 9*(1&(h1
29e3c 3e 3e 36 29 29 3b 0a 20 20 20 20 20 20 68 32 20  >>6));.      h2 
29e3d 2b 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29  += 9*(1&(h2>>6))
29e3e 3b 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32  ;.      zKey[i/2
29e3f 5d 20 3d 20 28 68 32 20 26 20 30 78 30 66 29 20  ] = (h2 & 0x0f) 
29e40 7c 20 28 28 68 31 20 26 20 30 78 66 29 3c 3c 34  | ((h1 & 0xf)<<4
29e41 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
29e42 20 28 7a 4c 65 66 74 5b 33 5d 20 26 20 30 78 66   (zLeft[3] & 0xf
29e43 29 3d 3d 30 78 62 20 29 7b 0a 20 20 20 20 20 20  )==0xb ){.      
29e44 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20  sqlite3_key(db, 
29e45 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20  zKey, i/2);.    
29e46 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
29e47 69 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a  ite3_rekey(db, z
29e48 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d  Key, i/2);.    }
29e49 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
29e4a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  #if SQLITE_HAS_C
29e4b 4f 44 45 43 20 7c 7c 20 64 65 66 69 6e 65 64 28  ODEC || defined(
29e4c 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45  SQLITE_ENABLE_CE
29e4d 52 4f 44 29 0a 20 20 69 66 28 20 73 71 6c 69 74  ROD).  if( sqlit
29e4e 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29e4f 20 22 61 63 74 69 76 61 74 65 5f 65 78 74 65 6e   "activate_exten
29e50 73 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69  sions")==0 ){.#i
29e51 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
29e52 45 43 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  EC.    if( sqlit
29e53 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68  e3StrNICmp(zRigh
29e54 74 2c 20 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30  t, "see-", 4)==0
29e55 20 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e   ){.      extern
29e56 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63   void sqlite3_ac
29e57 74 69 76 61 74 65 5f 73 65 65 28 63 6f 6e 73 74  tivate_see(const
29e58 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73   char*);.      s
29e59 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f  qlite3_activate_
29e5a 73 65 65 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b  see(&zRight[4]);
29e5b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69  .    }.#endif.#i
29e5c 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
29e5d 4c 45 5f 43 45 52 4f 44 0a 20 20 20 20 69 66 28  LE_CEROD.    if(
29e5e 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
29e5f 28 7a 52 69 67 68 74 2c 20 22 63 65 72 6f 64 2d  (zRight, "cerod-
29e60 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 6)==0 ){.    
29e61 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71    extern void sq
29e62 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63  lite3_activate_c
29e63 65 72 6f 64 28 63 6f 6e 73 74 20 63 68 61 72 2a  erod(const char*
29e64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29e65 5f 61 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28  _activate_cerod(
29e66 26 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20  &zRight[6]);.   
29e67 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73   }.#endif.  }els
29e68 65 0a 23 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f  e.#endif.. .  {/
29e69 2a 20 45 6d 70 74 79 20 45 4c 53 45 20 63 6c 61  * Empty ELSE cla
29e6a 75 73 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f  use */}..  /* Co
29e6b 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 20  de an OP_Expire 
29e6c 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61  at the end of ea
29e6d 63 68 20 50 52 41 47 4d 41 20 70 72 6f 67 72 61  ch PRAGMA progra
29e6e 6d 20 74 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20  m to cause.  ** 
29e6f 74 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65  the VDBE impleme
29e70 6e 74 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61  nting the pragma
29e71 20 74 6f 20 65 78 70 69 72 65 2e 20 4d 6f 73 74   to expire. Most
29e72 20 28 61 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a   (all?) pragmas.
29e73 20 20 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61    ** are only va
29e74 6c 69 64 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  lid for a single
29e75 20 65 78 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f   execution..  */
29e76 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
29e77 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72  dOp2(v, OP_Expir
29e78 65 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a  e, 1, 0);..  /*.
29e79 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73    ** Reset the s
29e7a 61 66 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20  afety level, in 
29e7b 63 61 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79  case the fullfsy
29e7c 6e 63 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68  nc flag or synch
29e7d 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74  ronous.  ** sett
29e7e 69 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a  ing changed..  *
29e7f 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
29e80 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
29e81 4d 41 53 0a 20 20 69 66 28 20 64 62 2d 3e 61 75  MAS.  if( db->au
29e82 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
29e83 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53  sqlite3BtreeSetS
29e84 61 66 65 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e  afetyLevel(pDb->
29e85 70 42 74 2c 20 70 44 62 2d 3e 73 61 66 65 74 79  pBt, pDb->safety
29e86 5f 6c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  _level,.        
29e87 20 20 20 20 20 20 20 28 64 62 2d 3e 66 6c 61 67         (db->flag
29e88 73 26 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79  s&SQLITE_FullFSy
29e89 6e 63 29 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e  nc)!=0);.  }.#en
29e8a 64 69 66 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a  dif.pragma_out:.
29e8b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29e8c 64 62 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71  db, zLeft);.  sq
29e8d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29e8e 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64  zRight);.}..#end
29e8f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
29e90 54 5f 50 52 41 47 4d 41 20 2a 2f 0a 0a 2f 2a 2a  T_PRAGMA */../**
29e91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
29e92 20 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a   of pragma.c ***
29e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29e95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
29e96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
29e97 69 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65 2e  in file prepare.
29e98 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
29e99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29e9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
29e9b 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 35 0a 2a  ** 2005 May 25.*
29e9c 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
29e9d 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
29e9e 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
29e9f 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
29ea0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
29ea1 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
29ea2 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
29ea3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
29ea4 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
29ea5 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
29ea6 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
29ea7 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
29ea8 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
29ea9 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
29eaa 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
29eab 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
29eac 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
29ead 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
29eae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29eaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29eb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
29eb2 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
29eb3 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
29eb4 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
29eb5 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 0a  lite3_prepare().
29eb6 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e  ** interface, an
29eb7 64 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  d routines that 
29eb8 63 6f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f  contribute to lo
29eb9 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
29eba 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f  se schema.** fro
29ebb 6d 20 64 69 73 6b 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  m disk..*/../*.*
29ebc 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74 44  * Fill the InitD
29ebd 61 74 61 20 73 74 72 75 63 74 75 72 65 20 77 69  ata structure wi
29ebe 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
29ebf 61 67 65 20 74 68 61 74 20 69 6e 64 69 63 61 74  age that indicat
29ec0 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64  es.** that the d
29ec1 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75  atabase is corru
29ec2 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  pt..*/.static vo
29ec3 69 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61  id corruptSchema
29ec4 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44  (.  InitData *pD
29ec5 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74  ata,     /* Init
29ec6 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65  ialization conte
29ec7 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
29ec8 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20  ar *zObj,    /* 
29ec9 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61 72  Object being par
29eca 73 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74  sed at the point
29ecb 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63   of error */.  c
29ecc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74 72  onst char *zExtr
29ecd 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66  a   /* Error inf
29ece 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  ormation */.){. 
29ecf 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
29ed0 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20  Data->db;.  if( 
29ed1 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
29ed2 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20  d && (db->flags 
29ed3 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  & SQLITE_Recover
29ed4 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20  yMode)==0 ){.   
29ed5 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a   if( zObj==0 ) z
29ed6 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73  Obj = "?";.    s
29ed7 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
29ed8 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
29ed9 20 64 62 2c 0a 20 20 20 20 20 20 22 6d 61 6c 66   db,.      "malf
29eda 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 73  ormed database s
29edb 63 68 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62  chema (%s)", zOb
29edc 6a 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74  j);.    if( zExt
29edd 72 61 20 29 7b 0a 20 20 20 20 20 20 2a 70 44 61  ra ){.      *pDa
29ede 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20 73  ta->pzErrMsg = s
29edf 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
29ee0 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72  b, *pData->pzErr
29ee1 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20  Msg, .          
29ee2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ee3 20 20 20 20 20 20 20 22 25 73 20 2d 20 25 73 22         "%s - %s"
29ee4 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d  , *pData->pzErrM
29ee5 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20 20 20  sg, zExtra);.   
29ee6 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61 2d 3e   }.  }.  pData->
29ee7 72 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rc = db->mallocF
29ee8 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45 5f 4e  ailed ? SQLITE_N
29ee9 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 43 4f  OMEM : SQLITE_CO
29eea 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  RRUPT;.}../*.** 
29eeb 54 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c  This is the call
29eec 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72  back routine for
29eed 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69   the code that i
29eee 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a  nitializes the.*
29eef 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65  * database.  See
29ef0 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62   sqlite3Init() b
29ef1 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f  elow for additio
29ef2 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
29ef3 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
29ef4 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
29ef5 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73  from the OP_Pars
29ef6 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f  eSchema opcode o
29ef7 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a  f the VDBE..**.*
29ef8 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20  * Each callback 
29ef9 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c  contains the fol
29efa 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  lowing informati
29efb 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72  on:.**.**     ar
29efc 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20  gv[0] = name of 
29efd 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61  thing being crea
29efe 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  ted.**     argv[
29eff 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e  1] = root page n
29f00 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
29f01 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20  or index. 0 for 
29f02 74 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e  trigger or view.
29f03 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20  .**     argv[2] 
29f04 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74  = SQL text for t
29f05 68 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d  he CREATE statem
29f06 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  ent..**.*/.SQLIT
29f07 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
29f08 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
29f09 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69  k(void *pInit, i
29f0a 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a  nt argc, char **
29f0b 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74  argv, char **Not
29f0c 55 73 65 64 29 7b 0a 20 20 49 6e 69 74 44 61 74  Used){.  InitDat
29f0d 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e 69 74  a *pData = (Init
29f0e 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73  Data*)pInit;.  s
29f0f 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61  qlite3 *db = pDa
29f10 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  ta->db;.  int iD
29f11 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a  b = pData->iDb;.
29f12 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
29f13 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =3 );.  UNUSED_P
29f14 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
29f15 64 2c 20 61 72 67 63 29 3b 0a 20 20 61 73 73 65  d, argc);.  asse
29f16 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
29f17 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
29f18 29 20 29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72  ) );.  DbClearPr
29f19 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20  operty(db, iDb, 
29f1a 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69 66 28  DB_Empty);.  if(
29f1b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
29f1c 64 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74  d ){.    corrupt
29f1d 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
29f1e 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72  gv[0], 0);.    r
29f1f 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20  eturn 1;.  }..  
29f20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
29f21 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
29f22 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 29  .  if( argv==0 )
29f23 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
29f24 4d 69 67 68 74 20 68 61 70 70 65 6e 20 69 66 20  Might happen if 
29f25 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43 41 4c  EMPTY_RESULT_CAL
29f26 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e 20 2a 2f  LBACKS are on */
29f27 0a 20 20 69 66 28 20 61 72 67 76 5b 31 5d 3d 3d  .  if( argv[1]==
29f28 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74  0 ){.    corrupt
29f29 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
29f2a 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d 65 6c  gv[0], 0);.  }el
29f2b 73 65 20 69 66 28 20 61 72 67 76 5b 32 5d 20 26  se if( argv[2] &
29f2c 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a  & argv[2][0] ){.
29f2d 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20      /* Call the 
29f2e 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63 65 73  parser to proces
29f2f 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  s a CREATE TABLE
29f30 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45 57 2e  , INDEX or VIEW.
29f31 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65 63 61  .    ** But beca
29f32 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  use db->init.bus
29f33 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 6e  y is set to 1, n
29f34 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73 20 67  o VDBE code is g
29f35 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a 2a 20  enerated.    ** 
29f36 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20 41 6c  or executed.  Al
29f37 6c 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 65  l the parser doe
29f38 73 20 69 73 20 62 75 69 6c 64 20 74 68 65 20 69  s is build the i
29f39 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20 20 20  nternal data.   
29f3a 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74   ** structures t
29f3b 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
29f3c 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f   table, index, o
29f3d 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f 0a 20  r view..    */. 
29f3e 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20     char *zErr;. 
29f3f 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61     int rc;.    a
29f40 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
29f41 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62 2d 3e  busy );.    db->
29f42 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62 3b 0a  init.iDb = iDb;.
29f43 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77      db->init.new
29f44 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72 67 76  Tnum = atoi(argv
29f45 5b 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e  [1]);.    db->in
29f46 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72  it.orphanTrigger
29f47 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73   = 0;.    rc = s
29f48 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
29f49 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c 20 26  argv[2], 0, 0, &
29f4a 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d 3e 69  zErr);.    db->i
29f4b 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20  nit.iDb = 0;.   
29f4c 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
29f4d 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d  ITE_OK || zErr==
29f4e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  0 );.    if( SQL
29f4f 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20  ITE_OK!=rc ){.  
29f50 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
29f51 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 29  .orphanTrigger )
29f52 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
29f53 28 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20  ( iDb==1 );.    
29f54 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
29f55 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b   pData->rc = rc;
29f56 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
29f57 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
29f58 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  .          db->m
29f59 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
29f5a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
29f5b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e  f( rc!=SQLITE_IN
29f5c 54 45 52 52 55 50 54 20 26 26 20 72 63 21 3d 53  TERRUPT && rc!=S
29f5d 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a  QLITE_LOCKED ){.
29f5e 20 20 20 20 20 20 20 20 20 20 63 6f 72 72 75 70            corrup
29f5f 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61  tSchema(pData, a
29f60 72 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20  rgv[0], zErr);. 
29f61 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
29f62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
29f63 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
29f64 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
29f65 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a  ( argv[0]==0 ){.
29f66 20 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d      corruptSchem
29f67 61 28 70 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a  a(pData, 0, 0);.
29f68 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
29f69 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d  If the SQL colum
29f6a 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65  n is blank it me
29f6b 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20 69  ans this is an i
29f6c 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20 2a 2a  ndex that.    **
29f6d 20 77 61 73 20 63 72 65 61 74 65 64 20 74 6f 20   was created to 
29f6e 62 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  be the PRIMARY K
29f6f 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c 6c  EY or to fulfill
29f70 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20 2a 2a   a UNIQUE.    **
29f71 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20   constraint for 
29f72 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20  a CREATE TABLE. 
29f73 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c   The index shoul
29f74 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20  d have already. 
29f75 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65 61 74     ** been creat
29f76 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f 63 65  ed when we proce
29f77 73 73 65 64 20 74 68 65 20 43 52 45 41 54 45 20  ssed the CREATE 
29f78 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65 20 68  TABLE.  All we h
29f79 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f  ave.    ** to do
29f7a 20 68 65 72 65 20 69 73 20 72 65 63 6f 72 64 20   here is record 
29f7b 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
29f7c 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 69 6e  mber for that in
29f7d 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
29f7e 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20  Index *pIndex;. 
29f7f 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69     pIndex = sqli
29f80 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
29f81 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e 61 44   argv[0], db->aD
29f82 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
29f83 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30     if( pIndex==0
29f84 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
29f85 73 20 63 61 6e 20 6f 63 63 75 72 20 69 66 20 74  s can occur if t
29f86 68 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 69  here exists an i
29f87 6e 64 65 78 20 6f 6e 20 61 20 54 45 4d 50 20 74  ndex on a TEMP t
29f88 61 62 6c 65 20 77 68 69 63 68 0a 20 20 20 20 20  able which.     
29f89 20 2a 2a 20 68 61 73 20 74 68 65 20 73 61 6d 65   ** has the same
29f8a 20 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68 65 72   name as another
29f8b 20 69 6e 64 65 78 20 6f 6e 20 61 20 70 65 72 6d   index on a perm
29f8c 61 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20 53 69  anent index.  Si
29f8d 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  nce.      ** the
29f8e 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
29f8f 20 69 73 20 68 69 64 64 65 6e 20 62 79 20 74 68   is hidden by th
29f90 65 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 77 65  e TEMP table, we
29f91 20 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20   can also.      
29f92 2a 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f 72 65  ** safely ignore
29f93 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 68   the index on th
29f94 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c  e permanent tabl
29f95 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
29f96 20 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20    /* Do Nothing 
29f97 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  */;.    }else if
29f98 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33  ( sqlite3GetInt3
29f99 32 28 61 72 67 76 5b 31 5d 2c 20 26 70 49 6e 64  2(argv[1], &pInd
29f9a 65 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29 7b 0a  ex->tnum)==0 ){.
29f9b 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68        corruptSch
29f9c 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
29f9d 30 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72 6f 6f  0], "invalid roo
29f9e 74 70 61 67 65 22 29 3b 0a 20 20 20 20 7d 0a 20  tpage");.    }. 
29f9f 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
29fa0 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
29fa1 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61  to read the data
29fa2 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20  base schema and 
29fa3 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72  initialize inter
29fa4 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75  nal.** data stru
29fa5 63 74 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e  ctures for a sin
29fa6 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
29fa7 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  e.  The index of
29fa8 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
29fa9 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
29faa 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69  y iDb.  iDb==0 i
29fab 73 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d  s used for the m
29fac 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e  ain.** database.
29fad 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20    iDb==1 should 
29fae 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20  never be used.  
29faf 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66  iDb>=2 is used f
29fb0 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  or.** auxiliary 
29fb1 64 61 74 61 62 61 73 65 73 2e 20 20 52 65 74 75  databases.  Retu
29fb2 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51  rn one of the SQ
29fb3 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65  LITE_ error code
29fb4 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65  s to.** indicate
29fb5 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
29fb6 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
29fb7 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e  nt sqlite3InitOn
29fb8 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
29fb9 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70  nt iDb, char **p
29fba 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
29fbb 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  rc;.  int i;.  i
29fbc 6e 74 20 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65  nt size;.  Table
29fbd 20 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44   *pTab;.  Db *pD
29fbe 62 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  b;.  char const 
29fbf 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74  *azArg[4];.  int
29fc0 20 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e 69 74   meta[5];.  Init
29fc1 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20  Data initData;. 
29fc2 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
29fc3 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68  sterSchema;.  ch
29fc4 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
29fc5 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54  rName = SCHEMA_T
29fc6 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 69 6e 74  ABLE(iDb);.  int
29fc7 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
29fc8 6f 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20  on = 0;..  /*.  
29fc9 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 64 61  ** The master da
29fca 74 61 62 61 73 65 20 74 61 62 6c 65 20 68 61 73  tabase table has
29fcb 20 61 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b   a structure lik
29fcc 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74  e this.  */.  st
29fcd 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
29fce 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
29fcf 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
29fd0 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73  TABLE sqlite_mas
29fd1 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20  ter(\n".     "  
29fd2 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  type text,\n".  
29fd3 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c     "  name text,
29fd4 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f  \n".     "  tbl_
29fd5 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
29fd6 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69     "  rootpage i
29fd7 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20  nteger,\n".     
29fd8 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20  "  sql text\n". 
29fd9 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e      ")".  ;.#ifn
29fda 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29fdb 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20  TEMPDB.  static 
29fdc 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f  const char temp_
29fdd 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20  master_schema[] 
29fde 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  = .     "CREATE 
29fdf 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74  TEMP TABLE sqlit
29fe0 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e  e_temp_master(\n
29fe1 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74  ".     "  type t
29fe2 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
29fe3 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20  name text,\n".  
29fe4 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74     "  tbl_name t
29fe5 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ext,\n".     "  
29fe6 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72  rootpage integer
29fe7 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c  ,\n".     "  sql
29fe8 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29   text\n".     ")
29fe9 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64  ".  ;.#else.  #d
29fea 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65  efine temp_maste
29feb 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69  r_schema 0.#endi
29fec 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  f..  assert( iDb
29fed 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
29fee 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
29fef 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
29ff0 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74  hema );.  assert
29ff1 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
29ff2 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
29ff3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
29ff4 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  ==1 || sqlite3Bt
29ff5 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
29ff6 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20  ->aDb[iDb].pBt) 
29ff7 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72  );..  /* zMaster
29ff8 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74  Schema and zInit
29ff9 53 63 72 69 70 74 20 61 72 65 20 73 65 74 20 74  Script are set t
29ffa 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6d  o point at the m
29ffb 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20 20 2a  aster schema.  *
29ffc 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 61  * and initialisa
29ffd 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70 70 72  tion script appr
29ffe 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20  opriate for the 
29fff 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 0a 20  database being. 
2a000 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e   ** initialised.
2a001 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73 20   zMasterName is 
2a002 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
2a003 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20  master table..  
2a004 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  */.  if( !OMIT_T
2a005 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
2a006 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63  ){.    zMasterSc
2a007 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74  hema = temp_mast
2a008 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c  er_schema;.  }el
2a009 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53  se{.    zMasterS
2a00a 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73  chema = master_s
2a00b 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61  chema;.  }.  zMa
2a00c 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d  sterName = SCHEM
2a00d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20  A_TABLE(iDb);.. 
2a00e 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   /* Construct th
2a00f 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 2e  e schema tables.
2a010 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d 20    */.  azArg[0] 
2a011 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0a 20  = zMasterName;. 
2a012 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31 22 3b   azArg[1] = "1";
2a013 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20 7a 4d  .  azArg[2] = zM
2a014 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 61  asterSchema;.  a
2a015 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20 20 69  zArg[3] = 0;.  i
2a016 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b  nitData.db = db;
2a017 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20  .  initData.iDb 
2a018 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44 61 74  = iDb;.  initDat
2a019 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  a.rc = SQLITE_OK
2a01a 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45  ;.  initData.pzE
2a01b 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73 67  rrMsg = pzErrMsg
2a01c 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
2a01d 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2a01e 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c    sqlite3InitCal
2a01f 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61 2c  lback(&initData,
2a020 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a 41   3, (char **)azA
2a021 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29  rg, 0);.  (void)
2a022 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2a023 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69 74 44  db);.  if( initD
2a024 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72 63  ata.rc ){.    rc
2a025 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a   = initData.rc;.
2a026 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
2a027 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  ut;.  }.  pTab =
2a028 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2a029 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d  e(db, zMasterNam
2a02a 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
2a02b 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c  zName);.  if( AL
2a02c 57 41 59 53 28 70 54 61 62 29 20 29 7b 0a 20 20  WAYS(pTab) ){.  
2a02d 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
2a02e 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b   |= TF_Readonly;
2a02f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
2a030 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68 6f  e a cursor to ho
2a031 6c 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ld the database 
2a032 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20  open.  */.  pDb 
2a033 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
2a034 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d  .  if( pDb->pBt=
2a035 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4f  =0 ){.    if( !O
2a036 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 41 4c  MIT_TEMPDB && AL
2a037 57 41 59 53 28 69 44 62 3d 3d 31 29 20 29 7b 0a  WAYS(iDb==1) ){.
2a038 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70 65        DbSetPrope
2a039 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63  rty(db, 1, DB_Sc
2a03a 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20  hemaLoaded);.   
2a03b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
2a03c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
2a03d 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
2a03e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  ot already a rea
2a03f 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d  d-only (or read-
2a040 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
2a041 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 6f  on opened.  ** o
2a042 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64 61 74  n the b-tree dat
2a043 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20  abase, open one 
2a044 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61  now. If a transa
2a045 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c  ction is opened,
2a046 20 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c 20 62   it .  ** will b
2a047 65 20 63 6c 6f 73 65 64 20 62 65 66 6f 72 65 20  e closed before 
2a048 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
2a049 74 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73 71 6c  turns.  */.  sql
2a04a 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
2a04b 44 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  Db->pBt);.  if( 
2a04c 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  !sqlite3BtreeIsI
2a04d 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e  nReadTrans(pDb->
2a04e 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  pBt) ){.    rc =
2a04f 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
2a050 69 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74  inTrans(pDb->pBt
2a051 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
2a052 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2a053 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
2a054 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
2a055 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
2a056 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
2a057 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
2a058 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
2a059 7d 0a 20 20 20 20 6f 70 65 6e 65 64 54 72 61 6e  }.    openedTran
2a05a 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 7d  saction = 1;.  }
2a05b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64  ..  /* Get the d
2a05c 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66  atabase meta inf
2a05d 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  ormation..  **. 
2a05e 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20   ** Meta values 
2a05f 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  are as follows:.
2a060 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20    **    meta[0] 
2a061 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e    Schema cookie.
2a062 20 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65    Changes with e
2a063 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ach schema chang
2a064 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  e..  **    meta[
2a065 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74  1]   File format
2a066 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72   of schema layer
2a067 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32  ..  **    meta[2
2a068 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ]   Size of the 
2a069 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a  page cache..  **
2a06a 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 4c 61      meta[3]   La
2a06b 72 67 65 73 74 20 72 6f 6f 74 70 61 67 65 20 28  rgest rootpage (
2a06c 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d  auto/incr_vacuum
2a06d 20 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20 20 6d   mode).  **    m
2a06e 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65 78 74  eta[4]   Db text
2a06f 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46   encoding. 1:UTF
2a070 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a  -8 2:UTF-16LE 3:
2a071 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20  UTF-16BE.  **   
2a072 20 6d 65 74 61 5b 35 5d 20 20 20 55 73 65 72 20   meta[5]   User 
2a073 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  version.  **    
2a074 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d  meta[6]   Increm
2a075 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64  ental vacuum mod
2a076 65 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37  e.  **    meta[7
2a077 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20  ]   unused.  ** 
2a078 20 20 20 6d 65 74 61 5b 38 5d 20 20 20 75 6e 75     meta[8]   unu
2a079 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  sed.  **    meta
2a07a 5b 39 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [9]   unused.  *
2a07b 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65  *.  ** Note: The
2a07c 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45   #defined SQLITE
2a07d 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e  _UTF* symbols in
2a07e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72   sqliteInt.h cor
2a07f 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20  respond to.  ** 
2a080 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c  the possible val
2a081 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a  ues of meta[4]..
2a082 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
2a083 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74 61  i<ArraySize(meta
2a084 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  ); i++){.    sql
2a085 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
2a086 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20  (pDb->pBt, i+1, 
2a087 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29  (u32 *)&meta[i])
2a088 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63  ;.  }.  pDb->pSc
2a089 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
2a08a 6b 69 65 20 3d 20 6d 65 74 61 5b 42 54 52 45 45  kie = meta[BTREE
2a08b 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2d  _SCHEMA_VERSION-
2a08c 31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65  1];..  /* If ope
2a08d 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79  ning a non-empty
2a08e 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b   database, check
2a08f 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   the text encodi
2a090 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a  ng. For the.  **
2a091 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
2a092 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20  set sqlite3.enc 
2a093 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  to the encoding 
2a094 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
2a095 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61  base..  ** For a
2a096 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c 20 69  n attached db, i
2a097 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66  t is an error if
2a098 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
2a099 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20   not the same.  
2a09a 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e  ** as sqlite3.en
2a09b 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65  c..  */.  if( me
2a09c 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e  ta[BTREE_TEXT_EN
2a09d 43 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20 2f 2a  CODING-1] ){  /*
2a09e 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
2a09f 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  /.    if( iDb==0
2a0a0 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 6e 63   ){.      u8 enc
2a0a1 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f 2a 20  oding;.      /* 
2a0a2 49 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d  If opening the m
2a0a3 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65  ain database, se
2a0a4 74 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20  t ENC(db). */.  
2a0a5 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28      encoding = (
2a0a6 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 54 45  u8)meta[BTREE_TE
2a0a7 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26  XT_ENCODING-1] &
2a0a8 20 33 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e   3;.      if( en
2a0a9 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f  coding==0 ) enco
2a0aa 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f 55 54  ding = SQLITE_UT
2a0ab 46 38 3b 0a 20 20 20 20 20 20 45 4e 43 28 64 62  F8;.      ENC(db
2a0ac 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  ) = encoding;.  
2a0ad 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c      db->pDfltCol
2a0ae 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
2a0af 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
2a0b0 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22  E_UTF8, "BINARY"
2a0b1 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
2a0b2 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65  .      /* If ope
2a0b3 6e 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65 64  ning an attached
2a0b4 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 65   database, the e
2a0b5 6e 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d 61 74  ncoding much mat
2a0b6 63 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20  ch ENC(db) */.  
2a0b7 20 20 20 20 69 66 28 20 6d 65 74 61 5b 42 54 52      if( meta[BTR
2a0b8 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47  EE_TEXT_ENCODING
2a0b9 2d 31 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a  -1]!=ENC(db) ){.
2a0ba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2a0bb 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73  etString(pzErrMs
2a0bc 67 2c 20 64 62 2c 20 22 61 74 74 61 63 68 65 64  g, db, "attached
2a0bd 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20   databases must 
2a0be 75 73 65 20 74 68 65 20 73 61 6d 65 22 0a 20 20  use the same".  
2a0bf 20 20 20 20 20 20 20 20 20 20 22 20 74 65 78 74            " text
2a0c0 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69   encoding as mai
2a0c1 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20  n database");.  
2a0c2 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2a0c3 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
2a0c4 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72   goto initone_er
2a0c5 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  ror_out;.      }
2a0c6 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
2a0c7 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74      DbSetPropert
2a0c8 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d  y(db, iDb, DB_Em
2a0c9 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d  pty);.  }.  pDb-
2a0ca 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20  >pSchema->enc = 
2a0cb 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66 28 20  ENC(db);..  if( 
2a0cc 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
2a0cd 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  che_size==0 ){. 
2a0ce 20 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 42     size = meta[B
2a0cf 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
2a0d0 48 45 5f 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20  HE_SIZE-1];.    
2a0d1 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73  if( size==0 ){ s
2a0d2 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  ize = SQLITE_DEF
2a0d3 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b  AULT_CACHE_SIZE;
2a0d4 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3c   }.    if( size<
2a0d5 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65  0 ) size = -size
2a0d6 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  ;.    pDb->pSche
2a0d7 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d  ma->cache_size =
2a0d8 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74   size;.    sqlit
2a0d9 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
2a0da 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
2a0db 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
2a0dc 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20  e_size);.  }..  
2a0dd 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  /*.  ** file_for
2a0de 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f  mat==1    Versio
2a0df 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69  n 3.0.0..  ** fi
2a0e0 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20  le_format==2    
2a0e1 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20  Version 3.1.3.  
2a0e2 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41  // ALTER TABLE A
2a0e3 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66  DD COLUMN.  ** f
2a0e4 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20  ile_format==3   
2a0e5 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20   Version 3.1.4. 
2a0e6 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69   // ditto but wi
2a0e7 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61  th non-NULL defa
2a0e8 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66  ults.  ** file_f
2a0e9 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73  ormat==4    Vers
2a0ea 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44  ion 3.3.0.  // D
2a0eb 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f  ESC indices.  Bo
2a0ec 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a  olean constants.
2a0ed 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68    */.  pDb->pSch
2a0ee 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
2a0ef 20 3d 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45   = (u8)meta[BTRE
2a0f0 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d  E_FILE_FORMAT-1]
2a0f1 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  ;.  if( pDb->pSc
2a0f2 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
2a0f3 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d  t==0 ){.    pDb-
2a0f4 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
2a0f5 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  ormat = 1;.  }. 
2a0f6 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
2a0f7 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53  a->file_format>S
2a0f8 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
2a0f9 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73 71 6c  ORMAT ){.    sql
2a0fa 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
2a0fb 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73  ErrMsg, db, "uns
2a0fc 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f  upported file fo
2a0fd 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63 20 3d  rmat");.    rc =
2a0fe 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2a0ff 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
2a100 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
2a101 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 38 30    /* Ticket #280
2a102 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70 65 6e  4:  When we open
2a103 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 20 74   a database in t
2a104 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20 66 6f  he newer file fo
2a105 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72  rmat,.  ** clear
2a106 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69 6c 65   the legacy_file
2a107 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61 20 66  _format pragma f
2a108 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20 56 41  lag so that a VA
2a109 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e  CUUM will.  ** n
2a10a 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65  ot downgrade the
2a10b 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
2a10c 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  us invalidate an
2a10d 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a  y descending.  *
2a10e 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 74  * indices that t
2a10f 68 65 20 75 73 65 72 20 6d 69 67 68 74 20 68 61  he user might ha
2a110 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f  ve created..  */
2a111 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 26 26  .  if( iDb==0 &&
2a112 20 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45   meta[BTREE_FILE
2a113 5f 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20 29 7b  _FORMAT-1]>=4 ){
2a114 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  .    db->flags &
2a115 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61 63 79  = ~SQLITE_Legacy
2a116 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20  FileFmt;.  }..  
2a117 2f 2a 20 52 65 61 64 20 74 68 65 20 73 63 68 65  /* Read the sche
2a118 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ma information o
2a119 75 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  ut of the schema
2a11a 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61   tables.  */.  a
2a11b 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
2a11c 62 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20 20 20  busy );.  {.    
2a11d 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
2a11e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
2a11f 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
2a120 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c     "SELECT name,
2a121 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46   rootpage, sql F
2a122 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a 20 20  ROM '%q'.%s",.  
2a123 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
2a124 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65  b].zName, zMaste
2a125 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69  rName);.    (voi
2a126 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2a127 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65 66 20  ff(db);.#ifndef 
2a128 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
2a129 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a  ORIZATION.    {.
2a12a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74        int (*xAut
2a12b 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
2a12c 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
2a12d 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
2a12e 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
2a12f 20 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d       xAuth = db-
2a130 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62  >xAuth;.      db
2a131 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e  ->xAuth = 0;.#en
2a132 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73  dif.      rc = s
2a133 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
2a134 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
2a135 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
2a136 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65  Data, 0);.#ifnde
2a137 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2a138 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
2a139 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
2a13a 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  uth;.    }.#endi
2a13b 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  f.    if( rc==SQ
2a13c 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69  LITE_OK ) rc = i
2a13d 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
2a13e 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2a13f 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73  etyOn(db);.    s
2a140 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2a141 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20   zSql);.#ifndef 
2a142 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
2a143 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
2a144 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2a145 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
2a146 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
2a147 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
2a148 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
2a149 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2a14a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2a14b 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  M;.    sqlite3Re
2a14c 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
2a14d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  a(db, 0);.  }.  
2a14e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2a14f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26  K || (db->flags&
2a150 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
2a151 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c  ode)){.    /* Bl
2a152 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68  ack magic: If th
2a153 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  e SQLITE_Recover
2a154 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65  yMode flag is se
2a155 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72  t, then consider
2a156 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
2a157 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20  ma loaded, even 
2a158 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72  if errors occurr
2a159 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75  ed. In this situ
2a15a 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a  ation the .    *
2a15b 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65  * current sqlite
2a15c 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72  3_prepare() oper
2a15d 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c  ation will fail,
2a15e 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   but the followi
2a15f 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69  ng one.    ** wi
2a160 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f  ll attempt to co
2a161 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69  mpile the suppli
2a162 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61  ed statement aga
2a163 69 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75  inst whatever su
2a164 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74  bset.    ** of t
2a165 68 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f  he schema was lo
2a166 61 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  aded before the 
2a167 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2e 20  error occurred. 
2a168 54 68 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20  The primary.    
2a169 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  ** purpose of th
2a16a 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61  is is to allow a
2a16b 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c  ccess to the sql
2a16c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2a16d 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65  .    ** even whe
2a16e 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68  n its contents h
2a16f 61 76 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74  ave been corrupt
2a170 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44  ed..    */.    D
2a171 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
2a172 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c   iDb, DB_SchemaL
2a173 6f 61 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d  oaded);.    rc =
2a174 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
2a175 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
2a176 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61  for an error tha
2a177 74 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 73  t occurs after s
2a178 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f  uccessfully allo
2a179 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d  cating.  ** curM
2a17a 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20  ain and calling 
2a17b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
2a17c 72 28 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f  r(). For an erro
2a17d 72 20 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20  r that occurs.  
2a17e 2a 2a 20 62 65 66 6f 72 65 20 74 68 61 74 20 70  ** before that p
2a17f 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72  oint, jump to er
2a180 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e  ror_out..  */.in
2a181 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a  itone_error_out:
2a182 0a 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61  .  if( openedTra
2a183 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
2a184 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
2a185 69 74 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  it(pDb->pBt);.  
2a186 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
2a187 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b  Leave(pDb->pBt);
2a188 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69  ..error_out:.  i
2a189 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2a18a 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
2a18b 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
2a18c 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
2a18d 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  ailed = 1;.  }. 
2a18e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2a18f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
2a190 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
2a191 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61  es - the main da
2a192 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
2a193 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f   file.** used to
2a194 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79   store temporary
2a195 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79   tables, and any
2a196 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61   additional data
2a197 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72  base files.** cr
2a198 65 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41  eated using ATTA
2a199 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  CH statements.  
2a19a 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73  Return a success
2a19b 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a   code.  If an.**
2a19c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77   error occurs, w
2a19d 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rite an error me
2a19e 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72  ssage into *pzEr
2a19f 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  rMsg..**.** Afte
2a1a0 72 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  r a database is 
2a1a1 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
2a1a2 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
2a1a3 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62   bit is set.** b
2a1a4 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
2a1a5 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20   flags field of 
2a1a6 74 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65  the Db structure
2a1a7 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  . If the databas
2a1a8 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66  e.** file was of
2a1a9 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68   zero-length, th
2a1aa 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20  en the DB_Empty 
2a1ab 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74  flag is also set
2a1ac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2a1ad 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
2a1ae 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  nit(sqlite3 *db,
2a1af 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
2a1b0 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a  ){.  int i, rc;.
2a1b1 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74    int commit_int
2a1b2 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c  ernal = !(db->fl
2a1b3 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
2a1b4 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20  nChanges);.  .  
2a1b5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
2a1b6 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
2a1b7 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
2a1b8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64 62 2d  SQLITE_OK;.  db-
2a1b9 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a  >init.busy = 1;.
2a1ba 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
2a1bb 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
2a1bc 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
2a1bd 20 69 66 28 20 44 62 48 61 73 50 72 6f 70 65 72   if( DbHasProper
2a1be 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53 63 68  ty(db, i, DB_Sch
2a1bf 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d  emaLoaded) || i=
2a1c0 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =1 ) continue;. 
2a1c1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
2a1c2 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a  nitOne(db, i, pz
2a1c3 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28  ErrMsg);.    if(
2a1c4 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   rc ){.      sql
2a1c5 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2a1c6 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a  lSchema(db, i);.
2a1c7 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2a1c8 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74 68  Once all the oth
2a1c9 65 72 20 64 61 74 61 62 61 73 65 73 20 68 61 76  er databases hav
2a1ca 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73  e been initialis
2a1cb 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63 68  ed, load the sch
2a1cc 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ema.  ** for the
2a1cd 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e 20   TEMP database. 
2a1ce 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20 6c  This is loaded l
2a1cf 61 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d 50  ast, as the TEMP
2a1d0 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 73   database.  ** s
2a1d1 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69  chema may contai
2a1d2 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  n references to 
2a1d3 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65 72  objects in other
2a1d4 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f   databases..  */
2a1d5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a1d6 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69 66  OMIT_TEMPDB.  if
2a1d7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2a1d8 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6e 44  && ALWAYS(db->nD
2a1d9 62 3e 31 29 0a 20 20 20 20 20 20 20 20 20 20 20  b>1).           
2a1da 20 20 20 20 20 20 20 20 20 26 26 20 21 44 62 48           && !DbH
2a1db 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 31  asProperty(db, 1
2a1dc 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
2a1dd 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  d) ){.    rc = s
2a1de 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62  qlite3InitOne(db
2a1df 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  , 1, pzErrMsg);.
2a1e0 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
2a1e1 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
2a1e2 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2a1e3 62 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, 1);.    }.  }
2a1e4 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69  .#endif..  db->i
2a1e5 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20  nit.busy = 0;.  
2a1e6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2a1e7 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69 6e 74 65  K && commit_inte
2a1e8 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
2a1e9 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61  te3CommitInterna
2a1ea 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20  lChanges(db);.  
2a1eb 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20  }..  return rc; 
2a1ec 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2a1ed 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
2a1ee 70 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  p if the databas
2a1ef 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65  e schema is alre
2a1f0 61 64 79 20 69 6e 69 74 69 61 6c 69 73 65 64 2e  ady initialised.
2a1f1 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
2a1f2 68 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f 61  he schema is loa
2a1f3 64 65 64 2e 20 41 6e 20 65 72 72 6f 72 20 63 6f  ded. An error co
2a1f4 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
2a1f5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2a1f6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61  E int sqlite3Rea
2a1f7 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70  dSchema(Parse *p
2a1f8 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 72 63  Parse){.  int rc
2a1f9 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2a1fa 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2a1fb 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65  arse->db;.  asse
2a1fc 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
2a1fd 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
2a1fe 29 20 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e  ) );.  if( !db->
2a1ff 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
2a200 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69   rc = sqlite3Ini
2a201 74 28 64 62 2c 20 26 70 50 61 72 73 65 2d 3e 7a  t(db, &pParse->z
2a202 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20 20 69  ErrMsg);.  }.  i
2a203 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2a204 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
2a205 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61  rc = rc;.    pPa
2a206 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d  rse->nErr++;.  }
2a207 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2a208 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 73 63  ../*.** Check sc
2a209 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20  hema cookies in 
2a20a 61 6c 6c 20 64 61 74 61 62 61 73 65 73 2e 20 20  all databases.  
2a20b 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73  If any cookie is
2a20c 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74 65 20   out.** of date 
2a20d 73 65 74 20 70 50 61 72 73 65 2d 3e 72 63 20 74  set pParse->rc t
2a20e 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e  o SQLITE_SCHEMA.
2a20f 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20    If all schema 
2a210 63 6f 6f 6b 69 65 73 0a 2a 2a 20 6d 61 6b 65 20  cookies.** make 
2a211 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 70 50  no changes to pP
2a212 61 72 73 65 2d 3e 72 63 2e 0a 2a 2f 0a 73 74 61  arse->rc..*/.sta
2a213 74 69 63 20 76 6f 69 64 20 73 63 68 65 6d 61 49  tic void schemaI
2a214 73 56 61 6c 69 64 28 50 61 72 73 65 20 2a 70 50  sValid(Parse *pP
2a215 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
2a216 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2a217 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  b;.  int iDb;.  
2a218 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f  int rc;.  int co
2a219 6f 6b 69 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  okie;..  assert(
2a21a 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63   pParse->checkSc
2a21b 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74  hema );.  assert
2a21c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
2a21d 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
2a21e 29 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30 3b 20  );.  for(iDb=0; 
2a21f 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
2a220 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 65  ++){.    int ope
2a221 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d  nedTransaction =
2a222 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
2a223 72 75 65 20 69 66 20 61 20 74 72 61 6e 73 61 63  rue if a transac
2a224 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 2a  tion is opened *
2a225 2f 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  /.    Btree *pBt
2a226 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
2a227 70 42 74 3b 20 20 20 20 20 2f 2a 20 42 74 72 65  pBt;     /* Btre
2a228 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 72 65  e database to re
2a229 61 64 20 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 2a  ad cookie from *
2a22a 2f 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30  /.    if( pBt==0
2a22b 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20   ) continue;..  
2a22c 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2a22d 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72   not already a r
2a22e 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61  ead-only (or rea
2a22f 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63  d-write) transac
2a230 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 20 20  tion opened.    
2a231 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65  ** on the b-tree
2a232 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20   database, open 
2a233 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
2a234 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
2a235 6e 65 64 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20  ned, it .    ** 
2a236 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 20 69  will be closed i
2a237 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
2a238 20 72 65 61 64 69 6e 67 20 74 68 65 20 6d 65 74   reading the met
2a239 61 2d 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  a-value. */.    
2a23a 69 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65  if( !sqlite3Btre
2a23b 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70  eIsInReadTrans(p
2a23c 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Bt) ){.      rc 
2a23d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
2a23e 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 30 29  ginTrans(pBt, 0)
2a23f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
2a240 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
2a241 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
2a242 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
2a243 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2a244 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
2a245 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2a246 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2a247 3b 0a 20 20 20 20 20 20 6f 70 65 6e 65 64 54 72  ;.      openedTr
2a248 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20  ansaction = 1;. 
2a249 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 61     }..    /* Rea
2a24a 64 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  d the schema coo
2a24b 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74  kie from the dat
2a24c 61 62 61 73 65 2e 20 49 66 20 69 74 20 64 6f 65  abase. If it doe
2a24d 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20  s not match the 
2a24e 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 73 74  .    ** value st
2a24f 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ored as part of 
2a250 74 68 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65  the in the in-me
2a251 6d 6f 72 79 20 73 63 68 65 6d 61 20 72 65 70 72  mory schema repr
2a252 65 73 65 6e 74 61 74 69 6f 6e 2c 0a 20 20 20 20  esentation,.    
2a253 2a 2a 20 73 65 74 20 50 61 72 73 65 2e 72 63 20  ** set Parse.rc 
2a254 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  to SQLITE_SCHEMA
2a255 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
2a256 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74  BtreeGetMeta(pBt
2a257 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56  , BTREE_SCHEMA_V
2a258 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29 26  ERSION, (u32 *)&
2a259 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 69 66 28  cookie);.    if(
2a25a 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62   cookie!=db->aDb
2a25b 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73  [iDb].pSchema->s
2a25c 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a  chema_cookie ){.
2a25d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
2a25e 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41   = SQLITE_SCHEMA
2a25f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a260 43 6c 6f 73 65 20 74 68 65 20 74 72 61 6e 73 61  Close the transa
2a261 63 74 69 6f 6e 2c 20 69 66 20 6f 6e 65 20 77 61  ction, if one wa
2a262 73 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20  s opened. */.   
2a263 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e 73   if( openedTrans
2a264 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
2a265 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
2a266 69 74 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20  it(pBt);.    }. 
2a267 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
2a268 65 72 74 20 61 20 73 63 68 65 6d 61 20 70 6f 69  ert a schema poi
2a269 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 69 44  nter into the iD
2a26a 62 20 69 6e 64 65 78 20 74 68 61 74 20 69 6e 64  b index that ind
2a26b 69 63 61 74 65 73 0a 2a 2a 20 77 68 69 63 68 20  icates.** which 
2a26c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
2a26d 20 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65 20 73   db->aDb[] the s
2a26e 63 68 65 6d 61 20 72 65 66 65 72 73 20 74 6f 2e  chema refers to.
2a26f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 61  .**.** If the sa
2a270 6d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61  me database is a
2a271 74 74 61 63 68 65 64 20 6d 6f 72 65 20 74 68 61  ttached more tha
2a272 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69 72 73  n once, the firs
2a273 74 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61  t.** attached da
2a274 74 61 62 61 73 65 20 69 73 20 72 65 74 75 72 6e  tabase is return
2a275 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2a276 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2a277 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73  3SchemaToIndex(s
2a278 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65  qlite3 *db, Sche
2a279 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20  ma *pSchema){.  
2a27a 69 6e 74 20 69 20 3d 20 2d 31 30 30 30 30 30 30  int i = -1000000
2a27b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63 68 65  ;..  /* If pSche
2a27c 6d 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  ma is NULL, then
2a27d 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30   return -1000000
2a27e 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  . This happens w
2a27f 68 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20 20 2a  hen code in .  *
2a280 2a 20 65 78 70 72 2e 63 20 69 73 20 74 72 79 69  * expr.c is tryi
2a281 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20  ng to resolve a 
2a282 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 74  reference to a t
2a283 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 28  ransient table (
2a284 69 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20 63 72  i.e. one.  ** cr
2a285 65 61 74 65 64 20 62 79 20 61 20 73 75 62 2d 73  eated by a sub-s
2a286 65 6c 65 63 74 29 2e 20 49 6e 20 74 68 69 73 20  elect). In this 
2a287 63 61 73 65 20 74 68 65 20 72 65 74 75 72 6e 20  case the return 
2a288 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 0a 20  value of this . 
2a289 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f   ** function sho
2a28a 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75 73 65  uld never be use
2a28b 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20  d..  **.  ** We 
2a28c 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30 20  return -1000000 
2a28d 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6d  instead of the m
2a28e 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73 69 6d  ore usual -1 sim
2a28f 70 6c 79 20 62 65 63 61 75 73 65 20 75 73 69 6e  ply because usin
2a290 67 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30 30 20  g.  ** -1000000 
2a291 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65 63 74  as the incorrect
2a292 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62 2d 3e   index into db->
2a293 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20 0a 20  aDb[] is much . 
2a294 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20   ** more likely 
2a295 74 6f 20 63 61 75 73 65 20 61 20 73 65 67 66 61  to cause a segfa
2a296 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f 66 20  ult than -1 (of 
2a297 63 6f 75 72 73 65 20 74 68 65 72 65 20 61 72 65  course there are
2a298 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a 20 73   assert().  ** s
2a299 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c 20 62  tatements too, b
2a29a 75 74 20 69 74 20 6e 65 76 65 72 20 68 75 72 74  ut it never hurt
2a29b 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20 6f 64  s to play the od
2a29c 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ds)..  */.  asse
2a29d 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
2a29e 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
2a29f 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63 68 65  ) );.  if( pSche
2a2a0 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ma ){.    for(i=
2a2a1 30 3b 20 41 4c 57 41 59 53 28 69 3c 64 62 2d 3e  0; ALWAYS(i<db->
2a2a2 6e 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nDb); i++){.    
2a2a3 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
2a2a4 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65 6d  .pSchema==pSchem
2a2a5 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  a ){.        bre
2a2a6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2a2a7 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  }.    assert( i>
2a2a8 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20  =0 && i<db->nDb 
2a2a9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
2a2aa 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  i;.}../*.** Comp
2a2ab 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20 65 6e  ile the UTF-8 en
2a2ac 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a2ad 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20  ent zSql into a 
2a2ae 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
2a2af 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2a2b0 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 0a  sqlite3Prepare(.
2a2b1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2a2b2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2a2b3 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
2a2b4 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2a2b5 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
2a2b6 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
2a2b7 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
2a2b8 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
2a2b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a2ba 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
2a2bb 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69  in bytes. */.  i
2a2bc 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  nt saveSqlFlag, 
2a2bd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2a2be 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65 78   to copy SQL tex
2a2bf 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  t into the sqlit
2a2c0 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 56 64 62  e3_stmt */.  Vdb
2a2c1 65 20 2a 70 52 65 70 72 65 70 61 72 65 2c 20 20  e *pReprepare,  
2a2c2 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 62 65 69         /* VM bei
2a2c3 6e 67 20 72 65 70 72 65 70 61 72 65 64 20 2a 2f  ng reprepared */
2a2c4 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a2c5 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a2c6 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a2c7 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a2c8 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a2c9 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69  nst char **pzTai
2a2ca 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a2cb 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a2cc 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 61 72  ring */.){.  Par
2a2cd 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
2a2ce 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2a2cf 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  g context */.  c
2a2d0 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
2a2d1 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f  ;        /* Erro
2a2d2 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 69  r message */.  i
2a2d3 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2a2d4 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75  K;       /* Resu
2a2d5 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  lt code */.  int
2a2d6 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2a2d7 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2a2d8 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20  ounter */..  /* 
2a2d9 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 70 61 72  Allocate the par
2a2da 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2a2db 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74    pParse = sqlit
2a2dc 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
2a2dd 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61  (db, sizeof(*pPa
2a2de 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61  rse));.  if( pPa
2a2df 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  rse==0 ){.    rc
2a2e0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2a2e1 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72  .    goto end_pr
2a2e2 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20 70 50 61  epare;.  }.  pPa
2a2e3 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72 65 20  rse->pReprepare 
2a2e4 3d 20 70 52 65 70 72 65 70 61 72 65 3b 0a 0a 20  = pReprepare;.. 
2a2e5 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
2a2e6 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
2a2e7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55  rc = SQLITE_MISU
2a2e8 53 45 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64  SE;.    goto end
2a2e9 5f 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20  _prepare;.  }.  
2a2ea 61 73 73 65 72 74 28 20 70 70 53 74 6d 74 20 26  assert( ppStmt &
2a2eb 26 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 0a  & *ppStmt==0 );.
2a2ec 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
2a2ed 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2a2ee 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2a2ef 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
2a2f0 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20  mutex) );..  /* 
2a2f1 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66 79 20  Check to verify 
2a2f2 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
2a2f3 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72 65 61  ble to get a rea
2a2f4 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a 20 20  d lock on all.  
2a2f5 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
2a2f6 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62 69 6c  mas.  The inabil
2a2f7 69 74 79 20 74 6f 20 67 65 74 20 61 20 72 65 61  ity to get a rea
2a2f8 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74 65 73  d lock indicates
2a2f9 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d 65 20   that.  ** some 
2a2fa 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
2a2fb 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
2a2fc 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ding a write-loc
2a2fd 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20 2a 2a  k, which in.  **
2a2fe 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68 61 74   turn means that
2a2ff 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65   the other conne
2a300 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 75  ction has made u
2a301 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61 6e 67  ncommitted chang
2a302 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73  es.  ** to the s
2a303 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  chema..  **.  **
2a304 20 57 65 72 65 20 77 65 20 74 6f 20 70 72 6f 63   Were we to proc
2a305 65 65 64 20 61 6e 64 20 70 72 65 70 61 72 65 20  eed and prepare 
2a306 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 67  the statement ag
2a307 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f 6d 6d  ainst the uncomm
2a308 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68 65 6d  itted.  ** schem
2a309 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 66  a changes and if
2a30a 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20 63 68   those schema ch
2a30b 61 6e 67 65 73 20 61 72 65 20 73 75 62 73 65 71  anges are subseq
2a30c 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a 20 20  uently rolled.  
2a30d 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69 66 66  ** back and diff
2a30e 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20 61 72  erent changes ar
2a30f 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69 72 20  e made in their 
2a310 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68 65 6e  place, then when
2a311 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 70 61   this.  ** prepa
2a312 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 6f  red statement go
2a313 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20 73 63  es to run the sc
2a314 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f 75 6c  hema cookie woul
2a315 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65 63 74  d fail to detect
2a316 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
2a317 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61 73 74   change.  Disast
2a318 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f 77 2e  er would follow.
2a319 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
2a31a 74 68 72 65 61 64 20 69 73 20 63 75 72 72 65 6e  thread is curren
2a31b 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75 74 65  tly holding mute
2a31c 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72 65 65  xes on all Btree
2a31d 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  s (because.  ** 
2a31e 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 42 74  of the sqlite3Bt
2a31f 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 69 6e  reeEnterAll() in
2a320 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
2a321 72 65 70 61 72 65 28 29 29 20 73 6f 20 69 74 0a  repare()) so it.
2a322 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73    ** is not poss
2a323 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68 65 72  ible for another
2a324 20 74 68 72 65 61 64 20 74 6f 20 73 74 61 72 74   thread to start
2a325 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20 63 68   a new schema ch
2a326 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c 65 20  ange.  ** while 
2a327 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2a328 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63 65 2c  running.  Hence,
2a329 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20   we do not need 
2a32a 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20 6c 6f  to hold .  ** lo
2a32b 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68 65 6d  cks on the schem
2a32c 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65 64 20  a, we just need 
2a32d 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 62  to make sure nob
2a32e 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20 20 2a  ody else is .  *
2a32f 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d 2e 0a  * holding them..
2a330 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
2a331 68 61 74 20 73 65 74 74 69 6e 67 20 52 45 41 44  hat setting READ
2a332 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f 76 65  _UNCOMMITTED ove
2a333 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f 63 6b  rrides most lock
2a334 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20 2a 2a   detection,.  **
2a335 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a 6e 6f   but it does *no
2a336 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63 68 65  t* override sche
2a337 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f  ma lock detectio
2a338 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c 20 73  n, so this all s
2a339 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b 73 20  till.  ** works 
2a33a 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55 4e 43  even if READ_UNC
2a33b 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65 74 2e  OMMITTED is set.
2a33c 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
2a33d 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2a33e 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42   {.    Btree *pB
2a33f 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
2a340 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 20  Bt;.    if( pBt 
2a341 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2a342 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
2a343 64 73 4d 75 74 65 78 28 70 42 74 29 20 29 3b 0a  dsMutex(pBt) );.
2a344 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2a345 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
2a346 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ked(pBt);.      
2a347 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
2a348 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2a349 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  b = db->aDb[i].z
2a34a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71  Name;.        sq
2a34b 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
2a34c 63 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 68  c, "database sch
2a34d 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25  ema is locked: %
2a34e 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20  s", zDb);.      
2a34f 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2a350 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2a351 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a352 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2a353 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
2a354 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ed );.        go
2a355 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a  to end_prepare;.
2a356 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2a357 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  }..  sqlite3Vtab
2a358 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a  UnlockList(db);.
2a359 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20  .  pParse->db = 
2a35a 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  db;.  if( nBytes
2a35b 3e 3d 30 20 26 26 20 28 6e 42 79 74 65 73 3d 3d  >=0 && (nBytes==
2a35c 30 20 7c 7c 20 7a 53 71 6c 5b 6e 42 79 74 65 73  0 || zSql[nBytes
2a35d 2d 31 5d 21 3d 30 29 20 29 7b 0a 20 20 20 20 63  -1]!=0) ){.    c
2a35e 68 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20  har *zSqlCopy;. 
2a35f 20 20 20 69 6e 74 20 6d 78 4c 65 6e 20 3d 20 64     int mxLen = d
2a360 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
2a361 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
2a362 48 5d 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  H];.    testcase
2a363 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e 20  ( nBytes==mxLen 
2a364 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2a365 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e 2b 31   nBytes==mxLen+1
2a366 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74   );.    if( nByt
2a367 65 73 3e 6d 78 4c 65 6e 20 29 7b 0a 20 20 20 20  es>mxLen ){.    
2a368 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2a369 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
2a36a 2c 20 22 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f  , "statement too
2a36b 20 6c 6f 6e 67 22 29 3b 0a 20 20 20 20 20 20 28   long");.      (
2a36c 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2a36d 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
2a36e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
2a36f 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f  Exit(db, SQLITE_
2a370 54 4f 4f 42 49 47 29 3b 0a 20 20 20 20 20 20 67  TOOBIG);.      g
2a371 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b  oto end_prepare;
2a372 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43  .    }.    zSqlC
2a373 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53  opy = sqlite3DbS
2a374 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c  trNDup(db, zSql,
2a375 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66   nBytes);.    if
2a376 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20  ( zSqlCopy ){.  
2a377 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
2a378 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71  rser(pParse, zSq
2a379 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67 29  lCopy, &zErrMsg)
2a37a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
2a37b 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 43 6f  bFree(db, zSqlCo
2a37c 70 79 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  py);.      pPars
2a37d 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
2a37e 5b 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a  [pParse->zTail-z
2a37f 53 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20 7d 65  SqlCopy];.    }e
2a380 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73  lse{.      pPars
2a381 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
2a382 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a  [nBytes];.    }.
2a383 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2a384 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50  ite3RunParser(pP
2a385 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72  arse, zSql, &zEr
2a386 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  rMsg);.  }..  if
2a387 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2a388 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ed ){.    pParse
2a389 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
2a38a 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  MEM;.  }.  if( p
2a38b 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
2a38c 45 5f 44 4f 4e 45 20 29 20 70 50 61 72 73 65 2d  E_DONE ) pParse-
2a38d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
2a38e 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63  .  if( pParse->c
2a38f 68 65 63 6b 53 63 68 65 6d 61 20 29 7b 0a 20 20  heckSchema ){.  
2a390 20 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28    schemaIsValid(
2a391 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 69  pParse);.  }.  i
2a392 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  f( pParse->rc==S
2a393 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
2a394 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
2a395 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2a396 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
2a397 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2a398 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  d ){.    pParse-
2a399 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
2a39a 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 7a  EM;.  }.  if( pz
2a39b 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70 7a 54  Tail ){.    *pzT
2a39c 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e 7a 54  ail = pParse->zT
2a39d 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ail;.  }.  rc = 
2a39e 70 50 61 72 73 65 2d 3e 72 63 3b 0a 0a 23 69 66  pParse->rc;..#if
2a39f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2a3a0 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 72  _EXPLAIN.  if( r
2a3a1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2a3a2 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26 26  pParse->pVdbe &&
2a3a3 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
2a3a4 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
2a3a5 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73  onst char * cons
2a3a6 74 20 61 7a 43 6f 6c 4e 61 6d 65 5b 5d 20 3d 20  t azColName[] = 
2a3a7 7b 0a 20 20 20 20 20 20 20 22 61 64 64 72 22 2c  {.       "addr",
2a3a8 20 22 6f 70 63 6f 64 65 22 2c 20 22 70 31 22 2c   "opcode", "p1",
2a3a9 20 22 70 32 22 2c 20 22 70 33 22 2c 20 22 70 34   "p2", "p3", "p4
2a3aa 22 2c 20 22 70 35 22 2c 20 22 63 6f 6d 6d 65 6e  ", "p5", "commen
2a3ab 74 22 2c 0a 20 20 20 20 20 20 20 22 6f 72 64 65  t",.       "orde
2a3ac 72 22 2c 20 22 66 72 6f 6d 22 2c 20 22 64 65 74  r", "from", "det
2a3ad 61 69 6c 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20  ail".    };.    
2a3ae 69 6e 74 20 69 46 69 72 73 74 2c 20 6d 78 3b 0a  int iFirst, mx;.
2a3af 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
2a3b0 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20  explain==2 ){.  
2a3b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2a3b2 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72 73 65  etNumCols(pParse
2a3b3 2d 3e 70 56 64 62 65 2c 20 33 29 3b 0a 20 20 20  ->pVdbe, 3);.   
2a3b4 20 20 20 69 46 69 72 73 74 20 3d 20 38 3b 0a 20     iFirst = 8;. 
2a3b5 20 20 20 20 20 6d 78 20 3d 20 31 31 3b 0a 20 20       mx = 11;.  
2a3b6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
2a3b7 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
2a3b8 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70 56 64  Cols(pParse->pVd
2a3b9 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20 69 46  be, 8);.      iF
2a3ba 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  irst = 0;.      
2a3bb 6d 78 20 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20  mx = 8;.    }.  
2a3bc 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20    for(i=iFirst; 
2a3bd 69 3c 6d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  i<mx; i++){.    
2a3be 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2a3bf 43 6f 6c 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e  ColName(pParse->
2a3c0 70 56 64 62 65 2c 20 69 2d 69 46 69 72 73 74 2c  pVdbe, i-iFirst,
2a3c1 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 0a 20   COLNAME_NAME,. 
2a3c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3c3 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c             azCol
2a3c4 4e 61 6d 65 5b 69 5d 2c 20 53 51 4c 49 54 45 5f  Name[i], SQLITE_
2a3c5 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20  STATIC);.    }. 
2a3c6 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
2a3c7 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
2a3c8 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20  f(db) ){.    rc 
2a3c9 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
2a3ca 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2a3cb 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
2a3cc 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61 67 3d   || saveSqlFlag=
2a3cd 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  =0 );.  if( db->
2a3ce 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a  init.busy==0 ){.
2a3cf 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 20      Vdbe *pVdbe 
2a3d0 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2a3d1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a3d2 53 65 74 53 71 6c 28 70 56 64 62 65 2c 20 7a 53  SetSql(pVdbe, zS
2a3d3 71 6c 2c 20 28 69 6e 74 29 28 70 50 61 72 73 65  ql, (int)(pParse
2a3d4 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73  ->zTail-zSql), s
2a3d5 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a 20 20 7d  aveSqlFlag);.  }
2a3d6 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70  .  if( pParse->p
2a3d7 56 64 62 65 20 26 26 20 28 72 63 21 3d 53 51 4c  Vdbe && (rc!=SQL
2a3d8 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61  ITE_OK || db->ma
2a3d9 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0a 20  llocFailed) ){. 
2a3da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69     sqlite3VdbeFi
2a3db 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e 70  nalize(pParse->p
2a3dc 56 64 62 65 29 3b 0a 20 20 20 20 61 73 73 65 72  Vdbe);.    asser
2a3dd 74 28 21 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20  t(!(*ppStmt));. 
2a3de 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53   }else{.    *ppS
2a3df 74 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73  tmt = (sqlite3_s
2a3e0 74 6d 74 2a 29 70 50 61 72 73 65 2d 3e 70 56 64  tmt*)pParse->pVd
2a3e1 62 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a  be;.  }..  if( z
2a3e2 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 73 71  ErrMsg ){.    sq
2a3e3 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
2a3e4 63 2c 20 22 25 73 22 2c 20 7a 45 72 72 4d 73 67  c, "%s", zErrMsg
2a3e5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2a3e6 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
2a3e7 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2a3e8 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2a3e9 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20   rc, 0);.  }..  
2a3ea 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 54 72  /* Delete any Tr
2a3eb 69 67 67 65 72 50 72 67 20 73 74 72 75 63 74 75  iggerPrg structu
2a3ec 72 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 77 68  res allocated wh
2a3ed 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68 69 73  ile parsing this
2a3ee 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
2a3ef 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e   while( pParse->
2a3f0 70 54 72 69 67 67 65 72 50 72 67 20 29 7b 0a 20  pTriggerPrg ){. 
2a3f1 20 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70     TriggerPrg *p
2a3f2 54 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69  T = pParse->pTri
2a3f3 67 67 65 72 50 72 67 3b 0a 20 20 20 20 70 50 61  ggerPrg;.    pPa
2a3f4 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67  rse->pTriggerPrg
2a3f5 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b 0a 20 20   = pT->pNext;.  
2a3f6 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f    sqlite3VdbePro
2a3f7 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c 20 70  gramDelete(db, p
2a3f8 54 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 30 29 3b  T->pProgram, 0);
2a3f9 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2a3fa 65 65 28 64 62 2c 20 70 54 29 3b 0a 20 20 7d 0a  ee(db, pT);.  }.
2a3fb 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0a 0a 20  .end_prepare:.. 
2a3fc 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
2a3fd 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
2a3fe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
2a3ff 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
2a400 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
2a401 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
2a402 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73    return rc;.}.s
2a403 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
2a404 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
2a405 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2a406 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a407 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
2a408 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2a409 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
2a40a 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
2a40b 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
2a40c 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
2a40d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a40e 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
2a40f 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
2a410 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
2a411 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
2a412 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65  e to copy SQL te
2a413 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
2a414 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 56 64  te3_stmt */.  Vd
2a415 62 65 20 2a 70 4f 6c 64 2c 20 20 20 20 20 20 20  be *pOld,       
2a416 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 62 65          /* VM be
2a417 69 6e 67 20 72 65 70 72 65 70 61 72 65 64 20 2a  ing reprepared *
2a418 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2a419 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
2a41a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
2a41b 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
2a41c 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
2a41d 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
2a41e 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2a41f 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2a420 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2a421 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
2a422 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20 2a  ppStmt!=0 );.  *
2a423 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
2a424 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
2a425 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
2a426 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a427 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
2a428 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2a429 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2a42a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
2a42b 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20  erAll(db);.  rc 
2a42c 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  = sqlite3Prepare
2a42d 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65  (db, zSql, nByte
2a42e 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  s, saveSqlFlag, 
2a42f 70 4f 6c 64 2c 20 70 70 53 74 6d 74 2c 20 70 7a  pOld, ppStmt, pz
2a430 54 61 69 6c 29 3b 0a 20 20 69 66 28 20 72 63 3d  Tail);.  if( rc=
2a431 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
2a432 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  {.    sqlite3_fi
2a433 6e 61 6c 69 7a 65 28 2a 70 70 53 74 6d 74 29 3b  nalize(*ppStmt);
2a434 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2a435 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  3Prepare(db, zSq
2a436 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76 65 53  l, nBytes, saveS
2a437 71 6c 46 6c 61 67 2c 20 70 4f 6c 64 2c 20 70 70  qlFlag, pOld, pp
2a438 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20  Stmt, pzTail);. 
2a439 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
2a43a 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
2a43b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2a43c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2a43d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2a43e 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65  ./*.** Rerun the
2a43f 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20   compilation of 
2a440 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65  a statement afte
2a441 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  r a schema chang
2a442 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
2a443 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 75 63  statement is suc
2a444 63 65 73 73 66 75 6c 6c 79 20 72 65 63 6f 6d 70  cessfully recomp
2a445 69 6c 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c  iled, return SQL
2a446 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
2a447 65 2c 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 61  e,.** if the sta
2a448 74 65 6d 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65  tement cannot be
2a449 20 72 65 63 6f 6d 70 69 6c 65 64 20 62 65 63 61   recompiled beca
2a44a 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e  use another conn
2a44b 65 63 74 69 6f 6e 20 68 61 73 0a 2a 2a 20 6c 6f  ection has.** lo
2a44c 63 6b 65 64 20 74 68 65 20 73 71 6c 69 74 65 33  cked the sqlite3
2a44d 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 72  _master table, r
2a44e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
2a44f 4b 45 44 2e 20 49 66 20 61 6e 79 20 6f 74 68 65  KED. If any othe
2a450 72 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72  r error.** occur
2a451 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  s, return SQLITE
2a452 5f 53 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51 4c 49  _SCHEMA..*/.SQLI
2a453 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2a454 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28  qlite3Reprepare(
2a455 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
2a456 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  rc;.  sqlite3_st
2a457 6d 74 20 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73  mt *pNew;.  cons
2a458 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20  t char *zSql;.  
2a459 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
2a45a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
2a45b 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
2a45c 65 33 56 64 62 65 44 62 28 70 29 2d 3e 6d 75 74  e3VdbeDb(p)->mut
2a45d 65 78 29 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20  ex) );.  zSql = 
2a45e 73 71 6c 69 74 65 33 5f 73 71 6c 28 28 73 71 6c  sqlite3_sql((sql
2a45f 69 74 65 33 5f 73 74 6d 74 20 2a 29 70 29 3b 0a  ite3_stmt *)p);.
2a460 20 20 61 73 73 65 72 74 28 20 7a 53 71 6c 21 3d    assert( zSql!=
2a461 30 20 29 3b 20 20 2f 2a 20 52 65 70 72 65 70 61  0 );  /* Reprepa
2a462 72 65 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66  re only called f
2a463 6f 72 20 70 72 65 70 61 72 65 5f 76 32 28 29 20  or prepare_v2() 
2a464 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  statements */.  
2a465 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  db = sqlite3Vdbe
2a466 44 62 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  Db(p);.  assert(
2a467 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2a468 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
2a469 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2a46a 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
2a46b 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20  b, zSql, -1, 0, 
2a46c 70 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20 20  p, &pNew, 0);.  
2a46d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69 66  if( rc ){.    if
2a46e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
2a46f 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  EM ){.      db->
2a470 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2a471 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
2a472 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a 20  rt( pNew==0 );. 
2a473 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53     return (rc==S
2a474 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20 3f 20  QLITE_LOCKED) ? 
2a475 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 3a 20  SQLITE_LOCKED : 
2a476 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20  SQLITE_SCHEMA;. 
2a477 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
2a478 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20  rt( pNew!=0 );. 
2a479 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
2a47a 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e 65 77  Swap((Vdbe*)pNew
2a47b 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 54  , p);.  sqlite3T
2a47c 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28  ransferBindings(
2a47d 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33 5f 73  pNew, (sqlite3_s
2a47e 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74  tmt*)p);.  sqlit
2a47f 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 52  e3VdbeResetStepR
2a480 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70 4e 65  esult((Vdbe*)pNe
2a481 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  w);.  sqlite3Vdb
2a482 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a  eFinalize((Vdbe*
2a483 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e  )pNew);.  return
2a484 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
2a485 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f  /*.** Two versio
2a486 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69  ns of the offici
2a487 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20  al API.  Legacy 
2a488 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e  and new use.  In
2a489 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76   the legacy.** v
2a48a 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67  ersion, the orig
2a48b 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73  inal SQL text is
2a48c 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68   not saved in th
2a48d 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
2a48e 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69  ment.** and so i
2a48f 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  f a schema chang
2a490 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45  e occurs, SQLITE
2a491 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75 72  _SCHEMA is retur
2a492 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ned by.** sqlite
2a493 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68  3_step().  In th
2a494 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74  e new version, t
2a495 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
2a496 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65 64  text is retained
2a497 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74  .** and the stat
2a498 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74  ement is automat
2a499 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  ically recompile
2a49a 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63  d if an schema c
2a49b 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e  hange.** occurs.
2a49c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2a49d 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
2a49e 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  re(.  sqlite3 *d
2a49f 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2a4a0 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
2a4a1 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  le. */.  const c
2a4a2 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
2a4a3 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f     /* UTF-8 enco
2a4a4 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
2a4a5 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  t. */.  int nByt
2a4a6 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
2a4a7 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
2a4a8 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
2a4a9 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a4aa 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a4ab 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a4ac 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a4ad 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a4ae 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69  nst char **pzTai
2a4af 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a4b0 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a4b1 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
2a4b2 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   rc;.  rc = sqli
2a4b3 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72  te3LockAndPrepar
2a4b4 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73  e(db,zSql,nBytes
2a4b5 2c 30 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61  ,0,0,ppStmt,pzTa
2a4b6 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  il);.  assert( r
2a4b7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
2a4b8 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70  ppStmt==0 || *pp
2a4b9 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56  Stmt==0 );  /* V
2a4ba 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f  ERIFY: F13021 */
2a4bb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2a4bc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2a4bd 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
2a4be 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
2a4bf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a4c0 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
2a4c1 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
2a4c2 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
2a4c3 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
2a4c4 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
2a4c5 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
2a4c6 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
2a4c7 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
2a4c8 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
2a4c9 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
2a4ca 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f  *ppStmt,    /* O
2a4cb 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  UT: A pointer to
2a4cc 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
2a4cd 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e  atement */.  con
2a4ce 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
2a4cf 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
2a4d0 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72  nd of parsed str
2a4d1 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
2a4d2 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
2a4d3 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
2a4d4 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c  (db,zSql,nBytes,
2a4d5 31 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  1,0,ppStmt,pzTai
2a4d6 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
2a4d7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
2a4d8 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53  pStmt==0 || *ppS
2a4d9 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45  tmt==0 );  /* VE
2a4da 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a  RIFY: F13021 */.
2a4db 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2a4dc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a4dd 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
2a4de 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55 54 46   Compile the UTF
2a4df 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  -16 encoded SQL 
2a4e0 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69  statement zSql i
2a4e1 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74 20  nto a statement 
2a4e2 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  handle..*/.stati
2a4e3 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 72 65  c int sqlite3Pre
2a4e4 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65  pare16(.  sqlite
2a4e5 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2a4e6 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2a4e7 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f  handle. */ .  co
2a4e8 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20  nst void *zSql, 
2a4e9 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
2a4ea 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
2a4eb 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
2a4ec 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
2a4ed 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
2a4ee 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
2a4ef 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  s. */.  int save
2a4f0 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20  SqlFlag,        
2a4f1 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 61 76    /* True to sav
2a4f2 65 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20  e SQL text into 
2a4f3 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  the sqlite3_stmt
2a4f4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a4f5 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a4f6 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a4f7 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a4f8 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a4f9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
2a4fa 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a4fb 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a4fc 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a4fd 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
2a4fe 20 63 75 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73   currently works
2a4ff 20 62 79 20 66 69 72 73 74 20 74 72 61 6e 73 66   by first transf
2a500 6f 72 6d 69 6e 67 20 74 68 65 20 55 54 46 2d 31  orming the UTF-1
2a501 36 0a 20 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73  6.  ** encoded s
2a502 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20  tring to UTF-8, 
2a503 74 68 65 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71  then invoking sq
2a504 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 2e  lite3_prepare().
2a505 20 54 68 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79   The.  ** tricky
2a506 20 62 69 74 20 69 73 20 66 69 67 75 72 69 6e 67   bit is figuring
2a507 20 6f 75 74 20 74 68 65 20 70 6f 69 6e 74 65 72   out the pointer
2a508 20 74 6f 20 72 65 74 75 72 6e 20 69 6e 20 2a 70   to return in *p
2a509 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68  zTail..  */.  ch
2a50a 61 72 20 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e  ar *zSql8;.  con
2a50b 73 74 20 63 68 61 72 20 2a 7a 54 61 69 6c 38 20  st char *zTail8 
2a50c 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  = 0;.  int rc = 
2a50d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73  SQLITE_OK;..  as
2a50e 73 65 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a  sert( ppStmt );.
2a50f 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20    *ppStmt = 0;. 
2a510 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
2a511 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29  etyCheckOk(db) )
2a512 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2a513 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
2a514 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2a515 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2a516 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69  ;.  zSql8 = sqli
2a517 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20  te3Utf16to8(db, 
2a518 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20  zSql, nBytes);. 
2a519 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20   if( zSql8 ){.  
2a51a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
2a51b 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c  ckAndPrepare(db,
2a51c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65   zSql8, -1, save
2a51d 53 71 6c 46 6c 61 67 2c 20 30 2c 20 70 70 53 74  SqlFlag, 0, ppSt
2a51e 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20  mt, &zTail8);.  
2a51f 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c 38 20  }..  if( zTail8 
2a520 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20  && pzTail ){.   
2a521 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 70   /* If sqlite3_p
2a522 72 65 70 61 72 65 20 72 65 74 75 72 6e 73 20 61  repare returns a
2a523 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77   tail pointer, w
2a524 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 0a  e calculate the.
2a525 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
2a526 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  t pointer into t
2a527 68 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  he UTF-16 string
2a528 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74 68 65   by counting the
2a529 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20   unicode.    ** 
2a52a 63 68 61 72 61 63 74 65 72 73 20 62 65 74 77 65  characters betwe
2a52b 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61  en zSql8 and zTa
2a52c 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20 72 65  il8, and then re
2a52d 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
2a52e 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d  r.    ** the sam
2a52f 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
2a530 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  acters into the 
2a531 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20  UTF-16 string.. 
2a532 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68     */.    int ch
2a533 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73 71 6c  ars_parsed = sql
2a534 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
2a535 7a 53 71 6c 38 2c 20 28 69 6e 74 29 28 7a 54 61  zSql8, (int)(zTa
2a536 69 6c 38 2d 7a 53 71 6c 38 29 29 3b 0a 20 20 20  il8-zSql8));.   
2a537 20 2a 70 7a 54 61 69 6c 20 3d 20 28 75 38 20 2a   *pzTail = (u8 *
2a538 29 7a 53 71 6c 20 2b 20 73 71 6c 69 74 65 33 55  )zSql + sqlite3U
2a539 74 66 31 36 42 79 74 65 4c 65 6e 28 7a 53 71 6c  tf16ByteLen(zSql
2a53a 2c 20 63 68 61 72 73 5f 70 61 72 73 65 64 29 3b  , chars_parsed);
2a53b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
2a53c 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 38 29 3b  Free(db, zSql8);
2a53d 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33   .  rc = sqlite3
2a53e 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
2a53f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2a540 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2a541 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2a542 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72  }../*.** Two ver
2a543 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66  sions of the off
2a544 69 63 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61  icial API.  Lega
2a545 63 79 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20  cy and new use. 
2a546 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a   In the legacy.*
2a547 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f  * version, the o
2a548 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74  riginal SQL text
2a549 20 69 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e   is not saved in
2a54a 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74   the prepared st
2a54b 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73  atement.** and s
2a54c 6f 20 69 66 20 61 20 73 63 68 65 6d 61 20 63 68  o if a schema ch
2a54d 61 6e 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c  ange occurs, SQL
2a54e 49 54 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65  ITE_SCHEMA is re
2a54f 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c  turned by.** sql
2a550 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e  ite3_step().  In
2a551 20 74 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e   the new version
2a552 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  , the original S
2a553 51 4c 20 74 65 78 74 20 69 73 20 72 65 74 61 69  QL text is retai
2a554 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  ned.** and the s
2a555 74 61 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f  tatement is auto
2a556 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70  matically recomp
2a557 69 6c 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d  iled if an schem
2a558 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75  a change.** occu
2a559 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rs..*/.SQLITE_AP
2a55a 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
2a55b 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74  epare16(.  sqlit
2a55c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2a55d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2a55e 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63   handle. */ .  c
2a55f 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c  onst void *zSql,
2a560 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
2a561 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
2a562 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
2a563 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
2a564 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
2a565 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
2a566 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
2a567 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
2a568 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69     /* OUT: A poi
2a569 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70  nter to the prep
2a56a 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a  ared statement *
2a56b 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
2a56c 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a  *pzTail       /*
2a56d 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72   OUT: End of par
2a56e 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  sed string */.){
2a56f 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20  .  int rc;.  rc 
2a570 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  = sqlite3Prepare
2a571 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  16(db,zSql,nByte
2a572 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  s,0,ppStmt,pzTai
2a573 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
2a574 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
2a575 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53  pStmt==0 || *ppS
2a576 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45  tmt==0 );  /* VE
2a577 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a  RIFY: F13021 */.
2a578 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
2a579 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2a57a 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
2a57b 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
2a57c 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2a57d 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
2a57e 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20  le. */ .  const 
2a57f 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
2a580 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
2a581 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
2a582 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
2a583 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2a584 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2a585 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
2a586 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2a587 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
2a588 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
2a589 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
2a58a 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
2a58b 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
2a58c 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2a58d 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2a58e 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2a58f 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
2a590 69 74 65 33 50 72 65 70 61 72 65 31 36 28 64 62  ite3Prepare16(db
2a591 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 70  ,zSql,nBytes,1,p
2a592 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20  pStmt,pzTail);. 
2a593 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2a594 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74  ITE_OK || ppStmt
2a595 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d  ==0 || *ppStmt==
2a596 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a  0 );  /* VERIFY:
2a597 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74   F13021 */.  ret
2a598 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69  urn rc;.}..#endi
2a599 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2a59a 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _UTF16 */../****
2a59b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2a59c 66 20 70 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a  f prepare.c ****
2a59d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a59e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a59f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2a5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2a5a1 20 66 69 6c 65 20 73 65 6c 65 63 74 2e 63 20 2a   file select.c *
2a5a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2a5a5 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
2a5a6 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2a5a7 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2a5a8 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2a5a9 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2a5aa 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2a5ab 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2a5ac 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2a5ad 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2a5ae 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2a5af 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2a5b0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2a5b1 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2a5b2 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2a5b3 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2a5b4 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2a5b5 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2a5b6 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2a5b7 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2a5b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5bc 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2a5bd 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
2a5be 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
2a5bf 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
2a5c0 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64  arser.** to hand
2a5c1 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  le SELECT statem
2a5c2 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ents in SQLite..
2a5c3 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  */.../*.** Delet
2a5c4 65 20 61 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e  e all the conten
2a5c5 74 20 6f 66 20 61 20 53 65 6c 65 63 74 20 73 74  t of a Select st
2a5c6 72 75 63 74 75 72 65 20 62 75 74 20 64 6f 20 6e  ructure but do n
2a5c7 6f 74 20 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a  ot deallocate.**
2a5c8 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 72 75   the select stru
2a5c9 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2f  cture itself..*/
2a5ca 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65  .static void cle
2a5cb 61 72 53 65 6c 65 63 74 28 73 71 6c 69 74 65 33  arSelect(sqlite3
2a5cc 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29   *db, Select *p)
2a5cd 7b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  {.  sqlite3ExprL
2a5ce 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
2a5cf 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  >pEList);.  sqli
2a5d0 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2a5d1 28 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20  (db, p->pSrc);. 
2a5d2 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2a5d3 74 65 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  te(db, p->pWhere
2a5d4 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2a5d5 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2a5d6 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 73  ->pGroupBy);.  s
2a5d7 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2a5d8 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  (db, p->pHaving)
2a5d9 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
2a5da 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
2a5db 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71  >pOrderBy);.  sq
2a5dc 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
2a5dd 65 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29  e(db, p->pPrior)
2a5de 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
2a5df 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69  elete(db, p->pLi
2a5e0 6d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  mit);.  sqlite3E
2a5e1 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2a5e2 3e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a  >pOffset);.}../*
2a5e3 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
2a5e4 20 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75   SelectDest stru
2a5e5 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
2a5e6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a5e7 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
2a5e8 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 20 2a  nit(SelectDest *
2a5e9 70 44 65 73 74 2c 20 69 6e 74 20 65 44 65 73 74  pDest, int eDest
2a5ea 2c 20 69 6e 74 20 69 50 61 72 6d 29 7b 0a 20 20  , int iParm){.  
2a5eb 70 44 65 73 74 2d 3e 65 44 65 73 74 20 3d 20 28  pDest->eDest = (
2a5ec 75 38 29 65 44 65 73 74 3b 0a 20 20 70 44 65 73  u8)eDest;.  pDes
2a5ed 74 2d 3e 69 50 61 72 6d 20 3d 20 69 50 61 72 6d  t->iParm = iParm
2a5ee 3b 0a 20 20 70 44 65 73 74 2d 3e 61 66 66 69 6e  ;.  pDest->affin
2a5ef 69 74 79 20 3d 20 30 3b 0a 20 20 70 44 65 73 74  ity = 0;.  pDest
2a5f0 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 44  ->iMem = 0;.  pD
2a5f1 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 7d  est->nMem = 0;.}
2a5f2 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  .../*.** Allocat
2a5f3 65 20 61 20 6e 65 77 20 53 65 6c 65 63 74 20 73  e a new Select s
2a5f4 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74  tructure and ret
2a5f5 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2a5f6 20 74 68 61 74 0a 2a 2a 20 73 74 72 75 63 74 75   that.** structu
2a5f7 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
2a5f8 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
2a5f9 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 0a  lite3SelectNew(.
2a5fa 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2a5fb 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2a5fc 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2a5fd 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2a5fe 2c 20 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63  ,     /* which c
2a5ff 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64  olumns to includ
2a600 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  e in the result 
2a601 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
2a602 72 63 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68  rc,        /* th
2a603 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d  e FROM clause --
2a604 20 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f   which tables to
2a605 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20   scan */.  Expr 
2a606 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
2a607 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c   /* the WHERE cl
2a608 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ause */.  ExprLi
2a609 73 74 20 2a 70 47 72 6f 75 70 42 79 2c 20 20 20  st *pGroupBy,   
2a60a 2f 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  /* the GROUP BY 
2a60b 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
2a60c 20 2a 70 48 61 76 69 6e 67 2c 20 20 20 20 20 20   *pHaving,      
2a60d 20 20 2f 2a 20 74 68 65 20 48 41 56 49 4e 47 20    /* the HAVING 
2a60e 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
2a60f 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2a610 20 20 2f 2a 20 74 68 65 20 4f 52 44 45 52 20 42    /* the ORDER B
2a611 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  Y clause */.  in
2a612 74 20 69 73 44 69 73 74 69 6e 63 74 2c 20 20 20  t isDistinct,   
2a613 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74      /* true if t
2a614 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
2a615 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2a616 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74  /.  Expr *pLimit
2a617 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d  ,         /* LIM
2a618 49 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20  IT value.  NULL 
2a619 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a  means not used *
2a61a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65  /.  Expr *pOffse
2a61b 74 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46  t         /* OFF
2a61c 53 45 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c  SET value.  NULL
2a61d 20 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74   means no offset
2a61e 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20   */.){.  Select 
2a61f 2a 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 20  *pNew;.  Select 
2a620 73 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 74  standin;.  sqlit
2a621 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2a622 3e 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  >db;.  pNew = sq
2a623 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2a624 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e  o(db, sizeof(*pN
2a625 65 77 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ew) );.  assert(
2a626 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2a627 64 20 7c 7c 20 21 70 4f 66 66 73 65 74 20 7c 7c  d || !pOffset ||
2a628 20 70 4c 69 6d 69 74 20 29 3b 20 2f 2a 20 4f 46   pLimit ); /* OF
2a629 46 53 45 54 20 69 6d 70 6c 69 65 73 20 4c 49 4d  FSET implies LIM
2a62a 49 54 20 2a 2f 0a 20 20 69 66 28 20 70 4e 65 77  IT */.  if( pNew
2a62b 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 20  ==0 ){.    pNew 
2a62c 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20 20 20  = &standin;.    
2a62d 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20  memset(pNew, 0, 
2a62e 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a  sizeof(*pNew));.
2a62f 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69 73 74    }.  if( pEList
2a630 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c 69 73  ==0 ){.    pELis
2a631 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
2a632 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2a633 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72  , 0, sqlite3Expr
2a634 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 29 29 3b 0a  (db,TK_ALL,0));.
2a635 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69    }.  pNew->pELi
2a636 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70  st = pEList;.  p
2a637 4e 65 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63  New->pSrc = pSrc
2a638 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65  ;.  pNew->pWhere
2a639 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65   = pWhere;.  pNe
2a63a 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47  w->pGroupBy = pG
2a63b 72 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e  roupBy;.  pNew->
2a63c 70 48 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e  pHaving = pHavin
2a63d 67 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65  g;.  pNew->pOrde
2a63e 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a  rBy = pOrderBy;.
2a63f 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73    pNew->selFlags
2a640 20 3d 20 69 73 44 69 73 74 69 6e 63 74 20 3f 20   = isDistinct ? 
2a641 53 46 5f 44 69 73 74 69 6e 63 74 20 3a 20 30 3b  SF_Distinct : 0;
2a642 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b  .  pNew->op = TK
2a643 5f 53 45 4c 45 43 54 3b 0a 20 20 70 4e 65 77 2d  _SELECT;.  pNew-
2a644 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74  >pLimit = pLimit
2a645 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65  ;.  pNew->pOffse
2a646 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 61  t = pOffset;.  a
2a647 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 3d 3d  ssert( pOffset==
2a648 30 20 7c 7c 20 70 4c 69 6d 69 74 21 3d 30 20 29  0 || pLimit!=0 )
2a649 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
2a64a 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a  enEphm[0] = -1;.
2a64b 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
2a64c 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[1] = -1;.  
2a64d 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
2a64e 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 69 66  hm[2] = -1;.  if
2a64f 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2a650 65 64 20 29 20 7b 0a 20 20 20 20 63 6c 65 61 72  ed ) {.    clear
2a651 53 65 6c 65 63 74 28 64 62 2c 20 70 4e 65 77 29  Select(db, pNew)
2a652 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 21 3d  ;.    if( pNew!=
2a653 26 73 74 61 6e 64 69 6e 20 29 20 73 71 6c 69 74  &standin ) sqlit
2a654 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65  e3DbFree(db, pNe
2a655 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 30  w);.    pNew = 0
2a656 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
2a657 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  New;.}../*.** De
2a658 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e 20 53  lete the given S
2a659 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
2a65a 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73  and all of its s
2a65b 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  ubstructures..*/
2a65c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a65d 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
2a65e 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  ctDelete(sqlite3
2a65f 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29   *db, Select *p)
2a660 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
2a661 20 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c   clearSelect(db,
2a662 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   p);.    sqlite3
2a663 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20  DbFree(db, p);. 
2a664 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
2a665 6e 20 31 20 74 6f 20 33 20 69 64 65 6e 74 69 66  n 1 to 3 identif
2a666 69 65 72 73 20 70 72 65 63 65 65 64 69 6e 67 20  iers preceeding 
2a667 74 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64  the JOIN keyword
2a668 2c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 0a  , determine the.
2a669 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e  ** type of join.
2a66a 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65    Return an inte
2a66b 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 61  ger constant tha
2a66c 74 20 65 78 70 72 65 73 73 65 73 20 74 68 61 74  t expresses that
2a66d 20 74 79 70 65 0a 2a 2a 20 69 6e 20 74 65 72 6d   type.** in term
2a66e 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
2a66f 6e 67 20 62 69 74 20 76 61 6c 75 65 73 3a 0a 2a  ng bit values:.*
2a670 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e 45  *.**     JT_INNE
2a671 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 43 52 4f 53  R.**     JT_CROS
2a672 53 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f 55 54 45  S.**     JT_OUTE
2a673 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e 41 54 55  R.**     JT_NATU
2a674 52 41 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c 45  RAL.**     JT_LE
2a675 46 54 0a 2a 2a 20 20 20 20 20 4a 54 5f 52 49 47  FT.**     JT_RIG
2a676 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20  HT.**.** A full 
2a677 6f 75 74 65 72 20 6a 6f 69 6e 20 69 73 20 74 68  outer join is th
2a678 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  e combination of
2a679 20 4a 54 5f 4c 45 46 54 20 61 6e 64 20 4a 54 5f   JT_LEFT and JT_
2a67a 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  RIGHT..**.** If 
2a67b 61 6e 20 69 6c 6c 65 67 61 6c 20 6f 72 20 75 6e  an illegal or un
2a67c 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74  supported join t
2a67d 79 70 65 20 69 73 20 73 65 65 6e 2c 20 74 68 65  ype is seen, the
2a67e 6e 20 73 74 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  n still return.*
2a67f 2a 20 61 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62  * a join type, b
2a680 75 74 20 70 75 74 20 61 6e 20 65 72 72 6f 72 20  ut put an error 
2a681 69 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74  in the pParse st
2a682 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
2a683 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2a684 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 50  qlite3JoinType(P
2a685 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
2a686 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a  ken *pA, Token *
2a687 70 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a  pB, Token *pC){.
2a688 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20 3d    int jointype =
2a689 20 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61 70 41   0;.  Token *apA
2a68a 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a  ll[3];.  Token *
2a68b 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p;.             
2a68c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a68d 2f 2a 20 20 20 30 31 32 33 34 35 36 37 38 39 20  /*   0123456789 
2a68e 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36  123456789 123456
2a68f 37 38 39 20 31 32 33 20 2a 2f 0a 20 20 73 74 61  789 123 */.  sta
2a690 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
2a691 4b 65 79 54 65 78 74 5b 5d 20 3d 20 22 6e 61 74  KeyText[] = "nat
2a692 75 72 61 6c 65 66 74 6f 75 74 65 72 69 67 68 74  uraleftouteright
2a693 66 75 6c 6c 69 6e 6e 65 72 63 72 6f 73 73 22 3b  fullinnercross";
2a694 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
2a695 73 74 72 75 63 74 20 7b 0a 20 20 20 20 75 38 20  struct {.    u8 
2a696 69 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67  i;        /* Beg
2a697 69 6e 6e 69 6e 67 20 6f 66 20 6b 65 79 77 6f 72  inning of keywor
2a698 64 20 74 65 78 74 20 69 6e 20 7a 4b 65 79 54 65  d text in zKeyTe
2a699 78 74 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 6e  xt[] */.    u8 n
2a69a 43 68 61 72 3b 20 20 20 20 2f 2a 20 4c 65 6e 67  Char;    /* Leng
2a69b 74 68 20 6f 66 20 74 68 65 20 6b 65 79 77 6f 72  th of the keywor
2a69c 64 20 69 6e 20 63 68 61 72 61 63 74 65 72 73 20  d in characters 
2a69d 2a 2f 0a 20 20 20 20 75 38 20 63 6f 64 65 3b 20  */.    u8 code; 
2a69e 20 20 20 20 2f 2a 20 4a 6f 69 6e 20 74 79 70 65      /* Join type
2a69f 20 6d 61 73 6b 20 2a 2f 0a 20 20 7d 20 61 4b 65   mask */.  } aKe
2a6a0 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 20  yword[] = {.    
2a6a1 2f 2a 20 6e 61 74 75 72 61 6c 20 2a 2f 20 7b 20  /* natural */ { 
2a6a2 30 2c 20 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41  0,  7, JT_NATURA
2a6a3 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
2a6a4 20 7d 2c 0a 20 20 20 20 2f 2a 20 6c 65 66 74 20   },.    /* left 
2a6a5 20 20 20 2a 2f 20 7b 20 36 2c 20 20 34 2c 20 4a     */ { 6,  4, J
2a6a6 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20  T_LEFT|JT_OUTER 
2a6a7 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
2a6a8 2f 2a 20 6f 75 74 65 72 20 20 20 2a 2f 20 7b 20  /* outer   */ { 
2a6a9 31 30 2c 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20  10, 5, JT_OUTER 
2a6aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a6ab 20 7d 2c 0a 20 20 20 20 2f 2a 20 72 69 67 68 74   },.    /* right
2a6ac 20 20 20 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 4a     */ { 14, 5, J
2a6ad 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52  T_RIGHT|JT_OUTER
2a6ae 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
2a6af 2f 2a 20 66 75 6c 6c 20 20 20 20 2a 2f 20 7b 20  /* full    */ { 
2a6b0 31 39 2c 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a  19, 4, JT_LEFT|J
2a6b1 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52  T_RIGHT|JT_OUTER
2a6b2 20 7d 2c 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 72   },.    /* inner
2a6b3 20 20 20 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 4a     */ { 23, 5, J
2a6b4 54 5f 49 4e 4e 45 52 20 20 20 20 20 20 20 20 20  T_INNER         
2a6b5 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20           },.    
2a6b6 2f 2a 20 63 72 6f 73 73 20 20 20 2a 2f 20 7b 20  /* cross   */ { 
2a6b7 32 38 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c  28, 5, JT_INNER|
2a6b8 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 20 20 20  JT_CROSS        
2a6b9 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69   },.  };.  int i
2a6ba 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d 20  , j;.  apAll[0] 
2a6bb 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d  = pA;.  apAll[1]
2a6bc 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b 32   = pB;.  apAll[2
2a6bd 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69 3d  ] = pC;.  for(i=
2a6be 30 3b 20 69 3c 33 20 26 26 20 61 70 41 6c 6c 5b  0; i<3 && apAll[
2a6bf 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  i]; i++){.    p 
2a6c0 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20  = apAll[i];.    
2a6c1 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79  for(j=0; j<Array
2a6c2 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 3b 20  Size(aKeyword); 
2a6c3 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
2a6c4 70 2d 3e 6e 3d 3d 61 4b 65 79 77 6f 72 64 5b 6a  p->n==aKeyword[j
2a6c5 5d 2e 6e 43 68 61 72 20 0a 20 20 20 20 20 20 20  ].nChar .       
2a6c6 20 20 20 26 26 20 73 71 6c 69 74 65 33 53 74 72     && sqlite3Str
2a6c7 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70 2d 3e  NICmp((char*)p->
2a6c8 7a 2c 20 26 7a 4b 65 79 54 65 78 74 5b 61 4b 65  z, &zKeyText[aKe
2a6c9 79 77 6f 72 64 5b 6a 5d 2e 69 5d 2c 20 70 2d 3e  yword[j].i], p->
2a6ca 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  n)==0 ){.       
2a6cb 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 61 4b 65   jointype |= aKe
2a6cc 79 77 6f 72 64 5b 6a 5d 2e 63 6f 64 65 3b 0a 20  yword[j].code;. 
2a6cd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a6ce 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2a6cf 74 65 73 74 63 61 73 65 28 20 6a 3d 3d 30 20 7c  testcase( j==0 |
2a6d0 7c 20 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c  | j==1 || j==2 |
2a6d1 7c 20 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c  | j==3 || j==4 |
2a6d2 7c 20 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29  | j==5 || j==6 )
2a6d3 3b 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 72  ;.    if( j>=Arr
2a6d4 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29  aySize(aKeyword)
2a6d5 20 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79   ){.      jointy
2a6d6 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a  pe |= JT_ERROR;.
2a6d7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a6d8 20 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 20   }.  }.  if(.   
2a6d9 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a    (jointype & (J
2a6da 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52  T_INNER|JT_OUTER
2a6db 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54  ))==(JT_INNER|JT
2a6dc 5f 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 20  _OUTER) ||.     
2a6dd 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 45  (jointype & JT_E
2a6de 52 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20  RROR)!=0.  ){.  
2a6df 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2a6e0 70 20 3d 20 22 20 22 3b 0a 20 20 20 20 61 73 73  p = " ";.    ass
2a6e1 65 72 74 28 20 70 42 21 3d 30 20 29 3b 0a 20 20  ert( pB!=0 );.  
2a6e2 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a    if( pC==0 ){ z
2a6e3 53 70 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69  Sp++; }.    sqli
2a6e4 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2a6e5 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20  se, "unknown or 
2a6e6 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e  unsupported join
2a6e7 20 74 79 70 65 3a 20 22 0a 20 20 20 20 20 20 20   type: ".       
2a6e8 22 25 54 20 25 54 25 73 25 54 22 2c 20 70 41 2c  "%T %T%s%T", pA,
2a6e9 20 70 42 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20   pB, zSp, pC);. 
2a6ea 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54     jointype = JT
2a6eb 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20  _INNER;.  }else 
2a6ec 69 66 28 20 28 6a 6f 69 6e 74 79 70 65 20 26 20  if( (jointype & 
2a6ed 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 0a 20 20  JT_OUTER)!=0 .  
2a6ee 20 20 20 20 20 20 20 26 26 20 28 6a 6f 69 6e 74         && (joint
2a6ef 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a  ype & (JT_LEFT|J
2a6f0 54 5f 52 49 47 48 54 29 29 21 3d 4a 54 5f 4c 45  T_RIGHT))!=JT_LE
2a6f1 46 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  FT ){.    sqlite
2a6f2 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2a6f3 2c 20 0a 20 20 20 20 20 20 22 52 49 47 48 54 20  , .      "RIGHT 
2a6f4 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a  and FULL OUTER J
2a6f5 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72  OINs are not cur
2a6f6 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64  rently supported
2a6f7 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65  ");.    jointype
2a6f8 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d   = JT_INNER;.  }
2a6f9 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79  .  return jointy
2a6fa 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  pe;.}../*.** Ret
2a6fb 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
2a6fc 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74   a column in a t
2a6fd 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31  able.  Return -1
2a6fe 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a   if the column.*
2a6ff 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  * is not contain
2a700 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ed in the table.
2a701 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2a702 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65  olumnIndex(Table
2a703 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68   *pTab, const ch
2a704 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74  ar *zCol){.  int
2a705 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
2a706 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
2a707 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
2a708 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e  e3StrICmp(pTab->
2a709 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a  aCol[i].zName, z
2a70a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  Col)==0 ) return
2a70b 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   i;.  }.  return
2a70c 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   -1;.}../*.** Th
2a70d 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
2a70e 73 65 64 20 74 6f 20 61 64 64 20 74 65 72 6d 73  sed to add terms
2a70f 20 69 6d 70 6c 69 65 64 20 62 79 20 4a 4f 49 4e   implied by JOIN
2a710 20 73 79 6e 74 61 78 20 74 6f 20 74 68 65 0a 2a   syntax to the.*
2a711 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65  * WHERE clause e
2a712 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 61 20 53  xpression of a S
2a713 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2a714 20 54 68 65 20 6e 65 77 20 74 65 72 6d 2c 20 77   The new term, w
2a715 68 69 63 68 0a 2a 2a 20 69 73 20 41 4e 44 65 64  hich.** is ANDed
2a716 20 77 69 74 68 20 74 68 65 20 65 78 69 73 74 69   with the existi
2a717 6e 67 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c  ng WHERE clause,
2a718 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a   is of the form:
2a719 0a 2a 2a 0a 2a 2a 20 20 20 20 28 74 61 62 31 2e  .**.**    (tab1.
2a71a 63 6f 6c 31 20 3d 20 74 61 62 32 2e 63 6f 6c 32  col1 = tab2.col2
2a71b 29 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 74 61  ).**.** where ta
2a71c 62 31 20 69 73 20 74 68 65 20 69 53 72 63 27 74  b1 is the iSrc't
2a71d 68 20 74 61 62 6c 65 20 69 6e 20 53 72 63 4c 69  h table in SrcLi
2a71e 73 74 20 70 53 72 63 20 61 6e 64 20 74 61 62 32  st pSrc and tab2
2a71f 20 69 73 20 74 68 65 20 0a 2a 2a 20 28 69 53 72   is the .** (iSr
2a720 63 2b 31 29 27 74 68 2e 20 43 6f 6c 75 6d 6e 20  c+1)'th. Column 
2a721 63 6f 6c 31 20 69 73 20 63 6f 6c 75 6d 6e 20 69  col1 is column i
2a722 43 6f 6c 4c 65 66 74 20 6f 66 20 74 61 62 31 2c  ColLeft of tab1,
2a723 20 61 6e 64 20 63 6f 6c 32 20 69 73 0a 2a 2a 20   and col2 is.** 
2a724 63 6f 6c 75 6d 6e 20 69 43 6f 6c 52 69 67 68 74  column iColRight
2a725 20 6f 66 20 74 61 62 32 2e 0a 2a 2f 0a 73 74 61   of tab2..*/.sta
2a726 74 69 63 20 76 6f 69 64 20 61 64 64 57 68 65 72  tic void addWher
2a727 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a  eTerm(.  Parse *
2a728 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2a729 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2a72a 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2a72b 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
2a72c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a72d 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
2a72e 65 73 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75 73  es in FROM claus
2a72f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c  e */.  int iSrc,
2a730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a731 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2a732 6f 66 20 66 69 72 73 74 20 74 61 62 6c 65 20 74  of first table t
2a733 6f 20 6a 6f 69 6e 20 69 6e 20 70 53 72 63 20 2a  o join in pSrc *
2a734 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65 66 74  /.  int iColLeft
2a735 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a736 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2a737 63 6f 6c 75 6d 6e 20 69 6e 20 66 69 72 73 74 20  column in first 
2a738 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
2a739 43 6f 6c 52 69 67 68 74 2c 20 20 20 20 20 20 20  ColRight,       
2a73a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2a73b 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  dex of column in
2a73c 20 73 65 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f   second table */
2a73d 0a 20 20 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f  .  int isOuterJo
2a73e 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  in,             
2a73f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2a740 69 73 20 69 73 20 61 6e 20 4f 55 54 45 52 20 6a  is is an OUTER j
2a741 6f 69 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a  oin */.  Expr **
2a742 70 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20  ppWhere         
2a743 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
2a744 55 54 3a 20 54 68 65 20 57 48 45 52 45 20 63 6c  UT: The WHERE cl
2a745 61 75 73 65 20 74 6f 20 61 64 64 20 74 6f 20 2a  ause to add to *
2a746 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
2a747 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2a748 0a 20 20 45 78 70 72 20 2a 70 45 31 3b 0a 20 20  .  Expr *pE1;.  
2a749 45 78 70 72 20 2a 70 45 32 3b 0a 20 20 45 78 70  Expr *pE2;.  Exp
2a74a 72 20 2a 70 45 71 3b 0a 0a 20 20 61 73 73 65 72  r *pEq;..  asser
2a74b 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 28 69  t( pSrc->nSrc>(i
2a74c 53 72 63 2b 31 29 20 29 3b 0a 20 20 61 73 73 65  Src+1) );.  asse
2a74d 72 74 28 20 70 53 72 63 2d 3e 61 5b 69 53 72 63  rt( pSrc->a[iSrc
2a74e 5d 2e 70 54 61 62 20 29 3b 0a 20 20 61 73 73 65  ].pTab );.  asse
2a74f 72 74 28 20 70 53 72 63 2d 3e 61 5b 69 53 72 63  rt( pSrc->a[iSrc
2a750 2b 31 5d 2e 70 54 61 62 20 29 3b 0a 0a 20 20 70  +1].pTab );..  p
2a751 45 31 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  E1 = sqlite3Crea
2a752 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c  teColumnExpr(db,
2a753 20 70 53 72 63 2c 20 69 53 72 63 2c 20 69 43 6f   pSrc, iSrc, iCo
2a754 6c 4c 65 66 74 29 3b 0a 20 20 70 45 32 20 3d 20  lLeft);.  pE2 = 
2a755 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c  sqlite3CreateCol
2a756 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53 72 63  umnExpr(db, pSrc
2a757 2c 20 69 53 72 63 2b 31 2c 20 69 43 6f 6c 52 69  , iSrc+1, iColRi
2a758 67 68 74 29 3b 0a 0a 20 20 70 45 71 20 3d 20 73  ght);..  pEq = s
2a759 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2a75a 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45 31 2c 20  se, TK_EQ, pE1, 
2a75b 70 45 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  pE2, 0);.  if( p
2a75c 45 71 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69  Eq && isOuterJoi
2a75d 6e 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74  n ){.    ExprSet
2a75e 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50  Property(pEq, EP
2a75f 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20  _FromJoin);.    
2a760 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2a761 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 71 2c  AnyProperty(pEq,
2a762 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50   EP_TokenOnly|EP
2a763 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20  _Reduced) );.   
2a764 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
2a765 62 6c 65 28 70 45 71 29 3b 0a 20 20 20 20 70 45  ble(pEq);.    pE
2a766 71 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  q->iRightJoinTab
2a767 6c 65 20 3d 20 28 69 31 36 29 70 45 32 2d 3e 69  le = (i16)pE2->i
2a768 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70  Table;.  }.  *pp
2a769 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
2a76a 78 70 72 41 6e 64 28 64 62 2c 20 2a 70 70 57 68  xprAnd(db, *ppWh
2a76b 65 72 65 2c 20 70 45 71 29 3b 0a 7d 0a 0a 2f 2a  ere, pEq);.}../*
2a76c 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50 5f 46  .** Set the EP_F
2a76d 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79  romJoin property
2a76e 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66   on all terms of
2a76f 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
2a770 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65  ssion..** And se
2a771 74 20 74 68 65 20 45 78 70 72 2e 69 52 69 67 68  t the Expr.iRigh
2a772 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20 69 54  tJoinTable to iT
2a773 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79 20 74  able for every t
2a774 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 65 78  erm in the.** ex
2a775 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  pression..**.** 
2a776 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20  The EP_FromJoin 
2a777 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64  property is used
2a778 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61 6e 20   on terms of an 
2a779 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 74 65  expression to te
2a77a 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54 20 4f  ll.** the LEFT O
2a77b 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63 65 73  UTER JOIN proces
2a77c 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61 74 20  sing logic that 
2a77d 74 68 69 73 20 74 65 72 6d 20 69 73 20 70 61 72  this term is par
2a77e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e  t of the.** join
2a77f 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73 70 65   restriction spe
2a780 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 4f 4e  cified in the ON
2a781 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
2a782 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72 74 0a   and not a part.
2a783 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 67  ** of the more g
2a784 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63 6c 61  eneral WHERE cla
2a785 75 73 65 2e 20 20 54 68 65 73 65 20 74 65 72 6d  use.  These term
2a786 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76 65 72  s are moved over
2a787 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45 52 45   to the.** WHERE
2a788 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67 20 6a   clause during j
2a789 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 62  oin processing b
2a78a 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  ut we need to re
2a78b 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68 65 79  member that they
2a78c 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64 20 69  .** originated i
2a78d 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e  n the ON or USIN
2a78e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  G clause..**.** 
2a78f 54 68 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a  The Expr.iRightJ
2a790 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73 20 74  oinTable tells t
2a791 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2a792 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61 74 20  processing that 
2a793 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  the.** expressio
2a794 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 61 62  n depends on tab
2a795 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  le iRightJoinTab
2a796 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  le even if that 
2a797 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a 20  table is not.** 
2a798 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74 69  explicitly menti
2a799 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78 70 72  oned in the expr
2a79a 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20 69 6e  ession.  That in
2a79b 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65  formation is nee
2a79c 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73 65 73  ded.** for cases
2a79d 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
2a79e 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  *    SELECT * FR
2a79f 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20  OM t1 LEFT JOIN 
2a7a0 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62 20  t2 ON t1.a=t2.b 
2a7a1 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a  AND t1.x=5.**.**
2a7a2 20 54 68 65 20 77 68 65 72 65 20 63 6c 61 75 73   The where claus
2a7a3 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66 65 72  e needs to defer
2a7a4 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66   the handling of
2a7a5 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a 20 74   the t1.x=5.** t
2a7a6 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65 72 20  erm until after 
2a7a7 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66 20 74  the t2 loop of t
2a7a8 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74 68 61  he join.  In tha
2a7a9 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c  t way, a.** NULL
2a7aa 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62 65 20   t2 row will be 
2a7ab 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65 76 65  inserted wheneve
2a7ac 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66 20 77  r t1.x!=5.  If w
2a7ad 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66 65  e do not.** defe
2a7ae 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f  r the handling o
2a7af 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77 69 6c  f t1.x=5, it wil
2a7b0 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64 20 69  l be processed i
2a7b1 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 61 66  mmediately.** af
2a7b2 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f 70 20  ter the t1 loop 
2a7b3 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20 74 31  and rows with t1
2a7b4 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76 65 72  .x!=5 will never
2a7b5 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20 74 68   appear in.** th
2a7b6 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63 68 20  e output, which 
2a7b7 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f  is incorrect..*/
2a7b8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
2a7b9 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20 2a 70  JoinExpr(Expr *p
2a7ba 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a 20  , int iTable){. 
2a7bb 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
2a7bc 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
2a7bd 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  (p, EP_FromJoin)
2a7be 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45  ;.    assert( !E
2a7bf 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
2a7c0 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  y(p, EP_TokenOnl
2a7c1 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
2a7c2 0a 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65  .    ExprSetIrre
2a7c3 64 75 63 69 62 6c 65 28 70 29 3b 0a 20 20 20 20  ducible(p);.    
2a7c4 70 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  p->iRightJoinTab
2a7c5 6c 65 20 3d 20 28 69 31 36 29 69 54 61 62 6c 65  le = (i16)iTable
2a7c6 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45 78 70  ;.    setJoinExp
2a7c7 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62  r(p->pLeft, iTab
2a7c8 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e  le);.    p = p->
2a7c9 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a  pRight;.  } .}..
2a7ca 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2a7cb 6e 65 20 70 72 6f 63 65 73 73 65 73 20 74 68 65  ne processes the
2a7cc 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f   join informatio
2a7cd 6e 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 73  n for a SELECT s
2a7ce 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20  tatement..** ON 
2a7cf 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
2a7d0 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  s are converted 
2a7d1 69 6e 74 6f 20 65 78 74 72 61 20 74 65 72 6d 73  into extra terms
2a7d2 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
2a7d3 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c  ause..** NATURAL
2a7d4 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72 65 61   joins also crea
2a7d5 74 65 20 65 78 74 72 61 20 57 48 45 52 45 20 63  te extra WHERE c
2a7d6 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a  lause terms..**.
2a7d7 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 6f 66 20  ** The terms of 
2a7d8 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 72  a FROM clause ar
2a7d9 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  e contained in t
2a7da 68 65 20 53 65 6c 65 63 74 2e 70 53 72 63 20 73  he Select.pSrc s
2a7db 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65  tructure..** The
2a7dc 20 6c 65 66 74 20 6d 6f 73 74 20 74 61 62 6c 65   left most table
2a7dd 20 69 73 20 74 68 65 20 66 69 72 73 74 20 65 6e   is the first en
2a7de 74 72 79 20 69 6e 20 53 65 6c 65 63 74 2e 70 53  try in Select.pS
2a7df 72 63 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d  rc.  The right-m
2a7e0 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20  ost.** table is 
2a7e1 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2e 20  the last entry. 
2a7e2 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   The join operat
2a7e3 6f 72 20 69 73 20 68 65 6c 64 20 69 6e 20 74 68  or is held in th
2a7e4 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 74 68  e entry to.** th
2a7e5 65 20 6c 65 66 74 2e 20 20 54 68 75 73 20 65 6e  e left.  Thus en
2a7e6 74 72 79 20 30 20 63 6f 6e 74 61 69 6e 73 20 74  try 0 contains t
2a7e7 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72  he join operator
2a7e8 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20 62 65   for the join be
2a7e9 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69 65 73  tween.** entries
2a7ea 20 30 20 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f   0 and 1.  Any O
2a7eb 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
2a7ec 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
2a7ed 74 68 20 74 68 65 20 6a 6f 69 6e 20 61 72 65 0a  th the join are.
2a7ee 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63 68 65 64  ** also attached
2a7ef 20 74 6f 20 74 68 65 20 6c 65 66 74 20 65 6e 74   to the left ent
2a7f0 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ry..**.** This r
2a7f1 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
2a7f2 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
2a7f3 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e  ors encountered.
2a7f4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2a7f5 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e  qliteProcessJoin
2a7f6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2a7f7 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72  Select *p){.  Sr
2a7f8 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20  cList *pSrc;    
2a7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a7fa 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74   All tables in t
2a7fb 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2a7fc 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  /.  int i, j;   
2a7fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a7fe 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2a7ff 74 65 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ters */.  struct
2a800 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2a801 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66  Left;     /* Lef
2a802 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f  t table being jo
2a803 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74  ined */.  struct
2a804 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2a805 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 52 69 67  Right;    /* Rig
2a806 68 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a  ht table being j
2a807 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63  oined */..  pSrc
2a808 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 4c   = p->pSrc;.  pL
2a809 65 66 74 20 3d 20 26 70 53 72 63 2d 3e 61 5b 30  eft = &pSrc->a[0
2a80a 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 26 70  ];.  pRight = &p
2a80b 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72 28 69  Left[1];.  for(i
2a80c 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63  =0; i<pSrc->nSrc
2a80d 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68 74 2b  -1; i++, pRight+
2a80e 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20 20 20  +, pLeft++){.   
2a80f 20 54 61 62 6c 65 20 2a 70 4c 65 66 74 54 61 62   Table *pLeftTab
2a810 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0a   = pLeft->pTab;.
2a811 20 20 20 20 54 61 62 6c 65 20 2a 70 52 69 67 68      Table *pRigh
2a812 74 54 61 62 20 3d 20 70 52 69 67 68 74 2d 3e 70  tTab = pRight->p
2a813 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 69 73 4f  Tab;.    int isO
2a814 75 74 65 72 3b 0a 0a 20 20 20 20 69 66 28 20 4e  uter;..    if( N
2a815 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d 3d 30  EVER(pLeftTab==0
2a816 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d 3d 30   || pRightTab==0
2a817 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2a818 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70 52 69    isOuter = (pRi
2a819 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  ght->jointype & 
2a81a 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a 0a 20  JT_OUTER)!=0;.. 
2a81b 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e     /* When the N
2a81c 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 20 69  ATURAL keyword i
2a81d 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64 20 57  s present, add W
2a81e 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2a81f 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65  s for.    ** eve
2a820 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  ry column that t
2a821 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 68 61  he two tables ha
2a822 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20  ve in common..  
2a823 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
2a824 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  ght->jointype & 
2a825 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20  JT_NATURAL ){.  
2a826 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
2a827 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70  pOn || pRight->p
2a828 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  Using ){.       
2a829 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2a82a 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41 54 55  (pParse, "a NATU
2a82b 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74  RAL join may not
2a82c 20 68 61 76 65 20 22 0a 20 20 20 20 20 20 20 20   have ".        
2a82d 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49     "an ON or USI
2a82e 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a  NG clause", 0);.
2a82f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2a830 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2a831 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74  for(j=0; j<pLeft
2a832 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Tab->nCol; j++){
2a833 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
2a834 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61 62 2d  Name = pLeftTab-
2a835 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a  >aCol[j].zName;.
2a836 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 69 67          int iRig
2a837 68 74 43 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e  htCol = columnIn
2a838 64 65 78 28 70 52 69 67 68 74 54 61 62 2c 20 7a  dex(pRightTab, z
2a839 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Name);.        i
2a83a 66 28 20 69 52 69 67 68 74 43 6f 6c 3e 3d 30 20  f( iRightCol>=0 
2a83b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64  ){.          add
2a83c 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65  WhereTerm(pParse
2a83d 2c 20 70 53 72 63 2c 20 69 2c 20 6a 2c 20 69 52  , pSrc, i, j, iR
2a83e 69 67 68 74 43 6f 6c 2c 20 69 73 4f 75 74 65 72  ightCol, isOuter
2a83f 2c 20 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  , &p->pWhere);. 
2a840 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2a841 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
2a842 69 73 61 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20  isallow both ON 
2a843 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
2a844 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f  s in the same jo
2a845 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  in.    */.    if
2a846 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26  ( pRight->pOn &&
2a847 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20   pRight->pUsing 
2a848 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2a849 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2a84a 20 22 63 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f   "cannot have bo
2a84b 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20  th ON and USING 
2a84c 22 0a 20 20 20 20 20 20 20 20 22 63 6c 61 75 73  ".        "claus
2a84d 65 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a  es in the same j
2a84e 6f 69 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74  oin");.      ret
2a84f 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20  urn 1;.    }..  
2a850 20 20 2f 2a 20 41 64 64 20 74 68 65 20 4f 4e 20    /* Add the ON 
2a851 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 65 6e  clause to the en
2a852 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  d of the WHERE c
2a853 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64  lause, connected
2a854 20 62 79 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e   by.    ** an AN
2a855 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20  D operator..    
2a856 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  */.    if( pRigh
2a857 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20  t->pOn ){.      
2a858 69 66 28 20 69 73 4f 75 74 65 72 20 29 20 73 65  if( isOuter ) se
2a859 74 4a 6f 69 6e 45 78 70 72 28 70 52 69 67 68 74  tJoinExpr(pRight
2a85a 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69  ->pOn, pRight->i
2a85b 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70  Cursor);.      p
2a85c 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74  ->pWhere = sqlit
2a85d 65 33 45 78 70 72 41 6e 64 28 70 50 61 72 73 65  e3ExprAnd(pParse
2a85e 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c  ->db, p->pWhere,
2a85f 20 70 52 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20   pRight->pOn);. 
2a860 20 20 20 20 20 70 52 69 67 68 74 2d 3e 70 4f 6e       pRight->pOn
2a861 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
2a862 20 2f 2a 20 43 72 65 61 74 65 20 65 78 74 72 61   /* Create extra
2a863 20 74 65 72 6d 73 20 6f 6e 20 74 68 65 20 57 48   terms on the WH
2a864 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 65  ERE clause for e
2a865 61 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64  ach column named
2a866 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 55  .    ** in the U
2a867 53 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 45 78  SING clause.  Ex
2a868 61 6d 70 6c 65 3a 20 49 66 20 74 68 65 20 74 77  ample: If the tw
2a869 6f 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a  o tables to be j
2a86a 6f 69 6e 65 64 20 61 72 65 20 0a 20 20 20 20 2a  oined are .    *
2a86b 2a 20 41 20 61 6e 64 20 42 20 61 6e 64 20 74 68  * A and B and th
2a86c 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6e  e USING clause n
2a86d 61 6d 65 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a  ames X, Y, and Z
2a86e 2c 20 74 68 65 6e 20 61 64 64 20 74 68 69 73 0a  , then add this.
2a86f 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 57 48      ** to the WH
2a870 45 52 45 20 63 6c 61 75 73 65 3a 20 20 20 20 41  ERE clause:    A
2a871 2e 58 3d 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42  .X=B.X AND A.Y=B
2a872 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20  .Y AND A.Z=B.Z. 
2a873 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20     ** Report an 
2a874 65 72 72 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c  error if any col
2a875 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e  umn mentioned in
2a876 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   the USING claus
2a877 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  e is.    ** not 
2a878 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 62 6f 74  contained in bot
2a879 68 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a  h tables to be j
2a87a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  oined..    */.  
2a87b 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 55    if( pRight->pU
2a87c 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 49 64  sing ){.      Id
2a87d 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 52  List *pList = pR
2a87e 69 67 68 74 2d 3e 70 55 73 69 6e 67 3b 0a 20 20  ight->pUsing;.  
2a87f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2a880 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b  List->nId; j++){
2a881 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
2a882 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e 61 5b  Name = pList->a[
2a883 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  j].zName;.      
2a884 20 20 69 6e 74 20 69 4c 65 66 74 43 6f 6c 20 3d    int iLeftCol =
2a885 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65   columnIndex(pLe
2a886 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ftTab, zName);. 
2a887 20 20 20 20 20 20 20 69 6e 74 20 69 52 69 67 68         int iRigh
2a888 74 43 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64  tCol = columnInd
2a889 65 78 28 70 52 69 67 68 74 54 61 62 2c 20 7a 4e  ex(pRightTab, zN
2a88a 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ame);.        if
2a88b 28 20 69 4c 65 66 74 43 6f 6c 3c 30 20 7c 7c 20  ( iLeftCol<0 || 
2a88c 69 52 69 67 68 74 43 6f 6c 3c 30 20 29 7b 0a 20  iRightCol<0 ){. 
2a88d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2a88e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2a88f 20 22 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73   "cannot join us
2a890 69 6e 67 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20  ing column %s - 
2a891 63 6f 6c 75 6d 6e 20 22 0a 20 20 20 20 20 20 20  column ".       
2a892 20 20 20 20 20 22 6e 6f 74 20 70 72 65 73 65 6e       "not presen
2a893 74 20 69 6e 20 62 6f 74 68 20 74 61 62 6c 65 73  t in both tables
2a894 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
2a895 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2a896 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2a897 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70 50   addWhereTerm(pP
2a898 61 72 73 65 2c 20 70 53 72 63 2c 20 69 2c 20 69  arse, pSrc, i, i
2a899 4c 65 66 74 43 6f 6c 2c 20 69 52 69 67 68 74 43  LeftCol, iRightC
2a89a 6f 6c 2c 20 69 73 4f 75 74 65 72 2c 20 26 70 2d  ol, isOuter, &p-
2a89b 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 20 20  >pWhere);.      
2a89c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
2a89d 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
2a89e 20 49 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74   Insert code int
2a89f 6f 20 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20  o "v" that will 
2a8a0 70 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20  push the record 
2a8a1 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  on the top of th
2a8a2 65 0a 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20  e.** stack into 
2a8a3 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73  the sorter..*/.s
2a8a4 74 61 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f  tatic void pushO
2a8a5 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72  ntoSorter(.  Par
2a8a6 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2a8a7 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
2a8a8 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
2a8a9 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2a8aa 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
2a8ab 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65  Y clause */.  Se
2a8ac 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
2a8ad 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
2a8ae 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a8af 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  nt */.  int regD
2a8b0 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 2f  ata            /
2a8b1 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
2a8b2 6e 67 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f  ng data to be so
2a8b3 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  rted */.){.  Vdb
2a8b4 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2a8b5 56 64 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70  Vdbe;.  int nExp
2a8b6 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  r = pOrderBy->nE
2a8b7 78 70 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61  xpr;.  int regBa
2a8b8 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  se = sqlite3GetT
2a8b9 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2a8ba 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74   nExpr+2);.  int
2a8bb 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
2a8bc 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2a8bd 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2a8be 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2a8bf 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
2a8c0 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
2a8c1 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  st(pParse, pOrde
2a8c2 72 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29  rBy, regBase, 0)
2a8c3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a8c4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75  ddOp2(v, OP_Sequ
2a8c5 65 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e  ence, pOrderBy->
2a8c6 69 45 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73  iECursor, regBas
2a8c7 65 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69  e+nExpr);.  sqli
2a8c8 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2a8c9 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c  pParse, regData,
2a8ca 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31   regBase+nExpr+1
2a8cb 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 1);.  sqlite3V
2a8cc 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a8cd 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
2a8ce 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20  ase, nExpr + 2, 
2a8cf 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71  regRecord);.  sq
2a8d0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a8d1 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
2a8d2 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72   pOrderBy->iECur
2a8d3 73 6f 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  sor, regRecord);
2a8d4 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2a8d5 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a8d6 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73   regRecord);.  s
2a8d7 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2a8d8 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
2a8d9 65 67 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29  egBase, nExpr+2)
2a8da 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  ;.  if( pSelect-
2a8db 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69  >iLimit ){.    i
2a8dc 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b  nt addr1, addr2;
2a8dd 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b  .    int iLimit;
2a8de 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74  .    if( pSelect
2a8df 2d 3e 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20  ->iOffset ){.   
2a8e0 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c     iLimit = pSel
2a8e1 65 63 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a  ect->iOffset+1;.
2a8e2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a8e3 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63   iLimit = pSelec
2a8e4 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d  t->iLimit;.    }
2a8e5 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
2a8e6 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2a8e7 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69  , OP_IfZero, iLi
2a8e8 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mit);.    sqlite
2a8e9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a8ea 50 5f 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74  P_AddImm, iLimit
2a8eb 2c 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32  , -1);.    addr2
2a8ec 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2a8ed 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
2a8ee 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a8ef 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2a8f0 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r1);.    sqlite3
2a8f1 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2a8f2 5f 4c 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d  _Last, pOrderBy-
2a8f3 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  >iECursor);.    
2a8f4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a8f5 31 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20  1(v, OP_Delete, 
2a8f6 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
2a8f7 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  or);.    sqlite3
2a8f8 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2a8f9 61 64 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c  addr2);.    pSel
2a8fa 65 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b  ect->iLimit = 0;
2a8fb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
2a8fc 64 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  d code to implem
2a8fd 65 6e 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a  ent the OFFSET.*
2a8fe 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
2a8ff 64 65 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65  deOffset(.  Vdbe
2a900 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   *v,          /*
2a901 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
2a902 6e 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20  nto this VM */. 
2a903 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
2a904 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
2a905 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
2a906 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
2a907 69 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a  iContinue     /*
2a908 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b   Jump here to sk
2a909 69 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ip the current r
2a90a 65 63 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66  ecord */.){.  if
2a90b 28 20 70 2d 3e 69 4f 66 66 73 65 74 20 26 26 20  ( p->iOffset && 
2a90c 69 43 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a  iContinue!=0 ){.
2a90d 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
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 41 64 64 49 6d 6d  Op2(v, OP_AddImm
2a910 2c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31  , p->iOffset, -1
2a911 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  );.    addr = sq
2a912 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2a913 76 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e  v, OP_IfNeg, p->
2a914 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71  iOffset);.    sq
2a915 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a916 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
2a917 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56  Continue);.    V
2a918 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2a919 73 6b 69 70 20 4f 46 46 53 45 54 20 72 65 63 6f  skip OFFSET reco
2a91a 72 64 73 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  rds"));.    sqli
2a91b 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2a91c 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a  v, addr);.  }.}.
2a91d 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20  ./*.** Add code 
2a91e 74 68 61 74 20 77 69 6c 6c 20 63 68 65 63 6b 20  that will check 
2a91f 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
2a920 20 4e 20 72 65 67 69 73 74 65 72 73 20 73 74 61   N registers sta
2a921 72 74 69 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a  rting at iMem.**
2a922 20 66 6f 72 6d 20 61 20 64 69 73 74 69 6e 63 74   form a distinct
2a923 20 65 6e 74 72 79 2e 20 20 69 54 61 62 20 69 73   entry.  iTab is
2a924 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   a sorting index
2a925 20 74 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76   that holds prev
2a926 69 6f 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63  iously.** seen c
2a927 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74  ombinations of t
2a928 68 65 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20  he N values.  A 
2a929 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64  new entry is mad
2a92a 65 20 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20  e in iTab.** if 
2a92b 74 68 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61  the current N va
2a92c 6c 75 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a  lues are new..**
2a92d 0a 2a 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64  .** A jump to ad
2a92e 64 72 52 65 70 65 61 74 20 69 73 20 6d 61 64 65  drRepeat is made
2a92f 20 61 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c   and the N+1 val
2a930 75 65 73 20 61 72 65 20 70 6f 70 70 65 64 20 66  ues are popped f
2a931 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b  rom the.** stack
2a932 20 69 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c   if the top N el
2a933 65 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64  ements are not d
2a934 69 73 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74  istinct..*/.stat
2a935 69 63 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74  ic void codeDist
2a936 69 6e 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  inct(.  Parse *p
2a937 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
2a938 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
2a939 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
2a93a 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c  t */.  int iTab,
2a93b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
2a93c 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65  orting index use
2a93d 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69  d to test for di
2a93e 73 74 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20  stinctness */.  
2a93f 69 6e 74 20 61 64 64 72 52 65 70 65 61 74 2c 20  int addrRepeat, 
2a940 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65     /* Jump to he
2a941 72 65 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e  re if not distin
2a942 63 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20  ct */.  int N,  
2a943 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2a944 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
2a945 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20   */.  int iMem  
2a946 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
2a947 74 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  t element */.){.
2a948 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74    Vdbe *v;.  int
2a949 20 72 31 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72   r1;..  v = pPar
2a94a 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 72 31 20  se->pVdbe;.  r1 
2a94b 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2a94c 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73  Reg(pParse);.  s
2a94d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2a94e 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Int(v, OP_Found,
2a94f 20 69 54 61 62 2c 20 61 64 64 72 52 65 70 65 61   iTab, addrRepea
2a950 74 2c 20 69 4d 65 6d 2c 20 4e 29 3b 0a 20 20 73  t, iMem, N);.  s
2a951 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a952 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
2a953 64 2c 20 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b  d, iMem, N, r1);
2a954 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2a955 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
2a956 73 65 72 74 2c 20 69 54 61 62 2c 20 72 31 29 3b  sert, iTab, r1);
2a957 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2a958 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a959 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47   r1);.}../*.** G
2a95a 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
2a95b 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20 61 20   message when a 
2a95c 53 45 4c 45 43 54 20 69 73 20 75 73 65 64 20 77  SELECT is used w
2a95d 69 74 68 69 6e 20 61 20 73 75 62 65 78 70 72 65  ithin a subexpre
2a95e 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c  ssion.** (exampl
2a95f 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c 45 43  e:  "a IN (SELEC
2a960 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65 29 22  T * FROM table)"
2a961 29 20 62 75 74 20 69 74 20 68 61 73 20 6d 6f 72  ) but it has mor
2a962 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c 74 0a  e than 1 result.
2a963 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64  ** column.  We d
2a964 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75 62 72  o this in a subr
2a965 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65 20 74  outine because t
2a966 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  he error occurs 
2a967 69 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 70  in multiple.** p
2a968 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  laces..*/.static
2a969 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d 75 6c   int checkForMul
2a96a 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72  tiColumnSelectEr
2a96b 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ror(.  Parse *pP
2a96c 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
2a96d 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20 2a 2f  arse context. */
2a96e 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
2a96f 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73 74 69  Dest,   /* Desti
2a970 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45 43 54  nation of SELECT
2a971 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
2a972 74 20 6e 45 78 70 72 20 20 20 20 20 20 20 20 20  t nExpr         
2a973 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2a974 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 72  result columns r
2a975 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c 45 43  eturned by SELEC
2a976 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 44  T */.){.  int eD
2a977 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65  est = pDest->eDe
2a978 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70 72 3e  st;.  if( nExpr>
2a979 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53 52 54  1 && (eDest==SRT
2a97a 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d 3d 53  _Mem || eDest==S
2a97b 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20 20 73  RT_Set) ){.    s
2a97c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2a97d 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61 20 73  Parse, "only a s
2a97e 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61 6c 6c  ingle result all
2a97f 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20 20 20  owed for ".     
2a980 20 20 22 61 20 53 45 4c 45 43 54 20 74 68 61 74    "a SELECT that
2a981 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
2a982 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20 20 20  xpression");.   
2a983 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
2a984 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  se{.    return 0
2a985 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2a986 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
2a987 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 66  rates the code f
2a988 6f 72 20 74 68 65 20 69 6e 73 69 64 65 20 6f 66  or the inside of
2a989 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a   the inner loop.
2a98a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 0a  ** of a SELECT..
2a98b 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54 61 62 20  **.** If srcTab 
2a98c 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61 72 65 20  and nColumn are 
2a98d 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68 65 6e 20  both zero, then 
2a98e 74 68 65 20 70 45 4c 69 73 74 20 65 78 70 72 65  the pEList expre
2a98f 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 65 76  ssions.** are ev
2a990 61 6c 75 61 74 65 64 20 69 6e 20 6f 72 64 65 72  aluated in order
2a991 20 74 6f 20 67 65 74 20 74 68 65 20 64 61 74 61   to get the data
2a992 20 66 6f 72 20 74 68 69 73 20 72 6f 77 2e 20 20   for this row.  
2a993 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20  If nColumn>0.** 
2a994 74 68 65 6e 20 64 61 74 61 20 69 73 20 70 75 6c  then data is pul
2a995 6c 65 64 20 66 72 6f 6d 20 73 72 63 54 61 62 20  led from srcTab 
2a996 61 6e 64 20 70 45 4c 69 73 74 20 69 73 20 75 73  and pEList is us
2a997 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65 74 20 74  ed only to get t
2a998 68 65 0a 2a 2a 20 64 61 74 61 74 79 70 65 73 20  he.** datatypes 
2a999 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e  for each column.
2a99a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2a99b 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
2a99c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2a99d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
2a99e 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2a99f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2a9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a9a1 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65   The complete se
2a9a2 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 62  lect statement b
2a9a3 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
2a9a4 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2a9a5 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  ,       /* List 
2a9a6 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e 67 20  of values being 
2a9a7 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20 20 69  extracted */.  i
2a9a8 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20 20 20  nt srcTab,      
2a9a9 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c 20 64         /* Pull d
2a9aa 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20 74 61  ata from this ta
2a9ab 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
2a9ac 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
2a9ad 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
2a9ae 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75  lumns in the sou
2a9af 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  rce table */.  E
2a9b0 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2a9b1 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74  y,     /* If not
2a9b2 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65 73 75   NULL, sort resu
2a9b3 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73 20 6b  lts using this k
2a9b4 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74  ey */.  int dist
2a9b5 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  inct,           
2a9b6 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b 65 20  /* If >=0, make 
2a9b7 73 75 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  sure results are
2a9b8 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 53   distinct */.  S
2a9b9 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
2a9ba 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f  ,      /* How to
2a9bb 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20   dispose of the 
2a9bc 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
2a9bd 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20   iContinue,     
2a9be 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
2a9bf 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  e to continue wi
2a9c0 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20  th next row */. 
2a9c1 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20   int iBreak     
2a9c2 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
2a9c3 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f   here to break o
2a9c4 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20  ut of the inner 
2a9c5 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  loop */.){.  Vdb
2a9c6 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2a9c7 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
2a9c8 20 69 6e 74 20 68 61 73 44 69 73 74 69 6e 63 74   int hasDistinct
2a9c9 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
2a9ca 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   if the DISTINCT
2a9cb 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
2a9cc 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ent */.  int reg
2a9cd 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  Result;         
2a9ce 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66       /* Start of
2a9cf 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e 67 20   memory holding 
2a9d0 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
2a9d1 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
2a9d2 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a 20 48  t->eDest;   /* H
2a9d3 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ow to dispose of
2a9d4 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
2a9d5 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d  t iParm = pDest-
2a9d6 3e 69 50 61 72 6d 3b 20 20 20 2f 2a 20 46 69 72  >iParm;   /* Fir
2a9d7 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 64  st argument to d
2a9d8 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a  isposal method *
2a9d9 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 43  /.  int nResultC
2a9da 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
2a9db 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73  /* Number of res
2a9dc 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a  ult columns */..
2a9dd 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
2a9de 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29   if( NEVER(v==0)
2a9df 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
2a9e0 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
2a9e1 3b 0a 20 20 68 61 73 44 69 73 74 69 6e 63 74 20  ;.  hasDistinct 
2a9e2 3d 20 64 69 73 74 69 6e 63 74 3e 3d 30 3b 0a 20  = distinct>=0;. 
2a9e3 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30   if( pOrderBy==0
2a9e4 20 26 26 20 21 68 61 73 44 69 73 74 69 6e 63 74   && !hasDistinct
2a9e5 20 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73   ){.    codeOffs
2a9e6 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e  et(v, p, iContin
2a9e7 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50  ue);.  }..  /* P
2a9e8 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 65  ull the requeste
2a9e9 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a  d columns..  */.
2a9ea 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20    if( nColumn>0 
2a9eb 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f  ){.    nResultCo
2a9ec 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d  l = nColumn;.  }
2a9ed 65 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c  else{.    nResul
2a9ee 74 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e  tCol = pEList->n
2a9ef 45 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Expr;.  }.  if( 
2a9f0 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29  pDest->iMem==0 )
2a9f1 7b 0a 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65  {.    pDest->iMe
2a9f2 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  m = pParse->nMem
2a9f3 2b 31 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 6e  +1;.    pDest->n
2a9f4 4d 65 6d 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  Mem = nResultCol
2a9f5 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
2a9f6 65 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  em += nResultCol
2a9f7 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20  ;.  }else{ .    
2a9f8 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e 6e  assert( pDest->n
2a9f9 4d 65 6d 3d 3d 6e 52 65 73 75 6c 74 43 6f 6c 20  Mem==nResultCol 
2a9fa 29 3b 0a 20 20 7d 0a 20 20 72 65 67 52 65 73 75  );.  }.  regResu
2a9fb 6c 74 20 3d 20 70 44 65 73 74 2d 3e 69 4d 65 6d  lt = pDest->iMem
2a9fc 3b 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e  ;.  if( nColumn>
2a9fd 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 ){.    for(i=0
2a9fe 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ; i<nColumn; i++
2a9ff 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2aa00 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2aa01 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
2aa02 20 69 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29   i, regResult+i)
2aa03 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2aa04 69 66 28 20 65 44 65 73 74 21 3d 53 52 54 5f 45  if( eDest!=SRT_E
2aa05 78 69 73 74 73 20 29 7b 0a 20 20 20 20 2f 2a 20  xists ){.    /* 
2aa06 49 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  If the destinati
2aa07 6f 6e 20 69 73 20 61 6e 20 45 58 49 53 54 53 28  on is an EXISTS(
2aa08 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 2c  ...) expression,
2aa09 20 74 68 65 20 61 63 74 75 61 6c 0a 20 20 20 20   the actual.    
2aa0a 2a 2a 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  ** values return
2aa0b 65 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 54  ed by the SELECT
2aa0c 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65   are not require
2aa0d 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
2aa0e 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2aa0f 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ear(pParse);.   
2aa10 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2aa11 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2aa12 20 70 45 4c 69 73 74 2c 20 72 65 67 52 65 73 75   pEList, regResu
2aa13 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  lt, eDest==SRT_O
2aa14 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43  utput);.  }.  nC
2aa15 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75 6c 74 43  olumn = nResultC
2aa16 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ol;..  /* If the
2aa17 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
2aa18 64 20 77 61 73 20 70 72 65 73 65 6e 74 20 6f 6e  d was present on
2aa19 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2aa1a 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 74  ement.  ** and t
2aa1b 68 69 73 20 72 6f 77 20 68 61 73 20 62 65 65 6e  his row has been
2aa1c 20 73 65 65 6e 20 62 65 66 6f 72 65 2c 20 74 68   seen before, th
2aa1d 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b 65 20 74  en do not make t
2aa1e 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20 70 61 72  his row.  ** par
2aa1f 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e  t of the result.
2aa20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61 73 44  .  */.  if( hasD
2aa21 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 61  istinct ){.    a
2aa22 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
2aa23 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2aa24 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e  pEList->nExpr==n
2aa25 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 63 6f  Column );.    co
2aa26 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72 73  deDistinct(pPars
2aa27 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69 43 6f  e, distinct, iCo
2aa28 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c  ntinue, nColumn,
2aa29 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
2aa2a 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30   if( pOrderBy==0
2aa2b 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 4f 66   ){.      codeOf
2aa2c 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
2aa2d 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inue);.    }.  }
2aa2e 0a 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72  ..  if( checkFor
2aa2f 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63  MultiColumnSelec
2aa30 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70  tError(pParse, p
2aa31 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  Dest, pEList->nE
2aa32 78 70 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75  xpr) ){.    retu
2aa33 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63  rn;.  }..  switc
2aa34 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20  h( eDest ){.    
2aa35 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  /* In this mode,
2aa36 20 77 72 69 74 65 20 65 61 63 68 20 71 75 65 72   write each quer
2aa37 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68 65 20  y result to the 
2aa38 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  key of the tempo
2aa39 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c  rary.    ** tabl
2aa3a 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a  e iParm..    */.
2aa3b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2aa3c 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
2aa3d 45 43 54 0a 20 20 20 20 63 61 73 65 20 53 52 54  ECT.    case SRT
2aa3e 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20 20 20  _Union: {.      
2aa3f 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20 72 31  int r1;.      r1
2aa40 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2aa41 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2aa42 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aa43 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
2aa44 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c  Record, regResul
2aa45 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b  t, nColumn, r1);
2aa46 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2aa47 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2aa48 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  dxInsert, iParm,
2aa49 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
2aa4a 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2aa4b 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
2aa4c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aa4d 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72  }..    /* Constr
2aa4e 75 63 74 20 61 20 72 65 63 6f 72 64 20 66 72 6f  uct a record fro
2aa4f 6d 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  m the query resu
2aa50 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61 64 20  lt, but instead 
2aa51 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69 6e 67  of.    ** saving
2aa52 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20 75 73   that record, us
2aa53 65 20 69 74 20 61 73 20 61 20 6b 65 79 20 74 6f  e it as a key to
2aa54 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73   delete elements
2aa55 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 65   from.    ** the
2aa56 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2aa57 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20   iParm..    */. 
2aa58 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 63 65     case SRT_Exce
2aa59 70 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  pt: {.      sqli
2aa5a 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2aa5b 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69   OP_IdxDelete, i
2aa5c 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c 74 2c  Parm, regResult,
2aa5d 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
2aa5e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2aa5f 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 74 6f  ndif..    /* Sto
2aa60 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73  re the result as
2aa61 20 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e   data using a un
2aa62 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f  ique key..    */
2aa63 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61  .    case SRT_Ta
2aa64 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52  ble:.    case SR
2aa65 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20  T_EphemTab: {.  
2aa66 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
2aa67 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2aa68 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 74 65  Parse);.      te
2aa69 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2aa6a 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  RT_Table );.    
2aa6b 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
2aa6c 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20  t==SRT_EphemTab 
2aa6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2aa6e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2aa6f 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
2aa70 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Result, nColumn,
2aa71 20 72 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20   r1);.      if( 
2aa72 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2aa73 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
2aa74 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
2aa75 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20  rBy, p, r1);.   
2aa76 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2aa77 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
2aa78 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2aa79 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
2aa7a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2aa7b 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
2aa7c 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20  iParm, r2);.    
2aa7d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aa7e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
2aa7f 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 2c 20 72  rt, iParm, r1, r
2aa80 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
2aa81 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2aa82 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
2aa83 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2aa84 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2aa85 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
2aa86 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2aa87 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2aa88 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
2aa89 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aa8a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2aa8b 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
2aa8c 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
2aa8d 20 63 72 65 61 74 69 6e 67 20 61 20 73 65 74 20   creating a set 
2aa8e 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20  for an "expr IN 
2aa8f 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f  (SELECT ...)" co
2aa90 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20  nstruct,.    ** 
2aa91 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c  then there shoul
2aa92 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74  d be a single it
2aa93 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  em on the stack.
2aa94 20 20 57 72 69 74 65 20 74 68 69 73 0a 20 20 20    Write this.   
2aa95 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68   ** item into th
2aa96 65 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68  e set table with
2aa97 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20   bogus data..   
2aa98 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2aa99 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Set: {.      as
2aa9a 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31  sert( nColumn==1
2aa9b 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66   );.      p->aff
2aa9c 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 43  inity = sqlite3C
2aa9d 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
2aa9e 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
2aa9f 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69  r, pDest->affini
2aaa0 74 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ty);.      if( p
2aaa1 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2aaa2 20 20 20 2f 2a 20 41 74 20 66 69 72 73 74 20 67     /* At first g
2aaa3 6c 61 6e 63 65 20 79 6f 75 20 77 6f 75 6c 64 20  lance you would 
2aaa4 74 68 69 6e 6b 20 77 65 20 63 6f 75 6c 64 20 6f  think we could o
2aaa5 70 74 69 6d 69 7a 65 20 6f 75 74 20 74 68 65 0a  ptimize out the.
2aaa6 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45 52          ** ORDER
2aaa7 20 42 59 20 69 6e 20 74 68 69 73 20 63 61 73 65   BY in this case
2aaa8 20 73 69 6e 63 65 20 74 68 65 20 6f 72 64 65 72   since the order
2aaa9 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
2aaaa 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20 2a  he set.        *
2aaab 2a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65  * does not matte
2aaac 72 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d 69  r.  But there mi
2aaad 67 68 74 20 62 65 20 61 20 4c 49 4d 49 54 20 63  ght be a LIMIT c
2aaae 6c 61 75 73 65 2c 20 69 6e 20 77 68 69 63 68 0a  lause, in which.
2aaaf 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20          ** case 
2aab0 74 68 65 20 6f 72 64 65 72 20 64 6f 65 73 20 6d  the order does m
2aab1 61 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  atter */.       
2aab2 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28   pushOntoSorter(
2aab3 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
2aab4 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74 29 3b  , p, regResult);
2aab5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2aab6 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2aab7 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2aab8 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2aab9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2aaba 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
2aabb 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c  cord, regResult,
2aabc 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69   1, r1, &p->affi
2aabd 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
2aabe 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2aabf 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
2aac0 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
2aac1 6c 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  lt, 1);.        
2aac2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2aac3 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
2aac4 74 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20  t, iParm, r1);. 
2aac5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2aac6 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2aac7 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
2aac8 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2aac9 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2aaca 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e  any row exist in
2aacb 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c   the result set,
2aacc 20 72 65 63 6f 72 64 20 74 68 61 74 20 66 61 63   record that fac
2aacd 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20  t and abort..   
2aace 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2aacf 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20  _Exists: {.     
2aad0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2aad1 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2aad2 2c 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20  , 1, iParm);.   
2aad3 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
2aad4 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d  clause will term
2aad5 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66  inate the loop f
2aad6 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62  or us */.      b
2aad7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
2aad8 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
2aad9 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74   scalar select t
2aada 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
2aadb 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  n expression, th
2aadc 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20  en.    ** store 
2aadd 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  the results in t
2aade 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
2aadf 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62  emory cell and b
2aae0 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20  reak out.    ** 
2aae1 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70  of the scan loop
2aae2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
2aae3 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20  e SRT_Mem: {.   
2aae4 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
2aae5 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69  mn==1 );.      i
2aae6 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
2aae7 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53         pushOntoS
2aae8 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f  orter(pParse, pO
2aae9 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52 65  rderBy, p, regRe
2aaea 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  sult);.      }el
2aaeb 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2aaec 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2aaed 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c  pParse, regResul
2aaee 74 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20  t, iParm, 1);.  
2aaef 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d        /* The LIM
2aaf0 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a  IT clause will j
2aaf1 75 6d 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  ump out of the l
2aaf2 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
2aaf3 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2aaf4 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  k;.    }.#endif 
2aaf5 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
2aaf6 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
2aaf7 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20  */..    /* Send 
2aaf8 74 68 65 20 64 61 74 61 20 74 6f 20 74 68 65 20  the data to the 
2aaf9 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
2aafa 6e 20 6f 72 20 74 6f 20 61 20 73 75 62 72 6f 75  n or to a subrou
2aafb 74 69 6e 65 2e 20 20 49 6e 20 74 68 65 0a 20 20  tine.  In the.  
2aafc 20 20 2a 2a 20 63 61 73 65 20 6f 66 20 61 20 73    ** case of a s
2aafd 75 62 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 73  ubroutine, the s
2aafe 75 62 72 6f 75 74 69 6e 65 20 69 74 73 65 6c 66  ubroutine itself
2aaff 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2ab00 66 6f 72 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69  for.    ** poppi
2ab01 6e 67 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  ng the data from
2ab02 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20   the stack..    
2ab03 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
2ab04 43 6f 72 6f 75 74 69 6e 65 3a 0a 20 20 20 20 63  Coroutine:.    c
2ab05 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a 20  ase SRT_Output: 
2ab06 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2ab07 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
2ab08 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20 20  outine );.      
2ab09 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
2ab0a 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20  =SRT_Output );. 
2ab0b 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42       if( pOrderB
2ab0c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  y ){.        int
2ab0d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2ab0e 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2ab0f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ab10 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2ab11 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
2ab12 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Result, nColumn,
2ab13 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75   r1);.        pu
2ab14 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
2ab15 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
2ab16 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73  , r1);.        s
2ab17 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2ab18 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
2ab19 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2ab1a 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
2ab1b 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 20 20  outine ){.      
2ab1c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ab1d 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2ab1e 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a   pDest->iParm);.
2ab1f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2ab20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ab21 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
2ab22 75 6c 74 52 6f 77 2c 20 72 65 67 52 65 73 75 6c  ultRow, regResul
2ab23 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
2ab24 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2ab25 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2ab26 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  nge(pParse, regR
2ab27 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  esult, nColumn);
2ab28 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2ab29 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
2ab2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2ab2b 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20  _OMIT_TRIGGER). 
2ab2c 20 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68     /* Discard th
2ab2d 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68 69 73  e results.  This
2ab2e 20 69 73 20 75 73 65 64 20 66 6f 72 20 53 45 4c   is used for SEL
2ab2f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
2ab30 6e 73 69 64 65 0a 20 20 20 20 2a 2a 20 74 68 65  nside.    ** the
2ab31 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47   body of a TRIGG
2ab32 45 52 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65  ER.  The purpose
2ab33 20 6f 66 20 73 75 63 68 20 73 65 6c 65 63 74 73   of such selects
2ab34 20 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20   is to call.    
2ab35 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  ** user-defined 
2ab36 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 68  functions that h
2ab37 61 76 65 20 73 69 64 65 20 65 66 66 65 63 74 73  ave side effects
2ab38 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 63 61 72  .  We do not car
2ab39 65 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74  e.    ** about t
2ab3a 68 65 20 61 63 74 75 61 6c 20 72 65 73 75 6c 74  he actual result
2ab3b 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e  s of the select.
2ab3c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61  .    */.    defa
2ab3d 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
2ab3e 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f  ert( eDest==SRT_
2ab3f 44 69 73 63 61 72 64 20 29 3b 0a 20 20 20 20 20  Discard );.     
2ab40 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2ab41 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  ndif.  }..  /* J
2ab42 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ump to the end o
2ab43 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68  f the loop if th
2ab44 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68  e LIMIT is reach
2ab45 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
2ab46 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->iLimit ){.    
2ab47 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2ab48 3d 3d 30 20 29 3b 20 20 2f 2a 20 49 66 20 74 68  ==0 );  /* If th
2ab49 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ere is an ORDER 
2ab4a 42 59 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a  BY, the call to.
2ab4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70              ** p
2ab4d 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20  ushOntoSorter() 
2ab4e 77 6f 75 6c 64 20 68 61 76 65 20 63 6c 65 61 72  would have clear
2ab4f 65 64 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a  ed p->iLimit */.
2ab50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ab51 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 5a 65  ddOp3(v, OP_IfZe
2ab52 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69  ro, p->iLimit, i
2ab53 42 72 65 61 6b 2c 20 2d 31 29 3b 0a 20 20 7d 0a  Break, -1);.  }.
2ab54 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  }../*.** Given a
2ab55 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
2ab56 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65  t, generate a Ke
2ab57 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
2ab58 74 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20  that records.** 
2ab59 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
2ab5a 71 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20  quence for each 
2ab5b 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68  expression in th
2ab5c 61 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  at expression li
2ab5d 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
2ab5e 20 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20   ExprList is an 
2ab5f 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
2ab60 50 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e  P BY clause then
2ab61 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a   the resulting.*
2ab62 2a 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  * KeyInfo struct
2ab63 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61  ure is appropria
2ab64 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a  te for initializ
2ab65 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e  ing a virtual in
2ab66 64 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d  dex to.** implem
2ab67 65 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e  ent that clause.
2ab68 20 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73    If the ExprLis
2ab69 74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20  t is the result 
2ab6a 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a  set of a SELECT.
2ab6b 2a 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49  ** then the KeyI
2ab6c 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
2ab6d 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
2ab6e 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20   initializing a 
2ab6f 76 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78  virtual.** index
2ab70 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20   to implement a 
2ab71 44 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a  DISTINCT test..*
2ab72 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f  *.** Space to ho
2ab73 6c 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ld the KeyInfo s
2ab74 74 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61  tructure is obta
2ab75 69 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20  in from malloc. 
2ab76 20 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   The calling.** 
2ab77 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70  function is resp
2ab78 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69  onsible for seei
2ab79 6e 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72  ng that this str
2ab7a 75 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75  ucture is eventu
2ab7b 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20  ally.** freed.  
2ab7c 41 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  Add the KeyInfo 
2ab7d 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65  structure to the
2ab7e 20 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e 20   P4 field of an 
2ab7f 6f 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20  opcode using.** 
2ab80 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2ab81 46 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20  FF is the usual 
2ab82 77 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77  way of dealing w
2ab83 69 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61  ith this..*/.sta
2ab84 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79  tic KeyInfo *key
2ab85 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
2ab86 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2ab87 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
2ab88 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
2ab89 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2ab8a 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79  int nExpr;.  Key
2ab8b 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73  Info *pInfo;.  s
2ab8c 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2ab8d 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e  tem *pItem;.  in
2ab8e 74 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20  t i;..  nExpr = 
2ab8f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
2ab90 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  pInfo = sqlite3D
2ab91 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2ab92 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b  sizeof(*pInfo) +
2ab93 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43   nExpr*(sizeof(C
2ab94 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20  ollSeq*)+1) );. 
2ab95 20 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20   if( pInfo ){.  
2ab96 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72    pInfo->aSortOr
2ab97 64 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66  der = (u8*)&pInf
2ab98 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b  o->aColl[nExpr];
2ab99 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65  .    pInfo->nFie
2ab9a 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b  ld = (u16)nExpr;
2ab9b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e 63 20  .    pInfo->enc 
2ab9c 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70  = ENC(db);.    p
2ab9d 49 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  Info->db = db;. 
2ab9e 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
2ab9f 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e 45  m=pList->a; i<nE
2aba0 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
2aba1 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  +){.      CollSe
2aba2 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  q *pColl;.      
2aba3 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
2aba4 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
2aba5 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  e, pItem->pExpr)
2aba6 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f  ;.      if( !pCo
2aba7 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ll ){.        pC
2aba8 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
2aba9 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
2abaa 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b     pInfo->aColl[
2abab 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  i] = pColl;.    
2abac 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72    pInfo->aSortOr
2abad 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e  der[i] = pItem->
2abae 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d  sortOrder;.    }
2abaf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 49  .  }.  return pI
2abb0 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49  nfo;.}.../*.** I
2abb1 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70  f the inner loop
2abb2 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 75   was generated u
2abb3 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20  sing a non-null 
2abb4 70 4f 72 64 65 72 42 79 20 61 72 67 75 6d 65 6e  pOrderBy argumen
2abb5 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72  t,.** then the r
2abb6 65 73 75 6c 74 73 20 77 65 72 65 20 70 6c 61 63  esults were plac
2abb7 65 64 20 69 6e 20 61 20 73 6f 72 74 65 72 2e 20  ed in a sorter. 
2abb8 20 41 66 74 65 72 20 74 68 65 20 6c 6f 6f 70 20   After the loop 
2abb9 69 73 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  is terminated.**
2abba 20 77 65 20 6e 65 65 64 20 74 6f 20 72 75 6e 20   we need to run 
2abbb 74 68 65 20 73 6f 72 74 65 72 20 61 6e 64 20 6f  the sorter and o
2abbc 75 74 70 75 74 20 74 68 65 20 72 65 73 75 6c 74  utput the result
2abbd 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  s.  The followin
2abbe 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e  g.** routine gen
2abbf 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20  erates the code 
2abc0 6e 65 65 64 65 64 20 74 6f 20 64 6f 20 74 68 61  needed to do tha
2abc1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
2abc2 64 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61  d generateSortTa
2abc3 69 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  il(.  Parse *pPa
2abc4 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
2abc5 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2abc6 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2abc7 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2abc8 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56  statement */.  V
2abc9 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20  dbe *v,         
2abca 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2abcb 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
2abcc 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
2abcd 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  n,      /* Numbe
2abce 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
2abcf 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74  data */.  Select
2abd0 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20 57  Dest *pDest /* W
2abd1 72 69 74 65 20 74 68 65 20 73 6f 72 74 65 64 20  rite the sorted 
2abd2 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
2abd3 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 42 72 65  ){.  int addrBre
2abd4 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ak = sqlite3Vdbe
2abd5 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 20  MakeLabel(v);   
2abd6 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2abd7 6f 20 65 78 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20  o exit loop */. 
2abd8 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 69 6e 75   int addrContinu
2abd9 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
2abda 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a  akeLabel(v);  /*
2abdb 20 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 6e   Jump here for n
2abdc 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69  ext cycle */.  i
2abdd 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e 74 20 69  nt addr;.  int i
2abde 54 61 62 3b 0a 20 20 69 6e 74 20 70 73 65 75 64  Tab;.  int pseud
2abdf 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72  oTab = 0;.  Expr
2abe0 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d  List *pOrderBy =
2abe1 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 0a 20   p->pOrderBy;.. 
2abe2 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
2abe3 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74  st->eDest;.  int
2abe4 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e   iParm = pDest->
2abe5 69 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20 72 65  iParm;..  int re
2abe6 67 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65 67 52  gRow;.  int regR
2abe7 6f 77 69 64 3b 0a 0a 20 20 69 54 61 62 20 3d 20  owid;..  iTab = 
2abe8 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
2abe9 6f 72 3b 0a 20 20 72 65 67 52 6f 77 20 3d 20 73  or;.  regRow = s
2abea 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2abeb 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2abec 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2abed 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f  t || eDest==SRT_
2abee 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20  Coroutine ){.   
2abef 20 70 73 65 75 64 6f 54 61 62 20 3d 20 70 50 61   pseudoTab = pPa
2abf0 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2abf1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2abf2 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65  p3(v, OP_OpenPse
2abf3 75 64 6f 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  udo, pseudoTab, 
2abf4 72 65 67 52 6f 77 2c 20 6e 43 6f 6c 75 6d 6e 29  regRow, nColumn)
2abf5 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  ;.    regRowid =
2abf6 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2abf7 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69   regRowid = sqli
2abf8 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2abf9 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 61 64 64  arse);.  }.  add
2abfa 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56  r = 1 + sqlite3V
2abfb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2abfc 53 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 72  Sort, iTab, addr
2abfd 42 72 65 61 6b 29 3b 0a 20 20 63 6f 64 65 4f 66  Break);.  codeOf
2abfe 66 73 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43  fset(v, p, addrC
2abff 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69  ontinue);.  sqli
2ac00 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2ac01 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62   OP_Column, iTab
2ac02 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  , pOrderBy->nExp
2ac03 72 20 2b 20 31 2c 20 72 65 67 52 6f 77 29 3b 0a  r + 1, regRow);.
2ac04 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
2ac05 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  ){.    case SRT_
2ac06 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
2ac07 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
2ac08 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ac09 65 44 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65  eDest==SRT_Table
2ac0a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2ac0b 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45  se( eDest==SRT_E
2ac0c 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20  phemTab );.     
2ac0d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ac0e 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
2ac0f 64 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77  d, iParm, regRow
2ac10 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
2ac11 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2ac12 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
2ac13 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77  , regRow, regRow
2ac14 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
2ac15 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2ac16 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
2ac17 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2ac18 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
2ac19 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2ac1a 59 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  Y.    case SRT_S
2ac1b 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  et: {.      asse
2ac1c 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  rt( nColumn==1 )
2ac1d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ac1e 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2ac1f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
2ac20 6f 77 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 2c  ow, 1, regRowid,
2ac21 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31   &p->affinity, 1
2ac22 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ac23 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2ac24 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2ac25 72 65 67 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20  regRow, 1);.    
2ac26 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ac27 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2ac28 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65 67 52  ert, iParm, regR
2ac29 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65  owid);.      bre
2ac2a 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2ac2b 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20  se SRT_Mem: {.  
2ac2c 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
2ac2d 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
2ac2e 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
2ac2f 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  ove(pParse, regR
2ac30 6f 77 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20  ow, iParm, 1);. 
2ac31 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
2ac32 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65  T clause will te
2ac33 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2ac34 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20   for us */.     
2ac35 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2ac36 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75 6c 74  ndif.    default
2ac37 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  : {.      int i;
2ac38 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
2ac39 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74  Dest==SRT_Output
2ac3a 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43   || eDest==SRT_C
2ac3b 6f 72 6f 75 74 69 6e 65 20 29 3b 20 0a 20 20 20  oroutine ); .   
2ac3c 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
2ac3d 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29  st==SRT_Output )
2ac3e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2ac3f 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
2ac40 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20 20  outine );.      
2ac41 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2ac42 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2ac43 20 20 61 73 73 65 72 74 28 20 72 65 67 52 6f 77    assert( regRow
2ac44 21 3d 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 20  !=pDest->iMem+i 
2ac45 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2ac46 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2ac47 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 73 65 75 64  OP_Column, pseud
2ac48 6f 54 61 62 2c 20 69 2c 20 70 44 65 73 74 2d 3e  oTab, i, pDest->
2ac49 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 20 20 20  iMem+i);.       
2ac4a 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20   if( i==0 ){.   
2ac4b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2ac4c 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
2ac4d 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 29  FLAG_CLEARCACHE)
2ac4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2ac4f 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 44    }.      if( eD
2ac50 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2ac51 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2ac52 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2ac53 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 70 44  OP_ResultRow, pD
2ac54 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75  est->iMem, nColu
2ac55 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  mn);.        sql
2ac56 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2ac57 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2ac58 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c  se, pDest->iMem,
2ac59 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
2ac5a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ac5b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ac5c 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70  1(v, OP_Yield, p
2ac5d 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20  Dest->iParm);.  
2ac5e 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2ac5f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
2ac60 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2ac61 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2ac62 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  Row);.  sqlite3R
2ac63 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2ac64 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
2ac65 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73  ..  /* LIMIT has
2ac66 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
2ac67 64 20 62 79 20 74 68 65 20 70 75 73 68 4f 6e 74  d by the pushOnt
2ac68 6f 53 6f 72 74 65 72 28 29 20 72 6f 75 74 69 6e  oSorter() routin
2ac69 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
2ac6a 28 20 70 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29  ( p->iLimit==0 )
2ac6b 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74  ;..  /* The bott
2ac6c 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20  om of the loop. 
2ac6d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2ac6e 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2ac6f 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a   addrContinue);.
2ac70 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ac71 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
2ac72 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20 20 73  iTab, addr);.  s
2ac73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2ac74 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 42 72  eLabel(v, addrBr
2ac75 65 61 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73  eak);.  if( eDes
2ac76 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
2ac77 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
2ac78 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c  utine ){.    sql
2ac79 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ac7a 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
2ac7b 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d  doTab, 0);.  }.}
2ac7c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
2ac7d 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
2ac7e 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
2ac7f 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e  the 'declaration
2ac80 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a   type' of the.**
2ac81 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
2ac82 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61  r. The string ma
2ac83 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20  y be treated as 
2ac84 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61  static by the ca
2ac85 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ller..**.** The 
2ac86 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
2ac87 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61   is the exact da
2ac88 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  tatype definitio
2ac89 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  n extracted from
2ac8a 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
2ac8b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
2ac8c 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65  atement if the e
2ac8d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
2ac8e 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65  olumn. The.** de
2ac8f 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66  claration type f
2ac90 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64  or a ROWID field
2ac91 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61   is INTEGER. Exa
2ac92 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70  ctly when an exp
2ac93 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  ression.** is co
2ac94 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
2ac95 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78  n can be complex
2ac96 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65   in the presence
2ac97 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20   of subqueries. 
2ac98 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  The.** result-se
2ac99 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  t expression in 
2ac9a 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
2ac9b 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  wing SELECT stat
2ac9c 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f  ements is .** co
2ac9d 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
2ac9e 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  n by this functi
2ac9f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  on..**.**   SELE
2aca0 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
2aca1 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
2aca2 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
2aca3 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
2aca4 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
2aca5 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43  tbl);.**   SELEC
2aca6 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45  T abc FROM (SELE
2aca7 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52  CT col AS abc FR
2aca8 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20  OM tbl);.** .** 
2aca9 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
2acaa 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
2acab 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
2acac 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
2acad 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
2acae 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
2acaf 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e  nType(.  NameCon
2acb0 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78  text *pNC, .  Ex
2acb1 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e  pr *pExpr,.  con
2acb2 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67  st char **pzOrig
2acb3 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  inDb,.  const ch
2acb4 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62  ar **pzOriginTab
2acb5 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2acb6 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a  *pzOriginCol.){.
2acb7 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
2acb8 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ype = 0;.  char 
2acb9 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62  const *zOriginDb
2acba 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
2acbb 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d  st *zOriginTab =
2acbc 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
2acbd 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30   *zOriginCol = 0
2acbe 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28  ;.  int j;.  if(
2acbf 20 4e 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29   NEVER(pExpr==0)
2acc0 20 7c 7c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73   || pNC->pSrcLis
2acc1 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
2acc2 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ..  switch( pExp
2acc3 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  r->op ){.    cas
2acc4 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a  e TK_AGG_COLUMN:
2acc5 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
2acc6 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  UMN: {.      /* 
2acc7 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  The expression i
2acc8 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 4c 6f 63 61  s a column. Loca
2acc9 74 65 20 74 68 65 20 74 61 62 6c 65 20 74 68 65  te the table the
2acca 20 63 6f 6c 75 6d 6e 20 69 73 20 62 65 69 6e 67   column is being
2accb 0a 20 20 20 20 20 20 2a 2a 20 65 78 74 72 61 63  .      ** extrac
2accc 74 65 64 20 66 72 6f 6d 20 69 6e 20 4e 61 6d 65  ted from in Name
2accd 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69 73 74  Context.pSrcList
2acce 2e 20 54 68 69 73 20 74 61 62 6c 65 20 6d 61 79  . This table may
2accf 20 62 65 20 72 65 61 6c 0a 20 20 20 20 20 20 2a   be real.      *
2acd0 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  * database table
2acd1 20 6f 72 20 61 20 73 75 62 71 75 65 72 79 2e 0a   or a subquery..
2acd2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54        */.      T
2acd3 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
2acd4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
2acd5 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  ble structure co
2acd6 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63 74 65  lumn is extracte
2acd7 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20  d from */.      
2acd8 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 30 3b 20  Select *pS = 0; 
2acd9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2acda 65 6c 65 63 74 20 74 68 65 20 63 6f 6c 75 6d 6e  elect the column
2acdb 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72   is extracted fr
2acdc 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  om */.      int 
2acdd 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43  iCol = pExpr->iC
2acde 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 49 6e 64 65 78  olumn;  /* Index
2acdf 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 54   of column in pT
2ace0 61 62 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ab */.      test
2ace1 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
2ace2 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29  =TK_AGG_COLUMN )
2ace3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2ace4 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2ace5 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
2ace6 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 21 70  while( pNC && !p
2ace7 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 53  Tab ){.        S
2ace8 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2ace9 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
2acea 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
2aceb 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  0;j<pTabList->nS
2acec 72 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e  rc && pTabList->
2aced 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45  a[j].iCursor!=pE
2acee 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29  xpr->iTable;j++)
2acef 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c  ;.        if( j<
2acf0 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29  pTabList->nSrc )
2acf1 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 61 62  {.          pTab
2acf2 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
2acf3 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  ].pTab;.        
2acf4 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74 2d    pS = pTabList-
2acf5 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a 20  >a[j].pSelect;. 
2acf6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
2acf7 20 20 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e          pNC = pN
2acf8 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  C->pNext;.      
2acf9 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
2acfa 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29     if( pTab==0 )
2acfb 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20  {.        /* At 
2acfc 6f 6e 65 20 74 69 6d 65 2c 20 63 6f 64 65 20 73  one time, code s
2acfd 75 63 68 20 61 73 20 22 53 45 4c 45 43 54 20 6e  uch as "SELECT n
2acfe 65 77 2e 78 22 20 77 69 74 68 69 6e 20 61 20 74  ew.x" within a t
2acff 72 69 67 67 65 72 20 77 6f 75 6c 64 0a 20 20 20  rigger would.   
2ad00 20 20 20 20 20 2a 2a 20 63 61 75 73 65 20 74 68       ** cause th
2ad01 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 74 6f 20  is condition to 
2ad02 72 75 6e 2e 20 20 53 69 6e 63 65 20 74 68 65 6e  run.  Since then
2ad03 2c 20 77 65 20 68 61 76 65 20 72 65 73 74 72 75  , we have restru
2ad04 63 74 75 72 65 64 20 68 6f 77 0a 20 20 20 20 20  ctured how.     
2ad05 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 63 6f     ** trigger co
2ad06 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
2ad07 61 6e 64 20 73 6f 20 74 68 69 73 20 63 6f 6e 64  and so this cond
2ad08 69 74 69 6f 6e 20 69 73 20 6e 6f 20 6c 6f 6e 67  ition is no long
2ad09 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  er .        ** p
2ad0a 6f 73 73 69 62 6c 65 2e 20 48 6f 77 65 76 65 72  ossible. However
2ad0b 2c 20 69 74 20 63 61 6e 20 73 74 69 6c 6c 20 62  , it can still b
2ad0c 65 20 74 72 75 65 20 66 6f 72 20 73 74 61 74 65  e true for state
2ad0d 6d 65 6e 74 73 20 6c 69 6b 65 0a 20 20 20 20 20  ments like.     
2ad0e 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
2ad0f 69 6e 67 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ing:.        **.
2ad10 20 20 20 20 20 20 20 20 2a 2a 20 20 20 43 52 45          **   CRE
2ad11 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 6f 6c  ATE TABLE t1(col
2ad12 20 49 4e 54 45 47 45 52 29 3b 0a 20 20 20 20 20   INTEGER);.     
2ad13 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 28     **   SELECT (
2ad14 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29 20 46  SELECT t1.col) F
2ad15 52 4f 4d 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20  ROM FROM t1;.   
2ad16 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2ad17 2a 2a 20 77 68 65 6e 20 63 6f 6c 75 6d 6e 54 79  ** when columnTy
2ad18 70 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  pe() is called o
2ad19 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
2ad1a 20 22 74 31 2e 63 6f 6c 22 20 69 6e 20 74 68 65   "t1.col" in the
2ad1b 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 75 62   .        ** sub
2ad1c 2d 73 65 6c 65 63 74 2e 20 49 6e 20 74 68 69 73  -select. In this
2ad1d 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 63   case, set the c
2ad1e 6f 6c 75 6d 6e 20 74 79 70 65 20 74 6f 20 4e 55  olumn type to NU
2ad1f 4c 4c 2c 20 65 76 65 6e 0a 20 20 20 20 20 20 20  LL, even.       
2ad20 20 2a 2a 20 74 68 6f 75 67 68 20 69 74 20 73 68   ** though it sh
2ad21 6f 75 6c 64 20 72 65 61 6c 6c 79 20 62 65 20 22  ould really be "
2ad22 49 4e 54 45 47 45 52 22 2e 0a 20 20 20 20 20 20  INTEGER"..      
2ad23 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2ad24 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 70 72  This is not a pr
2ad25 6f 62 6c 65 6d 2c 20 61 73 20 74 68 65 20 63 6f  oblem, as the co
2ad26 6c 75 6d 6e 20 74 79 70 65 20 6f 66 20 22 74 31  lumn type of "t1
2ad27 2e 63 6f 6c 22 20 69 73 20 6e 65 76 65 72 0a 20  .col" is never. 
2ad28 20 20 20 20 20 20 20 2a 2a 20 75 73 65 64 2e 20         ** used. 
2ad29 57 68 65 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28  When columnType(
2ad2a 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ) is called on t
2ad2b 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 0a 20  he expression . 
2ad2c 20 20 20 20 20 20 20 2a 2a 20 22 28 53 45 4c 45         ** "(SELE
2ad2d 43 54 20 74 31 2e 63 6f 6c 29 22 2c 20 74 68 65  CT t1.col)", the
2ad2e 20 63 6f 72 72 65 63 74 20 74 79 70 65 20 69 73   correct type is
2ad2f 20 72 65 74 75 72 6e 65 64 20 28 73 65 65 20 74   returned (see t
2ad30 68 65 20 54 4b 5f 53 45 4c 45 43 54 0a 20 20 20  he TK_SELECT.   
2ad31 20 20 20 20 20 2a 2a 20 62 72 61 6e 63 68 20 62       ** branch b
2ad32 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20 20 20 20 20  elow.  */.      
2ad33 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2ad34 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2ad35 70 54 61 62 20 26 26 20 70 45 78 70 72 2d 3e 70  pTab && pExpr->p
2ad36 54 61 62 3d 3d 70 54 61 62 20 29 3b 0a 20 20 20  Tab==pTab );.   
2ad37 20 20 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20     if( pS ){.   
2ad38 20 20 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62       /* The "tab
2ad39 6c 65 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20  le" is actually 
2ad3a 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20  a sub-select or 
2ad3b 61 20 76 69 65 77 20 69 6e 20 74 68 65 20 46 52  a view in the FR
2ad3c 4f 4d 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20  OM clause.      
2ad3d 20 20 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45    ** of the SELE
2ad3e 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65  CT statement. Re
2ad3f 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
2ad40 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72  tion type and or
2ad41 69 67 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  igin.        ** 
2ad42 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 73  data for the res
2ad43 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f  ult-set column o
2ad44 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74  f the sub-select
2ad45 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2ad46 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2ad47 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c  iCol>=0 && iCol<
2ad48 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  pS->pEList->nExp
2ad49 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r) ){.          
2ad4a 2f 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6c 65  /* If iCol is le
2ad4b 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
2ad4c 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
2ad4d 6e 20 72 65 71 75 65 73 74 73 20 74 68 65 0a 20  n requests the. 
2ad4e 20 20 20 20 20 20 20 20 20 2a 2a 20 72 6f 77 69           ** rowi
2ad4f 64 20 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c  d of the sub-sel
2ad50 65 63 74 20 6f 72 20 76 69 65 77 2e 20 54 68 69  ect or view. Thi
2ad51 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  s expression is 
2ad52 6c 65 67 61 6c 20 28 73 65 65 20 0a 20 20 20 20  legal (see .    
2ad53 20 20 20 20 20 20 2a 2a 20 74 65 73 74 20 63 61        ** test ca
2ad54 73 65 20 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20  se misc2.2.2) - 
2ad55 69 74 20 61 6c 77 61 79 73 20 65 76 61 6c 75 61  it always evalua
2ad56 74 65 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20  tes to NULL..   
2ad57 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2ad58 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
2ad59 73 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 45  sNC;.          E
2ad5a 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c  xpr *p = pS->pEL
2ad5b 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
2ad5c 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e  pr;.          sN
2ad5d 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d  C.pSrcList = pS-
2ad5e 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20  >pSrc;.         
2ad5f 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43   sNC.pNext = pNC
2ad60 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e  ;.          sNC.
2ad61 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
2ad62 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  arse;.          
2ad63 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
2ad64 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
2ad65 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e  iginDb, &zOrigin
2ad66 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c  Tab, &zOriginCol
2ad67 29 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ); .        }.  
2ad68 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c      }else if( AL
2ad69 57 41 59 53 28 70 54 61 62 2d 3e 70 53 63 68 65  WAYS(pTab->pSche
2ad6a 6d 61 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ma) ){.        /
2ad6b 2a 20 41 20 72 65 61 6c 20 74 61 62 6c 65 20 2a  * A real table *
2ad6c 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
2ad6d 28 20 21 70 53 20 29 3b 0a 20 20 20 20 20 20 20  ( !pS );.       
2ad6e 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69 43   if( iCol<0 ) iC
2ad6f 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  ol = pTab->iPKey
2ad70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2ad71 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69  ( iCol==-1 || (i
2ad72 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70  Col>=0 && iCol<p
2ad73 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20  Tab->nCol) );.  
2ad74 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
2ad75 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54   ){.          zT
2ad76 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b  ype = "INTEGER";
2ad77 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
2ad78 69 6e 43 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b  inCol = "rowid";
2ad79 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2ad7a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
2ad7b 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
2ad7c 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20 20  l].zType;.      
2ad7d 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d      zOriginCol =
2ad7e 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
2ad7f 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
2ad80 20 7d 0a 20 20 20 20 20 20 20 20 7a 4f 72 69 67   }.        zOrig
2ad81 69 6e 54 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e  inTab = pTab->zN
2ad82 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ame;.        if(
2ad83 20 70 4e 43 2d 3e 70 50 61 72 73 65 20 29 7b 0a   pNC->pParse ){.
2ad84 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44            int iD
2ad85 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2ad86 61 54 6f 49 6e 64 65 78 28 70 4e 43 2d 3e 70 50  aToIndex(pNC->pP
2ad87 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
2ad88 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
2ad89 20 20 20 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20      zOriginDb = 
2ad8a 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d  pNC->pParse->db-
2ad8b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
2ad8c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ad8d 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2ad8e 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
2ad8f 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2ad90 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  RY.    case TK_S
2ad91 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f  ELECT: {.      /
2ad92 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
2ad93 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74   is a sub-select
2ad94 2e 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 63  . Return the dec
2ad95 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e  laration type an
2ad96 64 0a 20 20 20 20 20 20 2a 2a 20 6f 72 69 67 69  d.      ** origi
2ad97 6e 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 73  n info for the s
2ad98 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20  ingle column in 
2ad99 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
2ad9a 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  f the SELECT.   
2ad9b 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e     ** statement.
2ad9c 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2ad9d 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
2ad9e 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
2ad9f 53 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  S = pExpr->x.pSe
2ada0 6c 65 63 74 3b 0a 20 20 20 20 20 20 45 78 70 72  lect;.      Expr
2ada1 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74   *p = pS->pEList
2ada2 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
2ada3 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
2ada4 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2ada5 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2ada6 20 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 53   );.      sNC.pS
2ada7 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72  rcList = pS->pSr
2ada8 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 4e 65  c;.      sNC.pNe
2ada9 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20  xt = pNC;.      
2adaa 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43  sNC.pParse = pNC
2adab 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20  ->pParse;.      
2adac 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
2adad 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
2adae 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e  iginDb, &zOrigin
2adaf 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c  Tab, &zOriginCol
2adb0 29 3b 20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ); .      break;
2adb1 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
2adb2 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a 4f 72 69  }.  .  if( pzOri
2adb3 67 69 6e 44 62 20 29 7b 0a 20 20 20 20 61 73 73  ginDb ){.    ass
2adb4 65 72 74 28 20 70 7a 4f 72 69 67 69 6e 54 61 62  ert( pzOriginTab
2adb5 20 26 26 20 70 7a 4f 72 69 67 69 6e 43 6f 6c 20   && pzOriginCol 
2adb6 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  );.    *pzOrigin
2adb7 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44 62 3b 0a  Db = zOriginDb;.
2adb8 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 54 61 62      *pzOriginTab
2adb9 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62 3b 0a 20   = zOriginTab;. 
2adba 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 20     *pzOriginCol 
2adbb 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b 0a 20 20  = zOriginCol;.  
2adbc 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54 79 70 65  }.  return zType
2adbd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2adbe 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
2adbf 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45  ll tell the VDBE
2adc0 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
2adc1 20 74 79 70 65 73 20 6f 66 20 63 6f 6c 75 6d 6e   types of column
2adc2 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75  s.** in the resu
2adc3 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  lt set..*/.stati
2adc4 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43  c void generateC
2adc5 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20 20 50 61  olumnTypes(.  Pa
2adc6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2adc7 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2adc8 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
2adc9 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20   *pTabList,  /* 
2adca 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a  List of tables *
2adcb 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2adcc 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65  List    /* Expre
2adcd 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20  ssions defining 
2adce 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
2adcf 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  /.){.#ifndef SQL
2add0 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
2add1 45 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  E.  Vdbe *v = pP
2add2 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2add3 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  nt i;.  NameCont
2add4 65 78 74 20 73 4e 43 3b 0a 20 20 73 4e 43 2e 70  ext sNC;.  sNC.p
2add5 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
2add6 73 74 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  st;.  sNC.pParse
2add7 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 66 6f 72   = pParse;.  for
2add8 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e  (i=0; i<pEList->
2add9 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2adda 20 45 78 70 72 20 2a 70 20 3d 20 70 45 4c 69 73   Expr *p = pELis
2addb 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
2addc 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2addd 54 79 70 65 3b 0a 23 69 66 64 65 66 20 53 51 4c  Type;.#ifdef SQL
2adde 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
2addf 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 20 20 63  N_METADATA.    c
2ade0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67  onst char *zOrig
2ade1 44 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73  Db = 0;.    cons
2ade2 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 54 61 62  t char *zOrigTab
2ade3 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20   = 0;.    const 
2ade4 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d  char *zOrigCol =
2ade5 20 30 3b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   0;.    zType = 
2ade6 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
2ade7 20 70 2c 20 26 7a 4f 72 69 67 44 62 2c 20 26 7a   p, &zOrigDb, &z
2ade8 4f 72 69 67 54 61 62 2c 20 26 7a 4f 72 69 67 43  OrigTab, &zOrigC
2ade9 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ol);..    /* The
2adea 20 76 64 62 65 20 6d 75 73 74 20 6d 61 6b 65 20   vdbe must make 
2adeb 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20  its own copy of 
2adec 74 68 65 20 63 6f 6c 75 6d 6e 2d 74 79 70 65 20  the column-type 
2aded 61 6e 64 20 6f 74 68 65 72 20 0a 20 20 20 20 2a  and other .    *
2adee 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69  * column specifi
2adef 63 20 73 74 72 69 6e 67 73 2c 20 69 6e 20 63 61  c strings, in ca
2adf0 73 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  se the schema is
2adf1 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 74 68   reset before th
2adf2 69 73 0a 20 20 20 20 2a 2a 20 76 69 72 74 75 61  is.    ** virtua
2adf3 6c 20 6d 61 63 68 69 6e 65 20 69 73 20 64 65 6c  l machine is del
2adf4 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
2adf5 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2adf6 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2adf7 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 2c 20 7a  NAME_DATABASE, z
2adf8 4f 72 69 67 44 62 2c 20 53 51 4c 49 54 45 5f 54  OrigDb, SQLITE_T
2adf9 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
2adfa 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2adfb 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
2adfc 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72 69 67 54  ME_TABLE, zOrigT
2adfd 61 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ab, SQLITE_TRANS
2adfe 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
2adff 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2ae00 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43  (v, i, COLNAME_C
2ae01 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c  OLUMN, zOrigCol,
2ae02 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2ae03 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 7a 54  T);.#else.    zT
2ae04 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65  ype = columnType
2ae05 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20  (&sNC, p, 0, 0, 
2ae06 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  0);.#endif.    s
2ae07 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2ae08 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
2ae09 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20 7a 54 79  ME_DECLTYPE, zTy
2ae0a 70 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  pe, SQLITE_TRANS
2ae0b 49 45 4e 54 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  IENT);.  }.#endi
2ae0c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2ae0d 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a  _DECLTYPE */.}..
2ae0e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2ae0f 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74 65  ode that will te
2ae10 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65 20  ll the VDBE the 
2ae11 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
2ae12 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
2ae13 74 20 73 65 74 2e 20 20 54 68 69 73 20 69 6e 66  t set.  This inf
2ae14 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
2ae15 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a   to provide the.
2ae16 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65  ** azCol[] value
2ae17 73 20 69 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  s in the callbac
2ae18 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
2ae19 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e  d generateColumn
2ae1a 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a  Names(.  Parse *
2ae1b 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
2ae1c 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
2ae1d 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
2ae1e 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20  bList,  /* List 
2ae1f 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45  of tables */.  E
2ae20 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2ae21 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
2ae22 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 72  s defining the r
2ae23 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a  esult set */.){.
2ae24 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2ae25 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2ae26 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33   i, j;.  sqlite3
2ae27 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ae28 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d  b;.  int fullNam
2ae29 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a  es, shortNames;.
2ae2a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ae2b 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f  OMIT_EXPLAIN.  /
2ae2c 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
2ae2d 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68  EXPLAIN, skip th
2ae2e 69 73 20 73 74 65 70 20 2a 2f 0a 20 20 69 66 28  is step */.  if(
2ae2f 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
2ae30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2ae31 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
2ae32 28 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d  ( pParse->colNam
2ae33 65 73 53 65 74 20 7c 7c 20 4e 45 56 45 52 28 76  esSet || NEVER(v
2ae34 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  ==0) || db->mall
2ae35 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2ae36 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  n;.  pParse->col
2ae37 4e 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20  NamesSet = 1;.  
2ae38 66 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d  fullNames = (db-
2ae39 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2ae3a 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30  FullColNames)!=0
2ae3b 3b 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d  ;.  shortNames =
2ae3c 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
2ae3d 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
2ae3e 65 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65  es)!=0;.  sqlite
2ae3f 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2ae40 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  v, pEList->nExpr
2ae41 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2ae42 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
2ae43 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
2ae44 3b 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74  ;.    p = pEList
2ae45 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2ae46 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30    if( NEVER(p==0
2ae47 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2ae48 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b    if( pEList->a[
2ae49 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  i].zName ){.    
2ae4a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
2ae4b 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61  pEList->a[i].zNa
2ae4c 6d 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  me;.      sqlite
2ae4d 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2ae4e 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
2ae4f 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54  ME, zName, SQLIT
2ae50 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
2ae51 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e    }else if( (p->
2ae52 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
2ae53 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43   p->op==TK_AGG_C
2ae54 4f 4c 55 4d 4e 29 20 26 26 20 70 54 61 62 4c 69  OLUMN) && pTabLi
2ae55 73 74 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c  st ){.      Tabl
2ae56 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 63  e *pTab;.      c
2ae57 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20  har *zCol;.     
2ae58 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 69   int iCol = p->i
2ae59 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 66 6f  Column;.      fo
2ae5a 72 28 6a 3d 30 3b 20 41 4c 57 41 59 53 28 6a 3c  r(j=0; ALWAYS(j<
2ae5b 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b  pTabList->nSrc);
2ae5c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
2ae5d 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a  f( pTabList->a[j
2ae5e 5d 2e 69 43 75 72 73 6f 72 3d 3d 70 2d 3e 69 54  ].iCursor==p->iT
2ae5f 61 62 6c 65 20 29 20 62 72 65 61 6b 3b 0a 20 20  able ) break;.  
2ae60 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
2ae61 72 74 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  rt( j<pTabList->
2ae62 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 70 54  nSrc );.      pT
2ae63 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ab = pTabList->a
2ae64 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20  [j].pTab;.      
2ae65 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f  if( iCol<0 ) iCo
2ae66 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b  l = pTab->iPKey;
2ae67 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
2ae68 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c  Col==-1 || (iCol
2ae69 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
2ae6a 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20  ->nCol) );.     
2ae6b 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20   if( iCol<0 ){. 
2ae6c 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 22 72         zCol = "r
2ae6d 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c  owid";.      }el
2ae6e 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  se{.        zCol
2ae6f 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
2ae70 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ol].zName;.     
2ae71 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 73 68   }.      if( !sh
2ae72 6f 72 74 4e 61 6d 65 73 20 26 26 20 21 66 75 6c  ortNames && !ful
2ae73 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20 20 20  lNames ){.      
2ae74 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2ae75 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
2ae76 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20  LNAME_NAME, .   
2ae77 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2ae78 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 4c  DbStrDup(db, pEL
2ae79 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29  ist->a[i].zSpan)
2ae7a 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2ae7b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
2ae7c 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a  f( fullNames ){.
2ae7d 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
2ae7e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ame = 0;.       
2ae7f 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
2ae80 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e  MPrintf(db, "%s.
2ae81 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
2ae82 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , zCol);.       
2ae83 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2ae84 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2ae85 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
2ae86 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2ae87 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2ae88 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2ae89 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2ae8a 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
2ae8b 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54  , zCol, SQLITE_T
2ae8c 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
2ae8d 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
2ae8e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2ae8f 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
2ae90 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
2ae91 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2ae92 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 4c  DbStrDup(db, pEL
2ae93 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29  ist->a[i].zSpan)
2ae94 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2ae95 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67  );.    }.  }.  g
2ae96 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70  enerateColumnTyp
2ae97 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  es(pParse, pTabL
2ae98 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a  ist, pEList);.}.
2ae99 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ae9a 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
2ae9b 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20  LECT./*.** Name 
2ae9c 6f 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  of the connectio
2ae9d 6e 20 6f 70 65 72 61 74 6f 72 2c 20 75 73 65 64  n operator, used
2ae9e 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
2ae9f 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ges..*/.static c
2aea0 6f 6e 73 74 20 63 68 61 72 20 2a 73 65 6c 65 63  onst char *selec
2aea1 74 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b  tOpName(int id){
2aea2 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77  .  char *z;.  sw
2aea3 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20  itch( id ){.    
2aea4 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20  case TK_ALL:    
2aea5 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c     z = "UNION AL
2aea6 4c 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  L";   break;.   
2aea7 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53 45   case TK_INTERSE
2aea8 43 54 3a 20 7a 20 3d 20 22 49 4e 54 45 52 53 45  CT: z = "INTERSE
2aea9 43 54 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20  CT";   break;.  
2aeaa 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54    case TK_EXCEPT
2aeab 3a 20 20 20 20 7a 20 3d 20 22 45 58 43 45 50 54  :    z = "EXCEPT
2aeac 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ";      break;. 
2aead 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
2aeae 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e        z = "UNION
2aeaf 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
2aeb0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
2aeb1 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2aeb2 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
2aeb3 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a  _SELECT */../*.*
2aeb4 2a 20 47 69 76 65 6e 20 61 20 61 6e 20 65 78 70  * Given a an exp
2aeb5 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 28 77 68  ression list (wh
2aeb6 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 74 68  ich is really th
2aeb7 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73  e list of expres
2aeb8 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 66 6f  sions.** that fo
2aeb9 72 6d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  rm the result se
2aeba 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
2aebb 61 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75 74 65  atement) compute
2aebc 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
2aebd 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  column names for
2aebe 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 77 6f   a table that wo
2aebf 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 78 70  uld hold the exp
2aec0 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a  ression list..**
2aec1 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e  .** All column n
2aec2 61 6d 65 73 20 77 69 6c 6c 20 62 65 20 75 6e 69  ames will be uni
2aec3 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  que..**.** Only 
2aec4 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2aec5 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e 20 20   are computed.  
2aec6 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f  Column.zType, Co
2aec7 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61  lumn.zColl,.** a
2aec8 6e 64 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20  nd other fields 
2aec9 6f 66 20 43 6f 6c 75 6d 6e 20 61 72 65 20 7a 65  of Column are ze
2aeca 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  roed..**.** Retu
2aecb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
2aecc 73 75 63 63 65 73 73 2e 20 20 49 66 20 61 20 6d  success.  If a m
2aecd 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
2aece 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a   error occurs,.*
2aecf 2a 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20  * store NULL in 
2aed0 2a 70 61 43 6f 6c 20 61 6e 64 20 30 20 69 6e 20  *paCol and 0 in 
2aed1 2a 70 6e 43 6f 6c 20 61 6e 64 20 72 65 74 75 72  *pnCol and retur
2aed2 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  n SQLITE_NOMEM..
2aed3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
2aed4 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45  lectColumnsFromE
2aed5 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65  xprList(.  Parse
2aed6 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2aed7 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2aed8 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
2aed9 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20  ist *pEList,    
2aeda 20 20 20 2f 2a 20 45 78 70 72 20 6c 69 73 74 20     /* Expr list 
2aedb 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 64 65  from which to de
2aedc 72 69 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  rive column name
2aedd 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  s */.  int *pnCo
2aede 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l,             /
2aedf 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62  * Write the numb
2aee0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 68 65  er of columns he
2aee1 72 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  re */.  Column *
2aee2 2a 70 61 43 6f 6c 20 20 20 20 20 20 20 20 20 20  *paCol          
2aee3 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
2aee4 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 65 72   column list her
2aee5 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  e */.){.  sqlite
2aee6 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2aee7 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73  db;   /* Databas
2aee8 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2aee9 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
2aeea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2aeeb 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
2aeec 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  /.  int cnt;    
2aeed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aeee 2f 2a 20 49 6e 64 65 78 20 61 64 64 65 64 20 74  /* Index added t
2aeef 6f 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d 65 20  o make the name 
2aef0 75 6e 69 71 75 65 20 2a 2f 0a 20 20 43 6f 6c 75  unique */.  Colu
2aef1 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b  mn *aCol, *pCol;
2aef2 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
2aef3 6f 6f 70 69 6e 67 20 6f 76 65 72 20 72 65 73 75  ooping over resu
2aef4 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  lt columns */.  
2aef5 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20  int nCol;       
2aef6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2aef7 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2aef8 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2aef9 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 3b  et */.  Expr *p;
2aefa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aefb 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2aefc 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 72  n for a single r
2aefd 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  esult column */.
2aefe 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
2aeff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2af00 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
2af01 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20    int nName;    
2af02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2af03 20 53 69 7a 65 20 6f 66 20 6e 61 6d 65 20 69 6e   Size of name in
2af04 20 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a   zName[] */..  *
2af05 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70  pnCol = nCol = p
2af06 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
2af07 61 43 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20  aCol = *paCol = 
2af08 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2af09 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61  ero(db, sizeof(a
2af0a 43 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20  Col[0])*nCol);. 
2af0b 20 69 66 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72   if( aCol==0 ) r
2af0c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2af0d 45 4d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  EM;.  for(i=0, p
2af0e 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c  Col=aCol; i<nCol
2af0f 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
2af10 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70      /* Get an ap
2af11 70 72 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66  propriate name f
2af12 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20  or the column.  
2af13 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c    */.    p = pEL
2af14 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
2af15 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
2af16 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 45 78 70  pRight==0 || Exp
2af17 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e  rHasProperty(p->
2af18 70 52 69 67 68 74 2c 20 45 50 5f 49 6e 74 56 61  pRight, EP_IntVa
2af19 6c 75 65 29 0a 20 20 20 20 20 20 20 20 20 20 20  lue).           
2af1a 20 20 20 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74      || p->pRight
2af1b 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c  ->u.zToken==0 ||
2af1c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54   p->pRight->u.zT
2af1d 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20  oken[0]!=0 );.  
2af1e 20 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70    if( (zName = p
2af1f 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  EList->a[i].zNam
2af20 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  e)!=0 ){.      /
2af21 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  * If the column 
2af22 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20  contains an "AS 
2af23 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20  <name>" phrase, 
2af24 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68  use <name> as th
2af25 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20  e name */.      
2af26 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
2af27 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d  bStrDup(db, zNam
2af28 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
2af29 20 20 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45       Expr *pColE
2af2a 78 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65  xpr = p;  /* The
2af2b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
2af2c 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63   is the result c
2af2d 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
2af2e 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
2af2f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2af30 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
2af31 68 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  h this expressio
2af32 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
2af33 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d  ( pColExpr->op==
2af34 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 45 78 70  TK_DOT ) pColExp
2af35 72 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 52  r = pColExpr->pR
2af36 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20  ight;.      if( 
2af37 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  pColExpr->op==TK
2af38 5f 43 4f 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59  _COLUMN && ALWAY
2af39 53 28 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62  S(pColExpr->pTab
2af3a 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
2af3b 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75  /* For columns u
2af3c 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  se the column na
2af3d 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  me name */.     
2af3e 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43     int iCol = pC
2af3f 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  olExpr->iColumn;
2af40 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
2af41 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0a  pColExpr->pTab;.
2af42 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
2af43 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
2af44 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20  ->iPKey;.       
2af45 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
2af46 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
2af47 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2af48 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61     iCol>=0 ? pTa
2af49 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
2af4a 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a  ame : "rowid");.
2af4b 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2af4c 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  pColExpr->op==TK
2af4d 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 61  _ID ){.        a
2af4e 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2af4f 72 6f 70 65 72 74 79 28 70 43 6f 6c 45 78 70 72  roperty(pColExpr
2af50 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2af51 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  ;.        zName 
2af52 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2af53 28 64 62 2c 20 22 25 73 22 2c 20 70 43 6f 6c 45  (db, "%s", pColE
2af54 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  xpr->u.zToken);.
2af55 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2af56 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20       /* Use the 
2af57 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66  original text of
2af58 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72   the column expr
2af59 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61  ession as its na
2af5a 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e  me */.        zN
2af5b 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2af5c 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70  intf(db, "%s", p
2af5d 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  EList->a[i].zSpa
2af5e 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
2af5f 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  }.    if( db->ma
2af60 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2af61 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2af62 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  e(db, zName);.  
2af63 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2af64 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75  ..    /* Make su
2af65 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  re the column na
2af66 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20 49  me is unique.  I
2af67 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f  f the name is no
2af68 74 20 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a  t unique,.    **
2af69 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65 67 65   append a intege
2af6a 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f  r to the name so
2af6b 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d 65 73   that it becomes
2af6c 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a   unique..    */.
2af6d 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69      nName = sqli
2af6e 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2af6f 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e  e);.    for(j=cn
2af70 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a  t=0; j<i; j++){.
2af71 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2af72 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d  3StrICmp(aCol[j]
2af73 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d  .zName, zName)==
2af74 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  0 ){.        cha
2af75 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20  r *zNewName;.   
2af76 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65       zName[nName
2af77 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a  ] = 0;.        z
2af78 4e 65 77 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  NewName = sqlite
2af79 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
2af7a 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63  :%d", zName, ++c
2af7b 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nt);.        sql
2af7c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2af7d 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a  Name);.        z
2af7e 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b  Name = zNewName;
2af7f 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b  .        j = -1;
2af80 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 4e 61  .        if( zNa
2af81 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  me==0 ) break;. 
2af82 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2af83 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a   pCol->zName = z
2af84 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Name;.  }.  if( 
2af85 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2af86 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b   ){.    for(j=0;
2af87 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<i; j++){.    
2af88 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2af89 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  db, aCol[j].zNam
2af8a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
2af8b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2af8c 61 43 6f 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f  aCol);.    *paCo
2af8d 6c 20 3d 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f  l = 0;.    *pnCo
2af8e 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  l = 0;.    retur
2af8f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2af90 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2af91 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
2af92 20 41 64 64 20 74 79 70 65 20 61 6e 64 20 63 6f   Add type and co
2af93 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  llation informat
2af94 69 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  ion to a column 
2af95 6c 69 73 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a  list based on.**
2af96 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2af97 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ent..** .** The 
2af98 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72 65 73  column list pres
2af99 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d  umably came from
2af9a 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d   selectColumnNam
2af9b 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 29  esFromExprList()
2af9c 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20  ..** The column 
2af9d 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61  list has only na
2af9e 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f  mes, not types o
2af9f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54  r collations.  T
2afa0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67  his.** routine g
2afa1 6f 65 73 20 74 68 72 6f 75 67 68 20 61 6e 64 20  oes through and 
2afa2 61 64 64 73 20 74 68 65 20 74 79 70 65 73 20 61  adds the types a
2afa3 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a  nd collations..*
2afa4 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2afa5 65 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20  e requires that 
2afa6 61 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20  all identifiers 
2afa7 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  in the SELECT.**
2afa8 20 73 74 61 74 65 6d 65 6e 74 20 62 65 20 72 65   statement be re
2afa9 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  solved..*/.stati
2afaa 63 20 76 6f 69 64 20 73 65 6c 65 63 74 41 64 64  c void selectAdd
2afab 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
2afac 6c 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  lation(.  Parse 
2afad 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2afae 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2afaf 78 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  xts */.  int nCo
2afb0 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l,             /
2afb1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
2afb2 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20  mns */.  Column 
2afb3 2a 61 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f  *aCol,         /
2afb4 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  * List of column
2afb5 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  s */.  Select *p
2afb6 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20  Select       /* 
2afb7 53 45 4c 45 43 54 20 75 73 65 64 20 74 6f 20 64  SELECT used to d
2afb8 65 74 65 72 6d 69 6e 65 20 74 79 70 65 73 20 61  etermine types a
2afb9 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f  nd collations */
2afba 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
2afbb 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2afbc 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
2afbd 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  C;.  Column *pCo
2afbe 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  l;.  CollSeq *pC
2afbf 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  oll;.  int i;.  
2afc0 45 78 70 72 20 2a 70 3b 0a 20 20 73 74 72 75 63  Expr *p;.  struc
2afc1 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2afc2 2a 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  *a;..  assert( p
2afc3 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20 20 61  Select!=0 );.  a
2afc4 73 73 65 72 74 28 20 28 70 53 65 6c 65 63 74 2d  ssert( (pSelect-
2afc5 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52  >selFlags & SF_R
2afc6 65 73 6f 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20  esolved)!=0 );. 
2afc7 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70   assert( nCol==p
2afc8 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e  Select->pEList->
2afc9 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  nExpr || db->mal
2afca 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
2afcb 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2afcc 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
2afcd 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
2afce 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
2afcf 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2afd0 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20  Select->pSrc;.  
2afd1 61 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  a = pSelect->pEL
2afd2 69 73 74 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d  ist->a;.  for(i=
2afd3 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c  0, pCol=aCol; i<
2afd4 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
2afd5 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d  +){.    p = a[i]
2afd6 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c  .pExpr;.    pCol
2afd7 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65  ->zType = sqlite
2afd8 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 63 6f  3DbStrDup(db, co
2afd9 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70  lumnType(&sNC, p
2afda 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20  , 0, 0, 0));.   
2afdb 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
2afdc 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
2afdd 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20 69 66  inity(p);.    if
2afde 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
2afdf 3d 3d 30 20 29 20 70 43 6f 6c 2d 3e 61 66 66 69  ==0 ) pCol->affi
2afe0 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
2afe1 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 43 6f 6c  F_NONE;.    pCol
2afe2 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
2afe3 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2afe4 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
2afe5 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e   ){.      pCol->
2afe6 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44  zColl = sqlite3D
2afe7 62 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c  bStrDup(db, pCol
2afe8 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  l->zName);.    }
2afe9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
2afea 76 65 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61  ven a SELECT sta
2afeb 74 65 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65  tement, generate
2afec 20 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75   a Table structu
2afed 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
2afee 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20  s.** the result 
2afef 73 65 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45  set of that SELE
2aff0 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  CT..*/.SQLITE_PR
2aff1 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
2aff2 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
2aff3 65 6c 65 63 74 28 50 61 72 73 65 20 2a 70 50 61  elect(Parse *pPa
2aff4 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65  rse, Select *pSe
2aff5 6c 65 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a  lect){.  Table *
2aff6 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20  pTab;.  sqlite3 
2aff7 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2aff8 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 46 6c 61  ;.  int savedFla
2aff9 67 73 3b 0a 0a 20 20 73 61 76 65 64 46 6c 61 67  gs;..  savedFlag
2affa 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20  s = db->flags;. 
2affb 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53   db->flags &= ~S
2affc 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
2affd 65 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  es;.  db->flags 
2affe 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43  |= SQLITE_ShortC
2afff 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74  olNames;.  sqlit
2b000 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
2b001 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 30 29  rse, pSelect, 0)
2b002 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2b003 6e 45 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b  nErr ) return 0;
2b004 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
2b005 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c  t->pPrior ) pSel
2b006 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ect = pSelect->p
2b007 50 72 69 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61  Prior;.  db->fla
2b008 67 73 20 3d 20 73 61 76 65 64 46 6c 61 67 73 3b  gs = savedFlags;
2b009 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2b00a 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2b00b 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20  , sizeof(Table) 
2b00c 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  );.  if( pTab==0
2b00d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2b00e 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73  ;.  }.  /* The s
2b00f 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
2b010 66 53 65 6c 65 63 74 28 29 20 69 73 20 6f 6e 6c  fSelect() is onl
2b011 79 20 75 73 65 64 20 6e 20 63 6f 6e 74 65 78 74  y used n context
2b012 73 20 77 68 65 72 65 20 6c 6f 6f 6b 61 73 69 64  s where lookasid
2b013 65 0a 20 20 2a 2a 20 69 73 20 64 69 73 61 62 6c  e.  ** is disabl
2b014 65 64 2c 20 73 6f 20 77 65 20 6d 69 67 68 74 20  ed, so we might 
2b015 61 73 20 77 65 6c 6c 20 68 61 72 64 2d 63 6f 64  as well hard-cod
2b016 65 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20 74 6f  e pTab->dbMem to
2b017 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65   NULL. */.  asse
2b018 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  rt( db->lookasid
2b019 65 2e 62 45 6e 61 62 6c 65 64 3d 3d 30 20 29 3b  e.bEnabled==0 );
2b01a 0a 20 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20 3d  .  pTab->dbMem =
2b01b 20 30 3b 0a 20 20 70 54 61 62 2d 3e 6e 52 65 66   0;.  pTab->nRef
2b01c 20 3d 20 31 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e   = 1;.  pTab->zN
2b01d 61 6d 65 20 3d 20 30 3b 0a 20 20 73 65 6c 65 63  ame = 0;.  selec
2b01e 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72  tColumnsFromExpr
2b01f 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65  List(pParse, pSe
2b020 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 26 70  lect->pEList, &p
2b021 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62  Tab->nCol, &pTab
2b022 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c 65 63  ->aCol);.  selec
2b023 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e  tAddColumnTypeAn
2b024 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73  dCollation(pPars
2b025 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70  e, pTab->nCol, p
2b026 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 65  Tab->aCol, pSele
2b027 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69 50 4b  ct);.  pTab->iPK
2b028 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64  ey = -1;.  if( d
2b029 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b02a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2b02b 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b  leteTable(pTab);
2b02c 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2b02d 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62   }.  return pTab
2b02e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  ;.}../*.** Get a
2b02f 20 56 44 42 45 20 66 6f 72 20 74 68 65 20 67 69   VDBE for the gi
2b030 76 65 6e 20 70 61 72 73 65 72 20 63 6f 6e 74 65  ven parser conte
2b031 78 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65  xt.  Create a ne
2b032 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61  w one if necessa
2b033 72 79 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  ry..** If an err
2b034 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72  or occurs, retur
2b035 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c 65 61 76 65  n NULL and leave
2b036 20 61 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50   a message in pP
2b037 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arse..*/.SQLITE_
2b038 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71  PRIVATE Vdbe *sq
2b039 6c 69 74 65 33 47 65 74 56 64 62 65 28 50 61 72  lite3GetVdbe(Par
2b03a 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56  se *pParse){.  V
2b03b 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2b03c 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20 76 3d  >pVdbe;.  if( v=
2b03d 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20 70 50  =0 ){.    v = pP
2b03e 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71  arse->pVdbe = sq
2b03f 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
2b040 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 23 69 66  pParse->db);.#if
2b041 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b042 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 76  _TRACE.    if( v
2b043 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2b044 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
2b045 50 5f 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a  P_Trace);.    }.
2b046 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
2b047 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn v;.}.../*.**
2b048 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69 4c 69   Compute the iLi
2b049 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
2b04a 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 53 45  fields of the SE
2b04b 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20 74 68  LECT based on th
2b04c 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e 64 20  e.** pLimit and 
2b04d 70 4f 66 66 73 65 74 20 65 78 70 72 65 73 73 69  pOffset expressi
2b04e 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61 6e 64  ons.  pLimit and
2b04f 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20 74 68   pOffset hold th
2b050 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  e expressions.**
2b051 20 74 68 61 74 20 61 70 70 65 61 72 20 69 6e 20   that appear in 
2b052 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
2b053 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72   statement after
2b054 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f   the LIMIT and O
2b055 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f 72 64  FFSET.** keyword
2b056 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66 20 74  s.  Or NULL if t
2b057 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 61 72  hose keywords ar
2b058 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69 6d 69  e omitted. iLimi
2b059 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 0a 2a  t and iOffset .*
2b05a 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65  * are the intege
2b05b 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  r memory registe
2b05c 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 63 6f  r numbers for co
2b05d 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f 20 63  unters used to c
2b05e 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65 20 6c  ompute .** the l
2b05f 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 2e  imit and offset.
2b060 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
2b061 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66   limit and/or of
2b062 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a 20 69  fset, then .** i
2b063 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
2b064 74 20 61 72 65 20 6e 65 67 61 74 69 76 65 2e 0a  t are negative..
2b065 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2b066 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 76  ne changes the v
2b067 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69 74 20  alues of iLimit 
2b068 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e 6c 79  and iOffset only
2b069 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74 20 6f   if.** a limit o
2b06a 72 20 6f 66 66 73 65 74 20 69 73 20 64 65 66 69  r offset is defi
2b06b 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20 61 6e  ned by pLimit an
2b06c 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c 69 6d  d pOffset.  iLim
2b06d 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66 73 65  it and.** iOffse
2b06e 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  t should have be
2b06f 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61 70 70  en preset to app
2b070 72 6f 70 72 69 61 74 65 20 64 65 66 61 75 6c 74  ropriate default
2b071 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73 75 61   values.** (usua
2b072 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c 77 61  lly but not alwa
2b073 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74 6f 20  ys -1) prior to 
2b074 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
2b075 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66  tine..** Only if
2b076 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20 70 4f   pLimit!=0 or pO
2b077 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68 65 20  ffset!=0 do the 
2b078 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20  limit registers 
2b079 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64  get.** redefined
2b07a 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41 4c 4c  .  The UNION ALL
2b07b 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73 20 74   operator uses t
2b07c 68 69 73 20 70 72 6f 70 65 72 74 79 20 74 6f 20  his property to 
2b07d 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72 65 75  force.** the reu
2b07e 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c  se of the same l
2b07f 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20  imit and offset 
2b080 72 65 67 69 73 74 65 72 73 20 61 63 72 6f 73 73  registers across
2b081 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53 45 4c   multiple.** SEL
2b082 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
2b083 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2b084 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73  omputeLimitRegis
2b085 74 65 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  ters(Parse *pPar
2b086 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69  se, Select *p, i
2b087 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20 56 64  nt iBreak){.  Vd
2b088 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74  be *v = 0;.  int
2b089 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 69   iLimit = 0;.  i
2b08a 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20 69 6e  nt iOffset;.  in
2b08b 74 20 61 64 64 72 31 2c 20 6e 3b 0a 20 20 69 66  t addr1, n;.  if
2b08c 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65  ( p->iLimit ) re
2b08d 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a  turn;..  /* .  *
2b08e 2a 20 22 4c 49 4d 49 54 20 2d 31 22 20 61 6c 77  * "LIMIT -1" alw
2b08f 61 79 73 20 73 68 6f 77 73 20 61 6c 6c 20 72 6f  ays shows all ro
2b090 77 73 2e 20 20 54 68 65 72 65 20 69 73 20 73 6f  ws.  There is so
2b091 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74 72 61 76 65  me.  ** contrave
2b092 72 73 79 20 61 62 6f 75 74 20 77 68 61 74 20 74  rsy about what t
2b093 68 65 20 63 6f 72 72 65 63 74 20 62 65 68 61 76  he correct behav
2b094 69 6f 72 20 73 68 6f 75 6c 64 20 62 65 2e 0a 20  ior should be.. 
2b095 20 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   ** The current 
2b096 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
2b097 6e 74 65 72 70 72 65 74 73 20 22 4c 49 4d 49 54  nterprets "LIMIT
2b098 20 30 22 20 74 6f 20 6d 65 61 6e 0a 20 20 2a 2a   0" to mean.  **
2b099 20 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20   no rows..  */. 
2b09a 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2b09b 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  eClear(pParse);.
2b09c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
2b09d 66 73 65 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4c  fset==0 || p->pL
2b09e 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 69 66 28  imit!=0 );.  if(
2b09f 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20   p->pLimit ){.  
2b0a0 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c    p->iLimit = iL
2b0a1 69 6d 69 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  imit = ++pParse-
2b0a2 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76 20 3d 20 73  >nMem;.    v = s
2b0a3 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2b0a4 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 4e  arse);.    if( N
2b0a5 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74  EVER(v==0) ) ret
2b0a6 75 72 6e 3b 20 20 2f 2a 20 56 44 42 45 20 73 68  urn;  /* VDBE sh
2b0a7 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64  ould have alread
2b0a8 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
2b0a9 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69   */.    if( sqli
2b0aa 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
2b0ab 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 26 6e 29 20  (p->pLimit, &n) 
2b0ac 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2b0ad 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b0ae 5f 49 6e 74 65 67 65 72 2c 20 6e 2c 20 69 4c 69  _Integer, n, iLi
2b0af 6d 69 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65  mit);.      Vdbe
2b0b0 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d  Comment((v, "LIM
2b0b1 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20  IT counter"));. 
2b0b2 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b       if( n==0 ){
2b0b3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b0b4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b0b5 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 72 65 61 6b  _Goto, 0, iBreak
2b0b6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2b0b7 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2b0b8 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2b0b9 73 65 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69  se, p->pLimit, i
2b0ba 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 73 71  Limit);.      sq
2b0bb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2b0bc 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
2b0bd 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20   iLimit);.      
2b0be 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2b0bf 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72 22 29  "LIMIT counter")
2b0c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b0c1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b0c2 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c  _IfZero, iLimit,
2b0c3 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 7d 0a   iBreak);.    }.
2b0c4 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 73      if( p->pOffs
2b0c5 65 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69  et ){.      p->i
2b0c6 4f 66 66 73 65 74 20 3d 20 69 4f 66 66 73 65 74  Offset = iOffset
2b0c7 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2b0c8 6d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  m;.      pParse-
2b0c9 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c  >nMem++;   /* Al
2b0ca 6c 6f 63 61 74 65 20 61 6e 20 65 78 74 72 61 20  locate an extra 
2b0cb 72 65 67 69 73 74 65 72 20 66 6f 72 20 6c 69 6d  register for lim
2b0cc 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20  it+offset */.   
2b0cd 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2b0ce 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f  de(pParse, p->pO
2b0cf 66 66 73 65 74 2c 20 69 4f 66 66 73 65 74 29 3b  ffset, iOffset);
2b0d0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b0d1 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
2b0d2 75 73 74 42 65 49 6e 74 2c 20 69 4f 66 66 73 65  ustBeInt, iOffse
2b0d3 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  t);.      VdbeCo
2b0d4 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46 53 45  mment((v, "OFFSE
2b0d5 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20  T counter"));.  
2b0d6 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
2b0d7 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2b0d8 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73   OP_IfPos, iOffs
2b0d9 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  et);.      sqlit
2b0da 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b0db 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69  OP_Integer, 0, i
2b0dc 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 73  Offset);.      s
2b0dd 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2b0de 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20  re(v, addr1);.  
2b0df 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b0e0 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c  ddOp3(v, OP_Add,
2b0e1 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74   iLimit, iOffset
2b0e2 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20  , iOffset+1);.  
2b0e3 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2b0e4 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45  (v, "LIMIT+OFFSE
2b0e5 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64 72  T"));.      addr
2b0e6 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2b0e7 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f  ddOp1(v, OP_IfPo
2b0e8 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  s, iLimit);.    
2b0e9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b0ea 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b0eb 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74 2b 31  r, -1, iOffset+1
2b0ec 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b0ed 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2b0ee 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20  addr1);.    }.  
2b0ef 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  }.}..#ifndef SQL
2b0f0 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2b0f1 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52  D_SELECT./*.** R
2b0f2 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70  eturn the approp
2b0f3 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20  riate collating 
2b0f4 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65  sequence for the
2b0f5 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20   iCol-th column 
2b0f6 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  of.** the result
2b0f7 20 73 65 74 20 66 6f 72 20 74 68 65 20 63 6f 6d   set for the com
2b0f8 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73 74 61  pound-select sta
2b0f9 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52 65 74  tement "p".  Ret
2b0fa 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74  urn NULL if.** t
2b0fb 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f  he column has no
2b0fc 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
2b0fd 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a  ng sequence..**.
2b0fe 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67  ** The collating
2b0ff 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
2b100 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  e compound selec
2b101 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  t is taken from 
2b102 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74  the.** left-most
2b103 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73 65 6c   term of the sel
2b104 65 63 74 20 74 68 61 74 20 68 61 73 20 61 20 63  ect that has a c
2b105 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2b106 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c  e..*/.static Col
2b107 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63  lSeq *multiSelec
2b108 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  tCollSeq(Parse *
2b109 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a  pParse, Select *
2b10a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20  p, int iCol){.  
2b10b 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20  CollSeq *pRet;. 
2b10c 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29   if( p->pPrior )
2b10d 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d 75 6c  {.    pRet = mul
2b10e 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
2b10f 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f  pParse, p->pPrio
2b110 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73  r, iCol);.  }els
2b111 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b  e{.    pRet = 0;
2b112 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
2b113 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  Col>=0 );.  if( 
2b114 70 52 65 74 3d 3d 30 20 26 26 20 69 43 6f 6c 3c  pRet==0 && iCol<
2b115 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2b116 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 73   ){.    pRet = s
2b117 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
2b118 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c  q(pParse, p->pEL
2b119 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
2b11a 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  pr);.  }.  retur
2b11b 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66  n pRet;.}.#endif
2b11c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2b11d 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
2b11e 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  */../* Forward r
2b11f 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74  eference */.stat
2b120 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65  ic int multiSele
2b121 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72  ctOrderBy(.  Par
2b122 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2b123 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2b124 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2b125 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2b126 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f   /* The right-mo
2b127 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f  st of SELECTs to
2b128 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53   be coded */.  S
2b129 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
2b12a 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
2b12b 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65  do with query re
2b12c 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23 69  sults */.);...#i
2b12d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2b12e 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2b12f 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  T./*.** This rou
2b130 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
2b131 6f 20 70 72 6f 63 65 73 73 20 61 20 63 6f 6d 70  o process a comp
2b132 6f 75 6e 64 20 71 75 65 72 79 20 66 6f 72 6d 20  ound query form 
2b133 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d  from.** two or m
2b134 6f 72 65 20 73 65 70 61 72 61 74 65 20 71 75 65  ore separate que
2b135 72 69 65 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e  ries using UNION
2b136 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43  , UNION ALL, EXC
2b137 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45 52  EPT, or.** INTER
2b138 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70  SECT.**.** "p" p
2b139 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 69 67  oints to the rig
2b13a 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68 65 20 74  ht-most of the t
2b13b 77 6f 20 71 75 65 72 69 65 73 2e 20 20 74 68 65  wo queries.  the
2b13c 20 71 75 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a   query on the.**
2b13d 20 6c 65 66 74 20 69 73 20 70 2d 3e 70 50 72 69   left is p->pPri
2b13e 6f 72 2e 20 20 54 68 65 20 6c 65 66 74 20 71 75  or.  The left qu
2b13f 65 72 79 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62  ery could also b
2b140 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  e a compound que
2b141 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63  ry.** in which c
2b142 61 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ase this routine
2b143 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
2b144 72 65 63 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a  recursively. .**
2b145 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
2b146 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65  of the total que
2b147 72 79 20 61 72 65 20 74 6f 20 62 65 20 77 72 69  ry are to be wri
2b148 74 74 65 6e 20 69 6e 74 6f 20 61 20 64 65 73 74  tten into a dest
2b149 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79  ination.** of ty
2b14a 70 65 20 65 44 65 73 74 20 77 69 74 68 20 70 61  pe eDest with pa
2b14b 72 61 6d 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a  rameter iParm..*
2b14c 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20  *.** Example 1: 
2b14d 20 43 6f 6e 73 69 64 65 72 20 61 20 74 68 72 65   Consider a thre
2b14e 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53  e-way compound S
2b14f 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  QL statement..**
2b150 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61  .**     SELECT a
2b151 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53   FROM t1 UNION S
2b152 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 20  ELECT b FROM t2 
2b153 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 63 20 46  UNION SELECT c F
2b154 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69  ROM t3.**.** Thi
2b155 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70  s statement is p
2b156 61 72 73 65 64 20 75 70 20 61 73 20 66 6f 6c 6c  arsed up as foll
2b157 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
2b158 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a  ELECT c FROM t3.
2b159 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20  **      |.**    
2b15a 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43    `----->  SELEC
2b15b 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20  T b FROM t2.**  
2b15c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a                |.
2b15d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2b15e 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45    `------>  SELE
2b15f 43 54 20 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a  CT a FROM t1.**.
2b160 2a 2a 20 54 68 65 20 61 72 72 6f 77 73 20 69 6e  ** The arrows in
2b161 20 74 68 65 20 64 69 61 67 72 61 6d 20 61 62 6f   the diagram abo
2b162 76 65 20 72 65 70 72 65 73 65 6e 74 20 74 68 65  ve represent the
2b163 20 53 65 6c 65 63 74 2e 70 50 72 69 6f 72 20 70   Select.pPrior p
2b164 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66  ointer..** So if
2b165 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2b166 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 20 65   called with p e
2b167 71 75 61 6c 20 74 6f 20 74 68 65 20 74 33 20 71  qual to the t3 q
2b168 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50  uery, then.** pP
2b169 72 69 6f 72 20 77 69 6c 6c 20 62 65 20 74 68 65  rior will be the
2b16a 20 74 32 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f   t2 query.  p->o
2b16b 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49  p will be TK_UNI
2b16c 4f 4e 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ON in this case.
2b16d 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68  .**.** Notice th
2b16e 61 74 20 62 65 63 61 75 73 65 20 6f 66 20 74 68  at because of th
2b16f 65 20 77 61 79 20 53 51 4c 69 74 65 20 70 61 72  e way SQLite par
2b170 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  ses compound SEL
2b171 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64  ECTs, the.** ind
2b172 69 76 69 64 75 61 6c 20 73 65 6c 65 63 74 73 20  ividual selects 
2b173 61 6c 77 61 79 73 20 67 72 6f 75 70 20 66 72 6f  always group fro
2b174 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e  m left to right.
2b175 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
2b176 75 6c 74 69 53 65 6c 65 63 74 28 0a 20 20 50 61  ultiSelect(.  Pa
2b177 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2b178 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2b179 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2b17a 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2b17b 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d    /* The right-m
2b17c 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74  ost of SELECTs t
2b17d 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
2b17e 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2b17f 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f  t     /* What to
2b180 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72   do with query r
2b181 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69  esults */.){.  i
2b182 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2b183 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 73 73 20  K;   /* Success 
2b184 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 75 62 72  code from a subr
2b185 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53 65 6c 65  outine */.  Sele
2b186 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20  ct *pPrior;     
2b187 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c    /* Another SEL
2b188 45 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ECT immediately 
2b189 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20  to our left */. 
2b18a 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
2b18b 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
2b18c 74 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73 20  te code to this 
2b18d 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74  VDBE */.  Select
2b18e 44 65 73 74 20 64 65 73 74 3b 20 20 20 20 20 20  Dest dest;      
2b18f 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76 65 20 64  /* Alternative d
2b190 61 74 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  ata destination 
2b191 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 44 65  */.  Select *pDe
2b192 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a 20 43 68  lete = 0;  /* Ch
2b193 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65 20 73 65  ain of simple se
2b194 6c 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20  lects to delete 
2b195 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2b196 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ;          /* Da
2b197 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2b198 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  n */..  /* Make 
2b199 73 75 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f  sure there is no
2b19a 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d   ORDER BY or LIM
2b19b 49 54 20 63 6c 61 75 73 65 20 6f 6e 20 70 72 69  IT clause on pri
2b19c 6f 72 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c  or SELECTs.  Onl
2b19d 79 0a 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20  y.  ** the last 
2b19e 28 72 69 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c  (right-most) SEL
2b19f 45 43 54 20 69 6e 20 74 68 65 20 73 65 72 69 65  ECT in the serie
2b1a0 73 20 6d 61 79 20 68 61 76 65 20 61 6e 20 4f 52  s may have an OR
2b1a1 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e  DER BY or LIMIT.
2b1a2 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2b1a3 70 20 26 26 20 70 2d 3e 70 50 72 69 6f 72 20 29  p && p->pPrior )
2b1a4 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e 67 20 66 75  ;  /* Calling fu
2b1a5 6e 63 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65  nction guarantee
2b1a6 73 20 74 68 69 73 20 6d 75 63 68 20 2a 2f 0a 20  s this much */. 
2b1a7 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2b1a8 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e  ;.  pPrior = p->
2b1a9 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74  pPrior;.  assert
2b1aa 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68 74  ( pPrior->pRight
2b1ab 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20 29 3b 0a  most!=pPrior );.
2b1ac 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72    assert( pPrior
2b1ad 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 2d  ->pRightmost==p-
2b1ae 3e 70 52 69 67 68 74 6d 6f 73 74 20 29 3b 0a 20  >pRightmost );. 
2b1af 20 64 65 73 74 20 3d 20 2a 70 44 65 73 74 3b 0a   dest = *pDest;.
2b1b0 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4f    if( pPrior->pO
2b1b1 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71  rderBy ){.    sq
2b1b2 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2b1b3 61 72 73 65 2c 22 4f 52 44 45 52 20 42 59 20 63  arse,"ORDER BY c
2b1b4 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d  lause should com
2b1b5 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20 62  e after %s not b
2b1b6 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 65  efore",.      se
2b1b7 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70  lectOpName(p->op
2b1b8 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  ));.    rc = 1;.
2b1b9 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
2b1ba 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20  elect_end;.  }. 
2b1bb 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69   if( pPrior->pLi
2b1bc 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mit ){.    sqlit
2b1bd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2b1be 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  e,"LIMIT clause 
2b1bf 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65  should come afte
2b1c0 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22  r %s not before"
2b1c1 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70  ,.      selectOp
2b1c2 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20  Name(p->op));.  
2b1c3 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
2b1c4 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
2b1c5 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20  end;.  }..  v = 
2b1c6 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2b1c7 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74  Parse);.  assert
2b1c8 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 54 68  ( v!=0 );  /* Th
2b1c9 65 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 63  e VDBE already c
2b1ca 72 65 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e  reated by callin
2b1cb 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20  g function */.. 
2b1cc 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 64   /* Create the d
2b1cd 65 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70 6f  estination tempo
2b1ce 72 61 72 79 20 74 61 62 6c 65 20 69 66 20 6e 65  rary table if ne
2b1cf 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20 69  cessary.  */.  i
2b1d0 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53  f( dest.eDest==S
2b1d1 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20  RT_EphemTab ){. 
2b1d2 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45     assert( p->pE
2b1d3 4c 69 73 74 20 29 3b 0a 20 20 20 20 73 71 6c 69  List );.    sqli
2b1d4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b1d5 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2b1d6 6c 2c 20 64 65 73 74 2e 69 50 61 72 6d 2c 20 70  l, dest.iParm, p
2b1d7 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  ->pEList->nExpr)
2b1d8 3b 0a 20 20 20 20 64 65 73 74 2e 65 44 65 73 74  ;.    dest.eDest
2b1d9 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20   = SRT_Table;.  
2b1da 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
2b1db 65 20 61 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e  e all SELECTs in
2b1dc 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68   the statement h
2b1dd 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d  ave the same num
2b1de 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a  ber of elements.
2b1df 20 20 2a 2a 20 69 6e 20 74 68 65 69 72 20 72 65    ** in their re
2b1e0 73 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a  sult sets..  */.
2b1e1 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
2b1e2 69 73 74 20 26 26 20 70 50 72 69 6f 72 2d 3e 70  ist && pPrior->p
2b1e3 45 4c 69 73 74 20 29 3b 0a 20 20 69 66 28 20 70  EList );.  if( p
2b1e4 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21  ->pEList->nExpr!
2b1e5 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d  =pPrior->pEList-
2b1e6 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71  >nExpr ){.    sq
2b1e7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2b1e8 61 72 73 65 2c 20 22 53 45 4c 45 43 54 73 20 74  arse, "SELECTs t
2b1e9 6f 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72  o the left and r
2b1ea 69 67 68 74 20 6f 66 20 25 73 22 0a 20 20 20 20  ight of %s".    
2b1eb 20 20 22 20 64 6f 20 6e 6f 74 20 68 61 76 65 20    " do not have 
2b1ec 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20  the same number 
2b1ed 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
2b1ee 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65  s", selectOpName
2b1ef 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63  (p->op));.    rc
2b1f0 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d   = 1;.    goto m
2b1f1 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
2b1f2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 6f  .  }..  /* Compo
2b1f3 75 6e 64 20 53 45 4c 45 43 54 73 20 74 68 61 74  und SELECTs that
2b1f4 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42   have an ORDER B
2b1f5 59 20 63 6c 61 75 73 65 20 61 72 65 20 68 61 6e  Y clause are han
2b1f6 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  dled separately.
2b1f7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  .  */.  if( p->p
2b1f8 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72  OrderBy ){.    r
2b1f9 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63  eturn multiSelec
2b1fa 74 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c  tOrderBy(pParse,
2b1fb 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a   p, pDest);.  }.
2b1fc 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
2b1fd 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 66 74  ode for the left
2b1fe 20 61 6e 64 20 72 69 67 68 74 20 53 45 4c 45 43   and right SELEC
2b1ff 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  T statements..  
2b200 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e  */.  switch( p->
2b201 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2b202 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69  K_ALL: {.      i
2b203 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a 20 20 20  nt addr = 0;.   
2b204 20 20 20 61 73 73 65 72 74 28 20 21 70 50 72 69     assert( !pPri
2b205 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20  or->pLimit );.  
2b206 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d      pPrior->pLim
2b207 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a  it = p->pLimit;.
2b208 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f        pPrior->pO
2b209 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
2b20a 65 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  et;.      rc = s
2b20b 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2b20c 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64 65  rse, pPrior, &de
2b20d 73 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c  st);.      p->pL
2b20e 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  imit = 0;.      
2b20f 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a  p->pOffset = 0;.
2b210 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
2b211 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
2b212 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
2b213 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e       }.      p->
2b214 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
2b215 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 70 50    p->iLimit = pP
2b216 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20  rior->iLimit;.  
2b217 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d      p->iOffset =
2b218 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66 73 65 74   pPrior->iOffset
2b219 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
2b21a 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20  Limit ){.       
2b21b 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
2b21c 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b21d 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69  IfZero, p->iLimi
2b21e 74 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65  t);.        Vdbe
2b21f 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d  Comment((v, "Jum
2b220 70 20 61 68 65 61 64 20 69 66 20 4c 49 4d 49 54  p ahead if LIMIT
2b221 20 72 65 61 63 68 65 64 22 29 29 3b 0a 20 20 20   reached"));.   
2b222 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
2b223 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2b224 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74 29 3b  arse, p, &dest);
2b225 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b226 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2b227 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 20  ;.      pDelete 
2b228 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
2b229 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70     p->pPrior = p
2b22a 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 69 66 28  Prior;.      if(
2b22b 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20 20   addr ){.       
2b22c 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2b22d 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
2b22e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
2b22f 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2b230 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20  se TK_EXCEPT:.  
2b231 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a    case TK_UNION:
2b232 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e 69   {.      int uni
2b233 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75 72  onTab;    /* Cur
2b234 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
2b235 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
2b236 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  e holding result
2b237 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70 20   */.      u8 op 
2b238 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e  = 0;       /* On
2b239 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f 70  e of the SRT_ op
2b23a 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c  erations to appl
2b23b 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20  y to self */.   
2b23c 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20     int priorOp; 
2b23d 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f 20      /* The SRT_ 
2b23e 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70 70  operation to app
2b23f 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c 65  ly to prior sele
2b240 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  cts */.      Exp
2b241 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66  r *pLimit, *pOff
2b242 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  set; /* Saved va
2b243 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69  lues of p->nLimi
2b244 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74  t and p->nOffset
2b245 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
2b246 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74  dr;.      Select
2b247 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a  Dest uniondest;.
2b248 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b249 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50   p->op==TK_EXCEP
2b24a 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
2b24b 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ase( p->op==TK_U
2b24c 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 70 72  NION );.      pr
2b24d 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f  iorOp = SRT_Unio
2b24e 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73  n;.      if( des
2b24f 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70  t.eDest==priorOp
2b250 20 26 26 20 41 4c 57 41 59 53 28 21 70 2d 3e 70   && ALWAYS(!p->p
2b251 4c 69 6d 69 74 20 26 26 21 70 2d 3e 70 4f 66 66  Limit &&!p->pOff
2b252 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  set) ){.        
2b253 2f 2a 20 57 65 20 63 61 6e 20 72 65 75 73 65 20  /* We can reuse 
2b254 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
2b255 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  e generated by a
2b256 20 53 45 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20   SELECT to our. 
2b257 20 20 20 20 20 20 20 2a 2a 20 72 69 67 68 74 2e         ** right.
2b258 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2b259 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2b25a 52 69 67 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20  Rightmost!=p ); 
2b25b 20 2f 2a 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70   /* Can only hap
2b25c 70 65 6e 20 66 6f 72 20 6c 65 66 74 77 61 72 64  pen for leftward
2b25d 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 20 20   elements.      
2b25e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b25f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2b260 2a 20 6f 66 20 61 20 33 2d 77 61 79 20 6f 72 20  * of a 3-way or 
2b261 6d 6f 72 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  more compound */
2b262 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2b263 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b   p->pLimit==0 );
2b264 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c        /* Not all
2b265 6f 77 65 64 20 6f 6e 20 6c 65 66 74 77 61 72 64  owed on leftward
2b266 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
2b267 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2b268 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20  pOffset==0 );   
2b269 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64    /* Not allowed
2b26a 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c 65   on leftward ele
2b26b 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  ments */.       
2b26c 20 75 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74   unionTab = dest
2b26d 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65  .iParm;.      }e
2b26e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
2b26f 57 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20  We will need to 
2b270 63 72 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74  create our own t
2b271 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74  emporary table t
2b272 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20 20 20 20  o hold the.     
2b273 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61     ** intermedia
2b274 74 65 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20  te results..    
2b275 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75      */.        u
2b276 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72 73 65  nionTab = pParse
2b277 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
2b278 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72    assert( p->pOr
2b279 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  derBy==0 );.    
2b27a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2b27b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b27c 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2b27d 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a  , unionTab, 0);.
2b27e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b27f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
2b280 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20  0] == -1 );.    
2b281 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
2b282 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[0] = addr;. 
2b283 20 20 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74         p->pRight
2b284 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  most->selFlags |
2b285 3d 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72  = SF_UsesEphemer
2b286 61 6c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  al;.        asse
2b287 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
2b288 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2b289 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45  /* Code the SELE
2b28a 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  CT statements to
2b28b 20 6f 75 72 20 6c 65 66 74 0a 20 20 20 20 20 20   our left.      
2b28c 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2b28d 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72   !pPrior->pOrder
2b28e 42 79 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  By );.      sqli
2b28f 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2b290 74 28 26 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72  t(&uniondest, pr
2b291 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29  iorOp, unionTab)
2b292 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
2b293 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2b294 65 2c 20 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f  e, pPrior, &unio
2b295 6e 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66  ndest);.      if
2b296 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
2b297 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
2b298 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  t_end;.      }..
2b299 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68        /* Code th
2b29a 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54  e current SELECT
2b29b 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20   statement.     
2b29c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d   */.      if( p-
2b29d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29  >op==TK_EXCEPT )
2b29e 7b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d 20 53  {.        op = S
2b29f 52 54 5f 45 78 63 65 70 74 3b 0a 20 20 20 20 20  RT_Except;.     
2b2a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2b2a1 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54  assert( p->op==T
2b2a2 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20  K_UNION );.     
2b2a3 20 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f     op = SRT_Unio
2b2a4 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
2b2a5 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a   p->pPrior = 0;.
2b2a6 20 20 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 70        pLimit = p
2b2a7 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20  ->pLimit;.      
2b2a8 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->pLimit = 0;. 
2b2a9 20 20 20 20 20 70 4f 66 66 73 65 74 20 3d 20 70       pOffset = p
2b2aa 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->pOffset;.     
2b2ab 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b   p->pOffset = 0;
2b2ac 0a 20 20 20 20 20 20 75 6e 69 6f 6e 64 65 73 74  .      uniondest
2b2ad 2e 65 44 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20  .eDest = op;.   
2b2ae 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
2b2af 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
2b2b0 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20   &uniondest);.  
2b2b1 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
2b2b2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
2b2b3 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c       /* Query fl
2b2b4 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69  attening in sqli
2b2b5 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69 67 68  te3Select() migh
2b2b6 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64  t refill p->pOrd
2b2b7 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42  erBy..      ** B
2b2b8 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65 74 65  e sure to delete
2b2b9 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 74 68   p->pOrderBy, th
2b2ba 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69  erefore, to avoi
2b2bb 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e  d a memory leak.
2b2bc 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2b2bd 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2b2be 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
2b2bf 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 20  ;.      pDelete 
2b2c0 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
2b2c1 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70     p->pPrior = p
2b2c2 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e  Prior;.      p->
2b2c3 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
2b2c4 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2b2c5 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69  elete(db, p->pLi
2b2c6 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  mit);.      p->p
2b2c7 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a  Limit = pLimit;.
2b2c8 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74        p->pOffset
2b2c9 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 20 20   = pOffset;.    
2b2ca 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b    p->iLimit = 0;
2b2cb 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65  .      p->iOffse
2b2cc 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a  t = 0;..      /*
2b2cd 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61 74   Convert the dat
2b2ce 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  a in the tempora
2b2cf 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  ry table into wh
2b2d0 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20 20  atever form.    
2b2d1 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74 20    ** it is that 
2b2d2 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65 65  we currently nee
2b2d3 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
2b2d4 20 20 61 73 73 65 72 74 28 20 75 6e 69 6f 6e 54    assert( unionT
2b2d5 61 62 3d 3d 64 65 73 74 2e 69 50 61 72 6d 20 7c  ab==dest.iParm |
2b2d6 7c 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72  | dest.eDest!=pr
2b2d7 69 6f 72 4f 70 20 29 3b 0a 20 20 20 20 20 20 69  iorOp );.      i
2b2d8 66 28 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70  f( dest.eDest!=p
2b2d9 72 69 6f 72 4f 70 20 29 7b 0a 20 20 20 20 20 20  riorOp ){.      
2b2da 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72    int iCont, iBr
2b2db 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20  eak, iStart;.   
2b2dc 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2b2dd 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
2b2de 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
2b2df 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2b2e0 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63 74            Select
2b2e1 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20   *pFirst = p;.  
2b2e2 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
2b2e3 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20  First->pPrior ) 
2b2e4 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d  pFirst = pFirst-
2b2e5 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20  >pPrior;.       
2b2e6 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d     generateColum
2b2e7 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30  nNames(pParse, 0
2b2e8 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74  , pFirst->pEList
2b2e9 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2b2ea 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71       iBreak = sq
2b2eb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2b2ec 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69  el(v);.        i
2b2ed 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
2b2ee 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2b2ef 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c          computeL
2b2f0 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
2b2f1 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29  arse, p, iBreak)
2b2f2 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2b2f3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b2f4 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54  P_Rewind, unionT
2b2f5 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  ab, iBreak);.   
2b2f6 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73 71       iStart = sq
2b2f7 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2b2f8 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 20  Addr(v);.       
2b2f9 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
2b2fa 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
2b2fb 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61 62 2c  EList, unionTab,
2b2fc 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2b2fd 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
2b2fe 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 2d 31             0, -1
2b2ff 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20  , &dest, iCont, 
2b300 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
2b301 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b302 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
2b303 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
2b304 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b305 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e 54   OP_Next, unionT
2b306 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20  ab, iStart);.   
2b307 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b308 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2b309 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
2b30a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b30b 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
2b30c 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20  unionTab, 0);.  
2b30d 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2b30e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
2b30f 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 70 2d  ault: assert( p-
2b310 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43  >op==TK_INTERSEC
2b311 54 20 29 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74  T ); {.      int
2b312 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20 20 20   tab1, tab2;.   
2b313 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
2b314 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
2b315 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74      Expr *pLimit
2b316 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20 20  , *pOffset;.    
2b317 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
2b318 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e 74    SelectDest int
2b319 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20 20 20  ersectdest;.    
2b31a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20 20 20    int r1;..     
2b31b 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69 73   /* INTERSECT is
2b31c 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
2b31d 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63 65  the others since
2b31e 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20 20   it requires.   
2b31f 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72     ** two tempor
2b320 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65 6e  ary tables.  Hen
2b321 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f 77  ce it has its ow
2b322 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a 20  n case.  Begin. 
2b323 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63       ** by alloc
2b324 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65 73  ating the tables
2b325 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20   we will need.. 
2b326 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 61       */.      ta
2b327 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  b1 = pParse->nTa
2b328 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32 20  b++;.      tab2 
2b329 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2b32a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b32b 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  p->pOrderBy==0 )
2b32c 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  ;..      addr = 
2b32d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b32e 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65  2(v, OP_OpenEphe
2b32f 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b  meral, tab1, 0);
2b330 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2b331 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30  ->addrOpenEphm[0
2b332 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20  ] == -1 );.     
2b333 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
2b334 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 20  [0] = addr;.    
2b335 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d    p->pRightmost-
2b336 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
2b337 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20  UsesEphemeral;. 
2b338 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2b339 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20 20  pEList );..     
2b33a 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c   /* Code the SEL
2b33b 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66 74  ECTs to our left
2b33c 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20   into temporary 
2b33d 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20 20  table "tab1"..  
2b33e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
2b33f 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
2b340 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65 73  it(&intersectdes
2b341 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61  t, SRT_Union, ta
2b342 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  b1);.      rc = 
2b343 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2b344 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 69  arse, pPrior, &i
2b345 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a 20  ntersectdest);. 
2b346 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
2b347 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
2b348 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
2b349 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2b34a 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e 74  Code the current
2b34b 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65 6d   SELECT into tem
2b34c 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74 61  porary table "ta
2b34d 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  b2".      */.   
2b34e 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2b34f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b350 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
2b351 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20 20   tab2, 0);.     
2b352 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72   assert( p->addr
2b353 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d  OpenEphm[1] == -
2b354 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64  1 );.      p->ad
2b355 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20  drOpenEphm[1] = 
2b356 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70  addr;.      p->p
2b357 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20  Prior = 0;.     
2b358 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69   pLimit = p->pLi
2b359 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c  mit;.      p->pL
2b35a 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  imit = 0;.      
2b35b 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66  pOffset = p->pOf
2b35c 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  fset;.      p->p
2b35d 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  Offset = 0;.    
2b35e 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74 2e    intersectdest.
2b35f 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20 20  iParm = tab2;.  
2b360 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2b361 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2b362 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73 74  , &intersectdest
2b363 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2b364 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc!=SQLITE_OK
2b365 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74   );.      pDelet
2b366 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
2b367 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
2b368 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73   pPrior;.      s
2b369 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2b36a 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
2b36b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  .      p->pLimit
2b36c 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20   = pLimit;.     
2b36d 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f   p->pOffset = pO
2b36e 66 66 73 65 74 3b 0a 0a 20 20 20 20 20 20 2f 2a  ffset;..      /*
2b36f 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2b370 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74 65 72  o take the inter
2b371 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 74  section of the t
2b372 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20  wo temporary.   
2b373 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a 20 20     ** tables..  
2b374 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
2b375 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29  ert( p->pEList )
2b376 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74  ;.      if( dest
2b377 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  .eDest==SRT_Outp
2b378 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53 65  ut ){.        Se
2b379 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70  lect *pFirst = p
2b37a 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
2b37b 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20   pFirst->pPrior 
2b37c 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73  ) pFirst = pFirs
2b37d 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  t->pPrior;.     
2b37e 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d     generateColum
2b37f 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30  nNames(pParse, 0
2b380 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74  , pFirst->pEList
2b381 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2b382 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65   iBreak = sqlite
2b383 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2b384 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74 20 3d  );.      iCont =
2b385 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2b386 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
2b387 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69  computeLimitRegi
2b388 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c  sters(pParse, p,
2b389 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
2b38a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b38b 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
2b38c 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b 0a 20  tab1, iBreak);. 
2b38d 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
2b38e 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2b38f 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74 61 72  se);.      iStar
2b390 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
2b391 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b  ddOp2(v, OP_RowK
2b392 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b 0a 20  ey, tab1, r1);. 
2b393 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b394 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f  AddOp4Int(v, OP_
2b395 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20  NotFound, tab2, 
2b396 69 43 6f 6e 74 2c 20 72 31 2c 20 30 29 3b 0a 20  iCont, r1, 0);. 
2b397 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2b398 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2b399 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 65  e, r1);.      se
2b39a 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
2b39b 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
2b39c 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70 45 4c  st, tab1, p->pEL
2b39d 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20  ist->nExpr,.    
2b39e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b39f 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20    0, -1, &dest, 
2b3a0 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a  iCont, iBreak);.
2b3a1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b3a2 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b3a3 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 73   iCont);.      s
2b3a4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b3a5 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74 61 62  (v, OP_Next, tab
2b3a6 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20  1, iStart);.    
2b3a7 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2b3a8 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
2b3a9 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  eak);.      sqli
2b3aa 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b3ab 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32 2c   OP_Close, tab2,
2b3ac 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
2b3ad 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b3ae 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c 20  OP_Close, tab1, 
2b3af 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
2b3b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2b3b1 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69   Compute collati
2b3b2 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75 73 65  ng sequences use
2b3b3 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d 70 6f  d by .  ** tempo
2b3b4 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65 65 64  rary tables need
2b3b5 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
2b3b6 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
2b3b7 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61 63 68  ect..  ** Attach
2b3b8 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
2b3b9 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 74 65  ucture to all te
2b3ba 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a  mporary tables..
2b3bb 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 73    **.  ** This s
2b3bc 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62 79  ection is run by
2b3bd 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
2b3be 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2b3bf 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c 45   only..  ** SELE
2b3c0 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  CT statements to
2b3c1 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61 79 73   the left always
2b3c2 20 73 6b 69 70 20 74 68 69 73 20 70 61 72 74 2e   skip this part.
2b3c3 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74    The right-most
2b3c4 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d 69 67  .  ** SELECT mig
2b3c5 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 68 69  ht also skip thi
2b3c6 73 20 70 61 72 74 20 69 66 20 69 74 20 68 61 73  s part if it has
2b3c7 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61   no ORDER BY cla
2b3c8 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20  use and.  ** no 
2b3c9 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72 65 20  temp tables are 
2b3ca 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a 20  required..  */. 
2b3cb 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
2b3cc 20 26 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65   & SF_UsesEpheme
2b3cd 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ral ){.    int i
2b3ce 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b3cf 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2b3d0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
2b3d1 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2b3d2 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
2b3d3 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
2b3d4 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 73 75  nce for the resu
2b3d5 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 53 65  lt set */.    Se
2b3d6 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 20 20  lect *pLoop;    
2b3d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2b3d8 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75  or looping throu
2b3d9 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  gh SELECT statem
2b3da 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c  ents */.    Coll
2b3db 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20  Seq **apColl;   
2b3dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
2b3dd 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68   looping through
2b3de 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c   pKeyInfo->aColl
2b3df 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 43  [] */.    int nC
2b3e0 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
2b3e1 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2b3e2 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
2b3e3 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20  result set */.. 
2b3e4 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
2b3e5 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20  ightmost==p );. 
2b3e6 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c     nCol = p->pEL
2b3e7 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
2b3e8 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
2b3e9 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2b3ea 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
2b3eb 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
2b3ec 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c  (*pKeyInfo)+nCol
2b3ed 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
2b3ee 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20 69 66  *) + 1));.    if
2b3ef 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20  ( !pKeyInfo ){. 
2b3f0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2b3f1 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f  _NOMEM;.      go
2b3f2 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
2b3f3 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  end;.    }..    
2b3f4 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20  pKeyInfo->enc = 
2b3f5 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 4b 65  ENC(db);.    pKe
2b3f6 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20  yInfo->nField = 
2b3f7 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20 20 20 20  (u16)nCol;..    
2b3f8 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d  for(i=0, apColl=
2b3f9 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b  pKeyInfo->aColl;
2b3fa 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70   i<nCol; i++, ap
2b3fb 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a  Coll++){.      *
2b3fc 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65  apColl = multiSe
2b3fd 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72  lectCollSeq(pPar
2b3fe 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20 20  se, p, i);.     
2b3ff 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20   if( 0==*apColl 
2b400 29 7b 0a 20 20 20 20 20 20 20 20 2a 61 70 43 6f  ){.        *apCo
2b401 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
2b402 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
2b403 7d 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70  }..    for(pLoop
2b404 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70  =p; pLoop; pLoop
2b405 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b  =pLoop->pPrior){
2b406 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2b407 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<2; i++){.     
2b408 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 70 4c     int addr = pL
2b409 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
2b40a 6d 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  m[i];.        if
2b40b 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20  ( addr<0 ){.    
2b40c 20 20 20 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20        /* If [0] 
2b40d 69 73 20 75 6e 75 73 65 64 20 74 68 65 6e 20 5b  is unused then [
2b40e 31 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75 73 65  1] is also unuse
2b40f 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a 20 20  d.  So we can.  
2b410 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79          ** alway
2b411 73 20 73 61 66 65 6c 79 20 61 62 6f 72 74 20 61  s safely abort a
2b412 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 66 69  s soon as the fi
2b413 72 73 74 20 75 6e 75 73 65 64 20 73 6c 6f 74 20  rst unused slot 
2b414 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20  is found */.    
2b415 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
2b416 6f 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  oop->addrOpenEph
2b417 6d 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20  m[1]<0 );.      
2b418 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b419 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
2b41a 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
2b41b 28 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b  (v, addr, nCol);
2b41c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b41d 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2b41e 61 64 64 72 2c 20 28 63 68 61 72 2a 29 70 4b 65  addr, (char*)pKe
2b41f 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2b420 4f 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f  O);.        pLoo
2b421 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
2b422 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d  i] = -1;.      }
2b423 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2b424 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4b 65  e3DbFree(db, pKe
2b425 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c  yInfo);.  }..mul
2b426 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20  ti_select_end:. 
2b427 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 64   pDest->iMem = d
2b428 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 70 44 65 73  est.iMem;.  pDes
2b429 74 2d 3e 6e 4d 65 6d 20 3d 20 64 65 73 74 2e 6e  t->nMem = dest.n
2b42a 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  Mem;.  sqlite3Se
2b42b 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2b42c 44 65 6c 65 74 65 29 3b 0a 20 20 72 65 74 75 72  Delete);.  retur
2b42d 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
2b42e 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
2b42f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f  MPOUND_SELECT */
2b430 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20  ../*.** Code an 
2b431 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
2b432 65 20 66 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e  e for a coroutin
2b433 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2b434 20 6f 66 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20   of a.** SELECT 
2b435 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  statment..**.** 
2b436 54 68 65 20 64 61 74 61 20 74 6f 20 62 65 20 6f  The data to be o
2b437 75 74 70 75 74 20 69 73 20 63 6f 6e 74 61 69 6e  utput is contain
2b438 65 64 20 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e  ed in pIn->iMem.
2b439 20 20 54 68 65 72 65 20 61 72 65 0a 2a 2a 20 70    There are.** p
2b43a 49 6e 2d 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73  In->nMem columns
2b43b 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 20 20   to be output.  
2b43c 70 44 65 73 74 20 69 73 20 77 68 65 72 65 20 74  pDest is where t
2b43d 68 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64  he output should
2b43e 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a  .** be sent..**.
2b43f 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69 73 20  ** regReturn is 
2b440 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  the number of th
2b441 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69  e register holdi
2b442 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  ng the subroutin
2b443 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64 64 72  e.** return addr
2b444 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65  ess..**.** If re
2b445 67 50 72 65 76 3e 30 20 74 68 65 6e 20 69 74 20  gPrev>0 then it 
2b446 69 73 20 61 20 74 68 65 20 66 69 72 73 74 20 72  is a the first r
2b447 65 67 69 73 74 65 72 20 69 6e 20 61 20 76 65 63  egister in a vec
2b448 74 6f 72 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f  tor that.** reco
2b449 72 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  rds the previous
2b44a 20 6f 75 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65   output.  mem[re
2b44b 67 50 72 65 76 5d 20 69 73 20 61 20 66 6c 61 67  gPrev] is a flag
2b44c 20 74 68 61 74 20 69 73 20 66 61 6c 73 65 0a 2a   that is false.*
2b44d 2a 20 69 66 20 74 68 65 72 65 20 68 61 73 20 62  * if there has b
2b44e 65 65 6e 20 6e 6f 20 70 72 65 76 69 6f 75 73 20  een no previous 
2b44f 6f 75 74 70 75 74 2e 20 20 49 66 20 72 65 67 50  output.  If regP
2b450 72 65 76 3e 30 20 74 68 65 6e 20 63 6f 64 65 20  rev>0 then code 
2b451 69 73 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20  is.** generated 
2b452 74 6f 20 73 75 70 70 72 65 73 73 20 64 75 70 6c  to suppress dupl
2b453 69 63 61 74 65 73 2e 20 20 70 4b 65 79 49 6e 66  icates.  pKeyInf
2b454 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20 63 6f  o is used for co
2b455 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e  mparing.** keys.
2b456 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49  .**.** If the LI
2b457 4d 49 54 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e  MIT found in p->
2b458 69 4c 69 6d 69 74 20 69 73 20 72 65 61 63 68 65  iLimit is reache
2b459 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74  d, jump immediat
2b45a 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b  ely to.** iBreak
2b45b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2b45c 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75  generateOutputSu
2b45d 62 72 6f 75 74 69 6e 65 28 0a 20 20 50 61 72 73  broutine(.  Pars
2b45e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2b45f 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2b460 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2b461 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2b462 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
2b463 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
2b464 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 49 6e   SelectDest *pIn
2b465 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f  ,        /* Coro
2b466 75 74 69 6e 65 20 73 75 70 70 6c 79 69 6e 67 20  utine supplying 
2b467 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74  data */.  Select
2b468 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20  Dest *pDest,    
2b469 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 73 65    /* Where to se
2b46a 6e 64 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  nd the data */. 
2b46b 20 69 6e 74 20 72 65 67 52 65 74 75 72 6e 2c 20   int regReturn, 
2b46c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2b46d 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72  return address r
2b46e 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74  egister */.  int
2b46f 20 72 65 67 50 72 65 76 2c 20 20 20 20 20 20 20   regPrev,       
2b470 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
2b471 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72   result register
2b472 2e 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73  .  No uniqueness
2b473 20 69 66 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e   if 0 */.  KeyIn
2b474 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
2b475 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72     /* For compar
2b476 69 6e 67 20 77 69 74 68 20 70 72 65 76 69 6f 75  ing with previou
2b477 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  s entry */.  int
2b478 20 70 34 74 79 70 65 2c 20 20 20 20 20 20 20 20   p4type,        
2b479 20 20 20 20 20 2f 2a 20 54 68 65 20 70 34 20 74       /* The p4 t
2b47a 79 70 65 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f  ype for pKeyInfo
2b47b 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b   */.  int iBreak
2b47c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b47d 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 77 65   Jump here if we
2b47e 20 68 69 74 20 74 68 65 20 4c 49 4d 49 54 20 2a   hit the LIMIT *
2b47f 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
2b480 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2b481 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b    int iContinue;
2b482 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20  .  int addr;..  
2b483 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2b484 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2b485 3b 0a 20 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20  ;.  iContinue = 
2b486 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2b487 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53  abel(v);..  /* S
2b488 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74  uppress duplicat
2b489 65 73 20 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58  es for UNION, EX
2b48a 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53  CEPT, and INTERS
2b48b 45 43 54 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ECT .  */.  if( 
2b48c 72 65 67 50 72 65 76 20 29 7b 0a 20 20 20 20 69  regPrev ){.    i
2b48d 6e 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a  nt j1, j2;.    j
2b48e 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2b48f 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f  ddOp1(v, OP_IfNo
2b490 74 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20  t, regPrev);.   
2b491 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j2 = sqlite3Vdb
2b492 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
2b493 6d 70 61 72 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  mpare, pIn->iMem
2b494 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
2b495 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20  ->nMem,.        
2b496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b497 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2b498 79 49 6e 66 6f 2c 20 70 34 74 79 70 65 29 3b 0a  yInfo, p4type);.
2b499 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b49a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70  ddOp3(v, OP_Jump
2b49b 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74 69 6e 75  , j2+2, iContinu
2b49c 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20 20 73 71  e, j2+2);.    sq
2b49d 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2b49e 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
2b49f 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70  lite3ExprCodeCop
2b4a0 79 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  y(pParse, pIn->i
2b4a1 4d 65 6d 2c 20 72 65 67 50 72 65 76 2b 31 2c 20  Mem, regPrev+1, 
2b4a2 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  pIn->nMem);.    
2b4a3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b4a4 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b4a5 20 31 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20   1, regPrev);.  
2b4a6 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
2b4a7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2b4a8 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
2b4a9 2f 2a 20 53 75 70 70 72 65 73 73 20 74 68 65 20  /* Suppress the 
2b4aa 74 68 65 20 66 69 72 73 74 20 4f 46 46 53 45 54  the first OFFSET
2b4ab 20 65 6e 74 72 69 65 73 20 69 66 20 74 68 65 72   entries if ther
2b4ac 65 20 69 73 20 61 6e 20 4f 46 46 53 45 54 20 63  e is an OFFSET c
2b4ad 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20 63 6f 64  lause.  */.  cod
2b4ae 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43  eOffset(v, p, iC
2b4af 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 73 77 69  ontinue);..  swi
2b4b0 74 63 68 28 20 70 44 65 73 74 2d 3e 65 44 65 73  tch( pDest->eDes
2b4b1 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 6f 72  t ){.    /* Stor
2b4b2 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73 20  e the result as 
2b4b3 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e 69  data using a uni
2b4b4 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a  que key..    */.
2b4b5 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62      case SRT_Tab
2b4b6 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  le:.    case SRT
2b4b7 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20  _EphemTab: {.   
2b4b8 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
2b4b9 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2b4ba 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 6e 74  arse);.      int
2b4bb 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r2 = sqlite3Get
2b4bc 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2b4bd 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2b4be 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
2b4bf 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  RT_Table );.    
2b4c0 20 20 74 65 73 74 63 61 73 65 28 20 70 44 65 73    testcase( pDes
2b4c1 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70  t->eDest==SRT_Ep
2b4c2 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20  hemTab );.      
2b4c3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b4c4 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
2b4c5 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70  rd, pIn->iMem, p
2b4c6 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29 3b 0a 20  In->nMem, r1);. 
2b4c7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b4c8 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2b4c9 52 6f 77 69 64 2c 20 70 44 65 73 74 2d 3e 69 50  Rowid, pDest->iP
2b4ca 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  arm, r2);.      
2b4cb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b4cc 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
2b4cd 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31  pDest->iParm, r1
2b4ce 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r2);.      sql
2b4cf 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2b4d0 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e  (v, OPFLAG_APPEN
2b4d1 44 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  D);.      sqlite
2b4d2 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2b4d3 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20  pParse, r2);.   
2b4d4 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2b4d5 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2b4d6 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
2b4d7 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  k;.    }..#ifnde
2b4d8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
2b4d9 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66  BQUERY.    /* If
2b4da 20 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   we are creating
2b4db 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65   a set for an "e
2b4dc 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e  xpr IN (SELECT .
2b4dd 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a  ..)" construct,.
2b4de 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72      ** then ther
2b4df 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69  e should be a si
2b4e0 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65  ngle item on the
2b4e1 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74   stack.  Write t
2b4e2 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20  his.    ** item 
2b4e3 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62  into the set tab
2b4e4 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61  le with bogus da
2b4e5 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ta..    */.    c
2b4e6 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20  ase SRT_Set: {. 
2b4e7 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20       int r1;.   
2b4e8 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 2d 3e     assert( pIn->
2b4e9 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nMem==1 );.     
2b4ea 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 0a   p->affinity = .
2b4eb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2b4ec 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
2b4ed 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  p->pEList->a[0].
2b4ee 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e 61 66  pExpr, pDest->af
2b4ef 66 69 6e 69 74 79 29 3b 0a 20 20 20 20 20 20 72  finity);.      r
2b4f0 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
2b4f1 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2b4f2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b4f3 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
2b4f4 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d  eRecord, pIn->iM
2b4f5 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61  em, 1, r1, &p->a
2b4f6 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20  ffinity, 1);.   
2b4f7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2b4f8 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2b4f9 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  e(pParse, pIn->i
2b4fa 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  Mem, 1);.      s
2b4fb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b4fc 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
2b4fd 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20  , pDest->iParm, 
2b4fe 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r1);.      sqlit
2b4ff 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2b500 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
2b501 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2b502 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e 65 76 65  ..#if 0  /* Neve
2b503 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 6e 20 4f  r occurs on an O
2b504 52 44 45 52 20 42 59 20 71 75 65 72 79 20 2a 2f  RDER BY query */
2b505 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20 72  .    /* If any r
2b506 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65 20  ow exist in the 
2b507 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63 6f  result set, reco
2b508 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e 64  rd that fact and
2b509 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20   abort..    */. 
2b50a 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69 73     case SRT_Exis
2b50b 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  ts: {.      sqli
2b50c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b50d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2b50e 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20  pDest->iParm);. 
2b50f 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
2b510 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65  T clause will te
2b511 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2b512 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20   for us */.     
2b513 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2b514 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20  ndif..    /* If 
2b515 74 68 69 73 20 69 73 20 61 20 73 63 61 6c 61 72  this is a scalar
2b516 20 73 65 6c 65 63 74 20 74 68 61 74 20 69 73 20   select that is 
2b517 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
2b518 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20  ssion, then.    
2b519 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
2b51a 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70 70 72  ults in the appr
2b51b 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63  opriate memory c
2b51c 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75  ell and break ou
2b51d 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  t.    ** of the 
2b51e 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a  scan loop..    *
2b51f 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d  /.    case SRT_M
2b520 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  em: {.      asse
2b521 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31  rt( pIn->nMem==1
2b522 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
2b523 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
2b524 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  arse, pIn->iMem,
2b525 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 31   pDest->iParm, 1
2b526 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  );.      /* The 
2b527 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c  LIMIT clause wil
2b528 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20 74 68  l jump out of th
2b529 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
2b52a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2b52b 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69    }.#endif /* #i
2b52c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2b52d 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20  T_SUBQUERY */.. 
2b52e 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
2b52f 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
2b530 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65  a sequence of re
2b531 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a 20 73  gisters.    ** s
2b532 74 61 72 74 69 6e 67 20 61 74 20 70 44 65 73 74  tarting at pDest
2b533 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e 20 74 68  ->iMem.  Then th
2b534 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 79 69 65  e co-routine yie
2b535 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lds..    */.    
2b536 63 61 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69  case SRT_Corouti
2b537 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  ne: {.      if( 
2b538 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29  pDest->iMem==0 )
2b539 7b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 2d  {.        pDest-
2b53a 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74 65 33 47  >iMem = sqlite3G
2b53b 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
2b53c 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a  se, pIn->nMem);.
2b53d 20 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 6e          pDest->n
2b53e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d 65 6d 3b  Mem = pIn->nMem;
2b53f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2b540 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f  qlite3ExprCodeMo
2b541 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ve(pParse, pIn->
2b542 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69 4d 65  iMem, pDest->iMe
2b543 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 29 3b  m, pDest->nMem);
2b544 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b545 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
2b546 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61  ield, pDest->iPa
2b547 72 6d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  rm);.      break
2b548 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2b549 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61  If none of the a
2b54a 62 6f 76 65 2c 20 74 68 65 6e 20 74 68 65 20 72  bove, then the r
2b54b 65 73 75 6c 74 20 64 65 73 74 69 6e 61 74 69 6f  esult destinatio
2b54c 6e 20 6d 75 73 74 20 62 65 0a 20 20 20 20 2a 2a  n must be.    **
2b54d 20 53 52 54 5f 4f 75 74 70 75 74 2e 20 20 54 68   SRT_Output.  Th
2b54e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65  is routine is ne
2b54f 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20  ver called with 
2b550 61 6e 79 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a  any other.    **
2b551 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6f 74 68   destination oth
2b552 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 73  er than the ones
2b553 20 68 61 6e 64 6c 65 64 20 61 62 6f 76 65 20 6f   handled above o
2b554 72 20 53 52 54 5f 4f 75 74 70 75 74 2e 0a 20 20  r SRT_Output..  
2b555 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20    **.    ** For 
2b556 53 52 54 5f 4f 75 74 70 75 74 2c 20 72 65 73 75  SRT_Output, resu
2b557 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  lts are stored i
2b558 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  n a sequence of 
2b559 72 65 67 69 73 74 65 72 73 2e 20 20 0a 20 20 20  registers.  .   
2b55a 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 4f 50 5f   ** Then the OP_
2b55b 52 65 73 75 6c 74 52 6f 77 20 6f 70 63 6f 64 65  ResultRow opcode
2b55c 20 69 73 20 75 73 65 64 20 74 6f 20 63 61 75 73   is used to caus
2b55d 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
2b55e 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 74 75 72   to.    ** retur
2b55f 6e 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f  n the next row o
2b560 66 20 72 65 73 75 6c 74 2e 0a 20 20 20 20 2a 2f  f result..    */
2b561 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2b562 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44        assert( pD
2b563 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b564 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20 20  Output );.      
2b565 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b566 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
2b567 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49  w, pIn->iMem, pI
2b568 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20  n->nMem);.      
2b569 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2b56a 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2b56b 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
2b56c 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  , pIn->nMem);.  
2b56d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2b56e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  .  }..  /* Jump 
2b56f 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
2b570 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49  e loop if the LI
2b571 4d 49 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a  MIT is reached..
2b572 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c    */.  if( p->iL
2b573 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  imit ){.    sqli
2b574 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2b575 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69   OP_IfZero, p->i
2b576 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 2c 20 2d  Limit, iBreak, -
2b577 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  1);.  }..  /* Ge
2b578 6e 65 72 61 74 65 20 74 68 65 20 73 75 62 72 6f  nerate the subro
2b579 75 74 69 6e 65 20 72 65 74 75 72 6e 0a 20 20 2a  utine return.  *
2b57a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
2b57b 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
2b57c 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c  Continue);.  sql
2b57d 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2b57e 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67  , OP_Return, reg
2b57f 52 65 74 75 72 6e 29 3b 0a 0a 20 20 72 65 74 75  Return);..  retu
2b580 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn addr;.}../*.*
2b581 2a 20 41 6c 74 65 72 6e 61 74 69 76 65 20 63 6f  * Alternative co
2b582 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 6f  mpound select co
2b583 64 65 20 67 65 6e 65 72 61 74 6f 72 20 66 6f 72  de generator for
2b584 20 63 61 73 65 73 20 77 68 65 6e 20 74 68 65 72   cases when ther
2b585 65 0a 2a 2a 20 69 73 20 61 6e 20 4f 52 44 45 52  e.** is an ORDER
2b586 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a   BY clause..**.*
2b587 2a 20 57 65 20 61 73 73 75 6d 65 20 61 20 71 75  * We assume a qu
2b588 65 72 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ery of the follo
2b589 77 69 6e 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a  wing form:.**.**
2b58a 20 20 20 20 20 20 3c 73 65 6c 65 63 74 41 3e 20        <selectA> 
2b58b 20 3c 6f 70 65 72 61 74 6f 72 3e 20 20 3c 73 65   <operator>  <se
2b58c 6c 65 63 74 42 3e 20 20 4f 52 44 45 52 20 42 59  lectB>  ORDER BY
2b58d 20 3c 6f 72 64 65 72 62 79 6c 69 73 74 3e 0a 2a   <orderbylist>.*
2b58e 2a 0a 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20  *.** <operator> 
2b58f 69 73 20 6f 6e 65 20 6f 66 20 55 4e 49 4f 4e 20  is one of UNION 
2b590 41 4c 4c 2c 20 55 4e 49 4f 4e 2c 20 45 58 43 45  ALL, UNION, EXCE
2b591 50 54 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54  PT, or INTERSECT
2b592 2e 20 20 54 68 65 20 69 64 65 61 0a 2a 2a 20 69  .  The idea.** i
2b593 73 20 74 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c  s to code both <
2b594 73 65 6c 65 63 74 41 3e 20 61 6e 64 20 3c 73 65  selectA> and <se
2b595 6c 65 63 74 42 3e 20 77 69 74 68 20 74 68 65 20  lectB> with the 
2b596 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2b597 61 73 0a 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65  as.** co-routine
2b598 73 2e 20 20 54 68 65 6e 20 72 75 6e 20 74 68 65  s.  Then run the
2b599 20 63 6f 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20   co-routines in 
2b59a 70 61 72 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72  parallel and mer
2b59b 67 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a  ge the results.*
2b59c 2a 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  * into the outpu
2b59d 74 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 20  t.  In addition 
2b59e 74 6f 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75  to the two corou
2b59f 74 69 6e 65 73 20 28 63 61 6c 6c 65 64 20 73 65  tines (called se
2b5a0 6c 65 63 74 41 20 61 6e 64 0a 2a 2a 20 73 65 6c  lectA and.** sel
2b5a1 65 63 74 42 29 20 74 68 65 72 65 20 61 72 65 20  ectB) there are 
2b5a2 37 20 73 75 62 72 6f 75 74 69 6e 65 73 3a 0a 2a  7 subroutines:.*
2b5a3 2a 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20 20 20  *.**    outA:   
2b5a4 20 4d 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74   Move the output
2b5a5 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 41 20   of the selectA 
2b5a6 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74  coroutine into t
2b5a7 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20  he output.**    
2b5a8 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
2b5a9 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 0a  compound query..
2b5aa 2a 2a 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 20  **.**    outB:  
2b5ab 20 20 4d 6f 76 65 20 74 68 65 20 6f 75 74 70 75    Move the outpu
2b5ac 74 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 42  t of the selectB
2b5ad 20 63 6f 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20   coroutine into 
2b5ae 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20  the output.**   
2b5af 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65            of the
2b5b0 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e   compound query.
2b5b1 20 20 28 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65    (Only generate
2b5b2 64 20 66 6f 72 20 55 4e 49 4f 4e 20 61 6e 64 0a  d for UNION and.
2b5b3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 55  **             U
2b5b4 4e 49 4f 4e 20 41 4c 4c 2e 20 20 45 58 43 45 50  NION ALL.  EXCEP
2b5b5 54 20 61 6e 64 20 49 4e 53 45 52 54 53 45 43 54  T and INSERTSECT
2b5b6 20 6e 65 76 65 72 20 6f 75 74 70 75 74 20 61 20   never output a 
2b5b7 72 6f 77 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  row that.**     
2b5b8 20 20 20 20 20 20 20 20 61 70 70 65 61 72 73 20          appears 
2b5b9 6f 6e 6c 79 20 69 6e 20 42 2e 29 0a 2a 2a 0a 2a  only in B.).**.*
2b5ba 2a 20 20 20 20 41 6c 74 42 3a 20 20 20 20 43 61  *    AltB:    Ca
2b5bb 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  lled when there 
2b5bc 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74  is data from bot
2b5bd 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64  h coroutines and
2b5be 20 41 3c 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41   A<B..**.**    A
2b5bf 65 71 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  eqB:    Called w
2b5c0 68 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 74  hen there is dat
2b5c1 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f  a from both coro
2b5c2 75 74 69 6e 65 73 20 61 6e 64 20 41 3d 3d 42 2e  utines and A==B.
2b5c3 0a 2a 2a 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20  .**.**    AgtB: 
2b5c4 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74     Called when t
2b5c5 68 65 72 65 20 69 73 20 64 61 74 61 20 66 72 6f  here is data fro
2b5c6 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65  m both coroutine
2b5c7 73 20 61 6e 64 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a  s and A>B..**.**
2b5c8 20 20 20 20 45 6f 66 41 3a 20 20 20 20 43 61 6c      EofA:    Cal
2b5c9 6c 65 64 20 77 68 65 6e 20 64 61 74 61 20 69 73  led when data is
2b5ca 20 65 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20   exhausted from 
2b5cb 73 65 6c 65 63 74 41 2e 0a 2a 2a 0a 2a 2a 20 20  selectA..**.**  
2b5cc 20 20 45 6f 66 42 3a 20 20 20 20 43 61 6c 6c 65    EofB:    Calle
2b5cd 64 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 65  d when data is e
2b5ce 78 68 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65  xhausted from se
2b5cf 6c 65 63 74 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lectB..**.** The
2b5d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2b5d1 6f 66 20 74 68 65 20 6c 61 74 74 65 72 20 66 69  of the latter fi
2b5d2 76 65 20 73 75 62 72 6f 75 74 69 6e 65 73 20 64  ve subroutines d
2b5d3 65 70 65 6e 64 20 6f 6e 20 77 68 69 63 68 20 0a  epend on which .
2b5d4 2a 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73  ** <operator> is
2b5d5 20 75 73 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20   used:.**.**.** 
2b5d6 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f              UNIO
2b5d7 4e 20 41 4c 4c 20 20 20 20 20 20 20 20 20 55 4e  N ALL         UN
2b5d8 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 45  ION            E
2b5d9 58 43 45 50 54 20 20 20 20 20 20 20 20 20 20 49  XCEPT          I
2b5da 4e 54 45 52 53 45 43 54 0a 2a 2a 20 20 20 20 20  NTERSECT.**     
2b5db 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
2b5dc 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
2b5dd 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  -----  ---------
2b5de 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  -----  ---------
2b5df 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 41 6c  --------.**   Al
2b5e0 74 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74  tB:   outA, next
2b5e1 41 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78  A      outA, nex
2b5e2 74 41 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e  tA       outA, n
2b5e3 65 78 74 41 20 20 20 20 20 20 20 20 20 6e 65 78  extA         nex
2b5e4 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 65 71 42 3a  tA.**.**   AeqB:
2b5e5 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
2b5e6 20 20 20 20 20 20 20 6e 65 78 74 41 20 20 20 20         nextA    
2b5e7 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 20 20           nextA  
2b5e8 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78         outA, nex
2b5e9 74 41 0a 2a 2a 0a 2a 2a 20 20 20 41 67 74 42 3a  tA.**.**   AgtB:
2b5ea 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20     outB, nextB  
2b5eb 20 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 42 20      outB, nextB 
2b5ec 20 20 20 20 20 20 20 20 20 6e 65 78 74 42 20 20           nextB  
2b5ed 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 42 0a            nextB.
2b5ee 2a 2a 0a 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20  **.**   EofA:   
2b5ef 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20  outB, nextB     
2b5f0 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
2b5f1 20 20 20 20 20 20 68 61 6c 74 20 20 20 20 20 20        halt      
2b5f2 20 20 20 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a         halt.**.*
2b5f3 2a 20 20 20 45 6f 66 42 3a 20 20 20 6f 75 74 41  *   EofB:   outA
2b5f4 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f 75 74  , nextA      out
2b5f5 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 6f  A, nextA       o
2b5f6 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
2b5f7 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e     halt.**.** In
2b5f8 20 74 68 65 20 41 6c 74 42 2c 20 41 65 71 42 2c   the AltB, AeqB,
2b5f9 20 61 6e 64 20 41 67 74 42 20 73 75 62 72 6f 75   and AgtB subrou
2b5fa 74 69 6e 65 73 2c 20 61 6e 20 45 4f 46 20 6f 6e  tines, an EOF on
2b5fb 20 41 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78   A following nex
2b5fc 74 41 0a 2a 2a 20 63 61 75 73 65 73 20 61 6e 20  tA.** causes an 
2b5fd 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 20 74  immediate jump t
2b5fe 6f 20 45 6f 66 41 20 61 6e 64 20 61 6e 20 45 4f  o EofA and an EO
2b5ff 46 20 6f 6e 20 42 20 66 6f 6c 6c 6f 77 69 6e 67  F on B following
2b600 20 6e 65 78 74 42 20 63 61 75 73 65 73 0a 2a 2a   nextB causes.**
2b601 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75   an immediate ju
2b602 6d 70 20 74 6f 20 45 6f 66 42 2e 20 20 57 69 74  mp to EofB.  Wit
2b603 68 69 6e 20 45 6f 66 41 20 61 6e 64 20 45 6f 66  hin EofA and Eof
2b604 42 2c 20 61 6e 64 20 45 4f 46 20 6f 6e 20 65 6e  B, and EOF on en
2b605 74 72 79 20 6f 72 0a 2a 2a 20 66 6f 6c 6c 6f 77  try or.** follow
2b606 69 6e 67 20 6e 65 78 74 58 20 63 61 75 73 65 73  ing nextX causes
2b607 20 61 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65   a jump to the e
2b608 6e 64 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  nd of the select
2b609 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a   processing..**.
2b60a 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 72 65 6d  ** Duplicate rem
2b60b 6f 76 61 6c 20 69 6e 20 74 68 65 20 55 4e 49 4f  oval in the UNIO
2b60c 4e 2c 20 45 58 43 45 50 54 2c 20 61 6e 64 20 49  N, EXCEPT, and I
2b60d 4e 54 45 52 53 45 43 54 20 63 61 73 65 73 20 69  NTERSECT cases i
2b60e 73 20 68 61 6e 64 6c 65 64 0a 2a 2a 20 77 69 74  s handled.** wit
2b60f 68 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 73  hin the output s
2b610 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20  ubroutine.  The 
2b611 72 65 67 50 72 65 76 20 72 65 67 69 73 74 65 72  regPrev register
2b612 20 73 65 74 20 68 6f 6c 64 73 20 74 68 65 20 70   set holds the p
2b613 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 6f 75 74  reviously.** out
2b614 70 75 74 20 76 61 6c 75 65 2e 20 20 41 20 63 6f  put value.  A co
2b615 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65  mparison is made
2b616 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 76 61   against this va
2b617 6c 75 65 20 61 6e 64 20 74 68 65 20 6f 75 74 70  lue and the outp
2b618 75 74 0a 2a 2a 20 69 73 20 73 6b 69 70 70 65 64  ut.** is skipped
2b619 20 69 66 20 74 68 65 20 6e 65 78 74 20 72 65 73   if the next res
2b61a 75 6c 74 73 20 77 6f 75 6c 64 20 62 65 20 74 68  ults would be th
2b61b 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 72  e same as the pr
2b61c 65 76 69 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  evious..**.** Th
2b61d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2b61e 20 70 6c 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c   plan is to impl
2b61f 65 6d 65 6e 74 20 74 68 65 20 74 77 6f 20 63 6f  ement the two co
2b620 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 73 65 76  routines and sev
2b621 65 6e 0a 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65  en.** subroutine
2b622 73 20 66 69 72 73 74 2c 20 74 68 65 6e 20 70 75  s first, then pu
2b623 74 20 74 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f  t the control lo
2b624 67 69 63 20 61 74 20 74 68 65 20 62 6f 74 74 6f  gic at the botto
2b625 6d 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a  m.  Like this:.*
2b626 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 67 6f  *.**          go
2b627 74 6f 20 49 6e 69 74 0a 2a 2a 20 20 20 20 20 63  to Init.**     c
2b628 6f 41 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  oA: coroutine fo
2b629 72 20 6c 65 66 74 20 71 75 65 72 79 20 28 41 29  r left query (A)
2b62a 0a 2a 2a 20 20 20 20 20 63 6f 42 3a 20 63 6f 72  .**     coB: cor
2b62b 6f 75 74 69 6e 65 20 66 6f 72 20 72 69 67 68 74  outine for right
2b62c 20 71 75 65 72 79 20 28 42 29 0a 2a 2a 20 20 20   query (B).**   
2b62d 20 6f 75 74 41 3a 20 6f 75 74 70 75 74 20 6f 6e   outA: output on
2b62e 65 20 72 6f 77 20 6f 66 20 41 0a 2a 2a 20 20 20  e row of A.**   
2b62f 20 6f 75 74 42 3a 20 6f 75 74 70 75 74 20 6f 6e   outB: output on
2b630 65 20 72 6f 77 20 6f 66 20 42 20 28 55 4e 49 4f  e row of B (UNIO
2b631 4e 20 61 6e 64 20 55 4e 49 4f 4e 20 41 4c 4c 20  N and UNION ALL 
2b632 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 45 6f 66 41  only).**    EofA
2b633 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 45 6f 66 42  : ....**    EofB
2b634 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 6c 74 42  : ....**    AltB
2b635 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 65 71 42  : ....**    AeqB
2b636 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 41 67 74 42  : ....**    AgtB
2b637 3a 20 2e 2e 2e 0a 2a 2a 20 20 20 20 49 6e 69 74  : ....**    Init
2b638 3a 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 72  : initialize cor
2b639 6f 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 73  outine registers
2b63a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 79 69 65  .**          yie
2b63b 6c 64 20 63 6f 41 0a 2a 2a 20 20 20 20 20 20 20  ld coA.**       
2b63c 20 20 20 69 66 20 65 6f 66 28 41 29 20 67 6f 74     if eof(A) got
2b63d 6f 20 45 6f 66 41 0a 2a 2a 20 20 20 20 20 20 20  o EofA.**       
2b63e 20 20 20 79 69 65 6c 64 20 63 6f 42 0a 2a 2a 20     yield coB.** 
2b63f 20 20 20 20 20 20 20 20 20 69 66 20 65 6f 66 28           if eof(
2b640 42 29 20 67 6f 74 6f 20 45 6f 66 42 0a 2a 2a 20  B) goto EofB.** 
2b641 20 20 20 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65     Cmpr: Compare
2b642 20 41 2c 20 42 0a 2a 2a 20 20 20 20 20 20 20 20   A, B.**        
2b643 20 20 4a 75 6d 70 20 41 6c 74 42 2c 20 41 65 71    Jump AltB, Aeq
2b644 42 2c 20 41 67 74 42 0a 2a 2a 20 20 20 20 20 45  B, AgtB.**     E
2b645 6e 64 3a 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65  nd: ....**.** We
2b646 20 63 61 6c 6c 20 41 6c 74 42 2c 20 41 65 71 42   call AltB, AeqB
2b647 2c 20 41 67 74 42 2c 20 45 6f 66 41 2c 20 61 6e  , AgtB, EofA, an
2b648 64 20 45 6f 66 42 20 22 73 75 62 72 6f 75 74 69  d EofB "subrouti
2b649 6e 65 73 22 20 62 75 74 20 74 68 65 79 20 61 72  nes" but they ar
2b64a 65 20 6e 6f 74 0a 2a 2a 20 61 63 74 75 61 6c 6c  e not.** actuall
2b64b 79 20 63 61 6c 6c 65 64 20 75 73 69 6e 67 20 47  y called using G
2b64c 6f 73 75 62 20 61 6e 64 20 74 68 65 79 20 64 6f  osub and they do
2b64d 20 6e 6f 74 20 52 65 74 75 72 6e 2e 20 20 45 6f   not Return.  Eo
2b64e 66 41 20 61 6e 64 20 45 6f 66 42 20 6c 6f 6f 70  fA and EofB loop
2b64f 0a 2a 2a 20 75 6e 74 69 6c 20 61 6c 6c 20 64 61  .** until all da
2b650 74 61 20 69 73 20 65 78 68 61 75 73 74 65 64 20  ta is exhausted 
2b651 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65  then jump to the
2b652 20 22 65 6e 64 22 20 6c 61 62 65 2e 20 20 41 6c   "end" labe.  Al
2b653 74 42 2c 20 41 65 71 42 2c 0a 2a 2a 20 61 6e 64  tB, AeqB,.** and
2b654 20 41 67 74 42 20 6a 75 6d 70 20 74 6f 20 65 69   AgtB jump to ei
2b655 74 68 65 72 20 4c 32 20 6f 72 20 74 6f 20 6f 6e  ther L2 or to on
2b656 65 20 6f 66 20 45 6f 66 41 20 6f 72 20 45 6f 66  e of EofA or Eof
2b657 42 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  B..*/.#ifndef SQ
2b658 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
2b659 4e 44 5f 53 45 4c 45 43 54 0a 73 74 61 74 69 63  ND_SELECT.static
2b65a 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74   int multiSelect
2b65b 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65  OrderBy(.  Parse
2b65c 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2b65d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2b65e 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2b65f 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
2b660 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
2b661 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62   of SELECTs to b
2b662 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c  e coded */.  Sel
2b663 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20  ectDest *pDest  
2b664 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
2b665 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75   with query resu
2b666 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  lts */.){.  int 
2b667 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
2b668 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2b669 72 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  rs */.  Select *
2b66a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a  pPrior;       /*
2b66b 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43 54 20   Another SELECT 
2b66c 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6f  immediately to o
2b66d 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62  ur left */.  Vdb
2b66e 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2b66f 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
2b670 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44 42 45  ode to this VDBE
2b671 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
2b672 20 64 65 73 74 41 3b 20 20 20 20 20 2f 2a 20 44   destA;     /* D
2b673 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63  estination for c
2b674 6f 72 6f 75 74 69 6e 65 20 41 20 2a 2f 0a 20 20  oroutine A */.  
2b675 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 42  SelectDest destB
2b676 3b 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61  ;     /* Destina
2b677 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69  tion for corouti
2b678 6e 65 20 42 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ne B */.  int re
2b679 67 41 64 64 72 41 3b 20 20 20 20 20 20 20 20 20  gAddrA;         
2b67a 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69 73  /* Address regis
2b67b 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 41  ter for select-A
2b67c 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20   coroutine */.  
2b67d 69 6e 74 20 72 65 67 45 6f 66 41 3b 20 20 20 20  int regEofA;    
2b67e 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f        /* Flag to
2b67f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 73   indicate when s
2b680 65 6c 65 63 74 2d 41 20 69 73 20 63 6f 6d 70 6c  elect-A is compl
2b681 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ete */.  int reg
2b682 41 64 64 72 42 3b 20 20 20 20 20 20 20 20 20 2f  AddrB;         /
2b683 2a 20 41 64 64 72 65 73 73 20 72 65 67 69 73 74  * Address regist
2b684 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 42 20  er for select-B 
2b685 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  coroutine */.  i
2b686 6e 74 20 72 65 67 45 6f 66 42 3b 20 20 20 20 20  nt regEofB;     
2b687 20 20 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20       /* Flag to 
2b688 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 73 65  indicate when se
2b689 6c 65 63 74 2d 42 20 69 73 20 63 6f 6d 70 6c 65  lect-B is comple
2b68a 74 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  te */.  int addr
2b68b 53 65 6c 65 63 74 41 3b 20 20 20 20 20 20 2f 2a  SelectA;      /*
2b68c 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2b68d 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69  select-A corouti
2b68e 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2b68f 53 65 6c 65 63 74 42 3b 20 20 20 20 20 20 2f 2a  SelectB;      /*
2b690 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2b691 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69  select-B corouti
2b692 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  ne */.  int regO
2b693 75 74 41 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  utA;          /*
2b694 20 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65   Address registe
2b695 72 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74  r for the output
2b696 2d 41 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  -A subroutine */
2b697 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 42 3b 20  .  int regOutB; 
2b698 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2b699 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
2b69a 20 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75   the output-B su
2b69b 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2b69c 74 20 61 64 64 72 4f 75 74 41 3b 20 20 20 20 20  t addrOutA;     
2b69d 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2b69e 66 20 74 68 65 20 6f 75 74 70 75 74 2d 41 20 73  f the output-A s
2b69f 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
2b6a0 6e 74 20 61 64 64 72 4f 75 74 42 20 3d 20 30 3b  nt addrOutB = 0;
2b6a1 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2b6a2 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2d 42 20  of the output-B 
2b6a3 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
2b6a4 69 6e 74 20 61 64 64 72 45 6f 66 41 3b 20 20 20  int addrEofA;   
2b6a5 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2b6a6 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41   of the select-A
2b6a7 2d 65 78 68 61 75 73 74 65 64 20 73 75 62 72 6f  -exhausted subro
2b6a8 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2b6a9 64 64 72 45 6f 66 42 3b 20 20 20 20 20 20 20 20  ddrEofB;        
2b6aa 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b6ab 68 65 20 73 65 6c 65 63 74 2d 42 2d 65 78 68 61  he select-B-exha
2b6ac 75 73 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65  usted subroutine
2b6ad 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 6c   */.  int addrAl
2b6ae 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  tB;         /* A
2b6af 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3c  ddress of the A<
2b6b0 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
2b6b1 20 20 69 6e 74 20 61 64 64 72 41 65 71 42 3b 20    int addrAeqB; 
2b6b2 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2b6b3 73 73 20 6f 66 20 74 68 65 20 41 3d 3d 42 20 73  ss of the A==B s
2b6b4 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
2b6b5 6e 74 20 61 64 64 72 41 67 74 42 3b 20 20 20 20  nt addrAgtB;    
2b6b6 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2b6b7 6f 66 20 74 68 65 20 41 3e 42 20 73 75 62 72 6f  of the A>B subro
2b6b8 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
2b6b9 65 67 4c 69 6d 69 74 41 3b 20 20 20 20 20 20 20  egLimitA;       
2b6ba 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67 69 73 74   /* Limit regist
2b6bb 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20  er for select-A 
2b6bc 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69  */.  int regLimi
2b6bd 74 42 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  tB;        /* Li
2b6be 6d 69 74 20 72 65 67 69 73 74 65 72 20 66 6f 72  mit register for
2b6bf 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69   select-A */.  i
2b6c0 6e 74 20 72 65 67 50 72 65 76 3b 20 20 20 20 20  nt regPrev;     
2b6c1 20 20 20 20 20 2f 2a 20 41 20 72 61 6e 67 65 20       /* A range 
2b6c2 6f 66 20 72 65 67 69 73 74 65 72 73 20 74 6f 20  of registers to 
2b6c3 68 6f 6c 64 20 70 72 65 76 69 6f 75 73 20 6f 75  hold previous ou
2b6c4 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61  tput */.  int sa
2b6c5 76 65 64 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  vedLimit;       
2b6c6 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
2b6c7 66 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20  f p->iLimit */. 
2b6c8 20 69 6e 74 20 73 61 76 65 64 4f 66 66 73 65 74   int savedOffset
2b6c9 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20  ;      /* Saved 
2b6ca 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4f 66 66  value of p->iOff
2b6cb 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62  set */.  int lab
2b6cc 65 6c 43 6d 70 72 3b 20 20 20 20 20 20 20 20 2f  elCmpr;        /
2b6cd 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20  * Label for the 
2b6ce 73 74 61 72 74 20 6f 66 20 74 68 65 20 6d 65 72  start of the mer
2b6cf 67 65 20 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a  ge algorithm */.
2b6d0 20 20 69 6e 74 20 6c 61 62 65 6c 45 6e 64 3b 20    int labelEnd; 
2b6d1 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
2b6d2 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20   for the end of 
2b6d3 74 68 65 20 6f 76 65 72 61 6c 6c 20 53 45 4c 45  the overall SELE
2b6d4 43 54 20 73 74 6d 74 20 2a 2f 0a 20 20 69 6e 74  CT stmt */.  int
2b6d5 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   j1;            
2b6d6 20 20 20 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72     /* Jump instr
2b6d7 75 63 74 69 6f 6e 73 20 74 68 61 74 20 67 65 74  uctions that get
2b6d8 20 72 65 74 61 72 67 65 74 74 65 64 20 2a 2f 0a   retargetted */.
2b6d9 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20    int op;       
2b6da 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
2b6db 66 20 54 4b 5f 41 4c 4c 2c 20 54 4b 5f 55 4e 49  f TK_ALL, TK_UNI
2b6dc 4f 4e 2c 20 54 4b 5f 45 58 43 45 50 54 2c 20 54  ON, TK_EXCEPT, T
2b6dd 4b 5f 49 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20  K_INTERSECT */. 
2b6de 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 44 75   KeyInfo *pKeyDu
2b6df 70 20 3d 20 30 3b 20 2f 2a 20 43 6f 6d 70 61 72  p = 0; /* Compar
2b6e0 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ison information
2b6e1 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20 72   for duplicate r
2b6e2 65 6d 6f 76 61 6c 20 2a 2f 0a 20 20 4b 65 79 49  emoval */.  KeyI
2b6e3 6e 66 6f 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20  nfo *pKeyMerge; 
2b6e4 20 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20    /* Comparison 
2b6e5 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
2b6e6 6d 65 72 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0a  merging rows */.
2b6e7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2b6e8 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2b6e9 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2b6ea 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2b6eb 72 64 65 72 42 79 3b 20 20 20 2f 2a 20 54 68 65  rderBy;   /* The
2b6ec 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2b6ed 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72   */.  int nOrder
2b6ee 42 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  By;         /* N
2b6ef 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
2b6f0 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  n the ORDER BY c
2b6f1 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  lause */.  int *
2b6f2 61 50 65 72 6d 75 74 65 3b 20 20 20 20 20 20 20  aPermute;       
2b6f3 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d   /* Mapping from
2b6f4 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
2b6f5 74 6f 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  to result set co
2b6f6 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65  lumns */..  asse
2b6f7 72 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 21  rt( p->pOrderBy!
2b6f8 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2b6f9 70 4b 65 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a  pKeyDup==0 ); /*
2b6fa 20 22 4d 61 6e 61 67 65 64 22 20 63 6f 64 65 20   "Managed" code 
2b6fb 6e 65 65 64 73 20 74 68 69 73 2e 20 20 54 69 63  needs this.  Tic
2b6fc 6b 65 74 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20  ket #3382. */.  
2b6fd 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2b6fe 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  v = pParse->p
2b6ff 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Vdbe;.  assert( 
2b700 76 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a  v!=0 );       /*
2b701 20 41 6c 72 65 61 64 79 20 74 68 72 6f 77 6e 20   Already thrown 
2b702 74 68 65 20 65 72 72 6f 72 20 69 66 20 56 44 42  the error if VDB
2b703 45 20 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a  E alloc failed *
2b704 2f 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73  /.  labelEnd = s
2b705 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2b706 62 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43  bel(v);.  labelC
2b707 6d 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  mpr = sqlite3Vdb
2b708 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a  eMakeLabel(v);..
2b709 0a 20 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74  .  /* Patch up t
2b70a 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b70b 73 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70  se.  */.  op = p
2b70c 2d 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72  ->op;  .  pPrior
2b70d 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
2b70e 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e  assert( pPrior->
2b70f 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20  pOrderBy==0 );. 
2b710 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
2b711 4f 72 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72  OrderBy;.  asser
2b712 74 28 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20  t( pOrderBy );. 
2b713 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64   nOrderBy = pOrd
2b714 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20  erBy->nExpr;..  
2b715 2f 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73  /* For operators
2b716 20 6f 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f   other than UNIO
2b717 4e 20 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f  N ALL we have to
2b718 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
2b719 20 20 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42    ** the ORDER B
2b71a 59 20 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20  Y clause covers 
2b71b 65 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68  every term of th
2b71c 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41  e result set.  A
2b71d 64 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f  dd.  ** terms to
2b71e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2b71f 61 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72  ause as necessar
2b720 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  y..  */.  if( op
2b721 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20  !=TK_ALL ){.    
2b722 66 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c  for(i=1; db->mal
2b723 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20  locFailed==0 && 
2b724 69 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  i<=p->pEList->nE
2b725 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
2b726 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2b727 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2b728 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74      for(j=0, pIt
2b729 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
2b72a 6a 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c  j<nOrderBy; j++,
2b72b 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
2b72c 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
2b72d 2d 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20  ->iCol>0 );.    
2b72e 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
2b72f 43 6f 6c 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a  Col==i ) break;.
2b730 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2b731 28 20 6a 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b  ( j==nOrderBy ){
2b732 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
2b733 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
2b734 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52  r(db, TK_INTEGER
2b735 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
2b736 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
2b737 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2b738 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  .        pNew->f
2b739 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61  lags |= EP_IntVa
2b73a 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 70 4e 65  lue;.        pNe
2b73b 77 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 3b  w->u.iValue = i;
2b73c 0a 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42  .        pOrderB
2b73d 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
2b73e 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2b73f 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 4e 65 77  , pOrderBy, pNew
2b740 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 64 65  );.        pOrde
2b741 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 72 42 79 2b  rBy->a[nOrderBy+
2b742 2b 5d 2e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  +].iCol = (u16)i
2b743 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2b744 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74    }..  /* Comput
2b745 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
2b746 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64   permutation and
2b747 20 6b 65 79 69 6e 66 6f 20 74 68 61 74 20 69 73   keyinfo that is
2b748 20 75 73 65 64 20 77 69 74 68 0a 20 20 2a 2a 20   used with.  ** 
2b749 74 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20  the permutation 
2b74a 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
2b74b 65 20 69 66 20 74 68 65 20 6e 65 78 74 0a 20 20  e if the next.  
2b74c 2a 2a 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  ** row of result
2b74d 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c  s comes from sel
2b74e 65 63 74 41 20 6f 72 20 73 65 6c 65 63 74 42 2e  ectA or selectB.
2b74f 20 20 41 6c 73 6f 20 61 64 64 20 65 78 70 6c 69    Also add expli
2b750 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69  cit.  ** collati
2b751 6f 6e 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52  ons to the ORDER
2b752 20 42 59 20 63 6c 61 75 73 65 20 74 65 72 6d 73   BY clause terms
2b753 20 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68   so that when th
2b754 65 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 2a  e subqueries.  *
2b755 2a 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 61  * to the right a
2b756 6e 64 20 74 68 65 20 6c 65 66 74 20 61 72 65 20  nd the left are 
2b757 65 76 61 6c 75 61 74 65 64 2c 20 74 68 65 79 20  evaluated, they 
2b758 75 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a  use the correct.
2b759 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a    ** collation..
2b75a 20 20 2a 2f 0a 20 20 61 50 65 72 6d 75 74 65 20    */.  aPermute 
2b75b 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2b75c 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2b75d 69 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a  int)*nOrderBy);.
2b75e 20 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29    if( aPermute )
2b75f 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70  {.    struct Exp
2b760 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2b761 6d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  m;.    for(i=0, 
2b762 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
2b763 61 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69  a; i<nOrderBy; i
2b764 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2b765 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65      assert( pIte
2b766 6d 2d 3e 69 43 6f 6c 3e 30 20 20 26 26 20 70 49  m->iCol>0  && pI
2b767 74 65 6d 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e 70 45  tem->iCol<=p->pE
2b768 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20  List->nExpr );. 
2b769 20 20 20 20 20 61 50 65 72 6d 75 74 65 5b 69 5d       aPermute[i]
2b76a 20 3d 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 2d   = pItem->iCol -
2b76b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b   1;.    }.    pK
2b76c 65 79 4d 65 72 67 65 20 3d 0a 20 20 20 20 20 20  eyMerge =.      
2b76d 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
2b76e 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  aw(db, sizeof(*p
2b76f 4b 65 79 4d 65 72 67 65 29 2b 6e 4f 72 64 65 72  KeyMerge)+nOrder
2b770 42 79 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  By*(sizeof(CollS
2b771 65 71 2a 29 2b 31 29 29 3b 0a 20 20 20 20 69 66  eq*)+1));.    if
2b772 28 20 70 4b 65 79 4d 65 72 67 65 20 29 7b 0a 20  ( pKeyMerge ){. 
2b773 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
2b774 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
2b775 2a 29 26 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43  *)&pKeyMerge->aC
2b776 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20  oll[nOrderBy];. 
2b777 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
2b778 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f  nField = (u16)nO
2b779 72 64 65 72 42 79 3b 0a 20 20 20 20 20 20 70 4b  rderBy;.      pK
2b77a 65 79 4d 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45  eyMerge->enc = E
2b77b 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f  NC(db);.      fo
2b77c 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42  r(i=0; i<nOrderB
2b77d 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  y; i++){.       
2b77e 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
2b77f 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
2b780 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d  Term = pOrderBy-
2b781 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
2b782 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
2b783 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
2b784 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20  llate ){.       
2b785 20 20 20 70 43 6f 6c 6c 20 3d 20 70 54 65 72 6d     pColl = pTerm
2b786 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  ->pColl;.       
2b787 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2b788 20 20 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53    pColl = multiS
2b789 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
2b78a 72 73 65 2c 20 70 2c 20 61 50 65 72 6d 75 74 65  rse, p, aPermute
2b78b 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [i]);.          
2b78c 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20  pTerm->flags |= 
2b78d 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
2b78e 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e           pTerm->
2b78f 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20  pColl = pColl;. 
2b790 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2b791 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c   pKeyMerge->aCol
2b792 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20  l[i] = pColl;.  
2b793 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d        pKeyMerge-
2b794 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d  >aSortOrder[i] =
2b795 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
2b796 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20  sortOrder;.     
2b797 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2b798 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65 20  {.    pKeyMerge 
2b799 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  = 0;.  }..  /* R
2b79a 65 61 74 74 61 63 68 20 74 68 65 20 4f 52 44 45  eattach the ORDE
2b79b 52 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 74  R BY clause to t
2b79c 68 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20  he query..  */. 
2b79d 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70   p->pOrderBy = p
2b79e 4f 72 64 65 72 42 79 3b 0a 20 20 70 50 72 69 6f  OrderBy;.  pPrio
2b79f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71  r->pOrderBy = sq
2b7a0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
2b7a1 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72  (pParse->db, pOr
2b7a2 64 65 72 42 79 2c 20 30 29 3b 0a 0a 20 20 2f 2a  derBy, 0);..  /*
2b7a3 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67   Allocate a rang
2b7a4 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72  e of temporary r
2b7a5 65 67 69 73 74 65 72 73 20 61 6e 64 20 74 68 65  egisters and the
2b7a6 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a   KeyInfo needed.
2b7a7 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67    ** for the log
2b7a8 69 63 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20  ic that removes 
2b7a9 64 75 70 6c 69 63 61 74 65 20 72 65 73 75 6c 74  duplicate result
2b7aa 20 72 6f 77 73 20 77 68 65 6e 20 74 68 65 0a 20   rows when the. 
2b7ab 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 73 20   ** operator is 
2b7ac 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f  UNION, EXCEPT, o
2b7ad 72 20 49 4e 54 45 52 53 45 43 54 20 28 62 75 74  r INTERSECT (but
2b7ae 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e   not UNION ALL).
2b7af 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2b7b0 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65  TK_ALL ){.    re
2b7b1 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c  gPrev = 0;.  }el
2b7b2 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45 78 70  se{.    int nExp
2b7b3 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  r = p->pEList->n
2b7b4 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74  Expr;.    assert
2b7b5 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70  ( nOrderBy>=nExp
2b7b6 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2b7b7 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 67  ailed );.    reg
2b7b8 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 47 65  Prev = sqlite3Ge
2b7b9 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
2b7ba 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20  e, nExpr+1);.   
2b7bb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b7bc 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2b7bd 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b 0a 20  , 0, regPrev);. 
2b7be 20 20 20 70 4b 65 79 44 75 70 20 3d 20 73 71 6c     pKeyDup = sql
2b7bf 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2b7c0 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
2b7c1 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
2b7c2 4b 65 79 44 75 70 29 20 2b 20 6e 45 78 70 72 2a  KeyDup) + nExpr*
2b7c3 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  (sizeof(CollSeq*
2b7c4 29 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20  )+1) );.    if( 
2b7c5 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20 20 20  pKeyDup ){.     
2b7c6 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f   pKeyDup->aSortO
2b7c7 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65  rder = (u8*)&pKe
2b7c8 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70  yDup->aColl[nExp
2b7c9 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75  r];.      pKeyDu
2b7ca 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  p->nField = (u16
2b7cb 29 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4b  )nExpr;.      pK
2b7cc 65 79 44 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43  eyDup->enc = ENC
2b7cd 28 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  (db);.      for(
2b7ce 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b  i=0; i<nExpr; i+
2b7cf 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79  +){.        pKey
2b7d0 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20  Dup->aColl[i] = 
2b7d1 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53  multiSelectCollS
2b7d2 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 69 29  eq(pParse, p, i)
2b7d3 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75  ;.        pKeyDu
2b7d4 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  p->aSortOrder[i]
2b7d5 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2b7d6 20 20 7d 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53    }.  }. .  /* S
2b7d7 65 70 61 72 61 74 65 20 74 68 65 20 6c 65 66 74  eparate the left
2b7d8 20 61 6e 64 20 74 68 65 20 72 69 67 68 74 20 71   and the right q
2b7d9 75 65 72 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e  uery from one an
2b7da 6f 74 68 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e  other.  */.  p->
2b7db 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50  pPrior = 0;.  pP
2b7dc 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74  rior->pRightmost
2b7dd 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52   = 0;.  sqlite3R
2b7de 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
2b7df 42 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  By(pParse, p, p-
2b7e0 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45  >pOrderBy, "ORDE
2b7e1 52 22 29 3b 0a 20 20 69 66 28 20 70 50 72 69 6f  R");.  if( pPrio
2b7e2 72 2d 3e 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  r->pPrior==0 ){.
2b7e3 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c      sqlite3Resol
2b7e4 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70  veOrderGroupBy(p
2b7e5 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 70  Parse, pPrior, p
2b7e6 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c  Prior->pOrderBy,
2b7e7 20 22 4f 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a   "ORDER");.  }..
2b7e8 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    /* Compute the
2b7e9 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73   limit registers
2b7ea 20 2a 2f 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d   */.  computeLim
2b7eb 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
2b7ec 73 65 2c 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29  se, p, labelEnd)
2b7ed 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  ;.  if( p->iLimi
2b7ee 74 20 26 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20  t && op==TK_ALL 
2b7ef 29 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41  ){.    regLimitA
2b7f0 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2b7f1 6d 3b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 42  m;.    regLimitB
2b7f2 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2b7f3 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
2b7f4 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2b7f5 6f 70 79 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20  opy, p->iOffset 
2b7f6 3f 20 70 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a  ? p->iOffset+1 :
2b7f7 20 70 2d 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20   p->iLimit,.    
2b7f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
2b7fa 67 4c 69 6d 69 74 41 29 3b 0a 20 20 20 20 73 71  gLimitA);.    sq
2b7fb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b7fc 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c  v, OP_Copy, regL
2b7fd 69 6d 69 74 41 2c 20 72 65 67 4c 69 6d 69 74 42  imitA, regLimitB
2b7fe 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2b7ff 72 65 67 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c  regLimitA = regL
2b800 69 6d 69 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20  imitB = 0;.  }. 
2b801 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2b802 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
2b803 29 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  );.  p->pLimit =
2b804 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   0;.  sqlite3Exp
2b805 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
2b806 4f 66 66 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f  Offset);.  p->pO
2b807 66 66 73 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65  ffset = 0;..  re
2b808 67 41 64 64 72 41 20 3d 20 2b 2b 70 50 61 72 73  gAddrA = ++pPars
2b809 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f  e->nMem;.  regEo
2b80a 66 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  fA = ++pParse->n
2b80b 4d 65 6d 3b 0a 20 20 72 65 67 41 64 64 72 42 20  Mem;.  regAddrB 
2b80c 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2b80d 3b 0a 20 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b  ;.  regEofB = ++
2b80e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2b80f 72 65 67 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72  regOutA = ++pPar
2b810 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f  se->nMem;.  regO
2b811 75 74 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  utB = ++pParse->
2b812 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53  nMem;.  sqlite3S
2b813 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
2b814 65 73 74 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74  estA, SRT_Corout
2b815 69 6e 65 2c 20 72 65 67 41 64 64 72 41 29 3b 0a  ine, regAddrA);.
2b816 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2b817 65 73 74 49 6e 69 74 28 26 64 65 73 74 42 2c 20  estInit(&destB, 
2b818 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72  SRT_Coroutine, r
2b819 65 67 41 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20  egAddrB);..  /* 
2b81a 4a 75 6d 70 20 70 61 73 74 20 74 68 65 20 76 61  Jump past the va
2b81b 72 69 6f 75 73 20 73 75 62 72 6f 75 74 69 6e 65  rious subroutine
2b81c 73 20 61 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73  s and coroutines
2b81d 20 74 6f 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a   to the main.  *
2b81e 2a 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a  * merge loop.  *
2b81f 2f 0a 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33  /.  j1 = sqlite3
2b820 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
2b821 5f 47 6f 74 6f 29 3b 0a 20 20 61 64 64 72 53 65  _Goto);.  addrSe
2b822 6c 65 63 74 41 20 3d 20 73 71 6c 69 74 65 33 56  lectA = sqlite3V
2b823 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2b824 29 3b 0a 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  );...  /* Genera
2b825 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74  te a coroutine t
2b826 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53  o evaluate the S
2b827 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2b828 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74  to the.  ** left
2b829 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
2b82a 20 6f 70 65 72 61 74 6f 72 20 2d 20 74 68 65 20   operator - the 
2b82b 22 41 22 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f  "A" select..  */
2b82c 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  .  VdbeNoopComme
2b82d 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20 63 6f  nt((v, "Begin co
2b82e 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74  routine for left
2b82f 20 53 45 4c 45 43 54 22 29 29 3b 0a 20 20 70 50   SELECT"));.  pP
2b830 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72  rior->iLimit = r
2b831 65 67 4c 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69  egLimitA;.  sqli
2b832 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2b833 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74 41  , pPrior, &destA
2b834 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b835 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2b836 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 41  eger, 1, regEofA
2b837 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b838 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2b839 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2b83a 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2b83b 28 28 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74  ((v, "End corout
2b83c 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c  ine for left SEL
2b83d 45 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65  ECT"));..  /* Ge
2b83e 6e 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69  nerate a corouti
2b83f 6e 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74  ne to evaluate t
2b840 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2b841 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65  ent on .  ** the
2b842 20 72 69 67 68 74 20 2d 20 74 68 65 20 22 42 22   right - the "B"
2b843 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61   select.  */.  a
2b844 64 64 72 53 65 6c 65 63 74 42 20 3d 20 73 71 6c  ddrSelectB = sql
2b845 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2b846 64 64 72 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f  ddr(v);.  VdbeNo
2b847 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42  opComment((v, "B
2b848 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66  egin coroutine f
2b849 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
2b84a 29 29 3b 0a 20 20 73 61 76 65 64 4c 69 6d 69 74  ));.  savedLimit
2b84b 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20   = p->iLimit;.  
2b84c 73 61 76 65 64 4f 66 66 73 65 74 20 3d 20 70 2d  savedOffset = p-
2b84d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69  >iOffset;.  p->i
2b84e 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69 74  Limit = regLimit
2b84f 42 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20  B;.  p->iOffset 
2b850 3d 20 30 3b 20 20 0a 20 20 73 71 6c 69 74 65 33  = 0;  .  sqlite3
2b851 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2b852 2c 20 26 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e  , &destB);.  p->
2b853 69 4c 69 6d 69 74 20 3d 20 73 61 76 65 64 4c 69  iLimit = savedLi
2b854 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65  mit;.  p->iOffse
2b855 74 20 3d 20 73 61 76 65 64 4f 66 66 73 65 74 3b  t = savedOffset;
2b856 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b857 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2b858 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 42 29 3b  er, 1, regEofB);
2b859 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b85a 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
2b85b 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 56  , regAddrB);.  V
2b85c 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
2b85d 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e  v, "End coroutin
2b85e 65 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45  e for right SELE
2b85f 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  CT"));..  /* Gen
2b860 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2b861 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
2b862 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2b863 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a 20 73 65  of the A.  ** se
2b864 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
2b865 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
2b866 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
2b867 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  ct..  */.  VdbeN
2b868 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
2b869 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66  Output routine f
2b86a 6f 72 20 41 22 29 29 3b 0a 20 20 61 64 64 72 4f  or A"));.  addrO
2b86b 75 74 41 20 3d 20 67 65 6e 65 72 61 74 65 4f 75  utA = generateOu
2b86c 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70  tputSubroutine(p
2b86d 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
2b86e 20 20 20 20 20 20 20 20 70 2c 20 26 64 65 73 74          p, &dest
2b86f 41 2c 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74  A, pDest, regOut
2b870 41 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  A,.             
2b871 20 20 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65      regPrev, pKe
2b872 79 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  yDup, P4_KEYINFO
2b873 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45  _HANDOFF, labelE
2b874 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e  nd);.  .  /* Gen
2b875 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2b876 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
2b877 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
2b878 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a 20 73 65  of the B.  ** se
2b879 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
2b87a 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
2b87b 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
2b87c 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  ct..  */.  if( o
2b87d 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d  p==TK_ALL || op=
2b87e 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20  =TK_UNION ){.   
2b87f 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2b880 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72 6f 75  ((v, "Output rou
2b881 74 69 6e 65 20 66 6f 72 20 42 22 29 29 3b 0a 20  tine for B"));. 
2b882 20 20 20 61 64 64 72 4f 75 74 42 20 3d 20 67 65     addrOutB = ge
2b883 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72  nerateOutputSubr
2b884 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20  outine(pParse,. 
2b885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b886 70 2c 20 26 64 65 73 74 42 2c 20 70 44 65 73 74  p, &destB, pDest
2b887 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20 20 20 20  , regOutB,.     
2b888 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50              regP
2b889 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34  rev, pKeyDup, P4
2b88a 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c  _KEYINFO_STATIC,
2b88b 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a   labelEnd);.  }.
2b88c 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
2b88d 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72   subroutine to r
2b88e 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75  un when the resu
2b88f 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20  lts from select 
2b890 41 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75  A.  ** are exhau
2b891 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61  sted and only da
2b892 74 61 20 69 6e 20 73 65 6c 65 63 74 20 42 20 72  ta in select B r
2b893 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56  emains..  */.  V
2b894 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
2b895 76 2c 20 22 65 6f 66 2d 41 20 73 75 62 72 6f 75  v, "eof-A subrou
2b896 74 69 6e 65 22 29 29 3b 0a 20 20 69 66 28 20 6f  tine"));.  if( o
2b897 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20  p==TK_EXCEPT || 
2b898 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
2b899 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 41   ){.    addrEofA
2b89a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2b89b 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2b89c 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   0, labelEnd);. 
2b89d 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 61 64   }else{  .    ad
2b89e 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33  drEofA = sqlite3
2b89f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b8a0 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61  _If, regEofB, la
2b8a1 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c  belEnd);.    sql
2b8a2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b8a3 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
2b8a4 75 74 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a  utB, addrOutB);.
2b8a5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b8a6 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2b8a7 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20  d, regAddrB);.  
2b8a8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b8a9 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2b8aa 30 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  0, addrEofA);.  
2b8ab 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
2b8ac 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f   a subroutine to
2b8ad 20 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65   run when the re
2b8ae 73 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63  sults from selec
2b8af 74 20 42 0a 20 20 2a 2a 20 61 72 65 20 65 78 68  t B.  ** are exh
2b8b0 61 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20  austed and only 
2b8b1 64 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20 41  data in select A
2b8b2 20 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20   remains..  */. 
2b8b3 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45   if( op==TK_INTE
2b8b4 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64  RSECT ){.    add
2b8b5 72 45 6f 66 42 20 3d 20 61 64 64 72 45 6f 66 41  rEofB = addrEofA
2b8b6 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
2b8b7 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2b8b8 28 28 76 2c 20 22 65 6f 66 2d 42 20 73 75 62 72  ((v, "eof-B subr
2b8b9 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61  outine"));.    a
2b8ba 64 64 72 45 6f 66 42 20 3d 20 73 71 6c 69 74 65  ddrEofB = sqlite
2b8bb 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b8bc 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c  P_If, regEofA, l
2b8bd 61 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71  abelEnd);.    sq
2b8be 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b8bf 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2b8c0 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b  OutA, addrOutA);
2b8c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b8c2 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2b8c3 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2b8c4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b8c5 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2b8c6 20 30 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20   0, addrEofB);. 
2b8c7 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2b8c8 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65  e code to handle
2b8c9 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3c 42   the case of A<B
2b8ca 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
2b8cb 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c  Comment((v, "A-l
2b8cc 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29  t-B subroutine")
2b8cd 29 3b 0a 20 20 61 64 64 72 41 6c 74 42 20 3d 20  );.  addrAltB = 
2b8ce 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b8cf 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b8d0 65 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41  egOutA, addrOutA
2b8d1 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b8d2 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2b8d3 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2b8d4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b8d5 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
2b8d6 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b  EofA, addrEofA);
2b8d7 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b8d8 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2b8d9 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a   0, labelCmpr);.
2b8da 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
2b8db 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
2b8dc 65 20 63 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20  e case of A==B. 
2b8dd 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   */.  if( op==TK
2b8de 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 61 64 64 72  _ALL ){.    addr
2b8df 41 65 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b  AeqB = addrAltB;
2b8e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
2b8e1 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b  =TK_INTERSECT ){
2b8e2 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d 20  .    addrAeqB = 
2b8e3 61 64 64 72 41 6c 74 42 3b 0a 20 20 20 20 61 64  addrAltB;.    ad
2b8e4 64 72 41 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73  drAltB++;.  }els
2b8e5 65 7b 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43  e{.    VdbeNoopC
2b8e6 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 65 71  omment((v, "A-eq
2b8e7 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
2b8e8 3b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d  ;.    addrAeqB =
2b8e9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b8ea 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
2b8eb 6c 64 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20  ld, regAddrA);. 
2b8ec 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b8ed 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
2b8ee 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41  egEofA, addrEofA
2b8ef 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2b8f0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2b8f1 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70  oto, 0, labelCmp
2b8f2 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65  r);.  }..  /* Ge
2b8f3 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68  nerate code to h
2b8f4 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f  andle the case o
2b8f5 66 20 41 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62  f A>B.  */.  Vdb
2b8f6 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
2b8f7 20 22 41 2d 67 74 2d 42 20 73 75 62 72 6f 75 74   "A-gt-B subrout
2b8f8 69 6e 65 22 29 29 3b 0a 20 20 61 64 64 72 41 67  ine"));.  addrAg
2b8f9 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  tB = sqlite3Vdbe
2b8fa 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2b8fb 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c    if( op==TK_ALL
2b8fc 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   || op==TK_UNION
2b8fd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2b8fe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b8ff 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20  Gosub, regOutB, 
2b900 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20  addrOutB);.  }. 
2b901 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b902 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2b903 72 65 67 41 64 64 72 42 29 3b 0a 20 20 73 71 6c  regAddrB);.  sql
2b904 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b905 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42  , OP_If, regEofB
2b906 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 73  , addrEofB);.  s
2b907 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b908 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2b909 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f  labelCmpr);..  /
2b90a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 75 6e 73  * This code runs
2b90b 20 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c   once to initial
2b90c 69 7a 65 20 65 76 65 72 79 74 68 69 6e 67 2e 0a  ize everything..
2b90d 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2b90e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
2b90f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b910 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2b911 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 41  eger, 0, regEofA
2b912 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b913 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2b914 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f 66 42  eger, 0, regEofB
2b915 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2b916 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
2b917 75 62 2c 20 72 65 67 41 64 64 72 41 2c 20 61 64  ub, regAddrA, ad
2b918 64 72 53 65 6c 65 63 74 41 29 3b 0a 20 20 73 71  drSelectA);.  sq
2b919 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b91a 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
2b91b 41 64 64 72 42 2c 20 61 64 64 72 53 65 6c 65 63  AddrB, addrSelec
2b91c 74 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  tB);.  sqlite3Vd
2b91d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b91e 66 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64 72  f, regEofA, addr
2b91f 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65 33  EofA);.  sqlite3
2b920 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b921 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64  _If, regEofB, ad
2b922 64 72 45 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49  drEofB);..  /* I
2b923 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 6d 61 69  mplement the mai
2b924 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a  n merge loop.  *
2b925 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
2b926 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c  esolveLabel(v, l
2b927 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c  abelCmpr);.  sql
2b928 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2b929 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  , OP_Permutation
2b92a 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
2b92b 2a 29 61 50 65 72 6d 75 74 65 2c 20 50 34 5f 49  *)aPermute, P4_I
2b92c 4e 54 41 52 52 41 59 29 3b 0a 20 20 73 71 6c 69  NTARRAY);.  sqli
2b92d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2b92e 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73   OP_Compare, des
2b92f 74 41 2e 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69  tA.iMem, destB.i
2b930 4d 65 6d 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20  Mem, nOrderBy,. 
2b931 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b932 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2b933 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59  KeyMerge, P4_KEY
2b934 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
2b935 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b936 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61  p3(v, OP_Jump, a
2b937 64 64 72 41 6c 74 42 2c 20 61 64 64 72 41 65 71  ddrAltB, addrAeq
2b938 42 2c 20 61 64 64 72 41 67 74 42 29 3b 0a 0a 20  B, addrAgtB);.. 
2b939 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 65 6d 70   /* Release temp
2b93a 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 0a  orary registers.
2b93b 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72    */.  if( regPr
2b93c 65 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ev ){.    sqlite
2b93d 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2b93e 65 28 70 50 61 72 73 65 2c 20 72 65 67 50 72 65  e(pParse, regPre
2b93f 76 2c 20 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a  v, nOrderBy+1);.
2b940 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74    }..  /* Jump t
2b941 6f 20 74 68 65 20 74 68 69 73 20 70 6f 69 6e 74  o the this point
2b942 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 65 72   in order to ter
2b943 6d 69 6e 61 74 65 20 74 68 65 20 71 75 65 72 79  minate the query
2b944 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2b945 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2b946 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a  (v, labelEnd);..
2b947 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d    /* Set the num
2b948 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 63 6f  ber of output co
2b949 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28  lumns.  */.  if(
2b94a 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
2b94b 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
2b94c 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
2b94d 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 77 68  = pPrior;.    wh
2b94e 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72  ile( pFirst->pPr
2b94f 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70  ior ) pFirst = p
2b950 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  First->pPrior;. 
2b951 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d     generateColum
2b952 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30  nNames(pParse, 0
2b953 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74  , pFirst->pEList
2b954 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61  );.  }..  /* Rea
2b955 73 73 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70  ssembly the comp
2b956 6f 75 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68  ound query so th
2b957 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72  at it will be fr
2b958 65 65 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20  eed correctly.  
2b959 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ** by the callin
2b95a 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  g function */.  
2b95b 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b  if( p->pPrior ){
2b95c 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
2b95d 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  ctDelete(db, p->
2b95e 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70  pPrior);.  }.  p
2b95f 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
2b960 72 3b 0a 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20  r;..  /*** TBD: 
2b961 20 49 6e 73 65 72 74 20 73 75 62 72 6f 75 74 69   Insert subrouti
2b962 6e 65 20 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73  ne calls to clos
2b963 65 20 63 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63  e cursors on inc
2b964 6f 6d 70 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73  omplete.  **** s
2b965 75 62 71 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a  ubqueries ****/.
2b966 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2b967 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  OK;.}.#endif..#i
2b968 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2b969 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
2b96a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2b96b 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f  ITE_OMIT_VIEW)./
2b96c 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72  * Forward Declar
2b96d 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63  ations */.static
2b96e 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
2b96f 69 73 74 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ist(sqlite3*, Ex
2b970 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78  prList*, int, Ex
2b971 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63  prList*);.static
2b972 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63   void substSelec
2b973 74 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65  t(sqlite3*, Sele
2b974 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ct *, int, ExprL
2b975 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53  ist *);../*.** S
2b976 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  can through the 
2b977 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
2b978 2e 20 20 52 65 70 6c 61 63 65 20 65 76 65 72 79  .  Replace every
2b979 20 72 65 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a   reference to.**
2b97a 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62   a column in tab
2b97b 6c 65 20 6e 75 6d 62 65 72 20 69 54 61 62 6c 65  le number iTable
2b97c 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20   with a copy of 
2b97d 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a  the iColumn-th.*
2b97e 2a 20 65 6e 74 72 79 20 69 6e 20 70 45 4c 69 73  * entry in pELis
2b97f 74 2e 20 20 28 42 75 74 20 6c 65 61 76 65 20 72  t.  (But leave r
2b980 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
2b981 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a   ROWID column .*
2b982 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a  * unchanged.).**
2b983 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2b984 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
2b985 66 6c 61 74 74 65 6e 69 6e 67 20 70 72 6f 63 65  flattening proce
2b986 64 75 72 65 2e 20 20 41 20 73 75 62 71 75 65 72  dure.  A subquer
2b987 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65 73 75 6c  y.** whose resul
2b988 74 20 73 65 74 20 69 73 20 64 65 66 69 6e 65 64  t set is defined
2b989 20 62 79 20 70 45 4c 69 73 74 20 61 70 70 65 61   by pEList appea
2b98a 72 73 20 61 73 20 65 6e 74 72 79 20 69 6e 20 74  rs as entry in t
2b98b 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  he.** FROM claus
2b98c 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 75  e of a SELECT su
2b98d 63 68 20 74 68 61 74 20 74 68 65 20 56 44 42 45  ch that the VDBE
2b98e 20 63 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64   cursor assigned
2b98f 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d   to that.** FORM
2b990 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73   clause entry is
2b991 20 69 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72   iTable.  This r
2b992 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20  outine make the 
2b993 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a 20 63 68  necessary .** ch
2b994 61 6e 67 65 73 20 74 6f 20 70 45 78 70 72 20 73  anges to pExpr s
2b995 6f 20 74 68 61 74 20 69 74 20 72 65 66 65 72 73  o that it refers
2b996 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
2b997 20 73 6f 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a   source table.**
2b998 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2b999 20 72 61 74 68 65 72 20 74 68 65 20 72 65 73 75   rather the resu
2b99a 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 73 75  lt set of the su
2b99b 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  bquery..*/.stati
2b99c 63 20 45 78 70 72 20 2a 73 75 62 73 74 45 78 70  c Expr *substExp
2b99d 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
2b99e 2c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f  ,        /* Repo
2b99f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  rt malloc errors
2b9a0 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e 65 63 74   to this connect
2b9a1 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
2b9a2 45 78 70 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  Expr,        /* 
2b9a3 45 78 70 72 20 69 6e 20 77 68 69 63 68 20 73 75  Expr in which su
2b9a4 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72  bstitution occur
2b9a5 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  s */.  int iTabl
2b9a6 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  e,         /* Ta
2b9a7 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69  ble to be substi
2b9a8 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  tuted */.  ExprL
2b9a9 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
2b9aa 2a 20 53 75 62 73 74 69 74 75 74 65 20 65 78 70  * Substitute exp
2b9ab 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20  ressions */.){. 
2b9ac 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
2b9ad 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2b9ae 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
2b9af 4c 55 4d 4e 20 26 26 20 70 45 78 70 72 2d 3e 69  LUMN && pExpr->i
2b9b0 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b  Table==iTable ){
2b9b1 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e  .    if( pExpr->
2b9b2 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20  iColumn<0 ){.   
2b9b3 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
2b9b4 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73  K_NULL;.    }els
2b9b5 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  e{.      Expr *p
2b9b6 4e 65 77 3b 0a 20 20 20 20 20 20 61 73 73 65 72  New;.      asser
2b9b7 74 28 20 70 45 4c 69 73 74 21 3d 30 20 26 26 20  t( pEList!=0 && 
2b9b8 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70  pExpr->iColumn<p
2b9b9 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a  EList->nExpr );.
2b9ba 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
2b9bb 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26  xpr->pLeft==0 &&
2b9bc 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d   pExpr->pRight==
2b9bd 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20  0 );.      pNew 
2b9be 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
2b9bf 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 70  (db, pEList->a[p
2b9c0 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70  Expr->iColumn].p
2b9c1 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  Expr, 0);.      
2b9c2 69 66 28 20 70 4e 65 77 20 26 26 20 70 45 78 70  if( pNew && pExp
2b9c3 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  r->pColl ){.    
2b9c4 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20      pNew->pColl 
2b9c5 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a  = pExpr->pColl;.
2b9c6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2b9c7 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2b9c8 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  db, pExpr);.    
2b9c9 20 20 70 45 78 70 72 20 3d 20 70 4e 65 77 3b 0a    pExpr = pNew;.
2b9ca 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2b9cb 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20     pExpr->pLeft 
2b9cc 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20  = substExpr(db, 
2b9cd 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54  pExpr->pLeft, iT
2b9ce 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
2b9cf 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
2b9d0 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c   = substExpr(db,
2b9d1 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
2b9d2 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2b9d3 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
2b9d4 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2b9d5 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2b9d6 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c 65  .      substSele
2b9d7 63 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e  ct(db, pExpr->x.
2b9d8 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c  pSelect, iTable,
2b9d9 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 65   pEList);.    }e
2b9da 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62 73 74  lse{.      subst
2b9db 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 45 78  ExprList(db, pEx
2b9dc 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61  pr->x.pList, iTa
2b9dd 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b9de 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2b9df 20 70 45 78 70 72 3b 0a 7d 0a 73 74 61 74 69 63   pExpr;.}.static
2b9e0 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
2b9e1 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ist(.  sqlite3 *
2b9e2 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  db,         /* R
2b9e3 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
2b9e4 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45 78  ors here */.  Ex
2b9e5 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
2b9e6 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63     /* List to sc
2b9e7 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68 20  an and in which 
2b9e8 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75  to make substitu
2b9e9 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  tes */.  int iTa
2b9ea 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
2b9eb 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
2b9ec 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
2b9ed 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
2b9ee 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
2b9ef 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20   values */.){.  
2b9f0 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
2b9f1 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  st==0 ) return;.
2b9f2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
2b9f3 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2b9f4 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  .    pList->a[i]
2b9f5 2e 70 45 78 70 72 20 3d 20 73 75 62 73 74 45 78  .pExpr = substEx
2b9f6 70 72 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  pr(db, pList->a[
2b9f7 69 5d 2e 70 45 78 70 72 2c 20 69 54 61 62 6c 65  i].pExpr, iTable
2b9f8 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d  , pEList);.  }.}
2b9f9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
2b9fa 73 74 53 65 6c 65 63 74 28 0a 20 20 73 71 6c 69  stSelect(.  sqli
2b9fb 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2b9fc 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f   /* Report mallo
2b9fd 63 20 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f  c errors here */
2b9fe 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2b9ff 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
2ba00 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 77  T statement in w
2ba01 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62  hich to make sub
2ba02 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20  stitutions */.  
2ba03 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
2ba04 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
2ba05 20 62 65 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a   be replaced */.
2ba06 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2ba07 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69  st     /* Substi
2ba08 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29  tute values */.)
2ba09 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
2ba0a 63 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  c;.  struct SrcL
2ba0b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2ba0c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2ba0d 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  !p ) return;.  s
2ba0e 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
2ba0f 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 54 61 62   p->pEList, iTab
2ba10 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
2ba11 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
2ba12 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 54   p->pGroupBy, iT
2ba13 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
2ba14 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
2ba15 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  b, p->pOrderBy, 
2ba16 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2ba17 0a 20 20 70 2d 3e 70 48 61 76 69 6e 67 20 3d 20  .  p->pHaving = 
2ba18 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d  substExpr(db, p-
2ba19 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65  >pHaving, iTable
2ba1a 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 2d 3e  , pEList);.  p->
2ba1b 70 57 68 65 72 65 20 3d 20 73 75 62 73 74 45 78  pWhere = substEx
2ba1c 70 72 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  pr(db, p->pWhere
2ba1d 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
2ba1e 29 3b 0a 20 20 73 75 62 73 74 53 65 6c 65 63 74  );.  substSelect
2ba1f 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  (db, p->pPrior, 
2ba20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2ba21 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
2ba22 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72  c;.  assert( pSr
2ba23 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f  c );  /* Even fo
2ba24 72 20 28 53 45 4c 45 43 54 20 31 29 20 77 65 20  r (SELECT 1) we 
2ba25 68 61 76 65 3a 20 70 53 72 63 21 3d 30 20 62 75  have: pSrc!=0 bu
2ba26 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20  t pSrc->nSrc==0 
2ba27 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  */.  if( ALWAYS(
2ba28 70 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72  pSrc) ){.    for
2ba29 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70  (i=pSrc->nSrc, p
2ba2a 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e  Item=pSrc->a; i>
2ba2b 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
2ba2c 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c  {.      substSel
2ba2d 65 63 74 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ect(db, pItem->p
2ba2e 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20  Select, iTable, 
2ba2f 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  pEList);.    }. 
2ba30 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21   }.}.#endif /* !
2ba31 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2ba32 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2ba33 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2ba34 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a  _OMIT_VIEW) */..
2ba35 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2ba36 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2ba37 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  Y) || !defined(S
2ba38 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2ba39 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2ba3a 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
2ba3b 66 6c 61 74 74 65 6e 20 73 75 62 71 75 65 72 69  flatten subqueri
2ba3c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  es in order to s
2ba3d 70 65 65 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f  peed.** executio
2ba3e 6e 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 31  n.  It returns 1
2ba3f 20 69 66 20 69 74 20 6d 61 6b 65 73 20 63 68 61   if it makes cha
2ba40 6e 67 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f  nges and 0 if no
2ba41 20 66 6c 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f   flattening.** o
2ba42 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  ccurs..**.** To 
2ba43 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 63  understand the c
2ba44 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61 74 74 65  oncept of flatte
2ba45 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65 72 20 74  ning, consider t
2ba46 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
2ba47 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  query:.**.**    
2ba48 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 28   SELECT a FROM (
2ba49 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20  SELECT x+y AS a 
2ba4a 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c  FROM t1 WHERE z<
2ba4b 31 30 30 29 20 57 48 45 52 45 20 61 3e 35 0a 2a  100) WHERE a>5.*
2ba4c 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
2ba4d 20 77 61 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e   way of implemen
2ba4e 74 69 6e 67 20 74 68 69 73 20 71 75 65 72 79 20  ting this query 
2ba4f 69 73 20 74 6f 20 65 78 65 63 75 74 65 20 74 68  is to execute th
2ba50 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 66 69  e.** subquery fi
2ba51 72 73 74 20 61 6e 64 20 73 74 6f 72 65 20 74 68  rst and store th
2ba52 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74  e results in a t
2ba53 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20  emporary table, 
2ba54 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20  then.** run the 
2ba55 6f 75 74 65 72 20 71 75 65 72 79 20 6f 6e 20 74  outer query on t
2ba56 68 61 74 20 74 65 6d 70 6f 72 61 72 79 20 74 61  hat temporary ta
2ba57 62 6c 65 2e 20 20 54 68 69 73 20 72 65 71 75 69  ble.  This requi
2ba58 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65  res two.** passe
2ba59 73 20 6f 76 65 72 20 74 68 65 20 64 61 74 61 2e  s over the data.
2ba5a 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 62    Furthermore, b
2ba5b 65 63 61 75 73 65 20 74 68 65 20 74 65 6d 70 6f  ecause the tempo
2ba5c 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61  rary table.** ha
2ba5d 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c 20 74 68  s no indices, th
2ba5e 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
2ba5f 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  n the outer quer
2ba60 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f  y cannot be.** o
2ba61 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  ptimized..**.** 
2ba62 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74  This routine att
2ba63 65 6d 70 74 73 20 74 6f 20 72 65 77 72 69 74 65  empts to rewrite
2ba64 20 71 75 65 72 69 65 73 20 73 75 63 68 20 61 73   queries such as
2ba65 20 74 68 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a   the above into.
2ba66 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66 6c 61 74  ** a single flat
2ba67 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68   select, like th
2ba68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  is:.**.**     SE
2ba69 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52  LECT x+y AS a FR
2ba6a 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30  OM t1 WHERE z<10
2ba6b 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20  0 AND a>5.**.** 
2ba6c 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
2ba6d 65 64 20 66 6f 72 20 74 68 69 73 20 73 69 6d 70  ed for this simp
2ba6e 69 66 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20  ification gives 
2ba6f 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c 74 0a  the same result.
2ba70 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 68 61 73 20  ** but only has 
2ba71 74 6f 20 73 63 61 6e 20 74 68 65 20 64 61 74 61  to scan the data
2ba72 20 6f 6e 63 65 2e 20 20 41 6e 64 20 62 65 63 61   once.  And beca
2ba73 75 73 65 20 69 6e 64 69 63 65 73 20 6d 69 67 68  use indices migh
2ba74 74 20 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74  t .** exist on t
2ba75 68 65 20 74 61 62 6c 65 20 74 31 2c 20 61 20 63  he table t1, a c
2ba76 6f 6d 70 6c 65 74 65 20 73 63 61 6e 20 6f 66 20  omplete scan of 
2ba77 74 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 62  the data might b
2ba78 65 0a 2a 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a  e.** avoided..**
2ba79 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 6e 67 20 69  .** Flattening i
2ba7a 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64  s only attempted
2ba7b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66   if all of the f
2ba7c 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75  ollowing are tru
2ba7d 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20  e:.**.**   (1)  
2ba7e 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  The subquery and
2ba7f 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2ba80 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65   do not both use
2ba81 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a   aggregates..**.
2ba82 2a 2a 20 20 20 28 32 29 20 20 54 68 65 20 73 75  **   (2)  The su
2ba83 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e  bquery is not an
2ba84 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68   aggregate or th
2ba85 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
2ba86 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a   not a join..**.
2ba87 2a 2a 20 20 20 28 33 29 20 20 54 68 65 20 73 75  **   (3)  The su
2ba88 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 74 68  bquery is not th
2ba89 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
2ba8a 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72 20  of a left outer 
2ba8b 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 28  join.**        (
2ba8c 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69 63 6b 65  Originally ticke
2ba8d 74 20 23 33 30 36 2e 20 20 53 74 72 65 6e 67 68  t #306.  Strengh
2ba8e 74 65 6e 65 64 20 62 79 20 74 69 63 6b 65 74 20  tened by ticket 
2ba8f 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a 20 20 20 28  #3300).**.**   (
2ba90 34 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  4)  The subquery
2ba91 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54   is not DISTINCT
2ba92 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2ba93 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69  ery is not a joi
2ba94 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 20  n..**.**   (5)  
2ba95 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  The subquery is 
2ba96 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20  not DISTINCT or 
2ba97 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2ba98 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20  does not use.** 
2ba99 20 20 20 20 20 20 20 61 67 67 72 65 67 61 74 65         aggregate
2ba9a 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20  s..**.**   (6)  
2ba9b 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
2ba9c 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65 67  s not use aggreg
2ba9d 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74 65  ates or the oute
2ba9e 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a  r query is not.*
2ba9f 2a 20 20 20 20 20 20 20 20 44 49 53 54 49 4e 43  *        DISTINC
2baa0 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20  T..**.**   (7)  
2baa1 54 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73  The subquery has
2baa2 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a   a FROM clause..
2baa3 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20 54 68 65  **.**   (8)  The
2baa4 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
2baa5 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20  ot use LIMIT or 
2baa6 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2baa7 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a  is not a join..*
2baa8 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54 68 65 20  *.**   (9)  The 
2baa9 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2baaa 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74  t use LIMIT or t
2baab 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
2baac 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20  oes not use.**  
2baad 20 20 20 20 20 20 61 67 67 72 65 67 61 74 65 73        aggregates
2baae 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29 20 20 54  ..**.**  (10)  T
2baaf 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
2bab0 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65 67 61   not use aggrega
2bab1 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72  tes or the outer
2bab2 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a   query does not.
2bab3 2a 2a 20 20 20 20 20 20 20 20 75 73 65 20 4c 49  **        use LI
2bab4 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29  MIT..**.**  (11)
2bab5 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 61    The subquery a
2bab6 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  nd the outer que
2bab7 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68  ry do not both h
2bab8 61 76 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  ave ORDER BY cla
2bab9 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32  uses..**.**  (12
2baba 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74  )  Not implement
2babb 65 64 2e 20 20 53 75 62 73 75 6d 65 64 20 69 6e  ed.  Subsumed in
2babc 74 6f 20 72 65 73 74 72 69 63 74 69 6f 6e 20 28  to restriction (
2babd 33 29 2e 20 20 57 61 73 20 70 72 65 76 69 6f 75  3).  Was previou
2babe 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 61 20  sly.**        a 
2babf 73 65 70 61 72 61 74 65 20 72 65 73 74 72 69 63  separate restric
2bac0 74 69 6f 6e 20 64 65 72 69 76 69 6e 67 20 66 72  tion deriving fr
2bac1 6f 6d 20 74 69 63 6b 65 74 20 23 33 35 30 2e 0a  om ticket #350..
2bac2 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20 54 68 65  **.**  (13)  The
2bac3 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 6f 75   subquery and ou
2bac4 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74  ter query do not
2bac5 20 62 6f 74 68 20 75 73 65 20 4c 49 4d 49 54 0a   both use LIMIT.
2bac6 2a 2a 0a 2a 2a 20 20 28 31 34 29 20 20 54 68 65  **.**  (14)  The
2bac7 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
2bac8 6f 74 20 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a  ot use OFFSET.**
2bac9 0a 2a 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f  .**  (15)  The o
2baca 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2bacb 74 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  t part of a comp
2bacc 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74  ound select or t
2bacd 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 73 75 62  he.**        sub
2bace 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 68  query does not h
2bacf 61 76 65 20 62 6f 74 68 20 61 6e 20 4f 52 44 45  ave both an ORDE
2bad0 52 20 42 59 20 61 6e 64 20 61 20 4c 49 4d 49 54  R BY and a LIMIT
2bad1 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20   clause..**     
2bad2 20 20 20 28 53 65 65 20 74 69 63 6b 65 74 20 23     (See ticket #
2bad3 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36  2339).**.**  (16
2bad4 29 20 20 54 68 65 20 6f 75 74 65 72 20 71 75 65  )  The outer que
2bad5 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67  ry is not an agg
2bad6 72 65 67 61 74 65 20 6f 72 20 74 68 65 20 73 75  regate or the su
2bad7 62 71 75 65 72 79 20 64 6f 65 73 0a 2a 2a 20 20  bquery does.**  
2bad8 20 20 20 20 20 20 6e 6f 74 20 63 6f 6e 74 61 69        not contai
2bad9 6e 20 4f 52 44 45 52 20 42 59 2e 20 20 28 54 69  n ORDER BY.  (Ti
2bada 63 6b 65 74 20 23 32 39 34 32 29 20 20 54 68 69  cket #2942)  Thi
2badb 73 20 75 73 65 64 20 74 6f 20 6e 6f 74 20 6d 61  s used to not ma
2badc 74 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 75  tter.**        u
2badd 6e 74 69 6c 20 77 65 20 69 6e 74 72 6f 64 75 63  ntil we introduc
2bade 65 64 20 74 68 65 20 67 72 6f 75 70 5f 63 6f 6e  ed the group_con
2badf 63 61 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  cat() function. 
2bae0 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 29 20 20 54   .**.**  (17)  T
2bae1 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20  he sub-query is 
2bae2 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73  not a compound s
2bae3 65 6c 65 63 74 2c 20 6f 72 20 69 74 20 69 73 20  elect, or it is 
2bae4 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 0a 2a 2a 20  a UNION ALL .** 
2bae5 20 20 20 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20         compound 
2bae6 63 6c 61 75 73 65 20 6d 61 64 65 20 75 70 20 65  clause made up e
2bae7 6e 74 69 72 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61  ntirely of non-a
2bae8 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
2bae9 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20  , and .**       
2baea 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
2baeb 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  y:.**.**        
2baec 20 20 2a 20 69 73 20 6e 6f 74 20 69 74 73 65 6c    * is not itsel
2baed 66 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  f part of a comp
2baee 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0a 2a 2a 20  ound select,.** 
2baef 20 20 20 20 20 20 20 20 20 2a 20 69 73 20 6e 6f           * is no
2baf0 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f  t an aggregate o
2baf1 72 20 44 49 53 54 49 4e 43 54 20 71 75 65 72 79  r DISTINCT query
2baf2 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  , and.**        
2baf3 20 20 2a 20 68 61 73 20 6e 6f 20 6f 74 68 65 72    * has no other
2baf4 20 74 61 62 6c 65 73 20 6f 72 20 73 75 62 2d 73   tables or sub-s
2baf5 65 6c 65 63 74 73 20 69 6e 20 74 68 65 20 46 52  elects in the FR
2baf6 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  OM clause..**.**
2baf7 20 20 20 20 20 20 20 20 54 68 65 20 70 61 72 65          The pare
2baf8 6e 74 20 61 6e 64 20 73 75 62 2d 71 75 65 72 79  nt and sub-query
2baf9 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 57 48 45   may contain WHE
2bafa 52 45 20 63 6c 61 75 73 65 73 2e 20 53 75 62 6a  RE clauses. Subj
2bafb 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ect to.**       
2bafc 20 72 75 6c 65 73 20 28 31 31 29 2c 20 28 31 33   rules (11), (13
2bafd 29 20 61 6e 64 20 28 31 34 29 2c 20 74 68 65 79  ) and (14), they
2bafe 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69   may also contai
2baff 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a 20 20  n ORDER BY,.**  
2bb00 20 20 20 20 20 20 4c 49 4d 49 54 20 61 6e 64 20        LIMIT and 
2bb01 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 0a  OFFSET clauses..
2bb02 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20 49 66 20  **.**  (18)  If 
2bb03 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73  the sub-query is
2bb04 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2bb05 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 72  ct, then all ter
2bb06 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  ms of the.**    
2bb07 20 20 20 20 4f 52 44 45 52 20 62 79 20 63 6c 61      ORDER by cla
2bb08 75 73 65 20 6f 66 20 74 68 65 20 70 61 72 65 6e  use of the paren
2bb09 74 20 6d 75 73 74 20 62 65 20 73 69 6d 70 6c 65  t must be simple
2bb0a 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 0a   references to .
2bb0b 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e  **        column
2bb0c 73 20 6f 66 20 74 68 65 20 73 75 62 2d 71 75 65  s of the sub-que
2bb0d 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 39 29 20  ry..**.**  (19) 
2bb0e 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
2bb0f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54  es not use LIMIT
2bb10 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2bb11 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ery does not.** 
2bb12 20 20 20 20 20 20 20 68 61 76 65 20 61 20 57 48         have a WH
2bb13 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ERE clause..**.*
2bb14 2a 20 20 28 32 30 29 20 20 49 66 20 74 68 65 20  *  (20)  If the 
2bb15 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
2bb16 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20  ompound select, 
2bb17 74 68 65 6e 20 69 74 20 6d 75 73 74 20 6e 6f 74  then it must not
2bb18 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 61   use.**        a
2bb19 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2bb1a 65 2e 20 20 54 69 63 6b 65 74 20 23 33 37 37 33  e.  Ticket #3773
2bb1b 2e 20 20 57 65 20 63 6f 75 6c 64 20 72 65 6c 61  .  We could rela
2bb1c 78 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  x this constrain
2bb1d 74 0a 2a 2a 20 20 20 20 20 20 20 20 73 6f 6d 65  t.**        some
2bb1e 77 68 61 74 20 62 79 20 73 61 79 69 6e 67 20 74  what by saying t
2bb1f 68 61 74 20 74 68 65 20 74 65 72 6d 73 20 6f 66  hat the terms of
2bb20 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2bb21 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20 20 20 20  ause must.**    
2bb22 20 20 20 20 61 70 70 65 61 72 20 61 73 20 75 6e      appear as un
2bb23 6d 6f 64 69 66 69 65 64 20 72 65 73 75 6c 74 20  modified result 
2bb24 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f  columns in the o
2bb25 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 75 74  uter query.  But
2bb26 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20  .**        have 
2bb27 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  other optimizati
2bb28 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 74 6f 20 64  ons in mind to d
2bb29 65 61 6c 20 77 69 74 68 20 74 68 61 74 20 63 61  eal with that ca
2bb2a 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69  se..**.** In thi
2bb2b 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 22  s routine, the "
2bb2c 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  p" parameter is 
2bb2d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2bb2e 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 2a 2a   outer query..**
2bb2f 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
2bb30 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46 72 6f   p->pSrc->a[iFro
2bb31 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20 74 72  m].  isAgg is tr
2bb32 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 20  ue if the outer 
2bb33 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20 61 67  query.** uses ag
2bb34 67 72 65 67 61 74 65 73 20 61 6e 64 20 73 75 62  gregates and sub
2bb35 71 75 65 72 79 49 73 41 67 67 20 69 73 20 74 72  queryIsAgg is tr
2bb36 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65  ue if the subque
2bb37 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74  ry uses aggregat
2bb38 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61  es..**.** If fla
2bb39 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74 20 61  ttening is not a
2bb3a 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20 72  ttempted, this r
2bb3b 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
2bb3c 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 2e  p and returns 0.
2bb3d 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69 6e  .** If flattenin
2bb3e 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 74  g is attempted t
2bb3f 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2bb40 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  rns 1..**.** All
2bb41 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
2bb42 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73 74  on analysis must
2bb43 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20 74   occur on both t
2bb44 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61  he outer query a
2bb45 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71 75 65  nd.** the subque
2bb46 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ry before this r
2bb47 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a  outine runs..*/.
2bb48 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 74 74  static int flatt
2bb49 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20 50 61  enSubquery(.  Pa
2bb4a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2bb4b 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2bb4c 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
2bb4d 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
2bb4e 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 72  /* The parent or
2bb4f 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 73 74   outer SELECT st
2bb50 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
2bb51 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20   iFrom,         
2bb52 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d    /* Index in p-
2bb53 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68  >pSrc->a[] of th
2bb54 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65 72 79  e inner subquery
2bb55 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 2c   */.  int isAgg,
2bb56 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
2bb57 75 65 20 69 66 20 6f 75 74 65 72 20 53 45 4c 45  ue if outer SELE
2bb58 43 54 20 75 73 65 73 20 61 67 67 72 65 67 61 74  CT uses aggregat
2bb59 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  e functions */. 
2bb5a 20 69 6e 74 20 73 75 62 71 75 65 72 79 49 73 41   int subqueryIsA
2bb5b 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  gg    /* True if
2bb5c 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73   the subquery us
2bb5d 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  es aggregate fun
2bb5e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63  ctions */.){.  c
2bb5f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65  onst char *zSave
2bb60 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  dAuthContext = p
2bb61 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
2bb62 65 78 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  ext;.  Select *p
2bb63 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c 65 63 74  Parent;.  Select
2bb64 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20 2f 2a   *pSub;       /*
2bb65 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65 72 79   The inner query
2bb66 20 6f 72 20 22 73 75 62 71 75 65 72 79 22 20 2a   or "subquery" *
2bb67 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
2bb68 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  1;      /* Point
2bb69 65 72 20 74 6f 20 74 68 65 20 72 69 67 68 74 6d  er to the rightm
2bb6a 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 73 75  ost select in su
2bb6b 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63  b-query */.  Src
2bb6c 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
2bb6d 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
2bb6e 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  use of the outer
2bb6f 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c   query */.  SrcL
2bb70 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20 20 20  ist *pSubSrc;   
2bb71 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
2bb72 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
2bb73 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ry */.  ExprList
2bb74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54   *pList;    /* T
2bb75 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2bb76 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2bb77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
2bb78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42  t;        /* VDB
2bb79 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
2bb7a 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73 75  of the pSub resu
2bb7b 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62 6c  lt set temp tabl
2bb7c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
2bb7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2bb7e 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2bb7f 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20  Expr *pWhere;   
2bb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb81 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2bb82 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
2bb83 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2bb84 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68  Subitem;   /* Th
2bb85 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
2bb86 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2bb87 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
2bb88 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2bb89 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65  flattening is pe
2bb8a 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75 72 6e  rmitted.  Return
2bb8b 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a   0 if not..  */.
2bb8c 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
2bb8d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
2bb8e 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a 20  Prior==0 );  /* 
2bb8f 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61 74 74 65  Unable to flatte
2bb90 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69  n compound queri
2bb91 65 73 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70  es */.  pSrc = p
2bb92 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74  ->pSrc;.  assert
2bb93 28 20 70 53 72 63 20 26 26 20 69 46 72 6f 6d 3e  ( pSrc && iFrom>
2bb94 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53 72 63  =0 && iFrom<pSrc
2bb95 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53 75 62  ->nSrc );.  pSub
2bb96 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b  item = &pSrc->a[
2bb97 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61 72 65 6e  iFrom];.  iParen
2bb98 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 69 43  t = pSubitem->iC
2bb99 75 72 73 6f 72 3b 0a 20 20 70 53 75 62 20 3d 20  ursor;.  pSub = 
2bb9a 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63  pSubitem->pSelec
2bb9b 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75  t;.  assert( pSu
2bb9c 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73  b!=0 );.  if( is
2bb9d 41 67 67 20 26 26 20 73 75 62 71 75 65 72 79 49  Agg && subqueryI
2bb9e 73 41 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b  sAgg ) return 0;
2bb9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bba0 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2bba1 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20 73 75  (1)  */.  if( su
2bba2 62 71 75 65 72 79 49 73 41 67 67 20 26 26 20 70  bqueryIsAgg && p
2bba3 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65  Src->nSrc>1 ) re
2bba4 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
2bba5 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2bba6 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62 53 72  (2)  */.  pSubSr
2bba7 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a  c = pSub->pSrc;.
2bba8 20 20 61 73 73 65 72 74 28 20 70 53 75 62 53 72    assert( pSubSr
2bba9 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20  c );.  /* Prior 
2bbaa 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32  to version 3.1.2
2bbab 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64  , when LIMIT and
2bbac 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f 20 62   OFFSET had to b
2bbad 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e  e simple constan
2bbae 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62  ts,.  ** not arb
2bbaf 69 74 72 61 72 79 20 65 78 70 72 65 73 73 73 69  itrary expresssi
2bbb0 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20  ons, we allowed 
2bbb1 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f  some combining o
2bbb2 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  f LIMIT and OFFS
2bbb3 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20  ET.  ** because 
2bbb4 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20 63 6f  they could be co
2bbb5 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70 69 6c  mputed at compil
2bbb6 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77 68 65  e-time.  But whe
2bbb7 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  n LIMIT and OFFS
2bbb8 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65 20 61  ET.  ** became a
2bbb9 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73  rbitrary express
2bbba 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20 66 6f  ions, we were fo
2bbbb 72 63 65 64 20 74 6f 20 61 64 64 20 72 65 73 74  rced to add rest
2bbbc 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a 20 20  rictions (13).  
2bbbd 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a  ** and (14). */.
2bbbe 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d    if( pSub->pLim
2bbbf 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20  it && p->pLimit 
2bbc0 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
2bbc1 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2bbc2 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a  riction (13) */.
2bbc3 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 66 66    if( pSub->pOff
2bbc4 73 65 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  set ) return 0; 
2bbc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbc6 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2bbc7 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a  riction (14) */.
2bbc8 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d    if( p->pRightm
2bbc9 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70 4c 69  ost && pSub->pLi
2bbca 6d 69 74 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  mit && pSub->pOr
2bbcb 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74  derBy ){.    ret
2bbcc 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2bbcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbcf 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2bbd0 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69   (15) */.  }.  i
2bbd1 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63  f( pSubSrc->nSrc
2bbd2 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ==0 ) return 0; 
2bbd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbd4 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2bbd5 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69  tion (7)  */.  i
2bbd6 66 28 20 28 28 70 53 75 62 2d 3e 73 65 6c 46 6c  f( ((pSub->selFl
2bbd7 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
2bbd8 74 29 21 3d 30 20 7c 7c 20 70 53 75 62 2d 3e 70  t)!=0 || pSub->p
2bbd9 4c 69 6d 69 74 29 20 0a 20 20 20 20 20 20 20 20  Limit) .        
2bbda 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e   && (pSrc->nSrc>
2bbdb 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b 20 20  1 || isAgg) ){  
2bbdc 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2bbdd 69 63 74 69 6f 6e 73 20 28 34 29 28 35 29 28 38  ictions (4)(5)(8
2bbde 29 28 39 29 20 2a 2f 0a 20 20 20 20 20 72 65 74  )(9) */.     ret
2bbdf 75 72 6e 20 30 3b 20 20 20 20 20 20 20 0a 20 20  urn 0;       .  
2bbe0 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46  }.  if( (p->selF
2bbe1 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e  lags & SF_Distin
2bbe2 63 74 29 21 3d 30 20 26 26 20 73 75 62 71 75 65  ct)!=0 && subque
2bbe3 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20 20  ryIsAgg ){.     
2bbe4 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2bbe5 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2bbe6 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a 20 20 69   (6)  */.  }.  i
2bbe7 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 26  f( p->pOrderBy &
2bbe8 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  & pSub->pOrderBy
2bbe9 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20   ){.     return 
2bbea 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2bbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bbed 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 31 29  Restriction (11)
2bbee 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 69 73   */.  }.  if( is
2bbef 41 67 67 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  Agg && pSub->pOr
2bbf0 64 65 72 42 79 20 29 20 72 65 74 75 72 6e 20 30  derBy ) return 0
2bbf1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2bbf2 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2bbf3 28 31 36 29 20 2a 2f 0a 20 20 69 66 28 20 70 53  (16) */.  if( pS
2bbf4 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d  ub->pLimit && p-
2bbf5 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e  >pWhere ) return
2bbf6 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2bbf7 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2bbf8 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 42  (19) */..  /* OB
2bbf9 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 31  SOLETE COMMENT 1
2bbfa 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69  :.  ** Restricti
2bbfb 6f 6e 20 33 3a 20 20 49 66 20 74 68 65 20 73 75  on 3:  If the su
2bbfc 62 71 75 65 72 79 20 69 73 20 61 20 6a 6f 69 6e  bquery is a join
2bbfd 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  , make sure the 
2bbfe 73 75 62 71 75 65 72 79 20 69 73 20 0a 20 20 2a  subquery is .  *
2bbff 2a 20 6e 6f 74 20 75 73 65 64 20 61 73 20 74 68  * not used as th
2bc00 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
2bc01 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f 69 6e  of an outer join
2bc02 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 77  .  Examples of w
2bc03 68 79 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20  hy this.  ** is 
2bc04 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a  not allowed:.  *
2bc05 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74  *.  **         t
2bc06 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
2bc07 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29 0a 20  N (t2 JOIN t3). 
2bc08 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66   **.  ** If we f
2bc09 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65  latten the above
2bc0a 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20  , we would get. 
2bc0b 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
2bc0c 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20   (t1 LEFT OUTER 
2bc0d 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e 20 74 33  JOIN t2) JOIN t3
2bc0e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68 69 63 68  .  **.  ** which
2bc0f 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 74   is not at all t
2bc10 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 0a 20  he same thing.. 
2bc11 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f 4c 45 54   **.  ** OBSOLET
2bc12 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a 20 20 2a  E COMMENT 2:.  *
2bc13 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 32  * Restriction 12
2bc14 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65  :  If the subque
2bc15 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 20  ry is the right 
2bc16 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66  operand of a lef
2bc17 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20 6a 6f 69  t outer.  ** joi
2bc18 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  n, make sure the
2bc19 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6e 6f   subquery has no
2bc1a 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
2bc1b 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c 65 73 20   ** An examples 
2bc1c 6f 66 20 77 68 79 20 74 68 69 73 20 69 73 20 6e  of why this is n
2bc1d 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a  ot allowed:.  **
2bc1e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31  .  **         t1
2bc1f 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
2bc20 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
2bc21 74 32 20 57 48 45 52 45 20 74 32 2e 78 3e 30 29  t2 WHERE t2.x>0)
2bc22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65  .  **.  ** If we
2bc23 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f   flatten the abo
2bc24 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74  ve, we would get
2bc25 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2bc26 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45     (t1 LEFT OUTE
2bc27 52 20 4a 4f 49 4e 20 74 32 29 20 57 48 45 52 45  R JOIN t2) WHERE
2bc28 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a   t2.x>0.  **.  *
2bc29 2a 20 42 75 74 20 74 68 65 20 74 32 2e 78 3e 30  * But the t2.x>0
2bc2a 20 74 65 73 74 20 77 69 6c 6c 20 61 6c 77 61 79   test will alway
2bc2b 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c  s fail on a NULL
2bc2c 20 72 6f 77 20 6f 66 20 74 32 2c 20 77 68 69 63   row of t2, whic
2bc2d 68 0a 20 20 2a 2a 20 65 66 66 65 63 74 69 76 65  h.  ** effective
2bc2e 6c 79 20 63 6f 6e 76 65 72 74 73 20 74 68 65 20  ly converts the 
2bc2f 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e 74 6f 20  OUTER JOIN into 
2bc30 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20  an INNER JOIN.. 
2bc31 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53 20 4f 56   **.  ** THIS OV
2bc32 45 52 52 49 44 45 53 20 4f 42 53 4f 4c 45 54 45  ERRIDES OBSOLETE
2bc33 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41 4e 44 20   COMMENTS 1 AND 
2bc34 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a 20 54 69  2 ABOVE:.  ** Ti
2bc35 63 6b 65 74 20 23 33 33 30 30 20 73 68 6f 77 73  cket #3300 shows
2bc36 20 74 68 61 74 20 66 6c 61 74 74 65 6e 69 6e 67   that flattening
2bc37 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20   the right term 
2bc38 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 0a 20  of a LEFT JOIN. 
2bc39 20 2a 2a 20 69 73 20 66 72 61 75 67 68 74 20 77   ** is fraught w
2bc3a 69 74 68 20 64 61 6e 67 65 72 2e 20 20 42 65 73  ith danger.  Bes
2bc3b 74 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 77  t to avoid the w
2bc3c 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20 49 66 20  hole thing.  If 
2bc3d 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72  the.  ** subquer
2bc3e 79 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  y is the right t
2bc3f 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  erm of a LEFT JO
2bc40 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  IN, then do not 
2bc41 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20  flatten..  */.  
2bc42 69 66 28 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a  if( (pSubitem->j
2bc43 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55 54  ointype & JT_OUT
2bc44 45 52 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  ER)!=0 ){.    re
2bc45 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
2bc46 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 37  * Restriction 17
2bc47 3a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75 65  : If the sub-que
2bc48 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64  ry is a compound
2bc49 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 69 74   SELECT, then it
2bc4a 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73 65 20 6f   must.  ** use o
2bc4b 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e 20 41 4c  nly the UNION AL
2bc4c 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41 6e 64 20  L operator. And 
2bc4d 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 69 6d 70  none of the simp
2bc4e 6c 65 20 73 65 6c 65 63 74 20 71 75 65 72 69 65  le select querie
2bc4f 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d 61 6b 65  s.  ** that make
2bc50 20 75 70 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   up the compound
2bc51 20 53 45 4c 45 43 54 20 61 72 65 20 61 6c 6c 6f   SELECT are allo
2bc52 77 65 64 20 74 6f 20 62 65 20 61 67 67 72 65 67  wed to be aggreg
2bc53 61 74 65 20 6f 72 20 64 69 73 74 69 6e 63 74 0a  ate or distinct.
2bc54 20 20 2a 2a 20 71 75 65 72 69 65 73 2e 0a 20 20    ** queries..  
2bc55 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2bc56 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66 28  Prior ){.    if(
2bc57 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
2bc58 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2bc59 30 3b 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69  0;  /* Restricti
2bc5a 6f 6e 20 32 30 20 2a 2f 0a 20 20 20 20 7d 0a 20  on 20 */.    }. 
2bc5b 20 20 20 69 66 28 20 69 73 41 67 67 20 7c 7c 20     if( isAgg || 
2bc5c 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2bc5d 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20 7c  F_Distinct)!=0 |
2bc5e 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20  | pSrc->nSrc!=1 
2bc5f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2bc60 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  0;.    }.    for
2bc61 28 70 53 75 62 31 3d 70 53 75 62 3b 20 70 53 75  (pSub1=pSub; pSu
2bc62 62 31 3b 20 70 53 75 62 31 3d 70 53 75 62 31 2d  b1; pSub1=pSub1-
2bc63 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 20 20  >pPrior){.      
2bc64 74 65 73 74 63 61 73 65 28 20 28 70 53 75 62 31  testcase( (pSub1
2bc65 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
2bc66 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
2bc67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73  regate))==SF_Dis
2bc68 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 20 20 74  tinct );.      t
2bc69 65 73 74 63 61 73 65 28 20 28 70 53 75 62 31 2d  estcase( (pSub1-
2bc6a 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f  >selFlags & (SF_
2bc6b 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72  Distinct|SF_Aggr
2bc6c 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67 67 72  egate))==SF_Aggr
2bc6d 65 67 61 74 65 20 29 3b 0a 20 20 20 20 20 20 69  egate );.      i
2bc6e 66 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c  f( (pSub1->selFl
2bc6f 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
2bc70 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
2bc71 29 21 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 28  )!=0.       || (
2bc72 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 20 26 26  pSub1->pPrior &&
2bc73 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f 41   pSub1->op!=TK_A
2bc74 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 4e  LL) .       || N
2bc75 45 56 45 52 28 70 53 75 62 31 2d 3e 70 53 72 63  EVER(pSub1->pSrc
2bc76 3d 3d 30 29 20 7c 7c 20 70 53 75 62 31 2d 3e 70  ==0) || pSub1->p
2bc77 53 72 63 2d 3e 6e 53 72 63 21 3d 31 0a 20 20 20  Src->nSrc!=1.   
2bc78 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65     ){.        re
2bc79 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
2bc7a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
2bc7b 73 74 72 69 63 74 69 6f 6e 20 31 38 2e 20 2a 2f  striction 18. */
2bc7c 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64  .    if( p->pOrd
2bc7d 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69 6e  erBy ){.      in
2bc7e 74 20 69 69 3b 0a 20 20 20 20 20 20 66 6f 72 28  t ii;.      for(
2bc7f 69 69 3d 30 3b 20 69 69 3c 70 2d 3e 70 4f 72 64  ii=0; ii<p->pOrd
2bc80 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 69 2b  erBy->nExpr; ii+
2bc81 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2bc82 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69  p->pOrderBy->a[i
2bc83 69 5d 2e 69 43 6f 6c 3d 3d 30 20 29 20 72 65 74  i].iCol==0 ) ret
2bc84 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2bc85 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 2a 2a     }.  }..  /***
2bc86 2a 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74  ** If we reach t
2bc87 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c 61 74 74  his point, flatt
2bc88 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69 74 74  ening is permitt
2bc89 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a  ed. *****/..  /*
2bc8a 20 41 75 74 68 6f 72 69 7a 65 20 74 68 65 20 73   Authorize the s
2bc8b 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70 50 61  ubquery */.  pPa
2bc8c 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
2bc8d 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e  t = pSubitem->zN
2bc8e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33 41 75  ame;.  sqlite3Au
2bc8f 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2bc90 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30  SQLITE_SELECT, 0
2bc91 2c 20 30 2c 20 30 29 3b 0a 20 20 70 50 61 72 73  , 0, 0);.  pPars
2bc92 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  e->zAuthContext 
2bc93 3d 20 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74  = zSavedAuthCont
2bc94 65 78 74 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ext;..  /* If th
2bc95 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61  e sub-query is a
2bc96 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
2bc97 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
2bc98 20 28 62 79 20 72 65 73 74 72 69 63 74 69 6f 6e   (by restriction
2bc99 73 0a 20 20 2a 2a 20 31 37 20 61 6e 64 20 31 38  s.  ** 17 and 18
2bc9a 20 61 62 6f 76 65 29 20 69 74 20 6d 75 73 74 20   above) it must 
2bc9b 62 65 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61  be a UNION ALL a
2bc9c 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 71 75  nd the parent qu
2bc9d 65 72 79 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62  ery must .  ** b
2bc9e 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20  e of the form:. 
2bc9f 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c   **.  **     SEL
2bca0 45 43 54 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20  ECT <expr-list> 
2bca1 46 52 4f 4d 20 28 3c 73 75 62 2d 71 75 65 72 79  FROM (<sub-query
2bca2 3e 29 20 3c 77 68 65 72 65 2d 63 6c 61 75 73 65  >) <where-clause
2bca3 3e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c  > .  **.  ** fol
2bca4 6c 6f 77 65 64 20 62 79 20 61 6e 79 20 4f 52 44  lowed by any ORD
2bca5 45 52 20 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64  ER BY, LIMIT and
2bca6 2f 6f 72 20 4f 46 46 53 45 54 20 63 6c 61 75 73  /or OFFSET claus
2bca7 65 73 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20  es. This block. 
2bca8 20 2a 2a 20 63 72 65 61 74 65 73 20 4e 2d 31 20   ** creates N-1 
2bca9 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70 61  copies of the pa
2bcaa 72 65 6e 74 20 71 75 65 72 79 20 77 69 74 68 6f  rent query witho
2bcab 75 74 20 61 6e 79 20 4f 52 44 45 52 20 42 59 2c  ut any ORDER BY,
2bcac 20 4c 49 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20   LIMIT or .  ** 
2bcad 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 20 61  OFFSET clauses a
2bcae 6e 64 20 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f  nd joins them to
2bcaf 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73   the left-hand-s
2bcb0 69 64 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ide of the origi
2bcb1 6e 61 6c 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55  nal.  ** using U
2bcb2 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f  NION ALL operato
2bcb3 72 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  rs. In this case
2bcb4 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
2bcb5 20 6f 66 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20   of simple.  ** 
2bcb6 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
2bcb7 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e  s in the compoun
2bcb8 64 20 73 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a  d sub-query..  *
2bcb9 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a  *.  ** Example:.
2bcba 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45    **.  **     SE
2bcbb 4c 45 43 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a  LECT a+1 FROM (.
2bcbc 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45    **        SELE
2bcbd 43 54 20 78 20 46 52 4f 4d 20 74 61 62 0a 20 20  CT x FROM tab.  
2bcbe 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20  **        UNION 
2bcbf 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  ALL.  **        
2bcc0 53 45 4c 45 43 54 20 79 20 46 52 4f 4d 20 74 61  SELECT y FROM ta
2bcc1 62 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e  b.  **        UN
2bcc2 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20  ION ALL.  **    
2bcc3 20 20 20 20 53 45 4c 45 43 54 20 61 62 73 28 7a      SELECT abs(z
2bcc4 2a 32 29 20 46 52 4f 4d 20 74 61 62 32 0a 20 20  *2) FROM tab2.  
2bcc5 2a 2a 20 20 20 20 20 29 20 57 48 45 52 45 20 61  **     ) WHERE a
2bcc6 21 3d 35 20 4f 52 44 45 52 20 42 59 20 31 0a 20  !=5 ORDER BY 1. 
2bcc7 20 2a 2a 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f   **.  ** Transfo
2bcc8 72 6d 65 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a  rmed into:.  **.
2bcc9 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2bcca 78 2b 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45  x+1 FROM tab WHE
2bccb 52 45 20 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20  RE x+1!=5.  **  
2bccc 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a     UNION ALL.  *
2bccd 2a 20 20 20 20 20 53 45 4c 45 43 54 20 79 2b 31  *     SELECT y+1
2bcce 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20   FROM tab WHERE 
2bccf 79 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20  y+1!=5.  **     
2bcd0 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20  UNION ALL.  **  
2bcd1 20 20 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a     SELECT abs(z*
2bcd2 32 29 2b 31 20 46 52 4f 4d 20 74 61 62 32 20 57  2)+1 FROM tab2 W
2bcd3 48 45 52 45 20 61 62 73 28 7a 2a 32 29 2b 31 21  HERE abs(z*2)+1!
2bcd4 3d 35 0a 20 20 2a 2a 20 20 20 20 20 4f 52 44 45  =5.  **     ORDE
2bcd5 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a  R BY 1.  **.  **
2bcd6 20 57 65 20 63 61 6c 6c 20 74 68 69 73 20 74 68   We call this th
2bcd7 65 20 22 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71  e "compound-subq
2bcd8 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 22  uery flattening"
2bcd9 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75  ..  */.  for(pSu
2bcda 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20  b=pSub->pPrior; 
2bcdb 70 53 75 62 3b 20 70 53 75 62 3d 70 53 75 62 2d  pSub; pSub=pSub-
2bcdc 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 53 65  >pPrior){.    Se
2bcdd 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20  lect *pNew;.    
2bcde 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2bcdf 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79  By = p->pOrderBy
2bce0 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d  ;.    Expr *pLim
2bce1 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a  it = p->pLimit;.
2bce2 20 20 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69      Select *pPri
2bce3 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  or = p->pPrior;.
2bce4 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
2bce5 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63  = 0;.    p->pSrc
2bce6 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72   = 0;.    p->pPr
2bce7 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  ior = 0;.    p->
2bce8 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
2bce9 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53 65  pNew = sqlite3Se
2bcea 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2c 20 30  lectDup(db, p, 0
2bceb 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  );.    p->pLimit
2bcec 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70   = pLimit;.    p
2bced 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72  ->pOrderBy = pOr
2bcee 64 65 72 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53  derBy;.    p->pS
2bcef 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 20 20 70  rc = pSrc;.    p
2bcf0 2d 3e 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20  ->op = TK_ALL;. 
2bcf1 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
2bcf2 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 4e   = 0;.    if( pN
2bcf3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ew==0 ){.      p
2bcf4 4e 65 77 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20  New = pPrior;.  
2bcf5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2bcf6 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50  New->pPrior = pP
2bcf7 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 4e 65 77  rior;.      pNew
2bcf8 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
2bcf9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70  ;.    }.    p->p
2bcfa 50 72 69 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20  Prior = pNew;.  
2bcfb 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2bcfc 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20  Failed ) return 
2bcfd 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  1;.  }..  /* Beg
2bcfe 69 6e 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68  in flattening th
2bcff 65 20 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79  e iFrom-th entry
2bd00 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
2bd01 75 73 65 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65  use .  ** in the
2bd02 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20   outer query..  
2bd03 2a 2f 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62  */.  pSub = pSub
2bd04 31 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53  1 = pSubitem->pS
2bd05 65 6c 65 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c  elect;..  /* Del
2bd06 65 74 65 20 74 68 65 20 74 72 61 6e 73 69 65 6e  ete the transien
2bd07 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72  t table structur
2bd08 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
2bd09 68 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75  h the.  ** subqu
2bd0a 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ery.  */.  sqlit
2bd0b 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75  e3DbFree(db, pSu
2bd0c 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  bitem->zDatabase
2bd0d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2bd0e 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d  ee(db, pSubitem-
2bd0f 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
2bd10 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75  e3DbFree(db, pSu
2bd11 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  bitem->zAlias);.
2bd12 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74    pSubitem->zDat
2bd13 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 70 53 75  abase = 0;.  pSu
2bd14 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30  bitem->zName = 0
2bd15 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41  ;.  pSubitem->zA
2bd16 6c 69 61 73 20 3d 20 30 3b 0a 20 20 70 53 75 62  lias = 0;.  pSub
2bd17 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20  item->pSelect = 
2bd18 30 3b 0a 0a 20 20 2f 2a 20 44 65 66 65 72 20 64  0;..  /* Defer d
2bd19 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c  eleting the Tabl
2bd1a 65 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61  e object associa
2bd1b 74 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a  ted with the.  *
2bd1c 2a 20 73 75 62 71 75 65 72 79 20 75 6e 74 69 6c  * subquery until
2bd1d 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e   code generation
2bd1e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74   is.  ** complet
2bd1f 65 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 6d  e, since there m
2bd20 61 79 20 73 74 69 6c 6c 20 65 78 69 73 74 20 45  ay still exist E
2bd21 78 70 72 2e 70 54 61 62 20 65 6e 74 72 69 65 73  xpr.pTab entries
2bd22 20 74 68 61 74 0a 20 20 2a 2a 20 72 65 66 65 72   that.  ** refer
2bd23 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72 79   to the subquery
2bd24 20 65 76 65 6e 20 61 66 74 65 72 20 66 6c 61 74   even after flat
2bd25 74 65 6e 69 6e 67 2e 20 20 54 69 63 6b 65 74 20  tening.  Ticket 
2bd26 23 33 33 34 36 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  #3346..  **.  **
2bd27 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20   pSubitem->pTab 
2bd28 69 73 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55  is always non-NU
2bd29 4c 4c 20 62 79 20 74 65 73 74 20 72 65 73 74 72  LL by test restr
2bd2a 69 63 74 69 6f 6e 73 20 61 6e 64 20 74 65 73 74  ictions and test
2bd2b 73 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20  s above..  */.  
2bd2c 69 66 28 20 41 4c 57 41 59 53 28 70 53 75 62 69  if( ALWAYS(pSubi
2bd2d 74 65 6d 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b  tem->pTab!=0) ){
2bd2e 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
2bd2f 54 6f 44 65 6c 20 3d 20 70 53 75 62 69 74 65 6d  ToDel = pSubitem
2bd30 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20  ->pTab;.    if( 
2bd31 70 54 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d  pTabToDel->nRef=
2bd32 3d 31 20 29 7b 0a 20 20 20 20 20 20 50 61 72 73  =1 ){.      Pars
2bd33 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
2bd34 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2bd35 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20  vel(pParse);.   
2bd36 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e     pTabToDel->pN
2bd37 65 78 74 5a 6f 6d 62 69 65 20 3d 20 70 54 6f 70  extZombie = pTop
2bd38 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61  level->pZombieTa
2bd39 62 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76  b;.      pToplev
2bd3a 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d  el->pZombieTab =
2bd3b 20 70 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20   pTabToDel;.    
2bd3c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61  }else{.      pTa
2bd3d 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a  bToDel->nRef--;.
2bd3e 20 20 20 20 7d 0a 20 20 20 20 70 53 75 62 69 74      }.    pSubit
2bd3f 65 6d 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20  em->pTab = 0;.  
2bd40 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  }..  /* The foll
2bd41 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20  owing loop runs 
2bd42 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65  once for each te
2bd43 72 6d 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  rm in a compound
2bd44 2d 73 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 66  -subquery.  ** f
2bd45 6c 61 74 74 65 6e 69 6e 67 20 28 61 73 20 64 65  lattening (as de
2bd46 73 63 72 69 62 65 64 20 61 62 6f 76 65 29 2e 20  scribed above). 
2bd47 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67   If we are doing
2bd48 20 61 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e   a different kin
2bd49 64 0a 20 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65  d.  ** of flatte
2bd4a 6e 69 6e 67 20 2d 20 61 20 66 6c 61 74 74 65 6e  ning - a flatten
2bd4b 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ing other than a
2bd4c 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65   compound-subque
2bd4d 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a  ry flattening -.
2bd4e 20 20 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 6c    ** then this l
2bd4f 6f 6f 70 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e  oop only runs on
2bd50 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ce..  **.  ** Th
2bd51 69 73 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c  is loop moves al
2bd52 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 65 6c  l of the FROM el
2bd53 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73 75  ements of the su
2bd54 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a  bquery into the.
2bd55 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c    ** the FROM cl
2bd56 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65  ause of the oute
2bd57 72 20 71 75 65 72 79 2e 20 20 42 65 66 6f 72 65  r query.  Before
2bd58 20 64 6f 69 6e 67 20 74 68 69 73 2c 20 72 65 6d   doing this, rem
2bd59 65 6d 62 65 72 0a 20 20 2a 2a 20 74 68 65 20 63  ember.  ** the c
2bd5a 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
2bd5b 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75   the original ou
2bd5c 74 65 72 20 71 75 65 72 79 20 46 52 4f 4d 20 65  ter query FROM e
2bd5d 6c 65 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69  lement in.  ** i
2bd5e 50 61 72 65 6e 74 2e 20 20 54 68 65 20 69 50 61  Parent.  The iPa
2bd5f 72 65 6e 74 20 63 75 72 73 6f 72 20 77 69 6c 6c  rent cursor will
2bd60 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20   never be used. 
2bd61 20 53 75 62 73 65 71 75 65 6e 74 20 63 6f 64 65   Subsequent code
2bd62 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20  .  ** will scan 
2bd63 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b  expressions look
2bd64 69 6e 67 20 66 6f 72 20 69 50 61 72 65 6e 74 20  ing for iParent 
2bd65 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64 20 72  references and r
2bd66 65 70 6c 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73  eplace.  ** thos
2bd67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 69 74  e references wit
2bd68 68 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  h expressions th
2bd69 61 74 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  at resolve to th
2bd6a 65 20 73 75 62 71 75 65 72 79 20 46 52 4f 4d 0a  e subquery FROM.
2bd6b 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65    ** elements we
2bd6c 20 61 72 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67   are now copying
2bd6d 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28   in..  */.  for(
2bd6e 70 50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72 65  pParent=p; pPare
2bd6f 6e 74 3b 20 70 50 61 72 65 6e 74 3d 70 50 61 72  nt; pParent=pPar
2bd70 65 6e 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75  ent->pPrior, pSu
2bd71 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b  b=pSub->pPrior){
2bd72 0a 20 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63  .    int nSubSrc
2bd73 3b 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70  ;.    u8 jointyp
2bd74 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75 62 53  e = 0;.    pSubS
2bd75 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b  rc = pSub->pSrc;
2bd76 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
2bd77 75 73 65 20 6f 66 20 73 75 62 71 75 65 72 79 20  use of subquery 
2bd78 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72 63 20 3d  */.    nSubSrc =
2bd79 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20   pSubSrc->nSrc; 
2bd7a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
2bd7b 72 6d 73 20 69 6e 20 73 75 62 71 75 65 72 79 20  rms in subquery 
2bd7c 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2bd7d 20 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e     pSrc = pParen
2bd7e 74 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20  t->pSrc;     /* 
2bd7f 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
2bd80 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a  he outer query *
2bd81 2f 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 20  /..    if( pSrc 
2bd82 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2bd83 20 70 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20   pParent==p );  
2bd84 2f 2a 20 46 69 72 73 74 20 74 69 6d 65 20 74 68  /* First time th
2bd85 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a  rough the loop *
2bd86 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65  /.      jointype
2bd87 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69   = pSubitem->joi
2bd88 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65  ntype;.    }else
2bd89 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2bd8a 70 50 61 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f  pParent!=p );  /
2bd8b 2a 20 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71  * 2nd and subseq
2bd8c 75 65 6e 74 20 74 69 6d 65 73 20 74 68 72 6f 75  uent times throu
2bd8d 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  gh the loop */. 
2bd8e 20 20 20 20 20 70 53 72 63 20 3d 20 70 50 61 72       pSrc = pPar
2bd8f 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69  ent->pSrc = sqli
2bd90 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2bd91 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  (db, 0, 0, 0);. 
2bd92 20 20 20 20 20 69 66 28 20 70 53 72 63 3d 3d 30       if( pSrc==0
2bd93 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
2bd94 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
2bd95 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  iled );.        
2bd96 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2bd97 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
2bd98 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61   subquery uses a
2bd99 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20   single slot of 
2bd9a 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2bd9b 6f 66 20 74 68 65 20 6f 75 74 65 72 0a 20 20 20  of the outer.   
2bd9c 20 2a 2a 20 71 75 65 72 79 2e 20 20 49 66 20 74   ** query.  If t
2bd9d 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20  he subquery has 
2bd9e 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c  more than one el
2bd9f 65 6d 65 6e 74 20 69 6e 20 69 74 73 20 46 52 4f  ement in its FRO
2bda0 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a  M clause,.    **
2bda1 20 74 68 65 6e 20 65 78 70 61 6e 64 20 74 68 65   then expand the
2bda2 20 6f 75 74 65 72 20 71 75 65 72 79 20 74 6f 20   outer query to 
2bda3 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72 20 69  make space for i
2bda4 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c  t to hold all el
2bda5 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66  ements.    ** of
2bda6 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20   the subquery.. 
2bda7 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61     **.    ** Exa
2bda8 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2bda9 20 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20   **    SELECT * 
2bdaa 46 52 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c 45  FROM tabA, (SELE
2bdab 43 54 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20  CT * FROM sub1, 
2bdac 73 75 62 32 29 2c 20 74 61 62 42 3b 0a 20 20 20  sub2), tabB;.   
2bdad 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f   **.    ** The o
2bdae 75 74 65 72 20 71 75 65 72 79 20 68 61 73 20 33  uter query has 3
2bdaf 20 73 6c 6f 74 73 20 69 6e 20 69 74 73 20 46 52   slots in its FR
2bdb0 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20  OM clause.  One 
2bdb1 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20 20 20 20  slot of the.    
2bdb2 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 28  ** outer query (
2bdb3 74 68 65 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29  the middle slot)
2bdb4 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
2bdb5 73 75 62 71 75 65 72 79 2e 20 20 54 68 65 20 6e  subquery.  The n
2bdb6 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b  ext.    ** block
2bdb7 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 65 78   of code will ex
2bdb8 70 61 6e 64 20 74 68 65 20 6f 75 74 20 71 75 65  pand the out que
2bdb9 72 79 20 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20  ry to 4 slots.  
2bdba 54 68 65 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a  The middle.    *
2bdbb 2a 20 73 6c 6f 74 20 69 73 20 65 78 70 61 6e 64  * slot is expand
2bdbc 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20  ed to two slots 
2bdbd 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65  in order to make
2bdbe 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 0a 20   space for the. 
2bdbf 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e     ** two elemen
2bdc0 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ts in the FROM c
2bdc1 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62  lause of the sub
2bdc2 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  query..    */.  
2bdc3 20 20 69 66 28 20 6e 53 75 62 53 72 63 3e 31 20    if( nSubSrc>1 
2bdc4 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  ){.      pParent
2bdc5 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 20 3d 20  ->pSrc = pSrc = 
2bdc6 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
2bdc7 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63 2c 20  large(db, pSrc, 
2bdc8 6e 53 75 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b  nSubSrc-1,iFrom+
2bdc9 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62  1);.      if( db
2bdca 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2bdcb 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
2bdcc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2bdcd 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20      /* Transfer 
2bdce 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2bdcf 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 73  terms from the s
2bdd0 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65  ubquery into the
2bdd1 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
2bdd2 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ery..    */.    
2bdd3 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53  for(i=0; i<nSubS
2bdd4 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rc; i++){.      
2bdd5 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
2bdd6 65 74 65 28 64 62 2c 20 70 53 72 63 2d 3e 61 5b  ete(db, pSrc->a[
2bdd7 69 2b 69 46 72 6f 6d 5d 2e 70 55 73 69 6e 67 29  i+iFrom].pUsing)
2bdd8 3b 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b  ;.      pSrc->a[
2bdd9 69 2b 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53  i+iFrom] = pSubS
2bdda 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20  rc->a[i];.      
2bddb 6d 65 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d  memset(&pSubSrc-
2bddc 3e 61 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66  >a[i], 0, sizeof
2bddd 28 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29  (pSubSrc->a[i]))
2bdde 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63  ;.    }.    pSrc
2bddf 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74  ->a[iFrom].joint
2bde0 79 70 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a  ype = jointype;.
2bde1 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65    .    /* Now be
2bde2 67 69 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67  gin substituting
2bde3 20 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74   subquery result
2bde4 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73   set expressions
2bde5 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66   for .    ** ref
2bde6 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 69  erences to the i
2bde7 50 61 72 65 6e 74 20 69 6e 20 74 68 65 20 6f 75  Parent in the ou
2bde8 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a  ter query..    *
2bde9 2a 20 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c  * .    ** Exampl
2bdea 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
2bdeb 20 20 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62     SELECT a+5, b
2bdec 2a 31 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  *10 FROM (SELECT
2bded 20 78 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20   x*3 AS a, y+10 
2bdee 41 53 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48  AS b FROM t1) WH
2bdef 45 52 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20  ERE a>b;.    ** 
2bdf0 20 20 5c 20 20 20 20 20 20 20 20 20 20 20 20 20    \             
2bdf1 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f          \_______
2bdf2 5f 5f 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20  ______ subquery 
2bdf3 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20  __________/     
2bdf4 20 20 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20       /.    **   
2bdf5 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
2bdf6 5f 5f 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75  _______ outer qu
2bdf7 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ery ____________
2bdf8 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2bdf9 5f 5f 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  __/.    **.    *
2bdfa 2a 20 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65  * We look at eve
2bdfb 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
2bdfc 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2bdfd 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65   and every place
2bdfe 20 77 65 20 73 65 65 0a 20 20 20 20 2a 2a 20 22   we see.    ** "
2bdff 61 22 20 77 65 20 73 75 62 73 74 69 74 75 74 65  a" we substitute
2be00 20 22 78 2a 33 22 20 61 6e 64 20 65 76 65 72 79   "x*3" and every
2be01 20 70 6c 61 63 65 20 77 65 20 73 65 65 20 22 62   place we see "b
2be02 22 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20  " we substitute 
2be03 22 79 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20  "y+10"..    */. 
2be04 20 20 20 70 4c 69 73 74 20 3d 20 70 50 61 72 65     pList = pPare
2be05 6e 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20  nt->pEList;.    
2be06 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
2be07 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2be08 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e       if( pList->
2be09 61 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 7b  a[i].zName==0 ){
2be0a 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
2be0b 68 61 72 20 2a 7a 53 70 61 6e 20 3d 20 70 4c 69  har *zSpan = pLi
2be0c 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 3b 0a  st->a[i].zSpan;.
2be0d 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41          if( ALWA
2be0e 59 53 28 7a 53 70 61 6e 29 20 29 7b 0a 20 20 20  YS(zSpan) ){.   
2be0f 20 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b         pList->a[
2be10 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  i].zName = sqlit
2be11 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
2be12 53 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d  Span);.        }
2be13 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2be14 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74     substExprList
2be15 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45  (db, pParent->pE
2be16 4c 69 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70  List, iParent, p
2be17 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2be18 20 20 69 66 28 20 69 73 41 67 67 20 29 7b 0a 20    if( isAgg ){. 
2be19 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69       substExprLi
2be1a 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  st(db, pParent->
2be1b 70 47 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e  pGroupBy, iParen
2be1c 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29  t, pSub->pEList)
2be1d 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
2be1e 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74  >pHaving = subst
2be1f 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74  Expr(db, pParent
2be20 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65  ->pHaving, iPare
2be21 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
2be22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2be23 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
2be24 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2be25 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
2be26 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  By==0 );.      p
2be27 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79  Parent->pOrderBy
2be28 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42   = pSub->pOrderB
2be29 79 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70  y;.      pSub->p
2be2a 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
2be2b 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 65   }else if( pPare
2be2c 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  nt->pOrderBy ){.
2be2d 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c        substExprL
2be2e 69 73 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  ist(db, pParent-
2be2f 3e 70 4f 72 64 65 72 42 79 2c 20 69 50 61 72 65  >pOrderBy, iPare
2be30 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
2be31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2be32 20 70 53 75 62 2d 3e 70 57 68 65 72 65 20 29 7b   pSub->pWhere ){
2be33 0a 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20  .      pWhere = 
2be34 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2be35 62 2c 20 70 53 75 62 2d 3e 70 57 68 65 72 65 2c  b, pSub->pWhere,
2be36 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
2be37 20 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 30        pWhere = 0
2be38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2be39 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29 7b  subqueryIsAgg ){
2be3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2be3b 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d  Parent->pHaving=
2be3c 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
2be3d 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70  ent->pHaving = p
2be3e 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a  Parent->pWhere;.
2be3f 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2be40 57 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a  Where = pWhere;.
2be41 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2be42 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78  Having = substEx
2be43 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  pr(db, pParent->
2be44 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74  pHaving, iParent
2be45 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
2be46 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2be47 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65  pHaving = sqlite
2be48 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61  3ExprAnd(db, pPa
2be49 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 0a  rent->pHaving, .
2be4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be4c 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70    sqlite3ExprDup
2be4d 28 64 62 2c 20 70 53 75 62 2d 3e 70 48 61 76 69  (db, pSub->pHavi
2be4e 6e 67 2c 20 30 29 29 3b 0a 20 20 20 20 20 20 61  ng, 0));.      a
2be4f 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
2be50 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20  pGroupBy==0 );. 
2be51 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 47       pParent->pG
2be52 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33  roupBy = sqlite3
2be53 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
2be54 70 53 75 62 2d 3e 70 47 72 6f 75 70 42 79 2c 20  pSub->pGroupBy, 
2be55 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0);.    }else{. 
2be56 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57       pParent->pW
2be57 68 65 72 65 20 3d 20 73 75 62 73 74 45 78 70 72  here = substExpr
2be58 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57  (db, pParent->pW
2be59 68 65 72 65 2c 20 69 50 61 72 65 6e 74 2c 20 70  here, iParent, p
2be5a 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2be5b 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68      pParent->pWh
2be5c 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2be5d 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74  rAnd(db, pParent
2be5e 2d 3e 70 57 68 65 72 65 2c 20 70 57 68 65 72 65  ->pWhere, pWhere
2be5f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
2be60 2f 2a 20 54 68 65 20 66 6c 61 74 74 65 6e 65 64  /* The flattened
2be61 20 71 75 65 72 79 20 69 73 20 64 69 73 74 69 6e   query is distin
2be62 63 74 20 69 66 20 65 69 74 68 65 72 20 74 68 65  ct if either the
2be63 20 69 6e 6e 65 72 20 6f 72 20 74 68 65 0a 20 20   inner or the.  
2be64 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79    ** outer query
2be65 20 69 73 20 64 69 73 74 69 6e 63 74 2e 20 0a 20   is distinct. . 
2be66 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 72 65 6e     */.    pParen
2be67 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 70  t->selFlags |= p
2be68 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  Sub->selFlags & 
2be69 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 0a  SF_Distinct;.  .
2be6a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 45      /*.    ** SE
2be6b 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 28 53  LECT ... FROM (S
2be6c 45 4c 45 43 54 20 2e 2e 2e 20 4c 49 4d 49 54 20  ELECT ... LIMIT 
2be6d 61 20 4f 46 46 53 45 54 20 62 29 20 4c 49 4d 49  a OFFSET b) LIMI
2be6e 54 20 78 20 4f 46 46 53 45 54 20 79 3b 0a 20 20  T x OFFSET y;.  
2be6f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 65 20    **.    ** One 
2be70 69 73 20 74 65 6d 70 74 65 64 20 74 6f 20 74 72  is tempted to tr
2be71 79 20 74 6f 20 61 64 64 20 61 20 61 6e 64 20 62  y to add a and b
2be72 20 74 6f 20 63 6f 6d 62 69 6e 65 20 74 68 65 20   to combine the 
2be73 6c 69 6d 69 74 73 2e 20 20 42 75 74 20 74 68 69  limits.  But thi
2be74 73 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  s.    ** does no
2be75 74 20 77 6f 72 6b 20 69 66 20 65 69 74 68 65 72  t work if either
2be76 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61 74 69   limit is negati
2be77 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ve..    */.    i
2be78 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
2be79 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  ){.      pParent
2be7a 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d  ->pLimit = pSub-
2be7b 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70  >pLimit;.      p
2be7c 53 75 62 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b  Sub->pLimit = 0;
2be7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2be7e 20 46 69 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74   Finially, delet
2be7f 65 20 77 68 61 74 20 69 73 20 6c 65 66 74 20 6f  e what is left o
2be80 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 61  f the subquery a
2be81 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73  nd return.  ** s
2be82 75 63 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73  uccess..  */.  s
2be83 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2be84 74 65 28 64 62 2c 20 70 53 75 62 31 29 3b 0a 0a  te(db, pSub1);..
2be85 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
2be86 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
2be87 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
2be88 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e  QUERY) || !defin
2be89 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2be8a 49 45 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  IEW) */../*.** A
2be8b 6e 61 6c 79 7a 65 20 74 68 65 20 53 45 4c 45 43  nalyze the SELEC
2be8c 54 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73  T statement pass
2be8d 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
2be8e 74 20 74 6f 20 73 65 65 20 69 66 20 69 74 0a 2a  t to see if it.*
2be8f 2a 20 69 73 20 61 20 6d 69 6e 28 29 20 6f 72 20  * is a min() or 
2be90 6d 61 78 28 29 20 71 75 65 72 79 2e 20 52 65 74  max() query. Ret
2be91 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
2be92 59 5f 4d 49 4e 20 6f 72 20 57 48 45 52 45 5f 4f  Y_MIN or WHERE_O
2be93 52 44 45 52 42 59 5f 4d 41 58 20 69 66 20 0a 2a  RDERBY_MAX if .*
2be94 2a 20 69 74 20 69 73 2c 20 6f 72 20 30 20 6f 74  * it is, or 0 ot
2be95 68 65 72 77 69 73 65 2e 20 41 74 20 70 72 65 73  herwise. At pres
2be96 65 6e 74 2c 20 61 20 71 75 65 72 79 20 69 73 20  ent, a query is 
2be97 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65  considered to be
2be98 0a 2a 2a 20 61 20 6d 69 6e 28 29 2f 6d 61 78 28  .** a min()/max(
2be99 29 20 71 75 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a  ) query if:.**.*
2be9a 2a 20 20 20 31 2e 20 54 68 65 72 65 20 69 73 20  *   1. There is 
2be9b 61 20 73 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20  a single object 
2be9c 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2be9d 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54  se..**.**   2. T
2be9e 68 65 72 65 20 69 73 20 61 20 73 69 6e 67 6c 65  here is a single
2be9f 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
2bea0 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 61  he result set, a
2bea1 6e 64 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  nd it is.**     
2bea2 20 65 69 74 68 65 72 20 6d 69 6e 28 78 29 20 6f   either min(x) o
2bea3 72 20 6d 61 78 28 78 29 2c 20 77 68 65 72 65 20  r max(x), where 
2bea4 78 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 72 65  x is a column re
2bea5 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  ference..*/.stat
2bea6 69 63 20 75 38 20 6d 69 6e 4d 61 78 51 75 65 72  ic u8 minMaxQuer
2bea7 79 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  y(Select *p){.  
2bea8 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 45  Expr *pExpr;.  E
2bea9 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2beaa 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20  = p->pEList;..  
2beab 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  if( pEList->nExp
2beac 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20 57 48  r!=1 ) return WH
2bead 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d  ERE_ORDERBY_NORM
2beae 41 4c 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 45  AL;.  pExpr = pE
2beaf 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2beb0 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
2beb1 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p!=TK_AGG_FUNCTI
2beb2 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  ON ) return 0;. 
2beb3 20 69 66 28 20 4e 45 56 45 52 28 45 78 70 72 48   if( NEVER(ExprH
2beb4 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2beb5 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 29  , EP_xIsSelect))
2beb6 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
2beb7 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  EList = pExpr->x
2beb8 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45  .pList;.  if( pE
2beb9 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73  List==0 || pELis
2beba 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65  t->nExpr!=1 ) re
2bebb 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45  turn 0;.  if( pE
2bebc 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2bebd 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c  ->op!=TK_AGG_COL
2bebe 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 57 48 45  UMN ) return WHE
2bebf 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
2bec0 4c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78  L;.  assert( !Ex
2bec1 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2bec2 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
2bec3 29 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ) );.  if( sqlit
2bec4 65 33 53 74 72 49 43 6d 70 28 70 45 78 70 72 2d  e3StrICmp(pExpr-
2bec5 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 69 6e 22 29  >u.zToken,"min")
2bec6 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2bec7 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
2bec8 4d 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  MIN;.  }else if(
2bec9 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2beca 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  pExpr->u.zToken,
2becb 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0a 20 20 20  "max")==0 ){.   
2becc 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   return WHERE_OR
2becd 44 45 52 42 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20  DERBY_MAX;.  }. 
2bece 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   return WHERE_OR
2becf 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a  DERBY_NORMAL;.}.
2bed0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 6c 65 63  ./*.** The selec
2bed1 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73  t statement pass
2bed2 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
2bed3 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 61  argument is an a
2bed4 67 67 72 65 67 61 74 65 20 71 75 65 72 79 2e 0a  ggregate query..
2bed5 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
2bed6 67 6d 65 6e 74 20 69 73 20 74 68 65 20 61 73 73  gment is the ass
2bed7 6f 63 69 61 74 65 64 20 61 67 67 72 65 67 61 74  ociated aggregat
2bed8 65 2d 69 6e 66 6f 20 6f 62 6a 65 63 74 2e 20 54  e-info object. T
2bed9 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
2beda 20 74 65 73 74 73 20 69 66 20 74 68 65 20 53 45   tests if the SE
2bedb 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 66  LECT is of the f
2bedc 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c  orm:.**.**   SEL
2bedd 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f  ECT count(*) FRO
2bede 4d 20 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68  M <tbl>.**.** wh
2bedf 65 72 65 20 74 61 62 6c 65 20 69 73 20 61 20 64  ere table is a d
2bee0 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 6e  atabase table, n
2bee1 6f 74 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20  ot a sub-select 
2bee2 6f 72 20 76 69 65 77 2e 20 49 66 20 74 68 65 20  or view. If the 
2bee3 71 75 65 72 79 0a 2a 2a 20 64 6f 65 73 20 6d 61  query.** does ma
2bee4 74 63 68 20 74 68 69 73 20 70 61 74 74 65 72 6e  tch this pattern
2bee5 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72  , then a pointer
2bee6 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 6f 62   to the Table ob
2bee7 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 69 6e  ject representin
2bee8 67 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73 20 72 65  g.** <tbl> is re
2bee9 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
2beea 65 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  e, 0 is returned
2beeb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c  ..*/.static Tabl
2beec 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74  e *isSimpleCount
2beed 28 53 65 6c 65 63 74 20 2a 70 2c 20 41 67 67 49  (Select *p, AggI
2beee 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a  nfo *pAggInfo){.
2beef 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
2bef0 20 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 0a 20   Expr *pExpr;.. 
2bef1 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 47 72   assert( !p->pGr
2bef2 6f 75 70 42 79 20 29 3b 0a 0a 20 20 69 66 28 20  oupBy );..  if( 
2bef3 70 2d 3e 70 57 68 65 72 65 20 7c 7c 20 70 2d 3e  p->pWhere || p->
2bef4 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
2bef5 20 0a 20 20 20 7c 7c 20 70 2d 3e 70 53 72 63 2d   .   || p->pSrc-
2bef6 3e 6e 53 72 63 21 3d 31 20 7c 7c 20 70 2d 3e 70  >nSrc!=1 || p->p
2bef7 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63  Src->a[0].pSelec
2bef8 74 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  t.  ){.    retur
2bef9 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20  n 0;.  }.  pTab 
2befa 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e  = p->pSrc->a[0].
2befb 70 54 61 62 3b 0a 20 20 70 45 78 70 72 20 3d 20  pTab;.  pExpr = 
2befc 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  p->pEList->a[0].
2befd 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28  pExpr;.  assert(
2befe 20 70 54 61 62 20 26 26 20 21 70 54 61 62 2d 3e   pTab && !pTab->
2beff 70 53 65 6c 65 63 74 20 26 26 20 70 45 78 70 72  pSelect && pExpr
2bf00 20 29 3b 0a 0a 20 20 69 66 28 20 49 73 56 69 72   );..  if( IsVir
2bf01 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74  tual(pTab) ) ret
2bf02 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78  urn 0;.  if( pEx
2bf03 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46  pr->op!=TK_AGG_F
2bf04 55 4e 43 54 49 4f 4e 20 29 20 72 65 74 75 72 6e  UNCTION ) return
2bf05 20 30 3b 0a 20 20 69 66 28 20 28 70 41 67 67 49   0;.  if( (pAggI
2bf06 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 2e 70 46  nfo->aFunc[0].pF
2bf07 75 6e 63 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  unc->flags&SQLIT
2bf08 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29 3d 3d 30  E_FUNC_COUNT)==0
2bf09 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2bf0a 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26  f( pExpr->flags&
2bf0b 45 50 5f 44 69 73 74 69 6e 63 74 20 29 20 72 65  EP_Distinct ) re
2bf0c 74 75 72 6e 20 30 3b 0a 0a 20 20 72 65 74 75 72  turn 0;..  retur
2bf0d 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pTab;.}../*.**
2bf0e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 2d 6c   If the source-l
2bf0f 69 73 74 20 69 74 65 6d 20 70 61 73 73 65 64 20  ist item passed 
2bf10 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 77  as an argument w
2bf11 61 73 20 61 75 67 6d 65 6e 74 65 64 20 77 69 74  as augmented wit
2bf12 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58 45 44 20  h an.** INDEXED 
2bf13 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20  BY clause, then 
2bf14 74 72 79 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  try to locate th
2bf15 65 20 73 70 65 63 69 66 69 65 64 20 69 6e 64 65  e specified inde
2bf16 78 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 77  x. If there.** w
2bf17 61 73 20 73 75 63 68 20 61 20 63 6c 61 75 73 65  as such a clause
2bf18 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 69   and the named i
2bf19 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 66  ndex cannot be f
2bf1a 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 0a 2a 2a  ound, return .**
2bf1b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 61 6e   SQLITE_ERROR an
2bf1c 64 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72  d leave an error
2bf1d 20 69 6e 20 70 50 61 72 73 65 2e 20 4f 74 68 65   in pParse. Othe
2bf1e 72 77 69 73 65 2c 20 70 6f 70 75 6c 61 74 65 20  rwise, populate 
2bf1f 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65  .** pFrom->pInde
2bf20 78 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  x and return SQL
2bf21 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
2bf22 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2bf23 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f  lite3IndexedByLo
2bf24 6f 6b 75 70 28 50 61 72 73 65 20 2a 70 50 61 72  okup(Parse *pPar
2bf25 73 65 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69  se, struct SrcLi
2bf26 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 29 7b  st_item *pFrom){
2bf27 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54  .  if( pFrom->pT
2bf28 61 62 20 26 26 20 70 46 72 6f 6d 2d 3e 7a 49 6e  ab && pFrom->zIn
2bf29 64 65 78 20 29 7b 0a 20 20 20 20 54 61 62 6c 65  dex ){.    Table
2bf2a 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e   *pTab = pFrom->
2bf2b 70 54 61 62 3b 0a 20 20 20 20 63 68 61 72 20 2a  pTab;.    char *
2bf2c 7a 49 6e 64 65 78 20 3d 20 70 46 72 6f 6d 2d 3e  zIndex = pFrom->
2bf2d 7a 49 6e 64 65 78 3b 0a 20 20 20 20 49 6e 64 65  zIndex;.    Inde
2bf2e 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72  x *pIdx;.    for
2bf2f 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2bf30 65 78 3b 20 0a 20 20 20 20 20 20 20 20 70 49 64  ex; .        pId
2bf31 78 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  x && sqlite3StrI
2bf32 43 6d 70 28 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c  Cmp(pIdx->zName,
2bf33 20 7a 49 6e 64 65 78 29 3b 20 0a 20 20 20 20 20   zIndex); .     
2bf34 20 20 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e     pIdx=pIdx->pN
2bf35 65 78 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  ext.    );.    i
2bf36 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20 20 20  f( !pIdx ){.    
2bf37 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2bf38 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
2bf39 63 68 20 69 6e 64 65 78 3a 20 25 73 22 2c 20 7a  ch index: %s", z
2bf3a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 20 20 20 20  Index, 0);.     
2bf3b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2bf3c 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RROR;.    }.    
2bf3d 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 3d 20  pFrom->pIndex = 
2bf3e 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75  pIdx;.  }.  retu
2bf3f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2bf40 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2bf41 69 6e 65 20 69 73 20 61 20 57 61 6c 6b 65 72 20  ine is a Walker 
2bf42 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 22 65 78  callback for "ex
2bf43 70 61 6e 64 69 6e 67 22 20 61 20 53 45 4c 45 43  panding" a SELEC
2bf44 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
2bf45 22 45 78 70 61 6e 64 69 6e 67 22 20 6d 65 61 6e  "Expanding" mean
2bf46 73 20 74 6f 20 64 6f 20 74 68 65 20 66 6f 6c 6c  s to do the foll
2bf47 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
2bf48 28 31 29 20 20 4d 61 6b 65 20 73 75 72 65 20 56  (1)  Make sure V
2bf49 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2bf4a 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 73 73  rs have been ass
2bf4b 69 67 6e 65 64 20 74 6f 20 65 76 65 72 79 0a 2a  igned to every.*
2bf4c 2a 20 20 20 20 20 20 20 20 20 65 6c 65 6d 65 6e  *         elemen
2bf4d 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  t of the FROM cl
2bf4e 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ause..**.**    (
2bf4f 32 29 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20  2)  Fill in the 
2bf50 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54  pTabList->a[].pT
2bf51 61 62 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  ab fields in the
2bf52 20 53 72 63 4c 69 73 74 20 74 68 61 74 20 0a 2a   SrcList that .*
2bf53 2a 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65  *         define
2bf54 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  s FROM clause.  
2bf55 57 68 65 6e 20 76 69 65 77 73 20 61 70 70 65 61  When views appea
2bf56 72 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  r in the FROM cl
2bf57 61 75 73 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ause,.**        
2bf58 20 66 69 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e   fill pTabList->
2bf59 61 5b 5d 2e 70 53 65 6c 65 63 74 20 77 69 74 68  a[].pSelect with
2bf5a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53   a copy of the S
2bf5b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a  ELECT statement.
2bf5c 2a 2a 20 20 20 20 20 20 20 20 20 74 68 61 74 20  **         that 
2bf5d 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76  implements the v
2bf5e 69 65 77 2e 20 20 41 20 63 6f 70 79 20 69 73 20  iew.  A copy is 
2bf5f 6d 61 64 65 20 6f 66 20 74 68 65 20 76 69 65 77  made of the view
2bf60 27 73 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  's SELECT.**    
2bf61 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 20 73       statement s
2bf62 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 72  o that we can fr
2bf63 65 65 6c 79 20 6d 6f 64 69 66 79 20 6f 72 20 64  eely modify or d
2bf64 65 6c 65 74 65 20 74 68 61 74 20 73 74 61 74 65  elete that state
2bf65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ment.**         
2bf66 77 69 74 68 6f 75 74 20 77 6f 72 72 79 69 6e 67  without worrying
2bf67 20 61 62 6f 75 74 20 6d 65 73 73 69 6e 67 20 75   about messing u
2bf68 70 20 74 68 65 20 70 72 65 73 69 73 74 65 6e 74  p the presistent
2bf69 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a   representation.
2bf6a 2a 2a 20 20 20 20 20 20 20 20 20 6f 66 20 74 68  **         of th
2bf6b 65 20 76 69 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20  e view..**.**   
2bf6c 20 28 33 29 20 20 41 64 64 20 74 65 72 6d 73 20   (3)  Add terms 
2bf6d 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  to the WHERE cla
2bf6e 75 73 65 20 74 6f 20 61 63 63 6f 6d 6f 64 61 74  use to accomodat
2bf6f 65 20 74 68 65 20 4e 41 54 55 52 41 4c 20 6b 65  e the NATURAL ke
2bf70 79 77 6f 72 64 0a 2a 2a 20 20 20 20 20 20 20 20  yword.**        
2bf71 20 6f 6e 20 6a 6f 69 6e 73 20 61 6e 64 20 74 68   on joins and th
2bf72 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63  e ON and USING c
2bf73 6c 61 75 73 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a  lause of joins..
2bf74 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 53 63  **.**    (4)  Sc
2bf75 61 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63  an the list of c
2bf76 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
2bf77 73 75 6c 74 20 73 65 74 20 28 70 45 4c 69 73 74  sult set (pEList
2bf78 29 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20  ) looking.**    
2bf79 20 20 20 20 20 66 6f 72 20 69 6e 73 74 61 6e 63       for instanc
2bf7a 65 73 20 6f 66 20 74 68 65 20 22 2a 22 20 6f 70  es of the "*" op
2bf7b 65 72 61 74 6f 72 20 6f 72 20 74 68 65 20 54 41  erator or the TA
2bf7c 42 4c 45 2e 2a 20 6f 70 65 72 61 74 6f 72 2e 0a  BLE.* operator..
2bf7d 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20 66 6f  **         If fo
2bf7e 75 6e 64 2c 20 65 78 70 61 6e 64 20 65 61 63 68  und, expand each
2bf7f 20 22 2a 22 20 74 6f 20 62 65 20 65 76 65 72 79   "*" to be every
2bf80 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79   column in every
2bf81 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20   table.**       
2bf82 20 20 61 6e 64 20 54 41 42 4c 45 2e 2a 20 74 6f    and TABLE.* to
2bf83 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e   be every column
2bf84 20 69 6e 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f   in TABLE..**.*/
2bf85 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65  .static int sele
2bf86 63 74 45 78 70 61 6e 64 65 72 28 57 61 6c 6b 65  ctExpander(Walke
2bf87 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
2bf88 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20  ct *p){.  Parse 
2bf89 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65  *pParse = pWalke
2bf8a 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 69 6e 74  r->pParse;.  int
2bf8b 20 69 2c 20 6a 2c 20 6b 3b 0a 20 20 53 72 63 4c   i, j, k;.  SrcL
2bf8c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20  ist *pTabList;. 
2bf8d 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2bf8e 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  t;.  struct SrcL
2bf8f 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b  ist_item *pFrom;
2bf90 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2bf91 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
2bf92 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2bf93 69 6c 65 64 20 20 29 7b 0a 20 20 20 20 72 65 74  iled  ){.    ret
2bf94 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
2bf95 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70   }.  if( NEVER(p
2bf96 2d 3e 70 53 72 63 3d 3d 30 29 20 7c 7c 20 28 70  ->pSrc==0) || (p
2bf97 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2bf98 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29 7b 0a  Expanded)!=0 ){.
2bf99 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
2bf9a 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 73  rune;.  }.  p->s
2bf9b 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 45 78  elFlags |= SF_Ex
2bf9c 70 61 6e 64 65 64 3b 0a 20 20 70 54 61 62 4c 69  panded;.  pTabLi
2bf9d 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  st = p->pSrc;.  
2bf9e 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
2bf9f 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  st;..  /* Make s
2bfa0 75 72 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ure cursor numbe
2bfa1 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 73 73  rs have been ass
2bfa2 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20 65 6e 74  igned to all ent
2bfa3 72 69 65 73 20 69 6e 0a 20 20 2a 2a 20 74 68 65  ries in.  ** the
2bfa4 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2bfa5 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2bfa6 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ment..  */.  sql
2bfa7 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
2bfa8 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
2bfa9 20 70 54 61 62 4c 69 73 74 29 3b 0a 0a 20 20 2f   pTabList);..  /
2bfaa 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20  * Look up every 
2bfab 74 61 62 6c 65 20 6e 61 6d 65 64 20 69 6e 20 74  table named in t
2bfac 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2bfad 66 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20 49  f the select.  I
2bfae 66 0a 20 20 2a 2a 20 61 6e 20 65 6e 74 72 79 20  f.  ** an entry 
2bfaf 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2bfb0 73 65 20 69 73 20 61 20 73 75 62 71 75 65 72 79  se is a subquery
2bfb1 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 74 61   instead of a ta
2bfb2 62 6c 65 20 6f 72 20 76 69 65 77 2c 0a 20 20 2a  ble or view,.  *
2bfb3 2a 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20  * then create a 
2bfb4 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
2bfb5 73 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 73  structure to des
2bfb6 63 72 69 62 65 20 74 68 65 20 73 75 62 71 75 65  cribe the subque
2bfb7 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ry..  */.  for(i
2bfb8 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69  =0, pFrom=pTabLi
2bfb9 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  st->a; i<pTabLis
2bfba 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46  t->nSrc; i++, pF
2bfbb 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  rom++){.    Tabl
2bfbc 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
2bfbd 20 70 46 72 6f 6d 2d 3e 70 54 61 62 21 3d 30 20   pFrom->pTab!=0 
2bfbe 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
2bfbf 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 61   statement has a
2bfc0 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 70  lready been prep
2bfc1 61 72 65 64 2e 20 20 54 68 65 72 65 20 69 73 20  ared.  There is 
2bfc2 6e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 2a 2a  no need.      **
2bfc3 20 74 6f 20 67 6f 20 66 75 72 74 68 65 72 2e 20   to go further. 
2bfc4 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2bfc5 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72   i==0 );.      r
2bfc6 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2bfc7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2bfc8 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29  From->zName==0 )
2bfc9 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
2bfca 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2bfcb 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65       Select *pSe
2bfcc 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  l = pFrom->pSele
2bfcd 63 74 3b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73  ct;.      /* A s
2bfce 75 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20  ub-query in the 
2bfcf 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
2bfd0 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20   SELECT */.     
2bfd1 20 61 73 73 65 72 74 28 20 70 53 65 6c 21 3d 30   assert( pSel!=0
2bfd2 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2bfd3 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30  ( pFrom->pTab==0
2bfd4 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
2bfd5 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
2bfd6 6b 65 72 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20  ker, pSel);.    
2bfd7 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20    pFrom->pTab = 
2bfd8 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62  pTab = sqlite3Db
2bfd9 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2bfda 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20  izeof(Table));. 
2bfdb 20 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30       if( pTab==0
2bfdc 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
2bfdd 6f 72 74 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ort;.      pTab-
2bfde 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f  >dbMem = db->loo
2bfdf 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
2bfe0 3f 20 64 62 20 3a 20 30 3b 0a 20 20 20 20 20 20  ? db : 0;.      
2bfe1 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  pTab->nRef = 1;.
2bfe2 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2bfe3 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
2bfe4 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73  tf(db, "sqlite_s
2bfe5 75 62 71 75 65 72 79 5f 25 70 5f 22 2c 20 28 76  ubquery_%p_", (v
2bfe6 6f 69 64 2a 29 70 54 61 62 29 3b 0a 20 20 20 20  oid*)pTab);.    
2bfe7 20 20 77 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70    while( pSel->p
2bfe8 50 72 69 6f 72 20 29 7b 20 70 53 65 6c 20 3d 20  Prior ){ pSel = 
2bfe9 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b 20 7d 0a  pSel->pPrior; }.
2bfea 20 20 20 20 20 20 73 65 6c 65 63 74 43 6f 6c 75        selectColu
2bfeb 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  mnsFromExprList(
2bfec 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 45  pParse, pSel->pE
2bfed 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f  List, &pTab->nCo
2bfee 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b  l, &pTab->aCol);
2bfef 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 69 50 4b  .      pTab->iPK
2bff0 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70  ey = -1;.      p
2bff1 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d  Tab->tabFlags |=
2bff2 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 3b 0a 23   TF_Ephemeral;.#
2bff3 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b  endif.    }else{
2bff4 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 72 64  .      /* An ord
2bff5 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72 20 76  inary table or v
2bff6 69 65 77 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  iew name in the 
2bff7 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2bff8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
2bff9 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20  om->pTab==0 );. 
2bffa 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62       pFrom->pTab
2bffb 20 3d 20 70 54 61 62 20 3d 20 0a 20 20 20 20 20   = pTab = .     
2bffc 20 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65     sqlite3Locate
2bffd 54 61 62 6c 65 28 70 50 61 72 73 65 2c 30 2c 70  Table(pParse,0,p
2bffe 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f  From->zName,pFro
2bfff 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
2c000 20 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30       if( pTab==0
2c001 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
2c002 6f 72 74 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ort;.      pTab-
2c003 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20 21 64 65  >nRef++;.#if !de
2c004 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2c005 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
2c006 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ned (SQLITE_OMIT
2c007 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20  _VIRTUALTABLE). 
2c008 20 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70       if( pTab->p
2c009 53 65 6c 65 63 74 20 7c 7c 20 49 73 56 69 72 74  Select || IsVirt
2c00a 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
2c00b 20 20 20 20 20 2f 2a 20 57 65 20 72 65 61 63 68       /* We reach
2c00c 20 68 65 72 65 20 69 66 20 74 68 65 20 6e 61 6d   here if the nam
2c00d 65 64 20 74 61 62 6c 65 20 69 73 20 61 20 72 65  ed table is a re
2c00e 61 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f 0a 20  ally a view */. 
2c00f 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
2c010 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
2c011 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
2c012 62 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  b) ) return WRC_
2c013 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 61  Abort;.        a
2c014 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 53  ssert( pFrom->pS
2c015 65 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20  elect==0 );.    
2c016 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65      pFrom->pSele
2c017 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
2c018 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 2d 3e  ctDup(db, pTab->
2c019 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20  pSelect, 0);.   
2c01a 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b       sqlite3Walk
2c01b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
2c01c 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b  pFrom->pSelect);
2c01d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
2c01e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f      }..    /* Lo
2c01f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 6e  cate the index n
2c020 61 6d 65 64 20 62 79 20 74 68 65 20 49 4e 44 45  amed by the INDE
2c021 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20 69  XED BY clause, i
2c022 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 69 66  f any. */.    if
2c023 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64  ( sqlite3Indexed
2c024 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  ByLookup(pParse,
2c025 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20   pFrom) ){.     
2c026 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
2c027 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
2c028 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41 54 55 52  /* Process NATUR
2c029 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64  AL keywords, and
2c02a 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
2c02b 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a  auses of joins..
2c02c 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d    */.  if( db->m
2c02d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73  allocFailed || s
2c02e 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e  qliteProcessJoin
2c02f 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b 0a 20  (pParse, p) ){. 
2c030 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
2c031 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  ort;.  }..  /* F
2c032 6f 72 20 65 76 65 72 79 20 22 2a 22 20 74 68 61  or every "*" tha
2c033 74 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20  t occurs in the 
2c034 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69 6e 73  column list, ins
2c035 65 72 74 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ert the names of
2c036 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d 6e  .  ** all column
2c037 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e  s in all tables.
2c038 20 20 41 6e 64 20 66 6f 72 20 65 76 65 72 79 20    And for every 
2c039 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74 20 74  TABLE.* insert t
2c03a 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66  he names.  ** of
2c03b 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   all columns in 
2c03c 54 41 42 4c 45 2e 20 20 54 68 65 20 70 61 72 73  TABLE.  The pars
2c03d 65 72 20 69 6e 73 65 72 74 65 64 20 61 20 73 70  er inserted a sp
2c03e 65 63 69 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  ecial expression
2c03f 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 54  .  ** with the T
2c040 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20 66  K_ALL operator f
2c041 6f 72 20 65 61 63 68 20 22 2a 22 20 74 68 61 74  or each "*" that
2c042 20 69 74 20 66 6f 75 6e 64 20 69 6e 20 74 68 65   it found in the
2c043 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20 20   column list..  
2c044 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
2c045 20 63 6f 64 65 20 6a 75 73 74 20 68 61 73 20 74   code just has t
2c046 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 54 4b 5f  o locate the TK_
2c047 41 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ALL expressions 
2c048 61 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a 2a 20  and expand.  ** 
2c049 65 61 63 68 20 6f 6e 65 20 74 6f 20 74 68 65 20  each one to the 
2c04a 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c 75  list of all colu
2c04b 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65  mns in all table
2c04c 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  s..  **.  ** The
2c04d 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74   first loop just
2c04e 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69   checks to see i
2c04f 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
2c050 22 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a 20 20  "*" operators.  
2c051 2a 2a 20 74 68 61 74 20 6e 65 65 64 20 65 78 70  ** that need exp
2c052 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66  anding..  */.  f
2c053 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74  or(k=0; k<pEList
2c054 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20  ->nExpr; k++){. 
2c055 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 45     Expr *pE = pE
2c056 4c 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72  List->a[k].pExpr
2c057 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70  ;.    if( pE->op
2c058 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b  ==TK_ALL ) break
2c059 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
2c05a 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20  ->op!=TK_DOT || 
2c05b 70 45 2d 3e 70 52 69 67 68 74 21 3d 30 20 29 3b  pE->pRight!=0 );
2c05c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 2d  .    assert( pE-
2c05d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 28  >op!=TK_DOT || (
2c05e 70 45 2d 3e 70 4c 65 66 74 21 3d 30 20 26 26 20  pE->pLeft!=0 && 
2c05f 70 45 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54  pE->pLeft->op==T
2c060 4b 5f 49 44 29 20 29 3b 0a 20 20 20 20 69 66 28  K_ID) );.    if(
2c061 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20   pE->op==TK_DOT 
2c062 26 26 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f  && pE->pRight->o
2c063 70 3d 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61  p==TK_ALL ) brea
2c064 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6b 3c 70  k;.  }.  if( k<p
2c065 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  EList->nExpr ){.
2c066 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66      /*.    ** If
2c067 20 77 65 20 67 65 74 20 68 65 72 65 20 69 74 20   we get here it 
2c068 6d 65 61 6e 73 20 74 68 65 20 72 65 73 75 6c 74  means the result
2c069 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e   set contains on
2c06a 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22 0a 20 20  e or more "*".  
2c06b 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 73 20 74    ** operators t
2c06c 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 65  hat need to be e
2c06d 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f 70 20 74  xpanded.  Loop t
2c06e 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72  hrough each expr
2c06f 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 69 6e  ession.    ** in
2c070 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2c071 61 6e 64 20 65 78 70 61 6e 64 20 74 68 65 6d 20  and expand them 
2c072 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20  one by one..    
2c073 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  */.    struct Ex
2c074 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 20 3d  prList_item *a =
2c075 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20   pEList->a;.    
2c076 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 20 3d  ExprList *pNew =
2c077 20 30 3b 0a 20 20 20 20 69 6e 74 20 66 6c 61 67   0;.    int flag
2c078 73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  s = pParse->db->
2c079 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e 74 20 6c  flags;.    int l
2c07a 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66 6c 61 67  ongNames = (flag
2c07b 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43  s & SQLITE_FullC
2c07c 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a 20 20 20 20  olNames)!=0.    
2c07d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c07e 20 20 26 26 20 28 66 6c 61 67 73 20 26 20 53 51    && (flags & SQ
2c07f 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
2c080 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20 66 6f 72  es)==0;..    for
2c081 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e  (k=0; k<pEList->
2c082 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20  nExpr; k++){.   
2c083 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 61 5b     Expr *pE = a[
2c084 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  k].pExpr;.      
2c085 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d  assert( pE->op!=
2c086 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52  TK_DOT || pE->pR
2c087 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  ight!=0 );.     
2c088 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f   if( pE->op!=TK_
2c089 41 4c 4c 20 26 26 20 28 70 45 2d 3e 6f 70 21 3d  ALL && (pE->op!=
2c08a 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52  TK_DOT || pE->pR
2c08b 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c  ight->op!=TK_ALL
2c08c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
2c08d 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20  This particular 
2c08e 65 78 70 72 65 73 73 69 6f 6e 20 64 6f 65 73 20  expression does 
2c08f 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 65  not need to be e
2c090 78 70 61 6e 64 65 64 2e 0a 20 20 20 20 20 20 20  xpanded..       
2c091 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77   */.        pNew
2c092 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2c093 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2c094 20 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70   pNew, a[k].pExp
2c095 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  r);.        if( 
2c096 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
2c097 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e    pNew->a[pNew->
2c098 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d  nExpr-1].zName =
2c099 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20   a[k].zName;.   
2c09a 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70         pNew->a[p
2c09b 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 53  New->nExpr-1].zS
2c09c 70 61 6e 20 3d 20 61 5b 6b 5d 2e 7a 53 70 61 6e  pan = a[k].zSpan
2c09d 3b 0a 20 20 20 20 20 20 20 20 20 20 61 5b 6b 5d  ;.          a[k]
2c09e 2e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  .zName = 0;.    
2c09f 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 53 70 61 6e        a[k].zSpan
2c0a0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
2c0a1 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78          a[k].pEx
2c0a2 70 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  pr = 0;.      }e
2c0a3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
2c0a4 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  This expression 
2c0a5 69 73 20 61 20 22 2a 22 20 6f 72 20 61 20 22 54  is a "*" or a "T
2c0a6 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64  ABLE.*" and need
2c0a7 73 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20  s to be.        
2c0a8 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a  ** expanded. */.
2c0a9 20 20 20 20 20 20 20 20 69 6e 74 20 74 61 62 6c          int tabl
2c0aa 65 53 65 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  eSeen = 0;      
2c0ab 2f 2a 20 53 65 74 20 74 6f 20 31 20 77 68 65 6e  /* Set to 1 when
2c0ac 20 54 41 42 4c 45 20 6d 61 74 63 68 65 73 20 2a   TABLE matches *
2c0ad 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  /.        char *
2c0ae 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  zTName;         
2c0af 20 20 20 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61     /* text of na
2c0b0 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20  me of TABLE */. 
2c0b1 20 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f         if( pE->o
2c0b2 70 3d 3d 54 4b 5f 44 4f 54 20 29 7b 0a 20 20 20  p==TK_DOT ){.   
2c0b3 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2c0b4 45 2d 3e 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20  E->pLeft!=0 );. 
2c0b5 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2c0b6 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2c0b7 79 28 70 45 2d 3e 70 4c 65 66 74 2c 20 45 50 5f  y(pE->pLeft, EP_
2c0b8 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
2c0b9 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20         zTName = 
2c0ba 70 45 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f  pE->pLeft->u.zTo
2c0bb 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ken;.        }el
2c0bc 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  se{.          zT
2c0bd 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
2c0be 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28    }.        for(
2c0bf 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c  i=0, pFrom=pTabL
2c0c0 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69  ist->a; i<pTabLi
2c0c1 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
2c0c2 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  From++){.       
2c0c3 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
2c0c4 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20   pFrom->pTab;.  
2c0c5 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54          char *zT
2c0c6 61 62 4e 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e  abName = pFrom->
2c0c7 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20  zAlias;.        
2c0c8 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d    if( zTabName==
2c0c9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
2c0ca 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62   zTabName = pTab
2c0cb 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ->zName;.       
2c0cc 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
2c0cd 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2c0ce 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  led ) break;.   
2c0cf 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d         if( zTNam
2c0d0 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  e && sqlite3StrI
2c0d1 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a 54 61 62  Cmp(zTName, zTab
2c0d2 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Name)!=0 ){.    
2c0d3 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
2c0d4 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2c0d5 20 20 20 20 20 20 20 20 74 61 62 6c 65 53 65 65          tableSee
2c0d6 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  n = 1;.         
2c0d7 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62   for(j=0; j<pTab
2c0d8 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
2c0d9 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
2c0da 70 45 78 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a  pExpr, *pRight;.
2c0db 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72              char
2c0dc 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e   *zName = pTab->
2c0dd 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20  aCol[j].zName;. 
2c0de 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
2c0df 2a 7a 43 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 54  *zColname;  /* T
2c0e0 68 65 20 63 6f 6d 70 75 74 65 64 20 63 6f 6c 75  he computed colu
2c0e1 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  mn name */.     
2c0e2 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f         char *zTo
2c0e3 46 72 65 65 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f  Free;   /* Mallo
2c0e4 63 65 64 20 73 74 72 69 6e 67 20 74 68 61 74 20  ced string that 
2c0e5 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65  needs to be free
2c0e6 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  d */.           
2c0e7 20 54 6f 6b 65 6e 20 73 43 6f 6c 6e 61 6d 65 3b   Token sColname;
2c0e8 20 20 2f 2a 20 43 6f 6d 70 75 74 65 64 20 63 6f    /* Computed co
2c0e9 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 61 20 74  lumn name as a t
2c0ea 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20  oken */..       
2c0eb 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c       /* If a col
2c0ec 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20 61 73  umn is marked as
2c0ed 20 27 68 69 64 64 65 6e 27 20 28 63 75 72 72 65   'hidden' (curre
2c0ee 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62  ntly only possib
2c0ef 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  le.            *
2c0f0 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20 74 61  * for virtual ta
2c0f1 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e  bles), do not in
2c0f2 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68 65 20  clude it in the 
2c0f3 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20 20 20  expanded.       
2c0f4 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73       ** result-s
2c0f5 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 20  et list..       
2c0f6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2c0f7 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e      if( IsHidden
2c0f8 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43  Column(&pTab->aC
2c0f9 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20 20 20  ol[j]) ){.      
2c0fa 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 49          assert(I
2c0fb 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 3b  sVirtual(pTab));
2c0fc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63  .              c
2c0fd 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2c0fe 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
2c0ff 20 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 7a      if( i>0 && z
2c100 54 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  TName==0 ){.    
2c101 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74            struct
2c102 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2c103 4c 65 66 74 20 3d 20 26 70 54 61 62 4c 69 73 74  Left = &pTabList
2c104 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20  ->a[i-1];.      
2c105 20 20 20 20 20 20 20 20 69 66 28 20 28 70 4c 65          if( (pLe
2c106 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26  ft[1].jointype &
2c107 20 4a 54 5f 4e 41 54 55 52 41 4c 29 21 3d 30 20   JT_NATURAL)!=0 
2c108 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
2c109 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d             colum
2c10a 6e 49 6e 64 65 78 28 70 4c 65 66 74 2d 3e 70 54  nIndex(pLeft->pT
2c10b 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b  ab, zName)>=0 ){
2c10c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c10d 20 2f 2a 20 49 6e 20 61 20 4e 41 54 55 52 41 4c   /* In a NATURAL
2c10e 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20   join, omit the 
2c10f 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f  join columns fro
2c110 6d 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 20  m the .         
2c111 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20         ** table 
2c112 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a 2f 0a  on the right */.
2c113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c114 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2c115 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c116 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2c117 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70  te3IdListIndex(p
2c118 4c 65 66 74 5b 31 5d 2e 70 55 73 69 6e 67 2c 20  Left[1].pUsing, 
2c119 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20  zName)>=0 ){.   
2c11a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c11b 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74 68 20 61  In a join with a
2c11c 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2c 20 6f   USING clause, o
2c11d 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  mit columns in t
2c11e 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  he.             
2c11f 20 20 20 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75     ** using clau
2c120 73 65 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c  se from the tabl
2c121 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20  e on the right. 
2c122 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
2c123 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2c124 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2c125 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c126 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20         pRight = 
2c127 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
2c128 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29 3b 0a 20  TK_ID, zName);. 
2c129 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f 6c 6e             zColn
2c12a 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 20  ame = zName;.   
2c12b 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72 65 65           zToFree
2c12c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2c12d 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20    if( longNames 
2c12e 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  || pTabList->nSr
2c12f 63 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  c>1 ){.         
2c130 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
2c131 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2c132 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  pLeft = sqlite3E
2c133 78 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a  xpr(db, TK_ID, z
2c134 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  TabName);.      
2c135 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
2c136 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2c137 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65  rse, TK_DOT, pLe
2c138 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a  ft, pRight, 0);.
2c139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
2c13a 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20  ( longNames ){. 
2c13b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
2c13c 43 6f 6c 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  Colname = sqlite
2c13d 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
2c13e 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20  .%s", zTabName, 
2c13f 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
2c140 20 20 20 20 20 20 20 20 7a 54 6f 46 72 65 65 20          zToFree 
2c141 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20  = zColname;.    
2c142 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2c143 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2c144 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
2c145 70 72 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20  pr = pRight;.   
2c146 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c147 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71         pNew = sq
2c148 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2c149 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77  end(pParse, pNew
2c14a 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
2c14b 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 7a        sColname.z
2c14c 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20   = zColname;.   
2c14d 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d           sColnam
2c14e 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  e.n = sqlite3Str
2c14f 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d 65 29 3b  len30(zColname);
2c150 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2c151 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e  ite3ExprListSetN
2c152 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 65 77  ame(pParse, pNew
2c153 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20 30 29 3b  , &sColname, 0);
2c154 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2c155 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2c156 54 6f 46 72 65 65 29 3b 0a 20 20 20 20 20 20 20  ToFree);.       
2c157 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2c158 20 20 20 20 20 20 20 69 66 28 20 21 74 61 62 6c         if( !tabl
2c159 65 53 65 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  eSeen ){.       
2c15a 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 29 7b     if( zTName ){
2c15b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2c15c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c15d 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 61  rse, "no such ta
2c15e 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e 61 6d 65  ble: %s", zTName
2c15f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
2c160 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2c161 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2c162 70 50 61 72 73 65 2c 20 22 6e 6f 20 74 61 62 6c  pParse, "no tabl
2c163 65 73 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a  es specified");.
2c164 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2c165 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2c166 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45    }.    sqlite3E
2c167 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2c168 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70  , pEList);.    p
2c169 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b  ->pEList = pNew;
2c16a 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  }.#if SQLITE_
2c16b 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
2c16c 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20 70 2d   p->pEList && p-
2c16d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 64  >pEList->nExpr>d
2c16e 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
2c16f 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29  _LIMIT_COLUMN] )
2c170 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2c171 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
2c172 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  oo many columns 
2c173 69 6e 20 72 65 73 75 6c 74 20 73 65 74 22 29 3b  in result set");
2c174 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
2c175 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
2c176 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f  e;.}../*.** No-o
2c177 70 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 74 68  p routine for th
2c178 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c  e parse-tree wal
2c179 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ker..**.** When 
2c17a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2c17b 74 68 65 20 57 61 6c 6b 65 72 2e 78 45 78 70 72  the Walker.xExpr
2c17c 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20 65 78  Callback then ex
2c17d 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 0a 2a  pression trees.*
2c17e 2a 20 61 72 65 20 77 61 6c 6b 65 64 20 77 69 74  * are walked wit
2c17f 68 6f 75 74 20 61 6e 79 20 61 63 74 69 6f 6e 73  hout any actions
2c180 20 62 65 69 6e 67 20 74 61 6b 65 6e 20 61 74 20   being taken at 
2c181 65 61 63 68 20 6e 6f 64 65 2e 20 20 50 72 65 73  each node.  Pres
2c182 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e 20  umably,.** when 
2c183 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2c184 75 73 65 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e  used for Walker.
2c185 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68  xExprCallback th
2c186 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53  en .** Walker.xS
2c187 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 69 73  electCallback is
2c188 20 73 65 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74   set to do somet
2c189 68 69 6e 67 20 75 73 65 66 75 6c 20 66 6f 72 20  hing useful for 
2c18a 65 76 65 72 79 20 0a 2a 2a 20 73 75 62 71 75 65  every .** subque
2c18b 72 79 20 69 6e 20 74 68 65 20 70 61 72 73 65 72  ry in the parser
2c18c 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   tree..*/.static
2c18d 20 69 6e 74 20 65 78 70 72 57 61 6c 6b 4e 6f 6f   int exprWalkNoo
2c18e 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 65  p(Walker *NotUse
2c18f 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55 73 65 64  d, Expr *NotUsed
2c190 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  2){.  UNUSED_PAR
2c191 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
2c192 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65   NotUsed2);.  re
2c193 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
2c194 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
2c195 20 72 6f 75 74 69 6e 65 20 22 65 78 70 61 6e 64   routine "expand
2c196 73 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  s" a SELECT stat
2c197 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66  ement and all of
2c198 20 69 74 73 20 73 75 62 71 75 65 72 69 65 73 2e   its subqueries.
2c199 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74 69 6f 6e  .** For addition
2c19a 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  al information o
2c19b 6e 20 77 68 61 74 20 69 74 20 6d 65 61 6e 73 20  n what it means 
2c19c 74 6f 20 22 65 78 70 61 6e 64 22 20 61 20 53 45  to "expand" a SE
2c19d 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LECT.** statemen
2c19e 74 2c 20 73 65 65 20 74 68 65 20 63 6f 6d 6d 65  t, see the comme
2c19f 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c 65 63 74  nt on the select
2c1a0 45 78 70 61 6e 64 20 77 6f 72 6b 65 72 20 63 61  Expand worker ca
2c1a1 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a  llback above..**
2c1a2 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67 20 61 20  .** Expanding a 
2c1a3 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2c1a4 20 69 73 20 74 68 65 20 66 69 72 73 74 20 73 74   is the first st
2c1a5 65 70 20 69 6e 20 70 72 6f 63 65 73 73 69 6e 67  ep in processing
2c1a6 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61   a.** SELECT sta
2c1a7 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c  tement.  The SEL
2c1a8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6d 75  ECT statement mu
2c1a9 73 74 20 62 65 20 65 78 70 61 6e 64 65 64 20 62  st be expanded b
2c1aa 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65  efore.** name re
2c1ab 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 66  solution is perf
2c1ac 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ormed..**.** If 
2c1ad 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
2c1ae 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65  ong, an error me
2c1af 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e  ssage is written
2c1b0 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a   into pParse..**
2c1b1 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
2c1b2 63 74 69 6f 6e 20 63 61 6e 20 64 65 74 65 63 74  ction can detect
2c1b3 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 62 79 20   the problem by 
2c1b4 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50 61 72 73  looking at pPars
2c1b5 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f  e->nErr.** and/o
2c1b6 72 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  r pParse->db->ma
2c1b7 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73  llocFailed..*/.s
2c1b8 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
2c1b9 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 50  e3SelectExpand(P
2c1ba 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
2c1bb 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
2c1bc 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
2c1bd 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
2c1be 3d 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72  = selectExpander
2c1bf 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
2c1c0 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
2c1c1 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
2c1c2 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
2c1c3 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
2c1c4 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23   pSelect);.}...#
2c1c5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c1c6 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a  IT_SUBQUERY./*.*
2c1c7 2a 20 54 68 69 73 20 69 73 20 61 20 57 61 6c 6b  * This is a Walk
2c1c8 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  er.xSelectCallba
2c1c9 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ck callback for 
2c1ca 74 68 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  the sqlite3Selec
2c1cb 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69  tTypeInfo().** i
2c1cc 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
2c1cd 46 6f 72 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c  For each FROM-cl
2c1ce 61 75 73 65 20 73 75 62 71 75 65 72 79 2c 20 61  ause subquery, a
2c1cf 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20  dd Column.zType 
2c1d0 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c  and Column.zColl
2c1d1 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
2c1d2 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
2c1d3 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70 72  ucture that repr
2c1d4 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c  esents the resul
2c1d5 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74  t set.** of that
2c1d6 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a   subquery..**.**
2c1d7 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   The Table struc
2c1d8 74 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73  ture that repres
2c1d9 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20  ents the result 
2c1da 73 65 74 20 77 61 73 20 63 6f 6e 73 74 72 75 63  set was construc
2c1db 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74  ted.** by select
2c1dc 45 78 70 61 6e 64 65 72 28 29 20 62 75 74 20 74  Expander() but t
2c1dd 68 65 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c  he type and coll
2c1de 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ation informatio
2c1df 6e 20 77 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a  n was omitted.**
2c1e0 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 62   at that point b
2c1e1 65 63 61 75 73 65 20 69 64 65 6e 74 69 66 69 65  ecause identifie
2c1e2 72 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 62  rs had not yet b
2c1e3 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 54  een resolved.  T
2c1e4 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
2c1e5 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69  s called after i
2c1e6 64 65 6e 74 69 66 69 65 72 20 72 65 73 6f 6c 75  dentifier resolu
2c1e7 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2c1e8 69 6e 74 20 73 65 6c 65 63 74 41 64 64 53 75 62  int selectAddSub
2c1e9 71 75 65 72 79 54 79 70 65 49 6e 66 6f 28 57 61  queryTypeInfo(Wa
2c1ea 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
2c1eb 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72  elect *p){.  Par
2c1ec 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e  se *pParse;.  in
2c1ed 74 20 69 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  t i;.  SrcList *
2c1ee 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 74 72 75  pTabList;.  stru
2c1ef 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2c1f0 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72  *pFrom;..  asser
2c1f1 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  t( p->selFlags &
2c1f2 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a   SF_Resolved );.
2c1f3 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65    assert( (p->se
2c1f4 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54  lFlags & SF_HasT
2c1f5 79 70 65 49 6e 66 6f 29 3d 3d 30 20 29 3b 0a 20  ypeInfo)==0 );. 
2c1f6 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20   p->selFlags |= 
2c1f7 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 3b 0a  SF_HasTypeInfo;.
2c1f8 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b    pParse = pWalk
2c1f9 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 70 54  er->pParse;.  pT
2c1fa 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
2c1fb 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72  ;.  for(i=0, pFr
2c1fc 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20  om=pTabList->a; 
2c1fd 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2c1fe 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
2c1ff 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
2c200 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a   = pFrom->pTab;.
2c201 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
2c202 54 61 62 21 3d 30 29 20 26 26 20 28 70 54 61 62  Tab!=0) && (pTab
2c203 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2c204 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 29 7b  Ephemeral)!=0 ){
2c205 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d  .      /* A sub-
2c206 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f  query in the FRO
2c207 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
2c208 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 53 65  LECT */.      Se
2c209 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72  lect *pSel = pFr
2c20a 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20  om->pSelect;.   
2c20b 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 20     assert( pSel 
2c20c 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
2c20d 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70  pSel->pPrior ) p
2c20e 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69  Sel = pSel->pPri
2c20f 6f 72 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74  or;.      select
2c210 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64  AddColumnTypeAnd
2c211 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73 65  Collation(pParse
2c212 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54  , pTab->nCol, pT
2c213 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b  ab->aCol, pSel);
2c214 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2c215 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
2c216 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ;.}.#endif.../*.
2c217 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2c218 61 64 64 73 20 64 61 74 61 74 79 70 65 20 61 6e  adds datatype an
2c219 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  d collating sequ
2c21a 65 6e 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ence information
2c21b 20 74 6f 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65   to.** the Table
2c21c 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 61   structures of a
2c21d 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73  ll FROM-clause s
2c21e 75 62 71 75 65 72 69 65 73 20 69 6e 20 61 0a 2a  ubqueries in a.*
2c21f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
2c220 6e 74 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68  nt..**.** Use th
2c221 69 73 20 72 6f 75 74 69 6e 65 20 61 66 74 65 72  is routine after
2c222 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e   name resolution
2c223 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2c224 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41 64   sqlite3SelectAd
2c225 64 54 79 70 65 49 6e 66 6f 28 50 61 72 73 65 20  dTypeInfo(Parse 
2c226 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2c227 2a 70 53 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64  *pSelect){.#ifnd
2c228 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2c229 55 42 51 55 45 52 59 0a 20 20 57 61 6c 6b 65 72  UBQUERY.  Walker
2c22a 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43   w;.  w.xSelectC
2c22b 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74  allback = select
2c22c 41 64 64 53 75 62 71 75 65 72 79 54 79 70 65 49  AddSubqueryTypeI
2c22d 6e 66 6f 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  nfo;.  w.xExprCa
2c22e 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c  llback = exprWal
2c22f 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73  kNoop;.  w.pPars
2c230 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
2c231 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
2c232 26 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 23 65  &w, pSelect);.#e
2c233 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ndif.}.../*.** T
2c234 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
2c235 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
2c236 74 65 6d 65 6e 74 20 66 6f 72 20 70 72 6f 63 65  tement for proce
2c237 73 73 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66  ssing.  The.** f
2c238 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 63 63 6f  ollowing is acco
2c239 6d 70 6c 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20  mplished:.**.** 
2c23a 20 20 20 20 2a 20 20 56 44 42 45 20 43 75 72 73      *  VDBE Curs
2c23b 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61  or numbers are a
2c23c 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46  ssigned to all F
2c23d 52 4f 4d 2d 63 6c 61 75 73 65 20 74 65 72 6d 73  ROM-clause terms
2c23e 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 45 70 68 65  ..**     *  Ephe
2c23f 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65  meral Table obje
2c240 63 74 73 20 61 72 65 20 63 72 65 61 74 65 64 20  cts are created 
2c241 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61  for all FROM-cla
2c242 75 73 65 20 73 75 62 71 75 65 72 69 65 73 2e 0a  use subqueries..
2c243 2a 2a 20 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64  **     *  ON and
2c244 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61   USING clauses a
2c245 72 65 20 73 68 69 66 74 65 64 20 69 6e 74 6f 20  re shifted into 
2c246 57 48 45 52 45 20 73 74 61 74 65 6d 65 6e 74 73  WHERE statements
2c247 0a 2a 2a 20 20 20 20 20 2a 20 20 57 69 6c 64 63  .**     *  Wildc
2c248 61 72 64 73 20 22 2a 22 20 61 6e 64 20 22 54 41  ards "*" and "TA
2c249 42 4c 45 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74  BLE.*" in result
2c24a 20 73 65 74 73 20 61 72 65 20 65 78 70 61 6e 64   sets are expand
2c24b 65 64 2e 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64  ed..**     *  Id
2c24c 65 6e 74 69 66 69 65 72 73 20 69 6e 20 65 78 70  entifiers in exp
2c24d 72 65 73 73 69 6f 6e 20 61 72 65 20 6d 61 74 63  ression are matc
2c24e 68 65 64 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a  hed to tables..*
2c24f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2c250 65 20 61 63 74 73 20 72 65 63 75 72 73 69 76 65  e acts recursive
2c251 6c 79 20 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65  ly on all subque
2c252 72 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  ries within the 
2c253 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54  SELECT..*/.SQLIT
2c254 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2c255 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
2c256 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2c257 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
2c258 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2c259 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2c25a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c25b 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
2c25c 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
2c25d 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  . */.  NameConte
2c25e 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a  xt *pOuterNC  /*
2c25f 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f   Name context fo
2c260 72 20 63 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29  r container */.)
2c261 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
2c262 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d  .  if( NEVER(p==
2c263 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64  0) ) return;.  d
2c264 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2c265 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
2c266 73 20 26 20 53 46 5f 48 61 73 54 79 70 65 49 6e  s & SF_HasTypeIn
2c267 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  fo ) return;.  s
2c268 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
2c269 6e 64 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  nd(pParse, p);. 
2c26a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2c26b 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2c26c 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2c26d 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
2c26e 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72  SelectNames(pPar
2c26f 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 29  se, p, pOuterNC)
2c270 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2c271 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
2c272 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2c273 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  n;.  sqlite3Sele
2c274 63 74 41 64 64 54 79 70 65 49 6e 66 6f 28 70 50  ctAddTypeInfo(pP
2c275 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a  arse, p);.}../*.
2c276 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 67 67  ** Reset the agg
2c277 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74  regate accumulat
2c278 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 67  or..**.** The ag
2c279 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61  gregate accumula
2c27a 74 6f 72 20 69 73 20 61 20 73 65 74 20 6f 66 20  tor is a set of 
2c27b 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 68 61  memory cells tha
2c27c 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74 65 72 6d  t hold.** interm
2c27d 65 64 69 61 74 65 20 72 65 73 75 6c 74 73 20 77  ediate results w
2c27e 68 69 6c 65 20 63 61 6c 63 75 6c 61 74 69 6e 67  hile calculating
2c27f 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20 20   an aggregate.  
2c280 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
2c281 73 69 6d 70 6c 79 20 73 74 6f 72 65 73 20 4e 55  simply stores NU
2c282 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68  LLs in all of th
2c283 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  ose memory cells
2c284 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2c285 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f   resetAccumulato
2c286 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
2c287 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e   AggInfo *pAggIn
2c288 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  fo){.  Vdbe *v =
2c289 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2c28a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
2c28b 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a  t AggInfo_func *
2c28c 70 46 75 6e 63 3b 0a 20 20 69 66 28 20 70 41 67  pFunc;.  if( pAg
2c28d 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b 70 41 67  gInfo->nFunc+pAg
2c28e 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d  gInfo->nColumn==
2c28f 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
2c290 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
2c291 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c  i<pAggInfo->nCol
2c292 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  umn; i++){.    s
2c293 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c294 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2c295 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 69  pAggInfo->aCol[i
2c296 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 66  ].iMem);.  }.  f
2c297 6f 72 28 70 46 75 6e 63 3d 70 41 67 67 49 6e 66  or(pFunc=pAggInf
2c298 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69  o->aFunc, i=0; i
2c299 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
2c29a 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b  ; i++, pFunc++){
2c29b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c29c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2c29d 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e 69 4d 65  l, 0, pFunc->iMe
2c29e 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 46 75 6e  m);.    if( pFun
2c29f 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d 30 20  c->iDistinct>=0 
2c2a0 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2c2a1 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45 78 70 72  E = pFunc->pExpr
2c2a2 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2c2a3 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2c2a4 28 70 45 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  (pE, EP_xIsSelec
2c2a5 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  t) );.      if( 
2c2a6 70 45 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 7c  pE->x.pList==0 |
2c2a7 7c 20 70 45 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  | pE->x.pList->n
2c2a8 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 20  Expr!=1 ){.     
2c2a9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2c2aa 73 67 28 70 50 61 72 73 65 2c 20 22 44 49 53 54  sg(pParse, "DIST
2c2ab 49 4e 43 54 20 61 67 67 72 65 67 61 74 65 73 20  INCT aggregates 
2c2ac 6d 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c  must have exactl
2c2ad 79 20 6f 6e 65 20 22 0a 20 20 20 20 20 20 20 20  y one ".        
2c2ae 20 20 20 22 61 72 67 75 6d 65 6e 74 22 29 3b 0a     "argument");.
2c2af 20 20 20 20 20 20 20 20 70 46 75 6e 63 2d 3e 69          pFunc->i
2c2b0 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20  Distinct = -1;. 
2c2b1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c2b2 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
2c2b3 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
2c2b4 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
2c2b5 73 65 2c 20 70 45 2d 3e 78 2e 70 4c 69 73 74 29  se, pE->x.pList)
2c2b6 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c2b7 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2c2b8 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
2c2b9 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63   pFunc->iDistinc
2c2ba 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20  t, 0, 0,.       
2c2bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2bc 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2c2bd 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
2c2be 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d  ANDOFF);.      }
2c2bf 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2c2c0 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f  .** Invoke the O
2c2c1 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70  P_AggFinalize op
2c2c2 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 61  code for every a
2c2c3 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2c2c4 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67 67 49  n.** in the AggI
2c2c5 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  nfo structure..*
2c2c6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69  /.static void fi
2c2c7 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f  nalizeAggFunctio
2c2c8 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ns(Parse *pParse
2c2c9 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49  , AggInfo *pAggI
2c2ca 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  nfo){.  Vdbe *v 
2c2cb 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2c2cc 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
2c2cd 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20  ct AggInfo_func 
2c2ce 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  *pF;.  for(i=0, 
2c2cf 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  pF=pAggInfo->aFu
2c2d0 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  nc; i<pAggInfo->
2c2d1 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b  nFunc; i++, pF++
2c2d2 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  ){.    ExprList 
2c2d3 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78  *pList = pF->pEx
2c2d4 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
2c2d5 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2c2d6 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45  sProperty(pF->pE
2c2d7 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2c2d8 74 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  t) );.    sqlite
2c2d9 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2c2da 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70 46 2d 3e  P_AggFinal, pF->
2c2db 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f 20 70 4c  iMem, pList ? pL
2c2dc 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20  ist->nExpr : 0, 
2c2dd 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
2c2de 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29           (void*)
2c2df 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55  pF->pFunc, P4_FU
2c2e0 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  NCDEF);.  }.}../
2c2e1 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68 65 20  *.** Update the 
2c2e2 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65 6d 6f  accumulator memo
2c2e3 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 6e 20  ry cells for an 
2c2e4 61 67 67 72 65 67 61 74 65 20 62 61 73 65 64 20  aggregate based 
2c2e5 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  on.** the curren
2c2e6 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f  t cursor positio
2c2e7 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
2c2e8 64 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61  d updateAccumula
2c2e9 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  tor(Parse *pPars
2c2ea 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  e, AggInfo *pAgg
2c2eb 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76  Info){.  Vdbe *v
2c2ec 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2c2ed 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
2c2ee 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
2c2ef 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74 20 41   *pF;.  struct A
2c2f0 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 3b 0a  ggInfo_col *pC;.
2c2f1 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72  .  pAggInfo->dir
2c2f2 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 73  ectMode = 1;.  s
2c2f3 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c2f4 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c2f5 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67  for(i=0, pF=pAgg
2c2f6 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70  Info->aFunc; i<p
2c2f7 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
2c2f8 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20  i++, pF++){.    
2c2f9 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 69 6e  int nArg;.    in
2c2fa 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b 0a  t addrNext = 0;.
2c2fb 20 20 20 20 69 6e 74 20 72 65 67 41 67 67 3b 0a      int regAgg;.
2c2fc 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
2c2fd 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
2c2fe 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 73  >x.pList;.    as
2c2ff 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2c300 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72  operty(pF->pExpr
2c301 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c302 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  );.    if( pList
2c303 20 29 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d   ){.      nArg =
2c304 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20   pList->nExpr;. 
2c305 20 20 20 20 20 72 65 67 41 67 67 20 3d 20 73 71       regAgg = sq
2c306 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
2c307 65 28 70 50 61 72 73 65 2c 20 6e 41 72 67 29 3b  e(pParse, nArg);
2c308 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c309 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
2c30a 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 72 65  Parse, pList, re
2c30b 67 41 67 67 2c 20 30 29 3b 0a 20 20 20 20 7d 65  gAgg, 0);.    }e
2c30c 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41 72 67 20  lse{.      nArg 
2c30d 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 67 41 67  = 0;.      regAg
2c30e 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  g = 0;.    }.   
2c30f 20 69 66 28 20 70 46 2d 3e 69 44 69 73 74 69 6e   if( pF->iDistin
2c310 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  ct>=0 ){.      a
2c311 64 64 72 4e 65 78 74 20 3d 20 73 71 6c 69 74 65  ddrNext = sqlite
2c312 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2c313 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2c314 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20 20 20 20   nArg==1 );.    
2c315 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70    codeDistinct(p
2c316 50 61 72 73 65 2c 20 70 46 2d 3e 69 44 69 73 74  Parse, pF->iDist
2c317 69 6e 63 74 2c 20 61 64 64 72 4e 65 78 74 2c 20  inct, addrNext, 
2c318 31 2c 20 72 65 67 41 67 67 29 3b 0a 20 20 20 20  1, regAgg);.    
2c319 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 70 46  }.    if( pF->pF
2c31a 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  unc->flags & SQL
2c31b 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
2c31c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  L ){.      CollS
2c31d 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20  eq *pColl = 0;. 
2c31e 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
2c31f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
2c320 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
2c321 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69       assert( pLi
2c322 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 70 4c 69  st!=0 );  /* pLi
2c323 73 74 21 3d 30 20 69 66 20 70 46 2d 3e 70 46 75  st!=0 if pF->pFu
2c324 6e 63 20 68 61 73 20 4e 45 45 44 43 4f 4c 4c 20  nc has NEEDCOLL 
2c325 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  */.      for(j=0
2c326 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
2c327 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41  ; !pColl && j<nA
2c328 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rg; j++, pItem++
2c329 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2c32a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2c32b 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49  llSeq(pParse, pI
2c32c 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
2c32d 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
2c32e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
2c32f 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d   pColl = pParse-
2c330 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a  >db->pDfltColl;.
2c331 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2c332 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c333 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30  v, OP_CollSeq, 0
2c334 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29  , 0, 0, (char *)
2c335 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45  pColl, P4_COLLSE
2c336 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  Q);.    }.    sq
2c337 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c338 76 2c 20 4f 50 5f 41 67 67 53 74 65 70 2c 20 30  v, OP_AggStep, 0
2c339 2c 20 72 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d  , regAgg, pF->iM
2c33a 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  em,.            
2c33b 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a            (void*
2c33c 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46  )pF->pFunc, P4_F
2c33d 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 73 71 6c  UNCDEF);.    sql
2c33e 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2c33f 28 76 2c 20 28 75 38 29 6e 41 72 67 29 3b 0a 20  (v, (u8)nArg);. 
2c340 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2c341 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
2c342 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72 67 29  e, regAgg, nArg)
2c343 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2c344 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2c345 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2c346 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20  Agg, nArg);.    
2c347 69 66 28 20 61 64 64 72 4e 65 78 74 20 29 7b 0a  if( addrNext ){.
2c348 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c349 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2c34a 20 61 64 64 72 4e 65 78 74 29 3b 0a 20 20 20 20   addrNext);.    
2c34b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2c34c 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2c34d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
2c34e 28 69 3d 30 2c 20 70 43 3d 70 41 67 67 49 6e 66  (i=0, pC=pAggInf
2c34f 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49  o->aCol; i<pAggI
2c350 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f  nfo->nAccumulato
2c351 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20  r; i++, pC++){. 
2c352 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2c353 64 65 28 70 50 61 72 73 65 2c 20 70 43 2d 3e 70  de(pParse, pC->p
2c354 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b  Expr, pC->iMem);
2c355 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d  .  }.  pAggInfo-
2c356 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b  >directMode = 0;
2c357 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  .  sqlite3ExprCa
2c358 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29  cheClear(pParse)
2c359 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2c35a 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ate code for the
2c35b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2c35c 74 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 70  t given in the p
2c35d 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a 2a 2a 0a   argument.  .**.
2c35e 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  ** The results a
2c35f 72 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69  re distributed i
2c360 6e 20 76 61 72 69 6f 75 73 20 77 61 79 73 20 64  n various ways d
2c361 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a  epending on the.
2c362 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  ** contents of t
2c363 68 65 20 53 65 6c 65 63 74 44 65 73 74 20 73 74  he SelectDest st
2c364 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20  ructure pointed 
2c365 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 70  to by argument p
2c366 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f  Dest.** as follo
2c367 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 70 44  ws:.**.**     pD
2c368 65 73 74 2d 3e 65 44 65 73 74 20 20 20 20 52 65  est->eDest    Re
2c369 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d 2d 2d 2d  sult.**     ----
2c36a 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d  --------    ----
2c36b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c36c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c36d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 53  -------.**     S
2c36e 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 47  RT_Output      G
2c36f 65 6e 65 72 61 74 65 20 61 20 72 6f 77 20 6f 66  enerate a row of
2c370 20 6f 75 74 70 75 74 20 28 75 73 69 6e 67 20 74   output (using t
2c371 68 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 0a  he OP_ResultRow.
2c372 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c373 20 20 20 20 20 20 20 6f 70 63 6f 64 65 29 20 66         opcode) f
2c374 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74  or each row in t
2c375 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a  he result set..*
2c376 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4d 65 6d  *.**     SRT_Mem
2c377 20 20 20 20 20 20 20 20 20 4f 6e 6c 79 20 76 61           Only va
2c378 6c 69 64 20 69 66 20 74 68 65 20 72 65 73 75 6c  lid if the resul
2c379 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 6f  t is a single co
2c37a 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20  lumn..**        
2c37b 20 20 20 20 20 20 20 20 20 20 20 20 20 53 74 6f               Sto
2c37c 72 65 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c  re the first col
2c37d 75 6d 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74  umn of the first
2c37e 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a 2a 20 20   result row.**  
2c37f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c380 20 20 20 69 6e 20 72 65 67 69 73 74 65 72 20 70     in register p
2c381 44 65 73 74 2d 3e 69 50 61 72 6d 20 74 68 65 6e  Dest->iParm then
2c382 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 72 65 73   abandon the res
2c383 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2c384 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
2c385 71 75 65 72 79 2e 20 20 54 68 69 73 20 64 65 73  query.  This des
2c386 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c 69 65 73  tination implies
2c387 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a   "LIMIT 1"..**.*
2c388 2a 20 20 20 20 20 53 52 54 5f 53 65 74 20 20 20  *     SRT_Set   
2c389 20 20 20 20 20 20 54 68 65 20 72 65 73 75 6c 74        The result
2c38a 20 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c   must be a singl
2c38b 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74 6f 72 65  e column.  Store
2c38c 20 65 61 63 68 0a 2a 2a 20 20 20 20 20 20 20 20   each.**        
2c38d 20 20 20 20 20 20 20 20 20 20 20 20 20 72 6f 77               row
2c38e 20 6f 66 20 72 65 73 75 6c 74 20 61 73 20 74 68   of result as th
2c38f 65 20 6b 65 79 20 69 6e 20 74 61 62 6c 65 20 70  e key in table p
2c390 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a  Dest->iParm. .**
2c391 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c392 20 20 20 20 20 41 70 70 6c 79 20 74 68 65 20 61       Apply the a
2c393 66 66 69 6e 69 74 79 20 70 44 65 73 74 2d 3e 61  ffinity pDest->a
2c394 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 73  ffinity before s
2c395 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  toring.**       
2c396 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
2c397 73 75 6c 74 73 2e 20 20 55 73 65 64 20 74 6f 20  sults.  Used to 
2c398 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e 20 28 53  implement "IN (S
2c399 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a  ELECT ...)"..**.
2c39a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e 69 6f 6e  **     SRT_Union
2c39b 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65 73         Store res
2c39c 75 6c 74 73 20 61 73 20 61 20 6b 65 79 20 69 6e  ults as a key in
2c39d 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
2c39e 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  le pDest->iParm.
2c39f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45  .**.**     SRT_E
2c3a0 78 63 65 70 74 20 20 20 20 20 20 52 65 6d 6f 76  xcept      Remov
2c3a1 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 74  e results from t
2c3a2 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
2c3a3 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  le pDest->iParm.
2c3a4 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 54  .**.**     SRT_T
2c3a5 61 62 6c 65 20 20 20 20 20 20 20 53 74 6f 72 65  able       Store
2c3a6 20 72 65 73 75 6c 74 73 20 69 6e 20 74 65 6d 70   results in temp
2c3a7 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44 65 73  orary table pDes
2c3a8 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20 20 20 20  t->iParm..**    
2c3a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3aa 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20 53 52   This is like SR
2c3ab 54 5f 45 70 68 65 6d 54 61 62 20 65 78 63 65 70  T_EphemTab excep
2c3ac 74 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  t that the table
2c3ad 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2c3ae 20 20 20 20 20 20 20 20 69 73 20 61 73 73 75 6d          is assum
2c3af 65 64 20 74 6f 20 61 6c 72 65 61 64 79 20 62 65  ed to already be
2c3b0 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20   open..**.**    
2c3b1 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20   SRT_EphemTab   
2c3b2 20 43 72 65 61 74 65 20 61 6e 20 74 65 6d 70 6f   Create an tempo
2c3b3 72 61 72 79 20 74 61 62 6c 65 20 70 44 65 73 74  rary table pDest
2c3b4 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73 74 6f 72  ->iParm and stor
2c3b5 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2c3b6 20 20 20 20 20 20 20 20 20 74 68 65 20 72 65 73           the res
2c3b7 75 6c 74 20 74 68 65 72 65 2e 20 54 68 65 20 63  ult there. The c
2c3b8 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 6f 70  ursor is left op
2c3b9 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20 20 20 20  en after.**     
2c3ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3bb 72 65 74 75 72 6e 69 6e 67 2e 20 20 54 68 69 73  returning.  This
2c3bc 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 54 61 62   is like SRT_Tab
2c3bd 6c 65 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a  le except that.*
2c3be 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c3bf 20 20 20 20 20 20 74 68 69 73 20 64 65 73 74 69        this desti
2c3c0 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f 50 5f 4f  nation uses OP_O
2c3c1 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 6f 20  penEphemeral to 
2c3c2 63 72 65 61 74 65 0a 2a 2a 20 20 20 20 20 20 20  create.**       
2c3c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
2c3c4 65 20 74 61 62 6c 65 20 66 69 72 73 74 2e 0a 2a  e table first..*
2c3c5 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 43 6f 72  *.**     SRT_Cor
2c3c6 6f 75 74 69 6e 65 20 20 20 47 65 6e 65 72 61 74  outine   Generat
2c3c7 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74  e a co-routine t
2c3c8 68 61 74 20 72 65 74 75 72 6e 73 20 61 20 6e 65  hat returns a ne
2c3c9 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20  w row of.**     
2c3ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3cb 72 65 73 75 6c 74 73 20 65 61 63 68 20 74 69 6d  results each tim
2c3cc 65 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e  e it is invoked.
2c3cd 20 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e    The entry poin
2c3ce 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2c3cf 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
2c3d0 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20 73 74  co-routine is st
2c3d1 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
2c3d2 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a   pDest->iParm..*
2c3d3 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 69  *.**     SRT_Exi
2c3d4 73 74 73 20 20 20 20 20 20 53 74 6f 72 65 20 61  sts      Store a
2c3d5 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c   1 in memory cel
2c3d6 6c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 69  l pDest->iParm i
2c3d7 66 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  f the result.** 
2c3d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3d9 20 20 20 20 73 65 74 20 69 73 20 6e 6f 74 20 65      set is not e
2c3da 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mpty..**.**     
2c3db 53 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 20  SRT_Discard     
2c3dc 54 68 72 6f 77 20 74 68 65 20 72 65 73 75 6c 74  Throw the result
2c3dd 73 20 61 77 61 79 2e 20 20 54 68 69 73 20 69 73  s away.  This is
2c3de 20 75 73 65 64 20 62 79 20 53 45 4c 45 43 54 0a   used by SELECT.
2c3df 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2c3e0 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
2c3e1 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72  s within trigger
2c3e2 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20 70 75 72  s whose only pur
2c3e3 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20 20 20 20  pose is.**      
2c3e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2c3e5 68 65 20 73 69 64 65 2d 65 66 66 65 63 74 73 20  he side-effects 
2c3e6 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  of functions..**
2c3e7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c3e8 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
2c3e9 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  ber of errors.  
2c3ea 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72  If any errors ar
2c3eb 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64  e.** encountered
2c3ec 2c 20 74 68 65 6e 20 61 6e 20 61 70 70 72 6f 70  , then an approp
2c3ed 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73  riate error mess
2c3ee 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a  age is left in.*
2c3ef 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  * pParse->zErrMs
2c3f0 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  g..**.** This ro
2c3f1 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66  utine does NOT f
2c3f2 72 65 65 20 74 68 65 20 53 65 6c 65 63 74 20 73  ree the Select s
2c3f3 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
2c3f4 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  in.  The.** call
2c3f5 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65  ing function nee
2c3f6 64 73 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a  ds to do that..*
2c3f7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2c3f8 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
2c3f9 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ct(.  Parse *pPa
2c3fa 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
2c3fb 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2c3fc 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2c3fd 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
2c3fe 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2c3ff 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
2c400 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ed. */.  SelectD
2c401 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 20  est *pDest      
2c402 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
2c403 74 68 20 74 68 65 20 71 75 65 72 79 20 72 65 73  th the query res
2c404 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ults */.){.  int
2c405 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
2c406 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2c407 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72 65 49  ters */.  WhereI
2c408 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
2c409 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20   /* Return from 
2c40a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2c40b 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  n() */.  Vdbe *v
2c40c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c40d 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d  /* The virtual m
2c40e 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e  achine under con
2c40f 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  struction */.  i
2c410 6e 74 20 69 73 41 67 67 3b 20 20 20 20 20 20 20  nt isAgg;       
2c411 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
2c412 72 20 73 65 6c 65 63 74 20 6c 69 73 74 73 20 6c  r select lists l
2c413 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29 22 20 2a  ike "count(*)" *
2c414 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2c415 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69  List;      /* Li
2c416 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f  st of columns to
2c417 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a 20 20 53   extract. */.  S
2c418 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2c419 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ;     /* List of
2c41a 20 74 61 62 6c 65 73 20 74 6f 20 73 65 6c 65 63   tables to selec
2c41b 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72  t from */.  Expr
2c41c 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
2c41d 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2c41e 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
2c41f 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69  NULL */.  ExprLi
2c420 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20  st *pOrderBy;   
2c421 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2c422 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
2c423 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c   NULL */.  ExprL
2c424 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
2c425 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42    /* The GROUP B
2c426 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  Y clause.  May b
2c427 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72  e NULL */.  Expr
2c428 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 20 20   *pHaving;      
2c429 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49 4e 47     /* The HAVING
2c42a 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
2c42b 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69   NULL */.  int i
2c42c 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20  sDistinct;      
2c42d 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
2c42e 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
2c42f 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
2c430 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74 3b 20    int distinct; 
2c431 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2c432 65 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65  e to use for the
2c433 20 64 69 73 74 69 6e 63 74 20 73 65 74 20 2a 2f   distinct set */
2c434 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 20 20  .  int rc = 1;  
2c435 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
2c436 75 65 20 74 6f 20 72 65 74 75 72 6e 20 66 72 6f  ue to return fro
2c437 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  m this function 
2c438 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 6f 72  */.  int addrSor
2c439 74 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 41  tIndex;     /* A
2c43a 64 64 72 65 73 73 20 6f 66 20 61 6e 20 4f 50 5f  ddress of an OP_
2c43b 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
2c43c 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 41  struction */.  A
2c43d 67 67 49 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b  ggInfo sAggInfo;
2c43e 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
2c43f 74 69 6f 6e 20 75 73 65 64 20 62 79 20 61 67 67  tion used by agg
2c440 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20 2a  regate queries *
2c441 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b 20 20 20  /.  int iEnd;   
2c442 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
2c443 64 72 65 73 73 20 6f 66 20 74 68 65 20 65 6e 64  dress of the end
2c444 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f   of the query */
2c445 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
2c446 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2c447 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2c448 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62 20 3d 20  tion */..  db = 
2c449 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
2c44a 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  ( p==0 || db->ma
2c44b 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50  llocFailed || pP
2c44c 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
2c44d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2c44e 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
2c44f 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
2c450 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c  QLITE_SELECT, 0,
2c451 20 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e 20   0, 0) ) return 
2c452 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 41 67  1;.  memset(&sAg
2c453 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66  gInfo, 0, sizeof
2c454 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a 0a 20 20  (sAggInfo));..  
2c455 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64  if( IgnorableOrd
2c456 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20  erby(pDest) ){. 
2c457 20 20 20 61 73 73 65 72 74 28 70 44 65 73 74 2d     assert(pDest-
2c458 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 69 73  >eDest==SRT_Exis
2c459 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65  ts || pDest->eDe
2c45a 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c  st==SRT_Union ||
2c45b 20 0a 20 20 20 20 20 20 20 20 20 20 20 70 44 65   .           pDe
2c45c 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
2c45d 78 63 65 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e  xcept || pDest->
2c45e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61  eDest==SRT_Disca
2c45f 72 64 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 4f  rd);.    /* If O
2c460 52 44 45 52 20 42 59 20 6d 61 6b 65 73 20 6e 6f  RDER BY makes no
2c461 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20 74   difference in t
2c462 68 65 20 6f 75 74 70 75 74 20 74 68 65 6e 20 6e  he output then n
2c463 65 69 74 68 65 72 20 64 6f 65 73 0a 20 20 20 20  either does.    
2c464 2a 2a 20 44 49 53 54 49 4e 43 54 20 73 6f 20 69  ** DISTINCT so i
2c465 74 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64  t can be removed
2c466 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 20 73 71 6c   too. */.    sql
2c467 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2c468 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72  te(db, p->pOrder
2c469 42 79 29 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64  By);.    p->pOrd
2c46a 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d  erBy = 0;.    p-
2c46b 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46  >selFlags &= ~SF
2c46c 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20  _Distinct;.  }. 
2c46d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
2c46e 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29  ep(pParse, p, 0)
2c46f 3b 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70  ;.  pOrderBy = p
2c470 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 54  ->pOrderBy;.  pT
2c471 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
2c472 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e  ;.  pEList = p->
2c473 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 50  pEList;.  if( pP
2c474 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
2c475 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2c476 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63  {.    goto selec
2c477 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 73 41  t_end;.  }.  isA
2c478 67 67 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67  gg = (p->selFlag
2c479 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65  s & SF_Aggregate
2c47a 29 21 3d 30 3b 0a 20 20 61 73 73 65 72 74 28 20  )!=0;.  assert( 
2c47b 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20  pEList!=0 );..  
2c47c 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74  /* Begin generat
2c47d 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ing code..  */. 
2c47e 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2c47f 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2c480 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73  f( v==0 ) goto s
2c481 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a  elect_end;..  /*
2c482 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
2c483 6f 72 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69  or all sub-queri
2c484 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
2c485 6c 61 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21  lause.  */.#if !
2c486 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2c487 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2c488 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2c489 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f  _OMIT_VIEW).  fo
2c48a 72 28 69 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f  r(i=0; !p->pPrio
2c48b 72 20 26 26 20 69 3c 70 54 61 62 4c 69 73 74 2d  r && i<pTabList-
2c48c 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
2c48d 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2c48e 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
2c48f 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20  TabList->a[i];. 
2c490 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65     SelectDest de
2c491 73 74 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a  st;.    Select *
2c492 70 53 75 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53  pSub = pItem->pS
2c493 65 6c 65 63 74 3b 0a 20 20 20 20 69 6e 74 20 69  elect;.    int i
2c494 73 41 67 67 53 75 62 3b 0a 0a 20 20 20 20 69 66  sAggSub;..    if
2c495 28 20 70 53 75 62 3d 3d 30 20 7c 7c 20 70 49 74  ( pSub==0 || pIt
2c496 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20  em->isPopulated 
2c497 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20  ) continue;..   
2c498 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 50 61   /* Increment Pa
2c499 72 73 65 2e 6e 48 65 69 67 68 74 20 62 79 20 74  rse.nHeight by t
2c49a 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65  he height of the
2c49b 20 6c 61 72 67 65 73 74 20 65 78 70 72 65 73 73   largest express
2c49c 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72 65 65 20  ion.    ** tree 
2c49d 72 65 66 65 72 65 64 20 74 6f 20 62 79 20 74 68  refered to by th
2c49e 69 73 2c 20 74 68 65 20 70 61 72 65 6e 74 20 73  is, the parent s
2c49f 65 6c 65 63 74 2e 20 54 68 65 20 63 68 69 6c 64  elect. The child
2c4a0 20 73 65 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d   select.    ** m
2c4a1 61 79 20 63 6f 6e 74 61 69 6e 20 65 78 70 72 65  ay contain expre
2c4a2 73 73 69 6f 6e 20 74 72 65 65 73 20 6f 66 20 61  ssion trees of a
2c4a3 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53  t most.    ** (S
2c4a4 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
2c4a5 45 50 54 48 2d 50 61 72 73 65 2e 6e 48 65 69 67  EPTH-Parse.nHeig
2c4a6 68 74 29 20 68 65 69 67 68 74 2e 20 54 68 69 73  ht) height. This
2c4a7 20 69 73 20 61 20 62 69 74 0a 20 20 20 20 2a 2a   is a bit.    **
2c4a8 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76 61 74 69   more conservati
2c4a9 76 65 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72  ve than necessar
2c4aa 79 2c 20 62 75 74 20 6d 75 63 68 20 65 61 73 69  y, but much easi
2c4ab 65 72 20 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e  er than enforcin
2c4ac 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65 78 61 63  g.    ** an exac
2c4ad 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a  t limit..    */.
2c4ae 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69      pParse->nHei
2c4af 67 68 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 65  ght += sqlite3Se
2c4b0 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70  lectExprHeight(p
2c4b1 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  );..    /* Check
2c4b2 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 73   to see if the s
2c4b3 75 62 71 75 65 72 79 20 63 61 6e 20 62 65 20 61  ubquery can be a
2c4b4 62 73 6f 72 62 65 64 20 69 6e 74 6f 20 74 68 65  bsorbed into the
2c4b5 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20   parent. */.    
2c4b6 69 73 41 67 67 53 75 62 20 3d 20 28 70 53 75 62  isAggSub = (pSub
2c4b7 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2c4b8 41 67 67 72 65 67 61 74 65 29 21 3d 30 3b 0a 20  Aggregate)!=0;. 
2c4b9 20 20 20 69 66 28 20 66 6c 61 74 74 65 6e 53 75     if( flattenSu
2c4ba 62 71 75 65 72 79 28 70 50 61 72 73 65 2c 20 70  bquery(pParse, p
2c4bb 2c 20 69 2c 20 69 73 41 67 67 2c 20 69 73 41 67  , i, isAgg, isAg
2c4bc 67 53 75 62 29 20 29 7b 0a 20 20 20 20 20 20 69  gSub) ){.      i
2c4bd 66 28 20 69 73 41 67 67 53 75 62 20 29 7b 0a 20  f( isAggSub ){. 
2c4be 20 20 20 20 20 20 20 69 73 41 67 67 20 3d 20 31         isAgg = 1
2c4bf 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 73 65 6c  ;.        p->sel
2c4c0 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72  Flags |= SF_Aggr
2c4c1 65 67 61 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20  egate;.      }. 
2c4c2 20 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20       i = -1;.   
2c4c3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
2c4c4 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
2c4c5 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45  nit(&dest, SRT_E
2c4c6 70 68 65 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e  phemTab, pItem->
2c4c7 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  iCursor);.      
2c4c8 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69  assert( pItem->i
2c4c9 73 50 6f 70 75 6c 61 74 65 64 3d 3d 30 20 29 3b  sPopulated==0 );
2c4ca 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
2c4cb 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 75  lect(pParse, pSu
2c4cc 62 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20  b, &dest);.     
2c4cd 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61   pItem->isPopula
2c4ce 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ted = 1;.    }. 
2c4cf 20 20 20 69 66 28 20 2f 2a 70 50 61 72 73 65 2d     if( /*pParse-
2c4d0 3e 6e 45 72 72 20 7c 7c 2a 2f 20 64 62 2d 3e 6d  >nErr ||*/ db->m
2c4d1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2c4d2 20 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74       goto select
2c4d3 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _end;.    }.    
2c4d4 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20  pParse->nHeight 
2c4d5 2d 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  -= sqlite3Select
2c4d6 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a 20  ExprHeight(p);. 
2c4d7 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d     pTabList = p-
2c4d8 3e 70 53 72 63 3b 0a 20 20 20 20 69 66 28 20 21  >pSrc;.    if( !
2c4d9 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79  IgnorableOrderby
2c4da 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 20  (pDest) ){.     
2c4db 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
2c4dc 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 7d 0a 20  OrderBy;.    }. 
2c4dd 20 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d   }.  pEList = p-
2c4de 3e 70 45 4c 69 73 74 3b 0a 23 65 6e 64 69 66 0a  >pEList;.#endif.
2c4df 20 20 70 57 68 65 72 65 20 3d 20 70 2d 3e 70 57    pWhere = p->pW
2c4e0 68 65 72 65 3b 0a 20 20 70 47 72 6f 75 70 42 79  here;.  pGroupBy
2c4e1 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a   = p->pGroupBy;.
2c4e2 20 20 70 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70    pHaving = p->p
2c4e3 48 61 76 69 6e 67 3b 0a 20 20 69 73 44 69 73 74  Having;.  isDist
2c4e4 69 6e 63 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c  inct = (p->selFl
2c4e5 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
2c4e6 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64 65 66 20  t)!=0;..#ifndef 
2c4e7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
2c4e8 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a  OUND_SELECT.  /*
2c4e9 20 49 66 20 74 68 65 72 65 20 69 73 20 61 72 65   If there is are
2c4ea 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 71   a sequence of q
2c4eb 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20 65  ueries, do the e
2c4ec 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72 73  arlier ones firs
2c4ed 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  t..  */.  if( p-
2c4ee 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69  >pPrior ){.    i
2c4ef 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  f( p->pRightmost
2c4f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 65 6c  ==0 ){.      Sel
2c4f1 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69  ect *pLoop, *pRi
2c4f2 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ght = 0;.      i
2c4f3 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
2c4f4 20 20 69 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a    int mxSelect;.
2c4f5 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d        for(pLoop=
2c4f6 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d  p; pLoop; pLoop=
2c4f7 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63  pLoop->pPrior, c
2c4f8 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  nt++){.        p
2c4f9 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  Loop->pRightmost
2c4fa 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 70 4c   = p;.        pL
2c4fb 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69  oop->pNext = pRi
2c4fc 67 68 74 3b 0a 20 20 20 20 20 20 20 20 70 52 69  ght;.        pRi
2c4fd 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20  ght = pLoop;.   
2c4fe 20 20 20 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c     }.      mxSel
2c4ff 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  ect = db->aLimit
2c500 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
2c501 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a  MPOUND_SELECT];.
2c502 20 20 20 20 20 20 69 66 28 20 6d 78 53 65 6c 65        if( mxSele
2c503 63 74 20 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65  ct && cnt>mxSele
2c504 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
2c505 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2c506 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
2c507 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e  terms in compoun
2c508 64 20 53 45 4c 45 43 54 22 29 3b 0a 20 20 20 20  d SELECT");.    
2c509 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2c50a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2c50b 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65  return multiSele
2c50c 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44  ct(pParse, p, pD
2c50d 65 73 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  est);.  }.#endif
2c50e 0a 0a 20 20 2f 2a 20 49 66 20 77 72 69 74 69 6e  ..  /* If writin
2c50f 67 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67  g to memory or g
2c510 65 6e 65 72 61 74 69 6e 67 20 61 20 73 65 74 0a  enerating a set.
2c511 20 20 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67    ** only a sing
2c512 6c 65 20 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65  le column may be
2c513 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69   output..  */.#i
2c514 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c515 54 5f 53 55 42 51 55 45 52 59 0a 20 20 69 66 28  T_SUBQUERY.  if(
2c516 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f   checkForMultiCo
2c517 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72 28  lumnSelectError(
2c518 70 50 61 72 73 65 2c 20 70 44 65 73 74 2c 20 70  pParse, pDest, p
2c519 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b  EList->nExpr) ){
2c51a 0a 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74  .    goto select
2c51b 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  _end;.  }.#endif
2c51c 0a 0a 20 20 2f 2a 20 49 66 20 70 6f 73 73 69 62  ..  /* If possib
2c51d 6c 65 2c 20 72 65 77 72 69 74 65 20 74 68 65 20  le, rewrite the 
2c51e 71 75 65 72 79 20 74 6f 20 75 73 65 20 47 52 4f  query to use GRO
2c51f 55 50 20 42 59 20 69 6e 73 74 65 61 64 20 6f 66  UP BY instead of
2c520 20 44 49 53 54 49 4e 43 54 2e 0a 20 20 2a 2a 20   DISTINCT..  ** 
2c521 47 52 4f 55 50 20 42 59 20 6d 69 67 68 74 20 75  GROUP BY might u
2c522 73 65 20 61 6e 20 69 6e 64 65 78 2c 20 44 49 53  se an index, DIS
2c523 54 49 4e 43 54 20 6e 65 76 65 72 20 64 6f 65 73  TINCT never does
2c524 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2c525 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20   p->pGroupBy==0 
2c526 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  || (p->selFlags 
2c527 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21  & SF_Aggregate)!
2c528 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 2d 3e  =0 );.  if( (p->
2c529 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44  selFlags & (SF_D
2c52a 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65  istinct|SF_Aggre
2c52b 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69  gate))==SF_Disti
2c52c 6e 63 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 47  nct ){.    p->pG
2c52d 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33  roupBy = sqlite3
2c52e 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
2c52f 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29 3b 0a 20  p->pEList, 0);. 
2c530 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d     pGroupBy = p-
2c531 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 70  >pGroupBy;.    p
2c532 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53  ->selFlags &= ~S
2c533 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 20 20  F_Distinct;.    
2c534 69 73 44 69 73 74 69 6e 63 74 20 3d 20 30 3b 0a  isDistinct = 0;.
2c535 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2c536 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
2c537 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74  Y clause, then t
2c538 68 69 73 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a  his sorting.  **
2c539 20 69 6e 64 65 78 20 6d 69 67 68 74 20 65 6e 64   index might end
2c53a 20 75 70 20 62 65 69 6e 67 20 75 6e 75 73 65 64   up being unused
2c53b 20 69 66 20 74 68 65 20 64 61 74 61 20 63 61 6e   if the data can
2c53c 20 62 65 20 0a 20 20 2a 2a 20 65 78 74 72 61 63   be .  ** extrac
2c53d 74 65 64 20 69 6e 20 70 72 65 2d 73 6f 72 74 65  ted in pre-sorte
2c53e 64 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 61  d order.  If tha
2c53f 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
2c540 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f  hen the.  ** OP_
2c541 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
2c542 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62  struction will b
2c543 65 20 63 68 61 6e 67 65 64 20 74 6f 20 61 6e 20  e changed to an 
2c544 4f 50 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a  OP_Noop once.  *
2c545 2a 20 77 65 20 66 69 67 75 72 65 20 6f 75 74 20  * we figure out 
2c546 74 68 61 74 20 74 68 65 20 73 6f 72 74 69 6e 67  that the sorting
2c547 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e 65   index is not ne
2c548 65 64 65 64 2e 20 20 54 68 65 20 61 64 64 72 53  eded.  The addrS
2c549 6f 72 74 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61  ortIndex.  ** va
2c54a 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74  riable is used t
2c54b 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 68 61  o facilitate tha
2c54c 74 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20  t change..  */. 
2c54d 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2c54e 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
2c54f 65 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79  eyInfo;.    pKey
2c550 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
2c551 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
2c552 65 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  e, pOrderBy);.  
2c553 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75    pOrderBy->iECu
2c554 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  rsor = pParse->n
2c555 54 61 62 2b 2b 3b 0a 20 20 20 20 70 2d 3e 61 64  Tab++;.    p->ad
2c556 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20  drOpenEphm[2] = 
2c557 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d 0a  addrSortIndex =.
2c558 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c559 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
2c55a 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20  enEphemeral,.   
2c55b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c55c 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79          pOrderBy
2c55d 2d 3e 69 45 43 75 72 73 6f 72 2c 20 70 4f 72 64  ->iECursor, pOrd
2c55e 65 72 42 79 2d 3e 6e 45 78 70 72 2b 32 2c 20 30  erBy->nExpr+2, 0
2c55f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2c560 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
2c561 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
2c562 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
2c563 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2c564 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d 20  addrSortIndex = 
2c565 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  -1;.  }..  /* If
2c566 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 64   the output is d
2c567 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20 74 65  estined for a te
2c568 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 6f  mporary table, o
2c569 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 0a  pen that table..
2c56a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73 74    */.  if( pDest
2c56b 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  ->eDest==SRT_Eph
2c56c 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c  emTab ){.    sql
2c56d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c56e 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2c56f 61 6c 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  al, pDest->iParm
2c570 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
2c571 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
2c572 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a 20 20 2a  the limiter..  *
2c573 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71 6c 69 74  /.  iEnd = sqlit
2c574 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2c575 76 29 3b 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d  v);.  computeLim
2c576 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72  itRegisters(pPar
2c577 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b 0a 0a 20  se, p, iEnd);.. 
2c578 20 2f 2a 20 4f 70 65 6e 20 61 20 76 69 72 74 75   /* Open a virtu
2c579 61 6c 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  al index to use 
2c57a 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63 74  for the distinct
2c57b 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   set..  */.  if(
2c57c 20 69 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20   isDistinct ){. 
2c57d 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
2c57e 49 6e 66 6f 3b 0a 20 20 20 20 61 73 73 65 72 74  Info;.    assert
2c57f 28 20 69 73 41 67 67 20 7c 7c 20 70 47 72 6f 75  ( isAgg || pGrou
2c580 70 42 79 20 29 3b 0a 20 20 20 20 64 69 73 74 69  pBy );.    disti
2c581 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
2c582 61 62 2b 2b 3b 0a 20 20 20 20 70 4b 65 79 49 6e  ab++;.    pKeyIn
2c583 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
2c584 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2c585 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20   p->pEList);.   
2c586 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c587 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
2c588 65 6d 65 72 61 6c 2c 20 64 69 73 74 69 6e 63 74  emeral, distinct
2c589 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
2c58a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c58b 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2c58c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2c58d 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OFF);.  }else{. 
2c58e 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20 2d 31     distinct = -1
2c58f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 67 67 72  ;.  }..  /* Aggr
2c590 65 67 61 74 65 20 61 6e 64 20 6e 6f 6e 2d 61 67  egate and non-ag
2c591 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20  gregate queries 
2c592 61 72 65 20 68 61 6e 64 6c 65 64 20 64 69 66 66  are handled diff
2c593 65 72 65 6e 74 6c 79 20 2a 2f 0a 20 20 69 66 28  erently */.  if(
2c594 20 21 69 73 41 67 67 20 26 26 20 70 47 72 6f 75   !isAgg && pGrou
2c595 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  pBy==0 ){.    /*
2c596 20 54 68 69 73 20 63 61 73 65 20 69 73 20 66 6f   This case is fo
2c597 72 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20  r non-aggregate 
2c598 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2a 20 42  queries.    ** B
2c599 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  egin the databas
2c59a 65 20 73 63 61 6e 0a 20 20 20 20 2a 2f 0a 20 20  e scan.    */.  
2c59b 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
2c59c 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
2c59d 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2c59e 57 68 65 72 65 2c 20 26 70 4f 72 64 65 72 42 79  Where, &pOrderBy
2c59f 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57  , 0);.    if( pW
2c5a0 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73  Info==0 ) goto s
2c5a1 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20  elect_end;..    
2c5a2 2f 2a 20 49 66 20 73 6f 72 74 69 6e 67 20 69 6e  /* If sorting in
2c5a3 64 65 78 20 74 68 61 74 20 77 61 73 20 63 72 65  dex that was cre
2c5a4 61 74 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ated by a prior 
2c5a5 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2c5a6 20 0a 20 20 20 20 2a 2a 20 69 6e 73 74 72 75 63   .    ** instruc
2c5a7 74 69 6f 6e 20 65 6e 64 65 64 20 75 70 20 6e 6f  tion ended up no
2c5a8 74 20 62 65 69 6e 67 20 6e 65 65 64 65 64 2c 20  t being needed, 
2c5a9 74 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20  then change the 
2c5aa 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2c5ab 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 61 6e 20  .    ** into an 
2c5ac 4f 50 5f 4e 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  OP_Noop..    */.
2c5ad 20 20 20 20 69 66 28 20 61 64 64 72 53 6f 72 74      if( addrSort
2c5ae 49 6e 64 65 78 3e 3d 30 20 26 26 20 70 4f 72 64  Index>=0 && pOrd
2c5af 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  erBy==0 ){.     
2c5b0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2c5b1 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72  geToNoop(v, addr
2c5b2 53 6f 72 74 49 6e 64 65 78 2c 20 31 29 3b 0a 20  SortIndex, 1);. 
2c5b3 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e       p->addrOpen
2c5b4 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[2] = -1;.  
2c5b5 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20    }..    /* Use 
2c5b6 74 68 65 20 73 74 61 6e 64 61 72 64 20 69 6e 6e  the standard inn
2c5b7 65 72 20 6c 6f 6f 70 0a 20 20 20 20 2a 2f 0a 20  er loop.    */. 
2c5b8 20 20 20 61 73 73 65 72 74 28 21 69 73 44 69 73     assert(!isDis
2c5b9 74 69 6e 63 74 29 3b 0a 20 20 20 20 73 65 6c 65  tinct);.    sele
2c5ba 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72  ctInnerLoop(pPar
2c5bb 73 65 2c 20 70 2c 20 70 45 4c 69 73 74 2c 20 30  se, p, pEList, 0
2c5bc 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c 20 2d  , 0, pOrderBy, -
2c5bd 31 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20  1, pDest,.      
2c5be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
2c5bf 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 2c  Info->iContinue,
2c5c0 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29   pWInfo->iBreak)
2c5c1 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68  ;..    /* End th
2c5c2 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
2c5c3 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
2c5c4 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2c5c5 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73  (pWInfo);.  }els
2c5c6 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e{.    /* This i
2c5c7 73 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  s the processing
2c5c8 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 71   for aggregate q
2c5c9 75 65 72 69 65 73 20 2a 2f 0a 20 20 20 20 4e 61  ueries */.    Na
2c5ca 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
2c5cb 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
2c5cc 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  t for processing
2c5cd 20 61 67 67 72 65 67 61 74 65 20 69 6e 66 6f 72   aggregate infor
2c5ce 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e  mation */.    in
2c5cf 74 20 69 41 4d 65 6d 3b 20 20 20 20 20 20 20 20  t iAMem;        
2c5d0 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d 20 61    /* First Mem a
2c5d1 64 64 72 65 73 73 20 66 6f 72 20 73 74 6f 72 69  ddress for stori
2c5d2 6e 67 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  ng current GROUP
2c5d3 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69   BY */.    int i
2c5d4 42 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  BMem;          /
2c5d5 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72  * First Mem addr
2c5d6 65 73 73 20 66 6f 72 20 70 72 65 76 69 6f 75 73  ess for previous
2c5d7 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20   GROUP BY */.   
2c5d8 20 69 6e 74 20 69 55 73 65 46 6c 61 67 3b 20 20   int iUseFlag;  
2c5d9 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
2c5da 65 73 73 20 68 6f 6c 64 69 6e 67 20 66 6c 61 67  ess holding flag
2c5db 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2c5dc 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20 20 20   at least.      
2c5dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5de 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 6f 66 20    ** one row of 
2c5df 74 68 65 20 69 6e 70 75 74 20 74 6f 20 74 68 65  the input to the
2c5e0 20 61 67 67 72 65 67 61 74 6f 72 20 68 61 73 20   aggregator has 
2c5e1 62 65 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20  been.           
2c5e2 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
2c5e3 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 20  processed */.   
2c5e4 20 69 6e 74 20 69 41 62 6f 72 74 46 6c 61 67 3b   int iAbortFlag;
2c5e5 20 20 20 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72       /* Mem addr
2c5e6 65 73 73 20 77 68 69 63 68 20 63 61 75 73 65 73  ess which causes
2c5e7 20 71 75 65 72 79 20 61 62 6f 72 74 20 69 66 20   query abort if 
2c5e8 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 20 20  positive */.    
2c5e9 69 6e 74 20 67 72 6f 75 70 42 79 53 6f 72 74 3b  int groupBySort;
2c5ea 20 20 20 20 2f 2a 20 52 6f 77 73 20 63 6f 6d 65      /* Rows come
2c5eb 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 69 6e 20   from source in 
2c5ec 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 20 2a  GROUP BY order *
2c5ed 2f 0a 20 20 20 20 69 6e 74 20 61 64 64 72 45 6e  /.    int addrEn
2c5ee 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64  d;        /* End
2c5ef 20 6f 66 20 70 72 6f 63 65 73 73 69 6e 67 20 66   of processing f
2c5f0 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a  or this SELECT *
2c5f1 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65  /..    /* Remove
2c5f2 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69   any and all ali
2c5f3 61 73 65 73 20 62 65 74 77 65 65 6e 20 74 68 65  ases between the
2c5f4 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20   result set and 
2c5f5 74 68 65 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50  the.    ** GROUP
2c5f6 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 20 20   BY clause..    
2c5f7 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75  */.    if( pGrou
2c5f8 70 42 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  pBy ){.      int
2c5f9 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   k;             
2c5fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2c5fb 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2c5fc 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2c5fd 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2c5fe 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
2c5ff 20 6f 76 65 72 20 65 78 70 72 65 73 73 69 6f 6e   over expression
2c600 20 69 6e 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20   in a list */.. 
2c601 20 20 20 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45       for(k=p->pE
2c602 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74  List->nExpr, pIt
2c603 65 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b  em=p->pEList->a;
2c604 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d   k>0; k--, pItem
2c605 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74  ++){.        pIt
2c606 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a  em->iAlias = 0;.
2c607 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
2c608 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d 3e 6e 45  r(k=pGroupBy->nE
2c609 78 70 72 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75  xpr, pItem=pGrou
2c60a 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d  pBy->a; k>0; k--
2c60b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
2c60c 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61      pItem->iAlia
2c60d 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  s = 0;.      }. 
2c60e 20 20 20 7d 0a 0a 20 0a 20 20 20 20 2f 2a 20 43     }.. .    /* C
2c60f 72 65 61 74 65 20 61 20 6c 61 62 65 6c 20 74 6f  reate a label to
2c610 20 6a 75 6d 70 20 74 6f 20 77 68 65 6e 20 77 65   jump to when we
2c611 20 77 61 6e 74 20 74 6f 20 61 62 6f 72 74 20 74   want to abort t
2c612 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  he query */.    
2c613 61 64 64 72 45 6e 64 20 3d 20 73 71 6c 69 74 65  addrEnd = sqlite
2c614 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2c615 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65  );..    /* Conve
2c616 72 74 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64  rt TK_COLUMN nod
2c617 65 73 20 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43  es into TK_AGG_C
2c618 4f 4c 55 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65  OLUMN and make e
2c619 6e 74 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a  ntries in.    **
2c61a 20 73 41 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c   sAggInfo for al
2c61b 6c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  l TK_AGG_FUNCTIO
2c61c 4e 20 6e 6f 64 65 73 20 69 6e 20 65 78 70 72 65  N nodes in expre
2c61d 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
2c61e 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74    ** SELECT stat
2c61f 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ement..    */.  
2c620 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
2c621 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
2c622 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
2c623 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43   pParse;.    sNC
2c624 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
2c625 4c 69 73 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41  List;.    sNC.pA
2c626 67 67 49 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e  ggInfo = &sAggIn
2c627 66 6f 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f  fo;.    sAggInfo
2c628 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20  .nSortingColumn 
2c629 3d 20 70 47 72 6f 75 70 42 79 20 3f 20 70 47 72  = pGroupBy ? pGr
2c62a 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a  oupBy->nExpr+1 :
2c62b 20 30 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f   0;.    sAggInfo
2c62c 2e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f  .pGroupBy = pGro
2c62d 75 70 42 79 3b 0a 20 20 20 20 73 71 6c 69 74 65  upBy;.    sqlite
2c62e 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
2c62f 69 73 74 28 26 73 4e 43 2c 20 70 45 4c 69 73 74  ist(&sNC, pEList
2c630 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2c631 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
2c632 28 26 73 4e 43 2c 20 70 4f 72 64 65 72 42 79 29  (&sNC, pOrderBy)
2c633 3b 0a 20 20 20 20 69 66 28 20 70 48 61 76 69 6e  ;.    if( pHavin
2c634 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
2c635 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
2c636 72 65 67 61 74 65 73 28 26 73 4e 43 2c 20 70 48  regates(&sNC, pH
2c637 61 76 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20  aving);.    }.  
2c638 20 20 73 41 67 67 49 6e 66 6f 2e 6e 41 63 63 75    sAggInfo.nAccu
2c639 6d 75 6c 61 74 6f 72 20 3d 20 73 41 67 67 49 6e  mulator = sAggIn
2c63a 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  fo.nColumn;.    
2c63b 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49  for(i=0; i<sAggI
2c63c 6e 66 6f 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b  nfo.nFunc; i++){
2c63d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
2c63e 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2c63f 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69  sAggInfo.aFunc[i
2c640 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  ].pExpr, EP_xIsS
2c641 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
2c642 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
2c643 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
2c644 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69  sAggInfo.aFunc[i
2c645 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  ].pExpr->x.pList
2c646 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2c647 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2c648 64 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f  d ) goto select_
2c649 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f  end;..    /* Pro
2c64a 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72  cessing for aggr
2c64b 65 67 61 74 65 73 20 77 69 74 68 20 47 52 4f 55  egates with GROU
2c64c 50 20 42 59 20 69 73 20 76 65 72 79 20 64 69 66  P BY is very dif
2c64d 66 65 72 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a  ferent and.    *
2c64e 2a 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70  * much more comp
2c64f 6c 65 78 20 74 68 61 6e 20 61 67 67 72 65 67 61  lex than aggrega
2c650 74 65 73 20 77 69 74 68 6f 75 74 20 61 20 47 52  tes without a GR
2c651 4f 55 50 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20  OUP BY..    */. 
2c652 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20     if( pGroupBy 
2c653 29 7b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  ){.      KeyInfo
2c654 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20   *pKeyInfo;  /* 
2c655 4b 65 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  Keying informati
2c656 6f 6e 20 66 6f 72 20 74 68 65 20 67 72 6f 75 70  on for the group
2c657 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   by clause */.  
2c658 20 20 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20      int j1;     
2c659 20 20 20 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d          /* A-vs-
2c65a 42 20 63 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75  B comparision ju
2c65b 6d 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  mp */.      int 
2c65c 61 64 64 72 4f 75 74 70 75 74 52 6f 77 3b 20 20  addrOutputRow;  
2c65d 2f 2a 20 53 74 61 72 74 20 6f 66 20 73 75 62 72  /* Start of subr
2c65e 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70  outine that outp
2c65f 75 74 73 20 61 20 72 65 73 75 6c 74 20 72 6f 77  uts a result row
2c660 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
2c661 67 4f 75 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a  gOutputRow;   /*
2c662 20 52 65 74 75 72 6e 20 61 64 64 72 65 73 73 20   Return address 
2c663 72 65 67 69 73 74 65 72 20 66 6f 72 20 6f 75 74  register for out
2c664 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  put subroutine *
2c665 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
2c666 53 65 74 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53  SetAbort;   /* S
2c667 65 74 20 74 68 65 20 61 62 6f 72 74 20 66 6c 61  et the abort fla
2c668 67 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a  g and return */.
2c669 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f        int addrTo
2c66a 70 4f 66 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70  pOfLoop;  /* Top
2c66b 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 6c 6f   of the input lo
2c66c 6f 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  op */.      int 
2c66d 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78 3b 20  addrSortingIdx; 
2c66e 2f 2a 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70  /* The OP_OpenEp
2c66f 68 65 6d 65 72 61 6c 20 66 6f 72 20 74 68 65 20  hemeral for the 
2c670 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
2c671 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 52  .      int addrR
2c672 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 75  eset;      /* Su
2c673 62 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 65 73  broutine for res
2c674 65 74 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d  etting the accum
2c675 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  ulator */.      
2c676 69 6e 74 20 72 65 67 52 65 73 65 74 3b 20 20 20  int regReset;   
2c677 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64      /* Return ad
2c678 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2c679 6f 72 20 72 65 73 65 74 20 73 75 62 72 6f 75 74  or reset subrout
2c67a 69 6e 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a  ine */..      /*
2c67b 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 47   If there is a G
2c67c 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 77  ROUP BY clause w
2c67d 65 20 6d 69 67 68 74 20 6e 65 65 64 20 61 20 73  e might need a s
2c67e 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a  orting index to.
2c67f 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65        ** impleme
2c680 6e 74 20 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65  nt it.  Allocate
2c681 20 74 68 61 74 20 73 6f 72 74 69 6e 67 20 69 6e   that sorting in
2c682 64 65 78 20 6e 6f 77 2e 20 20 49 66 20 69 74 20  dex now.  If it 
2c683 74 75 72 6e 73 20 6f 75 74 0a 20 20 20 20 20 20  turns out.      
2c684 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f  ** that we do no
2c685 74 20 6e 65 65 64 20 69 74 20 61 66 74 65 72 20  t need it after 
2c686 61 6c 6c 2c 20 74 68 65 20 4f 70 65 6e 45 70 68  all, the OpenEph
2c687 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69  emeral instructi
2c688 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  on.      ** will
2c689 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   be converted in
2c68a 74 6f 20 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20  to a Noop.  .   
2c68b 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 41 67 67     */.      sAgg
2c68c 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20  Info.sortingIdx 
2c68d 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2c68e 3b 0a 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f  ;.      pKeyInfo
2c68f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78   = keyInfoFromEx
2c690 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2c691 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20  GroupBy);.      
2c692 61 64 64 72 53 6f 72 74 69 6e 67 49 64 78 20 3d  addrSortingIdx =
2c693 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c694 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
2c695 65 6d 65 72 61 6c 2c 20 0a 20 20 20 20 20 20 20  emeral, .       
2c696 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74     sAggInfo.sort
2c697 69 6e 67 49 64 78 2c 20 73 41 67 67 49 6e 66 6f  ingIdx, sAggInfo
2c698 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c  .nSortingColumn,
2c699 20 0a 20 20 20 20 20 20 20 20 20 20 30 2c 20 28   .          0, (
2c69a 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20  char*)pKeyInfo, 
2c69b 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
2c69c 46 46 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  FF);..      /* I
2c69d 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79  nitialize memory
2c69e 20 6c 6f 63 61 74 69 6f 6e 73 20 75 73 65 64 20   locations used 
2c69f 62 79 20 47 52 4f 55 50 20 42 59 20 61 67 67 72  by GROUP BY aggr
2c6a0 65 67 61 74 65 20 70 72 6f 63 65 73 73 69 6e 67  egate processing
2c6a1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2c6a2 69 55 73 65 46 6c 61 67 20 3d 20 2b 2b 70 50 61  iUseFlag = ++pPa
2c6a3 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
2c6a4 20 69 41 62 6f 72 74 46 6c 61 67 20 3d 20 2b 2b   iAbortFlag = ++
2c6a5 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2c6a6 20 20 20 20 72 65 67 4f 75 74 70 75 74 52 6f 77      regOutputRow
2c6a7 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2c6a8 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74  m;.      addrOut
2c6a9 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  putRow = sqlite3
2c6aa 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2c6ab 3b 0a 20 20 20 20 20 20 72 65 67 52 65 73 65 74  ;.      regReset
2c6ac 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2c6ad 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 52 65 73  m;.      addrRes
2c6ae 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  et = sqlite3Vdbe
2c6af 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2c6b0 20 20 20 20 69 41 4d 65 6d 20 3d 20 70 50 61 72      iAMem = pPar
2c6b1 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20  se->nMem + 1;.  
2c6b2 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
2c6b3 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45   += pGroupBy->nE
2c6b4 78 70 72 3b 0a 20 20 20 20 20 20 69 42 4d 65 6d  xpr;.      iBMem
2c6b5 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20   = pParse->nMem 
2c6b6 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73  + 1;.      pPars
2c6b7 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75  e->nMem += pGrou
2c6b8 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pBy->nExpr;.    
2c6b9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c6ba 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c6bb 72 2c 20 30 2c 20 69 41 62 6f 72 74 46 6c 61 67  r, 0, iAbortFlag
2c6bc 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c6bd 6d 65 6e 74 28 28 76 2c 20 22 63 6c 65 61 72 20  ment((v, "clear 
2c6be 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20  abort flag"));. 
2c6bf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c6c0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2c6c1 65 67 65 72 2c 20 30 2c 20 69 55 73 65 46 6c 61  eger, 0, iUseFla
2c6c2 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  g);.      VdbeCo
2c6c3 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63  mment((v, "indic
2c6c4 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  ate accumulator 
2c6c5 65 6d 70 74 79 22 29 29 3b 0a 0a 20 20 20 20 20  empty"));..     
2c6c6 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c 6f 6f 70   /* Begin a loop
2c6c7 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61   that will extra
2c6c8 63 74 20 61 6c 6c 20 73 6f 75 72 63 65 20 72 6f  ct all source ro
2c6c9 77 73 20 69 6e 20 47 52 4f 55 50 20 42 59 20 6f  ws in GROUP BY o
2c6ca 72 64 65 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54  rder..      ** T
2c6cb 68 69 73 20 6d 69 67 68 74 20 69 6e 76 6f 6c 76  his might involv
2c6cc 65 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 6c  e two separate l
2c6cd 6f 6f 70 73 20 77 69 74 68 20 61 6e 20 4f 50 5f  oops with an OP_
2c6ce 53 6f 72 74 20 69 6e 20 62 65 74 77 65 65 6e 2c  Sort in between,
2c6cf 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 74 20   or.      ** it 
2c6d0 6d 69 67 68 74 20 62 65 20 61 20 73 69 6e 67 6c  might be a singl
2c6d1 65 20 6c 6f 6f 70 20 74 68 61 74 20 75 73 65 73  e loop that uses
2c6d2 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 65 78 74   an index to ext
2c6d3 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ract information
2c6d4 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  .      ** in the
2c6d5 20 72 69 67 68 74 20 6f 72 64 65 72 20 74 6f 20   right order to 
2c6d6 62 65 67 69 6e 20 77 69 74 68 2e 0a 20 20 20 20  begin with..    
2c6d7 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
2c6d8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c6d9 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73  OP_Gosub, regRes
2c6da 65 74 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a  et, addrReset);.
2c6db 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73        pWInfo = s
2c6dc 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2c6dd 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
2c6de 74 2c 20 70 57 68 65 72 65 2c 20 26 70 47 72 6f  t, pWhere, &pGro
2c6df 75 70 42 79 2c 20 30 29 3b 0a 20 20 20 20 20 20  upBy, 0);.      
2c6e0 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20  if( pWInfo==0 ) 
2c6e1 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2c6e2 0a 20 20 20 20 20 20 69 66 28 20 70 47 72 6f 75  .      if( pGrou
2c6e3 70 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pBy==0 ){.      
2c6e4 20 20 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a    /* The optimiz
2c6e5 65 72 20 69 73 20 61 62 6c 65 20 74 6f 20 64 65  er is able to de
2c6e6 6c 69 76 65 72 20 72 6f 77 73 20 69 6e 20 67 72  liver rows in gr
2c6e7 6f 75 70 20 62 79 20 6f 72 64 65 72 20 73 6f 0a  oup by order so.
2c6e8 20 20 20 20 20 20 20 20 2a 2a 20 77 65 20 64 6f          ** we do
2c6e9 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 73 6f 72   not have to sor
2c6ea 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45  t.  The OP_OpenE
2c6eb 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77  phemeral table w
2c6ec 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a  ill be.        *
2c6ed 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65  * cancelled late
2c6ee 72 20 62 65 63 61 75 73 65 20 77 65 20 73 74 69  r because we sti
2c6ef 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74  ll need to use t
2c6f0 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20  he pKeyInfo.    
2c6f1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70      */.        p
2c6f2 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72  GroupBy = p->pGr
2c6f3 6f 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20 67  oupBy;.        g
2c6f4 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a  roupBySort = 0;.
2c6f5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2c6f6 20 20 20 20 20 2f 2a 20 52 6f 77 73 20 61 72 65       /* Rows are
2c6f7 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75   coming out in u
2c6f8 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f 72 64 65  ndetermined orde
2c6f9 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 70  r.  We have to p
2c6fa 75 73 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 65  ush.        ** e
2c6fb 61 63 68 20 72 6f 77 20 69 6e 74 6f 20 61 20 73  ach row into a s
2c6fc 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 20 74 65  orting index, te
2c6fd 72 6d 69 6e 61 74 65 20 74 68 65 20 66 69 72 73  rminate the firs
2c6fe 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20  t loop,.        
2c6ff 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65  ** then loop ove
2c700 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  r the sorting in
2c701 64 65 78 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  dex in order to 
2c702 67 65 74 20 74 68 65 20 6f 75 74 70 75 74 0a 20  get the output. 
2c703 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72         ** in sor
2c704 74 65 64 20 6f 72 64 65 72 0a 20 20 20 20 20 20  ted order.      
2c705 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
2c706 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20   regBase;.      
2c707 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
2c708 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43 6f  .        int nCo
2c709 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  l;.        int n
2c70a 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20 20 20 20  GroupBy;..      
2c70b 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20    groupBySort = 
2c70c 31 3b 0a 20 20 20 20 20 20 20 20 6e 47 72 6f 75  1;.        nGrou
2c70d 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 2d 3e  pBy = pGroupBy->
2c70e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 6e  nExpr;.        n
2c70f 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42 79 20 2b  Col = nGroupBy +
2c710 20 31 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20   1;.        j = 
2c711 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20  nGroupBy+1;.    
2c712 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
2c713 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b  AggInfo.nColumn;
2c714 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2c715 20 69 66 28 20 73 41 67 67 49 6e 66 6f 2e 61 43   if( sAggInfo.aC
2c716 6f 6c 5b 69 5d 2e 69 53 6f 72 74 65 72 43 6f 6c  ol[i].iSorterCol
2c717 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20  umn>=j ){.      
2c718 20 20 20 20 20 20 6e 43 6f 6c 2b 2b 3b 0a 20 20        nCol++;.  
2c719 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20            j++;. 
2c71a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c71b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 67     }.        reg
2c71c 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Base = sqlite3Ge
2c71d 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
2c71e 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  e, nCol);.      
2c71f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2c720 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2c721 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c722 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
2c723 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42  (pParse, pGroupB
2c724 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a  y, regBase, 0);.
2c725 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c726 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c727 53 65 71 75 65 6e 63 65 2c 20 73 41 67 67 49 6e  Sequence, sAggIn
2c728 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 72 65  fo.sortingIdx,re
2c729 67 42 61 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b  gBase+nGroupBy);
2c72a 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72  .        j = nGr
2c72b 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20  oupBy+1;.       
2c72c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67   for(i=0; i<sAgg
2c72d 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Info.nColumn; i+
2c72e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74  +){.          st
2c72f 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
2c730 20 2a 70 43 6f 6c 20 3d 20 26 73 41 67 67 49 6e   *pCol = &sAggIn
2c731 66 6f 2e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20  fo.aCol[i];.    
2c732 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
2c733 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a  iSorterColumn>=j
2c734 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2c735 69 6e 74 20 72 31 20 3d 20 6a 20 2b 20 72 65 67  int r1 = j + reg
2c736 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Base;.          
2c737 20 20 69 6e 74 20 72 32 3b 0a 0a 20 20 20 20 20    int r2;..     
2c738 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69         r2 = sqli
2c739 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
2c73a 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 0a 20 20  lumn(pParse, .  
2c73b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c73c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2c73d 6c 2d 3e 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69  l->pTab, pCol->i
2c73e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54  Column, pCol->iT
2c73f 61 62 6c 65 2c 20 72 31 2c 20 30 29 3b 0a 20 20  able, r1, 0);.  
2c740 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 31            if( r1
2c741 21 3d 72 32 20 29 7b 0a 20 20 20 20 20 20 20 20  !=r2 ){.        
2c742 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c743 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2c744 6f 70 79 2c 20 72 32 2c 20 72 31 29 3b 0a 20 20  opy, r2, r1);.  
2c745 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2c746 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20          j++;.   
2c747 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c748 20 7d 0a 20 20 20 20 20 20 20 20 72 65 67 52 65   }.        regRe
2c749 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65  cord = sqlite3Ge
2c74a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2c74b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c74c 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c74d 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
2c74e 67 42 61 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67  gBase, nCol, reg
2c74f 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20  Record);.       
2c750 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c751 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
2c752 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  rt, sAggInfo.sor
2c753 74 69 6e 67 49 64 78 2c 20 72 65 67 52 65 63 6f  tingIdx, regReco
2c754 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  rd);.        sql
2c755 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2c756 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  eg(pParse, regRe
2c757 63 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73  cord);.        s
2c758 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2c759 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
2c75a 65 67 42 61 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20  egBase, nCol);. 
2c75b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
2c75c 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
2c75d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c75e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c75f 53 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73  Sort, sAggInfo.s
2c760 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45  ortingIdx, addrE
2c761 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  nd);.        Vdb
2c762 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52  eComment((v, "GR
2c763 4f 55 50 20 42 59 20 73 6f 72 74 22 29 29 3b 0a  OUP BY sort"));.
2c764 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f          sAggInfo
2c765 2e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 3d  .useSortingIdx =
2c766 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   1;.        sqli
2c767 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2c768 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  r(pParse);.     
2c769 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 45 76 61   }..      /* Eva
2c76a 6c 75 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  luate the curren
2c76b 74 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  t GROUP BY terms
2c76c 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 62 30   and store in b0
2c76d 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20  , b1, b2....    
2c76e 20 20 2a 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f    ** (b0 is memo
2c76f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65  ry location iBMe
2c770 6d 2b 30 2c 20 62 31 20 69 73 20 69 42 4d 65 6d  m+0, b1 is iBMem
2c771 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  +1, and so forth
2c772 29 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20  ).      ** Then 
2c773 63 6f 6d 70 61 72 65 20 74 68 65 20 63 75 72 72  compare the curr
2c774 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65 72  ent GROUP BY ter
2c775 6d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 47  ms against the G
2c776 52 4f 55 50 20 42 59 20 74 65 72 6d 73 0a 20 20  ROUP BY terms.  
2c777 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20      ** from the 
2c778 70 72 65 76 69 6f 75 73 20 72 6f 77 20 63 75 72  previous row cur
2c779 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
2c77a 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20   a0, a1, a2.... 
2c77b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64       */.      ad
2c77c 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71  drTopOfLoop = sq
2c77d 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2c77e 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
2c77f 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c780 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c781 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2c782 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20  GroupBy->nExpr; 
2c783 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  j++){.        if
2c784 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b  ( groupBySort ){
2c785 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2c786 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c787 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 41 67 67 49  OP_Column, sAggI
2c788 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20  nfo.sortingIdx, 
2c789 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20  j, iBMem+j);.   
2c78a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c78b 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 64        sAggInfo.d
2c78c 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20  irectMode = 1;. 
2c78d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2c78e 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2c78f 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e   pGroupBy->a[j].
2c790 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b  pExpr, iBMem+j);
2c791 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2c792 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2c793 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2c794 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d 65 6d 2c  _Compare, iAMem,
2c795 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79   iBMem, pGroupBy
2c796 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
2c797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c798 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2c799 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b  fo, P4_KEYINFO);
2c79a 0a 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  .      j1 = sqli
2c79b 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2c79c 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c  dr(v);.      sql
2c79d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2c79e 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c  , OP_Jump, j1+1,
2c79f 20 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20 20 20 20   0, j1+1);..    
2c7a0 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2c7a1 64 65 20 74 68 61 74 20 72 75 6e 73 20 77 68 65  de that runs whe
2c7a2 6e 65 76 65 72 20 74 68 65 20 47 52 4f 55 50 20  never the GROUP 
2c7a3 42 59 20 63 68 61 6e 67 65 73 2e 0a 20 20 20 20  BY changes..    
2c7a4 20 20 2a 2a 20 43 68 61 6e 67 65 73 20 69 6e 20    ** Changes in 
2c7a5 74 68 65 20 47 52 4f 55 50 20 42 59 20 61 72 65  the GROUP BY are
2c7a6 20 64 65 74 65 63 74 65 64 20 62 79 20 74 68 65   detected by the
2c7a7 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65 0a 20   previous code. 
2c7a8 20 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20       ** block.  
2c7a9 49 66 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f  If there were no
2c7aa 20 63 68 61 6e 67 65 73 2c 20 74 68 69 73 20 62   changes, this b
2c7ab 6c 6f 63 6b 20 69 73 20 73 6b 69 70 70 65 64 2e  lock is skipped.
2c7ac 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2c7ad 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 63 6f 70  ** This code cop
2c7ae 69 65 73 20 63 75 72 72 65 6e 74 20 67 72 6f 75  ies current grou
2c7af 70 20 62 79 20 74 65 72 6d 73 20 69 6e 20 62 30  p by terms in b0
2c7b0 2c 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20 20 20 20  ,b1,b2,....     
2c7b1 20 2a 2a 20 6f 76 65 72 20 74 6f 20 61 30 2c 61   ** over to a0,a
2c7b2 31 2c 61 32 2e 20 20 49 74 20 74 68 65 6e 20 63  1,a2.  It then c
2c7b3 61 6c 6c 73 20 74 68 65 20 6f 75 74 70 75 74 20  alls the output 
2c7b4 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20 20  subroutine.     
2c7b5 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 73 20 74   ** and resets t
2c7b6 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63  he aggregate acc
2c7b7 75 6d 75 6c 61 74 6f 72 20 72 65 67 69 73 74 65  umulator registe
2c7b8 72 73 20 69 6e 20 70 72 65 70 61 72 61 74 69 6f  rs in preparatio
2c7b9 6e 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74  n.      ** for t
2c7ba 68 65 20 6e 65 78 74 20 47 52 4f 55 50 20 42 59  he next GROUP BY
2c7bb 20 62 61 74 63 68 2e 0a 20 20 20 20 20 20 2a 2f   batch..      */
2c7bc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c7bd 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
2c7be 65 2c 20 69 42 4d 65 6d 2c 20 69 41 4d 65 6d 2c  e, iBMem, iAMem,
2c7bf 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
2c7c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c7c1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c7c2 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75  _Gosub, regOutpu
2c7c3 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74  tRow, addrOutput
2c7c4 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
2c7c5 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74  Comment((v, "out
2c7c6 70 75 74 20 6f 6e 65 20 72 6f 77 22 29 29 3b 0a  put one row"));.
2c7c7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c7c8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2c7c9 50 6f 73 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c  Pos, iAbortFlag,
2c7ca 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20   addrEnd);.     
2c7cb 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2c7cc 20 22 63 68 65 63 6b 20 61 62 6f 72 74 20 66 6c   "check abort fl
2c7cd 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  ag"));.      sql
2c7ce 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c7cf 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52  , OP_Gosub, regR
2c7d0 65 73 65 74 2c 20 61 64 64 72 52 65 73 65 74 29  eset, addrReset)
2c7d1 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
2c7d2 65 6e 74 28 28 76 2c 20 22 72 65 73 65 74 20 61  ent((v, "reset a
2c7d3 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a  ccumulator"));..
2c7d4 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20        /* Update 
2c7d5 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63  the aggregate ac
2c7d6 63 75 6d 75 6c 61 74 6f 72 73 20 62 61 73 65 64  cumulators based
2c7d7 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   on the content 
2c7d8 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  of.      ** the 
2c7d9 63 75 72 72 65 6e 74 20 72 6f 77 0a 20 20 20 20  current row.    
2c7da 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
2c7db 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2c7dc 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 75 70 64  , j1);.      upd
2c7dd 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70  ateAccumulator(p
2c7de 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
2c7df 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c7e0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c7e1 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 55 73  _Integer, 1, iUs
2c7e2 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  eFlag);.      Vd
2c7e3 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69  beComment((v, "i
2c7e4 6e 64 69 63 61 74 65 20 64 61 74 61 20 69 6e 20  ndicate data in 
2c7e5 61 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a  accumulator"));.
2c7e6 0a 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66  .      /* End of
2c7e7 20 74 68 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20   the loop.      
2c7e8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 67 72 6f  */.      if( gro
2c7e9 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20  upBySort ){.    
2c7ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c7eb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2c7ec 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69  , sAggInfo.sorti
2c7ed 6e 67 49 64 78 2c 20 61 64 64 72 54 6f 70 4f 66  ngIdx, addrTopOf
2c7ee 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Loop);.      }el
2c7ef 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2c7f0 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
2c7f1 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  fo);.        sql
2c7f2 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
2c7f3 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74  Noop(v, addrSort
2c7f4 69 6e 67 49 64 78 2c 20 31 29 3b 0a 20 20 20 20  ingIdx, 1);.    
2c7f5 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75    }..      /* Ou
2c7f6 74 70 75 74 20 74 68 65 20 66 69 6e 61 6c 20 72  tput the final r
2c7f7 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a 20 20 20  ow of result.   
2c7f8 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
2c7f9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c7fa 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
2c7fb 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f 75 74  tputRow, addrOut
2c7fc 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56  putRow);.      V
2c7fd 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c7fe 6f 75 74 70 75 74 20 66 69 6e 61 6c 20 72 6f 77  output final row
2c7ff 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a  "));..      /* J
2c800 75 6d 70 20 6f 76 65 72 20 74 68 65 20 73 75 62  ump over the sub
2c801 72 6f 75 74 69 6e 65 73 0a 20 20 20 20 20 20 2a  routines.      *
2c802 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2c803 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c804 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45 6e 64  Goto, 0, addrEnd
2c805 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e  );..      /* Gen
2c806 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2c807 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
2c808 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
2c809 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20  the result.     
2c80a 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 73 20 73   ** set.  This s
2c80b 75 62 72 6f 75 74 69 6e 65 20 66 69 72 73 74 20  ubroutine first 
2c80c 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 69 55 73  looks at the iUs
2c80d 65 46 6c 61 67 2e 20 20 49 66 20 69 55 73 65 46  eFlag.  If iUseF
2c80e 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  lag.      ** is 
2c80f 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
2c810 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20  al to zero, the 
2c811 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20 61 20  subroutine is a 
2c812 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 20 20 20  no-op.  If.     
2c813 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 69   ** the processi
2c814 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65  ng calls for the
2c815 20 71 75 65 72 79 20 74 6f 20 61 62 6f 72 74 2c   query to abort,
2c816 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
2c817 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d  .      ** increm
2c818 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72 74 46  ents the iAbortF
2c819 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  lag memory locat
2c81a 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72  ion before retur
2c81b 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 20 2a 2a  ning in.      **
2c81c 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e 61 6c   order to signal
2c81d 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 61   the caller to a
2c81e 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  bort..      */. 
2c81f 20 20 20 20 20 61 64 64 72 53 65 74 41 62 6f 72       addrSetAbor
2c820 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
2c821 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2c822 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c823 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2c824 65 67 65 72 2c 20 31 2c 20 69 41 62 6f 72 74 46  eger, 1, iAbortF
2c825 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
2c826 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 65 74  Comment((v, "set
2c827 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a   abort flag"));.
2c828 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c829 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
2c82a 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52  turn, regOutputR
2c82b 6f 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ow);.      sqlit
2c82c 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2c82d 65 6c 28 76 2c 20 61 64 64 72 4f 75 74 70 75 74  el(v, addrOutput
2c82e 52 6f 77 29 3b 0a 20 20 20 20 20 20 61 64 64 72  Row);.      addr
2c82f 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69  OutputRow = sqli
2c830 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2c831 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c  dr(v);.      sql
2c832 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c833 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 55 73 65  , OP_IfPos, iUse
2c834 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 70 75 74  Flag, addrOutput
2c835 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20 20 56 64  Row+2);.      Vd
2c836 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47  beComment((v, "G
2c837 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67 65  roupby result ge
2c838 6e 65 72 61 74 6f 72 20 65 6e 74 72 79 20 70 6f  nerator entry po
2c839 69 6e 74 22 29 29 3b 0a 20 20 20 20 20 20 73 71  int"));.      sq
2c83a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2c83b 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65  v, OP_Return, re
2c83c 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  gOutputRow);.   
2c83d 20 20 20 66 69 6e 61 6c 69 7a 65 41 67 67 46 75     finalizeAggFu
2c83e 6e 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20  nctions(pParse, 
2c83f 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20  &sAggInfo);.    
2c840 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2c841 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 48 61  alse(pParse, pHa
2c842 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70 75 74  ving, addrOutput
2c843 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55  Row+1, SQLITE_JU
2c844 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
2c845 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
2c846 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
2c847 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72  EList, 0, 0, pOr
2c848 64 65 72 42 79 2c 0a 20 20 20 20 20 20 20 20 20  derBy,.         
2c849 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73               dis
2c84a 74 69 6e 63 74 2c 20 70 44 65 73 74 2c 0a 20 20  tinct, pDest,.  
2c84b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c84c 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f      addrOutputRo
2c84d 77 2b 31 2c 20 61 64 64 72 53 65 74 41 62 6f 72  w+1, addrSetAbor
2c84e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2c84f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2c850 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74  P_Return, regOut
2c851 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 56  putRow);.      V
2c852 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c853 65 6e 64 20 67 72 6f 75 70 62 79 20 72 65 73 75  end groupby resu
2c854 6c 74 20 67 65 6e 65 72 61 74 6f 72 22 29 29 3b  lt generator"));
2c855 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72  ..      /* Gener
2c856 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65  ate a subroutine
2c857 20 74 68 61 74 20 77 69 6c 6c 20 72 65 73 65 74   that will reset
2c858 20 74 68 65 20 67 72 6f 75 70 2d 62 79 20 61 63   the group-by ac
2c859 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20 20 20 20  cumulator.      
2c85a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2c85b 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2c85c 28 76 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a  (v, addrReset);.
2c85d 20 20 20 20 20 20 72 65 73 65 74 41 63 63 75 6d        resetAccum
2c85e 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26  ulator(pParse, &
2c85f 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20  sAggInfo);.     
2c860 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c861 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2c862 20 72 65 67 52 65 73 65 74 29 3b 0a 20 20 20 20   regReset);.    
2c863 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66   .    } /* endif
2c864 20 70 47 72 6f 75 70 42 79 2e 20 20 42 65 67 69   pGroupBy.  Begi
2c865 6e 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  n aggregate quer
2c866 69 65 73 20 77 69 74 68 6f 75 74 20 47 52 4f 55  ies without GROU
2c867 50 20 42 59 3a 20 2a 2f 0a 20 20 20 20 65 6c 73  P BY: */.    els
2c868 65 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  e {.      ExprLi
2c869 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 23 69  st *pDel = 0;.#i
2c86a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c86b 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 20 20 20  T_BTREECOUNT.   
2c86c 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
2c86d 20 20 20 20 20 20 69 66 28 20 28 70 54 61 62 20        if( (pTab 
2c86e 3d 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28  = isSimpleCount(
2c86f 70 2c 20 26 73 41 67 67 49 6e 66 6f 29 29 21 3d  p, &sAggInfo))!=
2c870 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
2c871 49 66 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74  If isSimpleCount
2c872 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  () returns a poi
2c873 6e 74 65 72 20 74 6f 20 61 20 54 61 62 6c 65 20  nter to a Table 
2c874 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 0a  structure, then.
2c875 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 53          ** the S
2c876 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  QL statement is 
2c877 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 20  of the form:.   
2c878 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c879 2a 2a 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e  **   SELECT coun
2c87a 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a  t(*) FROM <tbl>.
2c87b 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c87c 20 20 20 2a 2a 20 77 68 65 72 65 20 74 68 65 20     ** where the 
2c87d 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2c87e 72 65 74 75 72 6e 65 64 20 72 65 70 72 65 73 65  returned represe
2c87f 6e 74 73 20 74 61 62 6c 65 20 3c 74 62 6c 3e 2e  nts table <tbl>.
2c880 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2c881 20 20 20 20 2a 2a 20 54 68 69 73 20 73 74 61 74      ** This stat
2c882 65 6d 65 6e 74 20 69 73 20 73 6f 20 63 6f 6d 6d  ement is so comm
2c883 6f 6e 20 74 68 61 74 20 69 74 20 69 73 20 6f 70  on that it is op
2c884 74 69 6d 69 7a 65 64 20 73 70 65 63 69 61 6c 6c  timized speciall
2c885 79 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a  y. The.        *
2c886 2a 20 4f 50 5f 43 6f 75 6e 74 20 69 6e 73 74 72  * OP_Count instr
2c887 75 63 74 69 6f 6e 20 69 73 20 65 78 65 63 75 74  uction is execut
2c888 65 64 20 65 69 74 68 65 72 20 6f 6e 20 74 68 65  ed either on the
2c889 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 74 68   intkey table th
2c88a 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  at.        ** co
2c88b 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 20  ntains the data 
2c88c 66 6f 72 20 74 61 62 6c 65 20 3c 74 62 6c 3e 20  for table <tbl> 
2c88d 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66 20 69 74 73  or on one of its
2c88e 20 69 6e 64 65 78 65 73 2e 20 49 74 0a 20 20 20   indexes. It.   
2c88f 20 20 20 20 20 2a 2a 20 69 73 20 62 65 74 74 65       ** is bette
2c890 72 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 65  r to execute the
2c891 20 6f 70 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2c   op on an index,
2c892 20 61 73 20 69 6e 64 65 78 65 73 20 61 72 65 20   as indexes are 
2c893 61 6c 6d 6f 73 74 0a 20 20 20 20 20 20 20 20 2a  almost.        *
2c894 2a 20 61 6c 77 61 79 73 20 73 70 72 65 61 64 20  * always spread 
2c895 61 63 72 6f 73 73 20 6c 65 73 73 20 70 61 67 65  across less page
2c896 73 20 74 68 61 6e 20 74 68 65 69 72 20 63 6f 72  s than their cor
2c897 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65  responding table
2c898 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
2c899 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
2c89a 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2c89b 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
2c89c 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
2c89d 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 63  hema);.        c
2c89e 6f 6e 73 74 20 69 6e 74 20 69 43 73 72 20 3d 20  onst int iCsr = 
2c89f 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20  pParse->nTab++; 
2c8a0 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f      /* Cursor to
2c8a1 20 73 63 61 6e 20 62 2d 74 72 65 65 20 2a 2f 0a   scan b-tree */.
2c8a2 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
2c8a3 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
2c8a4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c8a5 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
2c8a6 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 4b 65 79  e */.        Key
2c8a7 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d  Info *pKeyInfo =
2c8a8 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2c8a9 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72    /* Keyinfo for
2c8aa 20 73 63 61 6e 6e 65 64 20 69 6e 64 65 78 20 2a   scanned index *
2c8ab 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20  /.        Index 
2c8ac 2a 70 42 65 73 74 20 3d 20 30 3b 20 20 20 20 20  *pBest = 0;     
2c8ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c8ae 2a 20 42 65 73 74 20 69 6e 64 65 78 20 66 6f 75  * Best index fou
2c8af 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20  nd so far */.   
2c8b0 20 20 20 20 20 69 6e 74 20 69 52 6f 6f 74 20 3d       int iRoot =
2c8b1 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20 20 20 20   pTab->tnum;    
2c8b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
2c8b3 74 20 70 61 67 65 20 6f 66 20 73 63 61 6e 6e 65  t page of scanne
2c8b4 64 20 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20  d b-tree */..   
2c8b5 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
2c8b6 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
2c8b7 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  rse, iDb);.     
2c8b8 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c     sqlite3TableL
2c8b9 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
2c8ba 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20   pTab->tnum, 0, 
2c8bb 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
2c8bc 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68         /* Search
2c8bd 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74   for the index t
2c8be 68 61 74 20 68 61 73 20 74 68 65 20 6c 65 61 73  hat has the leas
2c8bf 74 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 6c 75  t amount of colu
2c8c0 6d 6e 73 2e 20 49 66 0a 20 20 20 20 20 20 20 20  mns. If.        
2c8c1 2a 2a 20 74 68 65 72 65 20 69 73 20 73 75 63 68  ** there is such
2c8c2 20 61 6e 20 69 6e 64 65 78 2c 20 61 6e 64 20 69   an index, and i
2c8c3 74 20 68 61 73 20 6c 65 73 73 20 63 6f 6c 75 6d  t has less colum
2c8c4 6e 73 20 74 68 61 6e 20 74 68 65 20 74 61 62 6c  ns than the tabl
2c8c5 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f 65  e.        ** doe
2c8c6 73 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 61  s, then we can a
2c8c7 73 73 75 6d 65 20 74 68 61 74 20 69 74 20 63 6f  ssume that it co
2c8c8 6e 73 75 6d 65 73 20 6c 65 73 73 20 73 70 61 63  nsumes less spac
2c8c9 65 20 6f 6e 20 64 69 73 6b 20 61 6e 64 0a 20 20  e on disk and.  
2c8ca 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 68        ** will th
2c8cb 65 72 65 66 6f 72 65 20 62 65 20 63 68 65 61 70  erefore be cheap
2c8cc 65 72 20 74 6f 20 73 63 61 6e 20 74 6f 20 64 65  er to scan to de
2c8cd 74 65 72 6d 69 6e 65 20 74 68 65 20 71 75 65 72  termine the quer
2c8ce 79 20 72 65 73 75 6c 74 2e 0a 20 20 20 20 20 20  y result..      
2c8cf 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73    ** In this cas
2c8d0 65 20 73 65 74 20 69 52 6f 6f 74 20 74 6f 20 74  e set iRoot to t
2c8d1 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
2c8d2 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
2c8d3 20 62 2d 74 72 65 65 0a 20 20 20 20 20 20 20 20   b-tree.        
2c8d4 2a 2a 20 61 6e 64 20 70 4b 65 79 49 6e 66 6f 20  ** and pKeyInfo 
2c8d5 74 6f 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  to the KeyInfo s
2c8d6 74 72 75 63 74 75 72 65 20 72 65 71 75 69 72 65  tructure require
2c8d7 64 20 74 6f 20 6e 61 76 69 67 61 74 65 20 74 68  d to navigate th
2c8d8 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64  e.        ** ind
2c8d9 65 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ex..        **. 
2c8da 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 70 72 61         ** In pra
2c8db 63 74 69 63 65 20 74 68 65 20 4b 65 79 49 6e 66  ctice the KeyInf
2c8dc 6f 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c  o structure will
2c8dd 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 49 74   not be used. It
2c8de 20 69 73 20 6f 6e 6c 79 20 0a 20 20 20 20 20 20   is only .      
2c8df 20 20 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 6b    ** passed to k
2c8e0 65 65 70 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20  eep OP_OpenRead 
2c8e1 68 61 70 70 79 2e 0a 20 20 20 20 20 20 20 20 2a  happy..        *
2c8e2 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 49  /.        for(pI
2c8e3 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2c8e4 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2c8e5 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2c8e6 20 20 20 20 69 66 28 20 21 70 42 65 73 74 20 7c      if( !pBest |
2c8e7 7c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c  | pIdx->nColumn<
2c8e8 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  pBest->nColumn )
2c8e9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 42  {.            pB
2c8ea 65 73 74 20 3d 20 70 49 64 78 3b 0a 20 20 20 20  est = pIdx;.    
2c8eb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c8ec 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 42  }.        if( pB
2c8ed 65 73 74 20 26 26 20 70 42 65 73 74 2d 3e 6e 43  est && pBest->nC
2c8ee 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c  olumn<pTab->nCol
2c8ef 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 52   ){.          iR
2c8f0 6f 6f 74 20 3d 20 70 42 65 73 74 2d 3e 74 6e 75  oot = pBest->tnu
2c8f1 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65  m;.          pKe
2c8f2 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 49  yInfo = sqlite3I
2c8f3 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
2c8f4 73 65 2c 20 70 42 65 73 74 29 3b 0a 20 20 20 20  se, pBest);.    
2c8f5 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
2c8f6 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e  * Open a read-on
2c8f7 6c 79 20 63 75 72 73 6f 72 2c 20 65 78 65 63 75  ly cursor, execu
2c8f8 74 65 20 74 68 65 20 4f 50 5f 43 6f 75 6e 74 2c  te the OP_Count,
2c8f9 20 63 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f   close the curso
2c8fa 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  r. */.        sq
2c8fb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2c8fc 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
2c8fd 69 43 73 72 2c 20 69 52 6f 6f 74 2c 20 69 44 62  iCsr, iRoot, iDb
2c8fe 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2c8ff 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  KeyInfo ){.     
2c900 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c901 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
2c902 28 63 68 61 72 20 2a 29 70 4b 65 79 49 6e 66 6f  (char *)pKeyInfo
2c903 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
2c904 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 7d  DOFF);.        }
2c905 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c906 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c907 5f 43 6f 75 6e 74 2c 20 69 43 73 72 2c 20 73 41  _Count, iCsr, sA
2c908 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e  ggInfo.aFunc[0].
2c909 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73  iMem);.        s
2c90a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2c90b 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
2c90c 73 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  sr);.      }else
2c90d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2c90e 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e  E_OMIT_BTREECOUN
2c90f 54 20 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20  T */.      {.   
2c910 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66       /* Check if
2c911 20 74 68 65 20 71 75 65 72 79 20 69 73 20 6f 66   the query is of
2c912 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
2c913 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20  owing forms:.   
2c914 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c915 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 69 6e 28  **   SELECT min(
2c916 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20  x) FROM ....    
2c917 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20      **   SELECT 
2c918 6d 61 78 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a  max(x) FROM ....
2c919 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c91a 20 20 20 2a 2a 20 49 66 20 69 74 20 69 73 2c 20     ** If it is, 
2c91b 74 68 65 6e 20 61 73 6b 20 74 68 65 20 63 6f 64  then ask the cod
2c91c 65 20 69 6e 20 77 68 65 72 65 2e 63 20 74 6f 20  e in where.c to 
2c91d 61 74 74 65 6d 70 74 20 74 6f 20 73 6f 72 74 20  attempt to sort 
2c91e 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 20 20  results.        
2c91f 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65 20 77  ** as if there w
2c920 61 73 20 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20  as an "ORDER ON 
2c921 78 22 20 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20  x" or "ORDER ON 
2c922 78 20 44 45 53 43 22 20 63 6c 61 75 73 65 2e 20  x DESC" clause. 
2c923 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 77  .        ** If w
2c924 68 65 72 65 2e 63 20 69 73 20 61 62 6c 65 20 74  here.c is able t
2c925 6f 20 70 72 6f 64 75 63 65 20 72 65 73 75 6c 74  o produce result
2c926 73 20 73 6f 72 74 65 64 20 69 6e 20 74 68 69 73  s sorted in this
2c927 20 6f 72 64 65 72 2c 20 74 68 65 6e 0a 20 20 20   order, then.   
2c928 20 20 20 20 20 2a 2a 20 61 64 64 20 76 64 62 65       ** add vdbe
2c929 20 63 6f 64 65 20 74 6f 20 62 72 65 61 6b 20 6f   code to break o
2c92a 75 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73  ut of the proces
2c92b 73 69 6e 67 20 6c 6f 6f 70 20 61 66 74 65 72 20  sing loop after 
2c92c 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
2c92d 66 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20  first iteration 
2c92e 28 73 69 6e 63 65 20 74 68 65 20 66 69 72 73 74  (since the first
2c92f 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
2c930 65 20 6c 6f 6f 70 20 69 73 20 0a 20 20 20 20 20  e loop is .     
2c931 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64     ** guaranteed
2c932 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74   to operate on t
2c933 68 65 20 72 6f 77 20 77 69 74 68 20 74 68 65 20  he row with the 
2c934 6d 69 6e 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d  minimum or maxim
2c935 75 6d 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 76  um .        ** v
2c936 61 6c 75 65 20 6f 66 20 78 2c 20 74 68 65 20 6f  alue of x, the o
2c937 6e 6c 79 20 72 6f 77 20 72 65 71 75 69 72 65 64  nly row required
2c938 29 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  )..        **.  
2c939 20 20 20 20 20 20 2a 2a 20 41 20 73 70 65 63 69        ** A speci
2c93a 61 6c 20 66 6c 61 67 20 6d 75 73 74 20 62 65 20  al flag must be 
2c93b 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
2c93c 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 74 6f  3WhereBegin() to
2c93d 20 73 6c 69 67 68 74 6c 79 0a 20 20 20 20 20 20   slightly.      
2c93e 20 20 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68 61    ** modify beha
2c93f 76 69 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73  viour as follows
2c940 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  :.        **.   
2c941 20 20 20 20 20 2a 2a 20 20 20 2b 20 49 66 20 74       **   + If t
2c942 68 65 20 71 75 65 72 79 20 69 73 20 61 20 22 53  he query is a "S
2c943 45 4c 45 43 54 20 6d 69 6e 28 78 29 22 2c 20 74  ELECT min(x)", t
2c944 68 65 6e 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64  hen the loop cod
2c945 65 64 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a  ed by.        **
2c946 20 20 20 20 20 77 68 65 72 65 2e 63 20 73 68 6f       where.c sho
2c947 75 6c 64 20 6e 6f 74 20 69 74 65 72 61 74 65 20  uld not iterate 
2c948 6f 76 65 72 20 61 6e 79 20 76 61 6c 75 65 73 20  over any values 
2c949 77 69 74 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75  with a NULL valu
2c94a 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20  e.        **    
2c94b 20 66 6f 72 20 78 2e 0a 20 20 20 20 20 20 20 20   for x..        
2c94c 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
2c94d 2b 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  + The optimizer 
2c94e 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20  code in where.c 
2c94f 28 74 68 65 20 74 68 69 6e 67 20 74 68 61 74 20  (the thing that 
2c950 64 65 63 69 64 65 73 20 77 68 69 63 68 0a 20 20  decides which.  
2c951 20 20 20 20 20 20 2a 2a 20 20 20 20 20 69 6e 64        **     ind
2c952 65 78 20 6f 72 20 69 6e 64 69 63 65 73 20 74 6f  ex or indices to
2c953 20 75 73 65 29 20 73 68 6f 75 6c 64 20 70 6c 61   use) should pla
2c954 63 65 20 61 20 64 69 66 66 65 72 65 6e 74 20 70  ce a different p
2c955 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20 20 20 20  riority on .    
2c956 20 20 20 20 2a 2a 20 20 20 20 20 73 61 74 69 73      **     satis
2c957 66 79 69 6e 67 20 74 68 65 20 27 4f 52 44 45 52  fying the 'ORDER
2c958 20 42 59 27 20 63 6c 61 75 73 65 20 74 68 61 6e   BY' clause than
2c959 20 69 74 20 64 6f 65 73 20 69 6e 20 6f 74 68 65   it does in othe
2c95a 72 20 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20  r cases..       
2c95b 20 2a 2a 20 20 20 20 20 52 65 66 65 72 20 74 6f   **     Refer to
2c95c 20 63 6f 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e   code and commen
2c95d 74 73 20 69 6e 20 77 68 65 72 65 2e 63 20 66 6f  ts in where.c fo
2c95e 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 20 20 20  r details..     
2c95f 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 45 78     */.        Ex
2c960 70 72 4c 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20  prList *pMinMax 
2c961 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 75 38 20  = 0;.        u8 
2c962 66 6c 61 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65  flag = minMaxQue
2c963 72 79 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69  ry(p);.        i
2c964 66 28 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20  f( flag ){.     
2c965 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2c966 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d  prHasProperty(p-
2c967 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
2c968 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2c969 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  t) );.          
2c96a 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65  pMinMax = sqlite
2c96b 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
2c96c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d   p->pEList->a[0]
2c96d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c  .pExpr->x.pList,
2c96e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 44  0);.          pD
2c96f 65 6c 20 3d 20 70 4d 69 6e 4d 61 78 3b 0a 20 20  el = pMinMax;.  
2c970 20 20 20 20 20 20 20 20 69 66 28 20 70 4d 69 6e          if( pMin
2c971 4d 61 78 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c  Max && !db->mall
2c972 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2c973 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d          pMinMax-
2c974 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20  >a[0].sortOrder 
2c975 3d 20 66 6c 61 67 21 3d 57 48 45 52 45 5f 4f 52  = flag!=WHERE_OR
2c976 44 45 52 42 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a  DERBY_MIN ?1:0;.
2c977 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e              pMin
2c978 4d 61 78 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  Max->a[0].pExpr-
2c979 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b  >op = TK_COLUMN;
2c97a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2c97b 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
2c97c 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 72    /* This case r
2c97d 75 6e 73 20 69 66 20 74 68 65 20 61 67 67 72 65  uns if the aggre
2c97e 67 61 74 65 20 68 61 73 20 6e 6f 20 47 52 4f 55  gate has no GROU
2c97f 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 54 68  P BY clause.  Th
2c980 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f  e.        ** pro
2c981 63 65 73 73 69 6e 67 20 69 73 20 6d 75 63 68 20  cessing is much 
2c982 73 69 6d 70 6c 65 72 20 73 69 6e 63 65 20 74 68  simpler since th
2c983 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69  ere is only a si
2c984 6e 67 6c 65 20 72 6f 77 0a 20 20 20 20 20 20 20  ngle row.       
2c985 20 2a 2a 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20   ** of output.. 
2c986 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2c987 20 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74    resetAccumulat
2c988 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  or(pParse, &sAgg
2c989 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70  Info);.        p
2c98a 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
2c98b 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
2c98c 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
2c98d 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c 20 66 6c  re, &pMinMax, fl
2c98e 61 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ag);.        if(
2c98f 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20   pWInfo==0 ){.  
2c990 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2c991 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2c992 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20  , pDel);.       
2c993 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
2c994 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nd;.        }.  
2c995 20 20 20 20 20 20 75 70 64 61 74 65 41 63 63 75        updateAccu
2c996 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20  mulator(pParse, 
2c997 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20  &sAggInfo);.    
2c998 20 20 20 20 69 66 28 20 21 70 4d 69 6e 4d 61 78      if( !pMinMax
2c999 20 26 26 20 66 6c 61 67 20 29 7b 0a 20 20 20 20   && flag ){.    
2c99a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c99b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2c99c 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f 2d 3e 69  to, 0, pWInfo->i
2c99d 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20  Break);.        
2c99e 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2c99f 2c 20 22 25 73 28 29 20 62 79 20 69 6e 64 65 78  , "%s() by index
2c9a0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2c9a1 20 20 20 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f     (flag==WHERE_
2c9a2 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e  ORDERBY_MIN?"min
2c9a3 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20 20 20 20  ":"max")));.    
2c9a4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
2c9a5 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2c9a6 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 66  Info);.        f
2c9a7 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69  inalizeAggFuncti
2c9a8 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41 67  ons(pParse, &sAg
2c9a9 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a  gInfo);.      }.
2c9aa 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20  .      pOrderBy 
2c9ab 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
2c9ac 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
2c9ad 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61  arse, pHaving, a
2c9ae 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45 5f 4a  ddrEnd, SQLITE_J
2c9af 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
2c9b0 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f    selectInnerLoo
2c9b1 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e  p(pParse, p, p->
2c9b2 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c  pEList, 0, 0, 0,
2c9b3 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20   -1, .          
2c9b4 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65 73              pDes
2c9b5 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64 64 72  t, addrEnd, addr
2c9b6 45 6e 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  End);.      sqli
2c9b7 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2c9b8 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20  e(db, pDel);.   
2c9b9 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2c9ba 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2c9bb 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20  , addrEnd);.    
2c9bc 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67  .  } /* endif ag
2c9bd 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a 2f  gregate query */
2c9be 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2c9bf 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2c9c0 6c 61 75 73 65 2c 20 74 68 65 6e 20 77 65 20 6e  lause, then we n
2c9c1 65 65 64 20 74 6f 20 73 6f 72 74 20 74 68 65 20  eed to sort the 
2c9c2 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20 61 6e 64  results.  ** and
2c9c3 20 73 65 6e 64 20 74 68 65 6d 20 74 6f 20 74 68   send them to th
2c9c4 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 65 20 62  e callback one b
2c9c5 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  y one..  */.  if
2c9c6 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
2c9c7 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61    generateSortTa
2c9c8 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 76 2c  il(pParse, p, v,
2c9c9 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20   pEList->nExpr, 
2c9ca 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f  pDest);.  }..  /
2c9cb 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73  * Jump here to s
2c9cc 6b 69 70 20 74 68 69 73 20 71 75 65 72 79 0a 20  kip this query. 
2c9cd 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2c9ce 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2c9cf 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68   iEnd);..  /* Th
2c9d0 65 20 53 45 4c 45 43 54 20 77 61 73 20 73 75 63  e SELECT was suc
2c9d1 63 65 73 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e  cessfully coded.
2c9d2 20 20 20 53 65 74 20 74 68 65 20 72 65 74 75 72     Set the retur
2c9d3 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a  n code to 0.  **
2c9d4 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20   to indicate no 
2c9d5 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72  errors..  */.  r
2c9d6 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e  c = 0;..  /* Con
2c9d7 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65  trol jumps to he
2c9d8 72 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69  re if an error i
2c9d9 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 62  s encountered ab
2c9da 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a  ove, or upon.  *
2c9db 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 64  * successful cod
2c9dc 69 6e 67 20 6f 66 20 74 68 65 20 53 45 4c 45 43  ing of the SELEC
2c9dd 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65  T..  */.select_e
2c9de 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69  nd:..  /* Identi
2c9df 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  fy column names 
2c9e0 69 66 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  if results of th
2c9e1 65 20 53 45 4c 45 43 54 20 61 72 65 20 74 6f 20  e SELECT are to 
2c9e2 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a  be output..  */.
2c9e3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2c9e4 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d 3e 65 44  _OK && pDest->eD
2c9e5 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2c9e6 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43  ){.    generateC
2c9e7 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
2c9e8 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c  e, pTabList, pEL
2c9e9 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  ist);.  }..  sql
2c9ea 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 73  ite3DbFree(db, s
2c9eb 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20  AggInfo.aCol);. 
2c9ec 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2c9ed 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e  b, sAggInfo.aFun
2c9ee 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
2c9ef 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .}..#if defined(
2c9f0 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
2c9f1 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2c9f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c9f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c9f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c9f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c9f6 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2c9f7 67 20 63 6f 64 65 20 69 73 20 75 73 65 64 20 66  g code is used f
2c9f8 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  or testing and d
2c9f9 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20  ebugging only.  
2c9fa 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74  The code.** that
2c9fb 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f   follows does no
2c9fc 74 20 61 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d  t appear in norm
2c9fd 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a  al builds..**.**
2c9fe 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2c9ff 61 72 65 20 75 73 65 64 20 74 6f 20 70 72 69 6e  are used to prin
2ca00 74 20 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e  t out the conten
2ca01 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70 61 72 74  t of all or part
2ca02 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72 73 65 20   of a .** parse 
2ca03 73 74 72 75 63 74 75 72 65 73 20 73 75 63 68 20  structures such 
2ca04 61 73 20 53 65 6c 65 63 74 20 6f 72 20 45 78 70  as Select or Exp
2ca05 72 2e 20 20 53 75 63 68 20 70 72 69 6e 74 6f 75  r.  Such printou
2ca06 74 73 20 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a  ts are useful.**
2ca07 20 66 6f 72 20 68 65 6c 70 69 6e 67 20 74 6f 20   for helping to 
2ca08 75 6e 64 65 72 73 74 61 6e 64 20 77 68 61 74 20  understand what 
2ca09 69 73 20 68 61 70 70 65 6e 69 6e 67 20 69 6e 73  is happening ins
2ca0a 69 64 65 20 74 68 65 20 63 6f 64 65 20 67 65 6e  ide the code gen
2ca0b 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67  erator.** during
2ca0c 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f   the execution o
2ca0d 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54  f complex SELECT
2ca0e 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
2ca0f 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
2ca10 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20   are not called 
2ca11 61 6e 79 77 68 65 72 65 20 66 72 6f 6d 20 77 69  anywhere from wi
2ca12 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a  thin the normal.
2ca13 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e 20 20 54  ** code base.  T
2ca14 68 65 6e 20 61 72 65 20 69 6e 74 65 6e 64 65 64  hen are intended
2ca15 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 66 72   to be called fr
2ca16 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 64 65  om within the de
2ca17 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f  bugger.** or fro
2ca18 6d 20 74 65 6d 70 6f 72 61 72 79 20 22 70 72 69  m temporary "pri
2ca19 6e 74 66 22 20 73 74 61 74 65 6d 65 6e 74 73 20  ntf" statements 
2ca1a 69 6e 73 65 72 74 65 64 20 66 6f 72 20 64 65 62  inserted for deb
2ca1b 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ugging..*/.SQLIT
2ca1c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2ca1d 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28  qlite3PrintExpr(
2ca1e 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  Expr *p){.  if( 
2ca1f 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2ca20 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  (p, EP_IntValue)
2ca21 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   && p->u.zToken 
2ca22 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2ca23 62 75 67 50 72 69 6e 74 66 28 22 28 25 73 22 2c  bugPrintf("(%s",
2ca24 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20   p->u.zToken);. 
2ca25 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2ca26 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2ca27 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20 20  (%d", p->op);.  
2ca28 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74  }.  if( p->pLeft
2ca29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2ca2a 65 62 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b  ebugPrintf(" ");
2ca2b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
2ca2c 74 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 29 3b  tExpr(p->pLeft);
2ca2d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 52  .  }.  if( p->pR
2ca2e 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  ight ){.    sqli
2ca2f 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2ca30 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   ");.    sqlite3
2ca31 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 52 69  PrintExpr(p->pRi
2ca32 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ght);.  }.  sqli
2ca33 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2ca34 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  )");.}.SQLITE_PR
2ca35 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2ca36 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
2ca37 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
2ca38 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
2ca39 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2ca3a 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2ca3b 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2ca3c 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  (pList->a[i].pEx
2ca3d 70 72 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 70  pr);.    if( i<p
2ca3e 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 29 7b  List->nExpr-1 ){
2ca3f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
2ca40 62 75 67 50 72 69 6e 74 66 28 22 2c 20 22 29 3b  bugPrintf(", ");
2ca41 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c  .    }.  }.}.SQL
2ca42 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2ca43 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c   sqlite3PrintSel
2ca44 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 69  ect(Select *p, i
2ca45 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20 20 73 71  nt indent){.  sq
2ca46 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2ca47 28 22 25 2a 73 53 45 4c 45 43 54 28 25 70 29 20  ("%*sSELECT(%p) 
2ca48 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 2c 20 70  ", indent, "", p
2ca49 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 72 69 6e  );.  sqlite3Prin
2ca4a 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c  tExprList(p->pEL
2ca4b 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ist);.  sqlite3D
2ca4c 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
2ca4d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 72 63 20  ;.  if( p->pSrc 
2ca4e 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 50 72  ){.    char *zPr
2ca4f 65 66 69 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b  efix;.    int i;
2ca50 0a 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22  .    zPrefix = "
2ca51 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f 72 28 69  FROM";.    for(i
2ca52 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  =0; i<p->pSrc->n
2ca53 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
2ca54 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2ca55 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
2ca56 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20  ->pSrc->a[i];.  
2ca57 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2ca58 50 72 69 6e 74 66 28 22 25 2a 73 20 22 2c 20 69  Printf("%*s ", i
2ca59 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65 66 69 78  ndent+6, zPrefix
2ca5a 29 3b 0a 20 20 20 20 20 20 7a 50 72 65 66 69 78  );.      zPrefix
2ca5b 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 69 66 28   = "";.      if(
2ca5c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20   pItem->pSelect 
2ca5d 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2ca5e 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28  e3DebugPrintf("(
2ca5f 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71  \n");.        sq
2ca60 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74  lite3PrintSelect
2ca61 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c  (pItem->pSelect,
2ca62 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a 20 20 20   indent+10);.   
2ca63 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
2ca64 67 50 72 69 6e 74 66 28 22 25 2a 73 29 22 2c 20  gPrintf("%*s)", 
2ca65 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29 3b 0a 20  indent+8, "");. 
2ca66 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2ca67 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20  Item->zName ){. 
2ca68 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2ca69 62 75 67 50 72 69 6e 74 66 28 22 25 73 22 2c 20  bugPrintf("%s", 
2ca6a 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
2ca6b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ca6c 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a   pItem->pTab ){.
2ca6d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2ca6e 65 62 75 67 50 72 69 6e 74 66 28 22 28 74 61 62  ebugPrintf("(tab
2ca6f 6c 65 3a 20 25 73 29 22 2c 20 70 49 74 65 6d 2d  le: %s)", pItem-
2ca70 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  >pTab->zName);. 
2ca71 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ca72 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
2ca73 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2ca74 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 41  3DebugPrintf(" A
2ca75 53 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 41  S %s", pItem->zA
2ca76 6c 69 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lias);.      }. 
2ca77 20 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 70 53       if( i<p->pS
2ca78 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20  rc->nSrc-1 ){.  
2ca79 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
2ca7a 75 67 50 72 69 6e 74 66 28 22 2c 22 29 3b 0a 20  ugPrintf(",");. 
2ca7b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2ca7c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2ca7d 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  "\n");.    }.  }
2ca7e 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65  .  if( p->pWhere
2ca7f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2ca80 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
2ca81 57 48 45 52 45 20 22 2c 20 69 6e 64 65 6e 74 2c  WHERE ", indent,
2ca82 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2ca83 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 57  3PrintExpr(p->pW
2ca84 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  here);.    sqlit
2ca85 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2ca86 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n");.  }.  if( p
2ca87 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ->pGroupBy ){.  
2ca88 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2ca89 69 6e 74 66 28 22 25 2a 73 20 47 52 4f 55 50 20  intf("%*s GROUP 
2ca8a 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  BY ", indent, ""
2ca8b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2ca8c 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  intExprList(p->p
2ca8d 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 73 71  GroupBy);.    sq
2ca8e 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2ca8f 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66  ("\n");.  }.  if
2ca90 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 29 7b 0a  ( p->pHaving ){.
2ca91 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2ca92 50 72 69 6e 74 66 28 22 25 2a 73 20 48 41 56 49  Printf("%*s HAVI
2ca93 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  NG ", indent, ""
2ca94 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2ca95 69 6e 74 45 78 70 72 28 70 2d 3e 70 48 61 76 69  intExpr(p->pHavi
2ca96 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ng);.    sqlite3
2ca97 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
2ca98 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
2ca99 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2ca9a 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2ca9b 74 66 28 22 25 2a 73 20 4f 52 44 45 52 20 42 59  tf("%*s ORDER BY
2ca9c 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
2ca9d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
2ca9e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72  tExprList(p->pOr
2ca9f 64 65 72 42 79 29 3b 0a 20 20 20 20 73 71 6c 69  derBy);.    sqli
2caa0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2caa1 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 45  \n");.  }.}./* E
2caa2 6e 64 20 6f 66 20 74 68 65 20 73 74 72 75 63 74  nd of the struct
2caa3 75 72 65 20 64 65 62 75 67 20 70 72 69 6e 74 69  ure debug printi
2caa4 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a  ng code.********
2caa5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caa9 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  *****/.#endif /*
2caaa 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
2caab 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64  TEST) || defined
2caac 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a  (SQLITE_DEBUG) *
2caad 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2caae 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c 65 63 74  ** End of select
2caaf 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2cab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cab1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cab2 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2cab3 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 61  ** Begin file ta
2cab4 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ble.c **********
2cab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cab7 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2cab8 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2cab9 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2caba 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2cabb 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2cabc 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2cabd 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2cabe 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2cabf 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2cac0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2cac1 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2cac2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2cac3 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2cac4 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2cac5 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2cac6 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2cac7 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2cac8 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2cac9 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2caca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cacb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cacc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cacd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cace 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2cacf 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2cad0 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  the sqlite3_get_
2cad1 74 61 62 6c 65 28 29 20 61 6e 64 20 73 71 6c 69  table() and sqli
2cad2 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
2cad3 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f  .** interface ro
2cad4 75 74 69 6e 65 73 2e 20 20 54 68 65 73 65 20 61  utines.  These a
2cad5 72 65 20 6a 75 73 74 20 77 72 61 70 70 65 72 73  re just wrappers
2cad6 20 61 72 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e   around the main
2cad7 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f  .** interface ro
2cad8 75 74 69 6e 65 20 6f 66 20 73 71 6c 69 74 65 33  utine of sqlite3
2cad9 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  _exec()..**.** T
2cada 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
2cadb 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 65 20  e in a separate 
2cadc 66 69 6c 65 73 20 73 6f 20 74 68 61 74 20 74 68  files so that th
2cadd 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c  ey will not be l
2cade 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74 68 65 79  inked.** if they
2cadf 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a   are not used..*
2cae0 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
2cae1 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45  E_OMIT_GET_TABLE
2cae2 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ../*.** This str
2cae3 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
2cae4 6f 20 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d  o pass data from
2cae5 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2cae6 6c 65 28 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20  le() through.** 
2cae7 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
2cae8 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 73  function is uses
2cae9 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65   to build the re
2caea 73 75 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  sult..*/.typedef
2caeb 20 73 74 72 75 63 74 20 54 61 62 52 65 73 75 6c   struct TabResul
2caec 74 20 7b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52  t {.  char **azR
2caed 65 73 75 6c 74 3b 20 20 20 2f 2a 20 41 63 63 75  esult;   /* Accu
2caee 6d 75 6c 61 74 65 64 20 6f 75 74 70 75 74 20 2a  mulated output *
2caef 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  /.  char *zErrMs
2caf0 67 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  g;     /* Error 
2caf1 6d 65 73 73 61 67 65 20 74 65 78 74 2c 20 69 66  message text, if
2caf2 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2caf3 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
2caf4 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74  ;        /* Slot
2caf5 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
2caf6 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20  azResult[] */.  
2caf7 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20  int nRow;       
2caf8 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2caf9 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65 73 75  rows in the resu
2cafa 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  lt */.  int nCol
2cafb 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  umn;       /* Nu
2cafc 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2cafd 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  in the result */
2cafe 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
2caff 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 75        /* Slots u
2cb00 73 65 64 20 69 6e 20 61 7a 52 65 73 75 6c 74 5b  sed in azResult[
2cb01 5d 2e 20 20 28 6e 52 6f 77 2b 31 29 2a 6e 43 6f  ].  (nRow+1)*nCo
2cb02 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  lumn */.  int rc
2cb03 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2cb04 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
2cb05 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
2cb06 2a 2f 0a 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a  */.} TabResult;.
2cb07 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2cb08 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ine is called on
2cb09 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  ce for each row 
2cb0a 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 74 61  in the result ta
2cb0b 62 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a  ble.  Its job.**
2cb0c 20 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74   is to fill in t
2cb0d 68 65 20 54 61 62 52 65 73 75 6c 74 20 73 74 72  he TabResult str
2cb0e 75 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61  ucture appropria
2cb0f 74 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67  tely, allocating
2cb10 20 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61   new.** memory a
2cb11 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a  s necessary..*/.
2cb12 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
2cb13 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 28  e3_get_table_cb(
2cb14 76 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e 74 20  void *pArg, int 
2cb15 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67  nCol, char **arg
2cb16 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b  v, char **colv){
2cb17 0a 20 20 54 61 62 52 65 73 75 6c 74 20 2a 70 20  .  TabResult *p 
2cb18 3d 20 28 54 61 62 52 65 73 75 6c 74 2a 29 70 41  = (TabResult*)pA
2cb19 72 67 3b 20 20 2f 2a 20 52 65 73 75 6c 74 20 61  rg;  /* Result a
2cb1a 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
2cb1b 69 6e 74 20 6e 65 65 64 3b 20 20 20 20 20 20 20  int need;       
2cb1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb1d 20 20 2f 2a 20 53 6c 6f 74 73 20 6e 65 65 64 65    /* Slots neede
2cb1e 64 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74  d in p->azResult
2cb1f 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  [] */.  int i;  
2cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb21 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2cb22 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63  p counter */.  c
2cb23 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
2cb24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb25 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6c   /* A single col
2cb26 75 6d 6e 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f  umn of result */
2cb27 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2cb28 20 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68   there is enough
2cb29 20 73 70 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52   space in p->azR
2cb2a 65 73 75 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76  esult to hold ev
2cb2b 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65  erything.  ** we
2cb2c 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
2cb2d 72 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f  r from this invo
2cb2e 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 61  cation of the ca
2cb2f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  llback..  */.  i
2cb30 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26  f( p->nRow==0 &&
2cb31 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20   argv!=0 ){.    
2cb32 6e 65 65 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20  need = nCol*2;. 
2cb33 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64   }else{.    need
2cb34 20 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69   = nCol;.  }.  i
2cb35 66 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65  f( p->nData + ne
2cb36 65 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29  ed > p->nAlloc )
2cb37 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e  {.    char **azN
2cb38 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f  ew;.    p->nAllo
2cb39 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  c = p->nAlloc*2 
2cb3a 2b 20 6e 65 65 64 3b 0a 20 20 20 20 61 7a 4e 65  + need;.    azNe
2cb3b 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
2cb3c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74  loc( p->azResult
2cb3d 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
2cb3e 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20  p->nAlloc );.   
2cb3f 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20   if( azNew==0 ) 
2cb40 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  goto malloc_fail
2cb41 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73  ed;.    p->azRes
2cb42 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d  ult = azNew;.  }
2cb43 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
2cb44 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2c  s the first row,
2cb45 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61   then generate a
2cb46 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e 74  n extra row cont
2cb47 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  aining.  ** the 
2cb48 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c  names of all col
2cb49 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  umns..  */.  if(
2cb4a 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20   p->nRow==0 ){. 
2cb4b 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20     p->nColumn = 
2cb4c 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nCol;.    for(i=
2cb4d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2cb4e 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
2cb4f 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
2cb50 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20   colv[i]);.     
2cb51 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f   if( z==0 ) goto
2cb52 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a   malloc_failed;.
2cb53 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
2cb54 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
2cb55 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  z;.    }.  }else
2cb56 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21   if( p->nColumn!
2cb57 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c  =nCol ){.    sql
2cb58 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72  ite3_free(p->zEr
2cb59 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
2cb5a 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f  rrMsg = sqlite3_
2cb5b 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  mprintf(.       
2cb5c 22 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  "sqlite3_get_tab
2cb5d 6c 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68  le() called with
2cb5e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63   two or more inc
2cb5f 6f 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65  ompatible querie
2cb60 73 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d  s".    );.    p-
2cb61 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
2cb62 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  OR;.    return 1
2cb63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79  ;.  }..  /* Copy
2cb64 20 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64 61   over the row da
2cb65 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72  ta.  */.  if( ar
2cb66 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72  gv!=0 ){.    for
2cb67 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2cb68 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72  +){.      if( ar
2cb69 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[i]==0 ){.    
2cb6a 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20      z = 0;.     
2cb6b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2cb6c 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
2cb6d 74 72 6c 65 6e 33 30 28 61 72 67 76 5b 69 5d 29  trlen30(argv[i])
2cb6e 2b 31 3b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20  +1;.        z = 
2cb6f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
2cb70 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  n );.        if(
2cb71 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c   z==0 ) goto mal
2cb72 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  loc_failed;.    
2cb73 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 61 72      memcpy(z, ar
2cb74 67 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20 20 20 20  gv[i], n);.     
2cb75 20 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65   }.      p->azRe
2cb76 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d  sult[p->nData++]
2cb77 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = z;.    }.    
2cb78 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20 7d 0a 20  p->nRow++;.  }. 
2cb79 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6d 61 6c 6c   return 0;..mall
2cb7a 6f 63 5f 66 61 69 6c 65 64 3a 0a 20 20 70 2d 3e  oc_failed:.  p->
2cb7b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2cb7c 4d 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  M;.  return 1;.}
2cb7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68  ../*.** Query th
2cb7e 65 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74  e database.  But
2cb7f 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f   instead of invo
2cb80 6b 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 20  king a callback 
2cb81 66 6f 72 20 65 61 63 68 20 72 6f 77 2c 0a 2a 2a  for each row,.**
2cb82 20 6d 61 6c 6c 6f 63 28 29 20 66 6f 72 20 73 70   malloc() for sp
2cb83 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
2cb84 72 65 73 75 6c 74 20 61 6e 64 20 72 65 74 75 72  result and retur
2cb85 6e 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 73  n the entire res
2cb86 75 6c 74 73 0a 2a 2a 20 61 74 20 74 68 65 20 63  ults.** at the c
2cb87 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65  onclusion of the
2cb88 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   call..**.** The
2cb89 20 72 65 73 75 6c 74 20 74 68 61 74 20 69 73 20   result that is 
2cb8a 77 72 69 74 74 65 6e 20 74 6f 20 2a 2a 2a 70 61  written to ***pa
2cb8b 7a 52 65 73 75 6c 74 20 69 73 20 68 65 6c 64 20  zResult is held 
2cb8c 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
2cb8d 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ed.** from mallo
2cb8e 63 28 29 2e 20 20 42 75 74 20 74 68 65 20 63 61  c().  But the ca
2cb8f 6c 6c 65 72 20 63 61 6e 6e 6f 74 20 66 72 65 65  ller cannot free
2cb90 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 64 69 72   this memory dir
2cb91 65 63 74 6c 79 2e 20 20 0a 2a 2a 20 49 6e 73 74  ectly.  .** Inst
2cb92 65 61 64 2c 20 74 68 65 20 65 6e 74 69 72 65 20  ead, the entire 
2cb93 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20  table should be 
2cb94 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
2cb95 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 77  3_free_table() w
2cb96 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69  hen.** the calli
2cb97 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 20  ng procedure is 
2cb98 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 69  finished using i
2cb99 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
2cb9a 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
2cb9b 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65  _table(.  sqlite
2cb9c 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2cb9d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2cb9e 61 62 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74  abase on which t
2cb9f 68 65 20 53 51 4c 20 65 78 65 63 75 74 65 73 20  he SQL executes 
2cba0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2cba1 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20  *zSql,          
2cba2 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62   /* The SQL to b
2cba3 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20  e executed */.  
2cba4 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c  char ***pazResul
2cba5 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  t,          /* W
2cba6 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20  rite the result 
2cba7 74 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20  table here */.  
2cba8 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20  int *pnRow,     
2cba9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
2cbaa 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
2cbab 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72  of rows in the r
2cbac 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20  esult here */.  
2cbad 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20  int *pnColumn,  
2cbae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
2cbaf 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
2cbb0 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65  of columns of re
2cbb1 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 63  sult here */.  c
2cbb2 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
2cbb3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2cbb4 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ite error messag
2cbb5 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  es here */.){.  
2cbb6 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 52 65 73  int rc;.  TabRes
2cbb7 75 6c 74 20 72 65 73 3b 0a 0a 20 20 2a 70 61 7a  ult res;..  *paz
2cbb8 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66  Result = 0;.  if
2cbb9 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e  ( pnColumn ) *pn
2cbba 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66  Column = 0;.  if
2cbbb 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77  ( pnRow ) *pnRow
2cbbc 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 7a 45 72   = 0;.  if( pzEr
2cbbd 72 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d 73 67  rMsg ) *pzErrMsg
2cbbe 20 3d 20 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72   = 0;.  res.zErr
2cbbf 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e  Msg = 0;.  res.n
2cbc0 52 6f 77 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e  Row = 0;.  res.n
2cbc1 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65  Column = 0;.  re
2cbc2 73 2e 6e 44 61 74 61 20 3d 20 31 3b 0a 20 20 72  s.nData = 1;.  r
2cbc3 65 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a  es.nAlloc = 20;.
2cbc4 20 20 72 65 73 2e 72 63 20 3d 20 53 51 4c 49 54    res.rc = SQLIT
2cbc5 45 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65  E_OK;.  res.azRe
2cbc6 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  sult = sqlite3_m
2cbc7 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68 61  alloc(sizeof(cha
2cbc8 72 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29  r*)*res.nAlloc )
2cbc9 3b 0a 20 20 69 66 28 20 72 65 73 2e 61 7a 52 65  ;.  if( res.azRe
2cbca 73 75 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  sult==0 ){.     
2cbcb 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51  db->errCode = SQ
2cbcc 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2cbcd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2cbce 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e  OMEM;.  }.  res.
2cbcf 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b  azResult[0] = 0;
2cbd0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2cbd1 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73  exec(db, zSql, s
2cbd2 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
2cbd3 5f 63 62 2c 20 26 72 65 73 2c 20 70 7a 45 72 72  _cb, &res, pzErr
2cbd4 4d 73 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Msg);.  assert( 
2cbd5 73 69 7a 65 6f 66 28 72 65 73 2e 61 7a 52 65 73  sizeof(res.azRes
2cbd6 75 6c 74 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66  ult[0])>= sizeof
2cbd7 28 72 65 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20  (res.nData) );. 
2cbd8 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d   res.azResult[0]
2cbd9 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f   = SQLITE_INT_TO
2cbda 5f 50 54 52 28 72 65 73 2e 6e 44 61 74 61 29 3b  _PTR(res.nData);
2cbdb 0a 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29  .  if( (rc&0xff)
2cbdc 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29  ==SQLITE_ABORT )
2cbdd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
2cbde 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a  ee_table(&res.az
2cbdf 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20  Result[1]);.    
2cbe0 69 66 28 20 72 65 73 2e 7a 45 72 72 4d 73 67 20  if( res.zErrMsg 
2cbe1 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 7a 45  ){.      if( pzE
2cbe2 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20  rrMsg ){.       
2cbe3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70   sqlite3_free(*p
2cbe4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
2cbe5 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
2cbe6 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
2cbe7 73 22 2c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b  s",res.zErrMsg);
2cbe8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2cbe9 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e  qlite3_free(res.
2cbea 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a  zErrMsg);.    }.
2cbeb 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
2cbec 3d 20 72 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73  = res.rc;  /* As
2cbed 73 75 6d 65 20 33 32 2d 62 69 74 20 61 73 73 69  sume 32-bit assi
2cbee 67 6e 6d 65 6e 74 20 69 73 20 61 74 6f 6d 69 63  gnment is atomic
2cbef 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72   */.    return r
2cbf0 65 73 2e 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c  es.rc;.  }.  sql
2cbf1 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45  ite3_free(res.zE
2cbf2 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63  rrMsg);.  if( rc
2cbf3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2cbf4 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f     sqlite3_free_
2cbf5 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73  table(&res.azRes
2cbf6 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74  ult[1]);.    ret
2cbf7 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66  urn rc;.  }.  if
2cbf8 28 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73  ( res.nAlloc>res
2cbf9 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 63 68  .nData ){.    ch
2cbfa 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20  ar **azNew;.    
2cbfb 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  azNew = sqlite3_
2cbfc 72 65 61 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52  realloc( res.azR
2cbfd 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68  esult, sizeof(ch
2cbfe 61 72 2a 29 2a 72 65 73 2e 6e 44 61 74 61 20 29  ar*)*res.nData )
2cbff 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d  ;.    if( azNew=
2cc00 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2cc01 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26  te3_free_table(&
2cc02 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29  res.azResult[1])
2cc03 3b 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43  ;.      db->errC
2cc04 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ode = SQLITE_NOM
2cc05 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  EM;.      return
2cc06 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2cc07 20 20 20 7d 0a 20 20 20 20 72 65 73 2e 61 7a 52     }.    res.azR
2cc08 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20  esult = azNew;. 
2cc09 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20   }.  *pazResult 
2cc0a 3d 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  = &res.azResult[
2cc0b 31 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75  1];.  if( pnColu
2cc0c 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d  mn ) *pnColumn =
2cc0d 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20   res.nColumn;.  
2cc0e 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52  if( pnRow ) *pnR
2cc0f 6f 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20  ow = res.nRow;. 
2cc10 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2cc11 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2cc12 65 20 66 72 65 65 73 20 74 68 65 20 73 70 61 63  e frees the spac
2cc13 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65  e the sqlite3_ge
2cc14 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63  t_table() malloc
2cc15 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
2cc16 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66  I void sqlite3_f
2cc17 72 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68 61  ree_table(.  cha
2cc18 72 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20  r **azResult    
2cc19 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
2cc1a 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  t returned from 
2cc1b 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74  from sqlite3_get
2cc1c 5f 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20  _table() */.){. 
2cc1d 20 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29 7b   if( azResult ){
2cc1e 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  .    int i, n;. 
2cc1f 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20     azResult--;. 
2cc20 20 20 20 61 73 73 65 72 74 28 20 61 7a 52 65 73     assert( azRes
2cc21 75 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20  ult!=0 );.    n 
2cc22 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f  = SQLITE_PTR_TO_
2cc23 49 4e 54 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29  INT(azResult[0])
2cc24 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ;.    for(i=1; i
2cc25 3c 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a  <n; i++){ if( az
2cc26 52 65 73 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69  Result[i] ) sqli
2cc27 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c  te3_free(azResul
2cc28 74 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c  t[i]); }.    sql
2cc29 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75  ite3_free(azResu
2cc2a 6c 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  lt);.  }.}..#end
2cc2b 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2cc2c 54 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a  T_GET_TABLE */..
2cc2d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2cc2e 45 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a  End of table.c *
2cc2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2cc32 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2cc33 42 65 67 69 6e 20 66 69 6c 65 20 74 72 69 67 67  Begin file trigg
2cc34 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
2cc35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2cc37 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  /*.**.** The aut
2cc38 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2cc39 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2cc3a 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2cc3b 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2cc3c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
2cc3d 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2cc3e 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2cc3f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2cc40 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2cc41 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2cc42 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2cc43 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2cc44 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2cc45 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2cc46 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2cc47 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2cc48 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2cc49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc4d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2cc4e 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c  ontains the impl
2cc4f 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 54  ementation for T
2cc50 52 49 47 47 45 52 73 0a 2a 2f 0a 0a 23 69 66 6e  RIGGERs.*/..#ifn
2cc51 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2cc52 54 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65  TRIGGER./*.** De
2cc53 6c 65 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69  lete a linked li
2cc54 73 74 20 6f 66 20 54 72 69 67 67 65 72 53 74 65  st of TriggerSte
2cc55 70 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  p structures..*/
2cc56 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2cc57 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
2cc58 74 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71  teTriggerStep(sq
2cc59 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67  lite3 *db, Trigg
2cc5a 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
2cc5b 53 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28 20  Step){.  while( 
2cc5c 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a  pTriggerStep ){.
2cc5d 20 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20      TriggerStep 
2cc5e 2a 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67 65  * pTmp = pTrigge
2cc5f 72 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69 67  rStep;.    pTrig
2cc60 67 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67  gerStep = pTrigg
2cc61 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a  erStep->pNext;..
2cc62 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2cc63 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2cc64 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c  pWhere);.    sql
2cc65 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2cc66 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78  te(db, pTmp->pEx
2cc67 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c  prList);.    sql
2cc68 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2cc69 28 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65  (db, pTmp->pSele
2cc6a 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ct);.    sqlite3
2cc6b 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
2cc6c 20 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b   pTmp->pIdList);
2cc6d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ..    sqlite3DbF
2cc6e 72 65 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20  ree(db, pTmp);. 
2cc6f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
2cc70 6e 20 74 61 62 6c 65 20 70 54 61 62 2c 20 72 65  n table pTab, re
2cc71 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61  turn a list of a
2cc72 6c 6c 20 74 68 65 20 74 72 69 67 67 65 72 73 20  ll the triggers 
2cc73 61 74 74 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20  attached to .** 
2cc74 74 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 6c  the table. The l
2cc75 69 73 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64  ist is connected
2cc76 20 62 79 20 54 72 69 67 67 65 72 2e 70 4e 65 78   by Trigger.pNex
2cc77 74 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2a 0a 2a  t pointers..**.*
2cc78 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 74 72 69  * All of the tri
2cc79 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74 68  ggers on pTab th
2cc7a 61 74 20 61 72 65 20 69 6e 20 74 68 65 20 73 61  at are in the sa
2cc7b 6d 65 20 64 61 74 61 62 61 73 65 20 61 73 20 70  me database as p
2cc7c 54 61 62 0a 2a 2a 20 61 72 65 20 61 6c 72 65 61  Tab.** are alrea
2cc7d 64 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 70  dy attached to p
2cc7e 54 61 62 2d 3e 70 54 72 69 67 67 65 72 2e 20 20  Tab->pTrigger.  
2cc7f 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20  But there might 
2cc80 62 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  be additional.**
2cc81 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61   triggers on pTa
2cc82 62 20 69 6e 20 74 68 65 20 54 45 4d 50 20 73 63  b in the TEMP sc
2cc83 68 65 6d 61 2e 20 20 54 68 69 73 20 72 6f 75 74  hema.  This rout
2cc84 69 6e 65 20 70 72 65 70 65 6e 64 73 20 61 6c 6c  ine prepends all
2cc85 0a 2a 2a 20 54 45 4d 50 20 74 72 69 67 67 65 72  .** TEMP trigger
2cc86 73 20 6f 6e 20 70 54 61 62 20 74 6f 20 74 68 65  s on pTab to the
2cc87 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
2cc88 65 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  e pTab->pTrigger
2cc89 20 6c 69 73 74 0a 2a 2a 20 61 6e 64 20 72 65 74   list.** and ret
2cc8a 75 72 6e 73 20 74 68 65 20 63 6f 6d 62 69 6e 65  urns the combine
2cc8b 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f  d list..**.** To
2cc8c 20 73 74 61 74 65 20 69 74 20 61 6e 6f 74 68 65   state it anothe
2cc8d 72 20 77 61 79 3a 20 20 54 68 69 73 20 72 6f 75  r way:  This rou
2cc8e 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6c  tine returns a l
2cc8f 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67  ist of all trigg
2cc90 65 72 73 0a 2a 2a 20 74 68 61 74 20 66 69 72 65  ers.** that fire
2cc91 20 6f 66 66 20 6f 66 20 70 54 61 62 2e 20 20 54   off of pTab.  T
2cc92 68 65 20 6c 69 73 74 20 77 69 6c 6c 20 69 6e 63  he list will inc
2cc93 6c 75 64 65 20 61 6e 79 20 54 45 4d 50 20 74 72  lude any TEMP tr
2cc94 69 67 67 65 72 73 20 6f 6e 0a 2a 2a 20 70 54 61  iggers on.** pTa
2cc95 62 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65  b as well as the
2cc96 20 74 72 69 67 67 65 72 73 20 6c 69 73 65 64 20   triggers lised 
2cc97 69 6e 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65  in pTab->pTrigge
2cc98 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
2cc99 56 41 54 45 20 54 72 69 67 67 65 72 20 2a 73 71  VATE Trigger *sq
2cc9a 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74  lite3TriggerList
2cc9b 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2cc9c 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
2cc9d 53 63 68 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70  Schema * const p
2cc9e 54 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72  TmpSchema = pPar
2cc9f 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  se->db->aDb[1].p
2cca0 53 63 68 65 6d 61 3b 0a 20 20 54 72 69 67 67 65  Schema;.  Trigge
2cca1 72 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20  r *pList = 0;   
2cca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cca3 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
2cca4 72 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  rs to return */.
2cca5 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64  .  if( pParse->d
2cca6 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29  isableTriggers )
2cca7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2cca8 20 20 7d 0a 0a 20 20 69 66 28 20 70 54 6d 70 53    }..  if( pTmpS
2cca9 63 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63  chema!=pTab->pSc
2ccaa 68 65 6d 61 20 29 7b 0a 20 20 20 20 48 61 73 68  hema ){.    Hash
2ccab 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72  Elem *p;.    for
2ccac 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
2ccad 73 74 28 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e  st(&pTmpSchema->
2ccae 74 72 69 67 48 61 73 68 29 3b 20 70 3b 20 70 3d  trigHash); p; p=
2ccaf 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
2ccb0 29 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65  )){.      Trigge
2ccb1 72 20 2a 70 54 72 69 67 20 3d 20 28 54 72 69 67  r *pTrig = (Trig
2ccb2 67 65 72 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ger *)sqliteHash
2ccb3 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69  Data(p);.      i
2ccb4 66 28 20 70 54 72 69 67 2d 3e 70 54 61 62 53 63  f( pTrig->pTabSc
2ccb5 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
2ccb6 65 6d 61 0a 20 20 20 20 20 20 20 26 26 20 30 3d  ema.       && 0=
2ccb7 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
2ccb8 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54  pTrig->table, pT
2ccb9 61 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20  ab->zName) .    
2ccba 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72    ){.        pTr
2ccbb 69 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69  ig->pNext = (pLi
2ccbc 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54 61  st ? pList : pTa
2ccbd 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b->pTrigger);.  
2ccbe 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 70 54        pList = pT
2ccbf 72 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rig;.      }.   
2ccc0 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2ccc1 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20   (pList ? pList 
2ccc2 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  : pTab->pTrigger
2ccc3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
2ccc4 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
2ccc5 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74  e parser when it
2ccc6 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20 54   sees a CREATE T
2ccc7 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
2ccc8 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70 6f  .** up to the po
2ccc9 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49 4e  int of the BEGIN
2ccca 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67   before the trig
2cccb 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41 20  ger actions.  A 
2cccc 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75 63  Trigger.** struc
2cccd 74 75 72 65 20 69 73 20 67 65 6e 65 72 61 74 65  ture is generate
2ccce 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 69  d based on the i
2cccf 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c  nformation avail
2ccd0 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64 0a  able and stored.
2ccd1 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e  ** in pParse->pN
2ccd2 65 77 54 72 69 67 67 65 72 2e 20 20 41 66 74 65  ewTrigger.  Afte
2ccd3 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63  r the trigger ac
2ccd4 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
2ccd5 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73  parsed, the.** s
2ccd6 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
2ccd7 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ger() function i
2ccd8 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  s called to comp
2ccd9 6c 65 74 65 20 74 68 65 20 74 72 69 67 67 65 72  lete the trigger
2ccda 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  .** construction
2ccdb 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c   process..*/.SQL
2ccdc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2ccdd 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69   sqlite3BeginTri
2ccde 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
2ccdf 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54  Parse,      /* T
2cce0 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  he parse context
2cce1 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
2cce2 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
2cce3 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2cce4 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me1,      /* The
2cce5 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
2cce6 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  gger */.  Token 
2cce7 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a  *pName2,      /*
2cce8 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
2cce9 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e   trigger */.  in
2ccea 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20  t tr_tm,        
2cceb 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42    /* One of TK_B
2ccec 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c  EFORE, TK_AFTER,
2cced 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20   TK_INSTEAD */. 
2ccee 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
2ccef 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2ccf0 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44  K_INSERT, TK_UPD
2ccf1 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a  ATE, TK_DELETE *
2ccf2 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
2ccf3 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d  umns,   /* colum
2ccf4 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73 20 69  n list if this i
2ccf5 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 74  s an UPDATE OF t
2ccf6 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c  rigger */.  SrcL
2ccf7 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ist *pTableName,
2ccf8 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
2ccf9 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74 68  he table/view th
2ccfa 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65  e trigger applie
2ccfb 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a  s to */.  Expr *
2ccfc 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a  pWhen,        /*
2ccfd 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a   WHEN clause */.
2ccfe 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20    int isTemp,   
2ccff 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2cd00 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20 6b   the TEMPORARY k
2cd01 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
2cd02 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72  t */.  int noErr
2cd03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
2cd04 70 70 72 65 73 73 20 65 72 72 6f 72 73 20 69 66  ppress errors if
2cd05 20 74 68 65 20 74 72 69 67 67 65 72 20 61 6c 72   the trigger alr
2cd06 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29  eady exists */.)
2cd07 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
2cd08 69 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a 20 54  igger = 0;  /* T
2cd09 68 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 2a  he new trigger *
2cd0a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
2cd0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cd0c 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 74 72  able that the tr
2cd0d 69 67 67 65 72 20 66 69 72 65 73 20 6f 66 66 20  igger fires off 
2cd0e 6f 66 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  of */.  char *zN
2cd0f 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ame = 0;        
2cd10 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
2cd11 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69  rigger */.  sqli
2cd12 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2cd13 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61  ->db;  /* The da
2cd14 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2cd15 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
2cd16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cd17 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74  * The database t
2cd18 6f 20 73 74 6f 72 65 20 74 68 65 20 74 72 69 67  o store the trig
2cd19 67 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ger in */.  Toke
2cd1a 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20  n *pName;       
2cd1b 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61      /* The unqua
2cd1c 6c 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a  lified db name *
2cd1d 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  /.  DbFixer sFix
2cd1e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
2cd1f 74 61 74 65 20 76 65 63 74 6f 72 20 66 6f 72 20  tate vector for 
2cd20 74 68 65 20 44 42 20 66 69 78 65 72 20 2a 2f 0a  the DB fixer */.
2cd21 20 20 69 6e 74 20 69 54 61 62 44 62 3b 20 20 20    int iTabDb;   
2cd22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2cd23 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2cd24 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20  se holding pTab 
2cd25 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e  */..  assert( pN
2cd26 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20  ame1!=0 );   /* 
2cd27 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20  pName1->z might 
2cd28 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74  be NULL, but not
2cd29 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66 20 2a   pName1 itself *
2cd2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  /.  assert( pNam
2cd2b 65 32 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  e2!=0 );.  asser
2cd2c 74 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54  t( op==TK_INSERT
2cd2d 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54   || op==TK_UPDAT
2cd2e 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45  E || op==TK_DELE
2cd2f 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TE );.  assert( 
2cd30 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20  op>0 && op<0xff 
2cd31 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20  );.  if( isTemp 
2cd32 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45 4d  ){.    /* If TEM
2cd33 50 20 77 61 73 20 73 70 65 63 69 66 69 65 64 2c  P was specified,
2cd34 20 74 68 65 6e 20 74 68 65 20 74 72 69 67 67 65   then the trigge
2cd35 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62  r name may not b
2cd36 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a  e qualified. */.
2cd37 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e      if( pName2->
2cd38 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  n>0 ){.      sql
2cd39 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2cd3a 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
2cd3b 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74 20  trigger may not 
2cd3c 68 61 76 65 20 71 75 61 6c 69 66 69 65 64 20 6e  have qualified n
2cd3d 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74  ame");.      got
2cd3e 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2cd3f 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44 62  p;.    }.    iDb
2cd40 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65 20   = 1;.    pName 
2cd41 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c 73  = pName1;.  }els
2cd42 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65  e{.    /* Figure
2cd43 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61 74   out the db that
2cd44 20 74 68 65 20 74 68 65 20 74 72 69 67 67 65 72   the the trigger
2cd45 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64   will be created
2cd46 20 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d   in */.    iDb =
2cd47 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
2cd48 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
2cd49 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61  e1, pName2, &pNa
2cd4a 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  me);.    if( iDb
2cd4b 3c 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  <0 ){.      goto
2cd4c 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2cd4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2cd4e 2a 20 49 66 20 74 68 65 20 74 72 69 67 67 65 72  * If the trigger
2cd4f 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c   name was unqual
2cd50 69 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20 74  ified, and the t
2cd51 61 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20 74  able is a temp t
2cd52 61 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  able,.  ** then 
2cd53 73 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f 20  set iDb to 1 to 
2cd54 63 72 65 61 74 65 20 74 68 65 20 74 72 69 67 67  create the trigg
2cd55 65 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72  er in the tempor
2cd56 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
2cd57 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 53 72 63  ** If sqlite3Src
2cd58 4c 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72 65 74  ListLookup() ret
2cd59 75 72 6e 73 20 30 2c 20 69 6e 64 69 63 61 74 69  urns 0, indicati
2cd5a 6e 67 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65  ng the table doe
2cd5b 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69 73 74  s not.  ** exist
2cd5c 2c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 63  , the error is c
2cd5d 61 75 67 68 74 20 62 79 20 74 68 65 20 62 6c 6f  aught by the blo
2cd5e 63 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20  ck below..  */. 
2cd5f 20 69 66 28 20 21 70 54 61 62 6c 65 4e 61 6d 65   if( !pTableName
2cd60 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2cd61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2cd62 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2cd63 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73  ;.  }.  pTab = s
2cd64 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
2cd65 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
2cd66 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  leName);.  if( p
2cd67 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70  Name2->n==0 && p
2cd68 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 63  Tab && pTab->pSc
2cd69 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d  hema==db->aDb[1]
2cd6a 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  .pSchema ){.    
2cd6b 69 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  iDb = 1;.  }..  
2cd6c 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74 61  /* Ensure the ta
2cd6d 62 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 73  ble name matches
2cd6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61   database name a
2cd6f 6e 64 20 74 68 61 74 20 74 68 65 20 74 61 62 6c  nd that the tabl
2cd70 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 66  e exists */.  if
2cd71 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2cd72 65 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65  ed ) goto trigge
2cd73 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 73 73  r_cleanup;.  ass
2cd74 65 72 74 28 20 70 54 61 62 6c 65 4e 61 6d 65 2d  ert( pTableName-
2cd75 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66  >nSrc==1 );.  if
2cd76 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  ( sqlite3FixInit
2cd77 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20  (&sFix, pParse, 
2cd78 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20  iDb, "trigger", 
2cd79 70 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20  pName) && .     
2cd7a 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69   sqlite3FixSrcLi
2cd7b 73 74 28 26 73 46 69 78 2c 20 70 54 61 62 6c 65  st(&sFix, pTable
2cd7c 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
2cd7d 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2cd7e 70 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  p;.  }.  pTab = 
2cd7f 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
2cd80 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61  okup(pParse, pTa
2cd81 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  bleName);.  if( 
2cd82 21 70 54 61 62 20 29 7b 0a 20 20 20 20 2f 2a 20  !pTab ){.    /* 
2cd83 54 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e  The table does n
2cd84 6f 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20  ot exist. */.   
2cd85 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44   if( db->init.iD
2cd86 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a  b==1 ){.      /*
2cd87 20 54 69 63 6b 65 74 20 23 33 38 31 30 2e 0a 20   Ticket #3810.. 
2cd88 20 20 20 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79       ** Normally
2cd89 2c 20 77 68 65 6e 65 76 65 72 20 61 20 74 61 62  , whenever a tab
2cd8a 6c 65 20 69 73 20 64 72 6f 70 70 65 64 2c 20 61  le is dropped, a
2cd8b 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 74 72  ll associated tr
2cd8c 69 67 67 65 72 73 20 61 72 65 0a 20 20 20 20 20  iggers are.     
2cd8d 20 2a 2a 20 64 72 6f 70 70 65 64 20 74 6f 6f 2e   ** dropped too.
2cd8e 20 20 42 75 74 20 69 66 20 61 20 54 45 4d 50 20    But if a TEMP 
2cd8f 74 72 69 67 67 65 72 20 69 73 20 63 72 65 61 74  trigger is creat
2cd90 65 64 20 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d 50  ed on a non-TEMP
2cd91 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20   table.      ** 
2cd92 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 73  and the table is
2cd93 20 64 72 6f 70 70 65 64 20 62 79 20 61 20 64 69   dropped by a di
2cd94 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65  fferent database
2cd95 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65   connection, the
2cd96 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65  .      ** trigge
2cd97 72 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65  r is not visible
2cd98 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
2cd99 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
2cd9a 20 64 6f 65 73 20 74 68 65 0a 20 20 20 20 20 20   does the.      
2cd9b 2a 2a 20 64 72 6f 70 20 73 6f 20 74 68 65 20 74  ** drop so the t
2cd9c 72 69 67 67 65 72 20 63 61 6e 6e 6f 74 20 62 65  rigger cannot be
2cd9d 20 64 72 6f 70 70 65 64 2e 20 20 54 68 69 73 20   dropped.  This 
2cd9e 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20  results in an.  
2cd9f 20 20 20 20 2a 2a 20 22 6f 72 70 68 61 6e 65 64      ** "orphaned
2cda0 20 74 72 69 67 67 65 72 22 20 2d 20 61 20 74 72   trigger" - a tr
2cda1 69 67 67 65 72 20 77 68 6f 73 65 20 61 73 73 6f  igger whose asso
2cda2 63 69 61 74 65 64 20 74 61 62 6c 65 20 69 73 20  ciated table is 
2cda3 6d 69 73 73 69 6e 67 2e 0a 20 20 20 20 20 20 2a  missing..      *
2cda4 2f 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74  /.      db->init
2cda5 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 3d  .orphanTrigger =
2cda6 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f   1;.    }.    go
2cda7 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2cda8 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73  up;.  }.  if( Is
2cda9 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
2cdaa 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2cdab 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
2cdac 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67  nnot create trig
2cdad 67 65 72 73 20 6f 6e 20 76 69 72 74 75 61 6c 20  gers on virtual 
2cdae 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f  tables");.    go
2cdaf 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
2cdb0 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  up;.  }..  /* Ch
2cdb1 65 63 6b 20 74 68 61 74 20 74 68 65 20 74 72 69  eck that the tri
2cdb2 67 67 65 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74  gger name is not
2cdb3 20 72 65 73 65 72 76 65 64 20 61 6e 64 20 74 68   reserved and th
2cdb4 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 6f 66  at no trigger of
2cdb5 20 74 68 65 0a 20 20 2a 2a 20 73 70 65 63 69 66   the.  ** specif
2cdb6 69 65 64 20 6e 61 6d 65 20 65 78 69 73 74 73 20  ied name exists 
2cdb7 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  */.  zName = sql
2cdb8 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2cdb9 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
2cdba 69 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51  if( !zName || SQ
2cdbb 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2cdbc 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
2cdbd 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29  pParse, zName) )
2cdbe 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  {.    goto trigg
2cdbf 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
2cdc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 48 61 73    if( sqlite3Has
2cdc1 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b  hFind(&(db->aDb[
2cdc2 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
2cdc3 69 67 48 61 73 68 29 2c 0a 20 20 20 20 20 20 20  igHash),.       
2cdc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
2cdc5 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72  Name, sqlite3Str
2cdc6 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29 20 29 7b  len30(zName)) ){
2cdc7 0a 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20  .    if( !noErr 
2cdc8 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2cdc9 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2cdca 20 22 74 72 69 67 67 65 72 20 25 54 20 61 6c 72   "trigger %T alr
2cdcb 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e  eady exists", pN
2cdcc 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
2cdcd 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2cdce 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
2cdcf 44 6f 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20  Do not create a 
2cdd0 74 72 69 67 67 65 72 20 6f 6e 20 61 20 73 79 73  trigger on a sys
2cdd1 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  tem table */.  i
2cdd2 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
2cdd3 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  mp(pTab->zName, 
2cdd4 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30  "sqlite_", 7)==0
2cdd5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2cdd6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2cdd7 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 74  "cannot create t
2cdd8 72 69 67 67 65 72 20 6f 6e 20 73 79 73 74 65 6d  rigger on system
2cdd9 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 70 50   table");.    pP
2cdda 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
2cddb 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2cddc 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
2cddd 2a 20 49 4e 53 54 45 41 44 20 6f 66 20 74 72 69  * INSTEAD of tri
2cdde 67 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 66  ggers are only f
2cddf 6f 72 20 76 69 65 77 73 20 61 6e 64 20 76 69 65  or views and vie
2cde0 77 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20  ws only support 
2cde1 49 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20  INSTEAD.  ** of 
2cde2 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20  triggers..  */. 
2cde3 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
2cde4 63 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f  ct && tr_tm!=TK_
2cde5 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73  INSTEAD ){.    s
2cde6 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2cde7 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
2cde8 72 65 61 74 65 20 25 73 20 74 72 69 67 67 65 72  reate %s trigger
2cde9 20 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20 0a   on view: %S", .
2cdea 20 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20 3d          (tr_tm =
2cdeb 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45  = TK_BEFORE)?"BE
2cdec 46 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20 70  FORE":"AFTER", p
2cded 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20  TableName, 0);. 
2cdee 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2cdef 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2cdf0 66 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63  f( !pTab->pSelec
2cdf1 74 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49  t && tr_tm==TK_I
2cdf2 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71  NSTEAD ){.    sq
2cdf3 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2cdf4 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72  arse, "cannot cr
2cdf5 65 61 74 65 20 49 4e 53 54 45 41 44 20 4f 46 22  eate INSTEAD OF"
2cdf6 0a 20 20 20 20 20 20 20 20 22 20 74 72 69 67 67  .        " trigg
2cdf7 65 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22  er on table: %S"
2cdf8 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29  , pTableName, 0)
2cdf9 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  ;.    goto trigg
2cdfa 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
2cdfb 20 20 69 54 61 62 44 62 20 3d 20 73 71 6c 69 74    iTabDb = sqlit
2cdfc 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2cdfd 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2cdfe 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  a);..#ifndef SQL
2cdff 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2ce00 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
2ce01 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45  nt code = SQLITE
2ce02 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 3b  _CREATE_TRIGGER;
2ce03 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2ce04 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
2ce05 54 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  TabDb].zName;.  
2ce06 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2ce07 62 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20 3f  bTrig = isTemp ?
2ce08 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d   db->aDb[1].zNam
2ce09 65 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69 66 28  e : zDb;.    if(
2ce0a 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73   iTabDb==1 || is
2ce0b 54 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53 51  Temp ) code = SQ
2ce0c 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
2ce0d 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66  _TRIGGER;.    if
2ce0e 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
2ce0f 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
2ce10 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e   zName, pTab->zN
2ce11 61 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29 7b  ame, zDbTrig) ){
2ce12 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67  .      goto trig
2ce13 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ger_cleanup;.   
2ce14 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
2ce15 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
2ce16 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
2ce17 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  T, SCHEMA_TABLE(
2ce18 69 54 61 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b  iTabDb),0,zDb)){
2ce19 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67  .      goto trig
2ce1a 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ger_cleanup;.   
2ce1b 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
2ce1c 20 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46 20 74   /* INSTEAD OF t
2ce1d 72 69 67 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79  riggers can only
2ce1e 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73   appear on views
2ce1f 20 61 6e 64 20 42 45 46 4f 52 45 20 74 72 69 67   and BEFORE trig
2ce20 67 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74  gers.  ** cannot
2ce21 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73   appear on views
2ce22 2e 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20 61  .  So we might a
2ce23 73 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65  s well translate
2ce24 20 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54   every.  ** INST
2ce25 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 20 69  EAD OF trigger i
2ce26 6e 74 6f 20 61 20 42 45 46 4f 52 45 20 74 72 69  nto a BEFORE tri
2ce27 67 67 65 72 2e 20 20 49 74 20 73 69 6d 70 6c 69  gger.  It simpli
2ce28 66 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65  fies code.  ** e
2ce29 6c 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  lsewhere..  */. 
2ce2a 20 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b   if (tr_tm == TK
2ce2b 5f 49 4e 53 54 45 41 44 29 7b 0a 20 20 20 20 74  _INSTEAD){.    t
2ce2c 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45  r_tm = TK_BEFORE
2ce2d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c  ;.  }..  /* Buil
2ce2e 64 20 74 68 65 20 54 72 69 67 67 65 72 20 6f 62  d the Trigger ob
2ce2f 6a 65 63 74 20 2a 2f 0a 20 20 70 54 72 69 67 67  ject */.  pTrigg
2ce30 65 72 20 3d 20 28 54 72 69 67 67 65 72 2a 29 73  er = (Trigger*)s
2ce31 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2ce32 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
2ce33 69 67 67 65 72 29 29 3b 0a 20 20 69 66 28 20 70  igger));.  if( p
2ce34 54 72 69 67 67 65 72 3d 3d 30 20 29 20 67 6f 74  Trigger==0 ) got
2ce35 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2ce36 70 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 7a  p;.  pTrigger->z
2ce37 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20  Name = zName;.  
2ce38 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 54 72  zName = 0;.  pTr
2ce39 69 67 67 65 72 2d 3e 74 61 62 6c 65 20 3d 20 73  igger->table = s
2ce3a 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2ce3b 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61  b, pTableName->a
2ce3c 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54  [0].zName);.  pT
2ce3d 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20  rigger->pSchema 
2ce3e 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
2ce3f 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67  Schema;.  pTrigg
2ce40 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d  er->pTabSchema =
2ce41 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
2ce42 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d    pTrigger->op =
2ce43 20 28 75 38 29 6f 70 3b 0a 20 20 70 54 72 69 67   (u8)op;.  pTrig
2ce44 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f  ger->tr_tm = tr_
2ce45 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20  tm==TK_BEFORE ? 
2ce46 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3a  TRIGGER_BEFORE :
2ce47 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 3b 0a   TRIGGER_AFTER;.
2ce48 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65    pTrigger->pWhe
2ce49 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  n = sqlite3ExprD
2ce4a 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45 58  up(db, pWhen, EX
2ce4b 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
2ce4c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75   pTrigger->pColu
2ce4d 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  mns = sqlite3IdL
2ce4e 69 73 74 44 75 70 28 64 62 2c 20 70 43 6f 6c 75  istDup(db, pColu
2ce4f 6d 6e 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mns);.  assert( 
2ce50 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2ce51 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72  ger==0 );.  pPar
2ce52 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
2ce53 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a 74 72 69  = pTrigger;..tri
2ce54 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  gger_cleanup:.  
2ce55 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ce56 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  , zName);.  sqli
2ce57 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2ce58 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29  (db, pTableName)
2ce59 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73  ;.  sqlite3IdLis
2ce5a 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  tDelete(db, pCol
2ce5b 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  umns);.  sqlite3
2ce5c 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2ce5d 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21 70 50  When);.  if( !pP
2ce5e 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2ce5f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
2ce60 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62  DeleteTrigger(db
2ce61 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 7d  , pTrigger);.  }
2ce62 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2ce63 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  ( pParse->pNewTr
2ce64 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65 72 20  igger==pTrigger 
2ce65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2ce66 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2ce67 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c 6c  called after all
2ce68 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
2ce69 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65  actions have bee
2ce6a 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e 20 6f  n parsed.** in o
2ce6b 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65  rder to complete
2ce6c 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20   the process of 
2ce6d 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72 69  building the tri
2ce6e 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gger..*/.SQLITE_
2ce6f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2ce70 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65  ite3FinishTrigge
2ce71 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2ce72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2ce73 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
2ce74 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  /.  TriggerStep 
2ce75 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a 20 54  *pStepList, /* T
2ce76 68 65 20 74 72 69 67 67 65 72 65 64 20 70 72 6f  he triggered pro
2ce77 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  gram */.  Token 
2ce78 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20 20 20  *pAll           
2ce79 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61 74 20    /* Token that 
2ce7a 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63 6f  describes the co
2ce7b 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20 54 52  mplete CREATE TR
2ce7c 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20 54 72  IGGER */.){.  Tr
2ce7d 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20 70  igger *pTrig = p
2ce7e 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2ce7f 65 72 3b 20 20 20 20 2f 2a 20 54 72 69 67 67 65  er;    /* Trigge
2ce80 72 20 62 65 69 6e 67 20 66 69 6e 69 73 68 65 64  r being finished
2ce81 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
2ce82 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2ce83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ce84 2a 20 4e 61 6d 65 20 6f 66 20 74 72 69 67 67 65  * Name of trigge
2ce85 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  r */.  sqlite3 *
2ce86 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2ce87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ce88 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2ce89 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  */.  DbFixer sFi
2ce8a 78 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  x;.  int iDb;   
2ce8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ce8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ce8d 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   Database contai
2ce8e 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ning the trigger
2ce8f 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65   */.  Token name
2ce90 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
2ce91 20 2f 2a 20 54 72 69 67 67 65 72 20 6e 61 6d 65   /* Trigger name
2ce92 20 66 6f 72 20 65 72 72 6f 72 20 72 65 70 6f 72   for error repor
2ce93 74 69 6e 67 20 2a 2f 0a 0a 20 20 70 54 72 69 67  ting */..  pTrig
2ce94 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2ce95 72 69 67 67 65 72 3b 0a 20 20 70 50 61 72 73 65  rigger;.  pParse
2ce96 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20  ->pNewTrigger = 
2ce97 30 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  0;.  if( NEVER(p
2ce98 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20  Parse->nErr) || 
2ce99 21 70 54 72 69 67 20 29 20 67 6f 74 6f 20 74 72  !pTrig ) goto tr
2ce9a 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
2ce9b 6e 75 70 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70  nup;.  zName = p
2ce9c 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69  Trig->zName;.  i
2ce9d 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2ce9e 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
2ce9f 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63  ->db, pTrig->pSc
2cea0 68 65 6d 61 29 3b 0a 20 20 70 54 72 69 67 2d 3e  hema);.  pTrig->
2cea1 73 74 65 70 5f 6c 69 73 74 20 3d 20 70 53 74 65  step_list = pSte
2cea2 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28 20  pList;.  while( 
2cea3 70 53 74 65 70 4c 69 73 74 20 29 7b 0a 20 20 20  pStepList ){.   
2cea4 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54 72 69   pStepList->pTri
2cea5 67 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 70  g = pTrig;.    p
2cea6 53 74 65 70 4c 69 73 74 20 3d 20 70 53 74 65 70  StepList = pStep
2cea7 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  List->pNext;.  }
2cea8 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20 3d  .  nameToken.z =
2cea9 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20   pTrig->zName;. 
2ceaa 20 6e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 73   nameToken.n = s
2ceab 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 6e  qlite3Strlen30(n
2ceac 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 69  ameToken.z);.  i
2cead 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  f( sqlite3FixIni
2ceae 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c  t(&sFix, pParse,
2ceaf 20 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c   iDb, "trigger",
2ceb0 20 26 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20   &nameToken) .  
2ceb1 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74          && sqlit
2ceb2 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70  e3FixTriggerStep
2ceb3 28 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e 73  (&sFix, pTrig->s
2ceb4 74 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20  tep_list) ){.   
2ceb5 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e   goto triggerfin
2ceb6 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ish_cleanup;.  }
2ceb7 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 72 65  ..  /* if we are
2ceb8 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e   not initializin
2ceb9 67 2c 20 61 6e 64 20 74 68 69 73 20 74 72 69 67  g, and this trig
2ceba 67 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20  ger is not on a 
2cebb 54 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a  TEMP table, .  *
2cebc 2a 20 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69  * build the sqli
2cebd 74 65 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a  te_master entry.
2cebe 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e    */.  if( !db->
2cebf 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
2cec0 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68   Vdbe *v;.    ch
2cec1 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d  ar *z;..    /* M
2cec2 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  ake an entry in 
2cec3 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
2cec4 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76  r table */.    v
2cec5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2cec6 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
2cec7 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74  f( v==0 ) goto t
2cec8 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65  riggerfinish_cle
2cec9 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69 74 65  anup;.    sqlite
2ceca 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2cecb 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
2cecc 69 44 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71  iDb);.    z = sq
2cecd 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
2cece 62 2c 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e  b, (char*)pAll->
2cecf 7a 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20  z, pAll->n);.   
2ced0 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2ced1 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
2ced2 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
2ced3 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 74 72  %Q.%s VALUES('tr
2ced4 69 67 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27  igger',%Q,%Q,0,'
2ced5 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 25  CREATE TRIGGER %
2ced6 71 27 29 22 2c 0a 20 20 20 20 20 20 20 64 62 2d  q')",.       db-
2ced7 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
2ced8 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
2ced9 62 29 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  b), zName,.     
2ceda 20 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20    pTrig->table, 
2cedb 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  z);.    sqlite3D
2cedc 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20  bFree(db, z);.  
2cedd 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
2cede 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
2cedf 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
2cee0 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2cee1 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
2cee2 2c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d  , 0, 0, sqlite3M
2cee3 50 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20  Printf(.        
2cee4 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67 67  db, "type='trigg
2cee5 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25 71  er' AND name='%q
2cee6 27 22 2c 20 7a 4e 61 6d 65 29 2c 20 50 34 5f 44  '", zName), P4_D
2cee7 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a 20 20  YNAMIC.    );.  
2cee8 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  }..  if( db->ini
2cee9 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54 72  t.busy ){.    Tr
2ceea 69 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d 20 70  igger *pLink = p
2ceeb 54 72 69 67 3b 0a 20 20 20 20 48 61 73 68 20 2a  Trig;.    Hash *
2ceec 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 62  pHash = &db->aDb
2ceed 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  [iDb].pSchema->t
2ceee 72 69 67 48 61 73 68 3b 0a 20 20 20 20 70 54 72  rigHash;.    pTr
2ceef 69 67 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ig = sqlite3Hash
2cef0 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e  Insert(pHash, zN
2cef1 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
2cef2 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70 54 72  en30(zName), pTr
2cef3 69 67 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72  ig);.    if( pTr
2cef4 69 67 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  ig ){.      db->
2cef5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2cef6 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2cef7 70 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d 3d  pLink->pSchema==
2cef8 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d  pLink->pTabSchem
2cef9 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65  a ){.      Table
2cefa 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69 6e   *pTab;.      in
2cefb 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
2cefc 6c 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74 61 62  len30(pLink->tab
2cefd 6c 65 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20  le);.      pTab 
2cefe 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
2ceff 64 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63  d(&pLink->pTabSc
2cf00 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70  hema->tblHash, p
2cf01 4c 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b  Link->table, n);
2cf02 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2cf03 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  Tab!=0 );.      
2cf04 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70  pLink->pNext = p
2cf05 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20  Tab->pTrigger;. 
2cf06 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72 69 67       pTab->pTrig
2cf07 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20 20  ger = pLink;.   
2cf08 20 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66   }.  }..triggerf
2cf09 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20  inish_cleanup:. 
2cf0a 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
2cf0b 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 29  igger(db, pTrig)
2cf0c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ;.  assert( !pPa
2cf0d 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
2cf0e 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c   );.  sqlite3Del
2cf0f 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28 64  eteTriggerStep(d
2cf10 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d  b, pStepList);.}
2cf11 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53  ../*.** Turn a S
2cf12 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2cf13 28 74 68 61 74 20 74 68 65 20 70 53 65 6c 65 63  (that the pSelec
2cf14 74 20 70 61 72 61 6d 65 74 65 72 20 70 6f 69 6e  t parameter poin
2cf15 74 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61  ts to) into.** a
2cf16 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20   trigger step.  
2cf17 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
2cf18 20 74 6f 20 61 20 54 72 69 67 67 65 72 53 74 65   to a TriggerSte
2cf19 70 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  p structure..**.
2cf1a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2cf1b 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2cf1c 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20 61   when it finds a
2cf1d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2cf1e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20  t in.** body of 
2cf1f 61 20 54 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a  a TRIGGER.  .*/.
2cf20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
2cf21 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
2cf22 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
2cf23 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62  Step(sqlite3 *db
2cf24 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63  , Select *pSelec
2cf25 74 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  t){.  TriggerSte
2cf26 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20  p *pTriggerStep 
2cf27 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2cf28 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2cf29 28 54 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a  (TriggerStep));.
2cf2a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74    if( pTriggerSt
2cf2b 65 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20 73 71  ep==0 ) {.    sq
2cf2c 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
2cf2d 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
2cf2e 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2cf2f 7d 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  }.  pTriggerStep
2cf30 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54  ->op = TK_SELECT
2cf31 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
2cf32 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c  ->pSelect = pSel
2cf33 65 63 74 3b 0a 20 20 70 54 72 69 67 67 65 72 53  ect;.  pTriggerS
2cf34 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45  tep->orconf = OE
2cf35 5f 44 65 66 61 75 6c 74 3b 0a 20 20 72 65 74 75  _Default;.  retu
2cf36 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b  rn pTriggerStep;
2cf37 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
2cf38 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  te space to hold
2cf39 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 73   a new trigger s
2cf3a 74 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61  tep.  The alloca
2cf3b 74 65 64 20 73 70 61 63 65 0a 2a 2a 20 68 6f 6c  ted space.** hol
2cf3c 64 73 20 62 6f 74 68 20 74 68 65 20 54 72 69 67  ds both the Trig
2cf3d 67 65 72 53 74 65 70 20 6f 62 6a 65 63 74 20 61  gerStep object a
2cf3e 6e 64 20 74 68 65 20 54 72 69 67 67 65 72 53 74  nd the TriggerSt
2cf3f 65 70 2e 74 61 72 67 65 74 2e 7a 20 73 74 72 69  ep.target.z stri
2cf40 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
2cf41 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73  OOM error occurs
2cf42 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  , NULL is return
2cf43 65 64 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f  ed and db->mallo
2cf44 63 46 61 69 6c 65 64 20 69 73 20 73 65 74 2e 0a  cFailed is set..
2cf45 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67 65  */.static Trigge
2cf46 72 53 74 65 70 20 2a 74 72 69 67 67 65 72 53 74  rStep *triggerSt
2cf47 65 70 41 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71  epAllocate(.  sq
2cf48 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2cf49 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2cf4a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2cf4b 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 20 20 20   */.  u8 op,    
2cf4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cf4d 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6f 70 63    /* Trigger opc
2cf4e 6f 64 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ode */.  Token *
2cf4f 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  pName           
2cf50 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 72 67       /* The targ
2cf51 65 74 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20  et name */.){.  
2cf52 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
2cf53 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 70 54  iggerStep;..  pT
2cf54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
2cf55 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2cf56 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
2cf57 67 65 72 53 74 65 70 29 20 2b 20 70 4e 61 6d 65  gerStep) + pName
2cf58 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 70 54 72 69  ->n);.  if( pTri
2cf59 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20  ggerStep ){.    
2cf5a 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a  char *z = (char*
2cf5b 29 26 70 54 72 69 67 67 65 72 53 74 65 70 5b 31  )&pTriggerStep[1
2cf5c 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c  ];.    memcpy(z,
2cf5d 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65   pName->z, pName
2cf5e 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 72 69 67 67  ->n);.    pTrigg
2cf5f 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a  erStep->target.z
2cf60 20 3d 20 7a 3b 0a 20 20 20 20 70 54 72 69 67 67   = z;.    pTrigg
2cf61 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e  erStep->target.n
2cf62 20 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0a 20 20 20   = pName->n;.   
2cf63 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2cf64 70 20 3d 20 6f 70 3b 0a 20 20 7d 0a 20 20 72 65  p = op;.  }.  re
2cf65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2cf66 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c  p;.}../*.** Buil
2cf67 64 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70  d a trigger step
2cf68 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53 45 52   out of an INSER
2cf69 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65  T statement.  Re
2cf6a 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a  turn a pointer.*
2cf6b 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74 72 69  * to the new tri
2cf6c 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a  gger step..**.**
2cf6d 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
2cf6e 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
2cf6f 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e 20 49  hen it sees an I
2cf70 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74 68 65  NSERT inside the
2cf71 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 74 72  .** body of a tr
2cf72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  igger..*/.SQLITE
2cf73 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72  _PRIVATE Trigger
2cf74 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
2cf75 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 0a  ggerInsertStep(.
2cf76 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2cf77 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2cf78 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2cf79 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61   */.  Token *pTa
2cf7a 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d  bleName,  /* Nam
2cf7b 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
2cf7c 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69 6e 73  nto which we ins
2cf7d 65 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  ert */.  IdList 
2cf7e 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20  *pColumn,    /* 
2cf7f 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  List of columns 
2cf80 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f  in pTableName to
2cf81 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a   insert into */.
2cf82 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2cf83 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56 41 4c  st,   /* The VAL
2cf84 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c 69 73  UE clause: a lis
2cf85 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62  t of values to b
2cf86 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
2cf87 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
2cf88 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20      /* A SELECT 
2cf89 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 73  statement that s
2cf8a 75 70 70 6c 69 65 73 20 76 61 6c 75 65 73 20 2a  upplies values *
2cf8b 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20 20  /.  u8 orconf   
2cf8c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
2cf8d 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68  onflict algorith
2cf8e 6d 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  m (OE_Abort, OE_
2cf8f 52 65 70 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a  Replace, etc.) *
2cf90 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74  /.){.  TriggerSt
2cf91 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70  ep *pTriggerStep
2cf92 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69  ;..  assert(pELi
2cf93 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65 6c 65  st == 0 || pSele
2cf94 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73 73 65  ct == 0);.  asse
2cf95 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30 20 7c  rt(pEList != 0 |
2cf96 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30 20 7c  | pSelect != 0 |
2cf97 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2cf98 65 64 29 3b 0a 0a 20 20 70 54 72 69 67 67 65 72  ed);..  pTrigger
2cf99 53 74 65 70 20 3d 20 74 72 69 67 67 65 72 53 74  Step = triggerSt
2cf9a 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54  epAllocate(db, T
2cf9b 4b 5f 49 4e 53 45 52 54 2c 20 70 54 61 62 6c 65  K_INSERT, pTable
2cf9c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72  Name);.  if( pTr
2cf9d 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
2cf9e 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2cf9f 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
2cfa0 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53  SelectDup(db, pS
2cfa1 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52  elect, EXPRDUP_R
2cfa2 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69  EDUCE);.    pTri
2cfa3 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c 69 73  ggerStep->pIdLis
2cfa4 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20  t = pColumn;.   
2cfa5 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2cfa6 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74  ExprList = sqlit
2cfa7 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2cfa8 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55  , pEList, EXPRDU
2cfa9 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70  P_REDUCE);.    p
2cfaa 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63  TriggerStep->orc
2cfab 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20  onf = orconf;.  
2cfac 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
2cfad 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
2cfae 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 7d  b, pColumn);.  }
2cfaf 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
2cfb0 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c  stDelete(db, pEL
2cfb1 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
2cfb2 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2cfb3 70 53 65 6c 65 63 74 29 3b 0a 0a 20 20 72 65 74  pSelect);..  ret
2cfb4 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
2cfb5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  ;.}../*.** Const
2cfb6 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20 73  ruct a trigger s
2cfb7 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  tep that impleme
2cfb8 6e 74 73 20 61 6e 20 55 50 44 41 54 45 20 73 74  nts an UPDATE st
2cfb9 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75  atement and retu
2cfba 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
2cfbb 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 20  to that trigger 
2cfbc 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73 65  step.  The parse
2cfbd 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
2cfbe 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20  tine when it.** 
2cfbf 73 65 65 73 20 61 6e 20 55 50 44 41 54 45 20 73  sees an UPDATE s
2cfc0 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20  tatement inside 
2cfc1 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52  the body of a CR
2cfc2 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f  EATE TRIGGER..*/
2cfc3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2cfc4 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
2cfc5 69 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74  ite3TriggerUpdat
2cfc6 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  eStep(.  sqlite3
2cfc7 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
2cfc8 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
2cfc9 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f  nnection */.  To
2cfca 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
2cfcb 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
2cfcc 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70  e table to be up
2cfcd 64 61 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  dated */.  ExprL
2cfce 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20  ist *pEList,    
2cfcf 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61 75 73  /* The SET claus
2cfd0 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d  e: list of colum
2cfd1 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75 65 73  n and new values
2cfd2 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2cfd3 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  re,        /* Th
2cfd4 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2cfd5 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20 20  /.  u8 orconf   
2cfd6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2cfd7 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  conflict algorit
2cfd8 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f  hm. (OE_Abort, O
2cfd9 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 20 2a  E_Ignore, etc) *
2cfda 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74  /.){.  TriggerSt
2cfdb 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70  ep *pTriggerStep
2cfdc 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  ;..  pTriggerSte
2cfdd 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41  p = triggerStepA
2cfde 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 55  llocate(db, TK_U
2cfdf 50 44 41 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d  PDATE, pTableNam
2cfe0 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  e);.  if( pTrigg
2cfe1 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54  erStep ){.    pT
2cfe2 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70  riggerStep->pExp
2cfe3 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  rList = sqlite3E
2cfe4 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
2cfe5 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52  EList, EXPRDUP_R
2cfe6 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69  EDUCE);.    pTri
2cfe7 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65  ggerStep->pWhere
2cfe8 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2cfe9 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58  p(db, pWhere, EX
2cfea 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
2cfeb 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2cfec 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66  >orconf = orconf
2cfed 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2cfee 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2cfef 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  , pEList);.  sql
2cff0 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2cff1 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65  b, pWhere);.  re
2cff2 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2cff3 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  p;.}../*.** Cons
2cff4 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20  truct a trigger 
2cff5 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d  step that implem
2cff6 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20 73 74  ents a DELETE st
2cff7 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75  atement and retu
2cff8 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
2cff9 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 20  to that trigger 
2cffa 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73 65  step.  The parse
2cffb 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
2cffc 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20  tine when it.** 
2cffd 73 65 65 73 20 61 20 44 45 4c 45 54 45 20 73 74  sees a DELETE st
2cffe 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74  atement inside t
2cfff 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45  he body of a CRE
2d000 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a  ATE TRIGGER..*/.
2d001 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
2d002 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
2d003 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65  te3TriggerDelete
2d004 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20  Step(.  sqlite3 
2d005 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2d006 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2d007 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
2d008 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20  n *pTableName,  
2d009 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2d00a 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f 77 73   from which rows
2d00b 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a   are deleted */.
2d00c 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20    Expr *pWhere  
2d00d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2d00e 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2d00f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2d010 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b  p *pTriggerStep;
2d011 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ..  pTriggerStep
2d012 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41 6c   = triggerStepAl
2d013 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 44 45  locate(db, TK_DE
2d014 4c 45 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65  LETE, pTableName
2d015 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
2d016 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72  rStep ){.    pTr
2d017 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72  iggerStep->pWher
2d018 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  e = sqlite3ExprD
2d019 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45  up(db, pWhere, E
2d01a 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2d01b 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
2d01c 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65  ->orconf = OE_De
2d01d 66 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 73 71 6c  fault;.  }.  sql
2d01e 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2d01f 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65  b, pWhere);.  re
2d020 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
2d021 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63  p;.}../* .** Rec
2d022 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20  ursively delete 
2d023 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74  a Trigger struct
2d024 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ure.*/.SQLITE_PR
2d025 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2d026 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  e3DeleteTrigger(
2d027 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69  sqlite3 *db, Tri
2d028 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b  gger *pTrigger){
2d029 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 3d  .  if( pTrigger=
2d02a 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  =0 ) return;.  s
2d02b 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
2d02c 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69  gerStep(db, pTri
2d02d 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29  gger->step_list)
2d02e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2d02f 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  e(db, pTrigger->
2d030 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  zName);.  sqlite
2d031 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
2d032 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20  gger->table);.  
2d033 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2d034 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  e(db, pTrigger->
2d035 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65  pWhen);.  sqlite
2d036 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
2d037 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c  , pTrigger->pCol
2d038 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  umns);.  sqlite3
2d039 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67  DbFree(db, pTrig
2d03a 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ger);.}../*.** T
2d03b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2d03c 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20 61  called to drop a
2d03d 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74 68   trigger from th
2d03e 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
2d03f 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  a. .**.** This m
2d040 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69 72  ay be called dir
2d041 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 70  ectly from the p
2d042 61 72 73 65 72 20 61 6e 64 20 74 68 65 72 65 66  arser and theref
2d043 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a 2a  ore identifies.*
2d044 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 62 79  * the trigger by
2d045 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c 69   name.  The sqli
2d046 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74  te3DropTriggerPt
2d047 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  r() routine does
2d048 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62   the.** same job
2d049 20 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65   as this routine
2d04a 20 65 78 63 65 70 74 20 69 74 20 74 61 6b 65 73   except it takes
2d04b 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2d04c 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e 73  e trigger.** ins
2d04d 74 65 61 64 20 6f 66 20 74 68 65 20 74 72 69 67  tead of the trig
2d04e 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51  ger name..**/.SQ
2d04f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2d050 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69  d sqlite3DropTri
2d051 67 67 65 72 28 50 61 72 73 65 20 2a 70 50 61 72  gger(Parse *pPar
2d052 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61  se, SrcList *pNa
2d053 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a  me, int noErr){.
2d054 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2d055 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  ger = 0;.  int i
2d056 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2d057 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  zDb;.  const cha
2d058 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  r *zName;.  int 
2d059 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33  nName;.  sqlite3
2d05a 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2d05b 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  b;..  if( db->ma
2d05c 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
2d05d 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63  o drop_trigger_c
2d05e 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53 51  leanup;.  if( SQ
2d05f 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2d060 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
2d061 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  e) ){.    goto d
2d062 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
2d063 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  nup;.  }..  asse
2d064 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d  rt( pName->nSrc=
2d065 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70 4e  =1 );.  zDb = pN
2d066 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  ame->a[0].zDatab
2d067 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70  ase;.  zName = p
2d068 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  Name->a[0].zName
2d069 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  nName = sqli
2d06a 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2d06b 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54  e);.  for(i=OMIT
2d06c 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e  _TEMPDB; i<db->n
2d06d 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  Db; i++){.    in
2d06e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e  t j = (i<2) ? i^
2d06f 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63  1 : i;  /* Searc
2d070 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41  h TEMP before MA
2d071 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44  IN */.    if( zD
2d072 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  b && sqlite3StrI
2d073 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a  Cmp(db->aDb[j].z
2d074 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e  Name, zDb) ) con
2d075 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69 67  tinue;.    pTrig
2d076 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73  ger = sqlite3Has
2d077 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b  hFind(&(db->aDb[
2d078 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  j].pSchema->trig
2d079 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  Hash), zName, nN
2d07a 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54  ame);.    if( pT
2d07b 72 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b 0a  rigger ) break;.
2d07c 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69 67    }.  if( !pTrig
2d07d 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ger ){.    if( !
2d07e 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73  noErr ){.      s
2d07f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2d080 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2d081 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e  trigger: %S", pN
2d082 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ame, 0);.    }. 
2d083 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69     goto drop_tri
2d084 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2d085 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54  }.  sqlite3DropT
2d086 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65  riggerPtr(pParse
2d087 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64 72  , pTrigger);..dr
2d088 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
2d089 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
2d08a 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2d08b 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Name);.}../*.** 
2d08c 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
2d08d 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74   to the Table st
2d08e 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20  ructure for the 
2d08f 74 61 62 6c 65 20 74 68 61 74 20 61 20 74 72 69  table that a tri
2d090 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f  gger.** is set o
2d091 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61 62  n..*/.static Tab
2d092 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67 67  le *tableOfTrigg
2d093 65 72 28 54 72 69 67 67 65 72 20 2a 70 54 72 69  er(Trigger *pTri
2d094 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  gger){.  int n =
2d095 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2d096 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65  (pTrigger->table
2d097 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
2d098 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54 72  te3HashFind(&pTr
2d099 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d  igger->pTabSchem
2d09a 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69  a->tblHash, pTri
2d09b 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b  gger->table, n);
2d09c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20  .}.../*.** Drop 
2d09d 61 20 74 72 69 67 67 65 72 20 67 69 76 65 6e 20  a trigger given 
2d09e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
2d09f 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f 0a 53  t trigger. .*/.S
2d0a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d0a1 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
2d0a2 69 67 67 65 72 50 74 72 28 50 61 72 73 65 20 2a  iggerPtr(Parse *
2d0a3 70 50 61 72 73 65 2c 20 54 72 69 67 67 65 72 20  pParse, Trigger 
2d0a4 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 54 61  *pTrigger){.  Ta
2d0a5 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b 0a 20  ble   *pTable;. 
2d0a6 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69   Vdbe *v;.  sqli
2d0a7 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2d0a8 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
2d0a9 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
2d0aa 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2d0ab 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67  Parse->db, pTrig
2d0ac 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  ger->pSchema);. 
2d0ad 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
2d0ae 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
2d0af 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74 61 62  ;.  pTable = tab
2d0b0 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69  leOfTrigger(pTri
2d0b1 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28  gger);.  assert(
2d0b2 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61 73 73   pTable );.  ass
2d0b3 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 63  ert( pTable->pSc
2d0b4 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e  hema==pTrigger->
2d0b5 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62 3d 3d  pSchema || iDb==
2d0b6 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  1 );.#ifndef SQL
2d0b7 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2d0b8 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
2d0b9 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45  nt code = SQLITE
2d0ba 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0a 20  _DROP_TRIGGER;. 
2d0bb 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2d0bc 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
2d0bd 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
2d0be 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
2d0bf 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2d0c0 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d  );.    if( iDb==
2d0c1 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  1 ) code = SQLIT
2d0c2 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47  E_DROP_TEMP_TRIG
2d0c3 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  GER;.    if( sql
2d0c4 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2d0c5 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 72 69  arse, code, pTri
2d0c6 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61  gger->zName, pTa
2d0c7 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29  ble->zName, zDb)
2d0c8 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ||.      sqlite
2d0c9 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2d0ca 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  e, SQLITE_DELETE
2d0cb 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20  , zTab, 0, zDb) 
2d0cc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ){.      return;
2d0cd 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2d0ce 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  f..  /* Generate
2d0cf 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72 6f 79   code to destroy
2d0d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
2d0d1 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72 69 67  cord of the trig
2d0d2 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ger..  */.  asse
2d0d3 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b  rt( pTable!=0 );
2d0d4 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71 6c 69  .  if( (v = sqli
2d0d5 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2d0d6 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  e))!=0 ){.    in
2d0d7 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74 61 74  t base;.    stat
2d0d8 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
2d0d9 69 73 74 20 64 72 6f 70 54 72 69 67 67 65 72 5b  ist dropTrigger[
2d0da 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50  ] = {.      { OP
2d0db 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c 20  _Rewind,     0, 
2d0dc 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20  ADDR(9),  0},.  
2d0dd 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
2d0de 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20  ,    0, 1,      
2d0df 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20    0}, /* 1 */.  
2d0e0 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
2d0e1 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20       0, 1,      
2d0e2 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    2},.      { OP
2d0e3 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20  _Ne,         2, 
2d0e4 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20  ADDR(8),  1},.  
2d0e5 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
2d0e6 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20  ,    0, 1,      
2d0e7 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69    0}, /* 4: "tri
2d0e8 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b  gger" */.      {
2d0e9 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20   OP_Column,     
2d0ea 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32 7d 2c  0, 0,        2},
2d0eb 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20  .      { OP_Ne, 
2d0ec 20 20 20 20 20 20 20 20 32 2c 20 41 44 44 52 28          2, ADDR(
2d0ed 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b  8),  1},.      {
2d0ee 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20 20 20   OP_Delete,     
2d0ef 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 0,        0},
2d0f0 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74  .      { OP_Next
2d0f1 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44 52 28  ,       0, ADDR(
2d0f2 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f  1),  0}, /* 8 */
2d0f3 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c  .    };..    sql
2d0f4 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
2d0f5 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
2d0f6 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  0, iDb);.    sql
2d0f7 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61  ite3OpenMasterTa
2d0f8 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  ble(pParse, iDb)
2d0f9 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c  ;.    base = sql
2d0fa 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
2d0fb 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a 65 28  t(v,  ArraySize(
2d0fc 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20 64 72  dropTrigger), dr
2d0fd 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  opTrigger);.    
2d0fe 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2d0ff 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c 20 70  eP4(v, base+1, p
2d100 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
2d101 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
2d102 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 62  dbeChangeP4(v, b
2d103 61 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72 22  ase+4, "trigger"
2d104 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
2d105 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
2d106 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
2d107 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
2d108 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d109 43 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20  Close, 0, 0);.  
2d10a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d10b 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72  Op4(v, OP_DropTr
2d10c 69 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20 30  igger, iDb, 0, 0
2d10d 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  , pTrigger->zNam
2d10e 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  e, 0);.    if( p
2d10f 50 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b  Parse->nMem<3 ){
2d110 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
2d111 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20  Mem = 3;.    }. 
2d112 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f   }.}../*.** Remo
2d113 76 65 20 61 20 74 72 69 67 67 65 72 20 66 72 6f  ve a trigger fro
2d114 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
2d115 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 2a  s of the sqlite*
2d116 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
2d117 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d118 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
2d119 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  dDeleteTrigger(s
2d11a 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2d11b 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
2d11c 2a 7a 4e 61 6d 65 29 7b 0a 20 20 48 61 73 68 20  *zName){.  Hash 
2d11d 2a 70 48 61 73 68 20 3d 20 26 28 64 62 2d 3e 61  *pHash = &(db->a
2d11e 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
2d11f 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 54 72  >trigHash);.  Tr
2d120 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
2d121 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71  .  pTrigger = sq
2d122 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2d123 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71  pHash, zName, sq
2d124 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2d125 61 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66 28 20  ame), 0);.  if( 
2d126 41 4c 57 41 59 53 28 70 54 72 69 67 67 65 72 29  ALWAYS(pTrigger)
2d127 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 72 69   ){.    if( pTri
2d128 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  gger->pSchema==p
2d129 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68  Trigger->pTabSch
2d12a 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62  ema ){.      Tab
2d12b 6c 65 20 2a 70 54 61 62 20 3d 20 74 61 62 6c 65  le *pTab = table
2d12c 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67  OfTrigger(pTrigg
2d12d 65 72 29 3b 0a 20 20 20 20 20 20 54 72 69 67 67  er);.      Trigg
2d12e 65 72 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66  er **pp;.      f
2d12f 6f 72 28 70 70 3d 26 70 54 61 62 2d 3e 70 54 72  or(pp=&pTab->pTr
2d130 69 67 67 65 72 3b 20 2a 70 70 21 3d 70 54 72 69  igger; *pp!=pTri
2d131 67 67 65 72 3b 20 70 70 3d 26 28 28 2a 70 70 29  gger; pp=&((*pp)
2d132 2d 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20  ->pNext));.     
2d133 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e   *pp = (*pp)->pN
2d134 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ext;.    }.    s
2d135 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
2d136 67 65 72 28 64 62 2c 20 70 54 72 69 67 67 65 72  ger(db, pTrigger
2d137 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  );.    db->flags
2d138 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
2d139 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d 0a  nChanges;.  }.}.
2d13a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73  ./*.** pEList is
2d13b 20 74 68 65 20 53 45 54 20 63 6c 61 75 73 65 20   the SET clause 
2d13c 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73 74 61  of an UPDATE sta
2d13d 74 65 6d 65 6e 74 2e 20 20 45 61 63 68 20 65 6e  tement.  Each en
2d13e 74 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74  try.** in pEList
2d13f 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61   is of the forma
2d140 74 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e 20 20  t <id>=<expr>.  
2d141 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65 6e  If any of the en
2d142 74 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45 4c 69  tries.** in pELi
2d143 73 74 20 68 61 76 65 20 61 6e 20 3c 69 64 3e 20  st have an <id> 
2d144 77 68 69 63 68 20 6d 61 74 63 68 65 73 20 61 6e  which matches an
2d145 20 69 64 65 6e 74 69 66 69 65 72 20 69 6e 20 70   identifier in p
2d146 49 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20  IdList,.** then 
2d147 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66  return TRUE.  If
2d148 20 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c 20   pIdList==NULL, 
2d149 74 68 65 6e 20 69 74 20 69 73 20 63 6f 6e 73 69  then it is consi
2d14a 64 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c 64 63  dered a.** wildc
2d14b 61 72 64 20 74 68 61 74 20 6d 61 74 63 68 65 73  ard that matches
2d14c 20 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69 6b 65   anything.  Like
2d14d 77 69 73 65 20 69 66 20 70 45 4c 69 73 74 3d 3d  wise if pEList==
2d14e 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69 74 20  NULL then.** it 
2d14f 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67  matches anything
2d150 20 73 6f 20 61 6c 77 61 79 73 20 72 65 74 75 72   so always retur
2d151 6e 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e 20  n true.  Return 
2d152 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66  false only.** if
2d153 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
2d154 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ch..*/.static in
2d155 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65  t checkColumnOve
2d156 72 6c 61 70 28 49 64 4c 69 73 74 20 2a 70 49 64  rlap(IdList *pId
2d157 4c 69 73 74 2c 20 45 78 70 72 4c 69 73 74 20 2a  List, ExprList *
2d158 70 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 65  pEList){.  int e
2d159 3b 0a 20 20 69 66 28 20 70 49 64 4c 69 73 74 3d  ;.  if( pIdList=
2d15a 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 45 4c 69  =0 || NEVER(pELi
2d15b 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20  st==0) ) return 
2d15c 31 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c  1;.  for(e=0; e<
2d15d 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65  pEList->nExpr; e
2d15e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
2d15f 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
2d160 70 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d  pIdList, pEList-
2d161 3e 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20  >a[e].zName)>=0 
2d162 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
2d163 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
2d164 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c  /*.** Return a l
2d165 69 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67  ist of all trigg
2d166 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61  ers on table pTa
2d167 62 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74  b if there exist
2d168 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e  s at least.** on
2d169 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 6d  e trigger that m
2d16a 75 73 74 20 62 65 20 66 69 72 65 64 20 77 68 65  ust be fired whe
2d16b 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f  n an operation o
2d16c 66 20 74 79 70 65 20 27 6f 70 27 20 69 73 20 0a  f type 'op' is .
2d16d 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20  ** performed on 
2d16e 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20  the table, and, 
2d16f 69 66 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f  if that operatio
2d170 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20  n is an UPDATE, 
2d171 69 66 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f  if at.** least o
2d172 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ne of the column
2d173 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 69 73  s in pChanges is
2d174 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
2d175 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d176 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69  TE Trigger *sqli
2d177 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
2d178 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2d179 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
2d17a 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
2d17b 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d17c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2d17d 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74 61   table the conta
2d17e 69 6e 73 20 74 68 65 20 74 72 69 67 67 65 72 73  ins the triggers
2d17f 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
2d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d181 20 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   one of TK_DELET
2d182 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b  E, TK_INSERT, TK
2d183 5f 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78 70  _UPDATE */.  Exp
2d184 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
2d185 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20       /* Columns 
2d186 74 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20 61  that change in a
2d187 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  n UPDATE stateme
2d188 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61  nt */.  int *pMa
2d189 73 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sk              
2d18a 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20  /* OUT: Mask of 
2d18b 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c 54  TRIGGER_BEFORE|T
2d18c 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a  RIGGER_AFTER */.
2d18d 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20  ){.  int mask = 
2d18e 30 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4c  0;.  Trigger *pL
2d18f 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ist = sqlite3Tri
2d190 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
2d191 20 70 54 61 62 29 3b 0a 20 20 54 72 69 67 67 65   pTab);.  Trigge
2d192 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  r *p;.  assert( 
2d193 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69  pList==0 || IsVi
2d194 72 74 75 61 6c 28 70 54 61 62 29 3d 3d 30 20 29  rtual(pTab)==0 )
2d195 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b  ;.  for(p=pList;
2d196 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
2d197 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  .    if( p->op==
2d198 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d  op && checkColum
2d199 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c  nOverlap(p->pCol
2d19a 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29 20  umns, pChanges) 
2d19b 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d  ){.      mask |=
2d19c 20 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d   p->tr_tm;.    }
2d19d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b  .  }.  if( pMask
2d19e 20 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d   ){.    *pMask =
2d19f 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74   mask;.  }.  ret
2d1a0 75 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73  urn (mask ? pLis
2d1a1 74 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t : 0);.}../*.**
2d1a2 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70 53 74   Convert the pSt
2d1a3 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e  ep->target token
2d1a4 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73 74 20   into a SrcList 
2d1a5 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
2d1a6 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20  nter.** to that 
2d1a7 53 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  SrcList..**.** T
2d1a8 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73  his routine adds
2d1a9 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 61   a specific data
2d1aa 62 61 73 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65  base name, if ne
2d1ab 65 64 65 64 2c 20 74 6f 20 74 68 65 20 74 61 72  eded, to the tar
2d1ac 67 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d  get when.** form
2d1ad 69 6e 67 20 74 68 65 20 53 72 63 4c 69 73 74 2e  ing the SrcList.
2d1ae 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20    This prevents 
2d1af 61 20 74 72 69 67 67 65 72 20 69 6e 20 6f 6e 65  a trigger in one
2d1b0 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a   database from.*
2d1b1 2a 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 61  * referring to a
2d1b2 20 74 61 72 67 65 74 20 69 6e 20 61 6e 6f 74 68   target in anoth
2d1b3 65 72 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e  er database.  An
2d1b4 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 77 68   exception is wh
2d1b5 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65  en the.** trigge
2d1b6 72 20 69 73 20 69 6e 20 54 45 4d 50 20 69 6e 20  r is in TEMP in 
2d1b7 77 68 69 63 68 20 63 61 73 65 20 69 74 20 63 61  which case it ca
2d1b8 6e 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6f  n refer to any o
2d1b9 74 68 65 72 20 64 61 74 61 62 61 73 65 20 69 74  ther database it
2d1ba 0a 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74  .** wants..*/.st
2d1bb 61 74 69 63 20 53 72 63 4c 69 73 74 20 2a 74 61  atic SrcList *ta
2d1bc 72 67 65 74 53 72 63 4c 69 73 74 28 0a 20 20 50  rgetSrcList(.  P
2d1bd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2d1be 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2d1bf 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2d1c0 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
2d1c1 65 70 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67  ep   /* The trig
2d1c2 67 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ger containing t
2d1c3 68 65 20 74 61 72 67 65 74 20 74 6f 6b 65 6e 20  he target token 
2d1c4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  */.){.  int iDb;
2d1c5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d1c6 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
2d1c7 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a  abase to use */.
2d1c8 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
2d1c9 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73         /* SrcLis
2d1ca 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  t to be returned
2d1cb 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 73 71   */..  pSrc = sq
2d1cc 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2d1cd 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30  nd(pParse->db, 0
2d1ce 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74  , &pStep->target
2d1cf 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53 72 63  , 0);.  if( pSrc
2d1d0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2d1d1 70 53 72 63 2d 3e 6e 53 72 63 3e 30 20 29 3b 0a  pSrc->nSrc>0 );.
2d1d2 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
2d1d3 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 20 20 69 44  ->a!=0 );.    iD
2d1d4 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2d1d5 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
2d1d6 3e 64 62 2c 20 70 53 74 65 70 2d 3e 70 54 72 69  >db, pStep->pTri
2d1d7 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  g->pSchema);.   
2d1d8 20 69 66 28 20 69 44 62 3d 3d 30 20 7c 7c 20 69   if( iDb==0 || i
2d1d9 44 62 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 73  Db>=2 ){.      s
2d1da 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2d1db 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61  rse->db;.      a
2d1dc 73 73 65 72 74 28 20 69 44 62 3c 70 50 61 72 73  ssert( iDb<pPars
2d1dd 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  e->db->nDb );.  
2d1de 20 20 20 20 70 53 72 63 2d 3e 61 5b 70 53 72 63      pSrc->a[pSrc
2d1df 2d 3e 6e 53 72 63 2d 31 5d 2e 7a 44 61 74 61 62  ->nSrc-1].zDatab
2d1e0 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ase = sqlite3DbS
2d1e1 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e 61 44  trDup(db, db->aD
2d1e2 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2d1e3 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2d1e4 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pSrc;.}../*.**
2d1e5 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
2d1e6 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 74  ode for the stat
2d1e7 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68  ements inside th
2d1e8 65 20 62 6f 64 79 20 6f 66 20 61 20 73 69 6e 67  e body of a sing
2d1e9 6c 65 20 0a 2a 2a 20 74 72 69 67 67 65 72 2e 0a  le .** trigger..
2d1ea 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2d1eb 64 65 54 72 69 67 67 65 72 50 72 6f 67 72 61 6d  deTriggerProgram
2d1ec 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2d1ed 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
2d1ee 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
2d1ef 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2d1f0 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c  Step *pStepList,
2d1f1 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74     /* List of st
2d1f2 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20  atements inside 
2d1f3 74 68 65 20 74 72 69 67 67 65 72 20 62 6f 64 79  the trigger body
2d1f4 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66   */.  int orconf
2d1f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1f6 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f  /* Conflict algo
2d1f7 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74  rithm. (OE_Abort
2d1f8 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20  , etc) */  .){. 
2d1f9 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
2d1fa 74 65 70 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  tep;.  Vdbe *v =
2d1fb 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2d1fc 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2d1fd 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61  pParse->db;..  a
2d1fe 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
2d1ff 54 72 69 67 67 65 72 54 61 62 20 26 26 20 70 50  TriggerTab && pP
2d200 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  arse->pToplevel 
2d201 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 74  );.  assert( pSt
2d202 65 70 4c 69 73 74 20 29 3b 0a 20 20 61 73 73 65  epList );.  asse
2d203 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 66 6f  rt( v!=0 );.  fo
2d204 72 28 70 53 74 65 70 3d 70 53 74 65 70 4c 69 73  r(pStep=pStepLis
2d205 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d  t; pStep; pStep=
2d206 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  pStep->pNext){. 
2d207 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
2d208 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
2d209 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 69 6c   policy that wil
2d20a 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  l be used for th
2d20b 69 73 20 73 74 65 70 0a 20 20 20 20 2a 2a 20 6f  is step.    ** o
2d20c 66 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  f the trigger pr
2d20d 6f 67 72 61 6d 2e 20 49 66 20 74 68 65 20 73 74  ogram. If the st
2d20e 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61 75  atement that cau
2d20f 73 65 64 20 74 68 69 73 20 74 72 69 67 67 65 72  sed this trigger
2d210 0a 20 20 20 20 2a 2a 20 74 6f 20 66 69 72 65 20  .    ** to fire 
2d211 68 61 64 20 61 6e 20 65 78 70 6c 69 63 69 74 20  had an explicit 
2d212 4f 4e 20 43 4f 4e 46 4c 49 43 54 2c 20 74 68 65  ON CONFLICT, the
2d213 6e 20 75 73 65 20 69 74 2e 20 4f 74 68 65 72 77  n use it. Otherw
2d214 69 73 65 2c 20 75 73 65 0a 20 20 20 20 2a 2a 20  ise, use.    ** 
2d215 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
2d216 70 6f 6c 69 63 79 20 74 68 61 74 20 77 61 73 20  policy that was 
2d217 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72  specified as par
2d218 74 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  t of the trigger
2d219 0a 20 20 20 20 2a 2a 20 73 74 65 70 20 73 74 61  .    ** step sta
2d21a 74 65 6d 65 6e 74 2e 20 45 78 61 6d 70 6c 65 3a  tement. Example:
2d21b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2d21c 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2d21d 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e 20  AFTER INSERT ON 
2d21e 74 31 20 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a  t1 BEGIN;.    **
2d21f 20 20 20 20 20 49 4e 53 45 52 54 20 4f 52 20 52       INSERT OR R
2d220 45 50 4c 41 43 45 20 49 4e 54 4f 20 74 32 20 56  EPLACE INTO t2 V
2d221 41 4c 55 45 53 28 6e 65 77 2e 61 2c 20 6e 65 77  ALUES(new.a, new
2d222 2e 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20 45 4e  .b);.    **   EN
2d223 44 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  D;.    **.    **
2d224 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
2d225 31 20 2e 2e 2e 20 3b 20 20 20 20 20 20 20 20 20  1 ... ;         
2d226 20 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74     -- insert int
2d227 6f 20 74 32 20 75 73 65 73 20 52 45 50 4c 41 43  o t2 uses REPLAC
2d228 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2a 20  E policy.    ** 
2d229 20 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f    INSERT OR IGNO
2d22a 52 45 20 49 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b  RE INTO t1 ... ;
2d22b 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f    -- insert into
2d22c 20 74 32 20 75 73 65 73 20 49 47 4e 4f 52 45 20   t2 uses IGNORE 
2d22d 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2f 0a 20 20  policy.    */.  
2d22e 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e    pParse->eOrcon
2d22f 66 20 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45 5f  f = (orconf==OE_
2d230 44 65 66 61 75 6c 74 29 3f 70 53 74 65 70 2d 3e  Default)?pStep->
2d231 6f 72 63 6f 6e 66 3a 28 75 38 29 6f 72 63 6f 6e  orconf:(u8)orcon
2d232 66 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20  f;..    switch( 
2d233 70 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  pStep->op ){.   
2d234 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41 54     case TK_UPDAT
2d235 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  E: {.        sql
2d236 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73  ite3Update(pPars
2d237 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74 61  e, .          ta
2d238 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72  rgetSrcList(pPar
2d239 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20 20  se, pStep),.    
2d23a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2d23b 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 74  rListDup(db, pSt
2d23c 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20 30  ep->pExprList, 0
2d23d 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71  ), .          sq
2d23e 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2d23f 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 2c 20   pStep->pWhere, 
2d240 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70  0), .          p
2d241 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20  Parse->eOrconf. 
2d242 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
2d243 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2d244 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 49  .      case TK_I
2d245 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20 20  NSERT: {.       
2d246 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70   sqlite3Insert(p
2d247 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
2d248 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28    targetSrcList(
2d249 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a  pParse, pStep),.
2d24a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2d24b 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
2d24c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73   pStep->pExprLis
2d24d 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20  t, 0), .        
2d24e 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2d24f 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 53  up(db, pStep->pS
2d250 65 6c 65 63 74 2c 20 30 29 2c 20 0a 20 20 20 20  elect, 0), .    
2d251 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c        sqlite3IdL
2d252 69 73 74 44 75 70 28 64 62 2c 20 70 53 74 65 70  istDup(db, pStep
2d253 2d 3e 70 49 64 4c 69 73 74 29 2c 20 0a 20 20 20  ->pIdList), .   
2d254 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 65         pParse->e
2d255 4f 72 63 6f 6e 66 0a 20 20 20 20 20 20 20 20 29  Orconf.        )
2d256 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2d257 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
2d258 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a 20 7b  ase TK_DELETE: {
2d259 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2d25a 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73  DeleteFrom(pPars
2d25b 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74 61  e, .          ta
2d25c 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72  rgetSrcList(pPar
2d25d 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20 20  se, pStep),.    
2d25e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2d25f 72 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e  rDup(db, pStep->
2d260 70 57 68 65 72 65 2c 20 30 29 0a 20 20 20 20 20  pWhere, 0).     
2d261 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72     );.        br
2d262 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2d263 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65     default: asse
2d264 72 74 28 20 70 53 74 65 70 2d 3e 6f 70 3d 3d 54  rt( pStep->op==T
2d265 4b 5f 53 45 4c 45 43 54 20 29 3b 20 7b 0a 20 20  K_SELECT ); {.  
2d266 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
2d267 20 73 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20   sDest;.        
2d268 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
2d269 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2d26a 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 53  up(db, pStep->pS
2d26b 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20  elect, 0);.     
2d26c 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2d26d 44 65 73 74 49 6e 69 74 28 26 73 44 65 73 74 2c  DestInit(&sDest,
2d26e 20 53 52 54 5f 44 69 73 63 61 72 64 2c 20 30 29   SRT_Discard, 0)
2d26f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2d270 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2d271 70 53 65 6c 65 63 74 2c 20 26 73 44 65 73 74 29  pSelect, &sDest)
2d272 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2d273 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2d274 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
2d275 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d276 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66   }.    } .    if
2d277 28 20 70 53 74 65 70 2d 3e 6f 70 21 3d 54 4b 5f  ( pStep->op!=TK_
2d278 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20  SELECT ){.      
2d279 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d27a 30 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75  0(v, OP_ResetCou
2d27b 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  nt);.    }.  }..
2d27c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23    return 0;.}..#
2d27d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
2d27e 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  UG./*.** This fu
2d27f 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
2d280 6f 20 61 64 64 20 56 64 62 65 43 6f 6d 6d 65 6e  o add VdbeCommen
2d281 74 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e 73 20  t() annotations 
2d282 74 6f 20 61 20 56 44 42 45 0a 2a 2a 20 70 72 6f  to a VDBE.** pro
2d283 67 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f 74 20  gram. It is not 
2d284 75 73 65 64 20 69 6e 20 70 72 6f 64 75 63 74 69  used in producti
2d285 6f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20 66 6f  on code, only fo
2d286 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a  r debugging..*/.
2d287 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
2d288 72 20 2a 6f 6e 45 72 72 6f 72 54 65 78 74 28 69  r *onErrorText(i
2d289 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 73  nt onError){.  s
2d28a 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29  witch( onError )
2d28b 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62  {.    case OE_Ab
2d28c 6f 72 74 3a 20 20 20 20 72 65 74 75 72 6e 20 22  ort:    return "
2d28d 61 62 6f 72 74 22 3b 0a 20 20 20 20 63 61 73 65  abort";.    case
2d28e 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 20 72 65   OE_Rollback: re
2d28f 74 75 72 6e 20 22 72 6f 6c 6c 62 61 63 6b 22 3b  turn "rollback";
2d290 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69  .    case OE_Fai
2d291 6c 3a 20 20 20 20 20 72 65 74 75 72 6e 20 22 66  l:     return "f
2d292 61 69 6c 22 3b 0a 20 20 20 20 63 61 73 65 20 4f  ail";.    case O
2d293 45 5f 52 65 70 6c 61 63 65 3a 20 20 72 65 74 75  E_Replace:  retu
2d294 72 6e 20 22 72 65 70 6c 61 63 65 22 3b 0a 20 20  rn "replace";.  
2d295 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
2d296 3a 20 20 20 72 65 74 75 72 6e 20 22 69 67 6e 6f  :   return "igno
2d297 72 65 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  re";.    case OE
2d298 5f 44 65 66 61 75 6c 74 3a 20 20 72 65 74 75 72  _Default:  retur
2d299 6e 20 22 64 65 66 61 75 6c 74 22 3b 0a 20 20 7d  n "default";.  }
2d29a 0a 20 20 72 65 74 75 72 6e 20 22 6e 2f 61 22 3b  .  return "n/a";
2d29b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
2d29c 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 73   Parse context s
2d29d 74 72 75 63 74 75 72 65 20 70 46 72 6f 6d 20 68  tructure pFrom h
2d29e 61 73 20 6a 75 73 74 20 62 65 65 6e 20 75 73 65  as just been use
2d29f 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 75  d to create a su
2d2a0 62 2d 76 64 62 65 0a 2a 2a 20 28 74 72 69 67 67  b-vdbe.** (trigg
2d2a1 65 72 20 70 72 6f 67 72 61 6d 29 2e 20 49 66 20  er program). If 
2d2a2 61 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  an error has occ
2d2a3 75 72 72 65 64 2c 20 74 72 61 6e 73 66 65 72 20  urred, transfer 
2d2a4 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  error informatio
2d2a5 6e 0a 2a 2a 20 66 72 6f 6d 20 70 46 72 6f 6d 20  n.** from pFrom 
2d2a6 74 6f 20 70 54 6f 2e 0a 2a 2f 0a 73 74 61 74 69  to pTo..*/.stati
2d2a7 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72 50  c void transferP
2d2a8 61 72 73 65 45 72 72 6f 72 28 50 61 72 73 65 20  arseError(Parse 
2d2a9 2a 70 54 6f 2c 20 50 61 72 73 65 20 2a 70 46 72  *pTo, Parse *pFr
2d2aa 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  om){.  assert( p
2d2ab 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  From->zErrMsg==0
2d2ac 20 7c 7c 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 20   || pFrom->nErr 
2d2ad 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  );.  assert( pTo
2d2ae 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c 7c 20  ->zErrMsg==0 || 
2d2af 70 54 6f 2d 3e 6e 45 72 72 20 29 3b 0a 20 20 69  pTo->nErr );.  i
2d2b0 66 28 20 70 54 6f 2d 3e 6e 45 72 72 3d 3d 30 20  f( pTo->nErr==0 
2d2b1 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 45 72 72  ){.    pTo->zErr
2d2b2 4d 73 67 20 3d 20 70 46 72 6f 6d 2d 3e 7a 45 72  Msg = pFrom->zEr
2d2b3 72 4d 73 67 3b 0a 20 20 20 20 70 54 6f 2d 3e 6e  rMsg;.    pTo->n
2d2b4 45 72 72 20 3d 20 70 46 72 6f 6d 2d 3e 6e 45 72  Err = pFrom->nEr
2d2b5 72 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  r;.  }else{.    
2d2b6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 46  sqlite3DbFree(pF
2d2b7 72 6f 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d 3e  rom->db, pFrom->
2d2b8 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a  zErrMsg);.  }.}.
2d2b9 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e  ./*.** Create an
2d2ba 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65 77  d populate a new
2d2bb 20 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65   TriggerPrg obje
2d2bc 63 74 20 77 69 74 68 20 61 20 73 75 62 2d 70 72  ct with a sub-pr
2d2bd 6f 67 72 61 6d 20 0a 2a 2a 20 69 6d 70 6c 65 6d  ogram .** implem
2d2be 65 6e 74 69 6e 67 20 74 72 69 67 67 65 72 20 70  enting trigger p
2d2bf 54 72 69 67 67 65 72 20 77 69 74 68 20 4f 4e 20  Trigger with ON 
2d2c0 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
2d2c1 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a 73 74 61 74 69  orconf..*/.stati
2d2c2 63 20 54 72 69 67 67 65 72 50 72 67 20 2a 63 6f  c TriggerPrg *co
2d2c3 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20  deRowTrigger(.  
2d2c4 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2d2c5 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
2d2c6 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
2d2c7 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2d2c8 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67 67  gger,   /* Trigg
2d2c9 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20  er to code */.  
2d2ca 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
2d2cb 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2d2cc 65 20 70 54 72 69 67 67 65 72 20 69 73 20 61 74  e pTrigger is at
2d2cd 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69  tached to */.  i
2d2ce 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20  nt orconf       
2d2cf 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
2d2d0 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 63 6f 64  CT policy to cod
2d2d1 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
2d2d2 6d 20 77 69 74 68 20 2a 2f 0a 29 7b 0a 20 20 50  m with */.){.  P
2d2d3 61 72 73 65 20 2a 70 54 6f 70 20 3d 20 73 71 6c  arse *pTop = sql
2d2d4 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
2d2d5 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  l(pParse);.  sql
2d2d6 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2d2d7 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74 61  e->db;   /* Data
2d2d8 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
2d2d9 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72   TriggerPrg *pPr
2d2da 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
2d2db 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
2d2dc 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e  */.  Expr *pWhen
2d2dd 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2d2de 20 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 6f 66   /* Duplicate of
2d2df 20 74 72 69 67 67 65 72 20 57 48 45 4e 20 65 78   trigger WHEN ex
2d2e0 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 56 64  pression */.  Vd
2d2e1 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
2d2e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
2d2e3 70 6f 72 61 72 79 20 56 4d 20 2a 2f 0a 20 20 4e  porary VM */.  N
2d2e4 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
2d2e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
2d2e6 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 73  me context for s
2d2e7 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 53 75 62  ub-vdbe */.  Sub
2d2e8 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61  Program *pProgra
2d2e9 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75 62 2d  m = 0;   /* Sub-
2d2ea 76 64 62 65 20 66 6f 72 20 74 72 69 67 67 65 72  vdbe for trigger
2d2eb 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 50 61   program */.  Pa
2d2ec 72 73 65 20 2a 70 53 75 62 50 61 72 73 65 3b 20  rse *pSubParse; 
2d2ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2d2ee 73 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 73  se context for s
2d2ef 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 69 6e 74  ub-vdbe */.  int
2d2f0 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d 20 30   iEndTrigger = 0
2d2f1 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65  ;        /* Labe
2d2f2 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20  l to jump to if 
2d2f3 57 48 45 4e 20 69 73 20 66 61 6c 73 65 20 2a 2f  WHEN is false */
2d2f4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69  ..  assert( pTri
2d2f5 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c  gger->zName==0 |
2d2f6 7c 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54  | pTab==tableOfT
2d2f7 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29  rigger(pTrigger)
2d2f8 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61   );..  /* Alloca
2d2f9 74 65 20 74 68 65 20 54 72 69 67 67 65 72 50 72  te the TriggerPr
2d2fa 67 20 61 6e 64 20 53 75 62 50 72 6f 67 72 61 6d  g and SubProgram
2d2fb 20 6f 62 6a 65 63 74 73 2e 20 54 6f 20 65 6e 73   objects. To ens
2d2fc 75 72 65 20 74 68 61 74 20 74 68 65 79 0a 20 20  ure that they.  
2d2fd 2a 2a 20 61 72 65 20 66 72 65 65 64 20 69 66 20  ** are freed if 
2d2fe 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2d2ff 20 6c 69 6e 6b 20 74 68 65 6d 20 69 6e 74 6f 20   link them into 
2d300 74 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67  the Parse.pTrigg
2d301 65 72 50 72 67 20 0a 20 20 2a 2a 20 6c 69 73 74  erPrg .  ** list
2d302 20 6f 66 20 74 68 65 20 74 6f 70 2d 6c 65 76 65   of the top-leve
2d303 6c 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 73  l Parse object s
2d304 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61  ooner rather tha
2d305 6e 20 6c 61 74 65 72 2e 20 20 2a 2f 0a 20 20 70  n later.  */.  p
2d306 50 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Prg = sqlite3DbM
2d307 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2d308 7a 65 6f 66 28 54 72 69 67 67 65 72 50 72 67 29  zeof(TriggerPrg)
2d309 29 3b 0a 20 20 69 66 28 20 21 70 50 72 67 20 29  );.  if( !pPrg )
2d30a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 50 72   return 0;.  pPr
2d30b 67 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 2d  g->pNext = pTop-
2d30c 3e 70 54 72 69 67 67 65 72 50 72 67 3b 0a 20 20  >pTriggerPrg;.  
2d30d 70 54 6f 70 2d 3e 70 54 72 69 67 67 65 72 50 72  pTop->pTriggerPr
2d30e 67 20 3d 20 70 50 72 67 3b 0a 20 20 70 50 72 67  g = pPrg;.  pPrg
2d30f 2d 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70 50 72  ->pProgram = pPr
2d310 6f 67 72 61 6d 20 3d 20 73 71 6c 69 74 65 33 44  ogram = sqlite3D
2d311 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2d312 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61  sizeof(SubProgra
2d313 6d 29 29 3b 0a 20 20 69 66 28 20 21 70 50 72 6f  m));.  if( !pPro
2d314 67 72 61 6d 20 29 20 72 65 74 75 72 6e 20 30 3b  gram ) return 0;
2d315 0a 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 52 65  .  pProgram->nRe
2d316 66 20 3d 20 31 3b 0a 20 20 70 50 72 67 2d 3e 70  f = 1;.  pPrg->p
2d317 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67  Trigger = pTrigg
2d318 65 72 3b 0a 20 20 70 50 72 67 2d 3e 6f 72 63 6f  er;.  pPrg->orco
2d319 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 70  nf = orconf;.  p
2d31a 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 30 5d  Prg->aColmask[0]
2d31b 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a 20   = 0xffffffff;. 
2d31c 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b   pPrg->aColmask[
2d31d 31 5d 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b  1] = 0xffffffff;
2d31e 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
2d31f 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e  and populate a n
2d320 65 77 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  ew Parse context
2d321 20 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 64 69   to use for codi
2d322 6e 67 20 74 68 65 20 0a 20 20 2a 2a 20 74 72 69  ng the .  ** tri
2d323 67 67 65 72 20 73 75 62 2d 70 72 6f 67 72 61 6d  gger sub-program
2d324 2e 20 20 2a 2f 0a 20 20 70 53 75 62 50 61 72 73  .  */.  pSubPars
2d325 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b  e = sqlite3Stack
2d326 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  AllocZero(db, si
2d327 7a 65 6f 66 28 50 61 72 73 65 29 29 3b 0a 20 20  zeof(Parse));.  
2d328 69 66 28 20 21 70 53 75 62 50 61 72 73 65 20 29  if( !pSubParse )
2d329 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d   return 0;.  mem
2d32a 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2d32b 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
2d32c 2e 70 50 61 72 73 65 20 3d 20 70 53 75 62 50 61  .pParse = pSubPa
2d32d 72 73 65 3b 0a 20 20 70 53 75 62 50 61 72 73 65  rse;.  pSubParse
2d32e 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 53 75  ->db = db;.  pSu
2d32f 62 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  bParse->pTrigger
2d330 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 70 53  Tab = pTab;.  pS
2d331 75 62 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76  ubParse->pToplev
2d332 65 6c 20 3d 20 70 54 6f 70 3b 0a 20 20 70 53 75  el = pTop;.  pSu
2d333 62 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  bParse->zAuthCon
2d334 74 65 78 74 20 3d 20 70 54 72 69 67 67 65 72 2d  text = pTrigger-
2d335 3e 7a 4e 61 6d 65 3b 0a 20 20 70 53 75 62 50 61  >zName;.  pSubPa
2d336 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 20  rse->eTriggerOp 
2d337 3d 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 3b 0a  = pTrigger->op;.
2d338 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
2d339 74 56 64 62 65 28 70 53 75 62 50 61 72 73 65 29  tVdbe(pSubParse)
2d33a 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
2d33b 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2d33c 20 22 53 74 61 72 74 3a 20 25 73 2e 25 73 20 28   "Start: %s.%s (
2d33d 25 73 20 25 73 25 73 25 73 20 4f 4e 20 25 73 29  %s %s%s%s ON %s)
2d33e 22 2c 20 0a 20 20 20 20 20 20 70 54 72 69 67 67  ", .      pTrigg
2d33f 65 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72  er->zName, onErr
2d340 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 2c 0a  orText(orconf),.
2d341 20 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d        (pTrigger-
2d342 3e 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f  >tr_tm==TRIGGER_
2d343 42 45 46 4f 52 45 20 3f 20 22 42 45 46 4f 52 45  BEFORE ? "BEFORE
2d344 22 20 3a 20 22 41 46 54 45 52 22 29 2c 0a 20 20  " : "AFTER"),.  
2d345 20 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d        (pTrigger-
2d346 3e 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 3f  >op==TK_UPDATE ?
2d347 20 22 55 50 44 41 54 45 22 20 3a 20 22 22 29 2c   "UPDATE" : ""),
2d348 0a 20 20 20 20 20 20 20 20 28 70 54 72 69 67 67  .        (pTrigg
2d349 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52  er->op==TK_INSER
2d34a 54 20 3f 20 22 49 4e 53 45 52 54 22 20 3a 20 22  T ? "INSERT" : "
2d34b 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 54 72  "),.        (pTr
2d34c 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 45  igger->op==TK_DE
2d34d 4c 45 54 45 20 3f 20 22 44 45 4c 45 54 45 22 20  LETE ? "DELETE" 
2d34e 3a 20 22 22 29 2c 0a 20 20 20 20 20 20 70 54 61  : ""),.      pTa
2d34f 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 29 3b  b->zName.    ));
2d350 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2d351 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 73  OMIT_TRACE.    s
2d352 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2d353 50 34 28 76 2c 20 2d 31 2c 20 0a 20 20 20 20 20  P4(v, -1, .     
2d354 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2d355 64 62 2c 20 22 2d 2d 20 54 52 49 47 47 45 52 20  db, "-- TRIGGER 
2d356 25 73 22 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a  %s", pTrigger->z
2d357 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49  Name), P4_DYNAMI
2d358 43 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a  C.    );.#endif.
2d359 0a 20 20 20 20 2f 2a 20 49 66 20 6f 6e 65 20 77  .    /* If one w
2d35a 61 73 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f  as specified, co
2d35b 64 65 20 74 68 65 20 57 48 45 4e 20 63 6c 61 75  de the WHEN clau
2d35c 73 65 2e 20 49 66 20 69 74 20 65 76 61 6c 75 61  se. If it evalua
2d35d 74 65 73 20 74 6f 20 66 61 6c 73 65 0a 20 20 20  tes to false.   
2d35e 20 2a 2a 20 28 6f 72 20 4e 55 4c 4c 29 20 74 68   ** (or NULL) th
2d35f 65 20 73 75 62 2d 76 64 62 65 20 69 73 20 69 6d  e sub-vdbe is im
2d360 6d 65 64 69 61 74 65 6c 79 20 68 61 6c 74 65 64  mediately halted
2d361 20 62 79 20 6a 75 6d 70 69 6e 67 20 74 6f 20 74   by jumping to t
2d362 68 65 20 0a 20 20 20 20 2a 2a 20 4f 50 5f 48 61  he .    ** OP_Ha
2d363 6c 74 20 69 6e 73 65 72 74 65 64 20 61 74 20 74  lt inserted at t
2d364 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72  he end of the pr
2d365 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 69  ogram.  */.    i
2d366 66 28 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  f( pTrigger->pWh
2d367 65 6e 20 29 7b 0a 20 20 20 20 20 20 70 57 68 65  en ){.      pWhe
2d368 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  n = sqlite3ExprD
2d369 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  up(db, pTrigger-
2d36a 3e 70 57 68 65 6e 2c 20 30 29 3b 0a 20 20 20 20  >pWhen, 0);.    
2d36b 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
2d36c 3d 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45  =sqlite3ResolveE
2d36d 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
2d36e 57 68 65 6e 29 20 0a 20 20 20 20 20 20 20 26 26  When) .       &&
2d36f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2d370 64 3d 3d 30 20 0a 20 20 20 20 20 20 29 7b 0a 20  d==0 .      ){. 
2d371 20 20 20 20 20 20 20 69 45 6e 64 54 72 69 67 67         iEndTrigg
2d372 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  er = sqlite3Vdbe
2d373 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2d374 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2d375 72 49 66 46 61 6c 73 65 28 70 53 75 62 50 61 72  rIfFalse(pSubPar
2d376 73 65 2c 20 70 57 68 65 6e 2c 20 69 45 6e 64 54  se, pWhen, iEndT
2d377 72 69 67 67 65 72 2c 20 53 51 4c 49 54 45 5f 4a  rigger, SQLITE_J
2d378 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
2d379 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2d37a 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2d37b 70 57 68 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20  pWhen);.    }.. 
2d37c 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 74     /* Code the t
2d37d 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 69  rigger program i
2d37e 6e 74 6f 20 74 68 65 20 73 75 62 2d 76 64 62 65  nto the sub-vdbe
2d37f 2e 20 2a 2f 0a 20 20 20 20 63 6f 64 65 54 72 69  . */.    codeTri
2d380 67 67 65 72 50 72 6f 67 72 61 6d 28 70 53 75 62  ggerProgram(pSub
2d381 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2d  Parse, pTrigger-
2d382 3e 73 74 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f  >step_list, orco
2d383 6e 66 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73  nf);..    /* Ins
2d384 65 72 74 20 61 6e 20 4f 50 5f 48 61 6c 74 20 61  ert an OP_Halt a
2d385 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
2d386 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 2a 2f   sub-program. */
2d387 0a 20 20 20 20 69 66 28 20 69 45 6e 64 54 72 69  .    if( iEndTri
2d388 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71  gger ){.      sq
2d389 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2d38a 4c 61 62 65 6c 28 76 2c 20 69 45 6e 64 54 72 69  Label(v, iEndTri
2d38b 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gger);.    }.   
2d38c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d38d 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a  p0(v, OP_Halt);.
2d38e 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2d38f 28 76 2c 20 22 45 6e 64 3a 20 25 73 2e 25 73 22  (v, "End: %s.%s"
2d390 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  , pTrigger->zNam
2d391 65 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f  e, onErrorText(o
2d392 72 63 6f 6e 66 29 29 29 3b 0a 0a 20 20 20 20 74  rconf)));..    t
2d393 72 61 6e 73 66 65 72 50 61 72 73 65 45 72 72 6f  ransferParseErro
2d394 72 28 70 50 61 72 73 65 2c 20 70 53 75 62 50 61  r(pParse, pSubPa
2d395 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 64 62  rse);.    if( db
2d396 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
2d397 30 20 29 7b 0a 20 20 20 20 20 20 70 50 72 6f 67  0 ){.      pProg
2d398 72 61 6d 2d 3e 61 4f 70 20 3d 20 73 71 6c 69 74  ram->aOp = sqlit
2d399 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61  e3VdbeTakeOpArra
2d39a 79 28 76 2c 20 26 70 50 72 6f 67 72 61 6d 2d 3e  y(v, &pProgram->
2d39b 6e 4f 70 2c 20 26 70 54 6f 70 2d 3e 6e 4d 61 78  nOp, &pTop->nMax
2d39c 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Arg);.    }.    
2d39d 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 3d  pProgram->nMem =
2d39e 20 70 53 75 62 50 61 72 73 65 2d 3e 6e 4d 65 6d   pSubParse->nMem
2d39f 3b 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e  ;.    pProgram->
2d3a0 6e 43 73 72 20 3d 20 70 53 75 62 50 61 72 73 65  nCsr = pSubParse
2d3a1 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 70 50 72 6f  ->nTab;.    pPro
2d3a2 67 72 61 6d 2d 3e 74 6f 6b 65 6e 20 3d 20 28 76  gram->token = (v
2d3a3 6f 69 64 20 2a 29 70 54 72 69 67 67 65 72 3b 0a  oid *)pTrigger;.
2d3a4 20 20 20 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61      pPrg->aColma
2d3a5 73 6b 5b 30 5d 20 3d 20 70 53 75 62 50 61 72 73  sk[0] = pSubPars
2d3a6 65 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a 20 20 20 20  e->oldmask;.    
2d3a7 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 31  pPrg->aColmask[1
2d3a8 5d 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6e  ] = pSubParse->n
2d3a9 65 77 6d 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69  ewmask;.    sqli
2d3aa 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 76 29  te3VdbeDelete(v)
2d3ab 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
2d3ac 20 21 70 53 75 62 50 61 72 73 65 2d 3e 70 41 69   !pSubParse->pAi
2d3ad 6e 63 20 20 20 20 20 20 20 26 26 20 21 70 53 75  nc       && !pSu
2d3ae 62 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54  bParse->pZombieT
2d3af 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ab );.  assert( 
2d3b0 21 70 53 75 62 50 61 72 73 65 2d 3e 70 54 72 69  !pSubParse->pTri
2d3b1 67 67 65 72 50 72 67 20 26 26 20 21 70 53 75 62  ggerPrg && !pSub
2d3b2 50 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 20 29  Parse->nMaxArg )
2d3b3 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ;.  sqlite3Stack
2d3b4 46 72 65 65 28 64 62 2c 20 70 53 75 62 50 61 72  Free(db, pSubPar
2d3b5 73 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70  se);..  return p
2d3b6 50 72 67 3b 0a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a  Prg;.}.    ./*.*
2d3b7 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
2d3b8 65 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 50  er to a TriggerP
2d3b9 72 67 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69  rg object contai
2d3ba 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 70 72 6f  ning the sub-pro
2d3bb 67 72 61 6d 20 66 6f 72 0a 2a 2a 20 74 72 69 67  gram for.** trig
2d3bc 67 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74  ger pTrigger wit
2d3bd 68 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e  h default ON CON
2d3be 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 20  FLICT algorithm 
2d3bf 6f 72 63 6f 6e 66 2e 20 49 66 20 6e 6f 20 73 75  orconf. If no su
2d3c0 63 68 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 67  ch.** TriggerPrg
2d3c1 20 6f 62 6a 65 63 74 20 65 78 69 73 74 73 2c 20   object exists, 
2d3c2 61 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 73 20  a new object is 
2d3c3 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
2d3c4 70 75 6c 61 74 65 64 20 62 65 66 6f 72 65 0a 2a  pulated before.*
2d3c5 2a 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64  * being returned
2d3c6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67  ..*/.static Trig
2d3c7 67 65 72 50 72 67 20 2a 67 65 74 52 6f 77 54 72  gerPrg *getRowTr
2d3c8 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
2d3c9 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2d3ca 20 43 75 72 72 65 6e 74 20 70 61 72 73 65 20 63   Current parse c
2d3cb 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
2d3cc 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20  ger *pTrigger,  
2d3cd 20 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63   /* Trigger to c
2d3ce 6f 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ode */.  Table *
2d3cf 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a  pTab,         /*
2d3d0 20 54 68 65 20 74 61 62 6c 65 20 74 72 69 67 67   The table trigg
2d3d1 65 72 20 70 54 72 69 67 67 65 72 20 69 73 20 61  er pTrigger is a
2d3d2 74 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20  ttached to */.  
2d3d3 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20  int orconf      
2d3d4 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c       /* ON CONFL
2d3d5 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 2e 20 2a  ICT algorithm. *
2d3d6 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 52  /.){.  Parse *pR
2d3d7 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 61 72  oot = sqlite3Par
2d3d8 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2d3d9 65 29 3b 0a 20 20 54 72 69 67 67 65 72 50 72 67  e);.  TriggerPrg
2d3da 20 2a 70 50 72 67 3b 0a 0a 20 20 61 73 73 65 72   *pPrg;..  asser
2d3db 74 28 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61  t( pTrigger->zNa
2d3dc 6d 65 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74  me==0 || pTab==t
2d3dd 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54  ableOfTrigger(pT
2d3de 72 69 67 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a  rigger) );..  /*
2d3df 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 20   It may be that 
2d3e0 74 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73  this trigger has
2d3e1 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f   already been co
2d3e2 64 65 64 20 28 6f 72 20 69 73 20 69 6e 20 74 68  ded (or is in th
2d3e3 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f  e.  ** process o
2d3e4 66 20 62 65 69 6e 67 20 63 6f 64 65 64 29 2e 20  f being coded). 
2d3e5 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
2d3e6 61 73 65 2c 20 74 68 65 6e 20 61 6e 20 65 6e 74  ase, then an ent
2d3e7 72 79 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6d  ry with.  ** a m
2d3e8 61 74 63 68 69 6e 67 20 54 72 69 67 67 65 72 50  atching TriggerP
2d3e9 72 67 2e 70 54 72 69 67 67 65 72 20 66 69 65 6c  rg.pTrigger fiel
2d3ea 64 20 77 69 6c 6c 20 62 65 20 70 72 65 73 65 6e  d will be presen
2d3eb 74 20 73 6f 6d 65 77 68 65 72 65 0a 20 20 2a 2a  t somewhere.  **
2d3ec 20 69 6e 20 74 68 65 20 50 61 72 73 65 2e 70 54   in the Parse.pT
2d3ed 72 69 67 67 65 72 50 72 67 20 6c 69 73 74 2e 20  riggerPrg list. 
2d3ee 53 65 61 72 63 68 20 66 6f 72 20 73 75 63 68 20  Search for such 
2d3ef 61 6e 20 65 6e 74 72 79 2e 20 20 2a 2f 0a 20 20  an entry.  */.  
2d3f0 66 6f 72 28 70 50 72 67 3d 70 52 6f 6f 74 2d 3e  for(pPrg=pRoot->
2d3f1 70 54 72 69 67 67 65 72 50 72 67 3b 20 0a 20 20  pTriggerPrg; .  
2d3f2 20 20 20 20 70 50 72 67 20 26 26 20 28 70 50 72      pPrg && (pPr
2d3f3 67 2d 3e 70 54 72 69 67 67 65 72 21 3d 70 54 72  g->pTrigger!=pTr
2d3f4 69 67 67 65 72 20 7c 7c 20 70 50 72 67 2d 3e 6f  igger || pPrg->o
2d3f5 72 63 6f 6e 66 21 3d 6f 72 63 6f 6e 66 29 3b 20  rconf!=orconf); 
2d3f6 0a 20 20 20 20 20 20 70 50 72 67 3d 70 50 72 67  .      pPrg=pPrg
2d3f7 2d 3e 70 4e 65 78 74 0a 20 20 29 3b 0a 0a 20 20  ->pNext.  );..  
2d3f8 2f 2a 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e  /* If an existin
2d3f9 67 20 54 72 69 67 67 65 72 50 72 67 20 63 6f 75  g TriggerPrg cou
2d3fa 6c 64 20 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65  ld not be locate
2d3fb 64 2c 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  d, create a new 
2d3fc 6f 6e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70  one. */.  if( !p
2d3fd 50 72 67 20 29 7b 0a 20 20 20 20 70 50 72 67 20  Prg ){.    pPrg 
2d3fe 3d 20 63 6f 64 65 52 6f 77 54 72 69 67 67 65 72  = codeRowTrigger
2d3ff 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
2d400 72 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29  r, pTab, orconf)
2d401 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
2d402 70 50 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  pPrg;.}../*.** G
2d403 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2d404 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
2d405 67 72 61 6d 20 61 73 73 6f 63 69 61 74 65 64 20  gram associated 
2d406 77 69 74 68 20 74 72 69 67 67 65 72 20 70 20 6f  with trigger p o
2d407 6e 20 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  n .** table pTab
2d408 2e 20 54 68 65 20 72 65 67 2c 20 6f 72 63 6f 6e  . The reg, orcon
2d409 66 20 61 6e 64 20 69 67 6e 6f 72 65 4a 75 6d 70  f and ignoreJump
2d40a 20 70 61 72 61 6d 65 74 65 72 73 20 70 61 73 73   parameters pass
2d40b 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75  ed to this.** fu
2d40c 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 73  nction are the s
2d40d 61 6d 65 20 61 73 20 74 68 6f 73 65 20 64 65 73  ame as those des
2d40e 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 68 65  cribed in the he
2d40f 61 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ader function fo
2d410 72 0a 2a 2a 20 73 71 6c 69 74 65 33 43 6f 64 65  r.** sqlite3Code
2d411 52 6f 77 54 72 69 67 67 65 72 28 29 0a 2a 2f 0a  RowTrigger().*/.
2d412 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2d413 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52  oid sqlite3CodeR
2d414 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28  owTriggerDirect(
2d415 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2d416 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65  ,       /* Parse
2d417 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72   context */.  Tr
2d418 69 67 67 65 72 20 2a 70 2c 20 20 20 20 20 20 20  igger *p,       
2d419 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 74 6f     /* Trigger to
2d41a 20 63 6f 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65   code */.  Table
2d41b 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
2d41c 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
2d41d 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72  code triggers fr
2d41e 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 2c  om */.  int reg,
2d41f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d420 52 65 67 20 61 72 72 61 79 20 63 6f 6e 74 61 69  Reg array contai
2d421 6e 69 6e 67 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e  ning OLD.* and N
2d422 45 57 2e 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  EW.* values */. 
2d423 20 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20   int orconf,    
2d424 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46        /* ON CONF
2d425 4c 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20  LICT policy */. 
2d426 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20   int ignoreJump 
2d427 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63        /* Instruc
2d428 74 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20  tion to jump to 
2d429 66 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45  for RAISE(IGNORE
2d42a 29 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  ) */.){.  Vdbe *
2d42b 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2d42c 62 65 28 70 50 61 72 73 65 29 3b 20 2f 2a 20 4d  be(pParse); /* M
2d42d 61 69 6e 20 56 4d 20 2a 2f 0a 20 20 54 72 69 67  ain VM */.  Trig
2d42e 67 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 20 20  gerPrg *pPrg;.  
2d42f 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69  pPrg = getRowTri
2d430 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20  gger(pParse, p, 
2d431 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20  pTab, orconf);. 
2d432 20 61 73 73 65 72 74 28 20 70 50 72 67 20 7c 7c   assert( pPrg ||
2d433 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
2d434 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
2d435 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20  locFailed );..  
2d436 2f 2a 20 43 6f 64 65 20 74 68 65 20 4f 50 5f 50  /* Code the OP_P
2d437 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 69 6e  rogram opcode in
2d438 20 74 68 65 20 70 61 72 65 6e 74 20 56 44 42 45   the parent VDBE
2d439 2e 20 50 34 20 6f 66 20 74 68 65 20 4f 50 5f 50  . P4 of the OP_P
2d43a 72 6f 67 72 61 6d 20 0a 20 20 2a 2a 20 69 73 20  rogram .  ** is 
2d43b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2d43c 20 73 75 62 2d 76 64 62 65 20 63 6f 6e 74 61 69   sub-vdbe contai
2d43d 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ning the trigger
2d43e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20   program.  */.  
2d43f 69 66 28 20 70 50 72 67 20 29 7b 0a 20 20 20 20  if( pPrg ){.    
2d440 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d441 33 28 76 2c 20 4f 50 5f 50 72 6f 67 72 61 6d 2c  3(v, OP_Program,
2d442 20 72 65 67 2c 20 69 67 6e 6f 72 65 4a 75 6d 70   reg, ignoreJump
2d443 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  , ++pParse->nMem
2d444 29 3b 0a 20 20 20 20 70 50 72 67 2d 3e 70 50 72  );.    pPrg->pPr
2d445 6f 67 72 61 6d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  ogram->nRef++;. 
2d446 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2d447 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
2d448 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 50 72 67  onst char *)pPrg
2d449 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 50 34 5f 53  ->pProgram, P4_S
2d44a 55 42 50 52 4f 47 52 41 4d 29 3b 0a 20 20 20 20  UBPROGRAM);.    
2d44b 56 64 62 65 43 6f 6d 6d 65 6e 74 28 0a 20 20 20  VdbeComment(.   
2d44c 20 20 20 20 20 28 76 2c 20 22 43 61 6c 6c 3a 20       (v, "Call: 
2d44d 25 73 2e 25 73 22 2c 20 28 70 2d 3e 7a 4e 61 6d  %s.%s", (p->zNam
2d44e 65 3f 70 2d 3e 7a 4e 61 6d 65 3a 22 66 6b 65 79  e?p->zName:"fkey
2d44f 22 29 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28  "), onErrorText(
2d450 6f 72 63 6f 6e 66 29 29 29 3b 0a 0a 20 20 20 20  orconf)));..    
2d451 2f 2a 20 53 65 74 20 74 68 65 20 50 35 20 6f 70  /* Set the P5 op
2d452 65 72 61 6e 64 20 6f 66 20 74 68 65 20 4f 50 5f  erand of the OP_
2d453 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74  Program instruct
2d454 69 6f 6e 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20  ion to non-zero 
2d455 69 66 0a 20 20 20 20 2a 2a 20 72 65 63 75 72 73  if.    ** recurs
2d456 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ive invocation o
2d457 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20 70  f this trigger p
2d458 72 6f 67 72 61 6d 20 69 73 20 64 69 73 61 6c 6c  rogram is disall
2d459 6f 77 65 64 2e 20 52 65 63 75 72 73 69 76 65 0a  owed. Recursive.
2d45a 20 20 20 20 2a 2a 20 69 6e 76 6f 63 61 74 69 6f      ** invocatio
2d45b 6e 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 20  n is disallowed 
2d45c 69 66 20 28 61 29 20 74 68 65 20 73 75 62 2d 70  if (a) the sub-p
2d45d 72 6f 67 72 61 6d 20 69 73 20 72 65 61 6c 6c 79  rogram is really
2d45e 20 61 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20   a trigger,.    
2d45f 2a 2a 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e  ** not a foreign
2d460 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64   key action, and
2d461 20 28 62 29 20 74 68 65 20 66 6c 61 67 20 74 6f   (b) the flag to
2d462 20 65 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76   enable recursiv
2d463 65 20 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a  e triggers.    *
2d464 2a 20 69 73 20 63 6c 65 61 72 2e 20 20 2a 2f 0a  * is clear.  */.
2d465 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2d466 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 28  hangeP5(v, (u8)(
2d467 70 2d 3e 7a 4e 61 6d 65 20 26 26 20 21 28 70 50  p->zName && !(pP
2d468 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26  arse->db->flags&
2d469 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65  SQLITE_RecTrigge
2d46a 72 73 29 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rs)));.  }.}../*
2d46b 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c  .** This is call
2d46c 65 64 20 74 6f 20 63 6f 64 65 20 74 68 65 20 72  ed to code the r
2d46d 65 71 75 69 72 65 64 20 46 4f 52 20 45 41 43 48  equired FOR EACH
2d46e 20 52 4f 57 20 74 72 69 67 67 65 72 73 20 66 6f   ROW triggers fo
2d46f 72 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  r an operation.*
2d470 2a 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 2e  * on table pTab.
2d471 20 54 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74   The operation t
2d472 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20  o code triggers 
2d473 66 6f 72 20 28 49 4e 53 45 52 54 2c 20 55 50 44  for (INSERT, UPD
2d474 41 54 45 20 6f 72 20 44 45 4c 45 54 45 29 0a 2a  ATE or DELETE).*
2d475 2a 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68  * is given by th
2d476 65 20 6f 70 20 70 61 72 61 6d 61 74 65 72 2e 20  e op paramater. 
2d477 54 68 65 20 74 72 5f 74 6d 20 70 61 72 61 6d 65  The tr_tm parame
2d478 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ter determines w
2d479 68 65 74 68 65 72 20 74 68 65 0a 2a 2a 20 42 45  hether the.** BE
2d47a 46 4f 52 45 20 6f 72 20 41 46 54 45 52 20 74 72  FORE or AFTER tr
2d47b 69 67 67 65 72 73 20 61 72 65 20 63 6f 64 65 64  iggers are coded
2d47c 2e 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69  . If the operati
2d47d 6f 6e 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c  on is an UPDATE,
2d47e 20 74 68 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74   then.** paramet
2d47f 65 72 20 70 43 68 61 6e 67 65 73 20 69 73 20 70  er pChanges is p
2d480 61 73 73 65 64 20 74 68 65 20 6c 69 73 74 20 6f  assed the list o
2d481 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20  f columns being 
2d482 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  modified..**.** 
2d483 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
2d484 74 72 69 67 67 65 72 73 20 74 68 61 74 20 66 69  triggers that fi
2d485 72 65 20 61 74 20 74 68 65 20 73 70 65 63 69 66  re at the specif
2d486 69 65 64 20 74 69 6d 65 20 66 6f 72 20 74 68 65  ied time for the
2d487 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 70   specified.** op
2d488 65 72 61 74 69 6f 6e 20 6f 6e 20 70 54 61 62 2c  eration on pTab,
2d489 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
2d48a 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
2d48b 20 54 68 65 20 72 65 67 20 61 72 67 75 6d 65 6e   The reg argumen
2d48c 74 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73  t is the address
2d48d 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 69 6e   of the first in
2d48e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67   an array of reg
2d48f 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
2d490 63 6f 6e 74 61 69 6e 20 74 68 65 20 76 61 6c 75  contain the valu
2d491 65 73 20 73 75 62 73 74 69 74 75 74 65 64 20 66  es substituted f
2d492 6f 72 20 74 68 65 20 6e 65 77 2e 2a 20 61 6e 64  or the new.* and
2d493 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65   old.* reference
2d494 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 72 69 67  s.** in the trig
2d495 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20  ger program. If 
2d496 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
2d497 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61  of columns in ta
2d498 62 6c 65 20 70 54 61 62 0a 2a 2a 20 28 61 20 63  ble pTab.** (a c
2d499 6f 70 79 20 6f 66 20 70 54 61 62 2d 3e 6e 43 6f  opy of pTab->nCo
2d49a 6c 29 2c 20 74 68 65 6e 20 72 65 67 69 73 74 65  l), then registe
2d49b 72 73 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64  rs are populated
2d49c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
2d49d 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 20 20  **   Register   
2d49e 20 20 20 20 43 6f 6e 74 61 69 6e 73 0a 2a 2a 20      Contains.** 
2d49f 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
2d4a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d4a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d4a2 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 72 65  --------.**   re
2d4a3 67 2b 30 20 20 20 20 20 20 20 20 20 20 4f 4c 44  g+0          OLD
2d4a4 2e 72 6f 77 69 64 0a 2a 2a 20 20 20 72 65 67 2b  .rowid.**   reg+
2d4a5 31 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a  1          OLD.*
2d4a6 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d   value of left-m
2d4a7 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54  ost column of pT
2d4a8 61 62 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20 20 20  ab.**   ...     
2d4a9 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
2d4aa 72 65 67 2b 4e 20 20 20 20 20 20 20 20 20 20 4f  reg+N          O
2d4ab 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20 72 69  LD.* value of ri
2d4ac 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20  ght-most column 
2d4ad 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20 72 65 67  of pTab.**   reg
2d4ae 2b 4e 2b 31 20 20 20 20 20 20 20 20 4e 45 57 2e  +N+1        NEW.
2d4af 72 6f 77 69 64 0a 2a 2a 20 20 20 72 65 67 2b 4e  rowid.**   reg+N
2d4b0 2b 32 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20  +2        OLD.* 
2d4b1 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f  value of left-mo
2d4b2 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61  st column of pTa
2d4b3 62 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20  b.**   ...      
2d4b4 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72        ....**   r
2d4b5 65 67 2b 4e 2b 4e 2b 31 20 20 20 20 20 20 4e 45  eg+N+N+1      NE
2d4b6 57 2e 2a 20 76 61 6c 75 65 20 6f 66 20 72 69 67  W.* value of rig
2d4b7 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ht-most column o
2d4b8 66 20 70 54 61 62 0a 2a 2a 0a 2a 2a 20 46 6f 72  f pTab.**.** For
2d4b9 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67   ON DELETE trigg
2d4ba 65 72 73 2c 20 74 68 65 20 72 65 67 69 73 74 65  ers, the registe
2d4bb 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
2d4bc 65 20 4e 45 57 2e 2a 20 76 61 6c 75 65 73 20 77  e NEW.* values w
2d4bd 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20  ill.** never be 
2d4be 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20  accessed by the 
2d4bf 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2c  trigger program,
2d4c0 20 73 6f 20 74 68 65 79 20 61 72 65 20 6e 6f 74   so they are not
2d4c1 20 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 0a 2a   allocated or .*
2d4c2 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 74  * populated by t
2d4c3 68 65 20 63 61 6c 6c 65 72 20 28 74 68 65 72 65  he caller (there
2d4c4 20 69 73 20 6e 6f 20 64 61 74 61 20 74 6f 20 70   is no data to p
2d4c5 6f 70 75 6c 61 74 65 20 74 68 65 6d 20 77 69 74  opulate them wit
2d4c6 68 20 61 6e 79 77 61 79 29 2e 20 0a 2a 2a 20 53  h anyway). .** S
2d4c7 69 6d 69 6c 61 72 6c 79 2c 20 66 6f 72 20 4f 4e  imilarly, for ON
2d4c8 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 73   INSERT triggers
2d4c9 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72   the values stor
2d4ca 65 64 20 69 6e 20 74 68 65 20 4f 4c 44 2e 2a 20  ed in the OLD.* 
2d4cb 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 61 72 65  registers.** are
2d4cc 20 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2c   never accessed,
2d4cd 20 61 6e 64 20 73 6f 20 61 72 65 20 6e 6f 74 20   and so are not 
2d4ce 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65  allocated by the
2d4cf 20 63 61 6c 6c 65 72 2e 20 53 6f 2c 20 66 6f 72   caller. So, for
2d4d0 20 61 6e 0a 2a 2a 20 4f 4e 20 49 4e 53 45 52 54   an.** ON INSERT
2d4d1 20 74 72 69 67 67 65 72 2c 20 74 68 65 20 76 61   trigger, the va
2d4d2 6c 75 65 20 70 61 73 73 65 64 20 74 6f 20 74 68  lue passed to th
2d4d3 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 70  is function as p
2d4d4 61 72 61 6d 65 74 65 72 20 72 65 67 0a 2a 2a 20  arameter reg.** 
2d4d5 69 73 20 6e 6f 74 20 61 20 72 65 61 64 61 62 6c  is not a readabl
2d4d6 65 20 72 65 67 69 73 74 65 72 2c 20 61 6c 74 68  e register, alth
2d4d7 6f 75 67 68 20 72 65 67 69 73 74 65 72 73 20 28  ough registers (
2d4d8 72 65 67 2b 4e 29 20 74 68 72 6f 75 67 68 20 0a  reg+N) through .
2d4d9 2a 2a 20 28 72 65 67 2b 4e 2b 4e 2b 31 29 20 61  ** (reg+N+N+1) a
2d4da 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  re..**.** Parame
2d4db 74 65 72 20 6f 72 63 6f 6e 66 20 69 73 20 74 68  ter orconf is th
2d4dc 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 6c 69  e default confli
2d4dd 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c  ct resolution al
2d4de 67 6f 72 69 74 68 6d 20 66 6f 72 20 74 68 65 0a  gorithm for the.
2d4df 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  ** trigger progr
2d4e0 61 6d 20 74 6f 20 75 73 65 20 28 52 45 50 4c 41  am to use (REPLA
2d4e1 43 45 2c 20 49 47 4e 4f 52 45 20 65 74 63 2e 29  CE, IGNORE etc.)
2d4e2 2e 20 50 61 72 61 6d 65 74 65 72 20 69 67 6e 6f  . Parameter igno
2d4e3 72 65 4a 75 6d 70 0a 2a 2a 20 69 73 20 74 68 65  reJump.** is the
2d4e4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
2d4e5 74 20 63 6f 6e 74 72 6f 6c 20 73 68 6f 75 6c 64  t control should
2d4e6 20 6a 75 6d 70 20 74 6f 20 69 66 20 61 20 74 72   jump to if a tr
2d4e7 69 67 67 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a  igger program.**
2d4e8 20 72 61 69 73 65 73 20 61 6e 20 49 47 4e 4f 52   raises an IGNOR
2d4e9 45 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a  E exception..*/.
2d4ea 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2d4eb 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52  oid sqlite3CodeR
2d4ec 6f 77 54 72 69 67 67 65 72 28 0a 20 20 50 61 72  owTrigger(.  Par
2d4ed 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2d4ee 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
2d4ef 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  xt */.  Trigger 
2d4f0 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20  *pTrigger,   /* 
2d4f1 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
2d4f2 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 2a   on table pTab *
2d4f3 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
2d4f4 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
2d4f5 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  of TK_UPDATE, TK
2d4f6 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45  _INSERT, TK_DELE
2d4f7 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  TE */.  ExprList
2d4f8 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
2d4f9 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72  Changes list for
2d4fa 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74   any UPDATE OF t
2d4fb 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
2d4fc 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20   tr_tm,         
2d4fd 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47    /* One of TRIG
2d4fe 47 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47  GER_BEFORE, TRIG
2d4ff 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54  GER_AFTER */.  T
2d500 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2d501 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2d502 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72   to code trigger
2d503 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  s from */.  int 
2d504 72 65 67 2c 20 20 20 20 20 20 20 20 20 20 20 20  reg,            
2d505 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 69 6e   /* The first in
2d506 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67   an array of reg
2d507 69 73 74 65 72 73 20 28 73 65 65 20 61 62 6f 76  isters (see abov
2d508 65 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  e) */.  int orco
2d509 6e 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf,          /* 
2d50a 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
2d50b 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f  cy */.  int igno
2d50c 72 65 4a 75 6d 70 20 20 20 20 20 20 20 2f 2a 20  reJump       /* 
2d50d 49 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6a  Instruction to j
2d50e 75 6d 70 20 74 6f 20 66 6f 72 20 52 41 49 53 45  ump to for RAISE
2d50f 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b 0a 20  (IGNORE) */.){. 
2d510 20 54 72 69 67 67 65 72 20 2a 70 3b 20 20 20 20   Trigger *p;    
2d511 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
2d512 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
2d513 20 70 54 72 69 67 67 65 72 20 6c 69 73 74 20 2a   pTrigger list *
2d514 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d  /..  assert( op=
2d515 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70  =TK_UPDATE || op
2d516 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f  ==TK_INSERT || o
2d517 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 29 3b 0a  p==TK_DELETE );.
2d518 20 20 61 73 73 65 72 74 28 20 74 72 5f 74 6d 3d    assert( tr_tm=
2d519 3d 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20  =TRIGGER_BEFORE 
2d51a 7c 7c 20 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45  || tr_tm==TRIGGE
2d51b 52 5f 41 46 54 45 52 20 29 3b 0a 20 20 61 73 73  R_AFTER );.  ass
2d51c 65 72 74 28 20 28 6f 70 3d 3d 54 4b 5f 55 50 44  ert( (op==TK_UPD
2d51d 41 54 45 29 3d 3d 28 70 43 68 61 6e 67 65 73 21  ATE)==(pChanges!
2d51e 3d 30 29 20 29 3b 0a 0a 20 20 66 6f 72 28 70 3d  =0) );..  for(p=
2d51f 70 54 72 69 67 67 65 72 3b 20 70 3b 20 70 3d 70  pTrigger; p; p=p
2d520 2d 3e 70 4e 65 78 74 29 7b 0a 0a 20 20 20 20 2f  ->pNext){..    /
2d521 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  * Sanity checkin
2d522 67 3a 20 20 54 68 65 20 73 63 68 65 6d 61 20 66  g:  The schema f
2d523 6f 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61  or the trigger a
2d524 6e 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  nd for the table
2d525 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6c 77 61   are.    ** alwa
2d526 79 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65  ys defined.  The
2d527 20 74 72 69 67 67 65 72 20 6d 75 73 74 20 62 65   trigger must be
2d528 20 69 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68   in the same sch
2d529 65 6d 61 20 61 73 20 74 68 65 20 74 61 62 6c 65  ema as the table
2d52a 0a 20 20 20 20 2a 2a 20 6f 72 20 65 6c 73 65 20  .    ** or else 
2d52b 69 74 20 6d 75 73 74 20 62 65 20 61 20 54 45 4d  it must be a TEM
2d52c 50 20 74 72 69 67 67 65 72 2e 20 2a 2f 0a 20 20  P trigger. */.  
2d52d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 53 63    assert( p->pSc
2d52e 68 65 6d 61 21 3d 30 20 29 3b 0a 20 20 20 20 61  hema!=0 );.    a
2d52f 73 73 65 72 74 28 20 70 2d 3e 70 54 61 62 53 63  ssert( p->pTabSc
2d530 68 65 6d 61 21 3d 30 20 29 3b 0a 20 20 20 20 61  hema!=0 );.    a
2d531 73 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d  ssert( p->pSchem
2d532 61 3d 3d 70 2d 3e 70 54 61 62 53 63 68 65 6d 61  a==p->pTabSchema
2d533 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d   .         || p-
2d534 3e 70 53 63 68 65 6d 61 3d 3d 70 50 61 72 73 65  >pSchema==pParse
2d535 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ->db->aDb[1].pSc
2d536 68 65 6d 61 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  hema );..    /* 
2d537 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  Determine whethe
2d538 72 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 64 65  r we should code
2d539 20 74 68 69 73 20 74 72 69 67 67 65 72 20 2a 2f   this trigger */
2d53a 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  .    if( p->op==
2d53b 6f 70 20 0a 20 20 20 20 20 26 26 20 70 2d 3e 74  op .     && p->t
2d53c 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 0a 20 20 20  r_tm==tr_tm .   
2d53d 20 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e    && checkColumn
2d53e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75  Overlap(p->pColu
2d53f 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29 0a 20  mns, pChanges). 
2d540 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69     ){.      sqli
2d541 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
2d542 72 44 69 72 65 63 74 28 70 50 61 72 73 65 2c 20  rDirect(pParse, 
2d543 70 2c 20 70 54 61 62 2c 20 72 65 67 2c 20 6f 72  p, pTab, reg, or
2d544 63 6f 6e 66 2c 20 69 67 6e 6f 72 65 4a 75 6d 70  conf, ignoreJump
2d545 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
2d546 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72 73 20 6d  /*.** Triggers m
2d547 61 79 20 61 63 63 65 73 73 20 76 61 6c 75 65 73  ay access values
2d548 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6f   stored in the o
2d549 6c 64 2e 2a 20 6f 72 20 6e 65 77 2e 2a 20 70 73  ld.* or new.* ps
2d54a 65 75 64 6f 2d 74 61 62 6c 65 2e 20 0a 2a 2a 20  eudo-table. .** 
2d54b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
2d54c 74 75 72 6e 73 20 61 20 33 32 2d 62 69 74 20 62  turns a 32-bit b
2d54d 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e  itmask indicatin
2d54e 67 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20  g which columns 
2d54f 6f 66 20 74 68 65 20 0a 2a 2a 20 6f 6c 64 2e 2a  of the .** old.*
2d550 20 6f 72 20 6e 65 77 2e 2a 20 74 61 62 6c 65 73   or new.* tables
2d551 20 61 63 74 75 61 6c 6c 79 20 61 72 65 20 75 73   actually are us
2d552 65 64 20 62 79 20 74 72 69 67 67 65 72 73 2e 20  ed by triggers. 
2d553 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
2d554 20 0a 2a 2a 20 6d 61 79 20 62 65 20 75 73 65 64   .** may be used
2d555 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2c 20   by the caller, 
2d556 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  for example, to 
2d557 61 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20  avoid having to 
2d558 6c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 0a  load the entire.
2d559 2a 2a 20 6f 6c 64 2e 2a 20 72 65 63 6f 72 64 20  ** old.* record 
2d55a 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 77 68 65 6e  into memory when
2d55b 20 65 78 65 63 75 74 69 6e 67 20 61 6e 20 55 50   executing an UP
2d55c 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 63  DATE or DELETE c
2d55d 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 42 69  ommand..**.** Bi
2d55e 74 20 30 20 6f 66 20 74 68 65 20 72 65 74 75 72  t 0 of the retur
2d55f 6e 65 64 20 6d 61 73 6b 20 69 73 20 73 65 74 20  ned mask is set 
2d560 69 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  if the left-most
2d561 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a   column of the.*
2d562 2a 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20 61  * table may be a
2d563 63 63 65 73 73 65 64 20 75 73 69 6e 67 20 61 6e  ccessed using an
2d564 20 5b 6f 6c 64 7c 6e 65 77 5d 2e 3c 63 6f 6c 3e   [old|new].<col>
2d565 20 72 65 66 65 72 65 6e 63 65 2e 20 42 69 74 20   reference. Bit 
2d566 31 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20 74  1 is set if.** t
2d567 68 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 6d 6f  he second leftmo
2d568 73 74 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  st column value 
2d569 69 73 20 72 65 71 75 69 72 65 64 2c 20 61 6e 64  is required, and
2d56a 20 73 6f 20 6f 6e 2e 20 49 66 20 74 68 65 72 65   so on. If there
2d56b 0a 2a 2a 20 61 72 65 20 6d 6f 72 65 20 74 68 61  .** are more tha
2d56c 6e 20 33 32 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  n 32 columns in 
2d56d 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 61  the table, and a
2d56e 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 74  t least one of t
2d56f 68 65 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 77 69  he columns.** wi
2d570 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61  th an index grea
2d571 74 65 72 20 74 68 61 6e 20 33 32 20 6d 61 79 20  ter than 32 may 
2d572 62 65 20 61 63 63 65 73 73 65 64 2c 20 30 78 66  be accessed, 0xf
2d573 66 66 66 66 66 66 66 20 69 73 20 72 65 74 75 72  fffffff is retur
2d574 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ned..**.** It is
2d575 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
2d576 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
2d577 65 20 6f 6c 64 2e 72 6f 77 69 64 20 6f 72 20 6e  e old.rowid or n
2d578 65 77 2e 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20  ew.rowid column 
2d579 69 73 20 0a 2a 2a 20 61 63 63 65 73 73 65 64 20  is .** accessed 
2d57a 62 79 20 74 72 69 67 67 65 72 73 2e 20 54 68 65  by triggers. The
2d57b 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 61 6c 77   caller must alw
2d57c 61 79 73 20 61 73 73 75 6d 65 20 74 68 61 74 20  ays assume that 
2d57d 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 50 61 72  it is..**.** Par
2d57e 61 6d 65 74 65 72 20 69 73 4e 65 77 20 6d 75 73  ameter isNew mus
2d57f 74 20 62 65 20 65 69 74 68 65 72 20 31 20 6f 72  t be either 1 or
2d580 20 30 2e 20 49 66 20 69 74 20 69 73 20 30 2c 20   0. If it is 0, 
2d581 74 68 65 6e 20 74 68 65 20 6d 61 73 6b 20 72 65  then the mask re
2d582 74 75 72 6e 65 64 0a 2a 2a 20 61 70 70 6c 69 65  turned.** applie
2d583 73 20 74 6f 20 74 68 65 20 6f 6c 64 2e 2a 20 74  s to the old.* t
2d584 61 62 6c 65 2e 20 49 66 20 31 2c 20 74 68 65 20  able. If 1, the 
2d585 6e 65 77 2e 2a 20 74 61 62 6c 65 2e 0a 2a 2a 0a  new.* table..**.
2d586 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 74 72 5f  ** Parameter tr_
2d587 74 6d 20 6d 75 73 74 20 62 65 20 61 20 6d 61 73  tm must be a mas
2d588 6b 20 77 69 74 68 20 6f 6e 65 20 6f 72 20 62 6f  k with one or bo
2d589 74 68 20 6f 66 20 74 68 65 20 54 52 49 47 47 45  th of the TRIGGE
2d58a 52 5f 42 45 46 4f 52 45 0a 2a 2a 20 61 6e 64 20  R_BEFORE.** and 
2d58b 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 62 69  TRIGGER_AFTER bi
2d58c 74 73 20 73 65 74 2e 20 56 61 6c 75 65 73 20 61  ts set. Values a
2d58d 63 63 65 73 73 65 64 20 62 79 20 42 45 46 4f 52  ccessed by BEFOR
2d58e 45 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f  E triggers are o
2d58f 6e 6c 79 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20  nly.** included 
2d590 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  in the returned 
2d591 6d 61 73 6b 20 69 66 20 74 68 65 20 54 52 49 47  mask if the TRIG
2d592 47 45 52 5f 42 45 46 4f 52 45 20 62 69 74 20 69  GER_BEFORE bit i
2d593 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20  s set in the.** 
2d594 74 72 5f 74 6d 20 70 61 72 61 6d 65 74 65 72 2e  tr_tm parameter.
2d595 20 53 69 6d 69 6c 61 72 6c 79 2c 20 76 61 6c 75   Similarly, valu
2d596 65 73 20 61 63 63 65 73 73 65 64 20 62 79 20 41  es accessed by A
2d597 46 54 45 52 20 74 72 69 67 67 65 72 73 20 61 72  FTER triggers ar
2d598 65 20 6f 6e 6c 79 0a 2a 2a 20 69 6e 63 6c 75 64  e only.** includ
2d599 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e  ed in the return
2d59a 65 64 20 6d 61 73 6b 20 69 66 20 74 68 65 20 54  ed mask if the T
2d59b 52 49 47 47 45 52 5f 41 46 54 45 52 20 62 69 74  RIGGER_AFTER bit
2d59c 20 69 73 20 73 65 74 20 69 6e 20 74 72 5f 74 6d   is set in tr_tm
2d59d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2d59e 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 54  ATE u32 sqlite3T
2d59f 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 0a 20  riggerColmask(. 
2d5a0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d5a1 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
2d5a2 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
2d5a3 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20  ger *pTrigger,  
2d5a4 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
2d5a5 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54  gers on table pT
2d5a6 61 62 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ab */.  ExprList
2d5a7 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20   *pChanges,  /* 
2d5a8 43 68 61 6e 67 65 73 20 6c 69 73 74 20 66 6f 72  Changes list for
2d5a9 20 61 6e 79 20 55 50 44 41 54 45 20 4f 46 20 74   any UPDATE OF t
2d5aa 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
2d5ab 20 69 73 4e 65 77 2c 20 20 20 20 20 20 20 20 20   isNew,         
2d5ac 20 20 2f 2a 20 31 20 66 6f 72 20 6e 65 77 2e 2a    /* 1 for new.*
2d5ad 20 72 65 66 20 6d 61 73 6b 2c 20 30 20 66 6f 72   ref mask, 0 for
2d5ae 20 6f 6c 64 2e 2a 20 72 65 66 20 6d 61 73 6b 20   old.* ref mask 
2d5af 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20  */.  int tr_tm, 
2d5b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
2d5b1 6b 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46  k of TRIGGER_BEF
2d5b2 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45  ORE|TRIGGER_AFTE
2d5b3 52 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  R */.  Table *pT
2d5b4 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ab,         /* T
2d5b5 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65  he table to code
2d5b6 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a   triggers from *
2d5b7 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20  /.  int orconf  
2d5b8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2d5b9 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ult ON CONFLICT 
2d5ba 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67  policy for trigg
2d5bb 65 72 20 73 74 65 70 73 20 2a 2f 0a 29 7b 0a 20  er steps */.){. 
2d5bc 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 20 3d 20   const int op = 
2d5bd 70 43 68 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50  pChanges ? TK_UP
2d5be 44 41 54 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45  DATE : TK_DELETE
2d5bf 3b 0a 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30  ;.  u32 mask = 0
2d5c0 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a  ;.  Trigger *p;.
2d5c1 0a 20 20 61 73 73 65 72 74 28 20 69 73 4e 65 77  .  assert( isNew
2d5c2 3d 3d 31 20 7c 7c 20 69 73 4e 65 77 3d 3d 30 20  ==1 || isNew==0 
2d5c3 29 3b 0a 20 20 66 6f 72 28 70 3d 70 54 72 69 67  );.  for(p=pTrig
2d5c4 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  ger; p; p=p->pNe
2d5c5 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  xt){.    if( p->
2d5c6 6f 70 3d 3d 6f 70 20 26 26 20 28 74 72 5f 74 6d  op==op && (tr_tm
2d5c7 26 70 2d 3e 74 72 5f 74 6d 29 0a 20 20 20 20 20  &p->tr_tm).     
2d5c8 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  && checkColumnOv
2d5c9 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e  erlap(p->pColumn
2d5ca 73 2c 70 43 68 61 6e 67 65 73 29 0a 20 20 20 20  s,pChanges).    
2d5cb 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72  ){.      Trigger
2d5cc 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 20 20 20  Prg *pPrg;.     
2d5cd 20 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72   pPrg = getRowTr
2d5ce 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c  igger(pParse, p,
2d5cf 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a   pTab, orconf);.
2d5d0 20 20 20 20 20 20 69 66 28 20 70 50 72 67 20 29        if( pPrg )
2d5d1 7b 0a 20 20 20 20 20 20 20 20 6d 61 73 6b 20 7c  {.        mask |
2d5d2 3d 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b  = pPrg->aColmask
2d5d3 5b 69 73 4e 65 77 5d 3b 0a 20 20 20 20 20 20 7d  [isNew];.      }
2d5d4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
2d5d5 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 23 65  turn mask;.}..#e
2d5d6 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
2d5d7 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
2d5d8 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  GGER) */../*****
2d5d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2d5da 20 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a   trigger.c *****
2d5db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5dd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2d5de 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2d5df 66 69 6c 65 20 75 70 64 61 74 65 2e 63 20 2a 2a  file update.c **
2d5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5e2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2d5e3 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
2d5e4 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
2d5e5 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2d5e6 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2d5e7 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2d5e8 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2d5e9 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2d5ea 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2d5eb 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2d5ec 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2d5ed 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2d5ee 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2d5ef 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2d5f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2d5f1 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2d5f2 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2d5f3 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2d5f4 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2d5f5 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2d5f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d5fa 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2d5fb 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
2d5fc 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
2d5fd 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
2d5fe 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c  rser.** to handl
2d5ff 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
2d600 6e 74 73 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  nts..*/..#ifndef
2d601 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2d602 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72  TUALTABLE./* For
2d603 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
2d604 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
2d605 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62  updateVirtualTab
2d606 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
2d607 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  rse,       /* Th
2d608 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
2d609 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2d60a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20 54  pSrc,       /* T
2d60b 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2d60c 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64 20   to be modified 
2d60d 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2d60e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2d60f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
2d610 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2d611 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65 20  hanges,  /* The 
2d612 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67  columns to chang
2d613 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45 20  e in the UPDATE 
2d614 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45  statement */.  E
2d615 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 2c  xpr *pRowidExpr,
2d616 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2d617 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d 70  n used to recomp
2d618 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a 2f  ute the rowid */
2d619 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20 20  .  int *aXRef,  
2d61a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69          /* Mappi
2d61b 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20  ng from columns 
2d61c 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72 69  of pTab to entri
2d61d 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 2a  es in pChanges *
2d61e 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2d61f 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52           /* WHER
2d620 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
2d621 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
2d622 20 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a   */.);.#endif /*
2d623 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2d624 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
2d625 0a 2a 2a 20 54 68 65 20 6d 6f 73 74 20 72 65 63  .** The most rec
2d626 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74  ently coded inst
2d627 72 75 63 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f  ruction was an O
2d628 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72  P_Column to retr
2d629 69 65 76 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68  ieve the.** i-th
2d62a 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65   column of table
2d62b 20 70 54 61 62 2e 20 54 68 69 73 20 72 6f 75 74   pTab. This rout
2d62c 69 6e 65 20 73 65 74 73 20 74 68 65 20 50 34 20  ine sets the P4 
2d62d 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65  parameter of the
2d62e 20 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74   .** OP_Column t
2d62f 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  o the default va
2d630 6c 75 65 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a  lue, if any..**.
2d631 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  ** The default v
2d632 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  alue of a column
2d633 20 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79   is specified by
2d634 20 61 20 44 45 46 41 55 4c 54 20 63 6c 61 75 73   a DEFAULT claus
2d635 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c  e in the .** col
2d636 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20  umn definition. 
2d637 54 68 69 73 20 77 61 73 20 65 69 74 68 65 72 20  This was either 
2d638 73 75 70 70 6c 69 65 64 20 62 79 20 74 68 65 20  supplied by the 
2d639 75 73 65 72 20 77 68 65 6e 20 74 68 65 20 74 61  user when the ta
2d63a 62 6c 65 0a 2a 2a 20 77 61 73 20 63 72 65 61 74  ble.** was creat
2d63b 65 64 2c 20 6f 72 20 61 64 64 65 64 20 6c 61 74  ed, or added lat
2d63c 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  er to the table 
2d63d 64 65 66 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e  definition by an
2d63e 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20   ALTER TABLE.** 
2d63f 63 6f 6d 6d 61 6e 64 2e 20 49 66 20 74 68 65 20  command. If the 
2d640 6c 61 74 74 65 72 2c 20 74 68 65 6e 20 74 68 65  latter, then the
2d641 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 69 6e 20   row-records in 
2d642 74 68 65 20 74 61 62 6c 65 20 62 74 72 65 65 20  the table btree 
2d643 6f 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e  on disk.** may n
2d644 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c  ot contain a val
2d645 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ue for the colum
2d646 6e 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c  n and the defaul
2d647 74 20 76 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a  t value, taken.*
2d648 2a 20 66 72 6f 6d 20 74 68 65 20 50 34 20 70 61  * from the P4 pa
2d649 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 4f  rameter of the O
2d64a 50 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63  P_Column instruc
2d64b 74 69 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e 65  tion, is returne
2d64c 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66  d instead..** If
2d64d 20 74 68 65 20 66 6f 72 6d 65 72 2c 20 74 68 65   the former, the
2d64e 6e 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64  n all row-record
2d64f 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64  s are guaranteed
2d650 20 74 6f 20 69 6e 63 6c 75 64 65 20 61 20 76 61   to include a va
2d651 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63  lue.** for the c
2d652 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 50 34  olumn and the P4
2d653 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 72 65   value is not re
2d654 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f  quired..**.** Co
2d655 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73  lumn definitions
2d656 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 41   created by an A
2d657 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61  LTER TABLE comma
2d658 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65  nd may only have
2d659 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 64 65 66   .** literal def
2d65a 61 75 6c 74 20 76 61 6c 75 65 73 20 73 70 65 63  ault values spec
2d65b 69 66 69 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c  ified: a number,
2d65c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e   null or a strin
2d65d 67 2e 20 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a  g. (If a more.**
2d65e 20 63 6f 6d 70 6c 69 63 61 74 65 64 20 64 65 66   complicated def
2d65f 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20  ault expression 
2d660 76 61 6c 75 65 20 77 61 73 20 70 72 6f 76 69 64  value was provid
2d661 65 64 2c 20 69 74 20 69 73 20 65 76 61 6c 75 61  ed, it is evalua
2d662 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65  ted .** when the
2d663 20 41 4c 54 45 52 20 54 41 42 4c 45 20 69 73 20   ALTER TABLE is 
2d664 65 78 65 63 75 74 65 64 20 61 6e 64 20 6f 6e 65  executed and one
2d665 20 6f 66 20 74 68 65 20 6c 69 74 65 72 61 6c 20   of the literal 
2d666 76 61 6c 75 65 73 20 77 72 69 74 74 65 6e 0a 2a  values written.*
2d667 2a 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  * into the sqlit
2d668 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 29  e_master table.)
2d669 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65  .**.** Therefore
2d66a 2c 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74  , the P4 paramet
2d66b 65 72 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69  er is only requi
2d66c 72 65 64 20 69 66 20 74 68 65 20 64 65 66 61 75  red if the defau
2d66d 6c 74 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20  lt value for.** 
2d66e 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20  the column is a 
2d66f 6c 69 74 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20  literal number, 
2d670 73 74 72 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20  string or null. 
2d671 54 68 65 20 73 71 6c 69 74 65 33 56 61 6c 75 65  The sqlite3Value
2d672 46 72 6f 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75  FromExpr().** fu
2d673 6e 63 74 69 6f 6e 20 69 73 20 63 61 70 61 62 6c  nction is capabl
2d674 65 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e  e of transformin
2d675 67 20 74 68 65 73 65 20 74 79 70 65 73 20 6f 66  g these types of
2d676 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 74   expressions int
2d677 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  o.** sqlite3_val
2d678 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a  ue objects..**.*
2d679 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69  * If parameter i
2d67a 52 65 67 20 69 73 20 6e 6f 74 20 6e 65 67 61 74  Reg is not negat
2d67b 69 76 65 2c 20 63 6f 64 65 20 61 6e 20 4f 50 5f  ive, code an OP_
2d67c 52 65 61 6c 41 66 66 69 6e 69 74 79 20 69 6e 73  RealAffinity ins
2d67d 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 72  truction.** on r
2d67e 65 67 69 73 74 65 72 20 69 52 65 67 2e 20 54 68  egister iReg. Th
2d67f 69 73 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  is is used when 
2d680 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e  an equivalent in
2d681 74 65 67 65 72 20 76 61 6c 75 65 20 69 73 20 0a  teger value is .
2d682 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 70 6c 61  ** stored in pla
2d683 63 65 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20  ce of an 8-byte 
2d684 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
2d685 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  alue in order to
2d686 20 73 61 76 65 20 0a 2a 2a 20 73 70 61 63 65 2e   save .** space.
2d687 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d688 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
2d689 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62  olumnDefault(Vdb
2d68a 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61  e *v, Table *pTa
2d68b 62 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69 52  b, int i, int iR
2d68c 65 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  eg){.  assert( p
2d68d 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  Tab!=0 );.  if( 
2d68e 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29  !pTab->pSelect )
2d68f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  {.    sqlite3_va
2d690 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20  lue *pValue;.   
2d691 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 73 71   u8 enc = ENC(sq
2d692 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 29 3b  lite3VdbeDb(v));
2d693 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  .    Column *pCo
2d694 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
2d695 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  i];.    VdbeComm
2d696 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 22 2c  ent((v, "%s.%s",
2d697 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43   pTab->zName, pC
2d698 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20  ol->zName));.   
2d699 20 61 73 73 65 72 74 28 20 69 3c 70 54 61 62 2d   assert( i<pTab-
2d69a 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c  >nCol );.    sql
2d69b 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
2d69c 72 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  r(sqlite3VdbeDb(
2d69d 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c  v), pCol->pDflt,
2d69e 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20 20 20   enc, .         
2d69f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d6a0 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20  pCol->affinity, 
2d6a1 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 66  &pValue);.    if
2d6a2 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20 20 20  ( pValue ){.    
2d6a3 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2d6a4 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f  ngeP4(v, -1, (co
2d6a5 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61 6c 75  nst char *)pValu
2d6a6 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20  e, P4_MEM);.    
2d6a7 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2d6a8 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
2d6a9 4f 49 4e 54 0a 20 20 20 20 69 66 28 20 69 52 65  OINT.    if( iRe
2d6aa 67 3e 3d 30 20 26 26 20 70 54 61 62 2d 3e 61 43  g>=0 && pTab->aC
2d6ab 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3d 3d  ol[i].affinity==
2d6ac 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
2d6ad 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d6ae 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2d6af 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 69  _RealAffinity, i
2d6b0 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Reg);.    }.#end
2d6b1 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  if.  }.}../*.** 
2d6b2 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44 41 54  Process an UPDAT
2d6b3 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  E statement..**.
2d6b4 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52 20 49  **   UPDATE OR I
2d6b5 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 79 7a  GNORE table_wxyz
2d6b6 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 57 48   SET a=b, c=d WH
2d6b7 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f  ERE e<5 AND f NO
2d6b8 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20  T NULL;.**      
2d6b9 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f      \_______/ \_
2d6ba 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f  _______/     \__
2d6bb 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f  ____/       \___
2d6bc 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
2d6bd 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45 72              onEr
2d6be 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74 20 20  ror   pTabList  
2d6bf 20 20 20 20 70 43 68 61 6e 67 65 73 20 20 20 20      pChanges    
2d6c0 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 0a           pWhere.
2d6c1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2d6c2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70  E void sqlite3Up
2d6c3 64 61 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70  date(.  Parse *p
2d6c4 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
2d6c5 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2d6c6 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2d6c7 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2d6c8 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
2d6c9 20 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c 64   which we should
2d6ca 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20 2a   change things *
2d6cb 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2d6cc 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54 68  hanges,    /* Th
2d6cd 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e 67  ings to be chang
2d6ce 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  ed */.  Expr *pW
2d6cf 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20 2f  here,          /
2d6d0 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2d6d1 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c  se.  May be null
2d6d2 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
2d6d3 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r            /* 
2d6d4 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f  How to handle co
2d6d5 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20  nstraint errors 
2d6d6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
2d6d7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2d6d8 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2d6d9 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2d6da 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2d6db 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75  he table to be u
2d6dc 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  pdated */.  int 
2d6dd 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20  addr = 0;       
2d6de 20 20 20 2f 2a 20 56 44 42 45 20 69 6e 73 74 72     /* VDBE instr
2d6df 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73 20 6f  uction address o
2d6e0 66 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  f the start of t
2d6e1 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65  he loop */.  Whe
2d6e2 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
2d6e3 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
2d6e4 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45  on about the WHE
2d6e5 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56  RE clause */.  V
2d6e6 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
2d6e7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2d6e8 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e  tual database en
2d6e9 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20  gine */.  Index 
2d6ea 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
2d6eb 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
2d6ec 6f 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a  over indices */.
2d6ed 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20 20    int nIdx;     
2d6ee 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2d6ef 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74 68  er of indices th
2d6f0 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e 67  at need updating
2d6f1 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20   */.  int iCur; 
2d6f2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d6f3 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62  VDBE Cursor numb
2d6f4 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20  er of pTab */.  
2d6f5 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
2d6f6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2d6f7 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
2d6f8 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
2d6f9 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  dx = 0;      /* 
2d6fa 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 73 73  One register ass
2d6fb 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 69 6e  igned to each in
2d6fc 64 65 78 20 74 6f 20 62 65 20 75 70 64 61 74 65  dex to be update
2d6fd 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65  d */.  int *aXRe
2d6fe 66 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  f = 0;        /*
2d6ff 20 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68 65   aXRef[i] is the
2d700 20 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e 67   index in pChang
2d701 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20  es->a[] of the. 
2d702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d703 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78          ** an ex
2d704 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68 65  pression for the
2d705 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20   i-th column of 
2d706 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 20  the table..     
2d707 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d708 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d      ** aXRef[i]=
2d709 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68 20  =-1 if the i-th 
2d70a 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68  column is not ch
2d70b 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20  anged. */.  int 
2d70c 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20 20 20  chngRowid;      
2d70d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2d70e 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
2d70f 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67 65 64  is being changed
2d710 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77   */.  Expr *pRow
2d711 69 64 45 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20  idExpr = 0;  /* 
2d712 45 78 70 72 65 73 73 69 6f 6e 20 64 65 66 69 6e  Expression defin
2d713 69 6e 67 20 74 68 65 20 6e 65 77 20 72 65 63 6f  ing the new reco
2d714 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  rd number */.  i
2d715 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20  nt openAll = 0; 
2d716 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2d717 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e 65 65   all indices nee
2d718 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a  d to be opened *
2d719 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  /.  AuthContext 
2d71a 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68  sContext;  /* Th
2d71b 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2d71c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d  context */.  Nam
2d71d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20  eContext sNC;   
2d71e 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d      /* The name-
2d71f 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c  context to resol
2d720 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ve expressions i
2d721 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
2d722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d723 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   Database contai
2d724 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 62  ning the table b
2d725 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f 0a  eing updated */.
2d726 20 20 69 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b    int okOnePass;
2d727 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2d728 20 66 6f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c   for one-pass al
2d729 67 6f 72 69 74 68 6d 20 77 69 74 68 6f 75 74 20  gorithm without 
2d72a 74 68 65 20 46 49 46 4f 20 2a 2f 0a 20 20 69 6e  the FIFO */.  in
2d72b 74 20 68 61 73 46 4b 3b 20 20 20 20 20 20 20 20  t hasFK;        
2d72c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2d72d 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63  foreign key proc
2d72e 65 73 73 69 6e 67 20 69 73 20 72 65 71 75 69 72  essing is requir
2d72f 65 64 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ed */..#ifndef S
2d730 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2d731 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b  ER.  int isView;
2d732 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2d733 72 75 65 20 77 68 65 6e 20 75 70 64 61 74 69 6e  rue when updatin
2d734 67 20 61 20 76 69 65 77 20 28 49 4e 53 54 45 41  g a view (INSTEA
2d735 44 20 4f 46 20 74 72 69 67 67 65 72 29 20 2a 2f  D OF trigger) */
2d736 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2d737 67 67 65 72 3b 20 20 20 20 20 2f 2a 20 4c 69 73  gger;     /* Lis
2d738 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e  t of triggers on
2d739 20 70 54 61 62 2c 20 69 66 20 72 65 71 75 69 72   pTab, if requir
2d73a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6d 61 73  ed */.  int tmas
2d73b 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
2d73c 2a 20 4d 61 73 6b 20 6f 66 20 54 52 49 47 47 45  * Mask of TRIGGE
2d73d 52 5f 42 45 46 4f 52 45 7c 54 52 49 47 47 45 52  R_BEFORE|TRIGGER
2d73e 5f 41 46 54 45 52 20 2a 2f 0a 23 65 6e 64 69 66  _AFTER */.#endif
2d73f 0a 20 20 69 6e 74 20 6e 65 77 6d 61 73 6b 3b 20  .  int newmask; 
2d740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
2d741 6b 20 6f 66 20 4e 45 57 2e 2a 20 63 6f 6c 75 6d  k of NEW.* colum
2d742 6e 73 20 61 63 63 65 73 73 65 64 20 62 79 20 42  ns accessed by B
2d743 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 2a  EFORE triggers *
2d744 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72  /..  /* Register
2d745 20 41 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   Allocations */.
2d746 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e    int regRowCoun
2d747 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 20 63 6f  t = 0;   /* A co
2d748 75 6e 74 20 6f 66 20 72 6f 77 73 20 63 68 61 6e  unt of rows chan
2d749 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ged */.  int reg
2d74a 4f 6c 64 52 6f 77 69 64 3b 20 20 20 20 20 20 20  OldRowid;       
2d74b 2f 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69 64  /* The old rowid
2d74c 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77   */.  int regNew
2d74d 52 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20  Rowid;       /* 
2d74e 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f  The new rowid */
2d74f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 3b 0a 20  .  int regNew;. 
2d750 20 69 6e 74 20 72 65 67 4f 6c 64 20 3d 20 30 3b   int regOld = 0;
2d751 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74  .  int regRowSet
2d752 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77   = 0;     /* Row
2d753 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62  set of rows to b
2d754 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69  e updated */.  i
2d755 6e 74 20 72 65 67 52 65 63 3b 20 20 20 20 20 20  nt regRec;      
2d756 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2d757 72 20 75 73 65 64 20 66 6f 72 20 6e 65 77 20 74  r used for new t
2d758 61 62 6c 65 20 72 65 63 6f 72 64 20 74 6f 20 69  able record to i
2d759 6e 73 65 72 74 20 2a 2f 0a 0a 20 20 6d 65 6d 73  nsert */..  mems
2d75a 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c  et(&sContext, 0,
2d75b 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74   sizeof(sContext
2d75c 29 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73  ));.  db = pPars
2d75d 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61  e->db;.  if( pPa
2d75e 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
2d75f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2d760 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
2d761 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
2d762 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
2d763 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20  ->nSrc==1 );..  
2d764 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
2d765 62 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e  ble which we wan
2d766 74 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20 20  t to update. .  
2d767 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  */.  pTab = sqli
2d768 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
2d769 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
2d76a 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  t);.  if( pTab==
2d76b 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
2d76c 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d  cleanup;.  iDb =
2d76d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2d76e 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2d76f 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2d770 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  ;..  /* Figure o
2d771 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61 6e  ut if we have an
2d772 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 69  y triggers and i
2d773 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
2d774 67 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20 69  g.  ** updated i
2d775 73 20 61 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 23  s a view..  */.#
2d776 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d777 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72  IT_TRIGGER.  pTr
2d778 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54  igger = sqlite3T
2d779 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61  riggersExist(pPa
2d77a 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 55 50  rse, pTab, TK_UP
2d77b 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20  DATE, pChanges, 
2d77c 26 74 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69 65  &tmask);.  isVie
2d77d 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  w = pTab->pSelec
2d77e 74 21 3d 30 3b 0a 20 20 61 73 73 65 72 74 28 20  t!=0;.  assert( 
2d77f 70 54 72 69 67 67 65 72 20 7c 7c 20 74 6d 61 73  pTrigger || tmas
2d780 6b 3d 3d 30 20 29 3b 0a 23 65 6c 73 65 0a 23 20  k==0 );.#else.# 
2d781 64 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 20  define pTrigger 
2d782 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  0.# define isVie
2d783 77 20 30 0a 23 20 64 65 66 69 6e 65 20 74 6d 61  w 0.# define tma
2d784 73 6b 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64  sk 0.#endif.#ifd
2d785 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2d786 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69  IEW.# undef isVi
2d787 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69  ew.# define isVi
2d788 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69  ew 0.#endif..  i
2d789 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
2d78a 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
2d78b 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
2d78c 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2d78d 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
2d78e 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
2d78f 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ly(pParse, pTab,
2d790 20 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20 67   tmask) ){.    g
2d791 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d792 75 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66 20  up;.  }.  aXRef 
2d793 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2d794 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2d795 69 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f  int) * pTab->nCo
2d796 6c 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66  l );.  if( aXRef
2d797 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74  ==0 ) goto updat
2d798 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72  e_cleanup;.  for
2d799 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d79a 6f 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b 69  ol; i++) aXRef[i
2d79b 5d 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 41 6c  ] = -1;..  /* Al
2d79c 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f 72 73  locate a cursors
2d79d 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
2d79e 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 6e 64  tabase table and
2d79f 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73   for all indices
2d7a0 2e 0a 20 20 2a 2a 20 54 68 65 20 69 6e 64 65 78  ..  ** The index
2d7a1 20 63 75 72 73 6f 72 73 20 6d 69 67 68 74 20 6e   cursors might n
2d7a2 6f 74 20 62 65 20 75 73 65 64 2c 20 62 75 74 20  ot be used, but 
2d7a3 69 66 20 74 68 65 79 20 61 72 65 20 75 73 65 64  if they are used
2d7a4 20 74 68 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20   they.  ** need 
2d7a5 74 6f 20 6f 63 63 75 72 20 72 69 67 68 74 20 61  to occur right a
2d7a6 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
2d7a7 65 20 63 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f  e cursor.  So go
2d7a8 20 61 68 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20   ahead and.  ** 
2d7a9 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 20  allocate enough 
2d7aa 73 70 61 63 65 2c 20 6a 75 73 74 20 69 6e 20 63  space, just in c
2d7ab 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ase..  */.  pTab
2d7ac 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73  List->a[0].iCurs
2d7ad 6f 72 20 3d 20 69 43 75 72 20 3d 20 70 50 61 72  or = iCur = pPar
2d7ae 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
2d7af 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2d7b0 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2d7b1 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2d7b2 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b    pParse->nTab++
2d7b3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74  ;.  }..  /* Init
2d7b4 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 2d  ialize the name-
2d7b5 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 6d 65 6d  context */.  mem
2d7b6 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2d7b7 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
2d7b8 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
2d7b9 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
2d7ba 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 0a 20 20   = pTabList;..  
2d7bb 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63  /* Resolve the c
2d7bc 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61  olumn names in a
2d7bd 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ll the expressio
2d7be 6e 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f  ns of the.  ** o
2d7bf 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
2d7c0 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69  tement.  Also fi
2d7c1 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  nd the column in
2d7c2 64 65 78 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63  dex.  ** for eac
2d7c3 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75  h column to be u
2d7c4 70 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 43  pdated in the pC
2d7c5 68 61 6e 67 65 73 20 61 72 72 61 79 2e 20 20 46  hanges array.  F
2d7c6 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 63 6f 6c  or each.  ** col
2d7c7 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65  umn to be update
2d7c8 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  d, make sure we 
2d7c9 68 61 76 65 20 61 75 74 68 6f 72 69 7a 61 74 69  have authorizati
2d7ca 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0a 20 20 2a  on to change.  *
2d7cb 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20  * that column.. 
2d7cc 20 2a 2f 0a 20 20 63 68 6e 67 52 6f 77 69 64 20   */.  chngRowid 
2d7cd 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
2d7ce 69 3c 70 43 68 61 6e 67 65 73 2d 3e 6e 45 78 70  i<pChanges->nExp
2d7cf 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  r; i++){.    if(
2d7d0 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2d7d1 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
2d7d2 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45  Changes->a[i].pE
2d7d3 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  xpr) ){.      go
2d7d4 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2d7d5 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
2d7d6 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
2d7d7 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
2d7d8 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2d7d9 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  mp(pTab->aCol[j]
2d7da 2e 7a 4e 61 6d 65 2c 20 70 43 68 61 6e 67 65 73  .zName, pChanges
2d7db 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  ->a[i].zName)==0
2d7dc 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
2d7dd 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  j==pTab->iPKey )
2d7de 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 6e 67  {.          chng
2d7df 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
2d7e0 20 20 20 20 20 70 52 6f 77 69 64 45 78 70 72 20       pRowidExpr 
2d7e1 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  = pChanges->a[i]
2d7e2 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
2d7e3 7d 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b  }.        aXRef[
2d7e4 6a 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  j] = i;.        
2d7e5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2d7e6 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
2d7e7 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
2d7e8 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
2d7e9 73 52 6f 77 69 64 28 70 43 68 61 6e 67 65 73 2d  sRowid(pChanges-
2d7ea 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a  >a[i].zName) ){.
2d7eb 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69          chngRowi
2d7ec 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  d = 1;.        p
2d7ed 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61  RowidExpr = pCha
2d7ee 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  nges->a[i].pExpr
2d7ef 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2d7f0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2d7f1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2d7f2 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20  no such column: 
2d7f3 25 73 22 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  %s", pChanges->a
2d7f4 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
2d7f5 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d7f6 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
2d7f7 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2d7f8 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2d7f9 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20  RIZATION.    {. 
2d7fa 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
2d7fb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
2d7fc 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2d7fd 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20   SQLITE_UPDATE, 
2d7fe 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
2d7ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d800 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2d801 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d  ol[j].zName, db-
2d802 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2d803 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
2d804 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
2d805 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61         goto upda
2d806 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
2d807 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
2d808 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b  SQLITE_IGNORE ){
2d809 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a  .        aXRef[j
2d80a 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  ] = -1;.      }.
2d80b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2d80c 0a 0a 20 20 68 61 73 46 4b 20 3d 20 73 71 6c 69  ..  hasFK = sqli
2d80d 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70 50  te3FkRequired(pP
2d80e 61 72 73 65 2c 20 70 54 61 62 2c 20 61 58 52 65  arse, pTab, aXRe
2d80f 66 2c 20 63 68 6e 67 52 6f 77 69 64 29 3b 0a 0a  f, chngRowid);..
2d810 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65    /* Allocate me
2d811 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 61 72 72  mory for the arr
2d812 61 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 54  ay aRegIdx[].  T
2d813 68 65 72 65 20 69 73 20 6f 6e 65 20 65 6e 74 72  here is one entr
2d814 79 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 61 72  y in the.  ** ar
2d815 72 61 79 20 66 6f 72 20 65 61 63 68 20 69 6e 64  ray for each ind
2d816 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
2d817 74 68 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75  th table being u
2d818 70 64 61 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e  pdated.  Fill in
2d819 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
2d81a 77 69 74 68 20 61 20 72 65 67 69 73 74 65 72 20  with a register 
2d81b 6e 75 6d 62 65 72 20 66 6f 72 20 69 6e 64 69 63  number for indic
2d81c 65 73 20 74 68 61 74 20 61 72 65 20 74 6f 20 62  es that are to b
2d81d 65 20 75 73 65 64 0a 20 20 2a 2a 20 61 6e 64 20  e used.  ** and 
2d81e 77 69 74 68 20 7a 65 72 6f 20 66 6f 72 20 75 6e  with zero for un
2d81f 75 73 65 64 20 69 6e 64 69 63 65 73 2e 0a 20 20  used indices..  
2d820 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c  */.  for(nIdx=0,
2d821 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2d822 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2d823 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78  Idx->pNext, nIdx
2d824 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 49 64 78  ++){}.  if( nIdx
2d825 3e 30 20 29 7b 0a 20 20 20 20 61 52 65 67 49 64  >0 ){.    aRegId
2d826 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
2d827 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2d828 66 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78  f(Index*) * nIdx
2d829 20 29 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67   );.    if( aReg
2d82a 49 64 78 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  Idx==0 ) goto up
2d82b 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d82c 7d 0a 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64  }.  for(j=0, pId
2d82d 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d82e 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d82f 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
2d830 20 20 69 6e 74 20 72 65 67 3b 0a 20 20 20 20 69    int reg;.    i
2d831 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a  f( chngRowid ){.
2d832 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70 50        reg = ++pP
2d833 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2d834 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 67  }else{.      reg
2d835 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 0;.      for(
2d836 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  i=0; i<pIdx->nCo
2d837 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
2d838 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 70 49      if( aXRef[pI
2d839 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d  dx->aiColumn[i]]
2d83a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
2d83b 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d   reg = ++pParse-
2d83c 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20  >nMem;.         
2d83d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2d83e 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
2d83f 20 20 20 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d      aRegIdx[j] =
2d840 20 72 65 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   reg;.  }..  /* 
2d841 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
2d842 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 76 20 3d 20   code. */.  v = 
2d843 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d844 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
2d845 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
2d846 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20  _cleanup;.  if( 
2d847 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
2d848 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43  0 ) sqlite3VdbeC
2d849 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a  ountChanges(v);.
2d84a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
2d84b 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
2d84c 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23  rse, 1, iDb);..#
2d84d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d84e 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2d84f 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
2d850 6c 65 73 20 6d 75 73 74 20 62 65 20 68 61 6e 64  les must be hand
2d851 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 20 2a  led separately *
2d852 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
2d853 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 75  l(pTab) ){.    u
2d854 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c  pdateVirtualTabl
2d855 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  e(pParse, pTabLi
2d856 73 74 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67  st, pTab, pChang
2d857 65 73 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20  es, pRowidExpr, 
2d858 61 58 52 65 66 2c 0a 20 20 20 20 20 20 20 20 20  aXRef,.         
2d859 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
2d85a 68 65 72 65 29 3b 0a 20 20 20 20 70 57 68 65 72  here);.    pWher
2d85b 65 20 3d 20 30 3b 0a 20 20 20 20 70 54 61 62 4c  e = 0;.    pTabL
2d85c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74  ist = 0;.    got
2d85d 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2d85e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2d85f 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 71 75  /* Allocate requ
2d860 69 72 65 64 20 72 65 67 69 73 74 65 72 73 2e 20  ired registers. 
2d861 2a 2f 0a 20 20 72 65 67 4f 6c 64 52 6f 77 69 64  */.  regOldRowid
2d862 20 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d   = regNewRowid =
2d863 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2d864 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
2d865 7c 7c 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20  || hasFK ){.    
2d866 72 65 67 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d  regOld = pParse-
2d867 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 70  >nMem + 1;.    p
2d868 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
2d869 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20  Tab->nCol;.  }. 
2d86a 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 7c   if( chngRowid |
2d86b 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  | pTrigger || ha
2d86c 73 46 4b 20 29 7b 0a 20 20 20 20 72 65 67 4e 65  sFK ){.    regNe
2d86d 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  wRowid = ++pPars
2d86e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  e->nMem;.  }.  r
2d86f 65 67 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e  egNew = pParse->
2d870 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 70 50 61 72  nMem + 1;.  pPar
2d871 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2d872 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 52 65 63  ->nCol;.  regRec
2d873 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2d874 6d 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74  m;..  /* Start t
2d875 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74 2e  he view context.
2d876 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
2d877 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   ){.    sqlite3A
2d878 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70  uthContextPush(p
2d879 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74  Parse, &sContext
2d87a 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2d87b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
2d87c 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 75 70  are trying to up
2d87d 64 61 74 65 20 61 20 76 69 65 77 2c 20 72 65 61  date a view, rea
2d87e 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69  lize that view i
2d87f 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d  nto.  ** a ephem
2d880 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  eral table..  */
2d881 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2d882 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
2d883 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
2d884 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
2d885 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
2d886 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65  .    sqlite3Mate
2d887 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72  rializeView(pPar
2d888 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65  se, pTab, pWhere
2d889 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e  , iCur);.  }.#en
2d88a 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  dif..  /* Resolv
2d88b 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
2d88c 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78  es in all the ex
2d88d 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
2d88e 0a 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75  .  ** WHERE clau
2d88f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  se..  */.  if( s
2d890 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2d891 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68  rNames(&sNC, pWh
2d892 65 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ere) ){.    goto
2d893 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d894 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  .  }..  /* Begin
2d895 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2d896 61 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  an.  */.  sqlite
2d897 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d898 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c  P_Null, 0, regOl
2d899 64 52 6f 77 69 64 29 3b 0a 20 20 70 57 49 6e 66  dRowid);.  pWInf
2d89a 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
2d89b 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
2d89c 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 30  abList, pWhere,0
2d89d 2c 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f  , WHERE_ONEPASS_
2d89e 44 45 53 49 52 45 44 29 3b 0a 20 20 69 66 28 20  DESIRED);.  if( 
2d89f 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f  pWInfo==0 ) goto
2d8a0 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d8a1 0a 20 20 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 70  .  okOnePass = p
2d8a2 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
2d8a3 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
2d8a4 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76   the rowid of ev
2d8a5 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65 20 75  ery item to be u
2d8a6 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73  pdated..  */.  s
2d8a7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d8a8 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
2d8a9 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29  ur, regOldRowid)
2d8aa 3b 0a 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61  ;.  if( !okOnePa
2d8ab 73 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77  ss ){.    regRow
2d8ac 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Set = ++pParse->
2d8ad 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2d8ae 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d8af 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65 67  P_RowSetAdd, reg
2d8b0 52 6f 77 53 65 74 2c 20 72 65 67 4f 6c 64 52 6f  RowSet, regOldRo
2d8b1 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  wid);.  }..  /* 
2d8b2 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  End the database
2d8b3 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f   scan loop..  */
2d8b4 0a 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45  .  sqlite3WhereE
2d8b5 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 2f  nd(pWInfo);..  /
2d8b6 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2d8b7 20 63 6f 75 6e 74 20 6f 66 20 75 70 64 61 74 65   count of update
2d8b8 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20 20 69 66  d rows.  */.  if
2d8b9 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  ( (db->flags & S
2d8ba 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
2d8bb 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72   && !pParse->pTr
2d8bc 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20  iggerTab ){.    
2d8bd 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b  regRowCount = ++
2d8be 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2d8bf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d8c0 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2d8c1 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e  r, 0, regRowCoun
2d8c2 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  t);.  }..  if( !
2d8c3 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a  isView ){.    /*
2d8c4 20 0a 20 20 20 20 2a 2a 20 4f 70 65 6e 20 65 76   .    ** Open ev
2d8c5 65 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 6e  ery index that n
2d8c6 65 65 64 73 20 75 70 64 61 74 69 6e 67 2e 20 20  eeds updating.  
2d8c7 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 79  Note that if any
2d8c8 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 63 6f  .    ** index co
2d8c9 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  uld potentially 
2d8ca 69 6e 76 6f 6b 65 20 61 20 52 45 50 4c 41 43 45  invoke a REPLACE
2d8cb 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
2d8cc 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 61 63 74  tion .    ** act
2d8cd 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20 6e 65 65  ion, then we nee
2d8ce 64 20 74 6f 20 6f 70 65 6e 20 61 6c 6c 20 69 6e  d to open all in
2d8cf 64 69 63 65 73 20 62 65 63 61 75 73 65 20 77 65  dices because we
2d8d0 20 6d 69 67 68 74 20 6e 65 65 64 0a 20 20 20 20   might need.    
2d8d1 2a 2a 20 74 6f 20 62 65 20 64 65 6c 65 74 69 6e  ** to be deletin
2d8d2 67 20 73 6f 6d 65 20 72 65 63 6f 72 64 73 2e 0a  g some records..
2d8d3 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
2d8d4 6f 6b 4f 6e 65 50 61 73 73 20 29 20 73 71 6c 69  okOnePass ) sqli
2d8d5 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
2d8d6 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20  rse, iCur, iDb, 
2d8d7 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  pTab, OP_OpenWri
2d8d8 74 65 29 3b 20 0a 20 20 20 20 69 66 28 20 6f 6e  te); .    if( on
2d8d9 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2d8da 65 20 29 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41  e ){.      openA
2d8db 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ll = 1;.    }els
2d8dc 65 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c  e{.      openAll
2d8dd 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 0;.      for(
2d8de 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
2d8df 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2d8e0 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
2d8e1 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e      if( pIdx->on
2d8e2 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2d8e3 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  e ){.          o
2d8e4 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  penAll = 1;.    
2d8e5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d8e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2d8e7 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
2d8e8 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
2d8e9 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2d8ea 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
2d8eb 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65  ){.      if( ope
2d8ec 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b  nAll || aRegIdx[
2d8ed 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i]>0 ){.        
2d8ee 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
2d8ef 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
2d8f0 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
2d8f1 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2d8f2 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2d8f3 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 43  OP_OpenWrite, iC
2d8f4 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e  ur+i+1, pIdx->tn
2d8f5 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
2d8f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d8f7 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
2d8f8 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2d8f9 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2d8fa 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3e 69  ( pParse->nTab>i
2d8fb 43 75 72 2b 69 2b 31 20 29 3b 0a 20 20 20 20 20  Cur+i+1 );.     
2d8fc 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2d8fd 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 75 70  /* Top of the up
2d8fe 64 61 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  date loop */.  i
2d8ff 66 28 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a  f( okOnePass ){.
2d900 20 20 20 20 69 6e 74 20 61 31 20 3d 20 73 71 6c      int a1 = sql
2d901 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2d902 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
2d903 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  gOldRowid);.    
2d904 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2d905 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
2d906 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oto);.    sqlite
2d907 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2d908 20 61 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   a1);.  }else{. 
2d909 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2d90a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2d90b 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 72 65  P_RowSetRead, re
2d90c 67 52 6f 77 53 65 74 2c 20 30 2c 20 72 65 67 4f  gRowSet, 0, regO
2d90d 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20  ldRowid);.  }.. 
2d90e 20 2f 2a 20 4d 61 6b 65 20 63 75 72 73 6f 72 20   /* Make cursor 
2d90f 69 43 75 72 20 70 6f 69 6e 74 20 74 6f 20 74 68  iCur point to th
2d910 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 69 73  e record that is
2d911 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20   being updated. 
2d912 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 72 65 63  If.  ** this rec
2d913 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ord does not exi
2d914 73 74 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73  st for some reas
2d915 6f 6e 20 28 64 65 6c 65 74 65 64 20 62 79 20 61  on (deleted by a
2d916 20 74 72 69 67 67 65 72 2c 0a 20 20 2a 2a 20 66   trigger,.  ** f
2d917 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 6e  or example, then
2d918 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78   jump to the nex
2d919 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
2d91a 68 65 20 52 6f 77 53 65 74 20 6c 6f 6f 70 2e 20  he RowSet loop. 
2d91b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2d91c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2d91d 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
2d91e 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
2d91f 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
2d920 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
2d921 6c 6c 20 63 68 61 6e 67 65 2c 20 73 65 74 20 72  ll change, set r
2d922 65 67 69 73 74 65 72 20 72 65 67 4e 65 77 52 6f  egister regNewRo
2d923 77 69 64 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74  wid to.  ** cont
2d924 61 69 6e 20 74 68 65 20 6e 65 77 20 76 61 6c 75  ain the new valu
2d925 65 2e 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  e. If the record
2d926 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 62   number is not b
2d927 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2c 0a 20  eing modified,. 
2d928 20 2a 2a 20 74 68 65 6e 20 72 65 67 4e 65 77 52   ** then regNewR
2d929 6f 77 69 64 20 69 73 20 74 68 65 20 73 61 6d 65  owid is the same
2d92a 20 72 65 67 69 73 74 65 72 20 61 73 20 72 65 67   register as reg
2d92b 4f 6c 64 52 6f 77 69 64 2c 20 77 68 69 63 68 20  OldRowid, which 
2d92c 69 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  is.  ** already 
2d92d 70 6f 70 75 6c 61 74 65 64 2e 20 20 2a 2f 0a 20  populated.  */. 
2d92e 20 61 73 73 65 72 74 28 20 63 68 6e 67 52 6f 77   assert( chngRow
2d92f 69 64 20 7c 7c 20 70 54 72 69 67 67 65 72 20 7c  id || pTrigger |
2d930 7c 20 68 61 73 46 4b 20 7c 7c 20 72 65 67 4f 6c  | hasFK || regOl
2d931 64 52 6f 77 69 64 3d 3d 72 65 67 4e 65 77 52 6f  dRowid==regNewRo
2d932 77 69 64 20 29 3b 0a 20 20 69 66 28 20 63 68 6e  wid );.  if( chn
2d933 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 73 71  gRowid ){.    sq
2d934 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2d935 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72  arse, pRowidExpr
2d936 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
2d937 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d938 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
2d939 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77  BeInt, regNewRow
2d93a 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  id);.  }..  /* I
2d93b 66 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67  f there are trig
2d93c 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62  gers on this tab
2d93d 6c 65 2c 20 70 6f 70 75 6c 61 74 65 20 61 6e 20  le, populate an 
2d93e 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2d93f 72 73 20 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  rs .  ** with th
2d940 65 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a  e required old.*
2d941 20 63 6f 6c 75 6d 6e 20 64 61 74 61 2e 20 20 2a   column data.  *
2d942 2f 0a 20 20 69 66 28 20 68 61 73 46 4b 20 7c 7c  /.  if( hasFK ||
2d943 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2d944 20 75 33 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 28   u32 oldmask = (
2d945 68 61 73 46 4b 20 3f 20 73 71 6c 69 74 65 33 46  hasFK ? sqlite3F
2d946 6b 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c  kOldmask(pParse,
2d947 20 70 54 61 62 29 20 3a 20 30 29 3b 0a 20 20 20   pTab) : 0);.   
2d948 20 6f 6c 64 6d 61 73 6b 20 7c 3d 20 73 71 6c 69   oldmask |= sqli
2d949 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73  te3TriggerColmas
2d94a 6b 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  k(pParse, .     
2d94b 20 20 20 70 54 72 69 67 67 65 72 2c 20 70 43 68     pTrigger, pCh
2d94c 61 6e 67 65 73 2c 20 30 2c 20 54 52 49 47 47 45  anges, 0, TRIGGE
2d94d 52 5f 42 45 46 4f 52 45 7c 54 52 49 47 47 45 52  R_BEFORE|TRIGGER
2d94e 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 6f 6e  _AFTER, pTab, on
2d94f 45 72 72 6f 72 0a 20 20 20 20 29 3b 0a 20 20 20  Error.    );.   
2d950 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
2d951 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
2d952 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 69 5d      if( aXRef[i]
2d953 3c 30 20 7c 7c 20 6f 6c 64 6d 61 73 6b 3d 3d 30  <0 || oldmask==0
2d954 78 66 66 66 66 66 66 66 66 20 7c 7c 20 28 6f 6c  xffffffff || (ol
2d955 64 6d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 20  dmask & (1<<i)) 
2d956 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2d957 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2d958 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c  OP_Column, iCur,
2d959 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20   i, regOld+i);. 
2d95a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2d95b 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
2d95c 54 61 62 2c 20 69 2c 20 72 65 67 4f 6c 64 2b 69  Tab, i, regOld+i
2d95d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2d95e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2d95f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d960 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 2b  Null, 0, regOld+
2d961 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i);.      }.    
2d962 7d 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f  }.    if( chngRo
2d963 77 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  wid==0 ){.      
2d964 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d965 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65  2(v, OP_Copy, re
2d966 67 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 4e 65  gOldRowid, regNe
2d967 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20  wRowid);.    }. 
2d968 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74   }..  /* Populat
2d969 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 72  e the array of r
2d96a 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
2d96b 6e 67 20 61 74 20 72 65 67 4e 65 77 20 77 69 74  ng at regNew wit
2d96c 68 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 72  h the new.  ** r
2d96d 6f 77 20 64 61 74 61 2e 20 54 68 69 73 20 61 72  ow data. This ar
2d96e 72 61 79 20 69 73 20 75 73 65 64 20 74 6f 20 63  ray is used to c
2d96f 68 65 63 6b 20 63 6f 6e 73 74 61 69 6e 74 73 2c  heck constaints,
2d970 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 0a   create the new.
2d971 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69    ** table and i
2d972 6e 64 65 78 20 72 65 63 6f 72 64 73 2c 20 61 6e  ndex records, an
2d973 64 20 61 73 20 74 68 65 20 76 61 6c 75 65 73 20  d as the values 
2d974 66 6f 72 20 61 6e 79 20 6e 65 77 2e 2a 20 72 65  for any new.* re
2d975 66 65 72 65 6e 63 65 73 0a 20 20 2a 2a 20 6d 61  ferences.  ** ma
2d976 64 65 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a  de by triggers..
2d977 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
2d978 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f  re are one or mo
2d979 72 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65  re BEFORE trigge
2d97a 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  rs, then do not 
2d97b 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 20 20 2a  populate the.  *
2d97c 2a 20 72 65 67 69 73 74 65 72 73 20 61 73 73 6f  * registers asso
2d97d 63 69 61 74 65 64 20 77 69 74 68 20 63 6f 6c 75  ciated with colu
2d97e 6d 6e 73 20 74 68 61 74 20 61 72 65 20 28 61 29  mns that are (a)
2d97f 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79   not modified by
2d980 0a 20 20 2a 2a 20 74 68 69 73 20 55 50 44 41 54  .  ** this UPDAT
2d981 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  E statement and 
2d982 28 62 29 20 6e 6f 74 20 61 63 63 65 73 73 65 64  (b) not accessed
2d983 20 62 79 20 6e 65 77 2e 2a 20 72 65 66 65 72 65   by new.* refere
2d984 6e 63 65 73 2e 20 54 68 65 0a 20 20 2a 2a 20 76  nces. The.  ** v
2d985 61 6c 75 65 73 20 66 6f 72 20 72 65 67 69 73 74  alues for regist
2d986 65 72 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64  ers not modified
2d987 20 62 79 20 74 68 65 20 55 50 44 41 54 45 20 6d   by the UPDATE m
2d988 75 73 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20  ust be reloaded 
2d989 66 72 6f 6d 20 0a 20 20 2a 2a 20 74 68 65 20 64  from .  ** the d
2d98a 61 74 61 62 61 73 65 20 61 66 74 65 72 20 74 68  atabase after th
2d98b 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  e BEFORE trigger
2d98c 73 20 61 72 65 20 66 69 72 65 64 20 61 6e 79 77  s are fired anyw
2d98d 61 79 20 28 61 73 20 74 68 65 20 74 72 69 67 67  ay (as the trigg
2d98e 65 72 20 0a 20 20 2a 2a 20 6d 61 79 20 68 61 76  er .  ** may hav
2d98f 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65 6d 29  e modified them)
2d990 2e 20 53 6f 20 6e 6f 74 20 6c 6f 61 64 69 6e 67  . So not loading
2d991 20 74 68 6f 73 65 20 74 68 61 74 20 61 72 65 20   those that are 
2d992 6e 6f 74 20 67 6f 69 6e 67 20 74 6f 0a 20 20 2a  not going to.  *
2d993 2a 20 62 65 20 75 73 65 64 20 65 6c 69 6d 69 6e  * be used elimin
2d994 61 74 65 73 20 73 6f 6d 65 20 72 65 64 75 6e 64  ates some redund
2d995 61 6e 74 20 6f 70 63 6f 64 65 73 2e 0a 20 20 2a  ant opcodes..  *
2d996 2f 0a 20 20 6e 65 77 6d 61 73 6b 20 3d 20 73 71  /.  newmask = sq
2d997 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d  lite3TriggerColm
2d998 61 73 6b 28 0a 20 20 20 20 20 20 70 50 61 72 73  ask(.      pPars
2d999 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70 43 68  e, pTrigger, pCh
2d99a 61 6e 67 65 73 2c 20 31 2c 20 54 52 49 47 47 45  anges, 1, TRIGGE
2d99b 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20  R_BEFORE, pTab, 
2d99c 6f 6e 45 72 72 6f 72 0a 20 20 29 3b 0a 20 20 66  onError.  );.  f
2d99d 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
2d99e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2d99f 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
2d9a0 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ey ){.      sqli
2d9a1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d9a2 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2d9a3 4e 65 77 2b 69 29 3b 0a 20 20 20 20 7d 65 6c 73  New+i);.    }els
2d9a4 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20 61 58 52  e{.      j = aXR
2d9a5 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  ef[i];.      if(
2d9a6 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   j>=0 ){.       
2d9a7 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2d9a8 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65  (pParse, pChange
2d9a9 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72  s->a[j].pExpr, r
2d9aa 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20  egNew+i);.      
2d9ab 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 28 74 6d  }else if( 0==(tm
2d9ac 61 73 6b 26 54 52 49 47 47 45 52 5f 42 45 46 4f  ask&TRIGGER_BEFO
2d9ad 52 45 29 20 7c 7c 20 69 3e 33 31 20 7c 7c 20 28  RE) || i>31 || (
2d9ae 6e 65 77 6d 61 73 6b 26 28 31 3c 3c 69 29 29 20  newmask&(1<<i)) 
2d9af 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2d9b0 69 73 20 62 72 61 6e 63 68 20 6c 6f 61 64 73 20  is branch loads 
2d9b1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 63  the value of a c
2d9b2 6f 6c 75 6d 6e 20 74 68 61 74 20 77 69 6c 6c 20  olumn that will 
2d9b3 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 0a  not be changed .
2d9b4 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20          ** into 
2d9b5 61 20 72 65 67 69 73 74 65 72 2e 20 54 68 69 73  a register. This
2d9b6 20 69 73 20 64 6f 6e 65 20 69 66 20 74 68 65 72   is done if ther
2d9b7 65 20 61 72 65 20 6e 6f 20 42 45 46 4f 52 45 20  e are no BEFORE 
2d9b8 74 72 69 67 67 65 72 73 2c 20 6f 72 0a 20 20 20  triggers, or.   
2d9b9 20 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65       ** if there
2d9ba 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65   are one or more
2d9bb 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73   BEFORE triggers
2d9bc 20 74 68 61 74 20 75 73 65 20 74 68 69 73 20 76   that use this v
2d9bd 61 6c 75 65 20 76 69 61 0a 20 20 20 20 20 20 20  alue via.       
2d9be 20 2a 2a 20 61 20 6e 65 77 2e 2a 20 72 65 66 65   ** a new.* refe
2d9bf 72 65 6e 63 65 20 69 6e 20 61 20 74 72 69 67 67  rence in a trigg
2d9c0 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 20 20 20  er program..    
2d9c1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 74      */.        t
2d9c2 65 73 74 63 61 73 65 28 20 69 3d 3d 33 31 20 29  estcase( i==31 )
2d9c3 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
2d9c4 73 65 28 20 69 3d 3d 33 32 20 29 3b 0a 20 20 20  se( i==32 );.   
2d9c5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2d9c6 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
2d9c7 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65  umn, iCur, i, re
2d9c8 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 20  gNew+i);.       
2d9c9 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
2d9ca 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
2d9cb 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20  , regNew+i);.   
2d9cc 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2d9cd 20 20 2f 2a 20 46 69 72 65 20 61 6e 79 20 42 45    /* Fire any BE
2d9ce 46 4f 52 45 20 55 50 44 41 54 45 20 74 72 69 67  FORE UPDATE trig
2d9cf 67 65 72 73 2e 20 54 68 69 73 20 68 61 70 70 65  gers. This happe
2d9d0 6e 73 20 62 65 66 6f 72 65 20 63 6f 6e 73 74 72  ns before constr
2d9d1 61 69 6e 74 73 20 61 72 65 0a 20 20 2a 2a 20 76  aints are.  ** v
2d9d2 65 72 69 66 69 65 64 2e 20 4f 6e 65 20 63 6f 75  erified. One cou
2d9d3 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 74 68  ld argue that th
2d9d4 69 73 20 69 73 20 77 72 6f 6e 67 2e 0a 20 20 2a  is is wrong..  *
2d9d5 2f 0a 20 20 69 66 28 20 74 6d 61 73 6b 26 54 52  /.  if( tmask&TR
2d9d6 49 47 47 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a  IGGER_BEFORE ){.
2d9d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d9d8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69  ddOp2(v, OP_Affi
2d9d9 6e 69 74 79 2c 20 72 65 67 4e 65 77 2c 20 70 54  nity, regNew, pT
2d9da 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73  ab->nCol);.    s
2d9db 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
2d9dc 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b  ityStr(v, pTab);
2d9dd 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
2d9de 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
2d9df 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
2d9e0 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73  UPDATE, pChanges
2d9e1 2c 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47  , .        TRIGG
2d9e2 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
2d9e3 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e   regOldRowid, on
2d9e4 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20  Error, addr);.. 
2d9e5 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 74 72     /* The row-tr
2d9e6 69 67 67 65 72 20 6d 61 79 20 68 61 76 65 20 64  igger may have d
2d9e7 65 6c 65 74 65 64 20 74 68 65 20 72 6f 77 20 62  eleted the row b
2d9e8 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 49 6e  eing updated. In
2d9e9 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 61 73   this.    ** cas
2d9ea 65 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e  e, jump to the n
2d9eb 65 78 74 20 72 6f 77 2e 20 4e 6f 20 75 70 64 61  ext row. No upda
2d9ec 74 65 73 20 6f 72 20 41 46 54 45 52 20 74 72 69  tes or AFTER tri
2d9ed 67 67 65 72 73 20 61 72 65 20 0a 20 20 20 20 2a  ggers are .    *
2d9ee 2a 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73  * required. This
2d9ef 20 62 65 68 61 76 69 6f 75 72 20 2d 20 77 68 61   behaviour - wha
2d9f0 74 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74  t happens when t
2d9f1 68 65 20 72 6f 77 20 62 65 69 6e 67 20 75 70 64  he row being upd
2d9f2 61 74 65 64 0a 20 20 20 20 2a 2a 20 69 73 20 64  ated.    ** is d
2d9f3 65 6c 65 74 65 64 20 6f 72 20 72 65 6e 61 6d 65  eleted or rename
2d9f4 64 20 62 79 20 61 20 42 45 46 4f 52 45 20 74 72  d by a BEFORE tr
2d9f5 69 67 67 65 72 20 2d 20 69 73 20 6c 65 66 74 20  igger - is left 
2d9f6 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65  undefined in the
2d9f7 0a 20 20 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  .    ** document
2d9f8 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
2d9f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d9fa 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2d9fb 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c  sts, iCur, addr,
2d9fc 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a   regOldRowid);..
2d9fd 20 20 20 20 2f 2a 20 49 66 20 69 74 20 64 69 64      /* If it did
2d9fe 20 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 20   not delete it, 
2d9ff 74 68 65 20 72 6f 77 2d 74 72 69 67 67 65 72 20  the row-trigger 
2da00 6d 61 79 20 73 74 69 6c 6c 20 68 61 76 65 20 6d  may still have m
2da01 6f 64 69 66 69 65 64 20 0a 20 20 20 20 2a 2a 20  odified .    ** 
2da02 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  some of the colu
2da03 6d 6e 73 20 6f 66 20 74 68 65 20 72 6f 77 20 62  mns of the row b
2da04 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 4c 6f  eing updated. Lo
2da05 61 64 20 74 68 65 20 76 61 6c 75 65 73 20 66 6f  ad the values fo
2da06 72 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 63 6f  r .    ** all co
2da07 6c 75 6d 6e 73 20 6e 6f 74 20 6d 6f 64 69 66 69  lumns not modifi
2da08 65 64 20 62 79 20 74 68 65 20 75 70 64 61 74 65  ed by the update
2da09 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 20   statement into 
2da0a 74 68 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65  their .    ** re
2da0b 67 69 73 74 65 72 73 20 69 6e 20 63 61 73 65 20  gisters in case 
2da0c 74 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65  this has happene
2da0d 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  d..    */.    fo
2da0e 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
2da0f 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2da10 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3c 30 20   if( aXRef[i]<0 
2da11 26 26 20 69 21 3d 70 54 61 62 2d 3e 69 50 4b 65  && i!=pTab->iPKe
2da12 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
2da13 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2da14 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75  , OP_Column, iCu
2da15 72 2c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29 3b  r, i, regNew+i);
2da16 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2da17 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c  ColumnDefault(v,
2da18 20 70 54 61 62 2c 20 69 2c 20 72 65 67 4e 65 77   pTab, i, regNew
2da19 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
2da1a 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69   }.  }..  if( !i
2da1b 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74  sView ){.    int
2da1c 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   j1;            
2da1d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
2da1e 64 72 65 73 73 20 6f 66 20 6a 75 6d 70 20 69 6e  dress of jump in
2da1f 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  struction */..  
2da20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69    /* Do constrai
2da21 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20  nt checks. */.  
2da22 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
2da23 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b  eConstraintCheck
2da24 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
2da25 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77 69  iCur, regNewRowi
2da26 64 2c 0a 20 20 20 20 20 20 20 20 61 52 65 67 49  d,.        aRegI
2da27 64 78 2c 20 28 63 68 6e 67 52 6f 77 69 64 3f 72  dx, (chngRowid?r
2da28 65 67 4f 6c 64 52 6f 77 69 64 3a 30 29 2c 20 31  egOldRowid:0), 1
2da29 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 2c  , onError, addr,
2da2a 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20   0);..    /* Do 
2da2b 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  FK constraint ch
2da2c 65 63 6b 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ecks. */.    if(
2da2d 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 20 20   hasFK ){.      
2da2e 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 70  sqlite3FkCheck(p
2da2f 50 61 72 73 65 2c 20 70 54 61 62 2c 20 72 65 67  Parse, pTab, reg
2da30 4f 6c 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20  OldRowid, 0);.  
2da31 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
2da32 74 65 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  te the index ent
2da33 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ries associated 
2da34 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
2da35 20 72 65 63 6f 72 64 2e 20 20 2a 2f 0a 20 20 20   record.  */.   
2da36 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2da37 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2da38 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 30  tExists, iCur, 0
2da39 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
2da3a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
2da3b 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
2da3c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2da3d 69 43 75 72 2c 20 61 52 65 67 49 64 78 29 3b 0a  iCur, aRegIdx);.
2da3e 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 63 68 61    .    /* If cha
2da3f 6e 67 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  nging the record
2da40 20 6e 75 6d 62 65 72 2c 20 64 65 6c 65 74 65 20   number, delete 
2da41 74 68 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20  the old record. 
2da42 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 61 73 46   */.    if( hasF
2da43 4b 20 7c 7c 20 63 68 6e 67 52 6f 77 69 64 20 29  K || chngRowid )
2da44 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2da45 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2da46 44 65 6c 65 74 65 2c 20 69 43 75 72 2c 20 30 29  Delete, iCur, 0)
2da47 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2da48 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2da49 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 69 66 28  v, j1);..    if(
2da4a 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 20 20   hasFK ){.      
2da4b 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 70  sqlite3FkCheck(p
2da4c 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
2da4d 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20  regNewRowid);.  
2da4e 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 6e    }.  .    /* In
2da4f 73 65 72 74 20 74 68 65 20 6e 65 77 20 69 6e 64  sert the new ind
2da50 65 78 20 65 6e 74 72 69 65 73 20 61 6e 64 20 74  ex entries and t
2da51 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 2a  he new record. *
2da52 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d  /.    sqlite3Com
2da53 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 70  pleteInsertion(p
2da54 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
2da55 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 20  r, regNewRowid, 
2da56 61 52 65 67 49 64 78 2c 20 31 2c 20 30 2c 20 30  aRegIdx, 1, 0, 0
2da57 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 61 6e  );..    /* Do an
2da58 79 20 4f 4e 20 43 41 53 43 41 44 45 2c 20 53 45  y ON CASCADE, SE
2da59 54 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44 45  T NULL or SET DE
2da5a 46 41 55 4c 54 20 6f 70 65 72 61 74 69 6f 6e 73  FAULT operations
2da5b 20 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 20   required to.   
2da5c 20 2a 2a 20 68 61 6e 64 6c 65 20 72 6f 77 73 20   ** handle rows 
2da5d 28 70 6f 73 73 69 62 6c 79 20 69 6e 20 6f 74 68  (possibly in oth
2da5e 65 72 20 74 61 62 6c 65 73 29 20 74 68 61 74 20  er tables) that 
2da5f 72 65 66 65 72 20 76 69 61 20 61 20 66 6f 72 65  refer via a fore
2da60 69 67 6e 20 6b 65 79 0a 20 20 20 20 2a 2a 20 74  ign key.    ** t
2da61 6f 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 75  o the row just u
2da62 70 64 61 74 65 64 2e 20 2a 2f 20 0a 20 20 20 20  pdated. */ .    
2da63 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20 20  if( hasFK ){.   
2da64 20 20 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69     sqlite3FkActi
2da65 6f 6e 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ons(pParse, pTab
2da66 2c 20 70 43 68 61 6e 67 65 73 2c 20 72 65 67 4f  , pChanges, regO
2da67 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a  ldRowid);.    }.
2da68 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d    }..  /* Increm
2da69 65 6e 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e  ent the row coun
2da6a 74 65 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ter .  */.  if( 
2da6b 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
2da6c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26  ITE_CountRows) &
2da6d 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67  & !pParse->pTrig
2da6e 67 65 72 54 61 62 29 7b 0a 20 20 20 20 73 71 6c  gerTab){.    sql
2da6f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2da70 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
2da71 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20  RowCount, 1);.  
2da72 7d 0a 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65  }..  sqlite3Code
2da73 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
2da74 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
2da75 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73  UPDATE, pChanges
2da76 2c 20 0a 20 20 20 20 20 20 54 52 49 47 47 45 52  , .      TRIGGER
2da77 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 72 65  _AFTER, pTab, re
2da78 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45 72 72  gOldRowid, onErr
2da79 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a  or, addr);..  /*
2da7a 20 52 65 70 65 61 74 20 74 68 65 20 61 62 6f 76   Repeat the abov
2da7b 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20  e with the next 
2da7c 72 65 63 6f 72 64 20 74 6f 20 62 65 20 75 70 64  record to be upd
2da7d 61 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a  ated, until.  **
2da7e 20 61 6c 6c 20 72 65 63 6f 72 64 20 73 65 6c 65   all record sele
2da7f 63 74 65 64 20 62 79 20 74 68 65 20 57 48 45 52  cted by the WHER
2da80 45 20 63 6c 61 75 73 65 20 68 61 76 65 20 62 65  E clause have be
2da81 65 6e 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f  en updated..  */
2da82 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2da83 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2da84 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c   0, addr);.  sql
2da85 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2da86 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a  (v, addr);..  /*
2da87 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65   Close all table
2da88 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  s */.  for(i=0, 
2da89 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
2da8a 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2da8b 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b  dx->pNext, i++){
2da8c 0a 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c  .    if( openAll
2da8d 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30   || aRegIdx[i]>0
2da8e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2da8f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2da90 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b  P_Close, iCur+i+
2da91 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1, 0);.    }.  }
2da92 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2da93 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2da94 2c 20 69 43 75 72 2c 20 30 29 3b 0a 0a 20 20 2f  , iCur, 0);..  /
2da95 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c  * Update the sql
2da96 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
2da97 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68  le by storing th
2da98 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
2da99 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f  .  ** maximum ro
2da9a 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75  wid counter valu
2da9b 65 73 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c  es recorded whil
2da9c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f  e inserting into
2da9d 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d  .  ** autoincrem
2da9e 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ent tables..  */
2da9f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2daa0 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61 72  ested==0 && pPar
2daa1 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d  se->pTriggerTab=
2daa2 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
2daa3 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e  3AutoincrementEn
2daa4 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a  d(pParse);.  }..
2daa5 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    /*.  ** Return
2daa6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
2daa7 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68  ows that were ch
2daa8 61 6e 67 65 64 2e 20 49 66 20 74 68 69 73 20 72  anged. If this r
2daa9 6f 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20  outine is .  ** 
2daaa 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
2daab 62 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c  because of a cal
2daac 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74  l to sqlite3Nest
2daad 65 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f  edParse(), do no
2daae 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  t.  ** invoke th
2daaf 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
2dab0 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ion..  */.  if( 
2dab1 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
2dab2 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20  E_CountRows) && 
2dab3 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
2dab4 72 54 61 62 20 26 26 20 21 70 50 61 72 73 65 2d  rTab && !pParse-
2dab5 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73  >nested ){.    s
2dab6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dab7 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2dab8 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31  , regRowCount, 1
2dab9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2daba 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
2dabb 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2dabc 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2dabd 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
2dabe 2c 20 22 72 6f 77 73 20 75 70 64 61 74 65 64 22  , "rows updated"
2dabf 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2dac0 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c  ;.  }..update_cl
2dac1 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33  eanup:.  sqlite3
2dac2 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26  AuthContextPop(&
2dac3 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  sContext);.  sql
2dac4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61  ite3DbFree(db, a
2dac5 52 65 67 49 64 78 29 3b 0a 20 20 73 71 6c 69 74  RegIdx);.  sqlit
2dac6 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 58 52  e3DbFree(db, aXR
2dac7 65 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72  ef);.  sqlite3Sr
2dac8 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
2dac9 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
2daca 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2dacb 74 65 28 64 62 2c 20 70 43 68 61 6e 67 65 73 29  te(db, pChanges)
2dacc 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
2dacd 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65  elete(db, pWhere
2dace 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 2f  );.  return;.}./
2dacf 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 69 73 56  * Make sure "isV
2dad0 69 65 77 22 20 61 6e 64 20 6f 74 68 65 72 20 6d  iew" and other m
2dad1 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 61 62  acros defined ab
2dad2 6f 76 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65  ove are undefine
2dad3 64 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20  d. Otherwise.** 
2dad4 74 68 65 6c 79 20 6d 61 79 20 69 6e 74 65 72 66  thely may interf
2dad5 65 72 65 20 77 69 74 68 20 63 6f 6d 70 69 6c 61  ere with compila
2dad6 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72 20 66 75  tion of other fu
2dad7 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20  nctions in this 
2dad8 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69 6e 20 61  file.** (or in a
2dad9 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 66 20  nother file, if 
2dada 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65  this file become
2dadb 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 61 6d  s part of the am
2dadc 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f  algamation).  */
2dadd 0a 23 69 66 64 65 66 20 69 73 56 69 65 77 0a 20  .#ifdef isView. 
2dade 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 65  #undef isView.#e
2dadf 6e 64 69 66 0a 23 69 66 64 65 66 20 70 54 72 69  ndif.#ifdef pTri
2dae0 67 67 65 72 0a 20 23 75 6e 64 65 66 20 70 54 72  gger. #undef pTr
2dae1 69 67 67 65 72 0a 23 65 6e 64 69 66 0a 0a 23 69  igger.#endif..#i
2dae2 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2dae3 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
2dae4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2dae5 64 65 20 66 6f 72 20 61 6e 20 55 50 44 41 54 45  de for an UPDATE
2dae6 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
2dae7 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ble..**.** The s
2dae8 74 72 61 74 65 67 79 20 69 73 20 74 68 61 74 20  trategy is that 
2dae9 77 65 20 63 72 65 61 74 65 20 61 6e 20 65 70 68  we create an eph
2daea 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 74 68  emerial table th
2daeb 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66  at contains.** f
2daec 6f 72 20 65 61 63 68 20 72 6f 77 20 74 6f 20 62  or each row to b
2daed 65 20 63 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a  e changed:.**.**
2daee 20 20 20 28 41 29 20 20 54 68 65 20 6f 72 69 67     (A)  The orig
2daef 69 6e 61 6c 20 72 6f 77 69 64 20 6f 66 20 74 68  inal rowid of th
2daf0 61 74 20 72 6f 77 2e 0a 2a 2a 20 20 20 28 42 29  at row..**   (B)
2daf1 20 20 54 68 65 20 72 65 76 69 73 65 64 20 72 6f    The revised ro
2daf2 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 2e  wid for the row.
2daf3 20 28 6e 6f 74 65 31 29 0a 2a 2a 20 20 20 28 43   (note1).**   (C
2daf4 29 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  )  The content o
2daf5 66 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69  f every column i
2daf6 6e 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a  n the row..**.**
2daf7 20 54 68 65 6e 20 77 65 20 6c 6f 6f 70 20 6f 76   Then we loop ov
2daf8 65 72 20 74 68 69 73 20 65 70 68 65 6d 65 72 61  er this ephemera
2daf9 6c 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20  l table and for 
2dafa 65 61 63 68 20 72 6f 77 20 69 6e 0a 2a 2a 20 74  each row in.** t
2dafb 68 65 20 65 70 68 65 72 6d 65 72 61 6c 20 74 61  he ephermeral ta
2dafc 62 6c 65 20 63 61 6c 6c 20 56 55 70 64 61 74 65  ble call VUpdate
2dafd 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 66 69 6e  ..**.** When fin
2dafe 69 73 68 65 64 2c 20 64 72 6f 70 20 74 68 65 20  ished, drop the 
2daff 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
2db00 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74 65 31 29 20 41  .**.** (note1) A
2db01 63 74 75 61 6c 6c 79 2c 20 69 66 20 77 65 20 6b  ctually, if we k
2db02 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65 20 74  now in advance t
2db03 68 61 74 20 28 41 29 20 69 73 20 61 6c 77 61 79  hat (A) is alway
2db04 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 61 73  s the same.** as
2db05 20 28 42 29 20 77 65 20 6f 6e 6c 79 20 73 74 6f   (B) we only sto
2db06 72 65 20 28 41 29 2c 20 74 68 65 6e 20 64 75 70  re (A), then dup
2db07 6c 69 63 61 74 65 20 28 41 29 20 77 68 65 6e 20  licate (A) when 
2db08 70 75 6c 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f 75  pulling.** it ou
2db09 74 20 6f 66 20 74 68 65 20 65 70 68 65 6d 65 72  t of the ephemer
2db0a 61 6c 20 74 61 62 6c 65 20 62 65 66 6f 72 65 20  al table before 
2db0b 63 61 6c 6c 69 6e 67 20 56 55 70 64 61 74 65 2e  calling VUpdate.
2db0c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2db0d 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62  updateVirtualTab
2db0e 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
2db0f 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  rse,       /* Th
2db10 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
2db11 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2db12 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20 54  pSrc,       /* T
2db13 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2db14 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64 20   to be modified 
2db15 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2db16 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2db17 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
2db18 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2db19 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65 20  hanges,  /* The 
2db1a 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67  columns to chang
2db1b 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45 20  e in the UPDATE 
2db1c 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45  statement */.  E
2db1d 78 70 72 20 2a 70 52 6f 77 69 64 2c 20 20 20 20  xpr *pRowid,    
2db1e 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2db1f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d 70  n used to recomp
2db20 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a 2f  ute the rowid */
2db21 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20 20  .  int *aXRef,  
2db22 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69          /* Mappi
2db23 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20  ng from columns 
2db24 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72 69  of pTab to entri
2db25 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20 2a  es in pChanges *
2db26 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2db27 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52           /* WHER
2db28 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
2db29 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
2db2a 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
2db2b 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2db2c 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61  ;  /* Virtual ma
2db2d 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73  chine under cons
2db2e 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  truction */.  Ex
2db2f 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d  prList *pEList =
2db30 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 72   0;     /* The r
2db31 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2db32 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2db33 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
2db34 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 20 20  Select = 0;     
2db35 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2db36 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78  tatement */.  Ex
2db37 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
2db38 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f          /* Tempo
2db39 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  rary expression 
2db3a 2a 2f 0a 20 20 69 6e 74 20 65 70 68 65 6d 54 61  */.  int ephemTa
2db3b 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
2db3c 2a 20 54 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20  * Table holding 
2db3d 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
2db3e 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e  e SELECT */.  in
2db3f 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2db40 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2db41 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
2db42 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20 20   addr;          
2db43 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2db44 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
2db45 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 3b 20   */.  int iReg; 
2db46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db47 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 65  /* First registe
2db48 72 20 69 6e 20 73 65 74 20 70 61 73 73 65 64 20  r in set passed 
2db49 74 6f 20 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f  to OP_VUpdate */
2db4a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2db4b 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20   pParse->db; /* 
2db4c 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2db4d 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
2db4e 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f  har *pVTab = (co
2db4f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
2db50 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
2db51 54 61 62 29 3b 0a 20 20 53 65 6c 65 63 74 44 65  Tab);.  SelectDe
2db52 73 74 20 64 65 73 74 3b 0a 0a 20 20 2f 2a 20 43  st dest;..  /* C
2db53 6f 6e 73 74 72 75 63 74 20 74 68 65 20 53 45 4c  onstruct the SEL
2db54 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68  ECT statement th
2db55 61 74 20 77 69 6c 6c 20 66 69 6e 64 20 74 68 65  at will find the
2db56 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72 0a   new values for.
2db57 20 20 2a 2a 20 61 6c 6c 20 75 70 64 61 74 65 64    ** all updated
2db58 20 72 6f 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70   rows. .  */.  p
2db59 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
2db5a 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2db5b 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33  arse, 0, sqlite3
2db5c 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20  Expr(db, TK_ID, 
2db5d 22 5f 72 6f 77 69 64 5f 22 29 29 3b 0a 20 20 69  "_rowid_"));.  i
2db5e 66 28 20 70 52 6f 77 69 64 20 29 7b 0a 20 20 20  f( pRowid ){.   
2db5f 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65   pEList = sqlite
2db60 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2db61 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 0a  pParse, pEList,.
2db62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db64 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2db65 70 28 64 62 2c 20 70 52 6f 77 69 64 2c 20 30 29  p(db, pRowid, 0)
2db66 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
2db67 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20 29   pTab->iPKey<0 )
2db68 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2db69 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
2db6a 0a 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 69  .    if( aXRef[i
2db6b 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 45  ]>=0 ){.      pE
2db6c 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
2db6d 72 44 75 70 28 64 62 2c 20 70 43 68 61 6e 67 65  rDup(db, pChange
2db6e 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d 5d 2e 70  s->a[aXRef[i]].p
2db6f 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 7d 65  Expr, 0);.    }e
2db70 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
2db71 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2db72 62 2c 20 54 4b 5f 49 44 2c 20 70 54 61 62 2d 3e  b, TK_ID, pTab->
2db73 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[i].zName);.
2db74 20 20 20 20 7d 0a 20 20 20 20 70 45 4c 69 73 74      }.    pEList
2db75 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2db76 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2db77 20 70 45 4c 69 73 74 2c 20 70 45 78 70 72 29 3b   pEList, pExpr);
2db78 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d  .  }.  pSelect =
2db79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
2db7a 77 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  w(pParse, pEList
2db7b 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c 20  , pSrc, pWhere, 
2db7c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2db7d 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72 65 61 74  );.  .  /* Creat
2db7e 65 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  e the ephemeral 
2db7f 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
2db80 20 74 68 65 20 75 70 64 61 74 65 20 72 65 73 75   the update resu
2db81 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  lts will.  ** be
2db82 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20   stored..  */.  
2db83 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 65  assert( v );.  e
2db84 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73 65  phemTab = pParse
2db85 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69  ->nTab++;.  sqli
2db86 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2db87 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2db88 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70 54 61  l, ephemTab, pTa
2db89 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69  b->nCol+1+(pRowi
2db8a 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20 66 69  d!=0));..  /* fi
2db8b 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  ll the ephemeral
2db8c 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 73   table .  */.  s
2db8d 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2db8e 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
2db8f 54 61 62 6c 65 2c 20 65 70 68 65 6d 54 61 62 29  Table, ephemTab)
2db90 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2db91 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
2db92 74 2c 20 26 64 65 73 74 29 3b 0a 0a 20 20 2f 2a  t, &dest);..  /*
2db93 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2db94 6f 20 73 63 61 6e 20 74 68 65 20 65 70 68 65 6d  o scan the ephem
2db95 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 63  eral table and c
2db96 61 6c 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f 0a  all VUpdate. */.
2db97 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73    iReg = ++pPars
2db98 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70 50 61 72 73  e->nMem;.  pPars
2db99 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d  e->nMem += pTab-
2db9a 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 61 64 64 72 20  >nCol+1;.  addr 
2db9b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2db9c 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2db9d 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b 0a  , ephemTab, 0);.
2db9e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2db9f 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2dba0 2c 20 20 65 70 68 65 6d 54 61 62 2c 20 30 2c 20  ,  ephemTab, 0, 
2dba1 69 52 65 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  iReg);.  sqlite3
2dba2 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2dba3 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61  _Column, ephemTa
2dba4 62 2c 20 28 70 52 6f 77 69 64 3f 31 3a 30 29 2c  b, (pRowid?1:0),
2dba5 20 69 52 65 67 2b 31 29 3b 0a 20 20 66 6f 72 28   iReg+1);.  for(
2dba6 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2dba7 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  l; i++){.    sql
2dba8 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2dba9 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68  , OP_Column, eph
2dbaa 65 6d 54 61 62 2c 20 69 2b 31 2b 28 70 52 6f 77  emTab, i+1+(pRow
2dbab 69 64 21 3d 30 29 2c 20 69 52 65 67 2b 32 2b 69  id!=0), iReg+2+i
2dbac 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2dbad 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65  VtabMakeWritable
2dbae 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
2dbaf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2dbb0 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74  Op4(v, OP_VUpdat
2dbb1 65 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  e, 0, pTab->nCol
2dbb2 2b 32 2c 20 69 52 65 67 2c 20 70 56 54 61 62 2c  +2, iReg, pVTab,
2dbb3 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c   P4_VTAB);.  sql
2dbb4 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
2dbb5 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
2dbb6 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2dbb7 4e 65 78 74 2c 20 65 70 68 65 6d 54 61 62 2c 20  Next, ephemTab, 
2dbb8 61 64 64 72 2b 31 29 3b 0a 20 20 73 71 6c 69 74  addr+1);.  sqlit
2dbb9 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2dbba 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74  , addr);.  sqlit
2dbbb 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2dbbc 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d 54  OP_Close, ephemT
2dbbd 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c  ab, 0);..  /* Cl
2dbbe 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74  eanup */.  sqlit
2dbbf 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2dbc0 62 2c 20 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d  b, pSelect);  .}
2dbc1 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2dbc2 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2dbc3 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BLE */../*******
2dbc4 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75  ******* End of u
2dbc5 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  pdate.c ********
2dbc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbc8 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2dbc9 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2dbca 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a  le vacuum.c ****
2dbcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbcd 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2dbce 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a  03 April 6.**.**
2dbcf 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2dbd0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2dbd1 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2dbd2 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2dbd3 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2dbd4 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2dbd5 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2dbd6 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2dbd7 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2dbd8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2dbd9 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2dbda 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2dbdb 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2dbdc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2dbdd 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2dbde 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2dbdf 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2dbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbe1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2dbe5 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2dbe6 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
2dbe7 6c 65 6d 65 6e 74 20 74 68 65 20 56 41 43 55 55  lement the VACUU
2dbe8 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  M command..**.**
2dbe9 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64   Most of the cod
2dbea 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d  e in this file m
2dbeb 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79  ay be omitted by
2dbec 20 64 65 66 69 6e 69 6e 67 20 74 68 65 0a 2a 2a   defining the.**
2dbed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43   SQLITE_OMIT_VAC
2dbee 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 0a 23  UUM macro..*/..#
2dbef 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2dbf0 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20  TE_OMIT_VACUUM) 
2dbf1 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
2dbf2 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a  TE_OMIT_ATTACH).
2dbf3 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53  /*.** Execute zS
2dbf4 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64  ql on database d
2dbf5 62 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  b. Return an err
2dbf6 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
2dbf7 69 63 20 69 6e 74 20 65 78 65 63 53 71 6c 28 73  ic int execSql(s
2dbf8 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
2dbf9 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20  t char *zSql){. 
2dbfa 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
2dbfb 53 74 6d 74 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59  Stmt;.  VVA_ONLY
2dbfc 28 20 69 6e 74 20 72 63 3b 20 29 0a 20 20 69 66  ( int rc; ).  if
2dbfd 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72  ( !zSql ){.    r
2dbfe 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2dbff 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51  EM;.  }.  if( SQ
2dc00 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2dc01 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  _prepare(db, zSq
2dc02 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
2dc03 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2dc04 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
2dc05 64 62 29 3b 0a 20 20 7d 0a 20 20 56 56 41 5f 4f  db);.  }.  VVA_O
2dc06 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69  NLY( rc = ) sqli
2dc07 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
2dc08 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
2dc09 51 4c 49 54 45 5f 52 4f 57 20 29 3b 0a 20 20 72  QLITE_ROW );.  r
2dc0a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69  eturn sqlite3_fi
2dc0b 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d  nalize(pStmt);.}
2dc0c 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
2dc0d 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65  zSql on database
2dc0e 20 64 62 2e 20 54 68 65 20 73 74 61 74 65 6d 65   db. The stateme
2dc0f 6e 74 20 72 65 74 75 72 6e 73 20 65 78 61 63 74  nt returns exact
2dc10 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e  ly.** one column
2dc11 2e 20 45 78 65 63 75 74 65 20 74 68 69 73 20 61  . Execute this a
2dc12 73 20 53 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d  s SQL on the sam
2dc13 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  e database..*/.s
2dc14 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 45 78  tatic int execEx
2dc15 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64  ecSql(sqlite3 *d
2dc16 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2dc17 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Sql){.  sqlite3_
2dc18 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69  stmt *pStmt;.  i
2dc19 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  nt rc;..  rc = s
2dc1a 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64  qlite3_prepare(d
2dc1b 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53  b, zSql, -1, &pS
2dc1c 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  tmt, 0);.  if( r
2dc1d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2dc1e 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69  eturn rc;..  whi
2dc1f 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  le( SQLITE_ROW==
2dc20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
2dc21 6d 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  mt) ){.    rc = 
2dc22 65 78 65 63 53 71 6c 28 64 62 2c 20 28 63 68 61  execSql(db, (cha
2dc23 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
2dc24 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29  n_text(pStmt, 0)
2dc25 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2dc26 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2dc27 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
2dc28 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  ze(pStmt);.     
2dc29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
2dc2a 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
2dc2b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2dc2c 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pStmt);.}../*.*
2dc2d 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61  * The non-standa
2dc2e 72 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e  rd VACUUM comman
2dc2f 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6c 65  d is used to cle
2dc30 61 6e 20 75 70 20 74 68 65 20 64 61 74 61 62 61  an up the databa
2dc31 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20  se,.** collapse 
2dc32 66 72 65 65 20 73 70 61 63 65 2c 20 65 74 63 2e  free space, etc.
2dc33 20 20 49 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64    It is modelled
2dc34 20 61 66 74 65 72 20 74 68 65 20 56 41 43 55 55   after the VACUU
2dc35 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20  M command.** in 
2dc36 50 6f 73 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a  PostgreSQL..**.*
2dc37 2a 20 49 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30  * In version 1.0
2dc38 2e 78 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68  .x of SQLite, th
2dc39 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  e VACUUM command
2dc3a 20 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67   would call.** g
2dc3b 64 62 6d 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29  dbm_reorganize()
2dc3c 20 6f 6e 20 61 6c 6c 20 74 68 65 20 64 61 74 61   on all the data
2dc3d 62 61 73 65 20 74 61 62 6c 65 73 2e 20 20 42 75  base tables.  Bu
2dc3e 74 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77  t beginning.** w
2dc3f 69 74 68 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74  ith 2.0.0, SQLit
2dc40 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73  e no longer uses
2dc41 20 47 44 42 4d 20 73 6f 20 74 68 69 73 20 63 6f   GDBM so this co
2dc42 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63  mmand has.** bec
2dc43 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a  ome a no-op..*/.
2dc44 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2dc45 6f 69 64 20 73 71 6c 69 74 65 33 56 61 63 75 75  oid sqlite3Vacuu
2dc46 6d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  m(Parse *pParse)
2dc47 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
2dc48 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2dc49 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rse);.  if( v ){
2dc4a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2dc4b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63  AddOp2(v, OP_Vac
2dc4c 75 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a  uum, 0, 0);.  }.
2dc4d 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a    return;.}../*.
2dc4e 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2dc4f 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f  implements the O
2dc50 50 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64 65 20  P_Vacuum opcode 
2dc51 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a  of the VDBE..*/.
2dc52 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2dc53 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63  nt sqlite3RunVac
2dc54 75 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45 72 72  uum(char **pzErr
2dc55 4d 73 67 2c 20 73 71 6c 69 74 65 33 20 2a 64 62  Msg, sqlite3 *db
2dc56 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
2dc57 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20  LITE_OK;     /* 
2dc58 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
2dc59 20 73 65 72 76 69 63 65 20 72 6f 75 74 69 6e 65   service routine
2dc5a 73 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d  s */.  Btree *pM
2dc5b 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ain;           /
2dc5c 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 62  * The database b
2dc5d 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a 2f  eing vacuumed */
2dc5e 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d 70 3b  .  Btree *pTemp;
2dc5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2dc60 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2dc61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d 20 69  base we vacuum i
2dc62 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  nto */.  char *z
2dc63 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Sql = 0;        
2dc64 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
2dc65 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  ts */.  int save
2dc66 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  d_flags;        
2dc67 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
2dc68 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20  f the db->flags 
2dc69 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 6e  */.  int saved_n
2dc6a 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20  Change;      /* 
2dc6b 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64  Saved value of d
2dc6c 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 20 20  b->nChange */.  
2dc6d 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c  int saved_nTotal
2dc6e 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76 65 64  Change; /* Saved
2dc6f 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 54   value of db->nT
2dc70 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a 20 20  otalChange */.  
2dc71 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20 20  Db *pDb = 0;    
2dc72 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2dc73 61 73 65 20 74 6f 20 64 65 74 61 63 68 20 61 74  ase to detach at
2dc74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20 2a   end of vacuum *
2dc75 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44 62 3b  /.  int isMemDb;
2dc76 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2dc77 72 75 65 20 69 66 20 76 61 63 75 75 6d 69 6e 67  rue if vacuuming
2dc78 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61   a :memory: data
2dc79 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  base */.  int nR
2dc7a 65 73 3b 0a 0a 20 20 69 66 28 20 21 64 62 2d 3e  es;..  if( !db->
2dc7b 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
2dc7c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
2dc7d 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
2dc7e 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55 4d 20   "cannot VACUUM 
2dc7f 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74 72  from within a tr
2dc80 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
2dc81 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2dc82 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
2dc83 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  Save the current
2dc84 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61   value of the da
2dc85 74 61 62 61 73 65 20 66 6c 61 67 73 20 73 6f 20  tabase flags so 
2dc86 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 0a  that it can be .
2dc87 20 20 2a 2a 20 72 65 73 74 6f 72 65 64 20 62 65    ** restored be
2dc88 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20  fore returning. 
2dc89 54 68 65 6e 20 73 65 74 20 74 68 65 20 77 72 69  Then set the wri
2dc8a 74 61 62 6c 65 2d 73 63 68 65 6d 61 20 66 6c 61  table-schema fla
2dc8b 67 2c 20 61 6e 64 0a 20 20 2a 2a 20 64 69 73 61  g, and.  ** disa
2dc8c 62 6c 65 20 43 48 45 43 4b 20 61 6e 64 20 66 6f  ble CHECK and fo
2dc8d 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
2dc8e 61 69 6e 74 73 2e 20 20 2a 2f 0a 20 20 73 61 76  aints.  */.  sav
2dc8f 65 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66  ed_flags = db->f
2dc90 6c 61 67 73 3b 0a 20 20 73 61 76 65 64 5f 6e 43  lags;.  saved_nC
2dc91 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43 68 61  hange = db->nCha
2dc92 6e 67 65 3b 0a 20 20 73 61 76 65 64 5f 6e 54 6f  nge;.  saved_nTo
2dc93 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e  talChange = db->
2dc94 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 20 20  nTotalChange;.  
2dc95 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
2dc96 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20  ITE_WriteSchema 
2dc97 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  | SQLITE_IgnoreC
2dc98 68 65 63 6b 73 3b 0a 20 20 64 62 2d 3e 66 6c 61  hecks;.  db->fla
2dc99 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 46 6f  gs &= ~SQLITE_Fo
2dc9a 72 65 69 67 6e 4b 65 79 73 3b 0a 0a 20 20 70 4d  reignKeys;..  pM
2dc9b 61 69 6e 20 3d 20 64 62 2d 3e 61 44 62 5b 30 5d  ain = db->aDb[0]
2dc9c 2e 70 42 74 3b 0a 20 20 69 73 4d 65 6d 44 62 20  .pBt;.  isMemDb 
2dc9d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  = sqlite3PagerIs
2dc9e 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72  Memdb(sqlite3Btr
2dc9f 65 65 50 61 67 65 72 28 70 4d 61 69 6e 29 29 3b  eePager(pMain));
2dca0 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68  ..  /* Attach th
2dca1 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2dca2 62 61 73 65 20 61 73 20 27 76 61 63 75 75 6d 5f  base as 'vacuum_
2dca3 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68 72 6f  db'. The synchro
2dca4 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20 2a 2a  nous pragma.  **
2dca5 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f 20 27   can be set to '
2dca6 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20 66 69  off' for this fi
2dca7 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e 6f 74  le, as it is not
2dca8 20 72 65 63 6f 76 65 72 65 64 20 69 66 20 61 20   recovered if a 
2dca9 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63 75 72  crash.  ** occur
2dcaa 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20 69 6e  s anyway. The in
2dcab 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 64  tegrity of the d
2dcac 61 74 61 62 61 73 65 20 69 73 20 6d 61 69 6e 74  atabase is maint
2dcad 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a 2a 20  ained by a.  ** 
2dcae 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63 68 72  (possibly synchr
2dcaf 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63 74 69  onous) transacti
2dcb0 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  on opened on the
2dcb1 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 62   main database b
2dcb2 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c 69 74  efore.  ** sqlit
2dcb3 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
2dcb4 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a  ) is called..  *
2dcb5 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69 6d 69  *.  ** An optimi
2dcb6 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20  sation would be 
2dcb7 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75  to use a non-jou
2dcb8 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a 20 20  rnaled pager..  
2dcb9 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20 74 72  ** (Later:) I tr
2dcba 69 65 64 20 73 65 74 74 69 6e 67 20 22 50 52 41  ied setting "PRA
2dcbb 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e 6a 6f  GMA vacuum_db.jo
2dcbc 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22 20  urnal_mode=OFF" 
2dcbd 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20 61 63  but.  ** that ac
2dcbe 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68 65 20  tually made the 
2dcbf 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f 77 65  VACUUM run slowe
2dcc0 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c 65 20  r.  Very little 
2dcc1 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a 2a  journalling.  **
2dcc2 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72 73   actually occurs
2dcc3 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 76 61   when doing a va
2dcc4 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65 20 76  cuum since the v
2dcc5 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e 69 74  acuum_db is init
2dcc6 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70 74 79  ially.  ** empty
2dcc7 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f 75 72  .  Only the jour
2dcc8 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 77 72  nal header is wr
2dcc9 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65 6e 74  itten.  Apparent
2dcca 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f 72 65  ly it takes more
2dccb 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20 70 61  .  ** time to pa
2dccc 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68 65 20  rse and run the 
2dccd 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e 20 6a  PRAGMA to turn j
2dcce 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66 20 74  ournalling off t
2dccf 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20 2a 2a  han it does.  **
2dcd0 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6a 6f   to write the jo
2dcd1 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c  urnal header fil
2dcd2 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d  e..  */.  zSql =
2dcd3 20 22 41 54 54 41 43 48 20 27 27 20 41 53 20 76   "ATTACH '' AS v
2dcd4 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20 72 63  acuum_db;";.  rc
2dcd5 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 7a   = execSql(db, z
2dcd6 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Sql);.  if( rc!=
2dcd7 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2dcd8 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2dcd9 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
2dcda 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20 61  [db->nDb-1];.  a
2dcdb 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 64 62  ssert( strcmp(db
2dcdc 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d  ->aDb[db->nDb-1]
2dcdd 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d 5f 64  .zName,"vacuum_d
2dcde 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54 65 6d  b")==0 );.  pTem
2dcdf 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62 2d 3e  p = db->aDb[db->
2dce0 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20 2f  nDb-1].pBt;..  /
2dce1 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 65 78  * The call to ex
2dce2 65 63 53 71 6c 28 29 20 74 6f 20 61 74 74 61 63  ecSql() to attac
2dce3 68 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62  h the temp datab
2dce4 61 73 65 20 68 61 73 20 6c 65 66 74 20 74 68 65  ase has left the
2dce5 20 66 69 6c 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65   file.  ** locke
2dce6 64 20 28 61 73 20 74 68 65 72 65 20 77 61 73 20  d (as there was 
2dce7 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 61 63  more than one ac
2dce8 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20 77  tive statement w
2dce9 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
2dcea 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 72 65 61 64  ion.  ** to read
2dceb 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73 20   the schema was 
2dcec 63 6f 6e 63 6c 75 64 65 64 2e 20 55 6e 6c 6f 63  concluded. Unloc
2dced 6b 20 69 74 20 68 65 72 65 20 73 6f 20 74 68 61  k it here so tha
2dcee 74 20 74 68 69 73 20 64 6f 65 73 6e 27 74 0a 20  t this doesn't. 
2dcef 20 2a 2a 20 63 61 75 73 65 20 70 72 6f 62 6c 65   ** cause proble
2dcf0 6d 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c 20  ms for the call 
2dcf1 74 6f 20 42 74 72 65 65 53 65 74 50 61 67 65 53  to BtreeSetPageS
2dcf2 69 7a 65 28 29 20 62 65 6c 6f 77 2e 20 20 2a 2f  ize() below.  */
2dcf3 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  .  sqlite3BtreeC
2dcf4 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 0a 20  ommit(pTemp);.. 
2dcf5 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42   nRes = sqlite3B
2dcf6 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70  treeGetReserve(p
2dcf7 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56  Main);..  /* A V
2dcf8 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61  ACUUM cannot cha
2dcf9 6e 67 65 20 74 68 65 20 70 61 67 65 73 69 7a 65  nge the pagesize
2dcfa 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65 64   of an encrypted
2dcfb 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69   database. */.#i
2dcfc 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
2dcfd 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d 3e  CODEC.  if( db->
2dcfe 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b 0a  nextPagesize ){.
2dcff 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
2dd00 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
2dd01 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ey(sqlite3*, int
2dd02 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b  , void**, int*);
2dd03 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
2dd04 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20     char *zKey;. 
2dd05 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47     sqlite3CodecG
2dd06 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f  etKey(db, 0, (vo
2dd07 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65  id**)&zKey, &nKe
2dd08 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65 79  y);.    if( nKey
2dd09 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73   ) db->nextPages
2dd0a 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ize = 0;.  }.#en
2dd0b 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2dd0c 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
2dd0d 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74 65  ze(pTemp, sqlite
2dd0e 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2dd0f 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 2c 20  e(pMain), nRes, 
2dd10 30 29 0a 20 20 20 7c 7c 20 28 21 69 73 4d 65 6d  0).   || (!isMem
2dd11 44 62 20 26 26 20 73 71 6c 69 74 65 33 42 74 72  Db && sqlite3Btr
2dd12 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeSetPageSize(pT
2dd13 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67  emp, db->nextPag
2dd14 65 73 69 7a 65 2c 20 6e 52 65 73 2c 20 30 29 29  esize, nRes, 0))
2dd15 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d  .   || NEVER(db-
2dd16 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20  >mallocFailed). 
2dd17 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
2dd18 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
2dd19 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2dd1a 6d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 65 78  m;.  }.  rc = ex
2dd1b 65 63 53 71 6c 28 64 62 2c 20 22 50 52 41 47 4d  ecSql(db, "PRAGM
2dd1c 41 20 76 61 63 75 75 6d 5f 64 62 2e 73 79 6e 63  A vacuum_db.sync
2dd1d 68 72 6f 6e 6f 75 73 3d 4f 46 46 22 29 3b 0a 20  hronous=OFF");. 
2dd1e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2dd1f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  OK ){.    goto e
2dd20 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2dd21 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2dd22 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
2dd23 4d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  M.  sqlite3Btree
2dd24 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54  SetAutoVacuum(pT
2dd25 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 41 75 74  emp, db->nextAut
2dd26 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65  ovac>=0 ? db->ne
2dd27 78 74 41 75 74 6f 76 61 63 20 3a 0a 20 20 20 20  xtAutovac :.    
2dd28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd2a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
2dd2b 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
2dd2c 28 70 4d 61 69 6e 29 29 3b 0a 23 65 6e 64 69 66  (pMain));.#endif
2dd2d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74  ..  /* Begin a t
2dd2e 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
2dd2f 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2dd30 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53 49 56   "BEGIN EXCLUSIV
2dd31 45 3b 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  E;");.  if( rc!=
2dd32 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2dd33 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2dd34 0a 20 20 2f 2a 20 51 75 65 72 79 20 74 68 65 20  .  /* Query the 
2dd35 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 61  schema of the ma
2dd36 69 6e 20 64 61 74 61 62 61 73 65 2e 20 43 72 65  in database. Cre
2dd37 61 74 65 20 61 20 6d 69 72 72 6f 72 20 73 63 68  ate a mirror sch
2dd38 65 6d 61 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ema.  ** in the 
2dd39 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2dd3a 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  se..  */.  rc = 
2dd3b 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2dd3c 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2dd3d 43 52 45 41 54 45 20 54 41 42 4c 45 20 76 61 63  CREATE TABLE vac
2dd3e 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73  uum_db.' || subs
2dd3f 74 72 28 73 71 6c 2c 31 34 29 20 22 0a 20 20 20  tr(sql,14) ".   
2dd40 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74     "  FROM sqlit
2dd41 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 74  e_master WHERE t
2dd42 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44 20  ype='table' AND 
2dd43 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65  name!='sqlite_se
2dd44 71 75 65 6e 63 65 27 22 0a 20 20 20 20 20 20 22  quence'".      "
2dd45 20 20 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e     AND rootpage>
2dd46 30 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  0".  );.  if( rc
2dd47 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2dd48 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2dd49 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
2dd4a 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2dd4b 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
2dd4c 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
2dd4d 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
2dd4e 31 34 29 22 0a 20 20 20 20 20 20 22 20 20 46 52  14)".      "  FR
2dd4f 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2dd50 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20   WHERE sql LIKE 
2dd51 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 25 27  'CREATE INDEX %'
2dd52 20 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   ");.  if( rc!=S
2dd53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2dd54 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2dd55 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2dd56 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2dd57 4c 45 43 54 20 27 43 52 45 41 54 45 20 55 4e 49  LECT 'CREATE UNI
2dd58 51 55 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d  QUE INDEX vacuum
2dd59 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2dd5a 73 71 6c 2c 32 31 29 20 22 0a 20 20 20 20 20 20  sql,21) ".      
2dd5b 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d  "  FROM sqlite_m
2dd5c 61 73 74 65 72 20 57 48 45 52 45 20 73 71 6c 20  aster WHERE sql 
2dd5d 4c 49 4b 45 20 27 43 52 45 41 54 45 20 55 4e 49  LIKE 'CREATE UNI
2dd5e 51 55 45 20 49 4e 44 45 58 20 25 27 22 29 3b 0a  QUE INDEX %'");.
2dd5f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2dd60 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2dd61 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2dd62 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
2dd63 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6d   tables in the m
2dd64 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 46 6f  ain database. Fo
2dd65 72 20 65 61 63 68 2c 20 64 6f 0a 20 20 2a 2a 20  r each, do.  ** 
2dd66 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  an "INSERT INTO 
2dd67 76 61 63 75 75 6d 5f 64 62 2e 78 78 78 20 53 45  vacuum_db.xxx SE
2dd68 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e  LECT * FROM main
2dd69 2e 78 78 78 3b 22 20 74 6f 20 63 6f 70 79 0a 20  .xxx;" to copy. 
2dd6a 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   ** the contents
2dd6b 20 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72   to the temporar
2dd6c 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  y database..  */
2dd6d 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
2dd6e 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22  Sql(db, .      "
2dd6f 53 45 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49  SELECT 'INSERT I
2dd70 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20  NTO vacuum_db.' 
2dd71 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22  || quote(name) "
2dd72 0a 20 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c  .      "|| ' SEL
2dd73 45 43 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e  ECT * FROM main.
2dd74 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29  ' || quote(name)
2dd75 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20 20 22   || ';'".      "
2dd76 46 52 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65  FROM main.sqlite
2dd77 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20 20 20  _master ".      
2dd78 22 57 48 45 52 45 20 74 79 70 65 20 3d 20 27 74  "WHERE type = 't
2dd79 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d  able' AND name!=
2dd7a 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  'sqlite_sequence
2dd7b 27 20 22 0a 20 20 20 20 20 20 22 20 20 41 4e 44  ' ".      "  AND
2dd7c 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 0a 20 20   rootpage>0"..  
2dd7d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2dd7e 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2dd7f 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20  d_of_vacuum;..  
2dd80 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 74 68 65  /* Copy over the
2dd81 20 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 0a   sequence table.
2dd82 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
2dd83 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
2dd84 20 20 20 22 53 45 4c 45 43 54 20 27 44 45 4c 45     "SELECT 'DELE
2dd85 54 45 20 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64  TE FROM vacuum_d
2dd86 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d  b.' || quote(nam
2dd87 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20  e) || ';' ".    
2dd88 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64    "FROM vacuum_d
2dd89 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  b.sqlite_master 
2dd8a 57 48 45 52 45 20 6e 61 6d 65 3d 27 73 71 6c 69  WHERE name='sqli
2dd8b 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a 20  te_sequence' ". 
2dd8c 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2dd8d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2dd8e 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2dd8f 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c  rc = execExecSql
2dd90 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c  (db, .      "SEL
2dd91 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f  ECT 'INSERT INTO
2dd92 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2dd93 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20  quote(name) ".  
2dd94 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54      "|| ' SELECT
2dd95 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c   * FROM main.' |
2dd96 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
2dd97 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52   ';' ".      "FR
2dd98 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c  OM vacuum_db.sql
2dd99 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
2dd9a 20 6e 61 6d 65 3d 3d 27 73 71 6c 69 74 65 5f 73   name=='sqlite_s
2dd9b 65 71 75 65 6e 63 65 27 3b 22 0a 20 20 29 3b 0a  equence';".  );.
2dd9c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2dd9d 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2dd9e 66 5f 76 61 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a  f_vacuum;...  /*
2dd9f 20 43 6f 70 79 20 74 68 65 20 74 72 69 67 67 65   Copy the trigge
2dda0 72 73 2c 20 76 69 65 77 73 2c 20 61 6e 64 20 76  rs, views, and v
2dda1 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 66 72  irtual tables fr
2dda2 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
2dda3 62 61 73 65 0a 20 20 2a 2a 20 6f 76 65 72 20 74  base.  ** over t
2dda4 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  o the temporary 
2dda5 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20  database.  None 
2dda6 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63 74 73  of these objects
2dda7 20 68 61 73 20 61 6e 79 0a 20 20 2a 2a 20 61 73   has any.  ** as
2dda8 73 6f 63 69 61 74 65 64 20 73 74 6f 72 61 67 65  sociated storage
2dda9 2c 20 73 6f 20 61 6c 6c 20 77 65 20 68 61 76 65  , so all we have
2ddaa 20 74 6f 20 64 6f 20 69 73 20 63 6f 70 79 20 74   to do is copy t
2ddab 68 65 69 72 20 65 6e 74 72 69 65 73 0a 20 20 2a  heir entries.  *
2ddac 2a 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54  * from the SQLIT
2ddad 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 0a  E_MASTER table..
2ddae 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
2ddaf 53 71 6c 28 64 62 2c 0a 20 20 20 20 20 20 22 49  Sql(db,.      "I
2ddb0 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
2ddb1 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74  m_db.sqlite_mast
2ddb2 65 72 20 22 0a 20 20 20 20 20 20 22 20 20 53 45  er ".      "  SE
2ddb3 4c 45 43 54 20 74 79 70 65 2c 20 6e 61 6d 65 2c  LECT type, name,
2ddb4 20 74 62 6c 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70   tbl_name, rootp
2ddb5 61 67 65 2c 20 73 71 6c 22 0a 20 20 20 20 20 20  age, sql".      
2ddb6 22 20 20 20 20 46 52 4f 4d 20 6d 61 69 6e 2e 73  "    FROM main.s
2ddb7 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 20 20  qlite_master".  
2ddb8 20 20 20 20 22 20 20 20 57 48 45 52 45 20 74 79      "   WHERE ty
2ddb9 70 65 3d 27 76 69 65 77 27 20 4f 52 20 74 79 70  pe='view' OR typ
2ddba 65 3d 27 74 72 69 67 67 65 72 27 22 0a 20 20 20  e='trigger'".   
2ddbb 20 20 20 22 20 20 20 20 20 20 4f 52 20 28 74 79     "      OR (ty
2ddbc 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 72  pe='table' AND r
2ddbd 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20 20 29 3b  ootpage=0)".  );
2ddbe 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
2ddbf 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2ddc0 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
2ddc1 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74 68 65 20  int, unless the 
2ddc2 6d 61 69 6e 20 64 62 20 77 61 73 20 63 6f 6d 70  main db was comp
2ddc3 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c 20 74 68  letely empty, th
2ddc4 65 72 65 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a  ere is now a.  *
2ddc5 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  * transaction op
2ddc6 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d  en on the vacuum
2ddc7 20 64 61 74 61 62 61 73 65 2c 20 62 75 74 20 6e   database, but n
2ddc8 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64  ot on the main d
2ddc9 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70  atabase..  ** Op
2ddca 65 6e 20 61 20 62 74 72 65 65 20 6c 65 76 65 6c  en a btree level
2ddcb 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
2ddcc 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2ddcd 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 61  e. This allows a
2ddce 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  .  ** call to sq
2ddcf 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
2ddd0 6c 65 28 29 2e 20 54 68 65 20 6d 61 69 6e 20 64  le(). The main d
2ddd1 61 74 61 62 61 73 65 20 62 74 72 65 65 20 6c 65  atabase btree le
2ddd2 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  vel.  ** transac
2ddd3 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d  tion is then com
2ddd4 6d 69 74 74 65 64 2c 20 73 6f 20 74 68 65 20 53  mitted, so the S
2ddd5 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b  QL level never k
2ddd6 6e 6f 77 73 20 69 74 20 77 61 73 0a 20 20 2a 2a  nows it was.  **
2ddd7 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
2ddd8 69 6e 67 2e 20 54 68 69 73 20 77 61 79 2c 20 74  ing. This way, t
2ddd9 68 65 20 53 51 4c 20 74 72 61 6e 73 61 63 74 69  he SQL transacti
2ddda 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  on used to creat
2dddb 65 20 74 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f  e the.  ** tempo
2dddc 72 61 72 79 20 64 61 74 61 62 61 73 65 20 6e 65  rary database ne
2dddd 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ver needs to be 
2ddde 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a  committed..  */.
2dddf 20 20 7b 0a 20 20 20 20 75 33 32 20 6d 65 74 61    {.    u32 meta
2dde0 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ;.    int i;..  
2dde1 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61 79 20    /* This array 
2dde2 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68  determines which
2dde3 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c 75 65   meta meta value
2dde4 73 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20  s are preserved 
2dde5 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 61  in the.    ** va
2dde6 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e 74 72  cuum.  Even entr
2dde7 69 65 73 20 61 72 65 20 74 68 65 20 6d 65 74 61  ies are the meta
2dde8 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20 61 6e   value number an
2dde9 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a 20 20  d odd entries.  
2ddea 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e 63 72    ** are an incr
2ddeb 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79 20 74  ement to apply t
2ddec 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75 65  o the meta value
2dded 20 61 66 74 65 72 20 74 68 65 20 76 61 63 75 75   after the vacuu
2ddee 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 69 6e  m..    ** The in
2ddef 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64 20  crement is used 
2ddf0 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  to increase the 
2ddf1 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 73 6f  schema cookie so
2ddf2 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20 20 20   that other.    
2ddf3 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  ** connections t
2ddf4 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  o the same datab
2ddf5 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f  ase will know to
2ddf6 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68 65   reread the sche
2ddf7 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ma..    */.    s
2ddf8 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
2ddf9 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70 79 5b  gned char aCopy[
2ddfa 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 42 54 52  ] = {.       BTR
2ddfb 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  EE_SCHEMA_VERSIO
2ddfc 4e 2c 20 20 20 20 20 31 2c 20 20 2f 2a 20 41 64  N,     1,  /* Ad
2ddfd 64 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64  d one to the old
2ddfe 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a   schema cookie *
2ddff 2f 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 44  /.       BTREE_D
2de00 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
2de01 45 2c 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72  E, 0,  /* Preser
2de02 76 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70  ve the default p
2de03 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 20 2a  age cache size *
2de04 2f 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 54  /.       BTREE_T
2de05 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 20 20  EXT_ENCODING,   
2de06 20 20 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72     0,  /* Preser
2de07 76 65 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ve the text enco
2de08 64 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 42  ding */.       B
2de09 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f  TREE_USER_VERSIO
2de0a 4e 2c 20 20 20 20 20 20 20 30 2c 20 20 2f 2a 20  N,       0,  /* 
2de0b 50 72 65 73 65 72 76 65 20 74 68 65 20 75 73 65  Preserve the use
2de0c 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20  r version */.   
2de0d 20 7d 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28   };..    assert(
2de0e 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65   1==sqlite3Btree
2de0f 49 73 49 6e 54 72 61 6e 73 28 70 54 65 6d 70 29  IsInTrans(pTemp)
2de10 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2de11 31 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49  1==sqlite3BtreeI
2de12 73 49 6e 54 72 61 6e 73 28 70 4d 61 69 6e 29 20  sInTrans(pMain) 
2de13 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20  );..    /* Copy 
2de14 42 74 72 65 65 20 6d 65 74 61 20 76 61 6c 75 65  Btree meta value
2de15 73 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30  s */.    for(i=0
2de16 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 43  ; i<ArraySize(aC
2de17 6f 70 79 29 3b 20 69 2b 3d 32 29 7b 0a 20 20 20  opy); i+=2){.   
2de18 20 20 20 2f 2a 20 47 65 74 4d 65 74 61 28 29 20     /* GetMeta() 
2de19 61 6e 64 20 55 70 64 61 74 65 4d 65 74 61 28 29  and UpdateMeta()
2de1a 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 69 6e 20   cannot fail in 
2de1b 74 68 69 73 20 63 6f 6e 74 65 78 74 20 62 65 63  this context bec
2de1c 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 77 65  ause.      ** we
2de1d 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 70 61   already have pa
2de1e 67 65 20 31 20 6c 6f 61 64 65 64 20 69 6e 74 6f  ge 1 loaded into
2de1f 20 63 61 63 68 65 20 61 6e 64 20 6d 61 72 6b 65   cache and marke
2de20 64 20 64 69 72 74 79 2e 20 2a 2f 0a 20 20 20 20  d dirty. */.    
2de21 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
2de22 74 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f  tMeta(pMain, aCo
2de23 70 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20  py[i], &meta);. 
2de24 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2de25 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
2de26 28 70 54 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d  (pTemp, aCopy[i]
2de27 2c 20 6d 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31  , meta+aCopy[i+1
2de28 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 4e 45  ]);.      if( NE
2de29 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f  VER(rc!=SQLITE_O
2de2a 4b 29 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  K) ) goto end_of
2de2b 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a  _vacuum;.    }..
2de2c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2de2d 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
2de2e 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20  ain, pTemp);.   
2de2f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2de30 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2de31 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
2de32 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
2de33 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20  mmit(pTemp);.   
2de34 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2de35 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2de36 5f 76 61 63 75 75 6d 3b 0a 23 69 66 6e 64 65 66  _vacuum;.#ifndef
2de37 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2de38 4f 56 41 43 55 55 4d 0a 20 20 20 20 73 71 6c 69  OVACUUM.    sqli
2de39 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56  te3BtreeSetAutoV
2de3a 61 63 75 75 6d 28 70 4d 61 69 6e 2c 20 73 71 6c  acuum(pMain, sql
2de3b 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f  ite3BtreeGetAuto
2de3c 56 61 63 75 75 6d 28 70 54 65 6d 70 29 29 3b 0a  Vacuum(pTemp));.
2de3d 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73  #endif.  }..  as
2de3e 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
2de3f 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  _OK );.  rc = sq
2de40 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
2de41 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20 73 71 6c  eSize(pMain, sql
2de42 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
2de43 53 69 7a 65 28 70 54 65 6d 70 29 2c 20 6e 52 65  Size(pTemp), nRe
2de44 73 2c 31 29 3b 0a 0a 65 6e 64 5f 6f 66 5f 76 61  s,1);..end_of_va
2de45 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65 73 74 6f  cuum:.  /* Resto
2de46 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
2de47 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61  value of db->fla
2de48 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66 6c 61 67  gs */.  db->flag
2de49 73 20 3d 20 73 61 76 65 64 5f 66 6c 61 67 73 3b  s = saved_flags;
2de4a 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d  .  db->nChange =
2de4b 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0a   saved_nChange;.
2de4c 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e    db->nTotalChan
2de4d 67 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61  ge = saved_nTota
2de4e 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f 2a 20 43  lChange;..  /* C
2de4f 75 72 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69  urrently there i
2de50 73 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74  s an SQL level t
2de51 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
2de52 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20  on the vacuum.  
2de53 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20  ** database. No 
2de54 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f  locks are held o
2de55 6e 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65  n any other file
2de56 73 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69  s (since the mai
2de57 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20  n file.  ** was 
2de58 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65  committed at the
2de59 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53   btree level). S
2de5a 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64  o it safe to end
2de5b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2de5c 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c  .  ** by manuall
2de5d 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  y setting the au
2de5e 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f  toCommit flag to
2de5f 20 74 72 75 65 20 61 6e 64 20 64 65 74 61 63 68   true and detach
2de60 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63  ing the.  ** vac
2de61 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68  uum database. Th
2de62 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72  e vacuum_db jour
2de63 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65  nal file is dele
2de64 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  ted when the pag
2de65 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65  er.  ** is close
2de66 64 20 62 79 20 74 68 65 20 44 45 54 41 43 48 2e  d by the DETACH.
2de67 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f  .  */.  db->auto
2de68 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69  Commit = 1;..  i
2de69 66 28 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71  f( pDb ){.    sq
2de6a 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2de6b 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70  pDb->pBt);.    p
2de6c 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20  Db->pBt = 0;.   
2de6d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pDb->pSchema = 
2de6e 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  0;.  }..  sqlite
2de6f 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
2de70 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20  hema(db, 0);..  
2de71 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
2de72 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  dif  /* SQLITE_O
2de73 4d 49 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51  MIT_VACUUM && SQ
2de74 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
2de75 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2de76 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75  **** End of vacu
2de77 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
2de78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de7a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2de7b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2de7c 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vtab.c *********
2de7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de7f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
2de80 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68  June 10.**.** Th
2de81 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2de82 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2de83 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2de84 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2de85 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2de86 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2de87 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2de88 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2de89 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2de8a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2de8b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2de8c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2de8d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2de8e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2de8f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2de90 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2de91 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2de92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2de96 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2de97 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
2de98 65 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  e used to help i
2de99 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c  mplement virtual
2de9a 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 23 69 66 6e   tables..*/.#ifn
2de9b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2de9c 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 0a 2f 2a  VIRTUALTABLE../*
2de9d 0a 2a 2a 20 54 68 65 20 61 63 74 75 61 6c 20 66  .** The actual f
2de9e 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65  unction that doe
2de9f 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 63 72  s the work of cr
2dea0 65 61 74 69 6e 67 20 61 20 6e 65 77 20 6d 6f 64  eating a new mod
2dea1 75 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ule..** This fun
2dea2 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
2dea3 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65   the sqlite3_cre
2dea4 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 61 6e 64  ate_module() and
2dea5 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61  .** sqlite3_crea
2dea6 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69  te_module_v2() i
2dea7 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2f 0a 73 74  nterfaces..*/.st
2dea8 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 4d  atic int createM
2dea9 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
2deaa 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
2deab 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2deac 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f  base in which mo
2dead 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72  dule is register
2deae 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
2deaf 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
2deb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2deb1 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73  assigned to this
2deb2 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
2deb3 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
2deb4 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20  e *pModule,  /* 
2deb5 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  The definition o
2deb6 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
2deb7 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20    void *pAux,   
2deb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2deb9 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69    /* Context poi
2deba 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65  nter for xCreate
2debb 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76  /xConnect */.  v
2debc 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28  oid (*xDestroy)(
2debd 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f  void *)        /
2debe 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63  * Module destruc
2debf 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  tor function */.
2dec0 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 4e 61  ){.  int rc, nNa
2dec1 6d 65 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  me;.  Module *pM
2dec2 6f 64 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  od;..  sqlite3_m
2dec3 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
2dec4 75 74 65 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d  utex);.  nName =
2dec5 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2dec6 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20  (zName);.  pMod 
2dec7 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
2dec8 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2dec9 62 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65  b, sizeof(Module
2deca 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a  ) + nName + 1);.
2decb 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20    if( pMod ){.  
2decc 20 20 4d 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a    Module *pDel;.
2decd 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 20      char *zCopy 
2dece 3d 20 28 63 68 61 72 20 2a 29 28 26 70 4d 6f 64  = (char *)(&pMod
2decf 5b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  [1]);.    memcpy
2ded0 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e  (zCopy, zName, n
2ded1 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f  Name+1);.    pMo
2ded2 64 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79  d->zName = zCopy
2ded3 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64  ;.    pMod->pMod
2ded4 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20  ule = pModule;. 
2ded5 20 20 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20     pMod->pAux = 
2ded6 70 41 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e  pAux;.    pMod->
2ded7 78 44 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74  xDestroy = xDest
2ded8 72 6f 79 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20  roy;.    pDel = 
2ded9 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
2deda 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d  3HashInsert(&db-
2dedb 3e 61 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c  >aModule, zCopy,
2dedc 20 6e 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70   nName, (void*)p
2dedd 4d 6f 64 29 3b 0a 20 20 20 20 69 66 28 20 70 44  Mod);.    if( pD
2dede 65 6c 20 26 26 20 70 44 65 6c 2d 3e 78 44 65 73  el && pDel->xDes
2dedf 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 44  troy ){.      pD
2dee0 65 6c 2d 3e 78 44 65 73 74 72 6f 79 28 70 44 65  el->xDestroy(pDe
2dee1 6c 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  l->pAux);.    }.
2dee2 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2dee3 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20  e(db, pDel);.   
2dee4 20 69 66 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20   if( pDel==pMod 
2dee5 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2dee6 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2dee7 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2dee8 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
2dee9 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 65  ema(db, 0);.  }e
2deea 6c 73 65 20 69 66 28 20 78 44 65 73 74 72 6f 79  lse if( xDestroy
2deeb 20 29 7b 0a 20 20 20 20 78 44 65 73 74 72 6f 79   ){.    xDestroy
2deec 28 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 72 63  (pAux);.  }.  rc
2deed 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2deee 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29  t(db, SQLITE_OK)
2deef 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2def0 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2def1 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
2def2 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72  .}.../*.** Exter
2def3 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  nal API function
2def4 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
2def5 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61  a new virtual-ta
2def6 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53  ble module..*/.S
2def7 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2def8 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
2def9 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ule(.  sqlite3 *
2defa 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2defb 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2defc 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75  se in which modu
2defd 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  le is registered
2defe 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2deff 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
2df00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73        /* Name as
2df01 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d  signed to this m
2df02 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
2df03 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2df04 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68  *pModule,  /* Th
2df05 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
2df06 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
2df07 76 6f 69 64 20 2a 70 41 75 78 20 20 20 20 20 20  void *pAux      
2df08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2df09 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  /* Context point
2df0a 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  er for xCreate/x
2df0b 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20  Connect */.){.  
2df0c 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64  return createMod
2df0d 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70  ule(db, zName, p
2df0e 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 30 29  Module, pAux, 0)
2df0f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72  ;.}../*.** Exter
2df10 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  nal API function
2df11 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
2df12 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61  a new virtual-ta
2df13 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53  ble module..*/.S
2df14 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2df15 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
2df16 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ule_v2(.  sqlite
2df17 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2df18 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2df19 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d  abase in which m
2df1a 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65  odule is registe
2df1b 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  red */.  const c
2df1c 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
2df1d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
2df1e 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69   assigned to thi
2df1f 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  s module */.  co
2df20 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
2df21 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a  le *pModule,  /*
2df22 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20   The definition 
2df23 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
2df24 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20  .  void *pAux,  
2df25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2df26 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f     /* Context po
2df27 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74  inter for xCreat
2df28 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20  e/xConnect */.  
2df29 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
2df2a 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20  (void *)        
2df2b 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75  /* Module destru
2df2c 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
2df2d 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65  .){.  return cre
2df2e 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e  ateModule(db, zN
2df2f 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41  ame, pModule, pA
2df30 75 78 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d  ux, xDestroy);.}
2df31 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
2df32 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73   virtual table s
2df33 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  o that it cannot
2df34 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64   be disconnected
2df35 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e  ..** Locks nest.
2df36 20 20 45 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f    Every lock sho
2df37 75 6c 64 20 68 61 76 65 20 61 20 63 6f 72 72 65  uld have a corre
2df38 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e  sponding unlock.
2df39 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b  .** If an unlock
2df3a 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 72 65 73   is omitted, res
2df3b 6f 75 72 63 65 73 20 6c 65 61 6b 73 20 77 69 6c  ources leaks wil
2df3c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a  l occur.  .**.**
2df3d 20 49 66 20 61 20 64 69 73 63 6f 6e 6e 65 63 74   If a disconnect
2df3e 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 77 68   is attempted wh
2df3f 69 6c 65 20 61 20 76 69 72 74 75 61 6c 20 74 61  ile a virtual ta
2df40 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a  ble is locked,.*
2df41 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e 65 63 74  * the disconnect
2df42 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74   is deferred unt
2df43 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76  il all locks hav
2df44 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a  e been removed..
2df45 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2df46 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2df47 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70  abLock(VTable *p
2df48 56 54 61 62 29 7b 0a 20 20 70 56 54 61 62 2d 3e  VTab){.  pVTab->
2df49 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  nRef++;.}.../*.*
2df4a 2a 20 70 54 61 62 20 69 73 20 61 20 70 6f 69 6e  * pTab is a poin
2df4b 74 65 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73  ter to a Table s
2df4c 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73 65  tructure represe
2df4d 6e 74 69 6e 67 20 61 20 76 69 72 74 75 61 6c 2d  nting a virtual-
2df4e 74 61 62 6c 65 2e 0a 2a 2a 20 52 65 74 75 72 6e  table..** Return
2df4f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2df50 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  e VTable object 
2df51 75 73 65 64 20 62 79 20 63 6f 6e 6e 65 63 74 69  used by connecti
2df52 6f 6e 20 64 62 20 74 6f 20 61 63 63 65 73 73 20  on db to access 
2df53 0a 2a 2a 20 74 68 69 73 20 76 69 72 74 75 61 6c  .** this virtual
2df54 2d 74 61 62 6c 65 2c 20 69 66 20 6f 6e 65 20 68  -table, if one h
2df55 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  as been created,
2df56 20 6f 72 20 4e 55 4c 4c 20 6f 74 68 65 72 77 69   or NULL otherwi
2df57 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
2df58 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71  IVATE VTable *sq
2df59 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73  lite3GetVTable(s
2df5a 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c  qlite3 *db, Tabl
2df5b 65 20 2a 70 54 61 62 29 7b 0a 20 20 56 54 61 62  e *pTab){.  VTab
2df5c 6c 65 20 2a 70 56 74 61 62 3b 0a 20 20 61 73 73  le *pVtab;.  ass
2df5d 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70  ert( IsVirtual(p
2df5e 54 61 62 29 20 29 3b 0a 20 20 66 6f 72 28 70 56  Tab) );.  for(pV
2df5f 74 61 62 3d 70 54 61 62 2d 3e 70 56 54 61 62 6c  tab=pTab->pVTabl
2df60 65 3b 20 70 56 74 61 62 20 26 26 20 70 56 74 61  e; pVtab && pVta
2df61 62 2d 3e 64 62 21 3d 64 62 3b 20 70 56 74 61 62  b->db!=db; pVtab
2df62 3d 70 56 74 61 62 2d 3e 70 4e 65 78 74 29 3b 0a  =pVtab->pNext);.
2df63 20 20 72 65 74 75 72 6e 20 70 56 74 61 62 3b 0a    return pVtab;.
2df64 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
2df65 6e 74 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74  nt the ref-count
2df66 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61   on a virtual ta
2df67 62 6c 65 20 6f 62 6a 65 63 74 2e 20 57 68 65 6e  ble object. When
2df68 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 0a 2a   the ref-count.*
2df69 2a 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20  * reaches zero, 
2df6a 63 61 6c 6c 20 74 68 65 20 78 44 69 73 63 6f 6e  call the xDiscon
2df6b 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 74 6f  nect() method to
2df6c 20 64 65 6c 65 74 65 20 74 68 65 20 6f 62 6a 65   delete the obje
2df6d 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
2df6e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2df6f 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61  e3VtabUnlock(VTa
2df70 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 73  ble *pVTab){.  s
2df71 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 56 54  qlite3 *db = pVT
2df72 61 62 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72  ab->db;..  asser
2df73 74 28 20 64 62 20 29 3b 0a 20 20 61 73 73 65 72  t( db );.  asser
2df74 74 28 20 70 56 54 61 62 2d 3e 6e 52 65 66 3e 30  t( pVTab->nRef>0
2df75 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
2df76 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
2df77 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 70 56 54  Ok(db) );..  pVT
2df78 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  ab->nRef--;.  if
2df79 28 20 70 56 54 61 62 2d 3e 6e 52 65 66 3d 3d 30  ( pVTab->nRef==0
2df7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2df7b 76 74 61 62 20 2a 70 20 3d 20 70 56 54 61 62 2d  vtab *p = pVTab-
2df7c 3e 70 56 74 61 62 3b 0a 20 20 20 20 69 66 28 20  >pVtab;.    if( 
2df7d 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  p ){.#ifdef SQLI
2df7e 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69  TE_DEBUG.      i
2df7f 66 28 20 70 56 54 61 62 2d 3e 64 62 2d 3e 6d 61  f( pVTab->db->ma
2df80 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic==SQLITE_MAGI
2df81 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20  C_BUSY ){.      
2df82 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2df83 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2df84 20 20 20 20 20 20 70 2d 3e 70 4d 6f 64 75 6c 65        p->pModule
2df85 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 29  ->xDisconnect(p)
2df86 3b 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64 29  ;.        (void)
2df87 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
2df88 64 62 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  db);.      } els
2df89 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b  e.#endif.      {
2df8a 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4d 6f 64  .        p->pMod
2df8b 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74  ule->xDisconnect
2df8c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
2df8d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2df8e 46 72 65 65 28 64 62 2c 20 70 56 54 61 62 29 3b  Free(db, pVTab);
2df8f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61  .  }.}../*.** Ta
2df90 62 6c 65 20 70 20 69 73 20 61 20 76 69 72 74 75  ble p is a virtu
2df91 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  al table. This f
2df92 75 6e 63 74 69 6f 6e 20 6d 6f 76 65 73 20 61 6c  unction moves al
2df93 6c 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  l elements in th
2df94 65 0a 2a 2a 20 70 2d 3e 70 56 54 61 62 6c 65 20  e.** p->pVTable 
2df95 6c 69 73 74 20 74 6f 20 74 68 65 20 73 71 6c 69  list to the sqli
2df96 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
2df97 6c 69 73 74 73 20 6f 66 20 74 68 65 69 72 20 61  lists of their a
2df98 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 64 61 74  ssociated.** dat
2df99 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2df9a 73 20 74 6f 20 62 65 20 64 69 73 63 6f 6e 6e 65  s to be disconne
2df9b 63 74 65 64 20 61 74 20 74 68 65 20 6e 65 78 74  cted at the next
2df9c 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 0a 2a   opportunity. .*
2df9d 2a 20 45 78 63 65 70 74 2c 20 69 66 20 61 72 67  * Except, if arg
2df9e 75 6d 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20  ument db is not 
2df9f 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 65  NULL, then the e
2dfa0 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  ntry associated 
2dfa1 77 69 74 68 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  with.** connecti
2dfa2 6f 6e 20 64 62 20 69 73 20 6c 65 66 74 20 69 6e  on db is left in
2dfa3 20 74 68 65 20 70 2d 3e 70 56 54 61 62 6c 65 20   the p->pVTable 
2dfa4 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
2dfa5 56 54 61 62 6c 65 20 2a 76 74 61 62 44 69 73 63  VTable *vtabDisc
2dfa6 6f 6e 6e 65 63 74 41 6c 6c 28 73 71 6c 69 74 65  onnectAll(sqlite
2dfa7 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 29  3 *db, Table *p)
2dfa8 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 52 65 74  {.  VTable *pRet
2dfa9 20 3d 20 30 3b 0a 20 20 56 54 61 62 6c 65 20 2a   = 0;.  VTable *
2dfaa 70 56 54 61 62 6c 65 20 3d 20 70 2d 3e 70 56 54  pVTable = p->pVT
2dfab 61 62 6c 65 3b 0a 20 20 70 2d 3e 70 56 54 61 62  able;.  p->pVTab
2dfac 6c 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41 73  le = 0;..  /* As
2dfad 73 65 72 74 20 74 68 61 74 20 74 68 65 20 6d 75  sert that the mu
2dfae 74 65 78 20 28 69 66 20 61 6e 79 29 20 61 73 73  tex (if any) ass
2dfaf 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2dfb0 20 42 74 53 68 61 72 65 64 20 64 61 74 61 62 61   BtShared databa
2dfb1 73 65 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f  se .  ** that co
2dfb2 6e 74 61 69 6e 73 20 74 61 62 6c 65 20 70 20 69  ntains table p i
2dfb3 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61  s held by the ca
2dfb4 6c 6c 65 72 2e 20 53 65 65 20 68 65 61 64 65 72  ller. See header
2dfb5 20 63 6f 6d 6d 65 6e 74 73 20 0a 20 20 2a 2a 20   comments .  ** 
2dfb6 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 73  above function s
2dfb7 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2dfb8 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 20 65 78  List() for an ex
2dfb9 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79  planation of why
2dfba 0a 20 20 2a 2a 20 74 68 69 73 20 6d 61 6b 65 73  .  ** this makes
2dfbb 20 69 74 20 73 61 66 65 20 74 6f 20 61 63 63 65   it safe to acce
2dfbc 73 73 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70  ss the sqlite3.p
2dfbd 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20  Disconnect list 
2dfbe 6f 66 20 61 6e 79 0a 20 20 2a 2a 20 64 61 74 61  of any.  ** data
2dfbf 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2dfc0 74 68 61 74 20 6d 61 79 20 68 61 76 65 20 61 6e  that may have an
2dfc1 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 2d   entry in the p-
2dfc2 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 2e 20 20  >pVTable list.  
2dfc3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d  */.  assert( db=
2dfc4 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65  =0 ||.    sqlite
2dfc5 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
2dfc6 28 64 62 2d 3e 61 44 62 5b 73 71 6c 69 74 65 33  (db->aDb[sqlite3
2dfc7 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2dfc8 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 5d 2e 70  , p->pSchema)].p
2dfc9 42 74 29 20 0a 20 20 29 3b 0a 0a 20 20 77 68 69  Bt) .  );..  whi
2dfca 6c 65 28 20 70 56 54 61 62 6c 65 20 29 7b 0a 20  le( pVTable ){. 
2dfcb 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 32 20     sqlite3 *db2 
2dfcc 3d 20 70 56 54 61 62 6c 65 2d 3e 64 62 3b 0a 20  = pVTable->db;. 
2dfcd 20 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74     VTable *pNext
2dfce 20 3d 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78   = pVTable->pNex
2dfcf 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  t;.    assert( d
2dfd0 62 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62  b2 );.    if( db
2dfd1 32 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20 20 70  2==db ){.      p
2dfd2 52 65 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20  Ret = pVTable;. 
2dfd3 20 20 20 20 20 70 2d 3e 70 56 54 61 62 6c 65 20       p->pVTable 
2dfd4 3d 20 70 52 65 74 3b 0a 20 20 20 20 20 20 70 52  = pRet;.      pR
2dfd5 65 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20  et->pNext = 0;. 
2dfd6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2dfd7 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20 3d  pVTable->pNext =
2dfd8 20 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63   db2->pDisconnec
2dfd9 74 3b 0a 20 20 20 20 20 20 64 62 32 2d 3e 70 44  t;.      db2->pD
2dfda 69 73 63 6f 6e 6e 65 63 74 20 3d 20 70 56 54 61  isconnect = pVTa
2dfdb 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ble;.    }.    p
2dfdc 56 54 61 62 6c 65 20 3d 20 70 4e 65 78 74 3b 0a  VTable = pNext;.
2dfdd 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21    }..  assert( !
2dfde 64 62 20 7c 7c 20 70 52 65 74 20 29 3b 0a 20 20  db || pRet );.  
2dfdf 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a  return pRet;.}..
2dfe0 0a 2f 2a 0a 2a 2a 20 44 69 73 63 6f 6e 6e 65 63  ./*.** Disconnec
2dfe1 74 20 61 6c 6c 20 74 68 65 20 76 69 72 74 75 61  t all the virtua
2dfe2 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 73 20  l table objects 
2dfe3 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70  in the sqlite3.p
2dfe4 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e  Disconnect list.
2dfe5 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
2dfe6 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65  tion may only be
2dfe7 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
2dfe8 20 6d 75 74 65 78 65 73 20 61 73 73 6f 63 69 61   mutexes associa
2dfe9 74 65 64 20 77 69 74 68 20 61 6c 6c 0a 2a 2a 20  ted with all.** 
2dfea 73 68 61 72 65 64 20 62 2d 74 72 65 65 20 64 61  shared b-tree da
2dfeb 74 61 62 61 73 65 73 20 6f 70 65 6e 65 64 20 75  tabases opened u
2dfec 73 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  sing connection 
2dfed 64 62 20 61 72 65 20 68 65 6c 64 20 62 79 20 74  db are held by t
2dfee 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54  he .** caller. T
2dfef 68 69 73 20 69 73 20 64 6f 6e 65 20 74 6f 20 70  his is done to p
2dff0 72 6f 74 65 63 74 20 74 68 65 20 73 71 6c 69 74  rotect the sqlit
2dff1 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c  e3.pDisconnect l
2dff2 69 73 74 2e 20 54 68 65 0a 2a 2a 20 73 71 6c 69  ist. The.** sqli
2dff3 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
2dff4 6c 69 73 74 20 69 73 20 61 63 63 65 73 73 65 64  list is accessed
2dff5 20 6f 6e 6c 79 20 61 73 20 66 6f 6c 6c 6f 77 73   only as follows
2dff6 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 42 79 20  :.**.**   1) By 
2dff7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49  this function. I
2dff8 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61 6c 6c  n this case, all
2dff9 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65   BtShared mutexe
2dffa 73 20 61 6e 64 20 74 68 65 20 6d 75 74 65 78 0a  s and the mutex.
2dffb 2a 2a 20 20 20 20 20 20 61 73 73 6f 63 69 61 74  **      associat
2dffc 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
2dffd 62 61 73 65 20 68 61 6e 64 6c 65 20 69 74 73 65  base handle itse
2dffe 6c 66 20 6d 75 73 74 20 62 65 20 68 65 6c 64 2e  lf must be held.
2dfff 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20 66  .**.**   2) By f
2e000 75 6e 63 74 69 6f 6e 20 76 74 61 62 44 69 73 63  unction vtabDisc
2e001 6f 6e 6e 65 63 74 41 6c 6c 28 29 2c 20 77 68 65  onnectAll(), whe
2e002 6e 20 69 74 20 61 64 64 73 20 61 20 56 54 61 62  n it adds a VTab
2e003 6c 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 20  le entry to.**  
2e004 20 20 20 20 74 68 65 20 73 71 6c 69 74 65 33 2e      the sqlite3.
2e005 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74  pDisconnect list
2e006 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 65  . In this case e
2e007 69 74 68 65 72 20 74 68 65 20 42 74 53 68 61 72  ither the BtShar
2e008 65 64 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20  ed mutex.**     
2e009 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2e00a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
2e00b 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2e00c 69 73 20 73 74 6f 72 65 64 20 69 6e 20 69 73 20  is stored in is 
2e00d 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 72 2c  held.**      or,
2e00e 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   if the virtual 
2e00f 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20  table is stored 
2e010 69 6e 20 61 20 6e 6f 6e 2d 73 68 61 72 61 62 6c  in a non-sharabl
2e011 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  e database, then
2e012 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 64 61 74  .**      the dat
2e013 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74  abase handle mut
2e014 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a  ex is held..**.*
2e015 2a 20 41 73 20 61 20 72 65 73 75 6c 74 2c 20 61  * As a result, a
2e016 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2e017 6e 65 63 74 20 63 61 6e 6e 6f 74 20 62 65 20 61  nect cannot be a
2e018 63 63 65 73 73 65 64 20 73 69 6d 75 6c 74 61 6e  ccessed simultan
2e019 65 6f 75 73 6c 79 20 0a 2a 2a 20 62 79 20 6d 75  eously .** by mu
2e01a 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 2e 20  ltiple threads. 
2e01b 49 74 20 69 73 20 74 68 72 65 61 64 2d 73 61 66  It is thread-saf
2e01c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2e01d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2e01e 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28  3VtabUnlockList(
2e01f 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
2e020 56 54 61 62 6c 65 20 2a 70 20 3d 20 64 62 2d 3e  VTable *p = db->
2e021 70 44 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 64  pDisconnect;.  d
2e022 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20 3d  b->pDisconnect =
2e023 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   0;..  assert( s
2e024 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
2e025 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29  AllMutexes(db) )
2e026 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2e027 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
2e028 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
2e029 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c  if( p ){.    sql
2e02a 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
2e02b 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
2e02c 3b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  ;.    do {.     
2e02d 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d   VTable *pNext =
2e02e 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20   p->pNext;.     
2e02f 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
2e030 63 6b 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d  ck(p);.      p =
2e031 20 70 4e 65 78 74 3b 0a 20 20 20 20 7d 77 68 69   pNext;.    }whi
2e032 6c 65 28 20 70 20 29 3b 0a 20 20 7d 0a 7d 0a 0a  le( p );.  }.}..
2e033 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20  /*.** Clear any 
2e034 61 6e 64 20 61 6c 6c 20 76 69 72 74 75 61 6c 2d  and all virtual-
2e035 74 61 62 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f  table informatio
2e036 6e 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65  n from the Table
2e037 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 69 73   record..** This
2e038 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2e039 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
2e03a 20 6a 75 73 74 20 62 65 66 6f 72 65 20 64 65 6c   just before del
2e03b 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 0a  eting the Table.
2e03c 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a  ** record..**.**
2e03d 20 53 69 6e 63 65 20 69 74 20 69 73 20 61 20 76   Since it is a v
2e03e 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c 20 74 68  irtual-table, th
2e03f 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2e040 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  e contains a poi
2e041 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 68  nter.** to the h
2e042 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ead of a linked 
2e043 6c 69 73 74 20 6f 66 20 56 54 61 62 6c 65 20 73  list of VTable s
2e044 74 72 75 63 74 75 72 65 73 2e 20 45 61 63 68 20  tructures. Each 
2e045 56 54 61 62 6c 65 20 0a 2a 2a 20 73 74 72 75 63  VTable .** struc
2e046 74 75 72 65 20 69 73 20 61 73 73 6f 63 69 61 74  ture is associat
2e047 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  ed with a single
2e048 20 73 71 6c 69 74 65 33 2a 20 75 73 65 72 20 6f   sqlite3* user o
2e049 66 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a  f the schema..**
2e04a 20 54 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   The reference c
2e04b 6f 75 6e 74 20 6f 66 20 74 68 65 20 56 54 61 62  ount of the VTab
2e04c 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  le structure ass
2e04d 6f 63 69 61 74 65 64 20 77 69 74 68 20 64 61 74  ociated with dat
2e04e 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63  abase .** connec
2e04f 74 69 6f 6e 20 64 62 20 69 73 20 64 65 63 72 65  tion db is decre
2e050 6d 65 6e 74 65 64 20 69 6d 6d 65 64 69 61 74 65  mented immediate
2e051 6c 79 20 28 77 68 69 63 68 20 6d 61 79 20 6c 65  ly (which may le
2e052 61 64 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 74  ad to the .** st
2e053 72 75 63 74 75 72 65 20 62 65 69 6e 67 20 78 44  ructure being xD
2e054 69 73 63 6f 6e 6e 65 63 74 65 64 20 61 6e 64 20  isconnected and 
2e055 66 72 65 65 29 2e 20 41 6e 79 20 6f 74 68 65 72  free). Any other
2e056 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75 72   VTable structur
2e057 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73  es.** in the lis
2e058 74 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74  t are moved to t
2e059 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63  he sqlite3.pDisc
2e05a 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66 20 74  onnect list of t
2e05b 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a  he associated .*
2e05c 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
2e05d 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
2e05e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2e05f 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54  lite3VtabClear(T
2e060 61 62 6c 65 20 2a 70 29 7b 0a 20 20 76 74 61 62  able *p){.  vtab
2e061 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 30 2c  DisconnectAll(0,
2e062 20 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 7a   p);.  if( p->az
2e063 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20  ModuleArg ){.   
2e064 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
2e065 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c  i=0; i<p->nModul
2e066 65 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  eArg; i++){.    
2e067 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e068 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d  p->dbMem, p->azM
2e069 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a 20 20  oduleArg[i]);.  
2e06a 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2e06b 62 46 72 65 65 28 70 2d 3e 64 62 4d 65 6d 2c 20  bFree(p->dbMem, 
2e06c 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b  p->azModuleArg);
2e06d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
2e06e 64 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 61  d a new module a
2e06f 72 67 75 6d 65 6e 74 20 74 6f 20 70 54 61 62 6c  rgument to pTabl
2e070 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d  e->azModuleArg[]
2e071 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  ..** The string 
2e072 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 2d 20  is not copied - 
2e073 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73  the pointer is s
2e074 74 6f 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73  tored.  The.** s
2e075 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 66 72  tring will be fr
2e076 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  eed automaticall
2e077 79 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65  y when the table
2e078 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a   is.** deleted..
2e079 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
2e07a 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74  ddModuleArgument
2e07b 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61  (sqlite3 *db, Ta
2e07c 62 6c 65 20 2a 70 54 61 62 6c 65 2c 20 63 68 61  ble *pTable, cha
2e07d 72 20 2a 7a 41 72 67 29 7b 0a 20 20 69 6e 74 20  r *zArg){.  int 
2e07e 69 20 3d 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64  i = pTable->nMod
2e07f 75 6c 65 41 72 67 2b 2b 3b 0a 20 20 69 6e 74 20  uleArg++;.  int 
2e080 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28  nBytes = sizeof(
2e081 63 68 61 72 20 2a 29 2a 28 31 2b 70 54 61 62 6c  char *)*(1+pTabl
2e082 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a  e->nModuleArg);.
2e083 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c    char **azModul
2e084 65 41 72 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65  eArg;.  azModule
2e085 41 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62 52  Arg = sqlite3DbR
2e086 65 61 6c 6c 6f 63 28 64 62 2c 20 70 54 61 62 6c  ealloc(db, pTabl
2e087 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20  e->azModuleArg, 
2e088 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 61  nBytes);.  if( a
2e089 7a 4d 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b  zModuleArg==0 ){
2e08a 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20  .    int j;.    
2e08b 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b  for(j=0; j<i; j+
2e08c 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2e08d 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
2e08e 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  le->azModuleArg[
2e08f 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  j]);.    }.    s
2e090 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e091 20 7a 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69   zArg);.    sqli
2e092 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
2e093 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
2e094 67 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e  g);.    pTable->
2e095 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a  nModuleArg = 0;.
2e096 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 7a 4d    }else{.    azM
2e097 6f 64 75 6c 65 41 72 67 5b 69 5d 20 3d 20 7a 41  oduleArg[i] = zA
2e098 72 67 3b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65  rg;.    azModule
2e099 41 72 67 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20  Arg[i+1] = 0;.  
2e09a 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f  }.  pTable->azMo
2e09b 64 75 6c 65 41 72 67 20 3d 20 61 7a 4d 6f 64 75  duleArg = azModu
2e09c 6c 65 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  leArg;.}../*.** 
2e09d 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2e09e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
2e09f 65 6e 20 69 74 20 66 69 72 73 74 20 73 65 65 73  en it first sees
2e0a0 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41   a CREATE VIRTUA
2e0a1 4c 20 54 41 42 4c 45 0a 2a 2a 20 73 74 61 74 65  L TABLE.** state
2e0a2 6d 65 6e 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c  ment.  The modul
2e0a3 65 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20  e name has been 
2e0a4 70 61 72 73 65 64 2c 20 62 75 74 20 74 68 65 20  parsed, but the 
2e0a5 6f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a  optional list.**
2e0a6 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 74   of parameters t
2e0a7 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d  hat follow the m
2e0a8 6f 64 75 6c 65 20 6e 61 6d 65 20 61 72 65 20 73  odule name are s
2e0a9 74 69 6c 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f  till pending..*/
2e0aa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e0ab 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2e0ac 42 65 67 69 6e 50 61 72 73 65 28 0a 20 20 50 61  BeginParse(.  Pa
2e0ad 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2e0ae 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2e0af 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65  ontext */.  Toke
2e0b0 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20  n *pName1,      
2e0b1 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77    /* Name of new
2e0b2 20 74 61 62 6c 65 2c 20 6f 72 20 64 61 74 61 62   table, or datab
2e0b3 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f  ase name */.  To
2e0b4 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20  ken *pName2,    
2e0b5 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e      /* Name of n
2e0b6 65 77 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c  ew table or NULL
2e0b7 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f   */.  Token *pMo
2e0b8 64 75 6c 65 4e 61 6d 65 20 20 20 20 2f 2a 20 4e  duleName    /* N
2e0b9 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  ame of the modul
2e0ba 65 20 66 6f 72 20 74 68 65 20 76 69 72 74 75 61  e for the virtua
2e0bb 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  l table */.){.  
2e0bc 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
2e0bd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2e0be 61 62 61 73 65 20 74 68 65 20 74 61 62 6c 65 20  abase the table 
2e0bf 69 73 20 62 65 69 6e 67 20 63 72 65 61 74 65 64  is being created
2e0c0 20 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a   in */.  Table *
2e0c1 70 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 2f  pTable;        /
2e0c2 2a 20 54 68 65 20 6e 65 77 20 76 69 72 74 75 61  * The new virtua
2e0c3 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c  l table */.  sql
2e0c4 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2e0c5 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2e0c6 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
2e0c7 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
2e0c8 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
2e0c9 2c 20 70 4e 61 6d 65 32 2c 20 30 2c 20 30 2c 20  , pName2, 0, 0, 
2e0ca 31 2c 20 30 29 3b 0a 20 20 70 54 61 62 6c 65 20  1, 0);.  pTable 
2e0cb 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
2e0cc 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62 6c  ble;.  if( pTabl
2e0cd 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  e==0 ) return;. 
2e0ce 20 61 73 73 65 72 74 28 20 30 3d 3d 70 54 61 62   assert( 0==pTab
2e0cf 6c 65 2d 3e 70 49 6e 64 65 78 20 29 3b 0a 0a 20  le->pIndex );.. 
2e0d0 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2e0d1 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
2e0d2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2e0d3 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65  b, pTable->pSche
2e0d4 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ma);.  assert( i
2e0d5 44 62 3e 3d 30 20 29 3b 0a 0a 20 20 70 54 61 62  Db>=0 );..  pTab
2e0d6 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20  le->tabFlags |= 
2e0d7 54 46 5f 56 69 72 74 75 61 6c 3b 0a 20 20 70 54  TF_Virtual;.  pT
2e0d8 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  able->nModuleArg
2e0d9 20 3d 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c   = 0;.  addModul
2e0da 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54  eArgument(db, pT
2e0db 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d  able, sqlite3Nam
2e0dc 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
2e0dd 4d 6f 64 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20  ModuleName));.  
2e0de 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e  addModuleArgumen
2e0df 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71  t(db, pTable, sq
2e0e0 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2e0e1 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
2e0e2 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64  Name));.  addMod
2e0e3 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20  uleArgument(db, 
2e0e4 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44  pTable, sqlite3D
2e0e5 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62  bStrDup(db, pTab
2e0e6 6c 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70  le->zName));.  p
2e0e7 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
2e0e8 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 26 70 4d 6f  n.n = (int)(&pMo
2e0e9 64 75 6c 65 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f 64  duleName->z[pMod
2e0ea 75 6c 65 4e 61 6d 65 2d 3e 6e 5d 20 2d 20 70 4e  uleName->n] - pN
2e0eb 61 6d 65 31 2d 3e 7a 29 3b 0a 0a 23 69 66 6e 64  ame1->z);..#ifnd
2e0ec 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2e0ed 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f  UTHORIZATION.  /
2e0ee 2a 20 43 72 65 61 74 69 6e 67 20 61 20 76 69 72  * Creating a vir
2e0ef 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 76 6f 6b  tual table invok
2e0f0 65 73 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61  es the authoriza
2e0f1 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 74 77  tion callback tw
2e0f2 69 63 65 2e 0a 20 20 2a 2a 20 54 68 65 20 66 69  ice..  ** The fi
2e0f3 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 2c 20  rst invocation, 
2e0f4 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69 73  to obtain permis
2e0f5 73 69 6f 6e 20 74 6f 20 49 4e 53 45 52 54 20 61  sion to INSERT a
2e0f6 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 0a 20 20   row into the.  
2e0f7 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  ** sqlite_master
2e0f8 20 74 61 62 6c 65 2c 20 68 61 73 20 61 6c 72 65   table, has alre
2e0f9 61 64 79 20 62 65 65 6e 20 6d 61 64 65 20 62 79  ady been made by
2e0fa 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
2e0fb 6c 65 28 29 2e 0a 20 20 2a 2a 20 54 68 65 20 73  le()..  ** The s
2e0fc 65 63 6f 6e 64 20 63 61 6c 6c 2c 20 74 6f 20 6f  econd call, to o
2e0fd 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e  btain permission
2e0fe 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74   to create the t
2e0ff 61 62 6c 65 2c 20 69 73 20 6d 61 64 65 20 6e 6f  able, is made no
2e100 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  w..  */.  if( pT
2e101 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
2e102 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  g ){.    sqlite3
2e103 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2e104 2c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  , SQLITE_CREATE_
2e105 56 54 41 42 4c 45 2c 20 70 54 61 62 6c 65 2d 3e  VTABLE, pTable->
2e106 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20  zName, .        
2e107 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f      pTable->azMo
2e108 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 50 61 72  duleArg[0], pPar
2e109 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
2e10a 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e  .zName);.  }.#en
2e10b 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
2e10c 73 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20  s routine takes 
2e10d 74 68 65 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d  the module argum
2e10e 65 6e 74 20 74 68 61 74 20 68 61 73 20 62 65 65  ent that has bee
2e10f 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 0a 2a  n accumulating.*
2e110 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 41 72  * in pParse->zAr
2e111 67 5b 5d 20 61 6e 64 20 61 70 70 65 6e 64 73 20  g[] and appends 
2e112 69 74 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  it to the list o
2e113 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 74  f arguments on t
2e114 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  he.** virtual ta
2e115 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  ble currently un
2e116 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2e117 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 54 61 62   in pParse->pTab
2e118 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
2e119 69 64 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f  id addArgumentTo
2e11a 56 74 61 62 28 50 61 72 73 65 20 2a 70 50 61 72  Vtab(Parse *pPar
2e11b 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73  se){.  if( pPars
2e11c 65 2d 3e 73 41 72 67 2e 7a 20 26 26 20 41 4c 57  e->sArg.z && ALW
2e11d 41 59 53 28 70 50 61 72 73 65 2d 3e 70 4e 65 77  AYS(pParse->pNew
2e11e 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 63 6f  Table) ){.    co
2e11f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  nst char *z = (c
2e120 6f 6e 73 74 20 63 68 61 72 2a 29 70 50 61 72 73  onst char*)pPars
2e121 65 2d 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69  e->sArg.z;.    i
2e122 6e 74 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73  nt n = pParse->s
2e123 41 72 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74  Arg.n;.    sqlit
2e124 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e125 3e 64 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75  >db;.    addModu
2e126 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
2e127 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2e128 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  , sqlite3DbStrND
2e129 75 70 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20  up(db, z, n));. 
2e12a 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
2e12b 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2e12c 73 20 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20  s routine after 
2e12d 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55  the CREATE VIRTU
2e12e 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
2e12f 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63  nt.** has been c
2e130 6f 6d 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64  ompletely parsed
2e131 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e132 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2e133 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
2e134 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2e135 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54  oken *pEnd){.  T
2e136 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
2e137 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 20  rse->pNewTable; 
2e138 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
2e139 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20  ing constructed 
2e13a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2e13b 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
2e13c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2e13d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e13e 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 54 61 62  n */..  if( pTab
2e13f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
2e140 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61  addArgumentToVta
2e141 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61  b(pParse);.  pPa
2e142 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b  rse->sArg.z = 0;
2e143 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f  .  if( pTab->nMo
2e144 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74 75  duleArg<1 ) retu
2e145 72 6e 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74  rn;.  .  /* If t
2e146 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41  he CREATE VIRTUA
2e147 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  L TABLE statemen
2e148 74 20 69 73 20 62 65 69 6e 67 20 65 6e 74 65 72  t is being enter
2e149 65 64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  ed for the.  ** 
2e14a 66 69 72 73 74 20 74 69 6d 65 20 28 69 6e 20 6f  first time (in o
2e14b 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 74 68  ther words if th
2e14c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2e14d 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e  is actually bein
2e14e 67 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 6e  g.  ** created n
2e14f 6f 77 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75  ow instead of ju
2e150 73 74 20 62 65 69 6e 67 20 72 65 61 64 20 6f 75  st being read ou
2e151 74 20 6f 66 20 73 71 6c 69 74 65 5f 6d 61 73 74  t of sqlite_mast
2e152 65 72 29 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f  er) then.  ** do
2e153 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74   additional init
2e154 69 61 6c 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20  ialization work 
2e155 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 73 74  and store the st
2e156 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a  atement text.  *
2e157 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  * in the sqlite_
2e158 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20  master table..  
2e159 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
2e15a 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 63  it.busy ){.    c
2e15b 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20  har *zStmt;.    
2e15c 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20  char *zWhere;.  
2e15d 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 56    int iDb;.    V
2e15e 64 62 65 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20  dbe *v;..    /* 
2e15f 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70  Compute the comp
2e160 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
2e161 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2e162 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
2e163 2a 2f 0a 20 20 20 20 69 66 28 20 70 45 6e 64 20  */.    if( pEnd 
2e164 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
2e165 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20  >sNameToken.n = 
2e166 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20  (int)(pEnd->z - 
2e167 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
2e168 65 6e 2e 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b  en.z) + pEnd->n;
2e169 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74  .    }.    zStmt
2e16a 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2e16b 66 28 64 62 2c 20 22 43 52 45 41 54 45 20 56 49  f(db, "CREATE VI
2e16c 52 54 55 41 4c 20 54 41 42 4c 45 20 25 54 22 2c  RTUAL TABLE %T",
2e16d 20 26 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54   &pParse->sNameT
2e16e 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41  oken);..    /* A
2e16f 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65   slot for the re
2e170 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64 79  cord has already
2e171 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
2e172 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53  in the .    ** S
2e173 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
2e174 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65  le.  We just nee
2e175 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 61 74  d to update that
2e176 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20   slot with all. 
2e177 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d     ** the inform
2e178 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c  ation we've coll
2e179 65 63 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a  ected.  .    **.
2e17a 20 20 20 20 2a 2a 20 54 68 65 20 56 4d 20 72 65      ** The VM re
2e17b 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 70 50  gister number pP
2e17c 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 68  arse->regRowid h
2e17d 6f 6c 64 73 20 74 68 65 20 72 6f 77 69 64 20 6f  olds the rowid o
2e17e 66 20 61 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72  f an.    ** entr
2e17f 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  y in the sqlite_
2e180 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 68 74  master table tht
2e181 20 77 61 73 20 63 72 65 61 74 65 64 20 66 6f 72   was created for
2e182 20 74 68 69 73 20 76 74 61 62 0a 20 20 20 20 2a   this vtab.    *
2e183 2a 20 62 79 20 73 71 6c 69 74 65 33 53 74 61 72  * by sqlite3Star
2e184 74 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f  tTable()..    */
2e185 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
2e186 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2e187 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2e188 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e  a);.    sqlite3N
2e189 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
2e18a 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45  e,.      "UPDATE
2e18b 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20   %Q.%s ".       
2e18c 20 20 22 53 45 54 20 74 79 70 65 3d 27 74 61 62    "SET type='tab
2e18d 6c 65 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62  le', name=%Q, tb
2e18e 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70  l_name=%Q, rootp
2e18f 61 67 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a  age=0, sql=%Q ".
2e190 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72 6f         "WHERE ro
2e191 77 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20  wid=#%d",.      
2e192 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2e193 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
2e194 28 69 44 62 29 2c 0a 20 20 20 20 20 20 70 54 61  (iDb),.      pTa
2e195 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
2e196 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
2e197 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20     zStmt,.      
2e198 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64  pParse->regRowid
2e199 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69  .    );.    sqli
2e19a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
2e19b 74 6d 74 29 3b 0a 20 20 20 20 76 20 3d 20 73 71  tmt);.    v = sq
2e19c 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2e19d 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rse);.    sqlite
2e19e 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
2e19f 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20  arse, iDb);..   
2e1a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e1a1 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c  p2(v, OP_Expire,
2e1a2 20 30 2c 20 30 29 3b 0a 20 20 20 20 7a 57 68 65   0, 0);.    zWhe
2e1a3 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  re = sqlite3MPri
2e1a4 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25  ntf(db, "name='%
2e1a5 71 27 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  q'", pTab->zName
2e1a6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2e1a7 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
2e1a8 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
2e1a9 20 31 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50   1, 0, zWhere, P
2e1aa 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
2e1ab 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e1ac 34 28 76 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c  4(v, OP_VCreate,
2e1ad 20 69 44 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20   iDb, 0, 0, .   
2e1ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1af 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2e1b0 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2e1b1 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20  30(pTab->zName) 
2e1b2 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  + 1);.  }..  /* 
2e1b3 49 66 20 77 65 20 61 72 65 20 72 65 72 65 61 64  If we are reread
2e1b4 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ing the sqlite_m
2e1b5 61 73 74 65 72 20 74 61 62 6c 65 20 63 72 65 61  aster table crea
2e1b6 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  te the in-memory
2e1b7 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6f 66 20  .  ** record of 
2e1b8 74 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 78  the table. The x
2e1b9 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64  Connect() method
2e1ba 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75   is not called u
2e1bb 6e 74 69 6c 0a 20 20 2a 2a 20 74 68 65 20 66 69  ntil.  ** the fi
2e1bc 72 73 74 20 74 69 6d 65 20 74 68 65 20 76 69 72  rst time the vir
2e1bd 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 75 73  tual table is us
2e1be 65 64 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61  ed in an SQL sta
2e1bf 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 20 20 2a  tement. This.  *
2e1c0 2a 20 61 6c 6c 6f 77 73 20 61 20 73 63 68 65 6d  * allows a schem
2e1c1 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  a that contains 
2e1c2 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74  virtual tables t
2e1c3 6f 20 62 65 20 6c 6f 61 64 65 64 20 62 65 66 6f  o be loaded befo
2e1c4 72 65 0a 20 20 2a 2a 20 74 68 65 20 72 65 71 75  re.  ** the requ
2e1c5 69 72 65 64 20 76 69 72 74 75 61 6c 20 74 61 62  ired virtual tab
2e1c6 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
2e1c7 6e 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65  ns are registere
2e1c8 64 2e 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b 0a  d.  */.  else {.
2e1c9 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b      Table *pOld;
2e1ca 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63  .    Schema *pSc
2e1cb 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
2e1cc 68 65 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74 20  hema;.    const 
2e1cd 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54  char *zName = pT
2e1ce 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69  ab->zName;.    i
2e1cf 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
2e1d0 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2e1d1 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71  );.    pOld = sq
2e1d2 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2e1d3 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
2e1d4 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  h, zName, nName,
2e1d5 20 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20   pTab);.    if( 
2e1d6 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 64 62  pOld ){.      db
2e1d7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2e1d8 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   1;.      assert
2e1d9 28 20 70 54 61 62 3d 3d 70 4f 6c 64 20 29 3b 20  ( pTab==pOld ); 
2e1da 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20   /* Malloc must 
2e1db 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69  have failed insi
2e1dc 64 65 20 48 61 73 68 49 6e 73 65 72 74 28 29 20  de HashInsert() 
2e1dd 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  */.      return;
2e1de 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 63 68 65  .    }.    pSche
2e1df 6d 61 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d  ma->db = pParse-
2e1e0 3e 64 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  >db;.    pParse-
2e1e1 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a  >pNewTable = 0;.
2e1e2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
2e1e3 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
2e1e4 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
2e1e5 69 74 20 73 65 65 73 20 74 68 65 20 66 69 72 73  it sees the firs
2e1e6 74 20 74 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e  t token.** of an
2e1e7 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
2e1e8 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20   module name in 
2e1e9 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  a CREATE VIRTUAL
2e1ea 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2e1eb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e1ec 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2e1ed 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 73  VtabArgInit(Pars
2e1ee 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 61 64  e *pParse){.  ad
2e1ef 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28  dArgumentToVtab(
2e1f0 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73  pParse);.  pPars
2e1f1 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20  e->sArg.z = 0;. 
2e1f2 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20   pParse->sArg.n 
2e1f3 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  = 0;.}../*.** Th
2e1f4 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
2e1f5 68 69 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  his routine for 
2e1f6 65 61 63 68 20 74 6f 6b 65 6e 20 61 66 74 65 72  each token after
2e1f7 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
2e1f8 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 67 75 6d 65  .** in an argume
2e1f9 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  nt to the module
2e1fa 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54   name in a CREAT
2e1fb 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2e1fc 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
2e1fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e1fe 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  d sqlite3VtabArg
2e1ff 45 78 74 65 6e 64 28 50 61 72 73 65 20 2a 70 50  Extend(Parse *pP
2e200 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b  arse, Token *p){
2e201 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 72 67 20 3d  .  Token *pArg =
2e202 20 26 70 50 61 72 73 65 2d 3e 73 41 72 67 3b 0a   &pParse->sArg;.
2e203 20 20 69 66 28 20 70 41 72 67 2d 3e 7a 3d 3d 30    if( pArg->z==0
2e204 20 29 7b 0a 20 20 20 20 70 41 72 67 2d 3e 7a 20   ){.    pArg->z 
2e205 3d 20 70 2d 3e 7a 3b 0a 20 20 20 20 70 41 72 67  = p->z;.    pArg
2e206 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65  ->n = p->n;.  }e
2e207 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
2e208 70 41 72 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b  pArg->z < p->z);
2e209 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28  .    pArg->n = (
2e20a 69 6e 74 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d  int)(&p->z[p->n]
2e20b 20 2d 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d   - pArg->z);.  }
2e20c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
2e20d 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2e20e 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69   constructor (ei
2e20f 74 68 65 72 20 78 43 72 65 61 74 65 20 6f 72 20  ther xCreate or 
2e210 78 43 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a  xConnect). The.*
2e211 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * pointer to the
2e212 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   function to inv
2e213 6f 6b 65 20 69 73 20 70 61 73 73 65 64 20 61 73  oke is passed as
2e214 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
2e215 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73  meter.** to this
2e216 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73   procedure..*/.s
2e217 74 61 74 69 63 20 69 6e 74 20 76 74 61 62 43 61  tatic int vtabCa
2e218 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20  llConstructor(. 
2e219 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
2e21a 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20   Table *pTab,.  
2e21b 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20  Module *pMod,.  
2e21c 69 6e 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74  int (*xConstruct
2e21d 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a  )(sqlite3*,void*
2e21e 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
2e21f 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76  const*,sqlite3_v
2e220 74 61 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20  tab**,char**),. 
2e221 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b   char **pzErr.){
2e222 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62  .  VTable *pVTab
2e223 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  le;.  int rc;.  
2e224 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
2e225 74 2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74  t*azArg = (const
2e226 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54   char *const*)pT
2e227 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b  ab->azModuleArg;
2e228 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70 54  .  int nArg = pT
2e229 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 0a  ab->nModuleArg;.
2e22a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
2e22b 3b 0a 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c  ;.  char *zModul
2e22c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  eName = sqlite3M
2e22d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
2e22e 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a   pTab->zName);..
2e22f 20 20 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e 61    if( !zModuleNa
2e230 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  me ){.    return
2e231 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2e232 20 7d 0a 0a 20 20 70 56 54 61 62 6c 65 20 3d 20   }..  pVTable = 
2e233 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2e234 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56  ero(db, sizeof(V
2e235 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21  Table));.  if( !
2e236 70 56 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73  pVTable ){.    s
2e237 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e238 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20   zModuleName);. 
2e239 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2e23a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 56  _NOMEM;.  }.  pV
2e23b 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0a  Table->db = db;.
2e23c 20 20 70 56 54 61 62 6c 65 2d 3e 70 4d 6f 64 20    pVTable->pMod 
2e23d 3d 20 70 4d 6f 64 3b 0a 0a 20 20 61 73 73 65 72  = pMod;..  asser
2e23e 74 28 20 21 64 62 2d 3e 70 56 54 61 62 20 29 3b  t( !db->pVTab );
2e23f 0a 20 20 61 73 73 65 72 74 28 20 78 43 6f 6e 73  .  assert( xCons
2e240 74 72 75 63 74 20 29 3b 0a 20 20 64 62 2d 3e 70  truct );.  db->p
2e241 56 54 61 62 20 3d 20 70 54 61 62 3b 0a 0a 20 20  VTab = pTab;..  
2e242 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 76 69  /* Invoke the vi
2e243 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73  rtual table cons
2e244 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20 28 76 6f  tructor */.  (vo
2e245 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2e246 4f 66 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20  Off(db);.  rc = 
2e247 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20 70  xConstruct(db, p
2e248 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c  Mod->pAux, nArg,
2e249 20 61 7a 41 72 67 2c 20 26 70 56 54 61 62 6c 65   azArg, &pVTable
2e24a 2d 3e 70 56 74 61 62 2c 20 26 7a 45 72 72 29 3b  ->pVtab, &zErr);
2e24b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
2e24c 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
2e24d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
2e24e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f  OMEM ) db->mallo
2e24f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 0a 20 20  cFailed = 1;..  
2e250 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
2e251 63 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72  c ){.    if( zEr
2e252 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  r==0 ){.      *p
2e253 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
2e254 72 69 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c  rintf(db, "vtabl
2e255 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61  e constructor fa
2e256 69 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75  iled: %s", zModu
2e257 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c  leName);.    }el
2e258 73 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  se {.      *pzEr
2e259 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
2e25a 74 66 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72  tf(db, "%s", zEr
2e25b 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
2e25c 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2e25d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2e25e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2e25f 56 54 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65  VTable);.  }else
2e260 20 69 66 28 20 41 4c 57 41 59 53 28 70 56 54 61   if( ALWAYS(pVTa
2e261 62 6c 65 2d 3e 70 56 74 61 62 29 20 29 7b 0a 20  ble->pVtab) ){. 
2e262 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74     /* Justificat
2e263 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a  ion of ALWAYS():
2e264 20 20 41 20 63 6f 72 72 65 63 74 20 76 74 61 62    A correct vtab
2e265 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 6d 75 73   constructor mus
2e266 74 20 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a  t allocate.    *
2e267 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  * the sqlite3_vt
2e268 61 62 20 6f 62 6a 65 63 74 20 69 66 20 73 75 63  ab object if suc
2e269 63 65 73 73 66 75 6c 2e 20 20 2a 2f 0a 20 20 20  cessful.  */.   
2e26a 20 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2d   pVTable->pVtab-
2e26b 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d  >pModule = pMod-
2e26c 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56  >pModule;.    pV
2e26d 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b  Table->nRef = 1;
2e26e 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 70 56 54  .    if( db->pVT
2e26f 61 62 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  ab ){.      cons
2e270 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20  t char *zFormat 
2e271 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72  = "vtable constr
2e272 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64 65  uctor did not de
2e273 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25 73  clare schema: %s
2e274 22 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20  ";.      *pzErr 
2e275 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2e276 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54  (db, zFormat, pT
2e277 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2e278 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2e279 6f 63 6b 28 70 56 54 61 62 6c 65 29 3b 0a 20 20  ock(pVTable);.  
2e27a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2e27b 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
2e27c 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
2e27d 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 76  ;.      /* If ev
2e27e 65 72 79 74 68 69 6e 67 20 77 65 6e 74 20 61 63  erything went ac
2e27f 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2c  cording to plan,
2e280 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 20 56 54   link the new VT
2e281 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 20  able structure. 
2e282 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65       ** into the
2e283 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61   linked list hea
2e284 64 65 64 20 62 79 20 70 54 61 62 2d 3e 70 56 54  ded by pTab->pVT
2e285 61 62 6c 65 2e 20 54 68 65 6e 20 6c 6f 6f 70 20  able. Then loop 
2e286 74 68 72 6f 75 67 68 20 74 68 65 20 0a 20 20 20  through the .   
2e287 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66     ** columns of
2e288 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 73 65   the table to se
2e289 65 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 6d  e if any of them
2e28a 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 6f 6b   contain the tok
2e28b 65 6e 20 22 68 69 64 64 65 6e 22 2e 0a 20 20 20  en "hidden"..   
2e28c 20 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65 74     ** If so, set
2e28d 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69 73 48 69   the Column.isHi
2e28e 64 64 65 6e 20 66 6c 61 67 20 61 6e 64 20 72 65  dden flag and re
2e28f 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65 6e 20 66  move the token f
2e290 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  rom.      ** the
2e291 20 74 79 70 65 20 73 74 72 69 6e 67 2e 20 20 2a   type string.  *
2e292 2f 0a 20 20 20 20 20 20 70 56 54 61 62 6c 65 2d  /.      pVTable-
2e293 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70  >pNext = pTab->p
2e294 56 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 54  VTable;.      pT
2e295 61 62 2d 3e 70 56 54 61 62 6c 65 20 3d 20 70 56  ab->pVTable = pV
2e296 54 61 62 6c 65 3b 0a 0a 20 20 20 20 20 20 66 6f  Table;..      fo
2e297 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
2e298 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
2e299 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72  +){.        char
2e29a 20 2a 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e   *zType = pTab->
2e29b 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65  aCol[iCol].zType
2e29c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54  ;.        int nT
2e29d 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ype;.        int
2e29e 20 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   i = 0;.        
2e29f 69 66 28 20 21 7a 54 79 70 65 20 29 20 63 6f 6e  if( !zType ) con
2e2a0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 6e  tinue;.        n
2e2a1 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Type = sqlite3St
2e2a2 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b 0a 20  rlen30(zType);. 
2e2a3 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
2e2a4 65 33 53 74 72 4e 49 43 6d 70 28 22 68 69 64 64  e3StrNICmp("hidd
2e2a5 65 6e 22 2c 20 7a 54 79 70 65 2c 20 36 29 7c 7c  en", zType, 6)||
2e2a6 28 7a 54 79 70 65 5b 36 5d 20 26 26 20 7a 54 79  (zType[6] && zTy
2e2a7 70 65 5b 36 5d 21 3d 27 20 27 29 20 29 7b 0a 20  pe[6]!=' ') ){. 
2e2a8 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30           for(i=0
2e2a9 3b 20 69 3c 6e 54 79 70 65 3b 20 69 2b 2b 29 7b  ; i<nType; i++){
2e2aa 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
2e2ab 20 28 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e   (0==sqlite3StrN
2e2ac 49 43 6d 70 28 22 20 68 69 64 64 65 6e 22 2c 20  ICmp(" hidden", 
2e2ad 26 7a 54 79 70 65 5b 69 5d 2c 20 37 29 29 0a 20  &zType[i], 7)). 
2e2ae 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
2e2af 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30 27  zType[i+7]=='\0'
2e2b0 20 7c 7c 20 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d   || zType[i+7]==
2e2b1 27 20 27 29 0a 20 20 20 20 20 20 20 20 20 20 20  ' ').           
2e2b2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2e2b3 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
2e2b4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e2b5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2e2b6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2e2b7 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 6e 54          if( i<nT
2e2b8 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ype ){.         
2e2b9 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20   int j;.        
2e2ba 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b    int nDel = 6 +
2e2bb 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31   (zType[i+6] ? 1
2e2bc 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20   : 0);.         
2e2bd 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65   for(j=i; (j+nDe
2e2be 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b  l)<=nType; j++){
2e2bf 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 79  .            zTy
2e2c0 70 65 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b  pe[j] = zType[j+
2e2c1 6e 44 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20  nDel];.         
2e2c2 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
2e2c3 20 7a 54 79 70 65 5b 69 5d 3d 3d 27 5c 30 27 20   zType[i]=='\0' 
2e2c4 26 26 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20  && i>0 ){.      
2e2c5 20 20 20 20 20 20 61 73 73 65 72 74 28 7a 54 79        assert(zTy
2e2c6 70 65 5b 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a 20  pe[i-1]==' ');. 
2e2c7 20 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65             zType
2e2c8 5b 69 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  [i-1] = '\0';.  
2e2c9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2e2ca 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69      pTab->aCol[i
2e2cb 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20  Col].isHidden = 
2e2cc 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
2e2cd 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2e2ce 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e2cf 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29  db, zModuleName)
2e2d0 3b 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20  ;.  db->pVTab = 
2e2d1 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  0;.  return rc;.
2e2d2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
2e2d3 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65  nction is invoke
2e2d4 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
2e2d5 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e  to call the xCon
2e2d6 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 0a 2a 2a  nect() method.**
2e2d7 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2e2d8 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20 61  table pTab. If a
2e2d9 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2e2da 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
2e2db 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 61 6e   returned .** an
2e2dc 64 20 61 6e 20 65 72 72 6f 72 20 6c 65 66 74 20  d an error left 
2e2dd 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a  in pParse..**.**
2e2de 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   This call is a 
2e2df 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c 65 20 70  no-op if table p
2e2e0 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69 72  Tab is not a vir
2e2e1 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  tual table..*/.S
2e2e2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e2e3 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  t sqlite3VtabCal
2e2e4 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 20 2a  lConnect(Parse *
2e2e5 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
2e2e6 54 61 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  Tab){.  sqlite3 
2e2e7 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2e2e8 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2e2e9 7a 4d 6f 64 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a  zMod;.  Module *
2e2ea 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  pMod;.  int rc;.
2e2eb 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20  .  assert( pTab 
2e2ec 29 3b 0a 20 20 69 66 28 20 28 70 54 61 62 2d 3e  );.  if( (pTab->
2e2ed 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
2e2ee 72 74 75 61 6c 29 3d 3d 30 20 7c 7c 20 73 71 6c  rtual)==0 || sql
2e2ef 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
2e2f0 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72  , pTab) ){.    r
2e2f1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e2f2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  .  }..  /* Locat
2e2f3 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20 76  e the required v
2e2f4 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
2e2f5 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d 20  ule */.  zMod = 
2e2f6 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
2e2f7 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  g[0];.  pMod = (
2e2f8 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33 48  Module*)sqlite3H
2e2f9 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f  ashFind(&db->aMo
2e2fa 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c 69  dule, zMod, sqli
2e2fb 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f 64  te3Strlen30(zMod
2e2fc 29 29 3b 0a 0a 20 20 69 66 28 20 21 70 4d 6f 64  ));..  if( !pMod
2e2fd 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
2e2fe 61 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54  ar *zModule = pT
2e2ff 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  ab->azModuleArg[
2e300 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0];.    sqlite3E
2e301 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2e302 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a  "no such module:
2e303 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a   %s", zModule);.
2e304 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2e305 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
2e306 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d      char *zErr =
2e307 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 76 74 61   0;.    rc = vta
2e308 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72  bCallConstructor
2e309 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c  (db, pTab, pMod,
2e30a 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e   pMod->pModule->
2e30b 78 43 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29  xConnect, &zErr)
2e30c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2e30d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2e30e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2e30f 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a  (pParse, "%s", z
2e310 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Err);.    }.    
2e311 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2e312 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20  , zErr);.  }..  
2e313 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2e314 0a 2a 2a 20 41 64 64 20 74 68 65 20 76 69 72 74  .** Add the virt
2e315 75 61 6c 20 74 61 62 6c 65 20 70 56 54 61 62 20  ual table pVTab 
2e316 74 6f 20 74 68 65 20 61 72 72 61 79 20 73 71 6c  to the array sql
2e317 69 74 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a  ite3.aVTrans[]..
2e318 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64  */.static int ad
2e319 64 54 6f 56 54 72 61 6e 73 28 73 71 6c 69 74 65  dToVTrans(sqlite
2e31a 33 20 2a 64 62 2c 20 56 54 61 62 6c 65 20 2a 70  3 *db, VTable *p
2e31b 56 54 61 62 29 7b 0a 20 20 63 6f 6e 73 74 20 69  VTab){.  const i
2e31c 6e 74 20 41 52 52 41 59 5f 49 4e 43 52 20 3d 20  nt ARRAY_INCR = 
2e31d 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f 77 20 74 68  5;..  /* Grow th
2e31e 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  e sqlite3.aVTran
2e31f 73 20 61 72 72 61 79 20 69 66 20 72 65 71 75 69  s array if requi
2e320 72 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  red */.  if( (db
2e321 2d 3e 6e 56 54 72 61 6e 73 25 41 52 52 41 59 5f  ->nVTrans%ARRAY_
2e322 49 4e 43 52 29 3d 3d 30 20 29 7b 0a 20 20 20 20  INCR)==0 ){.    
2e323 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e 73  VTable **aVTrans
2e324 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 73  ;.    int nBytes
2e325 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65   = sizeof(sqlite
2e326 33 5f 76 74 61 62 20 2a 29 20 2a 20 28 64 62 2d  3_vtab *) * (db-
2e327 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52 52 41 59  >nVTrans + ARRAY
2e328 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61 56 54 72  _INCR);.    aVTr
2e329 61 6e 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52  ans = sqlite3DbR
2e32a 65 61 6c 6c 6f 63 28 64 62 2c 20 28 76 6f 69 64  ealloc(db, (void
2e32b 20 2a 29 64 62 2d 3e 61 56 54 72 61 6e 73 2c 20   *)db->aVTrans, 
2e32c 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28  nBytes);.    if(
2e32d 20 21 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20   !aVTrans ){.   
2e32e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2e32f 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
2e330 20 20 6d 65 6d 73 65 74 28 26 61 56 54 72 61 6e    memset(&aVTran
2e331 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73 5d 2c 20  s[db->nVTrans], 
2e332 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  0, sizeof(sqlite
2e333 33 5f 76 74 61 62 20 2a 29 2a 41 52 52 41 59 5f  3_vtab *)*ARRAY_
2e334 49 4e 43 52 29 3b 0a 20 20 20 20 64 62 2d 3e 61  INCR);.    db->a
2e335 56 54 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73  VTrans = aVTrans
2e336 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20  ;.  }..  /* Add 
2e337 70 56 74 61 62 20 74 6f 20 74 68 65 20 65 6e 64  pVtab to the end
2e338 20 6f 66 20 73 71 6c 69 74 65 33 2e 61 56 54 72   of sqlite3.aVTr
2e339 61 6e 73 20 2a 2f 0a 20 20 64 62 2d 3e 61 56 54  ans */.  db->aVT
2e33a 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73  rans[db->nVTrans
2e33b 2b 2b 5d 20 3d 20 70 56 54 61 62 3b 0a 20 20 73  ++] = pVTab;.  s
2e33c 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70  qlite3VtabLock(p
2e33d 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  VTab);.  return 
2e33e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2e33f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2e340 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20  n is invoked by 
2e341 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c  the vdbe to call
2e342 20 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 74   the xCreate met
2e343 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69  hod.** of the vi
2e344 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65  rtual table name
2e345 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61  d zTab in databa
2e346 73 65 20 69 44 62 2e 20 0a 2a 2a 0a 2a 2a 20 49  se iDb. .**.** I
2e347 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2e348 73 2c 20 2a 70 7a 45 72 72 20 69 73 20 73 65 74  s, *pzErr is set
2e349 20 74 6f 20 70 6f 69 6e 74 20 61 6e 20 61 6e 20   to point an an 
2e34a 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
2e34b 0a 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 20  .** description 
2e34c 6f 66 20 74 68 65 20 65 72 72 6f 72 20 61 6e 64  of the error and
2e34d 20 61 6e 20 53 51 4c 49 54 45 5f 58 58 58 20 65   an SQLITE_XXX e
2e34e 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
2e34f 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 69  urned..** In thi
2e350 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65  s case the calle
2e351 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69  r must call sqli
2e352 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
2e353 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a 53 51  on *pzErr..*/.SQ
2e354 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e355 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
2e356 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a  Create(sqlite3 *
2e357 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e  db, int iDb, con
2e358 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 63  st char *zTab, c
2e359 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20  har **pzErr){.  
2e35a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2e35b 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  OK;.  Table *pTa
2e35c 62 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f  b;.  Module *pMo
2e35d 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  d;.  const char 
2e35e 2a 7a 4d 6f 64 3b 0a 0a 20 20 70 54 61 62 20 3d  *zMod;..  pTab =
2e35f 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2e360 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e  e(db, zTab, db->
2e361 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
2e362 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20  .  assert( pTab 
2e363 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  && (pTab->tabFla
2e364 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
2e365 21 3d 30 20 26 26 20 21 70 54 61 62 2d 3e 70 56  !=0 && !pTab->pV
2e366 54 61 62 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 4c  Table );..  /* L
2e367 6f 63 61 74 65 20 74 68 65 20 72 65 71 75 69 72  ocate the requir
2e368 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ed virtual table
2e369 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f   module */.  zMo
2e36a 64 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75  d = pTab->azModu
2e36b 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64  leArg[0];.  pMod
2e36c 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69   = (Module*)sqli
2e36d 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
2e36e 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20  >aModule, zMod, 
2e36f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2e370 7a 4d 6f 64 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  zMod));..  /* If
2e371 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20   the module has 
2e372 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 20  been registered 
2e373 61 6e 64 20 69 6e 63 6c 75 64 65 73 20 61 20 43  and includes a C
2e374 72 65 61 74 65 20 6d 65 74 68 6f 64 2c 20 0a 20  reate method, . 
2e375 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e 6f   ** invoke it no
2e376 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65  w. If the module
2e377 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 72 65   has not been re
2e378 67 69 73 74 65 72 65 64 2c 20 72 65 74 75 72 6e  gistered, return
2e379 20 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f 72 2e   an .  ** error.
2e37a 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f 20 6e   Otherwise, do n
2e37b 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69  othing..  */.  i
2e37c 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20  f( !pMod ){.    
2e37d 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
2e37e 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20  MPrintf(db, "no 
2e37f 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22  such module: %s"
2e380 2c 20 7a 4d 6f 64 29 3b 0a 20 20 20 20 72 63 20  , zMod);.    rc 
2e381 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2e382 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
2e383 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72  = vtabCallConstr
2e384 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62 2c 20  uctor(db, pTab, 
2e385 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64  pMod, pMod->pMod
2e386 75 6c 65 2d 3e 78 43 72 65 61 74 65 2c 20 70 7a  ule->xCreate, pz
2e387 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Err);.  }..  /* 
2e388 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66  Justification of
2e389 20 41 4c 57 41 59 53 28 29 3a 20 20 54 68 65 20   ALWAYS():  The 
2e38a 78 43 6f 6e 73 74 72 75 63 74 6f 72 20 6d 65 74  xConstructor met
2e38b 68 6f 64 20 69 73 20 72 65 71 75 69 72 65 64 20  hod is required 
2e38c 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61  to.  ** create a
2e38d 20 76 61 6c 69 64 20 73 71 6c 69 74 65 33 5f 76   valid sqlite3_v
2e38e 74 61 62 20 69 66 20 69 74 20 72 65 74 75 72 6e  tab if it return
2e38f 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 2a 2f 0a  s SQLITE_OK. */.
2e390 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2e391 5f 4f 4b 20 26 26 20 41 4c 57 41 59 53 28 73 71  _OK && ALWAYS(sq
2e392 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2e393 62 2c 20 70 54 61 62 29 29 20 29 7b 0a 20 20 20  b, pTab)) ){.   
2e394 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54 72     rc = addToVTr
2e395 61 6e 73 28 64 62 2c 20 73 71 6c 69 74 65 33 47  ans(db, sqlite3G
2e396 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
2e397 62 29 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  b));.  }..  retu
2e398 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2e399 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2e39a 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65   used to set the
2e39b 20 73 63 68 65 6d 61 20 6f 66 20 61 20 76 69 72   schema of a vir
2e39c 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 49 74 20  tual table.  It 
2e39d 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c 69 64  is only.** valid
2e39e 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75   to call this fu
2e39f 6e 63 74 69 6f 6e 20 66 72 6f 6d 20 77 69 74 68  nction from with
2e3a0 69 6e 20 74 68 65 20 78 43 72 65 61 74 65 28 29  in the xCreate()
2e3a1 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f   or xConnect() o
2e3a2 66 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  f a.** virtual t
2e3a3 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  able module..*/.
2e3a4 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2e3a5 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
2e3a6 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  tab(sqlite3 *db,
2e3a7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 72   const char *zCr
2e3a8 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20 50 61  eateTable){.  Pa
2e3a9 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20  rse *pParse;..  
2e3aa 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2e3ab 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  OK;.  Table *pTa
2e3ac 62 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20  b;.  char *zErr 
2e3ad 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
2e3ae 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2e3af 6d 75 74 65 78 29 3b 0a 20 20 70 54 61 62 20 3d  mutex);.  pTab =
2e3b0 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69 66   db->pVTab;.  if
2e3b1 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 73  ( !pTab ){.    s
2e3b2 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2e3b3 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30  SQLITE_MISUSE, 0
2e3b4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
2e3b5 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2e3b6 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72  utex);.    retur
2e3b7 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
2e3b8 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
2e3b9 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2e3ba 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20   TF_Virtual)!=0 
2e3bb 29 3b 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 73  );..  pParse = s
2e3bc 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
2e3bd 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2e3be 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28  *pParse));.  if(
2e3bf 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20   pParse==0 ){.  
2e3c0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2e3c1 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  MEM;.  }else{.  
2e3c2 20 20 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72    pParse->declar
2e3c3 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 20 20 70  eVtab = 1;.    p
2e3c4 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a  Parse->db = db;.
2e3c5 20 20 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20    .    if( .    
2e3c6 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d      SQLITE_OK ==
2e3c7 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65   sqlite3RunParse
2e3c8 72 28 70 50 61 72 73 65 2c 20 7a 43 72 65 61 74  r(pParse, zCreat
2e3c9 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20 26  eTable, &zErr) &
2e3ca 26 20 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  & .        pPars
2e3cb 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 26 26 20  e->pNewTable && 
2e3cc 0a 20 20 20 20 20 20 20 20 21 70 50 61 72 73 65  .        !pParse
2e3cd 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65  ->pNewTable->pSe
2e3ce 6c 65 63 74 20 26 26 20 0a 20 20 20 20 20 20 20  lect && .       
2e3cf 20 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61   (pParse->pNewTa
2e3d0 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  ble->tabFlags & 
2e3d1 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20  TF_Virtual)==0. 
2e3d2 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20     ){.      if( 
2e3d3 21 70 54 61 62 2d 3e 61 43 6f 6c 20 29 7b 0a 20  !pTab->aCol ){. 
2e3d4 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f         pTab->aCo
2e3d5 6c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  l = pParse->pNew
2e3d6 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20  Table->aCol;.   
2e3d7 20 20 20 20 20 70 54 61 62 2d 3e 6e 43 6f 6c 20       pTab->nCol 
2e3d8 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
2e3d9 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20  ble->nCol;.     
2e3da 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54     pParse->pNewT
2e3db 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a  able->nCol = 0;.
2e3dc 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
2e3dd 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 20  pNewTable->aCol 
2e3de 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
2e3df 20 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30     db->pVTab = 0
2e3e0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
2e3e1 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2e3e2 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52  r(db, SQLITE_ERR
2e3e3 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  OR, zErr);.     
2e3e4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e3e5 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
2e3e6 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
2e3e7 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  R;.    }.    pPa
2e3e8 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62  rse->declareVtab
2e3e9 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 69 66 28   = 0;.  .    if(
2e3ea 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29   pParse->pVdbe )
2e3eb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2e3ec 64 62 65 46 69 6e 61 6c 69 7a 65 28 70 50 61 72  dbeFinalize(pPar
2e3ed 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20  se->pVdbe);.    
2e3ee 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  }.    sqlite3Del
2e3ef 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d  eteTable(pParse-
2e3f0 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 20  >pNewTable);.   
2e3f1 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
2e3f2 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
2e3f3 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72   }..  assert( (r
2e3f4 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20  c&0xff)==rc );. 
2e3f5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
2e3f6 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
2e3f7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2e3f8 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2e3f9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2e3fa 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2e3fb 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ion is invoked b
2e3fc 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61  y the vdbe to ca
2e3fd 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20  ll the xDestroy 
2e3fe 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
2e3ff 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e   virtual table n
2e400 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74  amed zTab in dat
2e401 61 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20  abase iDb. This 
2e402 6f 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61  occurs.** when a
2e403 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d   DROP TABLE is m
2e404 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  entioned..**.** 
2e405 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e  This call is a n
2e406 6f 2d 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20  o-op if zTab is 
2e407 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61  not a virtual ta
2e408 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
2e409 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2e40a 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
2e40b 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  y(sqlite3 *db, i
2e40c 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
2e40d 61 72 20 2a 7a 54 61 62 29 7b 0a 20 20 69 6e 74  ar *zTab){.  int
2e40e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2e40f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
2e410 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2e411 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
2e412 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  Tab, db->aDb[iDb
2e413 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ].zName);.  if( 
2e414 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 20 26  ALWAYS(pTab!=0 &
2e415 26 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 21  & pTab->pVTable!
2e416 3d 30 29 20 29 7b 0a 20 20 20 20 56 54 61 62 6c  =0) ){.    VTabl
2e417 65 20 2a 70 20 3d 20 76 74 61 62 44 69 73 63 6f  e *p = vtabDisco
2e418 6e 6e 65 63 74 41 6c 6c 28 64 62 2c 20 70 54 61  nnectAll(db, pTa
2e419 62 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71  b);..    rc = sq
2e41a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2e41b 62 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  b);.    assert( 
2e41c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
2e41d 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 70 4d 6f  .    rc = p->pMo
2e41e 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73  d->pModule->xDes
2e41f 74 72 6f 79 28 70 2d 3e 70 56 74 61 62 29 3b 0a  troy(p->pVtab);.
2e420 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2e421 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a  3SafetyOn(db);..
2e422 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68      /* Remove th
2e423 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20  e sqlite3_vtab* 
2e424 66 72 6f 6d 20 74 68 65 20 61 56 54 72 61 6e 73  from the aVTrans
2e425 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 61 70 70  [] array, if app
2e426 6c 69 63 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69  licable */.    i
2e427 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2e428 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
2e429 28 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3d  ( pTab->pVTable=
2e42a 3d 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d 3d  =p && p->pNext==
2e42b 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 56  0 );.      p->pV
2e42c 74 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  tab = 0;.      p
2e42d 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30  Tab->pVTable = 0
2e42e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2e42f 74 61 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20  tabUnlock(p);.  
2e430 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2e431 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2e432 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76  his function inv
2e433 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68 65 20  okes either the 
2e434 78 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f  xRollback or xCo
2e435 6d 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  mmit method.** o
2e436 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 76 69  f each of the vi
2e437 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
2e438 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
2e439 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20 6d  ans array. The m
2e43a 65 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64 20  ethod.** called 
2e43b 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
2e43c 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
2e43d 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c 20  ment, "offset", 
2e43e 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20  which is.** the 
2e43f 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d 65  offset of the me
2e440 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e 20  thod to call in 
2e441 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  the sqlite3_modu
2e442 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  le structure..**
2e443 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 69 73  .** The array is
2e444 20 63 6c 65 61 72 65 64 20 61 66 74 65 72 20 69   cleared after i
2e445 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c  nvoking the call
2e446 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74 69  backs. .*/.stati
2e447 63 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61 6c  c void callFinal
2e448 69 73 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62  iser(sqlite3 *db
2e449 2c 20 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a 20  , int offset){. 
2e44a 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 64 62   int i;.  if( db
2e44b 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20  ->aVTrans ){.   
2e44c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
2e44d 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20  nVTrans; i++){. 
2e44e 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56 54       VTable *pVT
2e44f 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73  ab = db->aVTrans
2e450 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  [i];.      sqlit
2e451 65 33 5f 76 74 61 62 20 2a 70 20 3d 20 70 56 54  e3_vtab *p = pVT
2e452 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20  ab->pVtab;.     
2e453 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
2e454 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74    int (*x)(sqlit
2e455 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20  e3_vtab *);.    
2e456 20 20 20 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a      x = *(int (*
2e457 2a 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  *)(sqlite3_vtab 
2e458 2a 29 29 28 28 63 68 61 72 20 2a 29 70 2d 3e 70  *))((char *)p->p
2e459 4d 6f 64 75 6c 65 20 2b 20 6f 66 66 73 65 74 29  Module + offset)
2e45a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 20  ;.        if( x 
2e45b 29 20 78 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a  ) x(p);.      }.
2e45c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61        sqlite3Vta
2e45d 62 55 6e 6c 6f 63 6b 28 70 56 54 61 62 29 3b 0a  bUnlock(pVTab);.
2e45e 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2e45f 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e  3DbFree(db, db->
2e460 61 56 54 72 61 6e 73 29 3b 0a 20 20 20 20 64 62  aVTrans);.    db
2e461 2d 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ->nVTrans = 0;. 
2e462 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d     db->aVTrans =
2e463 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
2e464 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 53 79 6e   Invoke the xSyn
2e465 63 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20  c method of all 
2e466 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
2e467 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  n the sqlite3.aV
2e468 54 72 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e 20  Trans.** array. 
2e469 52 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  Return the error
2e46a 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66 69   code for the fi
2e46b 72 73 74 20 65 72 72 6f 72 20 74 68 61 74 20 6f  rst error that o
2e46c 63 63 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51 4c  ccurs, or.** SQL
2e46d 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53  ITE_OK if all xS
2e46e 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ync operations a
2e46f 72 65 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a  re successful..*
2e470 2a 0a 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72 6d  *.** Set *pzErrm
2e471 73 67 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61  sg to point to a
2e472 20 62 75 66 66 65 72 20 74 68 61 74 20 73 68 6f   buffer that sho
2e473 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64 20  uld be released 
2e474 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  using .** sqlite
2e475 33 44 62 46 72 65 65 28 29 20 63 6f 6e 74 61 69  3DbFree() contai
2e476 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65  ning an error me
2e477 73 73 61 67 65 2c 20 69 66 20 6f 6e 65 20 69 73  ssage, if one is
2e478 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53   available..*/.S
2e479 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e47a 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e  t sqlite3VtabSyn
2e47b 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  c(sqlite3 *db, c
2e47c 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b  har **pzErrmsg){
2e47d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2e47e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2e47f 20 20 69 6e 74 20 72 63 73 61 66 65 74 79 3b 0a    int rcsafety;.
2e480 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61    VTable **aVTra
2e481 6e 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73  ns = db->aVTrans
2e482 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
2e483 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2e484 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20    db->aVTrans = 
2e485 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  0;.  for(i=0; rc
2e486 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
2e487 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b  <db->nVTrans; i+
2e488 2b 29 7b 0a 20 20 20 20 69 6e 74 20 28 2a 78 29  +){.    int (*x)
2e489 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2e48a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74  ;.    sqlite3_vt
2e48b 61 62 20 2a 70 56 74 61 62 20 3d 20 61 56 54 72  ab *pVtab = aVTr
2e48c 61 6e 73 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20  ans[i]->pVtab;. 
2e48d 20 20 20 69 66 28 20 70 56 74 61 62 20 26 26 20     if( pVtab && 
2e48e 28 78 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  (x = pVtab->pMod
2e48f 75 6c 65 2d 3e 78 53 79 6e 63 29 21 3d 30 20 29  ule->xSync)!=0 )
2e490 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 28 70  {.      rc = x(p
2e491 56 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  Vtab);.      sql
2e492 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a  ite3DbFree(db, *
2e493 70 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20  pzErrmsg);.     
2e494 20 2a 70 7a 45 72 72 6d 73 67 20 3d 20 70 56 74   *pzErrmsg = pVt
2e495 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20  ab->zErrMsg;.   
2e496 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73     pVtab->zErrMs
2e497 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
2e498 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d  .  db->aVTrans =
2e499 20 61 56 54 72 61 6e 73 3b 0a 20 20 72 63 73 61   aVTrans;.  rcsa
2e49a 66 65 74 79 20 3d 20 73 71 6c 69 74 65 33 53 61  fety = sqlite3Sa
2e49b 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69  fetyOn(db);..  i
2e49c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2e49d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63 73   ){.    rc = rcs
2e49e 61 66 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74  afety;.  }.  ret
2e49f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2e4a0 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c   Invoke the xRol
2e4a1 6c 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20  lback method of 
2e4a2 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c  all virtual tabl
2e4a3 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71  es in the .** sq
2e4a4 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72  lite3.aVTrans ar
2e4a5 72 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20  ray. Then clear 
2e4a6 74 68 65 20 61 72 72 61 79 20 69 74 73 65 6c 66  the array itself
2e4a7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e4a8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2e4a9 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69  tabRollback(sqli
2e4aa 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c  te3 *db){.  call
2e4ab 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66  Finaliser(db, of
2e4ac 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d  fsetof(sqlite3_m
2e4ad 6f 64 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29  odule,xRollback)
2e4ae 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
2e4af 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
2e4b0 49 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d  Invoke the xComm
2e4b1 69 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  it method of all
2e4b2 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2e4b3 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  in the .** sqlit
2e4b4 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2e4b5 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65  . Then clear the
2e4b6 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a   array itself..*
2e4b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e4b8 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
2e4b9 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
2e4ba 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c  db){.  callFinal
2e4bb 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f  iser(db, offseto
2e4bc 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  f(sqlite3_module
2e4bd 2c 78 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65  ,xCommit));.  re
2e4be 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2e4bf 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
2e4c0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56  virtual table pV
2e4c1 74 61 62 20 73 75 70 70 6f 72 74 73 20 74 68 65  tab supports the
2e4c2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74   transaction int
2e4c3 65 72 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69  erface.** (xBegi
2e4c4 6e 2f 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d  n/xRollback/xCom
2e4c5 6d 69 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c  mit and optional
2e4c6 6c 79 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20  ly xSync) and a 
2e4c7 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
2e4c8 2a 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  * not currently 
2e4c9 6f 70 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65  open, invoke the
2e4ca 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e   xBegin method n
2e4cb 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ow..**.** If the
2e4cc 20 78 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20   xBegin call is 
2e4cd 73 75 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63  successful, plac
2e4ce 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  e the sqlite3_vt
2e4cf 61 62 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e  ab pointer.** in
2e4d0 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
2e4d1 72 61 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53  rans array..*/.S
2e4d2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e4d3 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67  t sqlite3VtabBeg
2e4d4 69 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  in(sqlite3 *db, 
2e4d5 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a  VTable *pVTab){.
2e4d6 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2e4d7 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71  E_OK;.  const sq
2e4d8 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
2e4d9 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65  odule;..  /* Spe
2e4da 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 64 62  cial case: If db
2e4db 2d 3e 61 56 54 72 61 6e 73 20 69 73 20 4e 55 4c  ->aVTrans is NUL
2e4dc 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e  L and db->nVTran
2e4dd 73 20 69 73 20 67 72 65 61 74 65 72 0a 20 20 2a  s is greater.  *
2e4de 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  * than zero, the
2e4df 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
2e4e0 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  is being called 
2e4e1 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 0a 20 20  from within a.  
2e4e2 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c  ** virtual modul
2e4e3 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61  e xSync() callba
2e4e4 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61  ck. It is illega
2e4e5 6c 20 74 6f 20 77 72 69 74 65 20 74 6f 20 0a 20  l to write to . 
2e4e6 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75   ** virtual modu
2e4e7 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 69  le tables in thi
2e4e8 73 20 63 61 73 65 2c 20 73 6f 20 72 65 74 75 72  s case, so retur
2e4e9 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e  n SQLITE_LOCKED.
2e4ea 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
2e4eb 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62  te3VtabInSync(db
2e4ec 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2e4ed 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
2e4ee 20 7d 0a 20 20 69 66 28 20 21 70 56 54 61 62 20   }.  if( !pVTab 
2e4ef 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2e4f0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20  LITE_OK;.  } .  
2e4f1 70 4d 6f 64 75 6c 65 20 3d 20 70 56 54 61 62 2d  pModule = pVTab-
2e4f2 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  >pVtab->pModule;
2e4f3 0a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d  ..  if( pModule-
2e4f4 3e 78 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 69  >xBegin ){.    i
2e4f5 6e 74 20 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49  nt i;...    /* I
2e4f6 66 20 70 56 74 61 62 20 69 73 20 61 6c 72 65 61  f pVtab is alrea
2e4f7 64 79 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e  dy in the aVTran
2e4f8 73 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20  s array, return 
2e4f9 65 61 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72  early */.    for
2e4fa 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72  (i=0; i<db->nVTr
2e4fb 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ans; i++){.     
2e4fc 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73   if( db->aVTrans
2e4fd 5b 69 5d 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20  [i]==pVTab ){.  
2e4fe 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
2e4ff 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a  ITE_OK;.      }.
2e500 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e      }..    /* In
2e501 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20  voke the xBegin 
2e502 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63  method */.    rc
2e503 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67   = pModule->xBeg
2e504 69 6e 28 70 56 54 61 62 2d 3e 70 56 74 61 62 29  in(pVTab->pVtab)
2e505 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2e506 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2e507 20 72 63 20 3d 20 61 64 64 54 6f 56 54 72 61 6e   rc = addToVTran
2e508 73 28 64 62 2c 20 70 56 54 61 62 29 3b 0a 20 20  s(db, pVTab);.  
2e509 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2e50a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
2e50b 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
2e50c 72 20 28 70 44 65 66 29 20 69 73 20 61 20 66 75  r (pDef) is a fu
2e50d 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
2e50e 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73  ation.  The.** s
2e50f 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
2e510 28 70 45 78 70 72 29 20 69 73 20 74 68 65 20 66  (pExpr) is the f
2e511 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
2e512 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
2e513 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20 61  ** If pExpr is a
2e514 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
2e515 74 75 61 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e  tual table, then
2e516 20 6c 65 74 20 74 68 65 20 76 69 72 74 75 61 6c   let the virtual
2e517 0a 2a 2a 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d  .** table implem
2e518 65 6e 74 61 74 69 6f 6e 20 68 61 76 65 20 61 6e  entation have an
2e519 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
2e51a 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e  overload the fun
2e51b 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ction..**.** Thi
2e51c 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
2e51d 64 20 74 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75  d to allow virtu
2e51e 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
2e51f 6e 74 61 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f  ntations to.** o
2e520 76 65 72 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c  verload MATCH, L
2e521 49 4b 45 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52  IKE, GLOB, and R
2e522 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72 73 2e  EGEXP operators.
2e523 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69  .**.** Return ei
2e524 74 68 65 72 20 74 68 65 20 70 44 65 66 20 61 72  ther the pDef ar
2e525 67 75 6d 65 6e 74 20 28 69 6e 64 69 63 61 74 69  gument (indicati
2e526 6e 67 20 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72  ng no change) or
2e527 20 61 20 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44   a .** new FuncD
2e528 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ef structure tha
2e529 74 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 65  t is marked as e
2e52a 70 68 65 6d 65 72 61 6c 20 75 73 69 6e 67 20 74  phemeral using t
2e52b 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e  he.** SQLITE_FUN
2e52c 43 5f 45 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f  C_EPHEM flag..*/
2e52d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e52e 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33  FuncDef *sqlite3
2e52f 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63  VtabOverloadFunc
2e530 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  tion(.  sqlite3 
2e531 2a 64 62 2c 20 20 20 20 2f 2a 20 44 61 74 61 62  *db,    /* Datab
2e532 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66  ase connection f
2e533 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c  or reporting mal
2e534 6c 6f 63 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a  loc problems */.
2e535 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 2c    FuncDef *pDef,
2e536 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f    /* Function to
2e537 20 70 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f   possibly overlo
2e538 61 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67  ad */.  int nArg
2e539 2c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ,       /* Numbe
2e53a 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
2e53b 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
2e53c 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
2e53d 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
2e53e 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e  ument to the fun
2e53f 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61  ction */.){.  Ta
2e540 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c  ble *pTab;.  sql
2e541 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2e542 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ;.  sqlite3_modu
2e543 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64  le *pMod;.  void
2e544 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
2e545 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
2e546 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 20  qlite3_value**) 
2e547 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72  = 0;.  void *pAr
2e548 67 20 3d 20 30 3b 0a 20 20 46 75 6e 63 44 65 66  g = 0;.  FuncDef
2e549 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63   *pNew;.  int rc
2e54a 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c   = 0;.  char *zL
2e54b 6f 77 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69  owerName;.  unsi
2e54c 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a  gned char *z;...
2e54d 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
2e54e 65 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61  e the left opera
2e54f 6e 64 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69  nd is a column i
2e550 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
2e551 65 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52  e */.  if( NEVER
2e552 28 70 45 78 70 72 3d 3d 30 29 20 29 20 72 65 74  (pExpr==0) ) ret
2e553 75 72 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20  urn pDef;.  if( 
2e554 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f  pExpr->op!=TK_CO
2e555 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44  LUMN ) return pD
2e556 65 66 3b 0a 20 20 70 54 61 62 20 3d 20 70 45 78  ef;.  pTab = pEx
2e557 70 72 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28 20  pr->pTab;.  if( 
2e558 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29  NEVER(pTab==0) )
2e559 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2e55a 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c  if( (pTab->tabFl
2e55b 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
2e55c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44  )==0 ) return pD
2e55d 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20 73 71  ef;.  pVtab = sq
2e55e 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2e55f 62 2c 20 70 54 61 62 29 2d 3e 70 56 74 61 62 3b  b, pTab)->pVtab;
2e560 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62  .  assert( pVtab
2e561 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
2e562 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 21   pVtab->pModule!
2e563 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  =0 );.  pMod = (
2e564 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
2e565 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  )pVtab->pModule;
2e566 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46 69  .  if( pMod->xFi
2e567 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29 20  ndFunction==0 ) 
2e568 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a 20  return pDef;. . 
2e569 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78 46 69   /* Call the xFi
2e56a 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f  ndFunction metho
2e56b 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c  d on the virtual
2e56c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
2e56d 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73 65  ation.  ** to se
2e56e 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65 6d 65  e if the impleme
2e56f 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20 74 6f  ntation wants to
2e570 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73 20 66   overload this f
2e571 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20  unction .  */.  
2e572 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71 6c  zLowerName = sql
2e573 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2e574 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pDef->zName);. 
2e575 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65 20   if( zLowerName 
2e576 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75 6e  ){.    for(z=(un
2e577 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f  signed char*)zLo
2e578 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b  werName; *z; z++
2e579 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73 71  ){.      *z = sq
2e57a 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
2e57b 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  r[*z];.    }.   
2e57c 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69 6e   rc = pMod->xFin
2e57d 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61 62 2c  dFunction(pVtab,
2e57e 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61 6d   nArg, zLowerNam
2e57f 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41 72 67  e, &xFunc, &pArg
2e580 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2e581 46 72 65 65 28 64 62 2c 20 7a 4c 6f 77 65 72 4e  Free(db, zLowerN
2e582 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ame);.  }.  if( 
2e583 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  rc==0 ){.    ret
2e584 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 0a 20  urn pDef;.  }.. 
2e585 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   /* Create a new
2e586 20 65 70 68 65 6d 65 72 61 6c 20 66 75 6e 63 74   ephemeral funct
2e587 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 66  ion definition f
2e588 6f 72 20 74 68 65 20 6f 76 65 72 6c 6f 61 64 65  or the overloade
2e589 64 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  d.  ** function 
2e58a 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  */.  pNew = sqli
2e58b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2e58c 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  db, sizeof(*pNew
2e58d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2e58e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b                 +
2e58f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2e590 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 2b 20  (pDef->zName) + 
2e591 31 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d  1);.  if( pNew==
2e592 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2e593 70 44 65 66 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65  pDef;.  }.  *pNe
2e594 77 20 3d 20 2a 70 44 65 66 3b 0a 20 20 70 4e 65  w = *pDef;.  pNe
2e595 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72  w->zName = (char
2e596 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d   *)&pNew[1];.  m
2e597 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d  emcpy(pNew->zNam
2e598 65 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  e, pDef->zName, 
2e599 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2e59a 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b  pDef->zName)+1);
2e59b 0a 20 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20 3d  .  pNew->xFunc =
2e59c 20 78 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e   xFunc;.  pNew->
2e59d 70 55 73 65 72 44 61 74 61 20 3d 20 70 41 72 67  pUserData = pArg
2e59e 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20  ;.  pNew->flags 
2e59f 7c 3d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45  |= SQLITE_FUNC_E
2e5a0 50 48 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 70  PHEM;.  return p
2e5a1 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  New;.}../*.** Ma
2e5a2 6b 65 20 73 75 72 65 20 76 69 72 74 75 61 6c 20  ke sure virtual 
2e5a3 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 63 6f  table pTab is co
2e5a4 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 70  ntained in the p
2e5a5 50 61 72 73 65 2d 3e 61 70 56 69 72 74 75 61 6c  Parse->apVirtual
2e5a6 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79 20  Lock[].** array 
2e5a7 73 6f 20 74 68 61 74 20 61 6e 20 4f 50 5f 56 42  so that an OP_VB
2e5a8 65 67 69 6e 20 77 69 6c 6c 20 67 65 74 20 67 65  egin will get ge
2e5a9 6e 65 72 61 74 65 64 20 66 6f 72 20 69 74 2e 20  nerated for it. 
2e5aa 20 41 64 64 20 70 54 61 62 20 74 6f 20 74 68 65   Add pTab to the
2e5ab 0a 2a 2a 20 61 72 72 61 79 20 69 66 20 69 74 20  .** array if it 
2e5ac 69 73 20 6d 69 73 73 69 6e 67 2e 20 20 49 66 20  is missing.  If 
2e5ad 70 54 61 62 20 69 73 20 61 6c 72 65 61 64 79 20  pTab is already 
2e5ae 69 6e 20 74 68 65 20 61 72 72 61 79 2c 20 74 68  in the array, th
2e5af 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
2e5b0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c   a no-op..*/.SQL
2e5b1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2e5b2 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65   sqlite3VtabMake
2e5b3 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 20 2a  Writable(Parse *
2e5b4 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
2e5b5 54 61 62 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  Tab){.  Parse *p
2e5b6 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74  Toplevel = sqlit
2e5b7 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
2e5b8 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 69  pParse);.  int i
2e5b9 2c 20 6e 3b 0a 20 20 54 61 62 6c 65 20 2a 2a 61  , n;.  Table **a
2e5ba 70 56 74 61 62 4c 6f 63 6b 3b 0a 0a 20 20 61 73  pVtabLock;..  as
2e5bb 73 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28  sert( IsVirtual(
2e5bc 70 54 61 62 29 20 29 3b 0a 20 20 66 6f 72 28 69  pTab) );.  for(i
2e5bd 3d 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d  =0; i<pToplevel-
2e5be 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29  >nVtabLock; i++)
2e5bf 7b 0a 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d  {.    if( pTab==
2e5c0 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61  pToplevel->apVta
2e5c1 62 4c 6f 63 6b 5b 69 5d 20 29 20 72 65 74 75 72  bLock[i] ) retur
2e5c2 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 70 54  n;.  }.  n = (pT
2e5c3 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f  oplevel->nVtabLo
2e5c4 63 6b 2b 31 29 2a 73 69 7a 65 6f 66 28 70 54 6f  ck+1)*sizeof(pTo
2e5c5 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f  plevel->apVtabLo
2e5c6 63 6b 5b 30 5d 29 3b 0a 20 20 61 70 56 74 61 62  ck[0]);.  apVtab
2e5c7 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 72  Lock = sqlite3_r
2e5c8 65 61 6c 6c 6f 63 28 70 54 6f 70 6c 65 76 65 6c  ealloc(pToplevel
2e5c9 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29  ->apVtabLock, n)
2e5ca 3b 0a 20 20 69 66 28 20 61 70 56 74 61 62 4c 6f  ;.  if( apVtabLo
2e5cb 63 6b 20 29 7b 0a 20 20 20 20 70 54 6f 70 6c 65  ck ){.    pTople
2e5cc 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20  vel->apVtabLock 
2e5cd 3d 20 61 70 56 74 61 62 4c 6f 63 6b 3b 0a 20 20  = apVtabLock;.  
2e5ce 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56    pToplevel->apV
2e5cf 74 61 62 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65  tabLock[pTopleve
2e5d0 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20  l->nVtabLock++] 
2e5d1 3d 20 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b  = pTab;.  }else{
2e5d2 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  .    pToplevel->
2e5d3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2e5d4 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e   = 1;.  }.}..#en
2e5d5 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2e5d6 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2e5d7 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
2e5d8 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 74 61 62 2e  *** End of vtab.
2e5d9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2e5da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5dc 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2e5dd 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 77  *** Begin file w
2e5de 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  here.c *********
2e5df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5e1 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2e5e2 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
2e5e3 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2e5e4 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2e5e5 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2e5e6 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2e5e7 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2e5e8 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2e5e9 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2e5ea 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2e5eb 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2e5ec 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2e5ed 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2e5ee 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2e5ef 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2e5f0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2e5f1 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2e5f2 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2e5f3 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2e5f4 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2e5f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e5f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2e5f9 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69  is module contai
2e5fa 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 67  ns C code that g
2e5fb 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f  enerates VDBE co
2e5fc 64 65 20 75 73 65 64 20 74 6f 20 70 72 6f 63 65  de used to proce
2e5fd 73 73 0a 2a 2a 20 74 68 65 20 57 48 45 52 45 20  ss.** the WHERE 
2e5fe 63 6c 61 75 73 65 20 6f 66 20 53 51 4c 20 73 74  clause of SQL st
2e5ff 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20  atements.  This 
2e600 6d 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e  module is respon
2e601 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 67 65 6e  sible for.** gen
2e602 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65  erating the code
2e603 20 74 68 61 74 20 6c 6f 6f 70 73 20 74 68 72 6f   that loops thro
2e604 75 67 68 20 61 20 74 61 62 6c 65 20 6c 6f 6f 6b  ugh a table look
2e605 69 6e 67 20 66 6f 72 20 61 70 70 6c 69 63 61 62  ing for applicab
2e606 6c 65 0a 2a 2a 20 72 6f 77 73 2e 20 20 49 6e 64  le.** rows.  Ind
2e607 69 63 65 73 20 61 72 65 20 73 65 6c 65 63 74 65  ices are selecte
2e608 64 20 61 6e 64 20 75 73 65 64 20 74 6f 20 73 70  d and used to sp
2e609 65 65 64 20 74 68 65 20 73 65 61 72 63 68 20 77  eed the search w
2e60a 68 65 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f 20  hen doing.** so 
2e60b 69 73 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20  is applicable.  
2e60c 42 65 63 61 75 73 65 20 74 68 69 73 20 6d 6f 64  Because this mod
2e60d 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ule is responsib
2e60e 6c 65 20 66 6f 72 20 73 65 6c 65 63 74 69 6e 67  le for selecting
2e60f 0a 2a 2a 20 69 6e 64 69 63 65 73 2c 20 79 6f 75  .** indices, you
2e610 20 6d 69 67 68 74 20 61 6c 73 6f 20 74 68 69 6e   might also thin
2e611 6b 20 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c 65  k of this module
2e612 20 61 73 20 74 68 65 20 22 71 75 65 72 79 20 6f   as the "query o
2e613 70 74 69 6d 69 7a 65 72 22 2e 0a 2a 2f 0a 0a 2f  ptimizer"..*/../
2e614 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74 70 75  *.** Trace outpu
2e615 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69 66 20  t macros.*/.#if 
2e616 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
2e617 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  EST) || defined(
2e618 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51  SQLITE_DEBUG).SQ
2e619 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2e61a 20 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61   sqlite3WhereTra
2e61b 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23  ce = 0;.#endif.#
2e61c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2e61d 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e  E_TEST) && defin
2e61e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2e61f 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54  .# define WHERET
2e620 52 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69  RACE(X)  if(sqli
2e621 74 65 33 57 68 65 72 65 54 72 61 63 65 29 20 73  te3WhereTrace) s
2e622 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2e623 66 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  f X.#else.# defi
2e624 6e 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29  ne WHERETRACE(X)
2e625 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77  .#endif../* Forw
2e626 61 72 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f  ard reference.*/
2e627 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2e628 57 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72  WhereClause Wher
2e629 65 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66  eClause;.typedef
2e62a 20 73 74 72 75 63 74 20 57 68 65 72 65 4d 61 73   struct WhereMas
2e62b 6b 53 65 74 20 57 68 65 72 65 4d 61 73 6b 53 65  kSet WhereMaskSe
2e62c 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
2e62d 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 57 68  t WhereOrInfo Wh
2e62e 65 72 65 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64  ereOrInfo;.typed
2e62f 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 41  ef struct WhereA
2e630 6e 64 49 6e 66 6f 20 57 68 65 72 65 41 6e 64 49  ndInfo WhereAndI
2e631 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
2e632 75 63 74 20 57 68 65 72 65 43 6f 73 74 20 57 68  uct WhereCost Wh
2e633 65 72 65 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20  ereCost;../*.** 
2e634 54 68 65 20 71 75 65 72 79 20 67 65 6e 65 72 61  The query genera
2e635 74 6f 72 20 75 73 65 73 20 61 6e 20 61 72 72 61  tor uses an arra
2e636 79 20 6f 66 20 69 6e 73 74 61 6e 63 65 73 20 6f  y of instances o
2e637 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
2e638 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61   to.** help it a
2e639 6e 61 6c 79 7a 65 20 74 68 65 20 73 75 62 65 78  nalyze the subex
2e63a 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
2e63b 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2e63c 45 61 63 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c  Each WHERE.** cl
2e63d 61 75 73 65 20 73 75 62 65 78 70 72 65 73 73 69  ause subexpressi
2e63e 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
2e63f 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20  from the others 
2e640 62 79 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73  by AND operators
2e641 2c 0a 2a 2a 20 75 73 75 61 6c 6c 79 2c 20 6f 72  ,.** usually, or
2e642 20 73 6f 6d 65 74 69 6d 65 73 20 73 75 62 65 78   sometimes subex
2e643 70 72 65 73 73 69 6f 6e 73 20 73 65 70 61 72 61  pressions separa
2e644 74 65 64 20 62 79 20 4f 52 2e 0a 2a 2a 0a 2a 2a  ted by OR..**.**
2e645 20 41 6c 6c 20 57 68 65 72 65 54 65 72 6d 73 20   All WhereTerms 
2e646 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e  are collected in
2e647 74 6f 20 61 20 73 69 6e 67 6c 65 20 57 68 65 72  to a single Wher
2e648 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
2e649 65 2e 20 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  e.  .** The foll
2e64a 6f 77 69 6e 67 20 69 64 65 6e 74 69 74 79 20 68  owing identity h
2e64b 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  olds:.**.**     
2e64c 20 20 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43     WhereTerm.pWC
2e64d 2d 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64  ->a[WhereTerm.id
2e64e 78 5d 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a  x] == WhereTerm.
2e64f 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65 72  **.** When a ter
2e650 6d 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  m is of the form
2e651 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
2e652 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70       X <op> <exp
2e653 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58  r>.**.** where X
2e654 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d   is a column nam
2e655 65 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e  e and <op> is on
2e656 65 20 6f 66 20 63 65 72 74 61 69 6e 20 6f 70 65  e of certain ope
2e657 72 61 74 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20  rators,.** then 
2e658 57 68 65 72 65 54 65 72 6d 2e 6c 65 66 74 43 75  WhereTerm.leftCu
2e659 72 73 6f 72 20 61 6e 64 20 57 68 65 72 65 54 65  rsor and WhereTe
2e65a 72 6d 2e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20  rm.u.leftColumn 
2e65b 72 65 63 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75  record the.** cu
2e65c 72 73 6f 72 20 6e 75 6d 62 65 72 20 61 6e 64 20  rsor number and 
2e65d 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f  column number fo
2e65e 72 20 58 2e 20 20 57 68 65 72 65 54 65 72 6d 2e  r X.  WhereTerm.
2e65f 65 4f 70 65 72 61 74 6f 72 20 72 65 63 6f 72 64  eOperator record
2e660 73 0a 2a 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73  s.** the <op> us
2e661 69 6e 67 20 61 20 62 69 74 6d 61 73 6b 20 65 6e  ing a bitmask en
2e662 63 6f 64 69 6e 67 20 64 65 66 69 6e 65 64 20 62  coding defined b
2e663 79 20 57 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20  y WO_xxx below. 
2e664 20 54 68 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61   The.** use of a
2e665 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e   bitmask encodin
2e666 67 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74  g for the operat
2e667 6f 72 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20  or allows us to 
2e668 73 65 61 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c  search.** quickl
2e669 79 20 66 6f 72 20 74 65 72 6d 73 20 74 68 61 74  y for terms that
2e66a 20 6d 61 74 63 68 20 61 6e 79 20 6f 66 20 73 65   match any of se
2e66b 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20  veral different 
2e66c 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a  operators..**.**
2e66d 20 41 20 57 68 65 72 65 54 65 72 6d 20 6d 69 67   A WhereTerm mig
2e66e 68 74 20 61 6c 73 6f 20 62 65 20 74 77 6f 20 6f  ht also be two o
2e66f 72 20 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20  r more subterms 
2e670 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 3a  connected by OR:
2e671 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 28  .**.**         (
2e672 74 31 2e 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  t1.X <op> <expr>
2e673 29 20 4f 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20  ) OR (t1.Y <op> 
2e674 3c 65 78 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0a  <expr>) OR .....
2e675 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 73 65  **.** In this se
2e676 63 6f 6e 64 20 63 61 73 65 2c 20 77 74 46 6c 61  cond case, wtFla
2e677 67 20 61 73 20 74 68 65 20 54 45 52 4d 5f 4f 52  g as the TERM_OR
2e678 49 4e 46 4f 20 73 65 74 20 61 6e 64 20 65 4f 70  INFO set and eOp
2e679 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a  erator==WO_OR.**
2e67a 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 54 65   and the WhereTe
2e67b 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65  rm.u.pOrInfo fie
2e67c 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 75 78  ld points to aux
2e67d 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69  iliary informati
2e67e 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f  on that.** is co
2e67f 6c 6c 65 63 74 65 64 20 61 62 6f 75 74 20 74 68  llected about th
2e680 65 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 65 72  e.**.** If a ter
2e681 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  m in the WHERE c
2e682 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d  lause does not m
2e683 61 74 63 68 20 65 69 74 68 65 72 20 6f 66 20 74  atch either of t
2e684 68 65 20 74 77 6f 20 70 72 65 76 69 6f 75 73 0a  he two previous.
2e685 2a 2a 20 63 61 74 65 67 6f 72 69 65 73 2c 20 74  ** categories, t
2e686 68 65 6e 20 65 4f 70 65 72 61 74 6f 72 3d 3d 30  hen eOperator==0
2e687 2e 20 20 54 68 65 20 57 68 65 72 65 54 65 72 6d  .  The WhereTerm
2e688 2e 70 45 78 70 72 20 66 69 65 6c 64 20 69 73 20  .pExpr field is 
2e689 73 74 69 6c 6c 20 73 65 74 0a 2a 2a 20 74 6f 20  still set.** to 
2e68a 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 75 62  the original sub
2e68b 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65  expression conte
2e68c 6e 74 20 61 6e 64 20 77 74 46 6c 61 67 73 20 69  nt and wtFlags i
2e68d 73 20 73 65 74 20 75 70 20 61 70 70 72 6f 70 72  s set up appropr
2e68e 69 61 74 65 6c 79 0a 2a 2a 20 62 75 74 20 6e 6f  iately.** but no
2e68f 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 69 6e   other fields in
2e690 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 20 6f   the WhereTerm o
2e691 62 6a 65 63 74 20 61 72 65 20 6d 65 61 6e 69 6e  bject are meanin
2e692 67 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  gful..**.** When
2e693 20 65 4f 70 65 72 61 74 6f 72 21 3d 30 2c 20 70   eOperator!=0, p
2e694 72 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70  rereqRight and p
2e695 72 65 72 65 71 41 6c 6c 20 72 65 63 6f 72 64 20  rereqAll record 
2e696 73 65 74 73 20 6f 66 20 63 75 72 73 6f 72 20 6e  sets of cursor n
2e697 75 6d 62 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74  umbers,.** but t
2e698 68 65 79 20 64 6f 20 73 6f 20 69 6e 64 69 72 65  hey do so indire
2e699 63 74 6c 79 2e 20 20 41 20 73 69 6e 67 6c 65 20  ctly.  A single 
2e69a 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72  WhereMaskSet str
2e69b 75 63 74 75 72 65 20 74 72 61 6e 73 6c 61 74 65  ucture translate
2e69c 73 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62  s.** cursor numb
2e69d 65 72 20 69 6e 74 6f 20 62 69 74 73 20 61 6e 64  er into bits and
2e69e 20 74 68 65 20 74 72 61 6e 73 6c 61 74 65 64 20   the translated 
2e69f 62 69 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  bit is stored in
2e6a0 20 74 68 65 20 70 72 65 72 65 71 0a 2a 2a 20 66   the prereq.** f
2e6a1 69 65 6c 64 73 2e 20 20 54 68 65 20 74 72 61 6e  ields.  The tran
2e6a2 73 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20  slation is used 
2e6a3 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 78 69  in order to maxi
2e6a4 6d 69 7a 65 20 74 68 65 20 6e 75 6d 62 65 72 20  mize the number 
2e6a5 6f 66 0a 2a 2a 20 62 69 74 73 20 74 68 61 74 20  of.** bits that 
2e6a6 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 20 42 69  will fit in a Bi
2e6a7 74 6d 61 73 6b 2e 20 20 54 68 65 20 56 44 42 45  tmask.  The VDBE
2e6a8 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e6a9 6d 69 67 68 74 20 62 65 0a 2a 2a 20 73 70 72 65  might be.** spre
2e6aa 61 64 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20  ad out over the 
2e6ab 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74  non-negative int
2e6ac 65 67 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d  egers.  For exam
2e6ad 70 6c 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0a  ple, the cursor.
2e6ae 2a 2a 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74  ** numbers might
2e6af 20 62 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c   be 3, 8, 9, 10,
2e6b0 20 32 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64   20, 23, 41, and
2e6b1 20 34 35 2e 20 20 54 68 65 20 57 68 65 72 65 4d   45.  The WhereM
2e6b2 61 73 6b 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c  askSet.** transl
2e6b3 61 74 65 73 20 74 68 65 73 65 20 73 70 61 72 73  ates these spars
2e6b4 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e6b5 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76   into consecutiv
2e6b6 65 20 69 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65  e integers.** be
2e6b7 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 20 69  ginning with 0 i
2e6b8 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20  n order to make 
2e6b9 74 68 65 20 62 65 73 74 20 70 6f 73 73 69 62 6c  the best possibl
2e6ba 65 20 75 73 65 20 6f 66 20 74 68 65 20 61 76 61  e use of the ava
2e6bb 69 6c 61 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69  ilable.** bits i
2e6bc 6e 20 74 68 65 20 42 69 74 6d 61 73 6b 2e 20 20  n the Bitmask.  
2e6bd 53 6f 2c 20 69 6e 20 74 68 65 20 65 78 61 6d 70  So, in the examp
2e6be 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20 63 75  le above, the cu
2e6bf 72 73 6f 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20  rsor numbers.** 
2e6c0 77 6f 75 6c 64 20 62 65 20 6d 61 70 70 65 64 20  would be mapped 
2e6c1 69 6e 74 6f 20 69 6e 74 65 67 65 72 73 20 30 20  into integers 0 
2e6c2 74 68 72 6f 75 67 68 20 37 2e 0a 2a 2a 0a 2a 2a  through 7..**.**
2e6c3 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   The number of t
2e6c4 65 72 6d 73 20 69 6e 20 61 20 6a 6f 69 6e 20 69  erms in a join i
2e6c5 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65  s limited by the
2e6c6 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 0a   number of bits.
2e6c7 2a 2a 20 69 6e 20 70 72 65 72 65 71 52 69 67 68  ** in prereqRigh
2e6c8 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 2e  t and prereqAll.
2e6c9 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73    The default is
2e6ca 20 36 34 20 62 69 74 73 2c 20 68 65 6e 63 65 20   64 bits, hence 
2e6cb 53 51 4c 69 74 65 0a 2a 2a 20 69 73 20 6f 6e 6c  SQLite.** is onl
2e6cc 79 20 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73  y able to proces
2e6cd 73 20 6a 6f 69 6e 73 20 77 69 74 68 20 36 34 20  s joins with 64 
2e6ce 6f 72 20 66 65 77 65 72 20 74 61 62 6c 65 73 2e  or fewer tables.
2e6cf 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2e6d0 63 74 20 57 68 65 72 65 54 65 72 6d 20 57 68 65  ct WhereTerm Whe
2e6d1 72 65 54 65 72 6d 3b 0a 73 74 72 75 63 74 20 57  reTerm;.struct W
2e6d2 68 65 72 65 54 65 72 6d 20 7b 0a 20 20 45 78 70  hereTerm {.  Exp
2e6d3 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
2e6d4 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2e6d5 74 6f 20 74 68 65 20 73 75 62 65 78 70 72 65 73  to the subexpres
2e6d6 73 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68 69  sion that is thi
2e6d7 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20  s term */.  int 
2e6d8 69 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20  iParent;        
2e6d9 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 70      /* Disable p
2e6da 57 43 2d 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77  WC->a[iParent] w
2e6db 68 65 6e 20 74 68 69 73 20 74 65 72 6d 20 64 69  hen this term di
2e6dc 73 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20  sabled */.  int 
2e6dd 6c 65 66 74 43 75 72 73 6f 72 3b 20 20 20 20 20  leftCursor;     
2e6de 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
2e6df 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20  mber of X in "X 
2e6e0 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a  <op> <expr>" */.
2e6e1 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e    union {.    in
2e6e2 74 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20  t leftColumn;   
2e6e3 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
2e6e4 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22  number of X in "
2e6e5 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a  X <op> <expr>" *
2e6e6 2f 0a 20 20 20 20 57 68 65 72 65 4f 72 49 6e 66  /.    WhereOrInf
2e6e7 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a  o *pOrInfo;   /*
2e6e8 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   Extra informati
2e6e9 6f 6e 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d  on if eOperator=
2e6ea 3d 57 4f 5f 4f 52 20 2a 2f 0a 20 20 20 20 57 68  =WO_OR */.    Wh
2e6eb 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64  ereAndInfo *pAnd
2e6ec 49 6e 66 6f 3b 20 2f 2a 20 45 78 74 72 61 20 69  Info; /* Extra i
2e6ed 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f  nformation if eO
2e6ee 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20  perator==WO_AND 
2e6ef 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 31 36 20  */.  } u;.  u16 
2e6f0 65 4f 70 65 72 61 74 6f 72 3b 20 20 20 20 20 20  eOperator;      
2e6f1 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78 78 20 76      /* A WO_xx v
2e6f2 61 6c 75 65 20 64 65 73 63 72 69 62 69 6e 67 20  alue describing 
2e6f3 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38 20 77 74 46  <op> */.  u8 wtF
2e6f4 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
2e6f5 20 20 2f 2a 20 54 45 52 4d 5f 78 78 78 20 62 69    /* TERM_xxx bi
2e6f6 74 20 66 6c 61 67 73 2e 20 20 53 65 65 20 62 65  t flags.  See be
2e6f7 6c 6f 77 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69  low */.  u8 nChi
2e6f8 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
2e6f9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
2e6fa 69 6c 64 72 65 6e 20 74 68 61 74 20 6d 75 73 74  ildren that must
2e6fb 20 64 69 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20   disable us */. 
2e6fc 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2e6fd 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  C;       /* The 
2e6fe 63 6c 61 75 73 65 20 74 68 69 73 20 74 65 72 6d  clause this term
2e6ff 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20   is part of */. 
2e700 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 52   Bitmask prereqR
2e701 69 67 68 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d  ight;    /* Bitm
2e702 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 75 73  ask of tables us
2e703 65 64 20 62 79 20 70 45 78 70 72 2d 3e 70 52 69  ed by pExpr->pRi
2e704 67 68 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  ght */.  Bitmask
2e705 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20 20 20   prereqAll;     
2e706 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74   /* Bitmask of t
2e707 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64  ables referenced
2e708 20 62 79 20 70 45 78 70 72 20 2a 2f 0a 7d 3b 0a   by pExpr */.};.
2e709 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
2e70a 61 6c 75 65 73 20 6f 66 20 57 68 65 72 65 54 65  alues of WhereTe
2e70b 72 6d 2e 77 74 46 6c 61 67 73 0a 2a 2f 0a 23 64  rm.wtFlags.*/.#d
2e70c 65 66 69 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d  efine TERM_DYNAM
2e70d 49 43 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20  IC    0x01   /* 
2e70e 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c  Need to call sql
2e70f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2e710 62 2c 20 70 45 78 70 72 29 20 2a 2f 0a 23 64 65  b, pExpr) */.#de
2e711 66 69 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41  fine TERM_VIRTUA
2e712 4c 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41  L    0x02   /* A
2e713 64 64 65 64 20 62 79 20 74 68 65 20 6f 70 74 69  dded by the opti
2e714 6d 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63  mizer.  Do not c
2e715 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ode */.#define T
2e716 45 52 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30  ERM_CODED      0
2e717 78 30 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65  x04   /* This te
2e718 72 6d 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f  rm is already co
2e719 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ded */.#define T
2e71a 45 52 4d 5f 43 4f 50 49 45 44 20 20 20 20 20 30  ERM_COPIED     0
2e71b 78 30 38 20 20 20 2f 2a 20 48 61 73 20 61 20 63  x08   /* Has a c
2e71c 68 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  hild */.#define 
2e71d 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 20 20 20 20  TERM_ORINFO     
2e71e 30 78 31 30 20 20 20 2f 2a 20 4e 65 65 64 20 74  0x10   /* Need t
2e71f 6f 20 66 72 65 65 20 74 68 65 20 57 68 65 72 65  o free the Where
2e720 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 6f  Term.u.pOrInfo o
2e721 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65  bject */.#define
2e722 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 20 20   TERM_ANDINFO   
2e723 20 30 78 32 30 20 20 20 2f 2a 20 4e 65 65 64 20   0x20   /* Need 
2e724 74 6f 20 66 72 65 65 20 74 68 65 20 57 68 65 72  to free the Wher
2e725 65 54 65 72 6d 2e 75 2e 70 41 6e 64 49 6e 66 6f  eTerm.u.pAndInfo
2e726 20 6f 62 6a 20 2a 2f 0a 23 64 65 66 69 6e 65 20   obj */.#define 
2e727 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20  TERM_OR_OK      
2e728 30 78 34 30 20 20 20 2f 2a 20 55 73 65 64 20 64  0x40   /* Used d
2e729 75 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20  uring OR-clause 
2e72a 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f  processing */../
2e72b 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
2e72c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2e72d 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
2e72e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  s all informatio
2e72f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45  n about a.** WHE
2e730 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74  RE clause.  Most
2e731 6c 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e  ly this is a con
2e732 74 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f  tainer for one o
2e733 72 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d  r more WhereTerm
2e734 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  s..*/.struct Whe
2e735 72 65 43 6c 61 75 73 65 20 7b 0a 20 20 50 61 72  reClause {.  Par
2e736 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
2e737 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2e738 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2e739 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
2e73a 4d 61 73 6b 53 65 74 3b 20 20 2f 2a 20 4d 61 70  MaskSet;  /* Map
2e73b 70 69 6e 67 20 6f 66 20 74 61 62 6c 65 20 63 75  ping of table cu
2e73c 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20  rsor numbers to 
2e73d 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20 42 69  bitmasks */.  Bi
2e73e 74 6d 61 73 6b 20 76 6d 61 73 6b 3b 20 20 20 20  tmask vmask;    
2e73f 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73         /* Bitmas
2e740 6b 20 69 64 65 6e 74 69 66 79 69 6e 67 20 76 69  k identifying vi
2e741 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
2e742 6f 72 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20  ors */.  u8 op; 
2e743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e744 20 20 2f 2a 20 53 70 6c 69 74 20 6f 70 65 72 61    /* Split opera
2e745 74 6f 72 2e 20 20 54 4b 5f 41 4e 44 20 6f 72 20  tor.  TK_AND or 
2e746 54 4b 5f 4f 52 20 2a 2f 0a 20 20 69 6e 74 20 6e  TK_OR */.  int n
2e747 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
2e748 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2e749 20 74 65 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20   terms */.  int 
2e74a 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20  nSlot;          
2e74b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2e74c 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 5b 5d  f entries in a[]
2e74d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2e74e 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  *a;            /
2e74f 2a 20 45 61 63 68 20 61 5b 5d 20 64 65 73 63 72  * Each a[] descr
2e750 69 62 65 73 20 61 20 74 65 72 6d 20 6f 66 20 74  ibes a term of t
2e751 68 65 20 57 48 45 52 45 20 63 6c 75 61 73 65 20  he WHERE cluase 
2e752 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
2e753 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43  QLITE_SMALL_STAC
2e754 4b 29 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61  K).  WhereTerm a
2e755 53 74 61 74 69 63 5b 31 5d 3b 20 20 20 20 2f 2a  Static[1];    /*
2e756 20 49 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20   Initial static 
2e757 73 70 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f  space for a[] */
2e758 0a 23 65 6c 73 65 0a 20 20 57 68 65 72 65 54 65  .#else.  WhereTe
2e759 72 6d 20 61 53 74 61 74 69 63 5b 38 5d 3b 20 20  rm aStatic[8];  
2e75a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61    /* Initial sta
2e75b 74 69 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b  tic space for a[
2e75c 5d 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  ] */.#endif.};..
2e75d 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72  /*.** A WhereTer
2e75e 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72  m with eOperator
2e75f 3d 3d 57 4f 5f 4f 52 20 68 61 73 20 69 74 73 20  ==WO_OR has its 
2e760 75 2e 70 4f 72 49 6e 66 6f 20 70 6f 69 6e 74 65  u.pOrInfo pointe
2e761 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79  r set to.** a dy
2e762 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2e763 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ted instance of 
2e764 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2e765 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
2e766 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 7b  ct WhereOrInfo {
2e767 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 77  .  WhereClause w
2e768 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  c;          /* D
2e769 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 69 6e 74  ecomposition int
2e76a 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
2e76b 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c  Bitmask indexabl
2e76c 65 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  e;       /* Bitm
2e76d 61 73 6b 20 6f 66 20 61 6c 6c 20 69 6e 64 65 78  ask of all index
2e76e 61 62 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74  able tables in t
2e76f 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 7d 3b 0a  he clause */.};.
2e770 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65  ./*.** A WhereTe
2e771 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f  rm with eOperato
2e772 72 3d 3d 57 4f 5f 41 4e 44 20 68 61 73 20 69 74  r==WO_AND has it
2e773 73 20 75 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69  s u.pAndInfo poi
2e774 6e 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61  nter set to.** a
2e775 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
2e776 6f 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20  ocated instance 
2e777 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2e778 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
2e779 74 72 75 63 74 20 57 68 65 72 65 41 6e 64 49 6e  truct WhereAndIn
2e77a 66 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75  fo {.  WhereClau
2e77b 73 65 20 77 63 3b 20 20 20 20 20 20 20 20 20 20  se wc;          
2e77c 2f 2a 20 54 68 65 20 73 75 62 65 78 70 72 65 73  /* The subexpres
2e77d 73 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75 74 20  sion broken out 
2e77e 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
2e77f 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
2e780 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
2e781 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20  ure keeps track 
2e782 6f 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20  of a mapping.** 
2e783 62 65 74 77 65 65 6e 20 56 44 42 45 20 63 75 72  between VDBE cur
2e784 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e 64 20  sor numbers and 
2e785 62 69 74 73 20 6f 66 20 74 68 65 20 62 69 74 6d  bits of the bitm
2e786 61 73 6b 73 20 69 6e 20 57 68 65 72 65 54 65 72  asks in WhereTer
2e787 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  m..**.** The VDB
2e788 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  E cursor numbers
2e789 20 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67   are small integ
2e78a 65 72 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  ers contained in
2e78b 20 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69 74 65   .** SrcList_ite
2e78c 6d 2e 69 43 75 72 73 6f 72 20 61 6e 64 20 45 78  m.iCursor and Ex
2e78d 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73  pr.iTable fields
2e78e 2e 20 20 46 6f 72 20 61 6e 79 20 67 69 76 65 6e  .  For any given
2e78f 20 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61 75 73   WHERE .** claus
2e790 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  e, the cursor nu
2e791 6d 62 65 72 73 20 6d 69 67 68 74 20 6e 6f 74 20  mbers might not 
2e792 62 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64  begin with 0 and
2e793 20 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a 20 63   they might.** c
2e794 6f 6e 74 61 69 6e 20 67 61 70 73 20 69 6e 20 74  ontain gaps in t
2e795 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 73 65 71  he numbering seq
2e796 75 65 6e 63 65 2e 20 20 42 75 74 20 77 65 20 77  uence.  But we w
2e797 61 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61 78 69  ant to make maxi
2e798 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68  mum.** use of th
2e799 65 20 62 69 74 73 20 69 6e 20 6f 75 72 20 62 69  e bits in our bi
2e79a 74 6d 61 73 6b 73 2e 20 20 54 68 69 73 20 73 74  tmasks.  This st
2e79b 72 75 63 74 75 72 65 20 70 72 6f 76 69 64 65 73  ructure provides
2e79c 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 66 72   a mapping.** fr
2e79d 6f 6d 20 74 68 65 20 73 70 61 72 73 65 20 63 75  om the sparse cu
2e79e 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74  rsor numbers int
2e79f 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e  o consecutive in
2e7a0 74 65 67 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  tegers beginning
2e7a1 0a 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a  .** with 0..**.*
2e7a2 2a 20 49 66 20 57 68 65 72 65 4d 61 73 6b 53 65  * If WhereMaskSe
2e7a3 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65  t.ix[A]==B it me
2e7a4 61 6e 73 20 74 68 61 74 20 54 68 65 20 41 2d 74  ans that The A-t
2e7a5 68 20 62 69 74 20 6f 66 20 61 20 42 69 74 6d 61  h bit of a Bitma
2e7a6 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  sk.** correspond
2e7a7 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  s VDBE cursor nu
2e7a8 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41 2d 74  mber B.  The A-t
2e7a9 68 20 62 69 74 20 6f 66 20 61 20 62 69 74 6d 61  h bit of a bitma
2e7aa 73 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a  sk is 1<<A..**.*
2e7ab 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  * For example, i
2e7ac 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2e7ad 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20 75 73  se expression us
2e7ae 65 64 20 74 68 65 73 65 20 56 44 42 45 0a 2a 2a  ed these VDBE.**
2e7af 20 63 75 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c   cursors:  4, 5,
2e7b0 20 38 2c 20 32 39 2c 20 35 37 2c 20 37 33 2e 20   8, 29, 57, 73. 
2e7b1 20 54 68 65 6e 20 74 68 65 20 20 57 68 65 72 65   Then the  Where
2e7b2 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72  MaskSet structur
2e7b3 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74  e.** would map t
2e7b4 68 6f 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  hose cursor numb
2e7b5 65 72 73 20 69 6e 74 6f 20 62 69 74 73 20 30 20  ers into bits 0 
2e7b6 74 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a  through 5..**.**
2e7b7 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d   Note that the m
2e7b8 61 70 70 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65  apping is not ne
2e7b9 63 65 73 73 61 72 69 6c 79 20 6f 72 64 65 72 65  cessarily ordere
2e7ba 64 2e 20 20 49 6e 20 74 68 65 20 65 78 61 6d 70  d.  In the examp
2e7bb 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65  le.** above, the
2e7bc 20 6d 61 70 70 69 6e 67 20 6d 69 67 68 74 20 67   mapping might g
2e7bd 6f 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 34 2d  o like this:  4-
2e7be 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20  >3, 5->1, 8->2, 
2e7bf 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c  29->0,.** 57->5,
2e7c0 20 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20   73->4.  Or one 
2e7c1 6f 66 20 37 31 39 20 6f 74 68 65 72 20 63 6f 6d  of 719 other com
2e7c2 62 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68 74 20  binations might 
2e7c3 62 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64  be used. It.** d
2e7c4 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d  oes not really m
2e7c5 61 74 74 65 72 2e 20 20 57 68 61 74 20 69 73 20  atter.  What is 
2e7c6 69 6d 70 6f 72 74 61 6e 74 20 69 73 20 74 68 61  important is tha
2e7c7 74 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 0a  t sparse cursor.
2e7c8 2a 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67  ** numbers all g
2e7c9 65 74 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 62  et mapped into b
2e7ca 69 74 20 6e 75 6d 62 65 72 73 20 74 68 61 74 20  it numbers that 
2e7cb 62 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64  begin with 0 and
2e7cc 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67   contain.** no g
2e7cd 61 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  aps..*/.struct W
2e7ce 68 65 72 65 4d 61 73 6b 53 65 74 20 7b 0a 20 20  hereMaskSet {.  
2e7cf 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
2e7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e7d1 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73 69 67   Number of assig
2e7d2 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c 75 65  ned cursor value
2e7d3 73 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b 42 4d  s */.  int ix[BM
2e7d4 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  S];             
2e7d5 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 61       /* Cursor a
2e7d6 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20  ssigned to each 
2e7d7 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  bit */.};../*.**
2e7d8 20 41 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a   A WhereCost obj
2e7d9 65 63 74 20 72 65 63 6f 72 64 73 20 61 20 6c 6f  ect records a lo
2e7da 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20 61 6e  okup strategy an
2e7db 64 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 0a  d the estimated.
2e7dc 2a 2a 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75  ** cost of pursu
2e7dd 69 6e 67 20 74 68 61 74 20 73 74 72 61 74 65 67  ing that strateg
2e7de 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  y..*/.struct Whe
2e7df 72 65 43 6f 73 74 20 7b 0a 20 20 57 68 65 72 65  reCost {.  Where
2e7e0 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 2f 2a  Plan plan;    /*
2e7e1 20 54 68 65 20 6c 6f 6f 6b 75 70 20 73 74 72 61   The lookup stra
2e7e2 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  tegy */.  double
2e7e3 20 72 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20   rCost;      /* 
2e7e4 4f 76 65 72 61 6c 6c 20 63 6f 73 74 20 6f 66 20  Overall cost of 
2e7e5 70 75 72 73 75 69 6e 67 20 74 68 69 73 20 73 65  pursuing this se
2e7e6 61 72 63 68 20 73 74 72 61 74 65 67 79 20 2a 2f  arch strategy */
2e7e7 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20  .  double nRow; 
2e7e8 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74        /* Estimat
2e7e9 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  ed number of out
2e7ea 70 75 74 20 72 6f 77 73 20 2a 2f 0a 20 20 42 69  put rows */.  Bi
2e7eb 74 6d 61 73 6b 20 75 73 65 64 3b 20 20 20 20 20  tmask used;     
2e7ec 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 63   /* Bitmask of c
2e7ed 75 72 73 6f 72 73 20 75 73 65 64 20 62 79 20 74  ursors used by t
2e7ee 68 69 73 20 70 6c 61 6e 20 2a 2f 0a 7d 3b 0a 0a  his plan */.};..
2e7ef 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66  /*.** Bitmasks f
2e7f0 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  or the operators
2e7f1 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72   that indices ar
2e7f2 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69  e able to exploi
2e7f3 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20  t.  An.** OR-ed 
2e7f4 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74  combination of t
2e7f5 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20  hese values can 
2e7f6 62 65 20 75 73 65 64 20 77 68 65 6e 20 73 65 61  be used when sea
2e7f7 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65  rching for.** te
2e7f8 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65 72 65  rms in the where
2e7f9 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66   clause..*/.#def
2e7fa 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20 30 78  ine WO_IN     0x
2e7fb 30 30 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45  001.#define WO_E
2e7fc 51 20 20 20 20 20 30 78 30 30 32 0a 23 64 65 66  Q     0x002.#def
2e7fd 69 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20 28 57  ine WO_LT     (W
2e7fe 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f  O_EQ<<(TK_LT-TK_
2e7ff 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f  EQ)).#define WO_
2e800 4c 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28  LE     (WO_EQ<<(
2e801 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64  TK_LE-TK_EQ)).#d
2e802 65 66 69 6e 65 20 57 4f 5f 47 54 20 20 20 20 20  efine WO_GT     
2e803 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54  (WO_EQ<<(TK_GT-T
2e804 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
2e805 4f 5f 47 45 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_GE     (WO_EQ<
2e806 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a  <(TK_GE-TK_EQ)).
2e807 23 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48  #define WO_MATCH
2e808 20 20 30 78 30 34 30 0a 23 64 65 66 69 6e 65 20    0x040.#define 
2e809 57 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a  WO_ISNULL 0x080.
2e80a 23 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20  #define WO_OR   
2e80b 20 20 30 78 31 30 30 20 20 20 20 20 20 20 2f 2a    0x100       /*
2e80c 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d   Two or more OR-
2e80d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20  connected terms 
2e80e 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e  */.#define WO_AN
2e80f 44 20 20 20 20 30 78 32 30 30 20 20 20 20 20 20  D    0x200      
2e810 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20   /* Two or more 
2e811 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65  AND-connected te
2e812 72 6d 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  rms */..#define 
2e813 57 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66 66 20  WO_ALL    0xfff 
2e814 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2e815 20 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20 57 4f   all possible WO
2e816 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65  _* values */.#de
2e817 66 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45 20 30  fine WO_SINGLE 0
2e818 78 30 66 66 20 20 20 20 20 20 20 2f 2a 20 4d 61  x0ff       /* Ma
2e819 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f  sk of all non-co
2e81a 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61 6c 75  mpound WO_* valu
2e81b 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  es */../*.** Val
2e81c 75 65 20 66 6f 72 20 77 73 46 6c 61 67 73 20 72  ue for wsFlags r
2e81d 65 74 75 72 6e 65 64 20 62 79 20 62 65 73 74 49  eturned by bestI
2e81e 6e 64 65 78 28 29 20 61 6e 64 20 73 74 6f 72 65  ndex() and store
2e81f 64 20 69 6e 0a 2a 2a 20 57 68 65 72 65 4c 65 76  d in.** WhereLev
2e820 65 6c 2e 77 73 46 6c 61 67 73 2e 20 20 54 68 65  el.wsFlags.  The
2e821 73 65 20 66 6c 61 67 73 20 64 65 74 65 72 6d 69  se flags determi
2e822 6e 65 20 77 68 69 63 68 20 73 65 61 72 63 68 0a  ne which search.
2e823 2a 2a 20 73 74 72 61 74 65 67 69 65 73 20 61 72  ** strategies ar
2e824 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a  e appropriate..*
2e825 2a 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74 20 73  *.** The least s
2e826 69 67 6e 69 66 69 63 61 6e 74 20 31 32 20 62 69  ignificant 12 bi
2e827 74 73 20 69 73 20 72 65 73 65 72 76 65 64 20 61  ts is reserved a
2e828 73 20 61 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f  s a mask for WO_
2e829 20 76 61 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a   values above..*
2e82a 2a 20 54 68 65 20 57 68 65 72 65 4c 65 76 65 6c  * The WhereLevel
2e82b 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64 20 69  .wsFlags field i
2e82c 73 20 75 73 75 61 6c 6c 79 20 73 65 74 20 74 6f  s usually set to
2e82d 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f   WO_IN|WO_EQ|WO_
2e82e 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69  ISNULL..** But i
2e82f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  f the table is t
2e830 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
2e831 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57  f a left join, W
2e832 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67  hereLevel.wsFlag
2e833 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 57  s.** is set to W
2e834 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65  O_IN|WO_EQ.  The
2e835 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c   WhereLevel.wsFl
2e836 61 67 73 20 66 69 65 6c 64 20 63 61 6e 20 74 68  ags field can th
2e837 65 6e 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a  en be used as.**
2e838 20 74 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65   the "op" parame
2e839 74 65 72 20 74 6f 20 66 69 6e 64 54 65 72 6d 20  ter to findTerm 
2e83a 77 68 65 6e 20 77 65 20 61 72 65 20 72 65 73 6f  when we are reso
2e83b 6c 76 69 6e 67 20 65 71 75 61 6c 69 74 79 20 63  lving equality c
2e83c 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49  onstraints..** I
2e83d 53 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74  SNULL constraint
2e83e 73 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20  s will then not 
2e83f 62 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72  be used on the r
2e840 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
2e841 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54  left.** join.  T
2e842 69 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64  ickets #2177 and
2e843 20 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69   #2189..*/.#defi
2e844 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  ne WHERE_ROWID_E
2e845 51 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30  Q     0x00001000
2e846 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52 20    /* rowid=EXPR 
2e847 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e  or rowid IN (...
2e848 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
2e849 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 20  RE_ROWID_RANGE  
2e84a 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 72  0x00002000  /* r
2e84b 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72  owid<EXPR and/or
2e84c 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23   rowid>EXPR */.#
2e84d 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c  define WHERE_COL
2e84e 55 4d 4e 5f 45 51 20 20 20 20 30 78 30 30 30 31  UMN_EQ    0x0001
2e84f 30 30 30 30 20 20 2f 2a 20 78 3d 45 58 50 52 20  0000  /* x=EXPR 
2e850 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 6f 72  or x IN (...) or
2e851 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64   x IS NULL */.#d
2e852 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55  efine WHERE_COLU
2e853 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30 30 32 30  MN_RANGE 0x00020
2e854 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52 20 61  000  /* x<EXPR a
2e855 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a  nd/or x>EXPR */.
2e856 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f  #define WHERE_CO
2e857 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30 30 30  LUMN_IN    0x000
2e858 34 30 30 30 30 20 20 2f 2a 20 78 20 49 4e 20 28  40000  /* x IN (
2e859 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ...) */.#define 
2e85a 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c  WHERE_COLUMN_NUL
2e85b 4c 20 20 30 78 30 30 30 38 30 30 30 30 20 20 2f  L  0x00080000  /
2e85c 2a 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23  * x IS NULL */.#
2e85d 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49 4e 44  define WHERE_IND
2e85e 45 58 45 44 20 20 20 20 20 20 30 78 30 30 30 66  EXED      0x000f
2e85f 30 30 30 30 20 20 2f 2a 20 41 6e 79 74 68 69 6e  0000  /* Anythin
2e860 67 20 74 68 61 74 20 75 73 65 73 20 61 6e 20 69  g that uses an i
2e861 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ndex */.#define 
2e862 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 20 20  WHERE_IN_ABLE   
2e863 20 20 20 30 78 30 30 30 66 31 30 30 30 20 20 2f     0x000f1000  /
2e864 2a 20 41 62 6c 65 20 74 6f 20 73 75 70 70 6f 72  * Able to suppor
2e865 74 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72  t an IN operator
2e866 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2e867 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30  E_TOP_LIMIT    0
2e868 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20 78 3c  x00100000  /* x<
2e869 45 58 50 52 20 6f 72 20 78 3c 3d 45 58 50 52 20  EXPR or x<=EXPR 
2e86a 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64  constraint */.#d
2e86b 65 66 69 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f  efine WHERE_BTM_
2e86c 4c 49 4d 49 54 20 20 20 20 30 78 30 30 32 30 30  LIMIT    0x00200
2e86d 30 30 30 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f  000  /* x>EXPR o
2e86e 72 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72  r x>=EXPR constr
2e86f 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  aint */.#define 
2e870 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20  WHERE_IDX_ONLY  
2e871 20 20 20 30 78 30 30 38 30 30 30 30 30 20 20 2f     0x00800000  /
2e872 2a 20 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79  * Use index only
2e873 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f   - omit table */
2e874 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f  .#define WHERE_O
2e875 52 44 45 52 42 59 20 20 20 20 20 20 30 78 30 31  RDERBY      0x01
2e876 30 30 30 30 30 30 20 20 2f 2a 20 4f 75 74 70 75  000000  /* Outpu
2e877 74 20 77 69 6c 6c 20 61 70 70 65 61 72 20 69 6e  t will appear in
2e878 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 2a   correct order *
2e879 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e87a 52 45 56 45 52 53 45 20 20 20 20 20 20 30 78 30  REVERSE      0x0
2e87b 32 30 30 30 30 30 30 20 20 2f 2a 20 53 63 61 6e  2000000  /* Scan
2e87c 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
2e87d 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  r */.#define WHE
2e87e 52 45 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20  RE_UNIQUE       
2e87f 30 78 30 34 30 30 30 30 30 30 20 20 2f 2a 20 53  0x04000000  /* S
2e880 65 6c 65 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74  elects no more t
2e881 68 61 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23  han one row */.#
2e882 64 65 66 69 6e 65 20 57 48 45 52 45 5f 56 49 52  define WHERE_VIR
2e883 54 55 41 4c 54 41 42 4c 45 20 30 78 30 38 30 30  TUALTABLE 0x0800
2e884 30 30 30 30 20 20 2f 2a 20 55 73 65 20 76 69 72  0000  /* Use vir
2e885 74 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65  tual-table proce
2e886 73 73 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  ssing */.#define
2e887 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
2e888 20 20 20 20 30 78 31 30 30 30 30 30 30 30 20 20      0x10000000  
2e889 2f 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75 6c 74  /* OR using mult
2e88a 69 70 6c 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a  iple indices */.
2e88b 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
2e88c 65 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64  e a preallocated
2e88d 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72   WhereClause str
2e88e 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ucture..*/.stati
2e88f 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75  c void whereClau
2e890 73 65 49 6e 69 74 28 0a 20 20 57 68 65 72 65 43  seInit(.  WhereC
2e891 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2e892 20 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 43     /* The WhereC
2e893 6c 61 75 73 65 20 74 6f 20 62 65 20 69 6e 69 74  lause to be init
2e894 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72  ialized */.  Par
2e895 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2e896 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2e897 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2e898 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2e899 70 4d 61 73 6b 53 65 74 20 20 20 2f 2a 20 4d 61  pMaskSet   /* Ma
2e89a 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65  pping from table
2e89b 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e89c 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 29  to bitmasks */.)
2e89d 7b 0a 20 20 70 57 43 2d 3e 70 50 61 72 73 65 20  {.  pWC->pParse 
2e89e 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 57 43 2d  = pParse;.  pWC-
2e89f 3e 70 4d 61 73 6b 53 65 74 20 3d 20 70 4d 61 73  >pMaskSet = pMas
2e8a0 6b 53 65 74 3b 0a 20 20 70 57 43 2d 3e 6e 54 65  kSet;.  pWC->nTe
2e8a1 72 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e  rm = 0;.  pWC->n
2e8a2 53 6c 6f 74 20 3d 20 41 72 72 61 79 53 69 7a 65  Slot = ArraySize
2e8a3 28 70 57 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a  (pWC->aStatic);.
2e8a4 20 20 70 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e    pWC->a = pWC->
2e8a5 61 53 74 61 74 69 63 3b 0a 20 20 70 57 43 2d 3e  aStatic;.  pWC->
2e8a6 76 6d 61 73 6b 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  vmask = 0;.}../*
2e8a7 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
2e8a8 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ce */.static voi
2e8a9 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65  d whereClauseCle
2e8aa 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 2a 29  ar(WhereClause*)
2e8ab 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  ;../*.** Dealloc
2e8ac 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61  ate all memory a
2e8ad 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
2e8ae 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a   WhereOrInfo obj
2e8af 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ect..*/.static v
2e8b0 6f 69 64 20 77 68 65 72 65 4f 72 49 6e 66 6f 44  oid whereOrInfoD
2e8b1 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
2e8b2 62 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a  b, WhereOrInfo *
2e8b3 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  p){.  whereClaus
2e8b4 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a  eClear(&p->wc);.
2e8b5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e8b6 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  db, p);.}../*.**
2e8b7 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20   Deallocate all 
2e8b8 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
2e8b9 64 20 77 69 74 68 20 61 20 57 68 65 72 65 41 6e  d with a WhereAn
2e8ba 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f  dInfo object..*/
2e8bb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2e8bc 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28  reAndInfoDelete(
2e8bd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65  sqlite3 *db, Whe
2e8be 72 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20  reAndInfo *p){. 
2e8bf 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61   whereClauseClea
2e8c0 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c  r(&p->wc);.  sql
2e8c1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2e8c2 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c  );.}../*.** Deal
2e8c3 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65 43 6c  locate a WhereCl
2e8c4 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  ause structure. 
2e8c5 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
2e8c6 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74   structure.** it
2e8c7 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72 65 65  self is not free
2e8c8 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  d.  This routine
2e8c9 20 69 73 20 74 68 65 20 69 6e 76 65 72 73 65 20   is the inverse 
2e8ca 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  of whereClauseIn
2e8cb 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  it()..*/.static 
2e8cc 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65  void whereClause
2e8cd 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75 73  Clear(WhereClaus
2e8ce 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20 69  e *pWC){.  int i
2e8cf 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61  ;.  WhereTerm *a
2e8d0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2e8d1 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64  = pWC->pParse->d
2e8d2 62 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e  b;.  for(i=pWC->
2e8d3 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e  nTerm-1, a=pWC->
2e8d4 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b  a; i>=0; i--, a+
2e8d5 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e 77  +){.    if( a->w
2e8d6 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59  tFlags & TERM_DY
2e8d7 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73  NAMIC ){.      s
2e8d8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2e8d9 28 64 62 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a  (db, a->pExpr);.
2e8da 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 61 2d      }.    if( a-
2e8db 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2e8dc 4f 52 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20  ORINFO ){.      
2e8dd 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74  whereOrInfoDelet
2e8de 65 28 64 62 2c 20 61 2d 3e 75 2e 70 4f 72 49 6e  e(db, a->u.pOrIn
2e8df 66 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  fo);.    }else i
2e8e0 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20  f( a->wtFlags & 
2e8e1 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b 0a  TERM_ANDINFO ){.
2e8e2 20 20 20 20 20 20 77 68 65 72 65 41 6e 64 49 6e        whereAndIn
2e8e3 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e  foDelete(db, a->
2e8e4 75 2e 70 41 6e 64 49 6e 66 6f 29 3b 0a 20 20 20  u.pAndInfo);.   
2e8e5 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 43   }.  }.  if( pWC
2e8e6 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74 69  ->a!=pWC->aStati
2e8e7 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  c ){.    sqlite3
2e8e8 44 62 46 72 65 65 28 64 62 2c 20 70 57 43 2d 3e  DbFree(db, pWC->
2e8e9 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  a);.  }.}../*.**
2e8ea 20 41 64 64 20 61 20 73 69 6e 67 6c 65 20 6e 65   Add a single ne
2e8eb 77 20 57 68 65 72 65 54 65 72 6d 20 65 6e 74 72  w WhereTerm entr
2e8ec 79 20 74 6f 20 74 68 65 20 57 68 65 72 65 43 6c  y to the WhereCl
2e8ed 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43 2e  ause object pWC.
2e8ee 0a 2a 2a 20 54 68 65 20 6e 65 77 20 57 68 65 72  .** The new Wher
2e8ef 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20  eTerm object is 
2e8f0 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d  constructed from
2e8f1 20 45 78 70 72 20 70 20 61 6e 64 20 77 69 74 68   Expr p and with
2e8f2 20 77 74 46 6c 61 67 73 2e 0a 2a 2a 20 54 68 65   wtFlags..** The
2e8f3 20 69 6e 64 65 78 20 69 6e 20 70 57 43 2d 3e 61   index in pWC->a
2e8f4 5b 5d 20 6f 66 20 74 68 65 20 6e 65 77 20 57 68  [] of the new Wh
2e8f5 65 72 65 54 65 72 6d 20 69 73 20 72 65 74 75 72  ereTerm is retur
2e8f6 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a  ned on success..
2e8f7 2a 2a 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  ** 0 is returned
2e8f8 20 69 66 20 74 68 65 20 6e 65 77 20 57 68 65 72   if the new Wher
2e8f9 65 54 65 72 6d 20 63 6f 75 6c 64 20 6e 6f 74 20  eTerm could not 
2e8fa 62 65 20 61 64 64 65 64 20 64 75 65 20 74 6f 20  be added due to 
2e8fb 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  a memory.** allo
2e8fc 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 20 20 54  cation error.  T
2e8fd 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
2e8fe 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 77 69 6c  tion failure wil
2e8ff 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20 69 6e  l be recorded in
2e900 0a 2a 2a 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c  .** the db->mall
2e901 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 73 6f  ocFailed flag so
2e902 20 74 68 61 74 20 68 69 67 68 65 72 2d 6c 65 76   that higher-lev
2e903 65 6c 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6e  el functions can
2e904 20 64 65 74 65 63 74 20 69 74 2e 0a 2a 2a 0a 2a   detect it..**.*
2e905 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
2e906 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65  ill increase the
2e907 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 57 43   size of the pWC
2e908 2d 3e 61 5b 5d 20 61 72 72 61 79 20 61 73 20 6e  ->a[] array as n
2e909 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20  ecessary..**.** 
2e90a 49 66 20 74 68 65 20 77 74 46 6c 61 67 73 20 61  If the wtFlags a
2e90b 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73  rgument includes
2e90c 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74   TERM_DYNAMIC, t
2e90d 68 65 6e 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  hen responsibili
2e90e 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e  ty.** for freein
2e90f 67 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  g the expression
2e910 20 70 20 69 73 20 61 73 73 75 6d 65 64 20 62 79   p is assumed by
2e911 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2e912 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20   object pWC..** 
2e913 54 68 69 73 20 69 73 20 74 72 75 65 20 65 76 65  This is true eve
2e914 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  n if this routin
2e915 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63  e fails to alloc
2e916 61 74 65 20 61 20 6e 65 77 20 57 68 65 72 65 54  ate a new WhereT
2e917 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49  erm..**.** WARNI
2e918 4e 47 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e  NG:  This routin
2e919 65 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63 61  e might realloca
2e91a 74 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65  te the space use
2e91b 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68  d to store.** Wh
2e91c 65 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70  ereTerms.  All p
2e91d 6f 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72 65  ointers to Where
2e91e 54 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65 20  Terms should be 
2e91f 69 6e 76 61 6c 69 64 61 74 65 64 20 61 66 74 65  invalidated afte
2e920 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69  r.** calling thi
2e921 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75 63 68  s routine.  Such
2e922 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20 62 65   pointers may be
2e923 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 62   reinitialized b
2e924 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a  y referencing.**
2e925 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72   the pWC->a[] ar
2e926 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ray..*/.static i
2e927 6e 74 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  nt whereClauseIn
2e928 73 65 72 74 28 57 68 65 72 65 43 6c 61 75 73 65  sert(WhereClause
2e929 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 2c 20   *pWC, Expr *p, 
2e92a 75 38 20 77 74 46 6c 61 67 73 29 7b 0a 20 20 57  u8 wtFlags){.  W
2e92b 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2e92c 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 69 66  .  int idx;.  if
2e92d 28 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57  ( pWC->nTerm>=pW
2e92e 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20 20  C->nSlot ){.    
2e92f 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64 20  WhereTerm *pOld 
2e930 3d 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20 73 71  = pWC->a;.    sq
2e931 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d  lite3 *db = pWC-
2e932 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20  >pParse->db;.   
2e933 20 70 57 43 2d 3e 61 20 3d 20 73 71 6c 69 74 65   pWC->a = sqlite
2e934 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2e935 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30   sizeof(pWC->a[0
2e936 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20  ])*pWC->nSlot*2 
2e937 29 3b 0a 20 20 20 20 69 66 28 20 70 57 43 2d 3e  );.    if( pWC->
2e938 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  a==0 ){.      if
2e939 28 20 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ( wtFlags & TERM
2e93a 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20  _DYNAMIC ){.    
2e93b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2e93c 65 6c 65 74 65 28 64 62 2c 20 70 29 3b 0a 20 20  elete(db, p);.  
2e93d 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57 43 2d      }.      pWC-
2e93e 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 20  >a = pOld;.     
2e93f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
2e940 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43 2d  .    memcpy(pWC-
2e941 3e 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f 66  >a, pOld, sizeof
2e942 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d  (pWC->a[0])*pWC-
2e943 3e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 66 28  >nTerm);.    if(
2e944 20 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53 74 61   pOld!=pWC->aSta
2e945 74 69 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tic ){.      sql
2e946 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2e947 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Old);.    }.    
2e948 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 73 71 6c  pWC->nSlot = sql
2e949 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
2e94a 28 64 62 2c 20 70 57 43 2d 3e 61 29 2f 73 69 7a  (db, pWC->a)/siz
2e94b 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 3b 0a  eof(pWC->a[0]);.
2e94c 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70    }.  pTerm = &p
2e94d 57 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43 2d  WC->a[idx = pWC-
2e94e 3e 6e 54 65 72 6d 2b 2b 5d 3b 0a 20 20 70 54 65  >nTerm++];.  pTe
2e94f 72 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20  rm->pExpr = p;. 
2e950 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2e951 3d 20 77 74 46 6c 61 67 73 3b 0a 20 20 70 54 65  = wtFlags;.  pTe
2e952 72 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b 0a 20  rm->pWC = pWC;. 
2e953 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20   pTerm->iParent 
2e954 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 69  = -1;.  return i
2e955 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dx;.}../*.** Thi
2e956 73 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74 69  s routine identi
2e957 66 69 65 73 20 73 75 62 65 78 70 72 65 73 73 69  fies subexpressi
2e958 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  ons in the WHERE
2e959 20 63 6c 61 75 73 65 20 77 68 65 72 65 0a 2a 2a   clause where.**
2e95a 20 65 61 63 68 20 73 75 62 65 78 70 72 65 73 73   each subexpress
2e95b 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64  ion is separated
2e95c 20 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65 72   by the AND oper
2e95d 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74 68  ator or some oth
2e95e 65 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73  er.** operator s
2e95f 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2e960 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54  op parameter.  T
2e961 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  he WhereClause s
2e962 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 66  tructure.** is f
2e963 69 6c 6c 65 64 20 77 69 74 68 20 70 6f 69 6e 74  illed with point
2e964 65 72 73 20 74 6f 20 73 75 62 65 78 70 72 65 73  ers to subexpres
2e965 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78 61 6d  sions.  For exam
2e966 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48  ple:.**.**    WH
2e967 45 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f 27 20  ERE  a=='hello' 
2e968 41 4e 44 20 63 6f 61 6c 65 73 63 65 28 62 2c 31  AND coalesce(b,1
2e969 31 29 3c 31 30 20 41 4e 44 20 28 63 2b 31 32 21  1)<10 AND (c+12!
2e96a 3d 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a 20  =d OR c==22).** 
2e96b 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f            \_____
2e96c 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
2e96d 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c  _________/     \
2e96e 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2e96f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  /.**            
2e970 73 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20 20 20  slot[0]         
2e971 20 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20 20 20     slot[1]      
2e972 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 32 5d           slot[2]
2e973 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69  .**.** The origi
2e974 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
2e975 20 69 6e 20 70 45 78 70 72 20 69 73 20 75 6e 61   in pExpr is una
2e976 6c 74 65 72 65 64 2e 20 20 41 6c 6c 20 74 68 69  ltered.  All thi
2e977 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65  s routine.** doe
2e978 73 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d  s is make slot[]
2e979 20 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 20 74   entries point t
2e97a 6f 20 73 75 62 73 74 72 75 63 74 75 72 65 20 77  o substructure w
2e97b 69 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a  ithin pExpr..**.
2e97c 2a 2a 20 49 6e 20 74 68 65 20 70 72 65 76 69 6f  ** In the previo
2e97d 75 73 20 73 65 6e 74 65 6e 63 65 20 61 6e 64 20  us sentence and 
2e97e 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 2c 20  in the diagram, 
2e97f 22 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72 73 20  "slot[]" refers 
2e980 74 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72 65 43  to.** the WhereC
2e981 6c 61 75 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e  lause.a[] array.
2e982 20 20 54 68 65 20 73 6c 6f 74 5b 5d 20 61 72 72    The slot[] arr
2e983 61 79 20 67 72 6f 77 73 20 61 73 20 6e 65 65 64  ay grows as need
2e984 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a  ed to contain.**
2e985 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   all terms of th
2e986 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  e WHERE clause..
2e987 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2e988 68 65 72 65 53 70 6c 69 74 28 57 68 65 72 65 43  hereSplit(WhereC
2e989 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72  lause *pWC, Expr
2e98a 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70 29   *pExpr, int op)
2e98b 7b 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20 28 75  {.  pWC->op = (u
2e98c 38 29 6f 70 3b 0a 20 20 69 66 28 20 70 45 78 70  8)op;.  if( pExp
2e98d 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
2e98e 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2e98f 6f 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65 43  op ){.    whereC
2e990 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c  lauseInsert(pWC,
2e991 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 65   pExpr, 0);.  }e
2e992 6c 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53 70  lse{.    whereSp
2e993 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e  lit(pWC, pExpr->
2e994 70 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20 20  pLeft, op);.    
2e995 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20  whereSplit(pWC, 
2e996 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
2e997 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
2e998 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20 65   Initialize an e
2e999 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73  xpression mask s
2e99a 65 74 20 28 61 20 57 68 65 72 65 4d 61 73 6b 53  et (a WhereMaskS
2e99b 65 74 20 6f 62 6a 65 63 74 29 0a 2a 2f 0a 23 64  et object).*/.#d
2e99c 65 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65  efine initMaskSe
2e99d 74 28 50 29 20 20 6d 65 6d 73 65 74 28 50 2c 20  t(P)  memset(P, 
2e99e 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29 0a 0a  0, sizeof(*P))..
2e99f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2e9a0 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65   bitmask for the
2e9a1 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20 6e 75   given cursor nu
2e9a2 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20 30 20  mber.  Return 0 
2e9a3 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20 69 73  if.** iCursor is
2e9a4 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65 74 2e   not in the set.
2e9a5 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61  .*/.static Bitma
2e9a6 73 6b 20 67 65 74 4d 61 73 6b 28 57 68 65 72 65  sk getMask(Where
2e9a7 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2e9a8 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b  t, int iCursor){
2e9a9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
2e9aa 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c  rt( pMaskSet->n<
2e9ab 3d 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29  =sizeof(Bitmask)
2e9ac 2a 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  *8 );.  for(i=0;
2e9ad 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20   i<pMaskSet->n; 
2e9ae 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4d  i++){.    if( pM
2e9af 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69  askSet->ix[i]==i
2e9b0 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
2e9b1 72 65 74 75 72 6e 20 28 28 42 69 74 6d 61 73 6b  return ((Bitmask
2e9b2 29 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20  )1)<<i;.    }.  
2e9b3 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2e9b4 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
2e9b5 6e 65 77 20 6d 61 73 6b 20 66 6f 72 20 63 75 72  new mask for cur
2e9b6 73 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a  sor iCursor..**.
2e9b7 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  ** There is one 
2e9b8 63 75 72 73 6f 72 20 70 65 72 20 74 61 62 6c 65  cursor per table
2e9b9 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2e9ba 75 73 65 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  use.  The number
2e9bb 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e   of.** tables in
2e9bc 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2e9bd 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 61   is limited by a
2e9be 20 74 65 73 74 20 65 61 72 6c 79 20 69 6e 20 74   test early in t
2e9bf 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65  he.** sqlite3Whe
2e9c0 72 65 42 65 67 69 6e 28 29 20 72 6f 75 74 69 6e  reBegin() routin
2e9c1 65 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74  e.  So we know t
2e9c2 68 61 74 20 74 68 65 20 70 4d 61 73 6b 53 65 74  hat the pMaskSet
2e9c3 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20  ->ix[].** array 
2e9c4 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 66  will never overf
2e9c5 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  low..*/.static v
2e9c6 6f 69 64 20 63 72 65 61 74 65 4d 61 73 6b 28 57  oid createMask(W
2e9c7 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2e9c8 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73  skSet, int iCurs
2e9c9 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  or){.  assert( p
2e9ca 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72  MaskSet->n < Arr
2e9cb 61 79 53 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d  aySize(pMaskSet-
2e9cc 3e 69 78 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53  >ix) );.  pMaskS
2e9cd 65 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d  et->ix[pMaskSet-
2e9ce 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b  >n++] = iCursor;
2e9cf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2e9d0 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 28 72 65  outine walks (re
2e9d1 63 75 72 73 69 76 65 6c 79 29 20 61 6e 20 65 78  cursively) an ex
2e9d2 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e  pression tree an
2e9d3 64 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61  d generates.** a
2e9d4 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74   bitmask indicat
2e9d5 69 6e 67 20 77 68 69 63 68 20 74 61 62 6c 65 73  ing which tables
2e9d6 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 61   are used in tha
2e9d7 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  t expression.** 
2e9d8 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f  tree..**.** In o
2e9d9 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 72 6f  rder for this ro
2e9da 75 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74  utine to work, t
2e9db 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2e9dc 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a  ion must have.**
2e9dd 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f   previously invo
2e9de 6b 65 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ked sqlite3Resol
2e9df 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e  veExprNames() on
2e9e0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
2e9e1 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 68 65 61    See.** the hea
2e9e2 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74  der comment on t
2e9e3 68 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  hat routine for 
2e9e4 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2e9e5 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73  mation..** The s
2e9e6 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2e9e7 72 4e 61 6d 65 73 28 29 20 72 6f 75 74 69 6e 65  rNames() routine
2e9e8 73 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75  s looks for colu
2e9e9 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20  mn names and.** 
2e9ea 73 65 74 73 20 74 68 65 69 72 20 6f 70 63 6f 64  sets their opcod
2e9eb 65 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  es to TK_COLUMN 
2e9ec 61 6e 64 20 74 68 65 69 72 20 45 78 70 72 2e 69  and their Expr.i
2e9ed 54 61 62 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a  Table fields to.
2e9ee 2a 2a 20 74 68 65 20 56 44 42 45 20 63 75 72 73  ** the VDBE curs
2e9ef 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
2e9f0 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f   table.  This ro
2e9f1 75 74 69 6e 65 20 6a 75 73 74 20 68 61 73 20 74  utine just has t
2e9f2 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 20 74  o.** translate t
2e9f3 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
2e9f4 73 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76  s into bitmask v
2e9f5 61 6c 75 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c  alues and OR all
2e9f6 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 73  .** the bitmasks
2e9f7 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74   together..*/.st
2e9f8 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70  atic Bitmask exp
2e9f9 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2e9fa 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45  WhereMaskSet*, E
2e9fb 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69  xprList*);.stati
2e9fc 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
2e9fd 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
2e9fe 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65  hereMaskSet*, Se
2e9ff 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69 63 20 42  lect*);.static B
2ea00 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c 65  itmask exprTable
2ea01 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53  Usage(WhereMaskS
2ea02 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78  et *pMaskSet, Ex
2ea03 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73  pr *p){.  Bitmas
2ea04 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66  k mask = 0;.  if
2ea05 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
2ea06 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  0;.  if( p->op==
2ea07 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2ea08 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28   mask = getMask(
2ea09 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61  pMaskSet, p->iTa
2ea0a 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ble);.    return
2ea0b 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73   mask;.  }.  mas
2ea0c 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  k = exprTableUsa
2ea0d 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
2ea0e 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20  pRight);.  mask 
2ea0f 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67  |= exprTableUsag
2ea10 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70  e(pMaskSet, p->p
2ea11 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 45 78 70  Left);.  if( Exp
2ea12 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
2ea13 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2ea14 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2ea15 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67  rSelectTableUsag
2ea16 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78  e(pMaskSet, p->x
2ea17 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c  .pSelect);.  }el
2ea18 73 65 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  se{.    mask |= 
2ea19 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2ea1a 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
2ea1b 78 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20  x.pList);.  }.  
2ea1c 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73  return mask;.}.s
2ea1d 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2ea1e 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
2ea1f 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2ea20 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69 73  MaskSet, ExprLis
2ea21 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
2ea22 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61   i;.  Bitmask ma
2ea23 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c  sk = 0;.  if( pL
2ea24 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ist ){.    for(i
2ea25 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
2ea26 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2ea27 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c  mask |= exprTabl
2ea28 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2ea29 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
2ea2a 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pr);.    }.  }. 
2ea2b 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
2ea2c 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
2ea2d 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
2ea2e 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74  age(WhereMaskSet
2ea2f 20 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65   *pMaskSet, Sele
2ea30 63 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61  ct *pS){.  Bitma
2ea31 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77  sk mask = 0;.  w
2ea32 68 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20 20  hile( pS ){.    
2ea33 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74  mask |= exprList
2ea34 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2ea35 53 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29  Set, pS->pEList)
2ea36 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  ;.    mask |= ex
2ea37 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
2ea38 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70  (pMaskSet, pS->p
2ea39 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d 61  GroupBy);.    ma
2ea3a 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61  sk |= exprListTa
2ea3b 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2ea3c 74 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29  t, pS->pOrderBy)
2ea3d 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  ;.    mask |= ex
2ea3e 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2ea3f 73 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72  skSet, pS->pWher
2ea40 65 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  e);.    mask |= 
2ea41 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2ea42 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61  MaskSet, pS->pHa
2ea43 76 69 6e 67 29 3b 0a 20 20 20 20 70 53 20 3d 20  ving);.    pS = 
2ea44 70 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  pS->pPrior;.  }.
2ea45 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d    return mask;.}
2ea46 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
2ea47 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
2ea48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 6f 6e 65   operator is one
2ea49 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72   of the operator
2ea4a 73 20 74 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c  s that is.** all
2ea4b 6f 77 65 64 20 66 6f 72 20 61 6e 20 69 6e 64 65  owed for an inde
2ea4c 78 61 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75  xable WHERE clau
2ea4d 73 65 20 74 65 72 6d 2e 20 20 54 68 65 20 61 6c  se term.  The al
2ea4e 6c 6f 77 65 64 20 6f 70 65 72 61 74 6f 72 73 20  lowed operators 
2ea4f 61 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c  are.** "=", "<",
2ea50 20 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22   ">", "<=", ">="
2ea51 2c 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73  , and "IN"..*/.s
2ea52 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65  tatic int allowe
2ea53 64 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61  dOp(int op){.  a
2ea54 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
2ea55 45 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47  EQ && TK_GT<TK_G
2ea56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54  E );.  assert( T
2ea57 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b  K_LT>TK_EQ && TK
2ea58 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61  _LT<TK_GE );.  a
2ea59 73 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f  ssert( TK_LE>TK_
2ea5a 45 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47  EQ && TK_LE<TK_G
2ea5b 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54  E );.  assert( T
2ea5c 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b  K_GE==TK_EQ+4 );
2ea5d 0a 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b  .  return op==TK
2ea5e 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45  _IN || (op>=TK_E
2ea5f 51 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20  Q && op<=TK_GE) 
2ea60 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  || op==TK_ISNULL
2ea61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20  ;.}../*.** Swap 
2ea62 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20 74  two objects of t
2ea63 79 70 65 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65  ype TYPE..*/.#de
2ea64 66 69 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41  fine SWAP(TYPE,A
2ea65 2c 42 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41  ,B) {TYPE t=A; A
2ea66 3d 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a  =B; B=t;}../*.**
2ea67 20 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61   Commute a compa
2ea68 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20  rison operator. 
2ea69 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
2ea6a 74 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59  the form "X op Y
2ea6b 22 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74  ".** are convert
2ea6c 65 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22  ed into "Y op X"
2ea6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c  ..**.** If a col
2ea6e 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2ea6f 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  is associated wi
2ea70 74 68 20 65 69 74 68 65 72 20 74 68 65 20 6c 65  th either the le
2ea71 66 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73  ft or right.** s
2ea72 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61  ide of the compa
2ea73 72 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e  rison, it remain
2ea74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2ea75 68 20 74 68 65 20 73 61 6d 65 20 73 69 64 65 20  h the same side 
2ea76 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d  after.** the com
2ea77 6d 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20  mutation. So "Y 
2ea78 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f  collate NOCASE o
2ea79 70 20 58 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a  p X" becomes .**
2ea7a 20 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41   "X collate NOCA
2ea7b 53 45 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69  SE op Y". This i
2ea7c 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f  s because any co
2ea7d 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2ea7e 20 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20   on.** the left 
2ea7f 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63  hand side of a c
2ea80 6f 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69  omparison overri
2ea81 64 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f  des any collatio
2ea82 6e 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61  n sequence .** a
2ea83 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72  ttached to the r
2ea84 69 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61  ight. For the sa
2ea85 6d 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50  me reason the EP
2ea86 5f 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67  _ExpCollate flag
2ea87 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75  .** is not commu
2ea88 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ted..*/.static v
2ea89 6f 69 64 20 65 78 70 72 43 6f 6d 6d 75 74 65 28  oid exprCommute(
2ea8a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2ea8b 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 75  xpr *pExpr){.  u
2ea8c 31 36 20 65 78 70 52 69 67 68 74 20 3d 20 28 70  16 expRight = (p
2ea8d 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c  Expr->pRight->fl
2ea8e 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
2ea8f 61 74 65 29 3b 0a 20 20 75 31 36 20 65 78 70 4c  ate);.  u16 expL
2ea90 65 66 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c  eft = (pExpr->pL
2ea91 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  eft->flags & EP_
2ea92 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61  ExpCollate);.  a
2ea93 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70  ssert( allowedOp
2ea94 28 70 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70  (pExpr->op) && p
2ea95 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20  Expr->op!=TK_IN 
2ea96 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67  );.  pExpr->pRig
2ea97 68 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ht->pColl = sqli
2ea98 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2ea99 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
2ea9a 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e  ight);.  pExpr->
2ea9b 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73  pLeft->pColl = s
2ea9c 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
2ea9d 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  q(pParse, pExpr-
2ea9e 3e 70 4c 65 66 74 29 3b 0a 20 20 53 57 41 50 28  >pLeft);.  SWAP(
2ea9f 43 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e  CollSeq*,pExpr->
2eaa0 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45  pRight->pColl,pE
2eaa1 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c  xpr->pLeft->pCol
2eaa2 6c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69  l);.  pExpr->pRi
2eaa3 67 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45  ght->flags = (pE
2eaa4 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
2eaa5 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c  gs & ~EP_ExpColl
2eaa6 61 74 65 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a  ate) | expLeft;.
2eaa7 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e    pExpr->pLeft->
2eaa8 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e  flags = (pExpr->
2eaa9 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e  pLeft->flags & ~
2eaaa 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c  EP_ExpCollate) |
2eaab 20 65 78 70 52 69 67 68 74 3b 0a 20 20 53 57 41   expRight;.  SWA
2eaac 50 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70  P(Expr*,pExpr->p
2eaad 52 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65  Right,pExpr->pLe
2eaae 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72  ft);.  if( pExpr
2eaaf 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20  ->op>=TK_GT ){. 
2eab0 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
2eab1 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20  ==TK_GT+2 );.   
2eab2 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d   assert( TK_GE==
2eab3 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61  TK_LE+2 );.    a
2eab4 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f  ssert( TK_GT>TK_
2eab5 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
2eab6 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b  ( TK_GT<TK_LE );
2eab7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
2eab8 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26  pr->op>=TK_GT &&
2eab9 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47   pExpr->op<=TK_G
2eaba 45 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  E );.    pExpr->
2eabb 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70  op = ((pExpr->op
2eabc 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54  -TK_GT)^2)+TK_GT
2eabd 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2eabe 72 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b  ranslate from TK
2eabf 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  _xx operator to 
2eac0 57 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a  WO_xx bitmask..*
2eac1 2f 0a 73 74 61 74 69 63 20 75 31 36 20 6f 70 65  /.static u16 ope
2eac2 72 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70  ratorMask(int op
2eac3 29 7b 0a 20 20 75 31 36 20 63 3b 0a 20 20 61 73  ){.  u16 c;.  as
2eac4 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28  sert( allowedOp(
2eac5 6f 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d  op) );.  if( op=
2eac6 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20  =TK_IN ){.    c 
2eac7 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65  = WO_IN;.  }else
2eac8 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55   if( op==TK_ISNU
2eac9 4c 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f  LL ){.    c = WO
2eaca 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65  _ISNULL;.  }else
2eacb 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 57  {.    assert( (W
2eacc 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29  O_EQ<<(op-TK_EQ)
2eacd 29 20 3c 20 30 78 37 66 66 66 20 29 3b 0a 20 20  ) < 0x7fff );.  
2eace 20 20 63 20 3d 20 28 75 31 36 29 28 57 4f 5f 45    c = (u16)(WO_E
2eacf 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a  Q<<(op-TK_EQ));.
2ead0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70    }.  assert( op
2ead1 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63  !=TK_ISNULL || c
2ead2 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
2ead3 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2ead4 49 4e 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29  IN || c==WO_IN )
2ead5 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2ead6 54 4b 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45  TK_EQ || c==WO_E
2ead7 51 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  Q );.  assert( o
2ead8 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57  p!=TK_LT || c==W
2ead9 4f 5f 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74  O_LT );.  assert
2eada 28 20 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63  ( op!=TK_LE || c
2eadb 3d 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73  ==WO_LE );.  ass
2eadc 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c  ert( op!=TK_GT |
2eadd 7c 20 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20  | c==WO_GT );.  
2eade 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47  assert( op!=TK_G
2eadf 45 20 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b  E || c==WO_GE );
2eae0 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
2eae1 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72  /*.** Search for
2eae2 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
2eae3 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
2eae4 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
2eae5 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a  "X <op> <expr>".
2eae6 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 20  ** where X is a 
2eae7 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
2eae8 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c   iColumn of tabl
2eae9 65 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20  e iCur and <op> 
2eaea 69 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65  is one of.** the
2eaeb 20 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 20   WO_xx operator 
2eaec 63 6f 64 65 73 20 73 70 65 63 69 66 69 65 64 20  codes specified 
2eaed 62 79 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65  by the op parame
2eaee 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ter..** Return a
2eaef 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2eaf0 74 65 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 20  term.  Return 0 
2eaf1 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f  if not found..*/
2eaf2 0a 73 74 61 74 69 63 20 57 68 65 72 65 54 65 72  .static WhereTer
2eaf3 6d 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20 20 57  m *findTerm(.  W
2eaf4 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2eaf5 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2eaf6 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 73  E clause to be s
2eaf7 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74  earched */.  int
2eaf8 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
2eaf9 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
2eafa 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20  ber of LHS */.  
2eafb 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
2eafc 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
2eafd 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f  number of LHS */
2eafe 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2eaff 61 64 79 2c 20 20 20 20 20 2f 2a 20 52 48 53 20  ady,     /* RHS 
2eb00 6d 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70  must not overlap
2eb01 20 77 69 74 68 20 74 68 69 73 20 6d 61 73 6b 20   with this mask 
2eb02 2a 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20 20 20  */.  u32 op,    
2eb03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2eb04 73 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75  sk of WO_xx valu
2eb05 65 73 20 64 65 73 63 72 69 62 69 6e 67 20 6f 70  es describing op
2eb06 65 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65  erator */.  Inde
2eb07 78 20 2a 70 49 64 78 20 20 20 20 20 20 20 20 20  x *pIdx         
2eb08 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d    /* Must be com
2eb09 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 68 69  patible with thi
2eb0a 73 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20  s index, if not 
2eb0b 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  NULL */.){.  Whe
2eb0c 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20  reTerm *pTerm;. 
2eb0d 20 69 6e 74 20 6b 3b 0a 20 20 61 73 73 65 72 74   int k;.  assert
2eb0e 28 20 69 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f  ( iCur>=0 );.  o
2eb0f 70 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66  p &= WO_ALL;.  f
2eb10 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c  or(pTerm=pWC->a,
2eb11 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b   k=pWC->nTerm; k
2eb12 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; k--, pTerm++){
2eb13 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2eb14 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
2eb15 0a 20 20 20 20 20 20 20 26 26 20 28 70 54 65 72  .       && (pTer
2eb16 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 26  m->prereqRight &
2eb17 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20   notReady)==0.  
2eb18 20 20 20 20 20 26 26 20 70 54 65 72 6d 2d 3e 75       && pTerm->u
2eb19 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f  .leftColumn==iCo
2eb1a 6c 75 6d 6e 0a 20 20 20 20 20 20 20 26 26 20 28  lumn.       && (
2eb1b 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2eb1c 20 26 20 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b   & op)!=0.    ){
2eb1d 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20  .      if( pIdx 
2eb1e 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  && pTerm->eOpera
2eb1f 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29  tor!=WO_ISNULL )
2eb20 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
2eb21 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pX = pTerm->pExp
2eb22 72 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  r;.        CollS
2eb23 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  eq *pColl;.     
2eb24 20 20 20 63 68 61 72 20 69 64 78 61 66 66 3b 0a     char idxaff;.
2eb25 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
2eb26 20 20 20 20 20 20 20 50 61 72 73 65 20 2a 70 50         Parse *pP
2eb27 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2eb28 73 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 64 78  se;..        idx
2eb29 61 66 66 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  aff = pIdx->pTab
2eb2a 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e  le->aCol[iColumn
2eb2b 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20  ].affinity;.    
2eb2c 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
2eb2d 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28  IndexAffinityOk(
2eb2e 70 58 2c 20 69 64 78 61 66 66 29 20 29 20 63 6f  pX, idxaff) ) co
2eb2f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20  ntinue;..       
2eb30 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
2eb31 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
2eb32 75 65 6e 63 65 20 72 65 71 75 69 72 65 64 20 66  uence required f
2eb33 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f 72  rom an index for
2eb34 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 74  .        ** it t
2eb35 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  o be useful for 
2eb36 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70 72 65  optimising expre
2eb37 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20  ssion pX. Store 
2eb38 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
2eb39 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61 62 6c  value in variabl
2eb3a 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20 20 20  e pColl..       
2eb3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
2eb3c 72 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b 0a 20  rt(pX->pLeft);. 
2eb3d 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
2eb3e 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
2eb3f 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  areCollSeq(pPars
2eb40 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58  e, pX->pLeft, pX
2eb41 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
2eb42 20 20 20 61 73 73 65 72 74 28 70 43 6f 6c 6c 20     assert(pColl 
2eb43 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 29  || pParse->nErr)
2eb44 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  ;..        for(j
2eb45 3d 30 3b 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  =0; pIdx->aiColu
2eb46 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20  mn[j]!=iColumn; 
2eb47 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
2eb48 69 66 28 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64  if( NEVER(j>=pId
2eb49 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65  x->nColumn) ) re
2eb4a 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
2eb4b 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  }.        if( pC
2eb4c 6f 6c 6c 20 26 26 20 73 71 6c 69 74 65 33 53 74  oll && sqlite3St
2eb4d 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61  rICmp(pColl->zNa
2eb4e 6d 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  me, pIdx->azColl
2eb4f 5b 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  [j]) ) continue;
2eb50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
2eb51 65 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20  eturn pTerm;.   
2eb52 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2eb53 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  0;.}../* Forward
2eb54 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
2eb55 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
2eb56 61 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20  alyze(SrcList*, 
2eb57 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e  WhereClause*, in
2eb58 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  t);../*.** Call 
2eb59 65 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61  exprAnalyze on a
2eb5a 6c 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48  ll terms in a WH
2eb5b 45 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a  ERE clause.  .**
2eb5c 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .**.*/.static vo
2eb5d 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  id exprAnalyzeAl
2eb5e 6c 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  l(.  SrcList *pT
2eb5f 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  abList,       /*
2eb60 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2eb61 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2eb62 65 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f  e *pWC         /
2eb63 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
2eb64 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  se to be analyze
2eb65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  d */.){.  int i;
2eb66 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54  .  for(i=pWC->nT
2eb67 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  erm-1; i>=0; i--
2eb68 29 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  ){.    exprAnaly
2eb69 7a 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43  ze(pTabList, pWC
2eb6a 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  , i);.  }.}..#if
2eb6b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2eb6c 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
2eb6d 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  ON./*.** Check t
2eb6e 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76  o see if the giv
2eb6f 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
2eb70 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20   a LIKE or GLOB 
2eb71 6f 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a  operator that.**
2eb72 20 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65   can be optimize
2eb73 64 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69  d using inequali
2eb74 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ty constraints. 
2eb75 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
2eb76 69 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20  it is.** so and 
2eb77 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a  false if not..**
2eb78 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72  .** In order for
2eb79 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
2eb7a 20 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c   be optimizible,
2eb7b 20 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65   the RHS must be
2eb7c 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74   a string.** lit
2eb7d 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e  eral that does n
2eb7e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
2eb7f 77 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73  wildcard.  .*/.s
2eb80 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65  tatic int isLike
2eb81 4f 72 47 6c 6f 62 28 0a 20 20 50 61 72 73 65 20  OrGlob(.  Parse 
2eb82 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
2eb83 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
2eb84 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
2eb85 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
2eb86 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73  xpr,      /* Tes
2eb87 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
2eb88 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70  n */.  Expr **pp
2eb89 50 72 65 66 69 78 2c 20 20 2f 2a 20 50 6f 69 6e  Prefix,  /* Poin
2eb8a 74 65 72 20 74 6f 20 54 4b 5f 53 54 52 49 4e 47  ter to TK_STRING
2eb8b 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68   expression with
2eb8c 20 70 61 74 74 65 72 6e 20 70 72 65 66 69 78 20   pattern prefix 
2eb8d 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d  */.  int *pisCom
2eb8e 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65 20 69  plete, /* True i
2eb8f 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63  f the only wildc
2eb90 61 72 64 20 69 73 20 25 20 69 6e 20 74 68 65 20  ard is % in the 
2eb91 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 2a  last character *
2eb92 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65  /.  int *pnoCase
2eb93 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2eb94 20 75 70 70 65 72 63 61 73 65 20 69 73 20 65 71   uppercase is eq
2eb95 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65  uivalent to lowe
2eb96 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f  rcase */.){.  co
2eb97 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b  nst char *z = 0;
2eb98 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
2eb99 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c 49 4b  ng on RHS of LIK
2eb9a 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  E operator */.  
2eb9b 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
2eb9c 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20 52 69  Left;      /* Ri
2eb9d 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69 7a  ght and left siz
2eb9e 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72 61 74  e of LIKE operat
2eb9f 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  or */.  ExprList
2eba0 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20   *pList;        
2eba1 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 70     /* List of op
2eba2 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4c 49  erands to the LI
2eba3 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  KE operator */. 
2eba4 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20 20 20   int c;         
2eba5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2eba6 6e 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ne character in 
2eba7 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  z[] */.  int cnt
2eba8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2eba9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2ebaa 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70 72   non-wildcard pr
2ebab 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73 20  efix characters 
2ebac 2a 2f 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b  */.  char wc[3];
2ebad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebae 2f 2a 20 57 69 6c 64 63 61 72 64 20 63 68 61 72  /* Wildcard char
2ebaf 61 63 74 65 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c  acters */.  Coll
2ebb0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
2ebb1 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
2ebb2 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
2ebb3 20 4c 48 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65   LHS */.  sqlite
2ebb4 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ebb5 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65  db;  /* Database
2ebb6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2ebb7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2ebb8 70 56 61 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  pVal = 0;.  int 
2ebb9 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2ebba 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65         /* Opcode
2ebbb 20 6f 66 20 70 52 69 67 68 74 20 2a 2f 0a 0a 20   of pRight */.. 
2ebbc 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 4c   if( !sqlite3IsL
2ebbd 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ikeFunction(db, 
2ebbe 70 45 78 70 72 2c 20 70 6e 6f 43 61 73 65 2c 20  pExpr, pnoCase, 
2ebbf 77 63 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  wc) ){.    retur
2ebc0 6e 20 30 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20  n 0;.  }.#ifdef 
2ebc1 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20  SQLITE_EBCDIC.  
2ebc2 69 66 28 20 2a 70 6e 6f 43 61 73 65 20 29 20 72  if( *pnoCase ) r
2ebc3 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
2ebc4 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d    pList = pExpr-
2ebc5 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 70 4c 65 66  >x.pList;.  pLef
2ebc6 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  t = pList->a[1].
2ebc7 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 4c 65  pExpr;.  if( pLe
2ebc8 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  ft->op!=TK_COLUM
2ebc9 4e 20 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72  N || sqlite3Expr
2ebca 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 21  Affinity(pLeft)!
2ebcb 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
2ebcc 20 29 7b 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20   ){.    /* IMP: 
2ebcd 52 2d 30 32 30 36 35 2d 34 39 34 36 35 20 54 68  R-02065-49465 Th
2ebce 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
2ebcf 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20   of the LIKE or 
2ebd0 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 6d 75  GLOB operator mu
2ebd1 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 68 65  st.    ** be the
2ebd2 20 6e 61 6d 65 20 6f 66 20 61 6e 20 69 6e 64 65   name of an inde
2ebd3 78 65 64 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  xed column with 
2ebd4 54 45 58 54 20 61 66 66 69 6e 69 74 79 2e 20 2a  TEXT affinity. *
2ebd5 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
2ebd6 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c    }.  assert( pL
2ebd7 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 28 2d  eft->iColumn!=(-
2ebd8 31 29 20 29 3b 20 2f 2a 20 42 65 63 61 75 73 65  1) ); /* Because
2ebd9 20 49 50 4b 20 6e 65 76 65 72 20 68 61 73 20 41   IPK never has A
2ebda 46 46 5f 54 45 58 54 20 2a 2f 0a 20 20 70 43 6f  FF_TEXT */.  pCo
2ebdb 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
2ebdc 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2ebdd 70 4c 65 66 74 29 3b 0a 20 20 61 73 73 65 72 74  pLeft);.  assert
2ebde 28 20 70 43 6f 6c 6c 21 3d 30 20 29 3b 20 20 2f  ( pColl!=0 );  /
2ebdf 2a 20 45 76 65 72 79 20 6e 6f 6e 2d 49 50 4b 20  * Every non-IPK 
2ebe0 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 63 6f 6c  column has a col
2ebe1 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2ebe2 2a 2f 0a 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d  */.  if( (pColl-
2ebe3 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 43 4f  >type!=SQLITE_CO
2ebe4 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c 20 2a 70 6e  LL_BINARY || *pn
2ebe5 6f 43 61 73 65 29 20 26 26 0a 20 20 20 20 20 20  oCase) &&.      
2ebe6 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51  (pColl->type!=SQ
2ebe7 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45  LITE_COLL_NOCASE
2ebe8 20 7c 7c 20 21 2a 70 6e 6f 43 61 73 65 29 20 29   || !*pnoCase) )
2ebe9 7b 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d  {.    /* IMP: R-
2ebea 30 39 30 30 33 2d 33 32 30 34 36 20 46 6f 72 20  09003-32046 For 
2ebeb 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f  the GLOB operato
2ebec 72 2c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6d 75  r, the column mu
2ebed 73 74 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a  st use the.    *
2ebee 2a 20 64 65 66 61 75 6c 74 20 42 49 4e 41 52 59  * default BINARY
2ebef 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2ebf0 6e 63 65 2e 0a 20 20 20 20 2a 2a 20 49 4d 50 3a  nce..    ** IMP:
2ebf1 20 52 2d 34 31 34 30 38 2d 32 38 33 30 36 20 46   R-41408-28306 F
2ebf2 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  or the LIKE oper
2ebf3 61 74 6f 72 2c 20 69 66 20 63 61 73 65 5f 73 65  ator, if case_se
2ebf4 6e 73 69 74 69 76 65 5f 6c 69 6b 65 20 6d 6f 64  nsitive_like mod
2ebf5 65 0a 20 20 20 20 2a 2a 20 69 73 20 65 6e 61 62  e.    ** is enab
2ebf6 6c 65 64 20 74 68 65 6e 20 74 68 65 20 63 6f 6c  led then the col
2ebf7 75 6d 6e 20 6d 75 73 74 20 75 73 65 20 74 68 65  umn must use the
2ebf8 20 64 65 66 61 75 6c 74 20 42 49 4e 41 52 59 20   default BINARY 
2ebf9 63 6f 6c 6c 61 74 69 6e 67 0a 20 20 20 20 2a 2a  collating.    **
2ebfa 20 73 65 71 75 65 6e 63 65 2c 20 6f 72 20 69 66   sequence, or if
2ebfb 20 63 61 73 65 5f 73 65 6e 73 69 74 69 76 65 5f   case_sensitive_
2ebfc 6c 69 6b 65 20 6d 6f 64 65 20 69 73 20 64 69 73  like mode is dis
2ebfd 61 62 6c 65 64 20 74 68 65 6e 20 74 68 65 20 63  abled then the c
2ebfe 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 6d 75 73  olumn.    ** mus
2ebff 74 20 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d  t use the built-
2ec00 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74  in NOCASE collat
2ec01 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 20 20  ing sequence..  
2ec02 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20    */.    return 
2ec03 30 3b 0a 20 20 7d 0a 0a 20 20 70 52 69 67 68 74  0;.  }..  pRight
2ec04 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70   = pList->a[0].p
2ec05 45 78 70 72 3b 0a 20 20 6f 70 20 3d 20 70 52 69  Expr;.  op = pRi
2ec06 67 68 74 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f  ght->op;.  if( o
2ec07 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29  p==TK_REGISTER )
2ec08 7b 0a 20 20 20 20 6f 70 20 3d 20 70 52 69 67 68  {.    op = pRigh
2ec09 74 2d 3e 6f 70 32 3b 0a 20 20 7d 0a 20 20 69 66  t->op2;.  }.  if
2ec0a 28 20 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c  ( op==TK_VARIABL
2ec0b 45 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 70  E ){.    Vdbe *p
2ec0c 52 65 70 72 65 70 61 72 65 20 3d 20 70 50 61 72  Reprepare = pPar
2ec0d 73 65 2d 3e 70 52 65 70 72 65 70 61 72 65 3b 0a  se->pReprepare;.
2ec0e 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74      pVal = sqlit
2ec0f 65 33 56 64 62 65 47 65 74 56 61 6c 75 65 28 70  e3VdbeGetValue(p
2ec10 52 65 70 72 65 70 61 72 65 2c 20 70 52 69 67 68  Reprepare, pRigh
2ec11 74 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 53 51 4c 49  t->iColumn, SQLI
2ec12 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 20 20  TE_AFF_NONE);.  
2ec13 20 20 69 66 28 20 70 56 61 6c 20 26 26 20 73 71    if( pVal && sq
2ec14 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2ec15 28 70 56 61 6c 29 3d 3d 53 51 4c 49 54 45 5f 54  (pVal)==SQLITE_T
2ec16 45 58 54 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d  EXT ){.      z =
2ec17 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
2ec18 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c  _value_text(pVal
2ec19 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2ec1a 69 74 65 33 56 64 62 65 53 65 74 56 61 72 6d 61  ite3VdbeSetVarma
2ec1b 73 6b 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  sk(pParse->pVdbe
2ec1c 2c 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d  , pRight->iColum
2ec1d 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  n);.    assert( 
2ec1e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 56  pRight->op==TK_V
2ec1f 41 52 49 41 42 4c 45 20 7c 7c 20 70 52 69 67 68  ARIABLE || pRigh
2ec20 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  t->op==TK_REGIST
2ec21 45 52 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ER );.  }else if
2ec22 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20  ( op==TK_STRING 
2ec23 29 7b 0a 20 20 20 20 7a 20 3d 20 70 52 69 67 68  ){.    z = pRigh
2ec24 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 7d  t->u.zToken;.  }
2ec25 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20  .  if( z ){.    
2ec26 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69  cnt = 0;.    whi
2ec27 6c 65 28 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d  le( (c=z[cnt])!=
2ec28 30 20 26 26 20 63 21 3d 77 63 5b 30 5d 20 26 26  0 && c!=wc[0] &&
2ec29 20 63 21 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d   c!=wc[1] && c!=
2ec2a 77 63 5b 32 5d 20 29 7b 0a 20 20 20 20 20 20 63  wc[2] ){.      c
2ec2b 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nt++;.    }.    
2ec2c 69 66 28 20 63 6e 74 21 3d 30 20 26 26 20 63 21  if( cnt!=0 && c!
2ec2d 3d 30 20 26 26 20 32 35 35 21 3d 28 75 38 29 7a  =0 && 255!=(u8)z
2ec2e 5b 63 6e 74 2d 31 5d 20 29 7b 0a 20 20 20 20 20  [cnt-1] ){.     
2ec2f 20 45 78 70 72 20 2a 70 50 72 65 66 69 78 3b 0a   Expr *pPrefix;.
2ec30 20 20 20 20 20 20 2a 70 69 73 43 6f 6d 70 6c 65        *pisComple
2ec31 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63 5b  te = z[cnt]==wc[
2ec32 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d  0] && z[cnt+1]==
2ec33 30 3b 0a 20 20 20 20 20 20 70 50 72 65 66 69 78  0;.      pPrefix
2ec34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2ec35 62 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20 7a 29  b, TK_STRING, z)
2ec36 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65  ;.      if( pPre
2ec37 66 69 78 20 29 20 70 50 72 65 66 69 78 2d 3e 75  fix ) pPrefix->u
2ec38 2e 7a 54 6f 6b 65 6e 5b 63 6e 74 5d 20 3d 20 30  .zToken[cnt] = 0
2ec39 3b 0a 20 20 20 20 20 20 2a 70 70 50 72 65 66 69  ;.      *ppPrefi
2ec3a 78 20 3d 20 70 50 72 65 66 69 78 3b 0a 20 20 20  x = pPrefix;.   
2ec3b 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 56 41     if( op==TK_VA
2ec3c 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 20 20  RIABLE ){.      
2ec3d 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2ec3e 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 20  se->pVdbe;.     
2ec3f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2ec40 74 56 61 72 6d 61 73 6b 28 76 2c 20 70 52 69 67  tVarmask(v, pRig
2ec41 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ht->iColumn);.  
2ec42 20 20 20 20 20 20 69 66 28 20 2a 70 69 73 43 6f        if( *pisCo
2ec43 6d 70 6c 65 74 65 20 26 26 20 70 52 69 67 68 74  mplete && pRight
2ec44 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 20 29 7b  ->u.zToken[1] ){
2ec45 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  .          /* If
2ec46 20 74 68 65 20 72 68 73 20 6f 66 20 74 68 65 20   the rhs of the 
2ec47 4c 49 4b 45 20 65 78 70 72 65 73 73 69 6f 6e 20  LIKE expression 
2ec48 69 73 20 61 20 76 61 72 69 61 62 6c 65 2c 20 61  is a variable, a
2ec49 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  nd the current. 
2ec4a 20 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75           ** valu
2ec4b 65 20 6f 66 20 74 68 65 20 76 61 72 69 61 62 6c  e of the variabl
2ec4c 65 20 6d 65 61 6e 73 20 74 68 65 72 65 20 69 73  e means there is
2ec4d 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f   no need to invo
2ec4e 6b 65 20 74 68 65 20 4c 49 4b 45 0a 20 20 20 20  ke the LIKE.    
2ec4f 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
2ec50 6e 2c 20 74 68 65 6e 20 6e 6f 20 4f 50 5f 56 61  n, then no OP_Va
2ec51 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 61  riable will be a
2ec52 64 64 65 64 20 74 6f 20 74 68 65 20 70 72 6f 67  dded to the prog
2ec53 72 61 6d 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ram..          *
2ec54 2a 20 54 68 69 73 20 63 61 75 73 65 73 20 70 72  * This causes pr
2ec55 6f 62 6c 65 6d 73 20 66 6f 72 20 74 68 65 20 73  oblems for the s
2ec56 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
2ec57 6d 65 74 65 72 5f 6e 61 6d 65 28 29 0a 20 20 20  meter_name().   
2ec58 20 20 20 20 20 20 20 2a 2a 20 41 50 49 2e 20 54         ** API. T
2ec59 6f 20 77 6f 72 6b 61 72 6f 75 6e 64 20 74 68 65  o workaround the
2ec5a 6d 2c 20 61 64 64 20 61 20 64 75 6d 6d 79 20 4f  m, add a dummy O
2ec5b 50 5f 56 61 72 69 61 62 6c 65 20 68 65 72 65 2e  P_Variable here.
2ec5c 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 20 0a 20  .          */ . 
2ec5d 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20           int r1 
2ec5e 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2ec5f 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2ec60 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2ec61 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2ec62 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72 31 29  rse, pRight, r1)
2ec63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2ec64 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
2ec65 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  v, sqlite3VdbeCu
2ec66 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31 2c 20  rrentAddr(v)-1, 
2ec67 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
2ec68 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2ec69 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2ec6a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ec6b 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
2ec6c 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 7d      z = 0;.    }
2ec6d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56  .  }..  sqlite3V
2ec6e 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a  alueFree(pVal);.
2ec6f 20 20 72 65 74 75 72 6e 20 28 7a 21 3d 30 29 3b    return (z!=0);
2ec70 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2ec71 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  ITE_OMIT_LIKE_OP
2ec72 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a  TIMIZATION */...
2ec73 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2ec74 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2ec75 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
2ec76 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e  see if the given
2ec77 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2ec78 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
2ec79 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20           column 
2ec7a 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a  MATCH expr.**.**
2ec7b 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20 72   If it is then r
2ec7c 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
2ec7d 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41 4c 53  not, return FALS
2ec7e 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
2ec7f 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e   isMatchOfColumn
2ec80 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  (.  Expr *pExpr 
2ec81 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
2ec82 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
2ec83 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  ){.  ExprList *p
2ec84 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 78  List;..  if( pEx
2ec85 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54  pr->op!=TK_FUNCT
2ec86 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ION ){.    retur
2ec87 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  n 0;.  }.  if( s
2ec88 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45  qlite3StrICmp(pE
2ec89 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d  xpr->u.zToken,"m
2ec8a 61 74 63 68 22 29 21 3d 30 20 29 7b 0a 20 20 20  atch")!=0 ){.   
2ec8b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2ec8c 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e   pList = pExpr->
2ec8d 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  x.pList;.  if( p
2ec8e 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29  List->nExpr!=2 )
2ec8f 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2ec90 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
2ec91 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 20  >a[1].pExpr->op 
2ec92 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  != TK_COLUMN ){.
2ec93 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2ec94 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
2ec95 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2ec96 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2ec97 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
2ec98 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65 73  the pBase expres
2ec99 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64 20  sion originated 
2ec9a 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
2ec9b 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20  NG clause of.** 
2ec9c 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72 61  a join, then tra
2ec9d 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f 70  nsfer the approp
2ec9e 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20 6f  riate markings o
2ec9f 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e 0a  ver to derived..
2eca0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
2eca1 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
2eca2 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69 76  ngs(Expr *pDeriv
2eca3 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65 29  ed, Expr *pBase)
2eca4 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66 6c  {.  pDerived->fl
2eca5 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c  ags |= pBase->fl
2eca6 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69  ags & EP_FromJoi
2eca7 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 69  n;.  pDerived->i
2eca8 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
2eca9 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f   pBase->iRightJo
2ecaa 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20  inTable;.}..#if 
2ecab 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2ecac 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ecad 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65  TION) && !define
2ecae 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2ecaf 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 41 6e  BQUERY)./*.** An
2ecb0 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61  alyze a term tha
2ecb1 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  t consists of tw
2ecb2 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e  o or more OR-con
2ecb3 6e 65 63 74 65 64 0a 2a 2a 20 73 75 62 74 65 72  nected.** subter
2ecb4 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a 2a 2a 0a 2a  ms.  So in:.**.*
2ecb5 2a 20 20 20 20 20 2e 2e 2e 20 57 48 45 52 45 20  *     ... WHERE 
2ecb6 20 28 61 3d 35 29 20 41 4e 44 20 28 62 3d 37 20   (a=5) AND (b=7 
2ecb7 4f 52 20 63 3d 39 20 4f 52 20 64 3d 31 33 29 20  OR c=9 OR d=13) 
2ecb8 41 4e 44 20 28 64 3d 31 33 29 0a 2a 2a 20 20 20  AND (d=13).**   
2ecb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ecba 20 20 20 20 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e 5e         ^^^^^^^^^
2ecbb 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a  ^^^^^^^^^^^.**.*
2ecbc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2ecbd 6e 61 6c 79 7a 65 73 20 74 65 72 6d 73 20 73 75  nalyzes terms su
2ecbe 63 68 20 61 73 20 74 68 65 20 6d 69 64 64 6c 65  ch as the middle
2ecbf 20 74 65 72 6d 20 69 6e 20 74 68 65 20 61 62 6f   term in the abo
2ecc0 76 65 20 65 78 61 6d 70 6c 65 2e 0a 2a 2a 20 41  ve example..** A
2ecc1 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a   WhereOrTerm obj
2ecc2 65 63 74 20 69 73 20 63 6f 6d 70 75 74 65 64 20  ect is computed 
2ecc3 61 6e 64 20 61 74 74 61 63 68 65 64 20 74 6f 20  and attached to 
2ecc4 74 68 65 20 74 65 72 6d 20 75 6e 64 65 72 0a 2a  the term under.*
2ecc5 2a 20 61 6e 61 6c 79 73 69 73 2c 20 72 65 67 61  * analysis, rega
2ecc6 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75  rdless of the ou
2ecc7 74 63 6f 6d 65 20 6f 66 20 74 68 65 20 61 6e 61  tcome of the ana
2ecc8 6c 79 73 69 73 2e 20 20 48 65 6e 63 65 3a 0a 2a  lysis.  Hence:.*
2ecc9 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65  *.**     WhereTe
2ecca 72 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c 3d 20  rm.wtFlags   |= 
2eccb 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 0a 2a 2a 20   TERM_ORINFO.** 
2eccc 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
2eccd 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20 64 79  pOrInfo  =  a dy
2ecce 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2eccf 74 65 64 20 57 68 65 72 65 4f 72 54 65 72 6d 20  ted WhereOrTerm 
2ecd0 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65  object.**.** The
2ecd1 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e 61 6c   term being anal
2ecd2 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65 20 74  yzed must have t
2ecd3 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20 4f 52  wo or more of OR
2ecd4 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 74 65  -connected subte
2ecd5 72 6d 73 2e 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  rms..** A single
2ecd6 20 73 75 62 74 65 72 6d 20 6d 69 67 68 74 20 62   subterm might b
2ecd7 65 20 61 20 73 65 74 20 6f 66 20 41 4e 44 2d 63  e a set of AND-c
2ecd8 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d 73 75 62  onnected sub-sub
2ecd9 74 65 72 6d 73 2e 0a 2a 2a 20 45 78 61 6d 70 6c  terms..** Exampl
2ecda 65 73 20 6f 66 20 74 65 72 6d 73 20 75 6e 64 65  es of terms unde
2ecdb 72 20 61 6e 61 6c 79 73 69 73 3a 0a 2a 2a 0a 2a  r analysis:.**.*
2ecdc 2a 20 20 20 20 20 28 41 29 20 20 20 20 20 74 31  *     (A)     t1
2ecdd 2e 78 3d 74 32 2e 79 20 4f 52 20 74 31 2e 78 3d  .x=t2.y OR t1.x=
2ecde 74 32 2e 7a 20 4f 52 20 74 31 2e 79 3d 31 35 20  t2.z OR t1.y=15 
2ecdf 4f 52 20 74 31 2e 7a 3d 74 33 2e 61 2b 35 0a 2a  OR t1.z=t3.a+5.*
2ece0 2a 20 20 20 20 20 28 42 29 20 20 20 20 20 78 3d  *     (B)     x=
2ece1 65 78 70 72 31 20 4f 52 20 65 78 70 72 32 3d 78  expr1 OR expr2=x
2ece2 20 4f 52 20 78 3d 65 78 70 72 33 0a 2a 2a 20 20   OR x=expr3.**  
2ece3 20 20 20 28 43 29 20 20 20 20 20 74 31 2e 78 3d     (C)     t1.x=
2ece4 74 32 2e 79 20 4f 52 20 28 74 31 2e 78 3d 74 32  t2.y OR (t1.x=t2
2ece5 2e 7a 20 41 4e 44 20 74 31 2e 79 3d 31 35 29 0a  .z AND t1.y=15).
2ece6 2a 2a 20 20 20 20 20 28 44 29 20 20 20 20 20 78  **     (D)     x
2ece7 3d 65 78 70 72 31 20 4f 52 20 28 79 3e 31 31 20  =expr1 OR (y>11 
2ece8 41 4e 44 20 79 3c 32 32 20 41 4e 44 20 7a 20 4c  AND y<22 AND z L
2ece9 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a 27 29 0a 2a  IKE '*hello*').*
2ecea 2a 20 20 20 20 20 28 45 29 20 20 20 20 20 28 70  *     (E)     (p
2eceb 2e 61 3d 31 20 41 4e 44 20 71 2e 62 3d 32 20 41  .a=1 AND q.b=2 A
2ecec 4e 44 20 72 2e 63 3d 33 29 20 4f 52 20 28 70 2e  ND r.c=3) OR (p.
2eced 78 3d 34 20 41 4e 44 20 71 2e 79 3d 35 20 41 4e  x=4 AND q.y=5 AN
2ecee 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a 20 43  D r.z=6).**.** C
2ecef 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  ASE 1:.**.** If 
2ecf0 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65  all subterms are
2ecf1 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 54 2e 43   of the form T.C
2ecf2 3d 65 78 70 72 20 66 6f 72 20 73 6f 6d 65 20 73  =expr for some s
2ecf3 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  ingle column of 
2ecf4 43 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61  C.** a single ta
2ecf5 62 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e 20  ble T (as shown 
2ecf6 69 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f  in example B abo
2ecf7 76 65 29 20 74 68 65 6e 20 63 72 65 61 74 65 20  ve) then create 
2ecf8 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a  a new virtual.**
2ecf9 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 61 6e   term that is an
2ecfa 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20 65   equivalent IN e
2ecfb 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f  xpression.  In o
2ecfc 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74  ther words, if t
2ecfd 68 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69 6e 67  he term.** being
2ecfe 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a 2a 2a   analyzed is:.**
2ecff 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20 65 78 70  .**      x = exp
2ed00 72 31 20 20 4f 52 20 20 65 78 70 72 32 20 3d 20  r1  OR  expr2 = 
2ed01 78 20 20 4f 52 20 20 78 20 3d 20 65 78 70 72 33  x  OR  x = expr3
2ed02 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72 65 61  .**.** then crea
2ed03 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
2ed04 20 74 65 72 6d 20 6c 69 6b 65 20 74 68 69 73 3a   term like this:
2ed05 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e  .**.**      x IN
2ed06 20 28 65 78 70 72 31 2c 65 78 70 72 32 2c 65 78   (expr1,expr2,ex
2ed07 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20  pr3).**.** CASE 
2ed08 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20  2:.**.** If all 
2ed09 73 75 62 74 65 72 6d 73 20 61 72 65 20 69 6e 64  subterms are ind
2ed0a 65 78 61 62 6c 65 20 62 79 20 61 20 73 69 6e 67  exable by a sing
2ed0b 6c 65 20 74 61 62 6c 65 20 54 2c 20 74 68 65 6e  le table T, then
2ed0c 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57   set.**.**     W
2ed0d 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74  hereTerm.eOperat
2ed0e 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  or              
2ed0f 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20 20  =  WO_OR.**     
2ed10 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49  WhereTerm.u.pOrI
2ed11 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20  nfo->indexable  
2ed12 7c 3d 20 20 74 68 65 20 63 75 72 73 6f 72 20 6e  |=  the cursor n
2ed13 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
2ed14 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74 65 72  T.**.** A subter
2ed15 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65 22  m is "indexable"
2ed16 20 69 66 20 69 74 20 69 73 20 6f 66 20 74 68 65   if it is of the
2ed17 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c 6f   form.** "T.C <o
2ed18 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72 65  p> <expr>" where
2ed19 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d 6e   C is any column
2ed1a 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64 20   of table T and 
2ed1b 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20  .** <op> is one 
2ed1c 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3c 3d  of "=", "<", "<=
2ed1d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20 22 49  ", ">", ">=", "I
2ed1e 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49 4e 22  S NULL", or "IN"
2ed1f 2e 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20 69  ..** A subterm i
2ed20 73 20 61 6c 73 6f 20 69 6e 64 65 78 61 62 6c 65  s also indexable
2ed21 20 69 66 20 69 74 20 69 73 20 61 6e 20 41 4e 44   if it is an AND
2ed22 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a   of two or more.
2ed23 2a 2a 20 73 75 62 73 75 62 74 65 72 6d 73 20 61  ** subsubterms a
2ed24 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 77  t least one of w
2ed25 68 69 63 68 20 69 73 20 69 6e 64 65 78 61 62 6c  hich is indexabl
2ed26 65 2e 20 20 49 6e 64 65 78 61 62 6c 65 20 41 4e  e.  Indexable AN
2ed27 44 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73 20 68  D .** subterms h
2ed28 61 76 65 20 74 68 65 69 72 20 65 4f 70 65 72 61  ave their eOpera
2ed29 74 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f 41 4e  tor set to WO_AN
2ed2a 44 20 61 6e 64 20 74 68 65 79 20 68 61 76 65 0a  D and they have.
2ed2b 2a 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20 73 65  ** u.pAndInfo se
2ed2c 74 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c  t to a dynamical
2ed2d 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65  ly allocated Whe
2ed2e 72 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65 63 74  reAndTerm object
2ed2f 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e 6f  ..**.** From ano
2ed30 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76 69  ther point of vi
2ed31 65 77 2c 20 22 69 6e 64 65 78 61 62 6c 65 22 20  ew, "indexable" 
2ed32 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73  means that the s
2ed33 75 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a 20  ubterm could.** 
2ed34 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 75  potentially be u
2ed35 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65  sed with an inde
2ed36 78 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69  x if an appropri
2ed37 61 74 65 20 69 6e 64 65 78 20 65 78 69 73 74 73  ate index exists
2ed38 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c 79 73  ..** This analys
2ed39 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 73  is does not cons
2ed3a 69 64 65 72 20 77 68 65 74 68 65 72 20 6f 72 20  ider whether or 
2ed3b 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 20 65 78  not the index ex
2ed3c 69 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20 69 73  ists; that.** is
2ed3d 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 65 20 62   something the b
2ed3e 65 73 74 49 6e 64 65 78 28 29 20 72 6f 75 74 69  estIndex() routi
2ed3f 6e 65 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e  ne will determin
2ed40 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79 73 69  e.  This analysi
2ed41 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20  s.** only looks 
2ed42 61 74 20 77 68 65 74 68 65 72 20 73 75 62 74 65  at whether subte
2ed43 72 6d 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  rms appropriate 
2ed44 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65 78 69  for indexing exi
2ed45 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 78  st..**.** All ex
2ed46 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75 67 68  amples A through
2ed47 20 45 20 61 62 6f 76 65 20 61 6c 6c 20 73 61 74   E above all sat
2ed48 69 73 66 79 20 63 61 73 65 20 32 2e 20 20 42 75  isfy case 2.  Bu
2ed49 74 20 69 66 20 61 20 74 65 72 6d 0a 2a 2a 20 61  t if a term.** a
2ed4a 6c 73 6f 20 73 74 61 74 69 73 66 69 65 73 20 63  lso statisfies c
2ed4b 61 73 65 20 31 20 28 73 75 63 68 20 61 73 20 42  ase 1 (such as B
2ed4c 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
2ed4d 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 77 69 6c  he optimizer wil
2ed4e 6c 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72 65 66  l.** always pref
2ed4f 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20 69 6e  er case 1, so in
2ed50 20 74 68 61 74 20 63 61 73 65 20 77 65 20 70 72   that case we pr
2ed51 65 74 65 6e 64 20 74 68 61 74 20 63 61 73 65 20  etend that case 
2ed52 32 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 61 74 69  2 is not.** sati
2ed53 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  sfied..**.** It 
2ed54 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73  might be the cas
2ed55 65 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20  e that multiple 
2ed56 74 61 62 6c 65 73 20 61 72 65 20 69 6e 64 65 78  tables are index
2ed57 61 62 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  able.  For examp
2ed58 6c 65 2c 0a 2a 2a 20 28 45 29 20 61 62 6f 76 65  le,.** (E) above
2ed59 20 69 73 20 69 6e 64 65 78 61 62 6c 65 20 6f 6e   is indexable on
2ed5a 20 74 61 62 6c 65 73 20 50 2c 20 51 2c 20 61 6e   tables P, Q, an
2ed5b 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73  d R..**.** Terms
2ed5c 20 74 68 61 74 20 73 61 74 69 73 66 79 20 63 61   that satisfy ca
2ed5d 73 65 20 32 20 61 72 65 20 63 61 6e 64 69 64 61  se 2 are candida
2ed5e 74 65 73 20 66 6f 72 20 6c 6f 6f 6b 75 70 20 62  tes for lookup b
2ed5f 79 20 75 73 69 6e 67 0a 2a 2a 20 73 65 70 61 72  y using.** separ
2ed60 61 74 65 20 69 6e 64 69 63 65 73 20 74 6f 20 66  ate indices to f
2ed61 69 6e 64 20 72 6f 77 69 64 73 20 66 6f 72 20 65  ind rowids for e
2ed62 61 63 68 20 73 75 62 74 65 72 6d 20 61 6e 64 20  ach subterm and 
2ed63 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a 20 74 68 65  composing.** the
2ed64 20 75 6e 69 6f 6e 20 6f 66 20 61 6c 6c 20 72 6f   union of all ro
2ed65 77 69 64 73 20 75 73 69 6e 67 20 61 20 52 6f 77  wids using a Row
2ed66 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 54 68 69  Set object.  Thi
2ed67 73 20 69 73 20 73 69 6d 69 6c 61 72 0a 2a 2a 20  s is similar.** 
2ed68 74 6f 20 22 62 69 74 6d 61 70 20 69 6e 64 69 63  to "bitmap indic
2ed69 65 73 22 20 69 6e 20 6f 74 68 65 72 20 64 61 74  es" in other dat
2ed6a 61 62 61 73 65 20 65 6e 67 69 6e 65 73 2e 0a 2a  abase engines..*
2ed6b 2a 0a 2a 2a 20 4f 54 48 45 52 57 49 53 45 3a 0a  *.** OTHERWISE:.
2ed6c 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72  **.** If neither
2ed6d 20 63 61 73 65 20 31 20 6e 6f 72 20 63 61 73 65   case 1 nor case
2ed6e 20 32 20 61 70 70 6c 79 2c 20 74 68 65 6e 20 6c   2 apply, then l
2ed6f 65 61 76 65 20 74 68 65 20 65 4f 70 65 72 61 74  eave the eOperat
2ed70 6f 72 20 73 65 74 20 74 6f 0a 2a 2a 20 7a 65 72  or set to.** zer
2ed71 6f 2e 20 20 54 68 69 73 20 74 65 72 6d 20 69 73  o.  This term is
2ed72 20 6e 6f 74 20 75 73 65 66 75 6c 20 66 6f 72 20   not useful for 
2ed73 73 65 61 72 63 68 2e 0a 2a 2f 0a 73 74 61 74 69  search..*/.stati
2ed74 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2ed75 7a 65 4f 72 54 65 72 6d 28 0a 20 20 53 72 63 4c  zeOrTerm(.  SrcL
2ed76 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2ed77 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
2ed78 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  M clause */.  Wh
2ed79 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2ed7a 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 63          /* the c
2ed7b 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20 63 6c  omplete WHERE cl
2ed7c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ause */.  int id
2ed7d 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20  xTerm           
2ed7e 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2ed7f 74 68 65 20 4f 52 2d 74 65 72 6d 20 74 6f 20 62  the OR-term to b
2ed80 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b  e analyzed */.){
2ed81 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2ed82 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 20   = pWC->pParse; 
2ed83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2ed84 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2ed85 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2ed86 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2ed87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2ed88 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2ed89 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2ed8a 2a 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61  *pTerm = &pWC->a
2ed8b 5b 69 64 78 54 65 72 6d 5d 3b 20 20 20 20 2f 2a  [idxTerm];    /*
2ed8c 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2ed8d 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 45 78  analyzed */.  Ex
2ed8e 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72  pr *pExpr = pTer
2ed8f 6d 2d 3e 70 45 78 70 72 3b 20 20 20 20 20 20 20  m->pExpr;       
2ed90 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2ed91 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  ression of the t
2ed92 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  erm */.  WhereMa
2ed93 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 20  skSet *pMaskSet 
2ed94 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b  = pWC->pMaskSet;
2ed95 20 2f 2a 20 54 61 62 6c 65 20 75 73 65 20 6d 61   /* Table use ma
2ed96 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  sks */.  int i; 
2ed97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed99 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2ed9a 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  s */.  WhereClau
2ed9b 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 20 20  se *pOrWc;      
2ed9c 20 2f 2a 20 42 72 65 61 6b 75 70 20 6f 66 20 70   /* Breakup of p
2ed9d 54 65 72 6d 20 69 6e 74 6f 20 73 75 62 74 65 72  Term into subter
2ed9e 6d 73 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  ms */.  WhereTer
2ed9f 6d 20 2a 70 4f 72 54 65 72 6d 3b 20 20 20 20 20  m *pOrTerm;     
2eda0 20 20 2f 2a 20 41 20 53 75 62 2d 74 65 72 6d 20    /* A Sub-term 
2eda1 77 69 74 68 69 6e 20 74 68 65 20 70 4f 72 57 63  within the pOrWc
2eda2 20 2a 2f 0a 20 20 57 68 65 72 65 4f 72 49 6e 66   */.  WhereOrInf
2eda3 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 20 20  o *pOrInfo;     
2eda4 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e  /* Additional in
2eda5 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63 69  formation associ
2eda6 61 74 65 64 20 77 69 74 68 20 70 54 65 72 6d 20  ated with pTerm 
2eda7 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 63 68 6e  */.  Bitmask chn
2eda8 67 54 6f 49 4e 3b 20 20 20 20 20 20 20 20 20 2f  gToIN;         /
2eda9 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20 6d 69  * Tables that mi
2edaa 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73 65  ght satisfy case
2edab 20 31 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20   1 */.  Bitmask 
2edac 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20  indexable;      
2edad 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74    /* Tables that
2edae 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2c 20   are indexable, 
2edaf 73 61 74 69 73 66 79 69 6e 67 20 63 61 73 65 20  satisfying case 
2edb0 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 */..  /*.  ** 
2edb1 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63 6c 61  Break the OR cla
2edb2 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73 65 70  use into its sep
2edb3 61 72 61 74 65 20 73 75 62 74 65 72 6d 73 2e 20  arate subterms. 
2edb4 20 54 68 65 20 73 75 62 74 65 72 6d 73 20 61 72   The subterms ar
2edb5 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  e.  ** stored in
2edb6 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
2edb7 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
2edb8 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 57  ing within the W
2edb9 68 65 72 65 4f 72 49 6e 66 6f 0a 20 20 2a 2a 20  hereOrInfo.  ** 
2edba 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73 20 61  object that is a
2edbb 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6f  ttached to the o
2edbc 72 69 67 69 6e 61 6c 20 4f 52 20 63 6c 61 75 73  riginal OR claus
2edbd 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 61  e term..  */.  a
2edbe 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77  ssert( (pTerm->w
2edbf 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 44  tFlags & (TERM_D
2edc0 59 4e 41 4d 49 43 7c 54 45 52 4d 5f 4f 52 49 4e  YNAMIC|TERM_ORIN
2edc1 46 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 29  FO|TERM_ANDINFO)
2edc2 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
2edc3 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2edc4 4f 52 20 29 3b 0a 20 20 70 54 65 72 6d 2d 3e 75  OR );.  pTerm->u
2edc5 2e 70 4f 72 49 6e 66 6f 20 3d 20 70 4f 72 49 6e  .pOrInfo = pOrIn
2edc6 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2edc7 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2edc8 65 6f 66 28 2a 70 4f 72 49 6e 66 6f 29 29 3b 0a  eof(*pOrInfo));.
2edc9 20 20 69 66 28 20 70 4f 72 49 6e 66 6f 3d 3d 30    if( pOrInfo==0
2edca 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 65   ) return;.  pTe
2edcb 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2edcc 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a 20 20 70 4f  ERM_ORINFO;.  pO
2edcd 72 57 63 20 3d 20 26 70 4f 72 49 6e 66 6f 2d 3e  rWc = &pOrInfo->
2edce 77 63 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  wc;.  whereClaus
2edcf 65 49 6e 69 74 28 70 4f 72 57 63 2c 20 70 57 43  eInit(pOrWc, pWC
2edd0 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53  ->pParse, pMaskS
2edd1 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69  et);.  whereSpli
2edd2 74 28 70 4f 72 57 63 2c 20 70 45 78 70 72 2c 20  t(pOrWc, pExpr, 
2edd3 54 4b 5f 4f 52 29 3b 0a 20 20 65 78 70 72 41 6e  TK_OR);.  exprAn
2edd4 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70  alyzeAll(pSrc, p
2edd5 4f 72 57 63 29 3b 0a 20 20 69 66 28 20 64 62 2d  OrWc);.  if( db-
2edd6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2edd7 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2edd8 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e 3d  ( pOrWc->nTerm>=
2edd9 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 );..  /*.  ** 
2edda 43 6f 6d 70 75 74 65 20 74 68 65 20 73 65 74 20  Compute the set 
2eddb 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 6d  of tables that m
2eddc 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73  ight satisfy cas
2eddd 65 73 20 31 20 6f 72 20 32 2e 0a 20 20 2a 2f 0a  es 1 or 2..  */.
2edde 20 20 69 6e 64 65 78 61 62 6c 65 20 3d 20 7e 28    indexable = ~(
2eddf 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 63 68 6e  Bitmask)0;.  chn
2ede0 67 54 6f 49 4e 20 3d 20 7e 28 70 57 43 2d 3e 76  gToIN = ~(pWC->v
2ede1 6d 61 73 6b 29 3b 0a 20 20 66 6f 72 28 69 3d 70  mask);.  for(i=p
2ede2 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70  OrWc->nTerm-1, p
2ede3 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b  OrTerm=pOrWc->a;
2ede4 20 69 3e 3d 30 20 26 26 20 69 6e 64 65 78 61 62   i>=0 && indexab
2ede5 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  le; i--, pOrTerm
2ede6 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70 4f  ++){.    if( (pO
2ede7 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2ede8 20 26 20 57 4f 5f 53 49 4e 47 4c 45 29 3d 3d 30   & WO_SINGLE)==0
2ede9 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 41   ){.      WhereA
2edea 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f  ndInfo *pAndInfo
2edeb 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2edec 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2eded 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  or==0 );.      a
2edee 73 73 65 72 74 28 20 28 70 4f 72 54 65 72 6d 2d  ssert( (pOrTerm-
2edef 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2edf0 5f 41 4e 44 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52  _ANDINFO|TERM_OR
2edf1 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 20  INFO))==0 );.   
2edf2 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b     chngToIN = 0;
2edf3 0a 20 20 20 20 20 20 70 41 6e 64 49 6e 66 6f 20  .      pAndInfo 
2edf4 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2edf5 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2edf6 2a 70 41 6e 64 49 6e 66 6f 29 29 3b 0a 20 20 20  *pAndInfo));.   
2edf7 20 20 20 69 66 28 20 70 41 6e 64 49 6e 66 6f 20     if( pAndInfo 
2edf8 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2edf9 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 3b 0a  Clause *pAndWC;.
2edfa 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2edfb 6d 20 2a 70 41 6e 64 54 65 72 6d 3b 0a 20 20 20  m *pAndTerm;.   
2edfc 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2edfd 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20      Bitmask b = 
2edfe 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  0;.        pOrTe
2edff 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 20 3d  rm->u.pAndInfo =
2ee00 20 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20   pAndInfo;.     
2ee01 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2ee02 61 67 73 20 7c 3d 20 54 45 52 4d 5f 41 4e 44 49  ags |= TERM_ANDI
2ee03 4e 46 4f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  NFO;.        pOr
2ee04 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2ee05 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = WO_AND;.      
2ee06 20 20 70 41 6e 64 57 43 20 3d 20 26 70 41 6e 64    pAndWC = &pAnd
2ee07 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20  Info->wc;.      
2ee08 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69    whereClauseIni
2ee09 74 28 70 41 6e 64 57 43 2c 20 70 57 43 2d 3e 70  t(pAndWC, pWC->p
2ee0a 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29  Parse, pMaskSet)
2ee0b 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 53  ;.        whereS
2ee0c 70 6c 69 74 28 70 41 6e 64 57 43 2c 20 70 4f 72  plit(pAndWC, pOr
2ee0d 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 54 4b 5f  Term->pExpr, TK_
2ee0e 41 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 65 78  AND);.        ex
2ee0f 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72  prAnalyzeAll(pSr
2ee10 63 2c 20 70 41 6e 64 57 43 29 3b 0a 20 20 20 20  c, pAndWC);.    
2ee11 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62      testcase( db
2ee12 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2ee13 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64  ;.        if( !d
2ee14 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ee15 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ){.          for
2ee16 28 6a 3d 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70  (j=0, pAndTerm=p
2ee17 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64  AndWC->a; j<pAnd
2ee18 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20  WC->nTerm; j++, 
2ee19 70 41 6e 64 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  pAndTerm++){.   
2ee1a 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2ee1b 20 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72   pAndTerm->pExpr
2ee1c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
2ee1d 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41  if( allowedOp(pA
2ee1e 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f  ndTerm->pExpr->o
2ee1f 70 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  p) ){.          
2ee20 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b      b |= getMask
2ee21 28 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 54  (pMaskSet, pAndT
2ee22 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  erm->leftCursor)
2ee23 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2ee24 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2ee25 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 6e      }.        in
2ee26 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20  dexable &= b;.  
2ee27 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
2ee28 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  if( pOrTerm->wtF
2ee29 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49  lags & TERM_COPI
2ee2a 45 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  ED ){.      /* S
2ee2b 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20 66 6f  kip this term fo
2ee2c 72 20 6e 6f 77 2e 20 20 57 65 20 72 65 76 69 73  r now.  We revis
2ee2d 69 74 20 69 74 20 77 68 65 6e 20 77 65 20 70 72  it it when we pr
2ee2e 6f 63 65 73 73 20 74 68 65 0a 20 20 20 20 20 20  ocess the.      
2ee2f 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
2ee30 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 74 65   TERM_VIRTUAL te
2ee31 72 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b  rm */.    }else{
2ee32 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62  .      Bitmask b
2ee33 3b 0a 20 20 20 20 20 20 62 20 3d 20 67 65 74 4d  ;.      b = getM
2ee34 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f  ask(pMaskSet, pO
2ee35 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2ee36 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  r);.      if( pO
2ee37 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2ee38 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 7b   TERM_VIRTUAL ){
2ee39 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
2ee3a 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f  rm *pOther = &pO
2ee3b 72 57 63 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e  rWc->a[pOrTerm->
2ee3c 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20  iParent];.      
2ee3d 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70    b |= getMask(p
2ee3e 4d 61 73 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d  MaskSet, pOther-
2ee3f 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20  >leftCursor);.  
2ee40 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 64 65      }.      inde
2ee41 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20  xable &= b;.    
2ee42 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65    if( pOrTerm->e
2ee43 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20  Operator!=WO_EQ 
2ee44 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 54  ){.        chngT
2ee45 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  oIN = 0;.      }
2ee46 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 68  else{.        ch
2ee47 6e 67 54 6f 49 4e 20 26 3d 20 62 3b 0a 20 20 20  ngToIN &= b;.   
2ee48 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2ee49 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 63 6f 72 64    /*.  ** Record
2ee4a 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
2ee4b 65 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20  es that satisfy 
2ee4c 63 61 73 65 20 32 2e 20 20 54 68 65 20 73 65 74  case 2.  The set
2ee4d 20 6d 69 67 68 74 20 62 65 0a 20 20 2a 2a 20 65   might be.  ** e
2ee4e 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72  mpty..  */.  pOr
2ee4f 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20  Info->indexable 
2ee50 3d 20 69 6e 64 65 78 61 62 6c 65 3b 0a 20 20 70  = indexable;.  p
2ee51 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2ee52 3d 20 69 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f  = indexable==0 ?
2ee53 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a 0a 20 20 2f   0 : WO_OR;..  /
2ee54 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20  *.  ** chngToIN 
2ee55 68 6f 6c 64 73 20 61 20 73 65 74 20 6f 66 20 74  holds a set of t
2ee56 61 62 6c 65 73 20 74 68 61 74 20 2a 6d 69 67 68  ables that *migh
2ee57 74 2a 20 73 61 74 69 73 66 79 20 63 61 73 65 20  t* satisfy case 
2ee58 31 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20  1.  But.  ** we 
2ee59 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20  have to do some 
2ee5a 61 64 64 69 74 69 6f 6e 61 6c 20 63 68 65 63 6b  additional check
2ee5b 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 63 61  ing to see if ca
2ee5c 73 65 20 31 20 72 65 61 6c 6c 79 0a 20 20 2a 2a  se 1 really.  **
2ee5d 20 69 73 20 73 61 74 69 73 66 69 65 64 2e 0a 20   is satisfied.. 
2ee5e 20 2a 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49   **.  ** chngToI
2ee5f 4e 20 77 69 6c 6c 20 68 6f 6c 64 20 65 69 74 68  N will hold eith
2ee60 65 72 20 30 2c 20 31 2c 20 6f 72 20 32 20 62 69  er 0, 1, or 2 bi
2ee61 74 73 2e 20 20 54 68 65 20 30 2d 62 69 74 20 63  ts.  The 0-bit c
2ee62 61 73 65 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74  ase means.  ** t
2ee63 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hat there is no 
2ee64 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 74  possibility of t
2ee65 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 20  ransforming the 
2ee66 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61  OR clause into a
2ee67 6e 0a 20 20 2a 2a 20 49 4e 20 6f 70 65 72 61 74  n.  ** IN operat
2ee68 6f 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f  or because one o
2ee69 72 20 6d 6f 72 65 20 74 65 72 6d 73 20 69 6e 20  r more terms in 
2ee6a 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 63 6f  the OR clause co
2ee6b 6e 74 61 69 6e 0a 20 20 2a 2a 20 73 6f 6d 65 74  ntain.  ** somet
2ee6c 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
2ee6d 3d 3d 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 69  == on a column i
2ee6e 6e 20 74 68 65 20 73 69 6e 67 6c 65 20 74 61 62  n the single tab
2ee6f 6c 65 2e 20 20 54 68 65 20 31 2d 62 69 74 0a 20  le.  The 1-bit. 
2ee70 20 2a 2a 20 63 61 73 65 20 6d 65 61 6e 73 20 74   ** case means t
2ee71 68 61 74 20 65 76 65 72 79 20 74 65 72 6d 20 6f  hat every term o
2ee72 66 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20  f the OR clause 
2ee73 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  is of the form. 
2ee74 20 2a 2a 20 22 74 61 62 6c 65 2e 63 6f 6c 75 6d   ** "table.colum
2ee75 6e 3d 65 78 70 72 22 20 66 6f 72 20 73 6f 6d 65  n=expr" for some
2ee76 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20   single table.  
2ee77 54 68 65 20 6f 6e 65 20 62 69 74 20 74 68 61 74  The one bit that
2ee78 20 69 73 20 73 65 74 0a 20 20 2a 2a 20 77 69 6c   is set.  ** wil
2ee79 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  l correspond to 
2ee7a 74 68 65 20 63 6f 6d 6d 6f 6e 20 74 61 62 6c 65  the common table
2ee7b 2e 20 20 57 65 20 73 74 69 6c 6c 20 6e 65 65 64  .  We still need
2ee7c 20 74 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b   to check to mak
2ee7d 65 0a 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20  e.  ** sure the 
2ee7e 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75  same column is u
2ee7f 73 65 64 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73  sed on all terms
2ee80 2e 20 20 54 68 65 20 32 2d 62 69 74 20 63 61 73  .  The 2-bit cas
2ee81 65 20 69 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74  e is when.  ** t
2ee82 68 65 20 61 6c 6c 20 74 65 72 6d 73 20 61 72 65  he all terms are
2ee83 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 74 61   of the form "ta
2ee84 62 6c 65 31 2e 63 6f 6c 75 6d 6e 3d 74 61 62 6c  ble1.column=tabl
2ee85 65 32 2e 63 6f 6c 75 6d 6e 22 2e 20 20 49 74 0a  e2.column".  It.
2ee86 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 70 6f    ** might be po
2ee87 73 73 69 62 6c 65 20 74 6f 20 66 6f 72 6d 20 61  ssible to form a
2ee88 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69  n IN operator wi
2ee89 74 68 20 65 69 74 68 65 72 20 74 61 62 6c 65 31  th either table1
2ee8a 2e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 72 20  .column.  ** or 
2ee8b 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 20 61 73  table2.column as
2ee8c 20 74 68 65 20 4c 48 53 20 69 66 20 65 69 74 68   the LHS if eith
2ee8d 65 72 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  er is common to 
2ee8e 65 76 65 72 79 20 74 65 72 6d 20 6f 66 0a 20 20  every term of.  
2ee8f 2a 2a 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  ** the OR clause
2ee90 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
2ee91 20 74 68 61 74 20 74 65 72 6d 73 20 6f 66 20 74   that terms of t
2ee92 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65 2e 63  he form "table.c
2ee93 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65 2e 63 6f 6c  olumn1=table.col
2ee94 75 6d 6e 32 22 20 28 74 68 65 0a 20 20 2a 2a 20  umn2" (the.  ** 
2ee95 73 61 6d 65 20 74 61 62 6c 65 20 6f 6e 20 62 6f  same table on bo
2ee96 74 68 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20  th sizes of the 
2ee97 3d 3d 29 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70  ==) cannot be op
2ee98 74 69 6d 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20  timized..  */.  
2ee99 69 66 28 20 63 68 6e 67 54 6f 49 4e 20 29 7b 0a  if( chngToIN ){.
2ee9a 20 20 20 20 69 6e 74 20 6f 6b 54 6f 43 68 6e 67      int okToChng
2ee9b 54 6f 49 4e 20 3d 20 30 3b 20 20 20 20 20 2f 2a  ToIN = 0;     /*
2ee9c 20 54 72 75 65 20 69 66 20 74 68 65 20 63 6f 6e   True if the con
2ee9d 76 65 72 73 69 6f 6e 20 74 6f 20 49 4e 20 69 73  version to IN is
2ee9e 20 76 61 6c 69 64 20 2a 2f 0a 20 20 20 20 69 6e   valid */.    in
2ee9f 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20  t iColumn = -1; 
2eea0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2eea1 6e 20 69 6e 64 65 78 20 6f 6e 20 6c 68 73 20 6f  n index on lhs o
2eea2 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  f IN operator */
2eea3 0a 20 20 20 20 69 6e 74 20 69 43 75 72 73 6f 72  .    int iCursor
2eea4 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f   = -1;         /
2eea5 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 63  * Table cursor c
2eea6 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 74 65 72  ommon to all ter
2eea7 6d 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a 20  ms */.    int j 
2eea8 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2eea9 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2eeaa 74 65 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53  ter */..    /* S
2eeab 65 61 72 63 68 20 66 6f 72 20 61 20 74 61 62 6c  earch for a tabl
2eeac 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 61  e and column tha
2eead 74 20 61 70 70 65 61 72 73 20 6f 6e 20 6f 6e 65  t appears on one
2eeae 20 73 69 64 65 20 6f 72 20 74 68 65 0a 20 20 20   side or the.   
2eeaf 20 2a 2a 20 6f 74 68 65 72 20 6f 66 20 74 68 65   ** other of the
2eeb0 20 3d 3d 20 6f 70 65 72 61 74 6f 72 20 69 6e 20   == operator in 
2eeb1 65 76 65 72 79 20 73 75 62 74 65 72 6d 2e 20 20  every subterm.  
2eeb2 54 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 63  That table and c
2eeb3 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 77 69 6c  olumn.    ** wil
2eeb4 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20 69 6e  l be recorded in
2eeb5 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69 43 6f   iCursor and iCo
2eeb6 6c 75 6d 6e 2e 20 20 54 68 65 72 65 20 6d 69 67  lumn.  There mig
2eeb7 68 74 20 6e 6f 74 20 62 65 20 61 6e 79 0a 20 20  ht not be any.  
2eeb8 20 20 2a 2a 20 73 75 63 68 20 74 61 62 6c 65 20    ** such table 
2eeb9 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 53 65 74  and column.  Set
2eeba 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69 66   okToChngToIN if
2eebb 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
2eebc 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 61 6e 64  table.    ** and
2eebd 20 63 6f 6c 75 6d 6e 20 69 73 20 66 6f 75 6e 64   column is found
2eebe 20 62 75 74 20 6c 65 61 76 65 20 6f 6b 54 6f 43   but leave okToC
2eebf 68 6e 67 54 6f 49 4e 20 66 61 6c 73 65 20 69 66  hngToIN false if
2eec0 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 20 20   not found..    
2eec1 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  */.    for(j=0; 
2eec2 6a 3c 32 20 26 26 20 21 6f 6b 54 6f 43 68 6e 67  j<2 && !okToChng
2eec3 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  ToIN; j++){.    
2eec4 20 20 70 4f 72 54 65 72 6d 20 3d 20 70 4f 72 57    pOrTerm = pOrW
2eec5 63 2d 3e 61 3b 0a 20 20 20 20 20 20 66 6f 72 28  c->a;.      for(
2eec6 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31  i=pOrWc->nTerm-1
2eec7 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72  ; i>=0; i--, pOr
2eec8 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
2eec9 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2eeca 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2eecb 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  EQ );.        pO
2eecc 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2eecd 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20  = ~TERM_OR_OK;. 
2eece 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
2eecf 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d  rm->leftCursor==
2eed0 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  iCursor ){.     
2eed1 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20       /* This is 
2eed2 74 68 65 20 32 2d 62 69 74 20 63 61 73 65 20 61  the 2-bit case a
2eed3 6e 64 20 77 65 20 61 72 65 20 6f 6e 20 74 68 65  nd we are on the
2eed4 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f   second iteratio
2eed5 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20  n and.          
2eed6 2a 2a 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20  ** current term 
2eed7 69 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73  is from the firs
2eed8 74 20 69 74 65 72 61 74 69 6f 6e 2e 20 20 53 6f  t iteration.  So
2eed9 20 73 6b 69 70 20 74 68 69 73 20 74 65 72 6d 2e   skip this term.
2eeda 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73   */.          as
2eedb 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20  sert( j==1 );.  
2eedc 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
2eedd 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2eede 20 20 20 20 69 66 28 20 28 63 68 6e 67 54 6f 49      if( (chngToI
2eedf 4e 20 26 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  N & getMask(pMas
2eee0 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c  kSet, pOrTerm->l
2eee1 65 66 74 43 75 72 73 6f 72 29 29 3d 3d 30 20 29  eftCursor))==0 )
2eee2 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
2eee3 68 69 73 20 74 65 72 6d 20 6d 75 73 74 20 62 65  his term must be
2eee4 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 74 31 2e   of the form t1.
2eee5 61 3d 3d 74 32 2e 62 20 77 68 65 72 65 20 74 32  a==t2.b where t2
2eee6 20 69 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20   is in the.     
2eee7 20 20 20 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e       ** chngToIN
2eee8 20 73 65 74 20 62 75 74 20 74 31 20 69 73 20 6e   set but t1 is n
2eee9 6f 74 2e 20 20 54 68 69 73 20 74 65 72 6d 20 77  ot.  This term w
2eeea 69 6c 6c 20 62 65 20 65 69 74 68 65 72 20 70 72  ill be either pr
2eeeb 65 63 65 65 64 65 64 0a 20 20 20 20 20 20 20 20  eceeded.        
2eeec 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c 77 65 64 20    ** or follwed 
2eeed 62 79 20 61 6e 20 69 6e 76 65 72 74 65 64 20 63  by an inverted c
2eeee 6f 70 79 20 28 74 32 2e 62 3d 3d 74 31 2e 61 29  opy (t2.b==t1.a)
2eeef 2e 20 20 53 6b 69 70 20 74 68 69 73 20 74 65 72  .  Skip this ter
2eef0 6d 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  m .          ** 
2eef1 61 6e 64 20 75 73 65 20 69 74 73 20 69 6e 76 65  and use its inve
2eef2 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  rsion. */.      
2eef3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4f      testcase( pO
2eef4 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2eef5 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29 3b 0a   TERM_COPIED );.
2eef6 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61            testca
2eef7 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46  se( pOrTerm->wtF
2eef8 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
2eef9 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 20 20 20  UAL );.         
2eefa 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2eefb 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52  ->wtFlags & (TER
2eefc 4d 5f 43 4f 50 49 45 44 7c 54 45 52 4d 5f 56 49  M_COPIED|TERM_VI
2eefd 52 54 55 41 4c 29 20 29 3b 0a 20 20 20 20 20 20  RTUAL) );.      
2eefe 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2eeff 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ef00 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 54 65 72  iColumn = pOrTer
2ef01 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b  m->u.leftColumn;
2ef02 0a 20 20 20 20 20 20 20 20 69 43 75 72 73 6f 72  .        iCursor
2ef03 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74   = pOrTerm->left
2ef04 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
2ef05 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2ef06 20 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b 0a       if( i<0 ){.
2ef07 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 63 61          /* No ca
2ef08 6e 64 69 64 61 74 65 20 74 61 62 6c 65 2b 63 6f  ndidate table+co
2ef09 6c 75 6d 6e 20 77 61 73 20 66 6f 75 6e 64 2e 20  lumn was found. 
2ef0a 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 6f   This can only o
2ef0b 63 63 75 72 0a 20 20 20 20 20 20 20 20 2a 2a 20  ccur.        ** 
2ef0c 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74  on the second it
2ef0d 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  eration */.     
2ef0e 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 31 20     assert( j==1 
2ef0f 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
2ef10 74 28 20 28 63 68 6e 67 54 6f 49 4e 26 28 63 68  t( (chngToIN&(ch
2ef11 6e 67 54 6f 49 4e 2d 31 29 29 3d 3d 30 20 29 3b  ngToIN-1))==0 );
2ef12 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2ef13 20 63 68 6e 67 54 6f 49 4e 3d 3d 67 65 74 4d 61   chngToIN==getMa
2ef14 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 43 75  sk(pMaskSet, iCu
2ef15 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20 20 20  rsor) );.       
2ef16 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2ef17 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ef18 6a 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 20 20 2f  j==1 );..      /
2ef19 2a 20 57 65 20 68 61 76 65 20 66 6f 75 6e 64 20  * We have found 
2ef1a 61 20 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c  a candidate tabl
2ef1b 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 43  e and column.  C
2ef1c 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2ef1d 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 74 61 62  hat.      ** tab
2ef1e 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73  le and column is
2ef1f 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79   common to every
2ef20 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52 20   term in the OR 
2ef21 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
2ef22 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 31  okToChngToIN = 1
2ef23 3b 0a 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e  ;.      for(; i>
2ef24 3d 30 20 26 26 20 6f 6b 54 6f 43 68 6e 67 54 6f  =0 && okToChngTo
2ef25 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d  IN; i--, pOrTerm
2ef26 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  ++){.        ass
2ef27 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f  ert( pOrTerm->eO
2ef28 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29  perator==WO_EQ )
2ef29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  ;.        if( pO
2ef2a 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2ef2b 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20  r!=iCursor ){.  
2ef2c 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2ef2d 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52  >wtFlags &= ~TER
2ef2e 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2ef2f 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65   }else if( pOrTe
2ef30 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e  rm->u.leftColumn
2ef31 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  !=iColumn ){.   
2ef32 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54         okToChngT
2ef33 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  oIN = 0;.       
2ef34 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ef35 20 20 69 6e 74 20 61 66 66 4c 65 66 74 2c 20 61    int affLeft, a
2ef36 66 66 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  ffRight;.       
2ef37 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 69 67     /* If the rig
2ef38 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ht-hand side is 
2ef39 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74  also a column, t
2ef3a 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 69  hen the affiniti
2ef3b 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
2ef3c 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20 61 6e  of both right an
2ef3d 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d 75 73  d left sides mus
2ef3e 74 20 62 65 20 73 75 63 68 20 74 68 61 74 20 6e  t be such that n
2ef3f 6f 20 74 79 70 65 0a 20 20 20 20 20 20 20 20 20  o type.         
2ef40 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20   ** conversions 
2ef41 61 72 65 20 72 65 71 75 69 72 65 64 20 6f 6e 20  are required on 
2ef42 74 68 65 20 72 69 67 68 74 2e 20 20 28 54 69 63  the right.  (Tic
2ef43 6b 65 74 20 23 32 32 34 39 29 0a 20 20 20 20 20  ket #2249).     
2ef44 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2ef45 20 20 61 66 66 52 69 67 68 74 20 3d 20 73 71 6c    affRight = sql
2ef46 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
2ef47 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d  (pOrTerm->pExpr-
2ef48 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
2ef49 20 20 20 20 61 66 66 4c 65 66 74 20 3d 20 73 71      affLeft = sq
2ef4a 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2ef4b 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  y(pOrTerm->pExpr
2ef4c 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
2ef4d 20 20 20 20 69 66 28 20 61 66 66 52 69 67 68 74      if( affRight
2ef4e 21 3d 30 20 26 26 20 61 66 66 52 69 67 68 74 21  !=0 && affRight!
2ef4f 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20 20 20  =affLeft ){.    
2ef50 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67          okToChng
2ef51 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20  ToIN = 0;.      
2ef52 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ef53 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2ef54 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2ef55 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  OR_OK;.         
2ef56 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2ef57 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2ef58 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
2ef59 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69  , okToChngToIN i
2ef5a 73 20 74 72 75 65 20 69 66 20 6f 72 69 67 69 6e  s true if origin
2ef5b 61 6c 20 70 54 65 72 6d 20 73 61 74 69 73 66 69  al pTerm satisfi
2ef5c 65 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 20 31  es.    ** case 1
2ef5d 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c  .  In that case,
2ef5e 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77   construct a new
2ef5f 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20 74 68   virtual term th
2ef60 61 74 20 69 73 20 0a 20 20 20 20 2a 2a 20 70 54  at is .    ** pT
2ef61 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  erm converted in
2ef62 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  to an IN operato
2ef63 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
2ef64 28 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 29  ( okToChngToIN )
2ef65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44  {.      Expr *pD
2ef66 75 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  up;            /
2ef67 2a 20 41 20 74 72 61 6e 73 69 65 6e 74 20 64 75  * A transient du
2ef68 70 6c 69 63 61 74 65 20 65 78 70 72 65 73 73 69  plicate expressi
2ef69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
2ef6a 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 30 3b  List *pList = 0;
2ef6b 20 20 20 2f 2a 20 54 68 65 20 52 48 53 20 6f 66     /* The RHS of
2ef6c 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2ef6d 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2ef6e 70 4c 65 66 74 20 3d 20 30 3b 20 20 20 20 20 20  pLeft = 0;      
2ef6f 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74   /* The LHS of t
2ef70 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  he IN operator *
2ef71 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  /.      Expr *pN
2ef72 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ew;            /
2ef73 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 49  * The complete I
2ef74 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 0a 20  N operator */.. 
2ef75 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63       for(i=pOrWc
2ef76 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65  ->nTerm-1, pOrTe
2ef77 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d  rm=pOrWc->a; i>=
2ef78 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b  0; i--, pOrTerm+
2ef79 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2ef7a 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67  (pOrTerm->wtFlag
2ef7b 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d  s & TERM_OR_OK)=
2ef7c 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2ef7d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2ef7e 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2ef7f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
2ef80 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2ef81 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2ef82 3d 69 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20  =iCursor );.    
2ef83 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2ef84 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2ef85 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20  n==iColumn );.  
2ef86 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c        pDup = sql
2ef87 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2ef88 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e  pOrTerm->pExpr->
2ef89 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20  pRight, 0);.    
2ef8a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
2ef8b 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2ef8c 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  d(pWC->pParse, p
2ef8d 4c 69 73 74 2c 20 70 44 75 70 29 3b 0a 20 20 20  List, pDup);.   
2ef8e 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4f 72       pLeft = pOr
2ef8f 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65  Term->pExpr->pLe
2ef90 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ft;.      }.    
2ef91 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 21    assert( pLeft!
2ef92 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 44 75 70  =0 );.      pDup
2ef93 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2ef94 70 28 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 3b  p(db, pLeft, 0);
2ef95 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71  .      pNew = sq
2ef96 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ef97 65 2c 20 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20  e, TK_IN, pDup, 
2ef98 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  0, 0);.      if(
2ef99 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
2ef9a 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
2ef9b 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f 69       transferJoi
2ef9c 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20  nMarkings(pNew, 
2ef9d 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
2ef9e 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2ef9f 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
2efa0 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2efa1 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e          pNew->x.
2efa2 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
2efa3 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
2efa4 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2efa5 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54 45 52  t(pWC, pNew, TER
2efa6 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2efa7 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
2efa8 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2efa9 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  w==0 );.        
2efaa 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
2efab 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a  , pWC, idxNew);.
2efac 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20          pTerm = 
2efad 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2efae 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d 3e 61  ;.        pWC->a
2efaf 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74  [idxNew].iParent
2efb0 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2efb1 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2efb2 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  d = 1;.      }el
2efb3 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2efb4 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2efb5 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
2efb6 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 65 72      }.      pTer
2efb7 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30  m->eOperator = 0
2efb8 3b 20 20 2f 2a 20 63 61 73 65 20 31 20 74 72 75  ;  /* case 1 tru
2efb9 6d 70 73 20 63 61 73 65 20 32 20 2a 2f 0a 20 20  mps case 2 */.  
2efba 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
2efbb 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
2efbc 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
2efbd 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   && !SQLITE_OMIT
2efbe 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f  _SUBQUERY */.../
2efbf 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74  *.** The input t
2efc0 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
2efc1 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d 20 73  s an WhereTerm s
2efc2 74 72 75 63 74 75 72 65 20 77 69 74 68 20 6f 6e  tructure with on
2efc3 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45 78 70 72  ly the.** "pExpr
2efc4 22 20 66 69 65 6c 64 20 66 69 6c 6c 65 64 20 69  " field filled i
2efc5 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66 20 74  n.  The job of t
2efc6 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
2efc7 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a  o analyze the.**
2efc8 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 61   subexpression a
2efc9 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20  nd populate all 
2efca 74 68 65 20 6f 74 68 65 72 20 66 69 65 6c 64 73  the other fields
2efcb 20 6f 66 20 74 68 65 20 57 68 65 72 65 54 65 72   of the WhereTer
2efcc 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  m.** structure..
2efcd 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
2efce 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
2efcf 65 20 66 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c  e form "<expr> <
2efd0 6f 70 3e 20 58 22 20 69 74 20 67 65 74 73 20 63  op> X" it gets c
2efd1 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68  ommuted.** to th
2efd2 65 20 73 74 61 6e 64 61 72 64 20 66 6f 72 6d 20  e standard form 
2efd3 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72  of "X <op> <expr
2efd4 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  >"..**.** If the
2efd5 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2efd6 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f  f the form "X <o
2efd7 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f 74 68  p> Y" where both
2efd8 20 58 20 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20   X and Y are.** 
2efd9 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68  columns, then th
2efda 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65  e original expre
2efdb 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67  ssion is unchang
2efdc 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76 69 72  ed and a new vir
2efdd 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20  tual.** term of 
2efde 74 68 65 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e  the form "Y <op>
2efdf 20 58 22 20 69 73 20 61 64 64 65 64 20 74 6f 20   X" is added to 
2efe0 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2efe1 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64   and.** analyzed
2efe2 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 54 68   separately.  Th
2efe3 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20  e original term 
2efe4 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54  is marked with T
2efe5 45 52 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e  ERM_COPIED.** an
2efe6 64 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20 69  d the new term i
2efe7 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45  s marked with TE
2efe8 52 4d 5f 44 59 4e 41 4d 49 43 20 28 62 65 63 61  RM_DYNAMIC (beca
2efe9 75 73 65 20 69 74 27 73 20 70 45 78 70 72 0a 2a  use it's pExpr.*
2efea 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72  * needs to be fr
2efeb 65 65 64 20 77 69 74 68 20 74 68 65 20 57 68 65  eed with the Whe
2efec 72 65 43 6c 61 75 73 65 29 20 61 6e 64 20 54 45  reClause) and TE
2efed 52 4d 5f 56 49 52 54 55 41 4c 20 28 62 65 63 61  RM_VIRTUAL (beca
2efee 75 73 65 20 69 74 0a 2a 2a 20 69 73 20 61 20 63  use it.** is a c
2efef 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f 66 20  ommuted copy of 
2eff0 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29 20 20  a prior term.)  
2eff1 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72  The original ter
2eff2 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a  m has nChild=1.*
2eff3 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79 20 68  * and the copy h
2eff4 61 73 20 69 64 78 50 61 72 65 6e 74 20 73 65 74  as idxParent set
2eff5 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
2eff6 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65   the original te
2eff7 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  rm..*/.static vo
2eff8 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a  id exprAnalyze(.
2eff9 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
2effa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74              /* t
2effb 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
2effc 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2effd 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a  *pWC,         /*
2effe 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2efff 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65  e */.  int idxTe
2f000 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rm              
2f001 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2f002 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c   term to be anal
2f003 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65  yzed */.){.  Whe
2f004 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
2f005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f006 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2f007 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68  analyzed */.  Wh
2f008 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2f009 6b 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f  kSet;          /
2f00a 2a 20 53 65 74 20 6f 66 20 74 61 62 6c 65 20 69  * Set of table i
2f00b 6e 64 65 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20  ndex masks */.  
2f00c 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
2f00d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f00e 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2f00f 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  on to be analyze
2f010 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70  d */.  Bitmask p
2f011 72 65 72 65 71 4c 65 66 74 3b 20 20 20 20 20 20  rereqLeft;      
2f012 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65          /* Prere
2f013 71 75 65 73 69 74 65 73 20 6f 66 20 74 68 65 20  quesites of the 
2f014 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a  pExpr->pLeft */.
2f015 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71    Bitmask prereq
2f016 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  All;            
2f017 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69     /* Prerequesi
2f018 74 65 73 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a  tes of pExpr */.
2f019 20 20 42 69 74 6d 61 73 6b 20 65 78 74 72 61 52    Bitmask extraR
2f01a 69 67 68 74 20 3d 20 30 3b 20 20 20 20 20 20 20  ight = 0;       
2f01b 20 20 20 2f 2a 20 2a 2f 0a 20 20 45 78 70 72 20     /* */.  Expr 
2f01c 2a 70 53 74 72 31 20 3d 20 30 3b 20 20 20 20 20  *pStr1 = 0;     
2f01d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2f01e 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20  HS of LIKE/GLOB 
2f01f 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e  operator */.  in
2f020 74 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30  t isComplete = 0
2f021 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2f022 2a 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c  * RHS of LIKE/GL
2f023 4f 42 20 65 6e 64 73 20 77 69 74 68 20 77 69 6c  OB ends with wil
2f024 64 63 61 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  dcard */.  int n
2f025 6f 43 61 73 65 20 3d 20 30 3b 20 20 20 20 20 20  oCase = 0;      
2f026 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2f027 49 4b 45 2f 47 4c 4f 42 20 64 69 73 74 69 6e 67  IKE/GLOB disting
2f028 75 69 73 68 65 73 20 63 61 73 65 20 2a 2f 0a 20  uishes case */. 
2f029 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20   int op;        
2f02a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f02b 20 20 2f 2a 20 54 6f 70 2d 6c 65 76 65 6c 20 6f    /* Top-level o
2f02c 70 65 72 61 74 6f 72 2e 20 20 70 45 78 70 72 2d  perator.  pExpr-
2f02d 3e 6f 70 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  >op */.  Parse *
2f02e 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50  pParse = pWC->pP
2f02f 61 72 73 65 3b 20 20 20 20 20 2f 2a 20 50 61 72  arse;     /* Par
2f030 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2f031 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2f032 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2f033 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2f034 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
2f035 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2f036 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
2f037 72 6e 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20  rn;.  }.  pTerm 
2f038 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
2f039 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d  m];.  pMaskSet =
2f03a 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a   pWC->pMaskSet;.
2f03b 20 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d    pExpr = pTerm-
2f03c 3e 70 45 78 70 72 3b 0a 20 20 70 72 65 72 65 71  >pExpr;.  prereq
2f03d 4c 65 66 74 20 3d 20 65 78 70 72 54 61 62 6c 65  Left = exprTable
2f03e 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2f03f 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20  pExpr->pLeft);. 
2f040 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
2f041 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  .  if( op==TK_IN
2f042 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2f043 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30  pExpr->pRight==0
2f044 20 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72   );.    if( Expr
2f045 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2f046 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2f047 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d   ){.      pTerm-
2f048 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65  >prereqRight = e
2f049 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
2f04a 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
2f04b 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b  xpr->x.pSelect);
2f04c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2f04d 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52    pTerm->prereqR
2f04e 69 67 68 74 20 3d 20 65 78 70 72 4c 69 73 74 54  ight = exprListT
2f04f 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2f050 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  et, pExpr->x.pLi
2f051 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  st);.    }.  }el
2f052 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53  se if( op==TK_IS
2f053 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 54 65 72  NULL ){.    pTer
2f054 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
2f055 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2f056 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69   pTerm->prereqRi
2f057 67 68 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55  ght = exprTableU
2f058 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2f059 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
2f05a 20 7d 0a 20 20 70 72 65 72 65 71 41 6c 6c 20 3d   }.  prereqAll =
2f05b 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2f05c 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 29  pMaskSet, pExpr)
2f05d 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
2f05e 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2f05f 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20  P_FromJoin) ){. 
2f060 20 20 20 42 69 74 6d 61 73 6b 20 78 20 3d 20 67     Bitmask x = g
2f061 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2f062 20 70 45 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f   pExpr->iRightJo
2f063 69 6e 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 72  inTable);.    pr
2f064 65 72 65 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20  ereqAll |= x;.  
2f065 20 20 65 78 74 72 61 52 69 67 68 74 20 3d 20 78    extraRight = x
2f066 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73  -1;  /* ON claus
2f067 65 20 74 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20  e terms may not 
2f068 62 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20  be used with an 
2f069 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20 20 20  index.          
2f06a 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
2f06b 6f 6e 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66  on left table of
2f06c 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54   a LEFT JOIN.  T
2f06d 69 63 6b 65 74 20 23 33 30 31 35 20 2a 2f 0a 20  icket #3015 */. 
2f06e 20 7d 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72   }.  pTerm->prer
2f06f 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c  eqAll = prereqAl
2f070 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74  l;.  pTerm->left
2f071 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70  Cursor = -1;.  p
2f072 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20  Term->iParent = 
2f073 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70  -1;.  pTerm->eOp
2f074 65 72 61 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66  erator = 0;.  if
2f075 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20  ( allowedOp(op) 
2f076 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  && (pTerm->prere
2f077 71 52 69 67 68 74 20 26 20 70 72 65 72 65 71 4c  qRight & prereqL
2f078 65 66 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45  eft)==0 ){.    E
2f079 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78  xpr *pLeft = pEx
2f07a 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45  pr->pLeft;.    E
2f07b 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45  xpr *pRight = pE
2f07c 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
2f07d 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d   if( pLeft->op==
2f07e 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2f07f 20 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75     pTerm->leftCu
2f080 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54  rsor = pLeft->iT
2f081 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 54 65 72  able;.      pTer
2f082 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20  m->u.leftColumn 
2f083 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  = pLeft->iColumn
2f084 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 65  ;.      pTerm->e
2f085 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65 72 61  Operator = opera
2f086 74 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20 20 20  torMask(op);.   
2f087 20 7d 0a 20 20 20 20 69 66 28 20 70 52 69 67 68   }.    if( pRigh
2f088 74 20 26 26 20 70 52 69 67 68 74 2d 3e 6f 70 3d  t && pRight->op=
2f089 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  =TK_COLUMN ){.  
2f08a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2f08b 4e 65 77 3b 0a 20 20 20 20 20 20 45 78 70 72 20  New;.      Expr 
2f08c 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28  *pDup;.      if(
2f08d 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
2f08e 6f 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  or>=0 ){.       
2f08f 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
2f090 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69       pDup = sqli
2f091 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2f092 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  Expr, 0);.      
2f093 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2f094 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
2f095 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2f096 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b  elete(db, pDup);
2f097 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
2f098 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
2f099 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68       idxNew = wh
2f09a 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2f09b 70 57 43 2c 20 70 44 75 70 2c 20 54 45 52 4d 5f  pWC, pDup, TERM_
2f09c 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
2f09d 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 69  AMIC);.        i
2f09e 66 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 20 72  f( idxNew==0 ) r
2f09f 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 70  eturn;.        p
2f0a0 4e 65 77 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  New = &pWC->a[id
2f0a1 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 20 20 70  xNew];.        p
2f0a2 4e 65 77 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69  New->iParent = i
2f0a3 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20  dxTerm;.        
2f0a4 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2f0a5 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
2f0a6 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20    pTerm->nChild 
2f0a7 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 54 65  = 1;.        pTe
2f0a8 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2f0a9 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20  ERM_COPIED;.    
2f0aa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2f0ab 20 70 44 75 70 20 3d 20 70 45 78 70 72 3b 0a 20   pDup = pExpr;. 
2f0ac 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 54         pNew = pT
2f0ad 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  erm;.      }.   
2f0ae 20 20 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 70     exprCommute(p
2f0af 50 61 72 73 65 2c 20 70 44 75 70 29 3b 0a 20 20  Parse, pDup);.  
2f0b0 20 20 20 20 70 4c 65 66 74 20 3d 20 70 44 75 70      pLeft = pDup
2f0b1 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 70  ->pLeft;.      p
2f0b2 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  New->leftCursor 
2f0b3 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b  = pLeft->iTable;
2f0b4 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 6c  .      pNew->u.l
2f0b5 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
2f0b6 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
2f0b7 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52 69    pNew->prereqRi
2f0b8 67 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66 74  ght = prereqLeft
2f0b9 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72  ;.      pNew->pr
2f0ba 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71  ereqAll = prereq
2f0bb 41 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  All;.      pNew-
2f0bc 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
2f0bd 72 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e  ratorMask(pDup->
2f0be 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  op);.    }.  }..
2f0bf 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2f0c0 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49  MIT_BETWEEN_OPTI
2f0c1 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66  MIZATION.  /* If
2f0c2 20 61 20 74 65 72 6d 20 69 73 20 74 68 65 20 42   a term is the B
2f0c3 45 54 57 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c  ETWEEN operator,
2f0c4 20 63 72 65 61 74 65 20 74 77 6f 20 6e 65 77 20   create two new 
2f0c5 76 69 72 74 75 61 6c 20 74 65 72 6d 73 0a 20 20  virtual terms.  
2f0c6 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 20 74  ** that define t
2f0c7 68 65 20 72 61 6e 67 65 20 74 68 61 74 20 74 68  he range that th
2f0c8 65 20 42 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d  e BETWEEN implem
2f0c9 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ents.  For examp
2f0ca 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  le:.  **.  **   
2f0cb 20 20 20 61 20 42 45 54 57 45 45 4e 20 62 20 41     a BETWEEN b A
2f0cc 4e 44 20 63 0a 20 20 2a 2a 0a 20 20 2a 2a 20 69  ND c.  **.  ** i
2f0cd 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
2f0ce 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2f0cf 20 28 61 20 42 45 54 57 45 45 4e 20 62 20 41 4e   (a BETWEEN b AN
2f0d0 44 20 63 29 20 41 4e 44 20 28 61 3e 3d 62 29 20  D c) AND (a>=b) 
2f0d1 41 4e 44 20 28 61 3c 3d 63 29 0a 20 20 2a 2a 0a  AND (a<=c).  **.
2f0d2 20 20 2a 2a 20 54 68 65 20 74 77 6f 20 6e 65 77    ** The two new
2f0d3 20 74 65 72 6d 73 20 61 72 65 20 61 64 64 65 64   terms are added
2f0d4 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
2f0d5 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2f0d6 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68   object..  ** Th
2f0d7 65 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20  e new terms are 
2f0d8 22 64 79 6e 61 6d 69 63 22 20 61 6e 64 20 61 72  "dynamic" and ar
2f0d9 65 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68  e children of th
2f0da 65 20 6f 72 69 67 69 6e 61 6c 20 42 45 54 57 45  e original BETWE
2f0db 45 4e 0a 20 20 2a 2a 20 74 65 72 6d 2e 20 20 54  EN.  ** term.  T
2f0dc 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69  hat means that i
2f0dd 66 20 74 68 65 20 42 45 54 57 45 45 4e 20 74 65  f the BETWEEN te
2f0de 72 6d 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65  rm is coded, the
2f0df 20 63 68 69 6c 64 72 65 6e 20 61 72 65 0a 20 20   children are.  
2f0e0 2a 2a 20 73 6b 69 70 70 65 64 2e 20 20 4f 72 2c  ** skipped.  Or,
2f0e1 20 69 66 20 74 68 65 20 63 68 69 6c 64 72 65 6e   if the children
2f0e2 20 61 72 65 20 73 61 74 69 73 66 69 65 64 20 62   are satisfied b
2f0e3 79 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65 20  y an index, the 
2f0e4 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 42 45  original.  ** BE
2f0e5 54 57 45 45 4e 20 74 65 72 6d 20 69 73 20 73 6b  TWEEN term is sk
2f0e6 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c  ipped..  */.  el
2f0e7 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  se if( pExpr->op
2f0e8 3d 3d 54 4b 5f 42 45 54 57 45 45 4e 20 26 26 20  ==TK_BETWEEN && 
2f0e9 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20  pWC->op==TK_AND 
2f0ea 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  ){.    ExprList 
2f0eb 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  *pList = pExpr->
2f0ec 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74  x.pList;.    int
2f0ed 20 69 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63   i;.    static c
2f0ee 6f 6e 73 74 20 75 38 20 6f 70 73 5b 5d 20 3d 20  onst u8 ops[] = 
2f0ef 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45 7d 3b 0a  {TK_GE, TK_LE};.
2f0f0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
2f0f1 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t!=0 );.    asse
2f0f2 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  rt( pList->nExpr
2f0f3 3d 3d 32 20 29 3b 0a 20 20 20 20 66 6f 72 28 69  ==2 );.    for(i
2f0f4 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20  =0; i<2; i++){. 
2f0f5 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45       Expr *pNewE
2f0f6 78 70 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  xpr;.      int i
2f0f7 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 70 4e 65  dxNew;.      pNe
2f0f8 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  wExpr = sqlite3P
2f0f9 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 73  Expr(pParse, ops
2f0fa 5b 69 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20  [i], .          
2f0fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0fc 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2f0fd 70 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65  p(db, pExpr->pLe
2f0fe 66 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20  ft, 0),.        
2f0ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f100 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2f101 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61  Dup(db, pList->a
2f102 5b 69 5d 2e 70 45 78 70 72 2c 20 30 29 2c 20 30  [i].pExpr, 0), 0
2f103 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20  );.      idxNew 
2f104 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2f105 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
2f106 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  r, TERM_VIRTUAL|
2f107 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  TERM_DYNAMIC);. 
2f108 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f109 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20  dxNew==0 );.    
2f10a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53    exprAnalyze(pS
2f10b 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29  rc, pWC, idxNew)
2f10c 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
2f10d 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2f10e 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69  ;.      pWC->a[i
2f10f 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d  dxNew].iParent =
2f110 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 7d 0a   idxTerm;.    }.
2f111 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2f112 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65 6e 64 69  d = 2;.  }.#endi
2f113 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2f114 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a  _BETWEEN_OPTIMIZ
2f115 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 20 21 64  ATION */..#if !d
2f116 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2f117 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
2f118 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  ON) && !defined(
2f119 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2f11a 55 45 52 59 29 0a 20 20 2f 2a 20 41 6e 61 6c 79  UERY).  /* Analy
2f11b 7a 65 20 61 20 74 65 72 6d 20 74 68 61 74 20 69  ze a term that i
2f11c 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 74 77  s composed of tw
2f11d 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65 72  o or more subter
2f11e 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a  ms connected by.
2f11f 20 20 2a 2a 20 61 6e 20 4f 52 20 6f 70 65 72 61    ** an OR opera
2f120 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65  tor..  */.  else
2f121 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d   if( pExpr->op==
2f122 54 4b 5f 4f 52 20 29 7b 0a 20 20 20 20 61 73 73  TK_OR ){.    ass
2f123 65 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b  ert( pWC->op==TK
2f124 5f 41 4e 44 20 29 3b 0a 20 20 20 20 65 78 70 72  _AND );.    expr
2f125 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 70 53  AnalyzeOrTerm(pS
2f126 72 63 2c 20 70 57 43 2c 20 69 64 78 54 65 72 6d  rc, pWC, idxTerm
2f127 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  );.    pTerm = &
2f128 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b  pWC->a[idxTerm];
2f129 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2f12a 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2f12b 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23  TIMIZATION */..#
2f12c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2f12d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
2f12e 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64 20 63 6f  TION.  /* Add co
2f12f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72 65 64  nstraints to red
2f130 75 63 65 20 74 68 65 20 73 65 61 72 63 68 20 73  uce the search s
2f131 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b 45 20 6f  pace on a LIKE o
2f132 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f 70 65 72  r GLOB.  ** oper
2f133 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ator..  **.  ** 
2f134 41 20 6c 69 6b 65 20 70 61 74 74 65 72 6e 20 6f  A like pattern o
2f135 66 20 74 68 65 20 66 6f 72 6d 20 22 78 20 4c 49  f the form "x LI
2f136 4b 45 20 27 61 62 63 25 27 22 20 69 73 20 63 68  KE 'abc%'" is ch
2f137 61 6e 67 65 64 20 69 6e 74 6f 20 63 6f 6e 73 74  anged into const
2f138 72 61 69 6e 74 73 0a 20 20 2a 2a 0a 20 20 2a 2a  raints.  **.  **
2f139 20 20 20 20 20 20 20 20 20 20 78 3e 3d 27 61 62            x>='ab
2f13a 63 27 20 41 4e 44 20 78 3c 27 61 62 64 27 20 41  c' AND x<'abd' A
2f13b 4e 44 20 78 20 4c 49 4b 45 20 27 61 62 63 25 27  ND x LIKE 'abc%'
2f13c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c  .  **.  ** The l
2f13d 61 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66  ast character of
2f13e 20 74 68 65 20 70 72 65 66 69 78 20 22 61 62 63   the prefix "abc
2f13f 22 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  " is incremented
2f140 20 74 6f 20 66 6f 72 6d 20 74 68 65 0a 20 20 2a   to form the.  *
2f141 2a 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f  * termination co
2f142 6e 64 69 74 69 6f 6e 20 22 61 62 64 22 2e 0a 20  ndition "abd".. 
2f143 20 2a 2f 0a 20 20 69 66 28 20 70 57 43 2d 3e 6f   */.  if( pWC->o
2f144 70 3d 3d 54 4b 5f 41 4e 44 20 0a 20 20 20 26 26  p==TK_AND .   &&
2f145 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 70 50   isLikeOrGlob(pP
2f146 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 70 53  arse, pExpr, &pS
2f147 74 72 31 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65  tr1, &isComplete
2f148 2c 20 26 6e 6f 43 61 73 65 29 0a 20 20 29 7b 0a  , &noCase).  ){.
2f149 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b      Expr *pLeft;
2f14a 20 20 20 20 20 20 20 2f 2a 20 4c 48 53 20 6f 66         /* LHS of
2f14b 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65 72 61   LIKE/GLOB opera
2f14c 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
2f14d 2a 70 53 74 72 32 3b 20 20 20 20 20 20 20 2f 2a  *pStr2;       /*
2f14e 20 43 6f 70 79 20 6f 66 20 70 53 74 72 31 20 2d   Copy of pStr1 -
2f14f 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f   RHS of LIKE/GLO
2f150 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  B operator */.  
2f151 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72    Expr *pNewExpr
2f152 31 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65  1;.    Expr *pNe
2f153 77 45 78 70 72 32 3b 0a 20 20 20 20 69 6e 74 20  wExpr2;.    int 
2f154 69 64 78 4e 65 77 31 3b 0a 20 20 20 20 69 6e 74  idxNew1;.    int
2f155 20 69 64 78 4e 65 77 32 3b 0a 0a 20 20 20 20 70   idxNew2;..    p
2f156 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  Left = pExpr->x.
2f157 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70  pList->a[1].pExp
2f158 72 3b 0a 20 20 20 20 70 53 74 72 32 20 3d 20 73  r;.    pStr2 = s
2f159 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2f15a 2c 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20 20  , pStr1, 0);.   
2f15b 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2f15c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
2f15d 75 38 20 63 2c 20 2a 70 43 3b 20 20 20 20 20 20  u8 c, *pC;      
2f15e 20 2f 2a 20 4c 61 73 74 20 63 68 61 72 61 63 74   /* Last charact
2f15f 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  er before the fi
2f160 72 73 74 20 77 69 6c 64 63 61 72 64 20 2a 2f 0a  rst wildcard */.
2f161 20 20 20 20 20 20 70 43 20 3d 20 28 75 38 2a 29        pC = (u8*)
2f162 26 70 53 74 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e  &pStr2->u.zToken
2f163 5b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30  [sqlite3Strlen30
2f164 28 70 53 74 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pStr2->u.zToken
2f165 29 2d 31 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20  )-1];.      c = 
2f166 2a 70 43 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  *pC;.      if( n
2f167 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  oCase ){.       
2f168 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 20 69 73   /* The point is
2f169 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68   to increment th
2f16a 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
2f16b 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73   before the firs
2f16c 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c  t.        ** wil
2f16d 64 63 61 72 64 2e 20 20 42 75 74 20 69 66 20 77  dcard.  But if w
2f16e 65 20 69 6e 63 72 65 6d 65 6e 74 20 27 40 27 2c  e increment '@',
2f16f 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68 20   that will push 
2f170 69 74 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20  it into the.    
2f171 20 20 20 20 2a 2a 20 61 6c 70 68 61 62 65 74 69      ** alphabeti
2f172 63 20 72 61 6e 67 65 20 77 68 65 72 65 20 63 61  c range where ca
2f173 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77  se conversions w
2f174 69 6c 6c 20 6d 65 73 73 20 75 70 20 74 68 65 20  ill mess up the 
2f175 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 65 71  .        ** ineq
2f176 75 61 6c 69 74 79 2e 20 20 54 6f 20 61 76 6f 69  uality.  To avoi
2f177 64 20 74 68 69 73 2c 20 6d 61 6b 65 20 73 75 72  d this, make sur
2f178 65 20 74 6f 20 61 6c 73 6f 20 72 75 6e 20 74 68  e to also run th
2f179 65 20 66 75 6c 6c 0a 20 20 20 20 20 20 20 20 2a  e full.        *
2f17a 2a 20 4c 49 4b 45 20 6f 6e 20 61 6c 6c 20 63 61  * LIKE on all ca
2f17b 6e 64 69 64 61 74 65 20 65 78 70 72 65 73 73 69  ndidate expressi
2f17c 6f 6e 73 20 62 79 20 63 6c 65 61 72 69 6e 67 20  ons by clearing 
2f17d 74 68 65 20 69 73 43 6f 6d 70 6c 65 74 65 20 66  the isComplete f
2f17e 6c 61 67 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  lag.        */. 
2f17f 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 41         if( c=='A
2f180 27 2d 31 20 29 20 69 73 43 6f 6d 70 6c 65 74 65  '-1 ) isComplete
2f181 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 63   = 0;..        c
2f182 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   = sqlite3UpperT
2f183 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 20 20 20  oLower[c];.     
2f184 20 7d 0a 20 20 20 20 20 20 2a 70 43 20 3d 20 63   }.      *pC = c
2f185 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   + 1;.    }.    
2f186 70 4e 65 77 45 78 70 72 31 20 3d 20 73 71 6c 69  pNewExpr1 = sqli
2f187 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2f188 20 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65 33 45   TK_GE, sqlite3E
2f189 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c  xprDup(db,pLeft,
2f18a 30 29 2c 70 53 74 72 31 2c 30 29 3b 0a 20 20 20  0),pStr1,0);.   
2f18b 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 65   idxNew1 = where
2f18c 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2f18d 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 52  , pNewExpr1, TER
2f18e 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2f18f 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73  YNAMIC);.    tes
2f190 74 63 61 73 65 28 20 69 64 78 4e 65 77 31 3d 3d  tcase( idxNew1==
2f191 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61  0 );.    exprAna
2f192 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
2f193 69 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e  idxNew1);.    pN
2f194 65 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65  ewExpr2 = sqlite
2f195 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2f196 4b 5f 4c 54 2c 20 73 71 6c 69 74 65 33 45 78 70  K_LT, sqlite3Exp
2f197 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30 29  rDup(db,pLeft,0)
2f198 2c 70 53 74 72 32 2c 30 29 3b 0a 20 20 20 20 69  ,pStr2,0);.    i
2f199 64 78 4e 65 77 32 20 3d 20 77 68 65 72 65 43 6c  dxNew2 = whereCl
2f19a 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2f19b 70 4e 65 77 45 78 70 72 32 2c 20 54 45 52 4d 5f  pNewExpr2, TERM_
2f19c 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
2f19d 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73 74 63  AMIC);.    testc
2f19e 61 73 65 28 20 69 64 78 4e 65 77 32 3d 3d 30 20  ase( idxNew2==0 
2f19f 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  );.    exprAnaly
2f1a0 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
2f1a1 78 4e 65 77 32 29 3b 0a 20 20 20 20 70 54 65 72  xNew2);.    pTer
2f1a2 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2f1a3 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28 20 69 73  erm];.    if( is
2f1a4 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20 20  Complete ){.    
2f1a5 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 31    pWC->a[idxNew1
2f1a6 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
2f1a7 65 72 6d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e  erm;.      pWC->
2f1a8 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50 61 72 65  a[idxNew2].iPare
2f1a9 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
2f1aa 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2f1ab 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 2;.    }.  }
2f1ac 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2f1ad 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49  E_OMIT_LIKE_OPTI
2f1ae 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66  MIZATION */..#if
2f1af 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f1b0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2f1b1 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41 54 43  /* Add a WO_MATC
2f1b2 48 20 61 75 78 69 6c 69 61 72 79 20 74 65 72 6d  H auxiliary term
2f1b3 20 74 6f 20 74 68 65 20 63 6f 6e 73 74 72 61 69   to the constrai
2f1b4 6e 74 20 73 65 74 20 69 66 20 74 68 65 0a 20 20  nt set if the.  
2f1b5 2a 2a 20 63 75 72 72 65 6e 74 20 65 78 70 72 65  ** current expre
2f1b6 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2f1b7 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41  form:  column MA
2f1b8 54 43 48 20 65 78 70 72 2e 0a 20 20 2a 2a 20 54  TCH expr..  ** T
2f1b9 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
2f1ba 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 78  is used by the x
2f1bb 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
2f1bc 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72 74 75 61  s of.  ** virtua
2f1bd 6c 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 6e  l tables.  The n
2f1be 61 74 69 76 65 20 71 75 65 72 79 20 6f 70 74 69  ative query opti
2f1bf 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74 20 61  mizer does not a
2f1c0 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74 6f 20 64  ttempt.  ** to d
2f1c1 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 20  o anything with 
2f1c2 4d 41 54 43 48 20 66 75 6e 63 74 69 6f 6e 73 2e  MATCH functions.
2f1c3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 61  .  */.  if( isMa
2f1c4 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 45 78 70  tchOfColumn(pExp
2f1c5 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64  r) ){.    int id
2f1c6 78 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 20 2a  xNew;.    Expr *
2f1c7 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 0a  pRight, *pLeft;.
2f1c8 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2f1c9 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20 42 69 74  NewTerm;.    Bit
2f1ca 6d 61 73 6b 20 70 72 65 72 65 71 43 6f 6c 75 6d  mask prereqColum
2f1cb 6e 2c 20 70 72 65 72 65 71 45 78 70 72 3b 0a 0a  n, prereqExpr;..
2f1cc 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78      pRight = pEx
2f1cd 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30  pr->x.pList->a[0
2f1ce 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 4c 65  ].pExpr;.    pLe
2f1cf 66 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ft = pExpr->x.pL
2f1d0 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
2f1d1 0a 20 20 20 20 70 72 65 72 65 71 45 78 70 72 20  .    prereqExpr 
2f1d2 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2f1d3 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 69 67 68  (pMaskSet, pRigh
2f1d4 74 29 3b 0a 20 20 20 20 70 72 65 72 65 71 43 6f  t);.    prereqCo
2f1d5 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65  lumn = exprTable
2f1d6 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2f1d7 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
2f1d8 28 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72  (prereqExpr & pr
2f1d9 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29  ereqColumn)==0 )
2f1da 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  {.      Expr *pN
2f1db 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e  ewExpr;.      pN
2f1dc 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ewExpr = sqlite3
2f1dd 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2f1de 5f 4d 41 54 43 48 2c 20 0a 20 20 20 20 20 20 20  _MATCH, .       
2f1df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1e0 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65         0, sqlite
2f1e1 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52 69  3ExprDup(db, pRi
2f1e2 67 68 74 2c 20 30 29 2c 20 30 29 3b 0a 20 20 20  ght, 0), 0);.   
2f1e3 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72     idxNew = wher
2f1e4 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
2f1e5 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52  C, pNewExpr, TER
2f1e6 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2f1e7 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 74  YNAMIC);.      t
2f1e8 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d  estcase( idxNew=
2f1e9 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  =0 );.      pNew
2f1ea 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
2f1eb 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 70 4e  dxNew];.      pN
2f1ec 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  ewTerm->prereqRi
2f1ed 67 68 74 20 3d 20 70 72 65 72 65 71 45 78 70 72  ght = prereqExpr
2f1ee 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2f1ef 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70  ->leftCursor = p
2f1f0 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  Left->iTable;.  
2f1f1 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 75 2e      pNewTerm->u.
2f1f2 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65  leftColumn = pLe
2f1f3 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ft->iColumn;.   
2f1f4 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70     pNewTerm->eOp
2f1f5 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4d 41 54 43  erator = WO_MATC
2f1f6 48 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  H;.      pNewTer
2f1f7 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78  m->iParent = idx
2f1f8 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72  Term;.      pTer
2f1f9 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2f1fa 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 54 65 72  erm];.      pTer
2f1fb 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20  m->nChild = 1;. 
2f1fc 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c       pTerm->wtFl
2f1fd 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49  ags |= TERM_COPI
2f1fe 45 44 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  ED;.      pNewTe
2f1ff 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20  rm->prereqAll = 
2f200 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  pTerm->prereqAll
2f201 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2f202 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2f203 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
2f204 2f 0a 0a 20 20 2f 2a 20 50 72 65 76 65 6e 74 20  /..  /* Prevent 
2f205 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  ON clause terms 
2f206 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 66  of a LEFT JOIN f
2f207 72 6f 6d 20 62 65 69 6e 67 20 75 73 65 64 20 74  rom being used t
2f208 6f 20 64 72 69 76 65 0a 20 20 2a 2a 20 61 6e 20  o drive.  ** an 
2f209 69 6e 64 65 78 20 66 6f 72 20 74 61 62 6c 65 73  index for tables
2f20a 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
2f20b 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20  the join..  */. 
2f20c 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69   pTerm->prereqRi
2f20d 67 68 74 20 7c 3d 20 65 78 74 72 61 52 69 67 68  ght |= extraRigh
2f20e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t;.}../*.** Retu
2f20f 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79 20 6f  rn TRUE if any o
2f210 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2f211 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b 69 46  s in pList->a[iF
2f212 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69 6e  irst...] contain
2f213 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20  .** a reference 
2f214 74 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f 74 68  to any table oth
2f215 65 72 20 74 68 61 6e 20 74 68 65 20 69 42 61 73  er than the iBas
2f216 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  e table..*/.stat
2f217 69 63 20 69 6e 74 20 72 65 66 65 72 65 6e 63 65  ic int reference
2f218 73 4f 74 68 65 72 54 61 62 6c 65 73 28 0a 20 20  sOtherTables(.  
2f219 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
2f21a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61            /* Sea
2f21b 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 20  rch expressions 
2f21c 69 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f 0a 20  in ths list */. 
2f21d 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
2f21e 4d 61 73 6b 53 65 74 2c 20 20 20 2f 2a 20 4d 61  MaskSet,   /* Ma
2f21f 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65  pping from table
2f220 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a  s to bitmaps */.
2f221 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 20 20    int iFirst,   
2f222 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2f223 65 20 73 65 61 72 63 68 69 6e 67 20 77 69 74 68  e searching with
2f224 20 74 68 65 20 69 46 69 72 73 74 2d 74 68 20 65   the iFirst-th e
2f225 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69  xpression */.  i
2f226 6e 74 20 69 42 61 73 65 20 20 20 20 20 20 20 20  nt iBase        
2f227 20 20 20 20 20 20 20 20 20 2f 2a 20 49 67 6e 6f           /* Igno
2f228 72 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  re references to
2f229 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29   this table */.)
2f22a 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c 6c 6f  {.  Bitmask allo
2f22b 77 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b 28 70  wed = ~getMask(p
2f22c 4d 61 73 6b 53 65 74 2c 20 69 42 61 73 65 29 3b  MaskSet, iBase);
2f22d 0a 20 20 77 68 69 6c 65 28 20 69 46 69 72 73 74  .  while( iFirst
2f22e 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b  <pList->nExpr ){
2f22f 0a 20 20 20 20 69 66 28 20 28 65 78 70 72 54 61  .    if( (exprTa
2f230 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2f231 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72  t, pList->a[iFir
2f232 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61 6c 6c  st++].pExpr)&all
2f233 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20 20 20 20  owed)!=0 ){.    
2f234 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2f235 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
2f236 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.}.../*.** This
2f237 20 72 6f 75 74 69 6e 65 20 64 65 63 69 64 65 73   routine decides
2f238 20 69 66 20 70 49 64 78 20 63 61 6e 20 62 65 20   if pIdx can be 
2f239 75 73 65 64 20 74 6f 20 73 61 74 69 73 66 79 20  used to satisfy 
2f23a 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20  the ORDER BY.** 
2f23b 63 6c 61 75 73 65 2e 20 20 49 66 20 69 74 20 63  clause.  If it c
2f23c 61 6e 2c 20 69 74 20 72 65 74 75 72 6e 73 20 31  an, it returns 1
2f23d 2e 20 20 49 66 20 70 49 64 78 20 63 61 6e 6e 6f  .  If pIdx canno
2f23e 74 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a 2a  t satisfy the.**
2f23f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f240 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
2f241 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20  eturns 0..**.** 
2f242 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 4f  pOrderBy is an O
2f243 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66  RDER BY clause f
2f244 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74 61  rom a SELECT sta
2f245 74 65 6d 65 6e 74 2e 20 20 70 54 61 62 20 69 73  tement.  pTab is
2f246 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73   the.** left-mos
2f247 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  t table in the F
2f248 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
2f249 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54 20 73  at same SELECT s
2f24a 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a 20  tatement and.** 
2f24b 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 20  the table has a 
2f24c 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  cursor number of
2f24d 20 22 62 61 73 65 22 2e 20 20 70 49 64 78 20 69   "base".  pIdx i
2f24e 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54  s an index on pT
2f24f 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f 6c  ab..**.** nEqCol
2f250 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2f251 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 49 64  f columns of pId
2f252 78 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20  x that are used 
2f253 61 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63  as equality.** c
2f254 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41 6e 79  onstraints.  Any
2f255 20 6f 66 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e   of these column
2f256 73 20 6d 61 79 20 62 65 20 6d 69 73 73 69 6e 67  s may be missing
2f257 20 66 72 6f 6d 20 74 68 65 20 4f 52 44 45 52 20   from the ORDER 
2f258 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61 6e 64  BY.** clause and
2f259 20 74 68 65 20 6d 61 74 63 68 20 63 61 6e 20 73   the match can s
2f25a 74 69 6c 6c 20 62 65 20 61 20 73 75 63 63 65 73  till be a succes
2f25b 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65 72  s..**.** All ter
2f25c 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
2f25d 42 59 20 74 68 61 74 20 6d 61 74 63 68 20 61 67  BY that match ag
2f25e 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20  ainst the index 
2f25f 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 0a 2a  must be either.*
2f260 2a 20 41 53 43 20 6f 72 20 44 45 53 43 2e 20 20  * ASC or DESC.  
2f261 28 54 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52  (Terms of the OR
2f262 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 70 61  DER BY clause pa
2f263 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  st the end of a 
2f264 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65 78 20  UNIQUE.** index 
2f265 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 73  do not need to s
2f266 61 74 69 73 66 79 20 74 68 69 73 20 63 6f 6e 73  atisfy this cons
2f267 74 72 61 69 6e 74 2e 29 20 20 54 68 65 20 2a 70  traint.)  The *p
2f268 62 52 65 76 20 76 61 6c 75 65 20 69 73 0a 2a 2a  bRev value is.**
2f269 20 73 65 74 20 74 6f 20 31 20 69 66 20 74 68 65   set to 1 if the
2f26a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f26b 20 69 73 20 61 6c 6c 20 44 45 53 43 20 61 6e 64   is all DESC and
2f26c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 30 20   it is set to 0 
2f26d 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45 52 20  if.** the ORDER 
2f26e 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 6c 6c  BY clause is all
2f26f 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   ASC..*/.static 
2f270 69 6e 74 20 69 73 53 6f 72 74 69 6e 67 49 6e 64  int isSortingInd
2f271 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2f272 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
2f273 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2f274 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53   */.  WhereMaskS
2f275 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 2f 2a  et *pMaskSet, /*
2f276 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
2f277 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
2f278 72 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f  rs to bitmaps */
2f279 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
2f27a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f27b 65 20 69 6e 64 65 78 20 77 65 20 61 72 65 20 74  e index we are t
2f27c 65 73 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  esting */.  int 
2f27d 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  base,           
2f27e 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
2f27f 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
2f280 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20  le to be sorted 
2f281 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2f282 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20  OrderBy,     /* 
2f283 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2f284 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71  use */.  int nEq
2f285 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
2f286 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e   /* Number of in
2f287 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74 68  dex columns with
2f288 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   == constraints 
2f289 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20  */.  int *pbRev 
2f28a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f28b 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45  Set to 1 if ORDE
2f28c 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a  R BY is DESC */.
2f28d 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
2f28e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f28f 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2f290 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 73  nters */.  int s
2f291 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 20 20 20  ortOrder = 0;   
2f292 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 4f             /* XO
2f293 52 20 6f 66 20 69 6e 64 65 78 20 61 6e 64 20 4f  R of index and O
2f294 52 44 45 52 20 42 59 20 73 6f 72 74 20 64 69 72  RDER BY sort dir
2f295 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
2f296 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
2f297 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2f298 75 6d 62 65 72 20 6f 66 20 4f 52 44 45 52 20 42  umber of ORDER B
2f299 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72  Y terms */.  str
2f29a 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2f29b 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 2f 2a 20  m *pTerm;    /* 
2f29c 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52  A term of the OR
2f29d 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2f29e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2f29f 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
2f2a0 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2f2a1 21 3d 30 20 29 3b 0a 20 20 6e 54 65 72 6d 20 3d  !=0 );.  nTerm =
2f2a2 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
2f2a3 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72  ;.  assert( nTer
2f2a4 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 72 67  m>0 );..  /* Arg
2f2a5 75 6d 65 6e 74 20 70 49 64 78 20 6d 75 73 74 20  ument pIdx must 
2f2a6 65 69 74 68 65 72 20 70 6f 69 6e 74 20 74 6f 20  either point to 
2f2a7 61 20 27 72 65 61 6c 27 20 6e 61 6d 65 64 20 69  a 'real' named i
2f2a8 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2c 20  ndex structure, 
2f2a9 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 69 6e 64 65  .  ** or an inde
2f2aa 78 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f  x structure allo
2f2ab 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61  cated on the sta
2f2ac 63 6b 20 62 79 20 62 65 73 74 42 74 72 65 65 49  ck by bestBtreeI
2f2ad 6e 64 65 78 28 29 20 74 6f 0a 20 20 2a 2a 20 72  ndex() to.  ** r
2f2ae 65 70 72 65 73 65 6e 74 20 74 68 65 20 72 6f 77  epresent the row
2f2af 69 64 20 69 6e 64 65 78 20 74 68 61 74 20 69 73  id index that is
2f2b0 20 70 61 72 74 20 6f 66 20 65 76 65 72 79 20 74   part of every t
2f2b1 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65  able.  */.  asse
2f2b2 72 74 28 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  rt( pIdx->zName 
2f2b3 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  || (pIdx->nColum
2f2b4 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 61 69  n==1 && pIdx->ai
2f2b5 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d 31 29 20 29  Column[0]==-1) )
2f2b6 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65  ;..  /* Match te
2f2b7 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
2f2b8 20 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e   BY clause again
2f2b9 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20  st columns of.  
2f2ba 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20  ** the index..  
2f2bb 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  **.  ** Note tha
2f2bc 74 20 69 6e 64 69 63 65 73 20 68 61 76 65 20 70  t indices have p
2f2bd 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67  Idx->nColumn reg
2f2be 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75  ular columns plu
2f2bf 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74  s.  ** one addit
2f2c0 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e  ional column con
2f2c1 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69  taining the rowi
2f2c2 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f  d.  The rowid co
2f2c3 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65  lumn.  ** of the
2f2c4 20 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61   index is also a
2f2c5 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20  llowed to match 
2f2c6 61 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45  against the ORDE
2f2c7 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65  R BY.  ** clause
2f2c8 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a  ..  */.  for(i=j
2f2c9 3d 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65 72  =0, pTerm=pOrder
2f2ca 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26  By->a; j<nTerm &
2f2cb 26 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75  & i<=pIdx->nColu
2f2cc 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  mn; i++){.    Ex
2f2cd 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
2f2ce 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2f2cf 6f 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  on of the ORDER 
2f2d0 42 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20  BY pTerm */.    
2f2d1 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
2f2d2 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74     /* The collat
2f2d3 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ing sequence of 
2f2d4 70 45 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74  pExpr */.    int
2f2d5 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20   termSortOrder; 
2f2d6 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f  /* Sort order fo
2f2d7 72 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20  r this term */. 
2f2d8 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
2f2d9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74        /* The i-t
2f2da 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
2f2db 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 72  index.  -1 for r
2f2dc 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  owid */.    int 
2f2dd 69 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 2f  iSortOrder;    /
2f2de 2a 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30 20  * 1 for DESC, 0 
2f2df 66 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20 69  for ASC on the i
2f2e0 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a  -th index term *
2f2e1 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
2f2e2 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65   *zColl; /* Name
2f2e3 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
2f2e4 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 69  g sequence for i
2f2e5 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20 2a  -th index term *
2f2e6 2f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70  /..    pExpr = p
2f2e7 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
2f2e8 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2f2e9 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78  TK_COLUMN || pEx
2f2ea 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73 65  pr->iTable!=base
2f2eb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e   ){.      /* Can
2f2ec 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65   not use an inde
2f2ed 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69  x sort on anythi
2f2ee 6e 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ng that is not a
2f2ef 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20   column in the. 
2f2f0 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73       ** left-mos
2f2f1 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 46  t table of the F
2f2f2 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
2f2f3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2f2f4 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
2f2f5 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28  ite3ExprCollSeq(
2f2f6 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a  pParse, pExpr);.
2f2f7 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
2f2f8 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20  {.      pColl = 
2f2f9 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
2f2fa 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64     }.    if( pId
2f2fb 78 2d 3e 7a 4e 61 6d 65 20 26 26 20 69 3c 70 49  x->zName && i<pI
2f2fc 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  dx->nColumn ){. 
2f2fd 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70       iColumn = p
2f2fe 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
2f2ff 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
2f300 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c  umn==pIdx->pTabl
2f301 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  e->iPKey ){.    
2f302 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31      iColumn = -1
2f303 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f304 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64  iSortOrder = pId
2f305 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
2f306 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
2f307 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b  pIdx->azColl[i];
2f308 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2f309 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a    iColumn = -1;.
2f30a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72        iSortOrder
2f30b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43 6f 6c   = 0;.      zCol
2f30c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  l = pColl->zName
2f30d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2f30e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  pExpr->iColumn!=
2f30f 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69 74  iColumn || sqlit
2f310 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d  e3StrICmp(pColl-
2f311 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29  >zName, zColl) )
2f312 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20  {.      /* Term 
2f313 6a 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  j of the ORDER B
2f314 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  Y clause does no
2f315 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69  t match column i
2f316 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f   of the index */
2f317 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e 45 71  .      if( i<nEq
2f318 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Col ){.        /
2f319 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 6f  * If an index co
2f31a 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 63 6f 6e  lumn that is con
2f31b 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20 66  strained by == f
2f31c 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e  ails to match an
2f31d 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45  .        ** ORDE
2f31e 52 20 42 59 20 74 65 72 6d 2c 20 74 68 61 74 20  R BY term, that 
2f31f 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69 67 6e  is OK.  Just ign
2f320 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20  ore that column 
2f321 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20  of the index.   
2f322 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2f323 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2f324 7d 65 6c 73 65 20 69 66 28 20 69 3d 3d 70 49 64  }else if( i==pId
2f325 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  x->nColumn ){.  
2f326 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 63        /* Index c
2f327 6f 6c 75 6d 6e 20 69 20 69 73 20 74 68 65 20 72  olumn i is the r
2f328 6f 77 69 64 2e 20 20 41 6c 6c 20 6f 74 68 65 72  owid.  All other
2f329 20 74 65 72 6d 73 20 6d 61 74 63 68 2e 20 2a 2f   terms match. */
2f32a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2f32b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2f32c 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e       /* If an in
2f32d 64 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69 6c 73  dex column fails
2f32e 20 74 6f 20 6d 61 74 63 68 20 61 6e 64 20 69 73   to match and is
2f32f 20 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e 65 64   not constrained
2f330 20 62 79 20 3d 3d 0a 20 20 20 20 20 20 20 20 2a   by ==.        *
2f331 2a 20 74 68 65 6e 20 74 68 65 20 69 6e 64 65 78  * then the index
2f332 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79 20   cannot satisfy 
2f333 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6f 6e  the ORDER BY con
2f334 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20  straint..       
2f335 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
2f336 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
2f337 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2f338 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
2f339 21 3d 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3d 3d  !=0 || iColumn==
2f33a 2d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  -1 );.    assert
2f33b 28 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64  ( pTerm->sortOrd
2f33c 65 72 3d 3d 30 20 7c 7c 20 70 54 65 72 6d 2d 3e  er==0 || pTerm->
2f33d 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a  sortOrder==1 );.
2f33e 20 20 20 20 61 73 73 65 72 74 28 20 69 53 6f 72      assert( iSor
2f33f 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 69 53 6f  tOrder==0 || iSo
2f340 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20  rtOrder==1 );.  
2f341 20 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 20    termSortOrder 
2f342 3d 20 69 53 6f 72 74 4f 72 64 65 72 20 5e 20 70  = iSortOrder ^ p
2f343 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Term->sortOrder;
2f344 0a 20 20 20 20 69 66 28 20 69 3e 6e 45 71 43 6f  .    if( i>nEqCo
2f345 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 74  l ){.      if( t
2f346 65 72 6d 53 6f 72 74 4f 72 64 65 72 21 3d 73 6f  ermSortOrder!=so
2f347 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20  rtOrder ){.     
2f348 20 20 20 2f 2a 20 49 6e 64 69 63 65 73 20 63 61     /* Indices ca
2f349 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69  n only be used i
2f34a 66 20 61 6c 6c 20 4f 52 44 45 52 20 42 59 20 74  f all ORDER BY t
2f34b 65 72 6d 73 20 70 61 73 74 20 74 68 65 0a 20 20  erms past the.  
2f34c 20 20 20 20 20 20 2a 2a 20 65 71 75 61 6c 69 74        ** equalit
2f34d 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  y constraints ar
2f34e 65 20 61 6c 6c 20 65 69 74 68 65 72 20 44 45 53  e all either DES
2f34f 43 20 6f 72 20 41 53 43 2e 20 2a 2f 0a 20 20 20  C or ASC. */.   
2f350 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2f351 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2f352 7b 0a 20 20 20 20 20 20 73 6f 72 74 4f 72 64 65  {.      sortOrde
2f353 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f 72 64 65  r = termSortOrde
2f354 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b  r;.    }.    j++
2f355 3b 0a 20 20 20 20 70 54 65 72 6d 2b 2b 3b 0a 20  ;.    pTerm++;. 
2f356 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30     if( iColumn<0
2f357 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f   && !referencesO
2f358 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65  therTables(pOrde
2f359 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a  rBy, pMaskSet, j
2f35a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20 20  , base) ){.     
2f35b 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78   /* If the index
2f35c 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65  ed column is the
2f35d 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 6e 64   primary key and
2f35e 20 65 76 65 72 79 74 68 69 6e 67 20 6d 61 74 63   everything matc
2f35f 68 65 73 0a 20 20 20 20 20 20 2a 2a 20 73 6f 20  hes.      ** so 
2f360 66 61 72 20 61 6e 64 20 6e 6f 6e 65 20 6f 66 20  far and none of 
2f361 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  the ORDER BY ter
2f362 6d 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 20  ms to the right 
2f363 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65 72 0a  reference other.
2f364 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20        ** tables 
2f365 69 6e 20 74 68 65 20 6a 6f 69 6e 2c 20 74 68 65  in the join, the
2f366 6e 20 77 65 20 61 72 65 20 61 73 73 75 72 65 64  n we are assured
2f367 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20   that the index 
2f368 63 61 6e 20 62 65 20 75 73 65 64 20 0a 20 20 20  can be used .   
2f369 20 20 20 2a 2a 20 74 6f 20 73 6f 72 74 20 62 65     ** to sort be
2f36a 63 61 75 73 65 20 74 68 65 20 70 72 69 6d 61 72  cause the primar
2f36b 79 20 6b 65 79 20 69 73 20 75 6e 69 71 75 65 20  y key is unique 
2f36c 61 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f 66 20 74  and so none of t
2f36d 68 65 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a  he other.      *
2f36e 2a 20 63 6f 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d  * columns will m
2f36f 61 6b 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e  ake any differen
2f370 63 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ce.      */.    
2f371 20 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20    j = nTerm;.   
2f372 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62 52 65 76   }.  }..  *pbRev
2f373 20 3d 20 73 6f 72 74 4f 72 64 65 72 21 3d 30 3b   = sortOrder!=0;
2f374 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65 72 6d 20  .  if( j>=nTerm 
2f375 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65  ){.    /* All te
2f376 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
2f377 20 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 63   BY clause are c
2f378 6f 76 65 72 65 64 20 62 79 20 74 68 69 73 20 69  overed by this i
2f379 6e 64 65 78 20 73 6f 0a 20 20 20 20 2a 2a 20 74  ndex so.    ** t
2f37a 68 69 73 20 69 6e 64 65 78 20 63 61 6e 20 62 65  his index can be
2f37b 20 75 73 65 64 20 66 6f 72 20 73 6f 72 74 69 6e   used for sortin
2f37c 67 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  g. */.    return
2f37d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49   1;.  }.  if( pI
2f37e 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
2f37f 4e 6f 6e 65 20 26 26 20 69 3d 3d 70 49 64 78 2d  None && i==pIdx-
2f380 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 26  >nColumn.      &
2f381 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68  & !referencesOth
2f382 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42  erTables(pOrderB
2f383 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20  y, pMaskSet, j, 
2f384 62 61 73 65 29 20 29 7b 0a 20 20 20 20 2f 2a 20  base) ){.    /* 
2f385 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 69  All terms of thi
2f386 73 20 69 6e 64 65 78 20 6d 61 74 63 68 20 73 6f  s index match so
2f387 6d 65 20 70 72 65 66 69 78 20 6f 66 20 74 68 65  me prefix of the
2f388 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f389 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  .    ** and the 
2f38a 69 6e 64 65 78 20 69 73 20 55 4e 49 51 55 45 20  index is UNIQUE 
2f38b 61 6e 64 20 6e 6f 20 74 65 72 6d 73 20 6f 6e 20  and no terms on 
2f38c 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  the tail of the 
2f38d 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20  ORDER BY.    ** 
2f38e 63 6c 61 75 73 65 20 72 65 66 65 72 65 6e 63 65  clause reference
2f38f 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20 69 6e   other tables in
2f390 20 61 20 6a 6f 69 6e 2e 20 20 49 66 20 74 68 69   a join.  If thi
2f391 73 20 69 73 20 61 6c 6c 20 74 72 75 65 20 74 68  s is all true th
2f392 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 72  en.    ** the or
2f393 64 65 72 20 62 79 20 63 6c 61 75 73 65 20 69 73  der by clause is
2f394 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 20 2a 2f   superfluous. */
2f395 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2f396 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
2f397 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20  ../*.** Prepare 
2f398 61 20 63 72 75 64 65 20 65 73 74 69 6d 61 74 65  a crude estimate
2f399 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68   of the logarith
2f39a 6d 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76  m of the input v
2f39b 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20 72 65 73  alue..** The res
2f39c 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74 20 62 65  ults need not be
2f39d 20 65 78 61 63 74 2e 20 20 54 68 69 73 20 69 73   exact.  This is
2f39e 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 65   only used for e
2f39f 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20 74 68 65  stimating.** the
2f3a0 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f 66 20 70   total cost of p
2f3a1 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65 72 61 74  erforming operat
2f3a2 69 6f 6e 73 20 77 69 74 68 20 4f 28 6c 6f 67 4e  ions with O(logN
2f3a3 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29 0a 2a 2a  ) or O(NlogN).**
2f3a4 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20 20 42 65   complexity.  Be
2f3a5 63 61 75 73 65 20 4e 20 69 73 20 6a 75 73 74 20  cause N is just 
2f3a6 61 20 67 75 65 73 73 2c 20 69 74 20 69 73 20 6e  a guess, it is n
2f3a7 6f 20 67 72 65 61 74 20 74 72 61 67 65 64 79 20  o great tragedy 
2f3a8 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73 20 61 20  if.** logN is a 
2f3a9 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a 2f 0a 73  little off..*/.s
2f3aa 74 61 74 69 63 20 64 6f 75 62 6c 65 20 65 73 74  tatic double est
2f3ab 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29 7b 0a 20  Log(double N){. 
2f3ac 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20 3d 20 31   double logN = 1
2f3ad 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20 3d 20 31  ;.  double x = 1
2f3ae 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 78 20  0;.  while( N>x 
2f3af 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b 3d 20 31  ){.    logN += 1
2f3b0 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30 3b 0a 20  ;.    x *= 10;. 
2f3b1 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c 6f 67 4e   }.  return logN
2f3b2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 72  ;.}../*.** Two r
2f3b3 6f 75 74 69 6e 65 73 20 66 6f 72 20 70 72 69 6e  outines for prin
2f3b4 74 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ting the content
2f3b5 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 69   of an sqlite3_i
2f3b6 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 73 74 72  ndex_info.** str
2f3b7 75 63 74 75 72 65 2e 20 20 55 73 65 64 20 66 6f  ucture.  Used fo
2f3b8 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
2f3b9 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 49  bugging only.  I
2f3ba 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20 53 51 4c  f neither.** SQL
2f3bb 49 54 45 5f 54 45 53 54 20 6f 72 20 53 51 4c 49  ITE_TEST or SQLI
2f3bc 54 45 5f 44 45 42 55 47 20 61 72 65 20 64 65 66  TE_DEBUG are def
2f3bd 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 73 65  ined, then these
2f3be 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65   routines.** are
2f3bf 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20   no-ops..*/.#if 
2f3c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2f3c1 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2f3c2 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
2f3c3 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74  LITE_DEBUG).stat
2f3c4 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44  ic void TRACE_ID
2f3c5 58 5f 49 4e 50 55 54 53 28 73 71 6c 69 74 65 33  X_INPUTS(sqlite3
2f3c6 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b  _index_info *p){
2f3c7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2f3c8 21 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61  !sqlite3WhereTra
2f3c9 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  ce ) return;.  f
2f3ca 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
2f3cb 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a  nstraint; i++){.
2f3cc 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2f3cd 50 72 69 6e 74 66 28 22 20 20 63 6f 6e 73 74 72  Printf("  constr
2f3ce 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64  aint[%d]: col=%d
2f3cf 20 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d 25 64   termid=%d op=%d
2f3d0 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22 2c 0a   usabled=%d\n",.
2f3d1 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20         i,.      
2f3d2 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b   p->aConstraint[
2f3d3 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  i].iColumn,.    
2f3d4 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2f3d5 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74  t[i].iTermOffset
2f3d6 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2f3d7 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70 2c 0a 20  straint[i].op,. 
2f3d8 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
2f3d9 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 29 3b  aint[i].usable);
2f3da 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
2f3db 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79 3b 20 69  i<p->nOrderBy; i
2f3dc 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
2f3dd 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 6f  DebugPrintf("  o
2f3de 72 64 65 72 62 79 5b 25 64 5d 3a 20 63 6f 6c 3d  rderby[%d]: col=
2f3df 25 64 20 64 65 73 63 3d 25 64 5c 6e 22 2c 0a 20  %d desc=%d\n",. 
2f3e0 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
2f3e1 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 69  p->aOrderBy[i].i
2f3e2 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70  Column,.       p
2f3e3 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65  ->aOrderBy[i].de
2f3e4 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  sc);.  }.}.stati
2f3e5 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58  c void TRACE_IDX
2f3e6 5f 4f 55 54 50 55 54 53 28 73 71 6c 69 74 65 33  _OUTPUTS(sqlite3
2f3e7 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b  _index_info *p){
2f3e8 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
2f3e9 21 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61  !sqlite3WhereTra
2f3ea 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  ce ) return;.  f
2f3eb 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
2f3ec 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a  nstraint; i++){.
2f3ed 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2f3ee 50 72 69 6e 74 66 28 22 20 20 75 73 61 67 65 5b  Printf("  usage[
2f3ef 25 64 5d 3a 20 61 72 67 76 49 64 78 3d 25 64 20  %d]: argvIdx=%d 
2f3f0 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20 20 20 20  omit=%d\n",.    
2f3f1 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e     i,.       p->
2f3f2 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2f3f3 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 2c 0a 20  [i].argvIndex,. 
2f3f4 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
2f3f5 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69  aintUsage[i].omi
2f3f6 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  t);.  }.  sqlite
2f3f7 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20  3DebugPrintf("  
2f3f8 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c 20 70 2d  idxNum=%d\n", p-
2f3f9 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73 71 6c 69  >idxNum);.  sqli
2f3fa 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2f3fb 20 20 69 64 78 53 74 72 3d 25 73 5c 6e 22 2c 20    idxStr=%s\n", 
2f3fc 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 73 71  p->idxStr);.  sq
2f3fd 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2f3fe 28 22 20 20 6f 72 64 65 72 42 79 43 6f 6e 73 75  ("  orderByConsu
2f3ff 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d 3e 6f 72  med=%d\n", p->or
2f400 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 29 3b 0a  derByConsumed);.
2f401 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2f402 69 6e 74 66 28 22 20 20 65 73 74 69 6d 61 74 65  intf("  estimate
2f403 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e  dCost=%g\n", p->
2f404 65 73 74 69 6d 61 74 65 64 43 6f 73 74 29 3b 0a  estimatedCost);.
2f405 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
2f406 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53  TRACE_IDX_INPUTS
2f407 28 41 29 0a 23 64 65 66 69 6e 65 20 54 52 41 43  (A).#define TRAC
2f408 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 41 29  E_IDX_OUTPUTS(A)
2f409 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
2f40a 52 65 71 75 69 72 65 64 20 62 65 63 61 75 73 65  Required because
2f40b 20 62 65 73 74 49 6e 64 65 78 28 29 20 69 73 20   bestIndex() is 
2f40c 63 61 6c 6c 65 64 20 62 79 20 62 65 73 74 4f 72  called by bestOr
2f40d 43 6c 61 75 73 65 49 6e 64 65 78 28 29 20 0a 2a  ClauseIndex() .*
2f40e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65  /.static void be
2f40f 73 74 49 6e 64 65 78 28 0a 20 20 20 20 50 61 72  stIndex(.    Par
2f410 73 65 2a 2c 20 57 68 65 72 65 43 6c 61 75 73 65  se*, WhereClause
2f411 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  *, struct SrcLis
2f412 74 5f 69 74 65 6d 2a 2c 20 42 69 74 6d 61 73 6b  t_item*, Bitmask
2f413 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 57 68 65  , ExprList*, Whe
2f414 72 65 43 6f 73 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  reCost*);../*.**
2f415 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74   This routine at
2f416 74 65 6d 70 74 73 20 74 6f 20 66 69 6e 64 20 61  tempts to find a
2f417 6e 20 73 63 61 6e 6e 69 6e 67 20 73 74 72 61 74  n scanning strat
2f418 65 67 79 20 74 68 61 74 20 63 61 6e 20 62 65 20  egy that can be 
2f419 75 73 65 64 20 0a 2a 2a 20 74 6f 20 6f 70 74 69  used .** to opti
2f41a 6d 69 7a 65 20 61 6e 20 27 4f 52 27 20 65 78 70  mize an 'OR' exp
2f41b 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
2f41c 70 61 72 74 20 6f 66 20 61 20 57 48 45 52 45 20  part of a WHERE 
2f41d 63 6c 61 75 73 65 2e 20 0a 2a 2a 0a 2a 2a 20 54  clause. .**.** T
2f41e 68 65 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61  he table associa
2f41f 74 65 64 20 77 69 74 68 20 46 52 4f 4d 20 63 6c  ted with FROM cl
2f420 61 75 73 65 20 74 65 72 6d 20 70 53 72 63 20 6d  ause term pSrc m
2f421 61 79 20 62 65 20 65 69 74 68 65 72 20 61 0a 2a  ay be either a.*
2f422 2a 20 72 65 67 75 6c 61 72 20 42 2d 54 72 65 65  * regular B-Tree
2f423 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 72 74   table or a virt
2f424 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ual table..*/.st
2f425 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 4f 72  atic void bestOr
2f426 43 6c 61 75 73 65 49 6e 64 65 78 28 0a 20 20 50  ClauseIndex(.  P
2f427 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2f428 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f429 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
2f42a 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  t */.  WhereClau
2f42b 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
2f42c 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2f42d 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
2f42e 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2f42f 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 46  *pSrc,  /* The F
2f430 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ROM clause term 
2f431 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42  to search */.  B
2f432 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c  itmask notReady,
2f433 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2f434 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68  sk of cursors th
2f435 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  at are not avail
2f436 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
2f437 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2f438 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44        /* The ORD
2f439 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2f43a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f    WhereCost *pCo
2f43b 73 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  st            /*
2f43c 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65   Lowest cost que
2f43d 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69  ry plan */.){.#i
2f43e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f43f 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2f440 4e 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43  N.  const int iC
2f441 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  ur = pSrc->iCurs
2f442 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or;   /* The cur
2f443 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  sor of the table
2f444 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20   to be accessed 
2f445 2a 2f 0a 20 20 63 6f 6e 73 74 20 42 69 74 6d 61  */.  const Bitma
2f446 73 6b 20 6d 61 73 6b 53 72 63 20 3d 20 67 65 74  sk maskSrc = get
2f447 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53  Mask(pWC->pMaskS
2f448 65 74 2c 20 69 43 75 72 29 3b 20 20 2f 2a 20 42  et, iCur);  /* B
2f449 69 74 6d 61 73 6b 20 66 6f 72 20 70 53 72 63 20  itmask for pSrc 
2f44a 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2f44b 20 63 6f 6e 73 74 20 70 57 43 45 6e 64 20 3d 20   const pWCEnd = 
2f44c 26 70 57 43 2d 3e 61 5b 70 57 43 2d 3e 6e 54 65  &pWC->a[pWC->nTe
2f44d 72 6d 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 45  rm];        /* E
2f44e 6e 64 20 6f 66 20 70 57 43 2d 3e 61 5b 5d 20 2a  nd of pWC->a[] *
2f44f 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
2f450 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
2f451 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
2f452 65 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  e term of the WH
2f453 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 0a 20  ERE clause */.. 
2f454 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 57   /* Search the W
2f455 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2f456 73 20 66 6f 72 20 61 20 75 73 61 62 6c 65 20 57  s for a usable W
2f457 4f 5f 4f 52 20 74 65 72 6d 2e 20 2a 2f 0a 20 20  O_OR term. */.  
2f458 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61  for(pTerm=pWC->a
2f459 3b 20 70 54 65 72 6d 3c 70 57 43 45 6e 64 3b 20  ; pTerm<pWCEnd; 
2f45a 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pTerm++){.    if
2f45b 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2f45c 6f 72 3d 3d 57 4f 5f 4f 52 20 0a 20 20 20 20 20  or==WO_OR .     
2f45d 26 26 20 28 28 70 54 65 72 6d 2d 3e 70 72 65 72  && ((pTerm->prer
2f45e 65 71 41 6c 6c 20 26 20 7e 6d 61 73 6b 53 72 63  eqAll & ~maskSrc
2f45f 29 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30  ) & notReady)==0
2f460 0a 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d  .     && (pTerm-
2f461 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65  >u.pOrInfo->inde
2f462 78 61 62 6c 65 20 26 20 6d 61 73 6b 53 72 63 29  xable & maskSrc)
2f463 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  !=0 .    ){.    
2f464 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 20    WhereClause * 
2f465 63 6f 6e 73 74 20 70 4f 72 57 43 20 3d 20 26 70  const pOrWC = &p
2f466 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d  Term->u.pOrInfo-
2f467 3e 77 63 3b 0a 20 20 20 20 20 20 57 68 65 72 65  >wc;.      Where
2f468 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 4f 72  Term * const pOr
2f469 57 43 45 6e 64 20 3d 20 26 70 4f 72 57 43 2d 3e  WCEnd = &pOrWC->
2f46a 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 5d 3b  a[pOrWC->nTerm];
2f46b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  .      WhereTerm
2f46c 20 2a 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20   *pOrTerm;.     
2f46d 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 57 48 45   int flags = WHE
2f46e 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20  RE_MULTI_OR;.   
2f46f 20 20 20 64 6f 75 62 6c 65 20 72 54 6f 74 61 6c     double rTotal
2f470 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 75 62   = 0;.      doub
2f471 6c 65 20 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 20  le nRow = 0;.   
2f472 20 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 20     Bitmask used 
2f473 3d 20 30 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28  = 0;..      for(
2f474 70 4f 72 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61  pOrTerm=pOrWC->a
2f475 3b 20 70 4f 72 54 65 72 6d 3c 70 4f 72 57 43 45  ; pOrTerm<pOrWCE
2f476 6e 64 3b 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a  nd; pOrTerm++){.
2f477 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6f 73          WhereCos
2f478 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a 20 20 20  t sTermCost;.   
2f479 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2f47a 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e 64 65  ("... Multi-inde
2f47b 78 20 4f 52 20 74 65 73 74 69 6e 67 20 66 6f 72  x OR testing for
2f47c 20 74 65 72 6d 20 25 64 20 6f 66 20 25 64 2e 2e   term %d of %d..
2f47d 2e 2e 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  ..\n", .        
2f47e 20 20 28 70 4f 72 54 65 72 6d 20 2d 20 70 4f 72    (pOrTerm - pOr
2f47f 57 43 2d 3e 61 29 2c 20 28 70 54 65 72 6d 20 2d  WC->a), (pTerm -
2f480 20 70 57 43 2d 3e 61 29 0a 20 20 20 20 20 20 20   pWC->a).       
2f481 20 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   ));.        if(
2f482 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2f483 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20  tor==WO_AND ){. 
2f484 20 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c           WhereCl
2f485 61 75 73 65 20 2a 70 41 6e 64 57 43 20 3d 20 26  ause *pAndWC = &
2f486 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49  pOrTerm->u.pAndI
2f487 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20  nfo->wc;.       
2f488 20 20 20 62 65 73 74 49 6e 64 65 78 28 70 50 61     bestIndex(pPa
2f489 72 73 65 2c 20 70 41 6e 64 57 43 2c 20 70 53 72  rse, pAndWC, pSr
2f48a 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20  c, notReady, 0, 
2f48b 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20  &sTermCost);.   
2f48c 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2f48d 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73  OrTerm->leftCurs
2f48e 6f 72 3d 3d 69 43 75 72 20 29 7b 0a 20 20 20 20  or==iCur ){.    
2f48f 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73        WhereClaus
2f490 65 20 74 65 6d 70 57 43 3b 0a 20 20 20 20 20 20  e tempWC;.      
2f491 20 20 20 20 74 65 6d 70 57 43 2e 70 50 61 72 73      tempWC.pPars
2f492 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2f493 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57  .          tempW
2f494 43 2e 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43  C.pMaskSet = pWC
2f495 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 20 20  ->pMaskSet;.    
2f496 20 20 20 20 20 20 74 65 6d 70 57 43 2e 6f 70 20        tempWC.op 
2f497 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = TK_AND;.      
2f498 20 20 20 20 74 65 6d 70 57 43 2e 61 20 3d 20 70      tempWC.a = p
2f499 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20  OrTerm;.        
2f49a 20 20 74 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d    tempWC.nTerm =
2f49b 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65   1;.          be
2f49c 73 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  stIndex(pParse, 
2f49d 26 74 65 6d 70 57 43 2c 20 70 53 72 63 2c 20 6e  &tempWC, pSrc, n
2f49e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26 73 54 65  otReady, 0, &sTe
2f49f 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20  rmCost);.       
2f4a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2f4a1 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2f4a2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 54      }.        rT
2f4a3 6f 74 61 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73  otal += sTermCos
2f4a4 74 2e 72 43 6f 73 74 3b 0a 20 20 20 20 20 20 20  t.rCost;.       
2f4a5 20 6e 52 6f 77 20 2b 3d 20 73 54 65 72 6d 43 6f   nRow += sTermCo
2f4a6 73 74 2e 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20  st.nRow;.       
2f4a7 20 75 73 65 64 20 7c 3d 20 73 54 65 72 6d 43 6f   used |= sTermCo
2f4a8 73 74 2e 75 73 65 64 3b 0a 20 20 20 20 20 20 20  st.used;.       
2f4a9 20 69 66 28 20 72 54 6f 74 61 6c 3e 3d 70 43 6f   if( rTotal>=pCo
2f4aa 73 74 2d 3e 72 43 6f 73 74 20 29 20 62 72 65 61  st->rCost ) brea
2f4ab 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  k;.      }..    
2f4ac 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2f4ad 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2f4ae 75 73 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68  use, increase th
2f4af 65 20 73 63 61 6e 20 63 6f 73 74 20 74 6f 20 61  e scan cost to a
2f4b0 63 63 6f 75 6e 74 20 0a 20 20 20 20 20 20 2a 2a  ccount .      **
2f4b1 20 66 6f 72 20 74 68 65 20 63 6f 73 74 20 6f 66   for the cost of
2f4b2 20 74 68 65 20 73 6f 72 74 2e 20 2a 2f 0a 20 20   the sort. */.  
2f4b3 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
2f4b4 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !=0 ){.        r
2f4b5 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73  Total += nRow*es
2f4b6 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20  tLog(nRow);.    
2f4b7 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2f4b8 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63  "... sorting inc
2f4b9 72 65 61 73 65 73 20 4f 52 20 63 6f 73 74 20 74  reases OR cost t
2f4ba 6f 20 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61  o %.9g\n", rTota
2f4bb 6c 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  l));.      }..  
2f4bc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
2f4bd 73 74 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 75  st of scanning u
2f4be 73 69 6e 67 20 74 68 69 73 20 4f 52 20 74 65 72  sing this OR ter
2f4bf 6d 20 66 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69  m for optimizati
2f4c0 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c  on is.      ** l
2f4c1 65 73 73 20 74 68 61 6e 20 74 68 65 20 63 75 72  ess than the cur
2f4c2 72 65 6e 74 20 63 6f 73 74 20 73 74 6f 72 65 64  rent cost stored
2f4c3 20 69 6e 20 70 43 6f 73 74 2c 20 72 65 70 6c 61   in pCost, repla
2f4c4 63 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a  ce the contents.
2f4c5 20 20 20 20 20 20 2a 2a 20 6f 66 20 70 43 6f 73        ** of pCos
2f4c6 74 2e 20 2a 2f 0a 20 20 20 20 20 20 57 48 45 52  t. */.      WHER
2f4c7 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 6d 75 6c  ETRACE(("... mul
2f4c8 74 69 2d 69 6e 64 65 78 20 4f 52 20 63 6f 73 74  ti-index OR cost
2f4c9 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e 39 67 5c  =%.9g nrow=%.9g\
2f4ca 6e 22 2c 20 72 54 6f 74 61 6c 2c 20 6e 52 6f 77  n", rTotal, nRow
2f4cb 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 54  ));.      if( rT
2f4cc 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e 72 43 6f 73  otal<pCost->rCos
2f4cd 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  t ){.        pCo
2f4ce 73 74 2d 3e 72 43 6f 73 74 20 3d 20 72 54 6f 74  st->rCost = rTot
2f4cf 61 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73  al;.        pCos
2f4d0 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a  t->nRow = nRow;.
2f4d1 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75          pCost->u
2f4d2 73 65 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20  sed = used;.    
2f4d3 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e      pCost->plan.
2f4d4 77 73 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b  wsFlags = flags;
2f4d5 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  .        pCost->
2f4d6 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d 20 70  plan.u.pTerm = p
2f4d7 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Term;.      }.  
2f4d8 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
2f4d9 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  * SQLITE_OMIT_OR
2f4da 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
2f4db 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2f4dc 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2f4dd 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ABLE./*.** Alloc
2f4de 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65  ate and populate
2f4df 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65   an sqlite3_inde
2f4e0 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2f4e1 2e 20 49 74 20 69 73 20 74 68 65 20 0a 2a 2a 20  . It is the .** 
2f4e2 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f  responsibility o
2f4e3 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20  f the caller to 
2f4e4 65 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61  eventually relea
2f4e5 73 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  se the structure
2f4e6 0a 2a 2a 20 62 79 20 70 61 73 73 69 6e 67 20 74  .** by passing t
2f4e7 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72  he pointer retur
2f4e8 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ned by this func
2f4e9 74 69 6f 6e 20 74 6f 20 73 71 6c 69 74 65 33 5f  tion to sqlite3_
2f4ea 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  free()..*/.stati
2f4eb 63 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  c sqlite3_index_
2f4ec 69 6e 66 6f 20 2a 61 6c 6c 6f 63 61 74 65 49 6e  info *allocateIn
2f4ed 64 65 78 49 6e 66 6f 28 0a 20 20 50 61 72 73 65  dexInfo(.  Parse
2f4ee 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 57 68 65   *pParse, .  Whe
2f4ef 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 0a 20  reClause *pWC,. 
2f4f0 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2f4f1 69 74 65 6d 20 2a 70 53 72 63 2c 0a 20 20 45 78  item *pSrc,.  Ex
2f4f2 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2f4f3 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  .){.  int i, j;.
2f4f4 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 73    int nTerm;.  s
2f4f5 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2f4f6 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
2f4f7 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75  pIdxCons;.  stru
2f4f8 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2f4f9 5f 6f 72 64 65 72 62 79 20 2a 70 49 64 78 4f 72  _orderby *pIdxOr
2f4fa 64 65 72 42 79 3b 0a 20 20 73 74 72 75 63 74 20  derBy;.  struct 
2f4fb 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2f4fc 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a  nstraint_usage *
2f4fd 70 55 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54  pUsage;.  WhereT
2f4fe 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2f4ff 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 73 71  t nOrderBy;.  sq
2f500 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f501 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 0a 20 20 57   *pIdxInfo;..  W
2f502 48 45 52 45 54 52 41 43 45 28 28 22 52 65 63 6f  HERETRACE(("Reco
2f503 6d 70 75 74 69 6e 67 20 69 6e 64 65 78 20 69 6e  mputing index in
2f504 66 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c  fo for %s...\n",
2f505 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61   pSrc->pTab->zNa
2f506 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 75 6e  me));..  /* Coun
2f507 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
2f508 70 6f 73 73 69 62 6c 65 20 57 48 45 52 45 20 63  possible WHERE c
2f509 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74  lause constraint
2f50a 73 20 72 65 66 65 72 72 69 6e 67 0a 20 20 2a 2a  s referring.  **
2f50b 20 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c   to this virtual
2f50c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28   table */.  for(
2f50d 69 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d  i=nTerm=0, pTerm
2f50e 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e  =pWC->a; i<pWC->
2f50f 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72  nTerm; i++, pTer
2f510 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54  m++){.    if( pT
2f511 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
2f512 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72  != pSrc->iCursor
2f513 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2f514 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d   assert( (pTerm-
2f515 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72  >eOperator&(pTer
2f516 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29  m->eOperator-1))
2f517 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ==0 );.    testc
2f518 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2f519 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a  rator==WO_IN );.
2f51a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
2f51b 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
2f51c 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  WO_ISNULL );.   
2f51d 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65   if( pTerm->eOpe
2f51e 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57  rator & (WO_IN|W
2f51f 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74  O_ISNULL) ) cont
2f520 69 6e 75 65 3b 0a 20 20 20 20 6e 54 65 72 6d 2b  inue;.    nTerm+
2f521 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  +;.  }..  /* If 
2f522 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2f523 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c  use contains onl
2f524 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  y columns in the
2f525 20 63 75 72 72 65 6e 74 20 0a 20 20 2a 2a 20 76   current .  ** v
2f526 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 68 65  irtual table the
2f527 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  n allocate space
2f528 20 66 6f 72 20 74 68 65 20 61 4f 72 64 65 72 42   for the aOrderB
2f529 79 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a 20 74  y part of.  ** t
2f52a 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f52b 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  _info structure.
2f52c 0a 20 20 2a 2f 0a 20 20 6e 4f 72 64 65 72 42 79  .  */.  nOrderBy
2f52d 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f 72 64   = 0;.  if( pOrd
2f52e 65 72 42 79 20 29 7b 0a 20 20 20 20 66 6f 72 28  erBy ){.    for(
2f52f 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  i=0; i<pOrderBy-
2f530 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2f531 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20      Expr *pExpr 
2f532 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
2f533 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  .pExpr;.      if
2f534 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2f535 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d  COLUMN || pExpr-
2f536 3e 69 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 69  >iTable!=pSrc->i
2f537 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a  Cursor ) break;.
2f538 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d      }.    if( i=
2f539 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  =pOrderBy->nExpr
2f53a 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 64 65 72   ){.      nOrder
2f53b 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e  By = pOrderBy->n
2f53c 45 78 70 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Expr;.    }.  }.
2f53d 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
2f53e 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f53f 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  _info structure.
2f540 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20    */.  pIdxInfo 
2f541 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2f542 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62  cZero(pParse->db
2f543 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 49 6e  , sizeof(*pIdxIn
2f544 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  fo).            
2f545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b                 +
2f546 20 28 73 69 7a 65 6f 66 28 2a 70 49 64 78 43 6f   (sizeof(*pIdxCo
2f547 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28 2a 70 55  ns) + sizeof(*pU
2f548 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a 20 20 20  sage))*nTerm.   
2f549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f54a 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66          + sizeof
2f54b 28 2a 70 49 64 78 4f 72 64 65 72 42 79 29 2a 6e  (*pIdxOrderBy)*n
2f54c 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 69 66 28  OrderBy );.  if(
2f54d 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a   pIdxInfo==0 ){.
2f54e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2f54f 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 75 74  Msg(pParse, "out
2f550 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
2f551 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
2f552 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
2f553 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
2f554 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72  OINT... */.    r
2f555 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
2f556 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
2f557 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  e structure.  Th
2f558 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2f559 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 63  info structure c
2f55a 6f 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 6d 61 6e  ontains.  ** man
2f55b 79 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72  y fields that ar
2f55c 65 20 64 65 63 6c 61 72 65 64 20 22 63 6f 6e 73  e declared "cons
2f55d 74 22 20 74 6f 20 70 72 65 76 65 6e 74 20 78 42  t" to prevent xB
2f55e 65 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a 20 20  estIndex from.  
2f55f 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 6d  ** changing them
2f560 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f  .  We have to do
2f561 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61 73 74   some funky cast
2f562 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  ing in order to.
2f563 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
2f564 74 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a 20 20  those fields..  
2f565 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20  */.  pIdxCons = 
2f566 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2f567 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2f568 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a  *)&pIdxInfo[1];.
2f569 20 20 70 49 64 78 4f 72 64 65 72 42 79 20 3d 20    pIdxOrderBy = 
2f56a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2f56b 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 29 26  index_orderby*)&
2f56c 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b  pIdxCons[nTerm];
2f56d 0a 20 20 70 55 73 61 67 65 20 3d 20 28 73 74 72  .  pUsage = (str
2f56e 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2f56f 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2f570 67 65 2a 29 26 70 49 64 78 4f 72 64 65 72 42 79  ge*)&pIdxOrderBy
2f571 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 2a 28  [nOrderBy];.  *(
2f572 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  int*)&pIdxInfo->
2f573 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54  nConstraint = nT
2f574 65 72 6d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70  erm;.  *(int*)&p
2f575 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42  IdxInfo->nOrderB
2f576 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20  y = nOrderBy;.  
2f577 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2f578 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2f579 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  t**)&pIdxInfo->a
2f57a 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 49 64  Constraint = pId
2f57b 78 43 6f 6e 73 3b 0a 20 20 2a 28 73 74 72 75 63  xCons;.  *(struc
2f57c 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f57d 6f 72 64 65 72 62 79 2a 2a 29 26 70 49 64 78 49  orderby**)&pIdxI
2f57e 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20  nfo->aOrderBy = 
2f57f 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 2a  pIdxOrderBy;.  *
2f580 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  (struct sqlite3_
2f581 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2f582 5f 75 73 61 67 65 2a 2a 29 26 70 49 64 78 49 6e  _usage**)&pIdxIn
2f583 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  fo->aConstraintU
2f584 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20  sage =.         
2f585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f586 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f588 20 20 20 20 20 20 20 20 20 20 70 55 73 61 67 65            pUsage
2f589 3b 0a 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20  ;..  for(i=j=0, 
2f58a 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c  pTerm=pWC->a; i<
2f58b 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c  pWC->nTerm; i++,
2f58c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69   pTerm++){.    i
2f58d 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
2f58e 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43  rsor != pSrc->iC
2f58f 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65  ursor ) continue
2f590 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
2f591 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26  Term->eOperator&
2f592 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2f593 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  r-1))==0 );.    
2f594 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2f595 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49  >eOperator==WO_I
2f596 4e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  N );.    testcas
2f597 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  e( pTerm->eOpera
2f598 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29  tor==WO_ISNULL )
2f599 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d  ;.    if( pTerm-
2f59a 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
2f59b 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29  _IN|WO_ISNULL) )
2f59c 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70   continue;.    p
2f59d 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75  IdxCons[j].iColu
2f59e 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65  mn = pTerm->u.le
2f59f 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49  ftColumn;.    pI
2f5a0 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f  dxCons[j].iTermO
2f5a1 66 66 73 65 74 20 3d 20 69 3b 0a 20 20 20 20 70  ffset = i;.    p
2f5a2 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20  IdxCons[j].op = 
2f5a3 28 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70 65 72  (u8)pTerm->eOper
2f5a4 61 74 6f 72 3b 0a 20 20 20 20 2f 2a 20 54 68 65  ator;.    /* The
2f5a5 20 64 69 72 65 63 74 20 61 73 73 69 67 6e 6d 65   direct assignme
2f5a6 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f  nt in the previo
2f5a7 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73 73 69  us line is possi
2f5a8 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75 73 65  ble only because
2f5a9 0a 20 20 20 20 2a 2a 20 74 68 65 20 57 4f 5f 20  .    ** the WO_ 
2f5aa 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  and SQLITE_INDEX
2f5ab 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20 63 6f 64  _CONSTRAINT_ cod
2f5ac 65 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  es are identical
2f5ad 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 66 6f  .  The.    ** fo
2f5ae 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20  llowing asserts 
2f5af 76 65 72 69 66 79 20 74 68 69 73 20 66 61 63 74  verify this fact
2f5b0 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
2f5b1 20 57 4f 5f 45 51 3d 3d 53 51 4c 49 54 45 5f 49   WO_EQ==SQLITE_I
2f5b2 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2f5b3 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  EQ );.    assert
2f5b4 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f  ( WO_LT==SQLITE_
2f5b5 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
2f5b6 5f 4c 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72  _LT );.    asser
2f5b7 74 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45  t( WO_LE==SQLITE
2f5b8 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2f5b9 54 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73 73 65  T_LE );.    asse
2f5ba 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54  rt( WO_GT==SQLIT
2f5bb 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2f5bc 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20 61 73 73  NT_GT );.    ass
2f5bd 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49  ert( WO_GE==SQLI
2f5be 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2f5bf 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20 61 73  INT_GE );.    as
2f5c0 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d 3d  sert( WO_MATCH==
2f5c1 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2f5c2 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29 3b  STRAINT_MATCH );
2f5c3 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
2f5c4 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2f5c5 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f 5f  (WO_EQ|WO_LT|WO_
2f5c6 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57  LE|WO_GT|WO_GE|W
2f5c7 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20 20 20  O_MATCH) );.    
2f5c8 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  j++;.  }.  for(i
2f5c9 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20  =0; i<nOrderBy; 
2f5ca 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  i++){.    Expr *
2f5cb 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 79  pExpr = pOrderBy
2f5cc 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2f5cd 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d    pIdxOrderBy[i]
2f5ce 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72  .iColumn = pExpr
2f5cf 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70  ->iColumn;.    p
2f5d0 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65  IdxOrderBy[i].de
2f5d1 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  sc = pOrderBy->a
2f5d2 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  [i].sortOrder;. 
2f5d3 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 49 64   }..  return pId
2f5d4 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  xInfo;.}../*.** 
2f5d5 54 68 65 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  The table object
2f5d6 20 72 65 66 65 72 65 6e 63 65 20 70 61 73 73 65   reference passe
2f5d7 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
2f5d8 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
2f5d9 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73   function.** mus
2f5da 74 20 72 65 70 72 65 73 65 6e 74 20 61 20 76 69  t represent a vi
2f5db 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 69  rtual table. Thi
2f5dc 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b  s function invok
2f5dd 65 73 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  es the xBestInde
2f5de 78 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  x().** method of
2f5df 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2f5e0 6c 65 20 77 69 74 68 20 74 68 65 20 73 71 6c 69  le with the sqli
2f5e1 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 70  te3_index_info p
2f5e2 6f 69 6e 74 65 72 20 70 61 73 73 65 64 0a 2a 2a  ointer passed.**
2f5e3 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74   as the argument
2f5e4 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
2f5e5 72 6f 72 20 6f 63 63 75 72 73 2c 20 70 50 61 72  ror occurs, pPar
2f5e6 73 65 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  se is populated 
2f5e7 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
2f5e8 73 73 61 67 65 20 61 6e 64 20 61 0a 2a 2a 20 6e  ssage and a.** n
2f5e9 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69 73  on-zero value is
2f5ea 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
2f5eb 77 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72  wise, 0 is retur
2f5ec 6e 65 64 20 61 6e 64 20 74 68 65 20 6f 75 74 70  ned and the outp
2f5ed 75 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 74 68  ut.** part of th
2f5ee 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2f5ef 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  info structure i
2f5f0 73 20 6c 65 66 74 20 70 6f 70 75 6c 61 74 65 64  s left populated
2f5f1 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20  ..**.** Whether 
2f5f2 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20  or not an error 
2f5f3 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20  is returned, it 
2f5f4 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62  is the responsib
2f5f5 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20  ility of the.** 
2f5f6 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75  caller to eventu
2f5f7 61 6c 6c 79 20 66 72 65 65 20 70 2d 3e 69 64 78  ally free p->idx
2f5f8 53 74 72 20 69 66 20 70 2d 3e 6e 65 65 64 54 6f  Str if p->needTo
2f5f9 46 72 65 65 49 64 78 53 74 72 20 69 6e 64 69 63  FreeIdxStr indic
2f5fa 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 69  ates.** that thi
2f5fb 73 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a  s is required..*
2f5fc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 74 61  /.static int vta
2f5fd 62 42 65 73 74 49 6e 64 65 78 28 50 61 72 73 65  bBestIndex(Parse
2f5fe 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
2f5ff 2a 70 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69  *pTab, sqlite3_i
2f600 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20  ndex_info *p){. 
2f601 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
2f602 56 74 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65  Vtab = sqlite3Ge
2f603 74 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e  tVTable(pParse->
2f604 64 62 2c 20 70 54 61 62 29 2d 3e 70 56 74 61 62  db, pTab)->pVtab
2f605 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
2f606 20 72 63 3b 0a 0a 20 20 28 76 6f 69 64 29 73 71   rc;..  (void)sq
2f607 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 70  lite3SafetyOff(p
2f608 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 57 48  Parse->db);.  WH
2f609 45 52 45 54 52 41 43 45 28 28 22 78 42 65 73 74  ERETRACE(("xBest
2f60a 49 6e 64 65 78 20 66 6f 72 20 25 73 5c 6e 22 2c  Index for %s\n",
2f60b 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pTab->zName));.
2f60c 20 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55    TRACE_IDX_INPU
2f60d 54 53 28 70 29 3b 0a 20 20 72 63 20 3d 20 70 56  TS(p);.  rc = pV
2f60e 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42  tab->pModule->xB
2f60f 65 73 74 49 6e 64 65 78 28 70 56 74 61 62 2c 20  estIndex(pVtab, 
2f610 70 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f  p);.  TRACE_IDX_
2f611 4f 55 54 50 55 54 53 28 70 29 3b 0a 20 20 28 76  OUTPUTS(p);.  (v
2f612 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
2f613 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  yOn(pParse->db);
2f614 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
2f615 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
2f616 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2f617 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  M ){.      pPars
2f618 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
2f619 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  led = 1;.    }el
2f61a 73 65 20 69 66 28 20 21 70 56 74 61 62 2d 3e 7a  se if( !pVtab->z
2f61b 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
2f61c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2f61d 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73 71  pParse, "%s", sq
2f61e 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29 29  lite3ErrStr(rc))
2f61f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2f620 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2f621 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c  sg(pParse, "%s",
2f622 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29   pVtab->zErrMsg)
2f623 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
2f624 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72  lite3DbFree(pPar
2f625 73 65 2d 3e 64 62 2c 20 70 56 74 61 62 2d 3e 7a  se->db, pVtab->z
2f626 45 72 72 4d 73 67 29 3b 0a 20 20 70 56 74 61 62  ErrMsg);.  pVtab
2f627 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a  ->zErrMsg = 0;..
2f628 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2f629 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2f62a 29 7b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 61  ){.    if( !p->a
2f62b 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73  Constraint[i].us
2f62c 61 62 6c 65 20 26 26 20 70 2d 3e 61 43 6f 6e 73  able && p->aCons
2f62d 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61  traintUsage[i].a
2f62e 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20  rgvIndex>0 ){.  
2f62f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2f630 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
2f631 20 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 73         "table %s
2f632 3a 20 78 42 65 73 74 49 6e 64 65 78 20 72 65 74  : xBestIndex ret
2f633 75 72 6e 65 64 20 61 6e 20 69 6e 76 61 6c 69 64  urned an invalid
2f634 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e   plan", pTab->zN
2f635 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ame);.    }.  }.
2f636 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65  .  return pParse
2f637 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ->nErr;.}.../*.*
2f638 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 62 65  * Compute the be
2f639 73 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20 76  st index for a v
2f63a 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
2f63b 0a 2a 2a 20 54 68 65 20 62 65 73 74 20 69 6e 64  .** The best ind
2f63c 65 78 20 69 73 20 63 6f 6d 70 75 74 65 64 20 62  ex is computed b
2f63d 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
2f63e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
2f63f 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
2f640 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 72 6f  module.  This ro
2f641 75 74 69 6e 65 20 69 73 20 72 65 61 6c 6c 79 20  utine is really 
2f642 6a 75 73 74 20 61 20 77 72 61 70 70 65 72 20 74  just a wrapper t
2f643 68 61 74 20 73 65 74 73 20 75 70 0a 2a 2a 20 74  hat sets up.** t
2f644 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f645 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2f646 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20  that is used to 
2f647 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69 74 68  communicate with
2f648 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e 0a  .** xBestIndex..
2f649 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e 2c  **.** In a join,
2f64a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   this routine mi
2f64b 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 6d 75  ght be called mu
2f64c 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 66 6f 72  ltiple times for
2f64d 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76 69 72   the.** same vir
2f64e 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
2f64f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f650 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
2f651 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20   created.** and 
2f652 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20 74  initialized on t
2f653 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74  he first invocat
2f654 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20 6f  ion and reused o
2f655 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  n all subsequent
2f656 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e  .** invocations.
2f657 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e    The sqlite3_in
2f658 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2f659 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  re is also used 
2f65a 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69 73 20  when.** code is 
2f65b 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63 63  generated to acc
2f65c 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 20  ess the virtual 
2f65d 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65 72  table.  The wher
2f65e 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0a 2a  eInfoDelete() .*
2f65f 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20  * routine takes 
2f660 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e 67 20  care of freeing 
2f661 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2f662 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2f663 20 61 66 74 65 72 0a 2a 2a 20 65 76 65 72 79 62   after.** everyb
2f664 6f 64 79 20 68 61 73 20 66 69 6e 69 73 68 65 64  ody has finished
2f665 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73 74 61   with it..*/.sta
2f666 74 69 63 20 76 6f 69 64 20 62 65 73 74 56 69 72  tic void bestVir
2f667 74 75 61 6c 49 6e 64 65 78 28 0a 20 20 50 61 72  tualIndex(.  Par
2f668 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2f669 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f66a 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2f66b 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
2f66c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
2f66d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2f66e 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2f66f 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2f670 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 20 20  _item *pSrc,    
2f671 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2f672 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
2f673 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
2f674 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
2f675 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
2f676 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74   of cursors that
2f677 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
2f678 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  le */.  ExprList
2f679 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20   *pOrderBy,     
2f67a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
2f67b 72 64 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a  rder by clause *
2f67c 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70  /.  WhereCost *p
2f67d 43 6f 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  Cost,           
2f67e 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f      /* Lowest co
2f67f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f  st query plan */
2f680 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  .  sqlite3_index
2f681 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66  _info **ppIdxInf
2f682 6f 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f  o  /* Index info
2f683 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 74  rmation passed t
2f684 6f 20 78 42 65 73 74 49 6e 64 65 78 20 2a 2f 0a  o xBestIndex */.
2f685 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
2f686 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20   = pSrc->pTab;. 
2f687 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f688 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 20  nfo *pIdxInfo;. 
2f689 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2f68a 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2f68b 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74   *pIdxCons;.  st
2f68c 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f68d 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
2f68e 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57  age *pUsage;.  W
2f68f 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2f690 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
2f691 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20  nt nOrderBy;..  
2f692 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 73 46  /* Make sure wsF
2f693 6c 61 67 73 20 69 73 20 69 6e 69 74 69 61 6c 69  lags is initiali
2f694 7a 65 64 20 74 6f 20 73 6f 6d 65 20 73 61 6e 65  zed to some sane
2f695 20 76 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73   value. Otherwis
2f696 65 2c 20 69 66 20 74 68 65 20 0a 20 20 2a 2a 20  e, if the .  ** 
2f697 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c 6c 6f 63 61  malloc in alloca
2f698 74 65 49 6e 64 65 78 49 6e 66 6f 28 29 20 66 61  teIndexInfo() fa
2f699 69 6c 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e  ils and this fun
2f69a 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 6c 65  ction returns le
2f69b 61 76 69 6e 67 0a 20 20 2a 2a 20 77 73 46 6c 61  aving.  ** wsFla
2f69c 67 73 20 69 6e 20 61 6e 20 75 6e 69 6e 69 74 69  gs in an uniniti
2f69d 61 6c 69 7a 65 64 20 73 74 61 74 65 2c 20 74 68  alized state, th
2f69e 65 20 63 61 6c 6c 65 72 20 6d 61 79 20 62 65 68  e caller may beh
2f69f 61 76 65 20 75 6e 70 72 65 64 69 63 74 61 62 6c  ave unpredictabl
2f6a0 79 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  y..  */.  memset
2f6a1 28 70 43 6f 73 74 2c 20 30 2c 20 73 69 7a 65 6f  (pCost, 0, sizeo
2f6a2 66 28 2a 70 43 6f 73 74 29 29 3b 0a 20 20 70 43  f(*pCost));.  pC
2f6a3 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2f6a4 73 20 3d 20 57 48 45 52 45 5f 56 49 52 54 55 41  s = WHERE_VIRTUA
2f6a5 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f 2a 20 49 66  LTABLE;..  /* If
2f6a6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2f6a7 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2f6a8 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70  e has not been p
2f6a9 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  reviously.  ** a
2f6aa 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 6e 69  llocated and ini
2f6ab 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 61  tialized, then a
2f6ac 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74  llocate and init
2f6ad 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77 2e 0a 20  ialize it now.. 
2f6ae 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d   */.  pIdxInfo =
2f6af 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69   *ppIdxInfo;.  i
2f6b0 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29  f( pIdxInfo==0 )
2f6b1 7b 0a 20 20 20 20 2a 70 70 49 64 78 49 6e 66 6f  {.    *ppIdxInfo
2f6b2 20 3d 20 70 49 64 78 49 6e 66 6f 20 3d 20 61 6c   = pIdxInfo = al
2f6b3 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28  locateIndexInfo(
2f6b4 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72  pParse, pWC, pSr
2f6b5 63 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  c, pOrderBy);.  
2f6b6 7d 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  }.  if( pIdxInfo
2f6b7 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2f6b8 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20  n;.  }..  /* At 
2f6b9 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 20  this point, the 
2f6ba 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f6bb 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2f6bc 74 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74  t pIdxInfo point
2f6bd 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20 68  s.  ** to will h
2f6be 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ave been initial
2f6bf 69 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75 72  ized, either dur
2f6c0 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
2f6c1 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20 20  invocation or.  
2f6c2 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20 70  ** during some p
2f6c3 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 2e  rior invocation.
2f6c4 20 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68 61    Now we just ha
2f6c5 76 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20  ve to customize 
2f6c6 74 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c 73  the.  ** details
2f6c7 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72   of pIdxInfo for
2f6c8 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76   the current inv
2f6c9 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73 73  ocation and pass
2f6ca 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65 73   it to.  ** xBes
2f6cb 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20  tIndex..  */..  
2f6cc 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61  /* The module na
2f6cd 6d 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e  me must be defin
2f6ce 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68 69  ed. Also, by thi
2f6cf 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 75  s point there mu
2f6d0 73 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f 69  st.  ** be a poi
2f6d1 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74  nter to an sqlit
2f6d2 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
2f6d3 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 2a  e. Otherwise.  *
2f6d4 2a 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  * sqlite3ViewGet
2f6d5 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f  ColumnNames() wo
2f6d6 75 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64 20  uld have picked 
2f6d7 75 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a 20  up the error. . 
2f6d8 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
2f6d9 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20  ab->azModuleArg 
2f6da 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c  && pTab->azModul
2f6db 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73 73  eArg[0] );.  ass
2f6dc 65 72 74 28 20 73 71 6c 69 74 65 33 47 65 74 56  ert( sqlite3GetV
2f6dd 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62  Table(pParse->db
2f6de 2c 20 70 54 61 62 29 20 29 3b 0a 0a 20 20 2f 2a  , pTab) );..  /*
2f6df 20 53 65 74 20 74 68 65 20 61 43 6f 6e 73 74 72   Set the aConstr
2f6e0 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69  aint[].usable fi
2f6e1 65 6c 64 73 20 61 6e 64 20 69 6e 69 74 69 61 6c  elds and initial
2f6e2 69 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75  ize all .  ** ou
2f6e3 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 74  tput variables t
2f6e4 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a  o zero..  **.  *
2f6e5 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  * aConstraint[].
2f6e6 75 73 61 62 6c 65 20 69 73 20 74 72 75 65 20 66  usable is true f
2f6e7 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77  or constraints w
2f6e8 68 65 72 65 20 74 68 65 20 72 69 67 68 74 2d 68  here the right-h
2f6e9 61 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f  and.  ** side co
2f6ea 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65  ntains only refe
2f6eb 72 65 6e 63 65 73 20 74 6f 20 74 61 62 6c 65 73  rences to tables
2f6ec 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
2f6ed 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a  the current.  **
2f6ee 20 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65   table.  In othe
2f6ef 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20  r words, if the 
2f6f0 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6f 66  constraint is of
2f6f1 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a   the form:.  **.
2f6f2 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63    **           c
2f6f3 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a  olumn = expr.  *
2f6f4 2a 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20 61 72  *.  ** and we ar
2f6f5 65 20 65 76 61 6c 75 61 74 69 6e 67 20 61 20 6a  e evaluating a j
2f6f6 6f 69 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  oin, then the co
2f6f7 6e 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75  nstraint on colu
2f6f8 6d 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79  mn is .  ** only
2f6f9 20 76 61 6c 69 64 20 69 66 20 61 6c 6c 20 74 61   valid if all ta
2f6fa 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64 20  bles referenced 
2f6fb 69 6e 20 65 78 70 72 20 6f 63 63 75 72 20 74 6f  in expr occur to
2f6fc 20 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f   the left.  ** o
2f6fd 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74  f the table cont
2f6fe 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20  aining column.. 
2f6ff 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f   **.  ** The aCo
2f700 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61 72 72 61  nstraints[] arra
2f701 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69  y contains entri
2f702 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74  es for all const
2f703 72 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74  raints.  ** on t
2f704 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
2f705 2e 20 20 54 68 61 74 20 77 61 79 20 77 65 20 6f  .  That way we o
2f706 6e 6c 79 20 68 61 76 65 20 74 6f 20 63 6f 6d 70  nly have to comp
2f707 75 74 65 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a  ute it once.  **
2f708 20 65 76 65 6e 20 74 68 6f 75 67 68 20 77 65 20   even though we 
2f709 6d 69 67 68 74 20 74 72 79 20 74 6f 20 70 69 63  might try to pic
2f70a 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78  k the best index
2f70b 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e   multiple times.
2f70c 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 61  .  ** For each a
2f70d 74 74 65 6d 70 74 20 61 74 20 70 69 63 6b 69 6e  ttempt at pickin
2f70e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65 20  g an index, the 
2f70f 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20  order of tables 
2f710 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e  in the.  ** join
2f711 20 6d 69 67 68 74 20 62 65 20 64 69 66 66 65 72   might be differ
2f712 65 6e 74 20 73 6f 20 77 65 20 68 61 76 65 20 74  ent so we have t
2f713 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20  o recompute the 
2f714 75 73 61 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a  usable flag.  **
2f715 20 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f   each time..  */
2f716 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28  .  pIdxCons = *(
2f717 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2f718 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a  ndex_constraint*
2f719 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f  *)&pIdxInfo->aCo
2f71a 6e 73 74 72 61 69 6e 74 3b 0a 20 20 70 55 73 61  nstraint;.  pUsa
2f71b 67 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61  ge = pIdxInfo->a
2f71c 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b  ConstraintUsage;
2f71d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
2f71e 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  dxInfo->nConstra
2f71f 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f  int; i++, pIdxCo
2f720 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70  ns++){.    j = p
2f721 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66  IdxCons->iTermOf
2f722 66 73 65 74 3b 0a 20 20 20 20 70 54 65 72 6d 20  fset;.    pTerm 
2f723 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20  = &pWC->a[j];.  
2f724 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62    pIdxCons->usab
2f725 6c 65 20 3d 20 28 70 54 65 72 6d 2d 3e 70 72 65  le = (pTerm->pre
2f726 72 65 71 52 69 67 68 74 26 6e 6f 74 52 65 61 64  reqRight&notRead
2f727 79 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20 7d 0a  y) ? 0 : 1;.  }.
2f728 20 20 6d 65 6d 73 65 74 28 70 55 73 61 67 65 2c    memset(pUsage,
2f729 20 30 2c 20 73 69 7a 65 6f 66 28 70 55 73 61 67   0, sizeof(pUsag
2f72a 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e  e[0])*pIdxInfo->
2f72b 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  nConstraint);.  
2f72c 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65  if( pIdxInfo->ne
2f72d 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29  edToFreeIdxStr )
2f72e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
2f72f 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  ee(pIdxInfo->idx
2f730 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78  Str);.  }.  pIdx
2f731 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30  Info->idxStr = 0
2f732 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  ;.  pIdxInfo->id
2f733 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78  xNum = 0;.  pIdx
2f734 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65  Info->needToFree
2f735 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49  IdxStr = 0;.  pI
2f736 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43  dxInfo->orderByC
2f737 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20 20 2f  onsumed = 0;.  /
2f738 2a 20 28 28 64 6f 75 62 6c 65 29 32 29 20 49 6e  * ((double)2) In
2f739 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
2f73a 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
2f73b 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70 49 64 78  INT... */.  pIdx
2f73c 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
2f73d 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
2f73e 5f 44 42 4c 20 2f 20 28 28 64 6f 75 62 6c 65 29  _DBL / ((double)
2f73f 32 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d  2);.  nOrderBy =
2f740 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
2f741 72 42 79 3b 0a 20 20 69 66 28 20 21 70 4f 72 64  rBy;.  if( !pOrd
2f742 65 72 42 79 20 29 7b 0a 20 20 20 20 70 49 64 78  erBy ){.    pIdx
2f743 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d  Info->nOrderBy =
2f744 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76   0;.  }..  if( v
2f745 74 61 62 42 65 73 74 49 6e 64 65 78 28 70 50 61  tabBestIndex(pPa
2f746 72 73 65 2c 20 70 54 61 62 2c 20 70 49 64 78 49  rse, pTab, pIdxI
2f747 6e 66 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75  nfo) ){.    retu
2f748 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 49 64 78 43  rn;.  }..  pIdxC
2f749 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20 73  ons = *(struct s
2f74a 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2f74b 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49  straint**)&pIdxI
2f74c 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
2f74d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2f74e 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
2f74f 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aint; i++){.    
2f750 69 66 28 20 70 55 73 61 67 65 5b 69 5d 2e 61 72  if( pUsage[i].ar
2f751 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20  gvIndex>0 ){.   
2f752 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20 7c     pCost->used |
2f753 3d 20 70 57 43 2d 3e 61 5b 70 49 64 78 43 6f 6e  = pWC->a[pIdxCon
2f754 73 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74  s[i].iTermOffset
2f755 5d 2e 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20  ].prereqRight;. 
2f756 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
2f757 68 65 20 63 6f 73 74 20 69 73 20 6e 6f 74 20 61  he cost is not a
2f758 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c 61 72  llowed to be lar
2f759 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  ger than SQLITE_
2f75a 42 49 47 5f 44 42 4c 20 28 74 68 65 0a 20 20 2a  BIG_DBL (the.  *
2f75b 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f  * inital value o
2f75c 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e 20  f lowestCost in 
2f75d 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74  this loop. If it
2f75e 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20   is, then the.  
2f75f 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43  ** (cost<lowestC
2f760 6f 73 74 29 20 74 65 73 74 20 62 65 6c 6f 77 20  ost) test below 
2f761 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 74 72  will never be tr
2f762 75 65 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 55  ue..  ** .  ** U
2f763 73 65 20 22 28 64 6f 75 62 6c 65 29 32 22 20 69  se "(double)2" i
2f764 6e 73 74 65 61 64 20 6f 66 20 22 32 2e 30 22 20  nstead of "2.0" 
2f765 69 6e 20 63 61 73 65 20 4f 4d 49 54 5f 46 4c 4f  in case OMIT_FLO
2f766 41 54 49 4e 47 5f 50 4f 49 4e 54 20 0a 20 20 2a  ATING_POINT .  *
2f767 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20  * is defined..  
2f768 2a 2f 0a 20 20 69 66 28 20 28 53 51 4c 49 54 45  */.  if( (SQLITE
2f769 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c  _BIG_DBL/((doubl
2f76a 65 29 32 29 29 3c 70 49 64 78 49 6e 66 6f 2d 3e  e)2))<pIdxInfo->
2f76b 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 29 7b  estimatedCost ){
2f76c 0a 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73  .    pCost->rCos
2f76d 74 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f  t = (SQLITE_BIG_
2f76e 44 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32 29 29  DBL/((double)2))
2f76f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2f770 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 70 49  Cost->rCost = pI
2f771 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
2f772 64 43 6f 73 74 3b 0a 20 20 7d 0a 20 20 70 43 6f  dCost;.  }.  pCo
2f773 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62  st->plan.u.pVtab
2f774 49 64 78 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a  Idx = pIdxInfo;.
2f775 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e    if( pIdxInfo->
2f776 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20  orderByConsumed 
2f777 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c  ){.    pCost->pl
2f778 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48  an.wsFlags |= WH
2f779 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20 7d  ERE_ORDERBY;.  }
2f77a 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e  .  pCost->plan.n
2f77b 45 71 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e  Eq = 0;.  pIdxIn
2f77c 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e  fo->nOrderBy = n
2f77d 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 54  OrderBy;..  /* T
2f77e 72 79 20 74 6f 20 66 69 6e 64 20 61 20 6d 6f 72  ry to find a mor
2f77f 65 20 65 66 66 69 63 69 65 6e 74 20 61 63 63 65  e efficient acce
2f780 73 73 20 70 61 74 74 65 72 6e 20 62 79 20 75 73  ss pattern by us
2f781 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64  ing multiple ind
2f782 65 78 65 73 0a 20 20 2a 2a 20 74 6f 20 6f 70 74  exes.  ** to opt
2f783 69 6d 69 7a 65 20 61 6e 20 4f 52 20 65 78 70 72  imize an OR expr
2f784 65 73 73 69 6f 6e 20 77 69 74 68 69 6e 20 74 68  ession within th
2f785 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2f786 0a 20 20 2a 2f 0a 20 20 62 65 73 74 4f 72 43 6c  .  */.  bestOrCl
2f787 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65  auseIndex(pParse
2f788 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
2f789 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c  Ready, pOrderBy,
2f78a 20 70 43 6f 73 74 29 3b 0a 7d 0a 23 65 6e 64 69   pCost);.}.#endi
2f78b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2f78c 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2f78d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
2f78e 20 70 49 64 78 20 69 73 20 61 20 70 6f 69 6e 74   pIdx is a point
2f78f 65 72 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 73  er to an index s
2f790 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68 61  tructure that ha
2f791 73 20 61 6e 20 61 72 72 61 79 20 6f 66 0a 2a 2a  s an array of.**
2f792 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
2f793 4d 50 4c 45 53 20 65 76 65 6e 6c 79 20 73 70 61  MPLES evenly spa
2f794 63 65 64 20 73 61 6d 70 6c 65 73 20 6f 66 20 74  ced samples of t
2f795 68 65 20 66 69 72 73 74 20 69 6e 64 65 78 65 64  he first indexed
2f796 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 73 74 6f 72 65   column.** store
2f797 64 20 69 6e 20 49 6e 64 65 78 2e 61 53 61 6d 70  d in Index.aSamp
2f798 6c 65 2e 20 54 68 65 20 64 6f 6d 61 69 6e 20 6f  le. The domain o
2f799 66 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  f values stored 
2f79a 69 6e 20 73 61 69 64 20 63 6f 6c 75 6d 6e 0a 2a  in said column.*
2f79b 2a 20 6d 61 79 20 62 65 20 74 68 6f 75 67 68 74  * may be thought
2f79c 20 6f 66 20 61 73 20 64 69 76 69 64 65 64 20 69   of as divided i
2f79d 6e 74 6f 20 28 53 51 4c 49 54 45 5f 49 4e 44 45  nto (SQLITE_INDE
2f79e 58 5f 53 41 4d 50 4c 45 53 2b 31 29 20 72 65 67  X_SAMPLES+1) reg
2f79f 69 6f 6e 73 2e 0a 2a 2a 20 52 65 67 69 6f 6e 20  ions..** Region 
2f7a0 30 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 76  0 contains all v
2f7a1 61 6c 75 65 73 20 73 6d 61 6c 6c 65 72 20 74 68  alues smaller th
2f7a2 61 6e 20 74 68 65 20 66 69 72 73 74 20 73 61 6d  an the first sam
2f7a3 70 6c 65 20 76 61 6c 75 65 2e 20 52 65 67 69 6f  ple value. Regio
2f7a4 6e 0a 2a 2a 20 31 20 63 6f 6e 74 61 69 6e 73 20  n.** 1 contains 
2f7a5 76 61 6c 75 65 73 20 6c 61 72 67 65 72 20 74 68  values larger th
2f7a6 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
2f7a7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2f7a8 66 69 72 73 74 20 73 61 6d 70 6c 65 2c 0a 2a 2a  first sample,.**
2f7a9 20 62 75 74 20 73 6d 61 6c 6c 65 72 20 74 68 61   but smaller tha
2f7aa 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
2f7ab 68 65 20 73 65 63 6f 6e 64 2e 20 41 6e 64 20 73  he second. And s
2f7ac 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  o on..**.** If s
2f7ad 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20  uccessful, this 
2f7ae 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69  function determi
2f7af 6e 65 73 20 77 68 69 63 68 20 6f 66 20 74 68 65  nes which of the
2f7b0 20 72 65 67 69 6f 6e 73 20 76 61 6c 75 65 20 0a   regions value .
2f7b1 2a 2a 20 70 56 61 6c 20 6c 69 65 73 20 69 6e 2c  ** pVal lies in,
2f7b2 20 73 65 74 73 20 2a 70 69 52 65 67 69 6f 6e 20   sets *piRegion 
2f7b3 74 6f 20 74 68 65 20 72 65 67 69 6f 6e 20 69 6e  to the region in
2f7b4 64 65 78 20 28 61 20 76 61 6c 75 65 20 62 65 74  dex (a value bet
2f7b5 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 53 51  ween 0.** and SQ
2f7b6 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f7b7 45 53 2b 31 2c 20 69 6e 63 6c 75 73 69 76 65 29  ES+1, inclusive)
2f7b8 20 61 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c   and returns SQL
2f7b9 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f 72 2c 20 69  ITE_OK..** Or, i
2f7ba 66 20 61 6e 20 4f 4f 4d 20 6f 63 63 75 72 73 20  f an OOM occurs 
2f7bb 77 68 69 6c 65 20 63 6f 6e 76 65 72 74 69 6e 67  while converting
2f7bc 20 74 65 78 74 20 76 61 6c 75 65 73 20 62 65 74   text values bet
2f7bd 77 65 65 6e 20 65 6e 63 6f 64 69 6e 67 73 2c 0a  ween encodings,.
2f7be 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ** SQLITE_NOMEM 
2f7bf 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
2f7c0 2a 70 69 52 65 67 69 6f 6e 20 69 73 20 75 6e 64  *piRegion is und
2f7c1 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  efined..*/.#ifde
2f7c2 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2f7c3 53 54 41 54 32 0a 73 74 61 74 69 63 20 69 6e 74  STAT2.static int
2f7c4 20 77 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f   whereRangeRegio
2f7c5 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
2f7c6 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
2f7c7 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2f7c8 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64  nection */.  Ind
2f7c9 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
2f7ca 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2f7cb 78 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 64 6f  x to consider do
2f7cc 6d 61 69 6e 20 6f 66 20 2a 2f 0a 20 20 73 71 6c  main of */.  sql
2f7cd 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
2f7ce 2c 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  ,        /* Valu
2f7cf 65 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f  e to consider */
2f7d0 0a 20 20 69 6e 74 20 2a 70 69 52 65 67 69 6f 6e  .  int *piRegion
2f7d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f7d2 2a 20 4f 55 54 3a 20 52 65 67 69 6f 6e 20 6f 66  * OUT: Region of
2f7d3 20 64 6f 6d 61 69 6e 20 69 6e 20 77 68 69 63 68   domain in which
2f7d4 20 76 61 6c 75 65 20 6c 69 65 73 20 2a 2f 0a 29   value lies */.)
2f7d5 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  {.  if( ALWAYS(p
2f7d6 56 61 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 65  Val) ){.    Inde
2f7d7 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65  xSample *aSample
2f7d8 20 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65   = pIdx->aSample
2f7d9 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  ;.    int i = 0;
2f7da 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65 20 3d  .    int eType =
2f7db 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2f7dc 79 70 65 28 70 56 61 6c 29 3b 0a 0a 20 20 20 20  ype(pVal);..    
2f7dd 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  if( eType==SQLIT
2f7de 45 5f 49 4e 54 45 47 45 52 20 7c 7c 20 65 54 79  E_INTEGER || eTy
2f7df 70 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54  pe==SQLITE_FLOAT
2f7e0 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
2f7e1 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   r = sqlite3_val
2f7e2 75 65 5f 64 6f 75 62 6c 65 28 70 56 61 6c 29 3b  ue_double(pVal);
2f7e3 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2f7e4 69 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  i<SQLITE_INDEX_S
2f7e5 41 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b 0a 20 20  AMPLES; i++){.  
2f7e6 20 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c        if( aSampl
2f7e7 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49  e[i].eType==SQLI
2f7e8 54 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e 74 69 6e  TE_NULL ) contin
2f7e9 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ue;.        if( 
2f7ea 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65  aSample[i].eType
2f7eb 3e 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c  >=SQLITE_TEXT ||
2f7ec 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3e   aSample[i].u.r>
2f7ed 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
2f7ee 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 20 0a 20   }.    }else{ . 
2f7ef 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
2f7f0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2f7f1 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
2f7f2 6f 6c 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  oll;.      const
2f7f3 20 75 38 20 2a 7a 3b 0a 20 20 20 20 20 20 69 6e   u8 *z;.      in
2f7f4 74 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 70  t n;..      /* p
2f7f5 56 61 6c 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73  Val comes from s
2f7f6 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
2f7f7 78 70 72 28 29 20 73 6f 20 74 68 65 20 74 79 70  xpr() so the typ
2f7f8 65 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c  e cannot be NULL
2f7f9 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2f7fa 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
2f7fb 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d 53  TEXT || eType==S
2f7fc 51 4c 49 54 45 5f 42 4c 4f 42 20 29 3b 0a 0a 20  QLITE_BLOB );.. 
2f7fd 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d       if( eType==
2f7fe 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20  SQLITE_BLOB ){. 
2f7ff 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73         z = (cons
2f800 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76  t u8 *)sqlite3_v
2f801 61 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c 29 3b  alue_blob(pVal);
2f802 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
2f803 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
2f804 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2f805 70 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d 53 51 4c 49  pColl->enc==SQLI
2f806 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 20  TE_UTF8 );.     
2f807 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2f808 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
2f809 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51  etCollSeq(db, SQ
2f80a 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 2a 70  LITE_UTF8, 0, *p
2f80b 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20  Idx->azColl);.  
2f80c 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d        if( pColl=
2f80d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2f80e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2f80f 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2f810 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2f811 6e 63 65 3a 20 25 73 22 2c 0a 20 20 20 20 20 20  nce: %s",.      
2f812 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f813 20 20 20 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c      *pIdx->azCol
2f814 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  l);.          re
2f815 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2f816 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  R;.        }.   
2f817 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20       z = (const 
2f818 75 38 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75  u8 *)sqlite3Valu
2f819 65 54 65 78 74 28 70 56 61 6c 2c 20 70 43 6f 6c  eText(pVal, pCol
2f81a 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
2f81b 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 20   if( !z ){.     
2f81c 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2f81d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2f81e 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
2f81f 72 74 28 20 7a 20 26 26 20 70 43 6f 6c 6c 20 26  rt( z && pColl &
2f820 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b  & pColl->xCmp );
2f821 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
2f822 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42   = sqlite3ValueB
2f823 79 74 65 73 28 70 56 61 6c 2c 20 70 43 6f 6c 6c  ytes(pVal, pColl
2f824 2d 3e 65 6e 63 29 3b 0a 0a 20 20 20 20 20 20 66  ->enc);..      f
2f825 6f 72 28 69 3d 30 3b 20 69 3c 53 51 4c 49 54 45  or(i=0; i<SQLITE
2f826 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20  _INDEX_SAMPLES; 
2f827 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  i++){.        in
2f828 74 20 72 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  t r;.        int
2f829 20 65 53 61 6d 70 6c 65 74 79 70 65 20 3d 20 61   eSampletype = a
2f82a 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3b  Sample[i].eType;
2f82b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 53 61  .        if( eSa
2f82c 6d 70 6c 65 74 79 70 65 3d 3d 53 51 4c 49 54 45  mpletype==SQLITE
2f82d 5f 4e 55 4c 4c 20 7c 7c 20 65 53 61 6d 70 6c 65  _NULL || eSample
2f82e 74 79 70 65 3c 65 54 79 70 65 20 29 20 63 6f 6e  type<eType ) con
2f82f 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
2f830 66 28 20 28 65 53 61 6d 70 6c 65 74 79 70 65 21  f( (eSampletype!
2f831 3d 65 54 79 70 65 29 20 29 20 62 72 65 61 6b 3b  =eType) ) break;
2f832 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f833 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 20 20 20  OMIT_UTF16.     
2f834 20 20 20 69 66 28 20 70 43 6f 6c 6c 2d 3e 65 6e     if( pColl->en
2f835 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
2f836 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
2f837 6e 53 61 6d 70 6c 65 3b 0a 20 20 20 20 20 20 20  nSample;.       
2f838 20 20 20 63 68 61 72 20 2a 7a 53 61 6d 70 6c 65     char *zSample
2f839 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 74 6f   = sqlite3Utf8to
2f83a 31 36 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  16(.            
2f83b 20 20 64 62 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63    db, pColl->enc
2f83c 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a  , aSample[i].u.z
2f83d 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79  , aSample[i].nBy
2f83e 74 65 2c 20 26 6e 53 61 6d 70 6c 65 0a 20 20 20  te, &nSample.   
2f83f 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
2f840 20 20 20 20 69 66 28 20 21 7a 53 61 6d 70 6c 65      if( !zSample
2f841 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2f842 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
2f843 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2f844 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
2f845 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2f846 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2f847 20 20 20 72 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43     r = pColl->xC
2f848 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c  mp(pColl->pUser,
2f849 20 6e 53 61 6d 70 6c 65 2c 20 7a 53 61 6d 70 6c   nSample, zSampl
2f84a 65 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e, n, z);.      
2f84b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2f84c 65 28 64 62 2c 20 7a 53 61 6d 70 6c 65 29 3b 0a  e(db, zSample);.
2f84d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65          }else.#e
2f84e 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20  ndif.        {. 
2f84f 20 20 20 20 20 20 20 20 20 72 20 3d 20 70 43 6f           r = pCo
2f850 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e  ll->xCmp(pColl->
2f851 70 55 73 65 72 2c 20 61 53 61 6d 70 6c 65 5b 69  pUser, aSample[i
2f852 5d 2e 6e 42 79 74 65 2c 20 61 53 61 6d 70 6c 65  ].nByte, aSample
2f853 5b 69 5d 2e 75 2e 7a 2c 20 6e 2c 20 7a 29 3b 0a  [i].u.z, n, z);.
2f854 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f855 20 20 69 66 28 20 72 3e 30 20 29 20 62 72 65 61    if( r>0 ) brea
2f856 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2f857 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e  ..    assert( i>
2f858 3d 30 20 26 26 20 69 3c 3d 53 51 4c 49 54 45 5f  =0 && i<=SQLITE_
2f859 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b  INDEX_SAMPLES );
2f85a 0a 20 20 20 20 2a 70 69 52 65 67 69 6f 6e 20 3d  .    *piRegion =
2f85b 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   i;.  }.  return
2f85c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
2f85d 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 64 65 66  ndif   /* #ifdef
2f85e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
2f85f 54 41 54 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  TAT2 */../*.** I
2f860 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  f expression pEx
2f861 70 72 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  pr represents a 
2f862 6c 69 74 65 72 61 6c 20 76 61 6c 75 65 2c 20 73  literal value, s
2f863 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20  et *pp to point 
2f864 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65 33  to.** an sqlite3
2f865 5f 76 61 6c 75 65 20 73 74 72 75 63 74 75 72 65  _value structure
2f866 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2f867 73 61 6d 65 20 76 61 6c 75 65 2c 20 77 69 74 68  same value, with
2f868 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 61 66 66   affinity.** aff
2f869 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74 2c 20   applied to it, 
2f86a 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2f86b 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70  . It is the resp
2f86c 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68  onsibility of th
2f86d 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20  e .** caller to 
2f86e 65 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61  eventually relea
2f86f 73 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72  se this structur
2f870 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  e by passing it 
2f871 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 61  to .** sqlite3Va
2f872 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a  lueFree()..**.**
2f873 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   If the current 
2f874 70 61 72 73 65 20 69 73 20 61 20 72 65 63 6f 6d  parse is a recom
2f875 70 69 6c 65 20 28 73 71 6c 69 74 65 33 52 65 70  pile (sqlite3Rep
2f876 72 65 70 61 72 65 28 29 29 20 61 6e 64 20 70 45  repare()) and pE
2f877 78 70 72 0a 2a 2a 20 69 73 20 61 6e 20 53 51 4c  xpr.** is an SQL
2f878 20 76 61 72 69 61 62 6c 65 20 74 68 61 74 20 63   variable that c
2f879 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 20 6e  urrently has a n
2f87a 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20 62 6f  on-NULL value bo
2f87b 75 6e 64 20 74 6f 20 69 74 2c 0a 2a 2a 20 63 72  und to it,.** cr
2f87c 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f  eate an sqlite3_
2f87d 76 61 6c 75 65 20 73 74 72 75 63 74 75 72 65 20  value structure 
2f87e 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20  containing this 
2f87f 76 61 6c 75 65 2c 20 61 67 61 69 6e 20 77 69 74  value, again wit
2f880 68 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 61 66  h.** affinity af
2f881 66 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74 2c  f applied to it,
2f882 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
2f883 49 66 20 6e 65 69 74 68 65 72 20 6f 66 20 74 68  If neither of th
2f884 65 20 61 62 6f 76 65 20 61 70 70 6c 79 2c 20 73  e above apply, s
2f885 65 74 20 2a 70 70 20 74 6f 20 4e 55 4c 4c 2e 0a  et *pp to NULL..
2f886 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
2f887 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e  r occurs, return
2f888 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20   an error code. 
2f889 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54  Otherwise, SQLIT
2f88a 45 5f 4f 4b 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  E_OK..*/.#ifdef 
2f88b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2f88c 41 54 32 0a 73 74 61 74 69 63 20 69 6e 74 20 76  AT2.static int v
2f88d 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20  alueFromExpr(.  
2f88e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
2f88f 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a    Expr *pExpr, .
2f890 20 20 75 38 20 61 66 66 2c 20 0a 20 20 73 71 6c    u8 aff, .  sql
2f891 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 0a  ite3_value **pp.
2f892 29 7b 0a 20 20 2f 2a 20 54 68 65 20 65 76 61 6c  ){.  /* The eval
2f893 43 6f 6e 73 74 45 78 70 72 28 29 20 66 75 6e 63  ConstExpr() func
2f894 74 69 6f 6e 20 77 69 6c 6c 20 68 61 76 65 20 61  tion will have a
2f895 6c 72 65 61 64 79 20 63 6f 6e 76 65 72 74 65 64  lready converted
2f896 20 61 6e 79 20 54 4b 5f 56 41 52 49 41 42 4c 45   any TK_VARIABLE
2f897 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  .  ** expression
2f898 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 61 6e 20   involved in an 
2f899 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 74 6f 20  comparison into 
2f89a 61 20 54 4b 5f 52 45 47 49 53 54 45 52 2e 20 2a  a TK_REGISTER. *
2f89b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  /.  assert( pExp
2f89c 72 2d 3e 6f 70 21 3d 54 4b 5f 56 41 52 49 41 42  r->op!=TK_VARIAB
2f89d 4c 45 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70  LE );.  if( pExp
2f89e 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  r->op==TK_REGIST
2f89f 45 52 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 32  ER && pExpr->op2
2f8a0 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b  ==TK_VARIABLE ){
2f8a1 0a 20 20 20 20 69 6e 74 20 69 56 61 72 20 3d 20  .    int iVar = 
2f8a2 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pExpr->iColumn;.
2f8a3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2f8a4 65 74 56 61 72 6d 61 73 6b 28 70 50 61 72 73 65  etVarmask(pParse
2f8a5 2d 3e 70 56 64 62 65 2c 20 69 56 61 72 29 3b 0a  ->pVdbe, iVar);.
2f8a6 20 20 20 20 2a 70 70 20 3d 20 73 71 6c 69 74 65      *pp = sqlite
2f8a7 33 56 64 62 65 47 65 74 56 61 6c 75 65 28 70 50  3VdbeGetValue(pP
2f8a8 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72 65  arse->pReprepare
2f8a9 2c 20 69 56 61 72 2c 20 61 66 66 29 3b 0a 20 20  , iVar, aff);.  
2f8aa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f8ab 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OK;.  }.  return
2f8ac 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
2f8ad 6d 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64 62  mExpr(pParse->db
2f8ae 2c 20 70 45 78 70 72 2c 20 53 51 4c 49 54 45 5f  , pExpr, SQLITE_
2f8af 55 54 46 38 2c 20 61 66 66 2c 20 70 70 29 3b 0a  UTF8, aff, pp);.
2f8b0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2f8b1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2f8b2 20 75 73 65 64 20 74 6f 20 65 73 74 69 6d 61 74   used to estimat
2f8b3 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
2f8b4 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62  rows that will b
2f8b5 65 20 76 69 73 69 74 65 64 0a 2a 2a 20 62 79 20  e visited.** by 
2f8b6 73 63 61 6e 6e 69 6e 67 20 61 6e 20 69 6e 64 65  scanning an inde
2f8b7 78 20 66 6f 72 20 61 20 72 61 6e 67 65 20 6f 66  x for a range of
2f8b8 20 76 61 6c 75 65 73 2e 20 54 68 65 20 72 61 6e   values. The ran
2f8b9 67 65 20 6d 61 79 20 68 61 76 65 20 61 6e 20 75  ge may have an u
2f8ba 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 2c 20 61  pper.** bound, a
2f8bb 20 6c 6f 77 65 72 20 62 6f 75 6e 64 2c 20 6f 72   lower bound, or
2f8bc 20 62 6f 74 68 2e 20 54 68 65 20 57 48 45 52 45   both. The WHERE
2f8bd 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 74 68   clause terms th
2f8be 61 74 20 73 65 74 20 74 68 65 20 75 70 70 65 72  at set the upper
2f8bf 0a 2a 2a 20 61 6e 64 20 6c 6f 77 65 72 20 62 6f  .** and lower bo
2f8c0 75 6e 64 73 20 61 72 65 20 72 65 70 72 65 73 65  unds are represe
2f8c1 6e 74 65 64 20 62 79 20 70 4c 6f 77 65 72 20 61  nted by pLower a
2f8c2 6e 64 20 70 55 70 70 65 72 20 72 65 73 70 65 63  nd pUpper respec
2f8c3 74 69 76 65 6c 79 2e 20 46 6f 72 0a 2a 2a 20 65  tively. For.** e
2f8c4 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67  xample, assuming
2f8c5 20 74 68 61 74 20 69 6e 64 65 78 20 70 20 69 73   that index p is
2f8c6 20 6f 6e 20 74 31 28 61 29 3a 0a 2a 2a 0a 2a 2a   on t1(a):.**.**
2f8c7 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57     ... FROM t1 W
2f8c8 48 45 52 45 20 61 20 3e 20 3f 20 41 4e 44 20 61  HERE a > ? AND a
2f8c9 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20   < ? ....**     
2f8ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
2f8cb 5f 5f 5f 5f 5f 7c 20 20 20 7c 5f 5f 5f 5f 5f 7c  _____|   |_____|
2f8cc 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2f8cd 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20            |     
2f8ce 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20      |.**        
2f8cf 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 6f               pLo
2f8d0 77 65 72 20 20 20 20 70 55 70 70 65 72 0a 2a 2a  wer    pUpper.**
2f8d1 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 66  .** If either of
2f8d2 20 74 68 65 20 75 70 70 65 72 20 6f 72 20 6c 6f   the upper or lo
2f8d3 77 65 72 20 62 6f 75 6e 64 20 69 73 20 6e 6f 74  wer bound is not
2f8d4 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 4e   present, then N
2f8d5 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 69 6e  ULL is passed in
2f8d6 0a 2a 2a 20 70 6c 61 63 65 20 6f 66 20 74 68 65  .** place of the
2f8d7 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 57   corresponding W
2f8d8 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20  hereTerm..**.** 
2f8d9 54 68 65 20 6e 45 71 20 70 61 72 61 6d 65 74 65  The nEq paramete
2f8da 72 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20  r is passed the 
2f8db 69 6e 64 65 78 20 6f 66 20 74 68 65 20 69 6e 64  index of the ind
2f8dc 65 78 20 63 6f 6c 75 6d 6e 20 73 75 62 6a 65 63  ex column subjec
2f8dd 74 20 74 6f 20 74 68 65 0a 2a 2a 20 72 61 6e 67  t to the.** rang
2f8de 65 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 4f 72  e constraint. Or
2f8df 2c 20 65 71 75 69 76 61 6c 65 6e 74 6c 79 2c 20  , equivalently, 
2f8e0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 71  the number of eq
2f8e1 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2f8e2 74 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 20  ts.** optimized 
2f8e3 62 79 20 74 68 65 20 70 72 6f 70 6f 73 65 64 20  by the proposed 
2f8e4 69 6e 64 65 78 20 73 63 61 6e 2e 20 46 6f 72 20  index scan. For 
2f8e5 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 69 6e  example, assumin
2f8e6 67 20 69 6e 64 65 78 20 70 20 69 73 0a 2a 2a 20  g index p is.** 
2f8e7 6f 6e 20 74 31 28 61 2c 20 62 29 2c 20 61 6e 64  on t1(a, b), and
2f8e8 20 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 69   the SQL query i
2f8e9 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46  s:.**.**   ... F
2f8ea 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d  ROM t1 WHERE a =
2f8eb 20 3f 20 41 4e 44 20 62 20 3e 20 3f 20 41 4e 44   ? AND b > ? AND
2f8ec 20 62 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 0a 2a 2a   b < ? ....**.**
2f8ed 20 74 68 65 6e 20 6e 45 71 20 73 68 6f 75 6c 64   then nEq should
2f8ee 20 62 65 20 70 61 73 73 65 64 20 74 68 65 20 76   be passed the v
2f8ef 61 6c 75 65 20 31 20 28 61 73 20 74 68 65 20 72  alue 1 (as the r
2f8f0 61 6e 67 65 20 72 65 73 74 72 69 63 74 65 64 20  ange restricted 
2f8f1 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 62 2c 20 69 73  column,.** b, is
2f8f2 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65 66 74   the second left
2f8f3 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  -most column of 
2f8f4 74 68 65 20 69 6e 64 65 78 29 2e 20 4f 72 2c 20  the index). Or, 
2f8f5 69 66 20 74 68 65 20 71 75 65 72 79 20 69 73 3a  if the query is:
2f8f6 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f  .**.**   ... FRO
2f8f7 4d 20 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f  M t1 WHERE a > ?
2f8f8 20 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a   AND a < ? ....*
2f8f9 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20 73 68  *.** then nEq sh
2f8fa 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 30  ould be passed 0
2f8fb 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
2f8fc 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 61 6e  rned value is an
2f8fd 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
2f8fe 20 31 20 61 6e 64 20 31 30 30 2c 20 69 6e 63 6c   1 and 100, incl
2f8ff 75 73 69 76 65 2e 20 41 20 72 65 74 75 72 6e 0a  usive. A return.
2f900 2a 2a 20 76 61 6c 75 65 20 6f 66 20 31 20 69 6e  ** value of 1 in
2f901 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65  dicates that the
2f902 20 70 72 6f 70 6f 73 65 64 20 72 61 6e 67 65 20   proposed range 
2f903 73 63 61 6e 20 69 73 20 65 78 70 65 63 74 65 64  scan is expected
2f904 20 74 6f 20 76 69 73 69 74 0a 2a 2a 20 61 70 70   to visit.** app
2f905 72 6f 78 69 6d 61 74 65 6c 79 20 31 2f 31 30 30  roximately 1/100
2f906 74 68 20 28 31 25 29 20 6f 66 20 74 68 65 20 72  th (1%) of the r
2f907 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20  ows selected by 
2f908 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 79  the nEq equality
2f909 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  .** constraints 
2f90a 28 69 66 20 61 6e 79 29 2e 20 41 20 72 65 74 75  (if any). A retu
2f90b 72 6e 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20  rn value of 100 
2f90c 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 69  indicates that i
2f90d 74 20 69 73 20 65 78 70 65 63 74 65 64 0a 2a 2a  t is expected.**
2f90e 20 74 68 61 74 20 74 68 65 20 72 61 6e 67 65 20   that the range 
2f90f 73 63 61 6e 20 77 69 6c 6c 20 76 69 73 69 74 20  scan will visit 
2f910 65 76 65 72 79 20 72 6f 77 20 28 31 30 30 25 29  every row (100%)
2f911 20 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65   selected by the
2f912 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e   equality.** con
2f913 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  straints..**.** 
2f914 49 6e 20 74 68 65 20 61 62 73 65 6e 63 65 20 6f  In the absence o
2f915 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 41  f sqlite_stat2 A
2f916 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20 65 61 63  NALYZE data, eac
2f917 68 20 72 61 6e 67 65 20 69 6e 65 71 75 61 6c 69  h range inequali
2f918 74 79 0a 2a 2a 20 72 65 64 75 63 65 73 20 74 68  ty.** reduces th
2f919 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20 62  e search space b
2f91a 79 20 32 2f 33 72 64 73 2e 20 20 48 65 6e 63 65  y 2/3rds.  Hence
2f91b 20 61 20 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72   a single constr
2f91c 61 69 6e 74 20 28 78 3e 3f 29 0a 2a 2a 20 72 65  aint (x>?).** re
2f91d 73 75 6c 74 73 20 69 6e 20 61 20 72 65 74 75 72  sults in a retur
2f91e 6e 20 6f 66 20 33 33 20 61 6e 64 20 61 20 72 61  n of 33 and a ra
2f91f 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 28  nge constraint (
2f920 78 3e 3f 20 41 4e 44 20 78 3c 3f 29 20 72 65 73  x>? AND x<?) res
2f921 75 6c 74 73 0a 2a 2a 20 69 6e 20 61 20 72 65 74  ults.** in a ret
2f922 75 72 6e 20 6f 66 20 31 31 2e 0a 2a 2f 0a 73 74  urn of 11..*/.st
2f923 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 52 61  atic int whereRa
2f924 6e 67 65 53 63 61 6e 45 73 74 28 0a 20 20 50 61  ngeScanEst(.  Pa
2f925 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2f926 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 26 20     /* Parsing & 
2f927 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
2f928 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64  context */.  Ind
2f929 65 78 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ex *p,          
2f92a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 63    /* The index c
2f92b 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 61  ontaining the ra
2f92c 6e 67 65 2d 63 6f 6d 70 61 72 65 64 20 63 6f 6c  nge-compared col
2f92d 75 6d 6e 3b 20 22 78 22 20 2a 2f 0a 20 20 69 6e  umn; "x" */.  in
2f92e 74 20 6e 45 71 2c 20 20 20 20 20 20 20 20 20 20  t nEq,          
2f92f 20 20 20 2f 2a 20 69 6e 64 65 78 20 69 6e 74 6f     /* index into
2f930 20 70 2d 3e 61 43 6f 6c 5b 5d 20 6f 66 20 74 68   p->aCol[] of th
2f931 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72 65 64  e range-compared
2f932 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 57 68 65   column */.  Whe
2f933 72 65 54 65 72 6d 20 2a 70 4c 6f 77 65 72 2c 20  reTerm *pLower, 
2f934 20 20 2f 2a 20 4c 6f 77 65 72 20 62 6f 75 6e 64    /* Lower bound
2f935 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e 20 65   on the range. e
2f936 78 3a 20 22 78 3e 31 32 33 22 20 4d 69 67 68 74  x: "x>123" Might
2f937 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 57 68   be NULL */.  Wh
2f938 65 72 65 54 65 72 6d 20 2a 70 55 70 70 65 72 2c  ereTerm *pUpper,
2f939 20 20 20 2f 2a 20 55 70 70 65 72 20 62 6f 75 6e     /* Upper boun
2f93a 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e 20  d on the range. 
2f93b 65 78 3a 20 22 78 3c 34 35 35 22 20 4d 69 67 68  ex: "x<455" Migh
2f93c 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  t be NULL */.  i
2f93d 6e 74 20 2a 70 69 45 73 74 20 20 20 20 20 20 20  nt *piEst       
2f93e 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75      /* OUT: Retu
2f93f 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 29 7b 0a 20  rn value */.){. 
2f940 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2f941 5f 4f 4b 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  _OK;..#ifdef SQL
2f942 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
2f943 0a 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30 20 26  ..  if( nEq==0 &
2f944 26 20 70 2d 3e 61 53 61 6d 70 6c 65 20 29 7b 0a  & p->aSample ){.
2f945 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2f946 65 20 2a 70 4c 6f 77 65 72 56 61 6c 20 3d 20 30  e *pLowerVal = 0
2f947 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ;.    sqlite3_va
2f948 6c 75 65 20 2a 70 55 70 70 65 72 56 61 6c 20 3d  lue *pUpperVal =
2f949 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 45 73 74   0;.    int iEst
2f94a 3b 0a 20 20 20 20 69 6e 74 20 69 4c 6f 77 65 72  ;.    int iLower
2f94b 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 55   = 0;.    int iU
2f94c 70 70 65 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e  pper = SQLITE_IN
2f94d 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20  DEX_SAMPLES;.   
2f94e 20 75 38 20 61 66 66 20 3d 20 70 2d 3e 70 54 61   u8 aff = p->pTa
2f94f 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d 3e 61 69 43  ble->aCol[p->aiC
2f950 6f 6c 75 6d 6e 5b 30 5d 5d 2e 61 66 66 69 6e 69  olumn[0]].affini
2f951 74 79 3b 0a 0a 20 20 20 20 69 66 28 20 70 4c 6f  ty;..    if( pLo
2f952 77 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78 70  wer ){.      Exp
2f953 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 6f 77 65  r *pExpr = pLowe
2f954 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  r->pExpr->pRight
2f955 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 76 61 6c  ;.      rc = val
2f956 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61 72 73  ueFromExpr(pPars
2f957 65 2c 20 70 45 78 70 72 2c 20 61 66 66 2c 20 26  e, pExpr, aff, &
2f958 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20  pLowerVal);.    
2f959 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
2f95a 4c 49 54 45 5f 4f 4b 20 26 26 20 70 55 70 70 65  LITE_OK && pUppe
2f95b 72 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  r ){.      Expr 
2f95c 2a 70 45 78 70 72 20 3d 20 70 55 70 70 65 72 2d  *pExpr = pUpper-
2f95d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a  >pExpr->pRight;.
2f95e 20 20 20 20 20 20 72 63 20 3d 20 76 61 6c 75 65        rc = value
2f95f 46 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65 2c  FromExpr(pParse,
2f960 20 70 45 78 70 72 2c 20 61 66 66 2c 20 26 70 55   pExpr, aff, &pU
2f961 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 7d 0a  pperVal);.    }.
2f962 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2f963 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 4c 6f 77 65  ITE_OK || (pLowe
2f964 72 56 61 6c 3d 3d 30 20 26 26 20 70 55 70 70 65  rVal==0 && pUppe
2f965 72 56 61 6c 3d 3d 30 29 20 29 7b 0a 20 20 20 20  rVal==0) ){.    
2f966 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
2f967 65 65 28 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20  ee(pLowerVal);. 
2f968 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
2f969 65 46 72 65 65 28 70 55 70 70 65 72 56 61 6c 29  eFree(pUpperVal)
2f96a 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 72 61 6e  ;.      goto ran
2f96b 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3b  ge_est_fallback;
2f96c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
2f96d 4c 6f 77 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20  LowerVal==0 ){. 
2f96e 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 52       rc = whereR
2f96f 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73  angeRegion(pPars
2f970 65 2c 20 70 2c 20 70 55 70 70 65 72 56 61 6c 2c  e, p, pUpperVal,
2f971 20 26 69 55 70 70 65 72 29 3b 0a 20 20 20 20 20   &iUpper);.     
2f972 20 69 66 28 20 70 4c 6f 77 65 72 20 29 20 69 4c   if( pLower ) iL
2f973 6f 77 65 72 20 3d 20 69 55 70 70 65 72 2f 32 3b  ower = iUpper/2;
2f974 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
2f975 55 70 70 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20  UpperVal==0 ){. 
2f976 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 52       rc = whereR
2f977 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73  angeRegion(pPars
2f978 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61 6c 2c  e, p, pLowerVal,
2f979 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20   &iLower);.     
2f97a 20 69 66 28 20 70 55 70 70 65 72 20 29 20 69 55   if( pUpper ) iU
2f97b 70 70 65 72 20 3d 20 28 69 4c 6f 77 65 72 20 2b  pper = (iLower +
2f97c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
2f97d 4d 50 4c 45 53 20 2b 20 31 29 2f 32 3b 0a 20 20  MPLES + 1)/2;.  
2f97e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
2f97f 63 20 3d 20 77 68 65 72 65 52 61 6e 67 65 52 65  c = whereRangeRe
2f980 67 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 2c 20  gion(pParse, p, 
2f981 70 55 70 70 65 72 56 61 6c 2c 20 26 69 55 70 70  pUpperVal, &iUpp
2f982 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  er);.      if( r
2f983 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2f984 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 68 65          rc = whe
2f985 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50  reRangeRegion(pP
2f986 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56  arse, p, pLowerV
2f987 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20  al, &iLower);.  
2f988 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2f989 20 69 45 73 74 20 3d 20 69 55 70 70 65 72 20 2d   iEst = iUpper -
2f98a 20 69 4c 6f 77 65 72 3b 0a 20 20 20 20 74 65 73   iLower;.    tes
2f98b 74 63 61 73 65 28 20 69 45 73 74 3d 3d 53 51 4c  tcase( iEst==SQL
2f98c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
2f98d 53 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  S );.    assert(
2f98e 20 69 45 73 74 3c 3d 53 51 4c 49 54 45 5f 49 4e   iEst<=SQLITE_IN
2f98f 44 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b 0a 20  DEX_SAMPLES );. 
2f990 20 20 20 69 66 28 20 69 45 73 74 3c 31 20 29 7b     if( iEst<1 ){
2f991 0a 20 20 20 20 20 20 69 45 73 74 20 3d 20 31 3b  .      iEst = 1;
2f992 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
2f993 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 4c 6f  te3ValueFree(pLo
2f994 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c  werVal);.    sql
2f995 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 55  ite3ValueFree(pU
2f996 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 2a 70  pperVal);.    *p
2f997 69 45 73 74 20 3d 20 28 69 45 73 74 20 2a 20 31  iEst = (iEst * 1
2f998 30 30 29 2f 53 51 4c 49 54 45 5f 49 4e 44 45 58  00)/SQLITE_INDEX
2f999 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 72 65  _SAMPLES;.    re
2f99a 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 72 61 6e  turn rc;.  }.ran
2f99b 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3a  ge_est_fallback:
2f99c 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
2f99d 50 41 52 41 4d 45 54 45 52 28 70 50 61 72 73 65  PARAMETER(pParse
2f99e 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
2f99f 4d 45 54 45 52 28 70 29 3b 0a 20 20 55 4e 55 53  METER(p);.  UNUS
2f9a0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 45 71  ED_PARAMETER(nEq
2f9a1 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65  );.#endif.  asse
2f9a2 72 74 28 20 70 4c 6f 77 65 72 20 7c 7c 20 70 55  rt( pLower || pU
2f9a3 70 70 65 72 20 29 3b 0a 20 20 69 66 28 20 70 4c  pper );.  if( pL
2f9a4 6f 77 65 72 20 26 26 20 70 55 70 70 65 72 20 29  ower && pUpper )
2f9a5 7b 0a 20 20 20 20 2a 70 69 45 73 74 20 3d 20 31  {.    *piEst = 1
2f9a6 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
2f9a7 2a 70 69 45 73 74 20 3d 20 33 33 3b 0a 20 20 7d  *piEst = 33;.  }
2f9a8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2f9a9 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
2f9aa 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20   query plan for 
2f9ab 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74  accessing a part
2f9ac 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 57  icular table.  W
2f9ad 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74  rite the.** best
2f9ae 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20   query plan and 
2f9af 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68  its cost into th
2f9b0 65 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65  e WhereCost obje
2f9b1 63 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 74  ct supplied as t
2f9b2 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 72 61 6d  he.** last param
2f9b3 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eter..**.** The 
2f9b4 6c 6f 77 65 73 74 20 63 6f 73 74 20 70 6c 61 6e  lowest cost plan
2f9b5 20 77 69 6e 73 2e 20 20 54 68 65 20 63 6f 73 74   wins.  The cost
2f9b6 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
2f9b7 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  of the amount of
2f9b8 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64 69 73 6b  .** CPU and disk
2f9b9 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70 72 6f   I/O need to pro
2f9ba 63 65 73 73 20 74 68 65 20 72 65 71 75 65 73 74  cess the request
2f9bb 20 75 73 69 6e 67 20 74 68 65 20 73 65 6c 65 63   using the selec
2f9bc 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20 46 61 63  ted plan..** Fac
2f9bd 74 6f 72 73 20 74 68 61 74 20 69 6e 66 6c 75 65  tors that influe
2f9be 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c 75 64 65  nce cost include
2f9bf 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 54 68  :.**.**    *  Th
2f9c0 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d 62  e estimated numb
2f9c1 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
2f9c2 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76 65  will be retrieve
2f9c3 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20 20 20  d.  (The.**     
2f9c4 20 20 66 65 77 65 72 20 74 68 65 20 62 65 74 74    fewer the bett
2f9c5 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  er.).**.**    * 
2f9c6 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
2f9c7 73 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f 63 63  sorting must occ
2f9c8 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  ur..**.**    *  
2f9c9 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  Whether or not t
2f9ca 68 65 72 65 20 6d 75 73 74 20 62 65 20 73 65 70  here must be sep
2f9cb 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20 69 6e  arate lookups in
2f9cc 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e   the.**       in
2f9cd 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20 6d  dex and in the m
2f9ce 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ain table..**.**
2f9cf 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 6e   If there was an
2f9d0 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
2f9d1 73 65 20 28 70 53 72 63 2d 3e 70 49 6e 64 65 78  se (pSrc->pIndex
2f9d2 29 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  ) attached to th
2f9d3 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68  e table in.** th
2f9d4 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c  e SQL statement,
2f9d5 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
2f9d6 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65  ion only conside
2f9d7 72 73 20 70 6c 61 6e 73 20 75 73 69 6e 67 20 74  rs plans using t
2f9d8 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e 64  he .** named ind
2f9d9 65 78 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 70  ex. If no such p
2f9da 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  lan is found, th
2f9db 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  en the returned 
2f9dc 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c 49 54  cost is.** SQLIT
2f9dd 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20 61 20  E_BIG_DBL. If a 
2f9de 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 20 74 68  plan is found th
2f9df 61 74 20 75 73 65 73 20 74 68 65 20 6e 61 6d 65  at uses the name
2f9e0 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20 74 68 65  d index, .** the
2f9e1 6e 20 74 68 65 20 63 6f 73 74 20 69 73 20 63 61  n the cost is ca
2f9e2 6c 63 75 6c 61 74 65 64 20 69 6e 20 74 68 65 20  lculated in the 
2f9e3 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a 2a  usual way..**.**
2f9e4 20 49 66 20 61 20 4e 4f 54 20 49 4e 44 45 58 45   If a NOT INDEXE
2f9e5 44 20 63 6c 61 75 73 65 20 28 70 53 72 63 2d 3e  D clause (pSrc->
2f9e6 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30 29 20 77  notIndexed!=0) w
2f9e7 61 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  as attached to t
2f9e8 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6e 20  he table .** in 
2f9e9 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2f9ea 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69 6e  ment, then no in
2f9eb 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69 64  dexes are consid
2f9ec 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74  ered. However, t
2f9ed 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65 64 20  he .** selected 
2f9ee 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c 20 74  plan may still t
2f9ef 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66  ake advantage of
2f9f0 20 74 68 65 20 74 61 62 6c 65 73 20 62 75 69 6c   the tables buil
2f9f1 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a 20 69 6e  t-in rowid.** in
2f9f2 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  dex..*/.static v
2f9f3 6f 69 64 20 62 65 73 74 42 74 72 65 65 49 6e 64  oid bestBtreeInd
2f9f4 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2f9f5 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2f9f6 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
2f9f7 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2f9f8 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2f9f9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2f9fa 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2f9fb 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2f9fc 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f  t_item *pSrc,  /
2f9fd 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * The FROM claus
2f9fe 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68  e term to search
2f9ff 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2fa00 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20  tReady,         
2fa01 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
2fa02 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f  sors that are no
2fa03 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
2fa04 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2fa05 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rBy,         /* 
2fa06 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2fa07 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f  use */.  WhereCo
2fa08 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20  st *pCost       
2fa09 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63       /* Lowest c
2fa0a 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a  ost query plan *
2fa0b 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 43 75 72 20  /.){.  int iCur 
2fa0c 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b  = pSrc->iCursor;
2fa0d 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
2fa0e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2fa0f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
2fa10 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65 3b    Index *pProbe;
2fa11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fa12 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72 65   An index we are
2fa13 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a 20   evaluating */. 
2fa14 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
2fa15 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fa16 43 6f 70 79 20 6f 66 20 70 50 72 6f 62 65 2c 20  Copy of pProbe, 
2fa17 6f 72 20 7a 65 72 6f 20 66 6f 72 20 49 50 4b 20  or zero for IPK 
2fa18 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 65  index */.  int e
2fa19 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
2fa1a 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
2fa1b 74 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20  t mask of valid 
2fa1c 65 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f  equality operato
2fa1d 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 45  rs */.  int idxE
2fa1e 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
2fa1f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6d 61 73      /* Index mas
2fa20 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c  k of valid equal
2fa21 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f  ity operators */
2fa22 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b 20 20 20  .  Index sPk;   
2fa23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fa24 2a 20 41 20 66 61 6b 65 20 69 6e 64 65 78 20 6f  * A fake index o
2fa25 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 70 72  bject for the pr
2fa26 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 75  imary key */.  u
2fa27 6e 73 69 67 6e 65 64 20 69 6e 74 20 61 69 52 6f  nsigned int aiRo
2fa28 77 45 73 74 50 6b 5b 32 5d 3b 20 2f 2a 20 54 68  wEstPk[2]; /* Th
2fa29 65 20 61 69 52 6f 77 45 73 74 5b 5d 20 76 61 6c  e aiRowEst[] val
2fa2a 75 65 20 66 6f 72 20 74 68 65 20 73 50 6b 20 69  ue for the sPk i
2fa2b 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 69  ndex */.  int ai
2fa2c 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20 20  ColumnPk = -1;  
2fa2d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 43 6f        /* The aCo
2fa2e 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66 6f 72  lumn[] value for
2fa2f 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20 2a   the sPk index *
2fa30 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 4d 61  /.  int wsFlagMa
2fa31 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
2fa32 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61 67 73  /* Allowed flags
2fa33 20 69 6e 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e   in pCost->plan.
2fa34 77 73 46 6c 61 67 20 2a 2f 0a 0a 20 20 2f 2a 20  wsFlag */..  /* 
2fa35 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  Initialize the c
2fa36 6f 73 74 20 74 6f 20 61 20 77 6f 72 73 74 2d 63  ost to a worst-c
2fa37 61 73 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 6d  ase value */.  m
2fa38 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20  emset(pCost, 0, 
2fa39 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b  sizeof(*pCost));
2fa3a 0a 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20  .  pCost->rCost 
2fa3b 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  = SQLITE_BIG_DBL
2fa3c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70  ;..  /* If the p
2fa3d 53 72 63 20 74 61 62 6c 65 20 69 73 20 74 68 65  Src table is the
2fa3e 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
2fa3f 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e  a LEFT JOIN then
2fa40 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a   we may not.  **
2fa41 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20 74 6f   use an index to
2fa42 20 73 61 74 69 73 66 79 20 49 53 20 4e 55 4c 4c   satisfy IS NULL
2fa43 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20   constraints on 
2fa44 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 69  that table.  Thi
2fa45 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61 75 73  s is.  ** becaus
2fa46 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20  e columns might 
2fa47 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e 55 4c  end up being NUL
2fa48 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 64  L if the table d
2fa49 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a  oes not match -.
2fa4a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73 74 61    ** a circumsta
2fa4b 6e 63 65 20 77 68 69 63 68 20 74 68 65 20 69 6e  nce which the in
2fa4c 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20  dex cannot help 
2fa4d 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20 54 69  us discover.  Ti
2fa4e 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f  cket #2177..  */
2fa4f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6a 6f 69  .  if( pSrc->joi
2fa50 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 20  ntype & JT_LEFT 
2fa51 29 7b 0a 20 20 20 20 69 64 78 45 71 54 65 72 6d  ){.    idxEqTerm
2fa52 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f  Mask = WO_EQ|WO_
2fa53 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  IN;.  }else{.   
2fa54 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 20 3d   idxEqTermMask =
2fa55 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f   WO_EQ|WO_IN|WO_
2fa56 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 69  ISNULL;.  }..  i
2fa57 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 20  f( pSrc->pIndex 
2fa58 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 49 4e 44  ){.    /* An IND
2fa59 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 73  EXED BY clause s
2fa5a 70 65 63 69 66 69 65 73 20 61 20 70 61 72 74 69  pecifies a parti
2fa5b 63 75 6c 61 72 20 69 6e 64 65 78 20 74 6f 20 75  cular index to u
2fa5c 73 65 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d  se */.    pIdx =
2fa5d 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e   pProbe = pSrc->
2fa5e 70 49 6e 64 65 78 3b 0a 20 20 20 20 77 73 46 6c  pIndex;.    wsFl
2fa5f 61 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45 52 45  agMask = ~(WHERE
2fa60 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f  _ROWID_EQ|WHERE_
2fa61 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20  ROWID_RANGE);.  
2fa62 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 69    eqTermMask = i
2fa63 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20  dxEqTermMask;.  
2fa64 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
2fa65 65 72 65 20 69 73 20 6e 6f 20 49 4e 44 45 58 45  ere is no INDEXE
2fa66 44 20 42 59 20 63 6c 61 75 73 65 2e 20 20 43 72  D BY clause.  Cr
2fa67 65 61 74 65 20 61 20 66 61 6b 65 20 49 6e 64 65  eate a fake Inde
2fa68 78 20 6f 62 6a 65 63 74 20 74 6f 0a 20 20 20 20  x object to.    
2fa69 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 74 68 65  ** represent the
2fa6a 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
2fa6b 20 20 20 20 49 6e 64 65 78 20 2a 70 46 69 72 73      Index *pFirs
2fa6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
2fa6d 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20 69    /* Any other i
2fa6e 6e 64 65 78 20 6f 6e 20 74 68 65 20 74 61 62 6c  ndex on the tabl
2fa6f 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28  e */.    memset(
2fa70 26 73 50 6b 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sPk, 0, sizeof(
2fa71 49 6e 64 65 78 29 29 3b 0a 20 20 20 20 73 50 6b  Index));.    sPk
2fa72 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20  .nColumn = 1;.  
2fa73 20 20 73 50 6b 2e 61 69 43 6f 6c 75 6d 6e 20 3d    sPk.aiColumn =
2fa74 20 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b 0a 20 20   &aiColumnPk;.  
2fa75 20 20 73 50 6b 2e 61 69 52 6f 77 45 73 74 20 3d    sPk.aiRowEst =
2fa76 20 61 69 52 6f 77 45 73 74 50 6b 3b 0a 20 20 20   aiRowEstPk;.   
2fa77 20 61 69 52 6f 77 45 73 74 50 6b 5b 31 5d 20 3d   aiRowEstPk[1] =
2fa78 20 31 3b 0a 20 20 20 20 73 50 6b 2e 6f 6e 45 72   1;.    sPk.onEr
2fa79 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65  ror = OE_Replace
2fa7a 3b 0a 20 20 20 20 73 50 6b 2e 70 54 61 62 6c 65  ;.    sPk.pTable
2fa7b 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20   = pSrc->pTab;. 
2fa7c 20 20 20 70 46 69 72 73 74 20 3d 20 70 53 72 63     pFirst = pSrc
2fa7d 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a  ->pTab->pIndex;.
2fa7e 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 6f      if( pSrc->no
2fa7f 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 7b 0a 20  tIndexed==0 ){. 
2fa80 20 20 20 20 20 73 50 6b 2e 70 4e 65 78 74 20 3d       sPk.pNext =
2fa81 20 70 46 69 72 73 74 3b 0a 20 20 20 20 7d 0a 20   pFirst;.    }. 
2fa82 20 20 20 2f 2a 20 54 68 65 20 61 69 52 6f 77 45     /* The aiRowE
2fa83 73 74 50 6b 5b 30 5d 20 69 73 20 61 6e 20 65 73  stPk[0] is an es
2fa84 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 74 6f  timate of the to
2fa85 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  tal number of ro
2fa86 77 73 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ws in the.    **
2fa87 20 74 61 62 6c 65 2e 20 20 47 65 74 20 74 68 69   table.  Get thi
2fa88 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  s information fr
2fa89 6f 6d 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 69  om the ANALYZE i
2fa8a 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 69 74  nformation if it
2fa8b 20 69 73 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c   is.    ** avail
2fa8c 61 62 6c 65 2e 20 20 49 66 20 6e 6f 74 20 61 76  able.  If not av
2fa8d 61 69 6c 61 62 6c 65 2c 20 61 73 73 75 6d 65 20  ailable, assume 
2fa8e 74 68 65 20 74 61 62 6c 65 20 31 20 6d 69 6c 6c  the table 1 mill
2fa8f 69 6f 6e 20 72 6f 77 73 20 69 6e 20 73 69 7a 65  ion rows in size
2fa90 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2fa91 20 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20   pFirst ){.     
2fa92 20 61 73 73 65 72 74 28 20 70 46 69 72 73 74 2d   assert( pFirst-
2fa93 3e 61 69 52 6f 77 45 73 74 21 3d 30 20 29 3b 20  >aiRowEst!=0 ); 
2fa94 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 74 6f 67  /* Allocated tog
2fa95 65 74 68 65 72 20 77 69 74 68 20 70 46 69 72 73  ether with pFirs
2fa96 74 20 2a 2f 0a 20 20 20 20 20 20 61 69 52 6f 77  t */.      aiRow
2fa97 45 73 74 50 6b 5b 30 5d 20 3d 20 70 46 69 72 73  EstPk[0] = pFirs
2fa98 74 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a  t->aiRowEst[0];.
2fa99 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2fa9a 20 61 69 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d   aiRowEstPk[0] =
2fa9b 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20 7d 0a   1000000;.    }.
2fa9c 20 20 20 20 70 50 72 6f 62 65 20 3d 20 26 73 50      pProbe = &sP
2fa9d 6b 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73  k;.    wsFlagMas
2fa9e 6b 20 3d 20 7e 28 0a 20 20 20 20 20 20 20 20 57  k = ~(.        W
2fa9f 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57  HERE_COLUMN_IN|W
2faa0 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57  HERE_COLUMN_EQ|W
2faa1 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
2faa2 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  |WHERE_COLUMN_RA
2faa3 4e 47 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 65  NGE.    );.    e
2faa4 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45  qTermMask = WO_E
2faa5 51 7c 57 4f 5f 49 4e 3b 0a 20 20 20 20 70 49 64  Q|WO_IN;.    pId
2faa6 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  x = 0;.  }..  /*
2faa7 20 4c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 69   Loop over all i
2faa8 6e 64 69 63 65 73 20 6c 6f 6f 6b 69 6e 67 20 66  ndices looking f
2faa9 6f 72 20 74 68 65 20 62 65 73 74 20 6f 6e 65 20  or the best one 
2faaa 74 6f 20 75 73 65 0a 20 20 2a 2f 0a 20 20 66 6f  to use.  */.  fo
2faab 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 49 64 78  r(; pProbe; pIdx
2faac 3d 70 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e  =pProbe=pProbe->
2faad 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 6f 6e 73  pNext){.    cons
2faae 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 2a  t unsigned int *
2faaf 20 63 6f 6e 73 74 20 61 69 52 6f 77 45 73 74 20   const aiRowEst 
2fab0 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45  = pProbe->aiRowE
2fab1 73 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63  st;.    double c
2fab2 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ost;            
2fab3 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75      /* Cost of u
2fab4 73 69 6e 67 20 70 50 72 6f 62 65 20 2a 2f 0a 20  sing pProbe */. 
2fab5 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20     double nRow; 
2fab6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fab7 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62  * Estimated numb
2fab8 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 72 65  er of rows in re
2fab9 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20  sult set */.    
2faba 69 6e 74 20 72 65 76 3b 20 20 20 20 20 20 20 20  int rev;        
2fabb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2fabc 72 75 65 20 74 6f 20 73 63 61 6e 20 69 6e 20 72  rue to scan in r
2fabd 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a  everse order */.
2fabe 20 20 20 20 69 6e 74 20 77 73 46 6c 61 67 73 20      int wsFlags 
2fabf 3d 20 30 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b  = 0;.    Bitmask
2fac0 20 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20   used = 0;..    
2fac1 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
2fac2 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 70   variables are p
2fac3 6f 70 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f  opulated based o
2fac4 6e 20 74 68 65 20 70 72 6f 70 65 72 74 69 65 73  n the properties
2fac5 20 6f 66 0a 20 20 20 20 2a 2a 20 73 63 61 6e 20   of.    ** scan 
2fac6 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 2e  being evaluated.
2fac7 20 54 68 65 79 20 61 72 65 20 74 68 65 6e 20 75   They are then u
2fac8 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
2fac9 20 74 68 65 20 65 78 70 65 63 74 65 64 0a 20 20   the expected.  
2faca 20 20 2a 2a 20 63 6f 73 74 20 61 6e 64 20 6e 75    ** cost and nu
2facb 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 72 65 74  mber of rows ret
2facc 75 72 6e 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  urned..    **.  
2facd 20 20 2a 2a 20 20 6e 45 71 3a 20 0a 20 20 20 20    **  nEq: .    
2face 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  **    Number of 
2facf 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20 74  equality terms t
2fad0 68 61 74 20 63 61 6e 20 62 65 20 69 6d 70 6c 65  hat can be imple
2fad1 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68 65  mented using the
2fad2 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a 20   index..    **. 
2fad3 20 20 20 2a 2a 20 20 6e 49 6e 4d 75 6c 3a 20 20     **  nInMul:  
2fad4 0a 20 20 20 20 2a 2a 20 20 20 20 54 68 65 20 22  .    **    The "
2fad5 69 6e 2d 6d 75 6c 74 69 70 6c 69 65 72 22 2e 20  in-multiplier". 
2fad6 54 68 69 73 20 69 73 20 61 6e 20 65 73 74 69 6d  This is an estim
2fad7 61 74 65 20 6f 66 20 68 6f 77 20 6d 61 6e 79 20  ate of how many 
2fad8 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 73 20  seek operations 
2fad9 0a 20 20 20 20 2a 2a 20 20 20 20 53 51 4c 69 74  .    **    SQLit
2fada 65 20 6d 75 73 74 20 70 65 72 66 6f 72 6d 20 6f  e must perform o
2fadb 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 71  n the index in q
2fadc 75 65 73 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61  uestion. For exa
2fadd 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 0a 20 20  mple, if the .  
2fade 20 20 2a 2a 20 20 20 20 57 48 45 52 45 20 63 6c    **    WHERE cl
2fadf 61 75 73 65 20 69 73 3a 0a 20 20 20 20 2a 2a 0a  ause is:.    **.
2fae0 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52      **      WHER
2fae1 45 20 61 20 49 4e 20 28 31 2c 20 32 2c 20 33 29  E a IN (1, 2, 3)
2fae2 20 41 4e 44 20 62 20 49 4e 20 28 34 2c 20 35 2c   AND b IN (4, 5,
2fae3 20 36 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a   6).    **.    *
2fae4 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75 73 74  *    SQLite must
2fae5 20 70 65 72 66 6f 72 6d 20 39 20 6c 6f 6f 6b 75   perform 9 looku
2fae6 70 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 6f  ps on an index o
2fae7 6e 20 28 61 2c 20 62 29 2c 20 73 6f 20 6e 49 6e  n (a, b), so nIn
2fae8 4d 75 6c 20 69 73 20 0a 20 20 20 20 2a 2a 20 20  Mul is .    **  
2fae9 20 20 73 65 74 20 74 6f 20 39 2e 20 47 69 76 65    set to 9. Give
2faea 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68 65 6d  n the same schem
2faeb 61 20 61 6e 64 20 65 69 74 68 65 72 20 6f 66 20  a and either of 
2faec 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 57 48  the following WH
2faed 45 52 45 20 0a 20 20 20 20 2a 2a 20 20 20 20 63  ERE .    **    c
2faee 6c 61 75 73 65 73 3a 0a 20 20 20 20 2a 2a 0a 20  lauses:.    **. 
2faef 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45     **      WHERE
2faf0 20 61 20 3d 20 20 31 0a 20 20 20 20 2a 2a 20 20   a =  1.    **  
2faf1 20 20 20 20 57 48 45 52 45 20 61 20 3e 3d 20 32      WHERE a >= 2
2faf2 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2faf3 20 20 6e 49 6e 4d 75 6c 20 69 73 20 73 65 74 20    nInMul is set 
2faf4 74 6f 20 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  to 1..    **.   
2faf5 20 2a 2a 20 20 20 20 49 66 20 74 68 65 72 65 20   **    If there 
2faf6 65 78 69 73 74 73 20 61 20 57 48 45 52 45 20 74  exists a WHERE t
2faf7 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  erm of the form 
2faf8 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  "x IN (SELECT ..
2faf9 2e 29 22 2c 20 74 68 65 6e 20 0a 20 20 20 20 2a  .)", then .    *
2fafa 2a 20 20 20 20 74 68 65 20 73 75 62 2d 73 65 6c  *    the sub-sel
2fafb 65 63 74 20 69 73 20 61 73 73 75 6d 65 64 20 74  ect is assumed t
2fafc 6f 20 72 65 74 75 72 6e 20 32 35 20 72 6f 77 73  o return 25 rows
2fafd 20 66 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   for the purpose
2fafe 73 20 6f 66 20 0a 20 20 20 20 2a 2a 20 20 20 20  s of .    **    
2faff 64 65 74 65 72 6d 69 6e 69 6e 67 20 6e 49 6e 4d  determining nInM
2fb00 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ul..    **.    *
2fb01 2a 20 20 62 49 6e 45 73 74 3a 20 20 0a 20 20 20  *  bInEst:  .   
2fb02 20 2a 2a 20 20 20 20 53 65 74 20 74 6f 20 74 72   **    Set to tr
2fb03 75 65 20 69 66 20 74 68 65 72 65 20 77 61 73 20  ue if there was 
2fb04 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 22 78 20  at least one "x 
2fb05 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
2fb06 20 74 65 72 6d 20 75 73 65 64 20 0a 20 20 20 20   term used .    
2fb07 2a 2a 20 20 20 20 69 6e 20 64 65 74 65 72 6d 69  **    in determi
2fb08 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
2fb09 66 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a  f nInMul..    **
2fb0a 0a 20 20 20 20 2a 2a 20 20 6e 42 6f 75 6e 64 3a  .    **  nBound:
2fb0b 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e 20 65 73  .    **    An es
2fb0c 74 69 6d 61 74 65 20 6f 6e 20 74 68 65 20 61 6d  timate on the am
2fb0d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 61 62 6c  ount of the tabl
2fb0e 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 73  e that must be s
2fb0f 65 61 72 63 68 65 64 2e 20 20 41 0a 20 20 20 20  earched.  A.    
2fb10 2a 2a 20 20 20 20 76 61 6c 75 65 20 6f 66 20 31  **    value of 1
2fb11 30 30 20 6d 65 61 6e 73 20 74 68 65 20 65 6e 74  00 means the ent
2fb12 69 72 65 20 74 61 62 6c 65 20 69 73 20 73 65 61  ire table is sea
2fb13 72 63 68 65 64 2e 20 20 52 61 6e 67 65 20 63 6f  rched.  Range co
2fb14 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a  nstraints.    **
2fb15 20 20 20 20 6d 69 67 68 74 20 72 65 64 75 63 65      might reduce
2fb16 20 74 68 69 73 20 74 6f 20 61 20 76 61 6c 75 65   this to a value
2fb17 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20 74   less than 100 t
2fb18 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
2fb19 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20 20 20 61  only.    **    a
2fb1a 20 66 72 61 63 74 69 6f 6e 20 6f 66 20 74 68 65   fraction of the
2fb1b 20 74 61 62 6c 65 20 6e 65 65 64 73 20 73 65 61   table needs sea
2fb1c 72 63 68 69 6e 67 2e 20 20 49 6e 20 74 68 65 20  rching.  In the 
2fb1d 61 62 73 65 6e 63 65 20 6f 66 0a 20 20 20 20 2a  absence of.    *
2fb1e 2a 20 20 20 20 73 71 6c 69 74 65 5f 73 74 61 74  *    sqlite_stat
2fb1f 32 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20  2 ANALYZE data, 
2fb20 61 20 73 69 6e 67 6c 65 20 69 6e 65 71 75 61 6c  a single inequal
2fb21 69 74 79 20 72 65 64 75 63 65 73 20 74 68 65 20  ity reduces the 
2fb22 73 65 61 72 63 68 0a 20 20 20 20 2a 2a 20 20 20  search.    **   
2fb23 20 73 70 61 63 65 20 74 6f 20 31 2f 33 72 64 20   space to 1/3rd 
2fb24 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  its original siz
2fb25 65 2e 20 20 53 6f 20 61 6e 20 78 3e 3f 20 63 6f  e.  So an x>? co
2fb26 6e 73 74 72 61 69 6e 74 20 72 65 64 75 63 65 73  nstraint reduces
2fb27 0a 20 20 20 20 2a 2a 20 20 20 20 6e 42 6f 75 6e  .    **    nBoun
2fb28 64 20 74 6f 20 33 33 2e 20 20 54 77 6f 20 63 6f  d to 33.  Two co
2fb29 6e 73 74 72 61 69 6e 74 73 20 28 78 3e 3f 20 41  nstraints (x>? A
2fb2a 4e 44 20 78 3c 3f 29 20 72 65 64 75 63 65 20 6e  ND x<?) reduce n
2fb2b 42 6f 75 6e 64 20 74 6f 20 31 31 2e 0a 20 20 20  Bound to 11..   
2fb2c 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 53 6f 72   **.    **  bSor
2fb2d 74 3a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 20  t:   .    **    
2fb2e 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66  Boolean. True if
2fb2f 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44   there is an ORD
2fb30 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 68 61  ER BY clause tha
2fb31 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61  t will require a
2fb32 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 65 78 74  n .    **    ext
2fb33 65 72 6e 61 6c 20 73 6f 72 74 20 28 69 2e 65 2e  ernal sort (i.e.
2fb34 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 69 6e   scanning the in
2fb35 64 65 78 20 62 65 69 6e 67 20 65 76 61 6c 75 61  dex being evalua
2fb36 74 65 64 20 77 69 6c 6c 20 6e 6f 74 20 0a 20 20  ted will not .  
2fb37 20 20 2a 2a 20 20 20 20 63 6f 72 72 65 63 74 6c    **    correctl
2fb38 79 20 6f 72 64 65 72 20 72 65 63 6f 72 64 73 29  y order records)
2fb39 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2fb3a 20 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20 20 20 2a   bLookup: .    *
2fb3b 2a 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72  *    Boolean. Tr
2fb3c 75 65 20 69 66 20 66 6f 72 20 65 61 63 68 20 69  ue if for each i
2fb3d 6e 64 65 78 20 65 6e 74 72 79 20 76 69 73 69 74  ndex entry visit
2fb3e 65 64 20 61 20 6c 6f 6f 6b 75 70 20 6f 6e 20 74  ed a lookup on t
2fb3f 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f  he .    **    co
2fb40 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c  rresponding tabl
2fb41 65 20 62 2d 74 72 65 65 20 69 73 20 72 65 71 75  e b-tree is requ
2fb42 69 72 65 64 2e 20 54 68 69 73 20 69 73 20 61 6c  ired. This is al
2fb43 77 61 79 73 20 66 61 6c 73 65 20 0a 20 20 20 20  ways false .    
2fb44 2a 2a 20 20 20 20 66 6f 72 20 74 68 65 20 72 6f  **    for the ro
2fb45 77 69 64 20 69 6e 64 65 78 2e 20 46 6f 72 20 6f  wid index. For o
2fb46 74 68 65 72 20 69 6e 64 65 78 65 73 2c 20 69 74  ther indexes, it
2fb47 20 69 73 20 74 72 75 65 20 75 6e 6c 65 73 73 20   is true unless 
2fb48 61 6c 6c 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  all the .    ** 
2fb49 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68     columns of th
2fb4a 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20  e table used by 
2fb4b 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
2fb4c 6d 65 6e 74 20 61 72 65 20 70 72 65 73 65 6e 74  ment are present
2fb4d 20 69 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 74   in .    **    t
2fb4e 68 65 20 69 6e 64 65 78 20 28 73 75 63 68 20 61  he index (such a
2fb4f 6e 20 69 6e 64 65 78 20 69 73 20 73 6f 6d 65 74  n index is somet
2fb50 69 6d 65 73 20 64 65 73 63 72 69 62 65 64 20 61  imes described a
2fb51 73 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64  s a covering ind
2fb52 65 78 29 2e 0a 20 20 20 20 2a 2a 20 20 20 20 46  ex)..    **    F
2fb53 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65  or example, give
2fb54 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 28  n the index on (
2fb55 61 2c 20 62 29 2c 20 74 68 65 20 73 65 63 6f 6e  a, b), the secon
2fb56 64 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d of the followi
2fb57 6e 67 20 0a 20 20 20 20 2a 2a 20 20 20 20 74 77  ng .    **    tw
2fb58 6f 20 71 75 65 72 69 65 73 20 72 65 71 75 69 72  o queries requir
2fb59 65 73 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20  es table b-tree 
2fb5a 6c 6f 6f 6b 75 70 73 2c 20 62 75 74 20 74 68 65  lookups, but the
2fb5b 20 66 69 72 73 74 20 64 6f 65 73 20 6e 6f 74 2e   first does not.
2fb5c 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2fb5d 20 20 20 20 20 20 20 20 20 20 20 53 45 4c 45 43             SELEC
2fb5e 54 20 61 2c 20 62 20 20 20 20 46 52 4f 4d 20 74  T a, b    FROM t
2fb5f 62 6c 20 57 48 45 52 45 20 61 20 3d 20 31 3b 0a  bl WHERE a = 1;.
2fb60 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2fb61 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62 2c 20     SELECT a, b, 
2fb62 63 20 46 52 4f 4d 20 74 62 6c 20 57 48 45 52 45  c FROM tbl WHERE
2fb63 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2f 0a 20   a = 1;.    */. 
2fb64 20 20 20 69 6e 74 20 6e 45 71 3b 0a 20 20 20 20     int nEq;.    
2fb65 69 6e 74 20 62 49 6e 45 73 74 20 3d 20 30 3b 0a  int bInEst = 0;.
2fb66 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c 20 3d      int nInMul =
2fb67 20 31 3b 0a 20 20 20 20 69 6e 74 20 6e 42 6f 75   1;.    int nBou
2fb68 6e 64 20 3d 20 31 30 30 3b 0a 20 20 20 20 69 6e  nd = 100;.    in
2fb69 74 20 62 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20  t bSort = 0;.   
2fb6a 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 20 3d 20 30   int bLookup = 0
2fb6b 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d  ;..    /* Determ
2fb6c 69 6e 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f  ine the values o
2fb6d 66 20 6e 45 71 20 61 6e 64 20 6e 49 6e 4d 75 6c  f nEq and nInMul
2fb6e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6e 45 71 3d   */.    for(nEq=
2fb6f 30 3b 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e  0; nEq<pProbe->n
2fb70 43 6f 6c 75 6d 6e 3b 20 6e 45 71 2b 2b 29 7b 0a  Column; nEq++){.
2fb71 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2fb72 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *pTerm;         
2fb73 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65    /* A single te
2fb74 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
2fb75 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
2fb76 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e  int j = pProbe->
2fb77 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20  aiColumn[nEq];. 
2fb78 20 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e       pTerm = fin
2fb79 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
2fb7a 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 65 71   j, notReady, eq
2fb7b 54 65 72 6d 4d 61 73 6b 2c 20 70 49 64 78 29 3b  TermMask, pIdx);
2fb7c 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
2fb7d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
2fb7e 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 28 57     wsFlags |= (W
2fb7f 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57  HERE_COLUMN_EQ|W
2fb80 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 29 3b 0a  HERE_ROWID_EQ);.
2fb81 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
2fb82 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2fb83 49 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  IN ){.        Ex
2fb84 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72  pr *pExpr = pTer
2fb85 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
2fb86 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2fb87 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a 20 20  RE_COLUMN_IN;.  
2fb88 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
2fb89 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2fb8a 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2fb8b 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e 4d  {.          nInM
2fb8c 75 6c 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20  ul *= 25;.      
2fb8d 20 20 20 20 62 49 6e 45 73 74 20 3d 20 31 3b 0a      bInEst = 1;.
2fb8e 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
2fb8f 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  ( pExpr->x.pList
2fb90 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 49   ){.          nI
2fb91 6e 4d 75 6c 20 2a 3d 20 70 45 78 70 72 2d 3e 78  nMul *= pExpr->x
2fb92 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20  .pList->nExpr + 
2fb93 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
2fb94 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 65     }else if( pTe
2fb95 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2fb96 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  WO_ISNULL ){.   
2fb97 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2fb98 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c  WHERE_COLUMN_NUL
2fb99 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  L;.      }.     
2fb9a 20 75 73 65 64 20 7c 3d 20 70 54 65 72 6d 2d 3e   used |= pTerm->
2fb9b 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20  prereqRight;.   
2fb9c 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72   }..    /* Deter
2fb9d 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f  mine the value o
2fb9e 66 20 6e 42 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20  f nBound. */.   
2fb9f 20 69 66 28 20 6e 45 71 3c 70 50 72 6f 62 65 2d   if( nEq<pProbe-
2fba0 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
2fba1 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65    int j = pProbe
2fba2 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b  ->aiColumn[nEq];
2fba3 0a 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54  .      if( findT
2fba4 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a  erm(pWC, iCur, j
2fba5 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c  , notReady, WO_L
2fba6 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f  T|WO_LE|WO_GT|WO
2fba7 5f 47 45 2c 20 70 49 64 78 29 20 29 7b 0a 20 20  _GE, pIdx) ){.  
2fba8 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2fba9 2a 70 54 6f 70 20 3d 20 66 69 6e 64 54 65 72 6d  *pTop = findTerm
2fbaa 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e  (pWC, iCur, j, n
2fbab 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57  otReady, WO_LT|W
2fbac 4f 5f 4c 45 2c 20 70 49 64 78 29 3b 0a 20 20 20  O_LE, pIdx);.   
2fbad 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2fbae 70 42 74 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  pBtm = findTerm(
2fbaf 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f  pWC, iCur, j, no
2fbb0 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f  tReady, WO_GT|WO
2fbb1 5f 47 45 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  _GE, pIdx);.    
2fbb2 20 20 20 20 77 68 65 72 65 52 61 6e 67 65 53 63      whereRangeSc
2fbb3 61 6e 45 73 74 28 70 50 61 72 73 65 2c 20 70 50  anEst(pParse, pP
2fbb4 72 6f 62 65 2c 20 6e 45 71 2c 20 70 42 74 6d 2c  robe, nEq, pBtm,
2fbb5 20 70 54 6f 70 2c 20 26 6e 42 6f 75 6e 64 29 3b   pTop, &nBound);
2fbb6 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f  .        if( pTo
2fbb7 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77  p ){.          w
2fbb8 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2fbb9 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20  TOP_LIMIT;.     
2fbba 20 20 20 20 20 75 73 65 64 20 7c 3d 20 70 54 6f       used |= pTo
2fbbb 70 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b 0a  p->prereqRight;.
2fbbc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2fbbd 20 20 69 66 28 20 70 42 74 6d 20 29 7b 0a 20 20    if( pBtm ){.  
2fbbe 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2fbbf 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  |= WHERE_BTM_LIM
2fbc0 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20 75 73  IT;.          us
2fbc1 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70 72 65 72  ed |= pBtm->prer
2fbc2 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  eqRight;.       
2fbc3 20 7d 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61   }.        wsFla
2fbc4 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43 4f 4c  gs |= (WHERE_COL
2fbc5 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f  UMN_RANGE|WHERE_
2fbc6 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20  ROWID_RANGE);.  
2fbc7 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
2fbc8 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72  if( pProbe->onEr
2fbc9 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a  ror!=OE_None ){.
2fbca 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2fbcb 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2fbcc 43 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0a 20 20 20  COLUMN_IN );.   
2fbcd 20 20 20 74 65 73 74 63 61 73 65 28 20 77 73 46     testcase( wsF
2fbce 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c  lags & WHERE_COL
2fbcf 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  UMN_NULL );.    
2fbd0 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20 26    if( (wsFlags &
2fbd1 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49   (WHERE_COLUMN_I
2fbd2 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  N|WHERE_COLUMN_N
2fbd3 55 4c 4c 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ULL))==0 ){.    
2fbd4 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2fbd5 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20  HERE_UNIQUE;.   
2fbd6 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
2fbd7 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
2fbd8 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2fbd9 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20  e and the index 
2fbda 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64  being considered
2fbdb 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 61 74   will.    ** nat
2fbdc 75 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f 77 73  urally scan rows
2fbdd 20 69 6e 20 74 68 65 20 72 65 71 75 69 72 65 64   in the required
2fbde 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65 20   order, set the 
2fbdf 61 70 70 72 6f 70 72 69 61 74 65 20 66 6c 61 67  appropriate flag
2fbe0 73 0a 20 20 20 20 2a 2a 20 69 6e 20 77 73 46 6c  s.    ** in wsFl
2fbe1 61 67 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ags. Otherwise, 
2fbe2 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  if there is an O
2fbe3 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 62  RDER BY clause b
2fbe4 75 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20  ut the index.   
2fbe5 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 72 6f   ** will scan ro
2fbe6 77 73 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  ws in a differen
2fbe7 74 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65  t order, set the
2fbe8 20 62 53 6f 72 74 20 76 61 72 69 61 62 6c 65 2e   bSort variable.
2fbe9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72    */.    if( pOr
2fbea 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69  derBy ){.      i
2fbeb 66 28 20 28 77 73 46 6c 61 67 73 20 26 20 28 57  f( (wsFlags & (W
2fbec 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57  HERE_COLUMN_IN|W
2fbed 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
2fbee 29 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 26  ))==0.        &&
2fbef 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28   isSortingIndex(
2fbf0 70 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73  pParse,pWC->pMas
2fbf1 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69 43 75 72  kSet,pProbe,iCur
2fbf2 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26 72  ,pOrderBy,nEq,&r
2fbf3 65 76 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ev).      ){.   
2fbf4 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2fbf5 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47  WHERE_ROWID_RANG
2fbf6 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52  E|WHERE_COLUMN_R
2fbf7 41 4e 47 45 7c 57 48 45 52 45 5f 4f 52 44 45 52  ANGE|WHERE_ORDER
2fbf8 42 59 3b 0a 20 20 20 20 20 20 20 20 77 73 46 6c  BY;.        wsFl
2fbf9 61 67 73 20 7c 3d 20 28 72 65 76 20 3f 20 57 48  ags |= (rev ? WH
2fbfa 45 52 45 5f 52 45 56 45 52 53 45 20 3a 20 30 29  ERE_REVERSE : 0)
2fbfb 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2fbfc 20 20 20 20 20 20 20 62 53 6f 72 74 20 3d 20 31         bSort = 1
2fbfd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2fbfe 0a 20 20 20 20 2f 2a 20 49 66 20 63 75 72 72 65  .    /* If curre
2fbff 6e 74 6c 79 20 63 61 6c 63 75 6c 61 74 69 6e 67  ntly calculating
2fc00 20 74 68 65 20 63 6f 73 74 20 6f 66 20 75 73 69   the cost of usi
2fc01 6e 67 20 61 6e 20 69 6e 64 65 78 20 28 6e 6f 74  ng an index (not
2fc02 20 74 68 65 20 49 50 4b 0a 20 20 20 20 2a 2a 20   the IPK.    ** 
2fc03 69 6e 64 65 78 29 2c 20 64 65 74 65 72 6d 69 6e  index), determin
2fc04 65 20 69 66 20 61 6c 6c 20 72 65 71 75 69 72 65  e if all require
2fc05 64 20 63 6f 6c 75 6d 6e 20 64 61 74 61 20 6d 61  d column data ma
2fc06 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 77 69  y be obtained wi
2fc07 74 68 6f 75 74 20 0a 20 20 20 20 2a 2a 20 73 65  thout .    ** se
2fc08 65 6b 69 6e 67 20 74 6f 20 65 6e 74 72 69 65 73  eking to entries
2fc09 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61 62   in the main tab
2fc0a 6c 65 20 28 69 2e 65 2e 20 69 66 20 74 68 65 20  le (i.e. if the 
2fc0b 69 6e 64 65 78 20 69 73 20 61 20 63 6f 76 65 72  index is a cover
2fc0c 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78  ing.    ** index
2fc0d 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 29   for this query)
2fc0e 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65 74 20  . If it is, set 
2fc0f 74 68 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  the WHERE_IDX_ON
2fc10 4c 59 20 66 6c 61 67 20 69 6e 0a 20 20 20 20 2a  LY flag in.    *
2fc11 2a 20 77 73 46 6c 61 67 73 2e 20 4f 74 68 65 72  * wsFlags. Other
2fc12 77 69 73 65 2c 20 73 65 74 20 74 68 65 20 62 4c  wise, set the bL
2fc13 6f 6f 6b 75 70 20 76 61 72 69 61 62 6c 65 20 74  ookup variable t
2fc14 6f 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20 20 20  o true.  */.    
2fc15 69 66 28 20 70 49 64 78 20 26 26 20 77 73 46 6c  if( pIdx && wsFl
2fc16 61 67 73 20 29 7b 0a 20 20 20 20 20 20 42 69 74  ags ){.      Bit
2fc17 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d 3e 63  mask m = pSrc->c
2fc18 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 69 6e  olUsed;.      in
2fc19 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  t j;.      for(j
2fc1a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; j<pIdx->nCol
2fc1b 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  umn; j++){.     
2fc1c 20 20 20 69 6e 74 20 78 20 3d 20 70 49 64 78 2d     int x = pIdx-
2fc1d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20  >aiColumn[j];.  
2fc1e 20 20 20 20 20 20 69 66 28 20 78 3c 42 4d 53 2d        if( x<BMS-
2fc1f 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d  1 ){.          m
2fc20 20 26 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b 29   &= ~(((Bitmask)
2fc21 31 29 3c 3c 78 29 3b 0a 20 20 20 20 20 20 20 20  1)<<x);.        
2fc22 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2fc23 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20  if( m==0 ){.    
2fc24 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2fc25 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20  HERE_IDX_ONLY;. 
2fc26 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2fc27 20 20 20 20 62 4c 6f 6f 6b 75 70 20 3d 20 31 3b      bLookup = 1;
2fc28 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2fc29 20 20 20 20 2f 2a 2a 2a 2a 20 42 65 67 69 6e 20      /**** Begin 
2fc2a 61 64 64 69 6e 67 20 75 70 20 74 68 65 20 63 6f  adding up the co
2fc2b 73 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73  st of using this
2fc2c 20 69 6e 64 65 78 20 28 4e 65 65 64 73 20 69 6d   index (Needs im
2fc2d 70 72 6f 76 65 6d 65 6e 74 73 29 0a 20 20 20 20  provements).    
2fc2e 2a 2a 0a 20 20 20 20 2a 2a 20 45 73 74 69 6d 61  **.    ** Estima
2fc2f 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
2fc30 20 72 6f 77 73 20 6f 66 20 6f 75 74 70 75 74 2e   rows of output.
2fc31 20 20 46 6f 72 20 61 6e 20 49 4e 20 6f 70 65 72    For an IN oper
2fc32 61 74 6f 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20  ator,.    ** do 
2fc33 6e 6f 74 20 6c 65 74 20 74 68 65 20 65 73 74 69  not let the esti
2fc34 6d 61 74 65 20 65 78 63 65 65 64 20 68 61 6c 66  mate exceed half
2fc35 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65   the rows in the
2fc36 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20   table..    */. 
2fc37 20 20 20 6e 52 6f 77 20 3d 20 28 64 6f 75 62 6c     nRow = (doubl
2fc38 65 29 28 61 69 52 6f 77 45 73 74 5b 6e 45 71 5d  e)(aiRowEst[nEq]
2fc39 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0a 20 20 20 20   * nInMul);.    
2fc3a 69 66 28 20 62 49 6e 45 73 74 20 26 26 20 6e 52  if( bInEst && nR
2fc3b 6f 77 2a 32 3e 61 69 52 6f 77 45 73 74 5b 30 5d  ow*2>aiRowEst[0]
2fc3c 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20 3d   ){.      nRow =
2fc3d 20 61 69 52 6f 77 45 73 74 5b 30 5d 2f 32 3b 0a   aiRowEst[0]/2;.
2fc3e 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 3d 20 28        nInMul = (
2fc3f 69 6e 74 29 28 6e 52 6f 77 20 2f 20 61 69 52 6f  int)(nRow / aiRo
2fc40 77 45 73 74 5b 6e 45 71 5d 29 3b 0a 20 20 20 20  wEst[nEq]);.    
2fc41 7d 0a 0a 20 20 20 20 2f 2a 20 41 73 73 75 6d 65  }..    /* Assume
2fc42 20 63 6f 6e 73 74 61 6e 74 20 63 6f 73 74 20 74   constant cost t
2fc43 6f 20 61 63 63 65 73 73 20 61 20 72 6f 77 20 61  o access a row a
2fc44 6e 64 20 6c 6f 67 61 72 69 74 68 6d 69 63 20 63  nd logarithmic c
2fc45 6f 73 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f  ost to.    ** do
2fc46 20 61 20 62 69 6e 61 72 79 20 73 65 61 72 63 68   a binary search
2fc47 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 69 6e  .  Hence, the in
2fc48 69 74 69 61 6c 20 63 6f 73 74 20 69 73 20 74 68  itial cost is th
2fc49 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70  e number of outp
2fc4a 75 74 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 70  ut.    ** rows p
2fc4b 6c 75 73 20 6c 6f 67 32 28 74 61 62 6c 65 2d 73  lus log2(table-s
2fc4c 69 7a 65 29 20 74 69 6d 65 73 20 74 68 65 20 6e  ize) times the n
2fc4d 75 6d 62 65 72 20 6f 66 20 62 69 6e 61 72 79 20  umber of binary 
2fc4e 73 65 61 72 63 68 65 73 2e 0a 20 20 20 20 2a 2f  searches..    */
2fc4f 0a 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77  .    cost = nRow
2fc50 20 2b 20 6e 49 6e 4d 75 6c 2a 65 73 74 4c 6f 67   + nInMul*estLog
2fc51 28 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b 0a 0a  (aiRowEst[0]);..
2fc52 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20 74 68      /* Adjust th
2fc53 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2fc54 20 61 6e 64 20 74 68 65 20 63 6f 73 74 20 64 6f   and the cost do
2fc55 77 6e 77 61 72 64 20 74 6f 20 72 65 66 6c 65 63  wnward to reflec
2fc56 74 20 72 6f 77 73 0a 20 20 20 20 2a 2a 20 74 68  t rows.    ** th
2fc57 61 74 20 61 72 65 20 65 78 63 6c 75 64 65 64 20  at are excluded 
2fc58 62 79 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61  by range constra
2fc59 69 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ints..    */.   
2fc5a 20 6e 52 6f 77 20 3d 20 28 6e 52 6f 77 20 2a 20   nRow = (nRow * 
2fc5b 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64 29 20  (double)nBound) 
2fc5c 2f 20 28 64 6f 75 62 6c 65 29 31 30 30 3b 0a 20  / (double)100;. 
2fc5d 20 20 20 63 6f 73 74 20 3d 20 28 63 6f 73 74 20     cost = (cost 
2fc5e 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64  * (double)nBound
2fc5f 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30 3b  ) / (double)100;
2fc60 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 69 6e 20  ..    /* Add in 
2fc61 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 63 6f  the estimated co
2fc62 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 74 68  st of sorting th
2fc63 65 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2f 0a  e result.    */.
2fc64 20 20 20 20 69 66 28 20 62 53 6f 72 74 20 29 7b      if( bSort ){
2fc65 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20 63  .      cost += c
2fc66 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29  ost*estLog(cost)
2fc67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2fc68 49 66 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  If all informati
2fc69 6f 6e 20 63 61 6e 20 62 65 20 74 61 6b 65 6e 20  on can be taken 
2fc6a 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68  directly from th
2fc6b 65 20 69 6e 64 65 78 2c 20 77 65 20 61 76 6f 69  e index, we avoi
2fc6c 64 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74  d.    ** doing t
2fc6d 61 62 6c 65 20 6c 6f 6f 6b 75 70 73 2e 20 20 54  able lookups.  T
2fc6e 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65 20  his reduces the 
2fc6f 63 6f 73 74 20 62 79 20 68 61 6c 66 2e 20 20 28  cost by half.  (
2fc70 4e 6f 74 20 72 65 61 6c 6c 79 20 2d 0a 20 20 20  Not really -.   
2fc71 20 2a 2a 20 74 68 69 73 20 6e 65 65 64 73 20 74   ** this needs t
2fc72 6f 20 62 65 20 66 69 78 65 64 2e 29 0a 20 20 20  o be fixed.).   
2fc73 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49 64 78   */.    if( pIdx
2fc74 20 26 26 20 62 4c 6f 6f 6b 75 70 3d 3d 30 20 29   && bLookup==0 )
2fc75 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20  {.      cost /= 
2fc76 28 64 6f 75 62 6c 65 29 32 3b 0a 20 20 20 20 7d  (double)2;.    }
2fc77 0a 20 20 20 20 2f 2a 2a 2a 2a 20 43 6f 73 74 20  .    /**** Cost 
2fc78 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e  of using this in
2fc79 64 65 78 20 68 61 73 20 6e 6f 77 20 62 65 65 6e  dex has now been
2fc7a 20 63 6f 6d 70 75 74 65 64 20 2a 2a 2a 2a 2f 0a   computed ****/.
2fc7b 0a 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28  .    WHERETRACE(
2fc7c 28 0a 20 20 20 20 20 20 22 74 62 6c 3d 25 73 20  (.      "tbl=%s 
2fc7d 69 64 78 3d 25 73 20 6e 45 71 3d 25 64 20 6e 49  idx=%s nEq=%d nI
2fc7e 6e 4d 75 6c 3d 25 64 20 6e 42 6f 75 6e 64 3d 25  nMul=%d nBound=%
2fc7f 64 20 62 53 6f 72 74 3d 25 64 20 62 4c 6f 6f 6b  d bSort=%d bLook
2fc80 75 70 3d 25 64 22 0a 20 20 20 20 20 20 22 20 77  up=%d".      " w
2fc81 73 46 6c 61 67 73 3d 25 64 20 20 20 28 6e 52 6f  sFlags=%d   (nRo
2fc82 77 3d 25 2e 32 66 20 63 6f 73 74 3d 25 2e 32 66  w=%.2f cost=%.2f
2fc83 29 5c 6e 22 2c 0a 20 20 20 20 20 20 70 53 72 63  )\n",.      pSrc
2fc84 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 28  ->pTab->zName, (
2fc85 70 49 64 78 20 3f 20 70 49 64 78 2d 3e 7a 4e 61  pIdx ? pIdx->zNa
2fc86 6d 65 20 3a 20 22 69 70 6b 22 29 2c 20 0a 20 20  me : "ipk"), .  
2fc87 20 20 20 20 6e 45 71 2c 20 6e 49 6e 4d 75 6c 2c      nEq, nInMul,
2fc88 20 6e 42 6f 75 6e 64 2c 20 62 53 6f 72 74 2c 20   nBound, bSort, 
2fc89 62 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c 61 67 73  bLookup, wsFlags
2fc8a 2c 20 6e 52 6f 77 2c 20 63 6f 73 74 0a 20 20 20  , nRow, cost.   
2fc8b 20 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20   ));..    /* If 
2fc8c 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 74 68  this index is th
2fc8d 65 20 62 65 73 74 20 77 65 20 68 61 76 65 20 73  e best we have s
2fc8e 65 65 6e 20 73 6f 20 66 61 72 2c 20 74 68 65 6e  een so far, then
2fc8f 20 72 65 63 6f 72 64 20 74 68 69 73 0a 20 20 20   record this.   
2fc90 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 69 74   ** index and it
2fc91 73 20 63 6f 73 74 20 69 6e 20 74 68 65 20 70 43  s cost in the pC
2fc92 6f 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20  ost structure.. 
2fc93 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21     */.    if( (!
2fc94 70 49 64 78 20 7c 7c 20 77 73 46 6c 61 67 73 29  pIdx || wsFlags)
2fc95 20 26 26 20 63 6f 73 74 3c 70 43 6f 73 74 2d 3e   && cost<pCost->
2fc96 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 70  rCost ){.      p
2fc97 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f  Cost->rCost = co
2fc98 73 74 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d  st;.      pCost-
2fc99 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20  >nRow = nRow;.  
2fc9a 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64 20      pCost->used 
2fc9b 3d 20 75 73 65 64 3b 0a 20 20 20 20 20 20 70 43  = used;.      pC
2fc9c 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2fc9d 73 20 3d 20 28 77 73 46 6c 61 67 73 26 77 73 46  s = (wsFlags&wsF
2fc9e 6c 61 67 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20  lagMask);.      
2fc9f 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20  pCost->plan.nEq 
2fca0 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20 70 43 6f  = nEq;.      pCo
2fca1 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20  st->plan.u.pIdx 
2fca2 3d 20 70 49 64 78 3b 0a 20 20 20 20 7d 0a 0a 20  = pIdx;.    }.. 
2fca3 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77     /* If there w
2fca4 61 73 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59  as an INDEXED BY
2fca5 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 6f 6e   clause, then on
2fca6 6c 79 20 74 68 61 74 20 6f 6e 65 20 69 6e 64 65  ly that one inde
2fca7 78 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 73  x is.    ** cons
2fca8 69 64 65 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69  idered. */.    i
2fca9 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 20  f( pSrc->pIndex 
2fcaa 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a  ) break;..    /*
2fcab 20 52 65 73 65 74 20 6d 61 73 6b 73 20 66 6f 72   Reset masks for
2fcac 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20   the next index 
2fcad 69 6e 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  in the loop */. 
2fcae 20 20 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20     wsFlagMask = 
2fcaf 7e 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  ~(WHERE_ROWID_EQ
2fcb0 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e  |WHERE_ROWID_RAN
2fcb1 47 45 29 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d  GE);.    eqTermM
2fcb2 61 73 6b 20 3d 20 69 64 78 45 71 54 65 72 6d 4d  ask = idxEqTermM
2fcb3 61 73 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ask;.  }..  /* I
2fcb4 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52  f there is no OR
2fcb5 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6e  DER BY clause an
2fcb6 64 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65 76  d the SQLITE_Rev
2fcb7 65 72 73 65 4f 72 64 65 72 20 66 6c 61 67 0a 20  erseOrder flag. 
2fcb8 20 2a 2a 20 69 73 20 73 65 74 2c 20 74 68 65 6e   ** is set, then
2fcb9 20 72 65 76 65 72 73 65 20 74 68 65 20 6f 72 64   reverse the ord
2fcba 65 72 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  er that the inde
2fcbb 78 20 77 69 6c 6c 20 62 65 20 73 63 61 6e 6e 65  x will be scanne
2fcbc 64 0a 20 20 2a 2a 20 69 6e 2e 20 54 68 69 73 20  d.  ** in. This 
2fcbd 69 73 20 75 73 65 64 20 66 6f 72 20 61 70 70 6c  is used for appl
2fcbe 69 63 61 74 69 6f 6e 20 74 65 73 74 69 6e 67 2c  ication testing,
2fcbf 20 74 6f 20 68 65 6c 70 20 66 69 6e 64 20 63 61   to help find ca
2fcc0 73 65 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 61  ses.  ** where a
2fcc1 70 70 6c 69 63 61 74 69 6f 6e 20 62 65 68 61 76  pplication behav
2fcc2 69 6f 75 72 20 64 65 70 65 6e 64 73 20 6f 6e 20  iour depends on 
2fcc3 74 68 65 20 28 75 6e 64 65 66 69 6e 65 64 29 20  the (undefined) 
2fcc4 6f 72 64 65 72 20 74 68 61 74 0a 20 20 2a 2a 20  order that.  ** 
2fcc5 53 51 4c 69 74 65 20 6f 75 74 70 75 74 73 20 72  SQLite outputs r
2fcc6 6f 77 73 20 69 6e 20 69 6e 20 74 68 65 20 61 62  ows in in the ab
2fcc7 73 65 6e 63 65 20 6f 66 20 61 6e 20 4f 52 44 45  sence of an ORDE
2fcc8 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 2a 2f  R BY clause.  */
2fcc9 0a 20 20 69 66 28 20 21 70 4f 72 64 65 72 42 79  .  if( !pOrderBy
2fcca 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   && pParse->db->
2fccb 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52  flags & SQLITE_R
2fccc 65 76 65 72 73 65 4f 72 64 65 72 20 29 7b 0a 20  everseOrder ){. 
2fccd 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77     pCost->plan.w
2fcce 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2fccf 52 45 56 45 52 53 45 3b 0a 20 20 7d 0a 0a 20 20  REVERSE;.  }..  
2fcd0 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2fcd1 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e   || (pCost->plan
2fcd2 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f  .wsFlags&WHERE_O
2fcd3 52 44 45 52 42 59 29 3d 3d 30 20 29 3b 0a 20 20  RDERBY)==0 );.  
2fcd4 61 73 73 65 72 74 28 20 70 43 6f 73 74 2d 3e 70  assert( pCost->p
2fcd5 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30 20 7c 7c  lan.u.pIdx==0 ||
2fcd6 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73   (pCost->plan.ws
2fcd7 46 6c 61 67 73 26 57 48 45 52 45 5f 52 4f 57 49  Flags&WHERE_ROWI
2fcd8 44 5f 45 51 29 3d 3d 30 20 29 3b 0a 20 20 61 73  D_EQ)==0 );.  as
2fcd9 73 65 72 74 28 20 70 53 72 63 2d 3e 70 49 6e 64  sert( pSrc->pInd
2fcda 65 78 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c  ex==0 .       ||
2fcdb 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70   pCost->plan.u.p
2fcdc 49 64 78 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c  Idx==0 .       |
2fcdd 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e  | pCost->plan.u.
2fcde 70 49 64 78 3d 3d 70 53 72 63 2d 3e 70 49 6e 64  pIdx==pSrc->pInd
2fcdf 65 78 20 0a 20 20 29 3b 0a 0a 20 20 57 48 45 52  ex .  );..  WHER
2fce0 45 54 52 41 43 45 28 28 22 62 65 73 74 20 69 6e  ETRACE(("best in
2fce1 64 65 78 20 69 73 3a 20 25 73 5c 6e 22 2c 20 0a  dex is: %s\n", .
2fce2 20 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e      (pCost->plan
2fce3 2e 75 2e 70 49 64 78 20 3f 20 70 43 6f 73 74 2d  .u.pIdx ? pCost-
2fce4 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e  >plan.u.pIdx->zN
2fce5 61 6d 65 20 3a 20 22 69 70 6b 22 29 0a 20 20 29  ame : "ipk").  )
2fce6 29 3b 0a 20 20 0a 20 20 62 65 73 74 4f 72 43 6c  );.  .  bestOrCl
2fce7 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73 65  auseIndex(pParse
2fce8 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  , pWC, pSrc, not
2fce9 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c  Ready, pOrderBy,
2fcea 20 70 43 6f 73 74 29 3b 0a 20 20 70 43 6f 73 74   pCost);.  pCost
2fceb 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  ->plan.wsFlags |
2fcec 3d 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 7d 0a  = eqTermMask;.}.
2fced 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
2fcee 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 61  query plan for a
2fcef 63 63 65 73 73 69 6e 67 20 74 61 62 6c 65 20 70  ccessing table p
2fcf0 53 72 63 2d 3e 70 54 61 62 2e 20 57 72 69 74 65  Src->pTab. Write
2fcf1 20 74 68 65 0a 2a 2a 20 62 65 73 74 20 71 75 65   the.** best que
2fcf2 72 79 20 70 6c 61 6e 20 61 6e 64 20 69 74 73 20  ry plan and its 
2fcf3 63 6f 73 74 20 69 6e 74 6f 20 74 68 65 20 57 68  cost into the Wh
2fcf4 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 73  ereCost object s
2fcf5 75 70 70 6c 69 65 64 20 0a 2a 2a 20 61 73 20 74  upplied .** as t
2fcf6 68 65 20 6c 61 73 74 20 70 61 72 61 6d 65 74 65  he last paramete
2fcf7 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
2fcf8 20 6d 61 79 20 63 61 6c 63 75 6c 61 74 65 20 74   may calculate t
2fcf9 68 65 20 63 6f 73 74 20 6f 66 0a 2a 2a 20 62 6f  he cost of.** bo
2fcfa 74 68 20 72 65 61 6c 20 61 6e 64 20 76 69 72 74  th real and virt
2fcfb 75 61 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 2e  ual table scans.
2fcfc 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2fcfd 62 65 73 74 49 6e 64 65 78 28 0a 20 20 50 61 72  bestIndex(.  Par
2fcfe 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2fcff 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2fd00 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2fd01 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2fd02 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 20   *pWC,          
2fd03 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2fd04 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
2fd05 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2fd06 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f  Src,  /* The FRO
2fd07 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f  M clause term to
2fd08 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74   search */.  Bit
2fd09 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20  mask notReady,  
2fd0a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
2fd0b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74   of cursors that
2fd0c 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
2fd0d 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  le */.  ExprList
2fd0e 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20   *pOrderBy,     
2fd0f 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
2fd10 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2fd11 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74  WhereCost *pCost
2fd12 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2fd13 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79  owest cost query
2fd14 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e   plan */.){.#ifn
2fd15 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2fd16 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
2fd17 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 53 72  f( IsVirtual(pSr
2fd18 63 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20 20  c->pTab) ){.    
2fd19 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2fd1a 66 6f 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 62  fo *p = 0;.    b
2fd1b 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28  estVirtualIndex(
2fd1c 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72  pParse, pWC, pSr
2fd1d 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72  c, notReady, pOr
2fd1e 64 65 72 42 79 2c 20 70 43 6f 73 74 2c 20 26 70  derBy, pCost, &p
2fd1f 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 65  );.    if( p->ne
2fd20 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29  edToFreeIdxStr )
2fd21 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
2fd22 66 72 65 65 28 70 2d 3e 69 64 78 53 74 72 29 3b  free(p->idxStr);
2fd23 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2fd24 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d  e3DbFree(pParse-
2fd25 3e 64 62 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65  >db, p);.  }else
2fd26 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
2fd27 62 65 73 74 42 74 72 65 65 49 6e 64 65 78 28 70  bestBtreeIndex(p
2fd28 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63  Parse, pWC, pSrc
2fd29 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64  , notReady, pOrd
2fd2a 65 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20  erBy, pCost);.  
2fd2b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62  }.}../*.** Disab
2fd2c 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65  le a term in the
2fd2d 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2fd2e 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64  Except, do not d
2fd2f 69 73 61 62 6c 65 20 74 68 65 20 74 65 72 6d 0a  isable the term.
2fd30 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c  ** if it control
2fd31 73 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  s a LEFT OUTER J
2fd32 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 64 20 6e  OIN and it did n
2fd33 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69 6e 20  ot originate in 
2fd34 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49  the ON.** or USI
2fd35 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61  NG clause of tha
2fd36 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f  t join..**.** Co
2fd37 6e 73 69 64 65 72 20 74 68 65 20 74 65 72 6d 20  nsider the term 
2fd38 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65  t2.z='ok' in the
2fd39 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69   following queri
2fd3a 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20  es:.**.**   (1) 
2fd3b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
2fd3c 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f  1 LEFT JOIN t2 O
2fd3d 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 48 45 52  N t1.a=t2.x WHER
2fd3e 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20  E t2.z='ok'.**  
2fd3f 20 28 32 29 20 20 53 45 4c 45 43 54 20 2a 20 46   (2)  SELECT * F
2fd40 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e  ROM t1 LEFT JOIN
2fd41 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78   t2 ON t1.a=t2.x
2fd42 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a   AND t2.z='ok'.*
2fd43 2a 20 20 20 28 33 29 20 20 53 45 4c 45 43 54 20  *   (3)  SELECT 
2fd44 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 20 57 48  * FROM t1, t2 WH
2fd45 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e  ERE t1.a=t2.x AN
2fd46 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a  D t2.z='ok'.**.*
2fd47 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20  * The t2.z='ok' 
2fd48 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74  is disabled in t
2fd49 68 65 20 69 6e 20 28 32 29 20 62 65 63 61 75 73  he in (2) becaus
2fd4a 65 20 69 74 20 6f 72 69 67 69 6e 61 74 65 73 0a  e it originates.
2fd4b 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61  ** in the ON cla
2fd4c 75 73 65 2e 20 20 54 68 65 20 74 65 72 6d 20 69  use.  The term i
2fd4d 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28 33  s disabled in (3
2fd4e 29 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  ) because it is 
2fd4f 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61  not part.** of a
2fd50 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
2fd51 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 65 20 74  .  In (1), the t
2fd52 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 73 61 62  erm is not disab
2fd53 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62  led..**.** Disab
2fd54 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 61 75 73  ling a term caus
2fd55 65 73 20 74 68 61 74 20 74 65 72 6d 20 74 6f 20  es that term to 
2fd56 6e 6f 74 20 62 65 20 74 65 73 74 65 64 20 69 6e  not be tested in
2fd57 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a   the inner loop.
2fd58 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20  ** of the join. 
2fd59 20 44 69 73 61 62 6c 69 6e 67 20 69 73 20 61 6e   Disabling is an
2fd5a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20   optimization.  
2fd5b 57 68 65 6e 20 74 65 72 6d 73 20 61 72 65 20 73  When terms are s
2fd5c 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20 69  atisfied.** by i
2fd5d 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73 61 62  ndices, we disab
2fd5e 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65 76 65  le them to preve
2fd5f 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74 65 73  nt redundant tes
2fd60 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a  ts in the inner.
2fd61 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75  ** loop.  We wou
2fd62 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72 72 65  ld get the corre
2fd63 63 74 20 72 65 73 75 6c 74 73 20 69 66 20 6e 6f  ct results if no
2fd64 74 68 69 6e 67 20 77 65 72 65 20 65 76 65 72 20  thing were ever 
2fd65 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74  disabled,.** but
2fd66 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72 75 6e   joins might run
2fd67 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72   a little slower
2fd68 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69 73 20  .  The trick is 
2fd69 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20 6d 75  to disable as mu
2fd6a 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e 20  ch.** as we can 
2fd6b 77 69 74 68 6f 75 74 20 64 69 73 61 62 6c 69 6e  without disablin
2fd6c 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20  g too much.  If 
2fd6d 77 65 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28  we disabled in (
2fd6e 31 29 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a 20  1), we'd get.** 
2fd6f 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 77 65 72  the wrong answer
2fd70 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 38  .  See ticket #8
2fd71 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  13..*/.static vo
2fd72 69 64 20 64 69 73 61 62 6c 65 54 65 72 6d 28 57  id disableTerm(W
2fd73 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65  hereLevel *pLeve
2fd74 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  l, WhereTerm *pT
2fd75 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 54 65 72  erm){.  if( pTer
2fd76 6d 0a 20 20 20 20 20 20 26 26 20 41 4c 57 41 59  m.      && ALWAY
2fd77 53 28 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  S((pTerm->wtFlag
2fd78 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d  s & TERM_CODED)=
2fd79 3d 30 29 0a 20 20 20 20 20 20 26 26 20 28 70 4c  =0).      && (pL
2fd7a 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d  evel->iLeftJoin=
2fd7b 3d 30 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f  =0 || ExprHasPro
2fd7c 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78  perty(pTerm->pEx
2fd7d 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  pr, EP_FromJoin)
2fd7e 29 0a 20 20 29 7b 0a 20 20 20 20 70 54 65 72 6d  ).  ){.    pTerm
2fd7f 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52  ->wtFlags |= TER
2fd80 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 69 66 28  M_CODED;.    if(
2fd81 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e   pTerm->iParent>
2fd82 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72  =0 ){.      Wher
2fd83 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20  eTerm *pOther = 
2fd84 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70  &pTerm->pWC->a[p
2fd85 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a  Term->iParent];.
2fd86 20 20 20 20 20 20 69 66 28 20 28 2d 2d 70 4f 74        if( (--pOt
2fd87 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20  her->nChild)==0 
2fd88 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73 61 62  ){.        disab
2fd89 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70  leTerm(pLevel, p
2fd8a 4f 74 68 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Other);.      }.
2fd8b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
2fd8c 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 41 66  ** Code an OP_Af
2fd8d 66 69 6e 69 74 79 20 6f 70 63 6f 64 65 20 74 6f  finity opcode to
2fd8e 20 61 70 70 6c 79 20 74 68 65 20 63 6f 6c 75 6d   apply the colum
2fd8f 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
2fd90 67 20 7a 41 66 66 0a 2a 2a 20 74 6f 20 74 68 65  g zAff.** to the
2fd91 20 6e 20 72 65 67 69 73 74 65 72 73 20 73 74 61   n registers sta
2fd92 72 74 69 6e 67 20 61 74 20 62 61 73 65 2e 20 0a  rting at base. .
2fd93 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 6f 70 74 69  **.** As an opti
2fd94 6d 69 7a 61 74 69 6f 6e 2c 20 53 51 4c 49 54 45  mization, SQLITE
2fd95 5f 41 46 46 5f 4e 4f 4e 45 20 65 6e 74 72 69 65  _AFF_NONE entrie
2fd96 73 20 28 77 68 69 63 68 20 61 72 65 20 6e 6f 2d  s (which are no-
2fd97 6f 70 73 29 20 61 74 20 74 68 65 0a 2a 2a 20 62  ops) at the.** b
2fd98 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64  eginning and end
2fd99 20 6f 66 20 7a 41 66 66 20 61 72 65 20 69 67 6e   of zAff are ign
2fd9a 6f 72 65 64 2e 20 20 49 66 20 61 6c 6c 20 65 6e  ored.  If all en
2fd9b 74 72 69 65 73 20 69 6e 20 7a 41 66 66 20 61 72  tries in zAff ar
2fd9c 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  e.** SQLITE_AFF_
2fd9d 4e 4f 4e 45 2c 20 74 68 65 6e 20 6e 6f 20 63 6f  NONE, then no co
2fd9e 64 65 20 67 65 74 73 20 67 65 6e 65 72 61 74 65  de gets generate
2fd9f 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
2fda0 75 74 69 6e 65 20 6d 61 6b 65 73 20 69 74 73 20  utine makes its 
2fda1 6f 77 6e 20 63 6f 70 79 20 6f 66 20 7a 41 66 66  own copy of zAff
2fda2 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 6c   so that the cal
2fda3 6c 65 72 20 69 73 20 66 72 65 65 0a 2a 2a 20 74  ler is free.** t
2fda4 6f 20 6d 6f 64 69 66 79 20 7a 41 66 66 20 61 66  o modify zAff af
2fda5 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
2fda6 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61   returns..*/.sta
2fda7 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41 70 70  tic void codeApp
2fda8 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72 73 65  lyAffinity(Parse
2fda9 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 62 61   *pParse, int ba
2fdaa 73 65 2c 20 69 6e 74 20 6e 2c 20 63 68 61 72 20  se, int n, char 
2fdab 2a 7a 41 66 66 29 7b 0a 20 20 56 64 62 65 20 2a  *zAff){.  Vdbe *
2fdac 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2fdad 65 3b 0a 20 20 69 66 28 20 7a 41 66 66 3d 3d 30  e;.  if( zAff==0
2fdae 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2fdaf 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
2fdb0 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2fdb1 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
2fdb2 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 0a 20  sert( v!=0 );.. 
2fdb3 20 2f 2a 20 41 64 6a 75 73 74 20 62 61 73 65 20   /* Adjust base 
2fdb4 61 6e 64 20 6e 20 74 6f 20 73 6b 69 70 20 6f 76  and n to skip ov
2fdb5 65 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  er SQLITE_AFF_NO
2fdb6 4e 45 20 65 6e 74 72 69 65 73 20 61 74 20 74 68  NE entries at th
2fdb7 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a  e beginning.  **
2fdb8 20 61 6e 64 20 65 6e 64 20 6f 66 20 74 68 65 20   and end of the 
2fdb9 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 2e  affinity string.
2fdba 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e  .  */.  while( n
2fdbb 3e 30 20 26 26 20 7a 41 66 66 5b 30 5d 3d 3d 53  >0 && zAff[0]==S
2fdbc 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
2fdbd 7b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 62  {.    n--;.    b
2fdbe 61 73 65 2b 2b 3b 0a 20 20 20 20 7a 41 66 66 2b  ase++;.    zAff+
2fdbf 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  +;.  }.  while( 
2fdc0 6e 3e 31 20 26 26 20 7a 41 66 66 5b 6e 2d 31 5d  n>1 && zAff[n-1]
2fdc1 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
2fdc2 45 20 29 7b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20  E ){.    n--;.  
2fdc3 7d 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68 65  }..  /* Code the
2fdc4 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 6f 70 63   OP_Affinity opc
2fdc5 6f 64 65 20 69 66 20 74 68 65 72 65 20 69 73 20  ode if there is 
2fdc6 61 6e 79 74 68 69 6e 67 20 6c 65 66 74 20 74 6f  anything left to
2fdc7 20 64 6f 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 3e   do. */.  if( n>
2fdc8 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2fdc9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fdca 5f 41 66 66 69 6e 69 74 79 2c 20 62 61 73 65 2c  _Affinity, base,
2fdcb 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   n);.    sqlite3
2fdcc 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2fdcd 2d 31 2c 20 7a 41 66 66 2c 20 6e 29 3b 0a 20 20  -1, zAff, n);.  
2fdce 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2fdcf 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
2fdd0 28 70 50 61 72 73 65 2c 20 62 61 73 65 2c 20 6e  (pParse, base, n
2fdd1 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  );.  }.}.../*.**
2fdd2 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
2fdd3 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75 61  or a single equa
2fdd4 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68 65  lity term of the
2fdd5 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2fdd6 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74  An equality.** t
2fdd7 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68 65  erm can be eithe
2fdd8 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49 4e  r X=expr or X IN
2fdd9 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20   (...).   pTerm 
2fdda 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62  is the term to b
2fddb 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a  e .** coded..**.
2fddc 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
2fddd 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6e  alue for the con
2fdde 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74 20  straint is left 
2fddf 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67  in register iReg
2fde0 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f  ..**.** For a co
2fde1 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65 20  nstraint of the 
2fde2 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68 65  form X=expr, the
2fde3 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65   expression is e
2fde4 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74 73  valuated and its
2fde5 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c 65  .** result is le
2fde6 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  ft on the stack.
2fde7 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74    For constraint
2fde8 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 20  s of the form X 
2fde9 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73  IN (...).** this
2fdea 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70   routine sets up
2fdeb 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c   a loop that wil
2fdec 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61  l iterate over a
2fded 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a  ll values of X..
2fdee 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2fdef 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0a  deEqualityTerm(.
2fdf0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2fdf1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2fdf2 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2fdf3 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2fdf4 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65 72  rm,   /* The ter
2fdf5 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2fdf6 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64 65  lause to be code
2fdf7 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  d */.  WhereLeve
2fdf8 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57 68  l *pLevel, /* Wh
2fdf9 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  en level of the 
2fdfa 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20 61  FROM clause we a
2fdfb 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f  re working on */
2fdfc 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74 20 20  .  int iTarget  
2fdfd 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70         /* Attemp
2fdfe 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75 6c  t to leave resul
2fdff 74 73 20 69 6e 20 74 68 69 73 20 72 65 67 69 73  ts in this regis
2fe00 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ter */.){.  Expr
2fe01 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45   *pX = pTerm->pE
2fe02 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  xpr;.  Vdbe *v =
2fe03 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2fe04 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20    int iReg;     
2fe05 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fe06 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
2fe07 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20 61   results */..  a
2fe08 73 73 65 72 74 28 20 69 54 61 72 67 65 74 3e 30  ssert( iTarget>0
2fe09 20 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70   );.  if( pX->op
2fe0a 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20 69  ==TK_EQ ){.    i
2fe0b 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
2fe0c 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
2fe0d 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20  se, pX->pRight, 
2fe0e 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73  iTarget);.  }els
2fe0f 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  e if( pX->op==TK
2fe10 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69  _ISNULL ){.    i
2fe11 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a 20  Reg = iTarget;. 
2fe12 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fe13 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2fe14 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e 64   0, iReg);.#ifnd
2fe15 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2fe16 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73 65 7b  UBQUERY.  }else{
2fe17 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a  .    int eType;.
2fe18 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20      int iTab;.  
2fe19 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20    struct InLoop 
2fe1a 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65 72  *pIn;..    asser
2fe1b 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  t( pX->op==TK_IN
2fe1c 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 69   );.    iReg = i
2fe1d 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54 79 70  Target;.    eTyp
2fe1e 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  e = sqlite3FindI
2fe1f 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  nIndex(pParse, p
2fe20 58 2c 20 30 29 3b 0a 20 20 20 20 69 54 61 62 20  X, 0);.    iTab 
2fe21 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  = pX->iTable;.  
2fe22 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fe23 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2fe24 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 20 20  , iTab, 0);.    
2fe25 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e  assert( pLevel->
2fe26 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2fe27 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a  HERE_IN_ABLE );.
2fe28 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2fe29 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20  u.in.nIn==0 ){. 
2fe2a 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64       pLevel->add
2fe2b 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  rNxt = sqlite3Vd
2fe2c 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2fe2d 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c      }.    pLevel
2fe2e 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20  ->u.in.nIn++;.  
2fe2f 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61    pLevel->u.in.a
2fe30 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20  InLoop =.       
2fe31 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2fe32 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64  OrFree(pParse->d
2fe33 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  b, pLevel->u.in.
2fe34 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20  aInLoop,.       
2fe35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe36 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c         sizeof(pL
2fe37 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
2fe38 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75  op[0])*pLevel->u
2fe39 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49  .in.nIn);.    pI
2fe3a 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  n = pLevel->u.in
2fe3b 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66  .aInLoop;.    if
2fe3c 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 70  ( pIn ){.      p
2fe3d 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e  In += pLevel->u.
2fe3e 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20  in.nIn - 1;.    
2fe3f 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54    pIn->iCur = iT
2fe40 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  ab;.      if( eT
2fe41 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f  ype==IN_INDEX_RO
2fe42 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70  WID ){.        p
2fe43 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20  In->addrInTop = 
2fe44 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fe45 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
2fe46 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  Tab, iReg);.    
2fe47 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2fe48 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20   pIn->addrInTop 
2fe49 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2fe4a 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2fe4b 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29  , iTab, 0, iReg)
2fe4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2fe4d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fe4e 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
2fe4f 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  iReg);.    }else
2fe50 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
2fe51 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20  u.in.nIn = 0;.  
2fe52 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2fe53 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2fe54 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72  vel, pTerm);.  r
2fe55 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f  eturn iReg;.}../
2fe56 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2fe57 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61  de that will eva
2fe58 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64  luate all == and
2fe59 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   IN constraints 
2fe5a 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e  for an.** index.
2fe5b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
2fe5c 6c 65 2c 20 63 6f 6e 73 69 64 65 72 20 74 61 62  le, consider tab
2fe5d 6c 65 20 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c  le t1(a,b,c,d,e,
2fe5e 66 29 20 77 69 74 68 20 69 6e 64 65 78 20 69 31  f) with index i1
2fe5f 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70  (a,b,c)..** Supp
2fe60 6f 73 65 20 74 68 65 20 57 48 45 52 45 20 63 6c  ose the WHERE cl
2fe61 61 75 73 65 20 69 73 20 74 68 69 73 3a 20 20 61  ause is this:  a
2fe62 3d 3d 35 20 41 4e 44 20 62 20 49 4e 20 28 31 2c  ==5 AND b IN (1,
2fe63 32 2c 33 29 20 41 4e 44 20 63 3e 35 20 41 4e 44  2,3) AND c>5 AND
2fe64 20 63 3c 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64   c<10.** The ind
2fe65 65 78 20 68 61 73 20 61 73 20 6d 61 6e 79 20 61  ex has as many a
2fe66 73 20 74 68 72 65 65 20 65 71 75 61 6c 69 74 79  s three equality
2fe67 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 62 75   constraints, bu
2fe68 74 20 69 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61  t in this.** exa
2fe69 6d 70 6c 65 2c 20 74 68 65 20 74 68 69 72 64 20  mple, the third 
2fe6a 22 63 22 20 76 61 6c 75 65 20 69 73 20 61 6e 20  "c" value is an 
2fe6b 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 53 6f 20  inequality.  So 
2fe6c 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e  only two .** con
2fe6d 73 74 72 61 69 6e 74 73 20 61 72 65 20 63 6f 64  straints are cod
2fe6e 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ed.  This routin
2fe6f 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20  e will generate 
2fe70 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65  code to evaluate
2fe71 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 62 20 49  .** a==5 and b I
2fe72 4e 20 28 31 2c 32 2c 33 29 2e 20 20 54 68 65 20  N (1,2,3).  The 
2fe73 63 75 72 72 65 6e 74 20 76 61 6c 75 65 73 20 66  current values f
2fe74 6f 72 20 61 20 61 6e 64 20 62 20 77 69 6c 6c 20  or a and b will 
2fe75 62 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  be stored.** in 
2fe76 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
2fe77 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 69 6e  sters and the in
2fe78 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
2fe79 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74   register is ret
2fe7a 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  urned..**.** In 
2fe7b 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76  the example abov
2fe7c 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74  e nEq==2.  But t
2fe7d 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77  his subroutine w
2fe7e 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c  orks for any val
2fe7f 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63  ue.** of nEq inc
2fe80 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45  luding 0.  If nE
2fe81 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  q==0, this routi
2fe82 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e  ne is nearly a n
2fe83 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  o-op..** The onl
2fe84 79 20 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20  y thing it does 
2fe85 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  is allocate the 
2fe86 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d  pLevel->iMem mem
2fe87 6f 72 79 20 63 65 6c 6c 20 61 6e 64 0a 2a 2a 20  ory cell and.** 
2fe88 63 6f 6d 70 75 74 65 20 74 68 65 20 61 66 66 69  compute the affi
2fe89 6e 69 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  nity string..**.
2fe8a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2fe8b 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 73  always allocates
2fe8c 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65   at least one me
2fe8d 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 72 65  mory cell and re
2fe8e 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 69 6e 64  turns.** the ind
2fe8f 65 78 20 6f 66 20 74 68 61 74 20 6d 65 6d 6f 72  ex of that memor
2fe90 79 20 63 65 6c 6c 2e 20 54 68 65 20 63 6f 64 65  y cell. The code
2fe91 20 74 68 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74   that.** calls t
2fe92 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
2fe93 20 75 73 65 20 74 68 61 74 20 6d 65 6d 6f 72 79   use that memory
2fe94 20 63 65 6c 6c 20 74 6f 20 73 74 6f 72 65 20 74   cell to store t
2fe95 68 65 20 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 2a  he termination.*
2fe96 2a 20 6b 65 79 20 76 61 6c 75 65 20 6f 66 20 74  * key value of t
2fe97 68 65 20 6c 6f 6f 70 2e 20 20 49 66 20 6f 6e 65  he loop.  If one
2fe98 20 6f 72 20 6d 6f 72 65 20 49 4e 20 6f 70 65 72   or more IN oper
2fe99 61 74 6f 72 73 20 61 70 70 65 61 72 2c 20 74 68  ators appear, th
2fe9a 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  en.** this routi
2fe9b 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20  ne allocates an 
2fe9c 61 64 64 69 74 69 6f 6e 61 6c 20 6e 45 71 20 6d  additional nEq m
2fe9d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20  emory cells for 
2fe9e 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 2e  internal.** use.
2fe9f 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65  .**.** Before re
2fea0 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 41 66 66 20  turning, *pzAff 
2fea1 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
2fea2 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74  to a buffer cont
2fea3 61 69 6e 69 6e 67 20 61 0a 2a 2a 20 63 6f 70 79  aining a.** copy
2fea4 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61   of the column a
2fea5 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 6f  ffinity string o
2fea6 66 20 74 68 65 20 69 6e 64 65 78 20 61 6c 6c 6f  f the index allo
2fea7 63 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73  cated using.** s
2fea8 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29  qlite3DbMalloc()
2fea9 2e 20 45 78 63 65 70 74 2c 20 65 6e 74 72 69 65  . Except, entrie
2feaa 73 20 69 6e 20 74 68 65 20 63 6f 70 79 20 6f 66  s in the copy of
2feab 20 74 68 65 20 73 74 72 69 6e 67 20 61 73 73 6f   the string asso
2feac 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 65  ciated.** with e
2fead 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2feae 6e 74 73 20 74 68 61 74 20 75 73 65 20 4e 4f 4e  nts that use NON
2feaf 45 20 61 66 66 69 6e 69 74 79 20 61 72 65 20 73  E affinity are s
2feb0 65 74 20 74 6f 0a 2a 2a 20 53 51 4c 49 54 45 5f  et to.** SQLITE_
2feb1 41 46 46 5f 4e 4f 4e 45 2e 20 54 68 69 73 20 69  AFF_NONE. This i
2feb2 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53  s to deal with S
2feb3 51 4c 20 73 75 63 68 20 61 73 20 74 68 65 20 66  QL such as the f
2feb4 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
2feb5 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
2feb6 31 28 61 20 54 45 58 54 20 50 52 49 4d 41 52 59  1(a TEXT PRIMARY
2feb7 20 4b 45 59 2c 20 62 29 3b 0a 2a 2a 20 20 20 53   KEY, b);.**   S
2feb8 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 74  ELECT ... FROM t
2feb9 31 20 41 53 20 74 32 2c 20 74 31 20 57 48 45 52  1 AS t2, t1 WHER
2feba 45 20 74 31 2e 61 20 3d 20 74 32 2e 62 3b 0a 2a  E t1.a = t2.b;.*
2febb 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d  *.** In the exam
2febc 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20 69  ple above, the i
2febd 6e 64 65 78 20 6f 6e 20 74 31 28 61 29 20 68 61  ndex on t1(a) ha
2febe 73 20 54 45 58 54 20 61 66 66 69 6e 69 74 79 2e  s TEXT affinity.
2febf 20 42 75 74 20 73 69 6e 63 65 0a 2a 2a 20 74 68   But since.** th
2fec0 65 20 72 69 67 68 74 20 68 61 6e 64 20 73 69 64  e right hand sid
2fec1 65 20 6f 66 20 74 68 65 20 65 71 75 61 6c 69 74  e of the equalit
2fec2 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 28 74 32  y constraint (t2
2fec3 2e 62 29 20 68 61 73 20 4e 4f 4e 45 20 61 66 66  .b) has NONE aff
2fec4 69 6e 69 74 79 2c 0a 2a 2a 20 6e 6f 20 63 6f 6e  inity,.** no con
2fec5 76 65 72 73 69 6f 6e 20 73 68 6f 75 6c 64 20 62  version should b
2fec6 65 20 61 74 74 65 6d 70 74 65 64 20 62 65 66 6f  e attempted befo
2fec7 72 65 20 75 73 69 6e 67 20 61 20 74 32 2e 62 20  re using a t2.b 
2fec8 76 61 6c 75 65 20 61 73 20 70 61 72 74 20 6f 66  value as part of
2fec9 0a 2a 2a 20 61 20 6b 65 79 20 74 6f 20 73 65 61  .** a key to sea
2feca 72 63 68 20 74 68 65 20 69 6e 64 65 78 2e 20 48  rch the index. H
2fecb 65 6e 63 65 20 74 68 65 20 66 69 72 73 74 20 62  ence the first b
2fecc 79 74 65 20 69 6e 20 74 68 65 20 72 65 74 75 72  yte in the retur
2fecd 6e 65 64 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20  ned affinity.** 
2fece 73 74 72 69 6e 67 20 69 6e 20 74 68 69 73 20 65  string in this e
2fecf 78 61 6d 70 6c 65 20 77 6f 75 6c 64 20 62 65 20  xample would be 
2fed0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 41 46  set to SQLITE_AF
2fed1 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a 73 74 61 74 69  F_NONE..*/.stati
2fed2 63 20 69 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75  c int codeAllEqu
2fed3 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20 50 61  alityTerms(.  Pa
2fed4 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2fed5 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2fed6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2fed7 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20  eLevel *pLevel, 
2fed8 20 20 2f 2a 20 57 68 69 63 68 20 6e 65 73 74 65    /* Which neste
2fed9 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 46 52  d loop of the FR
2feda 4f 4d 20 77 65 20 61 72 65 20 63 6f 64 69 6e 67  OM we are coding
2fedb 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2fedc 65 20 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54  e *pWC,     /* T
2fedd 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2fede 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2fedf 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20 57 68  Ready,     /* Wh
2fee0 69 63 68 20 70 61 72 74 73 20 6f 66 20 46 52 4f  ich parts of FRO
2fee1 4d 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 62  M have not yet b
2fee2 65 65 6e 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69  een coded */.  i
2fee3 6e 74 20 6e 45 78 74 72 61 52 65 67 2c 20 20 20  nt nExtraReg,   
2fee4 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2fee5 66 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72  f extra register
2fee6 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f  s to allocate */
2fee7 0a 20 20 63 68 61 72 20 2a 2a 70 7a 41 66 66 20  .  char **pzAff 
2fee8 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
2fee9 20 53 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   Set to point to
2feea 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
2feeb 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71   */.){.  int nEq
2feec 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2feed 6e 45 71 3b 20 20 20 2f 2a 20 54 68 65 20 6e 75  nEq;   /* The nu
2feee 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e  mber of == or IN
2feef 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20   constraints to 
2fef0 63 6f 64 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a  code */.  Vdbe *
2fef1 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2fef2 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  e;      /* The v
2fef3 6d 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  m under construc
2fef4 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20  tion */.  Index 
2fef5 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
2fef6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
2fef7 6e 64 65 78 20 62 65 69 6e 67 20 75 73 65 64 20  ndex being used 
2fef8 66 6f 72 20 74 68 69 73 20 6c 6f 6f 70 20 2a 2f  for this loop */
2fef9 0a 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70 4c  .  int iCur = pL
2fefa 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 3b 20 20  evel->iTabCur;  
2fefb 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f   /* The cursor o
2fefc 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
2fefd 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2fefe 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  m;             /
2feff 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6e 73 74  * A single const
2ff00 72 61 69 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20  raint term */.  
2ff01 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
2ff02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ff03 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2ff04 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b 20  .  int regBase; 
2ff05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff06 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65   /* Base registe
2ff07 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 67 3b  r */.  int nReg;
2ff08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff09 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2ff0a 66 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 61  f registers to a
2ff0b 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 63 68 61  llocate */.  cha
2ff0c 72 20 2a 7a 41 66 66 3b 20 20 20 20 20 20 20 20  r *zAff;        
2ff0d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66             /* Af
2ff0e 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 74 6f  finity string to
2ff0f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a   return */..  /*
2ff10 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   This module is 
2ff11 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 6f 6e 20 71  only called on q
2ff12 75 65 72 79 20 70 6c 61 6e 73 20 74 68 61 74 20  uery plans that 
2ff13 75 73 65 20 61 6e 20 69 6e 64 65 78 2e 20 2a 2f  use an index. */
2ff14 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 76 65  .  assert( pLeve
2ff15 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2ff16 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  & WHERE_INDEXED 
2ff17 29 3b 0a 20 20 70 49 64 78 20 3d 20 70 4c 65 76  );.  pIdx = pLev
2ff18 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b  el->plan.u.pIdx;
2ff19 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
2ff1a 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f 72  t how many memor
2ff1b 79 20 63 65 6c 6c 73 20 77 65 20 77 69 6c 6c 20  y cells we will 
2ff1c 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c 6f 63 61  need then alloca
2ff1d 74 65 20 74 68 65 6d 2e 0a 20 20 2a 2f 0a 20 20  te them..  */.  
2ff1e 72 65 67 42 61 73 65 20 3d 20 70 50 61 72 73 65  regBase = pParse
2ff1f 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 6e 52  ->nMem + 1;.  nR
2ff20 65 67 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  eg = pLevel->pla
2ff21 6e 2e 6e 45 71 20 2b 20 6e 45 78 74 72 61 52 65  n.nEq + nExtraRe
2ff22 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  g;.  pParse->nMe
2ff23 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a 20 20 7a 41  m += nReg;..  zA
2ff24 66 66 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  ff = sqlite3DbSt
2ff25 72 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  rDup(pParse->db,
2ff26 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
2ff27 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78  inityStr(v, pIdx
2ff28 29 29 3b 0a 20 20 69 66 28 20 21 7a 41 66 66 20  ));.  if( !zAff 
2ff29 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  ){.    pParse->d
2ff2a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ff2b 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45  = 1;.  }..  /* E
2ff2c 76 61 6c 75 61 74 65 20 74 68 65 20 65 71 75 61  valuate the equa
2ff2d 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2ff2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2ff2f 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e  pIdx->nColumn>=n
2ff30 45 71 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b  Eq );.  for(j=0;
2ff31 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20   j<nEq; j++){.  
2ff32 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 69 6e    int r1;.    in
2ff33 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  t k = pIdx->aiCo
2ff34 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65  lumn[j];.    pTe
2ff35 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rm = findTerm(pW
2ff36 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52  C, iCur, k, notR
2ff37 65 61 64 79 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c  eady, pLevel->pl
2ff38 61 6e 2e 77 73 46 6c 61 67 73 2c 20 70 49 64 78  an.wsFlags, pIdx
2ff39 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  );.    if( NEVER
2ff3a 28 70 54 65 72 6d 3d 3d 30 29 20 29 20 62 72 65  (pTerm==0) ) bre
2ff3b 61 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ak;.    assert( 
2ff3c 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  (pTerm->wtFlags 
2ff3d 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30  & TERM_CODED)==0
2ff3e 20 29 3b 0a 20 20 20 20 72 31 20 3d 20 63 6f 64   );.    r1 = cod
2ff3f 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70 50  eEqualityTerm(pP
2ff40 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65  arse, pTerm, pLe
2ff41 76 65 6c 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b  vel, regBase+j);
2ff42 0a 20 20 20 20 69 66 28 20 72 31 21 3d 72 65 67  .    if( r1!=reg
2ff43 42 61 73 65 2b 6a 20 29 7b 0a 20 20 20 20 20 20  Base+j ){.      
2ff44 69 66 28 20 6e 52 65 67 3d 3d 31 20 29 7b 0a 20  if( nReg==1 ){. 
2ff45 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2ff46 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2ff47 72 73 65 2c 20 72 65 67 42 61 73 65 29 3b 0a 20  rse, regBase);. 
2ff48 20 20 20 20 20 20 20 72 65 67 42 61 73 65 20 3d         regBase =
2ff49 20 72 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   r1;.      }else
2ff4a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2ff4b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ff4c 50 5f 53 43 6f 70 79 2c 20 72 31 2c 20 72 65 67  P_SCopy, r1, reg
2ff4d 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 20 20 7d  Base+j);.      }
2ff4e 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63  .    }.    testc
2ff4f 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2ff50 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c  rator & WO_ISNUL
2ff51 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  L );.    testcas
2ff52 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  e( pTerm->eOpera
2ff53 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20  tor & WO_IN );. 
2ff54 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65     if( (pTerm->e
2ff55 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49  Operator & (WO_I
2ff56 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30  SNULL|WO_IN))==0
2ff57 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2ff58 70 52 69 67 68 74 20 3d 20 70 54 65 72 6d 2d 3e  pRight = pTerm->
2ff59 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2ff5a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2ff5b 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76  CodeIsNullJump(v
2ff5c 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73  , pRight, regBas
2ff5d 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64  e+j, pLevel->add
2ff5e 72 42 72 6b 29 3b 0a 20 20 20 20 20 20 69 66 28  rBrk);.      if(
2ff5f 20 7a 41 66 66 20 29 7b 0a 20 20 20 20 20 20 20   zAff ){.       
2ff60 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 6d 70   if( sqlite3Comp
2ff61 61 72 65 41 66 66 69 6e 69 74 79 28 70 52 69 67  areAffinity(pRig
2ff62 68 74 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51  ht, zAff[j])==SQ
2ff63 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b  LITE_AFF_NONE ){
2ff64 0a 20 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b  .          zAff[
2ff65 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  j] = SQLITE_AFF_
2ff66 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  NONE;.        }.
2ff67 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2ff68 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66  te3ExprNeedsNoAf
2ff69 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 52 69  finityChange(pRi
2ff6a 67 68 74 2c 20 7a 41 66 66 5b 6a 5d 29 20 29 7b  ght, zAff[j]) ){
2ff6b 0a 20 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b  .          zAff[
2ff6c 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  j] = SQLITE_AFF_
2ff6d 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  NONE;.        }.
2ff6e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2ff6f 7d 0a 20 20 2a 70 7a 41 66 66 20 3d 20 7a 41 66  }.  *pzAff = zAf
2ff70 66 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42  f;.  return regB
2ff71 61 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ase;.}../*.** Ge
2ff72 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2ff73 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
2ff74 20 69 4c 65 76 65 6c 2d 74 68 20 6c 6f 6f 70 20   iLevel-th loop 
2ff75 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
2ff76 75 73 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  use.** implement
2ff77 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20  ation described 
2ff78 62 79 20 70 57 49 6e 66 6f 2e 0a 2a 2f 0a 73 74  by pWInfo..*/.st
2ff79 61 74 69 63 20 42 69 74 6d 61 73 6b 20 63 6f 64  atic Bitmask cod
2ff7a 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74 28 0a 20  eOneLoopStart(. 
2ff7b 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
2ff7c 66 6f 2c 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74  fo,   /* Complet
2ff7d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  e information ab
2ff7e 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63 6c  out the WHERE cl
2ff7f 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c  ause */.  int iL
2ff80 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 2f  evel,          /
2ff81 2a 20 57 68 69 63 68 20 6c 65 76 65 6c 20 6f 66  * Which level of
2ff82 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 20 73 68 6f   pWInfo->a[] sho
2ff83 75 6c 64 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a  uld be coded */.
2ff84 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73    u16 wctrlFlags
2ff85 2c 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  ,      /* One of
2ff86 20 74 68 65 20 57 48 45 52 45 5f 2a 20 66 6c 61   the WHERE_* fla
2ff87 67 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71  gs defined in sq
2ff88 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a 20 20 42  liteInt.h */.  B
2ff89 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 20  itmask notReady 
2ff8a 20 20 20 20 2f 2a 20 57 68 69 63 68 20 74 61 62      /* Which tab
2ff8b 6c 65 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c  les are currentl
2ff8c 79 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 29  y available */.)
2ff8d 7b 0a 20 20 69 6e 74 20 6a 2c 20 6b 3b 20 20 20  {.  int j, k;   
2ff8e 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2ff8f 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
2ff90 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
2ff91 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
2ff92 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 74  cursor for the t
2ff93 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  able */.  int ad
2ff94 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20 20 2f  drNxt;         /
2ff95 2a 20 57 68 65 72 65 20 74 6f 20 6a 75 6d 70 20  * Where to jump 
2ff96 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
2ff97 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 61 73   the next IN cas
2ff98 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74 54  e */.  int omitT
2ff99 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54  able;       /* T
2ff9a 72 75 65 20 69 66 20 77 65 20 75 73 65 20 74 68  rue if we use th
2ff9b 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a  e index only */.
2ff9c 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 20    int bRev;     
2ff9d 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2ff9e 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 63 61  f we need to sca
2ff9f 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  n in reverse ord
2ffa0 65 72 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  er */.  WhereLev
2ffa1 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 2f 2a 20  el *pLevel;  /* 
2ffa2 54 68 65 20 77 68 65 72 65 20 6c 65 76 65 6c 20  The where level 
2ffa3 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
2ffa4 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2ffa5 43 3b 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f  C;    /* Decompo
2ffa6 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 6e  sition of the en
2ffa7 74 69 72 65 20 57 48 45 52 45 20 63 6c 61 75 73  tire WHERE claus
2ffa8 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  e */.  WhereTerm
2ffa9 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2ffaa 20 20 20 20 20 20 20 2f 2a 20 41 20 57 48 45 52         /* A WHER
2ffab 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 2a 2f  E clause term */
2ffac 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2ffad 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ffae 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2ffaf 6e 74 65 78 74 20 2a 2f 0a 20 20 56 64 62 65 20  ntext */.  Vdbe 
2ffb0 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2ffb1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ffb2 65 20 70 72 65 70 61 72 65 64 20 73 74 6d 74 20  e prepared stmt 
2ffb3 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
2ffb4 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ons */.  struct 
2ffb5 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54  SrcList_item *pT
2ffb6 61 62 49 74 65 6d 3b 20 20 2f 2a 20 46 52 4f 4d  abItem;  /* FROM
2ffb7 20 63 6c 61 75 73 65 20 74 65 72 6d 20 62 65 69   clause term bei
2ffb8 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e  ng coded */.  in
2ffb9 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20  t addrBrk;      
2ffba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ffbb 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72   Jump here to br
2ffbc 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  eak out of the l
2ffbd 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  oop */.  int add
2ffbe 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20  rCont;          
2ffbf 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
2ffc0 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
2ffc1 65 20 77 69 74 68 20 6e 65 78 74 20 63 79 63 6c  e with next cycl
2ffc2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69  e */.  int iRowi
2ffc3 64 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 20  dReg = 0;       
2ffc4 20 2f 2a 20 52 6f 77 69 64 20 69 73 20 73 74 6f   /* Rowid is sto
2ffc5 72 65 64 20 69 6e 20 74 68 69 73 20 72 65 67 69  red in this regi
2ffc6 73 74 65 72 2c 20 69 66 20 6e 6f 74 20 7a 65 72  ster, if not zer
2ffc7 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 6c 65  o */.  int iRele
2ffc8 61 73 65 52 65 67 20 3d 20 30 3b 20 20 20 20 20  aseReg = 0;     
2ffc9 20 2f 2a 20 54 65 6d 70 20 72 65 67 69 73 74 65   /* Temp registe
2ffca 72 20 74 6f 20 66 72 65 65 20 62 65 66 6f 72 65  r to free before
2ffcb 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 0a 20   returning */.. 
2ffcc 20 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66 6f   pParse = pWInfo
2ffcd 2d 3e 70 50 61 72 73 65 3b 0a 20 20 76 20 3d 20  ->pParse;.  v = 
2ffce 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2ffcf 20 70 57 43 20 3d 20 70 57 49 6e 66 6f 2d 3e 70   pWC = pWInfo->p
2ffd0 57 43 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20 26  WC;.  pLevel = &
2ffd1 70 57 49 6e 66 6f 2d 3e 61 5b 69 4c 65 76 65 6c  pWInfo->a[iLevel
2ffd2 5d 3b 0a 20 20 70 54 61 62 49 74 65 6d 20 3d 20  ];.  pTabItem = 
2ffd3 26 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73  &pWInfo->pTabLis
2ffd4 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72  t->a[pLevel->iFr
2ffd5 6f 6d 5d 3b 0a 20 20 69 43 75 72 20 3d 20 70 54  om];.  iCur = pT
2ffd6 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b  abItem->iCursor;
2ffd7 0a 20 20 62 52 65 76 20 3d 20 28 70 4c 65 76 65  .  bRev = (pLeve
2ffd8 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2ffd9 26 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 29  & WHERE_REVERSE)
2ffda 21 3d 30 3b 0a 20 20 6f 6d 69 74 54 61 62 6c 65  !=0;.  omitTable
2ffdb 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e   = (pLevel->plan
2ffdc 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2ffdd 5f 49 44 58 5f 4f 4e 4c 59 29 21 3d 30 20 0a 20  _IDX_ONLY)!=0 . 
2ffde 20 20 20 20 20 20 20 20 20 20 26 26 20 28 77 63            && (wc
2ffdf 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2ffe0 5f 46 4f 52 43 45 5f 54 41 42 4c 45 29 3d 3d 30  _FORCE_TABLE)==0
2ffe1 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 6c  ;..  /* Create l
2ffe2 61 62 65 6c 73 20 66 6f 72 20 74 68 65 20 22 62  abels for the "b
2ffe3 72 65 61 6b 22 20 61 6e 64 20 22 63 6f 6e 74 69  reak" and "conti
2ffe4 6e 75 65 22 20 69 6e 73 74 72 75 63 74 69 6f 6e  nue" instruction
2ffe5 73 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63  s.  ** for the c
2ffe6 75 72 72 65 6e 74 20 6c 6f 6f 70 2e 20 20 4a 75  urrent loop.  Ju
2ffe7 6d 70 20 74 6f 20 61 64 64 72 42 72 6b 20 74 6f  mp to addrBrk to
2ffe8 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 61 20   break out of a 
2ffe9 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 4a 75 6d 70 20  loop..  ** Jump 
2ffea 74 6f 20 63 6f 6e 74 20 74 6f 20 67 6f 20 69 6d  to cont to go im
2ffeb 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 74 68 65  mediately to the
2ffec 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20   next iteration 
2ffed 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 6f 70  of the.  ** loop
2ffee 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e  ..  **.  ** When
2ffef 20 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e 20   there is an IN 
2fff0 6f 70 65 72 61 74 6f 72 2c 20 77 65 20 61 6c 73  operator, we als
2fff1 6f 20 68 61 76 65 20 61 20 22 61 64 64 72 4e 78  o have a "addrNx
2fff2 74 22 20 6c 61 62 65 6c 20 74 68 61 74 0a 20 20  t" label that.  
2fff3 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 63 6f 6e 74  ** means to cont
2fff4 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65  inue with the ne
2fff5 78 74 20 49 4e 20 76 61 6c 75 65 20 63 6f 6d 62  xt IN value comb
2fff6 69 6e 61 74 69 6f 6e 2e 20 20 57 68 65 6e 0a 20  ination.  When. 
2fff7 20 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f   ** there are no
2fff8 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 69 6e   IN operators in
2fff9 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73   the constraints
2fffa 2c 20 74 68 65 20 22 61 64 64 72 4e 78 74 22 20  , the "addrNxt" 
2fffb 6c 61 62 65 6c 0a 20 20 2a 2a 20 69 73 20 74 68  label.  ** is th
2fffc 65 20 73 61 6d 65 20 61 73 20 22 61 64 64 72 42  e same as "addrB
2fffd 72 6b 22 2e 0a 20 20 2a 2f 0a 20 20 61 64 64 72  rk"..  */.  addr
2fffe 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64  Brk = pLevel->ad
2ffff 64 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e  drBrk = pLevel->
30000 61 64 64 72 4e 78 74 20 3d 20 73 71 6c 69 74 65  addrNxt = sqlite
30001 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
30002 29 3b 0a 20 20 61 64 64 72 43 6f 6e 74 20 3d 20  );.  addrCont = 
30003 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74  pLevel->addrCont
30004 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
30005 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f  keLabel(v);..  /
30006 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
30007 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
30008 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
30009 4e 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 0a  N, allocate and.
3000a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
3000b 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68  a memory cell th
3000c 61 74 20 72 65 63 6f 72 64 73 20 69 66 20 74 68  at records if th
3000d 69 73 20 74 61 62 6c 65 20 6d 61 74 63 68 65 73  is table matches
3000e 20 61 6e 79 0a 20 20 2a 2a 20 72 6f 77 20 6f 66   any.  ** row of
3000f 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20   the left table 
30010 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a  of the join..  *
30011 2f 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  /.  if( pLevel->
30012 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61 62  iFrom>0 && (pTab
30013 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65  Item[0].jointype
30014 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29   & JT_LEFT)!=0 )
30015 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4c  {.    pLevel->iL
30016 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70 50 61 72  eftJoin = ++pPar
30017 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
30018 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30019 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
3001a 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  , pLevel->iLeftJ
3001b 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  oin);.    VdbeCo
3001c 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 69 74 20  mment((v, "init 
3001d 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d 6d 61 74  LEFT JOIN no-mat
3001e 63 68 20 66 6c 61 67 22 29 29 3b 0a 20 20 7d 0a  ch flag"));.  }.
3001f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30020 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
30021 45 0a 20 20 69 66 28 20 20 28 70 4c 65 76 65 6c  E.  if(  (pLevel
30022 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
30023 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
30024 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 2f  BLE)!=0 ){.    /
30025 2a 20 43 61 73 65 20 30 3a 20 20 54 68 65 20 74  * Case 0:  The t
30026 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 61  able is a virtua
30027 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65 20 74 68  l-table.  Use th
30028 65 20 56 46 69 6c 74 65 72 20 61 6e 64 20 56 4e  e VFilter and VN
30029 65 78 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ext.    **      
3002a 20 20 20 20 74 6f 20 61 63 63 65 73 73 20 74 68      to access th
3002b 65 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20  e data..    */. 
3002c 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 2f     int iReg;   /
3002d 2a 20 50 33 20 56 61 6c 75 65 20 66 6f 72 20 4f  * P3 Value for O
3002e 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20  P_VFilter */.   
3002f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
30030 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d 20  nfo *pVtabIdx = 
30031 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
30032 56 74 61 62 49 64 78 3b 0a 20 20 20 20 69 6e 74  VtabIdx;.    int
30033 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70   nConstraint = p
30034 56 74 61 62 49 64 78 2d 3e 6e 43 6f 6e 73 74 72  VtabIdx->nConstr
30035 61 69 6e 74 3b 0a 20 20 20 20 73 74 72 75 63 74  aint;.    struct
30036 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
30037 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20  onstraint_usage 
30038 2a 61 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20  *aUsage =.      
30039 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3003a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3003b 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49            pVtabI
3003c 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55  dx->aConstraintU
3003d 73 61 67 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  sage;.    const 
3003e 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
3003f 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
30040 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0a 20  *aConstraint =. 
30041 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30042 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30043 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
30044 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74 72  VtabIdx->aConstr
30045 61 69 6e 74 3b 0a 0a 20 20 20 20 73 71 6c 69 74  aint;..    sqlit
30046 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28  e3ExprCachePush(
30047 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 52 65  pParse);.    iRe
30048 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
30049 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
3004a 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a  nConstraint+2);.
3004b 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 3d      for(j=1; j<=
3004c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b  nConstraint; j++
3004d 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30  ){.      for(k=0
3004e 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  ; k<nConstraint;
3004f 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   k++){.        i
30050 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67  f( aUsage[k].arg
30051 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20  vIndex==j ){.   
30052 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d         int iTerm
30053 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b   = aConstraint[k
30054 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ].iTermOffset;. 
30055 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
30056 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
30057 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e 70   pWC->a[iTerm].p
30058 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 52  Expr->pRight, iR
30059 65 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20 20 20  eg+j+1);.       
3005a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3005b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
3005c 20 20 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72    if( k==nConstr
3005d 61 69 6e 74 20 29 20 62 72 65 61 6b 3b 0a 20 20  aint ) break;.  
3005e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
3005f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30060 49 6e 74 65 67 65 72 2c 20 70 56 74 61 62 49 64  Integer, pVtabId
30061 78 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65 67 29  x->idxNum, iReg)
30062 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
30063 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
30064 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52 65 67  teger, j-1, iReg
30065 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
30066 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
30067 5f 56 46 69 6c 74 65 72 2c 20 69 43 75 72 2c 20  _VFilter, iCur, 
30068 61 64 64 72 42 72 6b 2c 20 69 52 65 67 2c 20 70  addrBrk, iReg, p
30069 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72 2c  VtabIdx->idxStr,
3006a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3006b 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d         pVtabIdx-
3006c 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
3006d 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46 20 3a  r ? P4_MPRINTF :
3006e 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
3006f 20 70 56 74 61 62 49 64 78 2d 3e 6e 65 65 64 54   pVtabIdx->needT
30070 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b  oFreeIdxStr = 0;
30071 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
30072 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b  nConstraint; j++
30073 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 55 73  ){.      if( aUs
30074 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20  age[j].omit ){. 
30075 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d         int iTerm
30076 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6a   = aConstraint[j
30077 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ].iTermOffset;. 
30078 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65         disableTe
30079 72 6d 28 70 4c 65 76 65 6c 2c 20 26 70 57 43 2d  rm(pLevel, &pWC-
3007a 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20 20  >a[iTerm]);.    
3007b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c    }.    }.    pL
3007c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e  evel->op = OP_VN
3007d 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ext;.    pLevel-
3007e 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20  >p1 = iCur;.    
3007f 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c  pLevel->p2 = sql
30080 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
30081 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  ddr(v);.    sqli
30082 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
30083 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52 65 67  nge(pParse, iReg
30084 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29  , nConstraint+2)
30085 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
30086 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65  rCachePop(pParse
30087 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  , 1);.  }else.#e
30088 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
30089 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
3008a 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 65 76 65   */..  if( pLeve
3008b 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3008c 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  & WHERE_ROWID_EQ
3008d 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
3008e 31 3a 20 20 57 65 20 63 61 6e 20 64 69 72 65 63  1:  We can direc
3008f 74 6c 79 20 72 65 66 65 72 65 6e 63 65 20 61 20  tly reference a 
30090 73 69 6e 67 6c 65 20 72 6f 77 20 75 73 69 6e 67  single row using
30091 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   an.    **      
30092 20 20 20 20 65 71 75 61 6c 69 74 79 20 63 6f 6d      equality com
30093 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20  parison against 
30094 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e  the ROWID field.
30095 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20    Or.    **     
30096 20 20 20 20 20 77 65 20 72 65 66 65 72 65 6e 63       we referenc
30097 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  e multiple rows 
30098 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64 20 49  using a "rowid I
30099 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a 20  N (...)".    ** 
3009a 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 75           constru
3009b 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ct..    */.    i
3009c 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c  ReleaseReg = sql
3009d 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
3009e 50 61 72 73 65 29 3b 0a 20 20 20 20 70 54 65 72  Parse);.    pTer
3009f 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  m = findTerm(pWC
300a0 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
300a1 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49  eady, WO_EQ|WO_I
300a2 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  N, 0);.    asser
300a3 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20  t( pTerm!=0 );. 
300a4 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
300a5 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a 20 20  ->pExpr!=0 );.  
300a6 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d    assert( pTerm-
300a7 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
300a8 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
300a9 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b   omitTable==0 );
300aa 0a 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d  .    iRowidReg =
300ab 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
300ac 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c  m(pParse, pTerm,
300ad 20 70 4c 65 76 65 6c 2c 20 69 52 65 6c 65 61 73   pLevel, iReleas
300ae 65 52 65 67 29 3b 0a 20 20 20 20 61 64 64 72 4e  eReg);.    addrN
300af 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64  xt = pLevel->add
300b0 72 4e 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  rNxt;.    sqlite
300b1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
300b2 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 52 6f  P_MustBeInt, iRo
300b3 77 69 64 52 65 67 2c 20 61 64 64 72 4e 78 74 29  widReg, addrNxt)
300b4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
300b5 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
300b6 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
300b7 64 64 72 4e 78 74 2c 20 69 52 6f 77 69 64 52 65  ddrNxt, iRowidRe
300b8 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  g);.    sqlite3E
300b9 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50  xprCacheStore(pP
300ba 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20  arse, iCur, -1, 
300bb 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
300bc 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
300bd 22 70 6b 22 29 29 3b 0a 20 20 20 20 70 4c 65 76  "pk"));.    pLev
300be 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70  el->op = OP_Noop
300bf 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c  ;.  }else if( pL
300c0 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
300c1 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
300c2 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a  _RANGE ){.    /*
300c3 20 43 61 73 65 20 32 3a 20 20 57 65 20 68 61 76   Case 2:  We hav
300c4 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20  e an inequality 
300c5 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e  comparison again
300c6 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65  st the ROWID fie
300c7 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ld..    */.    i
300c8 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e  nt testOp = OP_N
300c9 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 20 73 74 61  oop;.    int sta
300ca 72 74 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45  rt;.    int memE
300cb 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20  ndValue = 0;.   
300cc 20 57 68 65 72 65 54 65 72 6d 20 2a 70 53 74 61   WhereTerm *pSta
300cd 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20  rt, *pEnd;..    
300ce 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c  assert( omitTabl
300cf 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 53 74 61  e==0 );.    pSta
300d0 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rt = findTerm(pW
300d1 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
300d2 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
300d3 47 45 2c 20 30 29 3b 0a 20 20 20 20 70 45 6e 64  GE, 0);.    pEnd
300d4 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c   = findTerm(pWC,
300d5 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65   iCur, -1, notRe
300d6 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45  ady, WO_LT|WO_LE
300d7 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 62 52  , 0);.    if( bR
300d8 65 76 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72  ev ){.      pTer
300d9 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 20 20  m = pStart;.    
300da 20 20 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b    pStart = pEnd;
300db 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70 54  .      pEnd = pT
300dc 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  erm;.    }.    i
300dd 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20  f( pStart ){.   
300de 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 20     Expr *pX;    
300df 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
300e0 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
300e1 64 65 66 69 6e 65 73 20 74 68 65 20 73 74 61 72  defines the star
300e2 74 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20  t bound */.     
300e3 20 69 6e 74 20 72 31 2c 20 72 54 65 6d 70 3b 20   int r1, rTemp; 
300e4 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
300e5 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20  ers for holding 
300e6 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 61  the start bounda
300e7 72 79 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  ry */..      /* 
300e8 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
300e9 6e 73 74 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78  nstant maps TK_x
300ea 78 20 63 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72  x codes into cor
300eb 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20  responding .    
300ec 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65    ** seek opcode
300ed 73 2e 20 20 49 74 20 64 65 70 65 6e 64 73 20 6f  s.  It depends o
300ee 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6f  n a particular o
300ef 72 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78  rdering of TK_xx
300f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
300f1 63 6f 6e 73 74 20 75 38 20 61 4d 6f 76 65 4f 70  const u8 aMoveOp
300f2 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  [] = {.         
300f3 20 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f    /* TK_GT */  O
300f4 50 5f 53 65 65 6b 47 74 2c 0a 20 20 20 20 20 20  P_SeekGt,.      
300f5 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f       /* TK_LE */
300f6 20 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20    OP_SeekLe,.   
300f7 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54          /* TK_LT
300f8 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a   */  OP_SeekLt,.
300f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b             /* TK
300fa 5f 47 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47  _GE */  OP_SeekG
300fb 65 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  e.      };.     
300fc 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d   assert( TK_LE==
300fd 54 4b 5f 47 54 2b 31 20 29 3b 20 20 20 20 20 20  TK_GT+1 );      
300fe 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
300ff 20 6f 72 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20   ordering.. */. 
30100 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
30101 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20  LT==TK_GT+2 );  
30102 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74      /*  ... of t
30103 68 65 20 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e  he TK_xx values.
30104 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  .. */.      asse
30105 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54  rt( TK_GE==TK_GT
30106 2b 33 20 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e  +3 );      /*  .
30107 2e 2e 20 69 73 20 63 6f 72 72 65 63 63 74 2e 20  .. is correcct. 
30108 2a 2f 0a 0a 20 20 20 20 20 20 70 58 20 3d 20 70  */..      pX = p
30109 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20  Start->pExpr;.  
3010a 20 20 20 20 61 73 73 65 72 74 28 20 70 58 21 3d      assert( pX!=
3010b 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
3010c 74 28 20 70 53 74 61 72 74 2d 3e 6c 65 66 74 43  t( pStart->leftC
3010d 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20  ursor==iCur );. 
3010e 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
3010f 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
30110 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
30111 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20 20 20 20  , &rTemp);.     
30112 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30113 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58  p3(v, aMoveOp[pX
30114 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75  ->op-TK_GT], iCu
30115 72 2c 20 61 64 64 72 42 72 6b 2c 20 72 31 29 3b  r, addrBrk, r1);
30116 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
30117 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20  nt((v, "pk"));. 
30118 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
30119 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
3011a 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20  nge(pParse, r1, 
3011b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
3011c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
3011d 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a  pParse, rTemp);.
3011e 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72        disableTer
3011f 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61 72 74  m(pLevel, pStart
30120 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
30121 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30122 64 64 4f 70 32 28 76 2c 20 62 52 65 76 20 3f 20  ddOp2(v, bRev ? 
30123 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77  OP_Last : OP_Rew
30124 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42  ind, iCur, addrB
30125 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  rk);.    }.    i
30126 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20  f( pEnd ){.     
30127 20 45 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20   Expr *pX;.     
30128 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70   pX = pEnd->pExp
30129 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  r;.      assert(
3012a 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   pX!=0 );.      
3012b 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65  assert( pEnd->le
3012c 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
3012d 3b 0a 20 20 20 20 20 20 6d 65 6d 45 6e 64 56 61  ;.      memEndVa
3012e 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  lue = ++pParse->
3012f 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69  nMem;.      sqli
30130 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
30131 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20  se, pX->pRight, 
30132 6d 65 6d 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20  memEndValue);.  
30133 20 20 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d      if( pX->op==
30134 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d  TK_LT || pX->op=
30135 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 20 20  =TK_GT ){.      
30136 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20    testOp = bRev 
30137 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b  ? OP_Le : OP_Ge;
30138 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
30139 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62        testOp = b
3013a 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50  Rev ? OP_Lt : OP
3013b 5f 47 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _Gt;.      }.   
3013c 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
3013d 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20  Level, pEnd);.  
3013e 20 20 7d 0a 20 20 20 20 73 74 61 72 74 20 3d 20    }.    start = 
3013f 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
30140 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 70  ntAddr(v);.    p
30141 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76  Level->op = bRev
30142 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f   ? OP_Prev : OP_
30143 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Next;.    pLevel
30144 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20  ->p1 = iCur;.   
30145 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74   pLevel->p2 = st
30146 61 72 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  art;.    pLevel-
30147 3e 70 35 20 3d 20 28 70 53 74 61 72 74 3d 3d 30  >p5 = (pStart==0
30148 20 26 26 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a   && pEnd==0) ?1:
30149 30 3b 0a 20 20 20 20 69 66 28 20 74 65 73 74 4f  0;.    if( testO
3014a 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20  p!=OP_Noop ){.  
3014b 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20      iRowidReg = 
3014c 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71  iReleaseReg = sq
3014d 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
3014e 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
3014f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
30150 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
30151 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  ur, iRowidReg);.
30152 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
30153 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
30154 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52  se, iCur, -1, iR
30155 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
30156 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30157 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d  3(v, testOp, mem
30158 45 6e 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72  EndValue, addrBr
30159 6b 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20  k, iRowidReg);. 
3015a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3015b 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
3015c 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c  TE_AFF_NUMERIC |
3015d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
3015e 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  LL);.    }.  }el
3015f 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
30160 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57  lan.wsFlags & (W
30161 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
30162 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  E|WHERE_COLUMN_E
30163 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73  Q) ){.    /* Cas
30164 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73 69 6e  e 3: A scan usin
30165 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20  g an index..    
30166 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
30167 20 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75    The WHERE clau
30168 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a  se may contain z
30169 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75 61  ero or more equa
3016a 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20 20 20  lity .    **    
3016b 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d 3d 22       terms ("=="
3016c 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61 74 6f   or "IN" operato
3016d 72 73 29 20 74 68 61 74 20 72 65 66 65 72 20 74  rs) that refer t
3016e 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20  o the N.    **  
3016f 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74         left-most
30170 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
30171 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c  index. It may al
30172 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a  so contain.    *
30173 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75 61  *         inequa
30174 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
30175 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d   (>, <, >= or <=
30176 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64  ) on the indexed
30177 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
30178 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65  column that imme
30179 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20  diately follows 
3017a 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73  the N equalities
3017b 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20  . Only .    **  
3017c 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74         the right
3017d 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e  -most column can
3017e 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   be an inequalit
3017f 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75 73  y - the rest mus
30180 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
30181 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20 61 6e   use the "==" an
30182 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73  d "IN" operators
30183 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
30184 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20  f the .    **   
30185 20 20 20 20 20 20 69 6e 64 65 78 20 69 73 20 6f        index is o
30186 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20  n (x,y,z), then 
30187 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c  the following cl
30188 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20 0a 20  auses are all . 
30189 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6f 70     **         op
3018a 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a  timized:.    **.
3018b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
3018c 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20    x=5.    **    
3018d 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
3018e 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20  y=10.    **     
3018f 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
30190 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  <10.    **      
30191 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3e        x=5 AND y>
30192 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a  5 AND y<10.    *
30193 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
30194 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d   AND y=5 AND z<=
30195 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  10.    **.    **
30196 20 20 20 20 20 20 20 20 20 54 68 65 20 7a 3c 31           The z<1
30197 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  0 term of the fo
30198 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62  llowing cannot b
30199 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20  e used, only.   
3019a 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20   **         the 
3019b 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a  x=5 term:.    **
3019c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3019d 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a     x=5 AND z<10.
3019e 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
3019f 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20 7a        N may be z
301a0 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72 65  ero if there are
301a1 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
301a2 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20  traints..    ** 
301a3 20 20 20 20 20 20 20 20 49 66 20 74 68 65 72 65          If there
301a4 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69   are no inequali
301a5 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  ty constraints, 
301a6 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20 20 20  then N is at.   
301a7 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 61 73   **         leas
301a8 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  t one..    **.  
301a9 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69    **         Thi
301aa 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75  s case is also u
301ab 73 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61  sed when there a
301ac 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  re no WHERE clau
301ad 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  se.    **       
301ae 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75    constraints bu
301af 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65  t an index is se
301b0 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69  lected anyway, i
301b1 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 20  n order.    **  
301b2 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20         to force 
301b3 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
301b4 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61   to conform to a
301b5 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20  n ORDER BY..    
301b6 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20 61 53 74  */  .    int aSt
301b7 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20  artOp[] = {.    
301b8 20 20 30 2c 0a 20 20 20 20 20 20 30 2c 0a 20 20    0,.      0,.  
301b9 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20      OP_Rewind,  
301ba 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28           /* 2: (
301bb 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  !start_constrain
301bc 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26  ts && startEq &&
301bd 20 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20    !bRev) */.    
301be 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20 20 20    OP_Last,      
301bf 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28 21 73         /* 3: (!s
301c0 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
301c1 20 26 26 20 73 74 61 72 74 45 71 20 26 26 20 20   && startEq &&  
301c2 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20   bRev) */.      
301c3 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20 20 20 20  OP_SeekGt,      
301c4 20 20 20 20 20 2f 2a 20 34 3a 20 28 73 74 61 72       /* 4: (star
301c5 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
301c6 26 20 21 73 74 61 72 74 45 71 20 26 26 20 21 62  & !startEq && !b
301c7 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
301c8 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20 20 20 20  _SeekLt,        
301c9 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f     /* 5: (start_
301ca 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
301cb 21 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65  !startEq &&  bRe
301cc 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
301cd 65 65 6b 47 65 2c 20 20 20 20 20 20 20 20 20 20  eekGe,          
301ce 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f 63 6f   /* 6: (start_co
301cf 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73  nstraints  &&  s
301d0 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29  tartEq && !bRev)
301d1 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65   */.      OP_See
301d2 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 2f  kLe            /
301d3 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73  * 7: (start_cons
301d4 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74 61  traints  &&  sta
301d5 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20 2a  rtEq &&  bRev) *
301d6 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  /.    };.    int
301d7 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20 20   aEndOp[] = {.  
301d8 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20 20      OP_Noop,    
301d9 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20 28           /* 0: (
301da 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73  !end_constraints
301db 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64  ) */.      OP_Id
301dc 78 47 45 2c 20 20 20 20 20 20 20 20 20 20 20 20  xGE,            
301dd 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74  /* 1: (end_const
301de 72 61 69 6e 74 73 20 26 26 20 21 62 52 65 76 29  raints && !bRev)
301df 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78   */.      OP_Idx
301e0 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  LT             /
301e1 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72  * 2: (end_constr
301e2 61 69 6e 74 73 20 26 26 20 62 52 65 76 29 20 2a  aints && bRev) *
301e3 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  /.    };.    int
301e4 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70   nEq = pLevel->p
301e5 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20 69 6e 74  lan.nEq;.    int
301e6 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b   isMinQuery = 0;
301e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
301e8 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d  this is an optim
301e9 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69 6e 28  ized SELECT min(
301ea 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  x).. */.    int 
301eb 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20  regBase;        
301ec 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
301ed 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
301ee 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  g constraint val
301ef 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ues */.    int r
301f0 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
301f1 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
301f2 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
301f3 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67  WhereTerm *pRang
301f4 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20  eStart = 0;  /* 
301f5 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  Inequality const
301f6 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20 73  raint at range s
301f7 74 61 72 74 20 2a 2f 0a 20 20 20 20 57 68 65 72  tart */.    Wher
301f8 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64  eTerm *pRangeEnd
301f9 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71   = 0;    /* Ineq
301fa 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
301fb 74 20 61 74 20 72 61 6e 67 65 20 65 6e 64 20 2a  t at range end *
301fc 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 45  /.    int startE
301fd 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
301fe 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61     /* True if ra
301ff 6e 67 65 20 73 74 61 72 74 20 75 73 65 73 20 3d  nge start uses =
30200 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20  =, >= or <= */. 
30201 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20 20 20     int endEq;   
30202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30203 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65  /* True if range
30204 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d   end uses ==, >=
30205 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e   or <= */.    in
30206 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  t start_constrai
30207 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74  nts;       /* St
30208 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69 73 20  art of range is 
30209 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20  constrained */. 
3020a 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69     int nConstrai
3020b 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
3020c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6e  /* Number of con
3020d 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 2a 2f  straint terms */
3020e 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
3020f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
30210 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c 20 62   index we will b
30211 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69  e using */.    i
30212 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20  nt iIdxCur;     
30213 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
30214 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 69  cursor for the i
30215 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ndex */.    int 
30216 6e 45 78 74 72 61 52 65 67 20 3d 20 30 3b 20 20  nExtraReg = 0;  
30217 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78   /* Number of ex
30218 74 72 61 20 72 65 67 69 73 74 65 72 73 20 6e 65  tra registers ne
30219 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eded */.    int 
3021a 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
3021b 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20   /* Instruction 
3021c 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 63 68  opcode */.    ch
3021d 61 72 20 2a 7a 41 66 66 3b 0a 0a 20 20 20 20 70  ar *zAff;..    p
3021e 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  Idx = pLevel->pl
3021f 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 69  an.u.pIdx;.    i
30220 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d  IdxCur = pLevel-
30221 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 6b 20  >iIdxCur;.    k 
30222 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
30223 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a 20 43 6f  [nEq];     /* Co
30224 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75 61 6c  lumn for inequal
30225 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ity constraints 
30226 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  */..    /* If th
30227 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66 69 65  is loop satisfie
30228 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72 20 28  s a sort order (
30229 70 4f 72 64 65 72 42 79 29 20 72 65 71 75 65 73  pOrderBy) reques
3022a 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a 20 77  t that .    ** w
3022b 61 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  as passed to thi
3022c 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d  s function to im
3022d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45 43  plement a "SELEC
3022e 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20  T min(x) ..." . 
3022f 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68 65     ** query, the
30230 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  n the caller wil
30231 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65  l only allow the
30232 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72   loop to run for
30233 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65  .    ** a single
30234 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68 69 73   iteration. This
30235 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
30236 66 69 72 73 74 20 72 6f 77 20 72 65 74 75 72 6e  first row return
30237 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  ed.    ** should
30238 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55 4c 4c   not have a NULL
30239 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
3023a 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20   'x'. If column 
3023b 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20 74 68  'x' is.    ** th
3023c 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66 74 65  e first one afte
3023d 72 20 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69  r the nEq equali
3023e 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69  ty constraints i
3023f 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20 20 20  n the index,.   
30240 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69 72 65   ** this require
30241 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c 20 68  s some special h
30242 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a  andling..    */.
30243 20 20 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c      if( (wctrlFl
30244 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52 42  ags&WHERE_ORDERB
30245 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20 20 26  Y_MIN)!=0.     &
30246 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  & (pLevel->plan.
30247 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52  wsFlags&WHERE_OR
30248 44 45 52 42 59 29 0a 20 20 20 20 20 26 26 20 28  DERBY).     && (
30249 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45  pIdx->nColumn>nE
3024a 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  q).    ){.      
3024b 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72 64 65  /* assert( pOrde
3024c 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b  rBy->nExpr==1 );
3024d 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61 73 73   */.      /* ass
3024e 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 61  ert( pOrderBy->a
3024f 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75  [0].pExpr->iColu
30250 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75  mn==pIdx->aiColu
30251 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20 20  mn[nEq] ); */.  
30252 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79 20 3d      isMinQuery =
30253 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61   1;.      nExtra
30254 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a  Reg = 1;.    }..
30255 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20      /* Find any 
30256 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
30257 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f 72 20  raint terms for 
30258 74 68 65 20 73 74 61 72 74 20 61 6e 64 20 65 6e  the start and en
30259 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  d .    ** of the
3025a 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a 2f 0a   range. .    */.
3025b 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
3025c 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3025d 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29  HERE_TOP_LIMIT )
3025e 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65 45 6e  {.      pRangeEn
3025f 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  d = findTerm(pWC
30260 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65  , iCur, k, notRe
30261 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c  ady, (WO_LT|WO_L
30262 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  E), pIdx);.     
30263 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0a   nExtraReg = 1;.
30264 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
30265 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30266 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c  gs & WHERE_BTM_L
30267 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20 70 52  IMIT ){.      pR
30268 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69 6e 64  angeStart = find
30269 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
3026a 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f  k, notReady, (WO
3026b 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78  _GT|WO_GE), pIdx
3026c 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52  );.      nExtraR
3026d 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  eg = 1;.    }.. 
3026e 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
3026f 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  ode to evaluate 
30270 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  all constraint t
30271 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72  erms using == or
30272 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73   IN.    ** and s
30273 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  tore the values 
30274 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 20 69  of those terms i
30275 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  n an array of re
30276 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a 20 73  gisters.    ** s
30277 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 42 61  tarting at regBa
30278 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  se..    */.    r
30279 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41 6c 6c  egBase = codeAll
3027a 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20  EqualityTerms(. 
3027b 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 70         pParse, p
3027c 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e 6f 74 52  Level, pWC, notR
3027d 65 61 64 79 2c 20 6e 45 78 74 72 61 52 65 67 2c  eady, nExtraReg,
3027e 20 26 7a 41 66 66 0a 20 20 20 20 29 3b 0a 20 20   &zAff.    );.  
3027f 20 20 61 64 64 72 4e 78 74 20 3d 20 70 4c 65 76    addrNxt = pLev
30280 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a 0a 20 20  el->addrNxt;..  
30281 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64    /* If we are d
30282 6f 69 6e 67 20 61 20 72 65 76 65 72 73 65 20 6f  oing a reverse o
30283 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20  rder scan on an 
30284 61 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c  ascending index,
30285 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72   or.    ** a for
30286 77 61 72 64 20 6f 72 64 65 72 20 73 63 61 6e 20  ward order scan 
30287 6f 6e 20 61 20 64 65 73 63 65 6e 64 69 6e 67 20  on a descending 
30288 69 6e 64 65 78 2c 20 69 6e 74 65 72 63 68 61 6e  index, interchan
30289 67 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73  ge the .    ** s
3028a 74 61 72 74 20 61 6e 64 20 65 6e 64 20 74 65 72  tart and end ter
3028b 6d 73 20 28 70 52 61 6e 67 65 53 74 61 72 74 20  ms (pRangeStart 
3028c 61 6e 64 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a  and pRangeEnd)..
3028d 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62      */.    if( b
3028e 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72  Rev==(pIdx->aSor
3028f 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c  tOrder[nEq]==SQL
30290 49 54 45 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20  ITE_SO_ASC) ){. 
30291 20 20 20 20 20 53 57 41 50 28 57 68 65 72 65 54       SWAP(WhereT
30292 65 72 6d 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64  erm *, pRangeEnd
30293 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29 3b 0a  , pRangeStart);.
30294 20 20 20 20 7d 0a 0a 20 20 20 20 74 65 73 74 63      }..    testc
30295 61 73 65 28 20 70 52 61 6e 67 65 53 74 61 72 74  ase( pRangeStart
30296 20 26 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d   && pRangeStart-
30297 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
30298 4c 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  LE );.    testca
30299 73 65 28 20 70 52 61 6e 67 65 53 74 61 72 74 20  se( pRangeStart 
3029a 26 26 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e  && pRangeStart->
3029b 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47  eOperator & WO_G
3029c 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  E );.    testcas
3029d 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20  e( pRangeEnd && 
3029e 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72  pRangeEnd->eOper
3029f 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a  ator & WO_LE );.
302a0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
302a1 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67  angeEnd && pRang
302a2 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20  eEnd->eOperator 
302a3 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73  & WO_GE );.    s
302a4 74 61 72 74 45 71 20 3d 20 21 70 52 61 6e 67 65  tartEq = !pRange
302a5 53 74 61 72 74 20 7c 7c 20 70 52 61 6e 67 65 53  Start || pRangeS
302a6 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
302a7 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
302a8 0a 20 20 20 20 65 6e 64 45 71 20 3d 20 20 20 21  .    endEq =   !
302a9 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61  pRangeEnd || pRa
302aa 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
302ab 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45  r & (WO_LE|WO_GE
302ac 29 3b 0a 20 20 20 20 73 74 61 72 74 5f 63 6f 6e  );.    start_con
302ad 73 74 72 61 69 6e 74 73 20 3d 20 70 52 61 6e 67  straints = pRang
302ae 65 53 74 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b  eStart || nEq>0;
302af 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68  ..    /* Seek th
302b0 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 20 74  e index cursor t
302b1 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
302b2 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20  he range. */.   
302b3 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e   nConstraint = n
302b4 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e  Eq;.    if( pRan
302b5 67 65 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20  geStart ){.     
302b6 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
302b7 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70 45 78  pRangeStart->pEx
302b8 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
302b9 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
302ba 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74  e(pParse, pRight
302bb 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a  , regBase+nEq);.
302bc 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
302bd 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28  rCodeIsNullJump(
302be 76 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42 61  v, pRight, regBa
302bf 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29  se+nEq, addrNxt)
302c0 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 41 66 66  ;.      if( zAff
302c1 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
302c2 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
302c3 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a  finity(pRight, z
302c4 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d  Aff[nConstraint]
302c5 29 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  )==SQLITE_AFF_NO
302c6 4e 45 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  NE){.          /
302c7 2a 20 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70  * Since the comp
302c8 61 72 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20  arison is to be 
302c9 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e  performed with n
302ca 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20  o conversions.  
302cb 20 20 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 69          ** appli
302cc 65 64 20 74 6f 20 74 68 65 20 6f 70 65 72 61 6e  ed to the operan
302cd 64 73 2c 20 73 65 74 20 74 68 65 20 61 66 66 69  ds, set the affi
302ce 6e 69 74 79 20 74 6f 20 61 70 70 6c 79 20 74 6f  nity to apply to
302cf 20 70 52 69 67 68 74 20 74 6f 20 0a 20 20 20 20   pRight to .    
302d0 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f        ** SQLITE_
302d1 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20  AFF_NONE.  */.  
302d2 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f          zAff[nCo
302d3 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49  nstraint] = SQLI
302d4 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20  TE_AFF_NONE;.   
302d5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
302d6 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65  f( sqlite3ExprNe
302d7 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61  edsNoAffinityCha
302d8 6e 67 65 28 70 52 69 67 68 74 2c 20 7a 41 66 66  nge(pRight, zAff
302d9 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 20 29  [nConstraint]) )
302da 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 41 66 66  {.          zAff
302db 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20  [nConstraint] = 
302dc 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
302dd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
302de 20 7d 20 20 0a 20 20 20 20 20 20 6e 43 6f 6e 73   }  .      nCons
302df 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 65  traint++;.    }e
302e0 6c 73 65 20 69 66 28 20 69 73 4d 69 6e 51 75 65  lse if( isMinQue
302e1 72 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ry ){.      sqli
302e2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
302e3 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
302e4 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20  Base+nEq);.     
302e5 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a   nConstraint++;.
302e6 20 20 20 20 20 20 73 74 61 72 74 45 71 20 3d 20        startEq = 
302e7 30 3b 0a 20 20 20 20 20 20 73 74 61 72 74 5f 63  0;.      start_c
302e8 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 31 3b 0a  onstraints = 1;.
302e9 20 20 20 20 7d 0a 20 20 20 20 63 6f 64 65 41 70      }.    codeAp
302ea 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72  plyAffinity(pPar
302eb 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  se, regBase, nCo
302ec 6e 73 74 72 61 69 6e 74 2c 20 7a 41 66 66 29 3b  nstraint, zAff);
302ed 0a 20 20 20 20 6f 70 20 3d 20 61 53 74 61 72 74  .    op = aStart
302ee 4f 70 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74 72  Op[(start_constr
302ef 61 69 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74 61  aints<<2) + (sta
302f0 72 74 45 71 3c 3c 31 29 20 2b 20 62 52 65 76 5d  rtEq<<1) + bRev]
302f1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 70  ;.    assert( op
302f2 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63  !=0 );.    testc
302f3 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 52 65 77 69  ase( op==OP_Rewi
302f4 6e 64 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  nd );.    testca
302f5 73 65 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74 20  se( op==OP_Last 
302f6 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
302f7 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29   op==OP_SeekGt )
302f8 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
302f9 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 3b  op==OP_SeekGe );
302fa 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
302fb 70 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a  p==OP_SeekLe );.
302fc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
302fd 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 29 3b 0a 20  ==OP_SeekLt );. 
302fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
302ff 64 4f 70 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69  dOp4Int(v, op, i
30300 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c  IdxCur, addrNxt,
30301 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74   regBase, nConst
30302 72 61 69 6e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  raint);..    /* 
30303 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65 20 66  Load the value f
30304 6f 72 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74  or the inequalit
30305 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20  y constraint at 
30306 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20  the end of the. 
30307 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 69 66 20     ** range (if 
30308 61 6e 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  any)..    */.   
30309 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e   nConstraint = n
3030a 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e  Eq;.    if( pRan
3030b 67 65 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45  geEnd ){.      E
3030c 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52  xpr *pRight = pR
3030d 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72 2d 3e  angeEnd->pExpr->
3030e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73 71  pRight;.      sq
3030f 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 52 65  lite3ExprCacheRe
30310 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67  move(pParse, reg
30311 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20  Base+nEq);.     
30312 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
30313 28 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c  (pParse, pRight,
30314 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20   regBase+nEq);. 
30315 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
30316 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76  CodeIsNullJump(v
30317 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73  , pRight, regBas
30318 65 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b  e+nEq, addrNxt);
30319 0a 20 20 20 20 20 20 69 66 28 20 7a 41 66 66 20  .      if( zAff 
3031a 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
3031b 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
3031c 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41  inity(pRight, zA
3031d 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29  ff[nConstraint])
3031e 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
3031f 45 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  E){.          /*
30320 20 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61   Since the compa
30321 72 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70  rison is to be p
30322 65 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f  erformed with no
30323 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20   conversions.   
30324 20 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65         ** applie
30325 64 20 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64  d to the operand
30326 73 2c 20 73 65 74 20 74 68 65 20 61 66 66 69 6e  s, set the affin
30327 69 74 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20  ity to apply to 
30328 70 52 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20  pRight to .     
30329 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41       ** SQLITE_A
3032a 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20  FF_NONE.  */.   
3032b 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e         zAff[nCon
3032c 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54  straint] = SQLIT
3032d 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20  E_AFF_NONE;.    
3032e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
3032f 28 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65  ( sqlite3ExprNee
30330 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e  dsNoAffinityChan
30331 67 65 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b  ge(pRight, zAff[
30332 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 20 29 7b  nConstraint]) ){
30333 0a 20 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b  .          zAff[
30334 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53  nConstraint] = S
30335 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
30336 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30337 7d 20 20 0a 20 20 20 20 20 20 63 6f 64 65 41 70  }  .      codeAp
30338 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72  plyAffinity(pPar
30339 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71  se, regBase, nEq
3033a 2b 31 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 20  +1, zAff);.     
3033b 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a   nConstraint++;.
3033c 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
3033d 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e  3DbFree(pParse->
3033e 64 62 2c 20 7a 41 66 66 29 3b 0a 0a 20 20 20 20  db, zAff);..    
3033f 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f  /* Top of the lo
30340 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 70  op body */.    p
30341 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69  Level->p2 = sqli
30342 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
30343 64 72 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  dr(v);..    /* C
30344 68 65 63 6b 20 69 66 20 74 68 65 20 69 6e 64 65  heck if the inde
30345 78 20 63 75 72 73 6f 72 20 69 73 20 70 61 73 74  x cursor is past
30346 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
30347 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6f 70  range. */.    op
30348 20 3d 20 61 45 6e 64 4f 70 5b 28 70 52 61 6e 67   = aEndOp[(pRang
30349 65 45 6e 64 20 7c 7c 20 6e 45 71 29 20 2a 20 28  eEnd || nEq) * (
3034a 31 20 2b 20 62 52 65 76 29 5d 3b 0a 20 20 20 20  1 + bRev)];.    
3034b 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
3034c 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20 74 65 73  _Noop );.    tes
3034d 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64  tcase( op==OP_Id
3034e 78 47 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63  xGE );.    testc
3034f 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c  ase( op==OP_IdxL
30350 54 20 29 3b 0a 20 20 20 20 69 66 28 20 6f 70 21  T );.    if( op!
30351 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20  =OP_Noop ){.    
30352 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30353 4f 70 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49  Op4Int(v, op, iI
30354 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20  dxCur, addrNxt, 
30355 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72  regBase, nConstr
30356 61 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  aint);.      sql
30357 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
30358 28 76 2c 20 65 6e 64 45 71 21 3d 62 52 65 76 20  (v, endEq!=bRev 
30359 3f 31 3a 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  ?1:0);.    }..  
3035a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
3035b 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  e inequality con
3035c 73 74 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20  straints, check 
3035d 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20  that the value. 
3035e 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62     ** of the tab
3035f 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  le column that t
30360 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  he inequality co
30361 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e  ntrains is not N
30362 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69  ULL..    ** If i
30363 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68  t is, jump to th
30364 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  e next iteration
30365 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20   of the loop..  
30366 20 20 2a 2f 0a 20 20 20 20 72 31 20 3d 20 73 71    */.    r1 = sq
30367 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
30368 70 50 61 72 73 65 29 3b 0a 20 20 20 20 74 65 73  pParse);.    tes
30369 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
3036a 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3036b 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b  ERE_BTM_LIMIT );
3036c 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
3036d 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3036e 61 67 73 20 26 20 57 48 45 52 45 5f 54 4f 50 5f  ags & WHERE_TOP_
3036f 4c 49 4d 49 54 20 29 3b 0a 20 20 20 20 69 66 28  LIMIT );.    if(
30370 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
30371 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 42  Flags & (WHERE_B
30372 54 4d 5f 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54  TM_LIMIT|WHERE_T
30373 4f 50 5f 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20  OP_LIMIT) ){.   
30374 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
30375 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
30376 6e 2c 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c  n, iIdxCur, nEq,
30377 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
30378 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
30379 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20   OP_IsNull, r1, 
3037a 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d  addrCont);.    }
3037b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
3037c 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
3037d 65 2c 20 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  e, r1);..    /* 
3037e 53 65 65 6b 20 74 68 65 20 74 61 62 6c 65 20 63  Seek the table c
3037f 75 72 73 6f 72 2c 20 69 66 20 72 65 71 75 69 72  ursor, if requir
30380 65 64 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c  ed */.    disabl
30381 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52  eTerm(pLevel, pR
30382 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20  angeStart);.    
30383 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
30384 65 6c 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a  el, pRangeEnd);.
30385 20 20 20 20 69 66 28 20 21 6f 6d 69 74 54 61 62      if( !omitTab
30386 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 52 6f 77  le ){.      iRow
30387 69 64 52 65 67 20 3d 20 69 52 65 6c 65 61 73 65  idReg = iRelease
30388 52 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Reg = sqlite3Get
30389 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
3038a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3038b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
3038c 64 78 52 6f 77 69 64 2c 20 69 49 64 78 43 75 72  dxRowid, iIdxCur
3038d 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20  , iRowidReg);.  
3038e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3038f 61 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65  acheStore(pParse
30390 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77  , iCur, -1, iRow
30391 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71  idReg);.      sq
30392 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30393 76 2c 20 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72  v, OP_Seek, iCur
30394 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 20 20 2f  , iRowidReg);  /
30395 2a 20 44 65 66 65 72 72 65 64 20 73 65 65 6b 20  * Deferred seek 
30396 2a 2f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  */.    }..    /*
30397 20 52 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74   Record the inst
30398 72 75 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20  ruction used to 
30399 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
3039a 6f 70 2e 20 44 69 73 61 62 6c 65 20 0a 20 20 20  op. Disable .   
3039b 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   ** WHERE clause
3039c 20 74 65 72 6d 73 20 6d 61 64 65 20 72 65 64 75   terms made redu
3039d 6e 64 61 6e 74 20 62 79 20 74 68 65 20 69 6e 64  ndant by the ind
3039e 65 78 20 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20  ex range scan.. 
3039f 20 20 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c     */.    pLevel
303a0 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50  ->op = bRev ? OP
303a1 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b  _Prev : OP_Next;
303a2 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20  .    pLevel->p1 
303a3 3d 20 69 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c  = iIdxCur;.  }el
303a4 73 65 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  se..#ifndef SQLI
303a5 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
303a6 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c  IZATION.  if( pL
303a7 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
303a8 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49  gs & WHERE_MULTI
303a9 5f 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61  _OR ){.    /* Ca
303aa 73 65 20 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f  se 4:  Two or mo
303ab 72 65 20 73 65 70 61 72 61 74 65 6c 79 20 69 6e  re separately in
303ac 64 65 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e  dexed terms conn
303ad 65 63 74 65 64 20 62 79 20 4f 52 0a 20 20 20 20  ected by OR.    
303ae 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c  **.    ** Exampl
303af 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
303b0 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
303b1 74 31 28 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20  t1(a,b,c,d);.   
303b2 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44   **   CREATE IND
303b3 45 58 20 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a  EX i1 ON t1(a);.
303b4 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
303b5 49 4e 44 45 58 20 69 32 20 4f 4e 20 74 31 28 62  INDEX i2 ON t1(b
303b6 29 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41  );.    **   CREA
303b7 54 45 20 49 4e 44 45 58 20 69 33 20 4f 4e 20 74  TE INDEX i3 ON t
303b8 31 28 63 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  1(c);.    **.   
303b9 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46   **   SELECT * F
303ba 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3d 35  ROM t1 WHERE a=5
303bb 20 4f 52 20 62 3d 37 20 4f 52 20 28 63 3d 31 31   OR b=7 OR (c=11
303bc 20 41 4e 44 20 64 3d 31 33 29 0a 20 20 20 20 2a   AND d=13).    *
303bd 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 65 20  *.    ** In the 
303be 65 78 61 6d 70 6c 65 2c 20 74 68 65 72 65 20 61  example, there a
303bf 72 65 20 74 68 72 65 65 20 69 6e 64 65 78 65 64  re three indexed
303c0 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64   terms connected
303c1 20 62 79 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54   by OR..    ** T
303c2 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f  he top of the lo
303c3 6f 70 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68  op looks like th
303c4 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  is:.    **.    *
303c5 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20  *          Null 
303c6 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
303c7 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68         # Zero th
303c8 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20  e rowset in reg 
303c9 31 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  1.    **.    ** 
303ca 54 68 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 69  Then, for each i
303cb 6e 64 65 78 65 64 20 74 65 72 6d 2c 20 74 68 65  ndexed term, the
303cc 20 66 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20   following. The 
303cd 61 72 67 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20  arguments to.   
303ce 20 2a 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61   ** RowSetTest a
303cf 72 65 20 73 75 63 68 20 74 68 61 74 20 74 68 65  re such that the
303d0 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75   rowid of the cu
303d1 72 72 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73  rrent row is ins
303d2 65 72 74 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74  erted.    ** int
303d3 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66  o the RowSet. If
303d4 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 70   it is already p
303d5 72 65 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20  resent, control 
303d6 73 6b 69 70 73 20 74 68 65 0a 20 20 20 20 2a 2a  skips the.    **
303d7 20 47 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e   Gosub opcode an
303d8 64 20 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74  d jumps straight
303d9 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
303da 65 72 61 74 65 64 20 62 79 20 57 68 65 72 65 45  erated by WhereE
303db 6e 64 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  nd()..    **.   
303dc 20 2a 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74   **        sqlit
303dd 65 33 57 68 65 72 65 42 65 67 69 6e 28 3c 74 65  e3WhereBegin(<te
303de 72 6d 3e 29 0a 20 20 20 20 2a 2a 20 20 20 20 20  rm>).    **     
303df 20 20 20 20 20 52 6f 77 53 65 74 54 65 73 74 20       RowSetTest 
303e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303e1 20 23 20 49 6e 73 65 72 74 20 72 6f 77 69 64 20   # Insert rowid 
303e2 69 6e 74 6f 20 72 6f 77 73 65 74 0a 20 20 20 20  into rowset.    
303e3 2a 2a 20 20 20 20 20 20 20 20 20 20 47 6f 73 75  **          Gosu
303e4 62 20 20 20 20 20 20 32 20 41 0a 20 20 20 20 2a  b      2 A.    *
303e5 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  *        sqlite3
303e6 57 68 65 72 65 45 6e 64 28 29 0a 20 20 20 20 2a  WhereEnd().    *
303e7 2a 0a 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69  *.    ** Followi
303e8 6e 67 20 74 68 65 20 61 62 6f 76 65 2c 20 63 6f  ng the above, co
303e9 64 65 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20  de to terminate 
303ea 74 68 65 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20  the loop. Label 
303eb 41 2c 20 74 68 65 20 74 61 72 67 65 74 0a 20 20  A, the target.  
303ec 20 20 2a 2a 20 6f 66 20 74 68 65 20 47 6f 73 75    ** of the Gosu
303ed 62 20 61 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74  b above, jumps t
303ee 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f  o the instructio
303ef 6e 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68  n right after th
303f0 65 20 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20  e Goto..    **. 
303f1 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e     **          N
303f2 75 6c 6c 20 20 20 20 20 20 20 31 20 20 20 20 20  ull       1     
303f3 20 20 20 20 20 20 20 20 20 20 20 23 20 5a 65 72             # Zer
303f4 6f 20 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20  o the rowset in 
303f5 72 65 67 20 31 0a 20 20 20 20 2a 2a 20 20 20 20  reg 1.    **    
303f6 20 20 20 20 20 20 47 6f 74 6f 20 20 20 20 20 20        Goto      
303f7 20 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20   B              
303f8 20 20 23 20 54 68 65 20 6c 6f 6f 70 20 69 73 20    # The loop is 
303f9 66 69 6e 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a  finished..    **
303fa 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 41 3a  .    **       A:
303fb 20 3c 6c 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20   <loop body>    
303fc 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20 52               # R
303fd 65 74 75 72 6e 20 64 61 74 61 2c 20 77 68 61 74  eturn data, what
303fe 65 76 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ever..    **.   
303ff 20 2a 2a 20 20 20 20 20 20 20 20 20 20 52 65 74   **          Ret
30400 75 72 6e 20 20 20 20 20 32 20 20 20 20 20 20 20  urn     2       
30401 20 20 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20           # Jump 
30402 62 61 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75  back to the Gosu
30403 62 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  b.    **.    ** 
30404 20 20 20 20 20 20 42 3a 20 3c 61 66 74 65 72 20        B: <after 
30405 74 68 65 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a  the loop>.    **
30406 0a 20 20 20 20 2a 2f 0a 20 20 20 20 57 68 65 72  .    */.    Wher
30407 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20  eClause *pOrWc; 
30408 20 20 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61     /* The OR-cla
30409 75 73 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69  use broken out i
3040a 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a  nto subterms */.
3040b 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
3040c 46 69 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69  Final;     /* Fi
3040d 6e 61 6c 20 73 75 62 74 65 72 6d 20 77 69 74 68  nal subterm with
3040e 69 6e 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65  in the OR-clause
3040f 2e 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 74  . */.    SrcList
30410 20 6f 6e 65 54 61 62 3b 20 20 20 20 20 20 20 20   oneTab;        
30411 2f 2a 20 53 68 6f 72 74 65 6e 65 64 20 74 61 62  /* Shortened tab
30412 6c 65 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20  le list */..    
30413 69 6e 74 20 72 65 67 52 65 74 75 72 6e 20 3d 20  int regReturn = 
30414 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20  ++pParse->nMem; 
30415 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
30416 69 73 74 65 72 20 75 73 65 64 20 77 69 74 68 20  ister used with 
30417 4f 50 5f 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20  OP_Gosub */.    
30418 69 6e 74 20 72 65 67 52 6f 77 73 65 74 20 3d 20  int regRowset = 
30419 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
3041a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
3041b 69 73 74 65 72 20 66 6f 72 20 52 6f 77 53 65 74  ister for RowSet
3041c 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69   object */.    i
3041d 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b  nt regRowid = 0;
3041e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3041f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
30420 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77  ster holding row
30421 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c  id */.    int iL
30422 6f 6f 70 42 6f 64 79 20 3d 20 73 71 6c 69 74 65  oopBody = sqlite
30423 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
30424 29 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20  );  /* Start of 
30425 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20  loop body */.   
30426 20 69 6e 74 20 69 52 65 74 49 6e 69 74 3b 20 20   int iRetInit;  
30427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30428 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
30429 64 72 65 73 73 20 6f 66 20 72 65 67 52 65 74 75  dress of regRetu
3042a 72 6e 20 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69  rn init */.    i
3042b 6e 74 20 69 69 3b 0a 20 20 20 0a 20 20 20 20 70  nt ii;.   .    p
3042c 54 65 72 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70  Term = pLevel->p
3042d 6c 61 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20  lan.u.pTerm;.   
3042e 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d   assert( pTerm!=
3042f 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
30430 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
30431 72 3d 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20  r==WO_OR );.    
30432 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e  assert( (pTerm->
30433 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f  wtFlags & TERM_O
30434 52 49 4e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20  RINFO)!=0 );.   
30435 20 70 4f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d   pOrWc = &pTerm-
30436 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a  >u.pOrInfo->wc;.
30437 20 20 20 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f      pFinal = &pO
30438 72 57 63 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54  rWc->a[pOrWc->nT
30439 65 72 6d 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20  erm-1];..    /* 
3043a 53 65 74 20 75 70 20 61 20 53 72 63 4c 69 73 74  Set up a SrcList
3043b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74   containing just
3043c 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
3043d 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73   scanned by this
3043e 20 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e   loop. */.    on
3043f 65 54 61 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20  eTab.nSrc = 1;. 
30440 20 20 20 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63     oneTab.nAlloc
30441 20 3d 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62   = 1;.    oneTab
30442 2e 61 5b 30 5d 20 3d 20 2a 70 54 61 62 49 74 65  .a[0] = *pTabIte
30443 6d 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69  m;..    /* Initi
30444 61 6c 69 7a 65 20 74 68 65 20 72 6f 77 73 65 74  alize the rowset
30445 20 72 65 67 69 73 74 65 72 20 74 6f 20 63 6f 6e   register to con
30446 74 61 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51  tain NULL. An SQ
30447 4c 20 4e 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a  L NULL is .    *
30448 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  * equivalent to 
30449 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 2e  an empty rowset.
3044a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
3044b 6c 73 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 72  lso initialize r
3044c 65 67 52 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74  egReturn to cont
3044d 61 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20  ain the address 
3044e 6f 66 20 74 68 65 20 69 6e 73 74 72 75 63 74 69  of the instructi
3044f 6f 6e 20 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64  on .    ** immed
30450 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67  iately following
30451 20 74 68 65 20 4f 50 5f 52 65 74 75 72 6e 20 61   the OP_Return a
30452 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  t the bottom of 
30453 74 68 65 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20  the loop. This. 
30454 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65     ** is require
30455 64 20 69 6e 20 61 20 66 65 77 20 6f 62 73 63 75  d in a few obscu
30456 72 65 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73  re LEFT JOIN cas
30457 65 73 20 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c  es where control
30458 20 6a 75 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76   jumps.    ** ov
30459 65 72 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  er the top of th
3045a 65 20 6c 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20  e loop into the 
3045b 62 6f 64 79 20 6f 66 20 69 74 2e 20 49 6e 20 74  body of it. In t
3045c 68 69 73 20 63 61 73 65 20 74 68 65 20 0a 20 20  his case the .  
3045d 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 72 65 73    ** correct res
3045e 70 6f 6e 73 65 20 66 6f 72 20 74 68 65 20 65 6e  ponse for the en
3045f 64 2d 6f 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28  d-of-loop code (
30460 74 68 65 20 4f 50 5f 52 65 74 75 72 6e 29 20 69  the OP_Return) i
30461 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c  s to .    ** fal
30462 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
30463 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
30464 6e 2c 20 6a 75 73 74 20 61 73 20 61 6e 20 4f 50  n, just as an OP
30465 5f 4e 65 78 74 20 64 6f 65 73 20 69 66 0a 20 20  _Next does if.  
30466 20 20 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61    ** called on a
30467 6e 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  n uninitialized 
30468 63 75 72 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  cursor..    */. 
30469 20 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61     if( (wctrlFla
3046a 67 73 20 26 20 57 48 45 52 45 5f 44 55 50 4c 49  gs & WHERE_DUPLI
3046b 43 41 54 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a  CATES_OK)==0 ){.
3046c 20 20 20 20 20 20 72 65 67 52 6f 77 73 65 74 20        regRowset 
3046d 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
3046e 3b 0a 20 20 20 20 20 20 72 65 67 52 6f 77 69 64  ;.      regRowid
3046f 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
30470 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
30471 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
30472 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77  _Null, 0, regRow
30473 73 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  set);.    }.    
30474 69 52 65 74 49 6e 69 74 20 3d 20 73 71 6c 69 74  iRetInit = sqlit
30475 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
30476 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
30477 65 67 52 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20  egReturn);..    
30478 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72  for(ii=0; ii<pOr
30479 57 63 2d 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29  Wc->nTerm; ii++)
3047a 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
3047b 6d 20 2a 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f  m *pOrTerm = &pO
3047c 72 57 63 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20  rWc->a[ii];.    
3047d 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c    if( pOrTerm->l
3047e 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
3047f 7c 7c 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  || pOrTerm->eOpe
30480 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b  rator==WO_AND ){
30481 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 49 6e  .        WhereIn
30482 66 6f 20 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20  fo *pSubWInfo;  
30483 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20          /* Info 
30484 66 6f 72 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65  for single OR-te
30485 72 6d 20 73 63 61 6e 20 2a 2f 0a 20 20 20 20 20  rm scan */.     
30486 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75     /* Loop throu
30487 67 68 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73  gh table entries
30488 20 74 68 61 74 20 6d 61 74 63 68 20 74 65 72 6d   that match term
30489 20 70 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20   pOrTerm. */.   
3048a 20 20 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d       pSubWInfo =
3048b 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
3048c 69 6e 28 70 50 61 72 73 65 2c 20 26 6f 6e 65 54  in(pParse, &oneT
3048d 61 62 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78  ab, pOrTerm->pEx
3048e 70 72 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  pr, 0,.         
3048f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57                 W
30490 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c  HERE_OMIT_OPEN |
30491 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53   WHERE_OMIT_CLOS
30492 45 20 7c 20 57 48 45 52 45 5f 46 4f 52 43 45 5f  E | WHERE_FORCE_
30493 54 41 42 4c 45 29 3b 0a 20 20 20 20 20 20 20 20  TABLE);.        
30494 69 66 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b  if( pSubWInfo ){
30495 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28  .          if( (
30496 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
30497 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b  RE_DUPLICATES_OK
30498 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
30499 20 20 20 20 69 6e 74 20 69 53 65 74 20 3d 20 28      int iSet = (
3049a 28 69 69 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72  (ii==pOrWc->nTer
3049b 6d 2d 31 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20  m-1)?-1:ii);.   
3049c 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a           int r;.
3049d 20 20 20 20 20 20 20 20 20 20 20 20 72 20 3d 20              r = 
3049e 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
3049f 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  etColumn(pParse,
304a0 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c   pTabItem->pTab,
304a1 20 2d 31 2c 20 69 43 75 72 2c 20 0a 20 20 20 20   -1, iCur, .    
304a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304a4 20 20 20 20 20 72 65 67 52 6f 77 69 64 2c 20 30       regRowid, 0
304a5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
304a6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
304a7 49 6e 74 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74  Int(v, OP_RowSet
304a8 54 65 73 74 2c 20 72 65 67 52 6f 77 73 65 74 2c  Test, regRowset,
304a9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
304aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304ab 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72    sqlite3VdbeCur
304ac 72 65 6e 74 41 64 64 72 28 76 29 2b 32 2c 20 72  rentAddr(v)+2, r
304ad 2c 20 69 53 65 74 29 3b 0a 20 20 20 20 20 20 20  , iSet);.       
304ae 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
304af 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
304b0 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
304b1 67 52 65 74 75 72 6e 2c 20 69 4c 6f 6f 70 42 6f  gReturn, iLoopBo
304b2 64 79 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  dy);..          
304b3 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20 6c 6f  /* Finish the lo
304b4 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65  op through table
304b5 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6d 61   entries that ma
304b6 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65 72 6d  tch term pOrTerm
304b7 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73  . */.          s
304b8 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
304b9 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20  SubWInfo);.     
304ba 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
304bb 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
304bc 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 52  beChangeP1(v, iR
304bd 65 74 49 6e 69 74 2c 20 73 71 6c 69 74 65 33 56  etInit, sqlite3V
304be 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
304bf 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74  ));.    /* sqlit
304c0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
304c1 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
304c2 6f 77 73 65 74 29 3b 20 2a 2f 0a 20 20 20 20 73  owset); */.    s
304c3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
304c4 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
304c5 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29  pLevel->addrBrk)
304c6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
304c7 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
304c8 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20   iLoopBody);..  
304c9 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f    pLevel->op = O
304ca 50 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20 70 4c  P_Return;.    pL
304cb 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52 65  evel->p1 = regRe
304cc 74 75 72 6e 3b 0a 20 20 20 20 64 69 73 61 62 6c  turn;.    disabl
304cd 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54  eTerm(pLevel, pT
304ce 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  erm);.  }else.#e
304cf 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
304d0 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
304d1 49 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20 20  ION */..  {.    
304d2 2f 2a 20 43 61 73 65 20 35 3a 20 20 54 68 65 72  /* Case 5:  Ther
304d3 65 20 69 73 20 6e 6f 20 75 73 61 62 6c 65 20 69  e is no usable i
304d4 6e 64 65 78 2e 20 20 57 65 20 6d 75 73 74 20 64  ndex.  We must d
304d5 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20  o a complete.   
304d6 20 2a 2a 20 20 20 20 20 20 20 20 20 20 73 63 61   **          sca
304d7 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20  n of the entire 
304d8 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
304d9 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
304da 38 20 61 53 74 65 70 5b 5d 20 3d 20 7b 20 4f 50  8 aStep[] = { OP
304db 5f 4e 65 78 74 2c 20 4f 50 5f 50 72 65 76 20 7d  _Next, OP_Prev }
304dc 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  ;.    static con
304dd 73 74 20 75 38 20 61 53 74 61 72 74 5b 5d 20 3d  st u8 aStart[] =
304de 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 4f 50   { OP_Rewind, OP
304df 5f 4c 61 73 74 20 7d 3b 0a 20 20 20 20 61 73 73  _Last };.    ass
304e0 65 72 74 28 20 62 52 65 76 3d 3d 30 20 7c 7c 20  ert( bRev==0 || 
304e1 62 52 65 76 3d 3d 31 20 29 3b 0a 20 20 20 20 61  bRev==1 );.    a
304e2 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65  ssert( omitTable
304e3 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4c 65 76 65  ==0 );.    pLeve
304e4 6c 2d 3e 6f 70 20 3d 20 61 53 74 65 70 5b 62 52  l->op = aStep[bR
304e5 65 76 5d 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  ev];.    pLevel-
304e6 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20  >p1 = iCur;.    
304e7 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20 2b  pLevel->p2 = 1 +
304e8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
304e9 70 32 28 76 2c 20 61 53 74 61 72 74 5b 62 52 65  p2(v, aStart[bRe
304ea 76 5d 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72  v], iCur, addrBr
304eb 6b 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  k);.    pLevel->
304ec 70 35 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d 54  p5 = SQLITE_STMT
304ed 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f  STATUS_FULLSCAN_
304ee 53 54 45 50 3b 0a 20 20 7d 0a 20 20 6e 6f 74 52  STEP;.  }.  notR
304ef 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b  eady &= ~getMask
304f0 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20  (pWC->pMaskSet, 
304f1 69 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e 73  iCur);..  /* Ins
304f2 65 72 74 20 63 6f 64 65 20 74 6f 20 74 65 73 74  ert code to test
304f3 20 65 76 65 72 79 20 73 75 62 65 78 70 72 65 73   every subexpres
304f4 73 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65  sion that can be
304f5 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a 2a   completely.  **
304f6 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20   computed using 
304f7 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 20  the current set 
304f8 6f 66 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  of tables..  */.
304f9 20 20 6b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70    k = 0;.  for(p
304fa 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70  Term=pWC->a, j=p
304fb 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20  WC->nTerm; j>0; 
304fc 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20  j--, pTerm++){. 
304fd 20 20 20 45 78 70 72 20 2a 70 45 3b 0a 20 20 20     Expr *pE;.   
304fe 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
304ff 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
30500 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20  _VIRTUAL );.    
30501 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
30502 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
30503 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 69 66 28  CODED );.    if(
30504 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
30505 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c  & (TERM_VIRTUAL|
30506 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f  TERM_CODED) ) co
30507 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
30508 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c  (pTerm->prereqAl
30509 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d 30  l & notReady)!=0
3050a 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
3050b 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78   pE = pTerm->pEx
3050c 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  pr;.    assert( 
3050d 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  pE!=0 );.    if(
3050e 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
3050f 69 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72  in && !ExprHasPr
30510 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
30511 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20  omJoin) ){.     
30512 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
30513 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
30514 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
30515 70 45 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51  pE, addrCont, SQ
30516 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
30517 3b 0a 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20  ;.    k = 1;.   
30518 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
30519 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20  |= TERM_CODED;. 
3051a 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c   }..  /* For a L
3051b 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20  EFT OUTER JOIN, 
3051c 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  generate code th
3051d 61 74 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74  at will record t
3051e 68 65 20 66 61 63 74 20 74 68 61 74 0a 20 20 2a  he fact that.  *
3051f 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72  * at least one r
30520 6f 77 20 6f 66 20 74 68 65 20 72 69 67 68 74 20  ow of the right 
30521 74 61 62 6c 65 20 68 61 73 20 6d 61 74 63 68 65  table has matche
30522 64 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65  d the left table
30523 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  .  .  */.  if( p
30524 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
30525 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e   ){.    pLevel->
30526 61 64 64 72 46 69 72 73 74 20 3d 20 73 71 6c 69  addrFirst = sqli
30527 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
30528 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr(v);.    sqlit
30529 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
3052a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70  OP_Integer, 1, p
3052b 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
3052c 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
3052d 6e 74 28 28 76 2c 20 22 72 65 63 6f 72 64 20 4c  nt((v, "record L
3052e 45 46 54 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b  EFT JOIN hit"));
3052f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
30530 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
30531 65 29 3b 0a 20 20 20 20 66 6f 72 28 70 54 65 72  e);.    for(pTer
30532 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20 6a  m=pWC->a, j=0; j
30533 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b  <pWC->nTerm; j++
30534 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
30535 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
30536 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
30537 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20  M_VIRTUAL );.   
30538 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
30539 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
3053a 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20  RM_CODED );.    
3053b 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46    if( pTerm->wtF
3053c 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52  lags & (TERM_VIR
3053d 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29  TUAL|TERM_CODED)
3053e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
3053f 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70     if( (pTerm->p
30540 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65  rereqAll & notRe
30541 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e  ady)!=0 ) contin
30542 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
30543 28 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 29  ( pTerm->pExpr )
30544 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
30545 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
30546 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c  e, pTerm->pExpr,
30547 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54   addrCont, SQLIT
30548 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
30549 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c       pTerm->wtFl
3054a 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
3054b 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  D;.    }.  }.  s
3054c 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
3054d 70 52 65 67 28 70 50 61 72 73 65 2c 20 69 52 65  pReg(pParse, iRe
3054e 6c 65 61 73 65 52 65 67 29 3b 0a 0a 20 20 72 65  leaseReg);..  re
3054f 74 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0a 7d  turn notReady;.}
30550 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
30551 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a  LITE_TEST)./*.**
30552 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
30553 61 72 69 61 62 6c 65 20 68 6f 6c 64 73 20 61 20  ariable holds a 
30554 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e  text description
30555 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e 20 67   of query plan g
30556 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79 20 74  enerated.** by t
30557 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
30558 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68  all to sqlite3Wh
30559 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45 61 63  ereBegin().  Eac
3055a 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72 65 42  h call to WhereB
3055b 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69 74  egin.** overwrit
3055c 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e  es the previous.
3055d 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69    This informati
3055e 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  on is used for t
3055f 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 6e  esting and.** an
30560 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a  alysis only..*/.
30561 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
30562 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30563 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20 2f  an[BMS*2*40];  /
30564 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6a 6f  * Text of the jo
30565 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  in */.static int
30566 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20 20   nQPlan = 0;    
30567 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
30568 74 20 66 72 65 65 20 73 6c 6f 77 20 69 6e 20 5f  t free slow in _
30569 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a  query_plan[] */.
3056a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
3056b 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  E_TEST */.../*.*
3056c 2a 20 46 72 65 65 20 61 20 57 68 65 72 65 49 6e  * Free a WhereIn
3056d 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  fo structure.*/.
3056e 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
3056f 65 49 6e 66 6f 46 72 65 65 28 73 71 6c 69 74 65  eInfoFree(sqlite
30570 33 20 2a 64 62 2c 20 57 68 65 72 65 49 6e 66 6f  3 *db, WhereInfo
30571 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66 28   *pWInfo){.  if(
30572 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 69   pWInfo ){.    i
30573 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
30574 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65  0; i<pWInfo->nLe
30575 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  vel; i++){.     
30576 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
30577 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57 49  nfo *pInfo = pWI
30578 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49 6e  nfo->a[i].pIdxIn
30579 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  fo;.      if( pI
3057a 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  nfo ){.        /
3057b 2a 20 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d  * assert( pInfo-
3057c 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
3057d 72 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  r==0 || db->mall
3057e 6f 63 46 61 69 6c 65 64 20 29 3b 20 2a 2f 0a 20  ocFailed ); */. 
3057f 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f         if( pInfo
30580 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
30581 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  tr ){.          
30582 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e  sqlite3_free(pIn
30583 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20  fo->idxStr);.   
30584 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
30585 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
30586 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d   pInfo);.      }
30587 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 65 72 65  .    }.    where
30588 43 6c 61 75 73 65 43 6c 65 61 72 28 70 57 49 6e  ClauseClear(pWIn
30589 66 6f 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73 71  fo->pWC);.    sq
3058a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3058b 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a  pWInfo);.  }.}..
3058c 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
3058d 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
3058e 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66   the loop used f
3058f 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  or WHERE clause 
30590 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54  processing..** T
30591 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
30592 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
30593 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74  an opaque struct
30594 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ure that contain
30595 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s.** information
30596 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72 6d 69   needed to termi
30597 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20  nate the loop.  
30598 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 69  Later, the calli
30599 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68  ng routine.** sh
3059a 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69  ould invoke sqli
3059b 74 65 33 57 68 65 72 65 45 6e 64 28 29 20 77 69  te3WhereEnd() wi
3059c 74 68 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  th the return va
3059d 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
3059e 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  tion.** in order
3059f 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
305a0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
305a1 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ocessing..**.** 
305a2 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
305a3 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  rs, this routine
305a4 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a   returns NULL..*
305a5 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69  *.** The basic i
305a6 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61 20 6e  dea is to do a n
305a7 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20  ested loop, one 
305a8 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20 74 61  loop for each ta
305a9 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52  ble in.** the FR
305aa 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 73  OM clause of a s
305ab 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52 54 20  elect.  (INSERT 
305ac 61 6e 64 20 55 50 44 41 54 45 20 73 74 61 74 65  and UPDATE state
305ad 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a 2a 2a  ments are the.**
305ae 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c 45 43   same as a SELEC
305af 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20 73 69  T with only a si
305b0 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68  ngle table in th
305b1 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20  e FROM clause.) 
305b2 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c   For.** example,
305b3 20 69 66 20 74 68 65 20 53 51 4c 20 69 73 20 74   if the SQL is t
305b4 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
305b5 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
305b6 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52 45 20  1, t2, t3 WHERE 
305b7 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ...;.**.** Then 
305b8 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
305b9 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c  ed is conceptual
305ba 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c  ly like the foll
305bb 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
305bc 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69    foreach row1 i
305bd 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20  n t1 do       \ 
305be 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65     Code generate
305bf 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72 65  d.**        fore
305c0 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64  ach row2 in t2 d
305c1 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71  o      |-- by sq
305c2 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
305c3 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f  ).**          fo
305c4 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20 74 33  reach row3 in t3
305c5 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20   do   /.**      
305c6 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20        ....**    
305c7 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20        end       
305c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 20                \ 
305c9 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65     Code generate
305ca 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e 64 20  d.**        end 
305cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
305cc 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71         |-- by sq
305cd 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a  lite3WhereEnd().
305ce 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20 20 20  **      end     
305cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
305d0 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65      /.**.** Note
305d1 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70 73 20   that the loops 
305d2 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e 65 73  might not be nes
305d3 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72  ted in the order
305d4 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 0a 2a   in which they.*
305d5 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  * appear in the 
305d6 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66 20 61  FROM clause if a
305d7 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72   different order
305d8 20 69 73 20 62 65 74 74 65 72 20 61 62 6c 65 20   is better able 
305d9 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f  to make.** use o
305da 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65  f indices.  Note
305db 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65 6e 20   also that when 
305dc 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  the IN operator 
305dd 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68  appears in.** th
305de 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20  e WHERE clause, 
305df 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20  it might result 
305e0 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65  in additional ne
305e1 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a  sted loops for.*
305e2 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75  * scanning throu
305e3 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e  gh all values on
305e4 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
305e5 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a  side of the IN..
305e6 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
305e7 42 74 72 65 65 20 63 75 72 73 6f 72 73 20 61 73  Btree cursors as
305e8 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61  sociated with ea
305e9 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73  ch table.  t1 us
305ea 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d  es cursor.** num
305eb 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  ber pTabList->a[
305ec 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20  0].iCursor.  t2 
305ed 75 73 65 73 20 74 68 65 20 63 75 72 73 6f 72 20  uses the cursor 
305ee 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69  pTabList->a[1].i
305ef 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73  Cursor..** And s
305f0 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72  o forth.  This r
305f1 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
305f2 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68   code to open th
305f3 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f 72 73  ose VDBE cursors
305f4 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 57  .** and sqlite3W
305f5 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65 72 61  hereEnd() genera
305f6 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 6f 20  tes the code to 
305f7 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a  close them..**.*
305f8 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61 74 20  * The code that 
305f9 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
305fa 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20 6c 65  n() generates le
305fb 61 76 65 73 20 74 68 65 20 63 75 72 73 6f 72 73  aves the cursors
305fc 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61   named.** in pTa
305fd 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 61  bList pointing a
305fe 74 20 74 68 65 69 72 20 61 70 70 72 6f 70 72 69  t their appropri
305ff 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20 54 68  ate entries.  Th
30600 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20  e [...] code.** 
30601 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d  can use OP_Colum
30602 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f  n and OP_Rowid o
30603 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73 65 20  pcodes on these 
30604 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74 72 61  cursors to extra
30605 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20  ct.** data from 
30606 74 68 65 20 76 61 72 69 6f 75 73 20 74 61 62 6c  the various tabl
30607 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a  es of the loop..
30608 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 48 45  **.** If the WHE
30609 52 45 20 63 6c 61 75 73 65 20 69 73 20 65 6d 70  RE clause is emp
3060a 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63 68 20  ty, the foreach 
3060b 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63 68 20  loops must each 
3060c 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e  scan their.** en
3060d 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20 54 68  tire tables.  Th
3060e 75 73 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a  us a three-way j
3060f 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29  oin is an O(N^3)
30610 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74   operation.  But
30611 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   if.** the table
30612 73 20 68 61 76 65 20 69 6e 64 69 63 65 73 20 61  s have indices a
30613 6e 64 20 74 68 65 72 65 20 61 72 65 20 74 65 72  nd there are ter
30614 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ms in the WHERE 
30615 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a 20 72  clause that.** r
30616 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20 69 6e  efer to those in
30617 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74  dices, a complet
30618 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63 61 6e  e table scan can
30619 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e 64 20   be avoided and 
3061a 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c  the.** code will
3061b 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74 65 72   run much faster
3061c 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 77  .  Most of the w
3061d 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f 75 74  ork of this rout
3061e 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e 67 0a  ine is checking.
3061f 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ** to see if the
30620 72 65 20 61 72 65 20 69 6e 64 69 63 65 73 20 74  re are indices t
30621 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  hat can be used 
30622 74 6f 20 73 70 65 65 64 20 75 70 20 74 68 65 20  to speed up the 
30623 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d  loop..**.** Term
30624 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
30625 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75  lause are also u
30626 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69  sed to limit whi
30627 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79  ch rows actually
30628 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74  .** make it to t
30629 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20  he "..." in the 
3062a 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f  middle of the lo
3062b 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63 68 20  op.  After each 
3062c 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65  "foreach",.** te
3062d 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  rms of the WHERE
3062e 20 63 6c 61 75 73 65 20 74 68 61 74 20 75 73 65   clause that use
3062f 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20 74   only terms in t
30630 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74  hat loop and out
30631 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20  er.** loops are 
30632 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 66  evaluated and if
30633 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20 69 73   false a jump is
30634 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c   made around all
30635 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69   subsequent.** i
30636 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61  nner loops (or a
30637 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20  round the "..." 
30638 69 66 20 74 68 65 20 74 65 73 74 20 6f 63 63 75  if the test occu
30639 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 69 6e  rs within the in
3063a 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f  ner-.** most loo
3063b 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a  p).**.** OUTER J
3063c 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75  OINS.**.** An ou
3063d 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c  ter join of tabl
3063e 65 73 20 74 31 20 61 6e 64 20 74 32 20 69 73 20  es t1 and t2 is 
3063f 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64 65  conceptally code
30640 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  d as follows:.**
30641 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68 20 72  .**    foreach r
30642 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20  ow1 in t1 do.** 
30643 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a       flag = 0.**
30644 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f        foreach ro
30645 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20  w2 in t2 do.**  
30646 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20        start:.** 
30647 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20           ....** 
30648 20 20 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20           flag = 
30649 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a  1.**      end.**
3064a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d 3d 30        if flag==0
3064b 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
3064c 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20 63 75  move the row2 cu
3064d 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72  rsor to a null r
3064e 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f 74  ow.**        got
3064f 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 20 20  o start.**      
30650 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a  fi.**    end.**.
30651 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c 41 55  ** ORDER BY CLAU
30652 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a  SE PROCESSING.**
30653 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69  .** *ppOrderBy i
30654 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
30655 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
30656 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  se of a SELECT s
30657 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20  tatement,.** if 
30658 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 49  there is one.  I
30659 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52  f there is no OR
3065a 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f 72  DER BY clause or
3065b 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
3065c 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 66 72  .** is called fr
3065d 6f 6d 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20  om an UPDATE or 
3065e 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
3065f 2c 20 74 68 65 6e 20 70 70 4f 72 64 65 72 42 79  , then ppOrderBy
30660 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
30661 49 66 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20  If an index can 
30662 62 65 20 75 73 65 64 20 73 6f 20 74 68 61 74 20  be used so that 
30663 74 68 65 20 6e 61 74 75 72 61 6c 20 6f 75 74 70  the natural outp
30664 75 74 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20  ut order of the 
30665 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73  table.** scan is
30666 20 63 6f 72 72 65 63 74 20 66 6f 72 20 74 68 65   correct for the
30667 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
30668 2c 20 74 68 65 6e 20 74 68 61 74 20 69 6e 64 65  , then that inde
30669 78 20 69 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a  x is used and.**
3066a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 73   *ppOrderBy is s
3066b 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69  et to NULL.  Thi
3066c 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  s is an optimiza
3066d 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76 65 6e  tion that preven
3066e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73  ts an.** unneces
3066f 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65  sary sort of the
30670 20 72 65 73 75 6c 74 20 73 65 74 20 69 66 20 61   result set if a
30671 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70 72 69  n index appropri
30672 61 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f  ate for the.** O
30673 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
30674 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a  lready exists..*
30675 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 68 65 72  *.** If the wher
30676 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63  e clause loops c
30677 61 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e 67 65  annot be arrange
30678 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  d to provide the
30679 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70   correct.** outp
3067a 75 74 20 6f 72 64 65 72 2c 20 74 68 65 6e 20 74  ut order, then t
3067b 68 65 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73  he *ppOrderBy is
3067c 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53   unchanged..*/.S
3067d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 68  QLITE_PRIVATE Wh
3067e 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33  ereInfo *sqlite3
3067f 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20 50 61  WhereBegin(.  Pa
30680 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
30681 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
30682 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
30683 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
30684 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f  ,    /* A list o
30685 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20  f all tables to 
30686 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20  be scanned */.  
30687 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20  Expr *pWhere,   
30688 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
30689 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  RE clause */.  E
3068a 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65  xprList **ppOrde
3068b 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52  rBy, /* An ORDER
3068c 20 42 59 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e   BY clause, or N
3068d 55 4c 4c 20 2a 2f 0a 20 20 75 31 36 20 77 63 74  ULL */.  u16 wct
3068e 72 6c 46 6c 61 67 73 20 20 20 20 20 20 20 20 2f  rlFlags        /
3068f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45  * One of the WHE
30690 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e  RE_* flags defin
30691 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ed in sqliteInt.
30692 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  h */.){.  int i;
30693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30694 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
30695 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  nter */.  int nB
30696 79 74 65 57 49 6e 66 6f 3b 20 20 20 20 20 20 20  yteWInfo;       
30697 20 20 20 20 20 2f 2a 20 4e 75 6d 2e 20 62 79 74       /* Num. byt
30698 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
30699 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63   WhereInfo struc
3069a 74 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  t */.  WhereInfo
3069b 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20   *pWInfo;       
3069c 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65    /* Will become
3069d 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3069e 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
3069f 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20  on */.  Vdbe *v 
306a0 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
306a1 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
306a2 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
306a3 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  e */.  Bitmask n
306a4 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20 20 20  otReady;        
306a5 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61    /* Cursors tha
306a6 74 20 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f  t are not yet po
306a7 73 69 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68  sitioned */.  Wh
306a8 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
306a9 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  kSet;    /* The 
306aa 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20  expression mask 
306ab 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  set */.  WhereCl
306ac 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
306ad 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f           /* Deco
306ae 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65  mposition of the
306af 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
306b0 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
306b1 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d  t_item *pTabItem
306b2 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 65  ;  /* A single e
306b3 6e 74 72 79 20 66 72 6f 6d 20 70 54 61 62 4c 69  ntry from pTabLi
306b4 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  st */.  WhereLev
306b5 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20 20 20  el *pLevel;     
306b6 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
306b7 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74 68 65  gle level in the
306b8 20 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a 2f 0a   pWInfo list */.
306b9 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
306ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306bb 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65    /* First unuse
306bc 64 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6c  d FROM clause el
306bd 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 61  ement */.  int a
306be 6e 64 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  ndFlags;        
306bf 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65 64 20        /* AND-ed 
306c0 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61  combination of a
306c1 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74 46 6c  ll pWC->a[].wtFl
306c2 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ags */.  sqlite3
306c3 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
306c4 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
306c5 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
306c6 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
306c7 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
306c8 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c  FROM clause is l
306c9 69 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e 75  imited by the nu
306ca 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74  mber of.  ** bit
306cb 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a  s in a Bitmask .
306cc 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 4c    */.  if( pTabL
306cd 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b  ist->nSrc>BMS ){
306ce 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
306cf 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 74  rMsg(pParse, "at
306d0 20 6d 6f 73 74 20 25 64 20 74 61 62 6c 65 73 20   most %d tables 
306d1 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29  in a join", BMS)
306d2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
306d3 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61    }..  /* Alloca
306d4 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  te and initializ
306d5 65 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20  e the WhereInfo 
306d6 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77  structure that w
306d7 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a 20  ill become the. 
306d8 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65   ** return value
306d9 2e 20 41 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63  . A single alloc
306da 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ation is used to
306db 20 73 74 6f 72 65 20 74 68 65 20 57 68 65 72 65   store the Where
306dc 49 6e 66 6f 0a 20 20 2a 2a 20 73 74 72 75 63 74  Info.  ** struct
306dd 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  , the contents o
306de 66 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b 5d 2c  f WhereInfo.a[],
306df 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
306e0 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20   structure.  ** 
306e1 61 6e 64 20 74 68 65 20 57 68 65 72 65 4d 61 73  and the WhereMas
306e2 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 2e 20  kSet structure. 
306e3 53 69 6e 63 65 20 57 68 65 72 65 43 6c 61 75 73  Since WhereClaus
306e4 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 38 2d  e contains an 8-
306e5 62 79 74 65 0a 20 20 2a 2a 20 66 69 65 6c 64 20  byte.  ** field 
306e6 28 74 79 70 65 20 42 69 74 6d 61 73 6b 29 20 69  (type Bitmask) i
306e7 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65  t must be aligne
306e8 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 62  d on an 8-byte b
306e9 6f 75 6e 64 61 72 79 20 6f 6e 0a 20 20 2a 2a 20  oundary on.  ** 
306ea 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72  some architectur
306eb 65 73 2e 20 48 65 6e 63 65 20 74 68 65 20 52 4f  es. Hence the RO
306ec 55 4e 44 38 28 29 20 62 65 6c 6f 77 2e 0a 20 20  UND8() below..  
306ed 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  */.  db = pParse
306ee 2d 3e 64 62 3b 0a 20 20 6e 42 79 74 65 57 49 6e  ->db;.  nByteWIn
306ef 66 6f 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65  fo = ROUND8(size
306f0 6f 66 28 57 68 65 72 65 49 6e 66 6f 29 2b 28 70  of(WhereInfo)+(p
306f1 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 29  TabList->nSrc-1)
306f2 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76  *sizeof(WhereLev
306f3 65 6c 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d  el));.  pWInfo =
306f4 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
306f5 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20  Zero(db, .      
306f6 6e 42 79 74 65 57 49 6e 66 6f 20 2b 20 0a 20 20  nByteWInfo + .  
306f7 20 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65      sizeof(Where
306f8 43 6c 61 75 73 65 29 20 2b 0a 20 20 20 20 20 20  Clause) +.      
306f9 73 69 7a 65 6f 66 28 57 68 65 72 65 4d 61 73 6b  sizeof(WhereMask
306fa 53 65 74 29 0a 20 20 29 3b 0a 20 20 69 66 28 20  Set).  );.  if( 
306fb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
306fc 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65   ){.    goto whe
306fd 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20  reBeginError;.  
306fe 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76  }.  pWInfo->nLev
306ff 65 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e  el = pTabList->n
30700 53 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70  Src;.  pWInfo->p
30701 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
30702 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69    pWInfo->pTabLi
30703 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20  st = pTabList;. 
30704 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20   pWInfo->iBreak 
30705 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
30706 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 70 57 49  eLabel(v);.  pWI
30707 6e 66 6f 2d 3e 70 57 43 20 3d 20 70 57 43 20 3d  nfo->pWC = pWC =
30708 20 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 29   (WhereClause *)
30709 26 28 28 75 38 20 2a 29 70 57 49 6e 66 6f 29 5b  &((u8 *)pWInfo)[
3070a 6e 42 79 74 65 57 49 6e 66 6f 5d 3b 0a 20 20 70  nByteWInfo];.  p
3070b 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67  WInfo->wctrlFlag
3070c 73 20 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b 0a  s = wctrlFlags;.
3070d 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 28 57 68    pMaskSet = (Wh
3070e 65 72 65 4d 61 73 6b 53 65 74 2a 29 26 70 57 43  ereMaskSet*)&pWC
3070f 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69 74  [1];..  /* Split
30710 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
30711 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65 20  e into separate 
30712 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77  subexpressions w
30713 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20 73  here each.  ** s
30714 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  ubexpression is 
30715 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e 20  separated by an 
30716 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20  AND operator..  
30717 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65 74  */.  initMaskSet
30718 28 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77 68  (pMaskSet);.  wh
30719 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 57  ereClauseInit(pW
3071a 43 2c 20 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C, pParse, pMask
3071b 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  Set);.  sqlite3E
3071c 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
3071d 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 29  (pParse, pWhere)
3071e 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 70  ;.  whereSplit(p
3071f 57 43 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f 41  WC, pWhere, TK_A
30720 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 53  ND);.    .  /* S
30721 70 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57  pecial case: a W
30722 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
30723 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45   is constant.  E
30724 76 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a  valuate the.  **
30725 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
30726 65 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72  either jump over
30727 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65   all of the code
30728 20 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20   or fall thru.. 
30729 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65   */.  if( pWhere
3072a 20 26 26 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e   && (pTabList->n
3072b 53 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  Src==0 || sqlite
3072c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
3072d 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29 20  otJoin(pWhere)) 
3072e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
3072f 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
30730 2c 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66 6f  , pWhere, pWInfo
30731 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49 54 45  ->iBreak, SQLITE
30732 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
30733 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20    pWhere = 0;.  
30734 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 61  }..  /* Assign a
30735 20 62 69 74 20 66 72 6f 6d 20 74 68 65 20 62 69   bit from the bi
30736 74 6d 61 73 6b 20 74 6f 20 65 76 65 72 79 20 74  tmask to every t
30737 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  erm in the FROM 
30738 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  clause..  **.  *
30739 2a 20 57 68 65 6e 20 61 73 73 69 67 6e 69 6e 67  * When assigning
3073a 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73 20   bitmask values 
3073b 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63  to FROM clause c
3073c 75 72 73 6f 72 73 2c 20 69 74 20 6d 75 73 74 20  ursors, it must 
3073d 62 65 0a 20 20 2a 2a 20 74 68 65 20 63 61 73 65  be.  ** the case
3073e 20 74 68 61 74 20 69 66 20 58 20 69 73 20 74 68   that if X is th
3073f 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68  e bitmask for th
30740 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75  e N-th FROM clau
30741 73 65 20 74 65 72 6d 20 74 68 65 6e 0a 20 20 2a  se term then.  *
30742 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f  * the bitmask fo
30743 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73  r all FROM claus
30744 65 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20 6c  e terms to the l
30745 65 66 74 20 6f 66 20 74 68 65 20 4e 2d 74 68 20  eft of the N-th 
30746 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58 2d  term.  ** is (X-
30747 31 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73 73  1).   An express
30748 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20  ion from the ON 
30749 63 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54  clause of a LEFT
3074a 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20 20   JOIN can use.  
3074b 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69 67  ** its Expr.iRig
3074c 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75  htJoinTable valu
3074d 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62 69  e to find the bi
3074e 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69 67  tmask of the rig
3074f 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 66  ht table.  ** of
30750 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62 74   the join.  Subt
30751 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f 6d  racting one from
30752 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
30753 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20 61   bitmask gives a
30754 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66 6f  .  ** bitmask fo
30755 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20  r all tables to 
30756 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20  the left of the 
30757 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20 74  join.  Knowing t
30758 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20  he bitmask.  ** 
30759 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74  for all tables t
3075a 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 61 20  o the left of a 
3075b 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d 70  left join is imp
3075c 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b 65 74 20  ortant.  Ticket 
3075d 23 33 30 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  #3015..  **.  **
3075e 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 57   Configure the W
3075f 68 65 72 65 43 6c 61 75 73 65 2e 76 6d 61 73 6b  hereClause.vmask
30760 20 76 61 72 69 61 62 6c 65 20 73 6f 20 74 68 61   variable so tha
30761 74 20 62 69 74 73 20 74 68 61 74 20 63 6f 72 72  t bits that corr
30762 65 73 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20 76  espond.  ** to v
30763 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72  irtual table cur
30764 73 6f 72 73 20 61 72 65 20 73 65 74 2e 20 54 68  sors are set. Th
30765 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65  is is used to se
30766 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61 62 6c  lectively disabl
30767 65 20 0a 20 20 2a 2a 20 74 68 65 20 4f 52 2d 74  e .  ** the OR-t
30768 6f 2d 49 4e 20 74 72 61 6e 73 66 6f 72 6d 61 74  o-IN transformat
30769 69 6f 6e 20 69 6e 20 65 78 70 72 41 6e 61 6c 79  ion in exprAnaly
3076a 7a 65 4f 72 54 65 72 6d 28 29 2e 20 49 74 20 69  zeOrTerm(). It i
3076b 73 20 6e 6f 74 20 68 65 6c 70 66 75 6c 20 0a 20  s not helpful . 
3076c 20 2a 2a 20 77 69 74 68 20 76 69 72 74 75 61 6c   ** with virtual
3076d 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
3076e 61 73 73 65 72 74 28 20 70 57 43 2d 3e 76 6d 61  assert( pWC->vma
3076f 73 6b 3d 3d 30 20 26 26 20 70 4d 61 73 6b 53 65  sk==0 && pMaskSe
30770 74 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72  t->n==0 );.  for
30771 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74  (i=0; i<pTabList
30772 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
30773 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70 4d 61    createMask(pMa
30774 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
30775 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
30776 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
30777 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
30778 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28  .    if( ALWAYS(
30779 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  pTabList->a[i].p
3077a 54 61 62 29 20 26 26 20 49 73 56 69 72 74 75 61  Tab) && IsVirtua
3077b 6c 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d  l(pTabList->a[i]
3077c 2e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  .pTab) ){.      
3077d 70 57 43 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28 28  pWC->vmask |= ((
3077e 42 69 74 6d 61 73 6b 29 31 20 3c 3c 20 69 29 3b  Bitmask)1 << i);
3077f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
30780 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  }.#ifndef NDEBUG
30781 0a 20 20 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b  .  {.    Bitmask
30782 20 74 6f 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a   toTheLeft = 0;.
30783 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
30784 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
30785 2b 2b 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61  ++){.      Bitma
30786 73 6b 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70  sk m = getMask(p
30787 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73  MaskSet, pTabLis
30788 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29  t->a[i].iCursor)
30789 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
3078a 28 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74  (m-1)==toTheLeft
3078b 20 29 3b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c   );.      toTheL
3078c 65 66 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a  eft |= m;.    }.
3078d 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
3078e 20 41 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20   Analyze all of 
3078f 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  the subexpressio
30790 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65  ns.  Note that e
30791 78 70 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67  xprAnalyze() mig
30792 68 74 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77 20  ht.  ** add new 
30793 76 69 72 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e  virtual terms on
30794 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
30795 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
30796 20 57 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20   We do not.  ** 
30797 77 61 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20  want to analyze 
30798 74 68 65 73 65 20 76 69 72 74 75 61 6c 20 74 65  these virtual te
30799 72 6d 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e  rms, so start an
3079a 61 6c 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65  alyzing at the e
3079b 6e 64 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b  nd.  ** and work
3079c 20 66 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74   forward so that
3079d 20 74 68 65 20 61 64 64 65 64 20 76 69 72 74 75   the added virtu
3079e 61 6c 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76  al terms are nev
3079f 65 72 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20  er processed..  
307a0 2a 2f 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65  */.  exprAnalyze
307a1 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 70 57  All(pTabList, pW
307a2 43 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  C);.  if( db->ma
307a3 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
307a4 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69    goto whereBegi
307a5 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f  nError;.  }..  /
307a6 2a 20 43 68 6f 73 65 20 74 68 65 20 62 65 73 74  * Chose the best
307a7 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f   index to use fo
307a8 72 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20  r each table in 
307a9 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
307aa 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
307ab 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74 68  loop fills in th
307ac 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c  e following fiel
307ad 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ds:.  **.  **   
307ae 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78  pWInfo->a[].pIdx
307af 20 20 20 20 20 20 54 68 65 20 69 6e 64 65 78 20        The index 
307b0 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20  to use for this 
307b1 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f  level of the loo
307b2 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  p..  **   pWInfo
307b3 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73 20 20 20  ->a[].wsFlags   
307b4 57 48 45 52 45 5f 78 78 78 20 66 6c 61 67 73 20  WHERE_xxx flags 
307b5 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
307b6 70 49 64 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e  pIdx.  **   pWIn
307b7 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20  fo->a[].nEq     
307b8 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    The number of 
307b9 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72  == and IN constr
307ba 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57 49  aints.  **   pWI
307bb 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20  nfo->a[].iFrom  
307bc 20 20 20 57 68 69 63 68 20 74 65 72 6d 20 6f 66     Which term of
307bd 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
307be 20 69 73 20 62 65 69 6e 67 20 63 6f 64 65 64 0a   is being coded.
307bf 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
307c0 5b 5d 2e 69 54 61 62 43 75 72 20 20 20 54 68 65  [].iTabCur   The
307c1 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
307c2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
307c3 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  ble.  **   pWInf
307c4 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20 20  o->a[].iIdxCur  
307c5 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
307c6 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20   for the index. 
307c7 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
307c8 5d 2e 70 54 65 72 6d 20 20 20 20 20 57 68 65 6e  ].pTerm     When
307c9 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f 52 2c   wsFlags==WO_OR,
307ca 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 20 74   the OR-clause t
307cb 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  erm.  **.  ** Th
307cc 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 66 69 67  is loop also fig
307cd 75 72 65 73 20 6f 75 74 20 74 68 65 20 6e 65 73  ures out the nes
307ce 74 69 6e 67 20 6f 72 64 65 72 20 6f 66 20 74 61  ting order of ta
307cf 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  bles in the FROM
307d0 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20  .  ** clause..  
307d1 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d 20  */.  notReady = 
307d2 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 70  ~(Bitmask)0;.  p
307d3 54 61 62 49 74 65 6d 20 3d 20 70 54 61 62 4c 69  TabItem = pTabLi
307d4 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76 65 6c 20  st->a;.  pLevel 
307d5 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a 20 20 61  = pWInfo->a;.  a
307d6 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b 0a 20 20  ndFlags = ~0;.  
307d7 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a  WHERETRACE(("***
307d8 20 4f 70 74 69 6d 69 7a 65 72 20 53 74 61 72 74   Optimizer Start
307d9 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 66 6f 72   ***\n"));.  for
307da 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70 4c 65 76  (i=iFrom=0, pLev
307db 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c  el=pWInfo->a; i<
307dc 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
307dd 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a  i++, pLevel++){.
307de 20 20 20 20 57 68 65 72 65 43 6f 73 74 20 62 65      WhereCost be
307df 73 74 50 6c 61 6e 3b 20 20 20 20 20 20 20 20 20  stPlan;         
307e0 2f 2a 20 4d 6f 73 74 20 65 66 66 69 63 69 65 6e  /* Most efficien
307e1 74 20 70 6c 61 6e 20 73 65 65 6e 20 73 6f 20 66  t plan seen so f
307e2 61 72 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20  ar */.    Index 
307e3 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *pIdx;          
307e4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 66        /* Index f
307e5 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65 20 61 74  or FROM table at
307e6 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20   pTabItem */.   
307e7 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20   int j;         
307e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
307e9 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
307ea 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a   FROM tables */.
307eb 20 20 20 20 69 6e 74 20 62 65 73 74 4a 20 3d 20      int bestJ = 
307ec 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  -1;             
307ed 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  /* The value of 
307ee 6a 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b  j */.    Bitmask
307ef 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   m;             
307f0 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20       /* Bitmask 
307f1 76 61 6c 75 65 20 66 6f 72 20 6a 20 6f 72 20 62  value for j or b
307f2 65 73 74 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20  estJ */.    int 
307f3 69 73 4f 70 74 69 6d 61 6c 3b 20 20 20 20 20 20  isOptimal;      
307f4 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
307f5 74 6f 72 20 66 6f 72 20 6f 70 74 69 6d 61 6c 2f  tor for optimal/
307f6 6e 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73 65 61 72  non-optimal sear
307f7 63 68 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65  ch */..    memse
307f8 74 28 26 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20  t(&bestPlan, 0, 
307f9 73 69 7a 65 6f 66 28 62 65 73 74 50 6c 61 6e 29  sizeof(bestPlan)
307fa 29 3b 0a 20 20 20 20 62 65 73 74 50 6c 61 6e 2e  );.    bestPlan.
307fb 72 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42  rCost = SQLITE_B
307fc 49 47 5f 44 42 4c 3b 0a 0a 20 20 20 20 2f 2a 20  IG_DBL;..    /* 
307fd 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
307fe 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69   remaining entri
307ff 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
30800 6c 61 75 73 65 20 74 6f 20 66 69 6e 64 20 74 68  lause to find th
30801 65 0a 20 20 20 20 2a 2a 20 6e 65 78 74 20 6e 65  e.    ** next ne
30802 73 74 65 64 20 6c 6f 6f 70 2e 20 54 68 65 20 46  sted loop. The F
30803 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 69  ROM clause entri
30804 65 73 20 6d 61 79 20 62 65 20 69 74 65 72 61 74  es may be iterat
30805 65 64 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a  ed through.    *
30806 2a 20 65 69 74 68 65 72 20 6f 6e 63 65 20 6f 72  * either once or
30807 20 74 77 69 63 65 2e 20 0a 20 20 20 20 2a 2a 0a   twice. .    **.
30808 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74      ** The first
30809 20 69 74 65 72 61 74 69 6f 6e 2c 20 77 68 69 63   iteration, whic
3080a 68 20 69 73 20 61 6c 77 61 79 73 20 70 65 72 66  h is always perf
3080b 6f 72 6d 65 64 2c 20 73 65 61 72 63 68 65 73 20  ormed, searches 
3080c 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 46  for the.    ** F
3080d 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79  ROM clause entry
3080e 20 74 68 61 74 20 70 65 72 6d 69 74 73 20 74 68   that permits th
3080f 65 20 6c 6f 77 65 73 74 2d 63 6f 73 74 2c 20 22  e lowest-cost, "
30810 6f 70 74 69 6d 61 6c 22 20 73 63 61 6e 2e 20 49  optimal" scan. I
30811 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f  n.    ** this co
30812 6e 74 65 78 74 20 61 6e 20 6f 70 74 69 6d 61 6c  ntext an optimal
30813 20 73 63 61 6e 20 69 73 20 6f 6e 65 20 74 68 61   scan is one tha
30814 74 20 75 73 65 73 20 74 68 65 20 73 61 6d 65 20  t uses the same 
30815 73 74 72 61 74 65 67 79 0a 20 20 20 20 2a 2a 20  strategy.    ** 
30816 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 46 52  for the given FR
30817 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20  OM clause entry 
30818 61 73 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65  as would be sele
30819 63 74 65 64 20 69 66 20 74 68 65 20 65 6e 74 72  cted if the entr
3081a 79 0a 20 20 20 20 2a 2a 20 77 65 72 65 20 75 73  y.    ** were us
3081b 65 64 20 61 73 20 74 68 65 20 69 6e 6e 65 72 6d  ed as the innerm
3081c 6f 73 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e  ost nested loop.
3081d 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
3081e 2c 20 61 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  , a table.    **
3081f 20 69 73 20 63 68 6f 73 65 6e 20 73 75 63 68 20   is chosen such 
30820 74 68 61 74 20 74 68 65 20 63 6f 73 74 20 6f 66  that the cost of
30821 20 72 75 6e 6e 69 6e 67 20 74 68 61 74 20 74 61   running that ta
30822 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ble cannot be re
30823 64 75 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20  duced.    ** by 
30824 77 61 69 74 69 6e 67 20 66 6f 72 20 6f 74 68 65  waiting for othe
30825 72 20 74 61 62 6c 65 73 20 74 6f 20 72 75 6e 20  r tables to run 
30826 66 69 72 73 74 2e 0a 20 20 20 20 2a 2a 0a 20 20  first..    **.  
30827 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
30828 69 74 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c  iteration is onl
30829 79 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20 6e  y performed if n
3082a 6f 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20 73  o optimal scan s
3082b 74 72 61 74 65 67 69 65 73 0a 20 20 20 20 2a 2a  trategies.    **
3082c 20 77 65 72 65 20 66 6f 75 6e 64 20 62 79 20 74   were found by t
3082d 68 65 20 66 69 72 73 74 2e 20 54 68 69 73 20 69  he first. This i
3082e 74 65 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64  teration is used
3082f 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74   to search for t
30830 68 65 0a 20 20 20 20 2a 2a 20 6c 6f 77 65 73 74  he.    ** lowest
30831 20 63 6f 73 74 20 73 63 61 6e 20 6f 76 65 72 61   cost scan overa
30832 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ll..    **.    *
30833 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 69  * Previous versi
30834 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 65  ons of SQLite pe
30835 72 66 6f 72 6d 65 64 20 6f 6e 6c 79 20 74 68 65  rformed only the
30836 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f   second iteratio
30837 6e 20 2d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e  n -.    ** the n
30838 65 78 74 20 6f 75 74 65 72 6d 6f 73 74 20 6c 6f  ext outermost lo
30839 6f 70 20 77 61 73 20 61 6c 77 61 79 73 20 74 68  op was always th
3083a 61 74 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65  at with the lowe
3083b 73 74 20 6f 76 65 72 61 6c 6c 0a 20 20 20 20 2a  st overall.    *
3083c 2a 20 63 6f 73 74 2e 20 48 6f 77 65 76 65 72 2c  * cost. However,
3083d 20 74 68 69 73 20 6d 65 61 6e 74 20 74 68 61 74   this meant that
3083e 20 53 51 4c 69 74 65 20 63 6f 75 6c 64 20 73 65   SQLite could se
3083f 6c 65 63 74 20 74 68 65 20 77 72 6f 6e 67 20 70  lect the wrong p
30840 6c 61 6e 0a 20 20 20 20 2a 2a 20 66 6f 72 20 73  lan.    ** for s
30841 63 72 69 70 74 73 20 73 75 63 68 20 61 73 20 74  cripts such as t
30842 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20  he following:.  
30843 20 20 2a 2a 20 20 20 0a 20 20 20 20 2a 2a 20 20    **   .    **  
30844 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
30845 28 61 2c 20 62 29 3b 20 0a 20 20 20 20 2a 2a 20  (a, b); .    ** 
30846 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
30847 32 28 63 2c 20 64 29 3b 0a 20 20 20 20 2a 2a 20  2(c, d);.    ** 
30848 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
30849 74 32 2c 20 74 31 20 57 48 45 52 45 20 74 32 2e  t2, t1 WHERE t2.
3084a 72 6f 77 69 64 20 3d 20 74 31 2e 61 3b 0a 20 20  rowid = t1.a;.  
3084b 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
3084c 62 65 73 74 20 73 74 72 61 74 65 67 79 20 69 73  best strategy is
3084d 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
3084e 75 67 68 20 74 61 62 6c 65 20 74 31 20 66 69 72  ugh table t1 fir
3084f 73 74 2e 20 48 6f 77 65 76 65 72 20 69 74 0a 20  st. However it. 
30850 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73     ** is not pos
30851 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69  sible to determi
30852 6e 65 20 74 68 69 73 20 77 69 74 68 20 61 20 73  ne this with a s
30853 69 6d 70 6c 65 20 67 72 65 65 64 79 20 61 6c 67  imple greedy alg
30854 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 20 48  orithm..    ** H
30855 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68  owever, since th
30856 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65  e cost of a line
30857 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20  ar scan through 
30858 74 61 62 6c 65 20 74 32 20 69 73 20 74 68 65 20  table t2 is the 
30859 73 61 6d 65 20 0a 20 20 20 20 2a 2a 20 61 73 20  same .    ** as 
3085a 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69  the cost of a li
3085b 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67  near scan throug
3085c 68 20 74 61 62 6c 65 20 74 31 2c 20 61 20 73 69  h table t1, a si
3085d 6d 70 6c 65 20 67 72 65 65 64 79 20 0a 20 20 20  mple greedy .   
3085e 20 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 6d 61   ** algorithm ma
3085f 79 20 63 68 6f 6f 73 65 20 74 6f 20 75 73 65 20  y choose to use 
30860 74 32 20 66 6f 72 20 74 68 65 20 6f 75 74 65 72  t2 for the outer
30861 20 6c 6f 6f 70 2c 20 77 68 69 63 68 20 69 73 20   loop, which is 
30862 61 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20 63 6f  a much.    ** co
30863 73 74 6c 69 65 72 20 61 70 70 72 6f 61 63 68 2e  stlier approach.
30864 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
30865 69 73 4f 70 74 69 6d 61 6c 3d 31 3b 20 69 73 4f  isOptimal=1; isO
30866 70 74 69 6d 61 6c 3e 3d 30 20 26 26 20 62 65 73  ptimal>=0 && bes
30867 74 4a 3c 30 3b 20 69 73 4f 70 74 69 6d 61 6c 2d  tJ<0; isOptimal-
30868 2d 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73  -){.      Bitmas
30869 6b 20 6d 61 73 6b 20 3d 20 28 69 73 4f 70 74 69  k mask = (isOpti
3086a 6d 61 6c 20 3f 20 30 20 3a 20 6e 6f 74 52 65 61  mal ? 0 : notRea
3086b 64 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  dy);.      asser
3086c 74 28 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53  t( (pTabList->nS
3086d 72 63 2d 69 46 72 6f 6d 29 3e 31 20 7c 7c 20 69  rc-iFrom)>1 || i
3086e 73 4f 70 74 69 6d 61 6c 20 29 3b 0a 20 20 20 20  sOptimal );.    
3086f 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d 2c 20 70    for(j=iFrom, p
30870 54 61 62 49 74 65 6d 3d 26 70 54 61 62 4c 69 73  TabItem=&pTabLis
30871 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54 61 62 4c  t->a[j]; j<pTabL
30872 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b 2b 2c 20  ist->nSrc; j++, 
30873 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  pTabItem++){.   
30874 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52 65       int doNotRe
30875 6f 72 64 65 72 3b 20 20 20 20 2f 2a 20 54 72 75  order;    /* Tru
30876 65 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20  e if this table 
30877 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65  should not be re
30878 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20  ordered */.     
30879 20 20 20 57 68 65 72 65 43 6f 73 74 20 73 43 6f     WhereCost sCo
3087a 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f 73 74 20  st;     /* Cost 
3087b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
3087c 20 62 65 73 74 5b 56 69 72 74 75 61 6c 5d 49 6e   best[Virtual]In
3087d 64 65 78 28 29 20 2a 2f 0a 20 20 20 20 20 20 20  dex() */.       
3087e 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
3087f 72 42 79 3b 20 20 2f 2a 20 4f 52 44 45 52 20 42  rBy;  /* ORDER B
30880 59 20 63 6c 61 75 73 65 20 66 6f 72 20 69 6e 64  Y clause for ind
30881 65 78 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 2a  ex to optimize *
30882 2f 0a 20 20 0a 20 20 20 20 20 20 20 20 64 6f 4e  /.  .        doN
30883 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28 70 54  otReorder =  (pT
30884 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65  abItem->jointype
30885 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43   & (JT_LEFT|JT_C
30886 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20 20 20  ROSS))!=0;.     
30887 20 20 20 69 66 28 20 6a 21 3d 69 46 72 6f 6d 20     if( j!=iFrom 
30888 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20  && doNotReorder 
30889 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
3088a 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61   m = getMask(pMa
3088b 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65 6d 2d  skSet, pTabItem-
3088c 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >iCursor);.     
3088d 20 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74 52     if( (m & notR
3088e 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20  eady)==0 ){.    
3088f 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 69 46 72        if( j==iFr
30890 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20  om ) iFrom++;.  
30891 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
30892 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
30893 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 28      pOrderBy = (
30894 28 69 3d 3d 30 20 26 26 20 70 70 4f 72 64 65 72  (i==0 && ppOrder
30895 42 79 20 29 3f 2a 70 70 4f 72 64 65 72 42 79 3a  By )?*ppOrderBy:
30896 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 61  0);.  .        a
30897 73 73 65 72 74 28 20 70 54 61 62 49 74 65 6d 2d  ssert( pTabItem-
30898 3e 70 54 61 62 20 29 3b 0a 23 69 66 6e 64 65 66  >pTab );.#ifndef
30899 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
3089a 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
3089b 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
3089c 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 29 20  pTabItem->pTab) 
3089d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
3089e 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
3089f 2a 2a 70 70 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  **pp = &pWInfo->
308a0 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20  a[j].pIdxInfo;. 
308a1 20 20 20 20 20 20 20 20 20 62 65 73 74 56 69 72           bestVir
308a2 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65  tualIndex(pParse
308a3 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c  , pWC, pTabItem,
308a4 20 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c   mask, pOrderBy,
308a5 20 26 73 43 6f 73 74 2c 20 70 70 29 3b 0a 20 20   &sCost, pp);.  
308a6 20 20 20 20 20 20 7d 65 6c 73 65 20 0a 23 65 6e        }else .#en
308a7 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20  dif.        {.  
308a8 20 20 20 20 20 20 20 20 62 65 73 74 42 74 72 65          bestBtre
308a9 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  eIndex(pParse, p
308aa 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d 61  WC, pTabItem, ma
308ab 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 26 73  sk, pOrderBy, &s
308ac 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  Cost);.        }
308ad 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
308ae 20 69 73 4f 70 74 69 6d 61 6c 20 7c 7c 20 28 73   isOptimal || (s
308af 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74 52 65 61  Cost.used&notRea
308b0 64 79 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20  dy)==0 );..     
308b1 20 20 20 69 66 28 20 28 73 43 6f 73 74 2e 75 73     if( (sCost.us
308b2 65 64 26 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a  ed&notReady)==0.
308b3 20 20 20 20 20 20 20 20 20 26 26 20 28 6a 3d 3d           && (j==
308b4 69 46 72 6f 6d 20 7c 7c 20 73 43 6f 73 74 2e 72  iFrom || sCost.r
308b5 43 6f 73 74 3c 62 65 73 74 50 6c 61 6e 2e 72 43  Cost<bestPlan.rC
308b6 6f 73 74 29 20 0a 20 20 20 20 20 20 20 20 29 7b  ost) .        ){
308b7 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74 50  .          bestP
308b8 6c 61 6e 20 3d 20 73 43 6f 73 74 3b 0a 20 20 20  lan = sCost;.   
308b9 20 20 20 20 20 20 20 62 65 73 74 4a 20 3d 20 6a         bestJ = j
308ba 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
308bb 20 20 20 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f      if( doNotReo
308bc 72 64 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  rder ) break;.  
308bd 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
308be 61 73 73 65 72 74 28 20 62 65 73 74 4a 3e 3d 30  assert( bestJ>=0
308bf 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
308c0 6e 6f 74 52 65 61 64 79 20 26 20 67 65 74 4d 61  notReady & getMa
308c1 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61  sk(pMaskSet, pTa
308c2 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e  bList->a[bestJ].
308c3 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20  iCursor) );.    
308c4 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a  WHERETRACE(("***
308c5 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c 65 63   Optimizer selec
308c6 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f 72 20  ts table %d for 
308c7 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73 74  loop %d\n", best
308c8 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 4c  J,.           pL
308c9 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29 29  evel-pWInfo->a))
308ca 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73 74 50  ;.    if( (bestP
308cb 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  lan.plan.wsFlags
308cc 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59   & WHERE_ORDERBY
308cd 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  )!=0 ){.      *p
308ce 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
308cf 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67 73    }.    andFlags
308d0 20 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61   &= bestPlan.pla
308d1 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20 20 70  n.wsFlags;.    p
308d2 4c 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65  Level->plan = be
308d3 73 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20  stPlan.plan;.   
308d4 20 69 66 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c   if( bestPlan.pl
308d5 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
308d6 52 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20 20  RE_INDEXED ){.  
308d7 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78      pLevel->iIdx
308d8 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
308d9 61 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ab++;.    }else{
308da 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69  .      pLevel->i
308db 49 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20 20 20  IdxCur = -1;.   
308dc 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20   }.    notReady 
308dd 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73  &= ~getMask(pMas
308de 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e  kSet, pTabList->
308df 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f 72  a[bestJ].iCursor
308e0 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69  );.    pLevel->i
308e1 46 72 6f 6d 20 3d 20 28 75 38 29 62 65 73 74 4a  From = (u8)bestJ
308e2 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
308e3 74 68 61 74 20 69 66 20 74 68 65 20 74 61 62 6c  that if the tabl
308e4 65 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68 69  e scanned by thi
308e5 73 20 6c 6f 6f 70 20 69 74 65 72 61 74 69 6f 6e  s loop iteration
308e6 20 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a 20 49   had an.    ** I
308e7 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
308e8 20 61 74 74 61 63 68 65 64 20 74 6f 20 69 74 2c   attached to it,
308e9 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 64 20   that the named 
308ea 69 6e 64 65 78 20 69 73 20 62 65 69 6e 67 0a 20  index is being. 
308eb 20 20 20 2a 2a 20 75 73 65 64 20 66 6f 72 20 74     ** used for t
308ec 68 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f 74 2c  he scan. If not,
308ed 20 74 68 65 6e 20 71 75 65 72 79 20 63 6f 6d 70   then query comp
308ee 69 6c 61 74 69 6f 6e 20 68 61 73 20 66 61 69 6c  ilation has fail
308ef 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72  ed..    ** Retur
308f0 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
308f1 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20 70 54  */.    pIdx = pT
308f2 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d  abList->a[bestJ]
308f3 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69 66 28  .pIndex;.    if(
308f4 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69   pIdx ){.      i
308f5 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61  f( (bestPlan.pla
308f6 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
308f7 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30 20 29 7b  E_INDEXED)==0 ){
308f8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
308f9 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
308fa 20 22 63 61 6e 6e 6f 74 20 75 73 65 20 69 6e 64   "cannot use ind
308fb 65 78 3a 20 25 73 22 2c 20 70 49 64 78 2d 3e 7a  ex: %s", pIdx->z
308fc 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67  Name);.        g
308fd 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
308fe 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ror;.      }else
308ff 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
30900 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  an INDEXED BY cl
30901 61 75 73 65 20 69 73 20 75 73 65 64 2c 20 74 68  ause is used, th
30902 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 66 75  e bestIndex() fu
30903 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20  nction is.      
30904 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20    ** guaranteed 
30905 74 6f 20 66 69 6e 64 20 74 68 65 20 69 6e 64 65  to find the inde
30906 78 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  x specified in t
30907 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  he INDEXED BY cl
30908 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  ause.        ** 
30909 69 66 20 69 74 20 66 69 6e 64 20 61 6e 20 69 6e  if it find an in
3090a 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a 2f 0a 20  dex at all. */. 
3090b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 62         assert( b
3090c 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75 2e 70  estPlan.plan.u.p
3090d 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a 20 20 20  Idx==pIdx );.   
3090e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
3090f 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a   WHERETRACE(("**
30910 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46 69 6e 69  * Optimizer Fini
30911 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20  shed ***\n"));. 
30912 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
30913 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
30914 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
30915 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  o whereBeginErro
30916 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  r;.  }..  /* If 
30917 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20  the total query 
30918 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20 61 20 73  only selects a s
30919 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20  ingle row, then 
3091a 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a  the ORDER BY.  *
3091b 2a 20 63 6c 61 75 73 65 20 69 73 20 69 72 72 65  * clause is irre
3091c 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69  levant..  */.  i
3091d 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57  f( (andFlags & W
3091e 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20  HERE_UNIQUE)!=0 
3091f 26 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b 0a  && ppOrderBy ){.
30920 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20 3d      *ppOrderBy =
30921 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   0;.  }..  /* If
30922 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61   the caller is a
30923 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  n UPDATE or DELE
30924 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  TE statement tha
30925 74 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 0a  t is requesting.
30926 20 20 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e    ** to use a on
30927 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d  e-pass algorithm
30928 2c 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  , determine if t
30929 68 69 73 20 69 73 20 61 70 70 72 6f 70 72 69 61  his is appropria
3092a 74 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65  te..  ** The one
3092b 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
3092c 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 74 68  only works if th
3092d 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63  e WHERE clause c
3092e 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20  onstraints.  ** 
3092f 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  the statement to
30930 20 75 70 64 61 74 65 20 61 20 73 69 6e 67 6c 65   update a single
30931 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73   row..  */.  ass
30932 65 72 74 28 20 28 77 63 74 72 6c 46 6c 61 67 73  ert( (wctrlFlags
30933 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53   & WHERE_ONEPASS
30934 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20  _DESIRED)==0 || 
30935 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d  pWInfo->nLevel==
30936 31 20 29 3b 0a 20 20 69 66 28 20 28 77 63 74 72  1 );.  if( (wctr
30937 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  lFlags & WHERE_O
30938 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29 21  NEPASS_DESIRED)!
30939 3d 30 20 26 26 20 28 61 6e 64 46 6c 61 67 73 20  =0 && (andFlags 
3093a 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21  & WHERE_UNIQUE)!
3093b 3d 30 20 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f  =0 ){.    pWInfo
3093c 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b  ->okOnePass = 1;
3093d 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30  .    pWInfo->a[0
3093e 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ].plan.wsFlags &
3093f 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c  = ~WHERE_IDX_ONL
30940 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  Y;.  }..  /* Ope
30941 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20  n all tables in 
30942 74 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64  the pTabList and
30943 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c   any indices sel
30944 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73  ected for.  ** s
30945 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74  earching those t
30946 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71  ables..  */.  sq
30947 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
30948 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31  chema(pParse, -1
30949 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65  ); /* Insert the
3094a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72   cookie verifier
3094b 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69   Goto */.  for(i
3094c 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66  =0, pLevel=pWInf
3094d 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  o->a; i<pTabList
3094e 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65  ->nSrc; i++, pLe
3094f 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  vel++){.    Tabl
30950 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20  e *pTab;     /* 
30951 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f  Table to open */
30952 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20  .    int iDb;   
30953 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
30954 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  f database conta
30955 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65  ining table/inde
30956 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  x */..#ifndef SQ
30957 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
30958 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  N.    if( pParse
30959 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  ->explain==2 ){.
3095a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67        char *zMsg
3095b 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53  ;.      struct S
3095c 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
3095d 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
3095e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
3095f 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73  ;.      zMsg = s
30960 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
30961 2c 20 22 54 41 42 4c 45 20 25 73 22 2c 20 70 49  , "TABLE %s", pI
30962 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
30963 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41     if( pItem->zA
30964 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20 20  lias ){.        
30965 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
30966 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
30967 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a 4d 73   "%s AS %s", zMs
30968 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  g, pItem->zAlias
30969 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
3096a 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
3096b 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3096c 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29  RE_INDEXED)!=0 )
3096d 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
3096e 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
3096f 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 57  (db, zMsg, "%s W
30970 49 54 48 20 49 4e 44 45 58 20 25 73 22 2c 0a 20  ITH INDEX %s",. 
30971 20 20 20 20 20 20 20 20 20 20 7a 4d 73 67 2c 20            zMsg, 
30972 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
30973 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Idx->zName);.   
30974 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65     }else if( pLe
30975 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
30976 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f  s & WHERE_MULTI_
30977 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d  OR ){.        zM
30978 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70  sg = sqlite3MApp
30979 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22  endf(db, zMsg, "
3097a 25 73 20 56 49 41 20 4d 55 4c 54 49 2d 49 4e 44  %s VIA MULTI-IND
3097b 45 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73 67 29  EX UNION", zMsg)
3097c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
3097d 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
3097e 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  sFlags & (WHERE_
3097f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
30980 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20  OWID_RANGE) ){. 
30981 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
30982 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
30983 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55 53 49 4e  , zMsg, "%s USIN
30984 47 20 50 52 49 4d 41 52 59 20 4b 45 59 22 2c 20  G PRIMARY KEY", 
30985 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 23  zMsg);.      }.#
30986 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
30987 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
30988 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 28        else if( (
30989 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3098a 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 52  lags & WHERE_VIR
3098b 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b  TUALTABLE)!=0 ){
3098c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3098d 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74  _index_info *pVt
3098e 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e  abIdx = pLevel->
3098f 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b  plan.u.pVtabIdx;
30990 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
30991 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
30992 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49  db, zMsg, "%s VI
30993 52 54 55 41 4c 20 54 41 42 4c 45 20 49 4e 44 45  RTUAL TABLE INDE
30994 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a  X %d:%s", zMsg,.
30995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30996 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 69 64      pVtabIdx->id
30997 78 4e 75 6d 2c 20 70 56 74 61 62 49 64 78 2d 3e  xNum, pVtabIdx->
30998 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20 7d  idxStr);.      }
30999 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
3099a 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
3099b 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  sFlags & WHERE_O
3099c 52 44 45 52 42 59 20 29 7b 0a 20 20 20 20 20 20  RDERBY ){.      
3099d 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
3099e 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
3099f 67 2c 20 22 25 73 20 4f 52 44 45 52 20 42 59 22  g, "%s ORDER BY"
309a0 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  , zMsg);.      }
309a1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
309a2 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45  beAddOp4(v, OP_E
309a3 78 70 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65  xplain, i, pLeve
309a4 6c 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73  l->iFrom, 0, zMs
309a5 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  g, P4_DYNAMIC);.
309a6 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
309a7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
309a8 41 49 4e 20 2a 2f 0a 20 20 20 20 70 54 61 62 49  AIN */.    pTabI
309a9 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
309aa 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
309ab 5d 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 54  ];.    pTab = pT
309ac 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20  abItem->pTab;.  
309ad 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
309ae 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
309af 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
309b0 0a 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e  .    if( (pTab->
309b1 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70  tabFlags & TF_Ep
309b2 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70  hemeral)!=0 || p
309b3 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63  Tab->pSelect ) c
309b4 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
309b5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
309b6 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
309b7 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
309b8 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
309b9 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30  VIRTUALTABLE)!=0
309ba 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
309bb 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63  char *pVTab = (c
309bc 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
309bd 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
309be 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 69 6e   pTab);.      in
309bf 74 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65  t iCur = pTabIte
309c0 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
309c1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
309c2 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c  Op4(v, OP_VOpen,
309c3 20 69 43 75 72 2c 20 30 2c 20 30 2c 20 70 56 54   iCur, 0, 0, pVT
309c4 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20  ab, P4_VTAB);.  
309c5 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
309c6 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e     if( (pLevel->
309c7 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
309c8 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
309c9 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 77  0.         && (w
309ca 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
309cb 45 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30 20  E_OMIT_OPEN)==0 
309cc 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20  ){.      int op 
309cd 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50  = pWInfo->okOneP
309ce 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69  ass ? OP_OpenWri
309cf 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64  te : OP_OpenRead
309d0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
309d1 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
309d2 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
309d3 6f 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f  or, iDb, pTab, o
309d4 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  p);.      if( !p
309d5 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
309d6 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c 42   && pTab->nCol<B
309d7 4d 53 20 29 7b 0a 20 20 20 20 20 20 20 20 42 69  MS ){.        Bi
309d8 74 6d 61 73 6b 20 62 20 3d 20 70 54 61 62 49 74  tmask b = pTabIt
309d9 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20  em->colUsed;.   
309da 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a       int n = 0;.
309db 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 62 3b          for(; b;
309dc 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0a   b=b>>1, n++){}.
309dd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
309de 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 73  dbeChangeP4(v, s
309df 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
309e0 74 41 64 64 72 28 76 29 2d 31 2c 20 0a 20 20 20  tAddr(v)-1, .   
309e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309e2 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
309e3 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50  INT_TO_PTR(n), P
309e4 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20  4_INT32);.      
309e5 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61    assert( n<=pTa
309e6 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20  b->nCol );.     
309e7 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
309e8 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
309e9 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62  Lock(pParse, iDb
309ea 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c  , pTab->tnum, 0,
309eb 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
309ec 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d     }.    pLevel-
309ed 3e 69 54 61 62 43 75 72 20 3d 20 70 54 61 62 49  >iTabCur = pTabI
309ee 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
309ef 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70    if( (pLevel->p
309f0 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
309f1 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20  ERE_INDEXED)!=0 
309f2 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  ){.      Index *
309f3 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  pIx = pLevel->pl
309f4 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 20  an.u.pIdx;.     
309f5 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
309f6 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
309f7 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 78  info(pParse, pIx
309f8 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64  );.      int iId
309f9 78 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  xCur = pLevel->i
309fa 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 61 73  IdxCur;.      as
309fb 73 65 72 74 28 20 70 49 78 2d 3e 70 53 63 68 65  sert( pIx->pSche
309fc 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma==pTab->pSchem
309fd 61 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  a );.      asser
309fe 74 28 20 69 49 64 78 43 75 72 3e 3d 30 20 29 3b  t( iIdxCur>=0 );
309ff 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
30a00 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
30a01 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72  penRead, iIdxCur
30a02 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62  , pIx->tnum, iDb
30a03 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30a04 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
30a05 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
30a06 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
30a07 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
30a08 2c 20 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61  , "%s", pIx->zNa
30a09 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  me));.    }.    
30a0a 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
30a0b 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
30a0c 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e  iDb);.  }.  pWIn
30a0d 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74  fo->iTop = sqlit
30a0e 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
30a0f 72 28 76 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  r(v);..  /* Gene
30a10 72 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f  rate the code to
30a11 20 64 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20   do the search. 
30a12 20 45 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20   Each iteration 
30a13 6f 66 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20  of the for.  ** 
30a14 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72  loop below gener
30a15 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20 61 20  ates code for a 
30a16 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f  single nested lo
30a17 6f 70 20 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a  op of the VM.  *
30a18 2a 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a  * program..  */.
30a19 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42    notReady = ~(B
30a1a 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28  itmask)0;.  for(
30a1b 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  i=0; i<pTabList-
30a1c 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
30a1d 20 6e 6f 74 52 65 61 64 79 20 3d 20 63 6f 64 65   notReady = code
30a1e 4f 6e 65 4c 6f 6f 70 53 74 61 72 74 28 70 57 49  OneLoopStart(pWI
30a1f 6e 66 6f 2c 20 69 2c 20 77 63 74 72 6c 46 6c 61  nfo, i, wctrlFla
30a20 67 73 2c 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20  gs, notReady);. 
30a21 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74     pWInfo->iCont
30a22 69 6e 75 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 61  inue = pWInfo->a
30a23 5b 69 5d 2e 61 64 64 72 43 6f 6e 74 3b 0a 20 20  [i].addrCont;.  
30a24 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
30a25 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65  _TEST  /* For te
30a26 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
30a27 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a  ing use only */.
30a28 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74    /* Record in t
30a29 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e  he query plan in
30a2a 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
30a2b 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c  the current tabl
30a2c 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69  e.  ** and the i
30a2d 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61 63 63  ndex used to acc
30a2e 65 73 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e  ess it (if any).
30a2f 20 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 69    If the table i
30a30 74 73 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f  tself.  ** is no
30a31 74 20 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65  t used, its name
30a32 20 69 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20   is just '{}'.  
30a33 49 66 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75  If no index is u
30a34 73 65 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64  sed.  ** the ind
30a35 65 78 20 69 73 20 6c 69 73 74 65 64 20 61 73 20  ex is listed as 
30a36 22 7b 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72  "{}".  If the pr
30a37 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75 73 65  imary key is use
30a38 64 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78  d the.  ** index
30a39 20 6e 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20   name is '*'..  
30a3a 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
30a3b 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
30a3c 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  i++){.    char *
30a3d 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  z;.    int n;.  
30a3e 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e    pLevel = &pWIn
30a3f 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54  fo->a[i];.    pT
30a40 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69  abItem = &pTabLi
30a41 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46  st->a[pLevel->iF
30a42 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54  rom];.    z = pT
30a43 61 62 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a  abItem->zAlias;.
30a44 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a      if( z==0 ) z
30a45 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61   = pTabItem->pTa
30a46 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20  b->zName;.    n 
30a47 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
30a48 30 28 7a 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b  0(z);.    if( n+
30a49 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28  nQPlan < sizeof(
30a4a 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30a4b 61 6e 29 2d 31 30 20 29 7b 0a 20 20 20 20 20 20  an)-10 ){.      
30a4c 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
30a4d 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30a4e 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20  _IDX_ONLY ){.   
30a4f 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
30a50 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
30a51 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32  nQPlan], "{}", 2
30a52 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61  );.        nQPla
30a53 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65  n += 2;.      }e
30a54 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  lse{.        mem
30a55 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65  cpy(&sqlite3_que
30a56 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c  ry_plan[nQPlan],
30a57 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20   z, n);.        
30a58 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20  nQPlan += n;.   
30a59 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
30a5a 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
30a5b 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20  Plan++] = ' ';. 
30a5c 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73     }.    testcas
30a5d 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  e( pLevel->plan.
30a5e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30a5f 52 4f 57 49 44 5f 45 51 20 29 3b 0a 20 20 20 20  ROWID_EQ );.    
30a60 74 65 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c  testcase( pLevel
30a61 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
30a62 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e   WHERE_ROWID_RAN
30a63 47 45 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  GE );.    if( pL
30a64 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30a65 67 73 20 26 20 28 57 48 45 52 45 5f 52 4f 57 49  gs & (WHERE_ROWI
30a66 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44  D_EQ|WHERE_ROWID
30a67 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20 20 20 20  _RANGE) ){.     
30a68 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
30a69 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
30a6a 61 6e 5d 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20  an], "* ", 2);. 
30a6b 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32       nQPlan += 2
30a6c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
30a6d 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
30a6e 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
30a6f 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20  DEXED)!=0 ){.   
30a70 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74     n = sqlite3St
30a71 72 6c 65 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70  rlen30(pLevel->p
30a72 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d  lan.u.pIdx->zNam
30a73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 2b  e);.      if( n+
30a74 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28  nQPlan < sizeof(
30a75 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30a76 61 6e 29 2d 32 20 29 7b 0a 20 20 20 20 20 20 20  an)-2 ){.       
30a77 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
30a78 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
30a79 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  an], pLevel->pla
30a7a 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c  n.u.pIdx->zName,
30a7b 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50   n);.        nQP
30a7c 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20  lan += n;.      
30a7d 20 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f    sqlite3_query_
30a7e 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d  plan[nQPlan++] =
30a7f 20 27 20 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20   ' ';.      }.  
30a80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
30a81 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71  emcpy(&sqlite3_q
30a82 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
30a83 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20  ], "{} ", 3);.  
30a84 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b      nQPlan += 3;
30a85 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69  .    }.  }.  whi
30a86 6c 65 28 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20  le( nQPlan>0 && 
30a87 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30a88 61 6e 5b 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20  an[nQPlan-1]==' 
30a89 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ' ){.    sqlite3
30a8a 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51  _query_plan[--nQ
30a8b 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  Plan] = 0;.  }. 
30a8c 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
30a8d 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b  lan[nQPlan] = 0;
30a8e 0a 20 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23  .  nQPlan = 0;.#
30a8f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
30a90 54 45 53 54 20 2f 2f 20 54 65 73 74 69 6e 67 20  TEST // Testing 
30a91 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 75 73  and debugging us
30a92 65 20 6f 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20  e only */..  /* 
30a93 52 65 63 6f 72 64 20 74 68 65 20 63 6f 6e 74 69  Record the conti
30a94 6e 75 61 74 69 6f 6e 20 61 64 64 72 65 73 73 20  nuation address 
30a95 69 6e 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f  in the WhereInfo
30a96 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
30a97 6e 0a 20 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20  n.  ** clean up 
30a98 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f  and return..  */
30a99 0a 20 20 72 65 74 75 72 6e 20 70 57 49 6e 66 6f  .  return pWInfo
30a9a 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  ;..  /* Jump her
30a9b 65 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  e if malloc fail
30a9c 73 20 2a 2f 0a 77 68 65 72 65 42 65 67 69 6e 45  s */.whereBeginE
30a9d 72 72 6f 72 3a 0a 20 20 77 68 65 72 65 49 6e 66  rror:.  whereInf
30a9e 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f  oFree(db, pWInfo
30a9f 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
30aa0 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
30aa1 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
30aa2 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65  WHERE loop.  See
30aa3 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a   comments on .**
30aa4 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
30aa5 69 6e 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f  in() for additio
30aa6 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
30aa7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
30aa8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57  TE void sqlite3W
30aa9 68 65 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66  hereEnd(WhereInf
30aaa 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 50 61  o *pWInfo){.  Pa
30aab 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57  rse *pParse = pW
30aac 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20  Info->pParse;.  
30aad 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
30aae 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
30aaf 3b 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a  ;.  WhereLevel *
30ab0 70 4c 65 76 65 6c 3b 0a 20 20 53 72 63 4c 69 73  pLevel;.  SrcLis
30ab1 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20 70 57  t *pTabList = pW
30ab2 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a  Info->pTabList;.
30ab3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
30ab4 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f  pParse->db;..  /
30ab5 2a 20 47 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20  * Generate loop 
30ab6 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65  termination code
30ab7 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
30ab8 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
30ab9 50 61 72 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d  Parse);.  for(i=
30aba 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31  pTabList->nSrc-1
30abb 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
30abc 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e    pLevel = &pWIn
30abd 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 71  fo->a[i];.    sq
30abe 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
30abf 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d  Label(v, pLevel-
30ac0 3e 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20  >addrCont);.    
30ac1 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d  if( pLevel->op!=
30ac2 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20  OP_Noop ){.     
30ac3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30ac4 70 32 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70  p2(v, pLevel->op
30ac5 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c  , pLevel->p1, pL
30ac6 65 76 65 6c 2d 3e 70 32 29 3b 0a 20 20 20 20 20  evel->p2);.     
30ac7 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
30ac8 67 65 50 35 28 76 2c 20 70 4c 65 76 65 6c 2d 3e  geP5(v, pLevel->
30ac9 70 35 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  p5);.    }.    i
30aca 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
30acb 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30acc 49 4e 5f 41 42 4c 45 20 26 26 20 70 4c 65 76 65  IN_ABLE && pLeve
30acd 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3e 30 20 29 7b  l->u.in.nIn>0 ){
30ace 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49 6e  .      struct In
30acf 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20 20 20 20 20  Loop *pIn;.     
30ad0 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 73 71   int j;.      sq
30ad1 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
30ad2 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d  Label(v, pLevel-
30ad3 3e 61 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 20  >addrNxt);.     
30ad4 20 66 6f 72 28 6a 3d 70 4c 65 76 65 6c 2d 3e 75   for(j=pLevel->u
30ad5 2e 69 6e 2e 6e 49 6e 2c 20 70 49 6e 3d 26 70 4c  .in.nIn, pIn=&pL
30ad6 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
30ad7 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d  op[j-1]; j>0; j-
30ad8 2d 2c 20 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20  -, pIn--){.     
30ad9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
30ada 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61  mpHere(v, pIn->a
30adb 64 64 72 49 6e 54 6f 70 2b 31 29 3b 0a 20 20 20  ddrInTop+1);.   
30adc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30add 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
30ade 74 2c 20 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49  t, pIn->iCur, pI
30adf 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 29 3b 0a 20  n->addrInTop);. 
30ae0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30ae1 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49  beJumpHere(v, pI
30ae2 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2d 31 29 3b  n->addrInTop-1);
30ae3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
30ae4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
30ae5 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49   pLevel->u.in.aI
30ae6 6e 4c 6f 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20  nLoop);.    }.  
30ae7 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
30ae8 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
30ae9 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20  vel->addrBrk);. 
30aea 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69     if( pLevel->i
30aeb 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20  LeftJoin ){.    
30aec 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
30aed 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
30aee 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
30aef 5f 49 66 50 6f 73 2c 20 70 4c 65 76 65 6c 2d 3e  _IfPos, pLevel->
30af0 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
30af1 20 20 61 73 73 65 72 74 28 20 28 70 4c 65 76 65    assert( (pLeve
30af2 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30af3 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
30af4 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  )==0.           
30af5 7c 7c 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  || (pLevel->plan
30af6 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30af7 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 3b 0a  _INDEXED)!=0 );.
30af8 20 20 20 20 20 20 69 66 28 20 28 70 4c 65 76 65        if( (pLeve
30af9 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30afa 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
30afb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
30afc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30afd 31 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c  1(v, OP_NullRow,
30afe 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pTabList->a[i].
30aff 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  iCursor);.      
30b00 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  }.      if( pLev
30b01 65 6c 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29  el->iIdxCur>=0 )
30b02 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
30b03 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
30b04 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65  P_NullRow, pLeve
30b05 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20  l->iIdxCur);.   
30b06 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
30b07 4c 65 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65  Level->op==OP_Re
30b08 74 75 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  turn ){.        
30b09 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30b0a 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70  2(v, OP_Gosub, p
30b0b 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65  Level->p1, pLeve
30b0c 6c 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20  l->addrFirst);. 
30b0d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
30b0e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30b0f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
30b10 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64  , 0, pLevel->add
30b11 72 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d  rFirst);.      }
30b12 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
30b13 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
30b14 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  dr);.    }.  }..
30b15 20 20 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22    /* The "break"
30b16 20 70 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20   point is here, 
30b17 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 65 6e  just past the en
30b18 64 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c  d of the outer l
30b19 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74  oop..  ** Set it
30b1a 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
30b1b 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
30b1c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  (v, pWInfo->iBre
30b1d 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65  ak);..  /* Close
30b1e 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73   all of the curs
30b1f 6f 72 73 20 74 68 61 74 20 77 65 72 65 20 6f 70  ors that were op
30b20 65 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 57  ened by sqlite3W
30b21 68 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a  hereBegin..  */.
30b22 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65    for(i=0, pLeve
30b23 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70  l=pWInfo->a; i<p
30b24 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
30b25 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20  ++, pLevel++){. 
30b26 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
30b27 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d  t_item *pTabItem
30b28 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b   = &pTabList->a[
30b29 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a  pLevel->iFrom];.
30b2a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
30b2b 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62  = pTabItem->pTab
30b2c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
30b2d 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ab!=0 );.    if(
30b2e 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
30b2f 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29   & TF_Ephemeral)
30b30 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65  !=0 || pTab->pSe
30b31 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b  lect ) continue;
30b32 0a 20 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f  .    if( (pWInfo
30b33 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57  ->wctrlFlags & W
30b34 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29  HERE_OMIT_CLOSE)
30b35 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
30b36 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50   !pWInfo->okOneP
30b37 61 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e  ass && (pLevel->
30b38 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30b39 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
30b3a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
30b3b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
30b3c 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62  , OP_Close, pTab
30b3d 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a  Item->iCursor);.
30b3e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
30b3f 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
30b40 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30b41 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20  INDEXED)!=0 ){. 
30b42 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30b43 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
30b44 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49  lose, pLevel->iI
30b45 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a  dxCur);.      }.
30b46 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
30b47 20 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20   this scan uses 
30b48 61 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63  an index, make c
30b49 6f 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e  ode substitution
30b4a 73 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20  s to read data. 
30b4b 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69     ** from the i
30b4c 6e 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e  ndex in preferen
30b4d 63 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e  ce to the table.
30b4e 20 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73   Sometimes, this
30b4f 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68   means.    ** th
30b50 65 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76  e table need nev
30b51 65 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e  er be read from.
30b52 20 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f   This is a perfo
30b53 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20  rmance boost,.  
30b54 20 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65    ** as the vdbe
30b55 20 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74   level waits unt
30b56 69 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  il the table is 
30b57 72 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75  read before actu
30b58 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b  ally.    ** seek
30b59 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75  ing the table cu
30b5a 72 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f  rsor to the reco
30b5b 72 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  rd corresponding
30b5c 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a   to the current.
30b5d 20 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20      ** position 
30b5e 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20  in the index..  
30b5f 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c    ** .    ** Cal
30b60 6c 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  ls to the code g
30b61 65 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77  enerator in betw
30b62 65 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65  een sqlite3Where
30b63 42 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a  Begin and.    **
30b64 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
30b65 20 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74   will have creat
30b66 65 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66  ed code that ref
30b67 65 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c  erences the tabl
30b68 65 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c  e.    ** directl
30b69 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63  y.  This loop sc
30b6a 61 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64  ans all that cod
30b6b 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70  e looking for op
30b6c 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61  codes.    ** tha
30b6d 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  t reference the 
30b6e 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72  table and conver
30b6f 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63  ts them into opc
30b70 6f 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a  odes that.    **
30b71 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69   reference the i
30b72 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
30b73 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
30b74 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
30b75 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26  RE_INDEXED)!=0 &
30b76 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  & !db->mallocFai
30b77 6c 65 64 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  led){.      int 
30b78 6b 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20  k, j, last;.    
30b79 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
30b7a 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
30b7b 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
30b7c 75 2e 70 49 64 78 3b 0a 0a 20 20 20 20 20 20 61  u.pIdx;..      a
30b7d 73 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29  ssert( pIdx!=0 )
30b7e 3b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71  ;.      pOp = sq
30b7f 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
30b80 2c 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b  , pWInfo->iTop);
30b81 0a 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71  .      last = sq
30b82 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
30b83 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66  Addr(v);.      f
30b84 6f 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f  or(k=pWInfo->iTo
30b85 70 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20  p; k<last; k++, 
30b86 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  pOp++){.        
30b87 69 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65  if( pOp->p1!=pLe
30b88 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63  vel->iTabCur ) c
30b89 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
30b8a 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
30b8b 3d 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20  ==OP_Column ){. 
30b8c 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
30b8d 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
30b8e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
30b8f 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32       if( pOp->p2
30b90 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ==pIdx->aiColumn
30b91 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [j] ){.         
30b92 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a       pOp->p2 = j
30b93 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
30b94 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c  pOp->p1 = pLevel
30b95 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20  ->iIdxCur;.     
30b96 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
30b97 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
30b98 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30b99 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4c 65      assert( (pLe
30b9a 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
30b9b 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
30b9c 4c 59 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  LY)==0.         
30b9d 20 20 20 20 20 20 7c 7c 20 6a 3c 70 49 64 78 2d        || j<pIdx-
30b9e 3e 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20  >nColumn );.    
30b9f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f      }else if( pO
30ba0 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f  p->opcode==OP_Ro
30ba1 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  wid ){.         
30ba2 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65   pOp->p1 = pLeve
30ba3 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
30ba4 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64        pOp->opcod
30ba5 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b  e = OP_IdxRowid;
30ba6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
30ba7 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
30ba8 2f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70  /* Final cleanup
30ba9 0a 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66  .  */.  whereInf
30baa 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f  oFree(db, pWInfo
30bab 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
30bac 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
30bad 45 6e 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a  End of where.c *
30bae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30baf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
30bb1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
30bb2 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65  Begin file parse
30bb3 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
30bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
30bb6 2f 2a 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61  /* Driver templa
30bb7 74 65 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e  te for the LEMON
30bb8 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f   parser generato
30bb9 72 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  r..** The author
30bba 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
30bbb 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
30bbc 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  rce code..**.** 
30bbd 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
30bbe 22 6c 65 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f  "lempar.c" is mo
30bbf 64 69 66 69 65 64 2c 20 73 6c 69 67 68 74 6c 79  dified, slightly
30bc0 2c 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c  , for use by SQL
30bc1 69 74 65 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  ite..** The only
30bc2 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61   modifications a
30bc3 72 65 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20  re the addition 
30bc4 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e  of a couple of N
30bc5 45 56 45 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73  EVER().** macros
30bc6 20 74 6f 20 64 69 73 61 62 6c 65 20 74 65 73 74   to disable test
30bc7 73 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65  s that are neede
30bc8 64 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66  d in the case of
30bc9 20 61 20 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41   a general.** LA
30bca 4c 52 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75  LR(1) grammar bu
30bcb 74 20 77 68 69 63 68 20 61 72 65 20 61 6c 77 61  t which are alwa
30bcc 79 73 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0a  ys false in the.
30bcd 2a 2a 20 73 70 65 63 69 66 69 63 20 67 72 61 6d  ** specific gram
30bce 6d 61 72 20 75 73 65 64 20 62 79 20 53 51 4c 69  mar used by SQLi
30bcf 74 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20  te..*/./* First 
30bd0 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63  off, code is inc
30bd1 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f  luded that follo
30bd2 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22  ws the "include"
30bd3 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20   declaration.** 
30bd4 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72 61  in the input gra
30bd5 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a  mmar file. */...
30bd6 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c  /*.** Disable al
30bd7 6c 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79  l error recovery
30bd8 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74   processing in t
30bd9 68 65 20 70 61 72 73 65 72 20 70 75 73 68 2d 64  he parser push-d
30bda 6f 77 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e  own.** automaton
30bdb 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e  ..*/.#define YYN
30bdc 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31  OERRORRECOVERY 1
30bdd 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74  ../*.** Make yyt
30bde 65 73 74 63 61 73 65 28 29 20 74 68 65 20 73 61  estcase() the sa
30bdf 6d 65 20 61 73 20 74 65 73 74 63 61 73 65 28 29  me as testcase()
30be0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65  .*/.#define yyte
30be1 73 74 63 61 73 65 28 58 29 20 74 65 73 74 63 61  stcase(X) testca
30be2 73 65 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  se(X)../*.** An 
30be3 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
30be4 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
30be5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
30be6 75 74 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20  ut the.** LIMIT 
30be7 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
30be8 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  CT statement..*/
30be9 0a 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c  .struct LimitVal
30bea 20 7b 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69   {.  Expr *pLimi
30beb 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d  t;    /* The LIM
30bec 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  IT expression.  
30bed 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
30bee 20 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45   no limit */.  E
30bef 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
30bf0 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 65 78  /* The OFFSET ex
30bf1 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20  pression.  NULL 
30bf2 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65  if there is none
30bf3 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
30bf4 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
30bf5 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
30bf6 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
30bf7 20 4c 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20   LIKE,.** GLOB, 
30bf8 4e 4f 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f  NOT LIKE, and NO
30bf9 54 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73  T GLOB operators
30bfa 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65  ..*/.struct Like
30bfb 4f 70 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70  Op {.  Token eOp
30bfc 65 72 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b  erator;  /* "lik
30bfd 65 22 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20  e" or "glob" or 
30bfe 22 72 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e  "regexp" */.  in
30bff 74 20 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f  t not;         /
30c00 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f  * True if the NO
30c01 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  T keyword is pre
30c02 73 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  sent */.};../*.*
30c03 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
30c04 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
30c05 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62  tructure describ
30c06 65 73 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20  es the event of 
30c07 61 0a 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22  a.** TRIGGER.  "
30c08 61 22 20 69 73 20 74 68 65 20 65 76 65 6e 74 20  a" is the event 
30c09 74 79 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f  type, one of TK_
30c0a 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
30c0b 54 2c 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c  T,.** TK_DELETE,
30c0c 20 6f 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20   or TK_INSTEAD. 
30c0d 20 49 66 20 74 68 65 20 65 76 65 6e 74 20 69 73   If the event is
30c0e 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
30c0f 2a 2a 20 20 20 20 20 20 55 50 44 41 54 45 20 4f  **      UPDATE O
30c10 4e 20 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20  N (a,b,c).**.** 
30c11 54 68 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c  Then the "b" IdL
30c12 69 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20  ist records the 
30c13 6c 69 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f  list "a,b,c"..*/
30c14 0a 73 74 72 75 63 74 20 54 72 69 67 45 76 65 6e  .struct TrigEven
30c15 74 20 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73  t { int a; IdLis
30c16 74 20 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a  t * b; };../*.**
30c17 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
30c18 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68  this structure h
30c19 6f 6c 64 73 20 74 68 65 20 41 54 54 41 43 48 20  olds the ATTACH 
30c1a 6b 65 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20  key and the key 
30c1b 74 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  type..*/.struct 
30c1c 41 74 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20  AttachKey { int 
30c1d 74 79 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79  type;  Token key
30c1e 3b 20 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73  ; };...  /* This
30c1f 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 72 6f   is a utility ro
30c20 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 73 65  utine used to se
30c21 74 20 74 68 65 20 45 78 70 72 53 70 61 6e 2e 7a  t the ExprSpan.z
30c22 53 74 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45  Start and.  ** E
30c23 78 70 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c  xprSpan.zEnd val
30c24 75 65 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74  ues of pOut so t
30c25 68 61 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76  hat the span cov
30c26 65 72 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ers the complete
30c27 0a 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74  .  ** range of t
30c28 65 78 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ext beginning wi
30c29 74 68 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f  th pStart and go
30c2a 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ing to the end o
30c2b 66 20 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73  f pEnd..  */.  s
30c2c 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 53  tatic void spanS
30c2d 65 74 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75  et(ExprSpan *pOu
30c2e 74 2c 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74  t, Token *pStart
30c2f 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a  , Token *pEnd){.
30c30 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74      pOut->zStart
30c31 20 3d 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20   = pStart->z;.  
30c32 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26    pOut->zEnd = &
30c33 70 45 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d  pEnd->z[pEnd->n]
30c34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73  ;.  }..  /* Cons
30c35 74 72 75 63 74 20 61 20 6e 65 77 20 45 78 70 72  truct a new Expr
30c36 20 6f 62 6a 65 63 74 20 66 72 6f 6d 20 61 20 73   object from a s
30c37 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ingle identifier
30c38 2e 20 20 55 73 65 20 74 68 65 0a 20 20 2a 2a 20  .  Use the.  ** 
30c39 6e 65 77 20 45 78 70 72 20 74 6f 20 70 6f 70 75  new Expr to popu
30c3a 6c 61 74 65 20 70 4f 75 74 2e 20 20 53 65 74 20  late pOut.  Set 
30c3b 74 68 65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74  the span of pOut
30c3c 20 74 6f 20 62 65 20 74 68 65 20 69 64 65 6e 74   to be the ident
30c3d 69 66 69 65 72 0a 20 20 2a 2a 20 74 68 61 74 20  ifier.  ** that 
30c3e 63 72 65 61 74 65 64 20 74 68 65 20 65 78 70 72  created the expr
30c3f 65 73 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73  ession..  */.  s
30c40 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 45  tatic void spanE
30c41 78 70 72 28 45 78 70 72 53 70 61 6e 20 2a 70 4f  xpr(ExprSpan *pO
30c42 75 74 2c 20 50 61 72 73 65 20 2a 70 50 61 72 73  ut, Parse *pPars
30c43 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e  e, int op, Token
30c44 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 20 20 70   *pValue){.    p
30c45 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  Out->pExpr = sql
30c46 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30c47 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c  , op, 0, 0, pVal
30c48 75 65 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  ue);.    pOut->z
30c49 53 74 61 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e  Start = pValue->
30c4a 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  z;.    pOut->zEn
30c4b 64 20 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70  d = &pValue->z[p
30c4c 56 61 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a  Value->n];.  }..
30c4d 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
30c4e 65 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62  e constructs a b
30c4f 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  inary expression
30c50 20 6e 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f   node out of two
30c51 20 45 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f   ExprSpan.  ** o
30c52 62 6a 65 63 74 73 20 61 6e 64 20 75 73 65 73 20  bjects and uses 
30c53 74 68 65 20 72 65 73 75 6c 74 20 74 6f 20 70 6f  the result to po
30c54 70 75 6c 61 74 65 20 61 20 6e 65 77 20 45 78 70  pulate a new Exp
30c55 72 53 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20  rSpan object..  
30c56 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  */.  static void
30c57 20 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28   spanBinaryExpr(
30c58 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
30c59 4f 75 74 2c 20 20 20 20 20 2f 2a 20 57 72 69 74  Out,     /* Writ
30c5a 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
30c5b 65 20 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a  e */.    Parse *
30c5c 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
30c5d 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
30c5e 65 78 74 2e 20 20 45 72 72 6f 72 73 20 61 63 63  ext.  Errors acc
30c5f 75 6d 75 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a  umulate here */.
30c60 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20      int op,     
30c61 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
30c62 69 6e 61 72 79 20 6f 70 65 72 61 74 69 6f 6e 20  inary operation 
30c63 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20  */.    ExprSpan 
30c64 2a 70 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54 68  *pLeft,    /* Th
30c65 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
30c66 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  /.    ExprSpan *
30c67 70 52 69 67 68 74 20 20 20 20 2f 2a 20 54 68 65  pRight    /* The
30c68 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
30c69 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  /.  ){.    pOut-
30c6a 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  >pExpr = sqlite3
30c6b 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70  PExpr(pParse, op
30c6c 2c 20 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20  , pLeft->pExpr, 
30c6d 70 52 69 67 68 74 2d 3e 70 45 78 70 72 2c 20 30  pRight->pExpr, 0
30c6e 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74  );.    pOut->zSt
30c6f 61 72 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74  art = pLeft->zSt
30c70 61 72 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  art;.    pOut->z
30c71 45 6e 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45  End = pRight->zE
30c72 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  nd;.  }..  /* Co
30c73 6e 73 74 72 75 63 74 20 61 6e 20 65 78 70 72 65  nstruct an expre
30c74 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61  ssion node for a
30c75 20 75 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f   unary postfix o
30c76 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73  perator.  */.  s
30c77 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55  tatic void spanU
30c78 6e 61 72 79 50 6f 73 74 66 69 78 28 0a 20 20 20  naryPostfix(.   
30c79 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c   ExprSpan *pOut,
30c7a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
30c7b 20 74 68 65 20 6e 65 77 20 65 78 70 72 65 73 73   the new express
30c7c 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f  ion node here */
30c7d 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72  .    Parse *pPar
30c7e 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  se,         /* P
30c7f 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  arsing context t
30c80 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20  o record errors 
30c81 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20  */.    int op,  
30c82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30c83 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f   The operator */
30c84 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
30c85 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54  Operand,    /* T
30c86 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  he operand */.  
30c87 20 20 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70    Token *pPostOp
30c88 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
30c89 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f  operand token fo
30c8a 72 20 73 65 74 74 69 6e 67 20 74 68 65 20 73 70  r setting the sp
30c8b 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70  an */.  ){.    p
30c8c 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  Out->pExpr = sql
30c8d 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30c8e 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e  , op, pOperand->
30c8f 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
30c90 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
30c91 20 70 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72   pOperand->zStar
30c92 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  t;.    pOut->zEn
30c93 64 20 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b  d = &pPostOp->z[
30c94 70 50 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d  pPostOp->n];.  }
30c95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c96 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f             ..  /
30c97 2a 20 41 20 72 6f 75 74 69 6e 65 20 74 6f 20 63  * A routine to c
30c98 6f 6e 76 65 72 74 20 61 20 62 69 6e 61 72 79 20  onvert a binary 
30c99 54 4b 5f 49 53 20 6f 72 20 54 4b 5f 49 53 4e 4f  TK_IS or TK_ISNO
30c9a 54 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74  T expression int
30c9b 6f 20 61 0a 20 20 2a 2a 20 75 6e 61 72 79 20 54  o a.  ** unary T
30c9c 4b 5f 49 53 4e 55 4c 4c 20 6f 72 20 54 4b 5f 4e  K_ISNULL or TK_N
30c9d 4f 54 4e 55 4c 4c 20 65 78 70 72 65 73 73 69 6f  OTNULL expressio
30c9e 6e 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76  n. */.  static v
30c9f 6f 69 64 20 62 69 6e 61 72 79 54 6f 55 6e 61 72  oid binaryToUnar
30ca0 79 49 66 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70  yIfNull(Parse *p
30ca1 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 59 2c  Parse, Expr *pY,
30ca2 20 45 78 70 72 20 2a 70 41 2c 20 69 6e 74 20 6f   Expr *pA, int o
30ca3 70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  p){.    sqlite3 
30ca4 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
30ca5 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
30ca6 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26  llocFailed==0 &&
30ca7 20 70 59 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c   pY->op==TK_NULL
30ca8 20 29 7b 0a 20 20 20 20 20 20 70 41 2d 3e 6f 70   ){.      pA->op
30ca9 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 20   = (u8)op;.     
30caa 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
30cab 74 65 28 64 62 2c 20 70 41 2d 3e 70 52 69 67 68  te(db, pA->pRigh
30cac 74 29 3b 0a 20 20 20 20 20 20 70 41 2d 3e 70 52  t);.      pA->pR
30cad 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ight = 0;.    }.
30cae 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72    }..  /* Constr
30caf 75 63 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f  uct an expressio
30cb0 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75 6e 61  n node for a una
30cb1 72 79 20 70 72 65 66 69 78 20 6f 70 65 72 61 74  ry prefix operat
30cb2 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  or.  */.  static
30cb3 20 76 6f 69 64 20 73 70 61 6e 55 6e 61 72 79 50   void spanUnaryP
30cb4 72 65 66 69 78 28 0a 20 20 20 20 45 78 70 72 53  refix(.    ExprS
30cb5 70 61 6e 20 2a 70 4f 75 74 2c 20 20 20 20 20 20  pan *pOut,      
30cb6 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
30cb7 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ew expression no
30cb8 64 65 20 68 65 72 65 20 2a 2f 0a 20 20 20 20 50  de here */.    P
30cb9 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
30cba 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
30cbb 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 63 6f   context to reco
30cbc 72 64 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  rd errors */.   
30cbd 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
30cbe 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
30cbf 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45  perator */.    E
30cc0 78 70 72 53 70 61 6e 20 2a 70 4f 70 65 72 61 6e  xprSpan *pOperan
30cc1 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65  d,    /* The ope
30cc2 72 61 6e 64 20 2a 2f 0a 20 20 20 20 54 6f 6b 65  rand */.    Toke
30cc3 6e 20 2a 70 50 72 65 4f 70 20 20 20 20 20 20 20  n *pPreOp       
30cc4 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e 64    /* The operand
30cc5 20 74 6f 6b 65 6e 20 66 6f 72 20 73 65 74 74 69   token for setti
30cc6 6e 67 20 74 68 65 20 73 70 61 6e 20 2a 2f 0a 20  ng the span */. 
30cc7 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45   ){.    pOut->pE
30cc8 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
30cc9 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70  pr(pParse, op, p
30cca 4f 70 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c 20  Operand->pExpr, 
30ccb 30 2c 20 30 29 3b 0a 20 20 20 20 70 4f 75 74 2d  0, 0);.    pOut-
30ccc 3e 7a 53 74 61 72 74 20 3d 20 70 50 72 65 4f 70  >zStart = pPreOp
30ccd 2d 3e 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  ->z;.    pOut->z
30cce 45 6e 64 20 3d 20 70 4f 70 65 72 61 6e 64 2d 3e  End = pOperand->
30ccf 7a 45 6e 64 3b 0a 20 20 7d 0a 2f 2a 20 4e 65 78  zEnd;.  }./* Nex
30cd0 74 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20 76  t is all token v
30cd1 61 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72 6d  alues, in a form
30cd2 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73   suitable for us
30cd3 65 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72 73  e by makeheaders
30cd4 2e 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f  ..** This sectio
30cd5 6e 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20 75  n will be null u
30cd6 6e 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20 72  nless lemon is r
30cd7 75 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20 73  un with the -m s
30cd8 77 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a 2a  witch..*/./* .**
30cd9 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
30cda 20 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64 20   (all generated 
30cdb 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79  automatically by
30cdc 20 74 68 65 20 70 61 72 73 65 72 20 67 65 6e 65   the parser gene
30cdd 72 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69 66  rator).** specif
30cde 79 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b 69  y the various ki
30cdf 6e 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28 74  nds of tokens (t
30ce0 65 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20 74  erminals) that t
30ce1 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e 64  he parser.** und
30ce2 65 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a 2a  erstands. .**.**
30ce3 20 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65 72   Each symbol her
30ce4 65 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c 20  e is a terminal 
30ce5 73 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67 72  symbol in the gr
30ce6 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61 6b  ammar..*/./* Mak
30ce7 65 20 73 75 72 65 20 74 68 65 20 49 4e 54 45 52  e sure the INTER
30ce8 46 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64 65  FACE macro is de
30ce9 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  fined..*/.#ifnde
30cea 66 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64 65  f INTERFACE.# de
30ceb 66 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20 31  fine INTERFACE 1
30cec 0a 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20 6e  .#endif./* The n
30ced 65 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75 64  ext thing includ
30cee 65 64 20 69 73 20 73 65 72 69 65 73 20 6f 66 20  ed is series of 
30cef 64 65 66 69 6e 65 73 20 77 68 69 63 68 20 63 6f  defines which co
30cf0 6e 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75 73  ntrol.** various
30cf1 20 61 73 70 65 63 74 73 20 6f 66 20 74 68 65 20   aspects of the 
30cf2 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65 72  generated parser
30cf3 2e 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54 59  ..**    YYCODETY
30cf4 50 45 20 20 20 20 20 20 20 20 20 69 73 20 74 68  PE         is th
30cf5 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64  e data type used
30cf6 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72   for storing ter
30cf7 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  minal.**        
30cf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
30cf9 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e  nd nonterminal n
30cfa 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e  umbers.  "unsign
30cfb 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20  ed char" is.**  
30cfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cfd 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65       used if the
30cfe 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61  re are fewer tha
30cff 6e 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73 0a  n 250 terminals.
30d00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30d01 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e           and non
30d02 74 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e 74  terminals.  "int
30d03 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77  " is used otherw
30d04 69 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f 43  ise..**    YYNOC
30d05 4f 44 45 20 20 20 20 20 20 20 20 20 20 20 69 73  ODE           is
30d06 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79 70   a number of typ
30d07 65 20 59 59 43 4f 44 45 54 59 50 45 20 77 68 69  e YYCODETYPE whi
30d08 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a  ch corresponds.*
30d09 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
30d0a 20 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c 65          to no le
30d0b 67 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20  gal terminal or 
30d0c 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62  nonterminal numb
30d0d 65 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20 20  er.  This.**    
30d0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d0f 20 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73 65     number is use
30d10 64 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d 70  d to fill in emp
30d11 74 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65 20  ty slots of the 
30d12 68 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20 20  hash .**        
30d13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
30d14 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46 41  able..**    YYFA
30d15 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20 49  LLBACK         I
30d16 66 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73 20  f defined, this 
30d17 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 6f  indicates that o
30d18 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e  ne or more token
30d19 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
30d1a 20 20 20 20 20 20 20 20 20 20 20 68 61 76 65 20             have 
30d1b 66 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65 73  fall-back values
30d1c 20 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65   which should be
30d1d 20 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a 20   used if the.** 
30d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d1f 20 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20 76        original v
30d20 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  alue of the toke
30d21 6e 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73 65  n will not parse
30d22 2e 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f 4e  ..**    YYACTION
30d23 54 59 50 45 20 20 20 20 20 20 20 69 73 20 74 68  TYPE       is th
30d24 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64  e data type used
30d25 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 72   for storing ter
30d26 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  minal.**        
30d27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
30d28 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e  nd nonterminal n
30d29 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67 6e  umbers.  "unsign
30d2a 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20 20  ed char" is.**  
30d2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d2c 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68 65       used if the
30d2d 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61  re are fewer tha
30d2e 6e 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64 0a  n 250 rules and.
30d2f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30d30 20 20 20 20 20 20 20 20 20 73 74 61 74 65 73 20           states 
30d31 63 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74 22  combined.  "int"
30d32 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77 69   is used otherwi
30d33 73 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  se..**    sqlite
30d34 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45  3ParserTOKENTYPE
30d35 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
30d36 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 6d   type used for m
30d37 69 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76 65  inor tokens give
30d38 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n .**           
30d39 20 20 20 20 20 20 20 20 20 20 20 20 64 69 72 65              dire
30d3a 63 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72 73  ctly to the pars
30d3b 65 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65  er from the toke
30d3c 6e 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59 4d  nizer..**    YYM
30d3d 49 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20 20  INORTYPE        
30d3e 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65  is the data type
30d3f 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d 69   used for all mi
30d40 6e 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 20  nor tokens..**  
30d41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d42 20 20 20 20 20 54 68 69 73 20 69 73 20 74 79 70       This is typ
30d43 69 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20 6f  ically a union o
30d44 66 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f 6e  f many types, on
30d45 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  e of.**         
30d46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68                wh
30d47 69 63 68 20 69 73 20 73 71 6c 69 74 65 33 50 61  ich is sqlite3Pa
30d48 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20 20  rserTOKENTYPE.  
30d49 54 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65  The entry in the
30d4a 20 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20   union.**       
30d4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d4c 66 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73 20  for base tokens 
30d4d 69 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22 2e  is called "yy0".
30d4e 0a 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44 45  .**    YYSTACKDE
30d4f 50 54 48 20 20 20 20 20 20 20 69 73 20 74 68 65  PTH       is the
30d50 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f   maximum depth o
30d51 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73  f the parser's s
30d52 74 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20 20  tack.  If.**    
30d53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d54 20 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61 63     zero the stac
30d55 6b 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79  k is dynamically
30d56 20 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65 61   sized using rea
30d57 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71 6c  lloc().**    sql
30d58 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44  ite3ParserARG_SD
30d59 45 43 4c 20 20 20 20 20 41 20 73 74 61 74 69 63  ECL     A static
30d5a 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72   variable declar
30d5b 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65  ation for the %e
30d5c 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra_argument.**
30d5d 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
30d5e 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 41  rARG_PDECL     A
30d5f 20 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c 61   parameter decla
30d60 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25  ration for the %
30d61 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a  extra_argument.*
30d62 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
30d63 65 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20 20  erARG_STORE     
30d64 43 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25 65  Code to store %e
30d65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69 6e  xtra_argument in
30d66 74 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20  to yypParser.** 
30d67 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
30d68 41 52 47 5f 46 45 54 43 48 20 20 20 20 20 43 6f  ARG_FETCH     Co
30d69 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 25 65  de to extract %e
30d6a 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66 72  xtra_argument fr
30d6b 6f 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a 20  om yypParser.** 
30d6c 20 20 20 59 59 4e 53 54 41 54 45 20 20 20 20 20     YYNSTATE     
30d6d 20 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69 6e        the combin
30d6e 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74 61  ed number of sta
30d6f 74 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52 55  tes..**    YYNRU
30d70 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 74 68  LE            th
30d71 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c 65  e number of rule
30d72 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  s in the grammar
30d73 0a 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53 59  .**    YYERRORSY
30d74 4d 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68 65  MBOL      is the
30d75 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20   code number of 
30d76 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c  the error symbol
30d77 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20 20  .  If not.**    
30d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d79 20 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e     defined, then
30d7a 20 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72 6f   do no error pro
30d7b 63 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66  cessing..*/.#def
30d7c 69 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20 75  ine YYCODETYPE u
30d7d 6e 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64 65  nsigned char.#de
30d7e 66 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32 35  fine YYNOCODE 25
30d7f 34 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54 49  4.#define YYACTI
30d80 4f 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20  ONTYPE unsigned 
30d81 73 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69 6e  short int.#defin
30d82 65 20 59 59 57 49 4c 44 43 41 52 44 20 36 37 0a  e YYWILDCARD 67.
30d83 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50  #define sqlite3P
30d84 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 54  arserTOKENTYPE T
30d85 6f 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e 69  oken.typedef uni
30d86 6f 6e 20 7b 0a 20 20 69 6e 74 20 79 79 69 6e 69  on {.  int yyini
30d87 74 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  t;.  sqlite3Pars
30d88 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 30 3b  erTOKENTYPE yy0;
30d89 0a 20 20 53 65 6c 65 63 74 2a 20 79 79 33 3b 0a  .  Select* yy3;.
30d8a 20 20 45 78 70 72 4c 69 73 74 2a 20 79 79 31 34    ExprList* yy14
30d8b 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 36  ;.  SrcList* yy6
30d8c 35 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6b 65  5;.  struct Like
30d8d 4f 70 20 79 79 39 36 3b 0a 20 20 45 78 70 72 2a  Op yy96;.  Expr*
30d8e 20 79 79 31 33 32 3b 0a 20 20 75 38 20 79 79 31   yy132;.  u8 yy1
30d8f 38 36 3b 0a 20 20 69 6e 74 20 79 79 33 32 38 3b  86;.  int yy328;
30d90 0a 20 20 45 78 70 72 53 70 61 6e 20 79 79 33 34  .  ExprSpan yy34
30d91 36 3b 0a 20 20 73 74 72 75 63 74 20 54 72 69 67  6;.  struct Trig
30d92 45 76 65 6e 74 20 79 79 33 37 38 3b 0a 20 20 49  Event yy378;.  I
30d93 64 4c 69 73 74 2a 20 79 79 34 30 38 3b 0a 20 20  dList* yy408;.  
30d94 73 74 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75  struct {int valu
30d95 65 3b 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79  e; int mask;} yy
30d96 34 32 39 3b 0a 20 20 54 72 69 67 67 65 72 53 74  429;.  TriggerSt
30d97 65 70 2a 20 79 79 34 37 33 3b 0a 20 20 73 74 72  ep* yy473;.  str
30d98 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 79 79 34  uct LimitVal yy4
30d99 37 36 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50  76;.} YYMINORTYP
30d9a 45 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41  E;.#ifndef YYSTA
30d9b 43 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20  CKDEPTH.#define 
30d9c 59 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30  YYSTACKDEPTH 100
30d9d 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
30d9e 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
30d9f 5f 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50  _SDECL Parse *pP
30da0 61 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71  arse;.#define sq
30da1 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
30da2 44 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61  DECL ,Parse *pPa
30da3 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rse.#define sqli
30da4 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
30da5 43 48 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  CH Parse *pParse
30da6 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50   = yypParser->pP
30da7 61 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  arse.#define sql
30da8 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
30da9 4f 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70  ORE yypParser->p
30daa 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23  Parse = pParse.#
30dab 64 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20  define YYNSTATE 
30dac 36 32 39 0a 23 64 65 66 69 6e 65 20 59 59 4e 52  629.#define YYNR
30dad 55 4c 45 20 33 32 39 0a 23 64 65 66 69 6e 65 20  ULE 329.#define 
30dae 59 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65  YYFALLBACK 1.#de
30daf 66 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f  fine YY_NO_ACTIO
30db0 4e 20 20 20 20 20 20 28 59 59 4e 53 54 41 54 45  N      (YYNSTATE
30db1 2b 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66  +YYNRULE+2).#def
30db2 69 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43  ine YY_ACCEPT_AC
30db3 54 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b  TION  (YYNSTATE+
30db4 59 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69  YYNRULE+1).#defi
30db5 6e 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49  ne YY_ERROR_ACTI
30db6 4f 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59  ON   (YYNSTATE+Y
30db7 59 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20  YNRULE)../* The 
30db8 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73  yyzerominor cons
30db9 74 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  tant is used to 
30dba 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61  initialize insta
30dbb 6e 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e  nces of.** YYMIN
30dbc 4f 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74  ORTYPE objects t
30dbd 6f 20 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69  o zero. */.stati
30dbe 63 20 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54  c const YYMINORT
30dbf 59 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20  YPE yyzerominor 
30dc0 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 20 44 65 66  = { 0 };../* Def
30dc1 69 6e 65 20 74 68 65 20 79 79 74 65 73 74 63 61  ine the yytestca
30dc2 73 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 62 65  se() macro to be
30dc3 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 6e   a no-op if is n
30dc4 6f 74 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e  ot already defin
30dc5 65 64 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e  ed.** otherwise.
30dc6 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  .**.** Applicati
30dc7 6f 6e 73 20 63 61 6e 20 63 68 6f 6f 73 65 20 74  ons can choose t
30dc8 6f 20 64 65 66 69 6e 65 20 79 79 74 65 73 74 63  o define yytestc
30dc9 61 73 65 28 29 20 69 6e 20 74 68 65 20 25 69 6e  ase() in the %in
30dca 63 6c 75 64 65 20 73 65 63 74 69 6f 6e 0a 2a 2a  clude section.**
30dcb 20 74 6f 20 61 20 6d 61 63 72 6f 20 74 68 61 74   to a macro that
30dcc 20 63 61 6e 20 61 73 73 69 73 74 20 69 6e 20 76   can assist in v
30dcd 65 72 69 66 79 69 6e 67 20 63 6f 64 65 20 63 6f  erifying code co
30dce 76 65 72 61 67 65 2e 20 20 46 6f 72 20 70 72 6f  verage.  For pro
30dcf 64 75 63 74 69 6f 6e 0a 2a 2a 20 63 6f 64 65 20  duction.** code 
30dd0 74 68 65 20 79 79 74 65 73 74 63 61 73 65 28 29  the yytestcase()
30dd1 20 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62 65   macro should be
30dd2 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 42 75   turned off.  Bu
30dd3 74 20 69 74 20 69 73 20 75 73 65 66 75 6c 0a 2a  t it is useful.*
30dd4 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  * for testing..*
30dd5 2f 0a 23 69 66 6e 64 65 66 20 79 79 74 65 73 74  /.#ifndef yytest
30dd6 63 61 73 65 0a 23 20 64 65 66 69 6e 65 20 79 79  case.# define yy
30dd7 74 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64  testcase(X).#end
30dd8 69 66 0a 0a 0a 2f 2a 20 4e 65 78 74 20 61 72 65  if.../* Next are
30dd9 20 74 68 65 20 74 61 62 6c 65 73 20 75 73 65 64   the tables used
30dda 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
30ddb 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b  at action to tak
30ddc 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  e based on the.*
30ddd 2a 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  * current state 
30dde 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f  and lookahead to
30ddf 6b 65 6e 2e 20 20 54 68 65 73 65 20 74 61 62 6c  ken.  These tabl
30de0 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69  es are used to i
30de1 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63  mplement.** func
30de2 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20  tions that take 
30de3 61 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 61  a state number a
30de4 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c  nd lookahead val
30de5 75 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e  ue and return an
30de6 0a 2a 2a 20 61 63 74 69 6f 6e 20 69 6e 74 65 67  .** action integ
30de7 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70  er.  .**.** Supp
30de8 6f 73 65 20 74 68 65 20 61 63 74 69 6f 6e 20 69  ose the action i
30de9 6e 74 65 67 65 72 20 69 73 20 4e 2e 20 20 54 68  nteger is N.  Th
30dea 65 6e 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  en the action is
30deb 20 64 65 74 65 72 6d 69 6e 65 64 20 61 73 0a 2a   determined as.*
30dec 2a 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20  * follows.**.** 
30ded 20 20 30 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54    0 <= N < YYNST
30dee 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
30def 20 20 20 20 20 53 68 69 66 74 20 4e 2e 20 20 54       Shift N.  T
30df0 68 61 74 20 69 73 2c 20 70 75 73 68 20 74 68 65  hat is, push the
30df1 20 6c 6f 6f 6b 61 68 65 61 64 0a 2a 2a 20 20 20   lookahead.**   
30df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30df4 20 20 20 74 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68     token onto th
30df5 65 20 73 74 61 63 6b 20 61 6e 64 20 67 6f 74 6f  e stack and goto
30df6 20 73 74 61 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20   state N..**.** 
30df7 20 20 59 59 4e 53 54 41 54 45 20 3c 3d 20 4e 20    YYNSTATE <= N 
30df8 3c 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55  < YYNSTATE+YYNRU
30df9 4c 45 20 20 20 52 65 64 75 63 65 20 62 79 20 72  LE   Reduce by r
30dfa 75 6c 65 20 4e 2d 59 59 4e 53 54 41 54 45 2e 0a  ule N-YYNSTATE..
30dfb 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e  **.**   N == YYN
30dfc 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20  STATE+YYNRULE   
30dfd 20 20 20 20 20 20 20 20 20 20 20 41 20 73 79 6e             A syn
30dfe 74 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63  tax error has oc
30dff 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  curred..**.**   
30e00 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59  N == YYNSTATE+YY
30e01 4e 52 55 4c 45 2b 31 20 20 20 20 20 20 20 20 20  NRULE+1         
30e02 20 20 20 54 68 65 20 70 61 72 73 65 72 20 61 63     The parser ac
30e03 63 65 70 74 73 20 69 74 73 20 69 6e 70 75 74 2e  cepts its input.
30e04 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59  .**.**   N == YY
30e05 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32  NSTATE+YYNRULE+2
30e06 20 20 20 20 20 20 20 20 20 20 20 20 4e 6f 20 73              No s
30e07 75 63 68 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e  uch action.  Den
30e08 6f 74 65 73 20 75 6e 75 73 65 64 0a 2a 2a 20 20  otes unused.**  
30e09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e0b 20 20 20 20 73 6c 6f 74 73 20 69 6e 20 74 68 65      slots in the
30e0c 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62   yy_action[] tab
30e0d 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63  le..**.** The ac
30e0e 74 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 63 6f  tion table is co
30e0f 6e 73 74 72 75 63 74 65 64 20 61 73 20 61 20 73  nstructed as a s
30e10 69 6e 67 6c 65 20 6c 61 72 67 65 20 74 61 62 6c  ingle large tabl
30e11 65 20 6e 61 6d 65 64 20 79 79 5f 61 63 74 69 6f  e named yy_actio
30e12 6e 5b 5d 2e 0a 2a 2a 20 47 69 76 65 6e 20 73 74  n[]..** Given st
30e13 61 74 65 20 53 20 61 6e 64 20 6c 6f 6f 6b 61 68  ate S and lookah
30e14 65 61 64 20 58 2c 20 74 68 65 20 61 63 74 69 6f  ead X, the actio
30e15 6e 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 73  n is computed as
30e16 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 79 79 5f 61  .**.**      yy_a
30e17 63 74 69 6f 6e 5b 20 79 79 5f 73 68 69 66 74 5f  ction[ yy_shift_
30e18 6f 66 73 74 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a  ofst[S] + X ].**
30e19 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78  .** If the index
30e1a 20 76 61 6c 75 65 20 79 79 5f 73 68 69 66 74 5f   value yy_shift_
30e1b 6f 66 73 74 5b 53 5d 2b 58 20 69 73 20 6f 75 74  ofst[S]+X is out
30e1c 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20   of range or if 
30e1d 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 79 79 5f  the value.** yy_
30e1e 6c 6f 6f 6b 61 68 65 61 64 5b 79 79 5f 73 68 69  lookahead[yy_shi
30e1f 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 5d 20 69 73  ft_ofst[S]+X] is
30e20 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 58 20   not equal to X 
30e21 6f 72 20 69 66 20 79 79 5f 73 68 69 66 74 5f 6f  or if yy_shift_o
30e22 66 73 74 5b 53 5d 0a 2a 2a 20 69 73 20 65 71 75  fst[S].** is equ
30e23 61 6c 20 74 6f 20 59 59 5f 53 48 49 46 54 5f 55  al to YY_SHIFT_U
30e24 53 45 5f 44 46 4c 54 2c 20 69 74 20 6d 65 61 6e  SE_DFLT, it mean
30e25 73 20 74 68 61 74 20 74 68 65 20 61 63 74 69 6f  s that the actio
30e26 6e 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20  n is not in the 
30e27 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 61  table.** and tha
30e28 74 20 79 79 5f 64 65 66 61 75 6c 74 5b 53 5d 20  t yy_default[S] 
30e29 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69  should be used i
30e2a 6e 73 74 65 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20  nstead.  .**.** 
30e2b 54 68 65 20 66 6f 72 6d 75 6c 61 20 61 62 6f 76  The formula abov
30e2c 65 20 69 73 20 66 6f 72 20 63 6f 6d 70 75 74 69  e is for computi
30e2d 6e 67 20 74 68 65 20 61 63 74 69 6f 6e 20 77 68  ng the action wh
30e2e 65 6e 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64  en the lookahead
30e2f 20 69 73 0a 2a 2a 20 61 20 74 65 72 6d 69 6e 61   is.** a termina
30e30 6c 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 74 68  l symbol.  If th
30e31 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 20 61  e lookahead is a
30e32 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 28 61   non-terminal (a
30e33 73 20 6f 63 63 75 72 73 20 61 66 74 65 72 0a 2a  s occurs after.*
30e34 2a 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f  * a reduce actio
30e35 6e 29 20 74 68 65 6e 20 74 68 65 20 79 79 5f 72  n) then the yy_r
30e36 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72  educe_ofst[] arr
30e37 61 79 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c  ay is used in pl
30e38 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 79 79  ace of.** the yy
30e39 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72  _shift_ofst[] ar
30e3a 72 61 79 20 61 6e 64 20 59 59 5f 52 45 44 55 43  ray and YY_REDUC
30e3b 45 5f 55 53 45 5f 44 46 4c 54 20 69 73 20 75 73  E_USE_DFLT is us
30e3c 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  ed in place of.*
30e3d 2a 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44  * YY_SHIFT_USE_D
30e3e 46 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  FLT..**.** The f
30e3f 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65  ollowing are the
30e40 20 74 61 62 6c 65 73 20 67 65 6e 65 72 61 74 65   tables generate
30e41 64 20 69 6e 20 74 68 69 73 20 73 65 63 74 69 6f  d in this sectio
30e42 6e 3a 0a 2a 2a 0a 2a 2a 20 20 79 79 5f 61 63 74  n:.**.**  yy_act
30e43 69 6f 6e 5b 5d 20 20 20 20 20 20 20 20 41 20 73  ion[]        A s
30e44 69 6e 67 6c 65 20 74 61 62 6c 65 20 63 6f 6e 74  ingle table cont
30e45 61 69 6e 69 6e 67 20 61 6c 6c 20 61 63 74 69 6f  aining all actio
30e46 6e 73 2e 0a 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61  ns..**  yy_looka
30e47 68 65 61 64 5b 5d 20 20 20 20 20 41 20 74 61 62  head[]     A tab
30e48 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
30e49 65 20 6c 6f 6f 6b 61 68 65 61 64 20 66 6f 72 20  e lookahead for 
30e4a 65 61 63 68 20 65 6e 74 72 79 20 69 6e 0a 2a 2a  each entry in.**
30e4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e4c 20 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 2e 20       yy_action. 
30e4d 20 55 73 65 64 20 74 6f 20 64 65 74 65 63 74 20   Used to detect 
30e4e 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e  hash collisions.
30e4f 0a 2a 2a 20 20 79 79 5f 73 68 69 66 74 5f 6f 66  .**  yy_shift_of
30e50 73 74 5b 5d 20 20 20 20 46 6f 72 20 65 61 63 68  st[]    For each
30e51 20 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73   state, the offs
30e52 65 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f  et into yy_actio
30e53 6e 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  n for.**        
30e54 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69               shi
30e55 66 74 69 6e 67 20 74 65 72 6d 69 6e 61 6c 73 2e  fting terminals.
30e56 0a 2a 2a 20 20 79 79 5f 72 65 64 75 63 65 5f 6f  .**  yy_reduce_o
30e57 66 73 74 5b 5d 20 20 20 46 6f 72 20 65 61 63 68  fst[]   For each
30e58 20 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73   state, the offs
30e59 65 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f  et into yy_actio
30e5a 6e 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  n for.**        
30e5b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69               shi
30e5c 66 74 69 6e 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e  fting non-termin
30e5d 61 6c 73 20 61 66 74 65 72 20 61 20 72 65 64 75  als after a redu
30e5e 63 65 2e 0a 2a 2a 20 20 79 79 5f 64 65 66 61 75  ce..**  yy_defau
30e5f 6c 74 5b 5d 20 20 20 20 20 20 20 44 65 66 61 75  lt[]       Defau
30e60 6c 74 20 61 63 74 69 6f 6e 20 66 6f 72 20 65 61  lt action for ea
30e61 63 68 20 73 74 61 74 65 2e 0a 2a 2f 0a 23 64 65  ch state..*/.#de
30e62 66 69 6e 65 20 59 59 5f 41 43 54 54 41 42 5f 43  fine YY_ACTTAB_C
30e63 4f 55 4e 54 20 28 31 35 34 33 29 0a 73 74 61 74  OUNT (1543).stat
30e64 69 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f  ic const YYACTIO
30e65 4e 54 59 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b  NTYPE yy_action[
30e66 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20  ] = {. /*     0 
30e67 2a 2f 20 20 20 33 31 32 2c 20 20 20 34 39 2c 20  */   312,   49, 
30e68 20 35 35 34 2c 20 20 20 34 36 2c 20 20 31 34 37   554,   46,  147
30e69 2c 20 20 31 37 32 2c 20 20 36 32 36 2c 20 20 35  ,  172,  626,  5
30e6a 39 36 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a  96,   55,   55,.
30e6b 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20   /*    10 */    
30e6c 35 35 2c 20 20 20 35 35 2c 20 20 33 30 31 2c 20  55,   55,  301, 
30e6d 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30e6e 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20  ,   53,   52,   
30e6f 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20  52,   51,. /*   
30e70 20 32 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20   20 */    51,   
30e71 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20  51,   50,  237, 
30e72 20 36 31 37 2c 20 20 36 31 36 2c 20 20 36 31 35   617,  616,  615
30e73 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20 36  ,  622,  621,  6
30e74 30 37 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f  07,. /*    30 */
30e75 20 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 20     589,  583,   
30e76 34 38 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  48,   53,   53, 
30e77 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
30e78 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f  ,   52,   51,. /
30e79 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 20 35 31  *    40 */    51
30e7a 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32  ,   51,   50,  2
30e7b 33 37 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  37,   51,   51, 
30e7c 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37    51,   50,  237
30e7d 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 20 35  ,   56,. /*    5
30e7e 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34 37  0 */    57,   47
30e7f 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35  ,  581,  580,  5
30e80 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20  82,  582,   54, 
30e81 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35    54,   55,   55
30e82 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20  ,. /*    60 */  
30e83 20 20 35 35 2c 20 20 20 35 35 2c 20 20 32 31 36    55,   55,  216
30e84 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30e85 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20  53,   53,   52, 
30e86 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20    52,   51,. /* 
30e87 20 20 20 37 30 20 2a 2f 20 20 20 20 35 31 2c 20     70 */    51, 
30e88 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37    51,   50,  237
30e89 2c 20 20 33 31 32 2c 20 20 35 39 36 2c 20 20 20  ,  312,  596,   
30e8a 34 39 2c 20 20 33 32 39 2c 20 20 20 34 36 2c 20  49,  329,   46, 
30e8b 20 31 34 37 2c 0a 20 2f 2a 20 20 20 20 38 30 20   147,. /*    80 
30e8c 2a 2f 20 20 20 20 33 32 2c 20 20 20 35 33 2c 20  */    32,   53, 
30e8d 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30e8e 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
30e8f 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 0a  51,   51,   51,.
30e90 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 20   /*    90 */    
30e91 35 30 2c 20 20 32 33 37 2c 20 20 33 32 39 2c 20  50,  237,  329, 
30e92 20 36 32 32 2c 20 20 36 32 31 2c 20 20 36 31 39   622,  621,  619
30e93 2c 20 20 36 31 38 2c 20 20 31 36 36 2c 20 20 34  ,  618,  166,  4
30e94 33 33 2c 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20  33,  547,. /*   
30e95 31 30 30 20 2a 2f 20 20 20 33 38 31 2c 20 20 33  100 */   381,  3
30e96 37 38 2c 20 20 33 37 37 2c 20 20 35 34 39 2c 20  78,  377,  549, 
30e97 20 35 38 39 2c 20 20 35 38 33 2c 20 20 33 38 39   589,  583,  389
30e98 2c 20 20 34 39 30 2c 20 20 31 36 36 2c 20 20 20  ,  490,  166,   
30e99 35 38 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f  58,. /*   110 */
30e9a 20 20 20 33 37 36 2c 20 20 33 38 31 2c 20 20 33     376,  381,  3
30e9b 37 38 2c 20 20 33 37 37 2c 20 20 33 39 30 2c 20  78,  377,  390, 
30e9c 20 32 39 39 2c 20 20 36 32 32 2c 20 20 36 32 31   299,  622,  621
30e9d 2c 20 20 34 38 30 2c 20 20 20 36 37 2c 0a 20 2f  ,  480,   67,. /
30e9e 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 36 37 30  *   120 */   670
30e9f 2c 20 20 33 37 36 2c 20 20 36 32 30 2c 20 20 20  ,  376,  620,   
30ea0 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20  56,   57,   47, 
30ea1 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32   581,  580,  582
30ea2 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20 31 33  ,  582,. /*   13
30ea3 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 20 35 34  0 */    54,   54
30ea4 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30ea5 35 35 2c 20 20 20 35 35 2c 20 20 32 35 33 2c 20  55,   55,  253, 
30ea6 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30ea7 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20  ,. /*   140 */  
30ea8 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32    53,   52,   52
30ea9 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
30eaa 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20  51,   50,  237, 
30eab 20 33 31 32 2c 20 20 34 30 38 2c 0a 20 2f 2a 20   312,  408,. /* 
30eac 20 20 31 35 30 20 2a 2f 20 20 20 32 32 35 2c 20    150 */   225, 
30ead 20 35 37 38 2c 20 20 35 37 38 2c 20 20 31 33 33   578,  578,  133
30eae 2c 20 20 31 37 37 2c 20 20 31 33 39 2c 20 20 32  ,  177,  139,  2
30eaf 38 33 2c 20 20 33 38 34 2c 20 20 32 37 38 2c 20  83,  384,  278, 
30eb0 20 33 38 33 2c 0a 20 2f 2a 20 20 20 31 36 30 20   383,. /*   160 
30eb1 2a 2f 20 20 20 31 36 39 2c 20 20 36 31 39 2c 20  */   169,  619, 
30eb2 20 36 31 38 2c 20 20 36 30 31 2c 20 20 31 39 37   618,  601,  197
30eb3 2c 20 20 32 32 35 2c 20 20 32 37 34 2c 20 20 36  ,  225,  274,  6
30eb4 30 32 2c 20 20 34 33 39 2c 20 20 31 34 36 2c 0a  02,  439,  146,.
30eb5 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 31   /*   170 */   1
30eb6 33 39 2c 20 20 32 38 33 2c 20 20 33 38 34 2c 20  39,  283,  384, 
30eb7 20 32 37 38 2c 20 20 33 38 33 2c 20 20 31 36 39   278,  383,  169
30eb8 2c 20 20 35 36 39 2c 20 20 32 33 35 2c 20 20 35  ,  569,  235,  5
30eb9 38 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20 20  89,  583,. /*   
30eba 31 38 30 20 2a 2f 20 20 20 32 35 30 2c 20 20 32  180 */   250,  2
30ebb 37 34 2c 20 20 32 35 32 2c 20 20 36 32 30 2c 20  74,  252,  620, 
30ebc 20 36 31 39 2c 20 20 36 31 38 2c 20 20 35 34 36   619,  618,  546
30ebd 2c 20 20 34 33 36 2c 20 20 34 34 30 2c 20 20 34  ,  436,  440,  4
30ebe 34 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f  41,. /*   190 */
30ebf 20 20 20 31 36 38 2c 20 20 36 32 32 2c 20 20 36     168,  622,  6
30ec0 32 31 2c 20 20 35 34 37 2c 20 20 34 33 38 2c 20  21,  547,  438, 
30ec1 20 34 33 37 2c 20 20 31 39 32 2c 20 20 20 35 36   437,  192,   56
30ec2 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 0a 20 2f  ,   57,   47,. /
30ec3 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 35 38 31  *   200 */   581
30ec4 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35  ,  580,  582,  5
30ec5 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20  82,   54,   54, 
30ec6 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
30ec7 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 32 31  ,   55,. /*   21
30ec8 30 20 2a 2f 20 20 20 20 20 36 2c 20 20 20 35 33  0 */     6,   53
30ec9 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30eca 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20  53,   52,   52, 
30ecb 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31    51,   51,   51
30ecc 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20  ,. /*   220 */  
30ecd 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32    50,  237,  312
30ece 2c 20 20 32 38 32 2c 20 20 20 35 32 2c 20 20 20  ,  282,   52,   
30ecf 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  52,   51,   51, 
30ed0 20 20 35 31 2c 20 20 20 35 30 2c 0a 20 2f 2a 20    51,   50,. /* 
30ed1 20 20 32 33 30 20 2a 2f 20 20 20 32 33 37 2c 20    230 */   237, 
30ed2 20 34 39 30 2c 20 20 31 38 33 2c 20 20 32 38 31   490,  183,  281
30ed3 2c 20 20 35 34 37 2c 20 20 31 36 36 2c 20 20 34  ,  547,  166,  4
30ed4 33 39 2c 20 20 35 36 35 2c 20 20 33 38 31 2c 20  39,  565,  381, 
30ed5 20 33 37 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20   378,. /*   240 
30ed6 2a 2f 20 20 20 33 37 37 2c 20 20 35 39 36 2c 20  */   377,  596, 
30ed7 20 36 30 36 2c 20 20 20 36 37 2c 20 20 33 32 37   606,   67,  327
30ed8 2c 20 20 31 37 32 2c 20 20 36 32 30 2c 20 20 35  ,  172,  620,  5
30ed9 39 36 2c 20 20 33 37 36 2c 20 20 34 34 32 2c 0a  96,  376,  442,.
30eda 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 32   /*   250 */   2
30edb 33 36 2c 20 20 36 32 30 2c 20 20 35 38 39 2c 20  36,  620,  589, 
30edc 20 35 38 33 2c 20 20 33 30 36 2c 20 20 34 32 33   583,  306,  423
30edd 2c 20 20 34 34 30 2c 20 20 33 33 39 2c 20 20 32  ,  440,  339,  2
30ede 35 31 2c 20 20 36 31 39 2c 0a 20 2f 2a 20 20 20  51,  619,. /*   
30edf 32 36 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 33  260 */   618,  3
30ee0 33 31 2c 20 20 35 37 34 2c 20 20 35 37 33 2c 20  31,  574,  573, 
30ee1 20 20 20 37 2c 20 20 35 32 34 2c 20 20 31 39 34     7,  524,  194
30ee2 2c 20 20 34 38 31 2c 20 20 20 31 36 2c 20 20 35  ,  481,   16,  5
30ee3 39 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f  94,. /*   270 */
30ee4 20 20 20 31 38 39 2c 20 20 20 35 36 2c 20 20 20     189,   56,   
30ee5 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20  57,   47,  581, 
30ee6 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32   580,  582,  582
30ee7 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f  ,   54,   54,. /
30ee8 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 20 35 35  *   280 */    55
30ee9 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30eea 35 35 2c 20 20 35 34 35 2c 20 20 20 35 33 2c 20  55,  545,   53, 
30eeb 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
30eec 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20 32 39  ,   52,. /*   29
30eed 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35 31  0 */    52,   51
30eee 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
30eef 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20  50,  237,  312, 
30ef0 20 34 31 30 2c 20 20 34 36 34 2c 20 20 34 32 31   410,  464,  421
30ef1 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20  ,. /*   300 */  
30ef2 20 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39 32   592,  592,  592
30ef3 2c 20 20 36 37 31 2c 20 20 31 34 36 2c 20 20 34  ,  671,  146,  4
30ef4 31 30 2c 20 20 20 20 31 2c 20 20 32 30 35 2c 20  10,    1,  205, 
30ef5 20 34 31 30 2c 20 20 35 39 36 2c 0a 20 2f 2a 20   410,  596,. /* 
30ef6 20 20 33 31 30 20 2a 2f 20 20 20 36 32 32 2c 20    310 */   622, 
30ef7 20 36 32 31 2c 20 20 34 31 33 2c 20 20 34 32 30   621,  413,  420
30ef8 2c 20 20 39 34 39 2c 20 20 35 39 36 2c 20 20 39  ,  949,  596,  9
30ef9 34 39 2c 20 20 33 34 30 2c 20 20 32 33 36 2c 20  49,  340,  236, 
30efa 20 35 33 30 2c 0a 20 2f 2a 20 20 20 33 32 30 20   530,. /*   320 
30efb 2a 2f 20 20 20 34 31 33 2c 20 20 36 30 30 2c 20  */   413,  600, 
30efc 20 20 37 34 2c 20 20 34 31 33 2c 20 20 32 33 36    74,  413,  236
30efd 2c 20 20 35 35 32 2c 20 20 35 38 39 2c 20 20 35  ,  552,  589,  5
30efe 38 33 2c 20 20 35 34 37 2c 20 20 36 30 30 2c 0a  83,  547,  600,.
30eff 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 20   /*   330 */    
30f00 39 35 2c 20 20 20 36 38 2c 20 20 36 30 30 2c 20  95,   68,  600, 
30f01 20 20 38 38 2c 20 20 35 35 31 2c 20 20 36 32 32    88,  551,  622
30f02 2c 20 20 36 32 31 2c 20 20 34 36 35 2c 20 20 35  ,  621,  465,  5
30f03 34 32 2c 20 20 20 33 38 2c 0a 20 2f 2a 20 20 20  42,   38,. /*   
30f04 33 34 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 35  340 */    49,  5
30f05 39 39 2c 20 20 20 34 36 2c 20 20 31 34 37 2c 20  99,   46,  147, 
30f06 20 34 36 35 2c 20 20 20 35 36 2c 20 20 20 35 37   465,   56,   57
30f07 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35  ,   47,  581,  5
30f08 38 30 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f  80,. /*   350 */
30f09 20 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20     582,  582,   
30f0a 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20  54,   54,   55, 
30f0b 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
30f0c 2c 20 20 34 32 34 2c 20 20 20 35 33 2c 0a 20 2f  ,  424,   53,. /
30f0d 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 20 35 33  *   360 */    53
30f0e 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30f0f 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20  52,   52,   51, 
30f10 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30    51,   51,   50
30f11 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20 20 33 37  ,  237,. /*   37
30f12 30 20 2a 2f 20 20 20 33 31 32 2c 20 20 33 39 37  0 */   312,  397
30f13 2c 20 20 33 39 35 2c 20 20 32 33 32 2c 20 20 35  ,  395,  232,  5
30f14 32 39 2c 20 20 35 37 37 2c 20 20 33 38 37 2c 20  29,  577,  387, 
30f15 20 35 33 33 2c 20 20 36 31 39 2c 20 20 36 31 38   533,  619,  618
30f16 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20  ,. /*   380 */  
30f17 20 36 30 35 2c 20 20 34 39 32 2c 20 20 35 36 30   605,  492,  560
30f18 2c 20 20 35 38 38 2c 20 20 35 38 37 2c 20 20 33  ,  588,  587,  3
30f19 35 30 2c 20 20 32 35 37 2c 20 20 36 32 32 2c 20  50,  257,  622, 
30f1a 20 36 32 31 2c 20 20 34 39 35 2c 0a 20 2f 2a 20   621,  495,. /* 
30f1b 20 20 33 39 30 20 2a 2f 20 20 20 35 36 34 2c 20    390 */   564, 
30f1c 20 33 35 36 2c 20 20 33 35 30 2c 20 20 32 35 37   356,  350,  257
30f1d 2c 20 20 20 34 39 2c 20 20 32 33 39 2c 20 20 20  ,   49,  239,   
30f1e 34 36 2c 20 20 31 34 37 2c 20 20 35 35 39 2c 20  46,  147,  559, 
30f1f 20 33 35 37 2c 0a 20 2f 2a 20 20 20 34 30 30 20   357,. /*   400 
30f20 2a 2f 20 20 20 35 38 39 2c 20 20 35 38 33 2c 20  */   589,  583, 
30f21 20 32 33 39 2c 20 20 36 31 39 2c 20 20 36 31 38   239,  619,  618
30f22 2c 20 20 35 38 35 2c 20 20 35 38 34 2c 20 20 34  ,  585,  584,  4
30f23 30 38 2c 20 20 32 35 38 2c 20 20 35 37 38 2c 0a  08,  258,  578,.
30f24 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 35   /*   410 */   5
30f25 37 38 2c 20 20 36 37 32 2c 20 20 32 30 39 2c 20  78,  672,  209, 
30f26 20 20 33 35 2c 20 20 35 35 38 2c 20 20 32 35 38    35,  558,  258
30f27 2c 20 20 34 30 31 2c 20 20 36 32 32 2c 20 20 36  ,  401,  622,  6
30f28 32 31 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20  21,   56,. /*   
30f29 34 32 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20  420 */    57,   
30f2a 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20  47,  581,  580, 
30f2b 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34   582,  582,   54
30f2c 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20  ,   54,   55,   
30f2d 35 35 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f  55,. /*   430 */
30f2e 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 35      55,   55,  5
30f2f 38 36 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  86,   53,   53, 
30f30 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32    53,   53,   52
30f31 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f  ,   52,   51,. /
30f32 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 20 35 31  *   440 */    51
30f33 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32  ,   51,   50,  2
30f34 33 37 2c 20 20 33 31 32 2c 20 20 35 36 30 2c 20  37,  312,  560, 
30f35 20 35 39 39 2c 20 20 34 31 30 2c 20 20 35 32 36   599,  410,  526
30f36 2c 20 20 35 33 31 2c 0a 20 2f 2a 20 20 20 34 35  ,  531,. /*   45
30f37 30 20 2a 2f 20 20 20 31 38 34 2c 20 20 35 31 34  0 */   184,  514
30f38 2c 20 20 35 31 33 2c 20 20 34 37 34 2c 20 20 33  ,  513,  474,  3
30f39 36 36 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 20  66,  619,  618, 
30f3a 20 35 37 36 2c 20 20 34 31 30 2c 20 20 20 36 35   576,  410,   65
30f3b 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20  ,. /*   460 */  
30f3c 20 31 37 36 2c 20 20 35 35 39 2c 20 20 34 31 33   176,  559,  413
30f3d 2c 20 20 34 30 38 2c 20 20 33 31 31 2c 20 20 35  ,  408,  311,  5
30f3e 37 38 2c 20 20 35 37 38 2c 20 20 35 36 37 2c 20  78,  578,  567, 
30f3f 20 34 39 31 2c 20 20 32 31 35 2c 0a 20 2f 2a 20   491,  215,. /* 
30f40 20 20 34 37 30 20 2a 2f 20 20 20 33 35 32 2c 20    470 */   352, 
30f41 20 36 30 30 2c 20 20 20 39 34 2c 20 20 34 31 33   600,   94,  413
30f42 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 34  ,  589,  583,  4
30f43 37 34 2c 20 20 35 35 38 2c 20 20 34 30 38 2c 20  74,  558,  408, 
30f44 20 35 31 38 2c 0a 20 2f 2a 20 20 20 34 38 30 20   518,. /*   480 
30f45 2a 2f 20 20 20 35 37 38 2c 20 20 35 37 38 2c 20  */   578,  578, 
30f46 20 36 30 30 2c 20 20 20 39 35 2c 20 20 34 37 30   600,   95,  470
30f47 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 20 20 20  ,  619,  618,   
30f48 36 32 2c 20 20 34 32 30 2c 20 20 39 34 38 2c 0a  62,  420,  948,.
30f49 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 35   /*   490 */   5
30f4a 31 37 2c 20 20 39 34 38 2c 20 20 33 34 39 2c 20  17,  948,  349, 
30f4b 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37    56,   57,   47
30f4c 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35  ,  581,  580,  5
30f4d 38 32 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20  82,  582,. /*   
30f4e 35 30 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 20  500 */    54,   
30f4f 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  54,   55,   55, 
30f50 20 20 35 35 2c 20 20 20 35 35 2c 20 20 31 37 35    55,   55,  175
30f51 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30f52 35 33 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f  53,. /*   510 */
30f53 20 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20      53,   52,   
30f54 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  52,   51,   51, 
30f55 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37    51,   50,  237
30f56 2c 20 20 33 31 32 2c 20 20 34 39 30 2c 0a 20 2f  ,  312,  490,. /
30f57 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 31 35 37  *   520 */   157
30f58 2c 20 20 34 31 30 2c 20 20 35 30 39 2c 20 20 32  ,  410,  509,  2
30f59 39 32 2c 20 20 33 39 33 2c 20 20 33 37 33 2c 20  92,  393,  373, 
30f5a 20 33 34 38 2c 20 20 34 31 30 2c 20 20 36 32 33   348,  410,  623
30f5b 2c 20 20 34 31 30 2c 0a 20 2f 2a 20 20 20 35 33  ,  410,. /*   53
30f5c 30 20 2a 2f 20 20 20 34 32 38 2c 20 20 20 36 37  0 */   428,   67
30f5d 2c 20 20 36 31 31 2c 20 20 34 32 34 2c 20 20 36  ,  611,  424,  6
30f5e 32 30 2c 20 20 34 31 30 2c 20 20 34 31 33 2c 20  20,  410,  413, 
30f5f 20 35 34 30 2c 20 20 34 30 38 2c 20 20 31 37 31   540,  408,  171
30f60 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20  ,. /*   540 */  
30f61 20 35 37 38 2c 20 20 35 37 38 2c 20 20 34 31 33   578,  578,  413
30f62 2c 20 20 36 32 30 2c 20 20 34 31 33 2c 20 20 36  ,  620,  413,  6
30f63 30 30 2c 20 20 20 37 33 2c 20 20 36 32 30 2c 20  00,   73,  620, 
30f64 20 35 38 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20   589,  583,. /* 
30f65 20 20 35 35 30 20 2a 2f 20 20 20 34 31 33 2c 20    550 */   413, 
30f66 20 36 30 30 2c 20 20 20 38 30 2c 20 20 36 30 30   600,   80,  600
30f67 2c 20 20 20 38 38 2c 20 20 32 33 38 2c 20 20 31  ,   88,  238,  1
30f68 36 38 2c 20 20 33 30 36 2c 20 20 34 32 32 2c 20  68,  306,  422, 
30f69 20 36 30 30 2c 0a 20 2f 2a 20 20 20 35 36 30 20   600,. /*   560 
30f6a 2a 2f 20 20 20 20 38 30 2c 20 20 32 30 31 2c 20  */    80,  201, 
30f6b 20 20 31 38 2c 20 20 34 36 38 2c 20 20 34 30 36    18,  468,  406
30f6c 2c 20 20 35 37 34 2c 20 20 35 37 33 2c 20 20 20  ,  574,  573,   
30f6d 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 0a  56,   57,   47,.
30f6e 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 35   /*   570 */   5
30f6f 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20  81,  580,  582, 
30f70 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34   582,   54,   54
30f71 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30f72 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20  55,   55,. /*   
30f73 35 38 30 20 2a 2f 20 20 20 35 37 39 2c 20 20 20  580 */   579,   
30f74 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30f75 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32    53,   52,   52
30f76 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
30f77 35 31 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f  51,. /*   590 */
30f78 20 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33      50,  237,  3
30f79 31 32 2c 20 20 20 34 34 2c 20 20 32 33 33 2c 20  12,   44,  233, 
30f7a 20 35 39 39 2c 20 20 32 37 31 2c 20 20 33 32 30   599,  271,  320
30f7b 2c 20 20 33 34 31 2c 20 20 34 37 32 2c 0a 20 2f  ,  341,  472,. /
30f7c 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 34 31 30  *   600 */   410
30f7d 2c 20 20 38 37 34 2c 20 20 34 32 31 2c 20 20 34  ,  874,  421,  4
30f7e 37 33 2c 20 20 35 30 33 2c 20 20 33 31 39 2c 20  73,  503,  319, 
30f7f 20 34 31 30 2c 20 20 32 30 30 2c 20 20 31 34 34   410,  200,  144
30f80 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 36 31  ,   66,. /*   61
30f81 30 20 2a 2f 20 20 20 33 32 37 2c 20 20 34 38 33  0 */   327,  483
30f82 2c 20 20 35 30 38 2c 20 20 35 39 36 2c 20 20 32  ,  508,  596,  2
30f83 37 34 2c 20 20 34 31 33 2c 20 20 32 33 39 2c 20  74,  413,  239, 
30f84 20 33 36 34 2c 20 20 34 38 34 2c 20 20 33 38 32   364,  484,  382
30f85 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20  ,. /*   620 */  
30f86 20 20 33 30 2c 20 20 34 31 33 2c 20 20 35 38 39    30,  413,  589
30f87 2c 20 20 35 38 33 2c 20 20 36 30 30 2c 20 20 20  ,  583,  600,   
30f88 36 39 2c 20 20 35 30 32 2c 20 20 32 33 36 2c 20  69,  502,  236, 
30f89 20 33 34 32 2c 20 20 35 37 35 2c 0a 20 2f 2a 20   342,  575,. /* 
30f8a 20 20 36 33 30 20 2a 2f 20 20 20 36 30 30 2c 20    630 */   600, 
30f8b 20 20 39 37 2c 20 20 31 39 39 2c 20 20 31 39 38    97,  199,  198
30f8c 2c 20 20 32 30 39 2c 20 20 39 35 39 2c 20 20 31  ,  209,  959,  1
30f8d 38 36 2c 20 20 34 31 38 2c 20 20 20 20 32 2c 20  86,  418,    2, 
30f8e 20 35 36 36 2c 0a 20 2f 2a 20 20 20 36 34 30 20   566,. /*   640 
30f8f 2a 2f 20 20 20 32 36 39 2c 20 20 20 35 36 2c 20  */   269,   56, 
30f90 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31    57,   47,  581
30f91 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35  ,  580,  582,  5
30f92 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 0a  82,   54,   54,.
30f93 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 20   /*   650 */    
30f94 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
30f95 20 20 35 35 2c 20 20 34 31 30 2c 20 20 20 35 33    55,  410,   53
30f96 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30f97 35 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20  53,   52,. /*   
30f98 36 36 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20  660 */    52,   
30f99 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
30f9a 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32    50,  237,  312
30f9b 2c 20 20 32 36 33 2c 20 20 35 39 39 2c 20 20 34  ,  263,  599,  4
30f9c 31 33 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f  13,. /*   670 */
30f9d 20 20 20 34 31 30 2c 20 20 20 32 31 2c 20 20 31     410,   21,  1
30f9e 39 30 2c 20 20 33 35 38 2c 20 20 34 31 30 2c 20  90,  358,  410, 
30f9f 20 33 32 36 2c 20 20 34 31 30 2c 20 20 32 30 32   326,  410,  202
30fa0 2c 20 20 36 30 30 2c 20 20 31 30 30 2c 0a 20 2f  ,  600,  100,. /
30fa1 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 33 38 36  *   680 */   386
30fa2 2c 20 20 35 39 36 2c 20 20 36 32 30 2c 20 20 35  ,  596,  620,  5
30fa3 36 32 2c 20 20 32 36 35 2c 20 20 34 31 33 2c 20  62,  265,  413, 
30fa4 20 32 36 37 2c 20 20 34 31 30 2c 20 20 36 32 30   267,  410,  620
30fa5 2c 20 20 34 31 33 2c 0a 20 2f 2a 20 20 20 36 39  ,  413,. /*   69
30fa6 30 20 2a 2f 20 20 20 35 36 33 2c 20 20 34 31 33  0 */   563,  413
30fa7 2c 20 20 33 35 32 2c 20 20 20 20 34 2c 20 20 36  ,  352,    4,  6
30fa8 30 30 2c 20 20 20 39 38 2c 20 20 35 38 39 2c 20  00,   98,  589, 
30fa9 20 35 38 33 2c 20 20 36 30 30 2c 20 20 31 30 36   583,  600,  106
30faa 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20  ,. /*   700 */  
30fab 20 36 30 30 2c 20 20 31 30 34 2c 20 20 34 31 33   600,  104,  413
30fac 2c 20 20 31 37 34 2c 20 20 36 30 31 2c 20 20 36  ,  174,  601,  6
30fad 32 39 2c 20 20 36 32 37 2c 20 20 33 33 33 2c 20  29,  627,  333, 
30fae 20 36 30 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20   602,   34,. /* 
30faf 20 20 37 31 30 20 2a 2f 20 20 20 33 33 37 2c 20    710 */   337, 
30fb0 20 36 30 30 2c 20 20 31 30 38 2c 20 20 35 36 31   600,  108,  561
30fb1 2c 20 20 33 35 39 2c 20 20 20 35 36 2c 20 20 20  ,  359,   56,   
30fb2 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20  57,   47,  581, 
30fb3 20 35 38 30 2c 0a 20 2f 2a 20 20 20 37 32 30 20   580,. /*   720 
30fb4 2a 2f 20 20 20 35 38 32 2c 20 20 35 38 32 2c 20  */   582,  582, 
30fb5 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35    54,   54,   55
30fb6 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20  ,   55,   55,   
30fb7 35 35 2c 20 20 34 31 30 2c 20 20 20 35 33 2c 0a  55,  410,   53,.
30fb8 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20 20   /*   730 */    
30fb9 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
30fba 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31    52,   52,   51
30fbb 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20  ,   51,   51,   
30fbc 35 30 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20 20  50,  237,. /*   
30fbd 37 34 30 20 2a 2f 20 20 20 33 31 32 2c 20 20 34  740 */   312,  4
30fbe 31 30 2c 20 20 34 39 39 2c 20 20 34 31 33 2c 20  10,  499,  413, 
30fbf 20 31 36 37 2c 20 20 35 36 37 2c 20 20 34 30 35   167,  567,  405
30fc0 2c 20 20 32 31 35 2c 20 20 35 30 34 2c 20 20 35  ,  215,  504,  5
30fc1 30 35 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f  05,. /*   750 */
30fc2 20 20 20 33 31 36 2c 20 20 35 35 37 2c 20 20 36     316,  557,  6
30fc3 30 30 2c 20 20 31 30 39 2c 20 20 33 35 33 2c 20  00,  109,  353, 
30fc4 20 20 31 33 2c 20 20 34 31 33 2c 20 20 34 31 30    13,  413,  410
30fc5 2c 20 20 20 31 32 2c 20 20 34 31 30 2c 0a 20 2f  ,   12,  410,. /
30fc6 2a 20 20 20 37 36 30 20 2a 2f 20 20 20 35 33 38  *   760 */   538
30fc7 2c 20 20 34 31 30 2c 20 20 33 33 35 2c 20 20 33  ,  410,  335,  3
30fc8 35 38 2c 20 20 32 32 33 2c 20 20 36 30 30 2c 20  58,  223,  600, 
30fc9 20 31 33 34 2c 20 20 35 37 31 2c 20 20 35 37 31   134,  571,  571
30fca 2c 20 20 36 32 30 2c 0a 20 2f 2a 20 20 20 37 37  ,  620,. /*   77
30fcb 30 20 2a 2f 20 20 20 35 38 39 2c 20 20 35 38 33  0 */   589,  583
30fcc 2c 20 20 34 31 33 2c 20 20 20 32 30 2c 20 20 34  ,  413,   20,  4
30fcd 31 33 2c 20 20 36 32 30 2c 20 20 34 31 33 2c 20  13,  620,  413, 
30fce 20 32 37 32 2c 20 20 36 32 30 2c 20 20 31 36 37   272,  620,  167
30fcf 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20  ,. /*   780 */  
30fd0 20 31 36 37 2c 20 20 36 30 30 2c 20 20 31 33 35   167,  600,  135
30fd1 2c 20 20 36 30 30 2c 20 20 20 36 31 2c 20 20 36  ,  600,   61,  6
30fd2 30 30 2c 20 20 31 30 35 2c 20 20 33 31 37 2c 20  00,  105,  317, 
30fd3 20 31 34 38 2c 20 20 20 35 36 2c 0a 20 2f 2a 20   148,   56,. /* 
30fd4 20 20 37 39 30 20 2a 2f 20 20 20 20 35 37 2c 20    790 */    57, 
30fd5 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30    47,  581,  580
30fd6 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20  ,  582,  582,   
30fd7 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20  54,   54,   55, 
30fd8 20 20 35 35 2c 0a 20 2f 2a 20 20 20 38 30 30 20    55,. /*   800 
30fd9 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35 2c 20  */    55,   55, 
30fda 20 34 31 30 2c 20 20 20 35 33 2c 20 20 20 35 33   410,   53,   53
30fdb 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20  ,   53,   53,   
30fdc 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 0a  52,   52,   51,.
30fdd 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20   /*   810 */    
30fde 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20  51,   51,   50, 
30fdf 20 32 33 37 2c 20 20 33 31 32 2c 20 20 34 31 30   237,  312,  410
30fe0 2c 20 20 32 37 35 2c 20 20 34 31 33 2c 20 20 34  ,  275,  413,  4
30fe1 31 30 2c 20 20 32 37 35 2c 0a 20 2f 2a 20 20 20  10,  275,. /*   
30fe2 38 32 30 20 2a 2f 20 20 20 32 37 35 2c 20 20 32  820 */   275,  2
30fe3 32 32 2c 20 20 34 31 30 2c 20 20 33 33 30 2c 20  22,  410,  330, 
30fe4 20 33 36 33 2c 20 20 35 34 34 2c 20 20 36 30 30   363,  544,  600
30fe5 2c 20 20 31 30 33 2c 20 20 31 33 32 2c 20 20 33  ,  103,  132,  3
30fe6 36 30 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f  60,. /*   830 */
30fe7 20 20 20 34 31 33 2c 20 20 36 32 30 2c 20 20 35     413,  620,  5
30fe8 32 32 2c 20 20 34 31 33 2c 20 20 36 32 30 2c 20  22,  413,  620, 
30fe9 20 36 32 30 2c 20 20 34 31 30 2c 20 20 34 31 33   620,  410,  413
30fea 2c 20 20 31 37 30 2c 20 20 36 30 30 2c 0a 20 2f  ,  170,  600,. /
30feb 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 20 39 36  *   840 */    96
30fec 2c 20 20 36 30 33 2c 20 20 36 30 30 2c 20 20 31  ,  603,  600,  1
30fed 30 32 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 20  02,  589,  583, 
30fee 20 36 30 30 2c 20 20 20 37 37 2c 20 20 33 37 34   600,   77,  374
30fef 2c 20 20 35 33 36 2c 0a 20 2f 2a 20 20 20 38 35  ,  536,. /*   85
30ff0 30 20 2a 2f 20 20 20 31 36 37 2c 20 20 34 31 33  0 */   167,  413
30ff1 2c 20 20 31 34 33 2c 20 20 33 32 35 2c 20 20 32  ,  143,  325,  2
30ff2 35 36 2c 20 20 20 32 38 2c 20 20 32 32 34 2c 20  56,   28,  224, 
30ff3 20 33 32 34 2c 20 20 35 31 31 2c 20 20 35 32 38   324,  511,  528
30ff4 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20  ,. /*   860 */  
30ff5 20 36 30 30 2c 20 20 20 39 39 2c 20 20 35 32 37   600,   99,  527
30ff6 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20 20  ,   56,   57,   
30ff7 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20  47,  581,  580, 
30ff8 20 35 38 32 2c 20 20 35 38 32 2c 0a 20 2f 2a 20   582,  582,. /* 
30ff9 20 20 38 37 30 20 2a 2f 20 20 20 20 35 34 2c 20    870 */    54, 
30ffa 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35    54,   55,   55
30ffb 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34  ,   55,   55,  4
30ffc 31 30 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  10,   53,   53, 
30ffd 20 20 35 33 2c 0a 20 2f 2a 20 20 20 38 38 30 20    53,. /*   880 
30ffe 2a 2f 20 20 20 20 35 33 2c 20 20 20 35 32 2c 20  */    53,   52, 
30fff 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31    52,   51,   51
31000 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32  ,   51,   50,  2
31001 33 37 2c 20 20 33 31 32 2c 20 20 34 31 30 2c 0a  37,  312,  410,.
31002 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20 32   /*   890 */   2
31003 37 35 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20  75,  413,  410, 
31004 20 34 36 39 2c 20 20 32 37 35 2c 20 20 31 36 37   469,  275,  167
31005 2c 20 20 34 35 38 2c 20 20 20 33 39 2c 20 20 31  ,  458,   39,  1
31006 37 31 2c 20 20 20 33 37 2c 0a 20 2f 2a 20 20 20  71,   37,. /*   
31007 39 30 30 20 2a 2f 20 20 20 36 30 30 2c 20 20 31  900 */   600,  1
31008 33 38 2c 20 20 32 31 34 2c 20 20 31 34 34 2c 20  38,  214,  144, 
31009 20 34 31 33 2c 20 20 36 32 30 2c 20 20 31 34 32   413,  620,  142
3100a 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 36  ,  413,  410,  6
3100b 32 30 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f  20,. /*   910 */
3100c 20 20 20 34 31 30 2c 20 20 33 35 38 2c 20 20 32     410,  358,  2
3100d 33 39 2c 20 20 36 30 30 2c 20 20 31 33 37 2c 20  39,  600,  137, 
3100e 20 32 33 30 2c 20 20 36 30 30 2c 20 20 31 33 36   230,  600,  136
3100f 2c 20 20 35 38 39 2c 20 20 35 38 33 2c 0a 20 2f  ,  589,  583,. /
31010 2a 20 20 20 39 32 30 20 2a 2f 20 20 20 34 35 37  *   920 */   457
31011 2c 20 20 32 36 33 2c 20 20 20 32 33 2c 20 20 34  ,  263,   23,  4
31012 31 33 2c 20 20 33 35 31 2c 20 20 34 31 33 2c 20  13,  351,  413, 
31013 20 36 32 30 2c 20 20 33 32 33 2c 20 20 34 34 35   620,  323,  445
31014 2c 20 20 35 30 31 2c 0a 20 2f 2a 20 20 20 39 33  ,  501,. /*   93
31015 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 33 32 32  0 */    23,  322
31016 2c 20 20 36 30 30 2c 20 20 20 37 36 2c 20 20 36  ,  600,   76,  6
31017 30 30 2c 20 20 20 39 33 2c 20 20 36 32 30 2c 20  00,   93,  620, 
31018 20 20 35 36 2c 20 20 20 34 35 2c 20 20 20 34 37    56,   45,   47
31019 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20  ,. /*   940 */  
3101a 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32   581,  580,  582
3101b 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20  ,  582,   54,   
3101c 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  54,   55,   55, 
3101d 20 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20    55,   55,. /* 
3101e 20 20 39 35 30 20 2a 2f 20 20 20 34 31 30 2c 20    950 */   410, 
3101f 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
31020 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20  ,   53,   52,   
31021 35 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  52,   51,   51, 
31022 20 20 35 31 2c 0a 20 2f 2a 20 20 20 39 36 30 20    51,. /*   960 
31023 2a 2f 20 20 20 20 35 30 2c 20 20 32 33 37 2c 20  */    50,  237, 
31024 20 33 31 32 2c 20 20 34 31 30 2c 20 20 32 36 32   312,  410,  262
31025 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 34  ,  413,  410,  4
31026 32 36 2c 20 20 32 36 33 2c 20 20 33 30 38 2c 0a  26,  263,  308,.
31027 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 32   /*   970 */   2
31028 30 33 2c 20 20 32 31 33 2c 20 20 32 31 32 2c 20  03,  213,  212, 
31029 20 33 38 30 2c 20 20 36 30 30 2c 20 20 20 39 32   380,  600,   92
3102a 2c 20 20 35 32 30 2c 20 20 35 31 39 2c 20 20 34  ,  520,  519,  4
3102b 31 33 2c 20 20 31 33 30 2c 0a 20 2f 2a 20 20 20  13,  130,. /*   
3102c 39 38 30 20 2a 2f 20 20 20 35 33 38 2c 20 20 34  980 */   538,  4
3102d 31 33 2c 20 20 35 33 38 2c 20 20 36 32 30 2c 20  13,  538,  620, 
3102e 20 34 31 30 2c 20 20 36 32 38 2c 20 20 20 20 32   410,  628,    2
3102f 2c 20 20 36 30 30 2c 20 20 20 37 35 2c 20 20 32  ,  600,   75,  2
31030 37 33 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f  73,. /*   990 */
31031 20 20 20 36 30 30 2c 20 20 20 39 31 2c 20 20 35     600,   91,  5
31032 38 39 2c 20 20 35 38 33 2c 20 20 33 37 35 2c 20  89,  583,  375, 
31033 20 36 32 30 2c 20 20 31 32 39 2c 20 20 36 32 30   620,  129,  620
31034 2c 20 20 20 32 37 2c 20 20 34 31 33 2c 0a 20 2f  ,   27,  413,. /
31035 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 34 32 35  *  1000 */   425
31036 2c 20 20 33 30 37 2c 20 20 32 32 31 2c 20 20 31  ,  307,  221,  1
31037 32 38 2c 20 20 35 39 39 2c 20 20 35 39 39 2c 20  28,  599,  599, 
31038 20 35 39 39 2c 20 20 32 38 31 2c 20 20 36 30 30   599,  281,  600
31039 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 31 30 31  ,   90,. /*  101
3103a 30 20 2a 2f 20 20 20 33 37 31 2c 20 20 34 35 32  0 */   371,  452
3103b 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35  ,   57,   47,  5
3103c 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20  81,  580,  582, 
3103d 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34   582,   54,   54
3103e 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20  ,. /*  1020 */  
3103f 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35    55,   55,   55
31040 2c 20 20 20 35 35 2c 20 20 34 31 30 2c 20 20 20  ,   55,  410,   
31041 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20  53,   53,   53, 
31042 20 20 35 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20    53,   52,. /* 
31043 20 31 30 33 30 20 2a 2f 20 20 20 20 35 32 2c 20   1030 */    52, 
31044 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31    51,   51,   51
31045 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33  ,   50,  237,  3
31046 31 32 2c 20 20 34 31 30 2c 20 20 32 36 33 2c 20  12,  410,  263, 
31047 20 34 31 33 2c 0a 20 2f 2a 20 20 31 30 34 30 20   413,. /*  1040 
31048 2a 2f 20 20 20 34 31 30 2c 20 20 32 36 33 2c 20  */   410,  263, 
31049 20 32 36 33 2c 20 20 33 36 35 2c 20 20 32 30 38   263,  365,  208
3104a 2c 20 20 33 32 31 2c 20 20 32 30 36 2c 20 20 35  ,  321,  206,  5
3104b 34 32 2c 20 20 36 30 30 2c 20 20 31 30 31 2c 0a  42,  600,  101,.
3104c 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20 20   /*  1050 */    
3104d 35 30 2c 20 20 32 33 37 2c 20 20 34 31 33 2c 20  50,  237,  413, 
3104e 20 36 32 30 2c 20 20 36 31 30 2c 20 20 34 31 33   620,  610,  413
3104f 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20 34  ,  620,  620,  4
31050 31 30 2c 20 20 35 34 32 2c 0a 20 2f 2a 20 20 31  10,  542,. /*  1
31051 30 36 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 36  060 */   165,  6
31052 30 30 2c 20 20 20 38 39 2c 20 20 31 38 38 2c 20  00,   89,  188, 
31053 20 36 30 30 2c 20 20 20 38 37 2c 20 20 35 38 39   600,   87,  589
31054 2c 20 20 35 38 33 2c 20 20 34 37 38 2c 20 20 36  ,  583,  478,  6
31055 32 30 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f  20,. /*  1070 */
31056 20 20 20 34 36 37 2c 20 20 35 31 39 2c 20 20 31     467,  519,  1
31057 32 35 2c 20 20 34 31 33 2c 20 20 35 36 39 2c 20  25,  413,  569, 
31058 20 32 33 35 2c 20 20 35 34 32 2c 20 20 33 36 37   235,  542,  367
31059 2c 20 20 35 39 39 2c 20 20 34 37 35 2c 0a 20 2f  ,  599,  475,. /
3105a 2a 20 20 31 30 38 30 20 2a 2f 20 20 20 35 39 39  *  1080 */   599
3105b 2c 20 20 34 35 30 2c 20 20 36 30 30 2c 20 20 20  ,  450,  600,   
3105c 38 36 2c 20 20 34 34 39 2c 20 20 34 34 38 2c 20  86,  449,  448, 
3105d 20 32 33 31 2c 20 20 20 34 37 2c 20 20 35 38 31   231,   47,  581
3105e 2c 20 20 35 38 30 2c 0a 20 2f 2a 20 20 31 30 39  ,  580,. /*  109
3105f 30 20 2a 2f 20 20 20 35 38 32 2c 20 20 35 38 32  0 */   582,  582
31060 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20  ,   54,   54,   
31061 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20  55,   55,   55, 
31062 20 20 35 35 2c 20 20 32 38 37 2c 20 20 20 35 33    55,  287,   53
31063 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20  ,. /*  1100 */  
31064 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33    53,   53,   53
31065 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20  ,   52,   52,   
31066 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20  51,   51,   51, 
31067 20 20 35 30 2c 20 20 32 33 37 2c 0a 20 2f 2a 20    50,  237,. /* 
31068 20 31 31 31 30 20 2a 2f 20 20 20 20 34 33 2c 20   1110 */    43, 
31069 20 34 30 34 2c 20 20 34 31 30 2c 20 20 20 20 33   404,  410,    3
3106a 2c 20 20 34 31 30 2c 20 20 32 38 35 2c 20 20 32  ,  410,  285,  2
3106b 36 30 2c 20 20 34 31 34 2c 20 20 36 32 31 2c 20  60,  414,  621, 
3106c 20 32 36 33 2c 0a 20 2f 2a 20 20 31 31 32 30 20   263,. /*  1120 
3106d 2a 2f 20 20 20 36 30 39 2c 20 20 36 32 37 2c 20  */   609,  627, 
3106e 20 33 33 33 2c 20 20 20 34 33 2c 20 20 34 30 34   333,   43,  404
3106f 2c 20 20 34 31 30 2c 20 20 20 20 33 2c 20 20 34  ,  410,    3,  4
31070 31 33 2c 20 20 34 30 37 2c 20 20 34 31 33 2c 0a  13,  407,  413,.
31071 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 34   /*  1130 */   4
31072 31 34 2c 20 20 36 32 31 2c 20 20 31 37 31 2c 20  14,  621,  171, 
31073 20 32 36 33 2c 20 20 36 32 30 2c 20 20 36 32 30   263,  620,  620
31074 2c 20 20 36 30 30 2c 20 20 20 38 35 2c 20 20 36  ,  600,   85,  6
31075 30 30 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31  00,   72,. /*  1
31076 31 34 30 20 2a 2f 20 20 20 34 31 33 2c 20 20 34  140 */   413,  4
31077 30 37 2c 20 20 31 32 34 2c 20 20 31 34 30 2c 20  07,  124,  140, 
31078 20 33 35 33 2c 20 20 36 30 34 2c 20 20 34 30 39   353,  604,  409
31079 2c 20 20 34 30 32 2c 20 20 36 32 30 2c 20 20 36  ,  402,  620,  6
3107a 30 30 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f  00,. /*  1150 */
3107b 20 20 20 20 37 31 2c 20 20 32 39 31 2c 20 20 34      71,  291,  4
3107c 37 31 2c 20 20 34 39 35 2c 20 20 31 36 30 2c 20  71,  495,  160, 
3107d 20 31 32 33 2c 20 20 35 39 33 2c 20 20 35 36 35   123,  593,  565
3107e 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 0a 20 2f  ,  620,  620,. /
3107f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 34 30 32  *  1160 */   402
31080 2c 20 20 36 32 30 2c 20 20 32 32 30 2c 20 20 20  ,  620,  220,   
31081 31 35 2c 20 20 34 36 33 2c 20 20 34 36 30 2c 20  15,  463,  460, 
31082 20 36 32 30 2c 20 20 34 31 37 2c 20 20 36 32 35   620,  417,  625
31083 2c 20 20 31 35 39 2c 0a 20 2f 2a 20 20 31 31 37  ,  159,. /*  117
31084 30 20 2a 2f 20 20 20 35 36 35 2c 20 20 36 32 30  0 */   565,  620
31085 2c 20 20 33 39 39 2c 20 20 32 34 30 2c 20 20 31  ,  399,  240,  1
31086 35 38 2c 20 20 31 32 36 2c 20 20 32 31 39 2c 20  58,  126,  219, 
31087 20 20 34 30 2c 20 20 20 34 31 2c 20 20 35 33 32    40,   41,  532
31088 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20  ,. /*  1180 */  
31089 20 34 31 30 2c 20 20 32 30 37 2c 20 20 31 32 31   410,  207,  121
3108a 2c 20 20 31 32 30 2c 20 20 20 34 32 2c 20 20 34  ,  120,   42,  4
3108b 31 32 2c 20 20 34 31 31 2c 20 20 36 32 30 2c 20  12,  411,  620, 
3108c 20 32 36 33 2c 20 20 35 39 34 2c 0a 20 2f 2a 20   263,  594,. /* 
3108d 20 31 31 39 30 20 2a 2f 20 20 20 20 34 30 2c 20   1190 */    40, 
3108e 20 20 34 31 2c 20 20 35 35 36 2c 20 20 35 34 33    41,  556,  543
3108f 2c 20 20 20 32 35 2c 20 20 34 31 33 2c 20 20 20  ,   25,  413,   
31090 31 31 2c 20 20 20 34 32 2c 20 20 34 31 32 2c 20  11,   42,  412, 
31091 20 34 31 31 2c 0a 20 2f 2a 20 20 31 32 30 30 20   411,. /*  1200 
31092 2a 2f 20 20 20 20 32 34 2c 20 20 31 31 38 2c 20  */    24,  118, 
31093 20 35 39 34 2c 20 20 36 32 30 2c 20 20 36 30 30   594,  620,  600
31094 2c 20 20 20 38 34 2c 20 20 34 35 35 2c 20 20 36  ,   84,  455,  6
31095 32 30 2c 20 20 36 32 30 2c 20 20 20 34 33 2c 0a  20,  620,   43,.
31096 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20 34   /*  1210 */   4
31097 30 34 2c 20 20 32 31 38 2c 20 20 20 20 33 2c 20  04,  218,    3, 
31098 20 35 33 39 2c 20 20 31 35 36 2c 20 20 35 39 39   539,  156,  599
31099 2c 20 20 34 31 34 2c 20 20 36 32 31 2c 20 20 31  ,  414,  621,  1
3109a 31 33 2c 20 20 32 33 39 2c 0a 20 2f 2a 20 20 31  13,  239,. /*  1
3109b 32 32 30 20 2a 2f 20 20 20 35 39 32 2c 20 20 35  220 */   592,  5
3109c 39 32 2c 20 20 35 39 32 2c 20 20 35 39 31 2c 20  92,  592,  591, 
3109d 20 35 39 30 2c 20 20 20 31 34 2c 20 20 31 35 35   590,   14,  155
3109e 2c 20 20 34 30 37 2c 20 20 36 32 30 2c 20 20 35  ,  407,  620,  5
3109f 33 37 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f  37,. /*  1230 */
310a0 20 20 20 34 35 31 2c 20 20 32 34 37 2c 20 20 34     451,  247,  4
310a1 34 34 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20  44,  592,  592, 
310a2 20 35 39 32 2c 20 20 35 39 31 2c 20 20 35 39 30   592,  591,  590
310a3 2c 20 20 20 31 34 2c 20 20 33 34 33 2c 0a 20 2f  ,   14,  343,. /
310a4 2a 20 20 31 32 34 30 20 2a 2f 20 20 20 34 31 30  *  1240 */   410
310a5 2c 20 20 31 31 31 2c 20 20 34 31 30 2c 20 20 32  ,  111,  410,  2
310a6 37 37 2c 20 20 36 32 30 2c 20 20 34 31 30 2c 20  77,  620,  410, 
310a7 20 34 30 32 2c 20 20 34 31 30 2c 20 20 35 30 37   402,  410,  507
310a8 2c 20 20 31 31 30 2c 0a 20 2f 2a 20 20 31 32 35  ,  110,. /*  125
310a9 30 20 2a 2f 20 20 20 20 31 30 2c 20 20 20 36 34  0 */    10,   64
310aa 2c 20 20 32 30 34 2c 20 20 33 33 36 2c 20 20 34  ,  204,  336,  4
310ab 33 35 2c 20 20 34 31 33 2c 20 20 35 36 35 2c 20  35,  413,  565, 
310ac 20 34 31 33 2c 20 20 36 32 30 2c 20 20 32 37 36   413,  620,  276
310ad 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20  ,. /*  1260 */  
310ae 20 34 31 33 2c 20 20 34 33 34 2c 20 20 34 31 33   413,  434,  413
310af 2c 20 20 36 32 30 2c 20 20 36 30 30 2c 20 20 20  ,  620,  600,   
310b0 38 33 2c 20 20 36 30 30 2c 20 20 20 39 35 2c 20  83,  600,   95, 
310b1 20 33 33 34 2c 20 20 36 30 30 2c 0a 20 2f 2a 20   334,  600,. /* 
310b2 20 31 32 37 30 20 2a 2f 20 20 20 20 38 32 2c 20   1270 */    82, 
310b3 20 36 30 30 2c 20 20 20 38 31 2c 20 20 31 35 30   600,   81,  150
310b4 2c 20 20 36 32 30 2c 20 20 34 38 38 2c 20 20 20  ,  620,  488,   
310b5 34 30 2c 20 20 20 34 31 2c 20 20 32 37 30 2c 20  40,   41,  270, 
310b6 20 32 36 38 2c 0a 20 2f 2a 20 20 31 32 38 30 20   268,. /*  1280 
310b7 2a 2f 20 20 20 32 36 36 2c 20 20 31 39 31 2c 20  */   266,  191, 
310b8 20 33 33 32 2c 20 20 20 34 32 2c 20 20 34 31 32   332,   42,  412
310b9 2c 20 20 34 31 31 2c 20 20 35 39 39 2c 20 20 34  ,  411,  599,  4
310ba 31 30 2c 20 20 35 39 34 2c 20 20 32 34 31 2c 0a  10,  594,  241,.
310bb 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 36   /*  1290 */   6
310bc 32 30 2c 20 20 34 31 30 2c 20 20 32 36 34 2c 20  20,  410,  264, 
310bd 20 36 32 30 2c 20 20 36 32 30 2c 20 20 36 32 30   620,  620,  620
310be 2c 20 20 20 33 33 2c 20 20 34 30 34 2c 20 20 34  ,   33,  404,  4
310bf 31 39 2c 20 20 20 20 33 2c 0a 20 2f 2a 20 20 31  19,    3,. /*  1
310c0 33 30 30 20 2a 2f 20 20 20 31 30 37 2c 20 20 32  300 */   107,  2
310c1 32 39 2c 20 20 34 31 33 2c 20 20 34 31 34 2c 20  29,  413,  414, 
310c2 20 36 32 31 2c 20 20 31 34 39 2c 20 20 34 31 33   621,  149,  413
310c3 2c 20 20 36 32 30 2c 20 20 33 39 37 2c 20 20 31  ,  620,  397,  1
310c4 38 31 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f  81,. /*  1310 */
310c5 20 20 20 32 35 39 2c 20 20 36 30 30 2c 20 20 20     259,  600,   
310c6 37 30 2c 20 20 33 39 38 2c 20 20 34 30 37 2c 20  70,  398,  407, 
310c7 20 36 30 30 2c 20 20 20 31 37 2c 20 20 33 31 35   600,   17,  315
310c8 2c 20 20 33 31 34 2c 20 20 35 39 32 2c 0a 20 2f  ,  314,  592,. /
310c9 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 35 39 32  *  1320 */   592
310ca 2c 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20 35  ,  592,  591,  5
310cb 39 30 2c 20 20 20 31 34 2c 20 20 36 32 30 2c 20  90,   14,  620, 
310cc 20 31 32 37 2c 20 20 33 36 31 2c 20 20 36 32 34   127,  361,  624
310cd 2c 20 20 32 31 37 2c 0a 20 2f 2a 20 20 31 33 33  ,  217,. /*  133
310ce 30 20 2a 2f 20 20 20 34 36 32 2c 20 20 34 36 31  0 */   462,  461
310cf 2c 20 20 33 35 34 2c 20 20 34 30 32 2c 20 20 33  ,  354,  402,  3
310d0 30 34 2c 20 20 33 30 33 2c 20 20 33 30 32 2c 20  04,  303,  302, 
310d1 20 31 37 39 2c 20 20 33 30 30 2c 20 20 32 35 34   179,  300,  254
310d2 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20  ,. /*  1340 */  
310d3 20 36 31 34 2c 20 20 34 35 33 2c 20 20 36 32 30   614,  453,  620
310d4 2c 20 20 35 36 35 2c 20 20 34 35 34 2c 20 20 36  ,  565,  454,  6
310d5 32 30 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20  20,  620,  620, 
310d6 20 36 31 33 2c 20 20 36 31 32 2c 0a 20 2f 2a 20   613,  612,. /* 
310d7 20 31 33 35 30 20 2a 2f 20 20 20 34 34 33 2c 20   1350 */   443, 
310d8 20 34 31 36 2c 20 20 31 38 30 2c 20 20 32 34 36   416,  180,  246
310d9 2c 20 20 36 32 30 2c 20 20 31 35 31 2c 20 20 34  ,  620,  151,  4
310da 31 35 2c 20 20 32 34 35 2c 20 20 32 34 33 2c 20  15,  245,  243, 
310db 20 36 32 30 2c 0a 20 2f 2a 20 20 31 33 36 30 20   620,. /*  1360 
310dc 2a 2f 20 20 20 31 37 38 2c 20 20 35 39 38 2c 20  */   178,  598, 
310dd 20 32 34 32 2c 20 20 20 34 30 2c 20 20 20 34 31   242,   40,   41
310de 2c 20 20 36 32 30 2c 20 20 32 34 34 2c 20 20 20  ,  620,  244,   
310df 20 38 2c 20 20 36 32 30 2c 20 20 32 33 39 2c 0a   8,  620,  239,.
310e0 20 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20 20   /*  1370 */    
310e1 34 32 2c 20 20 34 31 32 2c 20 20 34 31 31 2c 20  42,  412,  411, 
310e2 20 36 32 30 2c 20 20 34 31 30 2c 20 20 35 39 34   620,  410,  594
310e3 2c 20 20 34 31 30 2c 20 20 36 32 30 2c 20 20 20  ,  410,  620,   
310e4 36 30 2c 20 20 31 35 33 2c 0a 20 2f 2a 20 20 31  60,  153,. /*  1
310e5 33 38 30 20 2a 2f 20 20 20 34 32 39 2c 20 20 34  380 */   429,  4
310e6 36 35 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20  65,  622,  621, 
310e7 20 32 39 36 2c 20 20 31 35 34 2c 20 20 20 33 30   296,  154,   30
310e8 2c 20 20 31 34 35 2c 20 20 31 35 32 2c 20 20 34  ,  145,  152,  4
310e9 31 33 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f  13,. /*  1390 */
310ea 20 20 20 33 38 38 2c 20 20 34 31 33 2c 20 20 32     388,  413,  2
310eb 39 35 2c 20 20 33 39 34 2c 20 20 32 39 34 2c 20  95,  394,  294, 
310ec 20 36 32 30 2c 20 20 20 33 31 2c 20 20 33 39 32   620,   31,  392
310ed 2c 20 20 36 30 30 2c 20 20 20 37 39 2c 0a 20 2f  ,  600,   79,. /
310ee 2a 20 20 31 34 30 30 20 2a 2f 20 20 20 36 30 30  *  1400 */   600
310ef 2c 20 20 20 37 38 2c 20 20 36 32 30 2c 20 20 32  ,   78,  620,  2
310f0 39 30 2c 20 20 33 39 36 2c 20 20 34 31 33 2c 20  90,  396,  413, 
310f1 20 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39 32   592,  592,  592
310f2 2c 20 20 35 39 31 2c 0a 20 2f 2a 20 20 31 34 31  ,  591,. /*  141
310f3 30 20 2a 2f 20 20 20 35 39 30 2c 20 20 20 31 34  0 */   590,   14
310f4 2c 20 20 36 32 30 2c 20 20 32 39 33 2c 20 20 36  ,  620,  293,  6
310f5 30 30 2c 20 20 20 20 39 2c 20 20 35 39 37 2c 20  00,    9,  597, 
310f6 20 20 35 39 2c 20 20 36 32 30 2c 20 20 20 33 36    59,  620,   36
310f7 2c 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20  ,. /*  1420 */  
310f8 20 35 35 35 2c 20 20 31 37 33 2c 20 20 35 36 35   555,  173,  565
310f9 2c 20 20 32 33 34 2c 20 20 31 38 35 2c 20 20 32  ,  234,  185,  2
310fa 38 38 2c 20 20 20 32 39 2c 20 20 35 34 31 2c 20  88,   29,  541, 
310fb 20 33 39 31 2c 20 20 33 34 35 2c 0a 20 2f 2a 20   391,  345,. /* 
310fc 20 31 34 33 30 20 2a 2f 20 20 20 32 34 38 2c 20   1430 */   248, 
310fd 20 32 38 36 2c 20 20 35 32 31 2c 20 20 35 33 35   286,  521,  535
310fe 2c 20 20 33 31 33 2c 20 20 32 38 34 2c 20 20 33  ,  313,  284,  3
310ff 38 35 2c 20 20 33 32 38 2c 20 20 35 33 34 2c 20  85,  328,  534, 
31100 20 32 33 39 2c 0a 20 2f 2a 20 20 31 34 34 30 20   239,. /*  1440 
31101 2a 2f 20 20 20 35 31 36 2c 20 20 35 31 35 2c 20  */   516,  515, 
31102 20 31 39 36 2c 20 20 31 39 35 2c 20 20 32 37 39   196,  195,  279
31103 2c 20 20 33 31 30 2c 20 20 35 31 31 2c 20 20 35  ,  310,  511,  5
31104 31 32 2c 20 20 35 31 30 2c 20 20 31 33 31 2c 0a  12,  510,  131,.
31105 20 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20 35   /*  1450 */   5
31106 32 34 2c 20 20 32 32 37 2c 20 20 32 35 38 2c 20  24,  227,  258, 
31107 20 32 32 38 2c 20 20 35 39 34 2c 20 20 33 30 39   228,  594,  309
31108 2c 20 20 34 38 37 2c 20 20 34 38 36 2c 20 20 34  ,  487,  486,  4
31109 39 33 2c 20 20 32 32 36 2c 0a 20 2f 2a 20 20 31  93,  226,. /*  1
3110a 34 36 30 20 2a 2f 20 20 20 33 37 32 2c 20 20 34  460 */   372,  4
3110b 38 35 2c 20 20 31 36 34 2c 20 20 33 33 38 2c 20  85,  164,  338, 
3110c 20 34 37 39 2c 20 20 31 36 33 2c 20 20 33 36 38   479,  163,  368
3110d 2c 20 20 33 37 30 2c 20 20 31 36 32 2c 20 20 20  ,  370,  162,   
3110e 32 36 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f  26,. /*  1470 */
3110f 20 20 20 32 31 31 2c 20 20 34 37 37 2c 20 20 32     211,  477,  2
31110 36 31 2c 20 20 31 36 31 2c 20 20 31 34 31 2c 20  61,  161,  141, 
31111 20 34 37 36 2c 20 20 33 36 32 2c 20 20 34 36 36   476,  362,  466
31112 2c 20 20 31 32 32 2c 20 20 31 38 37 2c 0a 20 2f  ,  122,  187,. /
31113 2a 20 20 31 34 38 30 20 2a 2f 20 20 20 31 31 39  *  1480 */   119
31114 2c 20 20 34 35 36 2c 20 20 33 34 37 2c 20 20 31  ,  456,  347,  1
31115 31 37 2c 20 20 33 34 36 2c 20 20 35 39 32 2c 20  17,  346,  592, 
31116 20 35 39 32 2c 20 20 35 39 32 2c 20 20 31 31 36   592,  592,  116
31117 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 34 39  ,  115,. /*  149
31118 30 20 2a 2f 20 20 20 31 31 34 2c 20 20 34 34 37  0 */   114,  447
31119 2c 20 20 31 31 32 2c 20 20 31 38 32 2c 20 20 33  ,  112,  182,  3
3111a 31 38 2c 20 20 20 32 32 2c 20 20 34 33 32 2c 20  18,   22,  432, 
3111b 20 20 31 39 2c 20 20 34 33 31 2c 20 20 34 33 30    19,  431,  430
3111c 2c 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20  ,. /*  1500 */  
3111d 20 20 36 33 2c 20 20 34 32 37 2c 20 20 36 30 38    63,  427,  608
3111e 2c 20 20 31 39 33 2c 20 20 32 39 37 2c 20 20 35  ,  193,  297,  5
3111f 39 35 2c 20 20 35 37 32 2c 20 20 35 37 30 2c 20  95,  572,  570, 
31120 20 34 30 33 2c 20 20 35 35 33 2c 0a 20 2f 2a 20   403,  553,. /* 
31121 20 31 35 31 30 20 2a 2f 20 20 20 35 35 30 2c 20   1510 */   550, 
31122 20 32 38 39 2c 20 20 32 38 30 2c 20 20 35 30 38   289,  280,  508
31123 2c 20 20 34 39 38 2c 20 20 34 39 37 2c 20 20 34  ,  498,  497,  4
31124 39 36 2c 20 20 34 39 34 2c 20 20 33 37 39 2c 20  96,  494,  379, 
31125 20 33 35 35 2c 0a 20 2f 2a 20 20 31 35 32 30 20   355,. /*  1520 
31126 2a 2f 20 20 20 34 35 39 2c 20 20 32 35 35 2c 20  */   459,  255, 
31127 20 32 34 39 2c 20 20 33 34 34 2c 20 20 34 34 36   249,  344,  446
31128 2c 20 20 33 30 35 2c 20 20 20 20 35 2c 20 20 35  ,  305,    5,  5
31129 36 38 2c 20 20 35 34 38 2c 20 20 32 39 38 2c 0a  68,  548,  298,.
3112a 20 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20 20 32   /*  1530 */   2
3112b 39 38 2c 20 20 32 31 30 2c 20 20 33 36 39 2c 20  98,  210,  369, 
3112c 20 32 39 38 2c 20 20 34 30 30 2c 20 20 35 30 36   298,  400,  506
3112d 2c 20 20 35 30 30 2c 20 20 34 38 39 2c 20 20 35  ,  500,  489,  5
3112e 32 35 2c 20 20 35 32 33 2c 0a 20 2f 2a 20 20 31  25,  523,. /*  1
3112f 35 34 30 20 2a 2f 20 20 20 34 38 32 2c 20 20 32  540 */   482,  2
31130 33 39 2c 20 20 32 33 37 2c 0a 7d 3b 0a 73 74 61  39,  237,.};.sta
31131 74 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45  tic const YYCODE
31132 54 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65 61  TYPE yy_lookahea
31133 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20  d[] = {. /*     
31134 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 32 32 32  0 */    19,  222
31135 2c 20 20 32 32 33 2c 20 20 32 32 34 2c 20 20 32  ,  223,  224,  2
31136 32 35 2c 20 20 20 32 34 2c 20 20 20 20 31 2c 20  25,   24,    1, 
31137 20 20 32 36 2c 20 20 20 37 37 2c 20 20 20 37 38    26,   77,   78
31138 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20  ,. /*    10 */  
31139 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 31 35    79,   80,   15
3113a 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
3113b 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
3113c 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20    87,   88,. /* 
3113d 20 20 20 32 30 20 2a 2f 20 20 20 20 38 39 2c 20     20 */    89, 
3113e 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32    90,   91,   92
3113f 2c 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20  ,    7,    8,   
31140 20 39 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20   9,   26,   27, 
31141 20 20 32 33 2c 0a 20 2f 2a 20 20 20 20 33 30 20    23,. /*    30 
31142 2a 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20  */    49,   50, 
31143 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
31144 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
31145 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a  86,   87,   88,.
31146 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 20   /*    40 */    
31147 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
31148 20 20 39 32 2c 20 20 20 38 38 2c 20 20 20 38 39    92,   88,   89
31149 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
3114a 39 32 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20  92,   68,. /*   
3114b 20 35 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20   50 */    69,   
3114c 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
3114d 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
3114e 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
3114f 37 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f  78,. /*    60 */
31150 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20      79,   80,   
31151 32 32 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  22,   82,   83, 
31152 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
31153 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f  ,   87,   88,. /
31154 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 20 38 39  *    70 */    89
31155 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
31156 39 32 2c 20 20 20 31 39 2c 20 20 20 39 34 2c 20  92,   19,   94, 
31157 20 32 32 32 2c 20 20 20 31 39 2c 20 20 32 32 34   222,   19,  224
31158 2c 20 20 32 32 35 2c 0a 20 2f 2a 20 20 20 20 38  ,  225,. /*    8
31159 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 20 38 32  0 */    25,   82
3115a 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
3115b 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
3115c 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
3115d 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20  ,. /*    90 */  
3115e 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
3115f 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20  ,   26,   27,   
31160 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c 20  94,   95,   96, 
31161 20 32 34 34 2c 20 20 20 32 35 2c 0a 20 2f 2a 20   244,   25,. /* 
31162 20 20 31 30 30 20 2a 2f 20 20 20 20 39 39 2c 20    100 */    99, 
31163 20 31 30 30 2c 20 20 31 30 31 2c 20 20 20 32 35   100,  101,   25
31164 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20  ,   49,   50,   
31165 31 39 2c 20 20 31 35 30 2c 20 20 20 39 36 2c 20  19,  150,   96, 
31166 20 20 35 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20    54,. /*   110 
31167 2a 2f 20 20 20 31 30 39 2c 20 20 20 39 39 2c 20  */   109,   99, 
31168 20 31 30 30 2c 20 20 31 30 31 2c 20 20 20 32 37   100,  101,   27
31169 2c 20 20 31 35 38 2c 20 20 20 32 36 2c 20 20 20  ,  158,   26,   
3116a 32 37 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 0a  27,  161,  162,.
3116b 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 31   /*   120 */   1
3116c 31 37 2c 20 20 31 30 39 2c 20 20 31 36 35 2c 20  17,  109,  165, 
3116d 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
3116e 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20  ,   71,   72,   
3116f 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20  73,   74,. /*   
31170 31 33 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20  130 */    75,   
31171 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
31172 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 31 36    79,   80,   16
31173 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
31174 38 34 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f  84,. /*   140 */
31175 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20      85,   86,   
31176 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
31177 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32    90,   91,   92
31178 2c 20 20 20 31 39 2c 20 20 31 31 31 2c 0a 20 2f  ,   19,  111,. /
31179 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 20 39 32  *   150 */    92
3117a 2c 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20 20  ,  113,  114,   
3117b 32 34 2c 20 20 20 39 36 2c 20 20 20 39 37 2c 20  24,   96,   97, 
3117c 20 20 39 38 2c 20 20 20 39 39 2c 20 20 31 30 30    98,   99,  100
3117d 2c 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 31 36  ,  101,. /*   16
3117e 30 20 2a 2f 20 20 20 31 30 32 2c 20 20 20 39 34  0 */   102,   94
3117f 2c 20 20 20 39 35 2c 20 20 31 31 32 2c 20 20 20  ,   95,  112,   
31180 32 35 2c 20 20 20 39 32 2c 20 20 31 30 38 2c 20  25,   92,  108, 
31181 20 31 31 36 2c 20 20 31 35 30 2c 20 20 20 39 35   116,  150,   95
31182 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20  ,. /*   170 */  
31183 20 20 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39    97,   98,   99
31184 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31  ,  100,  101,  1
31185 30 32 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  02,   86,   87, 
31186 20 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20    49,   50,. /* 
31187 20 20 31 38 30 20 2a 2f 20 20 20 20 36 30 2c 20    180 */    60, 
31188 20 31 30 38 2c 20 20 20 36 32 2c 20 20 31 36 35   108,   62,  165
31189 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31  ,   94,   95,  1
3118a 31 39 2c 20 20 20 39 37 2c 20 20 31 37 30 2c 20  19,   97,  170, 
3118b 20 31 37 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20   171,. /*   190 
3118c 2a 2f 20 20 20 20 35 30 2c 20 20 20 32 36 2c 20  */    50,   26, 
3118d 20 20 32 37 2c 20 20 31 31 39 2c 20 20 31 30 34    27,  119,  104
3118e 2c 20 20 31 30 35 2c 20 20 31 31 38 2c 20 20 20  ,  105,  118,   
3118f 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a  68,   69,   70,.
31190 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 20   /*   200 */    
31191 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
31192 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
31193 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
31194 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20  79,   80,. /*   
31195 32 31 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20  210 */    22,   
31196 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
31197 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
31198 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
31199 39 30 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f  90,. /*   220 */
3119a 20 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20      91,   92,   
3119b 31 39 2c 20 20 20 39 38 2c 20 20 20 38 36 2c 20  19,   98,   86, 
3119c 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
3119d 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 0a 20 2f  ,   90,   91,. /
3119e 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 20 39 32  *   230 */    92
3119f 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 31  ,  150,   23,  1
311a0 30 38 2c 20 20 20 32 35 2c 20 20 20 39 36 2c 20  08,   25,   96, 
311a1 20 31 35 30 2c 20 20 20 36 36 2c 20 20 20 39 39   150,   66,   99
311a2 2c 20 20 31 30 30 2c 0a 20 2f 2a 20 20 20 32 34  ,  100,. /*   24
311a3 30 20 2a 2f 20 20 20 31 30 31 2c 20 20 20 32 36  0 */   101,   26
311a4 2c 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 31  ,  161,  162,  1
311a5 30 34 2c 20 20 20 32 34 2c 20 20 31 36 35 2c 20  04,   24,  165, 
311a6 20 20 32 36 2c 20 20 31 30 39 2c 20 20 32 33 31    26,  109,  231
311a7 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20  ,. /*   250 */  
311a8 20 32 33 32 2c 20 20 31 36 35 2c 20 20 20 34 39   232,  165,   49
311a9 2c 20 20 20 35 30 2c 20 20 20 32 32 2c 20 20 20  ,   50,   22,   
311aa 32 33 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  23,  170,  171, 
311ab 20 31 33 38 2c 20 20 20 39 34 2c 0a 20 2f 2a 20   138,   94,. /* 
311ac 20 20 32 36 30 20 2a 2f 20 20 20 20 39 35 2c 20    260 */    95, 
311ad 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31   169,  170,  171
311ae 2c 20 20 20 37 36 2c 20 20 20 39 34 2c 20 20 31  ,   76,   94,  1
311af 38 35 2c 20 20 31 38 36 2c 20 20 20 32 32 2c 20  85,  186,   22, 
311b0 20 20 39 38 2c 0a 20 2f 2a 20 20 20 32 37 30 20    98,. /*   270 
311b1 2a 2f 20 20 20 20 32 34 2c 20 20 20 36 38 2c 20  */    24,   68, 
311b2 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
311b3 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
311b4 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a  74,   75,   76,.
311b5 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 20   /*   280 */    
311b6 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
311b7 20 20 38 30 2c 20 20 31 31 39 2c 20 20 20 38 32    80,  119,   82
311b8 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
311b9 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20  85,   86,. /*   
311ba 32 39 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20  290 */    87,   
311bb 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
311bc 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
311bd 2c 20 20 31 35 30 2c 20 20 20 31 31 2c 20 20 20  ,  150,   11,   
311be 36 37 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f  67,. /*   300 */
311bf 20 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31     129,  130,  1
311c0 33 31 2c 20 20 31 31 37 2c 20 20 20 39 35 2c 20  31,  117,   95, 
311c1 20 31 35 30 2c 20 20 20 32 32 2c 20 20 31 36 30   150,   22,  160
311c2 2c 20 20 31 35 30 2c 20 20 20 39 34 2c 0a 20 2f  ,  150,   94,. /
311c3 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 20 32 36  *   310 */    26
311c4 2c 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20 20  ,   27,  165,   
311c5 32 32 2c 20 20 20 32 33 2c 20 20 20 39 34 2c 20  22,   23,   94, 
311c6 20 20 32 35 2c 20 20 32 33 31 2c 20 20 32 33 32    25,  231,  232
311c7 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 33 32  ,   23,. /*   32
311c8 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34  0 */   165,  174
311c9 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 32  ,  175,  165,  2
311ca 33 32 2c 20 20 20 33 32 2c 20 20 20 34 39 2c 20  32,   32,   49, 
311cb 20 20 35 30 2c 20 20 31 31 39 2c 20 20 31 37 34    50,  119,  174
311cc 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20  ,. /*   330 */  
311cd 20 31 37 35 2c 20 20 20 32 32 2c 20 20 31 37 34   175,   22,  174
311ce 2c 20 20 31 37 35 2c 20 20 20 34 31 2c 20 20 20  ,  175,   41,   
311cf 32 36 2c 20 20 20 32 37 2c 20 20 20 35 37 2c 20  26,   27,   57, 
311d0 20 31 36 36 2c 20 20 31 33 36 2c 0a 20 2f 2a 20   166,  136,. /* 
311d1 20 20 33 34 30 20 2a 2f 20 20 20 32 32 32 2c 20    340 */   222, 
311d2 20 31 39 34 2c 20 20 32 32 34 2c 20 20 32 32 35   194,  224,  225
311d3 2c 20 20 20 35 37 2c 20 20 20 36 38 2c 20 20 20  ,   57,   68,   
311d4 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
311d5 20 20 37 32 2c 0a 20 2f 2a 20 20 20 33 35 30 20    72,. /*   350 
311d6 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20  */    73,   74, 
311d7 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
311d8 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
311d9 38 30 2c 20 20 20 36 37 2c 20 20 20 38 32 2c 0a  80,   67,   82,.
311da 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 20   /*   360 */    
311db 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
311dc 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
311dd 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
311de 39 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20  91,   92,. /*   
311df 33 37 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 32  370 */    19,  2
311e0 31 36 2c 20 20 32 31 34 2c 20 20 32 31 35 2c 20  16,  214,  215, 
311e1 20 20 32 33 2c 20 20 20 32 33 2c 20 20 32 32 31    23,   23,  221
311e2 2c 20 20 32 30 35 2c 20 20 20 39 34 2c 20 20 20  ,  205,   94,   
311e3 39 35 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f  95,. /*   380 */
311e4 20 20 20 31 37 32 2c 20 20 31 37 33 2c 20 20 20     172,  173,   
311e5 31 32 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20  12,   49,   50, 
311e6 20 31 30 35 2c 20 20 31 30 36 2c 20 20 20 32 36   105,  106,   26
311e7 2c 20 20 20 32 37 2c 20 20 31 38 31 2c 0a 20 2f  ,   27,  181,. /
311e8 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 20 32 33  *   390 */    23
311e9 2c 20 20 20 31 39 2c 20 20 31 30 35 2c 20 20 31  ,   19,  105,  1
311ea 30 36 2c 20 20 32 32 32 2c 20 20 31 31 35 2c 20  06,  222,  115, 
311eb 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20 32 38   224,  225,   28
311ec 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 34 30  ,   27,. /*   40
311ed 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 35 30  0 */    49,   50
311ee 2c 20 20 31 31 35 2c 20 20 20 39 34 2c 20 20 20  ,  115,   94,   
311ef 39 35 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  95,   71,   72, 
311f0 20 31 31 31 2c 20 20 31 32 38 2c 20 20 31 31 33   111,  128,  113
311f1 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20  ,. /*   410 */  
311f2 20 31 31 34 2c 20 20 31 31 37 2c 20 20 31 36 30   114,  117,  160
311f3 2c 20 20 31 33 36 2c 20 20 20 34 34 2c 20 20 31  ,  136,   44,  1
311f4 32 38 2c 20 20 20 34 36 2c 20 20 20 32 36 2c 20  28,   46,   26, 
311f5 20 20 32 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20    27,   68,. /* 
311f6 20 20 34 32 30 20 2a 2f 20 20 20 20 36 39 2c 20    420 */    69, 
311f7 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
311f8 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
311f9 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
311fa 20 20 37 38 2c 0a 20 2f 2a 20 20 20 34 33 30 20    78,. /*   430 
311fb 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20  */    79,   80, 
311fc 20 20 39 38 2c 20 20 20 38 32 2c 20 20 20 38 33    98,   82,   83
311fd 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
311fe 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a  86,   87,   88,.
311ff 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 20   /*   440 */    
31200 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
31201 20 20 39 32 2c 20 20 20 31 39 2c 20 20 20 31 32    92,   19,   12
31202 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 20 20 20  ,  194,  150,   
31203 32 33 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  23,   88,. /*   
31204 34 35 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 20  450 */    23,   
31205 20 37 2c 20 20 20 20 38 2c 20 20 31 30 35 2c 20   7,    8,  105, 
31206 20 31 30 36 2c 20 20 20 39 34 2c 20 20 20 39 35   106,   94,   95
31207 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20  ,   23,  150,   
31208 32 35 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f  25,. /*   460 */
31209 20 20 20 31 31 37 2c 20 20 20 32 38 2c 20 20 31     117,   28,  1
3120a 36 35 2c 20 20 31 31 31 2c 20 20 31 36 33 2c 20  65,  111,  163, 
3120b 20 31 31 33 2c 20 20 31 31 34 2c 20 20 31 36 36   113,  114,  166
3120c 2c 20 20 31 36 37 2c 20 20 31 36 38 2c 0a 20 2f  ,  167,  168,. /
3120d 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 32 31 38  *   470 */   218
3120e 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
3120f 36 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20  65,   49,   50, 
31210 20 31 32 38 2c 20 20 20 34 34 2c 20 20 31 31 31   128,   44,  111
31211 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20 20 34 38  ,   46,. /*   48
31212 30 20 2a 2f 20 20 20 31 31 33 2c 20 20 31 31 34  0 */   113,  114
31213 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
31214 32 31 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  21,   94,   95, 
31215 20 32 33 35 2c 20 20 20 32 32 2c 20 20 20 32 33   235,   22,   23
31216 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20  ,. /*   490 */  
31217 20 20 35 37 2c 20 20 20 32 35 2c 20 20 32 34 30    57,   25,  240
31218 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
31219 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
3121a 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20    73,   74,. /* 
3121b 20 20 35 30 30 20 2a 2f 20 20 20 20 37 35 2c 20    500 */    75, 
3121c 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
3121d 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
3121e 31 37 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  17,   82,   83, 
3121f 20 20 38 34 2c 0a 20 2f 2a 20 20 20 35 31 30 20    84,. /*   510 
31220 2a 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20  */    85,   86, 
31221 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
31222 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
31223 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 0a  92,   19,  150,.
31224 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 20   /*   520 */    
31225 32 35 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20  25,  150,   23, 
31226 20 32 32 36 2c 20 20 32 31 36 2c 20 20 20 31 39   226,  216,   19
31227 2c 20 20 20 36 33 2c 20 20 31 35 30 2c 20 20 31  ,   63,  150,  1
31228 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  50,  150,. /*   
31229 35 33 30 20 2a 2f 20 20 20 31 36 31 2c 20 20 31  530 */   161,  1
3122a 36 32 2c 20 20 31 35 30 2c 20 20 20 36 37 2c 20  62,  150,   67, 
3122b 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35   165,  150,  165
3122c 2c 20 20 20 32 33 2c 20 20 31 31 31 2c 20 20 20  ,   23,  111,   
3122d 32 35 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f  25,. /*   540 */
3122e 20 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20 31     113,  114,  1
3122f 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  65,  165,  165, 
31230 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35   174,  175,  165
31231 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f  ,   49,   50,. /
31232 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 31 36 35  *   550 */   165
31233 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
31234 37 34 2c 20 20 31 37 35 2c 20 20 31 39 37 2c 20  74,  175,  197, 
31235 20 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32 33    50,   22,   23
31236 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 35 36  ,  174,. /*   56
31237 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 36 30  0 */   175,  160
31238 2c 20 20 32 30 34 2c 20 20 31 30 30 2c 20 20 31  ,  204,  100,  1
31239 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  69,  170,  171, 
3123a 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
3123b 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20  ,. /*   570 */  
3123c 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
3123d 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
3123e 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
3123f 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20    79,   80,. /* 
31240 20 20 35 38 30 20 2a 2f 20 20 20 31 31 32 2c 20    580 */   112, 
31241 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
31242 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
31243 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
31244 20 20 39 30 2c 0a 20 2f 2a 20 20 20 35 39 30 20    90,. /*   590 
31245 2a 2f 20 20 20 20 39 31 2c 20 20 20 39 32 2c 20  */    91,   92, 
31246 20 20 31 39 2c 20 20 20 32 32 2c 20 20 32 31 35    19,   22,  215
31247 2c 20 20 31 39 34 2c 20 20 20 32 33 2c 20 20 32  ,  194,   23,  2
31248 32 30 2c 20 20 20 39 37 2c 20 20 20 33 30 2c 0a  20,   97,   30,.
31249 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 31   /*   600 */   1
3124a 35 30 2c 20 20 31 33 38 2c 20 20 20 36 37 2c 20  50,  138,   67, 
3124b 20 20 33 34 2c 20 20 20 33 36 2c 20 20 32 32 30    34,   36,  220
3124c 2c 20 20 31 35 30 2c 20 20 32 30 36 2c 20 20 32  ,  150,  206,  2
3124d 30 37 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20  07,   22,. /*   
3124e 36 31 30 20 2a 2f 20 20 20 31 30 34 2c 20 20 31  610 */   104,  1
3124f 38 31 2c 20 20 31 38 32 2c 20 20 20 32 36 2c 20  81,  182,   26, 
31250 20 31 30 38 2c 20 20 31 36 35 2c 20 20 31 31 35   108,  165,  115
31251 2c 20 20 20 34 38 2c 20 20 31 38 38 2c 20 20 20  ,   48,  188,   
31252 35 31 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f  51,. /*   620 */
31253 20 20 20 31 32 35 2c 20 20 31 36 35 2c 20 20 20     125,  165,   
31254 34 39 2c 20 20 20 35 30 2c 20 20 31 37 34 2c 20  49,   50,  174, 
31255 20 31 37 35 2c 20 20 20 35 38 2c 20 20 32 33 32   175,   58,  232
31256 2c 20 20 31 32 37 2c 20 20 20 32 33 2c 0a 20 2f  ,  127,   23,. /
31257 2a 20 20 20 36 33 30 20 2a 2f 20 20 20 31 37 34  *   630 */   174
31258 2c 20 20 31 37 35 2c 20 20 31 30 35 2c 20 20 31  ,  175,  105,  1
31259 30 36 2c 20 20 31 36 30 2c 20 20 31 34 32 2c 20  06,  160,  142, 
3125a 20 31 34 33 2c 20 20 31 34 34 2c 20 20 31 34 35   143,  144,  145
3125b 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 36 34  ,   23,. /*   64
3125c 30 20 2a 2f 20 20 20 20 31 36 2c 20 20 20 36 38  0 */    16,   68
3125d 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
3125e 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
3125f 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
31260 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20  ,. /*   650 */  
31261 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
31262 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20  ,   80,  150,   
31263 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
31264 20 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20    85,   86,. /* 
31265 20 20 36 36 30 20 2a 2f 20 20 20 20 38 37 2c 20    660 */    87, 
31266 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
31267 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
31268 31 39 2c 20 20 31 35 30 2c 20 20 31 39 34 2c 20  19,  150,  194, 
31269 20 31 36 35 2c 0a 20 2f 2a 20 20 20 36 37 30 20   165,. /*   670 
3126a 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 34 2c 20  */   150,   24, 
3126b 20 20 32 32 2c 20 20 31 35 30 2c 20 20 31 35 30    22,  150,  150
3126c 2c 20 20 31 30 37 2c 20 20 31 35 30 2c 20 20 20  ,  107,  150,   
3126d 32 32 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a  22,  174,  175,.
3126e 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 20   /*   680 */    
3126f 38 38 2c 20 20 20 39 34 2c 20 20 31 36 35 2c 20  88,   94,  165, 
31270 20 20 32 33 2c 20 20 20 36 30 2c 20 20 31 36 35    23,   60,  165
31271 2c 20 20 20 36 32 2c 20 20 31 35 30 2c 20 20 31  ,   62,  150,  1
31272 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20  65,  165,. /*   
31273 36 39 30 20 2a 2f 20 20 20 20 31 31 2c 20 20 31  690 */    11,  1
31274 36 35 2c 20 20 32 31 38 2c 20 20 20 33 35 2c 20  65,  218,   35, 
31275 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39   174,  175,   49
31276 2c 20 20 20 35 30 2c 20 20 31 37 34 2c 20 20 31  ,   50,  174,  1
31277 37 35 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f  75,. /*   700 */
31278 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31     174,  175,  1
31279 36 35 2c 20 20 20 32 35 2c 20 20 31 31 32 2c 20  65,   25,  112, 
3127a 20 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32     0,    1,    2
3127b 2c 20 20 31 31 36 2c 20 20 20 32 35 2c 0a 20 2f  ,  116,   25,. /
3127c 2a 20 20 20 37 31 30 20 2a 2f 20 20 20 31 39 33  *   710 */   193
3127d 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
3127e 32 33 2c 20 20 32 34 30 2c 20 20 20 36 38 2c 20  23,  240,   68, 
3127f 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
31280 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20 37 32  ,   72,. /*   72
31281 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34  0 */    73,   74
31282 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
31283 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
31284 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32    80,  150,   82
31285 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20  ,. /*   730 */  
31286 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
31287 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
31288 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
31289 20 20 39 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20    91,   92,. /* 
3128a 20 20 37 34 30 20 2a 2f 20 20 20 20 31 39 2c 20    740 */    19, 
3128b 20 31 35 30 2c 20 20 20 32 33 2c 20 20 31 36 35   150,   23,  165
3128c 2c 20 20 20 32 35 2c 20 20 31 36 36 2c 20 20 31  ,   25,  166,  1
3128d 36 37 2c 20 20 31 36 38 2c 20 20 20 39 37 2c 20  67,  168,   97, 
3128e 20 20 39 38 2c 0a 20 2f 2a 20 20 20 37 35 30 20    98,. /*   750 
3128f 2a 2f 20 20 20 31 35 35 2c 20 20 20 32 33 2c 20  */   155,   23, 
31290 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 35 30   174,  175,  150
31291 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31  ,   25,  165,  1
31292 35 30 2c 20 20 20 33 35 2c 20 20 31 35 30 2c 0a  50,   35,  150,.
31293 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20 31   /*   760 */   1
31294 35 30 2c 20 20 31 35 30 2c 20 20 32 34 35 2c 20  50,  150,  245, 
31295 20 31 35 30 2c 20 20 32 34 31 2c 20 20 31 37 34   150,  241,  174
31296 2c 20 20 31 37 35 2c 20 20 31 32 39 2c 20 20 31  ,  175,  129,  1
31297 33 30 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20  30,  165,. /*   
31298 37 37 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 20  770 */    49,   
31299 35 30 2c 20 20 31 36 35 2c 20 20 20 35 32 2c 20  50,  165,   52, 
3129a 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
3129b 2c 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20  ,   23,  165,   
3129c 32 35 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f  25,. /*   780 */
3129d 20 20 20 20 32 35 2c 20 20 31 37 34 2c 20 20 31      25,  174,  1
3129e 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  75,  174,  175, 
3129f 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 34 38   174,  175,  248
312a0 2c 20 20 32 34 39 2c 20 20 20 36 38 2c 0a 20 2f  ,  249,   68,. /
312a1 2a 20 20 20 37 39 30 20 2a 2f 20 20 20 20 36 39  *   790 */    69
312a2 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
312a3 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
312a4 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
312a5 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 38 30  ,   78,. /*   80
312a6 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30  0 */    79,   80
312a7 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20  ,  150,   82,   
312a8 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
312a9 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
312aa 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20  ,. /*   810 */  
312ab 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31    89,   90,   91
312ac 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31  ,   92,   19,  1
312ad 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  50,  150,  165, 
312ae 20 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20   150,  150,. /* 
312af 20 20 38 32 30 20 2a 2f 20 20 20 31 35 30 2c 20    820 */   150, 
312b0 20 32 31 37 2c 20 20 31 35 30 2c 20 20 32 31 33   217,  150,  213
312b1 2c 20 20 32 32 39 2c 20 20 31 31 39 2c 20 20 31  ,  229,  119,  1
312b2 37 34 2c 20 20 31 37 35 2c 20 20 20 32 32 2c 20  74,  175,   22, 
312b3 20 32 33 34 2c 0a 20 2f 2a 20 20 20 38 33 30 20   234,. /*   830 
312b4 2a 2f 20 20 20 31 36 35 2c 20 20 31 36 35 2c 20  */   165,  165, 
312b5 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
312b6 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
312b7 36 35 2c 20 20 20 33 35 2c 20 20 31 37 34 2c 0a  65,   35,  174,.
312b8 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 31   /*   840 */   1
312b9 37 35 2c 20 20 31 37 34 2c 20 20 31 37 34 2c 20  75,  174,  174, 
312ba 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30   175,   49,   50
312bb 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
312bc 32 33 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20  23,   27,. /*   
312bd 38 35 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31  850 */    25,  1
312be 36 35 2c 20 20 31 31 37 2c 20 20 31 38 37 2c 20  65,  117,  187, 
312bf 20 32 34 31 2c 20 20 20 32 32 2c 20 20 31 38 37   241,   22,  187
312c0 2c 20 20 31 38 37 2c 20 20 31 30 33 2c 20 20 20  ,  187,  103,   
312c1 32 33 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f  23,. /*   860 */
312c2 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20     174,  175,   
312c3 32 33 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  23,   68,   69, 
312c4 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
312c5 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f  ,   73,   74,. /
312c6 2a 20 20 20 38 37 30 20 2a 2f 20 20 20 20 37 35  *   870 */    75
312c7 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
312c8 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
312c9 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33   150,   82,   83
312ca 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 38 38  ,   84,. /*   88
312cb 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38 36  0 */    85,   86
312cc 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
312cd 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
312ce 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30    92,   19,  150
312cf 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20  ,. /*   890 */  
312d0 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30   150,  165,  150
312d1 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20  ,   23,  150,   
312d2 32 35 2c 20 20 20 32 33 2c 20 20 31 33 35 2c 20  25,   23,  135, 
312d3 20 20 32 35 2c 20 20 31 33 37 2c 0a 20 2f 2a 20    25,  137,. /* 
312d4 20 20 39 30 30 20 2a 2f 20 20 20 31 37 34 2c 20    900 */   174, 
312d5 20 31 37 35 2c 20 20 32 30 36 2c 20 20 32 30 37   175,  206,  207
312d6 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20  ,  165,  165,   
312d7 33 39 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  39,  165,  150, 
312d8 20 31 36 35 2c 0a 20 2f 2a 20 20 20 39 31 30 20   165,. /*   910 
312d9 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20  */   150,  150, 
312da 20 31 31 35 2c 20 20 31 37 34 2c 20 20 31 37 35   115,  174,  175
312db 2c 20 20 20 35 32 2c 20 20 31 37 34 2c 20 20 31  ,   52,  174,  1
312dc 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 0a  75,   49,   50,.
312dd 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20 20   /*   920 */    
312de 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 20  23,  150,   25, 
312df 20 31 36 35 2c 20 20 31 32 37 2c 20 20 31 36 35   165,  127,  165
312e0 2c 20 20 31 36 35 2c 20 20 31 38 37 2c 20 20 20  ,  165,  187,   
312e1 32 33 2c 20 20 20 32 39 2c 0a 20 2f 2a 20 20 20  23,   29,. /*   
312e2 39 33 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31  930 */    25,  1
312e3 38 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  87,  174,  175, 
312e4 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35   174,  175,  165
312e5 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
312e6 37 30 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f  70,. /*   940 */
312e7 20 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20      71,   72,   
312e8 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
312e9 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
312ea 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f  ,   79,   80,. /
312eb 2a 20 20 20 39 35 30 20 2a 2f 20 20 20 31 35 30  *   950 */   150
312ec 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
312ed 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
312ee 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
312ef 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20 39 36  ,   90,. /*   96
312f0 30 20 2a 2f 20 20 20 20 39 31 2c 20 20 20 39 32  0 */    91,   92
312f1 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31  ,   19,  150,  1
312f2 39 33 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  93,  165,  150, 
312f3 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35    23,  150,   25
312f4 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20  ,. /*   970 */  
312f5 20 31 36 30 2c 20 20 31 36 30 2c 20 20 31 36 30   160,  160,  160
312f6 2c 20 20 20 35 32 2c 20 20 31 37 34 2c 20 20 31  ,   52,  174,  1
312f7 37 35 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20  75,  190,  191, 
312f8 20 31 36 35 2c 20 20 20 32 32 2c 0a 20 2f 2a 20   165,   22,. /* 
312f9 20 20 39 38 30 20 2a 2f 20 20 20 31 35 30 2c 20    980 */   150, 
312fa 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35   165,  150,  165
312fb 2c 20 20 31 35 30 2c 20 20 31 34 34 2c 20 20 31  ,  150,  144,  1
312fc 34 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  45,  174,  175, 
312fd 20 20 32 33 2c 0a 20 2f 2a 20 20 20 39 39 30 20    23,. /*   990 
312fe 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20  */   174,  175, 
312ff 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 35 32    49,   50,   52
31300 2c 20 20 31 36 35 2c 20 20 20 32 32 2c 20 20 31  ,  165,   22,  1
31301 36 35 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 0a  65,   22,  165,.
31302 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 32   /*  1000 */   2
31303 35 30 2c 20 20 32 35 31 2c 20 20 32 34 31 2c 20  50,  251,  241, 
31304 20 20 32 32 2c 20 20 31 39 34 2c 20 20 31 39 34    22,  194,  194
31305 2c 20 20 31 39 34 2c 20 20 31 30 38 2c 20 20 31  ,  194,  108,  1
31306 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31  74,  175,. /*  1
31307 30 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31  010 */    19,  1
31308 39 33 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  93,   69,   70, 
31309 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
3130a 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
3130b 37 36 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f  76,. /*  1020 */
3130c 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20      77,   78,   
3130d 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20  79,   80,  150, 
3130e 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
3130f 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f  ,   85,   86,. /
31310 2a 20 20 31 30 33 30 20 2a 2f 20 20 20 20 38 37  *  1030 */    87
31311 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20  ,   88,   89,   
31312 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
31313 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30    19,  150,  150
31314 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 30 34  ,  165,. /*  104
31315 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30  0 */   150,  150
31316 2c 20 20 31 35 30 2c 20 20 32 31 33 2c 20 20 31  ,  150,  213,  1
31317 36 30 2c 20 20 32 31 33 2c 20 20 31 36 30 2c 20  60,  213,  160, 
31318 20 31 36 36 2c 20 20 31 37 34 2c 20 20 31 37 35   166,  174,  175
31319 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20  ,. /*  1050 */  
3131a 20 20 39 31 2c 20 20 20 39 32 2c 20 20 31 36 35    91,   92,  165
3131b 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
3131c 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  65,  165,  165, 
3131d 20 31 35 30 2c 20 20 31 36 36 2c 0a 20 2f 2a 20   150,  166,. /* 
3131e 20 31 30 36 30 20 2a 2f 20 20 20 31 30 32 2c 20   1060 */   102, 
3131f 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 34   174,  175,   24
31320 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
31321 34 39 2c 20 20 20 35 30 2c 20 20 20 32 30 2c 20  49,   50,   20, 
31322 20 31 36 35 2c 0a 20 2f 2a 20 20 31 30 37 30 20   165,. /*  1070 
31323 2a 2f 20 20 20 31 39 30 2c 20 20 31 39 31 2c 20  */   190,  191, 
31324 20 31 30 34 2c 20 20 31 36 35 2c 20 20 20 38 36   104,  165,   86
31325 2c 20 20 20 38 37 2c 20 20 31 36 36 2c 20 20 20  ,   87,  166,   
31326 34 33 2c 20 20 31 39 34 2c 20 20 20 35 39 2c 0a  43,  194,   59,.
31327 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20 31   /*  1080 */   1
31328 39 34 2c 20 20 31 39 33 2c 20 20 31 37 34 2c 20  94,  193,  174, 
31329 20 31 37 35 2c 20 20 31 39 33 2c 20 20 31 39 33   175,  193,  193
3132a 2c 20 20 32 30 35 2c 20 20 20 37 30 2c 20 20 20  ,  205,   70,   
3132b 37 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31  71,   72,. /*  1
3132c 30 39 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20  090 */    73,   
3132d 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
3132e 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
3132f 2c 20 20 20 38 30 2c 20 20 32 30 35 2c 20 20 20  ,   80,  205,   
31330 38 32 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f  82,. /*  1100 */
31331 20 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20      83,   84,   
31332 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
31333 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
31334 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 0a 20 2f  ,   91,   92,. /
31335 2a 20 20 31 31 31 30 20 2a 2f 20 20 20 20 31 39  *  1110 */    19
31336 2c 20 20 20 32 30 2c 20 20 31 35 30 2c 20 20 20  ,   20,  150,   
31337 32 32 2c 20 20 31 35 30 2c 20 20 32 30 35 2c 20  22,  150,  205, 
31338 20 31 33 38 2c 20 20 20 32 36 2c 20 20 20 32 37   138,   26,   27
31339 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31 32  ,  150,. /*  112
3133a 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 20 31  0 */   150,    1
3133b 2c 20 20 20 20 32 2c 20 20 20 31 39 2c 20 20 20  ,    2,   19,   
3133c 32 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 20  20,  150,   22, 
3133d 20 31 36 35 2c 20 20 20 33 37 2c 20 20 31 36 35   165,   37,  165
3133e 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20  ,. /*  1130 */  
3133f 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 35    26,   27,   25
31340 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31  ,  150,  165,  1
31341 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  65,  174,  175, 
31342 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20   174,  175,. /* 
31343 20 31 31 34 30 20 2a 2f 20 20 20 31 36 35 2c 20   1140 */   165, 
31344 20 20 33 37 2c 20 20 20 35 33 2c 20 20 31 35 30    37,   53,  150
31345 2c 20 20 31 35 30 2c 20 20 31 37 33 2c 20 20 31  ,  150,  173,  1
31346 35 30 2c 20 20 20 35 36 2c 20 20 31 36 35 2c 20  50,   56,  165, 
31347 20 31 37 34 2c 0a 20 2f 2a 20 20 31 31 35 30 20   174,. /*  1150 
31348 2a 2f 20 20 20 31 37 35 2c 20 20 31 35 30 2c 20  */   175,  150, 
31349 20 20 35 33 2c 20 20 31 38 31 2c 20 20 31 30 34    53,  181,  104
3134a 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 20 20 20  ,   22,  150,   
3134b 36 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 0a  66,  165,  165,.
3134c 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 20   /*  1160 */    
3134d 35 36 2c 20 20 31 36 35 2c 20 20 31 39 33 2c 20  56,  165,  193, 
3134e 20 20 20 35 2c 20 20 20 20 31 2c 20 20 20 32 37     5,    1,   27
3134f 2c 20 20 31 36 35 2c 20 20 31 34 36 2c 20 20 31  ,  165,  146,  1
31350 34 37 2c 20 20 31 31 37 2c 0a 20 2f 2a 20 20 31  47,  117,. /*  1
31351 31 37 30 20 2a 2f 20 20 20 20 36 36 2c 20 20 31  170 */    66,  1
31352 36 35 2c 20 20 31 35 30 2c 20 20 31 35 32 2c 20  65,  150,  152, 
31353 20 20 33 35 2c 20 20 31 35 34 2c 20 20 31 39 33    35,  154,  193
31354 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
31355 38 38 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f  88,. /*  1180 */
31356 20 20 20 31 35 30 2c 20 20 31 36 30 2c 20 20 31     150,  160,  1
31357 30 37 2c 20 20 31 32 36 2c 20 20 20 39 33 2c 20  07,  126,   93, 
31358 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 36 35    94,   95,  165
31359 2c 20 20 31 35 30 2c 20 20 20 39 38 2c 0a 20 2f  ,  150,   98,. /
3135a 2a 20 20 31 31 39 30 20 2a 2f 20 20 20 20 38 36  *  1190 */    86
3135b 2c 20 20 20 38 37 2c 20 20 31 35 30 2c 20 20 31  ,   87,  150,  1
3135c 35 30 2c 20 20 20 37 36 2c 20 20 31 36 35 2c 20  50,   76,  165, 
3135d 20 20 32 32 2c 20 20 20 39 33 2c 20 20 20 39 34    22,   93,   94
3135e 2c 20 20 20 39 35 2c 0a 20 2f 2a 20 20 31 32 30  ,   95,. /*  120
3135f 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 31 31 38  0 */    76,  118
31360 2c 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20 31  ,   98,  165,  1
31361 37 34 2c 20 20 31 37 35 2c 20 20 20 20 31 2c 20  74,  175,    1, 
31362 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 31 39   165,  165,   19
31363 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20  ,. /*  1210 */  
31364 20 20 32 30 2c 20 20 32 31 37 2c 20 20 20 32 32    20,  217,   22
31365 2c 20 20 31 35 30 2c 20 20 20 31 36 2c 20 20 31  ,  150,   16,  1
31366 39 34 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  94,   26,   27, 
31367 20 31 31 38 2c 20 20 31 31 35 2c 0a 20 2f 2a 20   118,  115,. /* 
31368 20 31 32 32 30 20 2a 2f 20 20 20 31 32 39 2c 20   1220 */   129, 
31369 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32   130,  131,  132
3136a 2c 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31  ,  133,  134,  1
3136b 32 30 2c 20 20 20 33 37 2c 20 20 31 36 35 2c 20  20,   37,  165, 
3136c 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 33 30 20   150,. /*  1230 
3136d 2a 2f 20 20 20 20 32 30 2c 20 20 31 39 33 2c 20  */    20,  193, 
3136e 20 31 32 37 2c 20 20 31 32 39 2c 20 20 31 33 30   127,  129,  130
3136f 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31  ,  131,  132,  1
31370 33 33 2c 20 20 31 33 34 2c 20 20 32 31 38 2c 0a  33,  134,  218,.
31371 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20 31   /*  1240 */   1
31372 35 30 2c 20 20 31 30 37 2c 20 20 31 35 30 2c 20  50,  107,  150, 
31373 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30   150,  165,  150
31374 2c 20 20 20 35 36 2c 20 20 31 35 30 2c 20 20 31  ,   56,  150,  1
31375 35 30 2c 20 20 31 32 36 2c 0a 20 2f 2a 20 20 31  50,  126,. /*  1
31376 32 35 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20  250 */    22,   
31377 31 36 2c 20 20 31 36 30 2c 20 20 20 36 35 2c 20  16,  160,   65, 
31378 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 36 36    23,  165,   66
31379 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31  ,  165,  165,  1
3137a 35 30 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f  50,. /*  1260 */
3137b 20 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20 31     165,   23,  1
3137c 36 35 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20  65,  165,  174, 
3137d 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35   175,  174,  175
3137e 2c 20 20 32 34 37 2c 20 20 31 37 34 2c 0a 20 2f  ,  247,  174,. /
3137f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20 31 37 35  *  1270 */   175
31380 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
31381 31 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  15,  165,  150, 
31382 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31 35 30    86,   87,  150
31383 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 38  ,  150,. /*  128
31384 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 32  0 */   150,   22
31385 2c 20 20 20 20 33 2c 20 20 20 39 33 2c 20 20 20  ,    3,   93,   
31386 39 34 2c 20 20 20 39 35 2c 20 20 31 39 34 2c 20  94,   95,  194, 
31387 20 31 35 30 2c 20 20 20 39 38 2c 20 20 31 34 30   150,   98,  140
31388 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20  ,. /*  1290 */  
31389 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30   165,  150,  150
3138a 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31  ,  165,  165,  1
3138b 36 35 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20  65,   19,   20, 
3138c 20 20 20 34 2c 20 20 20 32 32 2c 0a 20 2f 2a 20     4,   22,. /* 
3138d 20 31 33 30 30 20 2a 2f 20 20 20 31 36 34 2c 20   1300 */   164, 
3138e 20 31 38 30 2c 20 20 31 36 35 2c 20 20 20 32 36   180,  165,   26
3138f 2c 20 20 20 32 37 2c 20 20 32 34 39 2c 20 20 31  ,   27,  249,  1
31390 36 35 2c 20 20 31 36 35 2c 20 20 32 31 36 2c 20  65,  165,  216, 
31391 20 20 20 36 2c 0a 20 2f 2a 20 20 31 33 31 30 20     6,. /*  1310 
31392 2a 2f 20 20 20 31 35 30 2c 20 20 31 37 34 2c 20  */   150,  174, 
31393 20 31 37 35 2c 20 20 32 32 31 2c 20 20 20 33 37   175,  221,   37
31394 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32  ,  174,  175,  2
31395 35 32 2c 20 20 32 35 32 2c 20 20 31 32 39 2c 0a  52,  252,  129,.
31396 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 31   /*  1320 */   1
31397 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20  30,  131,  132, 
31398 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31 36 35   133,  134,  165
31399 2c 20 20 31 38 30 2c 20 20 31 35 30 2c 20 20 31  ,  180,  150,  1
3139a 34 39 2c 20 20 20 20 35 2c 0a 20 2f 2a 20 20 31  49,    5,. /*  1
3139b 33 33 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  330 */   150,  1
3139c 35 30 2c 20 20 31 35 30 2c 20 20 20 35 36 2c 20  50,  150,   56, 
3139d 20 20 31 30 2c 20 20 20 31 31 2c 20 20 20 31 32    10,   11,   12
3139e 2c 20 20 20 31 33 2c 20 20 20 31 34 2c 20 20 31  ,   13,   14,  1
3139f 35 30 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f  50,. /*  1340 */
313a0 20 20 20 31 34 39 2c 20 20 20 31 37 2c 20 20 31     149,   17,  1
313a1 36 35 2c 20 20 20 36 36 2c 20 20 31 35 30 2c 20  65,   66,  150, 
313a2 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
313a3 2c 20 20 31 34 39 2c 20 20 20 31 33 2c 0a 20 2f  ,  149,   13,. /
313a4 2a 20 20 31 33 35 30 20 2a 2f 20 20 20 31 35 30  *  1350 */   150
313a5 2c 20 20 31 34 39 2c 20 20 31 35 31 2c 20 20 31  ,  149,  151,  1
313a6 35 30 2c 20 20 31 36 35 2c 20 20 20 33 31 2c 20  50,  165,   31, 
313a7 20 31 35 39 2c 20 20 20 33 33 2c 20 20 31 35 30   159,   33,  150
313a8 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33 36  ,  165,. /*  136
313a9 30 20 2a 2f 20 20 20 31 35 31 2c 20 20 31 39 34  0 */   151,  194
313aa 2c 20 20 31 35 30 2c 20 20 20 38 36 2c 20 20 20  ,  150,   86,   
313ab 38 37 2c 20 20 31 36 35 2c 20 20 20 34 32 2c 20  87,  165,   42, 
313ac 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31 31 35    25,  165,  115
313ad 2c 0a 20 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20  ,. /*  1370 */  
313ae 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
313af 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20  ,  165,  150,   
313b0 39 38 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  98,  150,  165, 
313b1 20 20 32 32 2c 20 20 20 35 35 2c 0a 20 2f 2a 20    22,   55,. /* 
313b2 20 31 33 38 30 20 2a 2f 20 20 20 31 35 30 2c 20   1380 */   150, 
313b3 20 20 35 37 2c 20 20 20 32 36 2c 20 20 20 32 37    57,   26,   27
313b4 2c 20 20 31 39 39 2c 20 20 20 36 31 2c 20 20 31  ,  199,   61,  1
313b5 32 35 2c 20 20 31 35 30 2c 20 20 20 36 34 2c 20  25,  150,   64, 
313b6 20 31 36 35 2c 0a 20 2f 2a 20 20 31 33 39 30 20   165,. /*  1390 
313b7 2a 2f 20 20 20 31 35 30 2c 20 20 31 36 35 2c 20  */   150,  165, 
313b8 20 32 30 30 2c 20 20 31 32 32 2c 20 20 32 30 31   200,  122,  201
313b9 2c 20 20 31 36 35 2c 20 20 31 32 33 2c 20 20 31  ,  165,  123,  1
313ba 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a  50,  174,  175,.
313bb 20 2f 2a 20 20 31 34 30 30 20 2a 2f 20 20 20 31   /*  1400 */   1
313bc 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20  74,  175,  165, 
313bd 20 31 35 30 2c 20 20 31 32 31 2c 20 20 31 36 35   150,  121,  165
313be 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31  ,  129,  130,  1
313bf 33 31 2c 20 20 31 33 32 2c 0a 20 2f 2a 20 20 31  31,  132,. /*  1
313c0 34 31 30 20 2a 2f 20 20 20 31 33 33 2c 20 20 31  410 */   133,  1
313c1 33 34 2c 20 20 31 36 35 2c 20 20 32 30 32 2c 20  34,  165,  202, 
313c2 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30 33   174,  175,  203
313c3 2c 20 20 31 32 34 2c 20 20 31 36 35 2c 20 20 31  ,  124,  165,  1
313c4 33 35 2c 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f  35,. /*  1420 */
313c5 20 20 20 31 35 37 2c 20 20 31 31 37 2c 20 20 20     157,  117,   
313c6 36 36 2c 20 20 32 32 37 2c 20 20 31 35 37 2c 20  66,  227,  157, 
313c7 20 32 31 30 2c 20 20 31 30 34 2c 20 20 32 31 31   210,  104,  211
313c8 2c 20 20 31 32 30 2c 20 20 31 30 35 2c 0a 20 2f  ,  120,  105,. /
313c9 2a 20 20 31 34 33 30 20 2a 2f 20 20 20 31 30 36  *  1430 */   106
313ca 2c 20 20 32 31 30 2c 20 20 31 37 36 2c 20 20 32  ,  210,  176,  2
313cb 31 31 2c 20 20 31 31 30 2c 20 20 32 31 30 2c 20  11,  110,  210, 
313cc 20 31 30 34 2c 20 20 20 34 37 2c 20 20 32 31 31   104,   47,  211
313cd 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 34 34  ,  115,. /*  144
313ce 30 20 2a 2f 20 20 20 31 37 36 2c 20 20 31 38 34  0 */   176,  184
313cf 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31  ,   86,   87,  1
313d0 37 36 2c 20 20 31 37 39 2c 20 20 31 30 33 2c 20  76,  179,  103, 
313d1 20 31 37 38 2c 20 20 31 37 36 2c 20 20 20 32 32   178,  176,   22
313d2 2c 0a 20 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20  ,. /*  1450 */  
313d3 20 20 39 34 2c 20 20 20 39 32 2c 20 20 31 32 38    94,   92,  128
313d4 2c 20 20 32 33 30 2c 20 20 20 39 38 2c 20 20 31  ,  230,   98,  1
313d5 37 39 2c 20 20 31 37 36 2c 20 20 31 37 36 2c 20  79,  176,  176, 
313d6 20 31 38 34 2c 20 20 32 33 30 2c 0a 20 2f 2a 20   184,  230,. /* 
313d7 20 31 34 36 30 20 2a 2f 20 20 20 20 31 38 2c 20   1460 */    18, 
313d8 20 31 37 36 2c 20 20 31 35 36 2c 20 20 31 33 39   176,  156,  139
313d9 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20 20 20  ,  157,  156,   
313da 34 35 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20  45,  157,  156, 
313db 20 31 33 35 2c 0a 20 2f 2a 20 20 31 34 37 30 20   135,. /*  1470 
313dc 2a 2f 20 20 20 31 35 37 2c 20 20 31 35 37 2c 20  */   157,  157, 
313dd 20 32 33 38 2c 20 20 31 35 36 2c 20 20 20 36 38   238,  156,   68
313de 2c 20 20 32 33 39 2c 20 20 31 35 37 2c 20 20 31  ,  239,  157,  1
313df 38 39 2c 20 20 31 38 39 2c 20 20 32 31 39 2c 0a  89,  189,  219,.
313e0 20 2f 2a 20 20 31 34 38 30 20 2a 2f 20 20 20 20   /*  1480 */    
313e1 32 32 2c 20 20 31 39 39 2c 20 20 31 35 37 2c 20  22,  199,  157, 
313e2 20 31 39 32 2c 20 20 20 31 38 2c 20 20 31 32 39   192,   18,  129
313e3 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31  ,  130,  131,  1
313e4 39 32 2c 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31  92,  192,. /*  1
313e5 34 39 30 20 2a 2f 20 20 20 31 39 32 2c 20 20 31  490 */   192,  1
313e6 39 39 2c 20 20 31 38 39 2c 20 20 32 31 39 2c 20  99,  189,  219, 
313e7 20 31 35 37 2c 20 20 32 34 33 2c 20 20 20 34 30   157,  243,   40
313e8 2c 20 20 32 34 33 2c 20 20 31 35 37 2c 20 20 31  ,  243,  157,  1
313e9 35 37 2c 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f  57,. /*  1500 */
313ea 20 20 20 32 34 36 2c 20 20 20 33 38 2c 20 20 31     246,   38,  1
313eb 35 33 2c 20 20 31 39 36 2c 20 20 31 39 38 2c 20  53,  196,  198, 
313ec 20 31 36 36 2c 20 20 32 33 33 2c 20 20 32 33 33   166,  233,  233
313ed 2c 20 20 32 32 38 2c 20 20 31 37 37 2c 0a 20 2f  ,  228,  177,. /
313ee 2a 20 20 31 35 31 30 20 2a 2f 20 20 20 31 37 37  *  1510 */   177
313ef 2c 20 20 32 30 39 2c 20 20 31 37 37 2c 20 20 31  ,  209,  177,  1
313f0 38 32 2c 20 20 31 37 37 2c 20 20 31 36 36 2c 20  82,  177,  166, 
313f1 20 31 37 37 2c 20 20 31 36 36 2c 20 20 31 37 38   177,  166,  178
313f2 2c 20 20 32 34 32 2c 0a 20 2f 2a 20 20 31 35 32  ,  242,. /*  152
313f3 30 20 2a 2f 20 20 20 31 39 39 2c 20 20 32 34 32  0 */   199,  242
313f4 2c 20 20 32 30 39 2c 20 20 32 30 39 2c 20 20 31  ,  209,  209,  1
313f5 39 39 2c 20 20 31 34 38 2c 20 20 31 39 36 2c 20  99,  148,  196, 
313f6 20 31 36 36 2c 20 20 32 30 38 2c 20 20 31 39 35   166,  208,  195
313f7 2c 0a 20 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20  ,. /*  1530 */  
313f8 20 31 39 35 2c 20 20 32 33 36 2c 20 20 32 33 37   195,  236,  237
313f9 2c 20 20 31 39 35 2c 20 20 31 39 31 2c 20 20 31  ,  195,  191,  1
313fa 38 33 2c 20 20 31 38 33 2c 20 20 31 38 36 2c 20  83,  183,  186, 
313fb 20 31 37 34 2c 20 20 31 37 34 2c 0a 20 2f 2a 20   174,  174,. /* 
313fc 20 31 35 34 30 20 2a 2f 20 20 20 31 38 36 2c 20   1540 */   186, 
313fd 20 31 31 35 2c 20 20 20 39 32 2c 0a 7d 3b 0a 23   115,   92,.};.#
313fe 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
313ff 55 53 45 5f 44 46 4c 54 20 28 2d 37 30 29 0a 23  USE_DFLT (-70).#
31400 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
31401 43 4f 55 4e 54 20 28 34 31 37 29 0a 23 64 65 66  COUNT (417).#def
31402 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e  ine YY_SHIFT_MIN
31403 20 20 20 28 2d 36 39 29 0a 23 64 65 66 69 6e 65     (-69).#define
31404 20 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 20 20   YY_SHIFT_MAX   
31405 28 31 34 36 36 29 0a 73 74 61 74 69 63 20 63 6f  (1466).static co
31406 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68 69  nst short yy_shi
31407 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f  ft_ofst[] = {. /
31408 2a 20 20 20 20 20 30 20 2a 2f 20 20 31 31 32 30  *     0 */  1120
31409 2c 20 31 31 30 34 2c 20 31 33 32 34 2c 20 31 31  , 1104, 1324, 11
3140a 30 34 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  04, 1190, 1190, 
3140b 20 20 39 30 2c 20 20 20 39 30 2c 20 20 20 20 31    90,   90,    1
3140c 2c 20 20 2d 31 39 2c 0a 20 2f 2a 20 20 20 20 31  ,  -19,. /*    1
3140d 30 20 2a 2f 20 20 31 31 39 30 2c 20 31 31 39 30  0 */  1190, 1190
3140e 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
3140f 39 30 2c 20 20 32 38 30 2c 20 20 33 39 31 2c 20  90,  280,  391, 
31410 20 37 32 31 2c 20 31 30 39 31 2c 20 31 31 39 30   721, 1091, 1190
31411 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
31412 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
31413 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
31414 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
31415 31 31 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20  1190, 1190,. /* 
31416 20 20 20 33 30 20 2a 2f 20 20 31 31 39 30 2c 20     30 */  1190, 
31417 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
31418 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
31419 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
3141a 31 31 39 30 2c 0a 20 2f 2a 20 20 20 20 34 30 20  1190,. /*    40 
3141b 2a 2f 20 20 31 31 39 30 2c 20 31 31 39 30 2c 20  */  1190, 1190, 
3141c 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
3141d 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 32  , 1190, 1190, 12
3141e 37 37 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 0a  77, 1190, 1190,.
3141f 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 31 31   /*    50 */  11
31420 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20  90, 1190, 1190, 
31421 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30  1190, 1190, 1190
31422 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31  , 1190, 1190, 11
31423 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20  90, 1190,. /*   
31424 20 36 30 20 2a 2f 20 20 31 31 39 30 2c 20 20 2d   60 */  1190,  -
31425 34 39 2c 20 20 32 38 37 2c 20 20 33 39 31 2c 20  49,  287,  391, 
31426 20 33 39 31 2c 20 20 39 38 38 2c 20 20 39 38 38   391,  988,  988
31427 2c 20 20 32 31 35 2c 20 31 34 32 36 2c 20 20 20  ,  215, 1426,   
31428 35 35 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  55,. /*    70 */
31429 20 20 20 36 34 37 2c 20 20 35 37 33 2c 20 20 34     647,  573,  4
3142a 39 39 2c 20 20 34 32 35 2c 20 20 33 35 31 2c 20  99,  425,  351, 
3142b 20 32 37 37 2c 20 20 32 30 33 2c 20 20 31 32 39   277,  203,  129
3142c 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 0a 20 2f  ,  795,  795,. /
3142d 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 37 39 35  *    80 */   795
3142e 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
3142f 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20  95,  795,  795, 
31430 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
31431 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20 20 39  ,  795,. /*    9
31432 30 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39 35  0 */   795,  795
31433 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
31434 39 35 2c 20 20 37 39 35 2c 20 20 38 36 39 2c 20  95,  795,  869, 
31435 20 37 39 35 2c 20 20 39 34 33 2c 20 31 30 31 37   795,  943, 1017
31436 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
31437 31 30 31 37 2c 20 20 2d 36 39 2c 20 20 2d 36 39  1017,  -69,  -69
31438 2c 20 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20 20  ,  -69,  -69,   
31439 2d 31 2c 20 20 20 2d 31 2c 20 20 20 35 38 2c 20  -1,   -1,   58, 
3143a 20 31 33 38 2c 20 20 2d 34 34 2c 0a 20 2f 2a 20   138,  -44,. /* 
3143b 20 20 31 31 30 20 2a 2f 20 20 20 33 39 31 2c 20    110 */   391, 
3143c 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
3143d 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
3143e 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
3143f 20 33 39 31 2c 0a 20 2f 2a 20 20 20 31 32 30 20   391,. /*   120 
31440 2a 2f 20 20 20 33 39 31 2c 20 20 33 39 31 2c 20  */   391,  391, 
31441 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
31442 2c 20 20 33 39 31 2c 20 20 34 36 33 2c 20 20 35  ,  391,  463,  5
31443 30 36 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 0a  06,  391,  391,.
31444 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 33   /*   130 */   3
31445 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
31446 20 32 31 35 2c 20 20 39 35 39 2c 20 31 34 35 30   215,  959, 1450
31447 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d  ,  -70,  -70,  -
31448 37 30 2c 20 31 33 35 36 2c 0a 20 2f 2a 20 20 20  70, 1356,. /*   
31449 31 34 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 34  140 */    73,  4
3144a 33 33 2c 20 20 34 33 33 2c 20 20 33 36 31 2c 20  33,  433,  361, 
3144b 20 33 30 39 2c 20 20 31 36 35 2c 20 20 20 36 37   309,  165,   67
3144c 2c 20 20 32 38 34 2c 20 20 34 36 36 2c 20 20 32  ,  284,  466,  2
3144d 39 31 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  91,. /*   150 */
3144e 20 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33     391,  391,  3
3144f 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
31450 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
31451 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 0a 20 2f  ,  391,  391,. /
31452 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 33 39 31  *   160 */   391
31453 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
31454 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
31455 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
31456 2c 20 20 33 39 31 2c 0a 20 2f 2a 20 20 20 31 37  ,  391,. /*   17
31457 30 20 2a 2f 20 20 20 33 39 31 2c 20 20 33 39 31  0 */   391,  391
31458 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33  ,  391,  391,  3
31459 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20  91,  391,  391, 
3145a 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31   391,  391,  391
3145b 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
3145c 20 33 39 31 2c 20 20 33 39 31 2c 20 20 35 30 31   391,  391,  501
3145d 2c 20 20 32 32 31 2c 20 20 32 32 31 2c 20 20 32  ,  221,  221,  2
3145e 32 31 2c 20 20 37 30 35 2c 20 20 37 39 37 2c 20  21,  705,  797, 
3145f 31 34 32 36 2c 20 31 34 32 36 2c 0a 20 2f 2a 20  1426, 1426,. /* 
31460 20 20 31 39 30 20 2a 2f 20 20 31 34 32 36 2c 20    190 */  1426, 
31461 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30   -70,  -70,  -70
31462 2c 20 20 31 33 39 2c 20 20 31 37 31 2c 20 20 31  ,  139,  171,  1
31463 37 31 2c 20 20 20 31 32 2c 20 20 35 36 38 2c 20  71,   12,  568, 
31464 20 35 36 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20   568,. /*   200 
31465 2a 2f 20 20 20 32 30 39 2c 20 20 34 32 37 2c 20  */   209,  427, 
31466 20 33 37 30 2c 20 20 33 36 37 2c 20 20 33 35 32   370,  367,  352
31467 2c 20 20 32 39 36 2c 20 20 20 33 38 2c 20 20 20  ,  296,   38,   
31468 33 38 2c 20 20 20 33 38 2c 20 20 20 33 38 2c 0a  38,   38,   38,.
31469 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 33   /*   210 */   3
3146a 34 38 2c 20 20 35 36 39 2c 20 20 20 33 38 2c 20  48,  569,   38, 
3146b 20 20 33 38 2c 20 20 20 37 34 2c 20 20 35 38 37    38,   74,  587
3146c 2c 20 20 35 39 32 2c 20 20 20 31 37 2c 20 20 34  ,  592,   17,  4
3146d 39 35 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20 20  95,   87,. /*   
3146e 32 32 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 33  220 */    87,  3
3146f 37 32 2c 20 20 34 39 35 2c 20 20 33 37 32 2c 20  72,  495,  372, 
31470 20 37 35 35 2c 20 20 32 31 35 2c 20 20 32 39 33   755,  215,  293
31471 2c 20 20 32 31 35 2c 20 20 32 39 33 2c 20 20 31  ,  215,  293,  1
31472 34 30 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  40,. /*   230 */
31473 20 20 20 32 39 33 2c 20 20 20 38 37 2c 20 20 32     293,   87,  2
31474 39 33 2c 20 20 32 39 33 2c 20 20 37 36 32 2c 20  93,  293,  762, 
31475 20 36 33 38 2c 20 20 36 33 38 2c 20 20 32 31 35   638,  638,  215
31476 2c 20 20 20 37 38 2c 20 20 20 35 31 2c 0a 20 2f  ,   78,   51,. /
31477 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 32 34 36  *   240 */   246
31478 2c 20 31 34 36 33 2c 20 31 33 30 34 2c 20 31 33  , 1463, 1304, 13
31479 30 34 2c 20 31 34 35 36 2c 20 31 34 35 36 2c 20  04, 1456, 1456, 
3147a 31 33 30 34 2c 20 31 34 35 38 2c 20 31 34 30 36  1304, 1458, 1406
3147b 2c 20 31 32 36 31 2c 0a 20 2f 2a 20 20 20 32 35  , 1261,. /*   25
3147c 30 20 2a 2f 20 20 31 34 36 36 2c 20 31 34 36 36  0 */  1466, 1466
3147d 2c 20 31 34 36 36 2c 20 31 34 36 36 2c 20 31 33  , 1466, 1466, 13
3147e 30 34 2c 20 31 32 36 31 2c 20 31 34 35 38 2c 20  04, 1261, 1458, 
3147f 31 34 30 36 2c 20 31 34 30 36 2c 20 31 33 30 34  1406, 1406, 1304
31480 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
31481 31 34 34 32 2c 20 31 33 33 34 2c 20 31 34 32 31  1442, 1334, 1421
31482 2c 20 31 33 30 34 2c 20 31 33 30 34 2c 20 31 34  , 1304, 1304, 14
31483 34 32 2c 20 31 33 30 34 2c 20 31 34 34 32 2c 20  42, 1304, 1442, 
31484 31 33 30 34 2c 20 31 34 34 32 2c 0a 20 2f 2a 20  1304, 1442,. /* 
31485 20 20 32 37 30 20 2a 2f 20 20 31 34 32 37 2c 20    270 */  1427, 
31486 31 33 33 32 2c 20 31 33 33 32 2c 20 31 33 33 32  1332, 1332, 1332
31487 2c 20 31 33 39 30 2c 20 31 33 35 39 2c 20 31 33  , 1390, 1359, 13
31488 35 39 2c 20 31 34 32 37 2c 20 31 33 33 32 2c 20  59, 1427, 1332, 
31489 31 33 34 33 2c 0a 20 2f 2a 20 20 20 32 38 30 20  1343,. /*   280 
3148a 2a 2f 20 20 31 33 33 32 2c 20 31 33 39 30 2c 20  */  1332, 1390, 
3148b 31 33 33 32 2c 20 31 33 33 32 2c 20 31 33 30 38  1332, 1332, 1308
3148c 2c 20 31 33 32 32 2c 20 31 33 30 38 2c 20 31 33  , 1322, 1308, 13
3148d 32 32 2c 20 31 33 30 38 2c 20 31 33 32 32 2c 0a  22, 1308, 1322,.
3148e 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 33   /*   290 */  13
3148f 30 34 2c 20 31 33 30 34 2c 20 31 32 38 34 2c 20  04, 1304, 1284, 
31490 31 32 39 33 2c 20 31 32 38 33 2c 20 31 32 37 33  1293, 1283, 1273
31491 2c 20 31 32 37 31 2c 20 31 32 36 31 2c 20 31 32  , 1271, 1261, 12
31492 35 34 2c 20 31 33 34 32 2c 0a 20 2f 2a 20 20 20  54, 1342,. /*   
31493 33 30 30 20 2a 2f 20 20 31 33 33 36 2c 20 31 33  300 */  1336, 13
31494 33 36 2c 20 31 33 30 33 2c 20 31 33 30 33 2c 20  36, 1303, 1303, 
31495 31 33 30 33 2c 20 31 33 30 33 2c 20 20 2d 37 30  1303, 1303,  -70
31496 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d  ,  -70,  -70,  -
31497 37 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  70,. /*   310 */
31498 20 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20 33     -70,  -70,  3
31499 33 34 2c 20 20 31 32 30 2c 20 20 35 33 35 2c 20  34,  120,  535, 
3149a 20 32 33 32 2c 20 20 36 32 34 2c 20 20 39 34 34   232,  624,  944
3149b 2c 20 20 31 38 38 2c 20 20 39 30 35 2c 0a 20 2f  ,  188,  905,. /
3149c 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 38 39 37  *   320 */   897
3149d 2c 20 20 38 37 33 2c 20 20 38 37 30 2c 20 20 38  ,  873,  870,  8
3149e 32 35 2c 20 20 37 35 34 2c 20 20 37 31 39 2c 20  25,  754,  719, 
3149f 20 36 35 31 2c 20 20 35 32 37 2c 20 20 34 34 34   651,  527,  444
314a0 2c 20 20 31 32 35 2c 0a 20 2f 2a 20 20 20 33 33  ,  125,. /*   33
314a1 30 20 2a 2f 20 20 20 35 31 34 2c 20 20 34 33 34  0 */   514,  434
314a2 2c 20 31 32 39 34 2c 20 31 32 37 39 2c 20 31 32  , 1294, 1279, 12
314a3 35 39 2c 20 31 31 34 39 2c 20 31 32 35 38 2c 20  59, 1149, 1258, 
314a4 31 31 38 38 2c 20 31 32 33 35 2c 20 31 32 33 38  1188, 1235, 1238
314a5 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
314a6 31 32 33 31 2c 20 31 31 30 35 2c 20 31 32 32 38  1231, 1105, 1228
314a7 2c 20 31 31 32 33 2c 20 31 31 33 34 2c 20 31 31  , 1123, 1134, 11
314a8 30 30 2c 20 31 32 31 30 2c 20 31 31 30 36 2c 20  00, 1210, 1106, 
314a9 31 31 39 38 2c 20 31 32 30 35 2c 0a 20 2f 2a 20  1198, 1205,. /* 
314aa 20 20 33 35 30 20 2a 2f 20 20 31 30 38 33 2c 20    350 */  1083, 
314ab 31 31 37 34 2c 20 31 30 35 37 2c 20 31 31 32 34  1174, 1057, 1124
314ac 2c 20 31 31 31 38 2c 20 31 30 37 35 2c 20 31 31  , 1118, 1075, 11
314ad 33 38 2c 20 31 31 33 39 2c 20 31 30 35 32 2c 20  38, 1139, 1052, 
314ae 31 31 36 33 2c 0a 20 2f 2a 20 20 20 33 36 30 20  1163,. /*   360 
314af 2a 2f 20 20 31 31 35 38 2c 20 31 31 33 33 2c 20  */  1158, 1133, 
314b0 31 30 35 30 2c 20 20 39 37 38 2c 20 31 30 39 39  1050,  978, 1099
314b1 2c 20 31 31 30 37 2c 20 31 30 38 39 2c 20 31 30  , 1107, 1089, 10
314b2 32 30 2c 20 31 30 33 34 2c 20 20 39 36 38 2c 0a  20, 1034,  968,.
314b3 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31 30   /*   370 */  10
314b4 33 39 2c 20 31 30 34 38 2c 20 20 39 39 31 2c 20  39, 1048,  991, 
314b5 20 38 39 39 2c 20 20 39 35 38 2c 20 20 39 38 31   899,  958,  981
314b6 2c 20 20 39 34 32 2c 20 20 39 37 36 2c 20 20 39  ,  942,  976,  9
314b7 37 34 2c 20 20 39 36 36 2c 0a 20 2f 2a 20 20 20  74,  966,. /*   
314b8 33 38 30 20 2a 2f 20 20 20 39 35 37 2c 20 20 39  380 */   957,  9
314b9 32 31 2c 20 20 39 30 30 2c 20 20 38 33 33 2c 20  21,  900,  833, 
314ba 20 38 36 33 2c 20 20 38 36 37 2c 20 20 38 33 39   863,  867,  839
314bb 2c 20 20 38 33 36 2c 20 20 37 33 35 2c 20 20 38  ,  836,  735,  8
314bc 32 32 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  22,. /*   390 */
314bd 20 20 20 38 30 33 2c 20 20 38 30 36 2c 20 20 37     803,  806,  7
314be 30 36 2c 20 20 36 38 34 2c 20 20 37 32 33 2c 20  06,  684,  723, 
314bf 20 37 33 30 2c 20 20 36 35 38 2c 20 20 36 38 34   730,  658,  684
314c0 2c 20 20 37 32 38 2c 20 20 36 39 30 2c 0a 20 2f  ,  728,  690,. /
314c1 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 36 37 38  *   400 */   678
314c2 2c 20 20 36 36 30 2c 20 20 36 35 35 2c 20 20 36  ,  660,  655,  6
314c3 37 39 2c 20 20 36 35 30 2c 20 20 36 31 36 2c 20  79,  650,  616, 
314c4 20 36 30 36 2c 20 20 35 37 31 2c 20 20 34 36 38   606,  571,  468
314c5 2c 20 20 33 38 39 2c 0a 20 2f 2a 20 20 20 34 31  ,  389,. /*   41
314c6 30 20 2a 2f 20 20 20 33 34 33 2c 20 20 32 39 34  0 */   343,  294
314c7 2c 20 20 31 38 36 2c 20 20 20 20 33 2c 20 20 20  ,  186,    3,   
314c8 34 30 2c 20 20 20 20 36 2c 20 20 20 2d 33 2c 20  40,    6,   -3, 
314c9 20 20 20 35 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65     5,.};.#define
314ca 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44   YY_REDUCE_USE_D
314cb 46 4c 54 20 28 2d 32 32 32 29 0a 23 64 65 66 69  FLT (-222).#defi
314cc 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 43 4f 55  ne YY_REDUCE_COU
314cd 4e 54 20 28 33 31 31 29 0a 23 64 65 66 69 6e 65  NT (311).#define
314ce 20 59 59 5f 52 45 44 55 43 45 5f 4d 49 4e 20 20   YY_REDUCE_MIN  
314cf 20 28 2d 32 32 31 29 0a 23 64 65 66 69 6e 65 20   (-221).#define 
314d0 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 20 20  YY_REDUCE_MAX   
314d1 28 31 33 37 37 29 0a 73 74 61 74 69 63 20 63 6f  (1377).static co
314d2 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65 64  nst short yy_red
314d3 75 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20  uce_ofst[] = {. 
314d4 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 34 39  /*     0 */   49
314d5 33 2c 20 31 30 39 32 2c 20 31 30 32 31 2c 20 20  3, 1092, 1021,  
314d6 31 34 37 2c 20 20 31 35 38 2c 20 20 31 35 35 2c  147,  158,  155,
314d7 20 20 20 38 36 2c 20 20 20 31 38 2c 20 20 20 38     86,   18,   8
314d8 31 2c 20 20 31 37 32 2c 0a 20 2f 2a 20 20 20 20  1,  172,. /*    
314d9 31 30 20 2a 2f 20 20 20 33 38 35 2c 20 20 33 37  10 */   385,  37
314da 37 2c 20 20 33 30 38 2c 20 20 33 37 39 2c 20 20  7,  308,  379,  
314db 32 39 37 2c 20 20 32 35 32 2c 20 20 2d 34 33 2c  297,  252,  -43,
314dc 20 2d 31 34 36 2c 20 31 32 34 30 2c 20 31 32 32   -146, 1240, 122
314dd 36 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  6,. /*    20 */ 
314de 20 31 32 32 34 2c 20 31 31 34 31 2c 20 31 31 33   1224, 1141, 113
314df 37 2c 20 31 30 39 37 2c 20 31 30 39 35 2c 20 31  7, 1097, 1095, 1
314e0 30 39 30 2c 20 31 30 33 30 2c 20 20 39 37 35 2c  090, 1030,  975,
314e1 20 20 39 36 34 2c 20 20 39 36 32 2c 0a 20 2f 2a    964,  962,. /*
314e2 20 20 20 20 33 30 20 2a 2f 20 20 20 39 30 38 2c      30 */   908,
314e3 20 20 38 39 30 2c 20 20 38 38 37 2c 20 20 38 37    890,  887,  87
314e4 34 2c 20 20 38 33 34 2c 20 20 38 31 36 2c 20 20  4,  834,  816,  
314e5 38 31 33 2c 20 20 38 30 30 2c 20 20 37 36 30 2c  813,  800,  760,
314e6 20 20 37 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30    758,. /*    40
314e7 20 2a 2f 20 20 20 37 34 32 2c 20 20 37 33 39 2c   */   742,  739,
314e8 20 20 37 32 36 2c 20 20 36 38 36 2c 20 20 36 37    726,  686,  67
314e9 32 2c 20 20 36 36 38 2c 20 20 36 36 35 2c 20 20  2,  668,  665,  
314ea 36 35 32 2c 20 20 36 31 31 2c 20 20 36 30 39 2c  652,  611,  609,
314eb 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20  . /*    50 */   
314ec 36 30 37 2c 20 20 35 39 31 2c 20 20 35 37 38 2c  607,  591,  578,
314ed 20 20 35 33 37 2c 20 20 35 32 36 2c 20 20 35 32    537,  526,  52
314ee 34 2c 20 20 35 32 30 2c 20 20 35 30 34 2c 20 20  4,  520,  504,  
314ef 34 35 36 2c 20 20 34 35 30 2c 0a 20 2f 2a 20 20  456,  450,. /*  
314f0 20 20 36 30 20 2a 2f 20 20 20 33 37 31 2c 20 2d    60 */   371, -
314f1 32 32 31 2c 20 20 34 37 34 2c 20 20 33 36 39 2c  221,  474,  369,
314f2 20 20 35 31 37 2c 20 20 33 39 35 2c 20 20 20 39    517,  395,   9
314f3 32 2c 20 20 33 30 31 2c 20 20 34 30 31 2c 20 20  2,  301,  401,  
314f4 31 31 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a  118,. /*    70 *
314f5 2f 20 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  /   118,  118,  
314f6 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
314f7 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
314f8 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 0a 20  8,  118,  118,. 
314f9 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 31 31  /*    80 */   11
314fa 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
314fb 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
314fc 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
314fd 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20 20 20 20  8,  118,. /*    
314fe 39 30 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 31  90 */   118,  11
314ff 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
31500 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
31501 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
31502 38 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20  8,. /*   100 */ 
31503 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31    118,  118,  11
31504 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
31505 31 31 38 2c 20 20 31 31 38 2c 20 20 32 30 38 2c  118,  118,  208,
31506 20 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a    118,  118,. /*
31507 20 20 20 31 31 30 20 2a 2f 20 20 31 30 33 38 2c     110 */  1038,
31508 20 20 39 39 34 2c 20 20 39 38 33 2c 20 20 39 36    994,  983,  96
31509 39 2c 20 20 38 39 32 2c 20 20 38 39 31 2c 20 20  9,  892,  891,  
3150a 38 38 38 2c 20 20 38 31 38 2c 20 20 37 36 31 2c  888,  818,  761,
3150b 20 20 38 33 32 2c 0a 20 2f 2a 20 20 20 31 32 30    832,. /*   120
3150c 20 2a 2f 20 20 20 36 31 33 2c 20 20 36 30 34 2c   */   613,  604,
3150d 20 20 35 32 33 2c 20 20 37 34 34 2c 20 20 38 33    523,  744,  83
3150e 30 2c 20 20 37 37 31 2c 20 20 35 39 35 2c 20 20  0,  771,  595,  
3150f 34 33 30 2c 20 20 37 34 30 2c 20 20 36 37 30 2c  430,  740,  670,
31510 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20  . /*   130 */   
31511 36 36 39 2c 20 20 36 36 36 2c 20 20 36 31 30 2c  669,  666,  610,
31512 20 20 35 37 39 2c 20 20 31 31 38 2c 20 20 31 31    579,  118,  11
31513 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  8,  118,  118,  
31514 31 31 38 2c 20 20 36 36 37 2c 0a 20 2f 2a 20 20  118,  667,. /*  
31515 20 31 34 30 20 2a 2f 20 20 20 39 37 32 2c 20 20   140 */   972,  
31516 38 38 30 2c 20 20 37 38 36 2c 20 20 39 39 36 2c  880,  786,  996,
31517 20 31 32 35 33 2c 20 31 32 34 37 2c 20 31 32 33   1253, 1247, 123
31518 37 2c 20 31 30 30 31 2c 20 20 37 35 30 2c 20 20  7, 1001,  750,  
31519 37 35 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a  750,. /*   150 *
3151a 2f 20 20 31 32 33 30 2c 20 31 32 31 32 2c 20 31  /  1230, 1212, 1
3151b 32 30 38 2c 20 31 32 30 33 2c 20 31 32 30 30 2c  208, 1203, 1200,
3151c 20 31 31 39 34 2c 20 31 31 38 39 2c 20 31 31 38   1194, 1189, 118
3151d 32 2c 20 31 31 38 31 2c 20 31 31 38 30 2c 0a 20  2, 1181, 1180,. 
3151e 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 31 31 37  /*   160 */  117
3151f 37 2c 20 31 31 36 30 2c 20 31 31 34 32 2c 20 31  7, 1160, 1142, 1
31520 31 33 30 2c 20 31 31 32 39 2c 20 31 31 32 38 2c  130, 1129, 1128,
31521 20 31 31 32 35 2c 20 31 31 30 39 2c 20 31 30 39   1125, 1109, 109
31522 38 2c 20 31 30 39 33 2c 0a 20 2f 2a 20 20 20 31  8, 1093,. /*   1
31523 37 30 20 2a 2f 20 20 31 30 37 39 2c 20 31 30 36  70 */  1079, 106
31524 33 2c 20 31 30 34 33 2c 20 31 30 34 32 2c 20 31  3, 1043, 1042, 1
31525 30 32 32 2c 20 31 30 30 36 2c 20 20 39 39 36 2c  022, 1006,  996,
31526 20 20 39 39 33 2c 20 20 39 37 30 2c 20 20 39 30    993,  970,  90
31527 34 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  4,. /*   180 */ 
31528 20 20 33 38 32 2c 20 20 33 37 38 2c 20 20 38 38    382,  378,  88
31529 36 2c 20 20 39 31 30 2c 20 20 38 39 33 2c 20 20  6,  910,  893,  
3152a 38 38 31 2c 20 20 38 34 31 2c 20 20 38 38 34 2c  881,  841,  884,
3152b 20 20 38 31 32 2c 20 20 38 31 31 2c 0a 20 2f 2a    812,  811,. /*
3152c 20 20 20 31 39 30 20 2a 2f 20 20 20 38 31 30 2c     190 */   810,
3152d 20 20 35 33 39 2c 20 20 36 39 36 2c 20 20 33 35    539,  696,  35
3152e 38 2c 20 31 33 35 34 2c 20 31 33 36 35 2c 20 31  8, 1354, 1365, 1
3152f 33 36 34 2c 20 31 33 35 31 2c 20 31 33 35 33 2c  364, 1351, 1353,
31530 20 31 33 35 32 2c 0a 20 2f 2a 20 20 20 32 30 30   1352,. /*   200
31531 20 2a 2f 20 20 31 33 32 30 2c 20 31 33 33 38 2c   */  1320, 1338,
31532 20 31 33 34 33 2c 20 31 33 33 38 2c 20 31 33 33   1343, 1338, 133
31533 38 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 20 31  8, 1338, 1338, 1
31534 33 33 38 2c 20 31 33 33 38 2c 20 31 33 33 38 2c  338, 1338, 1338,
31535 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 31  . /*   210 */  1
31536 32 39 35 2c 20 31 32 39 35 2c 20 31 33 33 35 2c  295, 1295, 1335,
31537 20 31 33 33 34 2c 20 31 33 32 30 2c 20 31 33 36   1334, 1320, 136
31538 31 2c 20 31 33 33 30 2c 20 31 33 37 37 2c 20 31  1, 1330, 1377, 1
31539 33 32 35 2c 20 31 33 31 34 2c 0a 20 2f 2a 20 20  325, 1314,. /*  
3153a 20 32 32 30 20 2a 2f 20 20 31 33 31 33 2c 20 31   220 */  1313, 1
3153b 32 37 39 2c 20 31 33 32 31 2c 20 31 32 37 37 2c  279, 1321, 1277,
3153c 20 31 33 34 30 2c 20 31 33 35 31 2c 20 31 33 33   1340, 1351, 133
3153d 39 2c 20 31 33 34 39 2c 20 31 33 33 37 2c 20 31  9, 1349, 1337, 1
3153e 33 33 31 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a  331,. /*   230 *
3153f 2f 20 20 31 33 33 35 2c 20 31 33 30 32 2c 20 31  /  1335, 1302, 1
31540 33 33 33 2c 20 31 33 33 32 2c 20 31 32 38 30 2c  333, 1332, 1280,
31541 20 31 32 37 34 2c 20 31 32 37 33 2c 20 31 33 33   1274, 1273, 133
31542 39 2c 20 31 33 30 36 2c 20 31 33 30 37 2c 0a 20  9, 1306, 1307,. 
31543 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 33 34  /*   240 */  134
31544 39 2c 20 31 32 35 34 2c 20 31 33 34 32 2c 20 31  9, 1254, 1342, 1
31545 33 34 31 2c 20 31 32 35 34 2c 20 31 32 35 32 2c  341, 1254, 1252,
31546 20 31 33 33 37 2c 20 31 32 37 34 2c 20 31 33 30   1337, 1274, 130
31547 33 2c 20 31 32 39 32 2c 0a 20 2f 2a 20 20 20 32  3, 1292,. /*   2
31548 35 30 20 2a 2f 20 20 31 32 39 38 2c 20 31 32 39  50 */  1298, 129
31549 37 2c 20 31 32 39 36 2c 20 31 32 39 31 2c 20 31  7, 1296, 1291, 1
3154a 33 32 35 2c 20 31 32 38 32 2c 20 31 32 36 30 2c  325, 1282, 1260,
3154b 20 31 32 38 39 2c 20 31 32 38 38 2c 20 31 33 31   1289, 1288, 131
3154c 39 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20  9,. /*   260 */ 
3154d 20 31 33 31 37 2c 20 31 32 33 36 2c 20 31 32 33   1317, 1236, 123
3154e 34 2c 20 31 33 31 34 2c 20 31 33 31 33 2c 20 31  4, 1314, 1313, 1
3154f 33 31 32 2c 20 31 33 31 30 2c 20 31 33 30 39 2c  312, 1310, 1309,
31550 20 31 33 30 37 2c 20 31 33 30 36 2c 0a 20 2f 2a   1307, 1306,. /*
31551 20 20 20 32 37 30 20 2a 2f 20 20 31 32 37 36 2c     270 */  1276,
31552 20 31 32 38 35 2c 20 31 32 38 31 2c 20 31 32 38   1285, 1281, 128
31553 30 2c 20 31 32 37 34 2c 20 31 32 32 39 2c 20 31  0, 1274, 1229, 1
31554 32 32 33 2c 20 31 32 36 36 2c 20 31 32 37 32 2c  223, 1266, 1272,
31555 20 31 32 36 39 2c 0a 20 2f 2a 20 20 20 32 38 30   1269,. /*   280
31556 20 2a 2f 20 20 31 32 36 38 2c 20 31 32 35 37 2c   */  1268, 1257,
31557 20 31 32 36 34 2c 20 31 32 35 36 2c 20 31 32 32   1264, 1256, 122
31558 37 2c 20 31 32 32 35 2c 20 31 32 32 32 2c 20 31  7, 1225, 1222, 1
31559 32 32 31 2c 20 31 32 31 36 2c 20 31 32 31 35 2c  221, 1216, 1215,
3155a 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31  . /*   290 */  1
3155b 32 36 37 2c 20 31 32 36 33 2c 20 31 31 39 36 2c  267, 1263, 1196,
3155c 20 31 32 31 33 2c 20 31 32 31 31 2c 20 31 31 39   1213, 1211, 119
3155d 33 2c 20 31 31 39 32 2c 20 31 31 38 35 2c 20 31  3, 1192, 1185, 1
3155e 31 36 37 2c 20 31 31 39 37 2c 0a 20 2f 2a 20 20  167, 1197,. /*  
3155f 20 33 30 30 20 2a 2f 20 20 31 32 30 39 2c 20 31   300 */  1209, 1
31560 32 30 31 2c 20 31 32 30 32 2c 20 31 31 39 39 2c  201, 1202, 1199,
31561 20 31 31 39 31 2c 20 31 31 37 39 2c 20 31 30 36   1191, 1179, 106
31562 36 2c 20 31 30 36 35 2c 20 31 30 35 36 2c 20 31  6, 1065, 1056, 1
31563 31 34 36 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a  146,. /*   310 *
31564 2f 20 20 31 31 32 31 2c 20 31 31 33 36 2c 0a 7d  /  1121, 1136,.}
31565 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  ;.static const Y
31566 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 64  YACTIONTYPE yy_d
31567 65 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a  efault[] = {. /*
31568 20 20 20 20 20 30 20 2a 2f 20 20 20 36 33 34 2c       0 */   634,
31569 20 20 38 36 39 2c 20 20 39 35 38 2c 20 20 39 35    869,  958,  95
3156a 38 2c 20 20 39 35 38 2c 20 20 38 36 39 2c 20 20  8,  958,  869,  
3156b 38 39 38 2c 20 20 38 39 38 2c 20 20 39 35 38 2c  898,  898,  958,
3156c 20 20 37 35 37 2c 0a 20 2f 2a 20 20 20 20 31 30    757,. /*    10
3156d 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
3156e 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 36    958,  958,  86
3156f 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  7,  958,  958,  
31570 39 33 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  932,  958,  958,
31571 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20  . /*    20 */   
31572 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31573 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31574 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31575 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
31576 20 20 33 30 20 2a 2f 20 20 20 39 35 38 2c 20 20    30 */   958,  
31577 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31578 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31579 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3157a 39 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a  958,. /*    40 *
3157b 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
3157c 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3157d 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3157e 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
3157f 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39 35  /*    50 */   95
31580 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31581 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31582 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31583 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20  8,  958,. /*    
31584 36 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 34  60 */   958,  84
31585 31 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  1,  958,  958,  
31586 39 35 38 2c 20 20 38 39 38 2c 20 20 38 39 38 2c  958,  898,  898,
31587 20 20 36 37 33 2c 20 20 37 36 31 2c 20 20 37 39    673,  761,  79
31588 32 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  2,. /*    70 */ 
31589 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3158a 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3158b 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3158c 20 20 39 33 31 2c 20 20 39 33 33 2c 0a 20 2f 2a    931,  933,. /*
3158d 20 20 20 20 38 30 20 2a 2f 20 20 20 38 30 37 2c      80 */   807,
3158e 20 20 38 30 36 2c 20 20 38 30 30 2c 20 20 37 39    806,  800,  79
3158f 39 2c 20 20 39 31 31 2c 20 20 37 37 32 2c 20 20  9,  911,  772,  
31590 37 39 37 2c 20 20 37 39 30 2c 20 20 37 38 33 2c  797,  790,  783,
31591 20 20 37 39 34 2c 0a 20 2f 2a 20 20 20 20 39 30    794,. /*    90
31592 20 2a 2f 20 20 20 38 37 30 2c 20 20 38 36 33 2c   */   870,  863,
31593 20 20 38 36 34 2c 20 20 38 36 32 2c 20 20 38 36    864,  862,  86
31594 36 2c 20 20 38 37 31 2c 20 20 39 35 38 2c 20 20  6,  871,  958,  
31595 37 39 33 2c 20 20 38 32 39 2c 20 20 38 34 37 2c  793,  829,  847,
31596 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20  . /*   100 */   
31597 38 32 38 2c 20 20 38 34 36 2c 20 20 38 35 33 2c  828,  846,  853,
31598 20 20 38 34 35 2c 20 20 38 33 31 2c 20 20 38 34    845,  831,  84
31599 30 2c 20 20 38 33 30 2c 20 20 36 36 35 2c 20 20  0,  830,  665,  
3159a 38 33 32 2c 20 20 38 33 33 2c 0a 20 2f 2a 20 20  832,  833,. /*  
3159b 20 31 31 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   110 */   958,  
3159c 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3159d 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3159e 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3159f 39 35 38 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a  958,. /*   120 *
315a0 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
315a1 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315a2 20 20 39 35 38 2c 20 20 36 36 30 2c 20 20 37 32    958,  660,  72
315a3 36 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  6,  958,  958,. 
315a4 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 39 35  /*   130 */   95
315a5 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315a6 39 35 38 2c 20 20 38 33 34 2c 20 20 38 33 35 2c  958,  834,  835,
315a7 20 20 38 35 30 2c 20 20 38 34 39 2c 20 20 38 34    850,  849,  84
315a8 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31  8,  958,. /*   1
315a9 34 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  40 */   958,  95
315aa 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315ab 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315ac 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
315ad 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20  8,. /*   150 */ 
315ae 20 20 39 35 38 2c 20 20 39 33 38 2c 20 20 39 33    958,  938,  93
315af 36 2c 20 20 39 35 38 2c 20 20 38 38 32 2c 20 20  6,  958,  882,  
315b0 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315b1 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
315b2 20 20 20 31 36 30 20 2a 2f 20 20 20 39 35 38 2c     160 */   958,
315b3 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
315b4 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315b5 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315b6 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 37 30    958,. /*   170
315b7 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
315b8 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
315b9 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315ba 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315bb 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20  . /*   180 */   
315bc 39 35 38 2c 20 20 36 34 30 2c 20 20 39 35 38 2c  958,  640,  958,
315bd 20 20 37 35 37 2c 20 20 37 35 37 2c 20 20 37 35    757,  757,  75
315be 37 2c 20 20 36 33 34 2c 20 20 39 35 38 2c 20 20  7,  634,  958,  
315bf 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
315c0 20 31 39 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   190 */   958,  
315c1 39 35 30 2c 20 20 37 36 31 2c 20 20 37 35 31 2c  950,  761,  751,
315c2 20 20 37 31 37 2c 20 20 39 35 38 2c 20 20 39 35    717,  958,  95
315c3 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315c4 39 35 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a  958,. /*   200 *
315c5 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
315c6 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315c7 20 20 39 35 38 2c 20 20 38 30 32 2c 20 20 37 34    958,  802,  74
315c8 30 2c 20 20 39 32 31 2c 20 20 39 32 33 2c 0a 20  0,  921,  923,. 
315c9 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 39 35  /*   210 */   95
315ca 38 2c 20 20 39 30 34 2c 20 20 37 33 38 2c 20 20  8,  904,  738,  
315cb 36 36 32 2c 20 20 37 35 39 2c 20 20 36 37 35 2c  662,  759,  675,
315cc 20 20 37 34 39 2c 20 20 36 34 32 2c 20 20 37 39    749,  642,  79
315cd 36 2c 20 20 37 37 34 2c 0a 20 2f 2a 20 20 20 32  6,  774,. /*   2
315ce 32 30 20 2a 2f 20 20 20 37 37 34 2c 20 20 39 31  20 */   774,  91
315cf 36 2c 20 20 37 39 36 2c 20 20 39 31 36 2c 20 20  6,  796,  916,  
315d0 36 39 39 2c 20 20 39 35 38 2c 20 20 37 38 36 2c  699,  958,  786,
315d1 20 20 39 35 38 2c 20 20 37 38 36 2c 20 20 36 39    958,  786,  69
315d2 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  6,. /*   230 */ 
315d3 20 20 37 38 36 2c 20 20 37 37 34 2c 20 20 37 38    786,  774,  78
315d4 36 2c 20 20 37 38 36 2c 20 20 38 36 35 2c 20 20  6,  786,  865,  
315d5 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315d6 20 20 37 35 38 2c 20 20 37 34 39 2c 0a 20 2f 2a    758,  749,. /*
315d7 20 20 20 32 34 30 20 2a 2f 20 20 20 39 35 38 2c     240 */   958,
315d8 20 20 39 34 33 2c 20 20 37 36 35 2c 20 20 37 36    943,  765,  76
315d9 35 2c 20 20 39 33 35 2c 20 20 39 33 35 2c 20 20  5,  935,  935,  
315da 37 36 35 2c 20 20 38 30 38 2c 20 20 37 33 30 2c  765,  808,  730,
315db 20 20 37 39 36 2c 0a 20 2f 2a 20 20 20 32 35 30    796,. /*   250
315dc 20 2a 2f 20 20 20 37 33 37 2c 20 20 37 33 37 2c   */   737,  737,
315dd 20 20 37 33 37 2c 20 20 37 33 37 2c 20 20 37 36    737,  737,  76
315de 35 2c 20 20 37 39 36 2c 20 20 38 30 38 2c 20 20  5,  796,  808,  
315df 37 33 30 2c 20 20 37 33 30 2c 20 20 37 36 35 2c  730,  730,  765,
315e0 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20  . /*   260 */   
315e1 36 35 37 2c 20 20 39 31 30 2c 20 20 39 30 38 2c  657,  910,  908,
315e2 20 20 37 36 35 2c 20 20 37 36 35 2c 20 20 36 35    765,  765,  65
315e3 37 2c 20 20 37 36 35 2c 20 20 36 35 37 2c 20 20  7,  765,  657,  
315e4 37 36 35 2c 20 20 36 35 37 2c 0a 20 2f 2a 20 20  765,  657,. /*  
315e5 20 32 37 30 20 2a 2f 20 20 20 38 37 35 2c 20 20   270 */   875,  
315e6 37 32 38 2c 20 20 37 32 38 2c 20 20 37 32 38 2c  728,  728,  728,
315e7 20 20 37 31 33 2c 20 20 38 37 39 2c 20 20 38 37    713,  879,  87
315e8 39 2c 20 20 38 37 35 2c 20 20 37 32 38 2c 20 20  9,  875,  728,  
315e9 36 39 39 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a  699,. /*   280 *
315ea 2f 20 20 20 37 32 38 2c 20 20 37 31 33 2c 20 20  /   728,  713,  
315eb 37 32 38 2c 20 20 37 32 38 2c 20 20 37 37 38 2c  728,  728,  778,
315ec 20 20 37 37 33 2c 20 20 37 37 38 2c 20 20 37 37    773,  778,  77
315ed 33 2c 20 20 37 37 38 2c 20 20 37 37 33 2c 0a 20  3,  778,  773,. 
315ee 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 37 36  /*   290 */   76
315ef 35 2c 20 20 37 36 35 2c 20 20 39 35 38 2c 20 20  5,  765,  958,  
315f0 37 39 31 2c 20 20 37 37 39 2c 20 20 37 38 39 2c  791,  779,  789,
315f1 20 20 37 38 37 2c 20 20 37 39 36 2c 20 20 39 35    787,  796,  95
315f2 38 2c 20 20 37 31 36 2c 0a 20 2f 2a 20 20 20 33  8,  716,. /*   3
315f3 30 30 20 2a 2f 20 20 20 36 35 30 2c 20 20 36 35  00 */   650,  65
315f4 30 2c 20 20 36 33 39 2c 20 20 36 33 39 2c 20 20  0,  639,  639,  
315f5 36 33 39 2c 20 20 36 33 39 2c 20 20 39 35 35 2c  639,  639,  955,
315f6 20 20 39 35 35 2c 20 20 39 35 30 2c 20 20 37 30    955,  950,  70
315f7 31 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20  1,. /*   310 */ 
315f8 20 20 37 30 31 2c 20 20 36 38 33 2c 20 20 39 35    701,  683,  95
315f9 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315fa 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
315fb 20 20 38 38 34 2c 20 20 39 35 38 2c 0a 20 2f 2a    884,  958,. /*
315fc 20 20 20 33 32 30 20 2a 2f 20 20 20 39 35 38 2c     320 */   958,
315fd 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
315fe 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
315ff 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31600 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 33 30    958,. /*   330
31601 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
31602 20 20 39 35 38 2c 20 20 36 33 35 2c 20 20 39 34    958,  635,  94
31603 35 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  5,  958,  958,  
31604 39 34 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  942,  958,  958,
31605 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20  . /*   340 */   
31606 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31607 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31608 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31609 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
3160a 20 33 35 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   350 */   958,  
3160b 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3160c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3160d 38 2c 20 20 39 35 38 2c 20 20 39 31 34 2c 20 20  8,  958,  914,  
3160e 39 35 38 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a  958,. /*   360 *
3160f 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
31610 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31611 20 20 39 30 37 2c 20 20 39 30 36 2c 20 20 39 35    907,  906,  95
31612 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
31613 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 39 35  /*   370 */   95
31614 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31615 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31616 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31617 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33  8,  958,. /*   3
31618 38 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  80 */   958,  95
31619 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3161a 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3161b 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3161c 38 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  8,. /*   390 */ 
3161d 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3161e 38 2c 20 20 37 38 38 2c 20 20 39 35 38 2c 20 20  8,  788,  958,  
3161f 37 38 30 2c 20 20 39 35 38 2c 20 20 38 36 38 2c  780,  958,  868,
31620 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
31621 20 20 20 34 30 30 20 2a 2f 20 20 20 39 35 38 2c     400 */   958,
31622 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31623 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31624 39 35 38 2c 20 20 39 35 38 2c 20 20 37 34 33 2c  958,  958,  743,
31625 20 20 38 31 37 2c 0a 20 2f 2a 20 20 20 34 31 30    817,. /*   410
31626 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 31 36 2c   */   958,  816,
31627 20 20 38 32 30 2c 20 20 38 31 35 2c 20 20 36 36    820,  815,  66
31628 37 2c 20 20 39 35 38 2c 20 20 36 34 38 2c 20 20  7,  958,  648,  
31629 39 35 38 2c 20 20 36 33 31 2c 20 20 36 33 36 2c  958,  631,  636,
3162a 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20  . /*   420 */   
3162b 39 35 34 2c 20 20 39 35 37 2c 20 20 39 35 36 2c  954,  957,  956,
3162c 20 20 39 35 33 2c 20 20 39 35 32 2c 20 20 39 35    953,  952,  95
3162d 31 2c 20 20 39 34 36 2c 20 20 39 34 34 2c 20 20  1,  946,  944,  
3162e 39 34 31 2c 20 20 39 34 30 2c 0a 20 2f 2a 20 20  941,  940,. /*  
3162f 20 34 33 30 20 2a 2f 20 20 20 39 33 39 2c 20 20   430 */   939,  
31630 39 33 37 2c 20 20 39 33 34 2c 20 20 39 33 30 2c  937,  934,  930,
31631 20 20 38 38 38 2c 20 20 38 38 36 2c 20 20 38 39    888,  886,  89
31632 33 2c 20 20 38 39 32 2c 20 20 38 39 31 2c 20 20  3,  892,  891,  
31633 38 39 30 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a  890,. /*   440 *
31634 2f 20 20 20 38 38 39 2c 20 20 38 38 37 2c 20 20  /   889,  887,  
31635 38 38 35 2c 20 20 38 38 33 2c 20 20 38 30 33 2c  885,  883,  803,
31636 20 20 38 30 31 2c 20 20 37 39 38 2c 20 20 37 39    801,  798,  79
31637 35 2c 20 20 39 32 39 2c 20 20 38 38 31 2c 0a 20  5,  929,  881,. 
31638 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 37 33  /*   450 */   73
31639 39 2c 20 20 37 33 36 2c 20 20 37 33 35 2c 20 20  9,  736,  735,  
3163a 36 35 36 2c 20 20 39 34 37 2c 20 20 39 31 33 2c  656,  947,  913,
3163b 20 20 39 32 32 2c 20 20 39 32 30 2c 20 20 38 30    922,  920,  80
3163c 39 2c 20 20 39 31 39 2c 0a 20 2f 2a 20 20 20 34  9,  919,. /*   4
3163d 36 30 20 2a 2f 20 20 20 39 31 38 2c 20 20 39 31  60 */   918,  91
3163e 37 2c 20 20 39 31 35 2c 20 20 39 31 32 2c 20 20  7,  915,  912,  
3163f 38 39 39 2c 20 20 38 30 35 2c 20 20 38 30 34 2c  899,  805,  804,
31640 20 20 37 33 31 2c 20 20 38 37 33 2c 20 20 38 37    731,  873,  87
31641 32 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20  2,. /*   470 */ 
31642 20 20 36 35 39 2c 20 20 39 30 33 2c 20 20 39 30    659,  903,  90
31643 32 2c 20 20 39 30 31 2c 20 20 39 30 35 2c 20 20  2,  901,  905,  
31644 39 30 39 2c 20 20 39 30 30 2c 20 20 37 36 37 2c  909,  900,  767,
31645 20 20 36 35 38 2c 20 20 36 35 35 2c 0a 20 2f 2a    658,  655,. /*
31646 20 20 20 34 38 30 20 2a 2f 20 20 20 36 36 34 2c     480 */   664,
31647 20 20 37 32 30 2c 20 20 37 31 39 2c 20 20 37 32    720,  719,  72
31648 37 2c 20 20 37 32 35 2c 20 20 37 32 34 2c 20 20  7,  725,  724,  
31649 37 32 33 2c 20 20 37 32 32 2c 20 20 37 32 31 2c  723,  722,  721,
3164a 20 20 37 31 38 2c 0a 20 2f 2a 20 20 20 34 39 30    718,. /*   490
3164b 20 2a 2f 20 20 20 36 36 36 2c 20 20 36 37 34 2c   */   666,  674,
3164c 20 20 36 38 35 2c 20 20 37 31 32 2c 20 20 36 39    685,  712,  69
3164d 38 2c 20 20 36 39 37 2c 20 20 38 37 38 2c 20 20  8,  697,  878,  
3164e 38 38 30 2c 20 20 38 37 37 2c 20 20 38 37 36 2c  880,  877,  876,
3164f 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20  . /*   500 */   
31650 37 30 35 2c 20 20 37 31 30 2c 20 20 37 30 39 2c  705,  710,  709,
31651 20 20 37 30 38 2c 20 20 37 30 37 2c 20 20 37 30    708,  707,  70
31652 36 2c 20 20 37 30 34 2c 20 20 37 30 33 2c 20 20  6,  704,  703,  
31653 37 30 32 2c 20 20 36 39 35 2c 0a 20 2f 2a 20 20  702,  695,. /*  
31654 20 35 31 30 20 2a 2f 20 20 20 36 39 34 2c 20 20   510 */   694,  
31655 37 30 30 2c 20 20 36 39 33 2c 20 20 37 31 35 2c  700,  693,  715,
31656 20 20 37 31 34 2c 20 20 37 31 31 2c 20 20 36 39    714,  711,  69
31657 32 2c 20 20 37 33 34 2c 20 20 37 33 33 2c 20 20  2,  734,  733,  
31658 37 33 32 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a  732,. /*   520 *
31659 2f 20 20 20 37 32 39 2c 20 20 36 39 31 2c 20 20  /   729,  691,  
3165a 36 39 30 2c 20 20 36 38 39 2c 20 20 38 32 30 2c  690,  689,  820,
3165b 20 20 36 38 38 2c 20 20 36 38 37 2c 20 20 38 32    688,  687,  82
3165c 36 2c 20 20 38 32 35 2c 20 20 38 31 33 2c 0a 20  6,  825,  813,. 
3165d 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 38 35  /*   530 */   85
3165e 37 2c 20 20 37 35 34 2c 20 20 37 35 33 2c 20 20  7,  754,  753,  
3165f 37 35 32 2c 20 20 37 36 34 2c 20 20 37 36 33 2c  752,  764,  763,
31660 20 20 37 37 36 2c 20 20 37 37 35 2c 20 20 38 31    776,  775,  81
31661 31 2c 20 20 38 31 30 2c 0a 20 2f 2a 20 20 20 35  1,  810,. /*   5
31662 34 30 20 2a 2f 20 20 20 37 37 37 2c 20 20 37 36  40 */   777,  76
31663 32 2c 20 20 37 35 36 2c 20 20 37 35 35 2c 20 20  2,  756,  755,  
31664 37 37 31 2c 20 20 37 37 30 2c 20 20 37 36 39 2c  771,  770,  769,
31665 20 20 37 36 38 2c 20 20 37 36 30 2c 20 20 37 35    768,  760,  75
31666 30 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20  0,. /*   550 */ 
31667 20 20 37 38 32 2c 20 20 37 38 35 2c 20 20 37 38    782,  785,  78
31668 34 2c 20 20 37 38 31 2c 20 20 38 34 32 2c 20 20  4,  781,  842,  
31669 38 35 39 2c 20 20 37 36 36 2c 20 20 38 35 36 2c  859,  766,  856,
3166a 20 20 39 32 38 2c 20 20 39 32 37 2c 0a 20 2f 2a    928,  927,. /*
3166b 20 20 20 35 36 30 20 2a 2f 20 20 20 39 32 36 2c     560 */   926,
3166c 20 20 39 32 35 2c 20 20 39 32 34 2c 20 20 38 36    925,  924,  86
3166d 31 2c 20 20 38 36 30 2c 20 20 38 32 37 2c 20 20  1,  860,  827,  
3166e 38 32 34 2c 20 20 36 37 38 2c 20 20 36 37 39 2c  824,  678,  679,
3166f 20 20 38 39 37 2c 0a 20 2f 2a 20 20 20 35 37 30    897,. /*   570
31670 20 2a 2f 20 20 20 38 39 35 2c 20 20 38 39 36 2c   */   895,  896,
31671 20 20 38 39 34 2c 20 20 36 38 31 2c 20 20 36 38    894,  681,  68
31672 30 2c 20 20 36 37 37 2c 20 20 36 37 36 2c 20 20  0,  677,  676,  
31673 38 35 38 2c 20 20 37 34 35 2c 20 20 37 34 34 2c  858,  745,  744,
31674 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20  . /*   580 */   
31675 38 35 34 2c 20 20 38 35 31 2c 20 20 38 34 33 2c  854,  851,  843,
31676 20 20 38 33 38 2c 20 20 38 35 35 2c 20 20 38 35    838,  855,  85
31677 32 2c 20 20 38 34 34 2c 20 20 38 33 39 2c 20 20  2,  844,  839,  
31678 38 33 37 2c 20 20 38 33 36 2c 0a 20 2f 2a 20 20  837,  836,. /*  
31679 20 35 39 30 20 2a 2f 20 20 20 38 32 32 2c 20 20   590 */   822,  
3167a 38 32 31 2c 20 20 38 31 39 2c 20 20 38 31 38 2c  821,  819,  818,
3167b 20 20 38 31 34 2c 20 20 38 32 33 2c 20 20 36 36    814,  823,  66
3167c 39 2c 20 20 37 34 36 2c 20 20 37 34 32 2c 20 20  9,  746,  742,  
3167d 37 34 31 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a  741,. /*   600 *
3167e 2f 20 20 20 38 31 32 2c 20 20 37 34 38 2c 20 20  /   812,  748,  
3167f 37 34 37 2c 20 20 36 38 36 2c 20 20 36 38 34 2c  747,  686,  684,
31680 20 20 36 38 32 2c 20 20 36 36 33 2c 20 20 36 36    682,  663,  66
31681 31 2c 20 20 36 35 34 2c 20 20 36 35 32 2c 0a 20  1,  654,  652,. 
31682 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 36 35  /*   610 */   65
31683 31 2c 20 20 36 35 33 2c 20 20 36 34 39 2c 20 20  1,  653,  649,  
31684 36 34 37 2c 20 20 36 34 36 2c 20 20 36 34 35 2c  647,  646,  645,
31685 20 20 36 34 34 2c 20 20 36 34 33 2c 20 20 36 37    644,  643,  67
31686 32 2c 20 20 36 37 31 2c 0a 20 2f 2a 20 20 20 36  2,  671,. /*   6
31687 32 30 20 2a 2f 20 20 20 36 37 30 2c 20 20 36 36  20 */   670,  66
31688 38 2c 20 20 36 36 37 2c 20 20 36 34 31 2c 20 20  8,  667,  641,  
31689 36 33 38 2c 20 20 36 33 37 2c 20 20 36 33 33 2c  638,  637,  633,
3168a 20 20 36 33 32 2c 20 20 36 33 30 2c 0a 7d 3b 0a    632,  630,.};.
3168b 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61 62  ./* The next tab
3168c 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20 69  le maps tokens i
3168d 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b  nto fallback tok
3168e 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73 74  ens.  If a const
3168f 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68 65  ruct.** like the
31690 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a   following:.** .
31691 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61 63  **      %fallbac
31692 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a  k ID X Y Z..**.*
31693 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  * appears in the
31694 20 67 72 61 6d 6d 61 72 2c 20 74 68 65 6e 20 49   grammar, then I
31695 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c 6c  D becomes a fall
31696 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20 58  back token for X
31697 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20  , Y,.** and Z.  
31698 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66 20  Whenever one of 
31699 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59 2c  the tokens X, Y,
3169a 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20 74   or Z is input t
3169b 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20  o the parser.** 
3169c 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  but it does not 
3169d 70 61 72 73 65 2c 20 74 68 65 20 74 79 70 65 20  parse, the type 
3169e 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20  of the token is 
3169f 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61 6e  changed to ID an
316a0 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 69  d.** the parse i
316a1 73 20 72 65 74 72 69 65 64 20 62 65 66 6f 72 65  s retried before
316a2 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68 72   an error is thr
316a3 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59  own..*/.#ifdef Y
316a4 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69 63  YFALLBACK.static
316a5 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50   const YYCODETYP
316a6 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d  E yyFallback[] =
316a7 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20   {.    0,  /*   
316a8 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74 68         $ => noth
316a9 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
316aa 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e 20  *       SEMI => 
316ab 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36  nothing */.   26
316ac 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49 4e  ,  /*    EXPLAIN
316ad 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
316ae 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59 20    /*      QUERY 
316af 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
316b0 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20 3d   /*       PLAN =
316b1 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
316b2 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d 3e  /*      BEGIN =>
316b3 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f   ID */.    0,  /
316b4 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d 3e  * TRANSACTION =>
316b5 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
316b6 36 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52 45  6,  /*   DEFERRE
316b7 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  D => ID */.   26
316b8 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54 45  ,  /*  IMMEDIATE
316b9 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
316ba 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45 20    /*  EXCLUSIVE 
316bb 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20  => ID */.    0, 
316bc 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20 3d   /*     COMMIT =
316bd 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
316be 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 45  26,  /*        E
316bf 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ND => ID */.   2
316c0 36 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41 43  6,  /*   ROLLBAC
316c1 4b 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  K => ID */.   26
316c2 2c 20 20 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54  ,  /*  SAVEPOINT
316c3 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
316c4 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53 45 20    /*    RELEASE 
316c5 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20  => ID */.    0, 
316c6 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f 20 3d   /*         TO =
316c7 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
316c8 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 54 41 42   0,  /*      TAB
316c9 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  LE => nothing */
316ca 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
316cb 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e  CREATE => nothin
316cc 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
316cd 20 20 20 20 20 20 20 20 49 46 20 3d 3e 20 49 44          IF => ID
316ce 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
316cf 20 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74        NOT => not
316d0 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
316d1 2f 2a 20 20 20 20 20 45 58 49 53 54 53 20 3d 3e  /*     EXISTS =>
316d2 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
316d3 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 45 4d  6,  /*       TEM
316d4 50 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  P => ID */.    0
316d5 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c 50  ,  /*         LP
316d6 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
316d7 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
316d8 20 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    RP => nothing 
316d9 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
316da 20 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f 74 68        AS => noth
316db 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
316dc 2a 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d 3e 20  *      COMMA => 
316dd 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
316de 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 44  ,  /*         ID
316df 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
316e0 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e 44     0,  /*    IND
316e1 45 58 45 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  EXED => nothing 
316e2 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
316e3 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a     ABORT => ID *
316e4 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
316e5 20 41 43 54 49 4f 4e 20 3d 3e 20 49 44 20 2a 2f   ACTION => ID */
316e6 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
316e7 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f 0a   AFTER => ID */.
316e8 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 41 4e     26,  /*    AN
316e9 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  ALYZE => ID */. 
316ea 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
316eb 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   ASC => ID */.  
316ec 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 41 54 54   26,  /*     ATT
316ed 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACH => ID */.   
316ee 32 36 2c 20 20 2f 2a 20 20 20 20 20 42 45 46 4f  26,  /*     BEFO
316ef 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  RE => ID */.   2
316f0 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 42  6,  /*         B
316f1 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  Y => ID */.   26
316f2 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41 44 45  ,  /*    CASCADE
316f3 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
316f4 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53 54 20    /*       CAST 
316f5 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
316f6 20 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d   /*   COLUMNKW =
316f7 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
316f8 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e  /*   CONFLICT =>
316f9 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
316fa 2a 20 20 20 44 41 54 41 42 41 53 45 20 3d 3e 20  *   DATABASE => 
316fb 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
316fc 20 20 20 20 20 20 20 44 45 53 43 20 3d 3e 20 49         DESC => I
316fd 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
316fe 20 20 20 20 44 45 54 41 43 48 20 3d 3e 20 49 44      DETACH => ID
316ff 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
31700 20 20 20 20 20 45 41 43 48 20 3d 3e 20 49 44 20       EACH => ID 
31701 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
31702 20 20 20 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a      FAIL => ID *
31703 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
31704 20 20 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f      FOR => ID */
31705 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
31706 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a  IGNORE => ID */.
31707 20 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e 49 54     26,  /*  INIT
31708 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20  IALLY => ID */. 
31709 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 49 4e 53    26,  /*    INS
3170a 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TEAD => ID */.  
3170b 20 32 36 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45   26,  /*    LIKE
3170c 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  _KW => ID */.   
3170d 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 4d 41 54  26,  /*      MAT
3170e 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CH => ID */.   2
3170f 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4e  6,  /*         N
31710 4f 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  O => ID */.   26
31711 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4b 45 59  ,  /*        KEY
31712 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31713 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 46 20    /*         OF 
31714 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
31715 20 2f 2a 20 20 20 20 20 4f 46 46 53 45 54 20 3d   /*     OFFSET =
31716 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
31717 2f 2a 20 20 20 20 20 50 52 41 47 4d 41 20 3d 3e  /*     PRAGMA =>
31718 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
31719 2a 20 20 20 20 20 20 52 41 49 53 45 20 3d 3e 20  *      RAISE => 
3171a 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3171b 20 20 20 20 52 45 50 4c 41 43 45 20 3d 3e 20 49      REPLACE => I
3171c 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3171d 20 20 52 45 53 54 52 49 43 54 20 3d 3e 20 49 44    RESTRICT => ID
3171e 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3171f 20 20 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20        ROW => ID 
31720 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
31721 20 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a   TRIGGER => ID *
31722 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
31723 20 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f   VACUUM => ID */
31724 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
31725 20 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a    VIEW => ID */.
31726 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 56 49     26,  /*    VI
31727 52 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20  RTUAL => ID */. 
31728 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45 49    26,  /*    REI
31729 4e 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  NDEX => ID */.  
3172a 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e   26,  /*     REN
3172b 41 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  AME => ID */.   
3172c 32 36 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f  26,  /*   CTIME_
3172d 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 7d 3b 0a 23  KW => ID */.};.#
3172e 65 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42  endif /* YYFALLB
3172f 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66  ACK */../* The f
31730 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
31731 72 65 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  re represents a 
31732 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f  single element o
31733 66 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27  f the.** parser'
31734 73 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d  s stack.  Inform
31735 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63  ation stored inc
31736 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b  ludes:.**.**   +
31737 20 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62    The state numb
31738 65 72 20 66 6f 72 20 74 68 65 20 70 61 72 73 65  er for the parse
31739 72 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  r at this level 
3173a 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
3173b 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c  .**   +  The val
3173c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  ue of the token 
3173d 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c  stored at this l
3173e 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63  evel of the stac
3173f 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f  k..**      (In o
31740 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
31741 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a  "major" token.).
31742 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73  **.**   +  The s
31743 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74  emantic value st
31744 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76  ored at this lev
31745 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
31746 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20    This is.**    
31747 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f    the informatio
31748 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 61 63  n used by the ac
31749 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e  tion routines in
3174a 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a   the grammar..**
3174b 20 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65        It is some
3174c 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65  times called the
3174d 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a   "minor" token..
3174e 2a 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61 63  */.struct yyStac
3174f 6b 45 6e 74 72 79 20 7b 0a 20 20 59 59 41 43 54  kEntry {.  YYACT
31750 49 4f 4e 54 59 50 45 20 73 74 61 74 65 6e 6f 3b  IONTYPE stateno;
31751 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 2d 6e    /* The state-n
31752 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44  umber */.  YYCOD
31753 45 54 59 50 45 20 6d 61 6a 6f 72 3b 20 20 20 20  ETYPE major;    
31754 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
31755 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69  oken value.  Thi
31756 73 20 69 73 20 74 68 65 20 63 6f 64 65 0a 20 20  s is the code.  
31757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31758 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72         ** number
31759 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 61   for the token a
3175a 74 20 74 68 69 73 20 73 74 61 63 6b 20 6c 65 76  t this stack lev
3175b 65 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54  el */.  YYMINORT
3175c 59 50 45 20 6d 69 6e 6f 72 3b 20 20 20 20 20 2f  YPE minor;     /
3175d 2a 20 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c  * The user-suppl
3175e 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20  ied minor token 
3175f 76 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20  value.  This.   
31760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31761 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20        ** is the 
31762 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
31763 65 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65  en  */.};.typede
31764 66 20 73 74 72 75 63 74 20 79 79 53 74 61 63 6b  f struct yyStack
31765 45 6e 74 72 79 20 79 79 53 74 61 63 6b 45 6e 74  Entry yyStackEnt
31766 72 79 3b 0a 0a 2f 2a 20 54 68 65 20 73 74 61 74  ry;../* The stat
31767 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20  e of the parser 
31768 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f  is completely co
31769 6e 74 61 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e  ntained in an in
3176a 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65  stance of.** the
3176b 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
3176c 74 75 72 65 20 2a 2f 0a 73 74 72 75 63 74 20 79  ture */.struct y
3176d 79 50 61 72 73 65 72 20 7b 0a 20 20 69 6e 74 20  yParser {.  int 
3176e 79 79 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  yyidx;          
3176f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
31770 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e  ex of top elemen
31771 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0a 23 69  t in stack */.#i
31772 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
31773 54 41 43 4b 44 45 50 54 48 0a 20 20 69 6e 74 20  TACKDEPTH.  int 
31774 79 79 69 64 78 4d 61 78 3b 20 20 20 20 20 20 20  yyidxMax;       
31775 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
31776 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 79 79  imum value of yy
31777 69 64 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  idx */.#endif.  
31778 69 6e 74 20 79 79 65 72 72 63 6e 74 3b 20 20 20  int yyerrcnt;   
31779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3177a 20 53 68 69 66 74 73 20 6c 65 66 74 20 62 65 66   Shifts left bef
3177b 6f 72 65 20 6f 75 74 20 6f 66 20 74 68 65 20 65  ore out of the e
3177c 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rror */.  sqlite
3177d 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c  3ParserARG_SDECL
3177e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3177f 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 68 6f  /* A place to ho
31780 6c 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ld %extra_argume
31781 6e 74 20 2a 2f 0a 23 69 66 20 59 59 53 54 41 43  nt */.#if YYSTAC
31782 4b 44 45 50 54 48 3c 3d 30 0a 20 20 69 6e 74 20  KDEPTH<=0.  int 
31783 79 79 73 74 6b 73 7a 3b 20 20 20 20 20 20 20 20  yystksz;        
31784 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
31785 72 65 6e 74 20 73 69 64 65 20 6f 66 20 74 68 65  rent side of the
31786 20 73 74 61 63 6b 20 2a 2f 0a 20 20 79 79 53 74   stack */.  yySt
31787 61 63 6b 45 6e 74 72 79 20 2a 79 79 73 74 61 63  ackEntry *yystac
31788 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  k;        /* The
31789 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
3178a 2a 2f 0a 23 65 6c 73 65 0a 20 20 79 79 53 74 61  */.#else.  yySta
3178b 63 6b 45 6e 74 72 79 20 79 79 73 74 61 63 6b 5b  ckEntry yystack[
3178c 59 59 53 54 41 43 4b 44 45 50 54 48 5d 3b 20 20  YYSTACKDEPTH];  
3178d 2f 2a 20 54 68 65 20 70 61 72 73 65 72 27 73 20  /* The parser's 
3178e 73 74 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a  stack */.#endif.
3178f 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
31790 74 20 79 79 50 61 72 73 65 72 20 79 79 50 61 72  t yyParser yyPar
31791 73 65 72 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ser;..#ifndef ND
31792 45 42 55 47 0a 73 74 61 74 69 63 20 46 49 4c 45  EBUG.static FILE
31793 20 2a 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20   *yyTraceFILE = 
31794 30 3b 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  0;.static char *
31795 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20  yyTracePrompt = 
31796 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45  0;.#endif /* NDE
31797 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BUG */..#ifndef 
31798 4e 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75  NDEBUG./* .** Tu
31799 72 6e 20 70 61 72 73 65 72 20 74 72 61 63 69 6e  rn parser tracin
3179a 67 20 6f 6e 20 62 79 20 67 69 76 69 6e 67 20 61  g on by giving a
3179b 20 73 74 72 65 61 6d 20 74 6f 20 77 68 69 63 68   stream to which
3179c 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 74 72   to write the tr
3179d 61 63 65 0a 2a 2a 20 61 6e 64 20 61 20 70 72 6f  ace.** and a pro
3179e 6d 70 74 20 74 6f 20 70 72 65 66 61 63 65 20 65  mpt to preface e
3179f 61 63 68 20 74 72 61 63 65 20 6d 65 73 73 61 67  ach trace messag
317a0 65 2e 20 20 54 72 61 63 69 6e 67 20 69 73 20 74  e.  Tracing is t
317a1 75 72 6e 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20  urned off.** by 
317a2 6d 61 6b 69 6e 67 20 65 69 74 68 65 72 20 61 72  making either ar
317a3 67 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a  gument NULL .**.
317a4 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75  ** Inputs:.** <u
317a5 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 46 49 4c  l>.** <li> A FIL
317a6 45 2a 20 74 6f 20 77 68 69 63 68 20 74 72 61 63  E* to which trac
317a7 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20  e output should 
317a8 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 20  be written..**  
317a9 20 20 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65      If NULL, the
317aa 6e 20 74 72 61 63 69 6e 67 20 69 73 20 74 75 72  n tracing is tur
317ab 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e  ned off..** <li>
317ac 20 41 20 70 72 65 66 69 78 20 73 74 72 69 6e 67   A prefix string
317ad 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20   written at the 
317ae 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65  beginning of eve
317af 72 79 0a 2a 2a 20 20 20 20 20 20 6c 69 6e 65 20  ry.**      line 
317b0 6f 66 20 74 72 61 63 65 20 6f 75 74 70 75 74 2e  of trace output.
317b1 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20    If NULL, then 
317b2 74 72 61 63 69 6e 67 20 69 73 0a 2a 2a 20 20 20  tracing is.**   
317b3 20 20 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a     turned off..*
317b4 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75  * </ul>.**.** Ou
317b5 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a  tputs:.** None..
317b6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
317b7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
317b8 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a  rserTrace(FILE *
317b9 54 72 61 63 65 46 49 4c 45 2c 20 63 68 61 72 20  TraceFILE, char 
317ba 2a 7a 54 72 61 63 65 50 72 6f 6d 70 74 29 7b 0a  *zTracePrompt){.
317bb 20 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20    yyTraceFILE = 
317bc 54 72 61 63 65 46 49 4c 45 3b 0a 20 20 79 79 54  TraceFILE;.  yyT
317bd 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 7a 54 72  racePrompt = zTr
317be 61 63 65 50 72 6f 6d 70 74 3b 0a 20 20 69 66 28  acePrompt;.  if(
317bf 20 79 79 54 72 61 63 65 46 49 4c 45 3d 3d 30 20   yyTraceFILE==0 
317c0 29 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20  ) yyTracePrompt 
317c1 3d 20 30 3b 0a 20 20 65 6c 73 65 20 69 66 28 20  = 0;.  else if( 
317c2 79 79 54 72 61 63 65 50 72 6f 6d 70 74 3d 3d 30  yyTracePrompt==0
317c3 20 29 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d   ) yyTraceFILE =
317c4 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
317c5 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64  NDEBUG */..#ifnd
317c6 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72  ef NDEBUG./* For
317c7 20 74 72 61 63 69 6e 67 20 73 68 69 66 74 73 2c   tracing shifts,
317c8 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
317c9 6c 20 74 65 72 6d 69 6e 61 6c 73 20 61 6e 64 20  l terminals and 
317ca 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20  nonterminals.** 
317cb 61 72 65 20 72 65 71 75 69 72 65 64 2e 20 20 54  are required.  T
317cc 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
317cd 6c 65 20 73 75 70 70 6c 69 65 73 20 74 68 65 73  le supplies thes
317ce 65 20 6e 61 6d 65 73 20 2a 2f 0a 73 74 61 74 69  e names */.stati
317cf 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
317d0 6e 73 74 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  nst yyTokenName[
317d1 5d 20 3d 20 7b 20 0a 20 20 22 24 22 2c 20 20 20  ] = { .  "$",   
317d2 20 20 20 20 20 20 20 20 20 20 22 53 45 4d 49 22            "SEMI"
317d3 2c 20 20 20 20 20 20 20 20 20 20 22 45 58 50 4c  ,          "EXPL
317d4 41 49 4e 22 2c 20 20 20 20 20 20 20 22 51 55 45  AIN",       "QUE
317d5 52 59 22 2c 20 20 20 20 20 20 20 0a 20 20 22 50  RY",       .  "P
317d6 4c 41 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  LAN",          "
317d7 42 45 47 49 4e 22 2c 20 20 20 20 20 20 20 20 20  BEGIN",         
317d8 22 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20  "TRANSACTION",  
317d9 20 22 44 45 46 45 52 52 45 44 22 2c 20 20 20 20   "DEFERRED",    
317da 0a 20 20 22 49 4d 4d 45 44 49 41 54 45 22 2c 20  .  "IMMEDIATE", 
317db 20 20 20 20 22 45 58 43 4c 55 53 49 56 45 22 2c      "EXCLUSIVE",
317dc 20 20 20 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20       "COMMIT",  
317dd 20 20 20 20 20 20 22 45 4e 44 22 2c 20 20 20 20        "END",    
317de 20 20 20 20 20 0a 20 20 22 52 4f 4c 4c 42 41 43       .  "ROLLBAC
317df 4b 22 2c 20 20 20 20 20 20 22 53 41 56 45 50 4f  K",      "SAVEPO
317e0 49 4e 54 22 2c 20 20 20 20 20 22 52 45 4c 45 41  INT",     "RELEA
317e1 53 45 22 2c 20 20 20 20 20 20 20 22 54 4f 22 2c  SE",       "TO",
317e2 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 54 41            .  "TA
317e3 42 4c 45 22 2c 20 20 20 20 20 20 20 20 20 22 43  BLE",         "C
317e4 52 45 41 54 45 22 2c 20 20 20 20 20 20 20 20 22  REATE",        "
317e5 49 46 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  IF",            
317e6 22 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 20 0a  "NOT",         .
317e7 20 20 22 45 58 49 53 54 53 22 2c 20 20 20 20 20    "EXISTS",     
317e8 20 20 20 22 54 45 4d 50 22 2c 20 20 20 20 20 20     "TEMP",      
317e9 20 20 20 20 22 4c 50 22 2c 20 20 20 20 20 20 20      "LP",       
317ea 20 20 20 20 20 22 52 50 22 2c 20 20 20 20 20 20       "RP",      
317eb 20 20 20 20 0a 20 20 22 41 53 22 2c 20 20 20 20      .  "AS",    
317ec 20 20 20 20 20 20 20 20 22 43 4f 4d 4d 41 22 2c          "COMMA",
317ed 20 20 20 20 20 20 20 20 20 22 49 44 22 2c 20 20           "ID",  
317ee 20 20 20 20 20 20 20 20 20 20 22 49 4e 44 45 58            "INDEX
317ef 45 44 22 2c 20 20 20 20 20 0a 20 20 22 41 42 4f  ED",     .  "ABO
317f0 52 54 22 2c 20 20 20 20 20 20 20 20 20 22 41 43  RT",         "AC
317f1 54 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 22 41  TION",        "A
317f2 46 54 45 52 22 2c 20 20 20 20 20 20 20 20 20 22  FTER",         "
317f3 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20 0a 20  ANALYZE",     . 
317f4 20 22 41 53 43 22 2c 20 20 20 20 20 20 20 20 20   "ASC",         
317f5 20 20 22 41 54 54 41 43 48 22 2c 20 20 20 20 20    "ATTACH",     
317f6 20 20 20 22 42 45 46 4f 52 45 22 2c 20 20 20 20     "BEFORE",    
317f7 20 20 20 20 22 42 59 22 2c 20 20 20 20 20 20 20      "BY",       
317f8 20 20 20 0a 20 20 22 43 41 53 43 41 44 45 22 2c     .  "CASCADE",
317f9 20 20 20 20 20 20 20 22 43 41 53 54 22 2c 20 20         "CAST",  
317fa 20 20 20 20 20 20 20 20 22 43 4f 4c 55 4d 4e 4b          "COLUMNK
317fb 57 22 2c 20 20 20 20 20 20 22 43 4f 4e 46 4c 49  W",      "CONFLI
317fc 43 54 22 2c 20 20 20 20 0a 20 20 22 44 41 54 41  CT",    .  "DATA
317fd 42 41 53 45 22 2c 20 20 20 20 20 20 22 44 45 53  BASE",      "DES
317fe 43 22 2c 20 20 20 20 20 20 20 20 20 20 22 44 45  C",          "DE
317ff 54 41 43 48 22 2c 20 20 20 20 20 20 20 20 22 45  TACH",        "E
31800 41 43 48 22 2c 20 20 20 20 20 20 20 20 0a 20 20  ACH",        .  
31801 22 46 41 49 4c 22 2c 20 20 20 20 20 20 20 20 20  "FAIL",         
31802 20 22 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20   "FOR",         
31803 20 20 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20    "IGNORE",     
31804 20 20 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20     "INITIALLY", 
31805 20 20 0a 20 20 22 49 4e 53 54 45 41 44 22 2c 20    .  "INSTEAD", 
31806 20 20 20 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c        "LIKE_KW",
31807 20 20 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20         "MATCH", 
31808 20 20 20 20 20 20 20 20 22 4e 4f 22 2c 20 20 20          "NO",   
31809 20 20 20 20 20 20 20 0a 20 20 22 4b 45 59 22 2c         .  "KEY",
3180a 20 20 20 20 20 20 20 20 20 20 20 22 4f 46 22 2c             "OF",
3180b 20 20 20 20 20 20 20 20 20 20 20 20 22 4f 46 46              "OFF
3180c 53 45 54 22 2c 20 20 20 20 20 20 20 20 22 50 52  SET",        "PR
3180d 41 47 4d 41 22 2c 20 20 20 20 20 20 0a 20 20 22  AGMA",      .  "
3180e 52 41 49 53 45 22 2c 20 20 20 20 20 20 20 20 20  RAISE",         
3180f 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20 20 20  "REPLACE",      
31810 20 22 52 45 53 54 52 49 43 54 22 2c 20 20 20 20   "RESTRICT",    
31811 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20 20 20    "ROW",        
31812 20 0a 20 20 22 54 52 49 47 47 45 52 22 2c 20 20   .  "TRIGGER",  
31813 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c 20 20       "VACUUM",  
31814 20 20 20 20 20 20 22 56 49 45 57 22 2c 20 20 20        "VIEW",   
31815 20 20 20 20 20 20 20 22 56 49 52 54 55 41 4c 22         "VIRTUAL"
31816 2c 20 20 20 20 20 0a 20 20 22 52 45 49 4e 44 45  ,     .  "REINDE
31817 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e 41 4d  X",       "RENAM
31818 45 22 2c 20 20 20 20 20 20 20 20 22 43 54 49 4d  E",        "CTIM
31819 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41 4e 59  E_KW",      "ANY
3181a 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 4f  ",         .  "O
3181b 52 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  R",            "
3181c 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20 20 20  AND",           
3181d 22 49 53 22 2c 20 20 20 20 20 20 20 20 20 20 20  "IS",           
3181e 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20 20 20   "BETWEEN",     
3181f 0a 20 20 22 49 4e 22 2c 20 20 20 20 20 20 20 20  .  "IN",        
31820 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20      "ISNULL",   
31821 20 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20       "NOTNULL", 
31822 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20 20 20        "NE",     
31823 20 20 20 20 20 0a 20 20 22 45 51 22 2c 20 20 20       .  "EQ",   
31824 20 20 20 20 20 20 20 20 20 22 47 54 22 2c 20 20           "GT",  
31825 20 20 20 20 20 20 20 20 20 20 22 4c 45 22 2c 20            "LE", 
31826 20 20 20 20 20 20 20 20 20 20 20 22 4c 54 22 2c             "LT",
31827 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 47 45            .  "GE
31828 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 45  ",            "E
31829 53 43 41 50 45 22 2c 20 20 20 20 20 20 20 20 22  SCAPE",        "
3182a 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20 20 20  BITAND",        
3182b 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20 20 0a  "BITOR",       .
3182c 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20 20 20    "LSHIFT",     
3182d 20 20 20 22 52 53 48 49 46 54 22 2c 20 20 20 20     "RSHIFT",    
3182e 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20 20 20      "PLUS",     
3182f 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20 20 20       "MINUS",   
31830 20 20 20 20 0a 20 20 22 53 54 41 52 22 2c 20 20      .  "STAR",  
31831 20 20 20 20 20 20 20 20 22 53 4c 41 53 48 22 2c          "SLASH",
31832 20 20 20 20 20 20 20 20 20 22 52 45 4d 22 2c 20           "REM", 
31833 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e 43 41            "CONCA
31834 54 22 2c 20 20 20 20 20 20 0a 20 20 22 43 4f 4c  T",      .  "COL
31835 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22 42 49  LATE",       "BI
31836 54 4e 4f 54 22 2c 20 20 20 20 20 20 20 20 22 53  TNOT",        "S
31837 54 52 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  TRING",        "
31838 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 0a 20  JOIN_KW",     . 
31839 20 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20   "CONSTRAINT",  
3183a 20 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20    "DEFAULT",    
3183b 20 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20     "NULL",      
3183c 20 20 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20      "PRIMARY",  
3183d 20 20 20 0a 20 20 22 55 4e 49 51 55 45 22 2c 20     .  "UNIQUE", 
3183e 20 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20         "CHECK", 
3183f 20 20 20 20 20 20 20 20 22 52 45 46 45 52 45 4e          "REFEREN
31840 43 45 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e  CES",    "AUTOIN
31841 43 52 22 2c 20 20 20 20 0a 20 20 22 4f 4e 22 2c  CR",    .  "ON",
31842 20 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c              "DEL
31843 45 54 45 22 2c 20 20 20 20 20 20 20 20 22 55 50  ETE",        "UP
31844 44 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 53  DATE",        "S
31845 45 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  ET",         .  
31846 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 20  "DEFERRABLE",   
31847 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20 20   "FOREIGN",     
31848 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20 20    "DROP",       
31849 20 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20 20     "UNION",     
3184a 20 20 0a 20 20 22 41 4c 4c 22 2c 20 20 20 20 20    .  "ALL",     
3184b 20 20 20 20 20 20 22 45 58 43 45 50 54 22 2c 20        "EXCEPT", 
3184c 20 20 20 20 20 20 20 22 49 4e 54 45 52 53 45 43         "INTERSEC
3184d 54 22 2c 20 20 20 20 20 22 53 45 4c 45 43 54 22  T",     "SELECT"
3184e 2c 20 20 20 20 20 20 0a 20 20 22 44 49 53 54 49  ,      .  "DISTI
3184f 4e 43 54 22 2c 20 20 20 20 20 20 22 44 4f 54 22  NCT",      "DOT"
31850 2c 20 20 20 20 20 20 20 20 20 20 20 22 46 52 4f  ,           "FRO
31851 4d 22 2c 20 20 20 20 20 20 20 20 20 20 22 4a 4f  M",          "JO
31852 49 4e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  IN",        .  "
31853 55 53 49 4e 47 22 2c 20 20 20 20 20 20 20 20 20  USING",         
31854 22 4f 52 44 45 52 22 2c 20 20 20 20 20 20 20 20  "ORDER",        
31855 20 22 47 52 4f 55 50 22 2c 20 20 20 20 20 20 20   "GROUP",       
31856 20 20 22 48 41 56 49 4e 47 22 2c 20 20 20 20 20    "HAVING",     
31857 20 0a 20 20 22 4c 49 4d 49 54 22 2c 20 20 20 20   .  "LIMIT",    
31858 20 20 20 20 20 22 57 48 45 52 45 22 2c 20 20 20       "WHERE",   
31859 20 20 20 20 20 20 22 49 4e 54 4f 22 2c 20 20 20        "INTO",   
3185a 20 20 20 20 20 20 20 22 56 41 4c 55 45 53 22 2c         "VALUES",
3185b 20 20 20 20 20 20 0a 20 20 22 49 4e 53 45 52 54        .  "INSERT
3185c 22 2c 20 20 20 20 20 20 20 20 22 49 4e 54 45 47  ",        "INTEG
3185d 45 52 22 2c 20 20 20 20 20 20 20 22 46 4c 4f 41  ER",       "FLOA
3185e 54 22 2c 20 20 20 20 20 20 20 20 20 22 42 4c 4f  T",         "BLO
3185f 42 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 52  B",        .  "R
31860 45 47 49 53 54 45 52 22 2c 20 20 20 20 20 20 22  EGISTER",      "
31861 56 41 52 49 41 42 4c 45 22 2c 20 20 20 20 20 20  VARIABLE",      
31862 22 43 41 53 45 22 2c 20 20 20 20 20 20 20 20 20  "CASE",         
31863 20 22 57 48 45 4e 22 2c 20 20 20 20 20 20 20 20   "WHEN",        
31864 0a 20 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20  .  "THEN",      
31865 20 20 20 20 22 45 4c 53 45 22 2c 20 20 20 20 20      "ELSE",     
31866 20 20 20 20 20 22 49 4e 44 45 58 22 2c 20 20 20       "INDEX",   
31867 20 20 20 20 20 20 22 41 4c 54 45 52 22 2c 20 20        "ALTER",  
31868 20 20 20 20 20 0a 20 20 22 41 44 44 22 2c 20 20       .  "ADD",  
31869 20 20 20 20 20 20 20 20 20 22 65 72 72 6f 72 22           "error"
3186a 2c 20 20 20 20 20 20 20 20 20 22 69 6e 70 75 74  ,         "input
3186b 22 2c 20 20 20 20 20 20 20 20 20 22 63 6d 64 6c  ",         "cmdl
3186c 69 73 74 22 2c 20 20 20 20 20 0a 20 20 22 65 63  ist",     .  "ec
3186d 6d 64 22 2c 20 20 20 20 20 20 20 20 20 20 22 65  md",          "e
3186e 78 70 6c 61 69 6e 22 2c 20 20 20 20 20 20 20 22  xplain",       "
3186f 63 6d 64 78 22 2c 20 20 20 20 20 20 20 20 20 20  cmdx",          
31870 22 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20 0a  "cmd",         .
31871 20 20 22 74 72 61 6e 73 74 79 70 65 22 2c 20 20    "transtype",  
31872 20 20 20 22 74 72 61 6e 73 5f 6f 70 74 22 2c 20     "trans_opt", 
31873 20 20 20 20 22 6e 6d 22 2c 20 20 20 20 20 20 20      "nm",       
31874 20 20 20 20 20 22 73 61 76 65 70 6f 69 6e 74 5f       "savepoint_
31875 6f 70 74 22 2c 0a 20 20 22 63 72 65 61 74 65 5f  opt",.  "create_
31876 74 61 62 6c 65 22 2c 20 20 22 63 72 65 61 74 65  table",  "create
31877 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 20 20 22  _table_args",  "
31878 63 72 65 61 74 65 6b 77 22 2c 20 20 20 20 20 20  createkw",      
31879 22 74 65 6d 70 22 2c 20 20 20 20 20 20 20 20 0a  "temp",        .
3187a 20 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22 2c    "ifnotexists",
3187b 20 20 20 22 64 62 6e 6d 22 2c 20 20 20 20 20 20     "dbnm",      
3187c 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22      "columnlist"
3187d 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f  ,    "conslist_o
3187e 70 74 22 2c 0a 20 20 22 73 65 6c 65 63 74 22 2c  pt",.  "select",
3187f 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 22          "column"
31880 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e  ,        "column
31881 69 64 22 2c 20 20 20 20 20 20 22 74 79 70 65 22  id",      "type"
31882 2c 20 20 20 20 20 20 20 20 0a 20 20 22 63 61 72  ,        .  "car
31883 67 6c 69 73 74 22 2c 20 20 20 20 20 20 22 69 64  glist",      "id
31884 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 69  ",            "i
31885 64 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  ds",           "
31886 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20 0a 20  typetoken",   . 
31887 20 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20 20   "typename",    
31888 20 20 22 73 69 67 6e 65 64 22 2c 20 20 20 20 20    "signed",     
31889 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20     "plus_num",  
3188a 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c      "minus_num",
3188b 20 20 20 0a 20 20 22 63 61 72 67 22 2c 20 20 20     .  "carg",   
3188c 20 20 20 20 20 20 20 22 63 63 6f 6e 73 22 2c 20         "ccons", 
3188d 20 20 20 20 20 20 20 20 22 74 65 72 6d 22 2c 20          "term", 
3188e 20 20 20 20 20 20 20 20 20 22 65 78 70 72 22 2c           "expr",
3188f 20 20 20 20 20 20 20 20 0a 20 20 22 6f 6e 63 6f          .  "onco
31890 6e 66 22 2c 20 20 20 20 20 20 20 20 22 73 6f 72  nf",        "sor
31891 74 6f 72 64 65 72 22 2c 20 20 20 20 20 22 61 75  torder",     "au
31892 74 6f 69 6e 63 22 2c 20 20 20 20 20 20 20 22 69  toinc",       "i
31893 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20 0a 20 20  dxlist_opt", .  
31894 22 72 65 66 61 72 67 73 22 2c 20 20 20 20 20 20  "refargs",      
31895 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
31896 65 22 2c 20 20 22 72 65 66 61 72 67 22 2c 20 20  e",  "refarg",  
31897 20 20 20 20 20 20 22 72 65 66 61 63 74 22 2c 20        "refact", 
31898 20 20 20 20 20 0a 20 20 22 69 6e 69 74 5f 64 65       .  "init_de
31899 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22  ferred_pred_opt"
3189a 2c 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20  ,  "conslist",  
3189b 20 20 20 20 22 74 63 6f 6e 73 22 2c 20 20 20 20      "tcons",    
3189c 20 20 20 20 20 22 69 64 78 6c 69 73 74 22 2c 20       "idxlist", 
3189d 20 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75      .  "defer_su
3189e 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 20 20 22  bclause_opt",  "
3189f 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20  orconf",        
318a0 22 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 20 20  "resolvetype",  
318a1 20 22 72 61 69 73 65 74 79 70 65 22 2c 20 20 20   "raisetype",   
318a2 0a 20 20 22 69 66 65 78 69 73 74 73 22 2c 20 20  .  "ifexists",  
318a3 20 20 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20      "fullname", 
318a4 20 20 20 20 20 22 6f 6e 65 73 65 6c 65 63 74 22       "oneselect"
318a5 2c 20 20 20 20 20 22 6d 75 6c 74 69 73 65 6c 65  ,     "multisele
318a6 63 74 5f 6f 70 22 2c 0a 20 20 22 64 69 73 74 69  ct_op",.  "disti
318a7 6e 63 74 22 2c 20 20 20 20 20 20 22 73 65 6c 63  nct",      "selc
318a8 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f  ollist",    "fro
318a9 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 77 68  m",          "wh
318aa 65 72 65 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22  ere_opt",   .  "
318ab 67 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20 20  groupby_opt",   
318ac 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20  "having_opt",   
318ad 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20   "orderby_opt", 
318ae 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20    "limit_opt",  
318af 20 0a 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20   .  "sclp",     
318b0 20 20 20 20 20 22 61 73 22 2c 20 20 20 20 20 20       "as",      
318b1 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73        "seltablis
318b2 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66  t",    "stl_pref
318b3 69 78 22 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f 70  ix",  .  "joinop
318b4 22 2c 20 20 20 20 20 20 20 20 22 69 6e 64 65 78  ",        "index
318b5 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f 6f  ed_opt",   "on_o
318b6 70 74 22 2c 20 20 20 20 20 20 20 20 22 75 73 69  pt",        "usi
318b7 6e 67 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 6a  ng_opt",   .  "j
318b8 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20 20 20 22  oinop2",       "
318b9 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20  inscollist",    
318ba 22 73 6f 72 74 6c 69 73 74 22 2c 20 20 20 20 20  "sortlist",     
318bb 20 22 73 6f 72 74 69 74 65 6d 22 2c 20 20 20 20   "sortitem",    
318bc 0a 20 20 22 6e 65 78 70 72 6c 69 73 74 22 2c 20  .  "nexprlist", 
318bd 20 20 20 20 22 73 65 74 6c 69 73 74 22 2c 20 20      "setlist",  
318be 20 20 20 20 20 22 69 6e 73 65 72 74 5f 63 6d 64       "insert_cmd
318bf 22 2c 20 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73  ",    "inscollis
318c0 74 5f 6f 70 74 22 2c 0a 20 20 22 69 74 65 6d 6c  t_opt",.  "iteml
318c1 69 73 74 22 2c 20 20 20 20 20 20 22 65 78 70 72  ist",      "expr
318c2 6c 69 73 74 22 2c 20 20 20 20 20 20 22 6c 69 6b  list",      "lik
318c3 65 6f 70 22 2c 20 20 20 20 20 20 20 20 22 65 73  eop",        "es
318c4 63 61 70 65 22 2c 20 20 20 20 20 20 0a 20 20 22  cape",      .  "
318c5 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20 20 20  between_op",    
318c6 22 69 6e 5f 6f 70 22 2c 20 20 20 20 20 20 20 20  "in_op",        
318c7 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 22 2c   "case_operand",
318c8 20 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74    "case_exprlist
318c9 22 2c 0a 20 20 22 63 61 73 65 5f 65 6c 73 65 22  ",.  "case_else"
318ca 2c 20 20 20 20 20 22 75 6e 69 71 75 65 66 6c 61  ,     "uniquefla
318cb 67 22 2c 20 20 20 20 22 63 6f 6c 6c 61 74 65 22  g",    "collate"
318cc 2c 20 20 20 20 20 20 20 22 6e 6d 6e 75 6d 22 2c  ,       "nmnum",
318cd 20 20 20 20 20 20 20 0a 20 20 22 70 6c 75 73 5f         .  "plus_
318ce 6f 70 74 22 2c 20 20 20 20 20 20 22 6e 75 6d 62  opt",      "numb
318cf 65 72 22 2c 20 20 20 20 20 20 20 20 22 74 72 69  er",        "tri
318d0 67 67 65 72 5f 64 65 63 6c 22 2c 20 20 22 74 72  gger_decl",  "tr
318d1 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 22 2c  igger_cmd_list",
318d2 0a 20 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65  .  "trigger_time
318d3 22 2c 20 20 22 74 72 69 67 67 65 72 5f 65 76 65  ",  "trigger_eve
318d4 6e 74 22 2c 20 20 22 66 6f 72 65 61 63 68 5f 63  nt",  "foreach_c
318d5 6c 61 75 73 65 22 2c 20 20 22 77 68 65 6e 5f 63  lause",  "when_c
318d6 6c 61 75 73 65 22 2c 20 0a 20 20 22 74 72 69 67  lause", .  "trig
318d7 67 65 72 5f 63 6d 64 22 2c 20 20 20 22 74 72 6e  ger_cmd",   "trn
318d8 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 74 72  m",          "tr
318d9 69 64 78 62 79 22 2c 20 20 20 20 20 20 20 22 64  idxby",       "d
318da 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c  atabase_kw_opt",
318db 0a 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20  .  "key_opt",   
318dc 20 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f      "add_column_
318dd 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 22 6b 77 63  fullname",  "kwc
318de 6f 6c 75 6d 6e 5f 6f 70 74 22 2c 20 20 22 63 72  olumn_opt",  "cr
318df 65 61 74 65 5f 76 74 61 62 22 2c 20 0a 20 20 22  eate_vtab", .  "
318e0 76 74 61 62 61 72 67 6c 69 73 74 22 2c 20 20 20  vtabarglist",   
318e1 22 76 74 61 62 61 72 67 22 2c 20 20 20 20 20 20  "vtabarg",      
318e2 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c   "vtabargtoken",
318e3 20 20 22 6c 70 22 2c 20 20 20 20 20 20 20 20 20    "lp",         
318e4 20 0a 20 20 22 61 6e 79 6c 69 73 74 22 2c 20 20   .  "anylist",  
318e5 20 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a     .};.#endif /*
318e6 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e   NDEBUG */..#ifn
318e7 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f  def NDEBUG./* Fo
318e8 72 20 74 72 61 63 69 6e 67 20 72 65 64 75 63 65  r tracing reduce
318e9 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61   actions, the na
318ea 6d 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73  mes of all rules
318eb 20 61 72 65 20 72 65 71 75 69 72 65 64 2e 0a 2a   are required..*
318ec 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
318ed 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c  har *const yyRul
318ee 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  eName[] = {. /* 
318ef 20 20 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a    0 */ "input ::
318f0 3d 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20  = cmdlist",. /* 
318f1 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20    1 */ "cmdlist 
318f2 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64  ::= cmdlist ecmd
318f3 22 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63  ",. /*   2 */ "c
318f4 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22  mdlist ::= ecmd"
318f5 2c 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63  ,. /*   3 */ "ec
318f6 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f  md ::= SEMI",. /
318f7 2a 20 20 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a  *   4 */ "ecmd :
318f8 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20  := explain cmdx 
318f9 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a  SEMI",. /*   5 *
318fa 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c  / "explain ::=",
318fb 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22 65 78 70  . /*   6 */ "exp
318fc 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e  lain ::= EXPLAIN
318fd 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65  ",. /*   7 */ "e
318fe 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
318ff 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a  IN QUERY PLAN",.
31900 20 2f 2a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78   /*   8 */ "cmdx
31901 20 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20   ::= cmd",. /*  
31902 20 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42   9 */ "cmd ::= B
31903 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74  EGIN transtype t
31904 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  rans_opt",. /*  
31905 31 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74  10 */ "trans_opt
31906 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a   ::=",. /*  11 *
31907 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  / "trans_opt ::=
31908 20 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20   TRANSACTION",. 
31909 2f 2a 20 20 31 32 20 2a 2f 20 22 74 72 61 6e 73  /*  12 */ "trans
3190a 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43  _opt ::= TRANSAC
3190b 54 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31  TION nm",. /*  1
3190c 33 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20  3 */ "transtype 
3190d 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f  ::=",. /*  14 */
3190e 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20   "transtype ::= 
3190f 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20  DEFERRED",. /*  
31910 31 35 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65  15 */ "transtype
31911 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c   ::= IMMEDIATE",
31912 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72 61  . /*  16 */ "tra
31913 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55  nstype ::= EXCLU
31914 53 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a  SIVE",. /*  17 *
31915 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49  / "cmd ::= COMMI
31916 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f  T trans_opt",. /
31917 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  18 */ "cmd ::
31918 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22  = END trans_opt"
31919 2c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d  ,. /*  19 */ "cm
3191a 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74  d ::= ROLLBACK t
3191b 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  rans_opt",. /*  
3191c 32 30 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74  20 */ "savepoint
3191d 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49  _opt ::= SAVEPOI
3191e 4e 54 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20  NT",. /*  21 */ 
3191f 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a  "savepoint_opt :
31920 3a 3d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20  :=",. /*  22 */ 
31921 22 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49  "cmd ::= SAVEPOI
31922 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20  NT nm",. /*  23 
31923 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45  */ "cmd ::= RELE
31924 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70  ASE savepoint_op
31925 74 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a  t nm",. /*  24 *
31926 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42  / "cmd ::= ROLLB
31927 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f  ACK trans_opt TO
31928 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e   savepoint_opt n
31929 6d 22 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22  m",. /*  25 */ "
3192a 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74  cmd ::= create_t
3192b 61 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c  able create_tabl
3192c 65 5f 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36  e_args",. /*  26
3192d 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c   */ "create_tabl
3192e 65 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  e ::= createkw t
3192f 65 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65  emp TABLE ifnote
31930 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a  xists nm dbnm",.
31931 20 2f 2a 20 20 32 37 20 2a 2f 20 22 63 72 65 61   /*  27 */ "crea
31932 74 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 22  tekw ::= CREATE"
31933 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66  ,. /*  28 */ "if
31934 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a  notexists ::=",.
31935 20 2f 2a 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f   /*  29 */ "ifno
31936 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e  texists ::= IF N
31937 4f 54 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20  OT EXISTS",. /* 
31938 20 33 30 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d   30 */ "temp ::=
31939 20 54 45 4d 50 22 2c 0a 20 2f 2a 20 20 33 31 20   TEMP",. /*  31 
3193a 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20  */ "temp ::=",. 
3193b 2f 2a 20 20 33 32 20 2a 2f 20 22 63 72 65 61 74  /*  32 */ "creat
3193c 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d  e_table_args ::=
3193d 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63   LP columnlist c
3193e 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 22 2c  onslist_opt RP",
3193f 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22 63 72 65  . /*  33 */ "cre
31940 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a  ate_table_args :
31941 3a 3d 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20  := AS select",. 
31942 2f 2a 20 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d  /*  34 */ "colum
31943 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
31944 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d  list COMMA colum
31945 6e 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22  n",. /*  35 */ "
31946 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63  columnlist ::= c
31947 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33 36 20  olumn",. /*  36 
31948 2a 2f 20 22 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63  */ "column ::= c
31949 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72  olumnid type car
3194a 67 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 33 37 20  glist",. /*  37 
3194b 2a 2f 20 22 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d  */ "columnid ::=
3194c 20 6e 6d 22 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f   nm",. /*  38 */
3194d 20 22 69 64 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f   "id ::= ID",. /
3194e 2a 20 20 33 39 20 2a 2f 20 22 69 64 20 3a 3a 3d  *  39 */ "id ::=
3194f 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 20   INDEXED",. /*  
31950 34 30 20 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49  40 */ "ids ::= I
31951 44 7c 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20  D|STRING",. /*  
31952 34 31 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64  41 */ "nm ::= id
31953 22 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 6e  ",. /*  42 */ "n
31954 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20  m ::= STRING",. 
31955 2f 2a 20 20 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a  /*  43 */ "nm ::
31956 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20  = JOIN_KW",. /* 
31957 20 34 34 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d   44 */ "type ::=
31958 22 2c 0a 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74  ",. /*  45 */ "t
31959 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65  ype ::= typetoke
3195a 6e 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22  n",. /*  46 */ "
3195b 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
3195c 70 65 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 37  pename",. /*  47
3195d 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a   */ "typetoken :
3195e 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
3195f 69 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20  igned RP",. /*  
31960 34 38 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e  48 */ "typetoken
31961 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50   ::= typename LP
31962 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69   signed COMMA si
31963 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34  gned RP",. /*  4
31964 39 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a  9 */ "typename :
31965 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 30  := ids",. /*  50
31966 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a   */ "typename ::
31967 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73 22 2c  = typename ids",
31968 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22 73 69 67  . /*  51 */ "sig
31969 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  ned ::= plus_num
3196a 22 2c 0a 20 2f 2a 20 20 35 32 20 2a 2f 20 22 73  ",. /*  52 */ "s
3196b 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f  igned ::= minus_
3196c 6e 75 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f  num",. /*  53 */
3196d 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63   "carglist ::= c
3196e 61 72 67 6c 69 73 74 20 63 61 72 67 22 2c 0a 20  arglist carg",. 
3196f 2f 2a 20 20 35 34 20 2a 2f 20 22 63 61 72 67 6c  /*  54 */ "cargl
31970 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35  ist ::=",. /*  5
31971 35 20 2a 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43  5 */ "carg ::= C
31972 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f  ONSTRAINT nm cco
31973 6e 73 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20  ns",. /*  56 */ 
31974 22 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22  "carg ::= ccons"
31975 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63  ,. /*  57 */ "cc
31976 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
31977 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 38 20 2a  term",. /*  58 *
31978 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
31979 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50 22  AULT LP expr RP"
3197a 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63  ,. /*  59 */ "cc
3197b 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
3197c 50 4c 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20  PLUS term",. /* 
3197d 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   60 */ "ccons ::
3197e 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20  = DEFAULT MINUS 
3197f 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 31 20 2a  term",. /*  61 *
31980 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
31981 41 55 4c 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36  AULT id",. /*  6
31982 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  2 */ "ccons ::= 
31983 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  NULL onconf",. /
31984 2a 20 20 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20  *  63 */ "ccons 
31985 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63  ::= NOT NULL onc
31986 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f  onf",. /*  64 */
31987 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d   "ccons ::= PRIM
31988 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65  ARY KEY sortorde
31989 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63  r onconf autoinc
3198a 22 2c 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63  ",. /*  65 */ "c
3198b 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20  cons ::= UNIQUE 
3198c 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 36  onconf",. /*  66
3198d 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43   */ "ccons ::= C
3198e 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 22  HECK LP expr RP"
3198f 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63  ,. /*  67 */ "cc
31990 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43  ons ::= REFERENC
31991 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70  ES nm idxlist_op
31992 74 20 72 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20  t refargs",. /* 
31993 20 36 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   68 */ "ccons ::
31994 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  = defer_subclaus
31995 65 22 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22  e",. /*  69 */ "
31996 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54  ccons ::= COLLAT
31997 45 20 69 64 73 22 2c 0a 20 2f 2a 20 20 37 30 20  E ids",. /*  70 
31998 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22  */ "autoinc ::="
31999 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22 61 75  ,. /*  71 */ "au
3199a 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e  toinc ::= AUTOIN
3199b 43 52 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20  CR",. /*  72 */ 
3199c 22 72 65 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20  "refargs ::=",. 
3199d 2f 2a 20 20 37 33 20 2a 2f 20 22 72 65 66 61 72  /*  73 */ "refar
3199e 67 73 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72  gs ::= refargs r
3199f 65 66 61 72 67 22 2c 0a 20 2f 2a 20 20 37 34 20  efarg",. /*  74 
319a0 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d  */ "refarg ::= M
319a1 41 54 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37  ATCH nm",. /*  7
319a2 35 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  5 */ "refarg ::=
319a3 20 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63   ON DELETE refac
319a4 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22  t",. /*  76 */ "
319a5 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50  refarg ::= ON UP
319a6 44 41 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f  DATE refact",. /
319a7 2a 20 20 37 37 20 2a 2f 20 22 72 65 66 61 63 74  *  77 */ "refact
319a8 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22 2c 0a   ::= SET NULL",.
319a9 20 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65 66 61   /*  78 */ "refa
319aa 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55  ct ::= SET DEFAU
319ab 4c 54 22 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f 20  LT",. /*  79 */ 
319ac 22 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43  "refact ::= CASC
319ad 41 44 45 22 2c 0a 20 2f 2a 20 20 38 30 20 2a 2f  ADE",. /*  80 */
319ae 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53   "refact ::= RES
319af 54 52 49 43 54 22 2c 0a 20 2f 2a 20 20 38 31 20  TRICT",. /*  81 
319b0 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 4e  */ "refact ::= N
319b1 4f 20 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20  O ACTION",. /*  
319b2 38 32 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62  82 */ "defer_sub
319b3 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44  clause ::= NOT D
319b4 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64  EFERRABLE init_d
319b5 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
319b6 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22 64  ",. /*  83 */ "d
319b7 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
319b8 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  := DEFERRABLE in
319b9 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
319ba 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 34 20 2a  _opt",. /*  84 *
319bb 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64  / "init_deferred
319bc 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  _pred_opt ::=",.
319bd 20 2f 2a 20 20 38 35 20 2a 2f 20 22 69 6e 69 74   /*  85 */ "init
319be 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
319bf 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
319c0 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20   DEFERRED",. /* 
319c1 20 38 36 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66   86 */ "init_def
319c2 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a  erred_pred_opt :
319c3 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d  := INITIALLY IMM
319c4 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 37  EDIATE",. /*  87
319c5 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70   */ "conslist_op
319c6 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38 20  t ::=",. /*  88 
319c7 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  */ "conslist_opt
319c8 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c   ::= COMMA consl
319c9 69 73 74 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f  ist",. /*  89 */
319ca 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63   "conslist ::= c
319cb 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63  onslist COMMA tc
319cc 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f  ons",. /*  90 */
319cd 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63   "conslist ::= c
319ce 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a  onslist tcons",.
319cf 20 2f 2a 20 20 39 31 20 2a 2f 20 22 63 6f 6e 73   /*  91 */ "cons
319d0 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c  list ::= tcons",
319d1 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22 74 63 6f  . /*  92 */ "tco
319d2 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
319d3 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 39 33 20 2a  T nm",. /*  93 *
319d4 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49  / "tcons ::= PRI
319d5 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c  MARY KEY LP idxl
319d6 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f  ist autoinc RP o
319d7 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 34 20  nconf",. /*  94 
319d8 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e  */ "tcons ::= UN
319d9 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20  IQUE LP idxlist 
319da 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RP onconf",. /* 
319db 20 39 35 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   95 */ "tcons ::
319dc 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20  = CHECK LP expr 
319dd 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RP onconf",. /* 
319de 20 39 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   96 */ "tcons ::
319df 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50  = FOREIGN KEY LP
319e0 20 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45   idxlist RP REFE
319e1 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
319e2 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65  t_opt refargs de
319e3 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
319e4 74 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22  t",. /*  97 */ "
319e5 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
319e6 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39  opt ::=",. /*  9
319e7 38 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  8 */ "defer_subc
319e8 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65  lause_opt ::= de
319e9 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a  fer_subclause",.
319ea 20 2f 2a 20 20 39 39 20 2a 2f 20 22 6f 6e 63 6f   /*  99 */ "onco
319eb 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 30  nf ::=",. /* 100
319ec 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20   */ "onconf ::= 
319ed 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f  ON CONFLICT reso
319ee 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30  lvetype",. /* 10
319ef 31 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d  1 */ "orconf ::=
319f0 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22 6f  ",. /* 102 */ "o
319f1 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73  rconf ::= OR res
319f2 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31  olvetype",. /* 1
319f3 30 33 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79  03 */ "resolvety
319f4 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65  pe ::= raisetype
319f5 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20 22 72  ",. /* 104 */ "r
319f6 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49  esolvetype ::= I
319f7 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 31 30 35 20  GNORE",. /* 105 
319f8 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20  */ "resolvetype 
319f9 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f  ::= REPLACE",. /
319fa 2a 20 31 30 36 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 106 */ "cmd ::
319fb 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65  = DROP TABLE ife
319fc 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
319fd 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22 69 66 65  . /* 107 */ "ife
319fe 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49  xists ::= IF EXI
319ff 53 54 53 22 2c 0a 20 2f 2a 20 31 30 38 20 2a 2f  STS",. /* 108 */
31a00 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c   "ifexists ::=",
31a01 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22 63 6d 64  . /* 109 */ "cmd
31a02 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65   ::= createkw te
31a03 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69  mp VIEW ifnotexi
31a04 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73  sts nm dbnm AS s
31a05 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 30 20  elect",. /* 110 
31a06 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
31a07 20 56 49 45 57 20 69 66 65 78 69 73 74 73 20 66   VIEW ifexists f
31a08 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 31  ullname",. /* 11
31a09 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65  1 */ "cmd ::= se
31a0a 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 32 20 2a  lect",. /* 112 *
31a0b 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e  / "select ::= on
31a0c 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31  eselect",. /* 11
31a0d 33 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d  3 */ "select ::=
31a0e 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c   select multisel
31a0f 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74  ect_op oneselect
31a10 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20 22 6d  ",. /* 114 */ "m
31a11 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
31a12 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 31  = UNION",. /* 11
31a13 35 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63  5 */ "multiselec
31a14 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41  t_op ::= UNION A
31a15 4c 4c 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20  LL",. /* 116 */ 
31a16 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20  "multiselect_op 
31a17 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52  ::= EXCEPT|INTER
31a18 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 37 20 2a  SECT",. /* 117 *
31a19 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d  / "oneselect ::=
31a1a 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e 63 74   SELECT distinct
31a1b 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d   selcollist from
31a1c 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f 75 70   where_opt group
31a1d 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70  by_opt having_op
31a1e 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c 69  t orderby_opt li
31a1f 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31  mit_opt",. /* 11
31a20 38 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a  8 */ "distinct :
31a21 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f  := DISTINCT",. /
31a22 2a 20 31 31 39 20 2a 2f 20 22 64 69 73 74 69 6e  * 119 */ "distin
31a23 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a  ct ::= ALL",. /*
31a24 20 31 32 30 20 2a 2f 20 22 64 69 73 74 69 6e 63   120 */ "distinc
31a25 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 31 20  t ::=",. /* 121 
31a26 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c  */ "sclp ::= sel
31a27 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a  collist COMMA",.
31a28 20 2f 2a 20 31 32 32 20 2a 2f 20 22 73 63 6c 70   /* 122 */ "sclp
31a29 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a   ::=",. /* 123 *
31a2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  / "selcollist ::
31a2b 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73 22 2c  = sclp expr as",
31a2c 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c  . /* 124 */ "sel
31a2d 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70  collist ::= sclp
31a2e 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 35 20   STAR",. /* 125 
31a2f 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a  */ "selcollist :
31a30 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53  := sclp nm DOT S
31a31 54 41 52 22 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f  TAR",. /* 126 */
31a32 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c   "as ::= AS nm",
31a33 0a 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73 20  . /* 127 */ "as 
31a34 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32  ::= ids",. /* 12
31a35 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20  8 */ "as ::=",. 
31a36 2f 2a 20 31 32 39 20 2a 2f 20 22 66 72 6f 6d 20  /* 129 */ "from 
31a37 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f  ::=",. /* 130 */
31a38 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20   "from ::= FROM 
31a39 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a  seltablist",. /*
31a3a 20 31 33 31 20 2a 2f 20 22 73 74 6c 5f 70 72 65   131 */ "stl_pre
31a3b 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69  fix ::= seltabli
31a3c 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20  st joinop",. /* 
31a3d 31 33 32 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66  132 */ "stl_pref
31a3e 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 33  ix ::=",. /* 133
31a3f 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20   */ "seltablist 
31a40 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e  ::= stl_prefix n
31a41 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65  m dbnm as indexe
31a42 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69  d_opt on_opt usi
31a43 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 34  ng_opt",. /* 134
31a44 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20   */ "seltablist 
31a45 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c  ::= stl_prefix L
31a46 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f  P select RP as o
31a47 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22  n_opt using_opt"
31a48 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22 73 65  ,. /* 135 */ "se
31a49 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c  ltablist ::= stl
31a4a 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 74 61  _prefix LP selta
31a4b 62 6c 69 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f  blist RP as on_o
31a4c 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20  pt using_opt",. 
31a4d 2f 2a 20 31 33 36 20 2a 2f 20 22 64 62 6e 6d 20  /* 136 */ "dbnm 
31a4e 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 37 20 2a 2f  ::=",. /* 137 */
31a4f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e   "dbnm ::= DOT n
31a50 6d 22 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22  m",. /* 138 */ "
31a51 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20  fullname ::= nm 
31a52 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a  dbnm",. /* 139 *
31a53 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f  / "joinop ::= CO
31a54 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31  MMA|JOIN",. /* 1
31a55 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  40 */ "joinop ::
31a56 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 22 2c  = JOIN_KW JOIN",
31a57 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22 6a 6f 69  . /* 141 */ "joi
31a58 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  nop ::= JOIN_KW 
31a59 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34  nm JOIN",. /* 14
31a5a 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  2 */ "joinop ::=
31a5b 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a   JOIN_KW nm nm J
31a5c 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f  OIN",. /* 143 */
31a5d 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20   "on_opt ::= ON 
31a5e 65 78 70 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a  expr",. /* 144 *
31a5f 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  / "on_opt ::=",.
31a60 20 2f 2a 20 31 34 35 20 2a 2f 20 22 69 6e 64 65   /* 145 */ "inde
31a61 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  xed_opt ::=",. /
31a62 2a 20 31 34 36 20 2a 2f 20 22 69 6e 64 65 78 65  * 146 */ "indexe
31a63 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45  d_opt ::= INDEXE
31a64 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20 31 34  D BY nm",. /* 14
31a65 37 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70  7 */ "indexed_op
31a66 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45  t ::= NOT INDEXE
31a67 44 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22  D",. /* 148 */ "
31a68 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53  using_opt ::= US
31a69 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ING LP inscollis
31a6a 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34 39 20 2a  t RP",. /* 149 *
31a6b 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  / "using_opt ::=
31a6c 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22 6f  ",. /* 150 */ "o
31a6d 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c  rderby_opt ::=",
31a6e 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22 6f 72 64  . /* 151 */ "ord
31a6f 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44  erby_opt ::= ORD
31a70 45 52 20 42 59 20 73 6f 72 74 6c 69 73 74 22 2c  ER BY sortlist",
31a71 0a 20 2f 2a 20 31 35 32 20 2a 2f 20 22 73 6f 72  . /* 152 */ "sor
31a72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69  tlist ::= sortli
31a73 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65  st COMMA sortite
31a74 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  m sortorder",. /
31a75 2a 20 31 35 33 20 2a 2f 20 22 73 6f 72 74 6c 69  * 153 */ "sortli
31a76 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20  st ::= sortitem 
31a77 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20  sortorder",. /* 
31a78 31 35 34 20 2a 2f 20 22 73 6f 72 74 69 74 65 6d  154 */ "sortitem
31a79 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20   ::= expr",. /* 
31a7a 31 35 35 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65  155 */ "sortorde
31a7b 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f 2a 20  r ::= ASC",. /* 
31a7c 31 35 36 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65  156 */ "sortorde
31a7d 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20 2f 2a  r ::= DESC",. /*
31a7e 20 31 35 37 20 2a 2f 20 22 73 6f 72 74 6f 72 64   157 */ "sortord
31a7f 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 38  er ::=",. /* 158
31a80 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74   */ "groupby_opt
31a81 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39 20 2a   ::=",. /* 159 *
31a82 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a  / "groupby_opt :
31a83 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70  := GROUP BY nexp
31a84 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 36 30 20  rlist",. /* 160 
31a85 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a  */ "having_opt :
31a86 3a 3d 22 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f 20  :=",. /* 161 */ 
31a87 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20  "having_opt ::= 
31a88 48 41 56 49 4e 47 20 65 78 70 72 22 2c 0a 20 2f  HAVING expr",. /
31a89 2a 20 31 36 32 20 2a 2f 20 22 6c 69 6d 69 74 5f  * 162 */ "limit_
31a8a 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36  opt ::=",. /* 16
31a8b 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20  3 */ "limit_opt 
31a8c 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 22 2c  ::= LIMIT expr",
31a8d 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 6c 69 6d  . /* 164 */ "lim
31a8e 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
31a8f 20 65 78 70 72 20 4f 46 46 53 45 54 20 65 78 70   expr OFFSET exp
31a90 72 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22  r",. /* 165 */ "
31a91 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49  limit_opt ::= LI
31a92 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20 65  MIT expr COMMA e
31a93 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f  xpr",. /* 166 */
31a94 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45   "cmd ::= DELETE
31a95 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69   FROM fullname i
31a96 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72 65  ndexed_opt where
31a97 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 37 20 2a  _opt",. /* 167 *
31a98 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  / "where_opt ::=
31a99 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22 77  ",. /* 168 */ "w
31a9a 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45  here_opt ::= WHE
31a9b 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36  RE expr",. /* 16
31a9c 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 55 50  9 */ "cmd ::= UP
31a9d 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c  DATE orconf full
31a9e 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74  name indexed_opt
31a9f 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65   SET setlist whe
31aa0 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 37 30  re_opt",. /* 170
31aa1 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d   */ "setlist ::=
31aa2 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e   setlist COMMA n
31aa3 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20  m EQ expr",. /* 
31aa4 31 37 31 20 2a 2f 20 22 73 65 74 6c 69 73 74 20  171 */ "setlist 
31aa5 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 22 2c  ::= nm EQ expr",
31aa6 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 63 6d 64  . /* 172 */ "cmd
31aa7 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
31aa8 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
31aa9 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c  scollist_opt VAL
31aaa 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20  UES LP itemlist 
31aab 52 50 22 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f 20  RP",. /* 173 */ 
31aac 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  "cmd ::= insert_
31aad 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d  cmd INTO fullnam
31aae 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  e inscollist_opt
31aaf 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 37   select",. /* 17
31ab0 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e  4 */ "cmd ::= in
31ab1 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75  sert_cmd INTO fu
31ab2 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73  llname inscollis
31ab3 74 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41  t_opt DEFAULT VA
31ab4 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 37 35 20 2a  LUES",. /* 175 *
31ab5 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  / "insert_cmd ::
31ab6 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22  = INSERT orconf"
31ab7 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e  ,. /* 176 */ "in
31ab8 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50  sert_cmd ::= REP
31ab9 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37 37 20 2a  LACE",. /* 177 *
31aba 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20  / "itemlist ::= 
31abb 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65  itemlist COMMA e
31abc 78 70 72 22 2c 0a 20 2f 2a 20 31 37 38 20 2a 2f  xpr",. /* 178 */
31abd 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65   "itemlist ::= e
31abe 78 70 72 22 2c 0a 20 2f 2a 20 31 37 39 20 2a 2f  xpr",. /* 179 */
31abf 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74   "inscollist_opt
31ac0 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 38 30 20 2a   ::=",. /* 180 *
31ac1 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  / "inscollist_op
31ac2 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c  t ::= LP inscoll
31ac3 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38 31  ist RP",. /* 181
31ac4 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20   */ "inscollist 
31ac5 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43  ::= inscollist C
31ac6 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38  OMMA nm",. /* 18
31ac7 32 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  2 */ "inscollist
31ac8 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38   ::= nm",. /* 18
31ac9 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74  3 */ "expr ::= t
31aca 65 72 6d 22 2c 0a 20 2f 2a 20 31 38 34 20 2a 2f  erm",. /* 184 */
31acb 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78   "expr ::= LP ex
31acc 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31 38 35 20  pr RP",. /* 185 
31acd 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c  */ "term ::= NUL
31ace 4c 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22  L",. /* 186 */ "
31acf 65 78 70 72 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f  expr ::= id",. /
31ad0 2a 20 31 38 37 20 2a 2f 20 22 65 78 70 72 20 3a  * 187 */ "expr :
31ad1 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a  := JOIN_KW",. /*
31ad2 20 31 38 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   188 */ "expr ::
31ad3 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f  = nm DOT nm",. /
31ad4 2a 20 31 38 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 189 */ "expr :
31ad5 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54  := nm DOT nm DOT
31ad6 20 6e 6d 22 2c 0a 20 2f 2a 20 31 39 30 20 2a 2f   nm",. /* 190 */
31ad7 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47   "term ::= INTEG
31ad8 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a  ER|FLOAT|BLOB",.
31ad9 20 2f 2a 20 31 39 31 20 2a 2f 20 22 74 65 72 6d   /* 191 */ "term
31ada 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f   ::= STRING",. /
31adb 2a 20 31 39 32 20 2a 2f 20 22 65 78 70 72 20 3a  * 192 */ "expr :
31adc 3a 3d 20 52 45 47 49 53 54 45 52 22 2c 0a 20 2f  := REGISTER",. /
31add 2a 20 31 39 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 193 */ "expr :
31ade 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f  := VARIABLE",. /
31adf 2a 20 31 39 34 20 2a 2f 20 22 65 78 70 72 20 3a  * 194 */ "expr :
31ae0 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20  := expr COLLATE 
31ae1 69 64 73 22 2c 0a 20 2f 2a 20 31 39 35 20 2a 2f  ids",. /* 195 */
31ae2 20 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20   "expr ::= CAST 
31ae3 4c 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74  LP expr AS typet
31ae4 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 39  oken RP",. /* 19
31ae5 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49  6 */ "expr ::= I
31ae6 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78  D LP distinct ex
31ae7 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  prlist RP",. /* 
31ae8 31 39 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  197 */ "expr ::=
31ae9 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 22 2c   ID LP STAR RP",
31aea 0a 20 2f 2a 20 31 39 38 20 2a 2f 20 22 74 65 72  . /* 198 */ "ter
31aeb 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c  m ::= CTIME_KW",
31aec 0a 20 2f 2a 20 31 39 39 20 2a 2f 20 22 65 78 70  . /* 199 */ "exp
31aed 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65  r ::= expr AND e
31aee 78 70 72 22 2c 0a 20 2f 2a 20 32 30 30 20 2a 2f  xpr",. /* 200 */
31aef 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31af0 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30  OR expr",. /* 20
31af1 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  1 */ "expr ::= e
31af2 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20  xpr LT|GT|GE|LE 
31af3 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 32 20 2a  expr",. /* 202 *
31af4 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
31af5 20 45 51 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f   EQ|NE expr",. /
31af6 2a 20 32 30 33 20 2a 2f 20 22 65 78 70 72 20 3a  * 203 */ "expr :
31af7 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42  := expr BITAND|B
31af8 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49  ITOR|LSHIFT|RSHI
31af9 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30  FT expr",. /* 20
31afa 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  4 */ "expr ::= e
31afb 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65  xpr PLUS|MINUS e
31afc 78 70 72 22 2c 0a 20 2f 2a 20 32 30 35 20 2a 2f  xpr",. /* 205 */
31afd 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31afe 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65  STAR|SLASH|REM e
31aff 78 70 72 22 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f  xpr",. /* 206 */
31b00 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31b01 43 4f 4e 43 41 54 20 65 78 70 72 22 2c 0a 20 2f  CONCAT expr",. /
31b02 2a 20 32 30 37 20 2a 2f 20 22 6c 69 6b 65 6f 70  * 207 */ "likeop
31b03 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20   ::= LIKE_KW",. 
31b04 2f 2a 20 32 30 38 20 2a 2f 20 22 6c 69 6b 65 6f  /* 208 */ "likeo
31b05 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b  p ::= NOT LIKE_K
31b06 57 22 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f 20 22  W",. /* 209 */ "
31b07 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48  likeop ::= MATCH
31b08 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 6c  ",. /* 210 */ "l
31b09 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41  ikeop ::= NOT MA
31b0a 54 43 48 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f  TCH",. /* 211 */
31b0b 20 22 65 73 63 61 70 65 20 3a 3a 3d 20 45 53 43   "escape ::= ESC
31b0c 41 50 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  APE expr",. /* 2
31b0d 31 32 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a  12 */ "escape ::
31b0e 3d 22 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22  =",. /* 213 */ "
31b0f 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69  expr ::= expr li
31b10 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61 70 65  keop expr escape
31b11 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65  ",. /* 214 */ "e
31b12 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e  xpr ::= expr ISN
31b13 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f  ULL|NOTNULL",. /
31b14 2a 20 32 31 35 20 2a 2f 20 22 65 78 70 72 20 3a  * 215 */ "expr :
31b15 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c  := expr NOT NULL
31b16 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65  ",. /* 216 */ "e
31b17 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20  xpr ::= expr IS 
31b18 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 37 20 2a  expr",. /* 217 *
31b19 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
31b1a 20 49 53 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20   IS NOT expr",. 
31b1b 2f 2a 20 32 31 38 20 2a 2f 20 22 65 78 70 72 20  /* 218 */ "expr 
31b1c 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20  ::= NOT expr",. 
31b1d 2f 2a 20 32 31 39 20 2a 2f 20 22 65 78 70 72 20  /* 219 */ "expr 
31b1e 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 22  ::= BITNOT expr"
31b1f 2c 0a 20 2f 2a 20 32 32 30 20 2a 2f 20 22 65 78  ,. /* 220 */ "ex
31b20 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70  pr ::= MINUS exp
31b21 72 22 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22  r",. /* 221 */ "
31b22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78  expr ::= PLUS ex
31b23 70 72 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f 20  pr",. /* 222 */ 
31b24 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20  "between_op ::= 
31b25 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32  BETWEEN",. /* 22
31b26 33 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70  3 */ "between_op
31b27 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e   ::= NOT BETWEEN
31b28 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22 65  ",. /* 224 */ "e
31b29 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74  xpr ::= expr bet
31b2a 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44  ween_op expr AND
31b2b 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20   expr",. /* 225 
31b2c 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e  */ "in_op ::= IN
31b2d 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f 20 22 69  ",. /* 226 */ "i
31b2e 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 22  n_op ::= NOT IN"
31b2f 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f 20 22 65 78  ,. /* 227 */ "ex
31b30 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
31b31 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50  p LP exprlist RP
31b32 22 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22 65  ",. /* 228 */ "e
31b33 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63  xpr ::= LP selec
31b34 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 39 20 2a  t RP",. /* 229 *
31b35 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
31b36 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74   in_op LP select
31b37 20 52 50 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f   RP",. /* 230 */
31b38 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
31b39 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c 0a  in_op nm dbnm",.
31b3a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78 70 72   /* 231 */ "expr
31b3b 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73   ::= EXISTS LP s
31b3c 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32  elect RP",. /* 2
31b3d 33 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  32 */ "expr ::= 
31b3e 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e  CASE case_operan
31b3f 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  d case_exprlist 
31b40 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c 0a  case_else END",.
31b41 20 2f 2a 20 32 33 33 20 2a 2f 20 22 63 61 73 65   /* 233 */ "case
31b42 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61  _exprlist ::= ca
31b43 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45 4e  se_exprlist WHEN
31b44 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 22   expr THEN expr"
31b45 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22 63 61  ,. /* 234 */ "ca
31b46 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  se_exprlist ::= 
31b47 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65  WHEN expr THEN e
31b48 78 70 72 22 2c 0a 20 2f 2a 20 32 33 35 20 2a 2f  xpr",. /* 235 */
31b49 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20   "case_else ::= 
31b4a 45 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  ELSE expr",. /* 
31b4b 32 33 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73  236 */ "case_els
31b4c 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 37 20  e ::=",. /* 237 
31b4d 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64  */ "case_operand
31b4e 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20   ::= expr",. /* 
31b4f 32 33 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65  238 */ "case_ope
31b50 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  rand ::=",. /* 2
31b51 33 39 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20  39 */ "exprlist 
31b52 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a  ::= nexprlist",.
31b53 20 2f 2a 20 32 34 30 20 2a 2f 20 22 65 78 70 72   /* 240 */ "expr
31b54 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  list ::=",. /* 2
31b55 34 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74  41 */ "nexprlist
31b56 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43   ::= nexprlist C
31b57 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20  OMMA expr",. /* 
31b58 32 34 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73  242 */ "nexprlis
31b59 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a  t ::= expr",. /*
31b5a 20 32 34 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   243 */ "cmd ::=
31b5b 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65   createkw unique
31b5c 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74  flag INDEX ifnot
31b5d 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f  exists nm dbnm O
31b5e 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20  N nm LP idxlist 
31b5f 52 50 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20  RP",. /* 244 */ 
31b60 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20  "uniqueflag ::= 
31b61 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 34 35  UNIQUE",. /* 245
31b62 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20   */ "uniqueflag 
31b63 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36 20 2a 2f  ::=",. /* 246 */
31b64 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a   "idxlist_opt ::
31b65 3d 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22  =",. /* 247 */ "
31b66 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  idxlist_opt ::= 
31b67 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a  LP idxlist RP",.
31b68 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64 78 6c   /* 248 */ "idxl
31b69 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20  ist ::= idxlist 
31b6a 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65  COMMA nm collate
31b6b 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
31b6c 20 32 34 39 20 2a 2f 20 22 69 64 78 6c 69 73 74   249 */ "idxlist
31b6d 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20   ::= nm collate 
31b6e 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20  sortorder",. /* 
31b6f 32 35 30 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20  250 */ "collate 
31b70 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f  ::=",. /* 251 */
31b71 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f   "collate ::= CO
31b72 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20  LLATE ids",. /* 
31b73 32 35 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  252 */ "cmd ::= 
31b74 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69  DROP INDEX ifexi
31b75 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20  sts fullname",. 
31b76 2f 2a 20 32 35 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 253 */ "cmd :
31b77 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a 20  := VACUUM",. /* 
31b78 32 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  254 */ "cmd ::= 
31b79 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a 20  VACUUM nm",. /* 
31b7a 32 35 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  255 */ "cmd ::= 
31b7b 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c  PRAGMA nm dbnm",
31b7c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63 6d 64  . /* 256 */ "cmd
31b7d 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
31b7e 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20  bnm EQ nmnum",. 
31b7f 2f 2a 20 32 35 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 257 */ "cmd :
31b80 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
31b81 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a  m LP nmnum RP",.
31b82 20 2f 2a 20 32 35 38 20 2a 2f 20 22 63 6d 64 20   /* 258 */ "cmd 
31b83 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
31b84 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 22  nm EQ minus_num"
31b85 2c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22 63 6d  ,. /* 259 */ "cm
31b86 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
31b87 64 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75  dbnm LP minus_nu
31b88 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 36 30 20 2a  m RP",. /* 260 *
31b89 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75  / "nmnum ::= plu
31b8a 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 36 31 20  s_num",. /* 261 
31b8b 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d  */ "nmnum ::= nm
31b8c 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e  ",. /* 262 */ "n
31b8d 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a 20 2f  mnum ::= ON",. /
31b8e 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d 20  * 263 */ "nmnum 
31b8f 3a 3a 3d 20 44 45 4c 45 54 45 22 2c 0a 20 2f 2a  ::= DELETE",. /*
31b90 20 32 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   264 */ "nmnum :
31b91 3a 3d 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a  := DEFAULT",. /*
31b92 20 32 36 35 20 2a 2f 20 22 70 6c 75 73 5f 6e 75   265 */ "plus_nu
31b93 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e  m ::= plus_opt n
31b94 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36 36 20  umber",. /* 266 
31b95 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a  */ "minus_num ::
31b96 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22 2c  = MINUS number",
31b97 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22 6e 75 6d  . /* 267 */ "num
31b98 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c  ber ::= INTEGER|
31b99 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36 38 20  FLOAT",. /* 268 
31b9a 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d  */ "plus_opt ::=
31b9b 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36 39 20   PLUS",. /* 269 
31b9c 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d  */ "plus_opt ::=
31b9d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 63  ",. /* 270 */ "c
31b9e 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
31b9f 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47  trigger_decl BEG
31ba0 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  IN trigger_cmd_l
31ba1 69 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 37  ist END",. /* 27
31ba2 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65  1 */ "trigger_de
31ba3 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47  cl ::= temp TRIG
31ba4 47 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20  GER ifnotexists 
31ba5 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f  nm dbnm trigger_
31ba6 74 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65  time trigger_eve
31ba7 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66  nt ON fullname f
31ba8 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68  oreach_clause wh
31ba9 65 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20  en_clause",. /* 
31baa 32 37 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  272 */ "trigger_
31bab 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22  time ::= BEFORE"
31bac 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22 74 72  ,. /* 273 */ "tr
31bad 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41  igger_time ::= A
31bae 46 54 45 52 22 2c 0a 20 2f 2a 20 32 37 34 20 2a  FTER",. /* 274 *
31baf 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20  / "trigger_time 
31bb0 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c  ::= INSTEAD OF",
31bb1 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69  . /* 275 */ "tri
31bb2 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a  gger_time ::=",.
31bb3 20 2f 2a 20 32 37 36 20 2a 2f 20 22 74 72 69 67   /* 276 */ "trig
31bb4 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45  ger_event ::= DE
31bb5 4c 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f  LETE|INSERT",. /
31bb6 2a 20 32 37 37 20 2a 2f 20 22 74 72 69 67 67 65  * 277 */ "trigge
31bb7 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41  r_event ::= UPDA
31bb8 54 45 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20  TE",. /* 278 */ 
31bb9 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a  "trigger_event :
31bba 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73  := UPDATE OF ins
31bbb 63 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 37  collist",. /* 27
31bbc 39 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c  9 */ "foreach_cl
31bbd 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  ause ::=",. /* 2
31bbe 38 30 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63  80 */ "foreach_c
31bbf 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41  lause ::= FOR EA
31bc0 43 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 38 31  CH ROW",. /* 281
31bc1 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65   */ "when_clause
31bc2 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 32 20 2a   ::=",. /* 282 *
31bc3 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a  / "when_clause :
31bc4 3a 3d 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20  := WHEN expr",. 
31bc5 2f 2a 20 32 38 33 20 2a 2f 20 22 74 72 69 67 67  /* 283 */ "trigg
31bc6 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20  er_cmd_list ::= 
31bc7 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
31bc8 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d   trigger_cmd SEM
31bc9 49 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22  I",. /* 284 */ "
31bca 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
31bcb 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
31bcc 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 35 20   SEMI",. /* 285 
31bcd 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 22  */ "trnm ::= nm"
31bce 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20 22 74 72  ,. /* 286 */ "tr
31bcf 6e 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  nm ::= nm DOT nm
31bd0 22 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f 20 22 74  ",. /* 287 */ "t
31bd1 72 69 64 78 62 79 20 3a 3a 3d 22 2c 0a 20 2f 2a  ridxby ::=",. /*
31bd2 20 32 38 38 20 2a 2f 20 22 74 72 69 64 78 62 79   288 */ "tridxby
31bd3 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20   ::= INDEXED BY 
31bd4 6e 6d 22 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20  nm",. /* 289 */ 
31bd5 22 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e 4f 54  "tridxby ::= NOT
31bd6 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 32   INDEXED",. /* 2
31bd7 39 30 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  90 */ "trigger_c
31bd8 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72  md ::= UPDATE or
31bd9 63 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64 78 62  conf trnm tridxb
31bda 79 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68  y SET setlist wh
31bdb 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39  ere_opt",. /* 29
31bdc 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  1 */ "trigger_cm
31bdd 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
31bde 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f   INTO trnm insco
31bdf 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53  llist_opt VALUES
31be0 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22   LP itemlist RP"
31be1 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22 74 72  ,. /* 292 */ "tr
31be2 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e  igger_cmd ::= in
31be3 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72  sert_cmd INTO tr
31be4 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  nm inscollist_op
31be5 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32  t select",. /* 2
31be6 39 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  93 */ "trigger_c
31be7 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
31be8 4f 4d 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20  OM trnm tridxby 
31be9 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
31bea 32 39 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  294 */ "trigger_
31beb 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c  cmd ::= select",
31bec 0a 20 2f 2a 20 32 39 35 20 2a 2f 20 22 65 78 70  . /* 295 */ "exp
31bed 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49  r ::= RAISE LP I
31bee 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a 20 32  GNORE RP",. /* 2
31bef 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  96 */ "expr ::= 
31bf0 52 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79  RAISE LP raisety
31bf1 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c  pe COMMA nm RP",
31bf2 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 72 61 69  . /* 297 */ "rai
31bf3 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42  setype ::= ROLLB
31bf4 41 43 4b 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f  ACK",. /* 298 */
31bf5 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20   "raisetype ::= 
31bf6 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 39 39 20  ABORT",. /* 299 
31bf7 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a  */ "raisetype ::
31bf8 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 33 30 30  = FAIL",. /* 300
31bf9 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
31bfa 50 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73  P TRIGGER ifexis
31bfb 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f  ts fullname",. /
31bfc 2a 20 33 30 31 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 301 */ "cmd ::
31bfd 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  = ATTACH databas
31bfe 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53  e_kw_opt expr AS
31bff 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 22 2c 0a   expr key_opt",.
31c00 20 2f 2a 20 33 30 32 20 2a 2f 20 22 63 6d 64 20   /* 302 */ "cmd 
31c01 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62  ::= DETACH datab
31c02 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 22  ase_kw_opt expr"
31c03 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22 6b 65  ,. /* 303 */ "ke
31c04 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  y_opt ::=",. /* 
31c05 33 30 34 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20  304 */ "key_opt 
31c06 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c 0a 20  ::= KEY expr",. 
31c07 2f 2a 20 33 30 35 20 2a 2f 20 22 64 61 74 61 62  /* 305 */ "datab
31c08 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44  ase_kw_opt ::= D
31c09 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20 33 30  ATABASE",. /* 30
31c0a 36 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b  6 */ "database_k
31c0b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  w_opt ::=",. /* 
31c0c 33 30 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  307 */ "cmd ::= 
31c0d 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20 33 30  REINDEX",. /* 30
31c0e 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45  8 */ "cmd ::= RE
31c0f 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a  INDEX nm dbnm",.
31c10 20 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d 64 20   /* 309 */ "cmd 
31c11 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f  ::= ANALYZE",. /
31c12 2a 20 33 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 310 */ "cmd ::
31c13 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e  = ANALYZE nm dbn
31c14 6d 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20 22  m",. /* 311 */ "
31c15 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41  cmd ::= ALTER TA
31c16 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e  BLE fullname REN
31c17 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20  AME TO nm",. /* 
31c18 33 31 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  312 */ "cmd ::= 
31c19 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f  ALTER TABLE add_
31c1a 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20  column_fullname 
31c1b 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  ADD kwcolumn_opt
31c1c 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33 31   column",. /* 31
31c1d 33 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75 6d 6e  3 */ "add_column
31c1e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75  _fullname ::= fu
31c1f 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 31 34  llname",. /* 314
31c20 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70   */ "kwcolumn_op
31c21 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 35 20  t ::=",. /* 315 
31c22 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  */ "kwcolumn_opt
31c23 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a   ::= COLUMNKW",.
31c24 20 2f 2a 20 33 31 36 20 2a 2f 20 22 63 6d 64 20   /* 316 */ "cmd 
31c25 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 22  ::= create_vtab"
31c26 2c 0a 20 2f 2a 20 33 31 37 20 2a 2f 20 22 63 6d  ,. /* 317 */ "cm
31c27 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61  d ::= create_vta
31c28 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74  b LP vtabarglist
31c29 20 52 50 22 2c 0a 20 2f 2a 20 33 31 38 20 2a 2f   RP",. /* 318 */
31c2a 20 22 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a   "create_vtab ::
31c2b 3d 20 63 72 65 61 74 65 6b 77 20 56 49 52 54 55  = createkw VIRTU
31c2c 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d  AL TABLE nm dbnm
31c2d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a 20   USING nm",. /* 
31c2e 33 31 39 20 2a 2f 20 22 76 74 61 62 61 72 67 6c  319 */ "vtabargl
31c2f 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 22  ist ::= vtabarg"
31c30 2c 0a 20 2f 2a 20 33 32 30 20 2a 2f 20 22 76 74  ,. /* 320 */ "vt
31c31 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74  abarglist ::= vt
31c32 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20  abarglist COMMA 
31c33 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33 32  vtabarg",. /* 32
31c34 31 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a 3a  1 */ "vtabarg ::
31c35 3d 22 2c 0a 20 2f 2a 20 33 32 32 20 2a 2f 20 22  =",. /* 322 */ "
31c36 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74 61 62  vtabarg ::= vtab
31c37 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b 65 6e  arg vtabargtoken
31c38 22 2c 0a 20 2f 2a 20 33 32 33 20 2a 2f 20 22 76  ",. /* 323 */ "v
31c39 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20  tabargtoken ::= 
31c3a 41 4e 59 22 2c 0a 20 2f 2a 20 33 32 34 20 2a 2f  ANY",. /* 324 */
31c3b 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a   "vtabargtoken :
31c3c 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50  := lp anylist RP
31c3d 22 2c 0a 20 2f 2a 20 33 32 35 20 2a 2f 20 22 6c  ",. /* 325 */ "l
31c3e 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20 33  p ::= LP",. /* 3
31c3f 32 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a  26 */ "anylist :
31c40 3a 3d 22 2c 0a 20 2f 2a 20 33 32 37 20 2a 2f 20  :=",. /* 327 */ 
31c41 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79  "anylist ::= any
31c42 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74 20  list LP anylist 
31c43 52 50 22 2c 0a 20 2f 2a 20 33 32 38 20 2a 2f 20  RP",. /* 328 */ 
31c44 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79  "anylist ::= any
31c45 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23 65  list ANY",.};.#e
31c46 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
31c47 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43 4b 44  /...#if YYSTACKD
31c48 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54 72  EPTH<=0./*.** Tr
31c49 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68  y to increase th
31c4a 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 61  e size of the pa
31c4b 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73  rser stack..*/.s
31c4c 74 61 74 69 63 20 76 6f 69 64 20 79 79 47 72 6f  tatic void yyGro
31c4d 77 53 74 61 63 6b 28 79 79 50 61 72 73 65 72 20  wStack(yyParser 
31c4e 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77 53 69  *p){.  int newSi
31c4f 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74  ze;.  yyStackEnt
31c50 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65 77  ry *pNew;..  new
31c51 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73  Size = p->yystks
31c52 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70 4e 65  z*2 + 100;.  pNe
31c53 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79  w = realloc(p->y
31c54 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a  ystack, newSize*
31c55 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29  sizeof(pNew[0]))
31c56 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a  ;.  if( pNew ){.
31c57 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b 20 3d      p->yystack =
31c58 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 79 79   pNew;.    p->yy
31c59 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b  stksz = newSize;
31c5a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
31c5b 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
31c5c 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66 70 72  ILE ){.      fpr
31c5d 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
31c5e 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77 73 20  ,"%sStack grows 
31c5f 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21 5c 6e  to %d entries!\n
31c60 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
31c61 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20   yyTracePrompt, 
31c62 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20 20 20  p->yystksz);.   
31c63 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a   }.#endif.  }.}.
31c64 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 54  #endif../* .** T
31c65 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c  his function all
31c66 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70 61 72  ocates a new par
31c67 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  ser..** The only
31c68 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
31c69 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
31c6a 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72 6b 73  tion which works
31c6b 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e   like.** malloc.
31c6c 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a  .**.** Inputs:.*
31c6d 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * A pointer to t
31c6e 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  he function used
31c6f 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d   to allocate mem
31c70 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75  ory..**.** Outpu
31c71 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  ts:.** A pointer
31c72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20 20 54   to a parser.  T
31c73 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 75  his pointer is u
31c74 73 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e  sed in subsequen
31c75 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71  t calls.** to sq
31c76 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e 64 20  lite3Parser and 
31c77 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 65  sqlite3ParserFre
31c78 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
31c79 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
31c7a 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f  e3ParserAlloc(vo
31c7b 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63  id *(*mallocProc
31c7c 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20 79 79  )(size_t)){.  yy
31c7d 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 3b  Parser *pParser;
31c7e 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28 79 79  .  pParser = (yy
31c7f 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63  Parser*)(*malloc
31c80 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74 29 73  Proc)( (size_t)s
31c81 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72 29 20  izeof(yyParser) 
31c82 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 72  );.  if( pParser
31c83 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 72 2d   ){.    pParser-
31c84 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23 69 66  >yyidx = -1;.#if
31c85 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
31c86 41 43 4b 44 45 50 54 48 0a 20 20 20 20 70 50 61  ACKDEPTH.    pPa
31c87 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d  rser->yyidxMax =
31c88 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 59   0;.#endif.#if Y
31c89 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
31c8a 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74     pParser->yyst
31c8b 61 63 6b 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ack = NULL;.    
31c8c 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a  pParser->yystksz
31c8d 20 3d 20 30 3b 0a 20 20 20 20 79 79 47 72 6f 77   = 0;.    yyGrow
31c8e 53 74 61 63 6b 28 70 50 61 72 73 65 72 29 3b 0a  Stack(pParser);.
31c8f 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
31c90 75 72 6e 20 70 50 61 72 73 65 72 3b 0a 7d 0a 0a  urn pParser;.}..
31c91 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
31c92 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65   function delete
31c93 73 20 74 68 65 20 76 61 6c 75 65 20 61 73 73 6f  s the value asso
31c94 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a 2a  ciated with a.**
31c95 20 73 79 6d 62 6f 6c 2e 20 20 54 68 65 20 73 79   symbol.  The sy
31c96 6d 62 6f 6c 20 63 61 6e 20 62 65 20 65 69 74 68  mbol can be eith
31c97 65 72 20 61 20 74 65 72 6d 69 6e 61 6c 20 6f 72  er a terminal or
31c98 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a   nonterminal..**
31c99 20 22 79 79 6d 61 6a 6f 72 22 20 69 73 20 74 68   "yymajor" is th
31c9a 65 20 73 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61  e symbol code, a
31c9b 6e 64 20 22 79 79 70 6d 69 6e 6f 72 22 20 69 73  nd "yypminor" is
31c9c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
31c9d 20 74 68 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73   the value..*/.s
31c9e 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 64 65  tatic void yy_de
31c9f 73 74 72 75 63 74 6f 72 28 0a 20 20 79 79 50 61  structor(.  yyPa
31ca0 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
31ca1 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
31ca2 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50  r */.  YYCODETYP
31ca3 45 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 2f  E yymajor,     /
31ca4 2a 20 54 79 70 65 20 63 6f 64 65 20 66 6f 72 20  * Type code for 
31ca5 6f 62 6a 65 63 74 20 74 6f 20 64 65 73 74 72 6f  object to destro
31ca6 79 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  y */.  YYMINORTY
31ca7 50 45 20 2a 79 79 70 6d 69 6e 6f 72 20 20 20 2f  PE *yypminor   /
31ca8 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 74 6f 20  * The object to 
31ca9 62 65 20 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a  be destroyed */.
31caa 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  ){.  sqlite3Pars
31cab 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 73  erARG_FETCH;.  s
31cac 77 69 74 63 68 28 20 79 79 6d 61 6a 6f 72 20 29  witch( yymajor )
31cad 7b 0a 20 20 20 20 2f 2a 20 48 65 72 65 20 69 73  {.    /* Here is
31cae 20 69 6e 73 65 72 74 65 64 20 74 68 65 20 61 63   inserted the ac
31caf 74 69 6f 6e 73 20 77 68 69 63 68 20 74 61 6b 65  tions which take
31cb0 20 70 6c 61 63 65 20 77 68 65 6e 20 61 0a 20 20   place when a.  
31cb1 20 20 2a 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72    ** terminal or
31cb2 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73   non-terminal is
31cb3 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68 69   destroyed.  Thi
31cb4 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 20 20 20  s can happen.   
31cb5 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73 79 6d   ** when the sym
31cb6 62 6f 6c 20 69 73 20 70 6f 70 70 65 64 20 66 72  bol is popped fr
31cb7 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 64 75 72  om the stack dur
31cb8 69 6e 67 20 61 0a 20 20 20 20 2a 2a 20 72 65 64  ing a.    ** red
31cb9 75 63 65 20 6f 72 20 64 75 72 69 6e 67 20 65 72  uce or during er
31cba 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 6f  ror processing o
31cbb 72 20 77 68 65 6e 20 61 20 70 61 72 73 65 72 20  r when a parser 
31cbc 69 73 20 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67  is .    ** being
31cbd 20 64 65 73 74 72 6f 79 65 64 20 62 65 66 6f 72   destroyed befor
31cbe 65 20 69 74 20 69 73 20 66 69 6e 69 73 68 65 64  e it is finished
31cbf 20 70 61 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a   parsing..    **
31cc0 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75  .    ** Note: du
31cc1 72 69 6e 67 20 61 20 72 65 64 75 63 65 2c 20 74  ring a reduce, t
31cc2 68 65 20 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20  he only symbols 
31cc3 64 65 73 74 72 6f 79 65 64 20 61 72 65 20 74 68  destroyed are th
31cc4 6f 73 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 68  ose.    ** which
31cc5 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 52   appear on the R
31cc6 48 53 20 6f 66 20 74 68 65 20 72 75 6c 65 2c 20  HS of the rule, 
31cc7 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f  but which are no
31cc8 74 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e  t used.    ** in
31cc9 73 69 64 65 20 74 68 65 20 43 20 63 6f 64 65 2e  side the C code.
31cca 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
31ccb 20 31 36 30 3a 20 2f 2a 20 73 65 6c 65 63 74 20   160: /* select 
31ccc 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 34 3a  */.    case 194:
31ccd 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f   /* oneselect */
31cce 0a 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .{.sqlite3Select
31ccf 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
31cd0 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
31cd1 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  3));.}.      bre
31cd2 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37 34  ak;.    case 174
31cd3 3a 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20 20 20  : /* term */.   
31cd4 20 63 61 73 65 20 31 37 35 3a 20 2f 2a 20 65 78   case 175: /* ex
31cd5 70 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pr */.    case 2
31cd6 32 33 3a 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f  23: /* escape */
31cd7 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65  .{.sqlite3ExprDe
31cd8 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
31cd9 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 34   (yypminor->yy34
31cda 36 29 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20  6).pExpr);.}.   
31cdb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
31cdc 73 65 20 31 37 39 3a 20 2f 2a 20 69 64 78 6c 69  se 179: /* idxli
31cdd 73 74 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  st_opt */.    ca
31cde 73 65 20 31 38 37 3a 20 2f 2a 20 69 64 78 6c 69  se 187: /* idxli
31cdf 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  st */.    case 1
31ce0 39 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73  97: /* selcollis
31ce1 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30  t */.    case 20
31ce2 30 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70  0: /* groupby_op
31ce3 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30  t */.    case 20
31ce4 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70  2: /* orderby_op
31ce5 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30  t */.    case 20
31ce6 34 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20  4: /* sclp */.  
31ce7 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 73    case 214: /* s
31ce8 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  ortlist */.    c
31ce9 61 73 65 20 32 31 36 3a 20 2f 2a 20 6e 65 78 70  ase 216: /* nexp
31cea 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  rlist */.    cas
31ceb 65 20 32 31 37 3a 20 2f 2a 20 73 65 74 6c 69 73  e 217: /* setlis
31cec 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  t */.    case 22
31ced 30 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a  0: /* itemlist *
31cee 2f 0a 20 20 20 20 63 61 73 65 20 32 32 31 3a 20  /.    case 221: 
31cef 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20  /* exprlist */. 
31cf0 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20     case 227: /* 
31cf1 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f  case_exprlist */
31cf2 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .{.sqlite3ExprLi
31cf3 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
31cf4 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
31cf5 79 79 31 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20  yy14));.}.      
31cf6 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
31cf7 31 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65  193: /* fullname
31cf8 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 38   */.    case 198
31cf9 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  : /* from */.   
31cfa 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 73 65   case 206: /* se
31cfb 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20 20 20  ltablist */.    
31cfc 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 73 74 6c  case 207: /* stl
31cfd 5f 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73 71 6c  _prefix */.{.sql
31cfe 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
31cff 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
31d00 79 70 6d 69 6e 6f 72 2d 3e 79 79 36 35 29 29 3b  ypminor->yy65));
31d01 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
31d02 20 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a      case 199: /*
31d03 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 20 20   where_opt */.  
31d04 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a 20 68    case 201: /* h
31d05 61 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20  aving_opt */.   
31d06 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e   case 210: /* on
31d07 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
31d08 20 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65   215: /* sortite
31d09 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32  m */.    case 22
31d0a 36 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61  6: /* case_opera
31d0b 6e 64 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  nd */.    case 2
31d0c 32 38 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65  28: /* case_else
31d0d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 33 39   */.    case 239
31d0e 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65  : /* when_clause
31d0f 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 34 34   */.    case 244
31d10 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a  : /* key_opt */.
31d11 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  {.sqlite3ExprDel
31d12 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
31d13 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 33 32  (yypminor->yy132
31d14 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
31d15 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31 31 3a  k;.    case 211:
31d16 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f   /* using_opt */
31d17 0a 20 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f  .    case 213: /
31d18 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a  * inscollist */.
31d19 20 20 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a      case 219: /*
31d1a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
31d1b 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69  */.{.sqlite3IdLi
31d1c 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
31d1d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
31d1e 79 79 34 30 38 29 29 3b 0a 7d 0a 20 20 20 20 20  yy408));.}.     
31d1f 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
31d20 20 32 33 35 3a 20 2f 2a 20 74 72 69 67 67 65 72   235: /* trigger
31d21 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20 20 20  _cmd_list */.   
31d22 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 74 72   case 240: /* tr
31d23 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a 73  igger_cmd */.{.s
31d24 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
31d25 67 65 72 53 74 65 70 28 70 50 61 72 73 65 2d 3e  gerStep(pParse->
31d26 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
31d27 79 34 37 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20  y473));.}.      
31d28 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
31d29 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  237: /* trigger_
31d2a 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  event */.{.sqlit
31d2b 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70  e3IdListDelete(p
31d2c 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
31d2d 69 6e 6f 72 2d 3e 79 79 33 37 38 29 2e 62 29 3b  inor->yy378).b);
31d2e 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
31d2f 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 62 72      default:  br
31d30 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e 6f 20  eak;   /* If no 
31d31 64 65 73 74 72 75 63 74 6f 72 20 61 63 74 69 6f  destructor actio
31d32 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64 6f 20  n specified: do 
31d33 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 7d  nothing */.  }.}
31d34 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20  ../*.** Pop the 
31d35 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 6f  parser's stack o
31d36 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  nce..**.** If th
31d37 65 72 65 20 69 73 20 61 20 64 65 73 74 72 75 63  ere is a destruc
31d38 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73 73 6f  tor routine asso
31d39 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
31d3a 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a 20 69  token which.** i
31d3b 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68  s popped from th
31d3c 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 63 61  e stack, then ca
31d3d 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ll it..**.** Ret
31d3e 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f  urn the major to
31d3f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ken number for t
31d40 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70 65 64  he symbol popped
31d41 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31d42 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
31d43 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70 50  ack(yyParser *pP
31d44 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f 44 45  arser){.  YYCODE
31d45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a 20 20  TYPE yymajor;.  
31d46 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
31d47 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72 2d 3e  tos = &pParser->
31d48 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72 2d  yystack[pParser-
31d49 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 2f 2a 20 54  >yyidx];..  /* T
31d4a 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61  here is no mecha
31d4b 6e 69 73 6d 20 62 79 20 77 68 69 63 68 20 74 68  nism by which th
31d4c 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 63  e parser stack c
31d4d 61 6e 20 62 65 20 70 6f 70 70 65 64 20 62 65 6c  an be popped bel
31d4e 6f 77 0a 20 20 2a 2a 20 65 6d 70 74 79 20 69 6e  ow.  ** empty in
31d4f 20 53 51 4c 69 74 65 2e 20 20 2a 2f 0a 20 20 69   SQLite.  */.  i
31d50 66 28 20 4e 45 56 45 52 28 70 50 61 72 73 65 72  f( NEVER(pParser
31d51 2d 3e 79 79 69 64 78 3c 30 29 20 29 20 72 65 74  ->yyidx<0) ) ret
31d52 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e  urn 0;.#ifndef N
31d53 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
31d54 61 63 65 46 49 4c 45 20 26 26 20 70 50 61 72 73  aceFILE && pPars
31d55 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a  er->yyidx>=0 ){.
31d56 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
31d57 61 63 65 46 49 4c 45 2c 22 25 73 50 6f 70 70 69  aceFILE,"%sPoppi
31d58 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  ng %s\n",.      
31d59 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20  yyTracePrompt,. 
31d5a 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65       yyTokenName
31d5b 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b  [yytos->major]);
31d5c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79  .  }.#endif.  yy
31d5d 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d  major = yytos->m
31d5e 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74 72  ajor;.  yy_destr
31d5f 75 63 74 6f 72 28 70 50 61 72 73 65 72 2c 20 79  uctor(pParser, y
31d60 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e  ymajor, &yytos->
31d61 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73 65  minor);.  pParse
31d62 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65  r->yyidx--;.  re
31d63 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a  turn yymajor;.}.
31d64 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ./* .** Dealloca
31d65 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 61  te and destroy a
31d66 20 70 61 72 73 65 72 2e 20 20 44 65 73 74 72 75   parser.  Destru
31d67 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63 61  ctors are all ca
31d68 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20  lled for.** all 
31d69 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62  stack elements b
31d6a 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20 74  efore shutting t
31d6b 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a  he parser down..
31d6c 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
31d6d 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41   <ul>.** <li>  A
31d6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
31d6f 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73 68  parser.  This sh
31d70 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74 65  ould be a pointe
31d71 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61 69  r.**       obtai
31d72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
31d73 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20  ParserAlloc..** 
31d74 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20  <li>  A pointer 
31d75 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75 73  to a function us
31d76 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
31d77 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  mory obtained.**
31d78 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c         from mall
31d79 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a  oc..** </ul>.*/.
31d7a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
31d7b 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
31d7c 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a 70  rFree(.  void *p
31d7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
31d7e 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
31d7f 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  er to be deleted
31d80 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72 65   */.  void (*fre
31d81 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20 20  eProc)(void*)   
31d82 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73    /* Function us
31d83 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65  ed to reclaim me
31d84 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50  mory */.){.  yyP
31d85 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d  arser *pParser =
31d86 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20   (yyParser*)p;. 
31d87 20 2f 2a 20 49 6e 20 53 51 4c 69 74 65 2c 20 77   /* In SQLite, w
31d88 65 20 6e 65 76 65 72 20 74 72 79 20 74 6f 20 64  e never try to d
31d89 65 73 74 72 6f 79 20 61 20 70 61 72 73 65 72 20  estroy a parser 
31d8a 74 68 61 74 20 77 61 73 20 6e 6f 74 20 73 75 63  that was not suc
31d8b 63 65 73 73 66 75 6c 6c 79 0a 20 20 2a 2a 20 63  cessfully.  ** c
31d8c 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 66 69  reated in the fi
31d8d 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20  rst place. */.  
31d8e 69 66 28 20 4e 45 56 45 52 28 70 50 61 72 73 65  if( NEVER(pParse
31d8f 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  r==0) ) return;.
31d90 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 72    while( pParser
31d91 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
31d92 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
31d93 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66 20 59  (pParser);.#if Y
31d94 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
31d95 20 66 72 65 65 28 70 50 61 72 73 65 72 2d 3e 79   free(pParser->y
31d96 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69 66 0a  ystack);.#endif.
31d97 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28 28 76    (*freeProc)((v
31d98 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b 0a 7d  oid*)pParser);.}
31d99 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
31d9a 68 65 20 70 65 61 6b 20 64 65 70 74 68 20 6f 66  he peak depth of
31d9b 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72 20 61   the stack for a
31d9c 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69 66 64   parser..*/.#ifd
31d9d 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
31d9e 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 5f 50  CKDEPTH.SQLITE_P
31d9f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
31da0 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61  e3ParserStackPea
31da1 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 79 79  k(void *p){.  yy
31da2 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20  Parser *pParser 
31da3 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a  = (yyParser*)p;.
31da4 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 72    return pParser
31da5 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a 23 65  ->yyidxMax;.}.#e
31da6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
31da7 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
31da8 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61   action for a pa
31da9 72 73 65 72 20 67 69 76 65 6e 20 74 68 65 20 74  rser given the t
31daa 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d  erminal.** look-
31dab 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f  ahead token iLoo
31dac 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  kAhead..**.** If
31dad 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20   the look-ahead 
31dae 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44  token is YYNOCOD
31daf 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74 6f  E, then check to
31db0 20 73 65 65 20 69 66 20 74 68 65 20 61 63 74 69   see if the acti
31db1 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e  on is.** indepen
31db2 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b  dent of the look
31db3 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74 20 69  -ahead.  If it i
31db4 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61 63  s, return the ac
31db5 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a  tion, otherwise.
31db6 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f  ** return YY_NO_
31db7 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69  ACTION..*/.stati
31db8 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 73 68  c int yy_find_sh
31db9 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20 79 79  ift_action(.  yy
31dba 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 2c  Parser *pParser,
31dbb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
31dbc 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44  arser */.  YYCOD
31dbd 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64  ETYPE iLookAhead
31dbe 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b       /* The look
31dbf 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a  -ahead token */.
31dc0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
31dc1 74 20 73 74 61 74 65 6e 6f 20 3d 20 70 50 61 72  t stateno = pPar
31dc2 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61  ser->yystack[pPa
31dc3 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61  rser->yyidx].sta
31dc4 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20 73 74  teno;. .  if( st
31dc5 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54 5f 43  ateno>YY_SHIFT_C
31dc6 4f 55 4e 54 0a 20 20 20 7c 7c 20 28 69 20 3d 20  OUNT.   || (i = 
31dc7 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73 74  yy_shift_ofst[st
31dc8 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46  ateno])==YY_SHIF
31dc9 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20  T_USE_DFLT ){.  
31dca 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
31dcb 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
31dcc 7d 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f  }.  assert( iLoo
31dcd 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45  kAhead!=YYNOCODE
31dce 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b   );.  i += iLook
31dcf 41 68 65 61 64 3b 0a 20 20 69 66 28 20 69 3c 30  Ahead;.  if( i<0
31dd0 20 7c 7c 20 69 3e 3d 59 59 5f 41 43 54 54 41 42   || i>=YY_ACTTAB
31dd1 5f 43 4f 55 4e 54 20 7c 7c 20 79 79 5f 6c 6f 6f  _COUNT || yy_loo
31dd2 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b  kahead[i]!=iLook
31dd3 41 68 65 61 64 20 29 7b 0a 20 20 20 20 69 66 28  Ahead ){.    if(
31dd4 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b   iLookAhead>0 ){
31dd5 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41  .#ifdef YYFALLBA
31dd6 43 4b 0a 20 20 20 20 20 20 59 59 43 4f 44 45 54  CK.      YYCODET
31dd7 59 50 45 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20  YPE iFallback;  
31dd8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
31dd9 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20  lback token */. 
31dda 20 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68       if( iLookAh
31ddb 65 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c  ead<sizeof(yyFal
31ddc 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79  lback)/sizeof(yy
31ddd 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20  Fallback[0]).   
31dde 20 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46            && (iF
31ddf 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c  allback = yyFall
31de0 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  back[iLookAhead]
31de1 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20  )!=0 ){.#ifndef 
31de2 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69  NDEBUG.        i
31de3 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
31de4 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  {.          fpri
31de5 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
31de6 20 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20   "%sFALLBACK %s 
31de7 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  => %s\n",.      
31de8 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72         yyTracePr
31de9 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ompt, yyTokenNam
31dea 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79  e[iLookAhead], y
31deb 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c  yTokenName[iFall
31dec 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20  back]);.        
31ded 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
31dee 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f   return yy_find_
31def 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61  shift_action(pPa
31df0 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29  rser, iFallback)
31df1 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
31df2 0a 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41  .#ifdef YYWILDCA
31df3 52 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  RD.      {.     
31df4 20 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69     int j = i - i
31df5 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49  LookAhead + YYWI
31df6 4c 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20  LDCARD;.        
31df7 69 66 28 20 0a 23 69 66 20 59 59 5f 53 48 49 46  if( .#if YY_SHIF
31df8 54 5f 4d 49 4e 2b 59 59 57 49 4c 44 43 41 52 44  T_MIN+YYWILDCARD
31df9 3c 30 0a 20 20 20 20 20 20 20 20 20 20 6a 3e 3d  <0.          j>=
31dfa 30 20 26 26 0a 23 65 6e 64 69 66 0a 23 69 66 20  0 &&.#endif.#if 
31dfb 59 59 5f 53 48 49 46 54 5f 4d 41 58 2b 59 59 57  YY_SHIFT_MAX+YYW
31dfc 49 4c 44 43 41 52 44 3e 3d 59 59 5f 41 43 54 54  ILDCARD>=YY_ACTT
31dfd 41 42 5f 43 4f 55 4e 54 0a 20 20 20 20 20 20 20  AB_COUNT.       
31dfe 20 20 20 6a 3c 59 59 5f 41 43 54 54 41 42 5f 43     j<YY_ACTTAB_C
31dff 4f 55 4e 54 20 26 26 0a 23 65 6e 64 69 66 0a 20  OUNT &&.#endif. 
31e00 20 20 20 20 20 20 20 20 20 79 79 5f 6c 6f 6f 6b           yy_look
31e01 61 68 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44  ahead[j]==YYWILD
31e02 43 41 52 44 0a 20 20 20 20 20 20 20 20 29 7b 0a  CARD.        ){.
31e03 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
31e04 20 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54           if( yyT
31e05 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
31e06 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
31e07 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73  yyTraceFILE, "%s
31e08 57 49 4c 44 43 41 52 44 20 25 73 20 3d 3e 20 25  WILDCARD %s => %
31e09 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
31e0a 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d       yyTraceProm
31e0b 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  pt, yyTokenName[
31e0c 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54  iLookAhead], yyT
31e0d 6f 6b 65 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43  okenName[YYWILDC
31e0e 41 52 44 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  ARD]);.         
31e0f 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45   }.#endif /* NDE
31e10 42 55 47 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  BUG */.         
31e11 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f   return yy_actio
31e12 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n[j];.        }.
31e13 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f        }.#endif /
31e14 2a 20 59 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a  * YYWILDCARD */.
31e15 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
31e16 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74   yy_default[stat
31e17 65 6e 6f 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  eno];.  }else{. 
31e18 20 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74     return yy_act
31e19 69 6f 6e 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ion[i];.  }.}../
31e1a 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61 70  *.** Find the ap
31e1b 70 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e  propriate action
31e1c 20 66 6f 72 20 61 20 70 61 72 73 65 72 20 67 69   for a parser gi
31e1d 76 65 6e 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d  ven the non-term
31e1e 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65  inal.** look-ahe
31e1f 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68  ad token iLookAh
31e20 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ead..**.** If th
31e21 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  e look-ahead tok
31e22 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20  en is YYNOCODE, 
31e23 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65  then check to se
31e24 65 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20  e if the action 
31e25 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e  is.** independen
31e26 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68  t of the look-ah
31e27 65 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20  ead.  If it is, 
31e28 72 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f  return the actio
31e29 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20  n, otherwise.** 
31e2a 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54  return YY_NO_ACT
31e2b 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ION..*/.static i
31e2c 6e 74 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63  nt yy_find_reduc
31e2d 65 5f 61 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20  e_action(.  int 
31e2e 73 74 61 74 65 6e 6f 2c 20 20 20 20 20 20 20 20  stateno,        
31e2f 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
31e30 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f   state number */
31e31 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c  .  YYCODETYPE iL
31e32 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20  ookAhead     /* 
31e33 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  The look-ahead t
31e34 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oken */.){.  int
31e35 20 69 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52   i;.#ifdef YYERR
31e36 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20 73  ORSYMBOL.  if( s
31e37 74 61 74 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45  tateno>YY_REDUCE
31e38 5f 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20 72 65  _COUNT ){.    re
31e39 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
31e3a 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65  stateno];.  }.#e
31e3b 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20 73 74  lse.  assert( st
31e3c 61 74 65 6e 6f 3c 3d 59 59 5f 52 45 44 55 43 45  ateno<=YY_REDUCE
31e3d 5f 43 4f 55 4e 54 20 29 3b 0a 23 65 6e 64 69 66  _COUNT );.#endif
31e3e 0a 20 20 69 20 3d 20 79 79 5f 72 65 64 75 63 65  .  i = yy_reduce
31e3f 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a  _ofst[stateno];.
31e40 20 20 61 73 73 65 72 74 28 20 69 21 3d 59 59 5f    assert( i!=YY_
31e41 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20  REDUCE_USE_DFLT 
31e42 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f  );.  assert( iLo
31e43 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44  okAhead!=YYNOCOD
31e44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f  E );.  i += iLoo
31e45 6b 41 68 65 61 64 3b 0a 23 69 66 64 65 66 20 59  kAhead;.#ifdef Y
31e46 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69  YERRORSYMBOL.  i
31e47 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f  f( i<0 || i>=YY_
31e48 41 43 54 54 41 42 5f 43 4f 55 4e 54 20 7c 7c 20  ACTTAB_COUNT || 
31e49 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21  yy_lookahead[i]!
31e4a 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20  =iLookAhead ){. 
31e4b 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
31e4c 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
31e4d 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72   }.#else.  asser
31e4e 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f  t( i>=0 && i<YY_
31e4f 41 43 54 54 41 42 5f 43 4f 55 4e 54 20 29 3b 0a  ACTTAB_COUNT );.
31e50 20 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f    assert( yy_loo
31e51 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b  kahead[i]==iLook
31e52 41 68 65 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a  Ahead );.#endif.
31e53 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69    return yy_acti
31e54 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  on[i];.}../*.** 
31e55 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
31e56 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
31e57 69 66 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65  if the stack ove
31e58 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69  rflows..*/.stati
31e59 63 20 76 6f 69 64 20 79 79 53 74 61 63 6b 4f 76  c void yyStackOv
31e5a 65 72 66 6c 6f 77 28 79 79 50 61 72 73 65 72 20  erflow(yyParser 
31e5b 2a 79 79 70 50 61 72 73 65 72 2c 20 59 59 4d 49  *yypParser, YYMI
31e5c 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f  NORTYPE *yypMino
31e5d 72 29 7b 0a 20 20 20 73 71 6c 69 74 65 33 50 61  r){.   sqlite3Pa
31e5e 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20  rserARG_FETCH;. 
31e5f 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69    yypParser->yyi
31e60 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44  dx--;.#ifndef ND
31e61 45 42 55 47 0a 20 20 20 69 66 28 20 79 79 54 72  EBUG.   if( yyTr
31e62 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
31e63 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
31e64 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f 76 65  ILE,"%sStack Ove
31e65 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63  rflow!\n",yyTrac
31e66 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23  ePrompt);.   }.#
31e67 65 6e 64 69 66 0a 20 20 20 77 68 69 6c 65 28 20  endif.   while( 
31e68 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31e69 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72  >=0 ) yy_pop_par
31e6a 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72  ser_stack(yypPar
31e6b 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65  ser);.   /* Here
31e6c 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65   code is inserte
31e6d 64 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78 65  d which will exe
31e6e 63 75 74 65 20 69 66 20 74 68 65 20 70 61 72 73  cute if the pars
31e6f 65 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20 65  er.   ** stack e
31e70 76 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a  very overflows *
31e71 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  /..  UNUSED_PARA
31e72 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f 72 29 3b  METER(yypMinor);
31e73 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73 6f 6d 65   /* Silence some
31e74 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
31e75 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  gs */.  sqlite3E
31e76 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
31e77 22 70 61 72 73 65 72 20 73 74 61 63 6b 20 6f 76  "parser stack ov
31e78 65 72 66 6c 6f 77 22 29 3b 0a 20 20 70 50 61 72  erflow");.  pPar
31e79 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20 3d  se->parseError =
31e7a 20 31 3b 0a 20 20 20 73 71 6c 69 74 65 33 50 61   1;.   sqlite3Pa
31e7b 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f  rserARG_STORE; /
31e7c 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69  * Suppress warni
31e7d 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  ng about unused 
31e7e 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
31e7f 76 61 72 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  var */.}../*.** 
31e80 50 65 72 66 6f 72 6d 20 61 20 73 68 69 66 74 20  Perform a shift 
31e81 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  action..*/.stati
31e82 63 20 76 6f 69 64 20 79 79 5f 73 68 69 66 74 28  c void yy_shift(
31e83 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
31e84 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20  Parser,         
31e85 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 74   /* The parser t
31e86 6f 20 62 65 20 73 68 69 66 74 65 64 20 2a 2f 0a  o be shifted */.
31e87 20 20 69 6e 74 20 79 79 4e 65 77 53 74 61 74 65    int yyNewState
31e88 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
31e89 2f 2a 20 54 68 65 20 6e 65 77 20 73 74 61 74 65  /* The new state
31e8a 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a   to shift in */.
31e8b 20 20 69 6e 74 20 79 79 4d 61 6a 6f 72 2c 20 20    int yyMajor,  
31e8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31e8d 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  /* The major tok
31e8e 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a  en to shift in *
31e8f 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
31e90 2a 79 79 70 4d 69 6e 6f 72 20 20 20 20 20 20 20  *yypMinor       
31e91 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
31e92 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20  the minor token 
31e93 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 29  to shift in */.)
31e94 7b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79  {.  yyStackEntry
31e95 20 2a 79 79 74 6f 73 3b 0a 20 20 79 79 70 50 61   *yytos;.  yypPa
31e96 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23  rser->yyidx++;.#
31e97 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58  ifdef YYTRACKMAX
31e98 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69 66 28  STACKDEPTH.  if(
31e99 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
31e9a 78 3e 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  x>yypParser->yyi
31e9b 64 78 4d 61 78 20 29 7b 0a 20 20 20 20 79 79 70  dxMax ){.    yyp
31e9c 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78  Parser->yyidxMax
31e9d 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79   = yypParser->yy
31e9e 69 64 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  idx;.  }.#endif.
31e9f 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
31ea0 3e 30 20 0a 20 20 69 66 28 20 79 79 70 50 61 72  >0 .  if( yypPar
31ea1 73 65 72 2d 3e 79 79 69 64 78 3e 3d 59 59 53 54  ser->yyidx>=YYST
31ea2 41 43 4b 44 45 50 54 48 20 29 7b 0a 20 20 20 20  ACKDEPTH ){.    
31ea3 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28  yyStackOverflow(
31ea4 79 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d 69  yypParser, yypMi
31ea5 6e 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  nor);.    return
31ea6 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66  ;.  }.#else.  if
31ea7 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
31ea8 64 78 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e 79  dx>=yypParser->y
31ea9 79 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20 79 79  ystksz ){.    yy
31eaa 47 72 6f 77 53 74 61 63 6b 28 79 79 70 50 61 72  GrowStack(yypPar
31eab 73 65 72 29 3b 0a 20 20 20 20 69 66 28 20 79 79  ser);.    if( yy
31eac 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
31ead 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b  yypParser->yystk
31eae 73 7a 20 29 7b 0a 20 20 20 20 20 20 79 79 53 74  sz ){.      yySt
31eaf 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50  ackOverflow(yypP
31eb0 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29  arser, yypMinor)
31eb1 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
31eb2 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
31eb3 0a 20 20 79 79 74 6f 73 20 3d 20 26 79 79 70 50  .  yytos = &yypP
31eb4 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79  arser->yystack[y
31eb5 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  ypParser->yyidx]
31eb6 3b 0a 20 20 79 79 74 6f 73 2d 3e 73 74 61 74 65  ;.  yytos->state
31eb7 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e 54 59  no = (YYACTIONTY
31eb8 50 45 29 79 79 4e 65 77 53 74 61 74 65 3b 0a 20  PE)yyNewState;. 
31eb9 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20   yytos->major = 
31eba 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 4d 61  (YYCODETYPE)yyMa
31ebb 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69  jor;.  yytos->mi
31ebc 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b  nor = *yypMinor;
31ebd 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
31ebe 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
31ebf 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e  E && yypParser->
31ec0 79 79 69 64 78 3e 30 20 29 7b 0a 20 20 20 20 69  yyidx>0 ){.    i
31ec1 6e 74 20 69 3b 0a 20 20 20 20 66 70 72 69 6e 74  nt i;.    fprint
31ec2 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
31ec3 73 53 68 69 66 74 20 25 64 5c 6e 22 2c 79 79 54  sShift %d\n",yyT
31ec4 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77  racePrompt,yyNew
31ec5 53 74 61 74 65 29 3b 0a 20 20 20 20 66 70 72 69  State);.    fpri
31ec6 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
31ec7 22 25 73 53 74 61 63 6b 3a 22 2c 79 79 54 72 61  "%sStack:",yyTra
31ec8 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66  cePrompt);.    f
31ec9 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61  or(i=1; i<=yypPa
31eca 72 73 65 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b  rser->yyidx; i++
31ecb 29 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  ).      fprintf(
31ecc 79 79 54 72 61 63 65 46 49 4c 45 2c 22 20 25 73  yyTraceFILE," %s
31ecd 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  ",yyTokenName[yy
31ece 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
31ecf 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20  [i].major]);.   
31ed0 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
31ed1 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a  FILE,"\n");.  }.
31ed2 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65  #endif.}../* The
31ed3 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
31ed4 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
31ed5 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 76 65 72  ation about ever
31ed6 79 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69  y rule that.** i
31ed7 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 68  s used during th
31ed8 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61  e reduce..*/.sta
31ed9 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
31eda 20 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20   {.  YYCODETYPE 
31edb 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lhs;         /* 
31edc 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65  Symbol on the le
31edd 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
31ede 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e  the rule */.  un
31edf 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72 68 73  signed char nrhs
31ee0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
31ee1 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  of right-hand si
31ee2 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68  de symbols in th
31ee3 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75  e rule */.} yyRu
31ee4 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b  leInfo[] = {.  {
31ee5 20 31 34 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   142, 1 },.  { 1
31ee6 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33  43, 2 },.  { 143
31ee7 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20  , 1 },.  { 144, 
31ee8 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 33 20  1 },.  { 144, 3 
31ee9 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c  },.  { 145, 0 },
31eea 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20  .  { 145, 1 },. 
31eeb 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 145, 3 },.  {
31eec 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   146, 1 },.  { 1
31eed 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 39  47, 3 },.  { 149
31eee 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20  , 0 },.  { 149, 
31eef 31 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 32 20  1 },.  { 149, 2 
31ef0 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 30 20 7d 2c  },.  { 148, 0 },
31ef1 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20  .  { 148, 1 },. 
31ef2 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 148, 1 },.  {
31ef3 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   148, 1 },.  { 1
31ef4 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 2 },.  { 147
31ef5 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 2 },.  { 147, 
31ef6 32 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 31 20  2 },.  { 151, 1 
31ef7 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c  },.  { 151, 0 },
31ef8 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20  .  { 147, 2 },. 
31ef9 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 147, 3 },.  {
31efa 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   147, 5 },.  { 1
31efb 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 32  47, 2 },.  { 152
31efc 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20  , 6 },.  { 154, 
31efd 31 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 30 20  1 },.  { 156, 0 
31efe 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c  },.  { 156, 3 },
31eff 0a 20 20 7b 20 31 35 35 2c 20 31 20 7d 2c 0a 20  .  { 155, 1 },. 
31f00 20 7b 20 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 155, 0 },.  {
31f01 20 31 35 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   153, 4 },.  { 1
31f02 35 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 38  53, 2 },.  { 158
31f03 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20  , 3 },.  { 158, 
31f04 31 20 7d 2c 0a 20 20 7b 20 31 36 31 2c 20 33 20  1 },.  { 161, 3 
31f05 7d 2c 0a 20 20 7b 20 31 36 32 2c 20 31 20 7d 2c  },.  { 162, 1 },
31f06 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20  .  { 165, 1 },. 
31f07 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 165, 1 },.  {
31f08 20 31 36 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   166, 1 },.  { 1
31f09 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30  50, 1 },.  { 150
31f0a 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20  , 1 },.  { 150, 
31f0b 31 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 30 20  1 },.  { 163, 0 
31f0c 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c  },.  { 163, 1 },
31f0d 0a 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c 0a 20  .  { 167, 1 },. 
31f0e 20 7b 20 31 36 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 167, 4 },.  {
31f0f 20 31 36 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   167, 6 },.  { 1
31f10 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38  68, 1 },.  { 168
31f11 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20  , 2 },.  { 169, 
31f12 31 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20  1 },.  { 169, 1 
31f13 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 32 20 7d 2c  },.  { 164, 2 },
31f14 0a 20 20 7b 20 31 36 34 2c 20 30 20 7d 2c 0a 20  .  { 164, 0 },. 
31f15 20 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 172, 3 },.  {
31f16 20 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   172, 1 },.  { 1
31f17 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 2 },.  { 173
31f18 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 4 },.  { 173, 
31f19 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20  3 },.  { 173, 3 
31f1a 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  },.  { 173, 2 },
31f1b 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20  .  { 173, 2 },. 
31f1c 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 173, 3 },.  {
31f1d 20 31 37 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   173, 5 },.  { 1
31f1e 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 2 },.  { 173
31f1f 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 4 },.  { 173, 
31f20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20  4 },.  { 173, 1 
31f21 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  },.  { 173, 2 },
31f22 0a 20 20 7b 20 31 37 38 2c 20 30 20 7d 2c 0a 20  .  { 178, 0 },. 
31f23 20 7b 20 31 37 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 178, 1 },.  {
31f24 20 31 38 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   180, 0 },.  { 1
31f25 38 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32  80, 2 },.  { 182
31f26 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20  , 2 },.  { 182, 
31f27 33 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20  3 },.  { 182, 3 
31f28 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c  },.  { 183, 2 },
31f29 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20  .  { 183, 2 },. 
31f2a 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 183, 1 },.  {
31f2b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   183, 1 },.  { 1
31f2c 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 31  83, 2 },.  { 181
31f2d 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20  , 3 },.  { 181, 
31f2e 32 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 30 20  2 },.  { 184, 0 
31f2f 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c  },.  { 184, 2 },
31f30 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20  .  { 184, 2 },. 
31f31 20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 159, 0 },.  {
31f32 20 31 35 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   159, 2 },.  { 1
31f33 38 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 35  85, 3 },.  { 185
31f34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20  , 2 },.  { 185, 
31f35 31 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 32 20  1 },.  { 186, 2 
31f36 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 37 20 7d 2c  },.  { 186, 7 },
31f37 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20  .  { 186, 5 },. 
31f38 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b   { 186, 5 },.  {
31f39 20 31 38 36 2c 20 31 30 20 7d 2c 0a 20 20 7b 20   186, 10 },.  { 
31f3a 31 38 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  188, 0 },.  { 18
31f3b 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c  8, 1 },.  { 176,
31f3c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33   0 },.  { 176, 3
31f3d 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 30 20 7d   },.  { 189, 0 }
31f3e 2c 0a 20 20 7b 20 31 38 39 2c 20 32 20 7d 2c 0a  ,.  { 189, 2 },.
31f3f 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20    { 190, 1 },.  
31f40 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 190, 1 },.  { 
31f41 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  190, 1 },.  { 14
31f42 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 39 32 2c  7, 4 },.  { 192,
31f43 20 32 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 30   2 },.  { 192, 0
31f44 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d   },.  { 147, 8 }
31f45 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
31f46 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20    { 147, 1 },.  
31f47 7b 20 31 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 160, 1 },.  { 
31f48 31 36 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39  160, 3 },.  { 19
31f49 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c  5, 1 },.  { 195,
31f4a 20 32 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31   2 },.  { 195, 1
31f4b 20 7d 2c 0a 20 20 7b 20 31 39 34 2c 20 39 20 7d   },.  { 194, 9 }
31f4c 2c 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a  ,.  { 196, 1 },.
31f4d 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20    { 196, 1 },.  
31f4e 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 196, 0 },.  { 
31f4f 32 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  204, 2 },.  { 20
31f50 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c  4, 0 },.  { 197,
31f51 20 33 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 32   3 },.  { 197, 2
31f52 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 34 20 7d   },.  { 197, 4 }
31f53 2c 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d 2c 0a  ,.  { 205, 2 },.
31f54 20 20 7b 20 32 30 35 2c 20 31 20 7d 2c 0a 20 20    { 205, 1 },.  
31f55 7b 20 32 30 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 205, 0 },.  { 
31f56 31 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  198, 0 },.  { 19
31f57 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c  8, 2 },.  { 207,
31f58 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 30   2 },.  { 207, 0
31f59 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d   },.  { 206, 7 }
31f5a 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a  ,.  { 206, 7 },.
31f5b 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20    { 206, 7 },.  
31f5c 7b 20 31 35 37 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 157, 0 },.  { 
31f5d 31 35 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39  157, 2 },.  { 19
31f5e 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c  3, 2 },.  { 208,
31f5f 20 31 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 32   1 },.  { 208, 2
31f60 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d   },.  { 208, 3 }
31f61 2c 0a 20 20 7b 20 32 30 38 2c 20 34 20 7d 2c 0a  ,.  { 208, 4 },.
31f62 20 20 7b 20 32 31 30 2c 20 32 20 7d 2c 0a 20 20    { 210, 2 },.  
31f63 7b 20 32 31 30 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 210, 0 },.  { 
31f64 32 30 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  209, 0 },.  { 20
31f65 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 39 2c  9, 3 },.  { 209,
31f66 20 32 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 34   2 },.  { 211, 4
31f67 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 30 20 7d   },.  { 211, 0 }
31f68 2c 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d 2c 0a  ,.  { 202, 0 },.
31f69 20 20 7b 20 32 30 32 2c 20 33 20 7d 2c 0a 20 20    { 202, 3 },.  
31f6a 7b 20 32 31 34 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 214, 4 },.  { 
31f6b 32 31 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31  214, 2 },.  { 21
31f6c 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c  5, 1 },.  { 177,
31f6d 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31   1 },.  { 177, 1
31f6e 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 30 20 7d   },.  { 177, 0 }
31f6f 2c 0a 20 20 7b 20 32 30 30 2c 20 30 20 7d 2c 0a  ,.  { 200, 0 },.
31f70 20 20 7b 20 32 30 30 2c 20 33 20 7d 2c 0a 20 20    { 200, 3 },.  
31f71 7b 20 32 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 201, 0 },.  { 
31f72 32 30 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  201, 2 },.  { 20
31f73 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 33 2c  3, 0 },.  { 203,
31f74 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34   2 },.  { 203, 4
31f75 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d   },.  { 203, 4 }
31f76 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a  ,.  { 147, 5 },.
31f77 20 20 7b 20 31 39 39 2c 20 30 20 7d 2c 0a 20 20    { 199, 0 },.  
31f78 7b 20 31 39 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 199, 2 },.  { 
31f79 31 34 37 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 31  147, 7 },.  { 21
31f7a 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31 37 2c  7, 5 },.  { 217,
31f7b 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38   3 },.  { 147, 8
31f7c 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d   },.  { 147, 5 }
31f7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a  ,.  { 147, 6 },.
31f7e 20 20 7b 20 32 31 38 2c 20 32 20 7d 2c 0a 20 20    { 218, 2 },.  
31f7f 7b 20 32 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 218, 1 },.  { 
31f80 32 32 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32  220, 3 },.  { 22
31f81 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c  0, 1 },.  { 219,
31f82 20 30 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 33   0 },.  { 219, 3
31f83 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 33 20 7d   },.  { 213, 3 }
31f84 2c 0a 20 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a  ,.  { 213, 1 },.
31f85 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20    { 175, 1 },.  
31f86 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
31f87 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  174, 1 },.  { 17
31f88 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 1 },.  { 175,
31f89 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   1 },.  { 175, 3
31f8a 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d   },.  { 175, 5 }
31f8b 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a  ,.  { 174, 1 },.
31f8c 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20    { 174, 1 },.  
31f8d 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 175, 1 },.  { 
31f8e 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  175, 1 },.  { 17
31f8f 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
31f90 20 36 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35   6 },.  { 175, 5
31f91 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
31f92 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a  ,.  { 174, 1 },.
31f93 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31f94 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
31f95 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
31f96 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
31f97 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
31f98 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
31f99 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
31f9a 20 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20    { 222, 1 },.  
31f9b 7b 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 222, 2 },.  { 
31f9c 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  222, 1 },.  { 22
31f9d 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c  2, 2 },.  { 223,
31f9e 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 30   2 },.  { 223, 0
31f9f 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
31fa0 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a  ,.  { 175, 2 },.
31fa1 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31fa2 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
31fa3 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
31fa4 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 2 },.  { 175,
31fa5 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32   2 },.  { 175, 2
31fa6 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d   },.  { 175, 2 }
31fa7 2c 0a 20 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0a  ,.  { 224, 1 },.
31fa8 20 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0a 20 20    { 224, 2 },.  
31fa9 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 175, 5 },.  { 
31faa 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  225, 1 },.  { 22
31fab 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 2 },.  { 175,
31fac 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   5 },.  { 175, 3
31fad 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d   },.  { 175, 5 }
31fae 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a  ,.  { 175, 4 },.
31faf 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20    { 175, 4 },.  
31fb0 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 175, 5 },.  { 
31fb1 32 32 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32  227, 5 },.  { 22
31fb2 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 32 38 2c  7, 4 },.  { 228,
31fb3 20 32 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 30   2 },.  { 228, 0
31fb4 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 31 20 7d   },.  { 226, 1 }
31fb5 2c 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a  ,.  { 226, 0 },.
31fb6 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20    { 221, 1 },.  
31fb7 7b 20 32 32 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 221, 0 },.  { 
31fb8 32 31 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31  216, 3 },.  { 21
31fb9 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  6, 1 },.  { 147,
31fba 20 31 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20   11 },.  { 229, 
31fbb 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20  1 },.  { 229, 0 
31fbc 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c  },.  { 179, 0 },
31fbd 0a 20 20 7b 20 31 37 39 2c 20 33 20 7d 2c 0a 20  .  { 179, 3 },. 
31fbe 20 7b 20 31 38 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 187, 5 },.  {
31fbf 20 31 38 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   187, 3 },.  { 2
31fc0 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 30  30, 0 },.  { 230
31fc1 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 2 },.  { 147, 
31fc2 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  4 },.  { 147, 1 
31fc3 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
31fc4 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
31fc5 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
31fc6 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   147, 6 },.  { 1
31fc7 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 5 },.  { 147
31fc8 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20  , 6 },.  { 231, 
31fc9 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20  1 },.  { 231, 1 
31fca 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c  },.  { 231, 1 },
31fcb 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20  .  { 231, 1 },. 
31fcc 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 231, 1 },.  {
31fcd 20 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   170, 2 },.  { 1
31fce 37 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33  71, 2 },.  { 233
31fcf 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20  , 1 },.  { 232, 
31fd0 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 30 20  1 },.  { 232, 0 
31fd1 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  },.  { 147, 5 },
31fd2 0a 20 20 7b 20 32 33 34 2c 20 31 31 20 7d 2c 0a  .  { 234, 11 },.
31fd3 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20    { 236, 1 },.  
31fd4 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 236, 1 },.  { 
31fd5 32 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  236, 2 },.  { 23
31fd6 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 37 2c  6, 0 },.  { 237,
31fd7 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31   1 },.  { 237, 1
31fd8 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 33 20 7d   },.  { 237, 3 }
31fd9 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a  ,.  { 238, 0 },.
31fda 20 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a 20 20    { 238, 3 },.  
31fdb 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 239, 0 },.  { 
31fdc 32 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  239, 2 },.  { 23
31fdd 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c  5, 3 },.  { 235,
31fde 20 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31   2 },.  { 241, 1
31fdf 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 33 20 7d   },.  { 241, 3 }
31fe0 2c 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d 2c 0a  ,.  { 242, 0 },.
31fe1 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a 20 20    { 242, 3 },.  
31fe2 7b 20 32 34 32 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 242, 2 },.  { 
31fe3 32 34 30 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34  240, 7 },.  { 24
31fe4 30 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 34 30 2c  0, 8 },.  { 240,
31fe5 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 35   5 },.  { 240, 5
31fe6 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31 20 7d   },.  { 240, 1 }
31fe7 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a  ,.  { 175, 4 },.
31fe8 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a 20 20    { 175, 6 },.  
31fe9 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 191, 1 },.  { 
31fea 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39  191, 1 },.  { 19
31feb 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  1, 1 },.  { 147,
31fec 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36   4 },.  { 147, 6
31fed 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d   },.  { 147, 3 }
31fee 2c 0a 20 20 7b 20 32 34 34 2c 20 30 20 7d 2c 0a  ,.  { 244, 0 },.
31fef 20 20 7b 20 32 34 34 2c 20 32 20 7d 2c 0a 20 20    { 244, 2 },.  
31ff0 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 243, 1 },.  { 
31ff1 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  243, 0 },.  { 14
31ff2 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 1 },.  { 147,
31ff3 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31   3 },.  { 147, 1
31ff4 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d   },.  { 147, 3 }
31ff5 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a  ,.  { 147, 6 },.
31ff6 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20    { 147, 6 },.  
31ff7 7b 20 32 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 245, 1 },.  { 
31ff8 32 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34  246, 0 },.  { 24
31ff9 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  6, 1 },.  { 147,
31ffa 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34   1 },.  { 147, 4
31ffb 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 37 20 7d   },.  { 247, 7 }
31ffc 2c 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d 2c 0a  ,.  { 248, 1 },.
31ffd 20 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a 20 20    { 248, 3 },.  
31ffe 7b 20 32 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 249, 0 },.  { 
31fff 32 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 35  249, 2 },.  { 25
32000 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 35 30 2c  0, 1 },.  { 250,
32001 20 33 20 7d 2c 0a 20 20 7b 20 32 35 31 2c 20 31   3 },.  { 251, 1
32002 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 30 20 7d   },.  { 252, 0 }
32003 2c 0a 20 20 7b 20 32 35 32 2c 20 34 20 7d 2c 0a  ,.  { 252, 4 },.
32004 20 20 7b 20 32 35 32 2c 20 32 20 7d 2c 0a 7d 3b    { 252, 2 },.};
32005 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  ..static void yy
32006 5f 61 63 63 65 70 74 28 79 79 50 61 72 73 65 72  _accept(yyParser
32007 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20  *);  /* Forward 
32008 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a  Declaration */..
32009 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20  /*.** Perform a 
3200a 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 61 6e  reduce action an
3200b 64 20 74 68 65 20 73 68 69 66 74 20 74 68 61 74  d the shift that
3200c 20 6d 75 73 74 20 69 6d 6d 65 64 69 61 74 65 6c   must immediatel
3200d 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68 65 20  y.** follow the 
3200e 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  reduce..*/.stati
3200f 63 20 76 6f 69 64 20 79 79 5f 72 65 64 75 63 65  c void yy_reduce
32010 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  (.  yyParser *yy
32011 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20  pParser,        
32012 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
32013 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c 65 6e 6f  /.  int yyruleno
32014 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32015 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68   /* Number of th
32016 65 20 72 75 6c 65 20 62 79 20 77 68 69 63 68 20  e rule by which 
32017 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a 29 7b 0a  to reduce */.){.
32018 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b 20 20 20    int yygoto;   
32019 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3201a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 73 74    /* The next st
3201b 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 79 79 61  ate */.  int yya
3201c 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ct;             
3201d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3201e 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a 2f 0a 20  next action */. 
3201f 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 67   YYMINORTYPE yyg
32020 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20 20 20 20  otominor;       
32021 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74   /* The LHS of t
32022 68 65 20 72 75 6c 65 20 72 65 64 75 63 65 64 20  he rule reduced 
32023 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  */.  yyStackEntr
32024 79 20 2a 79 79 6d 73 70 3b 20 20 20 20 20 20 20  y *yymsp;       
32025 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20       /* The top 
32026 6f 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20  of the parser's 
32027 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 79  stack */.  int y
32028 79 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  ysize;          
32029 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d             /* Am
3202a 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74 68 65 20  ount to pop the 
3202b 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74  stack */.  sqlit
3202c 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
3202d 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20 26 79 79  H;.  yymsp = &yy
3202e 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
3202f 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  [yypParser->yyid
32030 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  x];.#ifndef NDEB
32031 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
32032 46 49 4c 45 20 26 26 20 79 79 72 75 6c 65 6e 6f  FILE && yyruleno
32033 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 26 26 20  >=0 .        && 
32034 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29 28 73  yyruleno<(int)(s
32035 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65  izeof(yyRuleName
32036 29 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e  )/sizeof(yyRuleN
32037 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20 20 20 20  ame[0])) ){.    
32038 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
32039 49 4c 45 2c 20 22 25 73 52 65 64 75 63 65 20 5b  ILE, "%sReduce [
3203a 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61 63 65  %s].\n", yyTrace
3203b 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79  Prompt,.      yy
3203c 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75 6c 65 6e  RuleName[yyrulen
3203d 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  o]);.  }.#endif 
3203e 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 20 20  /* NDEBUG */..  
3203f 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f 6d 70 6c  /* Silence compl
32040 61 69 6e 74 73 20 66 72 6f 6d 20 70 75 72 69 66  aints from purif
32041 79 20 61 62 6f 75 74 20 79 79 67 6f 74 6f 6d 69  y about yygotomi
32042 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69 6e 69 74  nor being uninit
32043 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20 69 6e 20  ialized.  ** in 
32044 73 6f 6d 65 20 63 61 73 65 73 20 77 68 65 6e 20  some cases when 
32045 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74  it is copied int
32046 6f 20 74 68 65 20 73 74 61 63 6b 20 61 66 74 65  o the stack afte
32047 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  r the following.
32048 20 20 2a 2a 20 73 77 69 74 63 68 2e 20 20 79 79    ** switch.  yy
32049 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20 75 6e 69  gotominor is uni
3204a 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20  nitialized when 
3204b 61 20 72 75 6c 65 20 72 65 64 75 63 65 73 20 74  a rule reduces t
3204c 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f  hat does.  ** no
3204d 74 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  t set the value 
3204e 6f 66 20 69 74 73 20 6c 65 66 74 2d 68 61 6e 64  of its left-hand
3204f 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d 69 6e 61   side nontermina
32050 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74 68 65 0a  l.  Leaving the.
32051 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68    ** value of th
32052 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 75 6e  e nonterminal un
32053 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 73 20 75  initialized is u
32054 74 74 65 72 6c 79 20 68 61 72 6d 6c 65 73 73 20  tterly harmless 
32055 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73 20  as long.  ** as 
32056 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 65 76  the value is nev
32057 65 72 20 75 73 65 64 2e 20 20 53 6f 20 72 65 61  er used.  So rea
32058 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20 74 68 69  lly the only thi
32059 6e 67 20 74 68 69 73 20 63 6f 64 65 0a 20 20 2a  ng this code.  *
3205a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65 73 20 69  * accomplishes i
3205b 73 20 74 6f 20 71 75 69 65 74 65 6e 20 70 75 72  s to quieten pur
3205c 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a  ify.  .  **.  **
3205d 20 32 30 30 37 2d 30 31 2d 31 36 3a 20 20 54 68   2007-01-16:  Th
3205e 65 20 77 69 72 65 73 68 61 72 6b 20 70 72 6f 6a  e wireshark proj
3205f 65 63 74 20 28 77 77 77 2e 77 69 72 65 73 68 61  ect (www.wiresha
32060 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74 73 20  rk.org) reports 
32061 74 68 61 74 0a 20 20 2a 2a 20 77 69 74 68 6f 75  that.  ** withou
32062 74 20 74 68 69 73 20 63 6f 64 65 2c 20 74 68 65  t this code, the
32063 69 72 20 70 61 72 73 65 72 20 73 65 67 66 61 75  ir parser segfau
32064 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75  lts.  I'm not su
32065 72 65 20 77 68 61 74 20 74 68 65 72 65 0a 20 20  re what there.  
32066 2a 2a 20 70 61 72 73 65 72 20 69 73 20 64 6f 69  ** parser is doi
32067 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20  ng to make this 
32068 68 61 70 70 65 6e 2e 20 20 54 68 69 73 20 69 73  happen.  This is
32069 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 67 20   the second bug 
3206a 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66 72 6f 6d  report.  ** from
3206b 20 77 69 72 65 73 68 61 72 6b 20 74 68 69 73 20   wireshark this 
3206c 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79 20 74  week.  Clearly t
3206d 68 65 79 20 61 72 65 20 73 74 72 65 73 73 69 6e  hey are stressin
3206e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79 73 0a  g Lemon in ways.
3206f 20 20 2a 2a 20 74 68 61 74 20 69 74 20 68 61 73    ** that it has
32070 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f   not been previo
32071 75 73 6c 79 20 73 74 72 65 73 73 65 64 2e 2e 2e  usly stressed...
32072 20 20 28 53 51 4c 69 74 65 20 74 69 63 6b 65 74    (SQLite ticket
32073 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a 20 20 2f   #2172).  */.  /
32074 2a 6d 65 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d  *memset(&yygotom
32075 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  inor, 0, sizeof(
32076 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f  yygotominor));*/
32077 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d  .  yygotominor =
32078 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a   yyzerominor;...
32079 20 20 73 77 69 74 63 68 28 20 79 79 72 75 6c 65    switch( yyrule
3207a 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67 69 6e  no ){.  /* Begin
3207b 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20 74 68  ning here are th
3207c 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61 73 65  e reduction case
3207d 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20 65 78  s.  A typical ex
3207e 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f  ample.  ** follo
3207f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73 65 20  ws:.  **   case 
32080 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c  0:.  **  #line <
32081 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72  lineno> <grammar
32082 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 7b  file>.  **     {
32083 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20 20 20   ... }          
32084 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c 69 65   // User supplie
32085 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23 6c 69  d code.  **  #li
32086 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69  ne <lineno> <thi
32087 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20  sfile>.  **     
32088 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20  break;.  */.    
32089 20 20 63 61 73 65 20 35 3a 20 2f 2a 20 65 78 70    case 5: /* exp
3208a 6c 61 69 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71  lain ::= */.{ sq
3208b 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28  lite3BeginParse(
3208c 70 50 61 72 73 65 2c 20 30 29 3b 20 7d 0a 20 20  pParse, 0); }.  
3208d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3208e 20 20 20 63 61 73 65 20 36 3a 20 2f 2a 20 65 78     case 6: /* ex
3208f 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49  plain ::= EXPLAI
32090 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65  N */.{ sqlite3Be
32091 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c  ginParse(pParse,
32092 20 31 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62   1); }.        b
32093 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32094 20 37 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a   7: /* explain :
32095 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59  := EXPLAIN QUERY
32096 20 50 4c 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74   PLAN */.{ sqlit
32097 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  e3BeginParse(pPa
32098 72 73 65 2c 20 32 29 3b 20 7d 0a 20 20 20 20 20  rse, 2); }.     
32099 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3209a 63 61 73 65 20 38 3a 20 2f 2a 20 63 6d 64 78 20  case 8: /* cmdx 
3209b 3a 3a 3d 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c  ::= cmd */.{ sql
3209c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
3209d 28 70 50 61 72 73 65 29 3b 20 7d 0a 20 20 20 20  (pParse); }.    
3209e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3209f 20 63 61 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20   case 9: /* cmd 
320a0 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e 73 74  ::= BEGIN transt
320a1 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f  ype trans_opt */
320a2 0a 7b 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72  .{sqlite3BeginTr
320a3 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ansaction(pParse
320a4 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
320a5 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20  r.yy328);}.     
320a6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
320a7 63 61 73 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e  case 13: /* tran
320a8 73 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  stype ::= */.{yy
320a9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
320aa 3d 20 54 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0a  = TK_DEFERRED;}.
320ab 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
320ac 20 20 20 20 20 63 61 73 65 20 31 34 3a 20 2f 2a       case 14: /*
320ad 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44   transtype ::= D
320ae 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20 20  EFERRED */.     
320af 20 63 61 73 65 20 31 35 3a 20 2f 2a 20 74 72 61   case 15: /* tra
320b0 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44  nstype ::= IMMED
320b1 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61  IATE */ yytestca
320b2 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 29  se(yyruleno==15)
320b3 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 3a  ;.      case 16:
320b4 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a   /* transtype ::
320b5 3d 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 20 79  = EXCLUSIVE */ y
320b6 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
320b7 6e 6f 3d 3d 31 36 29 3b 0a 20 20 20 20 20 20 63  no==16);.      c
320b8 61 73 65 20 31 31 34 3a 20 2f 2a 20 6d 75 6c 74  ase 114: /* mult
320b9 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
320ba 4e 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  NION */ yytestca
320bb 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 34  se(yyruleno==114
320bc 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  );.      case 11
320bd 36 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63  6: /* multiselec
320be 74 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c  t_op ::= EXCEPT|
320bf 49 4e 54 45 52 53 45 43 54 20 2a 2f 20 79 79 74  INTERSECT */ yyt
320c0 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
320c1 3d 3d 31 31 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==116);.{yygotom
320c2 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79 6d  inor.yy328 = yym
320c3 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20  sp[0].major;}.  
320c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
320c5 20 20 20 63 61 73 65 20 31 37 3a 20 2f 2a 20 63     case 17: /* c
320c6 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72  md ::= COMMIT tr
320c7 61 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20  ans_opt */.     
320c8 20 63 61 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64   case 18: /* cmd
320c9 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f   ::= END trans_o
320ca 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pt */ yytestcase
320cb 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 29 3b 0a  (yyruleno==18);.
320cc 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72  {sqlite3CommitTr
320cd 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ansaction(pParse
320ce 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
320cf 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
320d0 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c  : /* cmd ::= ROL
320d1 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20  LBACK trans_opt 
320d2 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c 6c 62  */.{sqlite3Rollb
320d3 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  ackTransaction(p
320d4 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
320d5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
320d6 73 65 20 32 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 22: /* cmd ::
320d7 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 20 2a  = SAVEPOINT nm *
320d8 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76  /.{.  sqlite3Sav
320d9 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53  epoint(pParse, S
320da 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 20  AVEPOINT_BEGIN, 
320db 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
320dc 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
320dd 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320de 65 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 23: /* cmd ::=
320df 20 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f 69   RELEASE savepoi
320e0 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20  nt_opt nm */.{. 
320e1 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e   sqlite3Savepoin
320e2 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f  t(pParse, SAVEPO
320e3 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 26 79 79  INT_RELEASE, &yy
320e4 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
320e5 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
320e6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
320e7 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f  4: /* cmd ::= RO
320e8 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74  LLBACK trans_opt
320e9 20 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70   TO savepoint_op
320ea 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  t nm */.{.  sqli
320eb 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61  te3Savepoint(pPa
320ec 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52  rse, SAVEPOINT_R
320ed 4f 4c 4c 42 41 43 4b 2c 20 26 79 79 6d 73 70 5b  OLLBACK, &yymsp[
320ee 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
320ef 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
320f0 20 20 20 20 20 20 63 61 73 65 20 32 36 3a 20 2f        case 26: /
320f1 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a  * create_table :
320f2 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70  := createkw temp
320f3 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73   TABLE ifnotexis
320f4 74 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a  ts nm dbnm */.{.
320f5 20 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54     sqlite3StartT
320f6 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79 6d  able(pParse,&yym
320f7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
320f8 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
320f9 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  .yy0,yymsp[-4].m
320fa 69 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c 79  inor.yy328,0,0,y
320fb 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
320fc 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y328);.}.       
320fd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
320fe 73 65 20 32 37 3a 20 2f 2a 20 63 72 65 61 74 65  se 27: /* create
320ff 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 20 2a 2f  kw ::= CREATE */
32100 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d  .{.  pParse->db-
32101 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
32102 6c 65 64 20 3d 20 30 3b 0a 20 20 79 79 67 6f 74  led = 0;.  yygot
32103 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d  ominor.yy0 = yym
32104 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b  sp[0].minor.yy0;
32105 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32106 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 3a  ;.      case 28:
32107 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73 20   /* ifnotexists 
32108 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
32109 65 20 33 31 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a  e 31: /* temp ::
3210a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3210b 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 29 3b 0a 20  yyruleno==31);. 
3210c 20 20 20 20 20 63 61 73 65 20 37 30 3a 20 2f 2a       case 70: /*
3210d 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 20   autoinc ::= */ 
3210e 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3210f 65 6e 6f 3d 3d 37 30 29 3b 0a 20 20 20 20 20 20  eno==70);.      
32110 63 61 73 65 20 38 32 3a 20 2f 2a 20 64 65 66 65  case 82: /* defe
32111 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20  r_subclause ::= 
32112 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 69  NOT DEFERRABLE i
32113 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
32114 64 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73 74 63  d_opt */ yytestc
32115 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 32  ase(yyruleno==82
32116 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 34  );.      case 84
32117 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72  : /* init_deferr
32118 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20  ed_pred_opt ::= 
32119 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3211a 72 75 6c 65 6e 6f 3d 3d 38 34 29 3b 0a 20 20 20  ruleno==84);.   
3211b 20 20 20 63 61 73 65 20 38 36 3a 20 2f 2a 20 69     case 86: /* i
3211c 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
3211d 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
3211e 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f  LLY IMMEDIATE */
3211f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32120 6c 65 6e 6f 3d 3d 38 36 29 3b 0a 20 20 20 20 20  leno==86);.     
32121 20 63 61 73 65 20 39 37 3a 20 2f 2a 20 64 65 66   case 97: /* def
32122 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
32123 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
32124 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 37 29  se(yyruleno==97)
32125 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 38  ;.      case 108
32126 3a 20 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a  : /* ifexists ::
32127 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32128 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 38 29 3b 0a  yyruleno==108);.
32129 20 20 20 20 20 20 63 61 73 65 20 31 31 39 3a 20        case 119: 
3212a 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20  /* distinct ::= 
3212b 41 4c 4c 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ALL */ yytestcas
3212c 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 39 29  e(yyruleno==119)
3212d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 30  ;.      case 120
3212e 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
3212f 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32130 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0a  yyruleno==120);.
32131 20 20 20 20 20 20 63 61 73 65 20 32 32 32 3a 20        case 222: 
32132 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  /* between_op ::
32133 3d 20 42 45 54 57 45 45 4e 20 2a 2f 20 79 79 74  = BETWEEN */ yyt
32134 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32135 3d 3d 32 32 32 29 3b 0a 20 20 20 20 20 20 63 61  ==222);.      ca
32136 73 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70  se 225: /* in_op
32137 20 3a 3a 3d 20 49 4e 20 2a 2f 20 79 79 74 65 73   ::= IN */ yytes
32138 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32139 32 32 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  225);.{yygotomin
3213a 6f 72 2e 79 79 33 32 38 20 3d 20 30 3b 7d 0a 20  or.yy328 = 0;}. 
3213b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3213c 20 20 20 20 63 61 73 65 20 32 39 3a 20 2f 2a 20      case 29: /* 
3213d 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20  ifnotexists ::= 
3213e 49 46 20 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f  IF NOT EXISTS */
3213f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 3a 20  .      case 30: 
32140 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50  /* temp ::= TEMP
32141 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32142 79 72 75 6c 65 6e 6f 3d 3d 33 30 29 3b 0a 20 20  yruleno==30);.  
32143 20 20 20 20 63 61 73 65 20 37 31 3a 20 2f 2a 20      case 71: /* 
32144 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f  autoinc ::= AUTO
32145 49 4e 43 52 20 2a 2f 20 79 79 74 65 73 74 63 61  INCR */ yytestca
32146 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 37 31 29  se(yyruleno==71)
32147 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 35 3a  ;.      case 85:
32148 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65   /* init_deferre
32149 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  d_pred_opt ::= I
3214a 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
3214b 44 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  D */ yytestcase(
3214c 79 79 72 75 6c 65 6e 6f 3d 3d 38 35 29 3b 0a 20  yyruleno==85);. 
3214d 20 20 20 20 20 63 61 73 65 20 31 30 37 3a 20 2f       case 107: /
3214e 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49  * ifexists ::= I
3214f 46 20 45 58 49 53 54 53 20 2a 2f 20 79 79 74 65  F EXISTS */ yyte
32150 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32151 3d 31 30 37 29 3b 0a 20 20 20 20 20 20 63 61 73  =107);.      cas
32152 65 20 31 31 38 3a 20 2f 2a 20 64 69 73 74 69 6e  e 118: /* distin
32153 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20  ct ::= DISTINCT 
32154 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32155 72 75 6c 65 6e 6f 3d 3d 31 31 38 29 3b 0a 20 20  ruleno==118);.  
32156 20 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f 2a      case 223: /*
32157 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20   between_op ::= 
32158 4e 4f 54 20 42 45 54 57 45 45 4e 20 2a 2f 20 79  NOT BETWEEN */ y
32159 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3215a 6e 6f 3d 3d 32 32 33 29 3b 0a 20 20 20 20 20 20  no==223);.      
3215b 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 69 6e 5f  case 226: /* in_
3215c 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f  op ::= NOT IN */
3215d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3215e 6c 65 6e 6f 3d 3d 32 32 36 29 3b 0a 7b 79 79 67  leno==226);.{yyg
3215f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
32160 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   1;}.        bre
32161 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
32162 32 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62  2: /* create_tab
32163 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63  le_args ::= LP c
32164 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69  olumnlist consli
32165 73 74 5f 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20  st_opt RP */.{. 
32166 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
32167 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
32168 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  1].minor.yy0,&yy
32169 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3216a 2c 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  ,0);.}.        b
3216b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3216c 20 33 33 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74   33: /* create_t
3216d 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53  able_args ::= AS
3216e 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73   select */.{.  s
3216f 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70  qlite3EndTable(p
32170 50 61 72 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b  Parse,0,0,yymsp[
32171 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20  0].minor.yy3);. 
32172 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
32173 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
32174 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32175 79 79 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy3);.}.        
32176 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32177 65 20 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20  e 36: /* column 
32178 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70  ::= columnid typ
32179 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a  e carglist */.{.
3217a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3217b 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  0.z = yymsp[-2].
3217c 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
3217d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
3217e 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d   = (int)(pParse-
3217f 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79  >sLastToken.z-yy
32180 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32181 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73  0.z) + pParse->s
32182 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20  LastToken.n;.}. 
32183 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32184 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a 20      case 37: /* 
32185 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20  columnid ::= nm 
32186 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 64  */.{.  sqlite3Ad
32187 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26  dColumn(pParse,&
32188 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32189 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  y0);.  yygotomin
3218a 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30  or.yy0 = yymsp[0
3218b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20  ].minor.yy0;.}. 
3218c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3218d 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a 20      case 38: /* 
3218e 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20  id ::= ID */.   
3218f 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a 20 69     case 39: /* i
32190 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 2a 2f  d ::= INDEXED */
32191 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32192 6c 65 6e 6f 3d 3d 33 39 29 3b 0a 20 20 20 20 20  leno==39);.     
32193 20 63 61 73 65 20 34 30 3a 20 2f 2a 20 69 64 73   case 40: /* ids
32194 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 20 2a   ::= ID|STRING *
32195 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32196 75 6c 65 6e 6f 3d 3d 34 30 29 3b 0a 20 20 20 20  uleno==40);.    
32197 20 20 63 61 73 65 20 34 31 3a 20 2f 2a 20 6e 6d    case 41: /* nm
32198 20 3a 3a 3d 20 69 64 20 2a 2f 20 79 79 74 65 73   ::= id */ yytes
32199 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3219a 34 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  41);.      case 
3219b 34 32 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54  42: /* nm ::= ST
3219c 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63 61  RING */ yytestca
3219d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 32 29  se(yyruleno==42)
3219e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 33 3a  ;.      case 43:
3219f 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f   /* nm ::= JOIN_
321a0 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  KW */ yytestcase
321a1 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 33 29 3b 0a  (yyruleno==43);.
321a2 20 20 20 20 20 20 63 61 73 65 20 34 36 3a 20 2f        case 46: /
321a3 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20  * typetoken ::= 
321a4 74 79 70 65 6e 61 6d 65 20 2a 2f 20 79 79 74 65  typename */ yyte
321a5 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
321a6 3d 34 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =46);.      case
321a7 20 34 39 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65   49: /* typename
321a8 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79 79 74 65   ::= ids */ yyte
321a9 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
321aa 3d 34 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =49);.      case
321ab 20 31 32 36 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20   126: /* as ::= 
321ac 41 53 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63  AS nm */ yytestc
321ad 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32  ase(yyruleno==12
321ae 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  6);.      case 1
321af 32 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 69 64  27: /* as ::= id
321b0 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  s */ yytestcase(
321b1 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 37 29 3b 0a  yyruleno==127);.
321b2 20 20 20 20 20 20 63 61 73 65 20 31 33 37 3a 20        case 137: 
321b3 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20  /* dbnm ::= DOT 
321b4 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
321b5 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 33 37 29 3b  (yyruleno==137);
321b6 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 36 3a  .      case 146:
321b7 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   /* indexed_opt 
321b8 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e  ::= INDEXED BY n
321b9 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  m */ yytestcase(
321ba 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 36 29 3b 0a  yyruleno==146);.
321bb 20 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 20        case 251: 
321bc 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43  /* collate ::= C
321bd 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 20 79 79  OLLATE ids */ yy
321be 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
321bf 6f 3d 3d 32 35 31 29 3b 0a 20 20 20 20 20 20 63  o==251);.      c
321c0 61 73 65 20 32 36 30 3a 20 2f 2a 20 6e 6d 6e 75  ase 260: /* nmnu
321c1 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a  m ::= plus_num *
321c2 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
321c3 75 6c 65 6e 6f 3d 3d 32 36 30 29 3b 0a 20 20 20  uleno==260);.   
321c4 20 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20     case 261: /* 
321c5 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20  nmnum ::= nm */ 
321c6 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
321c7 65 6e 6f 3d 3d 32 36 31 29 3b 0a 20 20 20 20 20  eno==261);.     
321c8 20 63 61 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d   case 262: /* nm
321c9 6e 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 20 79 79  num ::= ON */ yy
321ca 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
321cb 6f 3d 3d 32 36 32 29 3b 0a 20 20 20 20 20 20 63  o==262);.      c
321cc 61 73 65 20 32 36 33 3a 20 2f 2a 20 6e 6d 6e 75  ase 263: /* nmnu
321cd 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 20 2a 2f 20  m ::= DELETE */ 
321ce 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
321cf 65 6e 6f 3d 3d 32 36 33 29 3b 0a 20 20 20 20 20  eno==263);.     
321d0 20 63 61 73 65 20 32 36 34 3a 20 2f 2a 20 6e 6d   case 264: /* nm
321d1 6e 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  num ::= DEFAULT 
321d2 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
321d3 72 75 6c 65 6e 6f 3d 3d 32 36 34 29 3b 0a 20 20  ruleno==264);.  
321d4 20 20 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a      case 265: /*
321d5 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c   plus_num ::= pl
321d6 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72 20 2a 2f  us_opt number */
321d7 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
321d8 6c 65 6e 6f 3d 3d 32 36 35 29 3b 0a 20 20 20 20  leno==265);.    
321d9 20 20 63 61 73 65 20 32 36 36 3a 20 2f 2a 20 6d    case 266: /* m
321da 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e  inus_num ::= MIN
321db 55 53 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74  US number */ yyt
321dc 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
321dd 3d 3d 32 36 36 29 3b 0a 20 20 20 20 20 20 63 61  ==266);.      ca
321de 73 65 20 32 36 37 3a 20 2f 2a 20 6e 75 6d 62 65  se 267: /* numbe
321df 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  r ::= INTEGER|FL
321e0 4f 41 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73  OAT */ yytestcas
321e1 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 37 29  e(yyruleno==267)
321e2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 35  ;.      case 285
321e3 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d  : /* trnm ::= nm
321e4 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
321e5 79 72 75 6c 65 6e 6f 3d 3d 32 38 35 29 3b 0a 7b  yruleno==285);.{
321e6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20  yygotominor.yy0 
321e7 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
321e8 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  .yy0;}.        b
321e9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
321ea 20 34 35 3a 20 2f 2a 20 74 79 70 65 20 3a 3a 3d   45: /* type ::=
321eb 20 74 79 70 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73   typetoken */.{s
321ec 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
321ed 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ype(pParse,&yyms
321ee 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
321ef 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
321f0 0a 20 20 20 20 20 20 63 61 73 65 20 34 37 3a 20  .      case 47: 
321f1 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  /* typetoken ::=
321f2 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
321f3 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ned RP */.{.  yy
321f4 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
321f5 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  = yymsp[-3].mino
321f6 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
321f7 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28  ominor.yy0.n = (
321f8 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d  int)(&yymsp[0].m
321f9 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
321fa 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
321fb 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
321fc 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20  or.yy0.z);.}.   
321fd 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
321fe 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20 74 79    case 48: /* ty
321ff 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
32200 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43  name LP signed C
32201 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 20 2a  OMMA signed RP *
32202 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
32203 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b  r.yy0.z = yymsp[
32204 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -5].minor.yy0.z;
32205 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32206 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26 79 79  y0.n = (int)(&yy
32207 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32208 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
32209 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70  r.yy0.n] - yymsp
3220a 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-5].minor.yy0.z
3220b 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
3220c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
3220d 30 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a  0: /* typename :
3220e 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73 20  := typename ids 
3220f 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
32210 79 79 30 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e  yy0.z=yymsp[-1].
32211 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 20 79 79 67  minor.yy0.z; yyg
32212 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79  otominor.yy0.n=y
32213 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32214 30 2e 6e 2b 28 69 6e 74 29 28 79 79 6d 73 70 5b  0.n+(int)(yymsp[
32215 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79  0].minor.yy0.z-y
32216 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32217 79 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20 20 20  y0.z);}.        
32218 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32219 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 57: /* ccons :
3221a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 20  := DEFAULT term 
3221b 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 39  */.      case 59
3221c 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44  : /* ccons ::= D
3221d 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65 72 6d  EFAULT PLUS term
3221e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3221f 79 72 75 6c 65 6e 6f 3d 3d 35 39 29 3b 0a 7b 73  yruleno==59);.{s
32220 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
32221 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26 79 79  Value(pParse,&yy
32222 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32223 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  46);}.        br
32224 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32225 35 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  58: /* ccons ::=
32226 20 44 45 46 41 55 4c 54 20 4c 50 20 65 78 70 72   DEFAULT LP expr
32227 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41   RP */.{sqlite3A
32228 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
32229 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
3222a 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a  .minor.yy346);}.
3222b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3222c 20 20 20 20 20 63 61 73 65 20 36 30 3a 20 2f 2a       case 60: /*
3222d 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
3222e 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 20 2a 2f  LT MINUS term */
3222f 0a 7b 0a 20 20 45 78 70 72 53 70 61 6e 20 76 3b  .{.  ExprSpan v;
32230 0a 20 20 76 2e 70 45 78 70 72 20 3d 20 73 71 6c  .  v.pExpr = sql
32231 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32232 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d  , TK_UMINUS, yym
32233 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
32234 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a  6.pExpr, 0, 0);.
32235 20 20 76 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d    v.zStart = yym
32236 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
32237 2e 7a 3b 0a 20 20 76 2e 7a 45 6e 64 20 3d 20 79  .z;.  v.zEnd = y
32238 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32239 33 34 36 2e 7a 45 6e 64 3b 0a 20 20 73 71 6c 69  346.zEnd;.  sqli
3223a 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
3223b 75 65 28 70 50 61 72 73 65 2c 26 76 29 3b 0a 7d  ue(pParse,&v);.}
3223c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3223d 20 20 20 20 20 20 63 61 73 65 20 36 31 3a 20 2f        case 61: /
3223e 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41  * ccons ::= DEFA
3223f 55 4c 54 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78  ULT id */.{.  Ex
32240 70 72 53 70 61 6e 20 76 3b 0a 20 20 73 70 61 6e  prSpan v;.  span
32241 45 78 70 72 28 26 76 2c 20 70 50 61 72 73 65 2c  Expr(&v, pParse,
32242 20 54 4b 5f 53 54 52 49 4e 47 2c 20 26 79 79 6d   TK_STRING, &yym
32243 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32244 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44 65  ;.  sqlite3AddDe
32245 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73  faultValue(pPars
32246 65 2c 26 76 29 3b 0a 7d 0a 20 20 20 20 20 20 20  e,&v);.}.       
32247 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32248 73 65 20 36 33 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 63: /* ccons 
32249 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63  ::= NOT NULL onc
3224a 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  onf */.{sqlite3A
3224b 64 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65  ddNotNull(pParse
3224c 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3224d 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20  .yy328);}.      
3224e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3224f 61 73 65 20 36 34 3a 20 2f 2a 20 63 63 6f 6e 73  ase 64: /* ccons
32250 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59   ::= PRIMARY KEY
32251 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e   sortorder oncon
32252 66 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71  f autoinc */.{sq
32253 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b  lite3AddPrimaryK
32254 65 79 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73  ey(pParse,0,yyms
32255 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
32256 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8,yymsp[0].minor
32257 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d  .yy328,yymsp[-2]
32258 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a  .minor.yy328);}.
32259 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3225a 20 20 20 20 20 63 61 73 65 20 36 35 3a 20 2f 2a       case 65: /*
3225b 20 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55   ccons ::= UNIQU
3225c 45 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  E onconf */.{sql
3225d 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
3225e 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30 2c 79  pParse,0,0,0,0,y
3225f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32260 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20  328,0,0,0,0);}. 
32261 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32262 20 20 20 20 63 61 73 65 20 36 36 3a 20 2f 2a 20      case 66: /* 
32263 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20  ccons ::= CHECK 
32264 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73  LP expr RP */.{s
32265 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f  qlite3AddCheckCo
32266 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c  nstraint(pParse,
32267 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32268 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20  yy346.pExpr);}. 
32269 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3226a 20 20 20 20 63 61 73 65 20 36 37 3a 20 2f 2a 20      case 67: /* 
3226b 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45  ccons ::= REFERE
3226c 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f  NCES nm idxlist_
3226d 6f 70 74 20 72 65 66 61 72 67 73 20 2a 2f 0a 7b  opt refargs */.{
3226e 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
3226f 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 30  eignKey(pParse,0
32270 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
32271 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e  r.yy0,yymsp[-1].
32272 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70  minor.yy14,yymsp
32273 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29  [0].minor.yy328)
32274 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32275 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 38 3a  ;.      case 68:
32276 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65   /* ccons ::= de
32277 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f  fer_subclause */
32278 0a 7b 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f  .{sqlite3DeferFo
32279 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c  reignKey(pParse,
3227a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3227b 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20  y328);}.        
3227c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3227d 65 20 36 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 69: /* ccons :
3227e 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a  := COLLATE ids *
3227f 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  /.{sqlite3AddCol
32280 6c 61 74 65 54 79 70 65 28 70 50 61 72 73 65 2c  lateType(pParse,
32281 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32282 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
32283 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32284 65 20 37 32 3a 20 2f 2a 20 72 65 66 61 72 67 73  e 72: /* refargs
32285 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f   ::= */.{ yygoto
32286 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
32287 5f 4e 6f 6e 65 2a 30 78 30 31 30 31 3b 20 2f 2a  _None*0x0101; /*
32288 20 45 56 3a 20 52 2d 31 39 38 30 33 2d 34 35 38   EV: R-19803-458
32289 38 34 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62  84 */}.        b
3228a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3228b 20 37 33 3a 20 2f 2a 20 72 65 66 61 72 67 73 20   73: /* refargs 
3228c 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61  ::= refargs refa
3228d 72 67 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  rg */.{ yygotomi
3228e 6e 6f 72 2e 79 79 33 32 38 20 3d 20 28 79 79 6d  nor.yy328 = (yym
3228f 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
32290 32 38 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d  28 & ~yymsp[0].m
32291 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 29  inor.yy429.mask)
32292 20 7c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   | yymsp[0].mino
32293 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 3b 20 7d  r.yy429.value; }
32294 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32295 20 20 20 20 20 20 63 61 73 65 20 37 34 3a 20 2f        case 74: /
32296 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54  * refarg ::= MAT
32297 43 48 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74  CH nm */.{ yygot
32298 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c  ominor.yy429.val
32299 75 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f  ue = 0;     yygo
3229a 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61  tominor.yy429.ma
3229b 73 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d  sk = 0x000000; }
3229c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3229d 20 20 20 20 20 20 63 61 73 65 20 37 35 3a 20 2f        case 75: /
3229e 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20  * refarg ::= ON 
3229f 44 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a 2f  DELETE refact */
322a0 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
322a1 79 34 32 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d  y429.value = yym
322a2 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
322a3 38 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  8;     yygotomin
322a4 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20  or.yy429.mask = 
322a5 30 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20 20  0x0000ff; }.    
322a6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
322a7 20 63 61 73 65 20 37 36 3a 20 2f 2a 20 72 65 66   case 76: /* ref
322a8 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54  arg ::= ON UPDAT
322a9 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79  E refact */.{ yy
322aa 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e  gotominor.yy429.
322ab 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d  value = yymsp[0]
322ac 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3c 3c 38 3b  .minor.yy328<<8;
322ad 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
322ae 34 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66  429.mask = 0x00f
322af 66 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  f00; }.        b
322b0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
322b1 20 37 37 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   77: /* refact :
322b2 3a 3d 20 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b  := SET NULL */.{
322b3 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
322b4 32 38 20 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b  28 = OE_SetNull;
322b5 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36    /* EV: R-33326
322b6 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20  -45252 */}.     
322b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
322b8 63 61 73 65 20 37 38 3a 20 2f 2a 20 72 65 66 61  case 78: /* refa
322b9 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55  ct ::= SET DEFAU
322ba 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  LT */.{ yygotomi
322bb 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 53  nor.yy328 = OE_S
322bc 65 74 44 66 6c 74 3b 20 20 2f 2a 20 45 56 3a 20  etDflt;  /* EV: 
322bd 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f  R-33326-45252 */
322be 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
322bf 0a 20 20 20 20 20 20 63 61 73 65 20 37 39 3a 20  .      case 79: 
322c0 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 43 41  /* refact ::= CA
322c1 53 43 41 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74  SCADE */.{ yygot
322c2 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
322c3 45 5f 43 61 73 63 61 64 65 3b 20 20 2f 2a 20 45  E_Cascade;  /* E
322c4 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32  V: R-33326-45252
322c5 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65   */}.        bre
322c6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
322c7 30 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d  0: /* refact ::=
322c8 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79   RESTRICT */.{ y
322c9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
322ca 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b 20   = OE_Restrict; 
322cb 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34  /* EV: R-33326-4
322cc 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20  5252 */}.       
322cd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
322ce 73 65 20 38 31 3a 20 2f 2a 20 72 65 66 61 63 74  se 81: /* refact
322cf 20 3a 3a 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a   ::= NO ACTION *
322d0 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
322d1 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  yy328 = OE_None;
322d2 20 20 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33       /* EV: R-33
322d3 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20  326-45252 */}.  
322d4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322d5 20 20 20 63 61 73 65 20 38 33 3a 20 2f 2a 20 64     case 83: /* d
322d6 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
322d7 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  := DEFERRABLE in
322d8 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
322d9 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  _opt */.      ca
322da 73 65 20 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f  se 98: /* defer_
322db 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
322dc 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  = defer_subclaus
322dd 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  e */ yytestcase(
322de 79 79 72 75 6c 65 6e 6f 3d 3d 39 38 29 3b 0a 20  yyruleno==98);. 
322df 20 20 20 20 20 63 61 73 65 20 31 30 30 3a 20 2f       case 100: /
322e0 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20  * onconf ::= ON 
322e1 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65  CONFLICT resolve
322e2 74 79 70 65 20 2a 2f 20 79 79 74 65 73 74 63 61  type */ yytestca
322e3 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 30  se(yyruleno==100
322e4 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  );.      case 10
322e5 33 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70  3: /* resolvetyp
322e6 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 20  e ::= raisetype 
322e7 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
322e8 72 75 6c 65 6e 6f 3d 3d 31 30 33 29 3b 0a 7b 79  ruleno==103);.{y
322e9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
322ea 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
322eb 72 2e 79 79 33 32 38 3b 7d 0a 20 20 20 20 20 20  r.yy328;}.      
322ec 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
322ed 61 73 65 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c  ase 87: /* consl
322ee 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b  ist_opt ::= */.{
322ef 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
322f0 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  n = 0; yygotomin
322f1 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20  or.yy0.z = 0;}. 
322f2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
322f3 20 20 20 20 63 61 73 65 20 38 38 3a 20 2f 2a 20      case 88: /* 
322f4 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  conslist_opt ::=
322f5 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20   COMMA conslist 
322f6 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
322f7 79 79 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  yy0 = yymsp[-1].
322f8 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
322f9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
322fa 20 63 61 73 65 20 39 33 3a 20 2f 2a 20 74 63 6f   case 93: /* tco
322fb 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
322fc 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61 75  EY LP idxlist au
322fd 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 20  toinc RP onconf 
322fe 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72  */.{sqlite3AddPr
322ff 69 6d 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c  imaryKey(pParse,
32300 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
32301 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy14,yymsp[0].mi
32302 6e 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b  nor.yy328,yymsp[
32303 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  -2].minor.yy328,
32304 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
32305 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
32306 34 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  4: /* tcons ::= 
32307 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73  UNIQUE LP idxlis
32308 74 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b  t RP onconf */.{
32309 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
3230a 65 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c  ex(pParse,0,0,0,
3230b 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3230c 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy14,yymsp[0].mi
3230d 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30 2c 30 2c  nor.yy328,0,0,0,
3230e 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
3230f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
32310 35 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  5: /* tcons ::= 
32311 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50  CHECK LP expr RP
32312 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69   onconf */.{sqli
32313 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
32314 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d  raint(pParse,yym
32315 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
32316 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20  46.pExpr);}.    
32317 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32318 20 63 61 73 65 20 39 36 3a 20 2f 2a 20 74 63 6f   case 96: /* tco
32319 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b  ns ::= FOREIGN K
3231a 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  EY LP idxlist RP
3231b 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
3231c 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
3231d 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  gs defer_subclau
3231e 73 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20  se_opt */.{.    
3231f 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
32320 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 20  eignKey(pParse, 
32321 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
32322 79 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  yy14, &yymsp[-3]
32323 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73  .minor.yy0, yyms
32324 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
32325 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
32326 72 2e 79 79 33 32 38 29 3b 0a 20 20 20 20 73 71  r.yy328);.    sq
32327 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
32328 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
32329 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
3232a 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  8);.}.        br
3232b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3232c 39 39 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a  99: /* onconf ::
3232d 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
3232e 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 44 65 66  r.yy328 = OE_Def
3232f 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62  ault;}.        b
32330 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32331 20 31 30 31 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20   101: /* orconf 
32332 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
32333 6e 6f 72 2e 79 79 31 38 36 20 3d 20 4f 45 5f 44  nor.yy186 = OE_D
32334 65 66 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20  efault;}.       
32335 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32336 73 65 20 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e  se 102: /* orcon
32337 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65  f ::= OR resolve
32338 74 79 70 65 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  type */.{yygotom
32339 69 6e 6f 72 2e 79 79 31 38 36 20 3d 20 28 75 38  inor.yy186 = (u8
3233a 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  )yymsp[0].minor.
3233b 79 79 33 32 38 3b 7d 0a 20 20 20 20 20 20 20 20  yy328;}.        
3233c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3233d 65 20 31 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76  e 104: /* resolv
3233e 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45  etype ::= IGNORE
3233f 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32340 2e 79 79 33 32 38 20 3d 20 4f 45 5f 49 67 6e 6f  .yy328 = OE_Igno
32341 72 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  re;}.        bre
32342 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32343 30 35 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79  05: /* resolvety
32344 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a  pe ::= REPLACE *
32345 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
32346 79 33 32 38 20 3d 20 4f 45 5f 52 65 70 6c 61 63  y328 = OE_Replac
32347 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
32348 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
32349 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52  6: /* cmd ::= DR
3234a 4f 50 20 54 41 42 4c 45 20 69 66 65 78 69 73 74  OP TABLE ifexist
3234b 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  s fullname */.{.
3234c 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62    sqlite3DropTab
3234d 6c 65 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  le(pParse, yymsp
3234e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  [0].minor.yy65, 
3234f 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
32350 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20  or.yy328);.}.   
32351 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32352 20 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20 63    case 109: /* c
32353 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
32354 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65  temp VIEW ifnote
32355 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53  xists nm dbnm AS
32356 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73   select */.{.  s
32357 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77  qlite3CreateView
32358 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
32359 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -7].minor.yy0, &
3235a 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3235b 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  yy0, &yymsp[-2].
3235c 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70  minor.yy0, yymsp
3235d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79  [0].minor.yy3, y
3235e 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
3235f 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y328, yymsp[-4].
32360 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a  minor.yy328);.}.
32361 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32362 20 20 20 20 20 63 61 73 65 20 31 31 30 3a 20 2f       case 110: /
32363 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56  * cmd ::= DROP V
32364 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c  IEW ifexists ful
32365 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
32366 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50  ite3DropTable(pP
32367 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
32368 69 6e 6f 72 2e 79 79 36 35 2c 20 31 2c 20 79 79  inor.yy65, 1, yy
32369 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3236a 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  328);.}.        
3236b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3236c 65 20 31 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 111: /* cmd ::
3236d 3d 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20  = select */.{.  
3236e 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 20  SelectDest dest 
3236f 3d 20 7b 53 52 54 5f 4f 75 74 70 75 74 2c 20 30  = {SRT_Output, 0
32370 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71  , 0, 0, 0};.  sq
32371 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
32372 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
32373 6f 72 2e 79 79 33 2c 20 26 64 65 73 74 29 3b 0a  or.yy3, &dest);.
32374 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
32375 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
32376 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
32377 2e 79 79 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy3);.}.       
32378 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32379 73 65 20 31 31 32 3a 20 2f 2a 20 73 65 6c 65 63  se 112: /* selec
3237a 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20  t ::= oneselect 
3237b 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3237c 79 79 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  yy3 = yymsp[0].m
3237d 69 6e 6f 72 2e 79 79 33 3b 7d 0a 20 20 20 20 20  inor.yy3;}.     
3237e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3237f 63 61 73 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c  case 113: /* sel
32380 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d  ect ::= select m
32381 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e  ultiselect_op on
32382 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69  eselect */.{.  i
32383 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  f( yymsp[0].mino
32384 72 2e 79 79 33 20 29 7b 0a 20 20 20 20 79 79 6d  r.yy3 ){.    yym
32385 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2d  sp[0].minor.yy3-
32386 3e 6f 70 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  >op = (u8)yymsp[
32387 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b  -1].minor.yy328;
32388 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  .    yymsp[0].mi
32389 6e 6f 72 2e 79 79 33 2d 3e 70 50 72 69 6f 72 20  nor.yy3->pPrior 
3238a 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
3238b 72 2e 79 79 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a  r.yy3;.  }else{.
3238c 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
3238d 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3238e 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  db, yymsp[-2].mi
3238f 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 7d 0a 20 20  nor.yy3);.  }.  
32390 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20  yygotominor.yy3 
32391 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
32392 2e 79 79 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20  .yy3;.}.        
32393 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32394 65 20 31 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73  e 115: /* multis
32395 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49  elect_op ::= UNI
32396 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f 74  ON ALL */.{yygot
32397 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54  ominor.yy328 = T
32398 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20 20 20 20  K_ALL;}.        
32399 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3239a 65 20 31 31 37 3a 20 2f 2a 20 6f 6e 65 73 65 6c  e 117: /* onesel
3239b 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64  ect ::= SELECT d
3239c 69 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69  istinct selcolli
3239d 73 74 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70  st from where_op
3239e 74 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 68 61  t groupby_opt ha
3239f 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62 79  ving_opt orderby
323a0 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74 20 2a  _opt limit_opt *
323a1 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
323a2 72 2e 79 79 33 20 3d 20 73 71 6c 69 74 65 33 53  r.yy3 = sqlite3S
323a3 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c  electNew(pParse,
323a4 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
323a5 79 79 31 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d  yy14,yymsp[-5].m
323a6 69 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b  inor.yy65,yymsp[
323a7 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  -4].minor.yy132,
323a8 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
323a9 79 79 31 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy14,yymsp[-2].m
323aa 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70  inor.yy132,yymsp
323ab 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-1].minor.yy14,
323ac 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
323ad 79 79 33 32 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d  yy328,yymsp[0].m
323ae 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69  inor.yy476.pLimi
323af 74 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  t,yymsp[0].minor
323b0 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 29 3b  .yy476.pOffset);
323b1 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
323b2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 31  ;.      case 121
323b3 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 73 65  : /* sclp ::= se
323b4 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 2a  lcollist COMMA *
323b5 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 37  /.      case 247
323b6 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74  : /* idxlist_opt
323b7 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20   ::= LP idxlist 
323b8 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  RP */ yytestcase
323b9 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 37 29 3b  (yyruleno==247);
323ba 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
323bb 31 34 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  14 = yymsp[-1].m
323bc 69 6e 6f 72 2e 79 79 31 34 3b 7d 0a 20 20 20 20  inor.yy14;}.    
323bd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
323be 20 63 61 73 65 20 31 32 32 3a 20 2f 2a 20 73 63   case 122: /* sc
323bf 6c 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  lp ::= */.      
323c0 63 61 73 65 20 31 35 30 3a 20 2f 2a 20 6f 72 64  case 150: /* ord
323c1 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  erby_opt ::= */ 
323c2 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
323c3 65 6e 6f 3d 3d 31 35 30 29 3b 0a 20 20 20 20 20  eno==150);.     
323c4 20 63 61 73 65 20 31 35 38 3a 20 2f 2a 20 67 72   case 158: /* gr
323c5 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  oupby_opt ::= */
323c6 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
323c7 6c 65 6e 6f 3d 3d 31 35 38 29 3b 0a 20 20 20 20  leno==158);.    
323c8 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20 65    case 240: /* e
323c9 78 70 72 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79  xprlist ::= */ y
323ca 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
323cb 6e 6f 3d 3d 32 34 30 29 3b 0a 20 20 20 20 20 20  no==240);.      
323cc 63 61 73 65 20 32 34 36 3a 20 2f 2a 20 69 64 78  case 246: /* idx
323cd 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  list_opt ::= */ 
323ce 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
323cf 65 6e 6f 3d 3d 32 34 36 29 3b 0a 7b 79 79 67 6f  eno==246);.{yygo
323d0 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 30  tominor.yy14 = 0
323d1 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
323d2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 33  ;.      case 123
323d3 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20  : /* selcollist 
323d4 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73  ::= sclp expr as
323d5 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d   */.{.   yygotom
323d6 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
323d7 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
323d8 64 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  d(pParse, yymsp[
323d9 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -2].minor.yy14, 
323da 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
323db 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
323dc 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69   if( yymsp[0].mi
323dd 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 20 73 71  nor.yy0.n>0 ) sq
323de 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
323df 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
323e0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
323e1 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
323e2 79 30 2c 20 31 29 3b 0a 20 20 20 73 71 6c 69 74  y0, 1);.   sqlit
323e3 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61  e3ExprListSetSpa
323e4 6e 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  n(pParse,yygotom
323e5 69 6e 6f 72 2e 79 79 31 34 2c 26 79 79 6d 73 70  inor.yy14,&yymsp
323e6 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-1].minor.yy346
323e7 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
323e8 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
323e9 32 34 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73  24: /* selcollis
323ea 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 20  t ::= sclp STAR 
323eb 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  */.{.  Expr *p =
323ec 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61   sqlite3Expr(pPa
323ed 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c  rse->db, TK_ALL,
323ee 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e   0);.  yygotomin
323ef 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
323f0 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
323f1 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31  pParse, yymsp[-1
323f2 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29  ].minor.yy14, p)
323f3 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
323f4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  k;.      case 12
323f5 35 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  5: /* selcollist
323f6 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54   ::= sclp nm DOT
323f7 20 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70   STAR */.{.  Exp
323f8 72 20 2a 70 52 69 67 68 74 20 3d 20 73 71 6c 69  r *pRight = sqli
323f9 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
323fa 20 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26   TK_ALL, 0, 0, &
323fb 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
323fc 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65  y0);.  Expr *pLe
323fd 66 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ft = sqlite3PExp
323fe 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
323ff 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32   0, 0, &yymsp[-2
32400 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
32401 45 78 70 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c  Expr *pDot = sql
32402 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32403 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c  , TK_DOT, pLeft,
32404 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 79   pRight, 0);.  y
32405 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
32406 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
32407 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
32408 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32409 79 31 34 2c 20 70 44 6f 74 29 3b 0a 7d 0a 20 20  y14, pDot);.}.  
3240a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3240b 20 20 20 63 61 73 65 20 31 32 38 3a 20 2f 2a 20     case 128: /* 
3240c 61 73 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  as ::= */.{yygot
3240d 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30  ominor.yy0.n = 0
3240e 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3240f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 39  ;.      case 129
32410 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f  : /* from ::= */
32411 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32412 36 35 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  65 = sqlite3DbMa
32413 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d  llocZero(pParse-
32414 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67  >db, sizeof(*yyg
32415 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 29 29 3b  otominor.yy65));
32416 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32417 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 30 3a  .      case 130:
32418 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f   /* from ::= FRO
32419 4d 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a  M seltablist */.
3241a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
3241b 79 79 36 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  yy65 = yymsp[0].
3241c 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 73 71  minor.yy65;.  sq
3241d 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
3241e 74 4a 6f 69 6e 54 79 70 65 28 79 79 67 6f 74 6f  tJoinType(yygoto
3241f 6d 69 6e 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20  minor.yy65);.}. 
32420 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32421 20 20 20 20 63 61 73 65 20 31 33 31 3a 20 2f 2a      case 131: /*
32422 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20   stl_prefix ::= 
32423 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f  seltablist joino
32424 70 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f  p */.{.   yygoto
32425 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 79 79 6d  minor.yy65 = yym
32426 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-1].minor.yy6
32427 35 3b 0a 20 20 20 69 66 28 20 41 4c 57 41 59 53  5;.   if( ALWAYS
32428 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36  (yygotominor.yy6
32429 35 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  5 && yygotominor
3242a 2e 79 79 36 35 2d 3e 6e 53 72 63 3e 30 29 20 29  .yy65->nSrc>0) )
3242b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36   yygotominor.yy6
3242c 35 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  5->a[yygotominor
3242d 2e 79 79 36 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a  .yy65->nSrc-1].j
3242e 6f 69 6e 74 79 70 65 20 3d 20 28 75 38 29 79 79  ointype = (u8)yy
3242f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32430 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  28;.}.        br
32431 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32432 31 33 32 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  132: /* stl_pref
32433 69 78 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ix ::= */.{yygot
32434 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 30 3b  ominor.yy65 = 0;
32435 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32436 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 33 3a  .      case 133:
32437 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a   /* seltablist :
32438 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d  := stl_prefix nm
32439 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64   dbnm as indexed
3243a 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e  _opt on_opt usin
3243b 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67  g_opt */.{.  yyg
3243c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20  otominor.yy65 = 
3243d 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
3243e 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61  pendFromTerm(pPa
3243f 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  rse,yymsp[-6].mi
32440 6e 6f 72 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b  nor.yy65,&yymsp[
32441 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -5].minor.yy0,&y
32442 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
32443 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  y0,&yymsp[-3].mi
32444 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b  nor.yy0,0,yymsp[
32445 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  -1].minor.yy132,
32446 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32447 79 34 30 38 29 3b 0a 20 20 73 71 6c 69 74 65 33  y408);.  sqlite3
32448 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
32449 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
3244a 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73  inor.yy65, &yyms
3244b 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
3244c 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3244d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3244e 34 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74  4: /* seltablist
3244f 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
32450 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20  LP select RP as 
32451 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
32452 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
32453 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c  minor.yy65 = sql
32454 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
32455 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65  dFromTerm(pParse
32456 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  ,yymsp[-6].minor
32457 2e 79 79 36 35 2c 30 2c 30 2c 26 79 79 6d 73 70  .yy65,0,0,&yymsp
32458 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79  [-2].minor.yy0,y
32459 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3245a 79 33 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  y3,yymsp[-1].min
3245b 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 30  or.yy132,yymsp[0
3245c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a  ].minor.yy408);.
3245d 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
3245e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3245f 35 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74  5: /* seltablist
32460 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
32461 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 20 52 50  LP seltablist RP
32462 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67   as on_opt using
32463 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 69 66  _opt */.{.    if
32464 28 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f  ( yymsp[-6].mino
32465 72 2e 79 79 36 35 3d 3d 30 20 26 26 20 79 79 6d  r.yy65==0 && yym
32466 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32467 2e 6e 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d  .n==0 && yymsp[-
32468 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 3d 3d  1].minor.yy132==
32469 30 20 26 26 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  0 && yymsp[0].mi
3246a 6e 6f 72 2e 79 79 34 30 38 3d 3d 30 20 29 7b 0a  nor.yy408==0 ){.
3246b 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
3246c 72 2e 79 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d  r.yy65 = yymsp[-
3246d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20  4].minor.yy65;. 
3246e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3246f 53 65 6c 65 63 74 20 2a 70 53 75 62 71 75 65 72  Select *pSubquer
32470 79 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  y;.      sqlite3
32471 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
32472 54 79 70 65 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d  Type(yymsp[-4].m
32473 69 6e 6f 72 2e 79 79 36 35 29 3b 0a 20 20 20 20  inor.yy65);.    
32474 20 20 70 53 75 62 71 75 65 72 79 20 3d 20 73 71    pSubquery = sq
32475 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
32476 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 34  Parse,0,yymsp[-4
32477 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30  ].minor.yy65,0,0
32478 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20  ,0,0,0,0,0);.   
32479 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3247a 79 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63  y65 = sqlite3Src
3247b 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65  ListAppendFromTe
3247c 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  rm(pParse,yymsp[
3247d 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30  -6].minor.yy65,0
3247e 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ,0,&yymsp[-2].mi
3247f 6e 6f 72 2e 79 79 30 2c 70 53 75 62 71 75 65 72  nor.yy0,pSubquer
32480 79 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  y,yymsp[-1].mino
32481 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d  r.yy132,yymsp[0]
32482 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20  .minor.yy408);. 
32483 20 20 20 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20     }.  }.       
32484 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32485 73 65 20 31 33 36 3a 20 2f 2a 20 64 62 6e 6d 20  se 136: /* dbnm 
32486 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
32487 65 20 31 34 35 3a 20 2f 2a 20 69 6e 64 65 78 65  e 145: /* indexe
32488 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  d_opt ::= */ yyt
32489 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3248a 3d 3d 31 34 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==145);.{yygotom
3248b 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79  inor.yy0.z=0; yy
3248c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  gotominor.yy0.n=
3248d 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
3248e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3248f 38 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a  8: /* fullname :
32490 3a 3d 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79  := nm dbnm */.{y
32491 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
32492 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
32493 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
32494 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  b,0,&yymsp[-1].m
32495 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
32496 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
32497 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32498 20 20 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f       case 139: /
32499 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d  * joinop ::= COM
3249a 4d 41 7c 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67  MA|JOIN */.{ yyg
3249b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d  otominor.yy328 =
3249c 20 4a 54 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20   JT_INNER; }.   
3249d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3249e 20 20 63 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a    case 140: /* j
3249f 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
324a0 57 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f  W JOIN */.{ yygo
324a1 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
324a2 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
324a3 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31  pParse,&yymsp[-1
324a4 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29  ].minor.yy0,0,0)
324a5 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
324a6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
324a7 31 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d  1: /* joinop ::=
324a8 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e   JOIN_KW nm JOIN
324a9 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
324aa 72 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65  r.yy328 = sqlite
324ab 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
324ac 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
324ad 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0,&yymsp[-1]
324ae 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d  .minor.yy0,0); }
324af 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
324b0 20 20 20 20 20 20 63 61 73 65 20 31 34 32 3a 20        case 142: 
324b1 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
324b2 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e  IN_KW nm nm JOIN
324b3 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
324b4 72 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74 65  r.yy328 = sqlite
324b5 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73 65  3JoinType(pParse
324b6 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
324b7 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  r.yy0,&yymsp[-2]
324b8 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
324b9 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
324ba 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
324bb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
324bc 33 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d  3: /* on_opt ::=
324bd 20 4f 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20   ON expr */.    
324be 20 20 63 61 73 65 20 31 35 34 3a 20 2f 2a 20 73    case 154: /* s
324bf 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72  ortitem ::= expr
324c0 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
324c1 79 72 75 6c 65 6e 6f 3d 3d 31 35 34 29 3b 0a 20  yruleno==154);. 
324c2 20 20 20 20 20 63 61 73 65 20 31 36 31 3a 20 2f       case 161: /
324c3 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d  * having_opt ::=
324c4 20 48 41 56 49 4e 47 20 65 78 70 72 20 2a 2f 20   HAVING expr */ 
324c5 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
324c6 65 6e 6f 3d 3d 31 36 31 29 3b 0a 20 20 20 20 20  eno==161);.     
324c7 20 63 61 73 65 20 31 36 38 3a 20 2f 2a 20 77 68   case 168: /* wh
324c8 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52  ere_opt ::= WHER
324c9 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  E expr */ yytest
324ca 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
324cb 36 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  68);.      case 
324cc 32 33 35 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73  235: /* case_els
324cd 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 20  e ::= ELSE expr 
324ce 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
324cf 72 75 6c 65 6e 6f 3d 3d 32 33 35 29 3b 0a 20 20  ruleno==235);.  
324d0 20 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a      case 237: /*
324d1 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a   case_operand ::
324d2 3d 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  = expr */ yytest
324d3 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
324d4 33 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  37);.{yygotomino
324d5 72 2e 79 79 31 33 32 20 3d 20 79 79 6d 73 70 5b  r.yy132 = yymsp[
324d6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
324d7 45 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20 62  Expr;}.        b
324d8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
324d9 20 31 34 34 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20   144: /* on_opt 
324da 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
324db 65 20 31 36 30 3a 20 2f 2a 20 68 61 76 69 6e 67  e 160: /* having
324dc 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  _opt ::= */ yyte
324dd 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
324de 3d 31 36 30 29 3b 0a 20 20 20 20 20 20 63 61 73  =160);.      cas
324df 65 20 31 36 37 3a 20 2f 2a 20 77 68 65 72 65 5f  e 167: /* where_
324e0 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
324e1 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
324e2 31 36 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65  167);.      case
324e3 20 32 33 36 3a 20 2f 2a 20 63 61 73 65 5f 65 6c   236: /* case_el
324e4 73 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  se ::= */ yytest
324e5 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
324e6 33 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  36);.      case 
324e7 32 33 38 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65  238: /* case_ope
324e8 72 61 6e 64 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  rand ::= */ yyte
324e9 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
324ea 3d 32 33 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =238);.{yygotomi
324eb 6e 6f 72 2e 79 79 31 33 32 20 3d 20 30 3b 7d 0a  nor.yy132 = 0;}.
324ec 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
324ed 20 20 20 20 20 63 61 73 65 20 31 34 37 3a 20 2f       case 147: /
324ee 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a  * indexed_opt ::
324ef 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f  = NOT INDEXED */
324f0 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
324f1 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  0.z=0; yygotomin
324f2 6f 72 2e 79 79 30 2e 6e 3d 31 3b 7d 0a 20 20 20  or.yy0.n=1;}.   
324f3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
324f4 20 20 63 61 73 65 20 31 34 38 3a 20 2f 2a 20 75    case 148: /* u
324f5 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49  sing_opt ::= USI
324f6 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74  NG LP inscollist
324f7 20 52 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   RP */.      cas
324f8 65 20 31 38 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 180: /* inscol
324f9 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
324fa 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f  inscollist RP */
324fb 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
324fc 6c 65 6e 6f 3d 3d 31 38 30 29 3b 0a 7b 79 79 67  leno==180);.{yyg
324fd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d  otominor.yy408 =
324fe 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
324ff 2e 79 79 34 30 38 3b 7d 0a 20 20 20 20 20 20 20  .yy408;}.       
32500 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32501 73 65 20 31 34 39 3a 20 2f 2a 20 75 73 69 6e 67  se 149: /* using
32502 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
32503 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69    case 179: /* i
32504 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a  nscollist_opt ::
32505 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32506 79 79 72 75 6c 65 6e 6f 3d 3d 31 37 39 29 3b 0a  yyruleno==179);.
32507 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
32508 30 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  08 = 0;}.       
32509 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3250a 73 65 20 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72  se 151: /* order
3250b 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52  by_opt ::= ORDER
3250c 20 42 59 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a   BY sortlist */.
3250d 20 20 20 20 20 20 63 61 73 65 20 31 35 39 3a 20        case 159: 
3250e 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a  /* groupby_opt :
3250f 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70  := GROUP BY nexp
32510 72 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74 63  rlist */ yytestc
32511 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
32512 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  9);.      case 2
32513 33 39 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20  39: /* exprlist 
32514 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f  ::= nexprlist */
32515 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32516 6c 65 6e 6f 3d 3d 32 33 39 29 3b 0a 7b 79 79 67  leno==239);.{yyg
32517 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
32518 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32519 79 31 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y14;}.        br
3251a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3251b 31 35 32 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  152: /* sortlist
3251c 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f   ::= sortlist CO
3251d 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72  MMA sortitem sor
3251e 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79  torder */.{.  yy
3251f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
32520 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
32521 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
32522 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32523 31 34 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  14,yymsp[-1].min
32524 6f 72 2e 79 79 31 33 32 29 3b 0a 20 20 69 66 28  or.yy132);.  if(
32525 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32526 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  4 ) yygotominor.
32527 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  yy14->a[yygotomi
32528 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d  nor.yy14->nExpr-
32529 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
3252a 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
3252b 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20  r.yy328;.}.     
3252c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3252d 63 61 73 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72  case 153: /* sor
3252e 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74  tlist ::= sortit
3252f 65 6d 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a  em sortorder */.
32530 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
32531 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
32532 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
32533 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e  rse,0,yymsp[-1].
32534 6d 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 20 20  minor.yy132);.  
32535 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
32536 79 79 31 34 20 26 26 20 41 4c 57 41 59 53 28 79  yy14 && ALWAYS(y
32537 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d  ygotominor.yy14-
32538 3e 61 29 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  >a) ) yygotomino
32539 72 2e 79 79 31 34 2d 3e 61 5b 30 5d 2e 73 6f 72  r.yy14->a[0].sor
3253a 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d  tOrder = (u8)yym
3253b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
3253c 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8;.}.        bre
3253d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3253e 35 35 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  55: /* sortorder
3253f 20 3a 3a 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20   ::= ASC */.    
32540 20 20 63 61 73 65 20 31 35 37 3a 20 2f 2a 20 73    case 157: /* s
32541 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 20  ortorder ::= */ 
32542 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32543 65 6e 6f 3d 3d 31 35 37 29 3b 0a 7b 79 79 67 6f  eno==157);.{yygo
32544 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
32545 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 3b 7d 0a  SQLITE_SO_ASC;}.
32546 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32547 20 20 20 20 20 63 61 73 65 20 31 35 36 3a 20 2f       case 156: /
32548 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20  * sortorder ::= 
32549 44 45 53 43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  DESC */.{yygotom
3254a 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 53 51 4c  inor.yy328 = SQL
3254b 49 54 45 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20 20  ITE_SO_DESC;}.  
3254c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3254d 20 20 20 63 61 73 65 20 31 36 32 3a 20 2f 2a 20     case 162: /* 
3254e 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  limit_opt ::= */
3254f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32550 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20 30 3b 20  476.pLimit = 0; 
32551 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
32552 36 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a  6.pOffset = 0;}.
32553 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32554 20 20 20 20 20 63 61 73 65 20 31 36 33 3a 20 2f       case 163: /
32555 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20  * limit_opt ::= 
32556 4c 49 4d 49 54 20 65 78 70 72 20 2a 2f 0a 7b 79  LIMIT expr */.{y
32557 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
32558 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b  .pLimit = yymsp[
32559 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
3255a 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  Expr; yygotomino
3255b 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20  r.yy476.pOffset 
3255c 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
3255d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3255e 31 36 34 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70  164: /* limit_op
3255f 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
32560 20 4f 46 46 53 45 54 20 65 78 70 72 20 2a 2f 0a   OFFSET expr */.
32561 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
32562 37 36 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73  76.pLimit = yyms
32563 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
32564 36 2e 70 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d  6.pExpr; yygotom
32565 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73  inor.yy476.pOffs
32566 65 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  et = yymsp[0].mi
32567 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
32568 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32569 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 35 3a  .      case 165:
3256a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a   /* limit_opt ::
3256b 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d  = LIMIT expr COM
3256c 4d 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  MA expr */.{yygo
3256d 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f  tominor.yy476.pO
3256e 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32  ffset = yymsp[-2
3256f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32570 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  xpr; yygotominor
32571 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20  .yy476.pLimit = 
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 2e 70 45 78 70 72 3b 7d 0a 20 20 20  y346.pExpr;}.   
32574 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32575 20 20 63 61 73 65 20 31 36 36 3a 20 2f 2a 20 63    case 166: /* c
32576 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
32577 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  OM fullname inde
32578 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70  xed_opt where_op
32579 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
3257a 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
3257b 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3257c 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26  2].minor.yy65, &
3257d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3257e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  yy0);.  sqlite3D
3257f 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
32580 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
32581 2e 79 79 36 35 2c 79 79 6d 73 70 5b 30 5d 2e 6d  .yy65,yymsp[0].m
32582 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20  inor.yy132);.}. 
32583 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32584 20 20 20 20 63 61 73 65 20 31 36 39 3a 20 2f 2a      case 169: /*
32585 20 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20   cmd ::= UPDATE 
32586 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20  orconf fullname 
32587 69 6e 64 65 78 65 64 5f 6f 70 74 20 53 45 54 20  indexed_opt SET 
32588 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
32589 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
3258a 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
3258b 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3258c 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26  4].minor.yy65, &
3258d 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3258e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  yy0);.  sqlite3E
3258f 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
32590 74 68 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  th(pParse,yymsp[
32591 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 22  -1].minor.yy14,"
32592 73 65 74 20 6c 69 73 74 22 29 3b 20 0a 20 20 73  set list"); .  s
32593 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61  qlite3Update(pPa
32594 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
32595 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d  nor.yy65,yymsp[-
32596 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  1].minor.yy14,yy
32597 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
32598 33 32 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  32,yymsp[-5].min
32599 6f 72 2e 79 79 31 38 36 29 3b 0a 7d 0a 20 20 20  or.yy186);.}.   
3259a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3259b 20 20 63 61 73 65 20 31 37 30 3a 20 2f 2a 20 73    case 170: /* s
3259c 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69  etlist ::= setli
3259d 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65  st COMMA nm EQ e
3259e 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  xpr */.{.  yygot
3259f 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
325a0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
325a1 65 6e 64 28 70 50 61 72 73 65 2c 20 79 79 6d 73  end(pParse, yyms
325a2 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-4].minor.yy14
325a3 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
325a4 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
325a5 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
325a6 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  SetName(pParse, 
325a7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
325a8 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
325a9 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20  or.yy0, 1);.}.  
325aa 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
325ab 20 20 20 63 61 73 65 20 31 37 31 3a 20 2f 2a 20     case 171: /* 
325ac 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45  setlist ::= nm E
325ad 51 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79  Q expr */.{.  yy
325ae 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
325af 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
325b0 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30  Append(pParse, 0
325b1 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
325b2 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
325b3 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
325b4 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  SetName(pParse, 
325b5 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
325b6 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
325b7 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20  or.yy0, 1);.}.  
325b8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
325b9 20 20 20 63 61 73 65 20 31 37 32 3a 20 2f 2a 20     case 172: /* 
325ba 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
325bb 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
325bc 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
325bd 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69  VALUES LP itemli
325be 73 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65  st RP */.{sqlite
325bf 33 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20  3Insert(pParse, 
325c0 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
325c1 79 79 36 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  yy65, yymsp[-1].
325c2 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79  minor.yy14, 0, y
325c3 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
325c4 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  y408, yymsp[-7].
325c5 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20  minor.yy186);}. 
325c6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
325c7 20 20 20 20 63 61 73 65 20 31 37 33 3a 20 2f 2a      case 173: /*
325c8 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f   cmd ::= insert_
325c9 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d  cmd INTO fullnam
325ca 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  e inscollist_opt
325cb 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c 69   select */.{sqli
325cc 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73 65  te3Insert(pParse
325cd 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
325ce 72 2e 79 79 36 35 2c 20 30 2c 20 79 79 6d 73 70  r.yy65, 0, yymsp
325cf 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79  [0].minor.yy3, y
325d0 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
325d1 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y408, yymsp[-4].
325d2 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20  minor.yy186);}. 
325d3 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
325d4 20 20 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a      case 174: /*
325d5 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f   cmd ::= insert_
325d6 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d  cmd INTO fullnam
325d7 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  e inscollist_opt
325d8 20 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 20   DEFAULT VALUES 
325d9 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72  */.{sqlite3Inser
325da 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  t(pParse, yymsp[
325db 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  -3].minor.yy65, 
325dc 30 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  0, 0, yymsp[-2].
325dd 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d  minor.yy408, yym
325de 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-5].minor.yy1
325df 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  86);}.        br
325e0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
325e1 31 37 35 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63  175: /* insert_c
325e2 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72  md ::= INSERT or
325e3 63 6f 6e 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  conf */.{yygotom
325e4 69 6e 6f 72 2e 79 79 31 38 36 20 3d 20 79 79 6d  inor.yy186 = yym
325e5 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  sp[0].minor.yy18
325e6 36 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  6;}.        brea
325e7 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
325e8 36 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64  6: /* insert_cmd
325e9 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a   ::= REPLACE */.
325ea 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
325eb 38 36 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b  86 = OE_Replace;
325ec 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
325ed 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 37 3a  .      case 177:
325ee 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d   /* itemlist ::=
325ef 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20   itemlist COMMA 
325f0 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
325f1 73 65 20 32 34 31 3a 20 2f 2a 20 6e 65 78 70 72  se 241: /* nexpr
325f2 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69  list ::= nexprli
325f3 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f  st COMMA expr */
325f4 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
325f5 6c 65 6e 6f 3d 3d 32 34 31 29 3b 0a 7b 79 79 67  leno==241);.{yyg
325f6 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
325f7 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
325f8 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
325f9 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
325fa 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
325fb 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a  .yy346.pExpr);}.
325fc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
325fd 20 20 20 20 20 63 61 73 65 20 31 37 38 3a 20 2f       case 178: /
325fe 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65  * itemlist ::= e
325ff 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
32600 65 20 32 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 242: /* nexprl
32601 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20  ist ::= expr */ 
32602 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32603 65 6e 6f 3d 3d 32 34 32 29 3b 0a 7b 79 79 67 6f  eno==242);.{yygo
32604 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
32605 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
32606 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79 79  pend(pParse,0,yy
32607 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32608 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20  46.pExpr);}.    
32609 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3260a 20 63 61 73 65 20 31 38 31 3a 20 2f 2a 20 69 6e   case 181: /* in
3260b 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73  scollist ::= ins
3260c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d  collist COMMA nm
3260d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
3260e 2e 79 79 34 30 38 20 3d 20 73 71 6c 69 74 65 33  .yy408 = sqlite3
3260f 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  IdListAppend(pPa
32610 72 73 65 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d 32  rse->db,yymsp[-2
32611 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 26 79  ].minor.yy408,&y
32612 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32613 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
32614 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32615 38 32 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  82: /* inscollis
32616 74 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67  t ::= nm */.{yyg
32617 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d  otominor.yy408 =
32618 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70   sqlite3IdListAp
32619 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
3261a 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
3261b 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
3261c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3261d 73 65 20 31 38 33 3a 20 2f 2a 20 65 78 70 72 20  se 183: /* expr 
3261e 3a 3a 3d 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  ::= term */.    
3261f 20 20 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 65    case 211: /* e
32620 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45  scape ::= ESCAPE
32621 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
32622 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31  ase(yyruleno==21
32623 31 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  1);.{yygotominor
32624 2e 79 79 33 34 36 20 3d 20 79 79 6d 73 70 5b 30  .yy346 = yymsp[0
32625 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 3b 7d 0a  ].minor.yy346;}.
32626 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32627 20 20 20 20 20 63 61 73 65 20 31 38 34 3a 20 2f       case 184: /
32628 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78  * expr ::= LP ex
32629 70 72 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f  pr RP */.{yygoto
3262a 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3262b 72 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  r = yymsp[-1].mi
3262c 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
3262d 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
3262e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
3262f 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32630 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
32631 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
32632 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32633 65 20 31 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a  e 185: /* term :
32634 3a 3d 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  := NULL */.     
32635 20 63 61 73 65 20 31 39 30 3a 20 2f 2a 20 74 65   case 190: /* te
32636 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46  rm ::= INTEGER|F
32637 4c 4f 41 54 7c 42 4c 4f 42 20 2a 2f 20 79 79 74  LOAT|BLOB */ yyt
32638 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32639 3d 3d 31 39 30 29 3b 0a 20 20 20 20 20 20 63 61  ==190);.      ca
3263a 73 65 20 31 39 31 3a 20 2f 2a 20 74 65 72 6d 20  se 191: /* term 
3263b 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 20 79 79  ::= STRING */ yy
3263c 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3263d 6f 3d 3d 31 39 31 29 3b 0a 7b 73 70 61 6e 45 78  o==191);.{spanEx
3263e 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr(&yygotominor.
3263f 79 79 33 34 36 2c 20 70 50 61 72 73 65 2c 20 79  yy346, pParse, y
32640 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 26  ymsp[0].major, &
32641 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32642 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
32643 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32644 31 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  186: /* expr ::=
32645 20 69 64 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   id */.      cas
32646 65 20 31 38 37 3a 20 2f 2a 20 65 78 70 72 20 3a  e 187: /* expr :
32647 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79  := JOIN_KW */ yy
32648 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32649 6f 3d 3d 31 38 37 29 3b 0a 7b 73 70 61 6e 45 78  o==187);.{spanEx
3264a 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr(&yygotominor.
3264b 79 79 33 34 36 2c 20 70 50 61 72 73 65 2c 20 54  yy346, pParse, T
3264c 4b 5f 49 44 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  K_ID, &yymsp[0].
3264d 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
3264e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3264f 20 20 63 61 73 65 20 31 38 38 3a 20 2f 2a 20 65    case 188: /* e
32650 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
32651 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74  m */.{.  Expr *t
32652 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33 50 45  emp1 = sqlite3PE
32653 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
32654 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
32655 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -2].minor.yy0);.
32656 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20 3d 20    Expr *temp2 = 
32657 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
32658 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
32659 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3265a 72 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f  r.yy0);.  yygoto
3265b 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3265c 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3265d 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c  (pParse, TK_DOT,
3265e 20 74 65 6d 70 31 2c 20 74 65 6d 70 32 2c 20 30   temp1, temp2, 0
3265f 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79  );.  spanSet(&yy
32660 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
32661 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32662 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
32663 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
32664 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32665 20 20 63 61 73 65 20 31 38 39 3a 20 2f 2a 20 65    case 189: /* e
32666 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
32667 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  m DOT nm */.{.  
32668 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71  Expr *temp1 = sq
32669 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3266a 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
3266b 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
3266c 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  .yy0);.  Expr *t
3266d 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45  emp2 = sqlite3PE
3266e 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
3266f 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
32670 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -2].minor.yy0);.
32671 20 20 45 78 70 72 20 2a 74 65 6d 70 33 20 3d 20    Expr *temp3 = 
32672 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
32673 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
32674 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
32675 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a  r.yy0);.  Expr *
32676 74 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 50  temp4 = sqlite3P
32677 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32678 44 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70  DOT, temp2, temp
32679 33 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  3, 0);.  yygotom
3267a 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3267b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3267c 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
3267d 74 65 6d 70 31 2c 20 74 65 6d 70 34 2c 20 30 29  temp1, temp4, 0)
3267e 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ;.  spanSet(&yyg
3267f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  otominor.yy346,&
32680 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
32681 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
32682 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
32683 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32684 20 63 61 73 65 20 31 39 32 3a 20 2f 2a 20 65 78   case 192: /* ex
32685 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 20  pr ::= REGISTER 
32686 2a 2f 0a 7b 0a 20 20 2f 2a 20 57 68 65 6e 20 64  */.{.  /* When d
32687 6f 69 6e 67 20 61 20 6e 65 73 74 65 64 20 70 61  oing a nested pa
32688 72 73 65 2c 20 6f 6e 65 20 63 61 6e 20 69 6e 63  rse, one can inc
32689 6c 75 64 65 20 74 65 72 6d 73 20 69 6e 20 61 6e  lude terms in an
3268a 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
3268b 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20   that look like 
3268c 74 68 69 73 3a 20 20 20 23 31 20 23 32 20 2e 2e  this:   #1 #2 ..
3268d 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 72  .  These terms r
3268e 65 66 65 72 20 74 6f 20 72 65 67 69 73 74 65 72  efer to register
3268f 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 76 69  s.  ** in the vi
32690 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
32691 23 4e 20 69 73 20 74 68 65 20 4e 2d 74 68 20 72  #N is the N-th r
32692 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 69 66  egister. */.  if
32693 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
32694 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
32695 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
32696 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a  e, "near \"%T\":
32697 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20   syntax error", 
32698 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32699 79 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  yy0);.    yygoto
3269a 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3269b 72 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  r = 0;.  }else{.
3269c 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
3269d 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
3269e 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3269f 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20  e, TK_REGISTER, 
326a0 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  0, 0, &yymsp[0].
326a1 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20  minor.yy0);.    
326a2 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
326a3 79 79 33 34 36 2e 70 45 78 70 72 20 29 20 73 71  yy346.pExpr ) sq
326a4 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 26 79  lite3GetInt32(&y
326a5 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
326a6 30 2e 7a 5b 31 5d 2c 20 26 79 79 67 6f 74 6f 6d  0.z[1], &yygotom
326a7 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
326a8 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 20  ->iTable);.  }. 
326a9 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
326aa 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26 79 79  minor.yy346, &yy
326ab 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
326ac 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
326ad 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
326ae 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
326af 61 73 65 20 31 39 33 3a 20 2f 2a 20 65 78 70 72  ase 193: /* expr
326b0 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f   ::= VARIABLE */
326b1 0a 7b 0a 20 20 73 70 61 6e 45 78 70 72 28 26 79  .{.  spanExpr(&y
326b2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
326b3 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 56 41 52  , pParse, TK_VAR
326b4 49 41 42 4c 45 2c 20 26 79 79 6d 73 70 5b 30 5d  IABLE, &yymsp[0]
326b5 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73  .minor.yy0);.  s
326b6 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
326b7 56 61 72 4e 75 6d 62 65 72 28 70 50 61 72 73 65  VarNumber(pParse
326b8 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
326b9 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 70  346.pExpr);.  sp
326ba 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
326bb 6f 72 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70  or.yy346, &yymsp
326bc 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  [0].minor.yy0, &
326bd 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
326be 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
326bf 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
326c0 20 31 39 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   194: /* expr ::
326c1 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69  = expr COLLATE i
326c2 64 73 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  ds */.{.  yygoto
326c3 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
326c4 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 53  r = sqlite3ExprS
326c5 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79  etColl(pParse, y
326c6 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
326c7 79 33 34 36 2e 70 45 78 70 72 2c 20 26 79 79 6d  y346.pExpr, &yym
326c8 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
326c9 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
326ca 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79  yy346.zStart = y
326cb 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
326cc 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79  y346.zStart;.  y
326cd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
326ce 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
326cf 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
326d0 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
326d1 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .n];.}.        b
326d2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
326d3 20 31 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   195: /* expr ::
326d4 3d 20 43 41 53 54 20 4c 50 20 65 78 70 72 20 41  = CAST LP expr A
326d5 53 20 74 79 70 65 74 6f 6b 65 6e 20 52 50 20 2a  S typetoken RP *
326d6 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
326d7 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
326d8 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
326d9 72 73 65 2c 20 54 4b 5f 43 41 53 54 2c 20 79 79  rse, TK_CAST, yy
326da 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
326db 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 26 79  346.pExpr, 0, &y
326dc 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
326dd 79 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26  y0);.  spanSet(&
326de 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
326df 36 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  6,&yymsp[-5].min
326e0 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
326e1 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
326e2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
326e3 20 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a      case 196: /*
326e4 20 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20   expr ::= ID LP 
326e5 64 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73  distinct exprlis
326e6 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20  t RP */.{.  if( 
326e7 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
326e8 79 79 31 34 20 26 26 20 79 79 6d 73 70 5b 2d 31  yy14 && yymsp[-1
326e9 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45  ].minor.yy14->nE
326ea 78 70 72 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e  xpr>pParse->db->
326eb 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
326ec 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MIT_FUNCTION_ARG
326ed 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
326ee 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
326ef 20 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d   "too many argum
326f0 65 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e  ents on function
326f1 20 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d   %T", &yymsp[-4]
326f2 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
326f3 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
326f4 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
326f5 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
326f6 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
326f7 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  1].minor.yy14, &
326f8 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
326f9 79 79 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28  yy0);.  spanSet(
326fa 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
326fb 34 36 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  46,&yymsp[-4].mi
326fc 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
326fd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
326fe 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  if( yymsp[-2].mi
326ff 6e 6f 72 2e 79 79 33 32 38 20 26 26 20 79 79 67  nor.yy328 && yyg
32700 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32701 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f  Expr ){.    yygo
32702 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32703 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  xpr->flags |= EP
32704 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d  _Distinct;.  }.}
32705 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32706 20 20 20 20 20 20 63 61 73 65 20 31 39 37 3a 20        case 197: 
32707 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  /* expr ::= ID L
32708 50 20 53 54 41 52 20 52 50 20 2a 2f 0a 7b 0a 20  P STAR RP */.{. 
32709 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3270a 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
3270b 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70  e3ExprFunction(p
3270c 50 61 72 73 65 2c 20 30 2c 20 26 79 79 6d 73 70  Parse, 0, &yymsp
3270d 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-3].minor.yy0);
3270e 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
3270f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  tominor.yy346,&y
32710 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32711 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
32712 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
32713 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32714 63 61 73 65 20 31 39 38 3a 20 2f 2a 20 74 65 72  case 198: /* ter
32715 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a  m ::= CTIME_KW *
32716 2f 0a 7b 0a 20 20 2f 2a 20 54 68 65 20 43 55 52  /.{.  /* The CUR
32717 52 45 4e 54 5f 54 49 4d 45 2c 20 43 55 52 52 45  RENT_TIME, CURRE
32718 4e 54 5f 44 41 54 45 2c 20 61 6e 64 20 43 55 52  NT_DATE, and CUR
32719 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 76  RENT_TIMESTAMP v
3271a 61 6c 75 65 73 20 61 72 65 0a 20 20 2a 2a 20 74  alues are.  ** t
3271b 72 65 61 74 65 64 20 61 73 20 66 75 6e 63 74 69  reated as functi
3271c 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20  ons that return 
3271d 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 79  constants */.  y
3271e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3271f 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
32720 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61  ExprFunction(pPa
32721 72 73 65 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d  rse, 0,&yymsp[0]
32722 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69  .minor.yy0);.  i
32723 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
32724 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20  y346.pExpr ){.  
32725 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32726 33 34 36 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20  346.pExpr->op = 
32727 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20  TK_CONST_FUNC;  
32728 0a 20 20 7d 0a 20 20 73 70 61 6e 53 65 74 28 26  .  }.  spanSet(&
32729 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3272a 36 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  6, &yymsp[0].min
3272b 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
3272c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
3272d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3272e 20 20 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f       case 199: /
3272f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
32730 41 4e 44 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  AND expr */.    
32731 20 20 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65    case 200: /* e
32732 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20  xpr ::= expr OR 
32733 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
32734 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 30  se(yyruleno==200
32735 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  );.      case 20
32736 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  1: /* expr ::= e
32737 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20  xpr LT|GT|GE|LE 
32738 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
32739 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 31  se(yyruleno==201
3273a 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  );.      case 20
3273b 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  2: /* expr ::= e
3273c 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72 20 2a  xpr EQ|NE expr *
3273d 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3273e 75 6c 65 6e 6f 3d 3d 32 30 32 29 3b 0a 20 20 20  uleno==202);.   
3273f 20 20 20 63 61 73 65 20 32 30 33 3a 20 2f 2a 20     case 203: /* 
32740 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49  expr ::= expr BI
32741 54 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46  TAND|BITOR|LSHIF
32742 54 7c 52 53 48 49 46 54 20 65 78 70 72 20 2a 2f  T|RSHIFT expr */
32743 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32744 6c 65 6e 6f 3d 3d 32 30 33 29 3b 0a 20 20 20 20  leno==203);.    
32745 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20 65    case 204: /* e
32746 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55  xpr ::= expr PLU
32747 53 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 20  S|MINUS expr */ 
32748 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32749 65 6e 6f 3d 3d 32 30 34 29 3b 0a 20 20 20 20 20  eno==204);.     
3274a 20 63 61 73 65 20 32 30 35 3a 20 2f 2a 20 65 78   case 205: /* ex
3274b 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41 52  pr ::= expr STAR
3274c 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72 20  |SLASH|REM expr 
3274d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3274e 72 75 6c 65 6e 6f 3d 3d 32 30 35 29 3b 0a 20 20  ruleno==205);.  
3274f 20 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a      case 206: /*
32750 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43   expr ::= expr C
32751 4f 4e 43 41 54 20 65 78 70 72 20 2a 2f 20 79 79  ONCAT expr */ yy
32752 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32753 6f 3d 3d 32 30 36 29 3b 0a 7b 73 70 61 6e 42 69  o==206);.{spanBi
32754 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f  naryExpr(&yygoto
32755 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
32756 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a  se,yymsp[-1].maj
32757 6f 72 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  or,&yymsp[-2].mi
32758 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
32759 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29  [0].minor.yy346)
3275a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3275b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 37  ;.      case 207
3275c 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20  : /* likeop ::= 
3275d 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20  LIKE_KW */.     
3275e 20 63 61 73 65 20 32 30 39 3a 20 2f 2a 20 6c 69   case 209: /* li
3275f 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a  keop ::= MATCH *
32760 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32761 75 6c 65 6e 6f 3d 3d 32 30 39 29 3b 0a 7b 79 79  uleno==209);.{yy
32762 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65  gotominor.yy96.e
32763 4f 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70  Operator = yymsp
32764 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79  [0].minor.yy0; y
32765 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e  ygotominor.yy96.
32766 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  not = 0;}.      
32767 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32768 61 73 65 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65  ase 208: /* like
32769 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f  op ::= NOT LIKE_
3276a 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  KW */.      case
3276b 20 32 31 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20   210: /* likeop 
3276c 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f  ::= NOT MATCH */
3276d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3276e 6c 65 6e 6f 3d 3d 32 31 30 29 3b 0a 7b 79 79 67  leno==210);.{yyg
3276f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f  otominor.yy96.eO
32770 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b  perator = yymsp[
32771 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79  0].minor.yy0; yy
32772 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e  gotominor.yy96.n
32773 6f 74 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20  ot = 1;}.       
32774 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32775 73 65 20 32 31 32 3a 20 2f 2a 20 65 73 63 61 70  se 212: /* escap
32776 65 20 3a 3a 3d 20 2a 2f 0a 7b 6d 65 6d 73 65 74  e ::= */.{memset
32777 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (&yygotominor.yy
32778 33 34 36 2c 30 2c 73 69 7a 65 6f 66 28 79 79 67  346,0,sizeof(yyg
32779 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 29 29  otominor.yy346))
3277a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3277b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 33  ;.      case 213
3277c 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
3277d 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65  pr likeop expr e
3277e 73 63 61 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70  scape */.{.  Exp
3277f 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20  rList *pList;.  
32780 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
32781 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
32782 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31  arse,0, yymsp[-1
32783 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32784 78 70 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20  xpr);.  pList = 
32785 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
32786 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69  ppend(pParse,pLi
32787 73 74 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  st, yymsp[-3].mi
32788 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29  nor.yy346.pExpr)
32789 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d  ;.  if( yymsp[0]
3278a 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
3278b 70 72 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20  pr ){.    pList 
3278c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3278d 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70  tAppend(pParse,p
3278e 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  List, yymsp[0].m
3278f 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32790 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
32791 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32792 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75   = sqlite3ExprFu
32793 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  nction(pParse, p
32794 4c 69 73 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  List, &yymsp[-2]
32795 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65  .minor.yy96.eOpe
32796 72 61 74 6f 72 29 3b 0a 20 20 69 66 28 20 79 79  rator);.  if( yy
32797 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32798 39 36 2e 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d  96.not ) yygotom
32799 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3279a 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3279b 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20  pParse, TK_NOT, 
3279c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3279d 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a  6.pExpr, 0, 0);.
3279e 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3279f 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  346.zStart = yym
327a0 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
327a1 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79 67  46.zStart;.  yyg
327a2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
327a3 45 6e 64 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  End = yymsp[-1].
327a4 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
327a5 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
327a6 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
327a7 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
327a8 33 34 36 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73  346.pExpr->flags
327a9 20 7c 3d 20 45 50 5f 49 6e 66 69 78 46 75 6e 63   |= EP_InfixFunc
327aa 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
327ab 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
327ac 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
327ad 78 70 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55  xpr ISNULL|NOTNU
327ae 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79  LL */.{spanUnary
327af 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f 6d  Postfix(&yygotom
327b0 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73  inor.yy346,pPars
327b1 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72  e,yymsp[0].major
327b2 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
327b3 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30  r.yy346,&yymsp[0
327b4 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
327b5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
327b6 20 20 20 20 63 61 73 65 20 32 31 35 3a 20 2f 2a      case 215: /*
327b7 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e   expr ::= expr N
327b8 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e  OT NULL */.{span
327b9 55 6e 61 72 79 50 6f 73 74 66 69 78 28 26 79 79  UnaryPostfix(&yy
327ba 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
327bb 70 50 61 72 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c  pParse,TK_NOTNUL
327bc 4c 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  L,&yymsp[-2].min
327bd 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
327be 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
327bf 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
327c0 20 20 20 20 20 63 61 73 65 20 32 31 36 3a 20 2f       case 216: /
327c1 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
327c2 49 53 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  IS expr */.{.  s
327c3 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79  panBinaryExpr(&y
327c4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
327c5 2c 70 50 61 72 73 65 2c 54 4b 5f 49 53 2c 26 79  ,pParse,TK_IS,&y
327c6 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
327c7 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
327c8 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 20 20 62  inor.yy346);.  b
327c9 69 6e 61 72 79 54 6f 55 6e 61 72 79 49 66 4e 75  inaryToUnaryIfNu
327ca 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ll(pParse, yymsp
327cb 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
327cc 70 45 78 70 72 2c 20 79 79 67 6f 74 6f 6d 69 6e  pExpr, yygotomin
327cd 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
327ce 54 4b 5f 49 53 4e 55 4c 4c 29 3b 0a 7d 0a 20 20  TK_ISNULL);.}.  
327cf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
327d0 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20     case 217: /* 
327d1 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
327d2 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 7b 0a 20   NOT expr */.{. 
327d3 20 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28   spanBinaryExpr(
327d4 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
327d5 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 49 53 4e  46,pParse,TK_ISN
327d6 4f 54 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  OT,&yymsp[-3].mi
327d7 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
327d8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29  [0].minor.yy346)
327d9 3b 0a 20 20 62 69 6e 61 72 79 54 6f 55 6e 61 72  ;.  binaryToUnar
327da 79 49 66 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20  yIfNull(pParse, 
327db 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
327dc 79 33 34 36 2e 70 45 78 70 72 2c 20 79 79 67 6f  y346.pExpr, yygo
327dd 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
327de 78 70 72 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 29  xpr, TK_NOTNULL)
327df 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
327e0 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
327e1 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4e  8: /* expr ::= N
327e2 4f 54 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  OT expr */.     
327e3 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 65 78   case 219: /* ex
327e4 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78  pr ::= BITNOT ex
327e5 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
327e6 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 39 29 3b  (yyruleno==219);
327e7 0a 7b 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69  .{spanUnaryPrefi
327e8 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  x(&yygotominor.y
327e9 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d 73  y346,pParse,yyms
327ea 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d  p[-1].major,&yym
327eb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
327ec 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  6,&yymsp[-1].min
327ed 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
327ee 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
327ef 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 78 70 72  ase 220: /* expr
327f0 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72 20   ::= MINUS expr 
327f1 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 72 65  */.{spanUnaryPre
327f2 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  fix(&yygotominor
327f3 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54 4b  .yy346,pParse,TK
327f4 5f 55 4d 49 4e 55 53 2c 26 79 79 6d 73 70 5b 30  _UMINUS,&yymsp[0
327f5 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  ].minor.yy346,&y
327f6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
327f7 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
327f8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
327f9 32 32 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  221: /* expr ::=
327fa 20 50 4c 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73   PLUS expr */.{s
327fb 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26  panUnaryPrefix(&
327fc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
327fd 36 2c 70 50 61 72 73 65 2c 54 4b 5f 55 50 4c 55  6,pParse,TK_UPLU
327fe 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  S,&yymsp[0].mino
327ff 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d  r.yy346,&yymsp[-
32800 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  1].minor.yy0);}.
32801 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32802 20 20 20 20 20 63 61 73 65 20 32 32 34 3a 20 2f       case 224: /
32803 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
32804 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20  between_op expr 
32805 41 4e 44 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20  AND expr */.{.  
32806 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
32807 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
32808 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
32809 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
3280a 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
3280b 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
3280c 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
3280d 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79  pParse,pList, yy
3280e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3280f 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79 67  46.pExpr);.  yyg
32810 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32811 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
32812 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 42  xpr(pParse, TK_B
32813 45 54 57 45 45 4e 2c 20 79 79 6d 73 70 5b 2d 34  ETWEEN, yymsp[-4
32814 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
32815 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  xpr, 0, 0);.  if
32816 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
32817 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
32818 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32819 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  46.pExpr->x.pLis
3281a 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 7d 65 6c  t = pList;.  }el
3281b 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
3281c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
3281d 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29  arse->db, pList)
3281e 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 79 79 6d  ;.  } .  if( yym
3281f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
32820 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  28 ) yygotominor
32821 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
32822 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
32823 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
32824 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32825 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79  xpr, 0, 0);.  yy
32826 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32827 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
32828 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  4].minor.yy346.z
32829 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d  Start;.  yygotom
3282a 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20  inor.yy346.zEnd 
3282b 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3282c 2e 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 7d 0a 20  .yy346.zEnd;.}. 
3282d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3282e 20 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a      case 227: /*
3282f 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69   expr ::= expr i
32830 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74  n_op LP exprlist
32831 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67   RP */.{.    yyg
32832 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32833 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
32834 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
32835 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  N, yymsp[-4].min
32836 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
32837 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
32838 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32839 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  .pExpr ){.      
3283a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3283b 36 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  6.pExpr->x.pList
3283c 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
3283d 6f 72 2e 79 79 31 34 3b 0a 20 20 20 20 20 20 73  or.yy14;.      s
3283e 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
3283f 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ght(pParse, yygo
32840 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32841 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  xpr);.    }else{
32842 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
32843 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
32844 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
32845 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 29 3b 0a  1].minor.yy14);.
32846 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79      }.    if( yy
32847 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32848 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  328 ) yygotomino
32849 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
3284a 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3284b 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
3284c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3284d 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
3284e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3284f 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
32850 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-4].minor.yy34
32851 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79 79  6.zStart;.    yy
32852 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32853 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
32854 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
32855 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32856 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  n];.  }.        
32857 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32858 65 20 32 32 38 3a 20 2f 2a 20 65 78 70 72 20 3a  e 228: /* expr :
32859 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  := LP select RP 
3285a 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
3285b 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3285c 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3285d 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c 45 43  pParse, TK_SELEC
3285e 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  T, 0, 0, 0);.   
3285f 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
32860 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
32861 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
32862 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78  r.yy346.pExpr->x
32863 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70  .pSelect = yymsp
32864 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a  [-1].minor.yy3;.
32865 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
32866 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f  perty(yygotomino
32867 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 45  r.yy346.pExpr, E
32868 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20  P_xIsSelect);.  
32869 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
3286a 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
3286b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3286c 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  46.pExpr);.    }
3286d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
3286e 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
3286f 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
32870 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29  p[-1].minor.yy3)
32871 3b 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f  ;.    }.    yygo
32872 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
32873 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  tart = yymsp[-2]
32874 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
32875 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32876 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  346.zEnd = &yyms
32877 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
32878 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
32879 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20  yy0.n];.  }.    
3287a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3287b 20 63 61 73 65 20 32 32 39 3a 20 2f 2a 20 65 78   case 229: /* ex
3287c 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
3287d 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a  p LP select RP *
3287e 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  /.{.    yygotomi
3287f 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32880 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32881 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79  Parse, TK_IN, yy
32882 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32883 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
32884 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f  ;.    if( yygoto
32885 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32886 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  r ){.      yygot
32887 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32888 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20  pr->x.pSelect = 
32889 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3288a 79 79 33 3b 0a 20 20 20 20 20 20 45 78 70 72 53  yy3;.      ExprS
3288b 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74  etProperty(yygot
3288c 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
3288d 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
3288e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3288f 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
32890 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
32891 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
32892 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32893 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
32894 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
32895 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
32896 2e 79 79 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20  .yy3);.    }.   
32897 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
32898 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79 79 67  inor.yy328 ) yyg
32899 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3289a 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
3289b 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
3289c 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  OT, yygotominor.
3289d 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20  yy346.pExpr, 0, 
3289e 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  0);.    yygotomi
3289f 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
328a0 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e   = yymsp[-4].min
328a1 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b  or.yy346.zStart;
328a2 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
328a3 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79  .yy346.zEnd = &y
328a4 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
328a5 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0.z[yymsp[0].min
328a6 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20  or.yy0.n];.  }. 
328a7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
328a8 20 20 20 20 63 61 73 65 20 32 33 30 3a 20 2f 2a      case 230: /*
328a9 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69   expr ::= expr i
328aa 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  n_op nm dbnm */.
328ab 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  {.    SrcList *p
328ac 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Src = sqlite3Src
328ad 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
328ae 65 2d 3e 64 62 2c 20 30 2c 26 79 79 6d 73 70 5b  e->db, 0,&yymsp[
328af 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
328b0 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
328b1 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  0);.    yygotomi
328b2 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
328b3 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
328b4 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79  Parse, TK_IN, yy
328b5 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
328b6 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
328b7 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f  ;.    if( yygoto
328b8 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
328b9 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  r ){.      yygot
328ba 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
328bb 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20  pr->x.pSelect = 
328bc 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
328bd 28 70 50 61 72 73 65 2c 20 30 2c 70 53 72 63 2c  (pParse, 0,pSrc,
328be 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a  0,0,0,0,0,0,0);.
328bf 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
328c0 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f  perty(yygotomino
328c1 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 45  r.yy346.pExpr, E
328c2 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20  P_xIsSelect);.  
328c3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
328c4 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
328c5 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
328c6 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  46.pExpr);.    }
328c7 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
328c8 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
328c9 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72  (pParse->db, pSr
328ca 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  c);.    }.    if
328cb 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ( yymsp[-2].mino
328cc 72 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74 6f  r.yy328 ) yygoto
328cd 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
328ce 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
328cf 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
328d0 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
328d1 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  46.pExpr, 0, 0);
328d2 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
328d3 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20  .yy346.zStart = 
328d4 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
328d5 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20  yy346.zStart;.  
328d6 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
328d7 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70  346.zEnd = yymsp
328d8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  [0].minor.yy0.z 
328d9 3f 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ? &yymsp[0].mino
328da 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
328db 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 3a 20  .minor.yy0.n] : 
328dc 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
328dd 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 2d 31 5d  .yy0.z[yymsp[-1]
328de 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20  .minor.yy0.n];. 
328df 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
328e0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 31  ;.      case 231
328e1 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45 58  : /* expr ::= EX
328e2 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20 52  ISTS LP select R
328e3 50 20 2a 2f 0a 7b 0a 20 20 20 20 45 78 70 72 20  P */.{.    Expr 
328e4 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  *p = yygotominor
328e5 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
328e6 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
328e7 73 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c 20 30  se, TK_EXISTS, 0
328e8 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
328e9 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 78   p ){.      p->x
328ea 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70  .pSelect = yymsp
328eb 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a  [-1].minor.yy3;.
328ec 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
328ed 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53  perty(p, EP_xIsS
328ee 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71  elect);.      sq
328ef 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
328f0 68 74 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  ht(pParse, p);. 
328f1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
328f2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
328f3 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
328f4 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
328f5 79 79 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  yy3);.    }.    
328f6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
328f7 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  6.zStart = yymsp
328f8 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
328f9 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;.    yygotomino
328fa 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26  r.yy346.zEnd = &
328fb 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
328fc 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
328fd 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a  nor.yy0.n];.  }.
328fe 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
328ff 20 20 20 20 20 63 61 73 65 20 32 33 32 3a 20 2f       case 232: /
32900 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20  * expr ::= CASE 
32901 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73  case_operand cas
32902 65 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f  e_exprlist case_
32903 65 6c 73 65 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20  else END */.{.  
32904 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32905 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
32906 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
32907 4b 5f 43 41 53 45 2c 20 79 79 6d 73 70 5b 2d 33  K_CASE, yymsp[-3
32908 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20 79  ].minor.yy132, y
32909 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3290a 79 31 33 32 2c 20 30 29 3b 0a 20 20 69 66 28 20  y132, 0);.  if( 
3290b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3290c 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79  6.pExpr ){.    y
3290d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3290e 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20  .pExpr->x.pList 
3290f 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
32910 72 2e 79 79 31 34 3b 0a 20 20 20 20 73 71 6c 69  r.yy14;.    sqli
32911 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
32912 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
32913 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32914 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
32915 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
32916 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
32917 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
32918 72 2e 79 79 31 34 29 3b 0a 20 20 7d 0a 20 20 79  r.yy14);.  }.  y
32919 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3291a 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
3291b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -4].minor.yy0.z;
3291c 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3291d 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
3291e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3291f 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32920 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20  .yy0.n];.}.     
32921 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32922 63 61 73 65 20 32 33 33 3a 20 2f 2a 20 63 61 73  case 233: /* cas
32923 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63  e_exprlist ::= c
32924 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48 45  ase_exprlist WHE
32925 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
32926 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
32927 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
32928 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
32929 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34  (pParse,yymsp[-4
3292a 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79  ].minor.yy14, yy
3292b 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3292c 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79  346.pExpr);.  yy
3292d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
3292e 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3292f 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
32930 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  gotominor.yy14, 
32931 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32932 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20  y346.pExpr);.}. 
32933 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32934 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a      case 234: /*
32935 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a   case_exprlist :
32936 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
32937 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79  N expr */.{.  yy
32938 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
32939 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3293a 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
3293b 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
3293c 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
3293d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3293e 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
3293f 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
32940 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  ,yygotominor.yy1
32941 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  4, yymsp[0].mino
32942 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
32943 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32944 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 33 3a  .      case 243:
32945 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61   /* cmd ::= crea
32946 74 65 6b 77 20 75 6e 69 71 75 65 66 6c 61 67 20  tekw uniqueflag 
32947 49 4e 44 45 58 20 69 66 6e 6f 74 65 78 69 73 74  INDEX ifnotexist
32948 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20  s nm dbnm ON nm 
32949 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f  LP idxlist RP */
3294a 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  .{.  sqlite3Crea
3294b 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  teIndex(pParse, 
3294c 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  &yymsp[-6].minor
3294d 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 35 5d  .yy0, &yymsp[-5]
3294e 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 0a 20 20 20  .minor.yy0, .   
3294f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32950 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
32951 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
32952 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  b,0,&yymsp[-3].m
32953 69 6e 6f 72 2e 79 79 30 2c 30 29 2c 20 79 79 6d  inor.yy0,0), yym
32954 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
32955 34 2c 20 79 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e  4, yymsp[-9].min
32956 6f 72 2e 79 79 33 32 38 2c 0a 20 20 20 20 20 20  or.yy328,.      
32957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32958 26 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f  &yymsp[-10].mino
32959 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
3295a 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 53 51 4c 49  .minor.yy0, SQLI
3295b 54 45 5f 53 4f 5f 41 53 43 2c 20 79 79 6d 73 70  TE_SO_ASC, yymsp
3295c 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-7].minor.yy328
3295d 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
3295e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3295f 34 34 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61  44: /* uniquefla
32960 67 20 3a 3a 3d 20 55 4e 49 51 55 45 20 2a 2f 0a  g ::= UNIQUE */.
32961 20 20 20 20 20 20 63 61 73 65 20 32 39 38 3a 20        case 298: 
32962 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  /* raisetype ::=
32963 20 41 42 4f 52 54 20 2a 2f 20 79 79 74 65 73 74   ABORT */ yytest
32964 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
32965 39 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  98);.{yygotomino
32966 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 41 62 6f  r.yy328 = OE_Abo
32967 72 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  rt;}.        bre
32968 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32969 34 35 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61  45: /* uniquefla
3296a 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  g ::= */.{yygoto
3296b 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
3296c 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20 20 20 20  _None;}.        
3296d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3296e 65 20 32 34 38 3a 20 2f 2a 20 69 64 78 6c 69 73  e 248: /* idxlis
3296f 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f  t ::= idxlist CO
32970 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73  MMA nm collate s
32971 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20  ortorder */.{.  
32972 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69  Expr *p = 0;.  i
32973 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  f( yymsp[-1].min
32974 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20 20  or.yy0.n>0 ){.  
32975 20 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70    p = sqlite3Exp
32976 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b  r(pParse->db, TK
32977 5f 43 4f 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20 20  _COLUMN, 0);.   
32978 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43   sqlite3ExprSetC
32979 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 26  oll(pParse, p, &
3297a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3297b 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  yy0);.  }.  yygo
3297c 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
3297d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
3297e 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
3297f 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-4].minor.yy14
32980 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , p);.  sqlite3E
32981 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
32982 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
32983 72 2e 79 79 31 34 2c 26 79 79 6d 73 70 5b 2d 32  r.yy14,&yymsp[-2
32984 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 0a  ].minor.yy0,1);.
32985 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
32986 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61  tCheckLength(pPa
32987 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
32988 2e 79 79 31 34 2c 20 22 69 6e 64 65 78 22 29 3b  .yy14, "index");
32989 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
3298a 6f 72 2e 79 79 31 34 20 29 20 79 79 67 6f 74 6f  or.yy14 ) yygoto
3298b 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 79 79  minor.yy14->a[yy
3298c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e  gotominor.yy14->
3298d 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64  nExpr-1].sortOrd
3298e 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30  er = (u8)yymsp[0
3298f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d  ].minor.yy328;.}
32990 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32991 20 20 20 20 20 20 63 61 73 65 20 32 34 39 3a 20        case 249: 
32992 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e  /* idxlist ::= n
32993 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72  m collate sortor
32994 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20  der */.{.  Expr 
32995 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79  *p = 0;.  if( yy
32996 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32997 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d  0.n>0 ){.    p =
32998 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
32999 61 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c  arse, TK_COLUMN,
3299a 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73   0, 0, 0);.    s
3299b 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c  qlite3ExprSetCol
3299c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79  l(pParse, p, &yy
3299d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3299e 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  0);.  }.  yygoto
3299f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
329a0 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
329a1 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 70 29 3b  nd(pParse,0, p);
329a2 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
329a3 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65  stSetName(pParse
329a4 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
329a5 31 34 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  14, &yymsp[-2].m
329a6 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 20 20  inor.yy0, 1);.  
329a7 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
329a8 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73  heckLength(pPars
329a9 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
329aa 79 31 34 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20  y14, "index");. 
329ab 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
329ac 2e 79 79 31 34 20 29 20 79 79 67 6f 74 6f 6d 69  .yy14 ) yygotomi
329ad 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 79 79 67 6f  nor.yy14->a[yygo
329ae 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45  tominor.yy14->nE
329af 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72  xpr-1].sortOrder
329b0 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e   = (u8)yymsp[0].
329b1 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20  minor.yy328;.}. 
329b2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
329b3 20 20 20 20 63 61 73 65 20 32 35 30 3a 20 2f 2a      case 250: /*
329b4 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a   collate ::= */.
329b5 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
329b6 2e 7a 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69  .z = 0; yygotomi
329b7 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a  nor.yy0.n = 0;}.
329b8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
329b9 20 20 20 20 20 63 61 73 65 20 32 35 32 3a 20 2f       case 252: /
329ba 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49  * cmd ::= DROP I
329bb 4e 44 45 58 20 69 66 65 78 69 73 74 73 20 66 75  NDEX ifexists fu
329bc 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74  llname */.{sqlit
329bd 65 33 44 72 6f 70 49 6e 64 65 78 28 70 50 61 72  e3DropIndex(pPar
329be 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
329bf 6f 72 2e 79 79 36 35 2c 20 79 79 6d 73 70 5b 2d  or.yy65, yymsp[-
329c0 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  1].minor.yy328);
329c1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
329c2 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 33 3a  .      case 253:
329c3 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55   /* cmd ::= VACU
329c4 55 4d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  UM */.      case
329c5 20 32 35 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   254: /* cmd ::=
329c6 20 56 41 43 55 55 4d 20 6e 6d 20 2a 2f 20 79 79   VACUUM nm */ yy
329c7 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
329c8 6f 3d 3d 32 35 34 29 3b 0a 7b 73 71 6c 69 74 65  o==254);.{sqlite
329c9 33 56 61 63 75 75 6d 28 70 50 61 72 73 65 29 3b  3Vacuum(pParse);
329ca 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
329cb 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 35 3a  .      case 255:
329cc 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47   /* cmd ::= PRAG
329cd 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73  MA nm dbnm */.{s
329ce 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
329cf 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse,&yymsp[-1].m
329d0 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
329d1 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30  0].minor.yy0,0,0
329d2 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
329d3 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
329d4 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  6: /* cmd ::= PR
329d5 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
329d6 6e 6d 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65  nmnum */.{sqlite
329d7 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26  3Pragma(pParse,&
329d8 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
329d9 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0,&yymsp[-2].m
329da 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
329db 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b  0].minor.yy0,0);
329dc 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
329dd 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37 3a  .      case 257:
329de 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47   /* cmd ::= PRAG
329df 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d  MA nm dbnm LP nm
329e0 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74  num RP */.{sqlit
329e1 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
329e2 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
329e3 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  .yy0,&yymsp[-3].
329e4 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
329e5 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  [-1].minor.yy0,0
329e6 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
329e7 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
329e8 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  8: /* cmd ::= PR
329e9 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
329ea 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 7b 73 71  minus_num */.{sq
329eb 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
329ec 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se,&yymsp[-3].mi
329ed 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
329ee 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  2].minor.yy0,&yy
329ef 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
329f0 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,1);}.        br
329f1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
329f2 32 35 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  259: /* cmd ::= 
329f3 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
329f4 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50 20 2a  P minus_num RP *
329f5 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
329f6 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
329f7 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
329f8 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
329f9 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
329fa 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a 20 20 20 20  or.yy0,1);}.    
329fb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
329fc 20 63 61 73 65 20 32 37 30 3a 20 2f 2a 20 63 6d   case 270: /* cm
329fd 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  d ::= createkw t
329fe 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49  rigger_decl BEGI
329ff 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  N trigger_cmd_li
32a00 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 54 6f  st END */.{.  To
32a01 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a  ken all;.  all.z
32a02 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   = yymsp[-3].min
32a03 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 61 6c 6c 2e  or.yy0.z;.  all.
32a04 6e 20 3d 20 28 69 6e 74 29 28 79 79 6d 73 70 5b  n = (int)(yymsp[
32a05 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d  0].minor.yy0.z -
32a06 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
32a07 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d 73 70 5b  .yy0.z) + yymsp[
32a08 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a  0].minor.yy0.n;.
32a09 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54    sqlite3FinishT
32a0a 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 79  rigger(pParse, y
32a0b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32a0c 79 34 37 33 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20  y473, &all);.}. 
32a0d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32a0e 20 20 20 20 63 61 73 65 20 32 37 31 3a 20 2f 2a      case 271: /*
32a0f 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a   trigger_decl ::
32a10 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69  = temp TRIGGER i
32a11 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
32a12 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  nm trigger_time 
32a13 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e  trigger_event ON
32a14 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63   fullname foreac
32a15 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c  h_clause when_cl
32a16 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  ause */.{.  sqli
32a17 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28  te3BeginTrigger(
32a18 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
32a19 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  7].minor.yy0, &y
32a1a 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
32a1b 79 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  y0, yymsp[-5].mi
32a1c 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d 73 70  nor.yy328, yymsp
32a1d 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 38  [-4].minor.yy378
32a1e 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  .a, yymsp[-4].mi
32a1f 6e 6f 72 2e 79 79 33 37 38 2e 62 2c 20 79 79 6d  nor.yy378.b, yym
32a20 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-2].minor.yy6
32a21 35 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  5, yymsp[0].mino
32a22 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70 5b 2d  r.yy132, yymsp[-
32a23 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  10].minor.yy328,
32a24 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69 6e 6f 72   yymsp[-8].minor
32a25 2e 79 79 33 32 38 29 3b 0a 20 20 79 79 67 6f 74  .yy328);.  yygot
32a26 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 28 79 79  ominor.yy0 = (yy
32a27 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
32a28 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d  0.n==0?yymsp[-7]
32a29 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79 6d 73 70  .minor.yy0:yymsp
32a2a 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-6].minor.yy0);
32a2b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32a2c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 32  ;.      case 272
32a2d 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d  : /* trigger_tim
32a2e 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20 2a 2f 0a  e ::= BEFORE */.
32a2f 20 20 20 20 20 20 63 61 73 65 20 32 37 35 3a 20        case 275: 
32a30 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  /* trigger_time 
32a31 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
32a32 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 35 29  e(yyruleno==275)
32a33 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.{ yygotominor.
32a34 79 79 33 32 38 20 3d 20 54 4b 5f 42 45 46 4f 52  yy328 = TK_BEFOR
32a35 45 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  E; }.        bre
32a36 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32a37 37 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74  73: /* trigger_t
32a38 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 20 2a 2f  ime ::= AFTER */
32a39 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
32a3a 79 33 32 38 20 3d 20 54 4b 5f 41 46 54 45 52 3b  y328 = TK_AFTER;
32a3b 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
32a3c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
32a3d 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69  4: /* trigger_ti
32a3e 6d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f  me ::= INSTEAD O
32a3f 46 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  F */.{ yygotomin
32a40 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 49 4e  or.yy328 = TK_IN
32a41 53 54 45 41 44 3b 7d 0a 20 20 20 20 20 20 20 20  STEAD;}.        
32a42 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32a43 65 20 32 37 36 3a 20 2f 2a 20 74 72 69 67 67 65  e 276: /* trigge
32a44 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45  r_event ::= DELE
32a45 54 45 7c 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20  TE|INSERT */.   
32a46 20 20 20 63 61 73 65 20 32 37 37 3a 20 2f 2a 20     case 277: /* 
32a47 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a  trigger_event ::
32a48 3d 20 55 50 44 41 54 45 20 2a 2f 20 79 79 74 65  = UPDATE */ yyte
32a49 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32a4a 3d 32 37 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =277);.{yygotomi
32a4b 6e 6f 72 2e 79 79 33 37 38 2e 61 20 3d 20 79 79  nor.yy378.a = yy
32a4c 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79  msp[0].major; yy
32a4d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e  gotominor.yy378.
32a4e 62 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  b = 0;}.        
32a4f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32a50 65 20 32 37 38 3a 20 2f 2a 20 74 72 69 67 67 65  e 278: /* trigge
32a51 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41  r_event ::= UPDA
32a52 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73 74  TE OF inscollist
32a53 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32a54 2e 79 79 33 37 38 2e 61 20 3d 20 54 4b 5f 55 50  .yy378.a = TK_UP
32a55 44 41 54 45 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  DATE; yygotomino
32a56 72 2e 79 79 33 37 38 2e 62 20 3d 20 79 79 6d 73  r.yy378.b = yyms
32a57 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38  p[0].minor.yy408
32a58 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32a59 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 31  ;.      case 281
32a5a 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65  : /* when_clause
32a5b 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
32a5c 73 65 20 33 30 33 3a 20 2f 2a 20 6b 65 79 5f 6f  se 303: /* key_o
32a5d 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
32a5e 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32a5f 30 33 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  03);.{ yygotomin
32a60 6f 72 2e 79 79 31 33 32 20 3d 20 30 3b 20 7d 0a  or.yy132 = 0; }.
32a61 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32a62 20 20 20 20 20 63 61 73 65 20 32 38 32 3a 20 2f       case 282: /
32a63 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a  * when_clause ::
32a64 3d 20 57 48 45 4e 20 65 78 70 72 20 2a 2f 0a 20  = WHEN expr */. 
32a65 20 20 20 20 20 63 61 73 65 20 33 30 34 3a 20 2f       case 304: /
32a66 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45  * key_opt ::= KE
32a67 59 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  Y expr */ yytest
32a68 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32a69 30 34 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  04);.{ yygotomin
32a6a 6f 72 2e 79 79 31 33 32 20 3d 20 79 79 6d 73 70  or.yy132 = yymsp
32a6b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
32a6c 70 45 78 70 72 3b 20 7d 0a 20 20 20 20 20 20 20  pExpr; }.       
32a6d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32a6e 73 65 20 32 38 33 3a 20 2f 2a 20 74 72 69 67 67  se 283: /* trigg
32a6f 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20  er_cmd_list ::= 
32a70 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
32a71 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d   trigger_cmd SEM
32a72 49 20 2a 2f 0a 7b 0a 20 20 61 73 73 65 72 74 28  I */.{.  assert(
32a73 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
32a74 2e 79 79 34 37 33 21 3d 30 20 29 3b 0a 20 20 79  .yy473!=0 );.  y
32a75 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32a76 79 34 37 33 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65  y473->pLast->pNe
32a77 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  xt = yymsp[-1].m
32a78 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79  inor.yy473;.  yy
32a79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32a7a 34 37 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d  473->pLast = yym
32a7b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
32a7c 37 33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  73;.  yygotomino
32a7d 72 2e 79 79 34 37 33 20 3d 20 79 79 6d 73 70 5b  r.yy473 = yymsp[
32a7e 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b  -2].minor.yy473;
32a7f 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32a80 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 34  ;.      case 284
32a81 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
32a82 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
32a83 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 20  r_cmd SEMI */.{ 
32a84 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d 73 70  .  assert( yymsp
32a85 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-1].minor.yy473
32a86 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70 5b 2d  !=0 );.  yymsp[-
32a87 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e  1].minor.yy473->
32a88 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pLast = yymsp[-1
32a89 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20  ].minor.yy473;. 
32a8a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
32a8b 37 33 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  73 = yymsp[-1].m
32a8c 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a 20 20  inor.yy473;.}.  
32a8d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32a8e 20 20 20 63 61 73 65 20 32 38 36 3a 20 2f 2a 20     case 286: /* 
32a8f 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20  trnm ::= nm DOT 
32a90 6e 6d 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  nm */.{.  yygoto
32a91 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73  minor.yy0 = yyms
32a92 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a  p[0].minor.yy0;.
32a93 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
32a94 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
32a95 20 20 20 22 71 75 61 6c 69 66 69 65 64 20 74 61     "qualified ta
32a96 62 6c 65 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f  ble names are no
32a97 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 49 4e 53  t allowed on INS
32a98 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64  ERT, UPDATE, and
32a99 20 44 45 4c 45 54 45 20 22 0a 20 20 20 20 20 20   DELETE ".      
32a9a 20 20 22 73 74 61 74 65 6d 65 6e 74 73 20 77 69    "statements wi
32a9b 74 68 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b  thin triggers");
32a9c 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32a9d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 38  ;.      case 288
32a9e 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a 3a 3d  : /* tridxby ::=
32a9f 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a   INDEXED BY nm *
32aa0 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  /.{.  sqlite3Err
32aa1 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20  orMsg(pParse,.  
32aa2 20 20 20 20 20 20 22 74 68 65 20 49 4e 44 45 58        "the INDEX
32aa3 45 44 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ED BY clause is 
32aa4 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 55  not allowed on U
32aa5 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
32aa6 73 74 61 74 65 6d 65 6e 74 73 20 22 0a 20 20 20  statements ".   
32aa7 20 20 20 20 20 22 77 69 74 68 69 6e 20 74 72 69       "within tri
32aa8 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20  ggers");.}.     
32aa9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32aaa 63 61 73 65 20 32 38 39 3a 20 2f 2a 20 74 72 69  case 289: /* tri
32aab 64 78 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44  dxby ::= NOT IND
32aac 45 58 45 44 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  EXED */.{.  sqli
32aad 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
32aae 73 65 2c 0a 20 20 20 20 20 20 20 20 22 74 68 65  se,.        "the
32aaf 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
32ab0 75 73 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  use is not allow
32ab1 65 64 20 6f 6e 20 55 50 44 41 54 45 20 6f 72 20  ed on UPDATE or 
32ab2 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
32ab3 73 20 22 0a 20 20 20 20 20 20 20 20 22 77 69 74  s ".        "wit
32ab4 68 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0a  hin triggers");.
32ab5 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32ab6 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30 3a  .      case 290:
32ab7 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
32ab8 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
32ab9 66 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 53  f trnm tridxby S
32aba 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
32abb 5f 6f 70 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  _opt */.{ yygoto
32abc 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71  minor.yy473 = sq
32abd 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64 61  lite3TriggerUpda
32abe 74 65 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64  teStep(pParse->d
32abf 62 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  b, &yymsp[-4].mi
32ac0 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
32ac1 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  1].minor.yy14, y
32ac2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32ac3 31 33 32 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  132, yymsp[-5].m
32ac4 69 6e 6f 72 2e 79 79 31 38 36 29 3b 20 7d 0a 20  inor.yy186); }. 
32ac5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32ac6 20 20 20 20 63 61 73 65 20 32 39 31 3a 20 2f 2a      case 291: /*
32ac7 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
32ac8 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
32ac9 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74   trnm inscollist
32aca 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
32acb 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79  temlist RP */.{y
32acc 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33  ygotominor.yy473
32acd 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
32ace 72 49 6e 73 65 72 74 53 74 65 70 28 70 50 61 72  rInsertStep(pPar
32acf 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d  se->db, &yymsp[-
32ad0 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79  5].minor.yy0, yy
32ad1 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32ad2 34 30 38 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  408, yymsp[-1].m
32ad3 69 6e 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79 79  inor.yy14, 0, yy
32ad4 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-7].minor.yy
32ad5 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  186);}.        b
32ad6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32ad7 20 32 39 32 3a 20 2f 2a 20 74 72 69 67 67 65 72   292: /* trigger
32ad8 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
32ad9 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e  cmd INTO trnm in
32ada 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c  scollist_opt sel
32adb 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
32adc 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69  nor.yy473 = sqli
32add 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74  te3TriggerInsert
32ade 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
32adf 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
32ae0 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31 5d  r.yy0, yymsp[-1]
32ae1 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 30 2c  .minor.yy408, 0,
32ae2 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32ae3 79 79 33 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  yy3, yymsp[-4].m
32ae4 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20  inor.yy186);}.  
32ae5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32ae6 20 20 20 63 61 73 65 20 32 39 33 3a 20 2f 2a 20     case 293: /* 
32ae7 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
32ae8 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 6e 6d  DELETE FROM trnm
32ae9 20 74 72 69 64 78 62 79 20 77 68 65 72 65 5f 6f   tridxby where_o
32aea 70 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  pt */.{yygotomin
32aeb 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74  or.yy473 = sqlit
32aec 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53  e3TriggerDeleteS
32aed 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
32aee 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32aef 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0, yymsp[0].m
32af0 69 6e 6f 72 2e 79 79 31 33 32 29 3b 7d 0a 20 20  inor.yy132);}.  
32af1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32af2 20 20 20 63 61 73 65 20 32 39 34 3a 20 2f 2a 20     case 294: /* 
32af3 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
32af4 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74  select */.{yygot
32af5 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73  ominor.yy473 = s
32af6 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c  qlite3TriggerSel
32af7 65 63 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ectStep(pParse->
32af8 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  db, yymsp[0].min
32af9 6f 72 2e 79 79 33 29 3b 20 7d 0a 20 20 20 20 20  or.yy3); }.     
32afa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32afb 63 61 73 65 20 32 39 35 3a 20 2f 2a 20 65 78 70  case 295: /* exp
32afc 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49  r ::= RAISE LP I
32afd 47 4e 4f 52 45 20 52 50 20 2a 2f 0a 7b 0a 20 20  GNORE RP */.{.  
32afe 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32aff 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
32b00 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
32b01 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c 20 30  K_RAISE, 0, 0, 0
32b02 29 3b 20 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ); .  if( yygoto
32b03 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32b04 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  r ){.    yygotom
32b05 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32b06 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f  ->affinity = OE_
32b07 49 67 6e 6f 72 65 3b 0a 20 20 7d 0a 20 20 79 79  Ignore;.  }.  yy
32b08 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32b09 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
32b0a 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a  3].minor.yy0.z;.
32b0b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32b0c 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  346.zEnd = &yyms
32b0d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
32b0e 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
32b0f 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20  yy0.n];.}.      
32b10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32b11 61 73 65 20 32 39 36 3a 20 2f 2a 20 65 78 70 72  ase 296: /* expr
32b12 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
32b13 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
32b14 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
32b15 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32b16 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
32b17 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49  r(pParse, TK_RAI
32b18 53 45 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  SE, 0, 0, &yymsp
32b19 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
32b1a 20 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69   .  if( yygotomi
32b1b 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32b1c 29 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  ) {.    yygotomi
32b1d 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d  nor.yy346.pExpr-
32b1e 3e 61 66 66 69 6e 69 74 79 20 3d 20 28 63 68 61  >affinity = (cha
32b1f 72 29 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  r)yymsp[-3].mino
32b20 72 2e 79 79 33 32 38 3b 0a 20 20 7d 0a 20 20 79  r.yy328;.  }.  y
32b21 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32b22 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
32b23 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -5].minor.yy0.z;
32b24 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32b25 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
32b26 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32b27 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32b28 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20  .yy0.n];.}.     
32b29 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32b2a 63 61 73 65 20 32 39 37 3a 20 2f 2a 20 72 61 69  case 297: /* rai
32b2b 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42  setype ::= ROLLB
32b2c 41 43 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ACK */.{yygotomi
32b2d 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 52  nor.yy328 = OE_R
32b2e 6f 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20  ollback;}.      
32b2f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32b30 61 73 65 20 32 39 39 3a 20 2f 2a 20 72 61 69 73  ase 299: /* rais
32b31 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a  etype ::= FAIL *
32b32 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
32b33 79 33 32 38 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d  y328 = OE_Fail;}
32b34 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32b35 20 20 20 20 20 20 63 61 73 65 20 33 30 30 3a 20        case 300: 
32b36 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  /* cmd ::= DROP 
32b37 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74 73  TRIGGER ifexists
32b38 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20   fullname */.{. 
32b39 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
32b3a 67 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73 70  ger(pParse,yymsp
32b3b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 79  [0].minor.yy65,y
32b3c 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32b3d 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y328);.}.       
32b3e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32b3f 73 65 20 33 30 31 3a 20 2f 2a 20 63 6d 64 20 3a  se 301: /* cmd :
32b40 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61  := ATTACH databa
32b41 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41  se_kw_opt expr A
32b42 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a  S expr key_opt *
32b43 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 74 74  /.{.  sqlite3Att
32b44 61 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ach(pParse, yyms
32b45 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-3].minor.yy34
32b46 36 2e 70 45 78 70 72 2c 20 79 79 6d 73 70 5b 2d  6.pExpr, yymsp[-
32b47 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  1].minor.yy346.p
32b48 45 78 70 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  Expr, yymsp[0].m
32b49 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20  inor.yy132);.}. 
32b4a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32b4b 20 20 20 20 63 61 73 65 20 33 30 32 3a 20 2f 2a      case 302: /*
32b4c 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20   cmd ::= DETACH 
32b4d 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
32b4e 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  expr */.{.  sqli
32b4f 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73 65  te3Detach(pParse
32b50 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
32b51 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d  .yy346.pExpr);.}
32b52 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32b53 20 20 20 20 20 20 63 61 73 65 20 33 30 37 3a 20        case 307: 
32b54 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  /* cmd ::= REIND
32b55 45 58 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65  EX */.{sqlite3Re
32b56 69 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30 2c  index(pParse, 0,
32b57 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72   0);}.        br
32b58 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32b59 33 30 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  308: /* cmd ::= 
32b5a 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 20  REINDEX nm dbnm 
32b5b 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64  */.{sqlite3Reind
32b5c 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
32b5d 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
32b5e 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32b5f 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
32b60 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32b61 65 20 33 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 309: /* cmd ::
32b62 3d 20 41 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71  = ANALYZE */.{sq
32b63 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61  lite3Analyze(pPa
32b64 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20  rse, 0, 0);}.   
32b65 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32b66 20 20 63 61 73 65 20 33 31 30 3a 20 2f 2a 20 63    case 310: /* c
32b67 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e  md ::= ANALYZE n
32b68 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74  m dbnm */.{sqlit
32b69 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65  e3Analyze(pParse
32b6a 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
32b6b 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
32b6c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
32b6d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32b6e 20 20 20 20 63 61 73 65 20 33 31 31 3a 20 2f 2a      case 311: /*
32b6f 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54   cmd ::= ALTER T
32b70 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45  ABLE fullname RE
32b71 4e 41 4d 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a  NAME TO nm */.{.
32b72 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65    sqlite3AlterRe
32b73 6e 61 6d 65 54 61 62 6c 65 28 70 50 61 72 73 65  nameTable(pParse
32b74 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
32b75 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b 30 5d 2e  .yy65,&yymsp[0].
32b76 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
32b77 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32b78 20 20 20 63 61 73 65 20 33 31 32 3a 20 2f 2a 20     case 312: /* 
32b79 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41  cmd ::= ALTER TA
32b7a 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66  BLE add_column_f
32b7b 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f  ullname ADD kwco
32b7c 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20  lumn_opt column 
32b7d 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c  */.{.  sqlite3Al
32b7e 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
32b7f 6d 6e 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  mn(pParse, &yyms
32b80 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
32b81 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32b82 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 33  ;.      case 313
32b83 3a 20 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f  : /* add_column_
32b84 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c  fullname ::= ful
32b85 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50 61  lname */.{.  pPa
32b86 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69  rse->db->lookasi
32b87 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b  de.bEnabled = 0;
32b88 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42  .  sqlite3AlterB
32b89 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 70 50  eginAddColumn(pP
32b8a 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
32b8b 69 6e 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20 20  inor.yy65);.}.  
32b8c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32b8d 20 20 20 63 61 73 65 20 33 31 36 3a 20 2f 2a 20     case 316: /* 
32b8e 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76  cmd ::= create_v
32b8f 74 61 62 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56  tab */.{sqlite3V
32b90 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 70  tabFinishParse(p
32b91 50 61 72 73 65 2c 30 29 3b 7d 0a 20 20 20 20 20  Parse,0);}.     
32b92 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32b93 63 61 73 65 20 33 31 37 3a 20 2f 2a 20 63 6d 64  case 317: /* cmd
32b94 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
32b95 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74 20   LP vtabarglist 
32b96 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74  RP */.{sqlite3Vt
32b97 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 70 50  abFinishParse(pP
32b98 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  arse,&yymsp[0].m
32b99 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
32b9a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32b9b 20 63 61 73 65 20 33 31 38 3a 20 2f 2a 20 63 72   case 318: /* cr
32b9c 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72  eate_vtab ::= cr
32b9d 65 61 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54  eatekw VIRTUAL T
32b9e 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49  ABLE nm dbnm USI
32b9f 4e 47 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73  NG nm */.{.    s
32ba0 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50  qlite3VtabBeginP
32ba1 61 72 73 65 28 70 50 61 72 73 65 2c 20 26 79 79  arse(pParse, &yy
32ba2 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32ba3 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  0, &yymsp[-2].mi
32ba4 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
32ba5 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
32ba6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32ba7 20 20 20 20 20 20 63 61 73 65 20 33 32 31 3a 20        case 321: 
32ba8 2f 2a 20 76 74 61 62 61 72 67 20 3a 3a 3d 20 2a  /* vtabarg ::= *
32ba9 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72  /.{sqlite3VtabAr
32baa 67 49 6e 69 74 28 70 50 61 72 73 65 29 3b 7d 0a  gInit(pParse);}.
32bab 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32bac 20 20 20 20 20 63 61 73 65 20 33 32 33 3a 20 2f       case 323: /
32bad 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a  * vtabargtoken :
32bae 3a 3d 20 41 4e 59 20 2a 2f 0a 20 20 20 20 20 20  := ANY */.      
32baf 63 61 73 65 20 33 32 34 3a 20 2f 2a 20 76 74 61  case 324: /* vta
32bb0 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70  bargtoken ::= lp
32bb1 20 61 6e 79 6c 69 73 74 20 52 50 20 2a 2f 20 79   anylist RP */ y
32bb2 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32bb3 6e 6f 3d 3d 33 32 34 29 3b 0a 20 20 20 20 20 20  no==324);.      
32bb4 63 61 73 65 20 33 32 35 3a 20 2f 2a 20 6c 70 20  case 325: /* lp 
32bb5 3a 3a 3d 20 4c 50 20 2a 2f 20 79 79 74 65 73 74  ::= LP */ yytest
32bb6 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32bb7 32 35 29 3b 0a 7b 73 71 6c 69 74 65 33 56 74 61  25);.{sqlite3Vta
32bb8 62 41 72 67 45 78 74 65 6e 64 28 70 50 61 72 73  bArgExtend(pPars
32bb9 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e,&yymsp[0].mino
32bba 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
32bbb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65   break;.      de
32bbc 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 2f 2a 20  fault:.      /* 
32bbd 28 30 29 20 69 6e 70 75 74 20 3a 3a 3d 20 63 6d  (0) input ::= cm
32bbe 64 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74 63  dlist */ yytestc
32bbf 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 30 29  ase(yyruleno==0)
32bc0 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 29 20 63  ;.      /* (1) c
32bc1 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
32bc2 73 74 20 65 63 6d 64 20 2a 2f 20 79 79 74 65 73  st ecmd */ yytes
32bc3 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32bc4 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 29  1);.      /* (2)
32bc5 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d   cmdlist ::= ecm
32bc6 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  d */ yytestcase(
32bc7 79 79 72 75 6c 65 6e 6f 3d 3d 32 29 3b 0a 20 20  yyruleno==2);.  
32bc8 20 20 20 20 2f 2a 20 28 33 29 20 65 63 6d 64 20      /* (3) ecmd 
32bc9 3a 3a 3d 20 53 45 4d 49 20 2a 2f 20 79 79 74 65  ::= SEMI */ yyte
32bca 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32bcb 3d 33 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34  =3);.      /* (4
32bcc 29 20 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61  ) ecmd ::= expla
32bcd 69 6e 20 63 6d 64 78 20 53 45 4d 49 20 2a 2f 20  in cmdx SEMI */ 
32bce 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32bcf 65 6e 6f 3d 3d 34 29 3b 0a 20 20 20 20 20 20 2f  eno==4);.      /
32bd0 2a 20 28 31 30 29 20 74 72 61 6e 73 5f 6f 70 74  * (10) trans_opt
32bd1 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
32bd2 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 29  se(yyruleno==10)
32bd3 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 31 29 20  ;.      /* (11) 
32bd4 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52  trans_opt ::= TR
32bd5 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 20 79 79 74  ANSACTION */ yyt
32bd6 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32bd7 3d 3d 31 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==11);.      /* 
32bd8 28 31 32 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a  (12) trans_opt :
32bd9 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e  := TRANSACTION n
32bda 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  m */ yytestcase(
32bdb 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 29 3b 0a 20  yyruleno==12);. 
32bdc 20 20 20 20 20 2f 2a 20 28 32 30 29 20 73 61 76       /* (20) sav
32bdd 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53  epoint_opt ::= S
32bde 41 56 45 50 4f 49 4e 54 20 2a 2f 20 79 79 74 65  AVEPOINT */ yyte
32bdf 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32be0 3d 32 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =20);.      /* (
32be1 32 31 29 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70  21) savepoint_op
32be2 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
32be3 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31  ase(yyruleno==21
32be4 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 35 29  );.      /* (25)
32be5 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f   cmd ::= create_
32be6 74 61 62 6c 65 20 63 72 65 61 74 65 5f 74 61 62  table create_tab
32be7 6c 65 5f 61 72 67 73 20 2a 2f 20 79 79 74 65 73  le_args */ yytes
32be8 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32be9 32 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  25);.      /* (3
32bea 34 29 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a  4) columnlist ::
32beb 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d  = columnlist COM
32bec 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74  MA column */ yyt
32bed 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32bee 3d 3d 33 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==34);.      /* 
32bef 28 33 35 29 20 63 6f 6c 75 6d 6e 6c 69 73 74 20  (35) columnlist 
32bf0 3a 3a 3d 20 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79  ::= column */ yy
32bf1 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32bf2 6f 3d 3d 33 35 29 3b 0a 20 20 20 20 20 20 2f 2a  o==35);.      /*
32bf3 20 28 34 34 29 20 74 79 70 65 20 3a 3a 3d 20 2a   (44) type ::= *
32bf4 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32bf5 75 6c 65 6e 6f 3d 3d 34 34 29 3b 0a 20 20 20 20  uleno==44);.    
32bf6 20 20 2f 2a 20 28 35 31 29 20 73 69 67 6e 65 64    /* (51) signed
32bf7 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f   ::= plus_num */
32bf8 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32bf9 6c 65 6e 6f 3d 3d 35 31 29 3b 0a 20 20 20 20 20  leno==51);.     
32bfa 20 2f 2a 20 28 35 32 29 20 73 69 67 6e 65 64 20   /* (52) signed 
32bfb 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f  ::= minus_num */
32bfc 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32bfd 6c 65 6e 6f 3d 3d 35 32 29 3b 0a 20 20 20 20 20  leno==52);.     
32bfe 20 2f 2a 20 28 35 33 29 20 63 61 72 67 6c 69 73   /* (53) carglis
32bff 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20 63  t ::= carglist c
32c00 61 72 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73  arg */ yytestcas
32c01 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 33 29 3b  e(yyruleno==53);
32c02 0a 20 20 20 20 20 20 2f 2a 20 28 35 34 29 20 63  .      /* (54) c
32c03 61 72 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79  arglist ::= */ y
32c04 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c05 6e 6f 3d 3d 35 34 29 3b 0a 20 20 20 20 20 20 2f  no==54);.      /
32c06 2a 20 28 35 35 29 20 63 61 72 67 20 3a 3a 3d 20  * (55) carg ::= 
32c07 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63 63  CONSTRAINT nm cc
32c08 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ons */ yytestcas
32c09 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 35 29 3b  e(yyruleno==55);
32c0a 0a 20 20 20 20 20 20 2f 2a 20 28 35 36 29 20 63  .      /* (56) c
32c0b 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f  arg ::= ccons */
32c0c 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32c0d 6c 65 6e 6f 3d 3d 35 36 29 3b 0a 20 20 20 20 20  leno==56);.     
32c0e 20 2f 2a 20 28 36 32 29 20 63 63 6f 6e 73 20 3a   /* (62) ccons :
32c0f 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a  := NULL onconf *
32c10 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32c11 75 6c 65 6e 6f 3d 3d 36 32 29 3b 0a 20 20 20 20  uleno==62);.    
32c12 20 20 2f 2a 20 28 38 39 29 20 63 6f 6e 73 6c 69    /* (89) consli
32c13 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20  st ::= conslist 
32c14 43 4f 4d 4d 41 20 74 63 6f 6e 73 20 2a 2f 20 79  COMMA tcons */ y
32c15 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c16 6e 6f 3d 3d 38 39 29 3b 0a 20 20 20 20 20 20 2f  no==89);.      /
32c17 2a 20 28 39 30 29 20 63 6f 6e 73 6c 69 73 74 20  * (90) conslist 
32c18 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f  ::= conslist tco
32c19 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ns */ yytestcase
32c1a 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 30 29 3b 0a  (yyruleno==90);.
32c1b 20 20 20 20 20 20 2f 2a 20 28 39 31 29 20 63 6f        /* (91) co
32c1c 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73  nslist ::= tcons
32c1d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32c1e 79 72 75 6c 65 6e 6f 3d 3d 39 31 29 3b 0a 20 20  yruleno==91);.  
32c1f 20 20 20 20 2f 2a 20 28 39 32 29 20 74 63 6f 6e      /* (92) tcon
32c20 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54  s ::= CONSTRAINT
32c21 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73   nm */ yytestcas
32c22 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 32 29 3b  e(yyruleno==92);
32c23 0a 20 20 20 20 20 20 2f 2a 20 28 32 36 38 29 20  .      /* (268) 
32c24 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55  plus_opt ::= PLU
32c25 53 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  S */ yytestcase(
32c26 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 38 29 3b 0a  yyruleno==268);.
32c27 20 20 20 20 20 20 2f 2a 20 28 32 36 39 29 20 70        /* (269) p
32c28 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  lus_opt ::= */ y
32c29 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c2a 6e 6f 3d 3d 32 36 39 29 3b 0a 20 20 20 20 20 20  no==269);.      
32c2b 2f 2a 20 28 32 37 39 29 20 66 6f 72 65 61 63 68  /* (279) foreach
32c2c 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 20 79  _clause ::= */ y
32c2d 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c2e 6e 6f 3d 3d 32 37 39 29 3b 0a 20 20 20 20 20 20  no==279);.      
32c2f 2f 2a 20 28 32 38 30 29 20 66 6f 72 65 61 63 68  /* (280) foreach
32c30 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20  _clause ::= FOR 
32c31 45 41 43 48 20 52 4f 57 20 2a 2f 20 79 79 74 65  EACH ROW */ yyte
32c32 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c33 3d 32 38 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =280);.      /* 
32c34 28 32 38 37 29 20 74 72 69 64 78 62 79 20 3a 3a  (287) tridxby ::
32c35 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32c36 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 37 29 3b 0a  yyruleno==287);.
32c37 20 20 20 20 20 20 2f 2a 20 28 33 30 35 29 20 64        /* (305) d
32c38 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a  atabase_kw_opt :
32c39 3a 3d 20 44 41 54 41 42 41 53 45 20 2a 2f 20 79  := DATABASE */ y
32c3a 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c3b 6e 6f 3d 3d 33 30 35 29 3b 0a 20 20 20 20 20 20  no==305);.      
32c3c 2f 2a 20 28 33 30 36 29 20 64 61 74 61 62 61 73  /* (306) databas
32c3d 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  e_kw_opt ::= */ 
32c3e 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32c3f 65 6e 6f 3d 3d 33 30 36 29 3b 0a 20 20 20 20 20  eno==306);.     
32c40 20 2f 2a 20 28 33 31 34 29 20 6b 77 63 6f 6c 75   /* (314) kwcolu
32c41 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  mn_opt ::= */ yy
32c42 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32c43 6f 3d 3d 33 31 34 29 3b 0a 20 20 20 20 20 20 2f  o==314);.      /
32c44 2a 20 28 33 31 35 29 20 6b 77 63 6f 6c 75 6d 6e  * (315) kwcolumn
32c45 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b  _opt ::= COLUMNK
32c46 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  W */ yytestcase(
32c47 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 35 29 3b 0a  yyruleno==315);.
32c48 20 20 20 20 20 20 2f 2a 20 28 33 31 39 29 20 76        /* (319) v
32c49 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
32c4a 74 61 62 61 72 67 20 2a 2f 20 79 79 74 65 73 74  tabarg */ yytest
32c4b 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32c4c 31 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  19);.      /* (3
32c4d 32 30 29 20 76 74 61 62 61 72 67 6c 69 73 74 20  20) vtabarglist 
32c4e 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74 20  ::= vtabarglist 
32c4f 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 20 2a 2f  COMMA vtabarg */
32c50 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32c51 6c 65 6e 6f 3d 3d 33 32 30 29 3b 0a 20 20 20 20  leno==320);.    
32c52 20 20 2f 2a 20 28 33 32 32 29 20 76 74 61 62 61    /* (322) vtaba
32c53 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76  rg ::= vtabarg v
32c54 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f 20 79  tabargtoken */ y
32c55 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c56 6e 6f 3d 3d 33 32 32 29 3b 0a 20 20 20 20 20 20  no==322);.      
32c57 2f 2a 20 28 33 32 36 29 20 61 6e 79 6c 69 73 74  /* (326) anylist
32c58 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
32c59 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 36  se(yyruleno==326
32c5a 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 37  );.      /* (327
32c5b 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e  ) anylist ::= an
32c5c 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74  ylist LP anylist
32c5d 20 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73   RP */ yytestcas
32c5e 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 37 29  e(yyruleno==327)
32c5f 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 38 29  ;.      /* (328)
32c60 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79   anylist ::= any
32c61 6c 69 73 74 20 41 4e 59 20 2a 2f 20 79 79 74 65  list ANY */ yyte
32c62 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c63 3d 33 32 38 29 3b 0a 20 20 20 20 20 20 20 20 62  =328);.        b
32c64 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79 67  reak;.  };.  yyg
32c65 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f  oto = yyRuleInfo
32c66 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a  [yyruleno].lhs;.
32c67 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75 6c    yysize = yyRul
32c68 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e  eInfo[yyruleno].
32c69 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73 65  nrhs;.  yypParse
32c6a 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73 69  r->yyidx -= yysi
32c6b 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79 79  ze;.  yyact = yy
32c6c 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74  _find_reduce_act
32c6d 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a  ion(yymsp[-yysiz
32c6e 65 5d 2e 73 74 61 74 65 6e 6f 2c 28 59 59 43 4f  e].stateno,(YYCO
32c6f 44 45 54 59 50 45 29 79 79 67 6f 74 6f 29 3b 0a  DETYPE)yygoto);.
32c70 20 20 69 66 28 20 79 79 61 63 74 20 3c 20 59 59    if( yyact < YY
32c71 4e 53 54 41 54 45 20 29 7b 0a 23 69 66 64 65 66  NSTATE ){.#ifdef
32c72 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 49   NDEBUG.    /* I
32c73 66 20 77 65 20 61 72 65 20 6e 6f 74 20 64 65 62  f we are not deb
32c74 75 67 67 69 6e 67 20 61 6e 64 20 74 68 65 20 72  ugging and the r
32c75 65 64 75 63 65 20 61 63 74 69 6f 6e 20 70 6f 70  educe action pop
32c76 70 65 64 20 61 74 20 6c 65 61 73 74 0a 20 20 20  ped at least.   
32c77 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20   ** one element 
32c78 6f 66 66 20 74 68 65 20 73 74 61 63 6b 2c 20 74  off the stack, t
32c79 68 65 6e 20 77 65 20 63 61 6e 20 70 75 73 68 20  hen we can push 
32c7a 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  the new element 
32c7b 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6f 6e 74 6f  back.    ** onto
32c7c 20 74 68 65 20 73 74 61 63 6b 20 68 65 72 65 2c   the stack here,
32c7d 20 61 6e 64 20 73 6b 69 70 20 74 68 65 20 73 74   and skip the st
32c7e 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 74 65 73  ack overflow tes
32c7f 74 20 69 6e 20 79 79 5f 73 68 69 66 74 28 29 2e  t in yy_shift().
32c80 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 67 69 76  .    ** That giv
32c81 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74  es a significant
32c82 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65   speed improveme
32c83 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 79  nt. */.    if( y
32c84 79 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 79  ysize ){.      y
32c85 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2b  ypParser->yyidx+
32c86 2b 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 20 2d  +;.      yymsp -
32c87 3d 20 79 79 73 69 7a 65 2d 31 3b 0a 20 20 20 20  = yysize-1;.    
32c88 20 20 79 79 6d 73 70 2d 3e 73 74 61 74 65 6e 6f    yymsp->stateno
32c89 20 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45   = (YYACTIONTYPE
32c8a 29 79 79 61 63 74 3b 0a 20 20 20 20 20 20 79 79  )yyact;.      yy
32c8b 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59  msp->major = (YY
32c8c 43 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f 3b  CODETYPE)yygoto;
32c8d 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 69  .      yymsp->mi
32c8e 6e 6f 72 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f  nor = yygotomino
32c8f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  r;.    }else.#en
32c90 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  dif.    {.      
32c91 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73  yy_shift(yypPars
32c92 65 72 2c 79 79 61 63 74 2c 79 79 67 6f 74 6f 2c  er,yyact,yygoto,
32c93 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 3b 0a 20  &yygotominor);. 
32c94 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
32c95 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74 20    assert( yyact 
32c96 3d 3d 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59  == YYNSTATE + YY
32c97 4e 52 55 4c 45 20 2b 20 31 20 29 3b 0a 20 20 20  NRULE + 1 );.   
32c98 20 79 79 5f 61 63 63 65 70 74 28 79 79 70 50 61   yy_accept(yypPa
32c99 72 73 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rser);.  }.}../*
32c9a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
32c9b 67 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 20  g code executes 
32c9c 77 68 65 6e 20 74 68 65 20 70 61 72 73 65 20 66  when the parse f
32c9d 61 69 6c 73 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ails.*/.#ifndef 
32c9e 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52  YYNOERRORRECOVER
32c9f 59 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  Y.static void yy
32ca0 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20  _parse_failed(. 
32ca1 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
32ca2 72 73 65 72 20 20 20 20 20 20 20 20 20 20 20 2f  rser           /
32ca3 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
32ca4 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  ){.  sqlite3Pars
32ca5 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66  erARG_FETCH;.#if
32ca6 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66  ndef NDEBUG.  if
32ca7 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
32ca8 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
32ca9 72 61 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c  raceFILE,"%sFail
32caa 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
32cab 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  pt);.  }.#endif.
32cac 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73    while( yypPars
32cad 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
32cae 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
32caf 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
32cb0 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73   /* Here code is
32cb1 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20   inserted which 
32cb2 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64  will be executed
32cb3 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20   whenever the.  
32cb4 2a 2a 20 70 61 72 73 65 72 20 66 61 69 6c 73 20  ** parser fails 
32cb5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
32cb6 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20  erARG_STORE; /* 
32cb7 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67  Suppress warning
32cb8 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65   about unused %e
32cb9 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61  xtra_argument va
32cba 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 23 65 6e 64  riable */.}.#end
32cbb 69 66 20 2f 2a 20 59 59 4e 4f 45 52 52 4f 52 52  if /* YYNOERRORR
32cbc 45 43 4f 56 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a  ECOVERY */../*.*
32cbd 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
32cbe 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68  code executes wh
32cbf 65 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f  en a syntax erro
32cc0 72 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a  r first occurs..
32cc1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
32cc2 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a  y_syntax_error(.
32cc3 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
32cc4 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 20  arser,          
32cc5 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
32cc6 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c  /.  int yymajor,
32cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cc8 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20     /* The major 
32cc9 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f  type of the erro
32cca 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d  r token */.  YYM
32ccb 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72  INORTYPE yyminor
32ccc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
32ccd 68 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66  he minor type of
32cce 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e   the error token
32ccf 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
32cd0 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b  ParserARG_FETCH;
32cd1 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28  .#define TOKEN (
32cd2 79 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20  yyminor.yy0)..  
32cd3 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
32cd4 28 79 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53  (yymajor);  /* S
32cd5 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70  ilence some comp
32cd6 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f  iler warnings */
32cd7 0a 20 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e  .  assert( TOKEN
32cd8 2e 7a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65  .z[0] );  /* The
32cd9 20 74 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79   tokenizer alway
32cda 73 20 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b  s gives us a tok
32cdb 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  en */.  sqlite3E
32cdc 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
32cdd 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79  "near \"%T\": sy
32cde 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f  ntax error", &TO
32cdf 4b 45 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  KEN);.  pParse->
32ce0 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a  parseError = 1;.
32ce1 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
32ce2 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
32ce3 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
32ce4 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
32ce5 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61  a_argument varia
32ce6 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ble */.}../*.** 
32ce7 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  The following is
32ce8 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 20 74   executed when t
32ce9 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74  he parser accept
32cea 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
32ceb 20 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79   yy_accept(.  yy
32cec 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
32ced 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r           /* T
32cee 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a  he parser */.){.
32cef 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
32cf0 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65  RG_FETCH;.#ifnde
32cf1 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
32cf2 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
32cf3 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
32cf4 65 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21  eFILE,"%sAccept!
32cf5 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
32cf6 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  t);.  }.#endif. 
32cf7 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65   while( yypParse
32cf8 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79  r->yyidx>=0 ) yy
32cf9 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
32cfa 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  k(yypParser);.  
32cfb 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20  /* Here code is 
32cfc 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77  inserted which w
32cfd 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20  ill be executed 
32cfe 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a  whenever the.  *
32cff 2a 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  * parser accepts
32d00 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
32d01 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a  serARG_STORE; /*
32d02 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   Suppress warnin
32d03 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25  g about unused %
32d04 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76  extra_argument v
32d05 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a  ariable */.}../*
32d06 20 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72   The main parser
32d07 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65   program..** The
32d08 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
32d09 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
32d0a 61 20 73 74 72 75 63 74 75 72 65 20 6f 62 74 61  a structure obta
32d0b 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71  ined from.** "sq
32d0c 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
32d0d 22 20 77 68 69 63 68 20 64 65 73 63 72 69 62 65  " which describe
32d0e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  s the current st
32d0f 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65  ate of the parse
32d10 72 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  r..** The second
32d11 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
32d12 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d   major token num
32d13 62 65 72 2e 20 20 54 68 65 20 74 68 69 72 64 20  ber.  The third 
32d14 69 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20  is.** the minor 
32d15 74 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72  token.  The four
32d16 74 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75  th optional argu
32d17 6d 65 6e 74 20 69 73 20 77 68 61 74 65 76 65 72  ment is whatever
32d18 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e   the.** user wan
32d19 74 73 20 28 61 6e 64 20 73 70 65 63 69 66 69 65  ts (and specifie
32d1a 64 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  d in the grammar
32d1b 29 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62  ) and is availab
32d1c 6c 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79  le for.** use by
32d1d 20 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74   the action rout
32d1e 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75  ines..**.** Inpu
32d1f 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  ts:.** <ul>.** <
32d20 6c 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  li> A pointer to
32d21 20 74 68 65 20 70 61 72 73 65 72 20 28 61 6e 20   the parser (an 
32d22 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65  opaque structure
32d23 2e 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d  .).** <li> The m
32d24 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65  ajor token numbe
32d25 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d  r..** <li> The m
32d26 69 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65  inor token numbe
32d27 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70  r..** <li> An op
32d28 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66  tion argument of
32d29 20 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69   a grammar-speci
32d2a 66 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f  fied type..** </
32d2b 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74  ul>.**.** Output
32d2c 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53  s:.** None..*/.S
32d2d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
32d2e 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  id sqlite3Parser
32d2f 28 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20  (.  void *yyp,  
32d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d31 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
32d32 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c  /.  int yymajor,
32d33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d34 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f   /* The major to
32d35 6b 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20  ken code number 
32d36 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
32d37 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69  erTOKENTYPE yymi
32d38 6e 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65  nor       /* The
32d39 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74   value for the t
32d3a 6f 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oken */.  sqlite
32d3b 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c  3ParserARG_PDECL
32d3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32d3d 2a 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72  * Optional %extr
32d3e 61 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d  a_argument param
32d3f 65 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d  eter */.){.  YYM
32d40 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72  INORTYPE yyminor
32d41 75 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61  union;.  int yya
32d42 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ct;            /
32d43 2a 20 54 68 65 20 70 61 72 73 65 72 20 61 63 74  * The parser act
32d44 69 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79  ion. */.  int yy
32d45 65 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20  endofinput;     
32d46 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72  /* True if we ar
32d47 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
32d48 69 6e 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20  input */.#ifdef 
32d49 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
32d4a 69 6e 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d  int yyerrorhit =
32d4b 20 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66   0;   /* True if
32d4c 20 79 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76   yymajor has inv
32d4d 6f 6b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f  oked an error */
32d4e 0a 23 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73  .#endif.  yyPars
32d4f 65 72 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20  er *yypParser;  
32d50 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
32d51 0a 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69  ..  /* (re)initi
32d52 61 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65 72  alize the parser
32d53 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a  , if necessary *
32d54 2f 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d 20  /.  yypParser = 
32d55 28 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a  (yyParser*)yyp;.
32d56 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
32d57 3e 79 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20  >yyidx<0 ){.#if 
32d58 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a  YYSTACKDEPTH<=0.
32d59 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
32d5a 72 2d 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29  r->yystksz <=0 )
32d5b 7b 0a 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74  {.      /*memset
32d5c 28 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20  (&yyminorunion, 
32d5d 30 2c 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f  0, sizeof(yymino
32d5e 72 75 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20  runion));*/.    
32d5f 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d    yyminorunion =
32d60 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20   yyzerominor;.  
32d61 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66      yyStackOverf
32d62 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 26  low(yypParser, &
32d63 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
32d64 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
32d65 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79   }.#endif.    yy
32d66 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d  pParser->yyidx =
32d67 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65   0;.    yypParse
32d68 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31  r->yyerrcnt = -1
32d69 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  ;.    yypParser-
32d6a 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74  >yystack[0].stat
32d6b 65 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70  eno = 0;.    yyp
32d6c 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
32d6d 30 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20  0].major = 0;.  
32d6e 7d 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  }.  yyminorunion
32d6f 2e 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a  .yy0 = yyminor;.
32d70 20 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d    yyendofinput =
32d71 20 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20   (yymajor==0);. 
32d72 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
32d73 47 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65  G_STORE;..#ifnde
32d74 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
32d75 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
32d76 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
32d77 65 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25  eFILE,"%sInput %
32d78 73 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  s\n",yyTraceProm
32d79 70 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79  pt,yyTokenName[y
32d7a 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65  ymajor]);.  }.#e
32d7b 6e 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20  ndif..  do{.    
32d7c 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f  yyact = yy_find_
32d7d 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70  shift_action(yyp
32d7e 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59  Parser,(YYCODETY
32d7f 50 45 29 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20  PE)yymajor);.   
32d80 20 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53 54   if( yyact<YYNST
32d81 41 54 45 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ATE ){.      ass
32d82 65 72 74 28 20 21 79 79 65 6e 64 6f 66 69 6e 70  ert( !yyendofinp
32d83 75 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73  ut );  /* Imposs
32d84 69 62 6c 65 20 74 6f 20 73 68 69 66 74 20 74 68  ible to shift th
32d85 65 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  e $ token */.   
32d86 20 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50     yy_shift(yypP
32d87 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79 6d 61  arser,yyact,yyma
32d88 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
32d89 6e 29 3b 0a 20 20 20 20 20 20 79 79 70 50 61 72  n);.      yypPar
32d8a 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b  ser->yyerrcnt--;
32d8b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d  .      yymajor =
32d8c 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d   YYNOCODE;.    }
32d8d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74 20 3c  else if( yyact <
32d8e 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52   YYNSTATE + YYNR
32d8f 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79 79 5f  ULE ){.      yy_
32d90 72 65 64 75 63 65 28 79 79 70 50 61 72 73 65 72  reduce(yypParser
32d91 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45 29  ,yyact-YYNSTATE)
32d92 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
32d93 20 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74     assert( yyact
32d94 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43 54   == YY_ERROR_ACT
32d95 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20 59 59  ION );.#ifdef YY
32d96 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20  ERRORSYMBOL.    
32d97 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64    int yymx;.#end
32d98 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  if.#ifndef NDEBU
32d99 47 0a 20 20 20 20 20 20 69 66 28 20 79 79 54 72  G.      if( yyTr
32d9a 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
32d9b 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
32d9c 63 65 46 49 4c 45 2c 22 25 73 53 79 6e 74 61 78  ceFILE,"%sSyntax
32d9d 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61   Error!\n",yyTra
32d9e 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20  cePrompt);.     
32d9f 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66   }.#endif.#ifdef
32da0 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
32da1 20 20 20 20 20 2f 2a 20 41 20 73 79 6e 74 61 78       /* A syntax
32da2 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
32da3 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68  red..      ** Th
32da4 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e  e response to an
32da5 20 65 72 72 6f 72 20 64 65 70 65 6e 64 73 20 75   error depends u
32da6 70 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e  pon whether or n
32da7 6f 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ot the.      ** 
32da8 67 72 61 6d 6d 61 72 20 64 65 66 69 6e 65 73 20  grammar defines 
32da9 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22  an error token "
32daa 45 52 52 4f 52 22 2e 20 20 0a 20 20 20 20 20 20  ERROR".  .      
32dab 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  **.      ** This
32dac 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 20 69   is what we do i
32dad 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64 6f  f the grammar do
32dae 65 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a  es define ERROR:
32daf 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
32db0 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20 25  **  * Call the %
32db1 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75 6e  syntax_error fun
32db2 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a  ction..      **.
32db3 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67 69        **  * Begi
32db4 6e 20 70 6f 70 70 69 6e 67 20 74 68 65 20 73 74  n popping the st
32db5 61 63 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e 74  ack until we ent
32db6 65 72 20 61 20 73 74 61 74 65 20 77 68 65 72 65  er a state where
32db7 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 69 74 20  .      **    it 
32db8 69 73 20 6c 65 67 61 6c 20 74 6f 20 73 68 69 66  is legal to shif
32db9 74 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62  t the error symb
32dba 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66 74 0a 20  ol, then shift. 
32dbb 20 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20 65       **    the e
32dbc 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20  rror symbol..   
32dbd 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
32dbe 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20  * Set the error 
32dbf 63 6f 75 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a  count to three..
32dc0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
32dc1 2a 20 20 2a 20 42 65 67 69 6e 20 61 63 63 65 70  *  * Begin accep
32dc2 74 69 6e 67 20 61 6e 64 20 73 68 69 66 74 69 6e  ting and shiftin
32dc3 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e  g new tokens.  N
32dc4 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20 20 20  o new error.    
32dc5 20 20 2a 2a 20 20 20 20 70 72 6f 63 65 73 73 69    **    processi
32dc6 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e  ng will occur un
32dc7 74 69 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e 73  til three tokens
32dc8 20 68 61 76 65 20 62 65 65 6e 0a 20 20 20 20 20   have been.     
32dc9 20 2a 2a 20 20 20 20 73 68 69 66 74 65 64 20 73   **    shifted s
32dca 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20  uccessfully..   
32dcb 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20     **.      */. 
32dcc 20 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73       if( yypPars
32dcd 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20 29  er->yyerrcnt<0 )
32dce 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e  {.        yy_syn
32dcf 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72  tax_error(yypPar
32dd0 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69  ser,yymajor,yymi
32dd1 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
32dd2 20 7d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d 20   }.      yymx = 
32dd3 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
32dd4 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79  ck[yypParser->yy
32dd5 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20  idx].major;.    
32dd6 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59 45 52    if( yymx==YYER
32dd7 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65  RORSYMBOL || yye
32dd8 72 72 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e 64  rrorhit ){.#ifnd
32dd9 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20  ef NDEBUG.      
32dda 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
32ddb 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  E ){.          f
32ddc 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
32ddd 4c 45 2c 22 25 73 44 69 73 63 61 72 64 20 69 6e  LE,"%sDiscard in
32dde 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c  put token %s\n",
32ddf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79  .             yy
32de0 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f  TracePrompt,yyTo
32de1 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d  kenName[yymajor]
32de2 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  );.        }.#en
32de3 64 69 66 0a 20 20 20 20 20 20 20 20 79 79 5f 64  dif.        yy_d
32de4 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
32de5 73 65 72 2c 20 28 59 59 43 4f 44 45 54 59 50 45  ser, (YYCODETYPE
32de6 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  )yymajor,&yymino
32de7 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20  runion);.       
32de8 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
32de9 4f 44 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ODE;.      }else
32dea 7b 0a 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  {.         while
32deb 28 0a 20 20 20 20 20 20 20 20 20 20 79 79 70 50  (.          yypP
32dec 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20  arser->yyidx >= 
32ded 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 79  0 &&.          y
32dee 79 6d 78 20 21 3d 20 59 59 45 52 52 4f 52 53 59  ymx != YYERRORSY
32def 4d 42 4f 4c 20 26 26 0a 20 20 20 20 20 20 20 20  MBOL &&.        
32df0 20 20 28 79 79 61 63 74 20 3d 20 79 79 5f 66 69    (yyact = yy_fi
32df1 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e  nd_reduce_action
32df2 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
32df3 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72            yypPar
32df4 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70  ser->yystack[yyp
32df5 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73  Parser->yyidx].s
32df6 74 61 74 65 6e 6f 2c 0a 20 20 20 20 20 20 20 20  tateno,.        
32df7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32df8 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29 20  YYERRORSYMBOL)) 
32df9 3e 3d 20 59 59 4e 53 54 41 54 45 0a 20 20 20 20  >= YYNSTATE.    
32dfa 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
32dfb 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73   yy_pop_parser_s
32dfc 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b  tack(yypParser);
32dfd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
32dfe 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
32dff 2d 3e 79 79 69 64 78 20 3c 20 30 20 7c 7c 20 79  ->yyidx < 0 || y
32e00 79 6d 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  ymajor==0 ){.   
32e01 20 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75         yy_destru
32e02 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28  ctor(yypParser,(
32e03 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
32e04 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
32e05 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  );.          yy_
32e06 70 61 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70  parse_failed(yyp
32e07 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20  Parser);.       
32e08 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
32e09 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 20 20 7d  OCODE;.        }
32e0a 65 6c 73 65 20 69 66 28 20 79 79 6d 78 21 3d 59  else if( yymx!=Y
32e0b 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a  YERRORSYMBOL ){.
32e0c 20 20 20 20 20 20 20 20 20 20 59 59 4d 49 4e 4f            YYMINO
32e0d 52 54 59 50 45 20 75 32 3b 0a 20 20 20 20 20 20  RTYPE u2;.      
32e0e 20 20 20 20 75 32 2e 59 59 45 52 52 53 59 4d 44      u2.YYERRSYMD
32e0f 54 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  T = 0;.         
32e10 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72   yy_shift(yypPar
32e11 73 65 72 2c 79 79 61 63 74 2c 59 59 45 52 52 4f  ser,yyact,YYERRO
32e12 52 53 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20  RSYMBOL,&u2);.  
32e13 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
32e14 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
32e15 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20  >yyerrcnt = 3;. 
32e16 20 20 20 20 20 79 79 65 72 72 6f 72 68 69 74 20       yyerrorhit 
32e17 3d 20 31 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e  = 1;.#elif defin
32e18 65 64 28 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f  ed(YYNOERRORRECO
32e19 56 45 52 59 29 0a 20 20 20 20 20 20 2f 2a 20 49  VERY).      /* I
32e1a 66 20 74 68 65 20 59 59 4e 4f 45 52 52 4f 52 52  f the YYNOERRORR
32e1b 45 43 4f 56 45 52 59 20 6d 61 63 72 6f 20 69 73  ECOVERY macro is
32e1c 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 64   defined, then d
32e1d 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  o not attempt to
32e1e 0a 20 20 20 20 20 20 2a 2a 20 64 6f 20 61 6e 79  .      ** do any
32e1f 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 72   kind of error r
32e20 65 63 6f 76 65 72 79 2e 20 20 49 6e 73 74 65 61  ecovery.  Instea
32e21 64 2c 20 73 69 6d 70 6c 79 20 69 6e 76 6f 6b 65  d, simply invoke
32e22 20 74 68 65 20 73 79 6e 74 61 78 0a 20 20 20 20   the syntax.    
32e23 20 20 2a 2a 20 65 72 72 6f 72 20 72 6f 75 74 69    ** error routi
32e24 6e 65 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20  ne and continue 
32e25 67 6f 69 6e 67 20 61 73 20 69 66 20 6e 6f 74 68  going as if noth
32e26 69 6e 67 20 68 61 64 20 68 61 70 70 65 6e 65 64  ing had happened
32e27 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
32e28 20 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73   ** Applications
32e29 20 63 61 6e 20 73 65 74 20 74 68 69 73 20 6d 61   can set this ma
32e2a 63 72 6f 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  cro (for example
32e2b 20 69 6e 73 69 64 65 20 25 69 6e 63 6c 75 64 65   inside %include
32e2c 29 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 74 68  ) if.      ** th
32e2d 65 79 20 69 6e 74 65 6e 64 20 74 6f 20 61 62 61  ey intend to aba
32e2e 6e 64 6f 6e 20 74 68 65 20 70 61 72 73 65 20 75  ndon the parse u
32e2f 70 6f 6e 20 74 68 65 20 66 69 72 73 74 20 73 79  pon the first sy
32e30 6e 74 61 78 20 65 72 72 6f 72 20 73 65 65 6e 2e  ntax error seen.
32e31 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
32e32 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28  yy_syntax_error(
32e33 79 79 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f  yypParser,yymajo
32e34 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  r,yyminorunion);
32e35 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75  .      yy_destru
32e36 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28  ctor(yypParser,(
32e37 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
32e38 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
32e39 29 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72  );.      yymajor
32e3a 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
32e3b 20 20 20 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59     .#else  /* YY
32e3c 45 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e  ERRORSYMBOL is n
32e3d 6f 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20  ot defined */.  
32e3e 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 77      /* This is w
32e3f 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65  hat we do if the
32e40 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f   grammar does no
32e41 74 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a  t define ERROR:.
32e42 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
32e43 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20 65  *  * Report an e
32e44 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61 6e  rror message, an
32e45 64 20 74 68 72 6f 77 20 61 77 61 79 20 74 68 65  d throw away the
32e46 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20   input token..  
32e47 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
32e48 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20   * If the input 
32e49 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65 6e  token is $, then
32e4a 20 66 61 69 6c 20 74 68 65 20 70 61 72 73 65 2e   fail the parse.
32e4b 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
32e4c 2a 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73 75  ** As before, su
32e4d 62 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20 6d  bsequent error m
32e4e 65 73 73 61 67 65 73 20 61 72 65 20 73 75 70 70  essages are supp
32e4f 72 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20 20  ressed until.   
32e50 20 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70 75     ** three inpu
32e51 74 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62 65  t tokens have be
32e52 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  en successfully 
32e53 73 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20 2a  shifted..      *
32e54 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50  /.      if( yypP
32e55 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c  arser->yyerrcnt<
32e56 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79  =0 ){.        yy
32e57 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79  _syntax_error(yy
32e58 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c  pParser,yymajor,
32e59 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
32e5a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 70       }.      yyp
32e5b 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
32e5c 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f 64   = 3;.      yy_d
32e5d 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61 72  estructor(yypPar
32e5e 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
32e5f 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
32e60 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66  union);.      if
32e61 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29  ( yyendofinput )
32e62 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72  {.        yy_par
32e63 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
32e64 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ser);.      }.  
32e65 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
32e66 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20  NOCODE;.#endif. 
32e67 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79     }.  }while( y
32e68 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45  ymajor!=YYNOCODE
32e69 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
32e6a 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74  yidx>=0 );.  ret
32e6b 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  urn;.}../*******
32e6c 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
32e6d 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.c *********
32e6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e70 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
32e71 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
32e72 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  le tokenize.c **
32e73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e75 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
32e76 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
32e77 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
32e78 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
32e79 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
32e7a 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
32e7b 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
32e7c 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
32e7d 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
32e7e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
32e7f 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
32e80 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
32e81 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
32e82 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
32e83 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
32e84 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
32e85 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
32e86 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
32e87 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
32e88 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
32e89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
32e8d 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
32e8e 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
32e8f 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
32e90 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69  C code that spli
32e91 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20  ts an SQL input 
32e92 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a  string up into.*
32e93 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b  * individual tok
32e94 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68  ens and sends th
32e95 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62  ose tokens one-b
32e96 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68  y-one over to th
32e97 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20  e.** parser for 
32e98 61 6e 61 6c 79 73 69 73 2e 0a 2a 2f 0a 0a 2f 2a  analysis..*/../*
32e99 0a 2a 2a 20 54 68 65 20 63 68 61 72 4d 61 70 28  .** The charMap(
32e9a 29 20 6d 61 63 72 6f 20 6d 61 70 73 20 61 6c 70  ) macro maps alp
32e9b 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65  habetic characte
32e9c 72 73 20 69 6e 74 6f 20 74 68 65 69 72 0a 2a 2a  rs into their.**
32e9d 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49   lower-case ASCI
32e9e 49 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 4f  I equivalent.  O
32e9f 6e 20 41 53 43 49 49 20 6d 61 63 68 69 6e 65 73  n ASCII machines
32ea0 2c 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 2a  , this is just.*
32ea1 2a 20 61 6e 20 75 70 70 65 72 2d 74 6f 2d 6c 6f  * an upper-to-lo
32ea2 77 65 72 20 63 61 73 65 20 6d 61 70 2e 20 20 4f  wer case map.  O
32ea3 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65  n EBCDIC machine
32ea4 73 20 77 65 20 61 6c 73 6f 20 6e 65 65 64 0a 2a  s we also need.*
32ea5 2a 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20  * to adjust the 
32ea6 65 6e 63 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20  encoding.  Only 
32ea7 61 6c 70 68 61 62 65 74 69 63 20 63 68 61 72 61  alphabetic chara
32ea8 63 74 65 72 73 20 61 6e 64 20 75 6e 64 65 72 73  cters and unders
32ea9 63 6f 72 65 73 0a 2a 2a 20 6e 65 65 64 20 74 6f  cores.** need to
32eaa 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 2e 0a   be translated..
32eab 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
32eac 5f 41 53 43 49 49 0a 23 20 64 65 66 69 6e 65 20  _ASCII.# define 
32ead 63 68 61 72 4d 61 70 28 58 29 20 73 71 6c 69 74  charMap(X) sqlit
32eae 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
32eaf 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d  unsigned char)X]
32eb0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
32eb1 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 23 20 64  QLITE_EBCDIC.# d
32eb2 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28 58 29  efine charMap(X)
32eb3 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 28   ebcdicToAscii[(
32eb4 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 58 5d  unsigned char)X]
32eb5 0a 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  .const unsigned 
32eb6 63 68 61 72 20 65 62 63 64 69 63 54 6f 41 73 63  char ebcdicToAsc
32eb7 69 69 5b 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20  ii[] = {./* 0   
32eb8 31 20 20 20 32 20 20 20 33 20 20 20 34 20 20 20  1   2   3   4   
32eb9 35 20 20 20 36 20 20 20 37 20 20 20 38 20 20 20  5   6   7   8   
32eba 39 20 20 20 41 20 20 20 42 20 20 20 43 20 20 20  9   A   B   C   
32ebb 44 20 20 20 45 20 20 20 46 20 2a 2f 0a 20 20 20  D   E   F */.   
32ebc 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ebd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ebe 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ebf 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec0 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 0x */.   0,  
32ec1 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec3 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec4 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31  0,  0,  0,  /* 1
32ec5 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
32ec6 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec7 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec8 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ec9 30 2c 20 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f  0,  0,  /* 2x */
32eca 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
32ecb 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ecc 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ecd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ece 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  0,  /* 3x */.   
32ecf 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed0 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed1 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed3 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 4x */.   0,  
32ed4 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed5 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed6 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ed7 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 35  0,  0,  0,  /* 5
32ed8 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
32ed9 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32eda 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32edb 30 2c 20 20 30 2c 20 20 30 2c 20 39 35 2c 20 20  0,  0,  0, 95,  
32edc 30 2c 20 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f  0,  0,  /* 6x */
32edd 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
32ede 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32edf 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ee0 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ee1 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20  0,  /* 7x */.   
32ee2 30 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30  0, 97, 98, 99,10
32ee3 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30  0,101,102,103,10
32ee4 34 2c 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20  4,105,  0,  0,  
32ee5 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ee6 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 30 2c 31 30  /* 8x */.   0,10
32ee7 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31  6,107,108,109,11
32ee8 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31  0,111,112,113,11
32ee9 34 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  4,  0,  0,  0,  
32eea 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 39  0,  0,  0,  /* 9
32eeb 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31  x */.   0,  0,11
32eec 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31  5,116,117,118,11
32eed 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20  9,120,121,122,  
32eee 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32eef 30 2c 20 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f  0,  0,  /* Ax */
32ef0 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
32ef1 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ef2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ef3 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ef4 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20  0,  /* Bx */.   
32ef5 30 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30  0, 97, 98, 99,10
32ef6 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30  0,101,102,103,10
32ef7 34 2c 31 30 35 2c 20 20 30 2c 20 20 30 2c 20 20  4,105,  0,  0,  
32ef8 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32ef9 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 30 2c 31 30  /* Cx */.   0,10
32efa 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31  6,107,108,109,11
32efb 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31  0,111,112,113,11
32efc 34 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  4,  0,  0,  0,  
32efd 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 44  0,  0,  0,  /* D
32efe 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31  x */.   0,  0,11
32eff 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31  5,116,117,118,11
32f00 39 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 20 20  9,120,121,122,  
32f01 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32f02 30 2c 20 20 30 2c 20 20 2f 2a 20 45 78 20 2a 2f  0,  0,  /* Ex */
32f03 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
32f04 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32f05 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32f06 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
32f07 30 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a  0,  /* Fx */.};.
32f08 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
32f09 65 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  e sqlite3Keyword
32f0a 43 6f 64 65 20 66 75 6e 63 74 69 6f 6e 20 6c 6f  Code function lo
32f0b 6f 6b 73 20 75 70 20 61 6e 20 69 64 65 6e 74 69  oks up an identi
32f0c 66 69 65 72 20 74 6f 20 64 65 74 65 72 6d 69 6e  fier to determin
32f0d 65 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 61 20  e if.** it is a 
32f0e 6b 65 79 77 6f 72 64 2e 20 20 49 66 20 69 74 20  keyword.  If it 
32f0f 69 73 20 61 20 6b 65 79 77 6f 72 64 2c 20 74 68  is a keyword, th
32f10 65 20 74 6f 6b 65 6e 20 63 6f 64 65 20 6f 66 20  e token code of 
32f11 74 68 61 74 20 6b 65 79 77 6f 72 64 20 69 73 20  that keyword is 
32f12 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49  .** returned.  I
32f13 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 6e  f the input is n
32f14 6f 74 20 61 20 6b 65 79 77 6f 72 64 2c 20 54 4b  ot a keyword, TK
32f15 5f 49 44 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _ID is returned.
32f16 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
32f17 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
32f18 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 67 65  s routine was ge
32f19 6e 65 72 61 74 65 64 20 62 79 20 61 20 70 72 6f  nerated by a pro
32f1a 67 72 61 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f  gram,.** mkkeywo
32f1b 72 64 68 61 73 68 2e 68 2c 20 6c 6f 63 61 74 65  rdhash.h, locate
32f1c 64 20 69 6e 20 74 68 65 20 74 6f 6f 6c 20 73 75  d in the tool su
32f1d 62 64 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68  bdirectory of th
32f1e 65 20 64 69 73 74 72 69 62 75 74 69 6f 6e 2e 0a  e distribution..
32f1f 2a 2a 20 54 68 65 20 6f 75 74 70 75 74 20 6f 66  ** The output of
32f20 20 74 68 65 20 6d 6b 6b 65 79 77 6f 72 64 68 61   the mkkeywordha
32f21 73 68 2e 63 20 70 72 6f 67 72 61 6d 20 69 73 20  sh.c program is 
32f22 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 66  written into a f
32f23 69 6c 65 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65 79  ile.** named key
32f24 77 6f 72 64 68 61 73 68 2e 68 20 61 6e 64 20 74  wordhash.h and t
32f25 68 65 6e 20 69 6e 63 6c 75 64 65 64 20 69 6e 74  hen included int
32f26 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 66 69  o this source fi
32f27 6c 65 20 62 79 0a 2a 2a 20 74 68 65 20 23 69 6e  le by.** the #in
32f28 63 6c 75 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a  clude below..*/.
32f29 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
32f2a 49 6e 63 6c 75 64 65 20 6b 65 79 77 6f 72 64 68  Include keywordh
32f2b 61 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ash.h in the mid
32f2c 64 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e  dle of tokenize.
32f2d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  c ************/.
32f2e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
32f2f 42 65 67 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f  Begin file keywo
32f30 72 64 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a  rdhash.h *******
32f31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32f32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
32f33 2f 2a 2a 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65  /***** This file
32f34 20 63 6f 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61   contains automa
32f35 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
32f36 64 20 63 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a  d code ******.**
32f37 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
32f38 74 68 69 73 20 66 69 6c 65 20 68 61 73 20 62 65  this file has be
32f39 65 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  en automatically
32f3a 20 67 65 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a   generated by.**
32f3b 0a 2a 2a 20 20 20 73 71 6c 69 74 65 2f 74 6f 6f  .**   sqlite/too
32f3c 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e  l/mkkeywordhash.
32f3d 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  c.**.** The code
32f3e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d   in this file im
32f3f 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74  plements a funct
32f40 69 6f 6e 20 74 68 61 74 20 64 65 74 65 72 6d 69  ion that determi
32f41 6e 65 73 20 77 68 65 74 68 65 72 0a 2a 2a 20 6f  nes whether.** o
32f42 72 20 6e 6f 74 20 61 20 67 69 76 65 6e 20 69 64  r not a given id
32f43 65 6e 74 69 66 69 65 72 20 69 73 20 72 65 61 6c  entifier is real
32f44 6c 79 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72  ly an SQL keywor
32f45 64 2e 20 20 54 68 65 20 73 61 6d 65 20 74 68 69  d.  The same thi
32f46 6e 67 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 69  ng.** might be i
32f47 6d 70 6c 65 6d 65 6e 74 65 64 20 6d 6f 72 65 20  mplemented more 
32f48 64 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20 61  directly using a
32f49 20 68 61 6e 64 2d 77 72 69 74 74 65 6e 20 68 61   hand-written ha
32f4a 73 68 20 74 61 62 6c 65 2e 0a 2a 2a 20 42 75 74  sh table..** But
32f4b 20 62 79 20 75 73 69 6e 67 20 74 68 69 73 20 61   by using this a
32f4c 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
32f4d 65 72 61 74 65 64 20 63 6f 64 65 2c 20 74 68 65  erated code, the
32f4e 20 73 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 64   size of the cod
32f4f 65 0a 2a 2a 20 69 73 20 73 75 62 73 74 61 6e 74  e.** is substant
32f50 69 61 6c 6c 79 20 72 65 64 75 63 65 64 2e 20 20  ially reduced.  
32f51 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e  This is importan
32f52 74 20 66 6f 72 20 65 6d 62 65 64 64 65 64 20 61  t for embedded a
32f53 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f  pplications.** o
32f54 6e 20 70 6c 61 74 66 6f 72 6d 73 20 77 69 74 68  n platforms with
32f55 20 6c 69 6d 69 74 65 64 20 6d 65 6d 6f 72 79 2e   limited memory.
32f56 0a 2a 2f 0a 2f 2a 20 48 61 73 68 20 73 63 6f 72  .*/./* Hash scor
32f57 65 3a 20 31 37 35 20 2a 2f 0a 73 74 61 74 69 63  e: 175 */.static
32f58 20 69 6e 74 20 6b 65 79 77 6f 72 64 43 6f 64 65   int keywordCode
32f59 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
32f5a 69 6e 74 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65  int n){.  /* zTe
32f5b 78 74 5b 5d 20 65 6e 63 6f 64 65 73 20 38 31 31  xt[] encodes 811
32f5c 20 62 79 74 65 73 20 6f 66 20 6b 65 79 77 6f 72   bytes of keywor
32f5d 64 73 20 69 6e 20 35 34 31 20 62 79 74 65 73 20  ds in 541 bytes 
32f5e 2a 2f 0a 20 20 2f 2a 20 20 20 52 45 49 4e 44 45  */.  /*   REINDE
32f5f 58 45 44 45 53 43 41 50 45 41 43 48 45 43 4b 45  XEDESCAPEACHECKE
32f60 59 42 45 46 4f 52 45 49 47 4e 4f 52 45 47 45 58  YBEFOREIGNOREGEX
32f61 50 4c 41 49 4e 53 54 45 41 44 44 41 54 41 42 41  PLAINSTEADDATABA
32f62 53 45 4c 45 43 54 20 20 20 20 20 20 20 2a 2f 0a  SELECT       */.
32f63 20 20 2f 2a 20 20 20 41 42 4c 45 46 54 48 45 4e    /*   ABLEFTHEN
32f64 44 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43 45  DEFERRABLELSEXCE
32f65 50 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52  PTRANSACTIONATUR
32f66 41 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49 56  ALTERAISEXCLUSIV
32f67 45 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f  E         */.  /
32f68 2a 20 20 20 58 49 53 54 53 41 56 45 50 4f 49 4e  *   XISTSAVEPOIN
32f69 54 45 52 53 45 43 54 52 49 47 47 45 52 45 46 45  TERSECTRIGGEREFE
32f6a 52 45 4e 43 45 53 43 4f 4e 53 54 52 41 49 4e 54  RENCESCONSTRAINT
32f6b 4f 46 46 53 45 54 45 4d 50 4f 52 41 52 59 20 20  OFFSETEMPORARY  
32f6c 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
32f6d 20 55 4e 49 51 55 45 52 59 41 54 54 41 43 48 41   UNIQUERYATTACHA
32f6e 56 49 4e 47 52 4f 55 50 44 41 54 45 42 45 47 49  VINGROUPDATEBEGI
32f6f 4e 4e 45 52 45 4c 45 41 53 45 42 45 54 57 45 45  NNERELEASEBETWEE
32f70 4e 4f 54 4e 55 4c 4c 49 4b 45 20 20 20 20 20 20  NOTNULLIKE      
32f71 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 41      */.  /*   CA
32f72 53 43 41 44 45 4c 45 54 45 43 41 53 45 43 4f 4c  SCADELETECASECOL
32f73 4c 41 54 45 43 52 45 41 54 45 43 55 52 52 45 4e  LATECREATECURREN
32f74 54 5f 44 41 54 45 44 45 54 41 43 48 49 4d 4d 45  T_DATEDETACHIMME
32f75 44 49 41 54 45 4a 4f 49 4e 20 20 20 20 20 20 20  DIATEJOIN       
32f76 20 2a 2f 0a 20 20 2f 2a 20 20 20 53 45 52 54 4d   */.  /*   SERTM
32f77 41 54 43 48 50 4c 41 4e 41 4c 59 5a 45 50 52 41  ATCHPLANALYZEPRA
32f78 47 4d 41 42 4f 52 54 56 41 4c 55 45 53 56 49 52  GMABORTVALUESVIR
32f79 54 55 41 4c 49 4d 49 54 57 48 45 4e 57 48 45 52  TUALIMITWHENWHER
32f7a 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 2a 2f  ENAME         */
32f7b 0a 20 20 2f 2a 20 20 20 41 46 54 45 52 45 50 4c  .  /*   AFTEREPL
32f7c 41 43 45 41 4e 44 45 46 41 55 4c 54 41 55 54 4f  ACEANDEFAULTAUTO
32f7d 49 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c  INCREMENTCASTCOL
32f7e 55 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43  UMNCOMMITCONFLIC
32f7f 54 43 52 4f 53 53 20 20 20 20 20 2a 2f 0a 20 20  TCROSS     */.  
32f80 2f 2a 20 20 20 43 55 52 52 45 4e 54 5f 54 49 4d  /*   CURRENT_TIM
32f81 45 53 54 41 4d 50 52 49 4d 41 52 59 44 45 46 45  ESTAMPRIMARYDEFE
32f82 52 52 45 44 49 53 54 49 4e 43 54 44 52 4f 50 46  RREDISTINCTDROPF
32f83 41 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42 59  AILFROMFULLGLOBY
32f84 49 46 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20  IF      */.  /* 
32f85 20 20 49 53 4e 55 4c 4c 4f 52 44 45 52 45 53 54    ISNULLORDEREST
32f86 52 49 43 54 4f 55 54 45 52 49 47 48 54 52 4f 4c  RICTOUTERIGHTROL
32f87 4c 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49  LBACKROWUNIONUSI
32f88 4e 47 56 41 43 55 55 4d 56 49 45 57 20 20 20 20  NGVACUUMVIEW    
32f89 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49       */.  /*   I
32f8a 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 20  NITIALLY        
32f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f8e 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
32f8f 6e 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 35  nst char zText[5
32f90 34 30 5d 20 3d 20 7b 0a 20 20 20 20 27 52 27 2c  40] = {.    'R',
32f91 27 45 27 2c 27 49 27 2c 27 4e 27 2c 27 44 27 2c  'E','I','N','D',
32f92 27 45 27 2c 27 58 27 2c 27 45 27 2c 27 44 27 2c  'E','X','E','D',
32f93 27 45 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c  'E','S','C','A',
32f94 27 50 27 2c 27 45 27 2c 27 41 27 2c 27 43 27 2c  'P','E','A','C',
32f95 27 48 27 2c 0a 20 20 20 20 27 45 27 2c 27 43 27  'H',.    'E','C'
32f96 2c 27 4b 27 2c 27 45 27 2c 27 59 27 2c 27 42 27  ,'K','E','Y','B'
32f97 2c 27 45 27 2c 27 46 27 2c 27 4f 27 2c 27 52 27  ,'E','F','O','R'
32f98 2c 27 45 27 2c 27 49 27 2c 27 47 27 2c 27 4e 27  ,'E','I','G','N'
32f99 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27 47 27  ,'O','R','E','G'
32f9a 2c 0a 20 20 20 20 27 45 27 2c 27 58 27 2c 27 50  ,.    'E','X','P
32f9b 27 2c 27 4c 27 2c 27 41 27 2c 27 49 27 2c 27 4e  ','L','A','I','N
32f9c 27 2c 27 53 27 2c 27 54 27 2c 27 45 27 2c 27 41  ','S','T','E','A
32f9d 27 2c 27 44 27 2c 27 44 27 2c 27 41 27 2c 27 54  ','D','D','A','T
32f9e 27 2c 27 41 27 2c 27 42 27 2c 27 41 27 2c 0a 20  ','A','B','A',. 
32f9f 20 20 20 27 53 27 2c 27 45 27 2c 27 4c 27 2c 27     'S','E','L','
32fa0 45 27 2c 27 43 27 2c 27 54 27 2c 27 41 27 2c 27  E','C','T','A','
32fa1 42 27 2c 27 4c 27 2c 27 45 27 2c 27 46 27 2c 27  B','L','E','F','
32fa2 54 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27  T','H','E','N','
32fa3 44 27 2c 27 45 27 2c 27 46 27 2c 0a 20 20 20 20  D','E','F',.    
32fa4 27 45 27 2c 27 52 27 2c 27 52 27 2c 27 41 27 2c  'E','R','R','A',
32fa5 27 42 27 2c 27 4c 27 2c 27 45 27 2c 27 4c 27 2c  'B','L','E','L',
32fa6 27 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c  'S','E','X','C',
32fa7 27 45 27 2c 27 50 27 2c 27 54 27 2c 27 52 27 2c  'E','P','T','R',
32fa8 27 41 27 2c 27 4e 27 2c 0a 20 20 20 20 27 53 27  'A','N',.    'S'
32fa9 2c 27 41 27 2c 27 43 27 2c 27 54 27 2c 27 49 27  ,'A','C','T','I'
32faa 2c 27 4f 27 2c 27 4e 27 2c 27 41 27 2c 27 54 27  ,'O','N','A','T'
32fab 2c 27 55 27 2c 27 52 27 2c 27 41 27 2c 27 4c 27  ,'U','R','A','L'
32fac 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 41 27  ,'T','E','R','A'
32fad 2c 27 49 27 2c 0a 20 20 20 20 27 53 27 2c 27 45  ,'I',.    'S','E
32fae 27 2c 27 58 27 2c 27 43 27 2c 27 4c 27 2c 27 55  ','X','C','L','U
32faf 27 2c 27 53 27 2c 27 49 27 2c 27 56 27 2c 27 45  ','S','I','V','E
32fb0 27 2c 27 58 27 2c 27 49 27 2c 27 53 27 2c 27 54  ','X','I','S','T
32fb1 27 2c 27 53 27 2c 27 41 27 2c 27 56 27 2c 27 45  ','S','A','V','E
32fb2 27 2c 0a 20 20 20 20 27 50 27 2c 27 4f 27 2c 27  ',.    'P','O','
32fb3 49 27 2c 27 4e 27 2c 27 54 27 2c 27 45 27 2c 27  I','N','T','E','
32fb4 52 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27  R','S','E','C','
32fb5 54 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27  T','R','I','G','
32fb6 47 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 0a  G','E','R','E',.
32fb7 20 20 20 20 27 46 27 2c 27 45 27 2c 27 52 27 2c      'F','E','R',
32fb8 27 45 27 2c 27 4e 27 2c 27 43 27 2c 27 45 27 2c  'E','N','C','E',
32fb9 27 53 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c  'S','C','O','N',
32fba 27 53 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c  'S','T','R','A',
32fbb 27 49 27 2c 27 4e 27 2c 27 54 27 2c 0a 20 20 20  'I','N','T',.   
32fbc 20 27 4f 27 2c 27 46 27 2c 27 46 27 2c 27 53 27   'O','F','F','S'
32fbd 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 4d 27  ,'E','T','E','M'
32fbe 2c 27 50 27 2c 27 4f 27 2c 27 52 27 2c 27 41 27  ,'P','O','R','A'
32fbf 2c 27 52 27 2c 27 59 27 2c 27 55 27 2c 27 4e 27  ,'R','Y','U','N'
32fc0 2c 27 49 27 2c 27 51 27 2c 0a 20 20 20 20 27 55  ,'I','Q',.    'U
32fc1 27 2c 27 45 27 2c 27 52 27 2c 27 59 27 2c 27 41  ','E','R','Y','A
32fc2 27 2c 27 54 27 2c 27 54 27 2c 27 41 27 2c 27 43  ','T','T','A','C
32fc3 27 2c 27 48 27 2c 27 41 27 2c 27 56 27 2c 27 49  ','H','A','V','I
32fc4 27 2c 27 4e 27 2c 27 47 27 2c 27 52 27 2c 27 4f  ','N','G','R','O
32fc5 27 2c 27 55 27 2c 0a 20 20 20 20 27 50 27 2c 27  ','U',.    'P','
32fc6 44 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 27  D','A','T','E','
32fc7 42 27 2c 27 45 27 2c 27 47 27 2c 27 49 27 2c 27  B','E','G','I','
32fc8 4e 27 2c 27 4e 27 2c 27 45 27 2c 27 52 27 2c 27  N','N','E','R','
32fc9 45 27 2c 27 4c 27 2c 27 45 27 2c 27 41 27 2c 27  E','L','E','A','
32fca 53 27 2c 0a 20 20 20 20 27 45 27 2c 27 42 27 2c  S',.    'E','B',
32fcb 27 45 27 2c 27 54 27 2c 27 57 27 2c 27 45 27 2c  'E','T','W','E',
32fcc 27 45 27 2c 27 4e 27 2c 27 4f 27 2c 27 54 27 2c  'E','N','O','T',
32fcd 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c  'N','U','L','L',
32fce 27 49 27 2c 27 4b 27 2c 27 45 27 2c 27 43 27 2c  'I','K','E','C',
32fcf 0a 20 20 20 20 27 41 27 2c 27 53 27 2c 27 43 27  .    'A','S','C'
32fd0 2c 27 41 27 2c 27 44 27 2c 27 45 27 2c 27 4c 27  ,'A','D','E','L'
32fd1 2c 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 43 27  ,'E','T','E','C'
32fd2 2c 27 41 27 2c 27 53 27 2c 27 45 27 2c 27 43 27  ,'A','S','E','C'
32fd3 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20  ,'O','L','L',.  
32fd4 20 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43    'A','T','E','C
32fd5 27 2c 27 52 27 2c 27 45 27 2c 27 41 27 2c 27 54  ','R','E','A','T
32fd6 27 2c 27 45 27 2c 27 43 27 2c 27 55 27 2c 27 52  ','E','C','U','R
32fd7 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54  ','R','E','N','T
32fd8 27 2c 27 5f 27 2c 27 44 27 2c 0a 20 20 20 20 27  ','_','D',.    '
32fd9 41 27 2c 27 54 27 2c 27 45 27 2c 27 44 27 2c 27  A','T','E','D','
32fda 45 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c 27  E','T','A','C','
32fdb 48 27 2c 27 49 27 2c 27 4d 27 2c 27 4d 27 2c 27  H','I','M','M','
32fdc 45 27 2c 27 44 27 2c 27 49 27 2c 27 41 27 2c 27  E','D','I','A','
32fdd 54 27 2c 27 45 27 2c 0a 20 20 20 20 27 4a 27 2c  T','E',.    'J',
32fde 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c  'O','I','N','S',
32fdf 27 45 27 2c 27 52 27 2c 27 54 27 2c 27 4d 27 2c  'E','R','T','M',
32fe0 27 41 27 2c 27 54 27 2c 27 43 27 2c 27 48 27 2c  'A','T','C','H',
32fe1 27 50 27 2c 27 4c 27 2c 27 41 27 2c 27 4e 27 2c  'P','L','A','N',
32fe2 27 41 27 2c 0a 20 20 20 20 27 4c 27 2c 27 59 27  'A',.    'L','Y'
32fe3 2c 27 5a 27 2c 27 45 27 2c 27 50 27 2c 27 52 27  ,'Z','E','P','R'
32fe4 2c 27 41 27 2c 27 47 27 2c 27 4d 27 2c 27 41 27  ,'A','G','M','A'
32fe5 2c 27 42 27 2c 27 4f 27 2c 27 52 27 2c 27 54 27  ,'B','O','R','T'
32fe6 2c 27 56 27 2c 27 41 27 2c 27 4c 27 2c 27 55 27  ,'V','A','L','U'
32fe7 2c 0a 20 20 20 20 27 45 27 2c 27 53 27 2c 27 56  ,.    'E','S','V
32fe8 27 2c 27 49 27 2c 27 52 27 2c 27 54 27 2c 27 55  ','I','R','T','U
32fe9 27 2c 27 41 27 2c 27 4c 27 2c 27 49 27 2c 27 4d  ','A','L','I','M
32fea 27 2c 27 49 27 2c 27 54 27 2c 27 57 27 2c 27 48  ','I','T','W','H
32feb 27 2c 27 45 27 2c 27 4e 27 2c 27 57 27 2c 0a 20  ','E','N','W',. 
32fec 20 20 20 27 48 27 2c 27 45 27 2c 27 52 27 2c 27     'H','E','R','
32fed 45 27 2c 27 4e 27 2c 27 41 27 2c 27 4d 27 2c 27  E','N','A','M','
32fee 45 27 2c 27 41 27 2c 27 46 27 2c 27 54 27 2c 27  E','A','F','T','
32fef 45 27 2c 27 52 27 2c 27 45 27 2c 27 50 27 2c 27  E','R','E','P','
32ff0 4c 27 2c 27 41 27 2c 27 43 27 2c 0a 20 20 20 20  L','A','C',.    
32ff1 27 45 27 2c 27 41 27 2c 27 4e 27 2c 27 44 27 2c  'E','A','N','D',
32ff2 27 45 27 2c 27 46 27 2c 27 41 27 2c 27 55 27 2c  'E','F','A','U',
32ff3 27 4c 27 2c 27 54 27 2c 27 41 27 2c 27 55 27 2c  'L','T','A','U',
32ff4 27 54 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c  'T','O','I','N',
32ff5 27 43 27 2c 27 52 27 2c 0a 20 20 20 20 27 45 27  'C','R',.    'E'
32ff6 2c 27 4d 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27  ,'M','E','N','T'
32ff7 2c 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 54 27  ,'C','A','S','T'
32ff8 2c 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 55 27  ,'C','O','L','U'
32ff9 2c 27 4d 27 2c 27 4e 27 2c 27 43 27 2c 27 4f 27  ,'M','N','C','O'
32ffa 2c 27 4d 27 2c 0a 20 20 20 20 27 4d 27 2c 27 49  ,'M',.    'M','I
32ffb 27 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4e  ','T','C','O','N
32ffc 27 2c 27 46 27 2c 27 4c 27 2c 27 49 27 2c 27 43  ','F','L','I','C
32ffd 27 2c 27 54 27 2c 27 43 27 2c 27 52 27 2c 27 4f  ','T','C','R','O
32ffe 27 2c 27 53 27 2c 27 53 27 2c 27 43 27 2c 27 55  ','S','S','C','U
32fff 27 2c 0a 20 20 20 20 27 52 27 2c 27 52 27 2c 27  ',.    'R','R','
33000 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27  E','N','T','_','
33001 54 27 2c 27 49 27 2c 27 4d 27 2c 27 45 27 2c 27  T','I','M','E','
33002 53 27 2c 27 54 27 2c 27 41 27 2c 27 4d 27 2c 27  S','T','A','M','
33003 50 27 2c 27 52 27 2c 27 49 27 2c 27 4d 27 2c 0a  P','R','I','M',.
33004 20 20 20 20 27 41 27 2c 27 52 27 2c 27 59 27 2c      'A','R','Y',
33005 27 44 27 2c 27 45 27 2c 27 46 27 2c 27 45 27 2c  'D','E','F','E',
33006 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 44 27 2c  'R','R','E','D',
33007 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 49 27 2c  'I','S','T','I',
33008 27 4e 27 2c 27 43 27 2c 27 54 27 2c 0a 20 20 20  'N','C','T',.   
33009 20 27 44 27 2c 27 52 27 2c 27 4f 27 2c 27 50 27   'D','R','O','P'
3300a 2c 27 46 27 2c 27 41 27 2c 27 49 27 2c 27 4c 27  ,'F','A','I','L'
3300b 2c 27 46 27 2c 27 52 27 2c 27 4f 27 2c 27 4d 27  ,'F','R','O','M'
3300c 2c 27 46 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27  ,'F','U','L','L'
3300d 2c 27 47 27 2c 27 4c 27 2c 0a 20 20 20 20 27 4f  ,'G','L',.    'O
3300e 27 2c 27 42 27 2c 27 59 27 2c 27 49 27 2c 27 46  ','B','Y','I','F
3300f 27 2c 27 49 27 2c 27 53 27 2c 27 4e 27 2c 27 55  ','I','S','N','U
33010 27 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27 2c 27 52  ','L','L','O','R
33011 27 2c 27 44 27 2c 27 45 27 2c 27 52 27 2c 27 45  ','D','E','R','E
33012 27 2c 27 53 27 2c 0a 20 20 20 20 27 54 27 2c 27  ','S',.    'T','
33013 52 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c 27  R','I','C','T','
33014 4f 27 2c 27 55 27 2c 27 54 27 2c 27 45 27 2c 27  O','U','T','E','
33015 52 27 2c 27 49 27 2c 27 47 27 2c 27 48 27 2c 27  R','I','G','H','
33016 54 27 2c 27 52 27 2c 27 4f 27 2c 27 4c 27 2c 27  T','R','O','L','
33017 4c 27 2c 0a 20 20 20 20 27 42 27 2c 27 41 27 2c  L',.    'B','A',
33018 27 43 27 2c 27 4b 27 2c 27 52 27 2c 27 4f 27 2c  'C','K','R','O',
33019 27 57 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c  'W','U','N','I',
3301a 27 4f 27 2c 27 4e 27 2c 27 55 27 2c 27 53 27 2c  'O','N','U','S',
3301b 27 49 27 2c 27 4e 27 2c 27 47 27 2c 27 56 27 2c  'I','N','G','V',
3301c 0a 20 20 20 20 27 41 27 2c 27 43 27 2c 27 55 27  .    'A','C','U'
3301d 2c 27 55 27 2c 27 4d 27 2c 27 56 27 2c 27 49 27  ,'U','M','V','I'
3301e 2c 27 45 27 2c 27 57 27 2c 27 49 27 2c 27 4e 27  ,'E','W','I','N'
3301f 2c 27 49 27 2c 27 54 27 2c 27 49 27 2c 27 41 27  ,'I','T','I','A'
33020 2c 27 4c 27 2c 27 4c 27 2c 27 59 27 2c 0a 20 20  ,'L','L','Y',.  
33021 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
33022 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
33023 61 48 61 73 68 5b 31 32 37 5d 20 3d 20 7b 0a 20  aHash[127] = {. 
33024 20 20 20 20 20 37 32 2c 20 31 30 31 2c 20 31 31       72, 101, 11
33025 34 2c 20 20 37 30 2c 20 20 20 30 2c 20 20 34 35  4,  70,   0,  45
33026 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 37 38 2c  ,   0,   0,  78,
33027 20 20 20 30 2c 20 20 37 33 2c 20 20 20 30 2c 20     0,  73,   0, 
33028 20 20 30 2c 0a 20 20 20 20 20 20 34 32 2c 20 20    0,.      42,  
33029 31 32 2c 20 20 37 34 2c 20 20 31 35 2c 20 20 20  12,  74,  15,   
3302a 30 2c 20 31 31 33 2c 20 20 38 31 2c 20 20 35 30  0, 113,  81,  50
3302b 2c 20 31 30 38 2c 20 20 20 30 2c 20 20 31 39 2c  , 108,   0,  19,
3302c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
3302d 31 31 38 2c 20 20 20 30 2c 20 31 31 36 2c 20 31  118,   0, 116, 1
3302e 31 31 2c 20 20 20 30 2c 20 20 32 32 2c 20 20 38  11,   0,  22,  8
3302f 39 2c 20 20 20 30 2c 20 20 20 39 2c 20 20 20 30  9,   0,   9,   0
33030 2c 20 20 20 30 2c 20 20 36 36 2c 20 20 36 37 2c  ,   0,  66,  67,
33031 0a 20 20 20 20 20 20 20 30 2c 20 20 36 35 2c 20  .       0,  65, 
33032 20 20 36 2c 20 20 20 30 2c 20 20 34 38 2c 20 20    6,   0,  48,  
33033 38 36 2c 20 20 39 38 2c 20 20 20 30 2c 20 31 31  86,  98,   0, 11
33034 35 2c 20 20 39 37 2c 20 20 20 30 2c 20 20 20 30  5,  97,   0,   0
33035 2c 20 20 34 34 2c 0a 20 20 20 20 20 20 20 30 2c  ,  44,.       0,
33036 20 20 39 39 2c 20 20 32 34 2c 20 20 20 30 2c 20    99,  24,   0, 
33037 20 31 37 2c 20 20 20 30 2c 20 31 31 39 2c 20 20   17,   0, 119,  
33038 34 39 2c 20 20 32 33 2c 20 20 20 30 2c 20 20 20  49,  23,   0,   
33039 35 2c 20 31 30 36 2c 20 20 32 35 2c 0a 20 20 20  5, 106,  25,.   
3303a 20 20 20 39 32 2c 20 20 20 30 2c 20 20 20 30 2c     92,   0,   0,
3303b 20 31 32 31 2c 20 31 30 32 2c 20 20 35 36 2c 20   121, 102,  56, 
3303c 31 32 30 2c 20 20 35 33 2c 20 20 32 38 2c 20 20  120,  53,  28,  
3303d 35 31 2c 20 20 20 30 2c 20 20 38 37 2c 20 20 20  51,   0,  87,   
3303e 30 2c 0a 20 20 20 20 20 20 39 36 2c 20 20 32 36  0,.      96,  26
3303f 2c 20 20 20 30 2c 20 20 39 35 2c 20 20 20 30 2c  ,   0,  95,   0,
33040 20 20 20 30 2c 20 20 20 30 2c 20 20 39 31 2c 20     0,   0,  91, 
33041 20 38 38 2c 20 20 39 33 2c 20 20 38 34 2c 20 31   88,  93,  84, 1
33042 30 35 2c 20 20 31 34 2c 0a 20 20 20 20 20 20 33  05,  14,.      3
33043 39 2c 20 31 30 34 2c 20 20 20 30 2c 20 20 37 37  9, 104,   0,  77
33044 2c 20 20 20 30 2c 20 20 31 38 2c 20 20 38 35 2c  ,   0,  18,  85,
33045 20 31 30 37 2c 20 20 33 32 2c 20 20 20 30 2c 20   107,  32,   0, 
33046 31 31 37 2c 20 20 37 36 2c 20 31 30 39 2c 0a 20  117,  76, 109,. 
33047 20 20 20 20 20 35 38 2c 20 20 34 36 2c 20 20 38       58,  46,  8
33048 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 39 30  0,   0,   0,  90
33049 2c 20 20 34 30 2c 20 20 20 30 2c 20 31 31 32 2c  ,  40,   0, 112,
3304a 20 20 20 30 2c 20 20 33 36 2c 20 20 20 30 2c 20     0,  36,   0, 
3304b 20 20 30 2c 0a 20 20 20 20 20 20 32 39 2c 20 20    0,.      29,  
3304c 20 30 2c 20 20 38 32 2c 20 20 35 39 2c 20 20 36   0,  82,  59,  6
3304d 30 2c 20 20 20 30 2c 20 20 32 30 2c 20 20 35 37  0,   0,  20,  57
3304e 2c 20 20 20 30 2c 20 20 35 32 2c 0a 20 20 7d 3b  ,   0,  52,.  };
3304f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
33050 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4e  unsigned char aN
33051 65 78 74 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20  ext[121] = {.   
33052 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c      0,   0,   0,
33053 20 20 20 30 2c 20 20 20 34 2c 20 20 20 30 2c 20     0,   4,   0, 
33054 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
33055 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
33056 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32  0,.       0,   2
33057 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
33058 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
33059 20 31 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   13,   0,   0,  
3305a 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20   0,   0,.       
3305b 30 2c 20 20 20 37 2c 20 20 20 30 2c 20 20 20 30  0,   7,   0,   0
3305c 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3305d 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3305e 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
3305f 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
33060 30 2c 20 20 20 30 2c 20 20 33 33 2c 20 20 20 30  0,   0,  33,   0
33061 2c 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30 2c  ,  21,   0,   0,
33062 20 20 20 30 2c 20 20 34 33 2c 20 20 20 33 2c 20     0,  43,   3, 
33063 20 34 37 2c 0a 20 20 20 20 20 20 20 30 2c 20 20   47,.       0,  
33064 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33   0,   0,   0,  3
33065 30 2c 20 20 20 30 2c 20 20 35 34 2c 20 20 20 30  0,   0,  54,   0
33066 2c 20 20 33 38 2c 20 20 20 30 2c 20 20 20 30 2c  ,  38,   0,   0,
33067 20 20 20 30 2c 20 20 20 31 2c 0a 20 20 20 20 20     0,   1,.     
33068 20 36 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   62,   0,   0,  
33069 36 33 2c 20 20 20 30 2c 20 20 34 31 2c 20 20 20  63,   0,  41,   
3306a 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3306b 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3306c 0a 20 20 20 20 20 20 36 31 2c 20 20 20 30 2c 20  .      61,   0, 
3306d 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3306e 33 31 2c 20 20 35 35 2c 20 20 31 36 2c 20 20 33  31,  55,  16,  3
3306f 34 2c 20 20 31 30 2c 20 20 20 30 2c 20 20 20 30  4,  10,   0,   0
33070 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
33071 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
33072 20 31 31 2c 20 20 36 38 2c 20 20 37 35 2c 20 20   11,  68,  75,  
33073 20 30 2c 20 20 20 38 2c 20 20 20 30 2c 20 31 30   0,   8,   0, 10
33074 30 2c 20 20 39 34 2c 20 20 20 30 2c 0a 20 20 20  0,  94,   0,.   
33075 20 20 31 30 33 2c 20 20 20 30 2c 20 20 38 33 2c    103,   0,  83,
33076 20 20 20 30 2c 20 20 37 31 2c 20 20 20 30 2c 20     0,  71,   0, 
33077 20 20 30 2c 20 31 31 30 2c 20 20 32 37 2c 20 20    0, 110,  27,  
33078 33 37 2c 20 20 36 39 2c 20 20 37 39 2c 20 20 20  37,  69,  79,   
33079 30 2c 0a 20 20 20 20 20 20 33 35 2c 20 20 36 34  0,.      35,  64
3307a 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b  ,   0,   0,.  };
3307b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3307c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c  unsigned char aL
3307d 65 6e 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20  en[121] = {.    
3307e 20 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c 20     7,   7,   5, 
3307f 20 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20 20    4,   6,   4,  
33080 20 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   5,   3,   6,   
33081 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20 36  7,   3,   6,   6
33082 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37 2c  ,.       7,   7,
33083 20 20 20 33 2c 20 20 20 38 2c 20 20 20 32 2c 20     3,   8,   2, 
33084 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    6,   5,   4,  
33085 20 34 2c 20 20 20 33 2c 20 20 31 30 2c 20 20 20   4,   3,  10,   
33086 34 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 31 31  4,   6,.      11
33087 2c 20 20 20 36 2c 20 20 20 32 2c 20 20 20 37 2c  ,   6,   2,   7,
33088 20 20 20 35 2c 20 20 20 35 2c 20 20 20 39 2c 20     5,   5,   9, 
33089 20 20 36 2c 20 20 20 39 2c 20 20 20 39 2c 20 20    6,   9,   9,  
3308a 20 37 2c 20 20 31 30 2c 20 20 31 30 2c 0a 20 20   7,  10,  10,.  
3308b 20 20 20 20 20 34 2c 20 20 20 36 2c 20 20 20 32       4,   6,   2
3308c 2c 20 20 20 33 2c 20 20 20 39 2c 20 20 20 34 2c  ,   3,   9,   4,
3308d 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20     2,   6,   5, 
3308e 20 20 36 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    6,   6,   5,  
3308f 20 36 2c 0a 20 20 20 20 20 20 20 35 2c 20 20 20   6,.       5,   
33090 35 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20 37  5,   7,   7,   7
33091 2c 20 20 20 33 2c 20 20 20 32 2c 20 20 20 34 2c  ,   3,   2,   4,
33092 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c 20     4,   7,   3, 
33093 20 20 36 2c 20 20 20 34 2c 0a 20 20 20 20 20 20    6,   4,.      
33094 20 37 2c 20 20 20 36 2c 20 20 31 32 2c 20 20 20   7,   6,  12,   
33095 36 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20 36  6,   9,   4,   6
33096 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37 2c  ,   5,   4,   7,
33097 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a     6,   5,   6,.
33098 20 20 20 20 20 20 20 37 2c 20 20 20 35 2c 20 20         7,   5,  
33099 20 34 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20   4,   5,   6,   
3309a 35 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 37  5,   7,   3,   7
3309b 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32 2c  ,  13,   2,   2,
3309c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 36 2c 20     4,.       6, 
3309d 20 20 36 2c 20 20 20 38 2c 20 20 20 35 2c 20 20    6,   8,   5,  
3309e 31 37 2c 20 20 31 32 2c 20 20 20 37 2c 20 20 20  17,  12,   7,   
3309f 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 34  8,   8,   2,   4
330a0 2c 20 20 20 34 2c 20 20 20 34 2c 0a 20 20 20 20  ,   4,   4,.    
330a1 20 20 20 34 2c 20 20 20 34 2c 20 20 20 32 2c 20     4,   4,   2, 
330a2 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    2,   6,   5,  
330a3 20 38 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20   8,   5,   5,   
330a4 38 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20 35  8,   3,   5,   5
330a5 2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 34 2c  ,.       6,   4,
330a6 20 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a     9,   3,.  };.
330a7 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
330a8 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
330a9 74 20 61 4f 66 66 73 65 74 5b 31 32 31 5d 20 3d  t aOffset[121] =
330aa 20 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32   {.       0,   2
330ab 2c 20 20 20 32 2c 20 20 20 38 2c 20 20 20 39 2c  ,   2,   8,   9,
330ac 20 20 31 34 2c 20 20 31 36 2c 20 20 32 30 2c 20    14,  16,  20, 
330ad 20 32 33 2c 20 20 32 35 2c 20 20 32 35 2c 20 20   23,  25,  25,  
330ae 32 39 2c 20 20 33 33 2c 0a 20 20 20 20 20 20 33  29,  33,.      3
330af 36 2c 20 20 34 31 2c 20 20 34 36 2c 20 20 34 38  6,  41,  46,  48
330b0 2c 20 20 35 33 2c 20 20 35 34 2c 20 20 35 39 2c  ,  53,  54,  59,
330b1 20 20 36 32 2c 20 20 36 35 2c 20 20 36 37 2c 20    62,  65,  67, 
330b2 20 36 39 2c 20 20 37 38 2c 20 20 38 31 2c 0a 20   69,  78,  81,. 
330b3 20 20 20 20 20 38 36 2c 20 20 39 31 2c 20 20 39       86,  91,  9
330b4 35 2c 20 20 39 36 2c 20 31 30 31 2c 20 31 30 35  5,  96, 101, 105
330b5 2c 20 31 30 39 2c 20 31 31 37 2c 20 31 32 32 2c  , 109, 117, 122,
330b6 20 31 32 38 2c 20 31 33 36 2c 20 31 34 32 2c 20   128, 136, 142, 
330b7 31 35 32 2c 0a 20 20 20 20 20 31 35 39 2c 20 31  152,.     159, 1
330b8 36 32 2c 20 31 36 32 2c 20 31 36 35 2c 20 31 36  62, 162, 165, 16
330b9 37 2c 20 31 36 37 2c 20 31 37 31 2c 20 31 37 36  7, 167, 171, 176
330ba 2c 20 31 37 39 2c 20 31 38 34 2c 20 31 38 39 2c  , 179, 184, 189,
330bb 20 31 39 34 2c 20 31 39 37 2c 0a 20 20 20 20 20   194, 197,.     
330bc 32 30 33 2c 20 32 30 36 2c 20 32 31 30 2c 20 32  203, 206, 210, 2
330bd 31 37 2c 20 32 32 33 2c 20 32 32 33 2c 20 32 32  17, 223, 223, 22
330be 33 2c 20 32 32 36 2c 20 32 32 39 2c 20 32 33 33  3, 226, 229, 233
330bf 2c 20 32 33 34 2c 20 32 33 38 2c 20 32 34 34 2c  , 234, 238, 244,
330c0 0a 20 20 20 20 20 32 34 38 2c 20 32 35 35 2c 20  .     248, 255, 
330c1 32 36 31 2c 20 32 37 33 2c 20 32 37 39 2c 20 32  261, 273, 279, 2
330c2 38 38 2c 20 32 39 30 2c 20 32 39 36 2c 20 33 30  88, 290, 296, 30
330c3 31 2c 20 33 30 33 2c 20 33 31 30 2c 20 33 31 35  1, 303, 310, 315
330c4 2c 20 33 32 30 2c 0a 20 20 20 20 20 33 32 36 2c  , 320,.     326,
330c5 20 33 33 32 2c 20 33 33 37 2c 20 33 34 31 2c 20   332, 337, 341, 
330c6 33 34 34 2c 20 33 35 30 2c 20 33 35 34 2c 20 33  344, 350, 354, 3
330c7 36 31 2c 20 33 36 33 2c 20 33 37 30 2c 20 33 37  61, 363, 370, 37
330c8 32 2c 20 33 37 34 2c 20 33 38 33 2c 0a 20 20 20  2, 374, 383,.   
330c9 20 20 33 38 37 2c 20 33 39 33 2c 20 33 39 39 2c    387, 393, 399,
330ca 20 34 30 37 2c 20 34 31 32 2c 20 34 31 32 2c 20   407, 412, 412, 
330cb 34 32 38 2c 20 34 33 35 2c 20 34 34 32 2c 20 34  428, 435, 442, 4
330cc 34 33 2c 20 34 35 30 2c 20 34 35 34 2c 20 34 35  43, 450, 454, 45
330cd 38 2c 0a 20 20 20 20 20 34 36 32 2c 20 34 36 36  8,.     462, 466
330ce 2c 20 34 36 39 2c 20 34 37 31 2c 20 34 37 33 2c  , 469, 471, 473,
330cf 20 34 37 39 2c 20 34 38 33 2c 20 34 39 31 2c 20   479, 483, 491, 
330d0 34 39 35 2c 20 35 30 30 2c 20 35 30 38 2c 20 35  495, 500, 508, 5
330d1 31 31 2c 20 35 31 36 2c 0a 20 20 20 20 20 35 32  11, 516,.     52
330d2 31 2c 20 35 32 37 2c 20 35 33 31 2c 20 35 33 36  1, 527, 531, 536
330d3 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
330d4 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
330d5 68 61 72 20 61 43 6f 64 65 5b 31 32 31 5d 20 3d  har aCode[121] =
330d6 20 7b 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44 45   {.    TK_REINDE
330d7 58 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 45 44  X,    TK_INDEXED
330d8 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20 20  ,    TK_INDEX,  
330d9 20 20 20 20 54 4b 5f 44 45 53 43 2c 20 20 20 20      TK_DESC,    
330da 20 20 20 54 4b 5f 45 53 43 41 50 45 2c 20 20 20     TK_ESCAPE,   
330db 20 20 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c 20    .    TK_EACH, 
330dc 20 20 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c 20        TK_CHECK, 
330dd 20 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20 20       TK_KEY,    
330de 20 20 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20 20      TK_BEFORE,  
330df 20 20 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20 20     TK_FOREIGN,  
330e0 20 20 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20 20    .    TK_FOR,  
330e1 20 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45 2c        TK_IGNORE,
330e2 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c       TK_LIKE_KW,
330e3 20 20 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c 20      TK_EXPLAIN, 
330e4 20 20 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20 20     TK_INSTEAD,  
330e5 20 20 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20 20    .    TK_ADD,  
330e6 20 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41 53        TK_DATABAS
330e7 45 2c 20 20 20 54 4b 5f 41 53 2c 20 20 20 20 20  E,   TK_AS,     
330e8 20 20 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20 20      TK_SELECT,  
330e9 20 20 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20 20     TK_TABLE,    
330ea 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b    .    TK_JOIN_K
330eb 57 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20 20  W,    TK_THEN,  
330ec 20 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20 20       TK_END,    
330ed 20 20 20 20 54 4b 5f 44 45 46 45 52 52 41 42 4c      TK_DEFERRABL
330ee 45 2c 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20 20  E, TK_ELSE,     
330ef 20 20 0a 20 20 20 20 54 4b 5f 45 58 43 45 50 54    .    TK_EXCEPT
330f0 2c 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41 43  ,     TK_TRANSAC
330f1 54 49 4f 4e 2c 54 4b 5f 41 43 54 49 4f 4e 2c 20  TION,TK_ACTION, 
330f2 20 20 20 20 54 4b 5f 4f 4e 2c 20 20 20 20 20 20      TK_ON,      
330f3 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
330f4 20 20 0a 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c    .    TK_ALTER,
330f5 20 20 20 20 20 20 54 4b 5f 52 41 49 53 45 2c 20        TK_RAISE, 
330f6 20 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49 56       TK_EXCLUSIV
330f7 45 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c 20 20  E,  TK_EXISTS,  
330f8 20 20 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 2c     TK_SAVEPOINT,
330f9 20 20 0a 20 20 20 20 54 4b 5f 49 4e 54 45 52 53    .    TK_INTERS
330fa 45 43 54 2c 20 20 54 4b 5f 54 52 49 47 47 45 52  ECT,  TK_TRIGGER
330fb 2c 20 20 20 20 54 4b 5f 52 45 46 45 52 45 4e 43  ,    TK_REFERENC
330fc 45 53 2c 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e  ES, TK_CONSTRAIN
330fd 54 2c 20 54 4b 5f 49 4e 54 4f 2c 20 20 20 20 20  T, TK_INTO,     
330fe 20 20 0a 20 20 20 20 54 4b 5f 4f 46 46 53 45 54    .    TK_OFFSET
330ff 2c 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20 20 20  ,     TK_OF,    
33100 20 20 20 20 20 54 4b 5f 53 45 54 2c 20 20 20 20       TK_SET,    
33101 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20      TK_TEMP,    
33102 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20     TK_TEMP,     
33103 20 20 0a 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20    .    TK_OR,   
33104 20 20 20 20 20 20 54 4b 5f 55 4e 49 51 55 45 2c        TK_UNIQUE,
33105 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c 20 20       TK_QUERY,  
33106 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c 20 20      TK_ATTACH,  
33107 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20 20 20     TK_HAVING,   
33108 20 20 0a 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c    .    TK_GROUP,
33109 20 20 20 20 20 20 54 4b 5f 55 50 44 41 54 45 2c        TK_UPDATE,
3310a 20 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c 20 20       TK_BEGIN,  
3310b 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
3310c 20 20 20 54 4b 5f 52 45 4c 45 41 53 45 2c 20 20     TK_RELEASE,  
3310d 20 20 0a 20 20 20 20 54 4b 5f 42 45 54 57 45 45    .    TK_BETWEE
3310e 4e 2c 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  N,    TK_NOTNULL
3310f 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20 20 20  ,    TK_NOT,    
33110 20 20 20 20 54 4b 5f 4e 4f 2c 20 20 20 20 20 20      TK_NO,      
33111 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20     TK_NULL,     
33112 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b    .    TK_LIKE_K
33113 57 2c 20 20 20 20 54 4b 5f 43 41 53 43 41 44 45  W,    TK_CASCADE
33114 2c 20 20 20 20 54 4b 5f 41 53 43 2c 20 20 20 20  ,    TK_ASC,    
33115 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 20      TK_DELETE,  
33116 20 20 20 54 4b 5f 43 41 53 45 2c 20 20 20 20 20     TK_CASE,     
33117 20 20 0a 20 20 20 20 54 4b 5f 43 4f 4c 4c 41 54    .    TK_COLLAT
33118 45 2c 20 20 20 20 54 4b 5f 43 52 45 41 54 45 2c  E,    TK_CREATE,
33119 20 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57       TK_CTIME_KW
3311a 2c 20 20 20 54 4b 5f 44 45 54 41 43 48 2c 20 20  ,   TK_DETACH,  
3311b 20 20 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c     TK_IMMEDIATE,
3311c 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 2c 20    .    TK_JOIN, 
3311d 20 20 20 20 20 20 54 4b 5f 49 4e 53 45 52 54 2c        TK_INSERT,
3311e 20 20 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20 20       TK_MATCH,  
3311f 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20 20      TK_PLAN,    
33120 20 20 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20     TK_ANALYZE,  
33121 20 20 0a 20 20 20 20 54 4b 5f 50 52 41 47 4d 41    .    TK_PRAGMA
33122 2c 20 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c 20  ,     TK_ABORT, 
33123 20 20 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c 20       TK_VALUES, 
33124 20 20 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c 20      TK_VIRTUAL, 
33125 20 20 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20     TK_LIMIT,    
33126 20 20 0a 20 20 20 20 54 4b 5f 57 48 45 4e 2c 20    .    TK_WHEN, 
33127 20 20 20 20 20 20 54 4b 5f 57 48 45 52 45 2c 20        TK_WHERE, 
33128 20 20 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20       TK_RENAME, 
33129 20 20 20 20 54 4b 5f 41 46 54 45 52 2c 20 20 20      TK_AFTER,   
3312a 20 20 20 54 4b 5f 52 45 50 4c 41 43 45 2c 20 20     TK_REPLACE,  
3312b 20 20 0a 20 20 20 20 54 4b 5f 41 4e 44 2c 20 20    .    TK_AND,  
3312c 20 20 20 20 20 20 54 4b 5f 44 45 46 41 55 4c 54        TK_DEFAULT
3312d 2c 20 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43 52  ,    TK_AUTOINCR
3312e 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20 20  ,   TK_TO,      
3312f 20 20 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20     TK_IN,       
33130 20 20 0a 20 20 20 20 54 4b 5f 43 41 53 54 2c 20    .    TK_CAST, 
33131 20 20 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b        TK_COLUMNK
33132 57 2c 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20  W,   TK_COMMIT, 
33133 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c      TK_CONFLICT,
33134 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
33135 20 20 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f    .    TK_CTIME_
33136 4b 57 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b  KW,   TK_CTIME_K
33137 57 2c 20 20 20 54 4b 5f 50 52 49 4d 41 52 59 2c  W,   TK_PRIMARY,
33138 20 20 20 20 54 4b 5f 44 45 46 45 52 52 45 44 2c      TK_DEFERRED,
33139 20 20 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20     TK_DISTINCT, 
3313a 20 20 0a 20 20 20 20 54 4b 5f 49 53 2c 20 20 20    .    TK_IS,   
3313b 20 20 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20        TK_DROP,  
3313c 20 20 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20       TK_FAIL,   
3313d 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20      TK_FROM,    
3313e 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
3313f 20 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b    .    TK_LIKE_K
33140 57 2c 20 20 20 20 54 4b 5f 42 59 2c 20 20 20 20  W,    TK_BY,    
33141 20 20 20 20 20 54 4b 5f 49 46 2c 20 20 20 20 20       TK_IF,     
33142 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20      TK_ISNULL,  
33143 20 20 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20 20     TK_ORDER,    
33144 20 20 0a 20 20 20 20 54 4b 5f 52 45 53 54 52 49    .    TK_RESTRI
33145 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  CT,   TK_JOIN_KW
33146 2c 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c  ,    TK_JOIN_KW,
33147 20 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c      TK_ROLLBACK,
33148 20 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20 20 20     TK_ROW,      
33149 20 20 0a 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c    .    TK_UNION,
3314a 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20        TK_USING, 
3314b 20 20 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20       TK_VACUUM, 
3314c 20 20 20 20 54 4b 5f 56 49 45 57 2c 20 20 20 20      TK_VIEW,    
3314d 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c     TK_INITIALLY,
3314e 20 20 0a 20 20 20 20 54 4b 5f 41 4c 4c 2c 20 20    .    TK_ALL,  
3314f 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e        .  };.  in
33150 74 20 68 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c  t h, i;.  if( n<
33151 32 20 29 20 72 65 74 75 72 6e 20 54 4b 5f 49 44  2 ) return TK_ID
33152 3b 0a 20 20 68 20 3d 20 28 28 63 68 61 72 4d 61  ;.  h = ((charMa
33153 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20  p(z[0])*4) ^.   
33154 20 20 20 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d     (charMap(z[n-
33155 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e  1])*3) ^.      n
33156 29 20 25 20 31 32 37 3b 0a 20 20 66 6f 72 28 69  ) % 127;.  for(i
33157 3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68 5d 29  =((int)aHash[h])
33158 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e  -1; i>=0; i=((in
33159 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a  t)aNext[i])-1){.
3315a 20 20 20 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d      if( aLen[i]=
3315b 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =n && sqlite3Str
3315c 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66  NICmp(&zText[aOf
3315d 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30  fset[i]],z,n)==0
3315e 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61   ){.      testca
3315f 73 65 28 20 69 3d 3d 30 20 29 3b 20 2f 2a 20 52  se( i==0 ); /* R
33160 45 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20 20  EINDEX */.      
33161 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29  testcase( i==1 )
33162 3b 20 2f 2a 20 49 4e 44 45 58 45 44 20 2a 2f 0a  ; /* INDEXED */.
33163 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33164 69 3d 3d 32 20 29 3b 20 2f 2a 20 49 4e 44 45 58  i==2 ); /* INDEX
33165 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
33166 73 65 28 20 69 3d 3d 33 20 29 3b 20 2f 2a 20 44  se( i==3 ); /* D
33167 45 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ESC */.      tes
33168 74 63 61 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f  tcase( i==4 ); /
33169 2a 20 45 53 43 41 50 45 20 2a 2f 0a 20 20 20 20  * ESCAPE */.    
3316a 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35    testcase( i==5
3316b 20 29 3b 20 2f 2a 20 45 41 43 48 20 2a 2f 0a 20   ); /* EACH */. 
3316c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3316d 3d 3d 36 20 29 3b 20 2f 2a 20 43 48 45 43 4b 20  ==6 ); /* CHECK 
3316e 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3316f 65 28 20 69 3d 3d 37 20 29 3b 20 2f 2a 20 4b 45  e( i==7 ); /* KE
33170 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  Y */.      testc
33171 61 73 65 28 20 69 3d 3d 38 20 29 3b 20 2f 2a 20  ase( i==8 ); /* 
33172 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20  BEFORE */.      
33173 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 20 29  testcase( i==9 )
33174 3b 20 2f 2a 20 46 4f 52 45 49 47 4e 20 2a 2f 0a  ; /* FOREIGN */.
33175 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33176 69 3d 3d 31 30 20 29 3b 20 2f 2a 20 46 4f 52 20  i==10 ); /* FOR 
33177 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33178 65 28 20 69 3d 3d 31 31 20 29 3b 20 2f 2a 20 49  e( i==11 ); /* I
33179 47 4e 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74  GNORE */.      t
3317a 65 73 74 63 61 73 65 28 20 69 3d 3d 31 32 20 29  estcase( i==12 )
3317b 3b 20 2f 2a 20 52 45 47 45 58 50 20 2a 2f 0a 20  ; /* REGEXP */. 
3317c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3317d 3d 3d 31 33 20 29 3b 20 2f 2a 20 45 58 50 4c 41  ==13 ); /* EXPLA
3317e 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
3317f 63 61 73 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f  case( i==14 ); /
33180 2a 20 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 20  * INSTEAD */.   
33181 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33182 31 35 20 29 3b 20 2f 2a 20 41 44 44 20 2a 2f 0a  15 ); /* ADD */.
33183 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33184 69 3d 3d 31 36 20 29 3b 20 2f 2a 20 44 41 54 41  i==16 ); /* DATA
33185 42 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  BASE */.      te
33186 73 74 63 61 73 65 28 20 69 3d 3d 31 37 20 29 3b  stcase( i==17 );
33187 20 2f 2a 20 41 53 20 2a 2f 0a 20 20 20 20 20 20   /* AS */.      
33188 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38 20  testcase( i==18 
33189 29 3b 20 2f 2a 20 53 45 4c 45 43 54 20 2a 2f 0a  ); /* SELECT */.
3318a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3318b 69 3d 3d 31 39 20 29 3b 20 2f 2a 20 54 41 42 4c  i==19 ); /* TABL
3318c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
3318d 61 73 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a  ase( i==20 ); /*
3318e 20 4c 45 46 54 20 2a 2f 0a 20 20 20 20 20 20 74   LEFT */.      t
3318f 65 73 74 63 61 73 65 28 20 69 3d 3d 32 31 20 29  estcase( i==21 )
33190 3b 20 2f 2a 20 54 48 45 4e 20 2a 2f 0a 20 20 20  ; /* THEN */.   
33191 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33192 32 32 20 29 3b 20 2f 2a 20 45 4e 44 20 2a 2f 0a  22 ); /* END */.
33193 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33194 69 3d 3d 32 33 20 29 3b 20 2f 2a 20 44 45 46 45  i==23 ); /* DEFE
33195 52 52 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20  RRABLE */.      
33196 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 34 20  testcase( i==24 
33197 29 3b 20 2f 2a 20 45 4c 53 45 20 2a 2f 0a 20 20  ); /* ELSE */.  
33198 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33199 3d 32 35 20 29 3b 20 2f 2a 20 45 58 43 45 50 54  =25 ); /* EXCEPT
3319a 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3319b 73 65 28 20 69 3d 3d 32 36 20 29 3b 20 2f 2a 20  se( i==26 ); /* 
3319c 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a 20  TRANSACTION */. 
3319d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3319e 3d 3d 32 37 20 29 3b 20 2f 2a 20 41 43 54 49 4f  ==27 ); /* ACTIO
3319f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
331a0 61 73 65 28 20 69 3d 3d 32 38 20 29 3b 20 2f 2a  ase( i==28 ); /*
331a1 20 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   ON */.      tes
331a2 74 63 61 73 65 28 20 69 3d 3d 32 39 20 29 3b 20  tcase( i==29 ); 
331a3 2f 2a 20 4e 41 54 55 52 41 4c 20 2a 2f 0a 20 20  /* NATURAL */.  
331a4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
331a5 3d 33 30 20 29 3b 20 2f 2a 20 41 4c 54 45 52 20  =30 ); /* ALTER 
331a6 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
331a7 65 28 20 69 3d 3d 33 31 20 29 3b 20 2f 2a 20 52  e( i==31 ); /* R
331a8 41 49 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  AISE */.      te
331a9 73 74 63 61 73 65 28 20 69 3d 3d 33 32 20 29 3b  stcase( i==32 );
331aa 20 2f 2a 20 45 58 43 4c 55 53 49 56 45 20 2a 2f   /* EXCLUSIVE */
331ab 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
331ac 20 69 3d 3d 33 33 20 29 3b 20 2f 2a 20 45 58 49   i==33 ); /* EXI
331ad 53 54 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  STS */.      tes
331ae 74 63 61 73 65 28 20 69 3d 3d 33 34 20 29 3b 20  tcase( i==34 ); 
331af 2f 2a 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f 0a  /* SAVEPOINT */.
331b0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331b1 69 3d 3d 33 35 20 29 3b 20 2f 2a 20 49 4e 54 45  i==35 ); /* INTE
331b2 52 53 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74  RSECT */.      t
331b3 65 73 74 63 61 73 65 28 20 69 3d 3d 33 36 20 29  estcase( i==36 )
331b4 3b 20 2f 2a 20 54 52 49 47 47 45 52 20 2a 2f 0a  ; /* TRIGGER */.
331b5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331b6 69 3d 3d 33 37 20 29 3b 20 2f 2a 20 52 45 46 45  i==37 ); /* REFE
331b7 52 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20 20  RENCES */.      
331b8 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 38 20  testcase( i==38 
331b9 29 3b 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54  ); /* CONSTRAINT
331ba 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
331bb 73 65 28 20 69 3d 3d 33 39 20 29 3b 20 2f 2a 20  se( i==39 ); /* 
331bc 49 4e 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65  INTO */.      te
331bd 73 74 63 61 73 65 28 20 69 3d 3d 34 30 20 29 3b  stcase( i==40 );
331be 20 2f 2a 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20   /* OFFSET */.  
331bf 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
331c0 3d 34 31 20 29 3b 20 2f 2a 20 4f 46 20 2a 2f 0a  =41 ); /* OF */.
331c1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331c2 69 3d 3d 34 32 20 29 3b 20 2f 2a 20 53 45 54 20  i==42 ); /* SET 
331c3 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
331c4 65 28 20 69 3d 3d 34 33 20 29 3b 20 2f 2a 20 54  e( i==43 ); /* T
331c5 45 4d 50 4f 52 41 52 59 20 2a 2f 0a 20 20 20 20  EMPORARY */.    
331c6 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
331c7 34 20 29 3b 20 2f 2a 20 54 45 4d 50 20 2a 2f 0a  4 ); /* TEMP */.
331c8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331c9 69 3d 3d 34 35 20 29 3b 20 2f 2a 20 4f 52 20 2a  i==45 ); /* OR *
331ca 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
331cb 28 20 69 3d 3d 34 36 20 29 3b 20 2f 2a 20 55 4e  ( i==46 ); /* UN
331cc 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  IQUE */.      te
331cd 73 74 63 61 73 65 28 20 69 3d 3d 34 37 20 29 3b  stcase( i==47 );
331ce 20 2f 2a 20 51 55 45 52 59 20 2a 2f 0a 20 20 20   /* QUERY */.   
331cf 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331d0 34 38 20 29 3b 20 2f 2a 20 41 54 54 41 43 48 20  48 ); /* ATTACH 
331d1 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
331d2 65 28 20 69 3d 3d 34 39 20 29 3b 20 2f 2a 20 48  e( i==49 ); /* H
331d3 41 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74  AVING */.      t
331d4 65 73 74 63 61 73 65 28 20 69 3d 3d 35 30 20 29  estcase( i==50 )
331d5 3b 20 2f 2a 20 47 52 4f 55 50 20 2a 2f 0a 20 20  ; /* GROUP */.  
331d6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
331d7 3d 35 31 20 29 3b 20 2f 2a 20 55 50 44 41 54 45  =51 ); /* UPDATE
331d8 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
331d9 73 65 28 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20  se( i==52 ); /* 
331da 42 45 47 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74  BEGIN */.      t
331db 65 73 74 63 61 73 65 28 20 69 3d 3d 35 33 20 29  estcase( i==53 )
331dc 3b 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f 0a 20 20  ; /* INNER */.  
331dd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
331de 3d 35 34 20 29 3b 20 2f 2a 20 52 45 4c 45 41 53  =54 ); /* RELEAS
331df 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
331e0 61 73 65 28 20 69 3d 3d 35 35 20 29 3b 20 2f 2a  ase( i==55 ); /*
331e1 20 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20   BETWEEN */.    
331e2 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35    testcase( i==5
331e3 36 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55 4c 4c 20  6 ); /* NOTNULL 
331e4 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
331e5 65 28 20 69 3d 3d 35 37 20 29 3b 20 2f 2a 20 4e  e( i==57 ); /* N
331e6 4f 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  OT */.      test
331e7 63 61 73 65 28 20 69 3d 3d 35 38 20 29 3b 20 2f  case( i==58 ); /
331e8 2a 20 4e 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65  * NO */.      te
331e9 73 74 63 61 73 65 28 20 69 3d 3d 35 39 20 29 3b  stcase( i==59 );
331ea 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20   /* NULL */.    
331eb 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
331ec 30 20 29 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f 0a  0 ); /* LIKE */.
331ed 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331ee 69 3d 3d 36 31 20 29 3b 20 2f 2a 20 43 41 53 43  i==61 ); /* CASC
331ef 41 44 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ADE */.      tes
331f0 74 63 61 73 65 28 20 69 3d 3d 36 32 20 29 3b 20  tcase( i==62 ); 
331f1 2f 2a 20 41 53 43 20 2a 2f 0a 20 20 20 20 20 20  /* ASC */.      
331f2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 33 20  testcase( i==63 
331f3 29 3b 20 2f 2a 20 44 45 4c 45 54 45 20 2a 2f 0a  ); /* DELETE */.
331f4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331f5 69 3d 3d 36 34 20 29 3b 20 2f 2a 20 43 41 53 45  i==64 ); /* CASE
331f6 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
331f7 73 65 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20  se( i==65 ); /* 
331f8 43 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20  COLLATE */.     
331f9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 36   testcase( i==66
331fa 20 29 3b 20 2f 2a 20 43 52 45 41 54 45 20 2a 2f   ); /* CREATE */
331fb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
331fc 20 69 3d 3d 36 37 20 29 3b 20 2f 2a 20 43 55 52   i==67 ); /* CUR
331fd 52 45 4e 54 5f 44 41 54 45 20 2a 2f 0a 20 20 20  RENT_DATE */.   
331fe 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331ff 36 38 20 29 3b 20 2f 2a 20 44 45 54 41 43 48 20  68 ); /* DETACH 
33200 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33201 65 28 20 69 3d 3d 36 39 20 29 3b 20 2f 2a 20 49  e( i==69 ); /* I
33202 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20 20  MMEDIATE */.    
33203 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
33204 30 20 29 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0a  0 ); /* JOIN */.
33205 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33206 69 3d 3d 37 31 20 29 3b 20 2f 2a 20 49 4e 53 45  i==71 ); /* INSE
33207 52 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RT */.      test
33208 63 61 73 65 28 20 69 3d 3d 37 32 20 29 3b 20 2f  case( i==72 ); /
33209 2a 20 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20 20  * MATCH */.     
3320a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 33   testcase( i==73
3320b 20 29 3b 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a 20   ); /* PLAN */. 
3320c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3320d 3d 3d 37 34 20 29 3b 20 2f 2a 20 41 4e 41 4c 59  ==74 ); /* ANALY
3320e 5a 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ZE */.      test
3320f 63 61 73 65 28 20 69 3d 3d 37 35 20 29 3b 20 2f  case( i==75 ); /
33210 2a 20 50 52 41 47 4d 41 20 2a 2f 0a 20 20 20 20  * PRAGMA */.    
33211 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
33212 36 20 29 3b 20 2f 2a 20 41 42 4f 52 54 20 2a 2f  6 ); /* ABORT */
33213 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33214 20 69 3d 3d 37 37 20 29 3b 20 2f 2a 20 56 41 4c   i==77 ); /* VAL
33215 55 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  UES */.      tes
33216 74 63 61 73 65 28 20 69 3d 3d 37 38 20 29 3b 20  tcase( i==78 ); 
33217 2f 2a 20 56 49 52 54 55 41 4c 20 2a 2f 0a 20 20  /* VIRTUAL */.  
33218 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33219 3d 37 39 20 29 3b 20 2f 2a 20 4c 49 4d 49 54 20  =79 ); /* LIMIT 
3321a 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3321b 65 28 20 69 3d 3d 38 30 20 29 3b 20 2f 2a 20 57  e( i==80 ); /* W
3321c 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  HEN */.      tes
3321d 74 63 61 73 65 28 20 69 3d 3d 38 31 20 29 3b 20  tcase( i==81 ); 
3321e 2f 2a 20 57 48 45 52 45 20 2a 2f 0a 20 20 20 20  /* WHERE */.    
3321f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38    testcase( i==8
33220 32 20 29 3b 20 2f 2a 20 52 45 4e 41 4d 45 20 2a  2 ); /* RENAME *
33221 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33222 28 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20 41 46  ( i==83 ); /* AF
33223 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  TER */.      tes
33224 74 63 61 73 65 28 20 69 3d 3d 38 34 20 29 3b 20  tcase( i==84 ); 
33225 2f 2a 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20  /* REPLACE */.  
33226 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33227 3d 38 35 20 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f  =85 ); /* AND */
33228 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33229 20 69 3d 3d 38 36 20 29 3b 20 2f 2a 20 44 45 46   i==86 ); /* DEF
3322a 41 55 4c 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  AULT */.      te
3322b 73 74 63 61 73 65 28 20 69 3d 3d 38 37 20 29 3b  stcase( i==87 );
3322c 20 2f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   /* AUTOINCREMEN
3322d 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
3322e 61 73 65 28 20 69 3d 3d 38 38 20 29 3b 20 2f 2a  ase( i==88 ); /*
3322f 20 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   TO */.      tes
33230 74 63 61 73 65 28 20 69 3d 3d 38 39 20 29 3b 20  tcase( i==89 ); 
33231 2f 2a 20 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74  /* IN */.      t
33232 65 73 74 63 61 73 65 28 20 69 3d 3d 39 30 20 29  estcase( i==90 )
33233 3b 20 2f 2a 20 43 41 53 54 20 2a 2f 0a 20 20 20  ; /* CAST */.   
33234 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33235 39 31 20 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e 20  91 ); /* COLUMN 
33236 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33237 65 28 20 69 3d 3d 39 32 20 29 3b 20 2f 2a 20 43  e( i==92 ); /* C
33238 4f 4d 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20 74  OMMIT */.      t
33239 65 73 74 63 61 73 65 28 20 69 3d 3d 39 33 20 29  estcase( i==93 )
3323a 3b 20 2f 2a 20 43 4f 4e 46 4c 49 43 54 20 2a 2f  ; /* CONFLICT */
3323b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3323c 20 69 3d 3d 39 34 20 29 3b 20 2f 2a 20 43 52 4f   i==94 ); /* CRO
3323d 53 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SS */.      test
3323e 63 61 73 65 28 20 69 3d 3d 39 35 20 29 3b 20 2f  case( i==95 ); /
3323f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53 54  * CURRENT_TIMEST
33240 41 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  AMP */.      tes
33241 74 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b 20  tcase( i==96 ); 
33242 2f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20  /* CURRENT_TIME 
33243 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33244 65 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 50  e( i==97 ); /* P
33245 52 49 4d 41 52 59 20 2a 2f 0a 20 20 20 20 20 20  RIMARY */.      
33246 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 38 20  testcase( i==98 
33247 29 3b 20 2f 2a 20 44 45 46 45 52 52 45 44 20 2a  ); /* DEFERRED *
33248 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33249 28 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20 44 49  ( i==99 ); /* DI
3324a 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20  STINCT */.      
3324b 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 30  testcase( i==100
3324c 20 29 3b 20 2f 2a 20 49 53 20 2a 2f 0a 20 20 20   ); /* IS */.   
3324d 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3324e 31 30 31 20 29 3b 20 2f 2a 20 44 52 4f 50 20 2a  101 ); /* DROP *
3324f 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33250 28 20 69 3d 3d 31 30 32 20 29 3b 20 2f 2a 20 46  ( i==102 ); /* F
33251 41 49 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  AIL */.      tes
33252 74 63 61 73 65 28 20 69 3d 3d 31 30 33 20 29 3b  tcase( i==103 );
33253 20 2f 2a 20 46 52 4f 4d 20 2a 2f 0a 20 20 20 20   /* FROM */.    
33254 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
33255 30 34 20 29 3b 20 2f 2a 20 46 55 4c 4c 20 2a 2f  04 ); /* FULL */
33256 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33257 20 69 3d 3d 31 30 35 20 29 3b 20 2f 2a 20 47 4c   i==105 ); /* GL
33258 4f 42 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  OB */.      test
33259 63 61 73 65 28 20 69 3d 3d 31 30 36 20 29 3b 20  case( i==106 ); 
3325a 2f 2a 20 42 59 20 2a 2f 0a 20 20 20 20 20 20 74  /* BY */.      t
3325b 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 37 20  estcase( i==107 
3325c 29 3b 20 2f 2a 20 49 46 20 2a 2f 0a 20 20 20 20  ); /* IF */.    
3325d 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
3325e 30 38 20 29 3b 20 2f 2a 20 49 53 4e 55 4c 4c 20  08 ); /* ISNULL 
3325f 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33260 65 28 20 69 3d 3d 31 30 39 20 29 3b 20 2f 2a 20  e( i==109 ); /* 
33261 4f 52 44 45 52 20 2a 2f 0a 20 20 20 20 20 20 74  ORDER */.      t
33262 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 30 20  estcase( i==110 
33263 29 3b 20 2f 2a 20 52 45 53 54 52 49 43 54 20 2a  ); /* RESTRICT *
33264 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33265 28 20 69 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 4f  ( i==111 ); /* O
33266 55 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  UTER */.      te
33267 73 74 63 61 73 65 28 20 69 3d 3d 31 31 32 20 29  stcase( i==112 )
33268 3b 20 2f 2a 20 52 49 47 48 54 20 2a 2f 0a 20 20  ; /* RIGHT */.  
33269 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3326a 3d 31 31 33 20 29 3b 20 2f 2a 20 52 4f 4c 4c 42  =113 ); /* ROLLB
3326b 41 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ACK */.      tes
3326c 74 63 61 73 65 28 20 69 3d 3d 31 31 34 20 29 3b  tcase( i==114 );
3326d 20 2f 2a 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20   /* ROW */.     
3326e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
3326f 35 20 29 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f  5 ); /* UNION */
33270 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33271 20 69 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 55 53   i==116 ); /* US
33272 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ING */.      tes
33273 74 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29 3b  tcase( i==117 );
33274 20 2f 2a 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20   /* VACUUM */.  
33275 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33276 3d 31 31 38 20 29 3b 20 2f 2a 20 56 49 45 57 20  =118 ); /* VIEW 
33277 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33278 65 28 20 69 3d 3d 31 31 39 20 29 3b 20 2f 2a 20  e( i==119 ); /* 
33279 49 4e 49 54 49 41 4c 4c 59 20 2a 2f 0a 20 20 20  INITIALLY */.   
3327a 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3327b 31 32 30 20 29 3b 20 2f 2a 20 41 4c 4c 20 2a 2f  120 ); /* ALL */
3327c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 43  .      return aC
3327d 6f 64 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ode[i];.    }.  
3327e 7d 0a 20 20 72 65 74 75 72 6e 20 54 4b 5f 49 44  }.  return TK_ID
3327f 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
33280 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65  TE int sqlite3Ke
33281 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20  ywordCode(const 
33282 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
33283 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75  , int n){.  retu
33284 72 6e 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28  rn keywordCode((
33285 63 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a  char*)z, n);.}..
33286 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
33287 45 6e 64 20 6f 66 20 6b 65 79 77 6f 72 64 68 61  End of keywordha
33288 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
33289 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3328a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3328b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3328c 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
3328d 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
3328e 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
3328f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
33290 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20  ../*.** If X is 
33291 61 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74  a character that
33292 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20   can be used in 
33293 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68  an identifier th
33294 65 6e 0a 2a 2a 20 49 64 43 68 61 72 28 58 29 20  en.** IdChar(X) 
33295 77 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f  will be true.  O
33296 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66  therwise it is f
33297 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  alse..**.** For 
33298 41 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61  ASCII, any chara
33299 63 74 65 72 20 77 69 74 68 20 74 68 65 20 68 69  cter with the hi
3329a 67 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74  gh-order bit set
3329b 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69   is.** allowed i
3329c 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e  n an identifier.
3329d 20 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72    For 7-bit char
3329e 61 63 74 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69  acters, .** sqli
3329f 74 65 33 49 73 49 64 43 68 61 72 5b 58 5d 20 6d  te3IsIdChar[X] m
332a0 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20  ust be 1..**.** 
332a1 46 6f 72 20 45 42 43 44 49 43 2c 20 74 68 65 20  For EBCDIC, the 
332a2 72 75 6c 65 73 20 61 72 65 20 6d 6f 72 65 20 63  rules are more c
332a3 6f 6d 70 6c 65 78 20 62 75 74 20 68 61 76 65 20  omplex but have 
332a4 74 68 65 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20  the same.** end 
332a5 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69  result..**.** Ti
332a6 63 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68 65  cket #1066.  the
332a7 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64 6f   SQL standard do
332a8 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27  es not allow '$'
332a9 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c   in the.** middl
332aa 65 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73 2e  e of identfiers.
332ab 20 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20 69    But many SQL i
332ac 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
332ad 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  o. .** SQLite wi
332ae 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ll allow '$' in 
332af 69 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72 20  identifiers for 
332b0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a  compatibility..*
332b1 2a 20 42 75 74 20 74 68 65 20 66 65 61 74 75 72  * But the featur
332b2 65 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65  e is undocumente
332b3 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
332b4 49 54 45 5f 41 53 43 49 49 0a 23 64 65 66 69 6e  ITE_ASCII.#defin
332b5 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28 73  e IdChar(C)  ((s
332b6 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
332b7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 43 5d  unsigned char)C]
332b8 26 30 78 34 36 29 21 3d 30 29 0a 23 65 6e 64 69  &0x46)!=0).#endi
332b9 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
332ba 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50 52  EBCDIC.SQLITE_PR
332bb 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
332bc 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69 63   sqlite3IsEbcdic
332bd 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20  IdChar[] = {./* 
332be 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20 78  x0 x1 x2 x3 x4 x
332bf 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78 41  5 x6 x7 x8 x9 xA
332c0 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46 20   xB xC xD xE xF 
332c1 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20  */.    0, 0, 1, 
332c2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
332c3 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
332c4 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f   0, 0,  /* 4x */
332c5 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  .    0, 1, 1, 1,
332c6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332c7 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 0, 1, 0, 0, 0
332c8 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20  , 0,  /* 5x */. 
332c9 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31     0, 0, 1, 1, 1
332ca 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
332cb 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 0, 0, 1, 0, 
332cc 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  0,  /* 6x */.   
332cd 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
332ce 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
332cf 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
332d0 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 30    /* 7x */.    0
332d1 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
332d2 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
332d3 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20  0, 1, 1, 1, 0,  
332d4 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 8x */.    0, 
332d5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
332d6 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
332d7 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 20 2f 2a   1, 0, 1, 0,  /*
332d8 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c 20 30 2c   9x */.    1, 0,
332d9 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332da 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
332db 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 41  , 1, 1, 0,  /* A
332dc 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30  x */.    0, 0, 0
332dd 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
332de 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
332df 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 42 78 20  0, 0, 0,  /* Bx 
332e0 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
332e1 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
332e2 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 0, 1, 1, 1,
332e3 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78 20 2a 2f   1, 1,  /* Cx */
332e4 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  .    0, 1, 1, 1,
332e5 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332e6 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 0, 1, 1, 1, 1
332e7 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20  , 1,  /* Dx */. 
332e8 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31     0, 0, 1, 1, 1
332e9 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
332ea 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
332eb 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  1,  /* Ex */.   
332ec 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332ed 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
332ee 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
332ef 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64    /* Fx */.};.#d
332f0 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29 20  efine IdChar(C) 
332f1 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20 26   (((c=C)>=0x42 &
332f2 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  & sqlite3IsEbcdi
332f3 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d 29  cIdChar[c-0x40])
332f4 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ).#endif.../*.**
332f5 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67   Return the leng
332f6 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  th of the token 
332f7 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20 7a  that begins at z
332f8 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74  [0]. .** Store t
332f9 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e  he token type in
332fa 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f   *tokenType befo
332fb 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f  re returning..*/
332fc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
332fd 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f  int sqlite3GetTo
332fe 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ken(const unsign
332ff 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  ed char *z, int 
33300 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69  *tokenType){.  i
33301 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74 63  nt i, c;.  switc
33302 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73  h( *z ){.    cas
33303 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27  e ' ': case '\t'
33304 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73  : case '\n': cas
33305 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72  e '\f': case '\r
33306 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  ': {.      testc
33307 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 20 27 20 29  ase( z[0]==' ' )
33308 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
33309 28 20 7a 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b 0a  ( z[0]=='\t' );.
3330a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3330b 7a 5b 30 5d 3d 3d 27 5c 6e 27 20 29 3b 0a 20 20  z[0]=='\n' );.  
3330c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b      testcase( z[
3330d 30 5d 3d 3d 27 5c 66 27 20 29 3b 0a 20 20 20 20  0]=='\f' );.    
3330e 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3330f 3d 3d 27 5c 72 27 20 29 3b 0a 20 20 20 20 20 20  =='\r' );.      
33310 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33  for(i=1; sqlite3
33311 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69  Isspace(z[i]); i
33312 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b  ++){}.      *tok
33313 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43  enType = TK_SPAC
33314 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  E;.      return 
33315 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
33316 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '-': {.      i
33317 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a  f( z[1]=='-' ){.
33318 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b          for(i=2;
33319 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20   (c=z[i])!=0 && 
3331a 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a  c!='\n'; i++){}.
3331b 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
3331c 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20  pe = TK_SPACE;. 
3331d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
3331e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a  .      }.      *
3331f 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4d  tokenType = TK_M
33320 49 4e 55 53 3b 0a 20 20 20 20 20 20 72 65 74 75  INUS;.      retu
33321 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
33322 63 61 73 65 20 27 28 27 3a 20 7b 0a 20 20 20 20  case '(': {.    
33323 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
33324 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65 74 75  K_LP;.      retu
33325 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
33326 63 61 73 65 20 27 29 27 3a 20 7b 0a 20 20 20 20  case ')': {.    
33327 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
33328 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72 65 74 75  K_RP;.      retu
33329 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
3332a 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20 20 20  case ';': {.    
3332b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
3332c 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20 72 65  K_SEMI;.      re
3332d 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
3332e 20 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a 20 20    case '+': {.  
3332f 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
33330 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20 20 20   TK_PLUS;.      
33331 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
33332 20 20 20 20 63 61 73 65 20 27 2a 27 3a 20 7b 0a      case '*': {.
33333 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33334 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20 20 20   = TK_STAR;.    
33335 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
33336 7d 0a 20 20 20 20 63 61 73 65 20 27 2f 27 3a 20  }.    case '/': 
33337 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
33338 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30  !='*' || z[2]==0
33339 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
3333a 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41 53  enType = TK_SLAS
3333b 48 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  H;.        retur
3333c 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
3333d 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d 7a 5b     for(i=3, c=z[
3333e 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c 20 7a  2]; (c!='*' || z
3333f 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28 63 3d  [i]!='/') && (c=
33340 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d  z[i])!=0; i++){}
33341 0a 20 20 20 20 20 20 69 66 28 20 63 20 29 20 69  .      if( c ) i
33342 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ++;.      *token
33343 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b  Type = TK_SPACE;
33344 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
33345 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
33346 27 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '%': {.      *to
33347 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d  kenType = TK_REM
33348 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
33349 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
3334a 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '=': {.      *t
3334b 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51  okenType = TK_EQ
3334c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
3334d 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a   + (z[1]=='=');.
3334e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
3334f 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  <': {.      if( 
33350 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b  (c=z[1])=='=' ){
33351 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33352 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20  ype = TK_LE;.   
33353 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
33354 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
33355 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20  =='>' ){.       
33356 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
33357 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _NE;.        ret
33358 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
33359 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b  se if( c=='<' ){
3335a 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
3335b 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b  ype = TK_LSHIFT;
3335c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
3335d 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  2;.      }else{.
3335e 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
3335f 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20  pe = TK_LT;.    
33360 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
33361 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
33362 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20  case '>': {.    
33363 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d    if( (c=z[1])==
33364 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  '=' ){.        *
33365 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47  tokenType = TK_G
33366 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  E;.        retur
33367 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
33368 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20   if( c=='>' ){. 
33369 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
3336a 65 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20  e = TK_RSHIFT;. 
3336b 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
3336c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
3336d 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3336e 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20   = TK_GT;.      
3336f 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
33370 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61    }.    }.    ca
33371 73 65 20 27 21 27 3a 20 7b 0a 20 20 20 20 20 20  se '!': {.      
33372 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b  if( z[1]!='=' ){
33373 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33374 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
33375 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
33376 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
33377 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33378 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20  ype = TK_NE;.   
33379 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
3337a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
3337b 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20   case '|': {.   
3337c 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27     if( z[1]!='|'
3337d 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
3337e 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f  enType = TK_BITO
3337f 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  R;.        retur
33380 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 1;.      }else
33381 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
33382 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54  Type = TK_CONCAT
33383 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
33384 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   2;.      }.    
33385 7d 0a 20 20 20 20 63 61 73 65 20 27 2c 27 3a 20  }.    case ',': 
33386 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
33387 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20  pe = TK_COMMA;. 
33388 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
33389 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 26     }.    case '&
3338a 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
3338b 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e  nType = TK_BITAN
3338c 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  D;.      return 
3338d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
3338e 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '~': {.      *
3338f 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42  tokenType = TK_B
33390 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72 65 74  ITNOT;.      ret
33391 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
33392 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20 63   case '`':.    c
33393 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63 61  ase '\'':.    ca
33394 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20 20  se '"': {.      
33395 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d  int delim = z[0]
33396 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
33397 28 20 64 65 6c 69 6d 3d 3d 27 60 27 20 29 3b 0a  ( delim=='`' );.
33398 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33399 64 65 6c 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a 20  delim=='\'' );. 
3339a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64       testcase( d
3339b 65 6c 69 6d 3d 3d 27 22 27 20 29 3b 0a 20 20 20  elim=='"' );.   
3339c 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a     for(i=1; (c=z
3339d 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
3339e 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65         if( c==de
3339f 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lim ){.         
333a0 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c   if( z[i+1]==del
333a1 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  im ){.          
333a2 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
333a3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
333a4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
333a5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
333a6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
333a7 66 28 20 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20  f( c=='\'' ){.  
333a8 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
333a9 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20   = TK_STRING;.  
333aa 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31        return i+1
333ab 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
333ac 28 20 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c!=0 ){.      
333ad 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
333ae 4b 5f 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65  K_ID;.        re
333af 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20  turn i+1;.      
333b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
333b1 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
333b2 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
333b3 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
333b4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  }.    }.    case
333b5 20 27 2e 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20   '.': {.#ifndef 
333b6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
333b7 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20  TING_POINT.     
333b8 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64   if( !sqlite3Isd
333b9 69 67 69 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e  igit(z[1]) ).#en
333ba 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  dif.      {.    
333bb 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
333bc 20 54 4b 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20   TK_DOT;.       
333bd 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
333be 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   }.      /* If t
333bf 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65  he next characte
333c0 72 20 69 73 20 61 20 64 69 67 69 74 2c 20 74 68  r is a digit, th
333c1 69 73 20 69 73 20 61 20 66 6c 6f 61 74 69 6e 67  is is a floating
333c2 20 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20   point.      ** 
333c3 6e 75 6d 62 65 72 20 74 68 61 74 20 62 65 67 69  number that begi
333c4 6e 73 20 77 69 74 68 20 22 2e 22 2e 20 20 46 61  ns with ".".  Fa
333c5 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65  ll thru into the
333c6 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
333c7 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 30 27    }.    case '0'
333c8 3a 20 63 61 73 65 20 27 31 27 3a 20 63 61 73 65  : case '1': case
333c9 20 27 32 27 3a 20 63 61 73 65 20 27 33 27 3a 20   '2': case '3': 
333ca 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61  case '4':.    ca
333cb 73 65 20 27 35 27 3a 20 63 61 73 65 20 27 36 27  se '5': case '6'
333cc 3a 20 63 61 73 65 20 27 37 27 3a 20 63 61 73 65  : case '7': case
333cd 20 27 38 27 3a 20 63 61 73 65 20 27 39 27 3a 20   '8': case '9': 
333ce 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
333cf 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 3b 20 20  ( z[0]=='0' );  
333d0 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
333d1 27 31 27 20 29 3b 20 20 74 65 73 74 63 61 73 65  '1' );  testcase
333d2 28 20 7a 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a 20  ( z[0]=='2' );. 
333d3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
333d4 5b 30 5d 3d 3d 27 33 27 20 29 3b 20 20 74 65 73  [0]=='3' );  tes
333d5 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 34 27  tcase( z[0]=='4'
333d6 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
333d7 5b 30 5d 3d 3d 27 35 27 20 29 3b 0a 20 20 20 20  [0]=='5' );.    
333d8 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
333d9 3d 3d 27 36 27 20 29 3b 20 20 74 65 73 74 63 61  =='6' );  testca
333da 73 65 28 20 7a 5b 30 5d 3d 3d 27 37 27 20 29 3b  se( z[0]=='7' );
333db 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
333dc 3d 3d 27 38 27 20 29 3b 0a 20 20 20 20 20 20 74  =='8' );.      t
333dd 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
333de 39 27 20 29 3b 0a 20 20 20 20 20 20 2a 74 6f 6b  9' );.      *tok
333df 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45  enType = TK_INTE
333e0 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  GER;.      for(i
333e1 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67  =0; sqlite3Isdig
333e2 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
333e3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
333e4 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
333e5 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  INT.      if( z[
333e6 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20  i]=='.' ){.     
333e7 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
333e8 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
333e9 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69  digit(z[i]) ){ i
333ea 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74  ++; }.        *t
333eb 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c  okenType = TK_FL
333ec 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  OAT;.      }.   
333ed 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d 3d 27 65     if( (z[i]=='e
333ee 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29 20  ' || z[i]=='E') 
333ef 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 28 20  &&.           ( 
333f0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
333f1 5b 69 2b 31 5d 29 20 0a 20 20 20 20 20 20 20 20  [i+1]) .        
333f2 20 20 20 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d      || ((z[i+1]=
333f3 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d  ='+' || z[i+1]==
333f4 27 2d 27 29 20 26 26 20 73 71 6c 69 74 65 33 49  '-') && sqlite3I
333f5 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a  sdigit(z[i+2])).
333f6 20 20 20 20 20 20 20 20 20 20 20 29 0a 20 20 20             ).   
333f7 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20     ){.        i 
333f8 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77 68  += 2;.        wh
333f9 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
333fa 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b  git(z[i]) ){ i++
333fb 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  ; }.        *tok
333fc 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41  enType = TK_FLOA
333fd 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  T;.      }.#endi
333fe 66 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 49  f.      while( I
333ff 64 43 68 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20  dChar(z[i]) ){. 
33400 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
33401 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
33402 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
33403 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
33404 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
33405 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20  ase '[': {.     
33406 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d   for(i=1, c=z[0]
33407 3b 20 63 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a  ; c!=']' && (c=z
33408 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a  [i])!=0; i++){}.
33409 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3340a 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49   = c==']' ? TK_I
3340b 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  D : TK_ILLEGAL;.
3340c 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
3340d 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
3340e 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ?': {.      *tok
3340f 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52 49  enType = TK_VARI
33410 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72 28  ABLE;.      for(
33411 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64 69  i=1; sqlite3Isdi
33412 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  git(z[i]); i++){
33413 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
33414 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
33415 20 27 23 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '#': {.      fo
33416 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73  r(i=1; sqlite3Is
33417 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b  digit(z[i]); i++
33418 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3e  ){}.      if( i>
33419 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  1 ){.        /* 
3341a 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  Parameters of th
3341b 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65  e form #NNN (whe
3341c 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62  re NNN is a numb
3341d 65 72 29 20 61 72 65 20 75 73 65 64 0a 20 20 20  er) are used.   
3341e 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c       ** internal
3341f 6c 79 20 62 79 20 73 71 6c 69 74 65 33 4e 65 73  ly by sqlite3Nes
33420 74 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20  tedParse.  */.  
33421 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33422 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
33423 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
33424 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33425 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
33426 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  into the next ca
33427 73 65 20 69 66 20 74 68 65 20 27 23 27 20 69 73  se if the '#' is
33428 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79   not followed by
33429 0a 20 20 20 20 20 20 2a 2a 20 61 20 64 69 67 69  .      ** a digi
3342a 74 2e 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20  t. Try to match 
3342b 23 41 41 41 41 20 77 68 65 72 65 20 41 41 41 41  #AAAA where AAAA
3342c 20 69 73 20 61 20 70 61 72 61 6d 65 74 65 72 20   is a parameter 
3342d 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23  name. */.    }.#
3342e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3342f 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a  IT_TCL_VARIABLE.
33430 20 20 20 20 63 61 73 65 20 27 24 27 3a 0a 23 65      case '$':.#e
33431 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 40  ndif.    case '@
33432 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61  ':  /* For compa
33433 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d 53  tibility with MS
33434 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20   SQL Server */. 
33435 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20     case ':': {. 
33436 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a       int n = 0;.
33437 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33438 7a 5b 30 5d 3d 3d 27 24 27 20 29 3b 20 20 74 65  z[0]=='$' );  te
33439 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 40  stcase( z[0]=='@
3343a 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20  ' );  testcase( 
3343b 7a 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20  z[0]==':' );.   
3343c 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3343d 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20 20  TK_VARIABLE;.   
3343e 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a     for(i=1; (c=z
3343f 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
33440 20 20 20 20 20 20 20 69 66 28 20 49 64 43 68 61         if( IdCha
33441 72 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  r(c) ){.        
33442 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53    n++;.#ifndef S
33443 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56  QLITE_OMIT_TCL_V
33444 41 52 49 41 42 4c 45 0a 20 20 20 20 20 20 20 20  ARIABLE.        
33445 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 28 27  }else if( c=='('
33446 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20 20 20 20   && n>0 ){.     
33447 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20       do{.       
33448 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
33449 20 20 20 20 7d 77 68 69 6c 65 28 20 28 63 3d 7a      }while( (c=z
3344a 5b 69 5d 29 21 3d 30 20 26 26 20 21 73 71 6c 69  [i])!=0 && !sqli
3344b 74 65 33 49 73 73 70 61 63 65 28 63 29 20 26 26  te3Isspace(c) &&
3344c 20 63 21 3d 27 29 27 20 29 3b 0a 20 20 20 20 20   c!=')' );.     
3344d 20 20 20 20 20 69 66 28 20 63 3d 3d 27 29 27 20       if( c==')' 
3344e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
3344f 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
33450 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
33451 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
33452 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
33453 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
33454 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
33455 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3a 27 20  else if( c==':' 
33456 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29  && z[i+1]==':' )
33457 7b 0a 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b  {.          i++;
33458 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
33459 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
3345a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3345b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
3345c 69 66 28 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65  if( n==0 ) *toke
3345d 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
3345e 41 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  AL;.      return
3345f 20 69 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65   i;.    }.#ifnde
33460 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c  f SQLITE_OMIT_BL
33461 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63  OB_LITERAL.    c
33462 61 73 65 20 27 78 27 3a 20 63 61 73 65 20 27 58  ase 'x': case 'X
33463 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  ': {.      testc
33464 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 78 27 20 29  ase( z[0]=='x' )
33465 3b 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d  ; testcase( z[0]
33466 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20 69  =='X' );.      i
33467 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b  f( z[1]=='\'' ){
33468 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33469 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a 20  ype = TK_BLOB;. 
3346a 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b 20         for(i=2; 
3346b 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63  (c=z[i])!=0 && c
3346c 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20 20  !='\''; i++){.  
3346d 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c          if( !sql
3346e 69 74 65 33 49 73 78 64 69 67 69 74 28 63 29 20  ite3Isxdigit(c) 
3346f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ){.            *
33470 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
33471 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
33472 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
33473 20 20 20 20 20 20 69 66 28 20 69 25 32 20 7c 7c        if( i%2 ||
33474 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65   !c ) *tokenType
33475 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
33476 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20 69         if( c ) i
33477 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ++;.        retu
33478 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn i;.      }.  
33479 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
3347a 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
3347b 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a   the next case *
3347c 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  /.    }.#endif. 
3347d 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
3347e 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72 28      if( !IdChar(
3347f 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  *z) ){.        b
33480 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
33481 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64 43      for(i=1; IdC
33482 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  har(z[i]); i++){
33483 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
33484 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65  pe = keywordCode
33485 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a 20  ((char*)z, i);. 
33486 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
33487 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65     }.  }.  *toke
33488 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
33489 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  AL;.  return 1;.
3348a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65  }../*.** Run the
3348b 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20 67   parser on the g
3348c 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67 2e  iven SQL string.
3348d 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74 72    The parser str
3348e 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61 73  ucture is.** pas
3348f 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c 49  sed in.  An SQLI
33490 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65 20  TE_ status code 
33491 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  is returned.  If
33492 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
33493 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61 6e 64 20  .** then an and 
33494 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
33495 74 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f  to write an erro
33496 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 0a  r message into .
33497 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  ** memory obtain
33498 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
33499 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 6f 20  malloc() and to 
3349a 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20 70  make *pzErrMsg p
3349b 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20  oint to that.** 
3349c 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
3349d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3349e 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50   int sqlite3RunP
3349f 61 72 73 65 72 28 50 61 72 73 65 20 2a 70 50 61  arser(Parse *pPa
334a0 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rse, const char 
334a1 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a  *zSql, char **pz
334a2 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 6e  ErrMsg){.  int n
334a3 45 72 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Err = 0;        
334a4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
334a5 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65  mber of errors e
334a6 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20  ncountered */.  
334a7 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
334a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334a9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
334aa 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 67 69  */.  void *pEngi
334ab 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ne;             
334ac 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 45 4d 4f       /* The LEMO
334ad 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c 41 4c 52  N-generated LALR
334ae 28 31 29 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  (1) parser */.  
334af 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 20 20  int tokenType;  
334b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334b1 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 6e  /* type of the n
334b2 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ext token */.  i
334b3 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73  nt lastTokenPars
334b4 65 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f  ed = -1;       /
334b5 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 70 72  * type of the pr
334b6 65 76 69 6f 75 73 20 74 6f 6b 65 6e 20 2a 2f 0a  evious token */.
334b7 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61    u8 enableLooka
334b8 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  side;           
334b9 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
334ba 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64   of db->lookasid
334bb 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  e.bEnabled */.  
334bc 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
334bd 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20  arse->db;       
334be 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
334bf 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
334c0 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 3b 20 20 20  int mxSqlLen;   
334c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334c2 2f 2a 20 4d 61 78 20 6c 65 6e 67 74 68 20 6f 66  /* Max length of
334c3 20 61 6e 20 53 51 4c 20 73 74 72 69 6e 67 20 2a   an SQL string *
334c4 2f 0a 0a 0a 20 20 6d 78 53 71 6c 4c 65 6e 20 3d  /...  mxSqlLen =
334c5 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
334c6 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e  TE_LIMIT_SQL_LEN
334c7 47 54 48 5d 3b 0a 20 20 69 66 28 20 64 62 2d 3e  GTH];.  if( db->
334c8 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30  activeVdbeCnt==0
334c9 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69   ){.    db->u1.i
334ca 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30  sInterrupted = 0
334cb 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  ;.  }.  pParse->
334cc 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
334cd 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
334ce 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30 3b  = zSql;.  i = 0;
334cf 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72 72  .  assert( pzErr
334d0 4d 73 67 21 3d 30 20 29 3b 0a 20 20 70 45 6e 67  Msg!=0 );.  pEng
334d1 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61 72  ine = sqlite3Par
334d2 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a 28  serAlloc((void*(
334d3 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69 74  *)(size_t))sqlit
334d4 65 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20 69 66 28  e3Malloc);.  if(
334d5 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a 20   pEngine==0 ){. 
334d6 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
334d7 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  led = 1;.    ret
334d8 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
334d9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
334da 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
334db 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e==0 );.  assert
334dc 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  ( pParse->pNewTr
334dd 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73  igger==0 );.  as
334de 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56  sert( pParse->nV
334df 61 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ar==0 );.  asser
334e0 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  t( pParse->nVarE
334e1 78 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  xpr==0 );.  asse
334e2 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  rt( pParse->nVar
334e3 45 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a  ExprAlloc==0 );.
334e4 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
334e5 2d 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20 29  ->apVarExpr==0 )
334e6 3b 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73  ;.  enableLookas
334e7 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ide = db->lookas
334e8 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20  ide.bEnabled;.  
334e9 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  if( db->lookasid
334ea 65 2e 70 53 74 61 72 74 20 29 20 64 62 2d 3e 6c  e.pStart ) db->l
334eb 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
334ec 64 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  d = 1;.  while( 
334ed 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
334ee 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20  d && zSql[i]!=0 
334ef 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
334f0 3e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73  >=0 );.    pPars
334f1 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20  e->sLastToken.z 
334f2 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20  = &zSql[i];.    
334f3 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b  pParse->sLastTok
334f4 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47 65  en.n = sqlite3Ge
334f5 74 54 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64  tToken((unsigned
334f6 20 63 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c   char*)&zSql[i],
334f7 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20  &tokenType);.   
334f8 20 69 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c   i += pParse->sL
334f9 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20  astToken.n;.    
334fa 69 66 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29  if( i>mxSqlLen )
334fb 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
334fc 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42  rc = SQLITE_TOOB
334fd 49 47 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  IG;.      break;
334fe 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69 74 63  .    }.    switc
334ff 68 28 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a  h( tokenType ){.
33500 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 50        case TK_SP
33501 41 43 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ACE: {.        i
33502 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  f( db->u1.isInte
33503 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 20  rrupted ){.     
33504 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
33505 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
33506 74 65 72 72 75 70 74 22 29 3b 0a 20 20 20 20 20  terrupt");.     
33507 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
33508 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
33509 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
3350a 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a  to abort_parse;.
3350b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3350c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3350d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 49  .      case TK_I
3350e 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20 20 20 20 20  LLEGAL: {.      
3350f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33510 64 62 2c 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0a  db, *pzErrMsg);.
33511 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73          *pzErrMs
33512 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  g = sqlite3MPrin
33513 74 66 28 64 62 2c 20 22 75 6e 72 65 63 6f 67 6e  tf(db, "unrecogn
33514 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54  ized token: \"%T
33515 5c 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \"",.           
33516 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 50               &pP
33517 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
33518 29 3b 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b  );.        nErr+
33519 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  +;.        goto 
3351a 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20  abort_parse;.   
3351b 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
3351c 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20 20 20 20  TK_SEMI: {.     
3351d 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c     pParse->zTail
3351e 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20   = &zSql[i];.   
3351f 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72       /* Fall thr
33520 75 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75  u into the defau
33521 6c 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20  lt case */.     
33522 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
33523 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
33524 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e  te3Parser(pEngin
33525 65 2c 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50  e, tokenType, pP
33526 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
33527 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  , pParse);.     
33528 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73     lastTokenPars
33529 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0a  ed = tokenType;.
3352a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 72          if( pPar
3352b 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se->rc!=SQLITE_O
3352c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  K ){.          g
3352d 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b  oto abort_parse;
3352e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3352f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33530 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 61 62 6f 72  }.    }.  }.abor
33531 74 5f 70 61 72 73 65 3a 0a 20 20 69 66 28 20 7a  t_parse:.  if( z
33532 53 71 6c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45 72  Sql[i]==0 && nEr
33533 72 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e  r==0 && pParse->
33534 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
33535 0a 20 20 20 20 69 66 28 20 6c 61 73 74 54 6f 6b  .    if( lastTok
33536 65 6e 50 61 72 73 65 64 21 3d 54 4b 5f 53 45 4d  enParsed!=TK_SEM
33537 49 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  I ){.      sqlit
33538 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65  e3Parser(pEngine
33539 2c 20 54 4b 5f 53 45 4d 49 2c 20 70 50 61 72 73  , TK_SEMI, pPars
3353a 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70  e->sLastToken, p
3353b 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 50  Parse);.      pP
3353c 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a  arse->zTail = &z
3353d 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  Sql[i];.    }.  
3353e 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28    sqlite3Parser(
3353f 70 45 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61 72  pEngine, 0, pPar
33540 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20  se->sLastToken, 
33541 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 69 66  pParse);.  }.#if
33542 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54  def YYTRACKMAXST
33543 41 43 4b 44 45 50 54 48 0a 20 20 73 71 6c 69 74  ACKDEPTH.  sqlit
33544 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
33545 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52  TE_STATUS_PARSER
33546 5f 53 54 41 43 4b 2c 0a 20 20 20 20 20 20 73 71  _STACK,.      sq
33547 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63 6b  lite3ParserStack
33548 50 65 61 6b 28 70 45 6e 67 69 6e 65 29 0a 20 20  Peak(pEngine).  
33549 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 44  );.#endif /* YYD
3354a 45 42 55 47 20 2a 2f 0a 20 20 73 71 6c 69 74 65  EBUG */.  sqlite
3354b 33 50 61 72 73 65 72 46 72 65 65 28 70 45 6e 67  3ParserFree(pEng
3354c 69 6e 65 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  ine, sqlite3_fre
3354d 65 29 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  e);.  db->lookas
3354e 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65  ide.bEnabled = e
3354f 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a  nableLookaside;.
33550 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
33551 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 50  Failed ){.    pP
33552 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
33553 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
33554 66 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53  f( pParse->rc!=S
33555 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 72  QLITE_OK && pPar
33556 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44  se->rc!=SQLITE_D
33557 4f 4e 45 20 26 26 20 70 50 61 72 73 65 2d 3e 7a  ONE && pParse->z
33558 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20  ErrMsg==0 ){.   
33559 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
3355a 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  g(&pParse->zErrM
3355b 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
3355c 6c 69 74 65 33 45 72 72 53 74 72 28 70 50 61 72  lite3ErrStr(pPar
3355d 73 65 2d 3e 72 63 29 29 3b 0a 20 20 7d 0a 20 20  se->rc));.  }.  
3355e 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67  assert( pzErrMsg
3355f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61  !=0 );.  if( pPa
33560 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a  rse->zErrMsg ){.
33561 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
33562 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b  pParse->zErrMsg;
33563 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72  .    pParse->zEr
33564 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 6e 45  rMsg = 0;.    nE
33565 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rr++;.  }.  if( 
33566 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26 26  pParse->pVdbe &&
33567 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20   pParse->nErr>0 
33568 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  && pParse->neste
33569 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
3356a 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 50  te3VdbeDelete(pP
3356b 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
3356c 20 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20    pParse->pVdbe 
3356d 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  = 0;.  }.#ifndef
3356e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
3356f 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20  RED_CACHE.  if( 
33570 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
33571 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
33572 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
33573 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a  e->aTableLock);.
33574 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 61 62      pParse->aTab
33575 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  leLock = 0;.    
33576 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
33577 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  ck = 0;.  }.#end
33578 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
33579 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
3357a 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 44 62 46  BLE.  sqlite3DbF
3357b 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
3357c 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a 23 65 6e  apVtabLock);.#en
3357d 64 69 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f 44  dif..  if( !IN_D
3357e 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
3357f 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 50 61     /* If the pPa
33580 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62  rse->declareVtab
33581 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 64 6f   flag is set, do
33582 20 6e 6f 74 20 64 65 6c 65 74 65 20 61 6e 79 20   not delete any 
33583 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73 74  table .    ** st
33584 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 70  ructure built up
33585 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77   in pParse->pNew
33586 54 61 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69  Table. The calli
33587 6e 67 20 63 6f 64 65 20 28 73 65 65 20 76 74 61  ng code (see vta
33588 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c 6c  b.c).    ** will
33589 20 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62 69   take responsibi
3358a 6c 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e 67  lity for freeing
3358b 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
3358c 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ture..    */.   
3358d 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
3358e 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77  ble(pParse->pNew
3358f 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 73  Table);.  }..  s
33590 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
33591 67 65 72 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ger(db, pParse->
33592 70 4e 65 77 54 72 69 67 67 65 72 29 3b 0a 20 20  pNewTrigger);.  
33593 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
33594 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45  , pParse->apVarE
33595 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  xpr);.  sqlite3D
33596 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
33597 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20 77 68 69  ->aAlias);.  whi
33598 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 41 69 6e  le( pParse->pAin
33599 63 20 29 7b 0a 20 20 20 20 41 75 74 6f 69 6e 63  c ){.    Autoinc
3359a 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61 72 73 65  Info *p = pParse
3359b 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 70 50 61  ->pAinc;.    pPa
3359c 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20 70 2d 3e  rse->pAinc = p->
3359d 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
3359e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
3359f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 50  .  }.  while( pP
335a0 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62  arse->pZombieTab
335a1 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
335a2 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62   = pParse->pZomb
335a3 69 65 54 61 62 3b 0a 20 20 20 20 70 50 61 72 73  ieTab;.    pPars
335a4 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20  e->pZombieTab = 
335a5 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a  p->pNextZombie;.
335a6 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
335a7 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20  eTable(p);.  }. 
335a8 20 69 66 28 20 6e 45 72 72 3e 30 20 26 26 20 70   if( nErr>0 && p
335a9 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
335aa 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72  E_OK ){.    pPar
335ab 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
335ac 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74  ERROR;.  }.  ret
335ad 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a  urn nErr;.}../**
335ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
335af 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a   of tokenize.c *
335b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
335b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
335b4 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c 65 74 65  in file complete
335b5 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
335b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
335b8 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
335b9 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
335ba 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
335bb 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
335bc 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
335bd 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
335be 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
335bf 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
335c0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
335c1 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
335c2 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
335c3 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
335c4 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
335c5 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
335c6 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
335c7 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
335c8 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
335c9 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
335ca 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
335cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335cf 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69  ***.** An tokeni
335d0 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a  zer for SQL.**.*
335d1 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
335d2 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74  ains C code that
335d3 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
335d4 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
335d5 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68 69 73 20  () API..** This 
335d6 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 65 20  code used to be 
335d7 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b 65  part of the toke
335d8 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63 65 20 66  nizer.c source f
335d9 69 6c 65 2e 20 20 42 75 74 20 62 79 0a 2a 2a 20  ile.  But by.** 
335da 73 65 70 61 72 61 74 69 6e 67 20 69 74 20 6f 75  separating it ou
335db 74 2c 20 74 68 65 20 63 6f 64 65 20 77 69 6c 6c  t, the code will
335dc 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
335dd 79 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a  y omitted from.*
335de 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b 73 20 74  * static links t
335df 68 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20 69  hat do not use i
335e0 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  t..*/.#ifndef SQ
335e1 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45  LITE_OMIT_COMPLE
335e2 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  TE../*.** This i
335e3 73 20 64 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b  s defined in tok
335e4 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73  enize.c.  We jus
335e5 74 20 68 61 76 65 20 74 6f 20 69 6d 70 6f 72 74  t have to import
335e6 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e   the definition.
335e7 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
335e8 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
335e9 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
335ea 43 49 49 0a 23 64 65 66 69 6e 65 20 49 64 43 68  CII.#define IdCh
335eb 61 72 28 43 29 20 20 28 28 73 71 6c 69 74 65 33  ar(C)  ((sqlite3
335ec 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e  CtypeMap[(unsign
335ed 65 64 20 63 68 61 72 29 43 5d 26 30 78 34 36 29  ed char)C]&0x46)
335ee 21 3d 30 29 0a 23 65 6e 64 69 66 0a 23 69 66 64  !=0).#endif.#ifd
335ef 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ef SQLITE_EBCDIC
335f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
335f1 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
335f2 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72  e3IsEbcdicIdChar
335f3 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68  [];.#define IdCh
335f4 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e 3d  ar(C)  (((c=C)>=
335f5 30 78 34 32 20 26 26 20 73 71 6c 69 74 65 33 49  0x42 && sqlite3I
335f6 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 63 2d  sEbcdicIdChar[c-
335f7 30 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23  0x40])).#endif.#
335f8 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
335f9 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a  AMALGAMATION */.
335fa 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79  ../*.** Token ty
335fb 70 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  pes used by the 
335fc 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
335fd 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53 65 65  () routine.  See
335fe 20 74 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 63   the header.** c
335ff 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 61 74 20  omments on that 
33600 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 64  procedure for ad
33601 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
33602 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
33603 20 74 6b 53 45 4d 49 20 20 20 20 30 0a 23 64 65   tkSEMI    0.#de
33604 66 69 6e 65 20 74 6b 57 53 20 20 20 20 20 20 31  fine tkWS      1
33605 0a 23 64 65 66 69 6e 65 20 74 6b 4f 54 48 45 52  .#define tkOTHER
33606 20 20 20 32 0a 23 64 65 66 69 6e 65 20 74 6b 45     2.#define tkE
33607 58 50 4c 41 49 4e 20 33 0a 23 64 65 66 69 6e 65  XPLAIN 3.#define
33608 20 74 6b 43 52 45 41 54 45 20 20 34 0a 23 64 65   tkCREATE  4.#de
33609 66 69 6e 65 20 74 6b 54 45 4d 50 20 20 20 20 35  fine tkTEMP    5
3360a 0a 23 64 65 66 69 6e 65 20 74 6b 54 52 49 47 47  .#define tkTRIGG
3360b 45 52 20 36 0a 23 64 65 66 69 6e 65 20 74 6b 45  ER 6.#define tkE
3360c 4e 44 20 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20  ND     7../*.** 
3360d 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
3360e 68 65 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72  he given SQL str
3360f 69 6e 67 20 65 6e 64 73 20 69 6e 20 61 20 73 65  ing ends in a se
33610 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53  micolon..**.** S
33611 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20  pecial handling 
33612 69 73 20 72 65 71 75 69 72 65 20 66 6f 72 20 43  is require for C
33613 52 45 41 54 45 20 54 52 49 47 47 45 52 20 73 74  REATE TRIGGER st
33614 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65  atements..** Whe
33615 6e 65 76 65 72 20 74 68 65 20 43 52 45 41 54 45  never the CREATE
33616 20 54 52 49 47 47 45 52 20 6b 65 79 77 6f 72 64   TRIGGER keyword
33617 73 20 61 72 65 20 73 65 65 6e 2c 20 74 68 65 20  s are seen, the 
33618 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73  statement.** mus
33619 74 20 65 6e 64 20 77 69 74 68 20 22 3b 45 4e 44  t end with ";END
3361a 3b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ;"..**.** This i
3361b 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73  mplementation us
3361c 65 73 20 61 20 73 74 61 74 65 20 6d 61 63 68 69  es a state machi
3361d 6e 65 20 77 69 74 68 20 37 20 73 74 61 74 65 73  ne with 7 states
3361e 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54  :.**.**   (0) ST
3361f 41 52 54 20 20 20 20 20 41 74 20 74 68 65 20 62  ART     At the b
33620 65 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20  eginning or end 
33621 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  of an SQL statem
33622 65 6e 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ent.  This routi
33623 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ne.**           
33624 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 31 20        returns 1 
33625 69 66 20 69 74 20 65 6e 64 73 20 69 6e 20 74 68  if it ends in th
33626 65 20 53 54 41 52 54 20 73 74 61 74 65 20 61 6e  e START state an
33627 64 20 30 20 69 66 20 69 74 20 65 6e 64 73 0a 2a  d 0 if it ends.*
33628 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
33629 20 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 73    in any other s
3362a 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31  tate..**.**   (1
3362b 29 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65 20 61  ) NORMAL    We a
3362c 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  re in the middle
3362d 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 77 68   of statement wh
3362e 69 63 68 20 65 6e 64 73 20 77 69 74 68 20 61 20  ich ends with a 
3362f 73 69 6e 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20  single.**       
33630 20 20 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f            semico
33631 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29  lon..**.**   (2)
33632 20 45 58 50 4c 41 49 4e 20 20 20 54 68 65 20 6b   EXPLAIN   The k
33633 65 79 77 6f 72 64 20 45 58 50 4c 41 49 4e 20 68  eyword EXPLAIN h
33634 61 73 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20  as been seen at 
33635 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
33636 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
33637 20 20 20 20 20 61 20 73 74 61 74 65 6d 65 6e 74       a statement
33638 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52  ..**.**   (3) CR
33639 45 41 54 45 20 20 20 20 54 68 65 20 6b 65 79 77  EATE    The keyw
3363a 6f 72 64 20 43 52 45 41 54 45 20 68 61 73 20 62  ord CREATE has b
3363b 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20  een seen at the 
3363c 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a  beginning of a.*
3363d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3363e 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73    statement, pos
3363f 73 69 62 6c 79 20 70 72 65 63 65 65 64 65 64 20  sibly preceeded 
33640 62 79 20 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f  by EXPLAIN and/o
33641 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a  r followed by.**
33642 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33643 20 54 45 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41   TEMP or TEMPORA
33644 52 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54  RY.**.**   (4) T
33645 52 49 47 47 45 52 20 20 20 57 65 20 61 72 65 20  RIGGER   We are 
33646 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
33647 20 61 20 74 72 69 67 67 65 72 20 64 65 66 69 6e   a trigger defin
33648 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20  ition that must 
33649 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  be.**           
3364a 20 20 20 20 20 20 65 6e 64 65 64 20 62 79 20 61        ended by a
3364b 20 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20   semicolon, the 
3364c 6b 65 79 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64  keyword END, and
3364d 20 61 6e 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c   another semicol
3364e 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20  on..**.**   (5) 
3364f 53 45 4d 49 20 20 20 20 20 20 57 65 27 76 65 20  SEMI      We've 
33650 73 65 65 6e 20 74 68 65 20 66 69 72 73 74 20 73  seen the first s
33651 65 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20  emicolon in the 
33652 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63  ";END;" that occ
33653 75 72 73 20 61 74 0a 2a 2a 20 20 20 20 20 20 20  urs at.**       
33654 20 20 20 20 20 20 20 20 20 20 74 68 65 20 65 6e            the en
33655 64 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64  d of a trigger d
33656 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  efinition..**.**
33657 20 20 20 28 36 29 20 45 4e 44 20 20 20 20 20 20     (6) END      
33658 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65 20   We've seen the 
33659 22 3b 45 4e 44 22 20 6f 66 20 74 68 65 20 22 3b  ";END" of the ";
3365a 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72  END;" that occur
3365b 73 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  s at the end.** 
3365c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3365d 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 69 66  of a trigger dif
3365e 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  inition..**.** T
3365f 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65  ransitions betwe
33660 65 6e 20 73 74 61 74 65 73 20 61 62 6f 76 65 20  en states above 
33661 61 72 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62  are determined b
33662 79 20 74 6f 6b 65 6e 73 20 65 78 74 72 61 63 74  y tokens extract
33663 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69  ed.** from the i
33664 6e 70 75 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  nput.  The follo
33665 77 69 6e 67 20 74 6f 6b 65 6e 73 20 61 72 65 20  wing tokens are 
33666 73 69 67 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a  significant:.**.
33667 2a 2a 20 20 20 28 30 29 20 74 6b 53 45 4d 49 20  **   (0) tkSEMI 
33668 20 20 20 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e       A semicolon
33669 2e 0a 2a 2a 20 20 20 28 31 29 20 74 6b 57 53 20  ..**   (1) tkWS 
3366a 20 20 20 20 20 20 20 57 68 69 74 65 73 70 61 63         Whitespac
3366b 65 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f 54 48  e.**   (2) tkOTH
3366c 45 52 20 20 20 20 20 41 6e 79 20 6f 74 68 65 72  ER     Any other
3366d 20 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20   SQL token..**  
3366e 20 28 33 29 20 74 6b 45 58 50 4c 41 49 4e 20 20   (3) tkEXPLAIN  
3366f 20 54 68 65 20 22 65 78 70 6c 61 69 6e 22 20 6b   The "explain" k
33670 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29  eyword..**   (4)
33671 20 74 6b 43 52 45 41 54 45 20 20 20 20 54 68 65   tkCREATE    The
33672 20 22 63 72 65 61 74 65 22 20 6b 65 79 77 6f 72   "create" keywor
33673 64 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b 54 45  d..**   (5) tkTE
33674 4d 50 20 20 20 20 20 20 54 68 65 20 22 74 65 6d  MP      The "tem
33675 70 22 20 6f 72 20 22 74 65 6d 70 6f 72 61 72 79  p" or "temporary
33676 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20  " keyword..**   
33677 28 36 29 20 74 6b 54 52 49 47 47 45 52 20 20 20  (6) tkTRIGGER   
33678 54 68 65 20 22 74 72 69 67 67 65 72 22 20 6b 65  The "trigger" ke
33679 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20  yword..**   (7) 
3367a 74 6b 45 4e 44 20 20 20 20 20 20 20 54 68 65 20  tkEND       The 
3367b 22 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a  "end" keyword..*
3367c 2a 0a 2a 2a 20 57 68 69 74 65 73 70 61 63 65 20  *.** Whitespace 
3367d 6e 65 76 65 72 20 63 61 75 73 65 73 20 61 20 73  never causes a s
3367e 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 20  tate transition 
3367f 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20 69 67  and is always ig
33680 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nored..**.** If 
33681 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  we compile with 
33682 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
33683 47 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20  GER, all of the 
33684 63 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65 65 64  computation need
33685 65 64 0a 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69  ed.** to recogni
33686 7a 65 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ze the end of a 
33687 74 72 69 67 67 65 72 20 63 61 6e 20 62 65 20 6f  trigger can be o
33688 6d 69 74 74 65 64 2e 20 20 41 6c 6c 20 77 65 20  mitted.  All we 
33689 68 61 76 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73  have to do.** is
3368a 20 6c 6f 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69   look for a semi
3368b 63 6f 6c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f  colon that is no
3368c 74 20 70 61 72 74 20 6f 66 20 61 6e 20 73 74 72  t part of an str
3368d 69 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a  ing or comment..
3368e 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3368f 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  t sqlite3_comple
33690 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  te(const char *z
33691 53 71 6c 29 7b 0a 20 20 75 38 20 73 74 61 74 65  Sql){.  u8 state
33692 20 3d 20 30 3b 20 20 20 2f 2a 20 43 75 72 72 65   = 0;   /* Curre
33693 6e 74 20 73 74 61 74 65 2c 20 75 73 69 6e 67 20  nt state, using 
33694 6e 75 6d 62 65 72 73 20 64 65 66 69 6e 65 64 20  numbers defined 
33695 69 6e 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  in header commen
33696 74 20 2a 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b  t */.  u8 token;
33697 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
33698 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  of the next toke
33699 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  n */..#ifndef SQ
3369a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
3369b 52 0a 20 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78  R.  /* A complex
3369c 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69   statement machi
3369d 6e 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 63  ne used to detec
3369e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 43  t the end of a C
3369f 52 45 41 54 45 20 54 52 49 47 47 45 52 0a 20 20  REATE TRIGGER.  
336a0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  ** statement.  T
336a1 68 69 73 20 69 73 20 74 68 65 20 6e 6f 72 6d 61  his is the norma
336a2 6c 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73  l case..  */.  s
336a3 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 74  tatic const u8 t
336a4 72 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20  rans[7][8] = {. 
336a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336a6 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20      /* Token:   
336a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a               */.
336aa 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20       /* State:  
336ab 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57       **  SEMI  W
336ac 53 20 20 4f 54 48 45 52 20 45 58 50 4c 41 49 4e  S  OTHER EXPLAIN
336ad 20 20 43 52 45 41 54 45 20 20 54 45 4d 50 20 20    CREATE  TEMP  
336ae 54 52 49 47 47 45 52 20 20 45 4e 44 20 20 2a 2f  TRIGGER  END  */
336af 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54 41  .     /* 0   STA
336b0 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  RT: */ {    0,  
336b1 30 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 32  0,     1,      2
336b2 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20  ,      3,    1, 
336b3 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d        1,   1,  }
336b4 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52  ,.     /* 1  NOR
336b5 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  MAL: */ {    0, 
336b6 20 31 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20   1,     1,      
336b7 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 31 2c  1,      1,    1,
336b8 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20         1,   1,  
336b9 7d 2c 0a 20 20 20 20 20 2f 2a 20 32 20 45 58 50  },.     /* 2 EXP
336ba 4c 41 49 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  LAIN: */ {    0,
336bb 20 20 32 2c 20 20 20 20 20 32 2c 20 20 20 20 20    2,     2,     
336bc 20 31 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31   1,      3,    1
336bd 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20  ,       1,   1, 
336be 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 33 20 20 43   },.     /* 3  C
336bf 52 45 41 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30  REATE: */ {    0
336c0 2c 20 20 33 2c 20 20 20 20 20 31 2c 20 20 20 20  ,  3,     1,    
336c1 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
336c2 33 2c 20 20 20 20 20 20 20 34 2c 20 20 20 31 2c  3,       4,   1,
336c3 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54    },.     /* 4 T
336c4 52 49 47 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20  RIGGER: */ {    
336c5 35 2c 20 20 34 2c 20 20 20 20 20 34 2c 20 20 20  5,  4,     4,   
336c6 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
336c7 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34   4,       4,   4
336c8 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20  ,  },.     /* 5 
336c9 20 20 20 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20     SEMI: */ {   
336ca 20 35 2c 20 20 35 2c 20 20 20 20 20 34 2c 20 20   5,  5,     4,  
336cb 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
336cc 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20    4,       4,   
336cd 36 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36  6,  },.     /* 6
336ce 20 20 20 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20       END: */ {  
336cf 20 20 30 2c 20 20 36 2c 20 20 20 20 20 34 2c 20    0,  6,     4, 
336d0 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
336d1 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20     4,       4,  
336d2 20 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c   4,  },.  };.#el
336d3 73 65 0a 20 20 2f 2a 20 49 66 20 74 72 69 67 67  se.  /* If trigg
336d4 65 72 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70  ers are not supp
336d5 6f 72 65 64 20 62 79 20 74 68 69 73 20 63 6f 6d  ored by this com
336d6 70 69 6c 65 20 74 68 65 6e 20 74 68 65 20 73 74  pile then the st
336d7 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a  atement machine.
336d8 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74    ** used to det
336d9 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61  ect the end of a
336da 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6d 75   statement is mu
336db 63 68 20 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f  ch simplier.  */
336dc 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
336dd 75 38 20 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d  u8 trans[2][3] =
336de 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
336df 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
336e0 3a 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  :           */. 
336e1 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20      /* State:   
336e2 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53      **  SEMI  WS
336e3 20 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20 20 20    OTHER */.     
336e4 2f 2a 20 30 20 20 20 53 54 41 52 54 3a 20 2a 2f  /* 0   START: */
336e5 20 7b 20 20 20 20 30 2c 20 20 30 2c 20 20 20 20   {    0,  0,    
336e6 20 31 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31   1, },.     /* 1
336e7 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20    NORMAL: */ {  
336e8 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31 2c 20    0,  1,     1, 
336e9 7d 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f  },.  };.#endif /
336ea 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
336eb 49 47 47 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c  IGGER */..  whil
336ec 65 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20  e( *zSql ){.    
336ed 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b  switch( *zSql ){
336ee 0a 20 20 20 20 20 20 63 61 73 65 20 27 3b 27 3a  .      case ';':
336ef 20 7b 20 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c   {  /* A semicol
336f0 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f  on */.        to
336f1 6b 65 6e 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20  ken = tkSEMI;.  
336f2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
336f3 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
336f4 27 20 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ' ':.      case 
336f5 27 5c 72 27 3a 0a 20 20 20 20 20 20 63 61 73 65  '\r':.      case
336f6 20 27 5c 74 27 3a 0a 20 20 20 20 20 20 63 61 73   '\t':.      cas
336f7 65 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61  e '\n':.      ca
336f8 73 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57  se '\f': {  /* W
336f9 68 69 74 65 20 73 70 61 63 65 20 69 73 20 69 67  hite space is ig
336fa 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20  nored */.       
336fb 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20   token = tkWS;. 
336fc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
336fd 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
336fe 20 27 2f 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73   '/': {   /* C-s
336ff 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f  tyle comments */
33700 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71  .        if( zSq
33701 6c 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20  l[1]!='*' ){.   
33702 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
33703 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
33704 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
33705 20 7d 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 20   }.        zSql 
33706 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77 68  += 2;.        wh
33707 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20  ile( zSql[0] && 
33708 28 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c  (zSql[0]!='*' ||
33709 20 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29   zSql[1]!='/') )
3370a 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20  { zSql++; }.    
3370b 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d      if( zSql[0]=
3370c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
3370d 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20         zSql++;. 
3370e 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
3370f 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65  kWS;.        bre
33710 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
33711 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 20 20 20    case '-': {   
33712 2f 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d  /* SQL-style com
33713 6d 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20  ments from "--" 
33714 74 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a  to end of line *
33715 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53  /.        if( zS
33716 71 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20  ql[1]!='-' ){.  
33717 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
33718 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
33719 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3371a 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c    }.        whil
3371b 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71  e( *zSql && *zSq
3371c 6c 21 3d 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b  l!='\n' ){ zSql+
3371d 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  +; }.        if(
3371e 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75   *zSql==0 ) retu
3371f 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20  rn state==0;.   
33720 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57       token = tkW
33721 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  S;.        break
33722 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33723 63 61 73 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a  case '[': {   /*
33724 20 4d 69 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65   Microsoft-style
33725 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   identifiers in 
33726 5b 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20  [...] */.       
33727 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   zSql++;.       
33728 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26   while( *zSql &&
33729 20 2a 7a 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a   *zSql!=']' ){ z
3372a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
3372b 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20   if( *zSql==0 ) 
3372c 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
3372d 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3372e 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  R;.        break
3372f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33730 63 61 73 65 20 27 60 27 3a 20 20 20 20 20 2f 2a  case '`':     /*
33731 20 47 72 61 76 65 2d 61 63 63 65 6e 74 20 71 75   Grave-accent qu
33732 6f 74 65 64 20 73 79 6d 62 6f 6c 73 20 75 73 65  oted symbols use
33733 64 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20  d by MySQL */.  
33734 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20      case '"':   
33735 20 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64    /* single- and
33736 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73   double-quoted s
33737 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20  trings */.      
33738 63 61 73 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20  case '\'': {.   
33739 20 20 20 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53       int c = *zS
3373a 71 6c 3b 0a 20 20 20 20 20 20 20 20 7a 53 71 6c  ql;.        zSql
3373b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ++;.        whil
3373c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71  e( *zSql && *zSq
3373d 6c 21 3d 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20  l!=c ){ zSql++; 
3373e 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  }.        if( *z
3373f 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Sql==0 ) return 
33740 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  0;.        token
33741 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
33742 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
33743 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
33744 3a 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  : {.#ifdef SQLIT
33745 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20 20 20  E_EBCDIC.       
33746 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
33747 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
33748 20 69 66 28 20 49 64 43 68 61 72 28 28 75 38 29   if( IdChar((u8)
33749 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20 20 20  *zSql) ){.      
3374a 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 73 20      /* Keywords 
3374b 61 6e 64 20 75 6e 71 75 6f 74 65 64 20 69 64 65  and unquoted ide
3374c 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20 20 20 20  ntifiers */.    
3374d 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20        int nId;. 
3374e 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 49 64           for(nId
3374f 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71 6c 5b  =1; IdChar(zSql[
33750 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a  nId]); nId++){}.
33751 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
33752 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20  IT_TRIGGER.     
33753 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
33754 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20 20 20  THER;.#else.    
33755 20 20 20 20 20 20 73 77 69 74 63 68 28 20 2a 7a        switch( *z
33756 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Sql ){.         
33757 20 20 20 63 61 73 65 20 27 63 27 3a 20 63 61 73     case 'c': cas
33758 65 20 27 43 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'C': {.       
33759 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d         if( nId==
3375a 36 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  6 && sqlite3StrN
3375b 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72 65 61  ICmp(zSql, "crea
3375c 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20  te", 6)==0 ){.  
3375d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
3375e 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54 45 3b 0a  ken = tkCREATE;.
3375f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
33760 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
33761 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
33762 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
33763 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
33764 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
33765 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
33766 20 20 20 20 20 63 61 73 65 20 27 74 27 3a 20 63       case 't': c
33767 61 73 65 20 27 54 27 3a 20 7b 0a 20 20 20 20 20  ase 'T': {.     
33768 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64           if( nId
33769 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==7 && sqlite3St
3376a 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 72  rNICmp(zSql, "tr
3376b 69 67 67 65 72 22 2c 20 37 29 3d 3d 30 20 29 7b  igger", 7)==0 ){
3376c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3376d 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49 47 47   token = tkTRIGG
3376e 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ER;.            
3376f 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64 3d    }else if( nId=
33770 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =4 && sqlite3Str
33771 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d  NICmp(zSql, "tem
33772 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20  p", 4)==0 ){.   
33773 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
33774 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20  en = tkTEMP;.   
33775 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
33776 20 69 66 28 20 6e 49 64 3d 3d 39 20 26 26 20 73   if( nId==9 && s
33777 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
33778 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72 79 22  Sql, "temporary"
33779 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 9)==0 ){.     
3377a 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
3377b 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20   = tkTEMP;.     
3377c 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
3377d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3377e 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
3377f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
33780 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
33781 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
33782 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
33783 63 61 73 65 20 27 65 27 3a 20 20 63 61 73 65 20  case 'e':  case 
33784 27 45 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'E': {.         
33785 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 33 20       if( nId==3 
33786 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
33787 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22 2c 20  mp(zSql, "end", 
33788 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  3)==0 ){.       
33789 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
3378a 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20 20 20   tkEND;.        
3378b 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 69 66 6e        }else.#ifn
3378c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3378d 45 58 50 4c 41 49 4e 0a 20 20 20 20 20 20 20 20  EXPLAIN.        
3378e 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37        if( nId==7
3378f 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
33790 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70 6c 61  Cmp(zSql, "expla
33791 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20  in", 7)==0 ){.  
33792 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
33793 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49 4e 3b  ken = tkEXPLAIN;
33794 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
33795 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
33796 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20            {.    
33797 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
33798 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
33799 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
3379a 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3379b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
3379c 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61              defa
3379d 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  ult: {.         
3379e 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
3379f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
337a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
337a1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
337a2 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
337a3 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
337a4 45 52 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ER */.          
337a5 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20  zSql += nId-1;. 
337a6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
337a7 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61          /* Opera
337a8 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69 61 6c  tors and special
337a9 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20   symbols */.    
337aa 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
337ab 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 7d  OTHER;.        }
337ac 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
337ad 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
337ae 20 20 73 74 61 74 65 20 3d 20 74 72 61 6e 73 5b    state = trans[
337af 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20  state][token];. 
337b0 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20     zSql++;.  }. 
337b1 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d 30   return state==0
337b2 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
337b3 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
337b4 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
337b5 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
337b6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d   the sqlite3_com
337b7 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 20  plete() routine 
337b8 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 61 62 6f  described.** abo
337b9 76 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 20  ve, except that 
337ba 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  the parameter is
337bb 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20   required to be 
337bc 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 2c 20  UTF-16 encoded, 
337bd 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f  not.** UTF-8..*/
337be 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
337bf 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
337c0 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  16(const void *z
337c1 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Sql){.  sqlite3_
337c2 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 63  value *pVal;.  c
337c3 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 38  har const *zSql8
337c4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
337c5 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e  ITE_NOMEM;..#ifn
337c6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
337c7 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20  AUTOINIT.  rc = 
337c8 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
337c9 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
337ca 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
337cb 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69  if.  pVal = sqli
337cc 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a  te3ValueNew(0);.
337cd 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
337ce 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a  tStr(pVal, -1, z
337cf 53 71 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Sql, SQLITE_UTF1
337d0 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
337d1 53 54 41 54 49 43 29 3b 0a 20 20 7a 53 71 6c 38  STATIC);.  zSql8
337d2 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54   = sqlite3ValueT
337d3 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  ext(pVal, SQLITE
337d4 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a 53  _UTF8);.  if( zS
337d5 71 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ql8 ){.    rc = 
337d6 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
337d7 28 7a 53 71 6c 38 29 3b 0a 20 20 7d 65 6c 73 65  (zSql8);.  }else
337d8 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
337d9 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73  E_NOMEM;.  }.  s
337da 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
337db 70 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  pVal);.  return 
337dc 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30  sqlite3ApiExit(0
337dd 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  , rc);.}.#endif 
337de 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
337df 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  TF16 */.#endif /
337e0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
337e1 4d 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  MPLETE */../****
337e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
337e3 66 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a  f complete.c ***
337e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
337e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
337e8 20 66 69 6c 65 20 6d 61 69 6e 2e 63 20 2a 2a 2a   file main.c ***
337e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
337eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
337ec 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
337ed 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
337ee 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
337ef 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
337f0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
337f1 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
337f2 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
337f3 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
337f4 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
337f5 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
337f6 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
337f7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
337f8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
337f9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
337fa 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
337fb 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
337fc 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
337fd 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
337fe 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
337ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33801 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33802 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33803 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66  *.** Main file f
33804 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  or the SQLite li
33805 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74  brary.  The rout
33806 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c  ines in this fil
33807 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e.** implement t
33808 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e  he programmer in
33809 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c  terface to the l
3380a 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65  ibrary.  Routine
3380b 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69  s in.** other fi
3380c 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65  les are for inte
3380d 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69  rnal use by SQLi
3380e 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  te and should no
3380f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64  t be.** accessed
33810 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68 65   by users of the
33811 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 0a 23 69   library..*/..#i
33812 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
33813 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a 2a 2a 2a 2a  LE_FTS3./*******
33814 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
33815 66 74 73 33 2e 68 20 69 6e 20 74 68 65 20 6d 69  fts3.h in the mi
33816 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a  ddle of main.c *
33817 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33818 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
33819 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
3381a 6c 65 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a  le fts3.h ******
3381b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3381c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3381d 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
3381e 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20  06 Oct 10.**.** 
3381f 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
33820 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
33821 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
33822 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
33823 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
33824 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
33825 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
33826 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
33827 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
33828 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
33829 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
3382a 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
3382b 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
3382c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
3382d 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
3382e 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
3382f 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
33830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33832 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33833 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33834 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
33835 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
33836 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20  file is used by 
33837 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61  programs that wa
33838 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e  nt to link again
33839 73 74 20 74 68 65 0a 2a 2a 20 46 54 53 33 20 6c  st the.** FTS3 l
3383a 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20  ibrary.  All it 
3383b 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65 20  does is declare 
3383c 74 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 49  the sqlite3Fts3I
3383d 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  nit() interface.
3383e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72  .*/..#if 0.exter
3383f 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20  n "C" {.#endif  
33840 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
33841 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
33842 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
33843 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  3Init(sqlite3 *d
33844 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a  b);..#if 0.}  /*
33845 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23   extern "C" */.#
33846 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
33847 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  splus */../*****
33848 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
33849 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.h ********
3384a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3384b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3384c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
3384d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
3384e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
3384f 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
33850 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33851 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
33852 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
33853 4e 41 42 4c 45 5f 52 54 52 45 45 0a 2f 2a 2a 2a  NABLE_RTREE./***
33854 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
33855 75 64 65 20 72 74 72 65 65 2e 68 20 69 6e 20 74  ude rtree.h in t
33856 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69  he middle of mai
33857 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
33858 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
33859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
3385a 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 68 20 2a  n file rtree.h *
3385b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
3385e 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a  * 2008 May 26.**
3385f 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
33860 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
33861 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
33862 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
33863 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
33864 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
33865 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
33866 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
33867 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
33868 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
33869 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
3386a 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
3386b 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3386c 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3386d 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3386e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
3386f 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
33870 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
33871 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33875 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  *.**.** This hea
33876 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64  der file is used
33877 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61   by programs tha
33878 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61  t want to link a
33879 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 52 54  gainst the.** RT
3387a 52 45 45 20 6c 69 62 72 61 72 79 2e 20 20 41 6c  REE library.  Al
3387b 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63  l it does is dec
3387c 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33  lare the sqlite3
3387d 52 74 72 65 65 49 6e 69 74 28 29 20 69 6e 74 65  RtreeInit() inte
3387e 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30  rface..*/..#if 0
3387f 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65  .extern "C" {.#e
33880 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
33881 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  plus */..SQLITE_
33882 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
33883 74 65 33 52 74 72 65 65 49 6e 69 74 28 73 71 6c  te3RtreeInit(sql
33884 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20  ite3 *db);..#if 
33885 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22  0.}  /* extern "
33886 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a  C" */.#endif  /*
33887 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a   __cplusplus */.
33888 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33889 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 68 20   End of rtree.h 
3388a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3388b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3388c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3388d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3388e 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
3388f 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
33890 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
33891 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33892 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
33893 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
33894 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33895 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 69   Include sqlitei
33896 63 75 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  cu.h in the midd
33897 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a  le of main.c ***
33898 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33899 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3389a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
3389b 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teicu.h ********
3389c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3389d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3389e 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20  ./*.** 2008 May 
3389f 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  26.**.** The aut
338a0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
338a1 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
338a2 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
338a3 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
338a4 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
338a5 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
338a6 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
338a7 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
338a8 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
338a9 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
338aa 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
338ab 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
338ac 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
338ad 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
338ae 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
338af 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
338b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
338b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b5 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
338b6 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  s header file is
338b7 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d   used by program
338b8 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c  s that want to l
338b9 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a  ink against the.
338ba 2a 2a 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e  ** ICU extension
338bb 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69  .  All it does i
338bc 73 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71  s declare the sq
338bd 6c 69 74 65 33 49 63 75 49 6e 69 74 28 29 20 69  lite3IcuInit() i
338be 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69  nterface..*/..#i
338bf 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
338c0 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
338c1 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49  lusplus */..SQLI
338c2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
338c3 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73 71  qlite3IcuInit(sq
338c4 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66  lite3 *db);..#if
338c5 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20   0.}  /* extern 
338c6 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f  "C" */.#endif  /
338c7 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
338c8 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
338c9 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65  ** End of sqlite
338ca 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.h **********
338cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338cd 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
338ce 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
338cf 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
338d0 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  in main.c ******
338d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338d2 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  */.#endif../*.**
338d3 20 54 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20   The version of 
338d4 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2f 0a 23  the library.*/.#
338d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
338d6 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54  ALGAMATION.SQLIT
338d7 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
338d8 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
338d9 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53  [] = SQLITE_VERS
338da 49 4f 4e 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  ION;.#endif.SQLI
338db 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
338dc 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  r *sqlite3_libve
338dd 72 73 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65 74  rsion(void){ ret
338de 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 65 72 73  urn sqlite3_vers
338df 69 6f 6e 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50  ion; }.SQLITE_AP
338e0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
338e1 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76  lite3_sourceid(v
338e2 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
338e3 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 3b 20 7d  ITE_SOURCE_ID; }
338e4 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
338e5 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
338e6 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 7b  on_number(void){
338e7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 56   return SQLITE_V
338e8 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d  ERSION_NUMBER; }
338e9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
338ea 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61  sqlite3_threadsa
338eb 66 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  fe(void){ return
338ec 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
338ed 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64 65 66 69  FE; }..#if !defi
338ee 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
338ef 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65  TRACE) && define
338f0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
338f1 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 49  IOTRACE)./*.** I
338f2 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
338f3 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
338f4 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64   is not NULL and
338f5 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e   if.** SQLITE_EN
338f6 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 69 73 20  ABLE_IOTRACE is 
338f7 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 6d 65  enabled, then me
338f8 73 73 61 67 65 73 20 64 65 73 63 72 69 62 69 6e  ssages describin
338f9 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76 65 20  g.** I/O active 
338fa 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e  are written usin
338fb 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  g this function.
338fc 20 20 54 68 65 73 65 20 6d 65 73 73 61 67 65 73    These messages
338fd 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64  .** are intended
338fe 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61   for debugging a
338ff 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2f  ctivity only..*/
33900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33901 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49 6f  void (*sqlite3Io
33902 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68 61  Trace)(const cha
33903 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a 23 65  r*, ...) = 0;.#e
33904 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
33905 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
33906 62 61 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69  bal variable poi
33907 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20  nts to a string 
33908 77 68 69 63 68 20 69 73 20 74 68 65 0a 2a 2a 20  which is the.** 
33909 6e 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74  name of a direct
3390a 6f 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64  ory, then that d
3390b 69 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65  irectory will be
3390c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a   used to store.*
3390d 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
3390e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  s..**.** See als
3390f 6f 20 74 68 65 20 22 50 52 41 47 4d 41 20 74 65  o the "PRAGMA te
33910 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
33911 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e  ry" SQL command.
33912 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
33913 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d  har *sqlite3_tem
33914 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b  p_directory = 0;
33915 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
33916 7a 65 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a  ze SQLite.  .**.
33917 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
33918 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 74  must be called t
33919 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  o initialize the
3391a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3391b 6f 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61 6e 64 20  on,.** VFS, and 
3391c 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 73  mutex subsystems
3391d 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20   prior to doing 
3391e 61 6e 79 20 73 65 72 69 6f 75 73 20 77 6f 72 6b  any serious work
3391f 20 77 69 74 68 0a 2a 2a 20 53 51 4c 69 74 65 2e   with.** SQLite.
33920 20 20 42 75 74 20 61 73 20 6c 6f 6e 67 20 61 73    But as long as
33921 20 79 6f 75 20 64 6f 20 6e 6f 74 20 63 6f 6d 70   you do not comp
33922 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ile with SQLITE_
33923 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 2a 2a  OMIT_AUTOINIT.**
33924 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
33925 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 75 74  ll be called aut
33926 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 6b 65  omatically by ke
33927 79 20 72 6f 75 74 69 6e 65 73 20 73 75 63 68 20  y routines such 
33928 61 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70  as.** sqlite3_op
33929 65 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  en().  .**.** Th
3392a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
3392b 6e 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f 6e 20  no-op except on 
3392c 69 74 73 20 76 65 72 79 20 66 69 72 73 74 20 63  its very first c
3392d 61 6c 6c 20 66 6f 72 20 74 68 65 20 70 72 6f 63  all for the proc
3392e 65 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f 72 20 74  ess,.** or for t
3392f 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 61 66  he first call af
33930 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ter a call to sq
33931 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 2e 0a  lite3_shutdown..
33932 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
33933 74 68 72 65 61 64 20 74 6f 20 63 61 6c 6c 20 74  thread to call t
33934 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73  his routine runs
33935 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
33936 69 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65  ion to.** comple
33937 74 69 6f 6e 2e 20 20 49 66 20 73 75 62 73 65 71  tion.  If subseq
33938 75 65 6e 74 20 74 68 72 65 61 64 73 20 63 61 6c  uent threads cal
33939 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62  l this routine b
3393a 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a  efore the first.
3393b 2a 2a 20 74 68 72 65 61 64 20 68 61 73 20 66 69  ** thread has fi
3393c 6e 69 73 68 65 64 20 74 68 65 20 69 6e 69 74 69  nished the initi
3393d 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73  alization proces
3393e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 73  s, then the subs
3393f 65 71 75 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64  equent.** thread
33940 73 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74  s must block unt
33941 69 6c 20 74 68 65 20 66 69 72 73 74 20 74 68 72  il the first thr
33942 65 61 64 20 66 69 6e 69 73 68 65 73 20 77 69 74  ead finishes wit
33943 68 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  h the initializa
33944 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
33945 66 69 72 73 74 20 74 68 72 65 61 64 20 6d 69 67  first thread mig
33946 68 74 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  ht call this rou
33947 74 69 6e 65 20 72 65 63 75 72 73 69 76 65 6c 79  tine recursively
33948 2e 20 20 52 65 63 75 72 73 69 76 65 0a 2a 2a 20  .  Recursive.** 
33949 63 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f  calls to this ro
3394a 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74  utine should not
3394b 20 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f 75 72 73   block, of cours
3394c 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68  e.  Otherwise th
3394d 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74  e.** initializat
3394e 69 6f 6e 20 70 72 6f 63 65 73 73 20 77 6f 75 6c  ion process woul
3394f 64 20 6e 65 76 65 72 20 63 6f 6d 70 6c 65 74 65  d never complete
33950 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58 20 62 65  ..**.** Let X be
33951 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 61   the first threa
33952 64 20 74 6f 20 65 6e 74 65 72 20 74 68 69 73 20  d to enter this 
33953 72 6f 75 74 69 6e 65 2e 20 20 4c 65 74 20 59 20  routine.  Let Y 
33954 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  be some other.**
33955 20 74 68 72 65 61 64 2e 20 20 54 68 65 6e 20 77   thread.  Then w
33956 68 69 6c 65 20 74 68 65 20 69 6e 69 74 69 61 6c  hile the initial
33957 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
33958 68 69 73 20 72 6f 75 74 69 6e 65 20 62 79 20 58  his routine by X
33959 20 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65 74   is.** incomplet
3395a 65 2c 20 69 74 20 69 73 20 72 65 71 75 69 72 65  e, it is require
3395b 64 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  d that:.**.**   
3395c 20 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74 68 69   *  Calls to thi
3395d 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 59  s routine from Y
3395e 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69   must block unti
3395f 6c 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f 73 74  l the outer-most
33960 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c 20 62  .**       call b
33961 79 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e 0a 2a  y X completes..*
33962 2a 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63 75 72  *.**    *  Recur
33963 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 74 68  sive calls to th
33964 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20  is routine from 
33965 74 68 72 65 61 64 20 58 20 72 65 74 75 72 6e 20  thread X return 
33966 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20  immediately.**  
33967 20 20 20 20 20 77 69 74 68 6f 75 74 20 62 6c 6f       without blo
33968 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cking..*/.SQLITE
33969 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3396a 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64  _initialize(void
3396b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
3396c 65 78 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20  ex *pMaster;    
3396d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3396e 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 73 74    /* The main st
3396f 61 74 69 63 20 6d 75 74 65 78 20 2a 2f 0a 20 20  atic mutex */.  
33970 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
33971 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33972 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33973 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a  Result code */..
33974 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
33975 49 54 5f 57 53 44 0a 20 20 72 63 20 3d 20 73 71  IT_WSD.  rc = sq
33976 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 34  lite3_wsd_init(4
33977 30 39 36 2c 20 32 34 29 3b 0a 20 20 69 66 28 20  096, 24);.  if( 
33978 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
33979 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
3397a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
3397b 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 61 6c   If SQLite is al
3397c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 6c 79  ready completely
3397d 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
3397e 65 6e 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a  en this call.  *
3397f 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69  * to sqlite3_ini
33980 74 69 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64  tialize() should
33981 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75   be a no-op.  Bu
33982 74 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  t the initializa
33983 74 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73 74 20 62  tion.  ** must b
33984 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53 6f 20  e complete.  So 
33985 69 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f 74 20  isInit must not 
33986 62 65 20 73 65 74 20 75 6e 74 69 6c 20 74 68 65  be set until the
33987 20 76 65 72 79 20 65 6e 64 0a 20 20 2a 2a 20 6f   very end.  ** o
33988 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  f this routine..
33989 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
3398a 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
3398b 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53  sInit ) return S
3398c 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
3398d 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 75  Make sure the mu
3398e 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73  tex subsystem is
3398f 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 49   initialized.  I
33990 66 20 75 6e 61 62 6c 65 20 74 6f 20 0a 20 20 2a  f unable to .  *
33991 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * initialize the
33992 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
33993 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20 77  , return early w
33994 69 74 68 20 74 68 65 20 65 72 72 6f 72 2e 0a 20  ith the error.. 
33995 20 2a 2a 20 49 66 20 74 68 65 20 73 79 73 74 65   ** If the syste
33996 6d 20 69 73 20 73 6f 20 73 69 63 6b 20 74 68 61  m is so sick tha
33997 74 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  t we are unable 
33998 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75  to allocate a mu
33999 74 65 78 2c 0a 20 20 2a 2a 20 74 68 65 72 65 20  tex,.  ** there 
3399a 69 73 20 6e 6f 74 20 6d 75 63 68 20 53 51 4c 69  is not much SQLi
3399b 74 65 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 62  te is going to b
3399c 65 20 61 62 6c 65 20 74 6f 20 64 6f 2e 0a 20 20  e able to do..  
3399d 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 75 74 65  **.  ** The mute
3399e 78 20 73 75 62 73 79 73 74 65 6d 20 6d 75 73 74  x subsystem must
3399f 20 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73 65   take care of se
339a0 72 69 61 6c 69 7a 69 6e 67 20 69 74 73 20 6f 77  rializing its ow
339a1 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  n.  ** initializ
339a2 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63  ation..  */.  rc
339a3 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49   = sqlite3MutexI
339a4 6e 69 74 28 29 3b 0a 20 20 69 66 28 20 72 63 20  nit();.  if( rc 
339a5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
339a6 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
339a7 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73 74 65  e malloc() syste
339a8 6d 20 61 6e 64 20 74 68 65 20 72 65 63 75 72 73  m and the recurs
339a9 69 76 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d  ive pInitMutex m
339aa 75 74 65 78 2e 0a 20 20 2a 2a 20 54 68 69 73 20  utex..  ** This 
339ab 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 72 6f  operation is pro
339ac 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53 54  tected by the ST
339ad 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65  ATIC_MASTER mute
339ae 78 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20  x.  Note that.  
339af 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20  ** MutexAlloc() 
339b0 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20  is called for a 
339b1 73 74 61 74 69 63 20 6d 75 74 65 78 20 70 72 69  static mutex pri
339b2 6f 72 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 69  or to initializi
339b3 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d 61 6c 6c  ng the.  ** mall
339b4 6f 63 20 73 75 62 73 79 73 74 65 6d 20 2d 20 74  oc subsystem - t
339b5 68 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74  his implies that
339b6 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
339b7 6f 66 20 61 20 73 74 61 74 69 63 0a 20 20 2a 2a  of a static.  **
339b8 20 6d 75 74 65 78 20 6d 75 73 74 20 6e 6f 74 20   mutex must not 
339b9 72 65 71 75 69 72 65 20 73 75 70 70 6f 72 74 20  require support 
339ba 66 72 6f 6d 20 74 68 65 20 6d 61 6c 6c 6f 63 20  from the malloc 
339bb 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a  subsystem..  */.
339bc 20 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69    pMaster = sqli
339bd 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
339be 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
339bf 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c  C_MASTER);.  sql
339c0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
339c1 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 73 71 6c  (pMaster);.  sql
339c2 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
339c3 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 3d 20 31  .isMutexInit = 1
339c4 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
339c5 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d  GlobalConfig.isM
339c6 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a 20 20 20  allocInit ){.   
339c7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c   rc = sqlite3Mal
339c8 6c 6f 63 49 6e 69 74 28 29 3b 0a 20 20 7d 0a 20  locInit();.  }. 
339c9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
339ca 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
339cb 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
339cc 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 31 3b 0a  MallocInit = 1;.
339cd 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
339ce 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
339cf 69 74 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20  itMutex ){.     
339d0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
339d1 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20  nfig.pInitMutex 
339d2 3d 0a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  =.           sql
339d3 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
339d4 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
339d5 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 69 66  RSIVE);.      if
339d6 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
339d7 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
339d8 20 26 26 20 21 73 71 6c 69 74 65 33 47 6c 6f 62   && !sqlite3Glob
339d9 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
339da 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 72  tex ){.        r
339db 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
339dc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
339dd 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
339de 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
339df 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
339e0 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  ig.nRefInitMutex
339e1 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ++;.  }.  sqlite
339e2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d  3_mutex_leave(pM
339e3 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 49 66  aster);..  /* If
339e4 20 72 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54   rc is not SQLIT
339e5 45 5f 4f 4b 20 61 74 20 74 68 69 73 20 70 6f 69  E_OK at this poi
339e6 6e 74 2c 20 74 68 65 6e 20 65 69 74 68 65 72 20  nt, then either 
339e7 74 68 65 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20  the malloc.  ** 
339e8 73 75 62 73 79 73 74 65 6d 20 63 6f 75 6c 64 20  subsystem could 
339e9 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  not be initializ
339ea 65 64 20 6f 72 20 74 68 65 20 73 79 73 74 65 6d  ed or the system
339eb 20 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63   failed to alloc
339ec 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 49 6e  ate.  ** the pIn
339ed 69 74 4d 75 74 65 78 20 6d 75 74 65 78 2e 20 52  itMutex mutex. R
339ee 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69  eturn an error i
339ef 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 20 20  n either case.  
339f0 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  */.  if( rc!=SQL
339f1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
339f2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
339f3 2f 2a 20 44 6f 20 74 68 65 20 72 65 73 74 20 6f  /* Do the rest o
339f4 66 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  f the initializa
339f5 74 69 6f 6e 20 75 6e 64 65 72 20 74 68 65 20 72  tion under the r
339f6 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 73  ecursive mutex s
339f7 6f 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 77  o.  ** that we w
339f8 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 68  ill be able to h
339f9 61 6e 64 6c 65 20 72 65 63 75 72 73 69 76 65 20  andle recursive 
339fa 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20  calls into.  ** 
339fb 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
339fc 7a 65 28 29 2e 20 20 54 68 65 20 72 65 63 75 72  ze().  The recur
339fd 73 69 76 65 20 63 61 6c 6c 73 20 6e 6f 72 6d 61  sive calls norma
339fe 6c 6c 79 20 63 6f 6d 65 20 74 68 72 6f 75 67 68  lly come through
339ff 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  .  ** sqlite3_os
33a00 5f 69 6e 69 74 28 29 20 77 68 65 6e 20 69 74 20  _init() when it 
33a01 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 5f  invokes sqlite3_
33a02 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2c 20  vfs_register(), 
33a03 62 75 74 20 6f 74 68 65 72 0a 20 20 2a 2a 20 72  but other.  ** r
33a04 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 6d  ecursive calls m
33a05 69 67 68 74 20 61 6c 73 6f 20 62 65 20 70 6f 73  ight also be pos
33a06 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71  sible..  */.  sq
33a07 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
33a08 72 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  r(sqlite3GlobalC
33a09 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
33a0a 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
33a0b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
33a0c 6e 69 74 3d 3d 30 20 26 26 20 73 71 6c 69 74 65  nit==0 && sqlite
33a0d 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
33a0e 50 72 6f 67 72 65 73 73 3d 3d 30 20 29 7b 0a 20  Progress==0 ){. 
33a0f 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a     FuncDefHash *
33a10 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28  pHash = &GLOBAL(
33a11 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c  FuncDefHash, sql
33a12 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
33a13 6f 6e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ons);.    sqlite
33a14 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
33a15 50 72 6f 67 72 65 73 73 20 3d 20 31 3b 0a 20 20  Progress = 1;.  
33a16 20 20 6d 65 6d 73 65 74 28 70 48 61 73 68 2c 20    memset(pHash, 
33a17 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  0, sizeof(sqlite
33a18 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  3GlobalFunctions
33a19 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  ));.    sqlite3R
33a1a 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e  egisterGlobalFun
33a1b 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20 20 69 66  ctions();.    if
33a1c 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33a1d 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e  onfig.isPCacheIn
33a1e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  it==0 ){.      r
33a1f 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  c = sqlite3Pcach
33a20 65 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  eInitialize();. 
33a21 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
33a22 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
33a23 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33a24 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65  lConfig.isPCache
33a25 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Init = 1;.      
33a26 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 49 6e  rc = sqlite3OsIn
33a27 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  it();.    }.    
33a28 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
33a29 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
33a2a 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65  e3PCacheBufferSe
33a2b 74 75 70 28 20 73 71 6c 69 74 65 33 47 6c 6f 62  tup( sqlite3Glob
33a2c 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 2c 20  alConfig.pPage, 
33a2d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
33a2e 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
33a2f 7a 50 61 67 65 2c 20 73 71 6c 69 74 65 33 47 6c  zPage, sqlite3Gl
33a30 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
33a31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
33a32 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
33a33 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  nit = 1;.    }. 
33a34 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33a35 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73  Config.inProgres
33a36 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  s = 0;.  }.  sql
33a37 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33a38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
33a39 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29  nfig.pInitMutex)
33a3a 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b 20  ;..  /* Go back 
33a3b 75 6e 64 65 72 20 74 68 65 20 73 74 61 74 69 63  under the static
33a3c 20 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61 6e   mutex and clean
33a3d 20 75 70 20 74 68 65 20 72 65 63 75 72 73 69 76   up the recursiv
33a3e 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74 6f 20  e.  ** mutex to 
33a3f 70 72 65 76 65 6e 74 20 61 20 72 65 73 6f 75 72  prevent a resour
33a40 63 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20 20  ce leak..  */.  
33a41 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33a42 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
33a43 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33a44 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
33a45 78 2d 2d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  x--;.  if( sqlit
33a46 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
33a47 52 65 66 49 6e 69 74 4d 75 74 65 78 3c 3d 30 20  RefInitMutex<=0 
33a48 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
33a49 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33a4a 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  ig.nRefInitMutex
33a4b 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
33a4c 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
33a4d 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33a4e 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20  g.pInitMutex);. 
33a4f 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33a50 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65  Config.pInitMute
33a51 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  x = 0;.  }.  sql
33a52 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33a53 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a  (pMaster);..  /*
33a54 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
33a55 73 20 6a 75 73 74 20 61 20 73 61 6e 69 74 79 20  s just a sanity 
33a56 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  check to make su
33a57 72 65 20 53 51 4c 69 74 65 20 68 61 73 0a 20 20  re SQLite has.  
33a58 2a 2a 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65 64  ** been compiled
33a59 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 49 74 20   correctly.  It 
33a5a 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
33a5b 72 75 6e 20 74 68 69 73 20 63 6f 64 65 2c 20 62  run this code, b
33a5c 75 74 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74  ut.  ** we don't
33a5d 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74 20   want to run it 
33a5e 74 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73 6f  too often and so
33a5f 61 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65 73  ak up CPU cycles
33a60 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20 72 65 61   for no.  ** rea
33a61 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e 20  son.  So we run 
33a62 69 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 69  it once during i
33a63 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20  nitialization.. 
33a64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42   */.#ifndef NDEB
33a65 55 47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  UG.#ifndef SQLIT
33a66 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
33a67 50 4f 49 4e 54 0a 20 20 2f 2a 20 54 68 69 73 20  POINT.  /* This 
33a68 73 65 63 74 69 6f 6e 20 6f 66 20 63 6f 64 65 27  section of code'
33a69 73 20 6f 6e 6c 79 20 22 6f 75 74 70 75 74 22 20  s only "output" 
33a6a 69 73 20 76 69 61 20 61 73 73 65 72 74 28 29 20  is via assert() 
33a6b 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  statements. */. 
33a6c 20 69 66 20 28 20 72 63 3d 3d 53 51 4c 49 54 45   if ( rc==SQLITE
33a6d 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 36 34 20 78  _OK ){.    u64 x
33a6e 20 3d 20 28 28 28 75 36 34 29 31 29 3c 3c 36 33   = (((u64)1)<<63
33a6f 29 2d 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  )-1;.    double 
33a70 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73 69  y;.    assert(si
33a71 7a 65 6f 66 28 78 29 3d 3d 38 29 3b 0a 20 20 20  zeof(x)==8);.   
33a72 20 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78   assert(sizeof(x
33a73 29 3d 3d 73 69 7a 65 6f 66 28 79 29 29 3b 0a 20  )==sizeof(y));. 
33a74 20 20 20 6d 65 6d 63 70 79 28 26 79 2c 20 26 78     memcpy(&y, &x
33a75 2c 20 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , 8);.    assert
33a76 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 79  ( sqlite3IsNaN(y
33a77 29 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ) );.  }.#endif.
33a78 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
33a79 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e   rc;.}../*.** Un
33a7a 64 6f 20 74 68 65 20 65 66 66 65 63 74 73 20 6f  do the effects o
33a7b 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  f sqlite3_initia
33a7c 6c 69 7a 65 28 29 2e 20 20 4d 75 73 74 20 6e 6f  lize().  Must no
33a7d 74 20 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c  t be called whil
33a7e 65 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6f  e.** there are o
33a7f 75 74 73 74 61 6e 64 69 6e 67 20 64 61 74 61 62  utstanding datab
33a80 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
33a81 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  or memory alloca
33a82 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77 68 69 6c  tions or.** whil
33a83 65 20 61 6e 79 20 70 61 72 74 20 6f 66 20 53 51  e any part of SQ
33a84 4c 69 74 65 20 69 73 20 6f 74 68 65 72 77 69 73  Lite is otherwis
33a85 65 20 69 6e 20 75 73 65 20 69 6e 20 61 6e 79 20  e in use in any 
33a86 74 68 72 65 61 64 2e 20 20 54 68 69 73 0a 2a 2a  thread.  This.**
33a87 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20   routine is not 
33a88 74 68 72 65 61 64 73 61 66 65 2e 20 20 42 75 74  threadsafe.  But
33a89 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 69   it is safe to i
33a8a 6e 76 6f 6b 65 20 74 68 69 73 20 72 6f 75 74 69  nvoke this routi
33a8b 6e 65 0a 2a 2a 20 6f 6e 20 77 68 65 6e 20 53 51  ne.** on when SQ
33a8c 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20  Lite is already 
33a8d 73 68 75 74 20 64 6f 77 6e 2e 20 20 49 66 20 53  shut down.  If S
33a8e 51 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79  QLite is already
33a8f 20 73 68 75 74 20 64 6f 77 6e 0a 2a 2a 20 77 68   shut down.** wh
33a90 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
33a91 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 6e  is invoked, then
33a92 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
33a93 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f   a harmless no-o
33a94 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  p..*/.SQLITE_API
33a95 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75   int sqlite3_shu
33a96 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69  tdown(void){.  i
33a97 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
33a98 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 7b  Config.isInit ){
33a99 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f  .    sqlite3_os_
33a9a 65 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c 69 74  end();.    sqlit
33a9b 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78  e3_reset_auto_ex
33a9c 74 65 6e 73 69 6f 6e 28 29 3b 0a 20 20 20 20 73  tension();.    s
33a9d 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33a9e 69 67 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  ig.isInit = 0;. 
33a9f 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
33aa0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50  GlobalConfig.isP
33aa1 43 61 63 68 65 49 6e 69 74 20 29 7b 0a 20 20 20  CacheInit ){.   
33aa2 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 68   sqlite3PcacheSh
33aa3 75 74 64 6f 77 6e 28 29 3b 0a 20 20 20 20 73 71  utdown();.    sq
33aa4 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33aa5 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74 20 3d  g.isPCacheInit =
33aa6 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   0;.  }.  if( sq
33aa7 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33aa8 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29  g.isMallocInit )
33aa9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c  {.    sqlite3Mal
33aaa 6c 6f 63 45 6e 64 28 29 3b 0a 20 20 20 20 73 71  locEnd();.    sq
33aab 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33aac 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d  g.isMallocInit =
33aad 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   0;.  }.  if( sq
33aae 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33aaf 67 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 29 7b  g.isMutexInit ){
33ab0 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 75 74 65  .    sqlite3Mute
33ab1 78 45 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c 69  xEnd();.    sqli
33ab2 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33ab3 69 73 4d 75 74 65 78 49 6e 69 74 20 3d 20 30 3b  isMutexInit = 0;
33ab4 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
33ab5 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
33ab6 2a 2a 20 54 68 69 73 20 41 50 49 20 61 6c 6c 6f  ** This API allo
33ab7 77 73 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ws applications 
33ab8 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 67 6c  to modify the gl
33ab9 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  obal configurati
33aba 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 53 51 4c  on of.** the SQL
33abb 69 74 65 20 6c 69 62 72 61 72 79 20 61 74 20 72  ite library at r
33abc 75 6e 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  un-time..**.** T
33abd 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
33abe 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
33abf 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  d when there are
33ac0 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a   no outstanding.
33ac1 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
33ac2 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72  ections or memor
33ac3 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 20  y allocations.  
33ac4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
33ac5 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 61 66  not.** threadsaf
33ac6 65 2e 20 20 46 61 69 6c 75 72 65 20 74 6f 20 68  e.  Failure to h
33ac7 65 65 64 20 74 68 65 73 65 20 77 61 72 6e 69 6e  eed these warnin
33ac8 67 73 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 75  gs can lead to u
33ac9 6e 70 72 65 64 69 63 74 61 62 6c 65 0a 2a 2a 20  npredictable.** 
33aca 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c  behavior..*/.SQL
33acb 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33acc 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 20 6f  te3_config(int o
33acd 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  p, ...){.  va_li
33ace 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 72 63 20  st ap;.  int rc 
33acf 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
33ad0 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  /* sqlite3_confi
33ad1 67 28 29 20 73 68 61 6c 6c 20 72 65 74 75 72 6e  g() shall return
33ad2 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69   SQLITE_MISUSE i
33ad3 66 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  f it is invoked 
33ad4 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 65 20 53  while.  ** the S
33ad5 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 73  QLite library is
33ad6 20 69 6e 20 75 73 65 2e 20 2a 2f 0a 20 20 69 66   in use. */.  if
33ad7 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33ad8 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72  onfig.isInit ) r
33ad9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
33ada 55 53 45 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74  USE;..  va_start
33adb 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74  (ap, op);.  swit
33adc 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20 20 2f  ch( op ){..    /
33add 2a 20 4d 75 74 65 78 20 63 6f 6e 66 69 67 75 72  * Mutex configur
33ade 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 61 72  ation options ar
33adf 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  e only available
33ae0 20 69 6e 20 61 20 74 68 72 65 61 64 73 61 66 65   in a threadsafe
33ae1 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 2e  .    ** compile.
33ae2 20 0a 20 20 20 20 2a 2f 0a 23 69 66 20 53 51 4c   .    */.#if SQL
33ae3 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
33ae4 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
33ae5 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
33ae6 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  AD: {.      /* D
33ae7 69 73 61 62 6c 65 20 61 6c 6c 20 6d 75 74 65 78  isable all mutex
33ae8 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ing */.      sql
33ae9 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33aea 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 30 3b  .bCoreMutex = 0;
33aeb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33aec 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
33aed 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
33aee 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
33aef 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
33af0 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44  NFIG_MULTITHREAD
33af1 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 69 73  : {.      /* Dis
33af2 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66  able mutexing of
33af3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
33af4 74 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20 2f  tions */.      /
33af5 2a 20 45 6e 61 62 6c 65 20 6d 75 74 65 78 69 6e  * Enable mutexin
33af6 67 20 6f 66 20 63 6f 72 65 20 64 61 74 61 20 73  g of core data s
33af7 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20 20  tructures */.   
33af8 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33af9 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
33afa 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  x = 1;.      sql
33afb 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33afc 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30 3b  .bFullMutex = 0;
33afd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33afe 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
33aff 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41  ITE_CONFIG_SERIA
33b00 4c 49 5a 45 44 3a 20 7b 0a 20 20 20 20 20 20 2f  LIZED: {.      /
33b01 2a 20 45 6e 61 62 6c 65 20 61 6c 6c 20 6d 75 74  * Enable all mut
33b02 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 73  exing */.      s
33b03 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33b04 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20  ig.bCoreMutex = 
33b05 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  1;.      sqlite3
33b06 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75  GlobalConfig.bFu
33b07 6c 6c 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20  llMutex = 1;.   
33b08 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
33b09 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
33b0a 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3a 20 7b 0a  CONFIG_MUTEX: {.
33b0b 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79        /* Specify
33b0c 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20   an alternative 
33b0d 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
33b0e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
33b0f 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33b10 67 2e 6d 75 74 65 78 20 3d 20 2a 76 61 5f 61 72  g.mutex = *va_ar
33b11 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75  g(ap, sqlite3_mu
33b12 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20  tex_methods*);. 
33b13 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33b14 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
33b15 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
33b16 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65  X: {.      /* Re
33b17 74 72 69 65 76 65 20 74 68 65 20 63 75 72 72 65  trieve the curre
33b18 6e 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  nt mutex impleme
33b19 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ntation */.     
33b1a 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
33b1b 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
33b1c 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33 47 6c  ds*) = sqlite3Gl
33b1d 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
33b1e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33b1f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20     }.#endif...  
33b20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
33b21 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3a 20 7b 0a 20  NFIG_MALLOC: {. 
33b22 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20       /* Specify 
33b23 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d  an alternative m
33b24 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61  alloc implementa
33b25 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
33b26 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33b27 67 2e 6d 20 3d 20 2a 76 61 5f 61 72 67 28 61 70  g.m = *va_arg(ap
33b28 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  , sqlite3_mem_me
33b29 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
33b2a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
33b2b 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
33b2c 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3a 20 7b 0a  IG_GETMALLOC: {.
33b2d 20 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76        /* Retriev
33b2e 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61  e the current ma
33b2f 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74  lloc() implement
33b30 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69  ation */.      i
33b31 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
33b32 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
33b33 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 4d 65 6d  ==0 ) sqlite3Mem
33b34 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
33b35 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20      *va_arg(ap, 
33b36 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
33b37 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33 47  ods*) = sqlite3G
33b38 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 3b 0a 20  lobalConfig.m;. 
33b39 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33b3a 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
33b3b 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54  E_CONFIG_MEMSTAT
33b3c 55 53 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45  US: {.      /* E
33b3d 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65  nable or disable
33b3e 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 74 61 74   the malloc stat
33b3f 75 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 2a 2f  us collection */
33b40 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33b41 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
33b42 74 61 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  tat = va_arg(ap,
33b43 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65   int);.      bre
33b44 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
33b45 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
33b46 5f 53 43 52 41 54 43 48 3a 20 7b 0a 20 20 20 20  _SCRATCH: {.    
33b47 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61    /* Designate a
33b48 20 62 75 66 66 65 72 20 66 6f 72 20 73 63 72 61   buffer for scra
33b49 74 63 68 20 6d 65 6d 6f 72 79 20 73 70 61 63 65  tch memory space
33b4a 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
33b4b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
33b4c 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28  cratch = va_arg(
33b4d 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20  ap, void*);.    
33b4e 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
33b4f 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20  onfig.szScratch 
33b50 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
33b51 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
33b52 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
33b53 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28 61  ratch = va_arg(a
33b54 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62  p, int);.      b
33b55 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
33b56 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
33b57 49 47 5f 50 41 47 45 43 41 43 48 45 3a 20 7b 0a  IG_PAGECACHE: {.
33b58 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61        /* Designa
33b59 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72 20  te a buffer for 
33b5a 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
33b5b 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
33b5c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33b5d 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 76 61 5f  nfig.pPage = va_
33b5e 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a  arg(ap, void*);.
33b5f 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33b60 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
33b61 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
33b62 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
33b63 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50  3GlobalConfig.nP
33b64 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  age = va_arg(ap,
33b65 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65   int);.      bre
33b66 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ak;.    }..    c
33b67 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33b68 47 5f 50 43 41 43 48 45 3a 20 7b 0a 20 20 20 20  G_PCACHE: {.    
33b69 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20    /* Specify an 
33b6a 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65  alternative page
33b6b 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
33b6c 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ation */.      s
33b6d 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33b6e 69 67 2e 70 63 61 63 68 65 20 3d 20 2a 76 61 5f  ig.pcache = *va_
33b6f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
33b70 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29  pcache_methods*)
33b71 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33b72 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53     }..    case S
33b73 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
33b74 50 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20  PCACHE: {.      
33b75 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
33b76 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78  lConfig.pcache.x
33b77 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Init==0 ){.     
33b78 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
33b79 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
33b7a 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f      }.      *va_
33b7b 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
33b7c 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29  pcache_methods*)
33b7d 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
33b7e 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 3b 0a 20  Config.pcache;. 
33b7f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33b80 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
33b81 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
33b82 53 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64  SYS3) || defined
33b83 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  (SQLITE_ENABLE_M
33b84 45 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73 65  EMSYS5).    case
33b85 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
33b86 45 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  EAP: {.      /* 
33b87 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
33b88 65 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f  er for heap memo
33b89 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  ry space */.    
33b8a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
33b8b 6f 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61  onfig.pHeap = va
33b8c 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b  _arg(ap, void*);
33b8d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33b8e 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70  obalConfig.nHeap
33b8f 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
33b90 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
33b91 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e  3GlobalConfig.mn
33b92 52 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Req = va_arg(ap,
33b93 20 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66   int);..      if
33b94 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33b95 6f 6e 66 69 67 2e 70 48 65 61 70 3d 3d 30 20 29  onfig.pHeap==0 )
33b96 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
33b97 74 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72  the heap pointer
33b98 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72   is NULL, then r
33b99 65 73 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f  estore the mallo
33b9a 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  c implementation
33b9b 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b  .        ** back
33b9c 20 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72   to NULL pointer
33b9d 73 20 74 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c  s too.  This wil
33b9e 6c 20 63 61 75 73 65 20 74 68 65 20 6d 61 6c 6c  l cause the mall
33b9f 6f 63 20 74 6f 20 67 6f 0a 20 20 20 20 20 20 20  oc to go.       
33ba0 20 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20   ** back to its 
33ba1 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e  default implemen
33ba2 74 61 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69  tation when sqli
33ba3 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
33ba4 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72   is.        ** r
33ba5 75 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  un..        */. 
33ba6 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73         memset(&s
33ba7 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33ba8 69 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ig.m, 0, sizeof(
33ba9 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33baa 66 69 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d  fig.m));.      }
33bab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
33bac 20 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65   The heap pointe
33bad 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  r is not NULL, t
33bae 68 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20  hen install one 
33baf 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  of the.        *
33bb0 2a 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20  * mem5.c/mem3.c 
33bb1 6d 65 74 68 6f 64 73 2e 20 49 66 20 6e 65 69 74  methods. If neit
33bb2 68 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  her ENABLE_MEMSY
33bb3 53 33 20 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a  S3 nor.        *
33bb4 2a 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  * ENABLE_MEMSYS5
33bb5 20 69 73 20 64 65 66 69 6e 65 64 2c 20 72 65 74   is defined, ret
33bb6 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
33bb7 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20        */.#ifdef 
33bb8 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
33bb9 4d 53 59 53 33 0a 20 20 20 20 20 20 20 20 73 71  MSYS3.        sq
33bba 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33bbb 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65  g.m = *sqlite3Me
33bbc 6d 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a 23  mGetMemsys3();.#
33bbd 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
33bbe 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
33bbf 53 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  S5.        sqlit
33bc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
33bc1 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65   = *sqlite3MemGe
33bc2 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e 64  tMemsys5();.#end
33bc3 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  if.      }.     
33bc4 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
33bc5 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20 53  ndif..    case S
33bc6 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
33bc7 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20  KASIDE: {.      
33bc8 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33bc9 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 20  fig.szLookaside 
33bca 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
33bcb 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
33bcc 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f  GlobalConfig.nLo
33bcd 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67  okaside = va_arg
33bce 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
33bcf 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
33bd0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
33bd1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
33bd2 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65  ERROR;.      bre
33bd3 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
33bd4 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
33bd5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
33bd6 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 6f  * Set up the loo
33bd7 6b 61 73 69 64 65 20 62 75 66 66 65 72 73 20 66  kaside buffers f
33bd8 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  or a database co
33bd9 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74  nnection..** Ret
33bda 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
33bdb 20 73 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20 49   success.  .** I
33bdc 66 20 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20 61  f lookaside is a
33bdd 6c 72 65 61 64 79 20 61 63 74 69 76 65 2c 20 72  lready active, r
33bde 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
33bdf 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 20  Y..**.** The sz 
33be0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
33be1 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
33be2 20 69 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69   in each lookasi
33be3 64 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20  de slot..** The 
33be4 63 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  cnt parameter is
33be5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
33be6 6c 6f 74 73 2e 20 20 49 66 20 70 53 74 61 72 74  lots.  If pStart
33be7 20 69 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20   is NULL the.** 
33be8 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 6c 6f  space for the lo
33be9 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 69  okaside memory i
33bea 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
33beb 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
33bec 2e 0a 2a 2a 20 49 66 20 70 53 74 61 72 74 20 69  ..** If pStart i
33bed 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20  s not NULL then 
33bee 69 74 20 69 73 20 73 7a 2a 63 6e 74 20 62 79 74  it is sz*cnt byt
33bef 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20  es of memory to 
33bf0 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6c  use for.** the l
33bf1 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e  ookaside memory.
33bf2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
33bf3 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71  etupLookaside(sq
33bf4 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
33bf5 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69  *pBuf, int sz, i
33bf6 6e 74 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64 20  nt cnt){.  void 
33bf7 2a 70 53 74 61 72 74 3b 0a 20 20 69 66 28 20 64  *pStart;.  if( d
33bf8 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
33bf9 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
33bfa 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
33bfb 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 65  .  /* Free any e
33bfc 78 69 73 74 69 6e 67 20 6c 6f 6f 6b 61 73 69 64  xisting lookasid
33bfd 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 69  e buffer for thi
33bfe 73 20 68 61 6e 64 6c 65 20 62 65 66 6f 72 65 0a  s handle before.
33bff 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20    ** allocating 
33c00 61 20 6e 65 77 20 6f 6e 65 20 73 6f 20 77 65 20  a new one so we 
33c01 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 68 61  don't have to ha
33c02 76 65 20 73 70 61 63 65 20 66 6f 72 20 0a 20 20  ve space for .  
33c03 2a 2a 20 62 6f 74 68 20 61 74 20 74 68 65 20 73  ** both at the s
33c04 61 6d 65 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20  ame time..  */. 
33c05 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
33c06 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a  de.bMalloced ){.
33c07 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
33c08 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70  (db->lookaside.p
33c09 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20 2f 2a  Start);.  }.  /*
33c0a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6c   The size of a l
33c0b 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 20 6e 65  ookaside slot ne
33c0c 65 64 73 20 74 6f 20 62 65 20 6c 61 72 67 65 72  eds to be larger
33c0d 20 74 68 61 6e 20 61 20 70 6f 69 6e 74 65 72 0a   than a pointer.
33c0e 20 20 2a 2a 20 74 6f 20 62 65 20 75 73 65 66 75    ** to be usefu
33c0f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 7a  l..  */.  if( sz
33c10 3c 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f  <=(int)sizeof(Lo
33c11 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29 20  okasideSlot*) ) 
33c12 73 7a 20 3d 20 30 3b 0a 20 20 69 66 28 20 63 6e  sz = 0;.  if( cn
33c13 74 3c 30 20 29 20 63 6e 74 20 3d 20 30 3b 0a 20  t<0 ) cnt = 0;. 
33c14 20 69 66 28 20 73 7a 3d 3d 30 20 7c 7c 20 63 6e   if( sz==0 || cn
33c15 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 7a 20 3d  t==0 ){.    sz =
33c16 20 30 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d   0;.    pStart =
33c17 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
33c18 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73  pBuf==0 ){.    s
33c19 7a 20 3d 20 52 4f 55 4e 44 38 28 73 7a 29 3b 0a  z = ROUND8(sz);.
33c1a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
33c1b 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
33c1c 20 20 20 20 70 53 74 61 72 74 20 3d 20 73 71 6c      pStart = sql
33c1d 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 7a 2a 63  ite3Malloc( sz*c
33c1e 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  nt );.    sqlite
33c1f 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
33c20 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ();.  }else{.   
33c21 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38   sz = ROUNDDOWN8
33c22 28 73 7a 29 3b 0a 20 20 20 20 70 53 74 61 72 74  (sz);.    pStart
33c23 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 20 20 64   = pBuf;.  }.  d
33c24 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74  b->lookaside.pSt
33c25 61 72 74 20 3d 20 70 53 74 61 72 74 3b 0a 20 20  art = pStart;.  
33c26 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
33c27 72 65 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 6c  ree = 0;.  db->l
33c28 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 3d 20 28 75  ookaside.sz = (u
33c29 31 36 29 73 7a 3b 0a 20 20 69 66 28 20 70 53 74  16)sz;.  if( pSt
33c2a 61 72 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  art ){.    int i
33c2b 3b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53  ;.    LookasideS
33c2c 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 61 73 73 65  lot *p;.    asse
33c2d 72 74 28 20 73 7a 20 3e 20 28 69 6e 74 29 73 69  rt( sz > (int)si
33c2e 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53 6c  zeof(LookasideSl
33c2f 6f 74 2a 29 20 29 3b 0a 20 20 20 20 70 20 3d 20  ot*) );.    p = 
33c30 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29  (LookasideSlot*)
33c31 70 53 74 61 72 74 3b 0a 20 20 20 20 66 6f 72 28  pStart;.    for(
33c32 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20 69  i=cnt-1; i>=0; i
33c33 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  --){.      p->pN
33c34 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ext = db->lookas
33c35 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20 20  ide.pFree;.     
33c36 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
33c37 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Free = p;.      
33c38 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c  p = (LookasideSl
33c39 6f 74 2a 29 26 28 28 75 38 2a 29 70 29 5b 73 7a  ot*)&((u8*)p)[sz
33c3a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d  ];.    }.    db-
33c3b 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 20  >lookaside.pEnd 
33c3c 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  = p;.    db->loo
33c3d 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
33c3e 3d 20 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  = 1;.    db->loo
33c3f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64  kaside.bMalloced
33c40 20 3d 20 70 42 75 66 3d 3d 30 20 3f 31 3a 30 3b   = pBuf==0 ?1:0;
33c41 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
33c42 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
33c43 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 0;.    db->lo
33c44 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
33c45 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 0;.    db->lo
33c46 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65  okaside.bMalloce
33c47 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 0;.  }.  ret
33c48 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
33c49 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
33c4a 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
33c4b 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62  ted with a datab
33c4c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
33c4d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
33c4e 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
33c4f 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71  ite3_db_mutex(sq
33c50 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65  lite3 *db){.  re
33c51 74 75 72 6e 20 64 62 2d 3e 6d 75 74 65 78 3b 0a  turn db->mutex;.
33c52 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75  }../*.** Configu
33c53 72 61 74 69 6f 6e 20 73 65 74 74 69 6e 67 73 20  ration settings 
33c54 66 6f 72 20 61 6e 20 69 6e 64 69 76 69 64 75 61  for an individua
33c55 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  l database conne
33c56 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ction.*/.SQLITE_
33c57 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
33c58 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65  db_config(sqlite
33c59 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70 2c 20 2e  3 *db, int op, .
33c5a 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
33c5b 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 76  p;.  int rc;.  v
33c5c 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
33c5d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
33c5e 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
33c5f 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  _DBCONFIG_LOOKAS
33c60 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 76 6f 69  IDE: {.      voi
33c61 64 20 2a 70 42 75 66 20 3d 20 76 61 5f 61 72 67  d *pBuf = va_arg
33c62 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20  (ap, void*);.   
33c63 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61     int sz = va_a
33c64 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
33c65 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 76 61 5f     int cnt = va_
33c66 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
33c67 20 20 20 20 72 63 20 3d 20 73 65 74 75 70 4c 6f      rc = setupLo
33c68 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 42 75 66  okaside(db, pBuf
33c69 2c 20 73 7a 2c 20 63 6e 74 29 3b 0a 20 20 20 20  , sz, cnt);.    
33c6a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
33c6b 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
33c6c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
33c6d 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65  ERROR;.      bre
33c6e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
33c6f 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
33c70 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
33c71 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
33c72 66 20 74 68 65 20 62 75 66 66 65 72 20 7a 5b 30  f the buffer z[0
33c73 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69 6e 73 20  ..n-1] contains 
33c74 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a 2f 0a 73  all spaces..*/.s
33c75 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 53 70 61  tatic int allSpa
33c76 63 65 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ces(const char *
33c77 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77 68 69  z, int n){.  whi
33c78 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b 6e 2d 31  le( n>0 && z[n-1
33c79 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d 3b 20 7d  ]==' ' ){ n--; }
33c7a 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d 30 3b 0a  .  return n==0;.
33c7b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
33c7c 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
33c7d 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
33c7e 6e 61 6d 65 64 20 22 42 49 4e 41 52 59 22 20 77  named "BINARY" w
33c7f 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 0a 2a  hich is always.*
33c80 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a  * available..**.
33c81 2a 2a 20 49 66 20 74 68 65 20 70 61 64 46 6c 61  ** If the padFla
33c82 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f  g argument is no
33c83 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73 70 61 63  t NULL then spac
33c84 65 20 70 61 64 64 69 6e 67 20 61 74 20 74 68 65  e padding at the
33c85 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74 72 69 6e   end.** of strin
33c86 67 73 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20  gs is ignored.  
33c87 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 73 20  This implements 
33c88 74 68 65 20 52 54 52 49 4d 20 63 6f 6c 6c 61 74  the RTRIM collat
33c89 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
33c8a 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 28 0a  nt binCollFunc(.
33c8b 20 20 76 6f 69 64 20 2a 70 61 64 46 6c 61 67 2c    void *padFlag,
33c8c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f  .  int nKey1, co
33c8d 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c  nst void *pKey1,
33c8e 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f  .  int nKey2, co
33c8f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a  nst void *pKey2.
33c90 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 3b 0a  ){.  int rc, n;.
33c91 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65 79    n = nKey1<nKey
33c92 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e 4b 65 79  2 ? nKey1 : nKey
33c93 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70  2;.  rc = memcmp
33c94 28 70 4b 65 79 31 2c 20 70 4b 65 79 32 2c 20 6e  (pKey1, pKey2, n
33c95 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  );.  if( rc==0 )
33c96 7b 0a 20 20 20 20 69 66 28 20 70 61 64 46 6c 61  {.    if( padFla
33c97 67 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61  g.     && allSpa
33c98 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79  ces(((char*)pKey
33c99 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0a 20  1)+n, nKey1-n). 
33c9a 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65 73      && allSpaces
33c9b 28 28 28 63 68 61 72 2a 29 70 4b 65 79 32 29 2b  (((char*)pKey2)+
33c9c 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20 20 20 20  n, nKey2-n).    
33c9d 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61 76  ){.      /* Leav
33c9e 65 20 72 63 20 75 6e 63 68 61 6e 67 65 64 20 61  e rc unchanged a
33c9f 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  t 0 */.    }else
33ca0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 4b 65  {.      rc = nKe
33ca1 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20 20 20 20  y1 - nKey2;.    
33ca2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
33ca3 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f 74  c;.}../*.** Anot
33ca4 68 65 72 20 62 75 69 6c 74 2d 69 6e 20 63 6f 6c  her built-in col
33ca5 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 3a  lating sequence:
33ca6 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a 2a 2a 20   NOCASE. .**.** 
33ca7 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73  This collating s
33ca8 65 71 75 65 6e 63 65 20 69 73 20 69 6e 74 65 6e  equence is inten
33ca9 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 66  ded to be used f
33caa 6f 72 20 22 63 61 73 65 20 69 6e 64 65 70 65 6e  or "case indepen
33cab 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61 72 69 73  dant.** comparis
33cac 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73 20 6b 6e  on". SQLite's kn
33cad 6f 77 6c 65 64 67 65 20 6f 66 20 75 70 70 65 72  owledge of upper
33cae 20 61 6e 64 20 6c 6f 77 65 72 20 63 61 73 65 20   and lower case 
33caf 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a 2a 20 65  equivalents.** e
33cb0 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74 6f 20 74  xtends only to t
33cb1 68 65 20 32 36 20 63 68 61 72 61 63 74 65 72 73  he 26 characters
33cb2 20 75 73 65 64 20 69 6e 20 74 68 65 20 45 6e 67   used in the Eng
33cb3 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 2e 0a 2a  lish language..*
33cb4 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d 6f 6d 65  *.** At the mome
33cb5 6e 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79  nt there is only
33cb6 20 61 20 55 54 46 2d 38 20 69 6d 70 6c 65 6d 65   a UTF-8 impleme
33cb7 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ntation..*/.stat
33cb8 69 63 20 69 6e 74 20 6e 6f 63 61 73 65 43 6f 6c  ic int nocaseCol
33cb9 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20 20 76 6f  latingFunc(.  vo
33cba 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a 20 20 69  id *NotUsed,.  i
33cbb 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20  nt nKey1, const 
33cbc 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69  void *pKey1,.  i
33cbd 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20  nt nKey2, const 
33cbe 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20  void *pKey2.){. 
33cbf 20 69 6e 74 20 72 20 3d 20 73 71 6c 69 74 65 33   int r = sqlite3
33cc0 53 74 72 4e 49 43 6d 70 28 0a 20 20 20 20 20 20  StrNICmp(.      
33cc1 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b  (const char *)pK
33cc2 65 79 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  ey1, (const char
33cc3 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b 65 79 31   *)pKey2, (nKey1
33cc4 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31 3a 6e 4b  <nKey2)?nKey1:nK
33cc5 65 79 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ey2);.  UNUSED_P
33cc6 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
33cc7 29 3b 0a 20 20 69 66 28 20 30 3d 3d 72 20 29 7b  );.  if( 0==r ){
33cc8 0a 20 20 20 20 72 20 3d 20 6e 4b 65 79 31 2d 6e  .    r = nKey1-n
33cc9 4b 65 79 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Key2;.  }.  retu
33cca 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn r;.}../*.** R
33ccb 65 74 75 72 6e 20 74 68 65 20 52 4f 57 49 44 20  eturn the ROWID 
33ccc 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
33ccd 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a 53 51 4c  nt insert.*/.SQL
33cce 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69  ITE_API sqlite_i
33ccf 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73  nt64 sqlite3_las
33cd0 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73  t_insert_rowid(s
33cd1 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
33cd2 65 74 75 72 6e 20 64 62 2d 3e 6c 61 73 74 52 6f  eturn db->lastRo
33cd3 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  wid;.}../*.** Re
33cd4 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
33cd5 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  of changes in th
33cd6 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61  e most recent ca
33cd7 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78  ll to sqlite3_ex
33cd8 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ec()..*/.SQLITE_
33cd9 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
33cda 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  changes(sqlite3 
33cdb 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  *db){.  return d
33cdc 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f  b->nChange;.}../
33cdd 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
33cde 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65  number of change
33cdf 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74 61  s since the data
33ce0 62 61 73 65 20 68 61 6e 64 6c 65 20 77 61 73 20  base handle was 
33ce1 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  opened..*/.SQLIT
33ce2 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33ce3 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
33ce4 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
33ce5 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54 6f 74 61  return db->nTota
33ce6 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  lChange;.}../*.*
33ce7 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 70 65 6e  * Close all open
33ce8 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 54 68 69   savepoints. Thi
33ce9 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20  s function only 
33cea 6d 61 6e 69 70 75 6c 61 74 65 73 20 66 69 65 6c  manipulates fiel
33ceb 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  ds of the.** dat
33cec 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6f 62 6a  abase handle obj
33ced 65 63 74 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74  ect, it does not
33cee 20 63 6c 6f 73 65 20 61 6e 79 20 73 61 76 65 70   close any savep
33cef 6f 69 6e 74 73 20 74 68 61 74 20 6d 61 79 20 62  oints that may b
33cf0 65 20 6f 70 65 6e 0a 2a 2a 20 61 74 20 74 68 65  e open.** at the
33cf1 20 62 2d 74 72 65 65 2f 70 61 67 65 72 20 6c 65   b-tree/pager le
33cf2 76 65 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  vel..*/.SQLITE_P
33cf3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
33cf4 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e  te3CloseSavepoin
33cf5 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ts(sqlite3 *db){
33cf6 0a 20 20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53  .  while( db->pS
33cf7 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
33cf8 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 20  Savepoint *pTmp 
33cf9 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  = db->pSavepoint
33cfa 3b 0a 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70  ;.    db->pSavep
33cfb 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e 65  oint = pTmp->pNe
33cfc 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  xt;.    sqlite3D
33cfd 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29 3b  bFree(db, pTmp);
33cfe 0a 20 20 7d 0a 20 20 64 62 2d 3e 6e 53 61 76 65  .  }.  db->nSave
33cff 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d  point = 0;.  db-
33d00 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b  >nStatement = 0;
33d01 0a 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63  .  db->isTransac
33d02 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  tionSavepoint = 
33d03 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  0;.}../*.** Clos
33d04 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53 51  e an existing SQ
33d05 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a 2f  Lite database.*/
33d06 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33d07 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71  sqlite3_close(sq
33d08 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48 61  lite3 *db){.  Ha
33d09 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e 74  shElem *i;.  int
33d0a 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20 29   j;..  if( !db )
33d0b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
33d0c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
33d0d 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
33d0e 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
33d0f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
33d10 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
33d11 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
33d12 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33d13 65 78 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 52  ex);..  sqlite3R
33d14 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
33d15 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a  ma(db, 0);..  /*
33d16 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   If a transactio
33d17 6e 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 20 52  n is open, the R
33d18 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
33d19 6d 61 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 0a  ma() call above.
33d1a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 68 61    ** will not ha
33d1b 76 65 20 63 61 6c 6c 65 64 20 74 68 65 20 78 44  ve called the xD
33d1c 69 73 63 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68  isconnect() meth
33d1d 6f 64 20 6f 6e 20 61 6e 79 20 76 69 72 74 75 61  od on any virtua
33d1e 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e  l.  ** tables in
33d1f 20 74 68 65 20 64 62 2d 3e 61 56 54 72 61 6e 73   the db->aVTrans
33d20 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20 66 6f  [] array. The fo
33d21 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56  llowing sqlite3V
33d22 74 61 62 52 6f 6c 6c 62 61 63 6b 28 29 0a 20 20  tabRollback().  
33d23 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 64 6f 20  ** call will do 
33d24 73 6f 2e 20 57 65 20 6e 65 65 64 20 74 6f 20 64  so. We need to d
33d25 6f 20 74 68 69 73 20 62 65 66 6f 72 65 20 74 68  o this before th
33d26 65 20 63 68 65 63 6b 20 66 6f 72 20 61 63 74 69  e check for acti
33d27 76 65 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61 74  ve.  ** SQL stat
33d28 65 6d 65 6e 74 73 20 62 65 6c 6f 77 2c 20 61 73  ements below, as
33d29 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70   the v-table imp
33d2a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20  lementation may 
33d2b 62 65 20 73 74 6f 72 69 6e 67 0a 20 20 2a 2a 20  be storing.  ** 
33d2c 73 6f 6d 65 20 70 72 65 70 61 72 65 64 20 73 74  some prepared st
33d2d 61 74 65 6d 65 6e 74 73 20 69 6e 74 65 72 6e 61  atements interna
33d2e 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  lly..  */.  sqli
33d2f 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28  te3VtabRollback(
33d30 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  db);..  /* If th
33d31 65 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73  ere are any outs
33d32 74 61 6e 64 69 6e 67 20 56 4d 73 2c 20 72 65 74  tanding VMs, ret
33d33 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  urn SQLITE_BUSY.
33d34 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70 56   */.  if( db->pV
33d35 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  dbe ){.    sqlit
33d36 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
33d37 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20  TE_BUSY, .      
33d38 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63 6c 6f    "unable to clo
33d39 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69 6e 61  se due to unfina
33d3a 6c 69 73 65 64 20 73 74 61 74 65 6d 65 6e 74 73  lised statements
33d3b 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ");.    sqlite3_
33d3c 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
33d3d 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75  mutex);.    retu
33d3e 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
33d3f 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71    }.  assert( sq
33d40 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
33d41 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 3b 0a  SickOrOk(db) );.
33d42 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62  .  for(j=0; j<db
33d43 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nDb; j++){.   
33d44 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
33d45 2d 3e 61 44 62 5b 6a 5d 2e 70 42 74 3b 0a 20 20  ->aDb[j].pBt;.  
33d46 20 20 69 66 28 20 70 42 74 20 26 26 20 73 71 6c    if( pBt && sql
33d47 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63  ite3BtreeIsInBac
33d48 6b 75 70 28 70 42 74 29 20 29 7b 0a 20 20 20 20  kup(pBt) ){.    
33d49 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
33d4a 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
33d4b 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 61 62  .          "unab
33d4c 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20  le to close due 
33d4d 74 6f 20 75 6e 66 69 6e 69 73 68 65 64 20 62 61  to unfinished ba
33d4e 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 22 29  ckup operation")
33d4f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
33d50 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
33d51 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65  mutex);.      re
33d52 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
33d53 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
33d54 2a 20 46 72 65 65 20 61 6e 79 20 6f 75 74 73 74  * Free any outst
33d55 61 6e 64 69 6e 67 20 53 61 76 65 70 6f 69 6e 74  anding Savepoint
33d56 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a   structures. */.
33d57 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
33d58 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 0a 20  vepoints(db);.. 
33d59 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e   for(j=0; j<db->
33d5a 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73  nDb; j++){.    s
33d5b 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20  truct Db *pDb = 
33d5c 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20  &db->aDb[j];.   
33d5d 20 69 66 28 20 70 44 62 2d 3e 70 42 74 20 29 7b   if( pDb->pBt ){
33d5e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
33d5f 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42  reeClose(pDb->pB
33d60 74 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70  t);.      pDb->p
33d61 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  Bt = 0;.      if
33d62 28 20 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  ( j!=1 ){.      
33d63 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d    pDb->pSchema =
33d64 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
33d65 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
33d66 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
33d67 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a  ma(db, 0);..  /*
33d68 20 54 65 6c 6c 20 74 68 65 20 63 6f 64 65 20 69   Tell the code i
33d69 6e 20 6e 6f 74 69 66 79 2e 63 20 74 68 61 74 20  n notify.c that 
33d6a 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e  the connection n
33d6b 6f 20 6c 6f 6e 67 65 72 20 68 6f 6c 64 73 20 61  o longer holds a
33d6c 6e 79 0a 20 20 2a 2a 20 6c 6f 63 6b 73 20 61 6e  ny.  ** locks an
33d6d 64 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69  d does not requi
33d6e 72 65 20 61 6e 79 20 66 75 72 74 68 65 72 20 75  re any further u
33d6f 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
33d70 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73  lbacks..  */.  s
33d71 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
33d72 43 6c 6f 73 65 64 28 64 62 29 3b 0a 0a 20 20 61  Closed(db);..  a
33d73 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3c 3d  ssert( db->nDb<=
33d74 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  2 );.  assert( d
33d75 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53  b->aDb==db->aDbS
33d76 74 61 74 69 63 20 29 3b 0a 20 20 66 6f 72 28 6a  tatic );.  for(j
33d77 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65 28  =0; j<ArraySize(
33d78 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 20 6a 2b  db->aFunc.a); j+
33d79 2b 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66 20  +){.    FuncDef 
33d7a 2a 70 4e 65 78 74 2c 20 2a 70 48 61 73 68 2c 20  *pNext, *pHash, 
33d7b 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 64 62  *p;.    for(p=db
33d7c 2d 3e 61 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b  ->aFunc.a[j]; p;
33d7d 20 70 3d 70 48 61 73 68 29 7b 0a 20 20 20 20 20   p=pHash){.     
33d7e 20 70 48 61 73 68 20 3d 20 70 2d 3e 70 48 61 73   pHash = p->pHas
33d7f 68 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  h;.      while( 
33d80 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65  p ){.        pNe
33d81 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
33d82 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
33d83 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20  Free(db, p);.   
33d84 20 20 20 20 20 70 20 3d 20 70 4e 65 78 74 3b 0a       p = pNext;.
33d85 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
33d86 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65  }.  for(i=sqlite
33d87 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61  HashFirst(&db->a
33d88 43 6f 6c 6c 53 65 71 29 3b 20 69 3b 20 69 3d 73  CollSeq); i; i=s
33d89 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29  qliteHashNext(i)
33d8a 29 7b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  ){.    CollSeq *
33d8b 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71  pColl = (CollSeq
33d8c 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
33d8d 61 28 69 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 76  a(i);.    /* Inv
33d8e 6f 6b 65 20 61 6e 79 20 64 65 73 74 72 75 63 74  oke any destruct
33d8f 6f 72 73 20 72 65 67 69 73 74 65 72 65 64 20 66  ors registered f
33d90 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  or collation seq
33d91 75 65 6e 63 65 20 75 73 65 72 20 64 61 74 61 2e  uence user data.
33d92 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b   */.    for(j=0;
33d93 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<3; j++){.    
33d94 20 20 69 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78    if( pColl[j].x
33d95 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70  Del ){.        p
33d96 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f  Coll[j].xDel(pCo
33d97 6c 6c 5b 6a 5d 2e 70 55 73 65 72 29 3b 0a 20 20  ll[j].pUser);.  
33d98 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
33d99 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
33d9a 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20  , pColl);.  }.  
33d9b 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
33d9c 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
33d9d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33d9e 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
33d9f 45 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65  E.  for(i=sqlite
33da0 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61  HashFirst(&db->a
33da1 4d 6f 64 75 6c 65 29 3b 20 69 3b 20 69 3d 73 71  Module); i; i=sq
33da2 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29 29  liteHashNext(i))
33da3 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  {.    Module *pM
33da4 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73  od = (Module *)s
33da5 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
33da6 3b 0a 20 20 20 20 69 66 28 20 70 4d 6f 64 2d 3e  ;.    if( pMod->
33da7 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20  xDestroy ){.    
33da8 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79    pMod->xDestroy
33da9 28 70 4d 6f 64 2d 3e 70 41 75 78 29 3b 0a 20 20  (pMod->pAux);.  
33daa 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
33dab 62 46 72 65 65 28 64 62 2c 20 70 4d 6f 64 29 3b  bFree(db, pMod);
33dac 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
33dad 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f  shClear(&db->aMo
33dae 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  dule);.#endif.. 
33daf 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
33db0 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
33db1 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 73 20   /* Deallocates 
33db2 61 6e 79 20 63 61 63 68 65 64 20 65 72 72 6f 72  any cached error
33db3 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0a 20 20 69   strings. */.  i
33db4 66 28 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20  f( db->pErr ){. 
33db5 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
33db6 72 65 65 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  ree(db->pErr);. 
33db7 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73   }.  sqlite3Clos
33db8 65 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b  eExtensions(db);
33db9 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20  ..  db->magic = 
33dba 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52  SQLITE_MAGIC_ERR
33dbb 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 74 65  OR;..  /* The te
33dbc 6d 70 2d 64 61 74 61 62 61 73 65 20 73 63 68 65  mp-database sche
33dbd 6d 61 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ma is allocated 
33dbe 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d  differently from
33dbf 20 74 68 65 20 6f 74 68 65 72 20 73 63 68 65 6d   the other schem
33dc0 61 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 73 20 28  a.  ** objects (
33dc1 75 73 69 6e 67 20 73 71 6c 69 74 65 4d 61 6c 6c  using sqliteMall
33dc2 6f 63 28 29 20 64 69 72 65 63 74 6c 79 2c 20 69  oc() directly, i
33dc3 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65  nstead of sqlite
33dc4 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 29 2e  3BtreeSchema()).
33dc5 0a 20 20 2a 2a 20 53 6f 20 69 74 20 6e 65 65 64  .  ** So it need
33dc6 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 68 65  s to be freed he
33dc7 72 65 2e 20 54 6f 64 6f 3a 20 57 68 79 20 6e 6f  re. Todo: Why no
33dc8 74 20 72 6f 6c 6c 20 74 68 65 20 74 65 6d 70 20  t roll the temp 
33dc9 73 63 68 65 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a  schema into.  **
33dca 20 74 68 65 20 73 61 6d 65 20 73 71 6c 69 74 65   the same sqlite
33dcb 4d 61 6c 6c 6f 63 28 29 20 61 73 20 74 68 65 20  Malloc() as the 
33dcc 6f 6e 65 20 74 68 61 74 20 61 6c 6c 6f 63 61 74  one that allocat
33dcd 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
33dce 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 3f  .  ** structure?
33dcf 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44  .  */.  sqlite3D
33dd0 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44  bFree(db, db->aD
33dd1 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20  b[1].pSchema);. 
33dd2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
33dd3 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
33dd4 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53  .  db->magic = S
33dd5 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53  QLITE_MAGIC_CLOS
33dd6 45 44 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ED;.  sqlite3_mu
33dd7 74 65 78 5f 66 72 65 65 28 64 62 2d 3e 6d 75 74  tex_free(db->mut
33dd8 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  ex);.  assert( d
33dd9 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
33dda 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 46 61 69 6c  t==0 );  /* Fail
33ddb 73 20 6f 6e 20 61 20 6c 6f 6f 6b 61 73 69 64 65  s on a lookaside
33ddc 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a   memory leak */.
33ddd 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
33dde 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b  ide.bMalloced ){
33ddf 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
33de0 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  e(db->lookaside.
33de1 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20 73  pStart);.  }.  s
33de2 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b  qlite3_free(db);
33de3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
33de4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f  _OK;.}../*.** Ro
33de5 6c 6c 62 61 63 6b 20 61 6c 6c 20 64 61 74 61 62  llback all datab
33de6 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51  ase files..*/.SQ
33de7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
33de8 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
33de9 6b 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  kAll(sqlite3 *db
33dea 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
33deb 74 20 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20  t inTrans = 0;. 
33dec 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
33ded 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
33dee 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
33def 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
33df0 6c 6c 6f 63 28 29 3b 0a 20 20 66 6f 72 28 69 3d  lloc();.  for(i=
33df1 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
33df2 2b 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  +){.    if( db->
33df3 61 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20  aDb[i].pBt ){.  
33df4 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
33df5 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 64 62  treeIsInTrans(db
33df6 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b  ->aDb[i].pBt) ){
33df7 0a 20 20 20 20 20 20 20 20 69 6e 54 72 61 6e 73  .        inTrans
33df8 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
33df9 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
33dfa 52 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62  Rollback(db->aDb
33dfb 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20  [i].pBt);.      
33dfc 64 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61  db->aDb[i].inTra
33dfd 6e 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ns = 0;.    }.  
33dfe 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52  }.  sqlite3VtabR
33dff 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20 73  ollback(db);.  s
33e00 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
33e01 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20  alloc();..  if( 
33e02 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
33e03 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29  _InternChanges )
33e04 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
33e05 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
33e06 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 73  ments(db);.    s
33e07 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
33e08 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
33e09 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 79 20  ;.  }..  /* Any 
33e0a 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
33e0b 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 68  int violations h
33e0c 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 72 65 73  ave now been res
33e0d 6f 6c 76 65 64 2e 20 2a 2f 0a 20 20 64 62 2d 3e  olved. */.  db->
33e0e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20  nDeferredCons = 
33e0f 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20  0;..  /* If one 
33e10 68 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75  has been configu
33e11 72 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  red, invoke the 
33e12 72 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61  rollback-hook ca
33e13 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20  llback */.  if( 
33e14 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c  db->xRollbackCal
33e15 6c 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e  lback && (inTran
33e16 73 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f  s || !db->autoCo
33e17 6d 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d  mmit) ){.    db-
33e18 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61  >xRollbackCallba
33e19 63 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b  ck(db->pRollback
33e1a 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Arg);.  }.}../*.
33e1b 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74  ** Return a stat
33e1c 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64  ic string that d
33e1d 65 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e  escribes the kin
33e1e 64 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69  d of error speci
33e1f 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61  fied in the.** a
33e20 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  rgument..*/.SQLI
33e21 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
33e22 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72   char *sqlite3Er
33e23 72 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20  rStr(int rc){.  
33e24 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
33e25 72 2a 20 63 6f 6e 73 74 20 61 4d 73 67 5b 5d 20  r* const aMsg[] 
33e26 3d 20 7b 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  = {.    /* SQLIT
33e27 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 2a 2f  E_OK          */
33e28 20 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 2c   "not an error",
33e29 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45  .    /* SQLITE_E
33e2a 52 52 4f 52 20 20 20 20 20 20 20 2a 2f 20 22 53  RROR       */ "S
33e2b 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f  QL logic error o
33e2c 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61  r missing databa
33e2d 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  se",.    /* SQLI
33e2e 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 2a  TE_INTERNAL    *
33e2f 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  / 0,.    /* SQLI
33e30 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 2a  TE_PERM        *
33e31 2f 20 22 61 63 63 65 73 73 20 70 65 72 6d 69 73  / "access permis
33e32 73 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20  sion denied",.  
33e33 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 42 4f 52    /* SQLITE_ABOR
33e34 54 20 20 20 20 20 20 20 2a 2f 20 22 63 61 6c 6c  T       */ "call
33e35 62 61 63 6b 20 72 65 71 75 65 73 74 65 64 20 71  back requested q
33e36 75 65 72 79 20 61 62 6f 72 74 22 2c 0a 20 20 20  uery abort",.   
33e37 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20   /* SQLITE_BUSY 
33e38 20 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62         */ "datab
33e39 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a  ase is locked",.
33e3a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f      /* SQLITE_LO
33e3b 43 4b 45 44 20 20 20 20 20 20 2a 2f 20 22 64 61  CKED      */ "da
33e3c 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20  tabase table is 
33e3d 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20  locked",.    /* 
33e3e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20  SQLITE_NOMEM    
33e3f 20 20 20 2a 2f 20 22 6f 75 74 20 6f 66 20 6d 65     */ "out of me
33e40 6d 6f 72 79 22 2c 0a 20 20 20 20 2f 2a 20 53 51  mory",.    /* SQ
33e41 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20  LITE_READONLY   
33e42 20 2a 2f 20 22 61 74 74 65 6d 70 74 20 74 6f 20   */ "attempt to 
33e43 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79  write a readonly
33e44 20 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20 20   database",.    
33e45 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  /* SQLITE_INTERR
33e46 55 50 54 20 20 20 2a 2f 20 22 69 6e 74 65 72 72  UPT   */ "interr
33e47 75 70 74 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  upted",.    /* S
33e48 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20  QLITE_IOERR     
33e49 20 20 2a 2f 20 22 64 69 73 6b 20 49 2f 4f 20 65    */ "disk I/O e
33e4a 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51  rror",.    /* SQ
33e4b 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20  LITE_CORRUPT    
33e4c 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 64 69   */ "database di
33e4d 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66  sk image is malf
33e4e 6f 72 6d 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  ormed",.    /* S
33e4f 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20  QLITE_NOTFOUND  
33e50 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53    */ 0,.    /* S
33e51 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20  QLITE_FULL      
33e52 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 6f    */ "database o
33e53 72 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 22 2c  r disk is full",
33e54 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
33e55 41 4e 54 4f 50 45 4e 20 20 20 20 2a 2f 20 22 75  ANTOPEN    */ "u
33e56 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61  nable to open da
33e57 74 61 62 61 73 65 20 66 69 6c 65 22 2c 0a 20 20  tabase file",.  
33e58 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 52 4f 54    /* SQLITE_PROT
33e59 4f 43 4f 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20  OCOL    */ 0,.  
33e5a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54    /* SQLITE_EMPT
33e5b 59 20 20 20 20 20 20 20 2a 2f 20 22 74 61 62 6c  Y       */ "tabl
33e5c 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61  e contains no da
33e5d 74 61 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ta",.    /* SQLI
33e5e 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20 2a  TE_SCHEMA      *
33e5f 2f 20 22 64 61 74 61 62 61 73 65 20 73 63 68 65  / "database sche
33e60 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 22 2c  ma has changed",
33e61 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54  .    /* SQLITE_T
33e62 4f 4f 42 49 47 20 20 20 20 20 20 2a 2f 20 22 73  OOBIG      */ "s
33e63 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f  tring or blob to
33e64 6f 20 62 69 67 22 2c 0a 20 20 20 20 2f 2a 20 53  o big",.    /* S
33e65 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
33e66 20 20 2a 2f 20 22 63 6f 6e 73 74 72 61 69 6e 74    */ "constraint
33e67 20 66 61 69 6c 65 64 22 2c 0a 20 20 20 20 2f 2a   failed",.    /*
33e68 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48   SQLITE_MISMATCH
33e69 20 20 20 20 2a 2f 20 22 64 61 74 61 74 79 70 65      */ "datatype
33e6a 20 6d 69 73 6d 61 74 63 68 22 2c 0a 20 20 20 20   mismatch",.    
33e6b 2f 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  /* SQLITE_MISUSE
33e6c 20 20 20 20 20 20 2a 2f 20 22 6c 69 62 72 61 72        */ "librar
33e6d 79 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 65 64  y routine called
33e6e 20 6f 75 74 20 6f 66 20 73 65 71 75 65 6e 63 65   out of sequence
33e6f 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33e70 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 2a 2f 20  _NOLFS       */ 
33e71 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70  "large file supp
33e72 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65 64 22  ort is disabled"
33e73 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33e74 41 55 54 48 20 20 20 20 20 20 20 20 2a 2f 20 22  AUTH        */ "
33e75 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65  authorization de
33e76 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  nied",.    /* SQ
33e77 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20 20  LITE_FORMAT     
33e78 20 2a 2f 20 22 61 75 78 69 6c 69 61 72 79 20 64   */ "auxiliary d
33e79 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65  atabase format e
33e7a 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51  rror",.    /* SQ
33e7b 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20 20  LITE_RANGE      
33e7c 20 2a 2f 20 22 62 69 6e 64 20 6f 72 20 63 6f 6c   */ "bind or col
33e7d 75 6d 6e 20 69 6e 64 65 78 20 6f 75 74 20 6f 66  umn index out of
33e7e 20 72 61 6e 67 65 22 2c 0a 20 20 20 20 2f 2a 20   range",.    /* 
33e7f 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
33e80 20 20 20 2a 2f 20 22 66 69 6c 65 20 69 73 20 65     */ "file is e
33e81 6e 63 72 79 70 74 65 64 20 6f 72 20 69 73 20 6e  ncrypted or is n
33e82 6f 74 20 61 20 64 61 74 61 62 61 73 65 22 2c 0a  ot a database",.
33e83 20 20 7d 3b 0a 20 20 72 63 20 26 3d 20 30 78 66    };.  rc &= 0xf
33e84 66 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  f;.  if( ALWAYS(
33e85 72 63 3e 3d 30 29 20 26 26 20 72 63 3c 28 69 6e  rc>=0) && rc<(in
33e86 74 29 28 73 69 7a 65 6f 66 28 61 4d 73 67 29 2f  t)(sizeof(aMsg)/
33e87 73 69 7a 65 6f 66 28 61 4d 73 67 5b 30 5d 29 29  sizeof(aMsg[0]))
33e88 20 26 26 20 61 4d 73 67 5b 72 63 5d 21 3d 30 20   && aMsg[rc]!=0 
33e89 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 4d  ){.    return aM
33e8a 73 67 5b 72 63 5d 3b 0a 20 20 7d 65 6c 73 65 7b  sg[rc];.  }else{
33e8b 0a 20 20 20 20 72 65 74 75 72 6e 20 22 75 6e 6b  .    return "unk
33e8c 6e 6f 77 6e 20 65 72 72 6f 72 22 3b 0a 20 20 7d  nown error";.  }
33e8d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
33e8e 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74  outine implement
33e8f 73 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61 63  s a busy callbac
33e90 6b 20 74 68 61 74 20 73 6c 65 65 70 73 20 61 6e  k that sleeps an
33e91 64 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e  d tries.** again
33e92 20 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75 74   until a timeout
33e93 20 76 61 6c 75 65 20 69 73 20 72 65 61 63 68 65   value is reache
33e94 64 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74 20  d.  The timeout 
33e95 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69  value is.** an i
33e96 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66  nteger number of
33e97 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61   milliseconds pa
33e98 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 66  ssed in as the f
33e99 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
33e9a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
33e9b 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73  sqliteDefaultBus
33e9c 79 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64  yCallback(. void
33e9d 20 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20 20   *ptr,          
33e9e 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
33e9f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
33ea0 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20  int count       
33ea1 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
33ea2 65 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62 6c  er of times tabl
33ea3 65 20 68 61 73 20 62 65 65 6e 20 62 75 73 79 20  e has been busy 
33ea4 2a 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54 45  */.){.#if SQLITE
33ea5 5f 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69  _OS_WIN || (defi
33ea6 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29  ned(HAVE_USLEEP)
33ea7 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29   && HAVE_USLEEP)
33ea8 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
33ea9 75 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20  u8 delays[] =.  
33eaa 20 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30     { 1, 2, 5, 10
33eab 2c 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35  , 15, 20, 25, 25
33eac 2c 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c  ,  25,  50,  50,
33ead 20 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63   100 };.  static
33eae 20 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73   const u8 totals
33eaf 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31  [] =.     { 0, 1
33eb0 2c 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c  , 3,  8, 18, 33,
33eb1 20 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32   53, 78, 103, 12
33eb2 38 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23  8, 178, 228 };.#
33eb3 20 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 28   define NDELAY (
33eb4 73 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f 73  sizeof(delays)/s
33eb5 69 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29  izeof(delays[0])
33eb6 29 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ).  sqlite3 *db 
33eb7 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72  = (sqlite3 *)ptr
33eb8 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20  ;.  int timeout 
33eb9 3d 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75  = db->busyTimeou
33eba 74 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20  t;.  int delay, 
33ebb 70 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74  prior;..  assert
33ebc 28 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20  ( count>=0 );.  
33ebd 69 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c  if( count < NDEL
33ebe 41 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20  AY ){.    delay 
33ebf 3d 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b  = delays[count];
33ec0 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74  .    prior = tot
33ec1 61 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65  als[count];.  }e
33ec2 6c 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d  lse{.    delay =
33ec3 20 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31   delays[NDELAY-1
33ec4 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74  ];.    prior = t
33ec5 6f 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20  otals[NDELAY-1] 
33ec6 2b 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28  + delay*(count-(
33ec7 4e 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a  NDELAY-1));.  }.
33ec8 20 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65    if( prior + de
33ec9 6c 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b  lay > timeout ){
33eca 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d  .    delay = tim
33ecb 65 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20  eout - prior;.  
33ecc 20 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29    if( delay<=0 )
33ecd 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
33ece 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28   sqlite3OsSleep(
33ecf 64 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a  db->pVfs, delay*
33ed0 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  1000);.  return 
33ed1 31 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74  1;.#else.  sqlit
33ed2 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65  e3 *db = (sqlite
33ed3 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74  3 *)ptr;.  int t
33ed4 69 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74  imeout = ((sqlit
33ed5 65 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54  e3 *)ptr)->busyT
33ed6 69 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63  imeout;.  if( (c
33ed7 6f 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74  ount+1)*1000 > t
33ed8 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65  imeout ){.    re
33ed9 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71  turn 0;.  }.  sq
33eda 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d  lite3OsSleep(db-
33edb 3e 70 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b  >pVfs, 1000000);
33edc 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e  .  return 1;.#en
33edd 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  dif.}../*.** Inv
33ede 6f 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62 75  oke the given bu
33edf 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a  sy handler..**.*
33ee0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
33ee1 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  s called when an
33ee2 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65   operation faile
33ee3 64 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a  d with a lock..*
33ee4 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
33ee5 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  e returns non-ze
33ee6 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ro, the lock is 
33ee7 72 65 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a  retried.  If it.
33ee8 2a 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68  ** returns 0, th
33ee9 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72  e operation abor
33eea 74 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54  ts with an SQLIT
33eeb 45 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f  E_BUSY error..*/
33eec 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33eed 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b  int sqlite3Invok
33eee 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73  eBusyHandler(Bus
33eef 79 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20  yHandler *p){.  
33ef0 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e 45  int rc;.  if( NE
33ef1 56 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e  VER(p==0) || p->
33ef2 78 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e  xFunc==0 || p->n
33ef3 42 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20  Busy<0 ) return 
33ef4 30 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75  0;.  rc = p->xFu
33ef5 6e 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e  nc(p->pArg, p->n
33ef6 42 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d  Busy);.  if( rc=
33ef7 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75  =0 ){.    p->nBu
33ef8 73 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65  sy = -1;.  }else
33ef9 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b  {.    p->nBusy++
33efa 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
33efb 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c; .}../*.** Thi
33efc 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
33efd 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
33efe 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64   for an Sqlite d
33eff 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a  atabase to the.*
33f00 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b  * given callback
33f01 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
33f02 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e  he given argumen
33f03 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
33f04 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73   int sqlite3_bus
33f05 79 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c  y_handler(.  sql
33f06 69 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20  ite3 *db,.  int 
33f07 28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69  (*xBusy)(void*,i
33f08 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72  nt),.  void *pAr
33f09 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  g.){.  sqlite3_m
33f0a 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
33f0b 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73  utex);.  db->bus
33f0c 79 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d  yHandler.xFunc =
33f0d 20 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75   xBusy;.  db->bu
33f0e 73 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d  syHandler.pArg =
33f0f 20 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73   pArg;.  db->bus
33f10 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d  yHandler.nBusy =
33f11 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   0;.  sqlite3_mu
33f12 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
33f13 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
33f14 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
33f15 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33f16 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41  _PROGRESS_CALLBA
33f17 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  CK./*.** This ro
33f18 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70  utine sets the p
33f19 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
33f1a 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64   for an Sqlite d
33f1b 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a  atabase to the.*
33f1c 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b  * given callback
33f1d 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
33f1e 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e  he given argumen
33f1f 74 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73 20  t. The progress 
33f20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a  callback will.**
33f21 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72   be invoked ever
33f22 79 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a  y nOps opcodes..
33f23 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
33f24 69 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  id sqlite3_progr
33f25 65 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73  ess_handler(.  s
33f26 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 69  qlite3 *db, .  i
33f27 6e 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28  nt nOps,.  int (
33f28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64  *xProgress)(void
33f29 2a 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 72  *), .  void *pAr
33f2a 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  g.){.  sqlite3_m
33f2b 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
33f2c 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f 70  utex);.  if( nOp
33f2d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78  s>0 ){.    db->x
33f2e 50 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f 67  Progress = xProg
33f2f 72 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e 50  ress;.    db->nP
33f30 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70  rogressOps = nOp
33f31 73 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67  s;.    db->pProg
33f32 72 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b 0a  ressArg = pArg;.
33f33 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d    }else{.    db-
33f34 3e 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a  >xProgress = 0;.
33f35 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73      db->nProgres
33f36 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64 62  sOps = 0;.    db
33f37 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d  ->pProgressArg =
33f38 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
33f39 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
33f3a 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64  ->mutex);.}.#end
33f3b 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
33f3c 72 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73  routine installs
33f3d 20 61 20 64 65 66 61 75 6c 74 20 62 75 73 79 20   a default busy 
33f3e 68 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61 69  handler that wai
33f3f 74 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70  ts for the.** sp
33f40 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f  ecified number o
33f41 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62  f milliseconds b
33f42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
33f43 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0..*/.SQLITE_API
33f44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73   int sqlite3_bus
33f45 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65  y_timeout(sqlite
33f46 33 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a  3 *db, int ms){.
33f47 20 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20    if( ms>0 ){.  
33f48 20 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75    db->busyTimeou
33f49 74 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69  t = ms;.    sqli
33f4a 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
33f4b 28 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61 75  (db, sqliteDefau
33f4c 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20  ltBusyCallback, 
33f4d 28 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65  (void*)db);.  }e
33f4e 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
33f4f 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62  _busy_handler(db
33f50 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 0, 0);.  }.  r
33f51 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33f52 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20  .}../*.** Cause 
33f53 61 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65 72  any pending oper
33f54 61 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74  ation to stop at
33f55 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70   its earliest op
33f56 70 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51  portunity..*/.SQ
33f57 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
33f58 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
33f59 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
33f5a 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
33f5b 70 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a  pted = 1;.}.../*
33f5c 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
33f5d 6e 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65  n is exactly the
33f5e 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33   same as sqlite3
33f5f 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33f60 28 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74 68  (), except.** th
33f61 61 74 20 69 74 20 69 73 20 64 65 73 69 67 6e 65  at it is designe
33f62 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62  d to be called b
33f63 79 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e  y internal code.
33f64 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
33f65 69 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20  is.** that if a 
33f66 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 69  malloc() fails i
33f67 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  n sqlite3_create
33f68 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20  _function(), an 
33f69 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73  error code.** is
33f6a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
33f6b 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
33f6c 6c 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2f  lag cleared. .*/
33f6d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
33f6e 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74  int sqlite3Creat
33f6f 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
33f70 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68   *db,.  const ch
33f71 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  ar *zFunctionNam
33f72 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20  e,.  int nArg,. 
33f73 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64   int enc,.  void
33f74 20 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20 76   *pUserData,.  v
33f75 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
33f76 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
33f77 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
33f78 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
33f79 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
33f7a 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
33f7b 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76  3_value **),.  v
33f7c 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
33f7d 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a  lite3_context*).
33f7e 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b  ){.  FuncDef *p;
33f7f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20  .  int nName;.. 
33f80 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
33f81 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
33f82 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
33f83 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30  zFunctionName==0
33f84 20 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e 63   ||.      (xFunc
33f85 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78   && (xFinal || x
33f86 53 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20 20  Step)) || .     
33f87 20 28 21 78 46 75 6e 63 20 26 26 20 28 78 46 69   (!xFunc && (xFi
33f88 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29 29 20  nal && !xStep)) 
33f89 7c 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63  ||.      (!xFunc
33f8a 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26 20   && (!xFinal && 
33f8b 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20  xStep)) ||.     
33f8c 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72   (nArg<-1 || nAr
33f8d 67 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  g>SQLITE_MAX_FUN
33f8e 43 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20 20  CTION_ARG) ||.  
33f8f 20 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20      (255<(nName 
33f90 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
33f91 30 28 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  0( zFunctionName
33f92 29 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ))) ){.    retur
33f93 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
33f94 0a 20 20 7d 0a 20 20 0a 23 69 66 6e 64 65 66 20  .  }.  .#ifndef 
33f95 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
33f96 36 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45  6.  /* If SQLITE
33f97 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66  _UTF16 is specif
33f98 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64  ied as the encod
33f99 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66  ing type, transf
33f9a 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f  orm this.  ** to
33f9b 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
33f9c 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
33f9d 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74  _UTF16BE using t
33f9e 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  he.  ** SQLITE_U
33f9f 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
33fa0 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
33fa1 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
33fa2 6e 61 6c 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  nally..  **.  **
33fa3 20 49 66 20 53 51 4c 49 54 45 5f 41 4e 59 20 69   If SQLITE_ANY i
33fa4 73 20 73 70 65 63 69 66 69 65 64 2c 20 61 64 64  s specified, add
33fa5 20 74 68 72 65 65 20 76 65 72 73 69 6f 6e 73 20   three versions 
33fa6 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a  of the function.
33fa7 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73 68    ** to the hash
33fa8 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
33fa9 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
33faa 54 46 31 36 20 29 7b 0a 20 20 20 20 65 6e 63 20  TF16 ){.    enc 
33fab 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  = SQLITE_UTF16NA
33fac 54 49 56 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66  TIVE;.  }else if
33fad 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 41 4e  ( enc==SQLITE_AN
33fae 59 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  Y ){.    int rc;
33faf 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
33fb0 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
33fb1 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e  zFunctionName, n
33fb2 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Arg, SQLITE_UTF8
33fb3 2c 0a 20 20 20 20 20 20 20 20 20 70 55 73 65 72  ,.         pUser
33fb4 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53 74  Data, xFunc, xSt
33fb5 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20  ep, xFinal);.   
33fb6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33fb7 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
33fb8 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
33fb9 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
33fba 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
33fbb 54 45 5f 55 54 46 31 36 4c 45 2c 0a 20 20 20 20  TE_UTF16LE,.    
33fbc 20 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c        pUserData,
33fbd 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
33fbe 46 69 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  Final);.    }.  
33fbf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33fc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
33fc1 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
33fc2 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55    enc = SQLITE_U
33fc3 54 46 31 36 42 45 3b 0a 20 20 7d 0a 23 65 6c 73  TF16BE;.  }.#els
33fc4 65 0a 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45  e.  enc = SQLITE
33fc5 5f 55 54 46 38 3b 0a 23 65 6e 64 69 66 0a 20 20  _UTF8;.#endif.  
33fc6 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
33fc7 6e 20 65 78 69 73 74 69 6e 67 20 66 75 6e 63 74  n existing funct
33fc8 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6f 76 65  ion is being ove
33fc9 72 72 69 64 64 65 6e 20 6f 72 20 64 65 6c 65 74  rridden or delet
33fca 65 64 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20  ed. If so,.  ** 
33fcb 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 63  and there are ac
33fcc 74 69 76 65 20 56 4d 73 2c 20 74 68 65 6e 20 72  tive VMs, then r
33fcd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
33fce 59 2e 20 49 66 20 61 20 66 75 6e 63 74 69 6f 6e  Y. If a function
33fcf 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 6f  .  ** is being o
33fd0 76 65 72 72 69 64 64 65 6e 2f 64 65 6c 65 74 65  verridden/delete
33fd1 64 20 62 75 74 20 74 68 65 72 65 20 61 72 65 20  d but there are 
33fd2 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c 20 61  no active VMs, a
33fd3 6c 6c 6f 77 20 74 68 65 0a 20 20 2a 2a 20 6f 70  llow the.  ** op
33fd4 65 72 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 69  eration to conti
33fd5 6e 75 65 20 62 75 74 20 69 6e 76 61 6c 69 64 61  nue but invalida
33fd6 74 65 20 61 6c 6c 20 70 72 65 63 6f 6d 70 69 6c  te all precompil
33fd7 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  ed statements.. 
33fd8 20 2a 2f 0a 20 20 70 20 3d 20 73 71 6c 69 74 65   */.  p = sqlite
33fd9 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
33fda 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  , zFunctionName,
33fdb 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 28 75   nName, nArg, (u
33fdc 38 29 65 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28  8)enc, 0);.  if(
33fdd 20 70 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e   p && p->iPrefEn
33fde 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e 6e 41 72  c==enc && p->nAr
33fdf 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20 20 20 69  g==nArg ){.    i
33fe0 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  f( db->activeVdb
33fe1 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  eCnt ){.      sq
33fe2 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
33fe3 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
33fe4 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
33fe5 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 75 73  delete/modify us
33fe6 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 75 65 20  er-function due 
33fe7 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d  to active statem
33fe8 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 61 73  ents");.      as
33fe9 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
33fea 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
33feb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
33fec 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  USY;.    }else{.
33fed 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
33fee 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
33fef 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 7d  ments(db);.    }
33ff0 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69  .  }..  p = sqli
33ff1 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
33ff2 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  db, zFunctionNam
33ff3 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  e, nName, nArg, 
33ff4 28 75 38 29 65 6e 63 2c 20 31 29 3b 0a 20 20 61  (u8)enc, 1);.  a
33ff5 73 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d  ssert(p || db->m
33ff6 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20  allocFailed);.  
33ff7 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
33ff8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
33ff9 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67  M;.  }.  p->flag
33ffa 73 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e  s = 0;.  p->xFun
33ffb 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e  c = xFunc;.  p->
33ffc 78 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a 20  xStep = xStep;. 
33ffd 20 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20   p->xFinalize = 
33ffe 78 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73  xFinal;.  p->pUs
33fff 65 72 44 61 74 61 20 3d 20 70 55 73 65 72 44 61  erData = pUserDa
34000 74 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20  ta;.  p->nArg = 
34001 28 75 31 36 29 6e 41 72 67 3b 0a 20 20 72 65 74  (u16)nArg;.  ret
34002 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
34003 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 6e  ../*.** Create n
34004 65 77 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ew user function
34005 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
34006 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
34007 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
34008 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
34009 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
3400a 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
3400b 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c  nArg,.  int enc,
3400c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f  .  void *p,.  vo
3400d 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
3400e 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
3400f 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
34010 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
34011 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
34012 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
34013 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
34014 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
34015 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
34016 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
34017 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
34018 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
34019 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
3401a 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
3401b 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  tionName, nArg, 
3401c 65 6e 63 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78  enc, p, xFunc, x
3401d 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
3401e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
3401f 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
34020 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
34021 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
34022 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
34023 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
34024 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
34025 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
34026 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
34027 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
34028 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  b,.  const void 
34029 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
3402a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
3402b 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f  t eTextRep,.  vo
3402c 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a  id *p,.  void (*
3402d 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
3402e 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
3402f 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
34030 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
34031 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
34032 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
34033 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46  **),.  void (*xF
34034 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  inal)(sqlite3_co
34035 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74  ntext*).){.  int
34036 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75   rc;.  char *zFu
34037 6e 63 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  nc8;.  sqlite3_m
34038 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
34039 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
3403a 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
3403b 65 64 20 29 3b 0a 20 20 7a 46 75 6e 63 38 20 3d  ed );.  zFunc8 =
3403c 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38   sqlite3Utf16to8
3403d 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
3403e 6d 65 2c 20 2d 31 29 3b 0a 20 20 72 63 20 3d 20  me, -1);.  rc = 
3403f 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
34040 63 28 64 62 2c 20 7a 46 75 6e 63 38 2c 20 6e 41  c(db, zFunc8, nA
34041 72 67 2c 20 65 54 65 78 74 52 65 70 2c 20 70 2c  rg, eTextRep, p,
34042 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
34043 46 69 6e 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65  Final);.  sqlite
34044 33 44 62 46 72 65 65 28 64 62 2c 20 7a 46 75 6e  3DbFree(db, zFun
34045 63 38 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  c8);.  rc = sqli
34046 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
34047 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
34048 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
34049 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
3404a 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  c;.}.#endif.../*
3404b 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 68 61 74  .** Declare that
3404c 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20   a function has 
3404d 62 65 65 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20  been overloaded 
3404e 62 79 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  by a virtual tab
3404f 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
34050 20 66 75 6e 63 74 69 6f 6e 20 61 6c 72 65 61 64   function alread
34051 79 20 65 78 69 73 74 73 20 61 73 20 61 20 72 65  y exists as a re
34052 67 75 6c 61 72 20 67 6c 6f 62 61 6c 20 66 75 6e  gular global fun
34053 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 74  ction, then.** t
34054 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
34055 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 74 68 65 20   no-op.  If the 
34056 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
34057 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 63 72  t exist, then cr
34058 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e  eate.** a new on
34059 65 20 74 68 61 74 20 61 6c 77 61 79 73 20 74 68  e that always th
3405a 72 6f 77 73 20 61 20 72 75 6e 2d 74 69 6d 65 20  rows a run-time 
3405b 65 72 72 6f 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 57  error.  .**.** W
3405c 68 65 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c  hen virtual tabl
3405d 65 73 20 69 6e 74 65 6e 64 20 74 6f 20 70 72 6f  es intend to pro
3405e 76 69 64 65 20 61 6e 20 6f 76 65 72 6c 6f 61 64  vide an overload
3405f 65 64 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65  ed function, the
34060 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c  y.** should call
34061 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
34062 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67   make sure the g
34063 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 65  lobal function e
34064 78 69 73 74 73 2e 0a 2a 2a 20 41 20 67 6c 6f 62  xists..** A glob
34065 61 6c 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  al function must
34066 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 20   exist in order 
34067 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  for name resolut
34068 69 6f 6e 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 70  ion to work.** p
34069 72 6f 70 65 72 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  roperly..*/.SQLI
3406a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3406b 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
3406c 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  tion(.  sqlite3 
3406d 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
3406e 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
3406f 6e 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20 6e 4e  nArg.){.  int nN
34070 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
34071 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
34072 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
34073 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
34074 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
34075 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
34076 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e  ion(db, zName, n
34077 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
34078 54 45 5f 55 54 46 38 2c 20 30 29 3d 3d 30 20 29  TE_UTF8, 0)==0 )
34079 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65  {.    sqlite3Cre
3407a 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 4e 61 6d  ateFunc(db, zNam
3407b 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f  e, nArg, SQLITE_
3407c 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20 20  UTF8,.          
3407d 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 73              0, s
3407e 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e  qlite3InvalidFun
3407f 63 74 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  ction, 0, 0);.  
34080 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
34081 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49  ApiExit(db, SQLI
34082 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65  TE_OK);.  sqlite
34083 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
34084 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
34085 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
34086 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
34087 41 43 45 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  ACE./*.** Regist
34088 65 72 20 61 20 74 72 61 63 65 20 66 75 6e 63 74  er a trace funct
34089 69 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66  ion.  The pArg f
3408a 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73  rom the previous
3408b 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 74 72  ly registered tr
3408c 61 63 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ace.** is return
3408d 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ed.  .**.** A NU
3408e 4c 4c 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f  LL trace functio
3408f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20  n means that no 
34090 74 72 61 63 69 6e 67 20 69 73 20 65 78 65 63 75  tracing is execu
34091 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c  tes.  A non-NULL
34092 0a 2a 2a 20 74 72 61 63 65 20 69 73 20 61 20 70  .** trace is a p
34093 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
34094 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76  tion that is inv
34095 6f 6b 65 64 20 61 74 20 74 68 65 20 73 74 61 72  oked at the star
34096 74 20 6f 66 20 65 61 63 68 0a 2a 2a 20 53 51 4c  t of each.** SQL
34097 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
34098 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
34099 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71  sqlite3_trace(sq
3409a 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
3409b 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c  (*xTrace)(void*,
3409c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f  const char*), vo
3409d 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 76 6f 69  id *pArg){.  voi
3409e 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74  d *pOld;.  sqlit
3409f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
340a0 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c  b->mutex);.  pOl
340a1 64 20 3d 20 64 62 2d 3e 70 54 72 61 63 65 41 72  d = db->pTraceAr
340a2 67 3b 0a 20 20 64 62 2d 3e 78 54 72 61 63 65 20  g;.  db->xTrace 
340a3 3d 20 78 54 72 61 63 65 3b 0a 20 20 64 62 2d 3e  = xTrace;.  db->
340a4 70 54 72 61 63 65 41 72 67 20 3d 20 70 41 72 67  pTraceArg = pArg
340a5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
340a6 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
340a7 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c  x);.  return pOl
340a8 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  d;.}./*.** Regis
340a9 74 65 72 20 61 20 70 72 6f 66 69 6c 65 20 66 75  ter a profile fu
340aa 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 70 41 72  nction.  The pAr
340ab 67 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69  g from the previ
340ac 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
340ad 20 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75 6e   .** profile fun
340ae 63 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65  ction is returne
340af 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c  d.  .**.** A NUL
340b0 4c 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69  L profile functi
340b1 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f  on means that no
340b2 20 70 72 6f 66 69 6c 69 6e 67 20 69 73 20 65 78   profiling is ex
340b3 65 63 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e  ecutes.  A non-N
340b4 55 4c 4c 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 69  ULL.** profile i
340b5 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
340b6 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69   function that i
340b7 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65  s invoked at the
340b8 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a 2a   conclusion of.*
340b9 2a 20 65 61 63 68 20 53 51 4c 20 73 74 61 74 65  * each SQL state
340ba 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72 75 6e  ment that is run
340bb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
340bc 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72  void *sqlite3_pr
340bd 6f 66 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  ofile(.  sqlite3
340be 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 28 2a 78   *db,.  void (*x
340bf 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63  Profile)(void*,c
340c0 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
340c1 65 5f 75 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  e_uint64),.  voi
340c2 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 76 6f 69  d *pArg.){.  voi
340c3 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74  d *pOld;.  sqlit
340c4 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
340c5 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c  b->mutex);.  pOl
340c6 64 20 3d 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65  d = db->pProfile
340c7 41 72 67 3b 0a 20 20 64 62 2d 3e 78 50 72 6f 66  Arg;.  db->xProf
340c8 69 6c 65 20 3d 20 78 50 72 6f 66 69 6c 65 3b 0a  ile = xProfile;.
340c9 20 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72    db->pProfileAr
340ca 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
340cb 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
340cc 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
340cd 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 23 65 6e  turn pOld;.}.#en
340ce 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
340cf 49 54 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 2a  IT_TRACE */../**
340d0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2a  * EXPERIMENTAL *
340d1 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65  **.**.** Registe
340d2 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  r a function to 
340d3 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20  be invoked when 
340d4 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  a transaction co
340d5 6d 6d 65 6e 74 73 2e 0a 2a 2a 20 49 66 20 74 68  mments..** If th
340d6 65 20 69 6e 76 6f 6b 65 64 20 66 75 6e 63 74 69  e invoked functi
340d7 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  on returns non-z
340d8 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ero, then the co
340d9 6d 6d 69 74 20 62 65 63 6f 6d 65 73 20 61 0a 2a  mmit becomes a.*
340da 2a 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53  * rollback..*/.S
340db 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
340dc 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
340dd 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ook(.  sqlite3 *
340de 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
340df 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 68   /* Attach the h
340e0 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61  ook to this data
340e1 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  base */.  int (*
340e2 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  xCallback)(void*
340e3 29 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20  ),  /* Function 
340e4 74 6f 20 69 6e 76 6f 6b 65 20 6f 6e 20 65 61 63  to invoke on eac
340e5 68 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 6f  h commit */.  vo
340e6 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20  id *pArg        
340e7 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
340e8 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
340e9 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64  ion */.){.  void
340ea 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65   *pOld;.  sqlite
340eb 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
340ec 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64  ->mutex);.  pOld
340ed 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72   = db->pCommitAr
340ee 67 3b 0a 20 20 64 62 2d 3e 78 43 6f 6d 6d 69 74  g;.  db->xCommit
340ef 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
340f0 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d  back;.  db->pCom
340f1 6d 69 74 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  mitArg = pArg;. 
340f2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
340f3 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
340f4 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a  .  return pOld;.
340f5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
340f6 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  r a callback to 
340f7 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20  be invoked each 
340f8 74 69 6d 65 20 61 20 72 6f 77 20 69 73 20 75 70  time a row is up
340f9 64 61 74 65 64 2c 0a 2a 2a 20 69 6e 73 65 72 74  dated,.** insert
340fa 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 75 73  ed or deleted us
340fb 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73  ing this databas
340fc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
340fd 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
340fe 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65   *sqlite3_update
340ff 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33  _hook(.  sqlite3
34100 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
34101 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65     /* Attach the
34102 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61   hook to this da
34103 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64  tabase */.  void
34104 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
34105 69 64 2a 2c 69 6e 74 2c 63 68 61 72 20 63 6f 6e  id*,int,char con
34106 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20  st *,char const 
34107 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c  *,sqlite_int64),
34108 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20  .  void *pArg   
34109 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3410a 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
3410b 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
3410c 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73   void *pRet;.  s
3410d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3410e 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3410f 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 55 70 64   pRet = db->pUpd
34110 61 74 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78 55  ateArg;.  db->xU
34111 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 3d 20  pdateCallback = 
34112 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d  xCallback;.  db-
34113 3e 70 55 70 64 61 74 65 41 72 67 20 3d 20 70 41  >pUpdateArg = pA
34114 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
34115 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
34116 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
34117 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  Ret;.}../*.** Re
34118 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63  gister a callbac
34119 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  k to be invoked 
3411a 65 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e  each time a tran
3411b 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
3411c 64 0a 2a 2a 20 62 61 63 6b 20 62 79 20 74 68 69  d.** back by thi
3411d 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
3411e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
3411f 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
34120 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
34121 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
34122 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34123 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
34124 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
34125 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43  e */.  void (*xC
34126 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c  allback)(void*),
34127 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e   /* Callback fun
34128 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
34129 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20  *pArg           
3412a 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
3412b 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
3412c 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70   */.){.  void *p
3412d 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Ret;.  sqlite3_m
3412e 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3412f 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20  utex);.  pRet = 
34130 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
34131 3b 0a 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63  ;.  db->xRollbac
34132 6b 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c  kCallback = xCal
34133 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 52 6f  lback;.  db->pRo
34134 6c 6c 62 61 63 6b 41 72 67 20 3d 20 70 41 72 67  llbackArg = pArg
34135 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
34136 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
34137 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  x);.  return pRe
34138 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  t;.}../*.** This
34139 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
3413a 73 20 74 72 75 65 20 69 66 20 6d 61 69 6e 2d 6d  s true if main-m
3413b 65 6d 6f 72 79 20 73 68 6f 75 6c 64 20 62 65 20  emory should be 
3413c 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a  used instead of.
3413d 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  ** a temporary f
3413e 69 6c 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e  ile for transien
3413f 74 20 70 61 67 65 72 20 66 69 6c 65 73 20 61 6e  t pager files an
34140 64 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  d statement jour
34141 6e 61 6c 73 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  nals..** The val
34142 75 65 20 72 65 74 75 72 6e 65 64 20 64 65 70 65  ue returned depe
34143 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
34144 20 6f 66 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f   of db->temp_sto
34145 72 65 20 28 72 75 6e 74 69 6d 65 0a 2a 2a 20 70  re (runtime.** p
34146 61 72 61 6d 65 74 65 72 29 20 61 6e 64 20 74 68  arameter) and th
34147 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 76  e compile time v
34148 61 6c 75 65 20 6f 66 20 53 51 4c 49 54 45 5f 54  alue of SQLITE_T
34149 45 4d 50 5f 53 54 4f 52 45 2e 20 54 68 65 0a 2a  EMP_STORE. The.*
3414a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  * following tabl
3414b 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
3414c 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 62 65 74  relationship bet
3414d 77 65 65 6e 20 74 68 65 73 65 20 74 77 6f 20 76  ween these two v
3414e 61 6c 75 65 73 0a 2a 2a 20 61 6e 64 20 74 68 69  alues.** and thi
3414f 73 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  s functions retu
34150 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  rn value..**.** 
34151 20 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54    SQLITE_TEMP_ST
34152 4f 52 45 20 20 20 20 20 64 62 2d 3e 74 65 6d 70  ORE     db->temp
34153 5f 73 74 6f 72 65 20 20 20 20 20 4c 6f 63 61 74  _store     Locat
34154 69 6f 6e 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  ion of temporary
34155 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 2d   database.**   -
34156 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34157 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
34158 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ---     --------
34159 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3415a 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20  ------.**   0   
3415b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3415c 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
3415d 20 20 20 20 20 66 69 6c 65 20 20 20 20 20 20 28       file      (
3415e 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31  return 0).**   1
3415f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34160 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
34161 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
34162 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
34163 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
34164 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
34165 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72             memor
34166 79 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a  y    (return 1).
34167 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20 20  **   1          
34168 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20             0    
34169 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
3416a 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e 20  le      (return 
3416b 30 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  0).**   2       
3416c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
3416d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3416e 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74 75   file      (retu
3416f 72 6e 20 30 29 0a 2a 2a 20 20 20 32 20 20 20 20  rn 0).**   2    
34170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34171 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
34172 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
34173 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 32 20  eturn 1).**   2 
34174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34175 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
34176 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20         memory   
34177 20 28 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20   (return 1).**  
34178 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20   3              
34179 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20 20         any      
3417a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79            memory
3417b 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a 2a      (return 1).*
3417c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3417d 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d 70   int sqlite3Temp
3417e 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73  InMemory(const s
3417f 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23 69 66  qlite3 *db){.#if
34180 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
34181 52 45 3d 3d 31 0a 20 20 72 65 74 75 72 6e 20 28  RE==1.  return (
34182 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d   db->temp_store=
34183 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  =2 );.#endif.#if
34184 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
34185 52 45 3d 3d 32 0a 20 20 72 65 74 75 72 6e 20 28  RE==2.  return (
34186 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 21   db->temp_store!
34187 3d 31 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  =1 );.#endif.#if
34188 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
34189 52 45 3d 3d 33 0a 20 20 72 65 74 75 72 6e 20 31  RE==3.  return 1
3418a 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ;.#endif.#if SQL
3418b 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3c 31  ITE_TEMP_STORE<1
3418c 20 7c 7c 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f   || SQLITE_TEMP_
3418d 53 54 4f 52 45 3e 33 0a 20 20 72 65 74 75 72 6e  STORE>3.  return
3418e 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a   0;.#endif.}../*
3418f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
34190 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72   is called to cr
34191 65 61 74 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f  eate a connectio
34192 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  n to a database 
34193 42 54 72 65 65 0a 2a 2a 20 64 72 69 76 65 72 2e  BTree.** driver.
34194 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69    If zFilename i
34195 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
34196 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 61 74 20  file, then that 
34197 66 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65  file is.** opene
34198 64 20 61 6e 64 20 75 73 65 64 2e 20 20 49 66 20  d and used.  If 
34199 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65  zFilename is the
3419a 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65   magic name ":me
3419b 6d 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a 20 74  mory:" then.** t
3419c 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73  he database is s
3419d 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
3419e 28 61 6e 64 20 69 73 20 74 68 75 73 20 66 6f 72  (and is thus for
3419f 67 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e 20 61  gotten as soon a
341a0 73 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74  s.** the connect
341a1 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 29 20  ion is closed.) 
341a2 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
341a3 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64   NULL then the d
341a4 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 61 20  atabase.** is a 
341a5 22 76 69 72 74 75 61 6c 22 20 64 61 74 61 62 61  "virtual" databa
341a6 73 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74  se for transient
341a7 20 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20 69 73   use only and is
341a8 20 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a 20 73   deleted as.** s
341a9 6f 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e 6e 65  oon as the conne
341aa 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e  ction is closed.
341ab 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c  .**.** A virtual
341ac 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62 65   database can be
341ad 20 65 69 74 68 65 72 20 61 20 64 69 73 6b 20 66   either a disk f
341ae 69 6c 65 20 28 74 68 61 74 20 69 73 20 61 75 74  ile (that is aut
341af 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 64 65  omatically.** de
341b0 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 66  leted when the f
341b1 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 29 20 6f  ile is closed) o
341b2 72 20 69 74 20 61 6e 20 62 65 20 68 65 6c 64 20  r it an be held 
341b3 65 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65 6d 6f  entirely in memo
341b4 72 79 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ry..** The sqlit
341b5 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 29  e3TempInMemory()
341b6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
341b7 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
341b8 68 69 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hich..*/.SQLITE_
341b9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
341ba 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
341bb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
341bc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
341bd 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 77 68  Main database wh
341be 65 6e 20 6f 70 65 6e 69 6e 67 20 61 75 78 20 6f  en opening aux o
341bf 74 68 65 72 77 69 73 65 20 30 20 2a 2f 0a 20 20  therwise 0 */.  
341c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
341c1 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  ename,    /* Nam
341c2 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f  e of the file co
341c3 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72  ntaining the BTr
341c4 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ee database */. 
341c5 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c   int omitJournal
341c6 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 66  ,          /* if
341c7 20 54 52 55 45 20 74 68 65 6e 20 64 6f 20 6e 6f   TRUE then do no
341c8 74 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20 66  t journal this f
341c9 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61  ile */.  int nCa
341ca 63 68 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  che,            
341cb 20 20 20 2f 2a 20 48 6f 77 20 6d 61 6e 79 20 70     /* How many p
341cc 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65  ages in the page
341cd 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20   cache */.  int 
341ce 76 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20  vfsFlags,       
341cf 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70        /* Flags p
341d0 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
341d1 20 76 66 73 4f 70 65 6e 20 2a 2f 0a 20 20 42 74   vfsOpen */.  Bt
341d2 72 65 65 20 2a 2a 70 70 42 74 72 65 65 20 20 20  ree **ppBtree   
341d3 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
341d4 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20  er to new Btree 
341d5 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68  object written h
341d6 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
341d7 62 74 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 69  btFlags = 0;.  i
341d8 6e 74 20 72 63 3b 0a 20 20 0a 20 20 61 73 73 65  nt rc;.  .  asse
341d9 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
341da 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
341db 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
341dc 70 42 74 72 65 65 20 21 3d 20 30 29 3b 0a 20 20  pBtree != 0);.  
341dd 69 66 28 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 20  if( omitJournal 
341de 29 7b 0a 20 20 20 20 62 74 46 6c 61 67 73 20 7c  ){.    btFlags |
341df 3d 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55  = BTREE_OMIT_JOU
341e0 52 4e 41 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  RNAL;.  }.  if( 
341e1 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
341e2 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b  TE_NoReadlock ){
341e3 0a 20 20 20 20 62 74 46 6c 61 67 73 20 7c 3d 20  .    btFlags |= 
341e4 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43  BTREE_NO_READLOC
341e5 4b 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  K;.  }.#ifndef S
341e6 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
341e7 59 44 42 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  YDB.  if( zFilen
341e8 61 6d 65 3d 3d 30 20 26 26 20 73 71 6c 69 74 65  ame==0 && sqlite
341e9 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 64 62  3TempInMemory(db
341ea 29 20 29 7b 0a 20 20 20 20 7a 46 69 6c 65 6e 61  ) ){.    zFilena
341eb 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a 22 3b  me = ":memory:";
341ec 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
341ed 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20 53  f( (vfsFlags & S
341ee 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
341ef 44 42 29 21 3d 30 20 26 26 20 28 7a 46 69 6c 65  DB)!=0 && (zFile
341f0 6e 61 6d 65 3d 3d 30 20 7c 7c 20 2a 7a 46 69 6c  name==0 || *zFil
341f1 65 6e 61 6d 65 3d 3d 30 29 20 29 7b 0a 20 20 20  ename==0) ){.   
341f2 20 76 66 73 46 6c 61 67 73 20 3d 20 28 76 66 73   vfsFlags = (vfs
341f3 46 6c 61 67 73 20 26 20 7e 53 51 4c 49 54 45 5f  Flags & ~SQLITE_
341f4 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 20  OPEN_MAIN_DB) | 
341f5 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
341f6 5f 44 42 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  _DB;.  }.  rc = 
341f7 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e  sqlite3BtreeOpen
341f8 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 28 73 71 6c  (zFilename, (sql
341f9 69 74 65 33 20 2a 29 64 62 2c 20 70 70 42 74 72  ite3 *)db, ppBtr
341fa 65 65 2c 20 62 74 46 6c 61 67 73 2c 20 76 66 73  ee, btFlags, vfs
341fb 46 6c 61 67 73 29 3b 0a 0a 20 20 2f 2a 20 49 66  Flags);..  /* If
341fc 20 74 68 65 20 42 2d 54 72 65 65 20 77 61 73 20   the B-Tree was 
341fd 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65  successfully ope
341fe 6e 65 64 2c 20 73 65 74 20 74 68 65 20 70 61 67  ned, set the pag
341ff 65 72 2d 63 61 63 68 65 20 73 69 7a 65 20 74 6f  er-cache size to
34200 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c   the.  ** defaul
34201 74 20 76 61 6c 75 65 2e 20 45 78 63 65 70 74 2c  t value. Except,
34202 20 69 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   if the call to 
34203 42 74 72 65 65 4f 70 65 6e 28 29 20 72 65 74 75  BtreeOpen() retu
34204 72 6e 65 64 20 61 20 68 61 6e 64 6c 65 0a 20 20  rned a handle.  
34205 2a 2a 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 65 78  ** open on an ex
34206 69 73 74 69 6e 67 20 73 68 61 72 65 64 20 70 61  isting shared pa
34207 67 65 72 2d 63 61 63 68 65 2c 20 64 6f 20 6e 6f  ger-cache, do no
34208 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 67  t change the pag
34209 65 72 2d 63 61 63 68 65 20 0a 20 20 2a 2a 20 73  er-cache .  ** s
3420a 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ize..  */.  if( 
3420b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3420c 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65   0==sqlite3Btree
3420d 53 63 68 65 6d 61 28 2a 70 70 42 74 72 65 65 2c  Schema(*ppBtree,
3420e 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 73 71   0, 0) ){.    sq
3420f 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
34210 68 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65 2c  heSize(*ppBtree,
34211 20 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20   nCache);.  }.  
34212 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
34213 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 38  .** Return UTF-8
34214 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68   encoded English
34215 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e   language explan
34216 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ation of the mos
34217 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f  t recent.** erro
34218 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
34219 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
3421a 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69  ite3_errmsg(sqli
3421b 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e 73  te3 *db){.  cons
3421c 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28  t char *z;.  if(
3421d 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75   !db ){.    retu
3421e 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  rn sqlite3ErrStr
3421f 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  (SQLITE_NOMEM);.
34220 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74    }.  if( !sqlit
34221 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
34222 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20  kOrOk(db) ){.   
34223 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
34224 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4d 49 53  rrStr(SQLITE_MIS
34225 55 53 45 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  USE);.  }.  sqli
34226 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34227 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
34228 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
34229 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71  ed ){.    z = sq
3422a 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49  lite3ErrStr(SQLI
3422b 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 65 6c  TE_NOMEM);.  }el
3422c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 28 63 68 61  se{.    z = (cha
3422d 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
3422e 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b  _text(db->pErr);
3422f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 64 62  .    assert( !db
34230 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
34231 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
34232 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  {.      z = sqli
34233 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72  te3ErrStr(db->er
34234 72 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  rCode);.    }.  
34235 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
34236 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
34237 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  x);.  return z;.
34238 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
34239 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
3423a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36  ** Return UTF-16
3423b 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68   encoded English
3423c 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e   language explan
3423d 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ation of the mos
3423e 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f  t recent.** erro
3423f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
34240 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
34241 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71  ite3_errmsg16(sq
34242 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74  lite3 *db){.  st
34243 61 74 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6f  atic const u16 o
34244 75 74 4f 66 4d 65 6d 5b 5d 20 3d 20 7b 0a 20 20  utOfMem[] = {.  
34245 20 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c    'o', 'u', 't',
34246 20 27 20 27 2c 20 27 6f 27 2c 20 27 66 27 2c 20   ' ', 'o', 'f', 
34247 27 20 27 2c 20 27 6d 27 2c 20 27 65 27 2c 20 27  ' ', 'm', 'e', '
34248 6d 27 2c 20 27 6f 27 2c 20 27 72 27 2c 20 27 79  m', 'o', 'r', 'y
34249 27 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74  ', 0.  };.  stat
3424a 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6d 69 73  ic const u16 mis
3424b 75 73 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6c  use[] = {.    'l
3424c 27 2c 20 27 69 27 2c 20 27 62 27 2c 20 27 72 27  ', 'i', 'b', 'r'
3424d 2c 20 27 61 27 2c 20 27 72 27 2c 20 27 79 27 2c  , 'a', 'r', 'y',
3424e 20 27 20 27 2c 20 0a 20 20 20 20 27 72 27 2c 20   ' ', .    'r', 
3424f 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27  'o', 'u', 't', '
34250 69 27 2c 20 27 6e 27 2c 20 27 65 27 2c 20 27 20  i', 'n', 'e', ' 
34251 27 2c 20 0a 20 20 20 20 27 63 27 2c 20 27 61 27  ', .    'c', 'a'
34252 2c 20 27 6c 27 2c 20 27 6c 27 2c 20 27 65 27 2c  , 'l', 'l', 'e',
34253 20 27 64 27 2c 20 27 20 27 2c 20 0a 20 20 20 20   'd', ' ', .    
34254 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27  'o', 'u', 't', '
34255 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27 66   ', .    'o', 'f
34256 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 73 27  ', ' ', .    's'
34257 2c 20 27 65 27 2c 20 27 71 27 2c 20 27 75 27 2c  , 'e', 'q', 'u',
34258 20 27 65 27 2c 20 27 6e 27 2c 20 27 63 27 2c 20   'e', 'n', 'c', 
34259 27 65 27 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63  'e', 0.  };..  c
3425a 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20  onst void *z;.  
3425b 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72  if( !db ){.    r
3425c 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6f 75  eturn (void *)ou
3425d 74 4f 66 4d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66  tOfMem;.  }.  if
3425e 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
3425f 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
34260 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
34261 28 76 6f 69 64 20 2a 29 6d 69 73 75 73 65 3b 0a  (void *)misuse;.
34262 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
34263 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
34264 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  tex);.  if( db->
34265 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
34266 20 20 20 20 7a 20 3d 20 28 76 6f 69 64 20 2a 29      z = (void *)
34267 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 65 6c 73  outOfMem;.  }els
34268 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
34269 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
3426a 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 69  db->pErr);.    i
3426b 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
3426c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
3426d 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31  Str(db->pErr, -1
3426e 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
3426f 64 62 2d 3e 65 72 72 43 6f 64 65 29 2c 0a 20 20  db->errCode),.  
34270 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
34271 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
34272 54 49 43 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20  TIC);.      z = 
34273 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
34274 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a  xt16(db->pErr);.
34275 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 41 20 6d      }.    /* A m
34276 61 6c 6c 6f 63 28 29 20 6d 61 79 20 68 61 76 65  alloc() may have
34277 20 66 61 69 6c 65 64 20 77 69 74 68 69 6e 20 74   failed within t
34278 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
34279 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
3427a 29 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e 20  ).    ** above. 
3427b 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
3427c 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 62  ase, then the db
3427d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  ->mallocFailed f
3427e 6c 61 67 20 6e 65 65 64 73 20 74 6f 0a 20 20 20  lag needs to.   
3427f 20 2a 2a 20 62 65 20 63 6c 65 61 72 65 64 20 62   ** be cleared b
34280 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
34281 20 44 6f 20 74 68 69 73 20 64 69 72 65 63 74 6c   Do this directl
34282 79 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 76 69  y, instead of vi
34283 61 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  a.    ** sqlite3
34284 41 70 69 45 78 69 74 28 29 2c 20 74 6f 20 61 76  ApiExit(), to av
34285 6f 69 64 20 73 65 74 74 69 6e 67 20 74 68 65 20  oid setting the 
34286 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
34287 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 20  error message.. 
34288 20 20 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61     */.    db->ma
34289 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
3428a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
3428b 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
3428c 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  tex);.  return z
3428d 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
3428e 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
3428f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
34290 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
34291 20 65 72 72 6f 72 20 63 6f 64 65 20 67 65 6e 65   error code gene
34292 72 61 74 65 64 20 62 79 20 61 6e 20 53 51 4c 69  rated by an SQLi
34293 74 65 20 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e  te routine. If N
34294 55 4c 4c 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ULL is.** passed
34295 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
34296 6e 2c 20 77 65 20 61 73 73 75 6d 65 20 61 20 6d  n, we assume a m
34297 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64  alloc() failed d
34298 75 72 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70  uring sqlite3_op
34299 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  en()..*/.SQLITE_
3429a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3429b 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20  errcode(sqlite3 
3429c 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26  *db){.  if( db &
3429d 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  & !sqlite3Safety
3429e 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62  CheckSickOrOk(db
3429f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
342a0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
342a1 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20   }.  if( !db || 
342a2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
342a3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
342a4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
342a5 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72  .  return db->er
342a6 72 43 6f 64 65 20 26 20 64 62 2d 3e 65 72 72 4d  rCode & db->errM
342a7 61 73 6b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  ask;.}.SQLITE_AP
342a8 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
342a9 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73  tended_errcode(s
342aa 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
342ab 66 28 20 64 62 20 26 26 20 21 73 71 6c 69 74 65  f( db && !sqlite
342ac 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
342ad 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
342ae 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
342af 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
342b0 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  !db || db->mallo
342b1 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
342b2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
342b3 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  EM;.  }.  return
342b4 20 64 62 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d 0a   db->errCode;.}.
342b5 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
342b6 6e 65 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  new collating fu
342b7 6e 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61 62  nction for datab
342b8 61 73 65 20 22 64 62 22 2e 20 20 54 68 65 20 6e  ase "db".  The n
342b9 61 6d 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20  ame is zName.** 
342ba 61 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  and the encoding
342bb 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74   is enc..*/.stat
342bc 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f 6c  ic int createCol
342bd 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  lation(.  sqlite
342be 33 2a 20 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3* db,.  const c
342bf 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 75  har *zName, .  u
342c0 38 20 65 6e 63 2c 0a 20 20 75 38 20 63 6f 6c 6c  8 enc,.  u8 coll
342c1 54 79 70 65 2c 0a 20 20 76 6f 69 64 2a 20 70 43  Type,.  void* pC
342c2 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  tx,.  int(*xComp
342c3 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
342c4 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
342c5 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76  onst void*),.  v
342c6 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  oid(*xDel)(void*
342c7 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  ).){.  CollSeq *
342c8 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e 63  pColl;.  int enc
342c9 32 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  2;.  int nName =
342ca 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
342cb 28 7a 4e 61 6d 65 29 3b 0a 20 20 0a 20 20 61 73  (zName);.  .  as
342cc 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
342cd 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
342ce 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ex) );..  /* If 
342cf 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20  SQLITE_UTF16 is 
342d0 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65  specified as the
342d1 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20   encoding type, 
342d2 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20  transform this. 
342d3 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51   ** to one of SQ
342d4 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20  LITE_UTF16LE or 
342d5 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75  SQLITE_UTF16BE u
342d6 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51  sing the.  ** SQ
342d7 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
342d8 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55   macro. SQLITE_U
342d9 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64  TF16 is not used
342da 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a   internally..  *
342db 2f 0a 20 20 65 6e 63 32 20 3d 20 65 6e 63 3b 0a  /.  enc2 = enc;.
342dc 20 20 74 65 73 74 63 61 73 65 28 20 65 6e 63 32    testcase( enc2
342dd 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29  ==SQLITE_UTF16 )
342de 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 65 6e  ;.  testcase( en
342df 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  c2==SQLITE_UTF16
342e0 5f 41 4c 49 47 4e 45 44 20 29 3b 0a 20 20 69 66  _ALIGNED );.  if
342e1 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  ( enc2==SQLITE_U
342e2 54 46 31 36 20 7c 7c 20 65 6e 63 32 3d 3d 53 51  TF16 || enc2==SQ
342e3 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
342e4 45 44 20 29 7b 0a 20 20 20 20 65 6e 63 32 20 3d  ED ){.    enc2 =
342e5 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
342e6 49 56 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65  IVE;.  }.  if( e
342e7 6e 63 32 3c 53 51 4c 49 54 45 5f 55 54 46 38 20  nc2<SQLITE_UTF8 
342e8 7c 7c 20 65 6e 63 32 3e 53 51 4c 49 54 45 5f 55  || enc2>SQLITE_U
342e9 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 72 65  TF16BE ){.    re
342ea 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
342eb 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  SE;.  }..  /* Ch
342ec 65 63 6b 20 69 66 20 74 68 69 73 20 63 61 6c 6c  eck if this call
342ed 20 69 73 20 72 65 6d 6f 76 69 6e 67 20 6f 72 20   is removing or 
342ee 72 65 70 6c 61 63 69 6e 67 20 61 6e 20 65 78 69  replacing an exi
342ef 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20  sting collation 
342f0 0a 20 20 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20  .  ** sequence. 
342f1 49 66 20 73 6f 2c 20 61 6e 64 20 74 68 65 72 65  If so, and there
342f2 20 61 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c   are active VMs,
342f3 20 72 65 74 75 72 6e 20 62 75 73 79 2e 20 49 66   return busy. If
342f4 20 74 68 65 72 65 0a 20 20 2a 2a 20 61 72 65 20   there.  ** are 
342f5 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c 20 69  no active VMs, i
342f6 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
342f7 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  e-compiled state
342f8 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 43  ments..  */.  pC
342f9 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
342fa 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38  dCollSeq(db, (u8
342fb 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 30 29  )enc2, zName, 0)
342fc 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 26 26  ;.  if( pColl &&
342fd 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 7b 0a   pColl->xCmp ){.
342fe 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
342ff 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20  veVdbeCnt ){.   
34300 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
34301 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c  db, SQLITE_BUSY,
34302 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c   .        "unabl
34303 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69  e to delete/modi
34304 66 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  fy collation seq
34305 75 65 6e 63 65 20 64 75 65 20 74 6f 20 61 63 74  uence due to act
34306 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22 29  ive statements")
34307 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
34308 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
34309 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
3430a 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
3430b 6d 65 6e 74 73 28 64 62 29 3b 0a 0a 20 20 20 20  ments(db);..    
3430c 2f 2a 20 49 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  /* If collation 
3430d 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 77  sequence pColl w
3430e 61 73 20 63 72 65 61 74 65 64 20 64 69 72 65 63  as created direc
3430f 74 6c 79 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  tly by a call to
34310 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
34311 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
34312 2c 20 61 6e 64 20 6e 6f 74 20 67 65 6e 65 72 61  , and not genera
34313 74 65 64 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c  ted by synthColl
34314 53 65 71 28 29 2c 0a 20 20 20 20 2a 2a 20 74 68  Seq(),.    ** th
34315 65 6e 20 61 6e 79 20 63 6f 70 69 65 73 20 6d 61  en any copies ma
34316 64 65 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53  de by synthCollS
34317 65 71 28 29 20 6e 65 65 64 20 74 6f 20 62 65 20  eq() need to be 
34318 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 20 20 20  invalidated..   
34319 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f 6c 6c 61 74   ** Also, collat
3431a 69 6f 6e 20 64 65 73 74 72 75 63 74 6f 72 20 2d  ion destructor -
3431b 20 43 6f 6c 6c 53 65 71 2e 78 44 65 6c 28 29 20   CollSeq.xDel() 
3431c 2d 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6e  - function may n
3431d 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65  eed.    ** to be
3431e 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2f 20   called..    */ 
3431f 0a 20 20 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d  .    if( (pColl-
34320 3e 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55  >enc & ~SQLITE_U
34321 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3d 3d 65  TF16_ALIGNED)==e
34322 6e 63 32 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c  nc2 ){.      Col
34323 6c 53 65 71 20 2a 61 43 6f 6c 6c 20 3d 20 73 71  lSeq *aColl = sq
34324 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64  lite3HashFind(&d
34325 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61  b->aCollSeq, zNa
34326 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
34327 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66    int j;.      f
34328 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b  or(j=0; j<3; j++
34329 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  ){.        CollS
3432a 65 71 20 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a  eq *p = &aColl[j
3432b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ];.        if( p
3432c 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e  ->enc==pColl->en
3432d 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  c ){.          i
3432e 66 28 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20  f( p->xDel ){.  
3432f 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78 44 65            p->xDe
34330 6c 28 70 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20  l(p->pUser);.   
34331 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
34332 20 20 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a     p->xCmp = 0;.
34333 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34334 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  }.    }.  }..  p
34335 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
34336 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75  ndCollSeq(db, (u
34337 38 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 31  8)enc2, zName, 1
34338 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  );.  if( pColl )
34339 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d  {.    pColl->xCm
3433a 70 20 3d 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20  p = xCompare;.  
3433b 20 20 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d    pColl->pUser =
3433c 20 70 43 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c   pCtx;.    pColl
3433d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20  ->xDel = xDel;. 
3433e 20 20 20 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20     pColl->enc = 
3433f 28 75 38 29 28 65 6e 63 32 20 7c 20 28 65 6e 63  (u8)(enc2 | (enc
34340 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f   & SQLITE_UTF16_
34341 41 4c 49 47 4e 45 44 29 29 3b 0a 20 20 20 20 70  ALIGNED));.    p
34342 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 63 6f 6c  Coll->type = col
34343 6c 54 79 70 65 3b 0a 20 20 7d 0a 20 20 73 71 6c  lType;.  }.  sql
34344 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
34345 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72  LITE_OK, 0);.  r
34346 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
34347 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
34348 61 72 72 61 79 20 64 65 66 69 6e 65 73 20 68 61  array defines ha
34349 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 73 20  rd upper bounds 
3434a 6f 6e 20 6c 69 6d 69 74 20 76 61 6c 75 65 73 2e  on limit values.
3434b 20 20 54 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c    The.** initial
3434c 69 7a 65 72 20 6d 75 73 74 20 62 65 20 6b 65 70  izer must be kep
3434d 74 20 69 6e 20 73 79 6e 63 20 77 69 74 68 20 74  t in sync with t
3434e 68 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  he SQLITE_LIMIT_
3434f 2a 0a 2a 2a 20 23 64 65 66 69 6e 65 73 20 69 6e  *.** #defines in
34350 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 73   sqlite3.h..*/.s
34351 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
34352 61 48 61 72 64 4c 69 6d 69 74 5b 5d 20 3d 20 7b  aHardLimit[] = {
34353 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  .  SQLITE_MAX_LE
34354 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  NGTH,.  SQLITE_M
34355 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2c 0a 20  AX_SQL_LENGTH,. 
34356 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
34357 4d 4e 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  MN,.  SQLITE_MAX
34358 5f 45 58 50 52 5f 44 45 50 54 48 2c 0a 20 20 53  _EXPR_DEPTH,.  S
34359 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55  QLITE_MAX_COMPOU
3435a 4e 44 5f 53 45 4c 45 43 54 2c 0a 20 20 53 51 4c  ND_SELECT,.  SQL
3435b 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 2c  ITE_MAX_VDBE_OP,
3435c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  .  SQLITE_MAX_FU
3435d 4e 43 54 49 4f 4e 5f 41 52 47 2c 0a 20 20 53 51  NCTION_ARG,.  SQ
3435e 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
3435f 44 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  D,.  SQLITE_MAX_
34360 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
34361 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  GTH,.  SQLITE_MA
34362 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  X_VARIABLE_NUMBE
34363 52 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  R,.  SQLITE_MAX_
34364 54 52 49 47 47 45 52 5f 44 45 50 54 48 2c 0a 7d  TRIGGER_DEPTH,.}
34365 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  ;../*.** Make su
34366 72 65 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69  re the hard limi
34367 74 73 20 61 72 65 20 73 65 74 20 74 6f 20 72 65  ts are set to re
34368 61 73 6f 6e 61 62 6c 65 20 76 61 6c 75 65 73 0a  asonable values.
34369 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  */.#if SQLITE_MA
3436a 58 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65  X_LENGTH<100.# e
3436b 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
3436c 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61  LENGTH must be a
3436d 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64  t least 100.#end
3436e 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
3436f 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 31 30 30  X_SQL_LENGTH<100
34370 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
34371 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d  MAX_SQL_LENGTH m
34372 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20  ust be at least 
34373 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  100.#endif.#if S
34374 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
34375 4e 47 54 48 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  NGTH>SQLITE_MAX_
34376 4c 45 4e 47 54 48 0a 23 20 65 72 72 6f 72 20 53  LENGTH.# error S
34377 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
34378 4e 47 54 48 20 6d 75 73 74 20 6e 6f 74 20 62 65  NGTH must not be
34379 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 53 51   greater than SQ
3437a 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
3437b 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
3437c 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53  E_MAX_COMPOUND_S
3437d 45 4c 45 43 54 3c 32 0a 23 20 65 72 72 6f 72 20  ELECT<2.# error 
3437e 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f  SQLITE_MAX_COMPO
3437f 55 4e 44 5f 53 45 4c 45 43 54 20 6d 75 73 74 20  UND_SELECT must 
34380 62 65 20 61 74 20 6c 65 61 73 74 20 32 0a 23 65  be at least 2.#e
34381 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
34382 4d 41 58 5f 56 44 42 45 5f 4f 50 3c 34 30 0a 23  MAX_VDBE_OP<40.#
34383 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
34384 58 5f 56 44 42 45 5f 4f 50 20 6d 75 73 74 20 62  X_VDBE_OP must b
34385 65 20 61 74 20 6c 65 61 73 74 20 34 30 0a 23 65  e at least 40.#e
34386 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
34387 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
34388 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58  <0 || SQLITE_MAX
34389 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3e 31 30  _FUNCTION_ARG>10
3438a 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  00.# error SQLIT
3438b 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41  E_MAX_FUNCTION_A
3438c 52 47 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  RG must be betwe
3438d 65 6e 20 30 20 61 6e 64 20 31 30 30 30 0a 23 65  en 0 and 1000.#e
3438e 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
3438f 4d 41 58 5f 41 54 54 41 43 48 45 44 3c 30 20 7c  MAX_ATTACHED<0 |
34390 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  | SQLITE_MAX_ATT
34391 41 43 48 45 44 3e 33 30 0a 23 20 65 72 72 6f 72  ACHED>30.# error
34392 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41   SQLITE_MAX_ATTA
34393 43 48 45 44 20 6d 75 73 74 20 62 65 20 62 65 74  CHED must be bet
34394 77 65 65 6e 20 30 20 61 6e 64 20 33 30 0a 23 65  ween 0 and 30.#e
34395 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
34396 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
34397 5f 4c 45 4e 47 54 48 3c 31 0a 23 20 65 72 72 6f  _LENGTH<1.# erro
34398 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  r SQLITE_MAX_LIK
34399 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
3439a 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
3439b 74 20 31 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  t 1.#endif.#if S
3439c 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
3439d 3e 33 32 37 36 37 0a 23 20 65 72 72 6f 72 20 53  >32767.# error S
3439e 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
3439f 20 6d 75 73 74 20 6e 6f 74 20 65 78 63 65 65 64   must not exceed
343a0 20 33 32 37 36 37 0a 23 65 6e 64 69 66 0a 23 69   32767.#endif.#i
343a1 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49  f SQLITE_MAX_TRI
343a2 47 47 45 52 5f 44 45 50 54 48 3c 31 0a 23 20 65  GGER_DEPTH<1.# e
343a3 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
343a4 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 6d 75  TRIGGER_DEPTH mu
343a5 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
343a6 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
343a7 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
343a8 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20 52 65   of a limit.  Re
343a9 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c  port the old val
343aa 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 76  ue..** If an inv
343ab 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64 65 78  alid limit index
343ac 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 72 65   is supplied, re
343ad 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61 6b 65  port -1..** Make
343ae 20 6e 6f 20 63 68 61 6e 67 65 73 20 62 75 74 20   no changes but 
343af 73 74 69 6c 6c 20 72 65 70 6f 72 74 20 74 68 65  still report the
343b0 20 6f 6c 64 20 76 61 6c 75 65 20 69 66 20 74 68   old value if th
343b1 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 69  e.** new limit i
343b2 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a  s negative..**.*
343b3 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72 20 6c 69  * A new lower li
343b4 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20 73 68 72  mit does not shr
343b5 69 6e 6b 20 65 78 69 73 74 69 6e 67 20 63 6f 6e  ink existing con
343b6 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74 20 6d  structs..** It m
343b7 65 72 65 6c 79 20 70 72 65 76 65 6e 74 73 20 6e  erely prevents n
343b8 65 77 20 63 6f 6e 73 74 72 75 63 74 73 20 74 68  ew constructs th
343b9 61 74 20 65 78 63 65 65 64 20 74 68 65 20 6c 69  at exceed the li
343ba 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72 6d  mit.** from form
343bb 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ing..*/.SQLITE_A
343bc 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
343bd 69 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  imit(sqlite3 *db
343be 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c 20 69  , int limitId, i
343bf 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a 20 20  nt newLimit){.  
343c0 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a 20 20  int oldLimit;.  
343c1 69 66 28 20 6c 69 6d 69 74 49 64 3c 30 20 7c 7c  if( limitId<0 ||
343c2 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49 54 45   limitId>=SQLITE
343c3 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  _N_LIMIT ){.    
343c4 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
343c5 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62 2d 3e   oldLimit = db->
343c6 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b  aLimit[limitId];
343c7 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e  .  if( newLimit>
343c8 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65  =0 ){.    if( ne
343c9 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69 6d 69  wLimit>aHardLimi
343ca 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a 20 20  t[limitId] ){.  
343cb 20 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d 20 61      newLimit = a
343cc 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49  HardLimit[limitI
343cd 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  d];.    }.    db
343ce 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ->aLimit[limitId
343cf 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20  ] = newLimit;.  
343d0 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64 4c 69  }.  return oldLi
343d1 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  mit;.}../*.** Th
343d2 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
343d3 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70 65 6e  the work of open
343d4 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 6f  ing a database o
343d5 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a 20 73  n behalf of.** s
343d6 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e  qlite3_open() an
343d7 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  d sqlite3_open16
343d8 28 29 2e 20 54 68 65 20 64 61 74 61 62 61 73 65  (). The database
343d9 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69 6c 65   filename "zFile
343da 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20 55 54  name"  .** is UT
343db 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a 2f 0a  F-8 encoded..*/.
343dc 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44  static int openD
343dd 61 74 61 62 61 73 65 28 0a 20 20 63 6f 6e 73 74  atabase(.  const
343de 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
343df 2c 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69  , /* Database fi
343e0 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20 65 6e 63  lename UTF-8 enc
343e1 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  oded */.  sqlite
343e2 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20  3 **ppDb,       
343e3 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 65   /* OUT: Returne
343e4 64 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  d database handl
343e5 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
343e6 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 2f 2a  flags,        /*
343e7 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66 6c 61   Operational fla
343e8 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  gs */.  const ch
343e9 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 2f  ar *zVfs       /
343ea 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 56 46  * Name of the VF
343eb 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20  S to use */.){. 
343ec 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
343ed 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 73  int rc;.  int is
343ee 54 68 72 65 61 64 73 61 66 65 3b 0a 0a 20 20 2a  Threadsafe;..  *
343ef 70 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65  ppDb = 0;.#ifnde
343f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
343f1 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71  TOINIT.  rc = sq
343f2 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
343f3 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
343f4 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66  eturn rc;.#endif
343f5 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ..  if( sqlite3G
343f6 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
343f7 65 4d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20  eMutex==0 ){.   
343f8 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20   isThreadsafe = 
343f9 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  0;.  }else if( f
343fa 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
343fb 45 4e 5f 4e 4f 4d 55 54 45 58 20 29 7b 0a 20 20  EN_NOMUTEX ){.  
343fc 20 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d    isThreadsafe =
343fd 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
343fe 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
343ff 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 29 7b  PEN_FULLMUTEX ){
34400 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66  .    isThreadsaf
34401 65 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  e = 1;.  }else{.
34402 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65      isThreadsafe
34403 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
34404 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
34405 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61  x;.  }.  if( fla
34406 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
34407 5f 50 52 49 56 41 54 45 43 41 43 48 45 20 29 7b  _PRIVATECACHE ){
34408 0a 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 53  .    flags &= ~S
34409 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45  QLITE_OPEN_SHARE
3440a 44 43 41 43 48 45 3b 0a 20 20 7d 65 6c 73 65 20  DCACHE;.  }else 
3440b 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
3440c 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61  lConfig.sharedCa
3440d 63 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20  cheEnabled ){.  
3440e 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54    flags |= SQLIT
3440f 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
34410 48 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  HE;.  }..  /* Re
34411 6d 6f 76 65 20 68 61 72 6d 66 75 6c 20 62 69 74  move harmful bit
34412 73 20 66 72 6f 6d 20 74 68 65 20 66 6c 61 67 73  s from the flags
34413 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 0a   parameter.  **.
34414 20 20 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f    ** The SQLITE_
34415 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 61 6e 64  OPEN_NOMUTEX and
34416 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
34417 4c 4d 55 54 45 58 20 66 6c 61 67 73 20 77 65 72  LMUTEX flags wer
34418 65 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69 74  e.  ** dealt wit
34419 68 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  h in the previou
3441a 73 20 63 6f 64 65 20 62 6c 6f 63 6b 2e 20 20 42  s code block.  B
3441b 65 73 69 64 65 73 20 74 68 65 73 65 2c 20 74 68  esides these, th
3441c 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 76 61 6c 69  e only.  ** vali
3441d 64 20 69 6e 70 75 74 20 66 6c 61 67 73 20 66 6f  d input flags fo
3441e 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  r sqlite3_open_v
3441f 32 28 29 20 61 72 65 20 53 51 4c 49 54 45 5f 4f  2() are SQLITE_O
34420 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 2c 0a 20 20  PEN_READONLY,.  
34421 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ** SQLITE_OPEN_R
34422 45 41 44 57 52 49 54 45 2c 20 61 6e 64 20 53 51  EADWRITE, and SQ
34423 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
34424 2e 20 20 53 69 6c 65 6e 74 6c 79 20 6d 61 73 6b  .  Silently mask
34425 0a 20 20 2a 2a 20 6f 66 66 20 61 6c 6c 20 6f 74  .  ** off all ot
34426 68 65 72 20 66 6c 61 67 73 2e 0a 20 20 2a 2f 0a  her flags..  */.
34427 20 20 66 6c 61 67 73 20 26 3d 20 20 7e 28 20 53    flags &=  ~( S
34428 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
34429 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20  EONCLOSE |.     
3442a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
3442b 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
3442c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
3442d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
3442e 4e 5f 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20  N_DB |.         
3442f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
34430 4e 5f 54 45 4d 50 5f 44 42 20 7c 20 0a 20 20 20  N_TEMP_DB | .   
34431 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
34432 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
34433 54 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20  T_DB | .        
34434 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
34435 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20  EN_MAIN_JOURNAL 
34436 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  | .             
34437 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45    SQLITE_OPEN_TE
34438 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20  MP_JOURNAL | .  
34439 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
3443a 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52  ITE_OPEN_SUBJOUR
3443b 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20  NAL | .         
3443c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
3443d 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
3443e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   |.             
3443f 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f    SQLITE_OPEN_NO
34440 4d 55 54 45 58 20 7c 0a 20 20 20 20 20 20 20 20  MUTEX |.        
34441 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
34442 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 0a 20 20 20  EN_FULLMUTEX.   
34443 20 20 20 20 20 20 20 20 20 20 29 3b 0a 0a 20 20            );..  
34444 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
34445 73 71 6c 69 74 65 20 64 61 74 61 20 73 74 72 75  sqlite data stru
34446 63 74 75 72 65 20 2a 2f 0a 20 20 64 62 20 3d 20  cture */.  db = 
34447 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
34448 6f 28 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  o( sizeof(sqlite
34449 33 29 20 29 3b 0a 20 20 69 66 28 20 64 62 3d 3d  3) );.  if( db==
3444a 30 20 29 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f  0 ) goto opendb_
3444b 6f 75 74 3b 0a 20 20 69 66 28 20 69 73 54 68 72  out;.  if( isThr
3444c 65 61 64 73 61 66 65 20 29 7b 0a 20 20 20 20 64  eadsafe ){.    d
3444d 62 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  b->mutex = sqlit
3444e 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
3444f 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
34450 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 64 62  IVE);.    if( db
34451 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20  ->mutex==0 ){.  
34452 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
34453 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62 20 3d  (db);.      db =
34454 20 30 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f   0;.      goto o
34455 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 20 20 7d  pendb_out;.    }
34456 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
34457 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
34458 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72  utex);.  db->err
34459 4d 61 73 6b 20 3d 20 30 78 66 66 3b 0a 20 20 64  Mask = 0xff;.  d
3445a 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a 20 20 64 62  b->nDb = 2;.  db
3445b 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
3445c 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 64  _MAGIC_BUSY;.  d
3445d 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62  b->aDb = db->aDb
3445e 53 74 61 74 69 63 3b 0a 0a 20 20 61 73 73 65 72  Static;..  asser
3445f 74 28 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c  t( sizeof(db->aL
34460 69 6d 69 74 29 3d 3d 73 69 7a 65 6f 66 28 61 48  imit)==sizeof(aH
34461 61 72 64 4c 69 6d 69 74 29 20 29 3b 0a 20 20 6d  ardLimit) );.  m
34462 65 6d 63 70 79 28 64 62 2d 3e 61 4c 69 6d 69 74  emcpy(db->aLimit
34463 2c 20 61 48 61 72 64 4c 69 6d 69 74 2c 20 73 69  , aHardLimit, si
34464 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29  zeof(db->aLimit)
34465 29 3b 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d  );.  db->autoCom
34466 6d 69 74 20 3d 20 31 3b 0a 20 20 64 62 2d 3e 6e  mit = 1;.  db->n
34467 65 78 74 41 75 74 6f 76 61 63 20 3d 20 2d 31 3b  extAutovac = -1;
34468 0a 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73  .  db->nextPages
34469 69 7a 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 66  ize = 0;.  db->f
3446a 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53  lags |= SQLITE_S
3446b 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 0a 23 69 66  hortColNames.#if
3446c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
3446d 46 49 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a 20 20  FILE_FORMAT<4.  
3446e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
3446f 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69   SQLITE_LegacyFi
34470 6c 65 46 6d 74 0a 23 65 6e 64 69 66 0a 23 69 66  leFmt.#endif.#if
34471 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
34472 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  E_LOAD_EXTENSION
34473 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34474 20 20 7c 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45    | SQLITE_LoadE
34475 78 74 65 6e 73 69 6f 6e 0a 23 65 6e 64 69 66 0a  xtension.#endif.
34476 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  #if SQLITE_DEFAU
34477 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54 52 49  LT_RECURSIVE_TRI
34478 47 47 45 52 53 0a 20 20 20 20 20 20 20 20 20 20  GGERS.          
34479 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
3447a 52 65 63 54 72 69 67 67 65 72 73 0a 23 65 6e 64  RecTriggers.#end
3447b 69 66 0a 20 20 20 20 20 20 3b 0a 20 20 73 71 6c  if.      ;.  sql
3447c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 64 62  ite3HashInit(&db
3447d 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
3447e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3447f 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
34480 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
34481 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23  &db->aModule);.#
34482 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66  endif..  db->pVf
34483 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  s = sqlite3_vfs_
34484 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69 66  find(zVfs);.  if
34485 28 20 21 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20  ( !db->pVfs ){. 
34486 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
34487 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65  RROR;.    sqlite
34488 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
34489 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73 22  no such vfs: %s"
3448a 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 67 6f 74  , zVfs);.    got
3448b 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
3448c 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  }..  /* Add the 
3448d 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
3448e 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e 41 52  n sequence BINAR
3448f 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b 73 20  Y. BINARY works 
34490 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38 0a 20  for both UTF-8. 
34491 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36 2c 20   ** and UTF-16, 
34492 73 6f 20 61 64 64 20 61 20 76 65 72 73 69 6f 6e  so add a version
34493 20 66 6f 72 20 65 61 63 68 20 74 6f 20 61 76 6f   for each to avo
34494 69 64 20 61 6e 79 20 75 6e 6e 65 63 65 73 73 61  id any unnecessa
34495 72 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69  ry.  ** conversi
34496 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72  ons. The only er
34497 72 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 63 63  ror that can occ
34498 75 72 20 68 65 72 65 20 69 73 20 61 20 6d 61 6c  ur here is a mal
34499 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 0a 20  loc() failure.. 
3449a 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c   */.  createColl
3449b 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
3449c 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  Y", SQLITE_UTF8,
3449d 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e   SQLITE_COLL_BIN
3449e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  ARY, 0,.        
3449f 20 20 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c            binCol
344a0 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65  lFunc, 0);.  cre
344a1 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
344a2 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54   "BINARY", SQLIT
344a3 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54  E_UTF16BE, SQLIT
344a4 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30  E_COLL_BINARY, 0
344a5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
344a6 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c      binCollFunc,
344a7 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c   0);.  createCol
344a8 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
344a9 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  RY", SQLITE_UTF1
344aa 36 4c 45 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  6LE, SQLITE_COLL
344ab 5f 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20  _BINARY, 0,.    
344ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69                bi
344ad 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20  nCollFunc, 0);. 
344ae 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
344af 28 64 62 2c 20 22 52 54 52 49 4d 22 2c 20 53 51  (db, "RTRIM", SQ
344b0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
344b1 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 28 76 6f  E_COLL_USER, (vo
344b2 69 64 2a 29 31 2c 0a 20 20 20 20 20 20 20 20 20  id*)1,.         
344b3 20 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c           binColl
344b4 46 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28 20  Func, 0);.  if( 
344b5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
344b6 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65   ){.    goto ope
344b7 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64  ndb_out;.  }.  d
344b8 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73  b->pDfltColl = s
344b9 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
344ba 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46  q(db, SQLITE_UTF
344bb 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 30 29 3b  8, "BINARY", 0);
344bc 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70  .  assert( db->p
344bd 44 66 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a  DfltColl!=0 );..
344be 20 20 2f 2a 20 41 6c 73 6f 20 61 64 64 20 61 20    /* Also add a 
344bf 55 54 46 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e  UTF-8 case-insen
344c0 73 69 74 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e  sitive collation
344c1 20 73 65 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20   sequence. */.  
344c2 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
344c3 64 62 2c 20 22 4e 4f 43 41 53 45 22 2c 20 53 51  db, "NOCASE", SQ
344c4 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
344c5 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 2c 20 30  E_COLL_NOCASE, 0
344c6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
344c7 20 20 20 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74      nocaseCollat
344c8 69 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20  ingFunc, 0);..  
344c9 2f 2a 20 4f 70 65 6e 20 74 68 65 20 62 61 63 6b  /* Open the back
344ca 65 6e 64 20 64 61 74 61 62 61 73 65 20 64 72 69  end database dri
344cb 76 65 72 20 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65  ver */.  db->ope
344cc 6e 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a  nFlags = flags;.
344cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
344ce 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a  reeFactory(db, z
344cf 46 69 6c 65 6e 61 6d 65 2c 20 30 2c 20 53 51 4c  Filename, 0, SQL
344d0 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
344d1 45 5f 53 49 5a 45 2c 20 0a 20 20 20 20 20 20 20  E_SIZE, .       
344d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
344d3 20 20 20 20 66 6c 61 67 73 20 7c 20 53 51 4c 49      flags | SQLI
344d4 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c  TE_OPEN_MAIN_DB,
344d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
344d6 20 20 20 20 20 20 20 20 20 20 20 20 26 64 62 2d              &db-
344d7 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20  >aDb[0].pBt);.  
344d8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
344d9 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d  K ){.    if( rc=
344da 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
344db 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  MEM ){.      rc 
344dc 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
344dd 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
344de 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30  3Error(db, rc, 0
344df 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  );.    goto open
344e0 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62  db_out;.  }.  db
344e1 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61  ->aDb[0].pSchema
344e2 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
344e3 47 65 74 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b  Get(db, db->aDb[
344e4 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62 2d 3e 61  0].pBt);.  db->a
344e5 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 3d 20  Db[1].pSchema = 
344e6 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
344e7 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20 2f 2a 20  (db, 0);...  /* 
344e8 54 68 65 20 64 65 66 61 75 6c 74 20 73 61 66 65  The default safe
344e9 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20 74 68 65  ty_level for the
344ea 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69   main database i
344eb 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72 20 74 68  s 'full'; for th
344ec 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64 61 74 61  e temp.  ** data
344ed 62 61 73 65 20 69 74 20 69 73 20 27 4e 4f 4e 45  base it is 'NONE
344ee 27 2e 20 54 68 69 73 20 6d 61 74 63 68 65 73 20  '. This matches 
344ef 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20  the pager layer 
344f0 64 65 66 61 75 6c 74 73 2e 20 20 0a 20 20 2a 2f  defaults.  .  */
344f1 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 7a 4e  .  db->aDb[0].zN
344f2 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b 0a 20 20  ame = "main";.  
344f3 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61 66 65 74  db->aDb[0].safet
344f4 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 20 20 64  y_level = 3;.  d
344f5 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20  b->aDb[1].zName 
344f6 3d 20 22 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e  = "temp";.  db->
344f7 61 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65  aDb[1].safety_le
344f8 76 65 6c 20 3d 20 31 3b 0a 0a 20 20 64 62 2d 3e  vel = 1;..  db->
344f9 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
344fa 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 69 66 28  AGIC_OPEN;.  if(
344fb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
344fc 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70  d ){.    goto op
344fd 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  endb_out;.  }.. 
344fe 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c   /* Register all
344ff 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
34500 6f 6e 73 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20  ons, but do not 
34501 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20  attempt to read 
34502 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
34503 65 20 73 63 68 65 6d 61 20 79 65 74 2e 20 54 68  e schema yet. Th
34504 69 73 20 69 73 20 64 65 6c 61 79 65 64 20 75 6e  is is delayed un
34505 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 74 69  til the first ti
34506 6d 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  me the database.
34507 20 20 2a 2a 20 69 73 20 61 63 63 65 73 73 65 64    ** is accessed
34508 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
34509 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
3450a 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  _OK, 0);.  sqlit
3450b 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69  e3RegisterBuilti
3450c 6e 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a  nFunctions(db);.
3450d 0a 20 20 2f 2a 20 4c 6f 61 64 20 61 75 74 6f 6d  .  /* Load autom
3450e 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20  atic extensions 
3450f 2d 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61  - extensions tha
34510 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 67 69  t have been regi
34511 73 74 65 72 65 64 0a 20 20 2a 2a 20 75 73 69 6e  stered.  ** usin
34512 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 75  g the sqlite3_au
34513 74 6f 6d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f  tomatic_extensio
34514 6e 28 29 20 41 50 49 2e 0a 20 20 2a 2f 0a 20 20  n() API..  */.  
34515 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45  sqlite3AutoLoadE
34516 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 20  xtensions(db);. 
34517 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 72   rc = sqlite3_er
34518 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69 66 28  rcode(db);.  if(
34519 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3451a 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  {.    goto opend
3451b 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64  b_out;.  }..#ifd
3451c 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
3451d 5f 46 54 53 31 0a 20 20 69 66 28 20 21 64 62 2d  _FTS1.  if( !db-
3451e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
3451f 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20  .    extern int 
34520 73 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28  sqlite3Fts1Init(
34521 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72  sqlite3*);.    r
34522 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 31 49  c = sqlite3Fts1I
34523 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
34524 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
34525 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20  TE_ENABLE_FTS2. 
34526 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
34527 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51  Failed && rc==SQ
34528 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65  LITE_OK ){.    e
34529 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
3452a 33 46 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65  3Fts2Init(sqlite
3452b 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  3*);.    rc = sq
3452c 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 64 62  lite3Fts2Init(db
3452d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
3452e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
3452f 42 4c 45 5f 46 54 53 33 0a 20 20 69 66 28 20 21  BLE_FTS3.  if( !
34530 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
34531 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
34532 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
34533 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62  lite3Fts3Init(db
34534 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
34535 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
34536 42 4c 45 5f 49 43 55 0a 20 20 69 66 28 20 21 64  BLE_ICU.  if( !d
34537 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34538 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
34539 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
3453a 69 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b  ite3IcuInit(db);
3453b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
3453c 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
3453d 45 5f 52 54 52 45 45 0a 20 20 69 66 28 20 21 64  E_RTREE.  if( !d
3453e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3453f 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
34540 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
34541 74 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29  te3RtreeInit(db)
34542 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
34543 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
34544 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d   rc, 0);..  /* -
34545 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  DSQLITE_DEFAULT_
34546 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d  LOCKING_MODE=1 m
34547 61 6b 65 73 20 45 58 43 4c 55 53 49 56 45 20 74  akes EXCLUSIVE t
34548 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
34549 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d  ng.  ** mode.  -
3454a 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  DSQLITE_DEFAULT_
3454b 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d  LOCKING_MODE=0 m
3454c 61 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64  ake NORMAL the d
3454d 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20  efault locking. 
3454e 20 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67   ** mode.  Doing
3454f 20 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c 6c 20   nothing at all 
34550 61 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41  also makes NORMA
34551 4c 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20  L the default.. 
34552 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
34553 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
34554 47 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c  G_MODE.  db->dfl
34555 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49  tLockMode = SQLI
34556 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49  TE_DEFAULT_LOCKI
34557 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74  NG_MODE;.  sqlit
34558 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
34559 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  de(sqlite3BtreeP
3455a 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  ager(db->aDb[0].
3455b 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20  pBt),.          
3455c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3455d 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
3455e 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65  OCKING_MODE);.#e
3455f 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 61 62 6c  ndif..  /* Enabl
34560 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d  e the lookaside-
34561 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
34562 20 2a 2f 0a 20 20 73 65 74 75 70 4c 6f 6f 6b 61   */.  setupLooka
34563 73 69 64 65 28 64 62 2c 20 30 2c 20 73 71 6c 69  side(db, 0, sqli
34564 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
34565 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0a 20 20 20  szLookaside,.   
34566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34567 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
34568 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73  alConfig.nLookas
34569 69 64 65 29 3b 0a 0a 6f 70 65 6e 64 62 5f 6f 75  ide);..opendb_ou
3456a 74 3a 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20  t:.  if( db ){. 
3456b 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
3456c 75 74 65 78 21 3d 30 20 7c 7c 20 69 73 54 68 72  utex!=0 || isThr
3456d 65 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20 73 71  eadsafe==0 || sq
3456e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3456f 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d 30 20  g.bFullMutex==0 
34570 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
34571 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
34572 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20  utex);.  }.  rc 
34573 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  = sqlite3_errcod
34574 65 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  e(db);.  if( rc=
34575 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
34576 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f  .    sqlite3_clo
34577 73 65 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d  se(db);.    db =
34578 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
34579 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3457a 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
3457b 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49   SQLITE_MAGIC_SI
3457c 43 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20  CK;.  }.  *ppDb 
3457d 3d 20 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73  = db;.  return s
3457e 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
3457f 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f   rc);.}../*.** O
34580 70 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61  pen a new databa
34581 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  se handle..*/.SQ
34582 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
34583 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e  ite3_open(.  con
34584 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
34585 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  me, .  sqlite3 *
34586 2a 70 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75  *ppDb .){.  retu
34587 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28  rn openDatabase(
34588 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c  zFilename, ppDb,
34589 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3458a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
3458b 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
3458c 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
3458d 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  E, 0);.}.SQLITE_
3458e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3458f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74  open_v2(.  const
34590 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c   char *filename,
34591 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
34592 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20  ilename (UTF-8) 
34593 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  */.  sqlite3 **p
34594 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  pDb,         /* 
34595 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68  OUT: SQLite db h
34596 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66  andle */.  int f
34597 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
34598 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20     /* Flags */. 
34599 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66   const char *zVf
3459a 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  s        /* Name
3459b 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74   of VFS module t
3459c 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65  o use */.){.  re
3459d 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73  turn openDatabas
3459e 65 28 66 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62  e(filename, ppDb
3459f 2c 20 66 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a  , flags, zVfs);.
345a0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
345a1 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
345a2 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61  ** Open a new da
345a3 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a  tabase handle..*
345a4 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
345a5 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
345a6 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
345a7 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c  Filename, .  sql
345a8 69 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20  ite3 **ppDb.){. 
345a9 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69   char const *zFi
345aa 6c 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46  lename8;   /* zF
345ab 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20  ilename encoded 
345ac 69 6e 20 55 54 46 2d 38 20 69 6e 73 74 65 61 64  in UTF-8 instead
345ad 20 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20   of UTF-16 */.  
345ae 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
345af 56 61 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  Val;.  int rc;..
345b0 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e    assert( zFilen
345b1 61 6d 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ame );.  assert(
345b2 20 70 70 44 62 20 29 3b 0a 20 20 2a 70 70 44 62   ppDb );.  *ppDb
345b3 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
345b4 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
345b5 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  IT.  rc = sqlite
345b6 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  3_initialize();.
345b7 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
345b8 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70  n rc;.#endif.  p
345b9 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
345ba 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69  ueNew(0);.  sqli
345bb 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70  te3ValueSetStr(p
345bc 56 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61  Val, -1, zFilena
345bd 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  me, SQLITE_UTF16
345be 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
345bf 54 41 54 49 43 29 3b 0a 20 20 7a 46 69 6c 65 6e  TATIC);.  zFilen
345c0 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 56 61  ame8 = sqlite3Va
345c1 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51  lueText(pVal, SQ
345c2 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
345c3 28 20 7a 46 69 6c 65 6e 61 6d 65 38 20 29 7b 0a  ( zFilename8 ){.
345c4 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 61 74      rc = openDat
345c5 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 38  abase(zFilename8
345c6 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20  , ppDb,.        
345c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
345c8 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
345c9 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ITE | SQLITE_OPE
345ca 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 20 20  N_CREATE, 0);.  
345cb 20 20 61 73 73 65 72 74 28 20 2a 70 70 44 62 20    assert( *ppDb 
345cc 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
345cd 4d 45 4d 20 29 3b 0a 20 20 20 20 69 66 28 20 72  MEM );.    if( r
345ce 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
345cf 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 2a  !DbHasProperty(*
345d0 70 70 44 62 2c 20 30 2c 20 44 42 5f 53 63 68 65  ppDb, 0, DB_Sche
345d1 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20  maLoaded) ){.   
345d2 20 20 20 45 4e 43 28 2a 70 70 44 62 29 20 3d 20     ENC(*ppDb) = 
345d3 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
345d4 56 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  VE;.    }.  }els
345d5 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
345d6 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
345d7 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
345d8 28 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74 75 72  (pVal);..  retur
345d9 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  n sqlite3ApiExit
345da 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69  (0, rc);.}.#endi
345db 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
345dc 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
345dd 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
345de 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
345df 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
345e0 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a  base handle db..
345e1 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
345e2 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
345e3 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
345e4 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f  lite3* db, .  co
345e5 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
345e6 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20   .  int enc, .  
345e7 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e  void* pCtx,.  in
345e8 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69  t(*xCompare)(voi
345e9 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
345ea 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  d*,int,const voi
345eb 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  d*).){.  int rc;
345ec 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
345ed 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
345ee 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
345ef 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
345f0 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43  ;.  rc = createC
345f1 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  ollation(db, zNa
345f2 6d 65 2c 20 28 75 38 29 65 6e 63 2c 20 53 51 4c  me, (u8)enc, SQL
345f3 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70  ITE_COLL_USER, p
345f4 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30  Ctx, xCompare, 0
345f5 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
345f6 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
345f7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
345f8 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
345f9 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
345fa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
345fb 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
345fc 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
345fd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
345fe 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
345ff 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
34600 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
34601 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ion_v2(.  sqlite
34602 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
34603 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
34604 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
34605 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
34606 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
34607 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
34608 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
34609 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76  .  void(*xDel)(v
3460a 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
3460b 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
3460c 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3460d 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
3460e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3460f 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74   );.  rc = creat
34610 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
34611 4e 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c 20 53  Name, (u8)enc, S
34612 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c  QLITE_COLL_USER,
34613 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c   pCtx, xCompare,
34614 20 78 44 65 6c 29 3b 0a 20 20 72 63 20 3d 20 73   xDel);.  rc = s
34615 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
34616 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
34617 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
34618 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
34619 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  n rc;.}..#ifndef
3461a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
3461b 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  16./*.** Registe
3461c 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  r a new collatio
3461d 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  n sequence with 
3461e 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
3461f 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54  dle db..*/.SQLIT
34620 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
34621 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
34622 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a  on16(.  sqlite3*
34623 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f   db, .  const vo
34624 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74  id *zName,.  int
34625 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
34626 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
34627 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
34628 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
34629 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
3462a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3462b 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 4e  E_OK;.  char *zN
3462c 61 6d 65 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ame8;.  sqlite3_
3462d 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
3462e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74  mutex);.  assert
3462f 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
34630 6c 65 64 20 29 3b 0a 20 20 7a 4e 61 6d 65 38 20  led );.  zName8 
34631 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f  = sqlite3Utf16to
34632 38 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29  8(db, zName, -1)
34633 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 38 20 29  ;.  if( zName8 )
34634 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74  {.    rc = creat
34635 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
34636 4e 61 6d 65 38 2c 20 28 75 38 29 65 6e 63 2c 20  Name8, (u8)enc, 
34637 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52  SQLITE_COLL_USER
34638 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65  , pCtx, xCompare
34639 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
3463a 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
3463b 65 38 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  e8);.  }.  rc = 
3463c 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
3463d 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
3463e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3463f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
34640 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20  rn rc;.}.#endif 
34641 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
34642 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  TF16 */../*.** R
34643 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74  egister a collat
34644 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
34645 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69  tory callback wi
34646 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
34647 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65  handle.** db. Re
34648 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f  place any previo
34649 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63  usly installed c
3464a 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3464b 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51  e factory..*/.SQ
3464c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3464d 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
3464e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33  eeded(.  sqlite3
3464f 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *db, .  void *p
34650 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a  CollNeededArg, .
34651 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
34652 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  ded)(void*,sqlit
34653 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
34654 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b  ,const char*).){
34655 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34656 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
34657 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  );.  db->xCollNe
34658 65 64 65 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64  eded = xCollNeed
34659 65 64 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e  ed;.  db->xCollN
3465a 65 65 64 65 64 31 36 20 3d 20 30 3b 0a 20 20 64  eeded16 = 0;.  d
3465b 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
3465c 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41  g = pCollNeededA
3465d 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
3465e 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
3465f 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
34660 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
34661 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
34662 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67  _UTF16./*.** Reg
34663 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f  ister a collatio
34664 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f  n sequence facto
34665 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68  ry callback with
34666 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
34667 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c  ndle.** db. Repl
34668 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73  ace any previous
34669 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c  ly installed col
3466a 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3466b 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49  factory..*/.SQLI
3466c 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3466d 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
3466e 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ded16(.  sqlite3
3466f 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *db, .  void *p
34670 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a  CollNeededArg, .
34671 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
34672 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c  ded16)(void*,sql
34673 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
34674 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a  ep,const void*).
34675 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
34676 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
34677 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  ex);.  db->xColl
34678 4e 65 65 64 65 64 20 3d 20 30 3b 0a 20 20 64 62  Needed = 0;.  db
34679 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20  ->xCollNeeded16 
3467a 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b  = xCollNeeded16;
3467b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  .  db->pCollNeed
3467c 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65  edArg = pCollNee
3467d 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  dedArg;.  sqlite
3467e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3467f 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
34680 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
34681 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
34682 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
34683 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
34684 4d 49 54 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45  MIT_GLOBALRECOVE
34685 52 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  R.#ifndef SQLITE
34686 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
34687 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
34688 74 69 6f 6e 20 69 73 20 6e 6f 77 20 61 6e 20 61  tion is now an a
34689 6e 61 63 68 72 6f 6e 69 73 6d 2e 20 49 74 20 75  nachronism. It u
3468a 73 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 74  sed to be used t
3468b 6f 20 72 65 63 6f 76 65 72 20 66 72 6f 6d 20 61  o recover from a
3468c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  .** malloc() fai
3468d 6c 75 72 65 2c 20 62 75 74 20 53 51 4c 69 74 65  lure, but SQLite
3468e 20 6e 6f 77 20 64 6f 65 73 20 74 68 69 73 20 61   now does this a
3468f 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f  utomatically..*/
34690 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
34691 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72  sqlite3_global_r
34692 65 63 6f 76 65 72 28 76 6f 69 64 29 7b 0a 20 20  ecover(void){.  
34693 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
34694 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69  ;.}.#endif.#endi
34695 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f  f../*.** Test to
34696 20 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 20   see whether or 
34697 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65  not the database
34698 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69   connection is i
34699 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20  n autocommit.** 
3469a 6d 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52  mode.  Return TR
3469b 55 45 20 69 66 20 69 74 20 69 73 20 61 6e 64 20  UE if it is and 
3469c 46 41 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41  FALSE if not.  A
3469d 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69  utocommit mode i
3469e 73 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75  s on.** by defau
3469f 6c 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20  lt.  Autocommit 
346a0 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 61  is disabled by a
346a1 20 42 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74   BEGIN statement
346a2 20 61 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a   and reenabled.*
346a3 2a 20 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f  * by the next CO
346a4 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b  MMIT or ROLLBACK
346a5 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49  ..**.******* THI
346a6 53 20 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45  S IS AN EXPERIME
346a7 4e 54 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20  NTAL API AND IS 
346a8 53 55 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47  SUBJECT TO CHANG
346a9 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49  E ******.*/.SQLI
346aa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
346ab 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
346ac 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
346ad 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74    return db->aut
346ae 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64  oCommit;.}..#ifd
346af 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
346b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
346b1 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 73  ing routine is s
346b2 75 62 74 69 74 75 74 65 64 20 66 6f 72 20 63 6f  ubtituted for co
346b3 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f  nstant SQLITE_CO
346b4 52 52 55 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75  RRUPT in.** debu
346b5 67 67 69 6e 67 20 62 75 69 6c 64 73 2e 20 20 54  gging builds.  T
346b6 68 69 73 20 70 72 6f 76 69 64 65 73 20 61 20 77  his provides a w
346b7 61 79 20 74 6f 20 73 65 74 20 61 20 62 72 65 61  ay to set a brea
346b8 6b 70 6f 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a  kpoint for when.
346b9 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73  ** corruption is
346ba 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e   first detected.
346bb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
346bc 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f  TE int sqlite3Co
346bd 72 72 75 70 74 28 76 6f 69 64 29 7b 0a 20 20 72  rrupt(void){.  r
346be 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
346bf 52 55 50 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  RUPT;.}.#endif..
346c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
346c1 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f  MIT_DEPRECATED./
346c2 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63  *.** This is a c
346c3 6f 6e 76 65 6e 69 65 6e 63 65 20 72 6f 75 74 69  onvenience routi
346c4 6e 65 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75  ne that makes su
346c5 72 65 20 74 68 61 74 20 61 6c 6c 20 74 68 72 65  re that all thre
346c6 61 64 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 64  ad-specific.** d
346c7 61 74 61 20 66 6f 72 20 74 68 69 73 20 74 68 72  ata for this thr
346c8 65 61 64 20 68 61 73 20 62 65 65 6e 20 64 65 61  ead has been dea
346c9 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  llocated..**.** 
346ca 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72  SQLite no longer
346cb 20 75 73 65 73 20 74 68 72 65 61 64 2d 73 70 65   uses thread-spe
346cc 63 69 66 69 63 20 64 61 74 61 20 73 6f 20 74 68  cific data so th
346cd 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  is routine is no
346ce 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49  w a.** no-op.  I
346cf 74 20 69 73 20 72 65 74 61 69 6e 65 64 20 66 6f  t is retained fo
346d0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d  r historical com
346d1 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53  patibility..*/.S
346d2 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
346d3 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
346d4 65 61 6e 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 23  eanup(void){.}.#
346d5 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
346d6 75 72 6e 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61  urn meta informa
346d7 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 70 65  tion about a spe
346d8 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20  cific column of 
346d9 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
346da 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ..** See comment
346db 20 69 6e 20 73 71 6c 69 74 65 33 2e 68 20 28 73   in sqlite3.h (s
346dc 71 6c 69 74 65 2e 68 2e 69 6e 29 20 66 6f 72 20  qlite.h.in) for 
346dd 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64  details..*/.#ifd
346de 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
346df 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
346e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
346e1 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
346e2 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20  lumn_metadata(. 
346e3 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
346e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
346e5 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  Connection handl
346e6 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
346e7 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20  r *zDbName,     
346e8 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
346e9 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  ame or NULL */. 
346ea 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
346eb 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20  bleName,     /* 
346ec 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  Table name */.  
346ed 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
346ee 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43  umnName,    /* C
346ef 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
346f0 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44  char const **pzD
346f1 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f  ataType,    /* O
346f2 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20  UTPUT: Declared 
346f3 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63  data type */.  c
346f4 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f  har const **pzCo
346f5 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55  llSeq,     /* OU
346f6 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20  TPUT: Collation 
346f7 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f  sequence name */
346f8 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c  .  int *pNotNull
346f9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
346fa 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69  * OUTPUT: True i
346fb 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  f NOT NULL const
346fc 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a  raint exists */.
346fd 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b    int *pPrimaryK
346fe 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey,           /*
346ff 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66   OUTPUT: True if
34700 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20   column part of 
34701 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75  PK */.  int *pAu
34702 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20 20  toinc           
34703 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
34704 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
34705 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20   auto-increment 
34706 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
34707 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20    char *zErrMsg 
34708 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  = 0;.  Table *pT
34709 61 62 20 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e  ab = 0;.  Column
3470a 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e   *pCol = 0;.  in
3470b 74 20 69 43 6f 6c 3b 0a 0a 20 20 63 68 61 72 20  t iCol;..  char 
3470c 63 6f 6e 73 74 20 2a 7a 44 61 74 61 54 79 70 65  const *zDataType
3470d 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
3470e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20 30  st *zCollSeq = 0
3470f 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20  ;.  int notnull 
34710 3d 20 30 3b 0a 20 20 69 6e 74 20 70 72 69 6d 61  = 0;.  int prima
34711 72 79 6b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74  rykey = 0;.  int
34712 20 61 75 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20   autoinc = 0;.. 
34713 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64   /* Ensure the d
34714 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68  atabase schema h
34715 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a  as been loaded *
34716 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
34717 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
34718 78 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  x);.  (void)sqli
34719 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
3471a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
3471b 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72  nterAll(db);.  r
3471c 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28  c = sqlite3Init(
3471d 64 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20  db, &zErrMsg);. 
3471e 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
3471f 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  rc ){.    goto e
34720 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  rror_out;.  }.. 
34721 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74   /* Locate the t
34722 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e  able in question
34723 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
34724 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
34725 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44  , zTableName, zD
34726 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70  bName);.  if( !p
34727 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65  Tab || pTab->pSe
34728 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61 62  lect ){.    pTab
34729 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65   = 0;.    goto e
3472a 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  rror_out;.  }.. 
3472b 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f 6c   /* Find the col
3472c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e  umn for which in
3472d 66 6f 20 69 73 20 72 65 71 75 65 73 74 65 64 20  fo is requested 
3472e 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
3472f 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e  IsRowid(zColumnN
34730 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c  ame) ){.    iCol
34731 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a   = pTab->iPKey;.
34732 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20      if( iCol>=0 
34733 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d 20  ){.      pCol = 
34734 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  &pTab->aCol[iCol
34735 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  ];.    }.  }else
34736 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30  {.    for(iCol=0
34737 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ; iCol<pTab->nCo
34738 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  l; iCol++){.    
34739 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e    pCol = &pTab->
3473a 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20  aCol[iCol];.    
3473b 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
3473c 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e  StrICmp(pCol->zN
3473d 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  ame, zColumnName
3473e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  ) ){.        bre
3473f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
34740 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d  }.    if( iCol==
34741 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
34742 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20      pTab = 0;.  
34743 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
34744 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ut;.    }.  }.. 
34745 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
34746 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74  g block stores t
34747 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  he meta informat
34748 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ion that will be
34749 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74   returned.  ** t
3474a 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e 20  o the caller in 
3474b 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
3474c 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c  zDataType, zColl
3474d 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72  Seq, notnull, pr
3474e 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e  imarykey.  ** an
3474f 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74 68  d autoinc. At th
34750 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 61  is point there a
34751 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c 69  re two possibili
34752 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a  ties:.  ** .  **
34753 20 20 20 20 20 31 2e 20 54 68 65 20 73 70 65 63       1. The spec
34754 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ified column nam
34755 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22 6f  e was rowid", "o
34756 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22  id" or "_rowid_"
34757 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61 6e   .  **        an
34758 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 78  d there is no ex
34759 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
3475a 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20  d IPK column. . 
3475b 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20   **.  **     2. 
3475c 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  The table is not
3475d 20 61 20 76 69 65 77 20 61 6e 64 20 74 68 65 20   a view and the 
3475e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e  column name iden
3475f 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20  tified an .  ** 
34760 20 20 20 20 20 20 20 65 78 70 6c 69 63 69 74 6c         explicitl
34761 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
34762 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e 66  n. Copy meta inf
34763 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70  ormation from *p
34764 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  Col..  */ .  if(
34765 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61   pCol ){.    zDa
34766 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a  taType = pCol->z
34767 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53  Type;.    zCollS
34768 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c  eq = pCol->zColl
34769 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20  ;.    notnull = 
3476a 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30  pCol->notNull!=0
3476b 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79  ;.    primarykey
3476c 20 20 3d 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d    = pCol->isPrim
3476d 4b 65 79 21 3d 30 3b 0a 20 20 20 20 61 75 74 6f  Key!=0;.    auto
3476e 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  inc = pTab->iPKe
3476f 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54 61 62  y==iCol && (pTab
34770 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
34771 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 21 3d  Autoincrement)!=
34772 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
34773 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49 4e 54  zDataType = "INT
34774 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69 6d 61  EGER";.    prima
34775 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d 0a 20  rykey = 1;.  }. 
34776 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71 20 29   if( !zCollSeq )
34777 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d  {.    zCollSeq =
34778 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d 0a 0a   "BINARY";.  }..
34779 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73 71 6c  error_out:.  sql
3477a 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
3477b 6c 28 64 62 29 3b 0a 20 20 28 76 6f 69 64 29 73  l(db);.  (void)s
3477c 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
3477d 64 62 29 3b 0a 0a 20 20 2f 2a 20 57 68 65 74 68  db);..  /* Wheth
3477e 65 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  er the function 
3477f 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 20 6f  call succeeded o
34780 72 20 66 61 69 6c 65 64 2c 20 73 65 74 20 74 68  r failed, set th
34781 65 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74  e output paramet
34782 65 72 73 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74  ers.  ** to what
34783 65 76 65 72 20 74 68 65 69 72 20 6c 6f 63 61 6c  ever their local
34784 20 63 6f 75 6e 74 65 72 70 61 72 74 73 20 63 6f   counterparts co
34785 6e 74 61 69 6e 2e 20 49 66 20 61 6e 20 65 72 72  ntain. If an err
34786 6f 72 20 64 69 64 20 6f 63 63 75 72 2c 0a 20 20  or did occur,.  
34787 2a 2a 20 74 68 69 73 20 68 61 73 20 74 68 65 20  ** this has the 
34788 65 66 66 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e  effect of zeroin
34789 67 20 61 6c 6c 20 6f 75 74 70 75 74 20 70 61 72  g all output par
3478a 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  ameters..  */.  
3478b 69 66 28 20 70 7a 44 61 74 61 54 79 70 65 20 29  if( pzDataType )
3478c 20 2a 70 7a 44 61 74 61 54 79 70 65 20 3d 20 7a   *pzDataType = z
3478d 44 61 74 61 54 79 70 65 3b 0a 20 20 69 66 28 20  DataType;.  if( 
3478e 70 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43  pzCollSeq ) *pzC
3478f 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65  ollSeq = zCollSe
34790 71 3b 0a 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c  q;.  if( pNotNul
34791 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20  l ) *pNotNull = 
34792 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70  notnull;.  if( p
34793 50 72 69 6d 61 72 79 4b 65 79 20 29 20 2a 70 50  PrimaryKey ) *pP
34794 72 69 6d 61 72 79 4b 65 79 20 3d 20 70 72 69 6d  rimaryKey = prim
34795 61 72 79 6b 65 79 3b 0a 20 20 69 66 28 20 70 41  arykey;.  if( pA
34796 75 74 6f 69 6e 63 20 29 20 2a 70 41 75 74 6f 69  utoinc ) *pAutoi
34797 6e 63 20 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20  nc = autoinc;.. 
34798 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
34799 72 63 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20  rc && !pTab ){. 
3479a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
3479b 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
3479c 20 20 20 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c     zErrMsg = sql
3479d 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
3479e 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 20 63  "no such table c
3479f 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73 22 2c 20 7a  olumn: %s.%s", z
347a0 54 61 62 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20  TableName,.     
347a1 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b     zColumnName);
347a2 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
347a3 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71  _ERROR;.  }.  sq
347a4 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
347a5 63 2c 20 28 7a 45 72 72 4d 73 67 3f 22 25 73 22  c, (zErrMsg?"%s"
347a6 3a 30 29 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  :0), zErrMsg);. 
347a7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
347a8 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72  b, zErrMsg);.  r
347a9 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
347aa 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
347ab 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
347ac 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
347ad 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
347ae 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70  dif../*.** Sleep
347af 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68   for a little wh
347b0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ile.  Return the
347b1 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20   amount of time 
347b2 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  slept..*/.SQLITE
347b3 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
347b4 5f 73 6c 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a  _sleep(int ms){.
347b5 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
347b6 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  Vfs;.  int rc;. 
347b7 20 70 56 66 73 20 3d 20 73 71 6c 69 74 65 33 5f   pVfs = sqlite3_
347b8 76 66 73 5f 66 69 6e 64 28 30 29 3b 0a 20 20 69  vfs_find(0);.  i
347b9 66 28 20 70 56 66 73 3d 3d 30 20 29 20 72 65 74  f( pVfs==0 ) ret
347ba 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69  urn 0;..  /* Thi
347bb 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73  s function works
347bc 20 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73   in milliseconds
347bd 2c 20 62 75 74 20 74 68 65 20 75 6e 64 65 72 6c  , but the underl
347be 79 69 6e 67 20 4f 73 53 6c 65 65 70 28 29 20 0a  ying OsSleep() .
347bf 20 20 2a 2a 20 41 50 49 20 75 73 65 73 20 6d 69    ** API uses mi
347c0 63 72 6f 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63  croseconds. Henc
347c1 65 20 74 68 65 20 31 30 30 30 27 73 2e 0a 20 20  e the 1000's..  
347c2 2a 2f 0a 20 20 72 63 20 3d 20 28 73 71 6c 69 74  */.  rc = (sqlit
347c3 65 33 4f 73 53 6c 65 65 70 28 70 56 66 73 2c 20  e3OsSleep(pVfs, 
347c4 31 30 30 30 2a 6d 73 29 2f 31 30 30 30 29 3b 0a  1000*ms)/1000);.
347c5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
347c6 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20  /*.** Enable or 
347c7 64 69 73 61 62 6c 65 20 74 68 65 20 65 78 74 65  disable the exte
347c8 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
347c9 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
347ca 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74   int sqlite3_ext
347cb 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
347cc 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  es(sqlite3 *db, 
347cd 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71  int onoff){.  sq
347ce 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
347cf 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
347d0 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 6f 6e  db->errMask = on
347d1 6f 66 66 20 3f 20 30 78 66 66 66 66 66 66 66 66  off ? 0xffffffff
347d2 20 3a 20 30 78 66 66 3b 0a 20 20 73 71 6c 69 74   : 0xff;.  sqlit
347d3 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
347d4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
347d5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
347d6 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
347d7 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
347d8 6d 65 74 68 6f 64 20 6f 6e 20 61 20 70 61 72 74  method on a part
347d9 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 2e  icular database.
347da 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
347db 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  nt sqlite3_file_
347dc 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 20  control(sqlite3 
347dd 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
347de 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70  *zDbName, int op
347df 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
347e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
347e1 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74 20 69 44  _ERROR;.  int iD
347e2 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  b;.  sqlite3_mut
347e3 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
347e4 65 78 29 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61  ex);.  if( zDbNa
347e5 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 44 62  me==0 ){.    iDb
347e6 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
347e7 20 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 69 44     for(iDb=0; iD
347e8 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b  b<db->nDb; iDb++
347e9 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  ){.      if( str
347ea 63 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  cmp(db->aDb[iDb]
347eb 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29  .zName, zDbName)
347ec 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
347ed 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 62   }.  }.  if( iDb
347ee 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20  <db->nDb ){.    
347ef 42 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20  Btree *pBtree = 
347f0 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
347f1 3b 0a 20 20 20 20 69 66 28 20 70 42 74 72 65 65  ;.    if( pBtree
347f2 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20   ){.      Pager 
347f3 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 73  *pPager;.      s
347f4 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b  qlite3_file *fd;
347f5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
347f6 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29  reeEnter(pBtree)
347f7 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d  ;.      pPager =
347f8 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
347f9 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20  er(pBtree);.    
347fa 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
347fb 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 64 20  !=0 );.      fd 
347fc 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
347fd 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  le(pPager);.    
347fe 20 20 61 73 73 65 72 74 28 20 66 64 21 3d 30 20    assert( fd!=0 
347ff 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64 2d  );.      if( fd-
34800 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  >pMethods ){.   
34801 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
34802 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66  3OsFileControl(f
34803 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20  d, op, pArg);.  
34804 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
34805 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42  te3BtreeLeave(pB
34806 74 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  tree);.    }.  }
34807 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34808 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
34809 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20  );.  return rc; 
3480a 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65    .}../*.** Inte
3480b 72 66 61 63 65 20 74 6f 20 74 68 65 20 74 65 73  rface to the tes
3480c 74 69 6e 67 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53  ting logic..*/.S
3480d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3480e 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3480f 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b  ol(int op, ...){
34810 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23  .  int rc = 0;.#
34811 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34812 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
34813 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
34814 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29  va_start(ap, op)
34815 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
34816 7b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  {..    /*.    **
34817 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   Save the curren
34818 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50  t state of the P
34819 52 4e 47 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  RNG..    */.    
3481a 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
3481b 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 3a 20  CTRL_PRNG_SAVE: 
3481c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
3481d 72 6e 67 53 61 76 65 53 74 61 74 65 28 29 3b 0a  rngSaveState();.
3481e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3481f 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
34820 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 73 74  * Restore the st
34821 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47 20  ate of the PRNG 
34822 74 6f 20 74 68 65 20 6c 61 73 74 20 73 74 61 74  to the last stat
34823 65 20 73 61 76 65 64 20 75 73 69 6e 67 0a 20 20  e saved using.  
34824 20 20 2a 2a 20 50 52 4e 47 5f 53 41 56 45 2e 20    ** PRNG_SAVE. 
34825 20 49 66 20 50 52 4e 47 5f 53 41 56 45 20 68 61   If PRNG_SAVE ha
34826 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62  s never before b
34827 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e  een called, then
34828 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 76 65 72  .    ** this ver
34829 62 20 61 63 74 73 20 6c 69 6b 65 20 50 52 4e 47  b acts like PRNG
3482a 5f 52 45 53 45 54 2e 0a 20 20 20 20 2a 2f 0a 20  _RESET..    */. 
3482b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
3482c 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53  ESTCTRL_PRNG_RES
3482d 54 4f 52 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  TORE: {.      sq
3482e 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
3482f 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62  State();.      b
34830 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
34831 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73 65 74   /*.    ** Reset
34832 20 74 68 65 20 50 52 4e 47 20 62 61 63 6b 20 74   the PRNG back t
34833 6f 20 69 74 73 20 75 6e 69 6e 69 74 69 61 6c 69  o its uninitiali
34834 7a 65 64 20 73 74 61 74 65 2e 20 20 54 68 65 20  zed state.  The 
34835 6e 65 78 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a  next call.    **
34836 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 61 6e 64   to sqlite3_rand
34837 6f 6d 6e 65 73 73 28 29 20 77 69 6c 6c 20 72 65  omness() will re
34838 73 65 65 64 20 74 68 65 20 50 52 4e 47 20 75 73  seed the PRNG us
34839 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 61 6c  ing a single cal
3483a 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  l.    ** to the 
3483b 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68  xRandomness meth
3483c 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  od of the defaul
3483d 74 20 56 46 53 2e 0a 20 20 20 20 2a 2f 0a 20 20  t VFS..    */.  
3483e 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
3483f 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45  STCTRL_PRNG_RESE
34840 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  T: {.      sqlit
34841 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65  e3PrngResetState
34842 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ();.      break;
34843 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
34844 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74     **  sqlite3_t
34845 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 49 54 56  est_control(BITV
34846 45 43 5f 54 45 53 54 2c 20 73 69 7a 65 2c 20 70  EC_TEST, size, p
34847 72 6f 67 72 61 6d 29 0a 20 20 20 20 2a 2a 0a 20  rogram).    **. 
34848 20 20 20 2a 2a 20 52 75 6e 20 61 20 74 65 73 74     ** Run a test
34849 20 61 67 61 69 6e 73 74 20 61 20 42 69 74 76 65   against a Bitve
3484a 63 20 6f 62 6a 65 63 74 20 6f 66 20 73 69 7a 65  c object of size
3484b 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 61  .  The program a
3484c 72 67 75 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69  rgument.    ** i
3484d 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e  s an array of in
3484e 74 65 67 65 72 73 20 74 68 61 74 20 64 65 66 69  tegers that defi
3484f 6e 65 73 20 74 68 65 20 74 65 73 74 2e 20 20 52  nes the test.  R
34850 65 74 75 72 6e 20 2d 31 20 6f 6e 20 61 0a 20 20  eturn -1 on a.  
34851 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f    ** memory allo
34852 63 61 74 69 6f 6e 20 65 72 72 6f 72 2c 20 30 20  cation error, 0 
34853 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 72 20 6e  on success, or n
34854 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 6e 20 65  on-zero for an e
34855 72 72 6f 72 2e 0a 20 20 20 20 2a 2a 20 53 65 65  rror..    ** See
34856 20 74 68 65 20 73 71 6c 69 74 65 33 42 69 74 76   the sqlite3Bitv
34857 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 29 20  ecBuiltinTest() 
34858 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
34859 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20  nformation..    
3485a 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
3485b 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56  TE_TESTCTRL_BITV
3485c 45 43 5f 54 45 53 54 3a 20 7b 0a 20 20 20 20 20  EC_TEST: {.     
3485d 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67   int sz = va_arg
3485e 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
3485f 20 69 6e 74 20 2a 61 50 72 6f 67 20 3d 20 76 61   int *aProg = va
34860 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a  _arg(ap, int*);.
34861 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
34862 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54  e3BitvecBuiltinT
34863 65 73 74 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0a  est(sz, aProg);.
34864 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34865 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
34866 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  *  sqlite3_test_
34867 63 6f 6e 74 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d  control(BENIGN_M
34868 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c 20 78 42 65  ALLOC_HOOKS, xBe
34869 67 69 6e 2c 20 78 45 6e 64 29 0a 20 20 20 20 2a  gin, xEnd).    *
3486a 2a 0a 20 20 20 20 2a 2a 20 52 65 67 69 73 74 65  *.    ** Registe
3486b 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20  r hooks to call 
3486c 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 69 63  to indicate whic
3486d 68 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  h malloc() failu
3486e 72 65 73 20 0a 20 20 20 20 2a 2a 20 61 72 65 20  res .    ** are 
3486f 62 65 6e 69 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20  benign..    */. 
34870 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
34871 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d  ESTCTRL_BENIGN_M
34872 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20  ALLOC_HOOKS: {. 
34873 20 20 20 20 20 74 79 70 65 64 65 66 20 76 6f 69       typedef voi
34874 64 20 28 2a 76 6f 69 64 5f 66 75 6e 63 74 69 6f  d (*void_functio
34875 6e 29 28 76 6f 69 64 29 3b 0a 20 20 20 20 20 20  n)(void);.      
34876 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42  void_function xB
34877 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 20 20  enignBegin;.    
34878 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20    void_function 
34879 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 20 20 20 20  xBenignEnd;.    
3487a 20 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d    xBenignBegin =
3487b 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
3487c 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20  _function);.    
3487d 20 20 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76    xBenignEnd = v
3487e 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66  a_arg(ap, void_f
3487f 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20  unction);.      
34880 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c  sqlite3BenignMal
34881 6c 6f 63 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e  locHooks(xBenign
34882 42 65 67 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e  Begin, xBenignEn
34883 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  d);.      break;
34884 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
34885 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74     **  sqlite3_t
34886 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49  est_control(SQLI
34887 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44  TE_TESTCTRL_PEND
34888 49 4e 47 5f 42 59 54 45 2c 20 75 6e 73 69 67 6e  ING_BYTE, unsign
34889 65 64 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a  ed int X).    **
3488a 0a 20 20 20 20 2a 2a 20 53 65 74 20 74 68 65 20  .    ** Set the 
3488b 50 45 4e 44 49 4e 47 20 62 79 74 65 20 74 6f 20  PENDING byte to 
3488c 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65  the value in the
3488d 20 61 72 67 75 6d 65 6e 74 2c 20 69 66 20 58 3e   argument, if X>
3488e 30 2e 0a 20 20 20 20 2a 2a 20 4d 61 6b 65 20 6e  0..    ** Make n
3488f 6f 20 63 68 61 6e 67 65 73 20 69 66 20 58 3d 3d  o changes if X==
34890 30 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 76  0.  Return the v
34891 61 6c 75 65 20 6f 66 20 74 68 65 20 70 65 6e 64  alue of the pend
34892 69 6e 67 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  ing byte.    ** 
34893 61 73 20 69 74 20 65 78 69 73 74 69 6e 67 20 62  as it existing b
34894 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69  efore this routi
34895 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20  ne was called.. 
34896 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 4d 50     **.    ** IMP
34897 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e  ORTANT:  Changin
34898 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79  g the PENDING by
34899 74 65 20 66 72 6f 6d 20 30 78 34 30 30 30 30 30  te from 0x400000
3489a 30 30 20 72 65 73 75 6c 74 73 20 69 6e 0a 20 20  00 results in.  
3489b 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6d 70 61 74    ** an incompat
3489c 69 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69  ible database fi
3489d 6c 65 20 66 6f 72 6d 61 74 2e 20 20 43 68 61 6e  le format.  Chan
3489e 67 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e 47  ging the PENDING
3489f 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 77 68 69   byte.    ** whi
348a0 6c 65 20 61 6e 79 20 64 61 74 61 62 61 73 65 20  le any database 
348a1 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70  connection is op
348a2 65 6e 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  en results in un
348a3 64 65 66 69 6e 65 64 20 61 6e 64 0a 20 20 20 20  defined and.    
348a4 2a 2a 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62  ** dileterious b
348a5 65 68 61 76 69 6f 72 2e 0a 20 20 20 20 2a 2f 0a  ehavior..    */.
348a6 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
348a7 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47  TESTCTRL_PENDING
348a8 5f 42 59 54 45 3a 20 7b 0a 20 20 20 20 20 20 75  _BYTE: {.      u
348a9 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 56  nsigned int newV
348aa 61 6c 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  al = va_arg(ap, 
348ab 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20  unsigned int);. 
348ac 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
348ad 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 20 20  3PendingByte;.  
348ae 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c 20 29      if( newVal )
348af 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42   sqlite3PendingB
348b0 79 74 65 20 3d 20 6e 65 77 56 61 6c 3b 0a 20 20  yte = newVal;.  
348b1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
348b2 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
348b3 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
348b4 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53  ntrol(SQLITE_TES
348b5 54 43 54 52 4c 5f 41 53 53 45 52 54 2c 20 69 6e  TCTRL_ASSERT, in
348b6 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  t X).    **.    
348b7 2a 2a 20 54 68 69 73 20 61 63 74 69 6f 6e 20 70  ** This action p
348b8 72 6f 76 69 64 65 73 20 61 20 72 75 6e 2d 74 69  rovides a run-ti
348b9 6d 65 20 74 65 73 74 20 74 6f 20 73 65 65 20 77  me test to see w
348ba 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20  hether or not.  
348bb 20 20 2a 2a 20 61 73 73 65 72 74 28 29 20 77 61    ** assert() wa
348bc 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d  s enabled at com
348bd 70 69 6c 65 2d 74 69 6d 65 2e 20 20 49 66 20 58  pile-time.  If X
348be 20 69 73 20 74 72 75 65 20 61 6e 64 20 61 73 73   is true and ass
348bf 65 72 74 28 29 0a 20 20 20 20 2a 2a 20 69 73 20  ert().    ** is 
348c0 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  enabled, then th
348c1 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
348c2 73 20 74 72 75 65 2e 20 20 49 66 20 58 20 69 73  s true.  If X is
348c3 20 74 72 75 65 20 61 6e 64 0a 20 20 20 20 2a 2a   true and.    **
348c4 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69 73   assert() is dis
348c5 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20  abled, then the 
348c6 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
348c7 7a 65 72 6f 2e 20 20 49 66 20 58 20 69 73 0a 20  zero.  If X is. 
348c8 20 20 20 2a 2a 20 66 61 6c 73 65 20 61 6e 64 20     ** false and 
348c9 61 73 73 65 72 74 28 29 20 69 73 20 65 6e 61 62  assert() is enab
348ca 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 73  led, then the as
348cb 73 65 72 74 69 6f 6e 20 66 69 72 65 73 20 61 6e  sertion fires an
348cc 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f  d the.    ** pro
348cd 63 65 73 73 20 61 62 6f 72 74 73 2e 20 20 49 66  cess aborts.  If
348ce 20 58 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20   X is false and 
348cf 61 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61  assert() is disa
348d0 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20  bled, then the. 
348d1 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c     ** return val
348d2 75 65 20 69 73 20 7a 65 72 6f 2e 0a 20 20 20 20  ue is zero..    
348d3 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
348d4 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
348d5 52 54 3a 20 7b 0a 20 20 20 20 20 20 76 6f 6c 61  RT: {.      vola
348d6 74 69 6c 65 20 69 6e 74 20 78 20 3d 20 30 3b 0a  tile int x = 0;.
348d7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 78        assert( (x
348d8 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
348d9 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 72  ))!=0 );.      r
348da 63 20 3d 20 78 3b 0a 20 20 20 20 20 20 62 72 65  c = x;.      bre
348db 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20  ak;.    }...    
348dc 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
348dd 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
348de 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
348df 41 4c 57 41 59 53 2c 20 69 6e 74 20 58 29 0a 20  ALWAYS, int X). 
348e0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69     **.    ** Thi
348e1 73 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  s action provide
348e2 73 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73  s a run-time tes
348e3 74 20 74 6f 20 73 65 65 20 68 6f 77 20 74 68 65  t to see how the
348e4 20 41 4c 57 41 59 53 20 61 6e 64 0a 20 20 20 20   ALWAYS and.    
348e5 2a 2a 20 4e 45 56 45 52 20 6d 61 63 72 6f 73 20  ** NEVER macros 
348e6 77 65 72 65 20 64 65 66 69 6e 65 64 20 61 74 20  were defined at 
348e7 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 20 20  compile-time..  
348e8 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
348e9 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
348ea 41 4c 57 41 59 53 28 58 29 2e 20 20 0a 20 20 20  ALWAYS(X).  .   
348eb 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
348ec 65 63 6f 6d 6d 65 6e 64 65 64 20 74 65 73 74 20  ecommended test 
348ed 69 73 20 58 3d 3d 32 2e 20 20 49 66 20 74 68 65  is X==2.  If the
348ee 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
348ef 20 32 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 20   2, that means. 
348f0 20 20 20 2a 2a 20 41 4c 57 41 59 53 28 29 20 61     ** ALWAYS() a
348f1 6e 64 20 4e 45 56 45 52 28 29 20 61 72 65 20 62  nd NEVER() are b
348f2 6f 74 68 20 6e 6f 2d 6f 70 20 70 61 73 73 2d 74  oth no-op pass-t
348f3 68 72 6f 75 67 68 20 6d 61 63 72 6f 73 2c 20 77  hrough macros, w
348f4 68 69 63 68 20 69 73 20 74 68 65 0a 20 20 20 20  hich is the.    
348f5 2a 2a 20 64 65 66 61 75 6c 74 20 73 65 74 74 69  ** default setti
348f6 6e 67 2e 20 20 49 66 20 74 68 65 20 72 65 74 75  ng.  If the retu
348f7 72 6e 20 76 61 6c 75 65 20 69 73 20 31 2c 20 74  rn value is 1, t
348f8 68 65 6e 20 41 4c 57 41 59 53 28 29 20 69 73 20  hen ALWAYS() is 
348f9 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 68 61  either.    ** ha
348fa 72 64 2d 63 6f 64 65 64 20 74 6f 20 74 72 75 65  rd-coded to true
348fb 20 6f 72 20 65 6c 73 65 20 69 74 20 61 73 73 65   or else it asse
348fc 72 74 73 20 69 66 20 69 74 73 20 61 72 67 75 6d  rts if its argum
348fd 65 6e 74 20 69 73 20 66 61 6c 73 65 2e 0a 20 20  ent is false..  
348fe 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62    ** The first b
348ff 65 68 61 76 69 6f 72 20 28 68 61 72 64 2d 63 6f  ehavior (hard-co
34900 64 65 64 20 74 6f 20 74 72 75 65 29 20 69 73 20  ded to true) is 
34901 74 68 65 20 63 61 73 65 20 69 66 0a 20 20 20 20  the case if.    
34902 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ** SQLITE_TESTCT
34903 52 4c 5f 41 53 53 45 52 54 20 73 68 6f 77 73 20  RL_ASSERT shows 
34904 74 68 61 74 20 61 73 73 65 72 74 28 29 20 69 73  that assert() is
34905 20 64 69 73 61 62 6c 65 64 20 61 6e 64 20 74 68   disabled and th
34906 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20  e second.    ** 
34907 62 65 68 61 76 69 6f 72 20 28 61 73 73 65 72 74  behavior (assert
34908 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   if the argument
34909 20 74 6f 20 41 4c 57 41 59 53 28 29 20 69 73 20   to ALWAYS() is 
3490a 66 61 6c 73 65 29 20 69 73 20 74 68 65 20 63 61  false) is the ca
3490b 73 65 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c  se if.    ** SQL
3490c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53  ITE_TESTCTRL_ASS
3490d 45 52 54 20 73 68 6f 77 73 20 74 68 61 74 20 61  ERT shows that a
3490e 73 73 65 72 74 28 29 20 69 73 20 65 6e 61 62 6c  ssert() is enabl
3490f 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed..    **.    *
34910 2a 20 54 68 65 20 72 75 6e 2d 74 69 6d 65 20 74  * The run-time t
34911 65 73 74 20 70 72 6f 63 65 64 75 72 65 20 6d 69  est procedure mi
34912 67 68 74 20 6c 6f 6f 6b 20 73 6f 6d 65 74 68 69  ght look somethi
34913 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20  ng like this:.  
34914 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69    **.    **    i
34915 66 28 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  f( sqlite3_test_
34916 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54  control(SQLITE_T
34917 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 2c 20  ESTCTRL_ALWAYS, 
34918 32 29 3d 3d 32 20 29 7b 0a 20 20 20 20 2a 2a 20  2)==2 ){.    ** 
34919 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28 29       // ALWAYS()
3491a 20 61 6e 64 20 4e 45 56 45 52 28 29 20 61 72 65   and NEVER() are
3491b 20 6e 6f 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f   no-op pass-thro
3491c 75 67 68 20 6d 61 63 72 6f 73 0a 20 20 20 20 2a  ugh macros.    *
3491d 2a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73  *    }else if( s
3491e 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
3491f 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
34920 54 52 4c 5f 41 53 53 45 52 54 2c 20 31 29 20 29  TRL_ASSERT, 1) )
34921 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f  {.    **      //
34922 20 41 4c 57 41 59 53 28 78 29 20 61 73 73 65 72   ALWAYS(x) asser
34923 74 73 20 74 68 61 74 20 78 20 69 73 20 74 72 75  ts that x is tru
34924 65 2e 20 4e 45 56 45 52 28 78 29 20 61 73 73 65  e. NEVER(x) asse
34925 72 74 73 20 78 20 69 73 20 66 61 6c 73 65 2e 0a  rts x is false..
34926 20 20 20 20 2a 2a 20 20 20 20 7d 65 6c 73 65 7b      **    }else{
34927 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20  .    **      // 
34928 41 4c 57 41 59 53 28 78 29 20 69 73 20 61 20 63  ALWAYS(x) is a c
34929 6f 6e 73 74 61 6e 74 20 31 2e 20 20 4e 45 56 45  onstant 1.  NEVE
3492a 52 28 78 29 20 69 73 20 61 20 63 6f 6e 73 74 61  R(x) is a consta
3492b 6e 74 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 20  nt 0..    **    
3492c 7d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  }.    */.    cas
3492d 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3492e 4c 5f 41 4c 57 41 59 53 3a 20 7b 0a 20 20 20 20  L_ALWAYS: {.    
3492f 20 20 69 6e 74 20 78 20 3d 20 76 61 5f 61 72 67    int x = va_arg
34930 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
34931 72 63 20 3d 20 41 4c 57 41 59 53 28 78 29 3b 0a  rc = ALWAYS(x);.
34932 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34933 20 7d 0a 0a 20 20 20 20 2f 2a 20 20 20 73 71 6c   }..    /*   sql
34934 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
34935 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  l(SQLITE_TESTCTR
34936 4c 5f 52 45 53 45 52 56 45 2c 20 73 71 6c 69 74  L_RESERVE, sqlit
34937 65 33 20 2a 64 62 2c 20 69 6e 74 20 4e 29 0a 20  e3 *db, int N). 
34938 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74     **.    ** Set
34939 20 74 68 65 20 6e 52 65 73 65 72 76 65 20 73 69   the nReserve si
3493a 7a 65 20 74 6f 20 4e 20 66 6f 72 20 74 68 65 20  ze to N for the 
3493b 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 6f 6e  main database on
3493c 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
3493d 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20    ** connection 
3493e 64 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  db..    */.    c
3493f 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
34940 54 52 4c 5f 52 45 53 45 52 56 45 3a 20 7b 0a 20  TRL_RESERVE: {. 
34941 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
34942 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 73 71   = va_arg(ap, sq
34943 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 20 20 69  lite3*);.      i
34944 6e 74 20 78 20 3d 20 76 61 5f 61 72 67 28 61 70  nt x = va_arg(ap
34945 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  ,int);.      sql
34946 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
34947 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (db->mutex);.   
34948 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
34949 65 74 50 61 67 65 53 69 7a 65 28 64 62 2d 3e 61  etPageSize(db->a
3494a 44 62 5b 30 5d 2e 70 42 74 2c 20 30 2c 20 78 2c  Db[0].pBt, 0, x,
3494b 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
3494c 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3494d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  b->mutex);.     
3494e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
3494f 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b   }.  va_end(ap);
34950 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
34951 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
34952 45 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  EST */.  return 
34953 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rc;.}../********
34954 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61  ****** End of ma
34955 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
34956 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34958 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
34959 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3495a 65 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a  e notify.c *****
3495b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3495c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3495d 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
3495e 39 20 4d 61 72 63 68 20 33 0a 2a 2a 0a 2a 2a 20  9 March 3.**.** 
3495f 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
34960 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
34961 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
34962 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
34963 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
34964 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
34965 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
34966 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
34967 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
34968 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
34969 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
3496a 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
3496b 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
3496c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
3496d 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
3496e 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
3496f 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
34970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34971 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34972 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34973 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34974 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
34975 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
34976 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
34977 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
34978 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
34979 79 28 29 0a 2a 2a 20 41 50 49 20 6d 65 74 68 6f  y().** API metho
3497a 64 20 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69  d and its associ
3497b 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 61 6c 69  ated functionali
3497c 74 79 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20  ty..*/../* Omit 
3497d 74 68 69 73 20 65 6e 74 69 72 65 20 66 69 6c 65  this entire file
3497e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
3497f 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20  E_UNLOCK_NOTIFY 
34980 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 20  is not defined. 
34981 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
34982 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e  _ENABLE_UNLOCK_N
34983 4f 54 49 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62  OTIFY../*.** Pub
34984 6c 69 63 20 69 6e 74 65 72 66 61 63 65 73 3a 0a  lic interfaces:.
34985 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43  **.**   sqlite3C
34986 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
34987 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43  ().**   sqlite3C
34988 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
34989 64 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  d().**   sqlite3
3498a 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
3498b 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 5f  ().**   sqlite3_
3498c 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a  unlock_notify().
3498d 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 61 73 73 65  */..#define asse
3498e 72 74 4d 75 74 65 78 48 65 6c 64 28 29 20 5c 0a  rtMutexHeld() \.
3498f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
34990 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
34991 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
34992 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
34993 49 43 5f 4d 41 53 54 45 52 29 29 20 29 0a 0a 2f  IC_MASTER)) )../
34994 2a 0a 2a 2a 20 48 65 61 64 20 6f 66 20 61 20 6c  *.** Head of a l
34995 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
34996 6c 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74  l sqlite3 object
34997 73 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  s created by thi
34998 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 66 6f 72  s process.** for
34999 20 77 68 69 63 68 20 65 69 74 68 65 72 20 73 71   which either sq
3499a 6c 69 74 65 33 2e 70 42 6c 6f 63 6b 69 6e 67 43  lite3.pBlockingC
3499b 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 71 6c  onnection or sql
3499c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  ite3.pUnlockConn
3499d 65 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74  ection.** is not
3499e 20 4e 55 4c 4c 2e 20 54 68 69 73 20 76 61 72 69   NULL. This vari
3499f 61 62 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 61 63  able may only ac
349a0 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 68 65  cessed while the
349a1 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a   STATIC_MASTER.*
349a2 2a 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e  * mutex is held.
349a3 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
349a4 65 33 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73  e3 *SQLITE_WSD s
349a5 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
349a6 74 20 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20  t = 0;..#ifndef 
349a7 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69  NDEBUG./*.** Thi
349a8 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
349a9 63 6f 6d 70 6c 65 78 20 61 73 73 65 72 74 28 29  complex assert()
349aa 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20 74   that verifies t
349ab 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
349ac 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66 20 74   properties of t
349ad 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
349ae 63 74 69 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a  ctions list:.**.
349af 2a 2a 20 20 20 31 29 20 45 61 63 68 20 65 6e 74  **   1) Each ent
349b0 72 79 20 69 6e 20 74 68 65 20 6c 69 73 74 20 68  ry in the list h
349b1 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61  as a non-NULL va
349b2 6c 75 65 20 66 6f 72 20 65 69 74 68 65 72 20 0a  lue for either .
349b3 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43  **      pUnlockC
349b4 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c  onnection or pBl
349b5 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
349b6 2c 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a  , or both..**.**
349b7 20 20 20 32 29 20 41 6c 6c 20 65 6e 74 72 69 65     2) All entrie
349b8 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 68  s in the list th
349b9 61 74 20 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f  at share a commo
349ba 6e 20 76 61 6c 75 65 20 66 6f 72 20 0a 2a 2a 20  n value for .** 
349bb 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69       xUnlockNoti
349bc 66 79 20 61 72 65 20 67 72 6f 75 70 65 64 20 74  fy are grouped t
349bd 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20  ogether..**.**  
349be 20 33 29 20 49 66 20 74 68 65 20 61 72 67 75 6d   3) If the argum
349bf 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55  ent db is not NU
349c0 4c 4c 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  LL, then none of
349c1 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20   the entries in 
349c2 74 68 65 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 63  the.**      bloc
349c3 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
349c4 6c 69 73 74 20 68 61 76 65 20 70 55 6e 6c 6f 63  list have pUnloc
349c5 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70  kConnection or p
349c6 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
349c7 6f 6e 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74  on.**      set t
349c8 6f 20 64 62 2e 20 54 68 69 73 20 69 73 20 75 73  o db. This is us
349c9 65 64 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20  ed when closing 
349ca 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 2a  connection db..*
349cb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  /.static void ch
349cc 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
349cd 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
349ce 20 20 73 71 6c 69 74 65 33 20 2a 70 3b 0a 20 20    sqlite3 *p;.  
349cf 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f  for(p=sqlite3Blo
349d0 63 6b 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70  ckedList; p; p=p
349d1 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b  ->pNextBlocked){
349d2 0a 20 20 20 20 69 6e 74 20 73 65 65 6e 20 3d 20  .    int seen = 
349d3 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  0;.    sqlite3 *
349d4 70 32 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69  p2;..    /* Veri
349d5 66 79 20 70 72 6f 70 65 72 74 79 20 28 31 29 20  fy property (1) 
349d6 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
349d7 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
349d8 69 6f 6e 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b  ion || p->pBlock
349d9 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b  ingConnection );
349da 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20  ..    /* Verify 
349db 70 72 6f 70 65 72 74 79 20 28 32 29 20 2a 2f 0a  property (2) */.
349dc 20 20 20 20 66 6f 72 28 70 32 3d 73 71 6c 69 74      for(p2=sqlit
349dd 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 70  e3BlockedList; p
349de 32 21 3d 70 3b 20 70 32 3d 70 32 2d 3e 70 4e 65  2!=p; p2=p2->pNe
349df 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20  xtBlocked){.    
349e0 20 20 69 66 28 20 70 32 2d 3e 78 55 6e 6c 6f 63    if( p2->xUnloc
349e1 6b 4e 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c  kNotify==p->xUnl
349e2 6f 63 6b 4e 6f 74 69 66 79 20 29 20 73 65 65 6e  ockNotify ) seen
349e3 20 3d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65   = 1;.      asse
349e4 72 74 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e  rt( p2->xUnlockN
349e5 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63  otify==p->xUnloc
349e6 6b 4e 6f 74 69 66 79 20 7c 7c 20 21 73 65 65 6e  kNotify || !seen
349e7 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
349e8 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55  ( db==0 || p->pU
349e9 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21  nlockConnection!
349ea 3d 64 62 20 29 3b 0a 20 20 20 20 20 20 61 73 73  =db );.      ass
349eb 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d  ert( db==0 || p-
349ec 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
349ed 74 69 6f 6e 21 3d 64 62 20 29 3b 0a 20 20 20 20  tion!=db );.    
349ee 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  }.  }.}.#else.# 
349ef 64 65 66 69 6e 65 20 63 68 65 63 6b 4c 69 73 74  define checkList
349f0 50 72 6f 70 65 72 74 69 65 73 28 78 29 0a 23 65  Properties(x).#e
349f1 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  ndif../*.** Remo
349f2 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  ve connection db
349f3 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
349f4 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
349f5 73 74 2e 20 49 66 20 63 6f 6e 6e 65 63 74 69 6f  st. If connectio
349f6 6e 0a 2a 2a 20 64 62 20 69 73 20 6e 6f 74 20 63  n.** db is not c
349f7 75 72 72 65 6e 74 6c 79 20 61 20 70 61 72 74 20  urrently a part 
349f8 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69  of the list, thi
349f9 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
349fa 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
349fb 20 76 6f 69 64 20 72 65 6d 6f 76 65 46 72 6f 6d   void removeFrom
349fc 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69  BlockedList(sqli
349fd 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
349fe 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65  te3 **pp;.  asse
349ff 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20  rtMutexHeld();. 
34a00 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33   for(pp=&sqlite3
34a01 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70  BlockedList; *pp
34a02 3b 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70  ; pp = &(*pp)->p
34a03 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20  NextBlocked){.  
34a04 20 20 69 66 28 20 2a 70 70 3d 3d 64 62 20 29 7b    if( *pp==db ){
34a05 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70  .      *pp = (*p
34a06 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  p)->pNextBlocked
34a07 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
34a08 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
34a09 2a 20 41 64 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  * Add connection
34a0a 20 64 62 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b   db to the block
34a0b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed connections l
34a0c 69 73 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d  ist. It is assum
34a0d 65 64 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73  ed.** that it is
34a0e 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70   not already a p
34a0f 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2e  art of the list.
34a10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
34a11 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74  addToBlockedList
34a12 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
34a13 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 3b 0a 20   sqlite3 **pp;. 
34a14 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64   assertMutexHeld
34a15 28 29 3b 0a 20 20 66 6f 72 28 0a 20 20 20 20 70  ();.  for(.    p
34a16 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p=&sqlite3Blocke
34a17 64 4c 69 73 74 3b 20 0a 20 20 20 20 2a 70 70 20  dList; .    *pp 
34a18 26 26 20 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63  && (*pp)->xUnloc
34a19 6b 4e 6f 74 69 66 79 21 3d 64 62 2d 3e 78 55 6e  kNotify!=db->xUn
34a1a 6c 6f 63 6b 4e 6f 74 69 66 79 3b 20 0a 20 20 20  lockNotify; .   
34a1b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78   pp=&(*pp)->pNex
34a1c 74 42 6c 6f 63 6b 65 64 0a 20 20 29 3b 0a 20 20  tBlocked.  );.  
34a1d 64 62 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  db->pNextBlocked
34a1e 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20   = *pp;.  *pp = 
34a1f 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  db;.}../*.** Obt
34a20 61 69 6e 20 74 68 65 20 53 54 41 54 49 43 5f 4d  ain the STATIC_M
34a21 41 53 54 45 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a  ASTER mutex..*/.
34a22 73 74 61 74 69 63 20 76 6f 69 64 20 65 6e 74 65  static void ente
34a23 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  rMutex(void){.  
34a24 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
34a25 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
34a26 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
34a27 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
34a28 29 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50  ));.  checkListP
34a29 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 7d 0a  roperties(0);.}.
34a2a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 74  ./*.** Release t
34a2b 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52  he STATIC_MASTER
34a2c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   mutex..*/.stati
34a2d 63 20 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65  c void leaveMute
34a2e 78 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72  x(void){.  asser
34a2f 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20  tMutexHeld();.  
34a30 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
34a31 69 65 73 28 30 29 3b 0a 20 20 73 71 6c 69 74 65  ies(0);.  sqlite
34a32 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
34a33 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
34a34 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
34a35 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a  TIC_MASTER));.}.
34a36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
34a37 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
34a38 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   callback..**.**
34a39 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
34a3a 61 66 74 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  after connection
34a3b 20 22 64 62 22 20 68 61 73 20 61 74 74 65 6d 70   "db" has attemp
34a3c 74 65 64 20 73 6f 6d 65 20 6f 70 65 72 61 74 69  ted some operati
34a3d 6f 6e 0a 2a 2a 20 62 75 74 20 68 61 73 20 72 65  on.** but has re
34a3e 63 65 69 76 65 64 20 61 6e 20 53 51 4c 49 54 45  ceived an SQLITE
34a3f 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20 62 65  _LOCKED error be
34a40 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f  cause another co
34a41 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c  nnection.** (cal
34a42 6c 20 69 74 20 70 4f 74 68 65 72 29 20 69 6e 20  l it pOther) in 
34a43 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
34a44 20 77 61 73 20 62 75 73 79 20 75 73 69 6e 67 20   was busy using 
34a45 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 0a  the same shared.
34a46 2a 2a 20 63 61 63 68 65 2e 20 20 70 4f 74 68 65  ** cache.  pOthe
34a47 72 20 69 73 20 66 6f 75 6e 64 20 62 79 20 6c 6f  r is found by lo
34a48 6f 6b 69 6e 67 20 61 74 20 64 62 2d 3e 70 42 6c  oking at db->pBl
34a49 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
34a4a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
34a4b 20 69 73 20 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20   is no blocking 
34a4c 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 20  connection, the 
34a4d 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
34a4e 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c  ked immediately,
34a4f 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20  .** before this 
34a50 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e  routine returns.
34a51 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 74 68 65 72  .**.** If pOther
34a52 20 69 73 20 61 6c 72 65 61 64 79 20 62 6c 6f 63   is already bloc
34a53 6b 65 64 20 6f 6e 20 64 62 2c 20 74 68 65 6e 20  ked on db, then 
34a54 72 65 70 6f 72 74 20 53 51 4c 49 54 45 5f 4c 4f  report SQLITE_LO
34a55 43 4b 45 44 2c 20 74 6f 20 69 6e 64 69 63 61 74  CKED, to indicat
34a56 65 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2e  e.** a deadlock.
34a57 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
34a58 2c 20 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65  , make arrangeme
34a59 6e 74 73 20 74 6f 20 69 6e 76 6f 6b 65 20 78 4e  nts to invoke xN
34a5a 6f 74 69 66 79 20 77 68 65 6e 20 70 4f 74 68 65  otify when pOthe
34a5b 72 20 64 72 6f 70 73 0a 2a 2a 20 69 74 73 20 6c  r drops.** its l
34a5c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ocks..**.** Each
34a5d 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f   call to this ro
34a5e 75 74 69 6e 65 20 6f 76 65 72 72 69 64 65 73 20  utine overrides 
34a5f 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61  any prior callba
34a60 63 6b 73 20 72 65 67 69 73 74 65 72 65 64 0a 2a  cks registered.*
34a61 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 22 64  * on the same "d
34a62 62 22 2e 20 20 49 66 20 78 4e 6f 74 69 66 79 3d  b".  If xNotify=
34a63 3d 30 20 74 68 65 6e 20 61 6e 79 20 70 72 69 6f  =0 then any prio
34a64 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20  r callbacks are 
34a65 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 63  immediately.** c
34a66 61 6e 63 65 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  ancelled..*/.SQL
34a67 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
34a68 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
34a69 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  y(.  sqlite3 *db
34a6a 2c 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69  ,.  void (*xNoti
34a6b 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
34a6c 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
34a6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
34a6e 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69  LITE_OK;..  sqli
34a6f 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34a70 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 65 6e  db->mutex);.  en
34a71 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 69  terMutex();..  i
34a72 66 28 20 78 4e 6f 74 69 66 79 3d 3d 30 20 29 7b  f( xNotify==0 ){
34a73 0a 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42  .    removeFromB
34a74 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
34a75 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43      db->pUnlockC
34a76 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
34a77 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f     db->xUnlockNo
34a78 74 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 64 62  tify = 0;.    db
34a79 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30  ->pUnlockArg = 0
34a7a 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 30 3d  ;.  }else if( 0=
34a7b 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  =db->pBlockingCo
34a7c 6e 6e 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nnection ){.    
34a7d 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 69 6e 67 20  /* The blocking 
34a7e 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20  transaction has 
34a7f 62 65 65 6e 20 63 6f 6e 63 6c 75 64 65 64 2e 20  been concluded. 
34a80 4f 72 20 74 68 65 72 65 20 6e 65 76 65 72 20 77  Or there never w
34a81 61 73 20 61 20 0a 20 20 20 20 2a 2a 20 62 6c 6f  as a .    ** blo
34a82 63 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f  cking transactio
34a83 6e 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  n. In either cas
34a84 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 6e 6f  e, invoke the no
34a85 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 0a 20 20  tify callback.  
34a86 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
34a87 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 78 4e  . .    */.    xN
34a88 6f 74 69 66 79 28 26 70 41 72 67 2c 20 31 29 3b  otify(&pArg, 1);
34a89 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
34a8a 6c 69 74 65 33 20 2a 70 3b 0a 0a 20 20 20 20 66  lite3 *p;..    f
34a8b 6f 72 28 70 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69  or(p=db->pBlocki
34a8c 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 70 20  ngConnection; p 
34a8d 26 26 20 70 21 3d 64 62 3b 20 70 3d 70 2d 3e 70  && p!=db; p=p->p
34a8e 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
34a8f 29 7b 7d 0a 20 20 20 20 69 66 28 20 70 20 29 7b  ){}.    if( p ){
34a90 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
34a91 54 45 5f 4c 4f 43 4b 45 44 3b 20 20 20 20 20 20  TE_LOCKED;      
34a92 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 64 6c          /* Deadl
34a93 6f 63 6b 20 64 65 74 65 63 74 65 64 2e 20 2a 2f  ock detected. */
34a94 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
34a95 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e    db->pUnlockCon
34a96 6e 65 63 74 69 6f 6e 20 3d 20 64 62 2d 3e 70 42  nection = db->pB
34a97 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
34a98 6e 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 55 6e  n;.      db->xUn
34a99 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 78 4e 6f  lockNotify = xNo
34a9a 74 69 66 79 3b 0a 20 20 20 20 20 20 64 62 2d 3e  tify;.      db->
34a9b 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 70 41 72  pUnlockArg = pAr
34a9c 67 3b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 46  g;.      removeF
34a9d 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64  romBlockedList(d
34a9e 62 29 3b 0a 20 20 20 20 20 20 61 64 64 54 6f 42  b);.      addToB
34a9f 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
34aa0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61      }.  }..  lea
34aa1 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73  veMutex();.  ass
34aa2 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
34aa3 46 61 69 6c 65 64 20 29 3b 0a 20 20 73 71 6c 69  Failed );.  sqli
34aa4 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
34aa5 20 28 72 63 3f 22 64 61 74 61 62 61 73 65 20 69   (rc?"database i
34aa6 73 20 64 65 61 64 6c 6f 63 6b 65 64 22 3a 30 29  s deadlocked":0)
34aa7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
34aa8 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
34aa9 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
34aaa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
34aab 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
34aac 65 64 20 77 68 69 6c 65 20 73 74 65 70 70 69 6e  ed while steppin
34aad 67 20 6f 72 20 70 72 65 70 61 72 69 6e 67 20 61  g or preparing a
34aae 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 61   statement .** a
34aaf 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63  ssociated with c
34ab0 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20 54 68  onnection db. Th
34ab1 65 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c  e operation will
34ab2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
34ab3 4f 43 4b 45 44 0a 2a 2a 20 74 6f 20 74 68 65 20  OCKED.** to the 
34ab4 75 73 65 72 20 62 65 63 61 75 73 65 20 69 74 20  user because it 
34ab5 72 65 71 75 69 72 65 73 20 61 20 6c 6f 63 6b 20  requires a lock 
34ab6 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  that will not be
34ab7 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e   available.** un
34ab8 74 69 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  til connection p
34ab9 42 6c 6f 63 6b 65 72 20 63 6f 6e 63 6c 75 64 65  Blocker conclude
34aba 73 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72  s its current tr
34abb 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  ansaction..*/.SQ
34abc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
34abd 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
34abe 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74  ionBlocked(sqlit
34abf 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 20  e3 *db, sqlite3 
34ac0 2a 70 42 6c 6f 63 6b 65 72 29 7b 0a 20 20 65 6e  *pBlocker){.  en
34ac1 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  terMutex();.  if
34ac2 28 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  ( db->pBlockingC
34ac3 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20  onnection==0 && 
34ac4 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  db->pUnlockConne
34ac5 63 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  ction==0 ){.    
34ac6 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74  addToBlockedList
34ac7 28 64 62 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  (db);.  }.  db->
34ac8 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
34ac9 69 6f 6e 20 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a  ion = pBlocker;.
34aca 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a    leaveMutex();.
34acb 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
34acc 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
34acd 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 74 72 61   when.** the tra
34ace 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20  nsaction opened 
34acf 62 79 20 64 61 74 61 62 61 73 65 20 64 62 20 68  by database db h
34ad0 61 73 20 6a 75 73 74 20 66 69 6e 69 73 68 65 64  as just finished
34ad1 2e 20 4c 6f 63 6b 73 20 68 65 6c 64 20 0a 2a 2a  . Locks held .**
34ad2 20 62 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e   by database con
34ad3 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20  nection db have 
34ad4 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a  been released..*
34ad5 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
34ad6 6f 6e 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  on loops through
34ad7 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   each entry in t
34ad8 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
34ad9 63 74 69 6f 6e 73 0a 2a 2a 20 6c 69 73 74 20 61  ctions.** list a
34ada 6e 64 20 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c  nd does the foll
34adb 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  owing:.**.**   1
34adc 29 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  ) If the sqlite3
34add 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  .pBlockingConnec
34ade 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61  tion member of a
34adf 20 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a 2a   list entry is.*
34ae0 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
34ae1 2c 20 74 68 65 6e 20 73 65 74 20 70 42 6c 6f 63  , then set pBloc
34ae2 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30  kingConnection=0
34ae3 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20  ..**.**   2) If 
34ae4 74 68 65 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c  the sqlite3.pUnl
34ae5 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65  ockConnection me
34ae6 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65  mber of a list e
34ae7 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20 20  ntry is.**      
34ae8 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e 20  set to db, then 
34ae9 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6e 66 69  invoke the confi
34aea 67 75 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  gured unlock-not
34aeb 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  ify callback and
34aec 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 70 55 6e  .**      set pUn
34aed 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30  lockConnection=0
34aee 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20  ..**.**   3) If 
34aef 74 68 65 20 74 77 6f 20 73 74 65 70 73 20 61 62  the two steps ab
34af0 6f 76 65 20 6d 65 61 6e 20 74 68 61 74 20 70 42  ove mean that pB
34af1 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
34af2 6e 3d 3d 30 20 61 6e 64 0a 2a 2a 20 20 20 20 20  n==0 and.**     
34af3 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69   pUnlockConnecti
34af4 6f 6e 3d 3d 30 2c 20 72 65 6d 6f 76 65 20 74 68  on==0, remove th
34af5 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
34af6 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
34af7 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 6c 69 73  ions.**      lis
34af8 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
34af9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
34afa 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63  3ConnectionUnloc
34afb 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ked(sqlite3 *db)
34afc 7b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f  {.  void (*xUnlo
34afd 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  ckNotify)(void *
34afe 2a 2c 20 69 6e 74 29 20 3d 20 30 3b 20 2f 2a 20  *, int) = 0; /* 
34aff 55 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 62  Unlock-notify cb
34b00 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20   to invoke */.  
34b01 69 6e 74 20 6e 41 72 67 20 3d 20 30 3b 20 20 20  int nArg = 0;   
34b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b03 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
34b04 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
34b05 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c   aArg[] */.  sql
34b06 69 74 65 33 20 2a 2a 70 70 3b 20 20 20 20 20 20  ite3 **pp;      
34b07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b08 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
34b09 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
34b0a 76 6f 69 64 20 2a 2a 61 41 72 67 3b 20 20 20 20  void **aArg;    
34b0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
34b0c 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 75  guments to the u
34b0d 6e 6c 6f 63 6b 20 63 61 6c 6c 62 61 63 6b 20 2a  nlock callback *
34b0e 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 44 79 6e 20  /.  void **aDyn 
34b0f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
34b10 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * Dynamically al
34b11 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f  located space fo
34b12 72 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f  r aArg[] */.  vo
34b13 69 64 20 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b  id *aStatic[16];
34b14 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72           /* Star
34b15 74 65 72 20 73 70 61 63 65 20 66 6f 72 20 61 41  ter space for aA
34b16 72 67 5b 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63  rg[].  No malloc
34b17 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a 20 20   required */..  
34b18 61 41 72 67 20 3d 20 61 53 74 61 74 69 63 3b 0a  aArg = aStatic;.
34b19 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b 20    enterMutex(); 
34b1a 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 74 65 72          /* Enter
34b1b 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
34b1c 75 74 65 78 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  utex */..  /* Th
34b1d 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63  is loop runs onc
34b1e 65 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79  e for each entry
34b1f 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65 64 2d   in the blocked-
34b20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
34b21 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 70 3d 26 73  . */.  for(pp=&s
34b22 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
34b23 74 3b 20 2a 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70  t; *pp; /* no-op
34b24 20 2a 2f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   */ ){.    sqlit
34b25 65 33 20 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20 20  e3 *p = *pp;..  
34b26 20 20 2f 2a 20 53 74 65 70 20 31 2e 20 2a 2f 0a    /* Step 1. */.
34b27 20 20 20 20 69 66 28 20 70 2d 3e 70 42 6c 6f 63      if( p->pBloc
34b28 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  kingConnection==
34b29 64 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  db ){.      p->p
34b2a 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
34b2b 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  on = 0;.    }.. 
34b2c 20 20 20 2f 2a 20 53 74 65 70 20 32 2e 20 2a 2f     /* Step 2. */
34b2d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 55 6e 6c  .    if( p->pUnl
34b2e 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64  ockConnection==d
34b2f 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  b ){.      asser
34b30 74 28 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  t( p->xUnlockNot
34b31 69 66 79 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ify );.      if(
34b32 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66   p->xUnlockNotif
34b33 79 21 3d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  y!=xUnlockNotify
34b34 20 26 26 20 6e 41 72 67 21 3d 30 20 29 7b 0a 20   && nArg!=0 ){. 
34b35 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f         xUnlockNo
34b36 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29  tify(aArg, nArg)
34b37 3b 0a 20 20 20 20 20 20 20 20 6e 41 72 67 20 3d  ;.        nArg =
34b38 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   0;.      }..   
34b39 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
34b3a 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
34b3b 20 20 20 20 20 61 73 73 65 72 74 28 20 61 41 72       assert( aAr
34b3c 67 3d 3d 61 44 79 6e 20 7c 7c 20 28 61 44 79 6e  g==aDyn || (aDyn
34b3d 3d 3d 30 20 26 26 20 61 41 72 67 3d 3d 61 53 74  ==0 && aArg==aSt
34b3e 61 74 69 63 29 20 29 3b 0a 20 20 20 20 20 20 61  atic) );.      a
34b3f 73 73 65 72 74 28 20 6e 41 72 67 3c 3d 28 69 6e  ssert( nArg<=(in
34b40 74 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61  t)ArraySize(aSta
34b41 74 69 63 29 20 7c 7c 20 61 41 72 67 3d 3d 61 44  tic) || aArg==aD
34b42 79 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yn );.      if( 
34b43 28 21 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d  (!aDyn && nArg==
34b44 28 69 6e 74 29 41 72 72 61 79 53 69 7a 65 28 61  (int)ArraySize(a
34b45 53 74 61 74 69 63 29 29 0a 20 20 20 20 20 20 20  Static)).       
34b46 7c 7c 20 28 61 44 79 6e 20 26 26 20 6e 41 72 67  || (aDyn && nArg
34b47 3d 3d 28 69 6e 74 29 28 73 71 6c 69 74 65 33 44  ==(int)(sqlite3D
34b48 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
34b49 61 44 79 6e 29 2f 73 69 7a 65 6f 66 28 76 6f 69  aDyn)/sizeof(voi
34b4a 64 2a 29 29 29 0a 20 20 20 20 20 20 29 7b 0a 20  d*))).      ){. 
34b4b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 41         /* The aA
34b4c 72 67 5b 5d 20 61 72 72 61 79 20 6e 65 65 64 73  rg[] array needs
34b4d 20 74 6f 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 20   to grow. */.   
34b4e 20 20 20 20 20 76 6f 69 64 20 2a 2a 70 4e 65 77       void **pNew
34b4f 20 3d 20 28 76 6f 69 64 20 2a 2a 29 73 71 6c 69   = (void **)sqli
34b50 74 65 33 4d 61 6c 6c 6f 63 28 6e 41 72 67 2a 73  te3Malloc(nArg*s
34b51 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 2a 32 29  izeof(void *)*2)
34b52 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  ;.        if( pN
34b53 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ew ){.          
34b54 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 61 41 72  memcpy(pNew, aAr
34b55 67 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76  g, nArg*sizeof(v
34b56 6f 69 64 20 2a 29 29 3b 0a 20 20 20 20 20 20 20  oid *));.       
34b57 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
34b58 61 44 79 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  aDyn);.         
34b59 20 61 44 79 6e 20 3d 20 61 41 72 67 20 3d 20 70   aDyn = aArg = p
34b5a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  New;.        }el
34b5b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
34b5c 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65   This occurs whe
34b5d 6e 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 63  n the array of c
34b5e 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 20  ontext pointers 
34b5f 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20  that need to.   
34b60 20 20 20 20 20 20 20 2a 2a 20 62 65 20 70 61 73         ** be pas
34b61 73 65 64 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63  sed to the unloc
34b62 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
34b63 6b 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  k is larger than
34b64 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
34b65 2a 20 61 53 74 61 74 69 63 5b 5d 20 61 72 72 61  * aStatic[] arra
34b66 79 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74  y allocated on t
34b67 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65  he stack and the
34b68 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 20 20 20   attempt to .   
34b69 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61         ** alloca
34b6a 74 65 20 61 20 6c 61 72 67 65 72 20 61 72 72 61  te a larger arra
34b6b 79 20 66 72 6f 6d 20 74 68 65 20 68 65 61 70 20  y from the heap 
34b6c 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20 20 20  has failed..    
34b6d 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
34b6e 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20     ** This is a 
34b6f 64 69 66 66 69 63 75 6c 74 20 73 69 74 75 61 74  difficult situat
34b70 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 2e 20 52  ion to handle. R
34b71 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
34b72 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  r.          ** c
34b73 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
34b74 72 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e  r is insufficien
34b75 74 2c 20 61 73 20 65 76 65 6e 20 69 66 20 61 6e  t, as even if an
34b76 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 20 20   error code.    
34b77 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65 74 75        ** is retu
34b78 72 6e 65 64 20 74 68 65 20 74 72 61 6e 73 61 63  rned the transac
34b79 74 69 6f 6e 20 6f 6e 20 63 6f 6e 6e 65 63 74 69  tion on connecti
34b7a 6f 6e 20 64 62 20 77 69 6c 6c 20 73 74 69 6c 6c  on db will still
34b7b 20 62 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   be.          **
34b7c 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 65 20   closed and the 
34b7d 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
34b7e 6c 6c 62 61 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b  llbacks on block
34b7f 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20  ed connections. 
34b80 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c           ** will
34b81 20 67 6f 20 75 6e 69 73 73 75 65 64 2e 20 54 68   go unissued. Th
34b82 69 73 20 6d 69 67 68 74 20 63 61 75 73 65 20 74  is might cause t
34b83 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  he application t
34b84 6f 20 77 61 69 74 0a 20 20 20 20 20 20 20 20 20  o wait.         
34b85 20 2a 2a 20 69 6e 64 65 66 69 6e 69 74 65 6c 79   ** indefinitely
34b86 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
34b87 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 74  otify callback t
34b88 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 0a  hat will never .
34b89 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 72 72            ** arr
34b8a 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ive..          *
34b8b 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49  *.          ** I
34b8c 6e 73 74 65 61 64 2c 20 69 6e 76 6f 6b 65 20 74  nstead, invoke t
34b8d 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  he unlock-notify
34b8e 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74   callback with t
34b8f 68 65 20 63 6f 6e 74 65 78 74 0a 20 20 20 20 20  he context.     
34b90 20 20 20 20 20 2a 2a 20 61 72 72 61 79 20 61 6c       ** array al
34b91 72 65 61 64 79 20 61 63 63 75 6d 75 6c 61 74 65  ready accumulate
34b92 64 2e 20 57 65 20 63 61 6e 20 74 68 65 6e 20 63  d. We can then c
34b93 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20 61  lear the array a
34b94 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nd.          ** 
34b95 62 65 67 69 6e 20 61 63 63 75 6d 75 6c 61 74 69  begin accumulati
34b96 6e 67 20 61 6e 79 20 66 75 72 74 68 65 72 20 63  ng any further c
34b97 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 20  ontext pointers 
34b98 77 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20 20  without .       
34b99 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 20     ** requiring 
34b9a 61 6e 79 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f  any dynamic allo
34b9b 63 61 74 69 6f 6e 2e 20 54 68 69 73 20 69 73 20  cation. This is 
34b9c 73 75 62 2d 6f 70 74 69 6d 61 6c 20 62 65 63 61  sub-optimal beca
34b9d 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  use.          **
34b9e 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69   it means that i
34b9f 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 20 63 61  nstead of one ca
34ba0 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 6c 61  llback with a la
34ba1 72 67 65 20 61 72 72 61 79 20 6f 66 0a 20 20 20  rge array of.   
34ba2 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 65 78         ** contex
34ba3 74 20 70 6f 69 6e 74 65 72 73 20 74 68 65 20 61  t pointers the a
34ba4 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 6c 6c 20  pplication will 
34ba5 72 65 63 65 69 76 65 20 74 77 6f 20 6f 72 20 6d  receive two or m
34ba6 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ore.          **
34ba7 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 74 68 20   callbacks with 
34ba8 73 6d 61 6c 6c 65 72 20 61 72 72 61 79 73 20 6f  smaller arrays o
34ba9 66 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  f context pointe
34baa 72 73 2c 20 77 68 69 63 68 20 77 69 6c 6c 0a 20  rs, which will. 
34bab 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 64 75           ** redu
34bac 63 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ce the applicati
34bad 6f 6e 73 20 61 62 69 6c 69 74 79 20 74 6f 20 70  ons ability to p
34bae 72 69 6f 72 69 74 69 7a 65 20 6d 75 6c 74 69 70  rioritize multip
34baf 6c 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  le .          **
34bb0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 42 75   connections. Bu
34bb1 74 20 69 74 20 69 73 20 74 68 65 20 62 65 73 74  t it is the best
34bb2 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 6f 6e   that can be don
34bb3 65 20 75 6e 64 65 72 20 74 68 65 0a 20 20 20 20  e under the.    
34bb4 20 20 20 20 20 20 2a 2a 20 63 69 72 63 75 6d 73        ** circums
34bb5 74 61 6e 63 65 73 2e 0a 20 20 20 20 20 20 20 20  tances..        
34bb6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 78    */.          x
34bb7 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72  UnlockNotify(aAr
34bb8 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20  g, nArg);.      
34bb9 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20      nArg = 0;.  
34bba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
34bbb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64        sqlite3End
34bbc 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
34bbd 0a 20 20 20 20 20 20 61 41 72 67 5b 6e 41 72 67  .      aArg[nArg
34bbe 2b 2b 5d 20 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b  ++] = p->pUnlock
34bbf 41 72 67 3b 0a 20 20 20 20 20 20 78 55 6e 6c 6f  Arg;.      xUnlo
34bc0 63 6b 4e 6f 74 69 66 79 20 3d 20 70 2d 3e 78 55  ckNotify = p->xU
34bc1 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3b 0a 20 20 20  nlockNotify;.   
34bc2 20 20 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e     p->pUnlockCon
34bc3 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20  nection = 0;.   
34bc4 20 20 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74     p->xUnlockNot
34bc5 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ify = 0;.      p
34bc6 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30  ->pUnlockArg = 0
34bc7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
34bc8 53 74 65 70 20 33 2e 20 2a 2f 0a 20 20 20 20 69  Step 3. */.    i
34bc9 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  f( p->pBlockingC
34bca 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20  onnection==0 && 
34bcb 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
34bcc 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tion==0 ){.     
34bcd 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65   /* Remove conne
34bce 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74 68 65  ction p from the
34bcf 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
34bd0 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 20  ions list. */.  
34bd1 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65      *pp = p->pNe
34bd2 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 20  xtBlocked;.     
34bd3 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64   p->pNextBlocked
34bd4 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
34bd5 0a 20 20 20 20 20 20 70 70 20 3d 20 26 70 2d 3e  .      pp = &p->
34bd6 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20  pNextBlocked;.  
34bd7 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e    }.  }..  if( n
34bd8 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 78 55  Arg!=0 ){.    xU
34bd9 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72 67  nlockNotify(aArg
34bda 2c 20 6e 41 72 67 29 3b 0a 20 20 7d 0a 20 20 73  , nArg);.  }.  s
34bdb 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e  qlite3_free(aDyn
34bdc 29 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28  );.  leaveMutex(
34bdd 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  );         /* Le
34bde 61 76 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  ave STATIC_MASTE
34bdf 52 20 6d 75 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a  R mutex */.}../*
34be0 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c  .** This is call
34be1 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ed when the data
34be2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
34be3 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
34be4 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69  ument is .** bei
34be5 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68 65 20 63  ng closed. The c
34be6 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 72 65 6d  onnection is rem
34be7 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 62 6c  oved from the bl
34be8 6f 63 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53  ocked list..*/.S
34be9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
34bea 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  id sqlite3Connec
34beb 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74  tionClosed(sqlit
34bec 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74  e3 *db){.  sqlit
34bed 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
34bee 63 6b 65 64 28 64 62 29 3b 0a 20 20 65 6e 74 65  cked(db);.  ente
34bef 72 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 6d 6f  rMutex();.  remo
34bf0 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73  veFromBlockedLis
34bf1 74 28 64 62 29 3b 0a 20 20 63 68 65 63 6b 4c 69  t(db);.  checkLi
34bf2 73 74 50 72 6f 70 65 72 74 69 65 73 28 64 62 29  stProperties(db)
34bf3 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  ;.  leaveMutex()
34bf4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
34bf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
34bf6 6f 66 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a  of notify.c ****
34bf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34bf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
34bfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
34bfb 6e 20 66 69 6c 65 20 66 74 73 33 2e 63 20 2a 2a  n file fts3.c **
34bfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34bfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
34bff 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a  * 2006 Oct 10.**
34c00 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
34c01 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
34c02 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
34c03 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
34c04 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
34c05 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
34c06 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
34c07 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
34c08 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
34c09 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
34c0a 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
34c0b 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
34c0c 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
34c0d 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
34c0e 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
34c0f 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
34c10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
34c11 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
34c12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c16 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  *.**.** This is 
34c17 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65  an SQLite module
34c18 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75   implementing fu
34c19 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a  ll-text search..
34c1a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
34c1b 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
34c1c 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
34c1d 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
34c1e 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
34c1f 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
34c20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
34c21 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
34c22 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
34c23 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
34c24 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
34c25 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
34c26 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
34c27 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
34c28 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
34c29 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
34c2a 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
34c2b 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
34c2c 29 2e 0a 2a 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73  )..*/../* TODO(s
34c2d 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 65  hess) Consider e
34c2e 78 70 6f 72 74 69 6e 67 20 74 68 69 73 20 63 6f  xporting this co
34c2f 6d 6d 65 6e 74 20 74 6f 20 61 6e 20 48 54 4d 4c  mment to an HTML
34c30 20 66 69 6c 65 20 6f 72 20 74 68 65 0a 2a 2a 20   file or the.** 
34c31 77 69 6b 69 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20  wiki..*/./* The 
34c32 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20  full-text index 
34c33 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  is stored in a s
34c34 65 72 69 65 73 20 6f 66 20 62 2b 74 72 65 65 20  eries of b+tree 
34c35 28 2d 6c 69 6b 65 29 0a 2a 2a 20 73 74 72 75 63  (-like).** struc
34c36 74 75 72 65 73 20 63 61 6c 6c 65 64 20 73 65 67  tures called seg
34c37 6d 65 6e 74 73 20 77 68 69 63 68 20 6d 61 70 20  ments which map 
34c38 74 65 72 6d 73 20 74 6f 20 64 6f 63 6c 69 73 74  terms to doclist
34c39 73 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 75 63  s.  The.** struc
34c3a 74 75 72 65 73 20 61 72 65 20 6c 69 6b 65 20 62  tures are like b
34c3b 2b 74 72 65 65 73 20 69 6e 20 6c 61 79 6f 75 74  +trees in layout
34c3c 2c 20 62 75 74 20 61 72 65 20 63 6f 6e 73 74 72  , but are constr
34c3d 75 63 74 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  ucted from the.*
34c3e 2a 20 62 6f 74 74 6f 6d 20 75 70 20 69 6e 20 6f  * bottom up in o
34c3f 70 74 69 6d 61 6c 20 66 61 73 68 69 6f 6e 20 61  ptimal fashion a
34c40 6e 64 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74  nd are not updat
34c41 61 62 6c 65 2e 20 20 53 69 6e 63 65 20 74 72 65  able.  Since tre
34c42 65 73 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20  es.** are built 
34c43 66 72 6f 6d 20 74 68 65 20 62 6f 74 74 6f 6d 20  from the bottom 
34c44 75 70 2c 20 74 68 69 6e 67 73 20 77 69 6c 6c 20  up, things will 
34c45 62 65 20 64 65 73 63 72 69 62 65 64 20 66 72 6f  be described fro
34c46 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20  m the.** bottom 
34c47 75 70 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56  up..**.**.**** V
34c48 61 72 69 6e 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54  arints ****.** T
34c49 68 65 20 62 61 73 69 63 20 75 6e 69 74 20 6f 66  he basic unit of
34c4a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 76   encoding is a v
34c4b 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
34c4c 6e 74 65 67 65 72 20 63 61 6c 6c 65 64 20 61 0a  nteger called a.
34c4d 2a 2a 20 76 61 72 69 6e 74 2e 20 20 57 65 20 65  ** varint.  We e
34c4e 6e 63 6f 64 65 20 76 61 72 69 61 62 6c 65 2d 6c  ncode variable-l
34c4f 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20 69  ength integers i
34c50 6e 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20  n little-endian 
34c51 6f 72 64 65 72 0a 2a 2a 20 75 73 69 6e 67 20 73  order.** using s
34c52 65 76 65 6e 20 62 69 74 73 20 2a 20 70 65 72 20  even bits * per 
34c53 62 79 74 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  byte as follows:
34c54 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20  .**.** KEY:.**  
34c55 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78 78         A = 0xxxx
34c56 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66  xxx    7 bits of
34c57 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c   data and one fl
34c58 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20  ag bit.**       
34c59 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20    B = 1xxxxxxx  
34c5a 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61    7 bits of data
34c5b 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69   and one flag bi
34c5c 74 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20  t.**.**  7 bits 
34c5d 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d  - A.** 14 bits -
34c5e 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d   BA.** 21 bits -
34c5f 20 42 42 41 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f   BBA.** and so o
34c60 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  n..**.** This is
34c61 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 68 6f   identical to ho
34c62 77 20 73 71 6c 69 74 65 20 65 6e 63 6f 64 65 73  w sqlite encodes
34c63 20 76 61 72 69 6e 74 73 20 28 73 65 65 20 75 74   varints (see ut
34c64 69 6c 2e 63 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a  il.c)..**.**.***
34c65 2a 20 44 6f 63 75 6d 65 6e 74 20 6c 69 73 74 73  * Document lists
34c66 20 2a 2a 2a 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69   ****.** A docli
34c67 73 74 20 28 64 6f 63 75 6d 65 6e 74 20 6c 69 73  st (document lis
34c68 74 29 20 68 6f 6c 64 73 20 61 20 64 6f 63 69 64  t) holds a docid
34c69 2d 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 20  -sorted list of 
34c6a 68 69 74 73 20 66 6f 72 20 61 0a 2a 2a 20 67 69  hits for a.** gi
34c6b 76 65 6e 20 74 65 72 6d 2e 20 20 44 6f 63 6c 69  ven term.  Docli
34c6c 73 74 73 20 68 6f 6c 64 20 64 6f 63 69 64 73 2c  sts hold docids,
34c6d 20 61 6e 64 20 63 61 6e 20 6f 70 74 69 6f 6e 61   and can optiona
34c6e 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 0a 2a 2a  lly associate.**
34c6f 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 73   token positions
34c70 20 61 6e 64 20 6f 66 66 73 65 74 73 20 77 69 74   and offsets wit
34c71 68 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20  h docids..**.** 
34c72 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  A DL_POSITIONS_O
34c73 46 46 53 45 54 53 20 64 6f 63 6c 69 73 74 20 69  FFSETS doclist i
34c74 73 20 73 74 6f 72 65 64 20 6c 69 6b 65 20 74 68  s stored like th
34c75 69 73 3a 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79 20  is:.**.** array 
34c76 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 64 6f  {.**   varint do
34c77 63 69 64 3b 0a 2a 2a 20 20 20 61 72 72 61 79 20  cid;.**   array 
34c78 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
34c79 20 28 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20   (position list 
34c7a 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a  for column 0).**
34c7b 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f 73 69       varint posi
34c7c 74 69 6f 6e 3b 20 20 20 20 20 28 64 65 6c 74 61  tion;     (delta
34c7d 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 70   from previous p
34c7e 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50 4f 53  osition plus POS
34c7f 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20 76 61  _BASE).**     va
34c80 72 69 6e 74 20 73 74 61 72 74 4f 66 66 73 65 74  rint startOffset
34c81 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70  ;  (delta from p
34c82 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f 66 66  revious startOff
34c83 73 65 74 29 0a 2a 2a 20 20 20 20 20 76 61 72 69  set).**     vari
34c84 6e 74 20 65 6e 64 4f 66 66 73 65 74 3b 20 20 20  nt endOffset;   
34c85 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73 74 61   (delta from sta
34c86 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 7d  rtOffset).**   }
34c87 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a  .**   array {.**
34c88 20 20 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f       varint POS_
34c89 43 4f 4c 55 4d 4e 3b 20 20 20 28 6d 61 72 6b 73  COLUMN;   (marks
34c8a 20 73 74 61 72 74 20 6f 66 20 70 6f 73 69 74 69   start of positi
34c8b 6f 6e 20 6c 69 73 74 20 66 6f 72 20 6e 65 77 20  on list for new 
34c8c 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 76  column).**     v
34c8d 61 72 69 6e 74 20 63 6f 6c 75 6d 6e 3b 20 20 20  arint column;   
34c8e 20 20 20 20 28 69 6e 64 65 78 20 6f 66 20 6e 65      (index of ne
34c8f 77 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20  w column).**    
34c90 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20   array {.**     
34c91 20 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f    varint positio
34c92 6e 3b 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d  n;   (delta from
34c93 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69   previous positi
34c94 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45  on plus POS_BASE
34c95 29 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e  ).**       varin
34c96 74 20 73 74 61 72 74 4f 66 66 73 65 74 3b 28 64  t startOffset;(d
34c97 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f  elta from previo
34c98 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a  us startOffset).
34c99 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20  **       varint 
34c9a 65 6e 64 4f 66 66 73 65 74 3b 20 20 28 64 65 6c  endOffset;  (del
34c9b 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66  ta from startOff
34c9c 73 65 74 29 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a  set).**     }.**
34c9d 20 20 20 7d 0a 2a 2a 20 20 20 76 61 72 69 6e 74     }.**   varint
34c9e 20 50 4f 53 5f 45 4e 44 3b 20 20 20 20 20 20 20   POS_END;       
34c9f 20 28 6d 61 72 6b 73 20 65 6e 64 20 6f 66 20 70   (marks end of p
34ca0 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 68 69  ositions for thi
34ca1 73 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d  s document..** }
34ca2 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72  .**.** Here, arr
34ca3 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a  ay { X } means z
34ca4 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75  ero or more occu
34ca5 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64  rrences of X, ad
34ca6 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d  jacent in.** mem
34ca7 6f 72 79 2e 20 20 41 20 22 70 6f 73 69 74 69 6f  ory.  A "positio
34ca8 6e 22 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f  n" is an index o
34ca9 66 20 61 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  f a token in the
34caa 20 74 6f 6b 65 6e 20 73 74 72 65 61 6d 0a 2a 2a   token stream.**
34cab 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
34cac 65 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 77 68 69  e tokenizer, whi
34cad 6c 65 20 61 6e 20 22 6f 66 66 73 65 74 22 20 69  le an "offset" i
34cae 73 20 61 20 62 79 74 65 20 6f 66 66 73 65 74 2c  s a byte offset,
34caf 0a 2a 2a 20 62 6f 74 68 20 62 61 73 65 64 20 61  .** both based a
34cb0 74 20 30 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  t 0.  Note that 
34cb1 50 4f 53 5f 45 4e 44 20 61 6e 64 20 50 4f 53 5f  POS_END and POS_
34cb2 43 4f 4c 55 4d 4e 20 6f 63 63 75 72 20 69 6e 20  COLUMN occur in 
34cb3 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6c 6f 67 69  the.** same logi
34cb4 63 61 6c 20 70 6c 61 63 65 20 61 73 20 74 68 65  cal place as the
34cb5 20 70 6f 73 69 74 69 6f 6e 20 65 6c 65 6d 65 6e   position elemen
34cb6 74 2c 20 61 6e 64 20 61 63 74 20 61 73 20 73 65  t, and act as se
34cb7 6e 74 69 6e 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e  ntinals.** endin
34cb8 67 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  g a position lis
34cb9 74 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41  t array..**.** A
34cba 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 64 6f   DL_POSITIONS do
34cbb 63 6c 69 73 74 20 6f 6d 69 74 73 20 74 68 65 20  clist omits the 
34cbc 73 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64 20  startOffset and 
34cbd 65 6e 64 4f 66 66 73 65 74 0a 2a 2a 20 69 6e 66  endOffset.** inf
34cbe 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 20 44 4c 5f  ormation.  A DL_
34cbf 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 6f  DOCIDS doclist o
34cc0 6d 69 74 73 20 62 6f 74 68 20 74 68 65 20 70 6f  mits both the po
34cc1 73 69 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66  sition and.** of
34cc2 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  fset information
34cc3 2c 20 62 65 63 6f 6d 69 6e 67 20 61 6e 20 61 72  , becoming an ar
34cc4 72 61 79 20 6f 66 20 76 61 72 69 6e 74 2d 65 6e  ray of varint-en
34cc5 63 6f 64 65 64 20 64 6f 63 69 64 73 2e 0a 2a 2a  coded docids..**
34cc6 0a 2a 2a 20 4f 6e 2d 64 69 73 6b 20 64 61 74 61  .** On-disk data
34cc7 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74 79   is stored as ty
34cc8 70 65 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 73  pe DL_DEFAULT, s
34cc9 6f 20 77 65 20 64 6f 6e 27 74 20 73 65 72 69 61  o we don't seria
34cca 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 74 79 70 65  lize.** the type
34ccb 2e 20 20 44 75 65 20 74 6f 20 68 6f 77 20 64 65  .  Due to how de
34ccc 6c 65 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d  letion is implem
34ccd 65 6e 74 65 64 20 69 6e 20 74 68 65 20 73 65 67  ented in the seg
34cce 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 73 79 73  mentation.** sys
34ccf 74 65 6d 2c 20 6f 6e 2d 64 69 73 6b 20 64 6f 63  tem, on-disk doc
34cd0 6c 69 73 74 73 20 4d 55 53 54 20 73 74 6f 72 65  lists MUST store
34cd1 20 61 74 20 6c 65 61 73 74 20 70 6f 73 69 74 69   at least positi
34cd2 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  ons..**.**.**** 
34cd3 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e 6f 64  Segment leaf nod
34cd4 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65  es ****.** Segme
34cd5 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 73 74  nt leaf nodes st
34cd6 6f 72 65 20 74 65 72 6d 73 20 61 6e 64 20 64 6f  ore terms and do
34cd7 63 6c 69 73 74 73 2c 20 6f 72 64 65 72 65 64 20  clists, ordered 
34cd8 62 79 20 74 65 72 6d 2e 20 20 4c 65 61 66 0a 2a  by term.  Leaf.*
34cd9 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74  * nodes are writ
34cda 74 65 6e 20 75 73 69 6e 67 20 4c 65 61 66 57 72  ten using LeafWr
34cdb 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75  iter, and read u
34cdc 73 69 6e 67 20 4c 65 61 66 52 65 61 64 65 72 20  sing LeafReader 
34cdd 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74  (to.** iterate t
34cde 68 72 6f 75 67 68 20 61 20 73 69 6e 67 6c 65 20  hrough a single 
34cdf 6c 65 61 66 20 6e 6f 64 65 27 73 20 64 61 74 61  leaf node's data
34ce0 29 20 61 6e 64 20 4c 65 61 76 65 73 52 65 61 64  ) and LeavesRead
34ce1 65 72 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74  er (to.** iterat
34ce2 65 20 74 68 72 6f 75 67 68 20 61 20 73 65 67 6d  e through a segm
34ce3 65 6e 74 27 73 20 65 6e 74 69 72 65 20 6c 65 61  ent's entire lea
34ce4 66 20 6c 61 79 65 72 29 2e 20 20 4c 65 61 66 20  f layer).  Leaf 
34ce5 6e 6f 64 65 73 20 68 61 76 65 0a 2a 2a 20 74 68  nodes have.** th
34ce6 65 20 66 6f 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20  e format:.**.** 
34ce7 76 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20  varint iHeight; 
34ce8 20 20 20 20 20 20 20 20 20 20 20 20 28 68 65 69              (hei
34ce9 67 68 74 20 66 72 6f 6d 20 6c 65 61 66 20 6c 65  ght from leaf le
34cea 76 65 6c 2c 20 61 6c 77 61 79 73 20 30 29 0a 2a  vel, always 0).*
34ceb 2a 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20  * varint nTerm; 
34cec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6c                (l
34ced 65 6e 67 74 68 20 6f 66 20 66 69 72 73 74 20 74  ength of first t
34cee 65 72 6d 29 0a 2a 2a 20 63 68 61 72 20 70 54 65  erm).** char pTe
34cef 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20  rm[nTerm];      
34cf0 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20      (content of 
34cf1 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 76  first term).** v
34cf2 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20  arint nDoclist; 
34cf3 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67             (leng
34cf4 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73 73  th of term's ass
34cf5 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74 29  ociated doclist)
34cf6 0a 2a 2a 20 63 68 61 72 20 70 44 6f 63 6c 69 73  .** char pDoclis
34cf7 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20 20 20 20  t[nDoclist];    
34cf8 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63 6c  (content of docl
34cf9 69 73 74 29 0a 2a 2a 20 61 72 72 61 79 20 7b 0a  ist).** array {.
34cfa 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
34cfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
34cfc 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
34cfd 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
34cfe 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 50 72  .**   varint nPr
34cff 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
34d00 28 6c 65 6e 67 74 68 20 6f 66 20 70 72 65 66 69  (length of prefi
34d01 78 20 73 68 61 72 65 64 20 77 69 74 68 20 70 72  x shared with pr
34d02 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20  evious term).** 
34d03 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66 69 78    varint nSuffix
34d04 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  ;           (len
34d05 67 74 68 20 6f 66 20 75 6e 73 68 61 72 65 64 20  gth of unshared 
34d06 73 75 66 66 69 78 29 0a 2a 2a 20 20 20 63 68 61  suffix).**   cha
34d07 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53  r pTermSuffix[nS
34d08 75 66 66 69 78 5d 3b 28 75 6e 73 68 61 72 65 64  uffix];(unshared
34d09 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20   suffix of next 
34d0a 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e  term).**   varin
34d0b 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20  t nDoclist;     
34d0c 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
34d0d 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
34d0e 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 20 20  d doclist).**   
34d0f 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44  char pDoclist[nD
34d10 6f 63 6c 69 73 74 5d 3b 20 20 28 63 6f 6e 74 65  oclist];  (conte
34d11 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
34d12 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20  * }.**.** Here, 
34d13 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e  array { X } mean
34d14 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f  s zero or more o
34d15 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c  ccurrences of X,
34d16 20 61 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20   adjacent in.** 
34d17 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65  memory..**.** Le
34d18 61 66 20 6e 6f 64 65 73 20 61 72 65 20 62 72 6f  af nodes are bro
34d19 6b 65 6e 20 69 6e 74 6f 20 62 6c 6f 63 6b 73 20  ken into blocks 
34d1a 77 68 69 63 68 20 61 72 65 20 73 74 6f 72 65 64  which are stored
34d1b 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 69 6e   contiguously in
34d1c 0a 2a 2a 20 74 68 65 20 25 5f 73 65 67 6d 65 6e  .** the %_segmen
34d1d 74 73 20 74 61 62 6c 65 20 69 6e 20 73 6f 72 74  ts table in sort
34d1e 65 64 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ed order.  This 
34d1f 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20  means that when 
34d20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 61 20  the end.** of a 
34d21 6e 6f 64 65 20 69 73 20 72 65 61 63 68 65 64 2c  node is reached,
34d22 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 69   the next term i
34d23 73 20 69 6e 20 74 68 65 20 6e 6f 64 65 20 77 69  s in the node wi
34d24 74 68 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 67  th the next.** g
34d25 72 65 61 74 65 72 20 6e 6f 64 65 20 69 64 2e 0a  reater node id..
34d26 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69  **.** New data i
34d27 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e  s spilled to a n
34d28 65 77 20 6c 65 61 66 20 6e 6f 64 65 20 77 68 65  ew leaf node whe
34d29 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  n the current no
34d2a 64 65 0a 2a 2a 20 65 78 63 65 65 64 73 20 4c 45  de.** exceeds LE
34d2b 41 46 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65  AF_MAX bytes (de
34d2c 66 61 75 6c 74 20 32 30 34 38 29 2e 20 20 4e 65  fault 2048).  Ne
34d2d 77 20 64 61 74 61 20 77 68 69 63 68 20 69 74 73  w data which its
34d2e 65 6c 66 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72  elf is.** larger
34d2f 20 74 68 61 6e 20 53 54 41 4e 44 41 4c 4f 4e 45   than STANDALONE
34d30 5f 4d 49 4e 20 28 64 65 66 61 75 6c 74 20 31 30  _MIN (default 10
34d31 32 34 29 20 69 73 20 70 6c 61 63 65 64 20 69 6e  24) is placed in
34d32 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a   a standalone.**
34d33 20 6e 6f 64 65 20 28 61 20 6c 65 61 66 20 6e 6f   node (a leaf no
34d34 64 65 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  de with a single
34d35 20 74 65 72 6d 20 61 6e 64 20 64 6f 63 6c 69 73   term and doclis
34d36 74 29 2e 20 20 54 68 65 20 67 6f 61 6c 20 6f 66  t).  The goal of
34d37 0a 2a 2a 20 74 68 65 73 65 20 73 65 74 74 69 6e  .** these settin
34d38 67 73 20 69 73 20 74 6f 20 70 61 63 6b 20 74 6f  gs is to pack to
34d39 67 65 74 68 65 72 20 67 72 6f 75 70 73 20 6f 66  gether groups of
34d3a 20 73 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20   small doclists 
34d3b 77 68 69 6c 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20  while.** making 
34d3c 69 74 20 65 66 66 69 63 69 65 6e 74 20 74 6f 20  it efficient to 
34d3d 64 69 72 65 63 74 6c 79 20 61 63 63 65 73 73 20  directly access 
34d3e 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73 2e 20  large doclists. 
34d3f 20 54 68 65 0a 2a 2a 20 61 73 73 75 6d 70 74 69   The.** assumpti
34d40 6f 6e 20 69 73 20 74 68 61 74 20 6c 61 72 67 65  on is that large
34d41 20 64 6f 63 6c 69 73 74 73 20 72 65 70 72 65 73   doclists repres
34d42 65 6e 74 20 74 65 72 6d 73 20 77 68 69 63 68 20  ent terms which 
34d43 61 72 65 20 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65  are more.** like
34d44 6c 79 20 74 6f 20 62 65 20 71 75 65 72 79 20 74  ly to be query t
34d45 61 72 67 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f  argets..**.** TO
34d46 44 4f 28 73 68 65 73 73 29 20 49 74 20 6d 61 79  DO(shess) It may
34d47 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20 62   be useful for b
34d48 6c 6f 63 6b 69 6e 67 20 64 65 63 69 73 69 6f 6e  locking decision
34d49 73 20 74 6f 20 62 65 20 6d 6f 72 65 0a 2a 2a 20  s to be more.** 
34d4a 64 79 6e 61 6d 69 63 2e 20 20 46 6f 72 20 69 6e  dynamic.  For in
34d4b 73 74 61 6e 63 65 2c 20 69 74 20 6d 61 79 20 6d  stance, it may m
34d4c 61 6b 65 20 6d 6f 72 65 20 73 65 6e 73 65 20 74  ake more sense t
34d4d 6f 20 68 61 76 65 20 61 20 32 2e 35 6b 20 6c 65  o have a 2.5k le
34d4e 61 66 0a 2a 2a 20 6e 6f 64 65 20 72 61 74 68 65  af.** node rathe
34d4f 72 20 74 68 61 6e 20 73 70 6c 69 74 74 69 6e 67  r than splitting
34d50 20 69 6e 74 6f 20 32 6b 20 61 6e 64 20 2e 35 6b   into 2k and .5k
34d51 20 6e 6f 64 65 73 2e 20 20 4d 79 20 69 6e 74 75   nodes.  My intu
34d52 69 74 69 6f 6e 20 69 73 0a 2a 2a 20 74 68 61 74  ition is.** that
34d53 20 74 68 69 73 20 6d 69 67 68 74 20 65 78 74 65   this might exte
34d54 6e 64 20 74 68 72 6f 75 67 68 20 32 78 20 6f 72  nd through 2x or
34d55 20 34 78 20 74 68 65 20 70 61 67 65 73 69 7a 65   4x the pagesize
34d56 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
34d57 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  ment interior no
34d58 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d  des ****.** Segm
34d59 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  ent interior nod
34d5a 65 73 20 73 74 6f 72 65 20 62 6c 6f 63 6b 69 64  es store blockid
34d5b 73 20 66 6f 72 20 73 75 62 74 72 65 65 20 6e 6f  s for subtree no
34d5c 64 65 73 20 61 6e 64 20 74 65 72 6d 73 0a 2a 2a  des and terms.**
34d5d 20 74 6f 20 64 65 73 63 72 69 62 65 20 77 68 61   to describe wha
34d5e 74 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64  t data is stored
34d5f 20 62 79 20 74 68 65 20 65 61 63 68 20 73 75 62   by the each sub
34d60 74 72 65 65 2e 20 20 49 6e 74 65 72 69 6f 72 0a  tree.  Interior.
34d61 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69  ** nodes are wri
34d62 74 74 65 6e 20 75 73 69 6e 67 20 49 6e 74 65 72  tten using Inter
34d63 69 6f 72 57 72 69 74 65 72 2c 20 61 6e 64 20 72  iorWriter, and r
34d64 65 61 64 20 75 73 69 6e 67 0a 2a 2a 20 49 6e 74  ead using.** Int
34d65 65 72 69 6f 72 52 65 61 64 65 72 2e 20 20 49 6e  eriorReader.  In
34d66 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61 72  teriorWriters ar
34d67 65 20 63 72 65 61 74 65 64 20 61 73 20 6e 65 65  e created as nee
34d68 64 65 64 20 77 68 65 6e 0a 2a 2a 20 53 65 67 6d  ded when.** Segm
34d69 65 6e 74 57 72 69 74 65 72 20 63 72 65 61 74 65  entWriter create
34d6a 73 20 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65 73  s new leaf nodes
34d6b 2c 20 6f 72 20 77 68 65 6e 20 61 6e 20 69 6e 74  , or when an int
34d6c 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 69 74  erior node.** it
34d6d 73 65 6c 66 20 67 72 6f 77 73 20 74 6f 6f 20 62  self grows too b
34d6e 69 67 20 61 6e 64 20 6d 75 73 74 20 62 65 20 73  ig and must be s
34d6f 70 6c 69 74 2e 20 20 54 68 65 20 66 6f 72 6d 61  plit.  The forma
34d70 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 0a 2a 2a  t of interior.**
34d71 20 6e 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61   nodes:.**.** va
34d72 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20  rint iHeight;   
34d73 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
34d74 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
34d75 20 61 6c 77 61 79 73 20 3e 30 29 0a 2a 2a 20 76   always >0).** v
34d76 61 72 69 6e 74 20 69 42 6c 6f 63 6b 69 64 3b 20  arint iBlockid; 
34d77 20 20 20 20 20 20 20 20 20 28 62 6c 6f 63 6b 20           (block 
34d78 69 64 20 6f 66 20 6e 6f 64 65 27 73 20 6c 65 66  id of node's lef
34d79 74 6d 6f 73 74 20 73 75 62 74 72 65 65 29 0a 2a  tmost subtree).*
34d7a 2a 20 6f 70 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20  * optional {.** 
34d7b 20 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20    varint nTerm; 
34d7c 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
34d7d 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29  h of first term)
34d7e 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d  .**   char pTerm
34d7f 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 28 63  [nTerm];      (c
34d80 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
34d81 74 65 72 6d 29 0a 2a 2a 20 20 20 61 72 72 61 79  term).**   array
34d82 20 7b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   {.**           
34d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34d84 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74 65       (further te
34d85 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65 6e  rms are delta-en
34d86 63 6f 64 65 64 29 0a 2a 2a 20 20 20 20 20 76 61  coded).**     va
34d87 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20  rint nPrefix;   
34d88 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
34d89 20 6f 66 20 73 68 61 72 65 64 20 70 72 65 66 69   of shared prefi
34d8a 78 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20  x with previous 
34d8b 74 65 72 6d 29 0a 2a 2a 20 20 20 20 20 76 61 72  term).**     var
34d8c 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20  int nSuffix;    
34d8d 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
34d8e 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66 66  of unshared suff
34d8f 69 78 29 0a 2a 2a 20 20 20 20 20 63 68 61 72 20  ix).**     char 
34d90 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66  pTermSuffix[nSuf
34d91 66 69 78 5d 3b 20 28 75 6e 73 68 61 72 65 64 20  fix]; (unshared 
34d92 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20 74  suffix of next t
34d93 65 72 6d 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d  erm).**   }.** }
34d94 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 6f 70 74  .**.** Here, opt
34d95 69 6f 6e 61 6c 20 7b 20 58 20 7d 20 6d 65 61 6e  ional { X } mean
34d96 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 65 6c  s an optional el
34d97 65 6d 65 6e 74 2c 20 77 68 69 6c 65 20 61 72 72  ement, while arr
34d98 61 79 20 7b 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e  ay { X }.** mean
34d99 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f  s zero or more o
34d9a 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c  ccurrences of X,
34d9b 20 61 64 6a 61 63 65 6e 74 20 69 6e 20 6d 65 6d   adjacent in mem
34d9c 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  ory..**.** An in
34d9d 74 65 72 69 6f 72 20 6e 6f 64 65 20 65 6e 63 6f  terior node enco
34d9e 64 65 73 20 6e 20 74 65 72 6d 73 20 73 65 70 61  des n terms sepa
34d9f 72 61 74 69 6e 67 20 6e 2b 31 20 73 75 62 74 72  rating n+1 subtr
34da0 65 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 75 62  ees.  The.** sub
34da1 74 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20  tree blocks are 
34da2 63 6f 6e 74 69 67 75 6f 75 73 2c 20 73 6f 20 6f  contiguous, so o
34da3 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 73 75  nly the first su
34da4 62 74 72 65 65 27 73 20 62 6c 6f 63 6b 69 64 0a  btree's blockid.
34da5 2a 2a 20 69 73 20 65 6e 63 6f 64 65 64 2e 20 20  ** is encoded.  
34da6 54 68 65 20 73 75 62 74 72 65 65 20 61 74 20 69  The subtree at i
34da7 42 6c 6f 63 6b 69 64 20 77 69 6c 6c 20 63 6f 6e  Blockid will con
34da8 74 61 69 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6c  tain all terms l
34da9 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ess.** than the 
34daa 66 69 72 73 74 20 74 65 72 6d 20 65 6e 63 6f 64  first term encod
34dab 65 64 20 28 6f 72 20 61 6c 6c 20 74 65 72 6d 73  ed (or all terms
34dac 20 69 66 20 6e 6f 20 74 65 72 6d 20 69 73 20 65   if no term is e
34dad 6e 63 6f 64 65 64 29 2e 0a 2a 2a 20 4f 74 68 65  ncoded)..** Othe
34dae 72 77 69 73 65 2c 20 66 6f 72 20 74 65 72 6d 73  rwise, for terms
34daf 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
34db0 20 65 71 75 61 6c 20 74 6f 20 70 54 65 72 6d 5b   equal to pTerm[
34db1 69 5d 20 62 75 74 20 6c 65 73 73 0a 2a 2a 20 74  i] but less.** t
34db2 68 61 6e 20 70 54 65 72 6d 5b 69 2b 31 5d 2c 20  han pTerm[i+1], 
34db3 74 68 65 20 73 75 62 74 72 65 65 20 66 6f 72 20  the subtree for 
34db4 74 68 61 74 20 74 65 72 6d 20 77 69 6c 6c 20 62  that term will b
34db5 65 20 72 6f 6f 74 65 64 20 61 74 0a 2a 2a 20 69  e rooted at.** i
34db6 42 6c 6f 63 6b 69 64 2b 69 2e 20 20 49 6e 74 65  Blockid+i.  Inte
34db7 72 69 6f 72 20 6e 6f 64 65 73 20 6f 6e 6c 79 20  rior nodes only 
34db8 73 74 6f 72 65 20 65 6e 6f 75 67 68 20 74 65 72  store enough ter
34db9 6d 20 64 61 74 61 20 74 6f 0a 2a 2a 20 64 69 73  m data to.** dis
34dba 74 69 6e 67 75 69 73 68 20 61 64 6a 61 63 65 6e  tinguish adjacen
34dbb 74 20 63 68 69 6c 64 72 65 6e 20 28 69 66 20 74  t children (if t
34dbc 68 65 20 72 69 67 68 74 6d 6f 73 74 20 74 65 72  he rightmost ter
34dbd 6d 20 6f 66 20 74 68 65 20 6c 65 66 74 0a 2a 2a  m of the left.**
34dbe 20 63 68 69 6c 64 20 69 73 20 22 73 6f 6d 65 74   child is "somet
34dbf 68 69 6e 67 22 2c 20 61 6e 64 20 74 68 65 20 6c  hing", and the l
34dc0 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20  eftmost term of 
34dc1 74 68 65 20 72 69 67 68 74 20 63 68 69 6c 64 20  the right child 
34dc2 69 73 0a 2a 2a 20 22 77 69 63 6b 65 64 22 2c 20  is.** "wicked", 
34dc3 6f 6e 6c 79 20 22 77 22 20 69 73 20 73 74 6f 72  only "w" is stor
34dc4 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64  ed)..**.** New d
34dc5 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64 20 74  ata is spilled t
34dc6 6f 20 61 20 6e 65 77 20 69 6e 74 65 72 69 6f 72  o a new interior
34dc7 20 6e 6f 64 65 20 61 74 20 74 68 65 20 73 61 6d   node at the sam
34dc8 65 20 68 65 69 67 68 74 20 77 68 65 6e 0a 2a 2a  e height when.**
34dc9 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64   the current nod
34dca 65 20 65 78 63 65 65 64 73 20 49 4e 54 45 52 49  e exceeds INTERI
34dcb 4f 52 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65  OR_MAX bytes (de
34dcc 66 61 75 6c 74 20 32 30 34 38 29 2e 0a 2a 2a 20  fault 2048)..** 
34dcd 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
34dce 4d 53 20 28 64 65 66 61 75 6c 74 20 37 29 20 6b  MS (default 7) k
34dcf 65 65 70 73 20 6c 61 72 67 65 20 74 65 72 6d 73  eeps large terms
34dd0 20 66 72 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69   from monopolizi
34dd1 6e 67 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  ng.** interior n
34dd2 6f 64 65 73 20 61 6e 64 20 6d 61 6b 69 6e 67 20  odes and making 
34dd3 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b 69  the tree too ski
34dd4 6e 6e 79 2e 20 20 54 68 65 20 69 6e 74 65 72 69  nny.  The interi
34dd5 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 61 74 20 61  or nodes.** at a
34dd6 20 67 69 76 65 6e 20 68 65 69 67 68 74 20 61 72   given height ar
34dd7 65 20 6e 61 74 75 72 61 6c 6c 79 20 74 72 61 63  e naturally trac
34dd8 6b 65 64 20 62 79 20 69 6e 74 65 72 69 6f 72 20  ked by interior 
34dd9 6e 6f 64 65 73 20 61 74 0a 2a 2a 20 68 65 69 67  nodes at.** heig
34dda 68 74 2b 31 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  ht+1, and so on.
34ddb 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d  .**.**.**** Segm
34ddc 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 2a 2a  ent directory **
34ddd 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 67 6d 65 6e  **.** The segmen
34dde 74 20 64 69 72 65 63 74 6f 72 79 20 69 6e 20 74  t directory in t
34ddf 61 62 6c 65 20 25 5f 73 65 67 64 69 72 20 73 74  able %_segdir st
34de0 6f 72 65 73 20 6d 65 74 61 2d 69 6e 66 6f 72 6d  ores meta-inform
34de1 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72  ation for.** mer
34de2 67 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69 6e  ging and deletin
34de3 67 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20  g segments, and 
34de4 61 6c 73 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  also the root no
34de5 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 65 67  de of the.** seg
34de6 6d 65 6e 74 27 73 20 74 72 65 65 2e 0a 2a 2a 0a  ment's tree..**.
34de7 2a 2a 20 54 68 65 20 72 6f 6f 74 20 6e 6f 64 65  ** The root node
34de8 20 69 73 20 74 68 65 20 74 6f 70 20 6e 6f 64 65   is the top node
34de9 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74 27   of the segment'
34dea 73 20 74 72 65 65 20 61 66 74 65 72 20 65 6e 63  s tree after enc
34deb 6f 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e 74  oding.** the ent
34dec 69 72 65 20 73 65 67 6d 65 6e 74 2c 20 72 65 73  ire segment, res
34ded 74 72 69 63 74 65 64 20 74 6f 20 52 4f 4f 54 5f  tricted to ROOT_
34dee 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75  MAX bytes (defau
34def 6c 74 20 31 30 32 34 29 2e 0a 2a 2a 20 54 68 69  lt 1024)..** Thi
34df0 73 20 63 6f 75 6c 64 20 62 65 20 65 69 74 68 65  s could be eithe
34df1 72 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 6f 72  r a leaf node or
34df2 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
34df3 65 2e 20 20 49 66 20 74 68 65 20 74 6f 70 0a 2a  e.  If the top.*
34df4 2a 20 6e 6f 64 65 20 72 65 71 75 69 72 65 73 20  * node requires 
34df5 6d 6f 72 65 20 74 68 61 6e 20 52 4f 4f 54 5f 4d  more than ROOT_M
34df6 41 58 20 62 79 74 65 73 2c 20 69 74 20 69 73 20  AX bytes, it is 
34df7 66 6c 75 73 68 65 64 20 74 6f 20 25 5f 73 65 67  flushed to %_seg
34df8 6d 65 6e 74 73 0a 2a 2a 20 61 6e 64 20 61 20 6e  ments.** and a n
34df9 65 77 20 72 6f 6f 74 20 69 6e 74 65 72 69 6f 72  ew root interior
34dfa 20 6e 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   node is generat
34dfb 65 64 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64  ed (which should
34dfc 20 61 6c 77 61 79 73 20 66 69 74 0a 2a 2a 20 77   always fit.** w
34dfd 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 20 62  ithin ROOT_MAX b
34dfe 65 63 61 75 73 65 20 69 74 20 6f 6e 6c 79 20 6e  ecause it only n
34dff 65 65 64 73 20 73 70 61 63 65 20 66 6f 72 20 32  eeds space for 2
34e00 20 76 61 72 69 6e 74 73 2c 20 74 68 65 0a 2a 2a   varints, the.**
34e01 20 68 65 69 67 68 74 20 61 6e 64 20 74 68 65 20   height and the 
34e02 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 70  blockid of the p
34e03 72 65 76 69 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a  revious root)..*
34e04 2a 0a 2a 2a 20 54 68 65 20 6d 65 74 61 2d 69 6e  *.** The meta-in
34e05 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65  formation in the
34e06 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
34e07 72 79 20 69 73 3a 0a 2a 2a 20 20 20 6c 65 76 65  ry is:.**   leve
34e08 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
34e09 2d 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c 20  - segment level 
34e0a 28 73 65 65 20 62 65 6c 6f 77 29 0a 2a 2a 20 20  (see below).**  
34e0b 20 69 64 78 20 20 20 20 20 20 20 20 20 20 20 20   idx            
34e0c 20 20 20 20 20 2d 20 69 6e 64 65 78 20 77 69 74       - index wit
34e0d 68 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20 20 20 20  hin level.**    
34e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e0f 20 20 20 2d 20 28 6c 65 76 65 6c 2c 69 64 78 20     - (level,idx 
34e10 75 6e 69 71 75 65 6c 79 20 69 64 65 6e 74 69 66  uniquely identif
34e11 79 20 61 20 73 65 67 6d 65 6e 74 29 0a 2a 2a 20  y a segment).** 
34e12 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 20 20    start_block   
34e13 20 20 20 20 20 20 2d 20 66 69 72 73 74 20 6c 65        - first le
34e14 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 6c 65 61  af node.**   lea
34e15 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 20 20  ves_end_block   
34e16 20 2d 20 6c 61 73 74 20 6c 65 61 66 20 6e 6f 64   - last leaf nod
34e17 65 0a 2a 2a 20 20 20 65 6e 64 5f 62 6c 6f 63 6b  e.**   end_block
34e18 20 20 20 20 20 20 20 20 20 20 20 2d 20 6c 61 73             - las
34e19 74 20 62 6c 6f 63 6b 20 28 69 6e 63 6c 75 64 69  t block (includi
34e1a 6e 67 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ng interior node
34e1b 73 29 0a 2a 2a 20 20 20 72 6f 6f 74 20 20 20 20  s).**   root    
34e1c 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 63 6f              - co
34e1d 6e 74 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 6e  ntents of root n
34e1e 6f 64 65 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ode.**.** If the
34e1f 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 61 20   root node is a 
34e20 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 65 6e 20  leaf node, then 
34e21 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20  start_block,.** 
34e22 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
34e23 2c 20 61 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b 20  , and end_block 
34e24 61 72 65 20 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a  are all 0..**.**
34e25 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6d 65  .**** Segment me
34e26 72 67 69 6e 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f  rging ****.** To
34e27 20 61 6d 6f 72 74 69 7a 65 20 75 70 64 61 74 65   amortize update
34e28 20 63 6f 73 74 73 2c 20 73 65 67 6d 65 6e 74 73   costs, segments
34e29 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74   are grouped int
34e2a 6f 20 6c 65 76 65 6c 73 20 61 6e 64 0a 2a 2a 20  o levels and.** 
34e2b 6d 65 72 67 65 64 20 69 6e 20 62 61 74 63 68 65  merged in batche
34e2c 73 2e 20 20 45 61 63 68 20 69 6e 63 72 65 61 73  s.  Each increas
34e2d 65 20 69 6e 20 6c 65 76 65 6c 20 72 65 70 72 65  e in level repre
34e2e 73 65 6e 74 73 20 65 78 70 6f 6e 65 6e 74 69 61  sents exponentia
34e2f 6c 6c 79 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63 75  lly.** more docu
34e30 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  ments..**.** New
34e31 20 64 6f 63 75 6d 65 6e 74 73 20 28 61 63 74 75   documents (actu
34e32 61 6c 6c 79 2c 20 64 6f 63 75 6d 65 6e 74 20 75  ally, document u
34e33 70 64 61 74 65 73 29 20 61 72 65 20 74 6f 6b 65  pdates) are toke
34e34 6e 69 7a 65 64 20 61 6e 64 0a 2a 2a 20 77 72 69  nized and.** wri
34e35 74 74 65 6e 20 69 6e 64 69 76 69 64 75 61 6c 6c  tten individuall
34e36 79 20 28 75 73 69 6e 67 20 4c 65 61 66 57 72 69  y (using LeafWri
34e37 74 65 72 29 20 74 6f 20 61 20 6c 65 76 65 6c 20  ter) to a level 
34e38 30 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68 0a  0 segment, with.
34e39 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  ** incrementing 
34e3a 69 64 78 2e 20 20 57 68 65 6e 20 69 64 78 20 72  idx.  When idx r
34e3b 65 61 63 68 65 73 20 4d 45 52 47 45 5f 43 4f 55  eaches MERGE_COU
34e3c 4e 54 20 28 64 65 66 61 75 6c 74 20 31 36 29 2c  NT (default 16),
34e3d 20 61 6c 6c 0a 2a 2a 20 6c 65 76 65 6c 20 30 20   all.** level 0 
34e3e 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65 72  segments are mer
34e3f 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  ged into a singl
34e40 65 20 6c 65 76 65 6c 20 31 20 73 65 67 6d 65 6e  e level 1 segmen
34e41 74 2e 20 20 4c 65 76 65 6c 20 31 0a 2a 2a 20 69  t.  Level 1.** i
34e42 73 20 70 6f 70 75 6c 61 74 65 64 20 6c 69 6b 65  s populated like
34e43 20 6c 65 76 65 6c 20 30 2c 20 61 6e 64 20 65 76   level 0, and ev
34e44 65 6e 74 75 61 6c 6c 79 20 4d 45 52 47 45 5f 43  entually MERGE_C
34e45 4f 55 4e 54 20 6c 65 76 65 6c 20 31 0a 2a 2a 20  OUNT level 1.** 
34e46 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65 72  segments are mer
34e47 67 65 64 20 74 6f 20 61 20 73 69 6e 67 6c 65 20  ged to a single 
34e48 6c 65 76 65 6c 20 32 20 73 65 67 6d 65 6e 74 20  level 2 segment 
34e49 28 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a  (representing.**
34e4a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 5e 32 20 75   MERGE_COUNT^2 u
34e4b 70 64 61 74 65 73 29 2c 20 61 6e 64 20 73 6f 20  pdates), and so 
34e4c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d  on..**.** A segm
34e4d 65 6e 74 20 6d 65 72 67 65 20 74 72 61 76 65 72  ent merge traver
34e4e 73 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  ses all segments
34e4f 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76 65   at a given leve
34e50 6c 20 69 6e 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c  l in.** parallel
34e51 2c 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 73  , performing a s
34e52 74 72 61 69 67 68 74 66 6f 72 77 61 72 64 20 73  traightforward s
34e53 6f 72 74 65 64 20 6d 65 72 67 65 2e 20 20 53 69  orted merge.  Si
34e54 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c  nce segment.** l
34e55 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 77 72  eaf nodes are wr
34e56 69 74 74 65 6e 20 69 6e 20 74 6f 20 74 68 65 20  itten in to the 
34e57 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65  %_segments table
34e58 20 69 6e 20 6f 72 64 65 72 2c 20 74 68 69 73 0a   in order, this.
34e59 2a 2a 20 6d 65 72 67 65 20 74 72 61 76 65 72 73  ** merge travers
34e5a 65 73 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  es the underlyin
34e5b 67 20 73 71 6c 69 74 65 20 64 69 73 6b 20 73 74  g sqlite disk st
34e5c 72 75 63 74 75 72 65 73 20 65 66 66 69 63 69 65  ructures efficie
34e5d 6e 74 6c 79 2e 0a 2a 2a 20 41 66 74 65 72 20 74  ntly..** After t
34e5e 68 65 20 6d 65 72 67 65 2c 20 61 6c 6c 20 73 65  he merge, all se
34e5f 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 66 72 6f  gment blocks fro
34e60 6d 20 74 68 65 20 6d 65 72 67 65 64 20 6c 65 76  m the merged lev
34e61 65 6c 20 61 72 65 0a 2a 2a 20 64 65 6c 65 74 65  el are.** delete
34e62 64 2e 0a 2a 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43  d..**.** MERGE_C
34e63 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  OUNT controls ho
34e64 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65  w often we merge
34e65 20 73 65 67 6d 65 6e 74 73 2e 20 20 31 36 20 73   segments.  16 s
34e66 65 65 6d 73 20 74 6f 20 62 65 0a 2a 2a 20 73 6f  eems to be.** so
34e67 6d 65 77 68 61 74 20 6f 66 20 61 20 73 77 65 65  mewhat of a swee
34e68 74 20 73 70 6f 74 20 66 6f 72 20 69 6e 73 65 72  t spot for inser
34e69 74 69 6f 6e 20 70 65 72 66 6f 72 6d 61 6e 63 65  tion performance
34e6a 2e 20 20 33 32 20 61 6e 64 20 36 34 20 73 68 6f  .  32 and 64 sho
34e6b 77 0a 2a 2a 20 76 65 72 79 20 73 69 6d 69 6c 61  w.** very simila
34e6c 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6e 75  r performance nu
34e6d 6d 62 65 72 73 20 74 6f 20 31 36 20 6f 6e 20 69  mbers to 16 on i
34e6e 6e 73 65 72 74 69 6f 6e 2c 20 74 68 6f 75 67 68  nsertion, though
34e6f 20 74 68 65 79 27 72 65 0a 2a 2a 20 61 20 74 69   they're.** a ti
34e70 6e 79 20 62 69 74 20 73 6c 6f 77 65 72 20 28 70  ny bit slower (p
34e71 65 72 68 61 70 73 20 64 75 65 20 74 6f 20 6d 6f  erhaps due to mo
34e72 72 65 20 6f 76 65 72 68 65 61 64 20 69 6e 20 6d  re overhead in m
34e73 65 72 67 65 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72  erge-time.** sor
34e74 74 69 6e 67 29 2e 20 20 38 20 69 73 20 61 62 6f  ting).  8 is abo
34e75 75 74 20 32 30 25 20 73 6c 6f 77 65 72 20 74 68  ut 20% slower th
34e76 61 6e 20 31 36 2c 20 34 20 61 62 6f 75 74 20 35  an 16, 4 about 5
34e77 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 0a 2a  0% slower than.*
34e78 2a 20 31 36 2c 20 32 20 61 62 6f 75 74 20 36 36  * 16, 2 about 66
34e79 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36  % slower than 16
34e7a 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 71 75 65 72 79  ..**.** At query
34e7b 20 74 69 6d 65 2c 20 68 69 67 68 20 4d 45 52 47   time, high MERG
34e7c 45 5f 43 4f 55 4e 54 20 69 6e 63 72 65 61 73 65  E_COUNT increase
34e7d 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
34e7e 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 77 68 69 63  segments.** whic
34e7f 68 20 6e 65 65 64 20 74 6f 20 62 65 20 73 63 61  h need to be sca
34e80 6e 6e 65 64 20 61 6e 64 20 6d 65 72 67 65 64 2e  nned and merged.
34e81 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20    For instance, 
34e82 77 69 74 68 20 31 30 30 6b 20 64 6f 63 73 0a 2a  with 100k docs.*
34e83 2a 20 69 6e 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a  * inserted:.**.*
34e84 2a 20 20 20 20 4d 45 52 47 45 5f 43 4f 55 4e 54  *    MERGE_COUNT
34e85 20 20 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20     segments.**  
34e86 20 20 20 20 20 31 36 20 20 20 20 20 20 20 20 20       16         
34e87 20 20 32 35 0a 2a 2a 20 20 20 20 20 20 20 20 38    25.**        8
34e88 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 2a 2a             12.**
34e89 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20 20          4       
34e8a 20 20 20 20 31 30 0a 2a 2a 20 20 20 20 20 20 20      10.**       
34e8b 20 32 20 20 20 20 20 20 20 20 20 20 20 20 36 0a   2            6.
34e8c 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 70 70 65 61  **.** This appea
34e8d 72 73 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20  rs to have only 
34e8e 61 20 6d 6f 64 65 72 61 74 65 20 69 6d 70 61 63  a moderate impac
34e8f 74 20 6f 6e 20 71 75 65 72 69 65 73 20 66 6f 72  t on queries for
34e90 20 76 65 72 79 0a 2a 2a 20 66 72 65 71 75 65 6e   very.** frequen
34e91 74 20 74 65 72 6d 73 20 28 77 68 69 63 68 20 61  t terms (which a
34e92 72 65 20 73 6f 6d 65 77 68 61 74 20 64 6f 6d 69  re somewhat domi
34e93 6e 61 74 65 64 20 62 79 20 73 65 67 6d 65 6e 74  nated by segment
34e94 20 6d 65 72 67 65 0a 2a 2a 20 63 6f 73 74 73 29   merge.** costs)
34e95 2c 20 61 6e 64 20 69 6e 66 72 65 71 75 65 6e 74  , and infrequent
34e96 20 61 6e 64 20 6e 6f 6e 2d 65 78 69 73 74 65 6e   and non-existen
34e97 74 20 74 65 72 6d 73 20 73 74 69 6c 6c 20 73 65  t terms still se
34e98 65 6d 20 74 6f 20 62 65 20 66 61 73 74 0a 2a 2a  em to be fast.**
34e99 20 65 76 65 6e 20 77 69 74 68 20 6d 61 6e 79 20   even with many 
34e9a 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  segments..**.** 
34e9b 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 61 74  TODO(shess) That
34e9c 20 73 61 69 64 2c 20 69 74 20 77 6f 75 6c 64 20   said, it would 
34e9d 62 65 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20  be nice to have 
34e9e 61 20 62 65 74 74 65 72 20 71 75 65 72 79 2d 73  a better query-s
34e9f 69 64 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ide.** argument 
34ea0 66 6f 72 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  for MERGE_COUNT 
34ea1 6f 66 20 31 36 2e 20 20 41 6c 73 6f 2c 20 69 74  of 16.  Also, it
34ea2 20 69 73 20 70 6f 73 73 69 62 6c 65 2f 6c 69 6b   is possible/lik
34ea3 65 6c 79 20 74 68 61 74 0a 2a 2a 20 6f 70 74 69  ely that.** opti
34ea4 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20 74 68 69  mizations to thi
34ea5 6e 67 73 20 6c 69 6b 65 20 64 6f 63 6c 69 73 74  ngs like doclist
34ea6 20 6d 65 72 67 69 6e 67 20 77 69 6c 6c 20 73 77   merging will sw
34ea7 69 6e 67 20 74 68 65 20 73 77 65 65 74 0a 2a 2a  ing the sweet.**
34ea8 20 73 70 6f 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a   spot around..**
34ea9 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64  .**.**.**** Hand
34eaa 6c 69 6e 67 20 6f 66 20 64 65 6c 65 74 69 6f 6e  ling of deletion
34eab 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a 2a  s and updates **
34eac 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 77 65 27 72  **.** Since we'r
34ead 65 20 75 73 69 6e 67 20 61 20 73 65 67 6d 65 6e  e using a segmen
34eae 74 65 64 20 73 74 72 75 63 74 75 72 65 2c 20 77  ted structure, w
34eaf 69 74 68 20 6e 6f 20 64 6f 63 69 64 2d 6f 72 69  ith no docid-ori
34eb0 65 6e 74 65 64 0a 2a 2a 20 69 6e 64 65 78 20 69  ented.** index i
34eb1 6e 74 6f 20 74 68 65 20 74 65 72 6d 20 69 6e 64  nto the term ind
34eb2 65 78 2c 20 77 65 20 63 6c 65 61 72 6c 79 20 63  ex, we clearly c
34eb3 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 75 70 64  annot simply upd
34eb4 61 74 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20  ate the term.** 
34eb5 69 6e 64 65 78 20 77 68 65 6e 20 61 20 64 6f 63  index when a doc
34eb6 75 6d 65 6e 74 20 69 73 20 64 65 6c 65 74 65 64  ument is deleted
34eb7 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 46 6f   or updated.  Fo
34eb8 72 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 65 0a  r deletions, we.
34eb9 2a 2a 20 77 72 69 74 65 20 61 6e 20 65 6d 70 74  ** write an empt
34eba 79 20 64 6f 63 6c 69 73 74 20 28 76 61 72 69 6e  y doclist (varin
34ebb 74 28 64 6f 63 69 64 29 20 76 61 72 69 6e 74 28  t(docid) varint(
34ebc 50 4f 53 5f 45 4e 44 29 29 2c 20 66 6f 72 20 75  POS_END)), for u
34ebd 70 64 61 74 65 73 0a 2a 2a 20 77 65 20 73 69 6d  pdates.** we sim
34ebe 70 6c 79 20 77 72 69 74 65 20 74 68 65 20 6e 65  ply write the ne
34ebf 77 20 64 6f 63 6c 69 73 74 2e 20 20 53 65 67 6d  w doclist.  Segm
34ec0 65 6e 74 20 6d 65 72 67 65 73 20 6f 76 65 72 77  ent merges overw
34ec1 72 69 74 65 20 6f 6c 64 65 72 0a 2a 2a 20 64 61  rite older.** da
34ec2 74 61 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ta for a particu
34ec3 6c 61 72 20 64 6f 63 69 64 20 77 69 74 68 20 6e  lar docid with n
34ec4 65 77 65 72 20 64 61 74 61 2c 20 73 6f 20 64 65  ewer data, so de
34ec5 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65 73  letes or updates
34ec6 0a 2a 2a 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  .** will eventua
34ec7 6c 6c 79 20 6f 76 65 72 74 61 6b 65 20 74 68 65  lly overtake the
34ec8 20 65 61 72 6c 69 65 72 20 64 61 74 61 20 61 6e   earlier data an
34ec9 64 20 6b 6e 6f 63 6b 20 69 74 20 6f 75 74 2e 20  d knock it out. 
34eca 20 54 68 65 0a 2a 2a 20 71 75 65 72 79 20 6c 6f   The.** query lo
34ecb 67 69 63 20 6c 69 6b 65 77 69 73 65 20 6d 65 72  gic likewise mer
34ecc 67 65 73 20 64 6f 63 6c 69 73 74 73 20 73 6f 20  ges doclists so 
34ecd 74 68 61 74 20 6e 65 77 65 72 20 64 61 74 61 20  that newer data 
34ece 6b 6e 6f 63 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c  knocks out.** ol
34ecf 64 65 72 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  der data..**.** 
34ed0 54 4f 44 4f 28 73 68 65 73 73 29 20 50 72 6f 76  TODO(shess) Prov
34ed1 69 64 65 20 61 20 56 41 43 55 55 4d 20 74 79 70  ide a VACUUM typ
34ed2 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63  e operation to c
34ed3 6c 65 61 72 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20  lear out all.** 
34ed4 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 64 75  deletions and du
34ed5 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 54 68 69  plications.  Thi
34ed6 73 20 77 6f 75 6c 64 20 62 61 73 69 63 61 6c 6c  s would basicall
34ed7 79 20 62 65 20 61 20 66 6f 72 63 65 64 20 6d 65  y be a forced me
34ed8 72 67 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69  rge.** into a si
34ed9 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f  ngle segment..*/
34eda 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
34edb 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
34edc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
34edd 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 20  ABLE_FTS3)..#if 
34ede 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
34edf 4e 41 42 4c 45 5f 46 54 53 33 29 20 26 26 20 21  NABLE_FTS3) && !
34ee0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
34ee1 4f 52 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51  ORE).# define SQ
34ee2 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23 65 6e 64  LITE_CORE 1.#end
34ee3 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
34ee4 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33  *** Include fts3
34ee5 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  Int.h in the mid
34ee6 64 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a  dle of fts3.c **
34ee7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ee8 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
34ee9 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
34eea 74 73 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  ts3Int.h *******
34eeb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34eec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34eed 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4e  **/./*.** 2009 N
34eee 6f 76 20 31 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ov 12.**.** The 
34eef 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
34ef0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
34ef1 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
34ef2 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
34ef3 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
34ef4 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
34ef5 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
34ef6 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
34ef7 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
34ef8 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
34ef9 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
34efa 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
34efb 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
34efc 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
34efd 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
34efe 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
34eff 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
34f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f04 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a  *********.**.*/.
34f05 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 49 4e 54  .#ifndef _FTSINT
34f06 5f 48 0a 23 64 65 66 69 6e 65 20 5f 46 54 53 49  _H.#define _FTSI
34f07 4e 54 5f 48 0a 0a 23 69 66 20 21 64 65 66 69 6e  NT_H..#if !defin
34f08 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64  ed(NDEBUG) && !d
34f09 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
34f0a 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e  BUG) .# define N
34f0b 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a  DEBUG 1.#endif..
34f0c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34f0d 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b  Include fts3_tok
34f0e 65 6e 69 7a 65 72 2e 68 20 69 6e 20 74 68 65 20  enizer.h in the 
34f0f 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 49 6e  middle of fts3In
34f10 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  t.h **********/.
34f11 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34f12 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
34f13 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a  tokenizer.h ****
34f14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34f16 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20  /*.** 2006 July 
34f17 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
34f18 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
34f19 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
34f1a 73 6f 75 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a  source code..**.
34f1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66  *********.** Def
34f20 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
34f21 63 65 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73  ce to tokenizers
34f22 20 75 73 65 64 20 62 79 20 66 75 6c 6c 74 65 78   used by fulltex
34f23 74 2d 73 65 61 72 63 68 2e 20 20 54 68 65 72 65  t-search.  There
34f24 0a 2a 2a 20 61 72 65 20 74 68 72 65 65 20 62 61  .** are three ba
34f25 73 69 63 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a  sic components:.
34f26 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  **.** sqlite3_to
34f27 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69  kenizer_module i
34f28 73 20 61 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65  s a singleton de
34f29 66 69 6e 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e  fining the token
34f2a 69 7a 65 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63  izer.** interfac
34f2b 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  e functions.  Th
34f2c 69 73 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c  is is essentiall
34f2d 79 20 74 68 65 20 63 6c 61 73 73 20 73 74 72 75  y the class stru
34f2e 63 74 75 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b  cture for.** tok
34f2f 65 6e 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73  enizers..**.** s
34f30 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34f31 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 66 69   is used to defi
34f32 6e 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ne a particular 
34f33 74 6f 6b 65 6e 69 7a 65 72 2c 20 70 65 72 68 61  tokenizer, perha
34f34 70 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20  ps.** including 
34f35 63 75 73 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e  customization in
34f36 66 6f 72 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65  formation define
34f37 64 20 61 74 20 63 72 65 61 74 69 6f 6e 20 74 69  d at creation ti
34f38 6d 65 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  me..**.** sqlite
34f39 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
34f3a 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  or is generated 
34f3b 62 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74  by a tokenizer t
34f3c 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f  o generate.** to
34f3d 6b 65 6e 73 20 66 72 6f 6d 20 61 20 70 61 72 74  kens from a part
34f3e 69 63 75 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f  icular input..*/
34f3f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54  .#ifndef _FTS3_T
34f40 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66  OKENIZER_H_.#def
34f41 69 6e 65 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49  ine _FTS3_TOKENI
34f42 5a 45 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28  ZER_H_../* TODO(
34f43 73 68 65 73 73 29 20 4f 6e 6c 79 20 75 73 65 64  shess) Only used
34f44 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61   for SQLITE_OK a
34f45 6e 64 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61  nd SQLITE_DONE a
34f46 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20  t this time..** 
34f47 49 66 20 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72  If tokenizers ar
34f48 65 20 74 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20  e to be allowed 
34f49 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  to call sqlite3_
34f4a 2a 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74  *() functions, t
34f4b 68 65 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e  hen.** we will n
34f4c 65 65 64 20 61 20 77 61 79 20 74 6f 20 72 65 67  eed a way to reg
34f4d 69 73 74 65 72 20 74 68 65 20 41 50 49 20 63 6f  ister the API co
34f4e 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a  nsistently..*/..
34f4f 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73  /*.** Structures
34f50 20 75 73 65 64 20 62 79 20 74 68 65 20 74 6f 6b   used by the tok
34f51 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65  enizer interface
34f52 2e 20 57 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b  . When a new tok
34f53 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d  enizer.** implem
34f54 65 6e 74 61 74 69 6f 6e 20 69 73 20 72 65 67 69  entation is regi
34f55 73 74 65 72 65 64 2c 20 74 68 65 20 63 61 6c 6c  stered, the call
34f56 65 72 20 70 72 6f 76 69 64 65 73 20 61 20 70 6f  er provides a po
34f57 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73  inter to.** an s
34f58 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34f59 5f 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69  _module containi
34f5a 6e 67 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  ng pointers to t
34f5b 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66  he callback.** f
34f5c 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 61  unctions that ma
34f5d 6b 65 20 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65  ke up an impleme
34f5e 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57  ntation..**.** W
34f5f 68 65 6e 20 61 6e 20 66 74 73 33 20 74 61 62 6c  hen an fts3 tabl
34f60 65 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74  e is created, it
34f61 20 70 61 73 73 65 73 20 61 6e 79 20 61 72 67 75   passes any argu
34f62 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 0a  ments passed to.
34f63 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  ** the tokenizer
34f64 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 43   clause of the C
34f65 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
34f66 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  BLE statement to
34f67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
34f68 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
34f69 2e 78 43 72 65 61 74 65 28 29 20 66 75 6e 63 74  .xCreate() funct
34f6a 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 71 75 65  ion of the reque
34f6b 73 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a  sted tokenizer.*
34f6c 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
34f6d 2e 20 54 68 65 20 78 43 72 65 61 74 65 28 29 20  . The xCreate() 
34f6e 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e  function in turn
34f6f 20 72 65 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20   returns an .** 
34f70 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34f71 72 20 73 74 72 75 63 74 75 72 65 20 72 65 70 72  r structure repr
34f72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 70 65  esenting the spe
34f73 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20  cific tokenizer 
34f74 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 66 6f  to.** be used fo
34f75 72 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  r the fts3 table
34f76 20 28 63 75 73 74 6f 6d 69 7a 65 64 20 62 79 20   (customized by 
34f77 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c  the tokenizer cl
34f78 61 75 73 65 20 61 72 67 75 6d 65 6e 74 73 29 2e  ause arguments).
34f79 0a 2a 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69  .**.** To tokeni
34f7a 7a 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66  ze an input buff
34f7b 65 72 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f  er, the sqlite3_
34f7c 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
34f7d 2e 78 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68  .xOpen().** meth
34f7e 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e 20 49 74  od is called. It
34f7f 20 72 65 74 75 72 6e 73 20 61 6e 20 73 71 6c 69   returns an sqli
34f80 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34f81 72 73 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74  rsor object.** t
34f82 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20  hat may be used 
34f83 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70  to tokenize a sp
34f84 65 63 69 66 69 63 20 69 6e 70 75 74 20 62 75 66  ecific input buf
34f85 66 65 72 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20  fer based on.** 
34f86 74 68 65 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  the tokenization
34f87 20 72 75 6c 65 73 20 73 75 70 70 6c 69 65 64 20   rules supplied 
34f88 62 79 20 61 20 73 70 65 63 69 66 69 63 20 73 71  by a specific sq
34f89 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a  lite3_tokenizer.
34f8a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79  ** object..*/.ty
34f8b 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
34f8c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
34f8d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f  odule sqlite3_to
34f8e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a  kenizer_module;.
34f8f 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
34f90 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34f91 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34f92 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  er;.typedef stru
34f93 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ct sqlite3_token
34f94 69 7a 65 72 5f 63 75 72 73 6f 72 20 73 71 6c 69  izer_cursor sqli
34f95 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34f96 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74 20 73 71  rsor;..struct sq
34f97 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34f98 6d 6f 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20  module {..  /*. 
34f99 20 2a 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   ** Structure ve
34f9a 72 73 69 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c  rsion. Should al
34f9b 77 61 79 73 20 62 65 20 73 65 74 20 74 6f 20 30  ways be set to 0
34f9c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65  ..  */.  int iVe
34f9d 72 73 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  rsion;..  /*.  *
34f9e 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74  * Create a new t
34f9f 6f 6b 65 6e 69 7a 65 72 2e 20 54 68 65 20 76 61  okenizer. The va
34fa0 6c 75 65 73 20 69 6e 20 74 68 65 20 61 72 67 76  lues in the argv
34fa1 5b 5d 20 61 72 72 61 79 20 61 72 65 20 74 68 65  [] array are the
34fa2 0a 20 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20  .  ** arguments 
34fa3 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 22 74  passed to the "t
34fa4 6f 6b 65 6e 69 7a 65 72 22 20 63 6c 61 75 73 65  okenizer" clause
34fa5 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56   of the CREATE V
34fa6 49 52 54 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c  IRTUAL.  ** TABL
34fa7 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  E statement that
34fa8 20 63 72 65 61 74 65 64 20 74 68 65 20 66 74 73   created the fts
34fa9 33 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 78 61  3 table. For exa
34faa 6d 70 6c 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68  mple, if.  ** th
34fab 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20  e following SQL 
34fac 69 73 20 65 78 65 63 75 74 65 64 3a 0a 20 20 2a  is executed:.  *
34fad 2a 0a 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20  *.  **   CREATE 
34fae 2e 2e 20 55 53 49 4e 47 20 66 74 73 33 28 20 2e  .. USING fts3( .
34faf 2e 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c  .. , tokenizer <
34fb0 74 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20  tokenizer-name> 
34fb1 61 72 67 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a  arg1 arg2).  **.
34fb2 20 20 2a 2a 20 74 68 65 6e 20 61 72 67 63 20 69    ** then argc i
34fb3 73 20 73 65 74 20 74 6f 20 32 2c 20 61 6e 64 20  s set to 2, and 
34fb4 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61 79  the argv[] array
34fb5 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65   contains pointe
34fb6 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73  rs.  ** to the s
34fb7 74 72 69 6e 67 73 20 22 61 72 67 31 22 20 61 6e  trings "arg1" an
34fb8 64 20 22 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20  d "arg2"..  **. 
34fb9 20 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20   ** This method 
34fba 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69  should return ei
34fbb 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28  ther SQLITE_OK (
34fbc 30 29 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65  0), or an SQLite
34fbd 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64   error .  ** cod
34fbe 65 2e 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20  e. If SQLITE_OK 
34fbf 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65  is returned, the
34fc0 6e 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73  n *ppTokenizer s
34fc1 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a  hould be set.  *
34fc2 2a 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  * to point at th
34fc3 65 20 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20  e newly created 
34fc4 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75 63 74  tokenizer struct
34fc5 75 72 65 2e 20 54 68 65 20 67 65 6e 65 72 69 63  ure. The generic
34fc6 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  .  ** sqlite3_to
34fc7 6b 65 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20  kenizer.pModule 
34fc8 76 61 72 69 61 62 6c 65 20 73 68 6f 75 6c 64 20  variable should 
34fc9 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73  not be initialis
34fca 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20  ed by.  ** this 
34fcb 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65 20 63 61  callback. The ca
34fcc 6c 6c 65 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e  ller will do so.
34fcd 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  .  */.  int (*xC
34fce 72 65 61 74 65 29 28 0a 20 20 20 20 69 6e 74 20  reate)(.    int 
34fcf 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
34fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fd1 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67 76 20  /* Size of argv 
34fd2 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e  array */.    con
34fd3 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
34fd4 72 67 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgv,            
34fd5 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72   /* Tokenizer ar
34fd6 67 75 6d 65 6e 74 20 73 74 72 69 6e 67 73 20 2a  gument strings *
34fd7 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f  /.    sqlite3_to
34fd8 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65  kenizer **ppToke
34fd9 6e 69 7a 65 72 20 20 20 20 20 2f 2a 20 4f 55 54  nizer     /* OUT
34fda 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69  : Created tokeni
34fdb 7a 65 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f  zer */.  );..  /
34fdc 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61  *.  ** Destroy a
34fdd 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e  n existing token
34fde 69 7a 65 72 2e 20 54 68 65 20 66 74 73 33 20 6d  izer. The fts3 m
34fdf 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73  odule calls this
34fe0 20 6d 65 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61   method.  ** exa
34fe1 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61  ctly once for ea
34fe2 63 68 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ch successful ca
34fe3 6c 6c 20 74 6f 20 78 43 72 65 61 74 65 28 29 2e  ll to xCreate().
34fe4 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44  .  */.  int (*xD
34fe5 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
34fe6 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
34fe7 6e 69 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20  nizer);..  /*.  
34fe8 2a 2a 20 43 72 65 61 74 65 20 61 20 74 6f 6b 65  ** Create a toke
34fe9 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20  nizer cursor to 
34fea 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75  tokenize an inpu
34feb 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 63 61  t buffer. The ca
34fec 6c 6c 65 72 0a 20 20 2a 2a 20 69 73 20 72 65 73  ller.  ** is res
34fed 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e 73  ponsible for ens
34fee 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 69  uring that the i
34fef 6e 70 75 74 20 62 75 66 66 65 72 20 72 65 6d 61  nput buffer rema
34ff0 69 6e 73 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75  ins valid.  ** u
34ff1 6e 74 69 6c 20 74 68 65 20 63 75 72 73 6f 72 20  ntil the cursor 
34ff2 69 73 20 63 6c 6f 73 65 64 20 28 75 73 69 6e 67  is closed (using
34ff3 20 74 68 65 20 78 43 6c 6f 73 65 28 29 20 6d 65   the xClose() me
34ff4 74 68 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69  thod). .  */.  i
34ff5 6e 74 20 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20  nt (*xOpen)(.   
34ff6 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34ff7 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
34ff8 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a        /* Tokeniz
34ff9 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20  er object */.   
34ffa 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e   const char *pIn
34ffb 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c  put, int nBytes,
34ffc 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 62        /* Input b
34ffd 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c  uffer */.    sql
34ffe 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
34fff 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
35000 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74 65    /* OUT: Create
35001 64 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73  d tokenizer curs
35002 6f 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a  or */.  );..  /*
35003 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e  .  ** Destroy an
35004 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69   existing tokeni
35005 7a 65 72 20 63 75 72 73 6f 72 2e 20 54 68 65 20  zer cursor. The 
35006 66 74 73 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c  fts3 module call
35007 73 20 74 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74  s this .  ** met
35008 68 6f 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65  hod exactly once
35009 20 66 6f 72 20 65 61 63 68 20 73 75 63 63 65 73   for each succes
3500a 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70  sful call to xOp
3500b 65 6e 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  en()..  */.  int
3500c 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74   (*xClose)(sqlit
3500d 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3500e 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a  sor *pCursor);..
3500f 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65    /*.  ** Retrie
35010 76 65 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  ve the next toke
35011 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e  n from the token
35012 69 7a 65 72 20 63 75 72 73 6f 72 20 70 43 75 72  izer cursor pCur
35013 73 6f 72 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d  sor. This.  ** m
35014 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 65 69 74  ethod should eit
35015 68 65 72 20 72 65 74 75 72 6e 20 53 51 4c 49 54  her return SQLIT
35016 45 5f 4f 4b 20 61 6e 64 20 73 65 74 20 74 68 65  E_OK and set the
35017 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 0a 20   values of the. 
35018 20 2a 2a 20 22 4f 55 54 22 20 76 61 72 69 61 62   ** "OUT" variab
35019 6c 65 73 20 69 64 65 6e 74 69 66 69 65 64 20 62  les identified b
3501a 65 6c 6f 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f  elow, or SQLITE_
3501b 44 4f 4e 45 20 74 6f 20 69 6e 64 69 63 61 74 65  DONE to indicate
3501c 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 65   that.  ** the e
3501d 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  nd of the buffer
3501e 20 68 61 73 20 62 65 65 6e 20 72 65 61 63 68 65   has been reache
3501f 64 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  d, or an SQLite 
35020 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a  error code..  **
35021 0a 20 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73  .  ** *ppToken s
35022 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
35023 70 6f 69 6e 74 20 61 74 20 61 20 62 75 66 66 65  point at a buffe
35024 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
35025 20 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65   .  ** normalize
35026 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  d version of the
35027 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74   token (i.e. aft
35028 65 72 20 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64  er any case-fold
35029 69 6e 67 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20  ing and/or.  ** 
3502a 73 74 65 6d 6d 69 6e 67 20 68 61 73 20 62 65 65  stemming has bee
3502b 6e 20 70 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70  n performed). *p
3502c 6e 42 79 74 65 73 20 73 68 6f 75 6c 64 20 62 65  nBytes should be
3502d 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67   set to the leng
3502e 74 68 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20  th.  ** of this 
3502f 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 2e  buffer in bytes.
35030 20 54 68 65 20 69 6e 70 75 74 20 74 65 78 74 20   The input text 
35031 74 68 61 74 20 67 65 6e 65 72 61 74 65 64 20 74  that generated t
35032 68 65 20 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a  he token is.  **
35033 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
35034 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 73 20  he byte offsets 
35035 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 69 53  returned in *piS
35036 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64 0a 20  tartOffset and. 
35037 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74   ** *piEndOffset
35038 2e 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74  . *piStartOffset
35039 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74   should be set t
3503a 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
3503b 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 62 79  he first.  ** by
3503c 74 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  te of the token 
3503d 69 6e 20 74 68 65 20 69 6e 70 75 74 20 62 75 66  in the input buf
3503e 66 65 72 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65  fer. *piEndOffse
3503f 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a  t should be set.
35040 20 20 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65    ** to the inde
35041 78 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 62  x of the first b
35042 79 74 65 20 6a 75 73 74 20 70 61 73 74 20 74 68  yte just past th
35043 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b  e end of the tok
35044 65 6e 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69  en in.  ** the i
35045 6e 70 75 74 20 62 75 66 66 65 72 2e 0a 20 20 2a  nput buffer..  *
35046 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65  *.  ** The buffe
35047 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65  r *ppToken is se
35048 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73  t to point at is
35049 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20   managed by the 
3504a 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69  tokenizer.  ** i
3504b 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49  mplementation. I
3504c 74 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72  t is only requir
3504d 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75  ed to be valid u
3504e 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61  ntil the next ca
3504f 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74  ll.  ** to xNext
35050 28 29 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20  () or xClose(). 
35051 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28  .  */.  /* TODO(
35052 73 68 65 73 73 29 20 63 75 72 72 65 6e 74 20 69  shess) current i
35053 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65  mplementation re
35054 71 75 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f  quires pInput to
35055 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72   be.  ** nul-ter
35056 6d 69 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73  minated.  This s
35057 68 6f 75 6c 64 20 65 69 74 68 65 72 20 62 65 20  hould either be 
35058 66 69 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74  fixed, or pInput
35059 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f  /nBytes.  ** sho
3505a 75 6c 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64  uld be converted
3505b 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f   to zInput..  */
3505c 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28  .  int (*xNext)(
3505d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  .    sqlite3_tok
3505e 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
3505f 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b  Cursor,   /* Tok
35060 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f  enizer cursor */
35061 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
35062 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a  **ppToken, int *
35063 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54  pnBytes,  /* OUT
35064 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78  : Normalized tex
35065 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20  t for token */. 
35066 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f     int *piStartO
35067 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  ffset,  /* OUT: 
35068 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74  Byte offset of t
35069 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
3506a 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
3506b 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
3506c 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66   /* OUT: Byte of
3506d 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74  fset of end of t
3506e 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75  oken in input bu
3506f 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
35070 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
35071 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
35072 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e  of tokens return
35073 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f  ed before this o
35074 6e 65 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73  ne */.  );.};..s
35075 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
35076 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73  kenizer {.  cons
35077 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
35078 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  zer_module *pMod
35079 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64  ule;  /* The mod
3507a 75 6c 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b  ule for this tok
3507b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54  enizer */.  /* T
3507c 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65  okenizer impleme
3507d 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79  ntations will ty
3507e 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69  pically add addi
3507f 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f  tional fields */
35080 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69  .};..struct sqli
35081 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
35082 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
35083 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
35084 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a  enizer;       /*
35085 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74   Tokenizer for t
35086 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20  his cursor. */. 
35087 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d   /* Tokenizer im
35088 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69  plementations wi
35089 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64  ll typically add
3508a 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
3508b 64 73 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66  ds */.};..#endif
3508c 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49   /* _FTS3_TOKENI
3508d 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ZER_H_ */../****
3508e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3508f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
35090 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
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 2f 0a 2f 2a 2a 2a 2a  *********/./****
35093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
35094 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
35095 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73 33 49  eft off in fts3I
35096 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
35097 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
35098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
35099 64 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20 69  de fts3_hash.h i
3509a 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
3509b 66 74 73 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  fts3Int.h ******
3509c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
3509d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
3509e 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e   file fts3_hash.
3509f 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
350a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
350a2 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
350a3 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
350a4 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
350a5 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
350a6 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
350a7 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
350a8 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
350a9 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
350aa 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
350ab 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
350ac 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
350ad 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
350ae 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
350af 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
350b0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
350b1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
350b2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
350b3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
350b4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
350b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350b9 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
350ba 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72   header file for
350bb 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73   the generic has
350bc 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  h-table implemen
350bd 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e  ation.** used in
350be 20 53 51 4c 69 74 65 2e 20 20 57 65 27 76 65 20   SQLite.  We've 
350bf 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69 67  modified it slig
350c0 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73  htly to serve as
350c1 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a   a standalone.**
350c2 20 68 61 73 68 20 74 61 62 6c 65 20 69 6d 70 6c   hash table impl
350c3 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74  ementation for t
350c4 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64  he full-text ind
350c5 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  exing module..**
350c6 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53  .*/.#ifndef _FTS
350c7 33 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e  3_HASH_H_.#defin
350c8 65 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a  e _FTS3_HASH_H_.
350c9 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
350ca 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75  arations of stru
350cb 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64  ctures. */.typed
350cc 65 66 20 73 74 72 75 63 74 20 46 74 73 33 48 61  ef struct Fts3Ha
350cd 73 68 20 46 74 73 33 48 61 73 68 3b 0a 74 79 70  sh Fts3Hash;.typ
350ce 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33  edef struct Fts3
350cf 48 61 73 68 45 6c 65 6d 20 46 74 73 33 48 61 73  HashElem Fts3Has
350d0 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d  hElem;../* A com
350d1 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65  plete hash table
350d2 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
350d3 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
350d4 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54   structure..** T
350d5 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20  he internals of 
350d6 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
350d7 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  re intended to b
350d8 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65  e opaque -- clie
350d9 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c  nt.** code shoul
350da 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  d not attempt to
350db 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
350dc 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20  y the fields of 
350dd 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
350de 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61  * directly.  Cha
350df 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74 75  nge this structu
350e0 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67  re only by using
350e1 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65   the routines be
350e2 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  low..** However,
350e3 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70 72   many of the "pr
350e4 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22 66  ocedures" and "f
350e5 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f  unctions" for mo
350e6 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61  difying and.** a
350e7 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73 74  ccessing this st
350e8 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61 6c  ructure are real
350e9 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65  ly macros, so we
350ea 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61   can't really ma
350eb 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63  ke.** this struc
350ec 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a  ture opaque..*/.
350ed 73 74 72 75 63 74 20 46 74 73 33 48 61 73 68 20  struct Fts3Hash 
350ee 7b 0a 20 20 63 68 61 72 20 6b 65 79 43 6c 61 73  {.  char keyClas
350ef 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  s;          /* H
350f0 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45  ASH_INT, _POINTE
350f1 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e  R, _STRING, _BIN
350f2 41 52 59 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ARY */.  char co
350f3 70 79 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  pyKey;          
350f4 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79   /* True if copy
350f5 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20   of key made on 
350f6 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20  insert */.  int 
350f7 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20  count;          
350f8 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
350f9 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73   entries in this
350fa 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 74 73 33   table */.  Fts3
350fb 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b  HashElem *first;
350fc 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74      /* The first
350fd 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
350fe 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68  array */.  int h
350ff 74 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  tsize;          
35100 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
35101 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68  buckets in the h
35102 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ash table */.  s
35103 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 7b 20  truct _fts3ht { 
35104 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61         /* the ha
35105 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  sh table */.    
35106 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
35107 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
35108 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69  er of entries wi
35109 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a  th this hash */.
3510a 20 20 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d      Fts3HashElem
3510b 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 2f 2a 20   *chain;     /* 
3510c 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74  Pointer to first
3510d 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69 73   entry with this
3510e 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74   hash */.  } *ht
3510f 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c  ;.};../* Each el
35110 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73  ement in the has
35111 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e  h table is an in
35112 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
35113 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75  llowing .** stru
35114 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d  cture.  All elem
35115 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ents are stored 
35116 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62  on a single doub
35117 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a  ly-linked list..
35118 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69  **.** Again, thi
35119 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69  s structure is i
3511a 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70  ntended to be op
3511b 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e  aque, but it can
3511c 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20  't really.** be 
3511d 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20 69  opaque because i
3511e 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61 63  t is used by mac
3511f 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  ros..*/.struct F
35120 74 73 33 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20  ts3HashElem {.  
35121 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65  Fts3HashElem *ne
35122 78 74 2c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e 65  xt, *prev; /* Ne
35123 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20  xt and previous 
35124 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
35125 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  table */.  void 
35126 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  *data;          
35127 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73        /* Data as
35128 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
35129 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20  is element */.  
3512a 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20  void *pKey; int 
3512b 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4b 65  nKey;      /* Ke
3512c 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
3512d 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a  h this element *
3512e 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72  /.};../*.** Ther
3512f 65 20 61 72 65 20 32 20 64 69 66 66 65 72 65 6e  e are 2 differen
35130 74 20 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72 61  t modes of opera
35131 74 69 6f 6e 20 66 6f 72 20 61 20 68 61 73 68 20  tion for a hash 
35132 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46  table:.**.**   F
35133 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
35134 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e         pKey poin
35135 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74  ts to a string t
35136 68 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65  hat is nKey byte
35137 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20  s long.**       
35138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35139 20 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74      (including t
3513a 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  he null-terminat
3513b 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61  or, if any).  Ca
3513c 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  se.**           
3513d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3513e 69 73 20 72 65 73 70 65 63 74 65 64 20 69 6e 20  is respected in 
3513f 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a  comparisons..**.
35140 2a 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f 42  **   FTS3_HASH_B
35141 49 4e 41 52 59 20 20 20 20 20 20 20 20 70 4b 65  INARY        pKe
35142 79 20 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61  y points to bina
35143 72 79 20 64 61 74 61 20 6e 4b 65 79 20 62 79 74  ry data nKey byt
35144 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20  es long. .**    
35145 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35146 20 20 20 20 20 20 20 6d 65 6d 63 6d 70 28 29 20         memcmp() 
35147 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61  is used to compa
35148 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41  re keys..**.** A
35149 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79   copy of the key
3514a 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20   is made if the 
3514b 63 6f 70 79 4b 65 79 20 70 61 72 61 6d 65 74 65  copyKey paramete
3514c 72 20 74 6f 20 66 74 73 33 48 61 73 68 49 6e 69  r to fts3HashIni
3514d 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 23 64 65  t is 1.  .*/.#de
3514e 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 53  fine FTS3_HASH_S
3514f 54 52 49 4e 47 20 20 20 20 31 0a 23 64 65 66 69  TRING    1.#defi
35150 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e  ne FTS3_HASH_BIN
35151 41 52 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  ARY    2../*.** 
35152 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e  Access routines.
35153 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73    To delete, ins
35154 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ert a NULL point
35155 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
35156 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
35157 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 46  e3Fts3HashInit(F
35158 74 73 33 48 61 73 68 20 2a 70 4e 65 77 2c 20 63  ts3Hash *pNew, c
35159 68 61 72 20 6b 65 79 43 6c 61 73 73 2c 20 63 68  har keyClass, ch
3515a 61 72 20 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c  ar copyKey);.SQL
3515b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3515c 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
3515d 68 49 6e 73 65 72 74 28 46 74 73 33 48 61 73 68  hInsert(Fts3Hash
3515e 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
3515f 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76  Key, int nKey, v
35160 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c  oid *pData);.SQL
35161 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35162 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
35163 68 46 69 6e 64 28 63 6f 6e 73 74 20 46 74 73 33  hFind(const Fts3
35164 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  Hash*, const voi
35165 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
35166 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
35167 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
35168 74 73 33 48 61 73 68 43 6c 65 61 72 28 46 74 73  ts3HashClear(Fts
35169 33 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  3Hash*);../*.** 
3516a 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20 74 68  Shorthand for th
3516b 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76  e functions abov
3516c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74 73  e.*/.#define fts
3516d 33 48 61 73 68 49 6e 69 74 20 20 20 73 71 6c 69  3HashInit   sqli
3516e 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 0a  te3Fts3HashInit.
3516f 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
35170 49 6e 73 65 72 74 20 73 71 6c 69 74 65 33 46 74  Insert sqlite3Ft
35171 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23 64 65  s3HashInsert.#de
35172 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 6e  fine fts3HashFin
35173 64 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48  d   sqlite3Fts3H
35174 61 73 68 46 69 6e 64 0a 23 64 65 66 69 6e 65 20  ashFind.#define 
35175 66 74 73 33 48 61 73 68 43 6c 65 61 72 20 20 73  fts3HashClear  s
35176 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
35177 65 61 72 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  ear../*.** Macro
35178 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  s for looping ov
35179 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  er all elements 
3517a 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e  of a hash table.
3517b 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a    The idiom is.*
3517c 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  * like this:.**.
3517d 2a 2a 20 20 20 46 74 73 33 48 61 73 68 20 68 3b  **   Fts3Hash h;
3517e 0a 2a 2a 20 20 20 46 74 73 33 48 61 73 68 45 6c  .**   Fts3HashEl
3517f 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a  em *p;.**   ....
35180 2a 2a 20 20 20 66 6f 72 28 70 3d 66 74 73 33 48  **   for(p=fts3H
35181 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 3b  ashFirst(&h); p;
35182 20 70 3d 66 74 73 33 48 61 73 68 4e 65 78 74 28   p=fts3HashNext(
35183 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65  p)){.**     Some
35184 53 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61  Structure *pData
35185 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61 28   = fts3HashData(
35186 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f  p);.**     // do
35187 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20   something with 
35188 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a  pData.**   }.*/.
35189 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
3518a 46 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e  First(H)  ((H)->
3518b 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 66  first).#define f
3518c 74 73 33 48 61 73 68 4e 65 78 74 28 45 29 20 20  ts3HashNext(E)  
3518d 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65   ((E)->next).#de
3518e 66 69 6e 65 20 66 74 73 33 48 61 73 68 44 61 74  fine fts3HashDat
3518f 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74  a(E)   ((E)->dat
35190 61 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  a).#define fts3H
35191 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45  ashKey(E)    ((E
35192 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e 65  )->pKey).#define
35193 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65   fts3HashKeysize
35194 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a  (E) ((E)->nKey).
35195 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
35196 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
35197 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66  sh table.*/.#def
35198 69 6e 65 20 66 74 73 33 48 61 73 68 43 6f 75 6e  ine fts3HashCoun
35199 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e  t(H)  ((H)->coun
3519a 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46  t)..#endif /* _F
3519b 54 53 33 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a  TS3_HASH_H_ */..
3519c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3519d 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68  End of fts3_hash
3519e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
3519f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
351a1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
351a2 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
351a3 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
351a4 66 74 73 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  fts3Int.h ******
351a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
351a6 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 73  ./*.** This cons
351a7 74 61 6e 74 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  tant controls ho
351a8 77 20 6f 66 74 65 6e 20 73 65 67 6d 65 6e 74 73  w often segments
351a9 20 61 72 65 20 6d 65 72 67 65 64 2e 20 4f 6e 63   are merged. Onc
351aa 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 46  e there are.** F
351ab 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20  TS3_MERGE_COUNT 
351ac 73 65 67 6d 65 6e 74 73 20 6f 66 20 6c 65 76 65  segments of leve
351ad 6c 20 4e 2c 20 74 68 65 79 20 61 72 65 20 6d 65  l N, they are me
351ae 72 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  rged into a sing
351af 6c 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6f 66  le.** segment of
351b0 20 6c 65 76 65 6c 20 4e 2b 31 2e 0a 2a 2f 0a 23   level N+1..*/.#
351b1 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 45 52 47  define FTS3_MERG
351b2 45 5f 43 4f 55 4e 54 20 31 36 0a 0a 2f 2a 0a 2a  E_COUNT 16../*.*
351b3 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6d 61  * This is the ma
351b4 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  ximum amount of 
351b5 64 61 74 61 20 28 69 6e 20 62 79 74 65 73 29 20  data (in bytes) 
351b6 74 6f 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20  to store in the 
351b7 0a 2a 2a 20 46 74 73 33 54 61 62 6c 65 2e 70 65  .** Fts3Table.pe
351b8 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 68 20  ndingTerms hash 
351b9 74 61 62 6c 65 2e 20 4e 6f 72 6d 61 6c 6c 79 2c  table. Normally,
351ba 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
351bb 69 73 0a 2a 2a 20 70 6f 70 75 6c 61 74 65 64 20  is.** populated 
351bc 61 73 20 64 6f 63 75 6d 65 6e 74 73 20 61 72 65  as documents are
351bd 20 69 6e 73 65 72 74 65 64 2f 75 70 64 61 74 65   inserted/update
351be 64 2f 64 65 6c 65 74 65 64 20 69 6e 20 61 20 74  d/deleted in a t
351bf 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 61 6e  ransaction.** an
351c0 64 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  d used to create
351c1 20 61 20 6e 65 77 20 73 65 67 6d 65 6e 74 20 77   a new segment w
351c2 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
351c3 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
351c4 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 20  ..** However if 
351c5 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 72 65  this limit is re
351c6 61 63 68 65 64 20 6d 69 64 77 61 79 20 74 68 72  ached midway thr
351c7 6f 75 67 68 20 61 20 74 72 61 6e 73 61 63 74 69  ough a transacti
351c8 6f 6e 2c 20 61 20 6e 65 77 20 0a 2a 2a 20 73 65  on, a new .** se
351c9 67 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64  gment is created
351ca 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61   and the hash ta
351cb 62 6c 65 20 63 6c 65 61 72 65 64 20 69 6d 6d 65  ble cleared imme
351cc 64 69 61 74 65 6c 79 2e 0a 2a 2f 0a 23 64 65 66  diately..*/.#def
351cd 69 6e 65 20 46 54 53 33 5f 4d 41 58 5f 50 45 4e  ine FTS3_MAX_PEN
351ce 44 49 4e 47 5f 44 41 54 41 20 28 31 2a 31 30 32  DING_DATA (1*102
351cf 34 2a 31 30 32 34 29 0a 0a 2f 2a 0a 2a 2a 20 4d  4*1024)../*.** M
351d0 61 63 72 6f 20 74 6f 20 72 65 74 75 72 6e 20 74  acro to return t
351d1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65  he number of ele
351d2 6d 65 6e 74 73 20 69 6e 20 61 6e 20 61 72 72 61  ments in an arra
351d3 79 2e 20 53 51 4c 69 74 65 20 68 61 73 20 61 0a  y. SQLite has a.
351d4 2a 2a 20 73 69 6d 69 6c 61 72 20 6d 61 63 72 6f  ** similar macro
351d5 20 63 61 6c 6c 65 64 20 41 72 72 61 79 53 69 7a   called ArraySiz
351d6 65 28 29 2e 20 55 73 65 20 61 20 64 69 66 66 65  e(). Use a diffe
351d7 72 65 6e 74 20 6e 61 6d 65 20 74 6f 20 61 76 6f  rent name to avo
351d8 69 64 0a 2a 2a 20 61 20 63 6f 6c 6c 69 73 69 6f  id.** a collisio
351d9 6e 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  n when building 
351da 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
351db 77 69 74 68 20 62 75 69 6c 74 2d 69 6e 20 46 54  with built-in FT
351dc 53 33 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  S3..*/.#define S
351dd 69 7a 65 6f 66 41 72 72 61 79 28 58 29 20 28 28  izeofArray(X) ((
351de 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73  int)(sizeof(X)/s
351df 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f  izeof(X[0])))../
351e0 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e  *.** Maximum len
351e1 67 74 68 20 6f 66 20 61 20 76 61 72 69 6e 74 20  gth of a varint 
351e2 65 6e 63 6f 64 65 64 20 69 6e 74 65 67 65 72 2e  encoded integer.
351e3 20 54 68 65 20 76 61 72 69 6e 74 20 66 6f 72 6d   The varint form
351e4 61 74 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a  at is different.
351e5 2a 2a 20 66 72 6f 6d 20 74 68 61 74 20 75 73 65  ** from that use
351e6 64 20 62 79 20 53 51 4c 69 74 65 2c 20 73 6f 20  d by SQLite, so 
351e7 74 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  the maximum leng
351e8 74 68 20 69 73 20 31 30 2c 20 6e 6f 74 20 39 2e  th is 10, not 9.
351e9 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33  .*/.#define FTS3
351ea 5f 56 41 52 49 4e 54 5f 4d 41 58 20 31 30 0a 0a  _VARINT_MAX 10..
351eb 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69  /*.** This secti
351ec 6f 6e 20 70 72 6f 76 69 64 65 73 20 64 65 66 69  on provides defi
351ed 6e 69 74 69 6f 6e 73 20 74 6f 20 61 6c 6c 6f 77  nitions to allow
351ee 20 74 68 65 0a 2a 2a 20 46 54 53 33 20 65 78 74   the.** FTS3 ext
351ef 65 6e 73 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6d  ension to be com
351f0 70 69 6c 65 64 20 6f 75 74 73 69 64 65 20 6f 66  piled outside of
351f1 20 74 68 65 20 0a 2a 2a 20 61 6d 61 6c 67 61 6d   the .** amalgam
351f2 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ation..*/.#ifnde
351f3 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
351f4 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ATION./*.** Macr
351f5 6f 73 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  os indicating th
351f6 61 74 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65  at conditional e
351f7 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61  xpressions are a
351f8 6c 77 61 79 73 20 74 72 75 65 20 6f 72 0a 2a 2a  lways true or.**
351f9 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 20 64 65 66   false..*/.# def
351fa 69 6e 65 20 41 4c 57 41 59 53 28 78 29 20 28 78  ine ALWAYS(x) (x
351fb 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
351fc 28 58 29 20 20 28 78 29 0a 2f 2a 0a 2a 2a 20 49  (X)  (x)./*.** I
351fd 6e 74 65 72 6e 61 6c 20 74 79 70 65 73 20 75 73  nternal types us
351fe 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f  ed by SQLite..*/
351ff 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
35200 64 20 63 68 61 72 20 75 38 3b 20 20 20 20 20 20  d char u8;      
35201 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 28 6f 72     /* 1-byte (or
35202 20 6c 61 72 67 65 72 29 20 75 6e 73 69 67 6e 65   larger) unsigne
35203 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
35204 65 64 65 66 20 73 68 6f 72 74 20 69 6e 74 20 69  edef short int i
35205 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
35206 2a 20 32 2d 62 79 74 65 20 28 6f 72 20 6c 61 72  * 2-byte (or lar
35207 67 65 72 29 20 73 69 67 6e 65 64 20 69 6e 74 65  ger) signed inte
35208 67 65 72 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4d 61 63  ger */./*.** Mac
35209 72 6f 20 75 73 65 64 20 74 6f 20 73 75 70 70 72  ro used to suppr
3520a 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ess compiler war
3520b 6e 69 6e 67 73 20 66 6f 72 20 75 6e 75 73 65 64  nings for unused
3520c 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2f 0a   parameters..*/.
3520d 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50  #define UNUSED_P
3520e 41 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69  ARAMETER(x) (voi
3520f 64 29 28 78 29 0a 23 65 6e 64 69 66 0a 0a 74 79  d)(x).#endif..ty
35210 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73  pedef struct Fts
35211 33 54 61 62 6c 65 20 46 74 73 33 54 61 62 6c 65  3Table Fts3Table
35212 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
35213 20 46 74 73 33 43 75 72 73 6f 72 20 46 74 73 33   Fts3Cursor Fts3
35214 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20  Cursor;.typedef 
35215 73 74 72 75 63 74 20 46 74 73 33 45 78 70 72 20  struct Fts3Expr 
35216 46 74 73 33 45 78 70 72 3b 0a 74 79 70 65 64 65  Fts3Expr;.typede
35217 66 20 73 74 72 75 63 74 20 46 74 73 33 50 68 72  f struct Fts3Phr
35218 61 73 65 20 46 74 73 33 50 68 72 61 73 65 3b 0a  ase Fts3Phrase;.
35219 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
3521a 74 73 33 53 65 67 52 65 61 64 65 72 20 46 74 73  ts3SegReader Fts
3521b 33 53 65 67 52 65 61 64 65 72 3b 0a 74 79 70 65  3SegReader;.type
3521c 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33 53  def struct Fts3S
3521d 65 67 46 69 6c 74 65 72 20 46 74 73 33 53 65 67  egFilter Fts3Seg
3521e 46 69 6c 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41  Filter;../*.** A
3521f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61   connection to a
35220 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20   fulltext index 
35221 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
35222 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
35223 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ** structure. Th
35224 65 20 78 43 72 65 61 74 65 20 61 6e 64 20 78 43  e xCreate and xC
35225 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 63  onnect methods c
35226 72 65 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63  reate an instanc
35227 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72  e.** of this str
35228 75 63 74 75 72 65 20 61 6e 64 20 78 44 65 73 74  ucture and xDest
35229 72 6f 79 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e  roy and xDisconn
3522a 65 63 74 20 66 72 65 65 20 74 68 61 74 20 69 6e  ect free that in
3522b 73 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f  stance..** All o
3522c 74 68 65 72 20 6d 65 74 68 6f 64 73 20 72 65 63  ther methods rec
3522d 65 69 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74  eive a pointer t
3522e 6f 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  o the structure 
3522f 61 73 20 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a  as one of their.
35230 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f  ** arguments..*/
35231 0a 73 74 72 75 63 74 20 46 74 73 33 54 61 62 6c  .struct Fts3Tabl
35232 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  e {.  sqlite3_vt
35233 61 62 20 62 61 73 65 3b 20 20 20 20 20 20 20 20  ab base;        
35234 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c        /* Base cl
35235 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c 69  ass used by SQLi
35236 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c  te core */.  sql
35237 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
35238 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35239 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
3523a 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e  nection */.  con
3523b 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
3523c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3523d 6c 6f 67 69 63 61 6c 20 64 61 74 61 62 61 73 65  logical database
3523e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
3523f 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
35240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 69             /* vi
35241 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65  rtual table name
35242 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
35243 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
35244 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20        /* number 
35245 6f 66 20 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 73  of named columns
35246 20 69 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c   in virtual tabl
35247 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  e */.  char **az
35248 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
35249 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e         /* column
3524a 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65   names.  malloce
3524b 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74  d */.  sqlite3_t
3524c 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3524d 69 7a 65 72 3b 20 20 2f 2a 20 74 6f 6b 65 6e 69  izer;  /* tokeni
3524e 7a 65 72 20 66 6f 72 20 69 6e 73 65 72 74 73 20  zer for inserts 
3524f 61 6e 64 20 71 75 65 72 69 65 73 20 2a 2f 0a 0a  and queries */..
35250 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64    /* Precompiled
35251 20 73 74 61 74 65 6d 65 6e 74 73 20 75 73 65 64   statements used
35252 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   by the implemen
35253 74 61 74 69 6f 6e 2e 20 45 61 63 68 20 6f 66 20  tation. Each of 
35254 74 68 65 73 65 20 0a 20 20 2a 2a 20 73 74 61 74  these .  ** stat
35255 65 6d 65 6e 74 73 20 69 73 20 72 75 6e 20 61 6e  ements is run an
35256 64 20 72 65 73 65 74 20 77 69 74 68 69 6e 20 61  d reset within a
35257 20 73 69 6e 67 6c 65 20 76 69 72 74 75 61 6c 20   single virtual 
35258 74 61 62 6c 65 20 41 50 49 20 63 61 6c 6c 2e 20  table API call. 
35259 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
3525a 73 74 6d 74 20 2a 61 53 74 6d 74 5b 31 38 5d 3b  stmt *aStmt[18];
3525b 0a 0a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74  ..  /* Pointer t
3525c 6f 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  o string contain
3525d 69 6e 67 20 74 68 65 20 53 51 4c 3a 0a 20 20 2a  ing the SQL:.  *
3525e 2a 0a 20 20 2a 2a 20 22 53 45 4c 45 43 54 20 62  *.  ** "SELECT b
3525f 6c 6f 63 6b 20 46 52 4f 4d 20 25 5f 73 65 67 6d  lock FROM %_segm
35260 65 6e 74 73 20 57 48 45 52 45 20 62 6c 6f 63 6b  ents WHERE block
35261 69 64 20 42 45 54 57 45 45 4e 20 3f 20 41 4e 44  id BETWEEN ? AND
35262 20 3f 20 0a 20 20 2a 2a 20 20 20 20 4f 52 44 45   ? .  **    ORDE
35263 52 20 42 59 20 62 6c 6f 63 6b 69 64 22 0a 20 20  R BY blockid".  
35264 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 65 6c 65  */.  char *zSele
35265 63 74 4c 65 61 76 65 73 3b 0a 20 20 69 6e 74 20  ctLeaves;.  int 
35266 6e 4c 65 61 76 65 73 53 74 6d 74 3b 20 20 20 20  nLeavesStmt;    
35267 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
35268 61 6c 69 64 20 73 74 61 74 65 6d 65 6e 74 73 20  alid statements 
35269 69 6e 20 61 4c 65 61 76 65 73 53 74 6d 74 20 2a  in aLeavesStmt *
3526a 2f 0a 20 20 69 6e 74 20 6e 4c 65 61 76 65 73 54  /.  int nLeavesT
3526b 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  otal;           
3526c 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
3526d 62 65 72 20 6f 66 20 70 72 65 70 61 72 65 64 20  ber of prepared 
3526e 6c 65 61 76 65 73 20 73 74 6d 74 73 20 2a 2f 0a  leaves stmts */.
3526f 20 20 69 6e 74 20 6e 4c 65 61 76 65 73 41 6c 6c    int nLeavesAll
35270 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  oc;             
35271 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
35272 69 7a 65 20 6f 66 20 61 4c 65 61 76 65 73 53 74  ize of aLeavesSt
35273 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  mt */.  sqlite3_
35274 73 74 6d 74 20 2a 2a 61 4c 65 61 76 65 73 53 74  stmt **aLeavesSt
35275 6d 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79  mt;     /* Array
35276 20 6f 66 20 70 72 65 70 61 72 65 64 20 7a 53 65   of prepared zSe
35277 6c 65 63 74 4c 65 61 76 65 73 20 73 74 6d 74 73  lectLeaves stmts
35278 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4e 6f 64 65   */..  int nNode
35279 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
3527a 20 20 20 20 20 20 20 2f 2a 20 53 6f 66 74 20 6c         /* Soft l
3527b 69 6d 69 74 20 66 6f 72 20 6e 6f 64 65 20 73 69  imit for node si
3527c 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  ze */..  /* The 
3527d 66 6f 6c 6c 6f 77 69 6e 67 20 68 61 73 68 20 74  following hash t
3527e 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
3527f 62 75 66 66 65 72 20 70 65 6e 64 69 6e 67 20 69  buffer pending i
35280 6e 64 65 78 20 75 70 64 61 74 65 73 20 64 75 72  ndex updates dur
35281 69 6e 67 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  ing.  ** transac
35282 74 69 6f 6e 73 2e 20 56 61 72 69 61 62 6c 65 20  tions. Variable 
35283 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73 74  nPendingData est
35284 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f 72  imates the memor
35285 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 20  y size of the . 
35286 20 2a 2a 20 70 65 6e 64 69 6e 67 20 64 61 74 61   ** pending data
35287 2c 20 69 6e 63 6c 75 64 69 6e 67 20 68 61 73 68  , including hash
35288 20 74 61 62 6c 65 20 6f 76 65 72 68 65 61 64 2c   table overhead,
35289 20 62 75 74 20 6e 6f 74 20 6d 61 6c 6c 6f 63 20   but not malloc 
3528a 6f 76 65 72 68 65 61 64 2e 20 0a 20 20 2a 2a 20  overhead. .  ** 
3528b 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74  When nPendingDat
3528c 61 20 65 78 63 65 65 64 73 20 46 54 53 33 5f 4d  a exceeds FTS3_M
3528d 41 58 5f 50 45 4e 44 49 4e 47 5f 44 41 54 41 2c  AX_PENDING_DATA,
3528e 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 66   the buffer is f
3528f 6c 75 73 68 65 64 20 0a 20 20 2a 2a 20 61 75 74  lushed .  ** aut
35290 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 56 61 72 69  omatically. Vari
35291 61 62 6c 65 20 69 50 72 65 76 44 6f 63 69 64 20  able iPrevDocid 
35292 69 73 20 74 68 65 20 64 6f 63 69 64 20 6f 66 20  is the docid of 
35293 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
35294 79 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 64 20  y.  ** inserted 
35295 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 69  record..  */.  i
35296 6e 74 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b  nt nPendingData;
35297 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
35298 69 50 72 65 76 44 6f 63 69 64 3b 0a 20 20 46 74  iPrevDocid;.  Ft
35299 73 33 48 61 73 68 20 70 65 6e 64 69 6e 67 54 65  s3Hash pendingTe
3529a 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57  rms;.};../*.** W
3529b 68 65 6e 20 74 68 65 20 63 6f 72 65 20 77 61 6e  hen the core wan
3529c 74 73 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  ts to read from 
3529d 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
3529e 65 2c 20 69 74 20 63 72 65 61 74 65 73 20 61 0a  e, it creates a.
3529f 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
352a0 20 63 75 72 73 6f 72 20 28 61 6e 20 69 6e 73 74   cursor (an inst
352a1 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
352a2 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 29  owing structure)
352a3 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 4f   using.** the xO
352a4 70 65 6e 20 6d 65 74 68 6f 64 2e 20 43 75 72 73  pen method. Curs
352a5 6f 72 73 20 61 72 65 20 64 65 73 74 72 6f 79 65  ors are destroye
352a6 64 20 75 73 69 6e 67 20 74 68 65 20 78 43 6c 6f  d using the xClo
352a7 73 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  se method..*/.st
352a8 72 75 63 74 20 46 74 73 33 43 75 72 73 6f 72 20  ruct Fts3Cursor 
352a9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
352aa 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 20 20 20  _cursor base;   
352ab 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
352ac 73 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  s used by SQLite
352ad 20 63 6f 72 65 20 2a 2f 0a 20 20 69 31 36 20 65   core */.  i16 e
352ae 53 65 61 72 63 68 3b 20 20 20 20 20 20 20 20 20  Search;         
352af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
352b0 61 72 63 68 20 73 74 72 61 74 65 67 79 20 28 73  arch strategy (s
352b1 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 75  ee below) */.  u
352b2 38 20 69 73 45 6f 66 3b 20 20 20 20 20 20 20 20  8 isEof;        
352b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
352b4 2a 20 54 72 75 65 20 69 66 20 61 74 20 45 6e 64  * True if at End
352b5 20 4f 66 20 52 65 73 75 6c 74 73 20 2a 2f 0a 20   Of Results */. 
352b6 20 75 38 20 69 73 52 65 71 75 69 72 65 53 65 65   u8 isRequireSee
352b7 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
352b8 20 2f 2a 20 54 72 75 65 20 69 66 20 6d 75 73 74   /* True if must
352b9 20 73 65 65 6b 20 70 53 74 6d 74 20 74 6f 20 25   seek pStmt to %
352ba 5f 63 6f 6e 74 65 6e 74 20 72 6f 77 20 2a 2f 0a  _content row */.
352bb 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
352bc 70 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20  pStmt;          
352bd 20 20 2f 2a 20 50 72 65 70 61 72 65 64 20 73 74    /* Prepared st
352be 61 74 65 6d 65 6e 74 20 69 6e 20 75 73 65 20 62  atement in use b
352bf 79 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a  y the cursor */.
352c0 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70    Fts3Expr *pExp
352c1 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
352c2 20 20 2f 2a 20 50 61 72 73 65 64 20 4d 41 54 43    /* Parsed MATC
352c3 48 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 2a  H query string *
352c4 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
352c5 34 20 69 50 72 65 76 49 64 3b 20 20 20 20 20 20  4 iPrevId;      
352c6 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
352c7 69 64 20 72 65 61 64 20 66 72 6f 6d 20 61 44 6f  id read from aDo
352c8 63 6c 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20  clist */.  char 
352c9 2a 70 4e 65 78 74 49 64 3b 20 20 20 20 20 20 20  *pNextId;       
352ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
352cb 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 62  inter into the b
352cc 6f 64 79 20 6f 66 20 61 44 6f 63 6c 69 73 74 20  ody of aDoclist 
352cd 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 6f 63 6c  */.  char *aDocl
352ce 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
352cf 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
352d0 64 6f 63 69 64 73 20 66 6f 72 20 66 75 6c 6c 2d  docids for full-
352d1 74 65 78 74 20 71 75 65 72 69 65 73 20 2a 2f 0a  text queries */.
352d2 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20    int nDoclist; 
352d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
352d4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
352d5 66 65 72 20 61 74 20 61 44 6f 63 6c 69 73 74 20  fer at aDoclist 
352d6 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
352d7 20 46 74 73 33 43 75 72 73 6f 72 2e 65 53 65 61   Fts3Cursor.eSea
352d8 72 63 68 20 6d 65 6d 62 65 72 20 69 73 20 61 6c  rch member is al
352d9 77 61 79 73 20 73 65 74 20 74 6f 20 6f 6e 65 20  ways set to one 
352da 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
352db 2e 0a 2a 2a 20 41 63 74 75 61 6c 79 2c 20 46 74  ..** Actualy, Ft
352dc 73 33 43 75 72 73 6f 72 2e 65 53 65 61 72 63 68  s3Cursor.eSearch
352dd 20 63 61 6e 20 62 65 20 67 72 65 61 74 65 72 20   can be greater 
352de 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
352df 0a 2a 2a 20 46 54 53 33 5f 46 55 4c 4c 54 45 58  .** FTS3_FULLTEX
352e0 54 5f 53 45 41 52 43 48 2e 20 20 49 66 20 73 6f  T_SEARCH.  If so
352e1 2c 20 74 68 65 6e 20 46 74 73 33 43 75 72 73 6f  , then Fts3Curso
352e2 72 2e 65 53 65 61 72 63 68 20 2d 20 32 20 69 73  r.eSearch - 2 is
352e3 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 6f 66   the index.** of
352e4 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 6f 20 62   the column to b
352e5 65 20 73 65 61 72 63 68 65 64 2e 20 20 46 6f 72  e searched.  For
352e6 20 65 78 61 6d 70 6c 65 2c 20 69 6e 0a 2a 2a 0a   example, in.**.
352e7 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49  **     CREATE VI
352e8 52 54 55 41 4c 20 54 41 42 4c 45 20 65 78 31 20  RTUAL TABLE ex1 
352e9 55 53 49 4e 47 20 66 74 73 33 28 61 2c 62 2c 63  USING fts3(a,b,c
352ea 2c 64 29 3b 0a 2a 2a 20 20 20 20 20 53 45 4c 45  ,d);.**     SELE
352eb 43 54 20 64 6f 63 69 64 20 46 52 4f 4d 20 65 78  CT docid FROM ex
352ec 31 20 57 48 45 52 45 20 62 20 4d 41 54 43 48 20  1 WHERE b MATCH 
352ed 27 6f 6e 65 20 74 77 6f 20 74 68 72 65 65 27 3b  'one two three';
352ee 0a 2a 2a 20 0a 2a 2a 20 42 65 63 61 75 73 65 20  .** .** Because 
352ef 74 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 4d  the LHS of the M
352f0 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20 69 73  ATCH operator is
352f1 20 32 6e 64 20 63 6f 6c 75 6d 6e 20 22 62 22 2c   2nd column "b",
352f2 0a 2a 2a 20 46 74 73 33 43 75 72 73 6f 72 2e 65  .** Fts3Cursor.e
352f3 53 65 61 72 63 68 20 77 69 6c 6c 20 62 65 20 73  Search will be s
352f4 65 74 20 74 6f 20 46 54 53 33 5f 46 55 4c 4c 54  et to FTS3_FULLT
352f5 45 58 54 5f 53 45 41 52 43 48 2b 31 2e 20 20 28  EXT_SEARCH+1.  (
352f6 2b 30 20 66 6f 72 20 61 2c 0a 2a 2a 20 2b 31 20  +0 for a,.** +1 
352f7 66 6f 72 20 62 2c 20 2b 32 20 66 6f 72 20 63 2c  for b, +2 for c,
352f8 20 2b 33 20 66 6f 72 20 64 2e 29 20 20 49 66 20   +3 for d.)  If 
352f9 74 68 65 20 4c 48 53 20 6f 66 20 4d 41 54 43 48  the LHS of MATCH
352fa 20 77 65 72 65 20 22 65 78 31 22 20 0a 2a 2a 20   were "ex1" .** 
352fb 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
352fc 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 73 68 6f 75  all columns shou
352fd 6c 64 20 62 65 20 73 65 61 72 63 68 65 64 2c 0a  ld be searched,.
352fe 2a 2a 20 74 68 65 6e 20 65 53 65 61 72 63 68 20  ** then eSearch 
352ff 77 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  would be set to 
35300 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
35301 41 52 43 48 2b 34 2e 0a 2a 2f 0a 23 64 65 66 69  ARCH+4..*/.#defi
35302 6e 65 20 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e  ne FTS3_FULLSCAN
35303 5f 53 45 41 52 43 48 20 30 20 20 20 20 2f 2a 20  _SEARCH 0    /* 
35304 4c 69 6e 65 61 72 20 73 63 61 6e 20 6f 66 20 25  Linear scan of %
35305 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a  _content table *
35306 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 44  /.#define FTS3_D
35307 4f 43 49 44 5f 53 45 41 52 43 48 20 20 20 20 31  OCID_SEARCH    1
35308 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 62 79      /* Lookup by
35309 20 72 6f 77 69 64 20 6f 6e 20 25 5f 63 6f 6e 74   rowid on %_cont
3530a 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  ent table */.#de
3530b 66 69 6e 65 20 46 54 53 33 5f 46 55 4c 4c 54 45  fine FTS3_FULLTE
3530c 58 54 5f 53 45 41 52 43 48 20 32 20 20 20 20 2f  XT_SEARCH 2    /
3530d 2a 20 46 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  * Full-text inde
3530e 78 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f 2a 0a  x search */../*.
3530f 2a 2a 20 41 20 22 70 68 72 61 73 65 22 20 69 73  ** A "phrase" is
35310 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f   a sequence of o
35311 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e  ne or more token
35312 73 20 74 68 61 74 20 6d 75 73 74 20 6d 61 74 63  s that must matc
35313 68 20 69 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  h in.** sequence
35314 2e 20 20 41 20 73 69 6e 67 6c 65 20 74 6f 6b 65  .  A single toke
35315 6e 20 69 73 20 74 68 65 20 62 61 73 65 20 63 61  n is the base ca
35316 73 65 20 61 6e 64 20 74 68 65 20 6d 6f 73 74 20  se and the most 
35317 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 20  common case..** 
35318 46 6f 72 20 61 20 73 65 71 75 65 6e 63 65 20 6f  For a sequence o
35319 66 20 74 6f 6b 65 6e 73 20 63 6f 6e 74 61 69 6e  f tokens contain
3531a 65 64 20 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54 6f  ed in "...", nTo
3531b 6b 65 6e 20 77 69 6c 6c 20 62 65 20 74 68 65 20  ken will be the 
3531c 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 6f 6b  number.** of tok
3531d 65 6e 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e  ens in the strin
3531e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73  g..*/.struct Fts
3531f 33 50 68 72 61 73 65 20 7b 0a 20 20 69 6e 74 20  3Phrase {.  int 
35320 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  nToken;         
35321 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
35322 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68   of tokens in th
35323 65 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e  e phrase */.  in
35324 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
35325 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
35326 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 74 68 69 73  x of column this
35327 20 70 68 72 61 73 65 20 6d 75 73 74 20 6d 61 74   phrase must mat
35328 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4e 6f  ch */.  int isNo
35329 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3532a 20 20 20 2f 2a 20 50 68 72 61 73 65 20 70 72 65     /* Phrase pre
3532b 66 69 78 65 64 20 62 79 20 75 6e 61 72 79 20 6e  fixed by unary n
3532c 6f 74 20 28 2d 29 20 6f 70 65 72 61 74 6f 72 20  ot (-) operator 
3532d 2a 2f 0a 20 20 73 74 72 75 63 74 20 50 68 72 61  */.  struct Phra
3532e 73 65 54 6f 6b 65 6e 20 7b 0a 20 20 20 20 63 68  seToken {.    ch
3532f 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
35330 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
35331 66 20 74 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20  f the token */. 
35332 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20     int n;       
35333 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
35334 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
35335 6e 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64  n buffer pointed
35336 20 74 6f 20 62 79 20 7a 20 2a 2f 0a 20 20 20 20   to by z */.    
35337 69 6e 74 20 69 73 50 72 65 66 69 78 3b 20 20 20  int isPrefix;   
35338 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
35339 20 69 66 20 74 6f 6b 65 6e 20 65 6e 64 73 20 69   if token ends i
3533a 6e 20 77 69 74 68 20 61 20 22 2a 22 20 63 68 61  n with a "*" cha
3533b 72 61 63 74 65 72 20 2a 2f 0a 20 20 7d 20 61 54  racter */.  } aT
3533c 6f 6b 65 6e 5b 31 5d 3b 20 20 20 20 20 20 20 20  oken[1];        
3533d 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e         /* One en
3533e 74 72 79 20 66 6f 72 20 65 61 63 68 20 74 6f 6b  try for each tok
3533f 65 6e 20 69 6e 20 74 68 65 20 70 68 72 61 73 65  en in the phrase
35340 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
35341 74 72 65 65 20 6f 66 20 74 68 65 73 65 20 6f 62  tree of these ob
35342 6a 65 63 74 73 20 66 6f 72 6d 73 20 74 68 65 20  jects forms the 
35343 52 48 53 20 6f 66 20 61 20 4d 41 54 43 48 20 6f  RHS of a MATCH o
35344 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75  perator..*/.stru
35345 63 74 20 46 74 73 33 45 78 70 72 20 7b 0a 20 20  ct Fts3Expr {.  
35346 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20 20 20  int eType;      
35347 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
35348 65 20 6f 66 20 74 68 65 20 46 54 53 51 55 45 52  e of the FTSQUER
35349 59 5f 58 58 58 20 76 61 6c 75 65 73 20 64 65 66  Y_XXX values def
3534a 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  ined below */.  
3534b 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20 20 20 20  int nNear;      
3534c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
3534d 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54  lid if eType==FT
3534e 53 51 55 45 52 59 5f 4e 45 41 52 20 2a 2f 0a 20  SQUERY_NEAR */. 
3534f 20 46 74 73 33 45 78 70 72 20 2a 70 50 61 72 65   Fts3Expr *pPare
35350 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 70  nt;         /* p
35351 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74  Parent->pLeft==t
35352 68 69 73 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e  his or pParent->
35353 70 52 69 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0a  pRight==this */.
35354 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65 66    Fts3Expr *pLef
35355 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
35356 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  Left operand */.
35357 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 69 67    Fts3Expr *pRig
35358 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ht;          /* 
35359 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  Right operand */
3535a 0a 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70  .  Fts3Phrase *p
3535b 50 68 72 61 73 65 3b 20 20 20 20 20 20 20 2f 2a  Phrase;       /*
3535c 20 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d   Valid if eType=
3535d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
3535e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61   */.};../*.** Ca
3535f 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66  ndidate values f
35360 6f 72 20 46 74 73 33 51 75 65 72 79 2e 65 54 79  or Fts3Query.eTy
35361 70 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68  pe. Note that th
35362 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 66  e order of the f
35363 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20 76 61 6c  irst.** four val
35364 75 65 73 20 69 73 20 69 6e 20 6f 72 64 65 72 20  ues is in order 
35365 6f 66 20 70 72 65 63 65 64 65 6e 63 65 20 77 68  of precedence wh
35366 65 6e 20 70 61 72 73 69 6e 67 20 65 78 70 72 65  en parsing expre
35367 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a 20  ssions. For .** 
35368 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c  example, the fol
35369 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
3536a 22 61 20 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f  "a OR b AND c NO
3536b 54 20 64 20 4e 45 41 52 20 65 22 0a 2a 2a 0a 2a  T d NEAR e".**.*
3536c 2a 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  * is equivalent 
3536d 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f  to:.**.**   "a O
3536e 52 20 28 62 20 41 4e 44 20 28 63 20 4e 4f 54 20  R (b AND (c NOT 
3536f 28 64 20 4e 45 41 52 20 65 29 29 29 22 0a 2a 2f  (d NEAR e)))".*/
35370 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45 52  .#define FTSQUER
35371 59 5f 4e 45 41 52 20 20 20 31 0a 23 64 65 66 69  Y_NEAR   1.#defi
35372 6e 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 20  ne FTSQUERY_NOT 
35373 20 20 20 32 0a 23 64 65 66 69 6e 65 20 46 54 53     2.#define FTS
35374 51 55 45 52 59 5f 41 4e 44 20 20 20 20 33 0a 23  QUERY_AND    3.#
35375 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f  define FTSQUERY_
35376 4f 52 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65  OR     4.#define
35377 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
35378 20 35 0a 0a 0a 2f 2a 20 66 74 73 33 5f 69 6e 69   5.../* fts3_ini
35379 74 2e 63 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  t.c */.SQLITE_PR
3537a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3537b 33 46 74 73 33 44 65 6c 65 74 65 56 74 61 62 28  3Fts3DeleteVtab(
3537c 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  int, sqlite3_vta
3537d 62 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  b *);.SQLITE_PRI
3537e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3537f 46 74 73 33 49 6e 69 74 56 74 61 62 28 69 6e 74  Fts3InitVtab(int
35380 2c 20 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64  , sqlite3*, void
35381 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
35382 61 72 2a 63 6f 6e 73 74 2a 2c 20 0a 20 20 20 20  ar*const*, .    
35383 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35384 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
35385 20 2a 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 0a   **, char **);..
35386 2f 2a 20 66 74 73 33 5f 77 72 69 74 65 2e 63 20  /* fts3_write.c 
35387 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
35388 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
35389 33 55 70 64 61 74 65 4d 65 74 68 6f 64 28 73 71  3UpdateMethod(sq
3538a 6c 69 74 65 33 5f 76 74 61 62 2a 2c 69 6e 74 2c  lite3_vtab*,int,
3538b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 2c  sqlite3_value**,
3538c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 3b  sqlite3_int64*);
3538d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3538e 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 50  int sqlite3Fts3P
3538f 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
35390 28 46 74 73 33 54 61 62 6c 65 20 2a 29 3b 0a 53  (Fts3Table *);.S
35391 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
35392 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 50 65  id sqlite3Fts3Pe
35393 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28  ndingTermsClear(
35394 46 74 73 33 54 61 62 6c 65 20 2a 29 3b 0a 53 51  Fts3Table *);.SQ
35395 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
35396 20 73 71 6c 69 74 65 33 46 74 73 33 4f 70 74 69   sqlite3Fts3Opti
35397 6d 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20 2a  mize(Fts3Table *
35398 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
35399 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
3539a 33 53 65 67 52 65 61 64 65 72 4e 65 77 28 46 74  3SegReaderNew(Ft
3539b 73 33 54 61 62 6c 65 20 2a 2c 69 6e 74 2c 20 73  s3Table *,int, s
3539c 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 0a 20 20  qlite3_int64,.  
3539d 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 73  sqlite3_int64, s
3539e 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 63 6f  qlite3_int64, co
3539f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
353a0 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2a 2a   Fts3SegReader**
353a1 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
353a2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
353a3 73 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28  s3SegReaderFree(
353a4 46 74 73 33 54 61 62 6c 65 20 2a 2c 20 46 74 73  Fts3Table *, Fts
353a5 33 53 65 67 52 65 61 64 65 72 20 2a 29 3b 0a 53  3SegReader *);.S
353a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
353a7 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67  t sqlite3Fts3Seg
353a8 52 65 61 64 65 72 49 74 65 72 61 74 65 28 0a 20  ReaderIterate(. 
353a9 20 46 74 73 33 54 61 62 6c 65 20 2a 2c 20 46 74   Fts3Table *, Ft
353aa 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 2c 20  s3SegReader **, 
353ab 69 6e 74 2c 20 46 74 73 33 53 65 67 46 69 6c 74  int, Fts3SegFilt
353ac 65 72 20 2a 2c 0a 20 20 69 6e 74 20 28 2a 29 28  er *,.  int (*)(
353ad 46 74 73 33 54 61 62 6c 65 20 2a 2c 20 76 6f 69  Fts3Table *, voi
353ae 64 20 2a 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74  d *, char *, int
353af 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 2c 20  , char *, int), 
353b0 20 76 6f 69 64 20 2a 0a 29 3b 0a 53 51 4c 49 54   void *.);.SQLIT
353b1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
353b2 6c 69 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f  lite3Fts3ReadBlo
353b3 63 6b 28 46 74 73 33 54 61 62 6c 65 2a 2c 20 73  ck(Fts3Table*, s
353b4 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 63 68  qlite3_int64, ch
353b5 61 72 20 63 6f 6e 73 74 2a 2a 2c 20 69 6e 74 2a  ar const**, int*
353b6 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
353b7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
353b8 33 41 6c 6c 53 65 67 64 69 72 73 28 46 74 73 33  3AllSegdirs(Fts3
353b9 54 61 62 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f  Table*, sqlite3_
353ba 73 74 6d 74 20 2a 2a 29 3b 0a 0a 2f 2a 20 46 6c  stmt **);../* Fl
353bb 61 67 73 20 61 6c 6c 6f 77 65 64 20 61 73 20 70  ags allowed as p
353bc 61 72 74 20 6f 66 20 74 68 65 20 34 74 68 20 61  art of the 4th a
353bd 72 67 75 6d 65 6e 74 20 74 6f 20 53 65 67 6d 65  rgument to Segme
353be 6e 74 52 65 61 64 65 72 49 74 65 72 61 74 65 28  ntReaderIterate(
353bf 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53  ) */.#define FTS
353c0 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52  3_SEGMENT_REQUIR
353c1 45 5f 50 4f 53 20 20 20 30 78 30 30 30 30 30 30  E_POS   0x000000
353c2 30 31 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f  01.#define FTS3_
353c3 53 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45  SEGMENT_IGNORE_E
353c4 4d 50 54 59 20 20 30 78 30 30 30 30 30 30 30 32  MPTY  0x00000002
353c5 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53 45  .#define FTS3_SE
353c6 47 4d 45 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c  GMENT_COLUMN_FIL
353c7 54 45 52 20 30 78 30 30 30 30 30 30 30 34 0a 23  TER 0x00000004.#
353c8 64 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47 4d  define FTS3_SEGM
353c9 45 4e 54 5f 50 52 45 46 49 58 20 20 20 20 20 20  ENT_PREFIX      
353ca 20 20 30 78 30 30 30 30 30 30 30 38 0a 0a 2f 2a    0x00000008../*
353cb 20 54 79 70 65 20 70 61 73 73 65 64 20 61 73 20   Type passed as 
353cc 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  4th argument to 
353cd 53 65 67 6d 65 6e 74 52 65 61 64 65 72 49 74 65  SegmentReaderIte
353ce 72 61 74 65 28 29 20 2a 2f 0a 73 74 72 75 63 74  rate() */.struct
353cf 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 7b   Fts3SegFilter {
353d0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
353d1 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72  Term;.  int nTer
353d2 6d 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20  m;.  int iCol;. 
353d3 20 69 6e 74 20 66 6c 61 67 73 3b 0a 7d 3b 0a 0a   int flags;.};..
353d4 2f 2a 20 66 74 73 33 2e 63 20 2a 2f 0a 53 51 4c  /* fts3.c */.SQL
353d5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
353d6 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61  sqlite3Fts3PutVa
353d7 72 69 6e 74 28 63 68 61 72 20 2a 2c 20 73 71 6c  rint(char *, sql
353d8 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c  ite3_int64);.SQL
353d9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
353da 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
353db 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  rint(const char 
353dc 2a 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  *, sqlite_int64 
353dd 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
353de 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
353df 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f  s3GetVarint32(co
353e0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 20  nst char *, int 
353e1 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
353e2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
353e3 73 33 56 61 72 69 6e 74 4c 65 6e 28 73 71 6c 69  s3VarintLen(sqli
353e4 74 65 33 5f 75 69 6e 74 36 34 29 3b 0a 53 51 4c  te3_uint64);.SQL
353e5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
353e6 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71 75   sqlite3Fts3Dequ
353e7 6f 74 65 28 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a  ote(char *);../*
353e8 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
353e9 63 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  c */.SQLITE_PRIV
353ea 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
353eb 73 71 6c 69 74 65 33 46 74 73 33 4e 65 78 74 54  sqlite3Fts3NextT
353ec 6f 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  oken(const char 
353ed 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  *, int *);.SQLIT
353ee 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
353ef 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
353f0 68 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a  hTable(sqlite3 *
353f1 2c 20 46 74 73 33 48 61 73 68 20 2a 2c 20 63 6f  , Fts3Hash *, co
353f2 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
353f3 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
353f4 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 54  sqlite3Fts3InitT
353f5 6f 6b 65 6e 69 7a 65 72 28 46 74 73 33 48 61 73  okenizer(Fts3Has
353f6 68 20 2a 70 48 61 73 68 2c 20 0a 20 20 63 6f 6e  h *pHash, .  con
353f7 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
353f8 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 2c  e3_tokenizer **,
353f9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2c 20   const char **, 
353fa 63 68 61 72 20 2a 2a 0a 29 3b 0a 0a 2f 2a 20 66  char **.);../* f
353fb 74 73 33 5f 73 6e 69 70 70 65 74 2e 63 20 2a 2f  ts3_snippet.c */
353fc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
353fd 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
353fe 4f 66 66 73 65 74 73 28 73 71 6c 69 74 65 33 5f  Offsets(sqlite3_
353ff 63 6f 6e 74 65 78 74 2a 2c 20 46 74 73 33 43 75  context*, Fts3Cu
35400 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
35401 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
35402 74 65 33 46 74 73 33 53 6e 69 70 70 65 74 28 73  te3Fts3Snippet(s
35403 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
35404 20 46 74 73 33 43 75 72 73 6f 72 2a 2c 20 0a 20   Fts3Cursor*, . 
35405 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63   const char *, c
35406 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e  onst char *, con
35407 73 74 20 63 68 61 72 20 2a 0a 29 3b 0a 0a 2f 2a  st char *.);../*
35408 20 66 74 73 33 5f 65 78 70 72 2e 63 20 2a 2f 0a   fts3_expr.c */.
35409 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3540a 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  nt sqlite3Fts3Ex
3540b 70 72 50 61 72 73 65 28 73 71 6c 69 74 65 33 5f  prParse(sqlite3_
3540c 74 6f 6b 65 6e 69 7a 65 72 20 2a 2c 20 0a 20 20  tokenizer *, .  
3540d 63 68 61 72 20 2a 2a 2c 20 69 6e 74 2c 20 69 6e  char **, int, in
3540e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
3540f 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72 20 2a   int, Fts3Expr *
35410 2a 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *.);.SQLITE_PRIV
35411 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
35412 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
35413 33 45 78 70 72 20 2a 29 3b 0a 23 69 66 64 65 66  3Expr *);.#ifdef
35414 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
35415 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35416 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
35417 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61 63  InitTestInterfac
35418 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  e(sqlite3 *db);.
35419 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
3541a 2a 20 5f 46 54 53 49 4e 54 5f 48 20 2a 2f 0a 0a  * _FTSINT_H */..
3541b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3541c 45 6e 64 20 6f 66 20 66 74 73 33 49 6e 74 2e 68  End of fts3Int.h
3541d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3541e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3541f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
35420 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
35421 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
35422 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
35423 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
35424 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
35425 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
35426 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49 54 45 5f  _CORE .  SQLITE_
35427 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
35428 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 20 54 4f 44  #endif..../* TOD
35429 4f 28 73 68 65 73 73 29 20 4d 41 4e 2c 20 74 68  O(shess) MAN, th
3542a 69 73 20 74 68 69 6e 67 20 6e 65 65 64 73 20 73  is thing needs s
3542b 6f 6d 65 20 72 65 66 61 63 74 6f 72 69 6e 67 2e  ome refactoring.
3542c 20 20 41 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74    At minimum, it
3542d 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6e 69 63  .** would be nic
3542e 65 20 74 6f 20 6f 72 64 65 72 20 74 68 65 20 66  e to order the f
3542f 69 6c 65 20 62 65 74 74 65 72 2c 20 70 65 72 68  ile better, perh
35430 61 70 73 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c  aps something al
35431 6f 6e 67 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73  ong the.** lines
35432 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74   of:.**.**  - ut
35433 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a  ility functions.
35434 2a 2a 20 20 2d 20 74 61 62 6c 65 20 73 65 74 75  **  - table setu
35435 70 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  p functions.**  
35436 2d 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 66  - table update f
35437 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74  unctions.**  - t
35438 61 62 6c 65 20 71 75 65 72 79 20 66 75 6e 63 74  able query funct
35439 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74  ions.**.** Put t
3543a 68 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f  he query functio
3543b 6e 73 20 6c 61 73 74 20 62 65 63 61 75 73 65 20  ns last because 
3543c 74 68 65 79 27 72 65 20 6c 69 6b 65 6c 79 20 74  they're likely t
3543d 6f 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74  o reference.** t
3543e 79 70 65 64 65 66 73 20 6f 72 20 66 75 6e 63 74  ypedefs or funct
3543f 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 74 61  ions from the ta
35440 62 6c 65 20 75 70 64 61 74 65 20 73 65 63 74 69  ble update secti
35441 6f 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20  on..*/..#if 0.# 
35442 64 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28  define FTSTRACE(
35443 41 29 20 20 70 72 69 6e 74 66 20 41 3b 20 66 66  A)  printf A; ff
35444 6c 75 73 68 28 73 74 64 6f 75 74 29 0a 23 65 6c  lush(stdout).#el
35445 73 65 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54  se.# define FTST
35446 52 41 43 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a  RACE(A).#endif..
35447 74 79 70 65 64 65 66 20 65 6e 75 6d 20 44 6f 63  typedef enum Doc
35448 4c 69 73 74 54 79 70 65 20 7b 0a 20 20 44 4c 5f  ListType {.  DL_
35449 44 4f 43 49 44 53 2c 20 20 20 20 20 20 20 20 20  DOCIDS,         
3544a 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 6f       /* docids o
3544b 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49  nly */.  DL_POSI
3544c 54 49 4f 4e 53 2c 20 20 20 20 20 20 20 20 20 20  TIONS,          
3544d 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f 73   /* docids + pos
3544e 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f 50  itions */.  DL_P
3544f 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
35450 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
35451 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f 66 66 73  positions + offs
35452 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73 74  ets */.} DocList
35453 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79 20  Type;../*.** By 
35454 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 6f  default, only po
35455 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e 6f 74 20  sitions and not 
35456 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f 72  offsets are stor
35457 65 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73  ed in the doclis
35458 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61 6e 67 65  ts..** To change
35459 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 6f 66   this so that of
3545a 66 73 65 74 73 20 61 72 65 20 73 74 6f 72 65 64  fsets are stored
3545b 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65 20 77 69   too, compile wi
3545c 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  th.**.**        
3545d 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c 54 3d 44    -DDL_DEFAULT=D
3545e 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
3545f 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c 5f  ETS.**.** If DL_
35460 44 45 46 41 55 4c 54 20 69 73 20 73 65 74 20 74  DEFAULT is set t
35461 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20 79 6f 75  o DL_DOCIDS, you
35462 72 20 74 61 62 6c 65 20 63 61 6e 20 6f 6e 6c 79  r table can only
35463 20 62 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20   be inserted.** 
35464 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65 74 65 73  into (no deletes
35465 20 6f 72 20 75 70 64 61 74 65 73 29 2e 0a 2a 2f   or updates)..*/
35466 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44 45 46 41  .#ifndef DL_DEFA
35467 55 4c 54 0a 23 20 64 65 66 69 6e 65 20 44 4c 5f  ULT.# define DL_
35468 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f 53 49 54  DEFAULT DL_POSIT
35469 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a 65 6e 75  IONS.#endif..enu
3546a 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44 20 3d 20  m {.  POS_END = 
3546b 30 2c 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 64  0,        /* end
3546c 20 6f 66 20 74 68 69 73 20 70 6f 73 69 74 69 6f   of this positio
3546d 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50 4f 53 5f  n list */.  POS_
3546e 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20 20 20 20  COLUMN,         
3546f 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e  /* followed by n
35470 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ew column number
35471 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53 45 0a 7d   */.  POS_BASE.}
35472 3b 0a 0a 2f 2a 20 75 74 69 6c 69 74 79 20 66 75  ;../* utility fu
35473 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 43  nctions */../* C
35474 4c 45 41 52 28 29 20 61 6e 64 20 53 43 52 41 4d  LEAR() and SCRAM
35475 42 4c 45 28 29 20 61 62 73 74 72 61 63 74 20 6d  BLE() abstract m
35476 65 6d 73 65 74 28 29 20 6f 6e 20 61 20 70 6f 69  emset() on a poi
35477 6e 74 65 72 20 74 6f 20 61 20 73 69 6e 67 6c 65  nter to a single
35478 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 70 72  .** record to pr
35479 65 76 65 6e 74 20 65 72 72 6f 72 73 20 6f 66 20  event errors of 
3547a 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
3547b 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53 6f 6d 65  my_function(Some
3547c 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20 20 20 6d  Type *b){.**   m
3547d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20 73  emset(b, '\0', s
3547e 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f 2f 20 73  izeof(b));  // s
3547f 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a 65 6f 66  izeof(b)!=sizeof
35480 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a 20  (*b).** }.*/./* 
35481 54 4f 44 4f 28 73 68 65 73 73 29 20 4f 62 76 69  TODO(shess) Obvi
35482 6f 75 73 20 63 61 6e 64 69 64 61 74 65 73 20 66  ous candidates f
35483 6f 72 20 61 20 68 65 61 64 65 72 20 66 69 6c 65  or a header file
35484 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4c 45  . */.#define CLE
35485 41 52 28 62 29 20 6d 65 6d 73 65 74 28 62 2c 20  AR(b) memset(b, 
35486 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 2a 28 62  '\0', sizeof(*(b
35487 29 29 29 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  )))..#ifndef NDE
35488 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 53 43  BUG.#  define SC
35489 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d 73 65 74  RAMBLE(b) memset
3548a 28 62 2c 20 30 78 35 35 2c 20 73 69 7a 65 6f 66  (b, 0x55, sizeof
3548b 28 2a 28 62 29 29 29 0a 23 65 6c 73 65 0a 23 20  (*(b))).#else.# 
3548c 20 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45   define SCRAMBLE
3548d 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a  (b).#endif../* .
3548e 2a 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69  ** Write a 64-bi
3548f 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
35490 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d  h integer to mem
35491 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
35492 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e  p[0]..** The len
35493 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74  gth of data writ
35494 74 65 6e 20 77 69 6c 6c 20 62 65 20 62 65 74 77  ten will be betw
35495 65 65 6e 20 31 20 61 6e 64 20 46 54 53 33 5f 56  een 1 and FTS3_V
35496 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e  ARINT_MAX bytes.
35497 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
35498 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
35499 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
3549a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3549b 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  nt sqlite3Fts3Pu
3549c 74 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70 2c  tVarint(char *p,
3549d 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 29   sqlite_int64 v)
3549e 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
3549f 72 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65 64  r *q = (unsigned
354a0 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71   char *) p;.  sq
354a1 6c 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20 3d  lite_uint64 vu =
354a2 20 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71   v;.  do{.    *q
354a3 2b 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ++ = (unsigned c
354a4 68 61 72 29 20 28 28 76 75 20 26 20 30 78 37 66  har) ((vu & 0x7f
354a5 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76  ) | 0x80);.    v
354a6 75 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c  u >>= 7;.  }whil
354a7 65 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b  e( vu!=0 );.  q[
354a8 2d 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a  -1] &= 0x7f;  /*
354a9 20 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20 62   turn off high b
354aa 69 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74 65  it in final byte
354ab 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71 20   */.  assert( q 
354ac 2d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  - (unsigned char
354ad 20 2a 29 70 20 3c 3d 20 46 54 53 33 5f 56 41 52   *)p <= FTS3_VAR
354ae 49 4e 54 5f 4d 41 58 20 29 3b 0a 20 20 72 65 74  INT_MAX );.  ret
354af 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28  urn (int) (q - (
354b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
354b1 70 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  p);.}../* .** Re
354b2 61 64 20 61 20 36 34 2d 62 69 74 20 76 61 72 69  ad a 64-bit vari
354b3 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
354b4 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20  ger from memory 
354b5 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d  starting at p[0]
354b6 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
354b7 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
354b8 72 65 61 64 2c 20 6f 72 20 30 20 6f 6e 20 65 72  read, or 0 on er
354b9 72 6f 72 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ror..** The valu
354ba 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a  e is stored in *
354bb 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  v..*/.SQLITE_PRI
354bc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
354bd 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 63 6f  Fts3GetVarint(co
354be 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 73 71 6c  nst char *p, sql
354bf 69 74 65 5f 69 6e 74 36 34 20 2a 76 29 7b 0a 20  ite_int64 *v){. 
354c0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
354c1 63 68 61 72 20 2a 71 20 3d 20 28 63 6f 6e 73 74  char *q = (const
354c2 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
354c3 29 20 70 3b 0a 20 20 73 71 6c 69 74 65 5f 75 69  ) p;.  sqlite_ui
354c4 6e 74 36 34 20 78 20 3d 20 30 2c 20 79 20 3d 20  nt64 x = 0, y = 
354c5 31 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 71 20  1;.  while( (*q 
354c6 26 20 30 78 38 30 29 20 3d 3d 20 30 78 38 30 20  & 0x80) == 0x80 
354c7 29 7b 0a 20 20 20 20 78 20 2b 3d 20 79 20 2a 20  ){.    x += y * 
354c8 28 2a 71 2b 2b 20 26 20 30 78 37 66 29 3b 0a 20  (*q++ & 0x7f);. 
354c9 20 20 20 79 20 3c 3c 3d 20 37 3b 0a 20 20 20 20     y <<= 7;.    
354ca 69 66 28 20 71 20 2d 20 28 75 6e 73 69 67 6e 65  if( q - (unsigne
354cb 64 20 63 68 61 72 20 2a 29 70 20 3e 3d 20 46 54  d char *)p >= FT
354cc 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b  S3_VARINT_MAX ){
354cd 20 20 2f 2a 20 62 61 64 20 64 61 74 61 20 2a 2f    /* bad data */
354ce 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
354cf 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
354d0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
354d1 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b  x += y * (*q++);
354d2 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f  .  *v = (sqlite_
354d3 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65 74 75  int64) x;.  retu
354d4 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75  rn (int) (q - (u
354d5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
354d6 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 69  );.}../*.** Simi
354d7 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 46 74  lar to sqlite3Ft
354d8 73 33 47 65 74 56 61 72 69 6e 74 28 29 2c 20 65  s3GetVarint(), e
354d9 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6f  xcept that the o
354da 75 74 70 75 74 20 69 73 20 74 72 75 6e 63 61 74  utput is truncat
354db 65 64 20 74 6f 20 61 0a 2a 2a 20 33 32 2d 62 69  ed to a.** 32-bi
354dc 74 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65  t integer before
354dd 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
354de 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
354df 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
354e0 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f  s3GetVarint32(co
354e1 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69 6e 74  nst char *p, int
354e2 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74 65 5f 69   *pi){. sqlite_i
354e3 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20 72 65 74  nt64 i;. int ret
354e4 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65   = sqlite3Fts3Ge
354e5 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a  tVarint(p, &i);.
354e6 20 2a 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a   *pi = (int) i;.
354e7 20 61 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20   assert( *pi==i 
354e8 29 3b 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a  );. return ret;.
354e9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
354ea 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
354eb 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
354ec 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20  store the value 
354ed 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a  passed as the.**
354ee 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
354ef 69 6e 20 76 61 72 69 6e 74 20 66 6f 72 6d 2e 0a  in varint form..
354f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
354f1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
354f2 33 56 61 72 69 6e 74 4c 65 6e 28 73 71 6c 69 74  3VarintLen(sqlit
354f3 65 33 5f 75 69 6e 74 36 34 20 76 29 7b 0a 20 20  e3_uint64 v){.  
354f4 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b  int i = 0;.  do{
354f5 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20  .    i++;.    v 
354f6 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28  >>= 7;.  }while(
354f7 20 76 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72   v!=0 );.  retur
354f8 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
354f9 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79  nvert an SQL-sty
354fa 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le quoted string
354fb 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73   into a normal s
354fc 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e  tring by removin
354fd 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63  g.** the quote c
354fe 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 20  haracters.  The 
354ff 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f  conversion is do
35500 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66  ne in-place.  If
35501 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f   the.** input do
35502 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
35503 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  h a quote charac
35504 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ter, then this r
35505 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
35506 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  o-op..**.** Exam
35507 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ples:.**.**     
35508 22 61 62 63 22 20 20 20 62 65 63 6f 6d 65 73 20  "abc"   becomes 
35509 20 20 61 62 63 0a 2a 2a 20 20 20 20 20 27 78 79    abc.**     'xy
3550a 7a 27 20 20 20 62 65 63 6f 6d 65 73 20 20 20 78  z'   becomes   x
3550b 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20  yz.**     [pqr] 
3550c 20 20 62 65 63 6f 6d 65 73 20 20 20 70 71 72 0a    becomes   pqr.
3550d 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20 20 20 62  **     `mno`   b
3550e 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a  ecomes   mno.*/.
3550f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
35510 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 44  oid sqlite3Fts3D
35511 65 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b  equote(char *z){
35512 0a 20 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20  .  int quote;.  
35513 69 6e 74 20 69 2c 20 6a 3b 0a 0a 20 20 71 75 6f  int i, j;..  quo
35514 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69  te = z[0];.  swi
35515 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20  tch( quote ){.  
35516 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72    case '\'':  br
35517 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22  eak;.    case '"
35518 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ':   break;.    
35519 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61  case '`':   brea
3551a 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
3551b 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63    /* For MySQL c
3551c 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a  ompatibility */.
3551d 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20      case '[':   
3551e 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72  quote = ']';  br
3551f 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20  eak;  /* For MS 
35520 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74  SqlServer compat
35521 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64  ibility */.    d
35522 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72  efault:    retur
35523 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31  n;.  }.  for(i=1
35524 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b  , j=0; z[i]; i++
35525 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  ){.    if( z[i]=
35526 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20  =quote ){.      
35527 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74  if( z[i+1]==quot
35528 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a  e ){.        z[j
35529 2b 2b 5d 20 3d 20 28 63 68 61 72 29 71 75 6f 74  ++] = (char)quot
3552a 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  e;.        i++;.
3552b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3552c 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b       z[j++] = 0;
3552d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3552e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
3552f 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  e{.      z[j++] 
35530 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  = z[i];.    }.  
35531 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  }.}..static void
35532 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35533 69 6e 74 28 63 68 61 72 20 2a 2a 70 70 2c 20 73  int(char **pp, s
35534 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 56  qlite3_int64 *pV
35535 61 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  al){.  sqlite3_i
35536 6e 74 36 34 20 69 56 61 6c 3b 0a 20 20 2a 70 70  nt64 iVal;.  *pp
35537 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
35538 65 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 26 69  etVarint(*pp, &i
35539 56 61 6c 29 3b 0a 20 20 2a 70 56 61 6c 20 2b 3d  Val);.  *pVal +=
3553a 20 69 56 61 6c 3b 0a 7d 0a 0a 73 74 61 74 69 63   iVal;.}..static
3553b 20 76 6f 69 64 20 66 74 73 33 47 65 74 44 65 6c   void fts3GetDel
3553c 74 61 56 61 72 69 6e 74 32 28 63 68 61 72 20 2a  taVarint2(char *
3553d 2a 70 70 2c 20 63 68 61 72 20 2a 70 45 6e 64 2c  *pp, char *pEnd,
3553e 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
3553f 70 56 61 6c 29 7b 0a 20 20 69 66 28 20 2a 70 70  pVal){.  if( *pp
35540 3e 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 2a 70  >=pEnd ){.    *p
35541 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p = 0;.  }else{.
35542 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61      fts3GetDelta
35543 56 61 72 69 6e 74 28 70 70 2c 20 70 56 61 6c 29  Varint(pp, pVal)
35544 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
35545 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29  he xDisconnect()
35546 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
35547 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
35548 20 69 6e 74 20 66 74 73 33 44 69 73 63 6f 6e 6e   int fts3Disconn
35549 65 63 74 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  ectMethod(sqlite
3554a 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
3554b 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d    Fts3Table *p =
3554c 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56   (Fts3Table *)pV
3554d 74 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20  tab;.  int i;.. 
3554e 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 50 65 6e   assert( p->nPen
3554f 64 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0a 0a  dingData==0 );..
35550 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 70 72    /* Free any pr
35551 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
35552 73 20 68 65 6c 64 20 2a 2f 0a 20 20 66 6f 72 28  s held */.  for(
35553 69 3d 30 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72  i=0; i<SizeofArr
35554 61 79 28 70 2d 3e 61 53 74 6d 74 29 3b 20 69 2b  ay(p->aStmt); i+
35555 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  +){.    sqlite3_
35556 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 61 53 74 6d  finalize(p->aStm
35557 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  t[i]);.  }.  for
35558 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4c 65 61 76  (i=0; i<p->nLeav
35559 65 73 53 74 6d 74 3b 20 69 2b 2b 29 7b 0a 20 20  esStmt; i++){.  
3555a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
3555b 7a 65 28 70 2d 3e 61 4c 65 61 76 65 73 53 74 6d  ze(p->aLeavesStm
3555c 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  t[i]);.  }.  sql
3555d 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 53 65  ite3_free(p->zSe
3555e 6c 65 63 74 4c 65 61 76 65 73 29 3b 0a 20 20 73  lectLeaves);.  s
3555f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
35560 4c 65 61 76 65 73 53 74 6d 74 29 3b 0a 0a 20 20  LeavesStmt);..  
35561 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 74 6f  /* Invoke the to
35562 6b 65 6e 69 7a 65 72 20 64 65 73 74 72 75 63 74  kenizer destruct
35563 6f 72 20 74 6f 20 66 72 65 65 20 74 68 65 20 74  or to free the t
35564 6f 6b 65 6e 69 7a 65 72 2e 20 2a 2f 0a 20 20 70  okenizer. */.  p
35565 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d  ->pTokenizer->pM
35566 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28  odule->xDestroy(
35567 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  p->pTokenizer);.
35568 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
35569 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
3556a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3556b 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 20   The xDestroy() 
3556c 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65  virtual table me
3556d 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
3556e 69 6e 74 20 66 74 73 33 44 65 73 74 72 6f 79 4d  int fts3DestroyM
3556f 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74  ethod(sqlite3_vt
35570 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 69 6e  ab *pVtab){.  in
35571 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
35572 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35573 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
35574 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d    Fts3Table *p =
35575 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56   (Fts3Table *)pV
35576 74 61 62 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74  tab;..  /* Creat
35577 65 20 61 20 73 63 72 69 70 74 20 74 6f 20 64 72  e a script to dr
35578 6f 70 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  op the underlyin
35579 67 20 74 68 72 65 65 20 73 74 6f 72 61 67 65 20  g three storage 
3557a 74 61 62 6c 65 73 2e 20 2a 2f 0a 20 20 63 68 61  tables. */.  cha
3557b 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  r *zSql = sqlite
3557c 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20  3_mprintf(.     
3557d 20 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20   "DROP TABLE IF 
3557e 45 58 49 53 54 53 20 25 51 2e 27 25 71 5f 63 6f  EXISTS %Q.'%q_co
3557f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20 20 20 22  ntent';".      "
35580 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58  DROP TABLE IF EX
35581 49 53 54 53 20 25 51 2e 27 25 71 5f 73 65 67 6d  ISTS %Q.'%q_segm
35582 65 6e 74 73 27 3b 22 0a 20 20 20 20 20 20 22 44  ents';".      "D
35583 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49  ROP TABLE IF EXI
35584 53 54 53 20 25 51 2e 27 25 71 5f 73 65 67 64 69  STS %Q.'%q_segdi
35585 72 27 3b 22 2c 20 0a 20 20 20 20 20 20 70 2d 3e  r';", .      p->
35586 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70  zDb, p->zName, p
35587 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
35588 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
35589 65 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  e.  );..  /* If 
3558a 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65  malloc has faile
3558b 64 2c 20 73 65 74 20 72 63 20 74 6f 20 53 51 4c  d, set rc to SQL
3558c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 4f 74 68 65 72  ITE_NOMEM. Other
3558d 77 69 73 65 2c 20 74 72 79 20 74 6f 0a 20 20 2a  wise, try to.  *
3558e 2a 20 65 78 65 63 75 74 65 20 74 68 65 20 53 51  * execute the SQ
3558f 4c 20 73 63 72 69 70 74 20 63 72 65 61 74 65 64  L script created
35590 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69   above..  */.  i
35591 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72  f( zSql ){.    r
35592 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
35593 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c  (p->db, zSql, 0,
35594 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
35595 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
35596 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
35597 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
35598 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65    }..  /* If eve
35599 72 79 74 68 69 6e 67 20 68 61 73 20 77 6f 72 6b  rything has work
3559a 65 64 2c 20 69 6e 76 6f 6b 65 20 66 74 73 33 44  ed, invoke fts3D
3559b 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28  isconnectMethod(
3559c 29 20 74 6f 20 66 72 65 65 20 74 68 65 0a 20 20  ) to free the.  
3559d 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  ** memory associ
3559e 61 74 65 64 20 77 69 74 68 20 74 68 65 20 46 74  ated with the Ft
3559f 73 33 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  s3Table structur
355a0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  e and return SQL
355a1 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20 4f 74 68  ITE_OK..  ** Oth
355a2 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61  erwise, return a
355a3 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
355a4 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75  ode..  */.  retu
355a5 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rn (rc==SQLITE_O
355a6 4b 20 3f 20 66 74 73 33 44 69 73 63 6f 6e 6e 65  K ? fts3Disconne
355a7 63 74 4d 65 74 68 6f 64 28 70 56 74 61 62 29 20  ctMethod(pVtab) 
355a8 3a 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  : rc);.}.../*.**
355a9 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   Invoke sqlite3_
355aa 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74  declare_vtab() t
355ab 6f 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 63  o declare the sc
355ac 68 65 6d 61 20 66 6f 72 20 74 68 65 20 46 54 53  hema for the FTS
355ad 33 20 74 61 62 6c 65 0a 2a 2a 20 70 61 73 73 65  3 table.** passe
355ae 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
355af 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  rgument. This is
355b0 20 64 6f 6e 65 20 61 73 20 70 61 72 74 20 6f 66   done as part of
355b1 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 0a   the xConnect().
355b2 2a 2a 20 61 6e 64 20 78 43 72 65 61 74 65 28 29  ** and xCreate()
355b3 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61   methods..*/.sta
355b4 74 69 63 20 69 6e 74 20 66 74 73 33 44 65 63 6c  tic int fts3Decl
355b5 61 72 65 56 74 61 62 28 46 74 73 33 54 61 62 6c  areVtab(Fts3Tabl
355b6 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 20  e *p){.  int i; 
355b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355b8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
355b9 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
355ba 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
355bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355bc 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
355bd 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71  e */.  char *zSq
355be 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
355bf 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
355c0 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 74  atement passed t
355c1 6f 20 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  o declare_vtab()
355c2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
355c3 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
355c4 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
355c5 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 63 6f   user defined co
355c6 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43  lumns */..  /* C
355c7 72 65 61 74 65 20 61 20 6c 69 73 74 20 6f 66 20  reate a list of 
355c8 75 73 65 72 20 63 6f 6c 75 6d 6e 73 20 66 6f 72  user columns for
355c9 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
355ca 6c 65 20 2a 2f 0a 20 20 7a 43 6f 6c 73 20 3d 20  le */.  zCols = 
355cb 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
355cc 22 25 51 2c 20 22 2c 20 70 2d 3e 61 7a 43 6f 6c  "%Q, ", p->azCol
355cd 75 6d 6e 5b 30 5d 29 3b 0a 20 20 66 6f 72 28 69  umn[0]);.  for(i
355ce 3d 31 3b 20 7a 43 6f 6c 73 20 26 26 20 69 3c 70  =1; zCols && i<p
355cf 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
355d0 0a 20 20 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c  .    zCols = sql
355d1 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a  ite3_mprintf("%z
355d2 25 51 2c 20 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d  %Q, ", zCols, p-
355d3 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20  >azColumn[i]);. 
355d4 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20   }..  /* Create 
355d5 74 68 65 20 77 68 6f 6c 65 20 22 43 52 45 41 54  the whole "CREAT
355d6 45 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65  E TABLE" stateme
355d7 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 53 51  nt to pass to SQ
355d8 4c 69 74 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d  Lite */.  zSql =
355d9 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
355da 28 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20  (.      "CREATE 
355db 54 41 42 4c 45 20 78 28 25 73 20 25 51 20 48 49  TABLE x(%s %Q HI
355dc 44 44 45 4e 2c 20 64 6f 63 69 64 20 48 49 44 44  DDEN, docid HIDD
355dd 45 4e 29 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d 3e  EN)", zCols, p->
355de 7a 4e 61 6d 65 0a 20 20 29 3b 0a 0a 20 20 69 66  zName.  );..  if
355df 28 20 21 7a 43 6f 6c 73 20 7c 7c 20 21 7a 53 71  ( !zCols || !zSq
355e0 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  l ){.    rc = SQ
355e1 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
355e2 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
355e3 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
355e4 61 62 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 29 3b  ab(p->db, zSql);
355e5 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
355e6 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 73 71  free(zSql);.  sq
355e7 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6c 73  lite3_free(zCols
355e8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
355e9 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
355ea 74 68 65 20 62 61 63 6b 69 6e 67 20 73 74 6f 72  the backing stor
355eb 65 20 74 61 62 6c 65 73 20 28 25 5f 63 6f 6e 74  e tables (%_cont
355ec 65 6e 74 2c 20 25 5f 73 65 67 6d 65 6e 74 73 20  ent, %_segments 
355ed 61 6e 64 20 25 5f 73 65 67 64 69 72 29 0a 2a 2a  and %_segdir).**
355ee 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
355ef 20 46 54 53 33 20 74 61 62 6c 65 20 70 61 73 73   FTS3 table pass
355f0 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 61  ed as the only a
355f1 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  rgument. This is
355f2 20 64 6f 6e 65 0a 2a 2a 20 61 73 20 70 61 72 74   done.** as part
355f3 20 6f 66 20 74 68 65 20 76 74 61 62 20 78 43 72   of the vtab xCr
355f4 65 61 74 65 28 29 20 6d 65 74 68 6f 64 2e 0a 2a  eate() method..*
355f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
355f6 33 43 72 65 61 74 65 54 61 62 6c 65 73 28 46 74  3CreateTables(Ft
355f7 73 33 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69  s3Table *p){.  i
355f8 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
355f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
355fa 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
355fb 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
355fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355fd 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
355fe 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  ariable */.  cha
355ff 72 20 2a 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 3b  r *zContentCols;
35600 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35601 43 6f 6c 75 6d 6e 73 20 6f 66 20 25 5f 63 6f 6e  Columns of %_con
35602 74 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tent table */.  
35603 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20  char *zSql;     
35604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35605 2f 2a 20 53 51 4c 20 73 63 72 69 70 74 20 74 6f  /* SQL script to
35606 20 63 72 65 61 74 65 20 72 65 71 75 69 72 65 64   create required
35607 20 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a   tables */..  /*
35608 20 43 72 65 61 74 65 20 61 20 6c 69 73 74 20 6f   Create a list o
35609 66 20 75 73 65 72 20 63 6f 6c 75 6d 6e 73 20 66  f user columns f
3560a 6f 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 74  or the content t
3560b 61 62 6c 65 20 2a 2f 0a 20 20 7a 43 6f 6e 74 65  able */.  zConte
3560c 6e 74 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33  ntCols = sqlite3
3560d 5f 6d 70 72 69 6e 74 66 28 22 64 6f 63 69 64 20  _mprintf("docid 
3560e 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
3560f 4b 45 59 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30  KEY");.  for(i=0
35610 3b 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 20 26  ; zContentCols &
35611 26 20 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  & i<p->nColumn; 
35612 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  i++){.    char *
35613 7a 20 3d 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b  z = p->azColumn[
35614 69 5d 3b 0a 20 20 20 20 7a 43 6f 6e 74 65 6e 74  i];.    zContent
35615 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Cols = sqlite3_m
35616 70 72 69 6e 74 66 28 22 25 7a 2c 20 27 63 25 64  printf("%z, 'c%d
35617 25 71 27 22 2c 20 7a 43 6f 6e 74 65 6e 74 43 6f  %q'", zContentCo
35618 6c 73 2c 20 69 2c 20 7a 29 3b 0a 20 20 7d 0a 0a  ls, i, z);.  }..
35619 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
3561a 77 68 6f 6c 65 20 53 51 4c 20 73 63 72 69 70 74  whole SQL script
3561b 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   */.  zSql = sql
3561c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
3561d 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c      "CREATE TABL
3561e 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74  E %Q.'%q_content
3561f 27 28 25 73 29 3b 22 0a 20 20 20 20 20 20 22 43  '(%s);".      "C
35620 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 27  REATE TABLE %Q.'
35621 25 71 5f 73 65 67 6d 65 6e 74 73 27 28 62 6c 6f  %q_segments'(blo
35622 63 6b 69 64 20 49 4e 54 45 47 45 52 20 50 52 49  ckid INTEGER PRI
35623 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20  MARY KEY, block 
35624 42 4c 4f 42 29 3b 22 0a 20 20 20 20 20 20 22 43  BLOB);".      "C
35625 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 27  REATE TABLE %Q.'
35626 25 71 5f 73 65 67 64 69 72 27 28 22 0a 20 20 20  %q_segdir'(".   
35627 20 20 20 20 20 22 6c 65 76 65 6c 20 49 4e 54 45       "level INTE
35628 47 45 52 2c 22 0a 20 20 20 20 20 20 20 20 22 69  GER,".        "i
35629 64 78 20 49 4e 54 45 47 45 52 2c 22 0a 20 20 20  dx INTEGER,".   
3562a 20 20 20 20 20 22 73 74 61 72 74 5f 62 6c 6f 63       "start_bloc
3562b 6b 20 49 4e 54 45 47 45 52 2c 22 0a 20 20 20 20  k INTEGER,".    
3562c 20 20 20 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f      "leaves_end_
3562d 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0a  block INTEGER,".
3562e 20 20 20 20 20 20 20 20 22 65 6e 64 5f 62 6c 6f          "end_blo
3562f 63 6b 20 49 4e 54 45 47 45 52 2c 22 0a 20 20 20  ck INTEGER,".   
35630 20 20 20 20 20 22 72 6f 6f 74 20 42 4c 4f 42 2c       "root BLOB,
35631 22 0a 20 20 20 20 20 20 20 20 22 50 52 49 4d 41  ".        "PRIMA
35632 52 59 20 4b 45 59 28 6c 65 76 65 6c 2c 20 69 64  RY KEY(level, id
35633 78 29 22 0a 20 20 20 20 20 20 22 29 3b 22 2c 0a  x)".      ");",.
35634 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70 2d        p->zDb, p-
35635 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6e 74 65 6e 74  >zName, zContent
35636 43 6f 6c 73 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d  Cols, p->zDb, p-
35637 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 7a 44 62 2c 20  >zName, p->zDb, 
35638 70 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a 0a 20  p->zName.  );.. 
35639 20 2f 2a 20 55 6e 6c 65 73 73 20 61 20 6d 61 6c   /* Unless a mal
3563a 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 68 61  loc() failure ha
3563b 73 20 6f 63 63 75 72 72 65 64 2c 20 65 78 65 63  s occurred, exec
3563c 75 74 65 20 74 68 65 20 53 51 4c 20 73 63 72 69  ute the SQL scri
3563d 70 74 20 74 6f 20 0a 20 20 2a 2a 20 63 72 65 61  pt to .  ** crea
3563e 74 65 20 74 68 65 20 74 61 62 6c 65 73 20 75 73  te the tables us
3563f 65 64 20 74 6f 20 73 74 6f 72 65 20 64 61 74 61  ed to store data
35640 20 66 6f 72 20 74 68 69 73 20 46 54 53 33 20 76   for this FTS3 v
35641 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 20 20  irtual table..  
35642 2a 2f 0a 20 20 69 66 28 20 7a 43 6f 6e 74 65 6e  */.  if( zConten
35643 74 43 6f 6c 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c  tCols==0 || zSql
35644 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
35645 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35646 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
35647 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 2d 3e  sqlite3_exec(p->
35648 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20  db, zSql, 0, 0, 
35649 30 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  0);.  }..  sqlit
3564a 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
3564b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
3564c 6f 6e 74 65 6e 74 43 6f 6c 73 29 3b 0a 20 20 72  ontentCols);.  r
3564d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
3564e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
3564f 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
35650 74 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74  tation of both t
35651 68 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20  he xConnect and 
35652 78 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f  xCreate.** metho
35653 64 73 20 6f 66 20 74 68 65 20 46 54 53 33 20 76  ds of the FTS3 v
35654 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
35655 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 5d 20 61  .** The argv[] a
35656 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 74 68  rray contains th
35657 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
35658 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20 20 2d  **   argv[0]   -
35659 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a  > module name.**
3565a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20     argv[1]   -> 
3565b 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
3565c 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20     argv[2]   -> 
3565d 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  table name.**   
3565e 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 22 63 6f  argv[...] -> "co
3565f 6c 75 6d 6e 20 6e 61 6d 65 22 20 61 6e 64 20 6f  lumn name" and o
35660 74 68 65 72 20 6d 6f 64 75 6c 65 20 61 72 67 75  ther module argu
35661 6d 65 6e 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a  ment fields..*/.
35662 69 6e 74 20 66 74 73 33 49 6e 69 74 56 74 61 62  int fts3InitVtab
35663 28 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  (.  int isCreate
35664 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35665 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
35666 78 43 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66  xCreate, false f
35667 6f 72 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20  or xConnect */. 
35668 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
35669 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3566a 20 2f 2a 20 54 68 65 20 53 51 4c 69 74 65 20 64   /* The SQLite d
3566b 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3566c 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  on */.  void *pA
3566d 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ux,             
3566e 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
3566f 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
35670 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20   tokenizers */. 
35671 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20   int argc,      
35672 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35673 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
35674 65 6d 65 6e 74 73 20 69 6e 20 61 72 67 76 20 61  ements in argv a
35675 72 72 61 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rray */.  const 
35676 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72  char * const *ar
35677 67 76 2c 20 20 20 20 20 20 20 2f 2a 20 78 43 72  gv,       /* xCr
35678 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72  eate/xConnect ar
35679 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a  gument array */.
3567a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
3567b 2a 70 70 56 54 61 62 2c 20 20 20 20 20 20 20 20  *ppVTab,        
3567c 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72    /* Write the r
3567d 65 73 75 6c 74 69 6e 67 20 76 74 61 62 20 73 74  esulting vtab st
3567e 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
3567f 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20    char **pzErr  
35680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35681 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 79 20 65    /* Write any e
35682 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72  rror message her
35683 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61  e */.){.  Fts3Ha
35684 73 68 20 2a 70 48 61 73 68 20 3d 20 28 46 74 73  sh *pHash = (Fts
35685 33 48 61 73 68 20 2a 29 70 41 75 78 3b 0a 20 20  3Hash *)pAux;.  
35686 46 74 73 33 54 61 62 6c 65 20 2a 70 3b 20 20 20  Fts3Table *p;   
35687 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35688 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 6c  /* Pointer to al
35689 6c 6f 63 61 74 65 64 20 76 74 61 62 20 2a 2f 0a  located vtab */.
3568a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
3568b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3568c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
3568d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
3568e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3568f 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
35690 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
35691 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
35692 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35693 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63  /* Size of alloc
35694 61 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 2a  ation used for *
35695 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  p */.  int iCol;
35696 0a 20 20 69 6e 74 20 6e 53 74 72 69 6e 67 20 3d  .  int nString =
35697 20 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d   0;.  int nCol =
35698 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72   0;.  char *zCsr
35699 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 0a 20 20 69  ;.  int nDb;.  i
3569a 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 23 69 66 64 65  nt nName;..#ifde
3569b 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
3569c 63 68 61 72 20 2a 7a 54 65 73 74 50 61 72 61 6d  char *zTestParam
3569d 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 74 72 6e   = 0;.  if( strn
3569e 63 6d 70 28 61 72 67 76 5b 61 72 67 63 2d 31 5d  cmp(argv[argc-1]
3569f 2c 20 22 74 65 73 74 3a 22 2c 20 35 29 3d 3d 30  , "test:", 5)==0
356a0 20 29 7b 0a 20 20 20 20 7a 54 65 73 74 50 61 72   ){.    zTestPar
356a1 61 6d 20 3d 20 61 72 67 76 5b 61 72 67 63 2d 31  am = argv[argc-1
356a2 5d 3b 0a 20 20 20 20 61 72 67 63 2d 2d 3b 0a 20  ];.    argc--;. 
356a3 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 63 6f 6e   }.#endif..  con
356a4 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 69  st char *zTokeni
356a5 7a 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  zer = 0;        
356a6 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
356a7 66 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75  f tokenizer to u
356a8 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  se */.  sqlite3_
356a9 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
356aa 6e 69 7a 65 72 20 3d 20 30 3b 20 20 20 20 20 20  nizer = 0;      
356ab 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 66    /* Tokenizer f
356ac 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  or this table */
356ad 0a 0a 20 20 6e 44 62 20 3d 20 28 69 6e 74 29 73  ..  nDb = (int)s
356ae 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 20 2b  trlen(argv[1]) +
356af 20 31 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 28 69   1;.  nName = (i
356b0 6e 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 32  nt)strlen(argv[2
356b1 5d 29 20 2b 20 31 3b 0a 20 20 66 6f 72 28 69 3d  ]) + 1;.  for(i=
356b2 33 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  3; i<argc; i++){
356b3 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20  .    char const 
356b4 2a 7a 20 3d 20 61 72 67 76 5b 69 5d 3b 0a 20 20  *z = argv[i];.  
356b5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
356b6 73 33 49 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28  s3InitTokenizer(
356b7 70 48 61 73 68 2c 20 7a 2c 20 26 70 54 6f 6b 65  pHash, z, &pToke
356b8 6e 69 7a 65 72 2c 20 26 7a 54 6f 6b 65 6e 69 7a  nizer, &zTokeniz
356b9 65 72 2c 20 70 7a 45 72 72 29 3b 0a 20 20 20 20  er, pzErr);.    
356ba 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
356bb 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
356bc 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
356bd 69 66 28 20 7a 21 3d 7a 54 6f 6b 65 6e 69 7a 65  if( z!=zTokenize
356be 72 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 72 69  r ){.      nStri
356bf 6e 67 20 2b 3d 20 28 69 6e 74 29 28 73 74 72 6c  ng += (int)(strl
356c0 65 6e 28 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20  en(z) + 1);.    
356c1 7d 0a 20 20 7d 0a 20 20 6e 43 6f 6c 20 3d 20 61  }.  }.  nCol = a
356c2 72 67 63 20 2d 20 33 20 2d 20 28 7a 54 6f 6b 65  rgc - 3 - (zToke
356c3 6e 69 7a 65 72 21 3d 30 29 3b 0a 20 20 69 66 28  nizer!=0);.  if(
356c4 20 7a 54 6f 6b 65 6e 69 7a 65 72 3d 3d 30 20 29   zTokenizer==0 )
356c5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
356c6 65 33 46 74 73 33 49 6e 69 74 54 6f 6b 65 6e 69  e3Fts3InitTokeni
356c7 7a 65 72 28 70 48 61 73 68 2c 20 30 2c 20 26 70  zer(pHash, 0, &p
356c8 54 6f 6b 65 6e 69 7a 65 72 2c 20 30 2c 20 70 7a  Tokenizer, 0, pz
356c9 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Err);.    if( rc
356ca 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
356cb 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
356cc 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
356cd 28 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 3b 0a  ( pTokenizer );.
356ce 20 20 7d 0a 0a 20 20 69 66 28 20 6e 43 6f 6c 3d    }..  if( nCol=
356cf 3d 30 20 29 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d  =0 ){.    nCol =
356d0 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c   1;.  }..  /* Al
356d1 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  locate and popul
356d2 61 74 65 20 74 68 65 20 46 74 73 33 54 61 62 6c  ate the Fts3Tabl
356d3 65 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  e structure. */.
356d4 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
356d5 28 46 74 73 33 54 61 62 6c 65 29 20 2b 20 20 20  (Fts3Table) +   
356d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74             /* Ft
356d7 73 33 54 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20  s3Table */.     
356d8 20 20 20 20 20 6e 43 6f 6c 20 2a 20 73 69 7a 65       nCol * size
356d9 6f 66 28 63 68 61 72 20 2a 29 20 2b 20 20 20 20  of(char *) +    
356da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 7a 43            /* azC
356db 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20  olumn */.       
356dc 20 20 20 6e 4e 61 6d 65 20 2b 20 20 20 20 20 20     nName +      
356dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356de 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65          /* zName
356df 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e 44   */.          nD
356e0 62 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  b +             
356e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356e2 20 20 20 2f 2a 20 7a 44 62 20 2a 2f 0a 20 20 20     /* zDb */.   
356e3 20 20 20 20 20 20 20 6e 53 74 72 69 6e 67 3b 20         nString; 
356e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
356e6 70 61 63 65 20 66 6f 72 20 61 7a 43 6f 6c 75 6d  pace for azColum
356e7 6e 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 70  n strings */.  p
356e8 20 3d 20 28 46 74 73 33 54 61 62 6c 65 2a 29 73   = (Fts3Table*)s
356e9 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
356ea 79 74 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  yte);.  if( p==0
356eb 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
356ec 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
356ed 6f 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75  oto fts3_init_ou
356ee 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  t;.  }.  memset(
356ef 70 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 0a 20  p, 0, nByte);.. 
356f0 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70   p->db = db;.  p
356f1 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c  ->nColumn = nCol
356f2 3b 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44  ;.  p->nPendingD
356f3 61 74 61 20 3d 20 30 3b 0a 20 20 70 2d 3e 61 7a  ata = 0;.  p->az
356f4 43 6f 6c 75 6d 6e 20 3d 20 28 63 68 61 72 20 2a  Column = (char *
356f5 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e 70 54  *)&p[1];.  p->pT
356f6 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
356f7 6e 69 7a 65 72 3b 0a 20 20 70 2d 3e 6e 4e 6f 64  nizer;.  p->nNod
356f8 65 53 69 7a 65 20 3d 20 31 30 30 30 3b 0a 20 20  eSize = 1000;.  
356f9 7a 43 73 72 20 3d 20 28 63 68 61 72 20 2a 29 26  zCsr = (char *)&
356fa 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c  p->azColumn[nCol
356fb 5d 3b 0a 0a 20 20 66 74 73 33 48 61 73 68 49 6e  ];..  fts3HashIn
356fc 69 74 28 26 70 2d 3e 70 65 6e 64 69 6e 67 54 65  it(&p->pendingTe
356fd 72 6d 73 2c 20 46 54 53 33 5f 48 41 53 48 5f 53  rms, FTS3_HASH_S
356fe 54 52 49 4e 47 2c 20 31 29 3b 0a 0a 20 20 2f 2a  TRING, 1);..  /*
356ff 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 7a 4e 61   Fill in the zNa
35700 6d 65 20 61 6e 64 20 7a 44 62 20 66 69 65 6c 64  me and zDb field
35701 73 20 6f 66 20 74 68 65 20 76 74 61 62 20 73 74  s of the vtab st
35702 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 70 2d  ructure. */.  p-
35703 3e 7a 4e 61 6d 65 20 3d 20 7a 43 73 72 3b 0a 20  >zName = zCsr;. 
35704 20 6d 65 6d 63 70 79 28 7a 43 73 72 2c 20 61 72   memcpy(zCsr, ar
35705 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a 20  gv[2], nName);. 
35706 20 7a 43 73 72 20 2b 3d 20 6e 4e 61 6d 65 3b 0a   zCsr += nName;.
35707 20 20 70 2d 3e 7a 44 62 20 3d 20 7a 43 73 72 3b    p->zDb = zCsr;
35708 0a 20 20 6d 65 6d 63 70 79 28 7a 43 73 72 2c 20  .  memcpy(zCsr, 
35709 61 72 67 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20  argv[1], nDb);. 
3570a 20 7a 43 73 72 20 2b 3d 20 6e 44 62 3b 0a 0a 20   zCsr += nDb;.. 
3570b 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20   /* Fill in the 
3570c 61 7a 43 6f 6c 75 6d 6e 20 61 72 72 61 79 20 2a  azColumn array *
3570d 2f 0a 20 20 69 43 6f 6c 20 3d 20 30 3b 0a 20 20  /.  iCol = 0;.  
3570e 66 6f 72 28 69 3d 33 3b 20 69 3c 61 72 67 63 3b  for(i=3; i<argc;
3570f 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61   i++){.    if( a
35710 72 67 76 5b 69 5d 21 3d 7a 54 6f 6b 65 6e 69 7a  rgv[i]!=zTokeniz
35711 65 72 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  er ){.      char
35712 20 2a 7a 3b 20 0a 20 20 20 20 20 20 69 6e 74 20   *z; .      int 
35713 6e 3b 0a 20 20 20 20 20 20 7a 20 3d 20 28 63 68  n;.      z = (ch
35714 61 72 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33  ar *)sqlite3Fts3
35715 4e 65 78 74 54 6f 6b 65 6e 28 61 72 67 76 5b 69  NextToken(argv[i
35716 5d 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 6d 65  ], &n);.      me
35717 6d 63 70 79 28 7a 43 73 72 2c 20 7a 2c 20 6e 29  mcpy(zCsr, z, n)
35718 3b 0a 20 20 20 20 20 20 7a 43 73 72 5b 6e 5d 20  ;.      zCsr[n] 
35719 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71  = '\0';.      sq
3571a 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65  lite3Fts3Dequote
3571b 28 7a 43 73 72 29 3b 0a 20 20 20 20 20 20 70 2d  (zCsr);.      p-
3571c 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 43 6f 6c 2b 2b  >azColumn[iCol++
3571d 5d 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20  ] = zCsr;.      
3571e 7a 43 73 72 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20  zCsr += n+1;.   
3571f 20 20 20 61 73 73 65 72 74 28 20 7a 43 73 72 20     assert( zCsr 
35720 3c 3d 20 26 28 28 63 68 61 72 20 2a 29 70 29 5b  <= &((char *)p)[
35721 6e 42 79 74 65 5d 20 29 3b 0a 20 20 20 20 7d 0a  nByte] );.    }.
35722 20 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3d 3d    }.  if( iCol==
35723 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
35724 20 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 20 20 20 20   nCol==1 );.    
35725 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 30 5d 20 3d  p->azColumn[0] =
35726 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20 20 7d 0a   "content";.  }.
35727 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
35728 20 61 6e 20 78 43 72 65 61 74 65 20 63 61 6c 6c   an xCreate call
35729 2c 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64  , create the und
3572a 65 72 6c 79 69 6e 67 20 74 61 62 6c 65 73 20 69  erlying tables i
3572b 6e 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61  n the .  ** data
3572c 62 61 73 65 2e 20 54 4f 44 4f 3a 20 46 6f 72 20  base. TODO: For 
3572d 78 43 6f 6e 6e 65 63 74 28 29 2c 20 69 74 20 63  xConnect(), it c
3572e 6f 75 6c 64 20 76 65 72 69 66 79 20 74 68 61 74  ould verify that
3572f 20 73 61 69 64 20 74 61 62 6c 65 73 20 65 78 69   said tables exi
35730 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  st..  */.  if( i
35731 73 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72  sCreate ){.    r
35732 63 20 3d 20 66 74 73 33 43 72 65 61 74 65 54 61  c = fts3CreateTa
35733 62 6c 65 73 28 70 29 3b 0a 20 20 20 20 69 66 28  bles(p);.    if(
35734 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35735 20 67 6f 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f   goto fts3_init_
35736 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  out;.  }..  rc =
35737 20 66 74 73 33 44 65 63 6c 61 72 65 56 74 61 62   fts3DeclareVtab
35738 28 70 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (p);.  if( rc!=S
35739 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
3573a 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74 3b 0a 0a  fts3_init_out;..
3573b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
3573c 53 54 0a 20 20 69 66 28 20 7a 54 65 73 74 50 61  ST.  if( zTestPa
3573d 72 61 6d 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4e  ram ){.    p->nN
3573e 6f 64 65 53 69 7a 65 20 3d 20 61 74 6f 69 28 26  odeSize = atoi(&
3573f 7a 54 65 73 74 50 61 72 61 6d 5b 35 5d 29 3b 0a  zTestParam[5]);.
35740 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70    }.#endif.  *pp
35741 56 54 61 62 20 3d 20 26 70 2d 3e 62 61 73 65 3b  VTab = &p->base;
35742 0a 0a 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74 3a  ..fts3_init_out:
35743 0a 20 20 61 73 73 65 72 74 28 20 70 20 7c 7c 20  .  assert( p || 
35744 28 70 54 6f 6b 65 6e 69 7a 65 72 20 26 26 20 72  (pTokenizer && r
35745 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b  c!=SQLITE_OK) );
35746 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35747 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
35748 70 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 44  p ){.      fts3D
35749 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28  isconnectMethod(
3574a 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
3574b 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  p);.    }else{. 
3574c 20 20 20 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d       pTokenizer-
3574d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72  >pModule->xDestr
3574e 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  oy(pTokenizer);.
3574f 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
35750 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
35751 54 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 61  The xConnect() a
35752 6e 64 20 78 43 72 65 61 74 65 28 29 20 6d 65 74  nd xCreate() met
35753 68 6f 64 73 20 66 6f 72 20 74 68 65 20 76 69 72  hods for the vir
35754 74 75 61 6c 20 74 61 62 6c 65 2e 20 41 6c 6c 20  tual table. All 
35755 74 68 65 0a 2a 2a 20 77 6f 72 6b 20 69 73 20 64  the.** work is d
35756 6f 6e 65 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20  one in function 
35757 66 74 73 33 49 6e 69 74 56 74 61 62 28 29 2e 0a  fts3InitVtab()..
35758 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
35759 73 33 43 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28  s3ConnectMethod(
3575a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3575b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3575c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
3575d 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76  onnection */.  v
3575e 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20  oid *pAux,      
3575f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35760 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
35761 65 6e 69 7a 65 72 20 68 61 73 68 20 74 61 62 6c  enizer hash tabl
35762 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c  e */.  int argc,
35763 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35764 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
35765 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
35766 61 72 67 76 20 61 72 72 61 79 20 2a 2f 0a 20 20  argv array */.  
35767 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
35768 73 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20  st *argv,       
35769 2f 2a 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  /* xCreate/xConn
3576a 65 63 74 20 61 72 67 75 6d 65 6e 74 20 61 72 72  ect argument arr
3576b 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ay */.  sqlite3_
3576c 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 20 20  vtab **ppVtab,  
3576d 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3576e 4e 65 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62  New sqlite3_vtab
3576f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 68 61   object */.  cha
35770 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20  r **pzErr       
35771 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35772 4f 55 54 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  OUT: sqlite3_mal
35773 6c 6f 63 27 64 20 65 72 72 6f 72 20 6d 65 73 73  loc'd error mess
35774 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75  age */.){.  retu
35775 72 6e 20 66 74 73 33 49 6e 69 74 56 74 61 62 28  rn fts3InitVtab(
35776 30 2c 20 64 62 2c 20 70 41 75 78 2c 20 61 72 67  0, db, pAux, arg
35777 63 2c 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c  c, argv, ppVtab,
35778 20 70 7a 45 72 72 29 3b 0a 7d 0a 73 74 61 74 69   pzErr);.}.stati
35779 63 20 69 6e 74 20 66 74 73 33 43 72 65 61 74 65  c int fts3Create
3577a 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65  Method(.  sqlite
3577b 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3577c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
3577d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3577e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
3577f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35780 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
35781 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61   to tokenizer ha
35782 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  sh table */.  in
35783 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
35784 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35785 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
35786 6e 74 73 20 69 6e 20 61 72 67 76 20 61 72 72 61  nts in argv arra
35787 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  y */.  const cha
35788 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
35789 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74         /* xCreat
3578a 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d  e/xConnect argum
3578b 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
3578c 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
3578d 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  Vtab,          /
3578e 2a 20 4f 55 54 3a 20 4e 65 77 20 73 71 6c 69 74  * OUT: New sqlit
3578f 65 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 2a  e3_vtab object *
35790 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
35791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35792 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73 71 6c 69      /* OUT: sqli
35793 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20 65 72 72  te3_malloc'd err
35794 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 29 7b  or message */.){
35795 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 49 6e  .  return fts3In
35796 69 74 56 74 61 62 28 31 2c 20 64 62 2c 20 70 41  itVtab(1, db, pA
35797 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  ux, argc, argv, 
35798 70 70 56 74 61 62 2c 20 70 7a 45 72 72 29 3b 0a  ppVtab, pzErr);.
35799 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6d 70 6c 65 6d  }../* .** Implem
3579a 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
3579b 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f  xBestIndex metho
3579c 64 20 66 6f 72 20 46 54 53 33 20 74 61 62 6c 65  d for FTS3 table
3579d 73 2e 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  s. There.** are 
3579e 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 73  three possible s
3579f 74 72 61 74 65 67 69 65 73 2c 20 69 6e 20 6f 72  trategies, in or
357a0 64 65 72 20 6f 66 20 70 72 65 66 65 72 65 6e 63  der of preferenc
357a1 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 44 69  e:.**.**   1. Di
357a2 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72  rect lookup by r
357a3 6f 77 69 64 20 6f 72 20 64 6f 63 69 64 2e 20 0a  owid or docid. .
357a4 2a 2a 20 20 20 32 2e 20 46 75 6c 6c 2d 74 65 78  **   2. Full-tex
357a5 74 20 73 65 61 72 63 68 20 75 73 69 6e 67 20 61  t search using a
357a6 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
357a7 6f 6e 20 61 20 6e 6f 6e 2d 64 6f 63 69 64 20 63  on a non-docid c
357a8 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 33 2e 20 4c  olumn..**   3. L
357a9 69 6e 65 61 72 20 73 63 61 6e 20 6f 66 20 25 5f  inear scan of %_
357aa 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  content table..*
357ab 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
357ac 33 42 65 73 74 49 6e 64 65 78 4d 65 74 68 6f 64  3BestIndexMethod
357ad 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
357ae 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  VTab, sqlite3_in
357af 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29  dex_info *pInfo)
357b0 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  {.  Fts3Table *p
357b1 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
357b2 70 56 54 61 62 3b 0a 20 20 69 6e 74 20 69 3b 20  pVTab;.  int i; 
357b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357b4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
357b5 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
357b6 0a 20 20 69 6e 74 20 69 43 6f 6e 73 20 3d 20 2d  .  int iCons = -
357b7 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
357b8 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
357b9 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 75 73 65  onstraint to use
357ba 20 2a 2f 0a 0a 20 20 2f 2a 20 42 79 20 64 65 66   */..  /* By def
357bb 61 75 6c 74 20 75 73 65 20 61 20 66 75 6c 6c 20  ault use a full 
357bc 74 61 62 6c 65 20 73 63 61 6e 2e 20 54 68 69 73  table scan. This
357bd 20 69 73 20 61 6e 20 65 78 70 65 6e 73 69 76 65   is an expensive
357be 20 6f 70 74 69 6f 6e 2c 0a 20 20 2a 2a 20 73 6f   option,.  ** so
357bf 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 68 20   search through 
357c0 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  the constraints 
357c1 74 6f 20 73 65 65 20 69 66 20 61 20 6d 6f 72 65  to see if a more
357c2 20 65 66 66 69 63 69 65 6e 74 20 0a 20 20 2a 2a   efficient .  **
357c3 20 73 74 72 61 74 65 67 79 20 69 73 20 70 6f 73   strategy is pos
357c4 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 49  sible..  */.  pI
357c5 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 46 54  nfo->idxNum = FT
357c6 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52  S3_FULLSCAN_SEAR
357c7 43 48 3b 0a 20 20 70 49 6e 66 6f 2d 3e 65 73 74  CH;.  pInfo->est
357c8 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 35 30 30  imatedCost = 500
357c9 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  000;.  for(i=0; 
357ca 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  i<pInfo->nConstr
357cb 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aint; i++){.    
357cc 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
357cd 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20  ndex_constraint 
357ce 2a 70 43 6f 6e 73 20 3d 20 26 70 49 6e 66 6f 2d  *pCons = &pInfo-
357cf 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b  >aConstraint[i];
357d0 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e  .    if( pCons->
357d1 75 73 61 62 6c 65 3d 3d 30 20 29 20 63 6f 6e 74  usable==0 ) cont
357d2 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 41 20  inue;..    /* A 
357d3 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 6f 6e  direct lookup on
357d4 20 74 68 65 20 72 6f 77 69 64 20 6f 72 20 64 6f   the rowid or do
357d5 63 69 64 20 63 6f 6c 75 6d 6e 2e 20 54 68 69 73  cid column. This
357d6 20 69 73 20 74 68 65 20 62 65 73 74 0a 20 20 20   is the best.   
357d7 20 2a 2a 20 73 74 72 61 74 65 67 79 20 69 6e 20   ** strategy in 
357d8 61 6c 6c 20 63 61 73 65 73 2e 20 41 73 73 69 67  all cases. Assig
357d9 6e 20 61 20 63 6f 73 74 20 6f 66 20 31 2e 30 20  n a cost of 1.0 
357da 61 6e 64 20 72 65 74 75 72 6e 20 65 61 72 6c 79  and return early
357db 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
357dc 20 70 43 6f 6e 73 2d 3e 6f 70 3d 3d 53 51 4c 49   pCons->op==SQLI
357dd 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
357de 49 4e 54 5f 45 51 20 0a 20 20 20 20 20 26 26 20  INT_EQ .     && 
357df 28 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3c  (pCons->iColumn<
357e0 30 20 7c 7c 20 70 43 6f 6e 73 2d 3e 69 43 6f 6c  0 || pCons->iCol
357e1 75 6d 6e 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b  umn==p->nColumn+
357e2 31 20 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  1 ).    ){.     
357e3 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d   pInfo->idxNum =
357e4 20 46 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52   FTS3_DOCID_SEAR
357e5 43 48 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  CH;.      pInfo-
357e6 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d  >estimatedCost =
357e7 20 31 2e 30 3b 0a 20 20 20 20 20 20 69 43 6f 6e   1.0;.      iCon
357e8 73 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 72 65  s = i;.      bre
357e9 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
357ea 2a 20 41 20 4d 41 54 43 48 20 63 6f 6e 73 74 72  * A MATCH constr
357eb 61 69 6e 74 2e 20 55 73 65 20 61 20 66 75 6c 6c  aint. Use a full
357ec 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a 20 20  -text search..  
357ed 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74    **.    ** If t
357ee 68 65 72 65 20 69 73 20 6d 6f 72 65 20 74 68 61  here is more tha
357ef 6e 20 6f 6e 65 20 4d 41 54 43 48 20 63 6f 6e 73  n one MATCH cons
357f0 74 72 61 69 6e 74 20 61 76 61 69 6c 61 62 6c 65  traint available
357f1 2c 20 75 73 65 20 74 68 65 20 66 69 72 73 74 0a  , use the first.
357f2 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 63 6f 75      ** one encou
357f3 6e 74 65 72 65 64 2e 20 49 66 20 74 68 65 72 65  ntered. If there
357f4 20 69 73 20 62 6f 74 68 20 61 20 4d 41 54 43 48   is both a MATCH
357f5 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20   constraint and 
357f6 61 20 64 69 72 65 63 74 0a 20 20 20 20 2a 2a 20  a direct.    ** 
357f7 72 6f 77 69 64 2f 64 6f 63 69 64 20 6c 6f 6f 6b  rowid/docid look
357f8 75 70 2c 20 70 72 65 66 65 72 20 74 68 65 20 72  up, prefer the r
357f9 6f 77 69 64 2f 64 6f 63 69 64 20 73 74 72 61 74  owid/docid strat
357fa 65 67 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  egy..    */.    
357fb 69 66 28 20 69 43 6f 6e 73 3c 30 20 0a 20 20 20  if( iCons<0 .   
357fc 20 20 26 26 20 70 43 6f 6e 73 2d 3e 6f 70 3d 3d    && pCons->op==
357fd 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
357fe 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 0a 20  STRAINT_MATCH . 
357ff 20 20 20 20 26 26 20 70 43 6f 6e 73 2d 3e 69 43      && pCons->iC
35800 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e  olumn>=0 && pCon
35801 73 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 70 2d 3e 6e  s->iColumn<=p->n
35802 43 6f 6c 75 6d 6e 0a 20 20 20 20 29 7b 0a 20 20  Column.    ){.  
35803 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75      pInfo->idxNu
35804 6d 20 3d 20 46 54 53 33 5f 46 55 4c 4c 54 45 58  m = FTS3_FULLTEX
35805 54 5f 53 45 41 52 43 48 20 2b 20 70 43 6f 6e 73  T_SEARCH + pCons
35806 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
35807 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65   pInfo->estimate
35808 64 43 6f 73 74 20 3d 20 32 2e 30 3b 0a 20 20 20  dCost = 2.0;.   
35809 20 20 20 69 43 6f 6e 73 20 3d 20 69 3b 0a 20 20     iCons = i;.  
3580a 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69    }.  }..  if( i
3580b 43 6f 6e 73 3e 3d 30 20 29 7b 0a 20 20 20 20 70  Cons>=0 ){.    p
3580c 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
3580d 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d 2e 61 72  tUsage[iCons].ar
3580e 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20  gvIndex = 1;.   
3580f 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61   pInfo->aConstra
35810 69 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d 2e  intUsage[iCons].
35811 6f 6d 69 74 20 3d 20 31 3b 0a 20 20 7d 20 0a 20  omit = 1;.  } . 
35812 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35813 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  K;.}../*.** Impl
35814 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 4f  ementation of xO
35815 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  pen method..*/.s
35816 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4f 70  tatic int fts3Op
35817 65 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  enMethod(sqlite3
35818 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71  _vtab *pVTab, sq
35819 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
3581a 72 20 2a 2a 70 70 43 73 72 29 7b 0a 20 20 73 71  r **ppCsr){.  sq
3581b 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
3581c 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20  r *pCsr;        
3581d 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61         /* Alloca
3581e 74 65 64 20 63 75 72 73 6f 72 20 2a 2f 0a 0a 20  ted cursor */.. 
3581f 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
35820 52 28 70 56 54 61 62 29 3b 0a 0a 20 20 2f 2a 20  R(pVTab);..  /* 
35821 41 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65  Allocate a buffe
35822 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66  r large enough f
35823 6f 72 20 61 6e 20 46 74 73 33 43 75 72 73 6f 72  or an Fts3Cursor
35824 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 74   structure. If t
35825 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69  he.  ** allocati
35826 6f 6e 20 73 75 63 63 65 65 64 73 2c 20 7a 65 72  on succeeds, zer
35827 6f 20 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20  o it and return 
35828 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72  SQLITE_OK. Other
35829 77 69 73 65 2c 20 0a 20 20 2a 2a 20 69 66 20 74  wise, .  ** if t
3582a 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  he allocation fa
3582b 69 6c 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ils, return SQLI
3582c 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20  TE_NOMEM..  */. 
3582d 20 2a 70 70 43 73 72 20 3d 20 70 43 73 72 20 3d   *ppCsr = pCsr =
3582e 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63   (sqlite3_vtab_c
3582f 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
35830 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74  malloc(sizeof(Ft
35831 73 33 43 75 72 73 6f 72 29 29 3b 0a 20 20 69 66  s3Cursor));.  if
35832 28 20 21 70 43 73 72 20 29 7b 0a 20 20 20 20 72  ( !pCsr ){.    r
35833 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
35834 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  EM;.  }.  memset
35835 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66  (pCsr, 0, sizeof
35836 28 46 74 73 33 43 75 72 73 6f 72 29 29 3b 0a 20  (Fts3Cursor));. 
35837 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35838 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  K;.}../*********
35839 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3583a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3583b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3583c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3583d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3583e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3583f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
35841 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35842 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35843 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35844 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
35845 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35846 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35847 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35848 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35849 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  /.../*.** Close 
3584a 74 68 65 20 63 75 72 73 6f 72 2e 20 20 46 6f 72  the cursor.  For
3584b 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
3584c 72 6d 61 74 69 6f 6e 20 73 65 65 20 74 68 65 20  rmation see the 
3584d 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  documentation.**
3584e 20 6f 6e 20 74 68 65 20 78 43 6c 6f 73 65 20 6d   on the xClose m
3584f 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72  ethod of the vir
35850 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72  tual table inter
35851 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  face..*/.static 
35852 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73  int fulltextClos
35853 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e(sqlite3_vtab_c
35854 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
35855 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
35856 43 73 72 20 3d 20 28 46 74 73 33 43 75 72 73 6f  Csr = (Fts3Curso
35857 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20 20 73  r *)pCursor;.  s
35858 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
35859 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  pCsr->pStmt);.  
3585a 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
3585b 72 65 65 28 70 43 73 72 2d 3e 70 45 78 70 72 29  ree(pCsr->pExpr)
3585c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3585d 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 29  (pCsr->aDoclist)
3585e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3585f 28 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e  (pCsr);.  return
35860 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73   SQLITE_OK;.}..s
35861 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 75  tatic int fts3Cu
35862 72 73 6f 72 53 65 65 6b 28 46 74 73 33 43 75 72  rsorSeek(Fts3Cur
35863 73 6f 72 20 2a 70 43 73 72 29 7b 0a 20 20 69 66  sor *pCsr){.  if
35864 28 20 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72  ( pCsr->isRequir
35865 65 53 65 65 6b 20 29 7b 0a 20 20 20 20 70 43 73  eSeek ){.    pCs
35866 72 2d 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b  r->isRequireSeek
35867 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
35868 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 43 73  3_bind_int64(pCs
35869 72 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 70 43 73  r->pStmt, 1, pCs
3586a 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a 20 20 20  r->iPrevId);.   
3586b 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
3586c 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 43  =sqlite3_step(pC
3586d 73 72 2d 3e 70 53 74 6d 74 29 20 29 7b 0a 20 20  sr->pStmt) ){.  
3586e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3586f 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  E_OK;.    }else{
35870 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  .      int rc;. 
35871 20 20 20 20 20 70 43 73 72 2d 3e 69 73 45 6f 66       pCsr->isEof
35872 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   = 1;.      if( 
35873 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
35874 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
35875 43 73 72 2d 3e 70 53 74 6d 74 29 29 20 29 7b 0a  Csr->pStmt)) ){.
35876 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
35877 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
35878 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
35879 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  rc;.    }.  }els
3587a 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
3587b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a  LITE_OK;.  }.}..
3587c 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e  static int fts3N
3587d 65 78 74 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  extMethod(sqlite
3587e 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
3587f 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 72  Cursor){.  int r
35880 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
35881 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
35882 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 46  turn code */.  F
35883 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20  ts3Cursor *pCsr 
35884 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29  = (Fts3Cursor *)
35885 70 43 75 72 73 6f 72 3b 0a 0a 20 20 69 66 28 20  pCursor;..  if( 
35886 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 3d 3d  pCsr->aDoclist==
35887 30 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c  0 ){.    if( SQL
35888 49 54 45 5f 52 4f 57 21 3d 73 71 6c 69 74 65 33  ITE_ROW!=sqlite3
35889 5f 73 74 65 70 28 70 43 73 72 2d 3e 70 53 74 6d  _step(pCsr->pStm
3588a 74 29 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  t) ){.      pCsr
3588b 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a 20 20 20  ->isEof = 1;.   
3588c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3588d 72 65 73 65 74 28 70 43 73 72 2d 3e 70 53 74 6d  reset(pCsr->pStm
3588e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  t);.    }.  }els
3588f 65 20 69 66 28 20 70 43 73 72 2d 3e 70 4e 65 78  e if( pCsr->pNex
35890 74 49 64 3e 3d 26 70 43 73 72 2d 3e 61 44 6f 63  tId>=&pCsr->aDoc
35891 6c 69 73 74 5b 70 43 73 72 2d 3e 6e 44 6f 63 6c  list[pCsr->nDocl
35892 69 73 74 5d 20 29 7b 0a 20 20 20 20 70 43 73 72  ist] ){.    pCsr
35893 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a 20 20 7d  ->isEof = 1;.  }
35894 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
35895 33 5f 72 65 73 65 74 28 70 43 73 72 2d 3e 70 53  3_reset(pCsr->pS
35896 74 6d 74 29 3b 0a 20 20 20 20 66 74 73 33 47 65  tmt);.    fts3Ge
35897 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 43  tDeltaVarint(&pC
35898 73 72 2d 3e 70 4e 65 78 74 49 64 2c 20 26 70 43  sr->pNextId, &pC
35899 73 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a 20 20  sr->iPrevId);.  
3589a 20 20 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72    pCsr->isRequir
3589b 65 53 65 65 6b 20 3d 20 31 3b 0a 20 20 7d 0a 20  eSeek = 1;.  }. 
3589c 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
3589d 2f 2a 0a 2a 2a 20 54 68 65 20 62 75 66 66 65 72  /*.** The buffer
3589e 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
3589f 72 67 75 6d 65 6e 74 20 7a 4e 6f 64 65 20 28 73  rgument zNode (s
358a0 69 7a 65 20 6e 4e 6f 64 65 20 62 79 74 65 73 29  ize nNode bytes)
358a1 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a   contains the.**
358a2 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 61 20   root node of a 
358a3 62 2d 74 72 65 65 20 73 65 67 6d 65 6e 74 2e 20  b-tree segment. 
358a4 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73 20 67  The segment is g
358a5 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20  uaranteed to be 
358a6 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65 20  at least.** one 
358a7 6c 65 76 65 6c 20 68 69 67 68 20 28 69 2e 65 2e  level high (i.e.
358a8 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69   the root node i
358a9 73 20 6e 6f 74 20 61 6c 73 6f 20 61 20 6c 65 61  s not also a lea
358aa 66 29 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  f). If successfu
358ab 6c 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  l,.** this funct
358ac 69 6f 6e 20 6c 6f 63 61 74 65 73 20 74 68 65 20  ion locates the 
358ad 6c 65 61 66 20 6e 6f 64 65 20 6f 66 20 74 68 65  leaf node of the
358ae 20 73 65 67 6d 65 6e 74 20 74 68 61 74 20 6d 61   segment that ma
358af 79 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 0a 2a  y contain the .*
358b0 2a 20 74 65 72 6d 20 73 70 65 63 69 66 69 65 64  * term specified
358b1 20 62 79 20 61 72 67 75 6d 65 6e 74 73 20 7a 54   by arguments zT
358b2 65 72 6d 20 61 6e 64 20 6e 54 65 72 6d 20 61 6e  erm and nTerm an
358b3 64 20 77 72 69 74 65 73 20 69 74 73 20 62 6c 6f  d writes its blo
358b4 63 6b 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 74 6f  ck number .** to
358b5 20 2a 70 69 4c 65 61 66 2e 0a 2a 2a 0a 2a 2a 20   *piLeaf..**.** 
358b6 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  It is possible t
358b7 68 61 74 20 74 68 65 20 72 65 74 75 72 6e 65 64  hat the returned
358b8 20 6c 65 61 66 20 6e 6f 64 65 20 64 6f 65 73 20   leaf node does 
358b9 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  not contain the 
358ba 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74 65 72  specified.** ter
358bb 6d 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74  m. However, if t
358bc 68 65 20 73 65 67 6d 65 6e 74 20 64 6f 65 73 20  he segment does 
358bd 63 6f 6e 74 61 69 6e 20 73 61 69 64 20 74 65 72  contain said ter
358be 6d 2c 20 69 74 20 69 73 20 73 74 6f 72 65 64 20  m, it is stored 
358bf 6f 6e 0a 2a 2a 20 74 68 65 20 69 64 65 6e 74 69  on.** the identi
358c0 66 69 65 64 20 6c 65 61 66 20 6e 6f 64 65 2e 20  fied leaf node. 
358c1 42 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e  Because this fun
358c2 63 74 69 6f 6e 20 6f 6e 6c 79 20 69 6e 73 70 65  ction only inspe
358c3 63 74 73 20 69 6e 74 65 72 69 6f 72 0a 2a 2a 20  cts interior.** 
358c4 73 65 67 6d 65 6e 74 20 6e 6f 64 65 73 20 28 61  segment nodes (a
358c5 6e 64 20 6e 65 76 65 72 20 6c 6f 61 64 73 20 6c  nd never loads l
358c6 65 61 66 20 6e 6f 64 65 73 20 69 6e 74 6f 20 6d  eaf nodes into m
358c7 65 6d 6f 72 79 29 2c 20 69 74 20 69 73 20 6e 6f  emory), it is no
358c8 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f  t possible.** to
358c9 20 62 65 20 73 75 72 65 2e 0a 2a 2a 0a 2a 2a 20   be sure..**.** 
358ca 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
358cb 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rs, an error cod
358cc 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  e other than SQL
358cd 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
358ce 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 69  ed..*/ .static i
358cf 6e 74 20 66 74 73 33 53 65 6c 65 63 74 4c 65 61  nt fts3SelectLea
358d0 66 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  f(.  Fts3Table *
358d1 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
358d2 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
358d3 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
358d4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
358d5 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
358d6 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 73 65 6c    /* Term to sel
358d7 65 63 74 20 6c 65 61 76 65 73 20 66 6f 72 20 2a  ect leaves for *
358d8 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20  /.  int nTerm,  
358d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358da 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
358db 65 72 6d 20 7a 54 65 72 6d 20 69 6e 20 62 79 74  erm zTerm in byt
358dc 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
358dd 61 72 20 2a 7a 4e 6f 64 65 2c 20 20 20 20 20 20  ar *zNode,      
358de 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
358df 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73 65 67  r containing seg
358e0 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  ment interior no
358e1 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 6f 64  de */.  int nNod
358e2 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
358e3 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
358e4 6f 66 20 62 75 66 66 65 72 20 61 74 20 7a 4e 6f  of buffer at zNo
358e5 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  de */.  sqlite3_
358e6 69 6e 74 36 34 20 2a 70 69 4c 65 61 66 20 20 20  int64 *piLeaf   
358e7 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63          /* Selec
358e8 74 65 64 20 6c 65 61 66 20 6e 6f 64 65 20 2a 2f  ted leaf node */
358e9 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
358ea 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
358eb 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
358ec 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  code */.  const 
358ed 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 4e 6f  char *zCsr = zNo
358ee 64 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72  de;       /* Cur
358ef 73 6f 72 20 74 6f 20 69 74 65 72 61 74 65 20 74  sor to iterate t
358f0 68 72 6f 75 67 68 20 6e 6f 64 65 20 2a 2f 0a 20  hrough node */. 
358f1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e   const char *zEn
358f2 64 20 3d 20 26 7a 43 73 72 5b 6e 4e 6f 64 65 5d  d = &zCsr[nNode]
358f3 3b 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 74 65 72  ;/* End of inter
358f4 69 6f 72 20 6e 6f 64 65 20 62 75 66 66 65 72 20  ior node buffer 
358f5 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 66  */.  char *zBuff
358f6 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  er = 0;         
358f7 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74       /* Buffer t
358f8 6f 20 6c 6f 61 64 20 74 65 72 6d 73 20 69 6e 74  o load terms int
358f9 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f  o */.  int nAllo
358fa 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  c = 0;          
358fb 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
358fc 66 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  f allocated buff
358fd 65 72 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20  er */..  while( 
358fe 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 46  1 ){.    int isF
358ff 69 72 73 74 54 65 72 6d 20 3d 20 31 3b 20 20 20  irstTerm = 1;   
35900 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77         /* True w
35901 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 66  hen processing f
35902 69 72 73 74 20 74 65 72 6d 20 6f 6e 20 70 61 67  irst term on pag
35903 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 48 65  e */.    int iHe
35904 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
35905 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74         /* Height
35906 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 20 69 6e   of this node in
35907 20 74 72 65 65 20 2a 2f 0a 20 20 20 20 73 71 6c   tree */.    sql
35908 69 74 65 33 5f 69 6e 74 36 34 20 69 43 68 69 6c  ite3_int64 iChil
35909 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c  d;         /* Bl
3590a 6f 63 6b 20 69 64 20 6f 66 20 63 68 69 6c 64 20  ock id of child 
3590b 6e 6f 64 65 20 74 6f 20 64 65 73 63 65 6e 64 20  node to descend 
3590c 74 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 42  to */.    int nB
3590d 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  lock;           
3590e 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
3590f 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 69 6e  of child node in
35910 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 20 20 7a   bytes */..    z
35911 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  Csr += sqlite3Ft
35912 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a 43  s3GetVarint32(zC
35913 73 72 2c 20 26 69 48 65 69 67 68 74 29 3b 0a 20  sr, &iHeight);. 
35914 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74     zCsr += sqlit
35915 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28  e3Fts3GetVarint(
35916 7a 43 73 72 2c 20 26 69 43 68 69 6c 64 29 3b 0a  zCsr, &iChild);.
35917 20 20 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 43    .    while( zC
35918 73 72 3c 7a 45 6e 64 20 29 7b 0a 20 20 20 20 20  sr<zEnd ){.     
35919 20 69 6e 74 20 63 6d 70 3b 20 20 20 20 20 20 20   int cmp;       
3591a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3591b 6d 65 6d 63 6d 70 28 29 20 72 65 73 75 6c 74 20  memcmp() result 
3591c 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 53 75  */.      int nSu
3591d 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  ffix;           
3591e 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
3591f 74 65 72 6d 20 73 75 66 66 69 78 20 2a 2f 0a 20  term suffix */. 
35920 20 20 20 20 20 69 6e 74 20 6e 50 72 65 66 69 78       int nPrefix
35921 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
35922 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65 72 6d   /* Size of term
35923 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 20 20 20   prefix */.     
35924 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 20 20 20   int nBuffer;   
35925 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35926 54 6f 74 61 6c 20 74 65 72 6d 20 73 69 7a 65 20  Total term size 
35927 2a 2f 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 4c  */.  .      /* L
35928 6f 61 64 20 74 68 65 20 6e 65 78 74 20 74 65 72  oad the next ter
35929 6d 20 6f 6e 20 74 68 65 20 6e 6f 64 65 20 69 6e  m on the node in
3592a 74 6f 20 7a 42 75 66 66 65 72 20 2a 2f 0a 20 20  to zBuffer */.  
3592b 20 20 20 20 69 66 28 20 21 69 73 46 69 72 73 74      if( !isFirst
3592c 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
3592d 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46  zCsr += sqlite3F
3592e 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a  ts3GetVarint32(z
3592f 43 73 72 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  Csr, &nPrefix);.
35930 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 73        }.      is
35931 46 69 72 73 74 54 65 72 6d 20 3d 20 30 3b 0a 20  FirstTerm = 0;. 
35932 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c       zCsr += sql
35933 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
35934 74 33 32 28 7a 43 73 72 2c 20 26 6e 53 75 66 66  t32(zCsr, &nSuff
35935 69 78 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  ix);.      if( n
35936 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 3e 6e  Prefix+nSuffix>n
35937 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20  Alloc ){.       
35938 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20   char *zNew;.   
35939 20 20 20 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 6e       nAlloc = (n
3593a 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 29 20  Prefix+nSuffix) 
3593b 2a 20 32 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65  * 2;.        zNe
3593c 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  w = (char *)sqli
3593d 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 42 75 66  te3_realloc(zBuf
3593e 66 65 72 2c 20 6e 41 6c 6c 6f 63 29 3b 0a 20 20  fer, nAlloc);.  
3593f 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20        if( !zNew 
35940 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
35941 69 74 65 33 5f 66 72 65 65 28 7a 42 75 66 66 65  ite3_free(zBuffe
35942 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  r);.          re
35943 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
35944 4d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  M;.        }.   
35945 20 20 20 20 20 7a 42 75 66 66 65 72 20 3d 20 7a       zBuffer = z
35946 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  New;.      }.   
35947 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 66     memcpy(&zBuff
35948 65 72 5b 6e 50 72 65 66 69 78 5d 2c 20 7a 43 73  er[nPrefix], zCs
35949 72 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 20  r, nSuffix);.   
3594a 20 20 20 6e 42 75 66 66 65 72 20 3d 20 6e 50 72     nBuffer = nPr
3594b 65 66 69 78 20 2b 20 6e 53 75 66 66 69 78 3b 0a  efix + nSuffix;.
3594c 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6e 53        zCsr += nS
3594d 75 66 66 69 78 3b 0a 20 20 0a 20 20 20 20 20 20  uffix;.  .      
3594e 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 74  /* Compare the t
3594f 65 72 6d 20 77 65 20 61 72 65 20 73 65 61 72 63  erm we are searc
35950 68 69 6e 67 20 66 6f 72 20 77 69 74 68 20 74 68  hing for with th
35951 65 20 74 65 72 6d 20 6a 75 73 74 20 6c 6f 61 64  e term just load
35952 65 64 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a  ed from.      **
35953 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
35954 64 65 2e 20 49 66 20 74 68 65 20 73 70 65 63 69  de. If the speci
35955 66 69 65 64 20 74 65 72 6d 20 69 73 20 67 72 65  fied term is gre
35956 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
35957 61 6c 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  al.      ** to t
35958 68 65 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65  he term from the
35959 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20   interior node, 
3595a 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f  then all terms o
3595b 6e 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 0a  n the sub-tree .
3595c 20 20 20 20 20 20 2a 2a 20 68 65 61 64 65 64 20        ** headed 
3595d 62 79 20 6e 6f 64 65 20 69 43 68 69 6c 64 20 61  by node iChild a
3595e 72 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  re smaller than 
3595f 7a 54 65 72 6d 2e 20 4e 6f 20 6e 65 65 64 20 74  zTerm. No need t
35960 6f 20 73 65 61 72 63 68 20 0a 20 20 20 20 20 20  o search .      
35961 2a 2a 20 69 43 68 69 6c 64 2e 0a 20 20 20 20 20  ** iChild..     
35962 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
35963 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  the interior nod
35964 65 20 74 65 72 6d 20 69 73 20 6c 61 72 67 65 72  e term is larger
35965 20 74 68 61 6e 20 74 68 65 20 73 70 65 63 69 66   than the specif
35966 69 65 64 20 74 65 72 6d 2c 20 74 68 65 6e 0a 20  ied term, then. 
35967 20 20 20 20 20 2a 2a 20 74 68 65 20 74 72 65 65       ** the tree
35968 20 68 65 61 64 65 64 20 62 79 20 69 43 68 69 6c   headed by iChil
35969 64 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 74 68  d may contain th
3596a 65 20 73 70 65 63 69 66 69 65 64 20 74 65 72 6d  e specified term
3596b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
3596c 20 63 6d 70 20 3d 20 6d 65 6d 63 6d 70 28 7a 54   cmp = memcmp(zT
3596d 65 72 6d 2c 20 7a 42 75 66 66 65 72 2c 20 28 6e  erm, zBuffer, (n
3596e 42 75 66 66 65 72 3e 6e 54 65 72 6d 20 3f 20 6e  Buffer>nTerm ? n
3596f 54 65 72 6d 20 3a 20 6e 42 75 66 66 65 72 29 29  Term : nBuffer))
35970 3b 0a 20 20 20 20 20 20 69 66 28 20 63 6d 70 3c  ;.      if( cmp<
35971 30 20 7c 7c 20 28 63 6d 70 3d 3d 30 20 26 26 20  0 || (cmp==0 && 
35972 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 29 20 29  nBuffer>nTerm) )
35973 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 43   break;.      iC
35974 68 69 6c 64 2b 2b 3b 0a 20 20 20 20 7d 3b 0a 0a  hild++;.    };..
35975 20 20 20 20 2f 2a 20 49 66 20 28 69 48 65 69 67      /* If (iHeig
35976 68 74 3d 3d 31 29 2c 20 74 68 65 20 63 68 69 6c  ht==1), the chil
35977 64 72 65 6e 20 6f 66 20 74 68 69 73 20 69 6e 74  dren of this int
35978 65 72 69 6f 72 20 6e 6f 64 65 20 61 72 65 20 6c  erior node are l
35979 65 61 76 65 73 2e 20 54 68 65 0a 20 20 20 20 2a  eaves. The.    *
3597a 2a 20 73 70 65 63 69 66 69 65 64 20 74 65 72 6d  * specified term
3597b 20 6d 61 79 20 62 65 20 70 72 65 73 65 6e 74 20   may be present 
3597c 6f 6e 20 6c 65 61 66 20 6e 6f 64 65 20 69 43 68  on leaf node iCh
3597d 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ild..    */.    
3597e 69 66 28 20 69 48 65 69 67 68 74 3d 3d 31 20 29  if( iHeight==1 )
3597f 7b 0a 20 20 20 20 20 20 2a 70 69 4c 65 61 66 20  {.      *piLeaf 
35980 3d 20 69 43 68 69 6c 64 3b 0a 20 20 20 20 20 20  = iChild;.      
35981 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
35982 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20    /* Descend to 
35983 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 43  interior node iC
35984 68 69 6c 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20  hild. */.    rc 
35985 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  = sqlite3Fts3Rea
35986 64 42 6c 6f 63 6b 28 70 2c 20 69 43 68 69 6c 64  dBlock(p, iChild
35987 2c 20 26 7a 43 73 72 2c 20 26 6e 42 6c 6f 63 6b  , &zCsr, &nBlock
35988 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
35989 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b  QLITE_OK ) break
3598a 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a 43  ;.    zEnd = &zC
3598b 73 72 5b 6e 42 6c 6f 63 6b 5d 3b 0a 20 20 7d 0a  sr[nBlock];.  }.
3598c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3598d 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74 75 72  Buffer);.  retur
3598e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
3598f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
35990 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 64  used to create d
35991 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 73 65 72  elta-encoded ser
35992 69 61 6c 69 7a 65 64 20 6c 69 73 74 73 20 6f 66  ialized lists of
35993 20 46 54 53 33 20 0a 2a 2a 20 76 61 72 69 6e 74   FTS3 .** varint
35994 73 2e 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  s. Each call to 
35995 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 70  this function ap
35996 70 65 6e 64 73 20 61 20 73 69 6e 67 6c 65 20 76  pends a single v
35997 61 72 69 6e 74 20 74 6f 20 61 20 6c 69 73 74 2e  arint to a list.
35998 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35999 66 74 73 33 50 75 74 44 65 6c 74 61 56 61 72 69  fts3PutDeltaVari
3599a 6e 74 28 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c  nt(.  char **pp,
3599b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3599c 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
3599d 20 4f 75 74 70 75 74 20 70 6f 69 6e 74 65 72 20   Output pointer 
3599e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
3599f 36 34 20 2a 70 69 50 72 65 76 2c 20 20 20 20 20  64 *piPrev,     
359a0 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
359a1 50 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77  Previous value w
359a2 72 69 74 74 65 6e 20 74 6f 20 6c 69 73 74 20 2a  ritten to list *
359a3 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
359a4 34 20 69 56 61 6c 20 20 20 20 20 20 20 20 20 20  4 iVal          
359a5 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 69      /* Write thi
359a6 73 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 6c  s value to the l
359a7 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65  ist */.){.  asse
359a8 72 74 28 20 69 56 61 6c 2d 2a 70 69 50 72 65 76  rt( iVal-*piPrev
359a9 20 3e 20 30 20 7c 7c 20 28 2a 70 69 50 72 65 76   > 0 || (*piPrev
359aa 3d 3d 30 20 26 26 20 69 56 61 6c 3d 3d 30 29 20  ==0 && iVal==0) 
359ab 29 3b 0a 20 20 2a 70 70 20 2b 3d 20 73 71 6c 69  );.  *pp += sqli
359ac 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74  te3Fts3PutVarint
359ad 28 2a 70 70 2c 20 69 56 61 6c 2d 2a 70 69 50 72  (*pp, iVal-*piPr
359ae 65 76 29 3b 0a 20 20 2a 70 69 50 72 65 76 20 3d  ev);.  *piPrev =
359af 20 69 56 61 6c 3b 0a 7d 0a 0a 73 74 61 74 69 63   iVal;.}..static
359b0 20 76 6f 69 64 20 66 74 73 33 50 6f 73 6c 69 73   void fts3Poslis
359b1 74 43 6f 70 79 28 63 68 61 72 20 2a 2a 70 70 2c  tCopy(char **pp,
359b2 20 63 68 61 72 20 2a 2a 70 70 50 6f 73 6c 69 73   char **ppPoslis
359b3 74 29 7b 0a 20 20 63 68 61 72 20 2a 70 45 6e 64  t){.  char *pEnd
359b4 20 3d 20 2a 70 70 50 6f 73 6c 69 73 74 3b 0a 20   = *ppPoslist;. 
359b5 20 63 68 61 72 20 63 20 3d 20 30 3b 0a 20 20 77   char c = 0;.  w
359b6 68 69 6c 65 28 20 2a 70 45 6e 64 20 7c 20 63 20  hile( *pEnd | c 
359b7 29 20 63 20 3d 20 2a 70 45 6e 64 2b 2b 20 26 20  ) c = *pEnd++ & 
359b8 30 78 38 30 3b 0a 20 20 70 45 6e 64 2b 2b 3b 0a  0x80;.  pEnd++;.
359b9 20 20 69 66 28 20 70 70 20 29 7b 0a 20 20 20 20    if( pp ){.    
359ba 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29 28 70 45  int n = (int)(pE
359bb 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69 73 74 29  nd - *ppPoslist)
359bc 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20  ;.    char *p = 
359bd 2a 70 70 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  *pp;.    memcpy(
359be 70 2c 20 2a 70 70 50 6f 73 6c 69 73 74 2c 20 6e  p, *ppPoslist, n
359bf 29 3b 0a 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20  );.    p += n;. 
359c0 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 7d 0a     *pp = p;.  }.
359c1 20 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20 70    *ppPoslist = p
359c2 45 6e 64 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  End;.}..static v
359c3 6f 69 64 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69  oid fts3Columnli
359c4 73 74 43 6f 70 79 28 63 68 61 72 20 2a 2a 70 70  stCopy(char **pp
359c5 2c 20 63 68 61 72 20 2a 2a 70 70 50 6f 73 6c 69  , char **ppPosli
359c6 73 74 29 7b 0a 20 20 63 68 61 72 20 2a 70 45 6e  st){.  char *pEn
359c7 64 20 3d 20 2a 70 70 50 6f 73 6c 69 73 74 3b 0a  d = *ppPoslist;.
359c8 20 20 63 68 61 72 20 63 20 3d 20 30 3b 0a 20 20    char c = 0;.  
359c9 77 68 69 6c 65 28 20 30 78 46 45 20 26 20 28 2a  while( 0xFE & (*
359ca 70 45 6e 64 20 7c 20 63 29 20 29 20 63 20 3d 20  pEnd | c) ) c = 
359cb 2a 70 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0a  *pEnd++ & 0x80;.
359cc 20 20 69 66 28 20 70 70 20 29 7b 0a 20 20 20 20    if( pp ){.    
359cd 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29 28 70 45  int n = (int)(pE
359ce 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69 73 74 29  nd - *ppPoslist)
359cf 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20  ;.    char *p = 
359d0 2a 70 70 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  *pp;.    memcpy(
359d1 70 2c 20 2a 70 70 50 6f 73 6c 69 73 74 2c 20 6e  p, *ppPoslist, n
359d2 29 3b 0a 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20  );.    p += n;. 
359d3 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 7d 0a     *pp = p;.  }.
359d4 20 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20 70    *ppPoslist = p
359d5 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 0a 2a 2f  End;.}../*.**.*/
359d6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
359d7 33 50 6f 73 6c 69 73 74 4d 65 72 67 65 28 0a 20  3PoslistMerge(. 
359d8 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20 20 20   char **pp,     
359d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359da 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
359db 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70  r */.  char **pp
359dc 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
359dd 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 69         /* Left i
359de 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 63  nput list */.  c
359df 68 61 72 20 2a 2a 70 70 32 20 20 20 20 20 20 20  har **pp2       
359e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
359e1 2a 20 52 69 67 68 74 20 69 6e 70 75 74 20 6c 69  * Right input li
359e2 73 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  st */.){.  char 
359e3 2a 70 20 3d 20 2a 70 70 3b 0a 20 20 63 68 61 72  *p = *pp;.  char
359e4 20 2a 70 31 20 3d 20 2a 70 70 31 3b 0a 20 20 63   *p1 = *pp1;.  c
359e5 68 61 72 20 2a 70 32 20 3d 20 2a 70 70 32 3b 0a  har *p2 = *pp2;.
359e6 0a 20 20 77 68 69 6c 65 28 20 2a 70 31 20 26 26  .  while( *p1 &&
359e7 20 2a 70 32 20 29 7b 0a 20 20 20 20 69 6e 74 20   *p2 ){.    int 
359e8 69 43 6f 6c 31 20 3d 20 30 3b 0a 20 20 20 20 69  iCol1 = 0;.    i
359e9 6e 74 20 69 43 6f 6c 32 20 3d 20 30 3b 0a 20 20  nt iCol2 = 0;.  
359ea 20 20 69 66 28 20 2a 70 31 3d 3d 30 78 30 31 20    if( *p1==0x01 
359eb 29 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  ) sqlite3Fts3Get
359ec 56 61 72 69 6e 74 33 32 28 26 70 31 5b 31 5d 2c  Varint32(&p1[1],
359ed 20 26 69 43 6f 6c 31 29 3b 0a 20 20 20 20 69 66   &iCol1);.    if
359ee 28 20 2a 70 32 3d 3d 30 78 30 31 20 29 20 73 71  ( *p2==0x01 ) sq
359ef 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
359f0 6e 74 33 32 28 26 70 32 5b 31 5d 2c 20 26 69 43  nt32(&p2[1], &iC
359f1 6f 6c 32 29 3b 0a 0a 20 20 20 20 69 66 28 20 69  ol2);..    if( i
359f2 43 6f 6c 31 3d 3d 69 43 6f 6c 32 20 29 7b 0a 20  Col1==iCol2 ){. 
359f3 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
359f4 36 34 20 69 31 20 3d 20 30 3b 0a 20 20 20 20 20  64 i1 = 0;.     
359f5 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
359f6 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  2 = 0;.      sql
359f7 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65 76  ite3_int64 iPrev
359f8 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
359f9 69 43 6f 6c 31 21 3d 30 20 29 7b 0a 20 20 20 20  iCol1!=0 ){.    
359fa 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
359fb 20 20 20 2a 70 2b 2b 20 3d 20 30 78 30 31 3b 0a     *p++ = 0x01;.
359fc 20 20 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69          n = sqli
359fd 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74  te3Fts3PutVarint
359fe 28 70 2c 20 69 43 6f 6c 31 29 3b 0a 20 20 20 20  (p, iCol1);.    
359ff 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20 20 20 20      p += n;.    
35a00 20 20 20 20 70 31 20 2b 3d 20 31 20 2b 20 6e 3b      p1 += 1 + n;
35a01 0a 20 20 20 20 20 20 20 20 70 32 20 2b 3d 20 31  .        p2 += 1
35a02 20 2b 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   + n;.      }.  
35a03 20 20 20 20 77 68 69 6c 65 28 20 28 2a 70 31 26      while( (*p1&
35a04 30 78 46 45 29 20 7c 7c 20 28 2a 70 32 26 30 78  0xFE) || (*p2&0x
35a05 46 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  FE) ){.        i
35a06 66 28 20 69 31 3d 3d 69 32 20 29 7b 0a 20 20 20  f( i1==i2 ){.   
35a07 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65         fts3GetDe
35a08 6c 74 61 56 61 72 69 6e 74 28 26 70 31 2c 20 26  ltaVarint(&p1, &
35a09 69 31 29 3b 20 69 31 20 2d 3d 20 32 3b 0a 20 20  i1); i1 -= 2;.  
35a0a 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35a0b 65 6c 74 61 56 61 72 69 6e 74 28 26 70 32 2c 20  eltaVarint(&p2, 
35a0c 26 69 32 29 3b 20 69 32 20 2d 3d 20 32 3b 0a 20  &i2); i2 -= 2;. 
35a0d 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
35a0e 20 69 31 3c 69 32 20 29 7b 0a 20 20 20 20 20 20   i1<i2 ){.      
35a0f 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61      fts3GetDelta
35a10 56 61 72 69 6e 74 28 26 70 31 2c 20 26 69 31 29  Varint(&p1, &i1)
35a11 3b 20 69 31 20 2d 3d 20 32 3b 0a 20 20 20 20 20  ; i1 -= 2;.     
35a12 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35a13 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61      fts3GetDelta
35a14 56 61 72 69 6e 74 28 26 70 32 2c 20 26 69 32 29  Varint(&p2, &i2)
35a15 3b 20 69 32 20 2d 3d 20 32 3b 0a 20 20 20 20 20  ; i2 -= 2;.     
35a16 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 74 73     }.        fts
35a17 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3PutDeltaVarint(
35a18 26 70 2c 20 26 69 50 72 65 76 2c 20 28 69 31 3c  &p, &iPrev, (i1<
35a19 69 32 20 3f 20 69 31 20 3a 20 69 32 29 20 2b 20  i2 ? i1 : i2) + 
35a1a 32 29 3b 20 69 50 72 65 76 20 2d 3d 20 32 3b 0a  2); iPrev -= 2;.
35a1b 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28          if( 0==(
35a1c 2a 70 31 26 30 78 46 45 29 20 29 20 69 31 20 3d  *p1&0xFE) ) i1 =
35a1d 20 30 78 37 46 46 46 46 46 46 46 3b 0a 20 20 20   0x7FFFFFFF;.   
35a1e 20 20 20 20 20 69 66 28 20 30 3d 3d 28 2a 70 32       if( 0==(*p2
35a1f 26 30 78 46 45 29 20 29 20 69 32 20 3d 20 30 78  &0xFE) ) i2 = 0x
35a20 37 46 46 46 46 46 46 46 3b 0a 20 20 20 20 20 20  7FFFFFFF;.      
35a21 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
35a22 69 43 6f 6c 31 3c 69 43 6f 6c 32 20 29 7b 0a 20  iCol1<iCol2 ){. 
35a23 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c       fts3Columnl
35a24 69 73 74 43 6f 70 79 28 26 70 2c 20 26 70 31 29  istCopy(&p, &p1)
35a25 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
35a26 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73     fts3Columnlis
35a27 74 43 6f 70 79 28 26 70 2c 20 26 70 32 29 3b 0a  tCopy(&p, &p2);.
35a28 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 2b      }.  }..  *p+
35a29 2b 20 3d 20 27 5c 30 27 3b 0a 20 20 2a 70 70 20  + = '\0';.  *pp 
35a2a 3d 20 70 3b 0a 20 20 2a 70 70 31 20 3d 20 70 31  = p;.  *pp1 = p1
35a2b 20 2b 20 31 3b 0a 20 20 2a 70 70 32 20 3d 20 70   + 1;.  *pp2 = p
35a2c 32 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2 + 1;.}../*.** 
35a2d 6e 54 6f 6b 65 6e 3d 3d 31 20 73 65 61 72 63 68  nToken==1 search
35a2e 65 73 20 66 6f 72 20 61 64 6a 61 63 65 6e 74 20  es for adjacent 
35a2f 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  positions..*/.st
35a30 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 6f 73  atic int fts3Pos
35a31 6c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  listPhraseMerge(
35a32 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20  .  char **pp,   
35a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a34 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
35a35 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f  fer */.  int nTo
35a36 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ken,            
35a37 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
35a38 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65 20 69  mum difference i
35a39 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e  n token position
35a3a 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76  s */.  int isSav
35a3b 65 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20  eLeft,          
35a3c 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 20 74         /* Save t
35a3d 68 65 20 6c 65 66 74 20 70 6f 73 69 74 69 6f 6e  he left position
35a3e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 31   */.  char **pp1
35a3f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35a40 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 69 6e        /* Left in
35a41 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 63 68  put list */.  ch
35a42 61 72 20 2a 2a 70 70 32 20 20 20 20 20 20 20 20  ar **pp2        
35a43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35a44 20 52 69 67 68 74 20 69 6e 70 75 74 20 6c 69 73   Right input lis
35a45 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  t */.){.  char *
35a46 70 20 3d 20 28 70 70 20 3f 20 2a 70 70 20 3a 20  p = (pp ? *pp : 
35a47 30 29 3b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d  0);.  char *p1 =
35a48 20 2a 70 70 31 3b 0a 20 20 63 68 61 72 20 2a 70   *pp1;.  char *p
35a49 32 20 3d 20 2a 70 70 32 3b 0a 0a 20 20 69 6e 74  2 = *pp2;..  int
35a4a 20 69 43 6f 6c 31 20 3d 20 30 3b 0a 20 20 69 6e   iCol1 = 0;.  in
35a4b 74 20 69 43 6f 6c 32 20 3d 20 30 3b 0a 20 20 61  t iCol2 = 0;.  a
35a4c 73 73 65 72 74 28 20 2a 70 31 21 3d 30 20 26 26  ssert( *p1!=0 &&
35a4d 20 2a 70 32 21 3d 30 20 29 3b 0a 20 20 69 66 28   *p2!=0 );.  if(
35a4e 20 2a 70 31 3d 3d 30 78 30 31 20 29 7b 20 0a 20   *p1==0x01 ){ . 
35a4f 20 20 20 70 31 2b 2b 3b 0a 20 20 20 20 70 31 20     p1++;.    p1 
35a50 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
35a51 74 56 61 72 69 6e 74 33 32 28 70 31 2c 20 26 69  tVarint32(p1, &i
35a52 43 6f 6c 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Col1);.  }.  if(
35a53 20 2a 70 32 3d 3d 30 78 30 31 20 29 7b 20 0a 20   *p2==0x01 ){ . 
35a54 20 20 20 70 32 2b 2b 3b 0a 20 20 20 20 70 32 20     p2++;.    p2 
35a55 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
35a56 74 56 61 72 69 6e 74 33 32 28 70 32 2c 20 26 69  tVarint32(p2, &i
35a57 43 6f 6c 32 29 3b 0a 20 20 7d 0a 0a 20 20 77 68  Col2);.  }..  wh
35a58 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69 66  ile( 1 ){.    if
35a59 28 20 69 43 6f 6c 31 3d 3d 69 43 6f 6c 32 20 29  ( iCol1==iCol2 )
35a5a 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 53  {.      char *pS
35a5b 61 76 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 73  ave = p;.      s
35a5c 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72  qlite3_int64 iPr
35a5d 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ev = 0;.      sq
35a5e 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73  lite3_int64 iPos
35a5f 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  1 = 0;.      sql
35a60 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73 32  ite3_int64 iPos2
35a61 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28   = 0;..      if(
35a62 20 70 70 20 26 26 20 69 43 6f 6c 31 20 29 7b 0a   pp && iCol1 ){.
35a63 20 20 20 20 20 20 20 20 2a 70 2b 2b 20 3d 20 30          *p++ = 0
35a64 78 30 31 3b 0a 20 20 20 20 20 20 20 20 70 20 2b  x01;.        p +
35a65 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
35a66 56 61 72 69 6e 74 28 70 2c 20 69 43 6f 6c 31 29  Varint(p, iCol1)
35a67 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
35a68 20 61 73 73 65 72 74 28 20 2a 70 31 21 3d 30 78   assert( *p1!=0x
35a69 30 30 20 26 26 20 2a 70 32 21 3d 30 78 30 30 20  00 && *p2!=0x00 
35a6a 26 26 20 2a 70 31 21 3d 30 78 30 31 20 26 26 20  && *p1!=0x01 && 
35a6b 2a 70 32 21 3d 30 78 30 31 20 29 3b 0a 20 20 20  *p2!=0x01 );.   
35a6c 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35a6d 61 72 69 6e 74 28 26 70 31 2c 20 26 69 50 6f 73  arint(&p1, &iPos
35a6e 31 29 3b 20 69 50 6f 73 31 20 2d 3d 20 32 3b 0a  1); iPos1 -= 2;.
35a6f 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c        fts3GetDel
35a70 74 61 56 61 72 69 6e 74 28 26 70 32 2c 20 26 69  taVarint(&p2, &i
35a71 50 6f 73 32 29 3b 20 69 50 6f 73 32 20 2d 3d 20  Pos2); iPos2 -= 
35a72 32 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28  2;..      while(
35a73 20 31 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66   1 ){.        if
35a74 28 20 69 50 6f 73 32 3e 69 50 6f 73 31 20 26 26  ( iPos2>iPos1 &&
35a75 20 69 50 6f 73 32 3c 3d 69 50 6f 73 31 2b 6e 54   iPos2<=iPos1+nT
35a76 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  oken ){.        
35a77 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
35a78 69 53 61 76 65 3b 0a 20 20 20 20 20 20 20 20 20  iSave;.         
35a79 20 69 66 28 20 21 70 70 20 29 7b 0a 20 20 20 20   if( !pp ){.    
35a7a 20 20 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c          fts3Posl
35a7b 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b  istCopy(0, &p2);
35a7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73  .            fts
35a7d 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20  3PoslistCopy(0, 
35a7e 26 70 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  &p1);.          
35a7f 20 20 2a 70 70 31 20 3d 20 70 31 3b 0a 20 20 20    *pp1 = p1;.   
35a80 20 20 20 20 20 20 20 20 20 2a 70 70 32 20 3d 20           *pp2 = 
35a81 70 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  p2;.            
35a82 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
35a83 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
35a84 69 53 61 76 65 20 3d 20 69 73 53 61 76 65 4c 65  iSave = isSaveLe
35a85 66 74 20 3f 20 69 50 6f 73 31 20 3a 20 69 50 6f  ft ? iPos1 : iPo
35a86 73 32 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74  s2;.          ft
35a87 73 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74  s3PutDeltaVarint
35a88 28 26 70 2c 20 26 69 50 72 65 76 2c 20 69 53 61  (&p, &iPrev, iSa
35a89 76 65 2b 32 29 3b 20 69 50 72 65 76 20 2d 3d 20  ve+2); iPrev -= 
35a8a 32 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 61  2;.          pSa
35a8b 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ve = 0;.        
35a8c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50  }.        if( iP
35a8d 6f 73 32 3c 3d 69 50 6f 73 31 20 29 7b 0a 20 20  os2<=iPos1 ){.  
35a8e 20 20 20 20 20 20 20 20 69 66 28 20 28 2a 70 32          if( (*p2
35a8f 26 30 78 46 45 29 3d 3d 30 20 29 20 62 72 65 61  &0xFE)==0 ) brea
35a90 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  k;.          fts
35a91 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3GetDeltaVarint(
35a92 26 70 32 2c 20 26 69 50 6f 73 32 29 3b 20 69 50  &p2, &iPos2); iP
35a93 6f 73 32 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20  os2 -= 2;.      
35a94 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
35a95 20 20 20 69 66 28 20 28 2a 70 31 26 30 78 46 45     if( (*p1&0xFE
35a96 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  )==0 ) break;.  
35a97 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35a98 65 6c 74 61 56 61 72 69 6e 74 28 26 70 31 2c 20  eltaVarint(&p1, 
35a99 26 69 50 6f 73 31 29 3b 20 69 50 6f 73 31 20 2d  &iPos1); iPos1 -
35a9a 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 2;.        }. 
35a9b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
35a9c 20 70 53 61 76 65 20 26 26 20 70 70 20 29 7b 0a   pSave && pp ){.
35a9d 20 20 20 20 20 20 20 20 70 20 3d 20 70 53 61 76          p = pSav
35a9e 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  e;.      }..    
35a9f 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73 74    fts3Columnlist
35aa0 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20 20  Copy(0, &p1);.  
35aa1 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69      fts3Columnli
35aa2 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b 0a  stCopy(0, &p2);.
35aa3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 2a        assert( (*
35aa4 70 31 26 30 78 46 45 29 3d 3d 30 20 26 26 20 28  p1&0xFE)==0 && (
35aa5 2a 70 32 26 30 78 46 45 29 3d 3d 30 20 29 3b 0a  *p2&0xFE)==0 );.
35aa6 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a 70 31        if( 0==*p1
35aa7 20 7c 7c 20 30 3d 3d 2a 70 32 20 29 20 62 72 65   || 0==*p2 ) bre
35aa8 61 6b 3b 0a 0a 20 20 20 20 20 20 70 31 2b 2b 3b  ak;..      p1++;
35aa9 0a 20 20 20 20 20 20 70 31 20 2b 3d 20 73 71 6c  .      p1 += sql
35aaa 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
35aab 74 33 32 28 70 31 2c 20 26 69 43 6f 6c 31 29 3b  t32(p1, &iCol1);
35aac 0a 20 20 20 20 20 20 70 32 2b 2b 3b 0a 20 20 20  .      p2++;.   
35aad 20 20 20 70 32 20 2b 3d 20 73 71 6c 69 74 65 33     p2 += sqlite3
35aae 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
35aaf 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20 20  p2, &iCol2);.   
35ab0 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e   }..    /* Advan
35ab1 63 65 20 70 6f 69 6e 74 65 72 20 70 31 20 6f 72  ce pointer p1 or
35ab2 20 70 32 20 28 77 68 69 63 68 65 76 65 72 20 63   p2 (whichever c
35ab3 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
35ab4 65 20 73 6d 61 6c 6c 65 72 20 6f 66 0a 20 20 20  e smaller of.   
35ab5 20 2a 2a 20 69 43 6f 6c 31 20 61 6e 64 20 69 43   ** iCol1 and iC
35ab6 6f 6c 32 29 20 73 6f 20 74 68 61 74 20 69 74 20  ol2) so that it 
35ab7 70 6f 69 6e 74 73 20 74 6f 20 65 69 74 68 65 72  points to either
35ab8 20 74 68 65 20 30 78 30 30 20 74 68 61 74 20 6d   the 0x00 that m
35ab9 61 72 6b 73 20 74 68 65 0a 20 20 20 20 2a 2a 20  arks the.    ** 
35aba 65 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 74  end of the posit
35abb 69 6f 6e 20 6c 69 73 74 2c 20 6f 72 20 74 68 65  ion list, or the
35abc 20 30 78 30 31 20 74 68 61 74 20 70 72 65 63 65   0x01 that prece
35abd 64 65 73 20 74 68 65 20 6e 65 78 74 20 0a 20 20  des the next .  
35abe 20 20 2a 2a 20 63 6f 6c 75 6d 6e 2d 6e 75 6d 62    ** column-numb
35abf 65 72 20 69 6e 20 74 68 65 20 70 6f 73 69 74 69  er in the positi
35ac0 6f 6e 20 6c 69 73 74 2e 20 0a 20 20 20 20 2a 2f  on list. .    */
35ac1 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 69 43  .    else if( iC
35ac2 6f 6c 31 3c 69 43 6f 6c 32 20 29 7b 0a 20 20 20  ol1<iCol2 ){.   
35ac3 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73     fts3Columnlis
35ac4 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20  tCopy(0, &p1);. 
35ac5 20 20 20 20 20 69 66 28 20 30 3d 3d 2a 70 31 20       if( 0==*p1 
35ac6 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70  ) break;.      p
35ac7 31 2b 2b 3b 0a 20 20 20 20 20 20 70 31 20 2b 3d  1++;.      p1 +=
35ac8 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
35ac9 61 72 69 6e 74 33 32 28 70 31 2c 20 26 69 43 6f  arint32(p1, &iCo
35aca 6c 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  l1);.    }else{.
35acb 20 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e        fts3Column
35acc 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29  listCopy(0, &p2)
35acd 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a  ;.      if( 0==*
35ace 70 32 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  p2 ) break;.    
35acf 20 20 70 32 2b 2b 3b 0a 20 20 20 20 20 20 70 32    p2++;.      p2
35ad0 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
35ad1 65 74 56 61 72 69 6e 74 33 32 28 70 32 2c 20 26  etVarint32(p2, &
35ad2 69 43 6f 6c 32 29 3b 0a 20 20 20 20 7d 0a 20 20  iCol2);.    }.  
35ad3 7d 0a 0a 20 20 66 74 73 33 50 6f 73 6c 69 73 74  }..  fts3Poslist
35ad4 43 6f 70 79 28 30 2c 20 26 70 32 29 3b 0a 20 20  Copy(0, &p2);.  
35ad5 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28  fts3PoslistCopy(
35ad6 30 2c 20 26 70 31 29 3b 0a 20 20 2a 70 70 31 20  0, &p1);.  *pp1 
35ad7 3d 20 70 31 3b 0a 20 20 2a 70 70 32 20 3d 20 70  = p1;.  *pp2 = p
35ad8 32 3b 0a 20 20 69 66 28 20 21 70 70 20 7c 7c 20  2;.  if( !pp || 
35ad9 2a 70 70 3d 3d 70 20 29 7b 0a 20 20 20 20 72 65  *pp==p ){.    re
35ada 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70  turn 0;.  }.  *p
35adb 2b 2b 20 3d 20 30 78 30 30 3b 0a 20 20 2a 70 70  ++ = 0x00;.  *pp
35adc 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 31   = p;.  return 1
35add 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
35ade 66 74 73 33 50 6f 73 6c 69 73 74 4e 65 61 72 4d  fts3PoslistNearM
35adf 65 72 67 65 28 0a 20 20 63 68 61 72 20 2a 2a 70  erge(.  char **p
35ae0 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
35ae1 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
35ae2 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68  t buffer */.  ch
35ae3 61 72 20 2a 61 54 6d 70 2c 20 20 20 20 20 20 20  ar *aTmp,       
35ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35ae5 20 54 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   Temporary buffe
35ae6 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74  r space */.  int
35ae7 20 6e 52 69 67 68 74 2c 20 20 20 20 20 20 20 20   nRight,        
35ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35ae9 4d 61 78 69 6d 75 6d 20 64 69 66 66 65 72 65 6e  Maximum differen
35aea 63 65 20 69 6e 20 74 6f 6b 65 6e 20 70 6f 73 69  ce in token posi
35aeb 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tions */.  int n
35aec 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20  Left,           
35aed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
35aee 78 69 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65  ximum difference
35aef 20 69 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69   in token positi
35af0 6f 6e 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ons */.  char **
35af1 70 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20  pp1,            
35af2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74           /* Left
35af3 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20   input list */. 
35af4 20 63 68 61 72 20 2a 2a 70 70 32 20 20 20 20 20   char **pp2     
35af5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35af6 20 2f 2a 20 52 69 67 68 74 20 69 6e 70 75 74 20   /* Right input 
35af7 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  list */.){.  cha
35af8 72 20 2a 70 31 20 3d 20 2a 70 70 31 3b 0a 20 20  r *p1 = *pp1;.  
35af9 63 68 61 72 20 2a 70 32 20 3d 20 2a 70 70 32 3b  char *p2 = *pp2;
35afa 0a 0a 20 20 69 66 28 20 21 70 70 20 29 7b 0a 20  ..  if( !pp ){. 
35afb 20 20 20 69 66 28 20 66 74 73 33 50 6f 73 6c 69     if( fts3Posli
35afc 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 30 2c  stPhraseMerge(0,
35afd 20 6e 52 69 67 68 74 2c 20 30 2c 20 70 70 31 2c   nRight, 0, pp1,
35afe 20 70 70 32 29 20 29 20 72 65 74 75 72 6e 20 31   pp2) ) return 1
35aff 3b 0a 20 20 20 20 2a 70 70 31 20 3d 20 70 31 3b  ;.    *pp1 = p1;
35b00 0a 20 20 20 20 2a 70 70 32 20 3d 20 70 32 3b 0a  .    *pp2 = p2;.
35b01 20 20 20 20 72 65 74 75 72 6e 20 66 74 73 33 50      return fts3P
35b02 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72 67  oslistPhraseMerg
35b03 65 28 30 2c 20 6e 4c 65 66 74 2c 20 30 2c 20 70  e(0, nLeft, 0, p
35b04 70 32 2c 20 70 70 31 29 3b 0a 20 20 7d 65 6c 73  p2, pp1);.  }els
35b05 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 54 6d  e{.    char *pTm
35b06 70 31 20 3d 20 61 54 6d 70 3b 0a 20 20 20 20 63  p1 = aTmp;.    c
35b07 68 61 72 20 2a 70 54 6d 70 32 3b 0a 20 20 20 20  har *pTmp2;.    
35b08 63 68 61 72 20 2a 61 54 6d 70 32 3b 0a 20 20 20  char *aTmp2;.   
35b09 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 0a 0a 20   int res = 1;.. 
35b0a 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68     fts3PoslistPh
35b0b 72 61 73 65 4d 65 72 67 65 28 26 70 54 6d 70 31  raseMerge(&pTmp1
35b0c 2c 20 6e 52 69 67 68 74 2c 20 30 2c 20 70 70 31  , nRight, 0, pp1
35b0d 2c 20 70 70 32 29 3b 0a 20 20 20 20 61 54 6d 70  , pp2);.    aTmp
35b0e 32 20 3d 20 70 54 6d 70 32 20 3d 20 70 54 6d 70  2 = pTmp2 = pTmp
35b0f 31 3b 0a 20 20 20 20 2a 70 70 31 20 3d 20 70 31  1;.    *pp1 = p1
35b10 3b 0a 20 20 20 20 2a 70 70 32 20 3d 20 70 32 3b  ;.    *pp2 = p2;
35b11 0a 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74  .    fts3Poslist
35b12 50 68 72 61 73 65 4d 65 72 67 65 28 26 70 54 6d  PhraseMerge(&pTm
35b13 70 32 2c 20 6e 4c 65 66 74 2c 20 31 2c 20 70 70  p2, nLeft, 1, pp
35b14 32 2c 20 70 70 31 29 3b 0a 20 20 20 20 69 66 28  2, pp1);.    if(
35b15 20 70 54 6d 70 31 21 3d 61 54 6d 70 20 26 26 20   pTmp1!=aTmp && 
35b16 70 54 6d 70 32 21 3d 61 54 6d 70 32 20 29 7b 0a  pTmp2!=aTmp2 ){.
35b17 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73        fts3Poslis
35b18 74 4d 65 72 67 65 28 70 70 2c 20 26 61 54 6d 70  tMerge(pp, &aTmp
35b19 2c 20 26 61 54 6d 70 32 29 3b 0a 20 20 20 20 7d  , &aTmp2);.    }
35b1a 65 6c 73 65 20 69 66 28 20 70 54 6d 70 31 21 3d  else if( pTmp1!=
35b1b 61 54 6d 70 20 29 7b 0a 20 20 20 20 20 20 66 74  aTmp ){.      ft
35b1c 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 70 70  s3PoslistCopy(pp
35b1d 2c 20 26 61 54 6d 70 29 3b 0a 20 20 20 20 7d 65  , &aTmp);.    }e
35b1e 6c 73 65 20 69 66 28 20 70 54 6d 70 32 21 3d 61  lse if( pTmp2!=a
35b1f 54 6d 70 32 20 29 7b 0a 20 20 20 20 20 20 66 74  Tmp2 ){.      ft
35b20 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 70 70  s3PoslistCopy(pp
35b21 2c 20 26 61 54 6d 70 32 29 3b 0a 20 20 20 20 7d  , &aTmp2);.    }
35b22 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 73 20  else{.      res 
35b23 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
35b24 72 65 74 75 72 6e 20 72 65 73 3b 0a 20 20 7d 0a  return res;.  }.
35b25 7d 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 73 20  }../*.** Values 
35b26 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64  that may be used
35b27 20 61 73 20 74 68 65 20 66 69 72 73 74 20 70 61   as the first pa
35b28 72 61 6d 65 74 65 72 20 74 6f 20 66 74 73 33 44  rameter to fts3D
35b29 6f 63 6c 69 73 74 4d 65 72 67 65 28 29 2e 0a 2a  oclistMerge()..*
35b2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f  /.#define MERGE_
35b2b 4e 4f 54 20 20 20 20 20 20 20 20 32 20 20 20 20  NOT        2    
35b2c 20 20 20 20 2f 2a 20 44 20 2b 20 44 20 2d 3e 20      /* D + D -> 
35b2d 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52  D */.#define MER
35b2e 47 45 5f 41 4e 44 20 20 20 20 20 20 20 20 33 20  GE_AND        3 
35b2f 20 20 20 20 20 20 20 2f 2a 20 44 20 2b 20 44 20         /* D + D 
35b30 2d 3e 20 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20  -> D */.#define 
35b31 4d 45 52 47 45 5f 4f 52 20 20 20 20 20 20 20 20  MERGE_OR        
35b32 20 34 20 20 20 20 20 20 20 20 2f 2a 20 44 20 2b   4        /* D +
35b33 20 44 20 2d 3e 20 44 20 2a 2f 0a 23 64 65 66 69   D -> D */.#defi
35b34 6e 65 20 4d 45 52 47 45 5f 50 4f 53 5f 4f 52 20  ne MERGE_POS_OR 
35b35 20 20 20 20 35 20 20 20 20 20 20 20 20 2f 2a 20      5        /* 
35b36 50 20 2b 20 50 20 2d 3e 20 50 20 2a 2f 0a 23 64  P + P -> P */.#d
35b37 65 66 69 6e 65 20 4d 45 52 47 45 5f 50 48 52 41  efine MERGE_PHRA
35b38 53 45 20 20 20 20 20 36 20 20 20 20 20 20 20 20  SE     6        
35b39 2f 2a 20 50 20 2b 20 50 20 2d 3e 20 44 20 2a 2f  /* P + P -> D */
35b3a 0a 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 50  .#define MERGE_P
35b3b 4f 53 5f 50 48 52 41 53 45 20 37 20 20 20 20 20  OS_PHRASE 7     
35b3c 20 20 20 2f 2a 20 50 20 2b 20 50 20 2d 3e 20 50     /* P + P -> P
35b3d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52 47   */.#define MERG
35b3e 45 5f 4e 45 41 52 20 20 20 20 20 20 20 38 20 20  E_NEAR       8  
35b3f 20 20 20 20 20 20 2f 2a 20 50 20 2b 20 50 20 2d        /* P + P -
35b40 3e 20 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  > D */.#define M
35b41 45 52 47 45 5f 50 4f 53 5f 4e 45 41 52 20 20 20  ERGE_POS_NEAR   
35b42 39 20 20 20 20 20 20 20 20 2f 2a 20 50 20 2b 20  9        /* P + 
35b43 50 20 2d 3e 20 50 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  P -> P */../*.**
35b44 20 4d 65 72 67 65 20 74 68 65 20 74 77 6f 20 64   Merge the two d
35b45 6f 63 6c 69 73 74 73 20 70 61 73 73 65 64 20 69  oclists passed i
35b46 6e 20 62 75 66 66 65 72 20 61 31 20 28 73 69 7a  n buffer a1 (siz
35b47 65 20 6e 31 20 62 79 74 65 73 29 20 61 6e 64 20  e n1 bytes) and 
35b48 61 32 0a 2a 2a 20 28 73 69 7a 65 20 6e 32 20 62  a2.** (size n2 b
35b49 79 74 65 73 29 2e 20 54 68 65 20 6f 75 74 70 75  ytes). The outpu
35b4a 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
35b4b 70 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75  pre-allocated bu
35b4c 66 66 65 72 20 61 42 75 66 66 65 72 2c 0a 2a 2a  ffer aBuffer,.**
35b4d 20 77 68 69 63 68 20 69 73 20 67 75 61 72 61 6e   which is guaran
35b4e 74 65 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65  teed to be large
35b4f 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
35b50 74 68 65 20 72 65 73 75 6c 74 73 2e 20 54 68 65  the results. The
35b51 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79   number.** of by
35b52 74 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 61  tes written to a
35b53 42 75 66 66 65 72 20 69 73 20 73 74 6f 72 65 64  Buffer is stored
35b54 20 69 6e 20 2a 70 6e 42 75 66 66 65 72 20 62 65   in *pnBuffer be
35b55 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
35b56 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
35b57 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ful, SQLITE_OK i
35b58 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
35b59 72 77 69 73 65 2c 20 69 66 20 61 20 6d 61 6c 6c  rwise, if a mall
35b5a 6f 63 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75  oc error.** occu
35b5b 72 73 20 77 68 69 6c 65 20 61 6c 6c 6f 63 61 74  rs while allocat
35b5c 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ing a temporary 
35b5d 62 75 66 66 65 72 20 61 73 20 70 61 72 74 20 6f  buffer as part o
35b5e 66 20 74 68 65 20 6d 65 72 67 65 20 6f 70 65 72  f the merge oper
35b5f 61 74 69 6f 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45  ation,.** SQLITE
35b60 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
35b61 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
35b62 74 20 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72  t fts3DoclistMer
35b63 67 65 28 0a 20 20 69 6e 74 20 6d 65 72 67 65 74  ge(.  int merget
35b64 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
35b65 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
35b66 74 68 65 20 4d 45 52 47 45 5f 58 58 58 20 63 6f  the MERGE_XXX co
35b67 6e 73 74 61 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  nstants */.  int
35b68 20 6e 50 61 72 61 6d 31 2c 20 20 20 20 20 20 20   nParam1,       
35b69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35b6a 55 73 65 64 20 62 79 20 4d 45 52 47 45 5f 4e 45  Used by MERGE_NE
35b6b 41 52 20 61 6e 64 20 4d 45 52 47 45 5f 50 4f 53  AR and MERGE_POS
35b6c 5f 4e 45 41 52 20 2a 2f 0a 20 20 69 6e 74 20 6e  _NEAR */.  int n
35b6d 50 61 72 61 6d 32 2c 20 20 20 20 20 20 20 20 20  Param2,         
35b6e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
35b6f 65 64 20 62 79 20 4d 45 52 47 45 5f 4e 45 41 52  ed by MERGE_NEAR
35b70 20 61 6e 64 20 4d 45 52 47 45 5f 50 4f 53 5f 4e   and MERGE_POS_N
35b71 45 41 52 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61  EAR */.  char *a
35b72 42 75 66 66 65 72 2c 20 20 20 20 20 20 20 20 20  Buffer,         
35b73 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d           /* Pre-
35b74 61 6c 6c 6f 63 61 74 65 64 20 6f 75 74 70 75 74  allocated output
35b75 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
35b76 20 2a 70 6e 42 75 66 66 65 72 2c 20 20 20 20 20   *pnBuffer,     
35b77 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35b78 4f 55 54 3a 20 42 79 74 65 73 20 77 72 69 74 74  OUT: Bytes writt
35b79 65 6e 20 74 6f 20 61 42 75 66 66 65 72 20 2a 2f  en to aBuffer */
35b7a 0a 20 20 63 68 61 72 20 2a 61 31 2c 20 20 20 20  .  char *a1,    
35b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b7c 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e     /* Buffer con
35b7d 74 61 69 6e 69 6e 67 20 66 69 72 73 74 20 64 6f  taining first do
35b7e 63 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  clist */.  int n
35b7f 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
35b80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
35b81 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 31 20  ze of buffer a1 
35b82 2a 2f 0a 20 20 63 68 61 72 20 2a 61 32 2c 20 20  */.  char *a2,  
35b83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b84 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63       /* Buffer c
35b85 6f 6e 74 61 69 6e 69 6e 67 20 73 65 63 6f 6e 64  ontaining second
35b86 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 69 6e   doclist */.  in
35b87 74 20 6e 32 20 20 20 20 20 20 20 20 20 20 20 20  t n2            
35b88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35b89 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
35b8a 61 32 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  a2 */.){.  sqlit
35b8b 65 33 5f 69 6e 74 36 34 20 69 31 20 3d 20 30 3b  e3_int64 i1 = 0;
35b8c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
35b8d 20 69 32 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   i2 = 0;.  sqlit
35b8e 65 33 5f 69 6e 74 36 34 20 69 50 72 65 76 20 3d  e3_int64 iPrev =
35b8f 20 30 3b 0a 0a 20 20 63 68 61 72 20 2a 70 20 3d   0;..  char *p =
35b90 20 61 42 75 66 66 65 72 3b 0a 20 20 63 68 61 72   aBuffer;.  char
35b91 20 2a 70 31 20 3d 20 61 31 3b 0a 20 20 63 68 61   *p1 = a1;.  cha
35b92 72 20 2a 70 32 20 3d 20 61 32 3b 0a 20 20 63 68  r *p2 = a2;.  ch
35b93 61 72 20 2a 70 45 6e 64 31 20 3d 20 26 61 31 5b  ar *pEnd1 = &a1[
35b94 6e 31 5d 3b 0a 20 20 63 68 61 72 20 2a 70 45 6e  n1];.  char *pEn
35b95 64 32 20 3d 20 26 61 32 5b 6e 32 5d 3b 0a 0a 20  d2 = &a2[n2];.. 
35b96 20 61 73 73 65 72 74 28 20 6d 65 72 67 65 74 79   assert( mergety
35b97 70 65 3d 3d 4d 45 52 47 45 5f 4f 52 20 20 20 20  pe==MERGE_OR    
35b98 20 7c 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d   || mergetype==M
35b99 45 52 47 45 5f 50 4f 53 5f 4f 52 20 0a 20 20 20  ERGE_POS_OR .   
35b9a 20 20 20 20 7c 7c 20 6d 65 72 67 65 74 79 70 65      || mergetype
35b9b 3d 3d 4d 45 52 47 45 5f 41 4e 44 20 20 20 20 7c  ==MERGE_AND    |
35b9c 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52  | mergetype==MER
35b9d 47 45 5f 4e 4f 54 0a 20 20 20 20 20 20 20 7c 7c  GE_NOT.       ||
35b9e 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47   mergetype==MERG
35b9f 45 5f 50 48 52 41 53 45 20 7c 7c 20 6d 65 72 67  E_PHRASE || merg
35ba0 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f 53  etype==MERGE_POS
35ba1 5f 50 48 52 41 53 45 0a 20 20 20 20 20 20 20 7c  _PHRASE.       |
35ba2 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52  | mergetype==MER
35ba3 47 45 5f 4e 45 41 52 20 20 20 7c 7c 20 6d 65 72  GE_NEAR   || mer
35ba4 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50 4f  getype==MERGE_PO
35ba5 53 5f 4e 45 41 52 0a 20 20 29 3b 0a 0a 20 20 69  S_NEAR.  );..  i
35ba6 66 28 20 21 61 42 75 66 66 65 72 20 29 7b 0a 20  f( !aBuffer ){. 
35ba7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
35ba8 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f  _NOMEM;.  }..  /
35ba9 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74  * Read the first
35baa 20 64 6f 63 69 64 20 66 72 6f 6d 20 65 61 63 68   docid from each
35bab 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 66 74   doclist */.  ft
35bac 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
35bad 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26 69  2(&p1, pEnd1, &i
35bae 31 29 3b 0a 20 20 66 74 73 33 47 65 74 44 65 6c  1);.  fts3GetDel
35baf 74 61 56 61 72 69 6e 74 32 28 26 70 32 2c 20 70  taVarint2(&p2, p
35bb0 45 6e 64 32 2c 20 26 69 32 29 3b 0a 0a 20 20 73  End2, &i2);..  s
35bb1 77 69 74 63 68 28 20 6d 65 72 67 65 74 79 70 65  witch( mergetype
35bb2 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4d 45 52   ){.    case MER
35bb3 47 45 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20  GE_OR:.    case 
35bb4 4d 45 52 47 45 5f 50 4f 53 5f 4f 52 3a 0a 20 20  MERGE_POS_OR:.  
35bb5 20 20 20 20 77 68 69 6c 65 28 20 70 31 20 7c 7c      while( p1 ||
35bb6 20 70 32 20 29 7b 0a 20 20 20 20 20 20 20 20 69   p2 ){.        i
35bb7 66 28 20 70 32 20 26 26 20 70 31 20 26 26 20 69  f( p2 && p1 && i
35bb8 31 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20 20 20  1==i2 ){.       
35bb9 20 20 20 66 74 73 33 50 75 74 44 65 6c 74 61 56     fts3PutDeltaV
35bba 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65 76  arint(&p, &iPrev
35bbb 2c 20 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20  , i1);.         
35bbc 20 69 66 28 20 6d 65 72 67 65 74 79 70 65 3d 3d   if( mergetype==
35bbd 4d 45 52 47 45 5f 50 4f 53 5f 4f 52 20 29 20 66  MERGE_POS_OR ) f
35bbe 74 73 33 50 6f 73 6c 69 73 74 4d 65 72 67 65 28  ts3PoslistMerge(
35bbf 26 70 2c 20 26 70 31 2c 20 26 70 32 29 3b 0a 20  &p, &p1, &p2);. 
35bc0 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74           fts3Get
35bc1 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31  DeltaVarint2(&p1
35bc2 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20  , pEnd1, &i1);. 
35bc3 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74           fts3Get
35bc4 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 32  DeltaVarint2(&p2
35bc5 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a 20  , pEnd2, &i2);. 
35bc6 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
35bc7 20 21 70 32 20 7c 7c 20 28 70 31 20 26 26 20 69   !p2 || (p1 && i
35bc8 31 3c 69 32 29 20 29 7b 0a 20 20 20 20 20 20 20  1<i2) ){.       
35bc9 20 20 20 66 74 73 33 50 75 74 44 65 6c 74 61 56     fts3PutDeltaV
35bca 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65 76  arint(&p, &iPrev
35bcb 2c 20 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20  , i1);.         
35bcc 20 69 66 28 20 6d 65 72 67 65 74 79 70 65 3d 3d   if( mergetype==
35bcd 4d 45 52 47 45 5f 50 4f 53 5f 4f 52 20 29 20 66  MERGE_POS_OR ) f
35bce 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 26  ts3PoslistCopy(&
35bcf 70 2c 20 26 70 31 29 3b 0a 20 20 20 20 20 20 20  p, &p1);.       
35bd0 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35bd1 61 72 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64  arint2(&p1, pEnd
35bd2 31 2c 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20  1, &i1);.       
35bd3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
35bd4 20 20 66 74 73 33 50 75 74 44 65 6c 74 61 56 61    fts3PutDeltaVa
35bd5 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65 76 2c  rint(&p, &iPrev,
35bd6 20 69 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20   i2);.          
35bd7 69 66 28 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d  if( mergetype==M
35bd8 45 52 47 45 5f 50 4f 53 5f 4f 52 20 29 20 66 74  ERGE_POS_OR ) ft
35bd9 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 26 70  s3PoslistCopy(&p
35bda 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20 20  , &p2);.        
35bdb 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
35bdc 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32  rint2(&p2, pEnd2
35bdd 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20  , &i2);.        
35bde 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
35bdf 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65  break;..    case
35be0 20 4d 45 52 47 45 5f 41 4e 44 3a 0a 20 20 20 20   MERGE_AND:.    
35be1 20 20 77 68 69 6c 65 28 20 70 31 20 26 26 20 70    while( p1 && p
35be2 32 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  2 ){.        if(
35be3 20 69 31 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20   i1==i2 ){.     
35be4 20 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74       fts3PutDelt
35be5 61 56 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72  aVarint(&p, &iPr
35be6 65 76 2c 20 69 31 29 3b 0a 20 20 20 20 20 20 20  ev, i1);.       
35be7 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35be8 61 72 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64  arint2(&p1, pEnd
35be9 31 2c 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20  1, &i1);.       
35bea 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35beb 61 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64  arint2(&p2, pEnd
35bec 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20  2, &i2);.       
35bed 20 7d 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32   }else if( i1<i2
35bee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74   ){.          ft
35bef 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
35bf0 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26 69  2(&p1, pEnd1, &i
35bf1 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  1);.        }els
35bf2 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  e{.          fts
35bf3 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35bf4 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32  (&p2, pEnd2, &i2
35bf5 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
35bf6 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
35bf7 3b 0a 0a 20 20 20 20 63 61 73 65 20 4d 45 52 47  ;..    case MERG
35bf8 45 5f 4e 4f 54 3a 0a 20 20 20 20 20 20 77 68 69  E_NOT:.      whi
35bf9 6c 65 28 20 70 31 20 29 7b 0a 20 20 20 20 20 20  le( p1 ){.      
35bfa 20 20 69 66 28 20 70 32 20 26 26 20 69 31 3d 3d    if( p2 && i1==
35bfb 69 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i2 ){.          
35bfc 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
35bfd 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20  nt2(&p1, pEnd1, 
35bfe 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  &i1);.          
35bff 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
35c00 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c 20  nt2(&p2, pEnd2, 
35c01 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  &i2);.        }e
35c02 6c 73 65 20 69 66 28 20 21 70 32 20 7c 7c 20 69  lse if( !p2 || i
35c03 31 3c 69 32 20 29 7b 0a 20 20 20 20 20 20 20 20  1<i2 ){.        
35c04 20 20 66 74 73 33 50 75 74 44 65 6c 74 61 56 61    fts3PutDeltaVa
35c05 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65 76 2c  rint(&p, &iPrev,
35c06 20 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20   i1);.          
35c07 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
35c08 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20  nt2(&p1, pEnd1, 
35c09 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  &i1);.        }e
35c0a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66  lse{.          f
35c0b 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e  ts3GetDeltaVarin
35c0c 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26  t2(&p2, pEnd2, &
35c0d 69 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  i2);.        }. 
35c0e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
35c0f 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 4d 45  ak;..    case ME
35c10 52 47 45 5f 50 4f 53 5f 50 48 52 41 53 45 3a 0a  RGE_POS_PHRASE:.
35c11 20 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 50      case MERGE_P
35c12 48 52 41 53 45 3a 20 7b 0a 20 20 20 20 20 20 63  HRASE: {.      c
35c13 68 61 72 20 2a 2a 70 70 50 6f 73 20 3d 20 28 6d  har **ppPos = (m
35c14 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f  ergetype==MERGE_
35c15 50 48 52 41 53 45 20 3f 20 30 20 3a 20 26 70 29  PHRASE ? 0 : &p)
35c16 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70  ;.      while( p
35c17 31 20 26 26 20 70 32 20 29 7b 0a 20 20 20 20 20  1 && p2 ){.     
35c18 20 20 20 69 66 28 20 69 31 3d 3d 69 32 20 29 7b     if( i1==i2 ){
35c19 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
35c1a 2a 70 53 61 76 65 20 3d 20 70 3b 0a 20 20 20 20  *pSave = p;.    
35c1b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
35c1c 74 36 34 20 69 50 72 65 76 53 61 76 65 20 3d 20  t64 iPrevSave = 
35c1d 69 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 20  iPrev;.         
35c1e 20 66 74 73 33 50 75 74 44 65 6c 74 61 56 61 72   fts3PutDeltaVar
35c1f 69 6e 74 28 26 70 2c 20 26 69 50 72 65 76 2c 20  int(&p, &iPrev, 
35c20 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  i1);.          i
35c21 66 28 20 30 3d 3d 66 74 73 33 50 6f 73 6c 69 73  f( 0==fts3Poslis
35c22 74 50 68 72 61 73 65 4d 65 72 67 65 28 70 70 50  tPhraseMerge(ppP
35c23 6f 73 2c 20 31 2c 20 30 2c 20 26 70 31 2c 20 26  os, 1, 0, &p1, &
35c24 70 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  p2) ){.         
35c25 20 20 20 70 20 3d 20 70 53 61 76 65 3b 0a 20 20     p = pSave;.  
35c26 20 20 20 20 20 20 20 20 20 20 69 50 72 65 76 20            iPrev 
35c27 3d 20 69 50 72 65 76 53 61 76 65 3b 0a 20 20 20  = iPrevSave;.   
35c28 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35c29 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35c2a 61 72 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64  arint2(&p1, pEnd
35c2b 31 2c 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20  1, &i1);.       
35c2c 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35c2d 61 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64  arint2(&p2, pEnd
35c2e 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20  2, &i2);.       
35c2f 20 7d 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32   }else if( i1<i2
35c30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74   ){.          ft
35c31 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c  s3PoslistCopy(0,
35c32 20 26 70 31 29 3b 0a 20 20 20 20 20 20 20 20 20   &p1);.         
35c33 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35c34 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c  int2(&p1, pEnd1,
35c35 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 7d   &i1);.        }
35c36 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
35c37 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28  fts3PoslistCopy(
35c38 30 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20  0, &p2);.       
35c39 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35c3a 61 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64  arint2(&p2, pEnd
35c3b 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20  2, &i2);.       
35c3c 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
35c3d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
35c3e 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 50 4f     case MERGE_PO
35c3f 53 5f 4e 45 41 52 3a 0a 20 20 20 20 63 61 73 65  S_NEAR:.    case
35c40 20 4d 45 52 47 45 5f 4e 45 41 52 3a 20 7b 0a 20   MERGE_NEAR: {. 
35c41 20 20 20 20 20 63 68 61 72 20 2a 61 54 6d 70 20       char *aTmp 
35c42 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20  = 0;.      char 
35c43 2a 2a 70 70 50 6f 73 20 3d 20 30 3b 0a 20 20 20  **ppPos = 0;.   
35c44 20 20 20 69 66 28 20 6d 65 72 67 65 74 79 70 65     if( mergetype
35c45 3d 3d 4d 45 52 47 45 5f 50 4f 53 5f 4e 45 41 52  ==MERGE_POS_NEAR
35c46 20 29 7b 0a 20 20 20 20 20 20 20 20 70 70 50 6f   ){.        ppPo
35c47 73 20 3d 20 26 70 3b 0a 20 20 20 20 20 20 20 20  s = &p;.        
35c48 61 54 6d 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d  aTmp = sqlite3_m
35c49 61 6c 6c 6f 63 28 32 2a 28 6e 31 2b 6e 32 29 29  alloc(2*(n1+n2))
35c4a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 61  ;.        if( !a
35c4b 54 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Tmp ){.         
35c4c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
35c4d 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  OMEM;.        }.
35c4e 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 77        }..      w
35c4f 68 69 6c 65 28 20 70 31 20 26 26 20 70 32 20 29  hile( p1 && p2 )
35c50 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 31  {.        if( i1
35c51 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20 20 20 20  ==i2 ){.        
35c52 20 20 63 68 61 72 20 2a 70 53 61 76 65 20 3d 20    char *pSave = 
35c53 70 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  p;.          sql
35c54 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65 76  ite3_int64 iPrev
35c55 53 61 76 65 20 3d 20 69 50 72 65 76 3b 0a 20 20  Save = iPrev;.  
35c56 20 20 20 20 20 20 20 20 66 74 73 33 50 75 74 44          fts3PutD
35c57 65 6c 74 61 56 61 72 69 6e 74 28 26 70 2c 20 26  eltaVarint(&p, &
35c58 69 50 72 65 76 2c 20 69 31 29 3b 0a 0a 20 20 20  iPrev, i1);..   
35c59 20 20 20 20 20 20 20 69 66 28 20 21 66 74 73 33         if( !fts3
35c5a 50 6f 73 6c 69 73 74 4e 65 61 72 4d 65 72 67 65  PoslistNearMerge
35c5b 28 70 70 50 6f 73 2c 20 61 54 6d 70 2c 20 6e 50  (ppPos, aTmp, nP
35c5c 61 72 61 6d 31 2c 20 6e 50 61 72 61 6d 32 2c 20  aram1, nParam2, 
35c5d 26 70 31 2c 20 26 70 32 29 20 29 7b 0a 20 20 20  &p1, &p2) ){.   
35c5e 20 20 20 20 20 20 20 20 20 69 50 72 65 76 20 3d           iPrev =
35c5f 20 69 50 72 65 76 53 61 76 65 3b 0a 20 20 20 20   iPrevSave;.    
35c60 20 20 20 20 20 20 20 20 70 20 3d 20 70 53 61 76          p = pSav
35c61 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  e;.          }..
35c62 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35c63 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35c64 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a  1, pEnd1, &i1);.
35c65 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35c66 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35c67 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a  2, pEnd2, &i2);.
35c68 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
35c69 28 20 69 31 3c 69 32 20 29 7b 0a 20 20 20 20 20  ( i1<i2 ){.     
35c6a 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74       fts3Poslist
35c6b 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20 20  Copy(0, &p1);.  
35c6c 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35c6d 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c  eltaVarint2(&p1,
35c6e 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20   pEnd1, &i1);.  
35c6f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
35c70 20 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69         fts3Posli
35c71 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b 0a  stCopy(0, &p2);.
35c72 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35c73 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35c74 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a  2, pEnd2, &i2);.
35c75 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35c76 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
35c77 66 72 65 65 28 61 54 6d 70 29 3b 0a 20 20 20 20  free(aTmp);.    
35c78 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
35c79 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
35c7a 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
35c7b 6c 69 64 20 6d 65 72 67 65 74 79 70 65 20 76 61  lid mergetype va
35c7c 6c 75 65 20 70 61 73 73 65 64 20 74 6f 20 66 74  lue passed to ft
35c7d 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 29  s3DoclistMerge()
35c7e 22 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 6e 42 75  ");.  }..  *pnBu
35c7f 66 66 65 72 20 3d 20 28 69 6e 74 29 28 70 2d 61  ffer = (int)(p-a
35c80 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74 75 72  Buffer);.  retur
35c81 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
35c82 2f 2a 20 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  /* .** A pointer
35c83 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
35c84 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
35c85 65 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  e is used as the
35c86 20 63 6f 6e 74 65 78 74 20 0a 2a 2a 20 61 72 67   context .** arg
35c87 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
35c88 46 74 73 33 53 65 67 52 65 61 64 65 72 49 74 65  Fts3SegReaderIte
35c89 72 61 74 65 28 29 0a 2a 2f 0a 74 79 70 65 64 65  rate().*/.typede
35c8a 66 20 73 74 72 75 63 74 20 54 65 72 6d 53 65 6c  f struct TermSel
35c8b 65 63 74 20 54 65 72 6d 53 65 6c 65 63 74 3b 0a  ect TermSelect;.
35c8c 73 74 72 75 63 74 20 54 65 72 6d 53 65 6c 65 63  struct TermSelec
35c8d 74 20 7b 0a 20 20 69 6e 74 20 69 73 52 65 71 50  t {.  int isReqP
35c8e 6f 73 3b 0a 20 20 63 68 61 72 20 2a 61 4f 75 74  os;.  char *aOut
35c8f 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  put;            
35c90 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27        /* Malloc'
35c91 64 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  d output buffer 
35c92 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 70 75 74  */.  int nOutput
35c93 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35c94 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
35c95 6f 75 74 70 75 74 20 69 6e 20 62 79 74 65 73 20  output in bytes 
35c96 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  */.};../*.** Thi
35c97 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
35c98 65 64 20 61 73 20 74 68 65 20 73 71 6c 69 74 65  ed as the sqlite
35c99 33 46 74 73 33 53 65 67 52 65 61 64 65 72 49 74  3Fts3SegReaderIt
35c9a 65 72 61 74 65 28 29 20 63 61 6c 6c 62 61 63 6b  erate() callback
35c9b 20 77 68 65 6e 0a 2a 2a 20 71 75 65 72 79 69 6e   when.** queryin
35c9c 67 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  g the full-text 
35c9d 69 6e 64 65 78 20 66 6f 72 20 61 20 64 6f 63 6c  index for a docl
35c9e 69 73 74 20 61 73 73 6f 63 69 61 74 65 64 20 77  ist associated w
35c9f 69 74 68 20 61 20 74 65 72 6d 20 6f 72 0a 2a 2a  ith a term or.**
35ca0 20 74 65 72 6d 2d 70 72 65 66 69 78 2e 0a 2a 2f   term-prefix..*/
35ca1 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
35ca2 54 65 72 6d 53 65 6c 65 63 74 43 62 28 0a 20 20  TermSelectCb(.  
35ca3 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
35ca4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ca5 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
35ca6 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 76 6f 69   object */.  voi
35ca7 64 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20  d *pContext,    
35ca8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35ca9 50 6f 69 6e 74 65 72 20 74 6f 20 54 65 72 6d 53  Pointer to TermS
35caa 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
35cab 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 72 6d  */.  char *zTerm
35cac 2c 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20  ,.  int nTerm,. 
35cad 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 2c   char *aDoclist,
35cae 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 0a  .  int nDoclist.
35caf 29 7b 0a 20 20 54 65 72 6d 53 65 6c 65 63 74 20  ){.  TermSelect 
35cb0 2a 70 54 53 20 3d 20 28 54 65 72 6d 53 65 6c 65  *pTS = (TermSele
35cb1 63 74 20 2a 29 70 43 6f 6e 74 65 78 74 3b 0a 20  ct *)pContext;. 
35cb2 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70 54 53 2d   int nNew = pTS-
35cb3 3e 6e 4f 75 74 70 75 74 20 2b 20 6e 44 6f 63 6c  >nOutput + nDocl
35cb4 69 73 74 3b 0a 20 20 63 68 61 72 20 2a 61 4e 65  ist;.  char *aNe
35cb5 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  w = sqlite3_mall
35cb6 6f 63 28 6e 4e 65 77 29 3b 0a 0a 20 20 55 4e 55  oc(nNew);..  UNU
35cb7 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29  SED_PARAMETER(p)
35cb8 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
35cb9 45 54 45 52 28 7a 54 65 72 6d 29 3b 0a 20 20 55  ETER(zTerm);.  U
35cba 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
35cbb 6e 54 65 72 6d 29 3b 0a 0a 20 20 69 66 28 20 21  nTerm);..  if( !
35cbc 61 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75  aNew ){.    retu
35cbd 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
35cbe 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 54 53 2d  .  }..  if( pTS-
35cbf 3e 6e 4f 75 74 70 75 74 3d 3d 30 20 29 7b 0a 20  >nOutput==0 ){. 
35cc0 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
35cc1 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20   the first term 
35cc2 73 65 6c 65 63 74 65 64 2c 20 63 6f 70 79 20 74  selected, copy t
35cc3 68 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 74 68  he doclist to th
35cc4 65 20 6f 75 74 70 75 74 0a 20 20 20 20 2a 2a 20  e output.    ** 
35cc5 62 75 66 66 65 72 20 75 73 69 6e 67 20 6d 65 6d  buffer using mem
35cc6 63 70 79 28 29 2e 20 54 4f 44 4f 3a 20 41 64 64  cpy(). TODO: Add
35cc7 20 61 20 77 61 79 20 74 6f 20 74 72 61 6e 73 66   a way to transf
35cc8 65 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68  er control of th
35cc9 65 0a 20 20 20 20 2a 2a 20 61 44 6f 63 6c 69 73  e.    ** aDoclis
35cca 74 20 62 75 66 66 65 72 20 66 72 6f 6d 20 74 68  t buffer from th
35ccb 65 20 63 61 6c 6c 65 72 20 73 6f 20 61 73 20 74  e caller so as t
35ccc 6f 20 61 76 6f 69 64 20 74 68 65 20 6d 65 6d 63  o avoid the memc
35ccd 70 79 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  py()..    */.   
35cce 20 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 61 44   memcpy(aNew, aD
35ccf 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69 73 74  oclist, nDoclist
35cd0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
35cd1 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 62 75  /* The output bu
35cd2 66 66 65 72 20 69 73 20 6e 6f 74 20 65 6d 70 74  ffer is not empt
35cd3 79 2e 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74  y. Merge doclist
35cd4 20 61 44 6f 63 6c 69 73 74 20 77 69 74 68 20 74   aDoclist with t
35cd5 68 65 0a 20 20 20 20 2a 2a 20 65 78 69 73 74 69  he.    ** existi
35cd6 6e 67 20 6f 75 74 70 75 74 2e 20 54 68 69 73 20  ng output. This 
35cd7 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
35cd8 77 69 74 68 20 70 72 65 66 69 78 2d 73 65 61 72  with prefix-sear
35cd9 63 68 65 73 20 28 61 73 0a 20 20 20 20 2a 2a 20  ches (as.    ** 
35cda 73 65 61 72 63 68 65 73 20 66 6f 72 20 65 78 61  searches for exa
35cdb 63 74 20 74 65 72 6d 73 20 72 65 74 75 72 6e 20  ct terms return 
35cdc 65 78 61 63 74 6c 79 20 6f 6e 65 20 64 6f 63 6c  exactly one docl
35cdd 69 73 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ist)..    */.   
35cde 20 69 6e 74 20 6d 65 72 67 65 74 79 70 65 20 3d   int mergetype =
35cdf 20 28 70 54 53 2d 3e 69 73 52 65 71 50 6f 73 20   (pTS->isReqPos 
35ce0 3f 20 4d 45 52 47 45 5f 50 4f 53 5f 4f 52 20 3a  ? MERGE_POS_OR :
35ce1 20 4d 45 52 47 45 5f 4f 52 29 3b 0a 20 20 20 20   MERGE_OR);.    
35ce2 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65  fts3DoclistMerge
35ce3 28 6d 65 72 67 65 74 79 70 65 2c 20 30 2c 20 30  (mergetype, 0, 0
35ce4 2c 0a 20 20 20 20 20 20 20 20 61 4e 65 77 2c 20  ,.        aNew, 
35ce5 26 6e 4e 65 77 2c 20 70 54 53 2d 3e 61 4f 75 74  &nNew, pTS->aOut
35ce6 70 75 74 2c 20 70 54 53 2d 3e 6e 4f 75 74 70 75  put, pTS->nOutpu
35ce7 74 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f  t, aDoclist, nDo
35ce8 63 6c 69 73 74 0a 20 20 20 20 29 3b 0a 20 20 7d  clist.    );.  }
35ce9 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
35cea 28 70 54 53 2d 3e 61 4f 75 74 70 75 74 29 3b 0a  (pTS->aOutput);.
35ceb 20 20 70 54 53 2d 3e 61 4f 75 74 70 75 74 20 3d    pTS->aOutput =
35cec 20 61 4e 65 77 3b 0a 20 20 70 54 53 2d 3e 6e 4f   aNew;.  pTS->nO
35ced 75 74 70 75 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20  utput = nNew;.. 
35cee 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35cef 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
35cf0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 72 65 69   function retrei
35cf1 76 65 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20  ves the doclist 
35cf2 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
35cf3 64 20 74 65 72 6d 20 28 6f 72 20 74 65 72 6d 0a  d term (or term.
35cf4 2a 2a 20 70 72 65 66 69 78 29 20 66 72 6f 6d 20  ** prefix) from 
35cf5 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 0a 2a  the database. .*
35cf6 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
35cf7 64 20 64 6f 63 6c 69 73 74 20 6d 61 79 20 62 65  d doclist may be
35cf8 20 69 6e 20 6f 6e 65 20 6f 66 20 74 77 6f 20 66   in one of two f
35cf9 6f 72 6d 61 74 73 2c 20 64 65 70 65 6e 64 69 6e  ormats, dependin
35cfa 67 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 76 61 6c  g on the .** val
35cfb 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20  ue of parameter 
35cfc 69 73 52 65 71 50 6f 73 2e 20 49 66 20 69 73 52  isReqPos. If isR
35cfd 65 71 50 6f 73 20 69 73 20 7a 65 72 6f 2c 20 74  eqPos is zero, t
35cfe 68 65 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20  hen the doclist 
35cff 69 73 0a 2a 2a 20 61 20 73 6f 72 74 65 64 20 6c  is.** a sorted l
35d00 69 73 74 20 6f 66 20 64 65 6c 74 61 2d 63 6f 6d  ist of delta-com
35d01 70 72 65 73 73 65 64 20 64 6f 63 69 64 73 2e 20  pressed docids. 
35d02 49 66 20 69 73 52 65 71 50 6f 73 20 69 73 20 6e  If isReqPos is n
35d03 6f 6e 2d 7a 65 72 6f 2c 20 0a 2a 2a 20 74 68 65  on-zero, .** the
35d04 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6c  n the returned l
35d05 69 73 74 20 69 73 20 69 6e 20 74 68 65 20 73 61  ist is in the sa
35d06 6d 65 20 66 6f 72 6d 61 74 20 61 73 20 69 73 20  me format as is 
35d07 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a  stored in the.**
35d08 20 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75   database withou
35d09 74 20 74 68 65 20 66 6f 75 6e 64 20 6c 65 6e 67  t the found leng
35d0a 74 68 20 73 70 65 63 69 66 69 65 72 20 61 74 20  th specifier at 
35d0b 74 68 65 20 73 74 61 72 74 20 6f 66 20 6f 6e 2d  the start of on-
35d0c 64 69 73 6b 0a 2a 2a 20 64 6f 63 6c 69 73 74 73  disk.** doclists
35d0d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35d0e 66 74 73 33 54 65 72 6d 53 65 6c 65 63 74 28 0a  fts3TermSelect(.
35d0f 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
35d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d11 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
35d12 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  le handle */.  i
35d13 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
35d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35d15 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72  * Column to quer
35d16 79 20 28 6f 72 20 2d 76 65 20 66 6f 72 20 61 6c  y (or -ve for al
35d17 6c 20 63 6f 6c 75 6d 6e 73 29 20 2a 2f 0a 20 20  l columns) */.  
35d18 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72  const char *zTer
35d19 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
35d1a 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79  /* Term to query
35d1b 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54   for */.  int nT
35d1c 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
35d1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
35d1e 65 20 6f 66 20 7a 54 65 72 6d 20 69 6e 20 62 79  e of zTerm in by
35d1f 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50  tes */.  int isP
35d20 72 65 66 69 78 2c 20 20 20 20 20 20 20 20 20 20  refix,          
35d21 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
35d22 20 66 6f 72 20 61 20 70 72 65 66 69 78 20 73 65   for a prefix se
35d23 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 73  arch */.  int is
35d24 52 65 71 50 6f 73 2c 20 20 20 20 20 20 20 20 20  ReqPos,         
35d25 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
35d26 65 20 74 6f 20 69 6e 63 6c 75 64 65 20 70 6f 73  e to include pos
35d27 69 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e 20 6f  ition lists in o
35d28 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 2a  utput */.  int *
35d29 70 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20  pnOut,          
35d2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
35d2b 54 3a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65  T: Size of buffe
35d2c 72 20 61 74 20 2a 70 70 4f 75 74 20 2a 2f 0a 20  r at *ppOut */. 
35d2d 20 63 68 61 72 20 2a 2a 70 70 4f 75 74 20 20 20   char **ppOut   
35d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d2f 20 2f 2a 20 4f 55 54 3a 20 4d 61 6c 6c 6f 63 65   /* OUT: Malloce
35d30 64 20 72 65 73 75 6c 74 20 62 75 66 66 65 72 20  d result buffer 
35d31 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
35d32 20 54 65 72 6d 53 65 6c 65 63 74 20 74 73 63 3b   TermSelect tsc;
35d33 0a 20 20 46 74 73 33 53 65 67 46 69 6c 74 65 72  .  Fts3SegFilter
35d34 20 66 69 6c 74 65 72 3b 20 20 20 20 20 20 20 20   filter;        
35d35 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20 74 65     /* Segment te
35d36 72 6d 20 66 69 6c 74 65 72 20 63 6f 6e 66 69 67  rm filter config
35d37 75 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 46 74 73  uration */.  Fts
35d38 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70 53  3SegReader **apS
35d39 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20 2f 2a 20  egment = 0;  /* 
35d3a 41 72 72 61 79 20 6f 66 20 73 65 67 6d 65 6e 74  Array of segment
35d3b 73 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66  s to read data f
35d3c 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65  rom */.  int nSe
35d3d 67 6d 65 6e 74 20 3d 20 30 3b 20 20 20 20 20 20  gment = 0;      
35d3e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
35d3f 20 6f 66 20 61 70 53 65 67 6d 65 6e 74 20 61 72   of apSegment ar
35d40 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  ray */.  int nAl
35d41 6c 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20  loc = 0;        
35d42 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
35d43 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 73 65  cated size of se
35d44 67 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20  gment array */. 
35d45 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
35d46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d47 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
35d48 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
35d49 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20  t *pStmt;       
35d4a 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
35d4b 65 6d 65 6e 74 20 74 6f 20 73 63 61 6e 20 25 5f  ement to scan %_
35d4c 73 65 67 64 69 72 20 74 61 62 6c 65 20 2a 2f 0a  segdir table */.
35d4d 20 20 69 6e 74 20 69 41 67 65 20 3d 20 30 3b 20    int iAge = 0; 
35d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d4f 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73    /* Used to ass
35d50 69 67 6e 20 61 67 65 73 20 74 6f 20 73 65 67 6d  ign ages to segm
35d51 65 6e 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f  ents */..  /* Lo
35d52 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
35d53 6e 74 69 72 65 20 25 5f 73 65 67 64 69 72 20 74  ntire %_segdir t
35d54 61 62 6c 65 2e 20 46 6f 72 20 65 61 63 68 20 73  able. For each s
35d55 65 67 6d 65 6e 74 2c 20 63 72 65 61 74 65 20 61  egment, create a
35d56 0a 20 20 2a 2a 20 46 74 73 33 53 65 67 52 65 61  .  ** Fts3SegRea
35d57 64 65 72 20 74 6f 20 69 74 65 72 61 74 65 20 74  der to iterate t
35d58 68 72 6f 75 67 68 20 74 68 65 20 73 75 62 73 65  hrough the subse
35d59 74 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  t of the segment
35d5a 20 6c 65 61 76 65 73 0a 20 20 2a 2a 20 74 68 61   leaves.  ** tha
35d5b 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20  t may contain a 
35d5c 74 65 72 6d 20 74 68 61 74 20 6d 61 74 63 68 65  term that matche
35d5d 73 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 46  s zTerm/nTerm. F
35d5e 6f 72 20 6e 6f 6e 2d 70 72 65 66 69 78 0a 20 20  or non-prefix.  
35d5f 2a 2a 20 73 65 61 72 63 68 65 73 2c 20 74 68 69  ** searches, thi
35d60 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 73 69  s is always a si
35d61 6e 67 6c 65 20 6c 65 61 66 2e 20 46 6f 72 20 70  ngle leaf. For p
35d62 72 65 66 69 78 20 73 65 61 72 63 68 65 73 2c 20  refix searches, 
35d63 74 68 69 73 0a 20 20 2a 2a 20 6d 61 79 20 62 65  this.  ** may be
35d64 20 61 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 6c   a contiguous bl
35d65 6f 63 6b 20 6f 66 20 6c 65 61 76 65 73 2e 0a 20  ock of leaves.. 
35d66 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 64   **.  ** The cod
35d67 65 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 64  e in this loop d
35d68 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79  oes not actually
35d69 20 6c 6f 61 64 20 61 6e 79 20 6c 65 61 76 65 73   load any leaves
35d6a 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 20 20 2a   into memory.  *
35d6b 2a 20 28 75 6e 6c 65 73 73 20 74 68 65 20 72 6f  * (unless the ro
35d6c 6f 74 20 6e 6f 64 65 20 68 61 70 70 65 6e 73 20  ot node happens 
35d6d 74 6f 20 62 65 20 61 20 6c 65 61 66 29 2e 20 49  to be a leaf). I
35d6e 74 20 73 69 6d 70 6c 79 20 65 78 61 6d 69 6e 65  t simply examine
35d6f 73 20 74 68 65 0a 20 20 2a 2a 20 62 2d 74 72 65  s the.  ** b-tre
35d70 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 64  e structure to d
35d71 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 20 6c  etermine which l
35d72 65 61 76 65 73 20 6e 65 65 64 20 74 6f 20 62 65  eaves need to be
35d73 20 69 6e 73 70 65 63 74 65 64 2e 0a 20 20 2a 2f   inspected..  */
35d74 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46  .  rc = sqlite3F
35d75 74 73 33 41 6c 6c 53 65 67 64 69 72 73 28 70 2c  ts3AllSegdirs(p,
35d76 20 26 70 53 74 6d 74 29 3b 0a 20 20 77 68 69 6c   &pStmt);.  whil
35d77 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
35d78 20 26 26 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d   && SQLITE_ROW==
35d79 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  (rc = sqlite3_st
35d7a 65 70 28 70 53 74 6d 74 29 29 20 29 7b 0a 20 20  ep(pStmt)) ){.  
35d7b 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
35d7c 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69  *pNew = 0;.    i
35d7d 6e 74 20 6e 52 6f 6f 74 20 3d 20 73 71 6c 69 74  nt nRoot = sqlit
35d7e 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
35d7f 70 53 74 6d 74 2c 20 34 29 3b 0a 20 20 20 20 63  pStmt, 4);.    c
35d80 68 61 72 20 63 6f 6e 73 74 20 2a 7a 52 6f 6f 74  har const *zRoot
35d81 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
35d82 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 34 29  n_blob(pStmt, 4)
35d83 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
35d84 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70  3_column_int64(p
35d85 53 74 6d 74 2c 20 31 29 3d 3d 30 20 29 7b 0a 20  Stmt, 1)==0 ){. 
35d86 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 69       /* The enti
35d87 72 65 20 73 65 67 6d 65 6e 74 20 69 73 20 73 74  re segment is st
35d88 6f 72 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f 74  ored on the root
35d89 20 6e 6f 64 65 20 28 77 68 69 63 68 20 6d 75 73   node (which mus
35d8a 74 20 62 65 20 61 0a 20 20 20 20 20 20 2a 2a 20  t be a.      ** 
35d8b 6c 65 61 66 29 2e 20 44 6f 20 6e 6f 74 20 62 6f  leaf). Do not bo
35d8c 74 68 65 72 20 69 6e 73 70 65 63 74 69 6e 67 20  ther inspecting 
35d8d 61 6e 79 20 64 61 74 61 20 69 6e 20 74 68 69 73  any data in this
35d8e 20 63 61 73 65 2c 20 6a 75 73 74 0a 20 20 20 20   case, just.    
35d8f 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20 46 74    ** create a Ft
35d90 73 33 53 65 67 52 65 61 64 65 72 20 74 6f 20 73  s3SegReader to s
35d91 63 61 6e 20 74 68 65 20 73 69 6e 67 6c 65 20 6c  can the single l
35d92 65 61 66 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  eaf. .      */. 
35d93 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
35d94 33 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65  3Fts3SegReaderNe
35d95 77 28 70 2c 20 69 41 67 65 2c 20 30 2c 20 30 2c  w(p, iAge, 0, 0,
35d96 20 30 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f 6f 74   0, zRoot, nRoot
35d97 2c 20 26 70 4e 65 77 29 3b 0a 20 20 20 20 7d 65  , &pNew);.    }e
35d98 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  lse{.      int r
35d99 63 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c2;             
35d9a 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
35d9b 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65   value of sqlite
35d9c 33 46 74 73 33 52 65 61 64 42 6c 6f 63 6b 28 29  3Fts3ReadBlock()
35d9d 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
35d9e 33 5f 69 6e 74 36 34 20 69 31 3b 20 20 20 20 20  3_int64 i1;     
35d9f 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 69 64        /* Blockid
35da0 20 6f 66 20 6c 65 61 66 20 74 68 61 74 20 6d 61   of leaf that ma
35da1 79 20 63 6f 6e 74 61 69 6e 20 7a 54 65 72 6d 20  y contain zTerm 
35da2 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74  */.      rc = ft
35da3 73 33 53 65 6c 65 63 74 4c 65 61 66 28 70 2c 20  s3SelectLeaf(p, 
35da4 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 52  zTerm, nTerm, zR
35da5 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 20 26 69 31 29  oot, nRoot, &i1)
35da6 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
35da7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
35da8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
35da9 36 34 20 69 32 20 3d 20 73 71 6c 69 74 65 33 5f  64 i2 = sqlite3_
35daa 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74  column_int64(pSt
35dab 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  mt, 2);.        
35dac 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
35dad 53 65 67 52 65 61 64 65 72 4e 65 77 28 70 2c 20  SegReaderNew(p, 
35dae 69 41 67 65 2c 20 69 31 2c 20 69 32 2c 20 30 2c  iAge, i1, i2, 0,
35daf 20 30 2c 20 30 2c 20 26 70 4e 65 77 29 3b 0a 20   0, 0, &pNew);. 
35db0 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
35db1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
35db2 61 6c 6c 20 74 6f 20 52 65 61 64 42 6c 6f 63 6b  all to ReadBlock
35db3 28 29 20 73 65 72 76 65 73 20 74 6f 20 72 65 73  () serves to res
35db4 65 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  et the SQL state
35db5 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 75 73  ment.      ** us
35db6 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 62  ed to retrieve b
35db7 6c 6f 63 6b 73 20 6f 66 20 64 61 74 61 20 66 72  locks of data fr
35db8 6f 6d 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  om the %_segment
35db9 73 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69  s table. If it i
35dba 73 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 72  s.      ** not r
35dbb 65 73 65 74 20 68 65 72 65 2c 20 74 68 65 6e 20  eset here, then 
35dbc 69 74 20 6d 61 79 20 72 65 6d 61 69 6e 20 63 6c  it may remain cl
35dbd 61 73 73 69 66 69 65 64 20 61 73 20 61 6e 20 61  assified as an a
35dbe 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20  ctive statement 
35dbf 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 53 51 4c  .      ** by SQL
35dc0 69 74 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6c  ite, which may l
35dc1 65 61 64 20 74 6f 20 22 44 52 4f 50 20 54 41 42  ead to "DROP TAB
35dc2 4c 45 22 20 6f 72 20 22 44 45 54 41 43 48 22 20  LE" or "DETACH" 
35dc3 63 6f 6d 6d 61 6e 64 73 20 0a 20 20 20 20 20 20  commands .      
35dc4 2a 2a 20 66 61 69 6c 69 6e 67 2e 0a 20 20 20 20  ** failing..    
35dc5 20 20 2a 2f 20 0a 20 20 20 20 20 20 72 63 32 20    */ .      rc2 
35dc6 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  = sqlite3Fts3Rea
35dc7 64 42 6c 6f 63 6b 28 70 2c 20 30 2c 20 30 2c 20  dBlock(p, 0, 0, 
35dc8 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
35dc9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
35dca 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
35dcb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
35dcc 20 20 20 69 41 67 65 2b 2b 3b 0a 0a 20 20 20 20     iAge++;..    
35dcd 2f 2a 20 49 66 20 61 20 6e 65 77 20 46 74 73 33  /* If a new Fts3
35dce 53 65 67 52 65 61 64 65 72 20 77 61 73 20 61 6c  SegReader was al
35dcf 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 69 74 20  located, add it 
35dd0 74 6f 20 74 68 65 20 61 70 53 65 67 6d 65 6e 74  to the apSegment
35dd1 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 20 20 61   array. */.    a
35dd2 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 7c  ssert( pNew!=0 |
35dd3 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc!=SQLITE_OK 
35dd4 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 20  );.    if( pNew 
35dd5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 53 65  ){.      if( nSe
35dd6 67 6d 65 6e 74 3d 3d 6e 41 6c 6c 6f 63 20 29 7b  gment==nAlloc ){
35dd7 0a 20 20 20 20 20 20 20 20 46 74 73 33 53 65 67  .        Fts3Seg
35dd8 52 65 61 64 65 72 20 2a 2a 70 41 72 72 61 79 3b  Reader **pArray;
35dd9 0a 20 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20  .        nAlloc 
35dda 2b 3d 20 31 36 3b 0a 20 20 20 20 20 20 20 20 70  += 16;.        p
35ddb 41 72 72 61 79 20 3d 20 28 46 74 73 33 53 65 67  Array = (Fts3Seg
35ddc 52 65 61 64 65 72 20 2a 2a 29 73 71 6c 69 74 65  Reader **)sqlite
35ddd 33 5f 72 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20  3_realloc(.     
35dde 20 20 20 20 20 20 20 61 70 53 65 67 6d 65 6e 74         apSegment
35ddf 2c 20 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28  , nAlloc*sizeof(
35de0 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 29  Fts3SegReader *)
35de1 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
35de2 20 20 20 20 69 66 28 20 21 70 41 72 72 61 79 20      if( !pArray 
35de3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
35de4 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
35de5 72 46 72 65 65 28 70 2c 20 70 4e 65 77 29 3b 0a  rFree(p, pNew);.
35de6 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
35de7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
35de8 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69         goto fini
35de9 73 68 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  shed;.        }.
35dea 20 20 20 20 20 20 20 20 61 70 53 65 67 6d 65 6e          apSegmen
35deb 74 20 3d 20 70 41 72 72 61 79 3b 0a 20 20 20 20  t = pArray;.    
35dec 20 20 7d 0a 20 20 20 20 20 20 61 70 53 65 67 6d    }.      apSegm
35ded 65 6e 74 5b 6e 53 65 67 6d 65 6e 74 2b 2b 5d 20  ent[nSegment++] 
35dee 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 20 20  = pNew;.    }.  
35def 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
35df0 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 61  TE_DONE ){.    a
35df1 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
35df2 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 67 6f 74 6f  E_OK );.    goto
35df3 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 7d 0a 0a   finished;.  }..
35df4 20 20 6d 65 6d 73 65 74 28 26 74 73 63 2c 20 30    memset(&tsc, 0
35df5 2c 20 73 69 7a 65 6f 66 28 54 65 72 6d 53 65 6c  , sizeof(TermSel
35df6 65 63 74 29 29 3b 0a 20 20 74 73 63 2e 69 73 52  ect));.  tsc.isR
35df7 65 71 50 6f 73 20 3d 20 69 73 52 65 71 50 6f 73  eqPos = isReqPos
35df8 3b 0a 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67  ;..  filter.flag
35df9 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  s = FTS3_SEGMENT
35dfa 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20 0a 20  _IGNORE_EMPTY . 
35dfb 20 20 20 20 20 20 20 7c 20 28 69 73 50 72 65 66         | (isPref
35dfc 69 78 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e  ix ? FTS3_SEGMEN
35dfd 54 5f 50 52 45 46 49 58 20 3a 20 30 29 0a 20 20  T_PREFIX : 0).  
35dfe 20 20 20 20 20 20 7c 20 28 69 73 52 65 71 50 6f        | (isReqPo
35dff 73 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  s ? FTS3_SEGMENT
35e00 5f 52 45 51 55 49 52 45 5f 50 4f 53 20 3a 20 30  _REQUIRE_POS : 0
35e01 29 0a 20 20 20 20 20 20 20 20 7c 20 28 69 43 6f  ).        | (iCo
35e02 6c 75 6d 6e 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20  lumn<p->nColumn 
35e03 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43  ? FTS3_SEGMENT_C
35e04 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52 20 3a 20 30  OLUMN_FILTER : 0
35e05 29 3b 0a 20 20 66 69 6c 74 65 72 2e 69 43 6f 6c  );.  filter.iCol
35e06 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 66 69   = iColumn;.  fi
35e07 6c 74 65 72 2e 7a 54 65 72 6d 20 3d 20 7a 54 65  lter.zTerm = zTe
35e08 72 6d 3b 0a 20 20 66 69 6c 74 65 72 2e 6e 54 65  rm;.  filter.nTe
35e09 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 72  rm = nTerm;..  r
35e0a 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53  c = sqlite3Fts3S
35e0b 65 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28  egReaderIterate(
35e0c 70 2c 20 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53  p, apSegment, nS
35e0d 65 67 6d 65 6e 74 2c 20 26 66 69 6c 74 65 72 2c  egment, &filter,
35e0e 0a 20 20 20 20 20 20 66 74 73 33 54 65 72 6d 53  .      fts3TermS
35e0f 65 6c 65 63 74 43 62 2c 20 28 76 6f 69 64 20 2a  electCb, (void *
35e10 29 26 74 73 63 0a 20 20 29 3b 0a 0a 20 20 69 66  )&tsc.  );..  if
35e11 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
35e12 29 7b 0a 20 20 20 20 2a 70 70 4f 75 74 20 3d 20  ){.    *ppOut = 
35e13 74 73 63 2e 61 4f 75 74 70 75 74 3b 0a 20 20 20  tsc.aOutput;.   
35e14 20 2a 70 6e 4f 75 74 20 3d 20 74 73 63 2e 6e 4f   *pnOut = tsc.nO
35e15 75 74 70 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  utput;.  }else{.
35e16 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
35e17 28 74 73 63 2e 61 4f 75 74 70 75 74 29 3b 0a 20  (tsc.aOutput);. 
35e18 20 7d 0a 0a 66 69 6e 69 73 68 65 64 3a 0a 20 20   }..finished:.  
35e19 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53  sqlite3_reset(pS
35e1a 74 6d 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  tmt);.  for(i=0;
35e1b 20 69 3c 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b   i<nSegment; i++
35e1c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
35e1d 73 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28  s3SegReaderFree(
35e1e 70 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29  p, apSegment[i])
35e1f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
35e20 66 72 65 65 28 61 70 53 65 67 6d 65 6e 74 29 3b  free(apSegment);
35e21 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
35e22 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
35e23 61 20 44 6f 63 4c 69 73 74 20 63 6f 72 72 65 73  a DocList corres
35e24 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70  ponding to the p
35e25 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 2e 0a  hrase *pPhrase..
35e26 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
35e27 73 33 50 68 72 61 73 65 53 65 6c 65 63 74 28 0a  s3PhraseSelect(.
35e28 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
35e29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e2a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
35e2b 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46  le handle */.  F
35e2c 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
35e2d 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  se,            /
35e2e 2a 20 50 68 72 61 73 65 20 74 6f 20 72 65 74 75  * Phrase to retu
35e2f 72 6e 20 61 20 64 6f 63 6c 69 73 74 20 66 6f 72  rn a doclist for
35e30 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 65 71 50   */.  int isReqP
35e31 6f 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  os,             
35e32 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
35e33 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 63   output should c
35e34 6f 6e 74 61 69 6e 20 70 6f 73 69 74 69 6f 6e 73  ontain positions
35e35 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61 4f   */.  char **paO
35e36 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ut,             
35e37 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
35e38 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27  inter to malloc'
35e39 64 20 72 65 73 75 6c 74 20 62 75 66 66 65 72 20  d result buffer 
35e3a 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 75 74 20  */.  int *pnOut 
35e3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e3c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a       /* OUT: Siz
35e3d 65 20 6f 66 20 62 75 66 66 65 72 20 61 74 20 2a  e of buffer at *
35e3e 70 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20 20 63 68  paOut */.){.  ch
35e3f 61 72 20 2a 70 4f 75 74 20 3d 20 30 3b 0a 20 20  ar *pOut = 0;.  
35e40 69 6e 74 20 6e 4f 75 74 20 3d 20 30 3b 0a 20 20  int nOut = 0;.  
35e41 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
35e42 4f 4b 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  OK;.  int ii;.  
35e43 69 6e 74 20 69 43 6f 6c 20 3d 20 70 50 68 72 61  int iCol = pPhra
35e44 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 69  se->iColumn;.  i
35e45 6e 74 20 69 73 54 65 72 6d 50 6f 73 20 3d 20 28  nt isTermPos = (
35e46 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e  pPhrase->nToken>
35e47 31 20 7c 7c 20 69 73 52 65 71 50 6f 73 29 3b 0a  1 || isReqPos);.
35e48 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 50  .  assert( p->nP
35e49 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b  endingData==0 );
35e4a 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
35e4b 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  <pPhrase->nToken
35e4c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  ; ii++){.    str
35e4d 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 20  uct PhraseToken 
35e4e 2a 70 54 6f 6b 20 3d 20 26 70 50 68 72 61 73 65  *pTok = &pPhrase
35e4f 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 3b 0a 20 20  ->aToken[ii];.  
35e50 20 20 63 68 61 72 20 2a 7a 20 3d 20 70 54 6f 6b    char *z = pTok
35e51 2d 3e 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20  ->z;            
35e52 2f 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20 6f 66  /* Next token of
35e53 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f 0a 20   the phrase */. 
35e54 20 20 20 69 6e 74 20 6e 20 3d 20 70 54 6f 6b 2d     int n = pTok-
35e55 3e 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  >n;             
35e56 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 20 69 6e   /* Size of z in
35e57 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 69 6e   bytes */.    in
35e58 74 20 69 73 50 72 65 66 69 78 20 3d 20 70 54 6f  t isPrefix = pTo
35e59 6b 2d 3e 69 73 50 72 65 66 69 78 3b 2f 2a 20 54  k->isPrefix;/* T
35e5a 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 69 73 20  rue if token is 
35e5b 61 20 70 72 65 66 69 78 20 2a 2f 0a 20 20 20 20  a prefix */.    
35e5c 63 68 61 72 20 2a 70 4c 69 73 74 3b 20 20 20 20  char *pList;    
35e5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35e5e 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65   Pointer to toke
35e5f 6e 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20  n doclist */.   
35e60 20 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20 20 20   int nList;     
35e61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35e62 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
35e63 20 61 74 20 70 4c 69 73 74 20 2a 2f 0a 0a 20 20   at pList */..  
35e64 20 20 72 63 20 3d 20 66 74 73 33 54 65 72 6d 53    rc = fts3TermS
35e65 65 6c 65 63 74 28 70 2c 20 69 43 6f 6c 2c 20 7a  elect(p, iCol, z
35e66 2c 20 6e 2c 20 69 73 50 72 65 66 69 78 2c 20 69  , n, isPrefix, i
35e67 73 54 65 72 6d 50 6f 73 2c 20 26 6e 4c 69 73 74  sTermPos, &nList
35e68 2c 20 26 70 4c 69 73 74 29 3b 0a 20 20 20 20 69  , &pList);.    i
35e69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
35e6a 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69   ) break;..    i
35e6b 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20  f( ii==0 ){.    
35e6c 20 20 70 4f 75 74 20 3d 20 70 4c 69 73 74 3b 0a    pOut = pList;.
35e6d 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 6e 4c 69        nOut = nLi
35e6e 73 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  st;.    }else{. 
35e6f 20 20 20 20 20 2f 2a 20 4d 65 72 67 65 20 74 68       /* Merge th
35e70 65 20 6e 65 77 20 74 65 72 6d 20 6c 69 73 74 20  e new term list 
35e71 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  and the current 
35e72 6f 75 74 70 75 74 2e 20 49 66 20 74 68 69 73 20  output. If this 
35e73 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  is the.      ** 
35e74 6c 61 73 74 20 74 65 72 6d 20 69 6e 20 74 68 65  last term in the
35e75 20 70 68 72 61 73 65 2c 20 61 6e 64 20 70 6f 73   phrase, and pos
35e76 69 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 72  itions are not r
35e77 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 0a 20  equired in the. 
35e78 20 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 6f       ** output o
35e79 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  f this function,
35e7a 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 63   the positions c
35e7b 61 6e 20 62 65 20 64 72 6f 70 70 65 64 20 61 73  an be dropped as
35e7c 20 70 61 72 74 0a 20 20 20 20 20 20 2a 2a 20 6f   part.      ** o
35e7d 66 20 74 68 69 73 20 6d 65 72 67 65 2e 20 45 69  f this merge. Ei
35e7e 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 72 65  ther way, the re
35e7f 73 75 6c 74 20 6f 66 20 74 68 69 73 20 6d 65 72  sult of this mer
35e80 67 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20  ge will be.     
35e81 20 2a 2a 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   ** smaller than
35e82 20 6e 4c 69 73 74 20 62 79 74 65 73 2e 20 54 68   nList bytes. Th
35e83 65 20 63 6f 64 65 20 69 6e 20 66 74 73 33 44 6f  e code in fts3Do
35e84 63 6c 69 73 74 4d 65 72 67 65 28 29 20 69 73 20  clistMerge() is 
35e85 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 2a 2a  written.      **
35e86 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 73   so that it is s
35e87 61 66 65 20 74 6f 20 75 73 65 20 70 4c 69 73 74  afe to use pList
35e88 20 61 73 20 74 68 65 20 6f 75 74 70 75 74 20 61   as the output a
35e89 73 20 77 65 6c 6c 20 61 73 20 61 6e 20 69 6e 70  s well as an inp
35e8a 75 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74  ut.      ** in t
35e8b 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20  his case..      
35e8c 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6d 65 72  */.      int mer
35e8d 67 65 74 79 70 65 20 3d 20 4d 45 52 47 45 5f 50  getype = MERGE_P
35e8e 4f 53 5f 50 48 52 41 53 45 3b 0a 20 20 20 20 20  OS_PHRASE;.     
35e8f 20 69 66 28 20 69 69 3d 3d 70 50 68 72 61 73 65   if( ii==pPhrase
35e90 2d 3e 6e 54 6f 6b 65 6e 2d 31 20 26 26 20 21 69  ->nToken-1 && !i
35e91 73 52 65 71 50 6f 73 20 29 7b 0a 20 20 20 20 20  sReqPos ){.     
35e92 20 20 20 6d 65 72 67 65 74 79 70 65 20 3d 20 4d     mergetype = M
35e93 45 52 47 45 5f 50 48 52 41 53 45 3b 0a 20 20 20  ERGE_PHRASE;.   
35e94 20 20 20 7d 0a 20 20 20 20 20 20 66 74 73 33 44     }.      fts3D
35e95 6f 63 6c 69 73 74 4d 65 72 67 65 28 6d 65 72 67  oclistMerge(merg
35e96 65 74 79 70 65 2c 20 30 2c 20 30 2c 20 70 4c 69  etype, 0, 0, pLi
35e97 73 74 2c 20 26 6e 4f 75 74 2c 20 70 4f 75 74 2c  st, &nOut, pOut,
35e98 20 6e 4f 75 74 2c 20 70 4c 69 73 74 2c 20 6e 4c   nOut, pList, nL
35e99 69 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ist);.      sqli
35e9a 74 65 33 5f 66 72 65 65 28 70 4f 75 74 29 3b 0a  te3_free(pOut);.
35e9b 20 20 20 20 20 20 70 4f 75 74 20 3d 20 70 4c 69        pOut = pLi
35e9c 73 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  st;.    }.  }.. 
35e9d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35e9e 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 61 4f 75 74  OK ){.    *paOut
35e9f 20 3d 20 70 4f 75 74 3b 0a 20 20 20 20 2a 70 6e   = pOut;.    *pn
35ea0 4f 75 74 20 3d 20 6e 4f 75 74 3b 0a 20 20 7d 65  Out = nOut;.  }e
35ea1 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
35ea2 5f 66 72 65 65 28 70 4f 75 74 29 3b 0a 20 20 7d  _free(pOut);.  }
35ea3 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
35ea4 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20  ./*.** Evaluate 
35ea5 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 65 78  the full-text ex
35ea6 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61  pression pExpr a
35ea7 67 61 69 6e 73 74 20 66 74 73 33 20 74 61 62 6c  gainst fts3 tabl
35ea8 65 20 70 54 61 62 2e 20 53 74 6f 72 65 0a 2a 2a  e pTab. Store.**
35ea9 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 64   the resulting d
35eaa 6f 63 6c 69 73 74 20 69 6e 20 2a 70 61 4f 75 74  oclist in *paOut
35eab 20 61 6e 64 20 2a 70 6e 4f 75 74 2e 0a 2a 2f 0a   and *pnOut..*/.
35eac 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 46  static int evalF
35ead 74 73 33 45 78 70 72 28 0a 20 20 46 74 73 33 54  ts3Expr(.  Fts3T
35eae 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
35eaf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
35eb0 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
35eb1 6c 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  le */.  Fts3Expr
35eb2 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
35eb3 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
35eb4 64 20 66 74 73 33 20 65 78 70 72 65 73 73 69 6f  d fts3 expressio
35eb5 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61  n */.  char **pa
35eb6 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  Out,            
35eb7 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
35eb8 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63  ointer to malloc
35eb9 27 64 20 72 65 73 75 6c 74 20 62 75 66 66 65 72  'd result buffer
35eba 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 75 74   */.  int *pnOut
35ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ebc 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69        /* OUT: Si
35ebd 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74 20  ze of buffer at 
35ebe 2a 70 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20 20 69  *paOut */.){.  i
35ebf 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
35ec0 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
35ec1 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
35ec2 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20  ..  /* Zero the 
35ec3 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72  output parameter
35ec4 73 2e 20 2a 2f 0a 20 20 2a 70 61 4f 75 74 20 3d  s. */.  *paOut =
35ec5 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 30   0;.  *pnOut = 0
35ec6 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 20 29  ;..  if( pExpr )
35ec7 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  {.    if( pExpr-
35ec8 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
35ec9 5f 50 48 52 41 53 45 20 29 7b 0a 20 20 20 20 20  _PHRASE ){.     
35eca 20 69 6e 74 20 69 73 52 65 71 50 6f 73 20 3d 20   int isReqPos = 
35ecb 28 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 20  (pExpr->pParent 
35ecc 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e  && pExpr->pParen
35ecd 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
35ece 52 59 5f 4e 45 41 52 29 3b 0a 20 20 20 20 20 20  RY_NEAR);.      
35ecf 72 63 20 3d 20 66 74 73 33 50 68 72 61 73 65 53  rc = fts3PhraseS
35ed0 65 6c 65 63 74 28 70 2c 20 70 45 78 70 72 2d 3e  elect(p, pExpr->
35ed1 70 50 68 72 61 73 65 2c 20 69 73 52 65 71 50 6f  pPhrase, isReqPo
35ed2 73 2c 20 70 61 4f 75 74 2c 20 70 6e 4f 75 74 29  s, paOut, pnOut)
35ed3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
35ed4 20 20 20 63 68 61 72 20 2a 61 4c 65 66 74 3b 0a     char *aLeft;.
35ed5 20 20 20 20 20 20 63 68 61 72 20 2a 61 52 69 67        char *aRig
35ed6 68 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4c  ht;.      int nL
35ed7 65 66 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  eft;.      int n
35ed8 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20 69 66  Right;..      if
35ed9 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
35eda 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28   = evalFts3Expr(
35edb 70 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  p, pExpr->pRight
35edc 2c 20 26 61 52 69 67 68 74 2c 20 26 6e 52 69 67  , &aRight, &nRig
35edd 68 74 29 29 0a 20 20 20 20 20 20 20 26 26 20 53  ht)).       && S
35ede 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
35edf 65 76 61 6c 46 74 73 33 45 78 70 72 28 70 2c 20  evalFts3Expr(p, 
35ee0 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 61  pExpr->pLeft, &a
35ee1 4c 65 66 74 2c 20 26 6e 4c 65 66 74 29 29 0a 20  Left, &nLeft)). 
35ee2 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
35ee3 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 65  assert( pExpr->e
35ee4 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
35ee5 45 41 52 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54  EAR || pExpr->eT
35ee6 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52  ype==FTSQUERY_OR
35ee7 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 20 20       .          
35ee8 20 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54 79 70    || pExpr->eTyp
35ee9 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e 44 20  e==FTSQUERY_AND 
35eea 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54 79 70 65   || pExpr->eType
35eeb 3d 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54 0a 20  ==FTSQUERY_NOT. 
35eec 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
35eed 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
35eee 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20  >eType ){.      
35eef 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
35ef0 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
35ef1 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
35ef2 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20  pLeft;.         
35ef3 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 69     Fts3Expr *pRi
35ef4 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ght;.           
35ef5 20 69 6e 74 20 6d 65 72 67 65 74 79 70 65 20 3d   int mergetype =
35ef6 20 4d 45 52 47 45 5f 4e 45 41 52 3b 0a 20 20 20   MERGE_NEAR;.   
35ef7 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50 61           int nPa
35ef8 72 61 6d 31 3b 0a 20 20 20 20 20 20 20 20 20 20  ram1;.          
35ef9 20 20 69 6e 74 20 6e 50 61 72 61 6d 32 3b 0a 20    int nParam2;. 
35efa 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
35efb 2a 61 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20  *aBuffer;.      
35efc 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 20 20       .          
35efd 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 50 61    if( pExpr->pPa
35efe 72 65 6e 74 20 26 26 20 70 45 78 70 72 2d 3e 70  rent && pExpr->p
35eff 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46  Parent->eType==F
35f00 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a  TSQUERY_NEAR ){.
35f01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
35f02 72 67 65 74 79 70 65 20 3d 20 4d 45 52 47 45 5f  rgetype = MERGE_
35f03 50 4f 53 5f 4e 45 41 52 3b 0a 20 20 20 20 20 20  POS_NEAR;.      
35f04 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35f05 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
35f06 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
35f07 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4c 65        while( pLe
35f08 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  ft->eType==FTSQU
35f09 45 52 59 5f 4e 45 41 52 20 29 7b 20 0a 20 20 20  ERY_NEAR ){ .   
35f0a 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 66 74             pLeft
35f0b 3d 70 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a  =pLeft->pRight;.
35f0c 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
35f0d 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
35f0e 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74   = pExpr->pRight
35f0f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
35f10 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 65 54  sert( pRight->eT
35f11 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
35f12 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  RASE );.        
35f13 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
35f14 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
35f15 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 0a 20 20  RY_PHRASE );..  
35f16 20 20 20 20 20 20 20 20 20 20 6e 50 61 72 61 6d            nParam
35f17 31 20 3d 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72  1 = pExpr->nNear
35f18 2b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +1;.            
35f19 6e 50 61 72 61 6d 32 20 3d 20 6e 50 61 72 61 6d  nParam2 = nParam
35f1a 31 2b 70 4c 65 66 74 2d 3e 70 50 68 72 61 73 65  1+pLeft->pPhrase
35f1b 2d 3e 6e 54 6f 6b 65 6e 2b 70 52 69 67 68 74 2d  ->nToken+pRight-
35f1c 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
35f1d 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -2;.            
35f1e 61 42 75 66 66 65 72 20 3d 20 73 71 6c 69 74 65  aBuffer = sqlite
35f1f 33 5f 6d 61 6c 6c 6f 63 28 6e 4c 65 66 74 2b 6e  3_malloc(nLeft+n
35f20 52 69 67 68 74 2b 31 29 3b 0a 20 20 20 20 20 20  Right+1);.      
35f21 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 44        rc = fts3D
35f22 6f 63 6c 69 73 74 4d 65 72 67 65 28 6d 65 72 67  oclistMerge(merg
35f23 65 74 79 70 65 2c 20 6e 50 61 72 61 6d 31 2c 20  etype, nParam1, 
35f24 6e 50 61 72 61 6d 32 2c 20 61 42 75 66 66 65 72  nParam2, aBuffer
35f25 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35f26 20 20 70 6e 4f 75 74 2c 20 61 4c 65 66 74 2c 20    pnOut, aLeft, 
35f27 6e 4c 65 66 74 2c 20 61 52 69 67 68 74 2c 20 6e  nLeft, aRight, n
35f28 52 69 67 68 74 0a 20 20 20 20 20 20 20 20 20 20  Right.          
35f29 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20    );.           
35f2a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35f2b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
35f2c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
35f2d 28 61 42 75 66 66 65 72 29 3b 0a 20 20 20 20 20  (aBuffer);.     
35f2e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
35f2f 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 61 4f              *paO
35f30 75 74 20 3d 20 61 42 75 66 66 65 72 3b 0a 20 20  ut = aBuffer;.  
35f31 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
35f32 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
35f33 66 72 65 65 28 61 4c 65 66 74 29 3b 0a 20 20 20  free(aLeft);.   
35f34 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
35f35 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
35f36 20 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51         case FTSQ
35f37 55 45 52 59 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  UERY_OR: {.     
35f38 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61         /* Alloca
35f39 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72 20  te a buffer for 
35f3a 74 68 65 20 6f 75 74 70 75 74 2e 20 54 68 65 20  the output. The 
35f3b 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 69 73 20  maximum size is 
35f3c 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
35f3d 2a 2a 20 73 75 6d 20 6f 66 20 74 68 65 20 73 69  ** sum of the si
35f3e 7a 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20 69  zes of the two i
35f3f 6e 70 75 74 20 62 75 66 66 65 72 73 2e 20 54 68  nput buffers. Th
35f40 65 20 2b 31 20 74 65 72 6d 20 69 73 0a 20 20 20  e +1 term is.   
35f41 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 74           ** so t
35f42 68 61 74 20 61 20 62 75 66 66 65 72 20 6f 66 20  hat a buffer of 
35f43 7a 65 72 6f 20 62 79 74 65 73 20 69 73 20 6e 65  zero bytes is ne
35f44 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 2d 20  ver allocated - 
35f45 74 68 69 73 20 63 61 6e 0a 20 20 20 20 20 20 20  this can.       
35f46 20 20 20 20 20 2a 2a 20 63 61 75 73 65 20 66 74       ** cause ft
35f47 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 29  s3DoclistMerge()
35f48 20 74 6f 20 69 6e 63 6f 72 72 65 63 74 6c 79 20   to incorrectly 
35f49 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
35f4a 4d 45 4d 2e 0a 20 20 20 20 20 20 20 20 20 20 20  MEM..           
35f4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
35f4c 63 68 61 72 20 2a 61 42 75 66 66 65 72 20 3d 20  char *aBuffer = 
35f4d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
35f4e 52 69 67 68 74 2b 6e 4c 65 66 74 2b 31 29 3b 0a  Right+nLeft+1);.
35f4f 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
35f50 20 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72 67   fts3DoclistMerg
35f51 65 28 4d 45 52 47 45 5f 4f 52 2c 20 30 2c 20 30  e(MERGE_OR, 0, 0
35f52 2c 20 61 42 75 66 66 65 72 2c 20 70 6e 4f 75 74  , aBuffer, pnOut
35f53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35f54 20 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20    aLeft, nLeft, 
35f55 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74 0a 20  aRight, nRight. 
35f56 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
35f57 20 20 20 20 20 20 20 20 20 20 2a 70 61 4f 75 74            *paOut
35f58 20 3d 20 61 42 75 66 66 65 72 3b 0a 20 20 20 20   = aBuffer;.    
35f59 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
35f5a 66 72 65 65 28 61 4c 65 66 74 29 3b 0a 20 20 20  free(aLeft);.   
35f5b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
35f5c 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
35f5d 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20         default: 
35f5e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  {.            as
35f5f 73 65 72 74 28 20 46 54 53 51 55 45 52 59 5f 4e  sert( FTSQUERY_N
35f60 4f 54 3d 3d 4d 45 52 47 45 5f 4e 4f 54 20 26 26  OT==MERGE_NOT &&
35f61 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3d 3d 4d   FTSQUERY_AND==M
35f62 45 52 47 45 5f 41 4e 44 20 29 3b 0a 20 20 20 20  ERGE_AND );.    
35f63 20 20 20 20 20 20 20 20 66 74 73 33 44 6f 63 6c          fts3Docl
35f64 69 73 74 4d 65 72 67 65 28 70 45 78 70 72 2d 3e  istMerge(pExpr->
35f65 65 54 79 70 65 2c 20 30 2c 20 30 2c 20 61 4c 65  eType, 0, 0, aLe
35f66 66 74 2c 20 70 6e 4f 75 74 2c 0a 20 20 20 20 20  ft, pnOut,.     
35f67 20 20 20 20 20 20 20 20 20 20 20 61 4c 65 66 74             aLeft
35f68 2c 20 6e 4c 65 66 74 2c 20 61 52 69 67 68 74 2c  , nLeft, aRight,
35f69 20 6e 52 69 67 68 74 0a 20 20 20 20 20 20 20 20   nRight.        
35f6a 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20      );.         
35f6b 20 20 20 2a 70 61 4f 75 74 20 3d 20 61 4c 65 66     *paOut = aLef
35f6c 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  t;.            b
35f6d 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
35f6e 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
35f6f 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
35f70 33 5f 66 72 65 65 28 61 52 69 67 68 74 29 3b 0a  3_free(aRight);.
35f71 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
35f72 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
35f73 20 54 68 69 73 20 69 73 20 74 68 65 20 78 46 69   This is the xFi
35f74 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65 20 66  lter interface f
35f75 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  or the virtual t
35f76 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74 68  able.  See.** th
35f77 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
35f78 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 64  xFilter method d
35f79 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
35f7a 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
35f7b 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
35f7c 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 46 54 53  * If idxNum==FTS
35f7d 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43  3_FULLSCAN_SEARC
35f7e 48 20 74 68 65 6e 20 64 6f 20 61 20 66 75 6c 6c  H then do a full
35f7f 20 74 61 62 6c 65 20 73 63 61 6e 20 61 67 61 69   table scan agai
35f80 6e 73 74 0a 2a 2a 20 74 68 65 20 25 5f 63 6f 6e  nst.** the %_con
35f81 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tent table..**.*
35f82 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 46 54 53  * If idxNum==FTS
35f83 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 20 74  3_DOCID_SEARCH t
35f84 68 65 6e 20 64 6f 20 61 20 64 6f 63 69 64 20 6c  hen do a docid l
35f85 6f 6f 6b 75 70 20 66 6f 72 20 61 20 73 69 6e 67  ookup for a sing
35f86 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 74  le entry.** in t
35f87 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  he %_content tab
35f88 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78  le..**.** If idx
35f89 4e 75 6d 3e 3d 46 54 53 33 5f 46 55 4c 4c 54 45  Num>=FTS3_FULLTE
35f8a 58 54 5f 53 45 41 52 43 48 20 74 68 65 6e 20 75  XT_SEARCH then u
35f8b 73 65 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74  se the full text
35f8c 20 69 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20   index.  The.** 
35f8d 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65  column on the le
35f8e 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ft-hand side of 
35f8f 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
35f90 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  or is column.** 
35f91 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d 2d 46 54  number idxNum-FT
35f92 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52  S3_FULLTEXT_SEAR
35f93 43 48 2c 20 30 20 69 6e 64 65 78 65 64 2e 20 20  CH, 0 indexed.  
35f94 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72  argv[0] is the r
35f95 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 64  ight-hand.** sid
35f96 65 20 6f 66 20 74 68 65 20 4d 41 54 43 48 20 6f  e of the MATCH o
35f97 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f 2a 20 54  perator..*/./* T
35f98 4f 44 4f 28 73 68 65 73 73 29 20 55 70 67 72 61  ODO(shess) Upgra
35f99 64 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e  de the cursor in
35f9a 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64  itialization and
35f9b 20 64 65 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a   destruction to.
35f9c 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 66  ** account for f
35f9d 74 73 33 46 69 6c 74 65 72 4d 65 74 68 6f 64 28  ts3FilterMethod(
35f9e 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 6d  ) being called m
35f9f 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 6f 6e  ultiple times on
35fa0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 75 72   the.** same cur
35fa1 73 6f 72 2e 20 54 68 65 20 63 75 72 72 65 6e 74  sor. The current
35fa2 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76 65 72   solution is ver
35fa3 79 20 66 72 61 67 69 6c 65 2e 20 41 70 70 6c 79  y fragile. Apply
35fa4 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74 73 33 20   fix to.** fts3 
35fa5 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a  as appropriate..
35fa6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
35fa7 73 33 46 69 6c 74 65 72 4d 65 74 68 6f 64 28 0a  s3FilterMethod(.
35fa8 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
35fa9 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
35faa 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
35fab 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 71 75  used for this qu
35fac 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  ery */.  int idx
35fad 4e 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  Num,            
35fae 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 61           /* Stra
35faf 74 65 67 79 20 69 6e 64 65 78 20 2a 2f 0a 20 20  tegy index */.  
35fb0 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53  const char *idxS
35fb1 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
35fb2 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 20 20 69  /* Unused */.  i
35fb3 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20 20  nt nVal,        
35fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35fb5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
35fb6 65 6e 74 73 20 69 6e 20 61 70 56 61 6c 20 2a 2f  ents in apVal */
35fb7 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
35fb8 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20 20   **apVal        
35fb9 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20     /* Arguments 
35fba 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69 6e 67  for the indexing
35fbb 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a 20 20   scheme */.){.  
35fbc 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 53 71  const char *azSq
35fbd 6c 5b 5d 20 3d 20 7b 0a 20 20 20 20 22 53 45 4c  l[] = {.    "SEL
35fbe 45 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e 27 25  ECT * FROM %Q.'%
35fbf 71 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45 52 45  q_content' WHERE
35fc0 20 64 6f 63 69 64 20 3d 20 3f 22 2c 20 2f 2a 20   docid = ?", /* 
35fc1 6e 6f 6e 2d 66 75 6c 6c 2d 74 61 62 6c 65 2d 73  non-full-table-s
35fc2 63 61 6e 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45  can */.    "SELE
35fc3 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e 27 25 71  CT * FROM %Q.'%q
35fc4 5f 63 6f 6e 74 65 6e 74 27 22 2c 20 20 20 20 20  _content'",     
35fc5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
35fc6 75 6c 6c 2d 74 61 62 6c 65 2d 73 63 61 6e 20 2a  ull-table-scan *
35fc7 2f 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 72 63 3b  /.  };.  int rc;
35fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fc9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
35fca 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61  rn code */.  cha
35fcb 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 20  r *zSql;        
35fcc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35fcd 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73  SQL statement us
35fce 65 64 20 74 6f 20 61 63 63 65 73 73 20 25 5f 63  ed to access %_c
35fcf 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 46 74 73 33  ontent */.  Fts3
35fd0 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33  Table *p = (Fts3
35fd1 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d  Table *)pCursor-
35fd2 3e 70 56 74 61 62 3b 0a 20 20 46 74 73 33 43 75  >pVtab;.  Fts3Cu
35fd3 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74  rsor *pCsr = (Ft
35fd4 73 33 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73  s3Cursor *)pCurs
35fd5 6f 72 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41  or;..  UNUSED_PA
35fd6 52 41 4d 45 54 45 52 28 69 64 78 53 74 72 29 3b  RAMETER(idxStr);
35fd7 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
35fd8 54 45 52 28 6e 56 61 6c 29 3b 0a 0a 20 20 61 73  TER(nVal);..  as
35fd9 73 65 72 74 28 20 69 64 78 4e 75 6d 3e 3d 30 20  sert( idxNum>=0 
35fda 26 26 20 69 64 78 4e 75 6d 3c 3d 28 46 54 53 33  && idxNum<=(FTS3
35fdb 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48  _FULLTEXT_SEARCH
35fdc 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 3b 0a  +p->nColumn) );.
35fdd 20 20 61 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d    assert( nVal==
35fde 30 20 7c 7c 20 6e 56 61 6c 3d 3d 31 20 29 3b 0a  0 || nVal==1 );.
35fdf 20 20 61 73 73 65 72 74 28 20 28 6e 56 61 6c 3d    assert( (nVal=
35fe0 3d 30 29 3d 3d 28 69 64 78 4e 75 6d 3d 3d 46 54  =0)==(idxNum==FT
35fe1 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52  S3_FULLSCAN_SEAR
35fe2 43 48 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20  CH) );..  /* In 
35fe3 63 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20  case the cursor 
35fe4 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 65  has been used be
35fe5 66 6f 72 65 2c 20 63 6c 65 61 72 20 69 74 20 6e  fore, clear it n
35fe6 6f 77 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ow. */.  sqlite3
35fe7 5f 66 69 6e 61 6c 69 7a 65 28 70 43 73 72 2d 3e  _finalize(pCsr->
35fe8 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65  pStmt);.  sqlite
35fe9 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 44 6f  3_free(pCsr->aDo
35fea 63 6c 69 73 74 29 3b 0a 20 20 6d 65 6d 73 65 74  clist);.  memset
35feb 28 26 70 43 75 72 73 6f 72 5b 31 5d 2c 20 30 2c  (&pCursor[1], 0,
35fec 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75 72 73   sizeof(Fts3Curs
35fed 6f 72 29 2d 73 69 7a 65 6f 66 28 73 71 6c 69 74  or)-sizeof(sqlit
35fee 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 29 29  e3_vtab_cursor))
35fef 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 20  ;..  /* Compile 
35ff0 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
35ff1 6e 74 20 66 6f 72 20 74 68 69 73 20 63 75 72 73  nt for this curs
35ff2 6f 72 2e 20 46 6f 72 20 61 20 66 75 6c 6c 2d 74  or. For a full-t
35ff3 61 62 6c 65 2d 73 63 61 6e 2c 20 74 68 65 0a 20  able-scan, the. 
35ff4 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6c 6f   ** statement lo
35ff5 6f 70 73 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  ops through all 
35ff6 72 6f 77 73 20 6f 66 20 74 68 65 20 25 5f 63 6f  rows of the %_co
35ff7 6e 74 65 6e 74 20 74 61 62 6c 65 2e 20 46 6f 72  ntent table. For
35ff8 20 61 0a 20 20 2a 2a 20 66 75 6c 6c 2d 74 65 78   a.  ** full-tex
35ff9 74 20 71 75 65 72 79 20 6f 72 20 64 6f 63 69 64  t query or docid
35ffa 20 6c 6f 6f 6b 75 70 2c 20 74 68 65 20 73 74 61   lookup, the sta
35ffb 74 65 6d 65 6e 74 20 72 65 74 72 69 65 76 65 73  tement retrieves
35ffc 20 61 20 73 69 6e 67 6c 65 0a 20 20 2a 2a 20 72   a single.  ** r
35ffd 6f 77 20 62 79 20 64 6f 63 69 64 2e 0a 20 20 2a  ow by docid..  *
35ffe 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74  /.  zSql = sqlit
35fff 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c  e3_mprintf(azSql
36000 5b 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 46 55  [idxNum==FTS3_FU
36001 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 5d 2c 20  LLSCAN_SEARCH], 
36002 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  p->zDb, p->zName
36003 29 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29  );.  if( !zSql )
36004 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
36005 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65  E_NOMEM;.  }else
36006 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
36007 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 70 2d  e3_prepare_v2(p-
36008 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  >db, zSql, -1, &
36009 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b  pCsr->pStmt, 0);
3600a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
3600b 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 69  e(zSql);.  }.  i
3600c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3600d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
3600e 70 43 73 72 2d 3e 65 53 65 61 72 63 68 20 3d 20  pCsr->eSearch = 
3600f 28 69 31 36 29 69 64 78 4e 75 6d 3b 0a 0a 20 20  (i16)idxNum;..  
36010 69 66 28 20 69 64 78 4e 75 6d 3d 3d 46 54 53 33  if( idxNum==FTS3
36011 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 20 29 7b  _DOCID_SEARCH ){
36012 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
36013 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70 43 73  3_bind_value(pCs
36014 72 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 61 70 56  r->pStmt, 1, apV
36015 61 6c 5b 30 5d 29 3b 0a 20 20 7d 65 6c 73 65 20  al[0]);.  }else 
36016 69 66 28 20 69 64 78 4e 75 6d 21 3d 46 54 53 33  if( idxNum!=FTS3
36017 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48  _FULLSCAN_SEARCH
36018 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   ){.    int iCol
36019 20 3d 20 69 64 78 4e 75 6d 2d 46 54 53 33 5f 46   = idxNum-FTS3_F
3601a 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 3b 0a  ULLTEXT_SEARCH;.
3601b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
3601c 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e 73 74 20  zQuery = (const 
3601d 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
3601e 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b  alue_text(apVal[
3601f 30 5d 29 3b 0a 0a 20 20 20 20 69 66 28 20 7a 51  0]);..    if( zQ
36020 75 65 72 79 3d 3d 30 20 26 26 20 73 71 6c 69 74  uery==0 && sqlit
36021 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70  e3_value_type(ap
36022 56 61 6c 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f  Val[0])!=SQLITE_
36023 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 65  NULL ){.      re
36024 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
36025 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  M;.    }.    rc 
36026 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e  = sqlite3Fts3Pen
36027 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68 28 70  dingTermsFlush(p
36028 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
36029 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3602a 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20  n rc;..    rc = 
3602b 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
3602c 61 72 73 65 28 70 2d 3e 70 54 6f 6b 65 6e 69 7a  arse(p->pTokeniz
3602d 65 72 2c 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c  er, p->azColumn,
3602e 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 0a 20 20   p->nColumn, .  
3602f 20 20 20 20 20 20 69 43 6f 6c 2c 20 7a 51 75 65        iCol, zQue
36030 72 79 2c 20 2d 31 2c 20 26 70 43 73 72 2d 3e 70  ry, -1, &pCsr->p
36031 45 78 70 72 0a 20 20 20 20 29 3b 0a 20 20 20 20  Expr.    );.    
36032 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
36033 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
36034 20 20 20 20 72 63 20 3d 20 65 76 61 6c 46 74 73      rc = evalFts
36035 33 45 78 70 72 28 70 2c 20 70 43 73 72 2d 3e 70  3Expr(p, pCsr->p
36036 45 78 70 72 2c 20 26 70 43 73 72 2d 3e 61 44 6f  Expr, &pCsr->aDo
36037 63 6c 69 73 74 2c 20 26 70 43 73 72 2d 3e 6e 44  clist, &pCsr->nD
36038 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 70 43 73  oclist);.    pCs
36039 72 2d 3e 70 4e 65 78 74 49 64 20 3d 20 70 43 73  r->pNextId = pCs
3603a 72 2d 3e 61 44 6f 63 6c 69 73 74 3b 0a 20 20 20  r->aDoclist;.   
3603b 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64 20 3d   pCsr->iPrevId =
3603c 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72   0;.  }..  if( r
3603d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3603e 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75  eturn rc;.  retu
3603f 72 6e 20 66 74 73 33 4e 65 78 74 4d 65 74 68 6f  rn fts3NextMetho
36040 64 28 70 43 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f  d(pCursor);.}../
36041 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  * .** This is th
36042 65 20 78 45 6f 66 20 6d 65 74 68 6f 64 20 6f 66  e xEof method of
36043 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
36044 6c 65 2e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73  le. SQLite calls
36045 20 74 68 69 73 20 0a 2a 2a 20 72 6f 75 74 69 6e   this .** routin
36046 65 20 74 6f 20 66 69 6e 64 20 6f 75 74 20 69 66  e to find out if
36047 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64 20   it has reached 
36048 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72 65 73  the end of a res
36049 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74  ult set..*/.stat
3604a 69 63 20 69 6e 74 20 66 74 73 33 45 6f 66 4d 65  ic int fts3EofMe
3604b 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61  thod(sqlite3_vta
3604c 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
3604d 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 28 46  r){.  return ((F
3604e 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43 75 72  ts3Cursor *)pCur
3604f 73 6f 72 29 2d 3e 69 73 45 6f 66 3b 0a 7d 0a 0a  sor)->isEof;.}..
36050 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  /* .** This is t
36051 68 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f 64  he xRowid method
36052 2e 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  . The SQLite cor
36053 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  e calls this rou
36054 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74 72 69  tine to.** retri
36055 65 76 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f  eve the rowid fo
36056 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
36057 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  w of the result 
36058 73 65 74 2e 20 66 74 73 33 0a 2a 2a 20 65 78 70  set. fts3.** exp
36059 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e 74 2e 64  oses %_content.d
3605a 6f 63 69 64 20 61 73 20 74 68 65 20 72 6f 77 69  ocid as the rowi
3605b 64 20 66 6f 72 20 74 68 65 20 76 69 72 74 75 61  d for the virtua
3605c 6c 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20  l table. The.** 
3605d 72 6f 77 69 64 20 73 68 6f 75 6c 64 20 62 65 20  rowid should be 
3605e 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 52 6f 77  written to *pRow
3605f 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  id..*/.static in
36060 74 20 66 74 73 33 52 6f 77 69 64 4d 65 74 68 6f  t fts3RowidMetho
36061 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  d(sqlite3_vtab_c
36062 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
36063 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52  sqlite_int64 *pR
36064 6f 77 69 64 29 7b 0a 20 20 46 74 73 33 43 75 72  owid){.  Fts3Cur
36065 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73  sor *pCsr = (Fts
36066 33 43 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  3Cursor *) pCurs
36067 6f 72 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e  or;.  if( pCsr->
36068 61 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20 20 20  aDoclist ){.    
36069 2a 70 52 6f 77 69 64 20 3d 20 70 43 73 72 2d 3e  *pRowid = pCsr->
3606a 69 50 72 65 76 49 64 3b 0a 20 20 7d 65 6c 73 65  iPrevId;.  }else
3606b 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20  {.    *pRowid = 
3606c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
3606d 6e 74 36 34 28 70 43 73 72 2d 3e 70 53 74 6d 74  nt64(pCsr->pStmt
3606e 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
3606f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
36070 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73 20  ./* .** This is 
36071 74 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68  the xColumn meth
36072 6f 64 2c 20 63 61 6c 6c 65 64 20 62 79 20 53 51  od, called by SQ
36073 4c 69 74 65 20 74 6f 20 72 65 71 75 65 73 74 20  Lite to request 
36074 61 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20  a value from.** 
36075 74 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65  the row that the
36076 20 73 75 70 70 6c 69 65 64 20 63 75 72 73 6f 72   supplied cursor
36077 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
36078 73 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  s to..*/.static 
36079 69 6e 74 20 66 74 73 33 43 6f 6c 75 6d 6e 4d 65  int fts3ColumnMe
3607a 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  thod(.  sqlite3_
3607b 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
3607c 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f  rsor,   /* Curso
3607d 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 76 61  r to retrieve va
3607e 6c 75 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 73 71  lue from */.  sq
3607f 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
36080 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20 2f 2a  Context,      /*
36081 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 73 71 6c   Context for sql
36082 69 74 65 33 5f 72 65 73 75 6c 74 5f 78 78 78 28  ite3_result_xxx(
36083 29 20 63 61 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74  ) calls */.  int
36084 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
36085 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36086 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
36087 74 6f 20 72 65 61 64 20 76 61 6c 75 65 20 66 72  to read value fr
36088 6f 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  om */.){.  int r
36089 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
3608a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
3608b 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 46  turn Code */.  F
3608c 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20  ts3Cursor *pCsr 
3608d 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29  = (Fts3Cursor *)
3608e 20 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73 33   pCursor;.  Fts3
3608f 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33  Table *p = (Fts3
36090 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d  Table *)pCursor-
36091 3e 70 56 74 61 62 3b 0a 0a 20 20 2f 2a 20 54 68  >pVtab;..  /* Th
36092 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 73  e column value s
36093 75 70 70 6c 69 65 64 20 62 79 20 53 51 4c 69 74  upplied by SQLit
36094 65 20 6d 75 73 74 20 62 65 20 69 6e 20 72 61 6e  e must be in ran
36095 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ge. */.  assert(
36096 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c   iCol>=0 && iCol
36097 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29  <=p->nColumn+1 )
36098 3b 0a 0a 20 20 72 63 20 3d 20 66 74 73 33 43 75  ;..  rc = fts3Cu
36099 72 73 6f 72 53 65 65 6b 28 70 43 73 72 29 3b 0a  rsorSeek(pCsr);.
3609a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3609b 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 69  _OK ){.    if( i
3609c 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b  Col==p->nColumn+
3609d 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  1 ){.      /* Th
3609e 69 73 20 63 61 6c 6c 20 69 73 20 61 20 72 65 71  is call is a req
3609f 75 65 73 74 20 66 6f 72 20 74 68 65 20 22 64 6f  uest for the "do
360a0 63 69 64 22 20 63 6f 6c 75 6d 6e 2e 20 53 69 6e  cid" column. Sin
360a1 63 65 20 22 64 6f 63 69 64 22 20 69 73 20 61 6e  ce "docid" is an
360a2 20 0a 20 20 20 20 20 20 2a 2a 20 61 6c 69 61 73   .      ** alias
360a3 20 66 6f 72 20 22 72 6f 77 69 64 22 2c 20 75 73   for "rowid", us
360a4 65 20 74 68 65 20 78 52 6f 77 69 64 28 29 20 6d  e the xRowid() m
360a5 65 74 68 6f 64 20 74 6f 20 6f 62 74 61 69 6e 20  ethod to obtain 
360a6 74 68 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 20  the value..     
360a7 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
360a8 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a  3_int64 iRowid;.
360a9 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 52        rc = fts3R
360aa 6f 77 69 64 4d 65 74 68 6f 64 28 70 43 75 72 73  owidMethod(pCurs
360ab 6f 72 2c 20 26 69 52 6f 77 69 64 29 3b 0a 20 20  or, &iRowid);.  
360ac 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
360ad 6c 74 5f 69 6e 74 36 34 28 70 43 6f 6e 74 65 78  lt_int64(pContex
360ae 74 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  t, iRowid);.    
360af 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6c 3d 3d  }else if( iCol==
360b0 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  p->nColumn ){.  
360b1 20 20 20 20 2f 2a 20 54 68 65 20 65 78 74 72 61      /* The extra
360b2 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 6e 61   column whose na
360b3 6d 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  me is the same a
360b4 73 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20  s the table..   
360b5 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20 62     ** Return a b
360b6 6c 6f 62 20 77 68 69 63 68 20 69 73 20 61 20 70  lob which is a p
360b7 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75  ointer to the cu
360b8 72 73 6f 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  rsor..      */. 
360b9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
360ba 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65 78  ult_blob(pContex
360bb 74 2c 20 26 70 43 73 72 2c 20 73 69 7a 65 6f 66  t, &pCsr, sizeof
360bc 28 70 43 73 72 29 2c 20 53 51 4c 49 54 45 5f 54  (pCsr), SQLITE_T
360bd 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d  RANSIENT);.    }
360be 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
360bf 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
360c0 28 70 43 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  (pContext, sqlit
360c1 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
360c2 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f  pCsr->pStmt, iCo
360c3 6c 2b 31 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  l+1));.    }.  }
360c4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
360c5 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./* .** This fun
360c6 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69 6d 70  ction is the imp
360c7 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
360c8 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62  he xUpdate callb
360c9 61 63 6b 20 75 73 65 64 20 62 79 20 0a 2a 2a 20  ack used by .** 
360ca 46 54 53 33 20 76 69 72 74 75 61 6c 20 74 61 62  FTS3 virtual tab
360cb 6c 65 73 2e 20 49 74 20 69 73 20 69 6e 76 6f 6b  les. It is invok
360cc 65 64 20 62 79 20 53 51 4c 69 74 65 20 65 61 63  ed by SQLite eac
360cd 68 20 74 69 6d 65 20 61 20 72 6f 77 20 69 73 20  h time a row is 
360ce 74 6f 20 62 65 0a 2a 2a 20 69 6e 73 65 72 74 65  to be.** inserte
360cf 64 2c 20 75 70 64 61 74 65 64 20 6f 72 20 64 65  d, updated or de
360d0 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  leted..*/.static
360d1 20 69 6e 74 20 66 74 73 33 55 70 64 61 74 65 4d   int fts3UpdateM
360d2 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33  ethod(.  sqlite3
360d3 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 20 20  _vtab *pVtab,   
360d4 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
360d5 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
360d6 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20   */.  int nArg, 
360d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360d8 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
360d9 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20   argument array 
360da 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
360db 75 65 20 2a 2a 61 70 56 61 6c 2c 20 20 20 20 20  ue **apVal,     
360dc 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
360dd 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
360de 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52  sqlite_int64 *pR
360df 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
360e0 2f 2a 20 4f 55 54 3a 20 54 68 65 20 61 66 66 65  /* OUT: The affe
360e1 63 74 65 64 20 28 6f 72 20 65 66 66 65 63 74 65  cted (or effecte
360e2 64 29 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20  d) rowid */.){. 
360e3 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46   return sqlite3F
360e4 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f 64 28  ts3UpdateMethod(
360e5 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 61 70 56  pVtab, nArg, apV
360e6 61 6c 2c 20 70 52 6f 77 69 64 29 3b 0a 7d 0a 0a  al, pRowid);.}..
360e7 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
360e8 74 69 6f 6e 20 6f 66 20 78 53 79 6e 63 28 29 20  tion of xSync() 
360e9 6d 65 74 68 6f 64 2e 20 46 6c 75 73 68 20 74 68  method. Flush th
360ea 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
360eb 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 0a  e pending-terms.
360ec 2a 2a 20 68 61 73 68 2d 74 61 62 6c 65 20 74 6f  ** hash-table to
360ed 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
360ee 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
360ef 33 53 79 6e 63 4d 65 74 68 6f 64 28 73 71 6c 69  3SyncMethod(sqli
360f0 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
360f1 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
360f2 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72  e3Fts3PendingTer
360f3 6d 73 46 6c 75 73 68 28 28 46 74 73 33 54 61 62  msFlush((Fts3Tab
360f4 6c 65 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a  le *)pVtab);.}..
360f5 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
360f6 74 69 6f 6e 20 6f 66 20 78 42 65 67 69 6e 28 29  tion of xBegin()
360f7 20 6d 65 74 68 6f 64 2e 20 54 68 69 73 20 69 73   method. This is
360f8 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
360f9 74 69 63 20 69 6e 74 20 66 74 73 33 42 65 67 69  tic int fts3Begi
360fa 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  nMethod(sqlite3_
360fb 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
360fc 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
360fd 28 70 56 74 61 62 29 3b 0a 20 20 61 73 73 65 72  (pVtab);.  asser
360fe 74 28 20 28 28 46 74 73 33 54 61 62 6c 65 20 2a  t( ((Fts3Table *
360ff 29 70 56 74 61 62 29 2d 3e 6e 50 65 6e 64 69 6e  )pVtab)->nPendin
36100 67 44 61 74 61 3d 3d 30 20 29 3b 0a 20 20 72 65  gData==0 );.  re
36101 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36102 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
36103 6e 74 61 74 69 6f 6e 20 6f 66 20 78 43 6f 6d 6d  ntation of xComm
36104 69 74 28 29 20 6d 65 74 68 6f 64 2e 20 54 68 69  it() method. Thi
36105 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68  s is a no-op. Th
36106 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a  e contents of.**
36107 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72   the pending-ter
36108 6d 73 20 68 61 73 68 2d 74 61 62 6c 65 20 68 61  ms hash-table ha
36109 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
3610a 66 6c 75 73 68 65 64 20 69 6e 74 6f 20 74 68 65  flushed into the
3610b 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 62 79 20   database.** by 
3610c 66 74 73 33 53 79 6e 63 4d 65 74 68 6f 64 28 29  fts3SyncMethod()
3610d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3610e 66 74 73 33 43 6f 6d 6d 69 74 4d 65 74 68 6f 64  fts3CommitMethod
3610f 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
36110 56 74 61 62 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Vtab){.  UNUSED_
36111 50 41 52 41 4d 45 54 45 52 28 70 56 74 61 62 29  PARAMETER(pVtab)
36112 3b 0a 20 20 61 73 73 65 72 74 28 20 28 28 46 74  ;.  assert( ((Ft
36113 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29  s3Table *)pVtab)
36114 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d  ->nPendingData==
36115 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  0 );.  return SQ
36116 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
36117 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
36118 20 6f 66 20 78 52 6f 6c 6c 62 61 63 6b 28 29 2e   of xRollback().
36119 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e   Discard the con
3611a 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 65 6e  tents of the pen
3611b 64 69 6e 67 2d 74 65 72 6d 73 0a 2a 2a 20 68 61  ding-terms.** ha
3611c 73 68 2d 74 61 62 6c 65 2e 20 41 6e 79 20 63 68  sh-table. Any ch
3611d 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68  anges made to th
3611e 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 72  e database are r
3611f 65 76 65 72 74 65 64 20 62 79 20 53 51 4c 69 74  everted by SQLit
36120 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
36121 20 66 74 73 33 52 6f 6c 6c 62 61 63 6b 4d 65 74   fts3RollbackMet
36122 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
36123 20 2a 70 56 74 61 62 29 7b 0a 20 20 73 71 6c 69   *pVtab){.  sqli
36124 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65  te3Fts3PendingTe
36125 72 6d 73 43 6c 65 61 72 28 28 46 74 73 33 54 61  rmsClear((Fts3Ta
36126 62 6c 65 20 2a 29 70 56 74 61 62 29 3b 0a 20 20  ble *)pVtab);.  
36127 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
36128 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65  ;.}../*.** Helpe
36129 72 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  r function used 
3612a 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  by the implement
3612b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 76 65  ation of the ove
3612c 72 6c 6f 61 64 65 64 20 73 6e 69 70 70 65 74 28  rloaded snippet(
3612d 29 2c 0a 2a 2a 20 6f 66 66 73 65 74 73 28 29 20  ),.** offsets() 
3612e 61 6e 64 20 6f 70 74 69 6d 69 7a 65 28 29 20 53  and optimize() S
3612f 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  QL functions..**
36130 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
36131 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
36132 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73  hird argument is
36133 20 61 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 0a   a blob of size.
36134 2a 2a 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75  ** sizeof(Fts3Cu
36135 72 73 6f 72 2a 29 2c 20 74 68 65 6e 20 74 68 65  rsor*), then the
36136 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 73 20 61   blob contents a
36137 72 65 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  re copied to the
36138 20 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69   .** output vari
36139 61 62 6c 65 20 2a 70 70 43 73 72 20 61 6e 64 20  able *ppCsr and 
3613a 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
3613b 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
3613c 2c 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6d 65  , an error.** me
3613d 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e  ssage is written
3613e 20 74 6f 20 63 6f 6e 74 65 78 74 20 70 43 6f 6e   to context pCon
3613f 74 65 78 74 20 61 6e 64 20 53 51 4c 49 54 45 5f  text and SQLITE_
36140 45 52 52 4f 52 20 72 65 74 75 72 6e 65 64 2e 20  ERROR returned. 
36141 54 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 70 61  The.** string pa
36142 73 73 65 64 20 76 69 61 20 7a 46 75 6e 63 20 69  ssed via zFunc i
36143 73 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  s used as part o
36144 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
36145 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  age..*/.static i
36146 6e 74 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41  nt fts3FunctionA
36147 72 67 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  rg(.  sqlite3_co
36148 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
36149 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 66 75 6e        /* SQL fun
3614a 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65  ction call conte
3614b 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
3614c 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 20 20 20  ar *zFunc,      
3614d 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74          /* Funct
3614e 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71  ion name */.  sq
3614f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
36150 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
36151 20 61 72 67 76 5b 30 5d 20 70 61 73 73 65 64 20   argv[0] passed 
36152 74 6f 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  to function */. 
36153 20 46 74 73 33 43 75 72 73 6f 72 20 2a 2a 70 70   Fts3Cursor **pp
36154 43 73 72 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Csr         /* O
36155 55 54 3a 20 53 74 6f 72 65 20 63 75 72 73 6f 72  UT: Store cursor
36156 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a   handle here */.
36157 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  ){.  Fts3Cursor 
36158 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 73 71 6c  *pRet;.  if( sql
36159 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
3615a 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 42 4c  pVal)!=SQLITE_BL
3615b 4f 42 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65  OB .   && sqlite
3615c 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 70 56  3_value_bytes(pV
3615d 61 6c 29 21 3d 73 69 7a 65 6f 66 28 46 74 73 33  al)!=sizeof(Fts3
3615e 43 75 72 73 6f 72 20 2a 29 0a 20 20 29 7b 0a 20  Cursor *).  ){. 
3615f 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
36160 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36161 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
36162 72 67 75 6d 65 6e 74 20 74 6f 20 25 73 22 2c 20  rgument to %s", 
36163 7a 46 75 6e 63 29 3b 0a 20 20 20 20 73 71 6c 69  zFunc);.    sqli
36164 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
36165 28 70 43 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c  (pContext, zErr,
36166 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
36167 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20  3_free(zErr);.  
36168 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
36169 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 6d 65 6d  ERROR;.  }.  mem
3616a 63 70 79 28 26 70 52 65 74 2c 20 73 71 6c 69 74  cpy(&pRet, sqlit
3616b 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 70 56  e3_value_blob(pV
3616c 61 6c 29 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  al), sizeof(Fts3
3616d 43 75 72 73 6f 72 20 2a 29 29 3b 0a 20 20 2a 70  Cursor *));.  *p
3616e 70 43 73 72 20 3d 20 70 52 65 74 3b 0a 20 20 72  pCsr = pRet;.  r
3616f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36170 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
36171 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
36172 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63 74 69  snippet() functi
36173 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a 73  on for FTS3.*/.s
36174 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 53  tatic void fts3S
36175 6e 69 70 70 65 74 46 75 6e 63 28 0a 20 20 73 71  nippetFunc(.  sq
36176 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
36177 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  Context,.  int a
36178 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
36179 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
3617a 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
3617b 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
3617c 20 2f 2a 20 43 75 72 73 6f 72 20 68 61 6e 64 6c   /* Cursor handl
3617d 65 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  e passed through
3617e 20 61 70 56 61 6c 5b 30 5d 20 2a 2f 0a 20 20 63   apVal[0] */.  c
3617f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
36180 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 63 6f 6e  t = "<b>";.  con
36181 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20  st char *zEnd = 
36182 22 3c 2f 62 3e 22 3b 0a 20 20 63 6f 6e 73 74 20  "</b>";.  const 
36183 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20  char *zEllipsis 
36184 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a  = "<b>...</b>";.
36185 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c 7c  .  if( argc<1 ||
36186 20 61 72 67 63 3e 34 20 29 20 72 65 74 75 72 6e   argc>4 ) return
36187 3b 0a 20 20 69 66 28 20 66 74 73 33 46 75 6e 63  ;.  if( fts3Func
36188 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78 74  tionArg(pContext
36189 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 61 72 67  , "snippet", arg
3618a 76 5b 30 5d 2c 20 26 70 43 73 72 29 20 29 20 72  v[0], &pCsr) ) r
3618b 65 74 75 72 6e 3b 0a 0a 20 20 73 77 69 74 63 68  eturn;..  switch
3618c 28 20 61 72 67 63 20 29 7b 0a 20 20 20 20 63 61  ( argc ){.    ca
3618d 73 65 20 34 3a 20 7a 45 6c 6c 69 70 73 69 73 20  se 4: zEllipsis 
3618e 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
3618f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
36190 74 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20  t(argv[3]);.    
36191 63 61 73 65 20 33 3a 20 7a 45 6e 64 20 3d 20 28  case 3: zEnd = (
36192 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
36193 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
36194 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 63 61 73  rgv[2]);.    cas
36195 65 20 32 3a 20 7a 53 74 61 72 74 20 3d 20 28 63  e 2: zStart = (c
36196 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
36197 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
36198 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 73  gv[1]);.  }..  s
36199 71 6c 69 74 65 33 46 74 73 33 53 6e 69 70 70 65  qlite3Fts3Snippe
3619a 74 28 70 43 6f 6e 74 65 78 74 2c 20 70 43 73 72  t(pContext, pCsr
3619b 2c 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20  , zStart, zEnd, 
3619c 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 7d 0a 0a 2f  zEllipsis);.}../
3619d 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3619e 69 6f 6e 20 6f 66 20 74 68 65 20 6f 66 66 73 65  ion of the offse
3619f 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ts() function fo
361a0 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63  r FTS3.*/.static
361a1 20 76 6f 69 64 20 66 74 73 33 4f 66 66 73 65 74   void fts3Offset
361a2 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  sFunc(.  sqlite3
361a3 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
361a4 78 74 2c 20 20 20 20 20 20 2f 2a 20 53 51 4c 69  xt,      /* SQLi
361a5 74 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  te function call
361a6 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
361a7 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20 20 20  t nVal,         
361a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
361a9 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e   Size of argumen
361aa 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c  t array */.  sql
361ab 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
361ac 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  al           /* 
361ad 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d 65 6e  Array of argumen
361ae 74 73 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 43  ts */.){.  Fts3C
361af 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20  ursor *pCsr;    
361b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
361b1 72 73 6f 72 20 68 61 6e 64 6c 65 20 70 61 73 73  rsor handle pass
361b2 65 64 20 74 68 72 6f 75 67 68 20 61 70 56 61 6c  ed through apVal
361b3 5b 30 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  [0] */..  UNUSED
361b4 5f 50 41 52 41 4d 45 54 45 52 28 6e 56 61 6c 29  _PARAMETER(nVal)
361b5 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 56 61  ;..  assert( nVa
361b6 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 66 74  l==1 );.  if( ft
361b7 73 33 46 75 6e 63 74 69 6f 6e 41 72 67 28 70 43  s3FunctionArg(pC
361b8 6f 6e 74 65 78 74 2c 20 22 6f 66 66 73 65 74 73  ontext, "offsets
361b9 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20 26 70 43  ", apVal[0], &pC
361ba 73 72 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  sr) ) return;.  
361bb 61 73 73 65 72 74 28 20 70 43 73 72 20 29 3b 0a  assert( pCsr );.
361bc 20 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66 66    sqlite3Fts3Off
361bd 73 65 74 73 28 70 43 6f 6e 74 65 78 74 2c 20 70  sets(pContext, p
361be 43 73 72 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  Csr);.}../* .** 
361bf 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
361c0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f 70  f the special op
361c1 74 69 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f  timize() functio
361c2 6e 20 66 6f 72 20 46 54 53 33 2e 20 54 68 69 73  n for FTS3. This
361c3 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 65   .** function me
361c4 72 67 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74  rges all segment
361c5 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
361c6 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65  e to a single se
361c7 67 6d 65 6e 74 2e 0a 2a 2a 20 45 78 61 6d 70 6c  gment..** Exampl
361c8 65 20 75 73 61 67 65 20 69 73 3a 0a 2a 2a 0a 2a  e usage is:.**.*
361c9 2a 20 20 20 53 45 4c 45 43 54 20 6f 70 74 69 6d  *   SELECT optim
361ca 69 7a 65 28 74 29 20 46 52 4f 4d 20 74 20 4c 49  ize(t) FROM t LI
361cb 4d 49 54 20 31 3b 0a 2a 2a 0a 2a 2a 20 77 68 65  MIT 1;.**.** whe
361cc 72 65 20 27 74 27 20 69 73 20 74 68 65 20 6e 61  re 't' is the na
361cd 6d 65 20 6f 66 20 61 6e 20 46 54 53 33 20 74 61  me of an FTS3 ta
361ce 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
361cf 6f 69 64 20 66 74 73 33 4f 70 74 69 6d 69 7a 65  oid fts3Optimize
361d0 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
361d1 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
361d2 74 2c 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74  t,      /* SQLit
361d3 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20  e function call 
361d4 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
361d5 20 6e 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20   nVal,          
361d6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
361d7 53 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e 74  Size of argument
361d8 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c 69   array */.  sqli
361d9 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
361da 6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  l           /* A
361db 72 72 61 79 20 6f 66 20 61 72 67 75 6d 65 6e 74  rray of argument
361dc 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  s */.){.  int rc
361dd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
361de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
361df 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 46 74  urn code */.  Ft
361e0 73 33 54 61 62 6c 65 20 2a 70 3b 20 20 20 20 20  s3Table *p;     
361e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
361e2 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68   Virtual table h
361e3 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 43  andle */.  Fts3C
361e4 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20  ursor *pCursor; 
361e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
361e6 72 73 6f 72 20 68 61 6e 64 6c 65 20 70 61 73 73  rsor handle pass
361e7 65 64 20 74 68 72 6f 75 67 68 20 61 70 56 61 6c  ed through apVal
361e8 5b 30 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  [0] */..  UNUSED
361e9 5f 50 41 52 41 4d 45 54 45 52 28 6e 56 61 6c 29  _PARAMETER(nVal)
361ea 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 56 61  ;..  assert( nVa
361eb 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 66 74  l==1 );.  if( ft
361ec 73 33 46 75 6e 63 74 69 6f 6e 41 72 67 28 70 43  s3FunctionArg(pC
361ed 6f 6e 74 65 78 74 2c 20 22 6f 70 74 69 6d 69 7a  ontext, "optimiz
361ee 65 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20 26 70  e", apVal[0], &p
361ef 43 75 72 73 6f 72 29 20 29 20 72 65 74 75 72 6e  Cursor) ) return
361f0 3b 0a 20 20 70 20 3d 20 28 46 74 73 33 54 61 62  ;.  p = (Fts3Tab
361f1 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d 3e 62 61  le *)pCursor->ba
361f2 73 65 2e 70 56 74 61 62 3b 0a 20 20 61 73 73 65  se.pVtab;.  asse
361f3 72 74 28 20 70 20 29 3b 0a 0a 20 20 72 63 20 3d  rt( p );..  rc =
361f4 20 73 71 6c 69 74 65 33 46 74 73 33 4f 70 74 69   sqlite3Fts3Opti
361f5 6d 69 7a 65 28 70 29 3b 0a 0a 20 20 73 77 69 74  mize(p);..  swit
361f6 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20 63 61  ch( rc ){.    ca
361f7 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a 0a 20 20  se SQLITE_OK:.  
361f8 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
361f9 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
361fa 2c 20 22 49 6e 64 65 78 20 6f 70 74 69 6d 69 7a  , "Index optimiz
361fb 65 64 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ed", -1, SQLITE_
361fc 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 62  STATIC);.      b
361fd 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
361fe 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20 20 20  QLITE_DONE:.    
361ff 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
36200 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20  _text(pContext, 
36201 22 49 6e 64 65 78 20 61 6c 72 65 61 64 79 20 6f  "Index already o
36202 70 74 69 6d 61 6c 22 2c 20 2d 31 2c 20 53 51 4c  ptimal", -1, SQL
36203 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
36204 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
36205 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 73 71 6c  fault:.      sql
36206 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
36207 72 5f 63 6f 64 65 28 70 43 6f 6e 74 65 78 74 2c  r_code(pContext,
36208 20 72 63 29 3b 0a 20 20 20 20 20 20 62 72 65 61   rc);.      brea
36209 6b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  k;.  }.}../*.** 
3620a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
3620b 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46 69 6e  lements the xFin
3620c 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64  dFunction method
3620d 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a 2a 2a   for the FTS3.**
3620e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
3620f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
36210 73 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 4d 65  s3FindFunctionMe
36211 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  thod(.  sqlite3_
36212 76 74 61 62 20 2a 70 56 74 61 62 2c 20 20 20 20  vtab *pVtab,    
36213 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75          /* Virtu
36214 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20  al table handle 
36215 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
36216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36217 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
36218 66 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61  f SQL function a
36219 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 63 6f  rguments */.  co
3621a 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3621b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3621c 20 4e 61 6d 65 20 6f 66 20 53 51 4c 20 66 75 6e   Name of SQL fun
3621d 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
3621e 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74  (**pxFunc)(sqlit
3621f 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
36220 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
36221 2c 20 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74  , /* OUT: Result
36222 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 70 70 41   */.  void **ppA
36223 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
36224 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20        /* Unused 
36225 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 4f  */.){.  struct O
36226 76 65 72 6c 6f 61 64 65 64 20 7b 0a 20 20 20 20  verloaded {.    
36227 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
36228 65 3b 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46  e;.    void (*xF
36229 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
3622a 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
3622b 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20  3_value**);.  } 
3622c 61 4f 76 65 72 6c 6f 61 64 5b 5d 20 3d 20 7b 0a  aOverload[] = {.
3622d 20 20 20 20 7b 20 22 73 6e 69 70 70 65 74 22 2c      { "snippet",
3622e 20 66 74 73 33 53 6e 69 70 70 65 74 46 75 6e 63   fts3SnippetFunc
3622f 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 66 66 73 65   },.    { "offse
36230 74 73 22 2c 20 66 74 73 33 4f 66 66 73 65 74 73  ts", fts3Offsets
36231 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 6f  Func },.    { "o
36232 70 74 69 6d 69 7a 65 22 2c 20 66 74 73 33 4f 70  ptimize", fts3Op
36233 74 69 6d 69 7a 65 46 75 6e 63 20 7d 2c 0a 20 20  timizeFunc },.  
36234 7d 3b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  };.  int i;     
36235 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36236 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
36237 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20   variable */..  
36238 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
36239 28 70 56 74 61 62 29 3b 0a 20 20 55 4e 55 53 45  (pVtab);.  UNUSE
3623a 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 41 72 67  D_PARAMETER(nArg
3623b 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
3623c 4d 45 54 45 52 28 70 70 41 72 67 29 3b 0a 0a 20  METER(ppArg);.. 
3623d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 69 7a 65   for(i=0; i<Size
3623e 6f 66 41 72 72 61 79 28 61 4f 76 65 72 6c 6f 61  ofArray(aOverloa
3623f 64 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  d); i++){.    if
36240 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20  ( strcmp(zName, 
36241 61 4f 76 65 72 6c 6f 61 64 5b 69 5d 2e 7a 4e 61  aOverload[i].zNa
36242 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
36243 2a 70 78 46 75 6e 63 20 3d 20 61 4f 76 65 72 6c  *pxFunc = aOverl
36244 6f 61 64 5b 69 5d 2e 78 46 75 6e 63 3b 0a 20 20  oad[i].xFunc;.  
36245 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
36246 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f    }.  }..  /* No
36247 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74 68 65   function of the
36248 20 73 70 65 63 69 66 69 65 64 20 6e 61 6d 65 20   specified name 
36249 77 61 73 20 66 6f 75 6e 64 2e 20 52 65 74 75 72  was found. Retur
3624a 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  n 0. */.  return
3624b 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70   0;.}../*.** Imp
3624c 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 46  lementation of F
3624d 54 53 33 20 78 52 65 6e 61 6d 65 20 6d 65 74 68  TS3 xRename meth
3624e 6f 64 2e 20 52 65 6e 61 6d 65 20 61 6e 20 66 74  od. Rename an ft
3624f 73 33 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  s3 table..*/.sta
36250 74 69 63 20 69 6e 74 20 66 74 73 33 52 65 6e 61  tic int fts3Rena
36251 6d 65 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69  meMethod(.  sqli
36252 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
36253 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
36254 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e  irtual table han
36255 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
36256 68 61 72 20 2a 7a 4e 61 6d 65 20 20 20 20 20 20  har *zName      
36257 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20           /* New 
36258 6e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f  name of table */
36259 0a 29 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20  .){.  Fts3Table 
3625a 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20  *p = (Fts3Table 
3625b 2a 29 70 56 74 61 62 3b 20 20 20 20 20 0a 20 20  *)pVtab;     .  
3625c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3625d 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20  NOMEM;          
3625e 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
3625f 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
36260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36261 20 20 20 20 2f 2a 20 53 51 4c 20 73 63 72 69 70      /* SQL scrip
36262 74 20 74 6f 20 72 75 6e 20 74 6f 20 72 65 6e 61  t to run to rena
36263 6d 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 0a 20  me tables */. . 
36264 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
36265 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22 41 4c  mprintf(.    "AL
36266 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
36267 5f 63 6f 6e 74 65 6e 74 27 20 20 52 45 4e 41 4d  _content'  RENAM
36268 45 20 54 4f 20 27 25 71 5f 63 6f 6e 74 65 6e 74  E TO '%q_content
36269 27 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54  ';".    "ALTER T
3626a 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67 6d  ABLE %Q.'%q_segm
3626b 65 6e 74 73 27 20 52 45 4e 41 4d 45 20 54 4f 20  ents' RENAME TO 
3626c 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 3b 22 0a  '%q_segments';".
3626d 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45      "ALTER TABLE
3626e 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20   %Q.'%q_segdir' 
3626f 20 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f    RENAME TO '%q_
36270 73 65 67 64 69 72 27 3b 22 0a 20 20 20 20 2c 20  segdir';".    , 
36271 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  p->zDb, p->zName
36272 2c 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70  , zName .    , p
36273 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
36274 20 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 2d   zName .    , p-
36275 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, p->zName, 
36276 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28  zName.  );.  if(
36277 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20   zSql ){.    rc 
36278 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70  = sqlite3_exec(p
36279 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30  ->db, zSql, 0, 0
3627a 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
3627b 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
3627c 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
3627d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  ..static const s
3627e 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74  qlite3_module ft
3627f 73 33 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 2f  s3Module = {.  /
36280 2a 20 69 56 65 72 73 69 6f 6e 20 20 20 20 20 20  * iVersion      
36281 2a 2f 20 30 2c 0a 20 20 2f 2a 20 78 43 72 65 61  */ 0,.  /* xCrea
36282 74 65 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33  te       */ fts3
36283 43 72 65 61 74 65 4d 65 74 68 6f 64 2c 0a 20 20  CreateMethod,.  
36284 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20 20 20 20  /* xConnect     
36285 20 2a 2f 20 66 74 73 33 43 6f 6e 6e 65 63 74 4d   */ fts3ConnectM
36286 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 42 65 73  ethod,.  /* xBes
36287 74 49 6e 64 65 78 20 20 20 20 2a 2f 20 66 74 73  tIndex    */ fts
36288 33 42 65 73 74 49 6e 64 65 78 4d 65 74 68 6f 64  3BestIndexMethod
36289 2c 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65  ,.  /* xDisconne
3628a 63 74 20 20 20 2a 2f 20 66 74 73 33 44 69 73 63  ct   */ fts3Disc
3628b 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 2c 0a 20 20  onnectMethod,.  
3628c 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20 20 20  /* xDestroy     
3628d 20 2a 2f 20 66 74 73 33 44 65 73 74 72 6f 79 4d   */ fts3DestroyM
3628e 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 4f 70 65  ethod,.  /* xOpe
3628f 6e 20 20 20 20 20 20 20 20 20 2a 2f 20 66 74 73  n         */ fts
36290 33 4f 70 65 6e 4d 65 74 68 6f 64 2c 0a 20 20 2f  3OpenMethod,.  /
36291 2a 20 78 43 6c 6f 73 65 20 20 20 20 20 20 20 20  * xClose        
36292 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65  */ fulltextClose
36293 2c 0a 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 20  ,.  /* xFilter  
36294 20 20 20 20 20 2a 2f 20 66 74 73 33 46 69 6c 74       */ fts3Filt
36295 65 72 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78  erMethod,.  /* x
36296 4e 65 78 74 20 20 20 20 20 20 20 20 20 2a 2f 20  Next         */ 
36297 66 74 73 33 4e 65 78 74 4d 65 74 68 6f 64 2c 0a  fts3NextMethod,.
36298 20 20 2f 2a 20 78 45 6f 66 20 20 20 20 20 20 20    /* xEof       
36299 20 20 20 2a 2f 20 66 74 73 33 45 6f 66 4d 65 74     */ fts3EofMet
3629a 68 6f 64 2c 0a 20 20 2f 2a 20 78 43 6f 6c 75 6d  hod,.  /* xColum
3629b 6e 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 43  n       */ fts3C
3629c 6f 6c 75 6d 6e 4d 65 74 68 6f 64 2c 0a 20 20 2f  olumnMethod,.  /
3629d 2a 20 78 52 6f 77 69 64 20 20 20 20 20 20 20 20  * xRowid        
3629e 2a 2f 20 66 74 73 33 52 6f 77 69 64 4d 65 74 68  */ fts3RowidMeth
3629f 6f 64 2c 0a 20 20 2f 2a 20 78 55 70 64 61 74 65  od,.  /* xUpdate
362a0 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 55 70         */ fts3Up
362a1 64 61 74 65 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a  dateMethod,.  /*
362a2 20 78 42 65 67 69 6e 20 20 20 20 20 20 20 20 2a   xBegin        *
362a3 2f 20 66 74 73 33 42 65 67 69 6e 4d 65 74 68 6f  / fts3BeginMetho
362a4 64 2c 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20  d,.  /* xSync   
362a5 20 20 20 20 20 20 2a 2f 20 66 74 73 33 53 79 6e        */ fts3Syn
362a6 63 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 43  cMethod,.  /* xC
362a7 6f 6d 6d 69 74 20 20 20 20 20 20 20 2a 2f 20 66  ommit       */ f
362a8 74 73 33 43 6f 6d 6d 69 74 4d 65 74 68 6f 64 2c  ts3CommitMethod,
362a9 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20  .  /* xRollback 
362aa 20 20 20 20 2a 2f 20 66 74 73 33 52 6f 6c 6c 62      */ fts3Rollb
362ab 61 63 6b 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20  ackMethod,.  /* 
362ac 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2a 2f  xFindFunction */
362ad 20 66 74 73 33 46 69 6e 64 46 75 6e 63 74 69 6f   fts3FindFunctio
362ae 6e 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 52  nMethod,.  /* xR
362af 65 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 66  ename */       f
362b0 74 73 33 52 65 6e 61 6d 65 4d 65 74 68 6f 64 2c  ts3RenameMethod,
362b1 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .};../*.** This 
362b2 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69  function is regi
362b3 73 74 65 72 65 64 20 61 73 20 74 68 65 20 6d 6f  stered as the mo
362b4 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20  dule destructor 
362b5 28 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 0a  (called when an.
362b6 2a 2a 20 46 54 53 33 20 65 6e 61 62 6c 65 64 20  ** FTS3 enabled 
362b7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
362b8 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 29 2e 20  ion is closed). 
362b9 49 74 20 66 72 65 65 73 20 74 68 65 20 6d 65 6d  It frees the mem
362ba 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  ory.** allocated
362bb 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 69 7a   for the tokeniz
362bc 65 72 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  er hash table..*
362bd 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 61  /.static void ha
362be 73 68 44 65 73 74 72 6f 79 28 76 6f 69 64 20 2a  shDestroy(void *
362bf 70 29 7b 0a 20 20 46 74 73 33 48 61 73 68 20 2a  p){.  Fts3Hash *
362c0 70 48 61 73 68 20 3d 20 28 46 74 73 33 48 61 73  pHash = (Fts3Has
362c1 68 20 2a 29 70 3b 0a 20 20 73 71 6c 69 74 65 33  h *)p;.  sqlite3
362c2 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 70 48  Fts3HashClear(pH
362c3 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ash);.  sqlite3_
362c4 66 72 65 65 28 70 48 61 73 68 29 3b 0a 7d 0a 0a  free(pHash);.}..
362c5 2f 2a 0a 2a 2a 20 54 68 65 20 66 74 73 33 20 62  /*.** The fts3 b
362c6 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65  uilt-in tokenize
362c7 72 73 20 2d 20 22 73 69 6d 70 6c 65 22 20 61 6e  rs - "simple" an
362c8 64 20 22 70 6f 72 74 65 72 22 20 2d 20 61 72 65  d "porter" - are
362c9 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20   implemented.** 
362ca 69 6e 20 66 69 6c 65 73 20 66 74 73 33 5f 74 6f  in files fts3_to
362cb 6b 65 6e 69 7a 65 72 31 2e 63 20 61 6e 64 20 66  kenizer1.c and f
362cc 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 72 65 73  ts3_porter.c res
362cd 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 20 66  pectively. The f
362ce 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20  ollowing.** two 
362cf 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  forward declarat
362d0 69 6f 6e 73 20 61 72 65 20 66 6f 72 20 66 75 6e  ions are for fun
362d1 63 74 69 6f 6e 73 20 64 65 63 6c 61 72 65 64 20  ctions declared 
362d2 69 6e 20 74 68 65 73 65 20 66 69 6c 65 73 0a 2a  in these files.*
362d3 2a 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  * used to retrie
362d4 76 65 20 74 68 65 20 72 65 73 70 65 63 74 69 76  ve the respectiv
362d5 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
362d6 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67  s..**.** Calling
362d7 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
362d8 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
362d9 65 28 29 20 73 65 74 73 20 74 68 65 20 76 61 6c  e() sets the val
362da 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  ue pointed.** to
362db 20 62 79 20 74 68 65 20 61 72 67 75 6d 65 6e 74   by the argument
362dc 20 74 6f 20 70 6f 69 6e 74 20 61 20 74 68 65 20   to point a the 
362dd 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a  "simple" tokeniz
362de 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  er implementatio
362df 6e 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 2e  n..** Function .
362e0 2e 2e 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65  ..PorterTokenize
362e1 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73 20 2a  rModule() sets *
362e2 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74  pModule to point
362e3 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 72 74 65   to the.** porte
362e4 72 20 74 6f 6b 65 6e 69 7a 65 72 2f 73 74 65 6d  r tokenizer/stem
362e5 6d 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  mer implementati
362e6 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
362e7 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
362e8 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
362e9 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
362ea 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
362eb 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
362ec 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dule);.SQLITE_PR
362ed 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
362ee 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65  e3Fts3PorterToke
362ef 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69  nizerModule(sqli
362f0 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
362f1 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
362f2 64 75 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dule);.SQLITE_PR
362f3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
362f4 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a  e3Fts3IcuTokeniz
362f5 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33  erModule(sqlite3
362f6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
362f7 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
362f8 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  e);../*.** Initi
362f9 61 6c 69 73 65 20 74 68 65 20 66 74 73 33 20 65  alise the fts3 e
362fa 78 74 65 6e 73 69 6f 6e 2e 20 49 66 20 74 68 69  xtension. If thi
362fb 73 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 62  s extension is b
362fc 75 69 6c 74 20 61 73 20 70 61 72 74 0a 2a 2a 20  uilt as part.** 
362fd 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 6c 69  of the sqlite li
362fe 62 72 61 72 79 2c 20 74 68 65 6e 20 74 68 69 73  brary, then this
362ff 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
36300 6c 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 0a  led directly by.
36301 2a 2a 20 53 51 4c 69 74 65 2e 20 49 66 20 66 74  ** SQLite. If ft
36302 73 33 20 69 73 20 62 75 69 6c 74 20 61 73 20 61  s3 is built as a
36303 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61   dynamically loa
36304 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2c  dable extension,
36305 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
36306 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
36307 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  he sqlite3_exten
36308 73 69 6f 6e 5f 69 6e 69 74 28 29 20 65 6e 74 72  sion_init() entr
36309 79 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  y point..*/.SQLI
3630a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3630b 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 73  qlite3Fts3Init(s
3630c 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
3630d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3630e 4b 3b 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70  K;.  Fts3Hash *p
3630f 48 61 73 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  Hash = 0;.  cons
36310 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
36311 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 53 69 6d  zer_module *pSim
36312 70 6c 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  ple = 0;.  const
36313 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36314 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 50 6f 72 74  er_module *pPort
36315 65 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  er = 0;.  const 
36316 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
36317 72 5f 6d 6f 64 75 6c 65 20 2a 70 49 63 75 20 3d  r_module *pIcu =
36318 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 46 74   0;..  sqlite3Ft
36319 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  s3SimpleTokenize
3631a 72 4d 6f 64 75 6c 65 28 26 70 53 69 6d 70 6c 65  rModule(&pSimple
3631b 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  );.  sqlite3Fts3
3631c 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  PorterTokenizerM
3631d 6f 64 75 6c 65 28 26 70 50 6f 72 74 65 72 29 3b  odule(&pPorter);
3631e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
3631f 4e 41 42 4c 45 5f 49 43 55 0a 20 20 73 71 6c 69  NABLE_ICU.  sqli
36320 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e 69  te3Fts3IcuTokeni
36321 7a 65 72 4d 6f 64 75 6c 65 28 26 70 49 63 75 29  zerModule(&pIcu)
36322 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  ;.#endif..  /* A
36323 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74  llocate and init
36324 69 61 6c 69 73 65 20 74 68 65 20 68 61 73 68 2d  ialise the hash-
36325 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73 74  table used to st
36326 6f 72 65 20 74 6f 6b 65 6e 69 7a 65 72 73 2e 20  ore tokenizers. 
36327 2a 2f 0a 20 20 70 48 61 73 68 20 3d 20 73 71 6c  */.  pHash = sql
36328 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
36329 6f 66 28 46 74 73 33 48 61 73 68 29 29 3b 0a 20  of(Fts3Hash));. 
3632a 20 69 66 28 20 21 70 48 61 73 68 20 29 7b 0a 20   if( !pHash ){. 
3632b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
3632c 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
3632d 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
3632e 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54  shInit(pHash, FT
3632f 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
36330 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  1);.  }..  /* Lo
36331 61 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  ad the built-in 
36332 74 6f 6b 65 6e 69 7a 65 72 73 20 69 6e 74 6f 20  tokenizers into 
36333 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
36334 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
36335 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
36336 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36337 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 73  Insert(pHash, "s
36338 69 6d 70 6c 65 22 2c 20 37 2c 20 28 76 6f 69 64  imple", 7, (void
36339 20 2a 29 70 53 69 6d 70 6c 65 29 0a 20 20 20 20   *)pSimple).    
3633a 20 7c 7c 20 73 71 6c 69 74 65 33 46 74 73 33 48   || sqlite3Fts3H
3633b 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c  ashInsert(pHash,
3633c 20 22 70 6f 72 74 65 72 22 2c 20 37 2c 20 28 76   "porter", 7, (v
3633d 6f 69 64 20 2a 29 70 50 6f 72 74 65 72 29 20 0a  oid *)pPorter) .
3633e 20 20 20 20 20 7c 7c 20 28 70 49 63 75 20 26 26       || (pIcu &&
3633f 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36340 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 69  Insert(pHash, "i
36341 63 75 22 2c 20 34 2c 20 28 76 6f 69 64 20 2a 29  cu", 4, (void *)
36342 70 49 63 75 29 29 0a 20 20 20 20 29 7b 0a 20 20  pIcu)).    ){.  
36343 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
36344 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
36345 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
36346 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 46 74  TEST.  sqlite3Ft
36347 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49 6e  s3ExprInitTestIn
36348 74 65 72 66 61 63 65 28 64 62 29 3b 0a 23 65 6e  terface(db);.#en
36349 64 69 66 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65  dif..  /* Create
3634a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
3634b 6c 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e  le wrapper aroun
3634c 64 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  d the hash-table
3634d 20 61 6e 64 20 6f 76 65 72 6c 6f 61 64 20 0a 20   and overload . 
3634e 20 2a 2a 20 74 68 65 20 74 77 6f 20 73 63 61 6c   ** the two scal
3634f 61 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66  ar functions. If
36350 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73   this is success
36351 66 75 6c 2c 20 72 65 67 69 73 74 65 72 20 74 68  ful, register th
36352 65 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 77 69  e.  ** module wi
36353 74 68 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a  th sqlite..  */.
36354 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
36355 3d 72 63 20 0a 20 20 20 26 26 20 53 51 4c 49 54  =rc .   && SQLIT
36356 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
36357 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54  te3Fts3InitHashT
36358 61 62 6c 65 28 64 62 2c 20 70 48 61 73 68 2c 20  able(db, pHash, 
36359 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 22  "fts3_tokenizer"
3635a 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
3635b 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
3635c 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
3635d 69 6f 6e 28 64 62 2c 20 22 73 6e 69 70 70 65 74  ion(db, "snippet
3635e 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51  ", -1)).   && SQ
3635f 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
36360 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
36361 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f 66  function(db, "of
36362 66 73 65 74 73 22 2c 20 31 29 29 0a 20 20 20 26  fsets", 1)).   &
36363 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
36364 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c   = sqlite3_overl
36365 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  oad_function(db,
36366 20 22 6f 70 74 69 6d 69 7a 65 22 2c 20 31 29 29   "optimize", 1))
36367 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
36368 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
36369 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 20 20 20  module_v2(.     
3636a 20 20 20 64 62 2c 20 22 66 74 73 33 22 2c 20 26     db, "fts3", &
3636b 66 74 73 33 4d 6f 64 75 6c 65 2c 20 28 76 6f 69  fts3Module, (voi
3636c 64 20 2a 29 70 48 61 73 68 2c 20 68 61 73 68 44  d *)pHash, hashD
3636d 65 73 74 72 6f 79 0a 20 20 20 20 29 3b 0a 20 20  estroy.    );.  
3636e 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72  }..  /* An error
3636f 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 44   has occurred. D
36370 65 6c 65 74 65 20 74 68 65 20 68 61 73 68 20 74  elete the hash t
36371 61 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20  able and return 
36372 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  the error code. 
36373 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  */.  assert( rc!
36374 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
36375 69 66 28 20 70 48 61 73 68 20 29 7b 0a 20 20 20  if( pHash ){.   
36376 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36377 43 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20  Clear(pHash);.  
36378 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
36379 48 61 73 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Hash);.  }.  ret
3637a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
3637b 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49  SQLITE_CORE.SQLI
3637c 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3637d 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
3637e 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
3637f 2c 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  , .  char **pzEr
36380 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71  rMsg,.  const sq
36381 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
36382 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51  es *pApi.){.  SQ
36383 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
36384 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74  NIT2(pApi).  ret
36385 75 72 6e 20 73 71 6c 69 74 65 33 46 74 73 33 49  urn sqlite3Fts3I
36386 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69  nit(db);.}.#endi
36387 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
36388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
36389 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fts3.c *******
3638a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3638b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3638c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
3638d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
3638e 20 66 69 6c 65 20 66 74 73 33 5f 65 78 70 72 2e   file fts3_expr.
3638f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
36390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36391 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
36392 20 32 30 30 38 20 4e 6f 76 20 32 38 0a 2a 2a 0a   2008 Nov 28.**.
36393 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
36394 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
36395 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
36396 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
36397 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
36398 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
36399 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
3639a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
3639b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
3639c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
3639d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
3639e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
3639f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
363a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
363a1 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
363a2 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
363a3 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
363a4 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
363a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
363a9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
363aa 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
363ab 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
363ac 20 61 20 70 61 72 73 65 72 20 66 6f 72 20 66 74   a parser for ft
363ad 73 33 20 71 75 65 72 79 20 73 74 72 69 6e 67 73  s3 query strings
363ae 0a 2a 2a 20 28 74 68 65 20 72 69 67 68 74 2d 68  .** (the right-h
363af 61 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  and argument to 
363b0 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
363b1 6f 72 29 2e 20 42 65 63 61 75 73 65 20 74 68 65  or). Because the
363b2 20 73 75 70 70 6f 72 74 65 64 20 0a 2a 2a 20 73   supported .** s
363b3 79 6e 74 61 78 20 69 73 20 72 65 6c 61 74 69 76  yntax is relativ
363b4 65 6c 79 20 73 69 6d 70 6c 65 2c 20 74 68 65 20  ely simple, the 
363b5 77 68 6f 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 2f  whole tokenizer/
363b6 70 61 72 73 65 72 20 73 79 73 74 65 6d 20 69 73  parser system is
363b7 0a 2a 2a 20 68 61 6e 64 2d 63 6f 64 65 64 2e 20  .** hand-coded. 
363b8 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
363b9 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
363ba 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
363bb 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a  ENABLE_FTS3)../*
363bc 0a 2a 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20  .** By default, 
363bd 74 68 69 73 20 6d 6f 64 75 6c 65 20 70 61 72 73  this module pars
363be 65 73 20 74 68 65 20 6c 65 67 61 63 79 20 73 79  es the legacy sy
363bf 6e 74 61 78 20 74 68 61 74 20 68 61 73 20 62 65  ntax that has be
363c0 65 6e 20 0a 2a 2a 20 74 72 61 64 69 74 69 6f 6e  en .** tradition
363c1 61 6c 6c 79 20 75 73 65 64 20 62 79 20 66 74 73  ally used by fts
363c2 33 2e 20 4f 72 2c 20 69 66 20 53 51 4c 49 54 45  3. Or, if SQLITE
363c3 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52  _ENABLE_FTS3_PAR
363c4 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73 20 64  ENTHESIS.** is d
363c5 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 69 74 20  efined, then it 
363c6 75 73 65 73 20 74 68 65 20 6e 65 77 20 73 79 6e  uses the new syn
363c7 74 61 78 2e 20 54 68 65 20 64 69 66 66 65 72 65  tax. The differe
363c8 6e 63 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  nces between.** 
363c9 74 68 65 20 6e 65 77 20 61 6e 64 20 74 68 65 20  the new and the 
363ca 6f 6c 64 20 73 79 6e 74 61 78 65 73 20 61 72 65  old syntaxes are
363cb 3a 0a 2a 2a 0a 2a 2a 20 20 61 29 20 54 68 65 20  :.**.**  a) The 
363cc 6e 65 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f  new syntax suppo
363cd 72 74 73 20 70 61 72 65 6e 74 68 65 73 69 73 2e  rts parenthesis.
363ce 20 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f   The old does no
363cf 74 2e 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54 68 65  t..**.**  b) The
363d0 20 6e 65 77 20 73 79 6e 74 61 78 20 73 75 70 70   new syntax supp
363d1 6f 72 74 73 20 74 68 65 20 41 4e 44 20 61 6e 64  orts the AND and
363d2 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 73 2e 20   NOT operators. 
363d3 54 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f 74  The old does not
363d4 2e 0a 2a 2a 0a 2a 2a 20 20 63 29 20 54 68 65 20  ..**.**  c) The 
363d5 6f 6c 64 20 73 79 6e 74 61 78 20 73 75 70 70 6f  old syntax suppo
363d6 72 74 73 20 74 68 65 20 22 2d 22 20 74 6f 6b 65  rts the "-" toke
363d7 6e 20 71 75 61 6c 69 66 69 65 72 2e 20 54 68 69  n qualifier. Thi
363d8 73 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 20 20 20  s is not .**    
363d9 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 74 68   supported by th
363da 65 20 6e 65 77 20 73 79 6e 74 61 78 20 28 69 74  e new syntax (it
363db 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
363dc 74 68 65 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72  the NOT operator
363dd 29 2e 0a 2a 2a 0a 2a 2a 20 20 64 29 20 57 68 65  )..**.**  d) Whe
363de 6e 20 75 73 69 6e 67 20 74 68 65 20 6f 6c 64 20  n using the old 
363df 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52 20 6f  syntax, the OR o
363e0 70 65 72 61 74 6f 72 20 68 61 73 20 61 20 67 72  perator has a gr
363e1 65 61 74 65 72 20 70 72 65 63 65 64 65 6e 63 65  eater precedence
363e2 0a 2a 2a 20 20 20 20 20 74 68 61 6e 20 61 6e 20  .**     than an 
363e3 69 6d 70 6c 69 63 69 74 20 41 4e 44 2e 20 57 68  implicit AND. Wh
363e4 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77  en using the new
363e5 2c 20 62 6f 74 68 20 69 6d 70 6c 69 63 69 74 79  , both implicity
363e6 20 61 6e 64 20 65 78 70 6c 69 63 69 74 0a 2a 2a   and explicit.**
363e7 20 20 20 20 20 41 4e 44 20 6f 70 65 72 61 74 6f       AND operato
363e8 72 73 20 68 61 76 65 20 61 20 68 69 67 68 65 72  rs have a higher
363e9 20 70 72 65 63 65 64 65 6e 63 65 20 74 68 61 6e   precedence than
363ea 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f   OR..**.** If co
363eb 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
363ec 54 45 5f 54 45 53 54 20 64 65 66 69 6e 65 64 2c  TE_TEST defined,
363ed 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c   then this modul
363ee 65 20 65 78 70 6f 72 74 73 20 74 68 65 0a 2a 2a  e exports the.**
363ef 20 73 79 6d 62 6f 6c 20 22 69 6e 74 20 73 71 6c   symbol "int sql
363f0 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
363f1 5f 70 61 72 65 6e 74 68 65 73 65 73 22 2e 20 53  _parentheses". S
363f2 65 74 74 69 6e 67 20 74 68 69 73 20 76 61 72 69  etting this vari
363f3 61 62 6c 65 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20  able.** to zero 
363f4 63 61 75 73 65 73 20 74 68 65 20 6d 6f 64 75 6c  causes the modul
363f5 65 20 74 6f 20 75 73 65 20 74 68 65 20 6f 6c 64  e to use the old
363f6 20 73 79 6e 74 61 78 2e 20 49 66 20 69 74 20 69   syntax. If it i
363f7 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 6e 6f 6e  s set to .** non
363f8 2d 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 73 79  -zero the new sy
363f9 6e 74 61 78 20 69 73 20 61 63 74 69 76 61 74 65  ntax is activate
363fa 64 2e 20 54 68 69 73 20 69 73 20 73 6f 20 62 6f  d. This is so bo
363fb 74 68 20 73 79 6e 74 61 78 65 73 20 63 61 6e 0a  th syntaxes can.
363fc 2a 2a 20 62 65 20 74 65 73 74 65 64 20 75 73 69  ** be tested usi
363fd 6e 67 20 61 20 73 69 6e 67 6c 65 20 62 75 69 6c  ng a single buil
363fe 64 20 6f 66 20 74 65 73 74 66 69 78 74 75 72 65  d of testfixture
363ff 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ..**.** The foll
36400 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65 73 20  owing describes 
36401 74 68 65 20 73 79 6e 74 61 78 20 73 75 70 70 6f  the syntax suppo
36402 72 74 65 64 20 62 79 20 74 68 65 20 66 74 73 33  rted by the fts3
36403 20 4d 41 54 43 48 0a 2a 2a 20 6f 70 65 72 61 74   MATCH.** operat
36404 6f 72 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 20  or in a similar 
36405 66 6f 72 6d 61 74 20 74 6f 20 74 68 61 74 20 75  format to that u
36406 73 65 64 20 62 79 20 74 68 65 20 6c 65 6d 6f 6e  sed by the lemon
36407 20 70 61 72 73 65 72 0a 2a 2a 20 67 65 6e 65 72   parser.** gener
36408 61 74 6f 72 2e 20 54 68 69 73 20 6d 6f 64 75 6c  ator. This modul
36409 65 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 61  e does not use a
3640a 63 74 75 61 6c 6c 79 20 6c 65 6d 6f 6e 2c 20 69  ctually lemon, i
3640b 74 20 75 73 65 73 20 61 0a 2a 2a 20 63 75 73 74  t uses a.** cust
3640c 6f 6d 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a  om parser..**.**
3640d 20 20 20 71 75 65 72 79 20 3a 3a 3d 20 61 6e 64     query ::= and
3640e 65 78 70 72 20 28 4f 52 20 61 6e 64 65 78 70 72  expr (OR andexpr
3640f 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 6e 64 65  )*..**.**   ande
36410 78 70 72 20 3a 3a 3d 20 6e 6f 74 65 78 70 72 20  xpr ::= notexpr 
36411 28 41 4e 44 3f 20 6e 6f 74 65 78 70 72 29 2a 2e  (AND? notexpr)*.
36412 0a 2a 2a 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72  .**.**   notexpr
36413 20 3a 3a 3d 20 6e 65 61 72 65 78 70 72 20 28 4e   ::= nearexpr (N
36414 4f 54 20 6e 65 61 72 65 78 70 72 7c 2d 54 4f 4b  OT nearexpr|-TOK
36415 45 4e 29 2a 2e 0a 2a 2a 20 20 20 6e 6f 74 65 78  EN)*..**   notex
36416 70 72 20 3a 3a 3d 20 4c 50 20 71 75 65 72 79 20  pr ::= LP query 
36417 52 50 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e 65 61 72  RP..**.**   near
36418 65 78 70 72 20 3a 3a 3d 20 70 68 72 61 73 65 20  expr ::= phrase 
36419 28 4e 45 41 52 20 64 69 73 74 61 6e 63 65 5f 6f  (NEAR distance_o
3641a 70 74 20 6e 65 61 72 65 78 70 72 29 2a 2e 0a 2a  pt nearexpr)*..*
3641b 2a 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f  *.**   distance_
3641c 6f 70 74 20 3a 3a 3d 20 2e 0a 2a 2a 20 20 20 64  opt ::= ..**   d
3641d 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20  istance_opt ::= 
3641e 2f 20 49 4e 54 45 47 45 52 2e 0a 2a 2a 0a 2a 2a  / INTEGER..**.**
3641f 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 54 4f     phrase ::= TO
36420 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72 61 73 65  KEN..**   phrase
36421 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 3a 54 4f 4b 45   ::= COLUMN:TOKE
36422 4e 2e 0a 2a 2a 20 20 20 70 68 72 61 73 65 20 3a  N..**   phrase :
36423 3a 3d 20 22 54 4f 4b 45 4e 20 54 4f 4b 45 4e 20  := "TOKEN TOKEN 
36424 54 4f 4b 45 4e 2e 2e 2e 22 2e 0a 2a 2f 0a 0a 23  TOKEN..."..*/..#
36425 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
36426 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
36427 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
36428 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
36429 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 23 20 69 66   = 0;.#else.# if
3642a 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
3642b 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
3642c 49 53 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  IS .#  define sq
3642d 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c  lite3_fts3_enabl
3642e 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 31 0a  e_parentheses 1.
3642f 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
36430 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e   sqlite3_fts3_en
36431 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
36432 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69   0.# endif.#endi
36433 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  f../*.** Default
36434 20 73 70 61 6e 20 66 6f 72 20 4e 45 41 52 20 6f   span for NEAR o
36435 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65  perators..*/.#de
36436 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 54 53 33  fine SQLITE_FTS3
36437 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41  _DEFAULT_NEAR_PA
36438 52 41 4d 20 31 30 0a 0a 0a 74 79 70 65 64 65 66  RAM 10...typedef
36439 20 73 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e   struct ParseCon
3643a 74 65 78 74 20 50 61 72 73 65 43 6f 6e 74 65 78  text ParseContex
3643b 74 3b 0a 73 74 72 75 63 74 20 50 61 72 73 65 43  t;.struct ParseC
3643c 6f 6e 74 65 78 74 20 7b 0a 20 20 73 71 6c 69 74  ontext {.  sqlit
3643d 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
3643e 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f  okenizer;      /
3643f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
36440 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
36441 61 72 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20  ar **azCol;     
36442 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
36443 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
36444 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
36445 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
36446 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
36447 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36448 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
36449 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
3644a 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
3644b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
3644c 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
3644d 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
3644e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   */.  sqlite3_co
3644f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 20 20  ntext *pCtx;    
36450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
36451 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
36452 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   here */.  int n
36453 4e 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Nest;           
36454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36455 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
36456 65 64 20 62 72 61 63 6b 65 74 73 20 2a 2f 0a 7d  ed brackets */.}
36457 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ;../*.** This fu
36458 6e 63 74 69 6f 6e 20 69 73 20 65 71 75 69 76 61  nction is equiva
36459 6c 65 6e 74 20 74 6f 20 74 68 65 20 73 74 61 6e  lent to the stan
3645a 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20 66  dard isspace() f
3645b 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
3645c 54 68 65 20 73 74 61 6e 64 61 72 64 20 69 73 73  The standard iss
3645d 70 61 63 65 28 29 20 63 61 6e 20 62 65 20 61 77  pace() can be aw
3645e 6b 77 61 72 64 20 74 6f 20 75 73 65 20 73 61 66  kward to use saf
3645f 65 6c 79 2c 20 62 65 63 61 75 73 65 20 61 6c 74  ely, because alt
36460 68 6f 75 67 68 20 69 74 0a 2a 2a 20 69 73 20 64  hough it.** is d
36461 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65 70 74  efined to accept
36462 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20   an argument of 
36463 74 79 70 65 20 69 6e 74 2c 20 69 74 73 20 62 65  type int, its be
36464 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70 61 73  haviour when pas
36465 73 65 64 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  sed.** an intege
36466 72 20 74 68 61 74 20 66 61 6c 6c 73 20 6f 75 74  r that falls out
36467 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67  side of the rang
36468 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65  e of the unsigne
36469 64 20 63 68 61 72 20 74 79 70 65 0a 2a 2a 20 69  d char type.** i
3646a 73 20 75 6e 64 65 66 69 6e 65 64 20 28 61 6e 64  s undefined (and
3646b 20 73 6f 6d 65 74 69 6d 65 73 2c 20 22 75 6e 64   sometimes, "und
3646c 65 66 69 6e 65 64 22 20 6d 65 61 6e 73 20 73 65  efined" means se
3646d 67 66 61 75 6c 74 29 2e 20 54 68 69 73 20 77 72  gfault). This wr
3646e 61 70 70 65 72 0a 2a 2a 20 69 73 20 64 65 66 69  apper.** is defi
3646f 6e 65 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e  ned to accept an
36470 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
36471 65 20 63 68 61 72 2c 20 61 6e 64 20 61 6c 77 61  e char, and alwa
36472 79 73 20 72 65 74 75 72 6e 73 20 30 20 66 6f 72  ys returns 0 for
36473 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 73 20 74  .** any values t
36474 68 61 74 20 66 61 6c 6c 20 6f 75 74 73 69 64 65  hat fall outside
36475 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66   of the range of
36476 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68   the unsigned ch
36477 61 72 20 74 79 70 65 20 28 69 2e 65 2e 0a 2a 2a  ar type (i.e..**
36478 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73   negative values
36479 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
3647a 20 66 74 73 33 69 73 73 70 61 63 65 28 63 68 61   fts3isspace(cha
3647b 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
3647c 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73  c&0x80)==0 ? iss
3647d 70 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  pace(c) : 0;.}..
3647e 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
3647f 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
36480 6d 20 62 75 66 66 65 72 20 7a 20 28 6c 65 6e 67  m buffer z (leng
36481 74 68 20 6e 29 20 75 73 69 6e 67 20 74 68 65 20  th n) using the 
36482 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 61 6e 64  tokenizer.** and
36483 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
36484 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  on (column names
36485 20 65 74 63 2e 29 20 69 6e 20 70 50 61 72 73 65   etc.) in pParse
36486 2e 20 43 72 65 61 74 65 20 61 6e 20 46 74 73 33  . Create an Fts3
36487 45 78 70 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  Expr.** structur
36488 65 20 6f 66 20 74 79 70 65 20 46 54 53 51 55 45  e of type FTSQUE
36489 52 59 5f 50 48 52 41 53 45 20 63 6f 6e 74 61 69  RY_PHRASE contai
3648a 6e 69 6e 67 20 61 20 70 68 72 61 73 65 20 63 6f  ning a phrase co
3648b 6e 73 69 73 74 69 6e 67 20 6f 66 20 74 68 69 73  nsisting of this
3648c 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e  .** single token
3648d 20 61 6e 64 20 73 65 74 20 2a 70 70 45 78 70 72   and set *ppExpr
3648e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 69 74 2e   to point to it.
3648f 20 49 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   If the end of t
36490 68 65 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  he buffer is.** 
36491 72 65 61 63 68 65 64 20 62 65 66 6f 72 65 20 61  reached before a
36492 20 74 6f 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c   token is found,
36493 20 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20   set *ppExpr to 
36494 7a 65 72 6f 2e 20 49 74 20 69 73 20 74 68 65 0a  zero. It is the.
36495 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ** responsibilit
36496 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
36497 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65  to eventually de
36498 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c  allocate the all
36499 6f 63 61 74 65 64 20 0a 2a 2a 20 46 74 73 33 45  ocated .** Fts3E
3649a 78 70 72 20 73 74 72 75 63 74 75 72 65 20 28 69  xpr structure (i
3649b 66 20 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e  f any) by passin
3649c 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  g it to sqlite3_
3649d 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  free()..**.** Re
3649e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
3649f 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
364a0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
364a1 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
364a2 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a  tion.** fails..*
364a3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
364a4 4e 65 78 74 54 6f 6b 65 6e 28 0a 20 20 50 61 72  NextToken(.  Par
364a5 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
364a6 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
364a7 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
364a8 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
364a9 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
364aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
364ac 20 56 61 6c 75 65 20 66 6f 72 20 46 74 73 33 50   Value for Fts3P
364ad 68 72 61 73 65 2e 69 43 6f 6c 75 6d 6e 20 2a 2f  hrase.iColumn */
364ae 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
364af 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
364b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
364b1 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
364b2 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
364b3 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
364b4 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
364b5 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
364b6 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  int *pnConsumed 
364b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364b8 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
364b9 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
364ba 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20  consumed */.){. 
364bb 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
364bc 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
364bd 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69   pParse->pTokeni
364be 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  zer;.  sqlite3_t
364bf 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
364c0 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
364c1 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
364c2 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dule;.  int rc;.
364c3 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
364c4 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
364c5 73 6f 72 3b 0a 20 20 46 74 73 33 45 78 70 72 20  sor;.  Fts3Expr 
364c6 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 69 6e 74  *pRet = 0;.  int
364c7 20 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a   nConsumed = 0;.
364c8 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
364c9 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
364ca 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f  r, z, n, &pCurso
364cb 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
364cc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63  LITE_OK ){.    c
364cd 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
364ce 6e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65  n;.    int nToke
364cf 6e 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  n, iStart, iEnd,
364d0 20 69 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 20 20   iPosition;.    
364d1 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
364d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364d3 20 20 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61           /* tota
364d4 6c 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63  l space to alloc
364d5 61 74 65 20 2a 2f 0a 0a 20 20 20 20 70 43 75 72  ate */..    pCur
364d6 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
364d7 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
364d8 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
364d9 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26  xNext(pCursor, &
364da 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
364db 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c   &iStart, &iEnd,
364dc 20 26 69 50 6f 73 69 74 69 6f 6e 29 3b 0a 0a 20   &iPosition);.. 
364dd 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
364de 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 42  E_OK ){.      nB
364df 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73  yte = sizeof(Fts
364e0 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28  3Expr) + sizeof(
364e1 46 74 73 33 50 68 72 61 73 65 29 20 2b 20 6e 54  Fts3Phrase) + nT
364e2 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 52 65 74  oken;.      pRet
364e3 20 3d 20 28 46 74 73 33 45 78 70 72 20 2a 29 73   = (Fts3Expr *)s
364e4 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
364e5 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yte);.      if( 
364e6 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  !pRet ){.       
364e7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
364e8 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EM;.      }else{
364e9 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
364ea 70 52 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b  pRet, 0, nByte);
364eb 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65  .        pRet->e
364ec 54 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f  Type = FTSQUERY_
364ed 50 48 52 41 53 45 3b 0a 20 20 20 20 20 20 20 20  PHRASE;.        
364ee 70 52 65 74 2d 3e 70 50 68 72 61 73 65 20 3d 20  pRet->pPhrase = 
364ef 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26 70  (Fts3Phrase *)&p
364f0 52 65 74 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Ret[1];.        
364f1 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  pRet->pPhrase->n
364f2 54 6f 6b 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20  Token = 1;.     
364f3 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65     pRet->pPhrase
364f4 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c  ->iColumn = iCol
364f5 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
364f6 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
364f7 30 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20  0].n = nToken;. 
364f8 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
364f9 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
364fa 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65  z = (char *)&pRe
364fb 74 2d 3e 70 50 68 72 61 73 65 5b 31 5d 3b 0a 20  t->pPhrase[1];. 
364fc 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 52         memcpy(pR
364fd 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  et->pPhrase->aTo
364fe 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e  ken[0].z, zToken
364ff 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20  , nToken);..    
36500 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 20 26      if( iEnd<n &
36501 26 20 7a 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29  & z[iEnd]=='*' )
36502 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65 74  {.          pRet
36503 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
36504 6e 5b 30 5d 2e 69 73 50 72 65 66 69 78 20 3d 20  n[0].isPrefix = 
36505 31 3b 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e  1;.          iEn
36506 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  d++;.        }. 
36507 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
36508 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
36509 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20 69  parentheses && i
3650a 53 74 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74  Start>0 && z[iSt
3650b 61 72 74 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20  art-1]=='-' ){. 
3650c 20 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70           pRet->p
3650d 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 3d 20  Phrase->isNot = 
3650e 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
3650f 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e 73     }.      nCons
36510 75 6d 65 64 20 3d 20 69 45 6e 64 3b 0a 20 20 20  umed = iEnd;.   
36511 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d   }..    pModule-
36512 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29  >xClose(pCursor)
36513 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a 70 6e 43 6f  ;.  }.  .  *pnCo
36514 6e 73 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d  nsumed = nConsum
36515 65 64 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  ed;.  *ppExpr = 
36516 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
36517 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c  c;.}.../*.** Enl
36518 61 72 67 65 20 61 20 6d 65 6d 6f 72 79 20 61 6c  arge a memory al
36519 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 61 6e  location.  If an
3651a 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 61   out-of-memory a
3651b 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73  llocation occurs
3651c 2c 0a 2a 2a 20 74 68 65 6e 20 66 72 65 65 20 74  ,.** then free t
3651d 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f  he old allocatio
3651e 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 2a 66 74 73 33  n..*/.void *fts3
3651f 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f  ReallocOrFree(vo
36520 69 64 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e  id *pOrig, int n
36521 4e 65 77 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  New){.  void *pR
36522 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  et = sqlite3_rea
36523 6c 6c 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65 77  lloc(pOrig, nNew
36524 29 3b 0a 20 20 69 66 28 20 21 70 52 65 74 20 29  );.  if( !pRet )
36525 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
36526 65 65 28 70 4f 72 69 67 29 3b 0a 20 20 7d 0a 20  ee(pOrig);.  }. 
36527 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
36528 0a 2f 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 49  ./*.** Buffer zI
36529 6e 70 75 74 2c 20 6c 65 6e 67 74 68 20 6e 49 6e  nput, length nIn
3652a 70 75 74 2c 20 63 6f 6e 74 61 69 6e 73 20 74 68  put, contains th
3652b 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
3652c 71 75 6f 74 65 64 20 73 74 72 69 6e 67 0a 2a 2a  quoted string.**
3652d 20 74 68 61 74 20 61 70 70 65 61 72 65 64 20 61   that appeared a
3652e 73 20 70 61 72 74 20 6f 66 20 61 6e 20 66 74 73  s part of an fts
3652f 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
36530 6f 6e 2e 20 4e 65 69 74 68 65 72 20 71 75 6f 74  on. Neither quot
36531 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69  e character.** i
36532 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
36533 65 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66  e buffer. This f
36534 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
36535 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 74 68 65   to tokenize the
36536 20 65 6e 74 69 72 65 0a 2a 2a 20 69 6e 70 75 74   entire.** input
36537 20 62 75 66 66 65 72 20 61 6e 64 20 63 72 65 61   buffer and crea
36538 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 20 73  te an Fts3Expr s
36539 74 72 75 63 74 75 72 65 20 6f 66 20 74 79 70 65  tructure of type
3653a 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
3653b 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20   .** containing 
3653c 74 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a  the results..**.
3653d 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
3653e 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
3653f 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45  eturned and *ppE
36540 78 70 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  xpr set to point
36541 20 61 74 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   at the.** alloc
36542 61 74 65 64 20 46 74 73 33 45 78 70 72 20 73 74  ated Fts3Expr st
36543 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69  ructure. Otherwi
36544 73 65 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54  se, either SQLIT
36545 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20  E_NOMEM (out of 
36546 6d 65 6d 6f 72 79 0a 2a 2a 20 65 72 72 6f 72 29  memory.** error)
36547 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   or SQLITE_ERROR
36548 20 28 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 65   (tokenization e
36549 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65  rror) is returne
3654a 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73 65  d and *ppExpr se
3654b 74 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74  t.** to 0..*/.st
3654c 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
3654d 53 74 72 69 6e 67 28 0a 20 20 50 61 72 73 65 43  String(.  ParseC
3654e 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20  ontext *pParse, 
3654f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36550 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20    /* fts3 query 
36551 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
36552 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36553 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75  Input, int nInpu
36554 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  t,         /* In
36555 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
36556 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70  Fts3Expr **ppExp
36557 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
36558 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
36559 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b  expression */.){
3655a 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
3655b 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
3655c 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65   = pParse->pToke
3655d 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33  nizer;.  sqlite3
3655e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
3655f 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65  e const *pModule
36560 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
36561 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63  Module;.  int rc
36562 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20  ;.  Fts3Expr *p 
36563 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
36564 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
36565 2a 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  *pCursor = 0;.  
36566 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b  char *zTemp = 0;
36567 0a 20 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30  .  int nTemp = 0
36568 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
36569 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
3656a 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
3656b 70 75 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a  put, &pCursor);.
3656c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3656d 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  _OK ){.    int i
3656e 69 3b 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  i;.    pCursor->
3656f 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
36570 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 66 6f 72  kenizer;.    for
36571 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
36572 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  E_OK; ii++){.   
36573 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
36574 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 6e 74  Token;.      int
36575 20 6e 54 6f 6b 65 6e 2c 20 69 42 65 67 69 6e 2c   nToken, iBegin,
36576 20 69 45 6e 64 2c 20 69 50 6f 73 3b 0a 20 20 20   iEnd, iPos;.   
36577 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
36578 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20  >xNext(pCursor, 
36579 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e  &zToken, &nToken
3657a 2c 20 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64  , &iBegin, &iEnd
3657b 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20 20 20 20  , &iPos);.      
3657c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3657d 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  K ){.        int
3657e 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
3657f 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65  Fts3Expr) + size
36580 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a  of(Fts3Phrase);.
36581 20 20 20 20 20 20 20 20 70 20 3d 20 66 74 73 33          p = fts3
36582 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c  ReallocOrFree(p,
36583 20 6e 42 79 74 65 2b 69 69 2a 73 69 7a 65 6f 66   nByte+ii*sizeof
36584 28 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f  (struct PhraseTo
36585 6b 65 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 7a  ken));.        z
36586 54 65 6d 70 20 3d 20 66 74 73 33 52 65 61 6c 6c  Temp = fts3Reall
36587 6f 63 4f 72 46 72 65 65 28 7a 54 65 6d 70 2c 20  ocOrFree(zTemp, 
36588 6e 54 65 6d 70 20 2b 20 6e 54 6f 6b 65 6e 29 3b  nTemp + nToken);
36589 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 20  .        if( !p 
3658a 7c 7c 20 21 7a 54 65 6d 70 20 29 7b 0a 20 20 20  || !zTemp ){.   
3658b 20 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d         goto no_m
3658c 65 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  em;.        }.  
3658d 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20        if( ii==0 
3658e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
3658f 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29  set(p, 0, nByte)
36590 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
36591 50 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68  Phrase = (Fts3Ph
36592 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  rase *)&p[1];.  
36593 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
36594 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
36595 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
36596 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68  ;.        p->pPh
36597 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 69  rase->nToken = i
36598 69 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  i+1;.        p->
36599 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
3659a 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a  ii].n = nToken;.
3659b 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
3659c 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a 54  zTemp[nTemp], zT
3659d 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20  oken, nToken);. 
3659e 20 20 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d 20         nTemp += 
3659f 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  nToken;.        
365a0 69 66 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74 20  if( iEnd<nInput 
365a1 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d  && zInput[iEnd]=
365a2 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
365a3 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54    p->pPhrase->aT
365a4 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69  oken[ii].isPrefi
365a5 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  x = 1;.        }
365a6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
365a7 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  p->pPhrase->aTok
365a8 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20  en[ii].isPrefix 
365a9 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
365aa 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
365ab 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
365ac 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  e(pCursor);.    
365ad 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d  pCursor = 0;.  }
365ae 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
365af 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69  TE_DONE ){.    i
365b0 6e 74 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20  nt jj;.    char 
365b1 2a 7a 4e 65 77 20 3d 20 4e 55 4c 4c 3b 0a 20 20  *zNew = NULL;.  
365b2 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 0a    int nNew = 0;.
365b3 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
365b4 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
365b5 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68   + sizeof(Fts3Ph
365b6 72 61 73 65 29 3b 0a 20 20 20 20 6e 42 79 74 65  rase);.    nByte
365b7 20 2b 3d 20 28 70 3f 28 70 2d 3e 70 50 68 72 61   += (p?(p->pPhra
365b8 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 3a 30 29  se->nToken-1):0)
365b9 20 2a 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74   * sizeof(struct
365ba 20 50 68 72 61 73 65 54 6f 6b 65 6e 29 3b 0a 20   PhraseToken);. 
365bb 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c     p = fts3Reall
365bc 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74  ocOrFree(p, nByt
365bd 65 20 2b 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20  e + nTemp);.    
365be 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20  if( !p ){.      
365bf 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
365c0 20 7d 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70   }.    if( zTemp
365c1 20 29 7b 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d   ){.      zNew =
365c2 20 26 28 28 28 63 68 61 72 20 2a 29 70 29 5b 6e   &(((char *)p)[n
365c3 42 79 74 65 5d 29 3b 0a 20 20 20 20 20 20 6d 65  Byte]);.      me
365c4 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 54 65 6d 70  mcpy(zNew, zTemp
365c5 2c 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20 7d 65  , nTemp);.    }e
365c6 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65  lse{.      memse
365c7 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 2b 6e 54  t(p, 0, nByte+nT
365c8 65 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  emp);.    }.    
365c9 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
365ca 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
365cb 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20  ;.    for(jj=0; 
365cc 6a 6a 3c 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  jj<p->pPhrase->n
365cd 54 6f 6b 65 6e 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  Token; jj++){.  
365ce 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e      p->pPhrase->
365cf 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26  aToken[jj].z = &
365d0 7a 4e 65 77 5b 6e 4e 65 77 5d 3b 0a 20 20 20 20  zNew[nNew];.    
365d1 20 20 6e 4e 65 77 20 2b 3d 20 70 2d 3e 70 50 68    nNew += p->pPh
365d2 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d  rase->aToken[jj]
365d3 2e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  .n;.    }.    sq
365d4 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70  lite3_free(zTemp
365d5 29 3b 0a 20 20 20 20 70 2d 3e 65 54 79 70 65 20  );.    p->eType 
365d6 3d 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  = FTSQUERY_PHRAS
365d7 45 3b 0a 20 20 20 20 70 2d 3e 70 50 68 72 61 73  E;.    p->pPhras
365d8 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61  e->iColumn = pPa
365d9 72 73 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c  rse->iDefaultCol
365da 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
365db 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70  E_OK;.  }..  *pp
365dc 45 78 70 72 20 3d 20 70 3b 0a 20 20 72 65 74 75  Expr = p;.  retu
365dd 72 6e 20 72 63 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a  rn rc;.no_mem:..
365de 20 20 69 66 28 20 70 43 75 72 73 6f 72 20 29 7b    if( pCursor ){
365df 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  .    pModule->xC
365e0 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20  lose(pCursor);. 
365e1 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
365e2 65 28 7a 54 65 6d 70 29 3b 0a 20 20 73 71 6c 69  e(zTemp);.  sqli
365e3 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 2a  te3_free(p);.  *
365e4 70 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 72 65  ppExpr = 0;.  re
365e5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
365e6 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63  M;.}../*.** Func
365e7 74 69 6f 6e 20 67 65 74 4e 65 78 74 4e 6f 64 65  tion getNextNode
365e8 28 29 2c 20 77 68 69 63 68 20 69 73 20 63 61 6c  (), which is cal
365e9 6c 65 64 20 62 79 20 66 74 73 33 45 78 70 72 50  led by fts3ExprP
365ea 61 72 73 65 28 29 2c 20 6d 61 79 20 69 74 73 65  arse(), may itse
365eb 6c 66 0a 2a 2a 20 63 61 6c 6c 20 66 74 73 33 45  lf.** call fts3E
365ec 78 70 72 50 61 72 73 65 28 29 2e 20 53 6f 20 74  xprParse(). So t
365ed 68 69 73 20 66 6f 72 77 61 72 64 20 64 65 63 6c  his forward decl
365ee 61 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69  aration is requi
365ef 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  red..*/.static i
365f0 6e 74 20 66 74 73 33 45 78 70 72 50 61 72 73 65  nt fts3ExprParse
365f1 28 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 2c  (ParseContext *,
365f2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
365f3 6e 74 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 2c  nt, Fts3Expr **,
365f4 20 69 6e 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   int *);../*.** 
365f5 54 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61  The output varia
365f6 62 6c 65 20 2a 70 70 45 78 70 72 20 69 73 20 70  ble *ppExpr is p
365f7 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e  opulated with an
365f8 20 61 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45   allocated Fts3E
365f9 78 70 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  xpr .** structur
365fa 65 2c 20 6f 72 20 73 65 74 20 74 6f 20 30 20 69  e, or set to 0 i
365fb 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
365fc 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 69 73   input buffer is
365fd 20 72 65 61 63 68 65 64 2e 0a 2a 2a 0a 2a 2a 20   reached..**.** 
365fe 52 65 74 75 72 6e 73 20 61 6e 20 53 51 4c 69 74  Returns an SQLit
365ff 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 53 51  e error code. SQ
36600 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
36601 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c  thing works, SQL
36602 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 66 20  ITE_NOMEM.** if 
36603 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
36604 20 6f 63 63 75 72 73 2c 20 6f 72 20 53 51 4c 49   occurs, or SQLI
36605 54 45 5f 45 52 52 4f 52 20 69 66 20 61 20 70 61  TE_ERROR if a pa
36606 72 73 65 20 65 72 72 6f 72 20 69 73 20 65 6e 63  rse error is enc
36607 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 49 66 20  ountered..** If 
36608 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20  SQLITE_ERROR is 
36609 72 65 74 75 72 6e 65 64 2c 20 70 43 6f 6e 74 65  returned, pConte
3660a 78 74 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  xt is populated 
3660b 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
3660c 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssage..*/.static
3660d 20 69 6e 74 20 67 65 74 4e 65 78 74 4e 6f 64 65   int getNextNode
3660e 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74  (.  ParseContext
3660f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
36610 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
36611 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65 20  ts3 query parse 
36612 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e  context */.  con
36613 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
36614 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
36615 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
36616 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45 78  ring */.  Fts3Ex
36617 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20  pr **ppExpr,    
36618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36619 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65 73    /* OUT: expres
3661a 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  sion */.  int *p
3661b 6e 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20  nConsumed       
3661c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3661d 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72    /* OUT: Number
3661e 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d   of bytes consum
3661f 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69  ed */.){.  stati
36620 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46  c const struct F
36621 74 73 33 4b 65 79 77 6f 72 64 20 7b 0a 20 20 20  ts3Keyword {.   
36622 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
36623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36624 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72         /* Keywor
36625 64 20 74 65 78 74 20 2a 2f 0a 20 20 20 20 75 6e  d text */.    un
36626 73 69 67 6e 65 64 20 63 68 61 72 20 6e 3b 20 20  signed char n;  
36627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36628 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
36629 20 74 68 65 20 6b 65 79 77 6f 72 64 20 2a 2f 0a   the keyword */.
3662a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
3662b 72 20 70 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20  r parenOnly;    
3662c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c            /* Onl
3662d 79 20 76 61 6c 69 64 20 69 6e 20 70 61 72 65 6e  y valid in paren
3662e 20 6d 6f 64 65 20 2a 2f 0a 20 20 20 20 75 6e 73   mode */.    uns
3662f 69 67 6e 65 64 20 63 68 61 72 20 65 54 79 70 65  igned char eType
36630 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36631 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f     /* Keyword co
36632 64 65 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f  de */.  } aKeywo
36633 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  rd[] = {.    { "
36634 4f 52 22 20 2c 20 20 32 2c 20 30 2c 20 46 54 53  OR" ,  2, 0, FTS
36635 51 55 45 52 59 5f 4f 52 20 20 20 7d 2c 0a 20 20  QUERY_OR   },.  
36636 20 20 7b 20 22 41 4e 44 22 2c 20 20 33 2c 20 31    { "AND",  3, 1
36637 2c 20 46 54 53 51 55 45 52 59 5f 41 4e 44 20 20  , FTSQUERY_AND  
36638 7d 2c 0a 20 20 20 20 7b 20 22 4e 4f 54 22 2c 20  },.    { "NOT", 
36639 20 33 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f   3, 1, FTSQUERY_
3663a 4e 4f 54 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e  NOT  },.    { "N
3663b 45 41 52 22 2c 20 34 2c 20 30 2c 20 46 54 53 51  EAR", 4, 0, FTSQ
3663c 55 45 52 59 5f 4e 45 41 52 20 7d 0a 20 20 7d 3b  UERY_NEAR }.  };
3663d 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
3663e 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 43 6f   iCol;.  int iCo
3663f 6c 4c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  lLen;.  int rc;.
36640 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74    Fts3Expr *pRet
36641 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63   = 0;..  const c
36642 68 61 72 20 2a 7a 49 6e 70 75 74 20 3d 20 7a 3b  har *zInput = z;
36643 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 20 3d 20  .  int nInput = 
36644 6e 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76  n;..  /* Skip ov
36645 65 72 20 61 6e 79 20 77 68 69 74 65 73 70 61 63  er any whitespac
36646 65 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e  e before checkin
36647 67 20 66 6f 72 20 61 20 6b 65 79 77 6f 72 64 2c  g for a keyword,
36648 20 61 6e 20 6f 70 65 6e 20 6f 72 0a 20 20 2a 2a   an open or.  **
36649 20 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2c 20   close bracket, 
3664a 6f 72 20 61 20 71 75 6f 74 65 64 20 73 74 72 69  or a quoted stri
3664b 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  ng. .  */.  whil
3664c 65 28 20 6e 49 6e 70 75 74 3e 30 20 26 26 20 66  e( nInput>0 && f
3664d 74 73 33 69 73 73 70 61 63 65 28 2a 7a 49 6e 70  ts3isspace(*zInp
3664e 75 74 29 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75  ut) ){.    nInpu
3664f 74 2d 2d 3b 0a 20 20 20 20 7a 49 6e 70 75 74 2b  t--;.    zInput+
36650 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 49 6e  +;.  }.  if( nIn
36651 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  put==0 ){.    re
36652 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
36653 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20  ;.  }..  /* See 
36654 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e  if we are dealin
36655 67 20 77 69 74 68 20 61 20 6b 65 79 77 6f 72 64  g with a keyword
36656 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b  . */.  for(ii=0;
36657 20 69 69 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66   ii<(int)(sizeof
36658 28 61 4b 65 79 77 6f 72 64 29 2f 73 69 7a 65 6f  (aKeyword)/sizeo
36659 66 28 73 74 72 75 63 74 20 46 74 73 33 4b 65 79  f(struct Fts3Key
3665a 77 6f 72 64 29 29 3b 20 69 69 2b 2b 29 7b 0a 20  word)); ii++){. 
3665b 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
3665c 46 74 73 33 4b 65 79 77 6f 72 64 20 2a 70 4b 65  Fts3Keyword *pKe
3665d 79 20 3d 20 26 61 4b 65 79 77 6f 72 64 5b 69 69  y = &aKeyword[ii
3665e 5d 3b 0a 0a 20 20 20 20 69 66 28 20 28 70 4b 65  ];..    if( (pKe
3665f 79 2d 3e 70 61 72 65 6e 4f 6e 6c 79 20 26 20 7e  y->parenOnly & ~
36660 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
36661 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 29  ble_parentheses)
36662 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  !=0 ){.      con
36663 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20  tinue;.    }..  
36664 20 20 69 66 28 20 6e 49 6e 70 75 74 3e 3d 70 4b    if( nInput>=pK
36665 65 79 2d 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d 63  ey->n && 0==memc
36666 6d 70 28 7a 49 6e 70 75 74 2c 20 70 4b 65 79 2d  mp(zInput, pKey-
36667 3e 7a 2c 20 70 4b 65 79 2d 3e 6e 29 20 29 7b 0a  >z, pKey->n) ){.
36668 20 20 20 20 20 20 69 6e 74 20 6e 4e 65 61 72 20        int nNear 
36669 3d 20 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45  = SQLITE_FTS3_DE
3666a 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d  FAULT_NEAR_PARAM
3666b 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79  ;.      int nKey
3666c 20 3d 20 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20 20   = pKey->n;.    
3666d 20 20 63 68 61 72 20 63 4e 65 78 74 3b 0a 0a 20    char cNext;.. 
3666e 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
3666f 69 73 20 61 20 22 4e 45 41 52 22 20 6b 65 79 77  is a "NEAR" keyw
36670 6f 72 64 2c 20 63 68 65 63 6b 20 66 6f 72 20 61  ord, check for a
36671 6e 20 65 78 70 6c 69 63 69 74 20 6e 65 61 72 6e  n explicit nearn
36672 65 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  ess. */.      if
36673 28 20 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46  ( pKey->eType==F
36674 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a  TSQUERY_NEAR ){.
36675 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
36676 6e 4b 65 79 3d 3d 34 20 29 3b 0a 20 20 20 20 20  nKey==4 );.     
36677 20 20 20 69 66 28 20 7a 49 6e 70 75 74 5b 34 5d     if( zInput[4]
36678 3d 3d 27 2f 27 20 26 26 20 7a 49 6e 70 75 74 5b  =='/' && zInput[
36679 35 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75  5]>='0' && zInpu
3667a 74 5b 35 5d 3c 3d 27 39 27 20 29 7b 0a 20 20 20  t[5]<='9' ){.   
3667b 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 30         nNear = 0
3667c 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
3667d 6e 4b 65 79 3d 35 3b 20 7a 49 6e 70 75 74 5b 6e  nKey=5; zInput[n
3667e 4b 65 79 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e  Key]>='0' && zIn
3667f 70 75 74 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b 20  put[nKey]<='9'; 
36680 6e 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20  nKey++){.       
36681 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 6e 4e 65       nNear = nNe
36682 61 72 20 2a 20 31 30 20 2b 20 28 7a 49 6e 70 75  ar * 10 + (zInpu
36683 74 5b 6e 4b 65 79 5d 20 2d 20 27 30 27 29 3b 0a  t[nKey] - '0');.
36684 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
36685 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
36686 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20       /* At this 
36687 70 6f 69 6e 74 20 74 68 69 73 20 69 73 20 70 72  point this is pr
36688 6f 62 61 62 6c 79 20 61 20 6b 65 79 77 6f 72 64  obably a keyword
36689 2e 20 42 75 74 20 66 6f 72 20 74 68 61 74 20 74  . But for that t
3668a 6f 20 62 65 20 74 72 75 65 2c 0a 20 20 20 20 20  o be true,.     
3668b 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 62 79 74   ** the next byt
3668c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 65  e must contain e
3668d 69 74 68 65 72 20 77 68 69 74 65 73 70 61 63 65  ither whitespace
3668e 2c 20 61 6e 20 6f 70 65 6e 20 6f 72 20 63 6c 6f  , an open or clo
3668f 73 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65  se.      ** pare
36690 6e 74 68 65 73 69 73 2c 20 61 20 71 75 6f 74 65  nthesis, a quote
36691 20 63 68 61 72 61 63 74 65 72 2c 20 6f 72 20 45   character, or E
36692 4f 46 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  OF. .      */.  
36693 20 20 20 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70      cNext = zInp
36694 75 74 5b 6e 4b 65 79 5d 3b 0a 20 20 20 20 20 20  ut[nKey];.      
36695 69 66 28 20 66 74 73 33 69 73 73 70 61 63 65 28  if( fts3isspace(
36696 63 4e 65 78 74 29 20 0a 20 20 20 20 20 20 20 7c  cNext) .       |
36697 7c 20 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20  | cNext=='"' || 
36698 63 4e 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e  cNext=='(' || cN
36699 65 78 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78  ext==')' || cNex
3669a 74 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20  t==0.      ){.  
3669b 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46 74        pRet = (Ft
3669c 73 33 45 78 70 72 20 2a 29 73 71 6c 69 74 65 33  s3Expr *)sqlite3
3669d 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
3669e 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20  ts3Expr));.     
3669f 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a     if( !pRet ){.
366a0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
366a1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
366a2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
366a3 20 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c   memset(pRet, 0,
366a4 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
366a5 29 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74  ));.        pRet
366a6 2d 3e 65 54 79 70 65 20 3d 20 70 4b 65 79 2d 3e  ->eType = pKey->
366a7 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70  eType;.        p
366a8 52 65 74 2d 3e 6e 4e 65 61 72 20 3d 20 6e 4e 65  Ret->nNear = nNe
366a9 61 72 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 45  ar;.        *ppE
366aa 78 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 20 20  xpr = pRet;.    
366ab 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
366ac 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75 74 20  = (int)((zInput 
366ad 2d 20 7a 29 20 2b 20 6e 4b 65 79 29 3b 0a 20 20  - z) + nKey);.  
366ae 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
366af 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a  ITE_OK;.      }.
366b0 0a 20 20 20 20 20 20 2f 2a 20 54 75 72 6e 73 20  .      /* Turns 
366b1 6f 75 74 20 74 68 61 74 20 77 61 73 6e 27 74 20  out that wasn't 
366b2 61 20 6b 65 79 77 6f 72 64 20 61 66 74 65 72 20  a keyword after 
366b3 61 6c 6c 2e 20 54 68 69 73 20 68 61 70 70 65 6e  all. This happen
366b4 73 20 69 66 20 74 68 65 0a 20 20 20 20 20 20 2a  s if the.      *
366b5 2a 20 75 73 65 72 20 68 61 73 20 73 75 70 70 6c  * user has suppl
366b6 69 65 64 20 61 20 74 6f 6b 65 6e 20 73 75 63 68  ied a token such
366b7 20 61 73 20 22 4f 52 61 63 6c 65 22 2e 20 43 6f   as "ORacle". Co
366b8 6e 74 69 6e 75 65 2e 0a 20 20 20 20 20 20 2a 2f  ntinue..      */
366b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
366ba 20 43 68 65 63 6b 20 66 6f 72 20 61 6e 20 6f 70   Check for an op
366bb 65 6e 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20  en bracket. */. 
366bc 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66 74 73   if( sqlite3_fts
366bd 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
366be 65 73 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20  eses ){.    if( 
366bf 2a 7a 49 6e 70 75 74 3d 3d 27 28 27 20 29 7b 0a  *zInput=='(' ){.
366c0 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 75        int nConsu
366c1 6d 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  med;.      int r
366c2 63 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  c;.      pParse-
366c3 3e 6e 4e 65 73 74 2b 2b 3b 0a 20 20 20 20 20 20  >nNest++;.      
366c4 72 63 20 3d 20 66 74 73 33 45 78 70 72 50 61 72  rc = fts3ExprPar
366c5 73 65 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70  se(pParse, &zInp
366c6 75 74 5b 31 5d 2c 20 6e 49 6e 70 75 74 2d 31 2c  ut[1], nInput-1,
366c7 20 70 70 45 78 70 72 2c 20 26 6e 43 6f 6e 73 75   ppExpr, &nConsu
366c8 6d 65 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  med);.      if( 
366c9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
366ca 20 21 2a 70 70 45 78 70 72 20 29 7b 0a 20 20 20   !*ppExpr ){.   
366cb 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
366cc 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20  _DONE;.      }. 
366cd 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64       *pnConsumed
366ce 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75 74   = (int)((zInput
366cf 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e 43 6f 6e   - z) + 1 + nCon
366d0 73 75 6d 65 64 29 3b 0a 20 20 20 20 20 20 72 65  sumed);.      re
366d1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
366d2 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66   .    /* Check f
366d3 6f 72 20 61 20 63 6c 6f 73 65 20 62 72 61 63 6b  or a close brack
366d4 65 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a  et. */.    if( *
366d5 7a 49 6e 70 75 74 3d 3d 27 29 27 20 29 7b 0a 20  zInput==')' ){. 
366d6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65       pParse->nNe
366d7 73 74 2d 2d 3b 0a 20 20 20 20 20 20 2a 70 6e 43  st--;.      *pnC
366d8 6f 6e 73 75 6d 65 64 20 3d 20 28 69 6e 74 29 28  onsumed = (int)(
366d9 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20 31  (zInput - z) + 1
366da 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
366db 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
366dc 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65   }.  }..  /* See
366dd 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
366de 6e 67 20 77 69 74 68 20 61 20 71 75 6f 74 65 64  ng with a quoted
366df 20 70 68 72 61 73 65 2e 20 49 66 20 74 68 69 73   phrase. If this
366e0 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
366e1 65 6e 0a 20 20 2a 2a 20 73 65 61 72 63 68 20 66  en.  ** search f
366e2 6f 72 20 74 68 65 20 63 6c 6f 73 69 6e 67 20 71  or the closing q
366e3 75 6f 74 65 20 61 6e 64 20 70 61 73 73 20 74 68  uote and pass th
366e4 65 20 77 68 6f 6c 65 20 73 74 72 69 6e 67 20 74  e whole string t
366e5 6f 20 67 65 74 4e 65 78 74 53 74 72 69 6e 67 28  o getNextString(
366e6 29 0a 20 20 2a 2a 20 66 6f 72 20 70 72 6f 63 65  ).  ** for proce
366e7 73 73 69 6e 67 2e 20 54 68 69 73 20 69 73 20 65  ssing. This is e
366e8 61 73 79 20 74 6f 20 64 6f 2c 20 61 73 20 66 74  asy to do, as ft
366e9 73 33 20 68 61 73 20 6e 6f 20 73 79 6e 74 61 78  s3 has no syntax
366ea 20 66 6f 72 20 65 73 63 61 70 69 6e 67 0a 20 20   for escaping.  
366eb 2a 2a 20 61 20 71 75 6f 74 65 20 63 68 61 72 61  ** a quote chara
366ec 63 74 65 72 20 65 6d 62 65 64 64 65 64 20 69 6e  cter embedded in
366ed 20 61 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a   a string..  */.
366ee 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27    if( *zInput=='
366ef 22 27 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69  "' ){.    for(ii
366f0 3d 31 3b 20 69 69 3c 6e 49 6e 70 75 74 20 26 26  =1; ii<nInput &&
366f1 20 7a 49 6e 70 75 74 5b 69 69 5d 21 3d 27 22 27   zInput[ii]!='"'
366f2 3b 20 69 69 2b 2b 29 3b 0a 20 20 20 20 2a 70 6e  ; ii++);.    *pn
366f3 43 6f 6e 73 75 6d 65 64 20 3d 20 28 69 6e 74 29  Consumed = (int)
366f4 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20  ((zInput - z) + 
366f5 69 69 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28  ii + 1);.    if(
366f6 20 69 69 3d 3d 6e 49 6e 70 75 74 20 29 7b 0a 20   ii==nInput ){. 
366f7 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
366f8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
366f9 20 20 20 20 72 65 74 75 72 6e 20 67 65 74 4e 65      return getNe
366fa 78 74 53 74 72 69 6e 67 28 70 50 61 72 73 65 2c  xtString(pParse,
366fb 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 69 69 2d   &zInput[1], ii-
366fc 31 2c 20 70 70 45 78 70 72 29 3b 0a 20 20 7d 0a  1, ppExpr);.  }.
366fd 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f  ..  /* If contro
366fe 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20  l flows to this 
366ff 70 6f 69 6e 74 2c 20 74 68 69 73 20 6d 75 73 74  point, this must
36700 20 62 65 20 61 20 72 65 67 75 6c 61 72 20 74 6f   be a regular to
36701 6b 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a 20 74 68  ken, or .  ** th
36702 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70  e end of the inp
36703 75 74 2e 20 52 65 61 64 20 61 20 72 65 67 75 6c  ut. Read a regul
36704 61 72 20 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74  ar token using t
36705 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  he sqlite3_token
36706 69 7a 65 72 0a 20 20 2a 2a 20 69 6e 74 65 72 66  izer.  ** interf
36707 61 63 65 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e  ace. Before doin
36708 67 20 73 6f 2c 20 66 69 67 75 72 65 20 6f 75 74  g so, figure out
36709 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
3670a 65 78 70 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f  explicit.  ** co
3670b 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72 20 66  lumn specifier f
3670c 6f 72 20 74 68 65 20 74 6f 6b 65 6e 2e 20 0a 20  or the token. . 
3670d 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 53   **.  ** TODO: S
3670e 74 72 61 6e 67 65 6c 79 2c 20 69 74 20 69 73 20  trangely, it is 
3670f 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
36710 61 73 73 6f 63 69 61 74 65 20 61 20 63 6f 6c 75  associate a colu
36711 6d 6e 20 73 70 65 63 69 66 69 65 72 0a 20 20 2a  mn specifier.  *
36712 2a 20 77 69 74 68 20 61 20 71 75 6f 74 65 64 20  * with a quoted 
36713 70 68 72 61 73 65 2c 20 6f 6e 6c 79 20 77 69 74  phrase, only wit
36714 68 20 61 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e  h a single token
36715 2e 20 4e 6f 74 20 73 75 72 65 20 69 66 20 74 68  . Not sure if th
36716 69 73 20 77 61 73 0a 20 20 2a 2a 20 61 6e 20 69  is was.  ** an i
36717 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 72  mplementation ar
36718 74 69 66 61 63 74 20 6f 72 20 61 6e 20 69 6e 74  tifact or an int
36719 65 6e 74 69 6f 6e 61 6c 20 64 65 63 69 73 69 6f  entional decisio
3671a 6e 20 77 68 65 6e 20 66 74 73 33 20 77 61 73 0a  n when fts3 was.
3671b 20 20 2a 2a 20 66 69 72 73 74 20 69 6d 70 6c 65    ** first imple
3671c 6d 65 6e 74 65 64 2e 20 57 68 69 63 68 65 76 65  mented. Whicheve
3671d 72 20 69 74 20 77 61 73 2c 20 74 68 69 73 20 6d  r it was, this m
3671e 6f 64 75 6c 65 20 64 75 70 6c 69 63 61 74 65 73  odule duplicates
3671f 20 74 68 65 20 0a 20 20 2a 2a 20 6c 69 6d 69 74   the .  ** limit
36720 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 43  ation..  */.  iC
36721 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 44 65  ol = pParse->iDe
36722 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 69 43 6f 6c  faultCol;.  iCol
36723 4c 65 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  Len = 0;.  for(i
36724 69 3d 30 3b 20 69 69 3c 70 50 61 72 73 65 2d 3e  i=0; ii<pParse->
36725 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  nCol; ii++){.   
36726 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
36727 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 7a 43 6f  r = pParse->azCo
36728 6c 5b 69 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6e  l[ii];.    int n
36729 53 74 72 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  Str = (int)strle
3672a 6e 28 7a 53 74 72 29 3b 0a 20 20 20 20 69 66 28  n(zStr);.    if(
3672b 20 6e 49 6e 70 75 74 3e 6e 53 74 72 20 26 26 20   nInput>nStr && 
3672c 7a 49 6e 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a  zInput[nStr]==':
3672d 27 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74  ' .     && sqlit
3672e 65 33 5f 73 74 72 6e 69 63 6d 70 28 7a 53 74 72  e3_strnicmp(zStr
3672f 2c 20 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d  , zInput, nStr)=
36730 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  =0 .    ){.     
36731 20 69 43 6f 6c 20 3d 20 69 69 3b 0a 20 20 20 20   iCol = ii;.    
36732 20 20 69 43 6f 6c 4c 65 6e 20 3d 20 28 69 6e 74    iColLen = (int
36733 29 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  )((zInput - z) +
36734 20 6e 53 74 72 20 2b 20 31 29 3b 0a 20 20 20 20   nStr + 1);.    
36735 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
36736 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 4e 65 78   }.  rc = getNex
36737 74 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 69  tToken(pParse, i
36738 43 6f 6c 2c 20 26 7a 5b 69 43 6f 6c 4c 65 6e 5d  Col, &z[iColLen]
36739 2c 20 6e 2d 69 43 6f 6c 4c 65 6e 2c 20 70 70 45  , n-iColLen, ppE
3673a 78 70 72 2c 20 70 6e 43 6f 6e 73 75 6d 65 64 29  xpr, pnConsumed)
3673b 3b 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  ;.  *pnConsumed 
3673c 2b 3d 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 72 65  += iColLen;.  re
3673d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
3673e 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
3673f 73 20 61 6e 20 46 74 73 33 45 78 70 72 20 73 74  s an Fts3Expr st
36740 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 62 69  ructure for a bi
36741 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 61  nary operator (a
36742 6e 79 20 74 79 70 65 0a 2a 2a 20 65 78 63 65 70  ny type.** excep
36743 74 20 61 6e 20 46 54 53 51 55 45 52 59 5f 50 48  t an FTSQUERY_PH
36744 52 41 53 45 29 2e 20 52 65 74 75 72 6e 20 61 6e  RASE). Return an
36745 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 72   integer value r
36746 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a  epresenting the.
36747 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 6f 66  ** precedence of
36748 20 74 68 65 20 6f 70 65 72 61 74 6f 72 2e 20 4c   the operator. L
36749 6f 77 65 72 20 76 61 6c 75 65 73 20 68 61 76 65  ower values have
3674a 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
3674b 65 6e 63 65 20 28 69 2e 65 2e 0a 2a 2a 20 67 72  ence (i.e..** gr
3674c 6f 75 70 20 6d 6f 72 65 20 74 69 67 68 74 6c 79  oup more tightly
3674d 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ). For example, 
3674e 69 6e 20 74 68 65 20 43 20 6c 61 6e 67 75 61 67  in the C languag
3674f 65 2c 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74  e, the == operat
36750 6f 72 0a 2a 2a 20 67 72 6f 75 70 73 20 6d 6f 72  or.** groups mor
36751 65 20 74 69 67 68 74 6c 79 20 74 68 61 6e 20 7c  e tightly than |
36752 7c 2c 20 61 6e 64 20 77 6f 75 6c 64 20 74 68 65  |, and would the
36753 72 65 66 6f 72 65 20 68 61 76 65 20 61 20 68 69  refore have a hi
36754 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 2e  gher precedence.
36755 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 75 73 69 6e  .**.** When usin
36756 67 20 74 68 65 20 6e 65 77 20 66 74 73 33 20 71  g the new fts3 q
36757 75 65 72 79 20 73 79 6e 74 61 78 20 28 77 68 65  uery syntax (whe
36758 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  n SQLITE_ENABLE_
36759 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
3675a 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 29 2c  .** is defined),
3675b 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
3675c 65 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 70  e operators in p
3675d 72 65 63 65 64 65 6e 63 65 20 66 72 6f 6d 20 68  recedence from h
3675e 69 67 68 65 73 74 20 74 6f 0a 2a 2a 20 6c 6f 77  ighest to.** low
3675f 65 73 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  est is:.**.**   
36760 4e 45 41 52 0a 2a 2a 20 20 20 4e 4f 54 0a 2a 2a  NEAR.**   NOT.**
36761 20 20 20 41 4e 44 20 28 69 6e 63 6c 75 64 69 6e     AND (includin
36762 67 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 73 29  g implicit ANDs)
36763 0a 2a 2a 20 20 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e  .**   OR.**.** N
36764 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 75 73  ote that when us
36765 69 6e 67 20 74 68 65 20 6f 6c 64 20 71 75 65 72  ing the old quer
36766 79 20 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52  y syntax, the OR
36767 20 6f 70 65 72 61 74 6f 72 20 68 61 73 20 61 20   operator has a 
36768 68 69 67 68 65 72 0a 2a 2a 20 70 72 65 63 65 64  higher.** preced
36769 65 6e 63 65 20 74 68 61 6e 20 74 68 65 20 41 4e  ence than the AN
3676a 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  D operator..*/.s
3676b 74 61 74 69 63 20 69 6e 74 20 6f 70 50 72 65 63  tatic int opPrec
3676c 65 64 65 6e 63 65 28 46 74 73 33 45 78 70 72 20  edence(Fts3Expr 
3676d 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
3676e 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ->eType!=FTSQUER
3676f 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 69 66  Y_PHRASE );.  if
36770 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  ( sqlite3_fts3_e
36771 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
36772 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  s ){.    return 
36773 70 2d 3e 65 54 79 70 65 3b 0a 20 20 7d 65 6c 73  p->eType;.  }els
36774 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
36775 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
36776 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
36777 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
36778 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52  ype==FTSQUERY_OR
36779 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
3677a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
3677b 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
3677c 52 59 5f 41 4e 44 20 29 3b 0a 20 20 72 65 74 75  RY_AND );.  retu
3677d 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 3;.}../*.** A
3677e 72 67 75 6d 65 6e 74 20 70 70 48 65 61 64 20 63  rgument ppHead c
3677f 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
36780 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
36781 20 68 65 61 64 20 6f 66 20 61 20 71 75 65 72 79   head of a query
36782 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
36783 74 72 65 65 20 62 65 69 6e 67 20 70 61 72 73 65  tree being parse
36784 64 2e 20 70 50 72 65 76 20 69 73 20 74 68 65 20  d. pPrev is the 
36785 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
36786 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a  most recently.**
36787 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
36788 68 65 20 74 72 65 65 2e 20 54 68 69 73 20 66 75  he tree. This fu
36789 6e 63 74 69 6f 6e 20 61 64 64 73 20 70 4e 65 77  nction adds pNew
3678a 2c 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  , which is alway
3678b 73 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 6f 70  s a binary.** op
3678c 65 72 61 74 6f 72 20 6e 6f 64 65 2c 20 69 6e 74  erator node, int
3678d 6f 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  o the expression
3678e 20 74 72 65 65 20 62 61 73 65 64 20 6f 6e 20 74   tree based on t
3678f 68 65 20 72 65 6c 61 74 69 76 65 20 70 72 65 63  he relative prec
36790 65 64 65 6e 63 65 0a 2a 2a 20 6f 66 20 70 4e 65  edence.** of pNe
36791 77 20 61 6e 64 20 74 68 65 20 65 78 69 73 74 69  w and the existi
36792 6e 67 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 20  ng nodes of the 
36793 74 72 65 65 2e 20 54 68 69 73 20 6d 61 79 20 72  tree. This may r
36794 65 73 75 6c 74 20 69 6e 20 74 68 65 20 68 65 61  esult in the hea
36795 64 0a 2a 2a 20 6f 66 20 74 68 65 20 74 72 65 65  d.** of the tree
36796 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e 20 77 68   changing, in wh
36797 69 63 68 20 63 61 73 65 20 2a 70 70 48 65 61 64  ich case *ppHead
36798 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
36799 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e 0a 2a 2f  ew root node..*/
3679a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
3679b 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
3679c 72 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  r(.  Fts3Expr **
3679d 70 70 48 65 61 64 2c 20 20 20 20 20 20 20 2f 2a  ppHead,       /*
3679e 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
3679f 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 61 20 74  root node of a t
367a0 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
367a1 72 20 2a 70 50 72 65 76 2c 20 20 20 20 20 20 20  r *pPrev,       
367a2 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f 73 74 20 72    /* Node most r
367a3 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
367a4 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65 20 2a   into the tree *
367a5 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e  /.  Fts3Expr *pN
367a6 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ew           /* 
367a7 4e 65 77 20 62 69 6e 61 72 79 20 6e 6f 64 65 20  New binary node 
367a8 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 65  to insert into e
367a9 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 2a  xpression tree *
367aa 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  /.){.  Fts3Expr 
367ab 2a 70 53 70 6c 69 74 20 3d 20 70 50 72 65 76 3b  *pSplit = pPrev;
367ac 0a 20 20 77 68 69 6c 65 28 20 70 53 70 6c 69 74  .  while( pSplit
367ad 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 6f 70 50  ->pParent && opP
367ae 72 65 63 65 64 65 6e 63 65 28 70 53 70 6c 69 74  recedence(pSplit
367af 2d 3e 70 50 61 72 65 6e 74 29 3c 3d 6f 70 50 72  ->pParent)<=opPr
367b0 65 63 65 64 65 6e 63 65 28 70 4e 65 77 29 20 29  ecedence(pNew) )
367b1 7b 0a 20 20 20 20 70 53 70 6c 69 74 20 3d 20 70  {.    pSplit = p
367b2 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a  Split->pParent;.
367b3 20 20 7d 0a 0a 20 20 69 66 28 20 70 53 70 6c 69    }..  if( pSpli
367b4 74 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20  t->pParent ){.  
367b5 20 20 61 73 73 65 72 74 28 20 70 53 70 6c 69 74    assert( pSplit
367b6 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
367b7 74 3d 3d 70 53 70 6c 69 74 20 29 3b 0a 20 20 20  t==pSplit );.   
367b8 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
367b9 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b  ->pRight = pNew;
367ba 0a 20 20 20 20 70 4e 65 77 2d 3e 70 50 61 72 65  .    pNew->pPare
367bb 6e 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61  nt = pSplit->pPa
367bc 72 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rent;.  }else{. 
367bd 20 20 20 2a 70 70 48 65 61 64 20 3d 20 70 4e 65     *ppHead = pNe
367be 77 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70  w;.  }.  pNew->p
367bf 4c 65 66 74 20 3d 20 70 53 70 6c 69 74 3b 0a 20  Left = pSplit;. 
367c0 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
367c1 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pNew;.}../*.*
367c2 2a 20 50 61 72 73 65 20 74 68 65 20 66 74 73 33  * Parse the fts3
367c3 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
367c4 6e 20 66 6f 75 6e 64 20 69 6e 20 62 75 66 66 65  n found in buffe
367c5 72 20 7a 2c 20 6c 65 6e 67 74 68 20 6e 2e 20 54  r z, length n. T
367c6 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
367c7 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 77  returns either w
367c8 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74  hen the end of t
367c9 68 65 20 62 75 66 66 65 72 20 69 73 20 72 65 61  he buffer is rea
367ca 63 68 65 64 20 6f 72 20 61 6e 20 75 6e 6d 61 74  ched or an unmat
367cb 63 68 65 64 20 0a 2a 2a 20 63 6c 6f 73 69 6e 67  ched .** closing
367cc 20 62 72 61 63 6b 65 74 20 2d 20 27 29 27 20 2d   bracket - ')' -
367cd 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
367ce 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
367cf 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
367d0 69 73 20 72 65 74 75 72 6e 65 64 2c 20 2a 70 70  is returned, *pp
367d1 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
367d2 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
367d3 61 72 73 65 64 20 66 6f 72 6d 20 6f 66 20 74 68  arsed form of th
367d4 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  e expression and
367d5 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 69 73 20   *pnConsumed is 
367d6 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
367d7 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 72 65  r of.** bytes re
367d8 61 64 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a  ad from buffer z
367d9 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70  . Otherwise, *pp
367da 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 30  Expr is set to 0
367db 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   and SQLITE_NOME
367dc 4d 0a 2a 2a 20 28 6f 75 74 20 6f 66 20 6d 65 6d  M.** (out of mem
367dd 6f 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51  ory error) or SQ
367de 4c 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73  LITE_ERROR (pars
367df 65 20 65 72 72 6f 72 29 20 69 73 20 72 65 74 75  e error) is retu
367e0 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
367e1 69 6e 74 20 66 74 73 33 45 78 70 72 50 61 72 73  int fts3ExprPars
367e2 65 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  e(.  ParseContex
367e3 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
367e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
367e5 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
367e6 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
367e7 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
367e8 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
367e9 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
367ea 20 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a   MATCH query */.
367eb 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45    Fts3Expr **ppE
367ec 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
367ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
367ee 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
367ef 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
367f0 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
367f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367f2 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
367f3 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
367f4 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 46  nsumed */.){.  F
367f5 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
367f6 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
367f7 50 72 65 76 20 3d 20 30 3b 0a 20 20 46 74 73 33  Prev = 0;.  Fts3
367f8 45 78 70 72 20 2a 70 4e 6f 74 42 72 61 6e 63 68  Expr *pNotBranch
367f9 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
367fa 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 75 73 65 64      /* Only used
367fb 20 69 6e 20 6c 65 67 61 63 79 20 70 61 72 73 65   in legacy parse
367fc 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   mode */.  int n
367fd 49 6e 20 3d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20  In = n;.  const 
367fe 63 68 61 72 20 2a 7a 49 6e 20 3d 20 7a 3b 0a 20  char *zIn = z;. 
367ff 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
36800 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  _OK;.  int isReq
36801 75 69 72 65 50 68 72 61 73 65 20 3d 20 31 3b 0a  uirePhrase = 1;.
36802 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
36803 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 46  LITE_OK ){.    F
36804 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
36805 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
36806 30 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4e  0;.    rc = getN
36807 65 78 74 4e 6f 64 65 28 70 50 61 72 73 65 2c 20  extNode(pParse, 
36808 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c 20 26 6e  zIn, nIn, &p, &n
36809 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Byte);.    if( r
3680a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3680b 20 20 20 20 20 20 69 6e 74 20 69 73 50 68 72 61        int isPhra
3680c 73 65 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 21  se;..      if( !
3680d 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
3680e 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
3680f 0a 20 20 20 20 20 20 20 26 26 20 70 2d 3e 65 54  .       && p->eT
36810 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
36811 52 41 53 45 20 26 26 20 70 2d 3e 70 50 68 72 61  RASE && p->pPhra
36812 73 65 2d 3e 69 73 4e 6f 74 20 0a 20 20 20 20 20  se->isNot .     
36813 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
36814 72 65 61 74 65 20 61 6e 20 69 6d 70 6c 69 63 69  reate an implici
36815 74 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 20  t NOT operator. 
36816 2a 2f 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  */.        Fts3E
36817 78 70 72 20 2a 70 4e 6f 74 20 3d 20 73 71 6c 69  xpr *pNot = sqli
36818 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
36819 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
3681a 20 20 20 20 20 20 69 66 28 20 21 70 4e 6f 74 20        if( !pNot 
3681b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
3681c 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
3681d 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  (p);.          r
3681e 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3681f 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
36820 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a   exprparse_out;.
36821 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36822 20 20 6d 65 6d 73 65 74 28 70 4e 6f 74 2c 20 30    memset(pNot, 0
36823 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  , sizeof(Fts3Exp
36824 72 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f  r));.        pNo
36825 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  t->eType = FTSQU
36826 45 52 59 5f 4e 4f 54 3b 0a 20 20 20 20 20 20 20  ERY_NOT;.       
36827 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20   pNot->pRight = 
36828 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  p;.        if( p
36829 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20 20  NotBranch ){.   
3682a 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 4c 65         pNot->pLe
3682b 66 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b  ft = pNotBranch;
3682c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3682d 20 20 20 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20     pNotBranch = 
3682e 70 4e 6f 74 3b 0a 20 20 20 20 20 20 20 20 70 20  pNot;.        p 
3682f 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  = pPrev;.      }
36830 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
36831 74 20 65 54 79 70 65 20 3d 20 70 2d 3e 65 54 79  t eType = p->eTy
36832 70 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  pe;.        asse
36833 72 74 28 20 65 54 79 70 65 21 3d 46 54 53 51 55  rt( eType!=FTSQU
36834 45 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 21 70  ERY_PHRASE || !p
36835 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74  ->pPhrase->isNot
36836 20 29 3b 0a 20 20 20 20 20 20 20 20 69 73 50 68   );.        isPh
36837 72 61 73 65 20 3d 20 28 65 54 79 70 65 3d 3d 46  rase = (eType==F
36838 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c  TSQUERY_PHRASE |
36839 7c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  | p->pLeft);..  
3683a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 73 52        /* The isR
3683b 65 71 75 69 72 65 50 68 72 61 73 65 20 76 61 72  equirePhrase var
3683c 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
3683d 74 72 75 65 20 69 66 20 61 20 70 68 72 61 73 65  true if a phrase
3683e 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 61   or.        ** a
3683f 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  n expression con
36840 74 61 69 6e 65 64 20 69 6e 20 70 61 72 65 6e 74  tained in parent
36841 68 65 73 69 73 20 69 73 20 72 65 71 75 69 72 65  hesis is require
36842 64 2e 20 49 66 20 61 0a 20 20 20 20 20 20 20 20  d. If a.        
36843 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74  ** binary operat
36844 6f 72 20 28 41 4e 44 2c 20 4f 52 2c 20 4e 4f 54  or (AND, OR, NOT
36845 20 6f 72 20 4e 45 41 52 29 20 69 73 20 65 6e 63   or NEAR) is enc
36846 6f 75 6e 74 65 64 20 77 68 65 6e 0a 20 20 20 20  ounted when.    
36847 20 20 20 20 2a 2a 20 69 73 52 65 71 75 69 72 65      ** isRequire
36848 50 68 72 61 73 65 20 69 73 20 73 65 74 2c 20 74  Phrase is set, t
36849 68 69 73 20 69 73 20 61 20 73 79 6e 74 61 78 20  his is a syntax 
3684a 65 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a  error..        *
3684b 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69  /.        if( !i
3684c 73 50 68 72 61 73 65 20 26 26 20 69 73 52 65 71  sPhrase && isReq
3684d 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20  uirePhrase ){.  
3684e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
3684f 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
36850 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
36851 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
36852 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72         goto expr
36853 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20  parse_out;.     
36854 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20     }.  .        
36855 69 66 28 20 69 73 50 68 72 61 73 65 20 26 26 20  if( isPhrase && 
36856 21 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65  !isRequirePhrase
36857 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
36858 20 49 6e 73 65 72 74 20 61 6e 20 69 6d 70 6c 69   Insert an impli
36859 63 69 74 20 41 4e 44 20 6f 70 65 72 61 74 6f 72  cit AND operator
3685a 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 46  . */.          F
3685b 74 73 33 45 78 70 72 20 2a 70 41 6e 64 3b 0a 20  ts3Expr *pAnd;. 
3685c 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
3685d 20 70 52 65 74 20 26 26 20 70 50 72 65 76 20 29   pRet && pPrev )
3685e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64  ;.          pAnd
3685f 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
36860 63 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  c(sizeof(Fts3Exp
36861 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  r));.          i
36862 66 28 20 21 70 41 6e 64 20 29 7b 0a 20 20 20 20  f( !pAnd ){.    
36863 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
36864 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
36865 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
36866 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
36867 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
36868 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20  exprparse_out;. 
36869 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3686a 20 20 20 20 20 6d 65 6d 73 65 74 28 70 41 6e 64       memset(pAnd
3686b 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  , 0, sizeof(Fts3
3686c 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
3686d 20 20 70 41 6e 64 2d 3e 65 54 79 70 65 20 3d 20    pAnd->eType = 
3686e 46 54 53 51 55 45 52 59 5f 41 4e 44 3b 0a 20 20  FTSQUERY_AND;.  
3686f 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 42 69          insertBi
36870 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52  naryOperator(&pR
36871 65 74 2c 20 70 50 72 65 76 2c 20 70 41 6e 64 29  et, pPrev, pAnd)
36872 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 72 65  ;.          pPre
36873 76 20 3d 20 70 41 6e 64 3b 0a 20 20 20 20 20 20  v = pAnd;.      
36874 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
36875 54 68 69 73 20 74 65 73 74 20 63 61 74 63 68 65  This test catche
36876 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61  s attempts to ma
36877 6b 65 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e  ke either operan
36878 64 20 6f 66 20 61 20 4e 45 41 52 0a 20 20 20 20  d of a NEAR.    
36879 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20      ** operator 
3687a 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
3687b 74 68 61 6e 20 61 20 70 68 72 61 73 65 2e 20 46  than a phrase. F
3687c 6f 72 20 65 78 61 6d 70 6c 65 2c 20 65 69 74 68  or example, eith
3687d 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  er of.        **
3687e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
3687f 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
36880 20 20 20 2a 2a 20 20 20 20 28 62 72 61 63 6b 65     **    (bracke
36881 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29 20  ted expression) 
36882 4e 45 41 52 20 70 68 72 61 73 65 0a 20 20 20 20  NEAR phrase.    
36883 20 20 20 20 2a 2a 20 20 20 20 70 68 72 61 73 65      **    phrase
36884 20 4e 45 41 52 20 28 62 72 61 63 6b 65 74 65 64   NEAR (bracketed
36885 20 65 78 70 72 65 73 73 69 6f 6e 29 0a 20 20 20   expression).   
36886 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
36887 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
36888 6f 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73  or in either cas
36889 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
3688a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76 20        if( pPrev 
3688b 26 26 20 28 0a 20 20 20 20 20 20 20 20 20 20 20  && (.           
3688c 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52   (eType==FTSQUER
3688d 59 5f 4e 45 41 52 20 26 26 20 21 69 73 50 68 72  Y_NEAR && !isPhr
3688e 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65 54  ase && pPrev->eT
3688f 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48  ype!=FTSQUERY_PH
36890 52 41 53 45 29 0a 20 20 20 20 20 20 20 20 20 7c  RASE).         |
36891 7c 20 28 65 54 79 70 65 21 3d 46 54 53 51 55 45  | (eType!=FTSQUE
36892 52 59 5f 50 48 52 41 53 45 20 26 26 20 69 73 50  RY_PHRASE && isP
36893 68 72 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e  hrase && pPrev->
36894 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
36895 4e 45 41 52 29 0a 20 20 20 20 20 20 20 20 29 29  NEAR).        ))
36896 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
36897 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
36898 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  p);.          rc
36899 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
3689a 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
3689b 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20  exprparse_out;. 
3689c 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20         }.  .    
3689d 20 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65      if( isPhrase
3689e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3689f 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20  ( pRet ){.      
368a0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
368a1 72 65 76 20 26 26 20 70 50 72 65 76 2d 3e 70 4c  rev && pPrev->pL
368a2 65 66 74 20 26 26 20 70 50 72 65 76 2d 3e 70 52  eft && pPrev->pR
368a3 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ight==0 );.     
368a4 20 20 20 20 20 20 20 70 50 72 65 76 2d 3e 70 52         pPrev->pR
368a5 69 67 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  ight = p;.      
368a6 20 20 20 20 20 20 70 2d 3e 70 50 61 72 65 6e 74        p->pParent
368a7 20 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20   = pPrev;.      
368a8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
368a9 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 70 3b         pRet = p;
368aa 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
368ab 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
368ac 20 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e 61        insertBina
368ad 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65 74  ryOperator(&pRet
368ae 2c 20 70 50 72 65 76 2c 20 70 29 3b 0a 20 20 20  , pPrev, p);.   
368af 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
368b0 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20 3d  sRequirePhrase =
368b1 20 21 69 73 50 68 72 61 73 65 3b 0a 20 20 20 20   !isPhrase;.    
368b2 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
368b3 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 20  ( nByte>0 );.   
368b4 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72   }.    assert( r
368b5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
368b6 28 6e 42 79 74 65 3e 30 20 26 26 20 6e 42 79 74  (nByte>0 && nByt
368b7 65 3c 3d 6e 49 6e 29 20 29 3b 0a 20 20 20 20 6e  e<=nIn) );.    n
368b8 49 6e 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 20  In -= nByte;.   
368b9 20 7a 49 6e 20 2b 3d 20 6e 42 79 74 65 3b 0a 20   zIn += nByte;. 
368ba 20 20 20 70 50 72 65 76 20 3d 20 70 3b 0a 20 20     pPrev = p;.  
368bb 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
368bc 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 52 65 74  ITE_DONE && pRet
368bd 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
368be 61 73 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ase ){.    rc = 
368bf 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
368c0 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
368c1 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
368c2 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
368c3 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
368c4 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
368c5 65 6e 74 68 65 73 65 73 20 26 26 20 70 4e 6f 74  entheses && pNot
368c6 42 72 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20  Branch ){.      
368c7 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
368c8 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
368c9 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65  _ERROR;.      }e
368ca 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 46 74 73  lse{.        Fts
368cb 33 45 78 70 72 20 2a 70 49 74 65 72 20 3d 20 70  3Expr *pIter = p
368cc 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20  NotBranch;.     
368cd 20 20 20 77 68 69 6c 65 28 20 70 49 74 65 72 2d     while( pIter-
368ce 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
368cf 20 20 20 20 70 49 74 65 72 20 3d 20 70 49 74 65      pIter = pIte
368d0 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
368d1 20 20 7d 0a 20 20 20 20 20 20 20 20 70 49 74 65    }.        pIte
368d2 72 2d 3e 70 4c 65 66 74 20 3d 20 70 52 65 74 3b  r->pLeft = pRet;
368d3 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20  .        pRet = 
368d4 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20  pNotBranch;.    
368d5 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
368d6 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 20  *pnConsumed = n 
368d7 2d 20 6e 49 6e 3b 0a 0a 65 78 70 72 70 61 72 73  - nIn;..exprpars
368d8 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  e_out:.  if( rc!
368d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
368da 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
368db 72 46 72 65 65 28 70 52 65 74 29 3b 0a 20 20 20  rFree(pRet);.   
368dc 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
368dd 46 72 65 65 28 70 4e 6f 74 42 72 61 6e 63 68 29  Free(pNotBranch)
368de 3b 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a  ;.    pRet = 0;.
368df 20 20 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20    }.  *ppExpr = 
368e0 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
368e1 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  c;.}../*.** Para
368e2 6d 65 74 65 72 73 20 7a 20 61 6e 64 20 6e 20 63  meters z and n c
368e3 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
368e4 20 74 6f 20 61 6e 64 20 6c 65 6e 67 74 68 20 6f   to and length o
368e5 66 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  f a buffer conta
368e6 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 66 74 73 33  ining.** an fts3
368e7 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
368e8 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
368e9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
368ea 74 74 65 6d 70 74 73 20 74 6f 20 70 61 72 73 65  ttempts to parse
368eb 20 74 68 65 0a 2a 2a 20 71 75 65 72 79 20 65 78   the.** query ex
368ec 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 63 72 65  pression and cre
368ed 61 74 65 20 61 20 74 72 65 65 20 6f 66 20 46 74  ate a tree of Ft
368ee 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65  s3Expr structure
368ef 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  s representing t
368f0 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 65 78 70  he.** parsed exp
368f1 72 65 73 73 69 6f 6e 2e 20 49 66 20 73 75 63 63  ression. If succ
368f2 65 73 73 66 75 6c 2c 20 2a 70 70 45 78 70 72 20  essful, *ppExpr 
368f3 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
368f4 74 6f 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f  to the head.** o
368f5 66 20 74 68 65 20 70 61 72 73 65 64 20 65 78 70  f the parsed exp
368f6 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
368f7 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
368f8 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
368f9 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 65  ror.** occurs, e
368fa 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
368fb 45 4d 20 28 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72  EM (out-of-memor
368fc 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51 4c 49  y error) or SQLI
368fd 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73 65 0a  TE_ERROR (parse.
368fe 2a 2a 20 65 72 72 6f 72 29 20 69 73 20 72 65 74  ** error) is ret
368ff 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78 70  urned and *ppExp
36900 72 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a  r is set to 0..*
36901 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  *.** If paramete
36902 72 20 6e 20 69 73 20 61 20 6e 65 67 61 74 69 76  r n is a negativ
36903 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 6e 20 7a  e number, then z
36904 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70   is assumed to p
36905 6f 69 6e 74 20 74 6f 20 61 0a 2a 2a 20 6e 75 6c  oint to a.** nul
36906 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
36907 6e 67 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74  ng and the lengt
36908 68 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  h is determined 
36909 75 73 69 6e 67 20 73 74 72 6c 65 6e 28 29 2e 0a  using strlen()..
3690a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
3690b 70 61 72 61 6d 65 74 65 72 2c 20 70 54 6f 6b 65  parameter, pToke
3690c 6e 69 7a 65 72 2c 20 69 73 20 70 61 73 73 65 64  nizer, is passed
3690d 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69   the fts3 tokeni
3690e 7a 65 72 20 6d 6f 64 75 6c 65 20 74 6f 0a 2a 2a  zer module to.**
3690f 20 75 73 65 20 74 6f 20 6e 6f 72 6d 61 6c 69 7a   use to normaliz
36910 65 20 71 75 65 72 79 20 74 6f 6b 65 6e 73 20 77  e query tokens w
36911 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68 65  hile parsing the
36912 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 54 68 65   expression. The
36913 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a 20 61 72 72 61   azCol[].** arra
36914 79 2c 20 77 68 69 63 68 20 69 73 20 61 73 73 75  y, which is assu
36915 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6e  med to contain n
36916 43 6f 6c 20 65 6e 74 72 69 65 73 2c 20 73 68 6f  Col entries, sho
36917 75 6c 64 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  uld contain the 
36918 6e 61 6d 65 73 0a 2a 2a 20 6f 66 20 65 61 63 68  names.** of each
36919 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74   column in the t
3691a 61 72 67 65 74 20 66 74 73 33 20 74 61 62 6c 65  arget fts3 table
3691b 2c 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20  , in order from 
3691c 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 20 0a  left to right. .
3691d 2a 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ** Column names 
3691e 6d 75 73 74 20 62 65 20 6e 75 6c 2d 74 65 72 6d  must be nul-term
3691f 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2e 0a  inated strings..
36920 2a 2a 0a 2a 2a 20 54 68 65 20 69 44 65 66 61 75  **.** The iDefau
36921 6c 74 43 6f 6c 20 70 61 72 61 6d 65 74 65 72 20  ltCol parameter 
36922 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
36923 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
36924 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a  e table column.*
36925 2a 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f  * that appears o
36926 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d  n the left-hand-
36927 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
36928 48 20 6f 70 65 72 61 74 6f 72 20 28 74 68 65 20  H operator (the 
36929 64 65 66 61 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d  default.** colum
3692a 6e 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e  n to match again
3692b 73 74 20 66 6f 72 20 74 6f 6b 65 6e 73 20 66 6f  st for tokens fo
3692c 72 20 77 68 69 63 68 20 61 20 63 6f 6c 75 6d 6e  r which a column
3692d 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 65 78 70   name is not exp
3692e 6c 69 63 69 74 6c 79 0a 2a 2a 20 73 70 65 63 69  licitly.** speci
3692f 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  fied as part of 
36930 74 68 65 20 71 75 65 72 79 20 73 74 72 69 6e 67  the query string
36931 29 2c 20 6f 72 20 2d 31 20 69 66 20 74 6f 6b 65  ), or -1 if toke
36932 6e 73 20 6d 61 79 20 62 79 20 64 65 66 61 75 6c  ns may by defaul
36933 74 0a 2a 2a 20 6d 61 74 63 68 20 61 6e 79 20 74  t.** match any t
36934 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  able column..*/.
36935 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
36936 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  nt sqlite3Fts3Ex
36937 70 72 50 61 72 73 65 28 0a 20 20 73 71 6c 69 74  prParse(.  sqlit
36938 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
36939 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 2f  okenizer,      /
3693a 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
3693b 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
3693c 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zCol,           
3693d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
3693e 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
3693f 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
36940 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
36941 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
36942 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36943 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
36944 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
36945 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
36946 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
36947 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
36948 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
36949 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3694a 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
3694b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
3694c 74 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72 79  t of MATCH query
3694d 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
3694e 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20  *ppExpr         
3694f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
36950 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
36951 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20  tructure */.){. 
36952 20 69 6e 74 20 6e 50 61 72 73 65 64 3b 0a 20 20   int nParsed;.  
36953 69 6e 74 20 72 63 3b 0a 20 20 50 61 72 73 65 43  int rc;.  ParseC
36954 6f 6e 74 65 78 74 20 73 50 61 72 73 65 3b 0a 20  ontext sParse;. 
36955 20 73 50 61 72 73 65 2e 70 54 6f 6b 65 6e 69 7a   sParse.pTokeniz
36956 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b  er = pTokenizer;
36957 0a 20 20 73 50 61 72 73 65 2e 61 7a 43 6f 6c 20  .  sParse.azCol 
36958 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  = (const char **
36959 29 61 7a 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  )azCol;.  sParse
3695a 2e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20  .nCol = nCol;.  
3695b 73 50 61 72 73 65 2e 69 44 65 66 61 75 6c 74 43  sParse.iDefaultC
3695c 6f 6c 20 3d 20 69 44 65 66 61 75 6c 74 43 6f 6c  ol = iDefaultCol
3695d 3b 0a 20 20 73 50 61 72 73 65 2e 6e 4e 65 73 74  ;.  sParse.nNest
3695e 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 3d 3d 30   = 0;.  if( z==0
3695f 20 29 7b 0a 20 20 20 20 2a 70 70 45 78 70 72 20   ){.    *ppExpr 
36960 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
36961 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
36962 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
36963 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  n = (int)strlen(
36964 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  z);.  }.  rc = f
36965 74 73 33 45 78 70 72 50 61 72 73 65 28 26 73 50  ts3ExprParse(&sP
36966 61 72 73 65 2c 20 7a 2c 20 6e 2c 20 70 70 45 78  arse, z, n, ppEx
36967 70 72 2c 20 26 6e 50 61 72 73 65 64 29 3b 0a 0a  pr, &nParsed);..
36968 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6d    /* Check for m
36969 69 73 6d 61 74 63 68 65 64 20 70 61 72 65 6e 74  ismatched parent
3696a 68 65 73 69 73 20 2a 2f 0a 20 20 69 66 28 20 72  hesis */.  if( r
3696b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
3696c 73 50 61 72 73 65 2e 6e 4e 65 73 74 20 29 7b 0a  sParse.nNest ){.
3696d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3696e 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74  ERROR;.    sqlit
3696f 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 2a  e3Fts3ExprFree(*
36970 70 70 45 78 70 72 29 3b 0a 20 20 20 20 2a 70 70  ppExpr);.    *pp
36971 45 78 70 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Expr = 0;.  }.. 
36972 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
36973 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73  *.** Free a pars
36974 65 64 20 66 74 73 33 20 71 75 65 72 79 20 65 78  ed fts3 query ex
36975 70 72 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74  pression allocat
36976 65 64 20 62 79 20 73 71 6c 69 74 65 33 46 74 73  ed by sqlite3Fts
36977 33 45 78 70 72 50 61 72 73 65 28 29 2e 0a 2a 2f  3ExprParse()..*/
36978 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
36979 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
3697a 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70  ExprFree(Fts3Exp
3697b 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  r *p){.  if( p )
3697c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
3697d 33 45 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65  3ExprFree(p->pLe
3697e 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ft);.    sqlite3
3697f 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e  Fts3ExprFree(p->
36980 70 52 69 67 68 74 29 3b 0a 20 20 20 20 73 71 6c  pRight);.    sql
36981 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
36982 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
36983 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36984 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36985 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36986 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36987 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
36988 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36989 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3698a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3698b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3698c 0a 2a 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61  .** Everything a
3698d 66 74 65 72 20 74 68 69 73 20 70 6f 69 6e 74 20  fter this point 
3698e 69 73 20 6a 75 73 74 20 74 65 73 74 20 63 6f 64  is just test cod
3698f 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  e..*/..#ifdef SQ
36990 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
36991 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 71 75  * Function to qu
36992 65 72 79 20 74 68 65 20 68 61 73 68 2d 74 61 62  ery the hash-tab
36993 6c 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 73  le of tokenizers
36994 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
36995 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f 0a 73 74 61  enizers)..*/.sta
36996 74 69 63 20 69 6e 74 20 71 75 65 72 79 54 65 73  tic int queryTes
36997 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71  tTokenizer(.  sq
36998 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f  lite3 *db, .  co
36999 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3699a 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74    .  const sqlit
3699b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3699c 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e  ule **pp.){.  in
3699d 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
3699e 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63  stmt *pStmt;.  c
3699f 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d  onst char zSql[]
369a0 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f   = "SELECT fts3_
369a1 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a  tokenizer(?)";..
369a2 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20    *pp = 0;.  rc 
369a3 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
369a4 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
369a5 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
369a6 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
369a7 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
369a8 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
369a9 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
369aa 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
369ab 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
369ac 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
369ad 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
369ae 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
369af 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
369b0 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
369b1 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )==SQLITE_BLOB )
369b2 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 28  {.      memcpy((
369b3 76 6f 69 64 20 2a 29 70 70 2c 20 73 71 6c 69 74  void *)pp, sqlit
369b4 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
369b5 53 74 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66  Stmt, 0), sizeof
369b6 28 2a 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20  (*pp));.    }.  
369b7 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  }..  return sqli
369b8 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
369b9 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  mt);.}../*.** Th
369ba 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70  is function is p
369bb 61 72 74 20 6f 66 20 74 68 65 20 74 65 73 74 20  art of the test 
369bc 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 74 68  interface for th
369bd 65 20 71 75 65 72 79 20 70 61 72 73 65 72 2e 20  e query parser. 
369be 49 74 0a 2a 2a 20 77 72 69 74 65 73 20 61 20 74  It.** writes a t
369bf 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ext representati
369c0 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  on of the query 
369c1 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
369c2 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66   into the.** buf
369c3 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
369c4 79 20 61 72 67 75 6d 65 6e 74 20 7a 42 75 66 2e  y argument zBuf.
369c5 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
369c6 68 61 74 20 7a 42 75 66 20 69 73 20 6c 61 72 67  hat zBuf is larg
369c7 65 20 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20  e .** enough to 
369c8 73 74 6f 72 65 20 74 68 65 20 72 65 71 75 69 72  store the requir
369c9 65 64 20 74 65 78 74 20 72 65 70 72 65 73 65 6e  ed text represen
369ca 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  tation..*/.stati
369cb 63 20 76 6f 69 64 20 65 78 70 72 54 6f 53 74 72  c void exprToStr
369cc 69 6e 67 28 46 74 73 33 45 78 70 72 20 2a 70 45  ing(Fts3Expr *pE
369cd 78 70 72 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  xpr, char *zBuf)
369ce 7b 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  {.  switch( pExp
369cf 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20  r->eType ){.    
369d0 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 50 48  case FTSQUERY_PH
369d1 52 41 53 45 3a 20 7b 0a 20 20 20 20 20 20 46 74  RASE: {.      Ft
369d2 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73  s3Phrase *pPhras
369d3 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61  e = pExpr->pPhra
369d4 73 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  se;.      int i;
369d5 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73  .      zBuf += s
369d6 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 50 48  printf(zBuf, "PH
369d7 52 41 53 45 20 25 64 20 25 64 22 2c 20 70 50 68  RASE %d %d", pPh
369d8 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70  rase->iColumn, p
369d9 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74 29 3b 0a  Phrase->isNot);.
369da 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
369db 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  <pPhrase->nToken
369dc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
369dd 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28  zBuf += sprintf(
369de 7a 42 75 66 2c 22 20 25 2e 2a 73 22 2c 70 50 68  zBuf," %.*s",pPh
369df 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e  rase->aToken[i].
369e0 6e 2c 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  n,pPhrase->aToke
369e1 6e 5b 69 5d 2e 7a 29 3b 0a 20 20 20 20 20 20 20  n[i].z);.       
369e2 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
369e3 28 7a 42 75 66 2c 22 25 73 22 2c 20 28 70 50 68  (zBuf,"%s", (pPh
369e4 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e  rase->aToken[i].
369e5 69 73 50 72 65 66 69 78 3f 22 2b 22 3a 22 22 29  isPrefix?"+":"")
369e6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
369e7 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a   return;.    }..
369e8 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
369e9 59 5f 4e 45 41 52 3a 0a 20 20 20 20 20 20 7a 42  Y_NEAR:.      zB
369ea 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
369eb 75 66 2c 20 22 4e 45 41 52 2f 25 64 20 22 2c 20  uf, "NEAR/%d ", 
369ec 70 45 78 70 72 2d 3e 6e 4e 65 61 72 29 3b 0a 20  pExpr->nNear);. 
369ed 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
369ee 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 4f  case FTSQUERY_NO
369ef 54 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d  T:.      zBuf +=
369f0 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
369f1 4e 4f 54 20 22 29 3b 0a 20 20 20 20 20 20 62 72  NOT ");.      br
369f2 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54  eak;.    case FT
369f3 53 51 55 45 52 59 5f 41 4e 44 3a 0a 20 20 20 20  SQUERY_AND:.    
369f4 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
369f5 66 28 7a 42 75 66 2c 20 22 41 4e 44 20 22 29 3b  f(zBuf, "AND ");
369f6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
369f7 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f    case FTSQUERY_
369f8 4f 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  OR:.      zBuf +
369f9 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
369fa 22 4f 52 20 22 29 3b 0a 20 20 20 20 20 20 62 72  "OR ");.      br
369fb 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 7a 42 75 66  eak;.  }..  zBuf
369fc 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
369fd 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f  , "{");.  exprTo
369fe 53 74 72 69 6e 67 28 70 45 78 70 72 2d 3e 70 4c  String(pExpr->pL
369ff 65 66 74 2c 20 7a 42 75 66 29 3b 0a 20 20 7a 42  eft, zBuf);.  zB
36a00 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 42 75  uf += strlen(zBu
36a01 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70  f);.  zBuf += sp
36a02 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7d 20 22  rintf(zBuf, "} "
36a03 29 3b 0a 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70  );..  zBuf += sp
36a04 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7b 22 29  rintf(zBuf, "{")
36a05 3b 0a 20 20 65 78 70 72 54 6f 53 74 72 69 6e 67  ;.  exprToString
36a06 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  (pExpr->pRight, 
36a07 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
36a08 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20   strlen(zBuf);. 
36a09 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
36a0a 28 7a 42 75 66 2c 20 22 7d 22 29 3b 0a 7d 0a 0a  (zBuf, "}");.}..
36a0b 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
36a0c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
36a0d 20 6f 66 20 61 20 73 63 61 6c 61 72 20 53 51 4c   of a scalar SQL
36a0e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74   function used t
36a0f 6f 20 74 65 73 74 20 74 68 65 20 0a 2a 2a 20 65  o test the .** e
36a10 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 72  xpression parser
36a11 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 63  . It should be c
36a12 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
36a13 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73 33 5f 65  :.**.**   fts3_e
36a14 78 70 72 74 65 73 74 28 3c 74 6f 6b 65 6e 69 7a  xprtest(<tokeniz
36a15 65 72 3e 2c 20 3c 65 78 70 72 3e 2c 20 3c 63 6f  er>, <expr>, <co
36a16 6c 75 6d 6e 20 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a  lumn 1>, ...);.*
36a17 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
36a18 72 67 75 6d 65 6e 74 2c 20 3c 74 6f 6b 65 6e 69  rgument, <tokeni
36a19 7a 65 72 3e 2c 20 69 73 20 74 68 65 20 6e 61 6d  zer>, is the nam
36a1a 65 20 6f 66 20 74 68 65 20 66 74 73 33 20 74 6f  e of the fts3 to
36a1b 6b 65 6e 69 7a 65 72 20 75 73 65 64 0a 2a 2a 20  kenizer used.** 
36a1c 74 6f 20 70 61 72 73 65 20 74 68 65 20 71 75 65  to parse the que
36a1d 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 28 73  ry expression (s
36a1e 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69  ee README.tokeni
36a1f 7a 65 72 73 29 2e 20 54 68 65 20 73 65 63 6f 6e  zers). The secon
36a20 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73  d argument.** is
36a21 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
36a22 73 73 69 6f 6e 20 74 6f 20 70 61 72 73 65 2e 20  ssion to parse. 
36a23 45 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 20  Each subsequent 
36a24 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
36a25 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c  name.** of a col
36a26 75 6d 6e 20 6f 66 20 74 68 65 20 66 74 73 33 20  umn of the fts3 
36a27 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 71  table that the q
36a28 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  uery expression 
36a29 6d 61 79 20 72 65 66 65 72 20 74 6f 2e 0a 2a 2a  may refer to..**
36a2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
36a2b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 66 74 73  .**   SELECT fts
36a2c 33 5f 65 78 70 72 74 65 73 74 28 27 73 69 6d 70  3_exprtest('simp
36a2d 6c 65 27 2c 20 27 42 69 6c 6c 20 63 6f 6c 32 3a  le', 'Bill col2:
36a2e 42 6c 6f 67 67 73 27 2c 20 27 63 6f 6c 31 27 2c  Bloggs', 'col1',
36a2f 20 27 63 6f 6c 32 27 29 3b 0a 2a 2f 0a 73 74 61   'col2');.*/.sta
36a30 74 69 63 20 76 6f 69 64 20 66 74 73 33 45 78 70  tic void fts3Exp
36a31 72 54 65 73 74 28 0a 20 20 73 71 6c 69 74 65 33  rTest(.  sqlite3
36a32 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
36a33 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
36a34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
36a35 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74  *argv.){.  sqlit
36a36 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
36a37 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75  ule const *pModu
36a38 6c 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  le = 0;.  sqlite
36a39 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
36a3a 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a 20 20 69  kenizer = 0;.  i
36a3b 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 2a  nt rc;.  char **
36a3c 61 7a 43 6f 6c 20 3d 20 30 3b 0a 20 20 63 6f 6e  azCol = 0;.  con
36a3d 73 74 20 63 68 61 72 20 2a 7a 45 78 70 72 3b 0a  st char *zExpr;.
36a3e 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 69    int nExpr;.  i
36a3f 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 69  nt nCol;.  int i
36a40 69 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  i;.  Fts3Expr *p
36a41 45 78 70 72 3b 0a 20 20 73 71 6c 69 74 65 33 20  Expr;.  sqlite3 
36a42 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
36a43 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
36a44 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 69 66 28  context);..  if(
36a45 20 61 72 67 63 3c 33 20 29 7b 0a 20 20 20 20 73   argc<3 ){.    s
36a46 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
36a47 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20  ror(context, .  
36a48 20 20 20 20 20 20 22 55 73 61 67 65 3a 20 66 74        "Usage: ft
36a49 73 33 5f 65 78 70 72 74 65 73 74 28 74 6f 6b 65  s3_exprtest(toke
36a4a 6e 69 7a 65 72 2c 20 65 78 70 72 2c 20 63 6f 6c  nizer, expr, col
36a4b 31 2c 20 2e 2e 2e 22 2c 20 2d 31 0a 20 20 20 20  1, ...", -1.    
36a4c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
36a4d 20 7d 0a 0a 20 20 72 63 20 3d 20 71 75 65 72 79   }..  rc = query
36a4e 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28 64 62  TestTokenizer(db
36a4f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
36a50 20 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e              (con
36a51 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
36a52 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
36a53 76 5b 30 5d 29 2c 20 26 70 4d 6f 64 75 6c 65 29  v[0]), &pModule)
36a54 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
36a55 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
36a56 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
36a57 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
36a58 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  xt);.    goto ex
36a59 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 65  prtest_out;.  }e
36a5a 6c 73 65 20 69 66 28 20 21 70 4d 6f 64 75 6c 65  lse if( !pModule
36a5b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
36a5c 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
36a5d 74 65 78 74 2c 20 22 4e 6f 20 73 75 63 68 20 74  text, "No such t
36a5e 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 22  okenizer module"
36a5f 2c 20 2d 31 29 3b 0a 20 20 20 20 67 6f 74 6f 20  , -1);.    goto 
36a60 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20  exprtest_out;.  
36a61 7d 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  }..  rc = pModul
36a62 65 2d 3e 78 43 72 65 61 74 65 28 30 2c 20 30 2c  e->xCreate(0, 0,
36a63 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20   &pTokenizer);. 
36a64 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
36a65 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d  ITE_NOMEM || rc=
36a66 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
36a67 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
36a68 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69  OMEM ){.    sqli
36a69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
36a6a 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
36a6b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65  .    goto exprte
36a6c 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54  st_out;.  }.  pT
36a6d 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
36a6e 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20  e = pModule;..  
36a6f 7a 45 78 70 72 20 3d 20 28 63 6f 6e 73 74 20 63  zExpr = (const c
36a70 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
36a71 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
36a72 29 3b 0a 20 20 6e 45 78 70 72 20 3d 20 73 71 6c  );.  nExpr = sql
36a73 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
36a74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 43 6f  (argv[1]);.  nCo
36a75 6c 20 3d 20 61 72 67 63 2d 32 3b 0a 20 20 61 7a  l = argc-2;.  az
36a76 43 6f 6c 20 3d 20 28 63 68 61 72 20 2a 2a 29 73  Col = (char **)s
36a77 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43  qlite3_malloc(nC
36a78 6f 6c 2a 73 69 7a 65 6f 66 28 63 68 61 72 20 2a  ol*sizeof(char *
36a79 29 29 3b 0a 20 20 69 66 28 20 21 61 7a 43 6f 6c  ));.  if( !azCol
36a7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
36a7b 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
36a7c 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
36a7d 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
36a7e 75 74 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 69  ut;.  }.  for(ii
36a7f 3d 30 3b 20 69 69 3c 6e 43 6f 6c 3b 20 69 69 2b  =0; ii<nCol; ii+
36a80 2b 29 7b 0a 20 20 20 20 61 7a 43 6f 6c 5b 69 69  +){.    azCol[ii
36a81 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  ] = (char *)sqli
36a82 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
36a83 72 67 76 5b 69 69 2b 32 5d 29 3b 0a 20 20 7d 0a  rgv[ii+2]);.  }.
36a84 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46  .  rc = sqlite3F
36a85 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
36a86 20 20 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2c 20      pTokenizer, 
36a87 61 7a 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 6e 43 6f  azCol, nCol, nCo
36a88 6c 2c 20 7a 45 78 70 72 2c 20 6e 45 78 70 72 2c  l, zExpr, nExpr,
36a89 20 26 70 45 78 70 72 0a 20 20 29 3b 0a 20 20 69   &pExpr.  );.  i
36a8a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
36a8b 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MEM ){.    sqlit
36a8c 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
36a8d 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
36a8e 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73      goto exprtes
36a8f 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69  t_out;.  }else i
36a90 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36a91 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75   ){.    char zBu
36a92 66 5b 34 30 39 36 5d 3b 0a 20 20 20 20 65 78 70  f[4096];.    exp
36a93 72 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2c  rToString(pExpr,
36a94 20 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69   zBuf);.    sqli
36a95 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
36a96 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
36a97 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
36a98 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ENT);.    sqlite
36a99 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 45  3Fts3ExprFree(pE
36a9a 78 70 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  xpr);.  }else{. 
36a9b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36a9c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
36a9d 20 22 45 72 72 6f 72 20 70 61 72 73 69 6e 67 20   "Error parsing 
36a9e 65 78 70 72 65 73 73 69 6f 6e 22 2c 20 2d 31 29  expression", -1)
36a9f 3b 0a 20 20 7d 0a 0a 65 78 70 72 74 65 73 74 5f  ;.  }..exprtest_
36aa0 6f 75 74 3a 0a 20 20 69 66 28 20 70 4d 6f 64 75  out:.  if( pModu
36aa1 6c 65 20 26 26 20 70 54 6f 6b 65 6e 69 7a 65 72  le && pTokenizer
36aa2 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f   ){.    rc = pMo
36aa3 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 70  dule->xDestroy(p
36aa4 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a  Tokenizer);.  }.
36aa5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
36aa6 7a 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zCol);.}../*.** 
36aa7 52 65 67 69 73 74 65 72 20 74 68 65 20 71 75 65  Register the que
36aa8 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61  ry expression pa
36aa9 72 73 65 72 20 74 65 73 74 20 66 75 6e 63 74 69  rser test functi
36aaa 6f 6e 20 66 74 73 33 5f 65 78 70 72 74 65 73 74  on fts3_exprtest
36aab 28 29 20 0a 2a 2a 20 77 69 74 68 20 64 61 74 61  () .** with data
36aac 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
36aad 64 62 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  db. .*/.SQLITE_P
36aae 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
36aaf 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74 54  te3Fts3ExprInitT
36ab0 65 73 74 49 6e 74 65 72 66 61 63 65 28 73 71 6c  estInterface(sql
36ab1 69 74 65 33 2a 20 64 62 29 7b 0a 20 20 73 71 6c  ite3* db){.  sql
36ab2 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
36ab3 74 69 6f 6e 28 0a 20 20 20 20 20 20 64 62 2c 20  tion(.      db, 
36ab4 22 66 74 73 33 5f 65 78 70 72 74 65 73 74 22 2c  "fts3_exprtest",
36ab5 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
36ab6 2c 20 30 2c 20 66 74 73 33 45 78 70 72 54 65 73  , 0, fts3ExprTes
36ab7 74 2c 20 30 2c 20 30 0a 20 20 29 3b 0a 7d 0a 0a  t, 0, 0.  );.}..
36ab8 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a  #endif.#endif /*
36ab9 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
36aba 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
36abb 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
36abc 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  FTS3) */../*****
36abd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
36abe 20 66 74 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a   fts3_expr.c ***
36abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ac1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
36ac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
36ac3 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 63  file fts3_hash.c
36ac4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
36ac5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ac6 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
36ac7 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
36ac8 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
36ac9 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
36aca 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
36acb 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
36acc 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
36acd 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
36ace 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
36acf 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
36ad0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
36ad1 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
36ad2 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
36ad3 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
36ad4 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
36ad5 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
36ad6 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
36ad7 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
36ad8 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
36ad9 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
36ada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36adb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36adc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36add 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ade 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
36adf 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
36ae0 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74  f generic hash-t
36ae1 61 62 6c 65 73 20 75 73 65 64 20 69 6e 20 53 51  ables used in SQ
36ae2 4c 69 74 65 2e 0a 2a 2a 20 57 65 27 76 65 20 6d  Lite..** We've m
36ae3 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69 67 68  odified it sligh
36ae4 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73 20  tly to serve as 
36ae5 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20 68 61 73  a standalone has
36ae6 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65  h table.** imple
36ae7 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68  mentation for th
36ae8 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
36ae9 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  xing module..*/.
36aea 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
36aeb 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
36aec 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66  only compiled if
36aed 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  :.**.**     * Th
36aee 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
36aef 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
36af0 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  an extension.** 
36af1 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
36af2 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
36af3 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
36af4 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  , or.**.**     *
36af5 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
36af6 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
36af7 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66  into the core of
36af8 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65  .**       SQLite
36af9 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
36afa 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
36afb 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a  S3 is defined)..
36afc 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
36afd 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
36afe 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
36aff 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f  NABLE_FTS3)..../
36b00 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 61 6e 64 20  *.** Malloc and 
36b01 46 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  Free functions.*
36b02 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66  /.static void *f
36b03 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 69 6e  ts3HashMalloc(in
36b04 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20  t n){.  void *p 
36b05 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
36b06 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  (n);.  if( p ){.
36b07 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c      memset(p, 0,
36b08 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   n);.  }.  retur
36b09 6e 20 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  n p;.}.static vo
36b0a 69 64 20 66 74 73 33 48 61 73 68 46 72 65 65 28  id fts3HashFree(
36b0b 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69  void *p){.  sqli
36b0c 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a  te3_free(p);.}..
36b0d 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d  /* Turn bulk mem
36b0e 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20  ory into a hash 
36b0f 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20  table object by 
36b10 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
36b11 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68  .** fields of th
36b12 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 65  e Hash structure
36b13 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69  ..**.** "pNew" i
36b14 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
36b15 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 68  he hash table th
36b16 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74  at is to be init
36b17 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6b 65 79 43  ialized..** keyC
36b18 6c 61 73 73 20 69 73 20 6f 6e 65 20 6f 66 20 74  lass is one of t
36b19 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 0a 2a 2a  he constants .**
36b1a 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52   FTS3_HASH_BINAR
36b1b 59 20 6f 72 20 46 54 53 33 5f 48 41 53 48 5f 53  Y or FTS3_HASH_S
36b1c 54 52 49 4e 47 2e 20 20 54 68 65 20 76 61 6c 75  TRING.  The valu
36b1d 65 20 6f 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a  e of keyClass .*
36b1e 2a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 61  * determines wha
36b1f 74 20 6b 69 6e 64 20 6f 66 20 6b 65 79 20 74 68  t kind of key th
36b20 65 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c  e hash table wil
36b21 6c 20 75 73 65 2e 20 20 22 63 6f 70 79 4b 65 79  l use.  "copyKey
36b22 22 20 69 73 0a 2a 2a 20 74 72 75 65 20 69 66 20  " is.** true if 
36b23 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73  the hash table s
36b24 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f  hould make its o
36b25 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20  wn private copy 
36b26 6f 66 20 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66  of keys and.** f
36b27 61 6c 73 65 20 69 66 20 69 74 20 73 68 6f 75 6c  alse if it shoul
36b28 64 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 73  d just use the s
36b29 75 70 70 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e  upplied pointer.
36b2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
36b2b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
36b2c 74 73 33 48 61 73 68 49 6e 69 74 28 46 74 73 33  ts3HashInit(Fts3
36b2d 48 61 73 68 20 2a 70 4e 65 77 2c 20 63 68 61 72  Hash *pNew, char
36b2e 20 6b 65 79 43 6c 61 73 73 2c 20 63 68 61 72 20   keyClass, char 
36b2f 63 6f 70 79 4b 65 79 29 7b 0a 20 20 61 73 73 65  copyKey){.  asse
36b30 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20  rt( pNew!=0 );. 
36b31 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73   assert( keyClas
36b32 73 3e 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52  s>=FTS3_HASH_STR
36b33 49 4e 47 20 26 26 20 6b 65 79 43 6c 61 73 73 3c  ING && keyClass<
36b34 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52  =FTS3_HASH_BINAR
36b35 59 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65 79  Y );.  pNew->key
36b36 43 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61 73 73  Class = keyClass
36b37 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65  ;.  pNew->copyKe
36b38 79 20 3d 20 63 6f 70 79 4b 65 79 3b 0a 20 20 70  y = copyKey;.  p
36b39 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a  New->first = 0;.
36b3a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20    pNew->count = 
36b3b 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a  0;.  pNew->htsiz
36b3c 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68  e = 0;.  pNew->h
36b3d 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d  t = 0;.}../* Rem
36b3e 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  ove all entries 
36b3f 66 72 6f 6d 20 61 20 68 61 73 68 20 74 61 62 6c  from a hash tabl
36b40 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20  e.  Reclaim all 
36b41 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20  memory..** Call 
36b42 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
36b43 64 65 6c 65 74 65 20 61 20 68 61 73 68 20 74 61  delete a hash ta
36b44 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 74 20  ble or to reset 
36b45 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20  a hash table.** 
36b46 74 6f 20 74 68 65 20 65 6d 70 74 79 20 73 74 61  to the empty sta
36b47 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  te..*/.SQLITE_PR
36b48 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
36b49 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
36b4a 46 74 73 33 48 61 73 68 20 2a 70 48 29 7b 0a 20  Fts3Hash *pH){. 
36b4b 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65   Fts3HashElem *e
36b4c 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lem;         /* 
36b4d 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
36b4e 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   all elements of
36b4f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20   the table */.. 
36b50 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
36b51 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66  ;.  elem = pH->f
36b52 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73  irst;.  pH->firs
36b53 74 20 3d 20 30 3b 0a 20 20 66 74 73 33 48 61 73  t = 0;.  fts3Has
36b54 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20  hFree(pH->ht);. 
36b55 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70   pH->ht = 0;.  p
36b56 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20  H->htsize = 0;. 
36b57 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a   while( elem ){.
36b58 20 20 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d      Fts3HashElem
36b59 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c   *next_elem = el
36b5a 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 66  em->next;.    if
36b5b 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
36b5c 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20   elem->pKey ){. 
36b5d 20 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65       fts3HashFre
36b5e 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20  e(elem->pKey);. 
36b5f 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61 73     }.    fts3Has
36b60 68 46 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20  hFree(elem);.   
36b61 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65   elem = next_ele
36b62 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75  m;.  }.  pH->cou
36b63 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  nt = 0;.}../*.**
36b64 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72   Hash and compar
36b65 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77  ison functions w
36b66 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20  hen the mode is 
36b67 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
36b68 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
36b69 74 73 33 53 74 72 48 61 73 68 28 63 6f 6e 73 74  ts3StrHash(const
36b6a 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74   void *pKey, int
36b6b 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73 74 20   nKey){.  const 
36b6c 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
36b6d 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
36b6e 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69 66 28  int h = 0;.  if(
36b6f 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65 79 20   nKey<=0 ) nKey 
36b70 3d 20 28 69 6e 74 29 20 73 74 72 6c 65 6e 28 7a  = (int) strlen(z
36b71 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79  );.  while( nKey
36b72 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d   > 0  ){.    h =
36b73 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 7a   (h<<3) ^ h ^ *z
36b74 2b 2b 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a  ++;.    nKey--;.
36b75 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26    }.  return h &
36b76 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73   0x7fffffff;.}.s
36b77 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74  tatic int fts3St
36b78 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 76  rCompare(const v
36b79 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20  oid *pKey1, int 
36b7a 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  n1, const void *
36b7b 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a  pKey2, int n2){.
36b7c 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20 72    if( n1!=n2 ) r
36b7d 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72  eturn 1;.  retur
36b7e 6e 20 73 74 72 6e 63 6d 70 28 28 63 6f 6e 73 74  n strncmp((const
36b7f 20 63 68 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f   char*)pKey1,(co
36b80 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 32 2c  nst char*)pKey2,
36b81 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61  n1);.}../*.** Ha
36b82 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f  sh and compariso
36b83 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e  n functions when
36b84 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53   the mode is FTS
36b85 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 0a 2a 2f  3_HASH_BINARY.*/
36b86 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
36b87 42 69 6e 48 61 73 68 28 63 6f 6e 73 74 20 76 6f  BinHash(const vo
36b88 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
36b89 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30  ey){.  int h = 0
36b8a 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
36b8b 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  z = (const char 
36b8c 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c 65 28  *)pKey;.  while(
36b8d 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b 0a 20   nKey-- > 0 ){. 
36b8e 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20     h = (h<<3) ^ 
36b8f 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a  h ^ *(z++);.  }.
36b90 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37    return h & 0x7
36b91 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69  fffffff;.}.stati
36b92 63 20 69 6e 74 20 66 74 73 33 42 69 6e 43 6f 6d  c int fts3BinCom
36b93 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20  pare(const void 
36b94 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20  *pKey1, int n1, 
36b95 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
36b96 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66  2, int n2){.  if
36b97 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72  ( n1!=n2 ) retur
36b98 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d 65  n 1;.  return me
36b99 6d 63 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79 32  mcmp(pKey1,pKey2
36b9a 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ,n1);.}../*.** R
36b9b 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
36b9c 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
36b9d 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  te hash function
36b9e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63   given the key c
36b9f 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lass..**.** The 
36ba0 43 20 73 79 6e 74 61 78 20 69 6e 20 74 68 69 73  C syntax in this
36ba1 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
36ba2 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66 61  tion may be unfa
36ba3 6d 69 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a 2a  milar to some .*
36ba4 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20 73  * programmers, s
36ba5 6f 20 77 65 20 70 72 6f 76 69 64 65 20 74 68 65  o we provide the
36ba6 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69 74   following addit
36ba7 69 6f 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69 6f  ional explanatio
36ba8 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  n:.**.** The nam
36ba9 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
36baa 6e 20 69 73 20 22 66 74 73 48 61 73 68 46 75 6e  n is "ftsHashFun
36bab 63 74 69 6f 6e 22 2e 20 20 54 68 65 20 66 75 6e  ction".  The fun
36bac 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a 2a 2a  ction takes a.**
36bad 20 73 69 6e 67 6c 65 20 70 61 72 61 6d 65 74 65   single paramete
36bae 72 20 22 6b 65 79 43 6c 61 73 73 22 2e 20 20 54  r "keyClass".  T
36baf 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
36bb0 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63 74 69  of ftsHashFuncti
36bb1 6f 6e 28 29 0a 2a 2a 20 69 73 20 61 20 70 6f 69  on().** is a poi
36bb2 6e 74 65 72 20 74 6f 20 61 6e 6f 74 68 65 72 20  nter to another 
36bb3 66 75 6e 63 74 69 6f 6e 2e 20 20 53 70 65 63 69  function.  Speci
36bb4 66 69 63 61 6c 6c 79 2c 20 74 68 65 20 72 65 74  fically, the ret
36bb5 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
36bb6 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
36bb7 29 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ) is a pointer t
36bb8 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  o a function tha
36bb9 74 20 74 61 6b 65 73 20 74 77 6f 20 70 61 72 61  t takes two para
36bba 6d 65 74 65 72 73 0a 2a 2a 20 77 69 74 68 20 74  meters.** with t
36bbb 79 70 65 73 20 22 63 6f 6e 73 74 20 76 6f 69 64  ypes "const void
36bbc 2a 22 20 61 6e 64 20 22 69 6e 74 22 20 61 6e 64  *" and "int" and
36bbd 20 72 65 74 75 72 6e 73 20 61 6e 20 22 69 6e 74   returns an "int
36bbe 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  "..*/.static int
36bbf 20 28 2a 66 74 73 48 61 73 68 46 75 6e 63 74 69   (*ftsHashFuncti
36bc0 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73 29  on(int keyClass)
36bc1 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
36bc2 74 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c 61  t){.  if( keyCla
36bc3 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53 54  ss==FTS3_HASH_ST
36bc4 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74 75  RING ){.    retu
36bc5 72 6e 20 26 66 74 73 33 53 74 72 48 61 73 68 3b  rn &fts3StrHash;
36bc6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
36bc7 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d  sert( keyClass==
36bc8 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
36bc9 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   );.    return &
36bca 66 74 73 33 42 69 6e 48 61 73 68 3b 0a 20 20 7d  fts3BinHash;.  }
36bcb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
36bcc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
36bcd 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61  e appropriate ha
36bce 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  sh function give
36bcf 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e  n the key class.
36bd0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 68 65 6c 70 20  .**.** For help 
36bd1 69 6e 20 69 6e 74 65 72 70 72 65 74 65 64 20 74  in interpreted t
36bd2 68 65 20 6f 62 73 63 75 72 65 20 43 20 63 6f 64  he obscure C cod
36bd3 65 20 69 6e 20 74 68 65 20 66 75 6e 63 74 69 6f  e in the functio
36bd4 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 0a 2a 2a  n definition,.**
36bd5 20 73 65 65 20 74 68 65 20 68 65 61 64 65 72 20   see the header 
36bd6 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 70  comment on the p
36bd7 72 65 76 69 6f 75 73 20 66 75 6e 63 74 69 6f 6e  revious function
36bd8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36bd9 28 2a 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63  (*ftsCompareFunc
36bda 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73  tion(int keyClas
36bdb 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  s))(const void*,
36bdc 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
36bdd 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79 43  int){.  if( keyC
36bde 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
36bdf 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65  STRING ){.    re
36be0 74 75 72 6e 20 26 66 74 73 33 53 74 72 43 6f 6d  turn &fts3StrCom
36be1 70 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pare;.  }else{. 
36be2 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c     assert( keyCl
36be3 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42  ass==FTS3_HASH_B
36be4 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74  INARY );.    ret
36be5 75 72 6e 20 26 66 74 73 33 42 69 6e 43 6f 6d 70  urn &fts3BinComp
36be6 61 72 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c  are;.  }.}../* L
36be7 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69  ink an element i
36be8 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62  nto the hash tab
36be9 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  le.*/.static voi
36bea 64 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74  d fts3HashInsert
36beb 45 6c 65 6d 65 6e 74 28 0a 20 20 46 74 73 33 48  Element(.  Fts3H
36bec 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
36bed 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c      /* The compl
36bee 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  ete hash table *
36bef 2f 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  /.  struct _fts3
36bf0 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 2f 2a 20  ht *pEntry,  /* 
36bf1 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 77  The entry into w
36bf2 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e 73  hich pNew is ins
36bf3 65 72 74 65 64 20 2a 2f 0a 20 20 46 74 73 33 48  erted */.  Fts3H
36bf4 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20  ashElem *pNew   
36bf5 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65      /* The eleme
36bf6 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  nt to be inserte
36bf7 64 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61  d */.){.  Fts3Ha
36bf8 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20  shElem *pHead;  
36bf9 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d     /* First elem
36bfa 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70  ent already in p
36bfb 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65 61 64  Entry */.  pHead
36bfc 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e   = pEntry->chain
36bfd 3b 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 7b  ;.  if( pHead ){
36bfe 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
36bff 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65  = pHead;.    pNe
36c00 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d  w->prev = pHead-
36c01 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70  >prev;.    if( p
36c02 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48  Head->prev ){ pH
36c03 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20  ead->prev->next 
36c04 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c  = pNew; }.    el
36c05 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  se             {
36c06 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65   pH->first = pNe
36c07 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e  w; }.    pHead->
36c08 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  prev = pNew;.  }
36c09 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  else{.    pNew->
36c0a 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74  next = pH->first
36c0b 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69  ;.    if( pH->fi
36c0c 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74  rst ){ pH->first
36c0d 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d  ->prev = pNew; }
36c0e 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20  .    pNew->prev 
36c0f 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72  = 0;.    pH->fir
36c10 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  st = pNew;.  }. 
36c11 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b   pEntry->count++
36c12 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  ;.  pEntry->chai
36c13 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a  n = pNew;.}.../*
36c14 20 52 65 73 69 7a 65 20 74 68 65 20 68 61 73 68   Resize the hash
36c15 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69   table so that i
36c16 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f  t cantains "new_
36c17 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e 0a 2a  size" buckets..*
36c18 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d 75 73  * "new_size" mus
36c19 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
36c1a 32 2e 20 20 54 68 65 20 68 61 73 68 20 74 61 62  2.  The hash tab
36c1b 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20 0a 2a  le might fail .*
36c1c 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73  * to resize if s
36c1d 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 66 61  qliteMalloc() fa
36c1e 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ils..**.** Retur
36c1f 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20  n non-zero if a 
36c20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
36c21 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
36c22 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
36c23 73 33 52 65 68 61 73 68 28 46 74 73 33 48 61 73  s3Rehash(Fts3Has
36c24 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77 5f 73  h *pH, int new_s
36c25 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  ize){.  struct _
36c26 66 74 73 33 68 74 20 2a 6e 65 77 5f 68 74 3b 20  fts3ht *new_ht; 
36c27 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
36c28 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a  new hash table *
36c29 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  /.  Fts3HashElem
36c2a 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c   *elem, *next_el
36c2b 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  em;  /* For loop
36c2c 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e  ing over existin
36c2d 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  g elements */.  
36c2e 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
36c2f 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20  st void*,int);  
36c30 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
36c31 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65  ction */..  asse
36c32 72 74 28 20 28 6e 65 77 5f 73 69 7a 65 20 26 20  rt( (new_size & 
36c33 28 6e 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d 30  (new_size-1))==0
36c34 20 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28   );.  new_ht = (
36c35 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a  struct _fts3ht *
36c36 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28  )fts3HashMalloc(
36c37 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66   new_size*sizeof
36c38 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 29  (struct _fts3ht)
36c39 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 68 74   );.  if( new_ht
36c3a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
36c3b 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28 70    fts3HashFree(p
36c3c 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74  H->ht);.  pH->ht
36c3d 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d   = new_ht;.  pH-
36c3e 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69  >htsize = new_si
36c3f 7a 65 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74  ze;.  xHash = ft
36c40 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48  sHashFunction(pH
36c41 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 66  ->keyClass);.  f
36c42 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73  or(elem=pH->firs
36c43 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20  t, pH->first=0; 
36c44 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78  elem; elem = nex
36c45 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74  t_elem){.    int
36c46 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 65 6c   h = (*xHash)(el
36c47 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e  em->pKey, elem->
36c48 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a  nKey) & (new_siz
36c49 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65  e-1);.    next_e
36c4a 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  lem = elem->next
36c4b 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68 49 6e  ;.    fts3HashIn
36c4c 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
36c4d 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d  &new_ht[h], elem
36c4e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
36c4f 30 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75  0;.}../* This fu
36c50 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65  nction (for inte
36c51 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c  rnal use only) l
36c52 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e  ocates an elemen
36c53 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20  t in an.** hash 
36c54 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68  table that match
36c55 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79  es the given key
36c56 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f 72 20  .  The hash for 
36c57 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20  this key has.** 
36c58 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d  already been com
36c59 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 61 73  puted and is pas
36c5a 73 65 64 20 61 73 20 74 68 65 20 34 74 68 20 70  sed as the 4th p
36c5b 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61  arameter..*/.sta
36c5c 74 69 63 20 46 74 73 33 48 61 73 68 45 6c 65 6d  tic Fts3HashElem
36c5d 20 2a 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e   *fts3FindElemen
36c5e 74 42 79 48 61 73 68 28 0a 20 20 63 6f 6e 73 74  tByHash(.  const
36c5f 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20 2f   Fts3Hash *pH, /
36c60 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73  * The pH to be s
36c61 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e  earched */.  con
36c62 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20  st void *pKey,  
36c63 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61   /* The key we a
36c64 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72  re searching for
36c65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 0a   */.  int nKey,.
36c66 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20    int h         
36c67 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73        /* The has
36c68 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20  h for this key. 
36c69 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61 73 68  */.){.  Fts3Hash
36c6a 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20  Elem *elem;     
36c6b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
36c6c 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
36c6d 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
36c6e 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
36c6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
36c71 65 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74  ements left to t
36c72 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  est */.  int (*x
36c73 43 6f 6d 70 61 72 65 29 28 63 6f 6e 73 74 20 76  Compare)(const v
36c74 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
36c75 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 63  oid*,int);  /* c
36c76 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
36c77 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d  on */..  if( pH-
36c78 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63  >ht ){.    struc
36c79 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72  t _fts3ht *pEntr
36c7a 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a  y = &pH->ht[h];.
36c7b 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72      elem = pEntr
36c7c 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f  y->chain;.    co
36c7d 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f  unt = pEntry->co
36c7e 75 6e 74 3b 0a 20 20 20 20 78 43 6f 6d 70 61 72  unt;.    xCompar
36c7f 65 20 3d 20 66 74 73 43 6f 6d 70 61 72 65 46 75  e = ftsCompareFu
36c80 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c  nction(pH->keyCl
36c81 61 73 73 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  ass);.    while(
36c82 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d   count-- && elem
36c83 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 2a   ){.      if( (*
36c84 78 43 6f 6d 70 61 72 65 29 28 65 6c 65 6d 2d 3e  xCompare)(elem->
36c85 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c  pKey,elem->nKey,
36c86 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b  pKey,nKey)==0 ){
36c87 20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e   .        return
36c88 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20   elem;.      }. 
36c89 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d       elem = elem
36c8a 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  ->next;.    }.  
36c8b 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
36c8c 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e  ./* Remove a sin
36c8d 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74  gle entry from t
36c8e 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
36c8f 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ven a pointer to
36c90 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74   that.** element
36c91 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74   and a hash on t
36c92 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79  he element's key
36c93 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
36c94 20 66 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65   fts3RemoveEleme
36c95 6e 74 42 79 48 61 73 68 28 0a 20 20 46 74 73 33  ntByHash(.  Fts3
36c96 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20  Hash *pH,       
36c97 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74    /* The pH cont
36c98 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f  aining "elem" */
36c99 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 2a  .  Fts3HashElem*
36c9a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20   elem,   /* The 
36c9b 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65  element to be re
36c9c 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70  moved from the p
36c9d 48 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20 20 20  H */.  int h    
36c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36c9f 48 61 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74  Hash value for t
36ca0 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b  he element */.){
36ca1 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68  .  struct _fts3h
36ca2 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28  t *pEntry;.  if(
36ca3 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20   elem->prev ){. 
36ca4 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e     elem->prev->n
36ca5 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ext = elem->next
36ca6 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ; .  }else{.    
36ca7 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d  pH->first = elem
36ca8 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66  ->next;.  }.  if
36ca9 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a  ( elem->next ){.
36caa 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e      elem->next->
36cab 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65  prev = elem->pre
36cac 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20  v;.  }.  pEntry 
36cad 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20  = &pH->ht[h];.  
36cae 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  if( pEntry->chai
36caf 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70  n==elem ){.    p
36cb0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65  Entry->chain = e
36cb1 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
36cb2 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d   pEntry->count--
36cb3 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e  ;.  if( pEntry->
36cb4 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20  count<=0 ){.    
36cb5 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20  pEntry->chain = 
36cb6 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d  0;.  }.  if( pH-
36cb7 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d  >copyKey && elem
36cb8 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 66 74  ->pKey ){.    ft
36cb9 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 2d  s3HashFree(elem-
36cba 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 66 74  >pKey);.  }.  ft
36cbb 73 33 48 61 73 68 46 72 65 65 28 20 65 6c 65 6d  s3HashFree( elem
36cbc 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d   );.  pH->count-
36cbd 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75  -;.  if( pH->cou
36cbe 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  nt<=0 ){.    ass
36cbf 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d  ert( pH->first==
36cc0 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
36cc1 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b   pH->count==0 );
36cc2 0a 20 20 20 20 66 74 73 33 48 61 73 68 43 6c 65  .    fts3HashCle
36cc3 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ar(pH);.  }.}../
36cc4 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63  * Attempt to loc
36cc5 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f  ate an element o
36cc6 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
36cc7 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a   pH with a key.*
36cc8 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70  * that matches p
36cc9 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72  Key,nKey.  Retur
36cca 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  n the data for t
36ccb 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69  his element if i
36ccc 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f  t is.** found, o
36ccd 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
36cce 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a  is no match..*/.
36ccf 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
36cd0 6f 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33  oid *sqlite3Fts3
36cd1 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 46  HashFind(const F
36cd2 74 73 33 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e  ts3Hash *pH, con
36cd3 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
36cd4 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20  nt nKey){.  int 
36cd5 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
36cd6 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20     /* A hash on 
36cd7 6b 65 79 20 2a 2f 0a 20 20 46 74 73 33 48 61 73  key */.  Fts3Has
36cd8 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
36cd9 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74  /* The element t
36cda 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20  hat matches key 
36cdb 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
36cdc 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
36cdd 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68  t);  /* The hash
36cde 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
36cdf 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20 70 48 2d  if( pH==0 || pH-
36ce0 3e 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  >ht==0 ) return 
36ce1 30 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73  0;.  xHash = fts
36ce2 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d  HashFunction(pH-
36ce3 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73  >keyClass);.  as
36ce4 73 65 72 74 28 20 78 48 61 73 68 21 3d 30 20 29  sert( xHash!=0 )
36ce5 3b 0a 20 20 68 20 3d 20 28 2a 78 48 61 73 68 29  ;.  h = (*xHash)
36ce6 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 20 61  (pKey,nKey);.  a
36ce7 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69  ssert( (pH->htsi
36ce8 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  ze & (pH->htsize
36ce9 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 65 6c 65  -1))==0 );.  ele
36cea 6d 20 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d  m = fts3FindElem
36ceb 65 6e 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65  entByHash(pH,pKe
36cec 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28 70 48 2d  y,nKey, h & (pH-
36ced 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a 20 20 72  >htsize-1));.  r
36cee 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65  eturn elem ? ele
36cef 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a  m->data : 0;.}..
36cf0 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65  /* Insert an ele
36cf1 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
36cf2 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68  sh table pH.  Th
36cf3 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b  e key is pKey,nK
36cf4 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  ey.** and the da
36cf5 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a  ta is "data"..**
36cf6 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e  .** If no elemen
36cf7 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20  t exists with a 
36cf8 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68  matching key, th
36cf9 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d  en a new.** elem
36cfa 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 2e 20  ent is created. 
36cfb 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b   A copy of the k
36cfc 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68  ey is made if th
36cfd 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61  e copyKey.** fla
36cfe 67 20 69 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20  g is set.  NULL 
36cff 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
36d00 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c  ** If another el
36d01 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78  ement already ex
36d02 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61  ists with the sa
36d03 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65  me key, then the
36d04 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70  .** new data rep
36d05 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61  laces the old da
36d06 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64  ta and the old d
36d07 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ata is returned.
36d08 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e  .** The key is n
36d09 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69  ot copied in thi
36d0a 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20  s instance.  If 
36d0b 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  a malloc fails, 
36d0c 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20  then.** the new 
36d0d 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
36d0e 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61   and the hash ta
36d0f 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
36d10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
36d11 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20  data" parameter 
36d12 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
36d13 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
36d14 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f  he.** element co
36d15 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22  rresponding to "
36d16 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20  key" is removed 
36d17 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
36d18 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
36d19 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
36d1a 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
36d1b 72 74 28 0a 20 20 46 74 73 33 48 61 73 68 20 2a  rt(.  Fts3Hash *
36d1c 70 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  pH,        /* Th
36d1d 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20  e hash table to 
36d1e 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20  insert into */. 
36d1f 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
36d20 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
36d21 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20   */.  int nKey, 
36d22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
36d23 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
36d24 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f   the key */.  vo
36d25 69 64 20 2a 64 61 74 61 20 20 20 20 20 20 20 20  id *data        
36d26 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a     /* The data *
36d27 2f 0a 29 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b  /.){.  int hraw;
36d28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d29 20 2f 2a 20 52 61 77 20 68 61 73 68 20 76 61 6c   /* Raw hash val
36d2a 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f  ue of the key */
36d2b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  .  int h;       
36d2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36d2d 74 68 65 20 68 61 73 68 20 6f 66 20 74 68 65 20  the hash of the 
36d2e 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20  key modulo hash 
36d2f 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20  table size */.  
36d30 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c  Fts3HashElem *el
36d31 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65  em;       /* Use
36d32 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74  d to loop thru t
36d33 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20  he element list 
36d34 2a 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65  */.  Fts3HashEle
36d35 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f  m *new_elem;   /
36d36 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64  * New element ad
36d37 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f  ded to the pH */
36d38 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28  .  int (*xHash)(
36d39 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
36d3a 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66  ;  /* The hash f
36d3b 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73  unction */..  as
36d3c 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20  sert( pH!=0 );. 
36d3d 20 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68   xHash = ftsHash
36d3e 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
36d3f 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74  Class);.  assert
36d40 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20  ( xHash!=0 );.  
36d41 68 72 61 77 20 3d 20 28 2a 78 48 61 73 68 29 28  hraw = (*xHash)(
36d42 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61  pKey, nKey);.  a
36d43 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69  ssert( (pH->htsi
36d44 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  ze & (pH->htsize
36d45 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d  -1))==0 );.  h =
36d46 20 68 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73   hraw & (pH->hts
36d47 69 7a 65 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d  ize-1);.  elem =
36d48 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74   fts3FindElement
36d49 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e  ByHash(pH,pKey,n
36d4a 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c  Key,h);.  if( el
36d4b 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  em ){.    void *
36d4c 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d  old_data = elem-
36d4d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64  >data;.    if( d
36d4e 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ata==0 ){.      
36d4f 66 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e  fts3RemoveElemen
36d50 74 42 79 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c  tByHash(pH,elem,
36d51 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  h);.    }else{. 
36d52 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20       elem->data 
36d53 3d 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  = data;.    }.  
36d54 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74    return old_dat
36d55 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61 74  a;.  }.  if( dat
36d56 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  a==0 ) return 0;
36d57 0a 20 20 69 66 28 20 28 70 48 2d 3e 68 74 73 69  .  if( (pH->htsi
36d58 7a 65 3d 3d 30 20 26 26 20 66 74 73 33 52 65 68  ze==0 && fts3Reh
36d59 61 73 68 28 70 48 2c 38 29 29 0a 20 20 20 7c 7c  ash(pH,8)).   ||
36d5a 20 28 70 48 2d 3e 63 6f 75 6e 74 3e 3d 70 48 2d   (pH->count>=pH-
36d5b 3e 68 74 73 69 7a 65 20 26 26 20 66 74 73 33 52  >htsize && fts3R
36d5c 65 68 61 73 68 28 70 48 2c 20 70 48 2d 3e 68 74  ehash(pH, pH->ht
36d5d 73 69 7a 65 2a 32 29 29 0a 20 20 29 7b 0a 20 20  size*2)).  ){.  
36d5e 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b    pH->count = 0;
36d5f 0a 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61  .    return data
36d60 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
36d61 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a  pH->htsize>0 );.
36d62 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 46 74    new_elem = (Ft
36d63 73 33 48 61 73 68 45 6c 65 6d 2a 29 66 74 73 33  s3HashElem*)fts3
36d64 48 61 73 68 4d 61 6c 6c 6f 63 28 20 73 69 7a 65  HashMalloc( size
36d65 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d 29  of(Fts3HashElem)
36d66 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c   );.  if( new_el
36d67 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64  em==0 ) return d
36d68 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63  ata;.  if( pH->c
36d69 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d  opyKey && pKey!=
36d6a 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65  0 ){.    new_ele
36d6b 6d 2d 3e 70 4b 65 79 20 3d 20 66 74 73 33 48 61  m->pKey = fts3Ha
36d6c 73 68 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29  shMalloc( nKey )
36d6d 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 5f 65 6c  ;.    if( new_el
36d6e 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20  em->pKey==0 ){. 
36d6f 20 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65       fts3HashFre
36d70 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20  e(new_elem);.   
36d71 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a     return data;.
36d72 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79      }.    memcpy
36d73 28 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d  ((void*)new_elem
36d74 2d 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b  ->pKey, pKey, nK
36d75 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ey);.  }else{.  
36d76 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79    new_elem->pKey
36d77 20 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a   = (void*)pKey;.
36d78 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e    }.  new_elem->
36d79 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70  nKey = nKey;.  p
36d7a 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 61 73  H->count++;.  as
36d7b 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65  sert( pH->htsize
36d7c 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
36d7d 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
36d7e 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
36d7f 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26   );.  h = hraw &
36d80 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b   (pH->htsize-1);
36d81 0a 20 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  .  fts3HashInser
36d82 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48  tElement(pH, &pH
36d83 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65  ->ht[h], new_ele
36d84 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  m);.  new_elem->
36d85 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 72  data = data;.  r
36d86 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64  eturn 0;.}..#end
36d87 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
36d88 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
36d89 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
36d8a 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f  ABLE_FTS3) */../
36d8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
36d8c 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68 2e  nd of fts3_hash.
36d8d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
36d8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36d8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
36d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
36d91 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 70  egin file fts3_p
36d92 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  orter.c ********
36d93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36d94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
36d95 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70 74 65 6d  *.** 2006 Septem
36d96 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 30.**.** The
36d97 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
36d98 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
36d99 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
36d9a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
36d9b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
36d9c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
36d9d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
36d9e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
36d9f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
36da0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
36da1 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
36da2 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
36da3 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
36da4 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
36da5 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
36da6 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
36da7 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
36da8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36da9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36daa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dac 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  *****.** Impleme
36dad 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66  ntation of the f
36dae 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68 20  ull-text-search 
36daf 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61 74 20 69  tokenizer that i
36db0 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 20 50  mplements.** a P
36db1 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 2a  orter stemmer..*
36db2 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
36db3 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
36db4 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20  s only compiled 
36db5 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20  if:.**.**     * 
36db6 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
36db7 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61  is being built a
36db8 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a  s an extension.*
36db9 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63  *       (in whic
36dba 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f  h case SQLITE_CO
36dbb 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  RE is not define
36dbc 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20  d), or.**.**    
36dbd 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
36dbe 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
36dbf 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20  t into the core 
36dc0 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69  of.**       SQLi
36dc1 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61 73  te (in which cas
36dc2 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
36dc3 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64 29  FTS3 is defined)
36dc4 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
36dc5 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
36dc6 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
36dc7 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a  _ENABLE_FTS3)...
36dc8 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65  ../*.** Class de
36dc9 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  rived from sqlit
36dca 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  e3_tokenizer.*/.
36dcb 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70  typedef struct p
36dcc 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
36dcd 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
36dce 6e 69 7a 65 72 20 62 61 73 65 3b 20 20 20 20 20  nizer base;     
36dcf 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20 2a   /* Base class *
36dd0 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  /.} porter_token
36dd1 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61  izer;../*.** Cla
36dd2 73 73 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20  ss derived from 
36dd3 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69 7a 65 72  sqlit3_tokenizer
36dd4 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79 70 65 64  _cursor.*/.typed
36dd5 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65 72  ef struct porter
36dd6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
36dd7 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
36dd8 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
36dd9 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ase;.  const cha
36dda 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20 20 20 20  r *zInput;      
36ddb 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20      /* input we 
36ddc 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a  are tokenizing *
36ddd 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 20  /.  int nInput; 
36dde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ddf 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20   /* size of the 
36de0 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  input */.  int i
36de1 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
36de2 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72 65          /* curre
36de3 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 7a  nt position in z
36de4 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  Input */.  int i
36de5 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
36de6 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78          /* index
36de7 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20 74   of next token t
36de8 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
36de9 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b  .  char *zToken;
36dea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36deb 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63  /* storage for c
36dec 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  urrent token */.
36ded 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61 74 65 64    int nAllocated
36dee 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
36def 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  * space allocate
36df0 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66 66  d to zToken buff
36df1 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74  er */.} porter_t
36df2 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b  okenizer_cursor;
36df3 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  .../*.** Create 
36df4 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20  a new tokenizer 
36df5 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61  instance..*/.sta
36df6 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 72  tic int porterCr
36df7 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63  eate(.  int argc
36df8 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  , const char * c
36df9 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71  onst *argv,.  sq
36dfa 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
36dfb 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b  **ppTokenizer.){
36dfc 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
36dfd 7a 65 72 20 2a 74 3b 0a 0a 20 20 55 4e 55 53 45  zer *t;..  UNUSE
36dfe 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
36dff 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
36e00 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 20  METER(argv);..  
36e01 74 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  t = (porter_toke
36e02 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33  nizer *) sqlite3
36e03 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
36e04 74 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55  t));.  if( t==NU
36e05 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
36e06 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73  TE_NOMEM;.  mems
36e07 65 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(t, 0, sizeof(
36e08 2a 74 29 29 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e  *t));.  *ppToken
36e09 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
36e0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
36e0b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
36e0c 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
36e0d 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
36e0e 70 6f 72 74 65 72 44 65 73 74 72 6f 79 28 73 71  porterDestroy(sq
36e0f 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
36e10 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
36e11 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
36e12 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
36e13 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
36e14 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
36e15 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
36e16 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
36e17 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
36e18 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
36e19 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
36e1a 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74  zInput[0..nInput
36e1b 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
36e1c 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
36e1d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
36e1e 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
36e1f 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
36e20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
36e21 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4f  atic int porterO
36e22 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
36e23 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
36e24 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
36e25 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
36e26 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
36e27 7a 49 6e 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70  zInput, int nInp
36e28 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  ut,        /* St
36e29 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
36e2a 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
36e2b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
36e2c 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
36e2d 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
36e2e 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
36e2f 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65  ){.  porter_toke
36e30 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
36e31 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
36e32 45 54 45 52 28 70 54 6f 6b 65 6e 69 7a 65 72 29  ETER(pTokenizer)
36e33 3b 0a 0a 20 20 63 20 3d 20 28 70 6f 72 74 65 72  ;..  c = (porter
36e34 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
36e35 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
36e36 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b  loc(sizeof(*c));
36e37 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29  .  if( c==NULL )
36e38 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
36e39 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 7a 49 6e 70  OMEM;..  c->zInp
36e3a 75 74 20 3d 20 7a 49 6e 70 75 74 3b 0a 20 20 69  ut = zInput;.  i
36e3b 66 28 20 7a 49 6e 70 75 74 3d 3d 30 20 29 7b 0a  f( zInput==0 ){.
36e3c 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20      c->nInput = 
36e3d 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  0;.  }else if( n
36e3e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20 20 20 63  Input<0 ){.    c
36e3f 2d 3e 6e 49 6e 70 75 74 20 3d 20 28 69 6e 74 29  ->nInput = (int)
36e40 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a  strlen(zInput);.
36e41 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e    }else{.    c->
36e42 6e 49 6e 70 75 74 20 3d 20 6e 49 6e 70 75 74 3b  nInput = nInput;
36e43 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65  .  }.  c->iOffse
36e44 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
36e45 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20         /* start 
36e46 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68  tokenizing at th
36e47 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20  e beginning */. 
36e48 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a   c->iToken = 0;.
36e49 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55    c->zToken = NU
36e4a 4c 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  LL;             
36e4b 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c    /* no space al
36e4c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f  located, yet. */
36e4d 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64  .  c->nAllocated
36e4e 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73   = 0;..  *ppCurs
36e4f 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20  or = &c->base;. 
36e50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
36e51 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  K;.}../*.** Clos
36e52 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  e a tokenization
36e53 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
36e54 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63  ly opened by a c
36e55 61 6c 6c 20 74 6f 0a 2a 2a 20 70 6f 72 74 65 72  all to.** porter
36e56 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f  Open() above..*/
36e57 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
36e58 65 72 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  erClose(sqlite3_
36e59 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
36e5a 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 70 6f   *pCursor){.  po
36e5b 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
36e5c 75 72 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74  ursor *c = (port
36e5d 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
36e5e 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
36e5f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
36e60 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c  ->zToken);.  sql
36e61 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20  ite3_free(c);.  
36e62 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
36e63 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77 65 6c 20  ;.}./*.** Vowel 
36e64 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a  or consonant.*/.
36e65 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
36e66 72 20 63 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20  r cType[] = {.  
36e67 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
36e68 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
36e69 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
36e6a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 0a   1, 1, 1, 1, 0,.
36e6b 20 20 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31     1, 1, 1, 2, 1
36e6c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73 43 6f 6e  .};../*.** isCon
36e6d 73 6f 6e 61 6e 74 28 29 20 61 6e 64 20 69 73 56  sonant() and isV
36e6e 6f 77 65 6c 28 29 20 64 65 74 65 72 6d 69 6e 65  owel() determine
36e6f 20 69 66 20 74 68 65 69 72 20 66 69 72 73 74 20   if their first 
36e70 63 68 61 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20  character in.** 
36e71 74 68 65 20 73 74 72 69 6e 67 20 74 68 65 79 20  the string they 
36e72 70 6f 69 6e 74 20 74 6f 20 69 73 20 61 20 63 6f  point to is a co
36e73 6e 73 6f 6e 61 6e 74 20 6f 72 20 61 20 76 6f 77  nsonant or a vow
36e74 65 6c 2c 20 61 63 63 6f 72 64 69 6e 67 0a 2a 2a  el, according.**
36e75 20 74 6f 20 50 6f 72 74 65 72 20 72 75 6c 73 2e   to Porter ruls.
36e76 20 20 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 6e 73 6f    .**.** A conso
36e77 6e 61 74 65 20 69 73 20 61 6e 79 20 6c 65 74 74  nate is any lett
36e78 65 72 20 6f 74 68 65 72 20 74 68 61 6e 20 27 61  er other than 'a
36e79 27 2c 20 27 65 27 2c 20 27 69 27 2c 20 27 6f 27  ', 'e', 'i', 'o'
36e7a 2c 20 6f 72 20 27 75 27 2e 0a 2a 2a 20 27 59 27  , or 'u'..** 'Y'
36e7b 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20   is a consonant 
36e7c 75 6e 6c 65 73 73 20 69 74 20 66 6f 6c 6c 6f 77  unless it follow
36e7d 73 20 61 6e 6f 74 68 65 72 20 63 6f 6e 73 6f 6e  s another conson
36e7e 61 6e 74 2c 0a 2a 2a 20 69 6e 20 77 68 69 63 68  ant,.** in which
36e7f 20 63 61 73 65 20 69 74 20 69 73 20 61 20 76 6f   case it is a vo
36e80 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  wel..**.** In th
36e81 65 73 65 20 72 6f 75 74 69 6e 65 2c 20 74 68 65  ese routine, the
36e82 20 6c 65 74 74 65 72 73 20 61 72 65 20 69 6e 20   letters are in 
36e83 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20  reverse order.  
36e84 53 6f 20 74 68 65 20 27 79 27 20 72 75 6c 65 0a  So the 'y' rule.
36e85 2a 2a 20 69 73 20 74 68 61 74 20 27 79 27 20 69  ** is that 'y' i
36e86 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e  s a consonant un
36e87 6c 65 73 73 20 69 74 20 69 73 20 66 6f 6c 6c 6f  less it is follo
36e88 77 65 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a  wed by another.*
36e89 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a  * consonent..*/.
36e8a 73 74 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77  static int isVow
36e8b 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  el(const char*);
36e8c 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 43 6f  .static int isCo
36e8d 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68  nsonant(const ch
36e8e 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b  ar *z){.  int j;
36e8f 0a 20 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a  .  char x = *z;.
36e90 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74    if( x==0 ) ret
36e91 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28  urn 0;.  assert(
36e92 20 78 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a   x>='a' && x<='z
36e93 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65  ' );.  j = cType
36e94 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a  [x-'a'];.  if( j
36e95 3c 32 20 29 20 72 65 74 75 72 6e 20 6a 3b 0a 20  <2 ) return j;. 
36e96 20 72 65 74 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20   return z[1]==0 
36e97 7c 7c 20 69 73 56 6f 77 65 6c 28 7a 20 2b 20 31  || isVowel(z + 1
36e98 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
36e99 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  isVowel(const ch
36e9a 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b  ar *z){.  int j;
36e9b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a 7a 3b 0a  .  char x = *z;.
36e9c 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72 65 74    if( x==0 ) ret
36e9d 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28  urn 0;.  assert(
36e9e 20 78 3e 3d 27 61 27 20 26 26 20 78 3c 3d 27 7a   x>='a' && x<='z
36e9f 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54 79 70 65  ' );.  j = cType
36ea0 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66 28 20 6a  [x-'a'];.  if( j
36ea1 3c 32 20 29 20 72 65 74 75 72 6e 20 31 2d 6a 3b  <2 ) return 1-j;
36ea2 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73  .  return isCons
36ea3 6f 6e 61 6e 74 28 7a 20 2b 20 31 29 3b 0a 7d 0a  onant(z + 1);.}.
36ea4 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 61 6e 79 20 73  ./*.** Let any s
36ea5 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f  equence of one o
36ea6 72 20 6d 6f 72 65 20 76 6f 77 65 6c 73 20 62 65  r more vowels be
36ea7 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
36ea8 56 20 61 6e 64 20 6c 65 74 0a 2a 2a 20 43 20 62  V and let.** C b
36ea9 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e  e sequence of on
36eaa 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 73 6f 6e  e or more conson
36eab 61 6e 74 73 2e 20 20 54 68 65 6e 20 65 76 65 72  ants.  Then ever
36eac 79 20 77 6f 72 64 20 63 61 6e 20 62 65 0a 2a 2a  y word can be.**
36ead 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 3a   represented as:
36eae 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
36eaf 20 5b 43 5d 20 28 56 43 29 7b 6d 7d 20 5b 56 5d   [C] (VC){m} [V]
36eb0 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 6f 73 65 3a  .**.** In prose:
36eb1 20 20 41 20 77 6f 72 64 20 69 73 20 61 6e 20 6f    A word is an o
36eb2 70 74 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e  ptional consonan
36eb3 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 7a 65  t followed by ze
36eb4 72 6f 20 6f 72 0a 2a 2a 20 76 6f 77 65 6c 2d 63  ro or.** vowel-c
36eb5 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 20 66  onsonant pairs f
36eb6 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 6f 70  ollowed by an op
36eb7 74 69 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20 20 22  tional vowel.  "
36eb8 6d 22 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d  m" is the.** num
36eb9 62 65 72 20 6f 66 20 76 6f 77 65 6c 20 63 6f 6e  ber of vowel con
36eba 73 6f 6e 61 6e 74 20 70 61 69 72 73 2e 20 20 54  sonant pairs.  T
36ebb 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6d 70  his routine comp
36ebc 75 74 65 73 20 74 68 65 20 76 61 6c 75 65 0a 2a  utes the value.*
36ebd 2a 20 6f 66 20 6d 20 66 6f 72 20 74 68 65 20 66  * of m for the f
36ebe 69 72 73 74 20 69 20 62 79 74 65 73 20 6f 66 20  irst i bytes of 
36ebf 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  a word..**.** Re
36ec0 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
36ec1 20 6d 2d 76 61 6c 75 65 20 66 6f 72 20 7a 20 69   m-value for z i
36ec2 73 20 31 20 6f 72 20 6d 6f 72 65 2e 20 20 49 6e  s 1 or more.  In
36ec3 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 0a 2a 2a   other words,.**
36ec4 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   return true if 
36ec5 7a 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65  z contains at le
36ec6 61 73 74 20 6f 6e 65 20 76 6f 77 65 6c 20 74 68  ast one vowel th
36ec7 61 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a  at is followed.*
36ec8 2a 20 62 79 20 61 20 63 6f 6e 73 6f 6e 61 6e 74  * by a consonant
36ec9 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  ..**.** In this 
36eca 72 6f 75 74 69 6e 65 20 7a 5b 5d 20 69 73 20 69  routine z[] is i
36ecb 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e  n reverse order.
36ecc 20 20 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c    So we are real
36ecd 6c 79 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20 66 6f  ly looking.** fo
36ece 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  r an instance of
36ecf 20 6f 66 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20   of a consonant 
36ed0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 6f  followed by a vo
36ed1 77 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  wel..*/.static i
36ed2 6e 74 20 6d 5f 67 74 5f 30 28 63 6f 6e 73 74 20  nt m_gt_0(const 
36ed3 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c  char *z){.  whil
36ed4 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b  e( isVowel(z) ){
36ed5 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a   z++; }.  if( *z
36ed6 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
36ed7 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f    while( isConso
36ed8 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  nant(z) ){ z++; 
36ed9 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30  }.  return *z!=0
36eda 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74  ;.}../* Like mgt
36edb 30 20 61 62 6f 76 65 20 65 78 63 65 70 74 20 77  0 above except w
36edc 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  e are looking fo
36edd 72 20 61 20 76 61 6c 75 65 20 6f 66 20 6d 20 77  r a value of m w
36ede 68 69 63 68 20 69 73 0a 2a 2a 20 65 78 61 63 74  hich is.** exact
36edf 6c 79 20 31 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly 1.*/.static i
36ee0 6e 74 20 6d 5f 65 71 5f 31 28 63 6f 6e 73 74 20  nt m_eq_1(const 
36ee1 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c  char *z){.  whil
36ee2 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b  e( isVowel(z) ){
36ee3 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a   z++; }.  if( *z
36ee4 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
36ee5 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f    while( isConso
36ee6 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  nant(z) ){ z++; 
36ee7 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
36ee8 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
36ee9 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b  e( isVowel(z) ){
36eea 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a   z++; }.  if( *z
36eeb 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==0 ) return 1;.
36eec 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f    while( isConso
36eed 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  nant(z) ){ z++; 
36eee 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30  }.  return *z==0
36eef 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74  ;.}../* Like mgt
36ef0 30 20 61 62 6f 76 65 20 65 78 63 65 70 74 20 77  0 above except w
36ef1 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  e are looking fo
36ef2 72 20 61 20 76 61 6c 75 65 20 6f 66 20 6d 3e 31  r a value of m>1
36ef3 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 72 20 6d   instead.** or m
36ef4 3e 30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  >0.*/.static int
36ef5 20 6d 5f 67 74 5f 31 28 63 6f 6e 73 74 20 63 68   m_gt_1(const ch
36ef6 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28  ar *z){.  while(
36ef7 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
36ef8 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
36ef9 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
36efa 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
36efb 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
36efc 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65    if( *z==0 ) re
36efd 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28  turn 0;.  while(
36efe 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a   isVowel(z) ){ z
36eff 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
36f00 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
36f01 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
36f02 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
36f03 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a    return *z!=0;.
36f04 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
36f05 54 52 55 45 20 69 66 20 74 68 65 72 65 20 69 73  TRUE if there is
36f06 20 61 20 76 6f 77 65 6c 20 61 6e 79 77 68 65 72   a vowel anywher
36f07 65 20 77 69 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d  e within z[0..n-
36f08 31 5d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  1].*/.static int
36f09 20 68 61 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20   hasVowel(const 
36f0a 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c  char *z){.  whil
36f0b 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
36f0c 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65  ) ){ z++; }.  re
36f0d 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f  turn *z!=0;.}../
36f0e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
36f0f 20 69 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64   if the word end
36f10 73 20 69 6e 20 61 20 64 6f 75 62 6c 65 20 63 6f  s in a double co
36f11 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  nsonant..**.** T
36f12 68 65 20 74 65 78 74 20 69 73 20 72 65 76 65 72  he text is rever
36f13 73 65 64 20 68 65 72 65 2e 20 53 6f 20 77 65 20  sed here. So we 
36f14 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69  are really looki
36f15 6e 67 20 61 74 0a 2a 2a 20 74 68 65 20 66 69 72  ng at.** the fir
36f16 73 74 20 74 77 6f 20 63 68 61 72 61 63 74 65 72  st two character
36f17 73 20 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61  s of z[]..*/.sta
36f18 74 69 63 20 69 6e 74 20 64 6f 75 62 6c 65 43 6f  tic int doubleCo
36f19 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74 20 63 68  nsonant(const ch
36f1a 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e  ar *z){.  return
36f1b 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
36f1c 26 26 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 3b 0a 7d  && z[0]==z[1];.}
36f1d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
36f1e 52 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20  RUE if the word 
36f1f 65 6e 64 73 20 77 69 74 68 20 74 68 72 65 65 20  ends with three 
36f20 6c 65 74 74 65 72 73 20 77 68 69 63 68 0a 2a 2a  letters which.**
36f21 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76   are consonant-v
36f22 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61  owel-consonent a
36f23 6e 64 20 77 68 65 72 65 20 74 68 65 20 66 69 6e  nd where the fin
36f24 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20  al consonant.** 
36f25 69 73 20 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c  is not 'w', 'x',
36f26 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54   or 'y'..**.** T
36f27 68 65 20 77 6f 72 64 20 69 73 20 72 65 76 65 72  he word is rever
36f28 73 65 64 20 68 65 72 65 2e 20 20 53 6f 20 77 65  sed here.  So we
36f29 20 61 72 65 20 72 65 61 6c 6c 79 20 63 68 65 63   are really chec
36f2a 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73  king the.** firs
36f2b 74 20 74 68 72 65 65 20 6c 65 74 74 65 72 73 20  t three letters 
36f2c 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 6f 6e  and the first on
36f2d 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b  e cannot be in [
36f2e 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wxy]..*/.static 
36f2f 69 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73  int star_oh(cons
36f30 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65  t char *z){.  re
36f31 74 75 72 6e 0a 20 20 20 20 69 73 43 6f 6e 73 6f  turn.    isConso
36f32 6e 61 6e 74 28 7a 29 20 26 26 0a 20 20 20 20 7a  nant(z) &&.    z
36f33 5b 30 5d 21 3d 27 77 27 20 26 26 20 7a 5b 30 5d  [0]!='w' && z[0]
36f34 21 3d 27 78 27 20 26 26 20 7a 5b 30 5d 21 3d 27  !='x' && z[0]!='
36f35 79 27 20 26 26 0a 20 20 20 20 69 73 56 6f 77 65  y' &&.    isVowe
36f36 6c 28 7a 2b 31 29 20 26 26 0a 20 20 20 20 69 73  l(z+1) &&.    is
36f37 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0a  Consonant(z+2);.
36f38 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
36f39 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20 7a  word ends with z
36f3a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28 29  From and xCond()
36f3b 20 69 73 20 74 72 75 65 20 66 6f 72 20 74 68 65   is true for the
36f3c 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65 20   stem.** of the 
36f3d 77 6f 72 64 20 74 68 61 74 20 70 72 65 63 65 65  word that precee
36f3e 64 73 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e 64  ds the zFrom end
36f3f 69 6e 67 2c 20 74 68 65 6e 20 63 68 61 6e 67 65  ing, then change
36f40 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67 20   the .** ending 
36f41 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68  to zTo..**.** Th
36f42 65 20 69 6e 70 75 74 20 77 6f 72 64 20 2a 70 7a  e input word *pz
36f43 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20 62   and zFrom are b
36f44 6f 74 68 20 69 6e 20 72 65 76 65 72 73 65 20 6f  oth in reverse o
36f45 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69 73  rder.  zTo.** is
36f46 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65 72   in normal order
36f47 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  . .**.** Return 
36f48 54 52 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d 61  TRUE if zFrom ma
36f49 74 63 68 65 73 2e 20 20 52 65 74 75 72 6e 20 46  tches.  Return F
36f4a 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d 20 64 6f  ALSE if zFrom do
36f4b 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 2e  es not.** match.
36f4c 20 20 4e 6f 74 20 74 68 61 74 20 54 52 55 45 20    Not that TRUE 
36f4d 69 73 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e  is returned even
36f4e 20 69 66 20 78 43 6f 6e 64 28 29 20 66 61 69 6c   if xCond() fail
36f4f 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62 73  s and.** no subs
36f50 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 2e  titution occurs.
36f51 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
36f52 74 65 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70 7a  tem(.  char **pz
36f53 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
36f54 20 54 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20   The word being 
36f55 73 74 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65  stemmed (Reverse
36f56 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  d) */.  const ch
36f57 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f  ar *zFrom,     /
36f58 2a 20 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20  * If the ending 
36f59 6d 61 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20  matches this... 
36f5a 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20  (Reversed) */.  
36f5b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c  const char *zTo,
36f5c 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68         /* ... ch
36f5d 61 6e 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20  ange the ending 
36f5e 74 6f 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76  to this (not rev
36f5f 65 72 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20  ersed) */.  int 
36f60 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20 63  (*xCond)(const c
36f61 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64 69  har*)   /* Condi
36f62 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62  tion that must b
36f63 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63  e true */.){.  c
36f64 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20  har *z = *pz;.  
36f65 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26 26  while( *zFrom &&
36f66 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a   *zFrom==*z ){ z
36f67 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a 20  ++; zFrom++; }. 
36f68 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20 29   if( *zFrom!=0 )
36f69 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
36f6a 20 78 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e 64   xCond && !xCond
36f6b 28 7a 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  (z) ) return 1;.
36f6c 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29 7b    while( *zTo ){
36f6d 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a 28  .    *(--z) = *(
36f6e 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a 70  zTo++);.  }.  *p
36f6f 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e 20  z = z;.  return 
36f70 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  1;.}../*.** This
36f71 20 69 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b   is the fallback
36f72 20 73 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68   stemmer used wh
36f73 65 6e 20 74 68 65 20 70 6f 72 74 65 72 20 73 74  en the porter st
36f74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61 70  emmer is.** inap
36f75 70 72 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20  propriate.  The 
36f76 69 6e 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f  input word is co
36f77 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75  pied into the ou
36f78 74 70 75 74 20 77 69 74 68 0a 2a 2a 20 55 53 2d  tput with.** US-
36f79 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69  ASCII case foldi
36f7a 6e 67 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  ng.  If the inpu
36f7b 74 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c 6f  t word is too lo
36f7c 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61 6e  ng (more.** than
36f7d 20 32 30 20 62 79 74 65 73 20 69 66 20 69 74 20   20 bytes if it 
36f7e 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67 69  contains no digi
36f7f 74 73 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e 20  ts or more than 
36f80 36 20 62 79 74 65 73 20 69 66 0a 2a 2a 20 69 74  6 bytes if.** it
36f81 20 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73   contains digits
36f82 29 20 74 68 65 6e 20 77 6f 72 64 20 69 73 20 74  ) then word is t
36f83 72 75 6e 63 61 74 65 64 20 74 6f 20 32 30 20 6f  runcated to 20 o
36f84 72 20 36 20 62 79 74 65 73 0a 2a 2a 20 62 79 20  r 6 bytes.** by 
36f85 74 61 6b 69 6e 67 20 31 30 20 6f 72 20 33 20 62  taking 10 or 3 b
36f86 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65  ytes from the be
36f87 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 2e  ginning and end.
36f88 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36f89 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f 6e  copy_stemmer(con
36f8a 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e  st char *zIn, in
36f8b 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75  t nIn, char *zOu
36f8c 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a  t, int *pnOut){.
36f8d 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b 0a    int i, mx, j;.
36f8e 20 20 69 6e 74 20 68 61 73 44 69 67 69 74 20 3d    int hasDigit =
36f8f 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
36f90 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <nIn; i++){.    
36f91 63 68 61 72 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b  char c = zIn[i];
36f92 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27 20  .    if( c>='A' 
36f93 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20  && c<='Z' ){.   
36f94 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d     zOut[i] = c -
36f95 20 27 41 27 20 2b 20 27 61 27 3b 0a 20 20 20 20   'A' + 'a';.    
36f96 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
36f97 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39   c>='0' && c<='9
36f98 27 20 29 20 68 61 73 44 69 67 69 74 20 3d 20 31  ' ) hasDigit = 1
36f99 3b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20  ;.      zOut[i] 
36f9a 3d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = c;.    }.  }. 
36f9b 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74 20 3f   mx = hasDigit ?
36f9c 20 33 20 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e   3 : 10;.  if( n
36f9d 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66  In>mx*2 ){.    f
36f9e 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d  or(j=mx, i=nIn-m
36f9f 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a  x; i<nIn; i++, j
36fa0 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b  ++){.      zOut[
36fa1 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20  j] = zOut[i];.  
36fa2 20 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20    }.    i = j;. 
36fa3 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30   }.  zOut[i] = 0
36fa4 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a  ;.  *pnOut = i;.
36fa5 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74  }.../*.** Stem t
36fa6 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 7a 49  he input word zI
36fa7 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74  n[0..nIn-1].  St
36fa8 6f 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 69  ore the output i
36fa9 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20  n zOut..** zOut 
36faa 69 73 20 61 74 20 6c 65 61 73 74 20 62 69 67 20  is at least big 
36fab 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e  enough to hold n
36fac 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69 74 65  In bytes.  Write
36fad 20 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73   the actual.** s
36fae 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ize of the outpu
36faf 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73 69 76  t word (exclusiv
36fb0 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20 74 65  e of the '\0' te
36fb1 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a  rminator) into *
36fb2 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79  pnOut..**.** Any
36fb3 20 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72   upper-case char
36fb4 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 55 53  acters in the US
36fb5 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72  -ASCII character
36fb6 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20   set ([A-Z]).** 
36fb7 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  are converted to
36fb8 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 20 55 70   lower case.  Up
36fb9 70 65 72 2d 63 61 73 65 20 55 54 46 20 63 68 61  per-case UTF cha
36fba 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 75  racters are.** u
36fbb 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20  nchanged..**.** 
36fbc 57 6f 72 64 73 20 74 68 61 74 20 61 72 65 20 6c  Words that are l
36fbd 6f 6e 67 65 72 20 74 68 61 6e 20 61 62 6f 75 74  onger than about
36fbe 20 32 30 20 62 79 74 65 73 20 61 72 65 20 73 74   20 bytes are st
36fbf 65 6d 6d 65 64 20 62 79 20 72 65 74 61 69 6e 69  emmed by retaini
36fc0 6e 67 0a 2a 2a 20 61 20 66 65 77 20 62 79 74 65  ng.** a few byte
36fc1 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  s from the begin
36fc2 6e 69 6e 67 20 61 6e 64 20 74 68 65 20 65 6e 64  ning and the end
36fc3 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20 49   of the word.  I
36fc4 66 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f  f the.** word co
36fc5 6e 74 61 69 6e 73 20 64 69 67 69 74 73 2c 20 33  ntains digits, 3
36fc6 20 62 79 74 65 73 20 61 72 65 20 74 61 6b 65 6e   bytes are taken
36fc7 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
36fc8 69 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74  ing and.** 3 byt
36fc9 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e  es from the end.
36fca 20 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73    For long words
36fcb 20 77 69 74 68 6f 75 74 20 64 69 67 69 74 73 2c   without digits,
36fcc 20 31 30 20 62 79 74 65 73 0a 2a 2a 20 61 72 65   10 bytes.** are
36fcd 20 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68   taken from each
36fce 20 65 6e 64 2e 20 20 55 53 2d 41 53 43 49 49 20   end.  US-ASCII 
36fcf 63 61 73 65 20 66 6f 6c 64 69 6e 67 20 73 74 69  case folding sti
36fd0 6c 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a  ll applies..** .
36fd1 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  ** If the input 
36fd2 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  word contains no
36fd3 74 20 64 69 67 69 74 73 20 62 75 74 20 64 6f 65  t digits but doe
36fd4 73 20 63 68 61 72 61 63 74 65 72 73 20 6e 6f 74  s characters not
36fd5 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d   .** in [a-zA-Z]
36fd6 20 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e   then no stemmin
36fd7 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61  g is attempted a
36fd8 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
36fd9 6a 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20  just .** copies 
36fda 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74  the input into t
36fdb 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68  he input into th
36fdc 65 20 6f 75 74 70 75 74 20 77 69 74 68 20 55 53  e output with US
36fdd 2d 41 53 43 49 49 0a 2a 2a 20 63 61 73 65 20 66  -ASCII.** case f
36fde 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74  olding..**.** St
36fdf 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69 6e 63  emming never inc
36fe0 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e 67 74  reases the lengt
36fe1 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20  h of the word.  
36fe2 53 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e  So there is.** n
36fe3 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f 76 65 72  o chance of over
36fe4 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f 75 74  flowing the zOut
36fe5 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
36fe6 69 63 20 76 6f 69 64 20 70 6f 72 74 65 72 5f 73  ic void porter_s
36fe7 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61  temmer(const cha
36fe8 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c  r *zIn, int nIn,
36fe9 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74   char *zOut, int
36fea 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20   *pnOut){.  int 
36feb 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 52 65  i, j;.  char zRe
36fec 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20 63 68 61  verse[28];.  cha
36fed 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20 69 66 28  r *z, *z2;.  if(
36fee 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e 3e 3d 73   nIn<3 || nIn>=s
36fef 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d  izeof(zReverse)-
36ff0 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  7 ){.    /* The 
36ff1 77 6f 72 64 20 69 73 20 74 6f 6f 20 62 69 67 20  word is too big 
36ff2 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72  or too small for
36ff3 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d   the porter stem
36ff4 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46 61 6c 6c  mer..    ** Fall
36ff5 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f 70 79  back to the copy
36ff6 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20   stemmer */.    
36ff7 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e  copy_stemmer(zIn
36ff8 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f  , nIn, zOut, pnO
36ff9 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ut);.    return;
36ffa 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 2c 20  .  }.  for(i=0, 
36ffb 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  j=sizeof(zRevers
36ffc 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b  e)-6; i<nIn; i++
36ffd 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63 68 61 72  , j--){.    char
36ffe 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20   c = zIn[i];.   
36fff 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63   if( c>='A' && c
37000 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='Z' ){.      z
37001 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b  Reverse[j] = c +
37002 20 27 61 27 20 2d 20 27 41 27 3b 0a 20 20 20 20   'a' - 'A';.    
37003 7d 65 6c 73 65 20 69 66 28 20 63 3e 3d 27 61 27  }else if( c>='a'
37004 20 26 26 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20   && c<='z' ){.  
37005 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20      zReverse[j] 
37006 3d 20 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = c;.    }else{.
37007 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65        /* The use
37008 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
37009 6e 6f 74 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20  not in [a-zA-Z] 
3700a 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 66 61  means that we fa
3700b 6c 6c 62 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20  llback.      ** 
3700c 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d  to the copy stem
3700d 6d 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70  mer */.      cop
3700e 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e  y_stemmer(zIn, n
3700f 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29  In, zOut, pnOut)
37010 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
37011 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
37012 65 74 28 26 7a 52 65 76 65 72 73 65 5b 73 69 7a  et(&zReverse[siz
37013 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 35 5d  eof(zReverse)-5]
37014 2c 20 30 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26  , 0, 5);.  z = &
37015 7a 52 65 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a  zReverse[j+1];..
37016 0a 20 20 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f  .  /* Step 1a */
37017 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27  .  if( z[0]=='s'
37018 20 29 7b 0a 20 20 20 20 69 66 28 0a 20 20 20 20   ){.    if(.    
37019 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 73 73   !stem(&z, "sess
3701a 22 2c 20 22 73 73 22 2c 20 30 29 20 26 26 0a 20  ", "ss", 0) &&. 
3701b 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73      !stem(&z, "s
3701c 65 69 22 2c 20 22 69 22 2c 20 30 29 20 20 26 26  ei", "i", 0)  &&
3701d 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20  .     !stem(&z, 
3701e 22 73 73 22 2c 20 22 73 73 22 2c 20 30 29 0a 20  "ss", "ss", 0). 
3701f 20 20 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b     ){.      z++;
37020 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
37021 20 53 74 65 70 20 31 62 20 2a 2f 20 20 0a 20 20   Step 1b */  .  
37022 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74  z2 = z;.  if( st
37023 65 6d 28 26 7a 2c 20 22 64 65 65 22 2c 20 22 65  em(&z, "dee", "e
37024 65 22 2c 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20  e", m_gt_0) ){. 
37025 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
37026 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20  .  The work was 
37027 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20  all in the test 
37028 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0a  */.  }else if( .
37029 20 20 20 20 20 28 73 74 65 6d 28 26 7a 2c 20 22       (stem(&z, "
3702a 67 6e 69 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  gni", "", hasVow
3702b 65 6c 29 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20  el) || stem(&z, 
3702c 22 64 65 22 2c 20 22 22 2c 20 68 61 73 56 6f 77  "de", "", hasVow
3702d 65 6c 29 29 0a 20 20 20 20 20 20 26 26 20 7a 21  el)).      && z!
3702e 3d 7a 32 0a 20 20 29 7b 0a 20 20 20 20 20 69 66  =z2.  ){.     if
3702f 28 20 73 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c  ( stem(&z, "ta",
37030 20 22 61 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ate", 0) ||.  
37031 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
37032 22 6c 62 22 2c 20 22 62 6c 65 22 2c 20 30 29 20  "lb", "ble", 0) 
37033 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
37034 28 26 7a 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22  (&z, "zi", "ize"
37035 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  , 0) ){.       /
37036 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
37037 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20  he work was all 
37038 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20  in the test */. 
37039 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f      }else if( do
3703a 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ubleConsonant(z)
3703b 20 26 26 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20   && (*z!='l' && 
3703c 2a 7a 21 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27  *z!='s' && *z!='
3703d 7a 27 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b  z') ){.       z+
3703e 2b 3b 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66  +;.     }else if
3703f 28 20 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73  ( m_eq_1(z) && s
37040 74 61 72 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20  tar_oh(z) ){.   
37041 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27      *(--z) = 'e'
37042 3b 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  ;.     }.  }..  
37043 2f 2a 20 53 74 65 70 20 31 63 20 2a 2f 0a 20 20  /* Step 1c */.  
37044 69 66 28 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26  if( z[0]=='y' &&
37045 20 68 61 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29   hasVowel(z+1) )
37046 7b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27  {.    z[0] = 'i'
37047 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
37048 20 32 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20   2 */.  switch( 
37049 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73 65 20  z[1] ){.   case 
3704a 27 61 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'a':.     stem(&
3704b 7a 2c 20 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61  z, "lanoita", "a
3704c 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
3704d 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
3704e 61 6e 6f 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20  anoit", "tion", 
3704f 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
37050 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27  eak;.   case 'c'
37051 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
37052 22 69 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20  "icne", "ence", 
37053 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37054 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c  stem(&z, "icna",
37055 20 22 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ance", m_gt_0)
37056 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
37057 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
37058 73 74 65 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c  stem(&z, "rezi",
37059 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ize", m_gt_0);
3705a 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3705b 63 61 73 65 20 27 67 27 3a 0a 20 20 20 20 20 73  case 'g':.     s
3705c 74 65 6d 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20  tem(&z, "igol", 
3705d 22 6c 6f 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  "log", m_gt_0);.
3705e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
3705f 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
37060 65 6d 28 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62  em(&z, "ilb", "b
37061 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  le", m_gt_0) ||.
37062 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37063 6c 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  lla", "al", m_gt
37064 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
37065 28 26 7a 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65  (&z, "iltne", "e
37066 6e 74 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  nt", m_gt_0) ||.
37067 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37068 6c 65 22 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30  le", "e", m_gt_0
37069 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
3706a 7a 2c 20 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73  z, "ilsuo", "ous
3706b 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
3706c 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
3706d 27 6f 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'o':.     stem(&
3706e 7a 2c 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69  z, "noitazi", "i
3706f 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ze", m_gt_0) ||.
37070 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
37071 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f  oita", "ate", m_
37072 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37073 65 6d 28 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22  em(&z, "rota", "
37074 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ate", m_gt_0);. 
37075 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
37076 73 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65  se 's':.     ste
37077 6d 28 26 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22  m(&z, "msila", "
37078 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
37079 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
3707a 73 65 6e 65 76 69 22 2c 20 22 69 76 65 22 2c 20  senevi", "ive", 
3707b 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
3707c 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75  stem(&z, "ssenlu
3707d 66 22 2c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f  f", "ful", m_gt_
3707e 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
3707f 26 7a 2c 20 22 73 73 65 6e 73 75 6f 22 2c 20 22  &z, "ssensuo", "
37080 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ous", m_gt_0);. 
37081 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
37082 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65  se 't':.     ste
37083 6d 28 26 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22  m(&z, "itila", "
37084 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
37085 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37086 74 69 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f  tivi", "ive", m_
37087 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37088 65 6d 28 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c  em(&z, "itilib",
37089 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b   "ble", m_gt_0);
3708a 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  .     break;.  }
3708b 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f  ..  /* Step 3 */
3708c 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20  .  switch( z[0] 
3708d 29 7b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  ){.   case 'e':.
3708e 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
3708f 74 61 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67  taci", "ic", m_g
37090 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65  t_0) ||.     ste
37091 6d 28 26 7a 2c 20 22 65 76 69 74 61 22 2c 20 22  m(&z, "evita", "
37092 22 2c 20 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a  ", m_gt_0)   ||.
37093 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65       stem(&z, "e
37094 7a 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67  zila", "al", m_g
37095 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
37096 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20  ;.   case 'i':. 
37097 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
37098 69 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74  ici", "ic", m_gt
37099 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
3709a 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20  .   case 'l':.  
3709b 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63     stem(&z, "lac
3709c 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
3709d 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
3709e 7a 2c 20 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f  z, "luf", "", m_
3709f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
370a0 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
370a1 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73       stem(&z, "s
370a2 73 65 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  sen", "", m_gt_0
370a3 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
370a4 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 20   }..  /* Step 4 
370a5 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31  */.  switch( z[1
370a6 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27  ] ){.   case 'a'
370a7 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
370a8 3d 27 6c 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='l' && m_gt_1(z
370a9 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
370aa 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
370ab 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
370ac 20 27 63 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'c':.     if( z
370ad 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d  [0]=='e' && z[2]
370ae 3d 3d 27 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d  =='n' && (z[3]==
370af 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27  'a' || z[3]=='e'
370b0 29 20 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34  )  && m_gt_1(z+4
370b1 29 20 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  )  ){.       z +
370b2 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
370b3 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
370b4 27 65 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'e':.     if( z[
370b5 30 5d 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f  0]=='r' && m_gt_
370b6 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  1(z+2) ){.      
370b7 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a   z += 2;.     }.
370b8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
370b9 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 69 66  ase 'i':.     if
370ba 28 20 7a 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d  ( z[0]=='c' && m
370bb 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
370bc 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
370bd 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
370be 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20     case 'l':.   
370bf 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
370c0 26 26 20 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20  && z[2]=='b' && 
370c1 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
370c2 33 5d 3d 3d 27 69 27 29 20 26 26 20 6d 5f 67 74  3]=='i') && m_gt
370c3 5f 31 28 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20  _1(z+4) ){.     
370c4 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d    z += 4;.     }
370c5 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
370c6 63 61 73 65 20 27 6e 27 3a 0a 20 20 20 20 20 69  case 'n':.     i
370c7 66 28 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a  f( z[0]=='t' ){.
370c8 20 20 20 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d         if( z[2]=
370c9 3d 27 61 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='a' ){.        
370ca 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29   if( m_gt_1(z+3)
370cb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 7a   ){.           z
370cc 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20   += 3;.         
370cd 7d 0a 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  }.       }else i
370ce 66 28 20 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a  f( z[2]=='e' ){.
370cf 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a           stem(&z
370d0 2c 20 22 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d  , "tneme", "", m
370d1 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
370d2 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
370d3 6d 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  m", "", m_gt_1) 
370d4 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
370d5 28 26 7a 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20  (&z, "tne", "", 
370d6 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 20 20  m_gt_1);.       
370d7 7d 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  }.     }.     br
370d8 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27  eak;.   case 'o'
370d9 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
370da 3d 27 75 27 20 29 7b 0a 20 20 20 20 20 20 20 69  ='u' ){.       i
370db 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  f( m_gt_1(z+2) )
370dc 7b 0a 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20  {.         z += 
370dd 32 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20  2;.       }.    
370de 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d   }else if( z[3]=
370df 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74  ='s' || z[3]=='t
370e0 27 20 29 7b 0a 20 20 20 20 20 20 20 73 74 65 6d  ' ){.       stem
370e1 28 26 7a 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20  (&z, "noi", "", 
370e2 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a  m_gt_1);.     }.
370e3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
370e4 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 69 66  ase 's':.     if
370e5 28 20 7a 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a  ( z[0]=='m' && z
370e6 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74  [2]=='i' && m_gt
370e7 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
370e8 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
370e9 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
370ea 63 61 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73  case 't':.     s
370eb 74 65 6d 28 26 7a 2c 20 22 65 74 61 22 2c 20 22  tem(&z, "eta", "
370ec 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
370ed 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
370ee 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
370ef 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
370f0 61 73 65 20 27 75 27 3a 0a 20 20 20 20 20 69 66  ase 'u':.     if
370f1 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a  ( z[0]=='s' && z
370f2 5b 32 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74  [2]=='o' && m_gt
370f3 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
370f4 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d    z += 3;.     }
370f5 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
370f6 63 61 73 65 20 27 76 27 3a 0a 20 20 20 63 61 73  case 'v':.   cas
370f7 65 20 27 7a 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'z':.     if( 
370f8 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32  z[0]=='e' && z[2
370f9 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='i' && m_gt_1
370fa 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
370fb 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
370fc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
370fd 20 20 2f 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a    /* Step 5a */.
370fe 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
370ff 29 7b 0a 20 20 20 20 69 66 28 20 6d 5f 67 74 5f  ){.    if( m_gt_
37100 31 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  1(z+1) ){.      
37101 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  z++;.    }else i
37102 66 28 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26  f( m_eq_1(z+1) &
37103 26 20 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20  & !star_oh(z+1) 
37104 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
37105 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
37106 65 70 20 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d  ep 5b */.  if( m
37107 5f 67 74 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d  _gt_1(z) && z[0]
37108 3d 3d 27 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27  =='l' && z[1]=='
37109 6c 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  l' ){.    z++;. 
3710a 20 7d 0a 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20   }..  /* z[] is 
3710b 6e 6f 77 20 74 68 65 20 73 74 65 6d 6d 65 64 20  now the stemmed 
3710c 77 6f 72 64 20 69 6e 20 72 65 76 65 72 73 65 20  word in reverse 
3710d 6f 72 64 65 72 2e 20 20 46 6c 69 70 20 69 74 20  order.  Flip it 
3710e 62 61 63 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64  back.  ** around
3710f 20 69 6e 74 6f 20 66 6f 72 77 61 72 64 20 6f 72   into forward or
37110 64 65 72 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a  der and return..
37111 20 20 2a 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20    */.  *pnOut = 
37112 69 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  i = (int)strlen(
37113 7a 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20  z);.  zOut[i] = 
37114 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 29  0;.  while( *z )
37115 7b 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69 5d 20  {.    zOut[--i] 
37116 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a  = *(z++);.  }.}.
37117 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72  ./*.** Character
37118 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61  s that can be pa
37119 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20  rt of a token.  
3711a 57 65 20 61 73 73 75 6d 65 20 61 6e 79 20 63 68  We assume any ch
3711b 61 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f 73 65  aracter.** whose
3711c 20 76 61 6c 75 65 20 69 73 20 67 72 65 61 74 65   value is greate
3711d 72 20 74 68 61 6e 20 30 78 38 30 20 28 61 6e 79  r than 0x80 (any
3711e 20 55 54 46 20 63 68 61 72 61 63 74 65 72 29 20   UTF character) 
3711f 63 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74 20 6f  can be.** part o
37120 66 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e 20 6f  f a token.  In o
37121 74 68 65 72 20 77 6f 72 64 73 2c 20 64 65 6c 69  ther words, deli
37122 6d 69 74 65 72 73 20 61 6c 6c 20 6d 75 73 74 20  miters all must 
37123 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 6f  have.** values o
37124 66 20 30 78 37 66 20 6f 72 20 6c 6f 77 65 72 2e  f 0x7f or lower.
37125 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
37126 20 63 68 61 72 20 70 6f 72 74 65 72 49 64 43 68   char porterIdCh
37127 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78  ar[] = {./* x0 x
37128 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36  1 x2 x3 x4 x5 x6
37129 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20   x7 x8 x9 xA xB 
3712a 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20  xC xD xE xF */. 
3712b 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31     1, 1, 1, 1, 1
3712c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3712d 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3712e 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  0,  /* 3x */.   
3712f 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
37130 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37131 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37132 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31    /* 4x */.    1
37133 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37134 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37135 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20  0, 0, 0, 0, 1,  
37136 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20  /* 5x */.    0, 
37137 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37138 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37139 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
3713a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c   6x */.    1, 1,
3713b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3713c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
3713d 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37  , 0, 0, 0,  /* 7
3713e 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
3713f 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28 63 68  isDelim(C) (((ch
37140 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26 26 20  =C)&0x80)==0 && 
37141 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70 6f 72  (ch<0x30 || !por
37142 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30 78 33  terIdChar[ch-0x3
37143 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  0]))../*.** Extr
37144 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
37145 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
37146 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20  zation cursor.  
37147 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a  The cursor must.
37148 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
37149 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ned by a prior c
3714a 61 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f 70 65  all to porterOpe
3714b 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n()..*/.static i
3714c 6e 74 20 70 6f 72 74 65 72 4e 65 78 74 28 0a 20  nt porterNext(. 
3714d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3714e 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
3714f 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72  or,  /* Cursor r
37150 65 74 75 72 6e 65 64 20 62 79 20 70 6f 72 74 65  eturned by porte
37151 72 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  rOpen */.  const
37152 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65 6e 2c   char **pzToken,
37153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37154 2a 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65 6e 20  * OUT: *pzToken 
37155 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78  is the token tex
37156 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79  t */.  int *pnBy
37157 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
37158 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
37159 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  T: Number of byt
3715a 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20  es in token */. 
3715b 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
3715c 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
3715d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61       /* OUT: Sta
3715e 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  rting offset of 
3715f 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
37160 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20  piEndOffset,    
37161 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37162 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66  * OUT: Ending of
37163 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
37164 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69  .  int *piPositi
37165 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
37166 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
37167 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20  osition integer 
37168 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  of token */.){. 
37169 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
3716a 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70  r_cursor *c = (p
3716b 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
3716c 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
3716d 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  r;.  const char 
3716e 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74 3b 0a  *z = c->zInput;.
3716f 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66  .  while( c->iOf
37170 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 29  fset<c->nInput )
37171 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  {.    int iStart
37172 4f 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20 20 20  Offset, ch;..   
37173 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65   /* Scan past de
37174 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
37175 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  rs */.    while(
37176 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
37177 49 6e 70 75 74 20 26 26 20 69 73 44 65 6c 69 6d  Input && isDelim
37178 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20  (z[c->iOffset]) 
37179 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
3717a 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
3717b 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64    /* Count non-d
3717c 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74  elimiter charact
3717d 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61  ers. */.    iSta
3717e 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  rtOffset = c->iO
3717f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65  ffset;.    while
37180 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e  ( c->iOffset<c->
37181 6e 49 6e 70 75 74 20 26 26 20 21 69 73 44 65 6c  nInput && !isDel
37182 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  im(z[c->iOffset]
37183 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
37184 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
37185 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73      if( c->iOffs
37186 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  et>iStartOffset 
37187 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  ){.      int n =
37188 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74 61   c->iOffset-iSta
37189 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
3718a 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f 63 61  if( n>c->nAlloca
3718b 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ted ){.        c
3718c 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e  ->nAllocated = n
3718d 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  +20;.        c->
3718e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  zToken = sqlite3
3718f 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54 6f 6b  _realloc(c->zTok
37190 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65  en, c->nAllocate
37191 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
37192 63 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20  c->zToken==NULL 
37193 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
37194 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
37195 20 20 20 20 20 70 6f 72 74 65 72 5f 73 74 65 6d       porter_stem
37196 6d 65 72 28 26 7a 5b 69 53 74 61 72 74 4f 66 66  mer(&z[iStartOff
37197 73 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54 6f 6b  set], n, c->zTok
37198 65 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a 20 20  en, pnBytes);.  
37199 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d 20 63      *pzToken = c
3719a 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  ->zToken;.      
3719b 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d  *piStartOffset =
3719c 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20   iStartOffset;. 
3719d 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65       *piEndOffse
3719e 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
3719f 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f        *piPositio
371a0 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b  n = c->iToken++;
371a1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
371a2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
371a3 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
371a4 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a  TE_DONE;.}../*.*
371a5 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75  * The set of rou
371a6 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65  tines that imple
371a7 6d 65 6e 74 20 74 68 65 20 70 6f 72 74 65 72 2d  ment the porter-
371a8 73 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69 7a 65  stemmer tokenize
371a9 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  r.*/.static cons
371aa 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
371ab 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65  zer_module porte
371ac 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
371ad 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f 72 74   = {.  0,.  port
371ae 65 72 43 72 65 61 74 65 2c 0a 20 20 70 6f 72 74  erCreate,.  port
371af 65 72 44 65 73 74 72 6f 79 2c 0a 20 20 70 6f 72  erDestroy,.  por
371b0 74 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72 74 65  terOpen,.  porte
371b1 72 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74 65 72  rClose,.  porter
371b2 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  Next,.};../*.** 
371b3 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
371b4 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2e  orter tokenizer.
371b5 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
371b6 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a  er to the new.**
371b7 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a 70   tokenizer in *p
371b8 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54  pModule.*/.SQLIT
371b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
371ba 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
371bb 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
371bc 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
371bd 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
371be 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20  t**ppModule.){. 
371bf 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 70 6f   *ppModule = &po
371c0 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  rterTokenizerMod
371c1 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ule;.}..#endif /
371c2 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
371c3 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
371c4 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
371c5 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
371c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
371c7 66 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20  f fts3_porter.c 
371c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
371cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
371cc 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e   file fts3_token
371cd 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.c *********
371ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
371d0 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a   2007 June 22.**
371d1 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
371d2 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
371d3 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
371d4 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
371d5 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
371d6 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
371d7 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
371d8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
371d9 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
371da 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
371db 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
371dc 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
371dd 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
371de 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
371df 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
371e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
371e1 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
371e2 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
371e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
371e7 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  *.**.** This is 
371e8 70 61 72 74 20 6f 66 20 61 6e 20 53 51 4c 69 74  part of an SQLit
371e9 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
371ea 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20  nting full-text 
371eb 73 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69 73 20  search..** This 
371ec 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
371ed 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 67  implements the g
371ee 65 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a 65 72  eneric tokenizer
371ef 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a   interface..*/..
371f0 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
371f1 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
371f2 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
371f3 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
371f4 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
371f5 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
371f6 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
371f7 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
371f8 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
371f9 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
371fa 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
371fb 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
371fc 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
371fd 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
371fe 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
371ff 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
37200 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
37201 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
37202 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
37203 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
37204 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
37205 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 6e  ABLE_FTS3)..#ifn
37206 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
37207 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
37208 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a  ON_INIT1.#endif.
37209 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3720a 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  tation of the SQ
3720b 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
3720c 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  n for accessing 
3720d 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a  the underlying .
3720e 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 54  ** hash table. T
3720f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
37210 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f   be called as fo
37211 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  llows:.**.**   S
37212 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
37213 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
37214 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  );.**   SELECT <
37215 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
37216 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e  key-name>, <poin
37217 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65  ter>);.**.** whe
37218 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  re <function-nam
37219 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70  e> is the name p
3721a 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
3721b 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
3721c 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74  to the sqlite3Ft
3721d 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
3721e 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e  ) function (e.g.
3721f 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   'fts3_tokenizer
37220 27 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ')..**.** If the
37221 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d   <pointer> argum
37222 65 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64  ent is specified
37223 2c 20 69 74 20 6d 75 73 74 20 62 65 20 61 20 62  , it must be a b
37224 6c 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63 6f 6e  lob value.** con
37225 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  taining a pointe
37226 72 20 74 6f 20 62 65 20 73 74 6f 72 65 64 20 61  r to be stored a
37227 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20  s the hash data 
37228 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
37229 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 3c   to the string <
3722a 6b 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20 3c 70  key-name>. If <p
3722b 6f 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74 20 73  ointer> is not s
3722c 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 0a 2a  pecified, then.*
3722d 2a 20 74 68 65 20 73 74 72 69 6e 67 20 3c 6b 65  * the string <ke
3722e 79 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61 6c 72  y-name> must alr
3722f 65 61 64 79 20 65 78 69 73 74 20 69 6e 20 74 68  eady exist in th
37230 65 20 68 61 73 20 74 61 62 6c 65 2e 20 4f 74 68  e has table. Oth
37231 65 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20 65 72  erwise,.** an er
37232 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
37233 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
37234 72 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69 6e 74  r not the <point
37235 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  er> argument is 
37236 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 20 76  specified, the v
37237 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a  alue returned.**
37238 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 74 61   is a blob conta
37239 69 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ining the pointe
3723a 72 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20  r stored as the 
3723b 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65 73  hash data corres
3723c 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 73 74  ponding.** to st
3723d 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20  ring <key-name> 
3723e 28 61 66 74 65 72 20 74 68 65 20 68 61 73 68 2d  (after the hash-
3723f 74 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64  table is updated
37240 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29  , if applicable)
37241 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
37242 20 73 63 61 6c 61 72 46 75 6e 63 28 0a 20 20 73   scalarFunc(.  s
37243 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
37244 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
37245 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
37246 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
37247 20 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   Fts3Hash *pHash
37248 3b 0a 20 20 76 6f 69 64 20 2a 70 50 74 72 20 3d  ;.  void *pPtr =
37249 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   0;.  const unsi
3724a 67 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61 6d 65  gned char *zName
3724b 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a  ;.  int nName;..
3724c 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
3724d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  1 || argc==2 );.
3724e 0a 20 20 70 48 61 73 68 20 3d 20 28 46 74 73 33  .  pHash = (Fts3
3724f 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75  Hash *)sqlite3_u
37250 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
37251 29 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  );..  zName = sq
37252 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
37253 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 4e 61  (argv[0]);.  nNa
37254 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
37255 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
37256 29 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  )+1;..  if( argc
37257 3d 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  ==2 ){.    void 
37258 2a 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74 20 6e  *pOld;.    int n
37259 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3725a 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b  _bytes(argv[1]);
3725b 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69 7a 65  .    if( n!=size
3725c 6f 66 28 70 50 74 72 29 20 29 7b 0a 20 20 20 20  of(pPtr) ){.    
3725d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3725e 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3725f 22 61 72 67 75 6d 65 6e 74 20 74 79 70 65 20 6d  "argument type m
37260 69 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b 0a 20  ismatch", -1);. 
37261 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
37262 20 7d 0a 20 20 20 20 70 50 74 72 20 3d 20 2a 28   }.    pPtr = *(
37263 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 5f  void **)sqlite3_
37264 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
37265 31 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20  1]);.    pOld = 
37266 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
37267 6e 73 65 72 74 28 70 48 61 73 68 2c 20 28 76 6f  nsert(pHash, (vo
37268 69 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  id *)zName, nNam
37269 65 2c 20 70 50 74 72 29 3b 0a 20 20 20 20 69 66  e, pPtr);.    if
3726a 28 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29 7b 0a  ( pOld==pPtr ){.
3726b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
3726c 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
3726d 78 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  xt, "out of memo
3726e 72 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ry", -1);.      
3726f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
37270 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 74 72 20  }else{.    pPtr 
37271 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  = sqlite3Fts3Has
37272 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61  hFind(pHash, zNa
37273 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
37274 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20  if( !pPtr ){.   
37275 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
37276 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
37277 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
37278 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
37279 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
3727a 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
3727b 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a  ext, zErr, -1);.
3727c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
3727d 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  ee(zErr);.      
3727e 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
3727f 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  }..  sqlite3_res
37280 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
37281 2c 20 28 76 6f 69 64 20 2a 29 26 70 50 74 72 2c  , (void *)&pPtr,
37282 20 73 69 7a 65 6f 66 28 70 50 74 72 29 2c 20 53   sizeof(pPtr), S
37283 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
37284 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
37285 66 74 73 33 49 73 49 64 43 68 61 72 28 63 68 61  fts3IsIdChar(cha
37286 72 20 63 29 7b 0a 20 20 73 74 61 74 69 63 20 63  r c){.  static c
37287 6f 6e 73 74 20 63 68 61 72 20 69 73 46 74 73 49  onst char isFtsI
37288 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 20 20 20 20  dChar[] = {.    
37289 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
3728a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3728b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
3728c 2c 20 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20  ,  /* 0x */.    
3728d 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
3728e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3728f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37290 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20  ,  /* 1x */.    
37291 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c    0, 0, 0, 0, 1,
37292 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
37293 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37294 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  ,  /* 2x */.    
37295 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
37296 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37297 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37298 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  ,  /* 3x */.    
37299 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
3729a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3729b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3729c 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20  ,  /* 4x */.    
3729d 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
3729e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3729f 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31  1, 0, 0, 0, 0, 1
372a0 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
372a1 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
372a2 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
372a3 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
372a4 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20  ,  /* 6x */.    
372a5 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
372a6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
372a7 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
372a8 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 7d 3b  ,  /* 7x */.  };
372a9 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
372aa 30 20 7c 7c 20 69 73 46 74 73 49 64 43 68 61 72  0 || isFtsIdChar
372ab 5b 28 69 6e 74 29 28 63 29 5d 29 3b 0a 7d 0a 0a  [(int)(c)]);.}..
372ac 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
372ad 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
372ae 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65 6e 28  e3Fts3NextToken(
372af 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 72  const char *zStr
372b0 2c 20 69 6e 74 20 2a 70 6e 29 7b 0a 20 20 63 6f  , int *pn){.  co
372b1 6e 73 74 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20  nst char *z1;.  
372b2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d  const char *z2 =
372b3 20 30 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74   0;..  /* Find t
372b4 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
372b5 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 2a 2f 0a 20  next token. */. 
372b6 20 7a 31 20 3d 20 7a 53 74 72 3b 0a 20 20 77 68   z1 = zStr;.  wh
372b7 69 6c 65 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20  ile( z2==0 ){.  
372b8 20 20 73 77 69 74 63 68 28 20 2a 7a 31 20 29 7b    switch( *z1 ){
372b9 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 30 27  .      case '\0'
372ba 3a 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  : return 0;     
372bb 20 20 20 2f 2a 20 4e 6f 20 6d 6f 72 65 20 74 6f     /* No more to
372bc 6b 65 6e 73 20 68 65 72 65 20 2a 2f 0a 20 20 20  kens here */.   
372bd 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a 20 20     case '\'':.  
372be 20 20 20 20 63 61 73 65 20 27 22 27 3a 0a 20 20      case '"':.  
372bf 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 7b 0a      case '`': {.
372c0 20 20 20 20 20 20 20 20 7a 32 20 3d 20 26 7a 31          z2 = &z1
372c1 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 77 68 69  [1];.        whi
372c2 6c 65 28 20 2a 7a 32 20 26 26 20 28 7a 32 5b 30  le( *z2 && (z2[0
372c3 5d 21 3d 2a 7a 31 20 7c 7c 20 7a 32 5b 31 5d 3d  ]!=*z1 || z2[1]=
372c4 3d 2a 7a 31 29 20 29 20 7a 32 2b 2b 3b 0a 20 20  =*z1) ) z2++;.  
372c5 20 20 20 20 20 20 69 66 28 20 2a 7a 32 20 29 20        if( *z2 ) 
372c6 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  z2++;.        br
372c7 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
372c8 20 20 20 63 61 73 65 20 27 5b 27 3a 0a 20 20 20     case '[':.   
372c9 20 20 20 20 20 7a 32 20 3d 20 26 7a 31 5b 31 5d       z2 = &z1[1]
372ca 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
372cb 20 2a 7a 32 20 26 26 20 7a 32 5b 30 5d 21 3d 27   *z2 && z2[0]!='
372cc 5d 27 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20  ]' ) z2++;.     
372cd 20 20 20 69 66 28 20 2a 7a 32 20 29 20 7a 32 2b     if( *z2 ) z2+
372ce 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
372cf 3b 0a 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  ;..      default
372d0 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 74  :.        if( ft
372d1 73 33 49 73 49 64 43 68 61 72 28 2a 7a 31 29 20  s3IsIdChar(*z1) 
372d2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 32 20  ){.          z2 
372d3 3d 20 26 7a 31 5b 31 5d 3b 0a 20 20 20 20 20 20  = &z1[1];.      
372d4 20 20 20 20 77 68 69 6c 65 28 20 66 74 73 33 49      while( fts3I
372d5 73 49 64 43 68 61 72 28 2a 7a 32 29 20 29 20 7a  sIdChar(*z2) ) z
372d6 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  2++;.        }el
372d7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 31  se{.          z1
372d8 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
372d9 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 6e 20 3d    }.  }..  *pn =
372da 20 28 69 6e 74 29 28 7a 32 2d 7a 31 29 3b 0a 20   (int)(z2-z1);. 
372db 20 72 65 74 75 72 6e 20 7a 31 3b 0a 7d 0a 0a 53   return z1;.}..S
372dc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
372dd 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  t sqlite3Fts3Ini
372de 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 46 74  tTokenizer(.  Ft
372df 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20  s3Hash *pHash,  
372e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
372e1 20 54 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68 20   Tokenizer hash 
372e2 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  table */.  const
372e3 20 63 68 61 72 20 2a 7a 41 72 67 2c 20 20 20 20   char *zArg,    
372e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
372e5 73 73 69 62 6c 65 20 74 6f 6b 65 6e 69 7a 65 72  ssible tokenizer
372e6 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e 20 2a   specification *
372e7 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
372e8 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 2c 20 20  nizer **ppTok,  
372e9 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65      /* OUT: Toke
372ea 6e 69 7a 65 72 20 28 69 66 20 61 70 70 6c 69 63  nizer (if applic
372eb 61 62 6c 65 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  able) */.  const
372ec 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65 6e 69   char **pzTokeni
372ed 7a 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 55  zer,       /* OU
372ee 54 3a 20 53 65 74 20 74 6f 20 7a 41 72 67 20 69  T: Set to zArg i
372ef 66 20 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  f is tokenizer *
372f0 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
372f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372f2 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20      /* OUT: Set 
372f3 74 6f 20 6d 61 6c 6c 6f 63 65 64 20 65 72 72 6f  to malloced erro
372f4 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 29 7b 0a  r message */.){.
372f5 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
372f6 20 2a 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 41   *z = (char *)zA
372f7 72 67 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63  rg;.  int n;.  c
372f8 68 61 72 20 2a 7a 43 6f 70 79 3b 0a 20 20 63 68  har *zCopy;.  ch
372f9 61 72 20 2a 7a 45 6e 64 3b 20 20 20 20 20 20 20  ar *zEnd;       
372fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
372fb 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 75 6c 2d   Pointer to nul-
372fc 74 65 72 6d 20 6f 66 20 7a 43 6f 70 79 20 2a 2f  term of zCopy */
372fd 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
372fe 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 6d 3b 0a  izer_module *m;.
372ff 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20  .  if( !z ){.   
37300 20 7a 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33   zCopy = sqlite3
37301 5f 6d 70 72 69 6e 74 66 28 22 73 69 6d 70 6c 65  _mprintf("simple
37302 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ");.  }else{.   
37303 20 77 68 69 6c 65 28 20 28 2a 7a 26 30 78 38 30   while( (*z&0x80
37304 29 20 26 26 20 69 73 73 70 61 63 65 28 2a 7a 29  ) && isspace(*z)
37305 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20   ) z++;.    if( 
37306 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70  sqlite3_strnicmp
37307 28 7a 2c 20 22 74 6f 6b 65 6e 69 7a 65 22 2c 20  (z, "tokenize", 
37308 38 29 20 7c 7c 20 66 74 73 33 49 73 49 64 43 68  8) || fts3IsIdCh
37309 61 72 28 7a 5b 38 5d 29 29 7b 0a 20 20 20 20 20  ar(z[8])){.     
3730a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3730b 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f  K;.    }.    zCo
3730c 70 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  py = sqlite3_mpr
3730d 69 6e 74 66 28 22 25 73 22 2c 20 26 7a 5b 38 5d  intf("%s", &z[8]
3730e 29 3b 0a 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 69  );.    *pzTokeni
3730f 7a 65 72 20 3d 20 7a 41 72 67 3b 0a 20 20 7d 0a  zer = zArg;.  }.
37310 20 20 69 66 28 20 21 7a 43 6f 70 79 20 29 7b 0a    if( !zCopy ){.
37311 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37312 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
37313 7a 45 6e 64 20 3d 20 26 7a 43 6f 70 79 5b 73 74  zEnd = &zCopy[st
37314 72 6c 65 6e 28 7a 43 6f 70 79 29 5d 3b 0a 0a 20  rlen(zCopy)];.. 
37315 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c   z = (char *)sql
37316 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65  ite3Fts3NextToke
37317 6e 28 7a 43 6f 70 79 2c 20 26 6e 29 3b 0a 20 20  n(zCopy, &n);.  
37318 7a 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 73  z[n] = '\0';.  s
37319 71 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74  qlite3Fts3Dequot
3731a 65 28 7a 29 3b 0a 0a 20 20 6d 20 3d 20 28 73 71  e(z);..  m = (sq
3731b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3731c 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  module *)sqlite3
3731d 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61  Fts3HashFind(pHa
3731e 73 68 2c 20 7a 2c 20 28 69 6e 74 29 73 74 72 6c  sh, z, (int)strl
3731f 65 6e 28 7a 29 2b 31 29 3b 0a 20 20 69 66 28 20  en(z)+1);.  if( 
37320 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  !m ){.    *pzErr
37321 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
37322 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65  tf("unknown toke
37323 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 29 3b 0a  nizer: %s", z);.
37324 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37325 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
37326 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
37327 2a 61 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 69  *aArg = 0;.    i
37328 6e 74 20 69 41 72 67 20 3d 20 30 3b 0a 20 20 20  nt iArg = 0;.   
37329 20 7a 20 3d 20 26 7a 5b 6e 2b 31 5d 3b 0a 20 20   z = &z[n+1];.  
3732a 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20    while( z<zEnd 
3732b 26 26 20 28 4e 55 4c 4c 21 3d 28 7a 20 3d 20 28  && (NULL!=(z = (
3732c 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 46 74  char *)sqlite3Ft
3732d 73 33 4e 65 78 74 54 6f 6b 65 6e 28 7a 2c 20 26  s3NextToken(z, &
3732e 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  n))) ){.      in
3732f 74 20 6e 4e 65 77 20 3d 20 73 69 7a 65 6f 66 28  t nNew = sizeof(
37330 63 68 61 72 20 2a 29 2a 28 69 41 72 67 2b 31 29  char *)*(iArg+1)
37331 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e  ;.      char con
37332 73 74 20 2a 2a 61 4e 65 77 20 3d 20 28 63 6f 6e  st **aNew = (con
37333 73 74 20 63 68 61 72 20 2a 2a 29 73 71 6c 69 74  st char **)sqlit
37334 65 33 5f 72 65 61 6c 6c 6f 63 28 28 76 6f 69 64  e3_realloc((void
37335 20 2a 29 61 41 72 67 2c 20 6e 4e 65 77 29 3b 0a   *)aArg, nNew);.
37336 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 20        if( !aNew 
37337 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
37338 65 33 5f 66 72 65 65 28 7a 43 6f 70 79 29 3b 0a  e3_free(zCopy);.
37339 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3733a 66 72 65 65 28 28 76 6f 69 64 20 2a 29 61 41 72  free((void *)aAr
3733b 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  g);.        retu
3733c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
3733d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
3733e 41 72 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20  Arg = aNew;.    
3733f 20 20 61 41 72 67 5b 69 41 72 67 2b 2b 5d 20 3d    aArg[iArg++] =
37340 20 7a 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d   z;.      z[n] =
37341 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71 6c   '\0';.      sql
37342 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65 28  ite3Fts3Dequote(
37343 7a 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 26 7a  z);.      z = &z
37344 5b 6e 2b 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  [n+1];.    }.   
37345 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65   rc = m->xCreate
37346 28 69 41 72 67 2c 20 61 41 72 67 2c 20 70 70 54  (iArg, aArg, ppT
37347 6f 6b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ok);.    assert(
37348 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
37349 7c 20 2a 70 70 54 6f 6b 20 29 3b 0a 20 20 20 20  | *ppTok );.    
3734a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3734b 4b 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  K ){.      *pzEr
3734c 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
3734d 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  ntf("unknown tok
3734e 65 6e 69 7a 65 72 22 29 3b 0a 20 20 20 20 7d 65  enizer");.    }e
3734f 6c 73 65 7b 0a 20 20 20 20 20 20 28 2a 70 70 54  lse{.      (*ppT
37350 6f 6b 29 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d  ok)->pModule = m
37351 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ; .    }.    sql
37352 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20  ite3_free((void 
37353 2a 29 61 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20  *)aArg);.  }..  
37354 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f  sqlite3_free(zCo
37355 70 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  py);.  return rc
37356 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
37357 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a  ITE_TEST.../*.**
37358 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
37359 6f 66 20 61 20 73 70 65 63 69 61 6c 20 53 51 4c  of a special SQL
3735a 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3735b 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f 6b   for testing tok
3735c 65 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73 69  enizers .** desi
3735d 67 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  gned to be used 
3735e 69 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68 20  in concert with 
3735f 74 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67 20  the Tcl testing 
37360 66 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73 0a  framework. This.
37361 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ** function must
37362 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20   be called with 
37363 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a  two arguments:.*
37364 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  *.**   SELECT <f
37365 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
37366 65 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74  ey-name>, <input
37367 2d 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20 20  -string>);.**   
37368 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e  SELECT <function
37369 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65  -name>(<key-name
3736a 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a  >, <pointer>);.*
3736b 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63  *.** where <func
3736c 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68  tion-name> is th
3736d 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73  e name passed as
3736e 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3736f 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ment.** to the s
37370 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
37371 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69  shTable() functi
37372 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74  on (e.g. 'fts3_t
37373 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63 6f  okenizer').** co
37374 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20  ncatenated with 
37375 74 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65 73  the string '_tes
37376 74 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74  t' (e.g. 'fts3_t
37377 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29 2e  okenizer_test').
37378 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
37379 6e 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  n value is a str
3737a 69 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65 20  ing that may be 
3737b 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61  interpreted as a
3737c 20 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46 6f   Tcl.** list. Fo
3737d 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20  r each token in 
3737e 74 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e  the <input-strin
3737f 67 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65 6e  g>, three elemen
37380 74 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64 20  ts are.** added 
37381 74 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  to the returned 
37382 6c 69 73 74 2e 20 54 68 65 20 66 69 72 73 74 20  list. The first 
37383 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f 73  is the token pos
37384 69 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20 73  ition, the .** s
37385 65 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f 6b  econd is the tok
37386 65 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64 2c  en text (folded,
37387 20 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29 20   stemmed, etc.) 
37388 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 69 73  and the third is
37389 20 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69 6e   the.** substrin
3738a 67 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72 69  g of <input-stri
3738b 6e 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ng> associated w
3738c 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20 46  ith the token. F
3738d 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a 20  or example, .** 
3738e 75 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74 2d  using the built-
3738f 69 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65  in "simple" toke
37390 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  nizer:.**.**   S
37391 45 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e 69  ELECT fts_tokeni
37392 7a 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c 65  zer_test('simple
37393 27 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65 20  ', 'I don't see 
37394 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69 6c  how');.**.** wil
37395 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 72  l return the str
37396 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b 30  ing:.**.**   "{0
37397 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27   i I 1 dont don'
37398 74 20 32 20 73 65 65 20 73 65 65 20 33 20 68 6f  t 2 see see 3 ho
37399 77 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a 2f  w how}".**   .*/
3739a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73  .static void tes
3739b 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  tFunc(.  sqlite3
3739c 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
3739d 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
3739e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
3739f 2a 61 72 67 76 0a 29 7b 0a 20 20 46 74 73 33 48  *argv.){.  Fts3H
373a0 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73 71  ash *pHash;.  sq
373a1 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
373a2 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71 6c  module *p;.  sql
373a3 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
373a4 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a  pTokenizer = 0;.
373a5 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
373a6 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73 72  zer_cursor *pCsr
373a7 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63   = 0;..  const c
373a8 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a  har *zErr = 0;..
373a9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
373aa 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  ame;.  int nName
373ab 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
373ac 7a 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49  zInput;.  int nI
373ad 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20 63  nput;..  const c
373ae 68 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 0a  har *zArg = 0;..
373af 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
373b0 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b  oken;.  int nTok
373b1 65 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74  en;.  int iStart
373b2 3b 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20  ;.  int iEnd;.  
373b3 69 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63 6c  int iPos;..  Tcl
373b4 5f 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20 61  _Obj *pRet;..  a
373b5 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c  ssert( argc==2 |
373b6 7c 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20 20  | argc==3 );..  
373b7 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  nName = sqlite3_
373b8 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
373b9 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  [0]);.  zName = 
373ba 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
373bb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
373bc 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e  (argv[0]);.  nIn
373bd 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
373be 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61  lue_bytes(argv[a
373bf 72 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75  rgc-1]);.  zInpu
373c0 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  t = (const char 
373c1 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
373c2 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31  text(argv[argc-1
373c3 5d 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d  ]);..  if( argc=
373c4 3d 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20 3d  =3 ){.    zArg =
373c5 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
373c6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
373c7 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a  t(argv[1]);.  }.
373c8 0a 20 20 70 48 61 73 68 20 3d 20 28 46 74 73 33  .  pHash = (Fts3
373c9 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75  Hash *)sqlite3_u
373ca 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
373cb 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  );.  p = (sqlite
373cc 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
373cd 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33  le *)sqlite3Fts3
373ce 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20  HashFind(pHash, 
373cf 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
373d0 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ..  if( !p ){.  
373d1 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73    char *zErr = s
373d2 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
373d3 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
373d4 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  r: %s", zName);.
373d5 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
373d6 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
373d7 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20  , zErr, -1);.   
373d8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
373d9 72 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  rr);.    return;
373da 0a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20 54  .  }..  pRet = T
373db 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 54  cl_NewObj();.  T
373dc 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28  cl_IncrRefCount(
373dd 70 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53 51  pRet);..  if( SQ
373de 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65  LITE_OK!=p->xCre
373df 61 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20 30  ate(zArg ? 1 : 0
373e0 2c 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65 6e  , &zArg, &pToken
373e1 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  izer) ){.    zEr
373e2 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43  r = "error in xC
373e3 72 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67 6f  reate()";.    go
373e4 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20  to finish;.  }. 
373e5 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
373e6 64 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28 20  dule = p;.  if( 
373e7 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 4f  SQLITE_OK!=p->xO
373e8 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
373e9 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  zInput, nInput, 
373ea 26 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45  &pCsr) ){.    zE
373eb 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78  rr = "error in x
373ec 4f 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74  Open()";.    got
373ed 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20  o finish;.  }.  
373ee 70 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  pCsr->pTokenizer
373ef 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a   = pTokenizer;..
373f0 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f    while( SQLITE_
373f1 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73  OK==p->xNext(pCs
373f2 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
373f3 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69  ken, &iStart, &i
373f4 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20  End, &iPos) ){. 
373f5 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70     Tcl_ListObjAp
373f6 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70  pendElement(0, p
373f7 52 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f  Ret, Tcl_NewIntO
373f8 62 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54  bj(iPos));.    T
373f9 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64  cl_ListObjAppend
373fa 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c  Element(0, pRet,
373fb 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62   Tcl_NewStringOb
373fc 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  j(zToken, nToken
373fd 29 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d  ));.    zToken =
373fe 20 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d   &zInput[iStart]
373ff 3b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69  ;.    nToken = i
37400 45 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20  End-iStart;.    
37401 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e  Tcl_ListObjAppen
37402 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74  dElement(0, pRet
37403 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f  , Tcl_NewStringO
37404 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  bj(zToken, nToke
37405 6e 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  n));.  }..  if( 
37406 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43  SQLITE_OK!=p->xC
37407 6c 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20  lose(pCsr) ){.  
37408 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
37409 69 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20  in xClose()";.  
3740a 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20    goto finish;. 
3740b 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f   }.  if( SQLITE_
3740c 4f 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28  OK!=p->xDestroy(
3740d 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20  pTokenizer) ){. 
3740e 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
3740f 20 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b   in xDestroy()";
37410 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68  .    goto finish
37411 3b 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20  ;.  }..finish:. 
37412 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20   if( zErr ){.   
37413 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37414 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
37415 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73  Err, -1);.  }els
37416 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
37417 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
37418 78 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e  xt, Tcl_GetStrin
37419 67 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c  g(pRet), -1, SQL
3741a 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
3741b 20 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65    }.  Tcl_DecrRe
3741c 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a  fCount(pRet);.}.
3741d 0a 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69  .static.int regi
3741e 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20  sterTokenizer(. 
3741f 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
37420 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
37421 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
37422 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
37423 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *p.){.  int rc;.
37424 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
37425 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63  pStmt;.  const c
37426 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45  har zSql[] = "SE
37427 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69  LECT fts3_tokeni
37428 7a 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72  zer(?, ?)";..  r
37429 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
3742a 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c  are_v2(db, zSql,
3742b 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
3742c 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3742d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
3742e 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71  rn rc;.  }..  sq
3742f 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
37430 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c  pStmt, 1, zName,
37431 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
37432 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62  IC);.  sqlite3_b
37433 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  ind_blob(pStmt, 
37434 32 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29  2, &p, sizeof(p)
37435 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
37436 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
37437 28 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75  (pStmt);..  retu
37438 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
37439 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73  ize(pStmt);.}..s
3743a 74 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54  tatic.int queryT
3743b 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69  okenizer(.  sqli
3743c 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72  te3 *db, .  char
3743d 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e   *zName,  .  con
3743e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
3743f 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70  izer_module **pp
37440 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
37441 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
37442 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  tmt;.  const cha
37443 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45  r zSql[] = "SELE
37444 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  CT fts3_tokenize
37445 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20  r(?)";..  *pp = 
37446 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
37447 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c  3_prepare_v2(db,
37448 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
37449 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  t, 0);.  if( rc!
3744a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3744b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
3744c 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
3744d 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20  _text(pStmt, 1, 
3744e 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54  zName, -1, SQLIT
3744f 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28  E_STATIC);.  if(
37450 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
37451 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
37452 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
37453 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
37454 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54  pStmt, 0)==SQLIT
37455 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
37456 6d 65 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 70  memcpy((void *)p
37457 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
37458 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
37459 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
3745a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
3745b 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
3745c 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
3745d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3745e 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
3745f 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
37460 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
37461 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
37462 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
37463 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
37464 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61  ation of the sca
37465 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73  lar function fts
37466 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65  3_tokenizer_inte
37467 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20  rnal_test()..** 
37468 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
37469 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
3746a 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f  g only, it is no
3746b 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  t included in th
3746c 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73  e.** build unles
3746d 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73  s SQLITE_TEST is
3746e 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
3746f 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
37470 68 69 73 20 69 73 20 74 6f 20 74 65 73 74 20 74  his is to test t
37471 68 61 74 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  hat the fts3_tok
37472 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f  enizer() functio
37473 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  n.** can be used
37474 20 61 73 20 64 65 73 69 67 6e 65 64 20 62 79 20   as designed by 
37475 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20 74 68  the C-code in th
37476 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  e queryTokenizer
37477 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74 65 72   and.** register
37478 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63  Tokenizer() func
37479 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54 68 65  tions above. The
3747a 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73  se two functions
3747b 20 61 72 65 20 72 65 70 65 61 74 65 64 0a 2a 2a   are repeated.**
3747c 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45 2e 74   in the README.t
3747d 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20 61 73  okenizer file as
3747e 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20   an example, so 
3747f 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
37480 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e  to.** test them.
37481 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68  .**.** To run th
37482 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75 61 74  e tests, evaluat
37483 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e  e the fts3_token
37484 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65  izer_internal_te
37485 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66  st() scalar.** f
37486 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20  unction with no 
37487 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73  arguments. An as
37488 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61 69 6c  sert() will fail
37489 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20 69 73   if a problem is
3748a 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 69 2e  .** detected. i.
3748b 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  e.:.**.**     SE
3748c 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69  LECT fts3_tokeni
3748d 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  zer_internal_tes
3748e 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  t();.**.*/.stati
3748f 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75  c void intTestFu
37490 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
37491 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
37492 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
37493 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
37494 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  gv.){.  int rc;.
37495 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
37496 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
37497 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71   *p1;.  const sq
37498 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
37499 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71  module *p2;.  sq
3749a 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c  lite3 *db = (sql
3749b 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75  ite3 *)sqlite3_u
3749c 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
3749d 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
3749e 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
3749f 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
374a0 28 61 72 67 76 29 3b 0a 0a 20 20 2f 2a 20 54 65  (argv);..  /* Te
374a1 73 74 20 74 68 65 20 71 75 65 72 79 20 66 75 6e  st the query fun
374a2 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
374a3 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
374a4 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 31 29  nizerModule(&p1)
374a5 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f  ;.  rc = queryTo
374a6 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 73 69 6d  kenizer(db, "sim
374a7 70 6c 65 22 2c 20 26 70 32 29 3b 0a 20 20 61 73  ple", &p2);.  as
374a8 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
374a9 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _OK );.  assert(
374aa 20 70 31 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20   p1==p2 );.  rc 
374ab 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  = queryTokenizer
374ac 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65  (db, "nosuchtoke
374ad 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20  nizer", &p2);.  
374ae 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
374af 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61 73  TE_ERROR );.  as
374b0 73 65 72 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20  sert( p2==0 );. 
374b1 20 61 73 73 65 72 74 28 20 30 3d 3d 73 74 72 63   assert( 0==strc
374b2 6d 70 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  mp(sqlite3_errms
374b3 67 28 64 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20  g(db), "unknown 
374b4 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63  tokenizer: nosuc
374b5 68 74 6f 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a  htokenizer") );.
374b6 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20 73  .  /* Test the s
374b7 74 6f 72 61 67 65 20 66 75 6e 63 74 69 6f 6e 20  torage function 
374b8 2a 2f 0a 20 20 72 63 20 3d 20 72 65 67 69 73 74  */.  rc = regist
374b9 65 72 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20  erTokenizer(db, 
374ba 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72  "nosuchtokenizer
374bb 22 2c 20 70 31 29 3b 0a 20 20 61 73 73 65 72 74  ", p1);.  assert
374bc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
374bd 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
374be 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
374bf 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
374c0 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &p2);.  assert( 
374c1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
374c2 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d 3d 70  .  assert( p2==p
374c3 31 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  1 );..  sqlite3_
374c4 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
374c5 65 78 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53  ext, "ok", -1, S
374c6 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
374c7 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ..#endif../*.** 
374c8 53 65 74 20 75 70 20 53 51 4c 20 6f 62 6a 65 63  Set up SQL objec
374c9 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 64  ts in database d
374ca 62 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  b used to access
374cb 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
374cc 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62  .** the hash tab
374cd 6c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  le pointed to by
374ce 20 61 72 67 75 6d 65 6e 74 20 70 48 61 73 68 2e   argument pHash.
374cf 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
374d0 6d 75 73 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69  must.** been ini
374d1 74 69 61 6c 69 73 65 64 20 74 6f 20 75 73 65 20  tialised to use 
374d2 73 74 72 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64  string keys, and
374d3 20 74 6f 20 74 61 6b 65 20 61 20 70 72 69 76 61   to take a priva
374d4 74 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74  te copy .** of t
374d5 68 65 20 6b 65 79 20 77 68 65 6e 20 61 20 76 61  he key when a va
374d6 6c 75 65 20 69 73 20 69 6e 73 65 72 74 65 64 2e  lue is inserted.
374d7 20 69 2e 65 2e 20 62 79 20 61 20 63 61 6c 6c 20   i.e. by a call 
374d8 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a  similar to:.**.*
374d9 2a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  *    sqlite3Fts3
374da 48 61 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20  HashInit(pHash, 
374db 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
374dc 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73  , 1);.**.** This
374dd 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 61   function adds a
374de 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
374df 20 28 73 65 65 20 68 65 61 64 65 72 20 63 6f 6d   (see header com
374e0 6d 65 6e 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63  ment above.** sc
374e1 61 6c 61 72 46 75 6e 63 28 29 20 69 6e 20 74 68  alarFunc() in th
374e2 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61  is file for deta
374e3 69 6c 73 29 20 61 6e 64 2c 20 69 66 20 45 4e 41  ils) and, if ENA
374e4 42 4c 45 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20  BLE_TABLE is.** 
374e5 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69  defined at compi
374e6 6c 61 74 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74  lation time, a t
374e7 65 6d 70 6f 72 61 72 79 20 76 69 72 74 75 61 6c  emporary virtual
374e8 20 74 61 62 6c 65 20 28 73 65 65 20 68 65 61 64   table (see head
374e9 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61  er .** comment a
374ea 62 6f 76 65 20 73 74 72 75 63 74 20 48 61 73 68  bove struct Hash
374eb 54 61 62 6c 65 56 74 61 62 29 20 74 6f 20 74 68  TableVtab) to th
374ec 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
374ed 61 2e 20 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76  a. Both .** prov
374ee 69 64 65 20 72 65 61 64 2f 77 72 69 74 65 20 61  ide read/write a
374ef 63 63 65 73 73 20 74 6f 20 74 68 65 20 63 6f 6e  ccess to the con
374f0 74 65 6e 74 73 20 6f 66 20 2a 70 48 61 73 68 2e  tents of *pHash.
374f1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
374f2 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
374f3 73 20 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d  s function, zNam
374f4 65 2c 20 69 73 20 75 73 65 64 20 61 73 20 74 68  e, is used as th
374f5 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74  e name.** of bot
374f6 68 20 74 68 65 20 73 63 61 6c 61 72 20 61 6e 64  h the scalar and
374f7 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20 74 68  , if created, th
374f8 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
374f9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
374fa 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
374fb 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
374fc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
374fd 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61  .  Fts3Hash *pHa
374fe 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  sh, .  const cha
374ff 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e  r *zName.){.  in
37500 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
37501 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 28 76  ;.  void *p = (v
37502 6f 69 64 20 2a 29 70 48 61 73 68 3b 0a 20 20 63  oid *)pHash;.  c
37503 6f 6e 73 74 20 69 6e 74 20 61 6e 79 20 3d 20 53  onst int any = S
37504 51 4c 49 54 45 5f 41 4e 59 3b 0a 20 20 63 68 61  QLITE_ANY;.  cha
37505 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a 20 20  r *zTest = 0;.  
37506 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d 20 30  char *zTest2 = 0
37507 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
37508 5f 54 45 53 54 0a 20 20 76 6f 69 64 20 2a 70 64  _TEST.  void *pd
37509 62 20 3d 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a  b = (void *)db;.
3750a 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69 74 65    zTest = sqlite
3750b 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 74 65  3_mprintf("%s_te
3750c 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a  st", zName);.  z
3750d 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65 33 5f  Test2 = sqlite3_
3750e 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e 74 65  mprintf("%s_inte
3750f 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d  rnal_test", zNam
37510 65 29 3b 0a 20 20 69 66 28 20 21 7a 54 65 73 74  e);.  if( !zTest
37511 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b 0a 20   || !zTest2 ){. 
37512 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
37513 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  OMEM;.  }.#endif
37514 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
37515 4b 21 3d 72 63 0a 20 20 20 7c 7c 20 53 51 4c 49  K!=rc.   || SQLI
37516 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
37517 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
37518 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
37519 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61  1, any, p, scala
3751a 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20  rFunc, 0, 0)).  
3751b 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
3751c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
3751d 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
3751e 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20   zName, 2, any, 
3751f 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30  p, scalarFunc, 0
37520 2c 20 30 29 29 0a 23 69 66 64 65 66 20 53 51 4c  , 0)).#ifdef SQL
37521 49 54 45 5f 54 45 53 54 0a 20 20 20 7c 7c 20 53  ITE_TEST.   || S
37522 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
37523 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
37524 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73  unction(db, zTes
37525 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74 65  t, 2, any, p, te
37526 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20  stFunc, 0, 0)). 
37527 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
37528 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
37529 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
3752a 2c 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79 2c  , zTest, 3, any,
3752b 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c   p, testFunc, 0,
3752c 20 30 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54   0)).   || SQLIT
3752d 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
3752e 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
3752f 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c 20  ion(db, zTest2, 
37530 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74  0, any, pdb, int
37531 54 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29  TestFunc, 0, 0))
37532 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 0a 20 20  .#endif.  );..  
37533 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
37534 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  st);.  sqlite3_f
37535 72 65 65 28 7a 54 65 73 74 32 29 3b 0a 20 20 72  ree(zTest2);.  r
37536 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
37537 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
37538 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
37539 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3753a 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
3753b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3753c 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65  End of fts3_toke
3753d 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  nizer.c ********
3753e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3753f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
37540 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
37541 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
37542 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a  tokenizer1.c ***
37543 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37544 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
37545 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31  /*.** 2006 Oct 1
37546 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
37547 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
37548 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
37549 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3754a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3754b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3754c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3754d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3754e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3754f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
37550 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
37551 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
37552 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
37553 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
37554 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
37555 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
37556 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
37557 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
37558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3755a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3755b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3755c 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  *****.**.** Impl
3755d 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
3755e 65 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d  e "simple" full-
3755f 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
37560 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nizer..*/../*.**
37561 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
37562 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
37563 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
37564 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
37565 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
37566 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
37567 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
37568 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
37569 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
3756a 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
3756b 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
3756c 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
3756d 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
3756e 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
3756f 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
37570 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
37571 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
37572 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
37573 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
37574 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
37575 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
37576 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70 65 64 65  FTS3).....typede
37577 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f  f struct simple_
37578 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71  tokenizer {.  sq
37579 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3757a 62 61 73 65 3b 0a 20 20 63 68 61 72 20 64 65 6c  base;.  char del
3757b 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20 20  im[128];        
3757c 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53 43       /* flag ASC
3757d 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f  II delimiters */
3757e 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
3757f 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66 20 73 74  zer;..typedef st
37580 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ruct simple_toke
37581 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20  nizer_cursor {. 
37582 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37583 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a  er_cursor base;.
37584 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49    const char *pI
37585 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f  nput;          /
37586 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20 74  * input we are t
37587 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69  okenizing */.  i
37588 6e 74 20 6e 42 79 74 65 73 3b 20 20 20 20 20 20  nt nBytes;      
37589 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3758a 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ize of the input
3758b 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   */.  int iOffse
3758c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3758d 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f     /* current po
3758e 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e 70 75 74  sition in pInput
3758f 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e   */.  int iToken
37590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37591 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e     /* index of n
37592 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20  ext token to be 
37593 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68  returned */.  ch
37594 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *pToken;     
37595 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
37596 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e  orage for curren
37597 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
37598 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64   nTokenAllocated
37599 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61  ;         /* spa
3759a 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ce allocated to 
3759b 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f  zToken buffer */
3759c 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
3759d 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 73 74  zer_cursor;...st
3759e 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44  atic int simpleD
3759f 65 6c 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  elim(simple_toke
375a0 6e 69 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e  nizer *t, unsign
375a1 65 64 20 63 68 61 72 20 63 29 7b 0a 20 20 72 65  ed char c){.  re
375a2 74 75 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74  turn c<0x80 && t
375a3 2d 3e 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f  ->delim[c];.}../
375a4 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
375a5 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74  w tokenizer inst
375a6 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ance..*/.static 
375a7 69 6e 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65  int simpleCreate
375a8 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  (.  int argc, co
375a9 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
375aa 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65   *argv,.  sqlite
375ab 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70  3_tokenizer **pp
375ac 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73  Tokenizer.){.  s
375ad 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
375ae 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70  *t;..  t = (simp
375af 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20  le_tokenizer *) 
375b0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
375b1 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66  izeof(*t));.  if
375b2 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ( t==NULL ) retu
375b3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
375b4 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20  .  memset(t, 0, 
375b5 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20  sizeof(*t));..  
375b6 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44  /* TODO(shess) D
375b7 65 6c 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74  elimiters need t
375b8 6f 20 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d  o remain the sam
375b9 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75  e from run to ru
375ba 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20  n,.  ** else we 
375bb 6e 65 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e  need to reindex.
375bc 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77    One solution w
375bd 6f 75 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74  ould be a meta-t
375be 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61  able to.  ** tra
375bf 63 6b 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74  ck such informat
375c0 69 6f 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62  ion in the datab
375c1 61 73 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f  ase, then we'd o
375c2 6e 6c 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20  nly want this.  
375c3 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ** information o
375c4 6e 20 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72  n the initial cr
375c5 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eate..  */.  if(
375c6 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69   argc>1 ){.    i
375c7 6e 74 20 69 2c 20 6e 20 3d 20 28 69 6e 74 29 73  nt i, n = (int)s
375c8 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a  trlen(argv[1]);.
375c9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
375ca 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e  ; i++){.      un
375cb 73 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d  signed char ch =
375cc 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20   argv[1][i];.   
375cd 20 20 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69     /* We explici
375ce 74 6c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72  tly don't suppor
375cf 74 20 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65  t UTF-8 delimite
375d0 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20  rs for now. */. 
375d1 20 20 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38       if( ch>=0x8
375d2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
375d3 69 74 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20  ite3_free(t);.  
375d4 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
375d5 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
375d6 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69   }.      t->deli
375d7 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d  m[ch] = 1;.    }
375d8 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
375d9 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68  /* Mark non-alph
375da 61 6e 75 6d 65 72 69 63 20 41 53 43 49 49 20 63  anumeric ASCII c
375db 68 61 72 61 63 74 65 72 73 20 61 73 20 64 65 6c  haracters as del
375dc 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69  imiters */.    i
375dd 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
375de 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
375df 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
375e0 69 5d 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29  i] = !isalnum(i)
375e1 20 3f 20 2d 31 20 3a 20 30 3b 0a 20 20 20 20 7d   ? -1 : 0;.    }
375e2 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
375e3 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
375e4 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
375e5 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
375e6 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
375e7 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
375e8 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71  simpleDestroy(sq
375e9 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
375ea 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
375eb 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
375ec 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
375ed 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
375ee 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
375ef 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
375f0 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
375f1 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
375f2 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
375f3 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
375f4 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
375f5 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
375f6 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
375f7 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
375f8 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
375f9 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
375fa 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
375fb 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f  atic int simpleO
375fc 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
375fd 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
375fe 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
375ff 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
37600 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
37601 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
37602 65 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  es,        /* St
37603 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
37604 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
37605 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
37606 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
37607 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
37608 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
37609 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
3760a 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
3760b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
3760c 45 54 45 52 28 70 54 6f 6b 65 6e 69 7a 65 72 29  ETER(pTokenizer)
3760d 3b 0a 0a 20 20 63 20 3d 20 28 73 69 6d 70 6c 65  ;..  c = (simple
3760e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3760f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
37610 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b  loc(sizeof(*c));
37611 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29  .  if( c==NULL )
37612 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
37613 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70  OMEM;..  c->pInp
37614 75 74 20 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69  ut = pInput;.  i
37615 66 28 20 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a  f( pInput==0 ){.
37616 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20      c->nBytes = 
37617 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  0;.  }else if( n
37618 42 79 74 65 73 3c 30 20 29 7b 0a 20 20 20 20 63  Bytes<0 ){.    c
37619 2d 3e 6e 42 79 74 65 73 20 3d 20 28 69 6e 74 29  ->nBytes = (int)
3761a 73 74 72 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a  strlen(pInput);.
3761b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e    }else{.    c->
3761c 6e 42 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b  nBytes = nBytes;
3761d 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65  .  }.  c->iOffse
3761e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
3761f 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20         /* start 
37620 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68  tokenizing at th
37621 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20  e beginning */. 
37622 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a   c->iToken = 0;.
37623 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55    c->pToken = NU
37624 4c 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  LL;             
37625 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c    /* no space al
37626 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f  located, yet. */
37627 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f  .  c->nTokenAllo
37628 63 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70  cated = 0;..  *p
37629 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
3762a 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  se;.  return SQL
3762b 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3762c 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
3762d 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
3762e 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
3762f 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  y a call to.** s
37630 69 6d 70 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76  impleOpen() abov
37631 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
37632 20 73 69 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c   simpleClose(sql
37633 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
37634 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
37635 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .  simple_tokeni
37636 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
37637 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
37638 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
37639 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  sor;.  sqlite3_f
3763a 72 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a  ree(c->pToken);.
3763b 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
3763c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3763d 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
3763e 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74  Extract the next
3763f 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f   token from a to
37640 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
37641 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  r.  The cursor m
37642 75 73 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  ust.** have been
37643 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69   opened by a pri
37644 6f 72 20 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c  or call to simpl
37645 65 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74  eOpen()..*/.stat
37646 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78  ic int simpleNex
37647 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  t(.  sqlite3_tok
37648 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
37649 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73  Cursor,  /* Curs
3764a 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  or returned by s
3764b 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63  impleOpen */.  c
3764c 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f  onst char **ppTo
3764d 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ken,            
3764e 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f     /* OUT: *ppTo
3764f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  ken is the token
37650 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a   text */.  int *
37651 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20  pnBytes,        
37652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37653 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
37654 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20   bytes in token 
37655 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72  */.  int *piStar
37656 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  tOffset,        
37657 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
37658 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74   Starting offset
37659 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   of token */.  i
3765a 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
3765b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3765c 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e     /* OUT: Endin
3765d 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  g offset of toke
3765e 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f  n */.  int *piPo
3765f 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  sition          
37660 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
37661 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65  T: Position inte
37662 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  ger of token */.
37663 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
37664 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20  nizer_cursor *c 
37665 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  = (simple_tokeni
37666 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  zer_cursor *) pC
37667 75 72 73 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f  ursor;.  simple_
37668 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28  tokenizer *t = (
37669 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
3766a 20 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f   *) pCursor->pTo
3766b 6b 65 6e 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67  kenizer;.  unsig
3766c 6e 65 64 20 63 68 61 72 20 2a 70 20 3d 20 28 75  ned char *p = (u
3766d 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 63  nsigned char *)c
3766e 2d 3e 70 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69  ->pInput;..  whi
3766f 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
37670 2d 3e 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20  ->nBytes ){.    
37671 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74  int iStartOffset
37672 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70  ;..    /* Scan p
37673 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68  ast delimiter ch
37674 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20  aracters */.    
37675 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
37676 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 73  t<c->nBytes && s
37677 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b  impleDelim(t, p[
37678 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
37679 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
3767a 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ++;.    }..    /
3767b 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69  * Count non-deli
3767c 6d 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73  miter characters
3767d 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f  . */.    iStartO
3767e 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73  ffset = c->iOffs
3767f 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63  et;.    while( c
37680 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79  ->iOffset<c->nBy
37681 74 65 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65  tes && !simpleDe
37682 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66  lim(t, p[c->iOff
37683 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  set]) ){.      c
37684 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20  ->iOffset++;.   
37685 20 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69   }..    if( c->i
37686 4f 66 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66  Offset>iStartOff
37687 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  set ){.      int
37688 20 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73   i, n = c->iOffs
37689 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b  et-iStartOffset;
3768a 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e  .      if( n>c->
3768b 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20  nTokenAllocated 
3768c 29 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 54  ){.        c->nT
3768d 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20  okenAllocated = 
3768e 6e 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d  n+20;.        c-
3768f 3e 70 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65  >pToken = sqlite
37690 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f  3_realloc(c->pTo
37691 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c  ken, c->nTokenAl
37692 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20  located);.      
37693 20 20 69 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d    if( c->pToken=
37694 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
37695 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
37696 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69     }.      for(i
37697 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20  =0; i<n; i++){. 
37698 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
37699 68 65 73 73 29 20 54 68 69 73 20 6e 65 65 64 73  hess) This needs
3769a 20 65 78 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61   expansion to ha
3769b 6e 64 6c 65 20 55 54 46 2d 38 0a 20 20 20 20 20  ndle UTF-8.     
3769c 20 20 20 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e     ** case-insen
3769d 73 69 74 69 76 69 74 79 2e 0a 20 20 20 20 20 20  sitivity..      
3769e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 73    */.        uns
3769f 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20  igned char ch = 
376a0 70 5b 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69  p[iStartOffset+i
376a1 5d 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54  ];.        c->pT
376a2 6f 6b 65 6e 5b 69 5d 20 3d 20 28 63 68 61 72 29  oken[i] = (char)
376a3 28 63 68 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77  (ch<0x80 ? tolow
376a4 65 72 28 63 68 29 20 3a 20 63 68 29 3b 0a 20 20  er(ch) : ch);.  
376a5 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 70 54      }.      *ppT
376a6 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e  oken = c->pToken
376a7 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73  ;.      *pnBytes
376a8 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53   = n;.      *piS
376a9 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74  tartOffset = iSt
376aa 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  artOffset;.     
376ab 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
376ac 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  c->iOffset;.    
376ad 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
376ae 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20  c->iToken++;..  
376af 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
376b0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
376b1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
376b2 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  DONE;.}../*.** T
376b3 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  he set of routin
376b4 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  es that implemen
376b5 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b  t the simple tok
376b6 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
376b7 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
376b8 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
376b9 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  simpleTokenizerM
376ba 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20  odule = {.  0,. 
376bb 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a 20   simpleCreate,. 
376bc 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0a   simpleDestroy,.
376bd 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20    simpleOpen,.  
376be 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20 73  simpleClose,.  s
376bf 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f  impleNext,.};../
376c0 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
376c1 6e 65 77 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  new simple token
376c2 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20 61 20  izer.  Return a 
376c3 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
376c4 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20  ew.** tokenizer 
376c5 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a  in *ppModule.*/.
376c6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
376c7 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
376c8 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
376c9 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  dule(.  sqlite3_
376ca 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
376cb 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65   const**ppModule
376cc 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20  .){.  *ppModule 
376cd 3d 20 26 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  = &simpleTokeniz
376ce 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e  erModule;.}..#en
376cf 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
376d0 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
376d1 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
376d2 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
376d3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
376d4 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65  End of fts3_toke
376d5 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  nizer1.c *******
376d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
376d8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
376d9 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
376da 77 72 69 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  write.c ********
376db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
376dd 2f 2a 0a 2a 2a 20 32 30 30 39 20 4f 63 74 20 32  /*.** 2009 Oct 2
376de 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
376df 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
376e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
376e1 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
376e2 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
376e3 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
376e4 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
376e5 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
376e6 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
376e7 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
376e8 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
376e9 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
376ea 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
376eb 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
376ec 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
376ed 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
376ee 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
376ef 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
376f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
376f4 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
376f5 20 66 69 6c 65 20 69 73 20 70 61 72 74 20 6f 66   file is part of
376f6 20 74 68 65 20 53 51 4c 69 74 65 20 46 54 53 33   the SQLite FTS3
376f7 20 65 78 74 65 6e 73 69 6f 6e 20 6d 6f 64 75 6c   extension modul
376f8 65 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  e. Specifically,
376f9 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 20 63 6f  .** this file co
376fa 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69  ntains code to i
376fb 6e 73 65 72 74 2c 20 75 70 64 61 74 65 20 61 6e  nsert, update an
376fc 64 20 64 65 6c 65 74 65 20 72 6f 77 73 20 66 72  d delete rows fr
376fd 6f 6d 20 46 54 53 33 0a 2a 2a 20 74 61 62 6c 65  om FTS3.** table
376fe 73 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61  s. It also conta
376ff 69 6e 73 20 63 6f 64 65 20 74 6f 20 6d 65 72 67  ins code to merg
37700 65 20 46 54 53 33 20 62 2d 74 72 65 65 20 73 65  e FTS3 b-tree se
37701 67 6d 65 6e 74 73 2e 20 53 6f 6d 65 0a 2a 2a 20  gments. Some.** 
37702 6f 66 20 74 68 65 20 73 75 62 2d 72 6f 75 74 69  of the sub-routi
37703 6e 65 73 20 75 73 65 64 20 74 6f 20 6d 65 72 67  nes used to merg
37704 65 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 61  e segments are a
37705 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68 65 20  lso used by the 
37706 71 75 65 72 79 20 0a 2a 2a 20 63 6f 64 65 20 69  query .** code i
37707 6e 20 66 74 73 33 2e 63 2e 0a 2a 2f 0a 0a 23 69  n fts3.c..*/..#i
37708 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
37709 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3770a 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3770b 5f 46 54 53 33 29 0a 0a 0a 74 79 70 65 64 65 66  _FTS3)...typedef
3770c 20 73 74 72 75 63 74 20 50 65 6e 64 69 6e 67 4c   struct PendingL
3770d 69 73 74 20 50 65 6e 64 69 6e 67 4c 69 73 74 3b  ist PendingList;
3770e 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3770f 53 65 67 6d 65 6e 74 4e 6f 64 65 20 53 65 67 6d  SegmentNode Segm
37710 65 6e 74 4e 6f 64 65 3b 0a 74 79 70 65 64 65 66  entNode;.typedef
37711 20 73 74 72 75 63 74 20 53 65 67 6d 65 6e 74 57   struct SegmentW
37712 72 69 74 65 72 20 53 65 67 6d 65 6e 74 57 72 69  riter SegmentWri
37713 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 44 61 74 61  ter;../*.** Data
37714 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20   structure used 
37715 77 68 69 6c 65 20 61 63 63 75 6d 75 6c 61 74 69  while accumulati
37716 6e 67 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  ng terms in the 
37717 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61  pending-terms ha
37718 73 68 0a 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65  sh.** table. The
37719 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
3771a 79 20 6d 61 70 73 20 66 72 6f 6d 20 74 65 72 6d  y maps from term
3771b 20 28 61 20 73 74 72 69 6e 67 29 20 74 6f 20 61   (a string) to a
3771c 20 6d 61 6c 6c 6f 63 65 64 0a 2a 2a 20 69 6e 73   malloced.** ins
3771d 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
3771e 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
3771f 63 74 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 7b  ct PendingList {
37720 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20  .  int nData;.  
37721 63 68 61 72 20 2a 61 44 61 74 61 3b 0a 20 20 69  char *aData;.  i
37722 6e 74 20 6e 53 70 61 63 65 3b 0a 20 20 73 71 6c  nt nSpace;.  sql
37723 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 61 73 74  ite3_int64 iLast
37724 44 6f 63 69 64 3b 0a 20 20 73 71 6c 69 74 65 33  Docid;.  sqlite3
37725 5f 69 6e 74 36 34 20 69 4c 61 73 74 43 6f 6c 3b  _int64 iLastCol;
37726 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
37727 20 69 4c 61 73 74 50 6f 73 3b 0a 7d 3b 0a 0a 2f   iLastPos;.};../
37728 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
37729 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3772a 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 74  re is used to it
3772b 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68  erate through th
3772c 65 20 74 65 72 6d 73 20 6f 6e 0a 2a 2a 20 61 20  e terms on.** a 
3772d 63 6f 6e 74 69 67 75 6f 75 73 20 73 65 74 20 6f  contiguous set o
3772e 66 20 73 65 67 6d 65 6e 74 20 62 2d 74 72 65 65  f segment b-tree
3772f 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20 41 6c 74   leaf nodes. Alt
37730 68 6f 75 67 68 20 74 68 65 20 64 65 74 61 69 6c  hough the detail
37731 73 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  s of.** this str
37732 75 63 74 75 72 65 20 61 72 65 20 6f 6e 6c 79 20  ucture are only 
37733 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 63  manipulated by c
37734 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
37735 2c 20 6f 70 61 71 75 65 20 68 61 6e 64 6c 65 73  , opaque handles
37736 0a 2a 2a 20 6f 66 20 74 79 70 65 20 46 74 73 33  .** of type Fts3
37737 53 65 67 52 65 61 64 65 72 2a 20 61 72 65 20 61  SegReader* are a
37738 6c 73 6f 20 75 73 65 64 20 62 79 20 63 6f 64 65  lso used by code
37739 20 69 6e 20 66 74 73 33 2e 63 20 74 6f 20 69 74   in fts3.c to it
3773a 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a  erate through.**
3773b 20 74 65 72 6d 73 20 77 68 65 6e 20 71 75 65 72   terms when quer
3773c 79 69 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65  ying the full-te
3773d 78 74 20 69 6e 64 65 78 2e 20 53 65 65 20 66 75  xt index. See fu
3773e 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  nctions:.**.**  
3773f 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
37740 65 61 64 65 72 4e 65 77 28 29 0a 2a 2a 20 20 20  eaderNew().**   
37741 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
37742 61 64 65 72 46 72 65 65 28 29 0a 2a 2a 20 20 20  aderFree().**   
37743 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
37744 61 64 65 72 49 74 65 72 61 74 65 28 29 0a 2a 2f  aderIterate().*/
37745 0a 73 74 72 75 63 74 20 46 74 73 33 53 65 67 52  .struct Fts3SegR
37746 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69 49  eader {.  int iI
37747 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
37748 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
37749 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c 20  ex within level 
3774a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
3774b 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 3b 0a  64 iStartBlock;.
3774c 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
3774d 69 45 6e 64 42 6c 6f 63 6b 3b 0a 20 20 73 71 6c  iEndBlock;.  sql
3774e 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
3774f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
37750 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 74 6f  SQL Statement to
37751 20 61 63 63 65 73 73 20 6c 65 61 66 20 6e 6f 64   access leaf nod
37752 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 4e  es */.  char *aN
37753 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
37754 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
37755 65 72 20 74 6f 20 6e 6f 64 65 20 64 61 74 61 20  er to node data 
37756 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 69  (or NULL) */.  i
37757 6e 74 20 6e 4e 6f 64 65 3b 20 20 20 20 20 20 20  nt nNode;       
37758 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37759 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
3775a 20 61 74 20 61 4e 6f 64 65 20 28 6f 72 20 30 29   at aNode (or 0)
3775b 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 41   */.  int nTermA
3775c 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
3775d 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
3775e 65 64 20 73 69 7a 65 20 6f 66 20 7a 54 65 72 6d  ed size of zTerm
3775f 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a   buffer */..  /*
37760 20 56 61 72 69 61 62 6c 65 73 20 73 65 74 20 62   Variables set b
37761 79 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e  y fts3SegReaderN
37762 65 78 74 28 29 2e 20 54 68 65 73 65 20 6d 61 79  ext(). These may
37763 20 62 65 20 72 65 61 64 20 64 69 72 65 63 74 6c   be read directl
37764 79 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 61  y.  ** by the ca
37765 6c 6c 65 72 2e 20 54 68 65 79 20 61 72 65 20 76  ller. They are v
37766 61 6c 69 64 20 66 72 6f 6d 20 74 68 65 20 74 69  alid from the ti
37767 6d 65 20 53 65 67 6d 65 6e 74 52 65 61 64 65 72  me SegmentReader
37768 4e 65 77 28 29 20 72 65 74 75 72 6e 73 0a 20 20  New() returns.  
37769 2a 2a 20 75 6e 74 69 6c 20 53 65 67 6d 65 6e 74  ** until Segment
3776a 52 65 61 64 65 72 4e 65 78 74 28 29 20 72 65 74  ReaderNext() ret
3776b 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f  urns something o
3776c 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
3776d 5f 4f 4b 0a 20 20 2a 2a 20 28 69 2e 65 2e 20 53  _OK.  ** (i.e. S
3776e 51 4c 49 54 45 5f 44 4f 4e 45 29 2e 0a 20 20 2a  QLITE_DONE)..  *
3776f 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20  /.  int nTerm;  
37770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37771 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
37772 20 62 79 74 65 73 20 69 6e 20 63 75 72 72 65 6e   bytes in curren
37773 74 20 74 65 72 6d 20 2a 2f 0a 20 20 63 68 61 72  t term */.  char
37774 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *zTerm;        
37775 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
37776 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 65 6e  ointer to curren
37777 74 20 74 65 72 6d 20 2a 2f 0a 20 20 63 68 61 72  t term */.  char
37778 20 2a 61 44 6f 63 6c 69 73 74 3b 20 20 20 20 20   *aDoclist;     
37779 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
3777a 6f 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c 69 73  ointer to doclis
3777b 74 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74  t of current ent
3777c 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63  ry */.  int nDoc
3777d 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  list;           
3777e 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
3777f 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e 20 63 75  of doclist in cu
37780 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 0a  rrent entry */..
37781 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
37782 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ng variables are
37783 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   used to iterate
37784 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 75 72   through the cur
37785 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 2a 2f 0a  rent doclist */.
37786 20 20 63 68 61 72 20 2a 70 4f 66 66 73 65 74 4c    char *pOffsetL
37787 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ist;.  sqlite3_i
37788 6e 74 36 34 20 69 44 6f 63 69 64 3b 0a 7d 3b 0a  nt64 iDocid;.};.
37789 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
3778a 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
3778b 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
3778c 63 72 65 61 74 65 20 61 20 73 65 67 6d 65 6e 74  create a segment
3778d 20 62 2d 74 72 65 65 20 69 6e 20 74 68 65 0a 2a   b-tree in the.*
3778e 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  * database. The 
3778f 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73  internal details
37790 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72   of this type ar
37791 65 20 6f 6e 6c 79 20 61 63 63 65 73 73 65 64 20  e only accessed 
37792 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  by the.** follow
37793 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a  ing functions:.*
37794 2a 0a 2a 2a 20 20 20 66 74 73 33 53 65 67 57 72  *.**   fts3SegWr
37795 69 74 65 72 41 64 64 28 29 0a 2a 2a 20 20 20 66  iterAdd().**   f
37796 74 73 33 53 65 67 57 72 69 74 65 72 46 6c 75 73  ts3SegWriterFlus
37797 68 28 29 0a 2a 2a 20 20 20 66 74 73 33 53 65 67  h().**   fts3Seg
37798 57 72 69 74 65 72 46 72 65 65 28 29 0a 2a 2f 0a  WriterFree().*/.
37799 73 74 72 75 63 74 20 53 65 67 6d 65 6e 74 57 72  struct SegmentWr
3779a 69 74 65 72 20 7b 0a 20 20 53 65 67 6d 65 6e 74  iter {.  Segment
3779b 4e 6f 64 65 20 2a 70 54 72 65 65 3b 20 20 20 20  Node *pTree;    
3779c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
3779d 74 65 72 20 74 6f 20 69 6e 74 65 72 69 6f 72 20  ter to interior 
3779e 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
3779f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
377a0 34 20 69 46 69 72 73 74 3b 20 20 20 20 20 20 20  4 iFirst;       
377a1 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 6c 6f      /* First slo
377a2 74 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20  t in %_segments 
377a3 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 73 71 6c  written */.  sql
377a4 69 74 65 33 5f 69 6e 74 36 34 20 69 46 72 65 65  ite3_int64 iFree
377a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
377a6 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 69  Next free slot i
377a7 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20 2a 2f 0a  n %_segments */.
377a8 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20    char *zTerm;  
377a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377aa 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
377ab 70 72 65 76 69 6f 75 73 20 74 65 72 6d 20 62 75  previous term bu
377ac 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  ffer */.  int nT
377ad 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
377ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
377af 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
377b0 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  zTerm */.  int n
377b1 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  Malloc;         
377b2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
377b3 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63 27 64 20 62  ze of malloc'd b
377b4 75 66 66 65 72 20 61 74 20 7a 4d 61 6c 6c 6f 63  uffer at zMalloc
377b5 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
377b6 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  loc;            
377b7 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27        /* Malloc'
377b8 64 20 73 70 61 63 65 20 28 70 6f 73 73 69 62 6c  d space (possibl
377b9 79 29 20 75 73 65 64 20 66 6f 72 20 7a 54 65 72  y) used for zTer
377ba 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 69 7a 65  m */.  int nSize
377bb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
377bc 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
377bd 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 20  f allocation at 
377be 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e  aData */.  int n
377bf 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
377c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
377c1 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 61  tes of data in a
377c2 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72 20 2a  Data */.  char *
377c3 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  aData;          
377c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
377c5 6e 74 65 72 20 74 6f 20 62 6c 6f 63 6b 20 66 72  nter to block fr
377c6 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d  om malloc() */.}
377c7 3b 0a 0a 2f 2a 0a 2a 2a 20 54 79 70 65 20 53 65  ;../*.** Type Se
377c8 67 6d 65 6e 74 4e 6f 64 65 20 69 73 20 75 73 65  gmentNode is use
377c9 64 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d by the followi
377ca 6e 67 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f  ng three functio
377cb 6e 73 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  ns to create.** 
377cc 74 68 65 20 69 6e 74 65 72 69 6f 72 20 70 61 72  the interior par
377cd 74 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  t of the segment
377ce 20 62 2b 2d 74 72 65 65 20 73 74 72 75 63 74 75   b+-tree structu
377cf 72 65 73 20 28 65 76 65 72 79 74 68 69 6e 67 20  res (everything 
377d0 65 78 63 65 70 74 0a 2a 2a 20 74 68 65 20 6c 65  except.** the le
377d1 61 66 20 6e 6f 64 65 73 29 2e 20 54 68 65 73 65  af nodes). These
377d2 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
377d3 79 70 65 20 61 72 65 20 6f 6e 6c 79 20 65 76 65  ype are only eve
377d4 72 20 75 73 65 64 20 62 79 20 63 6f 64 65 0a 2a  r used by code.*
377d5 2a 20 77 69 74 68 69 6e 20 74 68 65 20 66 74 73  * within the fts
377d6 33 53 65 67 57 72 69 74 65 72 58 58 58 28 29 20  3SegWriterXXX() 
377d7 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69  family of functi
377d8 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62  ons described ab
377d9 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  ove..**.**   fts
377da 33 4e 6f 64 65 41 64 64 54 65 72 6d 28 29 0a 2a  3NodeAddTerm().*
377db 2a 20 20 20 66 74 73 33 4e 6f 64 65 57 72 69 74  *   fts3NodeWrit
377dc 65 28 29 0a 2a 2a 20 20 20 66 74 73 33 4e 6f 64  e().**   fts3Nod
377dd 65 46 72 65 65 28 29 0a 2a 2f 0a 73 74 72 75 63  eFree().*/.struc
377de 74 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 7b 0a  t SegmentNode {.
377df 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
377e0 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20  Parent;         
377e1 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65    /* Parent node
377e2 20 28 6f 72 20 4e 55 4c 4c 20 66 6f 72 20 72 6f   (or NULL for ro
377e3 6f 74 20 6e 6f 64 65 29 20 2a 2f 0a 20 20 53 65  ot node) */.  Se
377e4 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 52 69 67 68  gmentNode *pRigh
377e5 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
377e6 20 50 6f 69 6e 74 65 72 20 74 6f 20 72 69 67 68   Pointer to righ
377e7 74 2d 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 53  t-sibling */.  S
377e8 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 4c 65 66  egmentNode *pLef
377e9 74 6d 6f 73 74 3b 20 20 20 20 20 20 20 20 20 2f  tmost;         /
377ea 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 65 66  * Pointer to lef
377eb 74 2d 6d 6f 73 74 20 6e 6f 64 65 20 6f 66 20 74  t-most node of t
377ec 68 69 73 20 64 65 70 74 68 20 2a 2f 0a 20 20 69  his depth */.  i
377ed 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20  nt nEntry;      
377ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
377ef 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
377f0 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6e 6f 64  s written to nod
377f1 65 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 63 68  e so far */.  ch
377f2 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20  ar *zTerm;      
377f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
377f4 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 72 65 76   Pointer to prev
377f5 69 6f 75 73 20 74 65 72 6d 20 62 75 66 66 65 72  ious term buffer
377f6 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
377f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377f8 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
377f9 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 54 65 72  of bytes in zTer
377fa 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 6c 6c  m */.  int nMall
377fb 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  oc;             
377fc 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
377fd 66 20 6d 61 6c 6c 6f 63 27 64 20 62 75 66 66 65  f malloc'd buffe
377fe 72 20 61 74 20 7a 4d 61 6c 6c 6f 63 20 2a 2f 0a  r at zMalloc */.
377ff 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b    char *zMalloc;
37800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37801 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 73 70    /* Malloc'd sp
37802 61 63 65 20 28 70 6f 73 73 69 62 6c 79 29 20 75  ace (possibly) u
37803 73 65 64 20 66 6f 72 20 7a 54 65 72 6d 20 2a 2f  sed for zTerm */
37804 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
37805 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37806 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 76     /* Bytes of v
37807 61 6c 69 64 20 64 61 74 61 20 73 6f 20 66 61 72  alid data so far
37808 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 61 74   */.  char *aDat
37809 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
3780a 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 64 61        /* Node da
3780b 74 61 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ta */.};../*.** 
3780c 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
3780d 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3780e 6d 65 6e 74 20 74 6f 20 66 74 73 33 53 71 6c 53  ment to fts3SqlS
3780f 74 6d 74 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tmt()..*/.#defin
37810 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 43 4f 4e  e SQL_DELETE_CON
37811 54 45 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  TENT            
37812 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 49   0.#define SQL_I
37813 53 5f 45 4d 50 54 59 20 20 20 20 20 20 20 20 20  S_EMPTY         
37814 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
37815 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41  ine SQL_DELETE_A
37816 4c 4c 5f 43 4f 4e 54 45 4e 54 20 20 20 20 20 20  LL_CONTENT      
37817 20 20 20 32 20 0a 23 64 65 66 69 6e 65 20 53 51     2 .#define SQ
37818 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 45 47  L_DELETE_ALL_SEG
37819 4d 45 4e 54 53 20 20 20 20 20 20 20 20 33 0a 23  MENTS        3.#
3781a 64 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54  define SQL_DELET
3781b 45 5f 41 4c 4c 5f 53 45 47 44 49 52 20 20 20 20  E_ALL_SEGDIR    
3781c 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
3781d 53 51 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45  SQL_SELECT_CONTE
3781e 4e 54 5f 42 59 5f 52 4f 57 49 44 20 20 20 20 35  NT_BY_ROWID    5
3781f 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e 45 58  .#define SQL_NEX
37820 54 5f 53 45 47 4d 45 4e 54 5f 49 4e 44 45 58 20  T_SEGMENT_INDEX 
37821 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
37822 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  e SQL_INSERT_SEG
37823 4d 45 4e 54 53 20 20 20 20 20 20 20 20 20 20 20  MENTS           
37824 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e   7.#define SQL_N
37825 45 58 54 5f 53 45 47 4d 45 4e 54 53 5f 49 44 20  EXT_SEGMENTS_ID 
37826 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66            8.#def
37827 69 6e 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53  ine SQL_INSERT_S
37828 45 47 44 49 52 20 20 20 20 20 20 20 20 20 20 20  EGDIR           
37829 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c     9.#define SQL
3782a 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 20 20  _SELECT_LEVEL   
3782b 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64             10.#d
3782c 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
3782d 5f 41 4c 4c 5f 4c 45 56 45 4c 20 20 20 20 20 20  _ALL_LEVEL      
3782e 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 53      11.#define S
3782f 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f  QL_SELECT_LEVEL_
37830 43 4f 55 4e 54 20 20 20 20 20 20 20 20 31 32 0a  COUNT        12.
37831 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45  #define SQL_SELE
37832 43 54 5f 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f  CT_SEGDIR_COUNT_
37833 4d 41 58 20 20 20 31 33 0a 23 64 65 66 69 6e 65  MAX   13.#define
37834 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 44   SQL_DELETE_SEGD
37835 49 52 5f 42 59 5f 4c 45 56 45 4c 20 20 20 20 31  IR_BY_LEVEL    1
37836 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 44 45  4.#define SQL_DE
37837 4c 45 54 45 5f 53 45 47 4d 45 4e 54 53 5f 52 41  LETE_SEGMENTS_RA
37838 4e 47 45 20 20 20 20 20 31 35 0a 23 64 65 66 69  NGE     15.#defi
37839 6e 65 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49  ne SQL_CONTENT_I
3783a 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
3783b 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   16.#define SQL_
3783c 47 45 54 5f 42 4c 4f 43 4b 20 20 20 20 20 20 20  GET_BLOCK       
3783d 20 20 20 20 20 20 20 20 20 20 31 37 0a 0a 2f 2a            17../*
3783e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
3783f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6f 62 74  n is used to obt
37840 61 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 70 72  ain an SQLite pr
37841 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
37842 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20 74   handle.** for t
37843 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 64 65  he statement ide
37844 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ntified by the s
37845 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20  econd argument. 
37846 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  If successful,.*
37847 2a 20 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20  * *pp is set to 
37848 74 68 65 20 72 65 71 75 65 73 74 65 64 20 73 74  the requested st
37849 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 61  atement handle a
3784a 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
3784b 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77  urned..** Otherw
3784c 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
3784d 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
3784e 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20 69 73  urned and *pp is
3784f 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a   set to 0..**.**
37850 20 49 66 20 61 72 67 75 6d 65 6e 74 20 61 70 56   If argument apV
37851 61 6c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  al is not NULL, 
37852 74 68 65 6e 20 69 74 20 6d 75 73 74 20 70 6f 69  then it must poi
37853 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79 20 77  nt to an array w
37854 69 74 68 0a 2a 2a 20 61 74 20 6c 65 61 73 74 20  ith.** at least 
37855 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20  as many entries 
37856 61 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64  as the requested
37857 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62   statement has b
37858 6f 75 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74  ound .** paramet
37859 65 72 73 2e 20 54 68 65 20 76 61 6c 75 65 73 20  ers. The values 
3785a 61 72 65 20 62 6f 75 6e 64 20 74 6f 20 74 68 65  are bound to the
3785b 20 73 74 61 74 65 6d 65 6e 74 73 20 70 61 72 61   statements para
3785c 6d 65 74 65 72 73 20 62 65 66 6f 72 65 0a 2a 2a  meters before.**
3785d 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
3785e 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 71  tatic int fts3Sq
3785f 6c 53 74 6d 74 28 0a 20 20 46 74 73 33 54 61 62  lStmt(.  Fts3Tab
37860 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
37861 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
37862 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
37863 20 2a 2f 0a 20 20 69 6e 74 20 65 53 74 6d 74 2c   */.  int eStmt,
37864 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37865 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
37866 74 68 65 20 53 51 4c 5f 58 58 58 20 63 6f 6e 73  the SQL_XXX cons
37867 74 61 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  tants above */. 
37868 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
37869 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
3786a 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
3786b 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 73  nt handle */.  s
3786c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
3786d 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f  pVal           /
3786e 2a 20 56 61 6c 75 65 73 20 74 6f 20 62 69 6e 64  * Values to bind
3786f 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f   to statement */
37870 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
37871 20 2a 61 7a 53 71 6c 5b 5d 20 3d 20 7b 0a 2f 2a   *azSql[] = {./*
37872 20 30 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20   0  */  "DELETE 
37873 46 52 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e 74  FROM %Q.'%q_cont
37874 65 6e 74 27 20 57 48 45 52 45 20 72 6f 77 69 64  ent' WHERE rowid
37875 20 3d 20 3f 22 2c 0a 2f 2a 20 31 20 20 2a 2f 20   = ?",./* 1  */ 
37876 20 22 53 45 4c 45 43 54 20 4e 4f 54 20 45 58 49   "SELECT NOT EXI
37877 53 54 53 28 53 45 4c 45 43 54 20 64 6f 63 69 64  STS(SELECT docid
37878 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e   FROM %Q.'%q_con
37879 74 65 6e 74 27 20 57 48 45 52 45 20 72 6f 77 69  tent' WHERE rowi
3787a 64 21 3d 3f 29 22 2c 0a 2f 2a 20 32 20 20 2a 2f  d!=?)",./* 2  */
3787b 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
3787c 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 22 2c  Q.'%q_content'",
3787d 0a 2f 2a 20 33 20 20 2a 2f 20 20 22 44 45 4c 45  ./* 3  */  "DELE
3787e 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73  TE FROM %Q.'%q_s
3787f 65 67 6d 65 6e 74 73 27 22 2c 0a 2f 2a 20 34 20  egments'",./* 4 
37880 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52 4f   */  "DELETE FRO
37881 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  M %Q.'%q_segdir'
37882 22 2c 0a 2f 2a 20 35 20 20 2a 2f 20 20 22 53 45  ",./* 5  */  "SE
37883 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e 27  LECT * FROM %Q.'
37884 25 71 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45 52  %q_content' WHER
37885 45 20 72 6f 77 69 64 3d 3f 22 2c 0a 2f 2a 20 36  E rowid=?",./* 6
37886 20 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 63 6f    */  "SELECT co
37887 61 6c 65 73 63 65 28 6d 61 78 28 69 64 78 29 2b  alesce(max(idx)+
37888 31 2c 20 30 29 20 46 52 4f 4d 20 25 51 2e 27 25  1, 0) FROM %Q.'%
37889 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45 20  q_segdir' WHERE 
3788a 6c 65 76 65 6c 3d 3f 22 2c 0a 2f 2a 20 37 20 20  level=?",./* 7  
3788b 2a 2f 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  */  "INSERT INTO
3788c 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73   %Q.'%q_segments
3788d 27 28 62 6c 6f 63 6b 69 64 2c 20 62 6c 6f 63 6b  '(blockid, block
3788e 29 20 56 41 4c 55 45 53 28 3f 2c 20 3f 29 22 2c  ) VALUES(?, ?)",
3788f 0a 2f 2a 20 38 20 20 2a 2f 20 20 22 53 45 4c 45  ./* 8  */  "SELE
37890 43 54 20 63 6f 61 6c 65 73 63 65 28 6d 61 78 28  CT coalesce(max(
37891 62 6c 6f 63 6b 69 64 29 2b 31 2c 20 31 29 20 46  blockid)+1, 1) F
37892 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65  ROM %Q.'%q_segme
37893 6e 74 73 27 22 2c 0a 2f 2a 20 39 20 20 2a 2f 20  nts'",./* 9  */ 
37894 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
37895 2e 27 25 71 5f 73 65 67 64 69 72 27 20 56 41 4c  .'%q_segdir' VAL
37896 55 45 53 28 3f 2c 3f 2c 3f 2c 3f 2c 3f 2c 3f 29  UES(?,?,?,?,?,?)
37897 22 2c 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ",..          /*
37898 20 52 65 74 75 72 6e 20 73 65 67 6d 65 6e 74 73   Return segments
37899 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6f   in order from o
3789a 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 2e  ldest to newest.
3789b 2a 2f 20 0a 2f 2a 20 31 30 20 2a 2f 20 20 22 53  */ ./* 10 */  "S
3789c 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61 72 74  ELECT idx, start
3789d 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
3789e 6e 64 5f 62 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c  nd_block, end_bl
3789f 6f 63 6b 2c 20 72 6f 6f 74 20 22 0a 20 20 20 20  ock, root ".    
378a0 20 20 20 20 20 20 20 20 22 46 52 4f 4d 20 25 51          "FROM %Q
378a1 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57 48 45  .'%q_segdir' WHE
378a2 52 45 20 6c 65 76 65 6c 20 3d 20 3f 20 4f 52 44  RE level = ? ORD
378a3 45 52 20 42 59 20 69 64 78 20 41 53 43 22 2c 0a  ER BY idx ASC",.
378a4 2f 2a 20 31 31 20 2a 2f 20 20 22 53 45 4c 45 43  /* 11 */  "SELEC
378a5 54 20 69 64 78 2c 20 73 74 61 72 74 5f 62 6c 6f  T idx, start_blo
378a6 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  ck, leaves_end_b
378a7 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c 6f 63 6b 2c  lock, end_block,
378a8 20 72 6f 6f 74 20 22 0a 20 20 20 20 20 20 20 20   root ".        
378a9 20 20 20 20 22 46 52 4f 4d 20 25 51 2e 27 25 71      "FROM %Q.'%q
378aa 5f 73 65 67 64 69 72 27 20 4f 52 44 45 52 20 42  _segdir' ORDER B
378ab 59 20 6c 65 76 65 6c 20 44 45 53 43 2c 20 69 64  Y level DESC, id
378ac 78 20 41 53 43 22 2c 0a 0a 2f 2a 20 31 32 20 2a  x ASC",../* 12 *
378ad 2f 20 20 22 53 45 4c 45 43 54 20 63 6f 75 6e 74  /  "SELECT count
378ae 28 2a 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  (*) FROM %Q.'%q_
378af 73 65 67 64 69 72 27 20 57 48 45 52 45 20 6c 65  segdir' WHERE le
378b0 76 65 6c 20 3d 20 3f 22 2c 0a 2f 2a 20 31 33 20  vel = ?",./* 13 
378b1 2a 2f 20 20 22 53 45 4c 45 43 54 20 63 6f 75 6e  */  "SELECT coun
378b2 74 28 2a 29 2c 20 6d 61 78 28 6c 65 76 65 6c 29  t(*), max(level)
378b3 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67   FROM %Q.'%q_seg
378b4 64 69 72 27 22 2c 0a 0a 2f 2a 20 31 34 20 2a 2f  dir'",../* 14 */
378b5 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
378b6 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57 48  Q.'%q_segdir' WH
378b7 45 52 45 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a  ERE level = ?",.
378b8 2f 2a 20 31 35 20 2a 2f 20 20 22 44 45 4c 45 54  /* 15 */  "DELET
378b9 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65  E FROM %Q.'%q_se
378ba 67 6d 65 6e 74 73 27 20 57 48 45 52 45 20 62 6c  gments' WHERE bl
378bb 6f 63 6b 69 64 20 42 45 54 57 45 45 4e 20 3f 20  ockid BETWEEN ? 
378bc 41 4e 44 20 3f 22 2c 0a 2f 2a 20 31 36 20 2a 2f  AND ?",./* 16 */
378bd 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25    "INSERT INTO %
378be 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 56  Q.'%q_content' V
378bf 41 4c 55 45 53 28 25 7a 29 22 2c 0a 2f 2a 20 31  ALUES(%z)",./* 1
378c0 37 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 62 6c  7 */  "SELECT bl
378c1 6f 63 6b 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ock FROM %Q.'%q_
378c2 73 65 67 6d 65 6e 74 73 27 20 57 48 45 52 45 20  segments' WHERE 
378c3 62 6c 6f 63 6b 69 64 20 3d 20 3f 22 2c 0a 20 20  blockid = ?",.  
378c4 7d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  };.  int rc = SQ
378c5 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
378c6 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
378c7 0a 20 20 61 73 73 65 72 74 28 20 53 69 7a 65 6f  .  assert( Sizeo
378c8 66 41 72 72 61 79 28 61 7a 53 71 6c 29 3d 3d 53  fArray(azSql)==S
378c9 69 7a 65 6f 66 41 72 72 61 79 28 70 2d 3e 61 53  izeofArray(p->aS
378ca 74 6d 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tmt) );.  assert
378cb 28 20 65 53 74 6d 74 3c 53 69 7a 65 6f 66 41 72  ( eStmt<SizeofAr
378cc 72 61 79 28 61 7a 53 71 6c 29 20 26 26 20 65 53  ray(azSql) && eS
378cd 74 6d 74 3e 3d 30 20 29 3b 0a 20 20 0a 20 20 70  tmt>=0 );.  .  p
378ce 53 74 6d 74 20 3d 20 70 2d 3e 61 53 74 6d 74 5b  Stmt = p->aStmt[
378cf 65 53 74 6d 74 5d 3b 0a 20 20 69 66 28 20 21 70  eStmt];.  if( !p
378d0 53 74 6d 74 20 29 7b 0a 20 20 20 20 63 68 61 72  Stmt ){.    char
378d1 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 69 66 28 20   *zSql;.    if( 
378d2 65 53 74 6d 74 3d 3d 53 51 4c 5f 43 4f 4e 54 45  eStmt==SQL_CONTE
378d3 4e 54 5f 49 4e 53 45 52 54 20 29 7b 0a 20 20 20  NT_INSERT ){.   
378d4 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
378d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
378d6 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
378d7 62 6c 65 20 2a 2f 20 20 0a 20 20 20 20 20 20 63  ble */  .      c
378d8 68 61 72 20 2a 7a 56 61 72 6c 69 73 74 3b 20 20  har *zVarlist;  
378d9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
378da 65 20 22 3f 2c 20 3f 2c 20 2e 2e 2e 22 20 73 74  e "?, ?, ..." st
378db 72 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7a 56  ring */.      zV
378dc 61 72 6c 69 73 74 20 3d 20 28 63 68 61 72 20 2a  arlist = (char *
378dd 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
378de 32 2a 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b  2*p->nColumn+2);
378df 0a 20 20 20 20 20 20 69 66 28 20 21 7a 56 61 72  .      if( !zVar
378e0 6c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  list ){.        
378e1 2a 70 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  *pp = 0;.       
378e2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
378e3 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
378e4 20 20 20 20 7a 56 61 72 6c 69 73 74 5b 30 5d 20      zVarlist[0] 
378e5 3d 20 27 3f 27 3b 0a 20 20 20 20 20 20 7a 56 61  = '?';.      zVa
378e6 72 6c 69 73 74 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e  rlist[p->nColumn
378e7 2a 32 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  *2+1] = '\0';.  
378e8 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
378e9 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  p->nColumn; i++)
378ea 7b 0a 20 20 20 20 20 20 20 20 7a 56 61 72 6c 69  {.        zVarli
378eb 73 74 5b 69 2a 32 2d 31 5d 20 3d 20 27 2c 27 3b  st[i*2-1] = ',';
378ec 0a 20 20 20 20 20 20 20 20 7a 56 61 72 6c 69 73  .        zVarlis
378ed 74 5b 69 2a 32 5d 20 3d 20 27 3f 27 3b 0a 20 20  t[i*2] = '?';.  
378ee 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 53 71 6c      }.      zSql
378ef 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
378f0 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74 5d 2c  tf(azSql[eStmt],
378f1 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
378f2 65 2c 20 7a 56 61 72 6c 69 73 74 29 3b 0a 20 20  e, zVarlist);.  
378f3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
378f4 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
378f5 72 69 6e 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d  rintf(azSql[eStm
378f6 74 5d 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a  t], p->zDb, p->z
378f7 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
378f8 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20   if( !zSql ){.  
378f9 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
378fa 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65  NOMEM;.    }else
378fb 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
378fc 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
378fd 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  p->db, zSql, -1,
378fe 20 26 70 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a   &pStmt, NULL);.
378ff 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
37900 65 65 28 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20  ee(zSql);.      
37901 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
37902 54 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d  TE_OK || pStmt==
37903 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53  0 );.      p->aS
37904 74 6d 74 5b 65 53 74 6d 74 5d 20 3d 20 70 53 74  tmt[eStmt] = pSt
37905 6d 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  mt;.    }.  }.  
37906 69 66 28 20 61 70 56 61 6c 20 29 7b 0a 20 20 20  if( apVal ){.   
37907 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20   int i;.    int 
37908 6e 50 61 72 61 6d 20 3d 20 73 71 6c 69 74 65 33  nParam = sqlite3
37909 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
3790a 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20  count(pStmt);.  
3790b 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
3790c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 50  QLITE_OK && i<nP
3790d 61 72 61 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aram; i++){.    
3790e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
3790f 69 6e 64 5f 76 61 6c 75 65 28 70 53 74 6d 74 2c  ind_value(pStmt,
37910 20 69 2b 31 2c 20 61 70 56 61 6c 5b 69 5d 29 3b   i+1, apVal[i]);
37911 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70  .    }.  }.  *pp
37912 20 3d 20 70 53 74 6d 74 3b 0a 20 20 72 65 74 75   = pStmt;.  retu
37913 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
37914 53 69 6d 69 6c 61 72 20 74 6f 20 66 74 73 33 53  Similar to fts3S
37915 71 6c 53 74 6d 74 28 29 2e 20 45 78 63 65 70 74  qlStmt(). Except
37916 2c 20 61 66 74 65 72 20 62 69 6e 64 69 6e 67 20  , after binding 
37917 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69  the parameters i
37918 6e 0a 2a 2a 20 61 72 72 61 79 20 61 70 56 61 6c  n.** array apVal
37919 5b 5d 20 74 6f 20 74 68 65 20 53 51 4c 20 73 74  [] to the SQL st
3791a 61 74 65 6d 65 6e 74 20 69 64 65 6e 74 69 66 69  atement identifi
3791b 65 64 20 62 79 20 65 53 74 6d 74 2c 20 74 68 65  ed by eStmt, the
3791c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 69 73   statement.** is
3791d 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a   executed..**.**
3791e 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   Returns SQLITE_
3791f 4f 4b 20 69 66 20 74 68 65 20 73 74 61 74 65 6d  OK if the statem
37920 65 6e 74 20 69 73 20 73 75 63 63 65 73 73 66 75  ent is successfu
37921 6c 6c 79 20 65 78 65 63 75 74 65 64 2c 20 6f 72  lly executed, or
37922 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 65 72   an.** SQLite er
37923 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69  ror code otherwi
37924 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
37925 74 20 66 74 73 33 53 71 6c 45 78 65 63 28 46 74  t fts3SqlExec(Ft
37926 73 33 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20  s3Table *p, int 
37927 65 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 76  eStmt, sqlite3_v
37928 61 6c 75 65 20 2a 2a 61 70 56 61 6c 29 7b 0a 20  alue **apVal){. 
37929 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3792a 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Stmt;.  int rc =
3792b 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
3792c 65 53 74 6d 74 2c 20 26 70 53 74 6d 74 2c 20 61  eStmt, &pStmt, a
3792d 70 56 61 6c 29 3b 20 0a 20 20 69 66 28 20 72 63  pVal); .  if( rc
3792e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3792f 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
37930 70 53 74 6d 74 29 3b 0a 20 20 20 20 72 63 20 3d  pStmt);.    rc =
37931 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37932 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Stmt);.  }.  ret
37933 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
37934 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20  * Read a single 
37935 62 6c 6f 63 6b 20 66 72 6f 6d 20 74 68 65 20 25  block from the %
37936 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 2e  _segments table.
37937 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65   If the specifie
37938 64 20 62 6c 6f 63 6b 0a 2a 2a 20 64 6f 65 73 20  d block.** does 
37939 6e 6f 74 20 65 78 69 73 74 2c 20 72 65 74 75 72  not exist, retur
3793a 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
3793b 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  . If some other 
3793c 65 72 72 6f 72 20 28 6d 61 6c 6c 6f 63 2c 20 49  error (malloc, I
3793d 4f 20 0a 2a 2a 20 65 74 63 2e 29 20 6f 63 63 75  O .** etc.) occu
3793e 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61  rs, return the a
3793f 70 70 72 6f 70 72 69 61 74 65 20 53 51 4c 69 74  ppropriate SQLit
37940 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  e error code..**
37941 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
37942 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
37943 74 20 2a 70 7a 42 6c 6f 63 6b 20 74 6f 20 70 6f  t *pzBlock to po
37944 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
37945 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 74 68  containing.** th
37946 65 20 62 6c 6f 63 6b 20 72 65 61 64 20 66 72 6f  e block read fro
37947 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  m the database, 
37948 61 6e 64 20 2a 70 6e 42 6c 6f 63 6b 20 74 6f 20  and *pnBlock to 
37949 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
3794a 72 65 61 64 0a 2a 2a 20 62 6c 6f 63 6b 20 69 6e  read.** block in
3794b 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 57 41   bytes..**.** WA
3794c 52 4e 49 4e 47 3a 20 54 68 65 20 72 65 74 75 72  RNING: The retur
3794d 6e 65 64 20 62 75 66 66 65 72 20 69 73 20 6f 6e  ned buffer is on
3794e 6c 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74  ly valid until t
3794f 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
37950 0a 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 52  .** sqlite3Fts3R
37951 65 61 64 42 6c 6f 63 6b 28 29 2e 0a 2a 2f 0a 53  eadBlock()..*/.S
37952 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
37953 74 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  t sqlite3Fts3Rea
37954 64 42 6c 6f 63 6b 28 0a 20 20 46 74 73 33 54 61  dBlock(.  Fts3Ta
37955 62 6c 65 20 2a 70 2c 0a 20 20 73 71 6c 69 74 65  ble *p,.  sqlite
37956 33 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 2c 0a  3_int64 iBlock,.
37957 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
37958 7a 42 6c 6f 63 6b 2c 0a 20 20 69 6e 74 20 2a 70  zBlock,.  int *p
37959 6e 42 6c 6f 63 6b 0a 29 7b 0a 20 20 73 71 6c 69  nBlock.){.  sqli
3795a 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
3795b 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33  .  int rc = fts3
3795c 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 47  SqlStmt(p, SQL_G
3795d 45 54 5f 42 4c 4f 43 4b 2c 20 26 70 53 74 6d 74  ET_BLOCK, &pStmt
3795e 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
3795f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
37960 72 6e 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  rn rc;.  sqlite3
37961 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 0a  _reset(pStmt);..
37962 20 20 69 66 28 20 70 7a 42 6c 6f 63 6b 20 29 7b    if( pzBlock ){
37963 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  .    sqlite3_bin
37964 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31  d_int64(pStmt, 1
37965 2c 20 69 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72  , iBlock);.    r
37966 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
37967 28 70 53 74 6d 74 29 3b 20 0a 20 20 20 20 69 66  (pStmt); .    if
37968 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
37969 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
3796a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
3796b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2a 70  .    }.  .    *p
3796c 6e 42 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  nBlock = sqlite3
3796d 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53  _column_bytes(pS
3796e 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 2a 70 7a  tmt, 0);.    *pz
3796f 42 6c 6f 63 6b 20 3d 20 28 63 68 61 72 20 2a 29  Block = (char *)
37970 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37971 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  lob(pStmt, 0);. 
37972 20 20 20 69 66 28 20 21 2a 70 7a 42 6c 6f 63 6b     if( !*pzBlock
37973 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
37974 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
37975 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
37976 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37977 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70 53 74 6d  /*.** Set *ppStm
37978 74 20 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  t to a statement
37979 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 79   handle that may
3797a 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72   be used to iter
3797b 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 61  ate through.** a
3797c 6c 6c 20 72 6f 77 73 20 69 6e 20 74 68 65 20 25  ll rows in the %
3797d 5f 73 65 67 64 69 72 20 74 61 62 6c 65 2c 20 66  _segdir table, f
3797e 72 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65  rom oldest to ne
3797f 77 65 73 74 2e 20 49 66 20 73 75 63 63 65 73 73  west. If success
37980 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53  ful,.** return S
37981 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20  QLITE_OK. If an 
37982 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
37983 6c 65 20 70 72 65 70 61 72 69 6e 67 20 74 68 65  le preparing the
37984 20 73 74 61 74 65 6d 65 6e 74 2c 20 0a 2a 2a 20   statement, .** 
37985 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
37986 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
37987 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79  ** There is only
37988 20 65 76 65 72 20 6f 6e 65 20 69 6e 73 74 61 6e   ever one instan
37989 63 65 20 6f 66 20 74 68 69 73 20 53 51 4c 20 73  ce of this SQL s
3798a 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65  tatement compile
3798b 64 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 46 54  d for.** each FT
3798c 53 33 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  S3 table..**.** 
3798d 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65  The statement re
3798e 74 75 72 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77  turns the follow
3798f 69 6e 67 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  ing columns from
37990 20 74 68 65 20 25 5f 73 65 67 64 69 72 20 74 61   the %_segdir ta
37991 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 30 3a 20  ble:.**.**   0: 
37992 69 64 78 0a 2a 2a 20 20 20 31 3a 20 73 74 61 72  idx.**   1: star
37993 74 5f 62 6c 6f 63 6b 0a 2a 2a 20 20 20 32 3a 20  t_block.**   2: 
37994 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
37995 0a 2a 2a 20 20 20 33 3a 20 65 6e 64 5f 62 6c 6f  .**   3: end_blo
37996 63 6b 0a 2a 2a 20 20 20 34 3a 20 72 6f 6f 74 0a  ck.**   4: root.
37997 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
37998 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
37999 33 41 6c 6c 53 65 67 64 69 72 73 28 46 74 73 33  3AllSegdirs(Fts3
3799a 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74 65  Table *p, sqlite
3799b 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29  3_stmt **ppStmt)
3799c 7b 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 53  {.  return fts3S
3799d 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45  qlStmt(p, SQL_SE
3799e 4c 45 43 54 5f 41 4c 4c 5f 4c 45 56 45 4c 2c 20  LECT_ALL_LEVEL, 
3799f 70 70 53 74 6d 74 2c 20 30 29 3b 0a 7d 0a 0a 0a  ppStmt, 0);.}...
379a0 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 73  /*.** Append a s
379a1 69 6e 67 6c 65 20 76 61 72 69 6e 74 20 74 6f 20  ingle varint to 
379a2 61 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 62 75  a PendingList bu
379a3 66 66 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20  ffer. SQLITE_OK 
379a4 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69  is returned.** i
379a5 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
379a6 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
379a7 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e   code otherwise.
379a8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
379a9 74 69 6f 6e 20 61 6c 73 6f 20 73 65 72 76 65 73  tion also serves
379aa 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65   to allocate the
379ab 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72   PendingList str
379ac 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a  ucture itself..*
379ad 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  * For example, t
379ae 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 50  o create a new P
379af 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75 63  endingList struc
379b0 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
379b1 74 77 6f 0a 2a 2a 20 76 61 72 69 6e 74 73 3a 0a  two.** varints:.
379b2 2a 2a 0a 2a 2a 20 20 20 50 65 6e 64 69 6e 67 4c  **.**   PendingL
379b3 69 73 74 20 2a 70 20 3d 20 30 3b 0a 2a 2a 20 20  ist *p = 0;.**  
379b4 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74   fts3PendingList
379b5 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c  AppendVarint(&p,
379b6 20 31 29 3b 0a 2a 2a 20 20 20 66 74 73 33 50 65   1);.**   fts3Pe
379b7 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56  ndingListAppendV
379b8 61 72 69 6e 74 28 26 70 2c 20 32 29 3b 0a 2a 2f  arint(&p, 2);.*/
379b9 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
379ba 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
379bb 64 56 61 72 69 6e 74 28 0a 20 20 50 65 6e 64 69  dVarint(.  Pendi
379bc 6e 67 4c 69 73 74 20 2a 2a 70 70 2c 20 20 20 20  ngList **pp,    
379bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
379be 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f  /OUT: Pointer to
379bf 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72   PendingList str
379c0 75 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  uct */.  sqlite3
379c1 5f 69 6e 74 36 34 20 69 20 20 20 20 20 20 20 20  _int64 i        
379c2 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
379c3 65 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64  e to append to d
379c4 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 50 65 6e 64  ata */.){.  Pend
379c5 69 6e 67 4c 69 73 74 20 2a 70 20 3d 20 2a 70 70  ingList *p = *pp
379c6 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
379c7 20 6f 72 20 67 72 6f 77 20 74 68 65 20 50 65 6e   or grow the Pen
379c8 64 69 6e 67 4c 69 73 74 20 61 73 20 72 65 71 75  dingList as requ
379c9 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 21  ired. */.  if( !
379ca 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  p ){.    p = sql
379cb 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
379cc 6f 66 28 2a 70 29 20 2b 20 31 30 30 29 3b 0a 20  of(*p) + 100);. 
379cd 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20     if( !p ){.   
379ce 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
379cf 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
379d0 20 20 70 2d 3e 6e 53 70 61 63 65 20 3d 20 31 30    p->nSpace = 10
379d1 30 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20  0;.    p->aData 
379d2 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31 5d 3b  = (char *)&p[1];
379d3 0a 20 20 20 20 70 2d 3e 6e 44 61 74 61 20 3d 20  .    p->nData = 
379d4 30 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66  0;.  }.  else if
379d5 28 20 70 2d 3e 6e 44 61 74 61 2b 46 54 53 33 5f  ( p->nData+FTS3_
379d6 56 41 52 49 4e 54 5f 4d 41 58 2b 31 3e 70 2d 3e  VARINT_MAX+1>p->
379d7 6e 53 70 61 63 65 20 29 7b 0a 20 20 20 20 69 6e  nSpace ){.    in
379d8 74 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 53 70 61  t nNew = p->nSpa
379d9 63 65 20 2a 20 32 3b 0a 20 20 20 20 70 20 3d 20  ce * 2;.    p = 
379da 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
379db 70 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20  p, sizeof(*p) + 
379dc 6e 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 21  nNew);.    if( !
379dd 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
379de 65 33 5f 66 72 65 65 28 2a 70 70 29 3b 0a 20 20  e3_free(*pp);.  
379df 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 20      *pp = 0;.   
379e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
379e1 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
379e2 20 20 70 2d 3e 6e 53 70 61 63 65 20 3d 20 6e 4e    p->nSpace = nN
379e3 65 77 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61  ew;.    p->aData
379e4 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31 5d   = (char *)&p[1]
379e5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70 65  ;.  }..  /* Appe
379e6 6e 64 20 74 68 65 20 6e 65 77 20 73 65 72 69 61  nd the new seria
379e7 6c 69 7a 65 64 20 76 61 72 69 6e 74 20 74 6f 20  lized varint to 
379e8 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
379e9 69 73 74 2e 20 2a 2f 0a 20 20 70 2d 3e 6e 44 61  ist. */.  p->nDa
379ea 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ta += sqlite3Fts
379eb 33 50 75 74 56 61 72 69 6e 74 28 26 70 2d 3e 61  3PutVarint(&p->a
379ec 44 61 74 61 5b 70 2d 3e 6e 44 61 74 61 5d 2c 20  Data[p->nData], 
379ed 69 29 3b 0a 20 20 70 2d 3e 61 44 61 74 61 5b 70  i);.  p->aData[p
379ee 2d 3e 6e 44 61 74 61 5d 20 3d 20 27 5c 30 27 3b  ->nData] = '\0';
379ef 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65  .  *pp = p;.  re
379f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
379f1 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 64  }../*.** Add a d
379f2 6f 63 69 64 2f 63 6f 6c 75 6d 6e 2f 70 6f 73 69  ocid/column/posi
379f3 74 69 6f 6e 20 65 6e 74 72 79 20 74 6f 20 61 20  tion entry to a 
379f4 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75  PendingList stru
379f5 63 74 75 72 65 2e 20 4e 6f 6e 2d 7a 65 72 6f 0a  cture. Non-zero.
379f6 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
379f7 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  f the structure 
379f8 69 73 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  is sqlite3_reall
379f9 6f 63 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  oced as part of 
379fa 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e  adding.** the en
379fb 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  try. Otherwise, 
379fc 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  zero..**.** If a
379fd 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
379fe 72 73 2c 20 2a 70 52 63 20 69 73 20 73 65 74 20  rs, *pRc is set 
379ff 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  to SQLITE_NOMEM 
37a00 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
37a01 2e 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 61 6c 77  ..** Zero is alw
37a02 61 79 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20  ays returned in 
37a03 74 68 69 73 20 63 61 73 65 2e 20 4f 74 68 65 72  this case. Other
37a04 77 69 73 65 2c 20 69 66 20 6e 6f 20 4f 4f 4d 20  wise, if no OOM 
37a05 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
37a06 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 53 51   it is set to SQ
37a07 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74  LITE_OK..*/.stat
37a08 69 63 20 69 6e 74 20 66 74 73 33 50 65 6e 64 69  ic int fts3Pendi
37a09 6e 67 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20  ngListAppend(.  
37a0a 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 2a 70 70  PendingList **pp
37a0b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37a0c 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 65 6e 64 69  /* IN/OUT: Pendi
37a0d 6e 67 4c 69 73 74 20 73 74 72 75 63 74 75 72 65  ngList structure
37a0e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37a0f 74 36 34 20 69 44 6f 63 69 64 2c 20 20 20 20 20  t64 iDocid,     
37a10 20 20 20 20 20 20 2f 2a 20 44 6f 63 69 64 20 66        /* Docid f
37a11 6f 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64 20  or entry to add 
37a12 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37a13 36 34 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20  64 iCol,        
37a14 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 66       /* Column f
37a15 6f 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64 20  or entry to add 
37a16 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37a17 36 34 20 69 50 6f 73 2c 20 20 20 20 20 20 20 20  64 iPos,        
37a18 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
37a19 20 6f 66 20 74 65 72 6d 20 66 6f 72 20 65 6e 74   of term for ent
37a1a 72 79 20 74 6f 20 61 64 64 20 2a 2f 0a 20 20 69  ry to add */.  i
37a1b 6e 74 20 2a 70 52 63 20 20 20 20 20 20 20 20 20  nt *pRc         
37a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37a1d 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 63 6f  * OUT: Return co
37a1e 64 65 20 2a 2f 0a 29 7b 0a 20 20 50 65 6e 64 69  de */.){.  Pendi
37a1f 6e 67 4c 69 73 74 20 2a 70 20 3d 20 2a 70 70 3b  ngList *p = *pp;
37a20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
37a21 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
37a22 28 20 21 70 20 7c 7c 20 70 2d 3e 69 4c 61 73 74  ( !p || p->iLast
37a23 44 6f 63 69 64 3c 3d 69 44 6f 63 69 64 20 29 3b  Docid<=iDocid );
37a24 0a 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 70 2d  ..  if( !p || p-
37a25 3e 69 4c 61 73 74 44 6f 63 69 64 21 3d 69 44 6f  >iLastDocid!=iDo
37a26 63 69 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  cid ){.    sqlit
37a27 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74 61 20  e3_int64 iDelta 
37a28 3d 20 69 44 6f 63 69 64 20 2d 20 28 70 20 3f 20  = iDocid - (p ? 
37a29 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 20 3a 20  p->iLastDocid : 
37a2a 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  0);.    if( p ){
37a2b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
37a2c 2d 3e 6e 44 61 74 61 3c 70 2d 3e 6e 53 70 61 63  ->nData<p->nSpac
37a2d 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
37a2e 74 28 20 70 2d 3e 61 44 61 74 61 5b 70 2d 3e 6e  t( p->aData[p->n
37a2f 44 61 74 61 5d 3d 3d 30 20 29 3b 0a 20 20 20 20  Data]==0 );.    
37a30 20 20 70 2d 3e 6e 44 61 74 61 2b 2b 3b 0a 20 20    p->nData++;.  
37a31 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49    }.    if( SQLI
37a32 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 74 73  TE_OK!=(rc = fts
37a33 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65  3PendingListAppe
37a34 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 69 44 65  ndVarint(&p, iDe
37a35 6c 74 61 29 29 20 29 7b 0a 20 20 20 20 20 20 67  lta)) ){.      g
37a36 6f 74 6f 20 70 65 6e 64 69 6e 67 6c 69 73 74 61  oto pendinglista
37a37 70 70 65 6e 64 5f 6f 75 74 3b 0a 20 20 20 20 7d  ppend_out;.    }
37a38 0a 20 20 20 20 70 2d 3e 69 4c 61 73 74 43 6f 6c  .    p->iLastCol
37a39 20 3d 20 2d 31 3b 0a 20 20 20 20 70 2d 3e 69 4c   = -1;.    p->iL
37a3a 61 73 74 50 6f 73 20 3d 20 30 3b 0a 20 20 20 20  astPos = 0;.    
37a3b 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 20 3d 20  p->iLastDocid = 
37a3c 69 44 6f 63 69 64 3b 0a 20 20 7d 0a 20 20 69 66  iDocid;.  }.  if
37a3d 28 20 69 43 6f 6c 3e 30 20 26 26 20 70 2d 3e 69  ( iCol>0 && p->i
37a3e 4c 61 73 74 43 6f 6c 21 3d 69 43 6f 6c 20 29 7b  LastCol!=iCol ){
37a3f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
37a40 4f 4b 21 3d 28 72 63 20 3d 20 66 74 73 33 50 65  OK!=(rc = fts3Pe
37a41 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56  ndingListAppendV
37a42 61 72 69 6e 74 28 26 70 2c 20 31 29 29 0a 20 20  arint(&p, 1)).  
37a43 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
37a44 3d 28 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69  =(rc = fts3Pendi
37a45 6e 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69  ngListAppendVari
37a46 6e 74 28 26 70 2c 20 69 43 6f 6c 29 29 0a 20 20  nt(&p, iCol)).  
37a47 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20    ){.      goto 
37a48 70 65 6e 64 69 6e 67 6c 69 73 74 61 70 70 65 6e  pendinglistappen
37a49 64 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  d_out;.    }.   
37a4a 20 70 2d 3e 69 4c 61 73 74 43 6f 6c 20 3d 20 69   p->iLastCol = i
37a4b 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 4c 61 73  Col;.    p->iLas
37a4c 74 50 6f 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  tPos = 0;.  }.  
37a4d 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20  if( iCol>=0 ){. 
37a4e 20 20 20 61 73 73 65 72 74 28 20 69 50 6f 73 3e     assert( iPos>
37a4f 70 2d 3e 69 4c 61 73 74 50 6f 73 20 7c 7c 20 28  p->iLastPos || (
37a50 69 50 6f 73 3d 3d 30 20 26 26 20 70 2d 3e 69 4c  iPos==0 && p->iL
37a51 61 73 74 50 6f 73 3d 3d 30 29 20 29 3b 0a 20 20  astPos==0) );.  
37a52 20 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69    rc = fts3Pendi
37a53 6e 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69  ngListAppendVari
37a54 6e 74 28 26 70 2c 20 32 2b 69 50 6f 73 2d 70 2d  nt(&p, 2+iPos-p-
37a55 3e 69 4c 61 73 74 50 6f 73 29 3b 0a 20 20 20 20  >iLastPos);.    
37a56 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37a57 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  K ){.      p->iL
37a58 61 73 74 50 6f 73 20 3d 20 69 50 6f 73 3b 0a 20  astPos = iPos;. 
37a59 20 20 20 7d 0a 20 20 7d 0a 0a 20 70 65 6e 64 69     }.  }.. pendi
37a5a 6e 67 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74  nglistappend_out
37a5b 3a 0a 20 20 2a 70 52 63 20 3d 20 72 63 3b 0a 20  :.  *pRc = rc;. 
37a5c 20 69 66 28 20 70 21 3d 2a 70 70 20 29 7b 0a 20   if( p!=*pp ){. 
37a5d 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 20 20     *pp = p;.    
37a5e 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
37a5f 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
37a60 2a 2a 20 54 6f 6b 65 6e 69 7a 65 20 74 68 65 20  ** Tokenize the 
37a61 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
37a62 74 72 69 6e 67 20 7a 54 65 78 74 20 61 6e 64 20  tring zText and 
37a63 61 64 64 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 74  add all tokens t
37a64 6f 20 74 68 65 0a 2a 2a 20 70 65 6e 64 69 6e 67  o the.** pending
37a65 2d 74 65 72 6d 73 20 68 61 73 68 2d 74 61 62 6c  -terms hash-tabl
37a66 65 2e 20 54 68 65 20 64 6f 63 69 64 20 75 73 65  e. The docid use
37a67 64 20 69 73 20 74 68 61 74 20 63 75 72 72 65 6e  d is that curren
37a68 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a  tly stored in.**
37a69 20 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20   p->iPrevDocid, 
37a6a 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  and the column i
37a6b 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61  s specified by a
37a6c 72 67 75 6d 65 6e 74 20 69 43 6f 6c 2e 0a 2a 2a  rgument iCol..**
37a6d 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
37a6e 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
37a6f 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
37a70 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
37a71 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
37a72 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 65 6e  atic int fts3Pen
37a73 64 69 6e 67 54 65 72 6d 73 41 64 64 28 46 74 73  dingTermsAdd(Fts
37a74 33 54 61 62 6c 65 20 2a 70 2c 20 63 6f 6e 73 74  3Table *p, const
37a75 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
37a76 74 20 69 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 72  t iCol){.  int r
37a77 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b  c;.  int iStart;
37a78 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20 69  .  int iEnd;.  i
37a79 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 63 68 61 72  nt iPos;..  char
37a7a 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 65 6e 3b 0a   const *zToken;.
37a7b 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 0a 20    int nToken;.. 
37a7c 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37a7d 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
37a7e 20 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a   p->pTokenizer;.
37a7f 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
37a80 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
37a81 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   *pModule = pTok
37a82 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
37a83 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
37a84 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73  izer_cursor *pCs
37a85 72 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  r;.  int (*xNext
37a86 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  )(sqlite3_tokeni
37a87 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
37a88 73 6f 72 2c 0a 20 20 20 20 20 20 63 6f 6e 73 74  sor,.      const
37a89 20 63 68 61 72 2a 2a 2c 69 6e 74 2a 2c 69 6e 74   char**,int*,int
37a8a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 0a 20  *,int*,int*);.. 
37a8b 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 69   assert( pTokeni
37a8c 7a 65 72 20 26 26 20 70 4d 6f 64 75 6c 65 20 29  zer && pModule )
37a8d 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
37a8e 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
37a8f 7a 65 72 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20  zer, zText, -1, 
37a90 26 70 43 73 72 29 3b 0a 20 20 69 66 28 20 72 63  &pCsr);.  if( rc
37a91 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
37a92 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
37a93 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65 6e  }.  pCsr->pToken
37a94 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  izer = pTokenize
37a95 72 3b 0a 0a 20 20 78 4e 65 78 74 20 3d 20 70 4d  r;..  xNext = pM
37a96 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 3b 0a 20 20  odule->xNext;.  
37a97 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b  while( SQLITE_OK
37a98 3d 3d 72 63 0a 20 20 20 20 20 20 26 26 20 53 51  ==rc.      && SQ
37a99 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 78  LITE_OK==(rc = x
37a9a 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b  Next(pCsr, &zTok
37a9b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53  en, &nToken, &iS
37a9c 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50  tart, &iEnd, &iP
37a9d 6f 73 29 29 0a 20 20 29 7b 0a 20 20 20 20 50 65  os)).  ){.    Pe
37a9e 6e 64 69 6e 67 4c 69 73 74 20 2a 70 4c 69 73 74  ndingList *pList
37a9f 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69  ;..    /* Positi
37aa0 6f 6e 73 20 63 61 6e 6e 6f 74 20 62 65 20 6e 65  ons cannot be ne
37aa1 67 61 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d  gative; we use -
37aa2 31 20 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f  1 as a terminato
37aa3 72 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  r internally..  
37aa4 20 20 2a 2a 20 54 6f 6b 65 6e 73 20 6d 75 73 74    ** Tokens must
37aa5 20 68 61 76 65 20 61 20 6e 6f 6e 2d 7a 65 72 6f   have a non-zero
37aa6 20 6c 65 6e 67 74 68 2e 0a 20 20 20 20 2a 2f 0a   length..    */.
37aa7 20 20 20 20 69 66 28 20 69 50 6f 73 3c 30 20 7c      if( iPos<0 |
37aa8 7c 20 21 7a 54 6f 6b 65 6e 20 7c 7c 20 6e 54 6f  | !zToken || nTo
37aa9 6b 65 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20  ken<=0 ){.      
37aaa 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
37aab 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  R;.      break;.
37aac 20 20 20 20 7d 0a 0a 20 20 20 20 70 4c 69 73 74      }..    pList
37aad 20 3d 20 28 50 65 6e 64 69 6e 67 4c 69 73 74 20   = (PendingList 
37aae 2a 29 66 74 73 33 48 61 73 68 46 69 6e 64 28 26  *)fts3HashFind(&
37aaf 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  p->pendingTerms,
37ab0 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29   zToken, nToken)
37ab1 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 20  ;.    if( pList 
37ab2 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 65 6e  ){.      p->nPen
37ab3 64 69 6e 67 44 61 74 61 20 2d 3d 20 28 70 4c 69  dingData -= (pLi
37ab4 73 74 2d 3e 6e 44 61 74 61 20 2b 20 6e 54 6f 6b  st->nData + nTok
37ab5 65 6e 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33  en + sizeof(Fts3
37ab6 48 61 73 68 45 6c 65 6d 29 29 3b 0a 20 20 20 20  HashElem));.    
37ab7 7d 0a 20 20 20 20 69 66 28 20 66 74 73 33 50 65  }.    if( fts3Pe
37ab8 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 28  ndingListAppend(
37ab9 26 70 4c 69 73 74 2c 20 70 2d 3e 69 50 72 65 76  &pList, p->iPrev
37aba 44 6f 63 69 64 2c 20 69 43 6f 6c 2c 20 69 50 6f  Docid, iCol, iPo
37abb 73 2c 20 26 72 63 29 20 29 7b 0a 20 20 20 20 20  s, &rc) ){.     
37abc 20 69 66 28 20 70 4c 69 73 74 3d 3d 66 74 73 33   if( pList==fts3
37abd 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70  HashInsert(&p->p
37abe 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 7a 54 6f  endingTerms, zTo
37abf 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 70 4c 69  ken, nToken, pLi
37ac0 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  st) ){.        /
37ac1 2a 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20  * Malloc failed 
37ac2 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
37ac3 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2e 20 54  the new entry. T
37ac4 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 0a 20 20  his can only .  
37ac5 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 20        ** happen 
37ac6 69 66 20 74 68 65 72 65 20 77 61 73 20 6e 6f 20  if there was no 
37ac7 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 66  previous entry f
37ac8 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e 2e 0a 20  or this token.. 
37ac9 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
37aca 20 20 61 73 73 65 72 74 28 20 30 3d 3d 66 74 73    assert( 0==fts
37acb 33 48 61 73 68 46 69 6e 64 28 26 70 2d 3e 70 65  3HashFind(&p->pe
37acc 6e 64 69 6e 67 54 65 72 6d 73 2c 20 7a 54 6f 6b  ndingTerms, zTok
37acd 65 6e 2c 20 6e 54 6f 6b 65 6e 29 20 29 3b 0a 20  en, nToken) );. 
37ace 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
37acf 72 65 65 28 70 4c 69 73 74 29 3b 0a 20 20 20 20  ree(pList);.    
37ad0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37ad1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
37ad2 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
37ad3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37ad4 20 20 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44      p->nPendingD
37ad5 61 74 61 20 2b 3d 20 28 70 4c 69 73 74 2d 3e 6e  ata += (pList->n
37ad6 44 61 74 61 20 2b 20 6e 54 6f 6b 65 6e 20 2b 20  Data + nToken + 
37ad7 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68 45  sizeof(Fts3HashE
37ad8 6c 65 6d 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  lem));.    }.  }
37ad9 0a 0a 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c  ..  pModule->xCl
37ada 6f 73 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74  ose(pCsr);.  ret
37adb 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
37adc 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
37add 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a   : rc);.}../* .*
37ade 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  * Calling this f
37adf 75 6e 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65  unction indicate
37ae0 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  s that subsequen
37ae1 74 20 63 61 6c 6c 73 20 74 6f 20 0a 2a 2a 20 66  t calls to .** f
37ae2 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41  ts3PendingTermsA
37ae3 64 64 28 29 20 61 72 65 20 74 6f 20 61 64 64 20  dd() are to add 
37ae4 74 65 72 6d 2f 70 6f 73 69 74 69 6f 6e 2d 6c 69  term/position-li
37ae5 73 74 20 70 61 69 72 73 20 66 6f 72 20 74 68 65  st pairs for the
37ae6 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
37ae7 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 77 69 74  the document wit
37ae8 68 20 64 6f 63 69 64 20 69 44 6f 63 69 64 2e 0a  h docid iDocid..
37ae9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
37aea 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 44 6f  s3PendingTermsDo
37aeb 63 69 64 28 46 74 73 33 54 61 62 6c 65 20 2a 70  cid(Fts3Table *p
37aec 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
37aed 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44  Docid){.  /* TOD
37aee 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f 72 65  O(shess) Explore
37aef 20 77 68 65 74 68 65 72 20 70 61 72 74 69 61 6c   whether partial
37af0 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20  ly flushing the 
37af1 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66  buffer on.  ** f
37af2 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c  orced-flush woul
37af3 64 20 70 72 6f 76 69 64 65 20 62 65 74 74 65 72  d provide better
37af4 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49   performance.  I
37af5 20 73 75 73 70 65 63 74 20 74 68 61 74 20 69 66   suspect that if
37af6 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72 65 64  .  ** we ordered
37af7 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 62 79   the doclists by
37af8 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73 68 65   size and flushe
37af9 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 75 6e  d the largest un
37afa 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62 75 66  til the.  ** buf
37afb 66 65 72 20 77 61 73 20 68 61 6c 66 20 65 6d 70  fer was half emp
37afc 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 6c  ty, that would l
37afd 65 74 20 74 68 65 20 6c 65 73 73 20 66 72 65 71  et the less freq
37afe 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20  uent terms.  ** 
37aff 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65 72 20  generate longer 
37b00 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20  doclists..  */. 
37b01 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 70 2d 3e   if( iDocid<=p->
37b02 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 70 2d  iPrevDocid || p-
37b03 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 46 54  >nPendingData>FT
37b04 53 33 5f 4d 41 58 5f 50 45 4e 44 49 4e 47 5f 44  S3_MAX_PENDING_D
37b05 41 54 41 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ATA ){.    int r
37b06 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  c = sqlite3Fts3P
37b07 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
37b08 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  (p);.    if( rc!
37b09 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
37b0a 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 2d  urn rc;.  }.  p-
37b0b 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44  >iPrevDocid = iD
37b0c 6f 63 69 64 3b 0a 20 20 72 65 74 75 72 6e 20 53  ocid;.  return S
37b0d 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 53 51 4c  QLITE_OK;.}..SQL
37b0e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
37b0f 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
37b10 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 46 74  ingTermsClear(Ft
37b11 73 33 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 46  s3Table *p){.  F
37b12 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c  ts3HashElem *pEl
37b13 65 6d 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  em;.  for(pElem=
37b14 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26 70  fts3HashFirst(&p
37b15 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b  ->pendingTerms);
37b16 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 66 74   pElem; pElem=ft
37b17 73 33 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  s3HashNext(pElem
37b18 29 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  )){.    sqlite3_
37b19 66 72 65 65 28 66 74 73 33 48 61 73 68 44 61 74  free(fts3HashDat
37b1a 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20  a(pElem));.  }. 
37b1b 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 28 26   fts3HashClear(&
37b1c 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  p->pendingTerms)
37b1d 3b 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44  ;.  p->nPendingD
37b1e 61 74 61 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ata = 0;.}../*.*
37b1f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37b20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
37b21 20 78 55 70 64 61 74 65 28 29 20 6d 65 74 68 6f   xUpdate() metho
37b22 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20  d as part of an 
37b23 49 4e 53 45 52 54 0a 2a 2a 20 6f 70 65 72 61 74  INSERT.** operat
37b24 69 6f 6e 2e 20 49 74 20 61 64 64 73 20 65 6e 74  ion. It adds ent
37b25 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 74 65  ries for each te
37b26 72 6d 20 69 6e 20 74 68 65 20 6e 65 77 20 72 65  rm in the new re
37b27 63 6f 72 64 20 74 6f 20 74 68 65 0a 2a 2a 20 70  cord to the.** p
37b28 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 68  endingTerms hash
37b29 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 72   table..**.** Ar
37b2a 67 75 6d 65 6e 74 20 61 70 56 61 6c 20 69 73 20  gument apVal is 
37b2b 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
37b2c 73 69 6d 69 6c 61 72 6c 79 20 6e 61 6d 65 64 20  similarly named 
37b2d 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
37b2e 74 6f 0a 2a 2a 20 66 74 73 33 49 6e 73 65 72 74  to.** fts3Insert
37b2f 44 61 74 61 28 29 2e 20 50 61 72 61 6d 65 74 65  Data(). Paramete
37b30 72 20 69 44 6f 63 69 64 20 69 73 20 74 68 65 20  r iDocid is the 
37b31 64 6f 63 69 64 20 6f 66 20 74 68 65 20 6e 65 77  docid of the new
37b32 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   row..*/.static 
37b33 69 6e 74 20 66 74 73 33 49 6e 73 65 72 74 54 65  int fts3InsertTe
37b34 72 6d 73 28 46 74 73 33 54 61 62 6c 65 20 2a 70  rms(Fts3Table *p
37b35 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
37b36 2a 2a 61 70 56 61 6c 29 7b 0a 20 20 69 6e 74 20  **apVal){.  int 
37b37 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
37b38 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
37b39 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
37b3a 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 32 3b 20 69   */.  for(i=2; i
37b3b 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 3b 20 69  <p->nColumn+2; i
37b3c 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ++){.    const c
37b3d 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 6f  har *zText = (co
37b3e 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
37b3f 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70  e3_value_text(ap
37b40 56 61 6c 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  Val[i]);.    if(
37b41 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20   zText ){.      
37b42 69 6e 74 20 72 63 20 3d 20 66 74 73 33 50 65 6e  int rc = fts3Pen
37b43 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c 20  dingTermsAdd(p, 
37b44 7a 54 65 78 74 2c 20 69 2d 32 29 3b 0a 20 20 20  zText, i-2);.   
37b45 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
37b46 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
37b47 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
37b48 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
37b49 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
37b4a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
37b4b 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
37b4c 64 20 62 79 20 74 68 65 20 78 55 70 64 61 74 65  d by the xUpdate
37b4d 28 29 20 6d 65 74 68 6f 64 20 66 6f 72 20 61 6e  () method for an
37b4e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
37b4f 6e 2e 0a 2a 2a 20 54 68 65 20 61 70 56 61 6c 20  n..** The apVal 
37b50 70 61 72 61 6d 65 74 65 72 20 69 73 20 70 61 73  parameter is pas
37b51 73 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68  sed a copy of th
37b52 65 20 61 70 56 61 6c 20 61 72 67 75 6d 65 6e 74  e apVal argument
37b53 20 70 61 73 73 65 64 20 62 79 0a 2a 2a 20 53 51   passed by.** SQ
37b54 4c 69 74 65 20 74 6f 20 74 68 65 20 78 55 70 64  Lite to the xUpd
37b55 61 74 65 28 29 20 6d 65 74 68 6f 64 2e 20 69 2e  ate() method. i.
37b56 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 70 56 61 6c  e:.**.**   apVal
37b57 5b 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 20  [0]             
37b58 20 20 20 4e 6f 74 20 75 73 65 64 20 66 6f 72 20     Not used for 
37b59 49 4e 53 45 52 54 2e 0a 2a 2a 20 20 20 61 70 56  INSERT..**   apV
37b5a 61 6c 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20  al[1]           
37b5b 20 20 20 20 20 72 6f 77 69 64 0a 2a 2a 20 20 20       rowid.**   
37b5c 61 70 56 61 6c 5b 32 5d 20 20 20 20 20 20 20 20  apVal[2]        
37b5d 20 20 20 20 20 20 20 20 4c 65 66 74 2d 6d 6f 73          Left-mos
37b5e 74 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63  t user-defined c
37b5f 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  olumn.**   ....*
37b60 2a 20 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f  *   apVal[p->nCo
37b61 6c 75 6d 6e 2b 31 5d 20 20 20 20 20 52 69 67 68  lumn+1]     Righ
37b62 74 2d 6d 6f 73 74 20 75 73 65 72 2d 64 65 66 69  t-most user-defi
37b63 6e 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20  ned column.**   
37b64 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e  apVal[p->nColumn
37b65 2b 32 5d 20 20 20 20 20 48 69 64 64 65 6e 20 63  +2]     Hidden c
37b66 6f 6c 75 6d 6e 20 77 69 74 68 20 73 61 6d 65 20  olumn with same 
37b67 6e 61 6d 65 20 61 73 20 74 61 62 6c 65 0a 2a 2a  name as table.**
37b68 20 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c     apVal[p->nCol
37b69 75 6d 6e 2b 33 5d 20 20 20 20 20 48 69 64 64 65  umn+3]     Hidde
37b6a 6e 20 22 64 6f 63 69 64 22 20 63 6f 6c 75 6d 6e  n "docid" column
37b6b 20 28 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69   (alias for rowi
37b6c 64 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d).*/.static int
37b6d 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61 28   fts3InsertData(
37b6e 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
37b6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37b70 20 20 20 2f 2a 20 46 75 6c 6c 2d 74 65 78 74 20     /* Full-text 
37b71 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
37b72 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
37b73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72  ,          /* Ar
37b74 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ray of values to
37b75 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 73 71 6c   insert */.  sql
37b76 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 44 6f  ite3_int64 *piDo
37b77 63 69 64 20 20 20 20 20 20 20 20 20 20 2f 2a 20  cid          /* 
37b78 4f 55 54 3a 20 44 6f 63 69 64 20 66 6f 72 20 72  OUT: Docid for r
37b79 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64  ow just inserted
37b7a 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
37b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37b7c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
37b7d 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  rn code */.  sql
37b7e 69 74 65 33 5f 73 74 6d 74 20 2a 70 43 6f 6e 74  ite3_stmt *pCont
37b7f 65 6e 74 49 6e 73 65 72 74 3b 20 20 20 2f 2a 20  entInsert;   /* 
37b80 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 5f 63 6f  INSERT INTO %_co
37b81 6e 74 65 6e 74 20 56 41 4c 55 45 53 28 2e 2e 2e  ntent VALUES(...
37b82 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  ) */..  /* Locat
37b83 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
37b84 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f 20 69  handle used to i
37b85 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20  nsert data into 
37b86 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 0a 20 20  the %_content.  
37b87 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20 53 51  ** table. The SQ
37b88 4c 20 66 6f 72 20 74 68 69 73 20 73 74 61 74 65  L for this state
37b89 6d 65 6e 74 20 69 73 3a 0a 20 20 2a 2a 0a 20 20  ment is:.  **.  
37b8a 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  **   INSERT INTO
37b8b 20 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c 55 45   %_content VALUE
37b8c 53 28 3f 2c 20 3f 2c 20 3f 2c 20 2e 2e 2e 29 0a  S(?, ?, ?, ...).
37b8d 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74    **.  ** The st
37b8e 61 74 65 6d 65 6e 74 20 66 65 61 74 75 72 65 73  atement features
37b8f 20 4e 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73   N '?' variables
37b90 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
37b91 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 0a   number of user.
37b92 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 63 6f 6c    ** defined col
37b93 75 6d 6e 73 20 69 6e 20 74 68 65 20 46 54 53 33  umns in the FTS3
37b94 20 74 61 62 6c 65 2c 20 70 6c 75 73 20 6f 6e 65   table, plus one
37b95 20 66 6f 72 20 74 68 65 20 64 6f 63 69 64 20 66   for the docid f
37b96 69 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ield..  */.  rc 
37b97 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c  = fts3SqlStmt(p,
37b98 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49 4e 53   SQL_CONTENT_INS
37b99 45 52 54 2c 20 26 70 43 6f 6e 74 65 6e 74 49 6e  ERT, &pContentIn
37b9a 73 65 72 74 2c 20 26 61 70 56 61 6c 5b 31 5d 29  sert, &apVal[1])
37b9b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
37b9c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
37b9d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
37b9e 2a 20 54 68 65 72 65 20 69 73 20 61 20 71 75 69  * There is a qui
37b9f 72 6b 20 68 65 72 65 2e 20 54 68 65 20 75 73 65  rk here. The use
37ba0 72 73 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  rs INSERT statem
37ba1 65 6e 74 20 6d 61 79 20 68 61 76 65 20 73 70 65  ent may have spe
37ba2 63 69 66 69 65 64 0a 20 20 2a 2a 20 61 20 76 61  cified.  ** a va
37ba3 6c 75 65 20 66 6f 72 20 74 68 65 20 22 72 6f 77  lue for the "row
37ba4 69 64 22 20 66 69 65 6c 64 2c 20 66 6f 72 20 74  id" field, for t
37ba5 68 65 20 22 64 6f 63 69 64 22 20 66 69 65 6c 64  he "docid" field
37ba6 2c 20 6f 72 20 66 6f 72 20 62 6f 74 68 2e 0a 20  , or for both.. 
37ba7 20 2a 2a 20 57 68 69 63 68 20 69 73 20 61 20 70   ** Which is a p
37ba8 72 6f 62 6c 65 6d 2c 20 73 69 6e 63 65 20 22 72  roblem, since "r
37ba9 6f 77 69 64 22 20 61 6e 64 20 22 64 6f 63 69 64  owid" and "docid
37baa 22 20 61 72 65 20 61 6c 69 61 73 65 73 20 66 6f  " are aliases fo
37bab 72 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20  r the.  ** same 
37bac 76 61 6c 75 65 2e 20 46 6f 72 20 65 78 61 6d 70  value. For examp
37bad 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  le:.  **.  **   
37bae 49 4e 53 45 52 54 20 49 4e 54 4f 20 66 74 73 33  INSERT INTO fts3
37baf 74 62 6c 28 72 6f 77 69 64 2c 20 64 6f 63 69 64  tbl(rowid, docid
37bb0 29 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a  ) VALUES(1, 2);.
37bb1 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 46 54 53    **.  ** In FTS
37bb2 33 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65 72  3, this is an er
37bb3 72 6f 72 2e 20 49 74 20 69 73 20 61 6e 20 65 72  ror. It is an er
37bb4 72 6f 72 20 74 6f 20 73 70 65 63 69 66 79 20 6e  ror to specify n
37bb5 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 73 0a 20  on-NULL values. 
37bb6 20 2a 2a 20 66 6f 72 20 62 6f 74 68 20 64 6f 63   ** for both doc
37bb7 69 64 20 61 6e 64 20 73 6f 6d 65 20 6f 74 68 65  id and some othe
37bb8 72 20 72 6f 77 69 64 20 61 6c 69 61 73 2e 0a 20  r rowid alias.. 
37bb9 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
37bba 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
37bbb 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
37bbc 33 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 29 20 29  3+p->nColumn]) )
37bbd 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
37bbe 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76  _NULL==sqlite3_v
37bbf 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
37bc0 30 5d 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49  0]).     && SQLI
37bc1 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33  TE_NULL!=sqlite3
37bc2 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61  _value_type(apVa
37bc3 6c 5b 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20  l[1]).    ){.   
37bc4 20 20 20 2f 2a 20 41 20 72 6f 77 69 64 2f 64 6f     /* A rowid/do
37bc5 63 69 64 20 63 6f 6e 66 6c 69 63 74 2e 20 2a 2f  cid conflict. */
37bc6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
37bc7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
37bc8 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
37bc9 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70 43  e3_bind_value(pC
37bca 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c 20 31 2c  ontentInsert, 1,
37bcb 20 61 70 56 61 6c 5b 33 2b 70 2d 3e 6e 43 6f 6c   apVal[3+p->nCol
37bcc 75 6d 6e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  umn]);.    if( r
37bcd 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
37bce 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
37bcf 20 2f 2a 20 45 78 65 63 75 74 65 20 74 68 65 20   /* Execute the 
37bd0 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 73  statement to ins
37bd1 65 72 74 20 74 68 65 20 72 65 63 6f 72 64 2e 20  ert the record. 
37bd2 53 65 74 20 2a 70 69 44 6f 63 69 64 20 74 6f 20  Set *piDocid to 
37bd3 74 68 65 20 0a 20 20 2a 2a 20 6e 65 77 20 64 6f  the .  ** new do
37bd4 63 69 64 20 76 61 6c 75 65 2e 20 0a 20 20 2a 2f  cid value. .  */
37bd5 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
37bd6 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 29 3b  pContentInsert);
37bd7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
37bd8 72 65 73 65 74 28 70 43 6f 6e 74 65 6e 74 49 6e  reset(pContentIn
37bd9 73 65 72 74 29 3b 0a 0a 20 20 2a 70 69 44 6f 63  sert);..  *piDoc
37bda 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
37bdb 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70  t_insert_rowid(p
37bdc 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  ->db);.  return 
37bdd 72 63 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 52  rc;.}..../*.** R
37bde 65 6d 6f 76 65 20 61 6c 6c 20 64 61 74 61 20 66  emove all data f
37bdf 72 6f 6d 20 74 68 65 20 46 54 53 33 20 74 61 62  rom the FTS3 tab
37be0 6c 65 2e 20 43 6c 65 61 72 20 74 68 65 20 68 61  le. Clear the ha
37be1 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  sh table contain
37be2 69 6e 67 0a 2a 2a 20 70 65 6e 64 69 6e 67 20 74  ing.** pending t
37be3 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erms..*/.static 
37be4 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 41 6c  int fts3DeleteAl
37be5 6c 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b  l(Fts3Table *p){
37be6 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
37be7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37be8 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
37be9 65 20 2a 2f 0a 0a 20 20 2f 2a 20 44 69 73 63 61  e */..  /* Disca
37bea 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rd the contents 
37beb 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74  of the pending-t
37bec 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 2e  erms hash table.
37bed 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 74 73   */.  sqlite3Fts
37bee 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65  3PendingTermsCle
37bef 61 72 28 70 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c  ar(p);..  /* Del
37bf0 65 74 65 20 65 76 65 72 79 74 68 69 6e 67 20 66  ete everything f
37bf1 72 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e  rom the %_conten
37bf2 74 2c 20 25 5f 73 65 67 6d 65 6e 74 73 20 61 6e  t, %_segments an
37bf3 64 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65  d %_segdir table
37bf4 73 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73  s. */.  rc = fts
37bf5 33 53 71 6c 45 78 65 63 28 70 2c 20 53 51 4c 5f  3SqlExec(p, SQL_
37bf6 44 45 4c 45 54 45 5f 41 4c 4c 5f 43 4f 4e 54 45  DELETE_ALL_CONTE
37bf7 4e 54 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  NT, 0);.  if( rc
37bf8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37bf9 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 45     rc = fts3SqlE
37bfa 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54  xec(p, SQL_DELET
37bfb 45 5f 41 4c 4c 5f 53 45 47 4d 45 4e 54 53 2c 20  E_ALL_SEGMENTS, 
37bfc 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  0);.  }.  if( rc
37bfd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37bfe 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 45     rc = fts3SqlE
37bff 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54  xec(p, SQL_DELET
37c00 45 5f 41 4c 4c 5f 53 45 47 44 49 52 2c 20 30 29  E_ALL_SEGDIR, 0)
37c01 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
37c02 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
37c03 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e  first element in
37c04 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61 72 72   the apVal[] arr
37c05 61 79 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ay is assumed to
37c06 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 64 6f 63   contain the doc
37c07 69 64 0a 2a 2a 20 28 61 6e 20 69 6e 74 65 67 65  id.** (an intege
37c08 72 29 20 6f 66 20 61 20 72 6f 77 20 61 62 6f 75  r) of a row abou
37c09 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e  t to be deleted.
37c0a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 74 65 72 6d   Remove all term
37c0b 73 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 75  s from the.** fu
37c0c 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 2e 0a 2a  ll-text index..*
37c0d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37c0e 33 44 65 6c 65 74 65 54 65 72 6d 73 28 46 74 73  3DeleteTerms(Fts
37c0f 33 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74  3Table *p, sqlit
37c10 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
37c11 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
37c12 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 65  qlite3_stmt *pSe
37c13 6c 65 63 74 3b 0a 0a 20 20 72 63 20 3d 20 66 74  lect;..  rc = ft
37c14 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c  s3SqlStmt(p, SQL
37c15 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54 5f  _SELECT_CONTENT_
37c16 42 59 5f 52 4f 57 49 44 2c 20 26 70 53 65 6c 65  BY_ROWID, &pSele
37c17 63 74 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66  ct, apVal);.  if
37c18 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37c19 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
37c1a 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
37c1b 74 65 70 28 70 53 65 6c 65 63 74 29 20 29 7b 0a  tep(pSelect) ){.
37c1c 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
37c1d 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70     for(i=1; i<=p
37c1e 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
37c1f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
37c20 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 6f  har *zText = (co
37c21 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
37c22 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
37c23 53 65 6c 65 63 74 2c 20 69 29 3b 0a 20 20 20 20  Select, i);.    
37c24 20 20 20 20 72 63 20 3d 20 66 74 73 33 50 65 6e      rc = fts3Pen
37c25 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c 20  dingTermsAdd(p, 
37c26 7a 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20  zText, -1);.    
37c27 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37c28 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
37c29 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
37c2a 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  (pSelect);.     
37c2b 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
37c2c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
37c2d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  }.    }.    rc =
37c2e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37c2f 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65  Select);.  }else
37c30 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
37c31 73 65 74 28 70 53 65 6c 65 63 74 29 3b 0a 20 20  set(pSelect);.  
37c32 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
37c33 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
37c34 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 6f 20 61  declaration to a
37c35 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 63  ccount for the c
37c36 69 72 63 75 6c 61 72 20 64 65 70 65 6e 64 65 6e  ircular dependen
37c37 63 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 66 75  cy between.** fu
37c38 6e 63 74 69 6f 6e 73 20 66 74 73 33 53 65 67 6d  nctions fts3Segm
37c39 65 6e 74 4d 65 72 67 65 28 29 20 61 6e 64 20 66  entMerge() and f
37c3a 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69  ts3AllocateSegdi
37c3b 72 49 64 78 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  rIdx()..*/.stati
37c3c 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e  c int fts3Segmen
37c3d 74 4d 65 72 67 65 28 46 74 73 33 54 61 62 6c 65  tMerge(Fts3Table
37c3e 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 0a 2a   *, int);../* .*
37c3f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37c40 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20  allocates a new 
37c41 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 64  level iLevel ind
37c42 65 78 20 69 6e 20 74 68 65 20 73 65 67 64 69 72  ex in the segdir
37c43 20 74 61 62 6c 65 2e 0a 2a 2a 20 55 73 75 61 6c   table..** Usual
37c44 6c 79 2c 20 69 6e 64 65 78 65 73 20 61 72 65 20  ly, indexes are 
37c45 61 6c 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e  allocated within
37c46 20 61 20 6c 65 76 65 6c 20 73 65 71 75 65 6e 74   a level sequent
37c47 69 61 6c 6c 79 20 73 74 61 72 74 69 6e 67 0a 2a  ially starting.*
37c48 2a 20 77 69 74 68 20 30 2c 20 73 6f 20 74 68 65  * with 0, so the
37c49 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78   allocated index
37c4a 20 69 73 20 6f 6e 65 20 67 72 65 61 74 65 72 20   is one greater 
37c4b 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72  than the value r
37c4c 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 3a 0a 2a  eturned.** by:.*
37c4d 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61  *.**   SELECT ma
37c4e 78 28 69 64 78 29 20 46 52 4f 4d 20 25 5f 73 65  x(idx) FROM %_se
37c4f 67 64 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c  gdir WHERE level
37c50 20 3d 20 3a 69 4c 65 76 65 6c 0a 2a 2a 0a 2a 2a   = :iLevel.**.**
37c51 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65   However, if the
37c52 72 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 46  re are already F
37c53 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20  TS3_MERGE_COUNT 
37c54 69 6e 64 65 78 65 73 20 61 74 20 74 68 65 20 72  indexes at the r
37c55 65 71 75 65 73 74 65 64 0a 2a 2a 20 6c 65 76 65  equested.** leve
37c56 6c 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  l, they are merg
37c57 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ed into a single
37c58 20 6c 65 76 65 6c 20 28 69 4c 65 76 65 6c 2b 31   level (iLevel+1
37c59 29 20 73 65 67 6d 65 6e 74 20 61 6e 64 20 74 68  ) segment and th
37c5a 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  e .** allocated 
37c5b 69 6e 64 65 78 20 69 73 20 30 2e 0a 2a 2a 0a 2a  index is 0..**.*
37c5c 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
37c5d 20 2a 70 69 49 64 78 20 69 73 20 73 65 74 20 74   *piIdx is set t
37c5e 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  o the allocated 
37c5f 69 6e 64 65 78 20 73 6c 6f 74 20 61 6e 64 20 53  index slot and S
37c60 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 72 65 74 75  QLITE_OK.** retu
37c61 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
37c62 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
37c63 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
37c64 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
37c65 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67   fts3AllocateSeg
37c66 64 69 72 49 64 78 28 46 74 73 33 54 61 62 6c 65  dirIdx(Fts3Table
37c67 20 2a 70 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *p, int iLevel,
37c68 20 69 6e 74 20 2a 70 69 49 64 78 29 7b 0a 20 20   int *piIdx){.  
37c69 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
37c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c6b 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
37c6c 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
37c6d 20 2a 70 4e 65 78 74 49 64 78 3b 20 20 20 20 20   *pNextIdx;     
37c6e 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6f 72      /* Query for
37c6f 20 6e 65 78 74 20 69 64 78 20 61 74 20 6c 65 76   next idx at lev
37c70 65 6c 20 69 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  el iLevel */.  i
37c71 6e 74 20 69 4e 65 78 74 20 3d 20 30 3b 20 20 20  nt iNext = 0;   
37c72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37c73 2a 20 52 65 73 75 6c 74 20 6f 66 20 71 75 65 72  * Result of quer
37c74 79 20 70 4e 65 78 74 49 64 78 20 2a 2f 0a 0a 20  y pNextIdx */.. 
37c75 20 2f 2a 20 53 65 74 20 76 61 72 69 61 62 6c 65   /* Set variable
37c76 20 69 4e 65 78 74 20 74 6f 20 74 68 65 20 6e 65   iNext to the ne
37c77 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67  xt available seg
37c78 64 69 72 20 69 6e 64 65 78 20 61 74 20 6c 65 76  dir index at lev
37c79 65 6c 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20  el iLevel. */.  
37c7a 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
37c7b 28 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47  (p, SQL_NEXT_SEG
37c7c 4d 45 4e 54 5f 49 4e 44 45 58 2c 20 26 70 4e 65  MENT_INDEX, &pNe
37c7d 78 74 49 64 78 2c 20 30 29 3b 0a 20 20 69 66 28  xtIdx, 0);.  if(
37c7e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37c7f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69  {.    sqlite3_bi
37c80 6e 64 5f 69 6e 74 28 70 4e 65 78 74 49 64 78 2c  nd_int(pNextIdx,
37c81 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20   1, iLevel);.   
37c82 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
37c83 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 4e  =sqlite3_step(pN
37c84 65 78 74 49 64 78 29 20 29 7b 0a 20 20 20 20 20  extIdx) ){.     
37c85 20 69 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33   iNext = sqlite3
37c86 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 4e 65 78  _column_int(pNex
37c87 74 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  tIdx, 0);.    }.
37c88 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37c89 5f 72 65 73 65 74 28 70 4e 65 78 74 49 64 78 29  _reset(pNextIdx)
37c8a 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
37c8b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37c8c 20 20 2f 2a 20 49 66 20 69 4e 65 78 74 20 69 73    /* If iNext is
37c8d 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e   FTS3_MERGE_COUN
37c8e 54 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  T, indicating th
37c8f 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20  at level iLevel 
37c90 69 73 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a  is already.    *
37c91 2a 20 66 75 6c 6c 2c 20 6d 65 72 67 65 20 61 6c  * full, merge al
37c92 6c 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 6c 65  l segments in le
37c93 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20  vel iLevel into 
37c94 61 20 73 69 6e 67 6c 65 20 69 4c 65 76 65 6c 2b  a single iLevel+
37c95 31 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74  1.    ** segment
37c96 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 28 6e   and allocate (n
37c97 65 77 6c 79 20 66 72 65 65 64 29 20 69 6e 64 65  ewly freed) inde
37c98 78 20 30 20 61 74 20 6c 65 76 65 6c 20 69 4c 65  x 0 at level iLe
37c99 76 65 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  vel. Otherwise,.
37c9a 20 20 20 20 2a 2a 20 69 66 20 69 4e 65 78 74 20      ** if iNext 
37c9b 69 73 20 6c 65 73 73 20 74 68 61 6e 20 46 54 53  is less than FTS
37c9c 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 2c 20 61  3_MERGE_COUNT, a
37c9d 6c 6c 6f 63 61 74 65 20 69 6e 64 65 78 20 69 4e  llocate index iN
37c9e 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext..    */.    
37c9f 69 66 28 20 69 4e 65 78 74 3e 3d 46 54 53 33 5f  if( iNext>=FTS3_
37ca0 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 7b 0a 20  MERGE_COUNT ){. 
37ca1 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65       rc = fts3Se
37ca2 67 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20 69 4c  gmentMerge(p, iL
37ca3 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 2a 70 69  evel);.      *pi
37ca4 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  Idx = 0;.    }el
37ca5 73 65 7b 0a 20 20 20 20 20 20 2a 70 69 49 64 78  se{.      *piIdx
37ca6 20 3d 20 69 4e 65 78 74 3b 0a 20 20 20 20 7d 0a   = iNext;.    }.
37ca7 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
37ca8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
37ca9 74 68 65 20 69 74 65 72 61 74 6f 72 20 70 61 73  the iterator pas
37caa 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
37cab 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
37cac 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74 68   next term in th
37cad 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 49 66  e.** segment. If
37cae 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
37caf 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
37cb0 65 64 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  ed. If there is 
37cb1 6e 6f 20 6e 65 78 74 20 74 65 72 6d 2c 0a 2a 2a  no next term,.**
37cb2 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 74   SQLITE_DONE. Ot
37cb3 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69  herwise, an SQLi
37cb4 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  te error code..*
37cb5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37cb6 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 28 46  3SegReaderNext(F
37cb7 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
37cb8 65 61 64 65 72 29 7b 0a 20 20 63 68 61 72 20 2a  eader){.  char *
37cb9 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
37cba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
37cbb 73 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  sor variable */.
37cbc 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20    int nPrefix;  
37cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37cbe 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
37cbf 79 74 65 73 20 69 6e 20 74 65 72 6d 20 70 72 65  ytes in term pre
37cc0 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75  fix */.  int nSu
37cc1 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  ffix;           
37cc2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
37cc3 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
37cc4 65 72 6d 20 73 75 66 66 69 78 20 2a 2f 0a 0a 20  erm suffix */.. 
37cc5 20 69 66 28 20 21 70 52 65 61 64 65 72 2d 3e 61   if( !pReader->a
37cc6 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20 20 20 70  Doclist ){.    p
37cc7 4e 65 78 74 20 3d 20 70 52 65 61 64 65 72 2d 3e  Next = pReader->
37cc8 61 4e 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  aNode;.  }else{.
37cc9 20 20 20 20 70 4e 65 78 74 20 3d 20 26 70 52 65      pNext = &pRe
37cca 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 5b 70  ader->aDoclist[p
37ccb 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74  Reader->nDoclist
37ccc 5d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70  ];.  }..  if( !p
37ccd 4e 65 78 74 20 7c 7c 20 70 4e 65 78 74 3e 3d 26  Next || pNext>=&
37cce 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70  pReader->aNode[p
37ccf 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d 20 29  Reader->nNode] )
37cd0 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
37cd1 20 20 69 66 28 20 21 70 52 65 61 64 65 72 2d 3e    if( !pReader->
37cd2 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 70  pStmt ){.      p
37cd3 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20 3d 20  Reader->aNode = 
37cd4 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
37cd5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
37cd6 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
37cd7 33 5f 73 74 65 70 28 70 52 65 61 64 65 72 2d 3e  3_step(pReader->
37cd8 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20  pStmt);.    if( 
37cd9 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
37cda 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d  {.      pReader-
37cdb 3e 61 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20  >aNode = 0;.    
37cdc 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
37cdd 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
37cde 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 20 20 20  TE_OK : rc);.   
37cdf 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e   }.    pReader->
37ce0 6e 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f  nNode = sqlite3_
37ce1 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65  column_bytes(pRe
37ce2 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b  ader->pStmt, 0);
37ce3 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61 4e  .    pReader->aN
37ce4 6f 64 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ode = (char *)sq
37ce5 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
37ce6 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  b(pReader->pStmt
37ce7 2c 20 30 29 3b 0a 20 20 20 20 70 4e 65 78 74 20  , 0);.    pNext 
37ce8 3d 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  = pReader->aNode
37ce9 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 4e 65 78 74  ;.  }.  .  pNext
37cea 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
37ceb 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74  etVarint32(pNext
37cec 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 70  , &nPrefix);.  p
37ced 4e 65 78 74 20 2b 3d 20 73 71 6c 69 74 65 33 46  Next += sqlite3F
37cee 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
37cef 4e 65 78 74 2c 20 26 6e 53 75 66 66 69 78 29 3b  Next, &nSuffix);
37cf0 0a 0a 20 20 69 66 28 20 6e 50 72 65 66 69 78 2b  ..  if( nPrefix+
37cf1 6e 53 75 66 66 69 78 3e 70 52 65 61 64 65 72 2d  nSuffix>pReader-
37cf2 3e 6e 54 65 72 6d 41 6c 6c 6f 63 20 29 7b 0a 20  >nTermAlloc ){. 
37cf3 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 6e     int nNew = (n
37cf4 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 29 2a  Prefix+nSuffix)*
37cf5 32 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65  2;.    char *zNe
37cf6 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
37cf7 6c 6f 63 28 70 52 65 61 64 65 72 2d 3e 7a 54 65  loc(pReader->zTe
37cf8 72 6d 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 69  rm, nNew);.    i
37cf9 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
37cfa 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37cfb 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
37cfc 20 70 52 65 61 64 65 72 2d 3e 7a 54 65 72 6d 20   pReader->zTerm 
37cfd 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 70 52 65 61  = zNew;.    pRea
37cfe 64 65 72 2d 3e 6e 54 65 72 6d 41 6c 6c 6f 63 20  der->nTermAlloc 
37cff 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 20 20 6d 65  = nNew;.  }.  me
37d00 6d 63 70 79 28 26 70 52 65 61 64 65 72 2d 3e 7a  mcpy(&pReader->z
37d01 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c 20 70  Term[nPrefix], p
37d02 4e 65 78 74 2c 20 6e 53 75 66 66 69 78 29 3b 0a  Next, nSuffix);.
37d03 20 20 70 52 65 61 64 65 72 2d 3e 6e 54 65 72 6d    pReader->nTerm
37d04 20 3d 20 6e 50 72 65 66 69 78 2b 6e 53 75 66 66   = nPrefix+nSuff
37d05 69 78 3b 0a 20 20 70 4e 65 78 74 20 2b 3d 20 6e  ix;.  pNext += n
37d06 53 75 66 66 69 78 3b 0a 20 20 70 4e 65 78 74 20  Suffix;.  pNext 
37d07 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
37d08 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74 2c  tVarint32(pNext,
37d09 20 26 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c   &pReader->nDocl
37d0a 69 73 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ist);.  assert( 
37d0b 70 4e 65 78 74 3c 26 70 52 65 61 64 65 72 2d 3e  pNext<&pReader->
37d0c 61 4e 6f 64 65 5b 70 52 65 61 64 65 72 2d 3e 6e  aNode[pReader->n
37d0d 4e 6f 64 65 5d 20 29 3b 0a 20 20 70 52 65 61 64  Node] );.  pRead
37d0e 65 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 70  er->aDoclist = p
37d0f 4e 65 78 74 3b 0a 20 20 70 52 65 61 64 65 72 2d  Next;.  pReader-
37d10 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 30  >pOffsetList = 0
37d11 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
37d12 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
37d13 65 74 20 74 68 65 20 53 65 67 52 65 61 64 65 72  et the SegReader
37d14 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
37d15 20 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20   first docid in 
37d16 74 68 65 20 64 6f 63 6c 69 73 74 20 61 73 73 6f  the doclist asso
37d17 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
37d18 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e  he current term.
37d19 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
37d1a 66 74 73 33 53 65 67 52 65 61 64 65 72 46 69 72  fts3SegReaderFir
37d1b 73 74 44 6f 63 69 64 28 46 74 73 33 53 65 67 52  stDocid(Fts3SegR
37d1c 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
37d1d 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73 73 65  .  int n;.  asse
37d1e 72 74 28 20 70 52 65 61 64 65 72 2d 3e 61 44 6f  rt( pReader->aDo
37d1f 63 6c 69 73 74 20 29 3b 0a 20 20 61 73 73 65 72  clist );.  asser
37d20 74 28 20 21 70 52 65 61 64 65 72 2d 3e 70 4f 66  t( !pReader->pOf
37d21 66 73 65 74 4c 69 73 74 20 29 3b 0a 20 20 6e 20  fsetList );.  n 
37d22 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
37d23 56 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e  Varint(pReader->
37d24 61 44 6f 63 6c 69 73 74 2c 20 26 70 52 65 61 64  aDoclist, &pRead
37d25 65 72 2d 3e 69 44 6f 63 69 64 29 3b 0a 20 20 70  er->iDocid);.  p
37d26 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37d27 69 73 74 20 3d 20 26 70 52 65 61 64 65 72 2d 3e  ist = &pReader->
37d28 61 44 6f 63 6c 69 73 74 5b 6e 5d 3b 0a 7d 0a 0a  aDoclist[n];.}..
37d29 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68  /*.** Advance th
37d2a 65 20 53 65 67 52 65 61 64 65 72 20 74 6f 20 70  e SegReader to p
37d2b 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78 74  oint to the next
37d2c 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
37d2d 63 6c 69 73 74 0a 2a 2a 20 61 73 73 6f 63 69 61  clist.** associa
37d2e 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  ted with the cur
37d2f 72 65 6e 74 20 74 65 72 6d 2e 0a 2a 2a 20 0a 2a  rent term..** .*
37d30 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 73 20 70  * If arguments p
37d31 70 4f 66 66 73 65 74 4c 69 73 74 20 61 6e 64 20  pOffsetList and 
37d32 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 61 72 65  pnOffsetList are
37d33 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
37d34 0a 2a 2a 20 2a 70 70 4f 66 66 73 65 74 4c 69 73  .** *ppOffsetLis
37d35 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
37d36 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63  t to the first c
37d37 6f 6c 75 6d 6e 2d 6f 66 66 73 65 74 20 6c 69 73  olumn-offset lis
37d38 74 0a 2a 2a 20 69 6e 20 74 68 65 20 64 6f 63 6c  t.** in the docl
37d39 69 73 74 20 65 6e 74 72 79 20 28 69 2e 65 2e 20  ist entry (i.e. 
37d3a 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 61 73 74  immediately past
37d3b 20 74 68 65 20 64 6f 63 69 64 20 76 61 72 69 6e   the docid varin
37d3c 74 29 2e 0a 2a 2a 20 2a 70 6e 4f 66 66 73 65 74  t)..** *pnOffset
37d3d 4c 69 73 74 20 69 73 20 73 65 74 20 74 6f 20 74  List is set to t
37d3e 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
37d3f 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 2d 6f   set of column-o
37d40 66 66 73 65 74 0a 2a 2a 20 6c 69 73 74 73 2c 20  ffset.** lists, 
37d41 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  not including th
37d42 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  e nul-terminator
37d43 20 62 79 74 65 2e 20 46 6f 72 20 65 78 61 6d 70   byte. For examp
37d44 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le:.*/.static vo
37d45 69 64 20 66 74 73 33 53 65 67 52 65 61 64 65 72  id fts3SegReader
37d46 4e 65 78 74 44 6f 63 69 64 28 0a 20 20 46 74 73  NextDocid(.  Fts
37d47 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65 61  3SegReader *pRea
37d48 64 65 72 2c 0a 20 20 63 68 61 72 20 2a 2a 70 70  der,.  char **pp
37d49 4f 66 66 73 65 74 4c 69 73 74 2c 0a 20 20 69 6e  OffsetList,.  in
37d4a 74 20 2a 70 6e 4f 66 66 73 65 74 4c 69 73 74 0a  t *pnOffsetList.
37d4b 29 7b 0a 20 20 63 68 61 72 20 2a 70 20 3d 20 70  ){.  char *p = p
37d4c 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37d4d 69 73 74 3b 0a 20 20 63 68 61 72 20 63 20 3d 20  ist;.  char c = 
37d4e 30 3b 0a 0a 20 20 2f 2a 20 50 6f 69 6e 74 65 72  0;..  /* Pointer
37d4f 20 70 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69   p currently poi
37d50 6e 74 73 20 61 74 20 74 68 65 20 66 69 72 73 74  nts at the first
37d51 20 62 79 74 65 20 6f 66 20 61 6e 20 6f 66 66 73   byte of an offs
37d52 65 74 20 6c 69 73 74 2e 20 54 68 65 0a 20 20 2a  et list. The.  *
37d53 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  * following two 
37d54 6c 69 6e 65 73 20 61 64 76 61 6e 63 65 20 69 74  lines advance it
37d55 20 74 6f 20 70 6f 69 6e 74 20 6f 6e 65 20 62 79   to point one by
37d56 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
37d57 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  of.  ** the same
37d58 20 6f 66 66 73 65 74 20 6c 69 73 74 2e 0a 20 20   offset list..  
37d59 2a 2f 0a 20 20 77 68 69 6c 65 28 20 2a 70 20 7c  */.  while( *p |
37d5a 20 63 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26 20   c ) c = *p++ & 
37d5b 30 78 38 30 3b 0a 20 20 70 2b 2b 3b 0a 0a 20 20  0x80;.  p++;..  
37d5c 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20  /* If required, 
37d5d 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 75 74  populate the out
37d5e 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 77 69  put variables wi
37d5f 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  th a pointer to 
37d60 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73 69 7a  and the.  ** siz
37d61 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  e of the previou
37d62 73 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e 0a 20  s offset-list.. 
37d63 20 2a 2f 0a 20 20 69 66 28 20 70 70 4f 66 66 73   */.  if( ppOffs
37d64 65 74 4c 69 73 74 20 29 7b 0a 20 20 20 20 2a 70  etList ){.    *p
37d65 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 70 52  pOffsetList = pR
37d66 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
37d67 73 74 3b 0a 20 20 20 20 2a 70 6e 4f 66 66 73 65  st;.    *pnOffse
37d68 74 4c 69 73 74 20 3d 20 28 69 6e 74 29 28 70 20  tList = (int)(p 
37d69 2d 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73  - pReader->pOffs
37d6a 65 74 4c 69 73 74 20 2d 20 31 29 3b 0a 20 20 7d  etList - 1);.  }
37d6b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
37d6c 61 72 65 20 6e 6f 20 6d 6f 72 65 20 65 6e 74 72  are no more entr
37d6d 69 65 73 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  ies in the docli
37d6e 73 74 2c 20 73 65 74 20 70 4f 66 66 73 65 74 4c  st, set pOffsetL
37d6f 69 73 74 20 74 6f 0a 20 20 2a 2a 20 4e 55 4c 4c  ist to.  ** NULL
37d70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74  . Otherwise, set
37d71 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 69   Fts3SegReader.i
37d72 44 6f 63 69 64 20 74 6f 20 74 68 65 20 6e 65 78  Docid to the nex
37d73 74 20 64 6f 63 69 64 20 61 6e 64 0a 20 20 2a 2a  t docid and.  **
37d74 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 70   Fts3SegReader.p
37d75 4f 66 66 73 65 74 4c 69 73 74 20 74 6f 20 70 6f  OffsetList to po
37d76 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20  int to the next 
37d77 6f 66 66 73 65 74 20 6c 69 73 74 20 62 65 66 6f  offset list befo
37d78 72 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 69 6e  re.  ** returnin
37d79 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 3e  g..  */.  if( p>
37d7a 3d 26 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c  =&pReader->aDocl
37d7b 69 73 74 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f  ist[pReader->nDo
37d7c 63 6c 69 73 74 5d 20 29 7b 0a 20 20 20 20 70 52  clist] ){.    pR
37d7d 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
37d7e 73 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  st = 0;.  }else{
37d7f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
37d80 36 34 20 69 44 65 6c 74 61 3b 0a 20 20 20 20 70  64 iDelta;.    p
37d81 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37d82 69 73 74 20 3d 20 70 20 2b 20 73 71 6c 69 74 65  ist = p + sqlite
37d83 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
37d84 2c 20 26 69 44 65 6c 74 61 29 3b 0a 20 20 20 20  , &iDelta);.    
37d85 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 20  pReader->iDocid 
37d86 2b 3d 20 69 44 65 6c 74 61 3b 0a 20 20 7d 0a 7d  += iDelta;.  }.}
37d87 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c  ../*.** Free all
37d88 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73   allocations ass
37d89 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
37d8a 20 69 74 65 72 61 74 6f 72 20 70 61 73 73 65 64   iterator passed
37d8b 20 61 73 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f   as the .** seco
37d8c 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  nd argument..*/.
37d8d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
37d8e 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
37d8f 65 67 52 65 61 64 65 72 46 72 65 65 28 46 74 73  egReaderFree(Fts
37d90 33 54 61 62 6c 65 20 2a 70 2c 20 46 74 73 33 53  3Table *p, Fts3S
37d91 65 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  egReader *pReade
37d92 72 29 7b 0a 20 20 69 66 28 20 70 52 65 61 64 65  r){.  if( pReade
37d93 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 65  r ){.    if( pRe
37d94 61 64 65 72 2d 3e 70 53 74 6d 74 20 29 7b 0a 20  ader->pStmt ){. 
37d95 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65       /* Move the
37d96 20 6c 65 61 66 2d 72 61 6e 67 65 20 53 45 4c 45   leaf-range SELE
37d97 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
37d98 74 68 65 20 61 4c 65 61 76 65 73 53 74 6d 74 5b  the aLeavesStmt[
37d99 5d 20 61 72 72 61 79 2c 0a 20 20 20 20 20 20 2a  ] array,.      *
37d9a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  * so that it can
37d9b 20 62 65 20 72 65 75 73 65 64 20 77 68 65 6e 20   be reused when 
37d9c 72 65 71 75 69 72 65 64 20 62 79 20 61 6e 6f 74  required by anot
37d9d 68 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20 20  her query..     
37d9e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
37d9f 28 20 70 2d 3e 6e 4c 65 61 76 65 73 53 74 6d 74  ( p->nLeavesStmt
37da0 3c 70 2d 3e 6e 4c 65 61 76 65 73 54 6f 74 61 6c  <p->nLeavesTotal
37da1 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
37da2 33 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d  3_reset(pReader-
37da3 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 70  >pStmt);.      p
37da4 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 5b 70 2d  ->aLeavesStmt[p-
37da5 3e 6e 4c 65 61 76 65 73 53 74 6d 74 2b 2b 5d 20  >nLeavesStmt++] 
37da6 3d 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  = pReader->pStmt
37da7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
37da8 74 65 33 5f 66 72 65 65 28 70 52 65 61 64 65 72  te3_free(pReader
37da9 2d 3e 7a 54 65 72 6d 29 3b 0a 20 20 20 20 73 71  ->zTerm);.    sq
37daa 6c 69 74 65 33 5f 66 72 65 65 28 70 52 65 61 64  lite3_free(pRead
37dab 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  er);.  }.}../*.*
37dac 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
37dad 20 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63   SegReader objec
37dae 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
37daf 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
37db0 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77  Fts3SegReaderNew
37db1 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
37db2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37db3 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
37db4 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
37db5 20 69 6e 74 20 69 41 67 65 2c 20 20 20 20 20 20   int iAge,      
37db6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37db7 20 2f 2a 20 53 65 67 6d 65 6e 74 20 22 61 67 65   /* Segment "age
37db8 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ". */.  sqlite3_
37db9 69 6e 74 36 34 20 69 53 74 61 72 74 4c 65 61 66  int64 iStartLeaf
37dba 2c 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ,       /* First
37dbb 20 6c 65 61 66 20 74 6f 20 74 72 61 76 65 72 73   leaf to travers
37dbc 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
37dbd 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 20 20  nt64 iEndLeaf,  
37dbe 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20         /* Final 
37dbf 6c 65 61 66 20 74 6f 20 74 72 61 76 65 72 73 65  leaf to traverse
37dc0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37dc1 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 2c 20 20  t64 iEndBlock,  
37dc2 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20 62        /* Final b
37dc3 6c 6f 63 6b 20 6f 66 20 73 65 67 6d 65 6e 74 20  lock of segment 
37dc4 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
37dc5 2a 7a 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20  *zRoot,         
37dc6 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63       /* Buffer c
37dc7 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6e  ontaining root n
37dc8 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  ode */.  int nRo
37dc9 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ot,             
37dca 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
37dcb 20 6f 66 20 62 75 66 66 65 72 20 63 6f 6e 74 61   of buffer conta
37dcc 69 6e 69 6e 67 20 72 6f 6f 74 20 6e 6f 64 65 20  ining root node 
37dcd 2a 2f 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  */.  Fts3SegRead
37dce 65 72 20 2a 2a 70 70 52 65 61 64 65 72 20 20 20  er **ppReader   
37dcf 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 6c 6c       /* OUT: All
37dd0 6f 63 61 74 65 64 20 46 74 73 33 53 65 67 52 65  ocated Fts3SegRe
37dd1 61 64 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ader */.){.  int
37dd2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
37dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37dd4 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
37dd5 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
37dd6 70 52 65 61 64 65 72 3b 20 20 20 20 20 20 20 20  pReader;        
37dd7 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c 6f 63 61   /* Newly alloca
37dd8 74 65 64 20 53 65 67 52 65 61 64 65 72 20 6f 62  ted SegReader ob
37dd9 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  ject */.  int nE
37dda 78 74 72 61 20 3d 20 30 3b 20 20 20 20 20 20 20  xtra = 0;       
37ddb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
37ddc 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73  es to allocate s
37ddd 65 67 6d 65 6e 74 20 72 6f 6f 74 20 6e 6f 64 65  egment root node
37dde 20 2a 2f 0a 0a 20 20 69 66 28 20 69 53 74 61 72   */..  if( iStar
37ddf 74 4c 65 61 66 3d 3d 30 20 29 7b 0a 20 20 20 20  tLeaf==0 ){.    
37de0 6e 45 78 74 72 61 20 3d 20 6e 52 6f 6f 74 3b 0a  nExtra = nRoot;.
37de1 20 20 7d 0a 0a 20 20 70 52 65 61 64 65 72 20 3d    }..  pReader =
37de2 20 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20   (Fts3SegReader 
37de3 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
37de4 28 73 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52  (sizeof(Fts3SegR
37de5 65 61 64 65 72 29 20 2b 20 6e 45 78 74 72 61 29  eader) + nExtra)
37de6 3b 0a 20 20 69 66 28 20 21 70 52 65 61 64 65 72  ;.  if( !pReader
37de7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
37de8 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
37de9 0a 20 20 6d 65 6d 73 65 74 28 70 52 65 61 64 65  .  memset(pReade
37dea 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73  r, 0, sizeof(Fts
37deb 33 53 65 67 52 65 61 64 65 72 29 29 3b 0a 20 20  3SegReader));.  
37dec 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74 42  pReader->iStartB
37ded 6c 6f 63 6b 20 3d 20 69 53 74 61 72 74 4c 65 61  lock = iStartLea
37dee 66 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 49  f;.  pReader->iI
37def 64 78 20 3d 20 69 41 67 65 3b 0a 20 20 70 52 65  dx = iAge;.  pRe
37df0 61 64 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 20  ader->iEndBlock 
37df1 3d 20 69 45 6e 64 42 6c 6f 63 6b 3b 0a 0a 20 20  = iEndBlock;..  
37df2 69 66 28 20 6e 45 78 74 72 61 20 29 7b 0a 20 20  if( nExtra ){.  
37df3 20 20 2f 2a 20 54 68 65 20 65 6e 74 69 72 65 20    /* The entire 
37df4 73 65 67 6d 65 6e 74 20 69 73 20 73 74 6f 72 65  segment is store
37df5 64 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 6e 6f  d in the root no
37df6 64 65 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  de. */.    pRead
37df7 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 28 63 68 61  er->aNode = (cha
37df8 72 20 2a 29 26 70 52 65 61 64 65 72 5b 31 5d 3b  r *)&pReader[1];
37df9 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 4e  .    pReader->nN
37dfa 6f 64 65 20 3d 20 6e 52 6f 6f 74 3b 0a 20 20 20  ode = nRoot;.   
37dfb 20 6d 65 6d 63 70 79 28 70 52 65 61 64 65 72 2d   memcpy(pReader-
37dfc 3e 61 4e 6f 64 65 2c 20 7a 52 6f 6f 74 2c 20 6e  >aNode, zRoot, n
37dfd 52 6f 6f 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Root);.  }else{.
37dfe 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65      /* If the te
37dff 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  xt of the SQL st
37e00 61 74 65 6d 65 6e 74 20 74 6f 20 69 74 65 72 61  atement to itera
37e01 74 65 20 74 68 72 6f 75 67 68 20 61 20 63 6f 6e  te through a con
37e02 74 69 67 75 6f 75 73 0a 20 20 20 20 2a 2a 20 73  tiguous.    ** s
37e03 65 74 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  et of entries in
37e04 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20   the %_segments 
37e05 74 61 62 6c 65 20 68 61 73 20 6e 6f 74 20 79 65  table has not ye
37e06 74 20 62 65 65 6e 20 63 6f 6d 70 6f 73 65 64 2c  t been composed,
37e07 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 6f 73 65 20  .    ** compose 
37e08 69 74 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20  it now..    */. 
37e09 20 20 20 69 66 28 20 21 70 2d 3e 7a 53 65 6c 65     if( !p->zSele
37e0a 63 74 4c 65 61 76 65 73 20 29 7b 0a 20 20 20 20  ctLeaves ){.    
37e0b 20 20 70 2d 3e 7a 53 65 6c 65 63 74 4c 65 61 76    p->zSelectLeav
37e0c 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  es = sqlite3_mpr
37e0d 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20 20 20  intf(.          
37e0e 22 53 45 4c 45 43 54 20 62 6c 6f 63 6b 20 46 52  "SELECT block FR
37e0f 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e  OM %Q.'%q_segmen
37e10 74 73 27 20 57 48 45 52 45 20 62 6c 6f 63 6b 69  ts' WHERE blocki
37e11 64 20 42 45 54 57 45 45 4e 20 3f 20 41 4e 44 20  d BETWEEN ? AND 
37e12 3f 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 4f  ? ".          "O
37e13 52 44 45 52 20 42 59 20 62 6c 6f 63 6b 69 64 22  RDER BY blockid"
37e14 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61  , p->zDb, p->zNa
37e15 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  me.      );.    
37e16 20 20 69 66 28 20 21 70 2d 3e 7a 53 65 6c 65 63    if( !p->zSelec
37e17 74 4c 65 61 76 65 73 20 29 7b 0a 20 20 20 20 20  tLeaves ){.     
37e18 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
37e19 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f  OMEM;.        go
37e1a 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
37e1b 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
37e1c 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
37e1d 6e 6f 20 66 72 65 65 20 73 74 61 74 65 6d 65 6e  no free statemen
37e1e 74 73 20 69 6e 20 74 68 65 20 61 4c 65 61 76 65  ts in the aLeave
37e1f 73 53 74 6d 74 5b 5d 20 61 72 72 61 79 2c 20 70  sStmt[] array, p
37e20 72 65 70 61 72 65 0a 20 20 20 20 2a 2a 20 61 20  repare.    ** a 
37e21 6e 65 77 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f  new statement no
37e22 77 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  w. Otherwise, re
37e23 75 73 65 20 61 20 70 72 65 70 61 72 65 64 20 73  use a prepared s
37e24 74 61 74 65 6d 65 6e 74 20 66 72 6f 6d 0a 20 20  tatement from.  
37e25 20 20 2a 2a 20 61 4c 65 61 76 65 73 53 74 6d 74    ** aLeavesStmt
37e26 5b 5d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  []..    */.    i
37e27 66 28 20 70 2d 3e 6e 4c 65 61 76 65 73 53 74 6d  f( p->nLeavesStm
37e28 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  t==0 ){.      if
37e29 28 20 70 2d 3e 6e 4c 65 61 76 65 73 54 6f 74 61  ( p->nLeavesTota
37e2a 6c 3d 3d 70 2d 3e 6e 4c 65 61 76 65 73 41 6c 6c  l==p->nLeavesAll
37e2b 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  oc ){.        in
37e2c 74 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 4c 65 61  t nNew = p->nLea
37e2d 76 65 73 41 6c 6c 6f 63 20 2b 20 31 36 3b 0a 20  vesAlloc + 16;. 
37e2e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
37e2f 74 6d 74 20 2a 2a 61 4e 65 77 20 3d 20 28 73 71  tmt **aNew = (sq
37e30 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 29 73 71  lite3_stmt **)sq
37e31 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a 20  lite3_realloc(. 
37e32 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4c             p->aL
37e33 65 61 76 65 73 53 74 6d 74 2c 20 6e 4e 65 77 2a  eavesStmt, nNew*
37e34 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 73  sizeof(sqlite3_s
37e35 74 6d 74 20 2a 29 0a 20 20 20 20 20 20 20 20 29  tmt *).        )
37e36 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 61  ;.        if( !a
37e37 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20  New ){.         
37e38 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
37e39 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  EM;.          go
37e3a 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
37e3b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
37e3c 2d 3e 6e 4c 65 61 76 65 73 41 6c 6c 6f 63 20 3d  ->nLeavesAlloc =
37e3d 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70   nNew;.        p
37e3e 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 20 3d 20  ->aLeavesStmt = 
37e3f 61 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20  aNew;.      }.  
37e40 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37e41 5f 70 72 65 70 61 72 65 5f 76 32 28 70 2d 3e 64  _prepare_v2(p->d
37e42 62 2c 20 70 2d 3e 7a 53 65 6c 65 63 74 4c 65 61  b, p->zSelectLea
37e43 76 65 73 2c 20 2d 31 2c 20 26 70 52 65 61 64 65  ves, -1, &pReade
37e44 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  r->pStmt, 0);.  
37e45 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37e46 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
37e47 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a   goto finished;.
37e48 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
37e49 3e 6e 4c 65 61 76 65 73 54 6f 74 61 6c 2b 2b 3b  >nLeavesTotal++;
37e4a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37e4b 20 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74    pReader->pStmt
37e4c 20 3d 20 70 2d 3e 61 4c 65 61 76 65 73 53 74 6d   = p->aLeavesStm
37e4d 74 5b 2d 2d 70 2d 3e 6e 4c 65 61 76 65 73 53 74  t[--p->nLeavesSt
37e4e 6d 74 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  mt];.    }..    
37e4f 2f 2a 20 42 69 6e 64 20 74 68 65 20 73 74 61 72  /* Bind the star
37e50 74 20 61 6e 64 20 65 6e 64 20 6c 65 61 66 20 62  t and end leaf b
37e51 6c 6f 63 6b 69 64 73 20 74 6f 20 74 68 65 20 70  lockids to the p
37e52 72 65 70 61 72 65 64 20 53 51 4c 20 73 74 61 74  repared SQL stat
37e53 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71  ement. */.    sq
37e54 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
37e55 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c  (pReader->pStmt,
37e56 20 31 2c 20 69 53 74 61 72 74 4c 65 61 66 29 3b   1, iStartLeaf);
37e57 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  .    sqlite3_bin
37e58 64 5f 69 6e 74 36 34 28 70 52 65 61 64 65 72 2d  d_int64(pReader-
37e59 3e 70 53 74 6d 74 2c 20 32 2c 20 69 45 6e 64 4c  >pStmt, 2, iEndL
37e5a 65 61 66 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  eaf);.  }.  rc =
37e5b 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65   fts3SegReaderNe
37e5c 78 74 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 66  xt(pReader);.. f
37e5d 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 72  inished:.  if( r
37e5e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37e5f 20 20 20 20 2a 70 70 52 65 61 64 65 72 20 3d 20      *ppReader = 
37e60 70 52 65 61 64 65 72 3b 0a 20 20 7d 65 6c 73 65  pReader;.  }else
37e61 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
37e62 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28 70  3SegReaderFree(p
37e63 2c 20 70 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a  , pReader);.  }.
37e64 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
37e65 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ./*.** The secon
37e66 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
37e67 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  is function is e
37e68 78 70 65 63 74 65 64 20 74 6f 20 62 65 20 61 20  xpected to be a 
37e69 73 74 61 74 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20  statement of.** 
37e6a 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
37e6b 20 20 53 45 4c 45 43 54 20 0a 2a 2a 20 20 20 20    SELECT .**    
37e6c 20 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20   idx,           
37e6d 20 20 20 20 20 20 20 2d 2d 20 63 6f 6c 20 30 0a         -- col 0.
37e6e 2a 2a 20 20 20 20 20 73 74 61 72 74 5f 62 6c 6f  **     start_blo
37e6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 2d 2d 20  ck,          -- 
37e70 63 6f 6c 20 31 0a 2a 2a 20 20 20 20 20 6c 65 61  col 1.**     lea
37e71 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 20  ves_end_block,  
37e72 20 20 20 2d 2d 20 63 6f 6c 20 32 0a 2a 2a 20 20     -- col 2.**  
37e73 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 2c 20 20 20     end_block,   
37e74 20 20 20 20 20 20 20 20 20 2d 2d 20 63 6f 6c 20           -- col 
37e75 33 0a 2a 2a 20 20 20 20 20 72 6f 6f 74 20 20 20  3.**     root   
37e76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
37e77 2d 20 63 6f 6c 20 34 0a 2a 2a 20 20 20 46 52 4f  - col 4.**   FRO
37e78 4d 20 25 5f 73 65 67 64 69 72 20 2e 2e 2e 0a 2a  M %_segdir ....*
37e79 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
37e7a 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 64  on allocates and
37e7b 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 61 20 46   initializes a F
37e7c 74 73 33 53 65 67 52 65 61 64 65 72 20 73 74 72  ts3SegReader str
37e7d 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 69 74 65  ucture to.** ite
37e7e 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65  rate through the
37e7f 20 74 65 72 6d 73 20 73 74 6f 72 65 64 20 69 6e   terms stored in
37e80 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 64 65   the segment ide
37e81 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 0a 2a  ntified by the.*
37e82 2a 20 63 75 72 72 65 6e 74 20 72 6f 77 20 74 68  * current row th
37e83 61 74 20 70 53 74 6d 74 20 69 73 20 70 6f 69 6e  at pStmt is poin
37e84 74 69 6e 67 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20  ting to. .**.** 
37e85 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  If successful, t
37e86 68 65 20 46 74 73 33 53 65 67 52 65 61 64 65 72  he Fts3SegReader
37e87 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
37e88 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  g to the first t
37e89 65 72 6d 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65  erm.** in the se
37e8a 67 6d 65 6e 74 20 61 6e 64 20 53 51 4c 49 54 45  gment and SQLITE
37e8b 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
37e8c 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53   Otherwise, an S
37e8d 51 4c 69 74 65 20 65 72 72 6f 72 0a 2a 2a 20 63  QLite error.** c
37e8e 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
37e8f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
37e90 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28  ts3SegReaderNew(
37e91 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
37e92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e93 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
37e94 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
37e95 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
37e96 74 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  tmt,            
37e97 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a  /* See above */.
37e98 20 20 69 6e 74 20 69 41 67 65 2c 20 20 20 20 20    int iAge,     
37e99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e9a 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20 22 61 67    /* Segment "ag
37e9b 65 22 2e 20 2a 2f 0a 20 20 46 74 73 33 53 65 67  e". */.  Fts3Seg
37e9c 52 65 61 64 65 72 20 2a 2a 70 70 52 65 61 64 65  Reader **ppReade
37e9d 72 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  r        /* OUT:
37e9e 20 41 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 53   Allocated Fts3S
37e9f 65 67 52 65 61 64 65 72 20 2a 2f 0a 29 7b 0a 20  egReader */.){. 
37ea0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46   return sqlite3F
37ea1 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28  ts3SegReaderNew(
37ea2 70 2c 20 69 41 67 65 2c 20 0a 20 20 20 20 20 20  p, iAge, .      
37ea3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
37ea4 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 29 2c 0a  nt64(pStmt, 1),.
37ea5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
37ea6 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74  lumn_int64(pStmt
37ea7 2c 20 32 29 2c 0a 20 20 20 20 20 20 73 71 6c 69  , 2),.      sqli
37ea8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
37ea9 28 70 53 74 6d 74 2c 20 33 29 2c 0a 20 20 20 20  (pStmt, 3),.    
37eaa 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
37eab 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 34 29 2c  _blob(pStmt, 4),
37eac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
37ead 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d  olumn_bytes(pStm
37eae 74 2c 20 34 29 2c 0a 20 20 20 20 20 20 70 70 52  t, 4),.      ppR
37eaf 65 61 64 65 72 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  eader.  );.}../*
37eb0 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
37eb1 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 65 64 20  entries pointed 
37eb2 74 6f 20 62 79 20 74 77 6f 20 46 74 73 33 53 65  to by two Fts3Se
37eb3 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75 72  gReader structur
37eb4 65 73 2e 20 0a 2a 2a 20 43 6f 6d 70 61 72 69 73  es. .** Comparis
37eb5 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
37eb6 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 45 4f 46  :.**.**   1) EOF
37eb7 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
37eb8 20 6e 6f 74 20 45 4f 46 2e 0a 2a 2a 0a 2a 2a 20   not EOF..**.** 
37eb9 20 20 32 29 20 54 68 65 20 63 75 72 72 65 6e 74    2) The current
37eba 20 74 65 72 6d 73 20 28 69 66 20 61 6e 79 29 20   terms (if any) 
37ebb 61 72 65 20 63 6f 6d 70 61 72 65 64 20 77 69 74  are compared wit
37ebc 68 20 6d 65 6d 63 6d 70 28 29 2e 20 49 66 20 6f  h memcmp(). If o
37ebd 6e 65 0a 2a 2a 20 20 20 20 20 20 74 65 72 6d 20  ne.**      term 
37ebe 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 61  is a prefix of a
37ebf 6e 6f 74 68 65 72 2c 20 74 68 65 20 6c 6f 6e 67  nother, the long
37ec0 65 72 20 74 65 72 6d 20 69 73 20 63 6f 6e 73 69  er term is consi
37ec1 64 65 72 65 64 20 74 68 65 0a 2a 2a 20 20 20 20  dered the.**    
37ec2 20 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20    larger..**.** 
37ec3 20 20 33 29 20 42 79 20 73 65 67 6d 65 6e 74 20    3) By segment 
37ec4 61 67 65 2e 20 41 6e 20 6f 6c 64 65 72 20 73 65  age. An older se
37ec5 67 6d 65 6e 74 20 69 73 20 63 6f 6e 73 69 64 65  gment is conside
37ec6 72 65 64 20 6c 61 72 67 65 72 2e 0a 2a 2f 0a 73  red larger..*/.s
37ec7 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
37ec8 67 52 65 61 64 65 72 43 6d 70 28 46 74 73 33 53  gReaderCmp(Fts3S
37ec9 65 67 52 65 61 64 65 72 20 2a 70 4c 68 73 2c 20  egReader *pLhs, 
37eca 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70  Fts3SegReader *p
37ecb 52 68 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Rhs){.  int rc;.
37ecc 20 20 69 66 28 20 70 4c 68 73 2d 3e 61 4e 6f 64    if( pLhs->aNod
37ecd 65 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65  e && pRhs->aNode
37ece 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 20   ){.    int rc2 
37ecf 3d 20 70 4c 68 73 2d 3e 6e 54 65 72 6d 20 2d 20  = pLhs->nTerm - 
37ed0 70 52 68 73 2d 3e 6e 54 65 72 6d 3b 0a 20 20 20  pRhs->nTerm;.   
37ed1 20 69 66 28 20 72 63 32 3c 30 20 29 7b 0a 20 20   if( rc2<0 ){.  
37ed2 20 20 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28      rc = memcmp(
37ed3 70 4c 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 52 68  pLhs->zTerm, pRh
37ed4 73 2d 3e 7a 54 65 72 6d 2c 20 70 4c 68 73 2d 3e  s->zTerm, pLhs->
37ed5 6e 54 65 72 6d 29 3b 0a 20 20 20 20 7d 65 6c 73  nTerm);.    }els
37ed6 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 65  e{.      rc = me
37ed7 6d 63 6d 70 28 70 4c 68 73 2d 3e 7a 54 65 72 6d  mcmp(pLhs->zTerm
37ed8 2c 20 70 52 68 73 2d 3e 7a 54 65 72 6d 2c 20 70  , pRhs->zTerm, p
37ed9 52 68 73 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20  Rhs->nTerm);.   
37eda 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 30   }.    if( rc==0
37edb 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
37edc 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  c2;.    }.  }els
37edd 65 7b 0a 20 20 20 20 72 63 20 3d 20 28 70 4c 68  e{.    rc = (pLh
37ede 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29 20 2d 20 28  s->aNode==0) - (
37edf 70 52 68 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29 3b  pRhs->aNode==0);
37ee0 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30  .  }.  if( rc==0
37ee1 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 52 68   ){.    rc = pRh
37ee2 73 2d 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e  s->iIdx - pLhs->
37ee3 69 49 64 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65  iIdx;.  }.  asse
37ee4 72 74 28 20 72 63 21 3d 30 20 29 3b 0a 20 20 72  rt( rc!=0 );.  r
37ee5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
37ee6 2a 2a 20 41 20 64 69 66 66 65 72 65 6e 74 20 63  ** A different c
37ee7 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
37ee8 6f 6e 20 66 6f 72 20 53 65 67 52 65 61 64 65 72  on for SegReader
37ee9 20 73 74 72 75 63 74 75 72 65 73 2e 20 49 6e 20   structures. In 
37eea 74 68 69 73 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c  this.** version,
37eeb 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
37eec 68 61 74 20 65 61 63 68 20 53 65 67 52 65 61 64  hat each SegRead
37eed 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  er points to an 
37eee 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 61 20 64 6f  entry in.** a do
37eef 63 6c 69 73 74 20 66 6f 72 20 69 64 65 6e 74 69  clist for identi
37ef0 63 61 6c 20 74 65 72 6d 73 2e 20 43 6f 6d 70 61  cal terms. Compa
37ef1 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 73  rison is made as
37ef2 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
37ef3 20 20 31 29 20 45 4f 46 20 28 65 6e 64 20 6f 66    1) EOF (end of
37ef4 20 64 6f 63 6c 69 73 74 20 69 6e 20 74 68 69 73   doclist in this
37ef5 20 63 61 73 65 29 20 69 73 20 67 72 65 61 74 65   case) is greate
37ef6 72 20 74 68 61 6e 20 6e 6f 74 20 45 4f 46 2e 0a  r than not EOF..
37ef7 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20 63 75  **.**   2) By cu
37ef8 72 72 65 6e 74 20 64 6f 63 69 64 2e 0a 2a 2a 0a  rrent docid..**.
37ef9 2a 2a 20 20 20 33 29 20 42 79 20 73 65 67 6d 65  **   3) By segme
37efa 6e 74 20 61 67 65 2e 20 41 6e 20 6f 6c 64 65 72  nt age. An older
37efb 20 73 65 67 6d 65 6e 74 20 69 73 20 63 6f 6e 73   segment is cons
37efc 69 64 65 72 65 64 20 6c 61 72 67 65 72 2e 0a 2a  idered larger..*
37efd 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37efe 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69 73  3SegReaderDoclis
37eff 74 43 6d 70 28 46 74 73 33 53 65 67 52 65 61 64  tCmp(Fts3SegRead
37f00 65 72 20 2a 70 4c 68 73 2c 20 46 74 73 33 53 65  er *pLhs, Fts3Se
37f01 67 52 65 61 64 65 72 20 2a 70 52 68 73 29 7b 0a  gReader *pRhs){.
37f02 20 20 69 6e 74 20 72 63 20 3d 20 28 70 4c 68 73    int rc = (pLhs
37f03 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 3d 3d 30  ->pOffsetList==0
37f04 29 2d 28 70 52 68 73 2d 3e 70 4f 66 66 73 65 74  )-(pRhs->pOffset
37f05 4c 69 73 74 3d 3d 30 29 3b 0a 20 20 69 66 28 20  List==0);.  if( 
37f06 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  rc==0 ){.    if(
37f07 20 70 4c 68 73 2d 3e 69 44 6f 63 69 64 3d 3d 70   pLhs->iDocid==p
37f08 52 68 73 2d 3e 69 44 6f 63 69 64 20 29 7b 0a 20  Rhs->iDocid ){. 
37f09 20 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d 3e       rc = pRhs->
37f0a 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49 64  iIdx - pLhs->iId
37f0b 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  x;.    }else{.  
37f0c 20 20 20 20 72 63 20 3d 20 28 70 4c 68 73 2d 3e      rc = (pLhs->
37f0d 69 44 6f 63 69 64 20 3e 20 70 52 68 73 2d 3e 69  iDocid > pRhs->i
37f0e 44 6f 63 69 64 29 20 3f 20 31 20 3a 20 2d 31 3b  Docid) ? 1 : -1;
37f0f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
37f10 65 72 74 28 20 70 4c 68 73 2d 3e 61 4e 6f 64 65  ert( pLhs->aNode
37f11 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65 20   && pRhs->aNode 
37f12 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
37f13 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
37f14 20 74 68 65 20 74 65 72 6d 20 74 68 61 74 20 74   the term that t
37f15 68 65 20 46 74 73 33 53 65 67 52 65 61 64 65 72  he Fts3SegReader
37f16 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61   object passed a
37f17 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
37f18 6d 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  ment.** points t
37f19 6f 20 77 69 74 68 20 74 68 65 20 74 65 72 6d 20  o with the term 
37f1a 73 70 65 63 69 66 69 65 64 20 62 79 20 61 72 67  specified by arg
37f1b 75 6d 65 6e 74 73 20 7a 54 65 72 6d 20 61 6e 64  uments zTerm and
37f1c 20 6e 54 65 72 6d 2e 20 0a 2a 2a 0a 2a 2a 20 49   nTerm. .**.** I
37f1d 66 20 74 68 65 20 70 53 65 67 20 69 74 65 72 61  f the pSeg itera
37f1e 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
37f1f 74 20 45 4f 46 2c 20 72 65 74 75 72 6e 20 30 2e  t EOF, return 0.
37f20 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
37f21 72 6e 0a 2a 2a 20 2d 76 65 20 69 66 20 74 68 65  rn.** -ve if the
37f22 20 70 53 65 67 20 74 65 72 6d 20 69 73 20 6c 65   pSeg term is le
37f23 73 73 20 74 68 61 6e 20 7a 54 65 72 6d 2f 6e 54  ss than zTerm/nT
37f24 65 72 6d 2c 20 30 20 69 66 20 74 68 65 20 74 77  erm, 0 if the tw
37f25 6f 20 74 65 72 6d 73 20 61 72 65 0a 2a 2a 20 65  o terms are.** e
37f26 71 75 61 6c 2c 20 6f 72 20 2b 76 65 20 69 66 20  qual, or +ve if 
37f27 74 68 65 20 70 53 65 67 20 74 65 72 6d 20 69 73  the pSeg term is
37f28 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 54   greater than zT
37f29 65 72 6d 2f 6e 54 65 72 6d 2e 0a 2a 2f 0a 73 74  erm/nTerm..*/.st
37f2a 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
37f2b 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 0a 20  ReaderTermCmp(. 
37f2c 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
37f2d 70 53 65 67 2c 20 20 20 20 20 20 20 20 20 20 20  pSeg,           
37f2e 20 2f 2a 20 53 65 67 6d 65 6e 74 20 72 65 61 64   /* Segment read
37f2f 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  er object */.  c
37f30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
37f31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
37f32 2a 20 54 65 72 6d 20 74 6f 20 63 6f 6d 70 61 72  * Term to compar
37f33 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  e to */.  int nT
37f34 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  erm             
37f35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
37f36 65 20 6f 66 20 74 65 72 6d 20 7a 54 65 72 6d 20  e of term zTerm 
37f37 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20  in bytes */.){. 
37f38 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20   int res = 0;.  
37f39 69 66 28 20 70 53 65 67 2d 3e 61 4e 6f 64 65 20  if( pSeg->aNode 
37f3a 29 7b 0a 20 20 20 20 69 66 28 20 70 53 65 67 2d  ){.    if( pSeg-
37f3b 3e 6e 54 65 72 6d 3e 6e 54 65 72 6d 20 29 7b 0a  >nTerm>nTerm ){.
37f3c 20 20 20 20 20 20 72 65 73 20 3d 20 6d 65 6d 63        res = memc
37f3d 6d 70 28 70 53 65 67 2d 3e 7a 54 65 72 6d 2c 20  mp(pSeg->zTerm, 
37f3e 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  zTerm, nTerm);. 
37f3f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
37f40 72 65 73 20 3d 20 6d 65 6d 63 6d 70 28 70 53 65  res = memcmp(pSe
37f41 67 2d 3e 7a 54 65 72 6d 2c 20 7a 54 65 72 6d 2c  g->zTerm, zTerm,
37f42 20 70 53 65 67 2d 3e 6e 54 65 72 6d 29 3b 0a 20   pSeg->nTerm);. 
37f43 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 73     }.    if( res
37f44 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 73  ==0 ){.      res
37f45 20 3d 20 70 53 65 67 2d 3e 6e 54 65 72 6d 2d 6e   = pSeg->nTerm-n
37f46 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Term;.    }.  }.
37f47 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
37f48 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
37f49 61 70 53 65 67 6d 65 6e 74 20 69 73 20 61 6e 20  apSegment is an 
37f4a 61 72 72 61 79 20 6f 66 20 6e 53 65 67 6d 65 6e  array of nSegmen
37f4b 74 20 65 6c 65 6d 65 6e 74 73 2e 20 49 74 20 69  t elements. It i
37f4c 73 20 6b 6e 6f 77 6e 20 74 68 61 74 0a 2a 2a 20  s known that.** 
37f4d 74 68 65 20 66 69 6e 61 6c 20 28 6e 53 65 67 6d  the final (nSegm
37f4e 65 6e 74 2d 6e 53 75 73 70 65 63 74 29 20 6d 65  ent-nSuspect) me
37f4f 6d 62 65 72 73 20 61 72 65 20 61 6c 72 65 61 64  mbers are alread
37f50 79 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  y in sorted orde
37f51 72 0a 2a 2a 20 28 61 63 63 6f 72 64 69 6e 67 20  r.** (according 
37f52 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
37f53 6e 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69  n function provi
37f54 64 65 64 29 2e 20 54 68 69 73 20 66 75 6e 63 74  ded). This funct
37f55 69 6f 6e 20 73 68 75 66 66 6c 65 73 0a 2a 2a 20  ion shuffles.** 
37f56 74 68 65 20 61 72 72 61 79 20 61 72 6f 75 6e 64  the array around
37f57 20 75 6e 74 69 6c 20 61 6c 6c 20 65 6e 74 72 69   until all entri
37f58 65 73 20 61 72 65 20 69 6e 20 73 6f 72 74 65 64  es are in sorted
37f59 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
37f5a 63 20 76 6f 69 64 20 66 74 73 33 53 65 67 52 65  c void fts3SegRe
37f5b 61 64 65 72 53 6f 72 74 28 0a 20 20 46 74 73 33  aderSort(.  Fts3
37f5c 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70 53 65  SegReader **apSe
37f5d 67 6d 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  gment,          
37f5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
37f5f 72 61 79 20 74 6f 20 73 6f 72 74 20 65 6e 74 72  ray to sort entr
37f60 69 65 73 20 6f 66 20 2a 2f 0a 20 20 69 6e 74 20  ies of */.  int 
37f61 6e 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20 20  nSegment,       
37f62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f63 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
37f64 7a 65 20 6f 66 20 61 70 53 65 67 6d 65 6e 74 20  ze of apSegment 
37f65 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  array */.  int n
37f66 53 75 73 70 65 63 74 2c 20 20 20 20 20 20 20 20  Suspect,        
37f67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f68 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73            /* Uns
37f69 6f 72 74 65 64 20 65 6e 74 72 79 20 63 6f 75 6e  orted entry coun
37f6a 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d  t */.  int (*xCm
37f6b 70 29 28 46 74 73 33 53 65 67 52 65 61 64 65 72  p)(Fts3SegReader
37f6c 20 2a 2c 20 46 74 73 33 53 65 67 52 65 61 64 65   *, Fts3SegReade
37f6d 72 20 2a 29 20 20 2f 2a 20 43 6f 6d 70 61 72 69  r *)  /* Compari
37f6e 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  son function */.
37f6f 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
37f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f71 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
37f72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20   variable */..  
37f73 61 73 73 65 72 74 28 20 6e 53 75 73 70 65 63 74  assert( nSuspect
37f74 3c 3d 6e 53 65 67 6d 65 6e 74 20 29 3b 0a 0a 20  <=nSegment );.. 
37f75 20 69 66 28 20 6e 53 75 73 70 65 63 74 3d 3d 6e   if( nSuspect==n
37f76 53 65 67 6d 65 6e 74 20 29 20 6e 53 75 73 70 65  Segment ) nSuspe
37f77 63 74 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d 6e 53  ct--;.  for(i=nS
37f78 75 73 70 65 63 74 2d 31 3b 20 69 3e 3d 30 3b 20  uspect-1; i>=0; 
37f79 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b  i--){.    int j;
37f7a 0a 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20 6a 3c  .    for(j=i; j<
37f7b 28 6e 53 65 67 6d 65 6e 74 2d 31 29 3b 20 6a 2b  (nSegment-1); j+
37f7c 2b 29 7b 0a 20 20 20 20 20 20 46 74 73 33 53 65  +){.      Fts3Se
37f7d 67 52 65 61 64 65 72 20 2a 70 54 6d 70 3b 0a 20  gReader *pTmp;. 
37f7e 20 20 20 20 20 69 66 28 20 78 43 6d 70 28 61 70       if( xCmp(ap
37f7f 53 65 67 6d 65 6e 74 5b 6a 5d 2c 20 61 70 53 65  Segment[j], apSe
37f80 67 6d 65 6e 74 5b 6a 2b 31 5d 29 3c 30 20 29 20  gment[j+1])<0 ) 
37f81 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6d  break;.      pTm
37f82 70 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 6a 2b  p = apSegment[j+
37f83 31 5d 3b 0a 20 20 20 20 20 20 61 70 53 65 67 6d  1];.      apSegm
37f84 65 6e 74 5b 6a 2b 31 5d 20 3d 20 61 70 53 65 67  ent[j+1] = apSeg
37f85 6d 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 20 20 61  ment[j];.      a
37f86 70 53 65 67 6d 65 6e 74 5b 6a 5d 20 3d 20 70 54  pSegment[j] = pT
37f87 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23  mp;.    }.  }..#
37f88 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
37f89 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
37f8a 65 20 6c 69 73 74 20 72 65 61 6c 6c 79 20 69 73  e list really is
37f8b 20 73 6f 72 74 65 64 20 6e 6f 77 2e 20 2a 2f 0a   sorted now. */.
37f8c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 28 6e 53    for(i=0; i<(nS
37f8d 75 73 70 65 63 74 2d 31 29 3b 20 69 2b 2b 29 7b  uspect-1); i++){
37f8e 0a 20 20 20 20 61 73 73 65 72 74 28 20 78 43 6d  .    assert( xCm
37f8f 70 28 61 70 53 65 67 6d 65 6e 74 5b 69 5d 2c 20  p(apSegment[i], 
37f90 61 70 53 65 67 6d 65 6e 74 5b 69 2b 31 5d 29 3c  apSegment[i+1])<
37f91 30 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  0 );.  }.#endif.
37f92 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e 73 65 72 74  }../* .** Insert
37f93 20 61 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74   a record into t
37f94 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  he %_segments ta
37f95 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
37f96 6e 74 20 66 74 73 33 57 72 69 74 65 53 65 67 6d  nt fts3WriteSegm
37f97 65 6e 74 28 0a 20 20 46 74 73 33 54 61 62 6c 65  ent(.  Fts3Table
37f98 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
37f99 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
37f9a 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
37f9b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
37f9c 34 20 69 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20  4 iBlock,       
37f9d 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20      /* Block id 
37f9e 66 6f 72 20 6e 65 77 20 62 6c 6f 63 6b 20 2a 2f  for new block */
37f9f 0a 20 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20  .  char *z,     
37fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fa1 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
37fa2 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
37fa3 6e 67 20 62 6c 6f 63 6b 20 64 61 74 61 20 2a 2f  ng block data */
37fa4 0a 20 20 69 6e 74 20 6e 20 20 20 20 20 20 20 20  .  int n        
37fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fa6 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
37fa7 66 66 65 72 20 7a 20 69 6e 20 62 79 74 65 73 20  ffer z in bytes 
37fa8 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
37fa9 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69  stmt *pStmt;.  i
37faa 6e 74 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53  nt rc = fts3SqlS
37fab 74 6d 74 28 70 2c 20 53 51 4c 5f 49 4e 53 45 52  tmt(p, SQL_INSER
37fac 54 5f 53 45 47 4d 45 4e 54 53 2c 20 26 70 53 74  T_SEGMENTS, &pSt
37fad 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
37fae 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37faf 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
37fb0 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 2c 20  int64(pStmt, 1, 
37fb1 69 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20  iBlock);.    rc 
37fb2 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
37fb3 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 7a 2c  lob(pStmt, 2, z,
37fb4 20 6e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   n, SQLITE_STATI
37fb5 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  C);.    if( rc==
37fb6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
37fb7 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
37fb8 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 72 63  pStmt);.      rc
37fb9 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
37fba 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d 0a 20  (pStmt);.    }. 
37fbb 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
37fbc 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e 73 65 72 74  }../* .** Insert
37fbd 20 61 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74   a record into t
37fbe 68 65 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c  he %_segdir tabl
37fbf 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
37fc0 20 66 74 73 33 57 72 69 74 65 53 65 67 64 69 72   fts3WriteSegdir
37fc1 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
37fc2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37fc3 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
37fc4 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
37fc5 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20   int iLevel,    
37fc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fc7 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 6c   /* Value for "l
37fc8 65 76 65 6c 22 20 66 69 65 6c 64 20 2a 2f 0a 20  evel" field */. 
37fc9 20 69 6e 74 20 69 49 64 78 2c 20 20 20 20 20 20   int iIdx,      
37fca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fcb 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 69   /* Value for "i
37fcc 64 78 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20 73  dx" field */.  s
37fcd 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
37fce 61 72 74 42 6c 6f 63 6b 2c 20 20 20 20 20 20 2f  artBlock,      /
37fcf 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 73 74 61  * Value for "sta
37fd0 72 74 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20  rt_block" field 
37fd1 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37fd2 36 34 20 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b  64 iLeafEndBlock
37fd3 2c 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  ,    /* Value fo
37fd4 72 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  r "leaves_end_bl
37fd5 6f 63 6b 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20  ock" field */.  
37fd6 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45  sqlite3_int64 iE
37fd7 6e 64 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  ndBlock,        
37fd8 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 65 6e  /* Value for "en
37fd9 64 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20 2a  d_block" field *
37fda 2f 0a 20 20 63 68 61 72 20 2a 7a 52 6f 6f 74 2c  /.  char *zRoot,
37fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fdc 20 20 20 20 2f 2a 20 42 6c 6f 62 20 76 61 6c 75      /* Blob valu
37fdd 65 20 66 6f 72 20 22 72 6f 6f 74 22 20 66 69 65  e for "root" fie
37fde 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f  ld */.  int nRoo
37fdf 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
37fe0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
37fe1 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75  r of bytes in bu
37fe2 66 66 65 72 20 7a 52 6f 6f 74 20 2a 2f 0a 29 7b  ffer zRoot */.){
37fe3 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
37fe4 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
37fe5 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
37fe6 2c 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  , SQL_INSERT_SEG
37fe7 44 49 52 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  DIR, &pStmt, 0);
37fe8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
37fe9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
37fea 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74  te3_bind_int(pSt
37feb 6d 74 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  mt, 1, iLevel);.
37fec 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
37fed 5f 69 6e 74 28 70 53 74 6d 74 2c 20 32 2c 20 69  _int(pStmt, 2, i
37fee 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Idx);.    sqlite
37fef 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74  3_bind_int64(pSt
37ff0 6d 74 2c 20 33 2c 20 69 53 74 61 72 74 42 6c 6f  mt, 3, iStartBlo
37ff1 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ck);.    sqlite3
37ff2 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d  _bind_int64(pStm
37ff3 74 2c 20 34 2c 20 69 4c 65 61 66 45 6e 64 42 6c  t, 4, iLeafEndBl
37ff4 6f 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ock);.    sqlite
37ff5 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74  3_bind_int64(pSt
37ff6 6d 74 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b  mt, 5, iEndBlock
37ff7 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
37ff8 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
37ff9 74 6d 74 2c 20 36 2c 20 7a 52 6f 6f 74 2c 20 6e  tmt, 6, zRoot, n
37ffa 52 6f 6f 74 2c 20 53 51 4c 49 54 45 5f 53 54 41  Root, SQLITE_STA
37ffb 54 49 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63  TIC);.    if( rc
37ffc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37ffd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
37ffe 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20  p(pStmt);.      
37fff 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
38000 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d  et(pStmt);.    }
38001 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
38002 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
38003 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  n the size of th
38004 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20  e common prefix 
38005 28 69 66 20 61 6e 79 29 20 73 68 61 72 65 64 20  (if any) shared 
38006 62 79 20 7a 50 72 65 76 20 61 6e 64 0a 2a 2a 20  by zPrev and.** 
38007 7a 4e 65 78 74 2c 20 69 6e 20 62 79 74 65 73 2e  zNext, in bytes.
38008 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a   For example, .*
38009 2a 0a 2a 2a 20 20 20 66 74 73 33 50 72 65 66 69  *.**   fts3Prefi
3800a 78 43 6f 6d 70 72 65 73 73 28 22 61 62 63 22 2c  xCompress("abc",
3800b 20 33 2c 20 22 61 62 63 64 65 66 22 2c 20 36 29   3, "abcdef", 6)
3800c 20 20 20 2f 2f 20 72 65 74 75 72 6e 73 20 33 0a     // returns 3.
3800d 2a 2a 20 20 20 66 74 73 33 50 72 65 66 69 78 43  **   fts3PrefixC
3800e 6f 6d 70 72 65 73 73 28 22 61 62 58 22 2c 20 33  ompress("abX", 3
3800f 2c 20 22 61 62 63 64 65 66 22 2c 20 36 29 20 20  , "abcdef", 6)  
38010 20 2f 2f 20 72 65 74 75 72 6e 73 20 32 0a 2a 2a   // returns 2.**
38011 20 20 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d     fts3PrefixCom
38012 70 72 65 73 73 28 22 61 62 58 22 2c 20 33 2c 20  press("abX", 3, 
38013 22 58 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f  "Xbcdef", 6)   /
38014 2f 20 72 65 74 75 72 6e 73 20 30 0a 2a 2f 0a 73  / returns 0.*/.s
38015 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 72  tatic int fts3Pr
38016 65 66 69 78 43 6f 6d 70 72 65 73 73 28 0a 20 20  efixCompress(.  
38017 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 65  const char *zPre
38018 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v,              
38019 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69  /* Buffer contai
3801a 6e 69 6e 67 20 70 72 65 76 69 6f 75 73 20 74 65  ning previous te
3801b 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 65  rm */.  int nPre
3801c 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v,              
3801d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
3801e 6f 66 20 62 75 66 66 65 72 20 7a 50 72 65 76 20  of buffer zPrev 
3801f 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f  in bytes */.  co
38020 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 78 74 2c  nst char *zNext,
38021 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38022 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   Buffer containi
38023 6e 67 20 6e 65 78 74 20 74 65 72 6d 20 2a 2f 0a  ng next term */.
38024 20 20 69 6e 74 20 6e 4e 65 78 74 20 20 20 20 20    int nNext     
38025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38026 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
38027 66 65 72 20 7a 4e 65 78 74 20 69 6e 20 62 79 74  fer zNext in byt
38028 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  es */.){.  int n
38029 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
3802a 45 54 45 52 28 6e 4e 65 78 74 29 3b 0a 20 20 66  ETER(nNext);.  f
3802b 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 50 72 65 76 20  or(n=0; n<nPrev 
3802c 26 26 20 7a 50 72 65 76 5b 6e 5d 3d 3d 7a 4e 65  && zPrev[n]==zNe
3802d 78 74 5b 6e 5d 3b 20 6e 2b 2b 29 3b 0a 20 20 72  xt[n]; n++);.  r
3802e 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
3802f 2a 20 41 64 64 20 74 65 72 6d 20 7a 54 65 72 6d  * Add term zTerm
38030 20 74 6f 20 74 68 65 20 53 65 67 6d 65 6e 74 4e   to the SegmentN
38031 6f 64 65 2e 20 49 74 20 69 73 20 67 75 61 72 61  ode. It is guara
38032 6e 74 65 65 64 20 74 68 61 74 20 7a 54 65 72 6d  nteed that zTerm
38033 20 69 73 20 6c 61 72 67 65 72 0a 2a 2a 20 28 61   is larger.** (a
38034 63 63 6f 72 64 69 6e 67 20 74 6f 20 6d 65 6d 63  ccording to memc
38035 6d 70 29 20 74 68 61 6e 20 74 68 65 20 70 72 65  mp) than the pre
38036 76 69 6f 75 73 20 74 65 72 6d 2e 0a 2a 2f 0a 73  vious term..*/.s
38037 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f  tatic int fts3No
38038 64 65 41 64 64 54 65 72 6d 28 0a 20 20 46 74 73  deAddTerm(.  Fts
38039 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
3803a 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
3803b 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
3803c 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64   */.  SegmentNod
3803d 65 20 2a 2a 70 70 54 72 65 65 2c 20 20 20 20 20  e **ppTree,     
3803e 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
3803f 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 68 61 6e   SegmentNode han
38040 64 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20 69 73  dle */ .  int is
38041 43 6f 70 79 54 65 72 6d 2c 20 20 20 20 20 20 20  CopyTerm,       
38042 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
38043 65 20 69 66 20 7a 54 65 72 6d 2f 6e 54 65 72 6d  e if zTerm/nTerm
38044 20 69 73 20 74 72 61 6e 73 69 65 6e 74 20 2a 2f   is transient */
38045 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
38046 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20  Term,           
38047 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
38048 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
38049 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  ng term */.  int
3804a 20 6e 54 65 72 6d 20 20 20 20 20 20 20 20 20 20   nTerm          
3804b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3804c 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 69 6e 20  Size of term in 
3804d 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 65  bytes */.){.  Se
3804e 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65 65  gmentNode *pTree
3804f 20 3d 20 2a 70 70 54 72 65 65 3b 0a 20 20 69 6e   = *ppTree;.  in
38050 74 20 72 63 3b 0a 20 20 53 65 67 6d 65 6e 74 4e  t rc;.  SegmentN
38051 6f 64 65 20 2a 70 4e 65 77 3b 0a 0a 20 20 2f 2a  ode *pNew;..  /*
38052 20 46 69 72 73 74 20 74 72 79 20 74 6f 20 61 70   First try to ap
38053 70 65 6e 64 20 74 68 65 20 74 65 72 6d 20 74 6f  pend the term to
38054 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64   the current nod
38055 65 2e 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20  e. Return early 
38056 69 66 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73  if .  ** this is
38057 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a   possible..  */.
38058 20 20 69 66 28 20 70 54 72 65 65 20 29 7b 0a 20    if( pTree ){. 
38059 20 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 70     int nData = p
3805a 54 72 65 65 2d 3e 6e 44 61 74 61 3b 20 20 20 20  Tree->nData;    
3805b 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65   /* Current size
3805c 20 6f 66 20 6e 6f 64 65 20 69 6e 20 62 79 74 65   of node in byte
3805d 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 52 65  s */.    int nRe
3805e 71 20 3d 20 6e 44 61 74 61 3b 20 20 20 20 20 20  q = nData;      
3805f 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72         /* Requir
38060 65 64 20 73 70 61 63 65 20 61 66 74 65 72 20 61  ed space after a
38061 64 64 69 6e 67 20 7a 54 65 72 6d 20 2a 2f 0a 20  dding zTerm */. 
38062 20 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20     int nPrefix; 
38063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38064 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
38065 74 65 73 20 6f 66 20 70 72 65 66 69 78 20 63 6f  tes of prefix co
38066 6d 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  mpression */.   
38067 20 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20   int nSuffix;   
38068 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38069 2a 20 53 75 66 66 69 78 20 6c 65 6e 67 74 68 20  * Suffix length 
3806a 2a 2f 0a 0a 20 20 20 20 6e 50 72 65 66 69 78 20  */..    nPrefix 
3806b 3d 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70  = fts3PrefixComp
3806c 72 65 73 73 28 70 54 72 65 65 2d 3e 7a 54 65 72  ress(pTree->zTer
3806d 6d 2c 20 70 54 72 65 65 2d 3e 6e 54 65 72 6d 2c  m, pTree->nTerm,
3806e 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   zTerm, nTerm);.
3806f 20 20 20 20 6e 53 75 66 66 69 78 20 3d 20 6e 54      nSuffix = nT
38070 65 72 6d 2d 6e 50 72 65 66 69 78 3b 0a 0a 20 20  erm-nPrefix;..  
38071 20 20 6e 52 65 71 20 2b 3d 20 73 71 6c 69 74 65    nReq += sqlite
38072 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e  3Fts3VarintLen(n
38073 50 72 65 66 69 78 29 2b 73 71 6c 69 74 65 33 46  Prefix)+sqlite3F
38074 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 53 75  ts3VarintLen(nSu
38075 66 66 69 78 29 2b 6e 53 75 66 66 69 78 3b 0a 20  ffix)+nSuffix;. 
38076 20 20 20 69 66 28 20 6e 52 65 71 3c 3d 70 2d 3e     if( nReq<=p->
38077 6e 4e 6f 64 65 53 69 7a 65 20 7c 7c 20 21 70 54  nNodeSize || !pT
38078 72 65 65 2d 3e 7a 54 65 72 6d 20 29 7b 0a 0a 20  ree->zTerm ){.. 
38079 20 20 20 20 20 69 66 28 20 6e 52 65 71 3e 70 2d       if( nReq>p-
3807a 3e 6e 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20  >nNodeSize ){.  
3807b 20 20 20 20 20 20 2f 2a 20 41 6e 20 75 6e 75 73        /* An unus
3807c 75 61 6c 20 63 61 73 65 3a 20 74 68 69 73 20 69  ual case: this i
3807d 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  s the first term
3807e 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
3807f 74 68 65 20 6e 6f 64 65 0a 20 20 20 20 20 20 20  the node.       
38080 20 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74   ** and the stat
38081 69 63 20 6e 6f 64 65 20 62 75 66 66 65 72 20 28  ic node buffer (
38082 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20 62 79 74  p->nNodeSize byt
38083 65 73 29 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  es) is not large
38084 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 6e 6f 75  .        ** enou
38085 67 68 2e 20 55 73 65 20 61 20 73 65 70 61 72 61  gh. Use a separa
38086 74 65 6c 79 20 6d 61 6c 6c 6f 63 65 64 20 62 75  tely malloced bu
38087 66 66 65 72 20 69 6e 73 74 65 61 64 20 54 68 69  ffer instead Thi
38088 73 20 77 61 73 74 65 73 0a 20 20 20 20 20 20 20  s wastes.       
38089 20 2a 2a 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65   ** p->nNodeSize
3808a 20 62 79 74 65 73 2c 20 62 75 74 20 73 69 6e 63   bytes, but sinc
3808b 65 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20  e this scenario 
3808c 6f 6e 6c 79 20 63 6f 6d 65 73 20 61 62 6f 75 74  only comes about
3808d 20 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   when.        **
3808e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
3808f 6e 74 61 69 6e 20 74 77 6f 20 74 65 72 6d 73 20  ntain two terms 
38090 74 68 61 74 20 73 68 61 72 65 20 61 20 70 72 65  that share a pre
38091 66 69 78 20 6f 66 20 61 6c 6d 6f 73 74 20 32 4b  fix of almost 2K
38092 42 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  B, .        ** t
38093 68 69 73 20 69 73 20 6e 6f 74 20 65 78 70 65 63  his is not expec
38094 74 65 64 20 74 6f 20 62 65 20 61 20 73 65 72 69  ted to be a seri
38095 6f 75 73 20 70 72 6f 62 6c 65 6d 2e 20 0a 20 20  ous problem. .  
38096 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
38097 20 61 73 73 65 72 74 28 20 70 54 72 65 65 2d 3e   assert( pTree->
38098 61 44 61 74 61 3d 3d 28 63 68 61 72 20 2a 29 26  aData==(char *)&
38099 70 54 72 65 65 5b 31 5d 20 29 3b 0a 20 20 20 20  pTree[1] );.    
3809a 20 20 20 20 70 54 72 65 65 2d 3e 61 44 61 74 61      pTree->aData
3809b 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
3809c 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 71 29 3b  e3_malloc(nReq);
3809d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 54  .        if( !pT
3809e 72 65 65 2d 3e 61 44 61 74 61 20 29 7b 0a 20 20  ree->aData ){.  
3809f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
380a0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
380a1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
380a2 20 20 20 20 20 20 69 66 28 20 70 54 72 65 65 2d        if( pTree-
380a3 3e 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  >zTerm ){.      
380a4 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f    /* There is no
380a5 20 70 72 65 66 69 78 2d 6c 65 6e 67 74 68 20 66   prefix-length f
380a6 69 65 6c 64 20 66 6f 72 20 66 69 72 73 74 20 74  ield for first t
380a7 65 72 6d 20 69 6e 20 61 20 6e 6f 64 65 20 2a 2f  erm in a node */
380a8 0a 20 20 20 20 20 20 20 20 6e 44 61 74 61 20 2b  .        nData +
380a9 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
380aa 56 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61  Varint(&pTree->a
380ab 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 50 72  Data[nData], nPr
380ac 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  efix);.      }..
380ad 20 20 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 73        nData += s
380ae 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
380af 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44 61 74  int(&pTree->aDat
380b0 61 5b 6e 44 61 74 61 5d 2c 20 6e 53 75 66 66 69  a[nData], nSuffi
380b1 78 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  x);.      memcpy
380b2 28 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b 6e  (&pTree->aData[n
380b3 44 61 74 61 5d 2c 20 26 7a 54 65 72 6d 5b 6e 50  Data], &zTerm[nP
380b4 72 65 66 69 78 5d 2c 20 6e 53 75 66 66 69 78 29  refix], nSuffix)
380b5 3b 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e  ;.      pTree->n
380b6 44 61 74 61 20 3d 20 6e 44 61 74 61 20 2b 20 6e  Data = nData + n
380b7 53 75 66 66 69 78 3b 0a 20 20 20 20 20 20 70 54  Suffix;.      pT
380b8 72 65 65 2d 3e 6e 45 6e 74 72 79 2b 2b 3b 0a 0a  ree->nEntry++;..
380b9 20 20 20 20 20 20 69 66 28 20 69 73 43 6f 70 79        if( isCopy
380ba 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
380bb 69 66 28 20 70 54 72 65 65 2d 3e 6e 4d 61 6c 6c  if( pTree->nMall
380bc 6f 63 3c 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20  oc<nTerm ){.    
380bd 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77        char *zNew
380be 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
380bf 6f 63 28 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f  oc(pTree->zMallo
380c0 63 2c 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20  c, nTerm*2);.   
380c1 20 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77         if( !zNew
380c2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
380c3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
380c4 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  MEM;.          }
380c5 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 65 65  .          pTree
380c6 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72  ->nMalloc = nTer
380c7 6d 2a 32 3b 0a 20 20 20 20 20 20 20 20 20 20 70  m*2;.          p
380c8 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  Tree->zMalloc = 
380c9 7a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a  zNew;.        }.
380ca 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a          pTree->z
380cb 54 65 72 6d 20 3d 20 70 54 72 65 65 2d 3e 7a 4d  Term = pTree->zM
380cc 61 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 6d  alloc;.        m
380cd 65 6d 63 70 79 28 70 54 72 65 65 2d 3e 7a 54 65  emcpy(pTree->zTe
380ce 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, zTerm, nTerm
380cf 29 3b 0a 20 20 20 20 20 20 20 20 70 54 72 65 65  );.        pTree
380d0 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d 3b  ->nTerm = nTerm;
380d1 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
380d2 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a 54 65        pTree->zTe
380d3 72 6d 20 3d 20 28 63 68 61 72 20 2a 29 7a 54 65  rm = (char *)zTe
380d4 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 72 65  rm;.        pTre
380d5 65 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d  e->nTerm = nTerm
380d6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
380d7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
380d8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
380d9 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f  * If control flo
380da 77 73 20 74 6f 20 68 65 72 65 2c 20 69 74 20 77  ws to here, it w
380db 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  as not possible 
380dc 74 6f 20 61 70 70 65 6e 64 20 7a 54 65 72 6d 20  to append zTerm 
380dd 74 6f 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  to the.  ** curr
380de 65 6e 74 20 6e 6f 64 65 2e 20 43 72 65 61 74 65  ent node. Create
380df 20 61 20 6e 65 77 20 6e 6f 64 65 20 28 61 20 72   a new node (a r
380e0 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20  ight-sibling of 
380e1 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65  the current node
380e2 29 2e 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  )..  ** If this 
380e3 69 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 64  is the first nod
380e4 65 20 69 6e 20 74 68 65 20 74 72 65 65 2c 20 74  e in the tree, t
380e5 68 65 20 74 65 72 6d 20 69 73 20 61 64 64 65 64  he term is added
380e6 20 74 6f 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a   to it..  **.  *
380e7 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  * Otherwise, the
380e8 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 64 64   term is not add
380e9 65 64 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 6f  ed to the new no
380ea 64 65 2c 20 69 74 20 69 73 20 6c 65 66 74 20 65  de, it is left e
380eb 6d 70 74 79 20 66 6f 72 0a 20 20 2a 2a 20 6e 6f  mpty for.  ** no
380ec 77 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  w. Instead, the 
380ed 74 65 72 6d 20 69 73 20 69 6e 73 65 72 74 65 64  term is inserted
380ee 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
380ef 20 6f 66 20 70 54 72 65 65 2e 20 49 66 20 70 54   of pTree. If pT
380f0 72 65 65 20 0a 20 20 2a 2a 20 68 61 73 20 6e 6f  ree .  ** has no
380f1 20 70 61 72 65 6e 74 2c 20 6f 6e 65 20 69 73 20   parent, one is 
380f2 63 72 65 61 74 65 64 20 68 65 72 65 2e 0a 20 20  created here..  
380f3 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 53 65 67  */.  pNew = (Seg
380f4 6d 65 6e 74 4e 6f 64 65 20 2a 29 73 71 6c 69 74  mentNode *)sqlit
380f5 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
380f6 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29 20 2b 20  (SegmentNode) + 
380f7 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a 20  p->nNodeSize);. 
380f8 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20   if( !pNew ){.  
380f9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
380fa 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
380fb 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a  set(pNew, 0, siz
380fc 65 6f 66 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29  eof(SegmentNode)
380fd 29 3b 0a 20 20 70 4e 65 77 2d 3e 6e 44 61 74 61  );.  pNew->nData
380fe 20 3d 20 31 20 2b 20 46 54 53 33 5f 56 41 52 49   = 1 + FTS3_VARI
380ff 4e 54 5f 4d 41 58 3b 0a 20 20 70 4e 65 77 2d 3e  NT_MAX;.  pNew->
38100 61 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  aData = (char *)
38101 26 70 4e 65 77 5b 31 5d 3b 0a 0a 20 20 69 66 28  &pNew[1];..  if(
38102 20 70 54 72 65 65 20 29 7b 0a 20 20 20 20 53 65   pTree ){.    Se
38103 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 50 61 72 65  gmentNode *pPare
38104 6e 74 20 3d 20 70 54 72 65 65 2d 3e 70 50 61 72  nt = pTree->pPar
38105 65 6e 74 3b 0a 20 20 20 20 72 63 20 3d 20 66 74  ent;.    rc = ft
38106 73 33 4e 6f 64 65 41 64 64 54 65 72 6d 28 70 2c  s3NodeAddTerm(p,
38107 20 26 70 50 61 72 65 6e 74 2c 20 69 73 43 6f 70   &pParent, isCop
38108 79 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54  yTerm, zTerm, nT
38109 65 72 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54  erm);.    if( pT
3810a 72 65 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20  ree->pParent==0 
3810b 29 7b 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e  ){.      pTree->
3810c 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
3810d 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 72  t;.    }.    pTr
3810e 65 65 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65  ee->pRight = pNe
3810f 77 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65  w;.    pNew->pLe
38110 66 74 6d 6f 73 74 20 3d 20 70 54 72 65 65 2d 3e  ftmost = pTree->
38111 70 4c 65 66 74 6d 6f 73 74 3b 0a 20 20 20 20 70  pLeftmost;.    p
38112 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  New->pParent = p
38113 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 4e 65 77  Parent;.    pNew
38114 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 54 72 65  ->zMalloc = pTre
38115 65 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20  e->zMalloc;.    
38116 70 4e 65 77 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20  pNew->nMalloc = 
38117 70 54 72 65 65 2d 3e 6e 4d 61 6c 6c 6f 63 3b 0a  pTree->nMalloc;.
38118 20 20 20 20 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c      pTree->zMall
38119 6f 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  oc = 0;.  }else{
3811a 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74  .    pNew->pLeft
3811b 6d 6f 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  most = pNew;.   
3811c 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64   rc = fts3NodeAd
3811d 64 54 65 72 6d 28 70 2c 20 26 70 4e 65 77 2c 20  dTerm(p, &pNew, 
3811e 69 73 43 6f 70 79 54 65 72 6d 2c 20 7a 54 65 72  isCopyTerm, zTer
3811f 6d 2c 20 6e 54 65 72 6d 29 3b 20 0a 20 20 7d 0a  m, nTerm); .  }.
38120 0a 20 20 2a 70 70 54 72 65 65 20 3d 20 70 4e 65  .  *ppTree = pNe
38121 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  w;.  return rc;.
38122 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  }../*.** Helper 
38123 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 66 74 73  function for fts
38124 33 4e 6f 64 65 57 72 69 74 65 28 29 2e 0a 2a 2f  3NodeWrite()..*/
38125 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
38126 54 72 65 65 46 69 6e 69 73 68 4e 6f 64 65 28 0a  TreeFinishNode(.
38127 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
38128 54 72 65 65 2c 20 0a 20 20 69 6e 74 20 69 48 65  Tree, .  int iHe
38129 69 67 68 74 2c 20 0a 20 20 73 71 6c 69 74 65 33  ight, .  sqlite3
3812a 5f 69 6e 74 36 34 20 69 4c 65 66 74 43 68 69 6c  _int64 iLeftChil
3812b 64 0a 29 7b 0a 20 20 69 6e 74 20 6e 53 74 61 72  d.){.  int nStar
3812c 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  t;.  assert( iHe
3812d 69 67 68 74 3e 3d 31 20 26 26 20 69 48 65 69 67  ight>=1 && iHeig
3812e 68 74 3c 31 32 38 20 29 3b 0a 20 20 6e 53 74 61  ht<128 );.  nSta
3812f 72 74 20 3d 20 46 54 53 33 5f 56 41 52 49 4e 54  rt = FTS3_VARINT
38130 5f 4d 41 58 20 2d 20 73 71 6c 69 74 65 33 46 74  _MAX - sqlite3Ft
38131 73 33 56 61 72 69 6e 74 4c 65 6e 28 69 4c 65 66  s3VarintLen(iLef
38132 74 43 68 69 6c 64 29 3b 0a 20 20 70 54 72 65 65  tChild);.  pTree
38133 2d 3e 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 20  ->aData[nStart] 
38134 3d 20 28 63 68 61 72 29 69 48 65 69 67 68 74 3b  = (char)iHeight;
38135 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  .  sqlite3Fts3Pu
38136 74 56 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e  tVarint(&pTree->
38137 61 44 61 74 61 5b 6e 53 74 61 72 74 2b 31 5d 2c  aData[nStart+1],
38138 20 69 4c 65 66 74 43 68 69 6c 64 29 3b 0a 20 20   iLeftChild);.  
38139 72 65 74 75 72 6e 20 6e 53 74 61 72 74 3b 0a 7d  return nStart;.}
3813a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ../*.** Write th
3813b 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65  e buffer for the
3813c 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 20 70 54   segment node pT
3813d 72 65 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ree and all of i
3813e 74 73 20 70 65 65 72 73 20 74 6f 20 74 68 65 0a  ts peers to the.
3813f 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ** database. The
38140 6e 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  n call this func
38141 74 69 6f 6e 20 72 65 63 75 72 73 69 76 65 6c 79  tion recursively
38142 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 70 61   to write the pa
38143 72 65 6e 74 20 6f 66 20 0a 2a 2a 20 70 54 72 65  rent of .** pTre
38144 65 20 61 6e 64 20 69 74 73 20 70 65 65 72 73 20  e and its peers 
38145 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
38146 20 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20   .**.** Except, 
38147 69 66 20 70 54 72 65 65 20 69 73 20 61 20 72 6f  if pTree is a ro
38148 6f 74 20 6e 6f 64 65 2c 20 64 6f 20 6e 6f 74 20  ot node, do not 
38149 77 72 69 74 65 20 69 74 20 74 6f 20 74 68 65 20  write it to the 
3814a 64 61 74 61 62 61 73 65 2e 20 49 6e 73 74 65 61  database. Instea
3814b 64 2c 0a 2a 2a 20 73 65 74 20 6f 75 74 70 75 74  d,.** set output
3814c 20 76 61 72 69 61 62 6c 65 73 20 2a 70 61 52 6f   variables *paRo
3814d 6f 74 20 61 6e 64 20 2a 70 6e 52 6f 6f 74 20 74  ot and *pnRoot t
3814e 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 72 6f  o contain the ro
3814f 6f 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  ot node..**.** I
38150 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  f successful, SQ
38151 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
38152 6e 65 64 20 61 6e 64 20 6f 75 74 70 75 74 20 76  ned and output v
38153 61 72 69 61 62 6c 65 20 2a 70 69 4c 61 73 74 20  ariable *piLast 
38154 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 74 68 65  is.** set to the
38155 20 6c 61 72 67 65 73 74 20 62 6c 6f 63 6b 69 64   largest blockid
38156 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
38157 64 61 74 61 62 61 73 65 20 28 6f 72 20 7a 65 72  database (or zer
38158 6f 20 69 66 20 6e 6f 0a 2a 2a 20 62 6c 6f 63 6b  o if no.** block
38159 73 20 77 65 72 65 20 77 72 69 74 74 65 6e 20 74  s were written t
3815a 6f 20 74 68 65 20 64 62 29 2e 20 4f 74 68 65 72  o the db). Other
3815b 77 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20  wise, an SQLite 
3815c 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 0a 2a  error code is .*
3815d 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  * returned..*/.s
3815e 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f  tatic int fts3No
3815f 64 65 57 72 69 74 65 28 0a 20 20 46 74 73 33 54  deWrite(.  Fts3T
38160 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
38161 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
38162 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
38163 6c 65 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 4e  le */.  SegmentN
38164 6f 64 65 20 2a 70 54 72 65 65 2c 20 20 20 20 20  ode *pTree,     
38165 20 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65          /* Segme
38166 6e 74 4e 6f 64 65 20 68 61 6e 64 6c 65 20 2a 2f  ntNode handle */
38167 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 2c 20  .  int iHeight, 
38168 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38169 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
3816a 74 68 69 73 20 6e 6f 64 65 20 69 6e 20 74 72 65  this node in tre
3816b 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
3816c 6e 74 36 34 20 69 4c 65 61 66 2c 20 20 20 20 20  nt64 iLeaf,     
3816d 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20         /* Block 
3816e 69 64 20 6f 66 20 66 69 72 73 74 20 6c 65 61 66  id of first leaf
3816f 20 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74   node */.  sqlit
38170 65 33 5f 69 6e 74 36 34 20 69 46 72 65 65 2c 20  e3_int64 iFree, 
38171 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c             /* Bl
38172 6f 63 6b 20 69 64 20 6f 66 20 6e 65 78 74 20 66  ock id of next f
38173 72 65 65 20 73 6c 6f 74 20 69 6e 20 25 5f 73 65  ree slot in %_se
38174 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69  gments */.  sqli
38175 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 61 73  te3_int64 *piLas
38176 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  t,          /* O
38177 55 54 3a 20 42 6c 6f 63 6b 20 69 64 20 6f 66 20  UT: Block id of 
38178 6c 61 73 74 20 65 6e 74 72 79 20 77 72 69 74 74  last entry writt
38179 65 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  en */.  char **p
3817a 61 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20  aRoot,          
3817b 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3817c 44 61 74 61 20 66 6f 72 20 72 6f 6f 74 20 6e 6f  Data for root no
3817d 64 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  de */.  int *pnR
3817e 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
3817f 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
38180 53 69 7a 65 20 6f 66 20 72 6f 6f 74 20 6e 6f 64  Size of root nod
38181 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b  e in bytes */.){
38182 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
38183 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 21 70  TE_OK;..  if( !p
38184 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Tree->pParent ){
38185 0a 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6e 6f 64  .    /* Root nod
38186 65 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20 2a  e of the tree. *
38187 2f 0a 20 20 20 20 69 6e 74 20 6e 53 74 61 72 74  /.    int nStart
38188 20 3d 20 66 74 73 33 54 72 65 65 46 69 6e 69 73   = fts3TreeFinis
38189 68 4e 6f 64 65 28 70 54 72 65 65 2c 20 69 48 65  hNode(pTree, iHe
3818a 69 67 68 74 2c 20 69 4c 65 61 66 29 3b 0a 20 20  ight, iLeaf);.  
3818b 20 20 2a 70 69 4c 61 73 74 20 3d 20 69 46 72 65    *piLast = iFre
3818c 65 2d 31 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74  e-1;.    *pnRoot
3818d 20 3d 20 70 54 72 65 65 2d 3e 6e 44 61 74 61 20   = pTree->nData 
3818e 2d 20 6e 53 74 61 72 74 3b 0a 20 20 20 20 2a 70  - nStart;.    *p
3818f 61 52 6f 6f 74 20 3d 20 26 70 54 72 65 65 2d 3e  aRoot = &pTree->
38190 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 3b 0a 20  aData[nStart];. 
38191 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53 65 67 6d   }else{.    Segm
38192 65 6e 74 4e 6f 64 65 20 2a 70 49 74 65 72 3b 0a  entNode *pIter;.
38193 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
38194 34 20 69 4e 65 78 74 46 72 65 65 20 3d 20 69 46  4 iNextFree = iF
38195 72 65 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ree;.    sqlite3
38196 5f 69 6e 74 36 34 20 69 4e 65 78 74 4c 65 61 66  _int64 iNextLeaf
38197 20 3d 20 69 4c 65 61 66 3b 0a 20 20 20 20 66 6f   = iLeaf;.    fo
38198 72 28 70 49 74 65 72 3d 70 54 72 65 65 2d 3e 70  r(pIter=pTree->p
38199 4c 65 66 74 6d 6f 73 74 3b 20 70 49 74 65 72 20  Leftmost; pIter 
3819a 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
3819b 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70  ; pIter=pIter->p
3819c 52 69 67 68 74 29 7b 0a 20 20 20 20 20 20 69 6e  Right){.      in
3819d 74 20 6e 53 74 61 72 74 20 3d 20 66 74 73 33 54  t nStart = fts3T
3819e 72 65 65 46 69 6e 69 73 68 4e 6f 64 65 28 70 49  reeFinishNode(pI
3819f 74 65 72 2c 20 69 48 65 69 67 68 74 2c 20 69 4e  ter, iHeight, iN
381a0 65 78 74 4c 65 61 66 29 3b 0a 20 20 20 20 20 20  extLeaf);.      
381a1 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 70 49 74  int nWrite = pIt
381a2 65 72 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 74 61  er->nData - nSta
381a3 72 74 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20  rt;.  .      rc 
381a4 3d 20 66 74 73 33 57 72 69 74 65 53 65 67 6d 65  = fts3WriteSegme
381a5 6e 74 28 70 2c 20 69 4e 65 78 74 46 72 65 65 2c  nt(p, iNextFree,
381a6 20 26 70 49 74 65 72 2d 3e 61 44 61 74 61 5b 6e   &pIter->aData[n
381a7 53 74 61 72 74 5d 2c 20 6e 57 72 69 74 65 29 3b  Start], nWrite);
381a8 0a 20 20 20 20 20 20 69 4e 65 78 74 46 72 65 65  .      iNextFree
381a9 2b 2b 3b 0a 20 20 20 20 20 20 69 4e 65 78 74 4c  ++;.      iNextL
381aa 65 61 66 20 2b 3d 20 28 70 49 74 65 72 2d 3e 6e  eaf += (pIter->n
381ab 45 6e 74 72 79 2b 31 29 3b 0a 20 20 20 20 7d 0a  Entry+1);.    }.
381ac 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
381ad 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61  TE_OK ){.      a
381ae 73 73 65 72 74 28 20 69 4e 65 78 74 4c 65 61 66  ssert( iNextLeaf
381af 3d 3d 69 46 72 65 65 20 29 3b 0a 20 20 20 20 20  ==iFree );.     
381b0 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 57 72   rc = fts3NodeWr
381b1 69 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 70  ite(.          p
381b2 2c 20 70 54 72 65 65 2d 3e 70 50 61 72 65 6e 74  , pTree->pParent
381b3 2c 20 69 48 65 69 67 68 74 2b 31 2c 20 69 46 72  , iHeight+1, iFr
381b4 65 65 2c 20 69 4e 65 78 74 46 72 65 65 2c 20 70  ee, iNextFree, p
381b5 69 4c 61 73 74 2c 20 70 61 52 6f 6f 74 2c 20 70  iLast, paRoot, p
381b6 6e 52 6f 6f 74 0a 20 20 20 20 20 20 29 3b 0a 20  nRoot.      );. 
381b7 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
381b8 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
381b9 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  Free all memory 
381ba 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73 6f  allocations asso
381bb 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
381bc 74 72 65 65 20 70 54 72 65 65 2e 0a 2a 2f 0a 73  tree pTree..*/.s
381bd 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 4e  tatic void fts3N
381be 6f 64 65 46 72 65 65 28 53 65 67 6d 65 6e 74 4e  odeFree(SegmentN
381bf 6f 64 65 20 2a 70 54 72 65 65 29 7b 0a 20 20 69  ode *pTree){.  i
381c0 66 28 20 70 54 72 65 65 20 29 7b 0a 20 20 20 20  f( pTree ){.    
381c1 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 20 3d  SegmentNode *p =
381c2 20 70 54 72 65 65 2d 3e 70 4c 65 66 74 6d 6f 73   pTree->pLeftmos
381c3 74 3b 0a 20 20 20 20 66 74 73 33 4e 6f 64 65 46  t;.    fts3NodeF
381c4 72 65 65 28 70 2d 3e 70 50 61 72 65 6e 74 29 3b  ree(p->pParent);
381c5 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b  .    while( p ){
381c6 0a 20 20 20 20 20 20 53 65 67 6d 65 6e 74 4e 6f  .      SegmentNo
381c7 64 65 20 2a 70 52 69 67 68 74 20 3d 20 70 2d 3e  de *pRight = p->
381c8 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66  pRight;.      if
381c9 28 20 70 2d 3e 61 44 61 74 61 21 3d 28 63 68 61  ( p->aData!=(cha
381ca 72 20 2a 29 26 70 5b 31 5d 20 29 7b 0a 20 20 20  r *)&p[1] ){.   
381cb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
381cc 65 28 70 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  e(p->aData);.   
381cd 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
381ce 74 28 20 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20  t( pRight==0 || 
381cf 70 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b  p->zMalloc==0 );
381d0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
381d1 72 65 65 28 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b  ree(p->zMalloc);
381d2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
381d3 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 70 20  ree(p);.      p 
381d4 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a  = pRight;.    }.
381d5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
381d6 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65 20 73   a term to the s
381d7 65 67 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 6e  egment being con
381d8 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65 20  structed by the 
381d9 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62  SegmentWriter ob
381da 6a 65 63 74 0a 2a 2a 20 2a 70 70 57 72 69 74 65  ject.** *ppWrite
381db 72 2e 20 57 68 65 6e 20 61 64 64 69 6e 67 20 74  r. When adding t
381dc 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 74 6f  he first term to
381dd 20 61 20 73 65 67 6d 65 6e 74 2c 20 2a 70 70 57   a segment, *ppW
381de 72 69 74 65 72 20 73 68 6f 75 6c 64 0a 2a 2a 20  riter should.** 
381df 62 65 20 70 61 73 73 65 64 20 4e 55 4c 4c 2e 20  be passed NULL. 
381e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69  This function wi
381e1 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ll allocate a ne
381e2 77 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20  w SegmentWriter 
381e3 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 72 65  object.** and re
381e4 74 75 72 6e 20 69 74 20 76 69 61 20 74 68 65 20  turn it via the 
381e5 69 6e 70 75 74 2f 6f 75 74 70 75 74 20 76 61 72  input/output var
381e6 69 61 62 6c 65 20 2a 70 70 57 72 69 74 65 72 20  iable *ppWriter 
381e7 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
381e8 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
381e9 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
381ea 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
381eb 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
381ec 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
381ed 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
381ee 57 72 69 74 65 72 41 64 64 28 0a 20 20 46 74 73  WriterAdd(.  Fts
381ef 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
381f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
381f1 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
381f2 6e 64 6c 65 20 2a 2f 0a 20 20 53 65 67 6d 65 6e  ndle */.  Segmen
381f3 74 57 72 69 74 65 72 20 2a 2a 70 70 57 72 69 74  tWriter **ppWrit
381f4 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f  er,       /* IN/
381f5 4f 55 54 3a 20 53 65 67 6d 65 6e 74 57 72 69 74  OUT: SegmentWrit
381f6 65 72 20 68 61 6e 64 6c 65 20 2a 2f 20 0a 20 20  er handle */ .  
381f7 69 6e 74 20 69 73 43 6f 70 79 54 65 72 6d 2c 20  int isCopyTerm, 
381f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381f9 2f 2a 20 54 72 75 65 20 69 66 20 62 75 66 66 65  /* True if buffe
381fa 72 20 7a 54 65 72 6d 20 6d 75 73 74 20 62 65 20  r zTerm must be 
381fb 63 6f 70 69 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  copied */.  cons
381fc 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20 20  t char *zTerm,  
381fd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
381fe 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72  ointer to buffer
381ff 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 65 72 6d   containing term
38200 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c   */.  int nTerm,
38201 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38202 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
38203 20 74 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a   term in bytes *
38204 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
38205 61 44 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20  aDoclist,       
38206 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
38207 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  o buffer contain
38208 69 6e 67 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20  ing doclist */. 
38209 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 20 20 20   int nDoclist   
3820a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3820b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63 6c   /* Size of docl
3820c 69 73 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ist in bytes */.
3820d 29 7b 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78  ){.  int nPrefix
3820e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3820f 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
38210 74 65 72 6d 20 70 72 65 66 69 78 20 69 6e 20 62  term prefix in b
38211 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ytes */.  int nS
38212 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
38213 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
38214 65 20 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78  e of term suffix
38215 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
38216 6e 74 20 6e 52 65 71 3b 20 20 20 20 20 20 20 20  nt nReq;        
38217 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38218 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
38219 73 20 72 65 71 75 69 72 65 64 20 6f 6e 20 6c 65  s required on le
3821a 61 66 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  af page */.  int
3821b 20 6e 44 61 74 61 3b 0a 20 20 53 65 67 6d 65 6e   nData;.  Segmen
3821c 74 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  tWriter *pWriter
3821d 20 3d 20 2a 70 70 57 72 69 74 65 72 3b 0a 0a 20   = *ppWriter;.. 
3821e 20 69 66 28 20 21 70 57 72 69 74 65 72 20 29 7b   if( !pWriter ){
3821f 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
38220 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
38221 53 74 6d 74 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c  Stmt;..    /* Al
38222 6c 6f 63 61 74 65 20 74 68 65 20 53 65 67 6d 65  locate the Segme
38223 6e 74 57 72 69 74 65 72 20 73 74 72 75 63 74 75  ntWriter structu
38224 72 65 20 2a 2f 0a 20 20 20 20 70 57 72 69 74 65  re */.    pWrite
38225 72 20 3d 20 28 53 65 67 6d 65 6e 74 57 72 69 74  r = (SegmentWrit
38226 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  er *)sqlite3_mal
38227 6c 6f 63 28 73 69 7a 65 6f 66 28 53 65 67 6d 65  loc(sizeof(Segme
38228 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 20 20  ntWriter));.    
38229 69 66 28 20 21 70 57 72 69 74 65 72 20 29 20 72  if( !pWriter ) r
3822a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
3822b 45 4d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  EM;.    memset(p
3822c 57 72 69 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f  Writer, 0, sizeo
3822d 66 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72 29  f(SegmentWriter)
3822e 29 3b 0a 20 20 20 20 2a 70 70 57 72 69 74 65 72  );.    *ppWriter
3822f 20 3d 20 70 57 72 69 74 65 72 3b 0a 0a 20 20 20   = pWriter;..   
38230 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62   /* Allocate a b
38231 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20 74  uffer in which t
38232 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74  o accumulate dat
38233 61 20 2a 2f 0a 20 20 20 20 70 57 72 69 74 65 72  a */.    pWriter
38234 2d 3e 61 44 61 74 61 20 3d 20 28 63 68 61 72 20  ->aData = (char 
38235 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
38236 28 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a  (p->nNodeSize);.
38237 20 20 20 20 69 66 28 20 21 70 57 72 69 74 65 72      if( !pWriter
38238 2d 3e 61 44 61 74 61 20 29 20 72 65 74 75 72 6e  ->aData ) return
38239 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3823a 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a     pWriter->nSiz
3823b 65 20 3d 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65  e = p->nNodeSize
3823c 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74  ;..    /* Find t
3823d 68 65 20 6e 65 78 74 20 66 72 65 65 20 62 6c 6f  he next free blo
3823e 63 6b 69 64 20 69 6e 20 74 68 65 20 25 5f 73 65  ckid in the %_se
3823f 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 2a 2f 0a  gments table */.
38240 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
38241 53 74 6d 74 28 70 2c 20 53 51 4c 5f 4e 45 58 54  Stmt(p, SQL_NEXT
38242 5f 53 45 47 4d 45 4e 54 53 5f 49 44 2c 20 26 70  _SEGMENTS_ID, &p
38243 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 69 66  Stmt, 0);.    if
38244 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
38245 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
38246 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
38247 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53  =sqlite3_step(pS
38248 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 70 57  tmt) ){.      pW
38249 72 69 74 65 72 2d 3e 69 46 72 65 65 20 3d 20 73  riter->iFree = s
3824a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
3824b 74 36 34 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  t64(pStmt, 0);. 
3824c 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 46       pWriter->iF
3824d 69 72 73 74 20 3d 20 70 57 72 69 74 65 72 2d 3e  irst = pWriter->
3824e 69 46 72 65 65 3b 0a 20 20 20 20 7d 0a 20 20 20  iFree;.    }.   
3824f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
38250 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  set(pStmt);.    
38251 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
38252 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
38253 20 7d 0a 20 20 6e 44 61 74 61 20 3d 20 70 57 72   }.  nData = pWr
38254 69 74 65 72 2d 3e 6e 44 61 74 61 3b 0a 0a 20 20  iter->nData;..  
38255 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33 50 72  nPrefix = fts3Pr
38256 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70 57 72  efixCompress(pWr
38257 69 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70 57 72  iter->zTerm, pWr
38258 69 74 65 72 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65  iter->nTerm, zTe
38259 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 6e 53  rm, nTerm);.  nS
3825a 75 66 66 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50  uffix = nTerm-nP
3825b 72 65 66 69 78 3b 0a 0a 20 20 2f 2a 20 46 69 67  refix;..  /* Fig
3825c 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
3825d 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69   bytes are requi
3825e 72 65 64 20 62 79 20 74 68 69 73 20 6e 65 77 20  red by this new 
3825f 65 6e 74 72 79 20 2a 2f 0a 20 20 6e 52 65 71 20  entry */.  nReq 
38260 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  = sqlite3Fts3Var
38261 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69 78 29 20  intLen(nPrefix) 
38262 2b 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63  +    /* varint c
38263 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66 69 78  ontaining prefix
38264 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c   size */.    sql
38265 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
38266 6e 28 6e 53 75 66 66 69 78 29 20 2b 20 20 20 20  n(nSuffix) +    
38267 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63       /* varint c
38268 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66 69 78  ontaining suffix
38269 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 6e 53 75   size */.    nSu
3826a 66 66 69 78 20 2b 20 20 20 20 20 20 20 20 20 20  ffix +          
3826b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3826c 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73 75 66       /* Term suf
3826d 66 69 78 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fix */.    sqlit
3826e 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28  e3Fts3VarintLen(
3826f 6e 44 6f 63 6c 69 73 74 29 20 2b 20 20 20 20 20  nDoclist) +     
38270 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f     /* Size of do
38271 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 6e 44 6f  clist */.    nDo
38272 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
38273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38274 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20       /* Doclist 
38275 64 61 74 61 20 2a 2f 0a 0a 20 20 69 66 28 20 6e  data */..  if( n
38276 44 61 74 61 3e 30 20 26 26 20 6e 44 61 74 61 2b  Data>0 && nData+
38277 6e 52 65 71 3e 70 2d 3e 6e 4e 6f 64 65 53 69 7a  nReq>p->nNodeSiz
38278 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  e ){.    int rc;
38279 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72  ..    /* The cur
3827a 72 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 69  rent leaf node i
3827b 73 20 66 75 6c 6c 2e 20 57 72 69 74 65 20 69 74  s full. Write it
3827c 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
3827d 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20  base. */.    rc 
3827e 3d 20 66 74 73 33 57 72 69 74 65 53 65 67 6d 65  = fts3WriteSegme
3827f 6e 74 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69  nt(p, pWriter->i
38280 46 72 65 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d  Free++, pWriter-
38281 3e 61 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a  >aData, nData);.
38282 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
38283 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
38284 63 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74  c;..    /* Add t
38285 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20  he current term 
38286 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20  to the interior 
38287 6e 6f 64 65 20 74 72 65 65 2e 20 54 68 65 20 74  node tree. The t
38288 65 72 6d 20 61 64 64 65 64 20 74 6f 0a 20 20 20  erm added to.   
38289 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72   ** the interior
3828a 20 74 72 65 65 20 6d 75 73 74 3a 0a 20 20 20 20   tree must:.    
3828b 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 61 29 20 62  **.    **   a) b
3828c 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  e greater than t
3828d 68 65 20 6c 61 72 67 65 73 74 20 74 65 72 6d 20  he largest term 
3828e 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  on the leaf node
3828f 20 6a 75 73 74 20 77 72 69 74 74 65 6e 0a 20 20   just written.  
38290 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65    **      to the
38291 20 64 61 74 61 62 61 73 65 20 28 73 74 69 6c 6c   database (still
38292 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 70 57   available in pW
38293 72 69 74 65 72 2d 3e 7a 54 65 72 6d 29 2c 20 61  riter->zTerm), a
38294 6e 64 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  nd.    **.    **
38295 20 20 20 62 29 20 62 65 20 6c 65 73 73 20 74 68     b) be less th
38296 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
38297 68 65 20 74 65 72 6d 20 61 62 6f 75 74 20 74 6f  he term about to
38298 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
38299 20 6e 65 77 0a 20 20 20 20 2a 2a 20 20 20 20 20   new.    **     
3829a 20 6c 65 61 66 20 6e 6f 64 65 20 28 7a 54 65 72   leaf node (zTer
3829b 6d 2f 6e 54 65 72 6d 29 2e 0a 20 20 20 20 2a 2a  m/nTerm)..    **
3829c 0a 20 20 20 20 2a 2a 20 49 6e 20 6f 74 68 65 72  .    ** In other
3829d 20 77 6f 72 64 73 2c 20 69 74 20 6d 75 73 74 20   words, it must 
3829e 62 65 20 74 68 65 20 70 72 65 66 69 78 20 6f 66  be the prefix of
3829f 20 7a 54 65 72 6d 20 31 20 62 79 74 65 20 6c 6f   zTerm 1 byte lo
382a0 6e 67 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a  nger than.    **
382a1 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66   the common pref
382a2 69 78 20 28 69 66 20 61 6e 79 29 20 6f 66 20 7a  ix (if any) of z
382a3 54 65 72 6d 20 61 6e 64 20 70 57 72 69 74 65 72  Term and pWriter
382a4 2d 3e 7a 54 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a  ->zTerm..    */.
382a5 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 72 65      assert( nPre
382a6 66 69 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20 20  fix<nTerm );.   
382a7 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64   rc = fts3NodeAd
382a8 64 54 65 72 6d 28 70 2c 20 26 70 57 72 69 74 65  dTerm(p, &pWrite
382a9 72 2d 3e 70 54 72 65 65 2c 20 69 73 43 6f 70 79  r->pTree, isCopy
382aa 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 50 72  Term, zTerm, nPr
382ab 65 66 69 78 2b 31 29 3b 0a 20 20 20 20 69 66 28  efix+1);.    if(
382ac 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
382ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
382ae 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20   nData = 0;.    
382af 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 20 3d  pWriter->nTerm =
382b0 20 30 3b 0a 0a 20 20 20 20 6e 50 72 65 66 69 78   0;..    nPrefix
382b1 20 3d 20 30 3b 0a 20 20 20 20 6e 53 75 66 66 69   = 0;.    nSuffi
382b2 78 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 6e  x = nTerm;.    n
382b3 52 65 71 20 3d 20 31 20 2b 20 20 20 20 20 20 20  Req = 1 +       
382b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382b5 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74         /* varint
382b6 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66   containing pref
382b7 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20  ix size */.     
382b8 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69   sqlite3Fts3Vari
382b9 6e 74 4c 65 6e 28 6e 54 65 72 6d 29 20 2b 20 20  ntLen(nTerm) +  
382ba 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74         /* varint
382bb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66   containing suff
382bc 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20  ix size */.     
382bd 20 6e 54 65 72 6d 20 2b 20 20 20 20 20 20 20 20   nTerm +        
382be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382bf 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73         /* Term s
382c0 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 73  uffix */.      s
382c1 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74  qlite3Fts3Varint
382c2 4c 65 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b 20  Len(nDoclist) + 
382c3 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
382c4 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  doclist */.     
382c5 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20   nDoclist;      
382c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382c7 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73         /* Doclis
382c8 74 20 64 61 74 61 20 2a 2f 0a 20 20 7d 0a 0a 20  t data */.  }.. 
382c9 20 2f 2a 20 49 66 20 74 68 65 20 62 75 66 66 65   /* If the buffe
382ca 72 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  r currently allo
382cb 63 61 74 65 64 20 69 73 20 74 6f 6f 20 73 6d 61  cated is too sma
382cc 6c 6c 20 66 6f 72 20 74 68 69 73 20 65 6e 74 72  ll for this entr
382cd 79 2c 20 72 65 61 6c 6c 6f 63 0a 20 20 2a 2a 20  y, realloc.  ** 
382ce 74 68 65 20 62 75 66 66 65 72 20 74 6f 20 6d 61  the buffer to ma
382cf 6b 65 20 69 74 20 6c 61 72 67 65 20 65 6e 6f 75  ke it large enou
382d0 67 68 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  gh..  */.  if( n
382d1 52 65 71 3e 70 57 72 69 74 65 72 2d 3e 6e 53 69  Req>pWriter->nSi
382d2 7a 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ze ){.    char *
382d3 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  aNew = sqlite3_r
382d4 65 61 6c 6c 6f 63 28 70 57 72 69 74 65 72 2d 3e  ealloc(pWriter->
382d5 61 44 61 74 61 2c 20 6e 52 65 71 29 3b 0a 20 20  aData, nReq);.  
382d6 20 20 69 66 28 20 21 61 4e 65 77 20 29 20 72 65    if( !aNew ) re
382d7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
382d8 4d 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  M;.    pWriter->
382d9 61 44 61 74 61 20 3d 20 61 4e 65 77 3b 0a 20 20  aData = aNew;.  
382da 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65    pWriter->nSize
382db 20 3d 20 6e 52 65 71 3b 0a 20 20 7d 0a 20 20 61   = nReq;.  }.  a
382dc 73 73 65 72 74 28 20 6e 44 61 74 61 2b 6e 52 65  ssert( nData+nRe
382dd 71 3c 3d 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a  q<=pWriter->nSiz
382de 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e  e );..  /* Appen
382df 64 20 74 68 65 20 70 72 65 66 69 78 2d 63 6f 6d  d the prefix-com
382e0 70 72 65 73 73 65 64 20 74 65 72 6d 20 61 6e 64  pressed term and
382e1 20 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20   doclist to the 
382e2 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 44 61  buffer. */.  nDa
382e3 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ta += sqlite3Fts
382e4 33 50 75 74 56 61 72 69 6e 74 28 26 70 57 72 69  3PutVarint(&pWri
382e5 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61  ter->aData[nData
382e6 5d 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 6e  ], nPrefix);.  n
382e7 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46  Data += sqlite3F
382e8 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 57  ts3PutVarint(&pW
382e9 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61  riter->aData[nDa
382ea 74 61 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20  ta], nSuffix);. 
382eb 20 6d 65 6d 63 70 79 28 26 70 57 72 69 74 65 72   memcpy(&pWriter
382ec 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20  ->aData[nData], 
382ed 26 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c  &zTerm[nPrefix],
382ee 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 6e 44 61   nSuffix);.  nDa
382ef 74 61 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0a 20  ta += nSuffix;. 
382f0 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65   nData += sqlite
382f1 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
382f2 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e  pWriter->aData[n
382f3 44 61 74 61 5d 2c 20 6e 44 6f 63 6c 69 73 74 29  Data], nDoclist)
382f4 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 57 72 69  ;.  memcpy(&pWri
382f5 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61  ter->aData[nData
382f6 5d 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f  ], aDoclist, nDo
382f7 63 6c 69 73 74 29 3b 0a 20 20 70 57 72 69 74 65  clist);.  pWrite
382f8 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74 61  r->nData = nData
382f9 20 2b 20 6e 44 6f 63 6c 69 73 74 3b 0a 0a 20 20   + nDoclist;..  
382fa 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  /* Save the curr
382fb 65 6e 74 20 74 65 72 6d 20 73 6f 20 74 68 61 74  ent term so that
382fc 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 20   it can be used 
382fd 74 6f 20 70 72 65 66 69 78 2d 63 6f 6d 70 72 65  to prefix-compre
382fe 73 73 20 74 68 65 20 6e 65 78 74 2e 0a 20 20 2a  ss the next..  *
382ff 2a 20 49 66 20 74 68 65 20 69 73 43 6f 70 79 54  * If the isCopyT
38300 65 72 6d 20 70 61 72 61 6d 65 74 65 72 20 69 73  erm parameter is
38301 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
38302 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
38303 6f 20 62 79 0a 20 20 2a 2a 20 7a 54 65 72 6d 20  o by.  ** zTerm 
38304 69 73 20 74 72 61 6e 73 69 65 6e 74 2c 20 73 6f  is transient, so
38305 20 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   take a copy of 
38306 74 68 65 20 74 65 72 6d 20 64 61 74 61 2e 20 4f  the term data. O
38307 74 68 65 72 77 69 73 65 2c 20 6a 75 73 74 0a 20  therwise, just. 
38308 20 2a 2a 20 73 74 6f 72 65 20 61 20 63 6f 70 79   ** store a copy
38309 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2e   of the pointer.
3830a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f  .  */.  if( isCo
3830b 70 79 54 65 72 6d 20 29 7b 0a 20 20 20 20 69 66  pyTerm ){.    if
3830c 28 20 6e 54 65 72 6d 3e 70 57 72 69 74 65 72 2d  ( nTerm>pWriter-
3830d 3e 6e 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >nMalloc ){.    
3830e 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73    char *zNew = s
3830f 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
38310 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 2c  Writer->zMalloc,
38311 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20 20 20   nTerm*2);.     
38312 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20   if( !zNew ){.  
38313 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
38314 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
38315 20 7d 0a 20 20 20 20 20 20 70 57 72 69 74 65 72   }.      pWriter
38316 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72  ->nMalloc = nTer
38317 6d 2a 32 3b 0a 20 20 20 20 20 20 70 57 72 69 74  m*2;.      pWrit
38318 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e  er->zMalloc = zN
38319 65 77 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65  ew;.      pWrite
3831a 72 2d 3e 7a 54 65 72 6d 20 3d 20 7a 4e 65 77 3b  r->zTerm = zNew;
3831b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
3831c 74 28 20 70 57 72 69 74 65 72 2d 3e 7a 54 65 72  t( pWriter->zTer
3831d 6d 3d 3d 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c  m==pWriter->zMal
3831e 6c 6f 63 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70  loc );.    memcp
3831f 79 28 70 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d  y(pWriter->zTerm
38320 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , zTerm, nTerm);
38321 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57  .  }else{.    pW
38322 72 69 74 65 72 2d 3e 7a 54 65 72 6d 20 3d 20 28  riter->zTerm = (
38323 63 68 61 72 20 2a 29 7a 54 65 72 6d 3b 0a 20 20  char *)zTerm;.  
38324 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65  }.  pWriter->nTe
38325 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 72  rm = nTerm;..  r
38326 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
38327 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6c 75 73 68 20  .}../*.** Flush 
38328 61 6c 6c 20 64 61 74 61 20 61 73 73 6f 63 69 61  all data associa
38329 74 65 64 20 77 69 74 68 20 74 68 65 20 53 65 67  ted with the Seg
3832a 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63  mentWriter objec
3832b 74 20 70 57 72 69 74 65 72 20 74 6f 20 74 68 65  t pWriter to the
3832c 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68  .** database. Th
3832d 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  is function must
3832e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72   be called after
3832f 20 61 6c 6c 20 74 65 72 6d 73 20 68 61 76 65 20   all terms have 
38330 62 65 65 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f  been added.** to
38331 20 74 68 65 20 73 65 67 6d 65 6e 74 20 75 73 69   the segment usi
38332 6e 67 20 66 74 73 33 53 65 67 57 72 69 74 65 72  ng fts3SegWriter
38333 41 64 64 28 29 2e 20 49 66 20 73 75 63 63 65 73  Add(). If succes
38334 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
38335 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  is.** returned. 
38336 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51  Otherwise, an SQ
38337 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
38338 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
38339 74 73 33 53 65 67 57 72 69 74 65 72 46 6c 75 73  ts3SegWriterFlus
3833a 68 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  h(.  Fts3Table *
3833b 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
3833c 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
3833d 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
3833e 20 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20    SegmentWriter 
3833f 2a 70 57 72 69 74 65 72 2c 20 20 20 20 20 20 20  *pWriter,       
38340 20 20 2f 2a 20 53 65 67 6d 65 6e 74 57 72 69 74    /* SegmentWrit
38341 65 72 20 74 6f 20 66 6c 75 73 68 20 74 6f 20 74  er to flush to t
38342 68 65 20 64 62 20 2a 2f 0a 20 20 69 6e 74 20 69  he db */.  int i
38343 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20  Level,          
38344 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
38345 6c 75 65 20 66 6f 72 20 27 6c 65 76 65 6c 27 20  lue for 'level' 
38346 63 6f 6c 75 6d 6e 20 6f 66 20 25 5f 73 65 67 64  column of %_segd
38347 69 72 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78  ir */.  int iIdx
38348 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38349 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
3834a 20 66 6f 72 20 27 69 64 78 27 20 63 6f 6c 75 6d   for 'idx' colum
3834b 6e 20 6f 66 20 25 5f 73 65 67 64 69 72 20 2a 2f  n of %_segdir */
3834c 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
3834d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3834e 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
3834f 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 70 57  code */.  if( pW
38350 72 69 74 65 72 2d 3e 70 54 72 65 65 20 29 7b 0a  riter->pTree ){.
38351 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
38352 34 20 69 4c 61 73 74 20 3d 20 30 3b 20 20 20 20  4 iLast = 0;    
38353 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 62 6c 6f    /* Largest blo
38354 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20 74 6f  ck id written to
38355 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
38356 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
38357 4c 61 73 74 4c 65 61 66 3b 20 20 20 20 20 20 2f  LastLeaf;      /
38358 2a 20 4c 61 72 67 65 73 74 20 6c 65 61 66 20 62  * Largest leaf b
38359 6c 6f 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20  lock id written 
3835a 74 6f 20 64 62 20 2a 2f 0a 20 20 20 20 63 68 61  to db */.    cha
3835b 72 20 2a 7a 52 6f 6f 74 20 3d 20 4e 55 4c 4c 3b  r *zRoot = NULL;
3835c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
3835d 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20  inter to buffer 
3835e 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20  containing root 
3835f 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  node */.    int 
38360 6e 52 6f 6f 74 20 3d 20 30 3b 20 20 20 20 20 20  nRoot = 0;      
38361 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
38362 65 20 6f 66 20 62 75 66 66 65 72 20 7a 52 6f 6f  e of buffer zRoo
38363 74 20 2a 2f 0a 0a 20 20 20 20 69 4c 61 73 74 4c  t */..    iLastL
38364 65 61 66 20 3d 20 70 57 72 69 74 65 72 2d 3e 69  eaf = pWriter->i
38365 46 72 65 65 3b 0a 20 20 20 20 72 63 20 3d 20 66  Free;.    rc = f
38366 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74 28  ts3WriteSegment(
38367 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 46 72 65  p, pWriter->iFre
38368 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61 44  e++, pWriter->aD
38369 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44  ata, pWriter->nD
3836a 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
3836b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3836c 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 4e 6f       rc = fts3No
3836d 64 65 57 72 69 74 65 28 70 2c 20 70 57 72 69 74  deWrite(p, pWrit
3836e 65 72 2d 3e 70 54 72 65 65 2c 20 31 2c 0a 20 20  er->pTree, 1,.  
3836f 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
38370 3e 69 46 69 72 73 74 2c 20 70 57 72 69 74 65 72  >iFirst, pWriter
38371 2d 3e 69 46 72 65 65 2c 20 26 69 4c 61 73 74 2c  ->iFree, &iLast,
38372 20 26 7a 52 6f 6f 74 2c 20 26 6e 52 6f 6f 74 29   &zRoot, &nRoot)
38373 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
38374 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
38375 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
38376 57 72 69 74 65 53 65 67 64 69 72 28 0a 20 20 20  WriteSegdir(.   
38377 20 20 20 20 20 20 20 70 2c 20 69 4c 65 76 65 6c         p, iLevel
38378 2c 20 69 49 64 78 2c 20 70 57 72 69 74 65 72 2d  , iIdx, pWriter-
38379 3e 69 46 69 72 73 74 2c 20 69 4c 61 73 74 4c 65  >iFirst, iLastLe
3837a 61 66 2c 20 69 4c 61 73 74 2c 20 7a 52 6f 6f 74  af, iLast, zRoot
3837b 2c 20 6e 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a  , nRoot);.    }.
3837c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
3837d 54 68 65 20 65 6e 74 69 72 65 20 74 72 65 65 20  The entire tree 
3837e 66 69 74 73 20 6f 6e 20 74 68 65 20 72 6f 6f 74  fits on the root
3837f 20 6e 6f 64 65 2e 20 57 72 69 74 65 20 69 74 20   node. Write it 
38380 74 6f 20 74 68 65 20 73 65 67 64 69 72 20 74 61  to the segdir ta
38381 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ble. */.    rc =
38382 20 66 74 73 33 57 72 69 74 65 53 65 67 64 69 72   fts3WriteSegdir
38383 28 0a 20 20 20 20 20 20 20 20 70 2c 20 69 4c 65  (.        p, iLe
38384 76 65 6c 2c 20 69 49 64 78 2c 20 30 2c 20 30 2c  vel, iIdx, 0, 0,
38385 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 61 44 61   0, pWriter->aDa
38386 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44 61  ta, pWriter->nDa
38387 74 61 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ta);.  }.  retur
38388 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
38389 65 6c 65 61 73 65 20 61 6c 6c 20 6d 65 6d 6f 72  elease all memor
3838a 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 53 65  y held by the Se
3838b 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65  gmentWriter obje
3838c 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ct passed as the
3838d 20 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d   .** first argum
3838e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
3838f 6f 69 64 20 66 74 73 33 53 65 67 57 72 69 74 65  oid fts3SegWrite
38390 72 46 72 65 65 28 53 65 67 6d 65 6e 74 57 72 69  rFree(SegmentWri
38391 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
38392 20 69 66 28 20 70 57 72 69 74 65 72 20 29 7b 0a   if( pWriter ){.
38393 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
38394 28 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 29  (pWriter->aData)
38395 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
38396 65 65 28 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c  ee(pWriter->zMal
38397 6c 6f 63 29 3b 0a 20 20 20 20 66 74 73 33 4e 6f  loc);.    fts3No
38398 64 65 46 72 65 65 28 70 57 72 69 74 65 72 2d 3e  deFree(pWriter->
38399 70 54 72 65 65 29 3b 0a 20 20 20 20 73 71 6c 69  pTree);.    sqli
3839a 74 65 33 5f 66 72 65 65 28 70 57 72 69 74 65 72  te3_free(pWriter
3839b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
3839c 54 68 65 20 66 69 72 73 74 20 76 61 6c 75 65 20  The first value 
3839d 69 6e 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61  in the apVal[] a
3839e 72 72 61 79 20 69 73 20 61 73 73 75 6d 65 64 20  rray is assumed 
3839f 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e  to contain an in
383a0 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66  teger..** This f
383a1 75 6e 63 74 69 6f 6e 20 74 65 73 74 73 20 69 66  unction tests if
383a2 20 74 68 65 72 65 20 65 78 69 73 74 20 61 6e 79   there exist any
383a3 20 64 6f 63 75 6d 65 6e 74 73 20 77 69 74 68 20   documents with 
383a4 64 6f 63 69 64 20 76 61 6c 75 65 73 20 74 68 61  docid values tha
383a5 74 0a 2a 2a 20 61 72 65 20 64 69 66 66 65 72 65  t.** are differe
383a6 6e 74 20 66 72 6f 6d 20 74 68 61 74 20 69 6e 74  nt from that int
383a7 65 67 65 72 2e 20 69 2e 65 2e 20 69 66 20 64 65  eger. i.e. if de
383a8 6c 65 74 69 6e 67 20 74 68 65 20 64 6f 63 75 6d  leting the docum
383a9 65 6e 74 20 77 69 74 68 20 64 6f 63 69 64 0a 2a  ent with docid.*
383aa 2a 20 61 70 56 61 6c 5b 30 5d 20 77 6f 75 6c 64  * apVal[0] would
383ab 20 6d 65 61 6e 20 74 68 65 20 46 54 53 33 20 74   mean the FTS3 t
383ac 61 62 6c 65 20 77 65 72 65 20 65 6d 70 74 79 2e  able were empty.
383ad 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
383ae 73 66 75 6c 2c 20 2a 70 69 73 45 6d 70 74 79 20  sful, *pisEmpty 
383af 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
383b0 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
383b1 6d 70 74 79 20 65 78 63 65 70 74 20 66 6f 72 0a  mpty except for.
383b2 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 61 70 56 61  ** document apVa
383b3 6c 5b 30 5d 2c 20 6f 72 20 66 61 6c 73 65 20 6f  l[0], or false o
383b4 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 53 51  therwise, and SQ
383b5 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
383b6 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72  ned. If an.** er
383b7 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53  ror occurs, an S
383b8 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
383b9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
383ba 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
383bb 49 73 45 6d 70 74 79 28 46 74 73 33 54 61 62 6c  IsEmpty(Fts3Tabl
383bc 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61  e *p, sqlite3_va
383bd 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20 69 6e 74  lue **apVal, int
383be 20 2a 70 69 73 45 6d 70 74 79 29 7b 0a 20 20 73   *pisEmpty){.  s
383bf 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
383c0 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  mt;.  int rc;.  
383c1 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
383c2 28 70 2c 20 53 51 4c 5f 49 53 5f 45 4d 50 54 59  (p, SQL_IS_EMPTY
383c3 2c 20 26 70 53 74 6d 74 2c 20 61 70 56 61 6c 29  , &pStmt, apVal)
383c4 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
383c5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
383c6 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
383c7 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
383c8 20 29 7b 0a 20 20 20 20 20 20 2a 70 69 73 45 6d   ){.      *pisEm
383c9 70 74 79 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  pty = sqlite3_co
383ca 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20  lumn_int(pStmt, 
383cb 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  0);.    }.    rc
383cc 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
383cd 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 72  (pStmt);.  }.  r
383ce 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
383cf 2a 2a 20 53 65 74 20 2a 70 6e 53 65 67 6d 65 6e  ** Set *pnSegmen
383d0 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
383d1 6f 66 20 73 65 67 6d 65 6e 74 73 20 6f 66 20 6c  of segments of l
383d2 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 20 74  evel iLevel in t
383d3 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
383d4 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
383d5 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
383d6 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  l, or an SQLite 
383d7 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6e 6f  error code if no
383d8 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
383d9 20 66 74 73 33 53 65 67 6d 65 6e 74 43 6f 75 6e   fts3SegmentCoun
383da 74 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  t(Fts3Table *p, 
383db 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20  int iLevel, int 
383dc 2a 70 6e 53 65 67 6d 65 6e 74 29 7b 0a 20 20 73  *pnSegment){.  s
383dd 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
383de 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  mt;.  int rc;.. 
383df 20 61 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3e   assert( iLevel>
383e0 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 66 74 73  =0 );.  rc = fts
383e1 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
383e2 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 43 4f 55  SELECT_LEVEL_COU
383e3 4e 54 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  NT, &pStmt, 0);.
383e4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
383e5 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
383e6 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
383e7 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20 69 4c  int(pStmt, 1, iL
383e8 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 53 51 4c  evel);.  if( SQL
383e9 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
383ea 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
383eb 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d      *pnSegment =
383ec 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
383ed 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  int(pStmt, 0);. 
383ee 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   }.  return sqli
383ef 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29  te3_reset(pStmt)
383f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a  ;.}../*.** Set *
383f1 70 6e 53 65 67 6d 65 6e 74 20 74 6f 20 74 68 65  pnSegment to the
383f2 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
383f3 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65   segments in the
383f4 20 64 61 74 61 62 61 73 65 2e 20 53 65 74 0a 2a   database. Set.*
383f5 2a 20 2a 70 6e 4d 61 78 20 74 6f 20 74 68 65 20  * *pnMax to the 
383f6 6c 61 72 67 65 73 74 20 73 65 67 6d 65 6e 74 20  largest segment 
383f7 6c 65 76 65 6c 20 69 6e 20 74 68 65 20 64 61 74  level in the dat
383f8 61 62 61 73 65 20 28 73 65 67 6d 65 6e 74 20 6c  abase (segment l
383f9 65 76 65 6c 73 0a 2a 2a 20 61 72 65 20 73 74 6f  evels.** are sto
383fa 72 65 64 20 69 6e 20 74 68 65 20 27 6c 65 76 65  red in the 'leve
383fb 6c 27 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  l' column of the
383fc 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65 29   %_segdir table)
383fd 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
383fe 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63  QLITE_OK if succ
383ff 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51  essful, or an SQ
38400 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
38401 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  if not..*/.stati
38402 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e  c int fts3Segmen
38403 74 43 6f 75 6e 74 4d 61 78 28 46 74 73 33 54 61  tCountMax(Fts3Ta
38404 62 6c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 53  ble *p, int *pnS
38405 65 67 6d 65 6e 74 2c 20 69 6e 74 20 2a 70 6e 4d  egment, int *pnM
38406 61 78 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ax){.  sqlite3_s
38407 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e  tmt *pStmt;.  in
38408 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 66 74  t rc;..  rc = ft
38409 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c  s3SqlStmt(p, SQL
3840a 5f 53 45 4c 45 43 54 5f 53 45 47 44 49 52 5f 43  _SELECT_SEGDIR_C
3840b 4f 55 4e 54 5f 4d 41 58 2c 20 26 70 53 74 6d 74  OUNT_MAX, &pStmt
3840c 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
3840d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3840e 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c  rn rc;.  if( SQL
3840f 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
38410 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
38411 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d      *pnSegment =
38412 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
38413 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  int(pStmt, 0);. 
38414 20 20 20 2a 70 6e 4d 61 78 20 3d 20 73 71 6c 69     *pnMax = sqli
38415 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70  te3_column_int(p
38416 53 74 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  Stmt, 1);.  }.  
38417 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72  return sqlite3_r
38418 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  eset(pStmt);.}..
38419 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3841a 69 6f 6e 20 69 73 20 75 73 65 64 20 61 66 74 65  ion is used afte
3841b 72 20 6d 65 72 67 69 6e 67 20 6d 75 6c 74 69 70  r merging multip
3841c 6c 65 20 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f  le segments into
3841d 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 0a   a single large.
3841e 2a 2a 20 73 65 67 6d 65 6e 74 20 74 6f 20 64 65  ** segment to de
3841f 6c 65 74 65 20 74 68 65 20 6f 6c 64 2c 20 6e 6f  lete the old, no
38420 77 20 72 65 64 75 6e 64 61 6e 74 2c 20 73 65 67  w redundant, seg
38421 6d 65 6e 74 20 62 2d 74 72 65 65 73 2e 20 53 70  ment b-trees. Sp
38422 65 63 69 66 69 63 61 6c 6c 79 2c 0a 2a 2a 20 69  ecifically,.** i
38423 74 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 31 29 20 44  t:.** .**   1) D
38424 65 6c 65 74 65 73 20 61 6c 6c 20 25 5f 73 65 67  eletes all %_seg
38425 6d 65 6e 74 73 20 65 6e 74 72 69 65 73 20 66 6f  ments entries fo
38426 72 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 61  r the segments a
38427 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a  ssociated with .
38428 2a 2a 20 20 20 20 20 20 65 61 63 68 20 6f 66 20  **      each of 
38429 74 68 65 20 53 65 67 52 65 61 64 65 72 20 6f 62  the SegReader ob
3842a 6a 65 63 74 73 20 69 6e 20 74 68 65 20 61 72 72  jects in the arr
3842b 61 79 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ay passed as the
3842c 20 74 68 69 72 64 20 0a 2a 2a 20 20 20 20 20 20   third .**      
3842d 61 72 67 75 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a  argument, and.**
3842e 0a 2a 2a 20 20 20 32 29 20 64 65 6c 65 74 65 73  .**   2) deletes
3842f 20 61 6c 6c 20 25 5f 73 65 67 64 69 72 20 65 6e   all %_segdir en
38430 74 72 69 65 73 20 77 69 74 68 20 6c 65 76 65 6c  tries with level
38431 20 69 4c 65 76 65 6c 2c 20 6f 72 20 61 6c 6c 20   iLevel, or all 
38432 25 5f 73 65 67 64 69 72 0a 2a 2a 20 20 20 20 20  %_segdir.**     
38433 20 65 6e 74 72 69 65 73 20 72 65 67 61 72 64 6c   entries regardl
38434 65 73 73 20 6f 66 20 6c 65 76 65 6c 20 69 66 20  ess of level if 
38435 28 69 4c 65 76 65 6c 3c 30 29 2e 0a 2a 2a 0a 2a  (iLevel<0)..**.*
38436 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
38437 65 74 75 72 6e 65 64 20 69 66 20 73 75 63 63 65  eturned if succe
38438 73 73 66 75 6c 2c 20 6f 74 68 65 72 77 69 73 65  ssful, otherwise
38439 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3843a 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
3843b 20 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 53   int fts3DeleteS
3843c 65 67 64 69 72 28 0a 20 20 46 74 73 33 54 61 62  egdir(.  Fts3Tab
3843d 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
3843e 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
3843f 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
38440 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c   */.  int iLevel
38441 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
38442 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 6f        /* Level o
38443 66 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72 69  f %_segdir entri
38444 65 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  es to delete */.
38445 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
38446 2a 2a 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20  **apSegment,    
38447 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 65    /* Array of Se
38448 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20  gReader objects 
38449 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72  */.  int nReader
3844a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3844b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
3844c 61 72 72 61 79 20 61 70 53 65 67 6d 65 6e 74 20  array apSegment 
3844d 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
3844e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3844f 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
38450 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n Code */.  int 
38451 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
38452 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
38453 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
38454 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
38455 6d 74 20 2a 70 44 65 6c 65 74 65 3b 20 20 20 20  mt *pDelete;    
38456 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
38457 74 65 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74 65  tement to delete
38458 20 72 6f 77 73 20 2a 2f 0a 0a 20 20 72 63 20 3d   rows */..  rc =
38459 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
3845a 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 4d 45  SQL_DELETE_SEGME
3845b 4e 54 53 5f 52 41 4e 47 45 2c 20 26 70 44 65 6c  NTS_RANGE, &pDel
3845c 65 74 65 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69  ete, 0);.  for(i
3845d 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
3845e 4b 20 26 26 20 69 3c 6e 52 65 61 64 65 72 3b 20  K && i<nReader; 
3845f 69 2b 2b 29 7b 0a 20 20 20 20 46 74 73 33 53 65  i++){.    Fts3Se
38460 67 52 65 61 64 65 72 20 2a 70 53 65 67 6d 65 6e  gReader *pSegmen
38461 74 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d  t = apSegment[i]
38462 3b 0a 20 20 20 20 69 66 28 20 70 53 65 67 6d 65  ;.    if( pSegme
38463 6e 74 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 20  nt->iStartBlock 
38464 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
38465 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c  _bind_int64(pDel
38466 65 74 65 2c 20 31 2c 20 70 53 65 67 6d 65 6e 74  ete, 1, pSegment
38467 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 29 3b 0a  ->iStartBlock);.
38468 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
38469 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74 65  nd_int64(pDelete
3846a 2c 20 32 2c 20 70 53 65 67 6d 65 6e 74 2d 3e 69  , 2, pSegment->i
3846b 45 6e 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  EndBlock);.     
3846c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44   sqlite3_step(pD
3846d 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63  elete);.      rc
3846e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
3846f 28 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d  (pDelete);.    }
38470 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
38471 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38472 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
38473 20 20 69 66 28 20 69 4c 65 76 65 6c 3e 3d 30 20    if( iLevel>=0 
38474 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  ){.    rc = fts3
38475 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44  SqlStmt(p, SQL_D
38476 45 4c 45 54 45 5f 53 45 47 44 49 52 5f 42 59 5f  ELETE_SEGDIR_BY_
38477 4c 45 56 45 4c 2c 20 26 70 44 65 6c 65 74 65 2c  LEVEL, &pDelete,
38478 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
38479 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3847a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
3847b 5f 69 6e 74 28 70 44 65 6c 65 74 65 2c 20 31 2c  _int(pDelete, 1,
3847c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20 20 20 20   iLevel);.      
3847d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44 65  sqlite3_step(pDe
3847e 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63 20  lete);.      rc 
3847f 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
38480 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 0a  pDelete);.    }.
38481 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
38482 3d 20 66 74 73 33 53 71 6c 45 78 65 63 28 70 2c  = fts3SqlExec(p,
38483 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f   SQL_DELETE_ALL_
38484 53 45 47 44 49 52 2c 20 30 29 3b 0a 20 20 7d 0a  SEGDIR, 0);.  }.
38485 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
38486 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
38487 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
38488 6c 65 64 2c 20 62 75 66 66 65 72 20 2a 70 70 4c  led, buffer *ppL
38489 69 73 74 20 28 73 69 7a 65 20 2a 70 6e 4c 69 73  ist (size *pnLis
3848a 74 20 62 79 74 65 73 29 20 63 6f 6e 74 61 69 6e  t bytes) contain
3848b 73 20 0a 2a 2a 20 61 20 70 6f 73 69 74 69 6f 6e  s .** a position
3848c 20 6c 69 73 74 20 74 68 61 74 20 6d 61 79 20 28   list that may (
3848d 6f 72 20 6d 61 79 20 6e 6f 74 29 20 66 65 61 74  or may not) feat
3848e 75 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6c  ure multiple col
3848f 75 6d 6e 73 2e 20 54 68 69 73 0a 2a 2a 20 66 75  umns. This.** fu
38490 6e 63 74 69 6f 6e 20 61 64 6a 75 73 74 73 20 74  nction adjusts t
38491 68 65 20 70 6f 69 6e 74 65 72 20 2a 70 70 4c 69  he pointer *ppLi
38492 73 74 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74  st and the lengt
38493 68 20 2a 70 6e 4c 69 73 74 20 73 6f 20 74 68 61  h *pnList so tha
38494 74 20 74 68 65 79 0a 2a 2a 20 69 64 65 6e 74 69  t they.** identi
38495 66 79 20 74 68 65 20 73 75 62 73 65 74 20 6f 66  fy the subset of
38496 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69   the position li
38497 73 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  st that correspo
38498 6e 64 73 20 74 6f 20 63 6f 6c 75 6d 6e 20 69 43  nds to column iC
38499 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ol..**.** If the
3849a 72 65 20 61 72 65 20 6e 6f 20 65 6e 74 72 69 65  re are no entrie
3849b 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 70  s in the input p
3849c 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
3849d 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 2c 20 74 68   column iCol, th
3849e 65 6e 0a 2a 2a 20 2a 70 6e 4c 69 73 74 20 69 73  en.** *pnList is
3849f 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 62 65 66   set to zero bef
384a0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
384a1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
384a2 73 33 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 0a  s3ColumnFilter(.
384a3 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20    int iCol,     
384a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
384a5 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 66    /* Column to f
384a6 69 6c 74 65 72 20 6f 6e 20 2a 2f 0a 20 20 63 68  ilter on */.  ch
384a7 61 72 20 2a 2a 70 70 4c 69 73 74 2c 20 20 20 20  ar **ppList,    
384a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
384a9 20 49 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72   IN/OUT: Pointer
384aa 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   to position lis
384ab 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4c 69  t */.  int *pnLi
384ac 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
384ad 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
384ae 3a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  : Size of buffer
384af 20 2a 70 70 4c 69 73 74 20 69 6e 20 62 79 74 65   *ppList in byte
384b0 73 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  s */.){.  char *
384b1 70 4c 69 73 74 20 3d 20 2a 70 70 4c 69 73 74 3b  pList = *ppList;
384b2 0a 20 20 69 6e 74 20 6e 4c 69 73 74 20 3d 20 2a  .  int nList = *
384b3 70 6e 4c 69 73 74 3b 0a 20 20 63 68 61 72 20 2a  pnList;.  char *
384b4 70 45 6e 64 20 3d 20 26 70 4c 69 73 74 5b 6e 4c  pEnd = &pList[nL
384b5 69 73 74 5d 3b 0a 20 20 69 6e 74 20 69 43 75 72  ist];.  int iCur
384b6 72 65 6e 74 20 3d 20 30 3b 0a 20 20 63 68 61 72  rent = 0;.  char
384b7 20 2a 70 20 3d 20 70 4c 69 73 74 3b 0a 0a 20 20   *p = pList;..  
384b8 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
384b9 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  );.  while( 1 ){
384ba 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20 30 3b  .    char c = 0;
384bb 0a 20 20 20 20 77 68 69 6c 65 28 20 70 3c 70 45  .    while( p<pE
384bc 6e 64 20 26 26 20 28 63 20 7c 20 2a 70 29 26 30  nd && (c | *p)&0
384bd 78 46 45 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26  xFE ) c = *p++ &
384be 20 30 78 38 30 3b 0a 20 20 0a 20 20 20 20 69 66   0x80;.  .    if
384bf 28 20 69 43 6f 6c 3d 3d 69 43 75 72 72 65 6e 74  ( iCol==iCurrent
384c0 20 29 7b 0a 20 20 20 20 20 20 6e 4c 69 73 74 20   ){.      nList 
384c1 3d 20 28 69 6e 74 29 28 70 20 2d 20 70 4c 69 73  = (int)(p - pLis
384c2 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
384c3 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 69 73  .    }..    nLis
384c4 74 20 2d 3d 20 28 69 6e 74 29 28 70 20 2d 20 70  t -= (int)(p - p
384c5 4c 69 73 74 29 3b 0a 20 20 20 20 70 4c 69 73 74  List);.    pList
384c6 20 3d 20 70 3b 0a 20 20 20 20 69 66 28 20 6e 4c   = p;.    if( nL
384c7 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist==0 ){.      
384c8 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
384c9 20 70 20 3d 20 26 70 4c 69 73 74 5b 31 5d 3b 0a   p = &pList[1];.
384ca 20 20 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33      p += sqlite3
384cb 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
384cc 70 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  p, &iCurrent);. 
384cd 20 7d 0a 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20   }..  *ppList = 
384ce 70 4c 69 73 74 3b 0a 20 20 2a 70 6e 4c 69 73 74  pList;.  *pnList
384cf 20 3d 20 6e 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a   = nList;.}../*.
384d0 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  ** sqlite3Fts3Se
384d1 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28 29  gReaderIterate()
384d2 20 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 77   callback used w
384d3 68 65 6e 20 6d 65 72 67 69 6e 67 20 6d 75 6c 74  hen merging mult
384d4 69 70 6c 65 20 0a 2a 2a 20 73 65 67 6d 65 6e 74  iple .** segment
384d5 73 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 69  s to create a si
384d6 6e 67 6c 65 2c 20 6c 61 72 67 65 72 20 73 65 67  ngle, larger seg
384d7 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
384d8 69 6e 74 20 66 74 73 33 4d 65 72 67 65 43 61 6c  int fts3MergeCal
384d9 6c 62 61 63 6b 28 0a 20 20 46 74 73 33 54 61 62  lback(.  Fts3Tab
384da 6c 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70  le *p,.  void *p
384db 43 6f 6e 74 65 78 74 2c 0a 20 20 63 68 61 72 20  Context,.  char 
384dc 2a 7a 54 65 72 6d 2c 0a 20 20 69 6e 74 20 6e 54  *zTerm,.  int nT
384dd 65 72 6d 2c 0a 20 20 63 68 61 72 20 2a 61 44 6f  erm,.  char *aDo
384de 63 6c 69 73 74 2c 0a 20 20 69 6e 74 20 6e 44 6f  clist,.  int nDo
384df 63 6c 69 73 74 0a 29 7b 0a 20 20 53 65 67 6d 65  clist.){.  Segme
384e0 6e 74 57 72 69 74 65 72 20 2a 2a 70 70 57 20 3d  ntWriter **ppW =
384e1 20 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20   (SegmentWriter 
384e2 2a 2a 29 70 43 6f 6e 74 65 78 74 3b 0a 20 20 72  **)pContext;.  r
384e3 65 74 75 72 6e 20 66 74 73 33 53 65 67 57 72 69  eturn fts3SegWri
384e4 74 65 72 41 64 64 28 70 2c 20 70 70 57 2c 20 31  terAdd(p, ppW, 1
384e5 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , zTerm, nTerm, 
384e6 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69  aDoclist, nDocli
384e7 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
384e8 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
384e9 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74  sed to iterate t
384ea 68 72 6f 75 67 68 20 61 20 63 6f 6e 74 69 67 75  hrough a contigu
384eb 6f 75 73 20 73 65 74 20 6f 66 20 74 65 72 6d 73  ous set of terms
384ec 20 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74   .** stored in t
384ed 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64  he full-text ind
384ee 65 78 2e 20 49 74 20 6d 65 72 67 65 73 20 64 61  ex. It merges da
384ef 74 61 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ta contained in 
384f0 6f 6e 65 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20  one or .** more 
384f1 73 65 67 6d 65 6e 74 73 20 74 6f 20 73 75 70 70  segments to supp
384f2 6f 72 74 20 74 68 69 73 2e 0a 2a 2a 0a 2a 2a 20  ort this..**.** 
384f3 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
384f4 65 6e 74 20 69 73 20 70 61 73 73 65 64 20 61 6e  ent is passed an
384f5 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65   array of pointe
384f6 72 73 20 74 6f 20 53 65 67 52 65 61 64 65 72 20  rs to SegReader 
384f7 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 6c 6c 6f 63  objects.** alloc
384f8 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 65  ated with sqlite
384f9 33 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65  3Fts3SegReaderNe
384fa 77 28 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69  w(). This functi
384fb 6f 6e 20 6d 65 72 67 65 73 20 74 68 65 20 72 61  on merges the ra
384fc 6e 67 65 20 0a 2a 2a 20 6f 66 20 74 65 72 6d 73  nge .** of terms
384fd 20 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63   selected by eac
384fe 68 20 53 65 67 52 65 61 64 65 72 2e 20 49 66 20  h SegReader. If 
384ff 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 73  a single term is
38500 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 6d   present in.** m
38501 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 65 67  ore than one seg
38502 6d 65 6e 74 2c 20 74 68 65 20 61 73 73 6f 63 69  ment, the associ
38503 61 74 65 64 20 64 6f 63 6c 69 73 74 73 20 61 72  ated doclists ar
38504 65 20 6d 65 72 67 65 64 2e 20 46 6f 72 20 65 61  e merged. For ea
38505 63 68 0a 2a 2a 20 74 65 72 6d 20 61 6e 64 20 28  ch.** term and (
38506 70 6f 73 73 69 62 6c 79 20 6d 65 72 67 65 64 29  possibly merged)
38507 20 64 6f 63 6c 69 73 74 20 69 6e 20 74 68 65 20   doclist in the 
38508 6d 65 72 67 65 64 20 72 61 6e 67 65 2c 20 74 68  merged range, th
38509 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75  e callback.** fu
3850a 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 69 73 20  nction xFunc is 
3850b 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 69 74 73  invoked with its
3850c 20 61 72 67 75 6d 65 6e 74 73 20 73 65 74 20 61   arguments set a
3850d 73 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 0a 2a 2a  s follows..**.**
3850e 20 20 20 61 72 67 20 30 3a 20 43 6f 70 79 20 6f     arg 0: Copy o
3850f 66 20 27 70 27 20 70 61 72 61 6d 65 74 65 72 20  f 'p' parameter 
38510 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
38511 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 61 72 67  unction.**   arg
38512 20 31 3a 20 43 6f 70 79 20 6f 66 20 27 70 43 6f   1: Copy of 'pCo
38513 6e 74 65 78 74 27 20 70 61 72 61 6d 65 74 65 72  ntext' parameter
38514 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
38515 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 61 72  function.**   ar
38516 67 20 32 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  g 2: Pointer to 
38517 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
38518 67 20 74 65 72 6d 0a 2a 2a 20 20 20 61 72 67 20  g term.**   arg 
38519 33 3a 20 53 69 7a 65 20 6f 66 20 61 72 67 20 32  3: Size of arg 2
3851a 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73   buffer in bytes
3851b 0a 2a 2a 20 20 20 61 72 67 20 34 3a 20 50 6f 69  .**   arg 4: Poi
3851c 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63  nter to buffer c
3851d 6f 6e 74 61 69 6e 69 6e 67 20 64 6f 63 6c 69 73  ontaining doclis
3851e 74 0a 2a 2a 20 20 20 61 72 67 20 35 3a 20 53 69  t.**   arg 5: Si
3851f 7a 65 20 6f 66 20 61 72 67 20 32 20 62 75 66 66  ze of arg 2 buff
38520 65 72 20 69 6e 20 62 79 74 65 73 0a 2a 2a 0a 2a  er in bytes.**.*
38521 2a 20 54 68 65 20 34 74 68 20 61 72 67 75 6d 65  * The 4th argume
38522 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
38523 69 6f 6e 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ion is a pointer
38524 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
38525 6f 66 20 74 79 70 65 0a 2a 2a 20 46 74 73 33 53  of type.** Fts3S
38526 65 67 46 69 6c 74 65 72 2c 20 64 65 66 69 6e 65  egFilter, define
38527 64 20 69 6e 20 66 74 73 33 49 6e 74 2e 68 2e 20  d in fts3Int.h. 
38528 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
38529 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
3852a 2a 20 66 75 72 74 68 65 72 20 72 65 73 74 72 69  * further restri
3852b 63 74 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  ct the range of 
3852c 74 65 72 6d 73 20 74 68 61 74 20 63 61 6c 6c 62  terms that callb
3852d 61 63 6b 73 20 61 72 65 20 6d 61 64 65 20 66 6f  acks are made fo
3852e 72 20 61 6e 64 0a 2a 2a 20 6d 6f 64 69 66 79 20  r and.** modify 
3852f 74 68 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66  the behaviour of
38530 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
38531 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
38532 76 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ve structure.** 
38533 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20 64  definition for d
38534 65 74 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54  etails..*/.SQLIT
38535 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
38536 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
38537 65 72 49 74 65 72 61 74 65 28 0a 20 20 46 74 73  erIterate(.  Fts
38538 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
38539 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3853a 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
3853b 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 53 65  ndle */.  Fts3Se
3853c 67 52 65 61 64 65 72 20 2a 2a 61 70 53 65 67 6d  gReader **apSegm
3853d 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 41 72 72  ent,      /* Arr
3853e 61 79 20 6f 66 20 46 74 73 33 53 65 67 52 65 61  ay of Fts3SegRea
3853f 64 65 72 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20  der objects */. 
38540 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 2c 20 20   int nSegment,  
38541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38542 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 70 53 65   /* Size of apSe
38543 67 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20  gment array */. 
38544 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 2a   Fts3SegFilter *
38545 70 46 69 6c 74 65 72 2c 20 20 20 20 20 20 20 20  pFilter,        
38546 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 73   /* Restrictions
38547 20 6f 6e 20 72 61 6e 67 65 20 6f 66 20 69 74 65   on range of ite
38548 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
38549 28 2a 78 46 75 6e 63 29 28 46 74 73 33 54 61 62  (*xFunc)(Fts3Tab
3854a 6c 65 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 68  le *, void *, ch
3854b 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20  ar *, int, char 
3854c 2a 2c 20 69 6e 74 29 2c 20 20 2f 2a 20 43 61 6c  *, int),  /* Cal
3854d 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20  lback */.  void 
3854e 2a 70 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20  *pContext       
3854f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
38550 6c 6c 62 61 63 6b 20 63 6f 6e 74 65 78 74 20 28  llback context (
38551 32 6e 64 20 61 72 67 75 6d 65 6e 74 29 20 2a 2f  2nd argument) */
38552 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
38553 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38554 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
38555 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
38556 63 68 61 72 20 2a 61 42 75 66 66 65 72 20 3d 20  char *aBuffer = 
38557 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
38558 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 6d 65 72  /* Buffer to mer
38559 67 65 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 2a  ge doclists in *
3855a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d  /.  int nAlloc =
3855b 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3855c 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
3855d 20 73 69 7a 65 20 6f 66 20 61 42 75 66 66 65 72   size of aBuffer
3855e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
3855f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
38560 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38561 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a  Return code */..
38562 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 45 6d    int isIgnoreEm
38563 70 74 79 20 3d 20 20 28 70 46 69 6c 74 65 72 2d  pty =  (pFilter-
38564 3e 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45  >flags & FTS3_SE
38565 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50  GMENT_IGNORE_EMP
38566 54 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  TY);.  int isReq
38567 75 69 72 65 50 6f 73 20 3d 20 20 20 28 70 46 69  uirePos =   (pFi
38568 6c 74 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54  lter->flags & FT
38569 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49  S3_SEGMENT_REQUI
3856a 52 45 5f 50 4f 53 29 3b 0a 20 20 69 6e 74 20 69  RE_POS);.  int i
3856b 73 43 6f 6c 46 69 6c 74 65 72 20 3d 20 20 20 20  sColFilter =    
3856c 28 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20  (pFilter->flags 
3856d 26 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43  & FTS3_SEGMENT_C
3856e 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52 29 3b 0a 20  OLUMN_FILTER);. 
3856f 20 69 6e 74 20 69 73 50 72 65 66 69 78 20 3d 20   int isPrefix = 
38570 20 20 20 20 20 20 28 70 46 69 6c 74 65 72 2d 3e        (pFilter->
38571 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45 47  flags & FTS3_SEG
38572 4d 45 4e 54 5f 50 52 45 46 49 58 29 3b 0a 0a 20  MENT_PREFIX);.. 
38573 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
38574 20 7a 65 72 6f 20 73 65 67 6d 65 6e 74 73 2c 20   zero segments, 
38575 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
38576 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 69 73 20 73   a no-op. This s
38577 63 65 6e 61 72 69 6f 0a 20 20 2a 2a 20 63 6f 6d  cenario.  ** com
38578 65 73 20 61 62 6f 75 74 20 6f 6e 6c 79 20 77 68  es about only wh
38579 65 6e 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20  en reading from 
3857a 61 6e 20 65 6d 70 74 79 20 64 61 74 61 62 61 73  an empty databas
3857b 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53  e..  */.  if( nS
3857c 65 67 6d 65 6e 74 3d 3d 30 20 29 20 67 6f 74 6f  egment==0 ) goto
3857d 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20 20 2f 2a   finished;..  /*
3857e 20 49 66 20 74 68 65 20 46 74 73 33 53 65 67 46   If the Fts3SegF
3857f 69 6c 74 65 72 20 64 65 66 69 6e 65 73 20 61 20  ilter defines a 
38580 73 70 65 63 69 66 69 63 20 74 65 72 6d 20 28 6f  specific term (o
38581 72 20 74 65 72 6d 20 70 72 65 66 69 78 29 20 74  r term prefix) t
38582 6f 20 73 65 61 72 63 68 20 0a 20 20 2a 2a 20 66  o search .  ** f
38583 6f 72 2c 20 74 68 65 6e 20 61 64 76 61 6e 63 65  or, then advance
38584 20 65 61 63 68 20 73 65 67 6d 65 6e 74 20 69 74   each segment it
38585 65 72 61 74 6f 72 20 75 6e 74 69 6c 20 69 74 20  erator until it 
38586 70 6f 69 6e 74 73 20 74 6f 20 61 20 74 65 72 6d  points to a term
38587 20 6f 66 0a 20 20 2a 2a 20 65 71 75 61 6c 20 6f   of.  ** equal o
38588 72 20 67 72 65 61 74 65 72 20 76 61 6c 75 65 20  r greater value 
38589 74 68 61 6e 20 74 68 65 20 73 70 65 63 69 66 69  than the specifi
3858a 65 64 20 74 65 72 6d 2e 20 54 68 69 73 20 70 72  ed term. This pr
3858b 65 76 65 6e 74 73 20 6d 61 6e 79 0a 20 20 2a 2a  events many.  **
3858c 20 75 6e 6e 65 63 65 73 73 61 72 79 20 6d 65 72   unnecessary mer
3858d 67 65 2f 73 6f 72 74 20 6f 70 65 72 61 74 69 6f  ge/sort operatio
3858e 6e 73 20 66 6f 72 20 74 68 65 20 63 61 73 65 20  ns for the case 
3858f 77 68 65 72 65 20 73 69 6e 67 6c 65 20 73 65 67  where single seg
38590 6d 65 6e 74 0a 20 20 2a 2a 20 62 2d 74 72 65 65  ment.  ** b-tree
38591 20 6c 65 61 66 20 6e 6f 64 65 73 20 63 6f 6e 74   leaf nodes cont
38592 61 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ain more than on
38593 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  e term..  */.  i
38594 66 28 20 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72  f( pFilter->zTer
38595 6d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 54 65  m ){.    int nTe
38596 72 6d 20 3d 20 70 46 69 6c 74 65 72 2d 3e 6e 54  rm = pFilter->nT
38597 65 72 6d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  erm;.    const c
38598 68 61 72 20 2a 7a 54 65 72 6d 20 3d 20 70 46 69  har *zTerm = pFi
38599 6c 74 65 72 2d 3e 7a 54 65 72 6d 3b 0a 20 20 20  lter->zTerm;.   
3859a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 65 67   for(i=0; i<nSeg
3859b 6d 65 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ment; i++){.    
3859c 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
3859d 2a 70 53 65 67 20 3d 20 61 70 53 65 67 6d 65 6e  *pSeg = apSegmen
3859e 74 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c  t[i];.      whil
3859f 65 28 20 66 74 73 33 53 65 67 52 65 61 64 65 72  e( fts3SegReader
385a0 54 65 72 6d 43 6d 70 28 70 53 65 67 2c 20 7a 54  TermCmp(pSeg, zT
385a1 65 72 6d 2c 20 6e 54 65 72 6d 29 3c 30 20 29 7b  erm, nTerm)<0 ){
385a2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74  .        rc = ft
385a3 73 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 28  s3SegReaderNext(
385a4 70 53 65 67 29 3b 0a 20 20 20 20 20 20 20 20 69  pSeg);.        i
385a5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
385a6 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64   ) goto finished
385a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
385a8 20 20 7d 0a 0a 20 20 66 74 73 33 53 65 67 52 65    }..  fts3SegRe
385a9 61 64 65 72 53 6f 72 74 28 61 70 53 65 67 6d 65  aderSort(apSegme
385aa 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 20 6e 53  nt, nSegment, nS
385ab 65 67 6d 65 6e 74 2c 20 66 74 73 33 53 65 67 52  egment, fts3SegR
385ac 65 61 64 65 72 43 6d 70 29 3b 0a 20 20 77 68 69  eaderCmp);.  whi
385ad 6c 65 28 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  le( apSegment[0]
385ae 2d 3e 61 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69  ->aNode ){.    i
385af 6e 74 20 6e 54 65 72 6d 20 3d 20 61 70 53 65 67  nt nTerm = apSeg
385b0 6d 65 6e 74 5b 30 5d 2d 3e 6e 54 65 72 6d 3b 0a  ment[0]->nTerm;.
385b1 20 20 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 20      char *zTerm 
385b2 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e  = apSegment[0]->
385b3 7a 54 65 72 6d 3b 0a 20 20 20 20 69 6e 74 20 6e  zTerm;.    int n
385b4 4d 65 72 67 65 20 3d 20 31 3b 0a 0a 20 20 20 20  Merge = 1;..    
385b5 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
385b6 70 72 65 66 69 78 2d 73 65 61 72 63 68 2c 20 61  prefix-search, a
385b7 6e 64 20 69 66 20 74 68 65 20 74 65 72 6d 20 74  nd if the term t
385b8 68 61 74 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  hat apSegment[0]
385b9 20 70 6f 69 6e 74 73 0a 20 20 20 20 2a 2a 20 74   points.    ** t
385ba 6f 20 64 6f 65 73 20 6e 6f 74 20 73 68 61 72 65  o does not share
385bb 20 61 20 73 75 66 66 69 78 20 77 69 74 68 20 70   a suffix with p
385bc 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 2f 6e 54  Filter->zTerm/nT
385bd 65 72 6d 2c 20 74 68 65 6e 20 61 6c 6c 20 0a 20  erm, then all . 
385be 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 63     ** required c
385bf 61 6c 6c 62 61 63 6b 73 20 68 61 76 65 20 62 65  allbacks have be
385c0 65 6e 20 6d 61 64 65 2e 20 49 6e 20 74 68 69 73  en made. In this
385c1 20 63 61 73 65 20 65 78 69 74 20 65 61 72 6c 79   case exit early
385c2 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
385c3 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68  Similarly, if th
385c4 69 73 20 69 73 20 61 20 73 65 61 72 63 68 20 66  is is a search f
385c5 6f 72 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63  or an exact matc
385c6 68 2c 20 61 6e 64 20 74 68 65 20 66 69 72 73 74  h, and the first
385c7 20 74 65 72 6d 0a 20 20 20 20 2a 2a 20 6f 66 20   term.    ** of 
385c8 73 65 67 6d 65 6e 74 20 61 70 53 65 67 6d 65 6e  segment apSegmen
385c9 74 5b 30 5d 20 69 73 20 6e 6f 74 20 61 20 6d 61  t[0] is not a ma
385ca 74 63 68 2c 20 65 78 69 74 20 65 61 72 6c 79 2e  tch, exit early.
385cb 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
385cc 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 20 29  pFilter->zTerm )
385cd 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 65 72  {.      if( nTer
385ce 6d 3c 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d  m<pFilter->nTerm
385cf 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 21 69 73   .       || (!is
385d0 50 72 65 66 69 78 20 26 26 20 6e 54 65 72 6d 3e  Prefix && nTerm>
385d1 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29 0a  pFilter->nTerm).
385d2 20 20 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d 70         || memcmp
385d3 28 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d  (zTerm, pFilter-
385d4 3e 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d  >zTerm, pFilter-
385d5 3e 6e 54 65 72 6d 29 20 0a 20 20 20 20 29 7b 0a  >nTerm) .    ){.
385d6 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e          goto fin
385d7 69 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20  ished;.      }. 
385d8 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28     }..    while(
385d9 20 6e 4d 65 72 67 65 3c 6e 53 65 67 6d 65 6e 74   nMerge<nSegment
385da 20 0a 20 20 20 20 20 20 20 20 26 26 20 61 70 53   .        && apS
385db 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d 3e  egment[nMerge]->
385dc 61 4e 6f 64 65 0a 20 20 20 20 20 20 20 20 26 26  aNode.        &&
385dd 20 61 70 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67   apSegment[nMerg
385de 65 5d 2d 3e 6e 54 65 72 6d 3d 3d 6e 54 65 72 6d  e]->nTerm==nTerm
385df 20 0a 20 20 20 20 20 20 20 20 26 26 20 30 3d 3d   .        && 0==
385e0 6d 65 6d 63 6d 70 28 7a 54 65 72 6d 2c 20 61 70  memcmp(zTerm, ap
385e1 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d  Segment[nMerge]-
385e2 3e 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 0a 20  >zTerm, nTerm). 
385e3 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4d 65 72     ){.      nMer
385e4 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ge++;.    }..   
385e5 20 69 66 28 20 6e 4d 65 72 67 65 3d 3d 31 20 26   if( nMerge==1 &
385e6 26 20 21 69 73 49 67 6e 6f 72 65 45 6d 70 74 79  & !isIgnoreEmpty
385e7 20 26 26 20 21 69 73 43 6f 6c 46 69 6c 74 65 72   && !isColFilter
385e8 20 26 26 20 69 73 52 65 71 75 69 72 65 50 6f 73   && isRequirePos
385e9 20 29 7b 0a 20 20 20 20 20 20 46 74 73 33 53 65   ){.      Fts3Se
385ea 67 52 65 61 64 65 72 20 2a 70 30 20 3d 20 61 70  gReader *p0 = ap
385eb 53 65 67 6d 65 6e 74 5b 30 5d 3b 0a 20 20 20 20  Segment[0];.    
385ec 20 20 72 63 20 3d 20 78 46 75 6e 63 28 70 2c 20    rc = xFunc(p, 
385ed 70 43 6f 6e 74 65 78 74 2c 20 7a 54 65 72 6d 2c  pContext, zTerm,
385ee 20 6e 54 65 72 6d 2c 20 70 30 2d 3e 61 44 6f 63   nTerm, p0->aDoc
385ef 6c 69 73 74 2c 20 70 30 2d 3e 6e 44 6f 63 6c 69  list, p0->nDocli
385f0 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  st);.      if( r
385f1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
385f2 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20  oto finished;.  
385f3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
385f4 6e 74 20 6e 44 6f 63 6c 69 73 74 20 3d 20 30 3b  nt nDoclist = 0;
385f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
385f6 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 2a 2f  ze of doclist */
385f7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
385f8 6e 74 36 34 20 69 50 72 65 76 20 3d 20 30 3b 20  nt64 iPrev = 0; 
385f9 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 64     /* Previous d
385fa 6f 63 69 64 20 73 74 6f 72 65 64 20 69 6e 20 64  ocid stored in d
385fb 6f 63 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20  oclist */..     
385fc 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
385fd 74 65 72 6d 20 6f 66 20 74 68 65 20 66 69 72 73  term of the firs
385fe 74 20 6e 4d 65 72 67 65 20 65 6e 74 72 69 65 73  t nMerge entries
385ff 20 69 6e 20 74 68 65 20 61 72 72 61 79 0a 20 20   in the array.  
38600 20 20 20 20 2a 2a 20 6f 66 20 46 74 73 33 53 65      ** of Fts3Se
38601 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20  gReader objects 
38602 69 73 20 74 68 65 20 73 61 6d 65 2e 20 54 68 65  is the same. The
38603 20 64 6f 63 6c 69 73 74 73 20 6d 75 73 74 20 62   doclists must b
38604 65 20 6d 65 72 67 65 64 0a 20 20 20 20 20 20 2a  e merged.      *
38605 2a 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 20 74  * and a single t
38606 65 72 6d 20 61 64 64 65 64 20 74 6f 20 74 68 65  erm added to the
38607 20 6e 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20   new segment..  
38608 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72      */.      for
38609 28 69 3d 30 3b 20 69 3c 6e 4d 65 72 67 65 3b 20  (i=0; i<nMerge; 
3860a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 74  i++){.        ft
3860b 73 33 53 65 67 52 65 61 64 65 72 46 69 72 73 74  s3SegReaderFirst
3860c 44 6f 63 69 64 28 61 70 53 65 67 6d 65 6e 74 5b  Docid(apSegment[
3860d 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
3860e 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
3860f 53 6f 72 74 28 61 70 53 65 67 6d 65 6e 74 2c 20  Sort(apSegment, 
38610 6e 4d 65 72 67 65 2c 20 6e 4d 65 72 67 65 2c 20  nMerge, nMerge, 
38611 66 74 73 33 53 65 67 52 65 61 64 65 72 44 6f 63  fts3SegReaderDoc
38612 6c 69 73 74 43 6d 70 29 3b 0a 20 20 20 20 20 20  listCmp);.      
38613 77 68 69 6c 65 28 20 61 70 53 65 67 6d 65 6e 74  while( apSegment
38614 5b 30 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74  [0]->pOffsetList
38615 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
38616 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
38617 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
38618 6f 66 20 73 65 67 6d 65 6e 74 73 20 74 68 61 74  of segments that
38619 20 73 68 61 72 65 20 61 20 64 6f 63 69 64 20 2a   share a docid *
3861a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  /.        char *
3861b 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69  pList;.        i
3861c 6e 74 20 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20  nt nList;.      
3861d 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20    int nByte;.   
3861e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
3861f 36 34 20 69 44 6f 63 69 64 20 3d 20 61 70 53 65  64 iDocid = apSe
38620 67 6d 65 6e 74 5b 30 5d 2d 3e 69 44 6f 63 69 64  gment[0]->iDocid
38621 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 65  ;.        fts3Se
38622 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63 69 64  gReaderNextDocid
38623 28 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2c 20 26  (apSegment[0], &
38624 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a  pList, &nList);.
38625 20 20 20 20 20 20 20 20 6a 20 3d 20 31 3b 0a 20          j = 1;. 
38626 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3c         while( j<
38627 6e 4d 65 72 67 65 20 0a 20 20 20 20 20 20 20 20  nMerge .        
38628 20 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e 74      && apSegment
38629 5b 6a 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74  [j]->pOffsetList
3862a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 26   .            &&
3862b 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2d 3e 69   apSegment[j]->i
3862c 44 6f 63 69 64 3d 3d 69 44 6f 63 69 64 20 0a 20  Docid==iDocid . 
3862d 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
3862e 20 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65      fts3SegReade
3862f 72 4e 65 78 74 44 6f 63 69 64 28 61 70 53 65 67  rNextDocid(apSeg
38630 6d 65 6e 74 5b 6a 5d 2c 20 30 2c 20 30 29 3b 0a  ment[j], 0, 0);.
38631 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20            j++;. 
38632 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
38633 20 20 69 66 28 20 69 73 43 6f 6c 46 69 6c 74 65    if( isColFilte
38634 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  r ){.          f
38635 74 73 33 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28  ts3ColumnFilter(
38636 70 46 69 6c 74 65 72 2d 3e 69 43 6f 6c 2c 20 26  pFilter->iCol, &
38637 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a  pList, &nList);.
38638 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
38639 20 20 20 69 66 28 20 21 69 73 49 67 6e 6f 72 65     if( !isIgnore
3863a 45 6d 70 74 79 20 7c 7c 20 6e 4c 69 73 74 3e 30  Empty || nList>0
3863b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 42   ){.          nB
3863c 79 74 65 20 3d 20 73 71 6c 69 74 65 33 46 74 73  yte = sqlite3Fts
3863d 33 56 61 72 69 6e 74 4c 65 6e 28 69 44 6f 63 69  3VarintLen(iDoci
3863e 64 2d 69 50 72 65 76 29 20 2b 20 28 69 73 52 65  d-iPrev) + (isRe
3863f 71 75 69 72 65 50 6f 73 3f 6e 4c 69 73 74 2b 31  quirePos?nList+1
38640 3a 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  :0);.          i
38641 66 28 20 6e 44 6f 63 6c 69 73 74 2b 6e 42 79 74  f( nDoclist+nByt
38642 65 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  e>nAlloc ){.    
38643 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 4e          char *aN
38644 65 77 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ew;.            
38645 6e 41 6c 6c 6f 63 20 3d 20 6e 44 6f 63 6c 69 73  nAlloc = nDoclis
38646 74 2b 6e 42 79 74 65 2a 32 3b 0a 20 20 20 20 20  t+nByte*2;.     
38647 20 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71         aNew = sq
38648 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 61 42  lite3_realloc(aB
38649 75 66 66 65 72 2c 20 6e 41 6c 6c 6f 63 29 3b 0a  uffer, nAlloc);.
3864a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
3864b 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !aNew ){.       
3864c 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
3864d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
3864e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e          goto fin
3864f 69 73 68 65 64 3b 0a 20 20 20 20 20 20 20 20 20  ished;.         
38650 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38651 20 61 42 75 66 66 65 72 20 3d 20 61 4e 65 77 3b   aBuffer = aNew;
38652 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
38653 20 20 20 20 20 20 20 6e 44 6f 63 6c 69 73 74 20         nDoclist 
38654 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  += sqlite3Fts3Pu
38655 74 56 61 72 69 6e 74 28 26 61 42 75 66 66 65 72  tVarint(&aBuffer
38656 5b 6e 44 6f 63 6c 69 73 74 5d 2c 20 69 44 6f 63  [nDoclist], iDoc
38657 69 64 2d 69 50 72 65 76 29 3b 0a 20 20 20 20 20  id-iPrev);.     
38658 20 20 20 20 20 69 50 72 65 76 20 3d 20 69 44 6f       iPrev = iDo
38659 63 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 69  cid;.          i
3865a 66 28 20 69 73 52 65 71 75 69 72 65 50 6f 73 20  f( isRequirePos 
3865b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
3865c 65 6d 63 70 79 28 26 61 42 75 66 66 65 72 5b 6e  emcpy(&aBuffer[n
3865d 44 6f 63 6c 69 73 74 5d 2c 20 70 4c 69 73 74 2c  Doclist], pList,
3865e 20 6e 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20   nList);.       
3865f 20 20 20 20 20 6e 44 6f 63 6c 69 73 74 20 2b 3d       nDoclist +=
38660 20 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20   nList;.        
38661 20 20 20 20 61 42 75 66 66 65 72 5b 6e 44 6f 63      aBuffer[nDoc
38662 6c 69 73 74 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0a  list++] = '\0';.
38663 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
38664 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 66      }..        f
38665 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74  ts3SegReaderSort
38666 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 4d 65 72  (apSegment, nMer
38667 67 65 2c 20 6a 2c 20 66 74 73 33 53 65 67 52 65  ge, j, fts3SegRe
38668 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 29 3b  aderDoclistCmp);
38669 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
3866a 69 66 28 20 6e 44 6f 63 6c 69 73 74 3e 30 20 29  if( nDoclist>0 )
3866b 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78  {.        rc = x
3866c 46 75 6e 63 28 70 2c 20 70 43 6f 6e 74 65 78 74  Func(p, pContext
3866d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , zTerm, nTerm, 
3866e 61 42 75 66 66 65 72 2c 20 6e 44 6f 63 6c 69 73  aBuffer, nDoclis
3866f 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
38670 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
38671 67 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20  goto finished;. 
38672 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
38673 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
38674 20 61 20 74 65 72 6d 20 73 70 65 63 69 66 69 65   a term specifie
38675 64 20 74 6f 20 66 69 6c 74 65 72 20 6f 6e 2c 20  d to filter on, 
38676 61 6e 64 20 74 68 69 73 20 69 73 20 6e 6f 74 20  and this is not 
38677 61 20 70 72 65 66 69 78 0a 20 20 20 20 2a 2a 20  a prefix.    ** 
38678 73 65 61 72 63 68 2c 20 72 65 74 75 72 6e 20 6e  search, return n
38679 6f 77 2e 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  ow. The callback
3867a 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
3867b 73 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65  s to the require
3867c 64 0a 20 20 20 20 2a 2a 20 74 65 72 6d 20 28 69  d.    ** term (i
3867d 66 20 73 75 63 68 20 61 20 74 65 72 6d 20 65 78  f such a term ex
3867e 69 73 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65  ists in the inde
3867f 78 29 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  x) has already b
38680 65 65 6e 20 6d 61 64 65 2e 0a 20 20 20 20 2a 2f  een made..    */
38681 0a 20 20 20 20 69 66 28 20 70 46 69 6c 74 65 72  .    if( pFilter
38682 2d 3e 7a 54 65 72 6d 20 26 26 20 21 69 73 50 72  ->zTerm && !isPr
38683 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20 67 6f  efix ){.      go
38684 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
38685 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
38686 20 69 3c 6e 4d 65 72 67 65 3b 20 69 2b 2b 29 7b   i<nMerge; i++){
38687 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
38688 53 65 67 52 65 61 64 65 72 4e 65 78 74 28 61 70  SegReaderNext(ap
38689 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20 20  Segment[i]);.   
3868a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3868b 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69  E_OK ) goto fini
3868c 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  shed;.    }.    
3868d 66 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72  fts3SegReaderSor
3868e 74 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65  t(apSegment, nSe
3868f 67 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 66  gment, nMerge, f
38690 74 73 33 53 65 67 52 65 61 64 65 72 43 6d 70 29  ts3SegReaderCmp)
38691 3b 0a 20 20 7d 0a 0a 20 66 69 6e 69 73 68 65 64  ;.  }.. finished
38692 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  :.  sqlite3_free
38693 28 61 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74  (aBuffer);.  ret
38694 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
38695 20 4d 65 72 67 65 20 61 6c 6c 20 6c 65 76 65 6c   Merge all level
38696 20 69 4c 65 76 65 6c 20 73 65 67 6d 65 6e 74 73   iLevel segments
38697 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
38698 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 0a   into a single .
38699 2a 2a 20 69 4c 65 76 65 6c 2b 31 20 73 65 67 6d  ** iLevel+1 segm
3869a 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 4c 65 76  ent. Or, if iLev
3869b 65 6c 3c 30 2c 20 6d 65 72 67 65 20 61 6c 6c 20  el<0, merge all 
3869c 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f 20 61 0a  segments into a.
3869d 2a 2a 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e  ** single segmen
3869e 74 20 77 69 74 68 20 61 20 6c 65 76 65 6c 20 65  t with a level e
3869f 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 65  qual to the nume
386a0 72 69 63 61 6c 6c 79 20 6c 61 72 67 65 73 74 20  rically largest 
386a1 6c 65 76 65 6c 20 0a 2a 2a 20 63 75 72 72 65 6e  level .** curren
386a2 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e 20 74  tly present in t
386a3 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
386a4 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74  ** If this funct
386a5 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69  ion is called wi
386a6 74 68 20 69 4c 65 76 65 6c 3c 30 2c 20 62 75 74  th iLevel<0, but
386a7 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f   there is only o
386a8 6e 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 69 6e  ne.** segment in
386a9 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 53   the database, S
386aa 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
386ab 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
386ac 6c 79 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ly. .** Otherwis
386ad 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  e, if successful
386ae 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
386af 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65  eturned. If an e
386b0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 0a 2a 2a  rror occurs, .**
386b1 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
386b2 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
386b3 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
386b4 20 66 74 73 33 53 65 67 6d 65 6e 74 4d 65 72 67   fts3SegmentMerg
386b5 65 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  e(Fts3Table *p, 
386b6 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 69  int iLevel){.  i
386b7 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
386b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
386b9 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
386ba 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ble */.  int rc;
386bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386bc 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
386bd 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
386be 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20 20   iIdx;          
386bf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
386c0 49 6e 64 65 78 20 6f 66 20 6e 65 77 20 73 65 67  Index of new seg
386c1 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4e  ment */.  int iN
386c2 65 77 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20  ewLevel;        
386c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76            /* Lev
386c4 65 6c 20 74 6f 20 63 72 65 61 74 65 20 6e 65 77  el to create new
386c5 20 73 65 67 6d 65 6e 74 20 61 74 20 2a 2f 0a 20   segment at */. 
386c6 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
386c7 53 74 6d 74 3b 0a 20 20 53 65 67 6d 65 6e 74 57  Stmt;.  SegmentW
386c8 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 20 3d  riter *pWriter =
386c9 20 30 3b 0a 20 20 69 6e 74 20 6e 53 65 67 6d 65   0;.  int nSegme
386ca 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nt = 0;         
386cb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
386cc 6f 66 20 73 65 67 6d 65 6e 74 73 20 62 65 69 6e  of segments bein
386cd 67 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 46 74  g merged */.  Ft
386ce 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70  s3SegReader **ap
386cf 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20 2f 2a  Segment = 0;  /*
386d0 20 41 72 72 61 79 20 6f 66 20 53 65 67 6d 65 6e   Array of Segmen
386d1 74 20 69 74 65 72 61 74 6f 72 73 20 2a 2f 0a 20  t iterators */. 
386d2 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 66   Fts3SegFilter f
386d3 69 6c 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  ilter;          
386d4 20 2f 2a 20 53 65 67 6d 65 6e 74 20 74 65 72 6d   /* Segment term
386d5 20 66 69 6c 74 65 72 20 63 6f 6e 64 69 74 69 6f   filter conditio
386d6 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 69 4c 65 76  n */..  if( iLev
386d7 65 6c 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  el<0 ){.    /* T
386d8 68 69 73 20 63 61 6c 6c 20 69 73 20 74 6f 20 6d  his call is to m
386d9 65 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74  erge all segment
386da 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
386db 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 20 20  e to a single.  
386dc 20 20 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 54 68    ** segment. Th
386dd 65 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6e  e level of the n
386de 65 77 20 73 65 67 6d 65 6e 74 20 69 73 20 65 71  ew segment is eq
386df 75 61 6c 20 74 6f 20 74 68 65 20 74 68 65 20 6e  ual to the the n
386e0 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 20 20 20  umerically .    
386e1 2a 2a 20 67 72 65 61 74 65 73 74 20 73 65 67 6d  ** greatest segm
386e2 65 6e 74 20 6c 65 76 65 6c 20 63 75 72 72 65 6e  ent level curren
386e3 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e 20 74  tly present in t
386e4 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  he database. The
386e5 20 69 6e 64 65 78 0a 20 20 20 20 2a 2a 20 6f 66   index.    ** of
386e6 20 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74   the new segment
386e7 20 69 73 20 61 6c 77 61 79 73 20 30 2e 0a 20 20   is always 0..  
386e8 20 20 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20    */.    iIdx = 
386e9 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  0;.    rc = fts3
386ea 53 65 67 6d 65 6e 74 43 6f 75 6e 74 4d 61 78 28  SegmentCountMax(
386eb 70 2c 20 26 6e 53 65 67 6d 65 6e 74 2c 20 26 69  p, &nSegment, &i
386ec 4e 65 77 4c 65 76 65 6c 29 3b 0a 20 20 20 20 69  NewLevel);.    i
386ed 66 28 20 6e 53 65 67 6d 65 6e 74 3d 3d 31 20 29  f( nSegment==1 )
386ee 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
386ef 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
386f0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
386f1 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 74  * This call is t
386f2 6f 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67 6d  o merge all segm
386f3 65 6e 74 73 20 61 74 20 6c 65 76 65 6c 20 69 4c  ents at level iL
386f4 65 76 65 6c 2e 20 46 69 6e 64 20 74 68 65 20 6e  evel. Find the n
386f5 65 78 74 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c  ext.    ** avail
386f6 61 62 6c 65 20 73 65 67 6d 65 6e 74 20 69 6e 64  able segment ind
386f7 65 78 20 61 74 20 6c 65 76 65 6c 20 69 4c 65 76  ex at level iLev
386f8 65 6c 2b 31 2e 20 54 68 65 20 63 61 6c 6c 20 74  el+1. The call t
386f9 6f 0a 20 20 20 20 2a 2a 20 66 74 73 33 41 6c 6c  o.    ** fts3All
386fa 6f 63 61 74 65 53 65 67 64 69 72 49 64 78 28 29  ocateSegdirIdx()
386fb 20 77 69 6c 6c 20 6d 65 72 67 65 20 74 68 65 20   will merge the 
386fc 73 65 67 6d 65 6e 74 73 20 61 74 20 6c 65 76 65  segments at leve
386fd 6c 20 69 4c 65 76 65 6c 2b 31 20 74 6f 20 0a 20  l iLevel+1 to . 
386fe 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69     ** a single i
386ff 4c 65 76 65 6c 2b 32 20 73 65 67 6d 65 6e 74 20  Level+2 segment 
38700 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  if necessary..  
38701 20 20 2a 2f 0a 20 20 20 20 69 4e 65 77 4c 65 76    */.    iNewLev
38702 65 6c 20 3d 20 69 4c 65 76 65 6c 2b 31 3b 0a 20  el = iLevel+1;. 
38703 20 20 20 72 63 20 3d 20 66 74 73 33 41 6c 6c 6f     rc = fts3Allo
38704 63 61 74 65 53 65 67 64 69 72 49 64 78 28 70 2c  cateSegdirIdx(p,
38705 20 69 4e 65 77 4c 65 76 65 6c 2c 20 26 69 49 64   iNewLevel, &iId
38706 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  x);.    if( rc!=
38707 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
38708 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20  rn rc;.    rc = 
38709 66 74 73 33 53 65 67 6d 65 6e 74 43 6f 75 6e 74  fts3SegmentCount
3870a 28 70 2c 20 69 4c 65 76 65 6c 2c 20 26 6e 53 65  (p, iLevel, &nSe
3870b 67 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66  gment);.  }.  if
3870c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3870d 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61  ) return rc;.  a
3870e 73 73 65 72 74 28 20 6e 53 65 67 6d 65 6e 74 3e  ssert( nSegment>
3870f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
38710 4e 65 77 4c 65 76 65 6c 3e 3d 30 20 29 3b 0a 0a  NewLevel>=0 );..
38711 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70    /* Allocate sp
38712 61 63 65 20 66 6f 72 20 61 6e 20 61 72 72 61 79  ace for an array
38713 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
38714 73 65 67 6d 65 6e 74 20 69 74 65 72 61 74 6f 72  segment iterator
38715 73 2e 20 2a 2f 0a 20 20 61 70 53 65 67 6d 65 6e  s. */.  apSegmen
38716 74 20 3d 20 28 46 74 73 33 53 65 67 52 65 61 64  t = (Fts3SegRead
38717 65 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  er**)sqlite3_mal
38718 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 53  loc(sizeof(Fts3S
38719 65 67 52 65 61 64 65 72 20 2a 29 2a 6e 53 65 67  egReader *)*nSeg
3871a 6d 65 6e 74 29 3b 0a 20 20 69 66 28 20 21 61 70  ment);.  if( !ap
3871b 53 65 67 6d 65 6e 74 20 29 7b 0a 20 20 20 20 72  Segment ){.    r
3871c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
3871d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  EM;.  }.  memset
3871e 28 61 70 53 65 67 6d 65 6e 74 2c 20 30 2c 20 73  (apSegment, 0, s
3871f 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52 65 61  izeof(Fts3SegRea
38720 64 65 72 20 2a 29 2a 6e 53 65 67 6d 65 6e 74 29  der *)*nSegment)
38721 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
38722 20 61 20 46 74 73 33 53 65 67 52 65 61 64 65 72   a Fts3SegReader
38723 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65   structure for e
38724 61 63 68 20 73 65 67 6d 65 6e 74 20 62 65 69 6e  ach segment bein
38725 67 20 6d 65 72 67 65 64 2e 20 41 20 0a 20 20 2a  g merged. A .  *
38726 2a 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  * Fts3SegReader 
38727 73 74 6f 72 65 73 20 74 68 65 20 73 74 61 74 65  stores the state
38728 20 64 61 74 61 20 72 65 71 75 69 72 65 64 20 74   data required t
38729 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
3872a 68 20 61 6c 6c 20 0a 20 20 2a 2a 20 65 6e 74 72  h all .  ** entr
3872b 69 65 73 20 6f 6e 20 61 6c 6c 20 6c 65 61 76 65  ies on all leave
3872c 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 73 65  s of a single se
3872d 67 6d 65 6e 74 2e 20 0a 20 20 2a 2f 0a 20 20 61  gment. .  */.  a
3872e 73 73 65 72 74 28 20 53 51 4c 5f 53 45 4c 45 43  ssert( SQL_SELEC
3872f 54 5f 4c 45 56 45 4c 2b 31 3d 3d 53 51 4c 5f 53  T_LEVEL+1==SQL_S
38730 45 4c 45 43 54 5f 41 4c 4c 5f 4c 45 56 45 4c 29  ELECT_ALL_LEVEL)
38731 3b 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c  ;.  rc = fts3Sql
38732 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45  Stmt(p, SQL_SELE
38733 43 54 5f 4c 45 56 45 4c 2b 28 69 4c 65 76 65 6c  CT_LEVEL+(iLevel
38734 3c 30 29 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  <0), &pStmt, 0);
38735 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
38736 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69  E_OK ) goto fini
38737 73 68 65 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  shed;.  sqlite3_
38738 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20  bind_int(pStmt, 
38739 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 66 6f  1, iLevel);.  fo
3873a 72 28 69 3d 30 3b 20 53 51 4c 49 54 45 5f 52 4f  r(i=0; SQLITE_RO
3873b 57 3d 3d 28 73 71 6c 69 74 65 33 5f 73 74 65 70  W==(sqlite3_step
3873c 28 70 53 74 6d 74 29 29 3b 20 69 2b 2b 29 7b 0a  (pStmt)); i++){.
3873d 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67      rc = fts3Seg
3873e 52 65 61 64 65 72 4e 65 77 28 70 2c 20 70 53 74  ReaderNew(p, pSt
3873f 6d 74 2c 20 69 2c 20 26 61 70 53 65 67 6d 65 6e  mt, i, &apSegmen
38740 74 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  t[i]);.    if( r
38741 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
38742 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73        goto finis
38743 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  hed;.    }.  }. 
38744 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
38745 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 70 53  set(pStmt);.  pS
38746 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  tmt = 0;.  if( r
38747 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
38748 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20  oto finished;.. 
38749 20 6d 65 6d 73 65 74 28 26 66 69 6c 74 65 72 2c   memset(&filter,
3874a 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 53   0, sizeof(Fts3S
3874b 65 67 46 69 6c 74 65 72 29 29 3b 0a 20 20 66 69  egFilter));.  fi
3874c 6c 74 65 72 2e 66 6c 61 67 73 20 3d 20 46 54 53  lter.flags = FTS
3874d 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52  3_SEGMENT_REQUIR
3874e 45 5f 50 4f 53 3b 0a 20 20 66 69 6c 74 65 72 2e  E_POS;.  filter.
3874f 66 6c 61 67 73 20 7c 3d 20 28 69 4c 65 76 65 6c  flags |= (iLevel
38750 3c 30 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e  <0 ? FTS3_SEGMEN
38751 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20 3a  T_IGNORE_EMPTY :
38752 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   0);.  rc = sqli
38753 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
38754 49 74 65 72 61 74 65 28 70 2c 20 61 70 53 65 67  Iterate(p, apSeg
38755 6d 65 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 0a  ment, nSegment,.
38756 20 20 20 20 20 20 26 66 69 6c 74 65 72 2c 20 66        &filter, f
38757 74 73 33 4d 65 72 67 65 43 61 6c 6c 62 61 63 6b  ts3MergeCallback
38758 2c 20 28 76 6f 69 64 20 2a 29 26 70 57 72 69 74  , (void *)&pWrit
38759 65 72 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  er.  );.  if( rc
3875a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
3875b 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20 20  to finished;..  
3875c 72 63 20 3d 20 66 74 73 33 44 65 6c 65 74 65 53  rc = fts3DeleteS
3875d 65 67 64 69 72 28 70 2c 20 69 4c 65 76 65 6c 2c  egdir(p, iLevel,
3875e 20 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65 67   apSegment, nSeg
3875f 6d 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  ment);.  if( rc=
38760 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
38761 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 57 72    rc = fts3SegWr
38762 69 74 65 72 46 6c 75 73 68 28 70 2c 20 70 57 72  iterFlush(p, pWr
38763 69 74 65 72 2c 20 69 4e 65 77 4c 65 76 65 6c 2c  iter, iNewLevel,
38764 20 69 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 66 69   iIdx);.  }.. fi
38765 6e 69 73 68 65 64 3a 0a 20 20 66 74 73 33 53 65  nished:.  fts3Se
38766 67 57 72 69 74 65 72 46 72 65 65 28 70 57 72 69  gWriterFree(pWri
38767 74 65 72 29 3b 0a 20 20 69 66 28 20 61 70 53 65  ter);.  if( apSe
38768 67 6d 65 6e 74 20 29 7b 0a 20 20 20 20 66 6f 72  gment ){.    for
38769 28 69 3d 30 3b 20 69 3c 6e 53 65 67 6d 65 6e 74  (i=0; i<nSegment
3876a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
3876b 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
3876c 65 72 46 72 65 65 28 70 2c 20 61 70 53 65 67 6d  erFree(p, apSegm
3876d 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ent[i]);.    }. 
3876e 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
3876f 61 70 53 65 67 6d 65 6e 74 29 3b 0a 20 20 7d 0a  apSegment);.  }.
38770 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
38771 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
38772 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
38773 69 73 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73  is is a comparis
38774 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  on function used
38775 20 61 73 20 61 20 71 73 6f 72 74 28 29 20 63 61   as a qsort() ca
38776 6c 6c 62 61 63 6b 20 77 68 65 6e 20 73 6f 72 74  llback when sort
38777 69 6e 67 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20  ing.** an array 
38778 6f 66 20 70 65 6e 64 69 6e 67 20 74 65 72 6d 73  of pending terms
38779 20 62 79 20 74 65 72 6d 2e 20 54 68 69 73 20 6f   by term. This o
3877a 63 63 75 72 73 20 61 73 20 70 61 72 74 20 6f 66  ccurs as part of
3877b 20 66 6c 75 73 68 69 6e 67 0a 2a 2a 20 74 68 65   flushing.** the
3877c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
3877d 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68   pending-terms h
3877e 61 73 68 20 74 61 62 6c 65 20 74 6f 20 74 68 65  ash table to the
3877f 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
38780 61 74 69 63 20 69 6e 74 20 71 73 6f 72 74 43 6f  atic int qsortCo
38781 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
38782 20 2a 6c 68 73 2c 20 63 6f 6e 73 74 20 76 6f 69   *lhs, const voi
38783 64 20 2a 72 68 73 29 7b 0a 20 20 63 68 61 72 20  d *rhs){.  char 
38784 2a 7a 31 20 3d 20 66 74 73 33 48 61 73 68 4b 65  *z1 = fts3HashKe
38785 79 28 2a 28 46 74 73 33 48 61 73 68 45 6c 65 6d  y(*(Fts3HashElem
38786 20 2a 2a 29 6c 68 73 29 3b 0a 20 20 63 68 61 72   **)lhs);.  char
38787 20 2a 7a 32 20 3d 20 66 74 73 33 48 61 73 68 4b   *z2 = fts3HashK
38788 65 79 28 2a 28 46 74 73 33 48 61 73 68 45 6c 65  ey(*(Fts3HashEle
38789 6d 20 2a 2a 29 72 68 73 29 3b 0a 20 20 69 6e 74  m **)rhs);.  int
3878a 20 6e 31 20 3d 20 66 74 73 33 48 61 73 68 4b 65   n1 = fts3HashKe
3878b 79 73 69 7a 65 28 2a 28 46 74 73 33 48 61 73 68  ysize(*(Fts3Hash
3878c 45 6c 65 6d 20 2a 2a 29 6c 68 73 29 3b 0a 20 20  Elem **)lhs);.  
3878d 69 6e 74 20 6e 32 20 3d 20 66 74 73 33 48 61 73  int n2 = fts3Has
3878e 68 4b 65 79 73 69 7a 65 28 2a 28 46 74 73 33 48  hKeysize(*(Fts3H
3878f 61 73 68 45 6c 65 6d 20 2a 2a 29 72 68 73 29 3b  ashElem **)rhs);
38790 0a 0a 20 20 69 6e 74 20 6e 20 3d 20 28 6e 31 3c  ..  int n = (n1<
38791 6e 32 20 3f 20 6e 31 20 3a 20 6e 32 29 3b 0a 20  n2 ? n1 : n2);. 
38792 20 69 6e 74 20 63 20 3d 20 6d 65 6d 63 6d 70 28   int c = memcmp(
38793 7a 31 2c 20 7a 32 2c 20 6e 29 3b 0a 20 20 69 66  z1, z2, n);.  if
38794 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20  ( c==0 ){.    c 
38795 3d 20 6e 31 20 2d 20 6e 32 3b 0a 20 20 7d 0a 20  = n1 - n2;.  }. 
38796 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f   return c;.}.../
38797 2a 20 0a 2a 2a 20 46 6c 75 73 68 20 74 68 65 20  * .** Flush the 
38798 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 65 6e 64  contents of pend
38799 69 6e 67 54 65 72 6d 73 20 74 6f 20 61 20 6c 65  ingTerms to a le
3879a 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 2e 0a 2a  vel 0 segment..*
3879b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3879c 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
3879d 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73  PendingTermsFlus
3879e 68 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b  h(Fts3Table *p){
3879f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  .  Fts3HashElem 
387a0 2a 70 45 6c 65 6d 3b 0a 20 20 69 6e 74 20 69 64  *pElem;.  int id
387a1 78 2c 20 72 63 2c 20 69 3b 0a 20 20 46 74 73 33  x, rc, i;.  Fts3
387a2 48 61 73 68 45 6c 65 6d 20 2a 2a 61 70 45 6c 65  HashElem **apEle
387a3 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  m;          /* A
387a4 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
387a5 20 74 6f 20 68 61 73 68 20 65 6c 65 6d 65 6e 74   to hash element
387a6 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 6c 65 6d  s */.  int nElem
387a7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
387a8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
387a9 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 6e 65 77   of terms in new
387aa 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20 53 65   segment */.  Se
387ab 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70 57 72  gmentWriter *pWr
387ac 69 74 65 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  iter = 0;     /*
387ad 20 55 73 65 64 20 74 6f 20 77 72 69 74 65 20 74   Used to write t
387ae 68 65 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 0a 20  he segment */.. 
387af 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 75 6d   /* Find the num
387b0 62 65 72 20 6f 66 20 74 65 72 6d 73 20 74 68 61  ber of terms tha
387b1 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74  t will make up t
387b2 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 2e 20  he new segment. 
387b3 49 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 61 72  If there.  ** ar
387b4 65 20 6e 6f 20 74 65 72 6d 73 2c 20 72 65 74 75  e no terms, retu
387b5 72 6e 20 65 61 72 6c 79 20 28 64 6f 20 6e 6f 74  rn early (do not
387b6 20 62 6f 74 68 65 72 20 74 6f 20 77 72 69 74 65   bother to write
387b7 20 61 6e 20 65 6d 70 74 79 20 73 65 67 6d 65 6e   an empty segmen
387b8 74 29 2e 0a 20 20 2a 2f 0a 20 20 6e 45 6c 65 6d  t)..  */.  nElem
387b9 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74   = fts3HashCount
387ba 28 26 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d  (&p->pendingTerm
387bb 73 29 3b 0a 20 20 69 66 28 20 6e 45 6c 65 6d 3d  s);.  if( nElem=
387bc 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
387bd 28 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( p->nPendingDat
387be 61 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  a==0 );.    retu
387bf 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
387c0 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  }..  /* Determin
387c1 65 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78  e the next index
387c2 20 61 74 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72   at level 0, mer
387c3 67 69 6e 67 20 61 73 20 6e 65 63 65 73 73 61 72  ging as necessar
387c4 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73  y. */.  rc = fts
387c5 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69 72 49  3AllocateSegdirI
387c6 64 78 28 70 2c 20 30 2c 20 26 69 64 78 29 3b 0a  dx(p, 0, &idx);.
387c7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
387c8 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
387c9 6e 20 72 63 3b 0a 20 20 7d 20 0a 0a 20 20 61 70  n rc;.  } ..  ap
387ca 45 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Elem = sqlite3_m
387cb 61 6c 6c 6f 63 28 6e 45 6c 65 6d 2a 73 69 7a 65  alloc(nElem*size
387cc 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20  of(Fts3HashElem 
387cd 2a 29 29 3b 0a 20 20 69 66 28 20 21 61 70 45 6c  *));.  if( !apEl
387ce 65 6d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  em ){.    return
387cf 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
387d0 20 7d 0a 0a 20 20 69 20 3d 20 30 3b 0a 20 20 66   }..  i = 0;.  f
387d1 6f 72 28 70 45 6c 65 6d 3d 66 74 73 33 48 61 73  or(pElem=fts3Has
387d2 68 46 69 72 73 74 28 26 70 2d 3e 70 65 6e 64 69  hFirst(&p->pendi
387d3 6e 67 54 65 72 6d 73 29 3b 20 70 45 6c 65 6d 3b  ngTerms); pElem;
387d4 20 70 45 6c 65 6d 3d 66 74 73 33 48 61 73 68 4e   pElem=fts3HashN
387d5 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
387d6 20 61 70 45 6c 65 6d 5b 69 2b 2b 5d 20 3d 20 70   apElem[i++] = p
387d7 45 6c 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  Elem;.  }.  asse
387d8 72 74 28 20 69 3d 3d 6e 45 6c 65 6d 20 29 3b 0a  rt( i==nElem );.
387d9 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
387da 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f  ) Should we allo
387db 77 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63  w user-defined c
387dc 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
387dd 65 73 2c 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20  es,.  ** here?  
387de 49 20 74 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20  I think we only 
387df 6e 65 65 64 20 74 68 61 74 20 6f 6e 63 65 20 77  need that once w
387e0 65 20 73 75 70 70 6f 72 74 20 70 72 65 66 69 78  e support prefix
387e1 20 73 65 61 72 63 68 65 73 2e 0a 20 20 2a 2a 20   searches..  ** 
387e2 41 6c 73 6f 2c 20 73 68 6f 75 6c 64 20 77 65 20  Also, should we 
387e3 62 65 20 75 73 69 6e 67 20 71 73 6f 72 74 28 29  be using qsort()
387e4 3f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 45 6c  ?.  */.  if( nEl
387e5 65 6d 3e 31 20 29 7b 0a 20 20 20 20 71 73 6f 72  em>1 ){.    qsor
387e6 74 28 61 70 45 6c 65 6d 2c 20 6e 45 6c 65 6d 2c  t(apElem, nElem,
387e7 20 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68   sizeof(Fts3Hash
387e8 45 6c 65 6d 20 2a 29 2c 20 71 73 6f 72 74 43 6f  Elem *), qsortCo
387e9 6d 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 0a 20 20  mpare);.  }...  
387ea 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 67  /* Write the seg
387eb 6d 65 6e 74 20 74 72 65 65 20 69 6e 74 6f 20 74  ment tree into t
387ec 68 65 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a  he database. */.
387ed 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
387ee 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 45  QLITE_OK && i<nE
387ef 6c 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  lem; i++){.    c
387f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 66  onst char *z = f
387f1 74 73 33 48 61 73 68 4b 65 79 28 61 70 45 6c 65  ts3HashKey(apEle
387f2 6d 5b 69 5d 29 3b 0a 20 20 20 20 69 6e 74 20 6e  m[i]);.    int n
387f3 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69   = fts3HashKeysi
387f4 7a 65 28 61 70 45 6c 65 6d 5b 69 5d 29 3b 0a 20  ze(apElem[i]);. 
387f5 20 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a     PendingList *
387f6 70 4c 69 73 74 20 3d 20 66 74 73 33 48 61 73 68  pList = fts3Hash
387f7 44 61 74 61 28 61 70 45 6c 65 6d 5b 69 5d 29 3b  Data(apElem[i]);
387f8 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65  .    rc = fts3Se
387f9 67 57 72 69 74 65 72 41 64 64 28 70 2c 20 26 70  gWriterAdd(p, &p
387fa 57 72 69 74 65 72 2c 20 30 2c 20 7a 2c 20 6e 2c  Writer, 0, z, n,
387fb 20 70 4c 69 73 74 2d 3e 61 44 61 74 61 2c 20 70   pList->aData, p
387fc 4c 69 73 74 2d 3e 6e 44 61 74 61 2b 31 29 3b 0a  List->nData+1);.
387fd 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
387fe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
387ff 63 20 3d 20 66 74 73 33 53 65 67 57 72 69 74 65  c = fts3SegWrite
38800 72 46 6c 75 73 68 28 70 2c 20 70 57 72 69 74 65  rFlush(p, pWrite
38801 72 2c 20 30 2c 20 69 64 78 29 3b 0a 20 20 7d 0a  r, 0, idx);.  }.
38802 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 20 61  .  /* Free all a
38803 6c 6c 6f 63 61 74 65 64 20 72 65 73 6f 75 72 63  llocated resourc
38804 65 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  es before return
38805 69 6e 67 20 2a 2f 0a 20 20 66 74 73 33 53 65 67  ing */.  fts3Seg
38806 57 72 69 74 65 72 46 72 65 65 28 70 57 72 69 74  WriterFree(pWrit
38807 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  er);.  sqlite3_f
38808 72 65 65 28 61 70 45 6c 65 6d 29 3b 0a 20 20 73  ree(apElem);.  s
38809 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e  qlite3Fts3Pendin
3880a 67 54 65 72 6d 73 43 6c 65 61 72 28 70 29 3b 0a  gTermsClear(p);.
3880b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3880c 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3880d 69 6f 6e 20 64 6f 65 73 20 74 68 65 20 77 6f 72  ion does the wor
3880e 6b 20 66 6f 72 20 74 68 65 20 78 55 70 64 61 74  k for the xUpdat
3880f 65 20 6d 65 74 68 6f 64 20 6f 66 20 46 54 53 33  e method of FTS3
38810 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
38811 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
38812 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
38813 33 46 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f  3Fts3UpdateMetho
38814 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  d(.  sqlite3_vta
38815 62 20 2a 70 56 74 61 62 2c 20 20 20 20 20 20 20  b *pVtab,       
38816 20 20 20 20 20 2f 2a 20 46 54 53 33 20 76 74 61       /* FTS3 vta
38817 62 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  b object */.  in
38818 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
38819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3881a 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e   Size of argumen
3881b 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c  t array */.  sql
3881c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
3881d 61 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  al,          /* 
3881e 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d 65 6e  Array of argumen
3881f 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ts */.  sqlite_i
38820 6e 74 36 34 20 2a 70 52 6f 77 69 64 20 20 20 20  nt64 *pRowid    
38821 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
38822 54 68 65 20 61 66 66 65 63 74 65 64 20 28 6f 72  The affected (or
38823 20 65 66 66 65 63 74 65 64 29 20 72 6f 77 69 64   effected) rowid
38824 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 54 61 62   */.){.  Fts3Tab
38825 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62  le *p = (Fts3Tab
38826 6c 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e  le *)pVtab;.  in
38827 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
38828 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
38829 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
3882a 20 20 69 6e 74 20 69 73 52 65 6d 6f 76 65 20 3d    int isRemove =
3882b 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3882c 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 6e    /* True for an
3882d 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
3882e 45 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  E */.  sqlite3_i
3882f 6e 74 36 34 20 69 52 65 6d 6f 76 65 20 3d 20 30  nt64 iRemove = 0
38830 3b 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20  ;      /* Rowid 
38831 72 65 6d 6f 76 65 64 20 62 79 20 55 50 44 41 54  removed by UPDAT
38832 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 0a  E or DELETE */..
38833 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
38834 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41  a DELETE or UPDA
38835 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65  TE operation, re
38836 6d 6f 76 65 20 74 68 65 20 6f 6c 64 20 72 65 63  move the old rec
38837 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ord. */.  if( sq
38838 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
38839 28 61 70 56 61 6c 5b 30 5d 29 21 3d 53 51 4c 49  (apVal[0])!=SQLI
3883a 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69  TE_NULL ){.    i
3883b 6e 74 20 69 73 45 6d 70 74 79 3b 0a 20 20 20 20  nt isEmpty;.    
3883c 72 63 20 3d 20 66 74 73 33 49 73 45 6d 70 74 79  rc = fts3IsEmpty
3883d 28 70 2c 20 61 70 56 61 6c 2c 20 26 69 73 45 6d  (p, apVal, &isEm
3883e 70 74 79 29 3b 0a 20 20 20 20 69 66 28 20 72 63  pty);.    if( rc
3883f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
38840 20 20 20 20 20 69 66 28 20 69 73 45 6d 70 74 79       if( isEmpty
38841 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44   ){.        /* D
38842 65 6c 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77  eleting this row
38843 20 6d 65 61 6e 73 20 74 68 65 20 77 68 6f 6c 65   means the whole
38844 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e   table is empty.
38845 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   In this case.  
38846 20 20 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20        ** delete 
38847 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
38848 61 6c 6c 20 74 68 72 65 65 20 74 61 62 6c 65 73  all three tables
38849 20 61 6e 64 20 74 68 72 6f 77 20 61 77 61 79 20   and throw away 
3884a 61 6e 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  any.        ** d
3884b 61 74 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69  ata in the pendi
3884c 6e 67 54 65 72 6d 73 20 68 61 73 68 20 74 61 62  ngTerms hash tab
3884d 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  le..        */. 
3884e 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
3884f 44 65 6c 65 74 65 41 6c 6c 28 70 29 3b 0a 20 20  DeleteAll(p);.  
38850 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38851 20 20 20 69 73 52 65 6d 6f 76 65 20 3d 20 31 3b     isRemove = 1;
38852 0a 20 20 20 20 20 20 20 20 69 52 65 6d 6f 76 65  .        iRemove
38853 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
38854 5f 69 6e 74 36 34 28 61 70 56 61 6c 5b 30 5d 29  _int64(apVal[0])
38855 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66  ;.        rc = f
38856 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 44  ts3PendingTermsD
38857 6f 63 69 64 28 70 2c 20 69 52 65 6d 6f 76 65 29  ocid(p, iRemove)
38858 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
38859 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3885a 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74           rc = ft
3885b 73 33 44 65 6c 65 74 65 54 65 72 6d 73 28 70 2c  s3DeleteTerms(p,
3885c 20 61 70 56 61 6c 29 3b 0a 20 20 20 20 20 20 20   apVal);.       
3885d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3885e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
3885f 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
38860 45 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45  Exec(p, SQL_DELE
38861 54 45 5f 43 4f 4e 54 45 4e 54 2c 20 61 70 56 61  TE_CONTENT, apVa
38862 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  l);.          }.
38863 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38864 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  }.    }.  }.  . 
38865 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
38866 6e 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  n INSERT or UPDA
38867 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 6e  TE operation, in
38868 73 65 72 74 20 74 68 65 20 6e 65 77 20 72 65 63  sert the new rec
38869 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 41  ord. */.  if( nA
3886a 72 67 3e 31 20 26 26 20 72 63 3d 3d 53 51 4c 49  rg>1 && rc==SQLI
3886b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
3886c 3d 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61  = fts3InsertData
3886d 28 70 2c 20 61 70 56 61 6c 2c 20 70 52 6f 77 69  (p, apVal, pRowi
3886e 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  d);.    if( rc==
3886f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 21 69  SQLITE_OK && (!i
38870 73 52 65 6d 6f 76 65 20 7c 7c 20 2a 70 52 6f 77  sRemove || *pRow
38871 69 64 21 3d 69 52 65 6d 6f 76 65 29 20 29 7b 0a  id!=iRemove) ){.
38872 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 50        rc = fts3P
38873 65 6e 64 69 6e 67 54 65 72 6d 73 44 6f 63 69 64  endingTermsDocid
38874 28 70 2c 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20  (p, *pRowid);.  
38875 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
38876 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
38877 20 20 20 72 63 20 3d 20 66 74 73 33 49 6e 73 65     rc = fts3Inse
38878 72 74 54 65 72 6d 73 28 70 2c 20 61 70 56 61 6c  rtTerms(p, apVal
38879 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
3887a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3887b 20 0a 2a 2a 20 46 6c 75 73 68 20 61 6e 79 20 64   .** Flush any d
3887c 61 74 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69  ata in the pendi
3887d 6e 67 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61  ng-terms hash ta
3887e 62 6c 65 20 74 6f 20 64 69 73 6b 2e 20 49 66 20  ble to disk. If 
3887f 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6d  successful,.** m
38880 65 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74  erge all segment
38881 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
38882 65 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  e (including the
38883 20 6e 65 77 20 73 65 67 6d 65 6e 74 2c 20 69 66   new segment, if
38884 20 0a 2a 2a 20 74 68 65 72 65 20 77 61 73 20 61   .** there was a
38885 6e 79 20 64 61 74 61 20 74 6f 20 66 6c 75 73 68  ny data to flush
38886 29 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  ) into a single 
38887 73 65 67 6d 65 6e 74 2e 20 0a 2a 2f 0a 53 51 4c  segment. .*/.SQL
38888 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
38889 73 71 6c 69 74 65 33 46 74 73 33 4f 70 74 69 6d  sqlite3Fts3Optim
3888a 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20 2a 70  ize(Fts3Table *p
3888b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
3888c 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
3888d 28 70 2d 3e 64 62 2c 20 22 53 41 56 45 50 4f 49  (p->db, "SAVEPOI
3888e 4e 54 20 66 74 73 33 22 2c 20 30 2c 20 30 2c 20  NT fts3", 0, 0, 
3888f 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0);.  if( rc==SQ
38890 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
38891 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  c = sqlite3Fts3P
38892 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
38893 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  (p);.    if( rc=
38894 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
38895 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67      rc = fts3Seg
38896 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20 2d 31 29  mentMerge(p, -1)
38897 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
38898 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
38899 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3889a 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20  te3_exec(p->db, 
3889b 22 52 45 4c 45 41 53 45 20 66 74 73 33 22 2c 20  "RELEASE fts3", 
3889c 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 65  0, 0, 0);.    }e
3889d 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
3889e 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 22  e3_exec(p->db, "
3889f 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 66 74 73 33  ROLLBACK TO fts3
388a0 20 3b 20 52 45 4c 45 41 53 45 20 66 74 73 33 22   ; RELEASE fts3"
388a1 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
388a2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
388a3 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  c;.}..#endif../*
388a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
388a5 64 20 6f 66 20 66 74 73 33 5f 77 72 69 74 65 2e  d of fts3_write.
388a6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
388a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
388a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
388aa 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 73 6e  gin file fts3_sn
388ab 69 70 70 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ippet.c ********
388ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
388ae 0a 2a 2a 20 32 30 30 39 20 4f 63 74 20 32 33 0a  .** 2009 Oct 23.
388af 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
388b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
388b1 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
388b2 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
388b3 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
388b4 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
388b5 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
388b6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
388b7 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
388b8 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
388b9 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
388ba 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
388bb 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
388bc 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
388bd 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
388be 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
388bf 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
388c0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
388c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
388c5 2a 2a 2a 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66  ***.*/..#if !def
388c6 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
388c7 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
388c8 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
388c9 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ...typedef struc
388ca 74 20 53 6e 69 70 70 65 74 20 53 6e 69 70 70 65  t Snippet Snippe
388cb 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  t;../*.** An ins
388cc 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
388cd 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
388ce 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
388cf 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 6d 61 74  generated.** mat
388d0 63 68 69 6e 67 2d 77 6f 72 64 20 6f 66 66 73 65  ching-word offse
388d1 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e  t information an
388d2 64 20 73 6e 69 70 70 65 74 73 2e 0a 2a 2f 0a 73  d snippets..*/.s
388d3 74 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a  truct Snippet {.
388d4 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20    int nMatch;   
388d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
388d6 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
388d7 72 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a  r of matches */.
388d8 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20    int nAlloc;   
388d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
388da 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63    /* Space alloc
388db 61 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b  ated for aMatch[
388dc 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e  ] */.  struct sn
388dd 69 70 70 65 74 4d 61 74 63 68 20 7b 20 20 2f 2a  ippetMatch {  /*
388de 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
388df 61 63 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72  ach matching ter
388e0 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e  m */.    char sn
388e1 53 74 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a  Status;       /*
388e2 20 53 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72   Status flag for
388e3 20 75 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74   use while const
388e4 72 75 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73  ructing snippets
388e5 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e   */.    short in
388e6 74 20 6e 42 79 74 65 3b 20 20 20 20 20 2f 2a 20  t nByte;     /* 
388e7 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
388e8 69 6e 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20  in the term */. 
388e9 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69 43 6f     short int iCo
388ea 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  l;      /* The c
388eb 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f 6e 74 61  olumn that conta
388ec 69 6e 73 20 74 68 65 20 6d 61 74 63 68 20 2a 2f  ins the match */
388ed 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69  .    short int i
388ee 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65  Term;     /* The
388ef 20 69 6e 64 65 78 20 69 6e 20 51 75 65 72 79 2e   index in Query.
388f0 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74 68 65 20  pTerms[] of the 
388f1 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f  matching term */
388f2 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b  .    int iToken;
388f3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
388f4 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
388f5 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74 20  tching document 
388f6 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74  token */.    int
388f7 20 69 53 74 61 72 74 3b 20 20 20 20 20 20 20 20   iStart;        
388f8 20 20 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20    /* The offset 
388f9 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 68 61  to the first cha
388fa 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 74 65  racter of the te
388fb 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63  rm */.  } *aMatc
388fc 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
388fd 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
388fe 73 20 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69  s to space obtai
388ff 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20  ned from malloc 
38900 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73  */.  char *zOffs
38901 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
38902 20 20 20 20 20 2f 2a 20 54 65 78 74 20 72 65 6e       /* Text ren
38903 64 65 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68  dering of aMatch
38904 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66  [] */.  int nOff
38905 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
38906 20 20 20 20 20 20 20 20 2f 2a 20 73 74 72 6c 65          /* strle
38907 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20  n(zOffset) */.  
38908 63 68 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20  char *zSnippet; 
38909 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3890a 2f 2a 20 53 6e 69 70 70 65 74 20 74 65 78 74 20  /* Snippet text 
3890b 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65  */.  int nSnippe
3890c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3890d 20 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a       /* strlen(z
3890e 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 3b 0a 0a  Snippet) */.};..
3890f 0a 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  ./* It is not sa
38910 66 65 20 74 6f 20 63 61 6c 6c 20 69 73 73 70 61  fe to call isspa
38911 63 65 28 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c  ce(), tolower(),
38912 20 6f 72 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e   or isalnum() on
38913 0a 2a 2a 20 68 69 2d 62 69 74 2d 73 65 74 20 63  .** hi-bit-set c
38914 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 69 73  haracters.  This
38915 20 69 73 20 74 68 65 20 73 61 6d 65 20 73 6f 6c   is the same sol
38916 75 74 69 6f 6e 20 75 73 65 64 20 69 6e 20 74 68  ution used in th
38917 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a  e.** tokenizer..
38918 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
38919 73 33 73 6e 69 70 70 65 74 49 73 73 70 61 63 65  s3snippetIsspace
3891a 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75  (char c){.  retu
3891b 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f  rn (c&0x80)==0 ?
3891c 20 69 73 73 70 61 63 65 28 63 29 20 3a 20 30 3b   isspace(c) : 0;
3891d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 74 72  .}.../*.** A Str
3891e 69 6e 67 42 75 66 66 65 72 20 6f 62 6a 65 63 74  ingBuffer object
3891f 20 68 6f 6c 64 73 20 61 20 7a 65 72 6f 2d 74 65   holds a zero-te
38920 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20  rminated string 
38921 74 68 61 74 20 67 72 6f 77 73 0a 2a 2a 20 61 72  that grows.** ar
38922 62 69 74 72 61 72 69 6c 79 20 62 79 20 61 70 70  bitrarily by app
38923 65 6e 64 69 6e 67 2e 20 20 53 70 61 63 65 20 74  ending.  Space t
38924 6f 20 68 6f 6c 64 20 74 68 65 20 73 74 72 69 6e  o hold the strin
38925 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  g is obtained.**
38926 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
38927 6c 6c 6f 63 28 29 2e 20 20 41 66 74 65 72 20 61  lloc().  After a
38928 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ny memory alloca
38929 74 69 6f 6e 20 66 61 69 6c 75 72 65 2c 20 0a 2a  tion failure, .*
3892a 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72 2e 7a  * StringBuffer.z
3892b 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20   is set to NULL 
3892c 61 6e 64 20 6e 6f 20 66 75 72 74 68 65 72 20 61  and no further a
3892d 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 74 74  llocation is att
3892e 65 6d 70 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64  empted..*/.typed
3892f 65 66 20 73 74 72 75 63 74 20 53 74 72 69 6e 67  ef struct String
38930 42 75 66 66 65 72 20 7b 0a 20 20 63 68 61 72 20  Buffer {.  char 
38931 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  *z;         /* T
38932 65 78 74 20 6f 66 20 74 68 65 20 73 74 72 69 6e  ext of the strin
38933 67 2e 20 20 53 70 61 63 65 20 66 72 6f 6d 20 6d  g.  Space from m
38934 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 69 6e 74 20  alloc. */.  int 
38935 6e 55 73 65 64 3b 20 20 20 20 20 20 20 2f 2a 20  nUsed;       /* 
38936 4e 75 6d 62 65 72 20 62 79 74 65 73 20 6f 66 20  Number bytes of 
38937 7a 5b 5d 20 75 73 65 64 2c 20 6e 6f 74 20 63 6f  z[] used, not co
38938 75 6e 74 69 6e 67 20 5c 30 30 30 20 74 65 72 6d  unting \000 term
38939 69 6e 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  inator */.  int 
3893a 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20  nAlloc;      /* 
3893b 42 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  Bytes allocated 
3893c 66 6f 72 20 7a 5b 5d 20 2a 2f 0a 7d 20 53 74 72  for z[] */.} Str
3893d 69 6e 67 42 75 66 66 65 72 3b 0a 0a 0a 2f 2a 0a  ingBuffer;.../*.
3893e 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
3893f 6e 65 77 20 53 74 72 69 6e 67 42 75 66 66 65 72  new StringBuffer
38940 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
38941 20 66 74 73 33 53 6e 69 70 70 65 74 53 62 49 6e   fts3SnippetSbIn
38942 69 74 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  it(StringBuffer 
38943 2a 70 29 7b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63  *p){.  p->nAlloc
38944 20 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 55 73   = 100;.  p->nUs
38945 65 64 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 20 3d  ed = 0;.  p->z =
38946 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
38947 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 7d 0a   p->nAlloc );.}.
38948 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 74 65  ./*.** Append te
38949 78 74 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  xt to the string
3894a 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
3894b 69 63 20 76 6f 69 64 20 66 74 73 33 53 6e 69 70  ic void fts3Snip
3894c 70 65 74 41 70 70 65 6e 64 28 53 74 72 69 6e 67  petAppend(String
3894d 42 75 66 66 65 72 20 2a 70 2c 20 63 6f 6e 73 74  Buffer *p, const
3894e 20 63 68 61 72 20 2a 7a 4e 65 77 2c 20 69 6e 74   char *zNew, int
3894f 20 6e 4e 65 77 29 7b 0a 20 20 69 66 28 20 70 2d   nNew){.  if( p-
38950 3e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  >z==0 ) return;.
38951 20 20 69 66 28 20 6e 4e 65 77 3c 30 20 29 20 6e    if( nNew<0 ) n
38952 4e 65 77 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  New = (int)strle
38953 6e 28 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 70  n(zNew);.  if( p
38954 2d 3e 6e 55 73 65 64 20 2b 20 6e 4e 65 77 20 3e  ->nUsed + nNew >
38955 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  = p->nAlloc ){. 
38956 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20     int nAlloc;. 
38957 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 0a     char *zNew;..
38958 20 20 20 20 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e      nAlloc = p->
38959 6e 55 73 65 64 20 2b 20 6e 4e 65 77 20 2b 20 70  nUsed + nNew + p
3895a 2d 3e 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7a 4e  ->nAlloc;.    zN
3895b 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
3895c 6c 6c 6f 63 28 70 2d 3e 7a 2c 20 6e 41 6c 6c 6f  lloc(p->z, nAllo
3895d 63 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 65 77  c);.    if( zNew
3895e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
3895f 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 29 3b  ite3_free(p->z);
38960 0a 20 20 20 20 20 20 70 2d 3e 7a 20 3d 20 30 3b  .      p->z = 0;
38961 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
38962 20 20 20 7d 0a 20 20 20 20 70 2d 3e 7a 20 3d 20     }.    p->z = 
38963 7a 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c  zNew;.    p->nAl
38964 6c 6f 63 20 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20  loc = nAlloc;.  
38965 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a  }.  memcpy(&p->z
38966 5b 70 2d 3e 6e 55 73 65 64 5d 2c 20 7a 4e 65 77  [p->nUsed], zNew
38967 2c 20 6e 4e 65 77 29 3b 0a 20 20 70 2d 3e 6e 55  , nNew);.  p->nU
38968 73 65 64 20 2b 3d 20 6e 4e 65 77 3b 0a 20 20 70  sed += nNew;.  p
38969 2d 3e 7a 5b 70 2d 3e 6e 55 73 65 64 5d 20 3d 20  ->z[p->nUsed] = 
3896a 30 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 74 68 65 20  0;.}../* If the 
3896b 53 74 72 69 6e 67 42 75 66 66 65 72 20 65 6e 64  StringBuffer end
3896c 73 20 69 6e 20 73 6f 6d 65 74 68 69 6e 67 20 6f  s in something o
3896d 74 68 65 72 20 74 68 61 6e 20 77 68 69 74 65 20  ther than white 
3896e 73 70 61 63 65 2c 20 61 64 64 20 61 0a 2a 2a 20  space, add a.** 
3896f 73 69 6e 67 6c 65 20 73 70 61 63 65 20 63 68 61  single space cha
38970 72 61 63 74 65 72 20 74 6f 20 74 68 65 20 65 6e  racter to the en
38971 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
38972 64 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70  d fts3SnippetApp
38973 65 6e 64 57 68 69 74 65 53 70 61 63 65 28 53 74  endWhiteSpace(St
38974 72 69 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a  ringBuffer *p){.
38975 20 20 69 66 28 20 70 2d 3e 7a 20 26 26 20 70 2d    if( p->z && p-
38976 3e 6e 55 73 65 64 20 26 26 20 21 66 74 73 33 73  >nUsed && !fts3s
38977 6e 69 70 70 65 74 49 73 73 70 61 63 65 28 70 2d  nippetIsspace(p-
38978 3e 7a 5b 70 2d 3e 6e 55 73 65 64 2d 31 5d 29 20  >z[p->nUsed-1]) 
38979 29 7b 0a 20 20 20 20 66 74 73 33 53 6e 69 70 70  ){.    fts3Snipp
3897a 65 74 41 70 70 65 6e 64 28 70 2c 20 22 20 22 2c  etAppend(p, " ",
3897b 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 52   1);.  }.}../* R
3897c 65 6d 6f 76 65 20 77 68 69 74 65 20 73 70 61 63  emove white spac
3897d 65 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  e from the end o
3897e 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66  f the StringBuff
3897f 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  er */.static voi
38980 64 20 66 74 73 33 53 6e 69 70 70 65 74 54 72 69  d fts3SnippetTri
38981 6d 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  mWhiteSpace(Stri
38982 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
38983 69 66 28 20 70 2d 3e 7a 20 29 7b 0a 20 20 20 20  if( p->z ){.    
38984 77 68 69 6c 65 28 20 70 2d 3e 6e 55 73 65 64 20  while( p->nUsed 
38985 26 26 20 66 74 73 33 73 6e 69 70 70 65 74 49 73  && fts3snippetIs
38986 73 70 61 63 65 28 70 2d 3e 7a 5b 70 2d 3e 6e 55  space(p->z[p->nU
38987 73 65 64 2d 31 5d 29 20 29 7b 0a 20 20 20 20 20  sed-1]) ){.     
38988 20 70 2d 3e 6e 55 73 65 64 2d 2d 3b 0a 20 20 20   p->nUsed--;.   
38989 20 7d 0a 20 20 20 20 70 2d 3e 7a 5b 70 2d 3e 6e   }.    p->z[p->n
3898a 55 73 65 64 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Used] = 0;.  }.}
3898b 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 6c 65 61 73 65  ../* .** Release
3898c 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f   all memory asso
3898d 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
3898e 53 6e 69 70 70 65 74 20 73 74 72 75 63 74 75 72  Snippet structur
3898f 65 20 70 61 73 73 65 64 20 61 73 0a 2a 2a 20 61  e passed as.** a
38990 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  n argument..*/.s
38991 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 53  tatic void fts3S
38992 6e 69 70 70 65 74 46 72 65 65 28 53 6e 69 70 70  nippetFree(Snipp
38993 65 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  et *p){.  if( p 
38994 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
38995 72 65 65 28 70 2d 3e 61 4d 61 74 63 68 29 3b 0a  ree(p->aMatch);.
38996 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
38997 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b 0a 20 20  (p->zOffset);.  
38998 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
38999 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 20  ->zSnippet);.   
3899a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
3899b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
3899c 70 70 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 65  ppend a single e
3899d 6e 74 72 79 20 74 6f 20 74 68 65 20 70 2d 3e 61  ntry to the p->a
3899e 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a  Match[] log..*/.
3899f 73 74 61 74 69 63 20 69 6e 74 20 73 6e 69 70 70  static int snipp
389a0 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 0a 20  etAppendMatch(. 
389a1 20 53 6e 69 70 70 65 74 20 2a 70 2c 20 20 20 20   Snippet *p,    
389a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 70             /* Ap
389a3 70 65 6e 64 20 74 68 65 20 65 6e 74 72 79 20 74  pend the entry t
389a4 6f 20 74 68 69 73 20 73 6e 69 70 70 65 74 20 2a  o this snippet *
389a5 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e  /.  int iCol, in
389a6 74 20 69 54 65 72 6d 2c 20 20 20 20 20 20 2f 2a  t iTerm,      /*
389a7 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   The column and 
389a8 71 75 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20 20  query term */.  
389a9 69 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20  int iToken,     
389aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 74            /* Mat
389ab 63 68 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20 64  ching token in d
389ac 6f 63 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  ocument */.  int
389ad 20 69 53 74 61 72 74 2c 20 69 6e 74 20 6e 42 79   iStart, int nBy
389ae 74 65 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  te     /* Offset
389af 20 61 6e 64 20 73 69 7a 65 20 6f 66 20 74 68 65   and size of the
389b0 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69   match */.){.  i
389b1 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73  nt i;.  struct s
389b2 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d 61  nippetMatch *pMa
389b3 74 63 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4d  tch;.  if( p->nM
389b4 61 74 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f  atch+1>=p->nAllo
389b5 63 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  c ){.    struct 
389b6 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4e  snippetMatch *pN
389b7 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f  ew;.    p->nAllo
389b8 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  c = p->nAlloc*2 
389b9 2b 20 31 30 3b 0a 20 20 20 20 70 4e 65 77 20 3d  + 10;.    pNew =
389ba 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
389bb 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e  (p->aMatch, p->n
389bc 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e  Alloc*sizeof(p->
389bd 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20  aMatch[0]) );.  
389be 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
389bf 0a 20 20 20 20 20 20 70 2d 3e 61 4d 61 74 63 68  .      p->aMatch
389c0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6e   = 0;.      p->n
389c1 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
389c2 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a   p->nAlloc = 0;.
389c3 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
389c4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
389c5 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d  .    p->aMatch =
389c6 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 20 3d   pNew;.  }.  i =
389c7 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20   p->nMatch++;.  
389c8 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61  pMatch = &p->aMa
389c9 74 63 68 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68  tch[i];.  pMatch
389ca 2d 3e 69 43 6f 6c 20 3d 20 28 73 68 6f 72 74 29  ->iCol = (short)
389cb 69 43 6f 6c 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  iCol;.  pMatch->
389cc 69 54 65 72 6d 20 3d 20 28 73 68 6f 72 74 29 69  iTerm = (short)i
389cd 54 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  Term;.  pMatch->
389ce 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b  iToken = iToken;
389cf 0a 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72  .  pMatch->iStar
389d0 74 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d  t = iStart;.  pM
389d1 61 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 28 73  atch->nByte = (s
389d2 68 6f 72 74 29 6e 42 79 74 65 3b 0a 20 20 72 65  hort)nByte;.  re
389d3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
389d4 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e 67 20  }../*.** Sizing 
389d5 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
389d6 74 68 65 20 63 69 72 63 75 6c 61 72 20 62 75 66  the circular buf
389d7 66 65 72 20 75 73 65 64 20 69 6e 20 73 6e 69 70  fer used in snip
389d8 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75  petOffsetsOfColu
389d9 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  mn().*/.#define 
389da 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 20 20  FTS3_ROTOR_SZ   
389db 28 33 32 29 0a 23 64 65 66 69 6e 65 20 46 54 53  (32).#define FTS
389dc 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 20 28 46 54  3_ROTOR_MASK (FT
389dd 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 0a 0a  S3_ROTOR_SZ-1)..
389de 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74  /*.** Function t
389df 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
389e0 68 20 74 68 65 20 74 6f 6b 65 6e 73 20 6f 66 20  h the tokens of 
389e1 61 20 63 6f 6d 70 69 6c 65 64 20 65 78 70 72 65  a compiled expre
389e2 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 63  ssion..**.** Exc
389e3 65 70 74 2c 20 73 6b 69 70 20 61 6c 6c 20 74 6f  ept, skip all to
389e4 6b 65 6e 73 20 6f 6e 20 74 68 65 20 72 69 67 68  kens on the righ
389e5 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61  t-hand side of a
389e6 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a   NOT operator..*
389e7 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
389e8 69 73 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20  is used to find 
389e9 74 6f 6b 65 6e 73 20 61 73 20 70 61 72 74 20 6f  tokens as part o
389ea 66 20 73 6e 69 70 70 65 74 20 61 6e 64 20 6f 66  f snippet and of
389eb 66 73 65 74 0a 2a 2a 20 67 65 6e 65 72 61 74 69  fset.** generati
389ec 6f 6e 20 61 6e 64 20 77 65 20 64 6f 20 6e 74 20  on and we do nt 
389ed 77 61 6e 74 20 73 6e 69 70 70 65 74 73 20 61 6e  want snippets an
389ee 64 20 6f 66 66 73 65 74 73 20 74 6f 20 72 65 70  d offsets to rep
389ef 6f 72 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 66  ort matches.** f
389f0 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65  or tokens on the
389f1 20 52 48 53 20 6f 66 20 61 20 4e 4f 54 2e 0a 2a   RHS of a NOT..*
389f2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
389f3 33 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e 28 46  3NextExprToken(F
389f4 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72  ts3Expr **ppExpr
389f5 2c 20 69 6e 74 20 2a 70 69 54 6f 6b 65 6e 29 7b  , int *piToken){
389f6 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d  .  Fts3Expr *p =
389f7 20 2a 70 70 45 78 70 72 3b 0a 20 20 69 6e 74 20   *ppExpr;.  int 
389f8 69 54 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f 6b 65  iToken = *piToke
389f9 6e 3b 0a 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c  n;.  if( iToken<
389fa 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74  0 ){.    /* In t
389fb 68 69 73 20 63 61 73 65 20 74 68 65 20 65 78 70  his case the exp
389fc 72 65 73 73 69 6f 6e 20 70 20 69 73 20 74 68 65  ression p is the
389fd 20 72 6f 6f 74 20 6f 66 20 61 6e 20 65 78 70 72   root of an expr
389fe 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20  ession tree..   
389ff 20 2a 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   ** Move to the 
38a00 66 69 72 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74  first token in t
38a01 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  he expression tr
38a02 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  ee..    */.    w
38a03 68 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20 29  hile( p->pLeft )
38a04 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  {.      p = p->p
38a05 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Left;.    }.    
38a06 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 7d 65  iToken = 0;.  }e
38a07 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
38a08 70 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46  p && p->eType==F
38a09 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
38a0a 3b 0a 20 20 20 20 69 66 28 20 69 54 6f 6b 65 6e  ;.    if( iToken
38a0b 3c 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  <(p->pPhrase->nT
38a0c 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20  oken-1) ){.     
38a0d 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d   iToken++;.    }
38a0e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 54 6f 6b  else{.      iTok
38a0f 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68  en = 0;.      wh
38a10 69 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20  ile( p->pParent 
38a11 26 26 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70  && p->pParent->p
38a12 4c 65 66 74 21 3d 70 20 29 7b 0a 20 20 20 20 20  Left!=p ){.     
38a13 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50     assert( p->pP
38a14 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70  arent->pRight==p
38a15 20 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20   );.        p = 
38a16 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  p->pParent;.    
38a17 20 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d    }.      p = p-
38a18 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20  >pParent;.      
38a19 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20  if( p ){.       
38a1a 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
38a1b 68 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ht!=0 );.       
38a1c 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a   p = p->pRight;.
38a1d 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
38a1e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  ->pLeft ){.     
38a1f 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66       p = p->pLef
38a20 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
38a21 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
38a22 20 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20    *ppExpr = p;. 
38a23 20 2a 70 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b   *piToken = iTok
38a24 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 70 3f 31  en;.  return p?1
38a25 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  :0;.}../*.** Ret
38a26 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
38a27 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
38a28 70 45 78 70 72 20 69 73 20 6c 6f 63 61 74 65 64  pExpr is located
38a29 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
38a2a 52 48 53 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65  RHS of a NOT ope
38a2b 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
38a2c 20 69 6e 74 20 66 74 73 33 45 78 70 72 42 65 6e   int fts3ExprBen
38a2d 65 61 74 68 4e 6f 74 28 46 74 73 33 45 78 70 72  eathNot(Fts3Expr
38a2e 20 2a 70 29 7b 0a 20 20 46 74 73 33 45 78 70 72   *p){.  Fts3Expr
38a2f 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 77 68 69   *pParent;.  whi
38a30 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 70 50 61  le( p ){.    pPa
38a31 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e  rent = p->pParen
38a32 74 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 65  t;.    if( pPare
38a33 6e 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 65  nt && pParent->e
38a34 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
38a35 4f 54 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70  OT && pParent->p
38a36 52 69 67 68 74 3d 3d 70 20 29 7b 0a 20 20 20 20  Right==p ){.    
38a37 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
38a38 7d 0a 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e  }.    p = pParen
38a39 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
38a3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  0;.}../*.** Add 
38a3b 65 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70  entries to pSnip
38a3c 70 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f  pet->aMatch[] fo
38a3d 72 20 65 76 65 72 79 20 6d 61 74 63 68 20 74 68  r every match th
38a3e 61 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73  at occurs agains
38a3f 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44  t.** document zD
38a40 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68  oc[0..nDoc-1] wh
38a41 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ich is stored in
38a42 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e   column iColumn.
38a43 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
38a44 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43  nippetOffsetsOfC
38a45 6f 6c 75 6d 6e 28 0a 20 20 46 74 73 33 43 75 72  olumn(.  Fts3Cur
38a46 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
38a47 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 74 65     /* The fullte
38a48 73 74 20 73 65 61 72 63 68 20 63 75 72 73 6f 72  st search cursor
38a49 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70   */.  Snippet *p
38a4a 53 6e 69 70 70 65 74 2c 20 20 20 20 20 20 20 20  Snippet,        
38a4b 20 20 20 20 20 2f 2a 20 54 68 65 20 53 6e 69 70       /* The Snip
38a4c 70 65 74 20 6f 62 6a 65 63 74 20 74 6f 20 62 65  pet object to be
38a4d 20 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20   filled in */.  
38a4e 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
38a4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38a50 2a 20 49 6e 64 65 78 20 6f 66 20 66 75 6c 6c 74  * Index of fullt
38a51 65 78 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  ext table column
38a52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
38a53 20 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20   *zDoc,         
38a54 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
38a55 74 68 65 20 66 75 6c 6c 74 65 78 74 20 74 61 62  the fulltext tab
38a56 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  le column */.  i
38a57 6e 74 20 6e 44 6f 63 20 20 20 20 20 20 20 20 20  nt nDoc         
38a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38a59 20 4c 65 6e 67 74 68 20 6f 66 20 7a 44 6f 63 20   Length of zDoc 
38a5a 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20  in bytes */.){. 
38a5b 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
38a5c 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
38a5d 2a 70 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54  *pTModule;  /* T
38a5e 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64  he tokenizer mod
38a5f 75 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ule */.  sqlite3
38a60 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
38a61 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 20 20  enizer;         
38a62 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
38a63 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  fic tokenizer */
38a64 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
38a65 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54 43  izer_cursor *pTC
38a66 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  ursor;        /*
38a67 20 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f   Tokenizer curso
38a68 72 20 2a 2f 0a 20 20 46 74 73 33 54 61 62 6c 65  r */.  Fts3Table
38a69 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 20 20   *pVtab;        
38a6a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
38a6b 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a  ull text index *
38a6c 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
38a6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a6e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
38a6f 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
38a70 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
38a71 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
38a72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a73 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
38a74 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72  nters */.  int r
38a75 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
38a76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a77 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
38a78 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
38a79 20 6d 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63   match, prevMatc
38a7a 68 3b 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61  h;       /* Phra
38a7b 73 65 20 73 65 61 72 63 68 20 62 69 74 6d 61 73  se search bitmas
38a7c 6b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ks */.  const ch
38a7d 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *zToken;     
38a7e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38a7f 4e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  Next token from 
38a80 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  the tokenizer */
38a81 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20  .  int nToken;  
38a82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38a83 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
38a84 6f 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69  of zToken */.  i
38a85 6e 74 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c  nt iBegin, iEnd,
38a86 20 69 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20   iPos;          
38a87 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f      /* Offsets o
38a88 66 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  f beginning and 
38a89 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  end */..  /* The
38a8a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
38a8b 62 6c 65 73 20 6b 65 65 70 20 61 20 63 69 72 63  bles keep a circ
38a8c 75 6c 61 72 20 62 75 66 66 65 72 20 6f 66 20 74  ular buffer of t
38a8d 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77  he last.  ** few
38a8e 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73   tokens */.  uns
38a8f 69 67 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72  igned int iRotor
38a90 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
38a91 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
38a92 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20  rrent token */. 
38a93 20 69 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e   int iRotorBegin
38a94 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b  [FTS3_ROTOR_SZ];
38a95 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69        /* Beginni
38a96 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  ng offset of tok
38a97 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74  en */.  int iRot
38a98 6f 72 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52  orLen[FTS3_ROTOR
38a99 5f 53 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  _SZ];        /* 
38a9a 4c 65 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20  Length of token 
38a9b 2a 2f 0a 0a 20 20 70 56 74 61 62 20 3d 20 20 28  */..  pVtab =  (
38a9c 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72  Fts3Table *)pCur
38a9d 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20  ->base.pVtab;.  
38a9e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62 2d  nColumn = pVtab-
38a9f 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b  >nColumn;.  pTok
38aa0 65 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d 3e  enizer = pVtab->
38aa1 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 54  pTokenizer;.  pT
38aa2 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69  Module = pTokeni
38aa3 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  zer->pModule;.  
38aa4 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78  rc = pTModule->x
38aa5 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  Open(pTokenizer,
38aa6 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 54   zDoc, nDoc, &pT
38aa7 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72  Cursor);.  if( r
38aa8 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
38aa9 20 70 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65   pTCursor->pToke
38aaa 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
38aab 65 72 3b 0a 0a 20 20 70 72 65 76 4d 61 74 63 68  er;..  prevMatch
38aac 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
38aad 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78  rc = pTModule->x
38aae 4e 65 78 74 28 70 54 43 75 72 73 6f 72 2c 20 26  Next(pTCursor, &
38aaf 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
38ab0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
38ab1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38ab2 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20  &iBegin, &iEnd, 
38ab3 26 69 50 6f 73 29 29 3d 3d 53 51 4c 49 54 45 5f  &iPos))==SQLITE_
38ab4 4f 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78  OK ){.    Fts3Ex
38ab5 70 72 20 2a 70 49 74 65 72 20 3d 20 70 43 75 72  pr *pIter = pCur
38ab6 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74  ->pExpr;.    int
38ab7 20 69 49 74 65 72 20 3d 20 2d 31 3b 0a 20 20 20   iIter = -1;.   
38ab8 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 69 52 6f   iRotorBegin[iRo
38ab9 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  tor&FTS3_ROTOR_M
38aba 41 53 4b 5d 20 3d 20 69 42 65 67 69 6e 3b 0a 20  ASK] = iBegin;. 
38abb 20 20 20 69 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f     iRotorLen[iRo
38abc 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  tor&FTS3_ROTOR_M
38abd 41 53 4b 5d 20 3d 20 69 45 6e 64 2d 69 42 65 67  ASK] = iEnd-iBeg
38abe 69 6e 3b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20  in;.    match = 
38abf 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
38ac0 69 3c 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a  i<(FTS3_ROTOR_SZ
38ac1 2d 31 29 20 26 26 20 66 74 73 33 4e 65 78 74 45  -1) && fts3NextE
38ac2 78 70 72 54 6f 6b 65 6e 28 26 70 49 74 65 72 2c  xprToken(&pIter,
38ac3 20 26 69 49 74 65 72 29 3b 20 69 2b 2b 29 7b 0a   &iIter); i++){.
38ac4 20 20 20 20 20 20 69 6e 74 20 6e 50 68 72 61 73        int nPhras
38ac5 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
38ac6 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
38ac7 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 63 75 72  of tokens in cur
38ac8 72 65 6e 74 20 70 68 72 61 73 65 20 2a 2f 0a 20  rent phrase */. 
38ac9 20 20 20 20 20 73 74 72 75 63 74 20 50 68 72 61       struct Phra
38aca 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b  seToken *pToken;
38acb 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
38acc 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  token */.      i
38acd 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
38ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38acf 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 2a  * Column index *
38ad0 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 66 74 73  /..      if( fts
38ad1 33 45 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28  3ExprBeneathNot(
38ad2 70 49 74 65 72 29 20 29 20 63 6f 6e 74 69 6e 75  pIter) ) continu
38ad3 65 3b 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65  e;.      nPhrase
38ad4 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73   = pIter->pPhras
38ad5 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  e->nToken;.     
38ad6 20 70 54 6f 6b 65 6e 20 3d 20 26 70 49 74 65 72   pToken = &pIter
38ad7 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
38ad8 6e 5b 69 49 74 65 72 5d 3b 0a 20 20 20 20 20 20  n[iIter];.      
38ad9 69 43 6f 6c 20 3d 20 70 49 74 65 72 2d 3e 70 50  iCol = pIter->pP
38ada 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  hrase->iColumn;.
38adb 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d        if( iCol>=
38adc 30 20 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d  0 && iCol<nColum
38add 6e 20 26 26 20 69 43 6f 6c 21 3d 69 43 6f 6c 75  n && iCol!=iColu
38ade 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  mn ) continue;. 
38adf 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d       if( pToken-
38ae0 3e 6e 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74  >n>nToken ) cont
38ae1 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20  inue;.      if( 
38ae2 21 70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69  !pToken->isPrefi
38ae3 78 20 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e  x && pToken->n<n
38ae4 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65  Token ) continue
38ae5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
38ae6 70 54 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65  pToken->n<=nToke
38ae7 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  n );.      if( m
38ae8 65 6d 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c  emcmp(pToken->z,
38ae9 20 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d   zToken, pToken-
38aea 3e 6e 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  >n) ) continue;.
38aeb 20 20 20 20 20 20 69 66 28 20 69 49 74 65 72 3e        if( iIter>
38aec 30 20 26 26 20 28 70 72 65 76 4d 61 74 63 68 20  0 && (prevMatch 
38aed 26 20 28 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63  & (1<<i))==0 ) c
38aee 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d  ontinue;.      m
38aef 61 74 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20  atch |= 1<<i;.  
38af0 20 20 20 20 69 66 28 20 69 3d 3d 28 46 54 53 33      if( i==(FTS3
38af1 5f 52 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20  _ROTOR_SZ-2) || 
38af2 6e 50 68 72 61 73 65 3d 3d 69 49 74 65 72 2b 31  nPhrase==iIter+1
38af3 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
38af4 6a 3d 6e 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d  j=nPhrase-1; j>=
38af5 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20  0; j--){.       
38af6 20 20 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74     int k = (iRot
38af7 6f 72 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f 54  or-j) & FTS3_ROT
38af8 4f 52 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20  OR_MASK;.       
38af9 20 20 20 72 63 20 3d 20 73 6e 69 70 70 65 74 41     rc = snippetA
38afa 70 70 65 6e 64 4d 61 74 63 68 28 70 53 6e 69 70  ppendMatch(pSnip
38afb 70 65 74 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 2d  pet, iColumn, i-
38afc 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20 20 20 20 20  j, iPos-j,.     
38afd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38afe 20 20 20 20 20 20 20 20 20 20 20 20 20 69 52 6f               iRo
38aff 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69 52 6f  torBegin[k], iRo
38b00 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20 20 20  torLen[k]);.    
38b01 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67        if( rc ) g
38b02 6f 74 6f 20 65 6e 64 5f 6f 66 66 73 65 74 73 5f  oto end_offsets_
38b03 6f 66 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  of_column;.     
38b04 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
38b05 20 7d 0a 20 20 20 20 70 72 65 76 4d 61 74 63 68   }.    prevMatch
38b06 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a 20 20 20   = match<<1;.   
38b07 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20 7d 0a 65   iRotor++;.  }.e
38b08 6e 64 5f 6f 66 66 73 65 74 73 5f 6f 66 5f 63 6f  nd_offsets_of_co
38b09 6c 75 6d 6e 3a 0a 20 20 70 54 4d 6f 64 75 6c 65  lumn:.  pTModule
38b0a 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73 6f  ->xClose(pTCurso
38b0b 72 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 72  r);  .  return r
38b0c 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
38b0d 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b   SQLITE_OK : rc;
38b0e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
38b0f 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68   entries from th
38b10 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75 63  e pSnippet struc
38b11 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74 20  ture to account 
38b12 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a 20  for the NEAR.** 
38b13 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20 74  operator. When t
38b14 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70  his is called, p
38b15 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73  Snippet contains
38b16 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f 6b   the list of tok
38b17 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20 70  en .** offsets p
38b18 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61 74  roduced by treat
38b19 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70 65  ing all NEAR ope
38b1a 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f 70  rators as AND op
38b1b 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69 73  erators..** This
38b1c 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65   function remove
38b1d 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74 68  s any entries th
38b1e 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  at should not be
38b1f 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a 2a   present after.*
38b20 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f 72  * accounting for
38b21 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72 69   the NEAR restri
38b22 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70  ction. For examp
38b23 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72 69  le, if the queri
38b24 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 69  ed.** document i
38b25 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41 20  s:.**.**     "A 
38b26 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a 2a  B C D E A".**.**
38b27 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20 69   and the query i
38b28 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41 20  s:.** .**     A 
38b29 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20 74  NEAR/0 E.**.** t
38b2a 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  hen when this fu
38b2b 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
38b2c 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f 6e   the Snippet con
38b2d 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66 73  tains token offs
38b2e 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64 20  ets.** 0, 4 and 
38b2f 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  5. This function
38b30 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30 22   removes the "0"
38b31 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65 20   entry (because 
38b32 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20 69  the first A.** i
38b33 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75 67  s not near enoug
38b34 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a 2a  h to an E)..**.*
38b35 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * When this func
38b36 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20  tion is called, 
38b37 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  the value pointe
38b38 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74 65  d to by paramete
38b39 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20 74  r piLeft is.** t
38b3a 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f 66  he integer id of
38b3b 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 74   the left-most t
38b3c 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72  oken in the expr
38b3d 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64  ession tree head
38b3e 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e 20  ed by.** pExpr. 
38b3f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
38b40 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66 74  crements *piLeft
38b41 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   by the total nu
38b42 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a 2a  mber of tokens.*
38b43 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  * in the express
38b44 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64 20  ion tree headed 
38b45 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20  by pExpr..**.** 
38b46 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
38b47 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73 2e  trimming occurs.
38b48 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f    Return 0 if no
38b49 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65 71   trimming is req
38b4a 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  uired..*/.static
38b4b 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65 74   int trimSnippet
38b4c 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33 45  Offsets(.  Fts3E
38b4d 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
38b4e 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20 65   /* The search e
38b4f 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
38b50 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65 74  nippet *pSnippet
38b51 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74 20  ,    /* The set 
38b52 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73 65  of snippet offse
38b53 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65 64  ts to be trimmed
38b54 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66   */.  int *piLef
38b55 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  t           /* I
38b56 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f 73  ndex of left-mos
38b57 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70 72  t token in pExpr
38b58 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 78   */.){.  if( pEx
38b59 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74 72  pr ){.    if( tr
38b5a 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
38b5b 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70  (pExpr->pLeft, p
38b5c 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74 29  Snippet, piLeft)
38b5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
38b5e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   1;.    }..    s
38b5f 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54  witch( pExpr->eT
38b60 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
38b61 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
38b62 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c 65  E:.        *piLe
38b63 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50 68  ft += pExpr->pPh
38b64 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20  rase->nToken;.  
38b65 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
38b66 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
38b67 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 20  _NEAR: {.       
38b68 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   /* The right-ha
38b69 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45 41  nd-side of a NEA
38b6a 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61 6c  R operator is al
38b6b 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20 54  ways a phrase. T
38b6c 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 65  he.        ** le
38b6d 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73 20  ft-hand-side is 
38b6e 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 20  either a phrase 
38b6f 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  or an expression
38b70 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a 20   tree that is . 
38b71 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c 66         ** itself
38b72 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45 41   headed by a NEA
38b73 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65 20  R operator. The 
38b74 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69 61  following initia
38b75 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20 20  lizations.      
38b76 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20 76    ** set local v
38b77 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74 6f  ariable iLeft to
38b78 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62 65   the token numbe
38b79 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f  r of the left-mo
38b7a 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  st.        ** to
38b7b 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68 74  ken in the right
38b7c 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61 6e  -hand phrase, an
38b7d 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65 20  d iRight to the 
38b7e 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20 20  right most.     
38b7f 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74     ** token in t
38b80 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e 20  he same phrase. 
38b81 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
38b82 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20 20  we had:.        
38b83 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
38b84 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27 22    <col> MATCH '"
38b85 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32 20  abc def" NEAR/2 
38b86 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20 20  "ghi jkl"'.     
38b87 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
38b88 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c 6c   then iLeft will
38b89 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74 6f   be set to 2 (to
38b8a 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67 68  ken number of gh
38b8b 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77 69  i) and nToken wi
38b8c 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65  ll.        ** be
38b8d 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20 20   set to 4..     
38b8e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 74     */.        Ft
38b8f 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20  s3Expr *pLeft = 
38b90 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
38b91 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
38b92 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
38b93 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
38b94 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69 4c  int iLeft = *piL
38b95 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  eft;.        int
38b96 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d 3e   nNear = pExpr->
38b97 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20 69  nNear;.        i
38b98 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69 67  nt nToken = pRig
38b99 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ht->pPhrase->nTo
38b9a 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ken;.        int
38b9b 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20 20   jj, ii;.       
38b9c 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79 70   if( pLeft->eTyp
38b9d 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
38b9e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4c   ){.          pL
38b9f 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52 69  eft = pLeft->pRi
38ba0 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ght;.        }. 
38ba1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
38ba2 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46 54  Right->eType==FT
38ba3 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
38ba4 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
38ba5 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46   pLeft->eType==F
38ba6 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
38ba7 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e  ;.        nToken
38ba8 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61   += pLeft->pPhra
38ba9 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20 20  se->nToken;..   
38baa 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
38bab 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74  i<pSnippet->nMat
38bac 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ch; ii++){.     
38bad 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70       struct snip
38bae 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26 70  petMatch *p = &p
38baf 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
38bb0 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ii];.          i
38bb1 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65  f( p->iTerm==iLe
38bb2 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
38bb3 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b 0a    int isOk = 0;.
38bb4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
38bb5 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e 20  nippet ii is an 
38bb6 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75 65  occurence of que
38bb7 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69 6e  ry term iLeft in
38bb8 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a 20   the document.. 
38bb9 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 74             ** It
38bba 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69 74   occurs at posit
38bbb 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29 20  ion (p->iToken) 
38bbc 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e  of the document.
38bbd 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20 20   We now.        
38bbe 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66 6f      ** search fo
38bbf 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  r an instance of
38bc0 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31 29   token (iLeft-1)
38bc1 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68   somewhere in th
38bc2 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e .            *
38bc3 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f 6b  * range (p->iTok
38bc4 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28 70  en - nNear)...(p
38bc5 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61 72  ->iToken + nNear
38bc6 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68 69   + nToken) withi
38bc7 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  n .            *
38bc8 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e 69  * the set of sni
38bc9 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63 74  ppetMatch struct
38bca 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73 20  ures. If one is 
38bcb 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e 20  found, proceed. 
38bcc 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
38bcd 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
38bce 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65 6d   found, then rem
38bcf 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69 2e  ove snippets ii.
38bd0 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20 20  .(ii+N-1) .     
38bd1 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74         ** from t
38bd2 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69 70  he matching snip
38bd3 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69 73  pets, where N is
38bd4 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
38bd5 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20 20  okens .         
38bd6 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65 20     ** in phrase 
38bd7 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2e  pRight->pPhrase.
38bd8 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
38bd9 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
38bda 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26 26  jj=0; isOk==0 &&
38bdb 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d   jj<pSnippet->nM
38bdc 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  atch; jj++){.   
38bdd 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63             struc
38bde 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
38bdf 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e  p2 = &pSnippet->
38be0 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20 20  aMatch[jj];.    
38be1 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
38be2 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74 2d  ->iTerm==(iLeft-
38be3 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  1) ){.          
38be4 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54        if( p2->iT
38be5 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65 6e  oken>=(p->iToken
38be6 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20 20  -nNear-1) .     
38be7 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70              && p
38be8 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69 54  2->iToken<(p->iT
38be9 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b 65  oken+nNear+nToke
38bea 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  n) .            
38beb 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
38bec 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20           isOk = 
38bed 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
38bee 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38bef 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38bf0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
38bf1 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20  f( !isOk ){.    
38bf2 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b            int kk
38bf3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
38bf4 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52 69  for(kk=0; kk<pRi
38bf5 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
38bf6 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20  oken; kk++){.   
38bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 6e               pSn
38bf8 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b 6b  ippet->aMatch[kk
38bf9 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b  +ii].iTerm = -2;
38bfa 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
38bfb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
38bfc 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
38bfd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
38bfe 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
38bff 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66   p->iTerm==(iLef
38c00 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  t-1) ){.        
38c01 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30      int isOk = 0
38c02 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f  ;.            fo
38c03 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20  r(jj=0; isOk==0 
38c04 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e  && jj<pSnippet->
38c05 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20  nMatch; jj++){. 
38c06 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
38c07 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
38c08 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74   *p2 = &pSnippet
38c09 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20  ->aMatch[jj];.  
38c0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
38c0b 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66 74  p2->iTerm==iLeft
38c0c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
38c0d 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f 6b      if( p2->iTok
38c0e 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e  en<=(p->iToken+n
38c0f 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20 20  Near+1) .       
38c10 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32 2d            && p2-
38c11 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f 6b  >iToken>(p->iTok
38c12 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e 29  en-nNear-nToken)
38c13 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
38c14 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
38c15 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31 3b         isOk = 1;
38c16 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
38c17 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
38c18 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
38c19 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
38c1a 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20 20   !isOk ){.      
38c1b 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a          int kk;.
38c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
38c1d 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66 74  r(kk=0; kk<pLeft
38c1e 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
38c1f 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  n; kk++){.      
38c20 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70 70            pSnipp
38c21 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b 6b  et->aMatch[ii-kk
38c22 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20 20  ].iTerm = -2;.  
38c23 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
38c24 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
38c25 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 1;.          
38c26 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
38c27 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38c28 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
38c29 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
38c2a 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
38c2b 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74  ts(pExpr->pRight
38c2c 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65  , pSnippet, piLe
38c2d 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ft) ){.      ret
38c2e 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
38c2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
38c30 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6c  /*.** Compute al
38c31 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74 68  l offsets for th
38c32 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
38c33 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a 2a   the query.  .**
38c34 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73 20   If the offsets 
38c35 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
38c36 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69 73  n computed, this
38c37 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
38c38 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
38c39 6e 74 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66  nt snippetAllOff
38c3a 73 65 74 73 28 46 74 73 33 43 75 72 73 6f 72 20  sets(Fts3Cursor 
38c3b 2a 70 43 73 72 2c 20 53 6e 69 70 70 65 74 20 2a  *pCsr, Snippet *
38c3c 2a 70 70 53 6e 69 70 70 65 74 29 7b 0a 20 20 46  *ppSnippet){.  F
38c3d 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
38c3e 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d  ts3Table *)pCsr-
38c3f 3e 62 61 73 65 2e 70 56 74 61 62 3b 20 20 2f 2a  >base.pVtab;  /*
38c40 20 54 68 65 20 46 54 53 33 20 76 69 72 74 75 61   The FTS3 virtua
38c41 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  l table */.  int
38c42 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
38c43 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38c44 20 63 6f 6c 75 6d 6e 73 2e 20 20 44 6f 63 69 64   columns.  Docid
38c45 20 64 6f 65 73 20 63 6f 75 6e 74 20 2a 2f 0a 20   does count */. 
38c46 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
38c47 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
38c48 20 6f 66 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20   of of a column 
38c49 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
38c4a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
38c4b 6f 6f 70 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  oop index */.  i
38c4c 6e 74 20 69 46 69 72 73 74 3b 20 20 20 20 20 20  nt iFirst;      
38c4d 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 63        /* First c
38c4e 6f 6c 75 6d 6e 20 74 6f 20 73 65 61 72 63 68 20  olumn to search 
38c4f 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 73 74 3b 20  */.  int iLast; 
38c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
38c51 61 73 74 20 63 6f 75 6d 6e 20 74 6f 20 73 65 61  ast coumn to sea
38c52 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 54 65  rch */.  int iTe
38c53 72 6d 20 3d 20 30 3b 0a 20 20 53 6e 69 70 70 65  rm = 0;.  Snippe
38c54 74 20 2a 70 53 6e 69 70 70 65 74 3b 0a 20 20 69  t *pSnippet;.  i
38c55 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
38c56 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d 3e  K;..  if( pCsr->
38c57 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20  pExpr==0 ){.    
38c58 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38c59 3b 0a 20 20 7d 0a 0a 20 20 70 53 6e 69 70 70 65  ;.  }..  pSnippe
38c5a 74 20 3d 20 28 53 6e 69 70 70 65 74 20 2a 29 73  t = (Snippet *)s
38c5b 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
38c5c 7a 65 6f 66 28 53 6e 69 70 70 65 74 29 29 3b 0a  zeof(Snippet));.
38c5d 20 20 2a 70 70 53 6e 69 70 70 65 74 20 3d 20 70    *ppSnippet = p
38c5e 53 6e 69 70 70 65 74 3b 0a 20 20 69 66 28 20 21  Snippet;.  if( !
38c5f 70 53 6e 69 70 70 65 74 20 29 7b 0a 20 20 20 20  pSnippet ){.    
38c60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
38c61 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  MEM;.  }.  memse
38c62 74 28 70 53 6e 69 70 70 65 74 2c 20 30 2c 20 73  t(pSnippet, 0, s
38c63 69 7a 65 6f 66 28 53 6e 69 70 70 65 74 29 29 3b  izeof(Snippet));
38c64 0a 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 2d  ..  nColumn = p-
38c65 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c  >nColumn;.  iCol
38c66 75 6d 6e 20 3d 20 28 70 43 73 72 2d 3e 65 53 65  umn = (pCsr->eSe
38c67 61 72 63 68 20 2d 20 32 29 3b 0a 20 20 69 66 28  arch - 2);.  if(
38c68 20 69 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20 69 43   iColumn<0 || iC
38c69 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 20 29  olumn>=nColumn )
38c6a 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f  {.    /* Look fo
38c6b 72 20 6d 61 74 63 68 65 73 20 6f 76 65 72 20 61  r matches over a
38c6c 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  ll columns of th
38c6d 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
38c6e 78 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20  x */.    iFirst 
38c6f 3d 20 30 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d  = 0;.    iLast =
38c70 20 6e 43 6f 6c 75 6d 6e 2d 31 3b 0a 20 20 7d 65   nColumn-1;.  }e
38c71 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b  lse{.    /* Look
38c72 20 66 6f 72 20 6d 61 74 63 68 65 73 20 69 6e 20   for matches in 
38c73 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63  the iColumn-th c
38c74 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
38c75 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69  ex only */.    i
38c76 46 69 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b  First = iColumn;
38c77 0a 20 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f  .    iLast = iCo
38c78 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  lumn;.  }.  for(
38c79 69 3d 69 46 69 72 73 74 3b 20 72 63 3d 3d 53 51  i=iFirst; rc==SQ
38c7a 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 3d 69 4c  LITE_OK && i<=iL
38c7b 61 73 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  ast; i++){.    c
38c7c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b  onst char *zDoc;
38c7d 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20  .    int nDoc;. 
38c7e 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74     zDoc = (const
38c7f 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63   char*)sqlite3_c
38c80 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 43 73 72 2d  olumn_text(pCsr-
38c81 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
38c82 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33    nDoc = sqlite3
38c83 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43  _column_bytes(pC
38c84 73 72 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b  sr->pStmt, i+1);
38c85 0a 20 20 20 20 69 66 28 20 7a 44 6f 63 3d 3d 30  .    if( zDoc==0
38c86 20 26 26 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   && sqlite3_colu
38c87 6d 6e 5f 74 79 70 65 28 70 43 73 72 2d 3e 70 53  mn_type(pCsr->pS
38c88 74 6d 74 2c 20 69 2b 31 29 21 3d 53 51 4c 49 54  tmt, i+1)!=SQLIT
38c89 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  E_NULL ){.      
38c8a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
38c8b 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  M;.    }else{.  
38c8c 20 20 20 20 72 63 20 3d 20 73 6e 69 70 70 65 74      rc = snippet
38c8d 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28  OffsetsOfColumn(
38c8e 70 43 73 72 2c 20 70 53 6e 69 70 70 65 74 2c 20  pCsr, pSnippet, 
38c8f 69 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a  i, zDoc, nDoc);.
38c90 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 68 69      }.  }..  whi
38c91 6c 65 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  le( trimSnippetO
38c92 66 66 73 65 74 73 28 70 43 73 72 2d 3e 70 45 78  ffsets(pCsr->pEx
38c93 70 72 2c 20 70 53 6e 69 70 70 65 74 2c 20 26 69  pr, pSnippet, &i
38c94 54 65 72 6d 29 20 29 7b 0a 20 20 20 20 69 54 65  Term) ){.    iTe
38c95 72 6d 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  rm = 0;.  }..  r
38c96 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
38c97 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 69  ** Convert the i
38c98 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
38c99 65 20 61 4d 61 74 63 68 5b 5d 20 61 72 72 61 79  e aMatch[] array
38c9a 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 0a   of the snippet.
38c9b 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 74 72 69  ** into the stri
38c9c 6e 67 20 7a 4f 66 66 73 65 74 5b 30 2e 2e 6e 4f  ng zOffset[0..nO
38c9d 66 66 73 65 74 2d 31 5d 2e 20 54 68 69 73 20 73  ffset-1]. This s
38c9e 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 61 73  tring is used as
38c9f 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 6f  .** the return o
38ca0 66 20 74 68 65 20 53 51 4c 20 6f 66 66 73 65 74  f the SQL offset
38ca1 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  s() function..*/
38ca2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
38ca3 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 53  ppetOffsetText(S
38ca4 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69 6e  nippet *p){.  in
38ca5 74 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d  t i;.  int cnt =
38ca6 20 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66   0;.  StringBuff
38ca7 65 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a 42  er sb;.  char zB
38ca8 75 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20 70  uf[200];.  if( p
38ca9 2d 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74 75  ->zOffset ) retu
38caa 72 6e 3b 0a 20 20 66 74 73 33 53 6e 69 70 70 65  rn;.  fts3Snippe
38cab 74 53 62 49 6e 69 74 28 26 73 62 29 3b 0a 20 20  tSbInit(&sb);.  
38cac 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d  for(i=0; i<p->nM
38cad 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
38cae 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
38caf 74 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70  tch *pMatch = &p
38cb0 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20  ->aMatch[i];.   
38cb1 20 69 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65   if( pMatch->iTe
38cb2 72 6d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  rm>=0 ){.      /
38cb3 2a 20 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63  * If snippetMatc
38cb4 68 2e 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20  h.iTerm is less 
38cb5 74 68 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65  than 0, then the
38cb6 20 6d 61 74 63 68 20 77 61 73 20 0a 20 20 20 20   match was .    
38cb7 20 20 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61    ** discarded a
38cb8 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
38cb9 73 69 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70  sing the NEAR op
38cba 65 72 61 74 6f 72 20 28 73 65 65 20 74 68 65 20  erator (see the 
38cbb 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e  .      ** trimSn
38cbc 69 70 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e  ippetOffsetsForN
38cbd 65 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ear() function f
38cbe 6f 72 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e  or details). Ign
38cbf 6f 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74  ore .      ** it
38cc0 20 69 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   in this case.  
38cc1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75      */.      zBu
38cc2 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  f[0] = ' ';.    
38cc3 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
38cc4 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d  tf(sizeof(zBuf)-
38cc5 31 2c 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c  1, &zBuf[cnt>0],
38cc6 20 22 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a   "%d %d %d %d",.
38cc7 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
38cc8 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e  ->iCol, pMatch->
38cc9 69 54 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69  iTerm, pMatch->i
38cca 53 74 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e  Start, pMatch->n
38ccb 42 79 74 65 29 3b 0a 20 20 20 20 20 20 66 74 73  Byte);.      fts
38ccc 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64 28 26  3SnippetAppend(&
38ccd 73 62 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a 20  sb, zBuf, -1);. 
38cce 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
38ccf 7d 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66 73  }.  }.  p->zOffs
38cd0 65 74 20 3d 20 73 62 2e 7a 3b 0a 20 20 70 2d 3e  et = sb.z;.  p->
38cd1 6e 4f 66 66 73 65 74 20 3d 20 73 62 2e 7a 20 3f  nOffset = sb.z ?
38cd2 20 73 62 2e 6e 55 73 65 64 20 3a 20 30 3b 0a 7d   sb.nUsed : 0;.}
38cd3 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e  ../*.** zDoc[0..
38cd4 6e 44 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73  nDoc-1] is phras
38cd5 65 20 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74  e of text.  aMat
38cd6 63 68 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20  ch[0..nMatch-1] 
38cd7 61 72 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20  are a set.** of 
38cd8 6d 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73  matching words s
38cd9 6f 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67  ome of which mig
38cda 68 74 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20  ht be in zDoc.  
38cdb 7a 44 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  zDoc is column.*
38cdc 2a 20 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a  * number iCol..*
38cdd 2a 0a 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73  *.** iBreak is s
38cde 75 67 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e  uggested spot in
38cdf 20 7a 44 6f 63 20 77 68 65 72 65 20 77 65 20 63   zDoc where we c
38ce0 6f 75 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e  ould begin or en
38ce1 64 20 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e  d an.** excerpt.
38ce2 20 20 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65    Return a value
38ce3 20 73 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65   similar to iBre
38ce4 61 6b 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20  ak but possibly 
38ce5 61 64 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62  adjusted.** to b
38ce6 65 20 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20  e a little left 
38ce7 6f 72 20 72 69 67 68 74 20 73 6f 20 74 68 61 74  or right so that
38ce8 20 74 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74   the break point
38ce9 20 69 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73   is better..*/.s
38cea 74 61 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f  tatic int wordBo
38ceb 75 6e 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42  undary(.  int iB
38cec 72 65 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20  reak,           
38ced 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
38cee 75 67 67 65 73 74 65 64 20 62 72 65 61 6b 20 70  uggested break p
38cef 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  oint */.  const 
38cf0 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
38cf1 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d          /* Docum
38cf2 65 6e 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ent text */.  in
38cf3 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
38cf4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
38cf5 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
38cf6 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74  n zDoc[] */.  st
38cf7 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
38cf8 68 20 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d  h *aMatch,  /* M
38cf9 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f  atching words */
38cfa 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20  .  int nMatch,  
38cfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38cfc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
38cfd 74 72 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b  tries in aMatch[
38cfe 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20  ] */.  int iCol 
38cff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38d00 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
38d01 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44  mn number for zD
38d02 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oc[] */.){.  int
38d03 20 69 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b   i;.  if( iBreak
38d04 3c 3d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  <=10 ){.    retu
38d05 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
38d06 69 42 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20  iBreak>=nDoc-10 
38d07 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44  ){.    return nD
38d08 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  oc;.  }.  for(i=
38d09 30 3b 20 41 4c 57 41 59 53 28 69 3c 6e 4d 61 74  0; ALWAYS(i<nMat
38d0a 63 68 29 20 26 26 20 61 4d 61 74 63 68 5b 69 5d  ch) && aMatch[i]
38d0b 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29  .iCol<iCol; i++)
38d0c 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4d  {}.  while( i<nM
38d0d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69  atch && aMatch[i
38d0e 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b  ].iStart+aMatch[
38d0f 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65 61 6b 20  i].nByte<iBreak 
38d10 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ i++; }.  if( 
38d11 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20 20 20 20  i<nMatch ){.    
38d12 69 66 28 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53  if( aMatch[i].iS
38d13 74 61 72 74 3c 69 42 72 65 61 6b 2b 31 30 20 29  tart<iBreak+10 )
38d14 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61  {.      return a
38d15 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 3b  Match[i].iStart;
38d16 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
38d17 3e 30 20 26 26 20 61 4d 61 74 63 68 5b 69 2d 31  >0 && aMatch[i-1
38d18 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b  ].iStart+aMatch[
38d19 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69 42 72 65  i-1].nByte>=iBre
38d1a 61 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ak ){.      retu
38d1b 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69  rn aMatch[i-1].i
38d1c 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Start;.    }.  }
38d1d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 31  .  for(i=1; i<=1
38d1e 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  0; i++){.    if(
38d1f 20 66 74 73 33 73 6e 69 70 70 65 74 49 73 73 70   fts3snippetIssp
38d20 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2d  ace(zDoc[iBreak-
38d21 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  i]) ){.      ret
38d22 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69 20 2b  urn iBreak - i +
38d23 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
38d24 28 20 66 74 73 33 73 6e 69 70 70 65 74 49 73 73  ( fts3snippetIss
38d25 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b  pace(zDoc[iBreak
38d26 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  +i]) ){.      re
38d27 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20 69 20  turn iBreak + i 
38d28 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  + 1;.    }.  }. 
38d29 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b 3b 0a   return iBreak;.
38d2a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  }..../*.** Allow
38d2b 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 6e  ed values for Sn
38d2c 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d 2e 73  ippet.aMatch[].s
38d2d 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65 66 69  nStatus.*/.#defi
38d2e 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52  ne SNIPPET_IGNOR
38d2f 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69 73 20  E  0   /* It is 
38d30 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69 73 20  ok to omit this 
38d31 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65 20 73  match from the s
38d32 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66 69 6e  nippet */.#defin
38d33 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45  e SNIPPET_DESIRE
38d34 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61 6e 74  D 1   /* We want
38d35 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 69 73   to include this
38d36 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20 73 6e   match in the sn
38d37 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ippet */../*.** 
38d38 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
38d39 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74 2e 0a  t of a snippet..
38d3a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
38d3b 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20 46 74  nippetText(.  Ft
38d3c 73 33 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  s3Cursor *pCurso
38d3d 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r,   /* The curs
38d3e 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65 20 73  or we need the s
38d3f 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20 20  nippet for */.  
38d40 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
38d41 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t,.  const char 
38d42 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20 20 20  *zStartMark,    
38d43 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70   /* Markup to ap
38d44 70 65 61 72 20 62 65 66 6f 72 65 20 65 61 63 68  pear before each
38d45 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73   match */.  cons
38d46 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61 72 6b  t char *zEndMark
38d47 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  ,       /* Marku
38d48 70 20 74 6f 20 61 70 70 65 61 72 20 61 66 74 65  p to appear afte
38d49 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  r each match */.
38d4a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
38d4b 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20 2f 2a  llipsis       /*
38d4c 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b 20 2a   Ellipsis mark *
38d4d 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
38d4e 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
38d4f 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 3b 0a  tMatch *aMatch;.
38d50 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a 20 20    int nMatch;.  
38d51 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a 20 20  int nDesired;.  
38d52 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
38d53 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a  .  int tailCol;.
38d54 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73 65 74    int tailOffset
38d55 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  ;.  int iCol;.  
38d56 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73  int nDoc;.  cons
38d57 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
38d58 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45 6e 64  int iStart, iEnd
38d59 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c 6c 69  ;.  int tailElli
38d5a 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  psis = 0;.  int 
38d5b 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20 73 71  iMatch;.  ..  sq
38d5c 6c 69 74 65 33 5f 66 72 65 65 28 70 53 6e 69 70  lite3_free(pSnip
38d5d 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a  pet->zSnippet);.
38d5e 20 20 70 53 6e 69 70 70 65 74 2d 3e 7a 53 6e 69    pSnippet->zSni
38d5f 70 70 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74  ppet = 0;.  aMat
38d60 63 68 20 3d 20 70 53 6e 69 70 70 65 74 2d 3e 61  ch = pSnippet->a
38d61 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68 20  Match;.  nMatch 
38d62 3d 20 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74  = pSnippet->nMat
38d63 63 68 3b 0a 20 20 66 74 73 33 53 6e 69 70 70 65  ch;.  fts3Snippe
38d64 74 53 62 49 6e 69 74 28 26 73 62 29 3b 0a 0a 20  tSbInit(&sb);.. 
38d65 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74   for(i=0; i<nMat
38d66 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 4d  ch; i++){.    aM
38d67 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73  atch[i].snStatus
38d68 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52   = SNIPPET_IGNOR
38d69 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69 72 65  E;.  }.  nDesire
38d6a 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  d = 0;.  for(i=0
38d6b 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52 5f 53  ; i<FTS3_ROTOR_S
38d6c 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72  Z; i++){.    for
38d6d 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20  (j=0; j<nMatch; 
38d6e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
38d6f 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d  aMatch[j].iTerm=
38d70 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4d  =i ){.        aM
38d71 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73  atch[j].snStatus
38d72 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52   = SNIPPET_DESIR
38d73 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73  ED;.        nDes
38d74 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ired++;.        
38d75 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
38d76 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61 74     }.  }..  iMat
38d77 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43 6f  ch = 0;.  tailCo
38d78 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f 66  l = -1;.  tailOf
38d79 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  fset = 0;.  for(
38d7a 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26  i=0; i<nMatch &&
38d7b 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b 2b   nDesired>0; i++
38d7c 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74 63  ){.    if( aMatc
38d7d 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d 53  h[i].snStatus!=S
38d7e 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29  NIPPET_DESIRED )
38d7f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e   continue;.    n
38d80 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 69  Desired--;.    i
38d81 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  Col = aMatch[i].
38d82 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20 3d  iCol;.    zDoc =
38d83 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
38d84 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
38d85 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74  t(pCursor->pStmt
38d86 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 6e  , iCol+1);.    n
38d87 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Doc = sqlite3_co
38d88 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72 73  lumn_bytes(pCurs
38d89 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b  or->pStmt, iCol+
38d8a 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20 3d  1);.    iStart =
38d8b 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
38d8c 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74 61  t - 40;.    iSta
38d8d 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72  rt = wordBoundar
38d8e 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c 20  y(iStart, zDoc, 
38d8f 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d  nDoc, aMatch, nM
38d90 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20  atch, iCol);.   
38d91 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30 20   if( iStart<=10 
38d92 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20  ){.      iStart 
38d93 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
38d94 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f 6c  f( iCol==tailCol
38d95 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69 6c   && iStart<=tail
38d96 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20 20  Offset+20 ){.   
38d97 20 20 20 69 53 74 61 72 74 20 3d 20 74 61 69 6c     iStart = tail
38d98 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20  Offset;.    }.  
38d99 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61 69    if( (iCol!=tai
38d9a 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c 3e  lCol && tailCol>
38d9b 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d 74  =0) || iStart!=t
38d9c 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20 20  ailOffset ){.   
38d9d 20 20 20 66 74 73 33 53 6e 69 70 70 65 74 54 72     fts3SnippetTr
38d9e 69 6d 57 68 69 74 65 53 70 61 63 65 28 26 73 62  imWhiteSpace(&sb
38d9f 29 3b 0a 20 20 20 20 20 20 66 74 73 33 53 6e 69  );.      fts3Sni
38da0 70 70 65 74 41 70 70 65 6e 64 57 68 69 74 65 53  ppetAppendWhiteS
38da1 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20  pace(&sb);.     
38da2 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70 65   fts3SnippetAppe
38da3 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
38da4 73 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 66 74  s, -1);.      ft
38da5 73 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64 57  s3SnippetAppendW
38da6 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a  hiteSpace(&sb);.
38da7 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20 3d      }.    iEnd =
38da8 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
38da9 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e 42  t + aMatch[i].nB
38daa 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69 45  yte + 40;.    iE
38dab 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72  nd = wordBoundar
38dac 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e 44  y(iEnd, zDoc, nD
38dad 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61 74  oc, aMatch, nMat
38dae 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20 69  ch, iCol);.    i
38daf 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31 30  f( iEnd>=nDoc-10
38db0 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20 3d   ){.      iEnd =
38db1 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61 69   nDoc;.      tai
38db2 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a 20  lEllipsis = 0;. 
38db3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38db4 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 31  tailEllipsis = 1
38db5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c  ;.    }.    whil
38db6 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  e( iMatch<nMatch
38db7 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
38db8 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b 20  h].iCol<iCol ){ 
38db9 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20 20  iMatch++; }.    
38dba 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45  while( iStart<iE
38dbb 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c  nd ){.      whil
38dbc 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  e( iMatch<nMatch
38dbd 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
38dbe 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72 74  h].iStart<iStart
38dbf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
38dc0 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e   aMatch[iMatch].
38dc1 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20 20  iCol<=iCol ){.  
38dc2 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b 0a        iMatch++;.
38dc3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
38dc4 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20  ( iMatch<nMatch 
38dc5 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
38dc6 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20 20  ].iStart<iEnd.  
38dc7 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d             && aM
38dc8 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f  atch[iMatch].iCo
38dc9 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20  l==iCol ){.     
38dca 20 20 20 66 74 73 33 53 6e 69 70 70 65 74 41 70     fts3SnippetAp
38dcb 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b  pend(&sb, &zDoc[
38dcc 69 53 74 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b  iStart], aMatch[
38dcd 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 20 2d  iMatch].iStart -
38dce 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
38dcf 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61 74 63    iStart = aMatc
38dd0 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
38dd1 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 6e  ;.        fts3Sn
38dd2 69 70 70 65 74 41 70 70 65 6e 64 28 26 73 62 2c  ippetAppend(&sb,
38dd3 20 7a 53 74 61 72 74 4d 61 72 6b 2c 20 2d 31 29   zStartMark, -1)
38dd4 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 6e  ;.        fts3Sn
38dd5 69 70 70 65 74 41 70 70 65 6e 64 28 26 73 62 2c  ippetAppend(&sb,
38dd6 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
38dd7 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e  aMatch[iMatch].n
38dd8 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 66  Byte);.        f
38dd9 74 73 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64  ts3SnippetAppend
38dda 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72 6b 2c 20  (&sb, zEndMark, 
38ddb 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  -1);.        iSt
38ddc 61 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d  art += aMatch[iM
38ddd 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20  atch].nByte;.   
38dde 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63       for(j=iMatc
38ddf 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a  h+1; j<nMatch; j
38de0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
38de1 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65  f( aMatch[j].iTe
38de2 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63  rm==aMatch[iMatc
38de3 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20  h].iTerm.       
38de4 20 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68         && aMatch
38de5 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e  [j].snStatus==SN
38de6 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b  IPPET_DESIRED ){
38de7 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65  .            nDe
38de8 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20  sired--;.       
38de9 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
38dea 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
38deb 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20  T_IGNORE;.      
38dec 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
38ded 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
38dee 20 20 20 20 20 66 74 73 33 53 6e 69 70 70 65 74       fts3Snippet
38def 41 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f  Append(&sb, &zDo
38df0 63 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 20  c[iStart], iEnd 
38df1 2d 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20  - iStart);.     
38df2 20 20 20 69 53 74 61 72 74 20 3d 20 69 45 6e 64     iStart = iEnd
38df3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
38df4 20 20 20 20 74 61 69 6c 43 6f 6c 20 3d 20 69 43      tailCol = iC
38df5 6f 6c 3b 0a 20 20 20 20 74 61 69 6c 4f 66 66 73  ol;.    tailOffs
38df6 65 74 20 3d 20 69 45 6e 64 3b 0a 20 20 7d 0a 20  et = iEnd;.  }. 
38df7 20 66 74 73 33 53 6e 69 70 70 65 74 54 72 69 6d   fts3SnippetTrim
38df8 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
38df9 0a 20 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70  .  if( tailEllip
38dfa 73 69 73 20 29 7b 0a 20 20 20 20 66 74 73 33 53  sis ){.    fts3S
38dfb 6e 69 70 70 65 74 41 70 70 65 6e 64 57 68 69 74  nippetAppendWhit
38dfc 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
38dfd 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70 65   fts3SnippetAppe
38dfe 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
38dff 73 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 70 53  s, -1);.  }.  pS
38e00 6e 69 70 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74  nippet->zSnippet
38e01 20 3d 20 73 62 2e 7a 3b 0a 20 20 70 53 6e 69 70   = sb.z;.  pSnip
38e02 70 65 74 2d 3e 6e 53 6e 69 70 70 65 74 20 3d 20  pet->nSnippet = 
38e03 73 62 2e 7a 20 3f 20 73 62 2e 6e 55 73 65 64 20  sb.z ? sb.nUsed 
38e04 3a 20 30 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50  : 0;.}..SQLITE_P
38e05 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
38e06 74 65 33 46 74 73 33 4f 66 66 73 65 74 73 28 0a  te3Fts3Offsets(.
38e07 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
38e08 74 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 20  t *pCtx,        
38e09 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63    /* SQLite func
38e0a 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78  tion call contex
38e0b 74 20 2a 2f 0a 20 20 46 74 73 33 43 75 72 73 6f  t */.  Fts3Curso
38e0c 72 20 2a 70 43 73 72 20 20 20 20 20 20 20 20 20  r *pCsr         
38e0d 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
38e0e 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20   object */.){.  
38e0f 53 6e 69 70 70 65 74 20 2a 70 3b 20 20 20 20 20  Snippet *p;     
38e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38e11 2f 2a 20 53 6e 69 70 70 65 74 20 73 74 72 75 63  /* Snippet struc
38e12 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ture */.  int rc
38e13 20 3d 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66   = snippetAllOff
38e14 73 65 74 73 28 70 43 73 72 2c 20 26 70 29 3b 0a  sets(pCsr, &p);.
38e15 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
38e16 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 6e 69 70 70  _OK ){.    snipp
38e17 65 74 4f 66 66 73 65 74 54 65 78 74 28 70 29 3b  etOffsetText(p);
38e18 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 4f 66 66  .    if( p->zOff
38e19 73 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  set ){.      sql
38e1a 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
38e1b 28 70 43 74 78 2c 20 70 2d 3e 7a 4f 66 66 73 65  (pCtx, p->zOffse
38e1c 74 2c 20 70 2d 3e 6e 4f 66 66 73 65 74 2c 20 53  t, p->nOffset, S
38e1d 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
38e1e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
38e1f 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
38e20 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70 43  t_error_nomem(pC
38e21 74 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  tx);.    }.  }el
38e22 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
38e23 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
38e24 65 6d 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20  em(pCtx);.  }.  
38e25 66 74 73 33 53 6e 69 70 70 65 74 46 72 65 65 28  fts3SnippetFree(
38e26 70 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  p);.}..SQLITE_PR
38e27 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
38e28 65 33 46 74 73 33 53 6e 69 70 70 65 74 28 0a 20  e3Fts3Snippet(. 
38e29 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
38e2a 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 20 20   *pCtx,         
38e2b 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63 74   /* SQLite funct
38e2c 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74  ion call context
38e2d 20 2a 2f 0a 20 20 46 74 73 33 43 75 72 73 6f 72   */.  Fts3Cursor
38e2e 20 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20   *pCsr,         
38e2f 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
38e30 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
38e31 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 2c 20  t char *zStart, 
38e32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
38e33 6e 69 70 70 65 74 20 73 74 61 72 74 20 74 65 78  nippet start tex
38e34 74 20 2d 20 22 3c 62 3e 22 20 2a 2f 0a 20 20 63  t - "<b>" */.  c
38e35 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 2c  onst char *zEnd,
38e36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38e37 2a 20 53 6e 69 70 70 65 74 20 65 6e 64 20 74 65  * Snippet end te
38e38 78 74 20 2d 20 22 3c 2f 62 3e 22 20 2a 2f 0a 20  xt - "</b>" */. 
38e39 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c   const char *zEl
38e3a 6c 69 70 73 69 73 20 20 20 20 20 20 20 20 20 20  lipsis          
38e3b 20 2f 2a 20 53 6e 69 70 70 65 74 20 65 6c 6c 69   /* Snippet elli
38e3c 70 73 69 73 20 74 65 78 74 20 2d 20 22 3c 62 3e  psis text - "<b>
38e3d 2e 2e 2e 3c 2f 62 3e 22 20 2a 2f 0a 29 7b 0a 20  ...</b>" */.){. 
38e3e 20 53 6e 69 70 70 65 74 20 2a 70 3b 20 20 20 20   Snippet *p;    
38e3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38e40 20 2f 2a 20 53 6e 69 70 70 65 74 20 73 74 72 75   /* Snippet stru
38e41 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72  cture */.  int r
38e42 63 20 3d 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66  c = snippetAllOf
38e43 66 73 65 74 73 28 70 43 73 72 2c 20 26 70 29 3b  fsets(pCsr, &p);
38e44 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
38e45 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 6e 69 70  E_OK ){.    snip
38e46 70 65 74 54 65 78 74 28 70 43 73 72 2c 20 70 2c  petText(pCsr, p,
38e47 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20 7a   zStart, zEnd, z
38e48 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 69  Ellipsis);.    i
38e49 66 28 20 70 2d 3e 7a 53 6e 69 70 70 65 74 20 29  f( p->zSnippet )
38e4a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
38e4b 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 74 78  result_text(pCtx
38e4c 2c 20 70 2d 3e 7a 53 6e 69 70 70 65 74 2c 20 70  , p->zSnippet, p
38e4d 2d 3e 6e 53 6e 69 70 70 65 74 2c 20 53 51 4c 49  ->nSnippet, SQLI
38e4e 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
38e4f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38e50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
38e51 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70 43 74 78 29  rror_nomem(pCtx)
38e52 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
38e53 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
38e54 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
38e55 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 66 74 73  pCtx);.  }.  fts
38e56 33 53 6e 69 70 70 65 74 46 72 65 65 28 70 29 3b  3SnippetFree(p);
38e57 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  .}..#endif../***
38e58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
38e59 6f 66 20 66 74 73 33 5f 73 6e 69 70 70 65 74 2e  of fts3_snippet.
38e5a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
38e5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
38e5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
38e5e 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20 2a  n file rtree.c *
38e5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
38e62 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
38e63 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
38e64 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
38e65 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
38e66 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
38e67 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
38e68 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
38e69 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
38e6a 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
38e6b 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
38e6c 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
38e6d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
38e6e 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
38e6f 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
38e70 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
38e71 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
38e72 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
38e73 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
38e74 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
38e75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38e79 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
38e7a 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
38e7b 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
38e7c 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
38e7d 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20 61  and r*-tree.** a
38e7e 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67  lgorithms packag
38e7f 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20  ed as an SQLite 
38e80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
38e81 64 75 6c 65 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64  dule..*/..#if !d
38e82 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
38e83 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
38e84 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
38e85 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EE)../*.** This 
38e86 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  file contains an
38e87 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
38e88 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  of a couple of d
38e89 69 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74  ifferent variant
38e8a 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72  s.** of the r-tr
38e8b 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65  ee algorithm. Se
38e8c 65 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c  e the README fil
38e8d 65 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65  e for further de
38e8e 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73  tails. The .** s
38e8f 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75  ame data-structu
38e90 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  re is used for a
38e91 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f  ll, but the algo
38e92 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72  rithms for inser
38e93 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20  t and.** delete 
38e94 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e  operations vary.
38e95 20 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73   The variants us
38e96 65 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  ed are selected 
38e97 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
38e98 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20  .** by defining 
38e99 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  the following sy
38e9a 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69  mbols:.*/../* Ei
38e9b 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f  ther, both or no
38e9c 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
38e9d 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74  ing may be set t
38e9e 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72  o activate .** r
38e9f 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c  *tree variant al
38ea0 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65  gorithms..*/.#de
38ea1 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
38ea2 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
38ea3 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56  TREE 0.#define V
38ea4 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
38ea5 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31  _REINSERT      1
38ea6 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79  ../* .** Exactly
38ea7 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
38ea8 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65  owing must be se
38ea9 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69  t to 1..*/.#defi
38eaa 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
38eab 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
38eac 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52  IT 0.#define VAR
38ead 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
38eae 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23  EAR_SPLIT    0.#
38eaf 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
38eb0 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20  STARTREE_SPLIT  
38eb1 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e         1..#defin
38eb2 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
38eb3 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20  N_SPLIT \.      
38eb4 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d    (VARIANT_GUTTM
38eb5 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c  AN_LINEAR_SPLIT|
38eb6 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e  |VARIANT_GUTTMAN
38eb7 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
38eb8 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  )..#if VARIANT_G
38eb9 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
38eba 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65  _SPLIT.  #define
38ebb 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61   PickNext Quadra
38ebc 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64  ticPickNext.  #d
38ebd 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
38ebe 51 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65  QuadraticPickSee
38ebf 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
38ec0 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
38ec1 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
38ec2 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
38ec3 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
38ec4 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
38ec5 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e  Next LinearPickN
38ec6 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69  ext.  #define Pi
38ec7 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69  ckSeeds LinearPi
38ec8 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e  ckSeeds.  #defin
38ec9 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
38eca 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23  litNodeGuttman.#
38ecb 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e  endif.#if VARIAN
38ecc 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
38ecd 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  T.  #define Assi
38ece 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
38ecf 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66  eStartree.#endif
38ed0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
38ed1 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f  E_CORE.  SQLITE_
38ed2 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
38ed3 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 0a 23  #else.#endif...#
38ed4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
38ed5 41 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70 65 64  ALGAMATION.typed
38ed6 65 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  ef sqlite3_int64
38ed7 20 69 36 34 3b 0a 74 79 70 65 64 65 66 20 75 6e   i64;.typedef un
38ed8 73 69 67 6e 65 64 20 63 68 61 72 20 75 38 3b 0a  signed char u8;.
38ed9 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
38eda 20 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64 69 66   int u32;.#endif
38edb 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
38edc 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74 79   Rtree Rtree;.ty
38edd 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
38ede 65 65 43 75 72 73 6f 72 20 52 74 72 65 65 43 75  eeCursor RtreeCu
38edf 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
38ee0 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20 52  ruct RtreeNode R
38ee1 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64 65  treeNode;.typede
38ee2 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43 65  f struct RtreeCe
38ee3 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74 79  ll RtreeCell;.ty
38ee4 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
38ee5 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74 72  eeConstraint Rtr
38ee6 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74 79  eeConstraint;.ty
38ee7 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65  pedef union Rtre
38ee8 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72  eCoord RtreeCoor
38ee9 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65 65  d;../* The rtree
38eea 20 6d 61 79 20 68 61 76 65 20 62 65 74 77 65 65   may have betwee
38eeb 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d 41  n 1 and RTREE_MA
38eec 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69 6d  X_DIMENSIONS dim
38eed 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65 66  ensions. */.#def
38eee 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 49  ine RTREE_MAX_DI
38eef 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20 53  MENSIONS 5../* S
38ef0 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62 6c  ize of hash tabl
38ef1 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20 54  e Rtree.aHash. T
38ef2 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 69  his hash table i
38ef3 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74  s not expected t
38ef4 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61 69  o.** ever contai
38ef5 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74 72  n very many entr
38ef6 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64 20  ies, so a fixed 
38ef7 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  number of bucket
38ef8 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a 2a  s is .** used..*
38ef9 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53 49  /.#define HASHSI
38efa 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20 41  ZE 128../* .** A
38efb 6e 20 72 74 72 65 65 20 76 69 72 74 75 61 6c 2d  n rtree virtual-
38efc 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  table object..*/
38efd 0a 73 74 72 75 63 74 20 52 74 72 65 65 20 7b 0a  .struct Rtree {.
38efe 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62    sqlite3_vtab b
38eff 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ase;.  sqlite3 *
38f00 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
38f01 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62     /* Host datab
38f02 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
38f03 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69 7a  /.  int iNodeSiz
38f04 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
38f05 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 73  /* Size in bytes
38f06 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69 6e   of each node in
38f07 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65 20   the node table 
38f08 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20 20  */.  int nDim;  
38f09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38f0a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 69   /* Number of di
38f0b 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  mensions */.  in
38f0c 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  t nBytesPerCell;
38f0d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
38f0e 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72 20  es consumed per 
38f0f 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 44  cell */.  int iD
38f10 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  epth;           
38f11 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
38f12 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 72 2d   depth of the r-
38f13 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
38f14 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  /.  char *zDb;  
38f15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38f16 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
38f17 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72  ase containing r
38f18 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  -tree table */. 
38f19 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
38f1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38f1b 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20 74  Name of r-tree t
38f1c 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65 65  able */ .  Rtree
38f1d 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53 48  Node *aHash[HASH
38f1e 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20 74  SIZE]; /* Hash t
38f1f 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  able of in-memor
38f20 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20 69  y nodes. */ .  i
38f21 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
38f22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
38f23 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
38f24 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 73 74  users of this st
38f25 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f 2a  ructure */..  /*
38f26 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20 72   List of nodes r
38f27 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 20  emoved during a 
38f28 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70 65  CondenseTree ope
38f29 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73 0a  ration. List is.
38f2a 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67 65    ** linked toge
38f2b 74 68 65 72 20 76 69 61 20 74 68 65 20 70 6f 69  ther via the poi
38f2c 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75 73  nter normally us
38f2d 65 64 20 66 6f 72 20 68 61 73 68 20 63 68 61 69  ed for hash chai
38f2e 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65 4e  ns -.  ** RtreeN
38f2f 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65 65  ode.pNext. Rtree
38f30 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72 65  Node.iNode store
38f31 73 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74  s the depth of t
38f32 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20 2a  he sub-tree .  *
38f33 2a 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  * headed by the 
38f34 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65 73  node (leaf nodes
38f35 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65 2e   have RtreeNode.
38f36 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f 0a  iNode==0)..  */.
38f37 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65    RtreeNode *pDe
38f38 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52 65  leted;.  int iRe
38f39 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20  insertHeight;   
38f3a 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f       /* Height o
38f3b 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e  f sub-trees Rein
38f3c 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f  sert() has run o
38f3d 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74 65  n */..  /* State
38f3e 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72  ments to read/wr
38f3f 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63  ite/delete a rec
38f40 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f 64  ord from xxx_nod
38f41 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  e */.  sqlite3_s
38f42 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b 0a  tmt *pReadNode;.
38f43 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
38f44 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73 71  pWriteNode;.  sq
38f45 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
38f46 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 53  eteNode;..  /* S
38f47 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61  tatements to rea
38f48 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61  d/write/delete a
38f49 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78   record from xxx
38f4a 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c 69  _rowid */.  sqli
38f4b 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 52  te3_stmt *pReadR
38f4c 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  owid;.  sqlite3_
38f4d 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77 69  stmt *pWriteRowi
38f4e 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  d;.  sqlite3_stm
38f4f 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64 3b  t *pDeleteRowid;
38f50 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74  ..  /* Statement
38f51 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f  s to read/write/
38f52 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20  delete a record 
38f53 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74 20  from xxx_parent 
38f54 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
38f55 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b 0a  t *pReadParent;.
38f56 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
38f57 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
38f58 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
38f59 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20  eleteParent;..  
38f5a 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a  int eCoordType;.
38f5b 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20  };../* Possible 
38f5c 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f 72  values for eCoor
38f5d 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69 6e  dType: */.#defin
38f5e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  e RTREE_COORD_RE
38f5f 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20 52  AL32 0.#define R
38f60 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
38f61 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d    1../*.** The m
38f62 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  inimum number of
38f63 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64 20 66   cells allowed f
38f64 6f 72 20 61 20 6e 6f 64 65 20 69 73 20 61 20 74  or a node is a t
38f65 68 69 72 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20  hird of the .** 
38f66 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75 74 6d  maximum. In Gutm
38f67 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a  an's notation:.*
38f68 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d 2f 33  *.**     m = M/3
38f69 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d  .**.** If an R*-
38f6a 74 72 65 65 20 22 52 65 69 6e 73 65 72 74 22 20  tree "Reinsert" 
38f6b 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
38f6c 75 69 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20  uired, the same 
38f6d 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
38f6e 6c 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  ls are removed f
38f6f 72 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c  rom the overfull
38f70 20 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65   node and reinse
38f71 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
38f72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ee..*/.#define R
38f73 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29  TREE_MINCELLS(p)
38f74 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53 69   ((((p)->iNodeSi
38f75 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74 65  ze-4)/(p)->nByte
38f76 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23 64 65  sPerCell)/3).#de
38f77 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49 4e 53  fine RTREE_REINS
38f78 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d 49 4e  ERT(p) RTREE_MIN
38f79 43 45 4c 4c 53 28 70 29 0a 23 64 65 66 69 6e 65  CELLS(p).#define
38f7a 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 20   RTREE_MAXCELLS 
38f7b 35 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74  51../* .** An rt
38f7c 72 65 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  ree cursor objec
38f7d 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  t..*/.struct Rtr
38f7e 65 65 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  eeCursor {.  sql
38f7f 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
38f80 20 62 61 73 65 3b 0a 20 20 52 74 72 65 65 4e 6f   base;.  RtreeNo
38f81 64 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20 20 20  de *pNode;      
38f82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
38f83 64 65 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  de cursor is cur
38f84 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
38f85 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  at */.  int iCel
38f86 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
38f87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
38f88 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65  ex of current ce
38f89 6c 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20  ll in pNode */. 
38f8a 20 69 6e 74 20 69 53 74 72 61 74 65 67 79 3b 20   int iStrategy; 
38f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38f8c 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64     /* Copy of id
38f8d 78 4e 75 6d 20 73 65 61 72 63 68 20 70 61 72 61  xNum search para
38f8e 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
38f8f 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
38f90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38f91 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
38f92 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74  s in aConstraint
38f93 20 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e 73 74   */.  RtreeConst
38f94 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69  raint *aConstrai
38f95 6e 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63  nt;     /* Searc
38f96 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  h constraints. *
38f97 2f 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74 72 65  /.};..union Rtre
38f98 65 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f 61 74  eCoord {.  float
38f99 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a   f;.  int i;.};.
38f9a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
38f9b 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43  ent is an RtreeC
38f9c 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65  oord. Return the
38f9d 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69   value stored wi
38f9e 74 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f  thin the RtreeCo
38f9f 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64  ord.** formatted
38fa0 20 61 73 20 61 20 64 6f 75 62 6c 65 2e 20 54 68   as a double. Th
38fa1 69 73 20 6d 61 63 72 6f 20 61 73 73 75 6d 65 73  is macro assumes
38fa2 20 74 68 61 74 20 6c 6f 63 61 6c 20 76 61 72 69   that local vari
38fa3 61 62 6c 65 20 70 52 74 72 65 65 20 70 6f 69 6e  able pRtree poin
38fa4 74 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52 74 72  ts.** to the Rtr
38fa5 65 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  ee structure ass
38fa6 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
38fa7 20 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a 2f 0a   RtreeCoord..*/.
38fa8 23 64 65 66 69 6e 65 20 44 43 4f 4f 52 44 28 63  #define DCOORD(c
38fa9 6f 6f 72 64 29 20 28 20 20 20 20 20 20 20 20 20  oord) (         
38faa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38fab 20 20 5c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65    \.  (pRtree->e
38fac 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
38fad 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f  _COORD_REAL32) ?
38fae 20 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f        \.    ((do
38faf 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20  uble)coord.f) : 
38fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38fb1 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
38fb2 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69  ((double)coord.i
38fb3 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
38fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
38fb5 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61 72 63  )../*.** A searc
38fb6 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  h constraint..*/
38fb7 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e  .struct RtreeCon
38fb8 73 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e 74 20  straint {.  int 
38fb9 69 43 6f 6f 72 64 3b 20 20 20 20 20 20 20 20 20  iCoord;         
38fba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38fbb 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73 74 72   Index of constr
38fbc 61 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61 74 65  ained coordinate
38fbd 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20   */.  int op;   
38fbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38fbf 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
38fc0 72 61 69 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  raining operatio
38fc1 6e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 56  n */.  double rV
38fc2 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  alue;           
38fc3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73           /* Cons
38fc4 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20 2a 2f  traint value. */
38fc5 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65  .};../* Possible
38fc6 20 76 61 6c 75 65 73 20 66 6f 72 20 52 74 72 65   values for Rtre
38fc7 65 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70 20 2a  eConstraint.op *
38fc8 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
38fc9 45 51 20 30 78 34 31 0a 23 64 65 66 69 6e 65 20  EQ 0x41.#define 
38fca 52 54 52 45 45 5f 4c 45 20 30 78 34 32 0a 23 64  RTREE_LE 0x42.#d
38fcb 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 30  efine RTREE_LT 0
38fcc 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x43.#define RTRE
38fcd 45 5f 47 45 20 30 78 34 34 0a 23 64 65 66 69 6e  E_GE 0x44.#defin
38fce 65 20 52 54 52 45 45 5f 47 54 20 30 78 34 35 0a  e RTREE_GT 0x45.
38fcf 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
38fd0 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e   structure node.
38fd1 0a 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f 72 6d  .**.** Data form
38fd2 61 74 20 28 52 74 72 65 65 4e 6f 64 65 2e 7a 44  at (RtreeNode.zD
38fd3 61 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ata):.**.**   1.
38fd4 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
38fd5 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 6e  the root node (n
38fd6 6f 64 65 20 31 29 2c 20 74 68 65 6e 20 74 68 65  ode 1), then the
38fd7 20 66 69 72 73 74 20 32 20 62 79 74 65 73 0a 2a   first 2 bytes.*
38fd8 2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 6f  *      of the no
38fd9 64 65 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74  de contain the t
38fda 72 65 65 20 64 65 70 74 68 20 61 73 20 61 20 62  ree depth as a b
38fdb 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
38fdc 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e  r..**      For n
38fdd 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74  on-root nodes, t
38fde 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73  he first 2 bytes
38fdf 20 61 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64   are left unused
38fe0 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
38fe1 20 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f   next 2 bytes co
38fe2 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
38fe3 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72   of entries curr
38fe4 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20 20 73  ently .**      s
38fe5 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f 64  tored in the nod
38fe6 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 54 68  e..**.**   3. Th
38fe7 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
38fe8 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 73  he node contains
38fe9 20 74 68 65 20 6e 6f 64 65 20 65 6e 74 72 69 65   the node entrie
38fea 73 2e 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a  s. Each entry.**
38feb 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
38fec 66 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 79 74  f a single 8-byt
38fed 65 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c 6f 77  e integer follow
38fee 65 64 20 62 79 20 61 6e 20 65 76 65 6e 20 6e 75  ed by an even nu
38fef 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f 66 20  mber.**      of 
38ff0 34 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74  4-byte coordinat
38ff1 65 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e 6f 64  es. For leaf nod
38ff2 65 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  es the integer i
38ff3 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 20  s the rowid.**  
38ff4 20 20 20 20 6f 66 20 61 20 72 65 63 6f 72 64 2e      of a record.
38ff5 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   For internal no
38ff6 64 65 73 20 69 74 20 69 73 20 74 68 65 20 6e 6f  des it is the no
38ff7 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 0a 2a  de number of a.*
38ff8 2a 20 20 20 20 20 20 63 68 69 6c 64 20 70 61 67  *      child pag
38ff9 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  e..*/.struct Rtr
38ffa 65 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65  eeNode {.  Rtree
38ffb 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20  Node *pParent;  
38ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38ffd 50 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20  Parent node */. 
38ffe 20 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e   i64 iNode;.  in
38fff 74 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73  t nRef;.  int is
39000 44 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61  Dirty;.  u8 *zDa
39001 74 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ta;.  RtreeNode 
39002 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
39003 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
39004 6e 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73  node in this has
39005 68 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64  h chain */.};.#d
39006 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64  efine NCELL(pNod
39007 65 29 20 72 65 61 64 49 6e 74 31 36 28 26 28 70  e) readInt16(&(p
39008 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29  Node)->zData[2])
39009 0a 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75  ../* .** Structu
3900a 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65  re to store a de
3900b 73 65 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65  serialized rtree
3900c 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75   record..*/.stru
3900d 63 74 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20  ct RtreeCell {. 
3900e 20 69 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52   i64 iRowid;.  R
3900f 74 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64  treeCoord aCoord
39010 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
39011 53 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69  SIONS*2];.};..#i
39012 66 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69  fndef MAX.# defi
39013 6e 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29  ne MAX(x,y) ((x)
39014 20 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28   < (y) ? (y) : (
39015 78 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  x)).#endif.#ifnd
39016 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
39017 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20  MIN(x,y) ((x) > 
39018 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29  (y) ? (y) : (x))
39019 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
3901a 75 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65  unctions to dese
3901b 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74  rialize a 16 bit
3901c 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74   integer, 32 bit
3901d 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64   real number and
3901e 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67  .** 64 bit integ
3901f 65 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c  er. The deserial
39020 69 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  ized value is re
39021 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
39022 63 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28  c int readInt16(
39023 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 *p){.  return
39024 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
39025 5d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  ];.}.static void
39026 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70   readCoord(u8 *p
39027 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43  , RtreeCoord *pC
39028 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d  oord){.  u32 i =
39029 20 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   (.    (((u32)p[
3902a 30 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  0]) << 24) + .  
3902b 20 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c    (((u32)p[1]) <
3902c 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
3902d 75 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29  u32)p[2]) <<  8)
3902e 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
3902f 5b 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [3]) <<  0).  );
39030 0a 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72  .  *(u32 *)pCoor
39031 64 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20  d = i;.}.static 
39032 69 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38  i64 readInt64(u8
39033 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
39034 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d  .    (((i64)p[0]
39035 29 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20  ) << 56) + .    
39036 28 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20  (((i64)p[1]) << 
39037 34 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  48) + .    (((i6
39038 34 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b  4)p[2]) << 40) +
39039 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33   .    (((i64)p[3
3903a 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20  ]) << 32) + .   
3903b 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c   (((i64)p[4]) <<
3903c 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69   24) + .    (((i
3903d 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20  64)p[5]) << 16) 
3903e 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
3903f 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  6]) <<  8) + .  
39040 20 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c    (((i64)p[7]) <
39041 3c 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  <  0).  );.}../*
39042 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f  .** Functions to
39043 20 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20   serialize a 16 
39044 62 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20  bit integer, 32 
39045 62 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20  bit real number 
39046 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e  and.** 64 bit in
39047 74 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65  teger. The value
39048 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
39049 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
3904a 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
3904b 68 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66  he argument buff
3904c 65 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20  er (always 2, 4 
3904d 61 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65  and 8 respective
3904e 6c 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly)..*/.static i
3904f 6e 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38  nt writeInt16(u8
39050 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70   *p, int i){.  p
39051 5b 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [0] = (i>> 8)&0x
39052 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e  FF;.  p[1] = (i>
39053 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
39054 75 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 2;.}.static 
39055 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75  int writeCoord(u
39056 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64  8 *p, RtreeCoord
39057 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32   *pCoord){.  u32
39058 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   i;.  assert( si
39059 7a 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29  zeof(RtreeCoord)
3905a 3d 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==4 );.  assert(
3905b 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
3905c 29 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a  );.  i = *(u32 *
3905d 29 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20  )pCoord;.  p[0] 
3905e 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a  = (i>>24)&0xFF;.
3905f 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29    p[1] = (i>>16)
39060 26 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20  &0xFF;.  p[2] = 
39061 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
39062 70 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[3] = (i>> 0)&0
39063 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b  xFF;.  return 4;
39064 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
39065 69 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20  iteInt64(u8 *p, 
39066 69 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  i64 i){.  p[0] =
39067 20 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20   (i>>56)&0xFF;. 
39068 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26   p[1] = (i>>48)&
39069 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
3906a 69 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70  i>>40)&0xFF;.  p
3906b 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78  [3] = (i>>32)&0x
3906c 46 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e  FF;.  p[4] = (i>
3906d 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35  >24)&0xFF;.  p[5
3906e 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
3906f 3b 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20  ;.  p[6] = (i>> 
39070 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20  8)&0xFF;.  p[7] 
39071 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
39072 20 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f    return 8;.}../
39073 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  *.** Increment t
39074 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
39075 6e 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f  nt of node p..*/
39076 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
39077 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65  eReference(Rtree
39078 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Node *p){.  if( 
39079 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  p ){.    p->nRef
3907a 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
3907b 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65   Clear the conte
3907c 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65  nt of node p (se
3907d 74 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30  t all bytes to 0
3907e 78 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  x00)..*/.static 
3907f 76 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74  void nodeZero(Rt
39080 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
39081 65 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66  eeNode *p){.  if
39082 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
39083 74 28 26 70 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20  t(&p->zData[2], 
39084 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
39085 53 69 7a 65 2d 32 29 3b 0a 20 20 20 20 70 2d 3e  Size-2);.    p->
39086 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 7d  isDirty = 1;.  }
39087 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
39088 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e  a node number iN
39089 6f 64 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ode, return the 
3908a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65  corresponding ke
3908b 79 20 74 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74  y to use.** in t
3908c 68 65 20 52 74 72 65 65 2e 61 48 61 73 68 20 74  he Rtree.aHash t
3908d 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
3908e 69 6e 74 20 6e 6f 64 65 48 61 73 68 28 69 36 34  int nodeHash(i64
3908f 20 69 4e 6f 64 65 29 7b 0a 20 20 72 65 74 75 72   iNode){.  retur
39090 6e 20 28 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e  n (.    (iNode>>
39091 35 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38  56) ^ (iNode>>48
39092 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20  ) ^ (iNode>>40) 
39093 5e 20 28 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20  ^ (iNode>>32) ^ 
39094 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29  .    (iNode>>24)
39095 20 5e 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e   ^ (iNode>>16) ^
39096 20 28 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28   (iNode>> 8) ^ (
39097 69 4e 6f 64 65 3e 3e 20 30 29 0a 20 20 29 20 25  iNode>> 0).  ) %
39098 20 48 41 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a   HASHSIZE;.}../*
39099 0a 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 6e  .** Search the n
3909a 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 20 66  ode hash table f
3909b 6f 72 20 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49  or node iNode. I
3909c 66 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  f found, return 
3909d 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
3909e 69 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  it. Otherwise, r
3909f 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
390a0 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
390a1 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72  deHashLookup(Rtr
390a2 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
390a3 69 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e  iNode){.  RtreeN
390a4 6f 64 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  ode *p;.  assert
390a5 28 20 69 4e 6f 64 65 21 3d 30 20 29 3b 0a 20 20  ( iNode!=0 );.  
390a6 66 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e 61 48  for(p=pRtree->aH
390a7 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 69 4e 6f  ash[nodeHash(iNo
390a8 64 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e 69 4e  de)]; p && p->iN
390a9 6f 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d 70 2d  ode!=iNode; p=p-
390aa 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72  >pNext);.  retur
390ab 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  n p;.}../*.** Ad
390ac 64 20 6e 6f 64 65 20 70 4e 6f 64 65 20 74 6f 20  d node pNode to 
390ad 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
390ae 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
390af 6f 69 64 20 6e 6f 64 65 48 61 73 68 49 6e 73 65  oid nodeHashInse
390b0 72 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  rt(Rtree *pRtree
390b1 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
390b2 64 65 29 7b 0a 20 20 69 66 28 20 70 4e 6f 64 65  de){.  if( pNode
390b3 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 61 73   ){.    int iHas
390b4 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  h;.    assert( p
390b5 4e 6f 64 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  Node->pNext==0 )
390b6 3b 0a 20 20 20 20 69 48 61 73 68 20 3d 20 6e 6f  ;.    iHash = no
390b7 64 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e  deHash(pNode->iN
390b8 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  ode);.    pNode-
390b9 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
390ba 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20  >aHash[iHash];. 
390bb 20 20 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68     pRtree->aHash
390bc 5b 69 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b  [iHash] = pNode;
390bd 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
390be 6d 6f 76 65 20 6e 6f 64 65 20 70 4e 6f 64 65 20  move node pNode 
390bf 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65 20 68 61  from the node ha
390c0 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
390c1 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61 73  tic void nodeHas
390c2 68 44 65 6c 65 74 65 28 52 74 72 65 65 20 2a 70  hDelete(Rtree *p
390c3 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
390c4 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
390c5 65 4e 6f 64 65 20 2a 2a 70 70 3b 0a 20 20 69 66  eNode **pp;.  if
390c6 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d  ( pNode->iNode!=
390c7 30 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 70  0 ){.    pp = &p
390c8 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64  Rtree->aHash[nod
390c9 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f  eHash(pNode->iNo
390ca 64 65 29 5d 3b 0a 20 20 20 20 66 6f 72 28 20 3b  de)];.    for( ;
390cb 20 28 2a 70 70 29 21 3d 70 4e 6f 64 65 3b 20 70   (*pp)!=pNode; p
390cc 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
390cd 74 29 7b 20 61 73 73 65 72 74 28 2a 70 70 29 3b  t){ assert(*pp);
390ce 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 4e 6f   }.    *pp = pNo
390cf 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70  de->pNext;.    p
390d0 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Node->pNext = 0;
390d1 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
390d2 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  locate and retur
390d3 6e 20 6e 65 77 20 72 2d 74 72 65 65 20 6e 6f 64  n new r-tree nod
390d4 65 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20 28 52  e. Initially, (R
390d5 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d  treeNode.iNode==
390d6 30 29 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  0),.** indicatin
390d7 67 20 74 68 61 74 20 6e 6f 64 65 20 68 61 73 20  g that node has 
390d8 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 61 73 73  not yet been ass
390d9 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d  igned a node num
390da 62 65 72 2e 20 49 74 20 69 73 0a 2a 2a 20 61 73  ber. It is.** as
390db 73 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75  signed a node nu
390dc 6d 62 65 72 20 77 68 65 6e 20 6e 6f 64 65 57 72  mber when nodeWr
390dd 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
390de 74 6f 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20  to write the.** 
390df 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 73 20 6f 75  node contents ou
390e0 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
390e1 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  e..*/.static Rtr
390e2 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 4e 65 77 28  eeNode *nodeNew(
390e3 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
390e4 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
390e5 74 2c 20 69 6e 74 20 7a 65 72 6f 29 7b 0a 20 20  t, int zero){.  
390e6 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
390e7 3b 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ;.  pNode = (Rtr
390e8 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
390e9 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
390ea 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
390eb 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
390ec 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
390ed 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c     memset(pNode,
390ee 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
390ef 4e 6f 64 65 29 20 2b 20 28 7a 65 72 6f 3f 70 52  Node) + (zero?pR
390f0 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3a  tree->iNodeSize:
390f1 30 29 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  0));.    pNode->
390f2 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70  zData = (u8 *)&p
390f3 4e 6f 64 65 5b 31 5d 3b 0a 20 20 20 20 70 4e 6f  Node[1];.    pNo
390f4 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
390f5 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74    pNode->pParent
390f6 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20   = pParent;.    
390f7 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
390f8 20 31 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65   1;.    nodeRefe
390f9 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
390fa 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f    }.  return pNo
390fb 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  de;.}../*.** Obt
390fc 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
390fd 74 6f 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  to an r-tree nod
390fe 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
390ff 0a 6e 6f 64 65 41 63 71 75 69 72 65 28 0a 20 20  .nodeAcquire(.  
39100 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
39101 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2d             /* R-
39102 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
39103 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65 2c 20 20  /.  i64 iNode,  
39104 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39105 2a 20 4e 6f 64 65 20 6e 75 6d 62 65 72 20 74 6f  * Node number to
39106 20 6c 6f 61 64 20 2a 2f 0a 20 20 52 74 72 65 65   load */.  Rtree
39107 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Node *pParent,  
39108 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20        /* Either 
39109 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20  the parent node 
3910a 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 52 74 72  or NULL */.  Rtr
3910b 65 65 4e 6f 64 65 20 2a 2a 70 70 4e 6f 64 65 20  eeNode **ppNode 
3910c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3910d 41 63 71 75 69 72 65 64 20 6e 6f 64 65 20 2a 2f  Acquired node */
3910e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
3910f 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
39110 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
39111 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6e   the requested n
39112 6f 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  ode is already i
39113 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
39114 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e  . If so,.  ** in
39115 63 72 65 61 73 65 20 69 74 73 20 72 65 66 65 72  crease its refer
39116 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72  ence count and r
39117 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20  eturn it..  */. 
39118 20 69 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f   if( (pNode = no
39119 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
3911a 72 65 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a  ree, iNode)) ){.
3911b 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
3911c 72 65 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e  rent || !pNode->
3911d 70 50 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65  pParent || pNode
3911e 2d 3e 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65  ->pParent==pPare
3911f 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  nt );.    if( pP
39120 61 72 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d  arent && !pNode-
39121 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
39122 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
39123 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
39124 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pNode->pParent =
39125 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a   pParent;.    }.
39126 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b      pNode->nRef+
39127 2b 3b 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d  +;.    *ppNode =
39128 20 70 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75   pNode;.    retu
39129 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
3912a 7d 0a 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74  }..  pNode = (Rt
3912b 72 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65  reeNode *)sqlite
3912c 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3912d 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74  RtreeNode) + pRt
3912e 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
3912f 0a 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b  .  if( !pNode ){
39130 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30  .    *ppNode = 0
39131 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
39132 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
39133 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
39134 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f  = pParent;.  pNo
39135 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75 38 20  de->zData = (u8 
39136 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20 20 70  *)&pNode[1];.  p
39137 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Node->nRef = 1;.
39138 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d    pNode->iNode =
39139 20 69 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 2d   iNode;.  pNode-
3913a 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
3913b 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30  pNode->pNext = 0
3913c 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ;..  sqlite3_bin
3913d 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
3913e 70 52 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e  pReadNode, 1, iN
3913f 6f 64 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ode);.  rc = sql
39140 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39141 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20  ->pReadNode);.  
39142 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
39143 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OW ){.    const 
39144 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69  u8 *zBlob = sqli
39145 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
39146 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
39147 65 2c 20 30 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e, 0);.    memcp
39148 79 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20  y(pNode->zData, 
39149 7a 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69  zBlob, pRtree->i
3914a 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 6e  NodeSize);.    n
3914b 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
3914c 72 65 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rent);.  }else{.
3914d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3914e 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f  (pNode);.    pNo
3914f 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a  de = 0;.  }..  *
39150 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a  ppNode = pNode;.
39151 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
39152 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
39153 61 64 4e 6f 64 65 29 3b 0a 0a 20 20 69 66 28 20  adNode);..  if( 
39154 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
39155 20 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20   iNode==1 ){.   
39156 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
39157 3d 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64  = readInt16(pNod
39158 65 2d 3e 7a 44 61 74 61 29 3b 0a 20 20 7d 0a 0a  e->zData);.  }..
39159 20 20 61 73 73 65 72 74 28 20 28 72 63 3d 3d 53    assert( (rc==S
3915a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4e 6f 64  QLITE_OK && pNod
3915b 65 29 20 7c 7c 20 28 70 4e 6f 64 65 3d 3d 30 20  e) || (pNode==0 
3915c 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  && rc!=SQLITE_OK
3915d 29 20 29 3b 0a 20 20 6e 6f 64 65 48 61 73 68 49  ) );.  nodeHashI
3915e 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
3915f 6f 64 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ode);..  return 
39160 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65  rc;.}../*.** Ove
39161 72 77 72 69 74 65 20 63 65 6c 6c 20 69 43 65 6c  rwrite cell iCel
39162 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20  l of node pNode 
39163 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  with the content
39164 73 20 6f 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  s of pCell..*/.s
39165 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f  tatic void nodeO
39166 76 65 72 77 72 69 74 65 43 65 6c 6c 28 0a 20 20  verwriteCell(.  
39167 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
39168 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
39169 64 65 2c 20 20 0a 20 20 52 74 72 65 65 43 65 6c  de,  .  RtreeCel
3916a 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *pCell, .  int
3916b 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20   iCell.){.  int 
3916c 69 69 3b 0a 20 20 75 38 20 2a 70 20 3d 20 26 70  ii;.  u8 *p = &p
3916d 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20  Node->zData[4 + 
3916e 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
3916f 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20  rCell*iCell];.  
39170 70 20 2b 3d 20 77 72 69 74 65 49 6e 74 36 34 28  p += writeInt64(
39171 70 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  p, pCell->iRowid
39172 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39173 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
39174 32 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70  2); ii++){.    p
39175 20 2b 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70   += writeCoord(p
39176 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64  , &pCell->aCoord
39177 5b 69 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f  [ii]);.  }.  pNo
39178 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
39179 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
3917a 20 63 65 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77   cell the cell w
3917b 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20  ith index iCell 
3917c 66 72 6f 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  from node pNode.
3917d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3917e 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 52  nodeDeleteCell(R
3917f 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
39180 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
39181 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38  int iCell){.  u8
39182 20 2a 70 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d   *pDst = &pNode-
39183 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
39184 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
39185 2a 69 43 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70  *iCell];.  u8 *p
39186 53 72 63 20 3d 20 26 70 44 73 74 5b 70 52 74 72  Src = &pDst[pRtr
39187 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
39188 6c 5d 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  l];.  int nByte 
39189 3d 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20  = (NCELL(pNode) 
3918a 2d 20 69 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70  - iCell - 1) * p
3918b 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
3918c 43 65 6c 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28  Cell;.  memmove(
3918d 70 44 73 74 2c 20 70 53 72 63 2c 20 6e 42 79 74  pDst, pSrc, nByt
3918e 65 29 3b 0a 20 20 77 72 69 74 65 49 6e 74 31 36  e);.  writeInt16
3918f 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32  (&pNode->zData[2
39190 5d 2c 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d  ], NCELL(pNode)-
39191 31 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44  1);.  pNode->isD
39192 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
39193 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  ** Insert the co
39194 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
39195 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70  Cell into node p
39196 4e 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73  Node. If the ins
39197 65 72 74 0a 2a 2a 20 69 73 20 73 75 63 63 65 73  ert.** is succes
39198 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
39199 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
3919a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 65 6e   there is not en
3919b 6f 75 67 68 20 66 72 65 65 20 73 70 61 63 65 20  ough free space 
3919c 69 6e 20 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e  in pNode, return
3919d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f   SQLITE_FULL..*/
3919e 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
3919f 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74  InsertCell(.  Rt
391a0 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
391a1 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
391a2 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
391a3 70 43 65 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20  pCell .){.  int 
391a4 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
391a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
391a6 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63  rent number of c
391a7 65 6c 6c 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f  ells in pNode */
391a8 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b  .  int nMaxCell;
391a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
391aa 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
391ab 65 72 20 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20  er of cells for 
391ac 70 4e 6f 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78  pNode */..  nMax
391ad 43 65 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e  Cell = (pRtree->
391ae 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74  iNodeSize-4)/pRt
391af 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
391b0 6c 6c 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43  ll;.  nCell = NC
391b1 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61  ELL(pNode);..  a
391b2 73 73 65 72 74 28 6e 43 65 6c 6c 3c 3d 6e 4d 61  ssert(nCell<=nMa
391b3 78 43 65 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 6e  xCell);..  if( n
391b4 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b  Cell<nMaxCell ){
391b5 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
391b6 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
391b7 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65  Node, pCell, nCe
391b8 6c 6c 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e  ll);.    writeIn
391b9 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t16(&pNode->zDat
391ba 61 5b 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a  a[2], nCell+1);.
391bb 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
391bc 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ty = 1;.  }..  r
391bd 65 74 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d  eturn (nCell==nM
391be 61 78 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  axCell);.}../*.*
391bf 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73  * If the node is
391c0 20 64 69 72 74 79 2c 20 77 72 69 74 65 20 69 74   dirty, write it
391c1 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
391c2 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
391c3 69 6e 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74  int.nodeWrite(Rt
391c4 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
391c5 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
391c6 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
391c7 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64  E_OK;.  if( pNod
391c8 65 2d 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20  e->isDirty ){.  
391c9 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
391ca 70 20 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69  p = pRtree->pWri
391cb 74 65 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20  teNode;.    if( 
391cc 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a  pNode->iNode ){.
391cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
391ce 6e 64 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70  nd_int64(p, 1, p
391cf 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
391d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
391d1 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
391d2 28 70 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20  (p, 1);.    }.  
391d3 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
391d4 6c 6f 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d  lob(p, 2, pNode-
391d5 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
391d6 69 4e 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54  iNodeSize, SQLIT
391d7 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
391d8 71 6c 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a  qlite3_step(p);.
391d9 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
391da 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ty = 0;.    rc =
391db 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
391dc 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  );.    if( pNode
391dd 2d 3e 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63  ->iNode==0 && rc
391de 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
391df 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64       pNode->iNod
391e0 65 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  e = sqlite3_last
391e1 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52  _insert_rowid(pR
391e2 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  tree->db);.     
391e3 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28   nodeHashInsert(
391e4 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
391e5 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
391e6 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
391e7 52 65 6c 65 61 73 65 20 61 20 72 65 66 65 72 65  Release a refere
391e8 6e 63 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49  nce to a node. I
391e9 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69  f the node is di
391ea 72 74 79 20 61 6e 64 20 74 68 65 20 72 65 66 65  rty and the refe
391eb 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64  rence.** count d
391ec 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68  rops to zero, th
391ed 65 20 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77  e node data is w
391ee 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
391ef 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
391f0 63 20 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73  c int.nodeReleas
391f1 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
391f2 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
391f3 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
391f4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
391f5 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73  pNode ){.    ass
391f6 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ert( pNode->nRef
391f7 3e 30 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  >0 );.    pNode-
391f8 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
391f9 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20   pNode->nRef==0 
391fa 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f  ){.      if( pNo
391fb 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
391fc 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e          pRtree->
391fd 69 44 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20  iDepth = -1;.   
391fe 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
391ff 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Node->pParent ){
39200 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
39201 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
39202 2c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  , pNode->pParent
39203 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
39204 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39205 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
39206 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
39207 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
39208 20 20 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48     }.      nodeH
39209 61 73 68 44 65 6c 65 74 65 28 70 52 74 72 65 65  ashDelete(pRtree
3920a 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  , pNode);.      
3920b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f  sqlite3_free(pNo
3920c 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
3920d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
3920e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
3920f 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
39210 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20  alue associated 
39211 77 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20  with cell iCell 
39212 6f 66 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65  of.** node pNode
39213 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20  . If pNode is a 
39214 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20  leaf node, this 
39215 69 73 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69  is a rowid. If i
39216 74 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72  t is.** an inter
39217 6e 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74  nal node, then t
39218 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  he 64-bit intege
39219 72 20 69 73 20 61 20 63 68 69 6c 64 20 70 61 67  r is a child pag
3921a 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
3921b 74 69 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52  tic i64 nodeGetR
3921c 6f 77 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70  owid(.  Rtree *p
3921d 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
3921e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
3921f 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73  nt iCell.){.  as
39220 73 65 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c  sert( iCell<NCEL
39221 4c 28 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65  L(pNode) );.  re
39222 74 75 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26  turn readInt64(&
39223 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
39224 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
39225 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a  erCell*iCell]);.
39226 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
39227 63 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72  coordinate iCoor
39228 64 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c  d from cell iCel
39229 6c 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l in node pNode.
3922a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3922b 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20  nodeGetCoord(.  
3922c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3922d 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3922e 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c  de, .  int iCell
3922f 2c 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a  ,.  int iCoord,.
39230 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43    RtreeCoord *pC
39231 6f 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 2f  oord           /
39232 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
39233 20 72 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b   result to */.){
39234 0a 20 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e  .  readCoord(&pN
39235 6f 64 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20  ode->zData[12 + 
39236 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
39237 72 43 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a  rCell*iCell + 4*
39238 69 43 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29  iCoord], pCoord)
39239 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72  ;.}../*.** Deser
3923a 69 61 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c  ialize cell iCel
3923b 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
3923c 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74   Populate the st
3923d 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a  ructure pointed.
3923e 2a 2a 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77  ** to by pCell w
3923f 69 74 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ith the results.
39240 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39241 6e 6f 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52  nodeGetCell(.  R
39242 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39243 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
39244 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c  e, .  int iCell,
39245 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
39246 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ell.){.  int ii;
39247 0a 20 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  .  pCell->iRowid
39248 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
39249 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
3924a 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d  Cell);.  for(ii=
3924b 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
3924c 69 6d 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  im*2; ii++){.   
3924d 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52   nodeGetCoord(pR
3924e 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
3924f 6c 6c 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e  ll, ii, &pCell->
39250 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
39251 0a 7d 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  .}.../* Forward 
39252 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
39253 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  the function tha
39254 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20  t does the work 
39255 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  of.** the virtua
39256 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
39257 43 72 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f  Create() and xCo
39258 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e  nnect() methods.
39259 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
3925a 74 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69  treeInit(.  sqli
3925b 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
3925c 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
3925d 63 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f  const*, sqlite3_
3925e 76 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a  vtab **, char **
3925f 2c 20 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a  , int.);../* .**
39260 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39261 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65  able module xCre
39262 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ate method..*/.s
39263 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43  tatic int rtreeC
39264 72 65 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  reate(.  sqlite3
39265 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
39266 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
39267 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
39268 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
39269 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
3926a 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
3926b 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
3926c 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
3926d 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
3926e 61 62 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d  ab, pzErr, 1);.}
3926f 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
39270 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
39271 75 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  ule xConnect met
39272 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
39273 6e 74 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28  nt rtreeConnect(
39274 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
39275 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20    void *pAux,.  
39276 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
39277 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
39278 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ,.  sqlite3_vtab
39279 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61   **ppVtab,.  cha
3927a 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72  r **pzErr.){.  r
3927b 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28  eturn rtreeInit(
3927c 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20  db, pAux, argc, 
3927d 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a  argv, ppVtab, pz
3927e 45 72 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Err, 0);.}../*.*
3927f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
39280 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
39281 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   count..*/.stati
39282 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 66 65  c void rtreeRefe
39283 72 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74  rence(Rtree *pRt
39284 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e  ree){.  pRtree->
39285 6e 42 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  nBusy++;.}../*.*
39286 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
39287 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
39288 20 63 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65   count. When the
39289 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
3928a 20 72 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f   reaches.** zero
3928b 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69   the structure i
3928c 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74  s deleted..*/.st
3928d 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 52  atic void rtreeR
3928e 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70 52  elease(Rtree *pR
3928f 74 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d  tree){.  pRtree-
39290 3e 6e 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20  >nBusy--;.  if( 
39291 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30  pRtree->nBusy==0
39292 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39293 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
39294 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20  >pReadNode);.   
39295 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
39296 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  e(pRtree->pWrite
39297 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Node);.    sqlit
39298 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
39299 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
3929a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
3929b 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
3929c 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  ReadRowid);.    
3929d 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
3929e 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
3929f 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
392a0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
392a1 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
392a2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
392a3 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
392a4 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20  pReadParent);.  
392a5 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
392a6 7a 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ze(pRtree->pWrit
392a7 65 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71  eParent);.    sq
392a8 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
392a9 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
392aa 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
392ab 65 33 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b  e3_free(pRtree);
392ac 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  .  }.}../* .** R
392ad 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
392ae 6c 65 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f  le module xDisco
392af 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
392b0 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
392b1 65 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69  eDisconnect(sqli
392b2 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
392b3 7b 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  {.  rtreeRelease
392b4 28 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 29  ((Rtree *)pVtab)
392b5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
392b6 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  E_OK;.}../* .** 
392b7 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
392b8 62 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74  ble module xDest
392b9 72 6f 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  roy method..*/.s
392ba 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44  tatic int rtreeD
392bb 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76  estroy(sqlite3_v
392bc 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52  tab *pVtab){.  R
392bd 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
392be 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20  Rtree *)pVtab;. 
392bf 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
392c0 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
392c1 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
392c2 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27  "DROP TABLE '%q'
392c3 2e 27 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20  .'%q_node';".   
392c4 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71   "DROP TABLE '%q
392c5 27 2e 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20  '.'%q_rowid';". 
392c6 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27     "DROP TABLE '
392c7 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b  %q'.'%q_parent';
392c8 22 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  ",.    pRtree->z
392c9 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
392ca 65 2c 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  e, .    pRtree->
392cb 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
392cc 6d 65 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  me,.    pRtree->
392cd 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
392ce 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a  me.  );.  if( !z
392cf 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63  Create ){.    rc
392d0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
392d1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
392d2 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
392d3 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65  pRtree->db, zCre
392d4 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ate, 0, 0, 0);. 
392d5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
392d6 7a 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20  zCreate);.  }.  
392d7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
392d8 4b 20 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65  K ){.    rtreeRe
392d9 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
392da 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
392db 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
392dc 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
392dd 6f 64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68  odule xOpen meth
392de 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
392df 74 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69  t rtreeOpen(sqli
392e0 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
392e1 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
392e2 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29  rsor **ppCursor)
392e3 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
392e4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72  ITE_NOMEM;.  Rtr
392e5 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a  eeCursor *pCsr;.
392e6 0a 20 20 70 43 73 72 20 3d 20 28 52 74 72 65 65  .  pCsr = (Rtree
392e7 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33  Cursor *)sqlite3
392e8 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
392e9 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20  treeCursor));.  
392ea 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20  if( pCsr ){.    
392eb 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
392ec 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73  sizeof(RtreeCurs
392ed 6f 72 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e  or));.    pCsr->
392ee 62 61 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54  base.pVtab = pVT
392ef 61 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ab;.    rc = SQL
392f0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70  ITE_OK;.  }.  *p
392f1 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74  pCursor = (sqlit
392f2 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
392f3 29 70 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e  )pCsr;..  return
392f4 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   rc;.}../* .** R
392f5 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
392f6 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73 65  le module xClose
392f7 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
392f8 69 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f 73  ic int rtreeClos
392f9 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e(sqlite3_vtab_c
392fa 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
392fb 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
392fc 52 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70 56  Rtree *)(cur->pV
392fd 74 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tab);.  int rc;.
392fe 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
392ff 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
39300 6f 72 20 2a 29 63 75 72 3b 0a 20 20 73 71 6c 69  or *)cur;.  sqli
39301 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61  te3_free(pCsr->a
39302 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 72  Constraint);.  r
39303 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
39304 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e  pRtree, pCsr->pN
39305 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
39306 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
39307 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
39308 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
39309 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f  table module xEo
3930a 66 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  f method..**.** 
3930b 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
3930c 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  if the cursor do
3930d 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
3930e 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
3930f 64 20 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e  d .** record (i.
39310 65 20 69 66 20 74 68 65 20 73 63 61 6e 20 68 61  e if the scan ha
39311 73 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72 20  s finished), or 
39312 7a 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a  zero otherwise..
39313 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
39314 72 65 65 45 6f 66 28 73 71 6c 69 74 65 33 5f 76  reeEof(sqlite3_v
39315 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 29  tab_cursor *cur)
39316 7b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  {.  RtreeCursor 
39317 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
39318 72 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 72 65  rsor *)cur;.  re
39319 74 75 72 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64  turn (pCsr->pNod
3931a 65 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  e==0);.}../* .**
3931b 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
3931c 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
3931d 20 74 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20   to a cell in a 
3931e 6e 6f 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a  non-leaf page..*
3931f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
39320 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65   the sub-tree he
39321 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c  aded by the cell
39322 20 69 73 20 66 69 6c 74 65 72 65 64 0a 2a 2a 20   is filtered.** 
39323 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68  (excluded) by th
39324 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e  e constraints in
39325 20 74 68 65 20 70 43 75 72 73 6f 72 2d 3e 61 43   the pCursor->aC
39326 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 0a 2a 2a 20  onstraint[] .** 
39327 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65 20  array, or false 
39328 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
39329 61 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72  atic int testRtr
3932a 65 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  eeCell(Rtree *pR
3932b 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
3932c 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
3932d 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
3932e 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62   int ii;.  int b
3932f 52 65 73 20 3d 20 30 3b 0a 0a 20 20 6e 6f 64 65  Res = 0;..  node
39330 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
39331 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
39332 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
39333 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
39334 3d 30 3b 20 62 52 65 73 3d 3d 30 20 26 26 20 69  =0; bRes==0 && i
39335 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
39336 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
39337 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
39338 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
39339 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
3933a 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 65  ];.    double ce
3933b 6c 6c 5f 6d 69 6e 20 3d 20 44 43 4f 4f 52 44 28  ll_min = DCOORD(
3933c 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e  cell.aCoord[(p->
3933d 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 5d 29 3b 0a  iCoord>>1)*2]);.
3933e 20 20 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f      double cell_
3933f 6d 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  max = DCOORD(cel
39340 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f  l.aCoord[(p->iCo
39341 6f 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a  ord>>1)*2+1]);..
39342 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70      assert(p->op
39343 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d  ==RTREE_LE || p-
39344 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c  >op==RTREE_LT ||
39345 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45   p->op==RTREE_GE
39346 20 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e   .        || p->
39347 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20  op==RTREE_GT || 
39348 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a  p->op==RTREE_EQ.
39349 20 20 20 20 29 3b 0a 0a 20 20 20 20 73 77 69 74      );..    swit
3934a 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
3934b 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 45     case RTREE_LE
3934c 3a 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a  : case RTREE_LT:
3934d 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75   bRes = p->rValu
3934e 65 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 62 72 65 61  e<cell_min; brea
3934f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  k;.      case RT
39350 52 45 45 5f 47 45 3a 20 63 61 73 65 20 52 54 52  REE_GE: case RTR
39351 45 45 5f 47 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_GT: bRes = p-
39352 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78  >rValue>cell_max
39353 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
39354 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 0a 20  ase RTREE_EQ: . 
39355 20 20 20 20 20 20 20 62 52 65 73 20 3d 20 28 70         bRes = (p
39356 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61  ->rValue>cell_ma
39357 78 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c 63  x || p->rValue<c
39358 65 6c 6c 5f 6d 69 6e 29 3b 0a 20 20 20 20 20 20  ell_min);.      
39359 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
3935a 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 62 52 65   }..  return bRe
3935b 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  s;.}../* .** Ret
3935c 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
3935d 63 65 6c 6c 20 74 68 61 74 20 63 75 72 73 6f 72  cell that cursor
3935e 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
3935f 6c 79 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  ly points to.** 
39360 77 6f 75 6c 64 20 62 65 20 66 69 6c 74 65 72 65  would be filtere
39361 64 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20  d (excluded) by 
39362 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  the constraints 
39363 69 6e 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 73  in the .** pCurs
39364 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
39365 5d 20 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73  ] array, or fals
39366 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  e otherwise..**.
39367 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
39368 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
39369 65 20 63 65 6c 6c 20 69 73 20 70 61 72 74 20 6f  e cell is part o
3936a 66 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 0a 2a  f a leaf node..*
3936b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73  /.static int tes
3936c 74 52 74 72 65 65 45 6e 74 72 79 28 52 74 72 65  tRtreeEntry(Rtre
3936d 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
3936e 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  Cursor *pCursor)
3936f 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65  {.  RtreeCell ce
39370 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  ll;.  int ii;.. 
39371 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
39372 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ree, pCursor->pN
39373 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43  ode, pCursor->iC
39374 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 66  ell, &cell);.  f
39375 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72  or(ii=0; ii<pCur
39376 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  sor->nConstraint
39377 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
39378 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20  eeConstraint *p 
39379 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e  = &pCursor->aCon
3937a 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
3937b 20 64 6f 75 62 6c 65 20 63 6f 6f 72 64 20 3d 20   double coord = 
3937c 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f  DCOORD(cell.aCoo
3937d 72 64 5b 70 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0a  rd[p->iCoord]);.
3937e 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
3937f 20 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52   assert(p->op==R
39380 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70  TREE_LE || p->op
39381 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d  ==RTREE_LT || p-
39382 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20  >op==RTREE_GE . 
39383 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d         || p->op=
39384 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e  =RTREE_GT || p->
39385 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20  op==RTREE_EQ.   
39386 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
39387 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  p->op ){.      c
39388 61 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 72 65  ase RTREE_LE: re
39389 73 20 3d 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72  s = (coord<=p->r
3938a 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20  Value); break;. 
3938b 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
3938c 4c 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LT: res = (coord
3938d 3c 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20 62 72  <p->rValue);  br
3938e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3938f 52 54 52 45 45 5f 47 45 3a 20 72 65 73 20 3d 20  RTREE_GE: res = 
39390 28 63 6f 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75  (coord>=p->rValu
39391 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  e); break;.     
39392 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
39393 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e  res = (coord>p->
39394 72 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b  rValue);  break;
39395 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
39396 45 5f 45 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_EQ: res = (coo
39397 72 64 3d 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20  rd==p->rValue); 
39398 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
39399 20 20 69 66 28 20 21 72 65 73 20 29 20 72 65 74    if( !res ) ret
3939a 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  urn 1;.  }..  re
3939b 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
3939c 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
3939d 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
3939e 20 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20   at a node that 
3939f 68 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65  heads a sub-tree
393a0 20 6f 66 0a 2a 2a 20 68 65 69 67 68 74 20 69 48   of.** height iH
393a1 65 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68  eight (if iHeigh
393a2 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e  t==0, then the n
393a3 6f 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20  ode is a leaf). 
393a4 44 65 73 63 65 6e 64 0a 2a 2a 20 74 6f 20 70 6f  Descend.** to po
393a5 69 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74 2d  int to the left-
393a6 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68 65  most cell of the
393a7 20 73 75 62 2d 74 72 65 65 20 74 68 61 74 20 6d   sub-tree that m
393a8 61 74 63 68 65 73 20 74 68 65 20 0a 2a 2a 20 63  atches the .** c
393a9 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72  onfigured constr
393aa 61 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  aints..*/.static
393ab 20 69 6e 74 20 64 65 73 63 65 6e 64 54 6f 43 65   int descendToCe
393ac 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
393ad 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43 75 72  ree, .  RtreeCur
393ae 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 0a 20  sor *pCursor, . 
393af 20 69 6e 74 20 69 48 65 69 67 68 74 2c 0a 20 20   int iHeight,.  
393b0 69 6e 74 20 2a 70 45 6f 66 20 20 20 20 20 20 20  int *pEof       
393b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
393b2 3a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  : Set to true if
393b3 20 63 61 6e 6e 6f 74 20 64 65 73 63 65 6e 64 20   cannot descend 
393b4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 6f  */.){.  int isEo
393b5 66 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  f;.  int rc;.  i
393b6 6e 74 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f  nt ii;.  RtreeNo
393b7 64 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 73 71  de *pChild;.  sq
393b8 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
393b9 69 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65  id;..  RtreeNode
393ba 20 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70   *pSavedNode = p
393bb 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0a 20  Cursor->pNode;. 
393bc 20 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c 20   int iSavedCell 
393bd 3d 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c  = pCursor->iCell
393be 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  ;..  assert( iHe
393bf 69 67 68 74 3e 3d 30 20 29 3b 0a 0a 20 20 69 66  ight>=0 );..  if
393c0 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a  ( iHeight==0 ){.
393c1 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74      isEof = test
393c2 52 74 72 65 65 45 6e 74 72 79 28 70 52 74 72 65  RtreeEntry(pRtre
393c3 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
393c4 65 6c 73 65 7b 0a 20 20 20 20 69 73 45 6f 66 20  else{.    isEof 
393c5 3d 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  = testRtreeCell(
393c6 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 29  pRtree, pCursor)
393c7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 45 6f  ;.  }.  if( isEo
393c8 66 20 7c 7c 20 69 48 65 69 67 68 74 3d 3d 30 20  f || iHeight==0 
393c9 29 7b 0a 20 20 20 20 2a 70 45 6f 66 20 3d 20 69  ){.    *pEof = i
393ca 73 45 6f 66 3b 0a 20 20 20 20 72 65 74 75 72 6e  sEof;.    return
393cb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
393cc 0a 20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  .  iRowid = node
393cd 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
393ce 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c   pCursor->pNode,
393cf 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29   pCursor->iCell)
393d0 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71  ;.  rc = nodeAcq
393d1 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f  uire(pRtree, iRo
393d2 77 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  wid, pCursor->pN
393d3 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  ode, &pChild);. 
393d4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
393d5 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
393d6 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 6e 6f 64 65   rc;.  }..  node
393d7 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
393d8 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 29 3b  pCursor->pNode);
393d9 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64  .  pCursor->pNod
393da 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 69 73  e = pChild;.  is
393db 45 6f 66 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  Eof = 1;.  for(i
393dc 69 3d 30 3b 20 69 73 45 6f 66 20 26 26 20 69 69  i=0; isEof && ii
393dd 3c 4e 43 45 4c 4c 28 70 43 68 69 6c 64 29 3b 20  <NCELL(pChild); 
393de 69 69 2b 2b 29 7b 0a 20 20 20 20 70 43 75 72 73  ii++){.    pCurs
393df 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 69 3b 0a  or->iCell = ii;.
393e0 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64      rc = descend
393e1 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  ToCell(pRtree, p
393e2 43 75 72 73 6f 72 2c 20 69 48 65 69 67 68 74 2d  Cursor, iHeight-
393e3 31 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  1, &isEof);.    
393e4 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
393e5 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
393e6 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
393e7 0a 20 20 69 66 28 20 69 73 45 6f 66 20 29 7b 0a  .  if( isEof ){.
393e8 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
393e9 73 6f 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69  sor->pNode==pChi
393ea 6c 64 20 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ld );.    nodeRe
393eb 66 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f  ference(pSavedNo
393ec 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 6c  de);.    nodeRel
393ed 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68  ease(pRtree, pCh
393ee 69 6c 64 29 3b 0a 20 20 20 20 70 43 75 72 73 6f  ild);.    pCurso
393ef 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76 65  r->pNode = pSave
393f0 64 4e 6f 64 65 3b 0a 20 20 20 20 70 43 75 72 73  dNode;.    pCurs
393f1 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53 61 76  or->iCell = iSav
393f2 65 64 43 65 6c 6c 3b 0a 20 20 7d 0a 0a 20 20 2a  edCell;.  }..  *
393f3 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20  pEof = isEof;.  
393f4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
393f5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f  ;.}../*.** One o
393f6 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 6e  f the cells in n
393f7 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 67 75 61  ode pNode is gua
393f8 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20  ranteed to have 
393f9 61 20 36 34 2d 62 69 74 20 0a 2a 2a 20 69 6e 74  a 64-bit .** int
393fa 65 67 65 72 20 76 61 6c 75 65 20 65 71 75 61 6c  eger value equal
393fb 20 74 6f 20 69 52 6f 77 69 64 2e 20 52 65 74 75   to iRowid. Retu
393fc 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
393fd 74 68 69 73 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74  this cell..*/.st
393fe 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 52 6f 77  atic int nodeRow
393ff 69 64 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  idIndex(Rtree *p
39400 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39401 20 2a 70 4e 6f 64 65 2c 20 69 36 34 20 69 52 6f   *pNode, i64 iRo
39402 77 69 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  wid){.  int ii;.
39403 20 20 66 6f 72 28 69 69 3d 30 3b 20 6e 6f 64 65    for(ii=0; node
39404 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
39405 20 70 4e 6f 64 65 2c 20 69 69 29 21 3d 69 52 6f   pNode, ii)!=iRo
39406 77 69 64 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  wid; ii++){.    
39407 61 73 73 65 72 74 28 20 69 69 3c 28 4e 43 45 4c  assert( ii<(NCEL
39408 4c 28 70 4e 6f 64 65 29 2d 31 29 20 29 3b 0a 20  L(pNode)-1) );. 
39409 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 69 3b 0a   }.  return ii;.
3940a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
3940b 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
3940c 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67   cell containing
3940d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e 6f   a pointer to no
3940e 64 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20 69  de pNode.** in i
3940f 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70 4e  ts parent. If pN
39410 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ode is the root 
39411 6e 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31 2e  node, return -1.
39412 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
39413 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 52  odeParentIndex(R
39414 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
39415 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b  reeNode *pNode){
39416 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50  .  RtreeNode *pP
39417 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
39418 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50  Parent;.  if( pP
39419 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  arent ){.    ret
3941a 75 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  urn nodeRowidInd
3941b 65 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ex(pRtree, pPare
3941c 6e 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  nt, pNode->iNode
3941d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3941e 2d 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  -1;.}../* .** Rt
3941f 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
39420 65 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d  e module xNext m
39421 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
39422 20 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73   int rtreeNext(s
39423 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
39424 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29  or *pVtabCursor)
39425 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
39426 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56  e = (Rtree *)(pV
39427 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
39428 29 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72  );.  RtreeCursor
39429 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43   *pCsr = (RtreeC
3942a 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72  ursor *)pVtabCur
3942b 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  sor;.  int rc = 
3942c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
3942d 28 20 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67  ( pCsr->iStrateg
3942e 79 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  y==1 ){.    /* T
3942f 68 69 73 20 22 73 63 61 6e 22 20 69 73 20 61 20  his "scan" is a 
39430 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79  direct lookup by
39431 20 72 6f 77 69 64 2e 20 54 68 65 72 65 20 69 73   rowid. There is
39432 20 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 2e 20   no next entry. 
39433 2a 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61  */.    nodeRelea
39434 73 65 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d  se(pRtree, pCsr-
39435 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 43 73  >pNode);.    pCs
39436 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->pNode = 0;.  
39437 7d 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 43  }..  else if( pC
39438 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20  sr->pNode ){.   
39439 20 2f 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   /* Move to the 
3943a 6e 65 78 74 20 65 6e 74 72 79 20 74 68 61 74 20  next entry that 
3943b 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6e 66  matches the conf
3943c 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
3943d 74 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ts. */.    int i
3943e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  Height = 0;.    
3943f 77 68 69 6c 65 28 20 70 43 73 72 2d 3e 70 4e 6f  while( pCsr->pNo
39440 64 65 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65  de ){.      Rtre
39441 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 20 3d 20 70  eNode *pNode = p
39442 43 73 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 20 20  Csr->pNode;.    
39443 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
39444 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
39445 20 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c    for(pCsr->iCel
39446 6c 2b 2b 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c  l++; pCsr->iCell
39447 3c 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43  <nCell; pCsr->iC
39448 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ell++){.        
39449 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 20 20 20  int isEof;.     
3944a 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54     rc = descendT
3944b 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  oCell(pRtree, pC
3944c 73 72 2c 20 69 48 65 69 67 68 74 2c 20 26 69 73  sr, iHeight, &is
3944d 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Eof);.        if
3944e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3944f 7c 7c 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20  || !isEof ){.   
39450 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
39451 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39452 20 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e    }.      pCsr->
39453 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70  pNode = pNode->p
39454 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 70 43  Parent;.      pC
39455 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
39456 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
39457 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ee, pNode);.    
39458 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
39459 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
3945a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
3945b 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
3945c 20 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b 3b        iHeight++;
3945d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
3945e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a  turn rc;.}../* .
3945f 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
39460 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 52   table module xR
39461 6f 77 69 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  owid method..*/.
39462 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
39463 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74  Rowid(sqlite3_vt
39464 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
39465 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69  Cursor, sqlite_i
39466 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20  nt64 *pRowid){. 
39467 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
39468 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43   (Rtree *)pVtabC
39469 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
3946a 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
3946b 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
3946c 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
3946d 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e  .  assert(pCsr->
3946e 70 4e 6f 64 65 29 3b 0a 20 20 2a 70 52 6f 77 69  pNode);.  *pRowi
3946f 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
39470 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  (pRtree, pCsr->p
39471 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c  Node, pCsr->iCel
39472 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  l);..  return SQ
39473 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
39474 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
39475 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43   table module xC
39476 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f  olumn method..*/
39477 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39478 65 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  eColumn(sqlite3_
39479 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
3947a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
3947b 74 20 2a 63 74 78 2c 20 69 6e 74 20 69 29 7b 0a  t *ctx, int i){.
3947c 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20    Rtree *pRtree 
3947d 3d 20 28 52 74 72 65 65 20 2a 29 63 75 72 2d 3e  = (Rtree *)cur->
3947e 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
3947f 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
39480 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b  reeCursor *)cur;
39481 0a 0a 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a  ..  if( i==0 ){.
39482 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d      i64 iRowid =
39483 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
39484 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
39485 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  e, pCsr->iCell);
39486 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
39487 75 6c 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69  ult_int64(ctx, i
39488 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
39489 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
3948a 63 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  c;.    nodeGetCo
3948b 6f 72 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  ord(pRtree, pCsr
3948c 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
3948d 43 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b 0a  Cell, i-1, &c);.
3948e 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
3948f 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
39490 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
39491 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
39492 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 74  result_double(ct
39493 78 2c 20 63 2e 66 29 3b 0a 20 20 20 20 7d 65 6c  x, c.f);.    }el
39494 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
39495 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64  ( pRtree->eCoord
39496 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
39497 44 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  D_INT32 );.     
39498 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
39499 69 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a 20  int(ctx, c.i);. 
3949a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
3949b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3949c 0a 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64 65  ./* .** Use node
3949d 41 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74  Acquire() to obt
3949e 61 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  ain the leaf nod
3949f 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
394a0 20 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a 2a   record with .**
394a1 20 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49   rowid iRowid. I
394a2 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
394a3 74 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69  t *ppLeaf to poi
394a4 6e 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61  nt to the node a
394a5 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  nd.** return SQL
394a6 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65  ITE_OK. If there
394a7 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f   is no such reco
394a8 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  rd in the table,
394a9 20 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66 20   set.** *ppLeaf 
394aa 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20  to 0 and return 
394ab 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e  SQLITE_OK. If an
394ac 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73   error occurs, s
394ad 65 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74 6f  et *ppLeaf.** to
394ae 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
394af 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
394b0 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
394b1 20 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f 64   int findLeafNod
394b2 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
394b3 20 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74 72   i64 iRowid, Rtr
394b4 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66 29  eeNode **ppLeaf)
394b5 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a 70  {.  int rc;.  *p
394b6 70 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71 6c  pLeaf = 0;.  sql
394b7 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
394b8 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
394b9 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
394ba 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74    if( sqlite3_st
394bb 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
394bc 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52  Rowid)==SQLITE_R
394bd 4f 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4e  OW ){.    i64 iN
394be 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ode = sqlite3_co
394bf 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65  lumn_int64(pRtre
394c0 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 30  e->pReadRowid, 0
394c1 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  );.    rc = node
394c2 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20  Acquire(pRtree, 
394c3 69 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61 66  iNode, 0, ppLeaf
394c4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
394c5 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
394c6 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  adRowid);.  }els
394c7 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
394c8 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
394c9 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
394ca 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
394cb 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  }.../* .** Rtree
394cc 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
394cd 6f 64 75 6c 65 20 78 46 69 6c 74 65 72 20 6d 65  odule xFilter me
394ce 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
394cf 69 6e 74 20 72 74 72 65 65 46 69 6c 74 65 72 28  int rtreeFilter(
394d0 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
394d1 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
394d2 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 64 78 4e  sor, .  int idxN
394d3 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  um, const char *
394d4 69 64 78 53 74 72 2c 0a 20 20 69 6e 74 20 61 72  idxStr,.  int ar
394d5 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
394d6 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 52 74  e **argv.){.  Rt
394d7 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
394d8 74 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73  tree *)pVtabCurs
394d9 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72  or->pVtab;.  Rtr
394da 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
394db 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
394dc 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20  pVtabCursor;..  
394dd 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
394de 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a   = 0;.  int ii;.
394df 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
394e0 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65  E_OK;..  rtreeRe
394e1 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b  ference(pRtree);
394e2 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
394e3 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  (pCsr->aConstrai
394e4 6e 74 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 6f  nt);.  pCsr->aCo
394e5 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b 0a 20 20  nstraint = 0;.  
394e6 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79 20  pCsr->iStrategy 
394e7 3d 20 69 64 78 4e 75 6d 3b 0a 0a 20 20 69 66 28  = idxNum;..  if(
394e8 20 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0a 20 20   idxNum==1 ){.  
394e9 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
394ea 65 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  e - lookup by ro
394eb 77 69 64 2e 20 2a 2f 0a 20 20 20 20 52 74 72 65  wid. */.    Rtre
394ec 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20  eNode *pLeaf;   
394ed 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e 20       /* Leaf on 
394ee 77 68 69 63 68 20 74 68 65 20 72 65 71 75 69 72  which the requir
394ef 65 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73 20  ed cell resides 
394f0 2a 2f 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69  */.    i64 iRowi
394f1 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
394f2 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
394f3 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
394f4 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
394f5 69 52 6f 77 69 64 2c 20 26 70 4c 65 61 66 29 3b  iRowid, &pLeaf);
394f6 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65  .    pCsr->pNode
394f7 20 3d 20 70 4c 65 61 66 3b 20 0a 20 20 20 20 69   = pLeaf; .    i
394f8 66 28 20 70 4c 65 61 66 20 26 26 20 72 63 3d 3d  f( pLeaf && rc==
394f9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
394fa 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20 3d     pCsr->iCell =
394fb 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
394fc 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69  pRtree, pLeaf, i
394fd 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
394fe 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f  }else{.    /* No
394ff 72 6d 61 6c 20 63 61 73 65 20 2d 20 72 2d 74 72  rmal case - r-tr
39500 65 65 20 73 63 61 6e 2e 20 53 65 74 20 75 70 20  ee scan. Set up 
39501 74 68 65 20 52 74 72 65 65 43 75 72 73 6f 72 2e  the RtreeCursor.
39502 61 43 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61  aConstraint arra
39503 79 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  y .    ** with t
39504 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f  he configured co
39505 6e 73 74 72 61 69 6e 74 73 2e 20 0a 20 20 20 20  nstraints. .    
39506 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  */.    if( argc>
39507 30 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  0 ){.      pCsr-
39508 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 73  >aConstraint = s
39509 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
3950a 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74 72  zeof(RtreeConstr
3950b 61 69 6e 74 29 2a 61 72 67 63 29 3b 0a 20 20 20  aint)*argc);.   
3950c 20 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72     pCsr->nConstr
3950d 61 69 6e 74 20 3d 20 61 72 67 63 3b 0a 20 20 20  aint = argc;.   
3950e 20 20 20 69 66 28 20 21 70 43 73 72 2d 3e 61 43     if( !pCsr->aC
3950f 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20  onstraint ){.   
39510 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
39511 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
39512 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
39513 65 72 74 28 20 28 69 64 78 53 74 72 3d 3d 30 20  ert( (idxStr==0 
39514 26 26 20 61 72 67 63 3d 3d 30 29 20 7c 7c 20 73  && argc==0) || s
39515 74 72 6c 65 6e 28 69 64 78 53 74 72 29 3d 3d 61  trlen(idxStr)==a
39516 72 67 63 2a 32 20 29 3b 0a 20 20 20 20 20 20 20  rgc*2 );.       
39517 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72   for(ii=0; ii<ar
39518 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  gc; ii++){.     
39519 20 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72       RtreeConstr
3951a 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d  aint *p = &pCsr-
3951b 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
3951c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f  ;.          p->o
3951d 70 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d  p = idxStr[ii*2]
3951e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  ;.          p->i
3951f 43 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69  Coord = idxStr[i
39520 69 2a 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20  i*2+1]-'a';.    
39521 20 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20        p->rValue 
39522 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39523 64 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29  double(argv[ii])
39524 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39525 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
39526 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39527 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
39528 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  ->pNode = 0;.   
39529 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
3952a 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30  ire(pRtree, 1, 0
3952b 2c 20 26 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  , &pRoot);.    }
3952c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3952d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3952e 69 6e 74 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  int isEof = 1;. 
3952f 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d       int nCell =
39530 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3b 0a 20   NCELL(pRoot);. 
39531 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
39532 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 20 20 20 20   = pRoot;.      
39533 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d  for(pCsr->iCell=
39534 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
39535 20 26 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c   && pCsr->iCell<
39536 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65  nCell; pCsr->iCe
39537 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  ll++){.        a
39538 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
39539 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
3953a 20 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e       rc = descen
3953b 64 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  dToCell(pRtree, 
3953c 70 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44  pCsr, pRtree->iD
3953d 65 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0a 20  epth, &isEof);. 
3953e 20 20 20 20 20 20 20 69 66 28 20 21 69 73 45 6f         if( !isEo
3953f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  f ){.          b
39540 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
39541 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39542 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39543 26 26 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20  && isEof ){.    
39544 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
39545 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29  ->pNode==pRoot )
39546 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65  ;.        nodeRe
39547 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
39548 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  oot);.        pC
39549 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20  sr->pNode = 0;. 
3954a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
3954b 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
3954c 4f 4b 20 7c 7c 20 21 70 43 73 72 2d 3e 70 4e 6f  OK || !pCsr->pNo
3954d 64 65 20 7c 7c 20 70 43 73 72 2d 3e 69 43 65 6c  de || pCsr->iCel
3954e 6c 3c 4e 43 45 4c 4c 28 70 43 73 72 2d 3e 70 4e  l<NCELL(pCsr->pN
3954f 6f 64 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20  ode) );.    }.  
39550 7d 0a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73  }..  rtreeReleas
39551 65 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74  e(pRtree);.  ret
39552 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
39553 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39554 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 42 65 73  able module xBes
39555 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 2e 20 54  tIndex method. T
39556 68 65 72 65 20 61 72 65 20 74 68 72 65 65 0a 2a  here are three.*
39557 2a 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74 72  * table scan str
39558 61 74 65 67 69 65 73 20 74 6f 20 63 68 6f 6f 73  ategies to choos
39559 65 20 66 72 6f 6d 20 28 69 6e 20 6f 72 64 65 72  e from (in order
3955a 20 66 72 6f 6d 20 6d 6f 73 74 20 74 6f 20 0a 2a   from most to .*
3955b 2a 20 6c 65 61 73 74 20 64 65 73 69 72 61 62 6c  * least desirabl
3955c 65 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 64 78 4e  e):.**.**   idxN
3955d 75 6d 20 20 20 20 20 69 64 78 53 74 72 20 20 20  um     idxStr   
3955e 20 20 20 20 20 53 74 72 61 74 65 67 79 0a 2a 2a       Strategy.**
3955f 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39560 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39561 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39562 2d 2d 2d 0a 2a 2a 20 20 20 20 20 31 20 20 20 20  ---.**     1    
39563 20 20 20 20 55 6e 75 73 65 64 20 20 20 20 20 20      Unused      
39564 20 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20    Direct lookup 
39565 62 79 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  by rowid..**    
39566 20 32 20 20 20 20 20 20 20 20 53 65 65 20 62 65   2        See be
39567 6c 6f 77 20 20 20 20 20 52 2d 74 72 65 65 20 71  low     R-tree q
39568 75 65 72 79 2e 0a 2a 2a 20 20 20 20 20 33 20 20  uery..**     3  
39569 20 20 20 20 20 20 55 6e 75 73 65 64 20 20 20 20        Unused    
3956a 20 20 20 20 46 75 6c 6c 20 74 61 62 6c 65 20 73      Full table s
3956b 63 61 6e 2e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  can..**   ------
3956c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3956d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3956e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
3956f 20 49 66 20 73 74 72 61 74 65 67 79 20 31 20 6f   If strategy 1 o
39570 72 20 33 20 69 73 20 75 73 65 64 2c 20 74 68 65  r 3 is used, the
39571 6e 20 69 64 78 53 74 72 20 69 73 20 6e 6f 74 20  n idxStr is not 
39572 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66 20 73  meaningful. If s
39573 74 72 61 74 65 67 79 0a 2a 2a 20 32 20 69 73 20  trategy.** 2 is 
39574 75 73 65 64 2c 20 69 64 78 53 74 72 20 69 73 20  used, idxStr is 
39575 66 6f 72 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e  formatted to con
39576 74 61 69 6e 20 32 20 62 79 74 65 73 20 66 6f 72  tain 2 bytes for
39577 20 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73 74 72   each .** constr
39578 61 69 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66  aint used. The f
39579 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f  irst two bytes o
3957a 66 20 69 64 78 53 74 72 20 63 6f 72 72 65 73 70  f idxStr corresp
3957b 6f 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63  ond to .** the c
3957c 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c  onstraint in sql
3957d 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e  ite3_index_info.
3957e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
3957f 5b 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72 67 76  [] with.** (argv
39580 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0a 2a  Index==1) etc..*
39581 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f  *.** The first o
39582 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62  f each pair of b
39583 79 74 65 73 20 69 6e 20 69 64 78 53 74 72 20 69  ytes in idxStr i
39584 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f  dentifies the co
39585 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70 65 72  nstraint.** oper
39586 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ator as follows:
39587 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 6f  .**.**   Operato
39588 72 20 20 20 20 42 79 74 65 20 56 61 6c 75 65 0a  r    Byte Value.
39589 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
3958a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
3958b 20 20 20 20 3d 20 20 20 20 20 20 20 20 30 78 34      =        0x4
3958c 31 20 28 27 41 27 29 0a 2a 2a 20 20 20 20 20 3c  1 ('A').**     <
3958d 3d 20 20 20 20 20 20 20 20 30 78 34 32 20 28 27  =        0x42 ('
3958e 42 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20 20 20  B').**      <   
3958f 20 20 20 20 20 30 78 34 33 20 28 27 43 27 29 0a       0x43 ('C').
39590 2a 2a 20 20 20 20 20 3e 3d 20 20 20 20 20 20 20  **     >=       
39591 20 30 78 34 34 20 28 27 44 27 29 0a 2a 2a 20 20   0x44 ('D').**  
39592 20 20 20 20 3e 20 20 20 20 20 20 20 20 30 78 34      >        0x4
39593 35 20 28 27 45 27 29 0a 2a 2a 20 20 20 2d 2d 2d  5 ('E').**   ---
39594 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39595 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ---.**.** The se
39596 63 6f 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69  cond of each pai
39597 72 20 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74  r of bytes ident
39598 69 66 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69  ifies the coordi
39599 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74  nate column.** t
3959a 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73  o which the cons
3959b 74 72 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20  traint applies. 
3959c 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f  The leftmost coo
3959d 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a  rdinate column.*
3959e 2a 20 69 73 20 27 61 27 2c 20 74 68 65 20 73 65  * is 'a', the se
3959f 63 6f 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65  cond from the le
395a0 66 74 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73  ft 'b' etc..*/.s
395a1 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42  tatic int rtreeB
395a2 65 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33  estIndex(sqlite3
395a3 5f 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69  _vtab *tab, sqli
395a4 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
395a5 70 49 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74  pIdxInfo){.  int
395a6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
395a7 0a 20 20 69 6e 74 20 69 69 2c 20 63 43 6f 6c 3b  .  int ii, cCol;
395a8 0a 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ..  int iIdx = 0
395a9 3b 0a 20 20 63 68 61 72 20 7a 49 64 78 53 74 72  ;.  char zIdxStr
395aa 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
395ab 53 49 4f 4e 53 2a 38 2b 31 5d 3b 0a 20 20 6d 65  SIONS*8+1];.  me
395ac 6d 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c  mset(zIdxStr, 0,
395ad 20 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29   sizeof(zIdxStr)
395ae 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49  );..  assert( pI
395af 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d 3d  dxInfo->idxStr==
395b0 30 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  0 );.  for(ii=0;
395b1 20 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43   ii<pIdxInfo->nC
395b2 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
395b3 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71 6c  {.    struct sql
395b4 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
395b5 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78  raint *p = &pIdx
395b6 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
395b7 74 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  t[ii];..    if( 
395b8 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
395b9 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d  iColumn==0 && p-
395ba 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  >op==SQLITE_INDE
395bb 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
395bc 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ){.      /* We h
395bd 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79 20  ave an equality 
395be 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68  constraint on th
395bf 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74 72  e rowid. Use str
395c0 61 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20 20 20  ategy 1. */.    
395c1 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
395c2 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69 69 3b  for(jj=0; jj<ii;
395c3 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
395c4 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
395c5 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 61  raintUsage[jj].a
395c6 72 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20  rgvIndex = 0;.  
395c7 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
395c8 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
395c9 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b 0a 20  [jj].omit = 0;. 
395ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 64       }.      pId
395cb 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
395cc 31 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  1;.      pIdxInf
395cd 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
395ce 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65  age[ii].argvInde
395cf 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64  x = 1;.      pId
395d0 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
395d1 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74  ntUsage[jj].omit
395d2 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 1;..      /* 
395d3 54 68 69 73 20 73 74 72 61 74 65 67 79 20 69 6e  This strategy in
395d4 76 6f 6c 76 65 73 20 61 20 74 77 6f 20 72 6f 77  volves a two row
395d5 69 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e  id lookups on an
395d6 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
395d7 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  es.      ** and 
395d8 74 68 65 6e 20 61 20 6c 69 6e 65 61 72 20 73 65  then a linear se
395d9 61 72 63 68 20 6f 66 20 61 6e 20 52 2d 54 72 65  arch of an R-Tre
395da 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 73 68 6f  e node. This sho
395db 75 6c 64 20 62 65 20 0a 20 20 20 20 20 20 2a 2a  uld be .      **
395dc 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f   considered almo
395dd 73 74 20 61 73 20 71 75 69 63 6b 20 61 73 20 61  st as quick as a
395de 20 64 69 72 65 63 74 20 72 6f 77 69 64 20 6c 6f   direct rowid lo
395df 6f 6b 75 70 20 28 66 6f 72 20 77 68 69 63 68 20  okup (for which 
395e0 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
395e1 20 75 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61   uses an interna
395e2 6c 20 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0a  l cost of 0.0)..
395e3 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20        */ .      
395e4 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
395e5 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0a  tedCost = 10.0;.
395e6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
395e7 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20  ITE_OK;.    }.. 
395e8 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65     if( p->usable
395e9 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3e 30   && p->iColumn>0
395ea 20 29 7b 0a 20 20 20 20 20 20 75 38 20 6f 70 20   ){.      u8 op 
395eb 3d 20 30 3b 0a 20 20 20 20 20 20 73 77 69 74 63  = 0;.      switc
395ec 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
395ed 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
395ee 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
395ef 5f 45 51 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f  _EQ: op = RTREE_
395f0 45 51 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  EQ; break;.     
395f1 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
395f2 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
395f3 47 54 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47  GT: op = RTREE_G
395f4 54 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  T; break;.      
395f5 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
395f6 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
395f7 45 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45  E: op = RTREE_LE
395f8 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
395f9 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
395fa 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
395fb 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b  : op = RTREE_LT;
395fc 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
395fd 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  case SQLITE_INDE
395fe 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 3a  X_CONSTRAINT_GE:
395ff 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b 20   op = RTREE_GE; 
39600 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
39601 20 20 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20       if( op ){. 
39602 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73         /* Make s
39603 75 72 65 20 74 68 69 73 20 70 61 72 74 69 63 75  ure this particu
39604 6c 61 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 68  lar constraint h
39605 61 73 20 6e 6f 74 20 62 65 65 6e 20 75 73 65 64  as not been used
39606 20 62 65 66 6f 72 65 2e 0a 20 20 20 20 20 20 20   before..       
39607 20 2a 2a 20 49 66 20 69 74 20 68 61 73 20 62 65   ** If it has be
39608 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2c 20  en used before, 
39609 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 20 20 20  ignore it..     
3960a 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
3960b 20 41 20 3c 3d 20 6f 72 20 3c 20 63 61 6e 20 62   A <= or < can b
3960c 65 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20  e used if there 
3960d 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 20 6f 72  is a prior >= or
3960e 20 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41   >..        ** A
3960f 20 3e 3d 20 6f 72 20 3e 20 63 61 6e 20 62 65 20   >= or > can be 
39610 75 73 65 64 20 69 66 20 74 68 65 72 65 20 69 73  used if there is
39611 20 61 20 70 72 69 6f 72 20 3c 20 6f 72 20 3c 3d   a prior < or <=
39612 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c  ..        ** A <
39613 3d 20 6f 72 20 3c 20 69 73 20 64 69 73 71 75 61  = or < is disqua
39614 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
39615 69 73 20 61 20 70 72 69 6f 72 20 3c 3d 2c 20 3c  is a prior <=, <
39616 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
39617 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 69 73   ** A >= or > is
39618 20 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66   disqualified if
39619 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
3961a 72 20 3e 3d 2c 20 3e 2c 20 6f 72 20 3d 3d 2e 0a  r >=, >, or ==..
3961b 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3d 3d 20          ** A == 
3961c 69 73 20 64 69 73 71 75 61 6c 69 66 65 64 20 69  is disqualifed i
3961d 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 70  f there is any p
3961e 72 69 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 2e  rior constraint.
3961f 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
39620 20 20 20 20 69 6e 74 20 6a 2c 20 6f 70 6d 73 6b      int j, opmsk
39621 3b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63  ;.        static
39622 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
39623 63 68 61 72 20 63 6f 6d 70 61 74 69 62 6c 65 5b  char compatible[
39624 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 31 2c 20 31  ] = { 0, 0, 1, 1
39625 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 20 20  , 2, 2 };.      
39626 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74    assert( compat
39627 69 62 6c 65 5b 52 54 52 45 45 5f 45 51 20 26 20  ible[RTREE_EQ & 
39628 37 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  7]==0 );.       
39629 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69   assert( compati
3962a 62 6c 65 5b 52 54 52 45 45 5f 4c 54 20 26 20 37  ble[RTREE_LT & 7
3962b 5d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ]==1 );.        
3962c 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62  assert( compatib
3962d 6c 65 5b 52 54 52 45 45 5f 4c 45 20 26 20 37 5d  le[RTREE_LE & 7]
3962e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
3962f 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c  ssert( compatibl
39630 65 5b 52 54 52 45 45 5f 47 54 20 26 20 37 5d 3d  e[RTREE_GT & 7]=
39631 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =2 );.        as
39632 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65  sert( compatible
39633 5b 52 54 52 45 45 5f 47 45 20 26 20 37 5d 3d 3d  [RTREE_GE & 7]==
39634 32 20 29 3b 0a 20 20 20 20 20 20 20 20 63 43 6f  2 );.        cCo
39635 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  l = p->iColumn -
39636 20 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20   1 + 'a';.      
39637 20 20 6f 70 6d 73 6b 20 3d 20 63 6f 6d 70 61 74    opmsk = compat
39638 69 62 6c 65 5b 6f 70 20 26 20 37 5d 3b 0a 20 20  ible[op & 7];.  
39639 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
3963a 3c 69 49 64 78 3b 20 6a 2b 3d 32 29 7b 0a 20 20  <iIdx; j+=2){.  
3963b 20 20 20 20 20 20 20 20 69 66 28 20 7a 49 64 78          if( zIdx
3963c 53 74 72 5b 6a 2b 31 5d 3d 3d 63 43 6f 6c 20 26  Str[j+1]==cCol &
3963d 26 20 28 63 6f 6d 70 61 74 69 62 6c 65 5b 7a 49  & (compatible[zI
3963e 64 78 53 74 72 5b 6a 5d 20 26 20 37 5d 20 26 20  dxStr[j] & 7] & 
3963f 6f 70 6d 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20  opmsk)!=0 ){.   
39640 20 20 20 20 20 20 20 20 20 6f 70 20 3d 20 30 3b           op = 0;
39641 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
39642 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
39643 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
39644 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 20 29  }.      if( op )
39645 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
39646 28 20 69 49 64 78 3c 73 69 7a 65 6f 66 28 7a 49  ( iIdx<sizeof(zI
39647 64 78 53 74 72 29 2d 31 20 29 3b 0a 20 20 20 20  dxStr)-1 );.    
39648 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78      zIdxStr[iIdx
39649 2b 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  ++] = op;.      
3964a 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
3964b 5d 20 3d 20 63 43 6f 6c 3b 0a 20 20 20 20 20 20  ] = cCol;.      
3964c 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
3964d 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
3964e 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49  .argvIndex = (iI
3964f 64 78 2f 32 29 3b 0a 20 20 20 20 20 20 20 20 70  dx/2);.        p
39650 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39651 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d  aintUsage[ii].om
39652 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  it = 1;.      }.
39653 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 49 64      }.  }..  pId
39654 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
39655 32 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  2;.  pIdxInfo->n
39656 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
39657 3d 20 31 3b 0a 20 20 69 66 28 20 69 49 64 78 3e  = 1;.  if( iIdx>
39658 30 20 26 26 20 30 3d 3d 28 70 49 64 78 49 6e 66  0 && 0==(pIdxInf
39659 6f 2d 3e 69 64 78 53 74 72 20 3d 20 73 71 6c 69  o->idxStr = sqli
3965a 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
3965b 2c 20 7a 49 64 78 53 74 72 29 29 20 29 7b 0a 20  , zIdxStr)) ){. 
3965c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3965d 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
3965e 73 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b  sert( iIdx>=0 );
3965f 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
39660 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30  imatedCost = (20
39661 30 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c  00000.0 / (doubl
39662 65 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0a 20  e)(iIdx + 1));. 
39663 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
39664 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
39665 4e 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 76 6f  N-dimensional vo
39666 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 65 6c 6c  lumn of the cell
39667 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 2e 0a 2a   stored in *p..*
39668 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
39669 65 6c 6c 41 72 65 61 28 52 74 72 65 65 20 2a 70  ellArea(Rtree *p
3966a 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
3966b 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72   *p){.  float ar
3966c 65 61 20 3d 20 31 2e 30 3b 0a 20 20 69 6e 74 20  ea = 1.0;.  int 
3966d 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
3966e 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
3966f 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
39670 20 61 72 65 61 20 3d 20 61 72 65 61 20 2a 20 28   area = area * (
39671 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39672 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
39673 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
39674 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
39675 72 65 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rea;.}../*.** Re
39676 74 75 72 6e 20 74 68 65 20 6d 61 72 67 69 6e 20  turn the margin 
39677 6c 65 6e 67 74 68 20 6f 66 20 63 65 6c 6c 20 70  length of cell p
39678 2e 20 54 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e  . The margin len
39679 67 74 68 20 69 73 20 74 68 65 20 73 75 6d 0a 2a  gth is the sum.*
3967a 2a 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 73  * of the objects
3967b 20 73 69 7a 65 20 69 6e 20 65 61 63 68 20 64 69   size in each di
3967c 6d 65 6e 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  mension..*/.stat
3967d 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72  ic float cellMar
3967e 67 69 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65  gin(Rtree *pRtre
3967f 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29  e, RtreeCell *p)
39680 7b 0a 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e  {.  float margin
39681 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 69 69   = 0.0;.  int ii
39682 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39683 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39684 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 6d  ); ii+=2){.    m
39685 61 72 67 69 6e 20 2b 3d 20 28 44 43 4f 4f 52 44  argin += (DCOORD
39686 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  (p->aCoord[ii+1]
39687 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43  ) - DCOORD(p->aC
39688 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a  oord[ii]));.  }.
39689 20 20 72 65 74 75 72 6e 20 6d 61 72 67 69 6e 3b    return margin;
3968a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 6f 72 65 20  .}../*.** Store 
3968b 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c  the union of cel
3968c 6c 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20  ls p1 and p2 in 
3968d 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  p1..*/.static vo
3968e 69 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72  id cellUnion(Rtr
3968f 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
39690 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
39691 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
39692 20 69 69 3b 0a 20 20 69 66 28 20 70 52 74 72 65   ii;.  if( pRtre
39693 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
39694 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
39695 32 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d  2 ){.    for(ii=
39696 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
39697 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
39698 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64        p1->aCoord
39699 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e 28 70 31 2d  [ii].f = MIN(p1-
3969a 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 2c 20 70  >aCoord[ii].f, p
3969b 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 29  2->aCoord[ii].f)
3969c 3b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f  ;.      p1->aCoo
3969d 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 4d 41 58  rd[ii+1].f = MAX
3969e 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  (p1->aCoord[ii+1
3969f 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
396a0 69 69 2b 31 5d 2e 66 29 3b 0a 20 20 20 20 7d 0a  ii+1].f);.    }.
396a1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
396a2 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
396a3 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
396a4 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  2){.      p1->aC
396a5 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 4d 49 4e  oord[ii].i = MIN
396a6 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
396a7 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  i, p2->aCoord[ii
396a8 5d 2e 69 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e  ].i);.      p1->
396a9 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d  aCoord[ii+1].i =
396aa 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MAX(p1->aCoord[
396ab 69 69 2b 31 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  ii+1].i, p2->aCo
396ac 6f 72 64 5b 69 69 2b 31 5d 2e 69 29 3b 0a 20 20  ord[ii+1].i);.  
396ad 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
396ae 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
396af 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64  the area covered
396b0 20 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73   by p2 is a subs
396b1 65 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63  et of the area c
396b2 6f 76 65 72 65 64 0a 2a 2a 20 62 79 20 70 31 2e  overed.** by p1.
396b3 20 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   False otherwise
396b4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
396b5 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 52 74 72  cellContains(Rtr
396b6 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
396b7 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
396b8 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
396b9 20 69 69 3b 0a 20 20 69 6e 74 20 69 73 49 6e 74   ii;.  int isInt
396ba 20 3d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f   = (pRtree->eCoo
396bb 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
396bc 4f 52 44 5f 49 4e 54 33 32 29 3b 0a 20 20 66 6f  ORD_INT32);.  fo
396bd 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
396be 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
396bf 3d 32 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  =2){.    RtreeCo
396c0 6f 72 64 20 2a 61 31 20 3d 20 26 70 31 2d 3e 61  ord *a1 = &p1->a
396c1 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 52  Coord[ii];.    R
396c2 74 72 65 65 43 6f 6f 72 64 20 2a 61 32 20 3d 20  treeCoord *a2 = 
396c3 26 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p2->aCoord[ii];
396c4 0a 20 20 20 20 69 66 28 20 28 21 69 73 49 6e 74  .    if( (!isInt
396c5 20 26 26 20 28 61 32 5b 30 5d 2e 66 3c 61 31 5b   && (a2[0].f<a1[
396c6 30 5d 2e 66 20 7c 7c 20 61 32 5b 31 5d 2e 66 3e  0].f || a2[1].f>
396c7 61 31 5b 31 5d 2e 66 29 29 20 0a 20 20 20 20 20  a1[1].f)) .     
396c8 7c 7c 20 28 20 69 73 49 6e 74 20 26 26 20 28 61  || ( isInt && (a
396c9 32 5b 30 5d 2e 69 3c 61 31 5b 30 5d 2e 69 20 7c  2[0].i<a1[0].i |
396ca 7c 20 61 32 5b 31 5d 2e 69 3e 61 31 5b 31 5d 2e  | a2[1].i>a1[1].
396cb 69 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  i)) .    ){.    
396cc 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
396cd 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
396ce 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
396cf 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 63 65 6c  n the amount cel
396d0 6c 20 70 20 77 6f 75 6c 64 20 67 72 6f 77 20 62  l p would grow b
396d1 79 20 69 66 20 69 74 20 77 65 72 65 20 75 6e 69  y if it were uni
396d2 6f 6e 65 64 20 77 69 74 68 20 70 43 65 6c 6c 2e  oned with pCell.
396d3 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74  .*/.static float
396d4 20 63 65 6c 6c 47 72 6f 77 74 68 28 52 74 72 65   cellGrowth(Rtre
396d5 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
396d6 43 65 6c 6c 20 2a 70 2c 20 52 74 72 65 65 43 65  Cell *p, RtreeCe
396d7 6c 6c 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 66 6c  ll *pCell){.  fl
396d8 6f 61 74 20 61 72 65 61 3b 0a 20 20 52 74 72 65  oat area;.  Rtre
396d9 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65  eCell cell;.  me
396da 6d 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73  mcpy(&cell, p, s
396db 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
396dc 29 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c  );.  area = cell
396dd 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
396de 6c 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  ll);.  cellUnion
396df 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
396e0 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
396e1 20 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65   (cellArea(pRtre
396e2 65 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b  e, &cell)-area);
396e3 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .}..#if VARIANT_
396e4 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
396e5 53 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41  SUBTREE || VARIA
396e6 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c  NT_RSTARTREE_SPL
396e7 49 54 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  IT.static float 
396e8 63 65 6c 6c 4f 76 65 72 6c 61 70 28 0a 20 20 52  cellOverlap(.  R
396e9 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
396ea 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
396eb 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
396ec 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
396ed 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
396ee 65 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  e.){.  int ii;. 
396ef 20 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d   float overlap =
396f0 20 30 2e 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30   0.0;.  for(ii=0
396f1 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
396f2 29 7b 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69  ){.    if( ii!=i
396f3 45 78 63 6c 75 64 65 20 29 7b 0a 20 20 20 20 20  Exclude ){.     
396f4 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
396f5 6c 6f 61 74 20 6f 20 3d 20 31 2e 30 3b 0a 20 20  loat o = 1.0;.  
396f6 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
396f7 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
396f8 29 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20  ); jj+=2){.     
396f9 20 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20     double x1;.  
396fa 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 32 3b        double x2;
396fb 0a 0a 20 20 20 20 20 20 20 20 78 31 20 3d 20 4d  ..        x1 = M
396fc 41 58 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  AX(DCOORD(p->aCo
396fd 6f 72 64 5b 6a 6a 5d 29 2c 20 44 43 4f 4f 52 44  ord[jj]), DCOORD
396fe 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
396ff 64 5b 6a 6a 5d 29 29 3b 0a 20 20 20 20 20 20 20  d[jj]));.       
39700 20 78 32 20 3d 20 4d 49 4e 28 44 43 4f 4f 52 44   x2 = MIN(DCOORD
39701 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  (p->aCoord[jj+1]
39702 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
39703 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  ii].aCoord[jj+1]
39704 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  ));..        if(
39705 20 78 32 3c 78 31 20 29 7b 0a 20 20 20 20 20 20   x2<x1 ){.      
39706 20 20 20 20 6f 20 3d 20 30 2e 30 3b 0a 20 20 20      o = 0.0;.   
39707 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
39708 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
39709 20 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28         o = o * (
3970a 78 32 2d 78 31 29 3b 0a 20 20 20 20 20 20 20 20  x2-x1);.        
3970b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
3970c 6f 76 65 72 6c 61 70 20 2b 3d 20 6f 3b 0a 20 20  overlap += o;.  
3970d 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
3970e 20 6f 76 65 72 6c 61 70 3b 0a 7d 0a 23 65 6e 64   overlap;.}.#end
3970f 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
39710 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
39711 53 55 42 54 52 45 45 0a 73 74 61 74 69 63 20 66  SUBTREE.static f
39712 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61 70  loat cellOverlap
39713 45 6e 6c 61 72 67 65 6d 65 6e 74 28 0a 20 20 52  Enlargement(.  R
39714 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39715 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
39716 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 49 6e    RtreeCell *pIn
39717 73 65 72 74 2c 20 0a 20 20 52 74 72 65 65 43 65  sert, .  RtreeCe
39718 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
39719 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20  t nCell, .  int 
3971a 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 66 6c  iExclude.){.  fl
3971b 6f 61 74 20 62 65 66 6f 72 65 3b 0a 20 20 66 6c  oat before;.  fl
3971c 6f 61 74 20 61 66 74 65 72 3b 0a 20 20 62 65 66  oat after;.  bef
3971d 6f 72 65 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  ore = cellOverla
3971e 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43 65  p(pRtree, p, aCe
3971f 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c  ll, nCell, iExcl
39720 75 64 65 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f  ude);.  cellUnio
39721 6e 28 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e  n(pRtree, p, pIn
39722 73 65 72 74 29 3b 0a 20 20 61 66 74 65 72 20 3d  sert);.  after =
39723 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
39724 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
39725 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
39726 0a 20 20 72 65 74 75 72 6e 20 61 66 74 65 72 2d  .  return after-
39727 62 65 66 6f 72 65 3b 0a 7d 0a 23 65 6e 64 69 66  before;.}.#endif
39728 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
39729 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
3972a 73 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66  s the ChooseLeaf
3972b 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
3972c 47 75 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2a 20 43  Gutman[84]..** C
3972d 68 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20  hooseSubTree in 
3972e 72 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f  r*tree terminolo
3972f 67 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  gy..*/.static in
39730 74 20 43 68 6f 6f 73 65 4c 65 61 66 28 0a 20 20  t ChooseLeaf(.  
39731 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
39732 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39733 52 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Rtree table */. 
39734 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39735 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
39736 20 43 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20   Cell to insert 
39737 69 6e 74 6f 20 72 74 72 65 65 20 2a 2f 0a 20 20  into rtree */.  
39738 69 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20 20  int iHeight,    
39739 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3973a 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72  Height of sub-tr
3973b 65 65 20 72 6f 6f 74 65 64 20 61 74 20 70 43 65  ee rooted at pCe
3973c 6c 6c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  ll */.  RtreeNod
3973d 65 20 2a 2a 70 70 4c 65 61 66 20 20 20 20 20 20  e **ppLeaf      
3973e 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c       /* OUT: Sel
3973f 65 63 74 65 64 20 6c 65 61 66 20 70 61 67 65 20  ected leaf page 
39740 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
39741 20 20 69 6e 74 20 69 69 3b 0a 20 20 52 74 72 65    int ii;.  Rtre
39742 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20  eNode *pNode;.  
39743 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39744 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
39745 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69  pNode);..  for(i
39746 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
39747 4f 4b 20 26 26 20 69 69 3c 28 70 52 74 72 65 65  OK && ii<(pRtree
39748 2d 3e 69 44 65 70 74 68 2d 69 48 65 69 67 68 74  ->iDepth-iHeight
39749 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  ); ii++){.    in
3974a 74 20 69 43 65 6c 6c 3b 0a 20 20 20 20 73 71 6c  t iCell;.    sql
3974b 69 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74  ite3_int64 iBest
3974c 3b 0a 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  ;..    float fMi
3974d 6e 47 72 6f 77 74 68 3b 0a 20 20 20 20 66 6c 6f  nGrowth;.    flo
3974e 61 74 20 66 4d 69 6e 41 72 65 61 3b 0a 20 20 20  at fMinArea;.   
3974f 20 66 6c 6f 61 74 20 66 4d 69 6e 4f 76 65 72 6c   float fMinOverl
39750 61 70 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 43 65  ap;..    int nCe
39751 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
39752 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
39753 20 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65   cell;.    Rtree
39754 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 0a 20  Node *pChild;.. 
39755 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43     RtreeCell *aC
39756 65 6c 6c 20 3d 20 30 3b 0a 0a 23 69 66 20 56 41  ell = 0;..#if VA
39757 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39758 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20  CHOOSESUBTREE.  
39759 20 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65    if( ii==(pRtre
3975a 65 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a  e->iDepth-1) ){.
3975b 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20        int jj;.  
3975c 20 20 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69      aCell = sqli
3975d 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
3975e 66 28 52 74 72 65 65 43 65 6c 6c 29 2a 6e 43 65  f(RtreeCell)*nCe
3975f 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ll);.      if( !
39760 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  aCell ){.       
39761 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
39762 45 4d 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65  EM;.        node
39763 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39764 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20  pNode);.        
39765 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20  pNode = 0;.     
39766 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
39767 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a     }.      for(j
39768 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a  j=0; jj<nCell; j
39769 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 6f  j++){.        no
3976a 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
3976b 2c 20 70 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43  , pNode, jj, &aC
3976c 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
3976d 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
3976e 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
3976f 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 77 68 69  e child node whi
39770 63 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c 61 72  ch will be enlar
39771 67 65 64 20 74 68 65 20 6c 65 61 73 74 20 69 66  ged the least if
39772 20 70 43 65 6c 6c 0a 20 20 20 20 2a 2a 20 69 73   pCell.    ** is
39773 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
39774 74 2e 20 52 65 73 6f 6c 76 65 20 74 69 65 73 20  t. Resolve ties 
39775 62 79 20 63 68 6f 6f 73 69 6e 67 20 74 68 65 20  by choosing the 
39776 65 6e 74 72 79 20 77 69 74 68 0a 20 20 20 20 2a  entry with.    *
39777 2a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 61  * the smallest a
39778 72 65 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rea..    */.    
39779 66 6f 72 28 69 43 65 6c 6c 3d 30 3b 20 69 43 65  for(iCell=0; iCe
3977a 6c 6c 3c 6e 43 65 6c 6c 3b 20 69 43 65 6c 6c 2b  ll<nCell; iCell+
3977b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
3977c 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 66 6c  growth;.      fl
3977d 6f 61 74 20 61 72 65 61 3b 0a 20 20 20 20 20 20  oat area;.      
3977e 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20  float overlap = 
3977f 30 2e 30 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47  0.0;.      nodeG
39780 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
39781 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65  Node, iCell, &ce
39782 6c 6c 29 3b 0a 20 20 20 20 20 20 67 72 6f 77 74  ll);.      growt
39783 68 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70  h = cellGrowth(p
39784 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43  Rtree, &cell, pC
39785 65 6c 6c 29 3b 0a 20 20 20 20 20 20 61 72 65 61  ell);.      area
39786 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
39787 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 23 69 66 20  ee, &cell);.#if 
39788 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
39789 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a  E_CHOOSESUBTREE.
3978a 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70        if( ii==(p
3978b 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
3978c 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72   ){.        over
3978d 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  lap = cellOverla
3978e 70 45 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74  pEnlargement(pRt
3978f 72 65 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c  ree,&cell,pCell,
39790 61 43 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c  aCell,nCell,iCel
39791 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  l);.      }.#end
39792 69 66 0a 20 20 20 20 20 20 69 66 28 20 28 69 43  if.      if( (iC
39793 65 6c 6c 3d 3d 30 29 20 0a 20 20 20 20 20 20 20  ell==0) .       
39794 7c 7c 20 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e  || (overlap<fMin
39795 4f 76 65 72 6c 61 70 29 20 0a 20 20 20 20 20 20  Overlap) .      
39796 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d   || (overlap==fM
39797 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f  inOverlap && gro
39798 77 74 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0a  wth<fMinGrowth).
39799 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
3979a 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
3979b 26 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47  && growth==fMinG
3979c 72 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d  rowth && area<fM
3979d 69 6e 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  inArea).      ){
3979e 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65  .        fMinOve
3979f 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a  rlap = overlap;.
397a0 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77          fMinGrow
397a1 74 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20 20  th = growth;.   
397a2 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d 20       fMinArea = 
397a3 61 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69 42  area;.        iB
397a4 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77 69  est = cell.iRowi
397a5 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  d;.      }.    }
397a6 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
397a7 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20 72  ee(aCell);.    r
397a8 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
397a9 70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20 70  pRtree, iBest, p
397aa 4e 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a  Node, &pChild);.
397ab 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
397ac 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
397ad 20 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68 69      pNode = pChi
397ae 6c 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 65  ld;.  }..  *ppLe
397af 61 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 65  af = pNode;.  re
397b0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
397b1 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74 68  * A cell with th
397b2 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20 61  e same content a
397b3 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73 74  s pCell has just
397b4 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20 69   been inserted i
397b5 6e 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65 20  nto.** the node 
397b6 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e 63  pNode. This func
397b7 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68 65  tion updates the
397b8 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63 65   bounding box ce
397b9 6c 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61 6e  lls in.** all an
397ba 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73 2e  cestor elements.
397bb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
397bc 41 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74  AdjustTree(.  Rt
397bd 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20  ree *pRtree,    
397be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
397bf 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
397c0 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
397c1 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
397c2 20 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20        /* Adjust 
397c3 61 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73  ancestry of this
397c4 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65   node. */.  Rtre
397c5 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20  eCell *pCell    
397c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
397c7 20 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a   This cell was j
397c8 75 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ust inserted */.
397c9 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
397ca 70 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69  p = pNode;.  whi
397cb 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29  le( p->pParent )
397cc 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
397cd 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e  cell;.    RtreeN
397ce 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
397cf 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
397d0 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50  nt iCell = nodeP
397d1 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
397d2 65 2c 20 70 29 3b 0a 0a 20 20 20 20 6e 6f 64 65  e, p);..    node
397d3 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
397d4 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20  pParent, iCell, 
397d5 26 63 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  &cell);.    if( 
397d6 21 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52  !cellContains(pR
397d7 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65  tree, &cell, pCe
397d8 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 65 6c  ll) ){.      cel
397d9 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
397da 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
397db 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
397dc 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50  eCell(pRtree, pP
397dd 61 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43  arent, &cell, iC
397de 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 0a 20 20  ell);.    }. .  
397df 20 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20    p = pParent;. 
397e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
397e1 65 20 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69  e mapping (iRowi
397e2 64 2d 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65  d->iNode) to the
397e3 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74   <rtree>_rowid t
397e4 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
397e5 69 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52  int rowidWrite(R
397e6 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71  tree *pRtree, sq
397e7 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
397e8 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
397e9 34 20 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69  4 iNode){.  sqli
397ea 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
397eb 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
397ec 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
397ed 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
397ee 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
397ef 69 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f  iteRowid, 2, iNo
397f0 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  de);.  sqlite3_s
397f1 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69  tep(pRtree->pWri
397f2 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75  teRowid);.  retu
397f3 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
397f4 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
397f5 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  owid);.}../*.** 
397f6 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69  Write mapping (i
397f7 4e 6f 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74  Node->iPar) to t
397f8 68 65 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e  he <rtree>_paren
397f9 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  t table..*/.stat
397fa 69 63 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69  ic int parentWri
397fb 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
397fc 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
397fd 69 4e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69  iNode, sqlite3_i
397fe 6e 74 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71  nt64 iPar){.  sq
397ff 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
39800 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
39801 61 72 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29  arent, 1, iNode)
39802 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
39803 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
39804 57 72 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20  WriteParent, 2, 
39805 69 50 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iPar);.  sqlite3
39806 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57  _step(pRtree->pW
39807 72 69 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72  riteParent);.  r
39808 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65  eturn sqlite3_re
39809 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69  set(pRtree->pWri
3980a 74 65 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74  teParent);.}..st
3980b 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
3980c 73 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a  sertCell(Rtree *
3980d 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52  , RtreeNode *, R
3980e 74 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29  treeCell *, int)
3980f 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  ;..#if VARIANT_G
39810 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
39811 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  LIT./*.** Implem
39812 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
39813 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f  linear variant o
39814 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29  f the PickNext()
39815 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a   function from.*
39816 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a  * Guttman[84]..*
39817 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65  /.static RtreeCe
39818 6c 6c 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65  ll *LinearPickNe
39819 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  xt(.  Rtree *pRt
3981a 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ree,.  RtreeCell
3981b 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
3981c 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43  nCell, .  RtreeC
3981d 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a  ell *pLeftBox, .
3981e 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69    RtreeCell *pRi
3981f 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61  ghtBox,.  int *a
39820 69 55 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69  iUsed.){.  int i
39821 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61  i;.  for(ii=0; a
39822 69 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29  iUsed[ii]; ii++)
39823 3b 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d  ;.  aiUsed[ii] =
39824 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
39825 65 6c 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ell[ii];.}../*.*
39826 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
39827 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76   of the linear v
39828 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69  ariant of the Pi
39829 63 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74 69  ckSeeds() functi
3982a 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
3982b 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
3982c 63 20 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63  c void LinearPic
3982d 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
3982e 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
3982f 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
39830 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
39831 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
39832 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
39833 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ed.){.  int i;. 
39834 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d   int iLeftSeed =
39835 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74   0;.  int iRight
39836 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c 6f 61  Seed = 1;.  floa
39837 74 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72  t maxNormalInner
39838 57 69 64 74 68 20 3d 20 30 2e 30 3b 0a 0a 20 20  Width = 0.0;..  
39839 2f 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65  /* Pick two "see
3983a 64 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68  d" cells from th
3983b 65 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73  e array of cells
3983c 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20  . The algorithm 
3983d 75 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69  used.  ** here i
3983e 73 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b  s the LinearPick
3983f 53 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20  Seeds algorithm 
39840 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34  from Gutman[1984
39841 5d 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64  ]. The .  ** ind
39842 69 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20  ices of the two 
39843 73 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68  seed cells in th
39844 65 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72  e array are stor
39845 65 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a  ed in local.  **
39846 20 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74   variables iLeft
39847 53 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53  Seek and iRightS
39848 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  eed..  */.  for(
39849 69 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e  i=0; i<pRtree->n
3984a 44 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  Dim; i++){.    f
3984b 6c 6f 61 74 20 78 31 20 3d 20 44 43 4f 4f 52 44  loat x1 = DCOORD
3984c 28 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64  (aCell[0].aCoord
3984d 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 66 6c 6f 61  [i*2]);.    floa
3984e 74 20 78 32 20 3d 20 44 43 4f 4f 52 44 28 61 43  t x2 = DCOORD(aC
3984f 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a  ell[0].aCoord[i*
39850 32 2b 31 5d 29 3b 0a 20 20 20 20 66 6c 6f 61 74  2+1]);.    float
39851 20 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 66 6c   x3 = x1;.    fl
39852 6f 61 74 20 78 34 20 3d 20 78 32 3b 0a 20 20 20  oat x4 = x2;.   
39853 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 69 6e   int jj;..    in
39854 74 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30 3b  t iCellLeft = 0;
39855 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 52 69  .    int iCellRi
39856 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 6f  ght = 0;..    fo
39857 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 43 65 6c 6c  r(jj=1; jj<nCell
39858 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  ; jj++){.      f
39859 6c 6f 61 74 20 6c 65 66 74 20 3d 20 44 43 4f 4f  loat left = DCOO
3985a 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f  RD(aCell[jj].aCo
3985b 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 20  ord[i*2]);.     
3985c 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 44   float right = D
3985d 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e  COORD(aCell[jj].
3985e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a  aCoord[i*2+1]);.
3985f 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3c  .      if( left<
39860 78 31 20 29 20 78 31 20 3d 20 6c 65 66 74 3b 0a  x1 ) x1 = left;.
39861 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3e        if( right>
39862 78 34 20 29 20 78 34 20 3d 20 72 69 67 68 74 3b  x4 ) x4 = right;
39863 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3e  .      if( left>
39864 78 33 20 29 7b 0a 20 20 20 20 20 20 20 20 78 33  x3 ){.        x3
39865 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 20   = left;.       
39866 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 6a 6a   iCellRight = jj
39867 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
39868 69 66 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0a  if( right<x2 ){.
39869 20 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67          x2 = rig
3986a 68 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ht;.        iCel
3986b 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20 20 20 20  lLeft = jj;.    
3986c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69    }.    }..    i
3986d 66 28 20 78 34 21 3d 78 31 20 29 7b 0a 20 20 20  f( x4!=x1 ){.   
3986e 20 20 20 66 6c 6f 61 74 20 6e 6f 72 6d 61 6c 77     float normalw
3986f 69 64 74 68 20 3d 20 28 78 33 20 2d 20 78 32 29  idth = (x3 - x2)
39870 20 2f 20 28 78 34 20 2d 20 78 31 29 3b 0a 20 20   / (x4 - x1);.  
39871 20 20 20 20 69 66 28 20 6e 6f 72 6d 61 6c 77 69      if( normalwi
39872 64 74 68 3e 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e  dth>maxNormalInn
39873 65 72 57 69 64 74 68 20 29 7b 0a 20 20 20 20 20  erWidth ){.     
39874 20 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69     iLeftSeed = i
39875 43 65 6c 6c 4c 65 66 74 3b 0a 20 20 20 20 20 20  CellLeft;.      
39876 20 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 69    iRightSeed = i
39877 43 65 6c 6c 52 69 67 68 74 3b 0a 20 20 20 20 20  CellRight;.     
39878 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
39879 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c  *piLeftSeed = iL
3987a 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69 52 69  eftSeed;.  *piRi
3987b 67 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74  ghtSeed = iRight
3987c 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Seed;.}.#endif /
3987d 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  * VARIANT_GUTTMA
3987e 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a  N_LINEAR_SPLIT *
3987f 2f 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  /..#if VARIANT_G
39880 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39881 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
39882 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
39883 68 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72  he quadratic var
39884 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
39885 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
39886 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
39887 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
39888 74 72 65 65 43 65 6c 6c 20 2a 51 75 61 64 72 61  treeCell *Quadra
39889 74 69 63 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  ticPickNext(.  R
3988a 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
3988b 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
3988c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
3988d 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
3988e 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
3988f 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
39890 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
39891 7b 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53  {.  #define FABS
39892 28 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e  (a) ((a)<0.0?-1.
39893 30 2a 28 61 29 3a 28 61 29 29 0a 0a 20 20 69 6e  0*(a):(a))..  in
39894 74 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b 0a  t iSelect = -1;.
39895 20 20 66 6c 6f 61 74 20 66 44 69 66 66 3b 0a 20    float fDiff;. 
39896 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
39897 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
39898 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69  i++){.    if( ai
39899 55 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20  Used[ii]==0 ){. 
3989a 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
3989b 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74  = cellGrowth(pRt
3989c 72 65 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26  ree, pLeftBox, &
3989d 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20  aCell[ii]);.    
3989e 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
3989f 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
398a0 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61 43  e, pLeftBox, &aC
398a1 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20  ell[ii]);.      
398a2 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 46 41 42  float diff = FAB
398a3 53 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a 20  S(right-left);. 
398a4 20 20 20 20 20 69 66 28 20 69 53 65 6c 65 63 74       if( iSelect
398a5 3c 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66 66  <0 || diff>fDiff
398a6 20 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69 66   ){.        fDif
398a7 66 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20 20  f = diff;.      
398a8 20 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b 0a    iSelect = ii;.
398a9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
398aa 7d 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65  }.  aiUsed[iSele
398ab 63 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  ct] = 1;.  retur
398ac 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74  n &aCell[iSelect
398ad 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ];.}../*.** Impl
398ae 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
398af 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69  e quadratic vari
398b0 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53  ant of the PickS
398b1 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  eeds() function 
398b2 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
398b3 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  84]..*/.static v
398b4 6f 69 64 20 51 75 61 64 72 61 74 69 63 50 69 63  oid QuadraticPic
398b5 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
398b6 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
398b7 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
398b8 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
398b9 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
398ba 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
398bb 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ed.){.  int ii;.
398bc 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e 74    int jj;..  int
398bd 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a   iLeftSeed = 0;.
398be 20 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64    int iRightSeed
398bf 20 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 66 57   = 1;.  float fW
398c0 61 73 74 65 20 3d 20 30 2e 30 3b 0a 0a 20 20 66  aste = 0.0;..  f
398c1 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
398c2 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  l; ii++){.    fo
398c3 72 28 6a 6a 3d 69 69 2b 31 3b 20 6a 6a 3c 6e 43  r(jj=ii+1; jj<nC
398c4 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
398c5 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
398c6 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
398c7 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20   &aCell[jj]);.  
398c8 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68      float growth
398c9 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52   = cellGrowth(pR
398ca 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tree, &aCell[ii]
398cb 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20  , &aCell[jj]);. 
398cc 20 20 20 20 20 66 6c 6f 61 74 20 77 61 73 74 65       float waste
398cd 20 3d 20 67 72 6f 77 74 68 20 2d 20 72 69 67 68   = growth - righ
398ce 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 77 61  t;..      if( wa
398cf 73 74 65 3e 66 57 61 73 74 65 20 29 7b 0a 20 20  ste>fWaste ){.  
398d0 20 20 20 20 20 20 69 4c 65 66 74 53 65 65 64 20        iLeftSeed 
398d1 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 69 52  = ii;.        iR
398d2 69 67 68 74 53 65 65 64 20 3d 20 6a 6a 3b 0a 20  ightSeed = jj;. 
398d3 20 20 20 20 20 20 20 66 57 61 73 74 65 20 3d 20         fWaste = 
398d4 77 61 73 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20  waste;.      }. 
398d5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c     }.  }..  *piL
398d6 65 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53  eftSeed = iLeftS
398d7 65 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53  eed;.  *piRightS
398d8 65 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64  eed = iRightSeed
398d9 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41  ;.}.#endif /* VA
398da 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
398db 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 2a 2f  ADRATIC_SPLIT */
398dc 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
398dd 73 20 61 49 64 78 2c 20 61 44 69 73 74 61 6e 63  s aIdx, aDistanc
398de 65 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c  e and aSpare all
398df 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73   point to arrays
398e0 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78   of size.** nIdx
398e1 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79  . The aIdx array
398e2 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65   contains the se
398e3 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72  t of integers fr
398e4 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64  om 0 to .** (nId
398e5 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69  x-1) in no parti
398e6 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69  cular order. Thi
398e7 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73  s function sorts
398e8 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69   the values.** i
398e9 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67  n aIdx according
398ea 20 74 6f 20 74 68 65 20 69 6e 64 65 78 65 64 20   to the indexed 
398eb 76 61 6c 75 65 73 20 69 6e 20 61 44 69 73 74 61  values in aDista
398ec 6e 63 65 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  nce. For.** exam
398ed 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ple, assuming th
398ee 65 20 69 6e 70 75 74 73 3a 0a 2a 2a 0a 2a 2a 20  e inputs:.**.** 
398ef 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
398f0 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
398f1 20 7d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63   }.**   aDistanc
398f2 65 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20  e = { 5.0, 2.0, 
398f3 37 2e 30 2c 20 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a  7.0, 6.0 }.**.**
398f4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
398f5 65 74 73 20 74 68 65 20 61 49 64 78 20 61 72 72  ets the aIdx arr
398f6 61 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a 0a 2a  ay to contain:.*
398f7 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20 20  *.**   aIdx     
398f8 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20 32   = { 0,   1,   2
398f9 2c 20 20 20 33 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  ,   3 }.**.** Th
398fa 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
398fb 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
398fc 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
398fd 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
398fe 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
398ff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
39900 72 74 42 79 44 69 73 74 61 6e 63 65 28 0a 20 20  rtByDistance(.  
39901 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e  int *aIdx, .  in
39902 74 20 6e 49 64 78 2c 20 0a 20 20 66 6c 6f 61 74  t nIdx, .  float
39903 20 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20   *aDistance, .  
39904 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20  int *aSpare.){. 
39905 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20   if( nIdx>1 ){. 
39906 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
39907 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
39908 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
39909 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
3990a 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
3990b 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
3990c 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
3990d 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
3990e 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
3990f 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
39910 73 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c  stance(aLeft, nL
39911 65 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  eft, aDistance, 
39912 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72  aSpare);.    Sor
39913 74 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67  tByDistance(aRig
39914 68 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73  ht, nRight, aDis
39915 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a  tance, aSpare);.
39916 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61  .    memcpy(aSpa
39917 72 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f  re, aLeft, sizeo
39918 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20  f(int)*nLeft);. 
39919 20 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72     aLeft = aSpar
3991a 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69  e;..    while( i
3991b 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52  Left<nLeft || iR
3991c 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20  ight<nRight ){. 
3991d 20 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d       if( iLeft==
3991e 6e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  nLeft ){.       
3991f 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
39920 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69  ht] = aRight[iRi
39921 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52  ght];.        iR
39922 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ight++;.      }e
39923 6c 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d  lse if( iRight==
39924 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
39925 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
39926 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65  ght] = aLeft[iLe
39927 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65  ft];.        iLe
39928 66 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ft++;.      }els
39929 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  e{.        float
3992a 20 66 4c 65 66 74 20 3d 20 61 44 69 73 74 61 6e   fLeft = aDistan
3992b 63 65 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d  ce[aLeft[iLeft]]
3992c 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  ;.        float 
3992d 66 52 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e  fRight = aDistan
3992e 63 65 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ce[aRight[iRight
3992f 5d 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]];.        if( 
39930 66 4c 65 66 74 3c 66 52 69 67 68 74 20 29 7b 0a  fLeft<fRight ){.
39931 20 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69            aIdx[i
39932 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
39933 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20  Left[iLeft];.   
39934 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
39935 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
39936 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c           aIdx[iL
39937 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52  eft+iRight] = aR
39938 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20  ight[iRight];.  
39939 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b          iRight++
3993a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3993b 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
3993c 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
3993d 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
3993e 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
3993f 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39940 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
39941 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
39942 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61    float left = a
39943 44 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a  Distance[aIdx[jj
39944 2d 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  -1]];.        fl
39945 6f 61 74 20 72 69 67 68 74 20 3d 20 61 44 69 73  oat right = aDis
39946 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 5d 5d 3b  tance[aIdx[jj]];
39947 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39948 20 6c 65 66 74 3c 3d 72 69 67 68 74 20 29 3b 0a   left<=right );.
39949 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
3994a 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ndif.  }.}../*.*
3994b 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78  * Arguments aIdx
3994c 2c 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70 61  , aCell and aSpa
3994d 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
3994e 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
3994f 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
39950 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
39951 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
39952 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
39953 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
39954 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
39955 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
39956 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
39957 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
39958 6f 72 64 69 6e 67 20 74 6f 20 64 69 6d 65 6e 73  ording to dimens
39959 69 6f 6e 20 69 44 69 6d 20 6f 66 20 74 68 65 20  ion iDim of the 
3995a 63 65 6c 6c 73 20 69 6e 20 61 43 65 6c 6c 2e 20  cells in aCell. 
3995b 54 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 76  The.** minimum v
3995c 61 6c 75 65 20 6f 66 20 64 69 6d 65 6e 73 69 6f  alue of dimensio
3995d 6e 20 69 44 69 6d 20 69 73 20 63 6f 6e 73 69 64  n iDim is consid
3995e 65 72 65 64 20 66 69 72 73 74 2c 20 74 68 65 0a  ered first, the.
3995f 2a 2a 20 6d 61 78 69 6d 75 6d 20 75 73 65 64 20  ** maximum used 
39960 74 6f 20 62 72 65 61 6b 20 74 69 65 73 2e 0a 2a  to break ties..*
39961 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20  *.** The aSpare 
39962 61 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73  array is used as
39963 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69   temporary worki
39964 6e 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a  ng space by the.
39965 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72  ** sorting algor
39966 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ithm..*/.static 
39967 76 6f 69 64 20 53 6f 72 74 42 79 44 69 6d 65 6e  void SortByDimen
39968 73 69 6f 6e 28 0a 20 20 52 74 72 65 65 20 2a 70  sion(.  Rtree *p
39969 52 74 72 65 65 2c 0a 20 20 69 6e 74 20 2a 61 49  Rtree,.  int *aI
3996a 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
3996b 20 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0a 20   .  int iDim, . 
3996c 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
3996d 6c 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  l, .  int *aSpar
3996e 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
3996f 31 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 4c  1 ){..    int iL
39970 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  eft = 0;.    int
39971 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20   iRight = 0;..  
39972 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49    int nLeft = nI
39973 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52  dx/2;.    int nR
39974 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66  ight = nIdx-nLef
39975 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66  t;.    int *aLef
39976 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e  t = aIdx;.    in
39977 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64  t *aRight = &aId
39978 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53  x[nLeft];..    S
39979 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
3997a 52 74 72 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c  Rtree, aLeft, nL
3997b 65 66 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c  eft, iDim, aCell
3997c 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53  , aSpare);.    S
3997d 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
3997e 52 74 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e  Rtree, aRight, n
3997f 52 69 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65  Right, iDim, aCe
39980 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20  ll, aSpare);..  
39981 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c    memcpy(aSpare,
39982 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69   aLeft, sizeof(i
39983 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20  nt)*nLeft);.    
39984 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a  aLeft = aSpare;.
39985 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74      while( iLeft
39986 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74  <nLeft || iRight
39987 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <nRight ){.     
39988 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 31 20 3d   double xleft1 =
39989 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c   DCOORD(aCell[aL
3998a 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f  eft[iLeft]].aCoo
3998b 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20  rd[iDim*2]);.   
3998c 20 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 32     double xleft2
3998d 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
3998e 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43  aLeft[iLeft]].aC
3998f 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b  oord[iDim*2+1]);
39990 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
39991 69 67 68 74 31 20 3d 20 44 43 4f 4f 52 44 28 61  ight1 = DCOORD(a
39992 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
39993 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
39994 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
39995 6c 65 20 78 72 69 67 68 74 32 20 3d 20 44 43 4f  le xright2 = DCO
39996 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74  ORD(aCell[aRight
39997 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64  [iRight]].aCoord
39998 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20  [iDim*2+1]);.   
39999 20 20 20 69 66 28 20 28 69 4c 65 66 74 21 3d 6e     if( (iLeft!=n
3999a 4c 65 66 74 29 20 26 26 20 28 28 69 52 69 67 68  Left) && ((iRigh
3999b 74 3d 3d 6e 52 69 67 68 74 29 0a 20 20 20 20 20  t==nRight).     
3999c 20 20 7c 7c 20 28 78 6c 65 66 74 31 3c 78 72 69    || (xleft1<xri
3999d 67 68 74 31 29 0a 20 20 20 20 20 20 20 7c 7c 20  ght1).       || 
3999e 28 78 6c 65 66 74 31 3d 3d 78 72 69 67 68 74 31  (xleft1==xright1
3999f 20 26 26 20 78 6c 65 66 74 32 3c 78 72 69 67 68   && xleft2<xrigh
399a0 74 32 29 0a 20 20 20 20 20 20 29 29 7b 0a 20 20  t2).      )){.  
399a1 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74        aIdx[iLeft
399a2 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74  +iRight] = aLeft
399a3 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20  [iLeft];.       
399a4 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20   iLeft++;.      
399a5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
399a6 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
399a7 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
399a8 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67  t];.        iRig
399a9 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht++;.      }.  
399aa 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f    }..#if 0.    /
399ab 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
399ac 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a   sort worked */.
399ad 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
399ae 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  jj;.      for(jj
399af 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b  =1; jj<nIdx; jj+
399b0 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61  +){.        floa
399b1 74 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c 6c  t xleft1 = aCell
399b2 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f  [aIdx[jj-1]].aCo
399b3 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20  ord[iDim*2];.   
399b4 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
399b5 32 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  2 = aCell[aIdx[j
399b6 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
399b7 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  m*2+1];.        
399b8 66 6c 6f 61 74 20 78 72 69 67 68 74 31 20 3d 20  float xright1 = 
399b9 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e  aCell[aIdx[jj]].
399ba 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a  aCoord[iDim*2];.
399bb 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72          float xr
399bc 69 67 68 74 32 20 3d 20 61 43 65 6c 6c 5b 61 49  ight2 = aCell[aI
399bd 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69  dx[jj]].aCoord[i
399be 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20  Dim*2+1];.      
399bf 20 20 61 73 73 65 72 74 28 20 78 6c 65 66 74 31    assert( xleft1
399c0 3c 3d 78 72 69 67 68 74 31 20 26 26 20 28 78 6c  <=xright1 && (xl
399c1 65 66 74 31 3c 78 72 69 67 68 74 31 20 7c 7c 20  eft1<xright1 || 
399c2 78 6c 65 66 74 32 3c 3d 78 72 69 67 68 74 32 29  xleft2<=xright2)
399c3 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
399c4 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  }.#endif.  }.}..
399c5 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
399c6 52 54 52 45 45 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  RTREE_SPLIT./*.*
399c7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
399c8 20 6f 66 20 74 68 65 20 52 2a 2d 74 72 65 65 20   of the R*-tree 
399c9 76 61 72 69 61 6e 74 20 6f 66 20 53 70 6c 69 74  variant of Split
399ca 4e 6f 64 65 20 66 72 6f 6d 20 42 65 63 6b 6d 61  Node from Beckma
399cb 6e 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73 74 61 74  n[1990]..*/.stat
399cc 69 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65  ic int splitNode
399cd 53 74 61 72 74 72 65 65 28 0a 20 20 52 74 72 65  Startree(.  Rtre
399ce 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
399cf 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20  eeCell *aCell,. 
399d0 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74   int nCell,.  Rt
399d1 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a  reeNode *pLeft,.
399d2 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69    RtreeNode *pRi
399d3 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ght,.  RtreeCell
399d4 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52   *pBboxLeft,.  R
399d5 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52  treeCell *pBboxR
399d6 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 2a  ight.){.  int **
399d7 61 61 53 6f 72 74 65 64 3b 0a 20 20 69 6e 74 20  aaSorted;.  int 
399d8 2a 61 53 70 61 72 65 3b 0a 20 20 69 6e 74 20 69  *aSpare;.  int i
399d9 69 3b 0a 0a 20 20 69 6e 74 20 69 42 65 73 74 44  i;..  int iBestD
399da 69 6d 3b 0a 20 20 69 6e 74 20 69 42 65 73 74 53  im;.  int iBestS
399db 70 6c 69 74 3b 0a 20 20 66 6c 6f 61 74 20 66 42  plit;.  float fB
399dc 65 73 74 4d 61 72 67 69 6e 3b 0a 0a 20 20 69 6e  estMargin;..  in
399dd 74 20 6e 42 79 74 65 20 3d 20 28 70 52 74 72 65  t nByte = (pRtre
399de 65 2d 3e 6e 44 69 6d 2b 31 29 2a 28 73 69 7a 65  e->nDim+1)*(size
399df 6f 66 28 69 6e 74 2a 29 2b 6e 43 65 6c 6c 2a 73  of(int*)+nCell*s
399e0 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 0a 20 20  izeof(int));..  
399e1 61 61 53 6f 72 74 65 64 20 3d 20 28 69 6e 74 20  aaSorted = (int 
399e2 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
399e3 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  c(nByte);.  if( 
399e4 21 61 61 53 6f 72 74 65 64 20 29 7b 0a 20 20 20  !aaSorted ){.   
399e5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
399e6 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 53 70  OMEM;.  }..  aSp
399e7 61 72 65 20 3d 20 26 28 28 69 6e 74 20 2a 29 26  are = &((int *)&
399e8 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d  aaSorted[pRtree-
399e9 3e 6e 44 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e  >nDim])[pRtree->
399ea 6e 44 69 6d 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 6d  nDim*nCell];.  m
399eb 65 6d 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20  emset(aaSorted, 
399ec 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 66 6f 72  0, nByte);.  for
399ed 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65  (ii=0; ii<pRtree
399ee 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nDim; ii++){. 
399ef 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 61     int jj;.    a
399f0 61 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26 28  aSorted[ii] = &(
399f1 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64  (int *)&aaSorted
399f2 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b  [pRtree->nDim])[
399f3 69 69 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 20 20 66  ii*nCell];.    f
399f4 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c  or(jj=0; jj<nCel
399f5 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  l; jj++){.      
399f6 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6a 6a 5d  aaSorted[ii][jj]
399f7 20 3d 20 6a 6a 3b 0a 20 20 20 20 7d 0a 20 20 20   = jj;.    }.   
399f8 20 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e   SortByDimension
399f9 28 70 52 74 72 65 65 2c 20 61 61 53 6f 72 74 65  (pRtree, aaSorte
399fa 64 5b 69 69 5d 2c 20 6e 43 65 6c 6c 2c 20 69 69  d[ii], nCell, ii
399fb 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29  , aCell, aSpare)
399fc 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
399fd 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
399fe 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66  im; ii++){.    f
399ff 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
39a00 30 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65  0;.    float fBe
39a01 73 74 4f 76 65 72 6c 61 70 3b 0a 20 20 20 20 66  stOverlap;.    f
39a02 6c 6f 61 74 20 66 42 65 73 74 41 72 65 61 3b 0a  loat fBestArea;.
39a03 20 20 20 20 69 6e 74 20 69 42 65 73 74 4c 65 66      int iBestLef
39a04 74 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74  t;.    int nLeft
39a05 3b 0a 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20  ;..    for(.    
39a06 20 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49    nLeft=RTREE_MI
39a07 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20  NCELLS(pRtree); 
39a08 0a 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28 6e  .      nLeft<=(n
39a09 43 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43 45  Cell-RTREE_MINCE
39a0a 4c 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0a 20  LLS(pRtree)); . 
39a0b 20 20 20 20 20 6e 4c 65 66 74 2b 2b 0a 20 20 20       nLeft++.   
39a0c 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43   ){.      RtreeC
39a0d 65 6c 6c 20 6c 65 66 74 3b 0a 20 20 20 20 20 20  ell left;.      
39a0e 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 3b  RtreeCell right;
39a0f 0a 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20  .      int kk;. 
39a10 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72 6c       float overl
39a11 61 70 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ap;.      float 
39a12 61 72 65 61 3b 0a 0a 20 20 20 20 20 20 6d 65 6d  area;..      mem
39a13 63 70 79 28 26 6c 65 66 74 2c 20 26 61 43 65 6c  cpy(&left, &aCel
39a14 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 30  l[aaSorted[ii][0
39a15 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
39a16 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 6d 65  Cell));.      me
39a17 6d 63 70 79 28 26 72 69 67 68 74 2c 20 26 61 43  mcpy(&right, &aC
39a18 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d  ell[aaSorted[ii]
39a19 5b 6e 43 65 6c 6c 2d 31 5d 5d 2c 20 73 69 7a 65  [nCell-1]], size
39a1a 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
39a1b 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 31 3b 20        for(kk=1; 
39a1c 6b 6b 3c 28 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b  kk<(nCell-1); kk
39a1d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
39a1e 20 6b 6b 3c 6e 4c 65 66 74 20 29 7b 0a 20 20 20   kk<nLeft ){.   
39a1f 20 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e         cellUnion
39a20 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20  (pRtree, &left, 
39a21 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
39a22 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20  ii][kk]]);.     
39a23 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39a24 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39a25 74 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61  tree, &right, &a
39a26 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
39a27 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20  ][kk]]);.       
39a28 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
39a29 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d   margin += cellM
39a2a 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 6c  argin(pRtree, &l
39a2b 65 66 74 29 3b 0a 20 20 20 20 20 20 6d 61 72 67  eft);.      marg
39a2c 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
39a2d 28 70 52 74 72 65 65 2c 20 26 72 69 67 68 74 29  (pRtree, &right)
39a2e 3b 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20  ;.      overlap 
39a2f 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52  = cellOverlap(pR
39a30 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 72 69  tree, &left, &ri
39a31 67 68 74 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20  ght, 1, -1);.   
39a32 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72     area = cellAr
39a33 65 61 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ea(pRtree, &left
39a34 29 20 2b 20 63 65 6c 6c 41 72 65 61 28 70 52 74  ) + cellArea(pRt
39a35 72 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  ree, &right);.  
39a36 20 20 20 20 69 66 28 20 28 6e 4c 65 66 74 3d 3d      if( (nLeft==
39a37 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
39a38 52 74 72 65 65 29 29 0a 20 20 20 20 20 20 20 7c  Rtree)).       |
39a39 7c 20 28 6f 76 65 72 6c 61 70 3c 66 42 65 73 74  | (overlap<fBest
39a3a 4f 76 65 72 6c 61 70 29 0a 20 20 20 20 20 20 20  Overlap).       
39a3b 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 42 65  || (overlap==fBe
39a3c 73 74 4f 76 65 72 6c 61 70 20 26 26 20 61 72 65  stOverlap && are
39a3d 61 3c 66 42 65 73 74 41 72 65 61 29 0a 20 20 20  a<fBestArea).   
39a3e 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 42     ){.        iB
39a3f 65 73 74 4c 65 66 74 20 3d 20 6e 4c 65 66 74 3b  estLeft = nLeft;
39a40 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 4f 76  .        fBestOv
39a41 65 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b  erlap = overlap;
39a42 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 41 72  .        fBestAr
39a43 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20 20  ea = area;.     
39a44 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
39a45 28 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67 69  ( ii==0 || margi
39a46 6e 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29 7b  n<fBestMargin ){
39a47 0a 20 20 20 20 20 20 69 42 65 73 74 44 69 6d 20  .      iBestDim 
39a48 3d 20 69 69 3b 0a 20 20 20 20 20 20 66 42 65 73  = ii;.      fBes
39a49 74 4d 61 72 67 69 6e 20 3d 20 6d 61 72 67 69 6e  tMargin = margin
39a4a 3b 0a 20 20 20 20 20 20 69 42 65 73 74 53 70 6c  ;.      iBestSpl
39a4b 69 74 20 3d 20 69 42 65 73 74 4c 65 66 74 3b 0a  it = iBestLeft;.
39a4c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6d 65 6d      }.  }..  mem
39a4d 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
39a4e 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69  aCell[aaSorted[i
39a4f 42 65 73 74 44 69 6d 5d 5b 30 5d 5d 2c 20 73 69  BestDim][0]], si
39a50 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
39a51 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78  ;.  memcpy(pBbox
39a52 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  Right, &aCell[aa
39a53 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d  Sorted[iBestDim]
39a54 5b 69 42 65 73 74 53 70 6c 69 74 5d 5d 2c 20 73  [iBestSplit]], s
39a55 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
39a56 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39a57 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
39a58 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
39a59 54 61 72 67 65 74 20 3d 20 28 69 69 3c 69 42 65  Target = (ii<iBe
39a5a 73 74 53 70 6c 69 74 29 3f 70 4c 65 66 74 3a 70  stSplit)?pLeft:p
39a5b 52 69 67 68 74 3b 0a 20 20 20 20 52 74 72 65 65  Right;.    Rtree
39a5c 43 65 6c 6c 20 2a 70 42 62 6f 78 20 3d 20 28 69  Cell *pBbox = (i
39a5d 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 42  i<iBestSplit)?pB
39a5e 62 6f 78 4c 65 66 74 3a 70 42 62 6f 78 52 69 67  boxLeft:pBboxRig
39a5f 68 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  ht;.    RtreeCel
39a60 6c 20 2a 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c  l *pCell = &aCel
39a61 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74  l[aaSorted[iBest
39a62 44 69 6d 5d 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e  Dim][ii]];.    n
39a63 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
39a64 74 72 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70  tree, pTarget, p
39a65 43 65 6c 6c 29 3b 0a 20 20 20 20 63 65 6c 6c 55  Cell);.    cellU
39a66 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62  nion(pRtree, pBb
39a67 6f 78 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a  ox, pCell);.  }.
39a68 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
39a69 61 61 53 6f 72 74 65 64 29 3b 0a 20 20 72 65 74  aaSorted);.  ret
39a6a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
39a6b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52  .#endif..#if VAR
39a6c 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
39a6d 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  IT./*.** Impleme
39a6e 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  ntation of the r
39a6f 65 67 75 6c 61 72 20 52 2d 74 72 65 65 20 53 70  egular R-tree Sp
39a70 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 47 75 74  litNode from Gut
39a71 74 6d 61 6e 5b 31 39 38 34 5d 2e 0a 2a 2f 0a 73  tman[1984]..*/.s
39a72 74 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e  tatic int splitN
39a73 6f 64 65 47 75 74 74 6d 61 6e 28 0a 20 20 52 74  odeGuttman(.  Rt
39a74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
39a75 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
39a76 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20  .  int nCell,.  
39a77 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
39a78 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
39a79 52 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65  Right,.  RtreeCe
39a7a 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20  ll *pBboxLeft,. 
39a7b 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
39a7c 78 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  xRight.){.  int 
39a7d 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
39a7e 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
39a7f 3d 20 31 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73  = 1;.  int *aiUs
39a80 65 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ed;.  int i;..  
39a81 61 69 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33  aiUsed = sqlite3
39a82 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 69  _malloc(sizeof(i
39a83 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 69 66  nt)*nCell);.  if
39a84 28 20 21 61 69 55 73 65 64 20 29 7b 0a 20 20 20  ( !aiUsed ){.   
39a85 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39a86 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
39a87 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69  et(aiUsed, 0, si
39a88 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
39a89 3b 0a 0a 20 20 50 69 63 6b 53 65 65 64 73 28 70  ;..  PickSeeds(p
39a8a 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43  Rtree, aCell, nC
39a8b 65 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65 64 2c  ell, &iLeftSeed,
39a8c 20 26 69 52 69 67 68 74 53 65 65 64 29 3b 0a 0a   &iRightSeed);..
39a8d 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c 65    memcpy(pBboxLe
39a8e 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74  ft, &aCell[iLeft
39a8f 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74  Seed], sizeof(Rt
39a90 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
39a91 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
39a92 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65 65  &aCell[iRightSee
39a93 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
39a94 43 65 6c 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e  Cell));.  nodeIn
39a95 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39a96 20 70 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69   pLeft, &aCell[i
39a97 4c 65 66 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f  LeftSeed]);.  no
39a98 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39a99 72 65 65 2c 20 70 52 69 67 68 74 2c 20 26 61 43  ree, pRight, &aC
39a9a 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 29  ell[iRightSeed])
39a9b 3b 0a 20 20 61 69 55 73 65 64 5b 69 4c 65 66 74  ;.  aiUsed[iLeft
39a9c 53 65 65 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55  Seed] = 1;.  aiU
39a9d 73 65 64 5b 69 52 69 67 68 74 53 65 65 64 5d 20  sed[iRightSeed] 
39a9e 3d 20 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43  = 1;..  for(i=nC
39a9f 65 6c 6c 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29  ell-2; i>0; i--)
39aa0 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
39aa1 2a 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e 65 78  *pNext;.    pNex
39aa2 74 20 3d 20 50 69 63 6b 4e 65 78 74 28 70 52 74  t = PickNext(pRt
39aa3 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c  ree, aCell, nCel
39aa4 6c 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 42  l, pBboxLeft, pB
39aa5 62 6f 78 52 69 67 68 74 2c 20 61 69 55 73 65 64  boxRight, aiUsed
39aa6 29 3b 0a 20 20 20 20 66 6c 6f 61 74 20 64 69 66  );.    float dif
39aa7 66 20 3d 20 20 0a 20 20 20 20 20 20 63 65 6c 6c  f =  .      cell
39aa8 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70  Growth(pRtree, p
39aa9 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74 29  BboxLeft, pNext)
39aaa 20 2d 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72   - .      cellGr
39aab 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62  owth(pRtree, pBb
39aac 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29 0a  oxRight, pNext).
39aad 20 20 20 20 3b 0a 20 20 20 20 69 66 28 20 28 52      ;.    if( (R
39aae 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52  TREE_MINCELLS(pR
39aaf 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67  tree)-NCELL(pRig
39ab0 68 74 29 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20  ht)==i).     || 
39ab1 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28 52 54  (diff>0.0 && (RT
39ab2 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
39ab3 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74  ree)-NCELL(pLeft
39ab4 29 21 3d 69 29 29 0a 20 20 20 20 29 7b 0a 20 20  )!=i)).    ){.  
39ab5 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
39ab6 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  ll(pRtree, pRigh
39ab7 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
39ab8 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
39ab9 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 70  e, pBboxRight, p
39aba 4e 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Next);.    }else
39abb 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e 73 65  {.      nodeInse
39abc 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
39abd 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20  Left, pNext);.  
39abe 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39abf 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c  tree, pBboxLeft,
39ac0 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20   pNext);.    }. 
39ac1 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
39ac2 65 65 28 61 69 55 73 65 64 29 3b 0a 20 20 72 65  ee(aiUsed);.  re
39ac3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
39ac4 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63  }.#endif..static
39ac5 20 69 6e 74 20 75 70 64 61 74 65 4d 61 70 70 69   int updateMappi
39ac6 6e 67 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ng(.  Rtree *pRt
39ac7 72 65 65 2c 20 0a 20 20 69 36 34 20 69 52 6f 77  ree, .  i64 iRow
39ac8 69 64 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  id, .  RtreeNode
39ac9 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
39aca 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  iHeight.){.  int
39acb 20 28 2a 78 53 65 74 4d 61 70 70 69 6e 67 29 28   (*xSetMapping)(
39acc 52 74 72 65 65 20 2a 2c 20 73 71 6c 69 74 65 33  Rtree *, sqlite3
39acd 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74 65 33 5f  _int64, sqlite3_
39ace 69 6e 74 36 34 29 3b 0a 20 20 78 53 65 74 4d 61  int64);.  xSetMa
39acf 70 70 69 6e 67 20 3d 20 28 28 69 48 65 69 67 68  pping = ((iHeigh
39ad0 74 3d 3d 30 29 3f 72 6f 77 69 64 57 72 69 74 65  t==0)?rowidWrite
39ad1 3a 70 61 72 65 6e 74 57 72 69 74 65 29 3b 0a 20  :parentWrite);. 
39ad2 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20 29   if( iHeight>0 )
39ad3 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
39ad4 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61  *pChild = nodeHa
39ad5 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c  shLookup(pRtree,
39ad6 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66   iRowid);.    if
39ad7 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20 20  ( pChild ){.    
39ad8 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39ad9 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50  tree, pChild->pP
39ada 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f  arent);.      no
39adb 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f 64  deReference(pNod
39adc 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c 64  e);.      pChild
39add 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64  ->pParent = pNod
39ade 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  e;.    }.  }.  r
39adf 65 74 75 72 6e 20 78 53 65 74 4d 61 70 70 69 6e  eturn xSetMappin
39ae0 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  g(pRtree, iRowid
39ae1 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
39ae2 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 53  .}..static int S
39ae3 70 6c 69 74 4e 6f 64 65 28 0a 20 20 52 74 72 65  plitNode(.  Rtre
39ae4 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
39ae5 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20  eeNode *pNode,. 
39ae6 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39ae7 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74  l,.  int iHeight
39ae8 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
39ae9 6e 74 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68  nt newCellIsRigh
39aea 74 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63  t = 0;..  int rc
39aeb 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
39aec 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
39aed 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65  L(pNode);.  Rtre
39aee 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20  eCell *aCell;.  
39aef 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20  int *aiUsed;..  
39af0 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
39af1 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64   = 0;.  RtreeNod
39af2 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a  e *pRight = 0;..
39af3 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74    RtreeCell left
39af4 62 62 6f 78 3b 0a 20 20 52 74 72 65 65 43 65 6c  bbox;.  RtreeCel
39af5 6c 20 72 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20  l rightbbox;..  
39af6 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61  /* Allocate an a
39af7 72 72 61 79 20 61 6e 64 20 70 6f 70 75 6c 61 74  rray and populat
39af8 65 20 69 74 20 77 69 74 68 20 61 20 63 6f 70 79  e it with a copy
39af9 20 6f 66 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20   of pCell and . 
39afa 20 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72   ** all cells fr
39afb 6f 6d 20 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54  om node pLeft. T
39afc 68 65 6e 20 7a 65 72 6f 20 74 68 65 20 6f 72 69  hen zero the ori
39afd 67 69 6e 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f  ginal node..  */
39afe 0a 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74  .  aCell = sqlit
39aff 65 33 5f 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f  e3_malloc((sizeo
39b00 66 28 52 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a  f(RtreeCell)+siz
39b01 65 6f 66 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c  eof(int))*(nCell
39b02 2b 31 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65  +1));.  if( !aCe
39b03 6c 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ll ){.    rc = S
39b04 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
39b05 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
39b06 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65  out;.  }.  aiUse
39b07 64 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c  d = (int *)&aCel
39b08 6c 5b 6e 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65  l[nCell+1];.  me
39b09 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20  mset(aiUsed, 0, 
39b0a 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65  sizeof(int)*(nCe
39b0b 6c 6c 2b 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d  ll+1));.  for(i=
39b0c 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
39b0d 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  {.    nodeGetCel
39b0e 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
39b0f 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a   i, &aCell[i]);.
39b10 20 20 7d 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70    }.  nodeZero(p
39b11 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
39b12 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e   memcpy(&aCell[n
39b13 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69  Cell], pCell, si
39b14 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
39b15 3b 0a 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20  ;.  nCell++;..  
39b16 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
39b17 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 69 67 68  ==1 ){.    pRigh
39b18 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
39b19 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20  ee, pNode, 1);. 
39b1a 20 20 20 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e     pLeft = nodeN
39b1b 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ew(pRtree, pNode
39b1c 2c 20 31 29 3b 0a 20 20 20 20 70 52 74 72 65 65  , 1);.    pRtree
39b1d 2d 3e 69 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20  ->iDepth++;.    
39b1e 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
39b1f 20 31 3b 0a 20 20 20 20 77 72 69 74 65 49 6e 74   1;.    writeInt
39b20 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  16(pNode->zData,
39b21 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 29   pRtree->iDepth)
39b22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
39b23 4c 65 66 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Left = pNode;.  
39b24 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e    pRight = nodeN
39b25 65 77 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ew(pRtree, pLeft
39b26 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20  ->pParent, 1);. 
39b27 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
39b28 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (pLeft);.  }..  
39b29 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20 21 70  if( !pLeft || !p
39b2a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 63 20  Right ){.    rc 
39b2b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
39b2c 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39b2d 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d  de_out;.  }..  m
39b2e 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61  emset(pLeft->zDa
39b2f 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69  ta, 0, pRtree->i
39b30 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d  NodeSize);.  mem
39b31 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44 61 74  set(pRight->zDat
39b32 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  a, 0, pRtree->iN
39b33 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20  odeSize);..  rc 
39b34 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28 70 52  = AssignCells(pR
39b35 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  tree, aCell, nCe
39b36 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  ll, pLeft, pRigh
39b37 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 26 72  t, &leftbbox, &r
39b38 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69 66 28  ightbbox);.  if(
39b39 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
39b3a 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  {.    goto split
39b3b 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
39b3c 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74 68 20   /* Ensure both 
39b3d 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61 76 65  child nodes have
39b3e 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 73   node numbers as
39b3f 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20  signed to them. 
39b40 2a 2f 0a 20 20 69 66 28 20 28 30 3d 3d 70 52 69  */.  if( (0==pRi
39b41 67 68 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51  ght->iNode && SQ
39b42 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e  LITE_OK!=(rc = n
39b43 6f 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c  odeWrite(pRtree,
39b44 20 70 52 69 67 68 74 29 29 29 0a 20 20 20 7c 7c   pRight))).   ||
39b45 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64   (0==pLeft->iNod
39b46 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  e && SQLITE_OK!=
39b47 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65 28  (rc = nodeWrite(
39b48 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 29 29  pRtree, pLeft)))
39b49 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  .  ){.    goto s
39b4a 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39b4b 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78 2e 69  }..  rightbbox.i
39b4c 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d 3e  Rowid = pRight->
39b4d 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f  iNode;.  leftbbo
39b4e 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65 66 74  x.iRowid = pLeft
39b4f 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20  ->iNode;..  if( 
39b50 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
39b51 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74 72 65  ){.    rc = rtre
39b52 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
39b53 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65  ee, pLeft->pPare
39b54 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69  nt, &leftbbox, i
39b55 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20 20 69  Height+1);.    i
39b56 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
39b57 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
39b58 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39b59 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
39b5a 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
39b5b 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61  ent = pLeft->pPa
39b5c 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
39b5d 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
39b5e 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
39b5f 65 66 74 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76  eft);.    nodeOv
39b60 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
39b61 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
39b62 66 74 62 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a  ftbbox, iCell);.
39b63 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70      AdjustTree(p
39b64 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
39b65 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a  &leftbbox);.  }.
39b66 20 20 69 66 28 20 28 72 63 20 3d 20 72 74 72 65    if( (rc = rtre
39b67 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
39b68 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50 61 72  ee, pRight->pPar
39b69 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f 78 2c  ent, &rightbbox,
39b6a 20 69 48 65 69 67 68 74 2b 31 29 29 20 29 7b 0a   iHeight+1)) ){.
39b6b 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39b6c 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66  de_out;.  }..  f
39b6d 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28  or(i=0; i<NCELL(
39b6e 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20  pRight); i++){. 
39b6f 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20     i64 iRowid = 
39b70 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
39b71 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69 29 3b  ree, pRight, i);
39b72 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65  .    rc = update
39b73 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
39b74 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74 2c 20  iRowid, pRight, 
39b75 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 69 66  iHeight);.    if
39b76 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d  ( iRowid==pCell-
39b77 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
39b78 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20   newCellIsRight 
39b79 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
39b7a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
39b7b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
39b7c 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39b7d 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e    }.  }.  if( pN
39b7e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b  ode->iNode==1 ){
39b7f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
39b80 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b  NCELL(pLeft); i+
39b81 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 52  +){.      i64 iR
39b82 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
39b83 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wid(pRtree, pLef
39b84 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 63 20  t, i);.      rc 
39b85 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
39b86 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
39b87 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b  pLeft, iHeight);
39b88 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
39b89 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39b8a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39b8b 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  de_out;.      }.
39b8c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
39b8d 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74  ( newCellIsRight
39b8e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
39b8f 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52  updateMapping(pR
39b90 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
39b91 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65 69  wid, pLeft, iHei
39b92 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ght);.  }..  if(
39b93 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
39b94 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52  {.    rc = nodeR
39b95 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
39b96 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52 69 67  Right);.    pRig
39b97 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ht = 0;.  }.  if
39b98 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39b99 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  ){.    rc = node
39b9a 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39b9b 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c 65 66  pLeft);.    pLef
39b9c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69  t = 0;.  }..spli
39b9d 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64  tnode_out:.  nod
39b9e 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39b9f 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65   pRight);.  node
39ba0 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39ba1 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74 65  pLeft);.  sqlite
39ba2 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20  3_free(aCell);. 
39ba3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
39ba4 74 61 74 69 63 20 69 6e 74 20 66 69 78 4c 65 61  tatic int fixLea
39ba5 66 50 61 72 65 6e 74 28 52 74 72 65 65 20 2a 70  fParent(Rtree *p
39ba6 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39ba7 20 2a 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20   *pLeaf){.  int 
39ba8 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39ba9 20 20 69 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f    if( pLeaf->iNo
39baa 64 65 21 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e  de!=1 && pLeaf->
39bab 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20  pParent==0 ){.  
39bac 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39bad 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65  nt64(pRtree->pRe
39bae 61 64 50 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65  adParent, 1, pLe
39baf 61 66 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  af->iNode);.    
39bb0 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70  if( sqlite3_step
39bb1 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
39bb2 72 65 6e 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  rent)==SQLITE_RO
39bb3 57 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  W ){.      i64 i
39bb4 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  Node = sqlite3_c
39bb5 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72  olumn_int64(pRtr
39bb6 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c  ee->pReadParent,
39bb7 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   0);.      rc = 
39bb8 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
39bb9 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70  ee, iNode, 0, &p
39bba 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  Leaf->pParent);.
39bbb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
39bbc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
39bbd 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  OR;.    }.    sq
39bbe 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39bbf 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29  ee->pReadParent)
39bc0 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
39bc1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
39bc2 20 72 63 20 3d 20 66 69 78 4c 65 61 66 50 61 72   rc = fixLeafPar
39bc3 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ent(pRtree, pLea
39bc4 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
39bc5 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
39bc6 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
39bc7 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
39bc8 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20  ee *, RtreeNode 
39bc9 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73  *, int, int);..s
39bca 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65  tatic int remove
39bcb 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74 72  Node(Rtree *pRtr
39bcc 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
39bcd 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67 68  Node, int iHeigh
39bce 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
39bcf 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
39bd0 6e 74 3b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b  nt;.  int iCell;
39bd1 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64  ..  assert( pNod
39bd2 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20  e->nRef==1 );.. 
39bd3 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65   /* Remove the e
39bd4 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72 65  ntry in the pare
39bd5 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43  nt cell. */.  iC
39bd6 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
39bd7 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e  Index(pRtree, pN
39bd8 6f 64 65 29 3b 0a 20 20 70 50 61 72 65 6e 74 20  ode);.  pParent 
39bd9 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
39bda 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  ;.  pNode->pPare
39bdb 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51  nt = 0;.  if( SQ
39bdc 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64  LITE_OK!=(rc = d
39bdd 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65  eleteCell(pRtree
39bde 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c  , pParent, iCell
39bdf 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 0a 20  , iHeight+1)) . 
39be0 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
39be1 28 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  (rc = nodeReleas
39be2 65 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  e(pRtree, pParen
39be3 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  t)).  ){.    ret
39be4 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
39be5 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78  * Remove the xxx
39be6 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a  _node entry. */.
39be7 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39be8 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65  nt64(pRtree->pDe
39be9 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f  leteNode, 1, pNo
39bea 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71  de->iNode);.  sq
39beb 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
39bec 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b  e->pDeleteNode);
39bed 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
39bee 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  !=(rc = sqlite3_
39bef 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 44  reset(pRtree->pD
39bf0 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20  eleteNode)) ){. 
39bf1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
39bf2 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
39bf3 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 65 6e  he xxx_parent en
39bf4 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  try. */.  sqlite
39bf5 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
39bf6 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
39bf7 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e  nt, 1, pNode->iN
39bf8 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
39bf9 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65  step(pRtree->pDe
39bfa 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 69  leteParent);.  i
39bfb 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
39bfc 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
39bfd 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
39bfe 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20 20 20  eParent)) ){.   
39bff 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
39c00 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74    .  /* Remove t
39c01 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65  he node from the
39c02 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20   in-memory hash 
39c03 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69  table and link i
39c04 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  t into.  ** the 
39c05 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20 6c  Rtree.pDeleted l
39c06 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  ist. Its content
39c07 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73  s will be re-ins
39c08 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a  erted later on..
39c09 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44    */.  nodeHashD
39c0a 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e  elete(pRtree, pN
39c0b 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69  ode);.  pNode->i
39c0c 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74 3b 0a  Node = iHeight;.
39c0d 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d    pNode->pNext =
39c0e 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65   pRtree->pDelete
39c0f 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  d;.  pNode->nRef
39c10 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 70 44  ++;.  pRtree->pD
39c11 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a  eleted = pNode;.
39c12 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
39c13 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _OK;.}..static v
39c14 6f 69 64 20 66 69 78 42 6f 75 6e 64 69 6e 67 42  oid fixBoundingB
39c15 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ox(Rtree *pRtree
39c16 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
39c17 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
39c18 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64   *pParent = pNod
39c19 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69 66  e->pParent;.  if
39c1a 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ( pParent ){.   
39c1b 20 69 6e 74 20 69 69 3b 20 0a 20 20 20 20 69 6e   int ii; .    in
39c1c 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
39c1d 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72 65  pNode);.    Rtre
39c1e 65 43 65 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20  eCell box;      
39c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39c20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e        /* Boundin
39c21 67 20 62 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20  g box for pNode 
39c22 2a 2f 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  */.    nodeGetCe
39c23 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
39c24 2c 20 30 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20  , 0, &box);.    
39c25 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65  for(ii=1; ii<nCe
39c26 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ll; ii++){.     
39c27 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
39c28 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65  .      nodeGetCe
39c29 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
39c2a 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  , ii, &cell);.  
39c2b 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39c2c 74 72 65 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c  tree, &box, &cel
39c2d 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f  l);.    }.    bo
39c2e 78 2e 69 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65  x.iRowid = pNode
39c2f 2d 3e 69 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20  ->iNode;.    ii 
39c30 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
39c31 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
39c32 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72  ;.    nodeOverwr
39c33 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  iteCell(pRtree, 
39c34 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69  pParent, &box, i
39c35 69 29 3b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64  i);.    fixBound
39c36 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70  ingBox(pRtree, p
39c37 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a  Parent);.  }.}..
39c38 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
39c39 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78 20 69   cell at index i
39c3a 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f  Cell of node pNo
39c3b 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f 76 69  de. After removi
39c3c 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20  ng the.** cell, 
39c3d 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74 72 65  adjust the r-tre
39c3e 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
39c3f 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   if required..*/
39c40 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65  .static int dele
39c41 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  teCell(Rtree *pR
39c42 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
39c43 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c  *pNode, int iCel
39c44 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  l, int iHeight){
39c45 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66  .  int rc;..  if
39c46 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
39c47 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
39c48 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 29  (pRtree, pNode))
39c49 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
39c4a 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  c;.  }..  /* Rem
39c4b 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 66 72 6f  ove the cell fro
39c4c 6d 20 74 68 65 20 6e 6f 64 65 2e 20 54 68 69 73  m the node. This
39c4d 20 63 61 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73   call just moves
39c4e 20 62 79 74 65 73 20 61 72 6f 75 6e 64 0a 20 20   bytes around.  
39c4f 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  ** the in-memory
39c50 20 6e 6f 64 65 20 69 6d 61 67 65 2c 20 73 6f 20   node image, so 
39c51 69 74 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a  it cannot fail..
39c52 20 20 2a 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74    */.  nodeDelet
39c53 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  eCell(pRtree, pN
39c54 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20  ode, iCell);..  
39c55 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69  /* If the node i
39c56 73 20 6e 6f 74 20 74 68 65 20 74 72 65 65 20 72  s not the tree r
39c57 6f 6f 74 20 61 6e 64 20 6e 6f 77 20 68 61 73 20  oot and now has 
39c58 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6d 69  less than the mi
39c59 6e 69 6d 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65  nimum.  ** numbe
39c5a 72 20 6f 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f  r of cells, remo
39c5b 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20 74  ve it from the t
39c5c 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ree. Otherwise, 
39c5d 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a 2a 20  update the.  ** 
39c5e 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
39c5f 6e 74 20 6e 6f 64 65 20 73 6f 20 74 68 61 74 20  nt node so that 
39c60 69 74 20 74 69 67 68 74 6c 79 20 63 6f 6e 74 61  it tightly conta
39c61 69 6e 73 20 74 68 65 20 75 70 64 61 74 65 64 0a  ins the updated.
39c62 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a    ** node..  */.
39c63 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
39c64 64 65 21 3d 31 20 29 7b 0a 20 20 20 20 52 74 72  de!=1 ){.    Rtr
39c65 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20  eeNode *pParent 
39c66 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
39c67 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 65  ;.    if( (pPare
39c68 6e 74 2d 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20  nt->iNode!=1 || 
39c69 4e 43 45 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d  NCELL(pParent)!=
39c6a 31 29 20 0a 20 20 20 20 20 26 26 20 28 4e 43 45  1) .     && (NCE
39c6b 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f  LL(pNode)<RTREE_
39c6c 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
39c6d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
39c6e 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70  c = removeNode(p
39c6f 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48  Rtree, pNode, iH
39c70 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
39c71 65 7b 0a 20 20 20 20 20 20 66 69 78 42 6f 75 6e  e{.      fixBoun
39c72 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20  dingBox(pRtree, 
39c73 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pNode);.    }.  
39c74 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
39c75 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 52 65  }..static int Re
39c76 69 6e 73 65 72 74 28 0a 20 20 52 74 72 65 65 20  insert(.  Rtree 
39c77 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
39c78 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
39c79 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39c7a 6c 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67 68  l, .  int iHeigh
39c7b 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64  t.){.  int *aOrd
39c7c 65 72 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  er;.  int *aSpar
39c7d 65 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e;.  RtreeCell *
39c7e 61 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a  aCell;.  float *
39c7f 61 44 69 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74  aDistance;.  int
39c80 20 6e 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20   nCell;.  float 
39c81 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52  aCenterCoord[RTR
39c82 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e  EE_MAX_DIMENSION
39c83 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a  S];.  int iDim;.
39c84 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
39c85 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39c86 0a 20 20 6d 65 6d 73 65 74 28 61 43 65 6e 74 65  .  memset(aCente
39c87 72 43 6f 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f  rCoord, 0, sizeo
39c88 66 28 66 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d  f(float)*RTREE_M
39c89 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a  AX_DIMENSIONS);.
39c8a 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  .  nCell = NCELL
39c8b 28 70 4e 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a  (pNode)+1;..  /*
39c8c 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 62 75   Allocate the bu
39c8d 66 66 65 72 73 20 75 73 65 64 20 62 79 20 74 68  ffers used by th
39c8e 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68  is operation. Th
39c8f 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a  e allocation is.
39c90 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73 68 65    ** relinquishe
39c91 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  d before this fu
39c92 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
39c93 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28    */.  aCell = (
39c94 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69  RtreeCell *)sqli
39c95 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c  te3_malloc(nCell
39c96 20 2a 20 28 0a 20 20 20 20 73 69 7a 65 6f 66 28   * (.    sizeof(
39c97 52 74 72 65 65 43 65 6c 6c 29 20 2b 20 20 20 20  RtreeCell) +    
39c98 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72       /* aCell ar
39c99 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
39c9a 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
39c9b 20 20 20 20 20 20 20 2f 2a 20 61 4f 72 64 65 72         /* aOrder
39c9c 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
39c9d 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
39c9e 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70  +         /* aSp
39c9f 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  are array */.   
39ca0 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20   sizeof(float)  
39ca1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39ca2 61 44 69 73 74 61 6e 63 65 20 61 72 72 61 79 20  aDistance array 
39ca3 2a 2f 0a 20 20 29 29 3b 0a 20 20 69 66 28 20 21  */.  ));.  if( !
39ca4 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  aCell ){.    ret
39ca5 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
39ca6 3b 0a 20 20 7d 0a 20 20 61 4f 72 64 65 72 20 20  ;.  }.  aOrder  
39ca7 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c    = (int *)&aCel
39ca8 6c 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61  l[nCell];.  aSpa
39ca9 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26  re    = (int *)&
39caa 61 4f 72 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20  aOrder[nCell];. 
39cab 20 61 44 69 73 74 61 6e 63 65 20 3d 20 28 66 6c   aDistance = (fl
39cac 6f 61 74 20 2a 29 26 61 53 70 61 72 65 5b 6e 43  oat *)&aSpare[nC
39cad 65 6c 6c 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ell];..  for(ii=
39cae 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  0; ii<nCell; ii+
39caf 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  +){.    if( ii==
39cb0 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20  (nCell-1) ){.   
39cb1 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c     memcpy(&aCell
39cb2 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a  [ii], pCell, siz
39cb3 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
39cb4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
39cb5 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
39cb6 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
39cb7 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20   &aCell[ii]);.  
39cb8 20 20 7d 0a 20 20 20 20 61 4f 72 64 65 72 5b 69    }.    aOrder[i
39cb9 69 5d 20 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72  i] = ii;.    for
39cba 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52  (iDim=0; iDim<pR
39cbb 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d  tree->nDim; iDim
39cbc 2b 2b 29 7b 0a 20 20 20 20 20 20 61 43 65 6e 74  ++){.      aCent
39cbd 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d  erCoord[iDim] +=
39cbe 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69   DCOORD(aCell[ii
39cbf 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
39cc0 29 3b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72  );.      aCenter
39cc1 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44  Coord[iDim] += D
39cc2 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
39cc3 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
39cc4 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
39cc5 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c  or(iDim=0; iDim<
39cc6 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44  pRtree->nDim; iD
39cc7 69 6d 2b 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74  im++){.    aCent
39cc8 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20  erCoord[iDim] = 
39cc9 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69  aCenterCoord[iDi
39cca 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c  m]/((float)nCell
39ccb 2a 32 2e 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f  *2.0);.  }..  fo
39ccc 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
39ccd 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69  ; ii++){.    aDi
39cce 73 74 61 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30  stance[ii] = 0.0
39ccf 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d 30  ;.    for(iDim=0
39cd0 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e  ; iDim<pRtree->n
39cd1 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20  Dim; iDim++){.  
39cd2 20 20 20 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20      float coord 
39cd3 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  = DCOORD(aCell[i
39cd4 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
39cd5 2b 31 5d 29 20 2d 20 0a 20 20 20 20 20 20 20 20  +1]) - .        
39cd6 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69    DCOORD(aCell[i
39cd7 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
39cd8 5d 29 3b 0a 20 20 20 20 20 20 61 44 69 73 74 61  ]);.      aDista
39cd9 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72  nce[ii] += (coor
39cda 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69  d-aCenterCoord[i
39cdb 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65  Dim])*(coord-aCe
39cdc 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29  nterCoord[iDim])
39cdd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53  ;.    }.  }..  S
39cde 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4f  ortByDistance(aO
39cdf 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69  rder, nCell, aDi
39ce0 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b  stance, aSpare);
39ce1 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
39ce2 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66  ee, pNode);..  f
39ce3 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
39ce4 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43  ITE_OK && ii<(nC
39ce5 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45  ell-(RTREE_MINCE
39ce6 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29 29 3b  LLS(pRtree)+1));
39ce7 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
39ce8 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c  eCell *p = &aCel
39ce9 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20  l[aOrder[ii]];. 
39cea 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
39ceb 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
39cec 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e   p);.    if( p->
39ced 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69  iRowid==pCell->i
39cee 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69  Rowid ){.      i
39cef 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
39cf0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f  .        rc = ro
39cf1 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
39cf2 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64   p->iRowid, pNod
39cf3 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
39cf4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
39cf5 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65  rc = parentWrite
39cf6 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77  (pRtree, p->iRow
39cf7 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  id, pNode->iNode
39cf8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
39cf9 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
39cfa 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39cfb 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70  fixBoundingBox(p
39cfc 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
39cfd 20 7d 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53   }.  for(; rc==S
39cfe 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e  QLITE_OK && ii<n
39cff 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
39d00 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
39d01 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
39d02 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
39d03 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
39d04 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
39d05 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
39d06 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
39d07 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
39d08 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   */.    RtreeNod
39d09 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20  e *pInsert;.    
39d0a 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26  RtreeCell *p = &
39d0b 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d  aCell[aOrder[ii]
39d0c 5d 3b 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f  ];.    rc = Choo
39d0d 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20 70  seLeaf(pRtree, p
39d0e 2c 20 69 48 65 69 67 68 74 2c 20 26 70 49 6e 73  , iHeight, &pIns
39d0f 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ert);.    if( rc
39d10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39d11 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
39d12 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
39d13 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39d14 20 70 49 6e 73 65 72 74 2c 20 70 2c 20 69 48 65   pInsert, p, iHe
39d15 69 67 68 74 29 3b 0a 20 20 20 20 20 20 72 63 32  ight);.      rc2
39d16 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
39d17 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
39d18 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
39d19 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39d1a 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
39d1b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
39d1c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
39d1d 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  aCell);.  return
39d1e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
39d1f 73 65 72 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20  sert cell pCell 
39d20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
39d21 20 4e 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 74   Node pNode is t
39d22 68 65 20 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a  he head of a .**
39d23 20 73 75 62 74 72 65 65 20 69 48 65 69 67 68 74   subtree iHeight
39d24 20 68 69 67 68 20 28 6c 65 61 66 20 6e 6f 64 65   high (leaf node
39d25 73 20 68 61 76 65 20 69 48 65 69 67 68 74 3d 3d  s have iHeight==
39d26 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0)..*/.static in
39d27 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
39d28 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
39d29 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ee,.  RtreeNode 
39d2a 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65 43  *pNode,.  RtreeC
39d2b 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e  ell *pCell,.  in
39d2c 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
39d2d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39d2e 4b 3b 0a 20 20 69 66 28 20 69 48 65 69 67 68 74  K;.  if( iHeight
39d2f 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  >0 ){.    RtreeN
39d30 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f  ode *pChild = no
39d31 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
39d32 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77  ree, pCell->iRow
39d33 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
39d34 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
39d35 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39d36 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
39d37 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
39d38 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
39d39 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
39d3a 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
39d3b 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f    }.  }.  if( no
39d3c 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39d3d 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
39d3e 6c 29 20 29 7b 0a 23 69 66 20 56 41 52 49 41 4e  l) ){.#if VARIAN
39d3f 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
39d40 53 45 52 54 0a 20 20 20 20 69 66 28 20 69 48 65  SERT.    if( iHe
39d41 69 67 68 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52  ight<=pRtree->iR
39d42 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 7c 7c  einsertHeight ||
39d43 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
39d44 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 70  ){.      rc = Sp
39d45 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  litNode(pRtree, 
39d46 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48  pNode, pCell, iH
39d47 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
39d48 65 7b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d  e{.      pRtree-
39d49 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74  >iReinsertHeight
39d4a 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 20 20   = iHeight;.    
39d4b 20 20 72 63 20 3d 20 52 65 69 6e 73 65 72 74 28    rc = Reinsert(
39d4c 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
39d4d 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a  Cell, iHeight);.
39d4e 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
39d4f 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70  rc = SplitNode(p
39d50 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43  Rtree, pNode, pC
39d51 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 23  ell, iHeight);.#
39d52 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20  endif.  }else{. 
39d53 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70 52     AdjustTree(pR
39d54 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
39d55 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 48 65  ll);.    if( iHe
39d56 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ight==0 ){.     
39d57 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65   rc = rowidWrite
39d58 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e  (pRtree, pCell->
39d59 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
39d5a 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
39d5b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 72  {.      rc = par
39d5c 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65 2c  entWrite(pRtree,
39d5d 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
39d5e 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
39d5f 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
39d60 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
39d61 69 6e 74 20 72 65 69 6e 73 65 72 74 4e 6f 64 65  int reinsertNode
39d62 43 6f 6e 74 65 6e 74 28 52 74 72 65 65 20 2a 70  Content(Rtree *p
39d63 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39d64 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
39d65 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ii;.  int rc = S
39d66 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
39d67 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
39d68 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ode);..  for(ii=
39d69 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
39d6a 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69   && ii<nCell; ii
39d6b 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f  ++){.    RtreeNo
39d6c 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20  de *pInsert;.   
39d6d 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
39d6e 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
39d6f 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
39d70 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20  ii, &cell);..   
39d71 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
39d72 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
39d73 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
39d74 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
39d75 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
39d76 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
39d77 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
39d78 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
39d79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 43 68 6f   */.    rc = Cho
39d7a 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20  oseLeaf(pRtree, 
39d7b 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e  &cell, pNode->iN
39d7c 6f 64 65 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a  ode, &pInsert);.
39d7d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
39d7e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
39d7f 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63  nt rc2;.      rc
39d80 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
39d81 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65  ll(pRtree, pInse
39d82 72 74 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65  rt, &cell, pNode
39d83 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  ->iNode);.      
39d84 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  rc2 = nodeReleas
39d85 65 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72  e(pRtree, pInser
39d86 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
39d87 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39d88 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
39d89 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39d8a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
39d8b 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20  }../*.** Select 
39d8c 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73  a currently unus
39d8d 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61 20 6e  ed rowid for a n
39d8e 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f 72 64  ew r-tree record
39d8f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39d90 6e 65 77 52 6f 77 69 64 28 52 74 72 65 65 20 2a  newRowid(Rtree *
39d91 70 52 74 72 65 65 2c 20 69 36 34 20 2a 70 69 52  pRtree, i64 *piR
39d92 6f 77 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b  owid){.  int rc;
39d93 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
39d94 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72  null(pRtree->pWr
39d95 69 74 65 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20  iteRowid, 1);.  
39d96 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
39d97 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
39d98 52 6f 77 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c  Rowid, 2);.  sql
39d99 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39d9a 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
39d9b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
39d9c 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72  eset(pRtree->pWr
39d9d 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69  iteRowid);.  *pi
39d9e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Rowid = sqlite3_
39d9f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
39da0 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20  d(pRtree->db);. 
39da1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
39da2 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
39da3 61 74 69 63 20 69 6e 74 20 68 61 73 68 49 73 45  atic int hashIsE
39da4 6d 70 74 79 28 52 74 72 65 65 20 2a 70 52 74 72  mpty(Rtree *pRtr
39da5 65 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ee){.  int ii;. 
39da6 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 48 41   for(ii=0; ii<HA
39da7 53 48 53 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20  SHSIZE; ii++){. 
39da8 20 20 20 61 73 73 65 72 74 28 20 21 70 52 74 72     assert( !pRtr
39da9 65 65 2d 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b  ee->aHash[ii] );
39daa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
39dab 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
39dac 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74   The xUpdate met
39dad 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f  hod for rtree mo
39dae 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62  dule virtual tab
39daf 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  les..*/.static i
39db0 6e 74 20 72 74 72 65 65 55 70 64 61 74 65 28 0a  nt rtreeUpdate(.
39db1 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
39db2 70 56 74 61 62 2c 20 0a 20 20 69 6e 74 20 6e 44  pVtab, .  int nD
39db3 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  ata, .  sqlite3_
39db4 76 61 6c 75 65 20 2a 2a 61 7a 44 61 74 61 2c 20  value **azData, 
39db5 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
39db6 2a 70 52 6f 77 69 64 0a 29 7b 0a 20 20 52 74 72  *pRowid.){.  Rtr
39db7 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
39db8 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69  ree *)pVtab;.  i
39db9 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39dba 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72  K;..  rtreeRefer
39dbb 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20  ence(pRtree);.. 
39dbc 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d 31   assert(nData>=1
39dbd 29 3b 0a 20 20 61 73 73 65 72 74 28 68 61 73 68  );.  assert(hash
39dbe 49 73 45 6d 70 74 79 28 70 52 74 72 65 65 29 29  IsEmpty(pRtree))
39dbf 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61 74  ;..  /* If azDat
39dc0 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20 53  a[0] is not an S
39dc1 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 69  QL NULL value, i
39dc2 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  t is the rowid o
39dc3 66 20 61 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  f a.  ** record 
39dc4 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 74  to delete from t
39dc5 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65 2e  he r-tree table.
39dc6 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
39dc7 6c 6f 63 6b 20 64 6f 65 73 0a 20 20 2a 2a 20 6a  lock does.  ** j
39dc8 75 73 74 20 74 68 61 74 2e 0a 20 20 2a 2f 0a 20  ust that..  */. 
39dc9 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
39dca 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 30  ue_type(azData[0
39dcb 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
39dcc 29 7b 0a 20 20 20 20 69 36 34 20 69 44 65 6c 65  ){.    i64 iDele
39dcd 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
39dce 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
39dcf 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20  to delete */.   
39dd0 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61   RtreeNode *pLea
39dd1 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
39dd2 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69  Leaf node contai
39dd3 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65 6c  ning record iDel
39dd4 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ete */.    int i
39dd5 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
39dd6 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
39dd7 6f 66 20 69 44 65 6c 65 74 65 20 63 65 6c 6c 20  of iDelete cell 
39dd8 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20 20 20 20  in pLeaf */.    
39dd9 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
39dda 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e  ;..    /* Obtain
39ddb 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
39ddc 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74 6f  the root node to
39ddd 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72 65   initialise Rtre
39dde 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 20 20  e.iDepth */.    
39ddf 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39de0 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
39de1 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  pRoot);..    /* 
39de2 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
39de3 63 65 20 74 6f 20 74 68 65 20 6c 65 61 66 20 6e  ce to the leaf n
39de4 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ode that contain
39de5 73 20 74 68 65 20 65 6e 74 72 79 20 0a 20 20 20  s the entry .   
39de6 20 2a 2a 20 61 62 6f 75 74 20 74 6f 20 62 65 20   ** about to be 
39de7 64 65 6c 65 74 65 64 2e 20 0a 20 20 20 20 2a 2f  deleted. .    */
39de8 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
39de9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39dea 69 44 65 6c 65 74 65 20 3d 20 73 71 6c 69 74 65  iDelete = sqlite
39deb 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a  3_value_int64(az
39dec 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20  Data[0]);.      
39ded 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64  rc = findLeafNod
39dee 65 28 70 52 74 72 65 65 2c 20 69 44 65 6c 65 74  e(pRtree, iDelet
39def 65 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  e, &pLeaf);.    
39df0 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
39df1 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75 65   the cell in que
39df2 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6c  stion from the l
39df3 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20  eaf node. */.   
39df4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39df5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
39df6 72 63 32 3b 0a 20 20 20 20 20 20 69 43 65 6c 6c  rc2;.      iCell
39df7 20 3d 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65   = nodeRowidInde
39df8 78 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  x(pRtree, pLeaf,
39df9 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20   iDelete);.     
39dfa 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c   rc = deleteCell
39dfb 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20  (pRtree, pLeaf, 
39dfc 69 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  iCell, 0);.     
39dfd 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
39dfe 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  se(pRtree, pLeaf
39dff 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
39e00 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
39e01 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
39e02 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
39e03 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65     /* Delete the
39e04 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
39e05 6e 74 72 79 20 69 6e 20 74 68 65 20 3c 72 74 72  ntry in the <rtr
39e06 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
39e07 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
39e08 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
39e09 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
39e0a 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
39e0b 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20 69  eleteRowid, 1, i
39e0c 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 73  Delete);.      s
39e0d 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
39e0e 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39e0f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
39e10 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39e11 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39e12 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
39e13 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f   Check if the ro
39e14 6f 74 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73 20  ot node now has 
39e15 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 69 6c  exactly one chil
39e16 64 2e 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65  d. If so, remove
39e17 0a 20 20 20 20 2a 2a 20 69 74 2c 20 73 63 68 65  .    ** it, sche
39e18 64 75 6c 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  dule the content
39e19 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 66  s of the child f
39e1a 6f 72 20 72 65 69 6e 73 65 72 74 69 6f 6e 20 61  or reinsertion a
39e1b 6e 64 20 0a 20 20 20 20 2a 2a 20 72 65 64 75 63  nd .    ** reduc
39e1c 65 20 74 68 65 20 74 72 65 65 20 68 65 69 67 68  e the tree heigh
39e1d 74 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a  t by one..    **
39e1e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20  .    ** This is 
39e1f 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63 6f  equivalent to co
39e20 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  pying the conten
39e21 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ts of the child 
39e22 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  into.    ** the 
39e23 72 6f 6f 74 20 6e 6f 64 65 20 28 74 68 65 20 6f  root node (the o
39e24 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 47 75  peration that Gu
39e25 74 6d 61 6e 27 73 20 70 61 70 65 72 20 73 61 79  tman's paper say
39e26 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 0a 20 20  s to perform .  
39e27 20 20 2a 2a 20 69 6e 20 74 68 69 73 20 73 63 65    ** in this sce
39e28 6e 61 72 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a 20  nario)..    */. 
39e29 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
39e2a 45 5f 4f 4b 20 26 26 20 70 52 74 72 65 65 2d 3e  E_OK && pRtree->
39e2b 69 44 65 70 74 68 3e 30 20 29 7b 0a 20 20 20 20  iDepth>0 ){.    
39e2c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39e2d 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c 28 70 52 6f  _OK && NCELL(pRo
39e2e 6f 74 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ot)==1 ){.      
39e2f 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
39e30 69 6c 64 3b 0a 20 20 20 20 20 20 20 20 69 36 34  ild;.        i64
39e31 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47 65   iChild = nodeGe
39e32 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
39e33 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Root, 0);.      
39e34 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
39e35 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69 6c  re(pRtree, iChil
39e36 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c  d, pRoot, &pChil
39e37 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
39e38 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
39e39 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
39e3a 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72 65  removeNode(pRtre
39e3b 65 2c 20 70 43 68 69 6c 64 2c 20 70 52 74 72 65  e, pChild, pRtre
39e3c 65 2d 3e 69 44 65 70 74 68 2d 31 29 3b 0a 20 20  e->iDepth-1);.  
39e3d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
39e3e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
39e3f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  K ){.          p
39e40 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 2d 3b  Rtree->iDepth--;
39e41 0a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 65  .          write
39e42 49 6e 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44 61  Int16(pRoot->zDa
39e43 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70  ta, pRtree->iDep
39e44 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  th);.          p
39e45 52 6f 6f 74 2d 3e 69 73 44 69 72 74 79 20 3d 20  Root->isDirty = 
39e46 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
39e47 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
39e48 2f 2a 20 52 65 2d 69 6e 73 65 72 74 20 74 68 65  /* Re-insert the
39e49 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e 79   contents of any
39e4a 20 75 6e 64 65 72 66 75 6c 6c 20 6e 6f 64 65 73   underfull nodes
39e4b 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
39e4c 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20 66  e tree. */.    f
39e4d 6f 72 28 70 4c 65 61 66 3d 70 52 74 72 65 65 2d  or(pLeaf=pRtree-
39e4e 3e 70 44 65 6c 65 74 65 64 3b 20 70 4c 65 61 66  >pDeleted; pLeaf
39e4f 3b 20 70 4c 65 61 66 3d 70 52 74 72 65 65 2d 3e  ; pLeaf=pRtree->
39e50 70 44 65 6c 65 74 65 64 29 7b 0a 20 20 20 20 20  pDeleted){.     
39e51 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39e52 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
39e53 20 3d 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43   = reinsertNodeC
39e54 6f 6e 74 65 6e 74 28 70 52 74 72 65 65 2c 20 70  ontent(pRtree, p
39e55 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Leaf);.      }. 
39e56 20 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44 65       pRtree->pDe
39e57 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e 70  leted = pLeaf->p
39e58 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  Next;.      sqli
39e59 74 65 33 5f 66 72 65 65 28 70 4c 65 61 66 29 3b  te3_free(pLeaf);
39e5a 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
39e5b 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72  elease the refer
39e5c 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74  ence to the root
39e5d 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66   node. */.    if
39e5e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39e5f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  ){.      rc = no
39e60 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
39e61 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 65  , pRoot);.    }e
39e62 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52  lse{.      nodeR
39e63 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
39e64 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Root);.    }.  }
39e65 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 7a  ..  /* If the az
39e66 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f 6e  Data[] array con
39e67 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20  tains more than 
39e68 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c 65  one element, ele
39e69 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44 61  ments.  ** (azDa
39e6a 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61 72  ta[2]..azData[ar
39e6b 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20 61  gc-1]) contain a
39e6c 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20 69   new record to i
39e6d 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a 20  nsert into.  ** 
39e6e 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
39e6f 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ture..  */.  if(
39e70 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
39e71 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20 20  & nData>1 ){.   
39e72 20 2f 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77   /* Insert a new
39e73 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65   record into the
39e74 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20 52   r-tree */.    R
39e75 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
39e76 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 52     int ii;.    R
39e77 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b  treeNode *pLeaf;
39e78 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
39e79 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f 72  e the cell.aCoor
39e7a 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20 66  d[] array. The f
39e7b 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20  irst coordinate 
39e7c 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a 2f  is azData[3]. */
39e7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 61  .    assert( nDa
39e7e 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e 44 69  ta==(pRtree->nDi
39e7f 6d 2a 32 20 2b 20 33 29 20 29 3b 0a 20 20 20 20  m*2 + 3) );.    
39e80 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
39e81 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
39e82 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
39e83 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
39e84 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39e85 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
39e86 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
39e87 69 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71  i].f = (float)sq
39e88 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
39e89 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29  le(azData[ii+3])
39e8a 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61  ;.        cell.a
39e8b 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20  Coord[ii+1].f = 
39e8c 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f 76  (float)sqlite3_v
39e8d 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61  alue_double(azDa
39e8e 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20  ta[ii+4]);.     
39e8f 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f     if( cell.aCoo
39e90 72 64 5b 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61 43  rd[ii].f>cell.aC
39e91 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 29 7b 0a  oord[ii+1].f ){.
39e92 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
39e93 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
39e94 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
39e95 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20   constraint;.   
39e96 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
39e97 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39e98 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
39e99 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
39e9a 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 63  i+=2){.        c
39e9b 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
39e9c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
39e9d 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 33  _int(azData[ii+3
39e9e 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c  ]);.        cell
39e9f 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20  .aCoord[ii+1].i 
39ea0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39ea1 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d  int(azData[ii+4]
39ea2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  );.        if( c
39ea3 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
39ea4 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b  >cell.aCoord[ii+
39ea5 31 5d 2e 69 20 29 7b 0a 20 20 20 20 20 20 20 20  1].i ){.        
39ea6 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
39ea7 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20  NSTRAINT;.      
39ea8 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61      goto constra
39ea9 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  int;.        }. 
39eaa 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
39eab 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
39eac 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
39ead 20 6e 65 77 20 72 6f 77 2e 20 2a 2f 0a 20 20 20   new row. */.   
39eae 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
39eaf 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 32  ue_type(azData[2
39eb0 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
39eb1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65  ){.      rc = ne
39eb2 77 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 26  wRowid(pRtree, &
39eb3 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20  cell.iRowid);.  
39eb4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
39eb5 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71 6c  ell.iRowid = sql
39eb6 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
39eb7 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20 20  (azData[2]);.   
39eb8 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
39eb9 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
39eba 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 63 65 6c  eadRowid, 1, cel
39ebb 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  l.iRowid);.     
39ebc 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
39ebd 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52  =sqlite3_step(pR
39ebe 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
39ebf 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
39ec0 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
39ec1 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
39ec2 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
39ec3 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
39ec4 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e          goto con
39ec5 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 7d  straint;.      }
39ec6 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
39ec7 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
39ec8 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
39ec9 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
39eca 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39ecb 20 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65       rc = Choose
39ecc 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
39ecd 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66 29 3b 0a  ll, 0, &pLeaf);.
39ece 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
39ecf 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39ed0 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
39ed1 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
39ed2 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 2d 31  nsertHeight = -1
39ed3 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74 72  ;.      rc = rtr
39ed4 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
39ed5 72 65 65 2c 20 70 4c 65 61 66 2c 20 26 63 65 6c  ree, pLeaf, &cel
39ed6 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  l, 0);.      rc2
39ed7 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
39ed8 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20  Rtree, pLeaf);. 
39ed9 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
39eda 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39edb 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
39edc 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 63    }.    }.  }..c
39edd 6f 6e 73 74 72 61 69 6e 74 3a 0a 20 20 72 74 72  onstraint:.  rtr
39ede 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
39edf 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
39ee0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 65  }../*.** The xRe
39ee1 6e 61 6d 65 20 6d 65 74 68 6f 64 20 66 6f 72 20  name method for 
39ee2 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72  rtree module vir
39ee3 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
39ee4 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
39ee5 52 65 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76  Rename(sqlite3_v
39ee6 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73  tab *pVtab, cons
39ee7 74 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65  t char *zNewName
39ee8 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
39ee9 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
39eea 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
39eeb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
39eec 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
39eed 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
39eee 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
39eef 51 2e 27 25 71 5f 6e 6f 64 65 27 20 20 20 52 45  Q.'%q_node'   RE
39ef0 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 6e 6f 64  NAME TO \"%w_nod
39ef1 65 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  e\";".    "ALTER
39ef2 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 70 61   TABLE %Q.'%q_pa
39ef3 72 65 6e 74 27 20 52 45 4e 41 4d 45 20 54 4f 20  rent' RENAME TO 
39ef4 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 3b 22 0a  \"%w_parent\";".
39ef5 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45      "ALTER TABLE
39ef6 20 25 51 2e 27 25 71 5f 72 6f 77 69 64 27 20 20   %Q.'%q_rowid'  
39ef7 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 72  RENAME TO \"%w_r
39ef8 6f 77 69 64 5c 22 3b 22 0a 20 20 20 20 2c 20 70  owid\";".    , p
39ef9 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72  Rtree->zDb, pRtr
39efa 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e  ee->zName, zNewN
39efb 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72 65  ame .    , pRtre
39efc 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
39efd 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20  zName, zNewName 
39efe 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a  .    , pRtree->z
39eff 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
39f00 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a 20 20 29 3b  e, zNewName.  );
39f01 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
39f02 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
39f03 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62 2c  exec(pRtree->db,
39f04 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSql, 0, 0, 0);
39f05 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
39f06 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72  e(zSql);.  }.  r
39f07 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
39f08 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  tic sqlite3_modu
39f09 6c 65 20 72 74 72 65 65 4d 6f 64 75 6c 65 20 3d  le rtreeModule =
39f0a 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20   {.  0,         
39f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f0c 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
39f0d 20 72 74 72 65 65 43 72 65 61 74 65 2c 20 20 20   rtreeCreate,   
39f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39f0f 78 43 72 65 61 74 65 20 2d 20 63 72 65 61 74 65  xCreate - create
39f10 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74   a table */.  rt
39f11 72 65 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20 20  reeConnect,     
39f12 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f            /* xCo
39f13 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74 20  nnect - connect 
39f14 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  to an existing t
39f15 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 42  able */.  rtreeB
39f16 65 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20 20  estIndex,       
39f17 20 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49 6e        /* xBestIn
39f18 64 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65 20  dex - Determine 
39f19 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20  search strategy 
39f1a 2a 2f 0a 20 20 72 74 72 65 65 44 69 73 63 6f 6e  */.  rtreeDiscon
39f1b 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  nect,           
39f1c 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20   /* xDisconnect 
39f1d 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 6f  - Disconnect fro
39f1e 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  m a table */.  r
39f1f 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20 20  treeDestroy,    
39f20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
39f21 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61 20  estroy - Drop a 
39f22 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
39f23 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
39f24 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
39f25 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72 20  - open a cursor 
39f26 2a 2f 0a 20 20 72 74 72 65 65 43 6c 6f 73 65 2c  */.  rtreeClose,
39f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f28 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20 63 6c 6f   /* xClose - clo
39f29 73 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20  se a cursor */. 
39f2a 20 72 74 72 65 65 46 69 6c 74 65 72 2c 20 20 20   rtreeFilter,   
39f2b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39f2c 78 46 69 6c 74 65 72 20 2d 20 63 6f 6e 66 69 67  xFilter - config
39f2d 75 72 65 20 73 63 61 6e 20 63 6f 6e 73 74 72 61  ure scan constra
39f2e 69 6e 74 73 20 2a 2f 0a 20 20 72 74 72 65 65 4e  ints */.  rtreeN
39f2f 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ext,            
39f30 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 2d        /* xNext -
39f31 20 61 64 76 61 6e 63 65 20 61 20 63 75 72 73 6f   advance a curso
39f32 72 20 2a 2f 0a 20 20 72 74 72 65 65 45 6f 66 2c  r */.  rtreeEof,
39f33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f34 20 20 20 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20 20     /* xEof */.  
39f35 72 74 72 65 65 43 6f 6c 75 6d 6e 2c 20 20 20 20  rtreeColumn,    
39f36 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
39f37 43 6f 6c 75 6d 6e 20 2d 20 72 65 61 64 20 64 61  Column - read da
39f38 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 52 6f 77  ta */.  rtreeRow
39f39 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
39f3a 20 20 20 20 2f 2a 20 78 52 6f 77 69 64 20 2d 20      /* xRowid - 
39f3b 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72  read data */.  r
39f3c 74 72 65 65 55 70 64 61 74 65 2c 20 20 20 20 20  treeUpdate,     
39f3d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
39f3e 70 64 61 74 65 20 2d 20 77 72 69 74 65 20 64 61  pdate - write da
39f3f 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ta */.  0,      
39f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f41 20 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 2d 20      /* xBegin - 
39f42 62 65 67 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  begin transactio
39f43 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  n */.  0,       
39f44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f45 20 20 20 2f 2a 20 78 53 79 6e 63 20 2d 20 73 79     /* xSync - sy
39f46 6e 63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  nc transaction *
39f47 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
39f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f49 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d 20 63 6f 6d  /* xCommit - com
39f4a 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  mit transaction 
39f4b 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
39f4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f4d 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 2d 20   /* xRollback - 
39f4e 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63  rollback transac
39f4f 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
39f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f51 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64 46 75        /* xFindFu
39f52 6e 63 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69 6f  nction - functio
39f53 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a 2f  n overloading */
39f54 0a 20 20 72 74 72 65 65 52 65 6e 61 6d 65 20 20  .  rtreeRename  
39f55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39f56 2a 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e 61  * xRename - rena
39f57 6d 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  me the table */.
39f58 7d 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  };..static int r
39f59 74 72 65 65 53 71 6c 49 6e 69 74 28 0a 20 20 52  treeSqlInit(.  R
39f5a 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39f5b 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
39f5c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
39f5d 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
39f5e 2a 7a 50 72 65 66 69 78 2c 20 0a 20 20 69 6e 74  *zPrefix, .  int
39f5f 20 69 73 43 72 65 61 74 65 0a 29 7b 0a 20 20 69   isCreate.){.  i
39f60 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39f61 4b 3b 0a 0a 20 20 23 64 65 66 69 6e 65 20 4e 5f  K;..  #define N_
39f62 53 54 41 54 45 4d 45 4e 54 20 39 0a 20 20 73 74  STATEMENT 9.  st
39f63 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
39f64 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41 54 45 4d 45  *azSql[N_STATEME
39f65 4e 54 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 52  NT] = {.    /* R
39f66 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
39f67 65 20 78 78 78 5f 6e 6f 64 65 20 74 61 62 6c 65  e xxx_node table
39f68 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54 20   */.    "SELECT 
39f69 64 61 74 61 20 46 52 4f 4d 20 27 25 71 27 2e 27  data FROM '%q'.'
39f6a 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
39f6b 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
39f6c 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
39f6d 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
39f6e 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 3a  q_node' VALUES(:
39f6f 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
39f70 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
39f71 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
39f72 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20 20  odeno = :1",..  
39f73 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
39f74 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77 69  ite the xxx_rowi
39f75 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22  d table */.    "
39f76 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20 46 52  SELECT nodeno FR
39f77 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  OM '%q'.'%q_rowi
39f78 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  d' WHERE rowid =
39f79 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45 52   :1",.    "INSER
39f7a 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54  T OR REPLACE INT
39f7b 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69 64  O '%q'.'%q_rowid
39f7c 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29  ' VALUES(:1, :2)
39f7d 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46  ",.    "DELETE F
39f7e 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77  ROM '%q'.'%q_row
39f7f 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  id' WHERE rowid 
39f80 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20 52  = :1",..    /* R
39f81 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
39f82 65 20 78 78 78 5f 70 61 72 65 6e 74 20 74 61 62  e xxx_parent tab
39f83 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43  le */.    "SELEC
39f84 54 20 70 61 72 65 6e 74 6e 6f 64 65 20 46 52 4f  T parentnode FRO
39f85 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65 6e  M '%q'.'%q_paren
39f86 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20  t' WHERE nodeno 
39f87 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45  = :1",.    "INSE
39f88 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
39f89 54 4f 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  TO '%q'.'%q_pare
39f8a 6e 74 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a  nt' VALUES(:1, :
39f8b 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45  2)",.    "DELETE
39f8c 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
39f8d 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
39f8e 65 6e 6f 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a 20  eno = :1".  };. 
39f8f 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
39f90 61 70 70 53 74 6d 74 5b 4e 5f 53 54 41 54 45 4d  appStmt[N_STATEM
39f91 45 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  ENT];.  int i;..
39f92 20 20 70 52 74 72 65 65 2d 3e 64 62 20 3d 20 64    pRtree->db = d
39f93 62 3b 0a 0a 20 20 69 66 28 20 69 73 43 72 65 61  b;..  if( isCrea
39f94 74 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  te ){.    char *
39f95 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74 65  zCreate = sqlite
39f96 33 5f 6d 70 72 69 6e 74 66 28 0a 22 43 52 45 41  3_mprintf(."CREA
39f97 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
39f98 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64 65  \"%w_node\"(node
39f99 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
39f9a 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c 4f  RY KEY, data BLO
39f9b 42 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42  B);"."CREATE TAB
39f9c 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 72  LE \"%w\".\"%w_r
39f9d 6f 77 69 64 5c 22 28 72 6f 77 69 64 20 49 4e 54  owid\"(rowid INT
39f9e 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
39f9f 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  , nodeno INTEGER
39fa0 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42 4c  );"."CREATE TABL
39fa1 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 70 61  E \"%w\".\"%w_pa
39fa2 72 65 6e 74 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e  rent\"(nodeno IN
39fa3 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
39fa4 59 2c 20 70 61 72 65 6e 74 6e 6f 64 65 20 49 4e  Y, parentnode IN
39fa5 54 45 47 45 52 29 3b 22 0a 22 49 4e 53 45 52 54  TEGER);"."INSERT
39fa6 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e   INTO '%q'.'%q_n
39fa7 6f 64 65 27 20 56 41 4c 55 45 53 28 31 2c 20 7a  ode' VALUES(1, z
39fa8 65 72 6f 62 6c 6f 62 28 25 64 29 29 22 2c 0a 20  eroblob(%d))",. 
39fa9 20 20 20 20 20 7a 44 62 2c 20 7a 50 72 65 66 69       zDb, zPrefi
39faa 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
39fab 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a   zDb, zPrefix, z
39fac 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 70 52 74  Db, zPrefix, pRt
39fad 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a 20  ree->iNodeSize. 
39fae 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a     );.    if( !z
39faf 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Create ){.      
39fb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
39fb1 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  MEM;.    }.    r
39fb2 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
39fb3 28 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  (db, zCreate, 0,
39fb4 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
39fb5 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
39fb6 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
39fb7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39fb8 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
39fb9 20 7d 0a 20 20 7d 0a 0a 20 20 61 70 70 53 74 6d   }.  }..  appStm
39fba 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[0] = &pRtree->
39fbb 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 61 70 70  pReadNode;.  app
39fbc 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52 74 72 65  Stmt[1] = &pRtre
39fbd 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20  e->pWriteNode;. 
39fbe 20 61 70 70 53 74 6d 74 5b 32 5d 20 3d 20 26 70   appStmt[2] = &p
39fbf 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f  Rtree->pDeleteNo
39fc0 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 33 5d  de;.  appStmt[3]
39fc1 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61   = &pRtree->pRea
39fc2 64 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d  dRowid;.  appStm
39fc3 74 5b 34 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[4] = &pRtree->
39fc4 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20 61  pWriteRowid;.  a
39fc5 70 70 53 74 6d 74 5b 35 5d 20 3d 20 26 70 52 74  ppStmt[5] = &pRt
39fc6 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
39fc7 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 36 5d 20  d;.  appStmt[6] 
39fc8 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64  = &pRtree->pRead
39fc9 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74 6d  Parent;.  appStm
39fca 74 5b 37 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[7] = &pRtree->
39fcb 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
39fcc 61 70 70 53 74 6d 74 5b 38 5d 20 3d 20 26 70 52  appStmt[8] = &pR
39fcd 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
39fce 65 6e 74 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ent;..  for(i=0;
39fcf 20 69 3c 4e 5f 53 54 41 54 45 4d 45 4e 54 20 26   i<N_STATEMENT &
39fd0 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
39fd1 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
39fd2 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
39fd3 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69 5d  mprintf(azSql[i]
39fd4 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 29 3b  , zDb, zPrefix);
39fd5 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
39fd6 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
39fd7 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64  te3_prepare_v2(d
39fd8 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70 70  b, zSql, -1, app
39fd9 53 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0a 20 20  Stmt[i], 0); .  
39fda 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
39fdb 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
39fdc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
39fdd 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
39fde 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
39fdf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
39fe0 72 6f 75 74 69 6e 65 20 71 75 65 72 69 65 73 20  routine queries 
39fe1 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
39fe2 64 62 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d  db for the page-
39fe3 73 69 7a 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  size used by.** 
39fe4 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 49 66  database zDb. If
39fe5 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
39fe6 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62 79   page-size in by
39fe7 74 65 73 20 69 73 20 77 72 69 74 74 65 6e 20 74  tes is written t
39fe8 6f 0a 2a 2a 20 2a 70 69 50 61 67 65 53 69 7a 65  o.** *piPageSize
39fe9 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
39fea 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
39feb 73 65 2c 20 61 6e 64 20 61 6e 20 53 51 4c 69 74  se, and an SQLit
39fec 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65  e error .** code
39fed 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
39fee 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 50  .static int getP
39fef 61 67 65 53 69 7a 65 28 73 71 6c 69 74 65 33 20  ageSize(sqlite3 
39ff0 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
39ff1 2a 7a 44 62 2c 20 69 6e 74 20 2a 70 69 50 61 67  *zDb, int *piPag
39ff2 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  eSize){.  int rc
39ff3 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
39ff4 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
39ff5 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
39ff6 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53 71  Stmt = 0;..  zSq
39ff7 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
39ff8 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51 2e 70  ntf("PRAGMA %Q.p
39ff9 61 67 65 5f 73 69 7a 65 22 2c 20 7a 44 62 29 3b  age_size", zDb);
39ffa 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
39ffb 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
39ffc 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
39ffd 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
39ffe 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
39fff 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
3a000 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3a001 28 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63  (zSql);.  if( rc
3a002 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
3a003 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a004 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  }..  if( SQLITE_
3a005 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
3a006 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
3a007 2a 70 69 50 61 67 65 53 69 7a 65 20 3d 20 73 71  *piPageSize = sq
3a008 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3a009 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a  (pStmt, 0);.  }.
3a00a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a00b 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
3a00c 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  ;.}../* .** This
3a00d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
3a00e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3a00f 6f 66 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e  of both the xCon
3a010 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74 65  nect and xCreate
3a011 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  .** methods of t
3a012 68 65 20 72 2d 74 72 65 65 20 76 69 72 74 75 61  he r-tree virtua
3a013 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  l table..**.**  
3a014 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f   argv[0]   -> mo
3a015 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  dule name.**   a
3a016 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74 61  rgv[1]   -> data
3a017 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  base name.**   a
3a018 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62 6c  rgv[2]   -> tabl
3a019 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76  e name.**   argv
3a01a 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e 20  [...] -> column 
3a01b 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61 74  names....*/.stat
3a01c 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74  ic int rtreeInit
3a01d 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a01e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a01f 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
3a020 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
3a021 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
3a022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a023 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
3a024 66 20 74 68 65 20 52 54 52 45 45 5f 43 4f 4f 52  f the RTREE_COOR
3a025 44 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f  D_* constants */
3a026 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e  .  int argc, con
3a027 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
3a028 72 67 76 2c 20 20 20 2f 2a 20 50 61 72 61 6d 65  rgv,   /* Parame
3a029 74 65 72 73 20 74 6f 20 43 52 45 41 54 45 20 54  ters to CREATE T
3a02a 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  ABLE statement *
3a02b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
3a02c 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20 20 20   **ppVtab,      
3a02d 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3a02e 4e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  New virtual tabl
3a02f 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
3a030 45 72 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Err,            
3a031 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3a032 54 3a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  T: Error message
3a033 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e  , if any */.  in
3a034 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 20  t isCreate      
3a035 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a036 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43    /* True for xC
3a037 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72  reate, false for
3a038 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a   xConnect */.){.
3a039 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3a03a 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 50 61 67  E_OK;.  int iPag
3a03b 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 52 74 72  eSize = 0;.  Rtr
3a03c 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69 6e  ee *pRtree;.  in
3a03d 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20  t nDb;          
3a03e 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
3a03f 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d 20   string argv[1] 
3a040 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
3a041 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
3a042 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61  ngth of string a
3a043 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74 20  rgv[2] */.  int 
3a044 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 69 6e  eCoordType = (in
3a045 74 29 70 41 75 78 3b 0a 0a 20 20 63 6f 6e 73 74  t)pAux;..  const
3a046 20 63 68 61 72 20 2a 61 45 72 72 4d 73 67 5b 5d   char *aErrMsg[]
3a047 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20   = {.    0,     
3a048 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a049 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a04a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a04b 2a 20 30 20 2a 2f 0a 20 20 20 20 22 57 72 6f 6e  * 0 */.    "Wron
3a04c 67 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  g number of colu
3a04d 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
3a04e 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
3a04f 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 22 54 6f   /* 1 */.    "To
3a050 6f 20 66 65 77 20 63 6f 6c 75 6d 6e 73 20 66 6f  o few columns fo
3a051 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65  r an rtree table
3a052 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
3a053 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 22     /* 2 */.    "
3a054 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  Too many columns
3a055 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61   for an rtree ta
3a056 62 6c 65 22 20 20 20 20 20 20 20 20 20 20 20 20  ble"            
3a057 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 7d       /* 3 */.  }
3a058 3b 0a 0a 20 20 69 6e 74 20 69 45 72 72 20 3d 20  ;..  int iErr = 
3a059 28 61 72 67 63 3c 36 29 20 3f 20 32 20 3a 20 61  (argc<6) ? 2 : a
3a05a 72 67 63 3e 28 52 54 52 45 45 5f 4d 41 58 5f 44  rgc>(RTREE_MAX_D
3a05b 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b 34 29 20 3f  IMENSIONS*2+4) ?
3a05c 20 33 20 3a 20 61 72 67 63 25 32 3b 0a 20 20 69   3 : argc%2;.  i
3a05d 66 28 20 61 45 72 72 4d 73 67 5b 69 45 72 72 5d  f( aErrMsg[iErr]
3a05e 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d   ){.    *pzErr =
3a05f 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a060 28 22 25 73 22 2c 20 61 45 72 72 4d 73 67 5b 69  ("%s", aErrMsg[i
3a061 45 72 72 5d 29 3b 0a 20 20 20 20 72 65 74 75 72  Err]);.    retur
3a062 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
3a063 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 50    }..  rc = getP
3a064 61 67 65 53 69 7a 65 28 64 62 2c 20 61 72 67 76  ageSize(db, argv
3a065 5b 31 5d 2c 20 26 69 50 61 67 65 53 69 7a 65 29  [1], &iPageSize)
3a066 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3a067 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
3a068 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
3a069 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
3a06a 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
3a06b 63 74 75 72 65 20 2a 2f 0a 20 20 6e 44 62 20 3d  cture */.  nDb =
3a06c 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29   strlen(argv[1])
3a06d 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c  ;.  nName = strl
3a06e 65 6e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 70  en(argv[2]);.  p
3a06f 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3a070 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
3a071 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44  sizeof(Rtree)+nD
3a072 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 69 66  b+nName+2);.  if
3a073 28 20 21 70 52 74 72 65 65 20 29 7b 0a 20 20 20  ( !pRtree ){.   
3a074 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3a075 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
3a076 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73 69  et(pRtree, 0, si
3a077 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62 2b  zeof(Rtree)+nDb+
3a078 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 70 52 74 72  nName+2);.  pRtr
3a079 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b 0a 20  ee->nBusy = 1;. 
3a07a 20 70 52 74 72 65 65 2d 3e 62 61 73 65 2e 70 4d   pRtree->base.pM
3a07b 6f 64 75 6c 65 20 3d 20 26 72 74 72 65 65 4d 6f  odule = &rtreeMo
3a07c 64 75 6c 65 3b 0a 20 20 70 52 74 72 65 65 2d 3e  dule;.  pRtree->
3a07d 7a 44 62 20 3d 20 28 63 68 61 72 20 2a 29 26 70  zDb = (char *)&p
3a07e 52 74 72 65 65 5b 31 5d 3b 0a 20 20 70 52 74 72  Rtree[1];.  pRtr
3a07f 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20 26 70 52 74  ee->zName = &pRt
3a080 72 65 65 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d 3b  ree->zDb[nDb+1];
3a081 0a 20 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 20  .  pRtree->nDim 
3a082 3d 20 28 61 72 67 63 2d 34 29 2f 32 3b 0a 20 20  = (argc-4)/2;.  
3a083 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
3a084 72 43 65 6c 6c 20 3d 20 38 20 2b 20 70 52 74 72  rCell = 8 + pRtr
3a085 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20 20  ee->nDim*4*2;.  
3a086 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79  pRtree->eCoordTy
3a087 70 65 20 3d 20 65 43 6f 6f 72 64 54 79 70 65 3b  pe = eCoordType;
3a088 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65  .  memcpy(pRtree
3a089 2d 3e 7a 44 62 2c 20 61 72 67 76 5b 31 5d 2c 20  ->zDb, argv[1], 
3a08a 6e 44 62 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  nDb);.  memcpy(p
3a08b 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61 72  Rtree->zName, ar
3a08c 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a 0a  gv[2], nName);..
3a08d 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
3a08e 74 68 65 20 6e 6f 64 65 20 73 69 7a 65 20 74 6f  the node size to
3a08f 20 75 73 65 2e 20 42 79 20 64 65 66 61 75 6c 74   use. By default
3a090 2c 20 75 73 65 20 36 34 20 62 79 74 65 73 20 6c  , use 64 bytes l
3a091 65 73 73 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68  ess than.  ** th
3a092 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d  e database page-
3a093 73 69 7a 65 2e 20 54 68 69 73 20 65 6e 73 75 72  size. This ensur
3a094 65 73 20 74 68 61 74 20 65 61 63 68 20 6e 6f 64  es that each nod
3a095 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 0a 20  e is stored on. 
3a096 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61 74   ** a single dat
3a097 61 62 61 73 65 20 70 61 67 65 2e 0a 20 20 2a 2a  abase page..  **
3a098 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
3a099 61 62 61 73 64 20 70 61 67 65 2d 73 69 7a 65 20  abasd page-size 
3a09a 69 73 20 73 6f 20 6c 61 72 67 65 20 74 68 61 74  is so large that
3a09b 20 6d 6f 72 65 20 74 68 61 6e 20 52 54 52 45 45   more than RTREE
3a09c 5f 4d 41 58 43 45 4c 4c 53 0a 20 20 2a 2a 20 65  _MAXCELLS.  ** e
3a09d 6e 74 72 69 65 73 20 77 6f 75 6c 64 20 66 69 74  ntries would fit
3a09e 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f 64   in a single nod
3a09f 65 2c 20 75 73 65 20 61 20 73 6d 61 6c 6c 65 72  e, use a smaller
3a0a0 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a 20 20 2a 2f   node-size..  */
3a0a1 0a 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  .  pRtree->iNode
3a0a2 53 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a 65  Size = iPageSize
3a0a3 2d 36 34 3b 0a 20 20 69 66 28 20 28 34 2b 70 52  -64;.  if( (4+pR
3a0a4 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
3a0a5 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c  ell*RTREE_MAXCEL
3a0a6 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f 64  LS)<pRtree->iNod
3a0a7 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 52 74  eSize ){.    pRt
3a0a8 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d  ree->iNodeSize =
3a0a9 20 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65   4+pRtree->nByte
3a0aa 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d  sPerCell*RTREE_M
3a0ab 41 58 43 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20 20  AXCELLS;.  }..  
3a0ac 2f 2a 20 43 72 65 61 74 65 2f 43 6f 6e 6e 65 63  /* Create/Connec
3a0ad 74 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  t to the underly
3a0ae 69 6e 67 20 72 65 6c 61 74 69 6f 6e 61 6c 20 64  ing relational d
3a0af 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
3a0b0 49 66 0a 20 20 2a 2a 20 74 68 61 74 20 69 73 20  If.  ** that is 
3a0b1 73 75 63 63 65 73 73 66 75 6c 2c 20 63 61 6c 6c  successful, call
3a0b2 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
3a0b3 5f 76 74 61 62 28 29 20 74 6f 20 63 6f 6e 66 69  _vtab() to confi
3a0b4 67 75 72 65 0a 20 20 2a 2a 20 74 68 65 20 72 2d  gure.  ** the r-
3a0b5 74 72 65 65 20 74 61 62 6c 65 20 73 63 68 65 6d  tree table schem
3a0b6 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 72  a..  */.  if( (r
3a0b7 63 20 3d 20 72 74 72 65 65 53 71 6c 49 6e 69 74  c = rtreeSqlInit
3a0b8 28 70 52 74 72 65 65 2c 20 64 62 2c 20 61 72 67  (pRtree, db, arg
3a0b9 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d 2c 20 69  v[1], argv[2], i
3a0ba 73 43 72 65 61 74 65 29 29 20 29 7b 0a 20 20 20  sCreate)) ){.   
3a0bb 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
3a0bc 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
3a0bd 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
3a0be 62 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  b));.  }else{.  
3a0bf 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73    char *zSql = s
3a0c0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3a0c1 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28 25  CREATE TABLE x(%
3a0c2 73 22 2c 20 61 72 67 76 5b 33 5d 29 3b 0a 20 20  s", argv[3]);.  
3a0c3 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20    char *zTmp;.  
3a0c4 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
3a0c5 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26 26 20  r(ii=4; zSql && 
3a0c6 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b 0a  ii<argc; ii++){.
3a0c7 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71        zTmp = zSq
3a0c8 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20  l;.      zSql = 
3a0c9 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3a0ca 22 25 73 2c 20 25 73 22 2c 20 7a 54 6d 70 2c 20  "%s, %s", zTmp, 
3a0cb 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
3a0cc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
3a0cd 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  mp);.    }.    i
3a0ce 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  f( zSql ){.     
3a0cf 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20   zTmp = zSql;.  
3a0d0 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
3a0d1 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29 3b  e3_mprintf("%s);
3a0d2 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20 20  ", zTmp);.      
3a0d3 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 6d  sqlite3_free(zTm
3a0d4 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  p);.    }.    if
3a0d5 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  ( !zSql ){.     
3a0d6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3a0d7 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  EM;.    }else if
3a0d8 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
3a0d9 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61   = sqlite3_decla
3a0da 72 65 5f 76 74 61 62 28 64 62 2c 20 7a 53 71 6c  re_vtab(db, zSql
3a0db 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  )) ){.      *pzE
3a0dc 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
3a0dd 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74  intf("%s", sqlit
3a0de 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
3a0df 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
3a0e0 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
3a0e1 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
3a0e2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70  ITE_OK ){.    *p
3a0e3 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65 33  pVtab = (sqlite3
3a0e4 5f 76 74 61 62 20 2a 29 70 52 74 72 65 65 3b 0a  _vtab *)pRtree;.
3a0e5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 74 72    }else{.    rtr
3a0e6 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
3a0e7 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3a0e8 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  rc;.}.../*.** Im
3a0e9 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3a0ea 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
3a0eb 6e 20 74 68 61 74 20 64 65 63 6f 64 65 73 20 72  n that decodes r
3a0ec 2d 74 72 65 65 20 6e 6f 64 65 73 20 74 6f 0a 2a  -tree nodes to.*
3a0ed 2a 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65  * human readable
3a0ee 20 73 74 72 69 6e 67 73 2e 20 54 68 69 73 20 63   strings. This c
3a0ef 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 64  an be used for d
3a0f0 65 62 75 67 67 69 6e 67 20 61 6e 64 20 61 6e 61  ebugging and ana
3a0f1 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lysis..**.** The
3a0f2 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a0f3 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d   takes two argum
3a0f4 65 6e 74 73 2c 20 61 20 62 6c 6f 62 20 6f 66 20  ents, a blob of 
3a0f5 64 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  data containing.
3a0f6 2a 2a 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  ** an r-tree nod
3a0f7 65 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  e, and the numbe
3a0f8 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20  r of dimensions 
3a0f9 74 68 65 20 72 2d 74 72 65 65 20 69 6e 64 65 78  the r-tree index
3a0fa 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 74 77 6f  es..** For a two
3a0fb 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d 74  -dimensional r-t
3a0fc 72 65 65 20 73 74 72 75 63 74 75 72 65 20 63 61  ree structure ca
3a0fd 6c 6c 65 64 20 22 72 74 22 2c 20 74 6f 20 64 65  lled "rt", to de
3a0fe 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c  serialize.** all
3a0ff 20 6e 6f 64 65 73 2c 20 61 20 73 74 61 74 65 6d   nodes, a statem
3a100 65 6e 74 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20  ent like:.**.** 
3a101 20 20 53 45 4c 45 43 54 20 72 74 72 65 65 6e 6f    SELECT rtreeno
3a102 64 65 28 32 2c 20 64 61 74 61 29 20 46 52 4f 4d  de(2, data) FROM
3a103 20 72 74 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20   rt_node;.**.** 
3a104 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61 62  The human readab
3a105 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73 20  le string takes 
3a106 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54 63  the form of a Tc
3a107 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65 0a  l list with one.
3a108 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ** entry for eac
3a109 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 2d  h cell in the r-
3a10a 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68 20  tree node. Each 
3a10b 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66 20  entry is itself 
3a10c 61 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e 74 61  a.** list, conta
3a10d 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79 74 65  ining the 8-byte
3a10e 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20 66 6f   rowid/pageno fo
3a10f 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20 0a 2a  llowed by the .*
3a110 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e  * <num-dimension
3a111 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e  >*2 coordinates.
3a112 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3a113 72 74 72 65 65 6e 6f 64 65 28 73 71 6c 69 74 65  rtreenode(sqlite
3a114 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a115 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a116 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a117 7b 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20  {.  char *zText 
3a118 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65  = 0;.  RtreeNode
3a119 20 6e 6f 64 65 3b 0a 20 20 52 74 72 65 65 20 74   node;.  Rtree t
3a11a 72 65 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a  ree;.  int ii;..
3a11b 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c 20    memset(&node, 
3a11c 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  0, sizeof(RtreeN
3a11d 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ode));.  memset(
3a11e 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66  &tree, 0, sizeof
3a11f 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65 65  (Rtree));.  tree
3a120 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33 5f  .nDim = sqlite3_
3a121 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67 5b  value_int(apArg[
3a122 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79 74  0]);.  tree.nByt
3a123 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
3a124 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a 20  8 * tree.nDim;. 
3a125 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28 75   node.zData = (u
3a126 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  8 *)sqlite3_valu
3a127 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d 29  e_blob(apArg[1])
3a128 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
3a129 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b 20  i<NCELL(&node); 
3a12a 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20  ii++){.    char 
3a12b 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20 20  zCell[512];.    
3a12c 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  int nCell = 0;. 
3a12d 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
3a12e 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  l;.    int jj;..
3a12f 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3a130 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69 69  &tree, &node, ii
3a131 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73 71  , &cell);.    sq
3a132 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35  lite3_snprintf(5
3a133 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b  12-nCell,&zCell[
3a134 6e 43 65 6c 6c 5d 2c 22 25 64 22 2c 20 63 65 6c  nCell],"%d", cel
3a135 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 6e  l.iRowid);.    n
3a136 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43  Cell = strlen(zC
3a137 65 6c 6c 29 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell);.    for(jj
3a138 3d 30 3b 20 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d  =0; jj<tree.nDim
3a139 2a 32 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  *2; jj++){.     
3a13a 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
3a13b 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65  f(512-nCell,&zCe
3a13c 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 20 25 66 22 2c  ll[nCell]," %f",
3a13d 28 64 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43 6f  (double)cell.aCo
3a13e 6f 72 64 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20  ord[jj].f);.    
3a13f 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e    nCell = strlen
3a140 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a  (zCell);.    }..
3a141 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b      if( zText ){
3a142 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65  .      char *zTe
3a143 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  xtNew = sqlite3_
3a144 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25 73 7d  mprintf("%s {%s}
3a145 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c 6c 29  ", zText, zCell)
3a146 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
3a147 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20  free(zText);.   
3a148 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78 74     zText = zText
3a149 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  New;.    }else{.
3a14a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73 71        zText = sq
3a14b 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 7b  lite3_mprintf("{
3a14c 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20  %s}", zCell);.  
3a14d 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73 71 6c    }.  }.  .  sql
3a14e 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
3a14f 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d 31 2c  (ctx, zText, -1,
3a150 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
3a151 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  }..static void r
3a152 74 72 65 65 64 65 70 74 68 28 73 71 6c 69 74 65  treedepth(sqlite
3a153 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a154 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a155 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a156 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  {.  if( sqlite3_
3a157 76 61 6c 75 65 5f 74 79 70 65 28 61 70 41 72 67  value_type(apArg
3a158 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
3a159 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  B .   || sqlite3
3a15a 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70 41  _value_bytes(apA
3a15b 72 67 5b 30 5d 29 3c 32 0a 20 20 29 7b 0a 20 20  rg[0])<2.  ){.  
3a15c 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a15d 5f 65 72 72 6f 72 28 63 74 78 2c 20 22 49 6e 76  _error(ctx, "Inv
3a15e 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  alid argument to
3a15f 20 72 74 72 65 65 64 65 70 74 68 28 29 22 2c 20   rtreedepth()", 
3a160 2d 31 29 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  -1); .  }else{. 
3a161 20 20 20 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 28     u8 *zBlob = (
3a162 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  u8 *)sqlite3_val
3a163 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 30 5d  ue_blob(apArg[0]
3a164 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3a165 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 72  esult_int(ctx, r
3a166 65 61 64 49 6e 74 31 36 28 7a 42 6c 6f 62 29 29  eadInt16(zBlob))
3a167 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
3a168 65 67 69 73 74 65 72 20 74 68 65 20 72 2d 74 72  egister the r-tr
3a169 65 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 64  ee module with d
3a16a 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
3a16b 62 2e 20 54 68 69 73 20 63 72 65 61 74 65 73 20  b. This creates 
3a16c 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
3a16d 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74 72  able module "rtr
3a16e 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62 75  ee" and the debu
3a16f 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20 73  gging/analysis s
3a170 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69  calar .** functi
3a171 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e 0a  on "rtreenode"..
3a172 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3a173 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72  E int sqlite3Rtr
3a174 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  eeInit(sqlite3 *
3a175 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
3a176 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
3a177 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a178 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38 20  ){.    int utf8 
3a179 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
3a17a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a17b 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
3a17c 64 62 2c 20 22 72 74 72 65 65 6e 6f 64 65 22 2c  db, "rtreenode",
3a17d 20 32 2c 20 75 74 66 38 2c 20 30 2c 20 72 74 72   2, utf8, 0, rtr
3a17e 65 65 6e 6f 64 65 2c 20 30 2c 20 30 29 3b 0a 20  eenode, 0, 0);. 
3a17f 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
3a180 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
3a181 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f  t utf8 = SQLITE_
3a182 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73  UTF8;.    rc = s
3a183 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
3a184 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65  nction(db, "rtre
3a185 65 64 65 70 74 68 22 2c 20 31 2c 20 75 74 66 38  edepth", 1, utf8
3a186 2c 20 30 2c 72 74 72 65 65 64 65 70 74 68 2c 20  , 0,rtreedepth, 
3a187 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  0, 0);.  }.  if(
3a188 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a189 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20  {.    void *c = 
3a18a 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f  (void *)RTREE_CO
3a18b 4f 52 44 5f 52 45 41 4c 33 32 3b 0a 20 20 20 20  ORD_REAL32;.    
3a18c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
3a18d 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62  ate_module_v2(db
3a18e 2c 20 22 72 74 72 65 65 22 2c 20 26 72 74 72 65  , "rtree", &rtre
3a18f 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a  eModule, c, 0);.
3a190 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
3a191 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76  LITE_OK ){.    v
3a192 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a  oid *c = (void *
3a193 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  )RTREE_COORD_INT
3a194 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  32;.    rc = sql
3a195 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
3a196 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65  le_v2(db, "rtree
3a197 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f 64  _i32", &rtreeMod
3a198 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a  ule, c, 0);.  }.
3a199 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3a19a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52  .#if !SQLITE_COR
3a19b 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  E.SQLITE_API int
3a19c 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
3a19d 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  on_init(.  sqlit
3a19e 65 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20 2a  e3 *db,.  char *
3a19f 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
3a1a0 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
3a1a1 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
3a1a2 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
3a1a3 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
3a1a4 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a1a5 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a 7d  RtreeInit(db);.}
3a1a6 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a  .#endif..#endif.
3a1a7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a1a8 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63 20   End of rtree.c 
3a1a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a1ac 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a1ad 20 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75 2e   Begin file icu.
3a1ae 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3a1af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a1b1 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
3a1b2 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
3a1b3 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3a1b4 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3a1b5 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3a1b6 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3a1b7 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3a1b8 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3a1b9 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3a1ba 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3a1bb 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3a1bc 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3a1bd 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3a1be 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
3a1bf 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
3a1c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3a1c1 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3a1c2 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3a1c3 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3a1c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a1c8 0a 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c 76  .** $Id: icu.c,v
3a1c9 20 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33 20   1.7 2007/12/13 
3a1ca 32 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78 70  21:54:11 drh Exp
3a1cb 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69   $.**.** This fi
3a1cc 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  le implements an
3a1cd 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65 74   integration bet
3a1ce 77 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69 62  ween the ICU lib
3a1cf 72 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65 72  rary .** ("Inter
3a1d0 6e 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65  national Compone
3a1d1 6e 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65 22  nts for Unicode"
3a1d2 2c 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63 65  , an open-source
3a1d3 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f 72   library .** for
3a1d4 20 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f 64   handling unicod
3a1d5 65 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c 69  e data) and SQLi
3a1d6 74 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61 74  te. The integrat
3a1d7 69 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43 55  ion uses .** ICU
3a1d8 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3a1d9 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51 4c  following to SQL
3a1da 69 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41  ite:.**.**   * A
3a1db 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
3a1dc 20 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67 65   of the SQL rege
3a1dd 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 61  xp() function (a
3a1de 6e 64 20 68 65 6e 63 65 20 52 45 47 45 58 50 0a  nd hence REGEXP.
3a1df 2a 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72 29  **     operator)
3a1e0 20 75 73 69 6e 67 20 74 68 65 20 49 43 55 20 75   using the ICU u
3a1e1 72 65 67 65 78 5f 58 58 28 29 20 41 50 49 73 2e  regex_XX() APIs.
3a1e2 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65  .**.**   * Imple
3a1e3 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
3a1e4 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70 70  e SQL scalar upp
3a1e5 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29  er() and lower()
3a1e6 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20   functions.**   
3a1e7 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69    for case mappi
3a1e8 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e  ng..**.**   * In
3a1e9 74 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43 55  tegration of ICU
3a1ea 20 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c 6c   and SQLite coll
3a1eb 61 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e 0a  ation seqences..
3a1ec 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70  **.**   * An imp
3a1ed 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
3a1ee 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
3a1ef 20 74 68 61 74 20 75 73 65 73 20 49 43 55 20 74   that uses ICU t
3a1f0 6f 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69 64  o .**     provid
3a1f1 65 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65  e case-independe
3a1f2 6e 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a  nt matching..*/.
3a1f3 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
3a1f4 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
3a1f5 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
3a1f6 42 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e 63  BLE_ICU)../* Inc
3a1f7 6c 75 64 65 20 49 43 55 20 68 65 61 64 65 72 73  lude ICU headers
3a1f8 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e   */.#include <un
3a1f9 69 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e 0a  icode/utypes.h>.
3a1fa 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
3a1fb 65 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e 63  e/uregex.h>.#inc
3a1fc 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73  lude <unicode/us
3a1fd 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
3a1fe 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e  e <unicode/ucol.
3a1ff 68 3e 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  h>...#ifndef SQL
3a200 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54  ITE_CORE.  SQLIT
3a201 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
3a202 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a  1.#else.#endif..
3a203 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65  /*.** Maximum le
3a204 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29 20  ngth (in bytes) 
3a205 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 69  of the pattern i
3a206 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  n a LIKE or GLOB
3a207 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  .** operator..*/
3a208 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3a209 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
3a20a 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65  _LENGTH.# define
3a20b 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
3a20c 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20  _PATTERN_LENGTH 
3a20d 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  50000.#endif../*
3a20e 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 6f 66 20 73  .** Version of s
3a20f 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 74 68  qlite3_free() th
3a210 61 74 20 69 73 20 61 6c 77 61 79 73 20 61 20 66  at is always a f
3a211 75 6e 63 74 69 6f 6e 2c 20 6e 65 76 65 72 20 61  unction, never a
3a212 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69   macro..*/.stati
3a213 63 20 76 6f 69 64 20 78 46 72 65 65 28 76 6f 69  c void xFree(voi
3a214 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  d *p){.  sqlite3
3a215 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
3a216 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
3a217 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
3a218 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
3a219 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
3a21a 20 69 73 0a 2a 2a 20 61 20 22 4c 49 4b 45 22 20   is.** a "LIKE" 
3a21b 65 78 70 72 65 73 73 69 6f 6e 2e 20 52 65 74 75  expression. Retu
3a21c 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20 74  rn true (1) if t
3a21d 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d 65  hey are the same
3a21e 20 61 6e 64 20 0a 2a 2a 20 66 61 6c 73 65 20 28   and .** false (
3a21f 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
3a220 69 66 66 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61  ifferent..*/.sta
3a221 74 69 63 20 69 6e 74 20 69 63 75 4c 69 6b 65 43  tic int icuLikeC
3a222 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20  ompare(.  const 
3a223 75 69 6e 74 38 5f 74 20 2a 7a 50 61 74 74 65 72  uint8_t *zPatter
3a224 6e 2c 20 20 20 2f 2a 20 4c 49 4b 45 20 70 61 74  n,   /* LIKE pat
3a225 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tern */.  const 
3a226 75 69 6e 74 38 5f 74 20 2a 7a 53 74 72 69 6e 67  uint8_t *zString
3a227 2c 20 20 20 20 2f 2a 20 54 68 65 20 55 54 46 2d  ,    /* The UTF-
3a228 38 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70  8 string to comp
3a229 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  are against */. 
3a22a 20 63 6f 6e 73 74 20 55 43 68 61 72 33 32 20 75   const UChar32 u
3a22b 45 73 63 20 20 20 20 20 20 20 20 20 2f 2a 20 54  Esc         /* T
3a22c 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
3a22d 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74  ter */.){.  stat
3a22e 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54  ic const int MAT
3a22f 43 48 5f 4f 4e 45 20 3d 20 28 55 43 68 61 72 33  CH_ONE = (UChar3
3a230 32 29 27 5f 27 3b 0a 20 20 73 74 61 74 69 63 20  2)'_';.  static 
3a231 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f  const int MATCH_
3a232 41 4c 4c 20 3d 20 28 55 43 68 61 72 33 32 29 27  ALL = (UChar32)'
3a233 25 27 3b 0a 0a 20 20 69 6e 74 20 69 50 61 74 74  %';..  int iPatt
3a234 65 72 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ern = 0;       /
3a235 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69  * Current byte i
3a236 6e 64 65 78 20 69 6e 20 7a 50 61 74 74 65 72 6e  ndex in zPattern
3a237 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 69 6e   */.  int iStrin
3a238 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
3a239 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e   Current byte in
3a23a 64 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20 2a  dex in zString *
3a23b 2f 0a 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63  /..  int prevEsc
3a23c 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ape = 0;     /* 
3a23d 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76  True if the prev
3a23e 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77  ious character w
3a23f 61 73 20 75 45 73 63 20 2a 2f 0a 0a 20 20 77 68  as uEsc */..  wh
3a240 69 6c 65 28 20 7a 50 61 74 74 65 72 6e 5b 69 50  ile( zPattern[iP
3a241 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20  attern]!=0 ){.. 
3a242 20 20 20 2f 2a 20 52 65 61 64 20 28 61 6e 64 20     /* Read (and 
3a243 63 6f 6e 73 75 6d 65 29 20 74 68 65 20 6e 65 78  consume) the nex
3a244 74 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d  t character from
3a245 20 74 68 65 20 69 6e 70 75 74 20 70 61 74 74 65   the input patte
3a246 72 6e 2e 20 2a 2f 0a 20 20 20 20 55 43 68 61 72  rn. */.    UChar
3a247 33 32 20 75 50 61 74 74 65 72 6e 3b 0a 20 20 20  32 uPattern;.   
3a248 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28   U8_NEXT_UNSAFE(
3a249 7a 50 61 74 74 65 72 6e 2c 20 69 50 61 74 74 65  zPattern, iPatte
3a24a 72 6e 2c 20 75 50 61 74 74 65 72 6e 29 3b 0a 20  rn, uPattern);. 
3a24b 20 20 20 61 73 73 65 72 74 28 75 50 61 74 74 65     assert(uPatte
3a24c 72 6e 21 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  rn!=0);..    /* 
3a24d 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 34 20  There are now 4 
3a24e 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a 20  possibilities:. 
3a24f 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
3a250 20 31 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   1. uPattern is 
3a251 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a252 63 68 2d 61 6c 6c 20 63 68 61 72 61 63 74 65 72  ch-all character
3a253 20 22 25 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "%",.    **    
3a254 20 32 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   2. uPattern is 
3a255 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a256 63 68 2d 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ch-one character
3a257 20 22 5f 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "_",.    **    
3a258 20 33 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   3. uPattern is 
3a259 61 6e 20 75 6e 65 73 63 61 70 65 64 20 65 73 63  an unescaped esc
3a25a 61 70 65 20 63 68 61 72 61 63 74 65 72 2c 20 6f  ape character, o
3a25b 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e 20  r.    **     4. 
3a25c 75 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20 62  uPattern is to b
3a25d 65 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e 20  e handled as an 
3a25e 6f 72 64 69 6e 61 72 79 20 63 68 61 72 61 63 74  ordinary charact
3a25f 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  er.    */.    if
3a260 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
3a261 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48   uPattern==MATCH
3a262 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 20 20 2f 2a  _ALL ){.      /*
3a263 20 43 61 73 65 20 31 2e 20 2a 2f 0a 20 20 20 20   Case 1. */.    
3a264 20 20 75 69 6e 74 38 5f 74 20 63 3b 0a 0a 20 20    uint8_t c;..  
3a265 20 20 20 20 2f 2a 20 53 6b 69 70 20 61 6e 79 20      /* Skip any 
3a266 4d 41 54 43 48 5f 41 4c 4c 20 6f 72 20 4d 41 54  MATCH_ALL or MAT
3a267 43 48 5f 4f 4e 45 20 63 68 61 72 61 63 74 65 72  CH_ONE character
3a268 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 0a  s that follow a.
3a269 20 20 20 20 20 20 2a 2a 20 4d 41 54 43 48 5f 41        ** MATCH_A
3a26a 4c 4c 2e 20 46 6f 72 20 65 61 63 68 20 4d 41 54  LL. For each MAT
3a26b 43 48 5f 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e 65  CH_ONE, skip one
3a26c 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
3a26d 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 65 73 74  e .      ** test
3a26e 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 20 20 2a   string..      *
3a26f 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  /.      while( (
3a270 63 3d 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74  c=zPattern[iPatt
3a271 65 72 6e 5d 29 20 3d 3d 20 4d 41 54 43 48 5f 41  ern]) == MATCH_A
3a272 4c 4c 20 7c 7c 20 63 20 3d 3d 20 4d 41 54 43 48  LL || c == MATCH
3a273 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20  _ONE ){.        
3a274 69 66 28 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45  if( c==MATCH_ONE
3a275 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3a276 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e  ( zString[iStrin
3a277 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  g]==0 ) return 0
3a278 3b 0a 20 20 20 20 20 20 20 20 20 20 55 38 5f 46  ;.          U8_F
3a279 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72  WD_1_UNSAFE(zStr
3a27a 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20  ing, iString);. 
3a27b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3a27c 20 69 50 61 74 74 65 72 6e 2b 2b 3b 0a 20 20 20   iPattern++;.   
3a27d 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
3a27e 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a27f 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  n]==0 ) return 1
3a280 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ;..      while( 
3a281 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
3a282 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
3a283 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 26  icuLikeCompare(&
3a284 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a285 6e 5d 2c 20 26 7a 53 74 72 69 6e 67 5b 69 53 74  n], &zString[iSt
3a286 72 69 6e 67 5d 2c 20 75 45 73 63 29 20 29 7b 0a  ring], uEsc) ){.
3a287 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a288 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
3a289 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
3a28a 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
3a28b 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 7d  String);.      }
3a28c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
3a28d 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
3a28e 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20 75  !prevEscape && u
3a28f 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f  Pattern==MATCH_O
3a290 4e 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  NE ){.      /* C
3a291 61 73 65 20 32 2e 20 2a 2f 0a 20 20 20 20 20 20  ase 2. */.      
3a292 69 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72  if( zString[iStr
3a293 69 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e  ing]==0 ) return
3a294 20 30 3b 0a 20 20 20 20 20 20 55 38 5f 46 57 44   0;.      U8_FWD
3a295 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e  _1_UNSAFE(zStrin
3a296 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 0a 20 20  g, iString);..  
3a297 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
3a298 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
3a299 65 72 6e 3d 3d 75 45 73 63 29 7b 0a 20 20 20 20  ern==uEsc){.    
3a29a 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f 0a    /* Case 3. */.
3a29b 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65        prevEscape
3a29c 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 65 6c 73 65   = 1;..    }else
3a29d 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
3a29e 34 2e 20 2a 2f 0a 20 20 20 20 20 20 55 43 68 61  4. */.      UCha
3a29f 72 33 32 20 75 53 74 72 69 6e 67 3b 0a 20 20 20  r32 uString;.   
3a2a0 20 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46     U8_NEXT_UNSAF
3a2a1 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
3a2a2 6e 67 2c 20 75 53 74 72 69 6e 67 29 3b 0a 20 20  ng, uString);.  
3a2a3 20 20 20 20 75 53 74 72 69 6e 67 20 3d 20 75 5f      uString = u_
3a2a4 66 6f 6c 64 43 61 73 65 28 75 53 74 72 69 6e 67  foldCase(uString
3a2a5 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45  , U_FOLD_CASE_DE
3a2a6 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20 75 50  FAULT);.      uP
3a2a7 61 74 74 65 72 6e 20 3d 20 75 5f 66 6f 6c 64 43  attern = u_foldC
3a2a8 61 73 65 28 75 50 61 74 74 65 72 6e 2c 20 55 5f  ase(uPattern, U_
3a2a9 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
3a2aa 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 53  T);.      if( uS
3a2ab 74 72 69 6e 67 21 3d 75 50 61 74 74 65 72 6e 20  tring!=uPattern 
3a2ac 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
3a2ad 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
3a2ae 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
3a2af 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
3a2b0 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b 69  return zString[i
3a2b1 53 74 72 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f  String]==0;.}../
3a2b2 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3a2b3 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65 28  ion of the like(
3a2b4 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
3a2b5 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3a2b6 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65  mplements.** the
3a2b7 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f   build-in LIKE o
3a2b8 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66 69  perator.  The fi
3a2b9 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
3a2ba 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
3a2bb 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61  the.** pattern a
3a2bc 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
3a2bd 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 74  gument is the st
3a2be 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20 53  ring.  So, the S
3a2bf 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a  QL statements:.*
3a2c0 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b  *.**       A LIK
3a2c1 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70  E B.**.** is imp
3a2c2 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b 65  lemented as like
3a2c3 28 42 2c 20 41 29 2e 20 49 66 20 74 68 65 72 65  (B, A). If there
3a2c4 20 69 73 20 61 6e 20 65 73 63 61 70 65 20 63 68   is an escape ch
3a2c5 61 72 61 63 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a  aracter E, .**.*
3a2c6 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
3a2c7 20 45 53 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20   ESCAPE E.**.** 
3a2c8 69 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69 6b  is mapped to lik
3a2c9 65 28 42 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73  e(B, A, E)..*/.s
3a2ca 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c 69  tatic void icuLi
3a2cb 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  keFunc(.  sqlite
3a2cc 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3a2cd 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c  xt, .  int argc,
3a2ce 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
3a2cf 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
3a2d0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3a2d1 72 20 2a 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f  r *zA = sqlite3_
3a2d2 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
3a2d3 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  0]);.  const uns
3a2d4 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 20 3d  igned char *zB =
3a2d5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a2d6 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
3a2d7 55 43 68 61 72 33 32 20 75 45 73 63 20 3d 20 30  UChar32 uEsc = 0
3a2d8 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68  ;..  /* Limit th
3a2d9 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
3a2da 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74  LIKE or GLOB pat
3a2db 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72  tern to avoid pr
3a2dc 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64  oblems.  ** of d
3a2dd 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e  eep recursion an
3a2de 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69  d N*N behavior i
3a2df 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  n patternCompare
3a2e0 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ()..  */.  if( s
3a2e1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a2e2 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49  es(argv[0])>SQLI
3a2e3 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
3a2e4 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20  ERN_LENGTH ){.  
3a2e5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a2e6 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3a2e7 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61  "LIKE or GLOB pa
3a2e8 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65  ttern too comple
3a2e9 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  x", -1);.    ret
3a2ea 75 72 6e 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66 28  urn;.  }...  if(
3a2eb 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
3a2ec 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
3a2ed 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
3a2ee 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
3a2ef 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
3a2f0 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
3a2f1 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
3a2f2 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
3a2f3 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 3d 20 73  */.    int nE= s
3a2f4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a2f5 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  es(argv[2]);.   
3a2f6 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
3a2f7 63 68 61 72 20 2a 7a 45 20 3d 20 73 71 6c 69 74  char *zE = sqlit
3a2f8 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
3a2f9 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 6e 74 20  gv[2]);.    int 
3a2fa 69 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 7a  i = 0;.    if( z
3a2fb 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  E==0 ) return;. 
3a2fc 20 20 20 55 38 5f 4e 45 58 54 28 7a 45 2c 20 69     U8_NEXT(zE, i
3a2fd 2c 20 6e 45 2c 20 75 45 73 63 29 3b 0a 20 20 20  , nE, uEsc);.   
3a2fe 20 69 66 28 20 69 21 3d 6e 45 29 7b 0a 20 20 20   if( i!=nE){.   
3a2ff 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3a300 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
3a301 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43   .          "ESC
3a302 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  APE expression m
3a303 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ust be a single 
3a304 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b  character", -1);
3a305 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
3a306 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
3a307 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20 20  zA && zB ){.    
3a308 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
3a309 6e 74 28 63 6f 6e 74 65 78 74 2c 20 69 63 75 4c  nt(context, icuL
3a30a 69 6b 65 43 6f 6d 70 61 72 65 28 7a 41 2c 20 7a  ikeCompare(zA, z
3a30b 42 2c 20 75 45 73 63 29 29 3b 0a 20 20 7d 0a 7d  B, uEsc));.  }.}
3a30c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
3a30d 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
3a30e 77 68 65 6e 20 61 6e 20 49 43 55 20 66 75 6e 63  when an ICU func
3a30f 74 69 6f 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d  tion called from
3a310 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 69   within.** the i
3a311 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3a312 20 61 6e 20 53 51 4c 20 73 63 61 6c 61 72 20 66   an SQL scalar f
3a313 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
3a314 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
3a315 54 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  The scalar funct
3a316 69 6f 6e 20 63 6f 6e 74 65 78 74 20 70 61 73 73  ion context pass
3a317 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
3a318 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
3a319 6c 6f 61 64 65 64 20 77 69 74 68 20 61 6e 20 65  loaded with an e
3a31a 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 61 73  rror message bas
3a31b 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ed on the follow
3a31c 69 6e 67 20 74 77 6f 20 61 72 67 73 2e 0a 2a 2f  ing two args..*/
3a31d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
3a31e 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0a 20  FunctionError(. 
3a31f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3a320 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f 2a   *pCtx,       /*
3a321 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20 66   SQLite scalar f
3a322 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
3a323 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a324 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
3a325 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43 55    /* Name of ICU
3a326 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66   function that f
3a327 61 69 6c 65 64 20 2a 2f 0a 20 20 55 45 72 72 6f  ailed */.  UErro
3a328 72 43 6f 64 65 20 65 20 20 20 20 20 20 20 20 20  rCode e         
3a329 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
3a32a 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
3a32b 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 2a  y ICU function *
3a32c 2f 0a 29 7b 0a 20 20 63 68 61 72 20 7a 42 75 66  /.){.  char zBuf
3a32d 5b 31 32 38 5d 3b 0a 20 20 73 71 6c 69 74 65 33  [128];.  sqlite3
3a32e 5f 73 6e 70 72 69 6e 74 66 28 31 32 38 2c 20 7a  _snprintf(128, z
3a32f 42 75 66 2c 20 22 49 43 55 20 65 72 72 6f 72 3a  Buf, "ICU error:
3a330 20 25 73 28 29 3a 20 25 73 22 2c 20 7a 4e 61 6d   %s(): %s", zNam
3a331 65 2c 20 75 5f 65 72 72 6f 72 4e 61 6d 65 28 65  e, u_errorName(e
3a332 29 29 3b 0a 20 20 7a 42 75 66 5b 31 32 37 5d 20  ));.  zBuf[127] 
3a333 3d 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74 65  = '\0';.  sqlite
3a334 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
3a335 43 74 78 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a  Ctx, zBuf, -1);.
3a336 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  }../*.** Functio
3a337 6e 20 74 6f 20 64 65 6c 65 74 65 20 63 6f 6d 70  n to delete comp
3a338 69 6c 65 64 20 72 65 67 65 78 70 20 6f 62 6a 65  iled regexp obje
3a339 63 74 73 2e 20 52 65 67 69 73 74 65 72 65 64 20  cts. Registered 
3a33a 61 73 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74  as.** a destruct
3a33b 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  or function with
3a33c 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
3a33d 64 61 74 61 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  data()..*/.stati
3a33e 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78 70  c void icuRegexp
3a33f 44 65 6c 65 74 65 28 76 6f 69 64 20 2a 70 29 7b  Delete(void *p){
3a340 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65  .  URegularExpre
3a341 73 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20 28  ssion *pExpr = (
3a342 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69  URegularExpressi
3a343 6f 6e 20 2a 29 70 3b 0a 20 20 75 72 65 67 65 78  on *)p;.  uregex
3a344 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0a 7d  _close(pExpr);.}
3a345 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3a346 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  tation of SQLite
3a347 20 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72   REGEXP operator
3a348 2e 20 54 68 69 73 20 73 63 61 6c 61 72 20 66 75  . This scalar fu
3a349 6e 63 74 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20  nction takes.** 
3a34a 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 54  two arguments. T
3a34b 68 65 20 66 69 72 73 74 20 69 73 20 61 20 72 65  he first is a re
3a34c 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
3a34d 20 70 61 74 74 65 72 6e 20 74 6f 20 63 6f 6d 70   pattern to comp
3a34e 69 6c 65 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e  ile.** the secon
3a34f 64 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 6f  d is a string to
3a350 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74   match against t
3a351 68 61 74 20 70 61 74 74 65 72 6e 2e 20 49 66 20  hat pattern. If 
3a352 65 69 74 68 65 72 20 0a 2a 2a 20 61 72 67 75 6d  either .** argum
3a353 65 6e 74 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  ent is an SQL NU
3a354 4c 4c 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 49 73  LL, then NULL Is
3a355 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
3a356 77 69 73 65 2c 20 74 68 65 20 72 65 73 75 6c 74  wise, the result
3a357 0a 2a 2a 20 69 73 20 31 20 69 66 20 74 68 65 20  .** is 1 if the 
3a358 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20 74  string matches t
3a359 68 65 20 70 61 74 74 65 72 6e 2c 20 6f 72 20 30  he pattern, or 0
3a35a 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
3a35b 2a 20 53 51 4c 69 74 65 20 6d 61 70 73 20 74 68  * SQLite maps th
3a35c 65 20 72 65 67 65 78 70 28 29 20 66 75 6e 63 74  e regexp() funct
3a35d 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 65 78  ion to the regex
3a35e 70 28 29 20 6f 70 65 72 61 74 6f 72 20 73 75 63  p() operator suc
3a35f 68 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 66 6f  h.** that the fo
3a360 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 65 20  llowing two are 
3a361 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a  equivalent:.**.*
3a362 2a 20 20 20 20 20 7a 53 74 72 69 6e 67 20 52 45  *     zString RE
3a363 47 45 58 50 20 7a 50 61 74 74 65 72 6e 0a 2a 2a  GEXP zPattern.**
3a364 20 20 20 20 20 72 65 67 65 78 70 28 7a 50 61 74       regexp(zPat
3a365 74 65 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0a 2a  tern, zString).*
3a366 2a 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66 6f  *.** Uses the fo
3a367 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67 65  llowing ICU rege
3a368 78 70 20 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20  xp APIs:.**.**  
3a369 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 29     uregex_open()
3a36a 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f 6d  .**     uregex_m
3a36b 61 74 63 68 65 73 28 29 0a 2a 2a 20 20 20 20 20  atches().**     
3a36c 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 29 0a 2a  uregex_close().*
3a36d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
3a36e 75 52 65 67 65 78 70 46 75 6e 63 28 73 71 6c 69  uRegexpFunc(sqli
3a36f 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20  te3_context *p, 
3a370 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a371 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a372 7b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  {.  UErrorCode s
3a373 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
3a374 52 52 4f 52 3b 0a 20 20 55 52 65 67 75 6c 61 72  RROR;.  URegular
3a375 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78 70  Expression *pExp
3a376 72 3b 0a 20 20 55 42 6f 6f 6c 20 72 65 73 3b 0a  r;.  UBool res;.
3a377 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
3a378 53 74 72 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  String = sqlite3
3a379 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70  _value_text16(ap
3a37a 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49  Arg[1]);..  /* I
3a37b 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
3a37c 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67 65  side of the rege
3a37d 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20 4e  xp operator is N
3a37e 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20  ULL, .  ** then 
3a37f 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  the result is al
3a380 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20  so NULL. .  */. 
3a381 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29 7b   if( !zString ){
3a382 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3a383 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69  ..  pExpr = sqli
3a384 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
3a385 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70 45  p, 0);.  if( !pE
3a386 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  xpr ){.    const
3a387 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72 6e   UChar *zPattern
3a388 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3a389 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d  _text16(apArg[0]
3a38a 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61 74  );.    if( !zPat
3a38b 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72 65  tern ){.      re
3a38c 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
3a38d 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f 6f  pExpr = uregex_o
3a38e 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d 31  pen(zPattern, -1
3a38f 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
3a390 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55 43  ;..    if( U_SUC
3a391 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3a392 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65        sqlite3_se
3a393 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c 20  t_auxdata(p, 0, 
3a394 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78 70  pExpr, icuRegexp
3a395 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65 6c  Delete);.    }el
3a396 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3a397 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  (!pExpr);.      
3a398 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72  icuFunctionError
3a399 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65 6e  (p, "uregex_open
3a39a 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3a39b 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
3a39c 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69 67    }..  /* Config
3a39d 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68 61  ure the text tha
3a39e 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  t the regular ex
3a39f 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65  pression operate
3a3a0 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67 65  s on. */.  urege
3a3a1 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3a3a2 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73   zString, -1, &s
3a3a3 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
3a3a4 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3a3a5 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3a3a6 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
3a3a7 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73 74  gex_setText", st
3a3a8 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
3a3a9 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 74  n;.  }..  /* Att
3a3aa 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20 2a  empt the match *
3a3ab 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65 78  /.  res = uregex
3a3ac 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c 20  _matches(pExpr, 
3a3ad 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  0, &status);.  i
3a3ae 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
3a3af 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
3a3b0 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
3a3b1 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73   "uregex_matches
3a3b2 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3a3b3 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
3a3b4 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20 74  * Set the text t
3a3b5 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20  hat the regular 
3a3b6 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61  expression opera
3a3b7 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c  tes on to a NULL
3a3b8 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 54  .  ** pointer. T
3a3b9 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  his is not reall
3a3ba 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74  y necessary, but
3a3bb 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74 68   it is tidier th
3a3bc 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67  an .  ** leaving
3a3bd 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
3a3be 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63  ression object c
3a3bf 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20 61  onfigured with a
3a3c0 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20 70  n invalid.  ** p
3a3c1 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68 69  ointer after thi
3a3c2 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
3a3c3 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67 65  ns..  */.  urege
3a3c4 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3a3c5 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b   0, 0, &status);
3a3c6 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31 20  ..  /* Return 1 
3a3c7 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  or 0. */.  sqlit
3a3c8 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 2c  e3_result_int(p,
3a3c9 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a 7d   res ? 1 : 0);.}
3a3ca 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3a3cb 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c 61  tations of scala
3a3cc 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  r functions for 
3a3cd 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20 75  case mapping - u
3a3ce 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20 6c  pper() and .** l
3a3cf 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f 6e  ower(). Function
3a3d0 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72 74   upper() convert
3a3d1 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20 75  s its input to u
3a3d2 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29 2e  pper-case (ABC).
3a3d3 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f 77  .** Function low
3a3d4 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74 6f  er() converts to
3a3d5 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62 63   lower-case (abc
3a3d6 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72 6f  )..**.** ICU pro
3a3d7 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73 20  vides two types 
3a3d8 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  of case mapping,
3a3d9 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20   "general" case 
3a3da 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20 22  mapping and.** "
3a3db 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66 69  language specifi
3a3dc 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43 55  c". Refer to ICU
3a3dd 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
3a3de 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  or the differenc
3a3df 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74 68  es.** between th
3a3e0 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  e two..**.** To 
3a3e1 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61 6c  utilise "general
3a3e2 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20  " case mapping, 
3a3e3 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20 6c  the upper() or l
3a3e4 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a 2a  ower() scalar .*
3a3e5 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
3a3e6 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65  invoked with one
3a3e7 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a   argument:.**.**
3a3e8 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43 27       upper('ABC'
3a3e9 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20 20  ) -> 'abc'.**   
3a3ea 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20 2d    lower('abc') -
3a3eb 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f  > 'ABC'.**.** To
3a3ec 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61 6e   access ICU "lan
3a3ed 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22 20  guage specific" 
3a3ee 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75 70  case mapping, up
3a3ef 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
3a3f0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  .** should be in
3a3f1 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
3a3f2 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73 65  rguments. The se
3a3f3 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
3a3f4 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
3a3f5 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75 73  the locale to us
3a3f6 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65 6d  e. Passing an em
3a3f7 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29 20  pty string ("") 
3a3f8 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  or SQL NULL valu
3a3f9 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63 6f  e.** as the seco
3a3fa 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
3a3fb 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f 6b  he same as invok
3a3fc 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d 65  ing the 1 argume
3a3fd 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  nt version.** of
3a3fe 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65   upper() or lowe
3a3ff 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c  r()..**.**     l
3a400 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75 73  ower('I', 'en_us
3a401 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20 20  ') -> 'i'.**    
3a402 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72 5f   lower('I', 'tr_
3a403 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73 6d  tr') -> '..' (sm
3a404 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a 2a  all dotless i).*
3a405 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e  *.** http://www.
3a406 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f  icu-project.org/
3a407 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78 2e  userguide/posix.
3a408 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e  html#case_mappin
3a409 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  gs.*/.static voi
3a40a 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 28  d icuCaseFunc16(
3a40b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a40c 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71  *p, int nArg, sq
3a40d 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
3a40e 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55 43  Arg){.  const UC
3a40f 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 55  har *zInput;.  U
3a410 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a 20  Char *zOutput;. 
3a411 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20 69   int nInput;.  i
3a412 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20 55  nt nOutput;..  U
3a413 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
3a414 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
3a415 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3a416 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20 61  Locale = 0;..  a
3a417 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c 7c  ssert(nArg==1 ||
3a418 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66 28   nArg==2);.  if(
3a419 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20 20   nArg==2 ){.    
3a41a 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74  zLocale = (const
3a41b 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
3a41c 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67  value_text(apArg
3a41d 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e  [1]);.  }..  zIn
3a41e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
3a41f 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67  lue_text16(apArg
3a420 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49 6e  [0]);.  if( !zIn
3a421 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3a422 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74 20  n;.  }.  nInput 
3a423 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3a424 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30 5d  bytes16(apArg[0]
3a425 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d 20  );..  nOutput = 
3a426 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b 0a  nInput * 2 + 2;.
3a427 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c 69    zOutput = sqli
3a428 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70  te3_malloc(nOutp
3a429 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74  ut);.  if( !zOut
3a42a 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3a42b 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  n;.  }..  if( sq
3a42c 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
3a42d 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72 54  p) ){.    u_strT
3a42e 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c 20  oUpper(zOutput, 
3a42f 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
3a430 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
3a431 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
3a432 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f 73    }else{.    u_s
3a433 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75  trToLower(zOutpu
3a434 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49  t, nOutput/2, zI
3a435 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20  nput, nInput/2, 
3a436 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
3a437 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 55  );.  }..  if( !U
3a438 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3a439 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3a43a 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f 73  ionError(p, "u_s
3a43b 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73 74  trToLower()/u_st
3a43c 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74 75  rToUpper", statu
3a43d 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  s);.    return;.
3a43e 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
3a43f 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c 20  esult_text16(p, 
3a440 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46 72  zOutput, -1, xFr
3a441 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ee);.}../*.** Co
3a442 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3a443 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
3a444 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
3a445 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
3a446 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
3a447 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
3a448 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
3a449 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
3a44a 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
3a44b 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  d icuCollationDe
3a44c 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a 20  l(void *pCtx){. 
3a44d 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20   UCollator *p = 
3a44e 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74  (UCollator *)pCt
3a44f 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28  x;.  ucol_close(
3a450 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c  p);.}../*.** Col
3a451 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3a452 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
3a453 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61 72  ion. The pCtx ar
3a454 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
3a455 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20  .** a UCollator 
3a456 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
3a457 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 75  usly allocated u
3a458 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29  sing ucol_open()
3a459 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3a45a 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
3a45b 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c 0a  (.  void *pCtx,.
3a45c 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63    int nLeft,.  c
3a45d 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66 74  onst void *zLeft
3a45e 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ,.  int nRight,.
3a45f 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 52    const void *zR
3a460 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61  ight.){.  UColla
3a461 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b 0a  tionResult res;.
3a462 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
3a463 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
3a464 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c  tx;.  res = ucol
3a465 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68  _strcoll(p, (UCh
3a466 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66  ar *)zLeft, nLef
3a467 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a 52  t/2, (UChar *)zR
3a468 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b  ight, nRight/2);
3a469 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20 29  .  switch( res )
3a46a 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  {.    case UCOL_
3a46b 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e 20  LESS:    return 
3a46c 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f  -1;.    case UCO
3a46d 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75 72  L_GREATER: retur
3a46e 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20 55  n +1;.    case U
3a46f 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65 74  COL_EQUAL:   ret
3a470 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
3a471 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65 64  ert(!"Unexpected
3a472 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
3a473 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28  om ucol_strcoll(
3a474 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  )");.  return 0;
3a475 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
3a476 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
3a477 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
3a478 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
3a479 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  on()..**.** This
3a47a 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a47b 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
3a47c 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62 61  ICU collation ba
3a47d 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  sed collation .*
3a47e 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53 51  * types to an SQ
3a47f 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63 6f  Lite database co
3a480 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73 20  nnection. It is 
3a481 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63  intended to be c
3a482 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c  alled.** as foll
3a483 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
3a484 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63  ELECT icu_load_c
3a485 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65  ollation(<locale
3a486 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61  >, <collation-na
3a487 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72  me>);.**.** Wher
3a488 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61 20  e <locale> is a 
3a489 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
3a48a 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65 20  g an ICU locale 
3a48b 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65 2e  identifier (i.e.
3a48c 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74 72  .** "en_AU", "tr
3a48d 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20 3c  _TR" etc.) and <
3a48e 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20  collation-name> 
3a48f 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
3a490 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
3a491 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65 61  sequence to crea
3a492 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  te..*/.static vo
3a493 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  id icuLoadCollat
3a494 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
3a495 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69 6e  ontext *p, .  in
3a496 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69 74  t nArg, .  sqlit
3a497 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
3a498 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
3a499 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73  b = (sqlite3 *)s
3a49a 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
3a49b 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64  (p);.  UErrorCod
3a49c 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52  e status = U_ZER
3a49d 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74  O_ERROR;.  const
3a49e 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20   char *zLocale; 
3a49f 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20 69       /* Locale i
3a4a0 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67 2e  dentifier - (eg.
3a4a1 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20 63   "jp_JP") */.  c
3a4a2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3a4a3 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  ;        /* SQL 
3a4a4 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
3a4a5 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61  ce name (eg. "ja
3a4a6 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55 43  panese") */.  UC
3a4a7 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61  ollator *pUColla
3a4a8 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c  tor;    /* ICU l
3a4a9 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e  ibrary collation
3a4aa 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
3a4ab 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
3a4ac 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
3a4ad 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74   code from sqlit
3a4ae 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3a4af 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61 73  ion_x() */..  as
3a4b0 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a 20  sert(nArg==2);. 
3a4b1 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
3a4b2 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
3a4b3 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
3a4b4 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  g[0]);.  zName =
3a4b5 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3a4b6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3a4b7 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20  t(apArg[1]);..  
3a4b8 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20  if( !zLocale || 
3a4b9 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72 65  !zName ){.    re
3a4ba 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55 43  turn;.  }..  pUC
3a4bb 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f  ollator = ucol_o
3a4bc 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73 74  pen(zLocale, &st
3a4bd 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
3a4be 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
3a4bf 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
3a4c0 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f 6c  onError(p, "ucol
3a4c1 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29 3b  _open", status);
3a4c2 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3a4c3 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a 20  .  assert(p);.. 
3a4c4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3a4c5 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
3a4c6 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  2(db, zName, SQL
3a4c7 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
3a4c8 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a   *)pUCollator, .
3a4c9 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74 69        icuCollati
3a4ca 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61  onColl, icuColla
3a4cb 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20 69  tionDel.  );.  i
3a4cc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3a4cd 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c 6f   ){.    ucol_clo
3a4ce 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a  se(pUCollator);.
3a4cf 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3a4d0 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72 72  lt_error(p, "Err
3a4d1 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20 63  or registering c
3a4d2 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
3a4d3 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  n", -1);.  }.}..
3a4d4 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
3a4d5 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e  he ICU extension
3a4d6 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20   functions with 
3a4d7 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f 0a  database db..*/.
3a4d8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3a4d9 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
3a4da 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
3a4db 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
3a4dc 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ar {.    const c
3a4dd 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
3a4de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4df 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e     /* Function n
3a4e0 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ame */.    int n
3a4e1 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3a4e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4e3 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3a4e4 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  f arguments */. 
3a4e5 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20 20     int enc;     
3a4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a4e8 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e 63  Optimal text enc
3a4e9 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69  oding */.    voi
3a4ea 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20 20  d *pContext;    
3a4eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4ec 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
3a4ed 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63 6f  3_user_data() co
3a4ee 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f 69  ntext */.    voi
3a4ef 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
3a4f0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
3a4f1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
3a4f2 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d 20  ;.  } scalars[] 
3a4f3 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78 70  = {.    {"regexp
3a4f4 22 2c 2d 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ",-1, SQLITE_ANY
3a4f5 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  ,          0, ic
3a4f6 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a 20  uRegexpFunc},.. 
3a4f7 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c     {"lower",  1,
3a4f8 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
3a4f9 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
3a4fa 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c  Func16},.    {"l
3a4fb 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  ower",  2, SQLIT
3a4fc 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
3a4fd 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
3a4fe 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
3a4ff 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    1, SQLITE_UTF1
3a500 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
3a501 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
3a502 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53   {"upper",  2, S
3a503 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
3a504 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
3a505 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f  nc16},..    {"lo
3a506 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  wer",  1, SQLITE
3a507 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
3a508 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
3a509 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ,.    {"lower", 
3a50a 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
3a50b 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
3a50c 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
3a50d 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51  {"upper",  1, SQ
3a50e 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
3a50f 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
3a510 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
3a511 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
3a512 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
3a513 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
3a514 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20  .    {"like",   
3a515 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
3a516 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69          0, icuLi
3a517 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c  keFunc},.    {"l
3a518 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49 54  ike",   3, SQLIT
3a519 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
3a51a 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c  0, icuLikeFunc},
3a51b 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61 64  ..    {"icu_load
3a51c 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32 2c  _collation",  2,
3a51d 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76   SQLITE_UTF8, (v
3a51e 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61 64  oid*)db, icuLoad
3a51f 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b  Collation},.  };
3a520 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
3a521 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
3a522 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ..  for(i=0; rc=
3a523 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
3a524 28 73 69 7a 65 6f 66 28 73 63 61 6c 61 72 73 29  (sizeof(scalars)
3a525 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 49  /sizeof(struct I
3a526 63 75 53 63 61 6c 61 72 29 29 3b 20 69 2b 2b 29  cuScalar)); i++)
3a527 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 63 75  {.    struct Icu
3a528 53 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63 61  Scalar *p = &sca
3a529 6c 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63 20  lars[i];.    rc 
3a52a 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
3a52b 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20  _function(.     
3a52c 20 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c     db, p->zName,
3a52d 20 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e 63   p->nArg, p->enc
3a52e 2c 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20 70  , p->pContext, p
3a52f 2d 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20 20  ->xFunc, 0, 0.  
3a530 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75    );.  }..  retu
3a531 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53  rn rc;.}..#if !S
3a532 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54  QLITE_CORE.SQLIT
3a533 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3a534 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
3a535 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a536 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72   .  char **pzErr
3a537 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
3a538 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
3a539 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
3a53a 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
3a53b 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
3a53c 72 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  rn sqlite3IcuIni
3a53d 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  t(db);.}.#endif.
3a53e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
3a53f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3a540 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.c **********
3a541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a543 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3a544 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
3a545 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20 2a  ile fts3_icu.c *
3a546 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a547 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a548 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
3a549 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a  007 June 22.**.*
3a54a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
3a54b 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
3a54c 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3a54d 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3a54e 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
3a54f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3a550 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
3a551 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
3a552 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
3a553 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3a554 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3a555 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3a556 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
3a557 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
3a558 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
3a559 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
3a55a 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
3a55b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3a55c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a55d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a55e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a55f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
3a560 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
3a561 74 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 66  ts a tokenizer f
3a562 6f 72 20 66 74 73 33 20 62 61 73 65 64 20 6f 6e  or fts3 based on
3a563 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79   the ICU library
3a564 2e 0a 2a 2a 20 0a 2a 2a 20 24 49 64 3a 20 66 74  ..** .** $Id: ft
3a565 73 33 5f 69 63 75 2e 63 2c 76 20 31 2e 33 20 32  s3_icu.c,v 1.3 2
3a566 30 30 38 2f 30 39 2f 30 31 20 31 38 3a 33 34 3a  008/09/01 18:34:
3a567 32 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  20 danielk1977 E
3a568 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  xp $.*/..#if !de
3a569 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3a56a 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3a56b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3a56c 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ).#ifdef SQLITE_
3a56d 45 4e 41 42 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e  ENABLE_ICU...#in
3a56e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3a56f 62 72 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  brk.h>.#include 
3a570 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36 2e 68  <unicode/utf16.h
3a571 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  >..typedef struc
3a572 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 49  t IcuTokenizer I
3a573 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  cuTokenizer;.typ
3a574 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75 43  edef struct IcuC
3a575 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f 72 3b  ursor IcuCursor;
3a576 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65  ..struct IcuToke
3a577 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65  nizer {.  sqlite
3a578 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65  3_tokenizer base
3a579 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c  ;.  char *zLocal
3a57a 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 49 63  e;.};..struct Ic
3a57b 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  uCursor {.  sqli
3a57c 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3a57d 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20 55 42  rsor base;..  UB
3a57e 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a 70 49  reakIterator *pI
3a57f 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49 43 55  ter;      /* ICU
3a580 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f 72 20   break-iterator 
3a581 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  object */.  int 
3a582 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  nChar;          
3a583 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3a584 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65 6d 65  r of UChar eleme
3a585 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f  nts in pInput */
3a586 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61 72 3b  .  UChar *aChar;
3a587 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a588 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75 74 20  * Copy of input 
3a589 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65 6e 63  using utf-16 enc
3a58a 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a  oding */.  int *
3a58b 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  aOffset;        
3a58c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
3a58d 73 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63  s of each charac
3a58e 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69 6e 70  ter in utf-8 inp
3a58f 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42 75  ut */..  int nBu
3a590 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42  ffer;.  char *zB
3a591 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20 69 54  uffer;..  int iT
3a592 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  oken;.};../*.** 
3a593 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
3a594 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e  enizer instance.
3a595 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a596 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20  cuCreate(.  int 
3a597 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
3a598 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a599 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
3a59a 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b 5d 20  tries in argv[] 
3a59b 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a59c 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
3a59d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
3a59e 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f 6e 20  enizer creation 
3a59f 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73  arguments */.  s
3a5a0 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3a5a1 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
3a5a2 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61      /* OUT: Crea
3a5a3 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  ted tokenizer */
3a5a4 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  .){.  IcuTokeniz
3a5a5 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 20 3d  er *p;.  int n =
3a5a6 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3e   0;..  if( argc>
3a5a7 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72  0 ){.    n = str
3a5a8 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  len(argv[0])+1;.
3a5a9 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75 54 6f    }.  p = (IcuTo
3a5aa 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74 65  kenizer *)sqlite
3a5ab 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3a5ac 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29  IcuTokenizer)+n)
3a5ad 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
3a5ae 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a5af 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
3a5b0 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
3a5b1 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 29 3b  (IcuTokenizer));
3a5b2 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20  ..  if( n ){.   
3a5b3 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28 63   p->zLocale = (c
3a5b4 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  har *)&p[1];.   
3a5b5 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63 61   memcpy(p->zLoca
3a5b6 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e 29 3b  le, argv[0], n);
3a5b7 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
3a5b8 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f  izer = (sqlite3_
3a5b9 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a  tokenizer *)p;..
3a5ba 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a5bb 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
3a5bc 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
3a5bd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a5be 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  cuDestroy(sqlite
3a5bf 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3a5c0 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63 75 54  kenizer){.  IcuT
3a5c1 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49  okenizer *p = (I
3a5c2 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54  cuTokenizer *)pT
3a5c3 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69  okenizer;.  sqli
3a5c4 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72  te3_free(p);.  r
3a5c5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3a5c6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
3a5c7 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e  e to begin token
3a5c8 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  izing a particul
3a5c9 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  ar string.  The 
3a5ca 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20  input.** string 
3a5cb 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
3a5cc 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79  is pInput[0..nBy
3a5cd 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f  tes-1].  A curso
3a5ce 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 63  r.** used to inc
3a5cf 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e  rementally token
3a5d0 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67 20  ize this string 
3a5d1 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 0a  is returned in .
3a5d2 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f  ** *ppCursor..*/
3a5d3 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 4f  .static int icuO
3a5d4 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
3a5d5 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3a5d6 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
3a5d7 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
3a5d8 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3a5d9 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  zInput,         
3a5da 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3a5db 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
3a5dc 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20  int nInput,     
3a5dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5de 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
3a5df 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20 62 79   of zInput in by
3a5e0 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tes */.  sqlite3
3a5e1 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3a5e2 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
3a5e3 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
3a5e4 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
3a5e5 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72  {.  IcuTokenizer
3a5e6 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69   *p = (IcuTokeni
3a5e7 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72  zer *)pTokenizer
3a5e8 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70  ;.  IcuCursor *p
3a5e9 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20 69 6e  Csr;..  const in
3a5ea 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f 46 4f  t32_t opt = U_FO
3a5eb 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 3b  LD_CASE_DEFAULT;
3a5ec 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74  .  UErrorCode st
3a5ed 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
3a5ee 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68 61 72  ROR;.  int nChar
3a5ef 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63 3b 0a  ;..  UChar32 c;.
3a5f0 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d 20 30    int iInput = 0
3a5f1 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d 20 30  ;.  int iOut = 0
3a5f2 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  ;..  *ppCursor =
3a5f3 20 30 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 70 75   0;..  if( nInpu
3a5f4 74 3c 30 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75  t<0 ){.    nInpu
3a5f5 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49 6e 70 75  t = strlen(zInpu
3a5f6 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20  t);.  }.  nChar 
3a5f7 3d 20 6e 49 6e 70 75 74 2b 31 3b 0a 20 20 70 43  = nInput+1;.  pC
3a5f8 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20  sr = (IcuCursor 
3a5f9 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3a5fa 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49  (.      sizeof(I
3a5fb 63 75 43 75 72 73 6f 72 29 20 2b 20 20 20 20 20  cuCursor) +     
3a5fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
3a5fd 75 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20  uCursor */.     
3a5fe 20 6e 43 68 61 72 20 2a 20 73 69 7a 65 6f 66 28   nChar * sizeof(
3a5ff 55 43 68 61 72 29 20 2b 20 20 20 20 20 20 20 20  UChar) +        
3a600 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72      /* IcuCursor
3a601 2e 61 43 68 61 72 5b 5d 20 2a 2f 0a 20 20 20 20  .aChar[] */.    
3a602 20 20 28 6e 43 68 61 72 2b 31 29 20 2a 20 73 69    (nChar+1) * si
3a603 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
3a604 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
3a605 72 2e 61 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a 20  r.aOffset[] */. 
3a606 20 29 3b 0a 20 20 69 66 28 20 21 70 43 73 72 20   );.  if( !pCsr 
3a607 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3a608 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3a609 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30    memset(pCsr, 0
3a60a 2c 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73  , sizeof(IcuCurs
3a60b 6f 72 29 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43  or));.  pCsr->aC
3a60c 68 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29 26  har = (UChar *)&
3a60d 70 43 73 72 5b 31 5d 3b 0a 20 20 70 43 73 72 2d  pCsr[1];.  pCsr-
3a60e 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 20  >aOffset = (int 
3a60f 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b 6e  *)&pCsr->aChar[n
3a610 43 68 61 72 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e  Char];..  pCsr->
3a611 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20  aOffset[iOut] = 
3a612 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f 4e 45 58  iInput;.  U8_NEX
3a613 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75 74  T(zInput, iInput
3a614 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 20 0a 20  , nInput, c); . 
3a615 20 77 68 69 6c 65 28 20 63 3e 30 20 29 7b 0a 20   while( c>0 ){. 
3a616 20 20 20 69 6e 74 20 69 73 45 72 72 6f 72 20 3d     int isError =
3a617 20 30 3b 0a 20 20 20 20 63 20 3d 20 75 5f 66 6f   0;.    c = u_fo
3a618 6c 64 43 61 73 65 28 63 2c 20 6f 70 74 29 3b 0a  ldCase(c, opt);.
3a619 20 20 20 20 55 31 36 5f 41 50 50 45 4e 44 28 70      U16_APPEND(p
3a61a 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74  Csr->aChar, iOut
3a61b 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69 73 45 72  , nChar, c, isEr
3a61c 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 69 73  ror);.    if( is
3a61d 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 73  Error ){.      s
3a61e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
3a61f 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
3a620 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3a621 20 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61 4f    }.    pCsr->aO
3a622 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49  ffset[iOut] = iI
3a623 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66 28 20 69  nput;..    if( i
3a624 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b 0a  Input<nInput ){.
3a625 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 49        U8_NEXT(zI
3a626 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49  nput, iInput, nI
3a627 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 65  nput, c);.    }e
3a628 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 30  lse{.      c = 0
3a629 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
3a62a 43 73 72 2d 3e 70 49 74 65 72 20 3d 20 75 62 72  Csr->pIter = ubr
3a62b 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57 4f 52 44  k_open(UBRK_WORD
3a62c 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43  , p->zLocale, pC
3a62d 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c  sr->aChar, iOut,
3a62e 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28   &status);.  if(
3a62f 20 21 55 5f 53 55 43 43 45 53 53 28 73 74 61 74   !U_SUCCESS(stat
3a630 75 73 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  us) ){.    sqlit
3a631 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
3a632 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3a633 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 43  _ERROR;.  }.  pC
3a634 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69 4f 75 74  sr->nChar = iOut
3a635 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72 73 74 28  ;..  ubrk_first(
3a636 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
3a637 2a 70 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c  *ppCursor = (sql
3a638 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3a639 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20 20  ursor *)pCsr;.  
3a63a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3a63b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
3a63c 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
3a63d 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
3a63e 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
3a63f 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28 29 2e  ll to icuOpen().
3a640 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a641 63 75 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  cuClose(sqlite3_
3a642 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3a643 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 49 63   *pCursor){.  Ic
3a644 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  uCursor *pCsr = 
3a645 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43 75  (IcuCursor *)pCu
3a646 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f  rsor;.  ubrk_clo
3a647 73 65 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  se(pCsr->pIter);
3a648 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3a649 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 29 3b 0a  pCsr->zBuffer);.
3a64a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a64b 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Csr);.  return S
3a64c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3a64d 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e  ** Extract the n
3a64e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61  ext token from a
3a64f 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   tokenization cu
3a650 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
3a651 69 6e 74 20 69 63 75 4e 65 78 74 28 0a 20 20 73  int icuNext(.  s
3a652 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3a653 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3a654 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
3a655 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
3a656 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  pen */.  const c
3a657 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
3a658 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a659 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
3a65a 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
3a65b 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65  */.  int *pnByte
3a65c 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
3a65d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3a65e 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
3a65f 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   in token */.  i
3a660 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
3a661 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
3a662 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74     /* OUT: Start
3a663 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
3a664 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
3a665 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20  EndOffset,      
3a666 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a667 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73  OUT: Ending offs
3a668 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
3a669 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e   int *piPosition
3a66a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a66b 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73       /* OUT: Pos
3a66c 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66  ition integer of
3a66d 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49   token */.){.  I
3a66e 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  cuCursor *pCsr =
3a66f 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43   (IcuCursor *)pC
3a670 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74 20 69 53  ursor;..  int iS
3a671 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
3a672 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  iEnd = 0;.  int 
3a673 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 77 68  nByte = 0;..  wh
3a674 69 6c 65 28 20 69 53 74 61 72 74 3d 3d 69 45 6e  ile( iStart==iEn
3a675 64 20 29 7b 0a 20 20 20 20 55 43 68 61 72 33 32  d ){.    UChar32
3a676 20 63 3b 0a 0a 20 20 20 20 69 53 74 61 72 74 20   c;..    iStart 
3a677 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28 70  = ubrk_current(p
3a678 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20  Csr->pIter);.   
3a679 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65 78   iEnd = ubrk_nex
3a67a 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a  t(pCsr->pIter);.
3a67b 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d 55 42      if( iEnd==UB
3a67c 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  RK_DONE ){.     
3a67d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
3a67e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ONE;.    }..    
3a67f 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45  while( iStart<iE
3a680 6e 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  nd ){.      int 
3a681 69 57 68 69 74 65 20 3d 20 69 53 74 61 72 74 3b  iWhite = iStart;
3a682 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 70  .      U8_NEXT(p
3a683 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 57 68 69  Csr->aChar, iWhi
3a684 74 65 2c 20 70 43 73 72 2d 3e 6e 43 68 61 72 2c  te, pCsr->nChar,
3a685 20 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75   c);.      if( u
3a686 5f 69 73 73 70 61 63 65 28 63 29 20 29 7b 0a 20  _isspace(c) ){. 
3a687 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20         iStart = 
3a688 69 57 68 69 74 65 3b 0a 20 20 20 20 20 20 7d 65  iWhite;.      }e
3a689 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65  lse{.        bre
3a68a 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
3a68b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 69 53 74  }.    assert(iSt
3a68c 61 72 74 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a  art<=iEnd);.  }.
3a68d 0a 20 20 64 6f 20 7b 0a 20 20 20 20 55 45 72 72  .  do {.    UErr
3a68e 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
3a68f 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20  U_ZERO_ERROR;.  
3a690 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20    if( nByte ){. 
3a691 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20       char *zNew 
3a692 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
3a693 63 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c  c(pCsr->zBuffer,
3a694 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69   nByte);.      i
3a695 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
3a696 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3a697 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
3a698 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75  .      pCsr->zBu
3a699 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20 20 20  ffer = zNew;.   
3a69a 20 20 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72     pCsr->nBuffer
3a69b 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a   = nByte;.    }.
3a69c 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 54 46 38  .    u_strToUTF8
3a69d 28 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e  (.        pCsr->
3a69e 7a 42 75 66 66 65 72 2c 20 70 43 73 72 2d 3e 6e  zBuffer, pCsr->n
3a69f 42 75 66 66 65 72 2c 20 26 6e 42 79 74 65 2c 20  Buffer, &nByte, 
3a6a0 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 76 61 72     /* Output var
3a6a1 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 70 43  s */.        &pC
3a6a2 73 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72 74  sr->aChar[iStart
3a6a3 5d 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c 20  ], iEnd-iStart, 
3a6a4 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 76        /* Input v
3a6a5 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  ars */.        &
3a6a6 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
3a6a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6a8 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
3a6a9 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72  t success/failur
3a6aa 65 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 7d 20  e */.    );.  } 
3a6ab 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70 43 73  while( nByte>pCs
3a6ac 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0a 0a 20  r->nBuffer );.. 
3a6ad 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 70 43 73 72   *ppToken = pCsr
3a6ae 2d 3e 7a 42 75 66 66 65 72 3b 0a 20 20 2a 70 6e  ->zBuffer;.  *pn
3a6af 42 79 74 65 73 20 3d 20 6e 42 79 74 65 3b 0a 20  Bytes = nByte;. 
3a6b0 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20   *piStartOffset 
3a6b1 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b  = pCsr->aOffset[
3a6b2 69 53 74 61 72 74 5d 3b 0a 20 20 2a 70 69 45 6e  iStart];.  *piEn
3a6b3 64 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e  dOffset = pCsr->
3a6b4 61 4f 66 66 73 65 74 5b 69 45 6e 64 5d 3b 0a 20  aOffset[iEnd];. 
3a6b5 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 70   *piPosition = p
3a6b6 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a  Csr->iToken++;..
3a6b7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a6b8 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
3a6b9 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
3a6ba 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
3a6bb 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  the simple token
3a6bc 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63  izer.*/.static c
3a6bd 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
3a6be 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 63  enizer_module ic
3a6bf 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3a6c0 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20   = {.  0,       
3a6c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c2 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
3a6c3 2a 2f 0a 20 20 69 63 75 43 72 65 61 74 65 2c 20  */.  icuCreate, 
3a6c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c5 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f    /* xCreate  */
3a6c6 0a 20 20 69 63 75 44 65 73 74 72 6f 79 2c 20 20  .  icuDestroy,  
3a6c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c8 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20  /* xCreate  */. 
3a6c9 20 69 63 75 4f 70 65 6e 2c 20 20 20 20 20 20 20   icuOpen,       
3a6ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a6cb 20 78 4f 70 65 6e 20 20 20 20 2a 2f 0a 20 20 69   xOpen    */.  i
3a6cc 63 75 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  cuClose,        
3a6cd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a6ce 43 6c 6f 73 65 20 20 20 2a 2f 0a 20 20 69 63 75  Close   */.  icu
3a6cf 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
3a6d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65            /* xNe
3a6d1 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  xt    */.};../*.
3a6d2 2a 2a 20 53 65 74 20 2a 70 70 4d 6f 64 75 6c 65  ** Set *ppModule
3a6d3 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
3a6d4 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3a6d5 6f 66 20 74 68 65 20 49 43 55 20 74 6f 6b 65 6e  of the ICU token
3a6d6 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  izer..*/.SQLITE_
3a6d7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3a6d8 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e  ite3Fts3IcuToken
3a6d9 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
3a6da 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3a6db 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
3a6dc 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
3a6dd 6f 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b 65  odule = &icuToke
3a6de 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a  nizerModule;.}..
3a6df 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
3a6e0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
3a6e1 49 43 55 29 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  ICU) */.#endif /
3a6e2 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3a6e3 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3a6e4 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3a6e5 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3a6e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3a6e7 66 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a  f fts3_icu.c ***
3a6e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a6e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a6ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a                 *********/.